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.
14 : **
15 : ** $Id: where.c 195361 2005-09-07 15:11:33Z iliaa $
16 : */
17 : #include "sqliteInt.h"
18 :
19 : /*
20 : ** The query generator uses an array of instances of this structure to
21 : ** help it analyze the subexpressions of the WHERE clause. Each WHERE
22 : ** clause subexpression is separated from the others by an AND operator.
23 : */
24 : typedef struct ExprInfo ExprInfo;
25 : struct ExprInfo {
26 : Expr *p; /* Pointer to the subexpression */
27 : u8 indexable; /* True if this subexprssion is usable by an index */
28 : short int idxLeft; /* p->pLeft is a column in this table number. -1 if
29 : ** p->pLeft is not the column of any table */
30 : short int idxRight; /* p->pRight is a column in this table number. -1 if
31 : ** p->pRight is not the column of any table */
32 : unsigned prereqLeft; /* Bitmask of tables referenced by p->pLeft */
33 : unsigned prereqRight; /* Bitmask of tables referenced by p->pRight */
34 : unsigned prereqAll; /* Bitmask of tables referenced by p */
35 : };
36 :
37 : /*
38 : ** An instance of the following structure keeps track of a mapping
39 : ** between VDBE cursor numbers and bitmasks. The VDBE cursor numbers
40 : ** are small integers contained in SrcList_item.iCursor and Expr.iTable
41 : ** fields. For any given WHERE clause, we want to track which cursors
42 : ** are being used, so we assign a single bit in a 32-bit word to track
43 : ** that cursor. Then a 32-bit integer is able to show the set of all
44 : ** cursors being used.
45 : */
46 : typedef struct ExprMaskSet ExprMaskSet;
47 : struct ExprMaskSet {
48 : int n; /* Number of assigned cursor values */
49 : int ix[31]; /* Cursor assigned to each bit */
50 : };
51 :
52 : /*
53 : ** Determine the number of elements in an array.
54 : */
55 : #define ARRAYSIZE(X) (sizeof(X)/sizeof(X[0]))
56 :
57 : /*
58 : ** This routine is used to divide the WHERE expression into subexpressions
59 : ** separated by the AND operator.
60 : **
61 : ** aSlot[] is an array of subexpressions structures.
62 : ** There are nSlot spaces left in this array. This routine attempts to
63 : ** split pExpr into subexpressions and fills aSlot[] with those subexpressions.
64 : ** The return value is the number of slots filled.
65 : */
66 527 : static int exprSplit(int nSlot, ExprInfo *aSlot, Expr *pExpr){
67 527 : int cnt = 0;
68 527 : if( pExpr==0 || nSlot<1 ) return 0;
69 73 : if( nSlot==1 || pExpr->op!=TK_AND ){
70 70 : aSlot[0].p = pExpr;
71 70 : return 1;
72 : }
73 3 : if( pExpr->pLeft->op!=TK_AND ){
74 3 : aSlot[0].p = pExpr->pLeft;
75 3 : cnt = 1 + exprSplit(nSlot-1, &aSlot[1], pExpr->pRight);
76 : }else{
77 0 : cnt = exprSplit(nSlot, aSlot, pExpr->pLeft);
78 0 : cnt += exprSplit(nSlot-cnt, &aSlot[cnt], pExpr->pRight);
79 : }
80 3 : return cnt;
81 : }
82 :
83 : /*
84 : ** Initialize an expression mask set
85 : */
86 : #define initMaskSet(P) memset(P, 0, sizeof(*P))
87 :
88 : /*
89 : ** Return the bitmask for the given cursor. Assign a new bitmask
90 : ** if this is the first time the cursor has been seen.
91 : */
92 1218 : static int getMask(ExprMaskSet *pMaskSet, int iCursor){
93 : int i;
94 1254 : for(i=0; i<pMaskSet->n; i++){
95 728 : if( pMaskSet->ix[i]==iCursor ) return 1<<i;
96 : }
97 526 : if( i==pMaskSet->n && i<ARRAYSIZE(pMaskSet->ix) ){
98 526 : pMaskSet->n++;
99 526 : pMaskSet->ix[i] = iCursor;
100 526 : return 1<<i;
101 : }
102 0 : return 0;
103 : }
104 :
105 : /*
106 : ** Destroy an expression mask set
107 : */
108 : #define freeMaskSet(P) /* NO-OP */
109 :
110 : /*
111 : ** This routine walks (recursively) an expression tree and generates
112 : ** a bitmask indicating which tables are used in that expression
113 : ** tree.
114 : **
115 : ** In order for this routine to work, the calling function must have
116 : ** previously invoked sqliteExprResolveIds() on the expression. See
117 : ** the header comment on that routine for additional information.
118 : ** The sqliteExprResolveIds() routines looks for column names and
119 : ** sets their opcodes to TK_COLUMN and their Expr.iTable fields to
120 : ** the VDBE cursor number of the table.
121 : */
122 381 : static int exprTableUsage(ExprMaskSet *pMaskSet, Expr *p){
123 381 : unsigned int mask = 0;
124 381 : if( p==0 ) return 0;
125 379 : if( p->op==TK_COLUMN ){
126 166 : mask = getMask(pMaskSet, p->iTable);
127 166 : if( mask==0 ) mask = -1;
128 166 : return mask;
129 : }
130 213 : if( p->pRight ){
131 78 : mask = exprTableUsage(pMaskSet, p->pRight);
132 : }
133 213 : if( p->pLeft ){
134 84 : mask |= exprTableUsage(pMaskSet, p->pLeft);
135 : }
136 213 : if( p->pList ){
137 : int i;
138 0 : for(i=0; i<p->pList->nExpr; i++){
139 0 : mask |= exprTableUsage(pMaskSet, p->pList->a[i].pExpr);
140 : }
141 : }
142 213 : return mask;
143 : }
144 :
145 : /*
146 : ** Return TRUE if the given operator is one of the operators that is
147 : ** allowed for an indexable WHERE clause. The allowed operators are
148 : ** "=", "<", ">", "<=", ">=", and "IN".
149 : */
150 73 : static int allowedOp(int op){
151 73 : switch( op ){
152 : case TK_LT:
153 : case TK_LE:
154 : case TK_GT:
155 : case TK_GE:
156 : case TK_EQ:
157 : case TK_IN:
158 68 : return 1;
159 : default:
160 5 : return 0;
161 : }
162 : }
163 :
164 : /*
165 : ** The input to this routine is an ExprInfo structure with only the
166 : ** "p" field filled in. The job of this routine is to analyze the
167 : ** subexpression and populate all the other fields of the ExprInfo
168 : ** structure.
169 : */
170 73 : static void exprAnalyze(ExprMaskSet *pMaskSet, ExprInfo *pInfo){
171 73 : Expr *pExpr = pInfo->p;
172 73 : pInfo->prereqLeft = exprTableUsage(pMaskSet, pExpr->pLeft);
173 73 : pInfo->prereqRight = exprTableUsage(pMaskSet, pExpr->pRight);
174 73 : pInfo->prereqAll = exprTableUsage(pMaskSet, pExpr);
175 73 : pInfo->indexable = 0;
176 73 : pInfo->idxLeft = -1;
177 73 : pInfo->idxRight = -1;
178 73 : if( allowedOp(pExpr->op) && (pInfo->prereqRight & pInfo->prereqLeft)==0 ){
179 68 : if( pExpr->pRight && pExpr->pRight->op==TK_COLUMN ){
180 8 : pInfo->idxRight = pExpr->pRight->iTable;
181 8 : pInfo->indexable = 1;
182 : }
183 68 : if( pExpr->pLeft->op==TK_COLUMN ){
184 68 : pInfo->idxLeft = pExpr->pLeft->iTable;
185 68 : pInfo->indexable = 1;
186 : }
187 : }
188 73 : }
189 :
190 : /*
191 : ** pOrderBy is an ORDER BY clause from a SELECT statement. pTab is the
192 : ** left-most table in the FROM clause of that same SELECT statement and
193 : ** the table has a cursor number of "base".
194 : **
195 : ** This routine attempts to find an index for pTab that generates the
196 : ** correct record sequence for the given ORDER BY clause. The return value
197 : ** is a pointer to an index that does the job. NULL is returned if the
198 : ** table has no index that will generate the correct sort order.
199 : **
200 : ** If there are two or more indices that generate the correct sort order
201 : ** and pPreferredIdx is one of those indices, then return pPreferredIdx.
202 : **
203 : ** nEqCol is the number of columns of pPreferredIdx that are used as
204 : ** equality constraints. Any index returned must have exactly this same
205 : ** set of columns. The ORDER BY clause only matches index columns beyond the
206 : ** the first nEqCol columns.
207 : **
208 : ** All terms of the ORDER BY clause must be either ASC or DESC. The
209 : ** *pbRev value is set to 1 if the ORDER BY clause is all DESC and it is
210 : ** set to 0 if the ORDER BY clause is all ASC.
211 : */
212 : static Index *findSortingIndex(
213 : Table *pTab, /* The table to be sorted */
214 : int base, /* Cursor number for pTab */
215 : ExprList *pOrderBy, /* The ORDER BY clause */
216 : Index *pPreferredIdx, /* Use this index, if possible and not NULL */
217 : int nEqCol, /* Number of index columns used with == constraints */
218 : int *pbRev /* Set to 1 if ORDER BY is DESC */
219 9 : ){
220 : int i, j;
221 : Index *pMatch;
222 : Index *pIdx;
223 : int sortOrder;
224 :
225 : assert( pOrderBy!=0 );
226 : assert( pOrderBy->nExpr>0 );
227 9 : sortOrder = pOrderBy->a[0].sortOrder & SQLITE_SO_DIRMASK;
228 18 : for(i=0; i<pOrderBy->nExpr; i++){
229 : Expr *p;
230 9 : if( (pOrderBy->a[i].sortOrder & SQLITE_SO_DIRMASK)!=sortOrder ){
231 : /* Indices can only be used if all ORDER BY terms are either
232 : ** DESC or ASC. Indices cannot be used on a mixture. */
233 0 : return 0;
234 : }
235 9 : if( (pOrderBy->a[i].sortOrder & SQLITE_SO_TYPEMASK)!=SQLITE_SO_UNK ){
236 : /* Do not sort by index if there is a COLLATE clause */
237 0 : return 0;
238 : }
239 9 : p = pOrderBy->a[i].pExpr;
240 9 : if( p->op!=TK_COLUMN || p->iTable!=base ){
241 : /* Can not use an index sort on anything that is not a column in the
242 : ** left-most table of the FROM clause */
243 0 : return 0;
244 : }
245 : }
246 :
247 : /* If we get this far, it means the ORDER BY clause consists only of
248 : ** ascending columns in the left-most table of the FROM clause. Now
249 : ** check for a matching index.
250 : */
251 9 : pMatch = 0;
252 19 : for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
253 10 : int nExpr = pOrderBy->nExpr;
254 10 : if( pIdx->nColumn < nEqCol || pIdx->nColumn < nExpr ) continue;
255 10 : for(i=j=0; i<nEqCol; i++){
256 0 : if( pPreferredIdx->aiColumn[i]!=pIdx->aiColumn[i] ) break;
257 0 : if( j<nExpr && pOrderBy->a[j].pExpr->iColumn==pIdx->aiColumn[i] ){ j++; }
258 : }
259 10 : if( i<nEqCol ) continue;
260 19 : for(i=0; i+j<nExpr; i++){
261 10 : if( pOrderBy->a[i+j].pExpr->iColumn!=pIdx->aiColumn[i+nEqCol] ) break;
262 : }
263 10 : if( i+j>=nExpr ){
264 9 : pMatch = pIdx;
265 9 : if( pIdx==pPreferredIdx ) break;
266 : }
267 : }
268 9 : if( pMatch && pbRev ){
269 9 : *pbRev = sortOrder==SQLITE_SO_DESC;
270 : }
271 9 : return pMatch;
272 : }
273 :
274 : /*
275 : ** Disable a term in the WHERE clause. Except, do not disable the term
276 : ** if it controls a LEFT OUTER JOIN and it did not originate in the ON
277 : ** or USING clause of that join.
278 : **
279 : ** Consider the term t2.z='ok' in the following queries:
280 : **
281 : ** (1) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x WHERE t2.z='ok'
282 : ** (2) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x AND t2.z='ok'
283 : ** (3) SELECT * FROM t1, t2 WHERE t1.a=t2.x AND t2.z='ok'
284 : **
285 : ** The t2.z='ok' is disabled in the in (2) because it did not originate
286 : ** in the ON clause. The term is disabled in (3) because it is not part
287 : ** of a LEFT OUTER JOIN. In (1), the term is not disabled.
288 : **
289 : ** Disabling a term causes that term to not be tested in the inner loop
290 : ** of the join. Disabling is an optimization. We would get the correct
291 : ** results if nothing were ever disabled, but joins might run a little
292 : ** slower. The trick is to disable as much as we can without disabling
293 : ** too much. If we disabled in (1), we'd get the wrong answer.
294 : ** See ticket #813.
295 : */
296 53 : static void disableTerm(WhereLevel *pLevel, Expr **ppExpr){
297 53 : Expr *pExpr = *ppExpr;
298 53 : if( pLevel->iLeftJoin==0 || ExprHasProperty(pExpr, EP_FromJoin) ){
299 53 : *ppExpr = 0;
300 : }
301 53 : }
302 :
303 : /*
304 : ** Generate the beginning of the loop used for WHERE clause processing.
305 : ** The return value is a pointer to an (opaque) structure that contains
306 : ** information needed to terminate the loop. Later, the calling routine
307 : ** should invoke sqliteWhereEnd() with the return value of this function
308 : ** in order to complete the WHERE clause processing.
309 : **
310 : ** If an error occurs, this routine returns NULL.
311 : **
312 : ** The basic idea is to do a nested loop, one loop for each table in
313 : ** the FROM clause of a select. (INSERT and UPDATE statements are the
314 : ** same as a SELECT with only a single table in the FROM clause.) For
315 : ** example, if the SQL is this:
316 : **
317 : ** SELECT * FROM t1, t2, t3 WHERE ...;
318 : **
319 : ** Then the code generated is conceptually like the following:
320 : **
321 : ** foreach row1 in t1 do \ Code generated
322 : ** foreach row2 in t2 do |-- by sqliteWhereBegin()
323 : ** foreach row3 in t3 do /
324 : ** ...
325 : ** end \ Code generated
326 : ** end |-- by sqliteWhereEnd()
327 : ** end /
328 : **
329 : ** There are Btree cursors associated with each table. t1 uses cursor
330 : ** number pTabList->a[0].iCursor. t2 uses the cursor pTabList->a[1].iCursor.
331 : ** And so forth. This routine generates code to open those VDBE cursors
332 : ** and sqliteWhereEnd() generates the code to close them.
333 : **
334 : ** If the WHERE clause is empty, the foreach loops must each scan their
335 : ** entire tables. Thus a three-way join is an O(N^3) operation. But if
336 : ** the tables have indices and there are terms in the WHERE clause that
337 : ** refer to those indices, a complete table scan can be avoided and the
338 : ** code will run much faster. Most of the work of this routine is checking
339 : ** to see if there are indices that can be used to speed up the loop.
340 : **
341 : ** Terms of the WHERE clause are also used to limit which rows actually
342 : ** make it to the "..." in the middle of the loop. After each "foreach",
343 : ** terms of the WHERE clause that use only terms in that loop and outer
344 : ** loops are evaluated and if false a jump is made around all subsequent
345 : ** inner loops (or around the "..." if the test occurs within the inner-
346 : ** most loop)
347 : **
348 : ** OUTER JOINS
349 : **
350 : ** An outer join of tables t1 and t2 is conceptally coded as follows:
351 : **
352 : ** foreach row1 in t1 do
353 : ** flag = 0
354 : ** foreach row2 in t2 do
355 : ** start:
356 : ** ...
357 : ** flag = 1
358 : ** end
359 : ** if flag==0 then
360 : ** move the row2 cursor to a null row
361 : ** goto start
362 : ** fi
363 : ** end
364 : **
365 : ** ORDER BY CLAUSE PROCESSING
366 : **
367 : ** *ppOrderBy is a pointer to the ORDER BY clause of a SELECT statement,
368 : ** if there is one. If there is no ORDER BY clause or if this routine
369 : ** is called from an UPDATE or DELETE statement, then ppOrderBy is NULL.
370 : **
371 : ** If an index can be used so that the natural output order of the table
372 : ** scan is correct for the ORDER BY clause, then that index is used and
373 : ** *ppOrderBy is set to NULL. This is an optimization that prevents an
374 : ** unnecessary sort of the result set if an index appropriate for the
375 : ** ORDER BY clause already exists.
376 : **
377 : ** If the where clause loops cannot be arranged to provide the correct
378 : ** output order, then the *ppOrderBy is unchanged.
379 : */
380 : WhereInfo *sqliteWhereBegin(
381 : Parse *pParse, /* The parser context */
382 : SrcList *pTabList, /* A list of all tables to be scanned */
383 : Expr *pWhere, /* The WHERE clause */
384 : int pushKey, /* If TRUE, leave the table key on the stack */
385 : ExprList **ppOrderBy /* An ORDER BY clause, or NULL */
386 524 : ){
387 : int i; /* Loop counter */
388 : WhereInfo *pWInfo; /* Will become the return value of this function */
389 524 : Vdbe *v = pParse->pVdbe; /* The virtual database engine */
390 524 : int brk, cont = 0; /* Addresses used during code generation */
391 : int nExpr; /* Number of subexpressions in the WHERE clause */
392 : int loopMask; /* One bit set for each outer loop */
393 : int haveKey; /* True if KEY is on the stack */
394 : ExprMaskSet maskSet; /* The expression mask set */
395 : int iDirectEq[32]; /* Term of the form ROWID==X for the N-th table */
396 : int iDirectLt[32]; /* Term of the form ROWID<X or ROWID<=X */
397 : int iDirectGt[32]; /* Term of the form ROWID>X or ROWID>=X */
398 : ExprInfo aExpr[101]; /* The WHERE clause is divided into these expressions */
399 :
400 : /* pushKey is only allowed if there is a single table (as in an INSERT or
401 : ** UPDATE statement)
402 : */
403 : assert( pushKey==0 || pTabList->nSrc==1 );
404 :
405 : /* Split the WHERE clause into separate subexpressions where each
406 : ** subexpression is separated by an AND operator. If the aExpr[]
407 : ** array fills up, the last entry might point to an expression which
408 : ** contains additional unfactored AND operators.
409 : */
410 524 : initMaskSet(&maskSet);
411 524 : memset(aExpr, 0, sizeof(aExpr));
412 524 : nExpr = exprSplit(ARRAYSIZE(aExpr), aExpr, pWhere);
413 524 : if( nExpr==ARRAYSIZE(aExpr) ){
414 0 : sqliteErrorMsg(pParse, "WHERE clause too complex - no more "
415 : "than %d terms allowed", (int)ARRAYSIZE(aExpr)-1);
416 0 : return 0;
417 : }
418 :
419 : /* Allocate and initialize the WhereInfo structure that will become the
420 : ** return value.
421 : */
422 524 : pWInfo = sqliteMalloc( sizeof(WhereInfo) + pTabList->nSrc*sizeof(WhereLevel));
423 524 : if( sqlite_malloc_failed ){
424 0 : sqliteFree(pWInfo);
425 0 : return 0;
426 : }
427 524 : pWInfo->pParse = pParse;
428 524 : pWInfo->pTabList = pTabList;
429 524 : pWInfo->peakNTab = pWInfo->savedNTab = pParse->nTab;
430 524 : pWInfo->iBreak = sqliteVdbeMakeLabel(v);
431 :
432 : /* Special case: a WHERE clause that is constant. Evaluate the
433 : ** expression and either jump over all of the code or fall thru.
434 : */
435 524 : if( pWhere && (pTabList->nSrc==0 || sqliteExprIsConstant(pWhere)) ){
436 1 : sqliteExprIfFalse(pParse, pWhere, pWInfo->iBreak, 1);
437 1 : pWhere = 0;
438 : }
439 :
440 : /* Analyze all of the subexpressions.
441 : */
442 597 : for(i=0; i<nExpr; i++){
443 73 : exprAnalyze(&maskSet, &aExpr[i]);
444 :
445 : /* If we are executing a trigger body, remove all references to
446 : ** new.* and old.* tables from the prerequisite masks.
447 : */
448 73 : if( pParse->trigStack ){
449 : int x;
450 0 : if( (x = pParse->trigStack->newIdx) >= 0 ){
451 0 : int mask = ~getMask(&maskSet, x);
452 0 : aExpr[i].prereqRight &= mask;
453 0 : aExpr[i].prereqLeft &= mask;
454 0 : aExpr[i].prereqAll &= mask;
455 : }
456 0 : if( (x = pParse->trigStack->oldIdx) >= 0 ){
457 0 : int mask = ~getMask(&maskSet, x);
458 0 : aExpr[i].prereqRight &= mask;
459 0 : aExpr[i].prereqLeft &= mask;
460 0 : aExpr[i].prereqAll &= mask;
461 : }
462 : }
463 : }
464 :
465 : /* Figure out what index to use (if any) for each nested loop.
466 : ** Make pWInfo->a[i].pIdx point to the index to use for the i-th nested
467 : ** loop where i==0 is the outer loop and i==pTabList->nSrc-1 is the inner
468 : ** loop.
469 : **
470 : ** If terms exist that use the ROWID of any table, then set the
471 : ** iDirectEq[], iDirectLt[], or iDirectGt[] elements for that table
472 : ** to the index of the term containing the ROWID. We always prefer
473 : ** to use a ROWID which can directly access a table rather than an
474 : ** index which requires reading an index first to get the rowid then
475 : ** doing a second read of the actual database table.
476 : **
477 : ** Actually, if there are more than 32 tables in the join, only the
478 : ** first 32 tables are candidates for indices. This is (again) due
479 : ** to the limit of 32 bits in an integer bitmask.
480 : */
481 524 : loopMask = 0;
482 1050 : for(i=0; i<pTabList->nSrc && i<ARRAYSIZE(iDirectEq); i++){
483 : int j;
484 526 : int iCur = pTabList->a[i].iCursor; /* The cursor for this table */
485 526 : int mask = getMask(&maskSet, iCur); /* Cursor mask for this table */
486 526 : Table *pTab = pTabList->a[i].pTab;
487 : Index *pIdx;
488 526 : Index *pBestIdx = 0;
489 526 : int bestScore = 0;
490 :
491 : /* Check to see if there is an expression that uses only the
492 : ** ROWID field of this table. For terms of the form ROWID==expr
493 : ** set iDirectEq[i] to the index of the term. For terms of the
494 : ** form ROWID<expr or ROWID<=expr set iDirectLt[i] to the term index.
495 : ** For terms like ROWID>expr or ROWID>=expr set iDirectGt[i].
496 : **
497 : ** (Added:) Treat ROWID IN expr like ROWID=expr.
498 : */
499 526 : pWInfo->a[i].iCur = -1;
500 526 : iDirectEq[i] = -1;
501 526 : iDirectLt[i] = -1;
502 526 : iDirectGt[i] = -1;
503 610 : for(j=0; j<nExpr; j++){
504 84 : if( aExpr[j].idxLeft==iCur && aExpr[j].p->pLeft->iColumn<0
505 : && (aExpr[j].prereqRight & loopMask)==aExpr[j].prereqRight ){
506 14 : switch( aExpr[j].p->op ){
507 : case TK_IN:
508 12 : case TK_EQ: iDirectEq[i] = j; break;
509 : case TK_LE:
510 2 : case TK_LT: iDirectLt[i] = j; break;
511 : case TK_GE:
512 0 : case TK_GT: iDirectGt[i] = j; break;
513 : }
514 : }
515 84 : if( aExpr[j].idxRight==iCur && aExpr[j].p->pRight->iColumn<0
516 : && (aExpr[j].prereqLeft & loopMask)==aExpr[j].prereqLeft ){
517 0 : switch( aExpr[j].p->op ){
518 0 : case TK_EQ: iDirectEq[i] = j; break;
519 : case TK_LE:
520 0 : case TK_LT: iDirectGt[i] = j; break;
521 : case TK_GE:
522 0 : case TK_GT: iDirectLt[i] = j; break;
523 : }
524 : }
525 : }
526 526 : if( iDirectEq[i]>=0 ){
527 12 : loopMask |= mask;
528 12 : pWInfo->a[i].pIdx = 0;
529 12 : continue;
530 : }
531 :
532 : /* Do a search for usable indices. Leave pBestIdx pointing to
533 : ** the "best" index. pBestIdx is left set to NULL if no indices
534 : ** are usable.
535 : **
536 : ** The best index is determined as follows. For each of the
537 : ** left-most terms that is fixed by an equality operator, add
538 : ** 8 to the score. The right-most term of the index may be
539 : ** constrained by an inequality. Add 1 if for an "x<..." constraint
540 : ** and add 2 for an "x>..." constraint. Chose the index that
541 : ** gives the best score.
542 : **
543 : ** This scoring system is designed so that the score can later be
544 : ** used to determine how the index is used. If the score&7 is 0
545 : ** then all constraints are equalities. If score&1 is not 0 then
546 : ** there is an inequality used as a termination key. (ex: "x<...")
547 : ** If score&2 is not 0 then there is an inequality used as the
548 : ** start key. (ex: "x>..."). A score or 4 is the special case
549 : ** of an IN operator constraint. (ex: "x IN ...").
550 : **
551 : ** The IN operator (as in "<expr> IN (...)") is treated the same as
552 : ** an equality comparison except that it can only be used on the
553 : ** left-most column of an index and other terms of the WHERE clause
554 : ** cannot be used in conjunction with the IN operator to help satisfy
555 : ** other columns of the index.
556 : */
557 674 : for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
558 160 : int eqMask = 0; /* Index columns covered by an x=... term */
559 160 : int ltMask = 0; /* Index columns covered by an x<... term */
560 160 : int gtMask = 0; /* Index columns covered by an x>... term */
561 160 : int inMask = 0; /* Index columns covered by an x IN .. term */
562 : int nEq, m, score;
563 :
564 160 : if( pIdx->nColumn>32 ) continue; /* Ignore indices too many columns */
565 254 : for(j=0; j<nExpr; j++){
566 94 : if( aExpr[j].idxLeft==iCur
567 : && (aExpr[j].prereqRight & loopMask)==aExpr[j].prereqRight ){
568 61 : int iColumn = aExpr[j].p->pLeft->iColumn;
569 : int k;
570 91 : for(k=0; k<pIdx->nColumn; k++){
571 61 : if( pIdx->aiColumn[k]==iColumn ){
572 31 : switch( aExpr[j].p->op ){
573 : case TK_IN: {
574 0 : if( k==0 ) inMask |= 1;
575 0 : break;
576 : }
577 : case TK_EQ: {
578 31 : eqMask |= 1<<k;
579 31 : break;
580 : }
581 : case TK_LE:
582 : case TK_LT: {
583 0 : ltMask |= 1<<k;
584 0 : break;
585 : }
586 : case TK_GE:
587 : case TK_GT: {
588 0 : gtMask |= 1<<k;
589 : break;
590 : }
591 : default: {
592 : /* CANT_HAPPEN */
593 : assert( 0 );
594 : break;
595 : }
596 : }
597 31 : break;
598 : }
599 : }
600 : }
601 94 : if( aExpr[j].idxRight==iCur
602 : && (aExpr[j].prereqLeft & loopMask)==aExpr[j].prereqLeft ){
603 16 : int iColumn = aExpr[j].p->pRight->iColumn;
604 : int k;
605 24 : for(k=0; k<pIdx->nColumn; k++){
606 16 : if( pIdx->aiColumn[k]==iColumn ){
607 8 : switch( aExpr[j].p->op ){
608 : case TK_EQ: {
609 8 : eqMask |= 1<<k;
610 8 : break;
611 : }
612 : case TK_LE:
613 : case TK_LT: {
614 0 : gtMask |= 1<<k;
615 0 : break;
616 : }
617 : case TK_GE:
618 : case TK_GT: {
619 0 : ltMask |= 1<<k;
620 : break;
621 : }
622 : default: {
623 : /* CANT_HAPPEN */
624 : assert( 0 );
625 : break;
626 : }
627 : }
628 8 : break;
629 : }
630 : }
631 : }
632 : }
633 :
634 : /* The following loop ends with nEq set to the number of columns
635 : ** on the left of the index with == constraints.
636 : */
637 199 : for(nEq=0; nEq<pIdx->nColumn; nEq++){
638 160 : m = (1<<(nEq+1))-1;
639 160 : if( (m & eqMask)!=m ) break;
640 : }
641 160 : score = nEq*8; /* Base score is 8 times number of == constraints */
642 160 : m = 1<<nEq;
643 160 : if( m & ltMask ) score++; /* Increase score for a < constraint */
644 160 : if( m & gtMask ) score+=2; /* Increase score for a > constraint */
645 160 : if( score==0 && inMask ) score = 4; /* Default score for IN constraint */
646 160 : if( score>bestScore ){
647 39 : pBestIdx = pIdx;
648 39 : bestScore = score;
649 : }
650 : }
651 514 : pWInfo->a[i].pIdx = pBestIdx;
652 514 : pWInfo->a[i].score = bestScore;
653 514 : pWInfo->a[i].bRev = 0;
654 514 : loopMask |= mask;
655 514 : if( pBestIdx ){
656 39 : pWInfo->a[i].iCur = pParse->nTab++;
657 39 : pWInfo->peakNTab = pParse->nTab;
658 : }
659 : }
660 :
661 : /* Check to see if the ORDER BY clause is or can be satisfied by the
662 : ** use of an index on the first table.
663 : */
664 524 : if( ppOrderBy && *ppOrderBy && pTabList->nSrc>0 ){
665 : Index *pSortIdx;
666 : Index *pIdx;
667 : Table *pTab;
668 9 : int bRev = 0;
669 :
670 9 : pTab = pTabList->a[0].pTab;
671 9 : pIdx = pWInfo->a[0].pIdx;
672 9 : if( pIdx && pWInfo->a[0].score==4 ){
673 : /* If there is already an IN index on the left-most table,
674 : ** it will not give the correct sort order.
675 : ** So, pretend that no suitable index is found.
676 : */
677 0 : pSortIdx = 0;
678 9 : }else if( iDirectEq[0]>=0 || iDirectLt[0]>=0 || iDirectGt[0]>=0 ){
679 : /* If the left-most column is accessed using its ROWID, then do
680 : ** not try to sort by index.
681 : */
682 0 : pSortIdx = 0;
683 : }else{
684 9 : int nEqCol = (pWInfo->a[0].score+4)/8;
685 9 : pSortIdx = findSortingIndex(pTab, pTabList->a[0].iCursor,
686 : *ppOrderBy, pIdx, nEqCol, &bRev);
687 : }
688 9 : if( pSortIdx && (pIdx==0 || pIdx==pSortIdx) ){
689 9 : if( pIdx==0 ){
690 9 : pWInfo->a[0].pIdx = pSortIdx;
691 9 : pWInfo->a[0].iCur = pParse->nTab++;
692 9 : pWInfo->peakNTab = pParse->nTab;
693 : }
694 9 : pWInfo->a[0].bRev = bRev;
695 9 : *ppOrderBy = 0;
696 : }
697 : }
698 :
699 : /* Open all tables in the pTabList and all indices used by those tables.
700 : */
701 1050 : for(i=0; i<pTabList->nSrc; i++){
702 : Table *pTab;
703 : Index *pIx;
704 :
705 526 : pTab = pTabList->a[i].pTab;
706 526 : if( pTab->isTransient || pTab->pSelect ) continue;
707 526 : sqliteVdbeAddOp(v, OP_Integer, pTab->iDb, 0);
708 526 : sqliteVdbeOp3(v, OP_OpenRead, pTabList->a[i].iCursor, pTab->tnum,
709 : pTab->zName, P3_STATIC);
710 526 : sqliteCodeVerifySchema(pParse, pTab->iDb);
711 526 : if( (pIx = pWInfo->a[i].pIdx)!=0 ){
712 48 : sqliteVdbeAddOp(v, OP_Integer, pIx->iDb, 0);
713 48 : sqliteVdbeOp3(v, OP_OpenRead, pWInfo->a[i].iCur, pIx->tnum, pIx->zName,0);
714 : }
715 : }
716 :
717 : /* Generate the code to do the search
718 : */
719 524 : loopMask = 0;
720 1050 : for(i=0; i<pTabList->nSrc; i++){
721 : int j, k;
722 526 : int iCur = pTabList->a[i].iCursor;
723 : Index *pIdx;
724 526 : WhereLevel *pLevel = &pWInfo->a[i];
725 :
726 : /* If this is the right table of a LEFT OUTER JOIN, allocate and
727 : ** initialize a memory cell that records if this table matches any
728 : ** row of the left table of the join.
729 : */
730 526 : if( i>0 && (pTabList->a[i-1].jointype & JT_LEFT)!=0 ){
731 8 : if( !pParse->nMem ) pParse->nMem++;
732 8 : pLevel->iLeftJoin = pParse->nMem++;
733 8 : sqliteVdbeAddOp(v, OP_String, 0, 0);
734 8 : sqliteVdbeAddOp(v, OP_MemStore, pLevel->iLeftJoin, 1);
735 : }
736 :
737 526 : pIdx = pLevel->pIdx;
738 526 : pLevel->inOp = OP_Noop;
739 538 : if( i<ARRAYSIZE(iDirectEq) && iDirectEq[i]>=0 ){
740 : /* Case 1: We can directly reference a single row using an
741 : ** equality comparison against the ROWID field. Or
742 : ** we reference multiple rows using a "rowid IN (...)"
743 : ** construct.
744 : */
745 12 : k = iDirectEq[i];
746 : assert( k<nExpr );
747 : assert( aExpr[k].p!=0 );
748 : assert( aExpr[k].idxLeft==iCur || aExpr[k].idxRight==iCur );
749 12 : brk = pLevel->brk = sqliteVdbeMakeLabel(v);
750 12 : if( aExpr[k].idxLeft==iCur ){
751 12 : Expr *pX = aExpr[k].p;
752 12 : if( pX->op!=TK_IN ){
753 12 : sqliteExprCode(pParse, aExpr[k].p->pRight);
754 0 : }else if( pX->pList ){
755 0 : sqliteVdbeAddOp(v, OP_SetFirst, pX->iTable, brk);
756 0 : pLevel->inOp = OP_SetNext;
757 0 : pLevel->inP1 = pX->iTable;
758 0 : pLevel->inP2 = sqliteVdbeCurrentAddr(v);
759 : }else{
760 : assert( pX->pSelect );
761 0 : sqliteVdbeAddOp(v, OP_Rewind, pX->iTable, brk);
762 0 : sqliteVdbeAddOp(v, OP_KeyAsData, pX->iTable, 1);
763 0 : pLevel->inP2 = sqliteVdbeAddOp(v, OP_FullKey, pX->iTable, 0);
764 0 : pLevel->inOp = OP_Next;
765 0 : pLevel->inP1 = pX->iTable;
766 : }
767 : }else{
768 0 : sqliteExprCode(pParse, aExpr[k].p->pLeft);
769 : }
770 12 : disableTerm(pLevel, &aExpr[k].p);
771 12 : cont = pLevel->cont = sqliteVdbeMakeLabel(v);
772 12 : sqliteVdbeAddOp(v, OP_MustBeInt, 1, brk);
773 12 : haveKey = 0;
774 12 : sqliteVdbeAddOp(v, OP_NotExists, iCur, brk);
775 12 : pLevel->op = OP_Noop;
776 553 : }else if( pIdx!=0 && pLevel->score>0 && pLevel->score%4==0 ){
777 : /* Case 2: There is an index and all terms of the WHERE clause that
778 : ** refer to the index use the "==" or "IN" operators.
779 : */
780 : int start;
781 : int testOp;
782 39 : int nColumn = (pLevel->score+4)/8;
783 39 : brk = pLevel->brk = sqliteVdbeMakeLabel(v);
784 78 : for(j=0; j<nColumn; j++){
785 42 : for(k=0; k<nExpr; k++){
786 42 : Expr *pX = aExpr[k].p;
787 42 : if( pX==0 ) continue;
788 42 : if( aExpr[k].idxLeft==iCur
789 : && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
790 : && pX->pLeft->iColumn==pIdx->aiColumn[j]
791 : ){
792 31 : if( pX->op==TK_EQ ){
793 31 : sqliteExprCode(pParse, pX->pRight);
794 31 : disableTerm(pLevel, &aExpr[k].p);
795 31 : break;
796 : }
797 0 : if( pX->op==TK_IN && nColumn==1 ){
798 0 : if( pX->pList ){
799 0 : sqliteVdbeAddOp(v, OP_SetFirst, pX->iTable, brk);
800 0 : pLevel->inOp = OP_SetNext;
801 0 : pLevel->inP1 = pX->iTable;
802 0 : pLevel->inP2 = sqliteVdbeCurrentAddr(v);
803 : }else{
804 : assert( pX->pSelect );
805 0 : sqliteVdbeAddOp(v, OP_Rewind, pX->iTable, brk);
806 0 : sqliteVdbeAddOp(v, OP_KeyAsData, pX->iTable, 1);
807 0 : pLevel->inP2 = sqliteVdbeAddOp(v, OP_FullKey, pX->iTable, 0);
808 0 : pLevel->inOp = OP_Next;
809 0 : pLevel->inP1 = pX->iTable;
810 : }
811 0 : disableTerm(pLevel, &aExpr[k].p);
812 0 : break;
813 : }
814 : }
815 11 : if( aExpr[k].idxRight==iCur
816 : && aExpr[k].p->op==TK_EQ
817 : && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
818 : && aExpr[k].p->pRight->iColumn==pIdx->aiColumn[j]
819 : ){
820 8 : sqliteExprCode(pParse, aExpr[k].p->pLeft);
821 8 : disableTerm(pLevel, &aExpr[k].p);
822 8 : break;
823 : }
824 : }
825 : }
826 39 : pLevel->iMem = pParse->nMem++;
827 39 : cont = pLevel->cont = sqliteVdbeMakeLabel(v);
828 39 : sqliteVdbeAddOp(v, OP_NotNull, -nColumn, sqliteVdbeCurrentAddr(v)+3);
829 39 : sqliteVdbeAddOp(v, OP_Pop, nColumn, 0);
830 39 : sqliteVdbeAddOp(v, OP_Goto, 0, brk);
831 39 : sqliteVdbeAddOp(v, OP_MakeKey, nColumn, 0);
832 39 : sqliteAddIdxKeyType(v, pIdx);
833 78 : if( nColumn==pIdx->nColumn || pLevel->bRev ){
834 39 : sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 0);
835 39 : testOp = OP_IdxGT;
836 : }else{
837 0 : sqliteVdbeAddOp(v, OP_Dup, 0, 0);
838 0 : sqliteVdbeAddOp(v, OP_IncrKey, 0, 0);
839 0 : sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
840 0 : testOp = OP_IdxGE;
841 : }
842 39 : if( pLevel->bRev ){
843 : /* Scan in reverse order */
844 0 : sqliteVdbeAddOp(v, OP_IncrKey, 0, 0);
845 0 : sqliteVdbeAddOp(v, OP_MoveLt, pLevel->iCur, brk);
846 0 : start = sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
847 0 : sqliteVdbeAddOp(v, OP_IdxLT, pLevel->iCur, brk);
848 0 : pLevel->op = OP_Prev;
849 : }else{
850 : /* Scan in the forward order */
851 39 : sqliteVdbeAddOp(v, OP_MoveTo, pLevel->iCur, brk);
852 39 : start = sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
853 39 : sqliteVdbeAddOp(v, testOp, pLevel->iCur, brk);
854 39 : pLevel->op = OP_Next;
855 : }
856 39 : sqliteVdbeAddOp(v, OP_RowKey, pLevel->iCur, 0);
857 39 : sqliteVdbeAddOp(v, OP_IdxIsNull, nColumn, cont);
858 39 : sqliteVdbeAddOp(v, OP_IdxRecno, pLevel->iCur, 0);
859 39 : if( i==pTabList->nSrc-1 && pushKey ){
860 0 : haveKey = 1;
861 : }else{
862 39 : sqliteVdbeAddOp(v, OP_MoveTo, iCur, 0);
863 39 : haveKey = 0;
864 : }
865 39 : pLevel->p1 = pLevel->iCur;
866 39 : pLevel->p2 = start;
867 477 : }else if( i<ARRAYSIZE(iDirectLt) && (iDirectLt[i]>=0 || iDirectGt[i]>=0) ){
868 : /* Case 3: We have an inequality comparison against the ROWID field.
869 : */
870 2 : int testOp = OP_Noop;
871 : int start;
872 :
873 2 : brk = pLevel->brk = sqliteVdbeMakeLabel(v);
874 2 : cont = pLevel->cont = sqliteVdbeMakeLabel(v);
875 2 : if( iDirectGt[i]>=0 ){
876 0 : k = iDirectGt[i];
877 : assert( k<nExpr );
878 : assert( aExpr[k].p!=0 );
879 : assert( aExpr[k].idxLeft==iCur || aExpr[k].idxRight==iCur );
880 0 : if( aExpr[k].idxLeft==iCur ){
881 0 : sqliteExprCode(pParse, aExpr[k].p->pRight);
882 : }else{
883 0 : sqliteExprCode(pParse, aExpr[k].p->pLeft);
884 : }
885 0 : sqliteVdbeAddOp(v, OP_ForceInt,
886 : aExpr[k].p->op==TK_LT || aExpr[k].p->op==TK_GT, brk);
887 0 : sqliteVdbeAddOp(v, OP_MoveTo, iCur, brk);
888 0 : disableTerm(pLevel, &aExpr[k].p);
889 : }else{
890 2 : sqliteVdbeAddOp(v, OP_Rewind, iCur, brk);
891 : }
892 2 : if( iDirectLt[i]>=0 ){
893 2 : k = iDirectLt[i];
894 : assert( k<nExpr );
895 : assert( aExpr[k].p!=0 );
896 : assert( aExpr[k].idxLeft==iCur || aExpr[k].idxRight==iCur );
897 2 : if( aExpr[k].idxLeft==iCur ){
898 2 : sqliteExprCode(pParse, aExpr[k].p->pRight);
899 : }else{
900 0 : sqliteExprCode(pParse, aExpr[k].p->pLeft);
901 : }
902 : /* sqliteVdbeAddOp(v, OP_MustBeInt, 0, sqliteVdbeCurrentAddr(v)+1); */
903 2 : pLevel->iMem = pParse->nMem++;
904 2 : sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
905 4 : if( aExpr[k].p->op==TK_LT || aExpr[k].p->op==TK_GT ){
906 2 : testOp = OP_Ge;
907 : }else{
908 0 : testOp = OP_Gt;
909 : }
910 2 : disableTerm(pLevel, &aExpr[k].p);
911 : }
912 2 : start = sqliteVdbeCurrentAddr(v);
913 2 : pLevel->op = OP_Next;
914 2 : pLevel->p1 = iCur;
915 2 : pLevel->p2 = start;
916 2 : if( testOp!=OP_Noop ){
917 2 : sqliteVdbeAddOp(v, OP_Recno, iCur, 0);
918 2 : sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
919 2 : sqliteVdbeAddOp(v, testOp, 0, brk);
920 : }
921 2 : haveKey = 0;
922 473 : }else if( pIdx==0 ){
923 : /* Case 4: There is no usable index. We must do a complete
924 : ** scan of the entire database table.
925 : */
926 : int start;
927 :
928 464 : brk = pLevel->brk = sqliteVdbeMakeLabel(v);
929 464 : cont = pLevel->cont = sqliteVdbeMakeLabel(v);
930 464 : sqliteVdbeAddOp(v, OP_Rewind, iCur, brk);
931 464 : start = sqliteVdbeCurrentAddr(v);
932 464 : pLevel->op = OP_Next;
933 464 : pLevel->p1 = iCur;
934 464 : pLevel->p2 = start;
935 464 : haveKey = 0;
936 : }else{
937 : /* Case 5: The WHERE clause term that refers to the right-most
938 : ** column of the index is an inequality. For example, if
939 : ** the index is on (x,y,z) and the WHERE clause is of the
940 : ** form "x=5 AND y<10" then this case is used. Only the
941 : ** right-most column can be an inequality - the rest must
942 : ** use the "==" operator.
943 : **
944 : ** This case is also used when there are no WHERE clause
945 : ** constraints but an index is selected anyway, in order
946 : ** to force the output order to conform to an ORDER BY.
947 : */
948 9 : int score = pLevel->score;
949 9 : int nEqColumn = score/8;
950 : int start;
951 : int leFlag, geFlag;
952 : int testOp;
953 :
954 : /* Evaluate the equality constraints
955 : */
956 9 : for(j=0; j<nEqColumn; j++){
957 0 : for(k=0; k<nExpr; k++){
958 0 : if( aExpr[k].p==0 ) continue;
959 0 : if( aExpr[k].idxLeft==iCur
960 : && aExpr[k].p->op==TK_EQ
961 : && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
962 : && aExpr[k].p->pLeft->iColumn==pIdx->aiColumn[j]
963 : ){
964 0 : sqliteExprCode(pParse, aExpr[k].p->pRight);
965 0 : disableTerm(pLevel, &aExpr[k].p);
966 0 : break;
967 : }
968 0 : if( aExpr[k].idxRight==iCur
969 : && aExpr[k].p->op==TK_EQ
970 : && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
971 : && aExpr[k].p->pRight->iColumn==pIdx->aiColumn[j]
972 : ){
973 0 : sqliteExprCode(pParse, aExpr[k].p->pLeft);
974 0 : disableTerm(pLevel, &aExpr[k].p);
975 0 : break;
976 : }
977 : }
978 : }
979 :
980 : /* Duplicate the equality term values because they will all be
981 : ** used twice: once to make the termination key and once to make the
982 : ** start key.
983 : */
984 9 : for(j=0; j<nEqColumn; j++){
985 0 : sqliteVdbeAddOp(v, OP_Dup, nEqColumn-1, 0);
986 : }
987 :
988 : /* Labels for the beginning and end of the loop
989 : */
990 9 : cont = pLevel->cont = sqliteVdbeMakeLabel(v);
991 9 : brk = pLevel->brk = sqliteVdbeMakeLabel(v);
992 :
993 : /* Generate the termination key. This is the key value that
994 : ** will end the search. There is no termination key if there
995 : ** are no equality terms and no "X<..." term.
996 : **
997 : ** 2002-Dec-04: On a reverse-order scan, the so-called "termination"
998 : ** key computed here really ends up being the start key.
999 : */
1000 9 : if( (score & 1)!=0 ){
1001 0 : for(k=0; k<nExpr; k++){
1002 0 : Expr *pExpr = aExpr[k].p;
1003 0 : if( pExpr==0 ) continue;
1004 0 : if( aExpr[k].idxLeft==iCur
1005 : && (pExpr->op==TK_LT || pExpr->op==TK_LE)
1006 : && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
1007 : && pExpr->pLeft->iColumn==pIdx->aiColumn[j]
1008 : ){
1009 0 : sqliteExprCode(pParse, pExpr->pRight);
1010 0 : leFlag = pExpr->op==TK_LE;
1011 0 : disableTerm(pLevel, &aExpr[k].p);
1012 0 : break;
1013 : }
1014 0 : if( aExpr[k].idxRight==iCur
1015 : && (pExpr->op==TK_GT || pExpr->op==TK_GE)
1016 : && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
1017 : && pExpr->pRight->iColumn==pIdx->aiColumn[j]
1018 : ){
1019 0 : sqliteExprCode(pParse, pExpr->pLeft);
1020 0 : leFlag = pExpr->op==TK_GE;
1021 0 : disableTerm(pLevel, &aExpr[k].p);
1022 0 : break;
1023 : }
1024 : }
1025 0 : testOp = OP_IdxGE;
1026 : }else{
1027 9 : testOp = nEqColumn>0 ? OP_IdxGE : OP_Noop;
1028 9 : leFlag = 1;
1029 : }
1030 9 : if( testOp!=OP_Noop ){
1031 0 : int nCol = nEqColumn + (score & 1);
1032 0 : pLevel->iMem = pParse->nMem++;
1033 0 : sqliteVdbeAddOp(v, OP_NotNull, -nCol, sqliteVdbeCurrentAddr(v)+3);
1034 0 : sqliteVdbeAddOp(v, OP_Pop, nCol, 0);
1035 0 : sqliteVdbeAddOp(v, OP_Goto, 0, brk);
1036 0 : sqliteVdbeAddOp(v, OP_MakeKey, nCol, 0);
1037 0 : sqliteAddIdxKeyType(v, pIdx);
1038 0 : if( leFlag ){
1039 0 : sqliteVdbeAddOp(v, OP_IncrKey, 0, 0);
1040 : }
1041 0 : if( pLevel->bRev ){
1042 0 : sqliteVdbeAddOp(v, OP_MoveLt, pLevel->iCur, brk);
1043 : }else{
1044 0 : sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
1045 : }
1046 9 : }else if( pLevel->bRev ){
1047 0 : sqliteVdbeAddOp(v, OP_Last, pLevel->iCur, brk);
1048 : }
1049 :
1050 : /* Generate the start key. This is the key that defines the lower
1051 : ** bound on the search. There is no start key if there are no
1052 : ** equality terms and if there is no "X>..." term. In
1053 : ** that case, generate a "Rewind" instruction in place of the
1054 : ** start key search.
1055 : **
1056 : ** 2002-Dec-04: In the case of a reverse-order search, the so-called
1057 : ** "start" key really ends up being used as the termination key.
1058 : */
1059 9 : if( (score & 2)!=0 ){
1060 0 : for(k=0; k<nExpr; k++){
1061 0 : Expr *pExpr = aExpr[k].p;
1062 0 : if( pExpr==0 ) continue;
1063 0 : if( aExpr[k].idxLeft==iCur
1064 : && (pExpr->op==TK_GT || pExpr->op==TK_GE)
1065 : && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
1066 : && pExpr->pLeft->iColumn==pIdx->aiColumn[j]
1067 : ){
1068 0 : sqliteExprCode(pParse, pExpr->pRight);
1069 0 : geFlag = pExpr->op==TK_GE;
1070 0 : disableTerm(pLevel, &aExpr[k].p);
1071 0 : break;
1072 : }
1073 0 : if( aExpr[k].idxRight==iCur
1074 : && (pExpr->op==TK_LT || pExpr->op==TK_LE)
1075 : && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
1076 : && pExpr->pRight->iColumn==pIdx->aiColumn[j]
1077 : ){
1078 0 : sqliteExprCode(pParse, pExpr->pLeft);
1079 0 : geFlag = pExpr->op==TK_LE;
1080 0 : disableTerm(pLevel, &aExpr[k].p);
1081 0 : break;
1082 : }
1083 : }
1084 : }else{
1085 9 : geFlag = 1;
1086 : }
1087 9 : if( nEqColumn>0 || (score&2)!=0 ){
1088 0 : int nCol = nEqColumn + ((score&2)!=0);
1089 0 : sqliteVdbeAddOp(v, OP_NotNull, -nCol, sqliteVdbeCurrentAddr(v)+3);
1090 0 : sqliteVdbeAddOp(v, OP_Pop, nCol, 0);
1091 0 : sqliteVdbeAddOp(v, OP_Goto, 0, brk);
1092 0 : sqliteVdbeAddOp(v, OP_MakeKey, nCol, 0);
1093 0 : sqliteAddIdxKeyType(v, pIdx);
1094 0 : if( !geFlag ){
1095 0 : sqliteVdbeAddOp(v, OP_IncrKey, 0, 0);
1096 : }
1097 0 : if( pLevel->bRev ){
1098 0 : pLevel->iMem = pParse->nMem++;
1099 0 : sqliteVdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
1100 0 : testOp = OP_IdxLT;
1101 : }else{
1102 0 : sqliteVdbeAddOp(v, OP_MoveTo, pLevel->iCur, brk);
1103 : }
1104 9 : }else if( pLevel->bRev ){
1105 0 : testOp = OP_Noop;
1106 : }else{
1107 9 : sqliteVdbeAddOp(v, OP_Rewind, pLevel->iCur, brk);
1108 : }
1109 :
1110 : /* Generate the the top of the loop. If there is a termination
1111 : ** key we have to test for that key and abort at the top of the
1112 : ** loop.
1113 : */
1114 9 : start = sqliteVdbeCurrentAddr(v);
1115 9 : if( testOp!=OP_Noop ){
1116 0 : sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
1117 0 : sqliteVdbeAddOp(v, testOp, pLevel->iCur, brk);
1118 : }
1119 9 : sqliteVdbeAddOp(v, OP_RowKey, pLevel->iCur, 0);
1120 9 : sqliteVdbeAddOp(v, OP_IdxIsNull, nEqColumn + (score & 1), cont);
1121 9 : sqliteVdbeAddOp(v, OP_IdxRecno, pLevel->iCur, 0);
1122 9 : if( i==pTabList->nSrc-1 && pushKey ){
1123 0 : haveKey = 1;
1124 : }else{
1125 9 : sqliteVdbeAddOp(v, OP_MoveTo, iCur, 0);
1126 9 : haveKey = 0;
1127 : }
1128 :
1129 : /* Record the instruction used to terminate the loop.
1130 : */
1131 9 : pLevel->op = pLevel->bRev ? OP_Prev : OP_Next;
1132 9 : pLevel->p1 = pLevel->iCur;
1133 9 : pLevel->p2 = start;
1134 : }
1135 526 : loopMask |= getMask(&maskSet, iCur);
1136 :
1137 : /* Insert code to test every subexpression that can be completely
1138 : ** computed using the current set of tables.
1139 : */
1140 610 : for(j=0; j<nExpr; j++){
1141 84 : if( aExpr[j].p==0 ) continue;
1142 31 : if( (aExpr[j].prereqAll & loopMask)!=aExpr[j].prereqAll ) continue;
1143 20 : if( pLevel->iLeftJoin && !ExprHasProperty(aExpr[j].p,EP_FromJoin) ){
1144 3 : continue;
1145 : }
1146 17 : if( haveKey ){
1147 0 : haveKey = 0;
1148 0 : sqliteVdbeAddOp(v, OP_MoveTo, iCur, 0);
1149 : }
1150 17 : sqliteExprIfFalse(pParse, aExpr[j].p, cont, 1);
1151 17 : aExpr[j].p = 0;
1152 : }
1153 526 : brk = cont;
1154 :
1155 : /* For a LEFT OUTER JOIN, generate code that will record the fact that
1156 : ** at least one row of the right table has matched the left table.
1157 : */
1158 526 : if( pLevel->iLeftJoin ){
1159 8 : pLevel->top = sqliteVdbeCurrentAddr(v);
1160 8 : sqliteVdbeAddOp(v, OP_Integer, 1, 0);
1161 8 : sqliteVdbeAddOp(v, OP_MemStore, pLevel->iLeftJoin, 1);
1162 19 : for(j=0; j<nExpr; j++){
1163 11 : if( aExpr[j].p==0 ) continue;
1164 3 : if( (aExpr[j].prereqAll & loopMask)!=aExpr[j].prereqAll ) continue;
1165 3 : if( haveKey ){
1166 : /* Cannot happen. "haveKey" can only be true if pushKey is true
1167 : ** an pushKey can only be true for DELETE and UPDATE and there are
1168 : ** no outer joins with DELETE and UPDATE.
1169 : */
1170 0 : haveKey = 0;
1171 0 : sqliteVdbeAddOp(v, OP_MoveTo, iCur, 0);
1172 : }
1173 3 : sqliteExprIfFalse(pParse, aExpr[j].p, cont, 1);
1174 3 : aExpr[j].p = 0;
1175 : }
1176 : }
1177 : }
1178 524 : pWInfo->iContinue = cont;
1179 524 : if( pushKey && !haveKey ){
1180 2 : sqliteVdbeAddOp(v, OP_Recno, pTabList->a[0].iCursor, 0);
1181 : }
1182 : freeMaskSet(&maskSet);
1183 524 : return pWInfo;
1184 : }
1185 :
1186 : /*
1187 : ** Generate the end of the WHERE loop. See comments on
1188 : ** sqliteWhereBegin() for additional information.
1189 : */
1190 524 : void sqliteWhereEnd(WhereInfo *pWInfo){
1191 524 : Vdbe *v = pWInfo->pParse->pVdbe;
1192 : int i;
1193 : WhereLevel *pLevel;
1194 524 : SrcList *pTabList = pWInfo->pTabList;
1195 :
1196 1050 : for(i=pTabList->nSrc-1; i>=0; i--){
1197 526 : pLevel = &pWInfo->a[i];
1198 526 : sqliteVdbeResolveLabel(v, pLevel->cont);
1199 526 : if( pLevel->op!=OP_Noop ){
1200 514 : sqliteVdbeAddOp(v, pLevel->op, pLevel->p1, pLevel->p2);
1201 : }
1202 526 : sqliteVdbeResolveLabel(v, pLevel->brk);
1203 526 : if( pLevel->inOp!=OP_Noop ){
1204 0 : sqliteVdbeAddOp(v, pLevel->inOp, pLevel->inP1, pLevel->inP2);
1205 : }
1206 526 : if( pLevel->iLeftJoin ){
1207 : int addr;
1208 8 : addr = sqliteVdbeAddOp(v, OP_MemLoad, pLevel->iLeftJoin, 0);
1209 8 : sqliteVdbeAddOp(v, OP_NotNull, 1, addr+4 + (pLevel->iCur>=0));
1210 8 : sqliteVdbeAddOp(v, OP_NullRow, pTabList->a[i].iCursor, 0);
1211 8 : if( pLevel->iCur>=0 ){
1212 8 : sqliteVdbeAddOp(v, OP_NullRow, pLevel->iCur, 0);
1213 : }
1214 8 : sqliteVdbeAddOp(v, OP_Goto, 0, pLevel->top);
1215 : }
1216 : }
1217 524 : sqliteVdbeResolveLabel(v, pWInfo->iBreak);
1218 1050 : for(i=0; i<pTabList->nSrc; i++){
1219 526 : Table *pTab = pTabList->a[i].pTab;
1220 : assert( pTab!=0 );
1221 526 : if( pTab->isTransient || pTab->pSelect ) continue;
1222 526 : pLevel = &pWInfo->a[i];
1223 526 : sqliteVdbeAddOp(v, OP_Close, pTabList->a[i].iCursor, 0);
1224 526 : if( pLevel->pIdx!=0 ){
1225 48 : sqliteVdbeAddOp(v, OP_Close, pLevel->iCur, 0);
1226 : }
1227 : }
1228 : #if 0 /* Never reuse a cursor */
1229 : if( pWInfo->pParse->nTab==pWInfo->peakNTab ){
1230 : pWInfo->pParse->nTab = pWInfo->savedNTab;
1231 : }
1232 : #endif
1233 524 : sqliteFree(pWInfo);
1234 : return;
1235 : }
|