drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1 | /* |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2 | ** 2001 September 15 |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 7 | ** May you do good and not evil. |
| 8 | ** May you find forgiveness for yourself and forgive others. |
| 9 | ** May you share freely, never taking more than you give. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 10 | ** |
| 11 | ************************************************************************* |
| 12 | ** This module contains C code that generates VDBE code used to process |
drh | 909626d | 2008-05-30 14:58:37 +0000 | [diff] [blame] | 13 | ** the WHERE clause of SQL statements. This module is responsible for |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 14 | ** generating the code that loops through a table looking for applicable |
| 15 | ** rows. Indices are selected and used to speed the search when doing |
| 16 | ** so is applicable. Because this module is responsible for selecting |
| 17 | ** indices, you might also think of this module as the "query optimizer". |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 18 | */ |
| 19 | #include "sqliteInt.h" |
drh | e54df42 | 2013-11-12 18:37:25 +0000 | [diff] [blame] | 20 | #include "whereInt.h" |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 21 | |
| 22 | /* |
drh | 6f32848 | 2013-06-05 23:39:34 +0000 | [diff] [blame] | 23 | ** Return the estimated number of output rows from a WHERE clause |
| 24 | */ |
drh | c63367e | 2013-06-10 20:46:50 +0000 | [diff] [blame] | 25 | u64 sqlite3WhereOutputRowCount(WhereInfo *pWInfo){ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 26 | return sqlite3LogEstToInt(pWInfo->nRowOut); |
drh | 6f32848 | 2013-06-05 23:39:34 +0000 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | /* |
| 30 | ** Return one of the WHERE_DISTINCT_xxxxx values to indicate how this |
| 31 | ** WHERE clause returns outputs for DISTINCT processing. |
| 32 | */ |
| 33 | int sqlite3WhereIsDistinct(WhereInfo *pWInfo){ |
| 34 | return pWInfo->eDistinct; |
| 35 | } |
| 36 | |
| 37 | /* |
| 38 | ** Return TRUE if the WHERE clause returns rows in ORDER BY order. |
| 39 | ** Return FALSE if the output needs to be sorted. |
| 40 | */ |
| 41 | int sqlite3WhereIsOrdered(WhereInfo *pWInfo){ |
drh | ddba0c2 | 2014-03-18 20:33:42 +0000 | [diff] [blame] | 42 | return pWInfo->nOBSat; |
drh | 6f32848 | 2013-06-05 23:39:34 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | /* |
| 46 | ** Return the VDBE address or label to jump to in order to continue |
| 47 | ** immediately with the next row of a WHERE clause. |
| 48 | */ |
| 49 | int sqlite3WhereContinueLabel(WhereInfo *pWInfo){ |
drh | a22a75e | 2014-03-21 18:16:23 +0000 | [diff] [blame] | 50 | assert( pWInfo->iContinue!=0 ); |
drh | 6f32848 | 2013-06-05 23:39:34 +0000 | [diff] [blame] | 51 | return pWInfo->iContinue; |
| 52 | } |
| 53 | |
| 54 | /* |
| 55 | ** Return the VDBE address or label to jump to in order to break |
| 56 | ** out of a WHERE loop. |
| 57 | */ |
| 58 | int sqlite3WhereBreakLabel(WhereInfo *pWInfo){ |
| 59 | return pWInfo->iBreak; |
| 60 | } |
| 61 | |
| 62 | /* |
| 63 | ** Return TRUE if an UPDATE or DELETE statement can operate directly on |
| 64 | ** the rowids returned by a WHERE clause. Return FALSE if doing an |
| 65 | ** UPDATE or DELETE might change subsequent WHERE clause results. |
drh | fc8d4f9 | 2013-11-08 15:19:46 +0000 | [diff] [blame] | 66 | ** |
| 67 | ** If the ONEPASS optimization is used (if this routine returns true) |
| 68 | ** then also write the indices of open cursors used by ONEPASS |
| 69 | ** into aiCur[0] and aiCur[1]. iaCur[0] gets the cursor of the data |
| 70 | ** table and iaCur[1] gets the cursor used by an auxiliary index. |
| 71 | ** Either value may be -1, indicating that cursor is not used. |
| 72 | ** Any cursors returned will have been opened for writing. |
| 73 | ** |
| 74 | ** aiCur[0] and aiCur[1] both get -1 if the where-clause logic is |
| 75 | ** unable to use the ONEPASS optimization. |
drh | 6f32848 | 2013-06-05 23:39:34 +0000 | [diff] [blame] | 76 | */ |
drh | fc8d4f9 | 2013-11-08 15:19:46 +0000 | [diff] [blame] | 77 | int sqlite3WhereOkOnePass(WhereInfo *pWInfo, int *aiCur){ |
| 78 | memcpy(aiCur, pWInfo->aiCurOnePass, sizeof(int)*2); |
drh | 6f32848 | 2013-06-05 23:39:34 +0000 | [diff] [blame] | 79 | return pWInfo->okOnePass; |
| 80 | } |
| 81 | |
| 82 | /* |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 83 | ** Move the content of pSrc into pDest |
| 84 | */ |
| 85 | static void whereOrMove(WhereOrSet *pDest, WhereOrSet *pSrc){ |
| 86 | pDest->n = pSrc->n; |
| 87 | memcpy(pDest->a, pSrc->a, pDest->n*sizeof(pDest->a[0])); |
| 88 | } |
| 89 | |
| 90 | /* |
| 91 | ** Try to insert a new prerequisite/cost entry into the WhereOrSet pSet. |
| 92 | ** |
| 93 | ** The new entry might overwrite an existing entry, or it might be |
| 94 | ** appended, or it might be discarded. Do whatever is the right thing |
| 95 | ** so that pSet keeps the N_OR_COST best entries seen so far. |
| 96 | */ |
| 97 | static int whereOrInsert( |
| 98 | WhereOrSet *pSet, /* The WhereOrSet to be updated */ |
| 99 | Bitmask prereq, /* Prerequisites of the new entry */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 100 | LogEst rRun, /* Run-cost of the new entry */ |
| 101 | LogEst nOut /* Number of outputs for the new entry */ |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 102 | ){ |
| 103 | u16 i; |
| 104 | WhereOrCost *p; |
| 105 | for(i=pSet->n, p=pSet->a; i>0; i--, p++){ |
| 106 | if( rRun<=p->rRun && (prereq & p->prereq)==prereq ){ |
| 107 | goto whereOrInsert_done; |
| 108 | } |
| 109 | if( p->rRun<=rRun && (p->prereq & prereq)==p->prereq ){ |
| 110 | return 0; |
| 111 | } |
| 112 | } |
| 113 | if( pSet->n<N_OR_COST ){ |
| 114 | p = &pSet->a[pSet->n++]; |
| 115 | p->nOut = nOut; |
| 116 | }else{ |
| 117 | p = pSet->a; |
| 118 | for(i=1; i<pSet->n; i++){ |
| 119 | if( p->rRun>pSet->a[i].rRun ) p = pSet->a + i; |
| 120 | } |
| 121 | if( p->rRun<=rRun ) return 0; |
| 122 | } |
| 123 | whereOrInsert_done: |
| 124 | p->prereq = prereq; |
| 125 | p->rRun = rRun; |
| 126 | if( p->nOut>nOut ) p->nOut = nOut; |
| 127 | return 1; |
| 128 | } |
| 129 | |
| 130 | /* |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 131 | ** Initialize a preallocated WhereClause structure. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 132 | */ |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 133 | static void whereClauseInit( |
| 134 | WhereClause *pWC, /* The WhereClause to be initialized */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 135 | WhereInfo *pWInfo /* The WHERE processing context */ |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 136 | ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 137 | pWC->pWInfo = pWInfo; |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 138 | pWC->pOuter = 0; |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 139 | pWC->nTerm = 0; |
drh | cad651e | 2007-04-20 12:22:01 +0000 | [diff] [blame] | 140 | pWC->nSlot = ArraySize(pWC->aStatic); |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 141 | pWC->a = pWC->aStatic; |
| 142 | } |
| 143 | |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 144 | /* Forward reference */ |
| 145 | static void whereClauseClear(WhereClause*); |
| 146 | |
| 147 | /* |
| 148 | ** Deallocate all memory associated with a WhereOrInfo object. |
| 149 | */ |
| 150 | static void whereOrInfoDelete(sqlite3 *db, WhereOrInfo *p){ |
drh | 5bd98ae | 2009-01-07 18:24:03 +0000 | [diff] [blame] | 151 | whereClauseClear(&p->wc); |
| 152 | sqlite3DbFree(db, p); |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | /* |
| 156 | ** Deallocate all memory associated with a WhereAndInfo object. |
| 157 | */ |
| 158 | static void whereAndInfoDelete(sqlite3 *db, WhereAndInfo *p){ |
drh | 5bd98ae | 2009-01-07 18:24:03 +0000 | [diff] [blame] | 159 | whereClauseClear(&p->wc); |
| 160 | sqlite3DbFree(db, p); |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 161 | } |
| 162 | |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 163 | /* |
| 164 | ** Deallocate a WhereClause structure. The WhereClause structure |
| 165 | ** itself is not freed. This routine is the inverse of whereClauseInit(). |
| 166 | */ |
| 167 | static void whereClauseClear(WhereClause *pWC){ |
| 168 | int i; |
| 169 | WhereTerm *a; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 170 | sqlite3 *db = pWC->pWInfo->pParse->db; |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 171 | for(i=pWC->nTerm-1, a=pWC->a; i>=0; i--, a++){ |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 172 | if( a->wtFlags & TERM_DYNAMIC ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 173 | sqlite3ExprDelete(db, a->pExpr); |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 174 | } |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 175 | if( a->wtFlags & TERM_ORINFO ){ |
| 176 | whereOrInfoDelete(db, a->u.pOrInfo); |
| 177 | }else if( a->wtFlags & TERM_ANDINFO ){ |
| 178 | whereAndInfoDelete(db, a->u.pAndInfo); |
| 179 | } |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 180 | } |
| 181 | if( pWC->a!=pWC->aStatic ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 182 | sqlite3DbFree(db, pWC->a); |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 183 | } |
| 184 | } |
| 185 | |
| 186 | /* |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 187 | ** Add a single new WhereTerm entry to the WhereClause object pWC. |
| 188 | ** The new WhereTerm object is constructed from Expr p and with wtFlags. |
| 189 | ** The index in pWC->a[] of the new WhereTerm is returned on success. |
| 190 | ** 0 is returned if the new WhereTerm could not be added due to a memory |
| 191 | ** allocation error. The memory allocation failure will be recorded in |
| 192 | ** the db->mallocFailed flag so that higher-level functions can detect it. |
| 193 | ** |
| 194 | ** This routine will increase the size of the pWC->a[] array as necessary. |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 195 | ** |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 196 | ** If the wtFlags argument includes TERM_DYNAMIC, then responsibility |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 197 | ** for freeing the expression p is assumed by the WhereClause object pWC. |
| 198 | ** This is true even if this routine fails to allocate a new WhereTerm. |
drh | b63a53d | 2007-03-31 01:34:44 +0000 | [diff] [blame] | 199 | ** |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 200 | ** WARNING: This routine might reallocate the space used to store |
drh | 909626d | 2008-05-30 14:58:37 +0000 | [diff] [blame] | 201 | ** WhereTerms. All pointers to WhereTerms should be invalidated after |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 202 | ** calling this routine. Such pointers may be reinitialized by referencing |
| 203 | ** the pWC->a[] array. |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 204 | */ |
drh | f07cf6e | 2015-03-06 16:45:16 +0000 | [diff] [blame] | 205 | static int whereClauseInsert(WhereClause *pWC, Expr *p, u16 wtFlags){ |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 206 | WhereTerm *pTerm; |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 207 | int idx; |
drh | 3975974 | 2013-08-02 23:40:45 +0000 | [diff] [blame] | 208 | testcase( wtFlags & TERM_VIRTUAL ); |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 209 | if( pWC->nTerm>=pWC->nSlot ){ |
| 210 | WhereTerm *pOld = pWC->a; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 211 | sqlite3 *db = pWC->pWInfo->pParse->db; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 212 | pWC->a = sqlite3DbMallocRaw(db, sizeof(pWC->a[0])*pWC->nSlot*2 ); |
drh | b63a53d | 2007-03-31 01:34:44 +0000 | [diff] [blame] | 213 | if( pWC->a==0 ){ |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 214 | if( wtFlags & TERM_DYNAMIC ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 215 | sqlite3ExprDelete(db, p); |
drh | b63a53d | 2007-03-31 01:34:44 +0000 | [diff] [blame] | 216 | } |
drh | f998b73 | 2007-11-26 13:36:00 +0000 | [diff] [blame] | 217 | pWC->a = pOld; |
drh | b63a53d | 2007-03-31 01:34:44 +0000 | [diff] [blame] | 218 | return 0; |
| 219 | } |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 220 | memcpy(pWC->a, pOld, sizeof(pWC->a[0])*pWC->nTerm); |
| 221 | if( pOld!=pWC->aStatic ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 222 | sqlite3DbFree(db, pOld); |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 223 | } |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 224 | pWC->nSlot = sqlite3DbMallocSize(db, pWC->a)/sizeof(pWC->a[0]); |
drh | fe32daa | 2014-12-05 19:50:58 +0000 | [diff] [blame] | 225 | 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] | 226 | } |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 227 | pTerm = &pWC->a[idx = pWC->nTerm++]; |
drh | a4c3c87 | 2013-09-12 17:29:25 +0000 | [diff] [blame] | 228 | if( p && ExprHasProperty(p, EP_Unlikely) ){ |
drh | d05ab6a | 2014-10-25 13:42:16 +0000 | [diff] [blame] | 229 | pTerm->truthProb = sqlite3LogEst(p->iTable) - 270; |
drh | cca9f3d | 2013-09-06 15:23:29 +0000 | [diff] [blame] | 230 | }else{ |
dan | aa9933c | 2014-04-24 20:04:49 +0000 | [diff] [blame] | 231 | pTerm->truthProb = 1; |
drh | cca9f3d | 2013-09-06 15:23:29 +0000 | [diff] [blame] | 232 | } |
drh | 7ee751d | 2012-12-19 15:53:51 +0000 | [diff] [blame] | 233 | pTerm->pExpr = sqlite3ExprSkipCollate(p); |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 234 | pTerm->wtFlags = wtFlags; |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 235 | pTerm->pWC = pWC; |
drh | 45b1ee4 | 2005-08-02 17:48:22 +0000 | [diff] [blame] | 236 | pTerm->iParent = -1; |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 237 | return idx; |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 238 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 239 | |
| 240 | /* |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 241 | ** This routine identifies subexpressions in the WHERE clause where |
drh | b6fb62d | 2005-09-20 08:47:20 +0000 | [diff] [blame] | 242 | ** each subexpression is separated by the AND operator or some other |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 243 | ** operator specified in the op parameter. The WhereClause structure |
| 244 | ** is filled with pointers to subexpressions. For example: |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 245 | ** |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 246 | ** WHERE a=='hello' AND coalesce(b,11)<10 AND (c+12!=d OR c==22) |
| 247 | ** \________/ \_______________/ \________________/ |
| 248 | ** slot[0] slot[1] slot[2] |
| 249 | ** |
| 250 | ** The original WHERE clause in pExpr is unaltered. All this routine |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 251 | ** does is make slot[] entries point to substructure within pExpr. |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 252 | ** |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 253 | ** In the previous sentence and in the diagram, "slot[]" refers to |
drh | 902b9ee | 2008-12-05 17:17:07 +0000 | [diff] [blame] | 254 | ** the WhereClause.a[] array. The slot[] array grows as needed to contain |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 255 | ** all terms of the WHERE clause. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 256 | */ |
drh | 74f91d4 | 2013-06-19 18:01:44 +0000 | [diff] [blame] | 257 | static void whereSplit(WhereClause *pWC, Expr *pExpr, u8 op){ |
drh | 0f517ea | 2015-04-21 02:12:13 +0000 | [diff] [blame] | 258 | Expr *pE2 = sqlite3ExprSkipCollate(pExpr); |
drh | 74f91d4 | 2013-06-19 18:01:44 +0000 | [diff] [blame] | 259 | pWC->op = op; |
drh | 0f517ea | 2015-04-21 02:12:13 +0000 | [diff] [blame] | 260 | if( pE2==0 ) return; |
| 261 | if( pE2->op!=op ){ |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 262 | whereClauseInsert(pWC, pExpr, 0); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 263 | }else{ |
drh | 0f517ea | 2015-04-21 02:12:13 +0000 | [diff] [blame] | 264 | whereSplit(pWC, pE2->pLeft, op); |
| 265 | whereSplit(pWC, pE2->pRight, op); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 266 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | /* |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 270 | ** Initialize a WhereMaskSet object |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 271 | */ |
drh | fd5874d | 2013-06-12 14:52:39 +0000 | [diff] [blame] | 272 | #define initMaskSet(P) (P)->n=0 |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 273 | |
| 274 | /* |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 275 | ** Return the bitmask for the given cursor number. Return 0 if |
| 276 | ** iCursor is not in the set. |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 277 | */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 278 | static Bitmask getMask(WhereMaskSet *pMaskSet, int iCursor){ |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 279 | int i; |
drh | fcd71b6 | 2011-04-05 22:08:24 +0000 | [diff] [blame] | 280 | assert( pMaskSet->n<=(int)sizeof(Bitmask)*8 ); |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 281 | for(i=0; i<pMaskSet->n; i++){ |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 282 | if( pMaskSet->ix[i]==iCursor ){ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 283 | return MASKBIT(i); |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 284 | } |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 285 | } |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 286 | return 0; |
| 287 | } |
| 288 | |
| 289 | /* |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 290 | ** Create a new mask for cursor iCursor. |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 291 | ** |
| 292 | ** There is one cursor per table in the FROM clause. The number of |
| 293 | ** tables in the FROM clause is limited by a test early in the |
drh | b6fb62d | 2005-09-20 08:47:20 +0000 | [diff] [blame] | 294 | ** sqlite3WhereBegin() routine. So we know that the pMaskSet->ix[] |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 295 | ** array will never overflow. |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 296 | */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 297 | static void createMask(WhereMaskSet *pMaskSet, int iCursor){ |
drh | cad651e | 2007-04-20 12:22:01 +0000 | [diff] [blame] | 298 | assert( pMaskSet->n < ArraySize(pMaskSet->ix) ); |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 299 | pMaskSet->ix[pMaskSet->n++] = iCursor; |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | /* |
drh | 4a6fc35 | 2013-08-07 01:18:38 +0000 | [diff] [blame] | 303 | ** These routines walk (recursively) an expression tree and generate |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 304 | ** a bitmask indicating which tables are used in that expression |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 305 | ** tree. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 306 | */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 307 | static Bitmask exprListTableUsage(WhereMaskSet*, ExprList*); |
| 308 | static Bitmask exprSelectTableUsage(WhereMaskSet*, Select*); |
| 309 | static Bitmask exprTableUsage(WhereMaskSet *pMaskSet, Expr *p){ |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 310 | Bitmask mask = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 311 | if( p==0 ) return 0; |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 312 | if( p->op==TK_COLUMN ){ |
drh | 8feb4b1 | 2004-07-19 02:12:14 +0000 | [diff] [blame] | 313 | mask = getMask(pMaskSet, p->iTable); |
drh | 8feb4b1 | 2004-07-19 02:12:14 +0000 | [diff] [blame] | 314 | return mask; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 315 | } |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 316 | mask = exprTableUsage(pMaskSet, p->pRight); |
| 317 | mask |= exprTableUsage(pMaskSet, p->pLeft); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 318 | if( ExprHasProperty(p, EP_xIsSelect) ){ |
| 319 | mask |= exprSelectTableUsage(pMaskSet, p->x.pSelect); |
| 320 | }else{ |
| 321 | mask |= exprListTableUsage(pMaskSet, p->x.pList); |
| 322 | } |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 323 | return mask; |
| 324 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 325 | static Bitmask exprListTableUsage(WhereMaskSet *pMaskSet, ExprList *pList){ |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 326 | int i; |
| 327 | Bitmask mask = 0; |
| 328 | if( pList ){ |
| 329 | for(i=0; i<pList->nExpr; i++){ |
| 330 | mask |= exprTableUsage(pMaskSet, pList->a[i].pExpr); |
drh | dd57912 | 2002-04-02 01:58:57 +0000 | [diff] [blame] | 331 | } |
| 332 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 333 | return mask; |
| 334 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 335 | static Bitmask exprSelectTableUsage(WhereMaskSet *pMaskSet, Select *pS){ |
drh | a430ae8 | 2007-09-12 15:41:01 +0000 | [diff] [blame] | 336 | Bitmask mask = 0; |
| 337 | while( pS ){ |
drh | a464c23 | 2011-09-16 19:04:03 +0000 | [diff] [blame] | 338 | SrcList *pSrc = pS->pSrc; |
drh | a430ae8 | 2007-09-12 15:41:01 +0000 | [diff] [blame] | 339 | mask |= exprListTableUsage(pMaskSet, pS->pEList); |
drh | f5b1138 | 2005-09-17 13:07:13 +0000 | [diff] [blame] | 340 | mask |= exprListTableUsage(pMaskSet, pS->pGroupBy); |
| 341 | mask |= exprListTableUsage(pMaskSet, pS->pOrderBy); |
| 342 | mask |= exprTableUsage(pMaskSet, pS->pWhere); |
| 343 | mask |= exprTableUsage(pMaskSet, pS->pHaving); |
drh | a464c23 | 2011-09-16 19:04:03 +0000 | [diff] [blame] | 344 | if( ALWAYS(pSrc!=0) ){ |
drh | 8850177 | 2011-09-16 17:43:06 +0000 | [diff] [blame] | 345 | int i; |
| 346 | for(i=0; i<pSrc->nSrc; i++){ |
| 347 | mask |= exprSelectTableUsage(pMaskSet, pSrc->a[i].pSelect); |
| 348 | mask |= exprTableUsage(pMaskSet, pSrc->a[i].pOn); |
| 349 | } |
| 350 | } |
drh | a430ae8 | 2007-09-12 15:41:01 +0000 | [diff] [blame] | 351 | pS = pS->pPrior; |
drh | f5b1138 | 2005-09-17 13:07:13 +0000 | [diff] [blame] | 352 | } |
| 353 | return mask; |
| 354 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 355 | |
| 356 | /* |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 357 | ** Return TRUE if the given operator is one of the operators that is |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 358 | ** allowed for an indexable WHERE clause term. The allowed operators are |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 359 | ** "=", "<", ">", "<=", ">=", "IN", and "IS NULL" |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 360 | */ |
| 361 | static int allowedOp(int op){ |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 362 | assert( TK_GT>TK_EQ && TK_GT<TK_GE ); |
| 363 | assert( TK_LT>TK_EQ && TK_LT<TK_GE ); |
| 364 | assert( TK_LE>TK_EQ && TK_LE<TK_GE ); |
| 365 | assert( TK_GE==TK_EQ+4 ); |
drh | fcd4953 | 2015-05-13 15:24:07 +0000 | [diff] [blame] | 366 | 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] | 367 | } |
| 368 | |
| 369 | /* |
drh | 909626d | 2008-05-30 14:58:37 +0000 | [diff] [blame] | 370 | ** Commute a comparison operator. Expressions of the form "X op Y" |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 371 | ** are converted into "Y op X". |
danielk1977 | eb5453d | 2007-07-30 14:40:48 +0000 | [diff] [blame] | 372 | ** |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 373 | ** If left/right precedence rules come into play when determining the |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 374 | ** collating sequence, then COLLATE operators are adjusted to ensure |
| 375 | ** that the collating sequence does not change. For example: |
| 376 | ** "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] | 377 | ** the left hand side of a comparison overrides any collation sequence |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 378 | ** attached to the right. For the same reason the EP_Collate flag |
danielk1977 | eb5453d | 2007-07-30 14:40:48 +0000 | [diff] [blame] | 379 | ** is not commuted. |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 380 | */ |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 381 | static void exprCommute(Parse *pParse, Expr *pExpr){ |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 382 | u16 expRight = (pExpr->pRight->flags & EP_Collate); |
| 383 | u16 expLeft = (pExpr->pLeft->flags & EP_Collate); |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 384 | assert( allowedOp(pExpr->op) && pExpr->op!=TK_IN ); |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 385 | if( expRight==expLeft ){ |
| 386 | /* Either X and Y both have COLLATE operator or neither do */ |
| 387 | if( expRight ){ |
| 388 | /* Both X and Y have COLLATE operators. Make sure X is always |
| 389 | ** used by clearing the EP_Collate flag from Y. */ |
| 390 | pExpr->pRight->flags &= ~EP_Collate; |
| 391 | }else if( sqlite3ExprCollSeq(pParse, pExpr->pLeft)!=0 ){ |
| 392 | /* Neither X nor Y have COLLATE operators, but X has a non-default |
| 393 | ** collating sequence. So add the EP_Collate marker on X to cause |
| 394 | ** it to be searched first. */ |
| 395 | pExpr->pLeft->flags |= EP_Collate; |
| 396 | } |
| 397 | } |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 398 | SWAP(Expr*,pExpr->pRight,pExpr->pLeft); |
| 399 | if( pExpr->op>=TK_GT ){ |
| 400 | assert( TK_LT==TK_GT+2 ); |
| 401 | assert( TK_GE==TK_LE+2 ); |
| 402 | assert( TK_GT>TK_EQ ); |
| 403 | assert( TK_GT<TK_LE ); |
| 404 | assert( pExpr->op>=TK_GT && pExpr->op<=TK_GE ); |
| 405 | pExpr->op = ((pExpr->op-TK_GT)^2)+TK_GT; |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 406 | } |
drh | 193bd77 | 2004-07-20 18:23:14 +0000 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | /* |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 410 | ** Translate from TK_xx operator to WO_xx bitmask. |
| 411 | */ |
drh | ec1724e | 2008-12-09 01:32:03 +0000 | [diff] [blame] | 412 | static u16 operatorMask(int op){ |
| 413 | u16 c; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 414 | assert( allowedOp(op) ); |
| 415 | if( op==TK_IN ){ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 416 | c = WO_IN; |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 417 | }else if( op==TK_ISNULL ){ |
| 418 | c = WO_ISNULL; |
drh | fcd4953 | 2015-05-13 15:24:07 +0000 | [diff] [blame] | 419 | }else if( op==TK_IS ){ |
drh | e8d0c61 | 2015-05-14 01:05:25 +0000 | [diff] [blame] | 420 | c = WO_IS; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 421 | }else{ |
drh | ec1724e | 2008-12-09 01:32:03 +0000 | [diff] [blame] | 422 | assert( (WO_EQ<<(op-TK_EQ)) < 0x7fff ); |
| 423 | c = (u16)(WO_EQ<<(op-TK_EQ)); |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 424 | } |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 425 | assert( op!=TK_ISNULL || c==WO_ISNULL ); |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 426 | assert( op!=TK_IN || c==WO_IN ); |
| 427 | assert( op!=TK_EQ || c==WO_EQ ); |
| 428 | assert( op!=TK_LT || c==WO_LT ); |
| 429 | assert( op!=TK_LE || c==WO_LE ); |
| 430 | assert( op!=TK_GT || c==WO_GT ); |
| 431 | assert( op!=TK_GE || c==WO_GE ); |
drh | e8d0c61 | 2015-05-14 01:05:25 +0000 | [diff] [blame] | 432 | assert( op!=TK_IS || c==WO_IS ); |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 433 | return c; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | /* |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 437 | ** Advance to the next WhereTerm that matches according to the criteria |
| 438 | ** established when the pScan object was initialized by whereScanInit(). |
| 439 | ** Return NULL if there are no more matching WhereTerms. |
| 440 | */ |
dan | b2cfc14 | 2013-07-05 11:10:54 +0000 | [diff] [blame] | 441 | static WhereTerm *whereScanNext(WhereScan *pScan){ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 442 | int iCur; /* The cursor on the LHS of the term */ |
| 443 | int iColumn; /* The column on the LHS of the term. -1 for IPK */ |
| 444 | Expr *pX; /* An expression being tested */ |
| 445 | WhereClause *pWC; /* Shorthand for pScan->pWC */ |
| 446 | WhereTerm *pTerm; /* The term being tested */ |
drh | 43b85ef | 2013-06-10 12:34:45 +0000 | [diff] [blame] | 447 | int k = pScan->k; /* Where to start scanning */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 448 | |
| 449 | while( pScan->iEquiv<=pScan->nEquiv ){ |
| 450 | iCur = pScan->aEquiv[pScan->iEquiv-2]; |
| 451 | iColumn = pScan->aEquiv[pScan->iEquiv-1]; |
| 452 | while( (pWC = pScan->pWC)!=0 ){ |
drh | 43b85ef | 2013-06-10 12:34:45 +0000 | [diff] [blame] | 453 | for(pTerm=pWC->a+k; k<pWC->nTerm; k++, pTerm++){ |
drh | e1a086e | 2013-10-28 20:15:56 +0000 | [diff] [blame] | 454 | if( pTerm->leftCursor==iCur |
| 455 | && pTerm->u.leftColumn==iColumn |
| 456 | && (pScan->iEquiv<=2 || !ExprHasProperty(pTerm->pExpr, EP_FromJoin)) |
| 457 | ){ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 458 | if( (pTerm->eOperator & WO_EQUIV)!=0 |
| 459 | && pScan->nEquiv<ArraySize(pScan->aEquiv) |
| 460 | ){ |
| 461 | int j; |
| 462 | pX = sqlite3ExprSkipCollate(pTerm->pExpr->pRight); |
| 463 | assert( pX->op==TK_COLUMN ); |
| 464 | for(j=0; j<pScan->nEquiv; j+=2){ |
| 465 | if( pScan->aEquiv[j]==pX->iTable |
| 466 | && pScan->aEquiv[j+1]==pX->iColumn ){ |
| 467 | break; |
| 468 | } |
| 469 | } |
| 470 | if( j==pScan->nEquiv ){ |
| 471 | pScan->aEquiv[j] = pX->iTable; |
| 472 | pScan->aEquiv[j+1] = pX->iColumn; |
| 473 | pScan->nEquiv += 2; |
| 474 | } |
| 475 | } |
| 476 | if( (pTerm->eOperator & pScan->opMask)!=0 ){ |
| 477 | /* Verify the affinity and collating sequence match */ |
| 478 | if( pScan->zCollName && (pTerm->eOperator & WO_ISNULL)==0 ){ |
| 479 | CollSeq *pColl; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 480 | Parse *pParse = pWC->pWInfo->pParse; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 481 | pX = pTerm->pExpr; |
| 482 | if( !sqlite3IndexAffinityOk(pX, pScan->idxaff) ){ |
| 483 | continue; |
| 484 | } |
| 485 | assert(pX->pLeft); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 486 | pColl = sqlite3BinaryCompareCollSeq(pParse, |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 487 | pX->pLeft, pX->pRight); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 488 | if( pColl==0 ) pColl = pParse->db->pDfltColl; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 489 | if( sqlite3StrICmp(pColl->zName, pScan->zCollName) ){ |
| 490 | continue; |
| 491 | } |
| 492 | } |
drh | e8d0c61 | 2015-05-14 01:05:25 +0000 | [diff] [blame] | 493 | if( (pTerm->eOperator & (WO_EQ|WO_IS))!=0 |
drh | a184fb8 | 2013-05-08 04:22:59 +0000 | [diff] [blame] | 494 | && (pX = pTerm->pExpr->pRight)->op==TK_COLUMN |
| 495 | && pX->iTable==pScan->aEquiv[0] |
| 496 | && pX->iColumn==pScan->aEquiv[1] |
| 497 | ){ |
drh | e8d0c61 | 2015-05-14 01:05:25 +0000 | [diff] [blame] | 498 | testcase( pTerm->eOperator & WO_IS ); |
drh | a184fb8 | 2013-05-08 04:22:59 +0000 | [diff] [blame] | 499 | continue; |
| 500 | } |
drh | 43b85ef | 2013-06-10 12:34:45 +0000 | [diff] [blame] | 501 | pScan->k = k+1; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 502 | return pTerm; |
| 503 | } |
| 504 | } |
| 505 | } |
drh | ad01d89 | 2013-06-19 13:59:49 +0000 | [diff] [blame] | 506 | pScan->pWC = pScan->pWC->pOuter; |
drh | 43b85ef | 2013-06-10 12:34:45 +0000 | [diff] [blame] | 507 | k = 0; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 508 | } |
| 509 | pScan->pWC = pScan->pOrigWC; |
drh | 43b85ef | 2013-06-10 12:34:45 +0000 | [diff] [blame] | 510 | k = 0; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 511 | pScan->iEquiv += 2; |
| 512 | } |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 513 | return 0; |
| 514 | } |
| 515 | |
| 516 | /* |
| 517 | ** Initialize a WHERE clause scanner object. Return a pointer to the |
| 518 | ** first match. Return NULL if there are no matches. |
| 519 | ** |
| 520 | ** The scanner will be searching the WHERE clause pWC. It will look |
| 521 | ** for terms of the form "X <op> <expr>" where X is column iColumn of table |
| 522 | ** iCur. The <op> must be one of the operators described by opMask. |
| 523 | ** |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 524 | ** If the search is for X and the WHERE clause contains terms of the |
| 525 | ** form X=Y then this routine might also return terms of the form |
| 526 | ** "Y <op> <expr>". The number of levels of transitivity is limited, |
| 527 | ** but is enough to handle most commonly occurring SQL statements. |
| 528 | ** |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 529 | ** If X is not the INTEGER PRIMARY KEY then X must be compatible with |
| 530 | ** index pIdx. |
| 531 | */ |
dan | b2cfc14 | 2013-07-05 11:10:54 +0000 | [diff] [blame] | 532 | static WhereTerm *whereScanInit( |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 533 | WhereScan *pScan, /* The WhereScan object being initialized */ |
| 534 | WhereClause *pWC, /* The WHERE clause to be scanned */ |
| 535 | int iCur, /* Cursor to scan for */ |
| 536 | int iColumn, /* Column to scan for */ |
| 537 | u32 opMask, /* Operator(s) to scan for */ |
| 538 | Index *pIdx /* Must be compatible with this index */ |
| 539 | ){ |
| 540 | int j; |
| 541 | |
drh | e9d935a | 2013-06-05 16:19:59 +0000 | [diff] [blame] | 542 | /* memset(pScan, 0, sizeof(*pScan)); */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 543 | pScan->pOrigWC = pWC; |
| 544 | pScan->pWC = pWC; |
| 545 | if( pIdx && iColumn>=0 ){ |
| 546 | pScan->idxaff = pIdx->pTable->aCol[iColumn].affinity; |
| 547 | for(j=0; pIdx->aiColumn[j]!=iColumn; j++){ |
dan | 39129ce | 2014-06-30 15:23:57 +0000 | [diff] [blame] | 548 | if( NEVER(j>pIdx->nColumn) ) return 0; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 549 | } |
| 550 | pScan->zCollName = pIdx->azColl[j]; |
drh | e9d935a | 2013-06-05 16:19:59 +0000 | [diff] [blame] | 551 | }else{ |
| 552 | pScan->idxaff = 0; |
| 553 | pScan->zCollName = 0; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 554 | } |
| 555 | pScan->opMask = opMask; |
drh | e9d935a | 2013-06-05 16:19:59 +0000 | [diff] [blame] | 556 | pScan->k = 0; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 557 | pScan->aEquiv[0] = iCur; |
| 558 | pScan->aEquiv[1] = iColumn; |
| 559 | pScan->nEquiv = 2; |
| 560 | pScan->iEquiv = 2; |
| 561 | return whereScanNext(pScan); |
| 562 | } |
| 563 | |
| 564 | /* |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 565 | ** Search for a term in the WHERE clause that is of the form "X <op> <expr>" |
| 566 | ** where X is a reference to the iColumn of table iCur and <op> is one of |
| 567 | ** the WO_xx operator codes specified by the op parameter. |
| 568 | ** Return a pointer to the term. Return 0 if not found. |
drh | 58eb1c0 | 2013-01-17 00:08:42 +0000 | [diff] [blame] | 569 | ** |
| 570 | ** The term returned might by Y=<expr> if there is another constraint in |
| 571 | ** the WHERE clause that specifies that X=Y. Any such constraints will be |
| 572 | ** identified by the WO_EQUIV bit in the pTerm->eOperator field. The |
| 573 | ** aEquiv[] array holds X and all its equivalents, with each SQL variable |
| 574 | ** taking up two slots in aEquiv[]. The first slot is for the cursor number |
| 575 | ** and the second is for the column number. There are 22 slots in aEquiv[] |
| 576 | ** so that means we can look for X plus up to 10 other equivalent values. |
| 577 | ** Hence a search for X will return <expr> if X=A1 and A1=A2 and A2=A3 |
| 578 | ** and ... and A9=A10 and A10=<expr>. |
| 579 | ** |
| 580 | ** If there are multiple terms in the WHERE clause of the form "X <op> <expr>" |
| 581 | ** then try for the one with no dependencies on <expr> - in other words where |
| 582 | ** <expr> is a constant expression of some kind. Only return entries of |
| 583 | ** 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] | 584 | ** the form "X <op> <const-expr>" exist. If no terms with a constant RHS |
| 585 | ** exist, try to return a term that does not use WO_EQUIV. |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 586 | */ |
| 587 | static WhereTerm *findTerm( |
| 588 | WhereClause *pWC, /* The WHERE clause to be searched */ |
| 589 | int iCur, /* Cursor number of LHS */ |
| 590 | int iColumn, /* Column number of LHS */ |
| 591 | Bitmask notReady, /* RHS must not overlap with this mask */ |
drh | ec1724e | 2008-12-09 01:32:03 +0000 | [diff] [blame] | 592 | u32 op, /* Mask of WO_xx values describing operator */ |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 593 | Index *pIdx /* Must be compatible with this index, if not NULL */ |
| 594 | ){ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 595 | WhereTerm *pResult = 0; |
| 596 | WhereTerm *p; |
| 597 | WhereScan scan; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 598 | |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 599 | p = whereScanInit(&scan, pWC, iCur, iColumn, op, pIdx); |
drh | e8d0c61 | 2015-05-14 01:05:25 +0000 | [diff] [blame] | 600 | op &= WO_EQ|WO_IS; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 601 | while( p ){ |
| 602 | if( (p->prereqRight & notReady)==0 ){ |
drh | e8d0c61 | 2015-05-14 01:05:25 +0000 | [diff] [blame] | 603 | if( p->prereqRight==0 && (p->eOperator&op)!=0 ){ |
| 604 | testcase( p->eOperator & WO_IS ); |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 605 | return p; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 606 | } |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 607 | if( pResult==0 ) pResult = p; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 608 | } |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 609 | p = whereScanNext(&scan); |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 610 | } |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 611 | return pResult; |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 612 | } |
| 613 | |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 614 | /* Forward reference */ |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 615 | static void exprAnalyze(SrcList*, WhereClause*, int); |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 616 | |
| 617 | /* |
| 618 | ** Call exprAnalyze on all terms in a WHERE clause. |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 619 | */ |
| 620 | static void exprAnalyzeAll( |
| 621 | SrcList *pTabList, /* the FROM clause */ |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 622 | WhereClause *pWC /* the WHERE clause to be analyzed */ |
| 623 | ){ |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 624 | int i; |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 625 | for(i=pWC->nTerm-1; i>=0; i--){ |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 626 | exprAnalyze(pTabList, pWC, i); |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 627 | } |
| 628 | } |
| 629 | |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 630 | #ifndef SQLITE_OMIT_LIKE_OPTIMIZATION |
| 631 | /* |
| 632 | ** Check to see if the given expression is a LIKE or GLOB operator that |
| 633 | ** can be optimized using inequality constraints. Return TRUE if it is |
| 634 | ** so and false if not. |
| 635 | ** |
| 636 | ** 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] | 637 | ** literal that does not begin with a wildcard. The LHS must be a column |
| 638 | ** 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] | 639 | ** that virtual tables cannot participate in the LIKE optimization.) The |
drh | f07cf6e | 2015-03-06 16:45:16 +0000 | [diff] [blame] | 640 | ** collating sequence for the column on the LHS must be appropriate for |
| 641 | ** the operator. |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 642 | */ |
| 643 | static int isLikeOrGlob( |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 644 | Parse *pParse, /* Parsing and code generating context */ |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 645 | Expr *pExpr, /* Test this expression */ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 646 | Expr **ppPrefix, /* Pointer to TK_STRING expression with pattern prefix */ |
drh | 9f504ea | 2008-02-23 21:55:39 +0000 | [diff] [blame] | 647 | int *pisComplete, /* True if the only wildcard is % in the last character */ |
| 648 | int *pnoCase /* True if uppercase is equivalent to lowercase */ |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 649 | ){ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 650 | const char *z = 0; /* String on RHS of LIKE operator */ |
drh | 5bd98ae | 2009-01-07 18:24:03 +0000 | [diff] [blame] | 651 | Expr *pRight, *pLeft; /* Right and left size of LIKE operator */ |
| 652 | ExprList *pList; /* List of operands to the LIKE operator */ |
| 653 | int c; /* One character in z[] */ |
| 654 | int cnt; /* Number of non-wildcard prefix characters */ |
| 655 | char wc[3]; /* Wildcard characters */ |
drh | 5bd98ae | 2009-01-07 18:24:03 +0000 | [diff] [blame] | 656 | sqlite3 *db = pParse->db; /* Database connection */ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 657 | sqlite3_value *pVal = 0; |
| 658 | int op; /* Opcode of pRight */ |
drh | d64fe2f | 2005-08-28 17:00:23 +0000 | [diff] [blame] | 659 | |
drh | 9f504ea | 2008-02-23 21:55:39 +0000 | [diff] [blame] | 660 | if( !sqlite3IsLikeFunction(db, pExpr, pnoCase, wc) ){ |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 661 | return 0; |
| 662 | } |
drh | 9f504ea | 2008-02-23 21:55:39 +0000 | [diff] [blame] | 663 | #ifdef SQLITE_EBCDIC |
| 664 | if( *pnoCase ) return 0; |
| 665 | #endif |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 666 | pList = pExpr->x.pList; |
drh | 55ef4d9 | 2005-08-14 01:20:37 +0000 | [diff] [blame] | 667 | pLeft = pList->a[1].pExpr; |
dan | c68939e | 2012-03-29 14:29:07 +0000 | [diff] [blame] | 668 | if( pLeft->op!=TK_COLUMN |
| 669 | || sqlite3ExprAffinity(pLeft)!=SQLITE_AFF_TEXT |
drh | f07cf6e | 2015-03-06 16:45:16 +0000 | [diff] [blame] | 670 | || IsVirtual(pLeft->pTab) /* Value might be numeric */ |
dan | c68939e | 2012-03-29 14:29:07 +0000 | [diff] [blame] | 671 | ){ |
drh | d91ca49 | 2009-10-22 20:50:36 +0000 | [diff] [blame] | 672 | /* IMP: R-02065-49465 The left-hand side of the LIKE or GLOB operator must |
| 673 | ** be the name of an indexed column with TEXT affinity. */ |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 674 | return 0; |
| 675 | } |
drh | d91ca49 | 2009-10-22 20:50:36 +0000 | [diff] [blame] | 676 | assert( pLeft->iColumn!=(-1) ); /* Because IPK never has AFF_TEXT */ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 677 | |
drh | 6ade453 | 2014-01-16 15:31:41 +0000 | [diff] [blame] | 678 | pRight = sqlite3ExprSkipCollate(pList->a[0].pExpr); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 679 | op = pRight->op; |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 680 | if( op==TK_VARIABLE ){ |
| 681 | Vdbe *pReprepare = pParse->pReprepare; |
drh | a704400 | 2010-09-14 18:22:59 +0000 | [diff] [blame] | 682 | int iCol = pRight->iColumn; |
drh | cf0fd4a | 2013-08-01 12:21:58 +0000 | [diff] [blame] | 683 | pVal = sqlite3VdbeGetBoundValue(pReprepare, iCol, SQLITE_AFF_NONE); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 684 | if( pVal && sqlite3_value_type(pVal)==SQLITE_TEXT ){ |
| 685 | z = (char *)sqlite3_value_text(pVal); |
| 686 | } |
drh | f9b22ca | 2011-10-21 16:47:31 +0000 | [diff] [blame] | 687 | sqlite3VdbeSetVarmask(pParse->pVdbe, iCol); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 688 | assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER ); |
| 689 | }else if( op==TK_STRING ){ |
| 690 | z = pRight->u.zToken; |
| 691 | } |
| 692 | if( z ){ |
shane | 8509570 | 2009-06-15 16:27:08 +0000 | [diff] [blame] | 693 | cnt = 0; |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 694 | 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] | 695 | cnt++; |
| 696 | } |
drh | 93ee23c | 2010-07-22 12:33:57 +0000 | [diff] [blame] | 697 | if( cnt!=0 && 255!=(u8)z[cnt-1] ){ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 698 | Expr *pPrefix; |
drh | 93ee23c | 2010-07-22 12:33:57 +0000 | [diff] [blame] | 699 | *pisComplete = c==wc[0] && z[cnt+1]==0; |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 700 | pPrefix = sqlite3Expr(db, TK_STRING, z); |
| 701 | if( pPrefix ) pPrefix->u.zToken[cnt] = 0; |
| 702 | *ppPrefix = pPrefix; |
| 703 | if( op==TK_VARIABLE ){ |
| 704 | Vdbe *v = pParse->pVdbe; |
drh | f9b22ca | 2011-10-21 16:47:31 +0000 | [diff] [blame] | 705 | sqlite3VdbeSetVarmask(v, pRight->iColumn); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 706 | if( *pisComplete && pRight->u.zToken[1] ){ |
| 707 | /* If the rhs of the LIKE expression is a variable, and the current |
| 708 | ** value of the variable means there is no need to invoke the LIKE |
| 709 | ** function, then no OP_Variable will be added to the program. |
| 710 | ** This causes problems for the sqlite3_bind_parameter_name() |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 711 | ** API. To work around them, add a dummy OP_Variable here. |
drh | bec451f | 2009-10-17 13:13:02 +0000 | [diff] [blame] | 712 | */ |
| 713 | int r1 = sqlite3GetTempReg(pParse); |
| 714 | sqlite3ExprCodeTarget(pParse, pRight, r1); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 715 | sqlite3VdbeChangeP3(v, sqlite3VdbeCurrentAddr(v)-1, 0); |
drh | bec451f | 2009-10-17 13:13:02 +0000 | [diff] [blame] | 716 | sqlite3ReleaseTempReg(pParse, r1); |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 717 | } |
| 718 | } |
| 719 | }else{ |
| 720 | z = 0; |
shane | 8509570 | 2009-06-15 16:27:08 +0000 | [diff] [blame] | 721 | } |
drh | f998b73 | 2007-11-26 13:36:00 +0000 | [diff] [blame] | 722 | } |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 723 | |
| 724 | sqlite3ValueFree(pVal); |
| 725 | return (z!=0); |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 726 | } |
| 727 | #endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */ |
| 728 | |
drh | edb193b | 2006-06-27 13:20:21 +0000 | [diff] [blame] | 729 | |
| 730 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 731 | /* |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 732 | ** Check to see if the given expression is of the form |
| 733 | ** |
| 734 | ** column MATCH expr |
| 735 | ** |
| 736 | ** If it is then return TRUE. If not, return FALSE. |
| 737 | */ |
| 738 | static int isMatchOfColumn( |
| 739 | Expr *pExpr /* Test this expression */ |
| 740 | ){ |
| 741 | ExprList *pList; |
| 742 | |
| 743 | if( pExpr->op!=TK_FUNCTION ){ |
| 744 | return 0; |
| 745 | } |
drh | 33e619f | 2009-05-28 01:00:55 +0000 | [diff] [blame] | 746 | if( sqlite3StrICmp(pExpr->u.zToken,"match")!=0 ){ |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 747 | return 0; |
| 748 | } |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 749 | pList = pExpr->x.pList; |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 750 | if( pList->nExpr!=2 ){ |
| 751 | return 0; |
| 752 | } |
| 753 | if( pList->a[1].pExpr->op != TK_COLUMN ){ |
| 754 | return 0; |
| 755 | } |
| 756 | return 1; |
| 757 | } |
drh | edb193b | 2006-06-27 13:20:21 +0000 | [diff] [blame] | 758 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 759 | |
| 760 | /* |
drh | 54a167d | 2005-11-26 14:08:07 +0000 | [diff] [blame] | 761 | ** If the pBase expression originated in the ON or USING clause of |
| 762 | ** a join, then transfer the appropriate markings over to derived. |
| 763 | */ |
| 764 | static void transferJoinMarkings(Expr *pDerived, Expr *pBase){ |
drh | d41d39f | 2013-08-28 16:27:01 +0000 | [diff] [blame] | 765 | if( pDerived ){ |
| 766 | pDerived->flags |= pBase->flags & EP_FromJoin; |
| 767 | pDerived->iRightJoinTable = pBase->iRightJoinTable; |
| 768 | } |
drh | 54a167d | 2005-11-26 14:08:07 +0000 | [diff] [blame] | 769 | } |
| 770 | |
drh | 9769efc | 2014-10-24 14:32:21 +0000 | [diff] [blame] | 771 | /* |
| 772 | ** Mark term iChild as being a child of term iParent |
| 773 | */ |
| 774 | static void markTermAsChild(WhereClause *pWC, int iChild, int iParent){ |
| 775 | pWC->a[iChild].iParent = iParent; |
| 776 | pWC->a[iChild].truthProb = pWC->a[iParent].truthProb; |
| 777 | pWC->a[iParent].nChild++; |
| 778 | } |
| 779 | |
drh | 8426636 | 2015-03-16 12:13:31 +0000 | [diff] [blame] | 780 | /* |
| 781 | ** Return the N-th AND-connected subterm of pTerm. Or if pTerm is not |
| 782 | ** a conjunction, then return just pTerm when N==0. If N is exceeds |
| 783 | ** the number of available subterms, return NULL. |
| 784 | */ |
| 785 | static WhereTerm *whereNthSubterm(WhereTerm *pTerm, int N){ |
| 786 | if( pTerm->eOperator!=WO_AND ){ |
| 787 | return N==0 ? pTerm : 0; |
| 788 | } |
| 789 | if( N<pTerm->u.pAndInfo->wc.nTerm ){ |
| 790 | return &pTerm->u.pAndInfo->wc.a[N]; |
| 791 | } |
| 792 | return 0; |
| 793 | } |
| 794 | |
| 795 | /* |
| 796 | ** Subterms pOne and pTwo are contained within WHERE clause pWC. The |
| 797 | ** two subterms are in disjunction - they are OR-ed together. |
| 798 | ** |
| 799 | ** If these two terms are both of the form: "A op B" with the same |
| 800 | ** A and B values but different operators and if the operators are |
| 801 | ** compatible (if one is = and the other is <, for example) then |
drh | c03acf2 | 2015-03-16 13:12:34 +0000 | [diff] [blame] | 802 | ** 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] | 803 | ** two. |
| 804 | ** |
| 805 | ** Some examples: |
| 806 | ** |
| 807 | ** x<y OR x=y --> x<=y |
| 808 | ** x=y OR x=y --> x=y |
| 809 | ** x<=y OR x<y --> x<=y |
| 810 | ** |
| 811 | ** The following is NOT generated: |
| 812 | ** |
| 813 | ** x<y OR x>y --> x!=y |
| 814 | */ |
| 815 | static void whereCombineDisjuncts( |
| 816 | SrcList *pSrc, /* the FROM clause */ |
| 817 | WhereClause *pWC, /* The complete WHERE clause */ |
| 818 | WhereTerm *pOne, /* First disjunct */ |
| 819 | WhereTerm *pTwo /* Second disjunct */ |
| 820 | ){ |
| 821 | u16 eOp = pOne->eOperator | pTwo->eOperator; |
| 822 | sqlite3 *db; /* Database connection (for malloc) */ |
| 823 | Expr *pNew; /* New virtual expression */ |
| 824 | int op; /* Operator for the combined expression */ |
| 825 | int idxNew; /* Index in pWC of the next virtual term */ |
| 826 | |
| 827 | if( (pOne->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return; |
| 828 | if( (pTwo->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return; |
| 829 | if( (eOp & (WO_EQ|WO_LT|WO_LE))!=eOp |
| 830 | && (eOp & (WO_EQ|WO_GT|WO_GE))!=eOp ) return; |
| 831 | assert( pOne->pExpr->pLeft!=0 && pOne->pExpr->pRight!=0 ); |
| 832 | assert( pTwo->pExpr->pLeft!=0 && pTwo->pExpr->pRight!=0 ); |
| 833 | if( sqlite3ExprCompare(pOne->pExpr->pLeft, pTwo->pExpr->pLeft, -1) ) return; |
| 834 | if( sqlite3ExprCompare(pOne->pExpr->pRight, pTwo->pExpr->pRight, -1) )return; |
| 835 | /* If we reach this point, it means the two subterms can be combined */ |
| 836 | if( (eOp & (eOp-1))!=0 ){ |
| 837 | if( eOp & (WO_LT|WO_LE) ){ |
| 838 | eOp = WO_LE; |
| 839 | }else{ |
| 840 | assert( eOp & (WO_GT|WO_GE) ); |
| 841 | eOp = WO_GE; |
| 842 | } |
| 843 | } |
| 844 | db = pWC->pWInfo->pParse->db; |
| 845 | pNew = sqlite3ExprDup(db, pOne->pExpr, 0); |
| 846 | if( pNew==0 ) return; |
| 847 | for(op=TK_EQ; eOp!=(WO_EQ<<(op-TK_EQ)); op++){ assert( op<TK_GE ); } |
| 848 | pNew->op = op; |
| 849 | idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC); |
| 850 | exprAnalyze(pSrc, pWC, idxNew); |
| 851 | } |
| 852 | |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 853 | #if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY) |
| 854 | /* |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 855 | ** Analyze a term that consists of two or more OR-connected |
| 856 | ** subterms. So in: |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 857 | ** |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 858 | ** ... WHERE (a=5) AND (b=7 OR c=9 OR d=13) AND (d=13) |
| 859 | ** ^^^^^^^^^^^^^^^^^^^^ |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 860 | ** |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 861 | ** This routine analyzes terms such as the middle term in the above example. |
| 862 | ** A WhereOrTerm object is computed and attached to the term under |
| 863 | ** analysis, regardless of the outcome of the analysis. Hence: |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 864 | ** |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 865 | ** WhereTerm.wtFlags |= TERM_ORINFO |
| 866 | ** WhereTerm.u.pOrInfo = a dynamically allocated WhereOrTerm object |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 867 | ** |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 868 | ** The term being analyzed must have two or more of OR-connected subterms. |
danielk1977 | fdc4019 | 2008-12-29 18:33:32 +0000 | [diff] [blame] | 869 | ** A single subterm might be a set of AND-connected sub-subterms. |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 870 | ** Examples of terms under analysis: |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 871 | ** |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 872 | ** (A) t1.x=t2.y OR t1.x=t2.z OR t1.y=15 OR t1.z=t3.a+5 |
| 873 | ** (B) x=expr1 OR expr2=x OR x=expr3 |
| 874 | ** (C) t1.x=t2.y OR (t1.x=t2.z AND t1.y=15) |
| 875 | ** (D) x=expr1 OR (y>11 AND y<22 AND z LIKE '*hello*') |
| 876 | ** (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] | 877 | ** (F) x>A OR (x=A AND y>=B) |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 878 | ** |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 879 | ** CASE 1: |
| 880 | ** |
drh | c3e552f | 2013-02-08 16:04:19 +0000 | [diff] [blame] | 881 | ** 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] | 882 | ** a single table T (as shown in example B above) then create a new virtual |
| 883 | ** term that is an equivalent IN expression. In other words, if the term |
| 884 | ** being analyzed is: |
| 885 | ** |
| 886 | ** x = expr1 OR expr2 = x OR x = expr3 |
| 887 | ** |
| 888 | ** then create a new virtual term like this: |
| 889 | ** |
| 890 | ** x IN (expr1,expr2,expr3) |
| 891 | ** |
| 892 | ** CASE 2: |
| 893 | ** |
drh | c03acf2 | 2015-03-16 13:12:34 +0000 | [diff] [blame] | 894 | ** If there are exactly two disjuncts one side has x>A and the other side |
| 895 | ** has x=A (for the same x and A) then add a new virtual conjunct term to the |
| 896 | ** WHERE clause of the form "x>=A". Example: |
| 897 | ** |
| 898 | ** x>A OR (x=A AND y>B) adds: x>=A |
| 899 | ** |
| 900 | ** The added conjunct can sometimes be helpful in query planning. |
drh | 8426636 | 2015-03-16 12:13:31 +0000 | [diff] [blame] | 901 | ** |
| 902 | ** CASE 3: |
| 903 | ** |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 904 | ** If all subterms are indexable by a single table T, then set |
| 905 | ** |
| 906 | ** WhereTerm.eOperator = WO_OR |
| 907 | ** WhereTerm.u.pOrInfo->indexable |= the cursor number for table T |
| 908 | ** |
| 909 | ** A subterm is "indexable" if it is of the form |
| 910 | ** "T.C <op> <expr>" where C is any column of table T and |
| 911 | ** <op> is one of "=", "<", "<=", ">", ">=", "IS NULL", or "IN". |
| 912 | ** A subterm is also indexable if it is an AND of two or more |
| 913 | ** subsubterms at least one of which is indexable. Indexable AND |
| 914 | ** subterms have their eOperator set to WO_AND and they have |
| 915 | ** u.pAndInfo set to a dynamically allocated WhereAndTerm object. |
| 916 | ** |
| 917 | ** From another point of view, "indexable" means that the subterm could |
| 918 | ** potentially be used with an index if an appropriate index exists. |
| 919 | ** This analysis does not consider whether or not the index exists; that |
drh | 4a6fc35 | 2013-08-07 01:18:38 +0000 | [diff] [blame] | 920 | ** is decided elsewhere. This analysis only looks at whether subterms |
| 921 | ** appropriate for indexing exist. |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 922 | ** |
drh | 4a6fc35 | 2013-08-07 01:18:38 +0000 | [diff] [blame] | 923 | ** All examples A through E above satisfy case 2. But if a term |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 924 | ** also satisfies case 1 (such as B) we know that the optimizer will |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 925 | ** always prefer case 1, so in that case we pretend that case 2 is not |
| 926 | ** satisfied. |
| 927 | ** |
| 928 | ** It might be the case that multiple tables are indexable. For example, |
| 929 | ** (E) above is indexable on tables P, Q, and R. |
| 930 | ** |
| 931 | ** Terms that satisfy case 2 are candidates for lookup by using |
| 932 | ** separate indices to find rowids for each subterm and composing |
| 933 | ** the union of all rowids using a RowSet object. This is similar |
| 934 | ** to "bitmap indices" in other database engines. |
| 935 | ** |
| 936 | ** OTHERWISE: |
| 937 | ** |
| 938 | ** If neither case 1 nor case 2 apply, then leave the eOperator set to |
| 939 | ** zero. This term is not useful for search. |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 940 | */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 941 | static void exprAnalyzeOrTerm( |
| 942 | SrcList *pSrc, /* the FROM clause */ |
| 943 | WhereClause *pWC, /* the complete WHERE clause */ |
| 944 | int idxTerm /* Index of the OR-term to be analyzed */ |
| 945 | ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 946 | WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */ |
| 947 | Parse *pParse = pWInfo->pParse; /* Parser context */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 948 | sqlite3 *db = pParse->db; /* Database connection */ |
| 949 | WhereTerm *pTerm = &pWC->a[idxTerm]; /* The term to be analyzed */ |
| 950 | Expr *pExpr = pTerm->pExpr; /* The expression of the term */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 951 | int i; /* Loop counters */ |
| 952 | WhereClause *pOrWc; /* Breakup of pTerm into subterms */ |
| 953 | WhereTerm *pOrTerm; /* A Sub-term within the pOrWc */ |
| 954 | WhereOrInfo *pOrInfo; /* Additional information associated with pTerm */ |
| 955 | Bitmask chngToIN; /* Tables that might satisfy case 1 */ |
| 956 | Bitmask indexable; /* Tables that are indexable, satisfying case 2 */ |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 957 | |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 958 | /* |
| 959 | ** Break the OR clause into its separate subterms. The subterms are |
| 960 | ** stored in a WhereClause structure containing within the WhereOrInfo |
| 961 | ** object that is attached to the original OR clause term. |
| 962 | */ |
| 963 | assert( (pTerm->wtFlags & (TERM_DYNAMIC|TERM_ORINFO|TERM_ANDINFO))==0 ); |
| 964 | assert( pExpr->op==TK_OR ); |
drh | 954701a | 2008-12-29 23:45:07 +0000 | [diff] [blame] | 965 | pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, sizeof(*pOrInfo)); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 966 | if( pOrInfo==0 ) return; |
| 967 | pTerm->wtFlags |= TERM_ORINFO; |
| 968 | pOrWc = &pOrInfo->wc; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 969 | whereClauseInit(pOrWc, pWInfo); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 970 | whereSplit(pOrWc, pExpr, TK_OR); |
| 971 | exprAnalyzeAll(pSrc, pOrWc); |
| 972 | if( db->mallocFailed ) return; |
| 973 | assert( pOrWc->nTerm>=2 ); |
| 974 | |
| 975 | /* |
| 976 | ** Compute the set of tables that might satisfy cases 1 or 2. |
| 977 | */ |
danielk1977 | e672c8e | 2009-05-22 15:43:26 +0000 | [diff] [blame] | 978 | indexable = ~(Bitmask)0; |
drh | c3e552f | 2013-02-08 16:04:19 +0000 | [diff] [blame] | 979 | chngToIN = ~(Bitmask)0; |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 980 | for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0 && indexable; i--, pOrTerm++){ |
| 981 | if( (pOrTerm->eOperator & WO_SINGLE)==0 ){ |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 982 | WhereAndInfo *pAndInfo; |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 983 | assert( (pOrTerm->wtFlags & (TERM_ANDINFO|TERM_ORINFO))==0 ); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 984 | chngToIN = 0; |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 985 | pAndInfo = sqlite3DbMallocRaw(db, sizeof(*pAndInfo)); |
| 986 | if( pAndInfo ){ |
| 987 | WhereClause *pAndWC; |
| 988 | WhereTerm *pAndTerm; |
| 989 | int j; |
| 990 | Bitmask b = 0; |
| 991 | pOrTerm->u.pAndInfo = pAndInfo; |
| 992 | pOrTerm->wtFlags |= TERM_ANDINFO; |
| 993 | pOrTerm->eOperator = WO_AND; |
| 994 | pAndWC = &pAndInfo->wc; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 995 | whereClauseInit(pAndWC, pWC->pWInfo); |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 996 | whereSplit(pAndWC, pOrTerm->pExpr, TK_AND); |
| 997 | exprAnalyzeAll(pSrc, pAndWC); |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 998 | pAndWC->pOuter = pWC; |
drh | 7c2fbde | 2009-01-07 20:58:57 +0000 | [diff] [blame] | 999 | testcase( db->mallocFailed ); |
drh | 96c7a7d | 2009-01-10 15:34:12 +0000 | [diff] [blame] | 1000 | if( !db->mallocFailed ){ |
| 1001 | for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){ |
| 1002 | assert( pAndTerm->pExpr ); |
| 1003 | if( allowedOp(pAndTerm->pExpr->op) ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1004 | b |= getMask(&pWInfo->sMaskSet, pAndTerm->leftCursor); |
drh | 96c7a7d | 2009-01-10 15:34:12 +0000 | [diff] [blame] | 1005 | } |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 1006 | } |
| 1007 | } |
| 1008 | indexable &= b; |
| 1009 | } |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1010 | }else if( pOrTerm->wtFlags & TERM_COPIED ){ |
| 1011 | /* Skip this term for now. We revisit it when we process the |
| 1012 | ** corresponding TERM_VIRTUAL term */ |
| 1013 | }else{ |
| 1014 | Bitmask b; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1015 | b = getMask(&pWInfo->sMaskSet, pOrTerm->leftCursor); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1016 | if( pOrTerm->wtFlags & TERM_VIRTUAL ){ |
| 1017 | WhereTerm *pOther = &pOrWc->a[pOrTerm->iParent]; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1018 | b |= getMask(&pWInfo->sMaskSet, pOther->leftCursor); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1019 | } |
| 1020 | indexable &= b; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1021 | if( (pOrTerm->eOperator & WO_EQ)==0 ){ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1022 | chngToIN = 0; |
| 1023 | }else{ |
| 1024 | chngToIN &= b; |
| 1025 | } |
| 1026 | } |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1027 | } |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1028 | |
| 1029 | /* |
drh | 8426636 | 2015-03-16 12:13:31 +0000 | [diff] [blame] | 1030 | ** Record the set of tables that satisfy case 3. The set might be |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 1031 | ** empty. |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1032 | */ |
| 1033 | pOrInfo->indexable = indexable; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 1034 | pTerm->eOperator = indexable==0 ? 0 : WO_OR; |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1035 | |
drh | 8426636 | 2015-03-16 12:13:31 +0000 | [diff] [blame] | 1036 | /* For a two-way OR, attempt to implementation case 2. |
| 1037 | */ |
| 1038 | if( indexable && pOrWc->nTerm==2 ){ |
| 1039 | int iOne = 0; |
| 1040 | WhereTerm *pOne; |
| 1041 | while( (pOne = whereNthSubterm(&pOrWc->a[0],iOne++))!=0 ){ |
| 1042 | int iTwo = 0; |
| 1043 | WhereTerm *pTwo; |
| 1044 | while( (pTwo = whereNthSubterm(&pOrWc->a[1],iTwo++))!=0 ){ |
| 1045 | whereCombineDisjuncts(pSrc, pWC, pOne, pTwo); |
| 1046 | } |
| 1047 | } |
| 1048 | } |
| 1049 | |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1050 | /* |
| 1051 | ** chngToIN holds a set of tables that *might* satisfy case 1. But |
| 1052 | ** we have to do some additional checking to see if case 1 really |
| 1053 | ** is satisfied. |
drh | 4e8be3b | 2009-06-08 17:11:08 +0000 | [diff] [blame] | 1054 | ** |
| 1055 | ** chngToIN will hold either 0, 1, or 2 bits. The 0-bit case means |
| 1056 | ** that there is no possibility of transforming the OR clause into an |
| 1057 | ** IN operator because one or more terms in the OR clause contain |
| 1058 | ** something other than == on a column in the single table. The 1-bit |
| 1059 | ** case means that every term of the OR clause is of the form |
| 1060 | ** "table.column=expr" for some single table. The one bit that is set |
| 1061 | ** will correspond to the common table. We still need to check to make |
| 1062 | ** sure the same column is used on all terms. The 2-bit case is when |
| 1063 | ** the all terms are of the form "table1.column=table2.column". It |
| 1064 | ** might be possible to form an IN operator with either table1.column |
| 1065 | ** or table2.column as the LHS if either is common to every term of |
| 1066 | ** the OR clause. |
| 1067 | ** |
| 1068 | ** Note that terms of the form "table.column1=table.column2" (the |
| 1069 | ** same table on both sizes of the ==) cannot be optimized. |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1070 | */ |
| 1071 | if( chngToIN ){ |
| 1072 | int okToChngToIN = 0; /* True if the conversion to IN is valid */ |
| 1073 | int iColumn = -1; /* Column index on lhs of IN operator */ |
shane | 63207ab | 2009-02-04 01:49:30 +0000 | [diff] [blame] | 1074 | int iCursor = -1; /* Table cursor common to all terms */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1075 | int j = 0; /* Loop counter */ |
| 1076 | |
| 1077 | /* Search for a table and column that appears on one side or the |
| 1078 | ** other of the == operator in every subterm. That table and column |
| 1079 | ** will be recorded in iCursor and iColumn. There might not be any |
| 1080 | ** such table and column. Set okToChngToIN if an appropriate table |
| 1081 | ** and column is found but leave okToChngToIN false if not found. |
| 1082 | */ |
| 1083 | for(j=0; j<2 && !okToChngToIN; j++){ |
| 1084 | pOrTerm = pOrWc->a; |
| 1085 | for(i=pOrWc->nTerm-1; i>=0; i--, pOrTerm++){ |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1086 | assert( pOrTerm->eOperator & WO_EQ ); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1087 | pOrTerm->wtFlags &= ~TERM_OR_OK; |
drh | 4e8be3b | 2009-06-08 17:11:08 +0000 | [diff] [blame] | 1088 | if( pOrTerm->leftCursor==iCursor ){ |
| 1089 | /* This is the 2-bit case and we are on the second iteration and |
| 1090 | ** current term is from the first iteration. So skip this term. */ |
| 1091 | assert( j==1 ); |
| 1092 | continue; |
| 1093 | } |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1094 | if( (chngToIN & getMask(&pWInfo->sMaskSet, pOrTerm->leftCursor))==0 ){ |
drh | 4e8be3b | 2009-06-08 17:11:08 +0000 | [diff] [blame] | 1095 | /* 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] | 1096 | ** chngToIN set but t1 is not. This term will be either preceded |
drh | 4e8be3b | 2009-06-08 17:11:08 +0000 | [diff] [blame] | 1097 | ** or follwed by an inverted copy (t2.b==t1.a). Skip this term |
| 1098 | ** and use its inversion. */ |
| 1099 | testcase( pOrTerm->wtFlags & TERM_COPIED ); |
| 1100 | testcase( pOrTerm->wtFlags & TERM_VIRTUAL ); |
| 1101 | assert( pOrTerm->wtFlags & (TERM_COPIED|TERM_VIRTUAL) ); |
| 1102 | continue; |
| 1103 | } |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1104 | iColumn = pOrTerm->u.leftColumn; |
| 1105 | iCursor = pOrTerm->leftCursor; |
| 1106 | break; |
| 1107 | } |
| 1108 | if( i<0 ){ |
drh | 4e8be3b | 2009-06-08 17:11:08 +0000 | [diff] [blame] | 1109 | /* No candidate table+column was found. This can only occur |
| 1110 | ** on the second iteration */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1111 | assert( j==1 ); |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1112 | assert( IsPowerOfTwo(chngToIN) ); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1113 | assert( chngToIN==getMask(&pWInfo->sMaskSet, iCursor) ); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1114 | break; |
| 1115 | } |
drh | 4e8be3b | 2009-06-08 17:11:08 +0000 | [diff] [blame] | 1116 | testcase( j==1 ); |
| 1117 | |
| 1118 | /* We have found a candidate table and column. Check to see if that |
| 1119 | ** table and column is common to every term in the OR clause */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1120 | okToChngToIN = 1; |
| 1121 | for(; i>=0 && okToChngToIN; i--, pOrTerm++){ |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1122 | assert( pOrTerm->eOperator & WO_EQ ); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1123 | if( pOrTerm->leftCursor!=iCursor ){ |
| 1124 | pOrTerm->wtFlags &= ~TERM_OR_OK; |
| 1125 | }else if( pOrTerm->u.leftColumn!=iColumn ){ |
| 1126 | okToChngToIN = 0; |
| 1127 | }else{ |
| 1128 | int affLeft, affRight; |
| 1129 | /* If the right-hand side is also a column, then the affinities |
| 1130 | ** of both right and left sides must be such that no type |
| 1131 | ** conversions are required on the right. (Ticket #2249) |
| 1132 | */ |
| 1133 | affRight = sqlite3ExprAffinity(pOrTerm->pExpr->pRight); |
| 1134 | affLeft = sqlite3ExprAffinity(pOrTerm->pExpr->pLeft); |
| 1135 | if( affRight!=0 && affRight!=affLeft ){ |
| 1136 | okToChngToIN = 0; |
| 1137 | }else{ |
| 1138 | pOrTerm->wtFlags |= TERM_OR_OK; |
| 1139 | } |
| 1140 | } |
| 1141 | } |
| 1142 | } |
| 1143 | |
| 1144 | /* At this point, okToChngToIN is true if original pTerm satisfies |
| 1145 | ** case 1. In that case, construct a new virtual term that is |
| 1146 | ** pTerm converted into an IN operator. |
| 1147 | */ |
| 1148 | if( okToChngToIN ){ |
| 1149 | Expr *pDup; /* A transient duplicate expression */ |
| 1150 | ExprList *pList = 0; /* The RHS of the IN operator */ |
| 1151 | Expr *pLeft = 0; /* The LHS of the IN operator */ |
| 1152 | Expr *pNew; /* The complete IN operator */ |
| 1153 | |
| 1154 | for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0; i--, pOrTerm++){ |
| 1155 | if( (pOrTerm->wtFlags & TERM_OR_OK)==0 ) continue; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1156 | assert( pOrTerm->eOperator & WO_EQ ); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1157 | assert( pOrTerm->leftCursor==iCursor ); |
| 1158 | assert( pOrTerm->u.leftColumn==iColumn ); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1159 | pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight, 0); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1160 | pList = sqlite3ExprListAppend(pWInfo->pParse, pList, pDup); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1161 | pLeft = pOrTerm->pExpr->pLeft; |
| 1162 | } |
| 1163 | assert( pLeft!=0 ); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1164 | pDup = sqlite3ExprDup(db, pLeft, 0); |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 1165 | pNew = sqlite3PExpr(pParse, TK_IN, pDup, 0, 0); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1166 | if( pNew ){ |
| 1167 | int idxNew; |
| 1168 | transferJoinMarkings(pNew, pExpr); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1169 | assert( !ExprHasProperty(pNew, EP_xIsSelect) ); |
| 1170 | pNew->x.pList = pList; |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1171 | idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC); |
| 1172 | testcase( idxNew==0 ); |
| 1173 | exprAnalyze(pSrc, pWC, idxNew); |
| 1174 | pTerm = &pWC->a[idxTerm]; |
drh | 9769efc | 2014-10-24 14:32:21 +0000 | [diff] [blame] | 1175 | markTermAsChild(pWC, idxNew, idxTerm); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1176 | }else{ |
| 1177 | sqlite3ExprListDelete(db, pList); |
| 1178 | } |
drh | 8426636 | 2015-03-16 12:13:31 +0000 | [diff] [blame] | 1179 | pTerm->eOperator = WO_NOOP; /* case 1 trumps case 3 */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1180 | } |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1181 | } |
drh | 3e35580 | 2007-02-23 23:13:33 +0000 | [diff] [blame] | 1182 | } |
| 1183 | #endif /* !SQLITE_OMIT_OR_OPTIMIZATION && !SQLITE_OMIT_SUBQUERY */ |
drh | 54a167d | 2005-11-26 14:08:07 +0000 | [diff] [blame] | 1184 | |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1185 | /* |
drh | 5820118 | 2015-05-16 20:51:25 +0000 | [diff] [blame] | 1186 | ** We already know that pExpr is a binary operator where both operands are |
| 1187 | ** column references. This routine checks to see if pExpr is an equivalence |
| 1188 | ** relation: |
| 1189 | ** 1. The SQLITE_Transitive optimization must be enabled |
| 1190 | ** 2. Must be either an == or an IS operator |
drh | 69c15fe | 2015-05-18 11:34:52 +0000 | [diff] [blame] | 1191 | ** 3. Not originating the ON clause of an OUTER JOIN |
drh | 5820118 | 2015-05-16 20:51:25 +0000 | [diff] [blame] | 1192 | ** 4. The affinities of A and B must be compatible |
drh | 69c15fe | 2015-05-18 11:34:52 +0000 | [diff] [blame] | 1193 | ** 5a. Both operands use the same collating sequence OR |
| 1194 | ** 5b. The overall collating sequence is BINARY |
drh | 5820118 | 2015-05-16 20:51:25 +0000 | [diff] [blame] | 1195 | ** If this routine returns TRUE, that means that the RHS can be substituted |
| 1196 | ** for the LHS anyplace else in the WHERE clause where the LHS column occurs. |
| 1197 | ** This is an optimization. No harm comes from returning 0. But if 1 is |
| 1198 | ** returned when it should not be, then incorrect answers might result. |
| 1199 | */ |
drh | 69c15fe | 2015-05-18 11:34:52 +0000 | [diff] [blame] | 1200 | static int termIsEquivalence(Parse *pParse, Expr *pExpr){ |
drh | 5820118 | 2015-05-16 20:51:25 +0000 | [diff] [blame] | 1201 | char aff1, aff2; |
drh | 69c15fe | 2015-05-18 11:34:52 +0000 | [diff] [blame] | 1202 | CollSeq *pColl; |
drh | 5820118 | 2015-05-16 20:51:25 +0000 | [diff] [blame] | 1203 | const char *zColl1, *zColl2; |
| 1204 | if( !OptimizationEnabled(pParse->db, SQLITE_Transitive) ) return 0; |
| 1205 | if( pExpr->op!=TK_EQ && pExpr->op!=TK_IS ) return 0; |
| 1206 | if( ExprHasProperty(pExpr, EP_FromJoin) ) return 0; |
| 1207 | aff1 = sqlite3ExprAffinity(pExpr->pLeft); |
| 1208 | aff2 = sqlite3ExprAffinity(pExpr->pRight); |
| 1209 | if( aff1!=aff2 |
| 1210 | && (!sqlite3IsNumericAffinity(aff1) || !sqlite3IsNumericAffinity(aff2)) |
| 1211 | ){ |
| 1212 | return 0; |
| 1213 | } |
drh | 69c15fe | 2015-05-18 11:34:52 +0000 | [diff] [blame] | 1214 | pColl = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft, pExpr->pRight); |
| 1215 | if( pColl==0 || sqlite3StrICmp(pColl->zName, "BINARY")==0 ) return 1; |
| 1216 | pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft); |
drh | c4d56dd | 2015-05-18 12:18:37 +0000 | [diff] [blame] | 1217 | /* Since pLeft and pRight are both a column references, their collating |
| 1218 | ** sequence should always be defined. */ |
| 1219 | zColl1 = ALWAYS(pColl) ? pColl->zName : 0; |
drh | 69c15fe | 2015-05-18 11:34:52 +0000 | [diff] [blame] | 1220 | pColl = sqlite3ExprCollSeq(pParse, pExpr->pRight); |
drh | c4d56dd | 2015-05-18 12:18:37 +0000 | [diff] [blame] | 1221 | zColl2 = ALWAYS(pColl) ? pColl->zName : 0; |
drh | 5820118 | 2015-05-16 20:51:25 +0000 | [diff] [blame] | 1222 | return sqlite3StrICmp(zColl1, zColl2)==0; |
| 1223 | } |
| 1224 | |
| 1225 | /* |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 1226 | ** The input to this routine is an WhereTerm structure with only the |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 1227 | ** "pExpr" field filled in. The job of this routine is to analyze the |
drh | 0aa74ed | 2005-07-16 13:33:20 +0000 | [diff] [blame] | 1228 | ** subexpression and populate all the other fields of the WhereTerm |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1229 | ** structure. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 1230 | ** |
| 1231 | ** If the expression is of the form "<expr> <op> X" it gets commuted |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1232 | ** to the standard form of "X <op> <expr>". |
| 1233 | ** |
| 1234 | ** If the expression is of the form "X <op> Y" where both X and Y are |
| 1235 | ** columns, then the original expression is unchanged and a new virtual |
| 1236 | ** term of the form "Y <op> X" is added to the WHERE clause and |
| 1237 | ** analyzed separately. The original term is marked with TERM_COPIED |
| 1238 | ** and the new term is marked with TERM_DYNAMIC (because it's pExpr |
| 1239 | ** needs to be freed with the WhereClause) and TERM_VIRTUAL (because it |
| 1240 | ** is a commuted copy of a prior term.) The original term has nChild=1 |
| 1241 | ** and the copy has idxParent set to the index of the original term. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1242 | */ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1243 | static void exprAnalyze( |
| 1244 | SrcList *pSrc, /* the FROM clause */ |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1245 | WhereClause *pWC, /* the WHERE clause */ |
| 1246 | int idxTerm /* Index of the term to be analyzed */ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1247 | ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1248 | WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1249 | WhereTerm *pTerm; /* The term to be analyzed */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 1250 | WhereMaskSet *pMaskSet; /* Set of table index masks */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1251 | Expr *pExpr; /* The expression to be analyzed */ |
| 1252 | Bitmask prereqLeft; /* Prerequesites of the pExpr->pLeft */ |
| 1253 | Bitmask prereqAll; /* Prerequesites of pExpr */ |
drh | 5e767c5 | 2010-02-25 04:15:47 +0000 | [diff] [blame] | 1254 | Bitmask extraRight = 0; /* Extra dependencies on LEFT JOIN */ |
drh | 1d452e1 | 2009-11-01 19:26:59 +0000 | [diff] [blame] | 1255 | Expr *pStr1 = 0; /* RHS of LIKE/GLOB operator */ |
| 1256 | int isComplete = 0; /* RHS of LIKE/GLOB ends with wildcard */ |
drh | a9c18a9 | 2015-03-06 20:49:52 +0000 | [diff] [blame] | 1257 | int noCase = 0; /* uppercase equivalent to lowercase */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1258 | int op; /* Top-level operator. pExpr->op */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1259 | Parse *pParse = pWInfo->pParse; /* Parsing context */ |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1260 | sqlite3 *db = pParse->db; /* Database connection */ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1261 | |
drh | f998b73 | 2007-11-26 13:36:00 +0000 | [diff] [blame] | 1262 | if( db->mallocFailed ){ |
| 1263 | return; |
| 1264 | } |
| 1265 | pTerm = &pWC->a[idxTerm]; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 1266 | pMaskSet = &pWInfo->sMaskSet; |
drh | 7ee751d | 2012-12-19 15:53:51 +0000 | [diff] [blame] | 1267 | pExpr = pTerm->pExpr; |
| 1268 | assert( pExpr->op!=TK_AS && pExpr->op!=TK_COLLATE ); |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1269 | prereqLeft = exprTableUsage(pMaskSet, pExpr->pLeft); |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 1270 | op = pExpr->op; |
| 1271 | if( op==TK_IN ){ |
drh | f5b1138 | 2005-09-17 13:07:13 +0000 | [diff] [blame] | 1272 | assert( pExpr->pRight==0 ); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1273 | if( ExprHasProperty(pExpr, EP_xIsSelect) ){ |
| 1274 | pTerm->prereqRight = exprSelectTableUsage(pMaskSet, pExpr->x.pSelect); |
| 1275 | }else{ |
| 1276 | pTerm->prereqRight = exprListTableUsage(pMaskSet, pExpr->x.pList); |
| 1277 | } |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 1278 | }else if( op==TK_ISNULL ){ |
| 1279 | pTerm->prereqRight = 0; |
drh | f5b1138 | 2005-09-17 13:07:13 +0000 | [diff] [blame] | 1280 | }else{ |
| 1281 | pTerm->prereqRight = exprTableUsage(pMaskSet, pExpr->pRight); |
| 1282 | } |
drh | 22d6a53 | 2005-09-19 21:05:48 +0000 | [diff] [blame] | 1283 | prereqAll = exprTableUsage(pMaskSet, pExpr); |
| 1284 | if( ExprHasProperty(pExpr, EP_FromJoin) ){ |
drh | 42165be | 2008-03-26 14:56:34 +0000 | [diff] [blame] | 1285 | Bitmask x = getMask(pMaskSet, pExpr->iRightJoinTable); |
| 1286 | prereqAll |= x; |
drh | dafc0ce | 2008-04-17 19:14:02 +0000 | [diff] [blame] | 1287 | extraRight = x-1; /* ON clause terms may not be used with an index |
| 1288 | ** on left table of a LEFT JOIN. Ticket #3015 */ |
drh | 22d6a53 | 2005-09-19 21:05:48 +0000 | [diff] [blame] | 1289 | } |
| 1290 | pTerm->prereqAll = prereqAll; |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1291 | pTerm->leftCursor = -1; |
drh | 45b1ee4 | 2005-08-02 17:48:22 +0000 | [diff] [blame] | 1292 | pTerm->iParent = -1; |
drh | b52076c | 2006-01-23 13:22:09 +0000 | [diff] [blame] | 1293 | pTerm->eOperator = 0; |
drh | 738fc79 | 2013-01-17 15:05:17 +0000 | [diff] [blame] | 1294 | if( allowedOp(op) ){ |
drh | 7a66da1 | 2012-12-07 20:31:11 +0000 | [diff] [blame] | 1295 | Expr *pLeft = sqlite3ExprSkipCollate(pExpr->pLeft); |
| 1296 | Expr *pRight = sqlite3ExprSkipCollate(pExpr->pRight); |
drh | 738fc79 | 2013-01-17 15:05:17 +0000 | [diff] [blame] | 1297 | u16 opMask = (pTerm->prereqRight & prereqLeft)==0 ? WO_ALL : WO_EQUIV; |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1298 | if( pLeft->op==TK_COLUMN ){ |
| 1299 | pTerm->leftCursor = pLeft->iTable; |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 1300 | pTerm->u.leftColumn = pLeft->iColumn; |
drh | 738fc79 | 2013-01-17 15:05:17 +0000 | [diff] [blame] | 1301 | pTerm->eOperator = operatorMask(op) & opMask; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1302 | } |
drh | 9be1870 | 2015-05-13 19:33:41 +0000 | [diff] [blame] | 1303 | if( op==TK_IS ) pTerm->wtFlags |= TERM_IS; |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1304 | if( pRight && pRight->op==TK_COLUMN ){ |
| 1305 | WhereTerm *pNew; |
| 1306 | Expr *pDup; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1307 | u16 eExtraOp = 0; /* Extra bits for pNew->eOperator */ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1308 | if( pTerm->leftCursor>=0 ){ |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1309 | int idxNew; |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1310 | pDup = sqlite3ExprDup(db, pExpr, 0); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1311 | if( db->mallocFailed ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1312 | sqlite3ExprDelete(db, pDup); |
drh | 28f4591 | 2006-10-18 23:26:38 +0000 | [diff] [blame] | 1313 | return; |
| 1314 | } |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1315 | idxNew = whereClauseInsert(pWC, pDup, TERM_VIRTUAL|TERM_DYNAMIC); |
| 1316 | if( idxNew==0 ) return; |
| 1317 | pNew = &pWC->a[idxNew]; |
drh | 9769efc | 2014-10-24 14:32:21 +0000 | [diff] [blame] | 1318 | markTermAsChild(pWC, idxNew, idxTerm); |
drh | ea19cc1 | 2015-05-16 19:17:17 +0000 | [diff] [blame] | 1319 | if( op==TK_IS ) pNew->wtFlags |= TERM_IS; |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1320 | pTerm = &pWC->a[idxTerm]; |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 1321 | pTerm->wtFlags |= TERM_COPIED; |
drh | ea19cc1 | 2015-05-16 19:17:17 +0000 | [diff] [blame] | 1322 | |
drh | 69c15fe | 2015-05-18 11:34:52 +0000 | [diff] [blame] | 1323 | if( termIsEquivalence(pParse, pDup) ){ |
drh | 5820118 | 2015-05-16 20:51:25 +0000 | [diff] [blame] | 1324 | pTerm->eOperator |= WO_EQUIV; |
| 1325 | eExtraOp = WO_EQUIV; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1326 | } |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1327 | }else{ |
| 1328 | pDup = pExpr; |
| 1329 | pNew = pTerm; |
| 1330 | } |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 1331 | exprCommute(pParse, pDup); |
drh | fb76f5a | 2012-12-08 14:16:47 +0000 | [diff] [blame] | 1332 | pLeft = sqlite3ExprSkipCollate(pDup->pLeft); |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1333 | pNew->leftCursor = pLeft->iTable; |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 1334 | pNew->u.leftColumn = pLeft->iColumn; |
drh | 5e767c5 | 2010-02-25 04:15:47 +0000 | [diff] [blame] | 1335 | testcase( (prereqLeft | extraRight) != prereqLeft ); |
| 1336 | pNew->prereqRight = prereqLeft | extraRight; |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1337 | pNew->prereqAll = prereqAll; |
drh | 738fc79 | 2013-01-17 15:05:17 +0000 | [diff] [blame] | 1338 | pNew->eOperator = (operatorMask(pDup->op) + eExtraOp) & opMask; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1339 | } |
| 1340 | } |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 1341 | |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1342 | #ifndef SQLITE_OMIT_BETWEEN_OPTIMIZATION |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 1343 | /* If a term is the BETWEEN operator, create two new virtual terms |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1344 | ** that define the range that the BETWEEN implements. For example: |
| 1345 | ** |
| 1346 | ** a BETWEEN b AND c |
| 1347 | ** |
| 1348 | ** is converted into: |
| 1349 | ** |
| 1350 | ** (a BETWEEN b AND c) AND (a>=b) AND (a<=c) |
| 1351 | ** |
| 1352 | ** The two new terms are added onto the end of the WhereClause object. |
| 1353 | ** The new terms are "dynamic" and are children of the original BETWEEN |
| 1354 | ** term. That means that if the BETWEEN term is coded, the children are |
| 1355 | ** skipped. Or, if the children are satisfied by an index, the original |
| 1356 | ** BETWEEN term is skipped. |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 1357 | */ |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 1358 | else if( pExpr->op==TK_BETWEEN && pWC->op==TK_AND ){ |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1359 | ExprList *pList = pExpr->x.pList; |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 1360 | int i; |
| 1361 | static const u8 ops[] = {TK_GE, TK_LE}; |
| 1362 | assert( pList!=0 ); |
| 1363 | assert( pList->nExpr==2 ); |
| 1364 | for(i=0; i<2; i++){ |
| 1365 | Expr *pNewExpr; |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1366 | int idxNew; |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 1367 | pNewExpr = sqlite3PExpr(pParse, ops[i], |
| 1368 | sqlite3ExprDup(db, pExpr->pLeft, 0), |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1369 | sqlite3ExprDup(db, pList->a[i].pExpr, 0), 0); |
drh | d41d39f | 2013-08-28 16:27:01 +0000 | [diff] [blame] | 1370 | transferJoinMarkings(pNewExpr, pExpr); |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1371 | idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC); |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 1372 | testcase( idxNew==0 ); |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 1373 | exprAnalyze(pSrc, pWC, idxNew); |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1374 | pTerm = &pWC->a[idxTerm]; |
drh | 9769efc | 2014-10-24 14:32:21 +0000 | [diff] [blame] | 1375 | markTermAsChild(pWC, idxNew, idxTerm); |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 1376 | } |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 1377 | } |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1378 | #endif /* SQLITE_OMIT_BETWEEN_OPTIMIZATION */ |
drh | ed37800 | 2005-07-28 23:12:08 +0000 | [diff] [blame] | 1379 | |
danielk1977 | 1576cd9 | 2006-01-14 08:02:28 +0000 | [diff] [blame] | 1380 | #if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY) |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1381 | /* Analyze a term that is composed of two or more subterms connected by |
| 1382 | ** an OR operator. |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 1383 | */ |
| 1384 | else if( pExpr->op==TK_OR ){ |
drh | 2943525 | 2008-12-28 18:35:08 +0000 | [diff] [blame] | 1385 | assert( pWC->op==TK_AND ); |
drh | 1a58fe0 | 2008-12-20 02:06:13 +0000 | [diff] [blame] | 1386 | exprAnalyzeOrTerm(pSrc, pWC, idxTerm); |
danielk1977 | f51d1bd | 2009-07-31 06:14:51 +0000 | [diff] [blame] | 1387 | pTerm = &pWC->a[idxTerm]; |
drh | 6c30be8 | 2005-07-29 15:10:17 +0000 | [diff] [blame] | 1388 | } |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1389 | #endif /* SQLITE_OMIT_OR_OPTIMIZATION */ |
| 1390 | |
| 1391 | #ifndef SQLITE_OMIT_LIKE_OPTIMIZATION |
| 1392 | /* Add constraints to reduce the search space on a LIKE or GLOB |
| 1393 | ** operator. |
drh | 9f504ea | 2008-02-23 21:55:39 +0000 | [diff] [blame] | 1394 | ** |
drh | a9c18a9 | 2015-03-06 20:49:52 +0000 | [diff] [blame] | 1395 | ** A like pattern of the form "x LIKE 'aBc%'" is changed into constraints |
drh | 9f504ea | 2008-02-23 21:55:39 +0000 | [diff] [blame] | 1396 | ** |
drh | a9c18a9 | 2015-03-06 20:49:52 +0000 | [diff] [blame] | 1397 | ** x>='ABC' AND x<'abd' AND x LIKE 'aBc%' |
drh | 9f504ea | 2008-02-23 21:55:39 +0000 | [diff] [blame] | 1398 | ** |
| 1399 | ** The last character of the prefix "abc" is incremented to form the |
drh | a9c18a9 | 2015-03-06 20:49:52 +0000 | [diff] [blame] | 1400 | ** termination condition "abd". If case is not significant (the default |
| 1401 | ** for LIKE) then the lower-bound is made all uppercase and the upper- |
| 1402 | ** bound is made all lowercase so that the bounds also work when comparing |
| 1403 | ** BLOBs. |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1404 | */ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1405 | if( pWC->op==TK_AND |
| 1406 | && isLikeOrGlob(pParse, pExpr, &pStr1, &isComplete, &noCase) |
| 1407 | ){ |
drh | 1d452e1 | 2009-11-01 19:26:59 +0000 | [diff] [blame] | 1408 | Expr *pLeft; /* LHS of LIKE/GLOB operator */ |
| 1409 | Expr *pStr2; /* Copy of pStr1 - RHS of LIKE/GLOB operator */ |
| 1410 | Expr *pNewExpr1; |
| 1411 | Expr *pNewExpr2; |
| 1412 | int idxNew1; |
| 1413 | int idxNew2; |
dan | 80103fc | 2015-03-20 08:43:59 +0000 | [diff] [blame] | 1414 | const char *zCollSeqName; /* Name of collating sequence */ |
drh | 8f1a7ed | 2015-03-06 19:47:38 +0000 | [diff] [blame] | 1415 | const u16 wtFlags = TERM_LIKEOPT | TERM_VIRTUAL | TERM_DYNAMIC; |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1416 | |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1417 | pLeft = pExpr->x.pList->a[1].pExpr; |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1418 | pStr2 = sqlite3ExprDup(db, pStr1, 0); |
drh | 8f1a7ed | 2015-03-06 19:47:38 +0000 | [diff] [blame] | 1419 | |
| 1420 | /* Convert the lower bound to upper-case and the upper bound to |
| 1421 | ** lower-case (upper-case is less than lower-case in ASCII) so that |
| 1422 | ** the range constraints also work for BLOBs |
| 1423 | */ |
| 1424 | if( noCase && !pParse->db->mallocFailed ){ |
| 1425 | int i; |
| 1426 | char c; |
drh | a9c18a9 | 2015-03-06 20:49:52 +0000 | [diff] [blame] | 1427 | pTerm->wtFlags |= TERM_LIKE; |
drh | 8f1a7ed | 2015-03-06 19:47:38 +0000 | [diff] [blame] | 1428 | for(i=0; (c = pStr1->u.zToken[i])!=0; i++){ |
| 1429 | pStr1->u.zToken[i] = sqlite3Toupper(c); |
| 1430 | pStr2->u.zToken[i] = sqlite3Tolower(c); |
| 1431 | } |
| 1432 | } |
| 1433 | |
drh | f998b73 | 2007-11-26 13:36:00 +0000 | [diff] [blame] | 1434 | if( !db->mallocFailed ){ |
drh | 254993e | 2009-06-08 19:44:36 +0000 | [diff] [blame] | 1435 | u8 c, *pC; /* Last character before the first wildcard */ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 1436 | pC = (u8*)&pStr2->u.zToken[sqlite3Strlen30(pStr2->u.zToken)-1]; |
drh | 9f504ea | 2008-02-23 21:55:39 +0000 | [diff] [blame] | 1437 | c = *pC; |
drh | 02a50b7 | 2008-05-26 18:33:40 +0000 | [diff] [blame] | 1438 | if( noCase ){ |
drh | 254993e | 2009-06-08 19:44:36 +0000 | [diff] [blame] | 1439 | /* The point is to increment the last character before the first |
| 1440 | ** wildcard. But if we increment '@', that will push it into the |
| 1441 | ** alphabetic range where case conversions will mess up the |
| 1442 | ** inequality. To avoid this, make sure to also run the full |
| 1443 | ** LIKE on all candidate expressions by clearing the isComplete flag |
| 1444 | */ |
drh | 3975974 | 2013-08-02 23:40:45 +0000 | [diff] [blame] | 1445 | if( c=='A'-1 ) isComplete = 0; |
drh | 02a50b7 | 2008-05-26 18:33:40 +0000 | [diff] [blame] | 1446 | c = sqlite3UpperToLower[c]; |
| 1447 | } |
drh | 9f504ea | 2008-02-23 21:55:39 +0000 | [diff] [blame] | 1448 | *pC = c + 1; |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1449 | } |
dan | 80103fc | 2015-03-20 08:43:59 +0000 | [diff] [blame] | 1450 | zCollSeqName = noCase ? "NOCASE" : "BINARY"; |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 1451 | pNewExpr1 = sqlite3ExprDup(db, pLeft, 0); |
drh | 8f1a7ed | 2015-03-06 19:47:38 +0000 | [diff] [blame] | 1452 | pNewExpr1 = sqlite3PExpr(pParse, TK_GE, |
dan | 80103fc | 2015-03-20 08:43:59 +0000 | [diff] [blame] | 1453 | sqlite3ExprAddCollateString(pParse,pNewExpr1,zCollSeqName), |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 1454 | pStr1, 0); |
drh | d41d39f | 2013-08-28 16:27:01 +0000 | [diff] [blame] | 1455 | transferJoinMarkings(pNewExpr1, pExpr); |
drh | 8f1a7ed | 2015-03-06 19:47:38 +0000 | [diff] [blame] | 1456 | idxNew1 = whereClauseInsert(pWC, pNewExpr1, wtFlags); |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 1457 | testcase( idxNew1==0 ); |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 1458 | exprAnalyze(pSrc, pWC, idxNew1); |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 1459 | pNewExpr2 = sqlite3ExprDup(db, pLeft, 0); |
drh | 8342e49 | 2010-07-22 17:49:52 +0000 | [diff] [blame] | 1460 | pNewExpr2 = sqlite3PExpr(pParse, TK_LT, |
dan | 80103fc | 2015-03-20 08:43:59 +0000 | [diff] [blame] | 1461 | sqlite3ExprAddCollateString(pParse,pNewExpr2,zCollSeqName), |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 1462 | pStr2, 0); |
drh | d41d39f | 2013-08-28 16:27:01 +0000 | [diff] [blame] | 1463 | transferJoinMarkings(pNewExpr2, pExpr); |
drh | 8f1a7ed | 2015-03-06 19:47:38 +0000 | [diff] [blame] | 1464 | idxNew2 = whereClauseInsert(pWC, pNewExpr2, wtFlags); |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 1465 | testcase( idxNew2==0 ); |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 1466 | exprAnalyze(pSrc, pWC, idxNew2); |
drh | 9eb2028 | 2005-08-24 03:52:18 +0000 | [diff] [blame] | 1467 | pTerm = &pWC->a[idxTerm]; |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1468 | if( isComplete ){ |
drh | 9769efc | 2014-10-24 14:32:21 +0000 | [diff] [blame] | 1469 | markTermAsChild(pWC, idxNew1, idxTerm); |
| 1470 | markTermAsChild(pWC, idxNew2, idxTerm); |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1471 | } |
| 1472 | } |
| 1473 | #endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */ |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1474 | |
| 1475 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 1476 | /* Add a WO_MATCH auxiliary term to the constraint set if the |
| 1477 | ** current expression is of the form: column MATCH expr. |
| 1478 | ** This information is used by the xBestIndex methods of |
| 1479 | ** virtual tables. The native query optimizer does not attempt |
| 1480 | ** to do anything with MATCH functions. |
| 1481 | */ |
| 1482 | if( isMatchOfColumn(pExpr) ){ |
| 1483 | int idxNew; |
| 1484 | Expr *pRight, *pLeft; |
| 1485 | WhereTerm *pNewTerm; |
| 1486 | Bitmask prereqColumn, prereqExpr; |
| 1487 | |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1488 | pRight = pExpr->x.pList->a[0].pExpr; |
| 1489 | pLeft = pExpr->x.pList->a[1].pExpr; |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1490 | prereqExpr = exprTableUsage(pMaskSet, pRight); |
| 1491 | prereqColumn = exprTableUsage(pMaskSet, pLeft); |
| 1492 | if( (prereqExpr & prereqColumn)==0 ){ |
drh | 1a90e09 | 2006-06-14 22:07:10 +0000 | [diff] [blame] | 1493 | Expr *pNewExpr; |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 1494 | pNewExpr = sqlite3PExpr(pParse, TK_MATCH, |
| 1495 | 0, sqlite3ExprDup(db, pRight, 0), 0); |
drh | 1a90e09 | 2006-06-14 22:07:10 +0000 | [diff] [blame] | 1496 | idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC); |
drh | 6a1e071 | 2008-12-05 15:24:15 +0000 | [diff] [blame] | 1497 | testcase( idxNew==0 ); |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1498 | pNewTerm = &pWC->a[idxNew]; |
| 1499 | pNewTerm->prereqRight = prereqExpr; |
| 1500 | pNewTerm->leftCursor = pLeft->iTable; |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 1501 | pNewTerm->u.leftColumn = pLeft->iColumn; |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1502 | pNewTerm->eOperator = WO_MATCH; |
drh | 9769efc | 2014-10-24 14:32:21 +0000 | [diff] [blame] | 1503 | markTermAsChild(pWC, idxNew, idxTerm); |
drh | d2ca60d | 2006-06-27 02:36:58 +0000 | [diff] [blame] | 1504 | pTerm = &pWC->a[idxTerm]; |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 1505 | pTerm->wtFlags |= TERM_COPIED; |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 1506 | pNewTerm->prereqAll = pTerm->prereqAll; |
| 1507 | } |
| 1508 | } |
| 1509 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
drh | dafc0ce | 2008-04-17 19:14:02 +0000 | [diff] [blame] | 1510 | |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 1511 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
drh | d3ed734 | 2011-09-21 00:09:41 +0000 | [diff] [blame] | 1512 | /* When sqlite_stat3 histogram data is available an operator of the |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 1513 | ** form "x IS NOT NULL" can sometimes be evaluated more efficiently |
| 1514 | ** as "x>NULL" if x is not an INTEGER PRIMARY KEY. So construct a |
| 1515 | ** virtual term of that form. |
| 1516 | ** |
drh | 9be1870 | 2015-05-13 19:33:41 +0000 | [diff] [blame] | 1517 | ** Note that the virtual term must be tagged with TERM_VNULL. |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 1518 | */ |
drh | ea6dc44 | 2011-04-08 21:35:26 +0000 | [diff] [blame] | 1519 | if( pExpr->op==TK_NOTNULL |
| 1520 | && pExpr->pLeft->op==TK_COLUMN |
| 1521 | && pExpr->pLeft->iColumn>=0 |
drh | d7d7147 | 2014-10-22 19:57:16 +0000 | [diff] [blame] | 1522 | && OptimizationEnabled(db, SQLITE_Stat34) |
drh | ea6dc44 | 2011-04-08 21:35:26 +0000 | [diff] [blame] | 1523 | ){ |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 1524 | Expr *pNewExpr; |
| 1525 | Expr *pLeft = pExpr->pLeft; |
| 1526 | int idxNew; |
| 1527 | WhereTerm *pNewTerm; |
| 1528 | |
| 1529 | pNewExpr = sqlite3PExpr(pParse, TK_GT, |
| 1530 | sqlite3ExprDup(db, pLeft, 0), |
| 1531 | sqlite3PExpr(pParse, TK_NULL, 0, 0, 0), 0); |
| 1532 | |
| 1533 | idxNew = whereClauseInsert(pWC, pNewExpr, |
drh | 9be1870 | 2015-05-13 19:33:41 +0000 | [diff] [blame] | 1534 | TERM_VIRTUAL|TERM_DYNAMIC|TERM_VNULL); |
drh | da91e71 | 2011-02-11 06:59:02 +0000 | [diff] [blame] | 1535 | if( idxNew ){ |
| 1536 | pNewTerm = &pWC->a[idxNew]; |
| 1537 | pNewTerm->prereqRight = 0; |
| 1538 | pNewTerm->leftCursor = pLeft->iTable; |
| 1539 | pNewTerm->u.leftColumn = pLeft->iColumn; |
| 1540 | pNewTerm->eOperator = WO_GT; |
drh | 9769efc | 2014-10-24 14:32:21 +0000 | [diff] [blame] | 1541 | markTermAsChild(pWC, idxNew, idxTerm); |
drh | da91e71 | 2011-02-11 06:59:02 +0000 | [diff] [blame] | 1542 | pTerm = &pWC->a[idxTerm]; |
drh | da91e71 | 2011-02-11 06:59:02 +0000 | [diff] [blame] | 1543 | pTerm->wtFlags |= TERM_COPIED; |
| 1544 | pNewTerm->prereqAll = pTerm->prereqAll; |
| 1545 | } |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 1546 | } |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 1547 | #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 1548 | |
drh | dafc0ce | 2008-04-17 19:14:02 +0000 | [diff] [blame] | 1549 | /* Prevent ON clause terms of a LEFT JOIN from being used to drive |
| 1550 | ** an index for tables to the left of the join. |
| 1551 | */ |
| 1552 | pTerm->prereqRight |= extraRight; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1553 | } |
| 1554 | |
drh | 7b4fc6a | 2007-02-06 13:26:32 +0000 | [diff] [blame] | 1555 | /* |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 1556 | ** This function searches pList for an entry that matches the iCol-th column |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 1557 | ** of index pIdx. |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1558 | ** |
| 1559 | ** If such an expression is found, its index in pList->a[] is returned. If |
| 1560 | ** no expression is found, -1 is returned. |
| 1561 | */ |
| 1562 | static int findIndexCol( |
| 1563 | Parse *pParse, /* Parse context */ |
| 1564 | ExprList *pList, /* Expression list to search */ |
| 1565 | int iBase, /* Cursor for table associated with pIdx */ |
| 1566 | Index *pIdx, /* Index to match column of */ |
| 1567 | int iCol /* Column of index to match */ |
| 1568 | ){ |
| 1569 | int i; |
| 1570 | const char *zColl = pIdx->azColl[iCol]; |
| 1571 | |
| 1572 | for(i=0; i<pList->nExpr; i++){ |
drh | 580c8c1 | 2012-12-08 03:34:04 +0000 | [diff] [blame] | 1573 | Expr *p = sqlite3ExprSkipCollate(pList->a[i].pExpr); |
drh | f1d3e32 | 2011-07-09 13:00:41 +0000 | [diff] [blame] | 1574 | if( p->op==TK_COLUMN |
| 1575 | && p->iColumn==pIdx->aiColumn[iCol] |
| 1576 | && p->iTable==iBase |
| 1577 | ){ |
drh | 580c8c1 | 2012-12-08 03:34:04 +0000 | [diff] [blame] | 1578 | CollSeq *pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr); |
drh | 65df68e | 2015-04-15 05:31:02 +0000 | [diff] [blame] | 1579 | if( pColl && 0==sqlite3StrICmp(pColl->zName, zColl) ){ |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1580 | return i; |
| 1581 | } |
| 1582 | } |
| 1583 | } |
| 1584 | |
| 1585 | return -1; |
| 1586 | } |
| 1587 | |
| 1588 | /* |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1589 | ** Return true if the DISTINCT expression-list passed as the third argument |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 1590 | ** is redundant. |
| 1591 | ** |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 1592 | ** A DISTINCT list is redundant if the database contains some subset of |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 1593 | ** columns that are unique and non-null. |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1594 | */ |
| 1595 | static int isDistinctRedundant( |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 1596 | Parse *pParse, /* Parsing context */ |
| 1597 | SrcList *pTabList, /* The FROM clause */ |
| 1598 | WhereClause *pWC, /* The WHERE clause */ |
| 1599 | ExprList *pDistinct /* The result set that needs to be DISTINCT */ |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1600 | ){ |
| 1601 | Table *pTab; |
| 1602 | Index *pIdx; |
| 1603 | int i; |
| 1604 | int iBase; |
| 1605 | |
| 1606 | /* If there is more than one table or sub-select in the FROM clause of |
| 1607 | ** this query, then it will not be possible to show that the DISTINCT |
| 1608 | ** clause is redundant. */ |
| 1609 | if( pTabList->nSrc!=1 ) return 0; |
| 1610 | iBase = pTabList->a[0].iCursor; |
| 1611 | pTab = pTabList->a[0].pTab; |
| 1612 | |
dan | 94e08d9 | 2011-07-02 06:44:05 +0000 | [diff] [blame] | 1613 | /* If any of the expressions is an IPK column on table iBase, then return |
| 1614 | ** true. Note: The (p->iTable==iBase) part of this test may be false if the |
| 1615 | ** current SELECT is a correlated sub-query. |
| 1616 | */ |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1617 | for(i=0; i<pDistinct->nExpr; i++){ |
drh | 580c8c1 | 2012-12-08 03:34:04 +0000 | [diff] [blame] | 1618 | Expr *p = sqlite3ExprSkipCollate(pDistinct->a[i].pExpr); |
dan | 94e08d9 | 2011-07-02 06:44:05 +0000 | [diff] [blame] | 1619 | if( p->op==TK_COLUMN && p->iTable==iBase && p->iColumn<0 ) return 1; |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1620 | } |
| 1621 | |
| 1622 | /* Loop through all indices on the table, checking each to see if it makes |
| 1623 | ** the DISTINCT qualifier redundant. It does so if: |
| 1624 | ** |
| 1625 | ** 1. The index is itself UNIQUE, and |
| 1626 | ** |
| 1627 | ** 2. All of the columns in the index are either part of the pDistinct |
| 1628 | ** list, or else the WHERE clause contains a term of the form "col=X", |
| 1629 | ** where X is a constant value. The collation sequences of the |
| 1630 | ** comparison and select-list expressions must match those of the index. |
dan | 6a36f43 | 2012-04-20 16:59:24 +0000 | [diff] [blame] | 1631 | ** |
| 1632 | ** 3. All of those index columns for which the WHERE clause does not |
| 1633 | ** contain a "col=X" term are subject to a NOT NULL constraint. |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1634 | */ |
| 1635 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
drh | 5f1d1d9 | 2014-07-31 22:59:04 +0000 | [diff] [blame] | 1636 | if( !IsUniqueIndex(pIdx) ) continue; |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 1637 | for(i=0; i<pIdx->nKeyCol; i++){ |
| 1638 | i16 iCol = pIdx->aiColumn[i]; |
dan | 6a36f43 | 2012-04-20 16:59:24 +0000 | [diff] [blame] | 1639 | if( 0==findTerm(pWC, iBase, iCol, ~(Bitmask)0, WO_EQ, pIdx) ){ |
| 1640 | int iIdxCol = findIndexCol(pParse, pDistinct, iBase, pIdx, i); |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 1641 | if( iIdxCol<0 || pTab->aCol[iCol].notNull==0 ){ |
dan | 6a36f43 | 2012-04-20 16:59:24 +0000 | [diff] [blame] | 1642 | break; |
| 1643 | } |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1644 | } |
| 1645 | } |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 1646 | if( i==pIdx->nKeyCol ){ |
dan | 6f34396 | 2011-07-01 18:26:40 +0000 | [diff] [blame] | 1647 | /* This index implies that the DISTINCT qualifier is redundant. */ |
| 1648 | return 1; |
| 1649 | } |
| 1650 | } |
| 1651 | |
| 1652 | return 0; |
| 1653 | } |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 1654 | |
drh | 8636e9c | 2013-06-11 01:50:08 +0000 | [diff] [blame] | 1655 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1656 | /* |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 1657 | ** Estimate the logarithm of the input value to base 2. |
drh | 28c4cf4 | 2005-07-27 20:41:43 +0000 | [diff] [blame] | 1658 | */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 1659 | static LogEst estLog(LogEst N){ |
drh | 696964d | 2014-06-12 15:46:46 +0000 | [diff] [blame] | 1660 | return N<=10 ? 0 : sqlite3LogEst(N) - 33; |
drh | 28c4cf4 | 2005-07-27 20:41:43 +0000 | [diff] [blame] | 1661 | } |
| 1662 | |
drh | 6d209d8 | 2006-06-27 01:54:26 +0000 | [diff] [blame] | 1663 | /* |
| 1664 | ** Two routines for printing the content of an sqlite3_index_info |
| 1665 | ** structure. Used for testing and debugging only. If neither |
| 1666 | ** SQLITE_TEST or SQLITE_DEBUG are defined, then these routines |
| 1667 | ** are no-ops. |
| 1668 | */ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 1669 | #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(WHERETRACE_ENABLED) |
drh | 6d209d8 | 2006-06-27 01:54:26 +0000 | [diff] [blame] | 1670 | static void TRACE_IDX_INPUTS(sqlite3_index_info *p){ |
| 1671 | int i; |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 1672 | if( !sqlite3WhereTrace ) return; |
drh | 6d209d8 | 2006-06-27 01:54:26 +0000 | [diff] [blame] | 1673 | for(i=0; i<p->nConstraint; i++){ |
| 1674 | sqlite3DebugPrintf(" constraint[%d]: col=%d termid=%d op=%d usabled=%d\n", |
| 1675 | i, |
| 1676 | p->aConstraint[i].iColumn, |
| 1677 | p->aConstraint[i].iTermOffset, |
| 1678 | p->aConstraint[i].op, |
| 1679 | p->aConstraint[i].usable); |
| 1680 | } |
| 1681 | for(i=0; i<p->nOrderBy; i++){ |
| 1682 | sqlite3DebugPrintf(" orderby[%d]: col=%d desc=%d\n", |
| 1683 | i, |
| 1684 | p->aOrderBy[i].iColumn, |
| 1685 | p->aOrderBy[i].desc); |
| 1686 | } |
| 1687 | } |
| 1688 | static void TRACE_IDX_OUTPUTS(sqlite3_index_info *p){ |
| 1689 | int i; |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 1690 | if( !sqlite3WhereTrace ) return; |
drh | 6d209d8 | 2006-06-27 01:54:26 +0000 | [diff] [blame] | 1691 | for(i=0; i<p->nConstraint; i++){ |
| 1692 | sqlite3DebugPrintf(" usage[%d]: argvIdx=%d omit=%d\n", |
| 1693 | i, |
| 1694 | p->aConstraintUsage[i].argvIndex, |
| 1695 | p->aConstraintUsage[i].omit); |
| 1696 | } |
| 1697 | sqlite3DebugPrintf(" idxNum=%d\n", p->idxNum); |
| 1698 | sqlite3DebugPrintf(" idxStr=%s\n", p->idxStr); |
| 1699 | sqlite3DebugPrintf(" orderByConsumed=%d\n", p->orderByConsumed); |
| 1700 | sqlite3DebugPrintf(" estimatedCost=%g\n", p->estimatedCost); |
dan | a9f5815 | 2013-11-11 19:01:33 +0000 | [diff] [blame] | 1701 | sqlite3DebugPrintf(" estimatedRows=%lld\n", p->estimatedRows); |
drh | 6d209d8 | 2006-06-27 01:54:26 +0000 | [diff] [blame] | 1702 | } |
| 1703 | #else |
| 1704 | #define TRACE_IDX_INPUTS(A) |
| 1705 | #define TRACE_IDX_OUTPUTS(A) |
| 1706 | #endif |
| 1707 | |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 1708 | #ifndef SQLITE_OMIT_AUTOMATIC_INDEX |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1709 | /* |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1710 | ** Return TRUE if the WHERE clause term pTerm is of a form where it |
| 1711 | ** could be used with an index to access pSrc, assuming an appropriate |
| 1712 | ** index existed. |
| 1713 | */ |
| 1714 | static int termCanDriveIndex( |
| 1715 | WhereTerm *pTerm, /* WHERE clause term to check */ |
| 1716 | struct SrcList_item *pSrc, /* Table we are trying to access */ |
| 1717 | Bitmask notReady /* Tables in outer loops of the join */ |
| 1718 | ){ |
| 1719 | char aff; |
| 1720 | if( pTerm->leftCursor!=pSrc->iCursor ) return 0; |
drh | e8d0c61 | 2015-05-14 01:05:25 +0000 | [diff] [blame] | 1721 | if( (pTerm->eOperator & (WO_EQ|WO_IS))==0 ) return 0; |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1722 | if( (pTerm->prereqRight & notReady)!=0 ) return 0; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 1723 | if( pTerm->u.leftColumn<0 ) return 0; |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1724 | aff = pSrc->pTab->aCol[pTerm->u.leftColumn].affinity; |
| 1725 | if( !sqlite3IndexAffinityOk(pTerm->pExpr, aff) ) return 0; |
drh | e0cc3c2 | 2015-05-13 17:54:08 +0000 | [diff] [blame] | 1726 | testcase( pTerm->pExpr->op==TK_IS ); |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1727 | return 1; |
| 1728 | } |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 1729 | #endif |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1730 | |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 1731 | |
| 1732 | #ifndef SQLITE_OMIT_AUTOMATIC_INDEX |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1733 | /* |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 1734 | ** Generate code to construct the Index object for an automatic index |
| 1735 | ** and to set up the WhereLevel object pLevel so that the code generator |
| 1736 | ** makes use of the automatic index. |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1737 | */ |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 1738 | static void constructAutomaticIndex( |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1739 | Parse *pParse, /* The parsing context */ |
| 1740 | WhereClause *pWC, /* The WHERE clause */ |
| 1741 | struct SrcList_item *pSrc, /* The FROM clause term to get the next index */ |
| 1742 | Bitmask notReady, /* Mask of cursors that are not available */ |
| 1743 | WhereLevel *pLevel /* Write new index here */ |
| 1744 | ){ |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 1745 | int nKeyCol; /* Number of columns in the constructed index */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1746 | WhereTerm *pTerm; /* A single term of the WHERE clause */ |
| 1747 | WhereTerm *pWCEnd; /* End of pWC->a[] */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1748 | Index *pIdx; /* Object describing the transient index */ |
| 1749 | Vdbe *v; /* Prepared statement under construction */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1750 | int addrInit; /* Address of the initialization bypass jump */ |
| 1751 | Table *pTable; /* The table being indexed */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1752 | int addrTop; /* Top of the index fill loop */ |
| 1753 | int regRecord; /* Register holding an index record */ |
| 1754 | int n; /* Column counter */ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1755 | int i; /* Loop counter */ |
| 1756 | int mxBitCol; /* Maximum column in pSrc->colUsed */ |
drh | 424aab8 | 2010-04-06 18:28:20 +0000 | [diff] [blame] | 1757 | CollSeq *pColl; /* Collating sequence to on a column */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 1758 | WhereLoop *pLoop; /* The Loop object */ |
drh | 77e57df | 2013-10-22 14:28:02 +0000 | [diff] [blame] | 1759 | char *zNotUsed; /* Extra space on the end of pIdx */ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1760 | Bitmask idxCols; /* Bitmap of columns used for indexing */ |
| 1761 | Bitmask extraCols; /* Bitmap of additional columns */ |
drh | 8d56e20 | 2013-06-28 23:55:45 +0000 | [diff] [blame] | 1762 | u8 sentWarning = 0; /* True if a warnning has been issued */ |
drh | 059b2d5 | 2014-10-24 19:28:09 +0000 | [diff] [blame] | 1763 | Expr *pPartial = 0; /* Partial Index Expression */ |
| 1764 | int iContinue = 0; /* Jump here to skip excluded rows */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1765 | |
| 1766 | /* Generate code to skip over the creation and initialization of the |
| 1767 | ** transient index on 2nd and subsequent iterations of the loop. */ |
| 1768 | v = pParse->pVdbe; |
| 1769 | assert( v!=0 ); |
drh | 7d17610 | 2014-02-18 03:07:12 +0000 | [diff] [blame] | 1770 | addrInit = sqlite3CodeOnce(pParse); VdbeCoverage(v); |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1771 | |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1772 | /* Count the number of columns that will be added to the index |
| 1773 | ** and used to match WHERE clause constraints */ |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 1774 | nKeyCol = 0; |
drh | 424aab8 | 2010-04-06 18:28:20 +0000 | [diff] [blame] | 1775 | pTable = pSrc->pTab; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1776 | pWCEnd = &pWC->a[pWC->nTerm]; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 1777 | pLoop = pLevel->pWLoop; |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1778 | idxCols = 0; |
drh | 81186b4 | 2013-06-18 01:52:41 +0000 | [diff] [blame] | 1779 | for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){ |
drh | 13cc90c | 2015-02-25 00:24:41 +0000 | [diff] [blame] | 1780 | Expr *pExpr = pTerm->pExpr; |
| 1781 | assert( !ExprHasProperty(pExpr, EP_FromJoin) /* prereq always non-zero */ |
| 1782 | || pExpr->iRightJoinTable!=pSrc->iCursor /* for the right-hand */ |
| 1783 | || pLoop->prereq!=0 ); /* table of a LEFT JOIN */ |
drh | 059b2d5 | 2014-10-24 19:28:09 +0000 | [diff] [blame] | 1784 | if( pLoop->prereq==0 |
drh | 051575c | 2014-10-25 12:28:25 +0000 | [diff] [blame] | 1785 | && (pTerm->wtFlags & TERM_VIRTUAL)==0 |
drh | 13cc90c | 2015-02-25 00:24:41 +0000 | [diff] [blame] | 1786 | && !ExprHasProperty(pExpr, EP_FromJoin) |
| 1787 | && sqlite3ExprIsTableConstant(pExpr, pSrc->iCursor) ){ |
drh | 059b2d5 | 2014-10-24 19:28:09 +0000 | [diff] [blame] | 1788 | pPartial = sqlite3ExprAnd(pParse->db, pPartial, |
drh | 13cc90c | 2015-02-25 00:24:41 +0000 | [diff] [blame] | 1789 | sqlite3ExprDup(pParse->db, pExpr, 0)); |
drh | 059b2d5 | 2014-10-24 19:28:09 +0000 | [diff] [blame] | 1790 | } |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1791 | if( termCanDriveIndex(pTerm, pSrc, notReady) ){ |
| 1792 | int iCol = pTerm->u.leftColumn; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 1793 | Bitmask cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol); |
drh | 52ff8ea | 2010-04-08 14:15:56 +0000 | [diff] [blame] | 1794 | testcase( iCol==BMS ); |
| 1795 | testcase( iCol==BMS-1 ); |
drh | 8d56e20 | 2013-06-28 23:55:45 +0000 | [diff] [blame] | 1796 | if( !sentWarning ){ |
| 1797 | sqlite3_log(SQLITE_WARNING_AUTOINDEX, |
| 1798 | "automatic index on %s(%s)", pTable->zName, |
| 1799 | pTable->aCol[iCol].zName); |
| 1800 | sentWarning = 1; |
| 1801 | } |
drh | 0013e72 | 2010-04-08 00:40:15 +0000 | [diff] [blame] | 1802 | if( (idxCols & cMask)==0 ){ |
drh | 059b2d5 | 2014-10-24 19:28:09 +0000 | [diff] [blame] | 1803 | if( whereLoopResize(pParse->db, pLoop, nKeyCol+1) ){ |
| 1804 | goto end_auto_index_create; |
| 1805 | } |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 1806 | pLoop->aLTerm[nKeyCol++] = pTerm; |
drh | 0013e72 | 2010-04-08 00:40:15 +0000 | [diff] [blame] | 1807 | idxCols |= cMask; |
| 1808 | } |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1809 | } |
| 1810 | } |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 1811 | assert( nKeyCol>0 ); |
| 1812 | pLoop->u.btree.nEq = pLoop->nLTerm = nKeyCol; |
drh | 53b52f7 | 2013-05-31 11:57:39 +0000 | [diff] [blame] | 1813 | pLoop->wsFlags = WHERE_COLUMN_EQ | WHERE_IDX_ONLY | WHERE_INDEXED |
drh | 986b387 | 2013-06-28 21:12:20 +0000 | [diff] [blame] | 1814 | | WHERE_AUTO_INDEX; |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1815 | |
| 1816 | /* Count the number of additional columns needed to create a |
| 1817 | ** covering index. A "covering index" is an index that contains all |
| 1818 | ** columns that are needed by the query. With a covering index, the |
| 1819 | ** original table never needs to be accessed. Automatic indices must |
| 1820 | ** be a covering index because the index will not be updated if the |
| 1821 | ** original table changes and the index and table cannot both be used |
| 1822 | ** if they go out of sync. |
| 1823 | */ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 1824 | extraCols = pSrc->colUsed & (~idxCols | MASKBIT(BMS-1)); |
drh | c3ef4fa | 2014-10-28 15:58:50 +0000 | [diff] [blame] | 1825 | mxBitCol = MIN(BMS-1,pTable->nCol); |
drh | 52ff8ea | 2010-04-08 14:15:56 +0000 | [diff] [blame] | 1826 | testcase( pTable->nCol==BMS-1 ); |
| 1827 | testcase( pTable->nCol==BMS-2 ); |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1828 | for(i=0; i<mxBitCol; i++){ |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 1829 | if( extraCols & MASKBIT(i) ) nKeyCol++; |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1830 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 1831 | if( pSrc->colUsed & MASKBIT(BMS-1) ){ |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 1832 | nKeyCol += pTable->nCol - BMS + 1; |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1833 | } |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1834 | |
| 1835 | /* Construct the Index object to describe this index */ |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 1836 | pIdx = sqlite3AllocateIndexObject(pParse->db, nKeyCol+1, 0, &zNotUsed); |
drh | 059b2d5 | 2014-10-24 19:28:09 +0000 | [diff] [blame] | 1837 | if( pIdx==0 ) goto end_auto_index_create; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 1838 | pLoop->u.btree.pIndex = pIdx; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1839 | pIdx->zName = "auto-index"; |
drh | 424aab8 | 2010-04-06 18:28:20 +0000 | [diff] [blame] | 1840 | pIdx->pTable = pTable; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1841 | n = 0; |
drh | 0013e72 | 2010-04-08 00:40:15 +0000 | [diff] [blame] | 1842 | idxCols = 0; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1843 | for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1844 | if( termCanDriveIndex(pTerm, pSrc, notReady) ){ |
drh | 0013e72 | 2010-04-08 00:40:15 +0000 | [diff] [blame] | 1845 | int iCol = pTerm->u.leftColumn; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 1846 | Bitmask cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol); |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 1847 | testcase( iCol==BMS-1 ); |
| 1848 | testcase( iCol==BMS ); |
drh | 0013e72 | 2010-04-08 00:40:15 +0000 | [diff] [blame] | 1849 | if( (idxCols & cMask)==0 ){ |
| 1850 | Expr *pX = pTerm->pExpr; |
| 1851 | idxCols |= cMask; |
| 1852 | pIdx->aiColumn[n] = pTerm->u.leftColumn; |
| 1853 | pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight); |
drh | 2903183 | 2015-04-15 07:34:25 +0000 | [diff] [blame] | 1854 | pIdx->azColl[n] = pColl ? pColl->zName : "BINARY"; |
drh | 0013e72 | 2010-04-08 00:40:15 +0000 | [diff] [blame] | 1855 | n++; |
| 1856 | } |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1857 | } |
| 1858 | } |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 1859 | assert( (u32)n==pLoop->u.btree.nEq ); |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1860 | |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 1861 | /* Add additional columns needed to make the automatic index into |
| 1862 | ** a covering index */ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1863 | for(i=0; i<mxBitCol; i++){ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 1864 | if( extraCols & MASKBIT(i) ){ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1865 | pIdx->aiColumn[n] = i; |
| 1866 | pIdx->azColl[n] = "BINARY"; |
| 1867 | n++; |
| 1868 | } |
| 1869 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 1870 | if( pSrc->colUsed & MASKBIT(BMS-1) ){ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 1871 | for(i=BMS-1; i<pTable->nCol; i++){ |
| 1872 | pIdx->aiColumn[n] = i; |
| 1873 | pIdx->azColl[n] = "BINARY"; |
| 1874 | n++; |
| 1875 | } |
| 1876 | } |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 1877 | assert( n==nKeyCol ); |
drh | 4415628 | 2013-10-23 22:23:03 +0000 | [diff] [blame] | 1878 | pIdx->aiColumn[n] = -1; |
| 1879 | pIdx->azColl[n] = "BINARY"; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1880 | |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 1881 | /* Create the automatic index */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1882 | assert( pLevel->iIdxCur>=0 ); |
drh | a1f4124 | 2013-05-31 20:00:58 +0000 | [diff] [blame] | 1883 | pLevel->iIdxCur = pParse->nTab++; |
drh | 2ec2fb2 | 2013-11-06 19:59:23 +0000 | [diff] [blame] | 1884 | sqlite3VdbeAddOp2(v, OP_OpenAutoindex, pLevel->iIdxCur, nKeyCol+1); |
| 1885 | sqlite3VdbeSetP4KeyInfo(pParse, pIdx); |
drh | a21a64d | 2010-04-06 22:33:55 +0000 | [diff] [blame] | 1886 | VdbeComment((v, "for %s", pTable->zName)); |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1887 | |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 1888 | /* Fill the automatic index with content */ |
drh | 059b2d5 | 2014-10-24 19:28:09 +0000 | [diff] [blame] | 1889 | sqlite3ExprCachePush(pParse); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 1890 | addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, pLevel->iTabCur); VdbeCoverage(v); |
drh | 059b2d5 | 2014-10-24 19:28:09 +0000 | [diff] [blame] | 1891 | if( pPartial ){ |
| 1892 | iContinue = sqlite3VdbeMakeLabel(v); |
| 1893 | sqlite3ExprIfFalse(pParse, pPartial, iContinue, SQLITE_JUMPIFNULL); |
drh | 051575c | 2014-10-25 12:28:25 +0000 | [diff] [blame] | 1894 | pLoop->wsFlags |= WHERE_PARTIALIDX; |
drh | 059b2d5 | 2014-10-24 19:28:09 +0000 | [diff] [blame] | 1895 | } |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1896 | regRecord = sqlite3GetTempReg(pParse); |
drh | 1c2c0b7 | 2014-01-04 19:27:05 +0000 | [diff] [blame] | 1897 | sqlite3GenerateIndexKey(pParse, pIdx, pLevel->iTabCur, regRecord, 0, 0, 0, 0); |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1898 | sqlite3VdbeAddOp2(v, OP_IdxInsert, pLevel->iIdxCur, regRecord); |
| 1899 | sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT); |
drh | 059b2d5 | 2014-10-24 19:28:09 +0000 | [diff] [blame] | 1900 | if( pPartial ) sqlite3VdbeResolveLabel(v, iContinue); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 1901 | sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1); VdbeCoverage(v); |
drh | a21a64d | 2010-04-06 22:33:55 +0000 | [diff] [blame] | 1902 | sqlite3VdbeChangeP5(v, SQLITE_STMTSTATUS_AUTOINDEX); |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1903 | sqlite3VdbeJumpHere(v, addrTop); |
| 1904 | sqlite3ReleaseTempReg(pParse, regRecord); |
drh | 059b2d5 | 2014-10-24 19:28:09 +0000 | [diff] [blame] | 1905 | sqlite3ExprCachePop(pParse); |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1906 | |
| 1907 | /* Jump here when skipping the initialization */ |
| 1908 | sqlite3VdbeJumpHere(v, addrInit); |
drh | 059b2d5 | 2014-10-24 19:28:09 +0000 | [diff] [blame] | 1909 | |
| 1910 | end_auto_index_create: |
| 1911 | sqlite3ExprDelete(pParse->db, pPartial); |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1912 | } |
drh | c633908 | 2010-04-07 16:54:58 +0000 | [diff] [blame] | 1913 | #endif /* SQLITE_OMIT_AUTOMATIC_INDEX */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 1914 | |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 1915 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 1916 | /* |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 1917 | ** Allocate and populate an sqlite3_index_info structure. It is the |
| 1918 | ** responsibility of the caller to eventually release the structure |
| 1919 | ** by passing the pointer returned by this function to sqlite3_free(). |
| 1920 | */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 1921 | static sqlite3_index_info *allocateIndexInfo( |
| 1922 | Parse *pParse, |
| 1923 | WhereClause *pWC, |
| 1924 | struct SrcList_item *pSrc, |
| 1925 | ExprList *pOrderBy |
| 1926 | ){ |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 1927 | int i, j; |
| 1928 | int nTerm; |
| 1929 | struct sqlite3_index_constraint *pIdxCons; |
| 1930 | struct sqlite3_index_orderby *pIdxOrderBy; |
| 1931 | struct sqlite3_index_constraint_usage *pUsage; |
| 1932 | WhereTerm *pTerm; |
| 1933 | int nOrderBy; |
| 1934 | sqlite3_index_info *pIdxInfo; |
| 1935 | |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 1936 | /* Count the number of possible WHERE clause constraints referring |
| 1937 | ** to this virtual table */ |
| 1938 | for(i=nTerm=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){ |
| 1939 | if( pTerm->leftCursor != pSrc->iCursor ) continue; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1940 | assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) ); |
| 1941 | testcase( pTerm->eOperator & WO_IN ); |
| 1942 | testcase( pTerm->eOperator & WO_ISNULL ); |
drh | ee14587 | 2015-05-14 13:18:47 +0000 | [diff] [blame] | 1943 | testcase( pTerm->eOperator & WO_IS ); |
dan | a4ff825 | 2014-01-20 19:55:33 +0000 | [diff] [blame] | 1944 | testcase( pTerm->eOperator & WO_ALL ); |
drh | ee14587 | 2015-05-14 13:18:47 +0000 | [diff] [blame] | 1945 | if( (pTerm->eOperator & ~(WO_ISNULL|WO_EQUIV|WO_IS))==0 ) continue; |
drh | b425699 | 2011-08-02 01:57:39 +0000 | [diff] [blame] | 1946 | if( pTerm->wtFlags & TERM_VNULL ) continue; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 1947 | nTerm++; |
| 1948 | } |
| 1949 | |
| 1950 | /* If the ORDER BY clause contains only columns in the current |
| 1951 | ** virtual table then allocate space for the aOrderBy part of |
| 1952 | ** the sqlite3_index_info structure. |
| 1953 | */ |
| 1954 | nOrderBy = 0; |
| 1955 | if( pOrderBy ){ |
drh | 56f1b99 | 2012-09-25 14:29:39 +0000 | [diff] [blame] | 1956 | int n = pOrderBy->nExpr; |
| 1957 | for(i=0; i<n; i++){ |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 1958 | Expr *pExpr = pOrderBy->a[i].pExpr; |
| 1959 | if( pExpr->op!=TK_COLUMN || pExpr->iTable!=pSrc->iCursor ) break; |
| 1960 | } |
drh | 56f1b99 | 2012-09-25 14:29:39 +0000 | [diff] [blame] | 1961 | if( i==n){ |
| 1962 | nOrderBy = n; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 1963 | } |
| 1964 | } |
| 1965 | |
| 1966 | /* Allocate the sqlite3_index_info structure |
| 1967 | */ |
| 1968 | pIdxInfo = sqlite3DbMallocZero(pParse->db, sizeof(*pIdxInfo) |
| 1969 | + (sizeof(*pIdxCons) + sizeof(*pUsage))*nTerm |
| 1970 | + sizeof(*pIdxOrderBy)*nOrderBy ); |
| 1971 | if( pIdxInfo==0 ){ |
| 1972 | sqlite3ErrorMsg(pParse, "out of memory"); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 1973 | return 0; |
| 1974 | } |
| 1975 | |
| 1976 | /* Initialize the structure. The sqlite3_index_info structure contains |
| 1977 | ** many fields that are declared "const" to prevent xBestIndex from |
| 1978 | ** changing them. We have to do some funky casting in order to |
| 1979 | ** initialize those fields. |
| 1980 | */ |
| 1981 | pIdxCons = (struct sqlite3_index_constraint*)&pIdxInfo[1]; |
| 1982 | pIdxOrderBy = (struct sqlite3_index_orderby*)&pIdxCons[nTerm]; |
| 1983 | pUsage = (struct sqlite3_index_constraint_usage*)&pIdxOrderBy[nOrderBy]; |
| 1984 | *(int*)&pIdxInfo->nConstraint = nTerm; |
| 1985 | *(int*)&pIdxInfo->nOrderBy = nOrderBy; |
| 1986 | *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint = pIdxCons; |
| 1987 | *(struct sqlite3_index_orderby**)&pIdxInfo->aOrderBy = pIdxOrderBy; |
| 1988 | *(struct sqlite3_index_constraint_usage**)&pIdxInfo->aConstraintUsage = |
| 1989 | pUsage; |
| 1990 | |
| 1991 | for(i=j=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){ |
drh | 281bbe2 | 2012-10-16 23:17:14 +0000 | [diff] [blame] | 1992 | u8 op; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 1993 | if( pTerm->leftCursor != pSrc->iCursor ) continue; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1994 | assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) ); |
| 1995 | testcase( pTerm->eOperator & WO_IN ); |
drh | ee14587 | 2015-05-14 13:18:47 +0000 | [diff] [blame] | 1996 | testcase( pTerm->eOperator & WO_IS ); |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 1997 | testcase( pTerm->eOperator & WO_ISNULL ); |
dan | a4ff825 | 2014-01-20 19:55:33 +0000 | [diff] [blame] | 1998 | testcase( pTerm->eOperator & WO_ALL ); |
drh | e8d0c61 | 2015-05-14 01:05:25 +0000 | [diff] [blame] | 1999 | if( (pTerm->eOperator & ~(WO_ISNULL|WO_EQUIV|WO_IS))==0 ) continue; |
drh | b425699 | 2011-08-02 01:57:39 +0000 | [diff] [blame] | 2000 | if( pTerm->wtFlags & TERM_VNULL ) continue; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2001 | pIdxCons[j].iColumn = pTerm->u.leftColumn; |
| 2002 | pIdxCons[j].iTermOffset = i; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 2003 | op = (u8)pTerm->eOperator & WO_ALL; |
drh | 281bbe2 | 2012-10-16 23:17:14 +0000 | [diff] [blame] | 2004 | if( op==WO_IN ) op = WO_EQ; |
| 2005 | pIdxCons[j].op = op; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2006 | /* The direct assignment in the previous line is possible only because |
| 2007 | ** the WO_ and SQLITE_INDEX_CONSTRAINT_ codes are identical. The |
| 2008 | ** following asserts verify this fact. */ |
| 2009 | assert( WO_EQ==SQLITE_INDEX_CONSTRAINT_EQ ); |
| 2010 | assert( WO_LT==SQLITE_INDEX_CONSTRAINT_LT ); |
| 2011 | assert( WO_LE==SQLITE_INDEX_CONSTRAINT_LE ); |
| 2012 | assert( WO_GT==SQLITE_INDEX_CONSTRAINT_GT ); |
| 2013 | assert( WO_GE==SQLITE_INDEX_CONSTRAINT_GE ); |
| 2014 | assert( WO_MATCH==SQLITE_INDEX_CONSTRAINT_MATCH ); |
drh | 281bbe2 | 2012-10-16 23:17:14 +0000 | [diff] [blame] | 2015 | 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] | 2016 | j++; |
| 2017 | } |
| 2018 | for(i=0; i<nOrderBy; i++){ |
| 2019 | Expr *pExpr = pOrderBy->a[i].pExpr; |
| 2020 | pIdxOrderBy[i].iColumn = pExpr->iColumn; |
| 2021 | pIdxOrderBy[i].desc = pOrderBy->a[i].sortOrder; |
| 2022 | } |
| 2023 | |
| 2024 | return pIdxInfo; |
| 2025 | } |
| 2026 | |
| 2027 | /* |
| 2028 | ** The table object reference passed as the second argument to this function |
| 2029 | ** must represent a virtual table. This function invokes the xBestIndex() |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 2030 | ** method of the virtual table with the sqlite3_index_info object that |
| 2031 | ** comes in as the 3rd argument to this function. |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2032 | ** |
| 2033 | ** If an error occurs, pParse is populated with an error message and a |
| 2034 | ** non-zero value is returned. Otherwise, 0 is returned and the output |
| 2035 | ** part of the sqlite3_index_info structure is left populated. |
| 2036 | ** |
| 2037 | ** Whether or not an error is returned, it is the responsibility of the |
| 2038 | ** caller to eventually free p->idxStr if p->needToFreeIdxStr indicates |
| 2039 | ** that this is required. |
| 2040 | */ |
| 2041 | static int vtabBestIndex(Parse *pParse, Table *pTab, sqlite3_index_info *p){ |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 2042 | sqlite3_vtab *pVtab = sqlite3GetVTable(pParse->db, pTab)->pVtab; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2043 | int i; |
| 2044 | int rc; |
| 2045 | |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2046 | TRACE_IDX_INPUTS(p); |
| 2047 | rc = pVtab->pModule->xBestIndex(pVtab, p); |
| 2048 | TRACE_IDX_OUTPUTS(p); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2049 | |
| 2050 | if( rc!=SQLITE_OK ){ |
| 2051 | if( rc==SQLITE_NOMEM ){ |
| 2052 | pParse->db->mallocFailed = 1; |
| 2053 | }else if( !pVtab->zErrMsg ){ |
| 2054 | sqlite3ErrorMsg(pParse, "%s", sqlite3ErrStr(rc)); |
| 2055 | }else{ |
| 2056 | sqlite3ErrorMsg(pParse, "%s", pVtab->zErrMsg); |
| 2057 | } |
| 2058 | } |
drh | b975598 | 2010-07-24 16:34:37 +0000 | [diff] [blame] | 2059 | sqlite3_free(pVtab->zErrMsg); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2060 | pVtab->zErrMsg = 0; |
| 2061 | |
| 2062 | for(i=0; i<p->nConstraint; i++){ |
| 2063 | if( !p->aConstraint[i].usable && p->aConstraintUsage[i].argvIndex>0 ){ |
| 2064 | sqlite3ErrorMsg(pParse, |
| 2065 | "table %s: xBestIndex returned an invalid plan", pTab->zName); |
| 2066 | } |
| 2067 | } |
| 2068 | |
| 2069 | return pParse->nErr; |
| 2070 | } |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2071 | #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) */ |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 2072 | |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 2073 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
drh | 28c4cf4 | 2005-07-27 20:41:43 +0000 | [diff] [blame] | 2074 | /* |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2075 | ** Estimate the location of a particular key among all keys in an |
| 2076 | ** index. Store the results in aStat as follows: |
drh | e847d32 | 2011-01-20 02:56:37 +0000 | [diff] [blame] | 2077 | ** |
dan | a3d0c13 | 2015-03-14 18:59:58 +0000 | [diff] [blame] | 2078 | ** aStat[0] Est. number of rows less than pRec |
| 2079 | ** aStat[1] Est. number of rows equal to pRec |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2080 | ** |
drh | 6d3f91d | 2014-11-05 19:26:12 +0000 | [diff] [blame] | 2081 | ** Return the index of the sample that is the smallest sample that |
dan | a3d0c13 | 2015-03-14 18:59:58 +0000 | [diff] [blame] | 2082 | ** is greater than or equal to pRec. Note that this index is not an index |
| 2083 | ** into the aSample[] array - it is an index into a virtual set of samples |
| 2084 | ** based on the contents of aSample[] and the number of fields in record |
| 2085 | ** pRec. |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2086 | */ |
drh | 6d3f91d | 2014-11-05 19:26:12 +0000 | [diff] [blame] | 2087 | static int whereKeyStats( |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2088 | Parse *pParse, /* Database connection */ |
| 2089 | Index *pIdx, /* Index to consider domain of */ |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2090 | UnpackedRecord *pRec, /* Vector of values to consider */ |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2091 | int roundUp, /* Round up if true. Round down if false */ |
| 2092 | tRowcnt *aStat /* OUT: stats written here */ |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2093 | ){ |
dan | f52bb8d | 2013-08-03 20:24:58 +0000 | [diff] [blame] | 2094 | IndexSample *aSample = pIdx->aSample; |
drh | fbc38de | 2013-09-03 19:26:22 +0000 | [diff] [blame] | 2095 | int iCol; /* Index of required stats in anEq[] etc. */ |
dan | a3d0c13 | 2015-03-14 18:59:58 +0000 | [diff] [blame] | 2096 | int i; /* Index of first sample >= pRec */ |
| 2097 | int iSample; /* Smallest sample larger than or equal to pRec */ |
dan | 84c309b | 2013-08-08 16:17:12 +0000 | [diff] [blame] | 2098 | int iMin = 0; /* Smallest sample not yet tested */ |
dan | 84c309b | 2013-08-08 16:17:12 +0000 | [diff] [blame] | 2099 | int iTest; /* Next sample to test */ |
| 2100 | int res; /* Result of comparison operation */ |
dan | a3d0c13 | 2015-03-14 18:59:58 +0000 | [diff] [blame] | 2101 | int nField; /* Number of fields in pRec */ |
| 2102 | tRowcnt iLower = 0; /* anLt[] + anEq[] of largest sample pRec is > */ |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2103 | |
drh | 4f99189 | 2013-10-11 15:05:05 +0000 | [diff] [blame] | 2104 | #ifndef SQLITE_DEBUG |
| 2105 | UNUSED_PARAMETER( pParse ); |
| 2106 | #endif |
drh | 7f59475 | 2013-12-03 19:49:55 +0000 | [diff] [blame] | 2107 | assert( pRec!=0 ); |
drh | 5c62486 | 2011-09-22 18:46:34 +0000 | [diff] [blame] | 2108 | assert( pIdx->nSample>0 ); |
dan | a3d0c13 | 2015-03-14 18:59:58 +0000 | [diff] [blame] | 2109 | assert( pRec->nField>0 && pRec->nField<=pIdx->nSampleCol ); |
| 2110 | |
| 2111 | /* Do a binary search to find the first sample greater than or equal |
| 2112 | ** to pRec. If pRec contains a single field, the set of samples to search |
| 2113 | ** is simply the aSample[] array. If the samples in aSample[] contain more |
| 2114 | ** than one fields, all fields following the first are ignored. |
| 2115 | ** |
| 2116 | ** If pRec contains N fields, where N is more than one, then as well as the |
| 2117 | ** samples in aSample[] (truncated to N fields), the search also has to |
| 2118 | ** consider prefixes of those samples. For example, if the set of samples |
| 2119 | ** in aSample is: |
| 2120 | ** |
| 2121 | ** aSample[0] = (a, 5) |
| 2122 | ** aSample[1] = (a, 10) |
| 2123 | ** aSample[2] = (b, 5) |
| 2124 | ** aSample[3] = (c, 100) |
| 2125 | ** aSample[4] = (c, 105) |
| 2126 | ** |
| 2127 | ** Then the search space should ideally be the samples above and the |
| 2128 | ** unique prefixes [a], [b] and [c]. But since that is hard to organize, |
| 2129 | ** the code actually searches this set: |
| 2130 | ** |
| 2131 | ** 0: (a) |
| 2132 | ** 1: (a, 5) |
| 2133 | ** 2: (a, 10) |
| 2134 | ** 3: (a, 10) |
| 2135 | ** 4: (b) |
| 2136 | ** 5: (b, 5) |
| 2137 | ** 6: (c) |
| 2138 | ** 7: (c, 100) |
| 2139 | ** 8: (c, 105) |
| 2140 | ** 9: (c, 105) |
| 2141 | ** |
| 2142 | ** For each sample in the aSample[] array, N samples are present in the |
| 2143 | ** effective sample array. In the above, samples 0 and 1 are based on |
| 2144 | ** sample aSample[0]. Samples 2 and 3 on aSample[1] etc. |
| 2145 | ** |
| 2146 | ** Often, sample i of each block of N effective samples has (i+1) fields. |
| 2147 | ** Except, each sample may be extended to ensure that it is greater than or |
| 2148 | ** equal to the previous sample in the array. For example, in the above, |
| 2149 | ** sample 2 is the first sample of a block of N samples, so at first it |
| 2150 | ** appears that it should be 1 field in size. However, that would make it |
| 2151 | ** smaller than sample 1, so the binary search would not work. As a result, |
| 2152 | ** it is extended to two fields. The duplicates that this creates do not |
| 2153 | ** cause any problems. |
| 2154 | */ |
| 2155 | nField = pRec->nField; |
| 2156 | iCol = 0; |
| 2157 | iSample = pIdx->nSample * nField; |
dan | 84c309b | 2013-08-08 16:17:12 +0000 | [diff] [blame] | 2158 | do{ |
dan | a3d0c13 | 2015-03-14 18:59:58 +0000 | [diff] [blame] | 2159 | int iSamp; /* Index in aSample[] of test sample */ |
| 2160 | int n; /* Number of fields in test sample */ |
| 2161 | |
| 2162 | iTest = (iMin+iSample)/2; |
| 2163 | iSamp = iTest / nField; |
| 2164 | if( iSamp>0 ){ |
| 2165 | /* The proposed effective sample is a prefix of sample aSample[iSamp]. |
| 2166 | ** Specifically, the shortest prefix of at least (1 + iTest%nField) |
| 2167 | ** fields that is greater than the previous effective sample. */ |
| 2168 | for(n=(iTest % nField) + 1; n<nField; n++){ |
| 2169 | if( aSample[iSamp-1].anLt[n-1]!=aSample[iSamp].anLt[n-1] ) break; |
| 2170 | } |
dan | 84c309b | 2013-08-08 16:17:12 +0000 | [diff] [blame] | 2171 | }else{ |
dan | a3d0c13 | 2015-03-14 18:59:58 +0000 | [diff] [blame] | 2172 | n = iTest + 1; |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2173 | } |
dan | a3d0c13 | 2015-03-14 18:59:58 +0000 | [diff] [blame] | 2174 | |
| 2175 | pRec->nField = n; |
| 2176 | res = sqlite3VdbeRecordCompare(aSample[iSamp].n, aSample[iSamp].p, pRec); |
| 2177 | if( res<0 ){ |
| 2178 | iLower = aSample[iSamp].anLt[n-1] + aSample[iSamp].anEq[n-1]; |
| 2179 | iMin = iTest+1; |
| 2180 | }else if( res==0 && n<nField ){ |
| 2181 | iLower = aSample[iSamp].anLt[n-1]; |
| 2182 | iMin = iTest+1; |
| 2183 | res = -1; |
| 2184 | }else{ |
| 2185 | iSample = iTest; |
| 2186 | iCol = n-1; |
| 2187 | } |
| 2188 | }while( res && iMin<iSample ); |
| 2189 | i = iSample / nField; |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2190 | |
dan | 84c309b | 2013-08-08 16:17:12 +0000 | [diff] [blame] | 2191 | #ifdef SQLITE_DEBUG |
| 2192 | /* The following assert statements check that the binary search code |
| 2193 | ** above found the right answer. This block serves no purpose other |
| 2194 | ** than to invoke the asserts. */ |
dan | a3d0c13 | 2015-03-14 18:59:58 +0000 | [diff] [blame] | 2195 | if( pParse->db->mallocFailed==0 ){ |
| 2196 | if( res==0 ){ |
| 2197 | /* If (res==0) is true, then pRec must be equal to sample i. */ |
| 2198 | assert( i<pIdx->nSample ); |
| 2199 | assert( iCol==nField-1 ); |
| 2200 | pRec->nField = nField; |
| 2201 | assert( 0==sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec) |
| 2202 | || pParse->db->mallocFailed |
| 2203 | ); |
| 2204 | }else{ |
| 2205 | /* Unless i==pIdx->nSample, indicating that pRec is larger than |
| 2206 | ** all samples in the aSample[] array, pRec must be smaller than the |
| 2207 | ** (iCol+1) field prefix of sample i. */ |
| 2208 | assert( i<=pIdx->nSample && i>=0 ); |
| 2209 | pRec->nField = iCol+1; |
| 2210 | assert( i==pIdx->nSample |
| 2211 | || sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)>0 |
| 2212 | || pParse->db->mallocFailed ); |
| 2213 | |
| 2214 | /* if i==0 and iCol==0, then record pRec is smaller than all samples |
| 2215 | ** in the aSample[] array. Otherwise, if (iCol>0) then pRec must |
| 2216 | ** be greater than or equal to the (iCol) field prefix of sample i. |
| 2217 | ** If (i>0), then pRec must also be greater than sample (i-1). */ |
| 2218 | if( iCol>0 ){ |
| 2219 | pRec->nField = iCol; |
| 2220 | assert( sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)<=0 |
| 2221 | || pParse->db->mallocFailed ); |
| 2222 | } |
| 2223 | if( i>0 ){ |
| 2224 | pRec->nField = nField; |
| 2225 | assert( sqlite3VdbeRecordCompare(aSample[i-1].n, aSample[i-1].p, pRec)<0 |
| 2226 | || pParse->db->mallocFailed ); |
| 2227 | } |
| 2228 | } |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2229 | } |
dan | 84c309b | 2013-08-08 16:17:12 +0000 | [diff] [blame] | 2230 | #endif /* ifdef SQLITE_DEBUG */ |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2231 | |
dan | 84c309b | 2013-08-08 16:17:12 +0000 | [diff] [blame] | 2232 | if( res==0 ){ |
dan | a3d0c13 | 2015-03-14 18:59:58 +0000 | [diff] [blame] | 2233 | /* Record pRec is equal to sample i */ |
| 2234 | assert( iCol==nField-1 ); |
dan | eea568d | 2013-08-07 19:46:15 +0000 | [diff] [blame] | 2235 | aStat[0] = aSample[i].anLt[iCol]; |
| 2236 | aStat[1] = aSample[i].anEq[iCol]; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2237 | }else{ |
dan | a3d0c13 | 2015-03-14 18:59:58 +0000 | [diff] [blame] | 2238 | /* At this point, the (iCol+1) field prefix of aSample[i] is the first |
| 2239 | ** sample that is greater than pRec. Or, if i==pIdx->nSample then pRec |
| 2240 | ** is larger than all samples in the array. */ |
| 2241 | tRowcnt iUpper, iGap; |
| 2242 | if( i>=pIdx->nSample ){ |
| 2243 | iUpper = sqlite3LogEstToInt(pIdx->aiRowLogEst[0]); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2244 | }else{ |
dan | a3d0c13 | 2015-03-14 18:59:58 +0000 | [diff] [blame] | 2245 | iUpper = aSample[i].anLt[iCol]; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2246 | } |
dan | a3d0c13 | 2015-03-14 18:59:58 +0000 | [diff] [blame] | 2247 | |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2248 | if( iLower>=iUpper ){ |
| 2249 | iGap = 0; |
| 2250 | }else{ |
| 2251 | iGap = iUpper - iLower; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2252 | } |
| 2253 | if( roundUp ){ |
| 2254 | iGap = (iGap*2)/3; |
| 2255 | }else{ |
| 2256 | iGap = iGap/3; |
| 2257 | } |
| 2258 | aStat[0] = iLower + iGap; |
dan | a3d0c13 | 2015-03-14 18:59:58 +0000 | [diff] [blame] | 2259 | aStat[1] = pIdx->aAvgEq[iCol]; |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2260 | } |
dan | a3d0c13 | 2015-03-14 18:59:58 +0000 | [diff] [blame] | 2261 | |
| 2262 | /* Restore the pRec->nField value before returning. */ |
| 2263 | pRec->nField = nField; |
drh | 6d3f91d | 2014-11-05 19:26:12 +0000 | [diff] [blame] | 2264 | return i; |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2265 | } |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 2266 | #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ |
dan | 937d0de | 2009-10-15 18:35:38 +0000 | [diff] [blame] | 2267 | |
| 2268 | /* |
dan | aa9933c | 2014-04-24 20:04:49 +0000 | [diff] [blame] | 2269 | ** If it is not NULL, pTerm is a term that provides an upper or lower |
| 2270 | ** bound on a range scan. Without considering pTerm, it is estimated |
| 2271 | ** that the scan will visit nNew rows. This function returns the number |
| 2272 | ** estimated to be visited after taking pTerm into account. |
| 2273 | ** |
| 2274 | ** If the user explicitly specified a likelihood() value for this term, |
| 2275 | ** then the return value is the likelihood multiplied by the number of |
| 2276 | ** input rows. Otherwise, this function assumes that an "IS NOT NULL" term |
| 2277 | ** has a likelihood of 0.50, and any other term a likelihood of 0.25. |
| 2278 | */ |
| 2279 | static LogEst whereRangeAdjust(WhereTerm *pTerm, LogEst nNew){ |
| 2280 | LogEst nRet = nNew; |
| 2281 | if( pTerm ){ |
| 2282 | if( pTerm->truthProb<=0 ){ |
| 2283 | nRet += pTerm->truthProb; |
dan | 7de2a1f | 2014-04-28 20:11:20 +0000 | [diff] [blame] | 2284 | }else if( (pTerm->wtFlags & TERM_VNULL)==0 ){ |
dan | aa9933c | 2014-04-24 20:04:49 +0000 | [diff] [blame] | 2285 | nRet -= 20; assert( 20==sqlite3LogEst(4) ); |
| 2286 | } |
| 2287 | } |
| 2288 | return nRet; |
| 2289 | } |
| 2290 | |
mistachkin | 2d84ac4 | 2014-06-26 21:32:09 +0000 | [diff] [blame] | 2291 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2292 | /* |
| 2293 | ** This function is called to estimate the number of rows visited by a |
| 2294 | ** range-scan on a skip-scan index. For example: |
| 2295 | ** |
| 2296 | ** CREATE INDEX i1 ON t1(a, b, c); |
| 2297 | ** SELECT * FROM t1 WHERE a=? AND c BETWEEN ? AND ?; |
| 2298 | ** |
| 2299 | ** Value pLoop->nOut is currently set to the estimated number of rows |
| 2300 | ** visited for scanning (a=? AND b=?). This function reduces that estimate |
| 2301 | ** by some factor to account for the (c BETWEEN ? AND ?) expression based |
| 2302 | ** on the stat4 data for the index. this scan will be peformed multiple |
| 2303 | ** times (once for each (a,b) combination that matches a=?) is dealt with |
| 2304 | ** by the caller. |
| 2305 | ** |
| 2306 | ** It does this by scanning through all stat4 samples, comparing values |
| 2307 | ** extracted from pLower and pUpper with the corresponding column in each |
| 2308 | ** sample. If L and U are the number of samples found to be less than or |
| 2309 | ** equal to the values extracted from pLower and pUpper respectively, and |
| 2310 | ** N is the total number of samples, the pLoop->nOut value is adjusted |
| 2311 | ** as follows: |
| 2312 | ** |
| 2313 | ** nOut = nOut * ( min(U - L, 1) / N ) |
| 2314 | ** |
| 2315 | ** If pLower is NULL, or a value cannot be extracted from the term, L is |
| 2316 | ** set to zero. If pUpper is NULL, or a value cannot be extracted from it, |
| 2317 | ** U is set to N. |
| 2318 | ** |
| 2319 | ** Normally, this function sets *pbDone to 1 before returning. However, |
| 2320 | ** if no value can be extracted from either pLower or pUpper (and so the |
| 2321 | ** estimate of the number of rows delivered remains unchanged), *pbDone |
| 2322 | ** is left as is. |
| 2323 | ** |
| 2324 | ** If an error occurs, an SQLite error code is returned. Otherwise, |
| 2325 | ** SQLITE_OK. |
| 2326 | */ |
| 2327 | static int whereRangeSkipScanEst( |
| 2328 | Parse *pParse, /* Parsing & code generating context */ |
| 2329 | WhereTerm *pLower, /* Lower bound on the range. ex: "x>123" Might be NULL */ |
| 2330 | WhereTerm *pUpper, /* Upper bound on the range. ex: "x<455" Might be NULL */ |
| 2331 | WhereLoop *pLoop, /* Update the .nOut value of this loop */ |
| 2332 | int *pbDone /* Set to true if at least one expr. value extracted */ |
| 2333 | ){ |
| 2334 | Index *p = pLoop->u.btree.pIndex; |
| 2335 | int nEq = pLoop->u.btree.nEq; |
| 2336 | sqlite3 *db = pParse->db; |
dan | 4e42ba4 | 2014-06-27 20:14:25 +0000 | [diff] [blame] | 2337 | int nLower = -1; |
| 2338 | int nUpper = p->nSample+1; |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2339 | int rc = SQLITE_OK; |
drh | d15f87e | 2014-07-24 22:41:20 +0000 | [diff] [blame] | 2340 | int iCol = p->aiColumn[nEq]; |
| 2341 | u8 aff = iCol>=0 ? p->pTable->aCol[iCol].affinity : SQLITE_AFF_INTEGER; |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2342 | CollSeq *pColl; |
| 2343 | |
| 2344 | sqlite3_value *p1 = 0; /* Value extracted from pLower */ |
| 2345 | sqlite3_value *p2 = 0; /* Value extracted from pUpper */ |
| 2346 | sqlite3_value *pVal = 0; /* Value extracted from record */ |
| 2347 | |
| 2348 | pColl = sqlite3LocateCollSeq(pParse, p->azColl[nEq]); |
| 2349 | if( pLower ){ |
| 2350 | rc = sqlite3Stat4ValueFromExpr(pParse, pLower->pExpr->pRight, aff, &p1); |
dan | 4e42ba4 | 2014-06-27 20:14:25 +0000 | [diff] [blame] | 2351 | nLower = 0; |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2352 | } |
| 2353 | if( pUpper && rc==SQLITE_OK ){ |
| 2354 | rc = sqlite3Stat4ValueFromExpr(pParse, pUpper->pExpr->pRight, aff, &p2); |
dan | 4e42ba4 | 2014-06-27 20:14:25 +0000 | [diff] [blame] | 2355 | nUpper = p2 ? 0 : p->nSample; |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2356 | } |
| 2357 | |
| 2358 | if( p1 || p2 ){ |
| 2359 | int i; |
| 2360 | int nDiff; |
| 2361 | for(i=0; rc==SQLITE_OK && i<p->nSample; i++){ |
| 2362 | rc = sqlite3Stat4Column(db, p->aSample[i].p, p->aSample[i].n, nEq, &pVal); |
| 2363 | if( rc==SQLITE_OK && p1 ){ |
| 2364 | int res = sqlite3MemCompare(p1, pVal, pColl); |
dan | 4e42ba4 | 2014-06-27 20:14:25 +0000 | [diff] [blame] | 2365 | if( res>=0 ) nLower++; |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2366 | } |
| 2367 | if( rc==SQLITE_OK && p2 ){ |
| 2368 | int res = sqlite3MemCompare(p2, pVal, pColl); |
dan | 4e42ba4 | 2014-06-27 20:14:25 +0000 | [diff] [blame] | 2369 | if( res>=0 ) nUpper++; |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2370 | } |
| 2371 | } |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2372 | nDiff = (nUpper - nLower); |
| 2373 | if( nDiff<=0 ) nDiff = 1; |
dan | 4e42ba4 | 2014-06-27 20:14:25 +0000 | [diff] [blame] | 2374 | |
| 2375 | /* If there is both an upper and lower bound specified, and the |
| 2376 | ** comparisons indicate that they are close together, use the fallback |
| 2377 | ** method (assume that the scan visits 1/64 of the rows) for estimating |
| 2378 | ** the number of rows visited. Otherwise, estimate the number of rows |
| 2379 | ** using the method described in the header comment for this function. */ |
| 2380 | if( nDiff!=1 || pUpper==0 || pLower==0 ){ |
| 2381 | int nAdjust = (sqlite3LogEst(p->nSample) - sqlite3LogEst(nDiff)); |
| 2382 | pLoop->nOut -= nAdjust; |
| 2383 | *pbDone = 1; |
| 2384 | WHERETRACE(0x10, ("range skip-scan regions: %u..%u adjust=%d est=%d\n", |
dan | fa88745 | 2014-06-28 15:26:10 +0000 | [diff] [blame] | 2385 | nLower, nUpper, nAdjust*-1, pLoop->nOut)); |
dan | 4e42ba4 | 2014-06-27 20:14:25 +0000 | [diff] [blame] | 2386 | } |
| 2387 | |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2388 | }else{ |
| 2389 | assert( *pbDone==0 ); |
| 2390 | } |
| 2391 | |
| 2392 | sqlite3ValueFree(p1); |
| 2393 | sqlite3ValueFree(p2); |
| 2394 | sqlite3ValueFree(pVal); |
| 2395 | |
| 2396 | return rc; |
| 2397 | } |
mistachkin | 2d84ac4 | 2014-06-26 21:32:09 +0000 | [diff] [blame] | 2398 | #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2399 | |
dan | aa9933c | 2014-04-24 20:04:49 +0000 | [diff] [blame] | 2400 | /* |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2401 | ** This function is used to estimate the number of rows that will be visited |
| 2402 | ** by scanning an index for a range of values. The range may have an upper |
| 2403 | ** bound, a lower bound, or both. The WHERE clause terms that set the upper |
| 2404 | ** and lower bounds are represented by pLower and pUpper respectively. For |
| 2405 | ** example, assuming that index p is on t1(a): |
| 2406 | ** |
| 2407 | ** ... FROM t1 WHERE a > ? AND a < ? ... |
| 2408 | ** |_____| |_____| |
| 2409 | ** | | |
| 2410 | ** pLower pUpper |
| 2411 | ** |
drh | 98cdf62 | 2009-08-20 18:14:42 +0000 | [diff] [blame] | 2412 | ** 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] | 2413 | ** place of the corresponding WhereTerm. |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2414 | ** |
drh | 6d3f91d | 2014-11-05 19:26:12 +0000 | [diff] [blame] | 2415 | ** 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] | 2416 | ** column subject to the range constraint. Or, equivalently, the number of |
| 2417 | ** equality constraints optimized by the proposed index scan. For example, |
| 2418 | ** assuming index p is on t1(a, b), and the SQL query is: |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2419 | ** |
| 2420 | ** ... FROM t1 WHERE a = ? AND b > ? AND b < ? ... |
| 2421 | ** |
dan | 6cb8d76 | 2013-08-08 11:48:57 +0000 | [diff] [blame] | 2422 | ** then nEq is set to 1 (as the range restricted column, b, is the second |
| 2423 | ** left-most column of the index). Or, if the query is: |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2424 | ** |
| 2425 | ** ... FROM t1 WHERE a > ? AND a < ? ... |
| 2426 | ** |
dan | 6cb8d76 | 2013-08-08 11:48:57 +0000 | [diff] [blame] | 2427 | ** then nEq is set to 0. |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2428 | ** |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 2429 | ** When this function is called, *pnOut is set to the sqlite3LogEst() of the |
dan | 6cb8d76 | 2013-08-08 11:48:57 +0000 | [diff] [blame] | 2430 | ** number of rows that the index scan is expected to visit without |
drh | 6d3f91d | 2014-11-05 19:26:12 +0000 | [diff] [blame] | 2431 | ** 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] | 2432 | ** 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] | 2433 | ** to account for the range constraints pLower and pUpper. |
dan | 6cb8d76 | 2013-08-08 11:48:57 +0000 | [diff] [blame] | 2434 | ** |
| 2435 | ** 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] | 2436 | ** used, a single range inequality reduces the search space by a factor of 4. |
| 2437 | ** and a pair of constraints (x>? AND x<?) reduces the expected number of |
| 2438 | ** rows visited by a factor of 64. |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2439 | */ |
| 2440 | static int whereRangeScanEst( |
drh | cdaca55 | 2009-08-20 13:45:07 +0000 | [diff] [blame] | 2441 | Parse *pParse, /* Parsing & code generating context */ |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2442 | WhereLoopBuilder *pBuilder, |
drh | cdaca55 | 2009-08-20 13:45:07 +0000 | [diff] [blame] | 2443 | WhereTerm *pLower, /* Lower bound on the range. ex: "x>123" Might be NULL */ |
| 2444 | WhereTerm *pUpper, /* Upper bound on the range. ex: "x<455" Might be NULL */ |
drh | 186ad8c | 2013-10-08 18:40:37 +0000 | [diff] [blame] | 2445 | WhereLoop *pLoop /* Modify the .nOut and maybe .rRun fields */ |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2446 | ){ |
dan | 69188d9 | 2009-08-19 08:18:32 +0000 | [diff] [blame] | 2447 | int rc = SQLITE_OK; |
drh | 186ad8c | 2013-10-08 18:40:37 +0000 | [diff] [blame] | 2448 | int nOut = pLoop->nOut; |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 2449 | LogEst nNew; |
dan | 69188d9 | 2009-08-19 08:18:32 +0000 | [diff] [blame] | 2450 | |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 2451 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
drh | 186ad8c | 2013-10-08 18:40:37 +0000 | [diff] [blame] | 2452 | Index *p = pLoop->u.btree.pIndex; |
drh | 4f99189 | 2013-10-11 15:05:05 +0000 | [diff] [blame] | 2453 | int nEq = pLoop->u.btree.nEq; |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2454 | |
drh | 6d3f91d | 2014-11-05 19:26:12 +0000 | [diff] [blame] | 2455 | if( p->nSample>0 && nEq<p->nSampleCol ){ |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2456 | if( nEq==pBuilder->nRecValid ){ |
| 2457 | UnpackedRecord *pRec = pBuilder->pRec; |
| 2458 | tRowcnt a[2]; |
| 2459 | u8 aff; |
drh | 98cdf62 | 2009-08-20 18:14:42 +0000 | [diff] [blame] | 2460 | |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2461 | /* Variable iLower will be set to the estimate of the number of rows in |
| 2462 | ** the index that are less than the lower bound of the range query. The |
| 2463 | ** lower bound being the concatenation of $P and $L, where $P is the |
| 2464 | ** key-prefix formed by the nEq values matched against the nEq left-most |
| 2465 | ** columns of the index, and $L is the value in pLower. |
| 2466 | ** |
| 2467 | ** Or, if pLower is NULL or $L cannot be extracted from it (because it |
| 2468 | ** is not a simple variable or literal value), the lower bound of the |
| 2469 | ** range is $P. Due to a quirk in the way whereKeyStats() works, even |
| 2470 | ** if $L is available, whereKeyStats() is called for both ($P) and |
drh | 6d3f91d | 2014-11-05 19:26:12 +0000 | [diff] [blame] | 2471 | ** ($P:$L) and the larger of the two returned values is used. |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2472 | ** |
| 2473 | ** Similarly, iUpper is to be set to the estimate of the number of rows |
| 2474 | ** less than the upper bound of the range query. Where the upper bound |
| 2475 | ** is either ($P) or ($P:$U). Again, even if $U is available, both values |
| 2476 | ** of iUpper are requested of whereKeyStats() and the smaller used. |
drh | 6d3f91d | 2014-11-05 19:26:12 +0000 | [diff] [blame] | 2477 | ** |
| 2478 | ** The number of rows between the two bounds is then just iUpper-iLower. |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2479 | */ |
drh | 6d3f91d | 2014-11-05 19:26:12 +0000 | [diff] [blame] | 2480 | tRowcnt iLower; /* Rows less than the lower bound */ |
| 2481 | tRowcnt iUpper; /* Rows less than the upper bound */ |
| 2482 | int iLwrIdx = -2; /* aSample[] for the lower bound */ |
| 2483 | int iUprIdx = -1; /* aSample[] for the upper bound */ |
dan | b3c02e2 | 2013-08-08 19:38:40 +0000 | [diff] [blame] | 2484 | |
drh | b34fc5b | 2014-08-28 17:20:37 +0000 | [diff] [blame] | 2485 | if( pRec ){ |
| 2486 | testcase( pRec->nField!=pBuilder->nRecValid ); |
| 2487 | pRec->nField = pBuilder->nRecValid; |
| 2488 | } |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2489 | if( nEq==p->nKeyCol ){ |
| 2490 | aff = SQLITE_AFF_INTEGER; |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2491 | }else{ |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2492 | aff = p->pTable->aCol[p->aiColumn[nEq]].affinity; |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2493 | } |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2494 | /* Determine iLower and iUpper using ($P) only. */ |
| 2495 | if( nEq==0 ){ |
| 2496 | iLower = 0; |
drh | 9f07cf7 | 2014-10-22 15:27:05 +0000 | [diff] [blame] | 2497 | iUpper = p->nRowEst0; |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2498 | }else{ |
| 2499 | /* Note: this call could be optimized away - since the same values must |
| 2500 | ** have been requested when testing key $P in whereEqualScanEst(). */ |
| 2501 | whereKeyStats(pParse, p, pRec, 0, a); |
| 2502 | iLower = a[0]; |
| 2503 | iUpper = a[0] + a[1]; |
dan | 6cb8d76 | 2013-08-08 11:48:57 +0000 | [diff] [blame] | 2504 | } |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2505 | |
drh | 69afd99 | 2014-10-08 02:53:25 +0000 | [diff] [blame] | 2506 | assert( pLower==0 || (pLower->eOperator & (WO_GT|WO_GE))!=0 ); |
| 2507 | assert( pUpper==0 || (pUpper->eOperator & (WO_LT|WO_LE))!=0 ); |
drh | 681fca0 | 2014-10-10 15:01:46 +0000 | [diff] [blame] | 2508 | assert( p->aSortOrder!=0 ); |
| 2509 | if( p->aSortOrder[nEq] ){ |
drh | 69afd99 | 2014-10-08 02:53:25 +0000 | [diff] [blame] | 2510 | /* The roles of pLower and pUpper are swapped for a DESC index */ |
| 2511 | SWAP(WhereTerm*, pLower, pUpper); |
| 2512 | } |
| 2513 | |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2514 | /* If possible, improve on the iLower estimate using ($P:$L). */ |
| 2515 | if( pLower ){ |
| 2516 | int bOk; /* True if value is extracted from pExpr */ |
| 2517 | Expr *pExpr = pLower->pExpr->pRight; |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2518 | rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq, &bOk); |
| 2519 | if( rc==SQLITE_OK && bOk ){ |
| 2520 | tRowcnt iNew; |
drh | 6d3f91d | 2014-11-05 19:26:12 +0000 | [diff] [blame] | 2521 | iLwrIdx = whereKeyStats(pParse, p, pRec, 0, a); |
drh | 69afd99 | 2014-10-08 02:53:25 +0000 | [diff] [blame] | 2522 | iNew = a[0] + ((pLower->eOperator & (WO_GT|WO_LE)) ? a[1] : 0); |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2523 | if( iNew>iLower ) iLower = iNew; |
| 2524 | nOut--; |
dan | f741e04 | 2014-08-25 18:29:38 +0000 | [diff] [blame] | 2525 | pLower = 0; |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2526 | } |
| 2527 | } |
| 2528 | |
| 2529 | /* If possible, improve on the iUpper estimate using ($P:$U). */ |
| 2530 | if( pUpper ){ |
| 2531 | int bOk; /* True if value is extracted from pExpr */ |
| 2532 | Expr *pExpr = pUpper->pExpr->pRight; |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2533 | rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq, &bOk); |
| 2534 | if( rc==SQLITE_OK && bOk ){ |
| 2535 | tRowcnt iNew; |
drh | 6d3f91d | 2014-11-05 19:26:12 +0000 | [diff] [blame] | 2536 | iUprIdx = whereKeyStats(pParse, p, pRec, 1, a); |
drh | 69afd99 | 2014-10-08 02:53:25 +0000 | [diff] [blame] | 2537 | iNew = a[0] + ((pUpper->eOperator & (WO_GT|WO_LE)) ? a[1] : 0); |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2538 | if( iNew<iUpper ) iUpper = iNew; |
| 2539 | nOut--; |
dan | f741e04 | 2014-08-25 18:29:38 +0000 | [diff] [blame] | 2540 | pUpper = 0; |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2541 | } |
| 2542 | } |
| 2543 | |
| 2544 | pBuilder->pRec = pRec; |
| 2545 | if( rc==SQLITE_OK ){ |
| 2546 | if( iUpper>iLower ){ |
| 2547 | nNew = sqlite3LogEst(iUpper - iLower); |
drh | 6d3f91d | 2014-11-05 19:26:12 +0000 | [diff] [blame] | 2548 | /* TUNING: If both iUpper and iLower are derived from the same |
| 2549 | ** sample, then assume they are 4x more selective. This brings |
| 2550 | ** the estimated selectivity more in line with what it would be |
| 2551 | ** if estimated without the use of STAT3/4 tables. */ |
| 2552 | if( iLwrIdx==iUprIdx ) nNew -= 20; assert( 20==sqlite3LogEst(4) ); |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2553 | }else{ |
| 2554 | nNew = 10; assert( 10==sqlite3LogEst(2) ); |
| 2555 | } |
| 2556 | if( nNew<nOut ){ |
| 2557 | nOut = nNew; |
| 2558 | } |
drh | ae914d7 | 2014-08-28 19:38:22 +0000 | [diff] [blame] | 2559 | WHERETRACE(0x10, ("STAT4 range scan: %u..%u est=%d\n", |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2560 | (u32)iLower, (u32)iUpper, nOut)); |
dan | b0b8290 | 2014-06-26 20:21:46 +0000 | [diff] [blame] | 2561 | } |
| 2562 | }else{ |
| 2563 | int bDone = 0; |
| 2564 | rc = whereRangeSkipScanEst(pParse, pLower, pUpper, pLoop, &bDone); |
| 2565 | if( bDone ) return rc; |
drh | 98cdf62 | 2009-08-20 18:14:42 +0000 | [diff] [blame] | 2566 | } |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2567 | } |
drh | 3f02218 | 2009-09-09 16:10:50 +0000 | [diff] [blame] | 2568 | #else |
| 2569 | UNUSED_PARAMETER(pParse); |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2570 | UNUSED_PARAMETER(pBuilder); |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2571 | assert( pLower || pUpper ); |
dan | f741e04 | 2014-08-25 18:29:38 +0000 | [diff] [blame] | 2572 | #endif |
dan | 7de2a1f | 2014-04-28 20:11:20 +0000 | [diff] [blame] | 2573 | assert( pUpper==0 || (pUpper->wtFlags & TERM_VNULL)==0 ); |
dan | aa9933c | 2014-04-24 20:04:49 +0000 | [diff] [blame] | 2574 | nNew = whereRangeAdjust(pLower, nOut); |
| 2575 | nNew = whereRangeAdjust(pUpper, nNew); |
dan | 7de2a1f | 2014-04-28 20:11:20 +0000 | [diff] [blame] | 2576 | |
drh | 4dd96a8 | 2014-10-24 15:26:29 +0000 | [diff] [blame] | 2577 | /* TUNING: If there is both an upper and lower limit and neither limit |
| 2578 | ** has an application-defined likelihood(), assume the range is |
dan | 42685f2 | 2014-04-28 19:34:06 +0000 | [diff] [blame] | 2579 | ** reduced by an additional 75%. This means that, by default, an open-ended |
| 2580 | ** range query (e.g. col > ?) is assumed to match 1/4 of the rows in the |
| 2581 | ** index. While a closed range (e.g. col BETWEEN ? AND ?) is estimated to |
| 2582 | ** match 1/64 of the index. */ |
drh | 4dd96a8 | 2014-10-24 15:26:29 +0000 | [diff] [blame] | 2583 | if( pLower && pLower->truthProb>0 && pUpper && pUpper->truthProb>0 ){ |
| 2584 | nNew -= 20; |
| 2585 | } |
dan | 7de2a1f | 2014-04-28 20:11:20 +0000 | [diff] [blame] | 2586 | |
dan | aa9933c | 2014-04-24 20:04:49 +0000 | [diff] [blame] | 2587 | nOut -= (pLower!=0) + (pUpper!=0); |
drh | abfa6d5 | 2013-09-11 03:53:22 +0000 | [diff] [blame] | 2588 | if( nNew<10 ) nNew = 10; |
| 2589 | if( nNew<nOut ) nOut = nNew; |
drh | ae914d7 | 2014-08-28 19:38:22 +0000 | [diff] [blame] | 2590 | #if defined(WHERETRACE_ENABLED) |
| 2591 | if( pLoop->nOut>nOut ){ |
| 2592 | WHERETRACE(0x10,("Range scan lowers nOut from %d to %d\n", |
| 2593 | pLoop->nOut, nOut)); |
| 2594 | } |
| 2595 | #endif |
drh | 186ad8c | 2013-10-08 18:40:37 +0000 | [diff] [blame] | 2596 | pLoop->nOut = (LogEst)nOut; |
dan | 02fa469 | 2009-08-17 17:06:58 +0000 | [diff] [blame] | 2597 | return rc; |
| 2598 | } |
| 2599 | |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 2600 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2601 | /* |
| 2602 | ** Estimate the number of rows that will be returned based on |
| 2603 | ** an equality constraint x=VALUE and where that VALUE occurs in |
| 2604 | ** the histogram data. This only works when x is the left-most |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2605 | ** column of an index and sqlite_stat3 histogram data is available |
drh | ac8eb11 | 2011-03-17 01:58:21 +0000 | [diff] [blame] | 2606 | ** for that index. When pExpr==NULL that means the constraint is |
| 2607 | ** "x IS NULL" instead of "x=VALUE". |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2608 | ** |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2609 | ** Write the estimated row count into *pnRow and return SQLITE_OK. |
| 2610 | ** If unable to make an estimate, leave *pnRow unchanged and return |
| 2611 | ** non-zero. |
drh | 9b3eb0a | 2011-01-21 14:37:04 +0000 | [diff] [blame] | 2612 | ** |
| 2613 | ** This routine can fail if it is unable to load a collating sequence |
| 2614 | ** required for string comparison, or if unable to allocate memory |
| 2615 | ** for a UTF conversion required for comparison. The error is stored |
| 2616 | ** in the pParse structure. |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2617 | */ |
drh | 041e09f | 2011-04-07 19:56:21 +0000 | [diff] [blame] | 2618 | static int whereEqualScanEst( |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2619 | Parse *pParse, /* Parsing & code generating context */ |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2620 | WhereLoopBuilder *pBuilder, |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2621 | Expr *pExpr, /* Expression for VALUE in the x=VALUE constraint */ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 2622 | tRowcnt *pnRow /* Write the revised row estimate here */ |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2623 | ){ |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2624 | Index *p = pBuilder->pNew->u.btree.pIndex; |
| 2625 | int nEq = pBuilder->pNew->u.btree.nEq; |
| 2626 | UnpackedRecord *pRec = pBuilder->pRec; |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2627 | u8 aff; /* Column affinity */ |
| 2628 | int rc; /* Subfunction return code */ |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2629 | tRowcnt a[2]; /* Statistics */ |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2630 | int bOk; |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2631 | |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2632 | assert( nEq>=1 ); |
dan | fd984b8 | 2014-06-30 18:02:20 +0000 | [diff] [blame] | 2633 | assert( nEq<=p->nColumn ); |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2634 | assert( p->aSample!=0 ); |
drh | 5c62486 | 2011-09-22 18:46:34 +0000 | [diff] [blame] | 2635 | assert( p->nSample>0 ); |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2636 | assert( pBuilder->nRecValid<nEq ); |
| 2637 | |
| 2638 | /* If values are not available for all fields of the index to the left |
| 2639 | ** of this one, no estimate can be made. Return SQLITE_NOTFOUND. */ |
| 2640 | if( pBuilder->nRecValid<(nEq-1) ){ |
| 2641 | return SQLITE_NOTFOUND; |
drh | 1f9c766 | 2011-03-17 01:34:26 +0000 | [diff] [blame] | 2642 | } |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2643 | |
dan | dd6e1f1 | 2013-08-10 19:08:30 +0000 | [diff] [blame] | 2644 | /* This is an optimization only. The call to sqlite3Stat4ProbeSetValue() |
| 2645 | ** below would return the same value. */ |
dan | fd984b8 | 2014-06-30 18:02:20 +0000 | [diff] [blame] | 2646 | if( nEq>=p->nColumn ){ |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2647 | *pnRow = 1; |
| 2648 | return SQLITE_OK; |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2649 | } |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2650 | |
dan | eea568d | 2013-08-07 19:46:15 +0000 | [diff] [blame] | 2651 | aff = p->pTable->aCol[p->aiColumn[nEq-1]].affinity; |
dan | 87cd932 | 2013-08-07 15:52:41 +0000 | [diff] [blame] | 2652 | rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq-1, &bOk); |
| 2653 | pBuilder->pRec = pRec; |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2654 | if( rc!=SQLITE_OK ) return rc; |
| 2655 | if( bOk==0 ) return SQLITE_NOTFOUND; |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2656 | pBuilder->nRecValid = nEq; |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2657 | |
dan | b3c02e2 | 2013-08-08 19:38:40 +0000 | [diff] [blame] | 2658 | whereKeyStats(pParse, p, pRec, 0, a); |
drh | 989578e | 2013-10-28 14:34:35 +0000 | [diff] [blame] | 2659 | WHERETRACE(0x10,("equality scan regions: %d\n", (int)a[1])); |
dan | b3c02e2 | 2013-08-08 19:38:40 +0000 | [diff] [blame] | 2660 | *pnRow = a[1]; |
dan | eea568d | 2013-08-07 19:46:15 +0000 | [diff] [blame] | 2661 | |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2662 | return rc; |
| 2663 | } |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 2664 | #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2665 | |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 2666 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2667 | /* |
| 2668 | ** Estimate the number of rows that will be returned based on |
drh | 5ac0607 | 2011-01-21 18:18:13 +0000 | [diff] [blame] | 2669 | ** an IN constraint where the right-hand side of the IN operator |
| 2670 | ** is a list of values. Example: |
| 2671 | ** |
| 2672 | ** WHERE x IN (1,2,3,4) |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2673 | ** |
| 2674 | ** Write the estimated row count into *pnRow and return SQLITE_OK. |
| 2675 | ** If unable to make an estimate, leave *pnRow unchanged and return |
| 2676 | ** non-zero. |
| 2677 | ** |
| 2678 | ** This routine can fail if it is unable to load a collating sequence |
| 2679 | ** required for string comparison, or if unable to allocate memory |
| 2680 | ** for a UTF conversion required for comparison. The error is stored |
| 2681 | ** in the pParse structure. |
| 2682 | */ |
drh | 041e09f | 2011-04-07 19:56:21 +0000 | [diff] [blame] | 2683 | static int whereInScanEst( |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2684 | Parse *pParse, /* Parsing & code generating context */ |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2685 | WhereLoopBuilder *pBuilder, |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2686 | 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] | 2687 | tRowcnt *pnRow /* Write the revised row estimate here */ |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2688 | ){ |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2689 | Index *p = pBuilder->pNew->u.btree.pIndex; |
dan | cfc9df7 | 2014-04-25 15:01:01 +0000 | [diff] [blame] | 2690 | i64 nRow0 = sqlite3LogEstToInt(p->aiRowLogEst[0]); |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2691 | int nRecValid = pBuilder->nRecValid; |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 2692 | int rc = SQLITE_OK; /* Subfunction return code */ |
| 2693 | tRowcnt nEst; /* Number of rows for a single term */ |
| 2694 | tRowcnt nRowEst = 0; /* New estimate of the number of rows */ |
| 2695 | int i; /* Loop counter */ |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2696 | |
| 2697 | assert( p->aSample!=0 ); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2698 | for(i=0; rc==SQLITE_OK && i<pList->nExpr; i++){ |
dan | cfc9df7 | 2014-04-25 15:01:01 +0000 | [diff] [blame] | 2699 | nEst = nRow0; |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2700 | rc = whereEqualScanEst(pParse, pBuilder, pList->a[i].pExpr, &nEst); |
drh | faacf17 | 2011-08-12 01:51:45 +0000 | [diff] [blame] | 2701 | nRowEst += nEst; |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2702 | pBuilder->nRecValid = nRecValid; |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2703 | } |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2704 | |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2705 | if( rc==SQLITE_OK ){ |
dan | cfc9df7 | 2014-04-25 15:01:01 +0000 | [diff] [blame] | 2706 | if( nRowEst > nRow0 ) nRowEst = nRow0; |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2707 | *pnRow = nRowEst; |
drh | 5418b12 | 2014-08-28 13:42:13 +0000 | [diff] [blame] | 2708 | WHERETRACE(0x10,("IN row estimate: est=%d\n", nRowEst)); |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2709 | } |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 2710 | assert( pBuilder->nRecValid==nRecValid ); |
drh | 0c50fa0 | 2011-01-21 16:27:18 +0000 | [diff] [blame] | 2711 | return rc; |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2712 | } |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 2713 | #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ |
drh | 8275975 | 2011-01-20 16:52:09 +0000 | [diff] [blame] | 2714 | |
drh | 46c35f9 | 2012-09-26 23:17:01 +0000 | [diff] [blame] | 2715 | /* |
drh | 2ffb118 | 2004-07-19 19:14:01 +0000 | [diff] [blame] | 2716 | ** Disable a term in the WHERE clause. Except, do not disable the term |
| 2717 | ** if it controls a LEFT OUTER JOIN and it did not originate in the ON |
| 2718 | ** or USING clause of that join. |
| 2719 | ** |
| 2720 | ** Consider the term t2.z='ok' in the following queries: |
| 2721 | ** |
| 2722 | ** (1) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x WHERE t2.z='ok' |
| 2723 | ** (2) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x AND t2.z='ok' |
| 2724 | ** (3) SELECT * FROM t1, t2 WHERE t1.a=t2.x AND t2.z='ok' |
| 2725 | ** |
drh | 23bf66d | 2004-12-14 03:34:34 +0000 | [diff] [blame] | 2726 | ** The t2.z='ok' is disabled in the in (2) because it originates |
drh | 2ffb118 | 2004-07-19 19:14:01 +0000 | [diff] [blame] | 2727 | ** in the ON clause. The term is disabled in (3) because it is not part |
| 2728 | ** of a LEFT OUTER JOIN. In (1), the term is not disabled. |
| 2729 | ** |
| 2730 | ** Disabling a term causes that term to not be tested in the inner loop |
drh | b6fb62d | 2005-09-20 08:47:20 +0000 | [diff] [blame] | 2731 | ** of the join. Disabling is an optimization. When terms are satisfied |
| 2732 | ** by indices, we disable them to prevent redundant tests in the inner |
| 2733 | ** loop. We would get the correct results if nothing were ever disabled, |
| 2734 | ** but joins might run a little slower. The trick is to disable as much |
| 2735 | ** as we can without disabling too much. If we disabled in (1), we'd get |
| 2736 | ** the wrong answer. See ticket #813. |
drh | 8f1a7ed | 2015-03-06 19:47:38 +0000 | [diff] [blame] | 2737 | ** |
| 2738 | ** If all the children of a term are disabled, then that term is also |
| 2739 | ** automatically disabled. In this way, terms get disabled if derived |
| 2740 | ** virtual terms are tested first. For example: |
| 2741 | ** |
| 2742 | ** x GLOB 'abc*' AND x>='abc' AND x<'acd' |
| 2743 | ** \___________/ \______/ \_____/ |
| 2744 | ** parent child1 child2 |
| 2745 | ** |
| 2746 | ** Only the parent term was in the original WHERE clause. The child1 |
| 2747 | ** and child2 terms were added by the LIKE optimization. If both of |
| 2748 | ** the virtual child terms are valid, then testing of the parent can be |
| 2749 | ** skipped. |
drh | a9c18a9 | 2015-03-06 20:49:52 +0000 | [diff] [blame] | 2750 | ** |
| 2751 | ** Usually the parent term is marked as TERM_CODED. But if the parent |
| 2752 | ** term was originally TERM_LIKE, then the parent gets TERM_LIKECOND instead. |
| 2753 | ** The TERM_LIKECOND marking indicates that the term should be coded inside |
| 2754 | ** a conditional such that is only evaluated on the second pass of a |
| 2755 | ** LIKE-optimization loop, when scanning BLOBs instead of strings. |
drh | 2ffb118 | 2004-07-19 19:14:01 +0000 | [diff] [blame] | 2756 | */ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 2757 | static void disableTerm(WhereLevel *pLevel, WhereTerm *pTerm){ |
drh | 8f1a7ed | 2015-03-06 19:47:38 +0000 | [diff] [blame] | 2758 | int nLoop = 0; |
| 2759 | while( pTerm |
drh | be837bd | 2010-04-30 21:03:24 +0000 | [diff] [blame] | 2760 | && (pTerm->wtFlags & TERM_CODED)==0 |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 2761 | && (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_FromJoin)) |
drh | 0259bc3 | 2013-09-09 19:37:46 +0000 | [diff] [blame] | 2762 | && (pLevel->notReady & pTerm->prereqAll)==0 |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 2763 | ){ |
drh | 8f1a7ed | 2015-03-06 19:47:38 +0000 | [diff] [blame] | 2764 | if( nLoop && (pTerm->wtFlags & TERM_LIKE)!=0 ){ |
| 2765 | pTerm->wtFlags |= TERM_LIKECOND; |
| 2766 | }else{ |
| 2767 | pTerm->wtFlags |= TERM_CODED; |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 2768 | } |
drh | 8f1a7ed | 2015-03-06 19:47:38 +0000 | [diff] [blame] | 2769 | if( pTerm->iParent<0 ) break; |
| 2770 | pTerm = &pTerm->pWC->a[pTerm->iParent]; |
| 2771 | pTerm->nChild--; |
| 2772 | if( pTerm->nChild!=0 ) break; |
| 2773 | nLoop++; |
drh | 2ffb118 | 2004-07-19 19:14:01 +0000 | [diff] [blame] | 2774 | } |
| 2775 | } |
| 2776 | |
| 2777 | /* |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2778 | ** Code an OP_Affinity opcode to apply the column affinity string zAff |
| 2779 | ** to the n registers starting at base. |
| 2780 | ** |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2781 | ** As an optimization, SQLITE_AFF_NONE entries (which are no-ops) at the |
| 2782 | ** beginning and end of zAff are ignored. If all entries in zAff are |
| 2783 | ** SQLITE_AFF_NONE, then no code gets generated. |
| 2784 | ** |
| 2785 | ** This routine makes its own copy of zAff so that the caller is free |
| 2786 | ** to modify zAff after this routine returns. |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 2787 | */ |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2788 | static void codeApplyAffinity(Parse *pParse, int base, int n, char *zAff){ |
| 2789 | Vdbe *v = pParse->pVdbe; |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2790 | if( zAff==0 ){ |
| 2791 | assert( pParse->db->mallocFailed ); |
| 2792 | return; |
| 2793 | } |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2794 | assert( v!=0 ); |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2795 | |
| 2796 | /* Adjust base and n to skip over SQLITE_AFF_NONE entries at the beginning |
| 2797 | ** and end of the affinity string. |
| 2798 | */ |
| 2799 | while( n>0 && zAff[0]==SQLITE_AFF_NONE ){ |
| 2800 | n--; |
| 2801 | base++; |
| 2802 | zAff++; |
| 2803 | } |
| 2804 | while( n>1 && zAff[n-1]==SQLITE_AFF_NONE ){ |
| 2805 | n--; |
| 2806 | } |
| 2807 | |
| 2808 | /* Code the OP_Affinity opcode if there is anything left to do. */ |
| 2809 | if( n>0 ){ |
| 2810 | sqlite3VdbeAddOp2(v, OP_Affinity, base, n); |
| 2811 | sqlite3VdbeChangeP4(v, -1, zAff, n); |
| 2812 | sqlite3ExprCacheAffinityChange(pParse, base, n); |
| 2813 | } |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 2814 | } |
| 2815 | |
drh | e8b9727 | 2005-07-19 22:22:12 +0000 | [diff] [blame] | 2816 | |
| 2817 | /* |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2818 | ** Generate code for a single equality term of the WHERE clause. An equality |
| 2819 | ** term can be either X=expr or X IN (...). pTerm is the term to be |
| 2820 | ** coded. |
| 2821 | ** |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2822 | ** The current value for the constraint is left in register iReg. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2823 | ** |
| 2824 | ** For a constraint of the form X=expr, the expression is evaluated and its |
| 2825 | ** result is left on the stack. For constraints of the form X IN (...) |
| 2826 | ** this routine sets up a loop that will iterate over all values of X. |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 2827 | */ |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2828 | static int codeEqualityTerm( |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 2829 | Parse *pParse, /* The parsing context */ |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 2830 | WhereTerm *pTerm, /* The term of the WHERE clause to be coded */ |
drh | 0fe456b | 2013-03-12 18:34:50 +0000 | [diff] [blame] | 2831 | WhereLevel *pLevel, /* The level of the FROM clause we are working on */ |
| 2832 | int iEq, /* Index of the equality term within this level */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2833 | int bRev, /* True for reverse-order IN operations */ |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2834 | int iTarget /* Attempt to leave results in this register */ |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 2835 | ){ |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 2836 | Expr *pX = pTerm->pExpr; |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 2837 | Vdbe *v = pParse->pVdbe; |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2838 | int iReg; /* Register holding results */ |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2839 | |
danielk1977 | 2d60549 | 2008-10-01 08:43:03 +0000 | [diff] [blame] | 2840 | assert( iTarget>0 ); |
drh | fcd4953 | 2015-05-13 15:24:07 +0000 | [diff] [blame] | 2841 | if( pX->op==TK_EQ || pX->op==TK_IS ){ |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2842 | iReg = sqlite3ExprCodeTarget(pParse, pX->pRight, iTarget); |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 2843 | }else if( pX->op==TK_ISNULL ){ |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2844 | iReg = iTarget; |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2845 | sqlite3VdbeAddOp2(v, OP_Null, 0, iReg); |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 2846 | #ifndef SQLITE_OMIT_SUBQUERY |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 2847 | }else{ |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 2848 | int eType; |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 2849 | int iTab; |
drh | 72e8fa4 | 2007-03-28 14:30:06 +0000 | [diff] [blame] | 2850 | struct InLoop *pIn; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2851 | WhereLoop *pLoop = pLevel->pWLoop; |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 2852 | |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2853 | if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 |
| 2854 | && pLoop->u.btree.pIndex!=0 |
| 2855 | && pLoop->u.btree.pIndex->aSortOrder[iEq] |
drh | d383216 | 2013-03-12 18:49:25 +0000 | [diff] [blame] | 2856 | ){ |
drh | 725e1ae | 2013-03-12 23:58:42 +0000 | [diff] [blame] | 2857 | testcase( iEq==0 ); |
drh | 725e1ae | 2013-03-12 23:58:42 +0000 | [diff] [blame] | 2858 | testcase( bRev ); |
drh | 1ccce44 | 2013-03-12 20:38:51 +0000 | [diff] [blame] | 2859 | bRev = !bRev; |
drh | 0fe456b | 2013-03-12 18:34:50 +0000 | [diff] [blame] | 2860 | } |
drh | 50b3996 | 2006-10-28 00:28:09 +0000 | [diff] [blame] | 2861 | assert( pX->op==TK_IN ); |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2862 | iReg = iTarget; |
drh | 3a85625 | 2014-08-01 14:46:57 +0000 | [diff] [blame] | 2863 | eType = sqlite3FindInIndex(pParse, pX, IN_INDEX_LOOP, 0); |
drh | 725e1ae | 2013-03-12 23:58:42 +0000 | [diff] [blame] | 2864 | if( eType==IN_INDEX_INDEX_DESC ){ |
| 2865 | testcase( bRev ); |
| 2866 | bRev = !bRev; |
| 2867 | } |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 2868 | iTab = pX->iTable; |
drh | 7d17610 | 2014-02-18 03:07:12 +0000 | [diff] [blame] | 2869 | sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iTab, 0); |
| 2870 | VdbeCoverageIf(v, bRev); |
| 2871 | VdbeCoverageIf(v, !bRev); |
drh | 6fa978d | 2013-05-30 19:29:19 +0000 | [diff] [blame] | 2872 | assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 ); |
| 2873 | pLoop->wsFlags |= WHERE_IN_ABLE; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2874 | if( pLevel->u.in.nIn==0 ){ |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 2875 | pLevel->addrNxt = sqlite3VdbeMakeLabel(v); |
drh | 72e8fa4 | 2007-03-28 14:30:06 +0000 | [diff] [blame] | 2876 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2877 | pLevel->u.in.nIn++; |
| 2878 | pLevel->u.in.aInLoop = |
| 2879 | sqlite3DbReallocOrFree(pParse->db, pLevel->u.in.aInLoop, |
| 2880 | sizeof(pLevel->u.in.aInLoop[0])*pLevel->u.in.nIn); |
| 2881 | pIn = pLevel->u.in.aInLoop; |
drh | 72e8fa4 | 2007-03-28 14:30:06 +0000 | [diff] [blame] | 2882 | if( pIn ){ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2883 | pIn += pLevel->u.in.nIn - 1; |
drh | 72e8fa4 | 2007-03-28 14:30:06 +0000 | [diff] [blame] | 2884 | pIn->iCur = iTab; |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2885 | if( eType==IN_INDEX_ROWID ){ |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 2886 | pIn->addrInTop = sqlite3VdbeAddOp2(v, OP_Rowid, iTab, iReg); |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2887 | }else{ |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 2888 | pIn->addrInTop = sqlite3VdbeAddOp3(v, OP_Column, iTab, 0, iReg); |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2889 | } |
drh | f93cd94 | 2013-11-21 03:12:25 +0000 | [diff] [blame] | 2890 | pIn->eEndLoopOp = bRev ? OP_PrevIfOpen : OP_NextIfOpen; |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 2891 | sqlite3VdbeAddOp1(v, OP_IsNull, iReg); VdbeCoverage(v); |
drh | a611040 | 2005-07-28 20:51:19 +0000 | [diff] [blame] | 2892 | }else{ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2893 | pLevel->u.in.nIn = 0; |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 2894 | } |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 2895 | #endif |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 2896 | } |
drh | 0fcef5e | 2005-07-19 17:38:22 +0000 | [diff] [blame] | 2897 | disableTerm(pLevel, pTerm); |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2898 | return iReg; |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 2899 | } |
| 2900 | |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2901 | /* |
| 2902 | ** Generate code that will evaluate all == and IN constraints for an |
drh | cd8629e | 2013-11-13 12:27:25 +0000 | [diff] [blame] | 2903 | ** index scan. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2904 | ** |
| 2905 | ** For example, consider table t1(a,b,c,d,e,f) with index i1(a,b,c). |
| 2906 | ** Suppose the WHERE clause is this: a==5 AND b IN (1,2,3) AND c>5 AND c<10 |
| 2907 | ** The index has as many as three equality constraints, but in this |
| 2908 | ** example, the third "c" value is an inequality. So only two |
| 2909 | ** constraints are coded. This routine will generate code to evaluate |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 2910 | ** a==5 and b IN (1,2,3). The current values for a and b will be stored |
| 2911 | ** in consecutive registers and the index of the first register is returned. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2912 | ** |
| 2913 | ** In the example above nEq==2. But this subroutine works for any value |
| 2914 | ** of nEq including 0. If nEq==0, this routine is nearly a no-op. |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2915 | ** The only thing it does is allocate the pLevel->iMem memory cell and |
| 2916 | ** compute the affinity string. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2917 | ** |
drh | cd8629e | 2013-11-13 12:27:25 +0000 | [diff] [blame] | 2918 | ** The nExtraReg parameter is 0 or 1. It is 0 if all WHERE clause constraints |
| 2919 | ** are == or IN and are covered by the nEq. nExtraReg is 1 if there is |
| 2920 | ** an inequality constraint (such as the "c>=5 AND c<10" in the example) that |
| 2921 | ** occurs after the nEq quality constraints. |
| 2922 | ** |
| 2923 | ** This routine allocates a range of nEq+nExtraReg memory cells and returns |
| 2924 | ** the index of the first memory cell in that range. The code that |
| 2925 | ** calls this routine will use that memory range to store keys for |
| 2926 | ** start and termination conditions of the loop. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2927 | ** key value of the loop. If one or more IN operators appear, then |
| 2928 | ** this routine allocates an additional nEq memory cells for internal |
| 2929 | ** use. |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2930 | ** |
| 2931 | ** Before returning, *pzAff is set to point to a buffer containing a |
| 2932 | ** copy of the column affinity string of the index allocated using |
| 2933 | ** sqlite3DbMalloc(). Except, entries in the copy of the string associated |
| 2934 | ** with equality constraints that use NONE affinity are set to |
| 2935 | ** SQLITE_AFF_NONE. This is to deal with SQL such as the following: |
| 2936 | ** |
| 2937 | ** CREATE TABLE t1(a TEXT PRIMARY KEY, b); |
| 2938 | ** SELECT ... FROM t1 AS t2, t1 WHERE t1.a = t2.b; |
| 2939 | ** |
| 2940 | ** In the example above, the index on t1(a) has TEXT affinity. But since |
| 2941 | ** the right hand side of the equality constraint (t2.b) has NONE affinity, |
| 2942 | ** no conversion should be attempted before using a t2.b value as part of |
| 2943 | ** a key to search the index. Hence the first byte in the returned affinity |
| 2944 | ** string in this example would be set to SQLITE_AFF_NONE. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2945 | */ |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2946 | static int codeAllEqualityTerms( |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2947 | Parse *pParse, /* Parsing context */ |
| 2948 | WhereLevel *pLevel, /* Which nested loop of the FROM we are coding */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2949 | int bRev, /* Reverse the order of IN operators */ |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2950 | int nExtraReg, /* Number of extra registers to allocate */ |
| 2951 | char **pzAff /* OUT: Set to point to affinity string */ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2952 | ){ |
drh | cd8629e | 2013-11-13 12:27:25 +0000 | [diff] [blame] | 2953 | u16 nEq; /* The number of == or IN constraints to code */ |
| 2954 | u16 nSkip; /* Number of left-most columns to skip */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2955 | Vdbe *v = pParse->pVdbe; /* The vm under construction */ |
| 2956 | Index *pIdx; /* The index being used for this loop */ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2957 | WhereTerm *pTerm; /* A single constraint term */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2958 | WhereLoop *pLoop; /* The WhereLoop object */ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2959 | int j; /* Loop counter */ |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 2960 | int regBase; /* Base register */ |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 2961 | int nReg; /* Number of registers to allocate */ |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2962 | char *zAff; /* Affinity string to return */ |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2963 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2964 | /* This module is only called on query plans that use an index. */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2965 | pLoop = pLevel->pWLoop; |
| 2966 | assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 ); |
| 2967 | nEq = pLoop->u.btree.nEq; |
drh | c8bbce1 | 2014-10-21 01:05:09 +0000 | [diff] [blame] | 2968 | nSkip = pLoop->nSkip; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2969 | pIdx = pLoop->u.btree.pIndex; |
| 2970 | assert( pIdx!=0 ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 2971 | |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2972 | /* Figure out how many memory cells we will need then allocate them. |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2973 | */ |
drh | 700a226 | 2008-12-17 19:22:15 +0000 | [diff] [blame] | 2974 | regBase = pParse->nMem + 1; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 2975 | nReg = pLoop->u.btree.nEq + nExtraReg; |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 2976 | pParse->nMem += nReg; |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 2977 | |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 2978 | zAff = sqlite3DbStrDup(pParse->db, sqlite3IndexAffinityStr(v, pIdx)); |
| 2979 | if( !zAff ){ |
| 2980 | pParse->db->mallocFailed = 1; |
| 2981 | } |
| 2982 | |
drh | cd8629e | 2013-11-13 12:27:25 +0000 | [diff] [blame] | 2983 | if( nSkip ){ |
| 2984 | int iIdxCur = pLevel->iIdxCur; |
drh | 7d17610 | 2014-02-18 03:07:12 +0000 | [diff] [blame] | 2985 | sqlite3VdbeAddOp1(v, (bRev?OP_Last:OP_Rewind), iIdxCur); |
| 2986 | VdbeCoverageIf(v, bRev==0); |
| 2987 | VdbeCoverageIf(v, bRev!=0); |
drh | e084f40 | 2013-11-13 17:24:38 +0000 | [diff] [blame] | 2988 | VdbeComment((v, "begin skip-scan on %s", pIdx->zName)); |
drh | 2e5ef4e | 2013-11-13 16:58:54 +0000 | [diff] [blame] | 2989 | j = sqlite3VdbeAddOp0(v, OP_Goto); |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 2990 | pLevel->addrSkip = sqlite3VdbeAddOp4Int(v, (bRev?OP_SeekLT:OP_SeekGT), |
drh | 7d17610 | 2014-02-18 03:07:12 +0000 | [diff] [blame] | 2991 | iIdxCur, 0, regBase, nSkip); |
| 2992 | VdbeCoverageIf(v, bRev==0); |
| 2993 | VdbeCoverageIf(v, bRev!=0); |
drh | 2e5ef4e | 2013-11-13 16:58:54 +0000 | [diff] [blame] | 2994 | sqlite3VdbeJumpHere(v, j); |
drh | cd8629e | 2013-11-13 12:27:25 +0000 | [diff] [blame] | 2995 | for(j=0; j<nSkip; j++){ |
| 2996 | sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, j, regBase+j); |
| 2997 | assert( pIdx->aiColumn[j]>=0 ); |
| 2998 | VdbeComment((v, "%s", pIdx->pTable->aCol[pIdx->aiColumn[j]].zName)); |
| 2999 | } |
| 3000 | } |
| 3001 | |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 3002 | /* Evaluate the equality constraints |
| 3003 | */ |
mistachkin | f641889 | 2013-08-28 01:54:12 +0000 | [diff] [blame] | 3004 | assert( zAff==0 || (int)strlen(zAff)>=nEq ); |
drh | cd8629e | 2013-11-13 12:27:25 +0000 | [diff] [blame] | 3005 | for(j=nSkip; j<nEq; j++){ |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 3006 | int r1; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3007 | pTerm = pLoop->aLTerm[j]; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3008 | assert( pTerm!=0 ); |
drh | cd8629e | 2013-11-13 12:27:25 +0000 | [diff] [blame] | 3009 | /* The following testcase is true for indices with redundant columns. |
drh | be837bd | 2010-04-30 21:03:24 +0000 | [diff] [blame] | 3010 | ** Ex: CREATE INDEX i1 ON t1(a,b,a); SELECT * FROM t1 WHERE a=0 AND b=0; */ |
| 3011 | testcase( (pTerm->wtFlags & TERM_CODED)!=0 ); |
drh | 3975974 | 2013-08-02 23:40:45 +0000 | [diff] [blame] | 3012 | testcase( pTerm->wtFlags & TERM_VIRTUAL ); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3013 | r1 = codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, regBase+j); |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 3014 | if( r1!=regBase+j ){ |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 3015 | if( nReg==1 ){ |
| 3016 | sqlite3ReleaseTempReg(pParse, regBase); |
| 3017 | regBase = r1; |
| 3018 | }else{ |
| 3019 | sqlite3VdbeAddOp2(v, OP_SCopy, r1, regBase+j); |
| 3020 | } |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 3021 | } |
drh | 981642f | 2008-04-19 14:40:43 +0000 | [diff] [blame] | 3022 | testcase( pTerm->eOperator & WO_ISNULL ); |
| 3023 | testcase( pTerm->eOperator & WO_IN ); |
drh | 72e8fa4 | 2007-03-28 14:30:06 +0000 | [diff] [blame] | 3024 | if( (pTerm->eOperator & (WO_ISNULL|WO_IN))==0 ){ |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 3025 | Expr *pRight = pTerm->pExpr->pRight; |
drh | 9be1870 | 2015-05-13 19:33:41 +0000 | [diff] [blame] | 3026 | if( (pTerm->wtFlags & TERM_IS)==0 && sqlite3ExprCanBeNull(pRight) ){ |
drh | 7d17610 | 2014-02-18 03:07:12 +0000 | [diff] [blame] | 3027 | sqlite3VdbeAddOp2(v, OP_IsNull, regBase+j, pLevel->addrBrk); |
| 3028 | VdbeCoverage(v); |
| 3029 | } |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 3030 | if( zAff ){ |
| 3031 | if( sqlite3CompareAffinity(pRight, zAff[j])==SQLITE_AFF_NONE ){ |
| 3032 | zAff[j] = SQLITE_AFF_NONE; |
| 3033 | } |
| 3034 | if( sqlite3ExprNeedsNoAffinityChange(pRight, zAff[j]) ){ |
| 3035 | zAff[j] = SQLITE_AFF_NONE; |
| 3036 | } |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 3037 | } |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 3038 | } |
| 3039 | } |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 3040 | *pzAff = zAff; |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 3041 | return regBase; |
drh | 51147ba | 2005-07-23 22:59:55 +0000 | [diff] [blame] | 3042 | } |
| 3043 | |
dan | 6f9702e | 2014-11-01 20:38:06 +0000 | [diff] [blame] | 3044 | #ifndef SQLITE_OMIT_EXPLAIN |
dan | 17c0bc0 | 2010-11-09 17:35:19 +0000 | [diff] [blame] | 3045 | /* |
drh | 69174c4 | 2010-11-12 15:35:59 +0000 | [diff] [blame] | 3046 | ** This routine is a helper for explainIndexRange() below |
| 3047 | ** |
| 3048 | ** pStr holds the text of an expression that we are building up one term |
| 3049 | ** at a time. This routine adds a new term to the end of the expression. |
| 3050 | ** Terms are separated by AND so add the "AND" text for second and subsequent |
| 3051 | ** terms only. |
| 3052 | */ |
| 3053 | static void explainAppendTerm( |
| 3054 | StrAccum *pStr, /* The text expression being built */ |
| 3055 | int iTerm, /* Index of this term. First is zero */ |
| 3056 | const char *zColumn, /* Name of the column */ |
| 3057 | const char *zOp /* Name of the operator */ |
| 3058 | ){ |
| 3059 | if( iTerm ) sqlite3StrAccumAppend(pStr, " AND ", 5); |
drh | a6353a3 | 2013-12-09 19:03:26 +0000 | [diff] [blame] | 3060 | sqlite3StrAccumAppendAll(pStr, zColumn); |
drh | 69174c4 | 2010-11-12 15:35:59 +0000 | [diff] [blame] | 3061 | sqlite3StrAccumAppend(pStr, zOp, 1); |
| 3062 | sqlite3StrAccumAppend(pStr, "?", 1); |
| 3063 | } |
| 3064 | |
| 3065 | /* |
dan | 17c0bc0 | 2010-11-09 17:35:19 +0000 | [diff] [blame] | 3066 | ** Argument pLevel describes a strategy for scanning table pTab. This |
drh | 6c97789 | 2014-10-10 15:47:46 +0000 | [diff] [blame] | 3067 | ** function appends text to pStr that describes the subset of table |
| 3068 | ** rows scanned by the strategy in the form of an SQL expression. |
dan | 17c0bc0 | 2010-11-09 17:35:19 +0000 | [diff] [blame] | 3069 | ** |
| 3070 | ** For example, if the query: |
| 3071 | ** |
| 3072 | ** SELECT * FROM t1 WHERE a=1 AND b>2; |
| 3073 | ** |
| 3074 | ** is run and there is an index on (a, b), then this function returns a |
| 3075 | ** string similar to: |
| 3076 | ** |
| 3077 | ** "a=? AND b>?" |
dan | 17c0bc0 | 2010-11-09 17:35:19 +0000 | [diff] [blame] | 3078 | */ |
drh | 1f8817c | 2014-10-10 19:15:35 +0000 | [diff] [blame] | 3079 | static void explainIndexRange(StrAccum *pStr, WhereLoop *pLoop, Table *pTab){ |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 3080 | Index *pIndex = pLoop->u.btree.pIndex; |
drh | cd8629e | 2013-11-13 12:27:25 +0000 | [diff] [blame] | 3081 | u16 nEq = pLoop->u.btree.nEq; |
drh | c8bbce1 | 2014-10-21 01:05:09 +0000 | [diff] [blame] | 3082 | u16 nSkip = pLoop->nSkip; |
drh | 69174c4 | 2010-11-12 15:35:59 +0000 | [diff] [blame] | 3083 | int i, j; |
| 3084 | Column *aCol = pTab->aCol; |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 3085 | i16 *aiColumn = pIndex->aiColumn; |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3086 | |
drh | 6c97789 | 2014-10-10 15:47:46 +0000 | [diff] [blame] | 3087 | if( nEq==0 && (pLoop->wsFlags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ) return; |
| 3088 | sqlite3StrAccumAppend(pStr, " (", 2); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3089 | for(i=0; i<nEq; i++){ |
dan | 39129ce | 2014-06-30 15:23:57 +0000 | [diff] [blame] | 3090 | char *z = aiColumn[i] < 0 ? "rowid" : aCol[aiColumn[i]].zName; |
drh | cd8629e | 2013-11-13 12:27:25 +0000 | [diff] [blame] | 3091 | if( i>=nSkip ){ |
drh | 6c97789 | 2014-10-10 15:47:46 +0000 | [diff] [blame] | 3092 | explainAppendTerm(pStr, i, z, "="); |
drh | cd8629e | 2013-11-13 12:27:25 +0000 | [diff] [blame] | 3093 | }else{ |
drh | 6c97789 | 2014-10-10 15:47:46 +0000 | [diff] [blame] | 3094 | if( i ) sqlite3StrAccumAppend(pStr, " AND ", 5); |
| 3095 | sqlite3XPrintf(pStr, 0, "ANY(%s)", z); |
drh | cd8629e | 2013-11-13 12:27:25 +0000 | [diff] [blame] | 3096 | } |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3097 | } |
| 3098 | |
drh | 69174c4 | 2010-11-12 15:35:59 +0000 | [diff] [blame] | 3099 | j = i; |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 3100 | if( pLoop->wsFlags&WHERE_BTM_LIMIT ){ |
dan | 39129ce | 2014-06-30 15:23:57 +0000 | [diff] [blame] | 3101 | char *z = aiColumn[j] < 0 ? "rowid" : aCol[aiColumn[j]].zName; |
drh | 6c97789 | 2014-10-10 15:47:46 +0000 | [diff] [blame] | 3102 | explainAppendTerm(pStr, i++, z, ">"); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3103 | } |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 3104 | if( pLoop->wsFlags&WHERE_TOP_LIMIT ){ |
dan | 39129ce | 2014-06-30 15:23:57 +0000 | [diff] [blame] | 3105 | char *z = aiColumn[j] < 0 ? "rowid" : aCol[aiColumn[j]].zName; |
drh | 6c97789 | 2014-10-10 15:47:46 +0000 | [diff] [blame] | 3106 | explainAppendTerm(pStr, i, z, "<"); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3107 | } |
drh | 6c97789 | 2014-10-10 15:47:46 +0000 | [diff] [blame] | 3108 | sqlite3StrAccumAppend(pStr, ")", 1); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3109 | } |
| 3110 | |
dan | 17c0bc0 | 2010-11-09 17:35:19 +0000 | [diff] [blame] | 3111 | /* |
| 3112 | ** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN |
dan | 037b532 | 2014-11-03 11:25:32 +0000 | [diff] [blame] | 3113 | ** command, or if either SQLITE_DEBUG or SQLITE_ENABLE_STMT_SCANSTATUS was |
| 3114 | ** defined at compile-time. If it is not a no-op, a single OP_Explain opcode |
| 3115 | ** is added to the output to describe the table scan strategy in pLevel. |
| 3116 | ** |
| 3117 | ** If an OP_Explain opcode is added to the VM, its address is returned. |
| 3118 | ** Otherwise, if no OP_Explain is coded, zero is returned. |
dan | 17c0bc0 | 2010-11-09 17:35:19 +0000 | [diff] [blame] | 3119 | */ |
dan | 6f9702e | 2014-11-01 20:38:06 +0000 | [diff] [blame] | 3120 | static int explainOneScan( |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3121 | Parse *pParse, /* Parse context */ |
| 3122 | SrcList *pTabList, /* Table list this loop refers to */ |
dan | 6f9702e | 2014-11-01 20:38:06 +0000 | [diff] [blame] | 3123 | WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */ |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3124 | int iLevel, /* Value for "level" column of output */ |
dan | 6f9702e | 2014-11-01 20:38:06 +0000 | [diff] [blame] | 3125 | int iFrom, /* Value for "from" column of output */ |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 3126 | u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */ |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3127 | ){ |
dan | 6f9702e | 2014-11-01 20:38:06 +0000 | [diff] [blame] | 3128 | int ret = 0; |
dan | 43764a8 | 2014-11-01 21:00:04 +0000 | [diff] [blame] | 3129 | #if !defined(SQLITE_DEBUG) && !defined(SQLITE_ENABLE_STMT_SCANSTATUS) |
drh | 84e55a8 | 2013-11-13 17:58:23 +0000 | [diff] [blame] | 3130 | if( pParse->explain==2 ) |
| 3131 | #endif |
| 3132 | { |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3133 | struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom]; |
dan | 17c0bc0 | 2010-11-09 17:35:19 +0000 | [diff] [blame] | 3134 | Vdbe *v = pParse->pVdbe; /* VM being constructed */ |
| 3135 | sqlite3 *db = pParse->db; /* Database handle */ |
dan | 6f9702e | 2014-11-01 20:38:06 +0000 | [diff] [blame] | 3136 | int iId = pParse->iSelectId; /* Select id (left-most output column) */ |
dan | 4bc39fa | 2010-11-13 16:42:27 +0000 | [diff] [blame] | 3137 | int isSearch; /* True for a SEARCH. False for SCAN. */ |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 3138 | WhereLoop *pLoop; /* The controlling WhereLoop object */ |
| 3139 | u32 flags; /* Flags that describe this loop */ |
dan | 6f9702e | 2014-11-01 20:38:06 +0000 | [diff] [blame] | 3140 | char *zMsg; /* Text to add to EQP output */ |
drh | 6c97789 | 2014-10-10 15:47:46 +0000 | [diff] [blame] | 3141 | StrAccum str; /* EQP output string */ |
| 3142 | char zBuf[100]; /* Initial space for EQP output string */ |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3143 | |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 3144 | pLoop = pLevel->pWLoop; |
| 3145 | flags = pLoop->wsFlags; |
dan | 6f9702e | 2014-11-01 20:38:06 +0000 | [diff] [blame] | 3146 | if( (flags&WHERE_MULTI_OR) || (wctrlFlags&WHERE_ONETABLE_ONLY) ) return 0; |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3147 | |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 3148 | isSearch = (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0 |
| 3149 | || ((flags&WHERE_VIRTUALTABLE)==0 && (pLoop->u.btree.nEq>0)) |
| 3150 | || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX)); |
dan | 6f9702e | 2014-11-01 20:38:06 +0000 | [diff] [blame] | 3151 | |
drh | c049057 | 2015-05-02 11:45:53 +0000 | [diff] [blame] | 3152 | sqlite3StrAccumInit(&str, db, zBuf, sizeof(zBuf), SQLITE_MAX_LENGTH); |
drh | 6c97789 | 2014-10-10 15:47:46 +0000 | [diff] [blame] | 3153 | sqlite3StrAccumAppendAll(&str, isSearch ? "SEARCH" : "SCAN"); |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 3154 | if( pItem->pSelect ){ |
drh | 6c97789 | 2014-10-10 15:47:46 +0000 | [diff] [blame] | 3155 | sqlite3XPrintf(&str, 0, " SUBQUERY %d", pItem->iSelectId); |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 3156 | }else{ |
drh | 6c97789 | 2014-10-10 15:47:46 +0000 | [diff] [blame] | 3157 | sqlite3XPrintf(&str, 0, " TABLE %s", pItem->zName); |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 3158 | } |
| 3159 | |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3160 | if( pItem->zAlias ){ |
drh | 6c97789 | 2014-10-10 15:47:46 +0000 | [diff] [blame] | 3161 | sqlite3XPrintf(&str, 0, " AS %s", pItem->zAlias); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3162 | } |
drh | 6c97789 | 2014-10-10 15:47:46 +0000 | [diff] [blame] | 3163 | if( (flags & (WHERE_IPK|WHERE_VIRTUALTABLE))==0 ){ |
| 3164 | const char *zFmt = 0; |
| 3165 | Index *pIdx; |
| 3166 | |
| 3167 | assert( pLoop->u.btree.pIndex!=0 ); |
| 3168 | pIdx = pLoop->u.btree.pIndex; |
dan | e96f2df | 2014-05-23 17:17:06 +0000 | [diff] [blame] | 3169 | assert( !(flags&WHERE_AUTO_INDEX) || (flags&WHERE_IDX_ONLY) ); |
drh | 48dd1d8 | 2014-05-27 18:18:58 +0000 | [diff] [blame] | 3170 | if( !HasRowid(pItem->pTab) && IsPrimaryKeyIndex(pIdx) ){ |
drh | c631faa | 2014-10-11 01:22:16 +0000 | [diff] [blame] | 3171 | if( isSearch ){ |
drh | 6c97789 | 2014-10-10 15:47:46 +0000 | [diff] [blame] | 3172 | zFmt = "PRIMARY KEY"; |
| 3173 | } |
drh | 051575c | 2014-10-25 12:28:25 +0000 | [diff] [blame] | 3174 | }else if( flags & WHERE_PARTIALIDX ){ |
| 3175 | zFmt = "AUTOMATIC PARTIAL COVERING INDEX"; |
dan | e96f2df | 2014-05-23 17:17:06 +0000 | [diff] [blame] | 3176 | }else if( flags & WHERE_AUTO_INDEX ){ |
drh | 6c97789 | 2014-10-10 15:47:46 +0000 | [diff] [blame] | 3177 | zFmt = "AUTOMATIC COVERING INDEX"; |
dan | e96f2df | 2014-05-23 17:17:06 +0000 | [diff] [blame] | 3178 | }else if( flags & WHERE_IDX_ONLY ){ |
drh | 6c97789 | 2014-10-10 15:47:46 +0000 | [diff] [blame] | 3179 | zFmt = "COVERING INDEX %s"; |
dan | e96f2df | 2014-05-23 17:17:06 +0000 | [diff] [blame] | 3180 | }else{ |
drh | 6c97789 | 2014-10-10 15:47:46 +0000 | [diff] [blame] | 3181 | zFmt = "INDEX %s"; |
dan | e96f2df | 2014-05-23 17:17:06 +0000 | [diff] [blame] | 3182 | } |
drh | 6c97789 | 2014-10-10 15:47:46 +0000 | [diff] [blame] | 3183 | if( zFmt ){ |
| 3184 | sqlite3StrAccumAppend(&str, " USING ", 7); |
| 3185 | sqlite3XPrintf(&str, 0, zFmt, pIdx->zName); |
| 3186 | explainIndexRange(&str, pLoop, pItem->pTab); |
| 3187 | } |
drh | ef71c1f | 2013-06-04 12:58:02 +0000 | [diff] [blame] | 3188 | }else if( (flags & WHERE_IPK)!=0 && (flags & WHERE_CONSTRAINT)!=0 ){ |
drh | 6c97789 | 2014-10-10 15:47:46 +0000 | [diff] [blame] | 3189 | const char *zRange; |
drh | 8e23daf | 2013-06-11 13:30:04 +0000 | [diff] [blame] | 3190 | if( flags&(WHERE_COLUMN_EQ|WHERE_COLUMN_IN) ){ |
drh | 6c97789 | 2014-10-10 15:47:46 +0000 | [diff] [blame] | 3191 | zRange = "(rowid=?)"; |
drh | 04098e6 | 2010-11-15 21:50:19 +0000 | [diff] [blame] | 3192 | }else if( (flags&WHERE_BOTH_LIMIT)==WHERE_BOTH_LIMIT ){ |
drh | 6c97789 | 2014-10-10 15:47:46 +0000 | [diff] [blame] | 3193 | zRange = "(rowid>? AND rowid<?)"; |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3194 | }else if( flags&WHERE_BTM_LIMIT ){ |
drh | 6c97789 | 2014-10-10 15:47:46 +0000 | [diff] [blame] | 3195 | zRange = "(rowid>?)"; |
| 3196 | }else{ |
| 3197 | assert( flags&WHERE_TOP_LIMIT); |
| 3198 | zRange = "(rowid<?)"; |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3199 | } |
drh | 6c97789 | 2014-10-10 15:47:46 +0000 | [diff] [blame] | 3200 | sqlite3StrAccumAppendAll(&str, " USING INTEGER PRIMARY KEY "); |
| 3201 | sqlite3StrAccumAppendAll(&str, zRange); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3202 | } |
| 3203 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 3204 | else if( (flags & WHERE_VIRTUALTABLE)!=0 ){ |
drh | 6c97789 | 2014-10-10 15:47:46 +0000 | [diff] [blame] | 3205 | sqlite3XPrintf(&str, 0, " VIRTUAL TABLE INDEX %d:%s", |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 3206 | pLoop->u.vtab.idxNum, pLoop->u.vtab.idxStr); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3207 | } |
| 3208 | #endif |
drh | 98545bb | 2014-10-10 17:20:39 +0000 | [diff] [blame] | 3209 | #ifdef SQLITE_EXPLAIN_ESTIMATED_ROWS |
dan | 6f9702e | 2014-11-01 20:38:06 +0000 | [diff] [blame] | 3210 | if( pLoop->nOut>=10 ){ |
| 3211 | sqlite3XPrintf(&str, 0, " (~%llu rows)", sqlite3LogEstToInt(pLoop->nOut)); |
| 3212 | }else{ |
| 3213 | sqlite3StrAccumAppend(&str, " (~1 row)", 9); |
dan | 04489b6 | 2014-10-31 20:11:32 +0000 | [diff] [blame] | 3214 | } |
dan | 6f9702e | 2014-11-01 20:38:06 +0000 | [diff] [blame] | 3215 | #endif |
| 3216 | zMsg = sqlite3StrAccumFinish(&str); |
| 3217 | ret = sqlite3VdbeAddOp4(v, OP_Explain, iId, iLevel, iFrom, zMsg,P4_DYNAMIC); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3218 | } |
dan | 6f9702e | 2014-11-01 20:38:06 +0000 | [diff] [blame] | 3219 | return ret; |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3220 | } |
| 3221 | #else |
dan | 6f9702e | 2014-11-01 20:38:06 +0000 | [diff] [blame] | 3222 | # define explainOneScan(u,v,w,x,y,z) 0 |
| 3223 | #endif /* SQLITE_OMIT_EXPLAIN */ |
| 3224 | |
| 3225 | #ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
dan | 037b532 | 2014-11-03 11:25:32 +0000 | [diff] [blame] | 3226 | /* |
| 3227 | ** Configure the VM passed as the first argument with an |
| 3228 | ** sqlite3_stmt_scanstatus() entry corresponding to the scan used to |
| 3229 | ** implement level pLvl. Argument pSrclist is a pointer to the FROM |
| 3230 | ** clause that the scan reads data from. |
| 3231 | ** |
| 3232 | ** If argument addrExplain is not 0, it must be the address of an |
| 3233 | ** OP_Explain instruction that describes the same loop. |
| 3234 | */ |
dan | 6f9702e | 2014-11-01 20:38:06 +0000 | [diff] [blame] | 3235 | static void addScanStatus( |
dan | 037b532 | 2014-11-03 11:25:32 +0000 | [diff] [blame] | 3236 | Vdbe *v, /* Vdbe to add scanstatus entry to */ |
| 3237 | SrcList *pSrclist, /* FROM clause pLvl reads data from */ |
| 3238 | WhereLevel *pLvl, /* Level to add scanstatus() entry for */ |
| 3239 | int addrExplain /* Address of OP_Explain (or 0) */ |
dan | 6f9702e | 2014-11-01 20:38:06 +0000 | [diff] [blame] | 3240 | ){ |
| 3241 | const char *zObj = 0; |
dan | 6f9702e | 2014-11-01 20:38:06 +0000 | [diff] [blame] | 3242 | WhereLoop *pLoop = pLvl->pWLoop; |
drh | cd934c3 | 2014-12-05 21:18:19 +0000 | [diff] [blame] | 3243 | if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 && pLoop->u.btree.pIndex!=0 ){ |
dan | 6f9702e | 2014-11-01 20:38:06 +0000 | [diff] [blame] | 3244 | zObj = pLoop->u.btree.pIndex->zName; |
| 3245 | }else{ |
| 3246 | zObj = pSrclist->a[pLvl->iFrom].zName; |
| 3247 | } |
dan | 037b532 | 2014-11-03 11:25:32 +0000 | [diff] [blame] | 3248 | sqlite3VdbeScanStatus( |
drh | 518140e | 2014-11-06 03:55:10 +0000 | [diff] [blame] | 3249 | v, addrExplain, pLvl->addrBody, pLvl->addrVisit, pLoop->nOut, zObj |
dan | 6f9702e | 2014-11-01 20:38:06 +0000 | [diff] [blame] | 3250 | ); |
| 3251 | } |
| 3252 | #else |
dan | e2f771b | 2014-11-03 15:33:17 +0000 | [diff] [blame] | 3253 | # define addScanStatus(a, b, c, d) ((void)d) |
dan | 6f9702e | 2014-11-01 20:38:06 +0000 | [diff] [blame] | 3254 | #endif |
| 3255 | |
drh | f07cf6e | 2015-03-06 16:45:16 +0000 | [diff] [blame] | 3256 | /* |
drh | a40da62 | 2015-03-09 12:11:56 +0000 | [diff] [blame] | 3257 | ** If the most recently coded instruction is a constant range contraint |
| 3258 | ** that originated from the LIKE optimization, then change the P3 to be |
drh | f07cf6e | 2015-03-06 16:45:16 +0000 | [diff] [blame] | 3259 | ** pLoop->iLikeRepCntr and set P5. |
| 3260 | ** |
drh | 1689707 | 2015-03-07 00:57:37 +0000 | [diff] [blame] | 3261 | ** The LIKE optimization trys to evaluate "x LIKE 'abc%'" as a range |
| 3262 | ** expression: "x>='ABC' AND x<'abd'". But this requires that the range |
| 3263 | ** scan loop run twice, once for strings and a second time for BLOBs. |
| 3264 | ** The OP_String opcodes on the second pass convert the upper and lower |
| 3265 | ** bound string contants to blobs. This routine makes the necessary changes |
| 3266 | ** to the OP_String opcodes for that to happen. |
drh | f07cf6e | 2015-03-06 16:45:16 +0000 | [diff] [blame] | 3267 | */ |
drh | 52fc05b | 2015-03-07 20:32:49 +0000 | [diff] [blame] | 3268 | static void whereLikeOptimizationStringFixup( |
| 3269 | Vdbe *v, /* prepared statement under construction */ |
| 3270 | WhereLevel *pLevel, /* The loop that contains the LIKE operator */ |
| 3271 | WhereTerm *pTerm /* The upper or lower bound just coded */ |
| 3272 | ){ |
| 3273 | if( pTerm->wtFlags & TERM_LIKEOPT ){ |
drh | a40da62 | 2015-03-09 12:11:56 +0000 | [diff] [blame] | 3274 | VdbeOp *pOp; |
| 3275 | assert( pLevel->iLikeRepCntr>0 ); |
| 3276 | pOp = sqlite3VdbeGetOp(v, -1); |
| 3277 | assert( pOp!=0 ); |
| 3278 | assert( pOp->opcode==OP_String8 |
| 3279 | || pTerm->pWC->pWInfo->pParse->db->mallocFailed ); |
drh | f07cf6e | 2015-03-06 16:45:16 +0000 | [diff] [blame] | 3280 | pOp->p3 = pLevel->iLikeRepCntr; |
| 3281 | pOp->p5 = 1; |
| 3282 | } |
| 3283 | } |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 3284 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3285 | /* |
| 3286 | ** Generate code for the start of the iLevel-th loop in the WHERE clause |
| 3287 | ** implementation described by pWInfo. |
| 3288 | */ |
| 3289 | static Bitmask codeOneLoopStart( |
| 3290 | WhereInfo *pWInfo, /* Complete information about the WHERE clause */ |
| 3291 | int iLevel, /* Which level of pWInfo->a[] should be coded */ |
drh | 7a48480 | 2012-03-16 00:28:11 +0000 | [diff] [blame] | 3292 | Bitmask notReady /* Which tables are currently available */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3293 | ){ |
| 3294 | int j, k; /* Loop counters */ |
| 3295 | int iCur; /* The VDBE cursor for the table */ |
| 3296 | int addrNxt; /* Where to jump to continue with the next IN case */ |
| 3297 | int omitTable; /* True if we use the index only */ |
| 3298 | int bRev; /* True if we need to scan in reverse order */ |
| 3299 | WhereLevel *pLevel; /* The where level to be coded */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3300 | WhereLoop *pLoop; /* The WhereLoop object being coded */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3301 | WhereClause *pWC; /* Decomposition of the entire WHERE clause */ |
| 3302 | WhereTerm *pTerm; /* A WHERE clause term */ |
| 3303 | Parse *pParse; /* Parsing context */ |
drh | 6b36e82 | 2013-07-30 15:10:32 +0000 | [diff] [blame] | 3304 | sqlite3 *db; /* Database connection */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3305 | Vdbe *v; /* The prepared stmt under constructions */ |
| 3306 | struct SrcList_item *pTabItem; /* FROM clause term being coded */ |
drh | 23d04d5 | 2008-12-23 23:56:22 +0000 | [diff] [blame] | 3307 | int addrBrk; /* Jump here to break out of the loop */ |
| 3308 | int addrCont; /* Jump here to continue with next cycle */ |
drh | 6149526 | 2009-04-22 15:32:59 +0000 | [diff] [blame] | 3309 | int iRowidReg = 0; /* Rowid is stored in this register, if not zero */ |
| 3310 | int iReleaseReg = 0; /* Temp register to free before returning */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3311 | |
| 3312 | pParse = pWInfo->pParse; |
| 3313 | v = pParse->pVdbe; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 3314 | pWC = &pWInfo->sWC; |
drh | 6b36e82 | 2013-07-30 15:10:32 +0000 | [diff] [blame] | 3315 | db = pParse->db; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3316 | pLevel = &pWInfo->a[iLevel]; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3317 | pLoop = pLevel->pWLoop; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3318 | pTabItem = &pWInfo->pTabList->a[pLevel->iFrom]; |
| 3319 | iCur = pTabItem->iCursor; |
drh | 0259bc3 | 2013-09-09 19:37:46 +0000 | [diff] [blame] | 3320 | pLevel->notReady = notReady & ~getMask(&pWInfo->sMaskSet, iCur); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3321 | bRev = (pWInfo->revMask>>iLevel)&1; |
| 3322 | omitTable = (pLoop->wsFlags & WHERE_IDX_ONLY)!=0 |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 3323 | && (pWInfo->wctrlFlags & WHERE_FORCE_TABLE)==0; |
drh | 6bc69a2 | 2013-11-19 12:33:23 +0000 | [diff] [blame] | 3324 | VdbeModuleComment((v, "Begin WHERE-loop%d: %s",iLevel,pTabItem->pTab->zName)); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3325 | |
| 3326 | /* Create labels for the "break" and "continue" instructions |
| 3327 | ** for the current loop. Jump to addrBrk to break out of a loop. |
| 3328 | ** Jump to cont to go immediately to the next iteration of the |
| 3329 | ** loop. |
| 3330 | ** |
| 3331 | ** When there is an IN operator, we also have a "addrNxt" label that |
| 3332 | ** means to continue with the next IN value combination. When |
| 3333 | ** there are no IN operators in the constraints, the "addrNxt" label |
| 3334 | ** is the same as "addrBrk". |
| 3335 | */ |
| 3336 | addrBrk = pLevel->addrBrk = pLevel->addrNxt = sqlite3VdbeMakeLabel(v); |
| 3337 | addrCont = pLevel->addrCont = sqlite3VdbeMakeLabel(v); |
| 3338 | |
| 3339 | /* If this is the right table of a LEFT OUTER JOIN, allocate and |
| 3340 | ** initialize a memory cell that records if this table matches any |
| 3341 | ** row of the left table of the join. |
| 3342 | */ |
| 3343 | if( pLevel->iFrom>0 && (pTabItem[0].jointype & JT_LEFT)!=0 ){ |
| 3344 | pLevel->iLeftJoin = ++pParse->nMem; |
| 3345 | sqlite3VdbeAddOp2(v, OP_Integer, 0, pLevel->iLeftJoin); |
| 3346 | VdbeComment((v, "init LEFT JOIN no-match flag")); |
| 3347 | } |
| 3348 | |
drh | 21172c4 | 2012-10-30 00:29:07 +0000 | [diff] [blame] | 3349 | /* Special case of a FROM clause subquery implemented as a co-routine */ |
| 3350 | if( pTabItem->viaCoroutine ){ |
| 3351 | int regYield = pTabItem->regReturn; |
drh | ed71a83 | 2014-02-07 19:18:10 +0000 | [diff] [blame] | 3352 | sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, pTabItem->addrFillSub); |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 3353 | pLevel->p2 = sqlite3VdbeAddOp2(v, OP_Yield, regYield, addrBrk); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 3354 | VdbeCoverage(v); |
drh | 725de29 | 2014-02-08 13:12:19 +0000 | [diff] [blame] | 3355 | VdbeComment((v, "next row of \"%s\"", pTabItem->pTab->zName)); |
drh | 21172c4 | 2012-10-30 00:29:07 +0000 | [diff] [blame] | 3356 | pLevel->op = OP_Goto; |
| 3357 | }else |
| 3358 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3359 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3360 | if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){ |
| 3361 | /* Case 1: The table is a virtual-table. Use the VFilter and VNext |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3362 | ** to access the data. |
| 3363 | */ |
| 3364 | int iReg; /* P3 Value for OP_VFilter */ |
drh | 281bbe2 | 2012-10-16 23:17:14 +0000 | [diff] [blame] | 3365 | int addrNotFound; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3366 | int nConstraint = pLoop->nLTerm; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3367 | |
drh | a62bb8d | 2009-11-23 21:23:45 +0000 | [diff] [blame] | 3368 | sqlite3ExprCachePush(pParse); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3369 | iReg = sqlite3GetTempRange(pParse, nConstraint+2); |
drh | 281bbe2 | 2012-10-16 23:17:14 +0000 | [diff] [blame] | 3370 | addrNotFound = pLevel->addrBrk; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3371 | for(j=0; j<nConstraint; j++){ |
drh | e225017 | 2013-05-31 18:13:50 +0000 | [diff] [blame] | 3372 | int iTarget = iReg+j+2; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3373 | pTerm = pLoop->aLTerm[j]; |
drh | 95ed68d | 2013-06-12 17:55:50 +0000 | [diff] [blame] | 3374 | if( pTerm==0 ) continue; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3375 | if( pTerm->eOperator & WO_IN ){ |
| 3376 | codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, iTarget); |
| 3377 | addrNotFound = pLevel->addrNxt; |
| 3378 | }else{ |
| 3379 | sqlite3ExprCode(pParse, pTerm->pExpr->pRight, iTarget); |
| 3380 | } |
| 3381 | } |
| 3382 | sqlite3VdbeAddOp2(v, OP_Integer, pLoop->u.vtab.idxNum, iReg); |
drh | 7e47cb8 | 2013-05-31 17:55:27 +0000 | [diff] [blame] | 3383 | sqlite3VdbeAddOp2(v, OP_Integer, nConstraint, iReg+1); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3384 | sqlite3VdbeAddOp4(v, OP_VFilter, iCur, addrNotFound, iReg, |
| 3385 | pLoop->u.vtab.idxStr, |
| 3386 | pLoop->u.vtab.needFree ? P4_MPRINTF : P4_STATIC); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 3387 | VdbeCoverage(v); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3388 | pLoop->u.vtab.needFree = 0; |
| 3389 | for(j=0; j<nConstraint && j<16; j++){ |
| 3390 | if( (pLoop->u.vtab.omitMask>>j)&1 ){ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3391 | disableTerm(pLevel, pLoop->aLTerm[j]); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3392 | } |
| 3393 | } |
| 3394 | pLevel->op = OP_VNext; |
| 3395 | pLevel->p1 = iCur; |
| 3396 | pLevel->p2 = sqlite3VdbeCurrentAddr(v); |
drh | 23d04d5 | 2008-12-23 23:56:22 +0000 | [diff] [blame] | 3397 | sqlite3ReleaseTempRange(pParse, iReg, nConstraint+2); |
drh | d249090 | 2014-04-13 19:28:15 +0000 | [diff] [blame] | 3398 | sqlite3ExprCachePop(pParse); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3399 | }else |
| 3400 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 3401 | |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3402 | if( (pLoop->wsFlags & WHERE_IPK)!=0 |
| 3403 | && (pLoop->wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_EQ))!=0 |
| 3404 | ){ |
| 3405 | /* Case 2: We can directly reference a single row using an |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3406 | ** equality comparison against the ROWID field. Or |
| 3407 | ** we reference multiple rows using a "rowid IN (...)" |
| 3408 | ** construct. |
| 3409 | */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3410 | assert( pLoop->u.btree.nEq==1 ); |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3411 | pTerm = pLoop->aLTerm[0]; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3412 | assert( pTerm!=0 ); |
| 3413 | assert( pTerm->pExpr!=0 ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3414 | assert( omitTable==0 ); |
drh | 3975974 | 2013-08-02 23:40:45 +0000 | [diff] [blame] | 3415 | testcase( pTerm->wtFlags & TERM_VIRTUAL ); |
drh | 0baa035 | 2014-02-25 21:55:16 +0000 | [diff] [blame] | 3416 | iReleaseReg = ++pParse->nMem; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3417 | iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, 0, bRev, iReleaseReg); |
drh | 0baa035 | 2014-02-25 21:55:16 +0000 | [diff] [blame] | 3418 | if( iRowidReg!=iReleaseReg ) sqlite3ReleaseTempReg(pParse, iReleaseReg); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3419 | addrNxt = pLevel->addrNxt; |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 3420 | sqlite3VdbeAddOp2(v, OP_MustBeInt, iRowidReg, addrNxt); VdbeCoverage(v); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3421 | sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addrNxt, iRowidReg); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 3422 | VdbeCoverage(v); |
drh | 459f63e | 2013-03-06 01:55:27 +0000 | [diff] [blame] | 3423 | sqlite3ExprCacheAffinityChange(pParse, iRowidReg, 1); |
drh | ceea332 | 2009-04-23 13:22:42 +0000 | [diff] [blame] | 3424 | sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3425 | VdbeComment((v, "pk")); |
| 3426 | pLevel->op = OP_Noop; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3427 | }else if( (pLoop->wsFlags & WHERE_IPK)!=0 |
| 3428 | && (pLoop->wsFlags & WHERE_COLUMN_RANGE)!=0 |
| 3429 | ){ |
| 3430 | /* Case 3: We have an inequality comparison against the ROWID field. |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3431 | */ |
| 3432 | int testOp = OP_Noop; |
| 3433 | int start; |
| 3434 | int memEndValue = 0; |
| 3435 | WhereTerm *pStart, *pEnd; |
| 3436 | |
| 3437 | assert( omitTable==0 ); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3438 | j = 0; |
| 3439 | pStart = pEnd = 0; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3440 | if( pLoop->wsFlags & WHERE_BTM_LIMIT ) pStart = pLoop->aLTerm[j++]; |
| 3441 | if( pLoop->wsFlags & WHERE_TOP_LIMIT ) pEnd = pLoop->aLTerm[j++]; |
drh | 81186b4 | 2013-06-18 01:52:41 +0000 | [diff] [blame] | 3442 | assert( pStart!=0 || pEnd!=0 ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3443 | if( bRev ){ |
| 3444 | pTerm = pStart; |
| 3445 | pStart = pEnd; |
| 3446 | pEnd = pTerm; |
| 3447 | } |
| 3448 | if( pStart ){ |
| 3449 | Expr *pX; /* The expression that defines the start bound */ |
| 3450 | int r1, rTemp; /* Registers for holding the start boundary */ |
| 3451 | |
| 3452 | /* The following constant maps TK_xx codes into corresponding |
| 3453 | ** seek opcodes. It depends on a particular ordering of TK_xx |
| 3454 | */ |
| 3455 | const u8 aMoveOp[] = { |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 3456 | /* TK_GT */ OP_SeekGT, |
| 3457 | /* TK_LE */ OP_SeekLE, |
| 3458 | /* TK_LT */ OP_SeekLT, |
| 3459 | /* TK_GE */ OP_SeekGE |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3460 | }; |
| 3461 | assert( TK_LE==TK_GT+1 ); /* Make sure the ordering.. */ |
| 3462 | assert( TK_LT==TK_GT+2 ); /* ... of the TK_xx values... */ |
| 3463 | assert( TK_GE==TK_GT+3 ); /* ... is correcct. */ |
| 3464 | |
drh | b5246e5 | 2013-07-08 21:12:57 +0000 | [diff] [blame] | 3465 | assert( (pStart->wtFlags & TERM_VNULL)==0 ); |
drh | 3975974 | 2013-08-02 23:40:45 +0000 | [diff] [blame] | 3466 | testcase( pStart->wtFlags & TERM_VIRTUAL ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3467 | pX = pStart->pExpr; |
| 3468 | assert( pX!=0 ); |
drh | b5246e5 | 2013-07-08 21:12:57 +0000 | [diff] [blame] | 3469 | testcase( pStart->leftCursor!=iCur ); /* transitive constraints */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3470 | r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, &rTemp); |
| 3471 | sqlite3VdbeAddOp3(v, aMoveOp[pX->op-TK_GT], iCur, addrBrk, r1); |
drh | 7d17610 | 2014-02-18 03:07:12 +0000 | [diff] [blame] | 3472 | VdbeComment((v, "pk")); |
| 3473 | VdbeCoverageIf(v, pX->op==TK_GT); |
| 3474 | VdbeCoverageIf(v, pX->op==TK_LE); |
| 3475 | VdbeCoverageIf(v, pX->op==TK_LT); |
| 3476 | VdbeCoverageIf(v, pX->op==TK_GE); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3477 | sqlite3ExprCacheAffinityChange(pParse, r1, 1); |
| 3478 | sqlite3ReleaseTempReg(pParse, rTemp); |
| 3479 | disableTerm(pLevel, pStart); |
| 3480 | }else{ |
| 3481 | sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iCur, addrBrk); |
drh | 7d17610 | 2014-02-18 03:07:12 +0000 | [diff] [blame] | 3482 | VdbeCoverageIf(v, bRev==0); |
| 3483 | VdbeCoverageIf(v, bRev!=0); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3484 | } |
| 3485 | if( pEnd ){ |
| 3486 | Expr *pX; |
| 3487 | pX = pEnd->pExpr; |
| 3488 | assert( pX!=0 ); |
drh | b5246e5 | 2013-07-08 21:12:57 +0000 | [diff] [blame] | 3489 | assert( (pEnd->wtFlags & TERM_VNULL)==0 ); |
| 3490 | testcase( pEnd->leftCursor!=iCur ); /* Transitive constraints */ |
drh | 3975974 | 2013-08-02 23:40:45 +0000 | [diff] [blame] | 3491 | testcase( pEnd->wtFlags & TERM_VIRTUAL ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3492 | memEndValue = ++pParse->nMem; |
| 3493 | sqlite3ExprCode(pParse, pX->pRight, memEndValue); |
| 3494 | if( pX->op==TK_LT || pX->op==TK_GT ){ |
| 3495 | testOp = bRev ? OP_Le : OP_Ge; |
| 3496 | }else{ |
| 3497 | testOp = bRev ? OP_Lt : OP_Gt; |
| 3498 | } |
| 3499 | disableTerm(pLevel, pEnd); |
| 3500 | } |
| 3501 | start = sqlite3VdbeCurrentAddr(v); |
| 3502 | pLevel->op = bRev ? OP_Prev : OP_Next; |
| 3503 | pLevel->p1 = iCur; |
| 3504 | pLevel->p2 = start; |
drh | 81186b4 | 2013-06-18 01:52:41 +0000 | [diff] [blame] | 3505 | assert( pLevel->p5==0 ); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3506 | if( testOp!=OP_Noop ){ |
drh | 0baa035 | 2014-02-25 21:55:16 +0000 | [diff] [blame] | 3507 | iRowidReg = ++pParse->nMem; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3508 | sqlite3VdbeAddOp2(v, OP_Rowid, iCur, iRowidReg); |
drh | ceea332 | 2009-04-23 13:22:42 +0000 | [diff] [blame] | 3509 | sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3510 | sqlite3VdbeAddOp3(v, testOp, memEndValue, addrBrk, iRowidReg); |
drh | 7d17610 | 2014-02-18 03:07:12 +0000 | [diff] [blame] | 3511 | VdbeCoverageIf(v, testOp==OP_Le); |
| 3512 | VdbeCoverageIf(v, testOp==OP_Lt); |
| 3513 | VdbeCoverageIf(v, testOp==OP_Ge); |
| 3514 | VdbeCoverageIf(v, testOp==OP_Gt); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3515 | sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC | SQLITE_JUMPIFNULL); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3516 | } |
drh | 1b0f026 | 2013-05-30 22:27:09 +0000 | [diff] [blame] | 3517 | }else if( pLoop->wsFlags & WHERE_INDEXED ){ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3518 | /* Case 4: A scan using an index. |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3519 | ** |
| 3520 | ** The WHERE clause may contain zero or more equality |
| 3521 | ** terms ("==" or "IN" operators) that refer to the N |
| 3522 | ** left-most columns of the index. It may also contain |
| 3523 | ** inequality constraints (>, <, >= or <=) on the indexed |
| 3524 | ** column that immediately follows the N equalities. Only |
| 3525 | ** the right-most column can be an inequality - the rest must |
| 3526 | ** use the "==" and "IN" operators. For example, if the |
| 3527 | ** index is on (x,y,z), then the following clauses are all |
| 3528 | ** optimized: |
| 3529 | ** |
| 3530 | ** x=5 |
| 3531 | ** x=5 AND y=10 |
| 3532 | ** x=5 AND y<10 |
| 3533 | ** x=5 AND y>5 AND y<10 |
| 3534 | ** x=5 AND y=5 AND z<=10 |
| 3535 | ** |
| 3536 | ** The z<10 term of the following cannot be used, only |
| 3537 | ** the x=5 term: |
| 3538 | ** |
| 3539 | ** x=5 AND z<10 |
| 3540 | ** |
| 3541 | ** N may be zero if there are inequality constraints. |
| 3542 | ** If there are no inequality constraints, then N is at |
| 3543 | ** least one. |
| 3544 | ** |
| 3545 | ** This case is also used when there are no WHERE clause |
| 3546 | ** constraints but an index is selected anyway, in order |
| 3547 | ** to force the output order to conform to an ORDER BY. |
| 3548 | */ |
drh | 3bb9b93 | 2010-08-06 02:10:00 +0000 | [diff] [blame] | 3549 | static const u8 aStartOp[] = { |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3550 | 0, |
| 3551 | 0, |
| 3552 | OP_Rewind, /* 2: (!start_constraints && startEq && !bRev) */ |
| 3553 | OP_Last, /* 3: (!start_constraints && startEq && bRev) */ |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 3554 | OP_SeekGT, /* 4: (start_constraints && !startEq && !bRev) */ |
| 3555 | OP_SeekLT, /* 5: (start_constraints && !startEq && bRev) */ |
| 3556 | OP_SeekGE, /* 6: (start_constraints && startEq && !bRev) */ |
| 3557 | OP_SeekLE /* 7: (start_constraints && startEq && bRev) */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3558 | }; |
drh | 3bb9b93 | 2010-08-06 02:10:00 +0000 | [diff] [blame] | 3559 | static const u8 aEndOp[] = { |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 3560 | OP_IdxGE, /* 0: (end_constraints && !bRev && !endEq) */ |
| 3561 | OP_IdxGT, /* 1: (end_constraints && !bRev && endEq) */ |
| 3562 | OP_IdxLE, /* 2: (end_constraints && bRev && !endEq) */ |
| 3563 | OP_IdxLT, /* 3: (end_constraints && bRev && endEq) */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3564 | }; |
drh | cd8629e | 2013-11-13 12:27:25 +0000 | [diff] [blame] | 3565 | u16 nEq = pLoop->u.btree.nEq; /* Number of == or IN terms */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3566 | int regBase; /* Base register holding constraint values */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3567 | WhereTerm *pRangeStart = 0; /* Inequality constraint at range start */ |
| 3568 | WhereTerm *pRangeEnd = 0; /* Inequality constraint at range end */ |
| 3569 | int startEq; /* True if range start uses ==, >= or <= */ |
| 3570 | int endEq; /* True if range end uses ==, >= or <= */ |
| 3571 | int start_constraints; /* Start of range is constrained */ |
| 3572 | int nConstraint; /* Number of constraint terms */ |
drh | 3bb9b93 | 2010-08-06 02:10:00 +0000 | [diff] [blame] | 3573 | Index *pIdx; /* The index we will be using */ |
| 3574 | int iIdxCur; /* The VDBE cursor for the index */ |
| 3575 | int nExtraReg = 0; /* Number of extra registers needed */ |
| 3576 | int op; /* Instruction opcode */ |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3577 | char *zStartAff; /* Affinity for start of range constraint */ |
drh | 33cad2f | 2013-11-15 12:41:01 +0000 | [diff] [blame] | 3578 | char cEndAff = 0; /* Affinity for end of range constraint */ |
drh | cfc6ca4 | 2014-02-14 23:49:13 +0000 | [diff] [blame] | 3579 | u8 bSeekPastNull = 0; /* True to seek past initial nulls */ |
| 3580 | u8 bStopAtNull = 0; /* Add condition to terminate at NULLs */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3581 | |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3582 | pIdx = pLoop->u.btree.pIndex; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3583 | iIdxCur = pLevel->iIdxCur; |
drh | c8bbce1 | 2014-10-21 01:05:09 +0000 | [diff] [blame] | 3584 | assert( nEq>=pLoop->nSkip ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3585 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3586 | /* If this loop satisfies a sort order (pOrderBy) request that |
| 3587 | ** was passed to this function to implement a "SELECT min(x) ..." |
| 3588 | ** query, then the caller will only allow the loop to run for |
| 3589 | ** a single iteration. This means that the first row returned |
| 3590 | ** should not have a NULL value stored in 'x'. If column 'x' is |
| 3591 | ** the first one after the nEq equality constraints in the index, |
| 3592 | ** this requires some special handling. |
| 3593 | */ |
drh | ddba0c2 | 2014-03-18 20:33:42 +0000 | [diff] [blame] | 3594 | assert( pWInfo->pOrderBy==0 |
| 3595 | || pWInfo->pOrderBy->nExpr==1 |
| 3596 | || (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)==0 ); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 3597 | if( (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)!=0 |
drh | ddba0c2 | 2014-03-18 20:33:42 +0000 | [diff] [blame] | 3598 | && pWInfo->nOBSat>0 |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 3599 | && (pIdx->nKeyCol>nEq) |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3600 | ){ |
drh | c8bbce1 | 2014-10-21 01:05:09 +0000 | [diff] [blame] | 3601 | assert( pLoop->nSkip==0 ); |
drh | cfc6ca4 | 2014-02-14 23:49:13 +0000 | [diff] [blame] | 3602 | bSeekPastNull = 1; |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 3603 | nExtraReg = 1; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3604 | } |
| 3605 | |
| 3606 | /* Find any inequality constraint terms for the start and end |
| 3607 | ** of the range. |
| 3608 | */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3609 | j = nEq; |
| 3610 | if( pLoop->wsFlags & WHERE_BTM_LIMIT ){ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3611 | pRangeStart = pLoop->aLTerm[j++]; |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 3612 | nExtraReg = 1; |
drh | 8031462 | 2015-03-09 13:01:02 +0000 | [diff] [blame] | 3613 | /* Like optimization range constraints always occur in pairs */ |
| 3614 | assert( (pRangeStart->wtFlags & TERM_LIKEOPT)==0 || |
| 3615 | (pLoop->wsFlags & WHERE_TOP_LIMIT)!=0 ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3616 | } |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3617 | if( pLoop->wsFlags & WHERE_TOP_LIMIT ){ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3618 | pRangeEnd = pLoop->aLTerm[j++]; |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 3619 | nExtraReg = 1; |
drh | a40da62 | 2015-03-09 12:11:56 +0000 | [diff] [blame] | 3620 | if( (pRangeEnd->wtFlags & TERM_LIKEOPT)!=0 ){ |
drh | 8031462 | 2015-03-09 13:01:02 +0000 | [diff] [blame] | 3621 | assert( pRangeStart!=0 ); /* LIKE opt constraints */ |
| 3622 | assert( pRangeStart->wtFlags & TERM_LIKEOPT ); /* occur in pairs */ |
drh | f07cf6e | 2015-03-06 16:45:16 +0000 | [diff] [blame] | 3623 | pLevel->iLikeRepCntr = ++pParse->nMem; |
drh | b7c60ba | 2015-03-07 02:51:59 +0000 | [diff] [blame] | 3624 | testcase( bRev ); |
| 3625 | testcase( pIdx->aSortOrder[nEq]==SQLITE_SO_DESC ); |
| 3626 | sqlite3VdbeAddOp2(v, OP_Integer, |
| 3627 | bRev ^ (pIdx->aSortOrder[nEq]==SQLITE_SO_DESC), |
| 3628 | pLevel->iLikeRepCntr); |
drh | 1689707 | 2015-03-07 00:57:37 +0000 | [diff] [blame] | 3629 | VdbeComment((v, "LIKE loop counter")); |
drh | f07cf6e | 2015-03-06 16:45:16 +0000 | [diff] [blame] | 3630 | pLevel->addrLikeRep = sqlite3VdbeCurrentAddr(v); |
| 3631 | } |
drh | cfc6ca4 | 2014-02-14 23:49:13 +0000 | [diff] [blame] | 3632 | if( pRangeStart==0 |
drh | cfc6ca4 | 2014-02-14 23:49:13 +0000 | [diff] [blame] | 3633 | && (j = pIdx->aiColumn[nEq])>=0 |
| 3634 | && pIdx->pTable->aCol[j].notNull==0 |
| 3635 | ){ |
| 3636 | bSeekPastNull = 1; |
| 3637 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3638 | } |
dan | 0df163a | 2014-03-06 12:36:26 +0000 | [diff] [blame] | 3639 | assert( pRangeEnd==0 || (pRangeEnd->wtFlags & TERM_VNULL)==0 ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3640 | |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 3641 | /* Generate code to evaluate all constraint terms using == or IN |
| 3642 | ** and store the values of those terms in an array of registers |
| 3643 | ** starting at regBase. |
| 3644 | */ |
drh | 613ba1e | 2013-06-15 15:11:45 +0000 | [diff] [blame] | 3645 | regBase = codeAllEqualityTerms(pParse,pLevel,bRev,nExtraReg,&zStartAff); |
drh | 33cad2f | 2013-11-15 12:41:01 +0000 | [diff] [blame] | 3646 | assert( zStartAff==0 || sqlite3Strlen30(zStartAff)>=nEq ); |
| 3647 | if( zStartAff ) cEndAff = zStartAff[nEq]; |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 3648 | addrNxt = pLevel->addrNxt; |
| 3649 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3650 | /* If we are doing a reverse order scan on an ascending index, or |
| 3651 | ** a forward order scan on a descending index, interchange the |
| 3652 | ** start and end terms (pRangeStart and pRangeEnd). |
| 3653 | */ |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 3654 | if( (nEq<pIdx->nKeyCol && bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC)) |
| 3655 | || (bRev && pIdx->nKeyCol==nEq) |
dan | 0c733f6 | 2011-11-16 15:27:09 +0000 | [diff] [blame] | 3656 | ){ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3657 | SWAP(WhereTerm *, pRangeEnd, pRangeStart); |
drh | cfc6ca4 | 2014-02-14 23:49:13 +0000 | [diff] [blame] | 3658 | SWAP(u8, bSeekPastNull, bStopAtNull); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3659 | } |
| 3660 | |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 3661 | testcase( pRangeStart && (pRangeStart->eOperator & WO_LE)!=0 ); |
| 3662 | testcase( pRangeStart && (pRangeStart->eOperator & WO_GE)!=0 ); |
| 3663 | testcase( pRangeEnd && (pRangeEnd->eOperator & WO_LE)!=0 ); |
| 3664 | testcase( pRangeEnd && (pRangeEnd->eOperator & WO_GE)!=0 ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3665 | startEq = !pRangeStart || pRangeStart->eOperator & (WO_LE|WO_GE); |
| 3666 | endEq = !pRangeEnd || pRangeEnd->eOperator & (WO_LE|WO_GE); |
| 3667 | start_constraints = pRangeStart || nEq>0; |
| 3668 | |
| 3669 | /* Seek the index cursor to the start of the range. */ |
| 3670 | nConstraint = nEq; |
| 3671 | if( pRangeStart ){ |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 3672 | Expr *pRight = pRangeStart->pExpr->pRight; |
| 3673 | sqlite3ExprCode(pParse, pRight, regBase+nEq); |
drh | 52fc05b | 2015-03-07 20:32:49 +0000 | [diff] [blame] | 3674 | whereLikeOptimizationStringFixup(v, pLevel, pRangeStart); |
drh | 9be1870 | 2015-05-13 19:33:41 +0000 | [diff] [blame] | 3675 | if( (pRangeStart->wtFlags & TERM_VNULL)==0 |
drh | 7d17610 | 2014-02-18 03:07:12 +0000 | [diff] [blame] | 3676 | && sqlite3ExprCanBeNull(pRight) |
| 3677 | ){ |
| 3678 | sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt); |
| 3679 | VdbeCoverage(v); |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 3680 | } |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3681 | if( zStartAff ){ |
| 3682 | if( sqlite3CompareAffinity(pRight, zStartAff[nEq])==SQLITE_AFF_NONE){ |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 3683 | /* Since the comparison is to be performed with no conversions |
| 3684 | ** applied to the operands, set the affinity to apply to pRight to |
| 3685 | ** SQLITE_AFF_NONE. */ |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3686 | zStartAff[nEq] = SQLITE_AFF_NONE; |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 3687 | } |
dan | 6ac4339 | 2010-06-09 15:47:11 +0000 | [diff] [blame] | 3688 | if( sqlite3ExprNeedsNoAffinityChange(pRight, zStartAff[nEq]) ){ |
| 3689 | zStartAff[nEq] = SQLITE_AFF_NONE; |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 3690 | } |
| 3691 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3692 | nConstraint++; |
drh | 3975974 | 2013-08-02 23:40:45 +0000 | [diff] [blame] | 3693 | testcase( pRangeStart->wtFlags & TERM_VIRTUAL ); |
drh | cfc6ca4 | 2014-02-14 23:49:13 +0000 | [diff] [blame] | 3694 | }else if( bSeekPastNull ){ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3695 | sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq); |
| 3696 | nConstraint++; |
| 3697 | startEq = 0; |
| 3698 | start_constraints = 1; |
| 3699 | } |
drh | cfc6ca4 | 2014-02-14 23:49:13 +0000 | [diff] [blame] | 3700 | codeApplyAffinity(pParse, regBase, nConstraint - bSeekPastNull, zStartAff); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3701 | op = aStartOp[(start_constraints<<2) + (startEq<<1) + bRev]; |
| 3702 | assert( op!=0 ); |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 3703 | sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 3704 | VdbeCoverage(v); |
drh | 7d17610 | 2014-02-18 03:07:12 +0000 | [diff] [blame] | 3705 | VdbeCoverageIf(v, op==OP_Rewind); testcase( op==OP_Rewind ); |
| 3706 | VdbeCoverageIf(v, op==OP_Last); testcase( op==OP_Last ); |
| 3707 | VdbeCoverageIf(v, op==OP_SeekGT); testcase( op==OP_SeekGT ); |
| 3708 | VdbeCoverageIf(v, op==OP_SeekGE); testcase( op==OP_SeekGE ); |
| 3709 | VdbeCoverageIf(v, op==OP_SeekLE); testcase( op==OP_SeekLE ); |
| 3710 | VdbeCoverageIf(v, op==OP_SeekLT); testcase( op==OP_SeekLT ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3711 | |
| 3712 | /* Load the value for the inequality constraint at the end of the |
| 3713 | ** range (if any). |
| 3714 | */ |
| 3715 | nConstraint = nEq; |
| 3716 | if( pRangeEnd ){ |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 3717 | Expr *pRight = pRangeEnd->pExpr->pRight; |
drh | f49f352 | 2009-12-30 14:12:38 +0000 | [diff] [blame] | 3718 | sqlite3ExprCacheRemove(pParse, regBase+nEq, 1); |
dan | 69f8bb9 | 2009-08-13 19:21:16 +0000 | [diff] [blame] | 3719 | sqlite3ExprCode(pParse, pRight, regBase+nEq); |
drh | 52fc05b | 2015-03-07 20:32:49 +0000 | [diff] [blame] | 3720 | whereLikeOptimizationStringFixup(v, pLevel, pRangeEnd); |
drh | 9be1870 | 2015-05-13 19:33:41 +0000 | [diff] [blame] | 3721 | if( (pRangeEnd->wtFlags & TERM_VNULL)==0 |
drh | 7d17610 | 2014-02-18 03:07:12 +0000 | [diff] [blame] | 3722 | && sqlite3ExprCanBeNull(pRight) |
| 3723 | ){ |
| 3724 | sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt); |
| 3725 | VdbeCoverage(v); |
drh | 534230c | 2011-01-22 00:10:45 +0000 | [diff] [blame] | 3726 | } |
drh | 33cad2f | 2013-11-15 12:41:01 +0000 | [diff] [blame] | 3727 | if( sqlite3CompareAffinity(pRight, cEndAff)!=SQLITE_AFF_NONE |
| 3728 | && !sqlite3ExprNeedsNoAffinityChange(pRight, cEndAff) |
| 3729 | ){ |
| 3730 | codeApplyAffinity(pParse, regBase+nEq, 1, &cEndAff); |
| 3731 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3732 | nConstraint++; |
drh | 3975974 | 2013-08-02 23:40:45 +0000 | [diff] [blame] | 3733 | testcase( pRangeEnd->wtFlags & TERM_VIRTUAL ); |
drh | cfc6ca4 | 2014-02-14 23:49:13 +0000 | [diff] [blame] | 3734 | }else if( bStopAtNull ){ |
| 3735 | sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq); |
| 3736 | endEq = 0; |
| 3737 | nConstraint++; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3738 | } |
drh | 6b36e82 | 2013-07-30 15:10:32 +0000 | [diff] [blame] | 3739 | sqlite3DbFree(db, zStartAff); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3740 | |
| 3741 | /* Top of the loop body */ |
| 3742 | pLevel->p2 = sqlite3VdbeCurrentAddr(v); |
| 3743 | |
| 3744 | /* Check if the index cursor is past the end of the range. */ |
drh | cfc6ca4 | 2014-02-14 23:49:13 +0000 | [diff] [blame] | 3745 | if( nConstraint ){ |
drh | 4a1d365 | 2014-02-14 15:13:36 +0000 | [diff] [blame] | 3746 | op = aEndOp[bRev*2 + endEq]; |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 3747 | sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint); |
drh | 7d17610 | 2014-02-18 03:07:12 +0000 | [diff] [blame] | 3748 | testcase( op==OP_IdxGT ); VdbeCoverageIf(v, op==OP_IdxGT ); |
| 3749 | testcase( op==OP_IdxGE ); VdbeCoverageIf(v, op==OP_IdxGE ); |
| 3750 | testcase( op==OP_IdxLT ); VdbeCoverageIf(v, op==OP_IdxLT ); |
| 3751 | testcase( op==OP_IdxLE ); VdbeCoverageIf(v, op==OP_IdxLE ); |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 3752 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3753 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3754 | /* Seek the table cursor, if required */ |
drh | 23d04d5 | 2008-12-23 23:56:22 +0000 | [diff] [blame] | 3755 | disableTerm(pLevel, pRangeStart); |
| 3756 | disableTerm(pLevel, pRangeEnd); |
drh | 85c1c55 | 2013-10-24 00:18:18 +0000 | [diff] [blame] | 3757 | if( omitTable ){ |
| 3758 | /* pIdx is a covering index. No need to access the main table. */ |
| 3759 | }else if( HasRowid(pIdx->pTable) ){ |
drh | 0baa035 | 2014-02-25 21:55:16 +0000 | [diff] [blame] | 3760 | iRowidReg = ++pParse->nMem; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3761 | sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, iRowidReg); |
drh | ceea332 | 2009-04-23 13:22:42 +0000 | [diff] [blame] | 3762 | sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3763 | sqlite3VdbeAddOp2(v, OP_Seek, iCur, iRowidReg); /* Deferred seek */ |
drh | a3bc66a | 2014-05-27 17:57:32 +0000 | [diff] [blame] | 3764 | }else if( iCur!=iIdxCur ){ |
drh | 85c1c55 | 2013-10-24 00:18:18 +0000 | [diff] [blame] | 3765 | Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable); |
| 3766 | iRowidReg = sqlite3GetTempRange(pParse, pPk->nKeyCol); |
| 3767 | for(j=0; j<pPk->nKeyCol; j++){ |
| 3768 | k = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[j]); |
| 3769 | sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, k, iRowidReg+j); |
| 3770 | } |
drh | 261c02d | 2013-10-25 14:46:15 +0000 | [diff] [blame] | 3771 | sqlite3VdbeAddOp4Int(v, OP_NotFound, iCur, addrCont, |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 3772 | iRowidReg, pPk->nKeyCol); VdbeCoverage(v); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3773 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3774 | |
| 3775 | /* Record the instruction used to terminate the loop. Disable |
| 3776 | ** WHERE clause terms made redundant by the index range scan. |
| 3777 | */ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 3778 | if( pLoop->wsFlags & WHERE_ONEROW ){ |
drh | 95e037b | 2011-03-09 21:02:31 +0000 | [diff] [blame] | 3779 | pLevel->op = OP_Noop; |
| 3780 | }else if( bRev ){ |
| 3781 | pLevel->op = OP_Prev; |
| 3782 | }else{ |
| 3783 | pLevel->op = OP_Next; |
| 3784 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3785 | pLevel->p1 = iIdxCur; |
drh | 0c8a934 | 2014-03-20 12:17:35 +0000 | [diff] [blame] | 3786 | pLevel->p3 = (pLoop->wsFlags&WHERE_UNQ_WANTED)!=0 ? 1:0; |
drh | 53cfbe9 | 2013-06-13 17:28:22 +0000 | [diff] [blame] | 3787 | if( (pLoop->wsFlags & WHERE_CONSTRAINT)==0 ){ |
drh | 3f4d1d1 | 2012-09-15 18:45:54 +0000 | [diff] [blame] | 3788 | pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP; |
| 3789 | }else{ |
| 3790 | assert( pLevel->p5==0 ); |
| 3791 | } |
drh | dd5f5a6 | 2008-12-23 13:35:23 +0000 | [diff] [blame] | 3792 | }else |
| 3793 | |
drh | 23d04d5 | 2008-12-23 23:56:22 +0000 | [diff] [blame] | 3794 | #ifndef SQLITE_OMIT_OR_OPTIMIZATION |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3795 | if( pLoop->wsFlags & WHERE_MULTI_OR ){ |
| 3796 | /* Case 5: Two or more separately indexed terms connected by OR |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3797 | ** |
| 3798 | ** Example: |
| 3799 | ** |
| 3800 | ** CREATE TABLE t1(a,b,c,d); |
| 3801 | ** CREATE INDEX i1 ON t1(a); |
| 3802 | ** CREATE INDEX i2 ON t1(b); |
| 3803 | ** CREATE INDEX i3 ON t1(c); |
| 3804 | ** |
| 3805 | ** SELECT * FROM t1 WHERE a=5 OR b=7 OR (c=11 AND d=13) |
| 3806 | ** |
| 3807 | ** In the example, there are three indexed terms connected by OR. |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3808 | ** The top of the loop looks like this: |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3809 | ** |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 3810 | ** Null 1 # Zero the rowset in reg 1 |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3811 | ** |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3812 | ** Then, for each indexed term, the following. The arguments to |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 3813 | ** RowSetTest are such that the rowid of the current row is inserted |
| 3814 | ** into the RowSet. If it is already present, control skips the |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3815 | ** Gosub opcode and jumps straight to the code generated by WhereEnd(). |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3816 | ** |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3817 | ** sqlite3WhereBegin(<term>) |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 3818 | ** RowSetTest # Insert rowid into rowset |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3819 | ** Gosub 2 A |
| 3820 | ** sqlite3WhereEnd() |
| 3821 | ** |
| 3822 | ** Following the above, code to terminate the loop. Label A, the target |
| 3823 | ** of the Gosub above, jumps to the instruction right after the Goto. |
| 3824 | ** |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 3825 | ** Null 1 # Zero the rowset in reg 1 |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3826 | ** Goto B # The loop is finished. |
| 3827 | ** |
| 3828 | ** A: <loop body> # Return data, whatever. |
| 3829 | ** |
| 3830 | ** Return 2 # Jump back to the Gosub |
| 3831 | ** |
| 3832 | ** B: <after the loop> |
| 3833 | ** |
drh | 5609baf | 2014-05-26 22:01:00 +0000 | [diff] [blame] | 3834 | ** Added 2014-05-26: If the table is a WITHOUT ROWID table, then |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 3835 | ** use an ephemeral index instead of a RowSet to record the primary |
drh | 5609baf | 2014-05-26 22:01:00 +0000 | [diff] [blame] | 3836 | ** keys of the rows we have already seen. |
| 3837 | ** |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3838 | */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3839 | WhereClause *pOrWc; /* The OR-clause broken out into subterms */ |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3840 | SrcList *pOrTab; /* Shortened table list or OR-clause generation */ |
dan | 0efb72c | 2012-08-24 18:44:56 +0000 | [diff] [blame] | 3841 | Index *pCov = 0; /* Potential covering index (or NULL) */ |
| 3842 | int iCovCur = pParse->nTab++; /* Cursor used for index scans (if any) */ |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3843 | |
| 3844 | int regReturn = ++pParse->nMem; /* Register used with OP_Gosub */ |
shane | 8509570 | 2009-06-15 16:27:08 +0000 | [diff] [blame] | 3845 | int regRowset = 0; /* Register for RowSet object */ |
| 3846 | int regRowid = 0; /* Register holding rowid */ |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3847 | int iLoopBody = sqlite3VdbeMakeLabel(v); /* Start of loop body */ |
| 3848 | int iRetInit; /* Address of regReturn init */ |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3849 | int untestedTerms = 0; /* Some terms not completely tested */ |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 3850 | int ii; /* Loop counter */ |
drh | 3526319 | 2014-07-22 20:02:19 +0000 | [diff] [blame] | 3851 | u16 wctrlFlags; /* Flags for sub-WHERE clause */ |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 3852 | Expr *pAndExpr = 0; /* An ".. AND (...)" expression */ |
dan | f97dad8 | 2014-05-26 20:06:45 +0000 | [diff] [blame] | 3853 | Table *pTab = pTabItem->pTab; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3854 | |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 3855 | pTerm = pLoop->aLTerm[0]; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3856 | assert( pTerm!=0 ); |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 3857 | assert( pTerm->eOperator & WO_OR ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 3858 | assert( (pTerm->wtFlags & TERM_ORINFO)!=0 ); |
| 3859 | pOrWc = &pTerm->u.pOrInfo->wc; |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3860 | pLevel->op = OP_Return; |
| 3861 | pLevel->p1 = regReturn; |
drh | 23d04d5 | 2008-12-23 23:56:22 +0000 | [diff] [blame] | 3862 | |
dan | bfca6a4 | 2012-08-24 10:52:35 +0000 | [diff] [blame] | 3863 | /* Set up a new SrcList in pOrTab containing the table being scanned |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3864 | ** by this loop in the a[0] slot and all notReady tables in a[1..] slots. |
| 3865 | ** This becomes the SrcList in the recursive call to sqlite3WhereBegin(). |
| 3866 | */ |
| 3867 | if( pWInfo->nLevel>1 ){ |
| 3868 | int nNotReady; /* The number of notReady tables */ |
| 3869 | struct SrcList_item *origSrc; /* Original list of tables */ |
| 3870 | nNotReady = pWInfo->nLevel - iLevel - 1; |
drh | 6b36e82 | 2013-07-30 15:10:32 +0000 | [diff] [blame] | 3871 | pOrTab = sqlite3StackAllocRaw(db, |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3872 | sizeof(*pOrTab)+ nNotReady*sizeof(pOrTab->a[0])); |
| 3873 | if( pOrTab==0 ) return notReady; |
drh | ad01d89 | 2013-06-19 13:59:49 +0000 | [diff] [blame] | 3874 | pOrTab->nAlloc = (u8)(nNotReady + 1); |
shaneh | 46aae3c | 2009-12-31 19:06:23 +0000 | [diff] [blame] | 3875 | pOrTab->nSrc = pOrTab->nAlloc; |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 3876 | memcpy(pOrTab->a, pTabItem, sizeof(*pTabItem)); |
| 3877 | origSrc = pWInfo->pTabList->a; |
| 3878 | for(k=1; k<=nNotReady; k++){ |
| 3879 | memcpy(&pOrTab->a[k], &origSrc[pLevel[k].iFrom], sizeof(pOrTab->a[k])); |
| 3880 | } |
| 3881 | }else{ |
| 3882 | pOrTab = pWInfo->pTabList; |
| 3883 | } |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3884 | |
drh | 1b26c7c | 2009-04-22 02:15:47 +0000 | [diff] [blame] | 3885 | /* Initialize the rowset register to contain NULL. An SQL NULL is |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 3886 | ** equivalent to an empty rowset. Or, create an ephemeral index |
drh | 5609baf | 2014-05-26 22:01:00 +0000 | [diff] [blame] | 3887 | ** capable of holding primary keys in the case of a WITHOUT ROWID. |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3888 | ** |
| 3889 | ** Also initialize regReturn to contain the address of the instruction |
| 3890 | ** immediately following the OP_Return at the bottom of the loop. This |
| 3891 | ** is required in a few obscure LEFT JOIN cases where control jumps |
| 3892 | ** over the top of the loop into the body of it. In this case the |
| 3893 | ** correct response for the end-of-loop code (the OP_Return) is to |
| 3894 | ** fall through to the next instruction, just as an OP_Next does if |
| 3895 | ** called on an uninitialized cursor. |
| 3896 | */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 3897 | if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){ |
dan | f97dad8 | 2014-05-26 20:06:45 +0000 | [diff] [blame] | 3898 | if( HasRowid(pTab) ){ |
| 3899 | regRowset = ++pParse->nMem; |
| 3900 | sqlite3VdbeAddOp2(v, OP_Null, 0, regRowset); |
| 3901 | }else{ |
| 3902 | Index *pPk = sqlite3PrimaryKeyIndex(pTab); |
| 3903 | regRowset = pParse->nTab++; |
| 3904 | sqlite3VdbeAddOp2(v, OP_OpenEphemeral, regRowset, pPk->nKeyCol); |
| 3905 | sqlite3VdbeSetP4KeyInfo(pParse, pPk); |
| 3906 | } |
drh | 336a530 | 2009-04-24 15:46:21 +0000 | [diff] [blame] | 3907 | regRowid = ++pParse->nMem; |
drh | 336a530 | 2009-04-24 15:46:21 +0000 | [diff] [blame] | 3908 | } |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3909 | iRetInit = sqlite3VdbeAddOp2(v, OP_Integer, 0, regReturn); |
| 3910 | |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 3911 | /* If the original WHERE clause is z of the form: (x1 OR x2 OR ...) AND y |
| 3912 | ** Then for every term xN, evaluate as the subexpression: xN AND z |
| 3913 | ** That way, terms in y that are factored into the disjunction will |
| 3914 | ** be picked up by the recursive calls to sqlite3WhereBegin() below. |
drh | 331b67c | 2012-03-09 22:02:08 +0000 | [diff] [blame] | 3915 | ** |
| 3916 | ** Actually, each subexpression is converted to "xN AND w" where w is |
| 3917 | ** the "interesting" terms of z - terms that did not originate in the |
| 3918 | ** ON or USING clause of a LEFT JOIN, and terms that are usable as |
| 3919 | ** indices. |
drh | b3129fa | 2013-05-09 14:20:11 +0000 | [diff] [blame] | 3920 | ** |
| 3921 | ** This optimization also only applies if the (x1 OR x2 OR ...) term |
| 3922 | ** is not contained in the ON clause of a LEFT JOIN. |
| 3923 | ** See ticket http://www.sqlite.org/src/info/f2369304e4 |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 3924 | */ |
| 3925 | if( pWC->nTerm>1 ){ |
drh | 7a48480 | 2012-03-16 00:28:11 +0000 | [diff] [blame] | 3926 | int iTerm; |
| 3927 | for(iTerm=0; iTerm<pWC->nTerm; iTerm++){ |
| 3928 | Expr *pExpr = pWC->a[iTerm].pExpr; |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 3929 | if( &pWC->a[iTerm] == pTerm ) continue; |
drh | 331b67c | 2012-03-09 22:02:08 +0000 | [diff] [blame] | 3930 | if( ExprHasProperty(pExpr, EP_FromJoin) ) continue; |
drh | 1d32488 | 2014-12-04 20:24:50 +0000 | [diff] [blame] | 3931 | if( (pWC->a[iTerm].wtFlags & TERM_VIRTUAL)!=0 ) continue; |
drh | 7a48480 | 2012-03-16 00:28:11 +0000 | [diff] [blame] | 3932 | if( (pWC->a[iTerm].eOperator & WO_ALL)==0 ) continue; |
drh | 1d32488 | 2014-12-04 20:24:50 +0000 | [diff] [blame] | 3933 | testcase( pWC->a[iTerm].wtFlags & TERM_ORINFO ); |
drh | 6b36e82 | 2013-07-30 15:10:32 +0000 | [diff] [blame] | 3934 | pExpr = sqlite3ExprDup(db, pExpr, 0); |
| 3935 | pAndExpr = sqlite3ExprAnd(db, pAndExpr, pExpr); |
drh | 331b67c | 2012-03-09 22:02:08 +0000 | [diff] [blame] | 3936 | } |
| 3937 | if( pAndExpr ){ |
| 3938 | pAndExpr = sqlite3PExpr(pParse, TK_AND, 0, pAndExpr, 0); |
| 3939 | } |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 3940 | } |
| 3941 | |
drh | 3fb6730 | 2014-05-27 16:41:39 +0000 | [diff] [blame] | 3942 | /* Run a separate WHERE clause for each term of the OR clause. After |
| 3943 | ** eliminating duplicates from other WHERE clauses, the action for each |
| 3944 | ** sub-WHERE clause is to to invoke the main loop body as a subroutine. |
| 3945 | */ |
drh | 36be4c4 | 2014-09-30 17:31:23 +0000 | [diff] [blame] | 3946 | wctrlFlags = WHERE_OMIT_OPEN_CLOSE |
| 3947 | | WHERE_FORCE_TABLE |
drh | 8e8e7ef | 2015-03-02 17:25:00 +0000 | [diff] [blame] | 3948 | | WHERE_ONETABLE_ONLY |
| 3949 | | WHERE_NO_AUTOINDEX; |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3950 | for(ii=0; ii<pOrWc->nTerm; ii++){ |
| 3951 | WhereTerm *pOrTerm = &pOrWc->a[ii]; |
drh | 7a5bcc0 | 2013-01-16 17:08:58 +0000 | [diff] [blame] | 3952 | if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND)!=0 ){ |
drh | 3fb6730 | 2014-05-27 16:41:39 +0000 | [diff] [blame] | 3953 | WhereInfo *pSubWInfo; /* Info for single OR-term scan */ |
| 3954 | Expr *pOrExpr = pOrTerm->pExpr; /* Current OR clause term */ |
| 3955 | int j1 = 0; /* Address of jump operation */ |
drh | b3129fa | 2013-05-09 14:20:11 +0000 | [diff] [blame] | 3956 | if( pAndExpr && !ExprHasProperty(pOrExpr, EP_FromJoin) ){ |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 3957 | pAndExpr->pLeft = pOrExpr; |
| 3958 | pOrExpr = pAndExpr; |
| 3959 | } |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3960 | /* Loop through table entries that match term pOrTerm. */ |
drh | 0a99ba3 | 2014-09-30 17:03:35 +0000 | [diff] [blame] | 3961 | WHERETRACE(0xffff, ("Subplan for OR-clause:\n")); |
drh | 8871ef5 | 2011-10-07 13:33:10 +0000 | [diff] [blame] | 3962 | pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0, |
drh | 3526319 | 2014-07-22 20:02:19 +0000 | [diff] [blame] | 3963 | wctrlFlags, iCovCur); |
drh | 6b36e82 | 2013-07-30 15:10:32 +0000 | [diff] [blame] | 3964 | assert( pSubWInfo || pParse->nErr || db->mallocFailed ); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 3965 | if( pSubWInfo ){ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 3966 | WhereLoop *pSubLoop; |
dan | 6f9702e | 2014-11-01 20:38:06 +0000 | [diff] [blame] | 3967 | int addrExplain = explainOneScan( |
| 3968 | pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0 |
| 3969 | ); |
| 3970 | addScanStatus(v, pOrTab, &pSubWInfo->a[0], addrExplain); |
dan | 89e7164 | 2014-11-01 18:08:04 +0000 | [diff] [blame] | 3971 | |
drh | 3fb6730 | 2014-05-27 16:41:39 +0000 | [diff] [blame] | 3972 | /* This is the sub-WHERE clause body. First skip over |
| 3973 | ** duplicate rows from prior sub-WHERE clauses, and record the |
| 3974 | ** rowid (or PRIMARY KEY) for the current row so that the same |
| 3975 | ** row will be skipped in subsequent sub-WHERE clauses. |
| 3976 | */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 3977 | if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){ |
drh | 336a530 | 2009-04-24 15:46:21 +0000 | [diff] [blame] | 3978 | int r; |
dan | f97dad8 | 2014-05-26 20:06:45 +0000 | [diff] [blame] | 3979 | int iSet = ((ii==pOrWc->nTerm-1)?-1:ii); |
| 3980 | if( HasRowid(pTab) ){ |
| 3981 | r = sqlite3ExprCodeGetColumn(pParse, pTab, -1, iCur, regRowid, 0); |
drh | 5609baf | 2014-05-26 22:01:00 +0000 | [diff] [blame] | 3982 | j1 = sqlite3VdbeAddOp4Int(v, OP_RowSetTest, regRowset, 0, r,iSet); |
dan | f97dad8 | 2014-05-26 20:06:45 +0000 | [diff] [blame] | 3983 | VdbeCoverage(v); |
| 3984 | }else{ |
| 3985 | Index *pPk = sqlite3PrimaryKeyIndex(pTab); |
| 3986 | int nPk = pPk->nKeyCol; |
| 3987 | int iPk; |
| 3988 | |
| 3989 | /* Read the PK into an array of temp registers. */ |
| 3990 | r = sqlite3GetTempRange(pParse, nPk); |
| 3991 | for(iPk=0; iPk<nPk; iPk++){ |
| 3992 | int iCol = pPk->aiColumn[iPk]; |
| 3993 | sqlite3ExprCodeGetColumn(pParse, pTab, iCol, iCur, r+iPk, 0); |
| 3994 | } |
| 3995 | |
| 3996 | /* Check if the temp table already contains this key. If so, |
| 3997 | ** the row has already been included in the result set and |
| 3998 | ** can be ignored (by jumping past the Gosub below). Otherwise, |
| 3999 | ** insert the key into the temp table and proceed with processing |
| 4000 | ** the row. |
| 4001 | ** |
| 4002 | ** Use some of the same optimizations as OP_RowSetTest: If iSet |
| 4003 | ** is zero, assume that the key cannot already be present in |
| 4004 | ** the temp table. And if iSet is -1, assume that there is no |
| 4005 | ** need to insert the key into the temp table, as it will never |
| 4006 | ** be tested for. */ |
| 4007 | if( iSet ){ |
drh | 5609baf | 2014-05-26 22:01:00 +0000 | [diff] [blame] | 4008 | j1 = sqlite3VdbeAddOp4Int(v, OP_Found, regRowset, 0, r, nPk); |
drh | 68c1215 | 2014-05-26 20:25:34 +0000 | [diff] [blame] | 4009 | VdbeCoverage(v); |
dan | f97dad8 | 2014-05-26 20:06:45 +0000 | [diff] [blame] | 4010 | } |
| 4011 | if( iSet>=0 ){ |
| 4012 | sqlite3VdbeAddOp3(v, OP_MakeRecord, r, nPk, regRowid); |
| 4013 | sqlite3VdbeAddOp3(v, OP_IdxInsert, regRowset, regRowid, 0); |
| 4014 | if( iSet ) sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT); |
| 4015 | } |
| 4016 | |
| 4017 | /* Release the array of temp registers */ |
| 4018 | sqlite3ReleaseTempRange(pParse, r, nPk); |
| 4019 | } |
drh | 336a530 | 2009-04-24 15:46:21 +0000 | [diff] [blame] | 4020 | } |
drh | 3fb6730 | 2014-05-27 16:41:39 +0000 | [diff] [blame] | 4021 | |
| 4022 | /* Invoke the main loop body as a subroutine */ |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 4023 | sqlite3VdbeAddOp2(v, OP_Gosub, regReturn, iLoopBody); |
drh | 3fb6730 | 2014-05-27 16:41:39 +0000 | [diff] [blame] | 4024 | |
| 4025 | /* Jump here (skipping the main loop body subroutine) if the |
| 4026 | ** current sub-WHERE row is a duplicate from prior sub-WHEREs. */ |
drh | 5609baf | 2014-05-26 22:01:00 +0000 | [diff] [blame] | 4027 | if( j1 ) sqlite3VdbeJumpHere(v, j1); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 4028 | |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 4029 | /* The pSubWInfo->untestedTerms flag means that this OR term |
| 4030 | ** contained one or more AND term from a notReady table. The |
| 4031 | ** terms from the notReady table could not be tested and will |
| 4032 | ** need to be tested later. |
| 4033 | */ |
| 4034 | if( pSubWInfo->untestedTerms ) untestedTerms = 1; |
| 4035 | |
dan | bfca6a4 | 2012-08-24 10:52:35 +0000 | [diff] [blame] | 4036 | /* If all of the OR-connected terms are optimized using the same |
| 4037 | ** index, and the index is opened using the same cursor number |
| 4038 | ** by each call to sqlite3WhereBegin() made by this loop, it may |
| 4039 | ** be possible to use that index as a covering index. |
| 4040 | ** |
| 4041 | ** If the call to sqlite3WhereBegin() above resulted in a scan that |
| 4042 | ** uses an index, and this is either the first OR-connected term |
| 4043 | ** processed or the index is the same as that used by all previous |
dan | 0efb72c | 2012-08-24 18:44:56 +0000 | [diff] [blame] | 4044 | ** terms, set pCov to the candidate covering index. Otherwise, set |
| 4045 | ** pCov to NULL to indicate that no candidate covering index will |
| 4046 | ** be available. |
dan | bfca6a4 | 2012-08-24 10:52:35 +0000 | [diff] [blame] | 4047 | */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 4048 | pSubLoop = pSubWInfo->a[0].pWLoop; |
drh | 986b387 | 2013-06-28 21:12:20 +0000 | [diff] [blame] | 4049 | assert( (pSubLoop->wsFlags & WHERE_AUTO_INDEX)==0 ); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 4050 | if( (pSubLoop->wsFlags & WHERE_INDEXED)!=0 |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 4051 | && (ii==0 || pSubLoop->u.btree.pIndex==pCov) |
drh | 48dd1d8 | 2014-05-27 18:18:58 +0000 | [diff] [blame] | 4052 | && (HasRowid(pTab) || !IsPrimaryKeyIndex(pSubLoop->u.btree.pIndex)) |
dan | bfca6a4 | 2012-08-24 10:52:35 +0000 | [diff] [blame] | 4053 | ){ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 4054 | assert( pSubWInfo->a[0].iIdxCur==iCovCur ); |
drh | 907717f | 2013-06-04 18:03:22 +0000 | [diff] [blame] | 4055 | pCov = pSubLoop->u.btree.pIndex; |
drh | 3526319 | 2014-07-22 20:02:19 +0000 | [diff] [blame] | 4056 | wctrlFlags |= WHERE_REOPEN_IDX; |
dan | bfca6a4 | 2012-08-24 10:52:35 +0000 | [diff] [blame] | 4057 | }else{ |
| 4058 | pCov = 0; |
| 4059 | } |
| 4060 | |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 4061 | /* Finish the loop through table entries that match term pOrTerm. */ |
| 4062 | sqlite3WhereEnd(pSubWInfo); |
| 4063 | } |
drh | dd5f5a6 | 2008-12-23 13:35:23 +0000 | [diff] [blame] | 4064 | } |
| 4065 | } |
drh | d40e208 | 2012-08-24 23:24:15 +0000 | [diff] [blame] | 4066 | pLevel->u.pCovidx = pCov; |
drh | 90abfd0 | 2012-10-09 21:07:23 +0000 | [diff] [blame] | 4067 | if( pCov ) pLevel->iIdxCur = iCovCur; |
drh | 331b67c | 2012-03-09 22:02:08 +0000 | [diff] [blame] | 4068 | if( pAndExpr ){ |
| 4069 | pAndExpr->pLeft = 0; |
drh | 6b36e82 | 2013-07-30 15:10:32 +0000 | [diff] [blame] | 4070 | sqlite3ExprDelete(db, pAndExpr); |
drh | 331b67c | 2012-03-09 22:02:08 +0000 | [diff] [blame] | 4071 | } |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 4072 | sqlite3VdbeChangeP1(v, iRetInit, sqlite3VdbeCurrentAddr(v)); |
danielk1977 | 1d46146 | 2009-04-21 09:02:45 +0000 | [diff] [blame] | 4073 | sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrBrk); |
| 4074 | sqlite3VdbeResolveLabel(v, iLoopBody); |
| 4075 | |
drh | 6b36e82 | 2013-07-30 15:10:32 +0000 | [diff] [blame] | 4076 | if( pWInfo->nLevel>1 ) sqlite3StackFree(db, pOrTab); |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 4077 | if( !untestedTerms ) disableTerm(pLevel, pTerm); |
drh | dd5f5a6 | 2008-12-23 13:35:23 +0000 | [diff] [blame] | 4078 | }else |
drh | 23d04d5 | 2008-12-23 23:56:22 +0000 | [diff] [blame] | 4079 | #endif /* SQLITE_OMIT_OR_OPTIMIZATION */ |
drh | dd5f5a6 | 2008-12-23 13:35:23 +0000 | [diff] [blame] | 4080 | |
| 4081 | { |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 4082 | /* Case 6: There is no usable index. We must do a complete |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 4083 | ** scan of the entire table. |
| 4084 | */ |
drh | 699b3d4 | 2009-02-23 16:52:07 +0000 | [diff] [blame] | 4085 | static const u8 aStep[] = { OP_Next, OP_Prev }; |
| 4086 | static const u8 aStart[] = { OP_Rewind, OP_Last }; |
| 4087 | assert( bRev==0 || bRev==1 ); |
drh | e73f059 | 2014-01-21 22:25:45 +0000 | [diff] [blame] | 4088 | if( pTabItem->isRecursive ){ |
drh | 340309f | 2014-01-22 00:23:49 +0000 | [diff] [blame] | 4089 | /* Tables marked isRecursive have only a single row that is stored in |
dan | 4102815 | 2014-01-22 10:22:25 +0000 | [diff] [blame] | 4090 | ** a pseudo-cursor. No need to Rewind or Next such cursors. */ |
drh | e73f059 | 2014-01-21 22:25:45 +0000 | [diff] [blame] | 4091 | pLevel->op = OP_Noop; |
| 4092 | }else{ |
| 4093 | pLevel->op = aStep[bRev]; |
| 4094 | pLevel->p1 = iCur; |
| 4095 | pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrBrk); |
drh | 7d17610 | 2014-02-18 03:07:12 +0000 | [diff] [blame] | 4096 | VdbeCoverageIf(v, bRev==0); |
| 4097 | VdbeCoverageIf(v, bRev!=0); |
drh | e73f059 | 2014-01-21 22:25:45 +0000 | [diff] [blame] | 4098 | pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP; |
| 4099 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 4100 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 4101 | |
dan | 6f9702e | 2014-11-01 20:38:06 +0000 | [diff] [blame] | 4102 | #ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
| 4103 | pLevel->addrVisit = sqlite3VdbeCurrentAddr(v); |
| 4104 | #endif |
| 4105 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 4106 | /* Insert code to test every subexpression that can be completely |
| 4107 | ** computed using the current set of tables. |
| 4108 | */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 4109 | for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){ |
| 4110 | Expr *pE; |
drh | 8f1a7ed | 2015-03-06 19:47:38 +0000 | [diff] [blame] | 4111 | int skipLikeAddr = 0; |
drh | 3975974 | 2013-08-02 23:40:45 +0000 | [diff] [blame] | 4112 | testcase( pTerm->wtFlags & TERM_VIRTUAL ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 4113 | testcase( pTerm->wtFlags & TERM_CODED ); |
| 4114 | if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue; |
drh | 0259bc3 | 2013-09-09 19:37:46 +0000 | [diff] [blame] | 4115 | if( (pTerm->prereqAll & pLevel->notReady)!=0 ){ |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 4116 | testcase( pWInfo->untestedTerms==0 |
| 4117 | && (pWInfo->wctrlFlags & WHERE_ONETABLE_ONLY)!=0 ); |
| 4118 | pWInfo->untestedTerms = 1; |
| 4119 | continue; |
| 4120 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 4121 | pE = pTerm->pExpr; |
| 4122 | assert( pE!=0 ); |
| 4123 | if( pLevel->iLeftJoin && !ExprHasProperty(pE, EP_FromJoin) ){ |
| 4124 | continue; |
| 4125 | } |
drh | 8f1a7ed | 2015-03-06 19:47:38 +0000 | [diff] [blame] | 4126 | if( pTerm->wtFlags & TERM_LIKECOND ){ |
| 4127 | assert( pLevel->iLikeRepCntr>0 ); |
drh | 1689707 | 2015-03-07 00:57:37 +0000 | [diff] [blame] | 4128 | skipLikeAddr = sqlite3VdbeAddOp1(v, OP_IfNot, pLevel->iLikeRepCntr); |
drh | 8f1a7ed | 2015-03-06 19:47:38 +0000 | [diff] [blame] | 4129 | VdbeCoverage(v); |
| 4130 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 4131 | sqlite3ExprIfFalse(pParse, pE, addrCont, SQLITE_JUMPIFNULL); |
drh | 8f1a7ed | 2015-03-06 19:47:38 +0000 | [diff] [blame] | 4132 | if( skipLikeAddr ) sqlite3VdbeJumpHere(v, skipLikeAddr); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 4133 | pTerm->wtFlags |= TERM_CODED; |
| 4134 | } |
| 4135 | |
drh | 0c41d22 | 2013-04-22 02:39:10 +0000 | [diff] [blame] | 4136 | /* Insert code to test for implied constraints based on transitivity |
| 4137 | ** of the "==" operator. |
| 4138 | ** |
| 4139 | ** Example: If the WHERE clause contains "t1.a=t2.b" and "t2.b=123" |
| 4140 | ** and we are coding the t1 loop and the t2 loop has not yet coded, |
| 4141 | ** then we cannot use the "t1.a=t2.b" constraint, but we can code |
| 4142 | ** the implied "t1.a=123" constraint. |
| 4143 | */ |
| 4144 | for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){ |
drh | 6b36e82 | 2013-07-30 15:10:32 +0000 | [diff] [blame] | 4145 | Expr *pE, *pEAlt; |
drh | 0c41d22 | 2013-04-22 02:39:10 +0000 | [diff] [blame] | 4146 | WhereTerm *pAlt; |
drh | 0c41d22 | 2013-04-22 02:39:10 +0000 | [diff] [blame] | 4147 | if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue; |
drh | ee14587 | 2015-05-14 13:18:47 +0000 | [diff] [blame] | 4148 | if( (pTerm->eOperator & (WO_EQ|WO_IS))==0 ) continue; |
drh | 4a00b33 | 2015-05-14 13:41:22 +0000 | [diff] [blame] | 4149 | if( (pTerm->eOperator & WO_EQUIV)==0 ) continue; |
drh | 0c41d22 | 2013-04-22 02:39:10 +0000 | [diff] [blame] | 4150 | if( pTerm->leftCursor!=iCur ) continue; |
drh | cdc2e43 | 2013-07-01 17:27:19 +0000 | [diff] [blame] | 4151 | if( pLevel->iLeftJoin ) continue; |
drh | 0c41d22 | 2013-04-22 02:39:10 +0000 | [diff] [blame] | 4152 | pE = pTerm->pExpr; |
| 4153 | assert( !ExprHasProperty(pE, EP_FromJoin) ); |
drh | 0259bc3 | 2013-09-09 19:37:46 +0000 | [diff] [blame] | 4154 | assert( (pTerm->prereqRight & pLevel->notReady)!=0 ); |
drh | e8d0c61 | 2015-05-14 01:05:25 +0000 | [diff] [blame] | 4155 | pAlt = findTerm(pWC, iCur, pTerm->u.leftColumn, notReady, |
| 4156 | WO_EQ|WO_IN|WO_IS, 0); |
drh | 0c41d22 | 2013-04-22 02:39:10 +0000 | [diff] [blame] | 4157 | if( pAlt==0 ) continue; |
drh | 5c10f3b | 2013-05-01 17:22:38 +0000 | [diff] [blame] | 4158 | if( pAlt->wtFlags & (TERM_CODED) ) continue; |
drh | e8d0c61 | 2015-05-14 01:05:25 +0000 | [diff] [blame] | 4159 | testcase( pAlt->eOperator & WO_EQ ); |
| 4160 | testcase( pAlt->eOperator & WO_IS ); |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 4161 | testcase( pAlt->eOperator & WO_IN ); |
drh | 6bc69a2 | 2013-11-19 12:33:23 +0000 | [diff] [blame] | 4162 | VdbeModuleComment((v, "begin transitive constraint")); |
drh | 6b36e82 | 2013-07-30 15:10:32 +0000 | [diff] [blame] | 4163 | pEAlt = sqlite3StackAllocRaw(db, sizeof(*pEAlt)); |
| 4164 | if( pEAlt ){ |
| 4165 | *pEAlt = *pAlt->pExpr; |
| 4166 | pEAlt->pLeft = pE->pLeft; |
| 4167 | sqlite3ExprIfFalse(pParse, pEAlt, addrCont, SQLITE_JUMPIFNULL); |
| 4168 | sqlite3StackFree(db, pEAlt); |
| 4169 | } |
drh | 0c41d22 | 2013-04-22 02:39:10 +0000 | [diff] [blame] | 4170 | } |
| 4171 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 4172 | /* For a LEFT OUTER JOIN, generate code that will record the fact that |
| 4173 | ** at least one row of the right table has matched the left table. |
| 4174 | */ |
| 4175 | if( pLevel->iLeftJoin ){ |
| 4176 | pLevel->addrFirst = sqlite3VdbeCurrentAddr(v); |
| 4177 | sqlite3VdbeAddOp2(v, OP_Integer, 1, pLevel->iLeftJoin); |
| 4178 | VdbeComment((v, "record LEFT JOIN hit")); |
drh | ceea332 | 2009-04-23 13:22:42 +0000 | [diff] [blame] | 4179 | sqlite3ExprCacheClear(pParse); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 4180 | for(pTerm=pWC->a, j=0; j<pWC->nTerm; j++, pTerm++){ |
drh | 3975974 | 2013-08-02 23:40:45 +0000 | [diff] [blame] | 4181 | testcase( pTerm->wtFlags & TERM_VIRTUAL ); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 4182 | testcase( pTerm->wtFlags & TERM_CODED ); |
| 4183 | if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue; |
drh | 0259bc3 | 2013-09-09 19:37:46 +0000 | [diff] [blame] | 4184 | if( (pTerm->prereqAll & pLevel->notReady)!=0 ){ |
drh | b057e56 | 2009-12-16 23:43:55 +0000 | [diff] [blame] | 4185 | assert( pWInfo->untestedTerms ); |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 4186 | continue; |
| 4187 | } |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 4188 | assert( pTerm->pExpr ); |
| 4189 | sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL); |
| 4190 | pTerm->wtFlags |= TERM_CODED; |
| 4191 | } |
| 4192 | } |
drh | 23d04d5 | 2008-12-23 23:56:22 +0000 | [diff] [blame] | 4193 | |
drh | 0259bc3 | 2013-09-09 19:37:46 +0000 | [diff] [blame] | 4194 | return pLevel->notReady; |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 4195 | } |
| 4196 | |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 4197 | #ifdef WHERETRACE_ENABLED |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4198 | /* |
drh | c90713d | 2014-09-30 13:46:49 +0000 | [diff] [blame] | 4199 | ** Print the content of a WhereTerm object |
| 4200 | */ |
| 4201 | static void whereTermPrint(WhereTerm *pTerm, int iTerm){ |
drh | 0a99ba3 | 2014-09-30 17:03:35 +0000 | [diff] [blame] | 4202 | if( pTerm==0 ){ |
| 4203 | sqlite3DebugPrintf("TERM-%-3d NULL\n", iTerm); |
| 4204 | }else{ |
| 4205 | char zType[4]; |
| 4206 | memcpy(zType, "...", 4); |
| 4207 | if( pTerm->wtFlags & TERM_VIRTUAL ) zType[0] = 'V'; |
| 4208 | if( pTerm->eOperator & WO_EQUIV ) zType[1] = 'E'; |
| 4209 | if( ExprHasProperty(pTerm->pExpr, EP_FromJoin) ) zType[2] = 'L'; |
drh | fcd4953 | 2015-05-13 15:24:07 +0000 | [diff] [blame] | 4210 | sqlite3DebugPrintf( |
| 4211 | "TERM-%-3d %p %s cursor=%-3d prob=%-3d op=0x%03x wtFlags=0x%04x\n", |
| 4212 | iTerm, pTerm, zType, pTerm->leftCursor, pTerm->truthProb, |
| 4213 | pTerm->eOperator, pTerm->wtFlags); |
drh | 0a99ba3 | 2014-09-30 17:03:35 +0000 | [diff] [blame] | 4214 | sqlite3TreeViewExpr(0, pTerm->pExpr, 0); |
| 4215 | } |
drh | c90713d | 2014-09-30 13:46:49 +0000 | [diff] [blame] | 4216 | } |
| 4217 | #endif |
| 4218 | |
| 4219 | #ifdef WHERETRACE_ENABLED |
| 4220 | /* |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4221 | ** Print a WhereLoop object for debugging purposes |
| 4222 | */ |
drh | c1ba2e7 | 2013-10-28 19:03:21 +0000 | [diff] [blame] | 4223 | static void whereLoopPrint(WhereLoop *p, WhereClause *pWC){ |
| 4224 | WhereInfo *pWInfo = pWC->pWInfo; |
drh | 989578e | 2013-10-28 14:34:35 +0000 | [diff] [blame] | 4225 | int nb = 1+(pWInfo->pTabList->nSrc+7)/8; |
| 4226 | struct SrcList_item *pItem = pWInfo->pTabList->a + p->iTab; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4227 | Table *pTab = pItem->pTab; |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 4228 | sqlite3DebugPrintf("%c%2d.%0*llx.%0*llx", p->cId, |
drh | a184fb8 | 2013-05-08 04:22:59 +0000 | [diff] [blame] | 4229 | p->iTab, nb, p->maskSelf, nb, p->prereq); |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 4230 | sqlite3DebugPrintf(" %12s", |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4231 | pItem->zAlias ? pItem->zAlias : pTab->zName); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4232 | if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){ |
drh | f3f69ac | 2014-08-20 23:38:07 +0000 | [diff] [blame] | 4233 | const char *zName; |
| 4234 | if( p->u.btree.pIndex && (zName = p->u.btree.pIndex->zName)!=0 ){ |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 4235 | if( strncmp(zName, "sqlite_autoindex_", 17)==0 ){ |
| 4236 | int i = sqlite3Strlen30(zName) - 1; |
| 4237 | while( zName[i]!='_' ) i--; |
| 4238 | zName += i; |
| 4239 | } |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 4240 | sqlite3DebugPrintf(".%-16s %2d", zName, p->u.btree.nEq); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4241 | }else{ |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 4242 | sqlite3DebugPrintf("%20s",""); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4243 | } |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4244 | }else{ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4245 | char *z; |
| 4246 | if( p->u.vtab.idxStr ){ |
drh | 3bd26f0 | 2013-05-24 14:52:03 +0000 | [diff] [blame] | 4247 | z = sqlite3_mprintf("(%d,\"%s\",%x)", |
| 4248 | p->u.vtab.idxNum, p->u.vtab.idxStr, p->u.vtab.omitMask); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4249 | }else{ |
drh | 3bd26f0 | 2013-05-24 14:52:03 +0000 | [diff] [blame] | 4250 | z = sqlite3_mprintf("(%d,%x)", p->u.vtab.idxNum, p->u.vtab.omitMask); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4251 | } |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 4252 | sqlite3DebugPrintf(" %-19s", z); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4253 | sqlite3_free(z); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4254 | } |
drh | f3f69ac | 2014-08-20 23:38:07 +0000 | [diff] [blame] | 4255 | if( p->wsFlags & WHERE_SKIPSCAN ){ |
drh | c8bbce1 | 2014-10-21 01:05:09 +0000 | [diff] [blame] | 4256 | sqlite3DebugPrintf(" f %05x %d-%d", p->wsFlags, p->nLTerm,p->nSkip); |
drh | f3f69ac | 2014-08-20 23:38:07 +0000 | [diff] [blame] | 4257 | }else{ |
| 4258 | sqlite3DebugPrintf(" f %05x N %d", p->wsFlags, p->nLTerm); |
| 4259 | } |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4260 | sqlite3DebugPrintf(" cost %d,%d,%d\n", p->rSetup, p->rRun, p->nOut); |
drh | c90713d | 2014-09-30 13:46:49 +0000 | [diff] [blame] | 4261 | if( p->nLTerm && (sqlite3WhereTrace & 0x100)!=0 ){ |
| 4262 | int i; |
| 4263 | for(i=0; i<p->nLTerm; i++){ |
drh | 0a99ba3 | 2014-09-30 17:03:35 +0000 | [diff] [blame] | 4264 | whereTermPrint(p->aLTerm[i], i); |
drh | c90713d | 2014-09-30 13:46:49 +0000 | [diff] [blame] | 4265 | } |
| 4266 | } |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4267 | } |
| 4268 | #endif |
| 4269 | |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4270 | /* |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4271 | ** Convert bulk memory into a valid WhereLoop that can be passed |
| 4272 | ** to whereLoopClear harmlessly. |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4273 | */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4274 | static void whereLoopInit(WhereLoop *p){ |
| 4275 | p->aLTerm = p->aLTermSpace; |
| 4276 | p->nLTerm = 0; |
| 4277 | p->nLSlot = ArraySize(p->aLTermSpace); |
| 4278 | p->wsFlags = 0; |
| 4279 | } |
| 4280 | |
| 4281 | /* |
| 4282 | ** Clear the WhereLoop.u union. Leave WhereLoop.pLTerm intact. |
| 4283 | */ |
| 4284 | static void whereLoopClearUnion(sqlite3 *db, WhereLoop *p){ |
drh | 986b387 | 2013-06-28 21:12:20 +0000 | [diff] [blame] | 4285 | if( p->wsFlags & (WHERE_VIRTUALTABLE|WHERE_AUTO_INDEX) ){ |
drh | 13e11b4 | 2013-06-06 23:44:25 +0000 | [diff] [blame] | 4286 | if( (p->wsFlags & WHERE_VIRTUALTABLE)!=0 && p->u.vtab.needFree ){ |
| 4287 | sqlite3_free(p->u.vtab.idxStr); |
| 4288 | p->u.vtab.needFree = 0; |
| 4289 | p->u.vtab.idxStr = 0; |
drh | 986b387 | 2013-06-28 21:12:20 +0000 | [diff] [blame] | 4290 | }else if( (p->wsFlags & WHERE_AUTO_INDEX)!=0 && p->u.btree.pIndex!=0 ){ |
drh | 13e11b4 | 2013-06-06 23:44:25 +0000 | [diff] [blame] | 4291 | sqlite3DbFree(db, p->u.btree.pIndex->zColAff); |
| 4292 | sqlite3DbFree(db, p->u.btree.pIndex); |
| 4293 | p->u.btree.pIndex = 0; |
| 4294 | } |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4295 | } |
| 4296 | } |
| 4297 | |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4298 | /* |
| 4299 | ** Deallocate internal memory used by a WhereLoop object |
| 4300 | */ |
| 4301 | static void whereLoopClear(sqlite3 *db, WhereLoop *p){ |
| 4302 | if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFree(db, p->aLTerm); |
| 4303 | whereLoopClearUnion(db, p); |
| 4304 | whereLoopInit(p); |
| 4305 | } |
| 4306 | |
| 4307 | /* |
| 4308 | ** Increase the memory allocation for pLoop->aLTerm[] to be at least n. |
| 4309 | */ |
| 4310 | static int whereLoopResize(sqlite3 *db, WhereLoop *p, int n){ |
| 4311 | WhereTerm **paNew; |
| 4312 | if( p->nLSlot>=n ) return SQLITE_OK; |
| 4313 | n = (n+7)&~7; |
| 4314 | paNew = sqlite3DbMallocRaw(db, sizeof(p->aLTerm[0])*n); |
| 4315 | if( paNew==0 ) return SQLITE_NOMEM; |
| 4316 | memcpy(paNew, p->aLTerm, sizeof(p->aLTerm[0])*p->nLSlot); |
| 4317 | if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFree(db, p->aLTerm); |
| 4318 | p->aLTerm = paNew; |
| 4319 | p->nLSlot = n; |
| 4320 | return SQLITE_OK; |
| 4321 | } |
| 4322 | |
| 4323 | /* |
| 4324 | ** Transfer content from the second pLoop into the first. |
| 4325 | */ |
| 4326 | static int whereLoopXfer(sqlite3 *db, WhereLoop *pTo, WhereLoop *pFrom){ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4327 | whereLoopClearUnion(db, pTo); |
drh | 0d31dc3 | 2013-09-06 00:40:59 +0000 | [diff] [blame] | 4328 | if( whereLoopResize(db, pTo, pFrom->nLTerm) ){ |
| 4329 | memset(&pTo->u, 0, sizeof(pTo->u)); |
| 4330 | return SQLITE_NOMEM; |
| 4331 | } |
drh | a201415 | 2013-06-07 00:29:23 +0000 | [diff] [blame] | 4332 | memcpy(pTo, pFrom, WHERE_LOOP_XFER_SZ); |
| 4333 | memcpy(pTo->aLTerm, pFrom->aLTerm, pTo->nLTerm*sizeof(pTo->aLTerm[0])); |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4334 | if( pFrom->wsFlags & WHERE_VIRTUALTABLE ){ |
| 4335 | pFrom->u.vtab.needFree = 0; |
drh | 986b387 | 2013-06-28 21:12:20 +0000 | [diff] [blame] | 4336 | }else if( (pFrom->wsFlags & WHERE_AUTO_INDEX)!=0 ){ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4337 | pFrom->u.btree.pIndex = 0; |
| 4338 | } |
| 4339 | return SQLITE_OK; |
| 4340 | } |
| 4341 | |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4342 | /* |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4343 | ** Delete a WhereLoop object |
| 4344 | */ |
| 4345 | static void whereLoopDelete(sqlite3 *db, WhereLoop *p){ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4346 | whereLoopClear(db, p); |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4347 | sqlite3DbFree(db, p); |
| 4348 | } |
drh | 84bfda4 | 2005-07-15 13:05:21 +0000 | [diff] [blame] | 4349 | |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 4350 | /* |
| 4351 | ** Free a WhereInfo structure |
| 4352 | */ |
drh | 10fe840 | 2008-10-11 16:47:35 +0000 | [diff] [blame] | 4353 | static void whereInfoFree(sqlite3 *db, WhereInfo *pWInfo){ |
drh | 52ff8ea | 2010-04-08 14:15:56 +0000 | [diff] [blame] | 4354 | if( ALWAYS(pWInfo) ){ |
dan | f89aa47 | 2015-04-25 12:20:24 +0000 | [diff] [blame] | 4355 | int i; |
| 4356 | for(i=0; i<pWInfo->nLevel; i++){ |
| 4357 | WhereLevel *pLevel = &pWInfo->a[i]; |
| 4358 | if( pLevel->pWLoop && (pLevel->pWLoop->wsFlags & WHERE_IN_ABLE) ){ |
| 4359 | sqlite3DbFree(db, pLevel->u.in.aInLoop); |
| 4360 | } |
| 4361 | } |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4362 | whereClauseClear(&pWInfo->sWC); |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4363 | while( pWInfo->pLoops ){ |
| 4364 | WhereLoop *p = pWInfo->pLoops; |
| 4365 | pWInfo->pLoops = p->pNextLoop; |
| 4366 | whereLoopDelete(db, p); |
| 4367 | } |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 4368 | sqlite3DbFree(db, pWInfo); |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 4369 | } |
| 4370 | } |
| 4371 | |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4372 | /* |
drh | e0de876 | 2014-11-05 13:13:13 +0000 | [diff] [blame] | 4373 | ** Return TRUE if all of the following are true: |
drh | b355c2c | 2014-04-18 22:20:31 +0000 | [diff] [blame] | 4374 | ** |
| 4375 | ** (1) X has the same or lower cost that Y |
| 4376 | ** (2) X is a proper subset of Y |
drh | e0de876 | 2014-11-05 13:13:13 +0000 | [diff] [blame] | 4377 | ** (3) X skips at least as many columns as Y |
drh | b355c2c | 2014-04-18 22:20:31 +0000 | [diff] [blame] | 4378 | ** |
| 4379 | ** By "proper subset" we mean that X uses fewer WHERE clause terms |
| 4380 | ** than Y and that every WHERE clause term used by X is also used |
| 4381 | ** by Y. |
| 4382 | ** |
| 4383 | ** If X is a proper subset of Y then Y is a better choice and ought |
| 4384 | ** to have a lower cost. This routine returns TRUE when that cost |
drh | e0de876 | 2014-11-05 13:13:13 +0000 | [diff] [blame] | 4385 | ** relationship is inverted and needs to be adjusted. The third rule |
| 4386 | ** was added because if X uses skip-scan less than Y it still might |
| 4387 | ** deserve a lower cost even if it is a proper subset of Y. |
drh | 3fb183d | 2014-03-31 19:49:00 +0000 | [diff] [blame] | 4388 | */ |
drh | b355c2c | 2014-04-18 22:20:31 +0000 | [diff] [blame] | 4389 | static int whereLoopCheaperProperSubset( |
| 4390 | const WhereLoop *pX, /* First WhereLoop to compare */ |
| 4391 | const WhereLoop *pY /* Compare against this WhereLoop */ |
| 4392 | ){ |
drh | 3fb183d | 2014-03-31 19:49:00 +0000 | [diff] [blame] | 4393 | int i, j; |
drh | c8bbce1 | 2014-10-21 01:05:09 +0000 | [diff] [blame] | 4394 | if( pX->nLTerm-pX->nSkip >= pY->nLTerm-pY->nSkip ){ |
| 4395 | return 0; /* X is not a subset of Y */ |
| 4396 | } |
drh | e0de876 | 2014-11-05 13:13:13 +0000 | [diff] [blame] | 4397 | if( pY->nSkip > pX->nSkip ) return 0; |
drh | b355c2c | 2014-04-18 22:20:31 +0000 | [diff] [blame] | 4398 | if( pX->rRun >= pY->rRun ){ |
| 4399 | if( pX->rRun > pY->rRun ) return 0; /* X costs more than Y */ |
| 4400 | if( pX->nOut > pY->nOut ) return 0; /* X costs more than Y */ |
drh | 3fb183d | 2014-03-31 19:49:00 +0000 | [diff] [blame] | 4401 | } |
drh | 9ee8810 | 2014-05-07 20:33:17 +0000 | [diff] [blame] | 4402 | for(i=pX->nLTerm-1; i>=0; i--){ |
drh | c8bbce1 | 2014-10-21 01:05:09 +0000 | [diff] [blame] | 4403 | if( pX->aLTerm[i]==0 ) continue; |
drh | b355c2c | 2014-04-18 22:20:31 +0000 | [diff] [blame] | 4404 | for(j=pY->nLTerm-1; j>=0; j--){ |
| 4405 | if( pY->aLTerm[j]==pX->aLTerm[i] ) break; |
| 4406 | } |
| 4407 | if( j<0 ) return 0; /* X not a subset of Y since term X[i] not used by Y */ |
| 4408 | } |
| 4409 | return 1; /* All conditions meet */ |
drh | 3fb183d | 2014-03-31 19:49:00 +0000 | [diff] [blame] | 4410 | } |
| 4411 | |
| 4412 | /* |
| 4413 | ** Try to adjust the cost of WhereLoop pTemplate upwards or downwards so |
| 4414 | ** that: |
drh | 53cd10a | 2014-03-31 18:24:18 +0000 | [diff] [blame] | 4415 | ** |
drh | 3fb183d | 2014-03-31 19:49:00 +0000 | [diff] [blame] | 4416 | ** (1) pTemplate costs less than any other WhereLoops that are a proper |
| 4417 | ** subset of pTemplate |
drh | 53cd10a | 2014-03-31 18:24:18 +0000 | [diff] [blame] | 4418 | ** |
drh | 3fb183d | 2014-03-31 19:49:00 +0000 | [diff] [blame] | 4419 | ** (2) pTemplate costs more than any other WhereLoops for which pTemplate |
| 4420 | ** is a proper subset. |
drh | 53cd10a | 2014-03-31 18:24:18 +0000 | [diff] [blame] | 4421 | ** |
drh | 3fb183d | 2014-03-31 19:49:00 +0000 | [diff] [blame] | 4422 | ** To say "WhereLoop X is a proper subset of Y" means that X uses fewer |
| 4423 | ** WHERE clause terms than Y and that every WHERE clause term used by X is |
| 4424 | ** also used by Y. |
drh | 53cd10a | 2014-03-31 18:24:18 +0000 | [diff] [blame] | 4425 | */ |
| 4426 | static void whereLoopAdjustCost(const WhereLoop *p, WhereLoop *pTemplate){ |
| 4427 | if( (pTemplate->wsFlags & WHERE_INDEXED)==0 ) return; |
drh | 53cd10a | 2014-03-31 18:24:18 +0000 | [diff] [blame] | 4428 | for(; p; p=p->pNextLoop){ |
drh | 3fb183d | 2014-03-31 19:49:00 +0000 | [diff] [blame] | 4429 | if( p->iTab!=pTemplate->iTab ) continue; |
| 4430 | if( (p->wsFlags & WHERE_INDEXED)==0 ) continue; |
drh | b355c2c | 2014-04-18 22:20:31 +0000 | [diff] [blame] | 4431 | if( whereLoopCheaperProperSubset(p, pTemplate) ){ |
| 4432 | /* Adjust pTemplate cost downward so that it is cheaper than its |
drh | e0de876 | 2014-11-05 13:13:13 +0000 | [diff] [blame] | 4433 | ** subset p. */ |
drh | 1b131b7 | 2014-10-21 16:01:40 +0000 | [diff] [blame] | 4434 | WHERETRACE(0x80,("subset cost adjustment %d,%d to %d,%d\n", |
| 4435 | pTemplate->rRun, pTemplate->nOut, p->rRun, p->nOut-1)); |
drh | 3fb183d | 2014-03-31 19:49:00 +0000 | [diff] [blame] | 4436 | pTemplate->rRun = p->rRun; |
| 4437 | pTemplate->nOut = p->nOut - 1; |
drh | b355c2c | 2014-04-18 22:20:31 +0000 | [diff] [blame] | 4438 | }else if( whereLoopCheaperProperSubset(pTemplate, p) ){ |
| 4439 | /* Adjust pTemplate cost upward so that it is costlier than p since |
| 4440 | ** pTemplate is a proper subset of p */ |
drh | 1b131b7 | 2014-10-21 16:01:40 +0000 | [diff] [blame] | 4441 | WHERETRACE(0x80,("subset cost adjustment %d,%d to %d,%d\n", |
| 4442 | pTemplate->rRun, pTemplate->nOut, p->rRun, p->nOut+1)); |
drh | 3fb183d | 2014-03-31 19:49:00 +0000 | [diff] [blame] | 4443 | pTemplate->rRun = p->rRun; |
| 4444 | pTemplate->nOut = p->nOut + 1; |
drh | 53cd10a | 2014-03-31 18:24:18 +0000 | [diff] [blame] | 4445 | } |
| 4446 | } |
| 4447 | } |
| 4448 | |
| 4449 | /* |
drh | 7a4b164 | 2014-03-29 21:16:07 +0000 | [diff] [blame] | 4450 | ** Search the list of WhereLoops in *ppPrev looking for one that can be |
| 4451 | ** supplanted by pTemplate. |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4452 | ** |
drh | 7a4b164 | 2014-03-29 21:16:07 +0000 | [diff] [blame] | 4453 | ** Return NULL if the WhereLoop list contains an entry that can supplant |
| 4454 | ** pTemplate, in other words if pTemplate does not belong on the list. |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4455 | ** |
drh | 7a4b164 | 2014-03-29 21:16:07 +0000 | [diff] [blame] | 4456 | ** If pX is a WhereLoop that pTemplate can supplant, then return the |
| 4457 | ** link that points to pX. |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 4458 | ** |
drh | 7a4b164 | 2014-03-29 21:16:07 +0000 | [diff] [blame] | 4459 | ** If pTemplate cannot supplant any existing element of the list but needs |
| 4460 | ** 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] | 4461 | */ |
drh | 7a4b164 | 2014-03-29 21:16:07 +0000 | [diff] [blame] | 4462 | static WhereLoop **whereLoopFindLesser( |
| 4463 | WhereLoop **ppPrev, |
| 4464 | const WhereLoop *pTemplate |
| 4465 | ){ |
| 4466 | WhereLoop *p; |
| 4467 | for(p=(*ppPrev); p; ppPrev=&p->pNextLoop, p=*ppPrev){ |
drh | dbb8023 | 2013-06-19 12:34:13 +0000 | [diff] [blame] | 4468 | if( p->iTab!=pTemplate->iTab || p->iSortIdx!=pTemplate->iSortIdx ){ |
| 4469 | /* If either the iTab or iSortIdx values for two WhereLoop are different |
| 4470 | ** then those WhereLoops need to be considered separately. Neither is |
| 4471 | ** a candidate to replace the other. */ |
| 4472 | continue; |
| 4473 | } |
| 4474 | /* In the current implementation, the rSetup value is either zero |
| 4475 | ** or the cost of building an automatic index (NlogN) and the NlogN |
| 4476 | ** is the same for compatible WhereLoops. */ |
| 4477 | assert( p->rSetup==0 || pTemplate->rSetup==0 |
| 4478 | || p->rSetup==pTemplate->rSetup ); |
| 4479 | |
| 4480 | /* whereLoopAddBtree() always generates and inserts the automatic index |
| 4481 | ** case first. Hence compatible candidate WhereLoops never have a larger |
| 4482 | ** rSetup. Call this SETUP-INVARIANT */ |
| 4483 | assert( p->rSetup>=pTemplate->rSetup ); |
| 4484 | |
drh | dabe36d | 2014-06-17 20:16:43 +0000 | [diff] [blame] | 4485 | /* Any loop using an appliation-defined index (or PRIMARY KEY or |
| 4486 | ** UNIQUE constraint) with one or more == constraints is better |
dan | 70273d0 | 2014-11-14 19:34:20 +0000 | [diff] [blame] | 4487 | ** than an automatic index. Unless it is a skip-scan. */ |
drh | dabe36d | 2014-06-17 20:16:43 +0000 | [diff] [blame] | 4488 | if( (p->wsFlags & WHERE_AUTO_INDEX)!=0 |
dan | 70273d0 | 2014-11-14 19:34:20 +0000 | [diff] [blame] | 4489 | && (pTemplate->nSkip)==0 |
drh | dabe36d | 2014-06-17 20:16:43 +0000 | [diff] [blame] | 4490 | && (pTemplate->wsFlags & WHERE_INDEXED)!=0 |
| 4491 | && (pTemplate->wsFlags & WHERE_COLUMN_EQ)!=0 |
| 4492 | && (p->prereq & pTemplate->prereq)==pTemplate->prereq |
| 4493 | ){ |
| 4494 | break; |
| 4495 | } |
| 4496 | |
drh | 53cd10a | 2014-03-31 18:24:18 +0000 | [diff] [blame] | 4497 | /* If existing WhereLoop p is better than pTemplate, pTemplate can be |
| 4498 | ** discarded. WhereLoop p is better if: |
| 4499 | ** (1) p has no more dependencies than pTemplate, and |
| 4500 | ** (2) p has an equal or lower cost than pTemplate |
| 4501 | */ |
| 4502 | if( (p->prereq & pTemplate->prereq)==p->prereq /* (1) */ |
| 4503 | && p->rSetup<=pTemplate->rSetup /* (2a) */ |
| 4504 | && p->rRun<=pTemplate->rRun /* (2b) */ |
| 4505 | && p->nOut<=pTemplate->nOut /* (2c) */ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4506 | ){ |
drh | 53cd10a | 2014-03-31 18:24:18 +0000 | [diff] [blame] | 4507 | return 0; /* Discard pTemplate */ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4508 | } |
drh | 53cd10a | 2014-03-31 18:24:18 +0000 | [diff] [blame] | 4509 | |
| 4510 | /* If pTemplate is always better than p, then cause p to be overwritten |
| 4511 | ** with pTemplate. pTemplate is better than p if: |
| 4512 | ** (1) pTemplate has no more dependences than p, and |
| 4513 | ** (2) pTemplate has an equal or lower cost than p. |
| 4514 | */ |
| 4515 | if( (p->prereq & pTemplate->prereq)==pTemplate->prereq /* (1) */ |
| 4516 | && p->rRun>=pTemplate->rRun /* (2a) */ |
| 4517 | && p->nOut>=pTemplate->nOut /* (2b) */ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4518 | ){ |
drh | add5ce3 | 2013-09-07 00:29:06 +0000 | [diff] [blame] | 4519 | assert( p->rSetup>=pTemplate->rSetup ); /* SETUP-INVARIANT above */ |
drh | 53cd10a | 2014-03-31 18:24:18 +0000 | [diff] [blame] | 4520 | break; /* Cause p to be overwritten by pTemplate */ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4521 | } |
| 4522 | } |
drh | 7a4b164 | 2014-03-29 21:16:07 +0000 | [diff] [blame] | 4523 | return ppPrev; |
| 4524 | } |
| 4525 | |
| 4526 | /* |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 4527 | ** Insert or replace a WhereLoop entry using the template supplied. |
| 4528 | ** |
| 4529 | ** An existing WhereLoop entry might be overwritten if the new template |
| 4530 | ** is better and has fewer dependencies. Or the template will be ignored |
| 4531 | ** and no insert will occur if an existing WhereLoop is faster and has |
| 4532 | ** fewer dependencies than the template. Otherwise a new WhereLoop is |
| 4533 | ** added based on the template. |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 4534 | ** |
drh | 7a4b164 | 2014-03-29 21:16:07 +0000 | [diff] [blame] | 4535 | ** If pBuilder->pOrSet is not NULL then we care about only the |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 4536 | ** prerequisites and rRun and nOut costs of the N best loops. That |
| 4537 | ** information is gathered in the pBuilder->pOrSet object. This special |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 4538 | ** processing mode is used only for OR clause processing. |
| 4539 | ** |
| 4540 | ** When accumulating multiple loops (when pBuilder->pOrSet is NULL) we |
| 4541 | ** still might overwrite similar loops with the new template if the |
drh | 53cd10a | 2014-03-31 18:24:18 +0000 | [diff] [blame] | 4542 | ** new template is better. Loops may be overwritten if the following |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 4543 | ** conditions are met: |
| 4544 | ** |
| 4545 | ** (1) They have the same iTab. |
| 4546 | ** (2) They have the same iSortIdx. |
| 4547 | ** (3) The template has same or fewer dependencies than the current loop |
| 4548 | ** (4) The template has the same or lower cost than the current loop |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 4549 | */ |
| 4550 | static int whereLoopInsert(WhereLoopBuilder *pBuilder, WhereLoop *pTemplate){ |
drh | 7a4b164 | 2014-03-29 21:16:07 +0000 | [diff] [blame] | 4551 | WhereLoop **ppPrev, *p; |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 4552 | WhereInfo *pWInfo = pBuilder->pWInfo; |
| 4553 | sqlite3 *db = pWInfo->pParse->db; |
| 4554 | |
| 4555 | /* If pBuilder->pOrSet is defined, then only keep track of the costs |
| 4556 | ** and prereqs. |
| 4557 | */ |
| 4558 | if( pBuilder->pOrSet!=0 ){ |
| 4559 | #if WHERETRACE_ENABLED |
drh | 5166986 | 2004-12-18 18:40:26 +0000 | [diff] [blame] | 4560 | u16 n = pBuilder->pOrSet->n; |
| 4561 | int x = |
| 4562 | #endif |
| 4563 | whereOrInsert(pBuilder->pOrSet, pTemplate->prereq, pTemplate->rRun, |
| 4564 | pTemplate->nOut); |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 4565 | #if WHERETRACE_ENABLED /* 0x8 */ |
| 4566 | if( sqlite3WhereTrace & 0x8 ){ |
drh | e318474 | 2002-06-19 14:27:05 +0000 | [diff] [blame] | 4567 | sqlite3DebugPrintf(x?" or-%d: ":" or-X: ", n); |
drh | acf3b98 | 2005-01-03 01:27:18 +0000 | [diff] [blame] | 4568 | whereLoopPrint(pTemplate, pBuilder->pWC); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4569 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 4570 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4571 | return SQLITE_OK; |
| 4572 | } |
| 4573 | |
drh | 7a4b164 | 2014-03-29 21:16:07 +0000 | [diff] [blame] | 4574 | /* Look for an existing WhereLoop to replace with pTemplate |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 4575 | */ |
drh | 53cd10a | 2014-03-31 18:24:18 +0000 | [diff] [blame] | 4576 | whereLoopAdjustCost(pWInfo->pLoops, pTemplate); |
drh | 7a4b164 | 2014-03-29 21:16:07 +0000 | [diff] [blame] | 4577 | ppPrev = whereLoopFindLesser(&pWInfo->pLoops, pTemplate); |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4578 | |
drh | 7a4b164 | 2014-03-29 21:16:07 +0000 | [diff] [blame] | 4579 | if( ppPrev==0 ){ |
| 4580 | /* There already exists a WhereLoop on the list that is better |
| 4581 | ** than pTemplate, so just ignore pTemplate */ |
| 4582 | #if WHERETRACE_ENABLED /* 0x8 */ |
| 4583 | if( sqlite3WhereTrace & 0x8 ){ |
drh | 9a7b41d | 2014-10-08 00:08:08 +0000 | [diff] [blame] | 4584 | sqlite3DebugPrintf(" skip: "); |
drh | 7a4b164 | 2014-03-29 21:16:07 +0000 | [diff] [blame] | 4585 | whereLoopPrint(pTemplate, pBuilder->pWC); |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4586 | } |
drh | 7a4b164 | 2014-03-29 21:16:07 +0000 | [diff] [blame] | 4587 | #endif |
| 4588 | return SQLITE_OK; |
| 4589 | }else{ |
| 4590 | p = *ppPrev; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4591 | } |
| 4592 | |
| 4593 | /* If we reach this point it means that either p[] should be overwritten |
| 4594 | ** with pTemplate[] if p[] exists, or if p==NULL then allocate a new |
| 4595 | ** WhereLoop and insert it. |
| 4596 | */ |
drh | 989578e | 2013-10-28 14:34:35 +0000 | [diff] [blame] | 4597 | #if WHERETRACE_ENABLED /* 0x8 */ |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 4598 | if( sqlite3WhereTrace & 0x8 ){ |
| 4599 | if( p!=0 ){ |
drh | 9a7b41d | 2014-10-08 00:08:08 +0000 | [diff] [blame] | 4600 | sqlite3DebugPrintf("replace: "); |
drh | c1ba2e7 | 2013-10-28 19:03:21 +0000 | [diff] [blame] | 4601 | whereLoopPrint(p, pBuilder->pWC); |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 4602 | } |
drh | 9a7b41d | 2014-10-08 00:08:08 +0000 | [diff] [blame] | 4603 | sqlite3DebugPrintf(" add: "); |
drh | c1ba2e7 | 2013-10-28 19:03:21 +0000 | [diff] [blame] | 4604 | whereLoopPrint(pTemplate, pBuilder->pWC); |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 4605 | } |
| 4606 | #endif |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4607 | if( p==0 ){ |
drh | 7a4b164 | 2014-03-29 21:16:07 +0000 | [diff] [blame] | 4608 | /* Allocate a new WhereLoop to add to the end of the list */ |
| 4609 | *ppPrev = p = sqlite3DbMallocRaw(db, sizeof(WhereLoop)); |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4610 | if( p==0 ) return SQLITE_NOMEM; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4611 | whereLoopInit(p); |
drh | 7a4b164 | 2014-03-29 21:16:07 +0000 | [diff] [blame] | 4612 | p->pNextLoop = 0; |
| 4613 | }else{ |
| 4614 | /* We will be overwriting WhereLoop p[]. But before we do, first |
| 4615 | ** go through the rest of the list and delete any other entries besides |
| 4616 | ** p[] that are also supplated by pTemplate */ |
| 4617 | WhereLoop **ppTail = &p->pNextLoop; |
| 4618 | WhereLoop *pToDel; |
| 4619 | while( *ppTail ){ |
| 4620 | ppTail = whereLoopFindLesser(ppTail, pTemplate); |
drh | dabe36d | 2014-06-17 20:16:43 +0000 | [diff] [blame] | 4621 | if( ppTail==0 ) break; |
drh | 7a4b164 | 2014-03-29 21:16:07 +0000 | [diff] [blame] | 4622 | pToDel = *ppTail; |
| 4623 | if( pToDel==0 ) break; |
| 4624 | *ppTail = pToDel->pNextLoop; |
| 4625 | #if WHERETRACE_ENABLED /* 0x8 */ |
| 4626 | if( sqlite3WhereTrace & 0x8 ){ |
drh | 9a7b41d | 2014-10-08 00:08:08 +0000 | [diff] [blame] | 4627 | sqlite3DebugPrintf(" delete: "); |
drh | 7a4b164 | 2014-03-29 21:16:07 +0000 | [diff] [blame] | 4628 | whereLoopPrint(pToDel, pBuilder->pWC); |
| 4629 | } |
| 4630 | #endif |
| 4631 | whereLoopDelete(db, pToDel); |
| 4632 | } |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4633 | } |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4634 | whereLoopXfer(db, p, pTemplate); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4635 | if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){ |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 4636 | Index *pIndex = p->u.btree.pIndex; |
| 4637 | if( pIndex && pIndex->tnum==0 ){ |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4638 | p->u.btree.pIndex = 0; |
| 4639 | } |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4640 | } |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 4641 | return SQLITE_OK; |
| 4642 | } |
| 4643 | |
| 4644 | /* |
drh | cca9f3d | 2013-09-06 15:23:29 +0000 | [diff] [blame] | 4645 | ** Adjust the WhereLoop.nOut value downward to account for terms of the |
| 4646 | ** WHERE clause that reference the loop but which are not used by an |
| 4647 | ** index. |
drh | 7a1bca7 | 2014-11-22 18:50:44 +0000 | [diff] [blame] | 4648 | * |
| 4649 | ** For every WHERE clause term that is not used by the index |
| 4650 | ** and which has a truth probability assigned by one of the likelihood(), |
| 4651 | ** likely(), or unlikely() SQL functions, reduce the estimated number |
| 4652 | ** of output rows by the probability specified. |
drh | cca9f3d | 2013-09-06 15:23:29 +0000 | [diff] [blame] | 4653 | ** |
drh | 7a1bca7 | 2014-11-22 18:50:44 +0000 | [diff] [blame] | 4654 | ** TUNING: For every WHERE clause term that is not used by the index |
| 4655 | ** and which does not have an assigned truth probability, heuristics |
| 4656 | ** described below are used to try to estimate the truth probability. |
| 4657 | ** TODO --> Perhaps this is something that could be improved by better |
| 4658 | ** table statistics. |
| 4659 | ** |
drh | ab4624d | 2014-11-22 19:52:10 +0000 | [diff] [blame] | 4660 | ** Heuristic 1: Estimate the truth probability as 93.75%. The 93.75% |
| 4661 | ** value corresponds to -1 in LogEst notation, so this means decrement |
drh | 7a1bca7 | 2014-11-22 18:50:44 +0000 | [diff] [blame] | 4662 | ** the WhereLoop.nOut field for every such WHERE clause term. |
| 4663 | ** |
| 4664 | ** Heuristic 2: If there exists one or more WHERE clause terms of the |
| 4665 | ** form "x==EXPR" and EXPR is not a constant 0 or 1, then make sure the |
| 4666 | ** final output row estimate is no greater than 1/4 of the total number |
| 4667 | ** of rows in the table. In other words, assume that x==EXPR will filter |
| 4668 | ** out at least 3 out of 4 rows. If EXPR is -1 or 0 or 1, then maybe the |
| 4669 | ** "x" column is boolean or else -1 or 0 or 1 is a common default value |
| 4670 | ** on the "x" column and so in that case only cap the output row estimate |
| 4671 | ** at 1/2 instead of 1/4. |
drh | cca9f3d | 2013-09-06 15:23:29 +0000 | [diff] [blame] | 4672 | */ |
drh | d8b77e2 | 2014-09-06 01:35:57 +0000 | [diff] [blame] | 4673 | static void whereLoopOutputAdjust( |
| 4674 | WhereClause *pWC, /* The WHERE clause */ |
| 4675 | WhereLoop *pLoop, /* The loop to adjust downward */ |
| 4676 | LogEst nRow /* Number of rows in the entire table */ |
| 4677 | ){ |
drh | 7d9e7d8 | 2013-09-11 17:39:09 +0000 | [diff] [blame] | 4678 | WhereTerm *pTerm, *pX; |
drh | cca9f3d | 2013-09-06 15:23:29 +0000 | [diff] [blame] | 4679 | Bitmask notAllowed = ~(pLoop->prereq|pLoop->maskSelf); |
drh | 7a1bca7 | 2014-11-22 18:50:44 +0000 | [diff] [blame] | 4680 | int i, j, k; |
| 4681 | LogEst iReduce = 0; /* pLoop->nOut should not exceed nRow-iReduce */ |
drh | add5ce3 | 2013-09-07 00:29:06 +0000 | [diff] [blame] | 4682 | |
drh | a389825 | 2014-11-22 12:22:13 +0000 | [diff] [blame] | 4683 | assert( (pLoop->wsFlags & WHERE_AUTO_INDEX)==0 ); |
drh | cca9f3d | 2013-09-06 15:23:29 +0000 | [diff] [blame] | 4684 | for(i=pWC->nTerm, pTerm=pWC->a; i>0; i--, pTerm++){ |
drh | 7d9e7d8 | 2013-09-11 17:39:09 +0000 | [diff] [blame] | 4685 | if( (pTerm->wtFlags & TERM_VIRTUAL)!=0 ) break; |
drh | cca9f3d | 2013-09-06 15:23:29 +0000 | [diff] [blame] | 4686 | if( (pTerm->prereqAll & pLoop->maskSelf)==0 ) continue; |
| 4687 | if( (pTerm->prereqAll & notAllowed)!=0 ) continue; |
drh | 7d9e7d8 | 2013-09-11 17:39:09 +0000 | [diff] [blame] | 4688 | for(j=pLoop->nLTerm-1; j>=0; j--){ |
| 4689 | pX = pLoop->aLTerm[j]; |
drh | d244744 | 2013-11-13 19:01:41 +0000 | [diff] [blame] | 4690 | if( pX==0 ) continue; |
drh | 7d9e7d8 | 2013-09-11 17:39:09 +0000 | [diff] [blame] | 4691 | if( pX==pTerm ) break; |
| 4692 | if( pX->iParent>=0 && (&pWC->a[pX->iParent])==pTerm ) break; |
| 4693 | } |
dan | aa9933c | 2014-04-24 20:04:49 +0000 | [diff] [blame] | 4694 | if( j<0 ){ |
drh | d8b77e2 | 2014-09-06 01:35:57 +0000 | [diff] [blame] | 4695 | if( pTerm->truthProb<=0 ){ |
drh | 7a1bca7 | 2014-11-22 18:50:44 +0000 | [diff] [blame] | 4696 | /* If a truth probability is specified using the likelihood() hints, |
| 4697 | ** then use the probability provided by the application. */ |
drh | d8b77e2 | 2014-09-06 01:35:57 +0000 | [diff] [blame] | 4698 | pLoop->nOut += pTerm->truthProb; |
| 4699 | }else{ |
drh | 7a1bca7 | 2014-11-22 18:50:44 +0000 | [diff] [blame] | 4700 | /* In the absence of explicit truth probabilities, use heuristics to |
| 4701 | ** guess a reasonable truth probability. */ |
drh | d8b77e2 | 2014-09-06 01:35:57 +0000 | [diff] [blame] | 4702 | pLoop->nOut--; |
drh | e8d0c61 | 2015-05-14 01:05:25 +0000 | [diff] [blame] | 4703 | if( pTerm->eOperator&(WO_EQ|WO_IS) ){ |
drh | 7a1bca7 | 2014-11-22 18:50:44 +0000 | [diff] [blame] | 4704 | Expr *pRight = pTerm->pExpr->pRight; |
drh | e0cc3c2 | 2015-05-13 17:54:08 +0000 | [diff] [blame] | 4705 | testcase( pTerm->pExpr->op==TK_IS ); |
drh | 7a1bca7 | 2014-11-22 18:50:44 +0000 | [diff] [blame] | 4706 | if( sqlite3ExprIsInteger(pRight, &k) && k>=(-1) && k<=1 ){ |
| 4707 | k = 10; |
| 4708 | }else{ |
| 4709 | k = 20; |
| 4710 | } |
| 4711 | if( iReduce<k ) iReduce = k; |
| 4712 | } |
drh | d8b77e2 | 2014-09-06 01:35:57 +0000 | [diff] [blame] | 4713 | } |
dan | aa9933c | 2014-04-24 20:04:49 +0000 | [diff] [blame] | 4714 | } |
drh | cca9f3d | 2013-09-06 15:23:29 +0000 | [diff] [blame] | 4715 | } |
drh | 7a1bca7 | 2014-11-22 18:50:44 +0000 | [diff] [blame] | 4716 | if( pLoop->nOut > nRow-iReduce ) pLoop->nOut = nRow - iReduce; |
drh | cca9f3d | 2013-09-06 15:23:29 +0000 | [diff] [blame] | 4717 | } |
| 4718 | |
| 4719 | /* |
drh | dbd9486 | 2014-07-23 23:57:42 +0000 | [diff] [blame] | 4720 | ** Adjust the cost C by the costMult facter T. This only occurs if |
| 4721 | ** compiled with -DSQLITE_ENABLE_COSTMULT |
| 4722 | */ |
| 4723 | #ifdef SQLITE_ENABLE_COSTMULT |
| 4724 | # define ApplyCostMultiplier(C,T) C += T |
| 4725 | #else |
| 4726 | # define ApplyCostMultiplier(C,T) |
| 4727 | #endif |
| 4728 | |
| 4729 | /* |
dan | 4a6b8a0 | 2014-04-30 14:47:01 +0000 | [diff] [blame] | 4730 | ** We have so far matched pBuilder->pNew->u.btree.nEq terms of the |
| 4731 | ** index pIndex. Try to match one more. |
| 4732 | ** |
| 4733 | ** When this function is called, pBuilder->pNew->nOut contains the |
| 4734 | ** number of rows expected to be visited by filtering using the nEq |
| 4735 | ** terms only. If it is modified, this value is restored before this |
| 4736 | ** function returns. |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4737 | ** |
| 4738 | ** If pProbe->tnum==0, that means pIndex is a fake index used for the |
| 4739 | ** INTEGER PRIMARY KEY. |
| 4740 | */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4741 | static int whereLoopAddBtreeIndex( |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4742 | WhereLoopBuilder *pBuilder, /* The WhereLoop factory */ |
| 4743 | struct SrcList_item *pSrc, /* FROM clause term being analyzed */ |
| 4744 | Index *pProbe, /* An index on pSrc */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 4745 | LogEst nInMul /* log(Number of iterations due to IN) */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4746 | ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 4747 | WhereInfo *pWInfo = pBuilder->pWInfo; /* WHERE analyse context */ |
| 4748 | Parse *pParse = pWInfo->pParse; /* Parsing context */ |
| 4749 | sqlite3 *db = pParse->db; /* Database connection malloc context */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4750 | WhereLoop *pNew; /* Template WhereLoop under construction */ |
| 4751 | WhereTerm *pTerm; /* A WhereTerm under consideration */ |
drh | 43fe25f | 2013-05-07 23:06:23 +0000 | [diff] [blame] | 4752 | int opMask; /* Valid operators for constraints */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4753 | WhereScan scan; /* Iterator for WHERE terms */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4754 | Bitmask saved_prereq; /* Original value of pNew->prereq */ |
| 4755 | u16 saved_nLTerm; /* Original value of pNew->nLTerm */ |
drh | cd8629e | 2013-11-13 12:27:25 +0000 | [diff] [blame] | 4756 | u16 saved_nEq; /* Original value of pNew->u.btree.nEq */ |
drh | c8bbce1 | 2014-10-21 01:05:09 +0000 | [diff] [blame] | 4757 | u16 saved_nSkip; /* Original value of pNew->nSkip */ |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4758 | u32 saved_wsFlags; /* Original value of pNew->wsFlags */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 4759 | LogEst saved_nOut; /* Original value of pNew->nOut */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4760 | int iCol; /* Index of the column in the table */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4761 | int rc = SQLITE_OK; /* Return code */ |
drh | d8b77e2 | 2014-09-06 01:35:57 +0000 | [diff] [blame] | 4762 | LogEst rSize; /* Number of rows in the table */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 4763 | LogEst rLogSize; /* Logarithm of table size */ |
drh | c7f0d22 | 2013-06-19 03:27:12 +0000 | [diff] [blame] | 4764 | WhereTerm *pTop = 0, *pBtm = 0; /* Top and bottom range constraints */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4765 | |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4766 | pNew = pBuilder->pNew; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4767 | if( db->mallocFailed ) return SQLITE_NOMEM; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4768 | |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4769 | assert( (pNew->wsFlags & WHERE_VIRTUALTABLE)==0 ); |
drh | 43fe25f | 2013-05-07 23:06:23 +0000 | [diff] [blame] | 4770 | assert( (pNew->wsFlags & WHERE_TOP_LIMIT)==0 ); |
| 4771 | if( pNew->wsFlags & WHERE_BTM_LIMIT ){ |
| 4772 | opMask = WO_LT|WO_LE; |
drh | ee14587 | 2015-05-14 13:18:47 +0000 | [diff] [blame] | 4773 | }else if( /*pProbe->tnum<=0 ||*/ (pSrc->jointype & JT_LEFT)!=0 ){ |
drh | 43fe25f | 2013-05-07 23:06:23 +0000 | [diff] [blame] | 4774 | opMask = WO_EQ|WO_IN|WO_GT|WO_GE|WO_LT|WO_LE; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4775 | }else{ |
drh | e8d0c61 | 2015-05-14 01:05:25 +0000 | [diff] [blame] | 4776 | 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] | 4777 | } |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 4778 | if( pProbe->bUnordered ) opMask &= ~(WO_GT|WO_GE|WO_LT|WO_LE); |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4779 | |
dan | 39129ce | 2014-06-30 15:23:57 +0000 | [diff] [blame] | 4780 | assert( pNew->u.btree.nEq<pProbe->nColumn ); |
| 4781 | iCol = pProbe->aiColumn[pNew->u.btree.nEq]; |
| 4782 | |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4783 | pTerm = whereScanInit(&scan, pBuilder->pWC, pSrc->iCursor, iCol, |
drh | 0f133a4 | 2013-05-22 17:01:17 +0000 | [diff] [blame] | 4784 | opMask, pProbe); |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4785 | saved_nEq = pNew->u.btree.nEq; |
drh | c8bbce1 | 2014-10-21 01:05:09 +0000 | [diff] [blame] | 4786 | saved_nSkip = pNew->nSkip; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4787 | saved_nLTerm = pNew->nLTerm; |
| 4788 | saved_wsFlags = pNew->wsFlags; |
| 4789 | saved_prereq = pNew->prereq; |
| 4790 | saved_nOut = pNew->nOut; |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4791 | pNew->rSetup = 0; |
drh | d8b77e2 | 2014-09-06 01:35:57 +0000 | [diff] [blame] | 4792 | rSize = pProbe->aiRowLogEst[0]; |
| 4793 | rLogSize = estLog(rSize); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4794 | for(; rc==SQLITE_OK && pTerm!=0; pTerm = whereScanNext(&scan)){ |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 4795 | u16 eOp = pTerm->eOperator; /* Shorthand for pTerm->eOperator */ |
dan | aa9933c | 2014-04-24 20:04:49 +0000 | [diff] [blame] | 4796 | LogEst rCostIdx; |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 4797 | LogEst nOutUnadjusted; /* nOut before IN() and WHERE adjustments */ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4798 | int nIn = 0; |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 4799 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 4800 | int nRecValid = pBuilder->nRecValid; |
drh | b5246e5 | 2013-07-08 21:12:57 +0000 | [diff] [blame] | 4801 | #endif |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 4802 | if( (eOp==WO_ISNULL || (pTerm->wtFlags&TERM_VNULL)!=0) |
dan | 8bff07a | 2013-08-29 14:56:14 +0000 | [diff] [blame] | 4803 | && (iCol<0 || pSrc->pTab->aCol[iCol].notNull) |
| 4804 | ){ |
| 4805 | continue; /* ignore IS [NOT] NULL constraints on NOT NULL columns */ |
| 4806 | } |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 4807 | if( pTerm->prereqRight & pNew->maskSelf ) continue; |
| 4808 | |
drh | a40da62 | 2015-03-09 12:11:56 +0000 | [diff] [blame] | 4809 | /* Do not allow the upper bound of a LIKE optimization range constraint |
| 4810 | ** to mix with a lower range bound from some other source */ |
| 4811 | if( pTerm->wtFlags & TERM_LIKEOPT && pTerm->eOperator==WO_LT ) continue; |
| 4812 | |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4813 | pNew->wsFlags = saved_wsFlags; |
| 4814 | pNew->u.btree.nEq = saved_nEq; |
| 4815 | pNew->nLTerm = saved_nLTerm; |
| 4816 | if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */ |
| 4817 | pNew->aLTerm[pNew->nLTerm++] = pTerm; |
| 4818 | pNew->prereq = (saved_prereq | pTerm->prereqRight) & ~pNew->maskSelf; |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 4819 | |
| 4820 | assert( nInMul==0 |
| 4821 | || (pNew->wsFlags & WHERE_COLUMN_NULL)!=0 |
| 4822 | || (pNew->wsFlags & WHERE_COLUMN_IN)!=0 |
| 4823 | || (pNew->wsFlags & WHERE_SKIPSCAN)!=0 |
| 4824 | ); |
| 4825 | |
| 4826 | if( eOp & WO_IN ){ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4827 | Expr *pExpr = pTerm->pExpr; |
| 4828 | pNew->wsFlags |= WHERE_COLUMN_IN; |
| 4829 | if( ExprHasProperty(pExpr, EP_xIsSelect) ){ |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 4830 | /* "x IN (SELECT ...)": TUNING: the SELECT returns 25 rows */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 4831 | nIn = 46; assert( 46==sqlite3LogEst(25) ); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4832 | }else if( ALWAYS(pExpr->x.pList && pExpr->x.pList->nExpr) ){ |
| 4833 | /* "x IN (value, value, ...)" */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 4834 | nIn = sqlite3LogEst(pExpr->x.pList->nExpr); |
drh | f1645f0 | 2013-05-07 19:44:38 +0000 | [diff] [blame] | 4835 | } |
drh | 2b59b3a | 2014-03-20 13:26:47 +0000 | [diff] [blame] | 4836 | assert( nIn>0 ); /* RHS always has 2 or more terms... The parser |
| 4837 | ** changes "x IN (?)" into "x=?". */ |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 4838 | |
drh | e8d0c61 | 2015-05-14 01:05:25 +0000 | [diff] [blame] | 4839 | }else if( eOp & (WO_EQ|WO_IS) ){ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4840 | pNew->wsFlags |= WHERE_COLUMN_EQ; |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 4841 | if( iCol<0 || (nInMul==0 && pNew->u.btree.nEq==pProbe->nKeyCol-1) ){ |
dan | 2813bde | 2015-04-11 11:44:27 +0000 | [diff] [blame] | 4842 | if( iCol>=0 && pProbe->uniqNotNull==0 ){ |
drh | e39a732 | 2014-02-03 14:04:11 +0000 | [diff] [blame] | 4843 | pNew->wsFlags |= WHERE_UNQ_WANTED; |
| 4844 | }else{ |
| 4845 | pNew->wsFlags |= WHERE_ONEROW; |
| 4846 | } |
drh | 21f7ff7 | 2013-06-03 15:07:23 +0000 | [diff] [blame] | 4847 | } |
dan | 2dd3cdc | 2014-04-26 20:21:14 +0000 | [diff] [blame] | 4848 | }else if( eOp & WO_ISNULL ){ |
| 4849 | pNew->wsFlags |= WHERE_COLUMN_NULL; |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 4850 | }else if( eOp & (WO_GT|WO_GE) ){ |
| 4851 | testcase( eOp & WO_GT ); |
| 4852 | testcase( eOp & WO_GE ); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4853 | pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_BTM_LIMIT; |
drh | 6f2bfad | 2013-06-03 17:35:22 +0000 | [diff] [blame] | 4854 | pBtm = pTerm; |
| 4855 | pTop = 0; |
drh | a40da62 | 2015-03-09 12:11:56 +0000 | [diff] [blame] | 4856 | if( pTerm->wtFlags & TERM_LIKEOPT ){ |
drh | 8031462 | 2015-03-09 13:01:02 +0000 | [diff] [blame] | 4857 | /* Range contraints that come from the LIKE optimization are |
| 4858 | ** always used in pairs. */ |
drh | a40da62 | 2015-03-09 12:11:56 +0000 | [diff] [blame] | 4859 | pTop = &pTerm[1]; |
| 4860 | assert( (pTop-(pTerm->pWC->a))<pTerm->pWC->nTerm ); |
| 4861 | assert( pTop->wtFlags & TERM_LIKEOPT ); |
| 4862 | assert( pTop->eOperator==WO_LT ); |
| 4863 | if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */ |
| 4864 | pNew->aLTerm[pNew->nLTerm++] = pTop; |
| 4865 | pNew->wsFlags |= WHERE_TOP_LIMIT; |
| 4866 | } |
dan | 2dd3cdc | 2014-04-26 20:21:14 +0000 | [diff] [blame] | 4867 | }else{ |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 4868 | assert( eOp & (WO_LT|WO_LE) ); |
| 4869 | testcase( eOp & WO_LT ); |
| 4870 | testcase( eOp & WO_LE ); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4871 | pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_TOP_LIMIT; |
drh | 6f2bfad | 2013-06-03 17:35:22 +0000 | [diff] [blame] | 4872 | pTop = pTerm; |
| 4873 | pBtm = (pNew->wsFlags & WHERE_BTM_LIMIT)!=0 ? |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4874 | pNew->aLTerm[pNew->nLTerm-2] : 0; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4875 | } |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 4876 | |
| 4877 | /* At this point pNew->nOut is set to the number of rows expected to |
| 4878 | ** be visited by the index scan before considering term pTerm, or the |
| 4879 | ** values of nIn and nInMul. In other words, assuming that all |
| 4880 | ** "x IN(...)" terms are replaced with "x = ?". This block updates |
| 4881 | ** the value of pNew->nOut to account for pTerm (but not nIn/nInMul). */ |
| 4882 | assert( pNew->nOut==saved_nOut ); |
drh | 6f2bfad | 2013-06-03 17:35:22 +0000 | [diff] [blame] | 4883 | if( pNew->wsFlags & WHERE_COLUMN_RANGE ){ |
dan | aa9933c | 2014-04-24 20:04:49 +0000 | [diff] [blame] | 4884 | /* Adjust nOut using stat3/stat4 data. Or, if there is no stat3/stat4 |
| 4885 | ** data, using some other estimate. */ |
drh | 186ad8c | 2013-10-08 18:40:37 +0000 | [diff] [blame] | 4886 | whereRangeScanEst(pParse, pBuilder, pBtm, pTop, pNew); |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 4887 | }else{ |
| 4888 | int nEq = ++pNew->u.btree.nEq; |
drh | e8d0c61 | 2015-05-14 01:05:25 +0000 | [diff] [blame] | 4889 | assert( eOp & (WO_ISNULL|WO_EQ|WO_IN|WO_IS) ); |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 4890 | |
| 4891 | assert( pNew->nOut==saved_nOut ); |
dan | 09e1df6 | 2014-04-29 16:10:22 +0000 | [diff] [blame] | 4892 | if( pTerm->truthProb<=0 && iCol>=0 ){ |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 4893 | assert( (eOp & WO_IN) || nIn==0 ); |
drh | c5f246e | 2014-05-01 20:24:21 +0000 | [diff] [blame] | 4894 | testcase( eOp & WO_IN ); |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 4895 | pNew->nOut += pTerm->truthProb; |
| 4896 | pNew->nOut -= nIn; |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 4897 | }else{ |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 4898 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 4899 | tRowcnt nOut = 0; |
| 4900 | if( nInMul==0 |
| 4901 | && pProbe->nSample |
| 4902 | && pNew->u.btree.nEq<=pProbe->nSampleCol |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 4903 | && ((eOp & WO_IN)==0 || !ExprHasProperty(pTerm->pExpr, EP_xIsSelect)) |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 4904 | ){ |
| 4905 | Expr *pExpr = pTerm->pExpr; |
drh | e8d0c61 | 2015-05-14 01:05:25 +0000 | [diff] [blame] | 4906 | if( (eOp & (WO_EQ|WO_ISNULL|WO_IS))!=0 ){ |
| 4907 | testcase( eOp & WO_EQ ); |
| 4908 | testcase( eOp & WO_IS ); |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 4909 | testcase( eOp & WO_ISNULL ); |
| 4910 | rc = whereEqualScanEst(pParse, pBuilder, pExpr->pRight, &nOut); |
| 4911 | }else{ |
| 4912 | rc = whereInScanEst(pParse, pBuilder, pExpr->x.pList, &nOut); |
| 4913 | } |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 4914 | if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK; |
| 4915 | if( rc!=SQLITE_OK ) break; /* Jump out of the pTerm loop */ |
| 4916 | if( nOut ){ |
| 4917 | pNew->nOut = sqlite3LogEst(nOut); |
| 4918 | if( pNew->nOut>saved_nOut ) pNew->nOut = saved_nOut; |
| 4919 | pNew->nOut -= nIn; |
| 4920 | } |
| 4921 | } |
| 4922 | if( nOut==0 ) |
| 4923 | #endif |
| 4924 | { |
| 4925 | pNew->nOut += (pProbe->aiRowLogEst[nEq] - pProbe->aiRowLogEst[nEq-1]); |
| 4926 | if( eOp & WO_ISNULL ){ |
| 4927 | /* TUNING: If there is no likelihood() value, assume that a |
| 4928 | ** "col IS NULL" expression matches twice as many rows |
| 4929 | ** as (col=?). */ |
| 4930 | pNew->nOut += 10; |
| 4931 | } |
| 4932 | } |
dan | 6cb8d76 | 2013-08-08 11:48:57 +0000 | [diff] [blame] | 4933 | } |
drh | 6f2bfad | 2013-06-03 17:35:22 +0000 | [diff] [blame] | 4934 | } |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 4935 | |
dan | aa9933c | 2014-04-24 20:04:49 +0000 | [diff] [blame] | 4936 | /* Set rCostIdx to the cost of visiting selected rows in index. Add |
| 4937 | ** it to pNew->rRun, which is currently set to the cost of the index |
| 4938 | ** seek only. Then, if this is a non-covering index, add the cost of |
| 4939 | ** visiting the rows in the main table. */ |
| 4940 | rCostIdx = pNew->nOut + 1 + (15*pProbe->szIdxRow)/pSrc->pTab->szTabRow; |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 4941 | pNew->rRun = sqlite3LogEstAdd(rLogSize, rCostIdx); |
drh | e217efc | 2013-06-12 03:48:41 +0000 | [diff] [blame] | 4942 | if( (pNew->wsFlags & (WHERE_IDX_ONLY|WHERE_IPK))==0 ){ |
dan | aa9933c | 2014-04-24 20:04:49 +0000 | [diff] [blame] | 4943 | pNew->rRun = sqlite3LogEstAdd(pNew->rRun, pNew->nOut + 16); |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 4944 | } |
drh | dbd9486 | 2014-07-23 23:57:42 +0000 | [diff] [blame] | 4945 | ApplyCostMultiplier(pNew->rRun, pProbe->pTable->costMult); |
dan | aa9933c | 2014-04-24 20:04:49 +0000 | [diff] [blame] | 4946 | |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 4947 | nOutUnadjusted = pNew->nOut; |
| 4948 | pNew->rRun += nInMul + nIn; |
| 4949 | pNew->nOut += nInMul + nIn; |
drh | d8b77e2 | 2014-09-06 01:35:57 +0000 | [diff] [blame] | 4950 | whereLoopOutputAdjust(pBuilder->pWC, pNew, rSize); |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 4951 | rc = whereLoopInsert(pBuilder, pNew); |
dan | 440e6ff | 2014-04-28 08:49:54 +0000 | [diff] [blame] | 4952 | |
| 4953 | if( pNew->wsFlags & WHERE_COLUMN_RANGE ){ |
| 4954 | pNew->nOut = saved_nOut; |
| 4955 | }else{ |
| 4956 | pNew->nOut = nOutUnadjusted; |
| 4957 | } |
dan | 8ad1d8b | 2014-04-25 20:22:45 +0000 | [diff] [blame] | 4958 | |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4959 | if( (pNew->wsFlags & WHERE_TOP_LIMIT)==0 |
dan | 39129ce | 2014-06-30 15:23:57 +0000 | [diff] [blame] | 4960 | && pNew->u.btree.nEq<pProbe->nColumn |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 4961 | ){ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 4962 | whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nInMul+nIn); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 4963 | } |
dan | ad45ed7 | 2013-08-08 12:21:32 +0000 | [diff] [blame] | 4964 | pNew->nOut = saved_nOut; |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 4965 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 4966 | pBuilder->nRecValid = nRecValid; |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 4967 | #endif |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 4968 | } |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4969 | pNew->prereq = saved_prereq; |
| 4970 | pNew->u.btree.nEq = saved_nEq; |
drh | c8bbce1 | 2014-10-21 01:05:09 +0000 | [diff] [blame] | 4971 | pNew->nSkip = saved_nSkip; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 4972 | pNew->wsFlags = saved_wsFlags; |
| 4973 | pNew->nOut = saved_nOut; |
| 4974 | pNew->nLTerm = saved_nLTerm; |
drh | c8bbce1 | 2014-10-21 01:05:09 +0000 | [diff] [blame] | 4975 | |
| 4976 | /* Consider using a skip-scan if there are no WHERE clause constraints |
| 4977 | ** available for the left-most terms of the index, and if the average |
| 4978 | ** number of repeats in the left-most terms is at least 18. |
| 4979 | ** |
| 4980 | ** The magic number 18 is selected on the basis that scanning 17 rows |
| 4981 | ** is almost always quicker than an index seek (even though if the index |
| 4982 | ** contains fewer than 2^17 rows we assume otherwise in other parts of |
| 4983 | ** the code). And, even if it is not, it should not be too much slower. |
| 4984 | ** On the other hand, the extra seeks could end up being significantly |
| 4985 | ** more expensive. */ |
| 4986 | assert( 42==sqlite3LogEst(18) ); |
| 4987 | if( saved_nEq==saved_nSkip |
| 4988 | && saved_nEq+1<pProbe->nKeyCol |
drh | f9df2fb | 2014-11-15 19:08:13 +0000 | [diff] [blame] | 4989 | && pProbe->noSkipScan==0 |
drh | c8bbce1 | 2014-10-21 01:05:09 +0000 | [diff] [blame] | 4990 | && pProbe->aiRowLogEst[saved_nEq+1]>=42 /* TUNING: Minimum for skip-scan */ |
| 4991 | && (rc = whereLoopResize(db, pNew, pNew->nLTerm+1))==SQLITE_OK |
| 4992 | ){ |
| 4993 | LogEst nIter; |
| 4994 | pNew->u.btree.nEq++; |
| 4995 | pNew->nSkip++; |
| 4996 | pNew->aLTerm[pNew->nLTerm++] = 0; |
| 4997 | pNew->wsFlags |= WHERE_SKIPSCAN; |
| 4998 | nIter = pProbe->aiRowLogEst[saved_nEq] - pProbe->aiRowLogEst[saved_nEq+1]; |
drh | c8bbce1 | 2014-10-21 01:05:09 +0000 | [diff] [blame] | 4999 | pNew->nOut -= nIter; |
| 5000 | /* TUNING: Because uncertainties in the estimates for skip-scan queries, |
| 5001 | ** add a 1.375 fudge factor to make skip-scan slightly less likely. */ |
| 5002 | nIter += 5; |
| 5003 | whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nIter + nInMul); |
| 5004 | pNew->nOut = saved_nOut; |
| 5005 | pNew->u.btree.nEq = saved_nEq; |
| 5006 | pNew->nSkip = saved_nSkip; |
| 5007 | pNew->wsFlags = saved_wsFlags; |
| 5008 | } |
| 5009 | |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 5010 | return rc; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 5011 | } |
| 5012 | |
| 5013 | /* |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 5014 | ** Return True if it is possible that pIndex might be useful in |
| 5015 | ** implementing the ORDER BY clause in pBuilder. |
| 5016 | ** |
| 5017 | ** Return False if pBuilder does not contain an ORDER BY clause or |
| 5018 | ** if there is no way for pIndex to be useful in implementing that |
| 5019 | ** ORDER BY clause. |
| 5020 | */ |
| 5021 | static int indexMightHelpWithOrderBy( |
| 5022 | WhereLoopBuilder *pBuilder, |
| 5023 | Index *pIndex, |
| 5024 | int iCursor |
| 5025 | ){ |
| 5026 | ExprList *pOB; |
drh | 6d38147 | 2013-06-13 17:58:08 +0000 | [diff] [blame] | 5027 | int ii, jj; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 5028 | |
drh | 53cfbe9 | 2013-06-13 17:28:22 +0000 | [diff] [blame] | 5029 | if( pIndex->bUnordered ) return 0; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 5030 | if( (pOB = pBuilder->pWInfo->pOrderBy)==0 ) return 0; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 5031 | for(ii=0; ii<pOB->nExpr; ii++){ |
drh | 45c154a | 2013-06-03 20:46:35 +0000 | [diff] [blame] | 5032 | Expr *pExpr = sqlite3ExprSkipCollate(pOB->a[ii].pExpr); |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 5033 | if( pExpr->op!=TK_COLUMN ) return 0; |
| 5034 | if( pExpr->iTable==iCursor ){ |
drh | 137fd4f | 2014-09-19 02:01:37 +0000 | [diff] [blame] | 5035 | if( pExpr->iColumn<0 ) return 1; |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 5036 | for(jj=0; jj<pIndex->nKeyCol; jj++){ |
drh | 6d38147 | 2013-06-13 17:58:08 +0000 | [diff] [blame] | 5037 | if( pExpr->iColumn==pIndex->aiColumn[jj] ) return 1; |
| 5038 | } |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 5039 | } |
| 5040 | } |
| 5041 | return 0; |
| 5042 | } |
| 5043 | |
| 5044 | /* |
drh | 92a121f | 2013-06-10 12:15:47 +0000 | [diff] [blame] | 5045 | ** Return a bitmask where 1s indicate that the corresponding column of |
| 5046 | ** the table is used by an index. Only the first 63 columns are considered. |
| 5047 | */ |
drh | fd5874d | 2013-06-12 14:52:39 +0000 | [diff] [blame] | 5048 | static Bitmask columnsInIndex(Index *pIdx){ |
drh | 92a121f | 2013-06-10 12:15:47 +0000 | [diff] [blame] | 5049 | Bitmask m = 0; |
| 5050 | int j; |
drh | ec95c44 | 2013-10-23 01:57:32 +0000 | [diff] [blame] | 5051 | for(j=pIdx->nColumn-1; j>=0; j--){ |
drh | 92a121f | 2013-06-10 12:15:47 +0000 | [diff] [blame] | 5052 | int x = pIdx->aiColumn[j]; |
drh | ec95c44 | 2013-10-23 01:57:32 +0000 | [diff] [blame] | 5053 | if( x>=0 ){ |
| 5054 | testcase( x==BMS-1 ); |
| 5055 | testcase( x==BMS-2 ); |
| 5056 | if( x<BMS-1 ) m |= MASKBIT(x); |
| 5057 | } |
drh | 92a121f | 2013-06-10 12:15:47 +0000 | [diff] [blame] | 5058 | } |
| 5059 | return m; |
| 5060 | } |
| 5061 | |
drh | 4bd5f73 | 2013-07-31 23:22:39 +0000 | [diff] [blame] | 5062 | /* Check to see if a partial index with pPartIndexWhere can be used |
| 5063 | ** in the current query. Return true if it can be and false if not. |
| 5064 | */ |
| 5065 | static int whereUsablePartialIndex(int iTab, WhereClause *pWC, Expr *pWhere){ |
| 5066 | int i; |
| 5067 | WhereTerm *pTerm; |
| 5068 | for(i=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){ |
dan | 2a45cb5 | 2015-02-24 20:10:49 +0000 | [diff] [blame] | 5069 | Expr *pExpr = pTerm->pExpr; |
| 5070 | if( sqlite3ExprImpliesExpr(pExpr, pWhere, iTab) |
| 5071 | && (!ExprHasProperty(pExpr, EP_FromJoin) || pExpr->iRightJoinTable==iTab) |
drh | 077f06e | 2015-02-24 16:48:59 +0000 | [diff] [blame] | 5072 | ){ |
| 5073 | return 1; |
| 5074 | } |
drh | 4bd5f73 | 2013-07-31 23:22:39 +0000 | [diff] [blame] | 5075 | } |
| 5076 | return 0; |
| 5077 | } |
drh | 92a121f | 2013-06-10 12:15:47 +0000 | [diff] [blame] | 5078 | |
| 5079 | /* |
dan | 51576f4 | 2013-07-02 10:06:15 +0000 | [diff] [blame] | 5080 | ** 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] | 5081 | ** is idenfied by pBuilder->pNew->iTab. That table is guaranteed to be |
| 5082 | ** a b-tree table, not a virtual table. |
dan | 8164722 | 2014-04-30 15:00:16 +0000 | [diff] [blame] | 5083 | ** |
| 5084 | ** The costs (WhereLoop.rRun) of the b-tree loops added by this function |
| 5085 | ** are calculated as follows: |
| 5086 | ** |
| 5087 | ** For a full scan, assuming the table (or index) contains nRow rows: |
| 5088 | ** |
| 5089 | ** cost = nRow * 3.0 // full-table scan |
| 5090 | ** cost = nRow * K // scan of covering index |
| 5091 | ** cost = nRow * (K+3.0) // scan of non-covering index |
| 5092 | ** |
| 5093 | ** where K is a value between 1.1 and 3.0 set based on the relative |
| 5094 | ** estimated average size of the index and table records. |
| 5095 | ** |
| 5096 | ** For an index scan, where nVisit is the number of index rows visited |
| 5097 | ** by the scan, and nSeek is the number of seek operations required on |
| 5098 | ** the index b-tree: |
| 5099 | ** |
| 5100 | ** cost = nSeek * (log(nRow) + K * nVisit) // covering index |
| 5101 | ** cost = nSeek * (log(nRow) + (K+3.0) * nVisit) // non-covering index |
| 5102 | ** |
| 5103 | ** Normally, nSeek is 1. nSeek values greater than 1 come about if the |
| 5104 | ** WHERE clause includes "x IN (....)" terms used in place of "x=?". Or when |
| 5105 | ** implicit "x IN (SELECT x FROM tbl)" terms are added for skip-scans. |
drh | 83a305f | 2014-07-22 12:05:32 +0000 | [diff] [blame] | 5106 | ** |
| 5107 | ** The estimated values (nRow, nVisit, nSeek) often contain a large amount |
| 5108 | ** of uncertainty. For this reason, scoring is designed to pick plans that |
| 5109 | ** "do the least harm" if the estimates are inaccurate. For example, a |
| 5110 | ** log(nRow) factor is omitted from a non-covering index scan in order to |
| 5111 | ** bias the scoring in favor of using an index, since the worst-case |
| 5112 | ** performance of using an index is far better than the worst-case performance |
| 5113 | ** of a full table scan. |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 5114 | */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 5115 | static int whereLoopAddBtree( |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 5116 | WhereLoopBuilder *pBuilder, /* WHERE clause information */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 5117 | Bitmask mExtra /* Extra prerequesites for using this table */ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 5118 | ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 5119 | WhereInfo *pWInfo; /* WHERE analysis context */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 5120 | Index *pProbe; /* An index we are evaluating */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 5121 | Index sPk; /* A fake index object for the primary key */ |
dan | cfc9df7 | 2014-04-25 15:01:01 +0000 | [diff] [blame] | 5122 | LogEst aiRowEstPk[2]; /* The aiRowLogEst[] value for the sPk index */ |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 5123 | i16 aiColumnPk = -1; /* The aColumn[] value for the sPk index */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 5124 | SrcList *pTabList; /* The FROM clause */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 5125 | struct SrcList_item *pSrc; /* The FROM clause btree term to add */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 5126 | WhereLoop *pNew; /* Template WhereLoop object */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 5127 | int rc = SQLITE_OK; /* Return code */ |
drh | d044d20 | 2013-05-31 12:43:55 +0000 | [diff] [blame] | 5128 | int iSortIdx = 1; /* Index number */ |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 5129 | int b; /* A boolean value */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 5130 | LogEst rSize; /* number of rows in the table */ |
| 5131 | LogEst rLogSize; /* Logarithm of the number of rows in the table */ |
drh | 4bd5f73 | 2013-07-31 23:22:39 +0000 | [diff] [blame] | 5132 | WhereClause *pWC; /* The parsed WHERE clause */ |
drh | 3495d20 | 2013-10-07 17:32:15 +0000 | [diff] [blame] | 5133 | Table *pTab; /* Table being queried */ |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 5134 | |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 5135 | pNew = pBuilder->pNew; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 5136 | pWInfo = pBuilder->pWInfo; |
| 5137 | pTabList = pWInfo->pTabList; |
| 5138 | pSrc = pTabList->a + pNew->iTab; |
drh | 3495d20 | 2013-10-07 17:32:15 +0000 | [diff] [blame] | 5139 | pTab = pSrc->pTab; |
drh | 4bd5f73 | 2013-07-31 23:22:39 +0000 | [diff] [blame] | 5140 | pWC = pBuilder->pWC; |
drh | 0823c89 | 2013-05-11 00:06:23 +0000 | [diff] [blame] | 5141 | assert( !IsVirtual(pSrc->pTab) ); |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 5142 | |
| 5143 | if( pSrc->pIndex ){ |
| 5144 | /* An INDEXED BY clause specifies a particular index to use */ |
| 5145 | pProbe = pSrc->pIndex; |
drh | ec95c44 | 2013-10-23 01:57:32 +0000 | [diff] [blame] | 5146 | }else if( !HasRowid(pTab) ){ |
| 5147 | pProbe = pTab->pIndex; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 5148 | }else{ |
| 5149 | /* There is no INDEXED BY clause. Create a fake Index object in local |
| 5150 | ** variable sPk to represent the rowid primary key index. Make this |
| 5151 | ** fake index the first in a chain of Index objects with all of the real |
| 5152 | ** indices to follow */ |
| 5153 | Index *pFirst; /* First of real indices on the table */ |
| 5154 | memset(&sPk, 0, sizeof(Index)); |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 5155 | sPk.nKeyCol = 1; |
dan | 39129ce | 2014-06-30 15:23:57 +0000 | [diff] [blame] | 5156 | sPk.nColumn = 1; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 5157 | sPk.aiColumn = &aiColumnPk; |
dan | cfc9df7 | 2014-04-25 15:01:01 +0000 | [diff] [blame] | 5158 | sPk.aiRowLogEst = aiRowEstPk; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 5159 | sPk.onError = OE_Replace; |
drh | 3495d20 | 2013-10-07 17:32:15 +0000 | [diff] [blame] | 5160 | sPk.pTable = pTab; |
dan | aa9933c | 2014-04-24 20:04:49 +0000 | [diff] [blame] | 5161 | sPk.szIdxRow = pTab->szTabRow; |
dan | cfc9df7 | 2014-04-25 15:01:01 +0000 | [diff] [blame] | 5162 | aiRowEstPk[0] = pTab->nRowLogEst; |
| 5163 | aiRowEstPk[1] = 0; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 5164 | pFirst = pSrc->pTab->pIndex; |
| 5165 | if( pSrc->notIndexed==0 ){ |
| 5166 | /* The real indices of the table are only considered if the |
| 5167 | ** NOT INDEXED qualifier is omitted from the FROM clause */ |
| 5168 | sPk.pNext = pFirst; |
| 5169 | } |
| 5170 | pProbe = &sPk; |
| 5171 | } |
dan | cfc9df7 | 2014-04-25 15:01:01 +0000 | [diff] [blame] | 5172 | rSize = pTab->nRowLogEst; |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 5173 | rLogSize = estLog(rSize); |
| 5174 | |
drh | feb56e0 | 2013-08-23 17:33:46 +0000 | [diff] [blame] | 5175 | #ifndef SQLITE_OMIT_AUTOMATIC_INDEX |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 5176 | /* Automatic indexes */ |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 5177 | if( !pBuilder->pOrSet |
drh | 8e8e7ef | 2015-03-02 17:25:00 +0000 | [diff] [blame] | 5178 | && (pWInfo->wctrlFlags & WHERE_NO_AUTOINDEX)==0 |
drh | 4fe425a | 2013-06-12 17:08:06 +0000 | [diff] [blame] | 5179 | && (pWInfo->pParse->db->flags & SQLITE_AutoIndex)!=0 |
| 5180 | && pSrc->pIndex==0 |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 5181 | && !pSrc->viaCoroutine |
| 5182 | && !pSrc->notIndexed |
drh | ec95c44 | 2013-10-23 01:57:32 +0000 | [diff] [blame] | 5183 | && HasRowid(pTab) |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 5184 | && !pSrc->isCorrelated |
dan | 62ba4e4 | 2014-01-15 18:21:41 +0000 | [diff] [blame] | 5185 | && !pSrc->isRecursive |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 5186 | ){ |
| 5187 | /* Generate auto-index WhereLoops */ |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 5188 | WhereTerm *pTerm; |
| 5189 | WhereTerm *pWCEnd = pWC->a + pWC->nTerm; |
| 5190 | for(pTerm=pWC->a; rc==SQLITE_OK && pTerm<pWCEnd; pTerm++){ |
drh | 79a13bf | 2013-05-31 20:28:28 +0000 | [diff] [blame] | 5191 | if( pTerm->prereqRight & pNew->maskSelf ) continue; |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 5192 | if( termCanDriveIndex(pTerm, pSrc, 0) ){ |
| 5193 | pNew->u.btree.nEq = 1; |
drh | c8bbce1 | 2014-10-21 01:05:09 +0000 | [diff] [blame] | 5194 | pNew->nSkip = 0; |
drh | ef86637 | 2013-05-22 20:49:02 +0000 | [diff] [blame] | 5195 | pNew->u.btree.pIndex = 0; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 5196 | pNew->nLTerm = 1; |
| 5197 | pNew->aLTerm[0] = pTerm; |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 5198 | /* TUNING: One-time cost for computing the automatic index is |
drh | 7e07433 | 2014-09-22 14:30:51 +0000 | [diff] [blame] | 5199 | ** estimated to be X*N*log2(N) where N is the number of rows in |
| 5200 | ** the table being indexed and where X is 7 (LogEst=28) for normal |
| 5201 | ** tables or 1.375 (LogEst=4) for views and subqueries. The value |
| 5202 | ** of X is smaller for views and subqueries so that the query planner |
| 5203 | ** will be more aggressive about generating automatic indexes for |
| 5204 | ** those objects, since there is no opportunity to add schema |
| 5205 | ** indexes on subqueries and views. */ |
| 5206 | pNew->rSetup = rLogSize + rSize + 4; |
| 5207 | if( pTab->pSelect==0 && (pTab->tabFlags & TF_Ephemeral)==0 ){ |
| 5208 | pNew->rSetup += 24; |
| 5209 | } |
drh | dbd9486 | 2014-07-23 23:57:42 +0000 | [diff] [blame] | 5210 | ApplyCostMultiplier(pNew->rSetup, pTab->costMult); |
drh | 986b387 | 2013-06-28 21:12:20 +0000 | [diff] [blame] | 5211 | /* TUNING: Each index lookup yields 20 rows in the table. This |
| 5212 | ** 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] | 5213 | ** of knowing how selective the index will ultimately be. It would |
drh | 986b387 | 2013-06-28 21:12:20 +0000 | [diff] [blame] | 5214 | ** not be unreasonable to make this value much larger. */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 5215 | pNew->nOut = 43; assert( 43==sqlite3LogEst(20) ); |
drh | b50596d | 2013-10-08 20:42:41 +0000 | [diff] [blame] | 5216 | pNew->rRun = sqlite3LogEstAdd(rLogSize,pNew->nOut); |
drh | 986b387 | 2013-06-28 21:12:20 +0000 | [diff] [blame] | 5217 | pNew->wsFlags = WHERE_AUTO_INDEX; |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 5218 | pNew->prereq = mExtra | pTerm->prereqRight; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 5219 | rc = whereLoopInsert(pBuilder, pNew); |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 5220 | } |
| 5221 | } |
| 5222 | } |
drh | feb56e0 | 2013-08-23 17:33:46 +0000 | [diff] [blame] | 5223 | #endif /* SQLITE_OMIT_AUTOMATIC_INDEX */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 5224 | |
| 5225 | /* Loop over all indices |
| 5226 | */ |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 5227 | for(; rc==SQLITE_OK && pProbe; pProbe=pProbe->pNext, iSortIdx++){ |
drh | 4bd5f73 | 2013-07-31 23:22:39 +0000 | [diff] [blame] | 5228 | if( pProbe->pPartIdxWhere!=0 |
dan | 0829169 | 2014-08-27 17:37:20 +0000 | [diff] [blame] | 5229 | && !whereUsablePartialIndex(pSrc->iCursor, pWC, pProbe->pPartIdxWhere) ){ |
| 5230 | testcase( pNew->iTab!=pSrc->iCursor ); /* See ticket [98d973b8f5] */ |
drh | 4bd5f73 | 2013-07-31 23:22:39 +0000 | [diff] [blame] | 5231 | continue; /* Partial index inappropriate for this query */ |
| 5232 | } |
dan | 7de2a1f | 2014-04-28 20:11:20 +0000 | [diff] [blame] | 5233 | rSize = pProbe->aiRowLogEst[0]; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 5234 | pNew->u.btree.nEq = 0; |
drh | c8bbce1 | 2014-10-21 01:05:09 +0000 | [diff] [blame] | 5235 | pNew->nSkip = 0; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 5236 | pNew->nLTerm = 0; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 5237 | pNew->iSortIdx = 0; |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5238 | pNew->rSetup = 0; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 5239 | pNew->prereq = mExtra; |
drh | 74f91d4 | 2013-06-19 18:01:44 +0000 | [diff] [blame] | 5240 | pNew->nOut = rSize; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 5241 | pNew->u.btree.pIndex = pProbe; |
| 5242 | b = indexMightHelpWithOrderBy(pBuilder, pProbe, pSrc->iCursor); |
drh | 53cfbe9 | 2013-06-13 17:28:22 +0000 | [diff] [blame] | 5243 | /* The ONEPASS_DESIRED flags never occurs together with ORDER BY */ |
| 5244 | assert( (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || b==0 ); |
drh | 43fe25f | 2013-05-07 23:06:23 +0000 | [diff] [blame] | 5245 | if( pProbe->tnum<=0 ){ |
| 5246 | /* Integer primary key index */ |
| 5247 | pNew->wsFlags = WHERE_IPK; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 5248 | |
| 5249 | /* Full table scan */ |
drh | d044d20 | 2013-05-31 12:43:55 +0000 | [diff] [blame] | 5250 | pNew->iSortIdx = b ? iSortIdx : 0; |
dan | aa9933c | 2014-04-24 20:04:49 +0000 | [diff] [blame] | 5251 | /* TUNING: Cost of full table scan is (N*3.0). */ |
| 5252 | pNew->rRun = rSize + 16; |
drh | dbd9486 | 2014-07-23 23:57:42 +0000 | [diff] [blame] | 5253 | ApplyCostMultiplier(pNew->rRun, pTab->costMult); |
drh | d8b77e2 | 2014-09-06 01:35:57 +0000 | [diff] [blame] | 5254 | whereLoopOutputAdjust(pWC, pNew, rSize); |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 5255 | rc = whereLoopInsert(pBuilder, pNew); |
drh | cca9f3d | 2013-09-06 15:23:29 +0000 | [diff] [blame] | 5256 | pNew->nOut = rSize; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 5257 | if( rc ) break; |
drh | 43fe25f | 2013-05-07 23:06:23 +0000 | [diff] [blame] | 5258 | }else{ |
drh | ec95c44 | 2013-10-23 01:57:32 +0000 | [diff] [blame] | 5259 | Bitmask m; |
| 5260 | if( pProbe->isCovering ){ |
| 5261 | pNew->wsFlags = WHERE_IDX_ONLY | WHERE_INDEXED; |
| 5262 | m = 0; |
| 5263 | }else{ |
| 5264 | m = pSrc->colUsed & ~columnsInIndex(pProbe); |
| 5265 | pNew->wsFlags = (m==0) ? (WHERE_IDX_ONLY|WHERE_INDEXED) : WHERE_INDEXED; |
| 5266 | } |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 5267 | |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 5268 | /* Full scan via index */ |
drh | 53cfbe9 | 2013-06-13 17:28:22 +0000 | [diff] [blame] | 5269 | if( b |
drh | 702ba9f | 2013-11-07 21:25:13 +0000 | [diff] [blame] | 5270 | || !HasRowid(pTab) |
drh | 53cfbe9 | 2013-06-13 17:28:22 +0000 | [diff] [blame] | 5271 | || ( m==0 |
| 5272 | && pProbe->bUnordered==0 |
drh | 702ba9f | 2013-11-07 21:25:13 +0000 | [diff] [blame] | 5273 | && (pProbe->szIdxRow<pTab->szTabRow) |
drh | 53cfbe9 | 2013-06-13 17:28:22 +0000 | [diff] [blame] | 5274 | && (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 |
| 5275 | && sqlite3GlobalConfig.bUseCis |
| 5276 | && OptimizationEnabled(pWInfo->pParse->db, SQLITE_CoverIdxScan) |
| 5277 | ) |
drh | e3b7c92 | 2013-06-03 19:17:40 +0000 | [diff] [blame] | 5278 | ){ |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 5279 | pNew->iSortIdx = b ? iSortIdx : 0; |
dan | aa9933c | 2014-04-24 20:04:49 +0000 | [diff] [blame] | 5280 | |
| 5281 | /* The cost of visiting the index rows is N*K, where K is |
| 5282 | ** between 1.1 and 3.0, depending on the relative sizes of the |
| 5283 | ** index and table rows. If this is a non-covering index scan, |
| 5284 | ** also add the cost of visiting table rows (N*3.0). */ |
| 5285 | pNew->rRun = rSize + 1 + (15*pProbe->szIdxRow)/pTab->szTabRow; |
| 5286 | if( m!=0 ){ |
| 5287 | pNew->rRun = sqlite3LogEstAdd(pNew->rRun, rSize+16); |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 5288 | } |
drh | dbd9486 | 2014-07-23 23:57:42 +0000 | [diff] [blame] | 5289 | ApplyCostMultiplier(pNew->rRun, pTab->costMult); |
drh | d8b77e2 | 2014-09-06 01:35:57 +0000 | [diff] [blame] | 5290 | whereLoopOutputAdjust(pWC, pNew, rSize); |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 5291 | rc = whereLoopInsert(pBuilder, pNew); |
drh | cca9f3d | 2013-09-06 15:23:29 +0000 | [diff] [blame] | 5292 | pNew->nOut = rSize; |
drh | 23f98da | 2013-05-21 15:52:07 +0000 | [diff] [blame] | 5293 | if( rc ) break; |
| 5294 | } |
| 5295 | } |
dan | 7a41923 | 2013-08-06 20:01:43 +0000 | [diff] [blame] | 5296 | |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5297 | rc = whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, 0); |
drh | 1435a9a | 2013-08-27 23:15:44 +0000 | [diff] [blame] | 5298 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
dan | 87cd932 | 2013-08-07 15:52:41 +0000 | [diff] [blame] | 5299 | sqlite3Stat4ProbeFree(pBuilder->pRec); |
| 5300 | pBuilder->nRecValid = 0; |
| 5301 | pBuilder->pRec = 0; |
dan | ddc2d6e | 2013-08-06 20:15:06 +0000 | [diff] [blame] | 5302 | #endif |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 5303 | |
| 5304 | /* If there was an INDEXED BY clause, then only that one index is |
| 5305 | ** considered. */ |
| 5306 | if( pSrc->pIndex ) break; |
| 5307 | } |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 5308 | return rc; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 5309 | } |
| 5310 | |
drh | 8636e9c | 2013-06-11 01:50:08 +0000 | [diff] [blame] | 5311 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 5312 | /* |
drh | 0823c89 | 2013-05-11 00:06:23 +0000 | [diff] [blame] | 5313 | ** Add all WhereLoop objects for a table of the join identified by |
| 5314 | ** pBuilder->pNew->iTab. That table is guaranteed to be a virtual table. |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 5315 | */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 5316 | static int whereLoopAddVirtual( |
dan | ff4b23b | 2013-11-12 12:17:16 +0000 | [diff] [blame] | 5317 | WhereLoopBuilder *pBuilder, /* WHERE clause information */ |
| 5318 | Bitmask mExtra |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 5319 | ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 5320 | WhereInfo *pWInfo; /* WHERE analysis context */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 5321 | Parse *pParse; /* The parsing context */ |
| 5322 | WhereClause *pWC; /* The WHERE clause */ |
| 5323 | struct SrcList_item *pSrc; /* The FROM clause term to search */ |
| 5324 | Table *pTab; |
| 5325 | sqlite3 *db; |
| 5326 | sqlite3_index_info *pIdxInfo; |
| 5327 | struct sqlite3_index_constraint *pIdxCons; |
| 5328 | struct sqlite3_index_constraint_usage *pUsage; |
| 5329 | WhereTerm *pTerm; |
| 5330 | int i, j; |
| 5331 | int iTerm, mxTerm; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 5332 | int nConstraint; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 5333 | int seenIn = 0; /* True if an IN operator is seen */ |
| 5334 | int seenVar = 0; /* True if a non-constant constraint is seen */ |
| 5335 | int iPhase; /* 0: const w/o IN, 1: const, 2: no IN, 2: IN */ |
| 5336 | WhereLoop *pNew; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 5337 | int rc = SQLITE_OK; |
| 5338 | |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 5339 | pWInfo = pBuilder->pWInfo; |
| 5340 | pParse = pWInfo->pParse; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 5341 | db = pParse->db; |
| 5342 | pWC = pBuilder->pWC; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 5343 | pNew = pBuilder->pNew; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 5344 | pSrc = &pWInfo->pTabList->a[pNew->iTab]; |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 5345 | pTab = pSrc->pTab; |
drh | 0823c89 | 2013-05-11 00:06:23 +0000 | [diff] [blame] | 5346 | assert( IsVirtual(pTab) ); |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 5347 | pIdxInfo = allocateIndexInfo(pParse, pWC, pSrc, pBuilder->pOrderBy); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 5348 | if( pIdxInfo==0 ) return SQLITE_NOMEM; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 5349 | pNew->prereq = 0; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 5350 | pNew->rSetup = 0; |
| 5351 | pNew->wsFlags = WHERE_VIRTUALTABLE; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 5352 | pNew->nLTerm = 0; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 5353 | pNew->u.vtab.needFree = 0; |
| 5354 | pUsage = pIdxInfo->aConstraintUsage; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 5355 | nConstraint = pIdxInfo->nConstraint; |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 5356 | if( whereLoopResize(db, pNew, nConstraint) ){ |
| 5357 | sqlite3DbFree(db, pIdxInfo); |
| 5358 | return SQLITE_NOMEM; |
| 5359 | } |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 5360 | |
drh | 0823c89 | 2013-05-11 00:06:23 +0000 | [diff] [blame] | 5361 | for(iPhase=0; iPhase<=3; iPhase++){ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 5362 | if( !seenIn && (iPhase&1)!=0 ){ |
| 5363 | iPhase++; |
| 5364 | if( iPhase>3 ) break; |
| 5365 | } |
| 5366 | if( !seenVar && iPhase>1 ) break; |
| 5367 | pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint; |
| 5368 | for(i=0; i<pIdxInfo->nConstraint; i++, pIdxCons++){ |
| 5369 | j = pIdxCons->iTermOffset; |
| 5370 | pTerm = &pWC->a[j]; |
| 5371 | switch( iPhase ){ |
| 5372 | case 0: /* Constants without IN operator */ |
| 5373 | pIdxCons->usable = 0; |
| 5374 | if( (pTerm->eOperator & WO_IN)!=0 ){ |
| 5375 | seenIn = 1; |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 5376 | } |
| 5377 | if( pTerm->prereqRight!=0 ){ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 5378 | seenVar = 1; |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 5379 | }else if( (pTerm->eOperator & WO_IN)==0 ){ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 5380 | pIdxCons->usable = 1; |
| 5381 | } |
| 5382 | break; |
| 5383 | case 1: /* Constants with IN operators */ |
| 5384 | assert( seenIn ); |
| 5385 | pIdxCons->usable = (pTerm->prereqRight==0); |
| 5386 | break; |
| 5387 | case 2: /* Variables without IN */ |
| 5388 | assert( seenVar ); |
| 5389 | pIdxCons->usable = (pTerm->eOperator & WO_IN)==0; |
| 5390 | break; |
| 5391 | default: /* Variables with IN */ |
| 5392 | assert( seenVar && seenIn ); |
| 5393 | pIdxCons->usable = 1; |
| 5394 | break; |
| 5395 | } |
| 5396 | } |
| 5397 | memset(pUsage, 0, sizeof(pUsage[0])*pIdxInfo->nConstraint); |
| 5398 | if( pIdxInfo->needToFreeIdxStr ) sqlite3_free(pIdxInfo->idxStr); |
| 5399 | pIdxInfo->idxStr = 0; |
| 5400 | pIdxInfo->idxNum = 0; |
| 5401 | pIdxInfo->needToFreeIdxStr = 0; |
| 5402 | pIdxInfo->orderByConsumed = 0; |
drh | 8636e9c | 2013-06-11 01:50:08 +0000 | [diff] [blame] | 5403 | pIdxInfo->estimatedCost = SQLITE_BIG_DBL / (double)2; |
dan | a9f5815 | 2013-11-11 19:01:33 +0000 | [diff] [blame] | 5404 | pIdxInfo->estimatedRows = 25; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 5405 | rc = vtabBestIndex(pParse, pTab, pIdxInfo); |
| 5406 | if( rc ) goto whereLoopAddVtab_exit; |
| 5407 | pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint; |
dan | ff4b23b | 2013-11-12 12:17:16 +0000 | [diff] [blame] | 5408 | pNew->prereq = mExtra; |
drh | c718f1c | 2013-05-08 20:05:58 +0000 | [diff] [blame] | 5409 | mxTerm = -1; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 5410 | assert( pNew->nLSlot>=nConstraint ); |
| 5411 | for(i=0; i<nConstraint; i++) pNew->aLTerm[i] = 0; |
drh | 3bd26f0 | 2013-05-24 14:52:03 +0000 | [diff] [blame] | 5412 | pNew->u.vtab.omitMask = 0; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 5413 | for(i=0; i<nConstraint; i++, pIdxCons++){ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 5414 | if( (iTerm = pUsage[i].argvIndex - 1)>=0 ){ |
| 5415 | j = pIdxCons->iTermOffset; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 5416 | if( iTerm>=nConstraint |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 5417 | || j<0 |
| 5418 | || j>=pWC->nTerm |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 5419 | || pNew->aLTerm[iTerm]!=0 |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 5420 | ){ |
| 5421 | rc = SQLITE_ERROR; |
| 5422 | sqlite3ErrorMsg(pParse, "%s.xBestIndex() malfunction", pTab->zName); |
| 5423 | goto whereLoopAddVtab_exit; |
| 5424 | } |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 5425 | testcase( iTerm==nConstraint-1 ); |
| 5426 | testcase( j==0 ); |
| 5427 | testcase( j==pWC->nTerm-1 ); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 5428 | pTerm = &pWC->a[j]; |
| 5429 | pNew->prereq |= pTerm->prereqRight; |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 5430 | assert( iTerm<pNew->nLSlot ); |
| 5431 | pNew->aLTerm[iTerm] = pTerm; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 5432 | if( iTerm>mxTerm ) mxTerm = iTerm; |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 5433 | testcase( iTerm==15 ); |
| 5434 | testcase( iTerm==16 ); |
drh | 5298630 | 2013-06-03 16:03:16 +0000 | [diff] [blame] | 5435 | if( iTerm<16 && pUsage[i].omit ) pNew->u.vtab.omitMask |= 1<<iTerm; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 5436 | if( (pTerm->eOperator & WO_IN)!=0 ){ |
| 5437 | if( pUsage[i].omit==0 ){ |
| 5438 | /* Do not attempt to use an IN constraint if the virtual table |
| 5439 | ** says that the equivalent EQ constraint cannot be safely omitted. |
| 5440 | ** If we do attempt to use such a constraint, some rows might be |
| 5441 | ** repeated in the output. */ |
| 5442 | break; |
| 5443 | } |
| 5444 | /* A virtual table that is constrained by an IN clause may not |
| 5445 | ** consume the ORDER BY clause because (1) the order of IN terms |
| 5446 | ** is not necessarily related to the order of output terms and |
| 5447 | ** (2) Multiple outputs from a single IN value will not merge |
| 5448 | ** together. */ |
| 5449 | pIdxInfo->orderByConsumed = 0; |
| 5450 | } |
| 5451 | } |
| 5452 | } |
drh | 4efc929 | 2013-06-06 23:02:03 +0000 | [diff] [blame] | 5453 | if( i>=nConstraint ){ |
| 5454 | pNew->nLTerm = mxTerm+1; |
| 5455 | assert( pNew->nLTerm<=pNew->nLSlot ); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 5456 | pNew->u.vtab.idxNum = pIdxInfo->idxNum; |
| 5457 | pNew->u.vtab.needFree = pIdxInfo->needToFreeIdxStr; |
| 5458 | pIdxInfo->needToFreeIdxStr = 0; |
| 5459 | pNew->u.vtab.idxStr = pIdxInfo->idxStr; |
drh | 0401ace | 2014-03-18 15:30:27 +0000 | [diff] [blame] | 5460 | pNew->u.vtab.isOrdered = (i8)(pIdxInfo->orderByConsumed ? |
| 5461 | pIdxInfo->nOrderBy : 0); |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5462 | pNew->rSetup = 0; |
drh | b50596d | 2013-10-08 20:42:41 +0000 | [diff] [blame] | 5463 | pNew->rRun = sqlite3LogEstFromDouble(pIdxInfo->estimatedCost); |
dan | a9f5815 | 2013-11-11 19:01:33 +0000 | [diff] [blame] | 5464 | pNew->nOut = sqlite3LogEst(pIdxInfo->estimatedRows); |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 5465 | whereLoopInsert(pBuilder, pNew); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 5466 | if( pNew->u.vtab.needFree ){ |
| 5467 | sqlite3_free(pNew->u.vtab.idxStr); |
| 5468 | pNew->u.vtab.needFree = 0; |
| 5469 | } |
| 5470 | } |
| 5471 | } |
| 5472 | |
| 5473 | whereLoopAddVtab_exit: |
| 5474 | if( pIdxInfo->needToFreeIdxStr ) sqlite3_free(pIdxInfo->idxStr); |
| 5475 | sqlite3DbFree(db, pIdxInfo); |
| 5476 | return rc; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 5477 | } |
drh | 8636e9c | 2013-06-11 01:50:08 +0000 | [diff] [blame] | 5478 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 5479 | |
| 5480 | /* |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 5481 | ** Add WhereLoop entries to handle OR terms. This works for either |
| 5482 | ** btrees or virtual tables. |
| 5483 | */ |
| 5484 | static int whereLoopAddOr(WhereLoopBuilder *pBuilder, Bitmask mExtra){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 5485 | WhereInfo *pWInfo = pBuilder->pWInfo; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 5486 | WhereClause *pWC; |
| 5487 | WhereLoop *pNew; |
| 5488 | WhereTerm *pTerm, *pWCEnd; |
| 5489 | int rc = SQLITE_OK; |
| 5490 | int iCur; |
| 5491 | WhereClause tempWC; |
| 5492 | WhereLoopBuilder sSubBuild; |
dan | 5da73e1 | 2014-04-30 18:11:55 +0000 | [diff] [blame] | 5493 | WhereOrSet sSum, sCur; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 5494 | struct SrcList_item *pItem; |
| 5495 | |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 5496 | pWC = pBuilder->pWC; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 5497 | pWCEnd = pWC->a + pWC->nTerm; |
| 5498 | pNew = pBuilder->pNew; |
drh | 77dfd5b | 2013-08-19 11:15:48 +0000 | [diff] [blame] | 5499 | memset(&sSum, 0, sizeof(sSum)); |
drh | 186ad8c | 2013-10-08 18:40:37 +0000 | [diff] [blame] | 5500 | pItem = pWInfo->pTabList->a + pNew->iTab; |
| 5501 | iCur = pItem->iCursor; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 5502 | |
| 5503 | for(pTerm=pWC->a; pTerm<pWCEnd && rc==SQLITE_OK; pTerm++){ |
| 5504 | if( (pTerm->eOperator & WO_OR)!=0 |
| 5505 | && (pTerm->u.pOrInfo->indexable & pNew->maskSelf)!=0 |
| 5506 | ){ |
| 5507 | WhereClause * const pOrWC = &pTerm->u.pOrInfo->wc; |
| 5508 | WhereTerm * const pOrWCEnd = &pOrWC->a[pOrWC->nTerm]; |
| 5509 | WhereTerm *pOrTerm; |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 5510 | int once = 1; |
| 5511 | int i, j; |
drh | 783dece | 2013-06-05 17:53:43 +0000 | [diff] [blame] | 5512 | |
drh | 783dece | 2013-06-05 17:53:43 +0000 | [diff] [blame] | 5513 | sSubBuild = *pBuilder; |
| 5514 | sSubBuild.pOrderBy = 0; |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 5515 | sSubBuild.pOrSet = &sCur; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 5516 | |
drh | 0a99ba3 | 2014-09-30 17:03:35 +0000 | [diff] [blame] | 5517 | WHERETRACE(0x200, ("Begin processing OR-clause %p\n", pTerm)); |
drh | c7f0d22 | 2013-06-19 03:27:12 +0000 | [diff] [blame] | 5518 | for(pOrTerm=pOrWC->a; pOrTerm<pOrWCEnd; pOrTerm++){ |
drh | 783dece | 2013-06-05 17:53:43 +0000 | [diff] [blame] | 5519 | if( (pOrTerm->eOperator & WO_AND)!=0 ){ |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 5520 | sSubBuild.pWC = &pOrTerm->u.pAndInfo->wc; |
| 5521 | }else if( pOrTerm->leftCursor==iCur ){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 5522 | tempWC.pWInfo = pWC->pWInfo; |
drh | 783dece | 2013-06-05 17:53:43 +0000 | [diff] [blame] | 5523 | tempWC.pOuter = pWC; |
| 5524 | tempWC.op = TK_AND; |
drh | 783dece | 2013-06-05 17:53:43 +0000 | [diff] [blame] | 5525 | tempWC.nTerm = 1; |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 5526 | tempWC.a = pOrTerm; |
| 5527 | sSubBuild.pWC = &tempWC; |
| 5528 | }else{ |
| 5529 | continue; |
| 5530 | } |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 5531 | sCur.n = 0; |
drh | 5265149 | 2014-09-30 14:14:19 +0000 | [diff] [blame] | 5532 | #ifdef WHERETRACE_ENABLED |
drh | 0a99ba3 | 2014-09-30 17:03:35 +0000 | [diff] [blame] | 5533 | WHERETRACE(0x200, ("OR-term %d of %p has %d subterms:\n", |
| 5534 | (int)(pOrTerm-pOrWC->a), pTerm, sSubBuild.pWC->nTerm)); |
| 5535 | if( sqlite3WhereTrace & 0x400 ){ |
| 5536 | for(i=0; i<sSubBuild.pWC->nTerm; i++){ |
| 5537 | whereTermPrint(&sSubBuild.pWC->a[i], i); |
| 5538 | } |
drh | 5265149 | 2014-09-30 14:14:19 +0000 | [diff] [blame] | 5539 | } |
| 5540 | #endif |
drh | 8636e9c | 2013-06-11 01:50:08 +0000 | [diff] [blame] | 5541 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 5542 | if( IsVirtual(pItem->pTab) ){ |
dan | ff4b23b | 2013-11-12 12:17:16 +0000 | [diff] [blame] | 5543 | rc = whereLoopAddVirtual(&sSubBuild, mExtra); |
drh | 8636e9c | 2013-06-11 01:50:08 +0000 | [diff] [blame] | 5544 | }else |
| 5545 | #endif |
| 5546 | { |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 5547 | rc = whereLoopAddBtree(&sSubBuild, mExtra); |
| 5548 | } |
drh | 36be4c4 | 2014-09-30 17:31:23 +0000 | [diff] [blame] | 5549 | if( rc==SQLITE_OK ){ |
| 5550 | rc = whereLoopAddOr(&sSubBuild, mExtra); |
| 5551 | } |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 5552 | assert( rc==SQLITE_OK || sCur.n==0 ); |
| 5553 | if( sCur.n==0 ){ |
| 5554 | sSum.n = 0; |
| 5555 | break; |
| 5556 | }else if( once ){ |
| 5557 | whereOrMove(&sSum, &sCur); |
| 5558 | once = 0; |
| 5559 | }else{ |
dan | 5da73e1 | 2014-04-30 18:11:55 +0000 | [diff] [blame] | 5560 | WhereOrSet sPrev; |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 5561 | whereOrMove(&sPrev, &sSum); |
| 5562 | sSum.n = 0; |
| 5563 | for(i=0; i<sPrev.n; i++){ |
| 5564 | for(j=0; j<sCur.n; j++){ |
| 5565 | whereOrInsert(&sSum, sPrev.a[i].prereq | sCur.a[j].prereq, |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 5566 | sqlite3LogEstAdd(sPrev.a[i].rRun, sCur.a[j].rRun), |
| 5567 | sqlite3LogEstAdd(sPrev.a[i].nOut, sCur.a[j].nOut)); |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 5568 | } |
| 5569 | } |
| 5570 | } |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 5571 | } |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 5572 | pNew->nLTerm = 1; |
| 5573 | pNew->aLTerm[0] = pTerm; |
| 5574 | pNew->wsFlags = WHERE_MULTI_OR; |
| 5575 | pNew->rSetup = 0; |
| 5576 | pNew->iSortIdx = 0; |
| 5577 | memset(&pNew->u, 0, sizeof(pNew->u)); |
| 5578 | for(i=0; rc==SQLITE_OK && i<sSum.n; i++){ |
dan | 5da73e1 | 2014-04-30 18:11:55 +0000 | [diff] [blame] | 5579 | /* TUNING: Currently sSum.a[i].rRun is set to the sum of the costs |
| 5580 | ** of all sub-scans required by the OR-scan. However, due to rounding |
| 5581 | ** errors, it may be that the cost of the OR-scan is equal to its |
| 5582 | ** most expensive sub-scan. Add the smallest possible penalty |
| 5583 | ** (equivalent to multiplying the cost by 1.07) to ensure that |
| 5584 | ** this does not happen. Otherwise, for WHERE clauses such as the |
| 5585 | ** following where there is an index on "y": |
| 5586 | ** |
| 5587 | ** WHERE likelihood(x=?, 0.99) OR y=? |
| 5588 | ** |
| 5589 | ** the planner may elect to "OR" together a full-table scan and an |
| 5590 | ** index lookup. And other similarly odd results. */ |
| 5591 | pNew->rRun = sSum.a[i].rRun + 1; |
drh | aa32e3c | 2013-07-16 21:31:23 +0000 | [diff] [blame] | 5592 | pNew->nOut = sSum.a[i].nOut; |
| 5593 | pNew->prereq = sSum.a[i].prereq; |
drh | fd5874d | 2013-06-12 14:52:39 +0000 | [diff] [blame] | 5594 | rc = whereLoopInsert(pBuilder, pNew); |
| 5595 | } |
drh | 0a99ba3 | 2014-09-30 17:03:35 +0000 | [diff] [blame] | 5596 | WHERETRACE(0x200, ("End processing OR-clause %p\n", pTerm)); |
drh | cf8fa7a | 2013-05-10 20:26:22 +0000 | [diff] [blame] | 5597 | } |
| 5598 | } |
| 5599 | return rc; |
| 5600 | } |
| 5601 | |
| 5602 | /* |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 5603 | ** Add all WhereLoop objects for all tables |
| 5604 | */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 5605 | static int whereLoopAddAll(WhereLoopBuilder *pBuilder){ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 5606 | WhereInfo *pWInfo = pBuilder->pWInfo; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 5607 | Bitmask mExtra = 0; |
| 5608 | Bitmask mPrior = 0; |
| 5609 | int iTab; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 5610 | SrcList *pTabList = pWInfo->pTabList; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 5611 | struct SrcList_item *pItem; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 5612 | sqlite3 *db = pWInfo->pParse->db; |
| 5613 | int nTabList = pWInfo->nLevel; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 5614 | int rc = SQLITE_OK; |
drh | c63367e | 2013-06-10 20:46:50 +0000 | [diff] [blame] | 5615 | u8 priorJoinType = 0; |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5616 | WhereLoop *pNew; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 5617 | |
| 5618 | /* Loop over the tables in the join, from left to right */ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 5619 | pNew = pBuilder->pNew; |
drh | a201415 | 2013-06-07 00:29:23 +0000 | [diff] [blame] | 5620 | whereLoopInit(pNew); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5621 | for(iTab=0, pItem=pTabList->a; iTab<nTabList; iTab++, pItem++){ |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 5622 | pNew->iTab = iTab; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 5623 | pNew->maskSelf = getMask(&pWInfo->sMaskSet, pItem->iCursor); |
drh | c63367e | 2013-06-10 20:46:50 +0000 | [diff] [blame] | 5624 | if( ((pItem->jointype|priorJoinType) & (JT_LEFT|JT_CROSS))!=0 ){ |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 5625 | mExtra = mPrior; |
| 5626 | } |
drh | c63367e | 2013-06-10 20:46:50 +0000 | [diff] [blame] | 5627 | priorJoinType = pItem->jointype; |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 5628 | if( IsVirtual(pItem->pTab) ){ |
dan | ff4b23b | 2013-11-12 12:17:16 +0000 | [diff] [blame] | 5629 | rc = whereLoopAddVirtual(pBuilder, mExtra); |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 5630 | }else{ |
| 5631 | rc = whereLoopAddBtree(pBuilder, mExtra); |
| 5632 | } |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 5633 | if( rc==SQLITE_OK ){ |
| 5634 | rc = whereLoopAddOr(pBuilder, mExtra); |
| 5635 | } |
drh | b2a90f0 | 2013-05-10 03:30:49 +0000 | [diff] [blame] | 5636 | mPrior |= pNew->maskSelf; |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 5637 | if( rc || db->mallocFailed ) break; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 5638 | } |
drh | a201415 | 2013-06-07 00:29:23 +0000 | [diff] [blame] | 5639 | whereLoopClear(db, pNew); |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 5640 | return rc; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 5641 | } |
| 5642 | |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5643 | /* |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5644 | ** Examine a WherePath (with the addition of the extra WhereLoop of the 5th |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5645 | ** parameters) to see if it outputs rows in the requested ORDER BY |
drh | 0401ace | 2014-03-18 15:30:27 +0000 | [diff] [blame] | 5646 | ** (or GROUP BY) without requiring a separate sort operation. Return N: |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5647 | ** |
drh | 0401ace | 2014-03-18 15:30:27 +0000 | [diff] [blame] | 5648 | ** N>0: N terms of the ORDER BY clause are satisfied |
| 5649 | ** N==0: No terms of the ORDER BY clause are satisfied |
| 5650 | ** N<0: Unknown yet how many terms of ORDER BY might be satisfied. |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5651 | ** |
drh | 9443342 | 2013-07-01 11:05:50 +0000 | [diff] [blame] | 5652 | ** Note that processing for WHERE_GROUPBY and WHERE_DISTINCTBY is not as |
| 5653 | ** strict. With GROUP BY and DISTINCT the only requirement is that |
| 5654 | ** equivalent rows appear immediately adjacent to one another. GROUP BY |
dan | 374cd78 | 2014-04-21 13:21:56 +0000 | [diff] [blame] | 5655 | ** 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] | 5656 | ** as equivalent rows are grouped together. Thus for GROUP BY and DISTINCT |
drh | 9443342 | 2013-07-01 11:05:50 +0000 | [diff] [blame] | 5657 | ** the pOrderBy terms can be matched in any order. With ORDER BY, the |
| 5658 | ** pOrderBy terms must be matched in strict left-to-right order. |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5659 | */ |
drh | 0401ace | 2014-03-18 15:30:27 +0000 | [diff] [blame] | 5660 | static i8 wherePathSatisfiesOrderBy( |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5661 | WhereInfo *pWInfo, /* The WHERE clause */ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5662 | ExprList *pOrderBy, /* ORDER BY or GROUP BY or DISTINCT clause to check */ |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5663 | WherePath *pPath, /* The WherePath to check */ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5664 | u16 wctrlFlags, /* Might contain WHERE_GROUPBY or WHERE_DISTINCTBY */ |
| 5665 | u16 nLoop, /* Number of entries in pPath->aLoop[] */ |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5666 | WhereLoop *pLast, /* Add this WhereLoop to the end of pPath->aLoop[] */ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5667 | Bitmask *pRevMask /* OUT: Mask of WhereLoops to run in reverse order */ |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5668 | ){ |
drh | 88da644 | 2013-05-27 17:59:37 +0000 | [diff] [blame] | 5669 | u8 revSet; /* True if rev is known */ |
| 5670 | u8 rev; /* Composite sort order */ |
| 5671 | u8 revIdx; /* Index sort order */ |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5672 | u8 isOrderDistinct; /* All prior WhereLoops are order-distinct */ |
| 5673 | u8 distinctColumns; /* True if the loop has UNIQUE NOT NULL columns */ |
| 5674 | u8 isMatch; /* iColumn matches a term of the ORDER BY clause */ |
drh | 416846a | 2013-11-06 12:56:04 +0000 | [diff] [blame] | 5675 | u16 nKeyCol; /* Number of key columns in pIndex */ |
| 5676 | u16 nColumn; /* Total number of ordered columns in the index */ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5677 | u16 nOrderBy; /* Number terms in the ORDER BY clause */ |
| 5678 | int iLoop; /* Index of WhereLoop in pPath being processed */ |
| 5679 | int i, j; /* Loop counters */ |
| 5680 | int iCur; /* Cursor number for current WhereLoop */ |
| 5681 | int iColumn; /* A column number within table iCur */ |
drh | e8ae583 | 2013-06-19 13:32:46 +0000 | [diff] [blame] | 5682 | WhereLoop *pLoop = 0; /* Current WhereLoop being processed. */ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5683 | WhereTerm *pTerm; /* A single term of the WHERE clause */ |
| 5684 | Expr *pOBExpr; /* An expression from the ORDER BY clause */ |
| 5685 | CollSeq *pColl; /* COLLATE function from an ORDER BY clause term */ |
| 5686 | Index *pIndex; /* The index associated with pLoop */ |
| 5687 | sqlite3 *db = pWInfo->pParse->db; /* Database connection */ |
| 5688 | Bitmask obSat = 0; /* Mask of ORDER BY terms satisfied so far */ |
| 5689 | Bitmask obDone; /* Mask of all ORDER BY terms */ |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5690 | Bitmask orderDistinctMask; /* Mask of all well-ordered loops */ |
drh | b8916be | 2013-06-14 02:51:48 +0000 | [diff] [blame] | 5691 | Bitmask ready; /* Mask of inner loops */ |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5692 | |
| 5693 | /* |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5694 | ** We say the WhereLoop is "one-row" if it generates no more than one |
| 5695 | ** 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] | 5696 | ** (a) All index columns match with WHERE_COLUMN_EQ. |
| 5697 | ** (b) The index is unique |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5698 | ** Any WhereLoop with an WHERE_COLUMN_EQ constraint on the rowid is one-row. |
| 5699 | ** Every one-row WhereLoop will have the WHERE_ONEROW bit set in wsFlags. |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5700 | ** |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5701 | ** We say the WhereLoop is "order-distinct" if the set of columns from |
| 5702 | ** that WhereLoop that are in the ORDER BY clause are different for every |
| 5703 | ** row of the WhereLoop. Every one-row WhereLoop is automatically |
| 5704 | ** order-distinct. A WhereLoop that has no columns in the ORDER BY clause |
| 5705 | ** is not order-distinct. To be order-distinct is not quite the same as being |
| 5706 | ** UNIQUE since a UNIQUE column or index can have multiple rows that |
| 5707 | ** are NULL and NULL values are equivalent for the purpose of order-distinct. |
| 5708 | ** To be order-distinct, the columns must be UNIQUE and NOT NULL. |
| 5709 | ** |
| 5710 | ** The rowid for a table is always UNIQUE and NOT NULL so whenever the |
| 5711 | ** rowid appears in the ORDER BY clause, the corresponding WhereLoop is |
| 5712 | ** automatically order-distinct. |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5713 | */ |
| 5714 | |
| 5715 | assert( pOrderBy!=0 ); |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5716 | if( nLoop && OptimizationDisabled(db, SQLITE_OrderByIdxJoin) ) return 0; |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5717 | |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5718 | nOrderBy = pOrderBy->nExpr; |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 5719 | testcase( nOrderBy==BMS-1 ); |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5720 | if( nOrderBy>BMS-1 ) return 0; /* Cannot optimize overly large ORDER BYs */ |
| 5721 | isOrderDistinct = 1; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5722 | obDone = MASKBIT(nOrderBy)-1; |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5723 | orderDistinctMask = 0; |
drh | b8916be | 2013-06-14 02:51:48 +0000 | [diff] [blame] | 5724 | ready = 0; |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5725 | for(iLoop=0; isOrderDistinct && obSat<obDone && iLoop<=nLoop; iLoop++){ |
drh | b8916be | 2013-06-14 02:51:48 +0000 | [diff] [blame] | 5726 | if( iLoop>0 ) ready |= pLoop->maskSelf; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5727 | pLoop = iLoop<nLoop ? pPath->aLoop[iLoop] : pLast; |
drh | 9dfaf62 | 2014-04-25 14:42:17 +0000 | [diff] [blame] | 5728 | if( pLoop->wsFlags & WHERE_VIRTUALTABLE ){ |
| 5729 | if( pLoop->u.vtab.isOrdered ) obSat = obDone; |
| 5730 | break; |
| 5731 | } |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5732 | iCur = pWInfo->pTabList->a[pLoop->iTab].iCursor; |
drh | b8916be | 2013-06-14 02:51:48 +0000 | [diff] [blame] | 5733 | |
| 5734 | /* Mark off any ORDER BY term X that is a column in the table of |
| 5735 | ** the current loop for which there is term in the WHERE |
| 5736 | ** clause of the form X IS NULL or X=? that reference only outer |
| 5737 | ** loops. |
| 5738 | */ |
| 5739 | for(i=0; i<nOrderBy; i++){ |
| 5740 | if( MASKBIT(i) & obSat ) continue; |
| 5741 | pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[i].pExpr); |
| 5742 | if( pOBExpr->op!=TK_COLUMN ) continue; |
| 5743 | if( pOBExpr->iTable!=iCur ) continue; |
| 5744 | pTerm = findTerm(&pWInfo->sWC, iCur, pOBExpr->iColumn, |
drh | e8d0c61 | 2015-05-14 01:05:25 +0000 | [diff] [blame] | 5745 | ~ready, WO_EQ|WO_ISNULL|WO_IS, 0); |
drh | b8916be | 2013-06-14 02:51:48 +0000 | [diff] [blame] | 5746 | if( pTerm==0 ) continue; |
drh | e8d0c61 | 2015-05-14 01:05:25 +0000 | [diff] [blame] | 5747 | if( (pTerm->eOperator&(WO_EQ|WO_IS))!=0 && pOBExpr->iColumn>=0 ){ |
drh | b8916be | 2013-06-14 02:51:48 +0000 | [diff] [blame] | 5748 | const char *z1, *z2; |
| 5749 | pColl = sqlite3ExprCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr); |
| 5750 | if( !pColl ) pColl = db->pDfltColl; |
| 5751 | z1 = pColl->zName; |
| 5752 | pColl = sqlite3ExprCollSeq(pWInfo->pParse, pTerm->pExpr); |
| 5753 | if( !pColl ) pColl = db->pDfltColl; |
| 5754 | z2 = pColl->zName; |
| 5755 | if( sqlite3StrICmp(z1, z2)!=0 ) continue; |
drh | e0cc3c2 | 2015-05-13 17:54:08 +0000 | [diff] [blame] | 5756 | testcase( pTerm->pExpr->op==TK_IS ); |
drh | b8916be | 2013-06-14 02:51:48 +0000 | [diff] [blame] | 5757 | } |
| 5758 | obSat |= MASKBIT(i); |
| 5759 | } |
| 5760 | |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5761 | if( (pLoop->wsFlags & WHERE_ONEROW)==0 ){ |
| 5762 | if( pLoop->wsFlags & WHERE_IPK ){ |
| 5763 | pIndex = 0; |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 5764 | nKeyCol = 0; |
drh | 416846a | 2013-11-06 12:56:04 +0000 | [diff] [blame] | 5765 | nColumn = 1; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5766 | }else if( (pIndex = pLoop->u.btree.pIndex)==0 || pIndex->bUnordered ){ |
drh | 1b0f026 | 2013-05-30 22:27:09 +0000 | [diff] [blame] | 5767 | return 0; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5768 | }else{ |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 5769 | nKeyCol = pIndex->nKeyCol; |
drh | 416846a | 2013-11-06 12:56:04 +0000 | [diff] [blame] | 5770 | nColumn = pIndex->nColumn; |
| 5771 | assert( nColumn==nKeyCol+1 || !HasRowid(pIndex->pTable) ); |
| 5772 | assert( pIndex->aiColumn[nColumn-1]==(-1) || !HasRowid(pIndex->pTable)); |
drh | 5f1d1d9 | 2014-07-31 22:59:04 +0000 | [diff] [blame] | 5773 | isOrderDistinct = IsUniqueIndex(pIndex); |
drh | 1b0f026 | 2013-05-30 22:27:09 +0000 | [diff] [blame] | 5774 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5775 | |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5776 | /* Loop through all columns of the index and deal with the ones |
| 5777 | ** that are not constrained by == or IN. |
| 5778 | */ |
| 5779 | rev = revSet = 0; |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5780 | distinctColumns = 0; |
drh | 416846a | 2013-11-06 12:56:04 +0000 | [diff] [blame] | 5781 | for(j=0; j<nColumn; j++){ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5782 | u8 bOnce; /* True to run the ORDER BY search loop */ |
| 5783 | |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5784 | /* Skip over == and IS NULL terms */ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5785 | if( j<pLoop->u.btree.nEq |
drh | c8bbce1 | 2014-10-21 01:05:09 +0000 | [diff] [blame] | 5786 | && pLoop->nSkip==0 |
drh | e8d0c61 | 2015-05-14 01:05:25 +0000 | [diff] [blame] | 5787 | && ((i = pLoop->aLTerm[j]->eOperator) & (WO_EQ|WO_ISNULL|WO_IS))!=0 |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5788 | ){ |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 5789 | if( i & WO_ISNULL ){ |
| 5790 | testcase( isOrderDistinct ); |
| 5791 | isOrderDistinct = 0; |
| 5792 | } |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5793 | continue; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5794 | } |
| 5795 | |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5796 | /* Get the column number in the table (iColumn) and sort order |
| 5797 | ** (revIdx) for the j-th column of the index. |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5798 | */ |
drh | 416846a | 2013-11-06 12:56:04 +0000 | [diff] [blame] | 5799 | if( pIndex ){ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5800 | iColumn = pIndex->aiColumn[j]; |
| 5801 | revIdx = pIndex->aSortOrder[j]; |
| 5802 | if( iColumn==pIndex->pTable->iPKey ) iColumn = -1; |
drh | dc3cd4b | 2013-05-30 23:21:20 +0000 | [diff] [blame] | 5803 | }else{ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5804 | iColumn = -1; |
| 5805 | revIdx = 0; |
drh | dc3cd4b | 2013-05-30 23:21:20 +0000 | [diff] [blame] | 5806 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5807 | |
| 5808 | /* An unconstrained column that might be NULL means that this |
drh | 416846a | 2013-11-06 12:56:04 +0000 | [diff] [blame] | 5809 | ** WhereLoop is not well-ordered |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5810 | */ |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5811 | if( isOrderDistinct |
| 5812 | && iColumn>=0 |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5813 | && j>=pLoop->u.btree.nEq |
| 5814 | && pIndex->pTable->aCol[iColumn].notNull==0 |
| 5815 | ){ |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5816 | isOrderDistinct = 0; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5817 | } |
| 5818 | |
| 5819 | /* Find the ORDER BY term that corresponds to the j-th column |
dan | 374cd78 | 2014-04-21 13:21:56 +0000 | [diff] [blame] | 5820 | ** of the index and mark that ORDER BY term off |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5821 | */ |
| 5822 | bOnce = 1; |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5823 | isMatch = 0; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5824 | for(i=0; bOnce && i<nOrderBy; i++){ |
| 5825 | if( MASKBIT(i) & obSat ) continue; |
| 5826 | pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[i].pExpr); |
drh | 93ec45d | 2013-06-17 18:20:48 +0000 | [diff] [blame] | 5827 | testcase( wctrlFlags & WHERE_GROUPBY ); |
| 5828 | testcase( wctrlFlags & WHERE_DISTINCTBY ); |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 5829 | if( (wctrlFlags & (WHERE_GROUPBY|WHERE_DISTINCTBY))==0 ) bOnce = 0; |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5830 | if( pOBExpr->op!=TK_COLUMN ) continue; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5831 | if( pOBExpr->iTable!=iCur ) continue; |
| 5832 | if( pOBExpr->iColumn!=iColumn ) continue; |
| 5833 | if( iColumn>=0 ){ |
| 5834 | pColl = sqlite3ExprCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr); |
| 5835 | if( !pColl ) pColl = db->pDfltColl; |
| 5836 | if( sqlite3StrICmp(pColl->zName, pIndex->azColl[j])!=0 ) continue; |
| 5837 | } |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5838 | isMatch = 1; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5839 | break; |
| 5840 | } |
drh | 4929047 | 2014-10-11 02:12:58 +0000 | [diff] [blame] | 5841 | if( isMatch && (wctrlFlags & WHERE_GROUPBY)==0 ){ |
drh | 59b8f2e | 2014-03-22 00:27:14 +0000 | [diff] [blame] | 5842 | /* Make sure the sort order is compatible in an ORDER BY clause. |
| 5843 | ** Sort order is irrelevant for a GROUP BY clause. */ |
| 5844 | if( revSet ){ |
| 5845 | if( (rev ^ revIdx)!=pOrderBy->a[i].sortOrder ) isMatch = 0; |
| 5846 | }else{ |
| 5847 | rev = revIdx ^ pOrderBy->a[i].sortOrder; |
| 5848 | if( rev ) *pRevMask |= MASKBIT(iLoop); |
| 5849 | revSet = 1; |
| 5850 | } |
| 5851 | } |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5852 | if( isMatch ){ |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 5853 | if( iColumn<0 ){ |
| 5854 | testcase( distinctColumns==0 ); |
| 5855 | distinctColumns = 1; |
| 5856 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5857 | obSat |= MASKBIT(i); |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5858 | }else{ |
| 5859 | /* No match found */ |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 5860 | if( j==0 || j<nKeyCol ){ |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 5861 | testcase( isOrderDistinct!=0 ); |
| 5862 | isOrderDistinct = 0; |
| 5863 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5864 | break; |
| 5865 | } |
| 5866 | } /* end Loop over all index columns */ |
drh | 81186b4 | 2013-06-18 01:52:41 +0000 | [diff] [blame] | 5867 | if( distinctColumns ){ |
| 5868 | testcase( isOrderDistinct==0 ); |
| 5869 | isOrderDistinct = 1; |
| 5870 | } |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5871 | } /* end-if not one-row */ |
| 5872 | |
| 5873 | /* Mark off any other ORDER BY terms that reference pLoop */ |
drh | e353ee3 | 2013-06-04 23:40:53 +0000 | [diff] [blame] | 5874 | if( isOrderDistinct ){ |
| 5875 | orderDistinctMask |= pLoop->maskSelf; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5876 | for(i=0; i<nOrderBy; i++){ |
| 5877 | Expr *p; |
drh | 434a931 | 2014-02-26 02:26:09 +0000 | [diff] [blame] | 5878 | Bitmask mTerm; |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5879 | if( MASKBIT(i) & obSat ) continue; |
| 5880 | p = pOrderBy->a[i].pExpr; |
drh | 434a931 | 2014-02-26 02:26:09 +0000 | [diff] [blame] | 5881 | mTerm = exprTableUsage(&pWInfo->sMaskSet,p); |
| 5882 | if( mTerm==0 && !sqlite3ExprIsConstant(p) ) continue; |
| 5883 | if( (mTerm&~orderDistinctMask)==0 ){ |
drh | 7699d1c | 2013-06-04 12:42:29 +0000 | [diff] [blame] | 5884 | obSat |= MASKBIT(i); |
| 5885 | } |
drh | 0afb423 | 2013-05-31 13:36:32 +0000 | [diff] [blame] | 5886 | } |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5887 | } |
drh | b8916be | 2013-06-14 02:51:48 +0000 | [diff] [blame] | 5888 | } /* End the loop over all WhereLoops from outer-most down to inner-most */ |
drh | 36ed034 | 2014-03-28 12:56:57 +0000 | [diff] [blame] | 5889 | if( obSat==obDone ) return (i8)nOrderBy; |
drh | d2de861 | 2014-03-18 18:59:07 +0000 | [diff] [blame] | 5890 | if( !isOrderDistinct ){ |
| 5891 | for(i=nOrderBy-1; i>0; i--){ |
| 5892 | Bitmask m = MASKBIT(i) - 1; |
| 5893 | if( (obSat&m)==m ) return i; |
| 5894 | } |
| 5895 | return 0; |
| 5896 | } |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 5897 | return -1; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5898 | } |
| 5899 | |
dan | 374cd78 | 2014-04-21 13:21:56 +0000 | [diff] [blame] | 5900 | |
| 5901 | /* |
| 5902 | ** If the WHERE_GROUPBY flag is set in the mask passed to sqlite3WhereBegin(), |
| 5903 | ** the planner assumes that the specified pOrderBy list is actually a GROUP |
| 5904 | ** BY clause - and so any order that groups rows as required satisfies the |
| 5905 | ** request. |
| 5906 | ** |
| 5907 | ** Normally, in this case it is not possible for the caller to determine |
| 5908 | ** whether or not the rows are really being delivered in sorted order, or |
| 5909 | ** just in some other order that provides the required grouping. However, |
| 5910 | ** if the WHERE_SORTBYGROUP flag is also passed to sqlite3WhereBegin(), then |
| 5911 | ** this function may be called on the returned WhereInfo object. It returns |
| 5912 | ** true if the rows really will be sorted in the specified order, or false |
| 5913 | ** otherwise. |
| 5914 | ** |
| 5915 | ** For example, assuming: |
| 5916 | ** |
| 5917 | ** CREATE INDEX i1 ON t1(x, Y); |
| 5918 | ** |
| 5919 | ** then |
| 5920 | ** |
| 5921 | ** SELECT * FROM t1 GROUP BY x,y ORDER BY x,y; -- IsSorted()==1 |
| 5922 | ** SELECT * FROM t1 GROUP BY y,x ORDER BY y,x; -- IsSorted()==0 |
| 5923 | */ |
| 5924 | int sqlite3WhereIsSorted(WhereInfo *pWInfo){ |
| 5925 | assert( pWInfo->wctrlFlags & WHERE_GROUPBY ); |
| 5926 | assert( pWInfo->wctrlFlags & WHERE_SORTBYGROUP ); |
| 5927 | return pWInfo->sorted; |
| 5928 | } |
| 5929 | |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 5930 | #ifdef WHERETRACE_ENABLED |
| 5931 | /* For debugging use only: */ |
| 5932 | static const char *wherePathName(WherePath *pPath, int nLoop, WhereLoop *pLast){ |
| 5933 | static char zName[65]; |
| 5934 | int i; |
| 5935 | for(i=0; i<nLoop; i++){ zName[i] = pPath->aLoop[i]->cId; } |
| 5936 | if( pLast ) zName[i++] = pLast->cId; |
| 5937 | zName[i] = 0; |
| 5938 | return zName; |
| 5939 | } |
| 5940 | #endif |
| 5941 | |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 5942 | /* |
dan | 50ae31e | 2014-08-08 16:52:28 +0000 | [diff] [blame] | 5943 | ** Return the cost of sorting nRow rows, assuming that the keys have |
| 5944 | ** nOrderby columns and that the first nSorted columns are already in |
| 5945 | ** order. |
| 5946 | */ |
| 5947 | static LogEst whereSortingCost( |
| 5948 | WhereInfo *pWInfo, |
| 5949 | LogEst nRow, |
| 5950 | int nOrderBy, |
| 5951 | int nSorted |
| 5952 | ){ |
| 5953 | /* TUNING: Estimated cost of a full external sort, where N is |
| 5954 | ** the number of rows to sort is: |
| 5955 | ** |
| 5956 | ** cost = (3.0 * N * log(N)). |
| 5957 | ** |
| 5958 | ** Or, if the order-by clause has X terms but only the last Y |
| 5959 | ** terms are out of order, then block-sorting will reduce the |
| 5960 | ** sorting cost to: |
| 5961 | ** |
| 5962 | ** cost = (3.0 * N * log(N)) * (Y/X) |
| 5963 | ** |
| 5964 | ** The (Y/X) term is implemented using stack variable rScale |
| 5965 | ** below. */ |
| 5966 | LogEst rScale, rSortCost; |
| 5967 | assert( nOrderBy>0 && 66==sqlite3LogEst(100) ); |
| 5968 | rScale = sqlite3LogEst((nOrderBy-nSorted)*100/nOrderBy) - 66; |
| 5969 | rSortCost = nRow + estLog(nRow) + rScale + 16; |
| 5970 | |
| 5971 | /* TUNING: The cost of implementing DISTINCT using a B-TREE is |
| 5972 | ** similar but with a larger constant of proportionality. |
| 5973 | ** Multiply by an additional factor of 3.0. */ |
| 5974 | if( pWInfo->wctrlFlags & WHERE_WANT_DISTINCT ){ |
| 5975 | rSortCost += 16; |
| 5976 | } |
| 5977 | |
| 5978 | return rSortCost; |
| 5979 | } |
| 5980 | |
| 5981 | /* |
dan | 51576f4 | 2013-07-02 10:06:15 +0000 | [diff] [blame] | 5982 | ** Given the list of WhereLoop objects at pWInfo->pLoops, this routine |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5983 | ** attempts to find the lowest cost path that visits each WhereLoop |
| 5984 | ** once. This path is then loaded into the pWInfo->a[].pWLoop fields. |
| 5985 | ** |
drh | c7f0d22 | 2013-06-19 03:27:12 +0000 | [diff] [blame] | 5986 | ** Assume that the total number of output rows that will need to be sorted |
| 5987 | ** will be nRowEst (in the 10*log2 representation). Or, ignore sorting |
| 5988 | ** costs if nRowEst==0. |
| 5989 | ** |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5990 | ** Return SQLITE_OK on success or SQLITE_NOMEM of a memory allocation |
| 5991 | ** error occurs. |
| 5992 | */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 5993 | static int wherePathSolver(WhereInfo *pWInfo, LogEst nRowEst){ |
drh | 783dece | 2013-06-05 17:53:43 +0000 | [diff] [blame] | 5994 | int mxChoice; /* Maximum number of simultaneous paths tracked */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5995 | int nLoop; /* Number of terms in the join */ |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 5996 | Parse *pParse; /* Parsing context */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 5997 | sqlite3 *db; /* The database connection */ |
| 5998 | int iLoop; /* Loop counter over the terms of the join */ |
| 5999 | int ii, jj; /* Loop counters */ |
drh | fde1e6b | 2013-09-06 17:45:42 +0000 | [diff] [blame] | 6000 | int mxI = 0; /* Index of next entry to replace */ |
drh | d2de861 | 2014-03-18 18:59:07 +0000 | [diff] [blame] | 6001 | int nOrderBy; /* Number of ORDER BY clause terms */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 6002 | LogEst mxCost = 0; /* Maximum cost of a set of paths */ |
dan | 50ae31e | 2014-08-08 16:52:28 +0000 | [diff] [blame] | 6003 | LogEst mxUnsorted = 0; /* Maximum unsorted cost of a set of path */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 6004 | int nTo, nFrom; /* Number of valid entries in aTo[] and aFrom[] */ |
| 6005 | WherePath *aFrom; /* All nFrom paths at the previous level */ |
| 6006 | WherePath *aTo; /* The nTo best paths at the current level */ |
| 6007 | WherePath *pFrom; /* An element of aFrom[] that we are working on */ |
| 6008 | WherePath *pTo; /* An element of aTo[] that we are working on */ |
| 6009 | WhereLoop *pWLoop; /* One of the WhereLoop objects */ |
| 6010 | WhereLoop **pX; /* Used to divy up the pSpace memory */ |
dan | 50ae31e | 2014-08-08 16:52:28 +0000 | [diff] [blame] | 6011 | LogEst *aSortCost = 0; /* Sorting and partial sorting costs */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 6012 | char *pSpace; /* Temporary memory used by this routine */ |
dan | e2c2785 | 2014-08-08 17:25:33 +0000 | [diff] [blame] | 6013 | int nSpace; /* Bytes of space allocated at pSpace */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 6014 | |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 6015 | pParse = pWInfo->pParse; |
| 6016 | db = pParse->db; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 6017 | nLoop = pWInfo->nLevel; |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 6018 | /* TUNING: For simple queries, only the best path is tracked. |
| 6019 | ** For 2-way joins, the 5 best paths are followed. |
| 6020 | ** For joins of 3 or more tables, track the 10 best paths */ |
drh | 2504c6c | 2014-06-02 11:26:33 +0000 | [diff] [blame] | 6021 | mxChoice = (nLoop<=1) ? 1 : (nLoop==2 ? 5 : 10); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 6022 | assert( nLoop<=pWInfo->pTabList->nSrc ); |
drh | ddef5dc | 2014-08-07 16:50:00 +0000 | [diff] [blame] | 6023 | WHERETRACE(0x002, ("---- begin solver. (nRowEst=%d)\n", nRowEst)); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 6024 | |
dan | 50ae31e | 2014-08-08 16:52:28 +0000 | [diff] [blame] | 6025 | /* If nRowEst is zero and there is an ORDER BY clause, ignore it. In this |
| 6026 | ** case the purpose of this call is to estimate the number of rows returned |
| 6027 | ** by the overall query. Once this estimate has been obtained, the caller |
| 6028 | ** will invoke this function a second time, passing the estimate as the |
| 6029 | ** nRowEst parameter. */ |
| 6030 | if( pWInfo->pOrderBy==0 || nRowEst==0 ){ |
| 6031 | nOrderBy = 0; |
| 6032 | }else{ |
| 6033 | nOrderBy = pWInfo->pOrderBy->nExpr; |
| 6034 | } |
| 6035 | |
| 6036 | /* Allocate and initialize space for aTo, aFrom and aSortCost[] */ |
dan | e2c2785 | 2014-08-08 17:25:33 +0000 | [diff] [blame] | 6037 | nSpace = (sizeof(WherePath)+sizeof(WhereLoop*)*nLoop)*mxChoice*2; |
| 6038 | nSpace += sizeof(LogEst) * nOrderBy; |
| 6039 | pSpace = sqlite3DbMallocRaw(db, nSpace); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 6040 | if( pSpace==0 ) return SQLITE_NOMEM; |
| 6041 | aTo = (WherePath*)pSpace; |
| 6042 | aFrom = aTo+mxChoice; |
| 6043 | memset(aFrom, 0, sizeof(aFrom[0])); |
| 6044 | pX = (WhereLoop**)(aFrom+mxChoice); |
drh | e9d935a | 2013-06-05 16:19:59 +0000 | [diff] [blame] | 6045 | for(ii=mxChoice*2, pFrom=aTo; ii>0; ii--, pFrom++, pX += nLoop){ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 6046 | pFrom->aLoop = pX; |
| 6047 | } |
dan | 50ae31e | 2014-08-08 16:52:28 +0000 | [diff] [blame] | 6048 | if( nOrderBy ){ |
| 6049 | /* If there is an ORDER BY clause and it is not being ignored, set up |
| 6050 | ** space for the aSortCost[] array. Each element of the aSortCost array |
| 6051 | ** is either zero - meaning it has not yet been initialized - or the |
| 6052 | ** cost of sorting nRowEst rows of data where the first X terms of |
| 6053 | ** the ORDER BY clause are already in order, where X is the array |
| 6054 | ** index. */ |
| 6055 | aSortCost = (LogEst*)pX; |
dan | e2c2785 | 2014-08-08 17:25:33 +0000 | [diff] [blame] | 6056 | memset(aSortCost, 0, sizeof(LogEst) * nOrderBy); |
dan | 50ae31e | 2014-08-08 16:52:28 +0000 | [diff] [blame] | 6057 | } |
dan | e2c2785 | 2014-08-08 17:25:33 +0000 | [diff] [blame] | 6058 | assert( aSortCost==0 || &pSpace[nSpace]==(char*)&aSortCost[nOrderBy] ); |
| 6059 | assert( aSortCost!=0 || &pSpace[nSpace]==(char*)pX ); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 6060 | |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 6061 | /* Seed the search with a single WherePath containing zero WhereLoops. |
| 6062 | ** |
dan | f104abb | 2015-03-16 20:40:00 +0000 | [diff] [blame] | 6063 | ** TUNING: Do not let the number of iterations go above 28. If the cost |
| 6064 | ** of computing an automatic index is not paid back within the first 28 |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 6065 | ** rows, then do not use the automatic index. */ |
dan | f104abb | 2015-03-16 20:40:00 +0000 | [diff] [blame] | 6066 | aFrom[0].nRow = MIN(pParse->nQueryLoop, 48); assert( 48==sqlite3LogEst(28) ); |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 6067 | nFrom = 1; |
dan | 50ae31e | 2014-08-08 16:52:28 +0000 | [diff] [blame] | 6068 | assert( aFrom[0].isOrdered==0 ); |
| 6069 | if( nOrderBy ){ |
| 6070 | /* If nLoop is zero, then there are no FROM terms in the query. Since |
| 6071 | ** in this case the query may return a maximum of one row, the results |
| 6072 | ** are already in the requested order. Set isOrdered to nOrderBy to |
| 6073 | ** indicate this. Or, if nLoop is greater than zero, set isOrdered to |
| 6074 | ** -1, indicating that the result set may or may not be ordered, |
| 6075 | ** depending on the loops added to the current plan. */ |
| 6076 | aFrom[0].isOrdered = nLoop>0 ? -1 : nOrderBy; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 6077 | } |
| 6078 | |
| 6079 | /* Compute successively longer WherePaths using the previous generation |
| 6080 | ** of WherePaths as the basis for the next. Keep track of the mxChoice |
| 6081 | ** best paths at each generation */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 6082 | for(iLoop=0; iLoop<nLoop; iLoop++){ |
| 6083 | nTo = 0; |
| 6084 | for(ii=0, pFrom=aFrom; ii<nFrom; ii++, pFrom++){ |
| 6085 | for(pWLoop=pWInfo->pLoops; pWLoop; pWLoop=pWLoop->pNextLoop){ |
dan | 50ae31e | 2014-08-08 16:52:28 +0000 | [diff] [blame] | 6086 | LogEst nOut; /* Rows visited by (pFrom+pWLoop) */ |
| 6087 | LogEst rCost; /* Cost of path (pFrom+pWLoop) */ |
| 6088 | LogEst rUnsorted; /* Unsorted cost of (pFrom+pWLoop) */ |
| 6089 | i8 isOrdered = pFrom->isOrdered; /* isOrdered for (pFrom+pWLoop) */ |
| 6090 | Bitmask maskNew; /* Mask of src visited by (..) */ |
| 6091 | Bitmask revMask = 0; /* Mask of rev-order loops for (..) */ |
| 6092 | |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 6093 | if( (pWLoop->prereq & ~pFrom->maskLoop)!=0 ) continue; |
| 6094 | if( (pWLoop->maskSelf & pFrom->maskLoop)!=0 ) continue; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 6095 | /* At this point, pWLoop is a candidate to be the next loop. |
| 6096 | ** Compute its cost */ |
dan | 50ae31e | 2014-08-08 16:52:28 +0000 | [diff] [blame] | 6097 | rUnsorted = sqlite3LogEstAdd(pWLoop->rSetup,pWLoop->rRun + pFrom->nRow); |
| 6098 | rUnsorted = sqlite3LogEstAdd(rUnsorted, pFrom->rUnsorted); |
drh | fde1e6b | 2013-09-06 17:45:42 +0000 | [diff] [blame] | 6099 | nOut = pFrom->nRow + pWLoop->nOut; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 6100 | maskNew = pFrom->maskLoop | pWLoop->maskSelf; |
drh | 0401ace | 2014-03-18 15:30:27 +0000 | [diff] [blame] | 6101 | if( isOrdered<0 ){ |
| 6102 | isOrdered = wherePathSatisfiesOrderBy(pWInfo, |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 6103 | pWInfo->pOrderBy, pFrom, pWInfo->wctrlFlags, |
drh | 0401ace | 2014-03-18 15:30:27 +0000 | [diff] [blame] | 6104 | iLoop, pWLoop, &revMask); |
drh | 3a5ba8b | 2013-06-03 15:34:48 +0000 | [diff] [blame] | 6105 | }else{ |
| 6106 | revMask = pFrom->revLoop; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 6107 | } |
dan | 50ae31e | 2014-08-08 16:52:28 +0000 | [diff] [blame] | 6108 | if( isOrdered>=0 && isOrdered<nOrderBy ){ |
| 6109 | if( aSortCost[isOrdered]==0 ){ |
| 6110 | aSortCost[isOrdered] = whereSortingCost( |
| 6111 | pWInfo, nRowEst, nOrderBy, isOrdered |
| 6112 | ); |
| 6113 | } |
| 6114 | rCost = sqlite3LogEstAdd(rUnsorted, aSortCost[isOrdered]); |
| 6115 | |
| 6116 | WHERETRACE(0x002, |
| 6117 | ("---- sort cost=%-3d (%d/%d) increases cost %3d to %-3d\n", |
| 6118 | aSortCost[isOrdered], (nOrderBy-isOrdered), nOrderBy, |
| 6119 | rUnsorted, rCost)); |
| 6120 | }else{ |
| 6121 | rCost = rUnsorted; |
| 6122 | } |
| 6123 | |
drh | ddef5dc | 2014-08-07 16:50:00 +0000 | [diff] [blame] | 6124 | /* Check to see if pWLoop should be added to the set of |
| 6125 | ** mxChoice best-so-far paths. |
| 6126 | ** |
| 6127 | ** First look for an existing path among best-so-far paths |
| 6128 | ** that covers the same set of loops and has the same isOrdered |
| 6129 | ** setting as the current path candidate. |
drh | f2a9030 | 2014-08-07 20:37:01 +0000 | [diff] [blame] | 6130 | ** |
| 6131 | ** The term "((pTo->isOrdered^isOrdered)&0x80)==0" is equivalent |
| 6132 | ** to (pTo->isOrdered==(-1))==(isOrdered==(-1))" for the range |
| 6133 | ** of legal values for isOrdered, -1..64. |
drh | ddef5dc | 2014-08-07 16:50:00 +0000 | [diff] [blame] | 6134 | */ |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 6135 | for(jj=0, pTo=aTo; jj<nTo; jj++, pTo++){ |
drh | fde1e6b | 2013-09-06 17:45:42 +0000 | [diff] [blame] | 6136 | if( pTo->maskLoop==maskNew |
drh | f2a9030 | 2014-08-07 20:37:01 +0000 | [diff] [blame] | 6137 | && ((pTo->isOrdered^isOrdered)&0x80)==0 |
drh | fde1e6b | 2013-09-06 17:45:42 +0000 | [diff] [blame] | 6138 | ){ |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 6139 | testcase( jj==nTo-1 ); |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 6140 | break; |
| 6141 | } |
| 6142 | } |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 6143 | if( jj>=nTo ){ |
drh | ddef5dc | 2014-08-07 16:50:00 +0000 | [diff] [blame] | 6144 | /* None of the existing best-so-far paths match the candidate. */ |
drh | ddef5dc | 2014-08-07 16:50:00 +0000 | [diff] [blame] | 6145 | if( nTo>=mxChoice |
dan | 50ae31e | 2014-08-08 16:52:28 +0000 | [diff] [blame] | 6146 | && (rCost>mxCost || (rCost==mxCost && rUnsorted>=mxUnsorted)) |
drh | ddef5dc | 2014-08-07 16:50:00 +0000 | [diff] [blame] | 6147 | ){ |
| 6148 | /* The current candidate is no better than any of the mxChoice |
| 6149 | ** paths currently in the best-so-far buffer. So discard |
| 6150 | ** this candidate as not viable. */ |
drh | 989578e | 2013-10-28 14:34:35 +0000 | [diff] [blame] | 6151 | #ifdef WHERETRACE_ENABLED /* 0x4 */ |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 6152 | if( sqlite3WhereTrace&0x4 ){ |
drh | fde1e6b | 2013-09-06 17:45:42 +0000 | [diff] [blame] | 6153 | sqlite3DebugPrintf("Skip %s cost=%-3d,%3d order=%c\n", |
| 6154 | wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, |
drh | 0401ace | 2014-03-18 15:30:27 +0000 | [diff] [blame] | 6155 | isOrdered>=0 ? isOrdered+'0' : '?'); |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 6156 | } |
| 6157 | #endif |
| 6158 | continue; |
| 6159 | } |
drh | ddef5dc | 2014-08-07 16:50:00 +0000 | [diff] [blame] | 6160 | /* If we reach this points it means that the new candidate path |
| 6161 | ** needs to be added to the set of best-so-far paths. */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 6162 | if( nTo<mxChoice ){ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 6163 | /* Increase the size of the aTo set by one */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 6164 | jj = nTo++; |
| 6165 | }else{ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 6166 | /* New path replaces the prior worst to keep count below mxChoice */ |
drh | fde1e6b | 2013-09-06 17:45:42 +0000 | [diff] [blame] | 6167 | jj = mxI; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 6168 | } |
| 6169 | pTo = &aTo[jj]; |
drh | 989578e | 2013-10-28 14:34:35 +0000 | [diff] [blame] | 6170 | #ifdef WHERETRACE_ENABLED /* 0x4 */ |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 6171 | if( sqlite3WhereTrace&0x4 ){ |
drh | fde1e6b | 2013-09-06 17:45:42 +0000 | [diff] [blame] | 6172 | sqlite3DebugPrintf("New %s cost=%-3d,%3d order=%c\n", |
| 6173 | wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, |
drh | 0401ace | 2014-03-18 15:30:27 +0000 | [diff] [blame] | 6174 | isOrdered>=0 ? isOrdered+'0' : '?'); |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 6175 | } |
| 6176 | #endif |
drh | f204dac | 2013-05-08 03:22:07 +0000 | [diff] [blame] | 6177 | }else{ |
drh | ddef5dc | 2014-08-07 16:50:00 +0000 | [diff] [blame] | 6178 | /* Control reaches here if best-so-far path pTo=aTo[jj] covers the |
| 6179 | ** same set of loops and has the sam isOrdered setting as the |
| 6180 | ** candidate path. Check to see if the candidate should replace |
| 6181 | ** pTo or if the candidate should be skipped */ |
| 6182 | if( pTo->rCost<rCost || (pTo->rCost==rCost && pTo->nRow<=nOut) ){ |
drh | 989578e | 2013-10-28 14:34:35 +0000 | [diff] [blame] | 6183 | #ifdef WHERETRACE_ENABLED /* 0x4 */ |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 6184 | if( sqlite3WhereTrace&0x4 ){ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 6185 | sqlite3DebugPrintf( |
drh | fde1e6b | 2013-09-06 17:45:42 +0000 | [diff] [blame] | 6186 | "Skip %s cost=%-3d,%3d order=%c", |
| 6187 | wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, |
drh | 0401ace | 2014-03-18 15:30:27 +0000 | [diff] [blame] | 6188 | isOrdered>=0 ? isOrdered+'0' : '?'); |
drh | fde1e6b | 2013-09-06 17:45:42 +0000 | [diff] [blame] | 6189 | sqlite3DebugPrintf(" vs %s cost=%-3d,%d order=%c\n", |
| 6190 | wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow, |
drh | 0401ace | 2014-03-18 15:30:27 +0000 | [diff] [blame] | 6191 | pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?'); |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 6192 | } |
| 6193 | #endif |
drh | ddef5dc | 2014-08-07 16:50:00 +0000 | [diff] [blame] | 6194 | /* Discard the candidate path from further consideration */ |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 6195 | testcase( pTo->rCost==rCost ); |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 6196 | continue; |
| 6197 | } |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 6198 | testcase( pTo->rCost==rCost+1 ); |
drh | ddef5dc | 2014-08-07 16:50:00 +0000 | [diff] [blame] | 6199 | /* Control reaches here if the candidate path is better than the |
| 6200 | ** pTo path. Replace pTo with the candidate. */ |
drh | 989578e | 2013-10-28 14:34:35 +0000 | [diff] [blame] | 6201 | #ifdef WHERETRACE_ENABLED /* 0x4 */ |
drh | ae70cf1 | 2013-05-31 15:18:46 +0000 | [diff] [blame] | 6202 | if( sqlite3WhereTrace&0x4 ){ |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 6203 | sqlite3DebugPrintf( |
drh | fde1e6b | 2013-09-06 17:45:42 +0000 | [diff] [blame] | 6204 | "Update %s cost=%-3d,%3d order=%c", |
| 6205 | wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, |
drh | 0401ace | 2014-03-18 15:30:27 +0000 | [diff] [blame] | 6206 | isOrdered>=0 ? isOrdered+'0' : '?'); |
drh | fde1e6b | 2013-09-06 17:45:42 +0000 | [diff] [blame] | 6207 | sqlite3DebugPrintf(" was %s cost=%-3d,%3d order=%c\n", |
| 6208 | wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow, |
drh | 0401ace | 2014-03-18 15:30:27 +0000 | [diff] [blame] | 6209 | pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?'); |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 6210 | } |
| 6211 | #endif |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 6212 | } |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 6213 | /* pWLoop is a winner. Add it to the set of best so far */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 6214 | pTo->maskLoop = pFrom->maskLoop | pWLoop->maskSelf; |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 6215 | pTo->revLoop = revMask; |
drh | fde1e6b | 2013-09-06 17:45:42 +0000 | [diff] [blame] | 6216 | pTo->nRow = nOut; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 6217 | pTo->rCost = rCost; |
dan | 50ae31e | 2014-08-08 16:52:28 +0000 | [diff] [blame] | 6218 | pTo->rUnsorted = rUnsorted; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 6219 | pTo->isOrdered = isOrdered; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 6220 | memcpy(pTo->aLoop, pFrom->aLoop, sizeof(WhereLoop*)*iLoop); |
| 6221 | pTo->aLoop[iLoop] = pWLoop; |
| 6222 | if( nTo>=mxChoice ){ |
drh | fde1e6b | 2013-09-06 17:45:42 +0000 | [diff] [blame] | 6223 | mxI = 0; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 6224 | mxCost = aTo[0].rCost; |
dan | 50ae31e | 2014-08-08 16:52:28 +0000 | [diff] [blame] | 6225 | mxUnsorted = aTo[0].nRow; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 6226 | for(jj=1, pTo=&aTo[1]; jj<mxChoice; jj++, pTo++){ |
dan | 50ae31e | 2014-08-08 16:52:28 +0000 | [diff] [blame] | 6227 | if( pTo->rCost>mxCost |
| 6228 | || (pTo->rCost==mxCost && pTo->rUnsorted>mxUnsorted) |
| 6229 | ){ |
drh | fde1e6b | 2013-09-06 17:45:42 +0000 | [diff] [blame] | 6230 | mxCost = pTo->rCost; |
dan | 50ae31e | 2014-08-08 16:52:28 +0000 | [diff] [blame] | 6231 | mxUnsorted = pTo->rUnsorted; |
drh | fde1e6b | 2013-09-06 17:45:42 +0000 | [diff] [blame] | 6232 | mxI = jj; |
| 6233 | } |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 6234 | } |
| 6235 | } |
| 6236 | } |
| 6237 | } |
| 6238 | |
drh | 989578e | 2013-10-28 14:34:35 +0000 | [diff] [blame] | 6239 | #ifdef WHERETRACE_ENABLED /* >=2 */ |
drh | 1b131b7 | 2014-10-21 16:01:40 +0000 | [diff] [blame] | 6240 | if( sqlite3WhereTrace & 0x02 ){ |
drh | a50ef11 | 2013-05-22 02:06:59 +0000 | [diff] [blame] | 6241 | sqlite3DebugPrintf("---- after round %d ----\n", iLoop); |
drh | d15cb17 | 2013-05-21 19:23:10 +0000 | [diff] [blame] | 6242 | for(ii=0, pTo=aTo; ii<nTo; ii++, pTo++){ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 6243 | sqlite3DebugPrintf(" %s cost=%-3d nrow=%-3d order=%c", |
drh | a50ef11 | 2013-05-22 02:06:59 +0000 | [diff] [blame] | 6244 | wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow, |
drh | 0401ace | 2014-03-18 15:30:27 +0000 | [diff] [blame] | 6245 | pTo->isOrdered>=0 ? (pTo->isOrdered+'0') : '?'); |
| 6246 | if( pTo->isOrdered>0 ){ |
drh | 88da644 | 2013-05-27 17:59:37 +0000 | [diff] [blame] | 6247 | sqlite3DebugPrintf(" rev=0x%llx\n", pTo->revLoop); |
| 6248 | }else{ |
| 6249 | sqlite3DebugPrintf("\n"); |
| 6250 | } |
drh | f204dac | 2013-05-08 03:22:07 +0000 | [diff] [blame] | 6251 | } |
| 6252 | } |
| 6253 | #endif |
| 6254 | |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 6255 | /* Swap the roles of aFrom and aTo for the next generation */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 6256 | pFrom = aTo; |
| 6257 | aTo = aFrom; |
| 6258 | aFrom = pFrom; |
| 6259 | nFrom = nTo; |
| 6260 | } |
| 6261 | |
drh | 75b9340 | 2013-05-31 20:43:57 +0000 | [diff] [blame] | 6262 | if( nFrom==0 ){ |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 6263 | sqlite3ErrorMsg(pParse, "no query solution"); |
drh | 75b9340 | 2013-05-31 20:43:57 +0000 | [diff] [blame] | 6264 | sqlite3DbFree(db, pSpace); |
| 6265 | return SQLITE_ERROR; |
| 6266 | } |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 6267 | |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 6268 | /* Find the lowest cost path. pFrom will be left pointing to that path */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 6269 | pFrom = aFrom; |
| 6270 | for(ii=1; ii<nFrom; ii++){ |
| 6271 | if( pFrom->rCost>aFrom[ii].rCost ) pFrom = &aFrom[ii]; |
| 6272 | } |
| 6273 | assert( pWInfo->nLevel==nLoop ); |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 6274 | /* Load the lowest cost path into pWInfo */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 6275 | for(iLoop=0; iLoop<nLoop; iLoop++){ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 6276 | WhereLevel *pLevel = pWInfo->a + iLoop; |
| 6277 | pLevel->pWLoop = pWLoop = pFrom->aLoop[iLoop]; |
drh | e217efc | 2013-06-12 03:48:41 +0000 | [diff] [blame] | 6278 | pLevel->iFrom = pWLoop->iTab; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 6279 | pLevel->iTabCur = pWInfo->pTabList->a[pLevel->iFrom].iCursor; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 6280 | } |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 6281 | if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT)!=0 |
| 6282 | && (pWInfo->wctrlFlags & WHERE_DISTINCTBY)==0 |
| 6283 | && pWInfo->eDistinct==WHERE_DISTINCT_NOOP |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 6284 | && nRowEst |
| 6285 | ){ |
| 6286 | Bitmask notUsed; |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 6287 | int rc = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pResultSet, pFrom, |
drh | 93ec45d | 2013-06-17 18:20:48 +0000 | [diff] [blame] | 6288 | WHERE_DISTINCTBY, nLoop-1, pFrom->aLoop[nLoop-1], ¬Used); |
drh | 0401ace | 2014-03-18 15:30:27 +0000 | [diff] [blame] | 6289 | if( rc==pWInfo->pResultSet->nExpr ){ |
| 6290 | pWInfo->eDistinct = WHERE_DISTINCT_ORDERED; |
| 6291 | } |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 6292 | } |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 6293 | if( pWInfo->pOrderBy ){ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 6294 | if( pWInfo->wctrlFlags & WHERE_DISTINCTBY ){ |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 6295 | if( pFrom->isOrdered==pWInfo->pOrderBy->nExpr ){ |
| 6296 | pWInfo->eDistinct = WHERE_DISTINCT_ORDERED; |
| 6297 | } |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 6298 | }else{ |
drh | ddba0c2 | 2014-03-18 20:33:42 +0000 | [diff] [blame] | 6299 | pWInfo->nOBSat = pFrom->isOrdered; |
drh | ea6c36e | 2014-03-19 14:30:55 +0000 | [diff] [blame] | 6300 | if( pWInfo->nOBSat<0 ) pWInfo->nOBSat = 0; |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 6301 | pWInfo->revMask = pFrom->revLoop; |
| 6302 | } |
dan | 374cd78 | 2014-04-21 13:21:56 +0000 | [diff] [blame] | 6303 | if( (pWInfo->wctrlFlags & WHERE_SORTBYGROUP) |
drh | 11b0481 | 2015-04-12 01:22:04 +0000 | [diff] [blame] | 6304 | && pWInfo->nOBSat==pWInfo->pOrderBy->nExpr && nLoop>0 |
dan | 374cd78 | 2014-04-21 13:21:56 +0000 | [diff] [blame] | 6305 | ){ |
dan | b645320 | 2014-10-10 20:52:53 +0000 | [diff] [blame] | 6306 | Bitmask revMask = 0; |
dan | 374cd78 | 2014-04-21 13:21:56 +0000 | [diff] [blame] | 6307 | int nOrder = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pOrderBy, |
dan | b645320 | 2014-10-10 20:52:53 +0000 | [diff] [blame] | 6308 | pFrom, 0, nLoop-1, pFrom->aLoop[nLoop-1], &revMask |
dan | 374cd78 | 2014-04-21 13:21:56 +0000 | [diff] [blame] | 6309 | ); |
| 6310 | assert( pWInfo->sorted==0 ); |
dan | b645320 | 2014-10-10 20:52:53 +0000 | [diff] [blame] | 6311 | if( nOrder==pWInfo->pOrderBy->nExpr ){ |
| 6312 | pWInfo->sorted = 1; |
| 6313 | pWInfo->revMask = revMask; |
| 6314 | } |
dan | 374cd78 | 2014-04-21 13:21:56 +0000 | [diff] [blame] | 6315 | } |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 6316 | } |
dan | 374cd78 | 2014-04-21 13:21:56 +0000 | [diff] [blame] | 6317 | |
| 6318 | |
drh | a50ef11 | 2013-05-22 02:06:59 +0000 | [diff] [blame] | 6319 | pWInfo->nRowOut = pFrom->nRow; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 6320 | |
| 6321 | /* Free temporary memory and return success */ |
| 6322 | sqlite3DbFree(db, pSpace); |
| 6323 | return SQLITE_OK; |
| 6324 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6325 | |
| 6326 | /* |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 6327 | ** Most queries use only a single table (they are not joins) and have |
| 6328 | ** simple == constraints against indexed fields. This routine attempts |
| 6329 | ** to plan those simple cases using much less ceremony than the |
| 6330 | ** general-purpose query planner, and thereby yield faster sqlite3_prepare() |
| 6331 | ** times for the common case. |
| 6332 | ** |
| 6333 | ** Return non-zero on success, if this query can be handled by this |
| 6334 | ** no-frills query planner. Return zero if this query needs the |
| 6335 | ** general-purpose query planner. |
| 6336 | */ |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 6337 | static int whereShortCut(WhereLoopBuilder *pBuilder){ |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 6338 | WhereInfo *pWInfo; |
| 6339 | struct SrcList_item *pItem; |
| 6340 | WhereClause *pWC; |
| 6341 | WhereTerm *pTerm; |
| 6342 | WhereLoop *pLoop; |
| 6343 | int iCur; |
drh | 92a121f | 2013-06-10 12:15:47 +0000 | [diff] [blame] | 6344 | int j; |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 6345 | Table *pTab; |
| 6346 | Index *pIdx; |
| 6347 | |
| 6348 | pWInfo = pBuilder->pWInfo; |
drh | 5822d6f | 2013-06-10 23:30:09 +0000 | [diff] [blame] | 6349 | if( pWInfo->wctrlFlags & WHERE_FORCE_TABLE ) return 0; |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 6350 | assert( pWInfo->pTabList->nSrc>=1 ); |
| 6351 | pItem = pWInfo->pTabList->a; |
| 6352 | pTab = pItem->pTab; |
| 6353 | if( IsVirtual(pTab) ) return 0; |
| 6354 | if( pItem->zIndex ) return 0; |
| 6355 | iCur = pItem->iCursor; |
| 6356 | pWC = &pWInfo->sWC; |
| 6357 | pLoop = pBuilder->pNew; |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 6358 | pLoop->wsFlags = 0; |
drh | c8bbce1 | 2014-10-21 01:05:09 +0000 | [diff] [blame] | 6359 | pLoop->nSkip = 0; |
drh | e8d0c61 | 2015-05-14 01:05:25 +0000 | [diff] [blame] | 6360 | pTerm = findTerm(pWC, iCur, -1, 0, WO_EQ|WO_IS, 0); |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 6361 | if( pTerm ){ |
drh | e8d0c61 | 2015-05-14 01:05:25 +0000 | [diff] [blame] | 6362 | testcase( pTerm->eOperator & WO_IS ); |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 6363 | pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_IPK|WHERE_ONEROW; |
| 6364 | pLoop->aLTerm[0] = pTerm; |
| 6365 | pLoop->nLTerm = 1; |
| 6366 | pLoop->u.btree.nEq = 1; |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 6367 | /* TUNING: Cost of a rowid lookup is 10 */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 6368 | pLoop->rRun = 33; /* 33==sqlite3LogEst(10) */ |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 6369 | }else{ |
| 6370 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
mistachkin | 4e5bef8 | 2015-05-15 20:14:00 +0000 | [diff] [blame] | 6371 | int opMask; |
dan | cd40abb | 2013-08-29 10:46:05 +0000 | [diff] [blame] | 6372 | assert( pLoop->aLTermSpace==pLoop->aLTerm ); |
drh | 5f1d1d9 | 2014-07-31 22:59:04 +0000 | [diff] [blame] | 6373 | if( !IsUniqueIndex(pIdx) |
dan | cd40abb | 2013-08-29 10:46:05 +0000 | [diff] [blame] | 6374 | || pIdx->pPartIdxWhere!=0 |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 6375 | || pIdx->nKeyCol>ArraySize(pLoop->aLTermSpace) |
dan | cd40abb | 2013-08-29 10:46:05 +0000 | [diff] [blame] | 6376 | ) continue; |
mistachkin | 4e5bef8 | 2015-05-15 20:14:00 +0000 | [diff] [blame] | 6377 | opMask = pIdx->uniqNotNull ? (WO_EQ|WO_IS) : WO_EQ; |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 6378 | for(j=0; j<pIdx->nKeyCol; j++){ |
mistachkin | 4e5bef8 | 2015-05-15 20:14:00 +0000 | [diff] [blame] | 6379 | pTerm = findTerm(pWC, iCur, pIdx->aiColumn[j], 0, opMask, pIdx); |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 6380 | if( pTerm==0 ) break; |
dan | 3072b53 | 2015-05-15 19:59:23 +0000 | [diff] [blame] | 6381 | testcase( pTerm->eOperator & WO_IS ); |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 6382 | pLoop->aLTerm[j] = pTerm; |
| 6383 | } |
drh | bbbdc83 | 2013-10-22 18:01:40 +0000 | [diff] [blame] | 6384 | if( j!=pIdx->nKeyCol ) continue; |
drh | 92a121f | 2013-06-10 12:15:47 +0000 | [diff] [blame] | 6385 | pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_ONEROW|WHERE_INDEXED; |
drh | ec95c44 | 2013-10-23 01:57:32 +0000 | [diff] [blame] | 6386 | if( pIdx->isCovering || (pItem->colUsed & ~columnsInIndex(pIdx))==0 ){ |
drh | 92a121f | 2013-06-10 12:15:47 +0000 | [diff] [blame] | 6387 | pLoop->wsFlags |= WHERE_IDX_ONLY; |
| 6388 | } |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 6389 | pLoop->nLTerm = j; |
| 6390 | pLoop->u.btree.nEq = j; |
| 6391 | pLoop->u.btree.pIndex = pIdx; |
drh | e1e2e9a | 2013-06-13 15:16:53 +0000 | [diff] [blame] | 6392 | /* TUNING: Cost of a unique index lookup is 15 */ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 6393 | pLoop->rRun = 39; /* 39==sqlite3LogEst(15) */ |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 6394 | break; |
| 6395 | } |
| 6396 | } |
drh | 3b75ffa | 2013-06-10 14:56:25 +0000 | [diff] [blame] | 6397 | if( pLoop->wsFlags ){ |
drh | bf539c4 | 2013-10-05 18:16:02 +0000 | [diff] [blame] | 6398 | pLoop->nOut = (LogEst)1; |
drh | 3b75ffa | 2013-06-10 14:56:25 +0000 | [diff] [blame] | 6399 | pWInfo->a[0].pWLoop = pLoop; |
| 6400 | pLoop->maskSelf = getMask(&pWInfo->sMaskSet, iCur); |
| 6401 | pWInfo->a[0].iTabCur = iCur; |
| 6402 | pWInfo->nRowOut = 1; |
drh | ddba0c2 | 2014-03-18 20:33:42 +0000 | [diff] [blame] | 6403 | if( pWInfo->pOrderBy ) pWInfo->nOBSat = pWInfo->pOrderBy->nExpr; |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 6404 | if( pWInfo->wctrlFlags & WHERE_WANT_DISTINCT ){ |
| 6405 | pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE; |
| 6406 | } |
drh | 3b75ffa | 2013-06-10 14:56:25 +0000 | [diff] [blame] | 6407 | #ifdef SQLITE_DEBUG |
| 6408 | pLoop->cId = '0'; |
| 6409 | #endif |
| 6410 | return 1; |
| 6411 | } |
| 6412 | return 0; |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 6413 | } |
| 6414 | |
| 6415 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6416 | ** Generate the beginning of the loop used for WHERE clause processing. |
| 6417 | ** The return value is a pointer to an opaque structure that contains |
| 6418 | ** information needed to terminate the loop. Later, the calling routine |
| 6419 | ** should invoke sqlite3WhereEnd() with the return value of this function |
| 6420 | ** in order to complete the WHERE clause processing. |
| 6421 | ** |
| 6422 | ** If an error occurs, this routine returns NULL. |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 6423 | ** |
| 6424 | ** The basic idea is to do a nested loop, one loop for each table in |
| 6425 | ** the FROM clause of a select. (INSERT and UPDATE statements are the |
| 6426 | ** same as a SELECT with only a single table in the FROM clause.) For |
| 6427 | ** example, if the SQL is this: |
| 6428 | ** |
| 6429 | ** SELECT * FROM t1, t2, t3 WHERE ...; |
| 6430 | ** |
| 6431 | ** Then the code generated is conceptually like the following: |
| 6432 | ** |
| 6433 | ** foreach row1 in t1 do \ Code generated |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 6434 | ** foreach row2 in t2 do |-- by sqlite3WhereBegin() |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 6435 | ** foreach row3 in t3 do / |
| 6436 | ** ... |
| 6437 | ** end \ Code generated |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 6438 | ** end |-- by sqlite3WhereEnd() |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 6439 | ** end / |
| 6440 | ** |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 6441 | ** Note that the loops might not be nested in the order in which they |
| 6442 | ** 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] | 6443 | ** use of indices. Note also that when the IN operator appears in |
| 6444 | ** the WHERE clause, it might result in additional nested loops for |
| 6445 | ** scanning through all values on the right-hand side of the IN. |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 6446 | ** |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 6447 | ** There are Btree cursors associated with each table. t1 uses cursor |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 6448 | ** number pTabList->a[0].iCursor. t2 uses the cursor pTabList->a[1].iCursor. |
| 6449 | ** And so forth. This routine generates code to open those VDBE cursors |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 6450 | ** and sqlite3WhereEnd() generates the code to close them. |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 6451 | ** |
drh | e6f85e7 | 2004-12-25 01:03:13 +0000 | [diff] [blame] | 6452 | ** The code that sqlite3WhereBegin() generates leaves the cursors named |
| 6453 | ** in pTabList pointing at their appropriate entries. The [...] code |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 6454 | ** can use OP_Column and OP_Rowid opcodes on these cursors to extract |
drh | e6f85e7 | 2004-12-25 01:03:13 +0000 | [diff] [blame] | 6455 | ** data from the various tables of the loop. |
| 6456 | ** |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 6457 | ** If the WHERE clause is empty, the foreach loops must each scan their |
| 6458 | ** entire tables. Thus a three-way join is an O(N^3) operation. But if |
| 6459 | ** the tables have indices and there are terms in the WHERE clause that |
| 6460 | ** refer to those indices, a complete table scan can be avoided and the |
| 6461 | ** code will run much faster. Most of the work of this routine is checking |
| 6462 | ** to see if there are indices that can be used to speed up the loop. |
| 6463 | ** |
| 6464 | ** Terms of the WHERE clause are also used to limit which rows actually |
| 6465 | ** make it to the "..." in the middle of the loop. After each "foreach", |
| 6466 | ** terms of the WHERE clause that use only terms in that loop and outer |
| 6467 | ** loops are evaluated and if false a jump is made around all subsequent |
| 6468 | ** inner loops (or around the "..." if the test occurs within the inner- |
| 6469 | ** most loop) |
| 6470 | ** |
| 6471 | ** OUTER JOINS |
| 6472 | ** |
| 6473 | ** An outer join of tables t1 and t2 is conceptally coded as follows: |
| 6474 | ** |
| 6475 | ** foreach row1 in t1 do |
| 6476 | ** flag = 0 |
| 6477 | ** foreach row2 in t2 do |
| 6478 | ** start: |
| 6479 | ** ... |
| 6480 | ** flag = 1 |
| 6481 | ** end |
drh | e318474 | 2002-06-19 14:27:05 +0000 | [diff] [blame] | 6482 | ** if flag==0 then |
| 6483 | ** move the row2 cursor to a null row |
| 6484 | ** goto start |
| 6485 | ** fi |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 6486 | ** end |
| 6487 | ** |
drh | e318474 | 2002-06-19 14:27:05 +0000 | [diff] [blame] | 6488 | ** ORDER BY CLAUSE PROCESSING |
| 6489 | ** |
drh | 9443342 | 2013-07-01 11:05:50 +0000 | [diff] [blame] | 6490 | ** pOrderBy is a pointer to the ORDER BY clause (or the GROUP BY clause |
| 6491 | ** if the WHERE_GROUPBY flag is set in wctrlFlags) of a SELECT statement |
drh | e318474 | 2002-06-19 14:27:05 +0000 | [diff] [blame] | 6492 | ** 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] | 6493 | ** is called from an UPDATE or DELETE statement, then pOrderBy is NULL. |
drh | fc8d4f9 | 2013-11-08 15:19:46 +0000 | [diff] [blame] | 6494 | ** |
| 6495 | ** The iIdxCur parameter is the cursor number of an index. If |
| 6496 | ** WHERE_ONETABLE_ONLY is set, iIdxCur is the cursor number of an index |
| 6497 | ** to use for OR clause processing. The WHERE clause should use this |
| 6498 | ** specific cursor. If WHERE_ONEPASS_DESIRED is set, then iIdxCur is |
| 6499 | ** the first cursor in an array of cursors for all indices. iIdxCur should |
| 6500 | ** be used to compute the appropriate cursor depending on which index is |
| 6501 | ** used. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6502 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 6503 | WhereInfo *sqlite3WhereBegin( |
danielk1977 | ed326d7 | 2004-11-16 15:50:19 +0000 | [diff] [blame] | 6504 | Parse *pParse, /* The parser context */ |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 6505 | SrcList *pTabList, /* FROM clause: A list of all tables to be scanned */ |
danielk1977 | ed326d7 | 2004-11-16 15:50:19 +0000 | [diff] [blame] | 6506 | Expr *pWhere, /* The WHERE clause */ |
drh | 0401ace | 2014-03-18 15:30:27 +0000 | [diff] [blame] | 6507 | ExprList *pOrderBy, /* An ORDER BY (or GROUP BY) clause, or NULL */ |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 6508 | ExprList *pResultSet, /* Result set of the query */ |
dan | 0efb72c | 2012-08-24 18:44:56 +0000 | [diff] [blame] | 6509 | u16 wctrlFlags, /* One of the WHERE_* flags defined in sqliteInt.h */ |
| 6510 | int iIdxCur /* If WHERE_ONETABLE_ONLY is set, index cursor number */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6511 | ){ |
danielk1977 | be22965 | 2009-03-20 14:18:51 +0000 | [diff] [blame] | 6512 | int nByteWInfo; /* Num. bytes allocated for WhereInfo struct */ |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 6513 | int nTabList; /* Number of elements in pTabList */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6514 | WhereInfo *pWInfo; /* Will become the return value of this function */ |
| 6515 | Vdbe *v = pParse->pVdbe; /* The virtual database engine */ |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 6516 | Bitmask notReady; /* Cursors that are not yet positioned */ |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 6517 | WhereLoopBuilder sWLB; /* The WhereLoop builder */ |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 6518 | WhereMaskSet *pMaskSet; /* The expression mask set */ |
drh | 56f1b99 | 2012-09-25 14:29:39 +0000 | [diff] [blame] | 6519 | WhereLevel *pLevel; /* A single level in pWInfo->a[] */ |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 6520 | WhereLoop *pLoop; /* Pointer to a single WhereLoop object */ |
drh | 9cd1c99 | 2012-09-25 20:43:35 +0000 | [diff] [blame] | 6521 | int ii; /* Loop counter */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 6522 | sqlite3 *db; /* Database connection */ |
drh | 5346e95 | 2013-05-08 14:14:26 +0000 | [diff] [blame] | 6523 | int rc; /* Return code */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6524 | |
drh | 56f1b99 | 2012-09-25 14:29:39 +0000 | [diff] [blame] | 6525 | |
| 6526 | /* Variable initialization */ |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 6527 | db = pParse->db; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 6528 | memset(&sWLB, 0, sizeof(sWLB)); |
drh | 0401ace | 2014-03-18 15:30:27 +0000 | [diff] [blame] | 6529 | |
| 6530 | /* An ORDER/GROUP BY clause of more than 63 terms cannot be optimized */ |
| 6531 | testcase( pOrderBy && pOrderBy->nExpr==BMS-1 ); |
| 6532 | if( pOrderBy && pOrderBy->nExpr>=BMS ) pOrderBy = 0; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 6533 | sWLB.pOrderBy = pOrderBy; |
drh | 56f1b99 | 2012-09-25 14:29:39 +0000 | [diff] [blame] | 6534 | |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 6535 | /* Disable the DISTINCT optimization if SQLITE_DistinctOpt is set via |
| 6536 | ** sqlite3_test_ctrl(SQLITE_TESTCTRL_OPTIMIZATIONS,...) */ |
| 6537 | if( OptimizationDisabled(db, SQLITE_DistinctOpt) ){ |
| 6538 | wctrlFlags &= ~WHERE_WANT_DISTINCT; |
| 6539 | } |
| 6540 | |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 6541 | /* 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] | 6542 | ** bits in a Bitmask |
| 6543 | */ |
drh | 67ae0cb | 2010-04-08 14:38:51 +0000 | [diff] [blame] | 6544 | testcase( pTabList->nSrc==BMS ); |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 6545 | if( pTabList->nSrc>BMS ){ |
| 6546 | sqlite3ErrorMsg(pParse, "at most %d tables in a join", BMS); |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 6547 | return 0; |
| 6548 | } |
| 6549 | |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 6550 | /* This function normally generates a nested loop for all tables in |
| 6551 | ** pTabList. But if the WHERE_ONETABLE_ONLY flag is set, then we should |
| 6552 | ** only generate code for the first table in pTabList and assume that |
| 6553 | ** any cursors associated with subsequent tables are uninitialized. |
| 6554 | */ |
| 6555 | nTabList = (wctrlFlags & WHERE_ONETABLE_ONLY) ? 1 : pTabList->nSrc; |
| 6556 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6557 | /* Allocate and initialize the WhereInfo structure that will become the |
danielk1977 | be22965 | 2009-03-20 14:18:51 +0000 | [diff] [blame] | 6558 | ** return value. A single allocation is used to store the WhereInfo |
| 6559 | ** struct, the contents of WhereInfo.a[], the WhereClause structure |
| 6560 | ** and the WhereMaskSet structure. Since WhereClause contains an 8-byte |
| 6561 | ** field (type Bitmask) it must be aligned on an 8-byte boundary on |
| 6562 | ** some architectures. Hence the ROUND8() below. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6563 | */ |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 6564 | nByteWInfo = ROUND8(sizeof(WhereInfo)+(nTabList-1)*sizeof(WhereLevel)); |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 6565 | pWInfo = sqlite3DbMallocZero(db, nByteWInfo + sizeof(WhereLoop)); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 6566 | if( db->mallocFailed ){ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 6567 | sqlite3DbFree(db, pWInfo); |
| 6568 | pWInfo = 0; |
danielk1977 | 85574e3 | 2008-10-06 05:32:18 +0000 | [diff] [blame] | 6569 | goto whereBeginError; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6570 | } |
drh | fc8d4f9 | 2013-11-08 15:19:46 +0000 | [diff] [blame] | 6571 | pWInfo->aiCurOnePass[0] = pWInfo->aiCurOnePass[1] = -1; |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 6572 | pWInfo->nLevel = nTabList; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6573 | pWInfo->pParse = pParse; |
| 6574 | pWInfo->pTabList = pTabList; |
drh | 6b7157b | 2013-05-10 02:00:35 +0000 | [diff] [blame] | 6575 | pWInfo->pOrderBy = pOrderBy; |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 6576 | pWInfo->pResultSet = pResultSet; |
drh | a22a75e | 2014-03-21 18:16:23 +0000 | [diff] [blame] | 6577 | pWInfo->iBreak = pWInfo->iContinue = sqlite3VdbeMakeLabel(v); |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 6578 | pWInfo->wctrlFlags = wctrlFlags; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 6579 | pWInfo->savedNQueryLoop = pParse->nQueryLoop; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 6580 | pMaskSet = &pWInfo->sMaskSet; |
drh | 1c8148f | 2013-05-04 20:25:23 +0000 | [diff] [blame] | 6581 | sWLB.pWInfo = pWInfo; |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 6582 | sWLB.pWC = &pWInfo->sWC; |
drh | 1ac87e1 | 2013-07-18 14:50:56 +0000 | [diff] [blame] | 6583 | sWLB.pNew = (WhereLoop*)(((char*)pWInfo)+nByteWInfo); |
| 6584 | assert( EIGHT_BYTE_ALIGNMENT(sWLB.pNew) ); |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 6585 | whereLoopInit(sWLB.pNew); |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 6586 | #ifdef SQLITE_DEBUG |
| 6587 | sWLB.pNew->cId = '*'; |
| 6588 | #endif |
drh | 08192d5 | 2002-04-30 19:20:28 +0000 | [diff] [blame] | 6589 | |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 6590 | /* Split the WHERE clause into separate subexpressions where each |
| 6591 | ** subexpression is separated by an AND operator. |
| 6592 | */ |
| 6593 | initMaskSet(pMaskSet); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 6594 | whereClauseInit(&pWInfo->sWC, pWInfo); |
drh | 3975974 | 2013-08-02 23:40:45 +0000 | [diff] [blame] | 6595 | whereSplit(&pWInfo->sWC, pWhere, TK_AND); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 6596 | |
drh | 08192d5 | 2002-04-30 19:20:28 +0000 | [diff] [blame] | 6597 | /* Special case: a WHERE clause that is constant. Evaluate the |
| 6598 | ** expression and either jump over all of the code or fall thru. |
| 6599 | */ |
drh | 759e858 | 2014-01-02 21:05:10 +0000 | [diff] [blame] | 6600 | for(ii=0; ii<sWLB.pWC->nTerm; ii++){ |
| 6601 | if( nTabList==0 || sqlite3ExprIsConstantNotJoin(sWLB.pWC->a[ii].pExpr) ){ |
| 6602 | sqlite3ExprIfFalse(pParse, sWLB.pWC->a[ii].pExpr, pWInfo->iBreak, |
| 6603 | SQLITE_JUMPIFNULL); |
| 6604 | sWLB.pWC->a[ii].wtFlags |= TERM_CODED; |
| 6605 | } |
drh | 08192d5 | 2002-04-30 19:20:28 +0000 | [diff] [blame] | 6606 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6607 | |
drh | 4fe425a | 2013-06-12 17:08:06 +0000 | [diff] [blame] | 6608 | /* Special case: No FROM clause |
| 6609 | */ |
| 6610 | if( nTabList==0 ){ |
drh | ddba0c2 | 2014-03-18 20:33:42 +0000 | [diff] [blame] | 6611 | if( pOrderBy ) pWInfo->nOBSat = pOrderBy->nExpr; |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 6612 | if( wctrlFlags & WHERE_WANT_DISTINCT ){ |
| 6613 | pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE; |
| 6614 | } |
drh | 4fe425a | 2013-06-12 17:08:06 +0000 | [diff] [blame] | 6615 | } |
| 6616 | |
drh | 42165be | 2008-03-26 14:56:34 +0000 | [diff] [blame] | 6617 | /* Assign a bit from the bitmask to every term in the FROM clause. |
| 6618 | ** |
| 6619 | ** When assigning bitmask values to FROM clause cursors, it must be |
| 6620 | ** the case that if X is the bitmask for the N-th FROM clause term then |
| 6621 | ** the bitmask for all FROM clause terms to the left of the N-th term |
| 6622 | ** is (X-1). An expression from the ON clause of a LEFT JOIN can use |
| 6623 | ** its Expr.iRightJoinTable value to find the bitmask of the right table |
| 6624 | ** of the join. Subtracting one from the right table bitmask gives a |
| 6625 | ** bitmask for all tables to the left of the join. Knowing the bitmask |
| 6626 | ** 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] | 6627 | ** |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 6628 | ** Note that bitmasks are created for all pTabList->nSrc tables in |
| 6629 | ** pTabList, not just the first nTabList tables. nTabList is normally |
| 6630 | ** equal to pTabList->nSrc but might be shortened to 1 if the |
| 6631 | ** WHERE_ONETABLE_ONLY flag is set. |
drh | 42165be | 2008-03-26 14:56:34 +0000 | [diff] [blame] | 6632 | */ |
drh | 9cd1c99 | 2012-09-25 20:43:35 +0000 | [diff] [blame] | 6633 | for(ii=0; ii<pTabList->nSrc; ii++){ |
| 6634 | createMask(pMaskSet, pTabList->a[ii].iCursor); |
drh | 42165be | 2008-03-26 14:56:34 +0000 | [diff] [blame] | 6635 | } |
| 6636 | #ifndef NDEBUG |
| 6637 | { |
| 6638 | Bitmask toTheLeft = 0; |
drh | 9cd1c99 | 2012-09-25 20:43:35 +0000 | [diff] [blame] | 6639 | for(ii=0; ii<pTabList->nSrc; ii++){ |
| 6640 | Bitmask m = getMask(pMaskSet, pTabList->a[ii].iCursor); |
drh | 42165be | 2008-03-26 14:56:34 +0000 | [diff] [blame] | 6641 | assert( (m-1)==toTheLeft ); |
| 6642 | toTheLeft |= m; |
| 6643 | } |
| 6644 | } |
| 6645 | #endif |
| 6646 | |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 6647 | /* Analyze all of the subexpressions. Note that exprAnalyze() might |
| 6648 | ** add new virtual terms onto the end of the WHERE clause. We do not |
| 6649 | ** want to analyze these virtual terms, so start analyzing at the end |
drh | b6fb62d | 2005-09-20 08:47:20 +0000 | [diff] [blame] | 6650 | ** and work forward so that the added virtual terms are never processed. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6651 | */ |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 6652 | exprAnalyzeAll(pTabList, &pWInfo->sWC); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 6653 | if( db->mallocFailed ){ |
danielk1977 | 85574e3 | 2008-10-06 05:32:18 +0000 | [diff] [blame] | 6654 | goto whereBeginError; |
drh | 0bbaa1b | 2005-08-19 19:14:12 +0000 | [diff] [blame] | 6655 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6656 | |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 6657 | if( wctrlFlags & WHERE_WANT_DISTINCT ){ |
| 6658 | if( isDistinctRedundant(pParse, pTabList, &pWInfo->sWC, pResultSet) ){ |
| 6659 | /* The DISTINCT marking is pointless. Ignore it. */ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 6660 | pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE; |
| 6661 | }else if( pOrderBy==0 ){ |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 6662 | /* Try to ORDER BY the result set to make distinct processing easier */ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 6663 | pWInfo->wctrlFlags |= WHERE_DISTINCTBY; |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 6664 | pWInfo->pOrderBy = pResultSet; |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 6665 | } |
dan | 38cc40c | 2011-06-30 20:17:15 +0000 | [diff] [blame] | 6666 | } |
| 6667 | |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 6668 | /* Construct the WhereLoop objects */ |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 6669 | WHERETRACE(0xffff,("*** Optimizer Start ***\n")); |
drh | c90713d | 2014-09-30 13:46:49 +0000 | [diff] [blame] | 6670 | #if defined(WHERETRACE_ENABLED) |
| 6671 | /* Display all terms of the WHERE clause */ |
| 6672 | if( sqlite3WhereTrace & 0x100 ){ |
| 6673 | int i; |
| 6674 | for(i=0; i<sWLB.pWC->nTerm; i++){ |
| 6675 | whereTermPrint(&sWLB.pWC->a[i], i); |
| 6676 | } |
| 6677 | } |
| 6678 | #endif |
| 6679 | |
drh | b8a8e8a | 2013-06-10 19:12:39 +0000 | [diff] [blame] | 6680 | if( nTabList!=1 || whereShortCut(&sWLB)==0 ){ |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 6681 | rc = whereLoopAddAll(&sWLB); |
| 6682 | if( rc ) goto whereBeginError; |
| 6683 | |
| 6684 | /* Display all of the WhereLoop objects if wheretrace is enabled */ |
drh | 989578e | 2013-10-28 14:34:35 +0000 | [diff] [blame] | 6685 | #ifdef WHERETRACE_ENABLED /* !=0 */ |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 6686 | if( sqlite3WhereTrace ){ |
| 6687 | WhereLoop *p; |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 6688 | int i; |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 6689 | static char zLabel[] = "0123456789abcdefghijklmnopqrstuvwyxz" |
| 6690 | "ABCDEFGHIJKLMNOPQRSTUVWYXZ"; |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 6691 | for(p=pWInfo->pLoops, i=0; p; p=p->pNextLoop, i++){ |
| 6692 | p->cId = zLabel[i%sizeof(zLabel)]; |
drh | c1ba2e7 | 2013-10-28 19:03:21 +0000 | [diff] [blame] | 6693 | whereLoopPrint(p, sWLB.pWC); |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 6694 | } |
| 6695 | } |
| 6696 | #endif |
| 6697 | |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 6698 | wherePathSolver(pWInfo, 0); |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 6699 | if( db->mallocFailed ) goto whereBeginError; |
| 6700 | if( pWInfo->pOrderBy ){ |
drh | c7f0d22 | 2013-06-19 03:27:12 +0000 | [diff] [blame] | 6701 | wherePathSolver(pWInfo, pWInfo->nRowOut+1); |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 6702 | if( db->mallocFailed ) goto whereBeginError; |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 6703 | } |
| 6704 | } |
drh | 60c96cd | 2013-06-09 17:21:25 +0000 | [diff] [blame] | 6705 | if( pWInfo->pOrderBy==0 && (db->flags & SQLITE_ReverseOrder)!=0 ){ |
drh | d84ce35 | 2013-06-04 18:27:41 +0000 | [diff] [blame] | 6706 | pWInfo->revMask = (Bitmask)(-1); |
drh | a50ef11 | 2013-05-22 02:06:59 +0000 | [diff] [blame] | 6707 | } |
drh | 81186b4 | 2013-06-18 01:52:41 +0000 | [diff] [blame] | 6708 | if( pParse->nErr || NEVER(db->mallocFailed) ){ |
drh | 75b9340 | 2013-05-31 20:43:57 +0000 | [diff] [blame] | 6709 | goto whereBeginError; |
| 6710 | } |
drh | 989578e | 2013-10-28 14:34:35 +0000 | [diff] [blame] | 6711 | #ifdef WHERETRACE_ENABLED /* !=0 */ |
drh | a18f3d2 | 2013-05-08 03:05:41 +0000 | [diff] [blame] | 6712 | if( sqlite3WhereTrace ){ |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 6713 | sqlite3DebugPrintf("---- Solution nRow=%d", pWInfo->nRowOut); |
drh | ddba0c2 | 2014-03-18 20:33:42 +0000 | [diff] [blame] | 6714 | if( pWInfo->nOBSat>0 ){ |
| 6715 | sqlite3DebugPrintf(" ORDERBY=%d,0x%llx", pWInfo->nOBSat, pWInfo->revMask); |
drh | 319f677 | 2013-05-14 15:31:07 +0000 | [diff] [blame] | 6716 | } |
drh | 4f402f2 | 2013-06-11 18:59:38 +0000 | [diff] [blame] | 6717 | switch( pWInfo->eDistinct ){ |
| 6718 | case WHERE_DISTINCT_UNIQUE: { |
| 6719 | sqlite3DebugPrintf(" DISTINCT=unique"); |
| 6720 | break; |
| 6721 | } |
| 6722 | case WHERE_DISTINCT_ORDERED: { |
| 6723 | sqlite3DebugPrintf(" DISTINCT=ordered"); |
| 6724 | break; |
| 6725 | } |
| 6726 | case WHERE_DISTINCT_UNORDERED: { |
| 6727 | sqlite3DebugPrintf(" DISTINCT=unordered"); |
| 6728 | break; |
| 6729 | } |
| 6730 | } |
| 6731 | sqlite3DebugPrintf("\n"); |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 6732 | for(ii=0; ii<pWInfo->nLevel; ii++){ |
drh | c1ba2e7 | 2013-10-28 19:03:21 +0000 | [diff] [blame] | 6733 | whereLoopPrint(pWInfo->a[ii].pWLoop, sWLB.pWC); |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 6734 | } |
| 6735 | } |
| 6736 | #endif |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 6737 | /* Attempt to omit tables from the join that do not effect the result */ |
drh | 1031bd9 | 2013-06-22 15:44:26 +0000 | [diff] [blame] | 6738 | if( pWInfo->nLevel>=2 |
| 6739 | && pResultSet!=0 |
| 6740 | && OptimizationEnabled(db, SQLITE_OmitNoopJoin) |
| 6741 | ){ |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 6742 | Bitmask tabUsed = exprListTableUsage(pMaskSet, pResultSet); |
drh | 67a5ec7 | 2013-09-03 14:03:47 +0000 | [diff] [blame] | 6743 | if( sWLB.pOrderBy ) tabUsed |= exprListTableUsage(pMaskSet, sWLB.pOrderBy); |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 6744 | while( pWInfo->nLevel>=2 ){ |
drh | 9d5a579 | 2013-06-28 13:43:33 +0000 | [diff] [blame] | 6745 | WhereTerm *pTerm, *pEnd; |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 6746 | pLoop = pWInfo->a[pWInfo->nLevel-1].pWLoop; |
drh | bc71b1d | 2013-06-21 02:15:48 +0000 | [diff] [blame] | 6747 | if( (pWInfo->pTabList->a[pLoop->iTab].jointype & JT_LEFT)==0 ) break; |
| 6748 | if( (wctrlFlags & WHERE_WANT_DISTINCT)==0 |
| 6749 | && (pLoop->wsFlags & WHERE_ONEROW)==0 |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 6750 | ){ |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 6751 | break; |
| 6752 | } |
drh | bc71b1d | 2013-06-21 02:15:48 +0000 | [diff] [blame] | 6753 | if( (tabUsed & pLoop->maskSelf)!=0 ) break; |
drh | 9d5a579 | 2013-06-28 13:43:33 +0000 | [diff] [blame] | 6754 | pEnd = sWLB.pWC->a + sWLB.pWC->nTerm; |
| 6755 | for(pTerm=sWLB.pWC->a; pTerm<pEnd; pTerm++){ |
| 6756 | if( (pTerm->prereqAll & pLoop->maskSelf)!=0 |
| 6757 | && !ExprHasProperty(pTerm->pExpr, EP_FromJoin) |
| 6758 | ){ |
| 6759 | break; |
| 6760 | } |
| 6761 | } |
| 6762 | if( pTerm<pEnd ) break; |
drh | bc71b1d | 2013-06-21 02:15:48 +0000 | [diff] [blame] | 6763 | WHERETRACE(0xffff, ("-> drop loop %c not used\n", pLoop->cId)); |
| 6764 | pWInfo->nLevel--; |
| 6765 | nTabList--; |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 6766 | } |
| 6767 | } |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 6768 | WHERETRACE(0xffff,("*** Optimizer Finished ***\n")); |
drh | 8e23daf | 2013-06-11 13:30:04 +0000 | [diff] [blame] | 6769 | pWInfo->pParse->nQueryLoop += pWInfo->nRowOut; |
drh | f1b5f5b | 2013-05-02 00:15:01 +0000 | [diff] [blame] | 6770 | |
drh | 08c88eb | 2008-04-10 13:33:18 +0000 | [diff] [blame] | 6771 | /* If the caller is an UPDATE or DELETE statement that is requesting |
| 6772 | ** to use a one-pass algorithm, determine if this is appropriate. |
drh | 24b7fe9 | 2013-09-30 19:33:06 +0000 | [diff] [blame] | 6773 | ** The one-pass algorithm only works if the WHERE clause constrains |
drh | 08c88eb | 2008-04-10 13:33:18 +0000 | [diff] [blame] | 6774 | ** the statement to update a single row. |
| 6775 | */ |
drh | 165be38 | 2008-12-05 02:36:33 +0000 | [diff] [blame] | 6776 | assert( (wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || pWInfo->nLevel==1 ); |
drh | 3b48e8c | 2013-06-12 20:18:16 +0000 | [diff] [blame] | 6777 | if( (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0 |
| 6778 | && (pWInfo->a[0].pWLoop->wsFlags & WHERE_ONEROW)!=0 ){ |
drh | 08c88eb | 2008-04-10 13:33:18 +0000 | [diff] [blame] | 6779 | pWInfo->okOnePass = 1; |
drh | 702ba9f | 2013-11-07 21:25:13 +0000 | [diff] [blame] | 6780 | if( HasRowid(pTabList->a[0].pTab) ){ |
| 6781 | pWInfo->a[0].pWLoop->wsFlags &= ~WHERE_IDX_ONLY; |
| 6782 | } |
drh | 08c88eb | 2008-04-10 13:33:18 +0000 | [diff] [blame] | 6783 | } |
drh | eb04de3 | 2013-05-10 15:16:30 +0000 | [diff] [blame] | 6784 | |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6785 | /* Open all tables in the pTabList and any indices selected for |
| 6786 | ** searching those tables. |
| 6787 | */ |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 6788 | notReady = ~(Bitmask)0; |
drh | 9cd1c99 | 2012-09-25 20:43:35 +0000 | [diff] [blame] | 6789 | for(ii=0, pLevel=pWInfo->a; ii<nTabList; ii++, pLevel++){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6790 | Table *pTab; /* Table to open */ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6791 | int iDb; /* Index of database containing table/index */ |
drh | 56f1b99 | 2012-09-25 14:29:39 +0000 | [diff] [blame] | 6792 | struct SrcList_item *pTabItem; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6793 | |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 6794 | pTabItem = &pTabList->a[pLevel->iFrom]; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6795 | pTab = pTabItem->pTab; |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 6796 | iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 6797 | pLoop = pLevel->pWLoop; |
drh | 424aab8 | 2010-04-06 18:28:20 +0000 | [diff] [blame] | 6798 | if( (pTab->tabFlags & TF_Ephemeral)!=0 || pTab->pSelect ){ |
drh | 75bb9f5 | 2010-04-06 18:51:42 +0000 | [diff] [blame] | 6799 | /* Do nothing */ |
| 6800 | }else |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 6801 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 6802 | if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){ |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 6803 | const char *pVTab = (const char *)sqlite3GetVTable(db, pTab); |
danielk1977 | 93626f4 | 2006-06-20 13:07:27 +0000 | [diff] [blame] | 6804 | int iCur = pTabItem->iCursor; |
danielk1977 | 595a523 | 2009-07-24 17:58:53 +0000 | [diff] [blame] | 6805 | sqlite3VdbeAddOp4(v, OP_VOpen, iCur, 0, 0, pVTab, P4_VTAB); |
drh | fc5e546 | 2012-12-03 17:04:40 +0000 | [diff] [blame] | 6806 | }else if( IsVirtual(pTab) ){ |
| 6807 | /* noop */ |
drh | 9eff616 | 2006-06-12 21:59:13 +0000 | [diff] [blame] | 6808 | }else |
| 6809 | #endif |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 6810 | if( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 |
drh | 9ef61f4 | 2011-10-07 14:40:59 +0000 | [diff] [blame] | 6811 | && (wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0 ){ |
drh | fc8d4f9 | 2013-11-08 15:19:46 +0000 | [diff] [blame] | 6812 | int op = OP_OpenRead; |
| 6813 | if( pWInfo->okOnePass ){ |
| 6814 | op = OP_OpenWrite; |
| 6815 | pWInfo->aiCurOnePass[0] = pTabItem->iCursor; |
| 6816 | }; |
drh | 08c88eb | 2008-04-10 13:33:18 +0000 | [diff] [blame] | 6817 | sqlite3OpenTable(pParse, pTabItem->iCursor, iDb, pTab, op); |
drh | fc8d4f9 | 2013-11-08 15:19:46 +0000 | [diff] [blame] | 6818 | assert( pTabItem->iCursor==pLevel->iTabCur ); |
drh | 7963b0e | 2013-06-17 21:37:40 +0000 | [diff] [blame] | 6819 | testcase( !pWInfo->okOnePass && pTab->nCol==BMS-1 ); |
| 6820 | testcase( !pWInfo->okOnePass && pTab->nCol==BMS ); |
drh | dd9930e | 2013-10-23 23:37:02 +0000 | [diff] [blame] | 6821 | if( !pWInfo->okOnePass && pTab->nCol<BMS && HasRowid(pTab) ){ |
danielk1977 | 9792eef | 2006-01-13 15:58:43 +0000 | [diff] [blame] | 6822 | Bitmask b = pTabItem->colUsed; |
| 6823 | int n = 0; |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 6824 | for(; b; b=b>>1, n++){} |
drh | 8cff69d | 2009-11-12 19:59:44 +0000 | [diff] [blame] | 6825 | sqlite3VdbeChangeP4(v, sqlite3VdbeCurrentAddr(v)-1, |
| 6826 | SQLITE_INT_TO_PTR(n), P4_INT32); |
danielk1977 | 9792eef | 2006-01-13 15:58:43 +0000 | [diff] [blame] | 6827 | assert( n<=pTab->nCol ); |
| 6828 | } |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 6829 | }else{ |
| 6830 | sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName); |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6831 | } |
drh | 7e47cb8 | 2013-05-31 17:55:27 +0000 | [diff] [blame] | 6832 | if( pLoop->wsFlags & WHERE_INDEXED ){ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 6833 | Index *pIx = pLoop->u.btree.pIndex; |
drh | fc8d4f9 | 2013-11-08 15:19:46 +0000 | [diff] [blame] | 6834 | int iIndexCur; |
| 6835 | int op = OP_OpenRead; |
drh | 4308e34 | 2013-11-11 16:55:52 +0000 | [diff] [blame] | 6836 | /* iIdxCur is always set if to a positive value if ONEPASS is possible */ |
| 6837 | assert( iIdxCur!=0 || (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 ); |
drh | 48dd1d8 | 2014-05-27 18:18:58 +0000 | [diff] [blame] | 6838 | if( !HasRowid(pTab) && IsPrimaryKeyIndex(pIx) |
drh | a3bc66a | 2014-05-27 17:57:32 +0000 | [diff] [blame] | 6839 | && (wctrlFlags & WHERE_ONETABLE_ONLY)!=0 |
| 6840 | ){ |
| 6841 | /* This is one term of an OR-optimization using the PRIMARY KEY of a |
| 6842 | ** WITHOUT ROWID table. No need for a separate index */ |
| 6843 | iIndexCur = pLevel->iTabCur; |
| 6844 | op = 0; |
| 6845 | }else if( pWInfo->okOnePass ){ |
drh | fc8d4f9 | 2013-11-08 15:19:46 +0000 | [diff] [blame] | 6846 | Index *pJ = pTabItem->pTab->pIndex; |
| 6847 | iIndexCur = iIdxCur; |
| 6848 | assert( wctrlFlags & WHERE_ONEPASS_DESIRED ); |
| 6849 | while( ALWAYS(pJ) && pJ!=pIx ){ |
| 6850 | iIndexCur++; |
| 6851 | pJ = pJ->pNext; |
| 6852 | } |
| 6853 | op = OP_OpenWrite; |
| 6854 | pWInfo->aiCurOnePass[1] = iIndexCur; |
| 6855 | }else if( iIdxCur && (wctrlFlags & WHERE_ONETABLE_ONLY)!=0 ){ |
| 6856 | iIndexCur = iIdxCur; |
drh | 3526319 | 2014-07-22 20:02:19 +0000 | [diff] [blame] | 6857 | if( wctrlFlags & WHERE_REOPEN_IDX ) op = OP_ReopenIdx; |
drh | fc8d4f9 | 2013-11-08 15:19:46 +0000 | [diff] [blame] | 6858 | }else{ |
| 6859 | iIndexCur = pParse->nTab++; |
| 6860 | } |
| 6861 | pLevel->iIdxCur = iIndexCur; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6862 | assert( pIx->pSchema==pTab->pSchema ); |
drh | b0367fb | 2012-08-25 02:11:13 +0000 | [diff] [blame] | 6863 | assert( iIndexCur>=0 ); |
drh | a3bc66a | 2014-05-27 17:57:32 +0000 | [diff] [blame] | 6864 | if( op ){ |
| 6865 | sqlite3VdbeAddOp3(v, op, iIndexCur, pIx->tnum, iDb); |
| 6866 | sqlite3VdbeSetP4KeyInfo(pParse, pIx); |
drh | e0997b3 | 2015-03-20 14:57:50 +0000 | [diff] [blame] | 6867 | if( (pLoop->wsFlags & WHERE_CONSTRAINT)!=0 |
| 6868 | && (pLoop->wsFlags & (WHERE_COLUMN_RANGE|WHERE_SKIPSCAN))==0 |
| 6869 | && (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)==0 |
| 6870 | ){ |
| 6871 | sqlite3VdbeChangeP5(v, OPFLAG_SEEKEQ); /* Hint to COMDB2 */ |
| 6872 | } |
drh | a3bc66a | 2014-05-27 17:57:32 +0000 | [diff] [blame] | 6873 | VdbeComment((v, "%s", pIx->zName)); |
| 6874 | } |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6875 | } |
drh | aceb31b | 2014-02-08 01:40:27 +0000 | [diff] [blame] | 6876 | if( iDb>=0 ) sqlite3CodeVerifySchema(pParse, iDb); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 6877 | notReady &= ~getMask(&pWInfo->sMaskSet, pTabItem->iCursor); |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6878 | } |
| 6879 | pWInfo->iTop = sqlite3VdbeCurrentAddr(v); |
drh | a21a64d | 2010-04-06 22:33:55 +0000 | [diff] [blame] | 6880 | if( db->mallocFailed ) goto whereBeginError; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6881 | |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 6882 | /* Generate the code to do the search. Each iteration of the for |
| 6883 | ** loop below generates code for a single nested loop of the VM |
| 6884 | ** program. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6885 | */ |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 6886 | notReady = ~(Bitmask)0; |
drh | 9cd1c99 | 2012-09-25 20:43:35 +0000 | [diff] [blame] | 6887 | for(ii=0; ii<nTabList; ii++){ |
dan | 6f9702e | 2014-11-01 20:38:06 +0000 | [diff] [blame] | 6888 | int addrExplain; |
| 6889 | int wsFlags; |
drh | 9cd1c99 | 2012-09-25 20:43:35 +0000 | [diff] [blame] | 6890 | pLevel = &pWInfo->a[ii]; |
dan | 6f9702e | 2014-11-01 20:38:06 +0000 | [diff] [blame] | 6891 | wsFlags = pLevel->pWLoop->wsFlags; |
drh | cc04afd | 2013-08-22 02:56:28 +0000 | [diff] [blame] | 6892 | #ifndef SQLITE_OMIT_AUTOMATIC_INDEX |
| 6893 | if( (pLevel->pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 ){ |
| 6894 | constructAutomaticIndex(pParse, &pWInfo->sWC, |
| 6895 | &pTabList->a[pLevel->iFrom], notReady, pLevel); |
| 6896 | if( db->mallocFailed ) goto whereBeginError; |
| 6897 | } |
| 6898 | #endif |
dan | 6f9702e | 2014-11-01 20:38:06 +0000 | [diff] [blame] | 6899 | addrExplain = explainOneScan( |
| 6900 | pParse, pTabList, pLevel, ii, pLevel->iFrom, wctrlFlags |
| 6901 | ); |
drh | cc04afd | 2013-08-22 02:56:28 +0000 | [diff] [blame] | 6902 | pLevel->addrBody = sqlite3VdbeCurrentAddr(v); |
drh | 70d1834 | 2013-06-06 19:16:33 +0000 | [diff] [blame] | 6903 | notReady = codeOneLoopStart(pWInfo, ii, notReady); |
dan | 4a07e3d | 2010-11-09 14:48:59 +0000 | [diff] [blame] | 6904 | pWInfo->iContinue = pLevel->addrCont; |
dan | 6f9702e | 2014-11-01 20:38:06 +0000 | [diff] [blame] | 6905 | if( (wsFlags&WHERE_MULTI_OR)==0 && (wctrlFlags&WHERE_ONETABLE_ONLY)==0 ){ |
| 6906 | addScanStatus(v, pTabList, pLevel, addrExplain); |
| 6907 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6908 | } |
drh | 7ec764a | 2005-07-21 03:48:20 +0000 | [diff] [blame] | 6909 | |
drh | 6fa978d | 2013-05-30 19:29:19 +0000 | [diff] [blame] | 6910 | /* Done. */ |
drh | 6bc69a2 | 2013-11-19 12:33:23 +0000 | [diff] [blame] | 6911 | VdbeModuleComment((v, "Begin WHERE-core")); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6912 | return pWInfo; |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 6913 | |
| 6914 | /* Jump here if malloc fails */ |
danielk1977 | 85574e3 | 2008-10-06 05:32:18 +0000 | [diff] [blame] | 6915 | whereBeginError: |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 6916 | if( pWInfo ){ |
| 6917 | pParse->nQueryLoop = pWInfo->savedNQueryLoop; |
| 6918 | whereInfoFree(db, pWInfo); |
| 6919 | } |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 6920 | return 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6921 | } |
| 6922 | |
| 6923 | /* |
drh | c27a1ce | 2002-06-14 20:58:45 +0000 | [diff] [blame] | 6924 | ** Generate the end of the WHERE loop. See comments on |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 6925 | ** sqlite3WhereBegin() for additional information. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6926 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 6927 | void sqlite3WhereEnd(WhereInfo *pWInfo){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 6928 | Parse *pParse = pWInfo->pParse; |
| 6929 | Vdbe *v = pParse->pVdbe; |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 6930 | int i; |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 6931 | WhereLevel *pLevel; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 6932 | WhereLoop *pLoop; |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 6933 | SrcList *pTabList = pWInfo->pTabList; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 6934 | sqlite3 *db = pParse->db; |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 6935 | |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 6936 | /* Generate loop termination code. |
| 6937 | */ |
drh | 6bc69a2 | 2013-11-19 12:33:23 +0000 | [diff] [blame] | 6938 | VdbeModuleComment((v, "End WHERE-core")); |
drh | ceea332 | 2009-04-23 13:22:42 +0000 | [diff] [blame] | 6939 | sqlite3ExprCacheClear(pParse); |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 6940 | for(i=pWInfo->nLevel-1; i>=0; i--){ |
drh | cd8629e | 2013-11-13 12:27:25 +0000 | [diff] [blame] | 6941 | int addr; |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 6942 | pLevel = &pWInfo->a[i]; |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 6943 | pLoop = pLevel->pWLoop; |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 6944 | sqlite3VdbeResolveLabel(v, pLevel->addrCont); |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 6945 | if( pLevel->op!=OP_Noop ){ |
drh | e39a732 | 2014-02-03 14:04:11 +0000 | [diff] [blame] | 6946 | sqlite3VdbeAddOp3(v, pLevel->op, pLevel->p1, pLevel->p2, pLevel->p3); |
drh | d1d3848 | 2008-10-07 23:46:38 +0000 | [diff] [blame] | 6947 | sqlite3VdbeChangeP5(v, pLevel->p5); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 6948 | VdbeCoverage(v); |
drh | 7d17610 | 2014-02-18 03:07:12 +0000 | [diff] [blame] | 6949 | VdbeCoverageIf(v, pLevel->op==OP_Next); |
| 6950 | VdbeCoverageIf(v, pLevel->op==OP_Prev); |
| 6951 | VdbeCoverageIf(v, pLevel->op==OP_VNext); |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 6952 | } |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 6953 | if( pLoop->wsFlags & WHERE_IN_ABLE && pLevel->u.in.nIn>0 ){ |
drh | 72e8fa4 | 2007-03-28 14:30:06 +0000 | [diff] [blame] | 6954 | struct InLoop *pIn; |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 6955 | int j; |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 6956 | sqlite3VdbeResolveLabel(v, pLevel->addrNxt); |
drh | 111a6a7 | 2008-12-21 03:51:16 +0000 | [diff] [blame] | 6957 | 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] | 6958 | sqlite3VdbeJumpHere(v, pIn->addrInTop+1); |
drh | 2d96b93 | 2013-02-08 18:48:23 +0000 | [diff] [blame] | 6959 | sqlite3VdbeAddOp2(v, pIn->eEndLoopOp, pIn->iCur, pIn->addrInTop); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 6960 | VdbeCoverage(v); |
drh | 7d17610 | 2014-02-18 03:07:12 +0000 | [diff] [blame] | 6961 | VdbeCoverageIf(v, pIn->eEndLoopOp==OP_PrevIfOpen); |
| 6962 | VdbeCoverageIf(v, pIn->eEndLoopOp==OP_NextIfOpen); |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 6963 | sqlite3VdbeJumpHere(v, pIn->addrInTop-1); |
drh | e23399f | 2005-07-22 00:31:39 +0000 | [diff] [blame] | 6964 | } |
drh | d99f706 | 2002-06-08 23:25:08 +0000 | [diff] [blame] | 6965 | } |
drh | b3190c1 | 2008-12-08 21:37:14 +0000 | [diff] [blame] | 6966 | sqlite3VdbeResolveLabel(v, pLevel->addrBrk); |
drh | cd8629e | 2013-11-13 12:27:25 +0000 | [diff] [blame] | 6967 | if( pLevel->addrSkip ){ |
drh | cd8629e | 2013-11-13 12:27:25 +0000 | [diff] [blame] | 6968 | sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrSkip); |
drh | e084f40 | 2013-11-13 17:24:38 +0000 | [diff] [blame] | 6969 | VdbeComment((v, "next skip-scan on %s", pLoop->u.btree.pIndex->zName)); |
drh | 2e5ef4e | 2013-11-13 16:58:54 +0000 | [diff] [blame] | 6970 | sqlite3VdbeJumpHere(v, pLevel->addrSkip); |
| 6971 | sqlite3VdbeJumpHere(v, pLevel->addrSkip-2); |
drh | cd8629e | 2013-11-13 12:27:25 +0000 | [diff] [blame] | 6972 | } |
drh | f07cf6e | 2015-03-06 16:45:16 +0000 | [diff] [blame] | 6973 | if( pLevel->addrLikeRep ){ |
drh | b7c60ba | 2015-03-07 02:51:59 +0000 | [diff] [blame] | 6974 | int op; |
| 6975 | if( sqlite3VdbeGetOp(v, pLevel->addrLikeRep-1)->p1 ){ |
| 6976 | op = OP_DecrJumpZero; |
| 6977 | }else{ |
| 6978 | op = OP_JumpZeroIncr; |
| 6979 | } |
| 6980 | sqlite3VdbeAddOp2(v, op, pLevel->iLikeRepCntr, pLevel->addrLikeRep); |
drh | f07cf6e | 2015-03-06 16:45:16 +0000 | [diff] [blame] | 6981 | VdbeCoverage(v); |
drh | f07cf6e | 2015-03-06 16:45:16 +0000 | [diff] [blame] | 6982 | } |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 6983 | if( pLevel->iLeftJoin ){ |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 6984 | addr = sqlite3VdbeAddOp1(v, OP_IfPos, pLevel->iLeftJoin); VdbeCoverage(v); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 6985 | assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 |
| 6986 | || (pLoop->wsFlags & WHERE_INDEXED)!=0 ); |
| 6987 | if( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 ){ |
drh | 35451c6 | 2009-11-12 04:26:39 +0000 | [diff] [blame] | 6988 | sqlite3VdbeAddOp1(v, OP_NullRow, pTabList->a[i].iCursor); |
| 6989 | } |
drh | 76f4cfb | 2013-05-31 18:20:52 +0000 | [diff] [blame] | 6990 | if( pLoop->wsFlags & WHERE_INDEXED ){ |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 6991 | sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iIdxCur); |
drh | 7f09b3e | 2002-08-13 13:15:49 +0000 | [diff] [blame] | 6992 | } |
drh | 336a530 | 2009-04-24 15:46:21 +0000 | [diff] [blame] | 6993 | if( pLevel->op==OP_Return ){ |
| 6994 | sqlite3VdbeAddOp2(v, OP_Gosub, pLevel->p1, pLevel->addrFirst); |
| 6995 | }else{ |
| 6996 | sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrFirst); |
| 6997 | } |
drh | d654be8 | 2005-09-20 17:42:23 +0000 | [diff] [blame] | 6998 | sqlite3VdbeJumpHere(v, addr); |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 6999 | } |
drh | 6bc69a2 | 2013-11-19 12:33:23 +0000 | [diff] [blame] | 7000 | VdbeModuleComment((v, "End WHERE-loop%d: %s", i, |
drh | fc8d4f9 | 2013-11-08 15:19:46 +0000 | [diff] [blame] | 7001 | pWInfo->pTabList->a[pLevel->iFrom].pTab->zName)); |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 7002 | } |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 7003 | |
| 7004 | /* The "break" point is here, just past the end of the outer loop. |
| 7005 | ** Set it. |
| 7006 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 7007 | sqlite3VdbeResolveLabel(v, pWInfo->iBreak); |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 7008 | |
drh | fd636c7 | 2013-06-21 02:05:06 +0000 | [diff] [blame] | 7009 | assert( pWInfo->nLevel<=pTabList->nSrc ); |
drh | c01a3c1 | 2009-12-16 22:10:49 +0000 | [diff] [blame] | 7010 | for(i=0, pLevel=pWInfo->a; i<pWInfo->nLevel; i++, pLevel++){ |
drh | 5f61229 | 2014-02-08 23:20:32 +0000 | [diff] [blame] | 7011 | int k, last; |
| 7012 | VdbeOp *pOp; |
dan | bfca6a4 | 2012-08-24 10:52:35 +0000 | [diff] [blame] | 7013 | Index *pIdx = 0; |
drh | 29dda4a | 2005-07-21 18:23:20 +0000 | [diff] [blame] | 7014 | struct SrcList_item *pTabItem = &pTabList->a[pLevel->iFrom]; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 7015 | Table *pTab = pTabItem->pTab; |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 7016 | assert( pTab!=0 ); |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 7017 | pLoop = pLevel->pWLoop; |
drh | fc8d4f9 | 2013-11-08 15:19:46 +0000 | [diff] [blame] | 7018 | |
drh | 5f61229 | 2014-02-08 23:20:32 +0000 | [diff] [blame] | 7019 | /* For a co-routine, change all OP_Column references to the table of |
| 7020 | ** the co-routine into OP_SCopy of result contained in a register. |
| 7021 | ** OP_Rowid becomes OP_Null. |
| 7022 | */ |
dan | fbf0f0e | 2014-03-03 14:20:30 +0000 | [diff] [blame] | 7023 | if( pTabItem->viaCoroutine && !db->mallocFailed ){ |
drh | 5f61229 | 2014-02-08 23:20:32 +0000 | [diff] [blame] | 7024 | last = sqlite3VdbeCurrentAddr(v); |
| 7025 | k = pLevel->addrBody; |
| 7026 | pOp = sqlite3VdbeGetOp(v, k); |
| 7027 | for(; k<last; k++, pOp++){ |
| 7028 | if( pOp->p1!=pLevel->iTabCur ) continue; |
| 7029 | if( pOp->opcode==OP_Column ){ |
drh | c438df1 | 2014-04-03 16:29:31 +0000 | [diff] [blame] | 7030 | pOp->opcode = OP_Copy; |
drh | 5f61229 | 2014-02-08 23:20:32 +0000 | [diff] [blame] | 7031 | pOp->p1 = pOp->p2 + pTabItem->regResult; |
| 7032 | pOp->p2 = pOp->p3; |
| 7033 | pOp->p3 = 0; |
| 7034 | }else if( pOp->opcode==OP_Rowid ){ |
| 7035 | pOp->opcode = OP_Null; |
| 7036 | pOp->p1 = 0; |
| 7037 | pOp->p3 = 0; |
| 7038 | } |
| 7039 | } |
| 7040 | continue; |
| 7041 | } |
| 7042 | |
drh | fc8d4f9 | 2013-11-08 15:19:46 +0000 | [diff] [blame] | 7043 | /* Close all of the cursors that were opened by sqlite3WhereBegin. |
| 7044 | ** Except, do not close cursors that will be reused by the OR optimization |
| 7045 | ** (WHERE_OMIT_OPEN_CLOSE). And do not close the OP_OpenWrite cursors |
| 7046 | ** created for the ONEPASS optimization. |
| 7047 | */ |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 7048 | if( (pTab->tabFlags & TF_Ephemeral)==0 |
| 7049 | && pTab->pSelect==0 |
drh | 9ef61f4 | 2011-10-07 14:40:59 +0000 | [diff] [blame] | 7050 | && (pWInfo->wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0 |
drh | 4139c99 | 2010-04-07 14:59:45 +0000 | [diff] [blame] | 7051 | ){ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 7052 | int ws = pLoop->wsFlags; |
drh | 8b307fb | 2010-04-06 15:57:05 +0000 | [diff] [blame] | 7053 | if( !pWInfo->okOnePass && (ws & WHERE_IDX_ONLY)==0 ){ |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 7054 | sqlite3VdbeAddOp1(v, OP_Close, pTabItem->iCursor); |
| 7055 | } |
drh | fc8d4f9 | 2013-11-08 15:19:46 +0000 | [diff] [blame] | 7056 | if( (ws & WHERE_INDEXED)!=0 |
| 7057 | && (ws & (WHERE_IPK|WHERE_AUTO_INDEX))==0 |
| 7058 | && pLevel->iIdxCur!=pWInfo->aiCurOnePass[1] |
| 7059 | ){ |
drh | 6df2acd | 2008-12-28 16:55:25 +0000 | [diff] [blame] | 7060 | sqlite3VdbeAddOp1(v, OP_Close, pLevel->iIdxCur); |
| 7061 | } |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 7062 | } |
| 7063 | |
drh | f003076 | 2013-06-14 13:27:01 +0000 | [diff] [blame] | 7064 | /* If this scan uses an index, make VDBE code substitutions to read data |
| 7065 | ** from the index instead of from the table where possible. In some cases |
| 7066 | ** this optimization prevents the table from ever being read, which can |
| 7067 | ** yield a significant performance boost. |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 7068 | ** |
| 7069 | ** Calls to the code generator in between sqlite3WhereBegin and |
| 7070 | ** sqlite3WhereEnd will have created code that references the table |
| 7071 | ** directly. This loop scans all that code looking for opcodes |
| 7072 | ** that reference the table and converts them into opcodes that |
| 7073 | ** reference the index. |
| 7074 | */ |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 7075 | if( pLoop->wsFlags & (WHERE_INDEXED|WHERE_IDX_ONLY) ){ |
| 7076 | pIdx = pLoop->u.btree.pIndex; |
| 7077 | }else if( pLoop->wsFlags & WHERE_MULTI_OR ){ |
drh | d40e208 | 2012-08-24 23:24:15 +0000 | [diff] [blame] | 7078 | pIdx = pLevel->u.pCovidx; |
dan | bfca6a4 | 2012-08-24 10:52:35 +0000 | [diff] [blame] | 7079 | } |
drh | 7ba39a9 | 2013-05-30 17:43:19 +0000 | [diff] [blame] | 7080 | if( pIdx && !db->mallocFailed ){ |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 7081 | last = sqlite3VdbeCurrentAddr(v); |
drh | cc04afd | 2013-08-22 02:56:28 +0000 | [diff] [blame] | 7082 | k = pLevel->addrBody; |
| 7083 | pOp = sqlite3VdbeGetOp(v, k); |
| 7084 | for(; k<last; k++, pOp++){ |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 7085 | if( pOp->p1!=pLevel->iTabCur ) continue; |
| 7086 | if( pOp->opcode==OP_Column ){ |
drh | ee0ec8e | 2013-10-31 17:38:01 +0000 | [diff] [blame] | 7087 | int x = pOp->p2; |
drh | 511717c | 2013-11-08 17:13:23 +0000 | [diff] [blame] | 7088 | assert( pIdx->pTable==pTab ); |
drh | ee0ec8e | 2013-10-31 17:38:01 +0000 | [diff] [blame] | 7089 | if( !HasRowid(pTab) ){ |
| 7090 | Index *pPk = sqlite3PrimaryKeyIndex(pTab); |
| 7091 | x = pPk->aiColumn[x]; |
| 7092 | } |
| 7093 | x = sqlite3ColumnOfIndex(pIdx, x); |
drh | 4415628 | 2013-10-23 22:23:03 +0000 | [diff] [blame] | 7094 | if( x>=0 ){ |
| 7095 | pOp->p2 = x; |
| 7096 | pOp->p1 = pLevel->iIdxCur; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 7097 | } |
drh | 4415628 | 2013-10-23 22:23:03 +0000 | [diff] [blame] | 7098 | assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 || x>=0 ); |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 7099 | }else if( pOp->opcode==OP_Rowid ){ |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 7100 | pOp->p1 = pLevel->iIdxCur; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 7101 | pOp->opcode = OP_IdxRowid; |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 7102 | } |
| 7103 | } |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 7104 | } |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 7105 | } |
drh | 9012bcb | 2004-12-19 00:11:35 +0000 | [diff] [blame] | 7106 | |
| 7107 | /* Final cleanup |
| 7108 | */ |
drh | f12cde5 | 2010-04-08 17:28:00 +0000 | [diff] [blame] | 7109 | pParse->nQueryLoop = pWInfo->savedNQueryLoop; |
| 7110 | whereInfoFree(db, pWInfo); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 7111 | return; |
| 7112 | } |