Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/sqlite/src/src/where.c |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | /* | - | ||||||||||||||||||||||||
2 | ** 2001 September 15 | - | ||||||||||||||||||||||||
3 | ** | - | ||||||||||||||||||||||||
4 | ** The author disclaims copyright to this source code. In place of | - | ||||||||||||||||||||||||
5 | ** a legal notice, here is a blessing: | - | ||||||||||||||||||||||||
6 | ** | - | ||||||||||||||||||||||||
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. | - | ||||||||||||||||||||||||
10 | ** | - | ||||||||||||||||||||||||
11 | ************************************************************************* | - | ||||||||||||||||||||||||
12 | ** This module contains C code that generates VDBE code used to process | - | ||||||||||||||||||||||||
13 | ** the WHERE clause of SQL statements. This module is responsible for | - | ||||||||||||||||||||||||
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". | - | ||||||||||||||||||||||||
18 | */ | - | ||||||||||||||||||||||||
19 | #include "sqliteInt.h" | - | ||||||||||||||||||||||||
20 | #include "whereInt.h" | - | ||||||||||||||||||||||||
21 | - | |||||||||||||||||||||||||
22 | /* | - | ||||||||||||||||||||||||
23 | ** Extra information appended to the end of sqlite3_index_info but not | - | ||||||||||||||||||||||||
24 | ** visible to the xBestIndex function, at least not directly. The | - | ||||||||||||||||||||||||
25 | ** sqlite3_vtab_collation() interface knows how to reach it, however. | - | ||||||||||||||||||||||||
26 | ** | - | ||||||||||||||||||||||||
27 | ** This object is not an API and can be changed from one release to the | - | ||||||||||||||||||||||||
28 | ** next. As long as allocateIndexInfo() and sqlite3_vtab_collation() | - | ||||||||||||||||||||||||
29 | ** agree on the structure, all will be well. | - | ||||||||||||||||||||||||
30 | */ | - | ||||||||||||||||||||||||
31 | typedef struct HiddenIndexInfo HiddenIndexInfo; | - | ||||||||||||||||||||||||
32 | struct HiddenIndexInfo { | - | ||||||||||||||||||||||||
33 | WhereClause *pWC; /* The Where clause being analyzed */ | - | ||||||||||||||||||||||||
34 | Parse *pParse; /* The parsing context */ | - | ||||||||||||||||||||||||
35 | }; | - | ||||||||||||||||||||||||
36 | - | |||||||||||||||||||||||||
37 | /* Forward declaration of methods */ | - | ||||||||||||||||||||||||
38 | static int whereLoopResize(sqlite3*, WhereLoop*, int); | - | ||||||||||||||||||||||||
39 | - | |||||||||||||||||||||||||
40 | /* Test variable that can be set to enable WHERE tracing */ | - | ||||||||||||||||||||||||
41 | #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG) | - | ||||||||||||||||||||||||
42 | /***/ int sqlite3WhereTrace = 0; | - | ||||||||||||||||||||||||
43 | #endif | - | ||||||||||||||||||||||||
44 | - | |||||||||||||||||||||||||
45 | - | |||||||||||||||||||||||||
46 | /* | - | ||||||||||||||||||||||||
47 | ** Return the estimated number of output rows from a WHERE clause | - | ||||||||||||||||||||||||
48 | */ | - | ||||||||||||||||||||||||
49 | LogEst sqlite3WhereOutputRowCount(WhereInfo *pWInfo){ | - | ||||||||||||||||||||||||
50 | return pWInfo->nRowOut; executed 489286 times by 435 tests: return pWInfo->nRowOut; Executed by:
| 489286 | ||||||||||||||||||||||||
51 | } | - | ||||||||||||||||||||||||
52 | - | |||||||||||||||||||||||||
53 | /* | - | ||||||||||||||||||||||||
54 | ** Return one of the WHERE_DISTINCT_xxxxx values to indicate how this | - | ||||||||||||||||||||||||
55 | ** WHERE clause returns outputs for DISTINCT processing. | - | ||||||||||||||||||||||||
56 | */ | - | ||||||||||||||||||||||||
57 | int sqlite3WhereIsDistinct(WhereInfo *pWInfo){ | - | ||||||||||||||||||||||||
58 | return pWInfo->eDistinct; executed 348 times by 1 test: return pWInfo->eDistinct; Executed by:
| 348 | ||||||||||||||||||||||||
59 | } | - | ||||||||||||||||||||||||
60 | - | |||||||||||||||||||||||||
61 | /* | - | ||||||||||||||||||||||||
62 | ** Return TRUE if the WHERE clause returns rows in ORDER BY order. | - | ||||||||||||||||||||||||
63 | ** Return FALSE if the output needs to be sorted. | - | ||||||||||||||||||||||||
64 | */ | - | ||||||||||||||||||||||||
65 | int sqlite3WhereIsOrdered(WhereInfo *pWInfo){ | - | ||||||||||||||||||||||||
66 | return pWInfo->nOBSat; executed 80481 times by 435 tests: return pWInfo->nOBSat; Executed by:
| 80481 | ||||||||||||||||||||||||
67 | } | - | ||||||||||||||||||||||||
68 | - | |||||||||||||||||||||||||
69 | /* | - | ||||||||||||||||||||||||
70 | ** In the ORDER BY LIMIT optimization, if the inner-most loop is known | - | ||||||||||||||||||||||||
71 | ** to emit rows in increasing order, and if the last row emitted by the | - | ||||||||||||||||||||||||
72 | ** inner-most loop did not fit within the sorter, then we can skip all | - | ||||||||||||||||||||||||
73 | ** subsequent rows for the current iteration of the inner loop (because they | - | ||||||||||||||||||||||||
74 | ** will not fit in the sorter either) and continue with the second inner | - | ||||||||||||||||||||||||
75 | ** loop - the loop immediately outside the inner-most. | - | ||||||||||||||||||||||||
76 | ** | - | ||||||||||||||||||||||||
77 | ** When a row does not fit in the sorter (because the sorter already | - | ||||||||||||||||||||||||
78 | ** holds LIMIT+OFFSET rows that are smaller), then a jump is made to the | - | ||||||||||||||||||||||||
79 | ** label returned by this function. | - | ||||||||||||||||||||||||
80 | ** | - | ||||||||||||||||||||||||
81 | ** If the ORDER BY LIMIT optimization applies, the jump destination should | - | ||||||||||||||||||||||||
82 | ** be the continuation for the second-inner-most loop. If the ORDER BY | - | ||||||||||||||||||||||||
83 | ** LIMIT optimization does not apply, then the jump destination should | - | ||||||||||||||||||||||||
84 | ** be the continuation for the inner-most loop. | - | ||||||||||||||||||||||||
85 | ** | - | ||||||||||||||||||||||||
86 | ** It is always safe for this routine to return the continuation of the | - | ||||||||||||||||||||||||
87 | ** inner-most loop, in the sense that a correct answer will result. | - | ||||||||||||||||||||||||
88 | ** Returning the continuation the second inner loop is an optimization | - | ||||||||||||||||||||||||
89 | ** that might make the code run a little faster, but should not change | - | ||||||||||||||||||||||||
90 | ** the final answer. | - | ||||||||||||||||||||||||
91 | */ | - | ||||||||||||||||||||||||
92 | int sqlite3WhereOrderByLimitOptLabel(WhereInfo *pWInfo){ | - | ||||||||||||||||||||||||
93 | WhereLevel *pInner; | - | ||||||||||||||||||||||||
94 | if( !pWInfo->bOrderedInnerLoop ){
| 51-57742 | ||||||||||||||||||||||||
95 | /* The ORDER BY LIMIT optimization does not apply. Jump to the | - | ||||||||||||||||||||||||
96 | ** continuation of the inner-most loop. */ | - | ||||||||||||||||||||||||
97 | return pWInfo->iContinue; executed 57742 times by 435 tests: return pWInfo->iContinue; Executed by:
| 57742 | ||||||||||||||||||||||||
98 | } | - | ||||||||||||||||||||||||
99 | pInner = &pWInfo->a[pWInfo->nLevel-1]; | - | ||||||||||||||||||||||||
100 | assert( pInner->addrNxt!=0 ); | - | ||||||||||||||||||||||||
101 | return pInner->addrNxt; executed 51 times by 1 test: return pInner->addrNxt; Executed by:
| 51 | ||||||||||||||||||||||||
102 | } | - | ||||||||||||||||||||||||
103 | - | |||||||||||||||||||||||||
104 | /* | - | ||||||||||||||||||||||||
105 | ** Return the VDBE address or label to jump to in order to continue | - | ||||||||||||||||||||||||
106 | ** immediately with the next row of a WHERE clause. | - | ||||||||||||||||||||||||
107 | */ | - | ||||||||||||||||||||||||
108 | int sqlite3WhereContinueLabel(WhereInfo *pWInfo){ | - | ||||||||||||||||||||||||
109 | assert( pWInfo->iContinue!=0 ); | - | ||||||||||||||||||||||||
110 | return pWInfo->iContinue; executed 248618 times by 435 tests: return pWInfo->iContinue; Executed by:
| 248618 | ||||||||||||||||||||||||
111 | } | - | ||||||||||||||||||||||||
112 | - | |||||||||||||||||||||||||
113 | /* | - | ||||||||||||||||||||||||
114 | ** Return the VDBE address or label to jump to in order to break | - | ||||||||||||||||||||||||
115 | ** out of a WHERE loop. | - | ||||||||||||||||||||||||
116 | */ | - | ||||||||||||||||||||||||
117 | int sqlite3WhereBreakLabel(WhereInfo *pWInfo){ | - | ||||||||||||||||||||||||
118 | return pWInfo->iBreak; executed 249683 times by 435 tests: return pWInfo->iBreak; Executed by:
| 249683 | ||||||||||||||||||||||||
119 | } | - | ||||||||||||||||||||||||
120 | - | |||||||||||||||||||||||||
121 | /* | - | ||||||||||||||||||||||||
122 | ** Return ONEPASS_OFF (0) if an UPDATE or DELETE statement is unable to | - | ||||||||||||||||||||||||
123 | ** operate directly on the rowis returned by a WHERE clause. Return | - | ||||||||||||||||||||||||
124 | ** ONEPASS_SINGLE (1) if the statement can operation directly because only | - | ||||||||||||||||||||||||
125 | ** a single row is to be changed. Return ONEPASS_MULTI (2) if the one-pass | - | ||||||||||||||||||||||||
126 | ** optimization can be used on multiple | - | ||||||||||||||||||||||||
127 | ** | - | ||||||||||||||||||||||||
128 | ** If the ONEPASS optimization is used (if this routine returns true) | - | ||||||||||||||||||||||||
129 | ** then also write the indices of open cursors used by ONEPASS | - | ||||||||||||||||||||||||
130 | ** into aiCur[0] and aiCur[1]. iaCur[0] gets the cursor of the data | - | ||||||||||||||||||||||||
131 | ** table and iaCur[1] gets the cursor used by an auxiliary index. | - | ||||||||||||||||||||||||
132 | ** Either value may be -1, indicating that cursor is not used. | - | ||||||||||||||||||||||||
133 | ** Any cursors returned will have been opened for writing. | - | ||||||||||||||||||||||||
134 | ** | - | ||||||||||||||||||||||||
135 | ** aiCur[0] and aiCur[1] both get -1 if the where-clause logic is | - | ||||||||||||||||||||||||
136 | ** unable to use the ONEPASS optimization. | - | ||||||||||||||||||||||||
137 | */ | - | ||||||||||||||||||||||||
138 | int sqlite3WhereOkOnePass(WhereInfo *pWInfo, int *aiCur){ | - | ||||||||||||||||||||||||
139 | memcpy(aiCur, pWInfo->aiCurOnePass, sizeof(int)*2); | - | ||||||||||||||||||||||||
140 | #ifdef WHERETRACE_ENABLED | - | ||||||||||||||||||||||||
141 | if( sqlite3WhereTrace && pWInfo->eOnePass!=ONEPASS_OFF ){ | - | ||||||||||||||||||||||||
142 | sqlite3DebugPrintf("%s cursors: %d %d\n", | - | ||||||||||||||||||||||||
143 | pWInfo->eOnePass==ONEPASS_SINGLE ? "ONEPASS_SINGLE" : "ONEPASS_MULTI", | - | ||||||||||||||||||||||||
144 | aiCur[0], aiCur[1]); | - | ||||||||||||||||||||||||
145 | } | - | ||||||||||||||||||||||||
146 | #endif | - | ||||||||||||||||||||||||
147 | return pWInfo->eOnePass; executed 40356 times by 378 tests: return pWInfo->eOnePass; Executed by:
| 40356 | ||||||||||||||||||||||||
148 | } | - | ||||||||||||||||||||||||
149 | - | |||||||||||||||||||||||||
150 | /* | - | ||||||||||||||||||||||||
151 | ** Move the content of pSrc into pDest | - | ||||||||||||||||||||||||
152 | */ | - | ||||||||||||||||||||||||
153 | static void whereOrMove(WhereOrSet *pDest, WhereOrSet *pSrc){ | - | ||||||||||||||||||||||||
154 | pDest->n = pSrc->n; | - | ||||||||||||||||||||||||
155 | memcpy(pDest->a, pSrc->a, pDest->n*sizeof(pDest->a[0])); | - | ||||||||||||||||||||||||
156 | } executed 18177 times by 1 test: end of block Executed by:
| 18177 | ||||||||||||||||||||||||
157 | - | |||||||||||||||||||||||||
158 | /* | - | ||||||||||||||||||||||||
159 | ** Try to insert a new prerequisite/cost entry into the WhereOrSet pSet. | - | ||||||||||||||||||||||||
160 | ** | - | ||||||||||||||||||||||||
161 | ** The new entry might overwrite an existing entry, or it might be | - | ||||||||||||||||||||||||
162 | ** appended, or it might be discarded. Do whatever is the right thing | - | ||||||||||||||||||||||||
163 | ** so that pSet keeps the N_OR_COST best entries seen so far. | - | ||||||||||||||||||||||||
164 | */ | - | ||||||||||||||||||||||||
165 | static int whereOrInsert( | - | ||||||||||||||||||||||||
166 | WhereOrSet *pSet, /* The WhereOrSet to be updated */ | - | ||||||||||||||||||||||||
167 | Bitmask prereq, /* Prerequisites of the new entry */ | - | ||||||||||||||||||||||||
168 | LogEst rRun, /* Run-cost of the new entry */ | - | ||||||||||||||||||||||||
169 | LogEst nOut /* Number of outputs for the new entry */ | - | ||||||||||||||||||||||||
170 | ){ | - | ||||||||||||||||||||||||
171 | u16 i; | - | ||||||||||||||||||||||||
172 | WhereOrCost *p; | - | ||||||||||||||||||||||||
173 | for(i=pSet->n, p=pSet->a; i>0; i--, p++){
| 12221-33817 | ||||||||||||||||||||||||
174 | if( rRun<=p->rRun && (prereq & p->prereq)==prereq ){
| 35-6346 | ||||||||||||||||||||||||
175 | goto whereOrInsert_done; executed 5840 times by 1 test: goto whereOrInsert_done; Executed by:
| 5840 | ||||||||||||||||||||||||
176 | } | - | ||||||||||||||||||||||||
177 | if( p->rRun<=rRun && (p->prereq & prereq)==p->prereq ){
| 22-6359 | ||||||||||||||||||||||||
178 | return 0; executed 6321 times by 1 test: return 0; Executed by:
| 6321 | ||||||||||||||||||||||||
179 | } | - | ||||||||||||||||||||||||
180 | } executed 60 times by 1 test: end of block Executed by:
| 60 | ||||||||||||||||||||||||
181 | if( pSet->n<N_OR_COST ){
| 1-33816 | ||||||||||||||||||||||||
182 | p = &pSet->a[pSet->n++]; | - | ||||||||||||||||||||||||
183 | p->nOut = nOut; | - | ||||||||||||||||||||||||
184 | }else{ executed 33816 times by 1 test: end of block Executed by:
| 33816 | ||||||||||||||||||||||||
185 | p = pSet->a; | - | ||||||||||||||||||||||||
186 | for(i=1; i<pSet->n; i++){
| 1-2 | ||||||||||||||||||||||||
187 | if( p->rRun>pSet->a[i].rRun ) p = pSet->a + i; never executed: p = pSet->a + i;
| 0-2 | ||||||||||||||||||||||||
188 | } executed 2 times by 1 test: end of block Executed by:
| 2 | ||||||||||||||||||||||||
189 | if( p->rRun<=rRun ) return 0; executed 1 time by 1 test: return 0; Executed by:
| 0-1 | ||||||||||||||||||||||||
190 | } never executed: end of block | 0 | ||||||||||||||||||||||||
191 | whereOrInsert_done: code before this statement executed 33816 times by 1 test: whereOrInsert_done: Executed by:
| 33816 | ||||||||||||||||||||||||
192 | p->prereq = prereq; | - | ||||||||||||||||||||||||
193 | p->rRun = rRun; | - | ||||||||||||||||||||||||
194 | if( p->nOut>nOut ) p->nOut = nOut; executed 4885 times by 1 test: p->nOut = nOut; Executed by:
| 4885-34771 | ||||||||||||||||||||||||
195 | return 1; executed 39656 times by 1 test: return 1; Executed by:
| 39656 | ||||||||||||||||||||||||
196 | } | - | ||||||||||||||||||||||||
197 | - | |||||||||||||||||||||||||
198 | /* | - | ||||||||||||||||||||||||
199 | ** Return the bitmask for the given cursor number. Return 0 if | - | ||||||||||||||||||||||||
200 | ** iCursor is not in the set. | - | ||||||||||||||||||||||||
201 | */ | - | ||||||||||||||||||||||||
202 | Bitmask sqlite3WhereGetMask(WhereMaskSet *pMaskSet, int iCursor){ | - | ||||||||||||||||||||||||
203 | int i; | - | ||||||||||||||||||||||||
204 | assert( pMaskSet->n<=(int)sizeof(Bitmask)*8 ); | - | ||||||||||||||||||||||||
205 | for(i=0; i<pMaskSet->n; i++){
| 162712-1973090 | ||||||||||||||||||||||||
206 | if( pMaskSet->ix[i]==iCursor ){
| 659486-1313604 | ||||||||||||||||||||||||
207 | return MASKBIT(i); executed 1313604 times by 435 tests: return (((Bitmask)1)<<(i)); Executed by:
| 1313604 | ||||||||||||||||||||||||
208 | } | - | ||||||||||||||||||||||||
209 | } executed 659486 times by 1 test: end of block Executed by:
| 659486 | ||||||||||||||||||||||||
210 | return 0; executed 162712 times by 1 test: return 0; Executed by:
| 162712 | ||||||||||||||||||||||||
211 | } | - | ||||||||||||||||||||||||
212 | - | |||||||||||||||||||||||||
213 | /* | - | ||||||||||||||||||||||||
214 | ** Create a new mask for cursor iCursor. | - | ||||||||||||||||||||||||
215 | ** | - | ||||||||||||||||||||||||
216 | ** There is one cursor per table in the FROM clause. The number of | - | ||||||||||||||||||||||||
217 | ** tables in the FROM clause is limited by a test early in the | - | ||||||||||||||||||||||||
218 | ** sqlite3WhereBegin() routine. So we know that the pMaskSet->ix[] | - | ||||||||||||||||||||||||
219 | ** array will never overflow. | - | ||||||||||||||||||||||||
220 | */ | - | ||||||||||||||||||||||||
221 | static void createMask(WhereMaskSet *pMaskSet, int iCursor){ | - | ||||||||||||||||||||||||
222 | assert( pMaskSet->n < ArraySize(pMaskSet->ix) ); | - | ||||||||||||||||||||||||
223 | pMaskSet->ix[pMaskSet->n++] = iCursor; | - | ||||||||||||||||||||||||
224 | } executed 279537 times by 435 tests: end of block Executed by:
| 279537 | ||||||||||||||||||||||||
225 | - | |||||||||||||||||||||||||
226 | /* | - | ||||||||||||||||||||||||
227 | ** Advance to the next WhereTerm that matches according to the criteria | - | ||||||||||||||||||||||||
228 | ** established when the pScan object was initialized by whereScanInit(). | - | ||||||||||||||||||||||||
229 | ** Return NULL if there are no more matching WhereTerms. | - | ||||||||||||||||||||||||
230 | */ | - | ||||||||||||||||||||||||
231 | static WhereTerm *whereScanNext(WhereScan *pScan){ | - | ||||||||||||||||||||||||
232 | int iCur; /* The cursor on the LHS of the term */ | - | ||||||||||||||||||||||||
233 | i16 iColumn; /* The column on the LHS of the term. -1 for IPK */ | - | ||||||||||||||||||||||||
234 | Expr *pX; /* An expression being tested */ | - | ||||||||||||||||||||||||
235 | WhereClause *pWC; /* Shorthand for pScan->pWC */ | - | ||||||||||||||||||||||||
236 | WhereTerm *pTerm; /* The term being tested */ | - | ||||||||||||||||||||||||
237 | int k = pScan->k; /* Where to start scanning */ | - | ||||||||||||||||||||||||
238 | - | |||||||||||||||||||||||||
239 | assert( pScan->iEquiv<=pScan->nEquiv ); | - | ||||||||||||||||||||||||
240 | pWC = pScan->pWC; | - | ||||||||||||||||||||||||
241 | while(1){ | - | ||||||||||||||||||||||||
242 | iColumn = pScan->aiColumn[pScan->iEquiv-1]; | - | ||||||||||||||||||||||||
243 | iCur = pScan->aiCur[pScan->iEquiv-1]; | - | ||||||||||||||||||||||||
244 | assert( pWC!=0 ); | - | ||||||||||||||||||||||||
245 | do{ | - | ||||||||||||||||||||||||
246 | for(pTerm=pWC->a+k; k<pWC->nTerm; k++, pTerm++){
| 1017582-1340311 | ||||||||||||||||||||||||
247 | if( pTerm->leftCursor==iCur
| 583793-756518 | ||||||||||||||||||||||||
248 | && pTerm->u.leftColumn==iColumn
| 160745-595773 | ||||||||||||||||||||||||
249 | && (iColumn!=XN_EXPR
| 80-160665 | ||||||||||||||||||||||||
250 | || sqlite3ExprCompareSkip(pTerm->pExpr->pLeft,
| 22-58 | ||||||||||||||||||||||||
251 | pScan->pIdxExpr,iCur)==0)
| 22-58 | ||||||||||||||||||||||||
252 | && (pScan->iEquiv<=1 || !ExprHasProperty(pTerm->pExpr, EP_FromJoin))
| 12-153550 | ||||||||||||||||||||||||
253 | ){ | - | ||||||||||||||||||||||||
254 | if( (pTerm->eOperator & WO_EQUIV)!=0
| 14154-146557 | ||||||||||||||||||||||||
255 | && pScan->nEquiv<ArraySize(pScan->aiCur)
| 0-14154 | ||||||||||||||||||||||||
256 | && (pX = sqlite3ExprSkipCollate(pTerm->pExpr->pRight))->op==TK_COLUMN
| 4-14150 | ||||||||||||||||||||||||
257 | ){ | - | ||||||||||||||||||||||||
258 | int j; | - | ||||||||||||||||||||||||
259 | for(j=0; j<pScan->nEquiv; j++){
| 7114-14729 | ||||||||||||||||||||||||
260 | if( pScan->aiCur[j]==pX->iTable
| 7108-7621 | ||||||||||||||||||||||||
261 | && pScan->aiColumn[j]==pX->iColumn ){
| 72-7036 | ||||||||||||||||||||||||
262 | break; executed 7036 times by 1 test: break; Executed by:
| 7036 | ||||||||||||||||||||||||
263 | } | - | ||||||||||||||||||||||||
264 | } executed 7693 times by 1 test: end of block Executed by:
| 7693 | ||||||||||||||||||||||||
265 | if( j==pScan->nEquiv ){
| 7036-7114 | ||||||||||||||||||||||||
266 | pScan->aiCur[j] = pX->iTable; | - | ||||||||||||||||||||||||
267 | pScan->aiColumn[j] = pX->iColumn; | - | ||||||||||||||||||||||||
268 | pScan->nEquiv++; | - | ||||||||||||||||||||||||
269 | } executed 7114 times by 1 test: end of block Executed by:
| 7114 | ||||||||||||||||||||||||
270 | } executed 14150 times by 1 test: end of block Executed by:
| 14150 | ||||||||||||||||||||||||
271 | if( (pTerm->eOperator & pScan->opMask)!=0 ){
| 29410-131301 | ||||||||||||||||||||||||
272 | /* Verify the affinity and collating sequence match */ | - | ||||||||||||||||||||||||
273 | if( pScan->zCollName && (pTerm->eOperator & WO_ISNULL)==0 ){
| 283-69769 | ||||||||||||||||||||||||
274 | CollSeq *pColl; | - | ||||||||||||||||||||||||
275 | Parse *pParse = pWC->pWInfo->pParse; | - | ||||||||||||||||||||||||
276 | pX = pTerm->pExpr; | - | ||||||||||||||||||||||||
277 | if( !sqlite3IndexAffinityOk(pX, pScan->idxaff) ){
| 937-68549 | ||||||||||||||||||||||||
278 | continue; executed 937 times by 1 test: continue; Executed by:
| 937 | ||||||||||||||||||||||||
279 | } | - | ||||||||||||||||||||||||
280 | assert(pX->pLeft); | - | ||||||||||||||||||||||||
281 | pColl = sqlite3BinaryCompareCollSeq(pParse, | - | ||||||||||||||||||||||||
282 | pX->pLeft, pX->pRight); | - | ||||||||||||||||||||||||
283 | if( pColl==0 ) pColl = pParse->db->pDfltColl; executed 2149 times by 1 test: pColl = pParse->db->pDfltColl; Executed by:
| 2149-66400 | ||||||||||||||||||||||||
284 | if( sqlite3StrICmp(pColl->zName, pScan->zCollName) ){
| 103-68446 | ||||||||||||||||||||||||
285 | continue; executed 103 times by 1 test: continue; Executed by:
| 103 | ||||||||||||||||||||||||
286 | } | - | ||||||||||||||||||||||||
287 | } executed 68446 times by 1 test: end of block Executed by:
| 68446 | ||||||||||||||||||||||||
288 | if( (pTerm->eOperator & (WO_EQ|WO_IS))!=0
| 50030-80231 | ||||||||||||||||||||||||
289 | && (pX = pTerm->pExpr->pRight)->op==TK_COLUMN
| 14737-65494 | ||||||||||||||||||||||||
290 | && pX->iTable==pScan->aiCur[0]
| 6917-7820 | ||||||||||||||||||||||||
291 | && pX->iColumn==pScan->aiColumn[0]
| 5-6912 | ||||||||||||||||||||||||
292 | ){ | - | ||||||||||||||||||||||||
293 | testcase( pTerm->eOperator & WO_IS ); | - | ||||||||||||||||||||||||
294 | continue; executed 6912 times by 1 test: continue; Executed by:
| 6912 | ||||||||||||||||||||||||
295 | } | - | ||||||||||||||||||||||||
296 | pScan->pWC = pWC; | - | ||||||||||||||||||||||||
297 | pScan->k = k+1; | - | ||||||||||||||||||||||||
298 | return pTerm; executed 123349 times by 30 tests: return pTerm; Executed by:
| 123349 | ||||||||||||||||||||||||
299 | } | - | ||||||||||||||||||||||||
300 | } executed 29410 times by 1 test: end of block Executed by:
| 29410 | ||||||||||||||||||||||||
301 | } executed 1209010 times by 366 tests: end of block Executed by:
| 1209010 | ||||||||||||||||||||||||
302 | pWC = pWC->pOuter; | - | ||||||||||||||||||||||||
303 | k = 0; | - | ||||||||||||||||||||||||
304 | }while( pWC!=0 ); executed 1017582 times by 435 tests: end of block Executed by:
| 124663-1017582 | ||||||||||||||||||||||||
305 | if( pScan->iEquiv>=pScan->nEquiv ) break; executed 885839 times by 435 tests: break; Executed by:
| 7080-885839 | ||||||||||||||||||||||||
306 | pWC = pScan->pOrigWC; | - | ||||||||||||||||||||||||
307 | k = 0; | - | ||||||||||||||||||||||||
308 | pScan->iEquiv++; | - | ||||||||||||||||||||||||
309 | } executed 7080 times by 1 test: end of block Executed by:
| 7080 | ||||||||||||||||||||||||
310 | return 0; executed 885839 times by 435 tests: return 0; Executed by:
| 885839 | ||||||||||||||||||||||||
311 | } | - | ||||||||||||||||||||||||
312 | - | |||||||||||||||||||||||||
313 | /* | - | ||||||||||||||||||||||||
314 | ** Initialize a WHERE clause scanner object. Return a pointer to the | - | ||||||||||||||||||||||||
315 | ** first match. Return NULL if there are no matches. | - | ||||||||||||||||||||||||
316 | ** | - | ||||||||||||||||||||||||
317 | ** The scanner will be searching the WHERE clause pWC. It will look | - | ||||||||||||||||||||||||
318 | ** for terms of the form "X <op> <expr>" where X is column iColumn of table | - | ||||||||||||||||||||||||
319 | ** iCur. Or if pIdx!=0 then X is column iColumn of index pIdx. pIdx | - | ||||||||||||||||||||||||
320 | ** must be one of the indexes of table iCur. | - | ||||||||||||||||||||||||
321 | ** | - | ||||||||||||||||||||||||
322 | ** The <op> must be one of the operators described by opMask. | - | ||||||||||||||||||||||||
323 | ** | - | ||||||||||||||||||||||||
324 | ** If the search is for X and the WHERE clause contains terms of the | - | ||||||||||||||||||||||||
325 | ** form X=Y then this routine might also return terms of the form | - | ||||||||||||||||||||||||
326 | ** "Y <op> <expr>". The number of levels of transitivity is limited, | - | ||||||||||||||||||||||||
327 | ** but is enough to handle most commonly occurring SQL statements. | - | ||||||||||||||||||||||||
328 | ** | - | ||||||||||||||||||||||||
329 | ** If X is not the INTEGER PRIMARY KEY then X must be compatible with | - | ||||||||||||||||||||||||
330 | ** index pIdx. | - | ||||||||||||||||||||||||
331 | */ | - | ||||||||||||||||||||||||
332 | static WhereTerm *whereScanInit( | - | ||||||||||||||||||||||||
333 | WhereScan *pScan, /* The WhereScan object being initialized */ | - | ||||||||||||||||||||||||
334 | WhereClause *pWC, /* The WHERE clause to be scanned */ | - | ||||||||||||||||||||||||
335 | int iCur, /* Cursor to scan for */ | - | ||||||||||||||||||||||||
336 | int iColumn, /* Column to scan for */ | - | ||||||||||||||||||||||||
337 | u32 opMask, /* Operator(s) to scan for */ | - | ||||||||||||||||||||||||
338 | Index *pIdx /* Must be compatible with this index */ | - | ||||||||||||||||||||||||
339 | ){ | - | ||||||||||||||||||||||||
340 | pScan->pOrigWC = pWC; | - | ||||||||||||||||||||||||
341 | pScan->pWC = pWC; | - | ||||||||||||||||||||||||
342 | pScan->pIdxExpr = 0; | - | ||||||||||||||||||||||||
343 | pScan->idxaff = 0; | - | ||||||||||||||||||||||||
344 | pScan->zCollName = 0; | - | ||||||||||||||||||||||||
345 | if( pIdx ){
| 374621-540418 | ||||||||||||||||||||||||
346 | int j = iColumn; | - | ||||||||||||||||||||||||
347 | iColumn = pIdx->aiColumn[j]; | - | ||||||||||||||||||||||||
348 | if( iColumn==XN_EXPR ){
| 151-540267 | ||||||||||||||||||||||||
349 | pScan->pIdxExpr = pIdx->aColExpr->a[j].pExpr; | - | ||||||||||||||||||||||||
350 | pScan->zCollName = pIdx->azColl[j]; | - | ||||||||||||||||||||||||
351 | }else if( iColumn==pIdx->pTable->iPKey ){ executed 151 times by 1 test: end of block Executed by:
| 151-328177 | ||||||||||||||||||||||||
352 | iColumn = XN_ROWID; | - | ||||||||||||||||||||||||
353 | }else if( iColumn>=0 ){ executed 212090 times by 435 tests: end of block Executed by:
| 76083-252094 | ||||||||||||||||||||||||
354 | pScan->idxaff = pIdx->pTable->aCol[iColumn].affinity; | - | ||||||||||||||||||||||||
355 | pScan->zCollName = pIdx->azColl[j]; | - | ||||||||||||||||||||||||
356 | } executed 252094 times by 356 tests: end of block Executed by:
| 252094 | ||||||||||||||||||||||||
357 | }else if( iColumn==XN_EXPR ){ executed 540418 times by 435 tests: end of block Executed by:
| 1-540418 | ||||||||||||||||||||||||
358 | return 0; executed 1 time by 1 test: return 0; Executed by:
| 1 | ||||||||||||||||||||||||
359 | } | - | ||||||||||||||||||||||||
360 | pScan->opMask = opMask; | - | ||||||||||||||||||||||||
361 | pScan->k = 0; | - | ||||||||||||||||||||||||
362 | pScan->aiCur[0] = iCur; | - | ||||||||||||||||||||||||
363 | pScan->aiColumn[0] = iColumn; | - | ||||||||||||||||||||||||
364 | pScan->nEquiv = 1; | - | ||||||||||||||||||||||||
365 | pScan->iEquiv = 1; | - | ||||||||||||||||||||||||
366 | return whereScanNext(pScan); executed 915038 times by 435 tests: return whereScanNext(pScan); Executed by:
| 915038 | ||||||||||||||||||||||||
367 | } | - | ||||||||||||||||||||||||
368 | - | |||||||||||||||||||||||||
369 | /* | - | ||||||||||||||||||||||||
370 | ** Search for a term in the WHERE clause that is of the form "X <op> <expr>" | - | ||||||||||||||||||||||||
371 | ** where X is a reference to the iColumn of table iCur or of index pIdx | - | ||||||||||||||||||||||||
372 | ** if pIdx!=0 and <op> is one of the WO_xx operator codes specified by | - | ||||||||||||||||||||||||
373 | ** the op parameter. Return a pointer to the term. Return 0 if not found. | - | ||||||||||||||||||||||||
374 | ** | - | ||||||||||||||||||||||||
375 | ** If pIdx!=0 then it must be one of the indexes of table iCur. | - | ||||||||||||||||||||||||
376 | ** Search for terms matching the iColumn-th column of pIdx | - | ||||||||||||||||||||||||
377 | ** rather than the iColumn-th column of table iCur. | - | ||||||||||||||||||||||||
378 | ** | - | ||||||||||||||||||||||||
379 | ** The term returned might by Y=<expr> if there is another constraint in | - | ||||||||||||||||||||||||
380 | ** the WHERE clause that specifies that X=Y. Any such constraints will be | - | ||||||||||||||||||||||||
381 | ** identified by the WO_EQUIV bit in the pTerm->eOperator field. The | - | ||||||||||||||||||||||||
382 | ** aiCur[]/iaColumn[] arrays hold X and all its equivalents. There are 11 | - | ||||||||||||||||||||||||
383 | ** slots in aiCur[]/aiColumn[] so that means we can look for X plus up to 10 | - | ||||||||||||||||||||||||
384 | ** other equivalent values. Hence a search for X will return <expr> if X=A1 | - | ||||||||||||||||||||||||
385 | ** and A1=A2 and A2=A3 and ... and A9=A10 and A10=<expr>. | - | ||||||||||||||||||||||||
386 | ** | - | ||||||||||||||||||||||||
387 | ** If there are multiple terms in the WHERE clause of the form "X <op> <expr>" | - | ||||||||||||||||||||||||
388 | ** then try for the one with no dependencies on <expr> - in other words where | - | ||||||||||||||||||||||||
389 | ** <expr> is a constant expression of some kind. Only return entries of | - | ||||||||||||||||||||||||
390 | ** the form "X <op> Y" where Y is a column in another table if no terms of | - | ||||||||||||||||||||||||
391 | ** the form "X <op> <const-expr>" exist. If no terms with a constant RHS | - | ||||||||||||||||||||||||
392 | ** exist, try to return a term that does not use WO_EQUIV. | - | ||||||||||||||||||||||||
393 | */ | - | ||||||||||||||||||||||||
394 | WhereTerm *sqlite3WhereFindTerm( | - | ||||||||||||||||||||||||
395 | WhereClause *pWC, /* The WHERE clause to be searched */ | - | ||||||||||||||||||||||||
396 | int iCur, /* Cursor number of LHS */ | - | ||||||||||||||||||||||||
397 | int iColumn, /* Column number of LHS */ | - | ||||||||||||||||||||||||
398 | Bitmask notReady, /* RHS must not overlap with this mask */ | - | ||||||||||||||||||||||||
399 | u32 op, /* Mask of WO_xx values describing operator */ | - | ||||||||||||||||||||||||
400 | Index *pIdx /* Must be compatible with this index, if not NULL */ | - | ||||||||||||||||||||||||
401 | ){ | - | ||||||||||||||||||||||||
402 | WhereTerm *pResult = 0; | - | ||||||||||||||||||||||||
403 | WhereTerm *p; | - | ||||||||||||||||||||||||
404 | WhereScan scan; | - | ||||||||||||||||||||||||
405 | - | |||||||||||||||||||||||||
406 | p = whereScanInit(&scan, pWC, iCur, iColumn, op, pIdx); | - | ||||||||||||||||||||||||
407 | op &= WO_EQ|WO_IS; | - | ||||||||||||||||||||||||
408 | while( p ){
| 33541-350807 | ||||||||||||||||||||||||
409 | if( (p->prereqRight & notReady)==0 ){
| 3317-30224 | ||||||||||||||||||||||||
410 | if( p->prereqRight==0 && (p->eOperator&op)!=0 ){
| 336-29888 | ||||||||||||||||||||||||
411 | testcase( p->eOperator & WO_IS ); | - | ||||||||||||||||||||||||
412 | return p; executed 29197 times by 30 tests: return p; Executed by:
| 29197 | ||||||||||||||||||||||||
413 | } | - | ||||||||||||||||||||||||
414 | if( pResult==0 ) pResult = p; executed 1025 times by 1 test: pResult = p; Executed by:
| 2-1025 | ||||||||||||||||||||||||
415 | } executed 1027 times by 1 test: end of block Executed by:
| 1027 | ||||||||||||||||||||||||
416 | p = whereScanNext(&scan); | - | ||||||||||||||||||||||||
417 | } executed 4344 times by 1 test: end of block Executed by:
| 4344 | ||||||||||||||||||||||||
418 | return pResult; executed 350807 times by 435 tests: return pResult; Executed by:
| 350807 | ||||||||||||||||||||||||
419 | } | - | ||||||||||||||||||||||||
420 | - | |||||||||||||||||||||||||
421 | /* | - | ||||||||||||||||||||||||
422 | ** This function searches pList for an entry that matches the iCol-th column | - | ||||||||||||||||||||||||
423 | ** of index pIdx. | - | ||||||||||||||||||||||||
424 | ** | - | ||||||||||||||||||||||||
425 | ** If such an expression is found, its index in pList->a[] is returned. If | - | ||||||||||||||||||||||||
426 | ** no expression is found, -1 is returned. | - | ||||||||||||||||||||||||
427 | */ | - | ||||||||||||||||||||||||
428 | static int findIndexCol( | - | ||||||||||||||||||||||||
429 | Parse *pParse, /* Parse context */ | - | ||||||||||||||||||||||||
430 | ExprList *pList, /* Expression list to search */ | - | ||||||||||||||||||||||||
431 | int iBase, /* Cursor for table associated with pIdx */ | - | ||||||||||||||||||||||||
432 | Index *pIdx, /* Index to match column of */ | - | ||||||||||||||||||||||||
433 | int iCol /* Column of index to match */ | - | ||||||||||||||||||||||||
434 | ){ | - | ||||||||||||||||||||||||
435 | int i; | - | ||||||||||||||||||||||||
436 | const char *zColl = pIdx->azColl[iCol]; | - | ||||||||||||||||||||||||
437 | - | |||||||||||||||||||||||||
438 | for(i=0; i<pList->nExpr; i++){
| 35-111 | ||||||||||||||||||||||||
439 | Expr *p = sqlite3ExprSkipCollate(pList->a[i].pExpr); | - | ||||||||||||||||||||||||
440 | if( p->op==TK_COLUMN
| 7-104 | ||||||||||||||||||||||||
441 | && p->iColumn==pIdx->aiColumn[iCol]
| 37-67 | ||||||||||||||||||||||||
442 | && p->iTable==iBase
| 0-37 | ||||||||||||||||||||||||
443 | ){ | - | ||||||||||||||||||||||||
444 | CollSeq *pColl = sqlite3ExprNNCollSeq(pParse, pList->a[i].pExpr); | - | ||||||||||||||||||||||||
445 | if( 0==sqlite3StrICmp(pColl->zName, zColl) ){
| 6-31 | ||||||||||||||||||||||||
446 | return i; executed 31 times by 1 test: return i; Executed by:
| 31 | ||||||||||||||||||||||||
447 | } | - | ||||||||||||||||||||||||
448 | } executed 6 times by 1 test: end of block Executed by:
| 6 | ||||||||||||||||||||||||
449 | } executed 80 times by 1 test: end of block Executed by:
| 80 | ||||||||||||||||||||||||
450 | - | |||||||||||||||||||||||||
451 | return -1; executed 35 times by 1 test: return -1; Executed by:
| 35 | ||||||||||||||||||||||||
452 | } | - | ||||||||||||||||||||||||
453 | - | |||||||||||||||||||||||||
454 | /* | - | ||||||||||||||||||||||||
455 | ** Return TRUE if the iCol-th column of index pIdx is NOT NULL | - | ||||||||||||||||||||||||
456 | */ | - | ||||||||||||||||||||||||
457 | static int indexColumnNotNull(Index *pIdx, int iCol){ | - | ||||||||||||||||||||||||
458 | int j; | - | ||||||||||||||||||||||||
459 | assert( pIdx!=0 ); | - | ||||||||||||||||||||||||
460 | assert( iCol>=0 && iCol<pIdx->nColumn ); | - | ||||||||||||||||||||||||
461 | j = pIdx->aiColumn[iCol]; | - | ||||||||||||||||||||||||
462 | if( j>=0 ){
| 24-312 | ||||||||||||||||||||||||
463 | return pIdx->pTable->aCol[j].notNull; executed 312 times by 1 test: return pIdx->pTable->aCol[j].notNull; Executed by:
| 312 | ||||||||||||||||||||||||
464 | }else if( j==(-1) ){
| 2-22 | ||||||||||||||||||||||||
465 | return 1; executed 22 times by 1 test: return 1; Executed by:
| 22 | ||||||||||||||||||||||||
466 | }else{ | - | ||||||||||||||||||||||||
467 | assert( j==(-2) ); | - | ||||||||||||||||||||||||
468 | return 0; /* Assume an indexed expression can always yield a NULL */ executed 2 times by 1 test: return 0; Executed by:
| 2 | ||||||||||||||||||||||||
469 | - | |||||||||||||||||||||||||
470 | } | - | ||||||||||||||||||||||||
471 | } | - | ||||||||||||||||||||||||
472 | - | |||||||||||||||||||||||||
473 | /* | - | ||||||||||||||||||||||||
474 | ** Return true if the DISTINCT expression-list passed as the third argument | - | ||||||||||||||||||||||||
475 | ** is redundant. | - | ||||||||||||||||||||||||
476 | ** | - | ||||||||||||||||||||||||
477 | ** A DISTINCT list is redundant if any subset of the columns in the | - | ||||||||||||||||||||||||
478 | ** DISTINCT list are collectively unique and individually non-null. | - | ||||||||||||||||||||||||
479 | */ | - | ||||||||||||||||||||||||
480 | static int isDistinctRedundant( | - | ||||||||||||||||||||||||
481 | Parse *pParse, /* Parsing context */ | - | ||||||||||||||||||||||||
482 | SrcList *pTabList, /* The FROM clause */ | - | ||||||||||||||||||||||||
483 | WhereClause *pWC, /* The WHERE clause */ | - | ||||||||||||||||||||||||
484 | ExprList *pDistinct /* The result set that needs to be DISTINCT */ | - | ||||||||||||||||||||||||
485 | ){ | - | ||||||||||||||||||||||||
486 | Table *pTab; | - | ||||||||||||||||||||||||
487 | Index *pIdx; | - | ||||||||||||||||||||||||
488 | int i; | - | ||||||||||||||||||||||||
489 | int iBase; | - | ||||||||||||||||||||||||
490 | - | |||||||||||||||||||||||||
491 | /* If there is more than one table or sub-select in the FROM clause of | - | ||||||||||||||||||||||||
492 | ** this query, then it will not be possible to show that the DISTINCT | - | ||||||||||||||||||||||||
493 | ** clause is redundant. */ | - | ||||||||||||||||||||||||
494 | if( pTabList->nSrc!=1 ) return 0; executed 63 times by 1 test: return 0; Executed by:
| 63-196 | ||||||||||||||||||||||||
495 | iBase = pTabList->a[0].iCursor; | - | ||||||||||||||||||||||||
496 | pTab = pTabList->a[0].pTab; | - | ||||||||||||||||||||||||
497 | - | |||||||||||||||||||||||||
498 | /* If any of the expressions is an IPK column on table iBase, then return | - | ||||||||||||||||||||||||
499 | ** true. Note: The (p->iTable==iBase) part of this test may be false if the | - | ||||||||||||||||||||||||
500 | ** current SELECT is a correlated sub-query. | - | ||||||||||||||||||||||||
501 | */ | - | ||||||||||||||||||||||||
502 | for(i=0; i<pDistinct->nExpr; i++){
| 189-318 | ||||||||||||||||||||||||
503 | Expr *p = sqlite3ExprSkipCollate(pDistinct->a[i].pExpr); | - | ||||||||||||||||||||||||
504 | if( p->op==TK_COLUMN && p->iTable==iBase && p->iColumn<0 ) return 1; executed 7 times by 1 test: return 1; Executed by:
| 3-274 | ||||||||||||||||||||||||
505 | } executed 311 times by 1 test: end of block Executed by:
| 311 | ||||||||||||||||||||||||
506 | - | |||||||||||||||||||||||||
507 | /* Loop through all indices on the table, checking each to see if it makes | - | ||||||||||||||||||||||||
508 | ** the DISTINCT qualifier redundant. It does so if: | - | ||||||||||||||||||||||||
509 | ** | - | ||||||||||||||||||||||||
510 | ** 1. The index is itself UNIQUE, and | - | ||||||||||||||||||||||||
511 | ** | - | ||||||||||||||||||||||||
512 | ** 2. All of the columns in the index are either part of the pDistinct | - | ||||||||||||||||||||||||
513 | ** list, or else the WHERE clause contains a term of the form "col=X", | - | ||||||||||||||||||||||||
514 | ** where X is a constant value. The collation sequences of the | - | ||||||||||||||||||||||||
515 | ** comparison and select-list expressions must match those of the index. | - | ||||||||||||||||||||||||
516 | ** | - | ||||||||||||||||||||||||
517 | ** 3. All of those index columns for which the WHERE clause does not | - | ||||||||||||||||||||||||
518 | ** contain a "col=X" term are subject to a NOT NULL constraint. | - | ||||||||||||||||||||||||
519 | */ | - | ||||||||||||||||||||||||
520 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
| 136-177 | ||||||||||||||||||||||||
521 | if( !IsUniqueIndex(pIdx) ) continue; executed 73 times by 1 test: continue; Executed by:
| 63-73 | ||||||||||||||||||||||||
522 | for(i=0; i<pIdx->nKeyCol; i++){
| 12-70 | ||||||||||||||||||||||||
523 | if( 0==sqlite3WhereFindTerm(pWC, iBase, i, ~(Bitmask)0, WO_EQ, pIdx) ){
| 4-66 | ||||||||||||||||||||||||
524 | if( findIndexCol(pParse, pDistinct, iBase, pIdx, i)<0 ) break; executed 35 times by 1 test: break; Executed by:
| 31-35 | ||||||||||||||||||||||||
525 | if( indexColumnNotNull(pIdx, i)==0 ) break; executed 16 times by 1 test: break; Executed by:
| 15-16 | ||||||||||||||||||||||||
526 | } executed 15 times by 1 test: end of block Executed by:
| 15 | ||||||||||||||||||||||||
527 | } executed 19 times by 1 test: end of block Executed by:
| 19 | ||||||||||||||||||||||||
528 | if( i==pIdx->nKeyCol ){
| 12-51 | ||||||||||||||||||||||||
529 | /* This index implies that the DISTINCT qualifier is redundant. */ | - | ||||||||||||||||||||||||
530 | return 1; executed 12 times by 1 test: return 1; Executed by:
| 12 | ||||||||||||||||||||||||
531 | } | - | ||||||||||||||||||||||||
532 | } executed 51 times by 1 test: end of block Executed by:
| 51 | ||||||||||||||||||||||||
533 | - | |||||||||||||||||||||||||
534 | return 0; executed 177 times by 1 test: return 0; Executed by:
| 177 | ||||||||||||||||||||||||
535 | } | - | ||||||||||||||||||||||||
536 | - | |||||||||||||||||||||||||
537 | - | |||||||||||||||||||||||||
538 | /* | - | ||||||||||||||||||||||||
539 | ** Estimate the logarithm of the input value to base 2. | - | ||||||||||||||||||||||||
540 | */ | - | ||||||||||||||||||||||||
541 | static LogEst estLog(LogEst N){ | - | ||||||||||||||||||||||||
542 | return N<=10 ? 0 : sqlite3LogEst(N) - 33; executed 821273 times by 435 tests: return N<=10 ? 0 : sqlite3LogEst(N) - 33; Executed by:
| 942-821273 | ||||||||||||||||||||||||
543 | } | - | ||||||||||||||||||||||||
544 | - | |||||||||||||||||||||||||
545 | /* | - | ||||||||||||||||||||||||
546 | ** Convert OP_Column opcodes to OP_Copy in previously generated code. | - | ||||||||||||||||||||||||
547 | ** | - | ||||||||||||||||||||||||
548 | ** This routine runs over generated VDBE code and translates OP_Column | - | ||||||||||||||||||||||||
549 | ** opcodes into OP_Copy when the table is being accessed via co-routine | - | ||||||||||||||||||||||||
550 | ** instead of via table lookup. | - | ||||||||||||||||||||||||
551 | ** | - | ||||||||||||||||||||||||
552 | ** If the bIncrRowid parameter is 0, then any OP_Rowid instructions on | - | ||||||||||||||||||||||||
553 | ** cursor iTabCur are transformed into OP_Null. Or, if bIncrRowid is non-zero, | - | ||||||||||||||||||||||||
554 | ** then each OP_Rowid is transformed into an instruction to increment the | - | ||||||||||||||||||||||||
555 | ** value stored in its output register. | - | ||||||||||||||||||||||||
556 | */ | - | ||||||||||||||||||||||||
557 | static void translateColumnToCopy( | - | ||||||||||||||||||||||||
558 | Parse *pParse, /* Parsing context */ | - | ||||||||||||||||||||||||
559 | int iStart, /* Translate from this opcode to the end */ | - | ||||||||||||||||||||||||
560 | int iTabCur, /* OP_Column/OP_Rowid references to this table */ | - | ||||||||||||||||||||||||
561 | int iRegister, /* The first column is in this register */ | - | ||||||||||||||||||||||||
562 | int bIncrRowid /* If non-zero, transform OP_rowid to OP_AddImm(1) */ | - | ||||||||||||||||||||||||
563 | ){ | - | ||||||||||||||||||||||||
564 | Vdbe *v = pParse->pVdbe; | - | ||||||||||||||||||||||||
565 | VdbeOp *pOp = sqlite3VdbeGetOp(v, iStart); | - | ||||||||||||||||||||||||
566 | int iEnd = sqlite3VdbeCurrentAddr(v); | - | ||||||||||||||||||||||||
567 | if( pParse->db->mallocFailed ) return; never executed: return;
| 0-35367 | ||||||||||||||||||||||||
568 | for(; iStart<iEnd; iStart++, pOp++){
| 35367-229659 | ||||||||||||||||||||||||
569 | if( pOp->p1!=iTabCur ) continue; executed 188002 times by 1 test: continue; Executed by:
| 41657-188002 | ||||||||||||||||||||||||
570 | if( pOp->opcode==OP_Column ){
| 1147-40510 | ||||||||||||||||||||||||
571 | pOp->opcode = OP_Copy; | - | ||||||||||||||||||||||||
572 | pOp->p1 = pOp->p2 + iRegister; | - | ||||||||||||||||||||||||
573 | pOp->p2 = pOp->p3; | - | ||||||||||||||||||||||||
574 | pOp->p3 = 0; | - | ||||||||||||||||||||||||
575 | }else if( pOp->opcode==OP_Rowid ){ executed 40510 times by 1 test: end of block Executed by:
| 9-40510 | ||||||||||||||||||||||||
576 | if( bIncrRowid ){
| 3-6 | ||||||||||||||||||||||||
577 | /* Increment the value stored in the P2 operand of the OP_Rowid. */ | - | ||||||||||||||||||||||||
578 | pOp->opcode = OP_AddImm; | - | ||||||||||||||||||||||||
579 | pOp->p1 = pOp->p2; | - | ||||||||||||||||||||||||
580 | pOp->p2 = 1; | - | ||||||||||||||||||||||||
581 | }else{ executed 3 times by 1 test: end of block Executed by:
| 3 | ||||||||||||||||||||||||
582 | pOp->opcode = OP_Null; | - | ||||||||||||||||||||||||
583 | pOp->p1 = 0; | - | ||||||||||||||||||||||||
584 | pOp->p3 = 0; | - | ||||||||||||||||||||||||
585 | } executed 6 times by 1 test: end of block Executed by:
| 6 | ||||||||||||||||||||||||
586 | } | - | ||||||||||||||||||||||||
587 | } executed 41657 times by 1 test: end of block Executed by:
| 41657 | ||||||||||||||||||||||||
588 | } executed 35367 times by 1 test: end of block Executed by:
| 35367 | ||||||||||||||||||||||||
589 | - | |||||||||||||||||||||||||
590 | /* | - | ||||||||||||||||||||||||
591 | ** Two routines for printing the content of an sqlite3_index_info | - | ||||||||||||||||||||||||
592 | ** structure. Used for testing and debugging only. If neither | - | ||||||||||||||||||||||||
593 | ** SQLITE_TEST or SQLITE_DEBUG are defined, then these routines | - | ||||||||||||||||||||||||
594 | ** are no-ops. | - | ||||||||||||||||||||||||
595 | */ | - | ||||||||||||||||||||||||
596 | #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(WHERETRACE_ENABLED) | - | ||||||||||||||||||||||||
597 | static void TRACE_IDX_INPUTS(sqlite3_index_info *p){ | - | ||||||||||||||||||||||||
598 | int i; | - | ||||||||||||||||||||||||
599 | if( !sqlite3WhereTrace ) return; | - | ||||||||||||||||||||||||
600 | for(i=0; i<p->nConstraint; i++){ | - | ||||||||||||||||||||||||
601 | sqlite3DebugPrintf(" constraint[%d]: col=%d termid=%d op=%d usabled=%d\n", | - | ||||||||||||||||||||||||
602 | i, | - | ||||||||||||||||||||||||
603 | p->aConstraint[i].iColumn, | - | ||||||||||||||||||||||||
604 | p->aConstraint[i].iTermOffset, | - | ||||||||||||||||||||||||
605 | p->aConstraint[i].op, | - | ||||||||||||||||||||||||
606 | p->aConstraint[i].usable); | - | ||||||||||||||||||||||||
607 | } | - | ||||||||||||||||||||||||
608 | for(i=0; i<p->nOrderBy; i++){ | - | ||||||||||||||||||||||||
609 | sqlite3DebugPrintf(" orderby[%d]: col=%d desc=%d\n", | - | ||||||||||||||||||||||||
610 | i, | - | ||||||||||||||||||||||||
611 | p->aOrderBy[i].iColumn, | - | ||||||||||||||||||||||||
612 | p->aOrderBy[i].desc); | - | ||||||||||||||||||||||||
613 | } | - | ||||||||||||||||||||||||
614 | } | - | ||||||||||||||||||||||||
615 | static void TRACE_IDX_OUTPUTS(sqlite3_index_info *p){ | - | ||||||||||||||||||||||||
616 | int i; | - | ||||||||||||||||||||||||
617 | if( !sqlite3WhereTrace ) return; | - | ||||||||||||||||||||||||
618 | for(i=0; i<p->nConstraint; i++){ | - | ||||||||||||||||||||||||
619 | sqlite3DebugPrintf(" usage[%d]: argvIdx=%d omit=%d\n", | - | ||||||||||||||||||||||||
620 | i, | - | ||||||||||||||||||||||||
621 | p->aConstraintUsage[i].argvIndex, | - | ||||||||||||||||||||||||
622 | p->aConstraintUsage[i].omit); | - | ||||||||||||||||||||||||
623 | } | - | ||||||||||||||||||||||||
624 | sqlite3DebugPrintf(" idxNum=%d\n", p->idxNum); | - | ||||||||||||||||||||||||
625 | sqlite3DebugPrintf(" idxStr=%s\n", p->idxStr); | - | ||||||||||||||||||||||||
626 | sqlite3DebugPrintf(" orderByConsumed=%d\n", p->orderByConsumed); | - | ||||||||||||||||||||||||
627 | sqlite3DebugPrintf(" estimatedCost=%g\n", p->estimatedCost); | - | ||||||||||||||||||||||||
628 | sqlite3DebugPrintf(" estimatedRows=%lld\n", p->estimatedRows); | - | ||||||||||||||||||||||||
629 | } | - | ||||||||||||||||||||||||
630 | #else | - | ||||||||||||||||||||||||
631 | #define TRACE_IDX_INPUTS(A) | - | ||||||||||||||||||||||||
632 | #define TRACE_IDX_OUTPUTS(A) | - | ||||||||||||||||||||||||
633 | #endif | - | ||||||||||||||||||||||||
634 | - | |||||||||||||||||||||||||
635 | #ifndef SQLITE_OMIT_AUTOMATIC_INDEX | - | ||||||||||||||||||||||||
636 | /* | - | ||||||||||||||||||||||||
637 | ** Return TRUE if the WHERE clause term pTerm is of a form where it | - | ||||||||||||||||||||||||
638 | ** could be used with an index to access pSrc, assuming an appropriate | - | ||||||||||||||||||||||||
639 | ** index existed. | - | ||||||||||||||||||||||||
640 | */ | - | ||||||||||||||||||||||||
641 | static int termCanDriveIndex( | - | ||||||||||||||||||||||||
642 | WhereTerm *pTerm, /* WHERE clause term to check */ | - | ||||||||||||||||||||||||
643 | struct SrcList_item *pSrc, /* Table we are trying to access */ | - | ||||||||||||||||||||||||
644 | Bitmask notReady /* Tables in outer loops of the join */ | - | ||||||||||||||||||||||||
645 | ){ | - | ||||||||||||||||||||||||
646 | char aff; | - | ||||||||||||||||||||||||
647 | if( pTerm->leftCursor!=pSrc->iCursor ) return 0; executed 305767 times by 360 tests: return 0; Executed by:
| 86476-305767 | ||||||||||||||||||||||||
648 | if( (pTerm->eOperator & (WO_EQ|WO_IS))==0 ) return 0; executed 18559 times by 3 tests: return 0; Executed by:
| 18559-67917 | ||||||||||||||||||||||||
649 | if( (pSrc->fg.jointype & JT_LEFT)
| 853-67064 | ||||||||||||||||||||||||
650 | && !ExprHasProperty(pTerm->pExpr, EP_FromJoin)
| 3-850 | ||||||||||||||||||||||||
651 | && (pTerm->eOperator & WO_IS)
| 0-3 | ||||||||||||||||||||||||
652 | ){ | - | ||||||||||||||||||||||||
653 | /* Cannot use an IS term from the WHERE clause as an index driver for | - | ||||||||||||||||||||||||
654 | ** the RHS of a LEFT JOIN. Such a term can only be used if it is from | - | ||||||||||||||||||||||||
655 | ** the ON clause. */ | - | ||||||||||||||||||||||||
656 | return 0; executed 3 times by 1 test: return 0; Executed by:
| 3 | ||||||||||||||||||||||||
657 | } | - | ||||||||||||||||||||||||
658 | if( (pTerm->prereqRight & notReady)!=0 ) return 0; executed 170 times by 1 test: return 0; Executed by:
| 170-67744 | ||||||||||||||||||||||||
659 | if( pTerm->u.leftColumn<0 ) return 0; executed 1152 times by 1 test: return 0; Executed by:
| 1152-66592 | ||||||||||||||||||||||||
660 | aff = pSrc->pTab->aCol[pTerm->u.leftColumn].affinity; | - | ||||||||||||||||||||||||
661 | if( !sqlite3IndexAffinityOk(pTerm->pExpr, aff) ) return 0; executed 463 times by 1 test: return 0; Executed by:
| 463-66129 | ||||||||||||||||||||||||
662 | testcase( pTerm->pExpr->op==TK_IS ); | - | ||||||||||||||||||||||||
663 | return 1; executed 66129 times by 33 tests: return 1; Executed by:
| 66129 | ||||||||||||||||||||||||
664 | } | - | ||||||||||||||||||||||||
665 | #endif | - | ||||||||||||||||||||||||
666 | - | |||||||||||||||||||||||||
667 | - | |||||||||||||||||||||||||
668 | #ifndef SQLITE_OMIT_AUTOMATIC_INDEX | - | ||||||||||||||||||||||||
669 | /* | - | ||||||||||||||||||||||||
670 | ** Generate code to construct the Index object for an automatic index | - | ||||||||||||||||||||||||
671 | ** and to set up the WhereLevel object pLevel so that the code generator | - | ||||||||||||||||||||||||
672 | ** makes use of the automatic index. | - | ||||||||||||||||||||||||
673 | */ | - | ||||||||||||||||||||||||
674 | static void constructAutomaticIndex( | - | ||||||||||||||||||||||||
675 | Parse *pParse, /* The parsing context */ | - | ||||||||||||||||||||||||
676 | WhereClause *pWC, /* The WHERE clause */ | - | ||||||||||||||||||||||||
677 | struct SrcList_item *pSrc, /* The FROM clause term to get the next index */ | - | ||||||||||||||||||||||||
678 | Bitmask notReady, /* Mask of cursors that are not available */ | - | ||||||||||||||||||||||||
679 | WhereLevel *pLevel /* Write new index here */ | - | ||||||||||||||||||||||||
680 | ){ | - | ||||||||||||||||||||||||
681 | int nKeyCol; /* Number of columns in the constructed index */ | - | ||||||||||||||||||||||||
682 | WhereTerm *pTerm; /* A single term of the WHERE clause */ | - | ||||||||||||||||||||||||
683 | WhereTerm *pWCEnd; /* End of pWC->a[] */ | - | ||||||||||||||||||||||||
684 | Index *pIdx; /* Object describing the transient index */ | - | ||||||||||||||||||||||||
685 | Vdbe *v; /* Prepared statement under construction */ | - | ||||||||||||||||||||||||
686 | int addrInit; /* Address of the initialization bypass jump */ | - | ||||||||||||||||||||||||
687 | Table *pTable; /* The table being indexed */ | - | ||||||||||||||||||||||||
688 | int addrTop; /* Top of the index fill loop */ | - | ||||||||||||||||||||||||
689 | int regRecord; /* Register holding an index record */ | - | ||||||||||||||||||||||||
690 | int n; /* Column counter */ | - | ||||||||||||||||||||||||
691 | int i; /* Loop counter */ | - | ||||||||||||||||||||||||
692 | int mxBitCol; /* Maximum column in pSrc->colUsed */ | - | ||||||||||||||||||||||||
693 | CollSeq *pColl; /* Collating sequence to on a column */ | - | ||||||||||||||||||||||||
694 | WhereLoop *pLoop; /* The Loop object */ | - | ||||||||||||||||||||||||
695 | char *zNotUsed; /* Extra space on the end of pIdx */ | - | ||||||||||||||||||||||||
696 | Bitmask idxCols; /* Bitmap of columns used for indexing */ | - | ||||||||||||||||||||||||
697 | Bitmask extraCols; /* Bitmap of additional columns */ | - | ||||||||||||||||||||||||
698 | u8 sentWarning = 0; /* True if a warnning has been issued */ | - | ||||||||||||||||||||||||
699 | Expr *pPartial = 0; /* Partial Index Expression */ | - | ||||||||||||||||||||||||
700 | int iContinue = 0; /* Jump here to skip excluded rows */ | - | ||||||||||||||||||||||||
701 | struct SrcList_item *pTabItem; /* FROM clause term being indexed */ | - | ||||||||||||||||||||||||
702 | int addrCounter = 0; /* Address where integer counter is initialized */ | - | ||||||||||||||||||||||||
703 | int regBase; /* Array of registers where record is assembled */ | - | ||||||||||||||||||||||||
704 | - | |||||||||||||||||||||||||
705 | /* Generate code to skip over the creation and initialization of the | - | ||||||||||||||||||||||||
706 | ** transient index on 2nd and subsequent iterations of the loop. */ | - | ||||||||||||||||||||||||
707 | v = pParse->pVdbe; | - | ||||||||||||||||||||||||
708 | assert( v!=0 ); | - | ||||||||||||||||||||||||
709 | addrInit = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v); | - | ||||||||||||||||||||||||
710 | - | |||||||||||||||||||||||||
711 | /* Count the number of columns that will be added to the index | - | ||||||||||||||||||||||||
712 | ** and used to match WHERE clause constraints */ | - | ||||||||||||||||||||||||
713 | nKeyCol = 0; | - | ||||||||||||||||||||||||
714 | pTable = pSrc->pTab; | - | ||||||||||||||||||||||||
715 | pWCEnd = &pWC->a[pWC->nTerm]; | - | ||||||||||||||||||||||||
716 | pLoop = pLevel->pWLoop; | - | ||||||||||||||||||||||||
717 | idxCols = 0; | - | ||||||||||||||||||||||||
718 | for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
| 2859-88711 | ||||||||||||||||||||||||
719 | Expr *pExpr = pTerm->pExpr; | - | ||||||||||||||||||||||||
720 | assert( !ExprHasProperty(pExpr, EP_FromJoin) /* prereq always non-zero */ | - | ||||||||||||||||||||||||
721 | || pExpr->iRightJoinTable!=pSrc->iCursor /* for the right-hand */ | - | ||||||||||||||||||||||||
722 | || pLoop->prereq!=0 ); /* table of a LEFT JOIN */ | - | ||||||||||||||||||||||||
723 | if( pLoop->prereq==0
| 146-88565 | ||||||||||||||||||||||||
724 | && (pTerm->wtFlags & TERM_VIRTUAL)==0
| 32-114 | ||||||||||||||||||||||||
725 | && !ExprHasProperty(pExpr, EP_FromJoin)
| 0-114 | ||||||||||||||||||||||||
726 | && sqlite3ExprIsTableConstant(pExpr, pSrc->iCursor) ){
| 50-64 | ||||||||||||||||||||||||
727 | pPartial = sqlite3ExprAnd(pParse->db, pPartial, | - | ||||||||||||||||||||||||
728 | sqlite3ExprDup(pParse->db, pExpr, 0)); | - | ||||||||||||||||||||||||
729 | } executed 64 times by 1 test: end of block Executed by:
| 64 | ||||||||||||||||||||||||
730 | if( termCanDriveIndex(pTerm, pSrc, notReady) ){
| 3019-85692 | ||||||||||||||||||||||||
731 | int iCol = pTerm->u.leftColumn; | - | ||||||||||||||||||||||||
732 | Bitmask cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol);
| 0-3019 | ||||||||||||||||||||||||
733 | testcase( iCol==BMS ); | - | ||||||||||||||||||||||||
734 | testcase( iCol==BMS-1 ); | - | ||||||||||||||||||||||||
735 | if( !sentWarning ){
| 160-2859 | ||||||||||||||||||||||||
736 | sqlite3_log(SQLITE_WARNING_AUTOINDEX, | - | ||||||||||||||||||||||||
737 | "automatic index on %s(%s)", pTable->zName, | - | ||||||||||||||||||||||||
738 | pTable->aCol[iCol].zName); | - | ||||||||||||||||||||||||
739 | sentWarning = 1; | - | ||||||||||||||||||||||||
740 | } executed 2859 times by 1 test: end of block Executed by:
| 2859 | ||||||||||||||||||||||||
741 | if( (idxCols & cMask)==0 ){
| 13-3006 | ||||||||||||||||||||||||
742 | if( whereLoopResize(pParse->db, pLoop, nKeyCol+1) ){
| 0-3006 | ||||||||||||||||||||||||
743 | goto end_auto_index_create; never executed: goto end_auto_index_create; | 0 | ||||||||||||||||||||||||
744 | } | - | ||||||||||||||||||||||||
745 | pLoop->aLTerm[nKeyCol++] = pTerm; | - | ||||||||||||||||||||||||
746 | idxCols |= cMask; | - | ||||||||||||||||||||||||
747 | } executed 3006 times by 1 test: end of block Executed by:
| 3006 | ||||||||||||||||||||||||
748 | } executed 3019 times by 1 test: end of block Executed by:
| 3019 | ||||||||||||||||||||||||
749 | } executed 88711 times by 1 test: end of block Executed by:
| 88711 | ||||||||||||||||||||||||
750 | assert( nKeyCol>0 ); | - | ||||||||||||||||||||||||
751 | pLoop->u.btree.nEq = pLoop->nLTerm = nKeyCol; | - | ||||||||||||||||||||||||
752 | pLoop->wsFlags = WHERE_COLUMN_EQ | WHERE_IDX_ONLY | WHERE_INDEXED | - | ||||||||||||||||||||||||
753 | | WHERE_AUTO_INDEX; | - | ||||||||||||||||||||||||
754 | - | |||||||||||||||||||||||||
755 | /* Count the number of additional columns needed to create a | - | ||||||||||||||||||||||||
756 | ** covering index. A "covering index" is an index that contains all | - | ||||||||||||||||||||||||
757 | ** columns that are needed by the query. With a covering index, the | - | ||||||||||||||||||||||||
758 | ** original table never needs to be accessed. Automatic indices must | - | ||||||||||||||||||||||||
759 | ** be a covering index because the index will not be updated if the | - | ||||||||||||||||||||||||
760 | ** original table changes and the index and table cannot both be used | - | ||||||||||||||||||||||||
761 | ** if they go out of sync. | - | ||||||||||||||||||||||||
762 | */ | - | ||||||||||||||||||||||||
763 | extraCols = pSrc->colUsed & (~idxCols | MASKBIT(BMS-1)); | - | ||||||||||||||||||||||||
764 | mxBitCol = MIN(BMS-1,pTable->nCol);
| 0-2859 | ||||||||||||||||||||||||
765 | testcase( pTable->nCol==BMS-1 ); | - | ||||||||||||||||||||||||
766 | testcase( pTable->nCol==BMS-2 ); | - | ||||||||||||||||||||||||
767 | for(i=0; i<mxBitCol; i++){
| 2859-3899 | ||||||||||||||||||||||||
768 | if( extraCols & MASKBIT(i) ) nKeyCol++; executed 722 times by 1 test: nKeyCol++; Executed by:
| 722-3177 | ||||||||||||||||||||||||
769 | } executed 3899 times by 1 test: end of block Executed by:
| 3899 | ||||||||||||||||||||||||
770 | if( pSrc->colUsed & MASKBIT(BMS-1) ){
| 0-2859 | ||||||||||||||||||||||||
771 | nKeyCol += pTable->nCol - BMS + 1; | - | ||||||||||||||||||||||||
772 | } never executed: end of block | 0 | ||||||||||||||||||||||||
773 | - | |||||||||||||||||||||||||
774 | /* Construct the Index object to describe this index */ | - | ||||||||||||||||||||||||
775 | pIdx = sqlite3AllocateIndexObject(pParse->db, nKeyCol+1, 0, &zNotUsed); | - | ||||||||||||||||||||||||
776 | if( pIdx==0 ) goto end_auto_index_create; never executed: goto end_auto_index_create;
| 0-2859 | ||||||||||||||||||||||||
777 | pLoop->u.btree.pIndex = pIdx; | - | ||||||||||||||||||||||||
778 | pIdx->zName = "auto-index"; | - | ||||||||||||||||||||||||
779 | pIdx->pTable = pTable; | - | ||||||||||||||||||||||||
780 | n = 0; | - | ||||||||||||||||||||||||
781 | idxCols = 0; | - | ||||||||||||||||||||||||
782 | for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
| 2859-88711 | ||||||||||||||||||||||||
783 | if( termCanDriveIndex(pTerm, pSrc, notReady) ){
| 3019-85692 | ||||||||||||||||||||||||
784 | int iCol = pTerm->u.leftColumn; | - | ||||||||||||||||||||||||
785 | Bitmask cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol);
| 0-3019 | ||||||||||||||||||||||||
786 | testcase( iCol==BMS-1 ); | - | ||||||||||||||||||||||||
787 | testcase( iCol==BMS ); | - | ||||||||||||||||||||||||
788 | if( (idxCols & cMask)==0 ){
| 13-3006 | ||||||||||||||||||||||||
789 | Expr *pX = pTerm->pExpr; | - | ||||||||||||||||||||||||
790 | idxCols |= cMask; | - | ||||||||||||||||||||||||
791 | pIdx->aiColumn[n] = pTerm->u.leftColumn; | - | ||||||||||||||||||||||||
792 | pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight); | - | ||||||||||||||||||||||||
793 | pIdx->azColl[n] = pColl ? pColl->zName : sqlite3StrBINARY;
| 1-3005 | ||||||||||||||||||||||||
794 | n++; | - | ||||||||||||||||||||||||
795 | } executed 3006 times by 1 test: end of block Executed by:
| 3006 | ||||||||||||||||||||||||
796 | } executed 3019 times by 1 test: end of block Executed by:
| 3019 | ||||||||||||||||||||||||
797 | } executed 88711 times by 1 test: end of block Executed by:
| 88711 | ||||||||||||||||||||||||
798 | assert( (u32)n==pLoop->u.btree.nEq ); | - | ||||||||||||||||||||||||
799 | - | |||||||||||||||||||||||||
800 | /* Add additional columns needed to make the automatic index into | - | ||||||||||||||||||||||||
801 | ** a covering index */ | - | ||||||||||||||||||||||||
802 | for(i=0; i<mxBitCol; i++){
| 2859-3899 | ||||||||||||||||||||||||
803 | if( extraCols & MASKBIT(i) ){
| 722-3177 | ||||||||||||||||||||||||
804 | pIdx->aiColumn[n] = i; | - | ||||||||||||||||||||||||
805 | pIdx->azColl[n] = sqlite3StrBINARY; | - | ||||||||||||||||||||||||
806 | n++; | - | ||||||||||||||||||||||||
807 | } executed 722 times by 1 test: end of block Executed by:
| 722 | ||||||||||||||||||||||||
808 | } executed 3899 times by 1 test: end of block Executed by:
| 3899 | ||||||||||||||||||||||||
809 | if( pSrc->colUsed & MASKBIT(BMS-1) ){
| 0-2859 | ||||||||||||||||||||||||
810 | for(i=BMS-1; i<pTable->nCol; i++){
| 0 | ||||||||||||||||||||||||
811 | pIdx->aiColumn[n] = i; | - | ||||||||||||||||||||||||
812 | pIdx->azColl[n] = sqlite3StrBINARY; | - | ||||||||||||||||||||||||
813 | n++; | - | ||||||||||||||||||||||||
814 | } never executed: end of block | 0 | ||||||||||||||||||||||||
815 | } never executed: end of block | 0 | ||||||||||||||||||||||||
816 | assert( n==nKeyCol ); | - | ||||||||||||||||||||||||
817 | pIdx->aiColumn[n] = XN_ROWID; | - | ||||||||||||||||||||||||
818 | pIdx->azColl[n] = sqlite3StrBINARY; | - | ||||||||||||||||||||||||
819 | - | |||||||||||||||||||||||||
820 | /* Create the automatic index */ | - | ||||||||||||||||||||||||
821 | assert( pLevel->iIdxCur>=0 ); | - | ||||||||||||||||||||||||
822 | pLevel->iIdxCur = pParse->nTab++; | - | ||||||||||||||||||||||||
823 | sqlite3VdbeAddOp2(v, OP_OpenAutoindex, pLevel->iIdxCur, nKeyCol+1); | - | ||||||||||||||||||||||||
824 | sqlite3VdbeSetP4KeyInfo(pParse, pIdx); | - | ||||||||||||||||||||||||
825 | VdbeComment((v, "for %s", pTable->zName)); | - | ||||||||||||||||||||||||
826 | - | |||||||||||||||||||||||||
827 | /* Fill the automatic index with content */ | - | ||||||||||||||||||||||||
828 | pTabItem = &pWC->pWInfo->pTabList->a[pLevel->iFrom]; | - | ||||||||||||||||||||||||
829 | if( pTabItem->fg.viaCoroutine ){
| 3-2856 | ||||||||||||||||||||||||
830 | int regYield = pTabItem->regReturn; | - | ||||||||||||||||||||||||
831 | addrCounter = sqlite3VdbeAddOp2(v, OP_Integer, 0, 0); | - | ||||||||||||||||||||||||
832 | sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, pTabItem->addrFillSub); | - | ||||||||||||||||||||||||
833 | addrTop = sqlite3VdbeAddOp1(v, OP_Yield, regYield); | - | ||||||||||||||||||||||||
834 | VdbeCoverage(v); | - | ||||||||||||||||||||||||
835 | VdbeComment((v, "next row of %s", pTabItem->pTab->zName)); | - | ||||||||||||||||||||||||
836 | }else{ executed 3 times by 1 test: end of block Executed by:
| 3 | ||||||||||||||||||||||||
837 | addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, pLevel->iTabCur); VdbeCoverage(v); | - | ||||||||||||||||||||||||
838 | } executed 2856 times by 1 test: end of block Executed by:
| 2856 | ||||||||||||||||||||||||
839 | if( pPartial ){
| 59-2800 | ||||||||||||||||||||||||
840 | iContinue = sqlite3VdbeMakeLabel(v); | - | ||||||||||||||||||||||||
841 | sqlite3ExprIfFalse(pParse, pPartial, iContinue, SQLITE_JUMPIFNULL); | - | ||||||||||||||||||||||||
842 | pLoop->wsFlags |= WHERE_PARTIALIDX; | - | ||||||||||||||||||||||||
843 | } executed 59 times by 1 test: end of block Executed by:
| 59 | ||||||||||||||||||||||||
844 | regRecord = sqlite3GetTempReg(pParse); | - | ||||||||||||||||||||||||
845 | regBase = sqlite3GenerateIndexKey( | - | ||||||||||||||||||||||||
846 | pParse, pIdx, pLevel->iTabCur, regRecord, 0, 0, 0, 0 | - | ||||||||||||||||||||||||
847 | ); | - | ||||||||||||||||||||||||
848 | sqlite3VdbeAddOp2(v, OP_IdxInsert, pLevel->iIdxCur, regRecord); | - | ||||||||||||||||||||||||
849 | sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT); | - | ||||||||||||||||||||||||
850 | if( pPartial ) sqlite3VdbeResolveLabel(v, iContinue); executed 59 times by 1 test: sqlite3VdbeResolveLabel(v, iContinue); Executed by:
| 59-2800 | ||||||||||||||||||||||||
851 | if( pTabItem->fg.viaCoroutine ){
| 3-2856 | ||||||||||||||||||||||||
852 | sqlite3VdbeChangeP2(v, addrCounter, regBase+n); | - | ||||||||||||||||||||||||
853 | testcase( pParse->db->mallocFailed ); | - | ||||||||||||||||||||||||
854 | translateColumnToCopy(pParse, addrTop, pLevel->iTabCur, | - | ||||||||||||||||||||||||
855 | pTabItem->regResult, 1); | - | ||||||||||||||||||||||||
856 | sqlite3VdbeGoto(v, addrTop); | - | ||||||||||||||||||||||||
857 | pTabItem->fg.viaCoroutine = 0; | - | ||||||||||||||||||||||||
858 | }else{ executed 3 times by 1 test: end of block Executed by:
| 3 | ||||||||||||||||||||||||
859 | sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1); VdbeCoverage(v); | - | ||||||||||||||||||||||||
860 | } executed 2856 times by 1 test: end of block Executed by:
| 2856 | ||||||||||||||||||||||||
861 | sqlite3VdbeChangeP5(v, SQLITE_STMTSTATUS_AUTOINDEX); | - | ||||||||||||||||||||||||
862 | sqlite3VdbeJumpHere(v, addrTop); | - | ||||||||||||||||||||||||
863 | sqlite3ReleaseTempReg(pParse, regRecord); | - | ||||||||||||||||||||||||
864 | - | |||||||||||||||||||||||||
865 | /* Jump here when skipping the initialization */ | - | ||||||||||||||||||||||||
866 | sqlite3VdbeJumpHere(v, addrInit); | - | ||||||||||||||||||||||||
867 | - | |||||||||||||||||||||||||
868 | end_auto_index_create: code before this statement executed 2859 times by 1 test: end_auto_index_create: Executed by:
| 2859 | ||||||||||||||||||||||||
869 | sqlite3ExprDelete(pParse->db, pPartial); | - | ||||||||||||||||||||||||
870 | } executed 2859 times by 1 test: end of block Executed by:
| 2859 | ||||||||||||||||||||||||
871 | #endif /* SQLITE_OMIT_AUTOMATIC_INDEX */ | - | ||||||||||||||||||||||||
872 | - | |||||||||||||||||||||||||
873 | #ifndef SQLITE_OMIT_VIRTUALTABLE | - | ||||||||||||||||||||||||
874 | /* | - | ||||||||||||||||||||||||
875 | ** Allocate and populate an sqlite3_index_info structure. It is the | - | ||||||||||||||||||||||||
876 | ** responsibility of the caller to eventually release the structure | - | ||||||||||||||||||||||||
877 | ** by passing the pointer returned by this function to sqlite3_free(). | - | ||||||||||||||||||||||||
878 | */ | - | ||||||||||||||||||||||||
879 | static sqlite3_index_info *allocateIndexInfo( | - | ||||||||||||||||||||||||
880 | Parse *pParse, /* The parsing context */ | - | ||||||||||||||||||||||||
881 | WhereClause *pWC, /* The WHERE clause being analyzed */ | - | ||||||||||||||||||||||||
882 | Bitmask mUnusable, /* Ignore terms with these prereqs */ | - | ||||||||||||||||||||||||
883 | struct SrcList_item *pSrc, /* The FROM clause term that is the vtab */ | - | ||||||||||||||||||||||||
884 | ExprList *pOrderBy, /* The ORDER BY clause */ | - | ||||||||||||||||||||||||
885 | u16 *pmNoOmit /* Mask of terms not to omit */ | - | ||||||||||||||||||||||||
886 | ){ | - | ||||||||||||||||||||||||
887 | int i, j; | - | ||||||||||||||||||||||||
888 | int nTerm; | - | ||||||||||||||||||||||||
889 | struct sqlite3_index_constraint *pIdxCons; | - | ||||||||||||||||||||||||
890 | struct sqlite3_index_orderby *pIdxOrderBy; | - | ||||||||||||||||||||||||
891 | struct sqlite3_index_constraint_usage *pUsage; | - | ||||||||||||||||||||||||
892 | struct HiddenIndexInfo *pHidden; | - | ||||||||||||||||||||||||
893 | WhereTerm *pTerm; | - | ||||||||||||||||||||||||
894 | int nOrderBy; | - | ||||||||||||||||||||||||
895 | sqlite3_index_info *pIdxInfo; | - | ||||||||||||||||||||||||
896 | u16 mNoOmit = 0; | - | ||||||||||||||||||||||||
897 | - | |||||||||||||||||||||||||
898 | /* Count the number of possible WHERE clause constraints referring | - | ||||||||||||||||||||||||
899 | ** to this virtual table */ | - | ||||||||||||||||||||||||
900 | for(i=nTerm=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
| 11464-23178 | ||||||||||||||||||||||||
901 | if( pTerm->leftCursor != pSrc->iCursor ) continue; executed 2900 times by 1 test: continue; Executed by:
| 2900-20278 | ||||||||||||||||||||||||
902 | if( pTerm->prereqRight & mUnusable ) continue; executed 31 times by 1 test: continue; Executed by:
| 31-20247 | ||||||||||||||||||||||||
903 | assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) ); | - | ||||||||||||||||||||||||
904 | testcase( pTerm->eOperator & WO_IN ); | - | ||||||||||||||||||||||||
905 | testcase( pTerm->eOperator & WO_ISNULL ); | - | ||||||||||||||||||||||||
906 | testcase( pTerm->eOperator & WO_IS ); | - | ||||||||||||||||||||||||
907 | testcase( pTerm->eOperator & WO_ALL ); | - | ||||||||||||||||||||||||
908 | if( (pTerm->eOperator & ~(WO_EQUIV))==0 ) continue; executed 4 times by 1 test: continue; Executed by:
| 4-20243 | ||||||||||||||||||||||||
909 | if( pTerm->wtFlags & TERM_VNULL ) continue; never executed: continue;
| 0-20243 | ||||||||||||||||||||||||
910 | assert( pTerm->u.leftColumn>=(-1) ); | - | ||||||||||||||||||||||||
911 | nTerm++; | - | ||||||||||||||||||||||||
912 | } executed 20243 times by 1 test: end of block Executed by:
| 20243 | ||||||||||||||||||||||||
913 | - | |||||||||||||||||||||||||
914 | /* If the ORDER BY clause contains only columns in the current | - | ||||||||||||||||||||||||
915 | ** virtual table then allocate space for the aOrderBy part of | - | ||||||||||||||||||||||||
916 | ** the sqlite3_index_info structure. | - | ||||||||||||||||||||||||
917 | */ | - | ||||||||||||||||||||||||
918 | nOrderBy = 0; | - | ||||||||||||||||||||||||
919 | if( pOrderBy ){
| 174-11290 | ||||||||||||||||||||||||
920 | int n = pOrderBy->nExpr; | - | ||||||||||||||||||||||||
921 | for(i=0; i<n; i++){
| 113-187 | ||||||||||||||||||||||||
922 | Expr *pExpr = pOrderBy->a[i].pExpr; | - | ||||||||||||||||||||||||
923 | if( pExpr->op!=TK_COLUMN || pExpr->iTable!=pSrc->iCursor ) break; executed 61 times by 1 test: break; Executed by:
| 26-161 | ||||||||||||||||||||||||
924 | } executed 126 times by 1 test: end of block Executed by:
| 126 | ||||||||||||||||||||||||
925 | if( i==n){
| 61-113 | ||||||||||||||||||||||||
926 | nOrderBy = n; | - | ||||||||||||||||||||||||
927 | } executed 113 times by 1 test: end of block Executed by:
| 113 | ||||||||||||||||||||||||
928 | } executed 174 times by 1 test: end of block Executed by:
| 174 | ||||||||||||||||||||||||
929 | - | |||||||||||||||||||||||||
930 | /* Allocate the sqlite3_index_info structure | - | ||||||||||||||||||||||||
931 | */ | - | ||||||||||||||||||||||||
932 | pIdxInfo = sqlite3DbMallocZero(pParse->db, sizeof(*pIdxInfo) | - | ||||||||||||||||||||||||
933 | + (sizeof(*pIdxCons) + sizeof(*pUsage))*nTerm | - | ||||||||||||||||||||||||
934 | + sizeof(*pIdxOrderBy)*nOrderBy + sizeof(*pHidden) ); | - | ||||||||||||||||||||||||
935 | if( pIdxInfo==0 ){
| 0-11464 | ||||||||||||||||||||||||
936 | sqlite3ErrorMsg(pParse, "out of memory"); | - | ||||||||||||||||||||||||
937 | return 0; never executed: return 0; | 0 | ||||||||||||||||||||||||
938 | } | - | ||||||||||||||||||||||||
939 | - | |||||||||||||||||||||||||
940 | /* Initialize the structure. The sqlite3_index_info structure contains | - | ||||||||||||||||||||||||
941 | ** many fields that are declared "const" to prevent xBestIndex from | - | ||||||||||||||||||||||||
942 | ** changing them. We have to do some funky casting in order to | - | ||||||||||||||||||||||||
943 | ** initialize those fields. | - | ||||||||||||||||||||||||
944 | */ | - | ||||||||||||||||||||||||
945 | pHidden = (struct HiddenIndexInfo*)&pIdxInfo[1]; | - | ||||||||||||||||||||||||
946 | pIdxCons = (struct sqlite3_index_constraint*)&pHidden[1]; | - | ||||||||||||||||||||||||
947 | pIdxOrderBy = (struct sqlite3_index_orderby*)&pIdxCons[nTerm]; | - | ||||||||||||||||||||||||
948 | pUsage = (struct sqlite3_index_constraint_usage*)&pIdxOrderBy[nOrderBy]; | - | ||||||||||||||||||||||||
949 | *(int*)&pIdxInfo->nConstraint = nTerm; | - | ||||||||||||||||||||||||
950 | *(int*)&pIdxInfo->nOrderBy = nOrderBy; | - | ||||||||||||||||||||||||
951 | *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint = pIdxCons; | - | ||||||||||||||||||||||||
952 | *(struct sqlite3_index_orderby**)&pIdxInfo->aOrderBy = pIdxOrderBy; | - | ||||||||||||||||||||||||
953 | *(struct sqlite3_index_constraint_usage**)&pIdxInfo->aConstraintUsage = | - | ||||||||||||||||||||||||
954 | pUsage; | - | ||||||||||||||||||||||||
955 | - | |||||||||||||||||||||||||
956 | pHidden->pWC = pWC; | - | ||||||||||||||||||||||||
957 | pHidden->pParse = pParse; | - | ||||||||||||||||||||||||
958 | for(i=j=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
| 11464-23178 | ||||||||||||||||||||||||
959 | u16 op; | - | ||||||||||||||||||||||||
960 | if( pTerm->leftCursor != pSrc->iCursor ) continue; executed 2900 times by 1 test: continue; Executed by:
| 2900-20278 | ||||||||||||||||||||||||
961 | if( pTerm->prereqRight & mUnusable ) continue; executed 31 times by 1 test: continue; Executed by:
| 31-20247 | ||||||||||||||||||||||||
962 | assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) ); | - | ||||||||||||||||||||||||
963 | testcase( pTerm->eOperator & WO_IN ); | - | ||||||||||||||||||||||||
964 | testcase( pTerm->eOperator & WO_IS ); | - | ||||||||||||||||||||||||
965 | testcase( pTerm->eOperator & WO_ISNULL ); | - | ||||||||||||||||||||||||
966 | testcase( pTerm->eOperator & WO_ALL ); | - | ||||||||||||||||||||||||
967 | if( (pTerm->eOperator & ~(WO_EQUIV))==0 ) continue; executed 4 times by 1 test: continue; Executed by:
| 4-20243 | ||||||||||||||||||||||||
968 | if( pTerm->wtFlags & TERM_VNULL ) continue; never executed: continue;
| 0-20243 | ||||||||||||||||||||||||
969 | if( (pSrc->fg.jointype & JT_LEFT)!=0
| 24-20219 | ||||||||||||||||||||||||
970 | && !ExprHasProperty(pTerm->pExpr, EP_FromJoin)
| 5-19 | ||||||||||||||||||||||||
971 | && (pTerm->eOperator & (WO_IS|WO_ISNULL))
| 2-3 | ||||||||||||||||||||||||
972 | ){ | - | ||||||||||||||||||||||||
973 | /* An "IS" term in the WHERE clause where the virtual table is the rhs | - | ||||||||||||||||||||||||
974 | ** of a LEFT JOIN. Do not pass this term to the virtual table | - | ||||||||||||||||||||||||
975 | ** implementation, as this can lead to incorrect results from SQL such | - | ||||||||||||||||||||||||
976 | ** as: | - | ||||||||||||||||||||||||
977 | ** | - | ||||||||||||||||||||||||
978 | ** "LEFT JOIN vtab WHERE vtab.col IS NULL" */ | - | ||||||||||||||||||||||||
979 | testcase( pTerm->eOperator & WO_ISNULL ); | - | ||||||||||||||||||||||||
980 | testcase( pTerm->eOperator & WO_IS ); | - | ||||||||||||||||||||||||
981 | continue; executed 3 times by 1 test: continue; Executed by:
| 3 | ||||||||||||||||||||||||
982 | } | - | ||||||||||||||||||||||||
983 | assert( pTerm->u.leftColumn>=(-1) ); | - | ||||||||||||||||||||||||
984 | pIdxCons[j].iColumn = pTerm->u.leftColumn; | - | ||||||||||||||||||||||||
985 | pIdxCons[j].iTermOffset = i; | - | ||||||||||||||||||||||||
986 | op = pTerm->eOperator & WO_ALL; | - | ||||||||||||||||||||||||
987 | if( op==WO_IN ) op = WO_EQ; executed 22 times by 1 test: op = 0x0002; Executed by:
| 22-20218 | ||||||||||||||||||||||||
988 | if( op==WO_AUX ){
| 139-20101 | ||||||||||||||||||||||||
989 | pIdxCons[j].op = pTerm->eMatchOp; | - | ||||||||||||||||||||||||
990 | }else if( op & (WO_ISNULL|WO_IS) ){ executed 139 times by 1 test: end of block Executed by:
| 15-20086 | ||||||||||||||||||||||||
991 | if( op==WO_ISNULL ){
| 5-10 | ||||||||||||||||||||||||
992 | pIdxCons[j].op = SQLITE_INDEX_CONSTRAINT_ISNULL; | - | ||||||||||||||||||||||||
993 | }else{ executed 5 times by 1 test: end of block Executed by:
| 5 | ||||||||||||||||||||||||
994 | pIdxCons[j].op = SQLITE_INDEX_CONSTRAINT_IS; | - | ||||||||||||||||||||||||
995 | } executed 10 times by 1 test: end of block Executed by:
| 10 | ||||||||||||||||||||||||
996 | }else{ | - | ||||||||||||||||||||||||
997 | pIdxCons[j].op = (u8)op; | - | ||||||||||||||||||||||||
998 | /* The direct assignment in the previous line is possible only because | - | ||||||||||||||||||||||||
999 | ** the WO_ and SQLITE_INDEX_CONSTRAINT_ codes are identical. The | - | ||||||||||||||||||||||||
1000 | ** following asserts verify this fact. */ | - | ||||||||||||||||||||||||
1001 | assert( WO_EQ==SQLITE_INDEX_CONSTRAINT_EQ ); | - | ||||||||||||||||||||||||
1002 | assert( WO_LT==SQLITE_INDEX_CONSTRAINT_LT ); | - | ||||||||||||||||||||||||
1003 | assert( WO_LE==SQLITE_INDEX_CONSTRAINT_LE ); | - | ||||||||||||||||||||||||
1004 | assert( WO_GT==SQLITE_INDEX_CONSTRAINT_GT ); | - | ||||||||||||||||||||||||
1005 | assert( WO_GE==SQLITE_INDEX_CONSTRAINT_GE ); | - | ||||||||||||||||||||||||
1006 | assert( pTerm->eOperator&(WO_IN|WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE|WO_AUX) ); | - | ||||||||||||||||||||||||
1007 | - | |||||||||||||||||||||||||
1008 | if( op & (WO_LT|WO_LE|WO_GT|WO_GE)
| 245-19841 | ||||||||||||||||||||||||
1009 | && sqlite3ExprIsVector(pTerm->pExpr->pRight)
| 4-241 | ||||||||||||||||||||||||
1010 | ){ | - | ||||||||||||||||||||||||
1011 | if( i<16 ) mNoOmit |= (1 << i); executed 4 times by 1 test: mNoOmit |= (1 << i); Executed by:
| 0-4 | ||||||||||||||||||||||||
1012 | if( op==WO_LT ) pIdxCons[j].op = WO_LE; executed 3 times by 1 test: pIdxCons[j].op = (0x0002<<(55 -53)); Executed by:
| 1-3 | ||||||||||||||||||||||||
1013 | if( op==WO_GT ) pIdxCons[j].op = WO_GE; never executed: pIdxCons[j].op = (0x0002<<(57 -53));
| 0-4 | ||||||||||||||||||||||||
1014 | } executed 4 times by 1 test: end of block Executed by:
| 4 | ||||||||||||||||||||||||
1015 | } executed 20086 times by 1 test: end of block Executed by:
| 20086 | ||||||||||||||||||||||||
1016 | - | |||||||||||||||||||||||||
1017 | j++; | - | ||||||||||||||||||||||||
1018 | } executed 20240 times by 1 test: end of block Executed by:
| 20240 | ||||||||||||||||||||||||
1019 | for(i=0; i<nOrderBy; i++){
| 125-11464 | ||||||||||||||||||||||||
1020 | Expr *pExpr = pOrderBy->a[i].pExpr; | - | ||||||||||||||||||||||||
1021 | pIdxOrderBy[i].iColumn = pExpr->iColumn; | - | ||||||||||||||||||||||||
1022 | pIdxOrderBy[i].desc = pOrderBy->a[i].sortOrder; | - | ||||||||||||||||||||||||
1023 | } executed 125 times by 1 test: end of block Executed by:
| 125 | ||||||||||||||||||||||||
1024 | - | |||||||||||||||||||||||||
1025 | *pmNoOmit = mNoOmit; | - | ||||||||||||||||||||||||
1026 | return pIdxInfo; executed 11464 times by 1 test: return pIdxInfo; Executed by:
| 11464 | ||||||||||||||||||||||||
1027 | } | - | ||||||||||||||||||||||||
1028 | - | |||||||||||||||||||||||||
1029 | /* | - | ||||||||||||||||||||||||
1030 | ** The table object reference passed as the second argument to this function | - | ||||||||||||||||||||||||
1031 | ** must represent a virtual table. This function invokes the xBestIndex() | - | ||||||||||||||||||||||||
1032 | ** method of the virtual table with the sqlite3_index_info object that | - | ||||||||||||||||||||||||
1033 | ** comes in as the 3rd argument to this function. | - | ||||||||||||||||||||||||
1034 | ** | - | ||||||||||||||||||||||||
1035 | ** If an error occurs, pParse is populated with an error message and a | - | ||||||||||||||||||||||||
1036 | ** non-zero value is returned. Otherwise, 0 is returned and the output | - | ||||||||||||||||||||||||
1037 | ** part of the sqlite3_index_info structure is left populated. | - | ||||||||||||||||||||||||
1038 | ** | - | ||||||||||||||||||||||||
1039 | ** Whether or not an error is returned, it is the responsibility of the | - | ||||||||||||||||||||||||
1040 | ** caller to eventually free p->idxStr if p->needToFreeIdxStr indicates | - | ||||||||||||||||||||||||
1041 | ** that this is required. | - | ||||||||||||||||||||||||
1042 | */ | - | ||||||||||||||||||||||||
1043 | static int vtabBestIndex(Parse *pParse, Table *pTab, sqlite3_index_info *p){ | - | ||||||||||||||||||||||||
1044 | sqlite3_vtab *pVtab = sqlite3GetVTable(pParse->db, pTab)->pVtab; | - | ||||||||||||||||||||||||
1045 | int rc; | - | ||||||||||||||||||||||||
1046 | - | |||||||||||||||||||||||||
1047 | TRACE_IDX_INPUTS(p); | - | ||||||||||||||||||||||||
1048 | rc = pVtab->pModule->xBestIndex(pVtab, p); | - | ||||||||||||||||||||||||
1049 | TRACE_IDX_OUTPUTS(p); | - | ||||||||||||||||||||||||
1050 | - | |||||||||||||||||||||||||
1051 | if( rc!=SQLITE_OK ){
| 1-12101 | ||||||||||||||||||||||||
1052 | if( rc==SQLITE_NOMEM ){
| 0-1 | ||||||||||||||||||||||||
1053 | sqlite3OomFault(pParse->db); | - | ||||||||||||||||||||||||
1054 | }else if( !pVtab->zErrMsg ){ never executed: end of block
| 0-1 | ||||||||||||||||||||||||
1055 | sqlite3ErrorMsg(pParse, "%s", sqlite3ErrStr(rc)); | - | ||||||||||||||||||||||||
1056 | }else{ never executed: end of block | 0 | ||||||||||||||||||||||||
1057 | sqlite3ErrorMsg(pParse, "%s", pVtab->zErrMsg); | - | ||||||||||||||||||||||||
1058 | } executed 1 time by 1 test: end of block Executed by:
| 1 | ||||||||||||||||||||||||
1059 | } | - | ||||||||||||||||||||||||
1060 | sqlite3_free(pVtab->zErrMsg); | - | ||||||||||||||||||||||||
1061 | pVtab->zErrMsg = 0; | - | ||||||||||||||||||||||||
1062 | - | |||||||||||||||||||||||||
1063 | #if 0 | - | ||||||||||||||||||||||||
1064 | /* This error is now caught by the caller. | - | ||||||||||||||||||||||||
1065 | ** Search for "xBestIndex malfunction" below */ | - | ||||||||||||||||||||||||
1066 | for(i=0; i<p->nConstraint; i++){ | - | ||||||||||||||||||||||||
1067 | if( !p->aConstraint[i].usable && p->aConstraintUsage[i].argvIndex>0 ){ | - | ||||||||||||||||||||||||
1068 | sqlite3ErrorMsg(pParse, | - | ||||||||||||||||||||||||
1069 | "table %s: xBestIndex returned an invalid plan", pTab->zName); | - | ||||||||||||||||||||||||
1070 | } | - | ||||||||||||||||||||||||
1071 | } | - | ||||||||||||||||||||||||
1072 | #endif | - | ||||||||||||||||||||||||
1073 | - | |||||||||||||||||||||||||
1074 | return pParse->nErr; executed 12102 times by 1 test: return pParse->nErr; Executed by:
| 12102 | ||||||||||||||||||||||||
1075 | } | - | ||||||||||||||||||||||||
1076 | #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) */ | - | ||||||||||||||||||||||||
1077 | - | |||||||||||||||||||||||||
1078 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 | - | ||||||||||||||||||||||||
1079 | /* | - | ||||||||||||||||||||||||
1080 | ** Estimate the location of a particular key among all keys in an | - | ||||||||||||||||||||||||
1081 | ** index. Store the results in aStat as follows: | - | ||||||||||||||||||||||||
1082 | ** | - | ||||||||||||||||||||||||
1083 | ** aStat[0] Est. number of rows less than pRec | - | ||||||||||||||||||||||||
1084 | ** aStat[1] Est. number of rows equal to pRec | - | ||||||||||||||||||||||||
1085 | ** | - | ||||||||||||||||||||||||
1086 | ** Return the index of the sample that is the smallest sample that | - | ||||||||||||||||||||||||
1087 | ** is greater than or equal to pRec. Note that this index is not an index | - | ||||||||||||||||||||||||
1088 | ** into the aSample[] array - it is an index into a virtual set of samples | - | ||||||||||||||||||||||||
1089 | ** based on the contents of aSample[] and the number of fields in record | - | ||||||||||||||||||||||||
1090 | ** pRec. | - | ||||||||||||||||||||||||
1091 | */ | - | ||||||||||||||||||||||||
1092 | static int whereKeyStats( | - | ||||||||||||||||||||||||
1093 | Parse *pParse, /* Database connection */ | - | ||||||||||||||||||||||||
1094 | Index *pIdx, /* Index to consider domain of */ | - | ||||||||||||||||||||||||
1095 | UnpackedRecord *pRec, /* Vector of values to consider */ | - | ||||||||||||||||||||||||
1096 | int roundUp, /* Round up if true. Round down if false */ | - | ||||||||||||||||||||||||
1097 | tRowcnt *aStat /* OUT: stats written here */ | - | ||||||||||||||||||||||||
1098 | ){ | - | ||||||||||||||||||||||||
1099 | IndexSample *aSample = pIdx->aSample; | - | ||||||||||||||||||||||||
1100 | int iCol; /* Index of required stats in anEq[] etc. */ | - | ||||||||||||||||||||||||
1101 | int i; /* Index of first sample >= pRec */ | - | ||||||||||||||||||||||||
1102 | int iSample; /* Smallest sample larger than or equal to pRec */ | - | ||||||||||||||||||||||||
1103 | int iMin = 0; /* Smallest sample not yet tested */ | - | ||||||||||||||||||||||||
1104 | int iTest; /* Next sample to test */ | - | ||||||||||||||||||||||||
1105 | int res; /* Result of comparison operation */ | - | ||||||||||||||||||||||||
1106 | int nField; /* Number of fields in pRec */ | - | ||||||||||||||||||||||||
1107 | tRowcnt iLower = 0; /* anLt[] + anEq[] of largest sample pRec is > */ | - | ||||||||||||||||||||||||
1108 | - | |||||||||||||||||||||||||
1109 | #ifndef SQLITE_DEBUG | - | ||||||||||||||||||||||||
1110 | UNUSED_PARAMETER( pParse ); | - | ||||||||||||||||||||||||
1111 | #endif | - | ||||||||||||||||||||||||
1112 | assert( pRec!=0 ); | - | ||||||||||||||||||||||||
1113 | assert( pIdx->nSample>0 ); | - | ||||||||||||||||||||||||
1114 | assert( pRec->nField>0 && pRec->nField<=pIdx->nSampleCol ); | - | ||||||||||||||||||||||||
1115 | - | |||||||||||||||||||||||||
1116 | /* Do a binary search to find the first sample greater than or equal | - | ||||||||||||||||||||||||
1117 | ** to pRec. If pRec contains a single field, the set of samples to search | - | ||||||||||||||||||||||||
1118 | ** is simply the aSample[] array. If the samples in aSample[] contain more | - | ||||||||||||||||||||||||
1119 | ** than one fields, all fields following the first are ignored. | - | ||||||||||||||||||||||||
1120 | ** | - | ||||||||||||||||||||||||
1121 | ** If pRec contains N fields, where N is more than one, then as well as the | - | ||||||||||||||||||||||||
1122 | ** samples in aSample[] (truncated to N fields), the search also has to | - | ||||||||||||||||||||||||
1123 | ** consider prefixes of those samples. For example, if the set of samples | - | ||||||||||||||||||||||||
1124 | ** in aSample is: | - | ||||||||||||||||||||||||
1125 | ** | - | ||||||||||||||||||||||||
1126 | ** aSample[0] = (a, 5) | - | ||||||||||||||||||||||||
1127 | ** aSample[1] = (a, 10) | - | ||||||||||||||||||||||||
1128 | ** aSample[2] = (b, 5) | - | ||||||||||||||||||||||||
1129 | ** aSample[3] = (c, 100) | - | ||||||||||||||||||||||||
1130 | ** aSample[4] = (c, 105) | - | ||||||||||||||||||||||||
1131 | ** | - | ||||||||||||||||||||||||
1132 | ** Then the search space should ideally be the samples above and the | - | ||||||||||||||||||||||||
1133 | ** unique prefixes [a], [b] and [c]. But since that is hard to organize, | - | ||||||||||||||||||||||||
1134 | ** the code actually searches this set: | - | ||||||||||||||||||||||||
1135 | ** | - | ||||||||||||||||||||||||
1136 | ** 0: (a) | - | ||||||||||||||||||||||||
1137 | ** 1: (a, 5) | - | ||||||||||||||||||||||||
1138 | ** 2: (a, 10) | - | ||||||||||||||||||||||||
1139 | ** 3: (a, 10) | - | ||||||||||||||||||||||||
1140 | ** 4: (b) | - | ||||||||||||||||||||||||
1141 | ** 5: (b, 5) | - | ||||||||||||||||||||||||
1142 | ** 6: (c) | - | ||||||||||||||||||||||||
1143 | ** 7: (c, 100) | - | ||||||||||||||||||||||||
1144 | ** 8: (c, 105) | - | ||||||||||||||||||||||||
1145 | ** 9: (c, 105) | - | ||||||||||||||||||||||||
1146 | ** | - | ||||||||||||||||||||||||
1147 | ** For each sample in the aSample[] array, N samples are present in the | - | ||||||||||||||||||||||||
1148 | ** effective sample array. In the above, samples 0 and 1 are based on | - | ||||||||||||||||||||||||
1149 | ** sample aSample[0]. Samples 2 and 3 on aSample[1] etc. | - | ||||||||||||||||||||||||
1150 | ** | - | ||||||||||||||||||||||||
1151 | ** Often, sample i of each block of N effective samples has (i+1) fields. | - | ||||||||||||||||||||||||
1152 | ** Except, each sample may be extended to ensure that it is greater than or | - | ||||||||||||||||||||||||
1153 | ** equal to the previous sample in the array. For example, in the above, | - | ||||||||||||||||||||||||
1154 | ** sample 2 is the first sample of a block of N samples, so at first it | - | ||||||||||||||||||||||||
1155 | ** appears that it should be 1 field in size. However, that would make it | - | ||||||||||||||||||||||||
1156 | ** smaller than sample 1, so the binary search would not work. As a result, | - | ||||||||||||||||||||||||
1157 | ** it is extended to two fields. The duplicates that this creates do not | - | ||||||||||||||||||||||||
1158 | ** cause any problems. | - | ||||||||||||||||||||||||
1159 | */ | - | ||||||||||||||||||||||||
1160 | nField = pRec->nField; | - | ||||||||||||||||||||||||
1161 | iCol = 0; | - | ||||||||||||||||||||||||
1162 | iSample = pIdx->nSample * nField; | - | ||||||||||||||||||||||||
1163 | do{ | - | ||||||||||||||||||||||||
1164 | int iSamp; /* Index in aSample[] of test sample */ | - | ||||||||||||||||||||||||
1165 | int n; /* Number of fields in test sample */ | - | ||||||||||||||||||||||||
1166 | - | |||||||||||||||||||||||||
1167 | iTest = (iMin+iSample)/2; | - | ||||||||||||||||||||||||
1168 | iSamp = iTest / nField; | - | ||||||||||||||||||||||||
1169 | if( iSamp>0 ){ | - | ||||||||||||||||||||||||
1170 | /* The proposed effective sample is a prefix of sample aSample[iSamp]. | - | ||||||||||||||||||||||||
1171 | ** Specifically, the shortest prefix of at least (1 + iTest%nField) | - | ||||||||||||||||||||||||
1172 | ** fields that is greater than the previous effective sample. */ | - | ||||||||||||||||||||||||
1173 | for(n=(iTest % nField) + 1; n<nField; n++){ | - | ||||||||||||||||||||||||
1174 | if( aSample[iSamp-1].anLt[n-1]!=aSample[iSamp].anLt[n-1] ) break; | - | ||||||||||||||||||||||||
1175 | } | - | ||||||||||||||||||||||||
1176 | }else{ | - | ||||||||||||||||||||||||
1177 | n = iTest + 1; | - | ||||||||||||||||||||||||
1178 | } | - | ||||||||||||||||||||||||
1179 | - | |||||||||||||||||||||||||
1180 | pRec->nField = n; | - | ||||||||||||||||||||||||
1181 | res = sqlite3VdbeRecordCompare(aSample[iSamp].n, aSample[iSamp].p, pRec); | - | ||||||||||||||||||||||||
1182 | if( res<0 ){ | - | ||||||||||||||||||||||||
1183 | iLower = aSample[iSamp].anLt[n-1] + aSample[iSamp].anEq[n-1]; | - | ||||||||||||||||||||||||
1184 | iMin = iTest+1; | - | ||||||||||||||||||||||||
1185 | }else if( res==0 && n<nField ){ | - | ||||||||||||||||||||||||
1186 | iLower = aSample[iSamp].anLt[n-1]; | - | ||||||||||||||||||||||||
1187 | iMin = iTest+1; | - | ||||||||||||||||||||||||
1188 | res = -1; | - | ||||||||||||||||||||||||
1189 | }else{ | - | ||||||||||||||||||||||||
1190 | iSample = iTest; | - | ||||||||||||||||||||||||
1191 | iCol = n-1; | - | ||||||||||||||||||||||||
1192 | } | - | ||||||||||||||||||||||||
1193 | }while( res && iMin<iSample ); | - | ||||||||||||||||||||||||
1194 | i = iSample / nField; | - | ||||||||||||||||||||||||
1195 | - | |||||||||||||||||||||||||
1196 | #ifdef SQLITE_DEBUG | - | ||||||||||||||||||||||||
1197 | /* The following assert statements check that the binary search code | - | ||||||||||||||||||||||||
1198 | ** above found the right answer. This block serves no purpose other | - | ||||||||||||||||||||||||
1199 | ** than to invoke the asserts. */ | - | ||||||||||||||||||||||||
1200 | if( pParse->db->mallocFailed==0 ){ | - | ||||||||||||||||||||||||
1201 | if( res==0 ){ | - | ||||||||||||||||||||||||
1202 | /* If (res==0) is true, then pRec must be equal to sample i. */ | - | ||||||||||||||||||||||||
1203 | assert( i<pIdx->nSample ); | - | ||||||||||||||||||||||||
1204 | assert( iCol==nField-1 ); | - | ||||||||||||||||||||||||
1205 | pRec->nField = nField; | - | ||||||||||||||||||||||||
1206 | assert( 0==sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec) | - | ||||||||||||||||||||||||
1207 | || pParse->db->mallocFailed | - | ||||||||||||||||||||||||
1208 | ); | - | ||||||||||||||||||||||||
1209 | }else{ | - | ||||||||||||||||||||||||
1210 | /* Unless i==pIdx->nSample, indicating that pRec is larger than | - | ||||||||||||||||||||||||
1211 | ** all samples in the aSample[] array, pRec must be smaller than the | - | ||||||||||||||||||||||||
1212 | ** (iCol+1) field prefix of sample i. */ | - | ||||||||||||||||||||||||
1213 | assert( i<=pIdx->nSample && i>=0 ); | - | ||||||||||||||||||||||||
1214 | pRec->nField = iCol+1; | - | ||||||||||||||||||||||||
1215 | assert( i==pIdx->nSample | - | ||||||||||||||||||||||||
1216 | || sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)>0 | - | ||||||||||||||||||||||||
1217 | || pParse->db->mallocFailed ); | - | ||||||||||||||||||||||||
1218 | - | |||||||||||||||||||||||||
1219 | /* if i==0 and iCol==0, then record pRec is smaller than all samples | - | ||||||||||||||||||||||||
1220 | ** in the aSample[] array. Otherwise, if (iCol>0) then pRec must | - | ||||||||||||||||||||||||
1221 | ** be greater than or equal to the (iCol) field prefix of sample i. | - | ||||||||||||||||||||||||
1222 | ** If (i>0), then pRec must also be greater than sample (i-1). */ | - | ||||||||||||||||||||||||
1223 | if( iCol>0 ){ | - | ||||||||||||||||||||||||
1224 | pRec->nField = iCol; | - | ||||||||||||||||||||||||
1225 | assert( sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)<=0 | - | ||||||||||||||||||||||||
1226 | || pParse->db->mallocFailed ); | - | ||||||||||||||||||||||||
1227 | } | - | ||||||||||||||||||||||||
1228 | if( i>0 ){ | - | ||||||||||||||||||||||||
1229 | pRec->nField = nField; | - | ||||||||||||||||||||||||
1230 | assert( sqlite3VdbeRecordCompare(aSample[i-1].n, aSample[i-1].p, pRec)<0 | - | ||||||||||||||||||||||||
1231 | || pParse->db->mallocFailed ); | - | ||||||||||||||||||||||||
1232 | } | - | ||||||||||||||||||||||||
1233 | } | - | ||||||||||||||||||||||||
1234 | } | - | ||||||||||||||||||||||||
1235 | #endif /* ifdef SQLITE_DEBUG */ | - | ||||||||||||||||||||||||
1236 | - | |||||||||||||||||||||||||
1237 | if( res==0 ){ | - | ||||||||||||||||||||||||
1238 | /* Record pRec is equal to sample i */ | - | ||||||||||||||||||||||||
1239 | assert( iCol==nField-1 ); | - | ||||||||||||||||||||||||
1240 | aStat[0] = aSample[i].anLt[iCol]; | - | ||||||||||||||||||||||||
1241 | aStat[1] = aSample[i].anEq[iCol]; | - | ||||||||||||||||||||||||
1242 | }else{ | - | ||||||||||||||||||||||||
1243 | /* At this point, the (iCol+1) field prefix of aSample[i] is the first | - | ||||||||||||||||||||||||
1244 | ** sample that is greater than pRec. Or, if i==pIdx->nSample then pRec | - | ||||||||||||||||||||||||
1245 | ** is larger than all samples in the array. */ | - | ||||||||||||||||||||||||
1246 | tRowcnt iUpper, iGap; | - | ||||||||||||||||||||||||
1247 | if( i>=pIdx->nSample ){ | - | ||||||||||||||||||||||||
1248 | iUpper = sqlite3LogEstToInt(pIdx->aiRowLogEst[0]); | - | ||||||||||||||||||||||||
1249 | }else{ | - | ||||||||||||||||||||||||
1250 | iUpper = aSample[i].anLt[iCol]; | - | ||||||||||||||||||||||||
1251 | } | - | ||||||||||||||||||||||||
1252 | - | |||||||||||||||||||||||||
1253 | if( iLower>=iUpper ){ | - | ||||||||||||||||||||||||
1254 | iGap = 0; | - | ||||||||||||||||||||||||
1255 | }else{ | - | ||||||||||||||||||||||||
1256 | iGap = iUpper - iLower; | - | ||||||||||||||||||||||||
1257 | } | - | ||||||||||||||||||||||||
1258 | if( roundUp ){ | - | ||||||||||||||||||||||||
1259 | iGap = (iGap*2)/3; | - | ||||||||||||||||||||||||
1260 | }else{ | - | ||||||||||||||||||||||||
1261 | iGap = iGap/3; | - | ||||||||||||||||||||||||
1262 | } | - | ||||||||||||||||||||||||
1263 | aStat[0] = iLower + iGap; | - | ||||||||||||||||||||||||
1264 | aStat[1] = pIdx->aAvgEq[nField-1]; | - | ||||||||||||||||||||||||
1265 | } | - | ||||||||||||||||||||||||
1266 | - | |||||||||||||||||||||||||
1267 | /* Restore the pRec->nField value before returning. */ | - | ||||||||||||||||||||||||
1268 | pRec->nField = nField; | - | ||||||||||||||||||||||||
1269 | return i; | - | ||||||||||||||||||||||||
1270 | } | - | ||||||||||||||||||||||||
1271 | #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ | - | ||||||||||||||||||||||||
1272 | - | |||||||||||||||||||||||||
1273 | /* | - | ||||||||||||||||||||||||
1274 | ** If it is not NULL, pTerm is a term that provides an upper or lower | - | ||||||||||||||||||||||||
1275 | ** bound on a range scan. Without considering pTerm, it is estimated | - | ||||||||||||||||||||||||
1276 | ** that the scan will visit nNew rows. This function returns the number | - | ||||||||||||||||||||||||
1277 | ** estimated to be visited after taking pTerm into account. | - | ||||||||||||||||||||||||
1278 | ** | - | ||||||||||||||||||||||||
1279 | ** If the user explicitly specified a likelihood() value for this term, | - | ||||||||||||||||||||||||
1280 | ** then the return value is the likelihood multiplied by the number of | - | ||||||||||||||||||||||||
1281 | ** input rows. Otherwise, this function assumes that an "IS NOT NULL" term | - | ||||||||||||||||||||||||
1282 | ** has a likelihood of 0.50, and any other term a likelihood of 0.25. | - | ||||||||||||||||||||||||
1283 | */ | - | ||||||||||||||||||||||||
1284 | static LogEst whereRangeAdjust(WhereTerm *pTerm, LogEst nNew){ | - | ||||||||||||||||||||||||
1285 | LogEst nRet = nNew; | - | ||||||||||||||||||||||||
1286 | if( pTerm ){
| 25792-56274 | ||||||||||||||||||||||||
1287 | if( pTerm->truthProb<=0 ){
| 5-56269 | ||||||||||||||||||||||||
1288 | nRet += pTerm->truthProb; | - | ||||||||||||||||||||||||
1289 | }else if( (pTerm->wtFlags & TERM_VNULL)==0 ){ executed 5 times by 1 test: end of block Executed by:
| 0-56269 | ||||||||||||||||||||||||
1290 | nRet -= 20; assert( 20==sqlite3LogEst(4) ); | - | ||||||||||||||||||||||||
1291 | } executed 56269 times by 1 test: end of block Executed by:
| 56269 | ||||||||||||||||||||||||
1292 | } executed 56274 times by 1 test: end of block Executed by:
| 56274 | ||||||||||||||||||||||||
1293 | return nRet; executed 82066 times by 1 test: return nRet; Executed by:
| 82066 | ||||||||||||||||||||||||
1294 | } | - | ||||||||||||||||||||||||
1295 | - | |||||||||||||||||||||||||
1296 | - | |||||||||||||||||||||||||
1297 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 | - | ||||||||||||||||||||||||
1298 | /* | - | ||||||||||||||||||||||||
1299 | ** Return the affinity for a single column of an index. | - | ||||||||||||||||||||||||
1300 | */ | - | ||||||||||||||||||||||||
1301 | char sqlite3IndexColumnAffinity(sqlite3 *db, Index *pIdx, int iCol){ | - | ||||||||||||||||||||||||
1302 | assert( iCol>=0 && iCol<pIdx->nColumn ); | - | ||||||||||||||||||||||||
1303 | if( !pIdx->zColAff ){ | - | ||||||||||||||||||||||||
1304 | if( sqlite3IndexAffinityStr(db, pIdx)==0 ) return SQLITE_AFF_BLOB; | - | ||||||||||||||||||||||||
1305 | } | - | ||||||||||||||||||||||||
1306 | return pIdx->zColAff[iCol]; | - | ||||||||||||||||||||||||
1307 | } | - | ||||||||||||||||||||||||
1308 | #endif | - | ||||||||||||||||||||||||
1309 | - | |||||||||||||||||||||||||
1310 | - | |||||||||||||||||||||||||
1311 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 | - | ||||||||||||||||||||||||
1312 | /* | - | ||||||||||||||||||||||||
1313 | ** This function is called to estimate the number of rows visited by a | - | ||||||||||||||||||||||||
1314 | ** range-scan on a skip-scan index. For example: | - | ||||||||||||||||||||||||
1315 | ** | - | ||||||||||||||||||||||||
1316 | ** CREATE INDEX i1 ON t1(a, b, c); | - | ||||||||||||||||||||||||
1317 | ** SELECT * FROM t1 WHERE a=? AND c BETWEEN ? AND ?; | - | ||||||||||||||||||||||||
1318 | ** | - | ||||||||||||||||||||||||
1319 | ** Value pLoop->nOut is currently set to the estimated number of rows | - | ||||||||||||||||||||||||
1320 | ** visited for scanning (a=? AND b=?). This function reduces that estimate | - | ||||||||||||||||||||||||
1321 | ** by some factor to account for the (c BETWEEN ? AND ?) expression based | - | ||||||||||||||||||||||||
1322 | ** on the stat4 data for the index. this scan will be peformed multiple | - | ||||||||||||||||||||||||
1323 | ** times (once for each (a,b) combination that matches a=?) is dealt with | - | ||||||||||||||||||||||||
1324 | ** by the caller. | - | ||||||||||||||||||||||||
1325 | ** | - | ||||||||||||||||||||||||
1326 | ** It does this by scanning through all stat4 samples, comparing values | - | ||||||||||||||||||||||||
1327 | ** extracted from pLower and pUpper with the corresponding column in each | - | ||||||||||||||||||||||||
1328 | ** sample. If L and U are the number of samples found to be less than or | - | ||||||||||||||||||||||||
1329 | ** equal to the values extracted from pLower and pUpper respectively, and | - | ||||||||||||||||||||||||
1330 | ** N is the total number of samples, the pLoop->nOut value is adjusted | - | ||||||||||||||||||||||||
1331 | ** as follows: | - | ||||||||||||||||||||||||
1332 | ** | - | ||||||||||||||||||||||||
1333 | ** nOut = nOut * ( min(U - L, 1) / N ) | - | ||||||||||||||||||||||||
1334 | ** | - | ||||||||||||||||||||||||
1335 | ** If pLower is NULL, or a value cannot be extracted from the term, L is | - | ||||||||||||||||||||||||
1336 | ** set to zero. If pUpper is NULL, or a value cannot be extracted from it, | - | ||||||||||||||||||||||||
1337 | ** U is set to N. | - | ||||||||||||||||||||||||
1338 | ** | - | ||||||||||||||||||||||||
1339 | ** Normally, this function sets *pbDone to 1 before returning. However, | - | ||||||||||||||||||||||||
1340 | ** if no value can be extracted from either pLower or pUpper (and so the | - | ||||||||||||||||||||||||
1341 | ** estimate of the number of rows delivered remains unchanged), *pbDone | - | ||||||||||||||||||||||||
1342 | ** is left as is. | - | ||||||||||||||||||||||||
1343 | ** | - | ||||||||||||||||||||||||
1344 | ** If an error occurs, an SQLite error code is returned. Otherwise, | - | ||||||||||||||||||||||||
1345 | ** SQLITE_OK. | - | ||||||||||||||||||||||||
1346 | */ | - | ||||||||||||||||||||||||
1347 | static int whereRangeSkipScanEst( | - | ||||||||||||||||||||||||
1348 | Parse *pParse, /* Parsing & code generating context */ | - | ||||||||||||||||||||||||
1349 | WhereTerm *pLower, /* Lower bound on the range. ex: "x>123" Might be NULL */ | - | ||||||||||||||||||||||||
1350 | WhereTerm *pUpper, /* Upper bound on the range. ex: "x<455" Might be NULL */ | - | ||||||||||||||||||||||||
1351 | WhereLoop *pLoop, /* Update the .nOut value of this loop */ | - | ||||||||||||||||||||||||
1352 | int *pbDone /* Set to true if at least one expr. value extracted */ | - | ||||||||||||||||||||||||
1353 | ){ | - | ||||||||||||||||||||||||
1354 | Index *p = pLoop->u.btree.pIndex; | - | ||||||||||||||||||||||||
1355 | int nEq = pLoop->u.btree.nEq; | - | ||||||||||||||||||||||||
1356 | sqlite3 *db = pParse->db; | - | ||||||||||||||||||||||||
1357 | int nLower = -1; | - | ||||||||||||||||||||||||
1358 | int nUpper = p->nSample+1; | - | ||||||||||||||||||||||||
1359 | int rc = SQLITE_OK; | - | ||||||||||||||||||||||||
1360 | u8 aff = sqlite3IndexColumnAffinity(db, p, nEq); | - | ||||||||||||||||||||||||
1361 | CollSeq *pColl; | - | ||||||||||||||||||||||||
1362 | - | |||||||||||||||||||||||||
1363 | sqlite3_value *p1 = 0; /* Value extracted from pLower */ | - | ||||||||||||||||||||||||
1364 | sqlite3_value *p2 = 0; /* Value extracted from pUpper */ | - | ||||||||||||||||||||||||
1365 | sqlite3_value *pVal = 0; /* Value extracted from record */ | - | ||||||||||||||||||||||||
1366 | - | |||||||||||||||||||||||||
1367 | pColl = sqlite3LocateCollSeq(pParse, p->azColl[nEq]); | - | ||||||||||||||||||||||||
1368 | if( pLower ){ | - | ||||||||||||||||||||||||
1369 | rc = sqlite3Stat4ValueFromExpr(pParse, pLower->pExpr->pRight, aff, &p1); | - | ||||||||||||||||||||||||
1370 | nLower = 0; | - | ||||||||||||||||||||||||
1371 | } | - | ||||||||||||||||||||||||
1372 | if( pUpper && rc==SQLITE_OK ){ | - | ||||||||||||||||||||||||
1373 | rc = sqlite3Stat4ValueFromExpr(pParse, pUpper->pExpr->pRight, aff, &p2); | - | ||||||||||||||||||||||||
1374 | nUpper = p2 ? 0 : p->nSample; | - | ||||||||||||||||||||||||
1375 | } | - | ||||||||||||||||||||||||
1376 | - | |||||||||||||||||||||||||
1377 | if( p1 || p2 ){ | - | ||||||||||||||||||||||||
1378 | int i; | - | ||||||||||||||||||||||||
1379 | int nDiff; | - | ||||||||||||||||||||||||
1380 | for(i=0; rc==SQLITE_OK && i<p->nSample; i++){ | - | ||||||||||||||||||||||||
1381 | rc = sqlite3Stat4Column(db, p->aSample[i].p, p->aSample[i].n, nEq, &pVal); | - | ||||||||||||||||||||||||
1382 | if( rc==SQLITE_OK && p1 ){ | - | ||||||||||||||||||||||||
1383 | int res = sqlite3MemCompare(p1, pVal, pColl); | - | ||||||||||||||||||||||||
1384 | if( res>=0 ) nLower++; | - | ||||||||||||||||||||||||
1385 | } | - | ||||||||||||||||||||||||
1386 | if( rc==SQLITE_OK && p2 ){ | - | ||||||||||||||||||||||||
1387 | int res = sqlite3MemCompare(p2, pVal, pColl); | - | ||||||||||||||||||||||||
1388 | if( res>=0 ) nUpper++; | - | ||||||||||||||||||||||||
1389 | } | - | ||||||||||||||||||||||||
1390 | } | - | ||||||||||||||||||||||||
1391 | nDiff = (nUpper - nLower); | - | ||||||||||||||||||||||||
1392 | if( nDiff<=0 ) nDiff = 1; | - | ||||||||||||||||||||||||
1393 | - | |||||||||||||||||||||||||
1394 | /* If there is both an upper and lower bound specified, and the | - | ||||||||||||||||||||||||
1395 | ** comparisons indicate that they are close together, use the fallback | - | ||||||||||||||||||||||||
1396 | ** method (assume that the scan visits 1/64 of the rows) for estimating | - | ||||||||||||||||||||||||
1397 | ** the number of rows visited. Otherwise, estimate the number of rows | - | ||||||||||||||||||||||||
1398 | ** using the method described in the header comment for this function. */ | - | ||||||||||||||||||||||||
1399 | if( nDiff!=1 || pUpper==0 || pLower==0 ){ | - | ||||||||||||||||||||||||
1400 | int nAdjust = (sqlite3LogEst(p->nSample) - sqlite3LogEst(nDiff)); | - | ||||||||||||||||||||||||
1401 | pLoop->nOut -= nAdjust; | - | ||||||||||||||||||||||||
1402 | *pbDone = 1; | - | ||||||||||||||||||||||||
1403 | WHERETRACE(0x10, ("range skip-scan regions: %u..%u adjust=%d est=%d\n", | - | ||||||||||||||||||||||||
1404 | nLower, nUpper, nAdjust*-1, pLoop->nOut)); | - | ||||||||||||||||||||||||
1405 | } | - | ||||||||||||||||||||||||
1406 | - | |||||||||||||||||||||||||
1407 | }else{ | - | ||||||||||||||||||||||||
1408 | assert( *pbDone==0 ); | - | ||||||||||||||||||||||||
1409 | } | - | ||||||||||||||||||||||||
1410 | - | |||||||||||||||||||||||||
1411 | sqlite3ValueFree(p1); | - | ||||||||||||||||||||||||
1412 | sqlite3ValueFree(p2); | - | ||||||||||||||||||||||||
1413 | sqlite3ValueFree(pVal); | - | ||||||||||||||||||||||||
1414 | - | |||||||||||||||||||||||||
1415 | return rc; | - | ||||||||||||||||||||||||
1416 | } | - | ||||||||||||||||||||||||
1417 | #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ | - | ||||||||||||||||||||||||
1418 | - | |||||||||||||||||||||||||
1419 | /* | - | ||||||||||||||||||||||||
1420 | ** This function is used to estimate the number of rows that will be visited | - | ||||||||||||||||||||||||
1421 | ** by scanning an index for a range of values. The range may have an upper | - | ||||||||||||||||||||||||
1422 | ** bound, a lower bound, or both. The WHERE clause terms that set the upper | - | ||||||||||||||||||||||||
1423 | ** and lower bounds are represented by pLower and pUpper respectively. For | - | ||||||||||||||||||||||||
1424 | ** example, assuming that index p is on t1(a): | - | ||||||||||||||||||||||||
1425 | ** | - | ||||||||||||||||||||||||
1426 | ** ... FROM t1 WHERE a > ? AND a < ? ... | - | ||||||||||||||||||||||||
1427 | ** |_____| |_____| | - | ||||||||||||||||||||||||
1428 | ** | | | - | ||||||||||||||||||||||||
1429 | ** pLower pUpper | - | ||||||||||||||||||||||||
1430 | ** | - | ||||||||||||||||||||||||
1431 | ** If either of the upper or lower bound is not present, then NULL is passed in | - | ||||||||||||||||||||||||
1432 | ** place of the corresponding WhereTerm. | - | ||||||||||||||||||||||||
1433 | ** | - | ||||||||||||||||||||||||
1434 | ** The value in (pBuilder->pNew->u.btree.nEq) is the number of the index | - | ||||||||||||||||||||||||
1435 | ** column subject to the range constraint. Or, equivalently, the number of | - | ||||||||||||||||||||||||
1436 | ** equality constraints optimized by the proposed index scan. For example, | - | ||||||||||||||||||||||||
1437 | ** assuming index p is on t1(a, b), and the SQL query is: | - | ||||||||||||||||||||||||
1438 | ** | - | ||||||||||||||||||||||||
1439 | ** ... FROM t1 WHERE a = ? AND b > ? AND b < ? ... | - | ||||||||||||||||||||||||
1440 | ** | - | ||||||||||||||||||||||||
1441 | ** then nEq is set to 1 (as the range restricted column, b, is the second | - | ||||||||||||||||||||||||
1442 | ** left-most column of the index). Or, if the query is: | - | ||||||||||||||||||||||||
1443 | ** | - | ||||||||||||||||||||||||
1444 | ** ... FROM t1 WHERE a > ? AND a < ? ... | - | ||||||||||||||||||||||||
1445 | ** | - | ||||||||||||||||||||||||
1446 | ** then nEq is set to 0. | - | ||||||||||||||||||||||||
1447 | ** | - | ||||||||||||||||||||||||
1448 | ** When this function is called, *pnOut is set to the sqlite3LogEst() of the | - | ||||||||||||||||||||||||
1449 | ** number of rows that the index scan is expected to visit without | - | ||||||||||||||||||||||||
1450 | ** considering the range constraints. If nEq is 0, then *pnOut is the number of | - | ||||||||||||||||||||||||
1451 | ** rows in the index. Assuming no error occurs, *pnOut is adjusted (reduced) | - | ||||||||||||||||||||||||
1452 | ** to account for the range constraints pLower and pUpper. | - | ||||||||||||||||||||||||
1453 | ** | - | ||||||||||||||||||||||||
1454 | ** In the absence of sqlite_stat4 ANALYZE data, or if such data cannot be | - | ||||||||||||||||||||||||
1455 | ** used, a single range inequality reduces the search space by a factor of 4. | - | ||||||||||||||||||||||||
1456 | ** and a pair of constraints (x>? AND x<?) reduces the expected number of | - | ||||||||||||||||||||||||
1457 | ** rows visited by a factor of 64. | - | ||||||||||||||||||||||||
1458 | */ | - | ||||||||||||||||||||||||
1459 | static int whereRangeScanEst( | - | ||||||||||||||||||||||||
1460 | Parse *pParse, /* Parsing & code generating context */ | - | ||||||||||||||||||||||||
1461 | WhereLoopBuilder *pBuilder, | - | ||||||||||||||||||||||||
1462 | WhereTerm *pLower, /* Lower bound on the range. ex: "x>123" Might be NULL */ | - | ||||||||||||||||||||||||
1463 | WhereTerm *pUpper, /* Upper bound on the range. ex: "x<455" Might be NULL */ | - | ||||||||||||||||||||||||
1464 | WhereLoop *pLoop /* Modify the .nOut and maybe .rRun fields */ | - | ||||||||||||||||||||||||
1465 | ){ | - | ||||||||||||||||||||||||
1466 | int rc = SQLITE_OK; | - | ||||||||||||||||||||||||
1467 | int nOut = pLoop->nOut; | - | ||||||||||||||||||||||||
1468 | LogEst nNew; | - | ||||||||||||||||||||||||
1469 | - | |||||||||||||||||||||||||
1470 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 | - | ||||||||||||||||||||||||
1471 | Index *p = pLoop->u.btree.pIndex; | - | ||||||||||||||||||||||||
1472 | int nEq = pLoop->u.btree.nEq; | - | ||||||||||||||||||||||||
1473 | - | |||||||||||||||||||||||||
1474 | if( p->nSample>0 && nEq<p->nSampleCol | - | ||||||||||||||||||||||||
1475 | && OptimizationEnabled(pParse->db, SQLITE_Stat34) | - | ||||||||||||||||||||||||
1476 | ){ | - | ||||||||||||||||||||||||
1477 | if( nEq==pBuilder->nRecValid ){ | - | ||||||||||||||||||||||||
1478 | UnpackedRecord *pRec = pBuilder->pRec; | - | ||||||||||||||||||||||||
1479 | tRowcnt a[2]; | - | ||||||||||||||||||||||||
1480 | int nBtm = pLoop->u.btree.nBtm; | - | ||||||||||||||||||||||||
1481 | int nTop = pLoop->u.btree.nTop; | - | ||||||||||||||||||||||||
1482 | - | |||||||||||||||||||||||||
1483 | /* Variable iLower will be set to the estimate of the number of rows in | - | ||||||||||||||||||||||||
1484 | ** the index that are less than the lower bound of the range query. The | - | ||||||||||||||||||||||||
1485 | ** lower bound being the concatenation of $P and $L, where $P is the | - | ||||||||||||||||||||||||
1486 | ** key-prefix formed by the nEq values matched against the nEq left-most | - | ||||||||||||||||||||||||
1487 | ** columns of the index, and $L is the value in pLower. | - | ||||||||||||||||||||||||
1488 | ** | - | ||||||||||||||||||||||||
1489 | ** Or, if pLower is NULL or $L cannot be extracted from it (because it | - | ||||||||||||||||||||||||
1490 | ** is not a simple variable or literal value), the lower bound of the | - | ||||||||||||||||||||||||
1491 | ** range is $P. Due to a quirk in the way whereKeyStats() works, even | - | ||||||||||||||||||||||||
1492 | ** if $L is available, whereKeyStats() is called for both ($P) and | - | ||||||||||||||||||||||||
1493 | ** ($P:$L) and the larger of the two returned values is used. | - | ||||||||||||||||||||||||
1494 | ** | - | ||||||||||||||||||||||||
1495 | ** Similarly, iUpper is to be set to the estimate of the number of rows | - | ||||||||||||||||||||||||
1496 | ** less than the upper bound of the range query. Where the upper bound | - | ||||||||||||||||||||||||
1497 | ** is either ($P) or ($P:$U). Again, even if $U is available, both values | - | ||||||||||||||||||||||||
1498 | ** of iUpper are requested of whereKeyStats() and the smaller used. | - | ||||||||||||||||||||||||
1499 | ** | - | ||||||||||||||||||||||||
1500 | ** The number of rows between the two bounds is then just iUpper-iLower. | - | ||||||||||||||||||||||||
1501 | */ | - | ||||||||||||||||||||||||
1502 | tRowcnt iLower; /* Rows less than the lower bound */ | - | ||||||||||||||||||||||||
1503 | tRowcnt iUpper; /* Rows less than the upper bound */ | - | ||||||||||||||||||||||||
1504 | int iLwrIdx = -2; /* aSample[] for the lower bound */ | - | ||||||||||||||||||||||||
1505 | int iUprIdx = -1; /* aSample[] for the upper bound */ | - | ||||||||||||||||||||||||
1506 | - | |||||||||||||||||||||||||
1507 | if( pRec ){ | - | ||||||||||||||||||||||||
1508 | testcase( pRec->nField!=pBuilder->nRecValid ); | - | ||||||||||||||||||||||||
1509 | pRec->nField = pBuilder->nRecValid; | - | ||||||||||||||||||||||||
1510 | } | - | ||||||||||||||||||||||||
1511 | /* Determine iLower and iUpper using ($P) only. */ | - | ||||||||||||||||||||||||
1512 | if( nEq==0 ){ | - | ||||||||||||||||||||||||
1513 | iLower = 0; | - | ||||||||||||||||||||||||
1514 | iUpper = p->nRowEst0; | - | ||||||||||||||||||||||||
1515 | }else{ | - | ||||||||||||||||||||||||
1516 | /* Note: this call could be optimized away - since the same values must | - | ||||||||||||||||||||||||
1517 | ** have been requested when testing key $P in whereEqualScanEst(). */ | - | ||||||||||||||||||||||||
1518 | whereKeyStats(pParse, p, pRec, 0, a); | - | ||||||||||||||||||||||||
1519 | iLower = a[0]; | - | ||||||||||||||||||||||||
1520 | iUpper = a[0] + a[1]; | - | ||||||||||||||||||||||||
1521 | } | - | ||||||||||||||||||||||||
1522 | - | |||||||||||||||||||||||||
1523 | assert( pLower==0 || (pLower->eOperator & (WO_GT|WO_GE))!=0 ); | - | ||||||||||||||||||||||||
1524 | assert( pUpper==0 || (pUpper->eOperator & (WO_LT|WO_LE))!=0 ); | - | ||||||||||||||||||||||||
1525 | assert( p->aSortOrder!=0 ); | - | ||||||||||||||||||||||||
1526 | if( p->aSortOrder[nEq] ){ | - | ||||||||||||||||||||||||
1527 | /* The roles of pLower and pUpper are swapped for a DESC index */ | - | ||||||||||||||||||||||||
1528 | SWAP(WhereTerm*, pLower, pUpper); | - | ||||||||||||||||||||||||
1529 | SWAP(int, nBtm, nTop); | - | ||||||||||||||||||||||||
1530 | } | - | ||||||||||||||||||||||||
1531 | - | |||||||||||||||||||||||||
1532 | /* If possible, improve on the iLower estimate using ($P:$L). */ | - | ||||||||||||||||||||||||
1533 | if( pLower ){ | - | ||||||||||||||||||||||||
1534 | int n; /* Values extracted from pExpr */ | - | ||||||||||||||||||||||||
1535 | Expr *pExpr = pLower->pExpr->pRight; | - | ||||||||||||||||||||||||
1536 | rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, nBtm, nEq, &n); | - | ||||||||||||||||||||||||
1537 | if( rc==SQLITE_OK && n ){ | - | ||||||||||||||||||||||||
1538 | tRowcnt iNew; | - | ||||||||||||||||||||||||
1539 | u16 mask = WO_GT|WO_LE; | - | ||||||||||||||||||||||||
1540 | if( sqlite3ExprVectorSize(pExpr)>n ) mask = (WO_LE|WO_LT); | - | ||||||||||||||||||||||||
1541 | iLwrIdx = whereKeyStats(pParse, p, pRec, 0, a); | - | ||||||||||||||||||||||||
1542 | iNew = a[0] + ((pLower->eOperator & mask) ? a[1] : 0); | - | ||||||||||||||||||||||||
1543 | if( iNew>iLower ) iLower = iNew; | - | ||||||||||||||||||||||||
1544 | nOut--; | - | ||||||||||||||||||||||||
1545 | pLower = 0; | - | ||||||||||||||||||||||||
1546 | } | - | ||||||||||||||||||||||||
1547 | } | - | ||||||||||||||||||||||||
1548 | - | |||||||||||||||||||||||||
1549 | /* If possible, improve on the iUpper estimate using ($P:$U). */ | - | ||||||||||||||||||||||||
1550 | if( pUpper ){ | - | ||||||||||||||||||||||||
1551 | int n; /* Values extracted from pExpr */ | - | ||||||||||||||||||||||||
1552 | Expr *pExpr = pUpper->pExpr->pRight; | - | ||||||||||||||||||||||||
1553 | rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, nTop, nEq, &n); | - | ||||||||||||||||||||||||
1554 | if( rc==SQLITE_OK && n ){ | - | ||||||||||||||||||||||||
1555 | tRowcnt iNew; | - | ||||||||||||||||||||||||
1556 | u16 mask = WO_GT|WO_LE; | - | ||||||||||||||||||||||||
1557 | if( sqlite3ExprVectorSize(pExpr)>n ) mask = (WO_LE|WO_LT); | - | ||||||||||||||||||||||||
1558 | iUprIdx = whereKeyStats(pParse, p, pRec, 1, a); | - | ||||||||||||||||||||||||
1559 | iNew = a[0] + ((pUpper->eOperator & mask) ? a[1] : 0); | - | ||||||||||||||||||||||||
1560 | if( iNew<iUpper ) iUpper = iNew; | - | ||||||||||||||||||||||||
1561 | nOut--; | - | ||||||||||||||||||||||||
1562 | pUpper = 0; | - | ||||||||||||||||||||||||
1563 | } | - | ||||||||||||||||||||||||
1564 | } | - | ||||||||||||||||||||||||
1565 | - | |||||||||||||||||||||||||
1566 | pBuilder->pRec = pRec; | - | ||||||||||||||||||||||||
1567 | if( rc==SQLITE_OK ){ | - | ||||||||||||||||||||||||
1568 | if( iUpper>iLower ){ | - | ||||||||||||||||||||||||
1569 | nNew = sqlite3LogEst(iUpper - iLower); | - | ||||||||||||||||||||||||
1570 | /* TUNING: If both iUpper and iLower are derived from the same | - | ||||||||||||||||||||||||
1571 | ** sample, then assume they are 4x more selective. This brings | - | ||||||||||||||||||||||||
1572 | ** the estimated selectivity more in line with what it would be | - | ||||||||||||||||||||||||
1573 | ** if estimated without the use of STAT3/4 tables. */ | - | ||||||||||||||||||||||||
1574 | if( iLwrIdx==iUprIdx ) nNew -= 20; assert( 20==sqlite3LogEst(4) ); | - | ||||||||||||||||||||||||
1575 | }else{ | - | ||||||||||||||||||||||||
1576 | nNew = 10; assert( 10==sqlite3LogEst(2) ); | - | ||||||||||||||||||||||||
1577 | } | - | ||||||||||||||||||||||||
1578 | if( nNew<nOut ){ | - | ||||||||||||||||||||||||
1579 | nOut = nNew; | - | ||||||||||||||||||||||||
1580 | } | - | ||||||||||||||||||||||||
1581 | WHERETRACE(0x10, ("STAT4 range scan: %u..%u est=%d\n", | - | ||||||||||||||||||||||||
1582 | (u32)iLower, (u32)iUpper, nOut)); | - | ||||||||||||||||||||||||
1583 | } | - | ||||||||||||||||||||||||
1584 | }else{ | - | ||||||||||||||||||||||||
1585 | int bDone = 0; | - | ||||||||||||||||||||||||
1586 | rc = whereRangeSkipScanEst(pParse, pLower, pUpper, pLoop, &bDone); | - | ||||||||||||||||||||||||
1587 | if( bDone ) return rc; | - | ||||||||||||||||||||||||
1588 | } | - | ||||||||||||||||||||||||
1589 | } | - | ||||||||||||||||||||||||
1590 | #else | - | ||||||||||||||||||||||||
1591 | UNUSED_PARAMETER(pParse); | - | ||||||||||||||||||||||||
1592 | UNUSED_PARAMETER(pBuilder); | - | ||||||||||||||||||||||||
1593 | assert( pLower || pUpper ); | - | ||||||||||||||||||||||||
1594 | #endif | - | ||||||||||||||||||||||||
1595 | assert( pUpper==0 || (pUpper->wtFlags & TERM_VNULL)==0 ); | - | ||||||||||||||||||||||||
1596 | nNew = whereRangeAdjust(pLower, nOut); | - | ||||||||||||||||||||||||
1597 | nNew = whereRangeAdjust(pUpper, nNew); | - | ||||||||||||||||||||||||
1598 | - | |||||||||||||||||||||||||
1599 | /* TUNING: If there is both an upper and lower limit and neither limit | - | ||||||||||||||||||||||||
1600 | ** has an application-defined likelihood(), assume the range is | - | ||||||||||||||||||||||||
1601 | ** reduced by an additional 75%. This means that, by default, an open-ended | - | ||||||||||||||||||||||||
1602 | ** range query (e.g. col > ?) is assumed to match 1/4 of the rows in the | - | ||||||||||||||||||||||||
1603 | ** index. While a closed range (e.g. col BETWEEN ? AND ?) is estimated to | - | ||||||||||||||||||||||||
1604 | ** match 1/64 of the index. */ | - | ||||||||||||||||||||||||
1605 | if( pLower && pLower->truthProb>0 && pUpper && pUpper->truthProb>0 ){
| 0-28305 | ||||||||||||||||||||||||
1606 | nNew -= 20; | - | ||||||||||||||||||||||||
1607 | } executed 15241 times by 1 test: end of block Executed by:
| 15241 | ||||||||||||||||||||||||
1608 | - | |||||||||||||||||||||||||
1609 | nOut -= (pLower!=0) + (pUpper!=0); | - | ||||||||||||||||||||||||
1610 | if( nNew<10 ) nNew = 10; executed 2646 times by 1 test: nNew = 10; Executed by:
| 2646-38387 | ||||||||||||||||||||||||
1611 | if( nNew<nOut ) nOut = nNew; executed 40387 times by 1 test: nOut = nNew; Executed by:
| 646-40387 | ||||||||||||||||||||||||
1612 | #if defined(WHERETRACE_ENABLED) | - | ||||||||||||||||||||||||
1613 | if( pLoop->nOut>nOut ){ | - | ||||||||||||||||||||||||
1614 | WHERETRACE(0x10,("Range scan lowers nOut from %d to %d\n", | - | ||||||||||||||||||||||||
1615 | pLoop->nOut, nOut)); | - | ||||||||||||||||||||||||
1616 | } | - | ||||||||||||||||||||||||
1617 | #endif | - | ||||||||||||||||||||||||
1618 | pLoop->nOut = (LogEst)nOut; | - | ||||||||||||||||||||||||
1619 | return rc; executed 41033 times by 1 test: return rc; Executed by:
| 41033 | ||||||||||||||||||||||||
1620 | } | - | ||||||||||||||||||||||||
1621 | - | |||||||||||||||||||||||||
1622 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 | - | ||||||||||||||||||||||||
1623 | /* | - | ||||||||||||||||||||||||
1624 | ** Estimate the number of rows that will be returned based on | - | ||||||||||||||||||||||||
1625 | ** an equality constraint x=VALUE and where that VALUE occurs in | - | ||||||||||||||||||||||||
1626 | ** the histogram data. This only works when x is the left-most | - | ||||||||||||||||||||||||
1627 | ** column of an index and sqlite_stat3 histogram data is available | - | ||||||||||||||||||||||||
1628 | ** for that index. When pExpr==NULL that means the constraint is | - | ||||||||||||||||||||||||
1629 | ** "x IS NULL" instead of "x=VALUE". | - | ||||||||||||||||||||||||
1630 | ** | - | ||||||||||||||||||||||||
1631 | ** Write the estimated row count into *pnRow and return SQLITE_OK. | - | ||||||||||||||||||||||||
1632 | ** If unable to make an estimate, leave *pnRow unchanged and return | - | ||||||||||||||||||||||||
1633 | ** non-zero. | - | ||||||||||||||||||||||||
1634 | ** | - | ||||||||||||||||||||||||
1635 | ** This routine can fail if it is unable to load a collating sequence | - | ||||||||||||||||||||||||
1636 | ** required for string comparison, or if unable to allocate memory | - | ||||||||||||||||||||||||
1637 | ** for a UTF conversion required for comparison. The error is stored | - | ||||||||||||||||||||||||
1638 | ** in the pParse structure. | - | ||||||||||||||||||||||||
1639 | */ | - | ||||||||||||||||||||||||
1640 | static int whereEqualScanEst( | - | ||||||||||||||||||||||||
1641 | Parse *pParse, /* Parsing & code generating context */ | - | ||||||||||||||||||||||||
1642 | WhereLoopBuilder *pBuilder, | - | ||||||||||||||||||||||||
1643 | Expr *pExpr, /* Expression for VALUE in the x=VALUE constraint */ | - | ||||||||||||||||||||||||
1644 | tRowcnt *pnRow /* Write the revised row estimate here */ | - | ||||||||||||||||||||||||
1645 | ){ | - | ||||||||||||||||||||||||
1646 | Index *p = pBuilder->pNew->u.btree.pIndex; | - | ||||||||||||||||||||||||
1647 | int nEq = pBuilder->pNew->u.btree.nEq; | - | ||||||||||||||||||||||||
1648 | UnpackedRecord *pRec = pBuilder->pRec; | - | ||||||||||||||||||||||||
1649 | int rc; /* Subfunction return code */ | - | ||||||||||||||||||||||||
1650 | tRowcnt a[2]; /* Statistics */ | - | ||||||||||||||||||||||||
1651 | int bOk; | - | ||||||||||||||||||||||||
1652 | - | |||||||||||||||||||||||||
1653 | assert( nEq>=1 ); | - | ||||||||||||||||||||||||
1654 | assert( nEq<=p->nColumn ); | - | ||||||||||||||||||||||||
1655 | assert( p->aSample!=0 ); | - | ||||||||||||||||||||||||
1656 | assert( p->nSample>0 ); | - | ||||||||||||||||||||||||
1657 | assert( pBuilder->nRecValid<nEq ); | - | ||||||||||||||||||||||||
1658 | - | |||||||||||||||||||||||||
1659 | /* If values are not available for all fields of the index to the left | - | ||||||||||||||||||||||||
1660 | ** of this one, no estimate can be made. Return SQLITE_NOTFOUND. */ | - | ||||||||||||||||||||||||
1661 | if( pBuilder->nRecValid<(nEq-1) ){ | - | ||||||||||||||||||||||||
1662 | return SQLITE_NOTFOUND; | - | ||||||||||||||||||||||||
1663 | } | - | ||||||||||||||||||||||||
1664 | - | |||||||||||||||||||||||||
1665 | /* This is an optimization only. The call to sqlite3Stat4ProbeSetValue() | - | ||||||||||||||||||||||||
1666 | ** below would return the same value. */ | - | ||||||||||||||||||||||||
1667 | if( nEq>=p->nColumn ){ | - | ||||||||||||||||||||||||
1668 | *pnRow = 1; | - | ||||||||||||||||||||||||
1669 | return SQLITE_OK; | - | ||||||||||||||||||||||||
1670 | } | - | ||||||||||||||||||||||||
1671 | - | |||||||||||||||||||||||||
1672 | rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, 1, nEq-1, &bOk); | - | ||||||||||||||||||||||||
1673 | pBuilder->pRec = pRec; | - | ||||||||||||||||||||||||
1674 | if( rc!=SQLITE_OK ) return rc; | - | ||||||||||||||||||||||||
1675 | if( bOk==0 ) return SQLITE_NOTFOUND; | - | ||||||||||||||||||||||||
1676 | pBuilder->nRecValid = nEq; | - | ||||||||||||||||||||||||
1677 | - | |||||||||||||||||||||||||
1678 | whereKeyStats(pParse, p, pRec, 0, a); | - | ||||||||||||||||||||||||
1679 | WHERETRACE(0x10,("equality scan regions %s(%d): %d\n", | - | ||||||||||||||||||||||||
1680 | p->zName, nEq-1, (int)a[1])); | - | ||||||||||||||||||||||||
1681 | *pnRow = a[1]; | - | ||||||||||||||||||||||||
1682 | - | |||||||||||||||||||||||||
1683 | return rc; | - | ||||||||||||||||||||||||
1684 | } | - | ||||||||||||||||||||||||
1685 | #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ | - | ||||||||||||||||||||||||
1686 | - | |||||||||||||||||||||||||
1687 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 | - | ||||||||||||||||||||||||
1688 | /* | - | ||||||||||||||||||||||||
1689 | ** Estimate the number of rows that will be returned based on | - | ||||||||||||||||||||||||
1690 | ** an IN constraint where the right-hand side of the IN operator | - | ||||||||||||||||||||||||
1691 | ** is a list of values. Example: | - | ||||||||||||||||||||||||
1692 | ** | - | ||||||||||||||||||||||||
1693 | ** WHERE x IN (1,2,3,4) | - | ||||||||||||||||||||||||
1694 | ** | - | ||||||||||||||||||||||||
1695 | ** Write the estimated row count into *pnRow and return SQLITE_OK. | - | ||||||||||||||||||||||||
1696 | ** If unable to make an estimate, leave *pnRow unchanged and return | - | ||||||||||||||||||||||||
1697 | ** non-zero. | - | ||||||||||||||||||||||||
1698 | ** | - | ||||||||||||||||||||||||
1699 | ** This routine can fail if it is unable to load a collating sequence | - | ||||||||||||||||||||||||
1700 | ** required for string comparison, or if unable to allocate memory | - | ||||||||||||||||||||||||
1701 | ** for a UTF conversion required for comparison. The error is stored | - | ||||||||||||||||||||||||
1702 | ** in the pParse structure. | - | ||||||||||||||||||||||||
1703 | */ | - | ||||||||||||||||||||||||
1704 | static int whereInScanEst( | - | ||||||||||||||||||||||||
1705 | Parse *pParse, /* Parsing & code generating context */ | - | ||||||||||||||||||||||||
1706 | WhereLoopBuilder *pBuilder, | - | ||||||||||||||||||||||||
1707 | ExprList *pList, /* The value list on the RHS of "x IN (v1,v2,v3,...)" */ | - | ||||||||||||||||||||||||
1708 | tRowcnt *pnRow /* Write the revised row estimate here */ | - | ||||||||||||||||||||||||
1709 | ){ | - | ||||||||||||||||||||||||
1710 | Index *p = pBuilder->pNew->u.btree.pIndex; | - | ||||||||||||||||||||||||
1711 | i64 nRow0 = sqlite3LogEstToInt(p->aiRowLogEst[0]); | - | ||||||||||||||||||||||||
1712 | int nRecValid = pBuilder->nRecValid; | - | ||||||||||||||||||||||||
1713 | int rc = SQLITE_OK; /* Subfunction return code */ | - | ||||||||||||||||||||||||
1714 | tRowcnt nEst; /* Number of rows for a single term */ | - | ||||||||||||||||||||||||
1715 | tRowcnt nRowEst = 0; /* New estimate of the number of rows */ | - | ||||||||||||||||||||||||
1716 | int i; /* Loop counter */ | - | ||||||||||||||||||||||||
1717 | - | |||||||||||||||||||||||||
1718 | assert( p->aSample!=0 ); | - | ||||||||||||||||||||||||
1719 | for(i=0; rc==SQLITE_OK && i<pList->nExpr; i++){ | - | ||||||||||||||||||||||||
1720 | nEst = nRow0; | - | ||||||||||||||||||||||||
1721 | rc = whereEqualScanEst(pParse, pBuilder, pList->a[i].pExpr, &nEst); | - | ||||||||||||||||||||||||
1722 | nRowEst += nEst; | - | ||||||||||||||||||||||||
1723 | pBuilder->nRecValid = nRecValid; | - | ||||||||||||||||||||||||
1724 | } | - | ||||||||||||||||||||||||
1725 | - | |||||||||||||||||||||||||
1726 | if( rc==SQLITE_OK ){ | - | ||||||||||||||||||||||||
1727 | if( nRowEst > nRow0 ) nRowEst = nRow0; | - | ||||||||||||||||||||||||
1728 | *pnRow = nRowEst; | - | ||||||||||||||||||||||||
1729 | WHERETRACE(0x10,("IN row estimate: est=%d\n", nRowEst)); | - | ||||||||||||||||||||||||
1730 | } | - | ||||||||||||||||||||||||
1731 | assert( pBuilder->nRecValid==nRecValid ); | - | ||||||||||||||||||||||||
1732 | return rc; | - | ||||||||||||||||||||||||
1733 | } | - | ||||||||||||||||||||||||
1734 | #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */ | - | ||||||||||||||||||||||||
1735 | - | |||||||||||||||||||||||||
1736 | - | |||||||||||||||||||||||||
1737 | #ifdef WHERETRACE_ENABLED | - | ||||||||||||||||||||||||
1738 | /* | - | ||||||||||||||||||||||||
1739 | ** Print the content of a WhereTerm object | - | ||||||||||||||||||||||||
1740 | */ | - | ||||||||||||||||||||||||
1741 | static void whereTermPrint(WhereTerm *pTerm, int iTerm){ | - | ||||||||||||||||||||||||
1742 | if( pTerm==0 ){ | - | ||||||||||||||||||||||||
1743 | sqlite3DebugPrintf("TERM-%-3d NULL\n", iTerm); | - | ||||||||||||||||||||||||
1744 | }else{ | - | ||||||||||||||||||||||||
1745 | char zType[4]; | - | ||||||||||||||||||||||||
1746 | char zLeft[50]; | - | ||||||||||||||||||||||||
1747 | memcpy(zType, "...", 4); | - | ||||||||||||||||||||||||
1748 | if( pTerm->wtFlags & TERM_VIRTUAL ) zType[0] = 'V'; | - | ||||||||||||||||||||||||
1749 | if( pTerm->eOperator & WO_EQUIV ) zType[1] = 'E'; | - | ||||||||||||||||||||||||
1750 | if( ExprHasProperty(pTerm->pExpr, EP_FromJoin) ) zType[2] = 'L'; | - | ||||||||||||||||||||||||
1751 | if( pTerm->eOperator & WO_SINGLE ){ | - | ||||||||||||||||||||||||
1752 | sqlite3_snprintf(sizeof(zLeft),zLeft,"left={%d:%d}", | - | ||||||||||||||||||||||||
1753 | pTerm->leftCursor, pTerm->u.leftColumn); | - | ||||||||||||||||||||||||
1754 | }else if( (pTerm->eOperator & WO_OR)!=0 && pTerm->u.pOrInfo!=0 ){ | - | ||||||||||||||||||||||||
1755 | sqlite3_snprintf(sizeof(zLeft),zLeft,"indexable=0x%lld", | - | ||||||||||||||||||||||||
1756 | pTerm->u.pOrInfo->indexable); | - | ||||||||||||||||||||||||
1757 | }else{ | - | ||||||||||||||||||||||||
1758 | sqlite3_snprintf(sizeof(zLeft),zLeft,"left=%d", pTerm->leftCursor); | - | ||||||||||||||||||||||||
1759 | } | - | ||||||||||||||||||||||||
1760 | sqlite3DebugPrintf( | - | ||||||||||||||||||||||||
1761 | "TERM-%-3d %p %s %-12s prob=%-3d op=0x%03x wtFlags=0x%04x", | - | ||||||||||||||||||||||||
1762 | iTerm, pTerm, zType, zLeft, pTerm->truthProb, | - | ||||||||||||||||||||||||
1763 | pTerm->eOperator, pTerm->wtFlags); | - | ||||||||||||||||||||||||
1764 | if( pTerm->iField ){ | - | ||||||||||||||||||||||||
1765 | sqlite3DebugPrintf(" iField=%d\n", pTerm->iField); | - | ||||||||||||||||||||||||
1766 | }else{ | - | ||||||||||||||||||||||||
1767 | sqlite3DebugPrintf("\n"); | - | ||||||||||||||||||||||||
1768 | } | - | ||||||||||||||||||||||||
1769 | sqlite3TreeViewExpr(0, pTerm->pExpr, 0); | - | ||||||||||||||||||||||||
1770 | } | - | ||||||||||||||||||||||||
1771 | } | - | ||||||||||||||||||||||||
1772 | #endif | - | ||||||||||||||||||||||||
1773 | - | |||||||||||||||||||||||||
1774 | #ifdef WHERETRACE_ENABLED | - | ||||||||||||||||||||||||
1775 | /* | - | ||||||||||||||||||||||||
1776 | ** Show the complete content of a WhereClause | - | ||||||||||||||||||||||||
1777 | */ | - | ||||||||||||||||||||||||
1778 | void sqlite3WhereClausePrint(WhereClause *pWC){ | - | ||||||||||||||||||||||||
1779 | int i; | - | ||||||||||||||||||||||||
1780 | for(i=0; i<pWC->nTerm; i++){ | - | ||||||||||||||||||||||||
1781 | whereTermPrint(&pWC->a[i], i); | - | ||||||||||||||||||||||||
1782 | } | - | ||||||||||||||||||||||||
1783 | } | - | ||||||||||||||||||||||||
1784 | #endif | - | ||||||||||||||||||||||||
1785 | - | |||||||||||||||||||||||||
1786 | #ifdef WHERETRACE_ENABLED | - | ||||||||||||||||||||||||
1787 | /* | - | ||||||||||||||||||||||||
1788 | ** Print a WhereLoop object for debugging purposes | - | ||||||||||||||||||||||||
1789 | */ | - | ||||||||||||||||||||||||
1790 | static void whereLoopPrint(WhereLoop *p, WhereClause *pWC){ | - | ||||||||||||||||||||||||
1791 | WhereInfo *pWInfo = pWC->pWInfo; | - | ||||||||||||||||||||||||
1792 | int nb = 1+(pWInfo->pTabList->nSrc+3)/4; | - | ||||||||||||||||||||||||
1793 | struct SrcList_item *pItem = pWInfo->pTabList->a + p->iTab; | - | ||||||||||||||||||||||||
1794 | Table *pTab = pItem->pTab; | - | ||||||||||||||||||||||||
1795 | Bitmask mAll = (((Bitmask)1)<<(nb*4)) - 1; | - | ||||||||||||||||||||||||
1796 | sqlite3DebugPrintf("%c%2d.%0*llx.%0*llx", p->cId, | - | ||||||||||||||||||||||||
1797 | p->iTab, nb, p->maskSelf, nb, p->prereq & mAll); | - | ||||||||||||||||||||||||
1798 | sqlite3DebugPrintf(" %12s", | - | ||||||||||||||||||||||||
1799 | pItem->zAlias ? pItem->zAlias : pTab->zName); | - | ||||||||||||||||||||||||
1800 | if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){ | - | ||||||||||||||||||||||||
1801 | const char *zName; | - | ||||||||||||||||||||||||
1802 | if( p->u.btree.pIndex && (zName = p->u.btree.pIndex->zName)!=0 ){ | - | ||||||||||||||||||||||||
1803 | if( strncmp(zName, "sqlite_autoindex_", 17)==0 ){ | - | ||||||||||||||||||||||||
1804 | int i = sqlite3Strlen30(zName) - 1; | - | ||||||||||||||||||||||||
1805 | while( zName[i]!='_' ) i--; | - | ||||||||||||||||||||||||
1806 | zName += i; | - | ||||||||||||||||||||||||
1807 | } | - | ||||||||||||||||||||||||
1808 | sqlite3DebugPrintf(".%-16s %2d", zName, p->u.btree.nEq); | - | ||||||||||||||||||||||||
1809 | }else{ | - | ||||||||||||||||||||||||
1810 | sqlite3DebugPrintf("%20s",""); | - | ||||||||||||||||||||||||
1811 | } | - | ||||||||||||||||||||||||
1812 | }else{ | - | ||||||||||||||||||||||||
1813 | char *z; | - | ||||||||||||||||||||||||
1814 | if( p->u.vtab.idxStr ){ | - | ||||||||||||||||||||||||
1815 | z = sqlite3_mprintf("(%d,\"%s\",%x)", | - | ||||||||||||||||||||||||
1816 | p->u.vtab.idxNum, p->u.vtab.idxStr, p->u.vtab.omitMask); | - | ||||||||||||||||||||||||
1817 | }else{ | - | ||||||||||||||||||||||||
1818 | z = sqlite3_mprintf("(%d,%x)", p->u.vtab.idxNum, p->u.vtab.omitMask); | - | ||||||||||||||||||||||||
1819 | } | - | ||||||||||||||||||||||||
1820 | sqlite3DebugPrintf(" %-19s", z); | - | ||||||||||||||||||||||||
1821 | sqlite3_free(z); | - | ||||||||||||||||||||||||
1822 | } | - | ||||||||||||||||||||||||
1823 | if( p->wsFlags & WHERE_SKIPSCAN ){ | - | ||||||||||||||||||||||||
1824 | sqlite3DebugPrintf(" f %05x %d-%d", p->wsFlags, p->nLTerm,p->nSkip); | - | ||||||||||||||||||||||||
1825 | }else{ | - | ||||||||||||||||||||||||
1826 | sqlite3DebugPrintf(" f %05x N %d", p->wsFlags, p->nLTerm); | - | ||||||||||||||||||||||||
1827 | } | - | ||||||||||||||||||||||||
1828 | sqlite3DebugPrintf(" cost %d,%d,%d\n", p->rSetup, p->rRun, p->nOut); | - | ||||||||||||||||||||||||
1829 | if( p->nLTerm && (sqlite3WhereTrace & 0x100)!=0 ){ | - | ||||||||||||||||||||||||
1830 | int i; | - | ||||||||||||||||||||||||
1831 | for(i=0; i<p->nLTerm; i++){ | - | ||||||||||||||||||||||||
1832 | whereTermPrint(p->aLTerm[i], i); | - | ||||||||||||||||||||||||
1833 | } | - | ||||||||||||||||||||||||
1834 | } | - | ||||||||||||||||||||||||
1835 | } | - | ||||||||||||||||||||||||
1836 | #endif | - | ||||||||||||||||||||||||
1837 | - | |||||||||||||||||||||||||
1838 | /* | - | ||||||||||||||||||||||||
1839 | ** Convert bulk memory into a valid WhereLoop that can be passed | - | ||||||||||||||||||||||||
1840 | ** to whereLoopClear harmlessly. | - | ||||||||||||||||||||||||
1841 | */ | - | ||||||||||||||||||||||||
1842 | static void whereLoopInit(WhereLoop *p){ | - | ||||||||||||||||||||||||
1843 | p->aLTerm = p->aLTermSpace; | - | ||||||||||||||||||||||||
1844 | p->nLTerm = 0; | - | ||||||||||||||||||||||||
1845 | p->nLSlot = ArraySize(p->aLTermSpace); | - | ||||||||||||||||||||||||
1846 | p->wsFlags = 0; | - | ||||||||||||||||||||||||
1847 | } executed 1572403 times by 435 tests: end of block Executed by:
| 1572403 | ||||||||||||||||||||||||
1848 | - | |||||||||||||||||||||||||
1849 | /* | - | ||||||||||||||||||||||||
1850 | ** Clear the WhereLoop.u union. Leave WhereLoop.pLTerm intact. | - | ||||||||||||||||||||||||
1851 | */ | - | ||||||||||||||||||||||||
1852 | static void whereLoopClearUnion(sqlite3 *db, WhereLoop *p){ | - | ||||||||||||||||||||||||
1853 | if( p->wsFlags & (WHERE_VIRTUALTABLE|WHERE_AUTO_INDEX) ){
| 73157-910094 | ||||||||||||||||||||||||
1854 | if( (p->wsFlags & WHERE_VIRTUALTABLE)!=0 && p->u.vtab.needFree ){
| 24-50684 | ||||||||||||||||||||||||
1855 | sqlite3_free(p->u.vtab.idxStr); | - | ||||||||||||||||||||||||
1856 | p->u.vtab.needFree = 0; | - | ||||||||||||||||||||||||
1857 | p->u.vtab.idxStr = 0; | - | ||||||||||||||||||||||||
1858 | }else if( (p->wsFlags & WHERE_AUTO_INDEX)!=0 && p->u.btree.pIndex!=0 ){ executed 24 times by 1 test: end of block Executed by:
| 24-50684 | ||||||||||||||||||||||||
1859 | sqlite3DbFree(db, p->u.btree.pIndex->zColAff); | - | ||||||||||||||||||||||||
1860 | sqlite3DbFreeNN(db, p->u.btree.pIndex); | - | ||||||||||||||||||||||||
1861 | p->u.btree.pIndex = 0; | - | ||||||||||||||||||||||||
1862 | } executed 2859 times by 1 test: end of block Executed by:
| 2859 | ||||||||||||||||||||||||
1863 | } executed 73157 times by 33 tests: end of block Executed by:
| 73157 | ||||||||||||||||||||||||
1864 | } executed 983251 times by 435 tests: end of block Executed by:
| 983251 | ||||||||||||||||||||||||
1865 | - | |||||||||||||||||||||||||
1866 | /* | - | ||||||||||||||||||||||||
1867 | ** Deallocate internal memory used by a WhereLoop object | - | ||||||||||||||||||||||||
1868 | */ | - | ||||||||||||||||||||||||
1869 | static void whereLoopClear(sqlite3 *db, WhereLoop *p){ | - | ||||||||||||||||||||||||
1870 | if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFreeNN(db, p->aLTerm); executed 213 times by 1 test: sqlite3DbFreeNN(db, p->aLTerm); Executed by:
| 213-619731 | ||||||||||||||||||||||||
1871 | whereLoopClearUnion(db, p); | - | ||||||||||||||||||||||||
1872 | whereLoopInit(p); | - | ||||||||||||||||||||||||
1873 | } executed 619944 times by 435 tests: end of block Executed by:
| 619944 | ||||||||||||||||||||||||
1874 | - | |||||||||||||||||||||||||
1875 | /* | - | ||||||||||||||||||||||||
1876 | ** Increase the memory allocation for pLoop->aLTerm[] to be at least n. | - | ||||||||||||||||||||||||
1877 | */ | - | ||||||||||||||||||||||||
1878 | static int whereLoopResize(sqlite3 *db, WhereLoop *p, int n){ | - | ||||||||||||||||||||||||
1879 | WhereTerm **paNew; | - | ||||||||||||||||||||||||
1880 | if( p->nLSlot>=n ) return SQLITE_OK; executed 467794 times by 435 tests: return 0; Executed by:
| 241-467794 | ||||||||||||||||||||||||
1881 | n = (n+7)&~7; | - | ||||||||||||||||||||||||
1882 | paNew = sqlite3DbMallocRawNN(db, sizeof(p->aLTerm[0])*n); | - | ||||||||||||||||||||||||
1883 | if( paNew==0 ) return SQLITE_NOMEM_BKPT; executed 4 times by 1 test: return 7; Executed by:
| 4-237 | ||||||||||||||||||||||||
1884 | memcpy(paNew, p->aLTerm, sizeof(p->aLTerm[0])*p->nLSlot); | - | ||||||||||||||||||||||||
1885 | if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFreeNN(db, p->aLTerm); executed 24 times by 1 test: sqlite3DbFreeNN(db, p->aLTerm); Executed by:
| 24-213 | ||||||||||||||||||||||||
1886 | p->aLTerm = paNew; | - | ||||||||||||||||||||||||
1887 | p->nLSlot = n; | - | ||||||||||||||||||||||||
1888 | return SQLITE_OK; executed 237 times by 1 test: return 0; Executed by:
| 237 | ||||||||||||||||||||||||
1889 | } | - | ||||||||||||||||||||||||
1890 | - | |||||||||||||||||||||||||
1891 | /* | - | ||||||||||||||||||||||||
1892 | ** Transfer content from the second pLoop into the first. | - | ||||||||||||||||||||||||
1893 | */ | - | ||||||||||||||||||||||||
1894 | static int whereLoopXfer(sqlite3 *db, WhereLoop *pTo, WhereLoop *pFrom){ | - | ||||||||||||||||||||||||
1895 | whereLoopClearUnion(db, pTo); | - | ||||||||||||||||||||||||
1896 | if( whereLoopResize(db, pTo, pFrom->nLTerm) ){
| 2-363305 | ||||||||||||||||||||||||
1897 | memset(&pTo->u, 0, sizeof(pTo->u)); | - | ||||||||||||||||||||||||
1898 | return SQLITE_NOMEM_BKPT; executed 2 times by 1 test: return 7; Executed by:
| 2 | ||||||||||||||||||||||||
1899 | } | - | ||||||||||||||||||||||||
1900 | memcpy(pTo, pFrom, WHERE_LOOP_XFER_SZ); | - | ||||||||||||||||||||||||
1901 | memcpy(pTo->aLTerm, pFrom->aLTerm, pTo->nLTerm*sizeof(pTo->aLTerm[0])); | - | ||||||||||||||||||||||||
1902 | if( pFrom->wsFlags & WHERE_VIRTUALTABLE ){
| 11782-351523 | ||||||||||||||||||||||||
1903 | pFrom->u.vtab.needFree = 0; | - | ||||||||||||||||||||||||
1904 | }else if( (pFrom->wsFlags & WHERE_AUTO_INDEX)!=0 ){ executed 11782 times by 1 test: end of block Executed by:
| 11782-300853 | ||||||||||||||||||||||||
1905 | pFrom->u.btree.pIndex = 0; | - | ||||||||||||||||||||||||
1906 | } executed 50670 times by 33 tests: end of block Executed by:
| 50670 | ||||||||||||||||||||||||
1907 | return SQLITE_OK; executed 363305 times by 435 tests: return 0; Executed by:
| 363305 | ||||||||||||||||||||||||
1908 | } | - | ||||||||||||||||||||||||
1909 | - | |||||||||||||||||||||||||
1910 | /* | - | ||||||||||||||||||||||||
1911 | ** Delete a WhereLoop object | - | ||||||||||||||||||||||||
1912 | */ | - | ||||||||||||||||||||||||
1913 | static void whereLoopDelete(sqlite3 *db, WhereLoop *p){ | - | ||||||||||||||||||||||||
1914 | whereLoopClear(db, p); | - | ||||||||||||||||||||||||
1915 | sqlite3DbFreeNN(db, p); | - | ||||||||||||||||||||||||
1916 | } executed 314590 times by 435 tests: end of block Executed by:
| 314590 | ||||||||||||||||||||||||
1917 | - | |||||||||||||||||||||||||
1918 | /* | - | ||||||||||||||||||||||||
1919 | ** Free a WhereInfo structure | - | ||||||||||||||||||||||||
1920 | */ | - | ||||||||||||||||||||||||
1921 | static void whereInfoFree(sqlite3 *db, WhereInfo *pWInfo){ | - | ||||||||||||||||||||||||
1922 | int i; | - | ||||||||||||||||||||||||
1923 | assert( pWInfo!=0 ); | - | ||||||||||||||||||||||||
1924 | for(i=0; i<pWInfo->nLevel; i++){
| 279346-332515 | ||||||||||||||||||||||||
1925 | WhereLevel *pLevel = &pWInfo->a[i]; | - | ||||||||||||||||||||||||
1926 | if( pLevel->pWLoop && (pLevel->pWLoop->wsFlags & WHERE_IN_ABLE) ){
| 679-278563 | ||||||||||||||||||||||||
1927 | sqlite3DbFree(db, pLevel->u.in.aInLoop); | - | ||||||||||||||||||||||||
1928 | } executed 679 times by 1 test: end of block Executed by:
| 679 | ||||||||||||||||||||||||
1929 | } executed 279346 times by 435 tests: end of block Executed by:
| 279346 | ||||||||||||||||||||||||
1930 | sqlite3WhereClauseClear(&pWInfo->sWC); | - | ||||||||||||||||||||||||
1931 | while( pWInfo->pLoops ){
| 311353-332515 | ||||||||||||||||||||||||
1932 | WhereLoop *p = pWInfo->pLoops; | - | ||||||||||||||||||||||||
1933 | pWInfo->pLoops = p->pNextLoop; | - | ||||||||||||||||||||||||
1934 | whereLoopDelete(db, p); | - | ||||||||||||||||||||||||
1935 | } executed 311353 times by 435 tests: end of block Executed by:
| 311353 | ||||||||||||||||||||||||
1936 | sqlite3DbFreeNN(db, pWInfo); | - | ||||||||||||||||||||||||
1937 | } executed 332515 times by 435 tests: end of block Executed by:
| 332515 | ||||||||||||||||||||||||
1938 | - | |||||||||||||||||||||||||
1939 | /* | - | ||||||||||||||||||||||||
1940 | ** Return TRUE if all of the following are true: | - | ||||||||||||||||||||||||
1941 | ** | - | ||||||||||||||||||||||||
1942 | ** (1) X has the same or lower cost that Y | - | ||||||||||||||||||||||||
1943 | ** (2) X uses fewer WHERE clause terms than Y | - | ||||||||||||||||||||||||
1944 | ** (3) Every WHERE clause term used by X is also used by Y | - | ||||||||||||||||||||||||
1945 | ** (4) X skips at least as many columns as Y | - | ||||||||||||||||||||||||
1946 | ** (5) If X is a covering index, than Y is too | - | ||||||||||||||||||||||||
1947 | ** | - | ||||||||||||||||||||||||
1948 | ** Conditions (2) and (3) mean that X is a "proper subset" of Y. | - | ||||||||||||||||||||||||
1949 | ** If X is a proper subset of Y then Y is a better choice and ought | - | ||||||||||||||||||||||||
1950 | ** to have a lower cost. This routine returns TRUE when that cost | - | ||||||||||||||||||||||||
1951 | ** relationship is inverted and needs to be adjusted. Constraint (4) | - | ||||||||||||||||||||||||
1952 | ** was added because if X uses skip-scan less than Y it still might | - | ||||||||||||||||||||||||
1953 | ** deserve a lower cost even if it is a proper subset of Y. Constraint (5) | - | ||||||||||||||||||||||||
1954 | ** was added because a covering index probably deserves to have a lower cost | - | ||||||||||||||||||||||||
1955 | ** than a non-covering index even if it is a proper subset. | - | ||||||||||||||||||||||||
1956 | */ | - | ||||||||||||||||||||||||
1957 | static int whereLoopCheaperProperSubset( | - | ||||||||||||||||||||||||
1958 | const WhereLoop *pX, /* First WhereLoop to compare */ | - | ||||||||||||||||||||||||
1959 | const WhereLoop *pY /* Compare against this WhereLoop */ | - | ||||||||||||||||||||||||
1960 | ){ | - | ||||||||||||||||||||||||
1961 | int i, j; | - | ||||||||||||||||||||||||
1962 | if( pX->nLTerm-pX->nSkip >= pY->nLTerm-pY->nSkip ){
| 18917-27925 | ||||||||||||||||||||||||
1963 | return 0; /* X is not a subset of Y */ executed 27925 times by 1 test: return 0; Executed by:
| 27925 | ||||||||||||||||||||||||
1964 | } | - | ||||||||||||||||||||||||
1965 | if( pY->nSkip > pX->nSkip ) return 0; executed 35 times by 1 test: return 0; Executed by:
| 35-18882 | ||||||||||||||||||||||||
1966 | if( pX->rRun >= pY->rRun ){
| 1262-17620 | ||||||||||||||||||||||||
1967 | if( pX->rRun > pY->rRun ) return 0; /* X costs more than Y */ executed 15588 times by 1 test: return 0; Executed by:
| 2032-15588 | ||||||||||||||||||||||||
1968 | if( pX->nOut > pY->nOut ) return 0; /* X costs more than Y */ executed 620 times by 1 test: return 0; Executed by:
| 620-1412 | ||||||||||||||||||||||||
1969 | } executed 1412 times by 1 test: end of block Executed by:
| 1412 | ||||||||||||||||||||||||
1970 | for(i=pX->nLTerm-1; i>=0; i--){
| 1692-3108 | ||||||||||||||||||||||||
1971 | if( pX->aLTerm[i]==0 ) continue; executed 1 time by 1 test: continue; Executed by:
| 1-3107 | ||||||||||||||||||||||||
1972 | for(j=pY->nLTerm-1; j>=0; j--){
| 982-6884 | ||||||||||||||||||||||||
1973 | if( pY->aLTerm[j]==pX->aLTerm[i] ) break; executed 2125 times by 1 test: break; Executed by:
| 2125-4759 | ||||||||||||||||||||||||
1974 | } executed 4759 times by 1 test: end of block Executed by:
| 4759 | ||||||||||||||||||||||||
1975 | if( j<0 ) return 0; /* X not a subset of Y since term X[i] not used by Y */ executed 982 times by 1 test: return 0; Executed by:
| 982-2125 | ||||||||||||||||||||||||
1976 | } executed 2125 times by 1 test: end of block Executed by:
| 2125 | ||||||||||||||||||||||||
1977 | if( (pX->wsFlags&WHERE_IDX_ONLY)!=0
| 202-1490 | ||||||||||||||||||||||||
1978 | && (pY->wsFlags&WHERE_IDX_ONLY)==0 ){
| 0-1490 | ||||||||||||||||||||||||
1979 | return 0; /* Constraint (5) */ never executed: return 0; | 0 | ||||||||||||||||||||||||
1980 | } | - | ||||||||||||||||||||||||
1981 | return 1; /* All conditions meet */ executed 1692 times by 1 test: return 1; Executed by:
| 1692 | ||||||||||||||||||||||||
1982 | } | - | ||||||||||||||||||||||||
1983 | - | |||||||||||||||||||||||||
1984 | /* | - | ||||||||||||||||||||||||
1985 | ** Try to adjust the cost of WhereLoop pTemplate upwards or downwards so | - | ||||||||||||||||||||||||
1986 | ** that: | - | ||||||||||||||||||||||||
1987 | ** | - | ||||||||||||||||||||||||
1988 | ** (1) pTemplate costs less than any other WhereLoops that are a proper | - | ||||||||||||||||||||||||
1989 | ** subset of pTemplate | - | ||||||||||||||||||||||||
1990 | ** | - | ||||||||||||||||||||||||
1991 | ** (2) pTemplate costs more than any other WhereLoops for which pTemplate | - | ||||||||||||||||||||||||
1992 | ** is a proper subset. | - | ||||||||||||||||||||||||
1993 | ** | - | ||||||||||||||||||||||||
1994 | ** To say "WhereLoop X is a proper subset of Y" means that X uses fewer | - | ||||||||||||||||||||||||
1995 | ** WHERE clause terms than Y and that every WHERE clause term used by X is | - | ||||||||||||||||||||||||
1996 | ** also used by Y. | - | ||||||||||||||||||||||||
1997 | */ | - | ||||||||||||||||||||||||
1998 | static void whereLoopAdjustCost(const WhereLoop *p, WhereLoop *pTemplate){ | - | ||||||||||||||||||||||||
1999 | if( (pTemplate->wsFlags & WHERE_INDEXED)==0 ) return; executed 327987 times by 435 tests: return; Executed by:
| 57699-327987 | ||||||||||||||||||||||||
2000 | for(; p; p=p->pNextLoop){
| 57699-88395 | ||||||||||||||||||||||||
2001 | if( p->iTab!=pTemplate->iTab ) continue; executed 11311 times by 1 test: continue; Executed by:
| 11311-77084 | ||||||||||||||||||||||||
2002 | if( (p->wsFlags & WHERE_INDEXED)==0 ) continue; executed 52819 times by 1 test: continue; Executed by:
| 24265-52819 | ||||||||||||||||||||||||
2003 | if( whereLoopCheaperProperSubset(p, pTemplate) ){
| 1688-22577 | ||||||||||||||||||||||||
2004 | /* Adjust pTemplate cost downward so that it is cheaper than its | - | ||||||||||||||||||||||||
2005 | ** subset p. */ | - | ||||||||||||||||||||||||
2006 | WHERETRACE(0x80,("subset cost adjustment %d,%d to %d,%d\n", | - | ||||||||||||||||||||||||
2007 | pTemplate->rRun, pTemplate->nOut, p->rRun, p->nOut-1)); | - | ||||||||||||||||||||||||
2008 | pTemplate->rRun = p->rRun; | - | ||||||||||||||||||||||||
2009 | pTemplate->nOut = p->nOut - 1; | - | ||||||||||||||||||||||||
2010 | }else if( whereLoopCheaperProperSubset(pTemplate, p) ){ executed 1688 times by 1 test: end of block Executed by:
| 4-22573 | ||||||||||||||||||||||||
2011 | /* Adjust pTemplate cost upward so that it is costlier than p since | - | ||||||||||||||||||||||||
2012 | ** pTemplate is a proper subset of p */ | - | ||||||||||||||||||||||||
2013 | WHERETRACE(0x80,("subset cost adjustment %d,%d to %d,%d\n", | - | ||||||||||||||||||||||||
2014 | pTemplate->rRun, pTemplate->nOut, p->rRun, p->nOut+1)); | - | ||||||||||||||||||||||||
2015 | pTemplate->rRun = p->rRun; | - | ||||||||||||||||||||||||
2016 | pTemplate->nOut = p->nOut + 1; | - | ||||||||||||||||||||||||
2017 | } executed 4 times by 1 test: end of block Executed by:
| 4 | ||||||||||||||||||||||||
2018 | } executed 24265 times by 1 test: end of block Executed by:
| 24265 | ||||||||||||||||||||||||
2019 | } executed 57699 times by 2 tests: end of block Executed by:
| 57699 | ||||||||||||||||||||||||
2020 | - | |||||||||||||||||||||||||
2021 | /* | - | ||||||||||||||||||||||||
2022 | ** Search the list of WhereLoops in *ppPrev looking for one that can be | - | ||||||||||||||||||||||||
2023 | ** replaced by pTemplate. | - | ||||||||||||||||||||||||
2024 | ** | - | ||||||||||||||||||||||||
2025 | ** Return NULL if pTemplate does not belong on the WhereLoop list. | - | ||||||||||||||||||||||||
2026 | ** In other words if pTemplate ought to be dropped from further consideration. | - | ||||||||||||||||||||||||
2027 | ** | - | ||||||||||||||||||||||||
2028 | ** If pX is a WhereLoop that pTemplate can replace, then return the | - | ||||||||||||||||||||||||
2029 | ** link that points to pX. | - | ||||||||||||||||||||||||
2030 | ** | - | ||||||||||||||||||||||||
2031 | ** If pTemplate cannot replace any existing element of the list but needs | - | ||||||||||||||||||||||||
2032 | ** to be added to the list as a new entry, then return a pointer to the | - | ||||||||||||||||||||||||
2033 | ** tail of the list. | - | ||||||||||||||||||||||||
2034 | */ | - | ||||||||||||||||||||||||
2035 | static WhereLoop **whereLoopFindLesser( | - | ||||||||||||||||||||||||
2036 | WhereLoop **ppPrev, | - | ||||||||||||||||||||||||
2037 | const WhereLoop *pTemplate | - | ||||||||||||||||||||||||
2038 | ){ | - | ||||||||||||||||||||||||
2039 | WhereLoop *p; | - | ||||||||||||||||||||||||
2040 | for(p=(*ppPrev); p; ppPrev=&p->pNextLoop, p=*ppPrev){
| 318063-418966 | ||||||||||||||||||||||||
2041 | if( p->iTab!=pTemplate->iTab || p->iSortIdx!=pTemplate->iSortIdx ){
| 39439-262690 | ||||||||||||||||||||||||
2042 | /* If either the iTab or iSortIdx values for two WhereLoop are different | - | ||||||||||||||||||||||||
2043 | ** then those WhereLoops need to be considered separately. Neither is | - | ||||||||||||||||||||||||
2044 | ** a candidate to replace the other. */ | - | ||||||||||||||||||||||||
2045 | continue; executed 302129 times by 31 tests: continue; Executed by:
| 302129 | ||||||||||||||||||||||||
2046 | } | - | ||||||||||||||||||||||||
2047 | /* In the current implementation, the rSetup value is either zero | - | ||||||||||||||||||||||||
2048 | ** or the cost of building an automatic index (NlogN) and the NlogN | - | ||||||||||||||||||||||||
2049 | ** is the same for compatible WhereLoops. */ | - | ||||||||||||||||||||||||
2050 | assert( p->rSetup==0 || pTemplate->rSetup==0 | - | ||||||||||||||||||||||||
2051 | || p->rSetup==pTemplate->rSetup ); | - | ||||||||||||||||||||||||
2052 | - | |||||||||||||||||||||||||
2053 | /* whereLoopAddBtree() always generates and inserts the automatic index | - | ||||||||||||||||||||||||
2054 | ** case first. Hence compatible candidate WhereLoops never have a larger | - | ||||||||||||||||||||||||
2055 | ** rSetup. Call this SETUP-INVARIANT */ | - | ||||||||||||||||||||||||
2056 | assert( p->rSetup>=pTemplate->rSetup ); | - | ||||||||||||||||||||||||
2057 | - | |||||||||||||||||||||||||
2058 | /* Any loop using an appliation-defined index (or PRIMARY KEY or | - | ||||||||||||||||||||||||
2059 | ** UNIQUE constraint) with one or more == constraints is better | - | ||||||||||||||||||||||||
2060 | ** than an automatic index. Unless it is a skip-scan. */ | - | ||||||||||||||||||||||||
2061 | if( (p->wsFlags & WHERE_AUTO_INDEX)!=0
| 50011-66826 | ||||||||||||||||||||||||
2062 | && (pTemplate->nSkip)==0
| 27-49984 | ||||||||||||||||||||||||
2063 | && (pTemplate->wsFlags & WHERE_INDEXED)!=0
| 5584-44400 | ||||||||||||||||||||||||
2064 | && (pTemplate->wsFlags & WHERE_COLUMN_EQ)!=0
| 1139-4445 | ||||||||||||||||||||||||
2065 | && (p->prereq & pTemplate->prereq)==pTemplate->prereq
| 78-4367 | ||||||||||||||||||||||||
2066 | ){ | - | ||||||||||||||||||||||||
2067 | break; executed 4367 times by 1 test: break; Executed by:
| 4367 | ||||||||||||||||||||||||
2068 | } | - | ||||||||||||||||||||||||
2069 | - | |||||||||||||||||||||||||
2070 | /* If existing WhereLoop p is better than pTemplate, pTemplate can be | - | ||||||||||||||||||||||||
2071 | ** discarded. WhereLoop p is better if: | - | ||||||||||||||||||||||||
2072 | ** (1) p has no more dependencies than pTemplate, and | - | ||||||||||||||||||||||||
2073 | ** (2) p has an equal or lower cost than pTemplate | - | ||||||||||||||||||||||||
2074 | */ | - | ||||||||||||||||||||||||
2075 | if( (p->prereq & pTemplate->prereq)==p->prereq /* (1) */
| 10856-101614 | ||||||||||||||||||||||||
2076 | && p->rSetup<=pTemplate->rSetup /* (2a) */
| 25987-75627 | ||||||||||||||||||||||||
2077 | && p->rRun<=pTemplate->rRun /* (2b) */
| 25550-50077 | ||||||||||||||||||||||||
2078 | && p->nOut<=pTemplate->nOut /* (2c) */
| 3225-22325 | ||||||||||||||||||||||||
2079 | ){ | - | ||||||||||||||||||||||||
2080 | return 0; /* Discard pTemplate */ executed 22325 times by 10 tests: return 0; Executed by:
| 22325 | ||||||||||||||||||||||||
2081 | } | - | ||||||||||||||||||||||||
2082 | - | |||||||||||||||||||||||||
2083 | /* If pTemplate is always better than p, then cause p to be overwritten | - | ||||||||||||||||||||||||
2084 | ** with pTemplate. pTemplate is better than p if: | - | ||||||||||||||||||||||||
2085 | ** (1) pTemplate has no more dependences than p, and | - | ||||||||||||||||||||||||
2086 | ** (2) pTemplate has an equal or lower cost than p. | - | ||||||||||||||||||||||||
2087 | */ | - | ||||||||||||||||||||||||
2088 | if( (p->prereq & pTemplate->prereq)==pTemplate->prereq /* (1) */
| 6824-83321 | ||||||||||||||||||||||||
2089 | && p->rRun>=pTemplate->rRun /* (2a) */
| 35674-47647 | ||||||||||||||||||||||||
2090 | && p->nOut>=pTemplate->nOut /* (2b) */
| 60-47587 | ||||||||||||||||||||||||
2091 | ){ | - | ||||||||||||||||||||||||
2092 | assert( p->rSetup>=pTemplate->rSetup ); /* SETUP-INVARIANT above */ | - | ||||||||||||||||||||||||
2093 | break; /* Cause p to be overwritten by pTemplate */ executed 47587 times by 1 test: break; Executed by:
| 47587 | ||||||||||||||||||||||||
2094 | } | - | ||||||||||||||||||||||||
2095 | } executed 42558 times by 10 tests: end of block Executed by:
| 42558 | ||||||||||||||||||||||||
2096 | return ppPrev; executed 370017 times by 435 tests: return ppPrev; Executed by:
| 370017 | ||||||||||||||||||||||||
2097 | } | - | ||||||||||||||||||||||||
2098 | - | |||||||||||||||||||||||||
2099 | /* | - | ||||||||||||||||||||||||
2100 | ** Insert or replace a WhereLoop entry using the template supplied. | - | ||||||||||||||||||||||||
2101 | ** | - | ||||||||||||||||||||||||
2102 | ** An existing WhereLoop entry might be overwritten if the new template | - | ||||||||||||||||||||||||
2103 | ** is better and has fewer dependencies. Or the template will be ignored | - | ||||||||||||||||||||||||
2104 | ** and no insert will occur if an existing WhereLoop is faster and has | - | ||||||||||||||||||||||||
2105 | ** fewer dependencies than the template. Otherwise a new WhereLoop is | - | ||||||||||||||||||||||||
2106 | ** added based on the template. | - | ||||||||||||||||||||||||
2107 | ** | - | ||||||||||||||||||||||||
2108 | ** If pBuilder->pOrSet is not NULL then we care about only the | - | ||||||||||||||||||||||||
2109 | ** prerequisites and rRun and nOut costs of the N best loops. That | - | ||||||||||||||||||||||||
2110 | ** information is gathered in the pBuilder->pOrSet object. This special | - | ||||||||||||||||||||||||
2111 | ** processing mode is used only for OR clause processing. | - | ||||||||||||||||||||||||
2112 | ** | - | ||||||||||||||||||||||||
2113 | ** When accumulating multiple loops (when pBuilder->pOrSet is NULL) we | - | ||||||||||||||||||||||||
2114 | ** still might overwrite similar loops with the new template if the | - | ||||||||||||||||||||||||
2115 | ** new template is better. Loops may be overwritten if the following | - | ||||||||||||||||||||||||
2116 | ** conditions are met: | - | ||||||||||||||||||||||||
2117 | ** | - | ||||||||||||||||||||||||
2118 | ** (1) They have the same iTab. | - | ||||||||||||||||||||||||
2119 | ** (2) They have the same iSortIdx. | - | ||||||||||||||||||||||||
2120 | ** (3) The template has same or fewer dependencies than the current loop | - | ||||||||||||||||||||||||
2121 | ** (4) The template has the same or lower cost than the current loop | - | ||||||||||||||||||||||||
2122 | */ | - | ||||||||||||||||||||||||
2123 | static int whereLoopInsert(WhereLoopBuilder *pBuilder, WhereLoop *pTemplate){ | - | ||||||||||||||||||||||||
2124 | WhereLoop **ppPrev, *p; | - | ||||||||||||||||||||||||
2125 | WhereInfo *pWInfo = pBuilder->pWInfo; | - | ||||||||||||||||||||||||
2126 | sqlite3 *db = pWInfo->pParse->db; | - | ||||||||||||||||||||||||
2127 | int rc; | - | ||||||||||||||||||||||||
2128 | - | |||||||||||||||||||||||||
2129 | /* Stop the search once we hit the query planner search limit */ | - | ||||||||||||||||||||||||
2130 | if( pBuilder->iPlanLimit==0 ){
| 0-447219 | ||||||||||||||||||||||||
2131 | WHERETRACE(0xffffffff,("=== query planner search limit reached ===\n")); | - | ||||||||||||||||||||||||
2132 | if( pBuilder->pOrSet ) pBuilder->pOrSet->n = 0; never executed: pBuilder->pOrSet->n = 0;
| 0 | ||||||||||||||||||||||||
2133 | return SQLITE_DONE; never executed: return 101; | 0 | ||||||||||||||||||||||||
2134 | } | - | ||||||||||||||||||||||||
2135 | pBuilder->iPlanLimit--; | - | ||||||||||||||||||||||||
2136 | - | |||||||||||||||||||||||||
2137 | /* If pBuilder->pOrSet is defined, then only keep track of the costs | - | ||||||||||||||||||||||||
2138 | ** and prereqs. | - | ||||||||||||||||||||||||
2139 | */ | - | ||||||||||||||||||||||||
2140 | if( pBuilder->pOrSet!=0 ){
| 61533-385686 | ||||||||||||||||||||||||
2141 | if( pTemplate->nLTerm ){
| 30341-31192 | ||||||||||||||||||||||||
2142 | #if WHERETRACE_ENABLED | - | ||||||||||||||||||||||||
2143 | u16 n = pBuilder->pOrSet->n; | - | ||||||||||||||||||||||||
2144 | int x = | - | ||||||||||||||||||||||||
2145 | #endif | - | ||||||||||||||||||||||||
2146 | whereOrInsert(pBuilder->pOrSet, pTemplate->prereq, pTemplate->rRun, | - | ||||||||||||||||||||||||
2147 | pTemplate->nOut); | - | ||||||||||||||||||||||||
2148 | #if WHERETRACE_ENABLED /* 0x8 */ | - | ||||||||||||||||||||||||
2149 | if( sqlite3WhereTrace & 0x8 ){ | - | ||||||||||||||||||||||||
2150 | sqlite3DebugPrintf(x?" or-%d: ":" or-X: ", n); | - | ||||||||||||||||||||||||
2151 | whereLoopPrint(pTemplate, pBuilder->pWC); | - | ||||||||||||||||||||||||
2152 | } | - | ||||||||||||||||||||||||
2153 | #endif | - | ||||||||||||||||||||||||
2154 | } executed 30341 times by 1 test: end of block Executed by:
| 30341 | ||||||||||||||||||||||||
2155 | return SQLITE_OK; executed 61533 times by 2 tests: return 0; Executed by:
| 61533 | ||||||||||||||||||||||||
2156 | } | - | ||||||||||||||||||||||||
2157 | - | |||||||||||||||||||||||||
2158 | /* Look for an existing WhereLoop to replace with pTemplate | - | ||||||||||||||||||||||||
2159 | */ | - | ||||||||||||||||||||||||
2160 | whereLoopAdjustCost(pWInfo->pLoops, pTemplate); | - | ||||||||||||||||||||||||
2161 | ppPrev = whereLoopFindLesser(&pWInfo->pLoops, pTemplate); | - | ||||||||||||||||||||||||
2162 | - | |||||||||||||||||||||||||
2163 | if( ppPrev==0 ){
| 22320-363366 | ||||||||||||||||||||||||
2164 | /* There already exists a WhereLoop on the list that is better | - | ||||||||||||||||||||||||
2165 | ** than pTemplate, so just ignore pTemplate */ | - | ||||||||||||||||||||||||
2166 | #if WHERETRACE_ENABLED /* 0x8 */ | - | ||||||||||||||||||||||||
2167 | if( sqlite3WhereTrace & 0x8 ){ | - | ||||||||||||||||||||||||
2168 | sqlite3DebugPrintf(" skip: "); | - | ||||||||||||||||||||||||
2169 | whereLoopPrint(pTemplate, pBuilder->pWC); | - | ||||||||||||||||||||||||
2170 | } | - | ||||||||||||||||||||||||
2171 | #endif | - | ||||||||||||||||||||||||
2172 | return SQLITE_OK; executed 22320 times by 10 tests: return 0; Executed by:
| 22320 | ||||||||||||||||||||||||
2173 | }else{ | - | ||||||||||||||||||||||||
2174 | p = *ppPrev; | - | ||||||||||||||||||||||||
2175 | } executed 363366 times by 435 tests: end of block Executed by:
| 363366 | ||||||||||||||||||||||||
2176 | - | |||||||||||||||||||||||||
2177 | /* If we reach this point it means that either p[] should be overwritten | - | ||||||||||||||||||||||||
2178 | ** with pTemplate[] if p[] exists, or if p==NULL then allocate a new | - | ||||||||||||||||||||||||
2179 | ** WhereLoop and insert it. | - | ||||||||||||||||||||||||
2180 | */ | - | ||||||||||||||||||||||||
2181 | #if WHERETRACE_ENABLED /* 0x8 */ | - | ||||||||||||||||||||||||
2182 | if( sqlite3WhereTrace & 0x8 ){ | - | ||||||||||||||||||||||||
2183 | if( p!=0 ){ | - | ||||||||||||||||||||||||
2184 | sqlite3DebugPrintf("replace: "); | - | ||||||||||||||||||||||||
2185 | whereLoopPrint(p, pBuilder->pWC); | - | ||||||||||||||||||||||||
2186 | sqlite3DebugPrintf(" with: "); | - | ||||||||||||||||||||||||
2187 | }else{ | - | ||||||||||||||||||||||||
2188 | sqlite3DebugPrintf(" add: "); | - | ||||||||||||||||||||||||
2189 | } | - | ||||||||||||||||||||||||
2190 | whereLoopPrint(pTemplate, pBuilder->pWC); | - | ||||||||||||||||||||||||
2191 | } | - | ||||||||||||||||||||||||
2192 | #endif | - | ||||||||||||||||||||||||
2193 | if( p==0 ){
| 48717-314649 | ||||||||||||||||||||||||
2194 | /* Allocate a new WhereLoop to add to the end of the list */ | - | ||||||||||||||||||||||||
2195 | *ppPrev = p = sqlite3DbMallocRawNN(db, sizeof(WhereLoop)); | - | ||||||||||||||||||||||||
2196 | if( p==0 ) return SQLITE_NOMEM_BKPT; executed 59 times by 1 test: return 7; Executed by:
| 59-314590 | ||||||||||||||||||||||||
2197 | whereLoopInit(p); | - | ||||||||||||||||||||||||
2198 | p->pNextLoop = 0; | - | ||||||||||||||||||||||||
2199 | }else{ executed 314590 times by 435 tests: end of block Executed by:
| 314590 | ||||||||||||||||||||||||
2200 | /* We will be overwriting WhereLoop p[]. But before we do, first | - | ||||||||||||||||||||||||
2201 | ** go through the rest of the list and delete any other entries besides | - | ||||||||||||||||||||||||
2202 | ** p[] that are also supplated by pTemplate */ | - | ||||||||||||||||||||||||
2203 | WhereLoop **ppTail = &p->pNextLoop; | - | ||||||||||||||||||||||||
2204 | WhereLoop *pToDel; | - | ||||||||||||||||||||||||
2205 | while( *ppTail ){
| 6656-45298 | ||||||||||||||||||||||||
2206 | ppTail = whereLoopFindLesser(ppTail, pTemplate); | - | ||||||||||||||||||||||||
2207 | if( ppTail==0 ) break; executed 5 times by 1 test: break; Executed by:
| 5-6651 | ||||||||||||||||||||||||
2208 | pToDel = *ppTail; | - | ||||||||||||||||||||||||
2209 | if( pToDel==0 ) break; executed 3414 times by 1 test: break; Executed by:
| 3237-3414 | ||||||||||||||||||||||||
2210 | *ppTail = pToDel->pNextLoop; | - | ||||||||||||||||||||||||
2211 | #if WHERETRACE_ENABLED /* 0x8 */ | - | ||||||||||||||||||||||||
2212 | if( sqlite3WhereTrace & 0x8 ){ | - | ||||||||||||||||||||||||
2213 | sqlite3DebugPrintf(" delete: "); | - | ||||||||||||||||||||||||
2214 | whereLoopPrint(pToDel, pBuilder->pWC); | - | ||||||||||||||||||||||||
2215 | } | - | ||||||||||||||||||||||||
2216 | #endif | - | ||||||||||||||||||||||||
2217 | whereLoopDelete(db, pToDel); | - | ||||||||||||||||||||||||
2218 | } executed 3237 times by 1 test: end of block Executed by:
| 3237 | ||||||||||||||||||||||||
2219 | } executed 48717 times by 1 test: end of block Executed by:
| 48717 | ||||||||||||||||||||||||
2220 | rc = whereLoopXfer(db, p, pTemplate); | - | ||||||||||||||||||||||||
2221 | if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){
| 11782-351525 | ||||||||||||||||||||||||
2222 | Index *pIndex = p->u.btree.pIndex; | - | ||||||||||||||||||||||||
2223 | if( pIndex && pIndex->tnum==0 ){
| 47024-298457 | ||||||||||||||||||||||||
2224 | p->u.btree.pIndex = 0; | - | ||||||||||||||||||||||||
2225 | } executed 251433 times by 435 tests: end of block Executed by:
| 251433 | ||||||||||||||||||||||||
2226 | } executed 351525 times by 435 tests: end of block Executed by:
| 351525 | ||||||||||||||||||||||||
2227 | return rc; executed 363307 times by 435 tests: return rc; Executed by:
| 363307 | ||||||||||||||||||||||||
2228 | } | - | ||||||||||||||||||||||||
2229 | - | |||||||||||||||||||||||||
2230 | /* | - | ||||||||||||||||||||||||
2231 | ** Adjust the WhereLoop.nOut value downward to account for terms of the | - | ||||||||||||||||||||||||
2232 | ** WHERE clause that reference the loop but which are not used by an | - | ||||||||||||||||||||||||
2233 | ** index. | - | ||||||||||||||||||||||||
2234 | * | - | ||||||||||||||||||||||||
2235 | ** For every WHERE clause term that is not used by the index | - | ||||||||||||||||||||||||
2236 | ** and which has a truth probability assigned by one of the likelihood(), | - | ||||||||||||||||||||||||
2237 | ** likely(), or unlikely() SQL functions, reduce the estimated number | - | ||||||||||||||||||||||||
2238 | ** of output rows by the probability specified. | - | ||||||||||||||||||||||||
2239 | ** | - | ||||||||||||||||||||||||
2240 | ** TUNING: For every WHERE clause term that is not used by the index | - | ||||||||||||||||||||||||
2241 | ** and which does not have an assigned truth probability, heuristics | - | ||||||||||||||||||||||||
2242 | ** described below are used to try to estimate the truth probability. | - | ||||||||||||||||||||||||
2243 | ** TODO --> Perhaps this is something that could be improved by better | - | ||||||||||||||||||||||||
2244 | ** table statistics. | - | ||||||||||||||||||||||||
2245 | ** | - | ||||||||||||||||||||||||
2246 | ** Heuristic 1: Estimate the truth probability as 93.75%. The 93.75% | - | ||||||||||||||||||||||||
2247 | ** value corresponds to -1 in LogEst notation, so this means decrement | - | ||||||||||||||||||||||||
2248 | ** the WhereLoop.nOut field for every such WHERE clause term. | - | ||||||||||||||||||||||||
2249 | ** | - | ||||||||||||||||||||||||
2250 | ** Heuristic 2: If there exists one or more WHERE clause terms of the | - | ||||||||||||||||||||||||
2251 | ** form "x==EXPR" and EXPR is not a constant 0 or 1, then make sure the | - | ||||||||||||||||||||||||
2252 | ** final output row estimate is no greater than 1/4 of the total number | - | ||||||||||||||||||||||||
2253 | ** of rows in the table. In other words, assume that x==EXPR will filter | - | ||||||||||||||||||||||||
2254 | ** out at least 3 out of 4 rows. If EXPR is -1 or 0 or 1, then maybe the | - | ||||||||||||||||||||||||
2255 | ** "x" column is boolean or else -1 or 0 or 1 is a common default value | - | ||||||||||||||||||||||||
2256 | ** on the "x" column and so in that case only cap the output row estimate | - | ||||||||||||||||||||||||
2257 | ** at 1/2 instead of 1/4. | - | ||||||||||||||||||||||||
2258 | */ | - | ||||||||||||||||||||||||
2259 | static void whereLoopOutputAdjust( | - | ||||||||||||||||||||||||
2260 | WhereClause *pWC, /* The WHERE clause */ | - | ||||||||||||||||||||||||
2261 | WhereLoop *pLoop, /* The loop to adjust downward */ | - | ||||||||||||||||||||||||
2262 | LogEst nRow /* Number of rows in the entire table */ | - | ||||||||||||||||||||||||
2263 | ){ | - | ||||||||||||||||||||||||
2264 | WhereTerm *pTerm, *pX; | - | ||||||||||||||||||||||||
2265 | Bitmask notAllowed = ~(pLoop->prereq|pLoop->maskSelf); | - | ||||||||||||||||||||||||
2266 | int i, j, k; | - | ||||||||||||||||||||||||
2267 | LogEst iReduce = 0; /* pLoop->nOut should not exceed nRow-iReduce */ | - | ||||||||||||||||||||||||
2268 | - | |||||||||||||||||||||||||
2269 | assert( (pLoop->wsFlags & WHERE_AUTO_INDEX)==0 ); | - | ||||||||||||||||||||||||
2270 | for(i=pWC->nTerm, pTerm=pWC->a; i>0; i--, pTerm++){
| 311256-531583 | ||||||||||||||||||||||||
2271 | if( (pTerm->wtFlags & TERM_VIRTUAL)!=0 ) break; executed 61528 times by 1 test: break; Executed by:
| 61528-470055 | ||||||||||||||||||||||||
2272 | if( (pTerm->prereqAll & pLoop->maskSelf)==0 ) continue; executed 106662 times by 4 tests: continue; Executed by:
| 106662-363393 | ||||||||||||||||||||||||
2273 | if( (pTerm->prereqAll & notAllowed)!=0 ) continue; executed 22004 times by 1 test: continue; Executed by:
| 22004-341389 | ||||||||||||||||||||||||
2274 | for(j=pLoop->nLTerm-1; j>=0; j--){
| 173165-250069 | ||||||||||||||||||||||||
2275 | pX = pLoop->aLTerm[j]; | - | ||||||||||||||||||||||||
2276 | if( pX==0 ) continue; executed 113 times by 1 test: continue; Executed by:
| 113-173052 | ||||||||||||||||||||||||
2277 | if( pX==pTerm ) break; executed 69579 times by 1 test: break; Executed by:
| 69579-103473 | ||||||||||||||||||||||||
2278 | if( pX->iParent>=0 && (&pWC->a[pX->iParent])==pTerm ) break; executed 21741 times by 1 test: break; Executed by:
| 21741-52166 | ||||||||||||||||||||||||
2279 | } executed 81732 times by 1 test: end of block Executed by:
| 81732 | ||||||||||||||||||||||||
2280 | if( j<0 ){
| 91320-250069 | ||||||||||||||||||||||||
2281 | if( pTerm->truthProb<=0 ){
| 156-249913 | ||||||||||||||||||||||||
2282 | /* If a truth probability is specified using the likelihood() hints, | - | ||||||||||||||||||||||||
2283 | ** then use the probability provided by the application. */ | - | ||||||||||||||||||||||||
2284 | pLoop->nOut += pTerm->truthProb; | - | ||||||||||||||||||||||||
2285 | }else{ executed 156 times by 1 test: end of block Executed by:
| 156 | ||||||||||||||||||||||||
2286 | /* In the absence of explicit truth probabilities, use heuristics to | - | ||||||||||||||||||||||||
2287 | ** guess a reasonable truth probability. */ | - | ||||||||||||||||||||||||
2288 | pLoop->nOut--; | - | ||||||||||||||||||||||||
2289 | if( pTerm->eOperator&(WO_EQ|WO_IS) ){
| 105082-144831 | ||||||||||||||||||||||||
2290 | Expr *pRight = pTerm->pExpr->pRight; | - | ||||||||||||||||||||||||
2291 | testcase( pTerm->pExpr->op==TK_IS ); | - | ||||||||||||||||||||||||
2292 | if( sqlite3ExprIsInteger(pRight, &k) && k>=(-1) && k<=1 ){
| 72-56612 | ||||||||||||||||||||||||
2293 | k = 10; | - | ||||||||||||||||||||||||
2294 | }else{ executed 2515 times by 3 tests: end of block Executed by:
| 2515 | ||||||||||||||||||||||||
2295 | k = 20; | - | ||||||||||||||||||||||||
2296 | } executed 102567 times by 31 tests: end of block Executed by:
| 102567 | ||||||||||||||||||||||||
2297 | if( iReduce<k ) iReduce = k; executed 93861 times by 33 tests: iReduce = k; Executed by:
| 11221-93861 | ||||||||||||||||||||||||
2298 | } executed 105082 times by 33 tests: end of block Executed by:
| 105082 | ||||||||||||||||||||||||
2299 | } executed 249913 times by 366 tests: end of block Executed by:
| 249913 | ||||||||||||||||||||||||
2300 | } | - | ||||||||||||||||||||||||
2301 | } executed 341389 times by 366 tests: end of block Executed by:
| 341389 | ||||||||||||||||||||||||
2302 | if( pLoop->nOut > nRow-iReduce ) pLoop->nOut = nRow - iReduce; executed 84354 times by 33 tests: pLoop->nOut = nRow - iReduce; Executed by:
| 84354-288430 | ||||||||||||||||||||||||
2303 | } executed 372784 times by 435 tests: end of block Executed by:
| 372784 | ||||||||||||||||||||||||
2304 | - | |||||||||||||||||||||||||
2305 | /* | - | ||||||||||||||||||||||||
2306 | ** Term pTerm is a vector range comparison operation. The first comparison | - | ||||||||||||||||||||||||
2307 | ** in the vector can be optimized using column nEq of the index. This | - | ||||||||||||||||||||||||
2308 | ** function returns the total number of vector elements that can be used | - | ||||||||||||||||||||||||
2309 | ** as part of the range comparison. | - | ||||||||||||||||||||||||
2310 | ** | - | ||||||||||||||||||||||||
2311 | ** For example, if the query is: | - | ||||||||||||||||||||||||
2312 | ** | - | ||||||||||||||||||||||||
2313 | ** WHERE a = ? AND (b, c, d) > (?, ?, ?) | - | ||||||||||||||||||||||||
2314 | ** | - | ||||||||||||||||||||||||
2315 | ** and the index: | - | ||||||||||||||||||||||||
2316 | ** | - | ||||||||||||||||||||||||
2317 | ** CREATE INDEX ... ON (a, b, c, d, e) | - | ||||||||||||||||||||||||
2318 | ** | - | ||||||||||||||||||||||||
2319 | ** then this function would be invoked with nEq=1. The value returned in | - | ||||||||||||||||||||||||
2320 | ** this case is 3. | - | ||||||||||||||||||||||||
2321 | */ | - | ||||||||||||||||||||||||
2322 | static int whereRangeVectorLen( | - | ||||||||||||||||||||||||
2323 | Parse *pParse, /* Parsing context */ | - | ||||||||||||||||||||||||
2324 | int iCur, /* Cursor open on pIdx */ | - | ||||||||||||||||||||||||
2325 | Index *pIdx, /* The index to be used for a inequality constraint */ | - | ||||||||||||||||||||||||
2326 | int nEq, /* Number of prior equality constraints on same index */ | - | ||||||||||||||||||||||||
2327 | WhereTerm *pTerm /* The vector inequality constraint */ | - | ||||||||||||||||||||||||
2328 | ){ | - | ||||||||||||||||||||||||
2329 | int nCmp = sqlite3ExprVectorSize(pTerm->pExpr->pLeft); | - | ||||||||||||||||||||||||
2330 | int i; | - | ||||||||||||||||||||||||
2331 | - | |||||||||||||||||||||||||
2332 | nCmp = MIN(nCmp, (pIdx->nColumn - nEq));
| 18020-23013 | ||||||||||||||||||||||||
2333 | for(i=1; i<nCmp; i++){
| 3317-39708 | ||||||||||||||||||||||||
2334 | /* Test if comparison i of pTerm is compatible with column (i+nEq) | - | ||||||||||||||||||||||||
2335 | ** of the index. If not, exit the loop. */ | - | ||||||||||||||||||||||||
2336 | char aff; /* Comparison affinity */ | - | ||||||||||||||||||||||||
2337 | char idxaff = 0; /* Indexed columns affinity */ | - | ||||||||||||||||||||||||
2338 | CollSeq *pColl; /* Comparison collation sequence */ | - | ||||||||||||||||||||||||
2339 | Expr *pLhs = pTerm->pExpr->pLeft->x.pList->a[i].pExpr; | - | ||||||||||||||||||||||||
2340 | Expr *pRhs = pTerm->pExpr->pRight; | - | ||||||||||||||||||||||||
2341 | if( pRhs->flags & EP_xIsSelect ){
| 1511-1806 | ||||||||||||||||||||||||
2342 | pRhs = pRhs->x.pSelect->pEList->a[i].pExpr; | - | ||||||||||||||||||||||||
2343 | }else{ executed 1511 times by 1 test: end of block Executed by:
| 1511 | ||||||||||||||||||||||||
2344 | pRhs = pRhs->x.pList->a[i].pExpr; | - | ||||||||||||||||||||||||
2345 | } executed 1806 times by 1 test: end of block Executed by:
| 1806 | ||||||||||||||||||||||||
2346 | - | |||||||||||||||||||||||||
2347 | /* Check that the LHS of the comparison is a column reference to | - | ||||||||||||||||||||||||
2348 | ** the right column of the right source table. And that the sort | - | ||||||||||||||||||||||||
2349 | ** order of the index column is the same as the sort order of the | - | ||||||||||||||||||||||||
2350 | ** leftmost index column. */ | - | ||||||||||||||||||||||||
2351 | if( pLhs->op!=TK_COLUMN
| 4-3313 | ||||||||||||||||||||||||
2352 | || pLhs->iTable!=iCur
| 0-3313 | ||||||||||||||||||||||||
2353 | || pLhs->iColumn!=pIdx->aiColumn[i+nEq]
| 1277-2036 | ||||||||||||||||||||||||
2354 | || pIdx->aSortOrder[i+nEq]!=pIdx->aSortOrder[nEq]
| 35-2001 | ||||||||||||||||||||||||
2355 | ){ | - | ||||||||||||||||||||||||
2356 | break; executed 1316 times by 1 test: break; Executed by:
| 1316 | ||||||||||||||||||||||||
2357 | } | - | ||||||||||||||||||||||||
2358 | - | |||||||||||||||||||||||||
2359 | testcase( pLhs->iColumn==XN_ROWID ); | - | ||||||||||||||||||||||||
2360 | aff = sqlite3CompareAffinity(pRhs, sqlite3ExprAffinity(pLhs)); | - | ||||||||||||||||||||||||
2361 | idxaff = sqlite3TableColumnAffinity(pIdx->pTable, pLhs->iColumn); | - | ||||||||||||||||||||||||
2362 | if( aff!=idxaff ) break; executed 8 times by 1 test: break; Executed by:
| 8-1993 | ||||||||||||||||||||||||
2363 | - | |||||||||||||||||||||||||
2364 | pColl = sqlite3BinaryCompareCollSeq(pParse, pLhs, pRhs); | - | ||||||||||||||||||||||||
2365 | if( pColl==0 ) break; executed 1 time by 1 test: break; Executed by:
| 1-1992 | ||||||||||||||||||||||||
2366 | if( sqlite3StrICmp(pColl->zName, pIdx->azColl[i+nEq]) ) break; never executed: break;
| 0-1992 | ||||||||||||||||||||||||
2367 | } executed 1992 times by 1 test: end of block Executed by:
| 1992 | ||||||||||||||||||||||||
2368 | return i; executed 41033 times by 1 test: return i; Executed by:
| 41033 | ||||||||||||||||||||||||
2369 | } | - | ||||||||||||||||||||||||
2370 | - | |||||||||||||||||||||||||
2371 | /* | - | ||||||||||||||||||||||||
2372 | ** Adjust the cost C by the costMult facter T. This only occurs if | - | ||||||||||||||||||||||||
2373 | ** compiled with -DSQLITE_ENABLE_COSTMULT | - | ||||||||||||||||||||||||
2374 | */ | - | ||||||||||||||||||||||||
2375 | #ifdef SQLITE_ENABLE_COSTMULT | - | ||||||||||||||||||||||||
2376 | # define ApplyCostMultiplier(C,T) C += T | - | ||||||||||||||||||||||||
2377 | #else | - | ||||||||||||||||||||||||
2378 | # define ApplyCostMultiplier(C,T) | - | ||||||||||||||||||||||||
2379 | #endif | - | ||||||||||||||||||||||||
2380 | - | |||||||||||||||||||||||||
2381 | /* | - | ||||||||||||||||||||||||
2382 | ** We have so far matched pBuilder->pNew->u.btree.nEq terms of the | - | ||||||||||||||||||||||||
2383 | ** index pIndex. Try to match one more. | - | ||||||||||||||||||||||||
2384 | ** | - | ||||||||||||||||||||||||
2385 | ** When this function is called, pBuilder->pNew->nOut contains the | - | ||||||||||||||||||||||||
2386 | ** number of rows expected to be visited by filtering using the nEq | - | ||||||||||||||||||||||||
2387 | ** terms only. If it is modified, this value is restored before this | - | ||||||||||||||||||||||||
2388 | ** function returns. | - | ||||||||||||||||||||||||
2389 | ** | - | ||||||||||||||||||||||||
2390 | ** If pProbe->tnum==0, that means pIndex is a fake index used for the | - | ||||||||||||||||||||||||
2391 | ** INTEGER PRIMARY KEY. | - | ||||||||||||||||||||||||
2392 | */ | - | ||||||||||||||||||||||||
2393 | static int whereLoopAddBtreeIndex( | - | ||||||||||||||||||||||||
2394 | WhereLoopBuilder *pBuilder, /* The WhereLoop factory */ | - | ||||||||||||||||||||||||
2395 | struct SrcList_item *pSrc, /* FROM clause term being analyzed */ | - | ||||||||||||||||||||||||
2396 | Index *pProbe, /* An index on pSrc */ | - | ||||||||||||||||||||||||
2397 | LogEst nInMul /* log(Number of iterations due to IN) */ | - | ||||||||||||||||||||||||
2398 | ){ | - | ||||||||||||||||||||||||
2399 | WhereInfo *pWInfo = pBuilder->pWInfo; /* WHERE analyse context */ | - | ||||||||||||||||||||||||
2400 | Parse *pParse = pWInfo->pParse; /* Parsing context */ | - | ||||||||||||||||||||||||
2401 | sqlite3 *db = pParse->db; /* Database connection malloc context */ | - | ||||||||||||||||||||||||
2402 | WhereLoop *pNew; /* Template WhereLoop under construction */ | - | ||||||||||||||||||||||||
2403 | WhereTerm *pTerm; /* A WhereTerm under consideration */ | - | ||||||||||||||||||||||||
2404 | int opMask; /* Valid operators for constraints */ | - | ||||||||||||||||||||||||
2405 | WhereScan scan; /* Iterator for WHERE terms */ | - | ||||||||||||||||||||||||
2406 | Bitmask saved_prereq; /* Original value of pNew->prereq */ | - | ||||||||||||||||||||||||
2407 | u16 saved_nLTerm; /* Original value of pNew->nLTerm */ | - | ||||||||||||||||||||||||
2408 | u16 saved_nEq; /* Original value of pNew->u.btree.nEq */ | - | ||||||||||||||||||||||||
2409 | u16 saved_nBtm; /* Original value of pNew->u.btree.nBtm */ | - | ||||||||||||||||||||||||
2410 | u16 saved_nTop; /* Original value of pNew->u.btree.nTop */ | - | ||||||||||||||||||||||||
2411 | u16 saved_nSkip; /* Original value of pNew->nSkip */ | - | ||||||||||||||||||||||||
2412 | u32 saved_wsFlags; /* Original value of pNew->wsFlags */ | - | ||||||||||||||||||||||||
2413 | LogEst saved_nOut; /* Original value of pNew->nOut */ | - | ||||||||||||||||||||||||
2414 | int rc = SQLITE_OK; /* Return code */ | - | ||||||||||||||||||||||||
2415 | LogEst rSize; /* Number of rows in the table */ | - | ||||||||||||||||||||||||
2416 | LogEst rLogSize; /* Logarithm of table size */ | - | ||||||||||||||||||||||||
2417 | WhereTerm *pTop = 0, *pBtm = 0; /* Top and bottom range constraints */ | - | ||||||||||||||||||||||||
2418 | - | |||||||||||||||||||||||||
2419 | pNew = pBuilder->pNew; | - | ||||||||||||||||||||||||
2420 | if( db->mallocFailed ) return SQLITE_NOMEM_BKPT; executed 4 times by 1 test: return 7; Executed by:
| 4-535035 | ||||||||||||||||||||||||
2421 | WHERETRACE(0x800, ("BEGIN %s.addBtreeIdx(%s), nEq=%d\n", | - | ||||||||||||||||||||||||
2422 | pProbe->pTable->zName,pProbe->zName, pNew->u.btree.nEq)); | - | ||||||||||||||||||||||||
2423 | - | |||||||||||||||||||||||||
2424 | assert( (pNew->wsFlags & WHERE_VIRTUALTABLE)==0 ); | - | ||||||||||||||||||||||||
2425 | assert( (pNew->wsFlags & WHERE_TOP_LIMIT)==0 ); | - | ||||||||||||||||||||||||
2426 | if( pNew->wsFlags & WHERE_BTM_LIMIT ){
| 13065-521970 | ||||||||||||||||||||||||
2427 | opMask = WO_LT|WO_LE; | - | ||||||||||||||||||||||||
2428 | }else{ executed 13065 times by 1 test: end of block Executed by:
| 13065 | ||||||||||||||||||||||||
2429 | assert( pNew->u.btree.nBtm==0 ); | - | ||||||||||||||||||||||||
2430 | opMask = WO_EQ|WO_IN|WO_GT|WO_GE|WO_LT|WO_LE|WO_ISNULL|WO_IS; | - | ||||||||||||||||||||||||
2431 | } executed 521970 times by 435 tests: end of block Executed by:
| 521970 | ||||||||||||||||||||||||
2432 | if( pProbe->bUnordered ) opMask &= ~(WO_GT|WO_GE|WO_LT|WO_LE); executed 16 times by 1 test: opMask &= ~((0x0002<<(54 -53))|(0x0002<<(57 -53))|(0x0002<<(56 -53))|(0x0002<<(55 -53))); Executed by:
| 16-535019 | ||||||||||||||||||||||||
2433 | - | |||||||||||||||||||||||||
2434 | assert( pNew->u.btree.nEq<pProbe->nColumn ); | - | ||||||||||||||||||||||||
2435 | - | |||||||||||||||||||||||||
2436 | saved_nEq = pNew->u.btree.nEq; | - | ||||||||||||||||||||||||
2437 | saved_nBtm = pNew->u.btree.nBtm; | - | ||||||||||||||||||||||||
2438 | saved_nTop = pNew->u.btree.nTop; | - | ||||||||||||||||||||||||
2439 | saved_nSkip = pNew->nSkip; | - | ||||||||||||||||||||||||
2440 | saved_nLTerm = pNew->nLTerm; | - | ||||||||||||||||||||||||
2441 | saved_wsFlags = pNew->wsFlags; | - | ||||||||||||||||||||||||
2442 | saved_prereq = pNew->prereq; | - | ||||||||||||||||||||||||
2443 | saved_nOut = pNew->nOut; | - | ||||||||||||||||||||||||
2444 | pTerm = whereScanInit(&scan, pBuilder->pWC, pSrc->iCursor, saved_nEq, | - | ||||||||||||||||||||||||
2445 | opMask, pProbe); | - | ||||||||||||||||||||||||
2446 | pNew->rSetup = 0; | - | ||||||||||||||||||||||||
2447 | rSize = pProbe->aiRowLogEst[0]; | - | ||||||||||||||||||||||||
2448 | rLogSize = estLog(rSize); | - | ||||||||||||||||||||||||
2449 | for(; rc==SQLITE_OK && pTerm!=0; pTerm = whereScanNext(&scan)){
| 4-624837 | ||||||||||||||||||||||||
2450 | u16 eOp = pTerm->eOperator; /* Shorthand for pTerm->eOperator */ | - | ||||||||||||||||||||||||
2451 | LogEst rCostIdx; | - | ||||||||||||||||||||||||
2452 | LogEst nOutUnadjusted; /* nOut before IN() and WHERE adjustments */ | - | ||||||||||||||||||||||||
2453 | int nIn = 0; | - | ||||||||||||||||||||||||
2454 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 | - | ||||||||||||||||||||||||
2455 | int nRecValid = pBuilder->nRecValid; | - | ||||||||||||||||||||||||
2456 | #endif | - | ||||||||||||||||||||||||
2457 | if( (eOp==WO_ISNULL || (pTerm->wtFlags&TERM_VNULL)!=0)
| 0-89503 | ||||||||||||||||||||||||
2458 | && indexColumnNotNull(pProbe, saved_nEq)
| 22-283 | ||||||||||||||||||||||||
2459 | ){ | - | ||||||||||||||||||||||||
2460 | continue; /* ignore IS [NOT] NULL constraints on NOT NULL columns */ executed 22 times by 1 test: continue; Executed by:
| 22 | ||||||||||||||||||||||||
2461 | } | - | ||||||||||||||||||||||||
2462 | if( pTerm->prereqRight & pNew->maskSelf ) continue; executed 128 times by 1 test: continue; Executed by:
| 128-89658 | ||||||||||||||||||||||||
2463 | - | |||||||||||||||||||||||||
2464 | /* Do not allow the upper bound of a LIKE optimization range constraint | - | ||||||||||||||||||||||||
2465 | ** to mix with a lower range bound from some other source */ | - | ||||||||||||||||||||||||
2466 | if( pTerm->wtFlags & TERM_LIKEOPT && pTerm->eOperator==WO_LT ) continue; executed 6974 times by 1 test: continue; Executed by:
| 6972-75712 | ||||||||||||||||||||||||
2467 | - | |||||||||||||||||||||||||
2468 | /* Do not allow constraints from the WHERE clause to be used by the | - | ||||||||||||||||||||||||
2469 | ** right table of a LEFT JOIN. Only constraints in the ON clause are | - | ||||||||||||||||||||||||
2470 | ** allowed */ | - | ||||||||||||||||||||||||
2471 | if( (pSrc->fg.jointype & JT_LEFT)!=0
| 295-82389 | ||||||||||||||||||||||||
2472 | && !ExprHasProperty(pTerm->pExpr, EP_FromJoin)
| 15-280 | ||||||||||||||||||||||||
2473 | ){ | - | ||||||||||||||||||||||||
2474 | continue; executed 15 times by 1 test: continue; Executed by:
| 15 | ||||||||||||||||||||||||
2475 | } | - | ||||||||||||||||||||||||
2476 | - | |||||||||||||||||||||||||
2477 | if( IsUniqueIndex(pProbe) && saved_nEq==pProbe->nKeyCol-1 ){
| 1734-56410 | ||||||||||||||||||||||||
2478 | pBuilder->bldFlags |= SQLITE_BLDF_UNIQUE; | - | ||||||||||||||||||||||||
2479 | }else{ executed 24525 times by 1 test: end of block Executed by:
| 24525 | ||||||||||||||||||||||||
2480 | pBuilder->bldFlags |= SQLITE_BLDF_INDEXED; | - | ||||||||||||||||||||||||
2481 | } executed 58144 times by 1 test: end of block Executed by:
| 58144 | ||||||||||||||||||||||||
2482 | pNew->wsFlags = saved_wsFlags; | - | ||||||||||||||||||||||||
2483 | pNew->u.btree.nEq = saved_nEq; | - | ||||||||||||||||||||||||
2484 | pNew->u.btree.nBtm = saved_nBtm; | - | ||||||||||||||||||||||||
2485 | pNew->u.btree.nTop = saved_nTop; | - | ||||||||||||||||||||||||
2486 | pNew->nLTerm = saved_nLTerm; | - | ||||||||||||||||||||||||
2487 | if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */ executed 2 times by 1 test: break; Executed by:
| 2-82667 | ||||||||||||||||||||||||
2488 | pNew->aLTerm[pNew->nLTerm++] = pTerm; | - | ||||||||||||||||||||||||
2489 | pNew->prereq = (saved_prereq | pTerm->prereqRight) & ~pNew->maskSelf; | - | ||||||||||||||||||||||||
2490 | - | |||||||||||||||||||||||||
2491 | assert( nInMul==0 | - | ||||||||||||||||||||||||
2492 | || (pNew->wsFlags & WHERE_COLUMN_NULL)!=0 | - | ||||||||||||||||||||||||
2493 | || (pNew->wsFlags & WHERE_COLUMN_IN)!=0 | - | ||||||||||||||||||||||||
2494 | || (pNew->wsFlags & WHERE_SKIPSCAN)!=0 | - | ||||||||||||||||||||||||
2495 | ); | - | ||||||||||||||||||||||||
2496 | - | |||||||||||||||||||||||||
2497 | if( eOp & WO_IN ){
| 1012-81655 | ||||||||||||||||||||||||
2498 | Expr *pExpr = pTerm->pExpr; | - | ||||||||||||||||||||||||
2499 | if( ExprHasProperty(pExpr, EP_xIsSelect) ){
| 481-531 | ||||||||||||||||||||||||
2500 | /* "x IN (SELECT ...)": TUNING: the SELECT returns 25 rows */ | - | ||||||||||||||||||||||||
2501 | int i; | - | ||||||||||||||||||||||||
2502 | nIn = 46; assert( 46==sqlite3LogEst(25) ); | - | ||||||||||||||||||||||||
2503 | - | |||||||||||||||||||||||||
2504 | /* The expression may actually be of the form (x, y) IN (SELECT...). | - | ||||||||||||||||||||||||
2505 | ** In this case there is a separate term for each of (x) and (y). | - | ||||||||||||||||||||||||
2506 | ** However, the nIn multiplier should only be applied once, not once | - | ||||||||||||||||||||||||
2507 | ** for each such term. The following loop checks that pTerm is the | - | ||||||||||||||||||||||||
2508 | ** first such term in use, and sets nIn back to 0 if it is not. */ | - | ||||||||||||||||||||||||
2509 | for(i=0; i<pNew->nLTerm-1; i++){
| 235-481 | ||||||||||||||||||||||||
2510 | if( pNew->aLTerm[i] && pNew->aLTerm[i]->pExpr==pExpr ) nIn = 0; executed 31 times by 1 test: nIn = 0; Executed by:
| 2-233 | ||||||||||||||||||||||||
2511 | } executed 235 times by 1 test: end of block Executed by:
| 235 | ||||||||||||||||||||||||
2512 | }else if( ALWAYS(pExpr->x.pList && pExpr->x.pList->nExpr) ){ executed 481 times by 1 test: end of block Executed by:
| 0-531 | ||||||||||||||||||||||||
2513 | /* "x IN (value, value, ...)" */ | - | ||||||||||||||||||||||||
2514 | nIn = sqlite3LogEst(pExpr->x.pList->nExpr); | - | ||||||||||||||||||||||||
2515 | assert( nIn>0 ); /* RHS always has 2 or more terms... The parser | - | ||||||||||||||||||||||||
2516 | ** changes "x IN (?)" into "x=?". */ | - | ||||||||||||||||||||||||
2517 | } executed 531 times by 1 test: end of block Executed by:
| 531 | ||||||||||||||||||||||||
2518 | if( pProbe->hasStat1 ){
| 28-984 | ||||||||||||||||||||||||
2519 | LogEst M, logK, safetyMargin; | - | ||||||||||||||||||||||||
2520 | /* Let: | - | ||||||||||||||||||||||||
2521 | ** N = the total number of rows in the table | - | ||||||||||||||||||||||||
2522 | ** K = the number of entries on the RHS of the IN operator | - | ||||||||||||||||||||||||
2523 | ** M = the number of rows in the table that match terms to the | - | ||||||||||||||||||||||||
2524 | ** to the left in the same index. If the IN operator is on | - | ||||||||||||||||||||||||
2525 | ** the left-most index column, M==N. | - | ||||||||||||||||||||||||
2526 | ** | - | ||||||||||||||||||||||||
2527 | ** Given the definitions above, it is better to omit the IN operator | - | ||||||||||||||||||||||||
2528 | ** from the index lookup and instead do a scan of the M elements, | - | ||||||||||||||||||||||||
2529 | ** testing each scanned row against the IN operator separately, if: | - | ||||||||||||||||||||||||
2530 | ** | - | ||||||||||||||||||||||||
2531 | ** M*log(K) < K*log(N) | - | ||||||||||||||||||||||||
2532 | ** | - | ||||||||||||||||||||||||
2533 | ** Our estimates for M, K, and N might be inaccurate, so we build in | - | ||||||||||||||||||||||||
2534 | ** a safety margin of 2 (LogEst: 10) that favors using the IN operator | - | ||||||||||||||||||||||||
2535 | ** with the index, as using an index has better worst-case behavior. | - | ||||||||||||||||||||||||
2536 | ** If we do not have real sqlite_stat1 data, always prefer to use | - | ||||||||||||||||||||||||
2537 | ** the index. | - | ||||||||||||||||||||||||
2538 | */ | - | ||||||||||||||||||||||||
2539 | M = pProbe->aiRowLogEst[saved_nEq]; | - | ||||||||||||||||||||||||
2540 | logK = estLog(nIn); | - | ||||||||||||||||||||||||
2541 | safetyMargin = 10; /* TUNING: extra weight for indexed IN */ | - | ||||||||||||||||||||||||
2542 | if( M + logK + safetyMargin < nIn + rLogSize ){
| 1-27 | ||||||||||||||||||||||||
2543 | WHERETRACE(0x40, | - | ||||||||||||||||||||||||
2544 | ("Scan preferred over IN operator on column %d of \"%s\" (%d<%d)\n", | - | ||||||||||||||||||||||||
2545 | saved_nEq, pProbe->zName, M+logK+10, nIn+rLogSize)); | - | ||||||||||||||||||||||||
2546 | continue; executed 1 time by 1 test: continue; Executed by:
| 1 | ||||||||||||||||||||||||
2547 | }else{ | - | ||||||||||||||||||||||||
2548 | WHERETRACE(0x40, | - | ||||||||||||||||||||||||
2549 | ("IN operator preferred on column %d of \"%s\" (%d>=%d)\n", | - | ||||||||||||||||||||||||
2550 | saved_nEq, pProbe->zName, M+logK+10, nIn+rLogSize)); | - | ||||||||||||||||||||||||
2551 | } executed 27 times by 1 test: end of block Executed by:
| 27 | ||||||||||||||||||||||||
2552 | } | - | ||||||||||||||||||||||||
2553 | pNew->wsFlags |= WHERE_COLUMN_IN; | - | ||||||||||||||||||||||||
2554 | }else if( eOp & (WO_EQ|WO_IS) ){ executed 1011 times by 1 test: end of block Executed by:
| 1011-41314 | ||||||||||||||||||||||||
2555 | int iCol = pProbe->aiColumn[saved_nEq]; | - | ||||||||||||||||||||||||
2556 | pNew->wsFlags |= WHERE_COLUMN_EQ; | - | ||||||||||||||||||||||||
2557 | assert( saved_nEq==pNew->u.btree.nEq ); | - | ||||||||||||||||||||||||
2558 | if( iCol==XN_ROWID
| 7968-32373 | ||||||||||||||||||||||||
2559 | || (iCol>=0 && nInMul==0 && saved_nEq==pProbe->nKeyCol-1)
| 25-32348 | ||||||||||||||||||||||||
2560 | ){ | - | ||||||||||||||||||||||||
2561 | if( iCol==XN_ROWID || pProbe->uniqNotNull
| 39-22009 | ||||||||||||||||||||||||
2562 | || (pProbe->nKeyCol==1 && pProbe->onError && eOp==WO_EQ)
| 292-20964 | ||||||||||||||||||||||||
2563 | ){ | - | ||||||||||||||||||||||||
2564 | pNew->wsFlags |= WHERE_ONEROW; | - | ||||||||||||||||||||||||
2565 | }else{ executed 8299 times by 1 test: end of block Executed by:
| 8299 | ||||||||||||||||||||||||
2566 | pNew->wsFlags |= WHERE_UNQ_WANTED; | - | ||||||||||||||||||||||||
2567 | } executed 21678 times by 1 test: end of block Executed by:
| 21678 | ||||||||||||||||||||||||
2568 | } | - | ||||||||||||||||||||||||
2569 | }else if( eOp & WO_ISNULL ){ executed 40341 times by 1 test: end of block Executed by:
| 281-41033 | ||||||||||||||||||||||||
2570 | pNew->wsFlags |= WHERE_COLUMN_NULL; | - | ||||||||||||||||||||||||
2571 | }else if( eOp & (WO_GT|WO_GE) ){ executed 281 times by 1 test: end of block Executed by:
| 281-20997 | ||||||||||||||||||||||||
2572 | testcase( eOp & WO_GT ); | - | ||||||||||||||||||||||||
2573 | testcase( eOp & WO_GE ); | - | ||||||||||||||||||||||||
2574 | pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_BTM_LIMIT; | - | ||||||||||||||||||||||||
2575 | pNew->u.btree.nBtm = whereRangeVectorLen( | - | ||||||||||||||||||||||||
2576 | pParse, pSrc->iCursor, pProbe, saved_nEq, pTerm | - | ||||||||||||||||||||||||
2577 | ); | - | ||||||||||||||||||||||||
2578 | pBtm = pTerm; | - | ||||||||||||||||||||||||
2579 | pTop = 0; | - | ||||||||||||||||||||||||
2580 | if( pTerm->wtFlags & TERM_LIKEOPT ){
| 6972-13064 | ||||||||||||||||||||||||
2581 | /* Range contraints that come from the LIKE optimization are | - | ||||||||||||||||||||||||
2582 | ** always used in pairs. */ | - | ||||||||||||||||||||||||
2583 | pTop = &pTerm[1]; | - | ||||||||||||||||||||||||
2584 | assert( (pTop-(pTerm->pWC->a))<pTerm->pWC->nTerm ); | - | ||||||||||||||||||||||||
2585 | assert( pTop->wtFlags & TERM_LIKEOPT ); | - | ||||||||||||||||||||||||
2586 | assert( pTop->eOperator==WO_LT ); | - | ||||||||||||||||||||||||
2587 | if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */ never executed: break;
| 0-6972 | ||||||||||||||||||||||||
2588 | pNew->aLTerm[pNew->nLTerm++] = pTop; | - | ||||||||||||||||||||||||
2589 | pNew->wsFlags |= WHERE_TOP_LIMIT; | - | ||||||||||||||||||||||||
2590 | pNew->u.btree.nTop = 1; | - | ||||||||||||||||||||||||
2591 | } executed 6972 times by 1 test: end of block Executed by:
| 6972 | ||||||||||||||||||||||||
2592 | }else{ executed 20036 times by 1 test: end of block Executed by:
| 20036 | ||||||||||||||||||||||||
2593 | assert( eOp & (WO_LT|WO_LE) ); | - | ||||||||||||||||||||||||
2594 | testcase( eOp & WO_LT ); | - | ||||||||||||||||||||||||
2595 | testcase( eOp & WO_LE ); | - | ||||||||||||||||||||||||
2596 | pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_TOP_LIMIT; | - | ||||||||||||||||||||||||
2597 | pNew->u.btree.nTop = whereRangeVectorLen( | - | ||||||||||||||||||||||||
2598 | pParse, pSrc->iCursor, pProbe, saved_nEq, pTerm | - | ||||||||||||||||||||||||
2599 | ); | - | ||||||||||||||||||||||||
2600 | pTop = pTerm; | - | ||||||||||||||||||||||||
2601 | pBtm = (pNew->wsFlags & WHERE_BTM_LIMIT)!=0 ?
| 8269-12728 | ||||||||||||||||||||||||
2602 | pNew->aLTerm[pNew->nLTerm-2] : 0; | - | ||||||||||||||||||||||||
2603 | } executed 20997 times by 1 test: end of block Executed by:
| 20997 | ||||||||||||||||||||||||
2604 | - | |||||||||||||||||||||||||
2605 | /* At this point pNew->nOut is set to the number of rows expected to | - | ||||||||||||||||||||||||
2606 | ** be visited by the index scan before considering term pTerm, or the | - | ||||||||||||||||||||||||
2607 | ** values of nIn and nInMul. In other words, assuming that all | - | ||||||||||||||||||||||||
2608 | ** "x IN(...)" terms are replaced with "x = ?". This block updates | - | ||||||||||||||||||||||||
2609 | ** the value of pNew->nOut to account for pTerm (but not nIn/nInMul). */ | - | ||||||||||||||||||||||||
2610 | assert( pNew->nOut==saved_nOut ); | - | ||||||||||||||||||||||||
2611 | if( pNew->wsFlags & WHERE_COLUMN_RANGE ){
| 41033-41633 | ||||||||||||||||||||||||
2612 | /* Adjust nOut using stat3/stat4 data. Or, if there is no stat3/stat4 | - | ||||||||||||||||||||||||
2613 | ** data, using some other estimate. */ | - | ||||||||||||||||||||||||
2614 | whereRangeScanEst(pParse, pBuilder, pBtm, pTop, pNew); | - | ||||||||||||||||||||||||
2615 | }else{ executed 41033 times by 1 test: end of block Executed by:
| 41033 | ||||||||||||||||||||||||
2616 | int nEq = ++pNew->u.btree.nEq; | - | ||||||||||||||||||||||||
2617 | assert( eOp & (WO_ISNULL|WO_EQ|WO_IN|WO_IS) ); | - | ||||||||||||||||||||||||
2618 | - | |||||||||||||||||||||||||
2619 | assert( pNew->nOut==saved_nOut ); | - | ||||||||||||||||||||||||
2620 | if( pTerm->truthProb<=0 && pProbe->aiColumn[saved_nEq]>=0 ){
| 6-41597 | ||||||||||||||||||||||||
2621 | assert( (eOp & WO_IN) || nIn==0 ); | - | ||||||||||||||||||||||||
2622 | testcase( eOp & WO_IN ); | - | ||||||||||||||||||||||||
2623 | pNew->nOut += pTerm->truthProb; | - | ||||||||||||||||||||||||
2624 | pNew->nOut -= nIn; | - | ||||||||||||||||||||||||
2625 | }else{ executed 30 times by 1 test: end of block Executed by:
| 30 | ||||||||||||||||||||||||
2626 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 | - | ||||||||||||||||||||||||
2627 | tRowcnt nOut = 0; | - | ||||||||||||||||||||||||
2628 | if( nInMul==0 | - | ||||||||||||||||||||||||
2629 | && pProbe->nSample | - | ||||||||||||||||||||||||
2630 | && pNew->u.btree.nEq<=pProbe->nSampleCol | - | ||||||||||||||||||||||||
2631 | && ((eOp & WO_IN)==0 || !ExprHasProperty(pTerm->pExpr, EP_xIsSelect)) | - | ||||||||||||||||||||||||
2632 | && OptimizationEnabled(db, SQLITE_Stat34) | - | ||||||||||||||||||||||||
2633 | ){ | - | ||||||||||||||||||||||||
2634 | Expr *pExpr = pTerm->pExpr; | - | ||||||||||||||||||||||||
2635 | if( (eOp & (WO_EQ|WO_ISNULL|WO_IS))!=0 ){ | - | ||||||||||||||||||||||||
2636 | testcase( eOp & WO_EQ ); | - | ||||||||||||||||||||||||
2637 | testcase( eOp & WO_IS ); | - | ||||||||||||||||||||||||
2638 | testcase( eOp & WO_ISNULL ); | - | ||||||||||||||||||||||||
2639 | rc = whereEqualScanEst(pParse, pBuilder, pExpr->pRight, &nOut); | - | ||||||||||||||||||||||||
2640 | }else{ | - | ||||||||||||||||||||||||
2641 | rc = whereInScanEst(pParse, pBuilder, pExpr->x.pList, &nOut); | - | ||||||||||||||||||||||||
2642 | } | - | ||||||||||||||||||||||||
2643 | if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK; | - | ||||||||||||||||||||||||
2644 | if( rc!=SQLITE_OK ) break; /* Jump out of the pTerm loop */ | - | ||||||||||||||||||||||||
2645 | if( nOut ){ | - | ||||||||||||||||||||||||
2646 | pNew->nOut = sqlite3LogEst(nOut); | - | ||||||||||||||||||||||||
2647 | if( pNew->nOut>saved_nOut ) pNew->nOut = saved_nOut; | - | ||||||||||||||||||||||||
2648 | pNew->nOut -= nIn; | - | ||||||||||||||||||||||||
2649 | } | - | ||||||||||||||||||||||||
2650 | } | - | ||||||||||||||||||||||||
2651 | if( nOut==0 ) | - | ||||||||||||||||||||||||
2652 | #endif | - | ||||||||||||||||||||||||
2653 | { | - | ||||||||||||||||||||||||
2654 | pNew->nOut += (pProbe->aiRowLogEst[nEq] - pProbe->aiRowLogEst[nEq-1]); | - | ||||||||||||||||||||||||
2655 | if( eOp & WO_ISNULL ){
| 281-41322 | ||||||||||||||||||||||||
2656 | /* TUNING: If there is no likelihood() value, assume that a | - | ||||||||||||||||||||||||
2657 | ** "col IS NULL" expression matches twice as many rows | - | ||||||||||||||||||||||||
2658 | ** as (col=?). */ | - | ||||||||||||||||||||||||
2659 | pNew->nOut += 10; | - | ||||||||||||||||||||||||
2660 | } executed 281 times by 1 test: end of block Executed by:
| 281 | ||||||||||||||||||||||||
2661 | } | - | ||||||||||||||||||||||||
2662 | } executed 41603 times by 1 test: end of block Executed by:
| 41603 | ||||||||||||||||||||||||
2663 | } | - | ||||||||||||||||||||||||
2664 | - | |||||||||||||||||||||||||
2665 | /* Set rCostIdx to the cost of visiting selected rows in index. Add | - | ||||||||||||||||||||||||
2666 | ** it to pNew->rRun, which is currently set to the cost of the index | - | ||||||||||||||||||||||||
2667 | ** seek only. Then, if this is a non-covering index, add the cost of | - | ||||||||||||||||||||||||
2668 | ** visiting the rows in the main table. */ | - | ||||||||||||||||||||||||
2669 | rCostIdx = pNew->nOut + 1 + (15*pProbe->szIdxRow)/pSrc->pTab->szTabRow; | - | ||||||||||||||||||||||||
2670 | pNew->rRun = sqlite3LogEstAdd(rLogSize, rCostIdx); | - | ||||||||||||||||||||||||
2671 | if( (pNew->wsFlags & (WHERE_IDX_ONLY|WHERE_IPK))==0 ){
| 35721-46945 | ||||||||||||||||||||||||
2672 | pNew->rRun = sqlite3LogEstAdd(pNew->rRun, pNew->nOut + 16); | - | ||||||||||||||||||||||||
2673 | } executed 46945 times by 1 test: end of block Executed by:
| 46945 | ||||||||||||||||||||||||
2674 | ApplyCostMultiplier(pNew->rRun, pProbe->pTable->costMult); | - | ||||||||||||||||||||||||
2675 | - | |||||||||||||||||||||||||
2676 | nOutUnadjusted = pNew->nOut; | - | ||||||||||||||||||||||||
2677 | pNew->rRun += nInMul + nIn; | - | ||||||||||||||||||||||||
2678 | pNew->nOut += nInMul + nIn; | - | ||||||||||||||||||||||||
2679 | whereLoopOutputAdjust(pBuilder->pWC, pNew, rSize); | - | ||||||||||||||||||||||||
2680 | rc = whereLoopInsert(pBuilder, pNew); | - | ||||||||||||||||||||||||
2681 | - | |||||||||||||||||||||||||
2682 | if( pNew->wsFlags & WHERE_COLUMN_RANGE ){
| 41033-41633 | ||||||||||||||||||||||||
2683 | pNew->nOut = saved_nOut; | - | ||||||||||||||||||||||||
2684 | }else{ executed 41033 times by 1 test: end of block Executed by:
| 41033 | ||||||||||||||||||||||||
2685 | pNew->nOut = nOutUnadjusted; | - | ||||||||||||||||||||||||
2686 | } executed 41633 times by 1 test: end of block Executed by:
| 41633 | ||||||||||||||||||||||||
2687 | - | |||||||||||||||||||||||||
2688 | if( (pNew->wsFlags & WHERE_TOP_LIMIT)==0
| 27969-54697 | ||||||||||||||||||||||||
2689 | && pNew->u.btree.nEq<pProbe->nColumn
| 8121-46576 | ||||||||||||||||||||||||
2690 | ){ | - | ||||||||||||||||||||||||
2691 | whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nInMul+nIn); | - | ||||||||||||||||||||||||
2692 | } executed 46576 times by 1 test: end of block Executed by:
| 46576 | ||||||||||||||||||||||||
2693 | pNew->nOut = saved_nOut; | - | ||||||||||||||||||||||||
2694 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 | - | ||||||||||||||||||||||||
2695 | pBuilder->nRecValid = nRecValid; | - | ||||||||||||||||||||||||
2696 | #endif | - | ||||||||||||||||||||||||
2697 | } executed 82666 times by 1 test: end of block Executed by:
| 82666 | ||||||||||||||||||||||||
2698 | pNew->prereq = saved_prereq; | - | ||||||||||||||||||||||||
2699 | pNew->u.btree.nEq = saved_nEq; | - | ||||||||||||||||||||||||
2700 | pNew->u.btree.nBtm = saved_nBtm; | - | ||||||||||||||||||||||||
2701 | pNew->u.btree.nTop = saved_nTop; | - | ||||||||||||||||||||||||
2702 | pNew->nSkip = saved_nSkip; | - | ||||||||||||||||||||||||
2703 | pNew->wsFlags = saved_wsFlags; | - | ||||||||||||||||||||||||
2704 | pNew->nOut = saved_nOut; | - | ||||||||||||||||||||||||
2705 | pNew->nLTerm = saved_nLTerm; | - | ||||||||||||||||||||||||
2706 | - | |||||||||||||||||||||||||
2707 | /* Consider using a skip-scan if there are no WHERE clause constraints | - | ||||||||||||||||||||||||
2708 | ** available for the left-most terms of the index, and if the average | - | ||||||||||||||||||||||||
2709 | ** number of repeats in the left-most terms is at least 18. | - | ||||||||||||||||||||||||
2710 | ** | - | ||||||||||||||||||||||||
2711 | ** The magic number 18 is selected on the basis that scanning 17 rows | - | ||||||||||||||||||||||||
2712 | ** is almost always quicker than an index seek (even though if the index | - | ||||||||||||||||||||||||
2713 | ** contains fewer than 2^17 rows we assume otherwise in other parts of | - | ||||||||||||||||||||||||
2714 | ** the code). And, even if it is not, it should not be too much slower. | - | ||||||||||||||||||||||||
2715 | ** On the other hand, the extra seeks could end up being significantly | - | ||||||||||||||||||||||||
2716 | ** more expensive. */ | - | ||||||||||||||||||||||||
2717 | assert( 42==sqlite3LogEst(18) ); | - | ||||||||||||||||||||||||
2718 | if( saved_nEq==saved_nSkip
| 34313-500722 | ||||||||||||||||||||||||
2719 | && saved_nEq+1<pProbe->nKeyCol
| 98013-402709 | ||||||||||||||||||||||||
2720 | && pProbe->noSkipScan==0
| 2-98011 | ||||||||||||||||||||||||
2721 | && OptimizationEnabled(db, SQLITE_SkipScan)
| 1-98010 | ||||||||||||||||||||||||
2722 | && pProbe->aiRowLogEst[saved_nEq+1]>=42 /* TUNING: Minimum for skip-scan */
| 617-97393 | ||||||||||||||||||||||||
2723 | && (rc = whereLoopResize(db, pNew, pNew->nLTerm+1))==SQLITE_OK
| 0-617 | ||||||||||||||||||||||||
2724 | ){ | - | ||||||||||||||||||||||||
2725 | LogEst nIter; | - | ||||||||||||||||||||||||
2726 | pNew->u.btree.nEq++; | - | ||||||||||||||||||||||||
2727 | pNew->nSkip++; | - | ||||||||||||||||||||||||
2728 | pNew->aLTerm[pNew->nLTerm++] = 0; | - | ||||||||||||||||||||||||
2729 | pNew->wsFlags |= WHERE_SKIPSCAN; | - | ||||||||||||||||||||||||
2730 | nIter = pProbe->aiRowLogEst[saved_nEq] - pProbe->aiRowLogEst[saved_nEq+1]; | - | ||||||||||||||||||||||||
2731 | pNew->nOut -= nIter; | - | ||||||||||||||||||||||||
2732 | /* TUNING: Because uncertainties in the estimates for skip-scan queries, | - | ||||||||||||||||||||||||
2733 | ** add a 1.375 fudge factor to make skip-scan slightly less likely. */ | - | ||||||||||||||||||||||||
2734 | nIter += 5; | - | ||||||||||||||||||||||||
2735 | whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nIter + nInMul); | - | ||||||||||||||||||||||||
2736 | pNew->nOut = saved_nOut; | - | ||||||||||||||||||||||||
2737 | pNew->u.btree.nEq = saved_nEq; | - | ||||||||||||||||||||||||
2738 | pNew->nSkip = saved_nSkip; | - | ||||||||||||||||||||||||
2739 | pNew->wsFlags = saved_wsFlags; | - | ||||||||||||||||||||||||
2740 | } executed 617 times by 1 test: end of block Executed by:
| 617 | ||||||||||||||||||||||||
2741 | - | |||||||||||||||||||||||||
2742 | WHERETRACE(0x800, ("END %s.addBtreeIdx(%s), nEq=%d, rc=%d\n", | - | ||||||||||||||||||||||||
2743 | pProbe->pTable->zName, pProbe->zName, saved_nEq, rc)); | - | ||||||||||||||||||||||||
2744 | return rc; executed 535035 times by 435 tests: return rc; Executed by:
| 535035 | ||||||||||||||||||||||||
2745 | } | - | ||||||||||||||||||||||||
2746 | - | |||||||||||||||||||||||||
2747 | /* | - | ||||||||||||||||||||||||
2748 | ** Return True if it is possible that pIndex might be useful in | - | ||||||||||||||||||||||||
2749 | ** implementing the ORDER BY clause in pBuilder. | - | ||||||||||||||||||||||||
2750 | ** | - | ||||||||||||||||||||||||
2751 | ** Return False if pBuilder does not contain an ORDER BY clause or | - | ||||||||||||||||||||||||
2752 | ** if there is no way for pIndex to be useful in implementing that | - | ||||||||||||||||||||||||
2753 | ** ORDER BY clause. | - | ||||||||||||||||||||||||
2754 | */ | - | ||||||||||||||||||||||||
2755 | static int indexMightHelpWithOrderBy( | - | ||||||||||||||||||||||||
2756 | WhereLoopBuilder *pBuilder, | - | ||||||||||||||||||||||||
2757 | Index *pIndex, | - | ||||||||||||||||||||||||
2758 | int iCursor | - | ||||||||||||||||||||||||
2759 | ){ | - | ||||||||||||||||||||||||
2760 | ExprList *pOB; | - | ||||||||||||||||||||||||
2761 | ExprList *aColExpr; | - | ||||||||||||||||||||||||
2762 | int ii, jj; | - | ||||||||||||||||||||||||
2763 | - | |||||||||||||||||||||||||
2764 | if( pIndex->bUnordered ) return 0; executed 14 times by 1 test: return 0; Executed by:
| 14-487875 | ||||||||||||||||||||||||
2765 | if( (pOB = pBuilder->pWInfo->pOrderBy)==0 ) return 0; executed 383729 times by 401 tests: return 0; Executed by:
| 104146-383729 | ||||||||||||||||||||||||
2766 | for(ii=0; ii<pOB->nExpr; ii++){
| 40803-110016 | ||||||||||||||||||||||||
2767 | Expr *pExpr = sqlite3ExprSkipCollate(pOB->a[ii].pExpr); | - | ||||||||||||||||||||||||
2768 | if( pExpr->op==TK_COLUMN && pExpr->iTable==iCursor ){
| 8073-93428 | ||||||||||||||||||||||||
2769 | if( pExpr->iColumn<0 ) return 1; executed 57777 times by 435 tests: return 1; Executed by:
| 27578-57777 | ||||||||||||||||||||||||
2770 | for(jj=0; jj<pIndex->nKeyCol; jj++){
| 22031-28652 | ||||||||||||||||||||||||
2771 | if( pExpr->iColumn==pIndex->aiColumn[jj] ) return 1; executed 5547 times by 2 tests: return 1; Executed by:
| 5547-23105 | ||||||||||||||||||||||||
2772 | } executed 23105 times by 1 test: end of block Executed by:
| 23105 | ||||||||||||||||||||||||
2773 | }else if( (aColExpr = pIndex->aColExpr)!=0 ){ executed 22031 times by 1 test: end of block Executed by:
| 81-24580 | ||||||||||||||||||||||||
2774 | for(jj=0; jj<pIndex->nKeyCol; jj++){
| 62-115 | ||||||||||||||||||||||||
2775 | if( pIndex->aiColumn[jj]!=XN_EXPR ) continue; executed 33 times by 1 test: continue; Executed by:
| 33-82 | ||||||||||||||||||||||||
2776 | if( sqlite3ExprCompareSkip(pExpr,aColExpr->a[jj].pExpr,iCursor)==0 ){
| 19-63 | ||||||||||||||||||||||||
2777 | return 1; executed 19 times by 1 test: return 1; Executed by:
| 19 | ||||||||||||||||||||||||
2778 | } | - | ||||||||||||||||||||||||
2779 | } executed 63 times by 1 test: end of block Executed by:
| 63 | ||||||||||||||||||||||||
2780 | } executed 62 times by 1 test: end of block Executed by:
| 62 | ||||||||||||||||||||||||
2781 | } executed 46673 times by 1 test: end of block Executed by:
| 46673 | ||||||||||||||||||||||||
2782 | return 0; executed 40803 times by 1 test: return 0; Executed by:
| 40803 | ||||||||||||||||||||||||
2783 | } | - | ||||||||||||||||||||||||
2784 | - | |||||||||||||||||||||||||
2785 | /* Check to see if a partial index with pPartIndexWhere can be used | - | ||||||||||||||||||||||||
2786 | ** in the current query. Return true if it can be and false if not. | - | ||||||||||||||||||||||||
2787 | */ | - | ||||||||||||||||||||||||
2788 | static int whereUsablePartialIndex(int iTab, WhereClause *pWC, Expr *pWhere){ | - | ||||||||||||||||||||||||
2789 | int i; | - | ||||||||||||||||||||||||
2790 | WhereTerm *pTerm; | - | ||||||||||||||||||||||||
2791 | Parse *pParse = pWC->pWInfo->pParse; | - | ||||||||||||||||||||||||
2792 | while( pWhere->op==TK_AND ){
| 12-170 | ||||||||||||||||||||||||
2793 | if( !whereUsablePartialIndex(iTab,pWC,pWhere->pLeft) ) return 0; never executed: return 0;
| 0-12 | ||||||||||||||||||||||||
2794 | pWhere = pWhere->pRight; | - | ||||||||||||||||||||||||
2795 | } executed 12 times by 1 test: end of block Executed by:
| 12 | ||||||||||||||||||||||||
2796 | if( pParse->db->flags & SQLITE_EnableQPSG ) pParse = 0; executed 7 times by 1 test: pParse = 0; Executed by:
| 7-163 | ||||||||||||||||||||||||
2797 | for(i=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
| 113-193 | ||||||||||||||||||||||||
2798 | Expr *pExpr = pTerm->pExpr; | - | ||||||||||||||||||||||||
2799 | if( (!ExprHasProperty(pExpr, EP_FromJoin) || pExpr->iRightJoinTable==iTab)
| 2-185 | ||||||||||||||||||||||||
2800 | && sqlite3ExprImpliesExpr(pParse, pExpr, pWhere, iTab)
| 57-130 | ||||||||||||||||||||||||
2801 | ){ | - | ||||||||||||||||||||||||
2802 | return 1; executed 57 times by 1 test: return 1; Executed by:
| 57 | ||||||||||||||||||||||||
2803 | } | - | ||||||||||||||||||||||||
2804 | } executed 136 times by 1 test: end of block Executed by:
| 136 | ||||||||||||||||||||||||
2805 | return 0; executed 113 times by 1 test: return 0; Executed by:
| 113 | ||||||||||||||||||||||||
2806 | } | - | ||||||||||||||||||||||||
2807 | - | |||||||||||||||||||||||||
2808 | /* | - | ||||||||||||||||||||||||
2809 | ** Add all WhereLoop objects for a single table of the join where the table | - | ||||||||||||||||||||||||
2810 | ** is identified by pBuilder->pNew->iTab. That table is guaranteed to be | - | ||||||||||||||||||||||||
2811 | ** a b-tree table, not a virtual table. | - | ||||||||||||||||||||||||
2812 | ** | - | ||||||||||||||||||||||||
2813 | ** The costs (WhereLoop.rRun) of the b-tree loops added by this function | - | ||||||||||||||||||||||||
2814 | ** are calculated as follows: | - | ||||||||||||||||||||||||
2815 | ** | - | ||||||||||||||||||||||||
2816 | ** For a full scan, assuming the table (or index) contains nRow rows: | - | ||||||||||||||||||||||||
2817 | ** | - | ||||||||||||||||||||||||
2818 | ** cost = nRow * 3.0 // full-table scan | - | ||||||||||||||||||||||||
2819 | ** cost = nRow * K // scan of covering index | - | ||||||||||||||||||||||||
2820 | ** cost = nRow * (K+3.0) // scan of non-covering index | - | ||||||||||||||||||||||||
2821 | ** | - | ||||||||||||||||||||||||
2822 | ** where K is a value between 1.1 and 3.0 set based on the relative | - | ||||||||||||||||||||||||
2823 | ** estimated average size of the index and table records. | - | ||||||||||||||||||||||||
2824 | ** | - | ||||||||||||||||||||||||
2825 | ** For an index scan, where nVisit is the number of index rows visited | - | ||||||||||||||||||||||||
2826 | ** by the scan, and nSeek is the number of seek operations required on | - | ||||||||||||||||||||||||
2827 | ** the index b-tree: | - | ||||||||||||||||||||||||
2828 | ** | - | ||||||||||||||||||||||||
2829 | ** cost = nSeek * (log(nRow) + K * nVisit) // covering index | - | ||||||||||||||||||||||||
2830 | ** cost = nSeek * (log(nRow) + (K+3.0) * nVisit) // non-covering index | - | ||||||||||||||||||||||||
2831 | ** | - | ||||||||||||||||||||||||
2832 | ** Normally, nSeek is 1. nSeek values greater than 1 come about if the | - | ||||||||||||||||||||||||
2833 | ** WHERE clause includes "x IN (....)" terms used in place of "x=?". Or when | - | ||||||||||||||||||||||||
2834 | ** implicit "x IN (SELECT x FROM tbl)" terms are added for skip-scans. | - | ||||||||||||||||||||||||
2835 | ** | - | ||||||||||||||||||||||||
2836 | ** The estimated values (nRow, nVisit, nSeek) often contain a large amount | - | ||||||||||||||||||||||||
2837 | ** of uncertainty. For this reason, scoring is designed to pick plans that | - | ||||||||||||||||||||||||
2838 | ** "do the least harm" if the estimates are inaccurate. For example, a | - | ||||||||||||||||||||||||
2839 | ** log(nRow) factor is omitted from a non-covering index scan in order to | - | ||||||||||||||||||||||||
2840 | ** bias the scoring in favor of using an index, since the worst-case | - | ||||||||||||||||||||||||
2841 | ** performance of using an index is far better than the worst-case performance | - | ||||||||||||||||||||||||
2842 | ** of a full table scan. | - | ||||||||||||||||||||||||
2843 | */ | - | ||||||||||||||||||||||||
2844 | static int whereLoopAddBtree( | - | ||||||||||||||||||||||||
2845 | WhereLoopBuilder *pBuilder, /* WHERE clause information */ | - | ||||||||||||||||||||||||
2846 | Bitmask mPrereq /* Extra prerequesites for using this table */ | - | ||||||||||||||||||||||||
2847 | ){ | - | ||||||||||||||||||||||||
2848 | WhereInfo *pWInfo; /* WHERE analysis context */ | - | ||||||||||||||||||||||||
2849 | Index *pProbe; /* An index we are evaluating */ | - | ||||||||||||||||||||||||
2850 | Index sPk; /* A fake index object for the primary key */ | - | ||||||||||||||||||||||||
2851 | LogEst aiRowEstPk[2]; /* The aiRowLogEst[] value for the sPk index */ | - | ||||||||||||||||||||||||
2852 | i16 aiColumnPk = -1; /* The aColumn[] value for the sPk index */ | - | ||||||||||||||||||||||||
2853 | SrcList *pTabList; /* The FROM clause */ | - | ||||||||||||||||||||||||
2854 | struct SrcList_item *pSrc; /* The FROM clause btree term to add */ | - | ||||||||||||||||||||||||
2855 | WhereLoop *pNew; /* Template WhereLoop object */ | - | ||||||||||||||||||||||||
2856 | int rc = SQLITE_OK; /* Return code */ | - | ||||||||||||||||||||||||
2857 | int iSortIdx = 1; /* Index number */ | - | ||||||||||||||||||||||||
2858 | int b; /* A boolean value */ | - | ||||||||||||||||||||||||
2859 | LogEst rSize; /* number of rows in the table */ | - | ||||||||||||||||||||||||
2860 | LogEst rLogSize; /* Logarithm of the number of rows in the table */ | - | ||||||||||||||||||||||||
2861 | WhereClause *pWC; /* The parsed WHERE clause */ | - | ||||||||||||||||||||||||
2862 | Table *pTab; /* Table being queried */ | - | ||||||||||||||||||||||||
2863 | - | |||||||||||||||||||||||||
2864 | pNew = pBuilder->pNew; | - | ||||||||||||||||||||||||
2865 | pWInfo = pBuilder->pWInfo; | - | ||||||||||||||||||||||||
2866 | pTabList = pWInfo->pTabList; | - | ||||||||||||||||||||||||
2867 | pSrc = pTabList->a + pNew->iTab; | - | ||||||||||||||||||||||||
2868 | pTab = pSrc->pTab; | - | ||||||||||||||||||||||||
2869 | pWC = pBuilder->pWC; | - | ||||||||||||||||||||||||
2870 | assert( !IsVirtual(pSrc->pTab) ); | - | ||||||||||||||||||||||||
2871 | - | |||||||||||||||||||||||||
2872 | if( pSrc->pIBIndex ){
| 126-259909 | ||||||||||||||||||||||||
2873 | /* An INDEXED BY clause specifies a particular index to use */ | - | ||||||||||||||||||||||||
2874 | pProbe = pSrc->pIBIndex; | - | ||||||||||||||||||||||||
2875 | }else if( !HasRowid(pTab) ){ executed 126 times by 2 tests: end of block Executed by:
| 126-259024 | ||||||||||||||||||||||||
2876 | pProbe = pTab->pIndex; | - | ||||||||||||||||||||||||
2877 | }else{ executed 885 times by 1 test: end of block Executed by:
| 885 | ||||||||||||||||||||||||
2878 | /* There is no INDEXED BY clause. Create a fake Index object in local | - | ||||||||||||||||||||||||
2879 | ** variable sPk to represent the rowid primary key index. Make this | - | ||||||||||||||||||||||||
2880 | ** fake index the first in a chain of Index objects with all of the real | - | ||||||||||||||||||||||||
2881 | ** indices to follow */ | - | ||||||||||||||||||||||||
2882 | Index *pFirst; /* First of real indices on the table */ | - | ||||||||||||||||||||||||
2883 | memset(&sPk, 0, sizeof(Index)); | - | ||||||||||||||||||||||||
2884 | sPk.nKeyCol = 1; | - | ||||||||||||||||||||||||
2885 | sPk.nColumn = 1; | - | ||||||||||||||||||||||||
2886 | sPk.aiColumn = &aiColumnPk; | - | ||||||||||||||||||||||||
2887 | sPk.aiRowLogEst = aiRowEstPk; | - | ||||||||||||||||||||||||
2888 | sPk.onError = OE_Replace; | - | ||||||||||||||||||||||||
2889 | sPk.pTable = pTab; | - | ||||||||||||||||||||||||
2890 | sPk.szIdxRow = pTab->szTabRow; | - | ||||||||||||||||||||||||
2891 | aiRowEstPk[0] = pTab->nRowLogEst; | - | ||||||||||||||||||||||||
2892 | aiRowEstPk[1] = 0; | - | ||||||||||||||||||||||||
2893 | pFirst = pSrc->pTab->pIndex; | - | ||||||||||||||||||||||||
2894 | if( pSrc->fg.notIndexed==0 ){
| 17-259007 | ||||||||||||||||||||||||
2895 | /* The real indices of the table are only considered if the | - | ||||||||||||||||||||||||
2896 | ** NOT INDEXED qualifier is omitted from the FROM clause */ | - | ||||||||||||||||||||||||
2897 | sPk.pNext = pFirst; | - | ||||||||||||||||||||||||
2898 | } executed 259007 times by 435 tests: end of block Executed by:
| 259007 | ||||||||||||||||||||||||
2899 | pProbe = &sPk; | - | ||||||||||||||||||||||||
2900 | } executed 259024 times by 435 tests: end of block Executed by:
| 259024 | ||||||||||||||||||||||||
2901 | rSize = pTab->nRowLogEst; | - | ||||||||||||||||||||||||
2902 | rLogSize = estLog(rSize); | - | ||||||||||||||||||||||||
2903 | - | |||||||||||||||||||||||||
2904 | #ifndef SQLITE_OMIT_AUTOMATIC_INDEX | - | ||||||||||||||||||||||||
2905 | /* Automatic indexes */ | - | ||||||||||||||||||||||||
2906 | if( !pBuilder->pOrSet /* Not part of an OR optimization */
| 19483-240552 | ||||||||||||||||||||||||
2907 | && (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)==0
| 17757-222795 | ||||||||||||||||||||||||
2908 | && (pWInfo->pParse->db->flags & SQLITE_AutoIndex)!=0
| 363-222432 | ||||||||||||||||||||||||
2909 | && pSrc->pIBIndex==0 /* Has no INDEXED BY clause */
| 116-222316 | ||||||||||||||||||||||||
2910 | && !pSrc->fg.notIndexed /* Has no NOT INDEXED clause */
| 13-222303 | ||||||||||||||||||||||||
2911 | && HasRowid(pTab) /* Not WITHOUT ROWID table. (FIXME: Why not?) */
| 828-221475 | ||||||||||||||||||||||||
2912 | && !pSrc->fg.isCorrelated /* Not a correlated subquery */
| 16-221459 | ||||||||||||||||||||||||
2913 | && !pSrc->fg.isRecursive /* Not a recursive common table expression. */
| 276-221183 | ||||||||||||||||||||||||
2914 | ){ | - | ||||||||||||||||||||||||
2915 | /* Generate auto-index WhereLoops */ | - | ||||||||||||||||||||||||
2916 | WhereTerm *pTerm; | - | ||||||||||||||||||||||||
2917 | WhereTerm *pWCEnd = pWC->a + pWC->nTerm; | - | ||||||||||||||||||||||||
2918 | for(pTerm=pWC->a; rc==SQLITE_OK && pTerm<pWCEnd; pTerm++){
| 14-460133 | ||||||||||||||||||||||||
2919 | if( pTerm->prereqRight & pNew->maskSelf ) continue; executed 24143 times by 2 tests: continue; Executed by:
| 24143-214821 | ||||||||||||||||||||||||
2920 | if( termCanDriveIndex(pTerm, pSrc, 0) ){
| 60091-154730 | ||||||||||||||||||||||||
2921 | pNew->u.btree.nEq = 1; | - | ||||||||||||||||||||||||
2922 | pNew->nSkip = 0; | - | ||||||||||||||||||||||||
2923 | pNew->u.btree.pIndex = 0; | - | ||||||||||||||||||||||||
2924 | pNew->nLTerm = 1; | - | ||||||||||||||||||||||||
2925 | pNew->aLTerm[0] = pTerm; | - | ||||||||||||||||||||||||
2926 | /* TUNING: One-time cost for computing the automatic index is | - | ||||||||||||||||||||||||
2927 | ** estimated to be X*N*log2(N) where N is the number of rows in | - | ||||||||||||||||||||||||
2928 | ** the table being indexed and where X is 7 (LogEst=28) for normal | - | ||||||||||||||||||||||||
2929 | ** tables or 0.5 (LogEst=-10) for views and subqueries. The value | - | ||||||||||||||||||||||||
2930 | ** of X is smaller for views and subqueries so that the query planner | - | ||||||||||||||||||||||||
2931 | ** will be more aggressive about generating automatic indexes for | - | ||||||||||||||||||||||||
2932 | ** those objects, since there is no opportunity to add schema | - | ||||||||||||||||||||||||
2933 | ** indexes on subqueries and views. */ | - | ||||||||||||||||||||||||
2934 | pNew->rSetup = rLogSize + rSize; | - | ||||||||||||||||||||||||
2935 | if( pTab->pSelect==0 && (pTab->tabFlags & TF_Ephemeral)==0 ){
| 113-59543 | ||||||||||||||||||||||||
2936 | pNew->rSetup += 28; | - | ||||||||||||||||||||||||
2937 | }else{ executed 59430 times by 33 tests: end of block Executed by:
| 59430 | ||||||||||||||||||||||||
2938 | pNew->rSetup -= 10; | - | ||||||||||||||||||||||||
2939 | } executed 661 times by 1 test: end of block Executed by:
| 661 | ||||||||||||||||||||||||
2940 | ApplyCostMultiplier(pNew->rSetup, pTab->costMult); | - | ||||||||||||||||||||||||
2941 | if( pNew->rSetup<0 ) pNew->rSetup = 0; executed 5 times by 1 test: pNew->rSetup = 0; Executed by:
| 5-60086 | ||||||||||||||||||||||||
2942 | /* TUNING: Each index lookup yields 20 rows in the table. This | - | ||||||||||||||||||||||||
2943 | ** is more than the usual guess of 10 rows, since we have no way | - | ||||||||||||||||||||||||
2944 | ** of knowing how selective the index will ultimately be. It would | - | ||||||||||||||||||||||||
2945 | ** not be unreasonable to make this value much larger. */ | - | ||||||||||||||||||||||||
2946 | pNew->nOut = 43; assert( 43==sqlite3LogEst(20) ); | - | ||||||||||||||||||||||||
2947 | pNew->rRun = sqlite3LogEstAdd(rLogSize,pNew->nOut); | - | ||||||||||||||||||||||||
2948 | pNew->wsFlags = WHERE_AUTO_INDEX; | - | ||||||||||||||||||||||||
2949 | pNew->prereq = mPrereq | pTerm->prereqRight; | - | ||||||||||||||||||||||||
2950 | rc = whereLoopInsert(pBuilder, pNew); | - | ||||||||||||||||||||||||
2951 | } executed 60091 times by 33 tests: end of block Executed by:
| 60091 | ||||||||||||||||||||||||
2952 | } executed 214821 times by 366 tests: end of block Executed by:
| 214821 | ||||||||||||||||||||||||
2953 | } executed 221183 times by 435 tests: end of block Executed by:
| 221183 | ||||||||||||||||||||||||
2954 | #endif /* SQLITE_OMIT_AUTOMATIC_INDEX */ | - | ||||||||||||||||||||||||
2955 | - | |||||||||||||||||||||||||
2956 | /* Loop over all indices. If there was an INDEXED BY clause, then only | - | ||||||||||||||||||||||||
2957 | ** consider index pProbe. */ | - | ||||||||||||||||||||||||
2958 | for(; rc==SQLITE_OK && pProbe;
| 16-747985 | ||||||||||||||||||||||||
2959 | pProbe=(pSrc->pIBIndex ? 0 : pProbe->pNext), iSortIdx++ | - | ||||||||||||||||||||||||
2960 | ){ | - | ||||||||||||||||||||||||
2961 | if( pProbe->pPartIdxWhere!=0
| 158-487851 | ||||||||||||||||||||||||
2962 | && !whereUsablePartialIndex(pSrc->iCursor, pWC, pProbe->pPartIdxWhere) ){
| 45-113 | ||||||||||||||||||||||||
2963 | testcase( pNew->iTab!=pSrc->iCursor ); /* See ticket [98d973b8f5] */ | - | ||||||||||||||||||||||||
2964 | continue; /* Partial index inappropriate for this query */ executed 113 times by 1 test: continue; Executed by:
| 113 | ||||||||||||||||||||||||
2965 | } | - | ||||||||||||||||||||||||
2966 | if( pProbe->bNoQuery ) continue; executed 7 times by 1 test: continue; Executed by:
| 7-487889 | ||||||||||||||||||||||||
2967 | rSize = pProbe->aiRowLogEst[0]; | - | ||||||||||||||||||||||||
2968 | pNew->u.btree.nEq = 0; | - | ||||||||||||||||||||||||
2969 | pNew->u.btree.nBtm = 0; | - | ||||||||||||||||||||||||
2970 | pNew->u.btree.nTop = 0; | - | ||||||||||||||||||||||||
2971 | pNew->nSkip = 0; | - | ||||||||||||||||||||||||
2972 | pNew->nLTerm = 0; | - | ||||||||||||||||||||||||
2973 | pNew->iSortIdx = 0; | - | ||||||||||||||||||||||||
2974 | pNew->rSetup = 0; | - | ||||||||||||||||||||||||
2975 | pNew->prereq = mPrereq; | - | ||||||||||||||||||||||||
2976 | pNew->nOut = rSize; | - | ||||||||||||||||||||||||
2977 | pNew->u.btree.pIndex = pProbe; | - | ||||||||||||||||||||||||
2978 | b = indexMightHelpWithOrderBy(pBuilder, pProbe, pSrc->iCursor); | - | ||||||||||||||||||||||||
2979 | /* The ONEPASS_DESIRED flags never occurs together with ORDER BY */ | - | ||||||||||||||||||||||||
2980 | assert( (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || b==0 ); | - | ||||||||||||||||||||||||
2981 | if( pProbe->tnum<=0 ){
| 228879-259010 | ||||||||||||||||||||||||
2982 | /* Integer primary key index */ | - | ||||||||||||||||||||||||
2983 | pNew->wsFlags = WHERE_IPK; | - | ||||||||||||||||||||||||
2984 | - | |||||||||||||||||||||||||
2985 | /* Full table scan */ | - | ||||||||||||||||||||||||
2986 | pNew->iSortIdx = b ? iSortIdx : 0;
| 46448-212562 | ||||||||||||||||||||||||
2987 | /* TUNING: Cost of full table scan is (N*3.0). */ | - | ||||||||||||||||||||||||
2988 | pNew->rRun = rSize + 16; | - | ||||||||||||||||||||||||
2989 | ApplyCostMultiplier(pNew->rRun, pTab->costMult); | - | ||||||||||||||||||||||||
2990 | whereLoopOutputAdjust(pWC, pNew, rSize); | - | ||||||||||||||||||||||||
2991 | rc = whereLoopInsert(pBuilder, pNew); | - | ||||||||||||||||||||||||
2992 | pNew->nOut = rSize; | - | ||||||||||||||||||||||||
2993 | if( rc ) break; executed 43 times by 1 test: break; Executed by:
| 43-258967 | ||||||||||||||||||||||||
2994 | }else{ executed 258967 times by 435 tests: end of block Executed by:
| 258967 | ||||||||||||||||||||||||
2995 | Bitmask m; | - | ||||||||||||||||||||||||
2996 | if( pProbe->isCovering ){
| 3870-225009 | ||||||||||||||||||||||||
2997 | pNew->wsFlags = WHERE_IDX_ONLY | WHERE_INDEXED; | - | ||||||||||||||||||||||||
2998 | m = 0; | - | ||||||||||||||||||||||||
2999 | }else{ executed 3870 times by 6 tests: end of block Executed by:
| 3870 | ||||||||||||||||||||||||
3000 | m = pSrc->colUsed & pProbe->colNotIdxed; | - | ||||||||||||||||||||||||
3001 | pNew->wsFlags = (m==0) ? (WHERE_IDX_ONLY|WHERE_INDEXED) : WHERE_INDEXED;
| 20265-204744 | ||||||||||||||||||||||||
3002 | } executed 225009 times by 351 tests: end of block Executed by:
| 225009 | ||||||||||||||||||||||||
3003 | - | |||||||||||||||||||||||||
3004 | /* Full scan via index */ | - | ||||||||||||||||||||||||
3005 | if( b
| 16895-211984 | ||||||||||||||||||||||||
3006 | || !HasRowid(pTab)
| 1103-210881 | ||||||||||||||||||||||||
3007 | || pProbe->pPartIdxWhere!=0
| 19-210862 | ||||||||||||||||||||||||
3008 | || ( m==0
| 17958-192904 | ||||||||||||||||||||||||
3009 | && pProbe->bUnordered==0
| 1-17957 | ||||||||||||||||||||||||
3010 | && (pProbe->szIdxRow<pTab->szTabRow)
| 4197-13760 | ||||||||||||||||||||||||
3011 | && (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0
| 632-13128 | ||||||||||||||||||||||||
3012 | && sqlite3GlobalConfig.bUseCis
| 3-13125 | ||||||||||||||||||||||||
3013 | && OptimizationEnabled(pWInfo->pParse->db, SQLITE_CoverIdxScan)
| 34-13091 | ||||||||||||||||||||||||
3014 | ) | - | ||||||||||||||||||||||||
3015 | ){ | - | ||||||||||||||||||||||||
3016 | pNew->iSortIdx = b ? iSortIdx : 0;
| 14213-16895 | ||||||||||||||||||||||||
3017 | - | |||||||||||||||||||||||||
3018 | /* The cost of visiting the index rows is N*K, where K is | - | ||||||||||||||||||||||||
3019 | ** between 1.1 and 3.0, depending on the relative sizes of the | - | ||||||||||||||||||||||||
3020 | ** index and table rows. */ | - | ||||||||||||||||||||||||
3021 | pNew->rRun = rSize + 1 + (15*pProbe->szIdxRow)/pTab->szTabRow; | - | ||||||||||||||||||||||||
3022 | if( m!=0 ){
| 11840-19268 | ||||||||||||||||||||||||
3023 | /* If this is a non-covering index scan, add in the cost of | - | ||||||||||||||||||||||||
3024 | ** doing table lookups. The cost will be 3x the number of | - | ||||||||||||||||||||||||
3025 | ** lookups. Take into account WHERE clause terms that can be | - | ||||||||||||||||||||||||
3026 | ** satisfied using just the index, and that do not require a | - | ||||||||||||||||||||||||
3027 | ** table lookup. */ | - | ||||||||||||||||||||||||
3028 | LogEst nLookup = rSize + 16; /* Base cost: N*3 */ | - | ||||||||||||||||||||||||
3029 | int ii; | - | ||||||||||||||||||||||||
3030 | int iCur = pSrc->iCursor; | - | ||||||||||||||||||||||||
3031 | WhereClause *pWC2 = &pWInfo->sWC; | - | ||||||||||||||||||||||||
3032 | for(ii=0; ii<pWC2->nTerm; ii++){
| 3624-13184 | ||||||||||||||||||||||||
3033 | WhereTerm *pTerm = &pWC2->a[ii]; | - | ||||||||||||||||||||||||
3034 | if( !sqlite3ExprCoveredByIndex(pTerm->pExpr, iCur, pProbe) ){
| 4968-8216 | ||||||||||||||||||||||||
3035 | break; executed 8216 times by 1 test: break; Executed by:
| 8216 | ||||||||||||||||||||||||
3036 | } | - | ||||||||||||||||||||||||
3037 | /* pTerm can be evaluated using just the index. So reduce | - | ||||||||||||||||||||||||
3038 | ** the expected number of table lookups accordingly */ | - | ||||||||||||||||||||||||
3039 | if( pTerm->truthProb<=0 ){
| 0-4968 | ||||||||||||||||||||||||
3040 | nLookup += pTerm->truthProb; | - | ||||||||||||||||||||||||
3041 | }else{ never executed: end of block | 0 | ||||||||||||||||||||||||
3042 | nLookup--; | - | ||||||||||||||||||||||||
3043 | if( pTerm->eOperator & (WO_EQ|WO_IS) ) nLookup -= 19; executed 1434 times by 1 test: nLookup -= 19; Executed by:
| 1434-3534 | ||||||||||||||||||||||||
3044 | } executed 4968 times by 1 test: end of block Executed by:
| 4968 | ||||||||||||||||||||||||
3045 | } | - | ||||||||||||||||||||||||
3046 | - | |||||||||||||||||||||||||
3047 | pNew->rRun = sqlite3LogEstAdd(pNew->rRun, nLookup); | - | ||||||||||||||||||||||||
3048 | } executed 11840 times by 2 tests: end of block Executed by:
| 11840 | ||||||||||||||||||||||||
3049 | ApplyCostMultiplier(pNew->rRun, pTab->costMult); | - | ||||||||||||||||||||||||
3050 | whereLoopOutputAdjust(pWC, pNew, rSize); | - | ||||||||||||||||||||||||
3051 | rc = whereLoopInsert(pBuilder, pNew); | - | ||||||||||||||||||||||||
3052 | pNew->nOut = rSize; | - | ||||||||||||||||||||||||
3053 | if( rc ) break; never executed: break;
| 0-31108 | ||||||||||||||||||||||||
3054 | } executed 31108 times by 2 tests: end of block Executed by:
| 31108 | ||||||||||||||||||||||||
3055 | } executed 228879 times by 356 tests: end of block Executed by:
| 228879 | ||||||||||||||||||||||||
3056 | - | |||||||||||||||||||||||||
3057 | pBuilder->bldFlags = 0; | - | ||||||||||||||||||||||||
3058 | rc = whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, 0); | - | ||||||||||||||||||||||||
3059 | if( pBuilder->bldFlags==SQLITE_BLDF_INDEXED ){
| 42557-445289 | ||||||||||||||||||||||||
3060 | /* If a non-unique index is used, or if a prefix of the key for | - | ||||||||||||||||||||||||
3061 | ** unique index is used (making the index functionally non-unique) | - | ||||||||||||||||||||||||
3062 | ** then the sqlite_stat1 data becomes important for scoring the | - | ||||||||||||||||||||||||
3063 | ** plan */ | - | ||||||||||||||||||||||||
3064 | pTab->tabFlags |= TF_StatsUsed; | - | ||||||||||||||||||||||||
3065 | } executed 42557 times by 1 test: end of block Executed by:
| 42557 | ||||||||||||||||||||||||
3066 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 | - | ||||||||||||||||||||||||
3067 | sqlite3Stat4ProbeFree(pBuilder->pRec); | - | ||||||||||||||||||||||||
3068 | pBuilder->nRecValid = 0; | - | ||||||||||||||||||||||||
3069 | pBuilder->pRec = 0; | - | ||||||||||||||||||||||||
3070 | #endif | - | ||||||||||||||||||||||||
3071 | } executed 487846 times by 435 tests: end of block Executed by:
| 487846 | ||||||||||||||||||||||||
3072 | return rc; executed 260035 times by 435 tests: return rc; Executed by:
| 260035 | ||||||||||||||||||||||||
3073 | } | - | ||||||||||||||||||||||||
3074 | - | |||||||||||||||||||||||||
3075 | #ifndef SQLITE_OMIT_VIRTUALTABLE | - | ||||||||||||||||||||||||
3076 | - | |||||||||||||||||||||||||
3077 | /* | - | ||||||||||||||||||||||||
3078 | ** Argument pIdxInfo is already populated with all constraints that may | - | ||||||||||||||||||||||||
3079 | ** be used by the virtual table identified by pBuilder->pNew->iTab. This | - | ||||||||||||||||||||||||
3080 | ** function marks a subset of those constraints usable, invokes the | - | ||||||||||||||||||||||||
3081 | ** xBestIndex method and adds the returned plan to pBuilder. | - | ||||||||||||||||||||||||
3082 | ** | - | ||||||||||||||||||||||||
3083 | ** A constraint is marked usable if: | - | ||||||||||||||||||||||||
3084 | ** | - | ||||||||||||||||||||||||
3085 | ** * Argument mUsable indicates that its prerequisites are available, and | - | ||||||||||||||||||||||||
3086 | ** | - | ||||||||||||||||||||||||
3087 | ** * It is not one of the operators specified in the mExclude mask passed | - | ||||||||||||||||||||||||
3088 | ** as the fourth argument (which in practice is either WO_IN or 0). | - | ||||||||||||||||||||||||
3089 | ** | - | ||||||||||||||||||||||||
3090 | ** Argument mPrereq is a mask of tables that must be scanned before the | - | ||||||||||||||||||||||||
3091 | ** virtual table in question. These are added to the plans prerequisites | - | ||||||||||||||||||||||||
3092 | ** before it is added to pBuilder. | - | ||||||||||||||||||||||||
3093 | ** | - | ||||||||||||||||||||||||
3094 | ** Output parameter *pbIn is set to true if the plan added to pBuilder | - | ||||||||||||||||||||||||
3095 | ** uses one or more WO_IN terms, or false otherwise. | - | ||||||||||||||||||||||||
3096 | */ | - | ||||||||||||||||||||||||
3097 | static int whereLoopAddVirtualOne( | - | ||||||||||||||||||||||||
3098 | WhereLoopBuilder *pBuilder, | - | ||||||||||||||||||||||||
3099 | Bitmask mPrereq, /* Mask of tables that must be used. */ | - | ||||||||||||||||||||||||
3100 | Bitmask mUsable, /* Mask of usable tables */ | - | ||||||||||||||||||||||||
3101 | u16 mExclude, /* Exclude terms using these operators */ | - | ||||||||||||||||||||||||
3102 | sqlite3_index_info *pIdxInfo, /* Populated object for xBestIndex */ | - | ||||||||||||||||||||||||
3103 | u16 mNoOmit, /* Do not omit these constraints */ | - | ||||||||||||||||||||||||
3104 | int *pbIn /* OUT: True if plan uses an IN(...) op */ | - | ||||||||||||||||||||||||
3105 | ){ | - | ||||||||||||||||||||||||
3106 | WhereClause *pWC = pBuilder->pWC; | - | ||||||||||||||||||||||||
3107 | struct sqlite3_index_constraint *pIdxCons; | - | ||||||||||||||||||||||||
3108 | struct sqlite3_index_constraint_usage *pUsage = pIdxInfo->aConstraintUsage; | - | ||||||||||||||||||||||||
3109 | int i; | - | ||||||||||||||||||||||||
3110 | int mxTerm; | - | ||||||||||||||||||||||||
3111 | int rc = SQLITE_OK; | - | ||||||||||||||||||||||||
3112 | WhereLoop *pNew = pBuilder->pNew; | - | ||||||||||||||||||||||||
3113 | Parse *pParse = pBuilder->pWInfo->pParse; | - | ||||||||||||||||||||||||
3114 | struct SrcList_item *pSrc = &pBuilder->pWInfo->pTabList->a[pNew->iTab]; | - | ||||||||||||||||||||||||
3115 | int nConstraint = pIdxInfo->nConstraint; | - | ||||||||||||||||||||||||
3116 | - | |||||||||||||||||||||||||
3117 | assert( (mUsable & mPrereq)==mPrereq ); | - | ||||||||||||||||||||||||
3118 | *pbIn = 0; | - | ||||||||||||||||||||||||
3119 | pNew->prereq = mPrereq; | - | ||||||||||||||||||||||||
3120 | - | |||||||||||||||||||||||||
3121 | /* Set the usable flag on the subset of constraints identified by | - | ||||||||||||||||||||||||
3122 | ** arguments mUsable and mExclude. */ | - | ||||||||||||||||||||||||
3123 | pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint; | - | ||||||||||||||||||||||||
3124 | for(i=0; i<nConstraint; i++, pIdxCons++){
| 12102-21068 | ||||||||||||||||||||||||
3125 | WhereTerm *pTerm = &pWC->a[pIdxCons->iTermOffset]; | - | ||||||||||||||||||||||||
3126 | pIdxCons->usable = 0; | - | ||||||||||||||||||||||||
3127 | if( (pTerm->prereqRight & mUsable)==pTerm->prereqRight
| 643-20425 | ||||||||||||||||||||||||
3128 | && (pTerm->eOperator & mExclude)==0
| 0-20425 | ||||||||||||||||||||||||
3129 | ){ | - | ||||||||||||||||||||||||
3130 | pIdxCons->usable = 1; | - | ||||||||||||||||||||||||
3131 | } executed 20425 times by 1 test: end of block Executed by:
| 20425 | ||||||||||||||||||||||||
3132 | } executed 21068 times by 1 test: end of block Executed by:
| 21068 | ||||||||||||||||||||||||
3133 | - | |||||||||||||||||||||||||
3134 | /* Initialize the output fields of the sqlite3_index_info structure */ | - | ||||||||||||||||||||||||
3135 | memset(pUsage, 0, sizeof(pUsage[0])*nConstraint); | - | ||||||||||||||||||||||||
3136 | assert( pIdxInfo->needToFreeIdxStr==0 ); | - | ||||||||||||||||||||||||
3137 | pIdxInfo->idxStr = 0; | - | ||||||||||||||||||||||||
3138 | pIdxInfo->idxNum = 0; | - | ||||||||||||||||||||||||
3139 | pIdxInfo->orderByConsumed = 0; | - | ||||||||||||||||||||||||
3140 | pIdxInfo->estimatedCost = SQLITE_BIG_DBL / (double)2; | - | ||||||||||||||||||||||||
3141 | pIdxInfo->estimatedRows = 25; | - | ||||||||||||||||||||||||
3142 | pIdxInfo->idxFlags = 0; | - | ||||||||||||||||||||||||
3143 | pIdxInfo->colUsed = (sqlite3_int64)pSrc->colUsed; | - | ||||||||||||||||||||||||
3144 | - | |||||||||||||||||||||||||
3145 | /* Invoke the virtual table xBestIndex() method */ | - | ||||||||||||||||||||||||
3146 | rc = vtabBestIndex(pParse, pSrc->pTab, pIdxInfo); | - | ||||||||||||||||||||||||
3147 | if( rc ) return rc; executed 2 times by 1 test: return rc; Executed by:
| 2-12100 | ||||||||||||||||||||||||
3148 | - | |||||||||||||||||||||||||
3149 | mxTerm = -1; | - | ||||||||||||||||||||||||
3150 | assert( pNew->nLSlot>=nConstraint ); | - | ||||||||||||||||||||||||
3151 | for(i=0; i<nConstraint; i++) pNew->aLTerm[i] = 0; executed 21064 times by 1 test: pNew->aLTerm[i] = 0; Executed by:
| 12100-21064 | ||||||||||||||||||||||||
3152 | pNew->u.vtab.omitMask = 0; | - | ||||||||||||||||||||||||
3153 | pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint; | - | ||||||||||||||||||||||||
3154 | for(i=0; i<nConstraint; i++, pIdxCons++){
| 11834-21064 | ||||||||||||||||||||||||
3155 | int iTerm; | - | ||||||||||||||||||||||||
3156 | if( (iTerm = pUsage[i].argvIndex - 1)>=0 ){
| 1988-19076 | ||||||||||||||||||||||||
3157 | WhereTerm *pTerm; | - | ||||||||||||||||||||||||
3158 | int j = pIdxCons->iTermOffset; | - | ||||||||||||||||||||||||
3159 | if( iTerm>=nConstraint
| 0-19076 | ||||||||||||||||||||||||
3160 | || j<0
| 0-19076 | ||||||||||||||||||||||||
3161 | || j>=pWC->nTerm
| 0-19076 | ||||||||||||||||||||||||
3162 | || pNew->aLTerm[iTerm]!=0
| 0-19076 | ||||||||||||||||||||||||
3163 | || pIdxCons->usable==0
| 266-18810 | ||||||||||||||||||||||||
3164 | ){ | - | ||||||||||||||||||||||||
3165 | sqlite3ErrorMsg(pParse,"%s.xBestIndex malfunction",pSrc->pTab->zName); | - | ||||||||||||||||||||||||
3166 | testcase( pIdxInfo->needToFreeIdxStr ); | - | ||||||||||||||||||||||||
3167 | return SQLITE_ERROR; executed 266 times by 1 test: return 1; Executed by:
| 266 | ||||||||||||||||||||||||
3168 | } | - | ||||||||||||||||||||||||
3169 | testcase( iTerm==nConstraint-1 ); | - | ||||||||||||||||||||||||
3170 | testcase( j==0 ); | - | ||||||||||||||||||||||||
3171 | testcase( j==pWC->nTerm-1 ); | - | ||||||||||||||||||||||||
3172 | pTerm = &pWC->a[j]; | - | ||||||||||||||||||||||||
3173 | pNew->prereq |= pTerm->prereqRight; | - | ||||||||||||||||||||||||
3174 | assert( iTerm<pNew->nLSlot ); | - | ||||||||||||||||||||||||
3175 | pNew->aLTerm[iTerm] = pTerm; | - | ||||||||||||||||||||||||
3176 | if( iTerm>mxTerm ) mxTerm = iTerm; executed 18759 times by 1 test: mxTerm = iTerm; Executed by:
| 51-18759 | ||||||||||||||||||||||||
3177 | testcase( iTerm==15 ); | - | ||||||||||||||||||||||||
3178 | testcase( iTerm==16 ); | - | ||||||||||||||||||||||||
3179 | if( iTerm<16 && pUsage[i].omit ) pNew->u.vtab.omitMask |= 1<<iTerm; executed 17824 times by 1 test: pNew->u.vtab.omitMask |= 1<<iTerm; Executed by:
| 0-18810 | ||||||||||||||||||||||||
3180 | if( (pTerm->eOperator & WO_IN)!=0 ){
| 21-18789 | ||||||||||||||||||||||||
3181 | /* A virtual table that is constrained by an IN clause may not | - | ||||||||||||||||||||||||
3182 | ** consume the ORDER BY clause because (1) the order of IN terms | - | ||||||||||||||||||||||||
3183 | ** is not necessarily related to the order of output terms and | - | ||||||||||||||||||||||||
3184 | ** (2) Multiple outputs from a single IN value will not merge | - | ||||||||||||||||||||||||
3185 | ** together. */ | - | ||||||||||||||||||||||||
3186 | pIdxInfo->orderByConsumed = 0; | - | ||||||||||||||||||||||||
3187 | pIdxInfo->idxFlags &= ~SQLITE_INDEX_SCAN_UNIQUE; | - | ||||||||||||||||||||||||
3188 | *pbIn = 1; assert( (mExclude & WO_IN)==0 ); | - | ||||||||||||||||||||||||
3189 | } executed 21 times by 1 test: end of block Executed by:
| 21 | ||||||||||||||||||||||||
3190 | } executed 18810 times by 1 test: end of block Executed by:
| 18810 | ||||||||||||||||||||||||
3191 | } executed 20798 times by 1 test: end of block Executed by:
| 20798 | ||||||||||||||||||||||||
3192 | pNew->u.vtab.omitMask &= ~mNoOmit; | - | ||||||||||||||||||||||||
3193 | - | |||||||||||||||||||||||||
3194 | pNew->nLTerm = mxTerm+1; | - | ||||||||||||||||||||||||
3195 | for(i=0; i<=mxTerm; i++){
| 11834-18810 | ||||||||||||||||||||||||
3196 | if( pNew->aLTerm[i]==0 ){
| 0-18810 | ||||||||||||||||||||||||
3197 | /* The non-zero argvIdx values must be contiguous. Raise an | - | ||||||||||||||||||||||||
3198 | ** error if they are not */ | - | ||||||||||||||||||||||||
3199 | sqlite3ErrorMsg(pParse,"%s.xBestIndex malfunction",pSrc->pTab->zName); | - | ||||||||||||||||||||||||
3200 | testcase( pIdxInfo->needToFreeIdxStr ); | - | ||||||||||||||||||||||||
3201 | return SQLITE_ERROR; never executed: return 1; | 0 | ||||||||||||||||||||||||
3202 | } | - | ||||||||||||||||||||||||
3203 | } executed 18810 times by 1 test: end of block Executed by:
| 18810 | ||||||||||||||||||||||||
3204 | assert( pNew->nLTerm<=pNew->nLSlot ); | - | ||||||||||||||||||||||||
3205 | pNew->u.vtab.idxNum = pIdxInfo->idxNum; | - | ||||||||||||||||||||||||
3206 | pNew->u.vtab.needFree = pIdxInfo->needToFreeIdxStr; | - | ||||||||||||||||||||||||
3207 | pIdxInfo->needToFreeIdxStr = 0; | - | ||||||||||||||||||||||||
3208 | pNew->u.vtab.idxStr = pIdxInfo->idxStr; | - | ||||||||||||||||||||||||
3209 | pNew->u.vtab.isOrdered = (i8)(pIdxInfo->orderByConsumed ?
| 16-11818 | ||||||||||||||||||||||||
3210 | pIdxInfo->nOrderBy : 0); | - | ||||||||||||||||||||||||
3211 | pNew->rSetup = 0; | - | ||||||||||||||||||||||||
3212 | pNew->rRun = sqlite3LogEstFromDouble(pIdxInfo->estimatedCost); | - | ||||||||||||||||||||||||
3213 | pNew->nOut = sqlite3LogEst(pIdxInfo->estimatedRows); | - | ||||||||||||||||||||||||
3214 | - | |||||||||||||||||||||||||
3215 | /* Set the WHERE_ONEROW flag if the xBestIndex() method indicated | - | ||||||||||||||||||||||||
3216 | ** that the scan will visit at most one row. Clear it otherwise. */ | - | ||||||||||||||||||||||||
3217 | if( pIdxInfo->idxFlags & SQLITE_INDEX_SCAN_UNIQUE ){
| 33-11801 | ||||||||||||||||||||||||
3218 | pNew->wsFlags |= WHERE_ONEROW; | - | ||||||||||||||||||||||||
3219 | }else{ executed 33 times by 1 test: end of block Executed by:
| 33 | ||||||||||||||||||||||||
3220 | pNew->wsFlags &= ~WHERE_ONEROW; | - | ||||||||||||||||||||||||
3221 | } executed 11801 times by 1 test: end of block Executed by:
| 11801 | ||||||||||||||||||||||||
3222 | rc = whereLoopInsert(pBuilder, pNew); | - | ||||||||||||||||||||||||
3223 | if( pNew->u.vtab.needFree ){
| 41-11793 | ||||||||||||||||||||||||
3224 | sqlite3_free(pNew->u.vtab.idxStr); | - | ||||||||||||||||||||||||
3225 | pNew->u.vtab.needFree = 0; | - | ||||||||||||||||||||||||
3226 | } executed 41 times by 1 test: end of block Executed by:
| 41 | ||||||||||||||||||||||||
3227 | WHERETRACE(0xffff, (" bIn=%d prereqIn=%04llx prereqOut=%04llx\n", | - | ||||||||||||||||||||||||
3228 | *pbIn, (sqlite3_uint64)mPrereq, | - | ||||||||||||||||||||||||
3229 | (sqlite3_uint64)(pNew->prereq & ~mPrereq))); | - | ||||||||||||||||||||||||
3230 | - | |||||||||||||||||||||||||
3231 | return rc; executed 11834 times by 1 test: return rc; Executed by:
| 11834 | ||||||||||||||||||||||||
3232 | } | - | ||||||||||||||||||||||||
3233 | - | |||||||||||||||||||||||||
3234 | /* | - | ||||||||||||||||||||||||
3235 | ** If this function is invoked from within an xBestIndex() callback, it | - | ||||||||||||||||||||||||
3236 | ** returns a pointer to a buffer containing the name of the collation | - | ||||||||||||||||||||||||
3237 | ** sequence associated with element iCons of the sqlite3_index_info.aConstraint | - | ||||||||||||||||||||||||
3238 | ** array. Or, if iCons is out of range or there is no active xBestIndex | - | ||||||||||||||||||||||||
3239 | ** call, return NULL. | - | ||||||||||||||||||||||||
3240 | */ | - | ||||||||||||||||||||||||
3241 | const char *sqlite3_vtab_collation(sqlite3_index_info *pIdxInfo, int iCons){ | - | ||||||||||||||||||||||||
3242 | HiddenIndexInfo *pHidden = (HiddenIndexInfo*)&pIdxInfo[1]; | - | ||||||||||||||||||||||||
3243 | const char *zRet = 0; | - | ||||||||||||||||||||||||
3244 | if( iCons>=0 && iCons<pIdxInfo->nConstraint ){
| 0-29 | ||||||||||||||||||||||||
3245 | CollSeq *pC = 0; | - | ||||||||||||||||||||||||
3246 | int iTerm = pIdxInfo->aConstraint[iCons].iTermOffset; | - | ||||||||||||||||||||||||
3247 | Expr *pX = pHidden->pWC->a[iTerm].pExpr; | - | ||||||||||||||||||||||||
3248 | if( pX->pLeft ){
| 1-28 | ||||||||||||||||||||||||
3249 | pC = sqlite3BinaryCompareCollSeq(pHidden->pParse, pX->pLeft, pX->pRight); | - | ||||||||||||||||||||||||
3250 | } executed 28 times by 1 test: end of block Executed by:
| 28 | ||||||||||||||||||||||||
3251 | zRet = (pC ? pC->zName : sqlite3StrBINARY);
| 1-28 | ||||||||||||||||||||||||
3252 | } executed 29 times by 1 test: end of block Executed by:
| 29 | ||||||||||||||||||||||||
3253 | return zRet; executed 29 times by 1 test: return zRet; Executed by:
| 29 | ||||||||||||||||||||||||
3254 | } | - | ||||||||||||||||||||||||
3255 | - | |||||||||||||||||||||||||
3256 | /* | - | ||||||||||||||||||||||||
3257 | ** Add all WhereLoop objects for a table of the join identified by | - | ||||||||||||||||||||||||
3258 | ** pBuilder->pNew->iTab. That table is guaranteed to be a virtual table. | - | ||||||||||||||||||||||||
3259 | ** | - | ||||||||||||||||||||||||
3260 | ** If there are no LEFT or CROSS JOIN joins in the query, both mPrereq and | - | ||||||||||||||||||||||||
3261 | ** mUnusable are set to 0. Otherwise, mPrereq is a mask of all FROM clause | - | ||||||||||||||||||||||||
3262 | ** entries that occur before the virtual table in the FROM clause and are | - | ||||||||||||||||||||||||
3263 | ** separated from it by at least one LEFT or CROSS JOIN. Similarly, the | - | ||||||||||||||||||||||||
3264 | ** mUnusable mask contains all FROM clause entries that occur after the | - | ||||||||||||||||||||||||
3265 | ** virtual table and are separated from it by at least one LEFT or | - | ||||||||||||||||||||||||
3266 | ** CROSS JOIN. | - | ||||||||||||||||||||||||
3267 | ** | - | ||||||||||||||||||||||||
3268 | ** For example, if the query were: | - | ||||||||||||||||||||||||
3269 | ** | - | ||||||||||||||||||||||||
3270 | ** ... FROM t1, t2 LEFT JOIN t3, t4, vt CROSS JOIN t5, t6; | - | ||||||||||||||||||||||||
3271 | ** | - | ||||||||||||||||||||||||
3272 | ** then mPrereq corresponds to (t1, t2) and mUnusable to (t5, t6). | - | ||||||||||||||||||||||||
3273 | ** | - | ||||||||||||||||||||||||
3274 | ** All the tables in mPrereq must be scanned before the current virtual | - | ||||||||||||||||||||||||
3275 | ** table. So any terms for which all prerequisites are satisfied by | - | ||||||||||||||||||||||||
3276 | ** mPrereq may be specified as "usable" in all calls to xBestIndex. | - | ||||||||||||||||||||||||
3277 | ** Conversely, all tables in mUnusable must be scanned after the current | - | ||||||||||||||||||||||||
3278 | ** virtual table, so any terms for which the prerequisites overlap with | - | ||||||||||||||||||||||||
3279 | ** mUnusable should always be configured as "not-usable" for xBestIndex. | - | ||||||||||||||||||||||||
3280 | */ | - | ||||||||||||||||||||||||
3281 | static int whereLoopAddVirtual( | - | ||||||||||||||||||||||||
3282 | WhereLoopBuilder *pBuilder, /* WHERE clause information */ | - | ||||||||||||||||||||||||
3283 | Bitmask mPrereq, /* Tables that must be scanned before this one */ | - | ||||||||||||||||||||||||
3284 | Bitmask mUnusable /* Tables that must be scanned after this one */ | - | ||||||||||||||||||||||||
3285 | ){ | - | ||||||||||||||||||||||||
3286 | int rc = SQLITE_OK; /* Return code */ | - | ||||||||||||||||||||||||
3287 | WhereInfo *pWInfo; /* WHERE analysis context */ | - | ||||||||||||||||||||||||
3288 | Parse *pParse; /* The parsing context */ | - | ||||||||||||||||||||||||
3289 | WhereClause *pWC; /* The WHERE clause */ | - | ||||||||||||||||||||||||
3290 | struct SrcList_item *pSrc; /* The FROM clause term to search */ | - | ||||||||||||||||||||||||
3291 | sqlite3_index_info *p; /* Object to pass to xBestIndex() */ | - | ||||||||||||||||||||||||
3292 | int nConstraint; /* Number of constraints in p */ | - | ||||||||||||||||||||||||
3293 | int bIn; /* True if plan uses IN(...) operator */ | - | ||||||||||||||||||||||||
3294 | WhereLoop *pNew; | - | ||||||||||||||||||||||||
3295 | Bitmask mBest; /* Tables used by best possible plan */ | - | ||||||||||||||||||||||||
3296 | u16 mNoOmit; | - | ||||||||||||||||||||||||
3297 | - | |||||||||||||||||||||||||
3298 | assert( (mPrereq & mUnusable)==0 ); | - | ||||||||||||||||||||||||
3299 | pWInfo = pBuilder->pWInfo; | - | ||||||||||||||||||||||||
3300 | pParse = pWInfo->pParse; | - | ||||||||||||||||||||||||
3301 | pWC = pBuilder->pWC; | - | ||||||||||||||||||||||||
3302 | pNew = pBuilder->pNew; | - | ||||||||||||||||||||||||
3303 | pSrc = &pWInfo->pTabList->a[pNew->iTab]; | - | ||||||||||||||||||||||||
3304 | assert( IsVirtual(pSrc->pTab) ); | - | ||||||||||||||||||||||||
3305 | p = allocateIndexInfo(pParse, pWC, mUnusable, pSrc, pBuilder->pOrderBy, | - | ||||||||||||||||||||||||
3306 | &mNoOmit); | - | ||||||||||||||||||||||||
3307 | if( p==0 ) return SQLITE_NOMEM_BKPT; never executed: return 7;
| 0-11464 | ||||||||||||||||||||||||
3308 | pNew->rSetup = 0; | - | ||||||||||||||||||||||||
3309 | pNew->wsFlags = WHERE_VIRTUALTABLE; | - | ||||||||||||||||||||||||
3310 | pNew->nLTerm = 0; | - | ||||||||||||||||||||||||
3311 | pNew->u.vtab.needFree = 0; | - | ||||||||||||||||||||||||
3312 | nConstraint = p->nConstraint; | - | ||||||||||||||||||||||||
3313 | if( whereLoopResize(pParse->db, pNew, nConstraint) ){
| 0-11464 | ||||||||||||||||||||||||
3314 | sqlite3DbFree(pParse->db, p); | - | ||||||||||||||||||||||||
3315 | return SQLITE_NOMEM_BKPT; never executed: return 7; | 0 | ||||||||||||||||||||||||
3316 | } | - | ||||||||||||||||||||||||
3317 | - | |||||||||||||||||||||||||
3318 | /* First call xBestIndex() with all constraints usable. */ | - | ||||||||||||||||||||||||
3319 | WHERETRACE(0x800, ("BEGIN %s.addVirtual()\n", pSrc->pTab->zName)); | - | ||||||||||||||||||||||||
3320 | WHERETRACE(0x40, (" VirtualOne: all usable\n")); | - | ||||||||||||||||||||||||
3321 | rc = whereLoopAddVirtualOne(pBuilder, mPrereq, ALLBITS, 0, p, mNoOmit, &bIn); | - | ||||||||||||||||||||||||
3322 | - | |||||||||||||||||||||||||
3323 | /* If the call to xBestIndex() with all terms enabled produced a plan | - | ||||||||||||||||||||||||
3324 | ** that does not require any source tables (IOW: a plan with mBest==0), | - | ||||||||||||||||||||||||
3325 | ** then there is no point in making any further calls to xBestIndex() | - | ||||||||||||||||||||||||
3326 | ** since they will all return the same result (if the xBestIndex() | - | ||||||||||||||||||||||||
3327 | ** implementation is sane). */ | - | ||||||||||||||||||||||||
3328 | if( rc==SQLITE_OK && (mBest = (pNew->prereq & ~mPrereq))!=0 ){
| 2-11462 | ||||||||||||||||||||||||
3329 | int seenZero = 0; /* True if a plan with no prereqs seen */ | - | ||||||||||||||||||||||||
3330 | int seenZeroNoIN = 0; /* Plan with no prereqs and no IN(...) seen */ | - | ||||||||||||||||||||||||
3331 | Bitmask mPrev = 0; | - | ||||||||||||||||||||||||
3332 | Bitmask mBestNoIn = 0; | - | ||||||||||||||||||||||||
3333 | - | |||||||||||||||||||||||||
3334 | /* If the plan produced by the earlier call uses an IN(...) term, call | - | ||||||||||||||||||||||||
3335 | ** xBestIndex again, this time with IN(...) terms disabled. */ | - | ||||||||||||||||||||||||
3336 | if( bIn ){
| 0-633 | ||||||||||||||||||||||||
3337 | WHERETRACE(0x40, (" VirtualOne: all usable w/o IN\n")); | - | ||||||||||||||||||||||||
3338 | rc = whereLoopAddVirtualOne( | - | ||||||||||||||||||||||||
3339 | pBuilder, mPrereq, ALLBITS, WO_IN, p, mNoOmit, &bIn); | - | ||||||||||||||||||||||||
3340 | assert( bIn==0 ); | - | ||||||||||||||||||||||||
3341 | mBestNoIn = pNew->prereq & ~mPrereq; | - | ||||||||||||||||||||||||
3342 | if( mBestNoIn==0 ){
| 0 | ||||||||||||||||||||||||
3343 | seenZero = 1; | - | ||||||||||||||||||||||||
3344 | seenZeroNoIN = 1; | - | ||||||||||||||||||||||||
3345 | } never executed: end of block | 0 | ||||||||||||||||||||||||
3346 | } never executed: end of block | 0 | ||||||||||||||||||||||||
3347 | - | |||||||||||||||||||||||||
3348 | /* Call xBestIndex once for each distinct value of (prereqRight & ~mPrereq) | - | ||||||||||||||||||||||||
3349 | ** in the set of terms that apply to the current virtual table. */ | - | ||||||||||||||||||||||||
3350 | while( rc==SQLITE_OK ){
| 0-1292 | ||||||||||||||||||||||||
3351 | int i; | - | ||||||||||||||||||||||||
3352 | Bitmask mNext = ALLBITS; | - | ||||||||||||||||||||||||
3353 | assert( mNext>0 ); | - | ||||||||||||||||||||||||
3354 | for(i=0; i<nConstraint; i++){
| 1292-1682 | ||||||||||||||||||||||||
3355 | Bitmask mThis = ( | - | ||||||||||||||||||||||||
3356 | pWC->a[p->aConstraint[i].iTermOffset].prereqRight & ~mPrereq | - | ||||||||||||||||||||||||
3357 | ); | - | ||||||||||||||||||||||||
3358 | if( mThis>mPrev && mThis<mNext ) mNext = mThis; executed 660 times by 1 test: mNext = mThis; Executed by:
| 27-995 | ||||||||||||||||||||||||
3359 | } executed 1682 times by 1 test: end of block Executed by:
| 1682 | ||||||||||||||||||||||||
3360 | mPrev = mNext; | - | ||||||||||||||||||||||||
3361 | if( mNext==ALLBITS ) break; executed 633 times by 1 test: break; Executed by:
| 633-659 | ||||||||||||||||||||||||
3362 | if( mNext==mBest || mNext==mBestNoIn ) continue; executed 631 times by 1 test: continue; Executed by:
| 0-631 | ||||||||||||||||||||||||
3363 | WHERETRACE(0x40, (" VirtualOne: mPrev=%04llx mNext=%04llx\n", | - | ||||||||||||||||||||||||
3364 | (sqlite3_uint64)mPrev, (sqlite3_uint64)mNext)); | - | ||||||||||||||||||||||||
3365 | rc = whereLoopAddVirtualOne( | - | ||||||||||||||||||||||||
3366 | pBuilder, mPrereq, mNext|mPrereq, 0, p, mNoOmit, &bIn); | - | ||||||||||||||||||||||||
3367 | if( pNew->prereq==mPrereq ){
| 5-23 | ||||||||||||||||||||||||
3368 | seenZero = 1; | - | ||||||||||||||||||||||||
3369 | if( bIn==0 ) seenZeroNoIN = 1; executed 23 times by 1 test: seenZeroNoIN = 1; Executed by:
| 0-23 | ||||||||||||||||||||||||
3370 | } executed 23 times by 1 test: end of block Executed by:
| 23 | ||||||||||||||||||||||||
3371 | } executed 28 times by 1 test: end of block Executed by:
| 28 | ||||||||||||||||||||||||
3372 | - | |||||||||||||||||||||||||
3373 | /* If the calls to xBestIndex() in the above loop did not find a plan | - | ||||||||||||||||||||||||
3374 | ** that requires no source tables at all (i.e. one guaranteed to be | - | ||||||||||||||||||||||||
3375 | ** usable), make a call here with all source tables disabled */ | - | ||||||||||||||||||||||||
3376 | if( rc==SQLITE_OK && seenZero==0 ){
| 0-633 | ||||||||||||||||||||||||
3377 | WHERETRACE(0x40, (" VirtualOne: all disabled\n")); | - | ||||||||||||||||||||||||
3378 | rc = whereLoopAddVirtualOne( | - | ||||||||||||||||||||||||
3379 | pBuilder, mPrereq, mPrereq, 0, p, mNoOmit, &bIn); | - | ||||||||||||||||||||||||
3380 | if( bIn==0 ) seenZeroNoIN = 1; executed 610 times by 1 test: seenZeroNoIN = 1; Executed by:
| 0-610 | ||||||||||||||||||||||||
3381 | } executed 610 times by 1 test: end of block Executed by:
| 610 | ||||||||||||||||||||||||
3382 | - | |||||||||||||||||||||||||
3383 | /* If the calls to xBestIndex() have so far failed to find a plan | - | ||||||||||||||||||||||||
3384 | ** that requires no source tables at all and does not use an IN(...) | - | ||||||||||||||||||||||||
3385 | ** operator, make a final call to obtain one here. */ | - | ||||||||||||||||||||||||
3386 | if( rc==SQLITE_OK && seenZeroNoIN==0 ){
| 0-367 | ||||||||||||||||||||||||
3387 | WHERETRACE(0x40, (" VirtualOne: all disabled and w/o IN\n")); | - | ||||||||||||||||||||||||
3388 | rc = whereLoopAddVirtualOne( | - | ||||||||||||||||||||||||
3389 | pBuilder, mPrereq, mPrereq, WO_IN, p, mNoOmit, &bIn); | - | ||||||||||||||||||||||||
3390 | } never executed: end of block | 0 | ||||||||||||||||||||||||
3391 | } executed 633 times by 1 test: end of block Executed by:
| 633 | ||||||||||||||||||||||||
3392 | - | |||||||||||||||||||||||||
3393 | if( p->needToFreeIdxStr ) sqlite3_free(p->idxStr); executed 2 times by 1 test: sqlite3_free(p->idxStr); Executed by:
| 2-11462 | ||||||||||||||||||||||||
3394 | sqlite3DbFreeNN(pParse->db, p); | - | ||||||||||||||||||||||||
3395 | WHERETRACE(0x800, ("END %s.addVirtual(), rc=%d\n", pSrc->pTab->zName, rc)); | - | ||||||||||||||||||||||||
3396 | return rc; executed 11464 times by 1 test: return rc; Executed by:
| 11464 | ||||||||||||||||||||||||
3397 | } | - | ||||||||||||||||||||||||
3398 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ | - | ||||||||||||||||||||||||
3399 | - | |||||||||||||||||||||||||
3400 | /* | - | ||||||||||||||||||||||||
3401 | ** Add WhereLoop entries to handle OR terms. This works for either | - | ||||||||||||||||||||||||
3402 | ** btrees or virtual tables. | - | ||||||||||||||||||||||||
3403 | */ | - | ||||||||||||||||||||||||
3404 | static int whereLoopAddOr( | - | ||||||||||||||||||||||||
3405 | WhereLoopBuilder *pBuilder, | - | ||||||||||||||||||||||||
3406 | Bitmask mPrereq, | - | ||||||||||||||||||||||||
3407 | Bitmask mUnusable | - | ||||||||||||||||||||||||
3408 | ){ | - | ||||||||||||||||||||||||
3409 | WhereInfo *pWInfo = pBuilder->pWInfo; | - | ||||||||||||||||||||||||
3410 | WhereClause *pWC; | - | ||||||||||||||||||||||||
3411 | WhereLoop *pNew; | - | ||||||||||||||||||||||||
3412 | WhereTerm *pTerm, *pWCEnd; | - | ||||||||||||||||||||||||
3413 | int rc = SQLITE_OK; | - | ||||||||||||||||||||||||
3414 | int iCur; | - | ||||||||||||||||||||||||
3415 | WhereClause tempWC; | - | ||||||||||||||||||||||||
3416 | WhereLoopBuilder sSubBuild; | - | ||||||||||||||||||||||||
3417 | WhereOrSet sSum, sCur; | - | ||||||||||||||||||||||||
3418 | struct SrcList_item *pItem; | - | ||||||||||||||||||||||||
3419 | - | |||||||||||||||||||||||||
3420 | pWC = pBuilder->pWC; | - | ||||||||||||||||||||||||
3421 | pWCEnd = pWC->a + pWC->nTerm; | - | ||||||||||||||||||||||||
3422 | pNew = pBuilder->pNew; | - | ||||||||||||||||||||||||
3423 | memset(&sSum, 0, sizeof(sSum)); | - | ||||||||||||||||||||||||
3424 | pItem = pWInfo->pTabList->a + pNew->iTab; | - | ||||||||||||||||||||||||
3425 | iCur = pItem->iCursor; | - | ||||||||||||||||||||||||
3426 | - | |||||||||||||||||||||||||
3427 | for(pTerm=pWC->a; pTerm<pWCEnd && rc==SQLITE_OK; pTerm++){
| 0-44000 | ||||||||||||||||||||||||
3428 | if( (pTerm->eOperator & WO_OR)!=0
| 4171-39829 | ||||||||||||||||||||||||
3429 | && (pTerm->u.pOrInfo->indexable & pNew->maskSelf)!=0
| 317-3854 | ||||||||||||||||||||||||
3430 | ){ | - | ||||||||||||||||||||||||
3431 | WhereClause * const pOrWC = &pTerm->u.pOrInfo->wc; | - | ||||||||||||||||||||||||
3432 | WhereTerm * const pOrWCEnd = &pOrWC->a[pOrWC->nTerm]; | - | ||||||||||||||||||||||||
3433 | WhereTerm *pOrTerm; | - | ||||||||||||||||||||||||
3434 | int once = 1; | - | ||||||||||||||||||||||||
3435 | int i, j; | - | ||||||||||||||||||||||||
3436 | - | |||||||||||||||||||||||||
3437 | sSubBuild = *pBuilder; | - | ||||||||||||||||||||||||
3438 | sSubBuild.pOrderBy = 0; | - | ||||||||||||||||||||||||
3439 | sSubBuild.pOrSet = &sCur; | - | ||||||||||||||||||||||||
3440 | - | |||||||||||||||||||||||||
3441 | WHERETRACE(0x200, ("Begin processing OR-clause %p\n", pTerm)); | - | ||||||||||||||||||||||||
3442 | for(pOrTerm=pOrWC->a; pOrTerm<pOrWCEnd; pOrTerm++){
| 2496-19689 | ||||||||||||||||||||||||
3443 | if( (pOrTerm->eOperator & WO_AND)!=0 ){
| 8541-11148 | ||||||||||||||||||||||||
3444 | sSubBuild.pWC = &pOrTerm->u.pAndInfo->wc; | - | ||||||||||||||||||||||||
3445 | }else if( pOrTerm->leftCursor==iCur ){ executed 8541 times by 1 test: end of block Executed by:
| 154-10994 | ||||||||||||||||||||||||
3446 | tempWC.pWInfo = pWC->pWInfo; | - | ||||||||||||||||||||||||
3447 | tempWC.pOuter = pWC; | - | ||||||||||||||||||||||||
3448 | tempWC.op = TK_AND; | - | ||||||||||||||||||||||||
3449 | tempWC.nTerm = 1; | - | ||||||||||||||||||||||||
3450 | tempWC.a = pOrTerm; | - | ||||||||||||||||||||||||
3451 | sSubBuild.pWC = &tempWC; | - | ||||||||||||||||||||||||
3452 | }else{ executed 10994 times by 2 tests: end of block Executed by:
| 10994 | ||||||||||||||||||||||||
3453 | continue; executed 154 times by 1 test: continue; Executed by:
| 154 | ||||||||||||||||||||||||
3454 | } | - | ||||||||||||||||||||||||
3455 | sCur.n = 0; | - | ||||||||||||||||||||||||
3456 | #ifdef WHERETRACE_ENABLED | - | ||||||||||||||||||||||||
3457 | WHERETRACE(0x200, ("OR-term %d of %p has %d subterms:\n", | - | ||||||||||||||||||||||||
3458 | (int)(pOrTerm-pOrWC->a), pTerm, sSubBuild.pWC->nTerm)); | - | ||||||||||||||||||||||||
3459 | if( sqlite3WhereTrace & 0x400 ){ | - | ||||||||||||||||||||||||
3460 | sqlite3WhereClausePrint(sSubBuild.pWC); | - | ||||||||||||||||||||||||
3461 | } | - | ||||||||||||||||||||||||
3462 | #endif | - | ||||||||||||||||||||||||
3463 | #ifndef SQLITE_OMIT_VIRTUALTABLE | - | ||||||||||||||||||||||||
3464 | if( IsVirtual(pItem->pTab) ){
| 52-19483 | ||||||||||||||||||||||||
3465 | rc = whereLoopAddVirtual(&sSubBuild, mPrereq, mUnusable); | - | ||||||||||||||||||||||||
3466 | }else executed 52 times by 1 test: end of block Executed by:
| 52 | ||||||||||||||||||||||||
3467 | #endif | - | ||||||||||||||||||||||||
3468 | { | - | ||||||||||||||||||||||||
3469 | rc = whereLoopAddBtree(&sSubBuild, mPrereq); | - | ||||||||||||||||||||||||
3470 | } executed 19483 times by 2 tests: end of block Executed by:
| 19483 | ||||||||||||||||||||||||
3471 | if( rc==SQLITE_OK ){
| 0-19535 | ||||||||||||||||||||||||
3472 | rc = whereLoopAddOr(&sSubBuild, mPrereq, mUnusable); | - | ||||||||||||||||||||||||
3473 | } executed 19535 times by 2 tests: end of block Executed by:
| 19535 | ||||||||||||||||||||||||
3474 | assert( rc==SQLITE_OK || sCur.n==0 ); | - | ||||||||||||||||||||||||
3475 | if( sCur.n==0 ){
| 1358-18177 | ||||||||||||||||||||||||
3476 | sSum.n = 0; | - | ||||||||||||||||||||||||
3477 | break; executed 1358 times by 2 tests: break; Executed by:
| 1358 | ||||||||||||||||||||||||
3478 | }else if( once ){
| 2580-15597 | ||||||||||||||||||||||||
3479 | whereOrMove(&sSum, &sCur); | - | ||||||||||||||||||||||||
3480 | once = 0; | - | ||||||||||||||||||||||||
3481 | }else{ executed 2580 times by 1 test: end of block Executed by:
| 2580 | ||||||||||||||||||||||||
3482 | WhereOrSet sPrev; | - | ||||||||||||||||||||||||
3483 | whereOrMove(&sPrev, &sSum); | - | ||||||||||||||||||||||||
3484 | sSum.n = 0; | - | ||||||||||||||||||||||||
3485 | for(i=0; i<sPrev.n; i++){
| 15597-15607 | ||||||||||||||||||||||||
3486 | for(j=0; j<sCur.n; j++){
| 15607-15637 | ||||||||||||||||||||||||
3487 | whereOrInsert(&sSum, sPrev.a[i].prereq | sCur.a[j].prereq, | - | ||||||||||||||||||||||||
3488 | sqlite3LogEstAdd(sPrev.a[i].rRun, sCur.a[j].rRun), | - | ||||||||||||||||||||||||
3489 | sqlite3LogEstAdd(sPrev.a[i].nOut, sCur.a[j].nOut)); | - | ||||||||||||||||||||||||
3490 | } executed 15637 times by 1 test: end of block Executed by:
| 15637 | ||||||||||||||||||||||||
3491 | } executed 15607 times by 1 test: end of block Executed by:
| 15607 | ||||||||||||||||||||||||
3492 | } executed 15597 times by 1 test: end of block Executed by:
| 15597 | ||||||||||||||||||||||||
3493 | } | - | ||||||||||||||||||||||||
3494 | pNew->nLTerm = 1; | - | ||||||||||||||||||||||||
3495 | pNew->aLTerm[0] = pTerm; | - | ||||||||||||||||||||||||
3496 | pNew->wsFlags = WHERE_MULTI_OR; | - | ||||||||||||||||||||||||
3497 | pNew->rSetup = 0; | - | ||||||||||||||||||||||||
3498 | pNew->iSortIdx = 0; | - | ||||||||||||||||||||||||
3499 | memset(&pNew->u, 0, sizeof(pNew->u)); | - | ||||||||||||||||||||||||
3500 | for(i=0; rc==SQLITE_OK && i<sSum.n; i++){
| 0-6364 | ||||||||||||||||||||||||
3501 | /* TUNING: Currently sSum.a[i].rRun is set to the sum of the costs | - | ||||||||||||||||||||||||
3502 | ** of all sub-scans required by the OR-scan. However, due to rounding | - | ||||||||||||||||||||||||
3503 | ** errors, it may be that the cost of the OR-scan is equal to its | - | ||||||||||||||||||||||||
3504 | ** most expensive sub-scan. Add the smallest possible penalty | - | ||||||||||||||||||||||||
3505 | ** (equivalent to multiplying the cost by 1.07) to ensure that | - | ||||||||||||||||||||||||
3506 | ** this does not happen. Otherwise, for WHERE clauses such as the | - | ||||||||||||||||||||||||
3507 | ** following where there is an index on "y": | - | ||||||||||||||||||||||||
3508 | ** | - | ||||||||||||||||||||||||
3509 | ** WHERE likelihood(x=?, 0.99) OR y=? | - | ||||||||||||||||||||||||
3510 | ** | - | ||||||||||||||||||||||||
3511 | ** the planner may elect to "OR" together a full-table scan and an | - | ||||||||||||||||||||||||
3512 | ** index lookup. And other similarly odd results. */ | - | ||||||||||||||||||||||||
3513 | pNew->rRun = sSum.a[i].rRun + 1; | - | ||||||||||||||||||||||||
3514 | pNew->nOut = sSum.a[i].nOut; | - | ||||||||||||||||||||||||
3515 | pNew->prereq = sSum.a[i].prereq; | - | ||||||||||||||||||||||||
3516 | rc = whereLoopInsert(pBuilder, pNew); | - | ||||||||||||||||||||||||
3517 | } executed 2510 times by 1 test: end of block Executed by:
| 2510 | ||||||||||||||||||||||||
3518 | WHERETRACE(0x200, ("End processing OR-clause %p\n", pTerm)); | - | ||||||||||||||||||||||||
3519 | } executed 3854 times by 2 tests: end of block Executed by:
| 3854 | ||||||||||||||||||||||||
3520 | } executed 44000 times by 2 tests: end of block Executed by:
| 44000 | ||||||||||||||||||||||||
3521 | return rc; executed 23675 times by 2 tests: return rc; Executed by:
| 23675 | ||||||||||||||||||||||||
3522 | } | - | ||||||||||||||||||||||||
3523 | - | |||||||||||||||||||||||||
3524 | /* | - | ||||||||||||||||||||||||
3525 | ** Add all WhereLoop objects for all tables | - | ||||||||||||||||||||||||
3526 | */ | - | ||||||||||||||||||||||||
3527 | static int whereLoopAddAll(WhereLoopBuilder *pBuilder){ | - | ||||||||||||||||||||||||
3528 | WhereInfo *pWInfo = pBuilder->pWInfo; | - | ||||||||||||||||||||||||
3529 | Bitmask mPrereq = 0; | - | ||||||||||||||||||||||||
3530 | Bitmask mPrior = 0; | - | ||||||||||||||||||||||||
3531 | int iTab; | - | ||||||||||||||||||||||||
3532 | SrcList *pTabList = pWInfo->pTabList; | - | ||||||||||||||||||||||||
3533 | struct SrcList_item *pItem; | - | ||||||||||||||||||||||||
3534 | struct SrcList_item *pEnd = &pTabList->a[pWInfo->nLevel]; | - | ||||||||||||||||||||||||
3535 | sqlite3 *db = pWInfo->pParse->db; | - | ||||||||||||||||||||||||
3536 | int rc = SQLITE_OK; | - | ||||||||||||||||||||||||
3537 | WhereLoop *pNew; | - | ||||||||||||||||||||||||
3538 | u8 priorJointype = 0; | - | ||||||||||||||||||||||||
3539 | - | |||||||||||||||||||||||||
3540 | /* Loop over the tables in the join, from left to right */ | - | ||||||||||||||||||||||||
3541 | pNew = pBuilder->pNew; | - | ||||||||||||||||||||||||
3542 | whereLoopInit(pNew); | - | ||||||||||||||||||||||||
3543 | /* Some pathological queries provide an unreasonable number of indexing | - | ||||||||||||||||||||||||
3544 | ** options. The iPlanLimit value prevents these queries from taking up | - | ||||||||||||||||||||||||
3545 | ** too much time in the planner. When iPlanLimit reaches zero, no further | - | ||||||||||||||||||||||||
3546 | ** index+constraint options are considered. Seed iPlanLimit to 10K but | - | ||||||||||||||||||||||||
3547 | ** also add an extra 1K to each table of the join, to ensure that each | - | ||||||||||||||||||||||||
3548 | ** table at least gets 1K opportunities. */ | - | ||||||||||||||||||||||||
3549 | pBuilder->iPlanLimit = 10000; | - | ||||||||||||||||||||||||
3550 | for(iTab=0, pItem=pTabList->a; pItem<pEnd; iTab++, pItem++){
| 251964-305023 | ||||||||||||||||||||||||
3551 | Bitmask mUnusable = 0; | - | ||||||||||||||||||||||||
3552 | pNew->iTab = iTab; | - | ||||||||||||||||||||||||
3553 | pBuilder->iPlanLimit += 1000; /* 1000 bonus for each table in the join */ | - | ||||||||||||||||||||||||
3554 | pNew->maskSelf = sqlite3WhereGetMask(&pWInfo->sMaskSet, pItem->iCursor); | - | ||||||||||||||||||||||||
3555 | if( ((pItem->fg.jointype|priorJointype) & (JT_LEFT|JT_CROSS))!=0 ){
| 684-251280 | ||||||||||||||||||||||||
3556 | /* This condition is true when pItem is the FROM clause term on the | - | ||||||||||||||||||||||||
3557 | ** right-hand-side of a LEFT or CROSS JOIN. */ | - | ||||||||||||||||||||||||
3558 | mPrereq = mPrior; | - | ||||||||||||||||||||||||
3559 | } executed 684 times by 1 test: end of block Executed by:
| 684 | ||||||||||||||||||||||||
3560 | priorJointype = pItem->fg.jointype; | - | ||||||||||||||||||||||||
3561 | #ifndef SQLITE_OMIT_VIRTUALTABLE | - | ||||||||||||||||||||||||
3562 | if( IsVirtual(pItem->pTab) ){
| 11412-240552 | ||||||||||||||||||||||||
3563 | struct SrcList_item *p; | - | ||||||||||||||||||||||||
3564 | for(p=&pItem[1]; p<pEnd; p++){
| 898-11412 | ||||||||||||||||||||||||
3565 | if( mUnusable || (p->fg.jointype & (JT_LEFT|JT_CROSS)) ){
| 1-897 | ||||||||||||||||||||||||
3566 | mUnusable |= sqlite3WhereGetMask(&pWInfo->sMaskSet, p->iCursor); | - | ||||||||||||||||||||||||
3567 | } executed 30 times by 1 test: end of block Executed by:
| 30 | ||||||||||||||||||||||||
3568 | } executed 898 times by 1 test: end of block Executed by:
| 898 | ||||||||||||||||||||||||
3569 | rc = whereLoopAddVirtual(pBuilder, mPrereq, mUnusable); | - | ||||||||||||||||||||||||
3570 | }else executed 11412 times by 1 test: end of block Executed by:
| 11412 | ||||||||||||||||||||||||
3571 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ | - | ||||||||||||||||||||||||
3572 | { | - | ||||||||||||||||||||||||
3573 | rc = whereLoopAddBtree(pBuilder, mPrereq); | - | ||||||||||||||||||||||||
3574 | } executed 240552 times by 435 tests: end of block Executed by:
| 240552 | ||||||||||||||||||||||||
3575 | if( rc==SQLITE_OK && pBuilder->pWC->hasOr ){
| 327-251637 | ||||||||||||||||||||||||
3576 | rc = whereLoopAddOr(pBuilder, mPrereq, mUnusable); | - | ||||||||||||||||||||||||
3577 | } executed 4140 times by 2 tests: end of block Executed by:
| 4140 | ||||||||||||||||||||||||
3578 | mPrior |= pNew->maskSelf; | - | ||||||||||||||||||||||||
3579 | if( rc || db->mallocFailed ){
| 4-251637 | ||||||||||||||||||||||||
3580 | if( rc==SQLITE_DONE ){
| 0-331 | ||||||||||||||||||||||||
3581 | /* We hit the query planner search limit set by iPlanLimit */ | - | ||||||||||||||||||||||||
3582 | rc = SQLITE_OK; | - | ||||||||||||||||||||||||
3583 | }else{ never executed: end of block | 0 | ||||||||||||||||||||||||
3584 | break; executed 331 times by 1 test: break; Executed by:
| 331 | ||||||||||||||||||||||||
3585 | } | - | ||||||||||||||||||||||||
3586 | } | - | ||||||||||||||||||||||||
3587 | } executed 251633 times by 435 tests: end of block Executed by:
| 251633 | ||||||||||||||||||||||||
3588 | - | |||||||||||||||||||||||||
3589 | whereLoopClear(db, pNew); | - | ||||||||||||||||||||||||
3590 | return rc; executed 305354 times by 435 tests: return rc; Executed by:
| 305354 | ||||||||||||||||||||||||
3591 | } | - | ||||||||||||||||||||||||
3592 | - | |||||||||||||||||||||||||
3593 | /* | - | ||||||||||||||||||||||||
3594 | ** Examine a WherePath (with the addition of the extra WhereLoop of the 6th | - | ||||||||||||||||||||||||
3595 | ** parameters) to see if it outputs rows in the requested ORDER BY | - | ||||||||||||||||||||||||
3596 | ** (or GROUP BY) without requiring a separate sort operation. Return N: | - | ||||||||||||||||||||||||
3597 | ** | - | ||||||||||||||||||||||||
3598 | ** N>0: N terms of the ORDER BY clause are satisfied | - | ||||||||||||||||||||||||
3599 | ** N==0: No terms of the ORDER BY clause are satisfied | - | ||||||||||||||||||||||||
3600 | ** N<0: Unknown yet how many terms of ORDER BY might be satisfied. | - | ||||||||||||||||||||||||
3601 | ** | - | ||||||||||||||||||||||||
3602 | ** Note that processing for WHERE_GROUPBY and WHERE_DISTINCTBY is not as | - | ||||||||||||||||||||||||
3603 | ** strict. With GROUP BY and DISTINCT the only requirement is that | - | ||||||||||||||||||||||||
3604 | ** equivalent rows appear immediately adjacent to one another. GROUP BY | - | ||||||||||||||||||||||||
3605 | ** and DISTINCT do not require rows to appear in any particular order as long | - | ||||||||||||||||||||||||
3606 | ** as equivalent rows are grouped together. Thus for GROUP BY and DISTINCT | - | ||||||||||||||||||||||||
3607 | ** the pOrderBy terms can be matched in any order. With ORDER BY, the | - | ||||||||||||||||||||||||
3608 | ** pOrderBy terms must be matched in strict left-to-right order. | - | ||||||||||||||||||||||||
3609 | */ | - | ||||||||||||||||||||||||
3610 | static i8 wherePathSatisfiesOrderBy( | - | ||||||||||||||||||||||||
3611 | WhereInfo *pWInfo, /* The WHERE clause */ | - | ||||||||||||||||||||||||
3612 | ExprList *pOrderBy, /* ORDER BY or GROUP BY or DISTINCT clause to check */ | - | ||||||||||||||||||||||||
3613 | WherePath *pPath, /* The WherePath to check */ | - | ||||||||||||||||||||||||
3614 | u16 wctrlFlags, /* WHERE_GROUPBY or _DISTINCTBY or _ORDERBY_LIMIT */ | - | ||||||||||||||||||||||||
3615 | u16 nLoop, /* Number of entries in pPath->aLoop[] */ | - | ||||||||||||||||||||||||
3616 | WhereLoop *pLast, /* Add this WhereLoop to the end of pPath->aLoop[] */ | - | ||||||||||||||||||||||||
3617 | Bitmask *pRevMask /* OUT: Mask of WhereLoops to run in reverse order */ | - | ||||||||||||||||||||||||
3618 | ){ | - | ||||||||||||||||||||||||
3619 | u8 revSet; /* True if rev is known */ | - | ||||||||||||||||||||||||
3620 | u8 rev; /* Composite sort order */ | - | ||||||||||||||||||||||||
3621 | u8 revIdx; /* Index sort order */ | - | ||||||||||||||||||||||||
3622 | u8 isOrderDistinct; /* All prior WhereLoops are order-distinct */ | - | ||||||||||||||||||||||||
3623 | u8 distinctColumns; /* True if the loop has UNIQUE NOT NULL columns */ | - | ||||||||||||||||||||||||
3624 | u8 isMatch; /* iColumn matches a term of the ORDER BY clause */ | - | ||||||||||||||||||||||||
3625 | u16 eqOpMask; /* Allowed equality operators */ | - | ||||||||||||||||||||||||
3626 | u16 nKeyCol; /* Number of key columns in pIndex */ | - | ||||||||||||||||||||||||
3627 | u16 nColumn; /* Total number of ordered columns in the index */ | - | ||||||||||||||||||||||||
3628 | u16 nOrderBy; /* Number terms in the ORDER BY clause */ | - | ||||||||||||||||||||||||
3629 | int iLoop; /* Index of WhereLoop in pPath being processed */ | - | ||||||||||||||||||||||||
3630 | int i, j; /* Loop counters */ | - | ||||||||||||||||||||||||
3631 | int iCur; /* Cursor number for current WhereLoop */ | - | ||||||||||||||||||||||||
3632 | int iColumn; /* A column number within table iCur */ | - | ||||||||||||||||||||||||
3633 | WhereLoop *pLoop = 0; /* Current WhereLoop being processed. */ | - | ||||||||||||||||||||||||
3634 | WhereTerm *pTerm; /* A single term of the WHERE clause */ | - | ||||||||||||||||||||||||
3635 | Expr *pOBExpr; /* An expression from the ORDER BY clause */ | - | ||||||||||||||||||||||||
3636 | CollSeq *pColl; /* COLLATE function from an ORDER BY clause term */ | - | ||||||||||||||||||||||||
3637 | Index *pIndex; /* The index associated with pLoop */ | - | ||||||||||||||||||||||||
3638 | sqlite3 *db = pWInfo->pParse->db; /* Database connection */ | - | ||||||||||||||||||||||||
3639 | Bitmask obSat = 0; /* Mask of ORDER BY terms satisfied so far */ | - | ||||||||||||||||||||||||
3640 | Bitmask obDone; /* Mask of all ORDER BY terms */ | - | ||||||||||||||||||||||||
3641 | Bitmask orderDistinctMask; /* Mask of all well-ordered loops */ | - | ||||||||||||||||||||||||
3642 | Bitmask ready; /* Mask of inner loops */ | - | ||||||||||||||||||||||||
3643 | - | |||||||||||||||||||||||||
3644 | /* | - | ||||||||||||||||||||||||
3645 | ** We say the WhereLoop is "one-row" if it generates no more than one | - | ||||||||||||||||||||||||
3646 | ** row of output. A WhereLoop is one-row if all of the following are true: | - | ||||||||||||||||||||||||
3647 | ** (a) All index columns match with WHERE_COLUMN_EQ. | - | ||||||||||||||||||||||||
3648 | ** (b) The index is unique | - | ||||||||||||||||||||||||
3649 | ** Any WhereLoop with an WHERE_COLUMN_EQ constraint on the rowid is one-row. | - | ||||||||||||||||||||||||
3650 | ** Every one-row WhereLoop will have the WHERE_ONEROW bit set in wsFlags. | - | ||||||||||||||||||||||||
3651 | ** | - | ||||||||||||||||||||||||
3652 | ** We say the WhereLoop is "order-distinct" if the set of columns from | - | ||||||||||||||||||||||||
3653 | ** that WhereLoop that are in the ORDER BY clause are different for every | - | ||||||||||||||||||||||||
3654 | ** row of the WhereLoop. Every one-row WhereLoop is automatically | - | ||||||||||||||||||||||||
3655 | ** order-distinct. A WhereLoop that has no columns in the ORDER BY clause | - | ||||||||||||||||||||||||
3656 | ** is not order-distinct. To be order-distinct is not quite the same as being | - | ||||||||||||||||||||||||
3657 | ** UNIQUE since a UNIQUE column or index can have multiple rows that | - | ||||||||||||||||||||||||
3658 | ** are NULL and NULL values are equivalent for the purpose of order-distinct. | - | ||||||||||||||||||||||||
3659 | ** To be order-distinct, the columns must be UNIQUE and NOT NULL. | - | ||||||||||||||||||||||||
3660 | ** | - | ||||||||||||||||||||||||
3661 | ** The rowid for a table is always UNIQUE and NOT NULL so whenever the | - | ||||||||||||||||||||||||
3662 | ** rowid appears in the ORDER BY clause, the corresponding WhereLoop is | - | ||||||||||||||||||||||||
3663 | ** automatically order-distinct. | - | ||||||||||||||||||||||||
3664 | */ | - | ||||||||||||||||||||||||
3665 | - | |||||||||||||||||||||||||
3666 | assert( pOrderBy!=0 ); | - | ||||||||||||||||||||||||
3667 | if( nLoop && OptimizationDisabled(db, SQLITE_OrderByIdxJoin) ) return 0; executed 24 times by 1 test: return 0; Executed by:
| 24-159223 | ||||||||||||||||||||||||
3668 | - | |||||||||||||||||||||||||
3669 | nOrderBy = pOrderBy->nExpr; | - | ||||||||||||||||||||||||
3670 | testcase( nOrderBy==BMS-1 ); | - | ||||||||||||||||||||||||
3671 | if( nOrderBy>BMS-1 ) return 0; /* Cannot optimize overly large ORDER BYs */ never executed: return 0;
| 0-162776 | ||||||||||||||||||||||||
3672 | isOrderDistinct = 1; | - | ||||||||||||||||||||||||
3673 | obDone = MASKBIT(nOrderBy)-1; | - | ||||||||||||||||||||||||
3674 | orderDistinctMask = 0; | - | ||||||||||||||||||||||||
3675 | ready = 0; | - | ||||||||||||||||||||||||
3676 | eqOpMask = WO_EQ | WO_IS | WO_ISNULL; | - | ||||||||||||||||||||||||
3677 | if( wctrlFlags & WHERE_ORDERBY_LIMIT ) eqOpMask |= WO_IN; executed 84834 times by 435 tests: eqOpMask |= 0x0001; Executed by:
| 77942-84834 | ||||||||||||||||||||||||
3678 | for(iLoop=0; isOrderDistinct && obSat<obDone && iLoop<=nLoop; iLoop++){
| 347-249811 | ||||||||||||||||||||||||
3679 | if( iLoop>0 ) ready |= pLoop->maskSelf; executed 3605 times by 1 test: ready |= pLoop->maskSelf; Executed by:
| 3605-162776 | ||||||||||||||||||||||||
3680 | if( iLoop<nLoop ){
| 3952-162429 | ||||||||||||||||||||||||
3681 | pLoop = pPath->aLoop[iLoop]; | - | ||||||||||||||||||||||||
3682 | if( wctrlFlags & WHERE_ORDERBY_LIMIT ) continue; executed 2793 times by 1 test: continue; Executed by:
| 1159-2793 | ||||||||||||||||||||||||
3683 | }else{ executed 1159 times by 1 test: end of block Executed by:
| 1159 | ||||||||||||||||||||||||
3684 | pLoop = pLast; | - | ||||||||||||||||||||||||
3685 | } executed 162429 times by 435 tests: end of block Executed by:
| 162429 | ||||||||||||||||||||||||
3686 | if( pLoop->wsFlags & WHERE_VIRTUALTABLE ){
| 764-162824 | ||||||||||||||||||||||||
3687 | if( pLoop->u.vtab.isOrdered ) obSat = obDone; executed 32 times by 1 test: obSat = obDone; Executed by:
| 32-732 | ||||||||||||||||||||||||
3688 | break; executed 764 times by 1 test: break; Executed by:
| 764 | ||||||||||||||||||||||||
3689 | }else{ | - | ||||||||||||||||||||||||
3690 | pLoop->u.btree.nIdxCol = 0; | - | ||||||||||||||||||||||||
3691 | } executed 162824 times by 435 tests: end of block Executed by:
| 162824 | ||||||||||||||||||||||||
3692 | iCur = pWInfo->pTabList->a[pLoop->iTab].iCursor; | - | ||||||||||||||||||||||||
3693 | - | |||||||||||||||||||||||||
3694 | /* Mark off any ORDER BY term X that is a column in the table of | - | ||||||||||||||||||||||||
3695 | ** the current loop for which there is term in the WHERE | - | ||||||||||||||||||||||||
3696 | ** clause of the form X IS NULL or X=? that reference only outer | - | ||||||||||||||||||||||||
3697 | ** loops. | - | ||||||||||||||||||||||||
3698 | */ | - | ||||||||||||||||||||||||
3699 | for(i=0; i<nOrderBy; i++){
| 162824-178373 | ||||||||||||||||||||||||
3700 | if( MASKBIT(i) & obSat ) continue; executed 719 times by 1 test: continue; Executed by:
| 719-177654 | ||||||||||||||||||||||||
3701 | pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[i].pExpr); | - | ||||||||||||||||||||||||
3702 | if( pOBExpr->op!=TK_COLUMN ) continue; executed 31227 times by 1 test: continue; Executed by:
| 31227-146427 | ||||||||||||||||||||||||
3703 | if( pOBExpr->iTable!=iCur ) continue; executed 6717 times by 1 test: continue; Executed by:
| 6717-139710 | ||||||||||||||||||||||||
3704 | pTerm = sqlite3WhereFindTerm(&pWInfo->sWC, iCur, pOBExpr->iColumn, | - | ||||||||||||||||||||||||
3705 | ~ready, eqOpMask, 0); | - | ||||||||||||||||||||||||
3706 | if( pTerm==0 ) continue; executed 136695 times by 435 tests: continue; Executed by:
| 3015-136695 | ||||||||||||||||||||||||
3707 | if( pTerm->eOperator==WO_IN ){
| 613-2402 | ||||||||||||||||||||||||
3708 | /* IN terms are only valid for sorting in the ORDER BY LIMIT | - | ||||||||||||||||||||||||
3709 | ** optimization, and then only if they are actually used | - | ||||||||||||||||||||||||
3710 | ** by the query plan */ | - | ||||||||||||||||||||||||
3711 | assert( wctrlFlags & WHERE_ORDERBY_LIMIT ); | - | ||||||||||||||||||||||||
3712 | for(j=0; j<pLoop->nLTerm && pTerm!=pLoop->aLTerm[j]; j++){} executed 186 times by 1 test: end of block Executed by:
| 186-561 | ||||||||||||||||||||||||
3713 | if( j>=pLoop->nLTerm ) continue; executed 238 times by 1 test: continue; Executed by:
| 238-375 | ||||||||||||||||||||||||
3714 | } executed 375 times by 1 test: end of block Executed by:
| 375 | ||||||||||||||||||||||||
3715 | if( (pTerm->eOperator&(WO_EQ|WO_IS))!=0 && pOBExpr->iColumn>=0 ){
| 5-2329 | ||||||||||||||||||||||||
3716 | if( sqlite3ExprCollSeqMatch(pWInfo->pParse,
| 11-2313 | ||||||||||||||||||||||||
3717 | pOrderBy->a[i].pExpr, pTerm->pExpr)==0 ){
| 11-2313 | ||||||||||||||||||||||||
3718 | continue; executed 11 times by 1 test: continue; Executed by:
| 11 | ||||||||||||||||||||||||
3719 | } | - | ||||||||||||||||||||||||
3720 | testcase( pTerm->pExpr->op==TK_IS ); | - | ||||||||||||||||||||||||
3721 | } executed 2313 times by 1 test: end of block Executed by:
| 2313 | ||||||||||||||||||||||||
3722 | obSat |= MASKBIT(i); | - | ||||||||||||||||||||||||
3723 | } executed 2766 times by 1 test: end of block Executed by:
| 2766 | ||||||||||||||||||||||||
3724 | - | |||||||||||||||||||||||||
3725 | if( (pLoop->wsFlags & WHERE_ONEROW)==0 ){
| 713-162111 | ||||||||||||||||||||||||
3726 | if( pLoop->wsFlags & WHERE_IPK ){
| 24749-137362 | ||||||||||||||||||||||||
3727 | pIndex = 0; | - | ||||||||||||||||||||||||
3728 | nKeyCol = 0; | - | ||||||||||||||||||||||||
3729 | nColumn = 1; | - | ||||||||||||||||||||||||
3730 | }else if( (pIndex = pLoop->u.btree.pIndex)==0 || pIndex->bUnordered ){ executed 137362 times by 435 tests: end of block Executed by:
| 3-137362 | ||||||||||||||||||||||||
3731 | return 0; executed 755 times by 1 test: return 0; Executed by:
| 755 | ||||||||||||||||||||||||
3732 | }else{ | - | ||||||||||||||||||||||||
3733 | nKeyCol = pIndex->nKeyCol; | - | ||||||||||||||||||||||||
3734 | nColumn = pIndex->nColumn; | - | ||||||||||||||||||||||||
3735 | assert( nColumn==nKeyCol+1 || !HasRowid(pIndex->pTable) ); | - | ||||||||||||||||||||||||
3736 | assert( pIndex->aiColumn[nColumn-1]==XN_ROWID | - | ||||||||||||||||||||||||
3737 | || !HasRowid(pIndex->pTable)); | - | ||||||||||||||||||||||||
3738 | isOrderDistinct = IsUniqueIndex(pIndex); | - | ||||||||||||||||||||||||
3739 | } executed 23994 times by 2 tests: end of block Executed by:
| 23994 | ||||||||||||||||||||||||
3740 | - | |||||||||||||||||||||||||
3741 | /* Loop through all columns of the index and deal with the ones | - | ||||||||||||||||||||||||
3742 | ** that are not constrained by == or IN. | - | ||||||||||||||||||||||||
3743 | */ | - | ||||||||||||||||||||||||
3744 | rev = revSet = 0; | - | ||||||||||||||||||||||||
3745 | distinctColumns = 0; | - | ||||||||||||||||||||||||
3746 | for(j=0; j<nColumn; j++){
| 83092-177697 | ||||||||||||||||||||||||
3747 | u8 bOnce = 1; /* True to run the ORDER BY search loop */ | - | ||||||||||||||||||||||||
3748 | - | |||||||||||||||||||||||||
3749 | assert( j>=pLoop->u.btree.nEq | - | ||||||||||||||||||||||||
3750 | || (pLoop->aLTerm[j]==0)==(j<pLoop->nSkip) | - | ||||||||||||||||||||||||
3751 | ); | - | ||||||||||||||||||||||||
3752 | if( j<pLoop->u.btree.nEq && j>=pLoop->nSkip ){
| 67-167064 | ||||||||||||||||||||||||
3753 | u16 eOp = pLoop->aLTerm[j]->eOperator; | - | ||||||||||||||||||||||||
3754 | - | |||||||||||||||||||||||||
3755 | /* Skip over == and IS and ISNULL terms. (Also skip IN terms when | - | ||||||||||||||||||||||||
3756 | ** doing WHERE_ORDERBY_LIMIT processing). | - | ||||||||||||||||||||||||
3757 | ** | - | ||||||||||||||||||||||||
3758 | ** If the current term is a column of an ((?,?) IN (SELECT...)) | - | ||||||||||||||||||||||||
3759 | ** expression for which the SELECT returns more than one column, | - | ||||||||||||||||||||||||
3760 | ** check that it is the only column used by this loop. Otherwise, | - | ||||||||||||||||||||||||
3761 | ** if it is one of two or more, none of the columns can be | - | ||||||||||||||||||||||||
3762 | ** considered to match an ORDER BY term. */ | - | ||||||||||||||||||||||||
3763 | if( (eOp & eqOpMask)!=0 ){
| 783-9783 | ||||||||||||||||||||||||
3764 | if( eOp & WO_ISNULL ){
| 53-9730 | ||||||||||||||||||||||||
3765 | testcase( isOrderDistinct ); | - | ||||||||||||||||||||||||
3766 | isOrderDistinct = 0; | - | ||||||||||||||||||||||||
3767 | } executed 53 times by 1 test: end of block Executed by:
| 53 | ||||||||||||||||||||||||
3768 | continue; executed 9783 times by 1 test: continue; Executed by:
| 9783 | ||||||||||||||||||||||||
3769 | }else if( ALWAYS(eOp & WO_IN) ){
| 0-783 | ||||||||||||||||||||||||
3770 | /* ALWAYS() justification: eOp is an equality operator due to the | - | ||||||||||||||||||||||||
3771 | ** j<pLoop->u.btree.nEq constraint above. Any equality other | - | ||||||||||||||||||||||||
3772 | ** than WO_IN is captured by the previous "if". So this one | - | ||||||||||||||||||||||||
3773 | ** always has to be WO_IN. */ | - | ||||||||||||||||||||||||
3774 | Expr *pX = pLoop->aLTerm[j]->pExpr; | - | ||||||||||||||||||||||||
3775 | for(i=j+1; i<pLoop->u.btree.nEq; i++){
| 294-771 | ||||||||||||||||||||||||
3776 | if( pLoop->aLTerm[i]->pExpr==pX ){
| 12-282 | ||||||||||||||||||||||||
3777 | assert( (pLoop->aLTerm[i]->eOperator & WO_IN) ); | - | ||||||||||||||||||||||||
3778 | bOnce = 0; | - | ||||||||||||||||||||||||
3779 | break; executed 12 times by 1 test: break; Executed by:
| 12 | ||||||||||||||||||||||||
3780 | } | - | ||||||||||||||||||||||||
3781 | } executed 282 times by 1 test: end of block Executed by:
| 282 | ||||||||||||||||||||||||
3782 | } executed 783 times by 1 test: end of block Executed by:
| 783 | ||||||||||||||||||||||||
3783 | } executed 783 times by 1 test: end of block Executed by:
| 783 | ||||||||||||||||||||||||
3784 | - | |||||||||||||||||||||||||
3785 | /* Get the column number in the table (iColumn) and sort order | - | ||||||||||||||||||||||||
3786 | ** (revIdx) for the j-th column of the index. | - | ||||||||||||||||||||||||
3787 | */ | - | ||||||||||||||||||||||||
3788 | if( pIndex ){
| 30552-137362 | ||||||||||||||||||||||||
3789 | iColumn = pIndex->aiColumn[j]; | - | ||||||||||||||||||||||||
3790 | revIdx = pIndex->aSortOrder[j]; | - | ||||||||||||||||||||||||
3791 | if( iColumn==pIndex->pTable->iPKey ) iColumn = XN_ROWID; executed 11014 times by 2 tests: iColumn = (-1); Executed by:
| 11014-19538 | ||||||||||||||||||||||||
3792 | }else{ executed 30552 times by 2 tests: end of block Executed by:
| 30552 | ||||||||||||||||||||||||
3793 | iColumn = XN_ROWID; | - | ||||||||||||||||||||||||
3794 | revIdx = 0; | - | ||||||||||||||||||||||||
3795 | } executed 137362 times by 435 tests: end of block Executed by:
| 137362 | ||||||||||||||||||||||||
3796 | - | |||||||||||||||||||||||||
3797 | /* An unconstrained column that might be NULL means that this | - | ||||||||||||||||||||||||
3798 | ** WhereLoop is not well-ordered | - | ||||||||||||||||||||||||
3799 | */ | - | ||||||||||||||||||||||||
3800 | if( isOrderDistinct
| 27797-140117 | ||||||||||||||||||||||||
3801 | && iColumn>=0
| 2317-137800 | ||||||||||||||||||||||||
3802 | && j>=pLoop->u.btree.nEq
| 182-2135 | ||||||||||||||||||||||||
3803 | && pIndex->pTable->aCol[iColumn].notNull==0
| 856-1279 | ||||||||||||||||||||||||
3804 | ){ | - | ||||||||||||||||||||||||
3805 | isOrderDistinct = 0; | - | ||||||||||||||||||||||||
3806 | } executed 1279 times by 1 test: end of block Executed by:
| 1279 | ||||||||||||||||||||||||
3807 | - | |||||||||||||||||||||||||
3808 | /* Find the ORDER BY term that corresponds to the j-th column | - | ||||||||||||||||||||||||
3809 | ** of the index and mark that ORDER BY term off | - | ||||||||||||||||||||||||
3810 | */ | - | ||||||||||||||||||||||||
3811 | isMatch = 0; | - | ||||||||||||||||||||||||
3812 | for(i=0; bOnce && i<nOrderBy; i++){
| 9008-181846 | ||||||||||||||||||||||||
3813 | if( MASKBIT(i) & obSat ) continue; executed 10096 times by 2 tests: continue; Executed by:
| 10096-162742 | ||||||||||||||||||||||||
3814 | pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[i].pExpr); | - | ||||||||||||||||||||||||
3815 | testcase( wctrlFlags & WHERE_GROUPBY ); | - | ||||||||||||||||||||||||
3816 | testcase( wctrlFlags & WHERE_DISTINCTBY ); | - | ||||||||||||||||||||||||
3817 | if( (wctrlFlags & (WHERE_GROUPBY|WHERE_DISTINCTBY))==0 ) bOnce = 0; executed 158327 times by 435 tests: bOnce = 0; Executed by:
| 4415-158327 | ||||||||||||||||||||||||
3818 | if( iColumn>=XN_ROWID ){
| 39-162703 | ||||||||||||||||||||||||
3819 | if( pOBExpr->op!=TK_COLUMN ) continue; executed 29325 times by 1 test: continue; Executed by:
| 29325-133378 | ||||||||||||||||||||||||
3820 | if( pOBExpr->iTable!=iCur ) continue; executed 4813 times by 1 test: continue; Executed by:
| 4813-128565 | ||||||||||||||||||||||||
3821 | if( pOBExpr->iColumn!=iColumn ) continue; executed 38763 times by 1 test: continue; Executed by:
| 38763-89802 | ||||||||||||||||||||||||
3822 | }else{ executed 89802 times by 435 tests: end of block Executed by:
| 89802 | ||||||||||||||||||||||||
3823 | Expr *pIdxExpr = pIndex->aColExpr->a[j].pExpr; | - | ||||||||||||||||||||||||
3824 | if( sqlite3ExprCompareSkip(pOBExpr, pIdxExpr, iCur) ){
| 19-20 | ||||||||||||||||||||||||
3825 | continue; executed 19 times by 1 test: continue; Executed by:
| 19 | ||||||||||||||||||||||||
3826 | } | - | ||||||||||||||||||||||||
3827 | } executed 20 times by 1 test: end of block Executed by:
| 20 | ||||||||||||||||||||||||
3828 | if( iColumn!=XN_ROWID ){
| 6726-83096 | ||||||||||||||||||||||||
3829 | pColl = sqlite3ExprNNCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr); | - | ||||||||||||||||||||||||
3830 | if( sqlite3StrICmp(pColl->zName, pIndex->azColl[j])!=0 ) continue; executed 126 times by 1 test: continue; Executed by:
| 126-6600 | ||||||||||||||||||||||||
3831 | } executed 6600 times by 2 tests: end of block Executed by:
| 6600 | ||||||||||||||||||||||||
3832 | pLoop->u.btree.nIdxCol = j+1; | - | ||||||||||||||||||||||||
3833 | isMatch = 1; | - | ||||||||||||||||||||||||
3834 | break; executed 89696 times by 435 tests: break; Executed by:
| 89696 | ||||||||||||||||||||||||
3835 | } | - | ||||||||||||||||||||||||
3836 | if( isMatch && (wctrlFlags & WHERE_GROUPBY)==0 ){
| 130-89696 | ||||||||||||||||||||||||
3837 | /* Make sure the sort order is compatible in an ORDER BY clause. | - | ||||||||||||||||||||||||
3838 | ** Sort order is irrelevant for a GROUP BY clause. */ | - | ||||||||||||||||||||||||
3839 | if( revSet ){
| 256-89310 | ||||||||||||||||||||||||
3840 | if( (rev ^ revIdx)!=pOrderBy->a[i].sortOrder ) isMatch = 0; executed 46 times by 1 test: isMatch = 0; Executed by:
| 46-210 | ||||||||||||||||||||||||
3841 | }else{ executed 256 times by 1 test: end of block Executed by:
| 256 | ||||||||||||||||||||||||
3842 | rev = revIdx ^ pOrderBy->a[i].sortOrder; | - | ||||||||||||||||||||||||
3843 | if( rev ) *pRevMask |= MASKBIT(iLoop); executed 5725 times by 1 test: *pRevMask |= (((Bitmask)1)<<(iLoop)); Executed by:
| 5725-83585 | ||||||||||||||||||||||||
3844 | revSet = 1; | - | ||||||||||||||||||||||||
3845 | } executed 89310 times by 435 tests: end of block Executed by:
| 89310 | ||||||||||||||||||||||||
3846 | } | - | ||||||||||||||||||||||||
3847 | if( isMatch ){
| 78264-89650 | ||||||||||||||||||||||||
3848 | if( iColumn==XN_ROWID ){
| 6558-83092 | ||||||||||||||||||||||||
3849 | testcase( distinctColumns==0 ); | - | ||||||||||||||||||||||||
3850 | distinctColumns = 1; | - | ||||||||||||||||||||||||
3851 | } executed 83092 times by 435 tests: end of block Executed by:
| 83092 | ||||||||||||||||||||||||
3852 | obSat |= MASKBIT(i); | - | ||||||||||||||||||||||||
3853 | }else{ executed 89650 times by 435 tests: end of block Executed by:
| 89650 | ||||||||||||||||||||||||
3854 | /* No match found */ | - | ||||||||||||||||||||||||
3855 | if( j==0 || j<nKeyCol ){
| 598-66458 | ||||||||||||||||||||||||
3856 | testcase( isOrderDistinct!=0 ); | - | ||||||||||||||||||||||||
3857 | isOrderDistinct = 0; | - | ||||||||||||||||||||||||
3858 | } executed 67056 times by 1 test: end of block Executed by:
| 67056 | ||||||||||||||||||||||||
3859 | break; executed 78264 times by 2 tests: break; Executed by:
| 78264 | ||||||||||||||||||||||||
3860 | } | - | ||||||||||||||||||||||||
3861 | } /* end Loop over all index columns */ | - | ||||||||||||||||||||||||
3862 | if( distinctColumns ){
| 78264-83092 | ||||||||||||||||||||||||
3863 | testcase( isOrderDistinct==0 ); | - | ||||||||||||||||||||||||
3864 | isOrderDistinct = 1; | - | ||||||||||||||||||||||||
3865 | } executed 83092 times by 435 tests: end of block Executed by:
| 83092 | ||||||||||||||||||||||||
3866 | } /* end-if not one-row */ executed 161356 times by 435 tests: end of block Executed by:
| 161356 | ||||||||||||||||||||||||
3867 | - | |||||||||||||||||||||||||
3868 | /* Mark off any other ORDER BY terms that reference pLoop */ | - | ||||||||||||||||||||||||
3869 | if( isOrderDistinct ){
| 77827-84242 | ||||||||||||||||||||||||
3870 | orderDistinctMask |= pLoop->maskSelf; | - | ||||||||||||||||||||||||
3871 | for(i=0; i<nOrderBy; i++){
| 84242-86451 | ||||||||||||||||||||||||
3872 | Expr *p; | - | ||||||||||||||||||||||||
3873 | Bitmask mTerm; | - | ||||||||||||||||||||||||
3874 | if( MASKBIT(i) & obSat ) continue; executed 84116 times by 435 tests: continue; Executed by:
| 2335-84116 | ||||||||||||||||||||||||
3875 | p = pOrderBy->a[i].pExpr; | - | ||||||||||||||||||||||||
3876 | mTerm = sqlite3WhereExprUsage(&pWInfo->sMaskSet,p); | - | ||||||||||||||||||||||||
3877 | if( mTerm==0 && !sqlite3ExprIsConstant(p) ) continue; executed 4 times by 1 test: continue; Executed by:
| 1-2330 | ||||||||||||||||||||||||
3878 | if( (mTerm&~orderDistinctMask)==0 ){
| 114-2217 | ||||||||||||||||||||||||
3879 | obSat |= MASKBIT(i); | - | ||||||||||||||||||||||||
3880 | } executed 114 times by 1 test: end of block Executed by:
| 114 | ||||||||||||||||||||||||
3881 | } executed 2331 times by 1 test: end of block Executed by:
| 2331 | ||||||||||||||||||||||||
3882 | } executed 84242 times by 435 tests: end of block Executed by:
| 84242 | ||||||||||||||||||||||||
3883 | } /* End the loop over all WhereLoops from outer-most down to inner-most */ executed 162069 times by 435 tests: end of block Executed by:
| 162069 | ||||||||||||||||||||||||
3884 | if( obSat==obDone ) return (i8)nOrderBy; executed 89261 times by 435 tests: return (i8)nOrderBy; Executed by:
| 72760-89261 | ||||||||||||||||||||||||
3885 | if( !isOrderDistinct ){
| 1079-71681 | ||||||||||||||||||||||||
3886 | for(i=nOrderBy-1; i>0; i--){
| 12026-70642 | ||||||||||||||||||||||||
3887 | Bitmask m = MASKBIT(i) - 1; | - | ||||||||||||||||||||||||
3888 | if( (obSat&m)==m ) return i; executed 1039 times by 1 test: return i; Executed by:
| 1039-10987 | ||||||||||||||||||||||||
3889 | } executed 10987 times by 1 test: end of block Executed by:
| 10987 | ||||||||||||||||||||||||
3890 | return 0; executed 70642 times by 1 test: return 0; Executed by:
| 70642 | ||||||||||||||||||||||||
3891 | } | - | ||||||||||||||||||||||||
3892 | return -1; executed 1079 times by 1 test: return -1; Executed by:
| 1079 | ||||||||||||||||||||||||
3893 | } | - | ||||||||||||||||||||||||
3894 | - | |||||||||||||||||||||||||
3895 | - | |||||||||||||||||||||||||
3896 | /* | - | ||||||||||||||||||||||||
3897 | ** If the WHERE_GROUPBY flag is set in the mask passed to sqlite3WhereBegin(), | - | ||||||||||||||||||||||||
3898 | ** the planner assumes that the specified pOrderBy list is actually a GROUP | - | ||||||||||||||||||||||||
3899 | ** BY clause - and so any order that groups rows as required satisfies the | - | ||||||||||||||||||||||||
3900 | ** request. | - | ||||||||||||||||||||||||
3901 | ** | - | ||||||||||||||||||||||||
3902 | ** Normally, in this case it is not possible for the caller to determine | - | ||||||||||||||||||||||||
3903 | ** whether or not the rows are really being delivered in sorted order, or | - | ||||||||||||||||||||||||
3904 | ** just in some other order that provides the required grouping. However, | - | ||||||||||||||||||||||||
3905 | ** if the WHERE_SORTBYGROUP flag is also passed to sqlite3WhereBegin(), then | - | ||||||||||||||||||||||||
3906 | ** this function may be called on the returned WhereInfo object. It returns | - | ||||||||||||||||||||||||
3907 | ** true if the rows really will be sorted in the specified order, or false | - | ||||||||||||||||||||||||
3908 | ** otherwise. | - | ||||||||||||||||||||||||
3909 | ** | - | ||||||||||||||||||||||||
3910 | ** For example, assuming: | - | ||||||||||||||||||||||||
3911 | ** | - | ||||||||||||||||||||||||
3912 | ** CREATE INDEX i1 ON t1(x, Y); | - | ||||||||||||||||||||||||
3913 | ** | - | ||||||||||||||||||||||||
3914 | ** then | - | ||||||||||||||||||||||||
3915 | ** | - | ||||||||||||||||||||||||
3916 | ** SELECT * FROM t1 GROUP BY x,y ORDER BY x,y; -- IsSorted()==1 | - | ||||||||||||||||||||||||
3917 | ** SELECT * FROM t1 GROUP BY y,x ORDER BY y,x; -- IsSorted()==0 | - | ||||||||||||||||||||||||
3918 | */ | - | ||||||||||||||||||||||||
3919 | int sqlite3WhereIsSorted(WhereInfo *pWInfo){ | - | ||||||||||||||||||||||||
3920 | assert( pWInfo->wctrlFlags & WHERE_GROUPBY ); | - | ||||||||||||||||||||||||
3921 | assert( pWInfo->wctrlFlags & WHERE_SORTBYGROUP ); | - | ||||||||||||||||||||||||
3922 | return pWInfo->sorted; executed 23 times by 1 test: return pWInfo->sorted; Executed by:
| 23 | ||||||||||||||||||||||||
3923 | } | - | ||||||||||||||||||||||||
3924 | - | |||||||||||||||||||||||||
3925 | #ifdef WHERETRACE_ENABLED | - | ||||||||||||||||||||||||
3926 | /* For debugging use only: */ | - | ||||||||||||||||||||||||
3927 | static const char *wherePathName(WherePath *pPath, int nLoop, WhereLoop *pLast){ | - | ||||||||||||||||||||||||
3928 | static char zName[65]; | - | ||||||||||||||||||||||||
3929 | int i; | - | ||||||||||||||||||||||||
3930 | for(i=0; i<nLoop; i++){ zName[i] = pPath->aLoop[i]->cId; } | - | ||||||||||||||||||||||||
3931 | if( pLast ) zName[i++] = pLast->cId; | - | ||||||||||||||||||||||||
3932 | zName[i] = 0; | - | ||||||||||||||||||||||||
3933 | return zName; | - | ||||||||||||||||||||||||
3934 | } | - | ||||||||||||||||||||||||
3935 | #endif | - | ||||||||||||||||||||||||
3936 | - | |||||||||||||||||||||||||
3937 | /* | - | ||||||||||||||||||||||||
3938 | ** Return the cost of sorting nRow rows, assuming that the keys have | - | ||||||||||||||||||||||||
3939 | ** nOrderby columns and that the first nSorted columns are already in | - | ||||||||||||||||||||||||
3940 | ** order. | - | ||||||||||||||||||||||||
3941 | */ | - | ||||||||||||||||||||||||
3942 | static LogEst whereSortingCost( | - | ||||||||||||||||||||||||
3943 | WhereInfo *pWInfo, | - | ||||||||||||||||||||||||
3944 | LogEst nRow, | - | ||||||||||||||||||||||||
3945 | int nOrderBy, | - | ||||||||||||||||||||||||
3946 | int nSorted | - | ||||||||||||||||||||||||
3947 | ){ | - | ||||||||||||||||||||||||
3948 | /* TUNING: Estimated cost of a full external sort, where N is | - | ||||||||||||||||||||||||
3949 | ** the number of rows to sort is: | - | ||||||||||||||||||||||||
3950 | ** | - | ||||||||||||||||||||||||
3951 | ** cost = (3.0 * N * log(N)). | - | ||||||||||||||||||||||||
3952 | ** | - | ||||||||||||||||||||||||
3953 | ** Or, if the order-by clause has X terms but only the last Y | - | ||||||||||||||||||||||||
3954 | ** terms are out of order, then block-sorting will reduce the | - | ||||||||||||||||||||||||
3955 | ** sorting cost to: | - | ||||||||||||||||||||||||
3956 | ** | - | ||||||||||||||||||||||||
3957 | ** cost = (3.0 * N * log(N)) * (Y/X) | - | ||||||||||||||||||||||||
3958 | ** | - | ||||||||||||||||||||||||
3959 | ** The (Y/X) term is implemented using stack variable rScale | - | ||||||||||||||||||||||||
3960 | ** below. */ | - | ||||||||||||||||||||||||
3961 | LogEst rScale, rSortCost; | - | ||||||||||||||||||||||||
3962 | assert( nOrderBy>0 && 66==sqlite3LogEst(100) ); | - | ||||||||||||||||||||||||
3963 | rScale = sqlite3LogEst((nOrderBy-nSorted)*100/nOrderBy) - 66; | - | ||||||||||||||||||||||||
3964 | rSortCost = nRow + rScale + 16; | - | ||||||||||||||||||||||||
3965 | - | |||||||||||||||||||||||||
3966 | /* Multiple by log(M) where M is the number of output rows. | - | ||||||||||||||||||||||||
3967 | ** Use the LIMIT for M if it is smaller */ | - | ||||||||||||||||||||||||
3968 | if( (pWInfo->wctrlFlags & WHERE_USE_LIMIT)!=0 && pWInfo->iLimit<nRow ){
| 15-25599 | ||||||||||||||||||||||||
3969 | nRow = pWInfo->iLimit; | - | ||||||||||||||||||||||||
3970 | } executed 561 times by 1 test: end of block Executed by:
| 561 | ||||||||||||||||||||||||
3971 | rSortCost += estLog(nRow); | - | ||||||||||||||||||||||||
3972 | return rSortCost; executed 26175 times by 1 test: return rSortCost; Executed by:
| 26175 | ||||||||||||||||||||||||
3973 | } | - | ||||||||||||||||||||||||
3974 | - | |||||||||||||||||||||||||
3975 | /* | - | ||||||||||||||||||||||||
3976 | ** Given the list of WhereLoop objects at pWInfo->pLoops, this routine | - | ||||||||||||||||||||||||
3977 | ** attempts to find the lowest cost path that visits each WhereLoop | - | ||||||||||||||||||||||||
3978 | ** once. This path is then loaded into the pWInfo->a[].pWLoop fields. | - | ||||||||||||||||||||||||
3979 | ** | - | ||||||||||||||||||||||||
3980 | ** Assume that the total number of output rows that will need to be sorted | - | ||||||||||||||||||||||||
3981 | ** will be nRowEst (in the 10*log2 representation). Or, ignore sorting | - | ||||||||||||||||||||||||
3982 | ** costs if nRowEst==0. | - | ||||||||||||||||||||||||
3983 | ** | - | ||||||||||||||||||||||||
3984 | ** Return SQLITE_OK on success or SQLITE_NOMEM of a memory allocation | - | ||||||||||||||||||||||||
3985 | ** error occurs. | - | ||||||||||||||||||||||||
3986 | */ | - | ||||||||||||||||||||||||
3987 | static int wherePathSolver(WhereInfo *pWInfo, LogEst nRowEst){ | - | ||||||||||||||||||||||||
3988 | int mxChoice; /* Maximum number of simultaneous paths tracked */ | - | ||||||||||||||||||||||||
3989 | int nLoop; /* Number of terms in the join */ | - | ||||||||||||||||||||||||
3990 | Parse *pParse; /* Parsing context */ | - | ||||||||||||||||||||||||
3991 | sqlite3 *db; /* The database connection */ | - | ||||||||||||||||||||||||
3992 | int iLoop; /* Loop counter over the terms of the join */ | - | ||||||||||||||||||||||||
3993 | int ii, jj; /* Loop counters */ | - | ||||||||||||||||||||||||
3994 | int mxI = 0; /* Index of next entry to replace */ | - | ||||||||||||||||||||||||
3995 | int nOrderBy; /* Number of ORDER BY clause terms */ | - | ||||||||||||||||||||||||
3996 | LogEst mxCost = 0; /* Maximum cost of a set of paths */ | - | ||||||||||||||||||||||||
3997 | LogEst mxUnsorted = 0; /* Maximum unsorted cost of a set of path */ | - | ||||||||||||||||||||||||
3998 | int nTo, nFrom; /* Number of valid entries in aTo[] and aFrom[] */ | - | ||||||||||||||||||||||||
3999 | WherePath *aFrom; /* All nFrom paths at the previous level */ | - | ||||||||||||||||||||||||
4000 | WherePath *aTo; /* The nTo best paths at the current level */ | - | ||||||||||||||||||||||||
4001 | WherePath *pFrom; /* An element of aFrom[] that we are working on */ | - | ||||||||||||||||||||||||
4002 | WherePath *pTo; /* An element of aTo[] that we are working on */ | - | ||||||||||||||||||||||||
4003 | WhereLoop *pWLoop; /* One of the WhereLoop objects */ | - | ||||||||||||||||||||||||
4004 | WhereLoop **pX; /* Used to divy up the pSpace memory */ | - | ||||||||||||||||||||||||
4005 | LogEst *aSortCost = 0; /* Sorting and partial sorting costs */ | - | ||||||||||||||||||||||||
4006 | char *pSpace; /* Temporary memory used by this routine */ | - | ||||||||||||||||||||||||
4007 | int nSpace; /* Bytes of space allocated at pSpace */ | - | ||||||||||||||||||||||||
4008 | - | |||||||||||||||||||||||||
4009 | pParse = pWInfo->pParse; | - | ||||||||||||||||||||||||
4010 | db = pParse->db; | - | ||||||||||||||||||||||||
4011 | nLoop = pWInfo->nLevel; | - | ||||||||||||||||||||||||
4012 | /* TUNING: For simple queries, only the best path is tracked. | - | ||||||||||||||||||||||||
4013 | ** For 2-way joins, the 5 best paths are followed. | - | ||||||||||||||||||||||||
4014 | ** For joins of 3 or more tables, track the 10 best paths */ | - | ||||||||||||||||||||||||
4015 | mxChoice = (nLoop<=1) ? 1 : (nLoop==2 ? 5 : 10);
| 629-361928 | ||||||||||||||||||||||||
4016 | assert( nLoop<=pWInfo->pTabList->nSrc ); | - | ||||||||||||||||||||||||
4017 | WHERETRACE(0x002, ("---- begin solver. (nRowEst=%d)\n", nRowEst)); | - | ||||||||||||||||||||||||
4018 | - | |||||||||||||||||||||||||
4019 | /* If nRowEst is zero and there is an ORDER BY clause, ignore it. In this | - | ||||||||||||||||||||||||
4020 | ** case the purpose of this call is to estimate the number of rows returned | - | ||||||||||||||||||||||||
4021 | ** by the overall query. Once this estimate has been obtained, the caller | - | ||||||||||||||||||||||||
4022 | ** will invoke this function a second time, passing the estimate as the | - | ||||||||||||||||||||||||
4023 | ** nRowEst parameter. */ | - | ||||||||||||||||||||||||
4024 | if( pWInfo->pOrderBy==0 || nRowEst==0 ){
| 65847-239169 | ||||||||||||||||||||||||
4025 | nOrderBy = 0; | - | ||||||||||||||||||||||||
4026 | }else{ executed 305028 times by 435 tests: end of block Executed by:
| 305028 | ||||||||||||||||||||||||
4027 | nOrderBy = pWInfo->pOrderBy->nExpr; | - | ||||||||||||||||||||||||
4028 | } executed 65847 times by 435 tests: end of block Executed by:
| 65847 | ||||||||||||||||||||||||
4029 | - | |||||||||||||||||||||||||
4030 | /* Allocate and initialize space for aTo, aFrom and aSortCost[] */ | - | ||||||||||||||||||||||||
4031 | nSpace = (sizeof(WherePath)+sizeof(WhereLoop*)*nLoop)*mxChoice*2; | - | ||||||||||||||||||||||||
4032 | nSpace += sizeof(LogEst) * nOrderBy; | - | ||||||||||||||||||||||||
4033 | pSpace = sqlite3DbMallocRawNN(db, nSpace); | - | ||||||||||||||||||||||||
4034 | if( pSpace==0 ) return SQLITE_NOMEM_BKPT; executed 51 times by 1 test: return 7; Executed by:
| 51-370824 | ||||||||||||||||||||||||
4035 | aTo = (WherePath*)pSpace; | - | ||||||||||||||||||||||||
4036 | aFrom = aTo+mxChoice; | - | ||||||||||||||||||||||||
4037 | memset(aFrom, 0, sizeof(aFrom[0])); | - | ||||||||||||||||||||||||
4038 | pX = (WhereLoop**)(aFrom+mxChoice); | - | ||||||||||||||||||||||||
4039 | for(ii=mxChoice*2, pFrom=aTo; ii>0; ii--, pFrom++, pX += nLoop){
| 370824-819434 | ||||||||||||||||||||||||
4040 | pFrom->aLoop = pX; | - | ||||||||||||||||||||||||
4041 | } executed 819434 times by 435 tests: end of block Executed by:
| 819434 | ||||||||||||||||||||||||
4042 | if( nOrderBy ){
| 65837-304987 | ||||||||||||||||||||||||
4043 | /* If there is an ORDER BY clause and it is not being ignored, set up | - | ||||||||||||||||||||||||
4044 | ** space for the aSortCost[] array. Each element of the aSortCost array | - | ||||||||||||||||||||||||
4045 | ** is either zero - meaning it has not yet been initialized - or the | - | ||||||||||||||||||||||||
4046 | ** cost of sorting nRowEst rows of data where the first X terms of | - | ||||||||||||||||||||||||
4047 | ** the ORDER BY clause are already in order, where X is the array | - | ||||||||||||||||||||||||
4048 | ** index. */ | - | ||||||||||||||||||||||||
4049 | aSortCost = (LogEst*)pX; | - | ||||||||||||||||||||||||
4050 | memset(aSortCost, 0, sizeof(LogEst) * nOrderBy); | - | ||||||||||||||||||||||||
4051 | } executed 65837 times by 435 tests: end of block Executed by:
| 65837 | ||||||||||||||||||||||||
4052 | assert( aSortCost==0 || &pSpace[nSpace]==(char*)&aSortCost[nOrderBy] ); | - | ||||||||||||||||||||||||
4053 | assert( aSortCost!=0 || &pSpace[nSpace]==(char*)pX ); | - | ||||||||||||||||||||||||
4054 | - | |||||||||||||||||||||||||
4055 | /* Seed the search with a single WherePath containing zero WhereLoops. | - | ||||||||||||||||||||||||
4056 | ** | - | ||||||||||||||||||||||||
4057 | ** TUNING: Do not let the number of iterations go above 28. If the cost | - | ||||||||||||||||||||||||
4058 | ** of computing an automatic index is not paid back within the first 28 | - | ||||||||||||||||||||||||
4059 | ** rows, then do not use the automatic index. */ | - | ||||||||||||||||||||||||
4060 | aFrom[0].nRow = MIN(pParse->nQueryLoop, 48); assert( 48==sqlite3LogEst(28) );
| 42313-328511 | ||||||||||||||||||||||||
4061 | nFrom = 1; | - | ||||||||||||||||||||||||
4062 | assert( aFrom[0].isOrdered==0 ); | - | ||||||||||||||||||||||||
4063 | if( nOrderBy ){
| 65837-304987 | ||||||||||||||||||||||||
4064 | /* If nLoop is zero, then there are no FROM terms in the query. Since | - | ||||||||||||||||||||||||
4065 | ** in this case the query may return a maximum of one row, the results | - | ||||||||||||||||||||||||
4066 | ** are already in the requested order. Set isOrdered to nOrderBy to | - | ||||||||||||||||||||||||
4067 | ** indicate this. Or, if nLoop is greater than zero, set isOrdered to | - | ||||||||||||||||||||||||
4068 | ** -1, indicating that the result set may or may not be ordered, | - | ||||||||||||||||||||||||
4069 | ** depending on the loops added to the current plan. */ | - | ||||||||||||||||||||||||
4070 | aFrom[0].isOrdered = nLoop>0 ? -1 : nOrderBy;
| 95-65742 | ||||||||||||||||||||||||
4071 | } executed 65837 times by 435 tests: end of block Executed by:
| 65837 | ||||||||||||||||||||||||
4072 | - | |||||||||||||||||||||||||
4073 | /* Compute successively longer WherePaths using the previous generation | - | ||||||||||||||||||||||||
4074 | ** of WherePaths as the basis for the next. Keep track of the mxChoice | - | ||||||||||||||||||||||||
4075 | ** best paths at each generation */ | - | ||||||||||||||||||||||||
4076 | for(iLoop=0; iLoop<nLoop; iLoop++){
| 319820-370824 | ||||||||||||||||||||||||
4077 | nTo = 0; | - | ||||||||||||||||||||||||
4078 | for(ii=0, pFrom=aFrom; ii<nFrom; ii++, pFrom++){
| 319820-367636 | ||||||||||||||||||||||||
4079 | for(pWLoop=pWInfo->pLoops; pWLoop; pWLoop=pWLoop->pNextLoop){
| 367636-3185083 | ||||||||||||||||||||||||
4080 | LogEst nOut; /* Rows visited by (pFrom+pWLoop) */ | - | ||||||||||||||||||||||||
4081 | LogEst rCost; /* Cost of path (pFrom+pWLoop) */ | - | ||||||||||||||||||||||||
4082 | LogEst rUnsorted; /* Unsorted cost of (pFrom+pWLoop) */ | - | ||||||||||||||||||||||||
4083 | i8 isOrdered = pFrom->isOrdered; /* isOrdered for (pFrom+pWLoop) */ | - | ||||||||||||||||||||||||
4084 | Bitmask maskNew; /* Mask of src visited by (..) */ | - | ||||||||||||||||||||||||
4085 | Bitmask revMask = 0; /* Mask of rev-order loops for (..) */ | - | ||||||||||||||||||||||||
4086 | - | |||||||||||||||||||||||||
4087 | if( (pWLoop->prereq & ~pFrom->maskLoop)!=0 ) continue; executed 453799 times by 1 test: continue; Executed by:
| 453799-2731284 | ||||||||||||||||||||||||
4088 | if( (pWLoop->maskSelf & pFrom->maskLoop)!=0 ) continue; executed 1348923 times by 1 test: continue; Executed by:
| 1348923-1382361 | ||||||||||||||||||||||||
4089 | if( (pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 && pFrom->nRow<3 ){
| 31800-1292719 | ||||||||||||||||||||||||
4090 | /* Do not use an automatic index if the this loop is expected | - | ||||||||||||||||||||||||
4091 | ** to run less than 1.25 times. It is tempting to also exclude | - | ||||||||||||||||||||||||
4092 | ** automatic index usage on an outer loop, but sometimes an automatic | - | ||||||||||||||||||||||||
4093 | ** index is useful in the outer loop of a correlated subquery. */ | - | ||||||||||||||||||||||||
4094 | assert( 10==sqlite3LogEst(2) ); | - | ||||||||||||||||||||||||
4095 | continue; executed 57842 times by 33 tests: continue; Executed by:
| 57842 | ||||||||||||||||||||||||
4096 | } | - | ||||||||||||||||||||||||
4097 | - | |||||||||||||||||||||||||
4098 | /* At this point, pWLoop is a candidate to be the next loop. | - | ||||||||||||||||||||||||
4099 | ** Compute its cost */ | - | ||||||||||||||||||||||||
4100 | rUnsorted = sqlite3LogEstAdd(pWLoop->rSetup,pWLoop->rRun + pFrom->nRow); | - | ||||||||||||||||||||||||
4101 | rUnsorted = sqlite3LogEstAdd(rUnsorted, pFrom->rUnsorted); | - | ||||||||||||||||||||||||
4102 | nOut = pFrom->nRow + pWLoop->nOut; | - | ||||||||||||||||||||||||
4103 | maskNew = pFrom->maskLoop | pWLoop->maskSelf; | - | ||||||||||||||||||||||||
4104 | if( isOrdered<0 ){
| 77883-1246636 | ||||||||||||||||||||||||
4105 | isOrdered = wherePathSatisfiesOrderBy(pWInfo, | - | ||||||||||||||||||||||||
4106 | pWInfo->pOrderBy, pFrom, pWInfo->wctrlFlags, | - | ||||||||||||||||||||||||
4107 | iLoop, pWLoop, &revMask); | - | ||||||||||||||||||||||||
4108 | }else{ executed 77883 times by 435 tests: end of block Executed by:
| 77883 | ||||||||||||||||||||||||
4109 | revMask = pFrom->revLoop; | - | ||||||||||||||||||||||||
4110 | } executed 1246636 times by 435 tests: end of block Executed by:
| 1246636 | ||||||||||||||||||||||||
4111 | if( isOrdered>=0 && isOrdered<nOrderBy ){
| 797-1323722 | ||||||||||||||||||||||||
4112 | if( aSortCost[isOrdered]==0 ){
| 16014-26175 | ||||||||||||||||||||||||
4113 | aSortCost[isOrdered] = whereSortingCost( | - | ||||||||||||||||||||||||
4114 | pWInfo, nRowEst, nOrderBy, isOrdered | - | ||||||||||||||||||||||||
4115 | ); | - | ||||||||||||||||||||||||
4116 | } executed 26175 times by 1 test: end of block Executed by:
| 26175 | ||||||||||||||||||||||||
4117 | /* TUNING: Add a small extra penalty (5) to sorting as an | - | ||||||||||||||||||||||||
4118 | ** extra encouragment to the query planner to select a plan | - | ||||||||||||||||||||||||
4119 | ** where the rows emerge in the correct order without any sorting | - | ||||||||||||||||||||||||
4120 | ** required. */ | - | ||||||||||||||||||||||||
4121 | rCost = sqlite3LogEstAdd(rUnsorted, aSortCost[isOrdered]) + 5; | - | ||||||||||||||||||||||||
4122 | - | |||||||||||||||||||||||||
4123 | WHERETRACE(0x002, | - | ||||||||||||||||||||||||
4124 | ("---- sort cost=%-3d (%d/%d) increases cost %3d to %-3d\n", | - | ||||||||||||||||||||||||
4125 | aSortCost[isOrdered], (nOrderBy-isOrdered), nOrderBy, | - | ||||||||||||||||||||||||
4126 | rUnsorted, rCost)); | - | ||||||||||||||||||||||||
4127 | }else{ executed 42189 times by 1 test: end of block Executed by:
| 42189 | ||||||||||||||||||||||||
4128 | rCost = rUnsorted; | - | ||||||||||||||||||||||||
4129 | rUnsorted -= 2; /* TUNING: Slight bias in favor of no-sort plans */ | - | ||||||||||||||||||||||||
4130 | } executed 1282330 times by 435 tests: end of block Executed by:
| 1282330 | ||||||||||||||||||||||||
4131 | - | |||||||||||||||||||||||||
4132 | /* Check to see if pWLoop should be added to the set of | - | ||||||||||||||||||||||||
4133 | ** mxChoice best-so-far paths. | - | ||||||||||||||||||||||||
4134 | ** | - | ||||||||||||||||||||||||
4135 | ** First look for an existing path among best-so-far paths | - | ||||||||||||||||||||||||
4136 | ** that covers the same set of loops and has the same isOrdered | - | ||||||||||||||||||||||||
4137 | ** setting as the current path candidate. | - | ||||||||||||||||||||||||
4138 | ** | - | ||||||||||||||||||||||||
4139 | ** The term "((pTo->isOrdered^isOrdered)&0x80)==0" is equivalent | - | ||||||||||||||||||||||||
4140 | ** to (pTo->isOrdered==(-1))==(isOrdered==(-1))" for the range | - | ||||||||||||||||||||||||
4141 | ** of legal values for isOrdered, -1..64. | - | ||||||||||||||||||||||||
4142 | */ | - | ||||||||||||||||||||||||
4143 | for(jj=0, pTo=aTo; jj<nTo; jj++, pTo++){
| 1207734-8971249 | ||||||||||||||||||||||||
4144 | if( pTo->maskLoop==maskNew
| 117223-8854026 | ||||||||||||||||||||||||
4145 | && ((pTo->isOrdered^isOrdered)&0x80)==0
| 438-116785 | ||||||||||||||||||||||||
4146 | ){ | - | ||||||||||||||||||||||||
4147 | testcase( jj==nTo-1 ); | - | ||||||||||||||||||||||||
4148 | break; executed 116785 times by 1 test: break; Executed by:
| 116785 | ||||||||||||||||||||||||
4149 | } | - | ||||||||||||||||||||||||
4150 | } executed 8854464 times by 1 test: end of block Executed by:
| 8854464 | ||||||||||||||||||||||||
4151 | if( jj>=nTo ){
| 116785-1207734 | ||||||||||||||||||||||||
4152 | /* None of the existing best-so-far paths match the candidate. */ | - | ||||||||||||||||||||||||
4153 | if( nTo>=mxChoice
| 367646-840088 | ||||||||||||||||||||||||
4154 | && (rCost>mxCost || (rCost==mxCost && rUnsorted>=mxUnsorted))
| 1-810747 | ||||||||||||||||||||||||
4155 | ){ | - | ||||||||||||||||||||||||
4156 | /* The current candidate is no better than any of the mxChoice | - | ||||||||||||||||||||||||
4157 | ** paths currently in the best-so-far buffer. So discard | - | ||||||||||||||||||||||||
4158 | ** this candidate as not viable. */ | - | ||||||||||||||||||||||||
4159 | #ifdef WHERETRACE_ENABLED /* 0x4 */ | - | ||||||||||||||||||||||||
4160 | if( sqlite3WhereTrace&0x4 ){ | - | ||||||||||||||||||||||||
4161 | sqlite3DebugPrintf("Skip %s cost=%-3d,%3d,%3d order=%c\n", | - | ||||||||||||||||||||||||
4162 | wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, rUnsorted, | - | ||||||||||||||||||||||||
4163 | isOrdered>=0 ? isOrdered+'0' : '?'); | - | ||||||||||||||||||||||||
4164 | } | - | ||||||||||||||||||||||||
4165 | #endif | - | ||||||||||||||||||||||||
4166 | continue; executed 824462 times by 1 test: continue; Executed by:
| 824462 | ||||||||||||||||||||||||
4167 | } | - | ||||||||||||||||||||||||
4168 | /* If we reach this points it means that the new candidate path | - | ||||||||||||||||||||||||
4169 | ** needs to be added to the set of best-so-far paths. */ | - | ||||||||||||||||||||||||
4170 | if( nTo<mxChoice ){
| 15626-367646 | ||||||||||||||||||||||||
4171 | /* Increase the size of the aTo set by one */ | - | ||||||||||||||||||||||||
4172 | jj = nTo++; | - | ||||||||||||||||||||||||
4173 | }else{ executed 367646 times by 435 tests: end of block Executed by:
| 367646 | ||||||||||||||||||||||||
4174 | /* New path replaces the prior worst to keep count below mxChoice */ | - | ||||||||||||||||||||||||
4175 | jj = mxI; | - | ||||||||||||||||||||||||
4176 | } executed 15626 times by 1 test: end of block Executed by:
| 15626 | ||||||||||||||||||||||||
4177 | pTo = &aTo[jj]; | - | ||||||||||||||||||||||||
4178 | #ifdef WHERETRACE_ENABLED /* 0x4 */ | - | ||||||||||||||||||||||||
4179 | if( sqlite3WhereTrace&0x4 ){ | - | ||||||||||||||||||||||||
4180 | sqlite3DebugPrintf("New %s cost=%-3d,%3d,%3d order=%c\n", | - | ||||||||||||||||||||||||
4181 | wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, rUnsorted, | - | ||||||||||||||||||||||||
4182 | isOrdered>=0 ? isOrdered+'0' : '?'); | - | ||||||||||||||||||||||||
4183 | } | - | ||||||||||||||||||||||||
4184 | #endif | - | ||||||||||||||||||||||||
4185 | }else{ executed 383272 times by 435 tests: end of block Executed by:
| 383272 | ||||||||||||||||||||||||
4186 | /* Control reaches here if best-so-far path pTo=aTo[jj] covers the | - | ||||||||||||||||||||||||
4187 | ** same set of loops and has the same isOrdered setting as the | - | ||||||||||||||||||||||||
4188 | ** candidate path. Check to see if the candidate should replace | - | ||||||||||||||||||||||||
4189 | ** pTo or if the candidate should be skipped. | - | ||||||||||||||||||||||||
4190 | ** | - | ||||||||||||||||||||||||
4191 | ** The conditional is an expanded vector comparison equivalent to: | - | ||||||||||||||||||||||||
4192 | ** (pTo->rCost,pTo->nRow,pTo->rUnsorted) <= (rCost,nOut,rUnsorted) | - | ||||||||||||||||||||||||
4193 | */ | - | ||||||||||||||||||||||||
4194 | if( pTo->rCost<rCost
| 48566-68219 | ||||||||||||||||||||||||
4195 | || (pTo->rCost==rCost
| 21389-27177 | ||||||||||||||||||||||||
4196 | && (pTo->nRow<nOut
| 108-27069 | ||||||||||||||||||||||||
4197 | || (pTo->nRow==nOut && pTo->rUnsorted<=rUnsorted)
| 64-26974 | ||||||||||||||||||||||||
4198 | ) | - | ||||||||||||||||||||||||
4199 | ) | - | ||||||||||||||||||||||||
4200 | ){ | - | ||||||||||||||||||||||||
4201 | #ifdef WHERETRACE_ENABLED /* 0x4 */ | - | ||||||||||||||||||||||||
4202 | if( sqlite3WhereTrace&0x4 ){ | - | ||||||||||||||||||||||||
4203 | sqlite3DebugPrintf( | - | ||||||||||||||||||||||||
4204 | "Skip %s cost=%-3d,%3d,%3d order=%c", | - | ||||||||||||||||||||||||
4205 | wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, rUnsorted, | - | ||||||||||||||||||||||||
4206 | isOrdered>=0 ? isOrdered+'0' : '?'); | - | ||||||||||||||||||||||||
4207 | sqlite3DebugPrintf(" vs %s cost=%-3d,%3d,%3d order=%c\n", | - | ||||||||||||||||||||||||
4208 | wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow, | - | ||||||||||||||||||||||||
4209 | pTo->rUnsorted, pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?'); | - | ||||||||||||||||||||||||
4210 | } | - | ||||||||||||||||||||||||
4211 | #endif | - | ||||||||||||||||||||||||
4212 | /* Discard the candidate path from further consideration */ | - | ||||||||||||||||||||||||
4213 | testcase( pTo->rCost==rCost ); | - | ||||||||||||||||||||||||
4214 | continue; executed 95237 times by 1 test: continue; Executed by:
| 95237 | ||||||||||||||||||||||||
4215 | } | - | ||||||||||||||||||||||||
4216 | testcase( pTo->rCost==rCost+1 ); | - | ||||||||||||||||||||||||
4217 | /* Control reaches here if the candidate path is better than the | - | ||||||||||||||||||||||||
4218 | ** pTo path. Replace pTo with the candidate. */ | - | ||||||||||||||||||||||||
4219 | #ifdef WHERETRACE_ENABLED /* 0x4 */ | - | ||||||||||||||||||||||||
4220 | if( sqlite3WhereTrace&0x4 ){ | - | ||||||||||||||||||||||||
4221 | sqlite3DebugPrintf( | - | ||||||||||||||||||||||||
4222 | "Update %s cost=%-3d,%3d,%3d order=%c", | - | ||||||||||||||||||||||||
4223 | wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, rUnsorted, | - | ||||||||||||||||||||||||
4224 | isOrdered>=0 ? isOrdered+'0' : '?'); | - | ||||||||||||||||||||||||
4225 | sqlite3DebugPrintf(" was %s cost=%-3d,%3d,%3d order=%c\n", | - | ||||||||||||||||||||||||
4226 | wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow, | - | ||||||||||||||||||||||||
4227 | pTo->rUnsorted, pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?'); | - | ||||||||||||||||||||||||
4228 | } | - | ||||||||||||||||||||||||
4229 | #endif | - | ||||||||||||||||||||||||
4230 | } executed 21548 times by 1 test: end of block Executed by:
| 21548 | ||||||||||||||||||||||||
4231 | /* pWLoop is a winner. Add it to the set of best so far */ | - | ||||||||||||||||||||||||
4232 | pTo->maskLoop = pFrom->maskLoop | pWLoop->maskSelf; | - | ||||||||||||||||||||||||
4233 | pTo->revLoop = revMask; | - | ||||||||||||||||||||||||
4234 | pTo->nRow = nOut; | - | ||||||||||||||||||||||||
4235 | pTo->rCost = rCost; | - | ||||||||||||||||||||||||
4236 | pTo->rUnsorted = rUnsorted; | - | ||||||||||||||||||||||||
4237 | pTo->isOrdered = isOrdered; | - | ||||||||||||||||||||||||
4238 | memcpy(pTo->aLoop, pFrom->aLoop, sizeof(WhereLoop*)*iLoop); | - | ||||||||||||||||||||||||
4239 | pTo->aLoop[iLoop] = pWLoop; | - | ||||||||||||||||||||||||
4240 | if( nTo>=mxChoice ){
| 79028-325792 | ||||||||||||||||||||||||
4241 | mxI = 0; | - | ||||||||||||||||||||||||
4242 | mxCost = aTo[0].rCost; | - | ||||||||||||||||||||||||
4243 | mxUnsorted = aTo[0].nRow; | - | ||||||||||||||||||||||||
4244 | for(jj=1, pTo=&aTo[1]; jj<mxChoice; jj++, pTo++){
| 185922-325792 | ||||||||||||||||||||||||
4245 | if( pTo->rCost>mxCost
| 19089-166833 | ||||||||||||||||||||||||
4246 | || (pTo->rCost==mxCost && pTo->rUnsorted>mxUnsorted)
| 8098-158735 | ||||||||||||||||||||||||
4247 | ){ | - | ||||||||||||||||||||||||
4248 | mxCost = pTo->rCost; | - | ||||||||||||||||||||||||
4249 | mxUnsorted = pTo->rUnsorted; | - | ||||||||||||||||||||||||
4250 | mxI = jj; | - | ||||||||||||||||||||||||
4251 | } executed 36408 times by 1 test: end of block Executed by:
| 36408 | ||||||||||||||||||||||||
4252 | } executed 185922 times by 1 test: end of block Executed by:
| 185922 | ||||||||||||||||||||||||
4253 | } executed 325792 times by 435 tests: end of block Executed by:
| 325792 | ||||||||||||||||||||||||
4254 | } executed 404820 times by 435 tests: end of block Executed by:
| 404820 | ||||||||||||||||||||||||
4255 | } executed 367636 times by 435 tests: end of block Executed by:
| 367636 | ||||||||||||||||||||||||
4256 | - | |||||||||||||||||||||||||
4257 | #ifdef WHERETRACE_ENABLED /* >=2 */ | - | ||||||||||||||||||||||||
4258 | if( sqlite3WhereTrace & 0x02 ){ | - | ||||||||||||||||||||||||
4259 | sqlite3DebugPrintf("---- after round %d ----\n", iLoop); | - | ||||||||||||||||||||||||
4260 | for(ii=0, pTo=aTo; ii<nTo; ii++, pTo++){ | - | ||||||||||||||||||||||||
4261 | sqlite3DebugPrintf(" %s cost=%-3d nrow=%-3d order=%c", | - | ||||||||||||||||||||||||
4262 | wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow, | - | ||||||||||||||||||||||||
4263 | pTo->isOrdered>=0 ? (pTo->isOrdered+'0') : '?'); | - | ||||||||||||||||||||||||
4264 | if( pTo->isOrdered>0 ){ | - | ||||||||||||||||||||||||
4265 | sqlite3DebugPrintf(" rev=0x%llx\n", pTo->revLoop); | - | ||||||||||||||||||||||||
4266 | }else{ | - | ||||||||||||||||||||||||
4267 | sqlite3DebugPrintf("\n"); | - | ||||||||||||||||||||||||
4268 | } | - | ||||||||||||||||||||||||
4269 | } | - | ||||||||||||||||||||||||
4270 | } | - | ||||||||||||||||||||||||
4271 | #endif | - | ||||||||||||||||||||||||
4272 | - | |||||||||||||||||||||||||
4273 | /* Swap the roles of aFrom and aTo for the next generation */ | - | ||||||||||||||||||||||||
4274 | pFrom = aTo; | - | ||||||||||||||||||||||||
4275 | aTo = aFrom; | - | ||||||||||||||||||||||||
4276 | aFrom = pFrom; | - | ||||||||||||||||||||||||
4277 | nFrom = nTo; | - | ||||||||||||||||||||||||
4278 | } executed 319820 times by 435 tests: end of block Executed by:
| 319820 | ||||||||||||||||||||||||
4279 | - | |||||||||||||||||||||||||
4280 | if( nFrom==0 ){
| 26-370798 | ||||||||||||||||||||||||
4281 | sqlite3ErrorMsg(pParse, "no query solution"); | - | ||||||||||||||||||||||||
4282 | sqlite3DbFreeNN(db, pSpace); | - | ||||||||||||||||||||||||
4283 | return SQLITE_ERROR; executed 26 times by 1 test: return 1; Executed by:
| 26 | ||||||||||||||||||||||||
4284 | } | - | ||||||||||||||||||||||||
4285 | - | |||||||||||||||||||||||||
4286 | /* Find the lowest cost path. pFrom will be left pointing to that path */ | - | ||||||||||||||||||||||||
4287 | pFrom = aFrom; | - | ||||||||||||||||||||||||
4288 | for(ii=1; ii<nFrom; ii++){
| 36-370798 | ||||||||||||||||||||||||
4289 | if( pFrom->rCost>aFrom[ii].rCost ) pFrom = &aFrom[ii]; executed 7 times by 1 test: pFrom = &aFrom[ii]; Executed by:
| 7-29 | ||||||||||||||||||||||||
4290 | } executed 36 times by 1 test: end of block Executed by:
| 36 | ||||||||||||||||||||||||
4291 | assert( pWInfo->nLevel==nLoop ); | - | ||||||||||||||||||||||||
4292 | /* Load the lowest cost path into pWInfo */ | - | ||||||||||||||||||||||||
4293 | for(iLoop=0; iLoop<nLoop; iLoop++){
| 319790-370798 | ||||||||||||||||||||||||
4294 | WhereLevel *pLevel = pWInfo->a + iLoop; | - | ||||||||||||||||||||||||
4295 | pLevel->pWLoop = pWLoop = pFrom->aLoop[iLoop]; | - | ||||||||||||||||||||||||
4296 | pLevel->iFrom = pWLoop->iTab; | - | ||||||||||||||||||||||||
4297 | pLevel->iTabCur = pWInfo->pTabList->a[pLevel->iFrom].iCursor; | - | ||||||||||||||||||||||||
4298 | } executed 319790 times by 435 tests: end of block Executed by:
| 319790 | ||||||||||||||||||||||||
4299 | if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT)!=0
| 497-370301 | ||||||||||||||||||||||||
4300 | && (pWInfo->wctrlFlags & WHERE_DISTINCTBY)==0
| 125-372 | ||||||||||||||||||||||||
4301 | && pWInfo->eDistinct==WHERE_DISTINCT_NOOP
| 27-98 | ||||||||||||||||||||||||
4302 | && nRowEst
| 49 | ||||||||||||||||||||||||
4303 | ){ | - | ||||||||||||||||||||||||
4304 | Bitmask notUsed; | - | ||||||||||||||||||||||||
4305 | int rc = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pResultSet, pFrom, | - | ||||||||||||||||||||||||
4306 | WHERE_DISTINCTBY, nLoop-1, pFrom->aLoop[nLoop-1], ¬Used); | - | ||||||||||||||||||||||||
4307 | if( rc==pWInfo->pResultSet->nExpr ){
| 10-39 | ||||||||||||||||||||||||
4308 | pWInfo->eDistinct = WHERE_DISTINCT_ORDERED; | - | ||||||||||||||||||||||||
4309 | } executed 10 times by 1 test: end of block Executed by:
| 10 | ||||||||||||||||||||||||
4310 | } executed 49 times by 1 test: end of block Executed by:
| 49 | ||||||||||||||||||||||||
4311 | pWInfo->bOrderedInnerLoop = 0; | - | ||||||||||||||||||||||||
4312 | if( pWInfo->pOrderBy ){
| 131674-239124 | ||||||||||||||||||||||||
4313 | if( pWInfo->wctrlFlags & WHERE_DISTINCTBY ){
| 372-131302 | ||||||||||||||||||||||||
4314 | if( pFrom->isOrdered==pWInfo->pOrderBy->nExpr ){
| 53-319 | ||||||||||||||||||||||||
4315 | pWInfo->eDistinct = WHERE_DISTINCT_ORDERED; | - | ||||||||||||||||||||||||
4316 | } executed 53 times by 1 test: end of block Executed by:
| 53 | ||||||||||||||||||||||||
4317 | }else{ executed 372 times by 1 test: end of block Executed by:
| 372 | ||||||||||||||||||||||||
4318 | pWInfo->nOBSat = pFrom->isOrdered; | - | ||||||||||||||||||||||||
4319 | pWInfo->revMask = pFrom->revLoop; | - | ||||||||||||||||||||||||
4320 | if( pWInfo->nOBSat<=0 ){
| 46235-85067 | ||||||||||||||||||||||||
4321 | pWInfo->nOBSat = 0; | - | ||||||||||||||||||||||||
4322 | if( nLoop>0 ){
| 90-84977 | ||||||||||||||||||||||||
4323 | u32 wsFlags = pFrom->aLoop[nLoop-1]->wsFlags; | - | ||||||||||||||||||||||||
4324 | if( (wsFlags & WHERE_ONEROW)==0
| 121-84856 | ||||||||||||||||||||||||
4325 | && (wsFlags&(WHERE_IPK|WHERE_COLUMN_IN))!=(WHERE_IPK|WHERE_COLUMN_IN)
| 10-84846 | ||||||||||||||||||||||||
4326 | ){ | - | ||||||||||||||||||||||||
4327 | Bitmask m = 0; | - | ||||||||||||||||||||||||
4328 | int rc = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pOrderBy, pFrom, | - | ||||||||||||||||||||||||
4329 | WHERE_ORDERBY_LIMIT, nLoop-1, pFrom->aLoop[nLoop-1], &m); | - | ||||||||||||||||||||||||
4330 | testcase( wsFlags & WHERE_IPK ); | - | ||||||||||||||||||||||||
4331 | testcase( wsFlags & WHERE_COLUMN_IN ); | - | ||||||||||||||||||||||||
4332 | if( rc==pWInfo->pOrderBy->nExpr ){
| 41789-43057 | ||||||||||||||||||||||||
4333 | pWInfo->bOrderedInnerLoop = 1; | - | ||||||||||||||||||||||||
4334 | pWInfo->revMask = m; | - | ||||||||||||||||||||||||
4335 | } executed 43057 times by 435 tests: end of block Executed by:
| 43057 | ||||||||||||||||||||||||
4336 | } executed 84846 times by 435 tests: end of block Executed by:
| 84846 | ||||||||||||||||||||||||
4337 | } executed 84977 times by 435 tests: end of block Executed by:
| 84977 | ||||||||||||||||||||||||
4338 | } executed 85067 times by 435 tests: end of block Executed by:
| 85067 | ||||||||||||||||||||||||
4339 | } executed 131302 times by 435 tests: end of block Executed by:
| 131302 | ||||||||||||||||||||||||
4340 | if( (pWInfo->wctrlFlags & WHERE_SORTBYGROUP)
| 3750-127924 | ||||||||||||||||||||||||
4341 | && pWInfo->nOBSat==pWInfo->pOrderBy->nExpr && nLoop>0
| 1-3727 | ||||||||||||||||||||||||
4342 | ){ | - | ||||||||||||||||||||||||
4343 | Bitmask revMask = 0; | - | ||||||||||||||||||||||||
4344 | int nOrder = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pOrderBy, | - | ||||||||||||||||||||||||
4345 | pFrom, 0, nLoop-1, pFrom->aLoop[nLoop-1], &revMask | - | ||||||||||||||||||||||||
4346 | ); | - | ||||||||||||||||||||||||
4347 | assert( pWInfo->sorted==0 ); | - | ||||||||||||||||||||||||
4348 | if( nOrder==pWInfo->pOrderBy->nExpr ){
| 4-18 | ||||||||||||||||||||||||
4349 | pWInfo->sorted = 1; | - | ||||||||||||||||||||||||
4350 | pWInfo->revMask = revMask; | - | ||||||||||||||||||||||||
4351 | } executed 18 times by 1 test: end of block Executed by:
| 18 | ||||||||||||||||||||||||
4352 | } executed 22 times by 1 test: end of block Executed by:
| 22 | ||||||||||||||||||||||||
4353 | } executed 131674 times by 435 tests: end of block Executed by:
| 131674 | ||||||||||||||||||||||||
4354 | - | |||||||||||||||||||||||||
4355 | - | |||||||||||||||||||||||||
4356 | pWInfo->nRowOut = pFrom->nRow; | - | ||||||||||||||||||||||||
4357 | - | |||||||||||||||||||||||||
4358 | /* Free temporary memory and return success */ | - | ||||||||||||||||||||||||
4359 | sqlite3DbFreeNN(db, pSpace); | - | ||||||||||||||||||||||||
4360 | return SQLITE_OK; executed 370798 times by 435 tests: return 0; Executed by:
| 370798 | ||||||||||||||||||||||||
4361 | } | - | ||||||||||||||||||||||||
4362 | - | |||||||||||||||||||||||||
4363 | /* | - | ||||||||||||||||||||||||
4364 | ** Most queries use only a single table (they are not joins) and have | - | ||||||||||||||||||||||||
4365 | ** simple == constraints against indexed fields. This routine attempts | - | ||||||||||||||||||||||||
4366 | ** to plan those simple cases using much less ceremony than the | - | ||||||||||||||||||||||||
4367 | ** general-purpose query planner, and thereby yield faster sqlite3_prepare() | - | ||||||||||||||||||||||||
4368 | ** times for the common case. | - | ||||||||||||||||||||||||
4369 | ** | - | ||||||||||||||||||||||||
4370 | ** Return non-zero on success, if this query can be handled by this | - | ||||||||||||||||||||||||
4371 | ** no-frills query planner. Return zero if this query needs the | - | ||||||||||||||||||||||||
4372 | ** general-purpose query planner. | - | ||||||||||||||||||||||||
4373 | */ | - | ||||||||||||||||||||||||
4374 | static int whereShortCut(WhereLoopBuilder *pBuilder){ | - | ||||||||||||||||||||||||
4375 | WhereInfo *pWInfo; | - | ||||||||||||||||||||||||
4376 | struct SrcList_item *pItem; | - | ||||||||||||||||||||||||
4377 | WhereClause *pWC; | - | ||||||||||||||||||||||||
4378 | WhereTerm *pTerm; | - | ||||||||||||||||||||||||
4379 | WhereLoop *pLoop; | - | ||||||||||||||||||||||||
4380 | int iCur; | - | ||||||||||||||||||||||||
4381 | int j; | - | ||||||||||||||||||||||||
4382 | Table *pTab; | - | ||||||||||||||||||||||||
4383 | Index *pIdx; | - | ||||||||||||||||||||||||
4384 | - | |||||||||||||||||||||||||
4385 | pWInfo = pBuilder->pWInfo; | - | ||||||||||||||||||||||||
4386 | if( pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE ) return 0; executed 17787 times by 1 test: return 0; Executed by:
| 17787-243171 | ||||||||||||||||||||||||
4387 | assert( pWInfo->pTabList->nSrc>=1 ); | - | ||||||||||||||||||||||||
4388 | pItem = pWInfo->pTabList->a; | - | ||||||||||||||||||||||||
4389 | pTab = pItem->pTab; | - | ||||||||||||||||||||||||
4390 | if( IsVirtual(pTab) ) return 0; executed 9775 times by 1 test: return 0; Executed by:
| 9775-233396 | ||||||||||||||||||||||||
4391 | if( pItem->fg.isIndexedBy ) return 0; executed 109 times by 2 tests: return 0; Executed by:
| 109-233287 | ||||||||||||||||||||||||
4392 | iCur = pItem->iCursor; | - | ||||||||||||||||||||||||
4393 | pWC = &pWInfo->sWC; | - | ||||||||||||||||||||||||
4394 | pLoop = pBuilder->pNew; | - | ||||||||||||||||||||||||
4395 | pLoop->wsFlags = 0; | - | ||||||||||||||||||||||||
4396 | pLoop->nSkip = 0; | - | ||||||||||||||||||||||||
4397 | pTerm = sqlite3WhereFindTerm(pWC, iCur, -1, 0, WO_EQ|WO_IS, 0); | - | ||||||||||||||||||||||||
4398 | if( pTerm ){
| 26485-206802 | ||||||||||||||||||||||||
4399 | testcase( pTerm->eOperator & WO_IS ); | - | ||||||||||||||||||||||||
4400 | pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_IPK|WHERE_ONEROW; | - | ||||||||||||||||||||||||
4401 | pLoop->aLTerm[0] = pTerm; | - | ||||||||||||||||||||||||
4402 | pLoop->nLTerm = 1; | - | ||||||||||||||||||||||||
4403 | pLoop->u.btree.nEq = 1; | - | ||||||||||||||||||||||||
4404 | /* TUNING: Cost of a rowid lookup is 10 */ | - | ||||||||||||||||||||||||
4405 | pLoop->rRun = 33; /* 33==sqlite3LogEst(10) */ | - | ||||||||||||||||||||||||
4406 | }else{ executed 26485 times by 30 tests: end of block Executed by:
| 26485 | ||||||||||||||||||||||||
4407 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
| 39509-206189 | ||||||||||||||||||||||||
4408 | int opMask; | - | ||||||||||||||||||||||||
4409 | assert( pLoop->aLTermSpace==pLoop->aLTerm ); | - | ||||||||||||||||||||||||
4410 | if( !IsUniqueIndex(pIdx)
| 5295-34214 | ||||||||||||||||||||||||
4411 | || pIdx->pPartIdxWhere!=0
| 7-5288 | ||||||||||||||||||||||||
4412 | || pIdx->nKeyCol>ArraySize(pLoop->aLTermSpace)
| 55-5233 | ||||||||||||||||||||||||
4413 | ) continue; executed 34276 times by 11 tests: continue; Executed by:
| 34276 | ||||||||||||||||||||||||
4414 | opMask = pIdx->uniqNotNull ? (WO_EQ|WO_IS) : WO_EQ;
| 1025-4208 | ||||||||||||||||||||||||
4415 | for(j=0; j<pIdx->nKeyCol; j++){
| 613-5313 | ||||||||||||||||||||||||
4416 | pTerm = sqlite3WhereFindTerm(pWC, iCur, j, 0, opMask, pIdx); | - | ||||||||||||||||||||||||
4417 | if( pTerm==0 ) break; executed 4620 times by 347 tests: break; Executed by:
| 693-4620 | ||||||||||||||||||||||||
4418 | testcase( pTerm->eOperator & WO_IS ); | - | ||||||||||||||||||||||||
4419 | pLoop->aLTerm[j] = pTerm; | - | ||||||||||||||||||||||||
4420 | } executed 693 times by 1 test: end of block Executed by:
| 693 | ||||||||||||||||||||||||
4421 | if( j!=pIdx->nKeyCol ) continue; executed 4620 times by 347 tests: continue; Executed by:
| 613-4620 | ||||||||||||||||||||||||
4422 | pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_ONEROW|WHERE_INDEXED; | - | ||||||||||||||||||||||||
4423 | if( pIdx->isCovering || (pItem->colUsed & pIdx->colNotIdxed)==0 ){
| 147-466 | ||||||||||||||||||||||||
4424 | pLoop->wsFlags |= WHERE_IDX_ONLY; | - | ||||||||||||||||||||||||
4425 | } executed 324 times by 1 test: end of block Executed by:
| 324 | ||||||||||||||||||||||||
4426 | pLoop->nLTerm = j; | - | ||||||||||||||||||||||||
4427 | pLoop->u.btree.nEq = j; | - | ||||||||||||||||||||||||
4428 | pLoop->u.btree.pIndex = pIdx; | - | ||||||||||||||||||||||||
4429 | /* TUNING: Cost of a unique index lookup is 15 */ | - | ||||||||||||||||||||||||
4430 | pLoop->rRun = 39; /* 39==sqlite3LogEst(15) */ | - | ||||||||||||||||||||||||
4431 | break; executed 613 times by 1 test: break; Executed by:
| 613 | ||||||||||||||||||||||||
4432 | } | - | ||||||||||||||||||||||||
4433 | } executed 206802 times by 435 tests: end of block Executed by:
| 206802 | ||||||||||||||||||||||||
4434 | if( pLoop->wsFlags ){
| 27098-206189 | ||||||||||||||||||||||||
4435 | pLoop->nOut = (LogEst)1; | - | ||||||||||||||||||||||||
4436 | pWInfo->a[0].pWLoop = pLoop; | - | ||||||||||||||||||||||||
4437 | assert( pWInfo->sMaskSet.n==1 && iCur==pWInfo->sMaskSet.ix[0] ); | - | ||||||||||||||||||||||||
4438 | pLoop->maskSelf = 1; /* sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur); */ | - | ||||||||||||||||||||||||
4439 | pWInfo->a[0].iTabCur = iCur; | - | ||||||||||||||||||||||||
4440 | pWInfo->nRowOut = 1; | - | ||||||||||||||||||||||||
4441 | if( pWInfo->pOrderBy ) pWInfo->nOBSat = pWInfo->pOrderBy->nExpr; executed 15 times by 1 test: pWInfo->nOBSat = pWInfo->pOrderBy->nExpr; Executed by:
| 15-27083 | ||||||||||||||||||||||||
4442 | if( pWInfo->wctrlFlags & WHERE_WANT_DISTINCT ){
| 2-27096 | ||||||||||||||||||||||||
4443 | pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE; | - | ||||||||||||||||||||||||
4444 | } executed 2 times by 1 test: end of block Executed by:
| 2 | ||||||||||||||||||||||||
4445 | #ifdef SQLITE_DEBUG | - | ||||||||||||||||||||||||
4446 | pLoop->cId = '0'; | - | ||||||||||||||||||||||||
4447 | #endif | - | ||||||||||||||||||||||||
4448 | return 1; executed 27098 times by 30 tests: return 1; Executed by:
| 27098 | ||||||||||||||||||||||||
4449 | } | - | ||||||||||||||||||||||||
4450 | return 0; executed 206189 times by 435 tests: return 0; Executed by:
| 206189 | ||||||||||||||||||||||||
4451 | } | - | ||||||||||||||||||||||||
4452 | - | |||||||||||||||||||||||||
4453 | /* | - | ||||||||||||||||||||||||
4454 | ** Helper function for exprIsDeterministic(). | - | ||||||||||||||||||||||||
4455 | */ | - | ||||||||||||||||||||||||
4456 | static int exprNodeIsDeterministic(Walker *pWalker, Expr *pExpr){ | - | ||||||||||||||||||||||||
4457 | if( pExpr->op==TK_FUNCTION && ExprHasProperty(pExpr, EP_ConstFunc)==0 ){
| 41-10732 | ||||||||||||||||||||||||
4458 | pWalker->eCode = 0; | - | ||||||||||||||||||||||||
4459 | return WRC_Abort; executed 192 times by 1 test: return 2; Executed by:
| 192 | ||||||||||||||||||||||||
4460 | } | - | ||||||||||||||||||||||||
4461 | return WRC_Continue; executed 10773 times by 4 tests: return 0; Executed by:
| 10773 | ||||||||||||||||||||||||
4462 | } | - | ||||||||||||||||||||||||
4463 | - | |||||||||||||||||||||||||
4464 | /* | - | ||||||||||||||||||||||||
4465 | ** Return true if the expression contains no non-deterministic SQL | - | ||||||||||||||||||||||||
4466 | ** functions. Do not consider non-deterministic SQL functions that are | - | ||||||||||||||||||||||||
4467 | ** part of sub-select statements. | - | ||||||||||||||||||||||||
4468 | */ | - | ||||||||||||||||||||||||
4469 | static int exprIsDeterministic(Expr *p){ | - | ||||||||||||||||||||||||
4470 | Walker w; | - | ||||||||||||||||||||||||
4471 | memset(&w, 0, sizeof(w)); | - | ||||||||||||||||||||||||
4472 | w.eCode = 1; | - | ||||||||||||||||||||||||
4473 | w.xExprCallback = exprNodeIsDeterministic; | - | ||||||||||||||||||||||||
4474 | w.xSelectCallback = sqlite3SelectWalkFail; | - | ||||||||||||||||||||||||
4475 | sqlite3WalkExpr(&w, p); | - | ||||||||||||||||||||||||
4476 | return w.eCode; executed 8064 times by 4 tests: return w.eCode; Executed by:
| 8064 | ||||||||||||||||||||||||
4477 | } | - | ||||||||||||||||||||||||
4478 | - | |||||||||||||||||||||||||
4479 | /* | - | ||||||||||||||||||||||||
4480 | ** Generate the beginning of the loop used for WHERE clause processing. | - | ||||||||||||||||||||||||
4481 | ** The return value is a pointer to an opaque structure that contains | - | ||||||||||||||||||||||||
4482 | ** information needed to terminate the loop. Later, the calling routine | - | ||||||||||||||||||||||||
4483 | ** should invoke sqlite3WhereEnd() with the return value of this function | - | ||||||||||||||||||||||||
4484 | ** in order to complete the WHERE clause processing. | - | ||||||||||||||||||||||||
4485 | ** | - | ||||||||||||||||||||||||
4486 | ** If an error occurs, this routine returns NULL. | - | ||||||||||||||||||||||||
4487 | ** | - | ||||||||||||||||||||||||
4488 | ** The basic idea is to do a nested loop, one loop for each table in | - | ||||||||||||||||||||||||
4489 | ** the FROM clause of a select. (INSERT and UPDATE statements are the | - | ||||||||||||||||||||||||
4490 | ** same as a SELECT with only a single table in the FROM clause.) For | - | ||||||||||||||||||||||||
4491 | ** example, if the SQL is this: | - | ||||||||||||||||||||||||
4492 | ** | - | ||||||||||||||||||||||||
4493 | ** SELECT * FROM t1, t2, t3 WHERE ...; | - | ||||||||||||||||||||||||
4494 | ** | - | ||||||||||||||||||||||||
4495 | ** Then the code generated is conceptually like the following: | - | ||||||||||||||||||||||||
4496 | ** | - | ||||||||||||||||||||||||
4497 | ** foreach row1 in t1 do \ Code generated | - | ||||||||||||||||||||||||
4498 | ** foreach row2 in t2 do |-- by sqlite3WhereBegin() | - | ||||||||||||||||||||||||
4499 | ** foreach row3 in t3 do / | - | ||||||||||||||||||||||||
4500 | ** ... | - | ||||||||||||||||||||||||
4501 | ** end \ Code generated | - | ||||||||||||||||||||||||
4502 | ** end |-- by sqlite3WhereEnd() | - | ||||||||||||||||||||||||
4503 | ** end / | - | ||||||||||||||||||||||||
4504 | ** | - | ||||||||||||||||||||||||
4505 | ** Note that the loops might not be nested in the order in which they | - | ||||||||||||||||||||||||
4506 | ** appear in the FROM clause if a different order is better able to make | - | ||||||||||||||||||||||||
4507 | ** use of indices. Note also that when the IN operator appears in | - | ||||||||||||||||||||||||
4508 | ** the WHERE clause, it might result in additional nested loops for | - | ||||||||||||||||||||||||
4509 | ** scanning through all values on the right-hand side of the IN. | - | ||||||||||||||||||||||||
4510 | ** | - | ||||||||||||||||||||||||
4511 | ** There are Btree cursors associated with each table. t1 uses cursor | - | ||||||||||||||||||||||||
4512 | ** number pTabList->a[0].iCursor. t2 uses the cursor pTabList->a[1].iCursor. | - | ||||||||||||||||||||||||
4513 | ** And so forth. This routine generates code to open those VDBE cursors | - | ||||||||||||||||||||||||
4514 | ** and sqlite3WhereEnd() generates the code to close them. | - | ||||||||||||||||||||||||
4515 | ** | - | ||||||||||||||||||||||||
4516 | ** The code that sqlite3WhereBegin() generates leaves the cursors named | - | ||||||||||||||||||||||||
4517 | ** in pTabList pointing at their appropriate entries. The [...] code | - | ||||||||||||||||||||||||
4518 | ** can use OP_Column and OP_Rowid opcodes on these cursors to extract | - | ||||||||||||||||||||||||
4519 | ** data from the various tables of the loop. | - | ||||||||||||||||||||||||
4520 | ** | - | ||||||||||||||||||||||||
4521 | ** If the WHERE clause is empty, the foreach loops must each scan their | - | ||||||||||||||||||||||||
4522 | ** entire tables. Thus a three-way join is an O(N^3) operation. But if | - | ||||||||||||||||||||||||
4523 | ** the tables have indices and there are terms in the WHERE clause that | - | ||||||||||||||||||||||||
4524 | ** refer to those indices, a complete table scan can be avoided and the | - | ||||||||||||||||||||||||
4525 | ** code will run much faster. Most of the work of this routine is checking | - | ||||||||||||||||||||||||
4526 | ** to see if there are indices that can be used to speed up the loop. | - | ||||||||||||||||||||||||
4527 | ** | - | ||||||||||||||||||||||||
4528 | ** Terms of the WHERE clause are also used to limit which rows actually | - | ||||||||||||||||||||||||
4529 | ** make it to the "..." in the middle of the loop. After each "foreach", | - | ||||||||||||||||||||||||
4530 | ** terms of the WHERE clause that use only terms in that loop and outer | - | ||||||||||||||||||||||||
4531 | ** loops are evaluated and if false a jump is made around all subsequent | - | ||||||||||||||||||||||||
4532 | ** inner loops (or around the "..." if the test occurs within the inner- | - | ||||||||||||||||||||||||
4533 | ** most loop) | - | ||||||||||||||||||||||||
4534 | ** | - | ||||||||||||||||||||||||
4535 | ** OUTER JOINS | - | ||||||||||||||||||||||||
4536 | ** | - | ||||||||||||||||||||||||
4537 | ** An outer join of tables t1 and t2 is conceptally coded as follows: | - | ||||||||||||||||||||||||
4538 | ** | - | ||||||||||||||||||||||||
4539 | ** foreach row1 in t1 do | - | ||||||||||||||||||||||||
4540 | ** flag = 0 | - | ||||||||||||||||||||||||
4541 | ** foreach row2 in t2 do | - | ||||||||||||||||||||||||
4542 | ** start: | - | ||||||||||||||||||||||||
4543 | ** ... | - | ||||||||||||||||||||||||
4544 | ** flag = 1 | - | ||||||||||||||||||||||||
4545 | ** end | - | ||||||||||||||||||||||||
4546 | ** if flag==0 then | - | ||||||||||||||||||||||||
4547 | ** move the row2 cursor to a null row | - | ||||||||||||||||||||||||
4548 | ** goto start | - | ||||||||||||||||||||||||
4549 | ** fi | - | ||||||||||||||||||||||||
4550 | ** end | - | ||||||||||||||||||||||||
4551 | ** | - | ||||||||||||||||||||||||
4552 | ** ORDER BY CLAUSE PROCESSING | - | ||||||||||||||||||||||||
4553 | ** | - | ||||||||||||||||||||||||
4554 | ** pOrderBy is a pointer to the ORDER BY clause (or the GROUP BY clause | - | ||||||||||||||||||||||||
4555 | ** if the WHERE_GROUPBY flag is set in wctrlFlags) of a SELECT statement | - | ||||||||||||||||||||||||
4556 | ** if there is one. If there is no ORDER BY clause or if this routine | - | ||||||||||||||||||||||||
4557 | ** is called from an UPDATE or DELETE statement, then pOrderBy is NULL. | - | ||||||||||||||||||||||||
4558 | ** | - | ||||||||||||||||||||||||
4559 | ** The iIdxCur parameter is the cursor number of an index. If | - | ||||||||||||||||||||||||
4560 | ** WHERE_OR_SUBCLAUSE is set, iIdxCur is the cursor number of an index | - | ||||||||||||||||||||||||
4561 | ** to use for OR clause processing. The WHERE clause should use this | - | ||||||||||||||||||||||||
4562 | ** specific cursor. If WHERE_ONEPASS_DESIRED is set, then iIdxCur is | - | ||||||||||||||||||||||||
4563 | ** the first cursor in an array of cursors for all indices. iIdxCur should | - | ||||||||||||||||||||||||
4564 | ** be used to compute the appropriate cursor depending on which index is | - | ||||||||||||||||||||||||
4565 | ** used. | - | ||||||||||||||||||||||||
4566 | */ | - | ||||||||||||||||||||||||
4567 | WhereInfo *sqlite3WhereBegin( | - | ||||||||||||||||||||||||
4568 | Parse *pParse, /* The parser context */ | - | ||||||||||||||||||||||||
4569 | SrcList *pTabList, /* FROM clause: A list of all tables to be scanned */ | - | ||||||||||||||||||||||||
4570 | Expr *pWhere, /* The WHERE clause */ | - | ||||||||||||||||||||||||
4571 | ExprList *pOrderBy, /* An ORDER BY (or GROUP BY) clause, or NULL */ | - | ||||||||||||||||||||||||
4572 | ExprList *pResultSet, /* Query result set. Req'd for DISTINCT */ | - | ||||||||||||||||||||||||
4573 | u16 wctrlFlags, /* The WHERE_* flags defined in sqliteInt.h */ | - | ||||||||||||||||||||||||
4574 | int iAuxArg /* If WHERE_OR_SUBCLAUSE is set, index cursor number | - | ||||||||||||||||||||||||
4575 | ** If WHERE_USE_LIMIT, then the limit amount */ | - | ||||||||||||||||||||||||
4576 | ){ | - | ||||||||||||||||||||||||
4577 | int nByteWInfo; /* Num. bytes allocated for WhereInfo struct */ | - | ||||||||||||||||||||||||
4578 | int nTabList; /* Number of elements in pTabList */ | - | ||||||||||||||||||||||||
4579 | WhereInfo *pWInfo; /* Will become the return value of this function */ | - | ||||||||||||||||||||||||
4580 | Vdbe *v = pParse->pVdbe; /* The virtual database engine */ | - | ||||||||||||||||||||||||
4581 | Bitmask notReady; /* Cursors that are not yet positioned */ | - | ||||||||||||||||||||||||
4582 | WhereLoopBuilder sWLB; /* The WhereLoop builder */ | - | ||||||||||||||||||||||||
4583 | WhereMaskSet *pMaskSet; /* The expression mask set */ | - | ||||||||||||||||||||||||
4584 | WhereLevel *pLevel; /* A single level in pWInfo->a[] */ | - | ||||||||||||||||||||||||
4585 | WhereLoop *pLoop; /* Pointer to a single WhereLoop object */ | - | ||||||||||||||||||||||||
4586 | int ii; /* Loop counter */ | - | ||||||||||||||||||||||||
4587 | sqlite3 *db; /* Database connection */ | - | ||||||||||||||||||||||||
4588 | int rc; /* Return code */ | - | ||||||||||||||||||||||||
4589 | u8 bFordelete = 0; /* OPFLAG_FORDELETE or zero, as appropriate */ | - | ||||||||||||||||||||||||
4590 | - | |||||||||||||||||||||||||
4591 | assert( (wctrlFlags & WHERE_ONEPASS_MULTIROW)==0 || ( | - | ||||||||||||||||||||||||
4592 | (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0 | - | ||||||||||||||||||||||||
4593 | && (wctrlFlags & WHERE_OR_SUBCLAUSE)==0 | - | ||||||||||||||||||||||||
4594 | )); | - | ||||||||||||||||||||||||
4595 | - | |||||||||||||||||||||||||
4596 | /* Only one of WHERE_OR_SUBCLAUSE or WHERE_USE_LIMIT */ | - | ||||||||||||||||||||||||
4597 | assert( (wctrlFlags & WHERE_OR_SUBCLAUSE)==0 | - | ||||||||||||||||||||||||
4598 | || (wctrlFlags & WHERE_USE_LIMIT)==0 ); | - | ||||||||||||||||||||||||
4599 | - | |||||||||||||||||||||||||
4600 | /* Variable initialization */ | - | ||||||||||||||||||||||||
4601 | db = pParse->db; | - | ||||||||||||||||||||||||
4602 | memset(&sWLB, 0, sizeof(sWLB)); | - | ||||||||||||||||||||||||
4603 | - | |||||||||||||||||||||||||
4604 | /* An ORDER/GROUP BY clause of more than 63 terms cannot be optimized */ | - | ||||||||||||||||||||||||
4605 | testcase( pOrderBy && pOrderBy->nExpr==BMS-1 ); | - | ||||||||||||||||||||||||
4606 | if( pOrderBy && pOrderBy->nExpr>=BMS ) pOrderBy = 0; never executed: pOrderBy = 0;
| 0-267147 | ||||||||||||||||||||||||
4607 | sWLB.pOrderBy = pOrderBy; | - | ||||||||||||||||||||||||
4608 | - | |||||||||||||||||||||||||
4609 | /* Disable the DISTINCT optimization if SQLITE_DistinctOpt is set via | - | ||||||||||||||||||||||||
4610 | ** sqlite3_test_ctrl(SQLITE_TESTCTRL_OPTIMIZATIONS,...) */ | - | ||||||||||||||||||||||||
4611 | if( OptimizationDisabled(db, SQLITE_DistinctOpt) ){
| 86-332868 | ||||||||||||||||||||||||
4612 | wctrlFlags &= ~WHERE_WANT_DISTINCT; | - | ||||||||||||||||||||||||
4613 | } executed 86 times by 1 test: end of block Executed by:
| 86 | ||||||||||||||||||||||||
4614 | - | |||||||||||||||||||||||||
4615 | /* The number of tables in the FROM clause is limited by the number of | - | ||||||||||||||||||||||||
4616 | ** bits in a Bitmask | - | ||||||||||||||||||||||||
4617 | */ | - | ||||||||||||||||||||||||
4618 | testcase( pTabList->nSrc==BMS ); | - | ||||||||||||||||||||||||
4619 | if( pTabList->nSrc>BMS ){
| 7-332947 | ||||||||||||||||||||||||
4620 | sqlite3ErrorMsg(pParse, "at most %d tables in a join", BMS); | - | ||||||||||||||||||||||||
4621 | return 0; executed 7 times by 1 test: return 0; Executed by:
| 7 | ||||||||||||||||||||||||
4622 | } | - | ||||||||||||||||||||||||
4623 | - | |||||||||||||||||||||||||
4624 | /* This function normally generates a nested loop for all tables in | - | ||||||||||||||||||||||||
4625 | ** pTabList. But if the WHERE_OR_SUBCLAUSE flag is set, then we should | - | ||||||||||||||||||||||||
4626 | ** only generate code for the first table in pTabList and assume that | - | ||||||||||||||||||||||||
4627 | ** any cursors associated with subsequent tables are uninitialized. | - | ||||||||||||||||||||||||
4628 | */ | - | ||||||||||||||||||||||||
4629 | nTabList = (wctrlFlags & WHERE_OR_SUBCLAUSE) ? 1 : pTabList->nSrc;
| 17787-315160 | ||||||||||||||||||||||||
4630 | - | |||||||||||||||||||||||||
4631 | /* Allocate and initialize the WhereInfo structure that will become the | - | ||||||||||||||||||||||||
4632 | ** return value. A single allocation is used to store the WhereInfo | - | ||||||||||||||||||||||||
4633 | ** struct, the contents of WhereInfo.a[], the WhereClause structure | - | ||||||||||||||||||||||||
4634 | ** and the WhereMaskSet structure. Since WhereClause contains an 8-byte | - | ||||||||||||||||||||||||
4635 | ** field (type Bitmask) it must be aligned on an 8-byte boundary on | - | ||||||||||||||||||||||||
4636 | ** some architectures. Hence the ROUND8() below. | - | ||||||||||||||||||||||||
4637 | */ | - | ||||||||||||||||||||||||
4638 | nByteWInfo = ROUND8(sizeof(WhereInfo)+(nTabList-1)*sizeof(WhereLevel)); | - | ||||||||||||||||||||||||
4639 | pWInfo = sqlite3DbMallocRawNN(db, nByteWInfo + sizeof(WhereLoop)); | - | ||||||||||||||||||||||||
4640 | if( db->mallocFailed ){
| 432-332515 | ||||||||||||||||||||||||
4641 | sqlite3DbFree(db, pWInfo); | - | ||||||||||||||||||||||||
4642 | pWInfo = 0; | - | ||||||||||||||||||||||||
4643 | goto whereBeginError; executed 432 times by 1 test: goto whereBeginError; Executed by:
| 432 | ||||||||||||||||||||||||
4644 | } | - | ||||||||||||||||||||||||
4645 | pWInfo->pParse = pParse; | - | ||||||||||||||||||||||||
4646 | pWInfo->pTabList = pTabList; | - | ||||||||||||||||||||||||
4647 | pWInfo->pOrderBy = pOrderBy; | - | ||||||||||||||||||||||||
4648 | pWInfo->pWhere = pWhere; | - | ||||||||||||||||||||||||
4649 | pWInfo->pResultSet = pResultSet; | - | ||||||||||||||||||||||||
4650 | pWInfo->aiCurOnePass[0] = pWInfo->aiCurOnePass[1] = -1; | - | ||||||||||||||||||||||||
4651 | pWInfo->nLevel = nTabList; | - | ||||||||||||||||||||||||
4652 | pWInfo->iBreak = pWInfo->iContinue = sqlite3VdbeMakeLabel(v); | - | ||||||||||||||||||||||||
4653 | pWInfo->wctrlFlags = wctrlFlags; | - | ||||||||||||||||||||||||
4654 | pWInfo->iLimit = iAuxArg; | - | ||||||||||||||||||||||||
4655 | pWInfo->savedNQueryLoop = pParse->nQueryLoop; | - | ||||||||||||||||||||||||
4656 | memset(&pWInfo->nOBSat, 0, | - | ||||||||||||||||||||||||
4657 | offsetof(WhereInfo,sWC) - offsetof(WhereInfo,nOBSat)); | - | ||||||||||||||||||||||||
4658 | memset(&pWInfo->a[0], 0, sizeof(WhereLoop)+nTabList*sizeof(WhereLevel)); | - | ||||||||||||||||||||||||
4659 | assert( pWInfo->eOnePass==ONEPASS_OFF ); /* ONEPASS defaults to OFF */ | - | ||||||||||||||||||||||||
4660 | pMaskSet = &pWInfo->sMaskSet; | - | ||||||||||||||||||||||||
4661 | sWLB.pWInfo = pWInfo; | - | ||||||||||||||||||||||||
4662 | sWLB.pWC = &pWInfo->sWC; | - | ||||||||||||||||||||||||
4663 | sWLB.pNew = (WhereLoop*)(((char*)pWInfo)+nByteWInfo); | - | ||||||||||||||||||||||||
4664 | assert( EIGHT_BYTE_ALIGNMENT(sWLB.pNew) ); | - | ||||||||||||||||||||||||
4665 | whereLoopInit(sWLB.pNew); | - | ||||||||||||||||||||||||
4666 | #ifdef SQLITE_DEBUG | - | ||||||||||||||||||||||||
4667 | sWLB.pNew->cId = '*'; | - | ||||||||||||||||||||||||
4668 | #endif | - | ||||||||||||||||||||||||
4669 | - | |||||||||||||||||||||||||
4670 | /* Split the WHERE clause into separate subexpressions where each | - | ||||||||||||||||||||||||
4671 | ** subexpression is separated by an AND operator. | - | ||||||||||||||||||||||||
4672 | */ | - | ||||||||||||||||||||||||
4673 | initMaskSet(pMaskSet); | - | ||||||||||||||||||||||||
4674 | sqlite3WhereClauseInit(&pWInfo->sWC, pWInfo); | - | ||||||||||||||||||||||||
4675 | sqlite3WhereSplit(&pWInfo->sWC, pWhere, TK_AND); | - | ||||||||||||||||||||||||
4676 | - | |||||||||||||||||||||||||
4677 | /* Special case: No FROM clause | - | ||||||||||||||||||||||||
4678 | */ | - | ||||||||||||||||||||||||
4679 | if( nTabList==0 ){
| 64640-267875 | ||||||||||||||||||||||||
4680 | if( pOrderBy ) pWInfo->nOBSat = pOrderBy->nExpr; executed 90 times by 1 test: pWInfo->nOBSat = pOrderBy->nExpr; Executed by:
| 90-64550 | ||||||||||||||||||||||||
4681 | if( wctrlFlags & WHERE_WANT_DISTINCT ){
| 10-64630 | ||||||||||||||||||||||||
4682 | pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE; | - | ||||||||||||||||||||||||
4683 | } executed 10 times by 1 test: end of block Executed by:
| 10 | ||||||||||||||||||||||||
4684 | ExplainQueryPlan((pParse, 0, "SCAN CONSTANT ROW")); | - | ||||||||||||||||||||||||
4685 | }else{ executed 64640 times by 1 test: end of block Executed by:
| 64640 | ||||||||||||||||||||||||
4686 | /* Assign a bit from the bitmask to every term in the FROM clause. | - | ||||||||||||||||||||||||
4687 | ** | - | ||||||||||||||||||||||||
4688 | ** The N-th term of the FROM clause is assigned a bitmask of 1<<N. | - | ||||||||||||||||||||||||
4689 | ** | - | ||||||||||||||||||||||||
4690 | ** The rule of the previous sentence ensures thta if X is the bitmask for | - | ||||||||||||||||||||||||
4691 | ** a table T, then X-1 is the bitmask for all other tables to the left of T. | - | ||||||||||||||||||||||||
4692 | ** Knowing the bitmask for all tables to the left of a left join is | - | ||||||||||||||||||||||||
4693 | ** important. Ticket #3015. | - | ||||||||||||||||||||||||
4694 | ** | - | ||||||||||||||||||||||||
4695 | ** Note that bitmasks are created for all pTabList->nSrc tables in | - | ||||||||||||||||||||||||
4696 | ** pTabList, not just the first nTabList tables. nTabList is normally | - | ||||||||||||||||||||||||
4697 | ** equal to pTabList->nSrc but might be shortened to 1 if the | - | ||||||||||||||||||||||||
4698 | ** WHERE_OR_SUBCLAUSE flag is set. | - | ||||||||||||||||||||||||
4699 | */ | - | ||||||||||||||||||||||||
4700 | ii = 0; | - | ||||||||||||||||||||||||
4701 | do{ | - | ||||||||||||||||||||||||
4702 | createMask(pMaskSet, pTabList->a[ii].iCursor); | - | ||||||||||||||||||||||||
4703 | sqlite3WhereTabFuncArgs(pParse, &pTabList->a[ii], &pWInfo->sWC); | - | ||||||||||||||||||||||||
4704 | }while( (++ii)<pTabList->nSrc ); executed 279537 times by 435 tests: end of block Executed by:
| 11662-279537 | ||||||||||||||||||||||||
4705 | #ifdef SQLITE_DEBUG | - | ||||||||||||||||||||||||
4706 | { | - | ||||||||||||||||||||||||
4707 | Bitmask mx = 0; | - | ||||||||||||||||||||||||
4708 | for(ii=0; ii<pTabList->nSrc; ii++){ | - | ||||||||||||||||||||||||
4709 | Bitmask m = sqlite3WhereGetMask(pMaskSet, pTabList->a[ii].iCursor); | - | ||||||||||||||||||||||||
4710 | assert( m>=mx ); | - | ||||||||||||||||||||||||
4711 | mx = m; | - | ||||||||||||||||||||||||
4712 | } | - | ||||||||||||||||||||||||
4713 | } | - | ||||||||||||||||||||||||
4714 | #endif | - | ||||||||||||||||||||||||
4715 | } executed 267875 times by 435 tests: end of block Executed by:
| 267875 | ||||||||||||||||||||||||
4716 | - | |||||||||||||||||||||||||
4717 | /* Analyze all of the subexpressions. */ | - | ||||||||||||||||||||||||
4718 | sqlite3WhereExprAnalyze(pTabList, &pWInfo->sWC); | - | ||||||||||||||||||||||||
4719 | if( db->mallocFailed ) goto whereBeginError; executed 63 times by 1 test: goto whereBeginError; Executed by:
| 63-332452 | ||||||||||||||||||||||||
4720 | - | |||||||||||||||||||||||||
4721 | /* Special case: WHERE terms that do not refer to any tables in the join | - | ||||||||||||||||||||||||
4722 | ** (constant expressions). Evaluate each such term, and jump over all the | - | ||||||||||||||||||||||||
4723 | ** generated code if the result is not true. | - | ||||||||||||||||||||||||
4724 | ** | - | ||||||||||||||||||||||||
4725 | ** Do not do this if the expression contains non-deterministic functions | - | ||||||||||||||||||||||||
4726 | ** that are not within a sub-select. This is not strictly required, but | - | ||||||||||||||||||||||||
4727 | ** preserves SQLite's legacy behaviour in the following two cases: | - | ||||||||||||||||||||||||
4728 | ** | - | ||||||||||||||||||||||||
4729 | ** FROM ... WHERE random()>0; -- eval random() once per row | - | ||||||||||||||||||||||||
4730 | ** FROM ... WHERE (SELECT random())>0; -- eval random() once overall | - | ||||||||||||||||||||||||
4731 | */ | - | ||||||||||||||||||||||||
4732 | for(ii=0; ii<sWLB.pWC->nTerm; ii++){
| 224468-332452 | ||||||||||||||||||||||||
4733 | WhereTerm *pT = &sWLB.pWC->a[ii]; | - | ||||||||||||||||||||||||
4734 | if( pT->wtFlags & TERM_VIRTUAL ) continue; executed 21772 times by 1 test: continue; Executed by:
| 21772-202696 | ||||||||||||||||||||||||
4735 | if( pT->prereqAll==0 && (nTabList==0 || exprIsDeterministic(pT->pExpr)) ){
| 82-194550 | ||||||||||||||||||||||||
4736 | sqlite3ExprIfFalse(pParse, pT->pExpr, pWInfo->iBreak, SQLITE_JUMPIFNULL); | - | ||||||||||||||||||||||||
4737 | pT->wtFlags |= TERM_CODED; | - | ||||||||||||||||||||||||
4738 | } executed 6262 times by 4 tests: end of block Executed by:
| 6262 | ||||||||||||||||||||||||
4739 | } executed 202696 times by 368 tests: end of block Executed by:
| 202696 | ||||||||||||||||||||||||
4740 | - | |||||||||||||||||||||||||
4741 | if( wctrlFlags & WHERE_WANT_DISTINCT ){
| 259-332193 | ||||||||||||||||||||||||
4742 | if( isDistinctRedundant(pParse, pTabList, &pWInfo->sWC, pResultSet) ){
| 19-240 | ||||||||||||||||||||||||
4743 | /* The DISTINCT marking is pointless. Ignore it. */ | - | ||||||||||||||||||||||||
4744 | pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE; | - | ||||||||||||||||||||||||
4745 | }else if( pOrderBy==0 ){ executed 19 times by 1 test: end of block Executed by:
| 19-186 | ||||||||||||||||||||||||
4746 | /* Try to ORDER BY the result set to make distinct processing easier */ | - | ||||||||||||||||||||||||
4747 | pWInfo->wctrlFlags |= WHERE_DISTINCTBY; | - | ||||||||||||||||||||||||
4748 | pWInfo->pOrderBy = pResultSet; | - | ||||||||||||||||||||||||
4749 | } executed 186 times by 1 test: end of block Executed by:
| 186 | ||||||||||||||||||||||||
4750 | } executed 259 times by 1 test: end of block Executed by:
| 259 | ||||||||||||||||||||||||
4751 | - | |||||||||||||||||||||||||
4752 | /* Construct the WhereLoop objects */ | - | ||||||||||||||||||||||||
4753 | #if defined(WHERETRACE_ENABLED) | - | ||||||||||||||||||||||||
4754 | if( sqlite3WhereTrace & 0xffff ){ | - | ||||||||||||||||||||||||
4755 | sqlite3DebugPrintf("*** Optimizer Start *** (wctrlFlags: 0x%x",wctrlFlags); | - | ||||||||||||||||||||||||
4756 | if( wctrlFlags & WHERE_USE_LIMIT ){ | - | ||||||||||||||||||||||||
4757 | sqlite3DebugPrintf(", limit: %d", iAuxArg); | - | ||||||||||||||||||||||||
4758 | } | - | ||||||||||||||||||||||||
4759 | sqlite3DebugPrintf(")\n"); | - | ||||||||||||||||||||||||
4760 | } | - | ||||||||||||||||||||||||
4761 | if( sqlite3WhereTrace & 0x100 ){ /* Display all terms of the WHERE clause */ | - | ||||||||||||||||||||||||
4762 | sqlite3WhereClausePrint(sWLB.pWC); | - | ||||||||||||||||||||||||
4763 | } | - | ||||||||||||||||||||||||
4764 | #endif | - | ||||||||||||||||||||||||
4765 | - | |||||||||||||||||||||||||
4766 | if( nTabList!=1 || whereShortCut(&sWLB)==0 ){
| 27098-260958 | ||||||||||||||||||||||||
4767 | rc = whereLoopAddAll(&sWLB); | - | ||||||||||||||||||||||||
4768 | if( rc ) goto whereBeginError; executed 327 times by 1 test: goto whereBeginError; Executed by:
| 327-305027 | ||||||||||||||||||||||||
4769 | - | |||||||||||||||||||||||||
4770 | #ifdef WHERETRACE_ENABLED | - | ||||||||||||||||||||||||
4771 | if( sqlite3WhereTrace ){ /* Display all of the WhereLoop objects */ | - | ||||||||||||||||||||||||
4772 | WhereLoop *p; | - | ||||||||||||||||||||||||
4773 | int i; | - | ||||||||||||||||||||||||
4774 | static const char zLabel[] = "0123456789abcdefghijklmnopqrstuvwyxz" | - | ||||||||||||||||||||||||
4775 | "ABCDEFGHIJKLMNOPQRSTUVWYXZ"; | - | ||||||||||||||||||||||||
4776 | for(p=pWInfo->pLoops, i=0; p; p=p->pNextLoop, i++){ | - | ||||||||||||||||||||||||
4777 | p->cId = zLabel[i%(sizeof(zLabel)-1)]; | - | ||||||||||||||||||||||||
4778 | whereLoopPrint(p, sWLB.pWC); | - | ||||||||||||||||||||||||
4779 | } | - | ||||||||||||||||||||||||
4780 | } | - | ||||||||||||||||||||||||
4781 | #endif | - | ||||||||||||||||||||||||
4782 | - | |||||||||||||||||||||||||
4783 | wherePathSolver(pWInfo, 0); | - | ||||||||||||||||||||||||
4784 | if( db->mallocFailed ) goto whereBeginError; executed 41 times by 1 test: goto whereBeginError; Executed by:
| 41-304986 | ||||||||||||||||||||||||
4785 | if( pWInfo->pOrderBy ){
| 65848-239138 | ||||||||||||||||||||||||
4786 | wherePathSolver(pWInfo, pWInfo->nRowOut+1); | - | ||||||||||||||||||||||||
4787 | if( db->mallocFailed ) goto whereBeginError; executed 10 times by 1 test: goto whereBeginError; Executed by:
| 10-65838 | ||||||||||||||||||||||||
4788 | } executed 65838 times by 435 tests: end of block Executed by:
| 65838 | ||||||||||||||||||||||||
4789 | } executed 304976 times by 435 tests: end of block Executed by:
| 304976 | ||||||||||||||||||||||||
4790 | if( pWInfo->pOrderBy==0 && (db->flags & SQLITE_ReverseOrder)!=0 ){
| 21-266221 | ||||||||||||||||||||||||
4791 | pWInfo->revMask = ALLBITS; | - | ||||||||||||||||||||||||
4792 | } executed 21 times by 1 test: end of block Executed by:
| 21 | ||||||||||||||||||||||||
4793 | if( pParse->nErr || NEVER(db->mallocFailed) ){
| 0-332008 | ||||||||||||||||||||||||
4794 | goto whereBeginError; executed 66 times by 1 test: goto whereBeginError; Executed by:
| 66 | ||||||||||||||||||||||||
4795 | } | - | ||||||||||||||||||||||||
4796 | #ifdef WHERETRACE_ENABLED | - | ||||||||||||||||||||||||
4797 | if( sqlite3WhereTrace ){ | - | ||||||||||||||||||||||||
4798 | sqlite3DebugPrintf("---- Solution nRow=%d", pWInfo->nRowOut); | - | ||||||||||||||||||||||||
4799 | if( pWInfo->nOBSat>0 ){ | - | ||||||||||||||||||||||||
4800 | sqlite3DebugPrintf(" ORDERBY=%d,0x%llx", pWInfo->nOBSat, pWInfo->revMask); | - | ||||||||||||||||||||||||
4801 | } | - | ||||||||||||||||||||||||
4802 | switch( pWInfo->eDistinct ){ | - | ||||||||||||||||||||||||
4803 | case WHERE_DISTINCT_UNIQUE: { | - | ||||||||||||||||||||||||
4804 | sqlite3DebugPrintf(" DISTINCT=unique"); | - | ||||||||||||||||||||||||
4805 | break; | - | ||||||||||||||||||||||||
4806 | } | - | ||||||||||||||||||||||||
4807 | case WHERE_DISTINCT_ORDERED: { | - | ||||||||||||||||||||||||
4808 | sqlite3DebugPrintf(" DISTINCT=ordered"); | - | ||||||||||||||||||||||||
4809 | break; | - | ||||||||||||||||||||||||
4810 | } | - | ||||||||||||||||||||||||
4811 | case WHERE_DISTINCT_UNORDERED: { | - | ||||||||||||||||||||||||
4812 | sqlite3DebugPrintf(" DISTINCT=unordered"); | - | ||||||||||||||||||||||||
4813 | break; | - | ||||||||||||||||||||||||
4814 | } | - | ||||||||||||||||||||||||
4815 | } | - | ||||||||||||||||||||||||
4816 | sqlite3DebugPrintf("\n"); | - | ||||||||||||||||||||||||
4817 | for(ii=0; ii<pWInfo->nLevel; ii++){ | - | ||||||||||||||||||||||||
4818 | whereLoopPrint(pWInfo->a[ii].pWLoop, sWLB.pWC); | - | ||||||||||||||||||||||||
4819 | } | - | ||||||||||||||||||||||||
4820 | } | - | ||||||||||||||||||||||||
4821 | #endif | - | ||||||||||||||||||||||||
4822 | - | |||||||||||||||||||||||||
4823 | /* Attempt to omit tables from the join that do not affect the result. | - | ||||||||||||||||||||||||
4824 | ** For a table to not affect the result, the following must be true: | - | ||||||||||||||||||||||||
4825 | ** | - | ||||||||||||||||||||||||
4826 | ** 1) The query must not be an aggregate. | - | ||||||||||||||||||||||||
4827 | ** 2) The table must be the RHS of a LEFT JOIN. | - | ||||||||||||||||||||||||
4828 | ** 3) Either the query must be DISTINCT, or else the ON or USING clause | - | ||||||||||||||||||||||||
4829 | ** must contain a constraint that limits the scan of the table to | - | ||||||||||||||||||||||||
4830 | ** at most a single row. | - | ||||||||||||||||||||||||
4831 | ** 4) The table must not be referenced by any part of the query apart | - | ||||||||||||||||||||||||
4832 | ** from its own USING or ON clause. | - | ||||||||||||||||||||||||
4833 | ** | - | ||||||||||||||||||||||||
4834 | ** For example, given: | - | ||||||||||||||||||||||||
4835 | ** | - | ||||||||||||||||||||||||
4836 | ** CREATE TABLE t1(ipk INTEGER PRIMARY KEY, v1); | - | ||||||||||||||||||||||||
4837 | ** CREATE TABLE t2(ipk INTEGER PRIMARY KEY, v2); | - | ||||||||||||||||||||||||
4838 | ** CREATE TABLE t3(ipk INTEGER PRIMARY KEY, v3); | - | ||||||||||||||||||||||||
4839 | ** | - | ||||||||||||||||||||||||
4840 | ** then table t2 can be omitted from the following: | - | ||||||||||||||||||||||||
4841 | ** | - | ||||||||||||||||||||||||
4842 | ** SELECT v1, v3 FROM t1 | - | ||||||||||||||||||||||||
4843 | ** LEFT JOIN t2 USING (t1.ipk=t2.ipk) | - | ||||||||||||||||||||||||
4844 | ** LEFT JOIN t3 USING (t1.ipk=t3.ipk) | - | ||||||||||||||||||||||||
4845 | ** | - | ||||||||||||||||||||||||
4846 | ** or from: | - | ||||||||||||||||||||||||
4847 | ** | - | ||||||||||||||||||||||||
4848 | ** SELECT DISTINCT v1, v3 FROM t1 | - | ||||||||||||||||||||||||
4849 | ** LEFT JOIN t2 | - | ||||||||||||||||||||||||
4850 | ** LEFT JOIN t3 USING (t1.ipk=t3.ipk) | - | ||||||||||||||||||||||||
4851 | */ | - | ||||||||||||||||||||||||
4852 | notReady = ~(Bitmask)0; | - | ||||||||||||||||||||||||
4853 | if( pWInfo->nLevel>=2
| 6546-325462 | ||||||||||||||||||||||||
4854 | && pResultSet!=0 /* guarantees condition (1) above */
| 117-6429 | ||||||||||||||||||||||||
4855 | && OptimizationEnabled(db, SQLITE_OmitNoopJoin)
| 5-6424 | ||||||||||||||||||||||||
4856 | ){ | - | ||||||||||||||||||||||||
4857 | int i; | - | ||||||||||||||||||||||||
4858 | Bitmask tabUsed = sqlite3WhereExprListUsage(pMaskSet, pResultSet); | - | ||||||||||||||||||||||||
4859 | if( sWLB.pOrderBy ){
| 2279-4145 | ||||||||||||||||||||||||
4860 | tabUsed |= sqlite3WhereExprListUsage(pMaskSet, sWLB.pOrderBy); | - | ||||||||||||||||||||||||
4861 | } executed 2279 times by 1 test: end of block Executed by:
| 2279 | ||||||||||||||||||||||||
4862 | for(i=pWInfo->nLevel-1; i>=1; i--){
| 6424-10964 | ||||||||||||||||||||||||
4863 | WhereTerm *pTerm, *pEnd; | - | ||||||||||||||||||||||||
4864 | struct SrcList_item *pItem; | - | ||||||||||||||||||||||||
4865 | pLoop = pWInfo->a[i].pWLoop; | - | ||||||||||||||||||||||||
4866 | pItem = &pWInfo->pTabList->a[pLoop->iTab]; | - | ||||||||||||||||||||||||
4867 | if( (pItem->fg.jointype & JT_LEFT)==0 ) continue; executed 10549 times by 1 test: continue; Executed by:
| 415-10549 | ||||||||||||||||||||||||
4868 | if( (wctrlFlags & WHERE_WANT_DISTINCT)==0
| 14-401 | ||||||||||||||||||||||||
4869 | && (pLoop->wsFlags & WHERE_ONEROW)==0
| 109-292 | ||||||||||||||||||||||||
4870 | ){ | - | ||||||||||||||||||||||||
4871 | continue; executed 292 times by 1 test: continue; Executed by:
| 292 | ||||||||||||||||||||||||
4872 | } | - | ||||||||||||||||||||||||
4873 | if( (tabUsed & pLoop->maskSelf)!=0 ) continue; executed 96 times by 1 test: continue; Executed by:
| 27-96 | ||||||||||||||||||||||||
4874 | pEnd = sWLB.pWC->a + sWLB.pWC->nTerm; | - | ||||||||||||||||||||||||
4875 | for(pTerm=sWLB.pWC->a; pTerm<pEnd; pTerm++){
| 21-70 | ||||||||||||||||||||||||
4876 | if( (pTerm->prereqAll & pLoop->maskSelf)!=0 ){
| 16-54 | ||||||||||||||||||||||||
4877 | if( !ExprHasProperty(pTerm->pExpr, EP_FromJoin)
| 2-52 | ||||||||||||||||||||||||
4878 | || pTerm->pExpr->iRightJoinTable!=pItem->iCursor
| 4-48 | ||||||||||||||||||||||||
4879 | ){ | - | ||||||||||||||||||||||||
4880 | break; executed 6 times by 1 test: break; Executed by:
| 6 | ||||||||||||||||||||||||
4881 | } | - | ||||||||||||||||||||||||
4882 | } executed 48 times by 1 test: end of block Executed by:
| 48 | ||||||||||||||||||||||||
4883 | } executed 64 times by 1 test: end of block Executed by:
| 64 | ||||||||||||||||||||||||
4884 | if( pTerm<pEnd ) continue; executed 6 times by 1 test: continue; Executed by:
| 6-21 | ||||||||||||||||||||||||
4885 | WHERETRACE(0xffff, ("-> drop loop %c not used\n", pLoop->cId)); | - | ||||||||||||||||||||||||
4886 | notReady &= ~pLoop->maskSelf; | - | ||||||||||||||||||||||||
4887 | for(pTerm=sWLB.pWC->a; pTerm<pEnd; pTerm++){
| 21-60 | ||||||||||||||||||||||||
4888 | if( (pTerm->prereqAll & pLoop->maskSelf)!=0 ){
| 16-44 | ||||||||||||||||||||||||
4889 | pTerm->wtFlags |= TERM_CODED; | - | ||||||||||||||||||||||||
4890 | } executed 44 times by 1 test: end of block Executed by:
| 44 | ||||||||||||||||||||||||
4891 | } executed 60 times by 1 test: end of block Executed by:
| 60 | ||||||||||||||||||||||||
4892 | if( i!=pWInfo->nLevel-1 ){
| 8-13 | ||||||||||||||||||||||||
4893 | int nByte = (pWInfo->nLevel-1-i) * sizeof(WhereLevel); | - | ||||||||||||||||||||||||
4894 | memmove(&pWInfo->a[i], &pWInfo->a[i+1], nByte); | - | ||||||||||||||||||||||||
4895 | } executed 8 times by 1 test: end of block Executed by:
| 8 | ||||||||||||||||||||||||
4896 | pWInfo->nLevel--; | - | ||||||||||||||||||||||||
4897 | nTabList--; | - | ||||||||||||||||||||||||
4898 | } executed 21 times by 1 test: end of block Executed by:
| 21 | ||||||||||||||||||||||||
4899 | } executed 6424 times by 1 test: end of block Executed by:
| 6424 | ||||||||||||||||||||||||
4900 | WHERETRACE(0xffff,("*** Optimizer Finished ***\n")); | - | ||||||||||||||||||||||||
4901 | pWInfo->pParse->nQueryLoop += pWInfo->nRowOut; | - | ||||||||||||||||||||||||
4902 | - | |||||||||||||||||||||||||
4903 | /* If the caller is an UPDATE or DELETE statement that is requesting | - | ||||||||||||||||||||||||
4904 | ** to use a one-pass algorithm, determine if this is appropriate. | - | ||||||||||||||||||||||||
4905 | ** | - | ||||||||||||||||||||||||
4906 | ** A one-pass approach can be used if the caller has requested one | - | ||||||||||||||||||||||||
4907 | ** and either (a) the scan visits at most one row or (b) each | - | ||||||||||||||||||||||||
4908 | ** of the following are true: | - | ||||||||||||||||||||||||
4909 | ** | - | ||||||||||||||||||||||||
4910 | ** * the caller has indicated that a one-pass approach can be used | - | ||||||||||||||||||||||||
4911 | ** with multiple rows (by setting WHERE_ONEPASS_MULTIROW), and | - | ||||||||||||||||||||||||
4912 | ** * the table is not a virtual table, and | - | ||||||||||||||||||||||||
4913 | ** * either the scan does not use the OR optimization or the caller | - | ||||||||||||||||||||||||
4914 | ** is a DELETE operation (WHERE_DUPLICATES_OK is only specified | - | ||||||||||||||||||||||||
4915 | ** for DELETE). | - | ||||||||||||||||||||||||
4916 | ** | - | ||||||||||||||||||||||||
4917 | ** The last qualification is because an UPDATE statement uses | - | ||||||||||||||||||||||||
4918 | ** WhereInfo.aiCurOnePass[1] to determine whether or not it really can | - | ||||||||||||||||||||||||
4919 | ** use a one-pass approach, and this is not set accurately for scans | - | ||||||||||||||||||||||||
4920 | ** that use the OR optimization. | - | ||||||||||||||||||||||||
4921 | */ | - | ||||||||||||||||||||||||
4922 | assert( (wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || pWInfo->nLevel==1 ); | - | ||||||||||||||||||||||||
4923 | if( (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0 ){
| 40356-291652 | ||||||||||||||||||||||||
4924 | int wsFlags = pWInfo->a[0].pWLoop->wsFlags; | - | ||||||||||||||||||||||||
4925 | int bOnerow = (wsFlags & WHERE_ONEROW)!=0; | - | ||||||||||||||||||||||||
4926 | if( bOnerow || (
| 15879-24477 | ||||||||||||||||||||||||
4927 | 0!=(wctrlFlags & WHERE_ONEPASS_MULTIROW)
| 7823-8056 | ||||||||||||||||||||||||
4928 | && 0==(wsFlags & WHERE_VIRTUALTABLE)
| 21-7802 | ||||||||||||||||||||||||
4929 | && (0==(wsFlags & WHERE_MULTI_OR) || (wctrlFlags & WHERE_DUPLICATES_OK))
| 1-7787 | ||||||||||||||||||||||||
4930 | )){ | - | ||||||||||||||||||||||||
4931 | pWInfo->eOnePass = bOnerow ? ONEPASS_SINGLE : ONEPASS_MULTI;
| 7801-24477 | ||||||||||||||||||||||||
4932 | if( HasRowid(pTabList->a[0].pTab) && (wsFlags & WHERE_IDX_ONLY) ){
| 192-32086 | ||||||||||||||||||||||||
4933 | if( wctrlFlags & WHERE_ONEPASS_MULTIROW ){
| 96-229 | ||||||||||||||||||||||||
4934 | bFordelete = OPFLAG_FORDELETE; | - | ||||||||||||||||||||||||
4935 | } executed 229 times by 1 test: end of block Executed by:
| 229 | ||||||||||||||||||||||||
4936 | pWInfo->a[0].pWLoop->wsFlags = (wsFlags & ~WHERE_IDX_ONLY); | - | ||||||||||||||||||||||||
4937 | } executed 325 times by 1 test: end of block Executed by:
| 325 | ||||||||||||||||||||||||
4938 | } executed 32278 times by 378 tests: end of block Executed by:
| 32278 | ||||||||||||||||||||||||
4939 | } executed 40356 times by 378 tests: end of block Executed by:
| 40356 | ||||||||||||||||||||||||
4940 | - | |||||||||||||||||||||||||
4941 | /* Open all tables in the pTabList and any indices selected for | - | ||||||||||||||||||||||||
4942 | ** searching those tables. | - | ||||||||||||||||||||||||
4943 | */ | - | ||||||||||||||||||||||||
4944 | for(ii=0, pLevel=pWInfo->a; ii<nTabList; ii++, pLevel++){
| 278504-332008 | ||||||||||||||||||||||||
4945 | Table *pTab; /* Table to open */ | - | ||||||||||||||||||||||||
4946 | int iDb; /* Index of database containing table/index */ | - | ||||||||||||||||||||||||
4947 | struct SrcList_item *pTabItem; | - | ||||||||||||||||||||||||
4948 | - | |||||||||||||||||||||||||
4949 | pTabItem = &pTabList->a[pLevel->iFrom]; | - | ||||||||||||||||||||||||
4950 | pTab = pTabItem->pTab; | - | ||||||||||||||||||||||||
4951 | iDb = sqlite3SchemaToIndex(db, pTab->pSchema); | - | ||||||||||||||||||||||||
4952 | pLoop = pLevel->pWLoop; | - | ||||||||||||||||||||||||
4953 | if( (pTab->tabFlags & TF_Ephemeral)!=0 || pTab->pSelect ){
| 2946-275558 | ||||||||||||||||||||||||
4954 | /* Do nothing */ | - | ||||||||||||||||||||||||
4955 | }else executed 36511 times by 1 test: end of block Executed by:
| 36511 | ||||||||||||||||||||||||
4956 | #ifndef SQLITE_OMIT_VIRTUALTABLE | - | ||||||||||||||||||||||||
4957 | if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){
| 11058-230935 | ||||||||||||||||||||||||
4958 | const char *pVTab = (const char *)sqlite3GetVTable(db, pTab); | - | ||||||||||||||||||||||||
4959 | int iCur = pTabItem->iCursor; | - | ||||||||||||||||||||||||
4960 | sqlite3VdbeAddOp4(v, OP_VOpen, iCur, 0, 0, pVTab, P4_VTAB); | - | ||||||||||||||||||||||||
4961 | }else if( IsVirtual(pTab) ){ executed 11058 times by 1 test: end of block Executed by:
| 14-230921 | ||||||||||||||||||||||||
4962 | /* noop */ | - | ||||||||||||||||||||||||
4963 | }else executed 14 times by 1 test: end of block Executed by:
| 14 | ||||||||||||||||||||||||
4964 | #endif | - | ||||||||||||||||||||||||
4965 | if( (pLoop->wsFlags & WHERE_IDX_ONLY)==0
| 8167-222754 | ||||||||||||||||||||||||
4966 | && (wctrlFlags & WHERE_OR_SUBCLAUSE)==0 ){
| 16562-206192 | ||||||||||||||||||||||||
4967 | int op = OP_OpenRead; | - | ||||||||||||||||||||||||
4968 | if( pWInfo->eOnePass!=ONEPASS_OFF ){
| 32078-174114 | ||||||||||||||||||||||||
4969 | op = OP_OpenWrite; | - | ||||||||||||||||||||||||
4970 | pWInfo->aiCurOnePass[0] = pTabItem->iCursor; | - | ||||||||||||||||||||||||
4971 | }; executed 32078 times by 378 tests: end of block Executed by:
| 32078 | ||||||||||||||||||||||||
4972 | sqlite3OpenTable(pParse, pTabItem->iCursor, iDb, pTab, op); | - | ||||||||||||||||||||||||
4973 | assert( pTabItem->iCursor==pLevel->iTabCur ); | - | ||||||||||||||||||||||||
4974 | testcase( pWInfo->eOnePass==ONEPASS_OFF && pTab->nCol==BMS-1 ); | - | ||||||||||||||||||||||||
4975 | testcase( pWInfo->eOnePass==ONEPASS_OFF && pTab->nCol==BMS ); | - | ||||||||||||||||||||||||
4976 | if( pWInfo->eOnePass==ONEPASS_OFF && pTab->nCol<BMS && HasRowid(pTab) ){
| 30-174114 | ||||||||||||||||||||||||
4977 | Bitmask b = pTabItem->colUsed; | - | ||||||||||||||||||||||||
4978 | int n = 0; | - | ||||||||||||||||||||||||
4979 | for(; b; b=b>>1, n++){} executed 515437 times by 435 tests: end of block Executed by:
| 173998-515437 | ||||||||||||||||||||||||
4980 | sqlite3VdbeChangeP4(v, -1, SQLITE_INT_TO_PTR(n), P4_INT32); | - | ||||||||||||||||||||||||
4981 | assert( n<=pTab->nCol ); | - | ||||||||||||||||||||||||
4982 | } executed 173998 times by 435 tests: end of block Executed by:
| 173998 | ||||||||||||||||||||||||
4983 | #ifdef SQLITE_ENABLE_CURSOR_HINTS | - | ||||||||||||||||||||||||
4984 | if( pLoop->u.btree.pIndex!=0 ){ | - | ||||||||||||||||||||||||
4985 | sqlite3VdbeChangeP5(v, OPFLAG_SEEKEQ|bFordelete); | - | ||||||||||||||||||||||||
4986 | }else | - | ||||||||||||||||||||||||
4987 | #endif | - | ||||||||||||||||||||||||
4988 | { | - | ||||||||||||||||||||||||
4989 | sqlite3VdbeChangeP5(v, bFordelete); | - | ||||||||||||||||||||||||
4990 | } | - | ||||||||||||||||||||||||
4991 | #ifdef SQLITE_ENABLE_COLUMN_USED_MASK | - | ||||||||||||||||||||||||
4992 | sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed, pTabItem->iCursor, 0, 0, | - | ||||||||||||||||||||||||
4993 | (const u8*)&pTabItem->colUsed, P4_INT64); | - | ||||||||||||||||||||||||
4994 | #endif | - | ||||||||||||||||||||||||
4995 | }else{ executed 206192 times by 435 tests: end of block Executed by:
| 206192 | ||||||||||||||||||||||||
4996 | sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName); | - | ||||||||||||||||||||||||
4997 | } executed 24729 times by 1 test: end of block Executed by:
| 24729 | ||||||||||||||||||||||||
4998 | if( pLoop->wsFlags & WHERE_INDEXED ){
| 28422-250082 | ||||||||||||||||||||||||
4999 | Index *pIx = pLoop->u.btree.pIndex; | - | ||||||||||||||||||||||||
5000 | int iIndexCur; | - | ||||||||||||||||||||||||
5001 | int op = OP_OpenRead; | - | ||||||||||||||||||||||||
5002 | /* iAuxArg is always set to a positive value if ONEPASS is possible */ | - | ||||||||||||||||||||||||
5003 | assert( iAuxArg!=0 || (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 ); | - | ||||||||||||||||||||||||
5004 | if( !HasRowid(pTab) && IsPrimaryKeyIndex(pIx)
| 148-27432 | ||||||||||||||||||||||||
5005 | && (wctrlFlags & WHERE_OR_SUBCLAUSE)!=0
| 7-835 | ||||||||||||||||||||||||
5006 | ){ | - | ||||||||||||||||||||||||
5007 | /* This is one term of an OR-optimization using the PRIMARY KEY of a | - | ||||||||||||||||||||||||
5008 | ** WITHOUT ROWID table. No need for a separate index */ | - | ||||||||||||||||||||||||
5009 | iIndexCur = pLevel->iTabCur; | - | ||||||||||||||||||||||||
5010 | op = 0; | - | ||||||||||||||||||||||||
5011 | }else if( pWInfo->eOnePass!=ONEPASS_OFF ){ executed 7 times by 1 test: end of block Executed by:
| 7-27880 | ||||||||||||||||||||||||
5012 | Index *pJ = pTabItem->pTab->pIndex; | - | ||||||||||||||||||||||||
5013 | iIndexCur = iAuxArg; | - | ||||||||||||||||||||||||
5014 | assert( wctrlFlags & WHERE_ONEPASS_DESIRED ); | - | ||||||||||||||||||||||||
5015 | while( ALWAYS(pJ) && pJ!=pIx ){
| 0-603 | ||||||||||||||||||||||||
5016 | iIndexCur++; | - | ||||||||||||||||||||||||
5017 | pJ = pJ->pNext; | - | ||||||||||||||||||||||||
5018 | } executed 68 times by 1 test: end of block Executed by:
| 68 | ||||||||||||||||||||||||
5019 | op = OP_OpenWrite; | - | ||||||||||||||||||||||||
5020 | pWInfo->aiCurOnePass[1] = iIndexCur; | - | ||||||||||||||||||||||||
5021 | }else if( iAuxArg && (wctrlFlags & WHERE_OR_SUBCLAUSE)!=0 ){ executed 535 times by 1 test: end of block Executed by:
| 535-27258 | ||||||||||||||||||||||||
5022 | iIndexCur = iAuxArg; | - | ||||||||||||||||||||||||
5023 | op = OP_ReopenIdx; | - | ||||||||||||||||||||||||
5024 | }else{ executed 13313 times by 1 test: end of block Executed by:
| 13313 | ||||||||||||||||||||||||
5025 | iIndexCur = pParse->nTab++; | - | ||||||||||||||||||||||||
5026 | } executed 14567 times by 2 tests: end of block Executed by:
| 14567 | ||||||||||||||||||||||||
5027 | pLevel->iIdxCur = iIndexCur; | - | ||||||||||||||||||||||||
5028 | assert( pIx->pSchema==pTab->pSchema ); | - | ||||||||||||||||||||||||
5029 | assert( iIndexCur>=0 ); | - | ||||||||||||||||||||||||
5030 | if( op ){
| 7-28415 | ||||||||||||||||||||||||
5031 | sqlite3VdbeAddOp3(v, op, iIndexCur, pIx->tnum, iDb); | - | ||||||||||||||||||||||||
5032 | sqlite3VdbeSetP4KeyInfo(pParse, pIx); | - | ||||||||||||||||||||||||
5033 | if( (pLoop->wsFlags & WHERE_CONSTRAINT)!=0
| 4766-23649 | ||||||||||||||||||||||||
5034 | && (pLoop->wsFlags & (WHERE_COLUMN_RANGE|WHERE_SKIPSCAN))==0
| 7840-15809 | ||||||||||||||||||||||||
5035 | && (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)==0
| 20-15789 | ||||||||||||||||||||||||
5036 | && pWInfo->eDistinct!=WHERE_DISTINCT_ORDERED
| 11-15778 | ||||||||||||||||||||||||
5037 | ){ | - | ||||||||||||||||||||||||
5038 | sqlite3VdbeChangeP5(v, OPFLAG_SEEKEQ); /* Hint to COMDB2 */ | - | ||||||||||||||||||||||||
5039 | } executed 15778 times by 1 test: end of block Executed by:
| 15778 | ||||||||||||||||||||||||
5040 | VdbeComment((v, "%s", pIx->zName)); | - | ||||||||||||||||||||||||
5041 | #ifdef SQLITE_ENABLE_COLUMN_USED_MASK | - | ||||||||||||||||||||||||
5042 | { | - | ||||||||||||||||||||||||
5043 | u64 colUsed = 0; | - | ||||||||||||||||||||||||
5044 | int ii, jj; | - | ||||||||||||||||||||||||
5045 | for(ii=0; ii<pIx->nColumn; ii++){ | - | ||||||||||||||||||||||||
5046 | jj = pIx->aiColumn[ii]; | - | ||||||||||||||||||||||||
5047 | if( jj<0 ) continue; | - | ||||||||||||||||||||||||
5048 | if( jj>63 ) jj = 63; | - | ||||||||||||||||||||||||
5049 | if( (pTabItem->colUsed & MASKBIT(jj))==0 ) continue; | - | ||||||||||||||||||||||||
5050 | colUsed |= ((u64)1)<<(ii<63 ? ii : 63); | - | ||||||||||||||||||||||||
5051 | } | - | ||||||||||||||||||||||||
5052 | sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed, iIndexCur, 0, 0, | - | ||||||||||||||||||||||||
5053 | (u8*)&colUsed, P4_INT64); | - | ||||||||||||||||||||||||
5054 | } | - | ||||||||||||||||||||||||
5055 | #endif /* SQLITE_ENABLE_COLUMN_USED_MASK */ | - | ||||||||||||||||||||||||
5056 | } executed 28415 times by 2 tests: end of block Executed by:
| 28415 | ||||||||||||||||||||||||
5057 | } executed 28422 times by 2 tests: end of block Executed by:
| 28422 | ||||||||||||||||||||||||
5058 | if( iDb>=0 ) sqlite3CodeVerifySchema(pParse, iDb); executed 275558 times by 435 tests: sqlite3CodeVerifySchema(pParse, iDb); Executed by:
| 2946-275558 | ||||||||||||||||||||||||
5059 | } executed 278504 times by 435 tests: end of block Executed by:
| 278504 | ||||||||||||||||||||||||
5060 | pWInfo->iTop = sqlite3VdbeCurrentAddr(v); | - | ||||||||||||||||||||||||
5061 | if( db->mallocFailed ) goto whereBeginError; executed 4 times by 1 test: goto whereBeginError; Executed by:
| 4-332004 | ||||||||||||||||||||||||
5062 | - | |||||||||||||||||||||||||
5063 | /* Generate the code to do the search. Each iteration of the for | - | ||||||||||||||||||||||||
5064 | ** loop below generates code for a single nested loop of the VM | - | ||||||||||||||||||||||||
5065 | ** program. | - | ||||||||||||||||||||||||
5066 | */ | - | ||||||||||||||||||||||||
5067 | for(ii=0; ii<nTabList; ii++){
| 278496-332004 | ||||||||||||||||||||||||
5068 | int addrExplain; | - | ||||||||||||||||||||||||
5069 | int wsFlags; | - | ||||||||||||||||||||||||
5070 | pLevel = &pWInfo->a[ii]; | - | ||||||||||||||||||||||||
5071 | wsFlags = pLevel->pWLoop->wsFlags; | - | ||||||||||||||||||||||||
5072 | #ifndef SQLITE_OMIT_AUTOMATIC_INDEX | - | ||||||||||||||||||||||||
5073 | if( (pLevel->pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 ){
| 2859-275637 | ||||||||||||||||||||||||
5074 | constructAutomaticIndex(pParse, &pWInfo->sWC, | - | ||||||||||||||||||||||||
5075 | &pTabList->a[pLevel->iFrom], notReady, pLevel); | - | ||||||||||||||||||||||||
5076 | if( db->mallocFailed ) goto whereBeginError; never executed: goto whereBeginError;
| 0-2859 | ||||||||||||||||||||||||
5077 | } executed 2859 times by 1 test: end of block Executed by:
| 2859 | ||||||||||||||||||||||||
5078 | #endif | - | ||||||||||||||||||||||||
5079 | addrExplain = sqlite3WhereExplainOneScan( | - | ||||||||||||||||||||||||
5080 | pParse, pTabList, pLevel, wctrlFlags | - | ||||||||||||||||||||||||
5081 | ); | - | ||||||||||||||||||||||||
5082 | pLevel->addrBody = sqlite3VdbeCurrentAddr(v); | - | ||||||||||||||||||||||||
5083 | notReady = sqlite3WhereCodeOneLoopStart(pWInfo, ii, notReady); | - | ||||||||||||||||||||||||
5084 | pWInfo->iContinue = pLevel->addrCont; | - | ||||||||||||||||||||||||
5085 | if( (wsFlags&WHERE_MULTI_OR)==0 && (wctrlFlags&WHERE_OR_SUBCLAUSE)==0 ){
| 2366-276130 | ||||||||||||||||||||||||
5086 | sqlite3WhereAddScanStatus(v, pTabList, pLevel, addrExplain); | - | ||||||||||||||||||||||||
5087 | } executed 258344 times by 435 tests: end of block Executed by:
| 258344 | ||||||||||||||||||||||||
5088 | } executed 278496 times by 435 tests: end of block Executed by:
| 278496 | ||||||||||||||||||||||||
5089 | - | |||||||||||||||||||||||||
5090 | /* Done. */ | - | ||||||||||||||||||||||||
5091 | VdbeModuleComment((v, "Begin WHERE-core")); | - | ||||||||||||||||||||||||
5092 | return pWInfo; executed 332004 times by 435 tests: return pWInfo; Executed by:
| 332004 | ||||||||||||||||||||||||
5093 | - | |||||||||||||||||||||||||
5094 | /* Jump here if malloc fails */ | - | ||||||||||||||||||||||||
5095 | whereBeginError: | - | ||||||||||||||||||||||||
5096 | if( pWInfo ){
| 432-511 | ||||||||||||||||||||||||
5097 | pParse->nQueryLoop = pWInfo->savedNQueryLoop; | - | ||||||||||||||||||||||||
5098 | whereInfoFree(db, pWInfo); | - | ||||||||||||||||||||||||
5099 | } executed 511 times by 1 test: end of block Executed by:
| 511 | ||||||||||||||||||||||||
5100 | return 0; executed 943 times by 1 test: return 0; Executed by:
| 943 | ||||||||||||||||||||||||
5101 | } | - | ||||||||||||||||||||||||
5102 | - | |||||||||||||||||||||||||
5103 | /* | - | ||||||||||||||||||||||||
5104 | ** Part of sqlite3WhereEnd() will rewrite opcodes to reference the | - | ||||||||||||||||||||||||
5105 | ** index rather than the main table. In SQLITE_DEBUG mode, we want | - | ||||||||||||||||||||||||
5106 | ** to trace those changes if PRAGMA vdbe_addoptrace=on. This routine | - | ||||||||||||||||||||||||
5107 | ** does that. | - | ||||||||||||||||||||||||
5108 | */ | - | ||||||||||||||||||||||||
5109 | #ifndef SQLITE_DEBUG | - | ||||||||||||||||||||||||
5110 | # define OpcodeRewriteTrace(D,K,P) /* no-op */ | - | ||||||||||||||||||||||||
5111 | #else | - | ||||||||||||||||||||||||
5112 | # define OpcodeRewriteTrace(D,K,P) sqlite3WhereOpcodeRewriteTrace(D,K,P) | - | ||||||||||||||||||||||||
5113 | static void sqlite3WhereOpcodeRewriteTrace( | - | ||||||||||||||||||||||||
5114 | sqlite3 *db, | - | ||||||||||||||||||||||||
5115 | int pc, | - | ||||||||||||||||||||||||
5116 | VdbeOp *pOp | - | ||||||||||||||||||||||||
5117 | ){ | - | ||||||||||||||||||||||||
5118 | if( (db->flags & SQLITE_VdbeAddopTrace)==0 ) return; | - | ||||||||||||||||||||||||
5119 | sqlite3VdbePrintOp(0, pc, pOp); | - | ||||||||||||||||||||||||
5120 | } | - | ||||||||||||||||||||||||
5121 | #endif | - | ||||||||||||||||||||||||
5122 | - | |||||||||||||||||||||||||
5123 | /* | - | ||||||||||||||||||||||||
5124 | ** Generate the end of the WHERE loop. See comments on | - | ||||||||||||||||||||||||
5125 | ** sqlite3WhereBegin() for additional information. | - | ||||||||||||||||||||||||
5126 | */ | - | ||||||||||||||||||||||||
5127 | void sqlite3WhereEnd(WhereInfo *pWInfo){ | - | ||||||||||||||||||||||||
5128 | Parse *pParse = pWInfo->pParse; | - | ||||||||||||||||||||||||
5129 | Vdbe *v = pParse->pVdbe; | - | ||||||||||||||||||||||||
5130 | int i; | - | ||||||||||||||||||||||||
5131 | WhereLevel *pLevel; | - | ||||||||||||||||||||||||
5132 | WhereLoop *pLoop; | - | ||||||||||||||||||||||||
5133 | SrcList *pTabList = pWInfo->pTabList; | - | ||||||||||||||||||||||||
5134 | sqlite3 *db = pParse->db; | - | ||||||||||||||||||||||||
5135 | - | |||||||||||||||||||||||||
5136 | /* Generate loop termination code. | - | ||||||||||||||||||||||||
5137 | */ | - | ||||||||||||||||||||||||
5138 | VdbeModuleComment((v, "End WHERE-core")); | - | ||||||||||||||||||||||||
5139 | for(i=pWInfo->nLevel-1; i>=0; i--){
| 278496-332004 | ||||||||||||||||||||||||
5140 | int addr; | - | ||||||||||||||||||||||||
5141 | pLevel = &pWInfo->a[i]; | - | ||||||||||||||||||||||||
5142 | pLoop = pLevel->pWLoop; | - | ||||||||||||||||||||||||
5143 | if( pLevel->op!=OP_Noop ){
| 31391-247105 | ||||||||||||||||||||||||
5144 | #ifndef SQLITE_DISABLE_SKIPAHEAD_DISTINCT | - | ||||||||||||||||||||||||
5145 | int addrSeek = 0; | - | ||||||||||||||||||||||||
5146 | Index *pIdx; | - | ||||||||||||||||||||||||
5147 | int n; | - | ||||||||||||||||||||||||
5148 | if( pWInfo->eDistinct==WHERE_DISTINCT_ORDERED
| 71-247034 | ||||||||||||||||||||||||
5149 | && i==pWInfo->nLevel-1 /* Ticket [ef9318757b152e3] 2017-10-21 */
| 18-53 | ||||||||||||||||||||||||
5150 | && (pLoop->wsFlags & WHERE_INDEXED)!=0
| 5-48 | ||||||||||||||||||||||||
5151 | && (pIdx = pLoop->u.btree.pIndex)->hasStat1
| 5-43 | ||||||||||||||||||||||||
5152 | && (n = pLoop->u.btree.nIdxCol)>0
| 2-3 | ||||||||||||||||||||||||
5153 | && pIdx->aiRowLogEst[n]>=36
| 0-3 | ||||||||||||||||||||||||
5154 | ){ | - | ||||||||||||||||||||||||
5155 | int r1 = pParse->nMem+1; | - | ||||||||||||||||||||||||
5156 | int j, op; | - | ||||||||||||||||||||||||
5157 | for(j=0; j<n; j++){
| 3-5 | ||||||||||||||||||||||||
5158 | sqlite3VdbeAddOp3(v, OP_Column, pLevel->iIdxCur, j, r1+j); | - | ||||||||||||||||||||||||
5159 | } executed 5 times by 1 test: end of block Executed by:
| 5 | ||||||||||||||||||||||||
5160 | pParse->nMem += n+1; | - | ||||||||||||||||||||||||
5161 | op = pLevel->op==OP_Prev ? OP_SeekLT : OP_SeekGT;
| 0-3 | ||||||||||||||||||||||||
5162 | addrSeek = sqlite3VdbeAddOp4Int(v, op, pLevel->iIdxCur, 0, r1, n); | - | ||||||||||||||||||||||||
5163 | VdbeCoverageIf(v, op==OP_SeekLT); | - | ||||||||||||||||||||||||
5164 | VdbeCoverageIf(v, op==OP_SeekGT); | - | ||||||||||||||||||||||||
5165 | sqlite3VdbeAddOp2(v, OP_Goto, 1, pLevel->p2); | - | ||||||||||||||||||||||||
5166 | } executed 3 times by 1 test: end of block Executed by:
| 3 | ||||||||||||||||||||||||
5167 | #endif /* SQLITE_DISABLE_SKIPAHEAD_DISTINCT */ | - | ||||||||||||||||||||||||
5168 | /* The common case: Advance to the next row */ | - | ||||||||||||||||||||||||
5169 | sqlite3VdbeResolveLabel(v, pLevel->addrCont); | - | ||||||||||||||||||||||||
5170 | sqlite3VdbeAddOp3(v, pLevel->op, pLevel->p1, pLevel->p2, pLevel->p3); | - | ||||||||||||||||||||||||
5171 | sqlite3VdbeChangeP5(v, pLevel->p5); | - | ||||||||||||||||||||||||
5172 | VdbeCoverage(v); | - | ||||||||||||||||||||||||
5173 | VdbeCoverageIf(v, pLevel->op==OP_Next); | - | ||||||||||||||||||||||||
5174 | VdbeCoverageIf(v, pLevel->op==OP_Prev); | - | ||||||||||||||||||||||||
5175 | VdbeCoverageIf(v, pLevel->op==OP_VNext); | - | ||||||||||||||||||||||||
5176 | #ifndef SQLITE_DISABLE_SKIPAHEAD_DISTINCT | - | ||||||||||||||||||||||||
5177 | if( addrSeek ) sqlite3VdbeJumpHere(v, addrSeek); executed 3 times by 1 test: sqlite3VdbeJumpHere(v, addrSeek); Executed by:
| 3-247102 | ||||||||||||||||||||||||
5178 | #endif | - | ||||||||||||||||||||||||
5179 | }else{ executed 247105 times by 435 tests: end of block Executed by:
| 247105 | ||||||||||||||||||||||||
5180 | sqlite3VdbeResolveLabel(v, pLevel->addrCont); | - | ||||||||||||||||||||||||
5181 | } executed 31391 times by 30 tests: end of block Executed by:
| 31391 | ||||||||||||||||||||||||
5182 | if( pLoop->wsFlags & WHERE_IN_ABLE && pLevel->u.in.nIn>0 ){
| 0-277817 | ||||||||||||||||||||||||
5183 | struct InLoop *pIn; | - | ||||||||||||||||||||||||
5184 | int j; | - | ||||||||||||||||||||||||
5185 | sqlite3VdbeResolveLabel(v, pLevel->addrNxt); | - | ||||||||||||||||||||||||
5186 | for(j=pLevel->u.in.nIn, pIn=&pLevel->u.in.aInLoop[j-1]; j>0; j--, pIn--){
| 679-813 | ||||||||||||||||||||||||
5187 | sqlite3VdbeJumpHere(v, pIn->addrInTop+1); | - | ||||||||||||||||||||||||
5188 | if( pIn->eEndLoopOp!=OP_Noop ){
| 38-775 | ||||||||||||||||||||||||
5189 | if( pIn->nPrefix ){
| 255-520 | ||||||||||||||||||||||||
5190 | assert( pLoop->wsFlags & WHERE_IN_EARLYOUT ); | - | ||||||||||||||||||||||||
5191 | sqlite3VdbeAddOp4Int(v, OP_IfNoHope, pLevel->iIdxCur, | - | ||||||||||||||||||||||||
5192 | sqlite3VdbeCurrentAddr(v)+2, | - | ||||||||||||||||||||||||
5193 | pIn->iBase, pIn->nPrefix); | - | ||||||||||||||||||||||||
5194 | VdbeCoverage(v); | - | ||||||||||||||||||||||||
5195 | } executed 255 times by 1 test: end of block Executed by:
| 255 | ||||||||||||||||||||||||
5196 | sqlite3VdbeAddOp2(v, pIn->eEndLoopOp, pIn->iCur, pIn->addrInTop); | - | ||||||||||||||||||||||||
5197 | VdbeCoverage(v); | - | ||||||||||||||||||||||||
5198 | VdbeCoverageIf(v, pIn->eEndLoopOp==OP_Prev); | - | ||||||||||||||||||||||||
5199 | VdbeCoverageIf(v, pIn->eEndLoopOp==OP_Next); | - | ||||||||||||||||||||||||
5200 | } executed 775 times by 1 test: end of block Executed by:
| 775 | ||||||||||||||||||||||||
5201 | sqlite3VdbeJumpHere(v, pIn->addrInTop-1); | - | ||||||||||||||||||||||||
5202 | } executed 813 times by 1 test: end of block Executed by:
| 813 | ||||||||||||||||||||||||
5203 | } executed 679 times by 1 test: end of block Executed by:
| 679 | ||||||||||||||||||||||||
5204 | sqlite3VdbeResolveLabel(v, pLevel->addrBrk); | - | ||||||||||||||||||||||||
5205 | if( pLevel->addrSkip ){
| 44-278452 | ||||||||||||||||||||||||
5206 | sqlite3VdbeGoto(v, pLevel->addrSkip); | - | ||||||||||||||||||||||||
5207 | VdbeComment((v, "next skip-scan on %s", pLoop->u.btree.pIndex->zName)); | - | ||||||||||||||||||||||||
5208 | sqlite3VdbeJumpHere(v, pLevel->addrSkip); | - | ||||||||||||||||||||||||
5209 | sqlite3VdbeJumpHere(v, pLevel->addrSkip-2); | - | ||||||||||||||||||||||||
5210 | } executed 44 times by 1 test: end of block Executed by:
| 44 | ||||||||||||||||||||||||
5211 | #ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS | - | ||||||||||||||||||||||||
5212 | if( pLevel->addrLikeRep ){
| 1726-276770 | ||||||||||||||||||||||||
5213 | sqlite3VdbeAddOp2(v, OP_DecrJumpZero, (int)(pLevel->iLikeRepCntr>>1), | - | ||||||||||||||||||||||||
5214 | pLevel->addrLikeRep); | - | ||||||||||||||||||||||||
5215 | VdbeCoverage(v); | - | ||||||||||||||||||||||||
5216 | } executed 1726 times by 1 test: end of block Executed by:
| 1726 | ||||||||||||||||||||||||
5217 | #endif | - | ||||||||||||||||||||||||
5218 | if( pLevel->iLeftJoin ){
| 425-278071 | ||||||||||||||||||||||||
5219 | int ws = pLoop->wsFlags; | - | ||||||||||||||||||||||||
5220 | addr = sqlite3VdbeAddOp1(v, OP_IfPos, pLevel->iLeftJoin); VdbeCoverage(v); | - | ||||||||||||||||||||||||
5221 | assert( (ws & WHERE_IDX_ONLY)==0 || (ws & WHERE_INDEXED)!=0 ); | - | ||||||||||||||||||||||||
5222 | if( (ws & WHERE_IDX_ONLY)==0 ){
| 199-226 | ||||||||||||||||||||||||
5223 | assert( pLevel->iTabCur==pTabList->a[pLevel->iFrom].iCursor ); | - | ||||||||||||||||||||||||
5224 | sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iTabCur); | - | ||||||||||||||||||||||||
5225 | } executed 226 times by 1 test: end of block Executed by:
| 226 | ||||||||||||||||||||||||
5226 | if( (ws & WHERE_INDEXED)
| 177-248 | ||||||||||||||||||||||||
5227 | || ((ws & WHERE_MULTI_OR) && pLevel->u.pCovidx)
| 3-164 | ||||||||||||||||||||||||
5228 | ){ | - | ||||||||||||||||||||||||
5229 | sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iIdxCur); | - | ||||||||||||||||||||||||
5230 | } executed 251 times by 1 test: end of block Executed by:
| 251 | ||||||||||||||||||||||||
5231 | if( pLevel->op==OP_Return ){
| 13-412 | ||||||||||||||||||||||||
5232 | sqlite3VdbeAddOp2(v, OP_Gosub, pLevel->p1, pLevel->addrFirst); | - | ||||||||||||||||||||||||
5233 | }else{ executed 13 times by 1 test: end of block Executed by:
| 13 | ||||||||||||||||||||||||
5234 | sqlite3VdbeGoto(v, pLevel->addrFirst); | - | ||||||||||||||||||||||||
5235 | } executed 412 times by 1 test: end of block Executed by:
| 412 | ||||||||||||||||||||||||
5236 | sqlite3VdbeJumpHere(v, addr); | - | ||||||||||||||||||||||||
5237 | } executed 425 times by 1 test: end of block Executed by:
| 425 | ||||||||||||||||||||||||
5238 | VdbeModuleComment((v, "End WHERE-loop%d: %s", i, | - | ||||||||||||||||||||||||
5239 | pWInfo->pTabList->a[pLevel->iFrom].pTab->zName)); | - | ||||||||||||||||||||||||
5240 | } executed 278496 times by 435 tests: end of block Executed by:
| 278496 | ||||||||||||||||||||||||
5241 | - | |||||||||||||||||||||||||
5242 | /* The "break" point is here, just past the end of the outer loop. | - | ||||||||||||||||||||||||
5243 | ** Set it. | - | ||||||||||||||||||||||||
5244 | */ | - | ||||||||||||||||||||||||
5245 | sqlite3VdbeResolveLabel(v, pWInfo->iBreak); | - | ||||||||||||||||||||||||
5246 | - | |||||||||||||||||||||||||
5247 | assert( pWInfo->nLevel<=pTabList->nSrc ); | - | ||||||||||||||||||||||||
5248 | for(i=0, pLevel=pWInfo->a; i<pWInfo->nLevel; i++, pLevel++){
| 278496-332004 | ||||||||||||||||||||||||
5249 | int k, last; | - | ||||||||||||||||||||||||
5250 | VdbeOp *pOp; | - | ||||||||||||||||||||||||
5251 | Index *pIdx = 0; | - | ||||||||||||||||||||||||
5252 | struct SrcList_item *pTabItem = &pTabList->a[pLevel->iFrom]; | - | ||||||||||||||||||||||||
5253 | Table *pTab = pTabItem->pTab; | - | ||||||||||||||||||||||||
5254 | assert( pTab!=0 ); | - | ||||||||||||||||||||||||
5255 | pLoop = pLevel->pWLoop; | - | ||||||||||||||||||||||||
5256 | - | |||||||||||||||||||||||||
5257 | /* For a co-routine, change all OP_Column references to the table of | - | ||||||||||||||||||||||||
5258 | ** the co-routine into OP_Copy of result contained in a register. | - | ||||||||||||||||||||||||
5259 | ** OP_Rowid becomes OP_Null. | - | ||||||||||||||||||||||||
5260 | */ | - | ||||||||||||||||||||||||
5261 | if( pTabItem->fg.viaCoroutine ){
| 35364-243132 | ||||||||||||||||||||||||
5262 | testcase( pParse->db->mallocFailed ); | - | ||||||||||||||||||||||||
5263 | translateColumnToCopy(pParse, pLevel->addrBody, pLevel->iTabCur, | - | ||||||||||||||||||||||||
5264 | pTabItem->regResult, 0); | - | ||||||||||||||||||||||||
5265 | continue; executed 35364 times by 1 test: continue; Executed by:
| 35364 | ||||||||||||||||||||||||
5266 | } | - | ||||||||||||||||||||||||
5267 | - | |||||||||||||||||||||||||
5268 | /* If this scan uses an index, make VDBE code substitutions to read data | - | ||||||||||||||||||||||||
5269 | ** from the index instead of from the table where possible. In some cases | - | ||||||||||||||||||||||||
5270 | ** this optimization prevents the table from ever being read, which can | - | ||||||||||||||||||||||||
5271 | ** yield a significant performance boost. | - | ||||||||||||||||||||||||
5272 | ** | - | ||||||||||||||||||||||||
5273 | ** Calls to the code generator in between sqlite3WhereBegin and | - | ||||||||||||||||||||||||
5274 | ** sqlite3WhereEnd will have created code that references the table | - | ||||||||||||||||||||||||
5275 | ** directly. This loop scans all that code looking for opcodes | - | ||||||||||||||||||||||||
5276 | ** that reference the table and converts them into opcodes that | - | ||||||||||||||||||||||||
5277 | ** reference the index. | - | ||||||||||||||||||||||||
5278 | */ | - | ||||||||||||||||||||||||
5279 | if( pLoop->wsFlags & (WHERE_INDEXED|WHERE_IDX_ONLY) ){
| 31277-211855 | ||||||||||||||||||||||||
5280 | pIdx = pLoop->u.btree.pIndex; | - | ||||||||||||||||||||||||
5281 | }else if( pLoop->wsFlags & WHERE_MULTI_OR ){ executed 31277 times by 2 tests: end of block Executed by:
| 2366-209489 | ||||||||||||||||||||||||
5282 | pIdx = pLevel->u.pCovidx; | - | ||||||||||||||||||||||||
5283 | } executed 2366 times by 1 test: end of block Executed by:
| 2366 | ||||||||||||||||||||||||
5284 | if( pIdx
| 31479-211653 | ||||||||||||||||||||||||
5285 | && (pWInfo->eOnePass==ONEPASS_OFF || !HasRowid(pIdx->pTable))
| 192-30936 | ||||||||||||||||||||||||
5286 | && !db->mallocFailed
| 12-31116 | ||||||||||||||||||||||||
5287 | ){ | - | ||||||||||||||||||||||||
5288 | last = sqlite3VdbeCurrentAddr(v); | - | ||||||||||||||||||||||||
5289 | k = pLevel->addrBody; | - | ||||||||||||||||||||||||
5290 | #ifdef SQLITE_DEBUG | - | ||||||||||||||||||||||||
5291 | if( db->flags & SQLITE_VdbeAddopTrace ){ | - | ||||||||||||||||||||||||
5292 | printf("TRANSLATE opcodes in range %d..%d\n", k, last-1); | - | ||||||||||||||||||||||||
5293 | } | - | ||||||||||||||||||||||||
5294 | #endif | - | ||||||||||||||||||||||||
5295 | pOp = sqlite3VdbeGetOp(v, k); | - | ||||||||||||||||||||||||
5296 | for(; k<last; k++, pOp++){
| 31116-1092366 | ||||||||||||||||||||||||
5297 | if( pOp->p1!=pLevel->iTabCur ) continue; executed 1000336 times by 2 tests: continue; Executed by:
| 92030-1000336 | ||||||||||||||||||||||||
5298 | if( pOp->opcode==OP_Column
| 38676-53354 | ||||||||||||||||||||||||
5299 | #ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC | - | ||||||||||||||||||||||||
5300 | || pOp->opcode==OP_Offset | - | ||||||||||||||||||||||||
5301 | #endif | - | ||||||||||||||||||||||||
5302 | ){ | - | ||||||||||||||||||||||||
5303 | int x = pOp->p2; | - | ||||||||||||||||||||||||
5304 | assert( pIdx->pTable==pTab ); | - | ||||||||||||||||||||||||
5305 | if( !HasRowid(pTab) ){
| 2004-36672 | ||||||||||||||||||||||||
5306 | Index *pPk = sqlite3PrimaryKeyIndex(pTab); | - | ||||||||||||||||||||||||
5307 | x = pPk->aiColumn[x]; | - | ||||||||||||||||||||||||
5308 | assert( x>=0 ); | - | ||||||||||||||||||||||||
5309 | } executed 2004 times by 1 test: end of block Executed by:
| 2004 | ||||||||||||||||||||||||
5310 | x = sqlite3ColumnOfIndex(pIdx, x); | - | ||||||||||||||||||||||||
5311 | if( x>=0 ){
| 16598-22078 | ||||||||||||||||||||||||
5312 | pOp->p2 = x; | - | ||||||||||||||||||||||||
5313 | pOp->p1 = pLevel->iIdxCur; | - | ||||||||||||||||||||||||
5314 | OpcodeRewriteTrace(db, k, pOp); | - | ||||||||||||||||||||||||
5315 | } executed 22078 times by 2 tests: end of block Executed by:
| 22078 | ||||||||||||||||||||||||
5316 | assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 || x>=0 | - | ||||||||||||||||||||||||
5317 | || pWInfo->eOnePass ); | - | ||||||||||||||||||||||||
5318 | }else if( pOp->opcode==OP_Rowid ){ executed 38676 times by 2 tests: end of block Executed by:
| 22402-38676 | ||||||||||||||||||||||||
5319 | pOp->p1 = pLevel->iIdxCur; | - | ||||||||||||||||||||||||
5320 | pOp->opcode = OP_IdxRowid; | - | ||||||||||||||||||||||||
5321 | OpcodeRewriteTrace(db, k, pOp); | - | ||||||||||||||||||||||||
5322 | }else if( pOp->opcode==OP_IfNullRow ){ executed 22402 times by 1 test: end of block Executed by:
| 1-30951 | ||||||||||||||||||||||||
5323 | pOp->p1 = pLevel->iIdxCur; | - | ||||||||||||||||||||||||
5324 | OpcodeRewriteTrace(db, k, pOp); | - | ||||||||||||||||||||||||
5325 | } executed 1 time by 1 test: end of block Executed by:
| 1 | ||||||||||||||||||||||||
5326 | } executed 92030 times by 2 tests: end of block Executed by:
| 92030 | ||||||||||||||||||||||||
5327 | #ifdef SQLITE_DEBUG | - | ||||||||||||||||||||||||
5328 | if( db->flags & SQLITE_VdbeAddopTrace ) printf("TRANSLATE complete\n"); | - | ||||||||||||||||||||||||
5329 | #endif | - | ||||||||||||||||||||||||
5330 | } executed 31116 times by 2 tests: end of block Executed by:
| 31116 | ||||||||||||||||||||||||
5331 | } executed 243132 times by 435 tests: end of block Executed by:
| 243132 | ||||||||||||||||||||||||
5332 | - | |||||||||||||||||||||||||
5333 | /* Final cleanup | - | ||||||||||||||||||||||||
5334 | */ | - | ||||||||||||||||||||||||
5335 | pParse->nQueryLoop = pWInfo->savedNQueryLoop; | - | ||||||||||||||||||||||||
5336 | whereInfoFree(db, pWInfo); | - | ||||||||||||||||||||||||
5337 | return; executed 332004 times by 435 tests: return; Executed by:
| 332004 | ||||||||||||||||||||||||
5338 | } | - | ||||||||||||||||||||||||
Source code | Switch to Preprocessed file |