blob: d9cf7c07635bbcd88c28de6a30847b3cbe901a28 [file] [log] [blame]
drh75897232000-05-29 14:26:00 +00001/*
drhb19a2bc2001-09-16 00:13:26 +00002** 2001 September 15
drh75897232000-05-29 14:26:00 +00003**
drhb19a2bc2001-09-16 00:13:26 +00004** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
drh75897232000-05-29 14:26:00 +00006**
drhb19a2bc2001-09-16 00:13:26 +00007** 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.
drh75897232000-05-29 14:26:00 +000010**
11*************************************************************************
12** This module contains C code that generates VDBE code used to process
drh51669862004-12-18 18:40:26 +000013** the WHERE clause of SQL statements. This module is reponsible 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".
drh75897232000-05-29 14:26:00 +000018**
drh0fcef5e2005-07-19 17:38:22 +000019** $Id: where.c,v 1.146 2005/07/19 17:38:23 drh Exp $
drh75897232000-05-29 14:26:00 +000020*/
21#include "sqliteInt.h"
22
23/*
drh0aa74ed2005-07-16 13:33:20 +000024** The number of bits in a Bitmask. "BMS" means "BitMask Size".
25*/
26#define BMS (sizeof(Bitmask)*8-1)
27
28/*
29** Determine the number of elements in an array.
30*/
31#define ARRAYSIZE(X) (sizeof(X)/sizeof(X[0]))
32
drh0fcef5e2005-07-19 17:38:22 +000033/* Forward reference
34*/
35typedef struct WhereClause WhereClause;
drh0aa74ed2005-07-16 13:33:20 +000036
37/*
drh75897232000-05-29 14:26:00 +000038** The query generator uses an array of instances of this structure to
39** help it analyze the subexpressions of the WHERE clause. Each WHERE
40** clause subexpression is separated from the others by an AND operator.
drh51669862004-12-18 18:40:26 +000041**
drh0fcef5e2005-07-19 17:38:22 +000042** All WhereTerms are collected into a single WhereClause structure.
43** The following identity holds:
drh51669862004-12-18 18:40:26 +000044**
drh0fcef5e2005-07-19 17:38:22 +000045** WhereTerm.pWC->a[WhereTerm.idx] == WhereTerm
drh51669862004-12-18 18:40:26 +000046**
drh0fcef5e2005-07-19 17:38:22 +000047** When a term is of the form:
48**
49** X <op> <expr>
50**
51** where X is a column name and <op> is one of certain operators,
52** then WhereTerm.leftCursor and WhereTerm.leftColumn record the
53** cursor number and column number for X.
54**
55** prereqRight and prereqAll record sets of cursor numbers,
drh51669862004-12-18 18:40:26 +000056** but they do so indirectly. A single ExprMaskSet structure translates
57** cursor number into bits and the translated bit is stored in the prereq
58** fields. The translation is used in order to maximize the number of
59** bits that will fit in a Bitmask. The VDBE cursor numbers might be
60** spread out over the non-negative integers. For example, the cursor
61** numbers might be 3, 8, 9, 10, 20, 23, 41, and 45. The ExprMaskSet
62** translates these sparse cursor numbers into consecutive integers
63** beginning with 0 in order to make the best possible use of the available
64** bits in the Bitmask. So, in the example above, the cursor numbers
65** would be mapped into integers 0 through 7.
drh75897232000-05-29 14:26:00 +000066*/
drh0aa74ed2005-07-16 13:33:20 +000067typedef struct WhereTerm WhereTerm;
68struct WhereTerm {
drh0fcef5e2005-07-19 17:38:22 +000069 Expr *pExpr; /* Pointer to the subexpression */
70 u16 idx; /* Index of this term in pWC->a[] */
71 i16 iPartner; /* Disable pWC->a[iPartner] when this term disabled */
drh0aa74ed2005-07-16 13:33:20 +000072 u16 flags; /* Bit flags. See below */
drh0fcef5e2005-07-19 17:38:22 +000073 i16 leftCursor; /* Cursor number of X in "X <op> <expr>" */
74 i16 leftColumn; /* Column number of X in "X <op> <expr>" */
75 WhereClause *pWC; /* The clause this term is part of */
76 Bitmask prereqRight; /* Bitmask of tables used by pRight */
drh51669862004-12-18 18:40:26 +000077 Bitmask prereqAll; /* Bitmask of tables referenced by p */
drh75897232000-05-29 14:26:00 +000078};
79
80/*
drh0aa74ed2005-07-16 13:33:20 +000081** Allowed values of WhereTerm.flags
82*/
83#define TERM_DYNAMIC 0x0001 /* Need to call sqlite3ExprDelete(p) */
84#define TERM_VIRTUAL 0x0002 /* Added by the optimizer. Do not code */
drh0fcef5e2005-07-19 17:38:22 +000085#define TERM_CODED 0x0004 /* This term is already coded */
drh0aa74ed2005-07-16 13:33:20 +000086
87/*
88** An instance of the following structure holds all information about a
89** WHERE clause. Mostly this is a container for one or more WhereTerms.
90*/
drh0aa74ed2005-07-16 13:33:20 +000091struct WhereClause {
92 int nTerm; /* Number of terms */
93 int nSlot; /* Number of entries in a[] */
94 WhereTerm *a; /* Pointer to an array of terms */
95 WhereTerm aStatic[10]; /* Initial static space for the terms */
96};
97
98/*
drh6a3ea0e2003-05-02 14:32:12 +000099** An instance of the following structure keeps track of a mapping
drh0aa74ed2005-07-16 13:33:20 +0000100** between VDBE cursor numbers and bits of the bitmasks in WhereTerm.
drh51669862004-12-18 18:40:26 +0000101**
102** The VDBE cursor numbers are small integers contained in
103** SrcList_item.iCursor and Expr.iTable fields. For any given WHERE
104** clause, the cursor numbers might not begin with 0 and they might
105** contain gaps in the numbering sequence. But we want to make maximum
106** use of the bits in our bitmasks. This structure provides a mapping
107** from the sparse cursor numbers into consecutive integers beginning
108** with 0.
109**
110** If ExprMaskSet.ix[A]==B it means that The A-th bit of a Bitmask
111** corresponds VDBE cursor number B. The A-th bit of a bitmask is 1<<A.
112**
113** For example, if the WHERE clause expression used these VDBE
114** cursors: 4, 5, 8, 29, 57, 73. Then the ExprMaskSet structure
115** would map those cursor numbers into bits 0 through 5.
116**
117** Note that the mapping is not necessarily ordered. In the example
118** above, the mapping might go like this: 4->3, 5->1, 8->2, 29->0,
119** 57->5, 73->4. Or one of 719 other combinations might be used. It
120** does not really matter. What is important is that sparse cursor
121** numbers all get mapped into bit numbers that begin with 0 and contain
122** no gaps.
drh6a3ea0e2003-05-02 14:32:12 +0000123*/
124typedef struct ExprMaskSet ExprMaskSet;
125struct ExprMaskSet {
drh1398ad32005-01-19 23:24:50 +0000126 int n; /* Number of assigned cursor values */
127 int ix[sizeof(Bitmask)*8]; /* Cursor assigned to each bit */
drh6a3ea0e2003-05-02 14:32:12 +0000128};
129
drh0aa74ed2005-07-16 13:33:20 +0000130
drh6a3ea0e2003-05-02 14:32:12 +0000131/*
drh0aa74ed2005-07-16 13:33:20 +0000132** Initialize a preallocated WhereClause structure.
drh75897232000-05-29 14:26:00 +0000133*/
drh0aa74ed2005-07-16 13:33:20 +0000134static void whereClauseInit(WhereClause *pWC){
135 pWC->nTerm = 0;
136 pWC->nSlot = ARRAYSIZE(pWC->aStatic);
137 pWC->a = pWC->aStatic;
138}
139
140/*
141** Deallocate a WhereClause structure. The WhereClause structure
142** itself is not freed. This routine is the inverse of whereClauseInit().
143*/
144static void whereClauseClear(WhereClause *pWC){
145 int i;
146 WhereTerm *a;
147 for(i=pWC->nTerm-1, a=pWC->a; i>=0; i--, a++){
148 if( a->flags & TERM_DYNAMIC ){
drh0fcef5e2005-07-19 17:38:22 +0000149 sqlite3ExprDelete(a->pExpr);
drh0aa74ed2005-07-16 13:33:20 +0000150 }
151 }
152 if( pWC->a!=pWC->aStatic ){
153 sqliteFree(pWC->a);
154 }
155}
156
157/*
158** Add a new entries to the WhereClause structure. Increase the allocated
159** space as necessary.
160*/
drh0fcef5e2005-07-19 17:38:22 +0000161static WhereTerm *whereClauseInsert(WhereClause *pWC, Expr *p, int flags){
drh0aa74ed2005-07-16 13:33:20 +0000162 WhereTerm *pTerm;
163 if( pWC->nTerm>=pWC->nSlot ){
164 WhereTerm *pOld = pWC->a;
165 pWC->a = sqliteMalloc( sizeof(pWC->a[0])*pWC->nSlot*2 );
drh0fcef5e2005-07-19 17:38:22 +0000166 if( pWC->a==0 ) return 0;
drh0aa74ed2005-07-16 13:33:20 +0000167 memcpy(pWC->a, pOld, sizeof(pWC->a[0])*pWC->nTerm);
168 if( pOld!=pWC->aStatic ){
169 sqliteFree(pOld);
170 }
171 pWC->nSlot *= 2;
172 }
drh0fcef5e2005-07-19 17:38:22 +0000173 pTerm = &pWC->a[pWC->nTerm];
174 pTerm->idx = pWC->nTerm;
175 pWC->nTerm++;
176 pTerm->pExpr = p;
drh0aa74ed2005-07-16 13:33:20 +0000177 pTerm->flags = flags;
drh0fcef5e2005-07-19 17:38:22 +0000178 pTerm->pWC = pWC;
179 pTerm->iPartner = -1;
180 return pTerm;
drh0aa74ed2005-07-16 13:33:20 +0000181}
drh75897232000-05-29 14:26:00 +0000182
183/*
drh51669862004-12-18 18:40:26 +0000184** This routine identifies subexpressions in the WHERE clause where
185** each subexpression is separate by the AND operator. aSlot is
186** filled with pointers to the subexpressions. For example:
drh75897232000-05-29 14:26:00 +0000187**
drh51669862004-12-18 18:40:26 +0000188** WHERE a=='hello' AND coalesce(b,11)<10 AND (c+12!=d OR c==22)
189** \________/ \_______________/ \________________/
190** slot[0] slot[1] slot[2]
191**
192** The original WHERE clause in pExpr is unaltered. All this routine
193** does is make aSlot[] entries point to substructure within pExpr.
194**
195** aSlot[] is an array of subexpressions structures. There are nSlot
196** spaces left in this array. This routine finds as many AND-separated
197** subexpressions as it can and puts pointers to those subexpressions
198** into aSlot[] entries. The return value is the number of slots filled.
drh75897232000-05-29 14:26:00 +0000199*/
drh0aa74ed2005-07-16 13:33:20 +0000200static void whereSplit(WhereClause *pWC, Expr *pExpr){
201 if( pExpr==0 ) return;
202 if( pExpr->op!=TK_AND ){
203 whereClauseInsert(pWC, pExpr, 0);
drh75897232000-05-29 14:26:00 +0000204 }else{
drh0aa74ed2005-07-16 13:33:20 +0000205 whereSplit(pWC, pExpr->pLeft);
206 whereSplit(pWC, pExpr->pRight);
drh75897232000-05-29 14:26:00 +0000207 }
drh75897232000-05-29 14:26:00 +0000208}
209
210/*
drh6a3ea0e2003-05-02 14:32:12 +0000211** Initialize an expression mask set
212*/
213#define initMaskSet(P) memset(P, 0, sizeof(*P))
214
215/*
drh1398ad32005-01-19 23:24:50 +0000216** Return the bitmask for the given cursor number. Return 0 if
217** iCursor is not in the set.
drh6a3ea0e2003-05-02 14:32:12 +0000218*/
drh51669862004-12-18 18:40:26 +0000219static Bitmask getMask(ExprMaskSet *pMaskSet, int iCursor){
drh6a3ea0e2003-05-02 14:32:12 +0000220 int i;
221 for(i=0; i<pMaskSet->n; i++){
drh51669862004-12-18 18:40:26 +0000222 if( pMaskSet->ix[i]==iCursor ){
223 return ((Bitmask)1)<<i;
224 }
drh6a3ea0e2003-05-02 14:32:12 +0000225 }
drh6a3ea0e2003-05-02 14:32:12 +0000226 return 0;
227}
228
229/*
drh1398ad32005-01-19 23:24:50 +0000230** Create a new mask for cursor iCursor.
drh0fcef5e2005-07-19 17:38:22 +0000231**
232** There is one cursor per table in the FROM clause. The number of
233** tables in the FROM clause is limited by a test early in the
234** sqlite3WhereBegin() routien. So we know that the pMaskSet->ix[]
235** array will never overflow.
drh1398ad32005-01-19 23:24:50 +0000236*/
237static void createMask(ExprMaskSet *pMaskSet, int iCursor){
drh0fcef5e2005-07-19 17:38:22 +0000238 assert( pMaskSet->n < ARRAYSIZE(pMaskSet->ix) );
239 pMaskSet->ix[pMaskSet->n++] = iCursor;
drh1398ad32005-01-19 23:24:50 +0000240}
241
242/*
drh6a3ea0e2003-05-02 14:32:12 +0000243** Destroy an expression mask set
244*/
245#define freeMaskSet(P) /* NO-OP */
246
247/*
drh75897232000-05-29 14:26:00 +0000248** This routine walks (recursively) an expression tree and generates
249** a bitmask indicating which tables are used in that expression
drh6a3ea0e2003-05-02 14:32:12 +0000250** tree.
drh75897232000-05-29 14:26:00 +0000251**
252** In order for this routine to work, the calling function must have
drh626a8792005-01-17 22:08:19 +0000253** previously invoked sqlite3ExprResolveNames() on the expression. See
drh75897232000-05-29 14:26:00 +0000254** the header comment on that routine for additional information.
drh626a8792005-01-17 22:08:19 +0000255** The sqlite3ExprResolveNames() routines looks for column names and
drh6a3ea0e2003-05-02 14:32:12 +0000256** sets their opcodes to TK_COLUMN and their Expr.iTable fields to
257** the VDBE cursor number of the table.
drh75897232000-05-29 14:26:00 +0000258*/
danielk1977b3bce662005-01-29 08:32:43 +0000259static Bitmask exprListTableUsage(ExprMaskSet *, ExprList *);
drh51669862004-12-18 18:40:26 +0000260static Bitmask exprTableUsage(ExprMaskSet *pMaskSet, Expr *p){
261 Bitmask mask = 0;
drh75897232000-05-29 14:26:00 +0000262 if( p==0 ) return 0;
drh967e8b72000-06-21 13:59:10 +0000263 if( p->op==TK_COLUMN ){
drh8feb4b12004-07-19 02:12:14 +0000264 mask = getMask(pMaskSet, p->iTable);
drh8feb4b12004-07-19 02:12:14 +0000265 return mask;
drh75897232000-05-29 14:26:00 +0000266 }
danielk1977b3bce662005-01-29 08:32:43 +0000267 mask = exprTableUsage(pMaskSet, p->pRight);
268 mask |= exprTableUsage(pMaskSet, p->pLeft);
269 mask |= exprListTableUsage(pMaskSet, p->pList);
270 if( p->pSelect ){
271 Select *pS = p->pSelect;
272 mask |= exprListTableUsage(pMaskSet, pS->pEList);
273 mask |= exprListTableUsage(pMaskSet, pS->pGroupBy);
274 mask |= exprListTableUsage(pMaskSet, pS->pOrderBy);
275 mask |= exprTableUsage(pMaskSet, pS->pWhere);
276 mask |= exprTableUsage(pMaskSet, pS->pHaving);
drh75897232000-05-29 14:26:00 +0000277 }
danielk1977b3bce662005-01-29 08:32:43 +0000278 return mask;
279}
280static Bitmask exprListTableUsage(ExprMaskSet *pMaskSet, ExprList *pList){
281 int i;
282 Bitmask mask = 0;
283 if( pList ){
284 for(i=0; i<pList->nExpr; i++){
285 mask |= exprTableUsage(pMaskSet, pList->a[i].pExpr);
drhdd579122002-04-02 01:58:57 +0000286 }
287 }
drh75897232000-05-29 14:26:00 +0000288 return mask;
289}
290
291/*
drh487ab3c2001-11-08 00:45:21 +0000292** Return TRUE if the given operator is one of the operators that is
drh51669862004-12-18 18:40:26 +0000293** allowed for an indexable WHERE clause term. The allowed operators are
drhc27a1ce2002-06-14 20:58:45 +0000294** "=", "<", ">", "<=", ">=", and "IN".
drh487ab3c2001-11-08 00:45:21 +0000295*/
296static int allowedOp(int op){
drh9a432672004-10-04 13:38:09 +0000297 assert( TK_GT==TK_LE-1 && TK_LE==TK_LT-1 && TK_LT==TK_GE-1 && TK_EQ==TK_GT-1);
298 return op==TK_IN || (op>=TK_EQ && op<=TK_GE);
drh487ab3c2001-11-08 00:45:21 +0000299}
300
301/*
drh51669862004-12-18 18:40:26 +0000302** Swap two objects of type T.
drh193bd772004-07-20 18:23:14 +0000303*/
304#define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;}
305
306/*
drh0fcef5e2005-07-19 17:38:22 +0000307** Commute a comparision operator. Expressions of the form "X op Y"
308** are converted into "Y op X".
drh193bd772004-07-20 18:23:14 +0000309*/
drh0fcef5e2005-07-19 17:38:22 +0000310static void exprCommute(Expr *pExpr){
311 assert(
312 pExpr->op==TK_EQ ||
313 pExpr->op==TK_NE ||
314 pExpr->op==TK_LT ||
315 pExpr->op==TK_LE ||
316 pExpr->op==TK_GT ||
317 pExpr->op==TK_GE
318 );
319 SWAP(CollSeq*,pExpr->pRight->pColl,pExpr->pLeft->pColl);
320 SWAP(Expr*,pExpr->pRight,pExpr->pLeft);
321 if( pExpr->op>=TK_GT ){
322 assert( TK_LT==TK_GT+2 );
323 assert( TK_GE==TK_LE+2 );
324 assert( TK_GT>TK_EQ );
325 assert( TK_GT<TK_LE );
326 assert( pExpr->op>=TK_GT && pExpr->op<=TK_GE );
327 pExpr->op = ((pExpr->op-TK_GT)^2)+TK_GT;
drh193bd772004-07-20 18:23:14 +0000328 }
drh193bd772004-07-20 18:23:14 +0000329}
330
331/*
drh0aa74ed2005-07-16 13:33:20 +0000332** The input to this routine is an WhereTerm structure with only the
drh75897232000-05-29 14:26:00 +0000333** "p" field filled in. The job of this routine is to analyze the
drh0aa74ed2005-07-16 13:33:20 +0000334** subexpression and populate all the other fields of the WhereTerm
drh75897232000-05-29 14:26:00 +0000335** structure.
336*/
drh0fcef5e2005-07-19 17:38:22 +0000337static void exprAnalyze(
338 SrcList *pSrc, /* the FROM clause */
339 ExprMaskSet *pMaskSet, /* table masks */
340 WhereTerm *pTerm /* the WHERE clause term to be analyzed */
341){
342 Expr *pExpr = pTerm->pExpr;
343 Bitmask prereqLeft;
344 Bitmask prereqAll;
345 int idxRight;
346
347 prereqLeft = exprTableUsage(pMaskSet, pExpr->pLeft);
348 pTerm->prereqRight = exprTableUsage(pMaskSet, pExpr->pRight);
349 pTerm->prereqAll = prereqAll = exprTableUsage(pMaskSet, pExpr);
350 pTerm->leftCursor = -1;
351 pTerm->iPartner = -1;
352 idxRight = -1;
353 if( allowedOp(pExpr->op) && (pTerm->prereqRight & prereqLeft)==0 ){
354 Expr *pLeft = pExpr->pLeft;
355 Expr *pRight = pExpr->pRight;
356 if( pLeft->op==TK_COLUMN ){
357 pTerm->leftCursor = pLeft->iTable;
358 pTerm->leftColumn = pLeft->iColumn;
drh75897232000-05-29 14:26:00 +0000359 }
drh0fcef5e2005-07-19 17:38:22 +0000360 if( pRight && pRight->op==TK_COLUMN ){
361 WhereTerm *pNew;
362 Expr *pDup;
363 if( pTerm->leftCursor>=0 ){
364 pDup = sqlite3ExprDup(pExpr);
365 pNew = whereClauseInsert(pTerm->pWC, pDup, TERM_VIRTUAL|TERM_DYNAMIC);
366 if( pNew==0 ) return;
367 pNew->iPartner = pTerm->idx;
368 }else{
369 pDup = pExpr;
370 pNew = pTerm;
371 }
372 exprCommute(pDup);
373 pLeft = pDup->pLeft;
374 pNew->leftCursor = pLeft->iTable;
375 pNew->leftColumn = pLeft->iColumn;
376 pNew->prereqRight = prereqLeft;
377 pNew->prereqAll = prereqAll;
drh75897232000-05-29 14:26:00 +0000378 }
379 }
380}
381
drh0fcef5e2005-07-19 17:38:22 +0000382
drh75897232000-05-29 14:26:00 +0000383/*
drh51669862004-12-18 18:40:26 +0000384** This routine decides if pIdx can be used to satisfy the ORDER BY
385** clause. If it can, it returns 1. If pIdx cannot satisfy the
386** ORDER BY clause, this routine returns 0.
387**
388** pOrderBy is an ORDER BY clause from a SELECT statement. pTab is the
389** left-most table in the FROM clause of that same SELECT statement and
390** the table has a cursor number of "base". pIdx is an index on pTab.
391**
392** nEqCol is the number of columns of pIdx that are used as equality
393** constraints. Any of these columns may be missing from the ORDER BY
394** clause and the match can still be a success.
395**
396** If the index is UNIQUE, then the ORDER BY clause is allowed to have
397** additional terms past the end of the index and the match will still
398** be a success.
399**
400** All terms of the ORDER BY that match against the index must be either
401** ASC or DESC. (Terms of the ORDER BY clause past the end of a UNIQUE
402** index do not need to satisfy this constraint.) The *pbRev value is
403** set to 1 if the ORDER BY clause is all DESC and it is set to 0 if
404** the ORDER BY clause is all ASC.
405*/
406static int isSortingIndex(
407 Parse *pParse, /* Parsing context */
408 Index *pIdx, /* The index we are testing */
409 Table *pTab, /* The table to be sorted */
410 int base, /* Cursor number for pTab */
411 ExprList *pOrderBy, /* The ORDER BY clause */
412 int nEqCol, /* Number of index columns with == constraints */
413 int *pbRev /* Set to 1 if ORDER BY is DESC */
414){
415 int i, j; /* Loop counters */
416 int sortOrder; /* Which direction we are sorting */
417 int nTerm; /* Number of ORDER BY terms */
418 struct ExprList_item *pTerm; /* A term of the ORDER BY clause */
419 sqlite3 *db = pParse->db;
420
421 assert( pOrderBy!=0 );
422 nTerm = pOrderBy->nExpr;
423 assert( nTerm>0 );
424
425 /* Match terms of the ORDER BY clause against columns of
426 ** the index.
427 */
428 for(i=j=0, pTerm=pOrderBy->a; j<nTerm && i<pIdx->nColumn; i++){
429 Expr *pExpr; /* The expression of the ORDER BY pTerm */
430 CollSeq *pColl; /* The collating sequence of pExpr */
431
432 pExpr = pTerm->pExpr;
433 if( pExpr->op!=TK_COLUMN || pExpr->iTable!=base ){
434 /* Can not use an index sort on anything that is not a column in the
435 ** left-most table of the FROM clause */
436 return 0;
437 }
438 pColl = sqlite3ExprCollSeq(pParse, pExpr);
439 if( !pColl ) pColl = db->pDfltColl;
drh9012bcb2004-12-19 00:11:35 +0000440 if( pExpr->iColumn!=pIdx->aiColumn[i] || pColl!=pIdx->keyInfo.aColl[i] ){
441 /* Term j of the ORDER BY clause does not match column i of the index */
442 if( i<nEqCol ){
drh51669862004-12-18 18:40:26 +0000443 /* If an index column that is constrained by == fails to match an
444 ** ORDER BY term, that is OK. Just ignore that column of the index
445 */
446 continue;
447 }else{
448 /* If an index column fails to match and is not constrained by ==
449 ** then the index cannot satisfy the ORDER BY constraint.
450 */
451 return 0;
452 }
453 }
454 if( i>nEqCol ){
455 if( pTerm->sortOrder!=sortOrder ){
456 /* Indices can only be used if all ORDER BY terms past the
457 ** equality constraints are all either DESC or ASC. */
458 return 0;
459 }
460 }else{
461 sortOrder = pTerm->sortOrder;
462 }
463 j++;
464 pTerm++;
465 }
466
467 /* The index can be used for sorting if all terms of the ORDER BY clause
468 ** or covered or if we ran out of index columns and the it is a UNIQUE
469 ** index.
470 */
471 if( j>=nTerm || (i>=pIdx->nColumn && pIdx->onError!=OE_None) ){
472 *pbRev = sortOrder==SQLITE_SO_DESC;
473 return 1;
474 }
475 return 0;
476}
477
478/*
drhb6c29892004-11-22 19:12:19 +0000479** Check table to see if the ORDER BY clause in pOrderBy can be satisfied
480** by sorting in order of ROWID. Return true if so and set *pbRev to be
481** true for reverse ROWID and false for forward ROWID order.
482*/
483static int sortableByRowid(
484 int base, /* Cursor number for table to be sorted */
485 ExprList *pOrderBy, /* The ORDER BY clause */
486 int *pbRev /* Set to 1 if ORDER BY is DESC */
487){
488 Expr *p;
489
490 assert( pOrderBy!=0 );
491 assert( pOrderBy->nExpr>0 );
492 p = pOrderBy->a[0].pExpr;
493 if( p->op==TK_COLUMN && p->iTable==base && p->iColumn==-1 ){
494 *pbRev = pOrderBy->a[0].sortOrder;
495 return 1;
496 }
497 return 0;
498}
499
500
501/*
drh2ffb1182004-07-19 19:14:01 +0000502** Disable a term in the WHERE clause. Except, do not disable the term
503** if it controls a LEFT OUTER JOIN and it did not originate in the ON
504** or USING clause of that join.
505**
506** Consider the term t2.z='ok' in the following queries:
507**
508** (1) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x WHERE t2.z='ok'
509** (2) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x AND t2.z='ok'
510** (3) SELECT * FROM t1, t2 WHERE t1.a=t2.x AND t2.z='ok'
511**
drh23bf66d2004-12-14 03:34:34 +0000512** The t2.z='ok' is disabled in the in (2) because it originates
drh2ffb1182004-07-19 19:14:01 +0000513** in the ON clause. The term is disabled in (3) because it is not part
514** of a LEFT OUTER JOIN. In (1), the term is not disabled.
515**
516** Disabling a term causes that term to not be tested in the inner loop
517** of the join. Disabling is an optimization. We would get the correct
518** results if nothing were ever disabled, but joins might run a little
519** slower. The trick is to disable as much as we can without disabling
520** too much. If we disabled in (1), we'd get the wrong answer.
521** See ticket #813.
522*/
drh0fcef5e2005-07-19 17:38:22 +0000523static void disableTerm(WhereLevel *pLevel, WhereTerm *pTerm){
524 if( pTerm
525 && (pTerm->flags & TERM_CODED)==0
526 && (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_FromJoin))
527 ){
528 pTerm->flags |= TERM_CODED;
529 if( pTerm->iPartner>=0 ){
530 disableTerm(pLevel, &pTerm->pWC->a[pTerm->iPartner]);
531 }
drh2ffb1182004-07-19 19:14:01 +0000532 }
533}
534
535/*
drh94a11212004-09-25 13:12:14 +0000536** Generate code that builds a probe for an index. Details:
537**
538** * Check the top nColumn entries on the stack. If any
539** of those entries are NULL, jump immediately to brk,
540** which is the loop exit, since no index entry will match
541** if any part of the key is NULL.
542**
543** * Construct a probe entry from the top nColumn entries in
544** the stack with affinities appropriate for index pIdx.
545*/
546static void buildIndexProbe(Vdbe *v, int nColumn, int brk, Index *pIdx){
547 sqlite3VdbeAddOp(v, OP_NotNull, -nColumn, sqlite3VdbeCurrentAddr(v)+3);
548 sqlite3VdbeAddOp(v, OP_Pop, nColumn, 0);
549 sqlite3VdbeAddOp(v, OP_Goto, 0, brk);
550 sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, 0);
551 sqlite3IndexAffinityStr(v, pIdx);
552}
553
554/*
555** Generate code for an equality term of the WHERE clause. An equality
556** term can be either X=expr or X IN (...). pTerm is the X.
557*/
558static void codeEqualityTerm(
559 Parse *pParse, /* The parsing context */
drh0aa74ed2005-07-16 13:33:20 +0000560 WhereTerm *pTerm, /* The term of the WHERE clause to be coded */
drh94a11212004-09-25 13:12:14 +0000561 int brk, /* Jump here to abandon the loop */
562 WhereLevel *pLevel /* When level of the FROM clause we are working on */
563){
drh0fcef5e2005-07-19 17:38:22 +0000564 Expr *pX = pTerm->pExpr;
drh94a11212004-09-25 13:12:14 +0000565 if( pX->op!=TK_IN ){
566 assert( pX->op==TK_EQ );
567 sqlite3ExprCode(pParse, pX->pRight);
danielk1977b3bce662005-01-29 08:32:43 +0000568#ifndef SQLITE_OMIT_SUBQUERY
drh94a11212004-09-25 13:12:14 +0000569 }else{
danielk1977b3bce662005-01-29 08:32:43 +0000570 int iTab;
drh94a11212004-09-25 13:12:14 +0000571 Vdbe *v = pParse->pVdbe;
danielk1977b3bce662005-01-29 08:32:43 +0000572
573 sqlite3CodeSubselect(pParse, pX);
574 iTab = pX->iTable;
drh94a11212004-09-25 13:12:14 +0000575 sqlite3VdbeAddOp(v, OP_Rewind, iTab, brk);
danielk1977b3bce662005-01-29 08:32:43 +0000576 VdbeComment((v, "# %.*s", pX->span.n, pX->span.z));
drh9012bcb2004-12-19 00:11:35 +0000577 pLevel->inP2 = sqlite3VdbeAddOp(v, OP_Column, iTab, 0);
drh94a11212004-09-25 13:12:14 +0000578 pLevel->inOp = OP_Next;
579 pLevel->inP1 = iTab;
danielk1977b3bce662005-01-29 08:32:43 +0000580#endif
drh94a11212004-09-25 13:12:14 +0000581 }
drh0fcef5e2005-07-19 17:38:22 +0000582 disableTerm(pLevel, pTerm);
drh94a11212004-09-25 13:12:14 +0000583}
584
drh84bfda42005-07-15 13:05:21 +0000585#ifdef SQLITE_TEST
586/*
587** The following variable holds a text description of query plan generated
588** by the most recent call to sqlite3WhereBegin(). Each call to WhereBegin
589** overwrites the previous. This information is used for testing and
590** analysis only.
591*/
592char sqlite3_query_plan[BMS*2*40]; /* Text of the join */
593static int nQPlan = 0; /* Next free slow in _query_plan[] */
594
595#endif /* SQLITE_TEST */
596
597
drh94a11212004-09-25 13:12:14 +0000598
599/*
drhe3184742002-06-19 14:27:05 +0000600** Generate the beginning of the loop used for WHERE clause processing.
drhacf3b982005-01-03 01:27:18 +0000601** The return value is a pointer to an opaque structure that contains
drh75897232000-05-29 14:26:00 +0000602** information needed to terminate the loop. Later, the calling routine
danielk19774adee202004-05-08 08:23:19 +0000603** should invoke sqlite3WhereEnd() with the return value of this function
drh75897232000-05-29 14:26:00 +0000604** in order to complete the WHERE clause processing.
605**
606** If an error occurs, this routine returns NULL.
drhc27a1ce2002-06-14 20:58:45 +0000607**
608** The basic idea is to do a nested loop, one loop for each table in
609** the FROM clause of a select. (INSERT and UPDATE statements are the
610** same as a SELECT with only a single table in the FROM clause.) For
611** example, if the SQL is this:
612**
613** SELECT * FROM t1, t2, t3 WHERE ...;
614**
615** Then the code generated is conceptually like the following:
616**
617** foreach row1 in t1 do \ Code generated
danielk19774adee202004-05-08 08:23:19 +0000618** foreach row2 in t2 do |-- by sqlite3WhereBegin()
drhc27a1ce2002-06-14 20:58:45 +0000619** foreach row3 in t3 do /
620** ...
621** end \ Code generated
danielk19774adee202004-05-08 08:23:19 +0000622** end |-- by sqlite3WhereEnd()
drhc27a1ce2002-06-14 20:58:45 +0000623** end /
624**
625** There are Btree cursors associated with each table. t1 uses cursor
drh6a3ea0e2003-05-02 14:32:12 +0000626** number pTabList->a[0].iCursor. t2 uses the cursor pTabList->a[1].iCursor.
627** And so forth. This routine generates code to open those VDBE cursors
danielk19774adee202004-05-08 08:23:19 +0000628** and sqlite3WhereEnd() generates the code to close them.
drhc27a1ce2002-06-14 20:58:45 +0000629**
drhe6f85e72004-12-25 01:03:13 +0000630** The code that sqlite3WhereBegin() generates leaves the cursors named
631** in pTabList pointing at their appropriate entries. The [...] code
drhf0863fe2005-06-12 21:35:51 +0000632** can use OP_Column and OP_Rowid opcodes on these cursors to extract
drhe6f85e72004-12-25 01:03:13 +0000633** data from the various tables of the loop.
634**
drhc27a1ce2002-06-14 20:58:45 +0000635** If the WHERE clause is empty, the foreach loops must each scan their
636** entire tables. Thus a three-way join is an O(N^3) operation. But if
637** the tables have indices and there are terms in the WHERE clause that
638** refer to those indices, a complete table scan can be avoided and the
639** code will run much faster. Most of the work of this routine is checking
640** to see if there are indices that can be used to speed up the loop.
641**
642** Terms of the WHERE clause are also used to limit which rows actually
643** make it to the "..." in the middle of the loop. After each "foreach",
644** terms of the WHERE clause that use only terms in that loop and outer
645** loops are evaluated and if false a jump is made around all subsequent
646** inner loops (or around the "..." if the test occurs within the inner-
647** most loop)
648**
649** OUTER JOINS
650**
651** An outer join of tables t1 and t2 is conceptally coded as follows:
652**
653** foreach row1 in t1 do
654** flag = 0
655** foreach row2 in t2 do
656** start:
657** ...
658** flag = 1
659** end
drhe3184742002-06-19 14:27:05 +0000660** if flag==0 then
661** move the row2 cursor to a null row
662** goto start
663** fi
drhc27a1ce2002-06-14 20:58:45 +0000664** end
665**
drhe3184742002-06-19 14:27:05 +0000666** ORDER BY CLAUSE PROCESSING
667**
668** *ppOrderBy is a pointer to the ORDER BY clause of a SELECT statement,
669** if there is one. If there is no ORDER BY clause or if this routine
670** is called from an UPDATE or DELETE statement, then ppOrderBy is NULL.
671**
672** If an index can be used so that the natural output order of the table
673** scan is correct for the ORDER BY clause, then that index is used and
674** *ppOrderBy is set to NULL. This is an optimization that prevents an
675** unnecessary sort of the result set if an index appropriate for the
676** ORDER BY clause already exists.
677**
678** If the where clause loops cannot be arranged to provide the correct
679** output order, then the *ppOrderBy is unchanged.
drh75897232000-05-29 14:26:00 +0000680*/
danielk19774adee202004-05-08 08:23:19 +0000681WhereInfo *sqlite3WhereBegin(
danielk1977ed326d72004-11-16 15:50:19 +0000682 Parse *pParse, /* The parser context */
683 SrcList *pTabList, /* A list of all tables to be scanned */
684 Expr *pWhere, /* The WHERE clause */
drhf8db1bc2005-04-22 02:38:37 +0000685 ExprList **ppOrderBy /* An ORDER BY clause, or NULL */
drh75897232000-05-29 14:26:00 +0000686){
687 int i; /* Loop counter */
688 WhereInfo *pWInfo; /* Will become the return value of this function */
689 Vdbe *v = pParse->pVdbe; /* The virtual database engine */
drhd4f5ee22003-07-16 00:54:31 +0000690 int brk, cont = 0; /* Addresses used during code generation */
drh0fcef5e2005-07-19 17:38:22 +0000691 Bitmask loopMask; /* One bit cleared for each outer loop */
drh0aa74ed2005-07-16 13:33:20 +0000692 WhereTerm *pTerm; /* A single term in the WHERE clause */
693 ExprMaskSet maskSet; /* The expression mask set */
694 int iDirectEq[BMS]; /* Term of the form ROWID==X for the N-th table */
695 int iDirectLt[BMS]; /* Term of the form ROWID<X or ROWID<=X */
696 int iDirectGt[BMS]; /* Term of the form ROWID>X or ROWID>=X */
697 WhereClause wc; /* The WHERE clause is divided into these terms */
drh9012bcb2004-12-19 00:11:35 +0000698 struct SrcList_item *pTabItem; /* A single entry from pTabList */
699 WhereLevel *pLevel; /* A single level in the pWInfo list */
drh75897232000-05-29 14:26:00 +0000700
drh1398ad32005-01-19 23:24:50 +0000701 /* The number of terms in the FROM clause is limited by the number of
702 ** bits in a Bitmask
703 */
704 if( pTabList->nSrc>sizeof(Bitmask)*8 ){
705 sqlite3ErrorMsg(pParse, "at most %d tables in a join",
706 sizeof(Bitmask)*8);
707 return 0;
708 }
709
drh83dcb1a2002-06-28 01:02:38 +0000710 /* Split the WHERE clause into separate subexpressions where each
drh0aa74ed2005-07-16 13:33:20 +0000711 ** subexpression is separated by an AND operator. If the wc.a[]
drh83dcb1a2002-06-28 01:02:38 +0000712 ** array fills up, the last entry might point to an expression which
713 ** contains additional unfactored AND operators.
714 */
drh6a3ea0e2003-05-02 14:32:12 +0000715 initMaskSet(&maskSet);
drh0aa74ed2005-07-16 13:33:20 +0000716 whereClauseInit(&wc);
717 whereSplit(&wc, pWhere);
drh1398ad32005-01-19 23:24:50 +0000718
drh75897232000-05-29 14:26:00 +0000719 /* Allocate and initialize the WhereInfo structure that will become the
720 ** return value.
721 */
drhad3cab52002-05-24 02:04:32 +0000722 pWInfo = sqliteMalloc( sizeof(WhereInfo) + pTabList->nSrc*sizeof(WhereLevel));
danielk1977132872b2004-05-10 10:37:18 +0000723 if( sqlite3_malloc_failed ){
danielk1977d5d56522005-03-16 12:15:20 +0000724 sqliteFree(pWInfo); /* Avoid leaking memory when malloc fails */
drh0aa74ed2005-07-16 13:33:20 +0000725 whereClauseClear(&wc);
drh75897232000-05-29 14:26:00 +0000726 return 0;
727 }
728 pWInfo->pParse = pParse;
729 pWInfo->pTabList = pTabList;
danielk19774adee202004-05-08 08:23:19 +0000730 pWInfo->iBreak = sqlite3VdbeMakeLabel(v);
drh08192d52002-04-30 19:20:28 +0000731
732 /* Special case: a WHERE clause that is constant. Evaluate the
733 ** expression and either jump over all of the code or fall thru.
734 */
danielk19774adee202004-05-08 08:23:19 +0000735 if( pWhere && (pTabList->nSrc==0 || sqlite3ExprIsConstant(pWhere)) ){
736 sqlite3ExprIfFalse(pParse, pWhere, pWInfo->iBreak, 1);
drhdf199a22002-06-14 22:38:41 +0000737 pWhere = 0;
drh08192d52002-04-30 19:20:28 +0000738 }
drh75897232000-05-29 14:26:00 +0000739
drh75897232000-05-29 14:26:00 +0000740 /* Analyze all of the subexpressions.
741 */
drh1398ad32005-01-19 23:24:50 +0000742 for(i=0; i<pTabList->nSrc; i++){
743 createMask(&maskSet, pTabList->a[i].iCursor);
744 }
drh0fcef5e2005-07-19 17:38:22 +0000745 for(i=wc.nTerm-1; i>=0; i--){
746 exprAnalyze(pTabList, &maskSet, &wc.a[i]);
drh75897232000-05-29 14:26:00 +0000747 }
748
drh75897232000-05-29 14:26:00 +0000749 /* Figure out what index to use (if any) for each nested loop.
drh6b563442001-11-07 16:48:26 +0000750 ** Make pWInfo->a[i].pIdx point to the index to use for the i-th nested
drhad3cab52002-05-24 02:04:32 +0000751 ** loop where i==0 is the outer loop and i==pTabList->nSrc-1 is the inner
drh8aff1012001-12-22 14:49:24 +0000752 ** loop.
753 **
754 ** If terms exist that use the ROWID of any table, then set the
755 ** iDirectEq[], iDirectLt[], or iDirectGt[] elements for that table
756 ** to the index of the term containing the ROWID. We always prefer
757 ** to use a ROWID which can directly access a table rather than an
drh0a36c572002-02-18 22:49:59 +0000758 ** index which requires reading an index first to get the rowid then
759 ** doing a second read of the actual database table.
drh75897232000-05-29 14:26:00 +0000760 **
761 ** Actually, if there are more than 32 tables in the join, only the
drh0a36c572002-02-18 22:49:59 +0000762 ** first 32 tables are candidates for indices. This is (again) due
763 ** to the limit of 32 bits in an integer bitmask.
drh75897232000-05-29 14:26:00 +0000764 */
drh0fcef5e2005-07-19 17:38:22 +0000765 loopMask = ~(Bitmask)0;
drh9012bcb2004-12-19 00:11:35 +0000766 pTabItem = pTabList->a;
767 pLevel = pWInfo->a;
768 for(i=0; i<pTabList->nSrc && i<ARRAYSIZE(iDirectEq); i++,pTabItem++,pLevel++){
drhc4a3c772001-04-04 11:48:57 +0000769 int j;
drh9012bcb2004-12-19 00:11:35 +0000770 int iCur = pTabItem->iCursor; /* The cursor for this table */
drh51669862004-12-18 18:40:26 +0000771 Bitmask mask = getMask(&maskSet, iCur); /* Cursor mask for this table */
drh9012bcb2004-12-19 00:11:35 +0000772 Table *pTab = pTabItem->pTab;
drh75897232000-05-29 14:26:00 +0000773 Index *pIdx;
774 Index *pBestIdx = 0;
drh487ab3c2001-11-08 00:45:21 +0000775 int bestScore = 0;
drh51669862004-12-18 18:40:26 +0000776 int bestRev = 0;
drh75897232000-05-29 14:26:00 +0000777
drhc4a3c772001-04-04 11:48:57 +0000778 /* Check to see if there is an expression that uses only the
drh8aff1012001-12-22 14:49:24 +0000779 ** ROWID field of this table. For terms of the form ROWID==expr
780 ** set iDirectEq[i] to the index of the term. For terms of the
781 ** form ROWID<expr or ROWID<=expr set iDirectLt[i] to the term index.
782 ** For terms like ROWID>expr or ROWID>=expr set iDirectGt[i].
drh174b6192002-12-03 02:22:52 +0000783 **
784 ** (Added:) Treat ROWID IN expr like ROWID=expr.
drhc4a3c772001-04-04 11:48:57 +0000785 */
drh9012bcb2004-12-19 00:11:35 +0000786 pLevel->iIdxCur = -1;
drh8aff1012001-12-22 14:49:24 +0000787 iDirectEq[i] = -1;
788 iDirectLt[i] = -1;
789 iDirectGt[i] = -1;
drh0aa74ed2005-07-16 13:33:20 +0000790 for(pTerm=wc.a, j=0; j<wc.nTerm; j++, pTerm++){
drh0fcef5e2005-07-19 17:38:22 +0000791 if( pTerm->leftCursor==iCur && pTerm->leftColumn<0
792 && (pTerm->prereqRight & loopMask)==0 ){
793 switch( pTerm->pExpr->op ){
drhd99f7062002-06-08 23:25:08 +0000794 case TK_IN:
drh8aff1012001-12-22 14:49:24 +0000795 case TK_EQ: iDirectEq[i] = j; break;
796 case TK_LE:
797 case TK_LT: iDirectLt[i] = j; break;
798 case TK_GE:
799 case TK_GT: iDirectGt[i] = j; break;
800 }
drhc4a3c772001-04-04 11:48:57 +0000801 }
drhc4a3c772001-04-04 11:48:57 +0000802 }
drhb6c29892004-11-22 19:12:19 +0000803
804 /* If we found a term that tests ROWID with == or IN, that term
805 ** will be used to locate the rows in the database table. There
drh7bac7002005-07-01 11:38:44 +0000806 ** is no need to continue into the code below that looks for
drhb6c29892004-11-22 19:12:19 +0000807 ** an index. We will always use the ROWID over an index.
808 */
drh8aff1012001-12-22 14:49:24 +0000809 if( iDirectEq[i]>=0 ){
drh0fcef5e2005-07-19 17:38:22 +0000810 loopMask &= ~mask;
drh94a11212004-09-25 13:12:14 +0000811 pLevel->pIdx = 0;
drhc4a3c772001-04-04 11:48:57 +0000812 continue;
813 }
814
drh75897232000-05-29 14:26:00 +0000815 /* Do a search for usable indices. Leave pBestIdx pointing to
drh487ab3c2001-11-08 00:45:21 +0000816 ** the "best" index. pBestIdx is left set to NULL if no indices
817 ** are usable.
drh75897232000-05-29 14:26:00 +0000818 **
drhacf3b982005-01-03 01:27:18 +0000819 ** The best index is the one with the highest score. The score
820 ** for the index is determined as follows. For each of the
drh487ab3c2001-11-08 00:45:21 +0000821 ** left-most terms that is fixed by an equality operator, add
drh51669862004-12-18 18:40:26 +0000822 ** 32 to the score. The right-most term of the index may be
823 ** constrained by an inequality. Add 4 if for an "x<..." constraint
824 ** and add 8 for an "x>..." constraint. If both constraints
825 ** are present, add 12.
826 **
827 ** If the left-most term of the index uses an IN operator
828 ** (ex: "x IN (...)") then add 16 to the score.
829 **
830 ** If an index can be used for sorting, add 2 to the score.
831 ** If an index contains all the terms of a table that are ever
832 ** used by any expression in the SQL statement, then add 1 to
833 ** the score.
drh487ab3c2001-11-08 00:45:21 +0000834 **
835 ** This scoring system is designed so that the score can later be
drh51669862004-12-18 18:40:26 +0000836 ** used to determine how the index is used. If the score&0x1c is 0
837 ** then all constraints are equalities. If score&0x4 is not 0 then
drh487ab3c2001-11-08 00:45:21 +0000838 ** there is an inequality used as a termination key. (ex: "x<...")
drh51669862004-12-18 18:40:26 +0000839 ** If score&0x8 is not 0 then there is an inequality used as the
840 ** start key. (ex: "x>..."). A score or 0x10 is the special case
drhc045ec52002-12-04 20:01:06 +0000841 ** of an IN operator constraint. (ex: "x IN ...").
drhd99f7062002-06-08 23:25:08 +0000842 **
drhc27a1ce2002-06-14 20:58:45 +0000843 ** The IN operator (as in "<expr> IN (...)") is treated the same as
844 ** an equality comparison except that it can only be used on the
845 ** left-most column of an index and other terms of the WHERE clause
846 ** cannot be used in conjunction with the IN operator to help satisfy
847 ** other columns of the index.
drh75897232000-05-29 14:26:00 +0000848 */
849 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drh51669862004-12-18 18:40:26 +0000850 Bitmask eqMask = 0; /* Index columns covered by an x=... term */
851 Bitmask ltMask = 0; /* Index columns covered by an x<... term */
852 Bitmask gtMask = 0; /* Index columns covered by an x>... term */
853 Bitmask inMask = 0; /* Index columns covered by an x IN .. term */
854 Bitmask m;
855 int nEq, score, bRev = 0;
drh75897232000-05-29 14:26:00 +0000856
drh51669862004-12-18 18:40:26 +0000857 if( pIdx->nColumn>sizeof(eqMask)*8 ){
858 continue; /* Ignore indices with too many columns to analyze */
859 }
drh0aa74ed2005-07-16 13:33:20 +0000860 for(pTerm=wc.a, j=0; j<wc.nTerm; j++, pTerm++){
drh0fcef5e2005-07-19 17:38:22 +0000861 Expr *pX = pTerm->pExpr;
drh94a11212004-09-25 13:12:14 +0000862 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pX->pLeft);
drh193bd772004-07-20 18:23:14 +0000863 if( !pColl && pX->pRight ){
864 pColl = sqlite3ExprCollSeq(pParse, pX->pRight);
danielk19770202b292004-06-09 09:55:16 +0000865 }
866 if( !pColl ){
867 pColl = pParse->db->pDfltColl;
868 }
drh0fcef5e2005-07-19 17:38:22 +0000869 if( pTerm->leftCursor==iCur && (pTerm->prereqRight & loopMask)==0 ){
870 int iColumn = pTerm->leftColumn;
drh75897232000-05-29 14:26:00 +0000871 int k;
drhdd9f8b42005-05-19 01:26:14 +0000872 char idxaff = iColumn>=0 ? pIdx->pTable->aCol[iColumn].affinity : 0;
drh967e8b72000-06-21 13:59:10 +0000873 for(k=0; k<pIdx->nColumn; k++){
danielk19770202b292004-06-09 09:55:16 +0000874 /* If the collating sequences or affinities don't match,
875 ** ignore this index. */
876 if( pColl!=pIdx->keyInfo.aColl[k] ) continue;
drh193bd772004-07-20 18:23:14 +0000877 if( !sqlite3IndexAffinityOk(pX, idxaff) ) continue;
danielk19770202b292004-06-09 09:55:16 +0000878 if( pIdx->aiColumn[k]==iColumn ){
drh193bd772004-07-20 18:23:14 +0000879 switch( pX->op ){
drh48185c12002-06-09 01:55:20 +0000880 case TK_IN: {
881 if( k==0 ) inMask |= 1;
882 break;
883 }
drh487ab3c2001-11-08 00:45:21 +0000884 case TK_EQ: {
drh51669862004-12-18 18:40:26 +0000885 eqMask |= ((Bitmask)1)<<k;
drh487ab3c2001-11-08 00:45:21 +0000886 break;
887 }
888 case TK_LE:
889 case TK_LT: {
drh51669862004-12-18 18:40:26 +0000890 ltMask |= ((Bitmask)1)<<k;
drh487ab3c2001-11-08 00:45:21 +0000891 break;
892 }
893 case TK_GE:
894 case TK_GT: {
drh51669862004-12-18 18:40:26 +0000895 gtMask |= ((Bitmask)1)<<k;
drh487ab3c2001-11-08 00:45:21 +0000896 break;
897 }
898 default: {
899 /* CANT_HAPPEN */
900 assert( 0 );
901 break;
902 }
903 }
drh75897232000-05-29 14:26:00 +0000904 break;
905 }
906 }
907 }
drh75897232000-05-29 14:26:00 +0000908 }
drhc045ec52002-12-04 20:01:06 +0000909
910 /* The following loop ends with nEq set to the number of columns
911 ** on the left of the index with == constraints.
912 */
drh487ab3c2001-11-08 00:45:21 +0000913 for(nEq=0; nEq<pIdx->nColumn; nEq++){
drh51669862004-12-18 18:40:26 +0000914 m = (((Bitmask)1)<<(nEq+1))-1;
drh487ab3c2001-11-08 00:45:21 +0000915 if( (m & eqMask)!=m ) break;
916 }
drh51669862004-12-18 18:40:26 +0000917
drh7bac7002005-07-01 11:38:44 +0000918 /* Begin assembling the score
drh51669862004-12-18 18:40:26 +0000919 */
920 score = nEq*32; /* Base score is 32 times number of == constraints */
921 m = ((Bitmask)1)<<nEq;
922 if( m & ltMask ) score+=4; /* Increase score for a < constraint */
923 if( m & gtMask ) score+=8; /* Increase score for a > constraint */
924 if( score==0 && inMask ) score = 16; /* Default score for IN constraint */
925
926 /* Give bonus points if this index can be used for sorting
927 */
drh9012bcb2004-12-19 00:11:35 +0000928 if( i==0 && score!=16 && ppOrderBy && *ppOrderBy ){
drh51669862004-12-18 18:40:26 +0000929 int base = pTabList->a[0].iCursor;
930 if( isSortingIndex(pParse, pIdx, pTab, base, *ppOrderBy, nEq, &bRev) ){
931 score += 2;
932 }
933 }
934
drh9012bcb2004-12-19 00:11:35 +0000935 /* Check to see if we can get away with using just the index without
936 ** ever reading the table. If that is the case, then add one bonus
937 ** point to the score.
938 */
939 if( score && pTabItem->colUsed < (((Bitmask)1)<<(BMS-1)) ){
940 for(m=0, j=0; j<pIdx->nColumn; j++){
941 int x = pIdx->aiColumn[j];
942 if( x<BMS-1 ){
943 m |= ((Bitmask)1)<<x;
944 }
945 }
946 if( (pTabItem->colUsed & m)==pTabItem->colUsed ){
947 score++;
948 }
949 }
950
drh51669862004-12-18 18:40:26 +0000951 /* If the score for this index is the best we have seen so far, then
952 ** save it
953 */
drh487ab3c2001-11-08 00:45:21 +0000954 if( score>bestScore ){
955 pBestIdx = pIdx;
956 bestScore = score;
drh51669862004-12-18 18:40:26 +0000957 bestRev = bRev;
drh75897232000-05-29 14:26:00 +0000958 }
959 }
drh94a11212004-09-25 13:12:14 +0000960 pLevel->pIdx = pBestIdx;
961 pLevel->score = bestScore;
drh51669862004-12-18 18:40:26 +0000962 pLevel->bRev = bestRev;
drh0fcef5e2005-07-19 17:38:22 +0000963 loopMask &= ~mask;
drh6b563442001-11-07 16:48:26 +0000964 if( pBestIdx ){
drh9012bcb2004-12-19 00:11:35 +0000965 pLevel->iIdxCur = pParse->nTab++;
drh6b563442001-11-07 16:48:26 +0000966 }
drh75897232000-05-29 14:26:00 +0000967 }
968
drhe3184742002-06-19 14:27:05 +0000969 /* Check to see if the ORDER BY clause is or can be satisfied by the
970 ** use of an index on the first table.
971 */
972 if( ppOrderBy && *ppOrderBy && pTabList->nSrc>0 ){
drh9012bcb2004-12-19 00:11:35 +0000973 Index *pIdx; /* Index derived from the WHERE clause */
974 Table *pTab; /* Left-most table in the FROM clause */
975 int bRev = 0; /* True to reverse the output order */
976 int iCur; /* Btree-cursor that will be used by pTab */
977 WhereLevel *pLevel0 = &pWInfo->a[0];
drhe3184742002-06-19 14:27:05 +0000978
drh9012bcb2004-12-19 00:11:35 +0000979 pTab = pTabList->a[0].pTab;
980 pIdx = pLevel0->pIdx;
981 iCur = pTabList->a[0].iCursor;
982 if( pIdx==0 && sortableByRowid(iCur, *ppOrderBy, &bRev) ){
983 /* The ORDER BY clause specifies ROWID order, which is what we
984 ** were going to be doing anyway...
985 */
986 *ppOrderBy = 0;
987 pLevel0->bRev = bRev;
988 }else if( pLevel0->score==16 ){
989 /* If there is already an IN index on the left-most table,
990 ** it will not give the correct sort order.
991 ** So, pretend that no suitable index is found.
992 */
993 }else if( iDirectEq[0]>=0 || iDirectLt[0]>=0 || iDirectGt[0]>=0 ){
994 /* If the left-most column is accessed using its ROWID, then do
995 ** not try to sort by index. But do delete the ORDER BY clause
996 ** if it is redundant.
997 */
998 }else if( (pLevel0->score&2)!=0 ){
999 /* The index that was selected for searching will cause rows to
1000 ** appear in sorted order.
1001 */
1002 *ppOrderBy = 0;
drh75897232000-05-29 14:26:00 +00001003 }
1004 }
1005
drh9012bcb2004-12-19 00:11:35 +00001006 /* Open all tables in the pTabList and any indices selected for
1007 ** searching those tables.
1008 */
1009 sqlite3CodeVerifySchema(pParse, -1); /* Insert the cookie verifier Goto */
1010 pLevel = pWInfo->a;
1011 for(i=0, pTabItem=pTabList->a; i<pTabList->nSrc; i++, pTabItem++, pLevel++){
1012 Table *pTab;
1013 Index *pIx;
1014 int iIdxCur = pLevel->iIdxCur;
1015
1016 pTab = pTabItem->pTab;
1017 if( pTab->isTransient || pTab->pSelect ) continue;
1018 if( (pLevel->score & 1)==0 ){
1019 sqlite3OpenTableForReading(v, pTabItem->iCursor, pTab);
1020 }
1021 pLevel->iTabCur = pTabItem->iCursor;
1022 if( (pIx = pLevel->pIdx)!=0 ){
1023 sqlite3VdbeAddOp(v, OP_Integer, pIx->iDb, 0);
1024 sqlite3VdbeOp3(v, OP_OpenRead, iIdxCur, pIx->tnum,
1025 (char*)&pIx->keyInfo, P3_KEYINFO);
1026 }
1027 if( (pLevel->score & 1)!=0 ){
drh9012bcb2004-12-19 00:11:35 +00001028 sqlite3VdbeAddOp(v, OP_SetNumColumns, iIdxCur, pIx->nColumn+1);
1029 }
1030 sqlite3CodeVerifySchema(pParse, pTab->iDb);
drh84bfda42005-07-15 13:05:21 +00001031
1032#ifdef SQLITE_TEST
1033 /* Record in the query plan information about the current table
1034 ** and the index used to access it (if any). If the table itself
drh9042f392005-07-15 23:24:23 +00001035 ** is not used, its name is just '{}'. If no index is used
drh84bfda42005-07-15 13:05:21 +00001036 ** the index is listed as "{}"
1037 */
1038 {
drh9042f392005-07-15 23:24:23 +00001039 char *z = pTabItem->zAlias;
1040 int n;
1041 if( z==0 ) z = pTab->zName;
1042 n = strlen(z);
drh84bfda42005-07-15 13:05:21 +00001043 if( n+nQPlan < sizeof(sqlite3_query_plan)-10 ){
drh9042f392005-07-15 23:24:23 +00001044 if( (pLevel->score & 1)!=0 ){
1045 strcpy(&sqlite3_query_plan[nQPlan], "{}");
1046 nQPlan += 2;
1047 }else{
1048 strcpy(&sqlite3_query_plan[nQPlan], z);
1049 nQPlan += n;
drh84bfda42005-07-15 13:05:21 +00001050 }
1051 sqlite3_query_plan[nQPlan++] = ' ';
1052 }
1053 if( pIx==0 ){
1054 strcpy(&sqlite3_query_plan[nQPlan], " {}");
1055 nQPlan += 3;
1056 }else{
1057 n = strlen(pIx->zName);
1058 if( n+nQPlan < sizeof(sqlite3_query_plan)-2 ){
1059 strcpy(&sqlite3_query_plan[nQPlan], pIx->zName);
1060 nQPlan += n;
1061 sqlite3_query_plan[nQPlan++] = ' ';
1062 }
1063 }
1064 }
1065#endif
drh9012bcb2004-12-19 00:11:35 +00001066 }
1067 pWInfo->iTop = sqlite3VdbeCurrentAddr(v);
1068
drh84bfda42005-07-15 13:05:21 +00001069#ifdef SQLITE_TEST
1070 /* Terminate the query plan description
1071 */
1072 while( nQPlan>0 && sqlite3_query_plan[nQPlan-1]==' ' ){
1073 sqlite3_query_plan[--nQPlan] = 0;
1074 }
1075 sqlite3_query_plan[nQPlan] = 0;
1076 nQPlan = 0;
1077#endif
1078
drh75897232000-05-29 14:26:00 +00001079 /* Generate the code to do the search
1080 */
drh0fcef5e2005-07-19 17:38:22 +00001081 loopMask = ~(Bitmask)0;
drh9012bcb2004-12-19 00:11:35 +00001082 pLevel = pWInfo->a;
1083 pTabItem = pTabList->a;
1084 for(i=0; i<pTabList->nSrc; i++, pTabItem++, pLevel++){
drh75897232000-05-29 14:26:00 +00001085 int j, k;
drh9012bcb2004-12-19 00:11:35 +00001086 int iCur = pTabItem->iCursor; /* The VDBE cursor for the table */
1087 Index *pIdx; /* The index we will be using */
1088 int iIdxCur; /* The VDBE cursor for the index */
1089 int omitTable; /* True if we use the index only */
1090
1091 pIdx = pLevel->pIdx;
1092 iIdxCur = pLevel->iIdxCur;
1093 pLevel->inOp = OP_Noop;
1094
1095 /* Check to see if it is appropriate to omit the use of the table
1096 ** here and use its index instead.
1097 */
1098 omitTable = (pLevel->score&1)!=0;
drh75897232000-05-29 14:26:00 +00001099
drhad2d8302002-05-24 20:31:36 +00001100 /* If this is the right table of a LEFT OUTER JOIN, allocate and
drh174b6192002-12-03 02:22:52 +00001101 ** initialize a memory cell that records if this table matches any
drhc27a1ce2002-06-14 20:58:45 +00001102 ** row of the left table of the join.
drhad2d8302002-05-24 20:31:36 +00001103 */
1104 if( i>0 && (pTabList->a[i-1].jointype & JT_LEFT)!=0 ){
1105 if( !pParse->nMem ) pParse->nMem++;
1106 pLevel->iLeftJoin = pParse->nMem++;
drhf0863fe2005-06-12 21:35:51 +00001107 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001108 sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iLeftJoin, 1);
drhad6d9462004-09-19 02:15:24 +00001109 VdbeComment((v, "# init LEFT JOIN no-match flag"));
drhad2d8302002-05-24 20:31:36 +00001110 }
1111
drh94a11212004-09-25 13:12:14 +00001112 if( i<ARRAYSIZE(iDirectEq) && (k = iDirectEq[i])>=0 ){
drh8aff1012001-12-22 14:49:24 +00001113 /* Case 1: We can directly reference a single row using an
drhc27a1ce2002-06-14 20:58:45 +00001114 ** equality comparison against the ROWID field. Or
1115 ** we reference multiple rows using a "rowid IN (...)"
1116 ** construct.
drhc4a3c772001-04-04 11:48:57 +00001117 */
drh0aa74ed2005-07-16 13:33:20 +00001118 assert( k<wc.nTerm );
1119 pTerm = &wc.a[k];
drh0fcef5e2005-07-19 17:38:22 +00001120 assert( pTerm->pExpr!=0 );
1121 assert( pTerm->leftCursor==iCur );
drh9012bcb2004-12-19 00:11:35 +00001122 assert( omitTable==0 );
drh94a11212004-09-25 13:12:14 +00001123 brk = pLevel->brk = sqlite3VdbeMakeLabel(v);
1124 codeEqualityTerm(pParse, pTerm, brk, pLevel);
danielk19774adee202004-05-08 08:23:19 +00001125 cont = pLevel->cont = sqlite3VdbeMakeLabel(v);
1126 sqlite3VdbeAddOp(v, OP_MustBeInt, 1, brk);
danielk19774adee202004-05-08 08:23:19 +00001127 sqlite3VdbeAddOp(v, OP_NotExists, iCur, brk);
tpoindex7a9b1612005-01-03 18:13:18 +00001128 VdbeComment((v, "pk"));
drh6b563442001-11-07 16:48:26 +00001129 pLevel->op = OP_Noop;
drh9012bcb2004-12-19 00:11:35 +00001130 }else if( pIdx!=0 && pLevel->score>3 && (pLevel->score&0x0c)==0 ){
drhc27a1ce2002-06-14 20:58:45 +00001131 /* Case 2: There is an index and all terms of the WHERE clause that
drhb6c29892004-11-22 19:12:19 +00001132 ** refer to the index using the "==" or "IN" operators.
drh75897232000-05-29 14:26:00 +00001133 */
drh6b563442001-11-07 16:48:26 +00001134 int start;
drh51669862004-12-18 18:40:26 +00001135 int nColumn = (pLevel->score+16)/32;
danielk19774adee202004-05-08 08:23:19 +00001136 brk = pLevel->brk = sqlite3VdbeMakeLabel(v);
drh772ae622004-05-19 13:13:08 +00001137
1138 /* For each column of the index, find the term of the WHERE clause that
1139 ** constraints that column. If the WHERE clause term is X=expr, then
drh0aa74ed2005-07-16 13:33:20 +00001140 ** generate code to evaluate expr and leave the result on the stack */
drh487ab3c2001-11-08 00:45:21 +00001141 for(j=0; j<nColumn; j++){
drh0aa74ed2005-07-16 13:33:20 +00001142 for(pTerm=wc.a, k=0; k<wc.nTerm; k++, pTerm++){
drh0fcef5e2005-07-19 17:38:22 +00001143 Expr *pX = pTerm->pExpr;
1144 assert( pX );
1145 if( pTerm->leftCursor==iCur
1146 && (pTerm->prereqRight & loopMask)==0
1147 && pTerm->leftColumn==pIdx->aiColumn[j]
drhac931eb2005-01-11 18:13:56 +00001148 && (pX->op==TK_EQ || pX->op==TK_IN)
drh75897232000-05-29 14:26:00 +00001149 ){
drh0fcef5e2005-07-19 17:38:22 +00001150 char idxaff = pIdx->pTable->aCol[pTerm->leftColumn].affinity;
1151 assert( (pTerm->flags & TERM_CODED)==0 );
drh94a11212004-09-25 13:12:14 +00001152 if( sqlite3IndexAffinityOk(pX, idxaff) ){
1153 codeEqualityTerm(pParse, pTerm, brk, pLevel);
1154 break;
drhd99f7062002-06-08 23:25:08 +00001155 }
drh75897232000-05-29 14:26:00 +00001156 }
drh75897232000-05-29 14:26:00 +00001157 }
1158 }
drh6b563442001-11-07 16:48:26 +00001159 pLevel->iMem = pParse->nMem++;
danielk19774adee202004-05-08 08:23:19 +00001160 cont = pLevel->cont = sqlite3VdbeMakeLabel(v);
drh94a11212004-09-25 13:12:14 +00001161 buildIndexProbe(v, nColumn, brk, pIdx);
danielk19773d1bfea2004-05-14 11:00:53 +00001162 sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iMem, 0);
drh772ae622004-05-19 13:13:08 +00001163
drh772ae622004-05-19 13:13:08 +00001164 /* Generate code (1) to move to the first matching element of the table.
1165 ** Then generate code (2) that jumps to "brk" after the cursor is past
1166 ** the last matching element of the table. The code (1) is executed
1167 ** once to initialize the search, the code (2) is executed before each
1168 ** iteration of the scan to see if the scan has finished. */
drhc045ec52002-12-04 20:01:06 +00001169 if( pLevel->bRev ){
1170 /* Scan in reverse order */
drh9012bcb2004-12-19 00:11:35 +00001171 sqlite3VdbeAddOp(v, OP_MoveLe, iIdxCur, brk);
danielk19774adee202004-05-08 08:23:19 +00001172 start = sqlite3VdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
drh9012bcb2004-12-19 00:11:35 +00001173 sqlite3VdbeAddOp(v, OP_IdxLT, iIdxCur, brk);
drhc045ec52002-12-04 20:01:06 +00001174 pLevel->op = OP_Prev;
1175 }else{
1176 /* Scan in the forward order */
drh9012bcb2004-12-19 00:11:35 +00001177 sqlite3VdbeAddOp(v, OP_MoveGe, iIdxCur, brk);
danielk19774adee202004-05-08 08:23:19 +00001178 start = sqlite3VdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
drh9012bcb2004-12-19 00:11:35 +00001179 sqlite3VdbeOp3(v, OP_IdxGE, iIdxCur, brk, "+", P3_STATIC);
drhc045ec52002-12-04 20:01:06 +00001180 pLevel->op = OP_Next;
1181 }
drh9012bcb2004-12-19 00:11:35 +00001182 sqlite3VdbeAddOp(v, OP_RowKey, iIdxCur, 0);
danielk19774adee202004-05-08 08:23:19 +00001183 sqlite3VdbeAddOp(v, OP_IdxIsNull, nColumn, cont);
drhe6f85e72004-12-25 01:03:13 +00001184 if( !omitTable ){
drhf0863fe2005-06-12 21:35:51 +00001185 sqlite3VdbeAddOp(v, OP_IdxRowid, iIdxCur, 0);
drhe6f85e72004-12-25 01:03:13 +00001186 sqlite3VdbeAddOp(v, OP_MoveGe, iCur, 0);
drh75897232000-05-29 14:26:00 +00001187 }
drh9012bcb2004-12-19 00:11:35 +00001188 pLevel->p1 = iIdxCur;
drh6b563442001-11-07 16:48:26 +00001189 pLevel->p2 = start;
drh8aff1012001-12-22 14:49:24 +00001190 }else if( i<ARRAYSIZE(iDirectLt) && (iDirectLt[i]>=0 || iDirectGt[i]>=0) ){
1191 /* Case 3: We have an inequality comparison against the ROWID field.
1192 */
1193 int testOp = OP_Noop;
1194 int start;
drhb6c29892004-11-22 19:12:19 +00001195 int bRev = pLevel->bRev;
drh8aff1012001-12-22 14:49:24 +00001196
drh9012bcb2004-12-19 00:11:35 +00001197 assert( omitTable==0 );
danielk19774adee202004-05-08 08:23:19 +00001198 brk = pLevel->brk = sqlite3VdbeMakeLabel(v);
1199 cont = pLevel->cont = sqlite3VdbeMakeLabel(v);
drhb6c29892004-11-22 19:12:19 +00001200 if( bRev ){
1201 int t = iDirectGt[i];
1202 iDirectGt[i] = iDirectLt[i];
1203 iDirectLt[i] = t;
1204 }
drh8aff1012001-12-22 14:49:24 +00001205 if( iDirectGt[i]>=0 ){
drh94a11212004-09-25 13:12:14 +00001206 Expr *pX;
drh8aff1012001-12-22 14:49:24 +00001207 k = iDirectGt[i];
drh0aa74ed2005-07-16 13:33:20 +00001208 assert( k<wc.nTerm );
1209 pTerm = &wc.a[k];
drh0fcef5e2005-07-19 17:38:22 +00001210 pX = pTerm->pExpr;
drh94a11212004-09-25 13:12:14 +00001211 assert( pX!=0 );
drh0fcef5e2005-07-19 17:38:22 +00001212 assert( pTerm->leftCursor==iCur );
drh94a11212004-09-25 13:12:14 +00001213 sqlite3ExprCode(pParse, pX->pRight);
danielk1977d0a69322005-02-02 01:10:44 +00001214 sqlite3VdbeAddOp(v, OP_ForceInt, pX->op==TK_LE || pX->op==TK_GT, brk);
drhb6c29892004-11-22 19:12:19 +00001215 sqlite3VdbeAddOp(v, bRev ? OP_MoveLt : OP_MoveGe, iCur, brk);
tpoindex7a9b1612005-01-03 18:13:18 +00001216 VdbeComment((v, "pk"));
drh0fcef5e2005-07-19 17:38:22 +00001217 disableTerm(pLevel, pTerm);
drh8aff1012001-12-22 14:49:24 +00001218 }else{
drhb6c29892004-11-22 19:12:19 +00001219 sqlite3VdbeAddOp(v, bRev ? OP_Last : OP_Rewind, iCur, brk);
drh8aff1012001-12-22 14:49:24 +00001220 }
1221 if( iDirectLt[i]>=0 ){
drh94a11212004-09-25 13:12:14 +00001222 Expr *pX;
drh8aff1012001-12-22 14:49:24 +00001223 k = iDirectLt[i];
drh0aa74ed2005-07-16 13:33:20 +00001224 assert( k<wc.nTerm );
1225 pTerm = &wc.a[k];
drh0fcef5e2005-07-19 17:38:22 +00001226 pX = pTerm->pExpr;
drh94a11212004-09-25 13:12:14 +00001227 assert( pX!=0 );
drh0fcef5e2005-07-19 17:38:22 +00001228 assert( pTerm->leftCursor==iCur );
drh94a11212004-09-25 13:12:14 +00001229 sqlite3ExprCode(pParse, pX->pRight);
drh8aff1012001-12-22 14:49:24 +00001230 pLevel->iMem = pParse->nMem++;
danielk19774adee202004-05-08 08:23:19 +00001231 sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
drh94a11212004-09-25 13:12:14 +00001232 if( pX->op==TK_LT || pX->op==TK_GT ){
drhb6c29892004-11-22 19:12:19 +00001233 testOp = bRev ? OP_Le : OP_Ge;
drh8aff1012001-12-22 14:49:24 +00001234 }else{
drhb6c29892004-11-22 19:12:19 +00001235 testOp = bRev ? OP_Lt : OP_Gt;
drh8aff1012001-12-22 14:49:24 +00001236 }
drh0fcef5e2005-07-19 17:38:22 +00001237 disableTerm(pLevel, pTerm);
drh8aff1012001-12-22 14:49:24 +00001238 }
danielk19774adee202004-05-08 08:23:19 +00001239 start = sqlite3VdbeCurrentAddr(v);
drhb6c29892004-11-22 19:12:19 +00001240 pLevel->op = bRev ? OP_Prev : OP_Next;
drh6a3ea0e2003-05-02 14:32:12 +00001241 pLevel->p1 = iCur;
drh8aff1012001-12-22 14:49:24 +00001242 pLevel->p2 = start;
1243 if( testOp!=OP_Noop ){
drhf0863fe2005-06-12 21:35:51 +00001244 sqlite3VdbeAddOp(v, OP_Rowid, iCur, 0);
danielk19774adee202004-05-08 08:23:19 +00001245 sqlite3VdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
drhf0863fe2005-06-12 21:35:51 +00001246 sqlite3VdbeAddOp(v, testOp, 'n', brk);
drh8aff1012001-12-22 14:49:24 +00001247 }
drh8aff1012001-12-22 14:49:24 +00001248 }else if( pIdx==0 ){
drhc27a1ce2002-06-14 20:58:45 +00001249 /* Case 4: There is no usable index. We must do a complete
drh8aff1012001-12-22 14:49:24 +00001250 ** scan of the entire database table.
1251 */
1252 int start;
drhb6c29892004-11-22 19:12:19 +00001253 int opRewind;
drh8aff1012001-12-22 14:49:24 +00001254
drh9012bcb2004-12-19 00:11:35 +00001255 assert( omitTable==0 );
danielk19774adee202004-05-08 08:23:19 +00001256 brk = pLevel->brk = sqlite3VdbeMakeLabel(v);
1257 cont = pLevel->cont = sqlite3VdbeMakeLabel(v);
drhb6c29892004-11-22 19:12:19 +00001258 if( pLevel->bRev ){
1259 opRewind = OP_Last;
1260 pLevel->op = OP_Prev;
1261 }else{
1262 opRewind = OP_Rewind;
1263 pLevel->op = OP_Next;
1264 }
1265 sqlite3VdbeAddOp(v, opRewind, iCur, brk);
danielk19774adee202004-05-08 08:23:19 +00001266 start = sqlite3VdbeCurrentAddr(v);
drh6a3ea0e2003-05-02 14:32:12 +00001267 pLevel->p1 = iCur;
drh8aff1012001-12-22 14:49:24 +00001268 pLevel->p2 = start;
drh487ab3c2001-11-08 00:45:21 +00001269 }else{
drhc27a1ce2002-06-14 20:58:45 +00001270 /* Case 5: The WHERE clause term that refers to the right-most
1271 ** column of the index is an inequality. For example, if
1272 ** the index is on (x,y,z) and the WHERE clause is of the
1273 ** form "x=5 AND y<10" then this case is used. Only the
1274 ** right-most column can be an inequality - the rest must
1275 ** use the "==" operator.
drhe3184742002-06-19 14:27:05 +00001276 **
1277 ** This case is also used when there are no WHERE clause
1278 ** constraints but an index is selected anyway, in order
1279 ** to force the output order to conform to an ORDER BY.
drh487ab3c2001-11-08 00:45:21 +00001280 */
1281 int score = pLevel->score;
drh51669862004-12-18 18:40:26 +00001282 int nEqColumn = score/32;
drh487ab3c2001-11-08 00:45:21 +00001283 int start;
danielk1977f7df9cc2004-06-16 12:02:47 +00001284 int leFlag=0, geFlag=0;
drh487ab3c2001-11-08 00:45:21 +00001285 int testOp;
1286
1287 /* Evaluate the equality constraints
1288 */
1289 for(j=0; j<nEqColumn; j++){
drh94a11212004-09-25 13:12:14 +00001290 int iIdxCol = pIdx->aiColumn[j];
drh0aa74ed2005-07-16 13:33:20 +00001291 for(pTerm=wc.a, k=0; k<wc.nTerm; k++, pTerm++){
drh0fcef5e2005-07-19 17:38:22 +00001292 Expr *pX;
1293 if( pTerm->leftCursor==iCur
1294 && pTerm->leftColumn==iIdxCol
1295 && (pX = pTerm->pExpr)->op==TK_EQ
1296 && (pTerm->prereqRight & loopMask)==0
drh487ab3c2001-11-08 00:45:21 +00001297 ){
drh0fcef5e2005-07-19 17:38:22 +00001298 assert( (pTerm->flags & TERM_CODED)==0 );
drh94a11212004-09-25 13:12:14 +00001299 sqlite3ExprCode(pParse, pX->pRight);
drh0fcef5e2005-07-19 17:38:22 +00001300 disableTerm(pLevel, pTerm);
drh487ab3c2001-11-08 00:45:21 +00001301 break;
1302 }
1303 }
drh0fcef5e2005-07-19 17:38:22 +00001304 assert( k<wc.nTerm );
drh487ab3c2001-11-08 00:45:21 +00001305 }
1306
drhc27a1ce2002-06-14 20:58:45 +00001307 /* Duplicate the equality term values because they will all be
drh487ab3c2001-11-08 00:45:21 +00001308 ** used twice: once to make the termination key and once to make the
1309 ** start key.
1310 */
1311 for(j=0; j<nEqColumn; j++){
danielk19774adee202004-05-08 08:23:19 +00001312 sqlite3VdbeAddOp(v, OP_Dup, nEqColumn-1, 0);
drh487ab3c2001-11-08 00:45:21 +00001313 }
1314
drhc045ec52002-12-04 20:01:06 +00001315 /* Labels for the beginning and end of the loop
1316 */
danielk19774adee202004-05-08 08:23:19 +00001317 cont = pLevel->cont = sqlite3VdbeMakeLabel(v);
1318 brk = pLevel->brk = sqlite3VdbeMakeLabel(v);
drhc045ec52002-12-04 20:01:06 +00001319
drh487ab3c2001-11-08 00:45:21 +00001320 /* Generate the termination key. This is the key value that
1321 ** will end the search. There is no termination key if there
drhc27a1ce2002-06-14 20:58:45 +00001322 ** are no equality terms and no "X<..." term.
drhc045ec52002-12-04 20:01:06 +00001323 **
1324 ** 2002-Dec-04: On a reverse-order scan, the so-called "termination"
1325 ** key computed here really ends up being the start key.
drh487ab3c2001-11-08 00:45:21 +00001326 */
drh51669862004-12-18 18:40:26 +00001327 if( (score & 4)!=0 ){
drh0aa74ed2005-07-16 13:33:20 +00001328 for(pTerm=wc.a, k=0; k<wc.nTerm; k++, pTerm++){
drh0fcef5e2005-07-19 17:38:22 +00001329 Expr *pX = pTerm->pExpr;
1330 assert( pX );
1331 if( pTerm->leftCursor==iCur
drh94a11212004-09-25 13:12:14 +00001332 && (pX->op==TK_LT || pX->op==TK_LE)
drh0fcef5e2005-07-19 17:38:22 +00001333 && (pTerm->prereqRight & loopMask)==0
1334 && pTerm->leftColumn==pIdx->aiColumn[j]
drh487ab3c2001-11-08 00:45:21 +00001335 ){
drh0fcef5e2005-07-19 17:38:22 +00001336 assert( (pTerm->flags & TERM_CODED)==0 );
drh94a11212004-09-25 13:12:14 +00001337 sqlite3ExprCode(pParse, pX->pRight);
1338 leFlag = pX->op==TK_LE;
drh0fcef5e2005-07-19 17:38:22 +00001339 disableTerm(pLevel, pTerm);
drh487ab3c2001-11-08 00:45:21 +00001340 break;
1341 }
1342 }
drh0fcef5e2005-07-19 17:38:22 +00001343 assert( k<wc.nTerm );
drh487ab3c2001-11-08 00:45:21 +00001344 testOp = OP_IdxGE;
1345 }else{
1346 testOp = nEqColumn>0 ? OP_IdxGE : OP_Noop;
1347 leFlag = 1;
1348 }
1349 if( testOp!=OP_Noop ){
drh51669862004-12-18 18:40:26 +00001350 int nCol = nEqColumn + ((score & 4)!=0);
drh487ab3c2001-11-08 00:45:21 +00001351 pLevel->iMem = pParse->nMem++;
drh94a11212004-09-25 13:12:14 +00001352 buildIndexProbe(v, nCol, brk, pIdx);
drhc045ec52002-12-04 20:01:06 +00001353 if( pLevel->bRev ){
drh7cf6e4d2004-05-19 14:56:55 +00001354 int op = leFlag ? OP_MoveLe : OP_MoveLt;
drh9012bcb2004-12-19 00:11:35 +00001355 sqlite3VdbeAddOp(v, op, iIdxCur, brk);
drhc045ec52002-12-04 20:01:06 +00001356 }else{
danielk19774adee202004-05-08 08:23:19 +00001357 sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
drhc045ec52002-12-04 20:01:06 +00001358 }
1359 }else if( pLevel->bRev ){
drh9012bcb2004-12-19 00:11:35 +00001360 sqlite3VdbeAddOp(v, OP_Last, iIdxCur, brk);
drh487ab3c2001-11-08 00:45:21 +00001361 }
1362
1363 /* Generate the start key. This is the key that defines the lower
drhc27a1ce2002-06-14 20:58:45 +00001364 ** bound on the search. There is no start key if there are no
1365 ** equality terms and if there is no "X>..." term. In
drh487ab3c2001-11-08 00:45:21 +00001366 ** that case, generate a "Rewind" instruction in place of the
1367 ** start key search.
drhc045ec52002-12-04 20:01:06 +00001368 **
1369 ** 2002-Dec-04: In the case of a reverse-order search, the so-called
1370 ** "start" key really ends up being used as the termination key.
drh487ab3c2001-11-08 00:45:21 +00001371 */
drh51669862004-12-18 18:40:26 +00001372 if( (score & 8)!=0 ){
drh0aa74ed2005-07-16 13:33:20 +00001373 for(pTerm=wc.a, k=0; k<wc.nTerm; k++, pTerm++){
drh0fcef5e2005-07-19 17:38:22 +00001374 Expr *pX = pTerm->pExpr;
1375 assert( pX );
1376 if( pTerm->leftCursor==iCur
drh94a11212004-09-25 13:12:14 +00001377 && (pX->op==TK_GT || pX->op==TK_GE)
drh0fcef5e2005-07-19 17:38:22 +00001378 && (pTerm->prereqRight & loopMask)==0
1379 && pTerm->leftColumn==pIdx->aiColumn[j]
drh487ab3c2001-11-08 00:45:21 +00001380 ){
drh0fcef5e2005-07-19 17:38:22 +00001381 assert( (pTerm->flags & TERM_CODED)==0 );
drh94a11212004-09-25 13:12:14 +00001382 sqlite3ExprCode(pParse, pX->pRight);
1383 geFlag = pX->op==TK_GE;
drh0fcef5e2005-07-19 17:38:22 +00001384 disableTerm(pLevel, pTerm);
drh487ab3c2001-11-08 00:45:21 +00001385 break;
1386 }
1387 }
drh7900ead2001-11-12 13:51:43 +00001388 }else{
1389 geFlag = 1;
drh487ab3c2001-11-08 00:45:21 +00001390 }
drh51669862004-12-18 18:40:26 +00001391 if( nEqColumn>0 || (score&8)!=0 ){
1392 int nCol = nEqColumn + ((score&8)!=0);
drh94a11212004-09-25 13:12:14 +00001393 buildIndexProbe(v, nCol, brk, pIdx);
drhc045ec52002-12-04 20:01:06 +00001394 if( pLevel->bRev ){
1395 pLevel->iMem = pParse->nMem++;
danielk19774adee202004-05-08 08:23:19 +00001396 sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
drhc045ec52002-12-04 20:01:06 +00001397 testOp = OP_IdxLT;
1398 }else{
drh7cf6e4d2004-05-19 14:56:55 +00001399 int op = geFlag ? OP_MoveGe : OP_MoveGt;
drh9012bcb2004-12-19 00:11:35 +00001400 sqlite3VdbeAddOp(v, op, iIdxCur, brk);
drhc045ec52002-12-04 20:01:06 +00001401 }
1402 }else if( pLevel->bRev ){
1403 testOp = OP_Noop;
drh487ab3c2001-11-08 00:45:21 +00001404 }else{
drh9012bcb2004-12-19 00:11:35 +00001405 sqlite3VdbeAddOp(v, OP_Rewind, iIdxCur, brk);
drh487ab3c2001-11-08 00:45:21 +00001406 }
1407
1408 /* Generate the the top of the loop. If there is a termination
1409 ** key we have to test for that key and abort at the top of the
1410 ** loop.
1411 */
danielk19774adee202004-05-08 08:23:19 +00001412 start = sqlite3VdbeCurrentAddr(v);
drh487ab3c2001-11-08 00:45:21 +00001413 if( testOp!=OP_Noop ){
danielk19774adee202004-05-08 08:23:19 +00001414 sqlite3VdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
drh9012bcb2004-12-19 00:11:35 +00001415 sqlite3VdbeAddOp(v, testOp, iIdxCur, brk);
danielk19773d1bfea2004-05-14 11:00:53 +00001416 if( (leFlag && !pLevel->bRev) || (!geFlag && pLevel->bRev) ){
1417 sqlite3VdbeChangeP3(v, -1, "+", P3_STATIC);
1418 }
drh487ab3c2001-11-08 00:45:21 +00001419 }
drh9012bcb2004-12-19 00:11:35 +00001420 sqlite3VdbeAddOp(v, OP_RowKey, iIdxCur, 0);
drh51669862004-12-18 18:40:26 +00001421 sqlite3VdbeAddOp(v, OP_IdxIsNull, nEqColumn + ((score&4)!=0), cont);
drhe6f85e72004-12-25 01:03:13 +00001422 if( !omitTable ){
drhf0863fe2005-06-12 21:35:51 +00001423 sqlite3VdbeAddOp(v, OP_IdxRowid, iIdxCur, 0);
drhe6f85e72004-12-25 01:03:13 +00001424 sqlite3VdbeAddOp(v, OP_MoveGe, iCur, 0);
drh487ab3c2001-11-08 00:45:21 +00001425 }
1426
1427 /* Record the instruction used to terminate the loop.
1428 */
drhc045ec52002-12-04 20:01:06 +00001429 pLevel->op = pLevel->bRev ? OP_Prev : OP_Next;
drh9012bcb2004-12-19 00:11:35 +00001430 pLevel->p1 = iIdxCur;
drh487ab3c2001-11-08 00:45:21 +00001431 pLevel->p2 = start;
drh75897232000-05-29 14:26:00 +00001432 }
drh0fcef5e2005-07-19 17:38:22 +00001433 loopMask &= ~getMask(&maskSet, iCur);
drh75897232000-05-29 14:26:00 +00001434
1435 /* Insert code to test every subexpression that can be completely
1436 ** computed using the current set of tables.
1437 */
drh0fcef5e2005-07-19 17:38:22 +00001438 for(pTerm=wc.a, j=wc.nTerm; j>0; j--, pTerm++){
1439 Expr *pE;
1440 if( pTerm->flags & (TERM_VIRTUAL|TERM_CODED) ) continue;
1441 if( (pTerm->prereqAll & loopMask)!=0 ) continue;
1442 pE = pTerm->pExpr;
1443 assert( pE!=0 );
drh392e5972005-07-08 14:14:22 +00001444 if( pLevel->iLeftJoin && !ExprHasProperty(pE, EP_FromJoin) ){
drh1f162302002-10-27 19:35:33 +00001445 continue;
1446 }
drh392e5972005-07-08 14:14:22 +00001447 sqlite3ExprIfFalse(pParse, pE, cont, 1);
drh0fcef5e2005-07-19 17:38:22 +00001448 pTerm->flags |= TERM_CODED;
drh75897232000-05-29 14:26:00 +00001449 }
1450 brk = cont;
drhad2d8302002-05-24 20:31:36 +00001451
1452 /* For a LEFT OUTER JOIN, generate code that will record the fact that
1453 ** at least one row of the right table has matched the left table.
1454 */
1455 if( pLevel->iLeftJoin ){
danielk19774adee202004-05-08 08:23:19 +00001456 pLevel->top = sqlite3VdbeCurrentAddr(v);
1457 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1458 sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iLeftJoin, 1);
drhad6d9462004-09-19 02:15:24 +00001459 VdbeComment((v, "# record LEFT JOIN hit"));
drh0aa74ed2005-07-16 13:33:20 +00001460 for(pTerm=wc.a, j=0; j<wc.nTerm; j++, pTerm++){
drh0fcef5e2005-07-19 17:38:22 +00001461 if( pTerm->flags & (TERM_VIRTUAL|TERM_CODED) ) continue;
1462 if( (pTerm->prereqAll & loopMask)!=0 ) continue;
1463 assert( pTerm->pExpr );
1464 sqlite3ExprIfFalse(pParse, pTerm->pExpr, cont, 1);
1465 pTerm->flags |= TERM_CODED;
drh1cc093c2002-06-24 22:01:57 +00001466 }
drhad2d8302002-05-24 20:31:36 +00001467 }
drh75897232000-05-29 14:26:00 +00001468 }
1469 pWInfo->iContinue = cont;
drh6a3ea0e2003-05-02 14:32:12 +00001470 freeMaskSet(&maskSet);
drh0aa74ed2005-07-16 13:33:20 +00001471 whereClauseClear(&wc);
drh75897232000-05-29 14:26:00 +00001472 return pWInfo;
1473}
1474
1475/*
drhc27a1ce2002-06-14 20:58:45 +00001476** Generate the end of the WHERE loop. See comments on
danielk19774adee202004-05-08 08:23:19 +00001477** sqlite3WhereBegin() for additional information.
drh75897232000-05-29 14:26:00 +00001478*/
danielk19774adee202004-05-08 08:23:19 +00001479void sqlite3WhereEnd(WhereInfo *pWInfo){
drh75897232000-05-29 14:26:00 +00001480 Vdbe *v = pWInfo->pParse->pVdbe;
drh19a775c2000-06-05 18:54:46 +00001481 int i;
drh6b563442001-11-07 16:48:26 +00001482 WhereLevel *pLevel;
drhad3cab52002-05-24 02:04:32 +00001483 SrcList *pTabList = pWInfo->pTabList;
drh9012bcb2004-12-19 00:11:35 +00001484 struct SrcList_item *pTabItem;
drh19a775c2000-06-05 18:54:46 +00001485
drh9012bcb2004-12-19 00:11:35 +00001486 /* Generate loop termination code.
1487 */
drhad3cab52002-05-24 02:04:32 +00001488 for(i=pTabList->nSrc-1; i>=0; i--){
drh6b563442001-11-07 16:48:26 +00001489 pLevel = &pWInfo->a[i];
danielk19774adee202004-05-08 08:23:19 +00001490 sqlite3VdbeResolveLabel(v, pLevel->cont);
drh6b563442001-11-07 16:48:26 +00001491 if( pLevel->op!=OP_Noop ){
danielk19774adee202004-05-08 08:23:19 +00001492 sqlite3VdbeAddOp(v, pLevel->op, pLevel->p1, pLevel->p2);
drh19a775c2000-06-05 18:54:46 +00001493 }
danielk19774adee202004-05-08 08:23:19 +00001494 sqlite3VdbeResolveLabel(v, pLevel->brk);
drhd99f7062002-06-08 23:25:08 +00001495 if( pLevel->inOp!=OP_Noop ){
danielk19774adee202004-05-08 08:23:19 +00001496 sqlite3VdbeAddOp(v, pLevel->inOp, pLevel->inP1, pLevel->inP2);
drhd99f7062002-06-08 23:25:08 +00001497 }
drhad2d8302002-05-24 20:31:36 +00001498 if( pLevel->iLeftJoin ){
1499 int addr;
danielk19774adee202004-05-08 08:23:19 +00001500 addr = sqlite3VdbeAddOp(v, OP_MemLoad, pLevel->iLeftJoin, 0);
drh9012bcb2004-12-19 00:11:35 +00001501 sqlite3VdbeAddOp(v, OP_NotNull, 1, addr+4 + (pLevel->iIdxCur>=0));
danielk19774adee202004-05-08 08:23:19 +00001502 sqlite3VdbeAddOp(v, OP_NullRow, pTabList->a[i].iCursor, 0);
drh9012bcb2004-12-19 00:11:35 +00001503 if( pLevel->iIdxCur>=0 ){
1504 sqlite3VdbeAddOp(v, OP_NullRow, pLevel->iIdxCur, 0);
drh7f09b3e2002-08-13 13:15:49 +00001505 }
danielk19774adee202004-05-08 08:23:19 +00001506 sqlite3VdbeAddOp(v, OP_Goto, 0, pLevel->top);
drhad2d8302002-05-24 20:31:36 +00001507 }
drh19a775c2000-06-05 18:54:46 +00001508 }
drh9012bcb2004-12-19 00:11:35 +00001509
1510 /* The "break" point is here, just past the end of the outer loop.
1511 ** Set it.
1512 */
danielk19774adee202004-05-08 08:23:19 +00001513 sqlite3VdbeResolveLabel(v, pWInfo->iBreak);
drh9012bcb2004-12-19 00:11:35 +00001514
drhacf3b982005-01-03 01:27:18 +00001515 /* Close all of the cursors that were opend by sqlite3WhereBegin.
drh9012bcb2004-12-19 00:11:35 +00001516 */
1517 pLevel = pWInfo->a;
1518 pTabItem = pTabList->a;
1519 for(i=0; i<pTabList->nSrc; i++, pTabItem++, pLevel++){
1520 Table *pTab = pTabItem->pTab;
drh5cf590c2003-04-24 01:45:04 +00001521 assert( pTab!=0 );
1522 if( pTab->isTransient || pTab->pSelect ) continue;
drh9012bcb2004-12-19 00:11:35 +00001523 if( (pLevel->score & 1)==0 ){
1524 sqlite3VdbeAddOp(v, OP_Close, pTabItem->iCursor, 0);
1525 }
drh6b563442001-11-07 16:48:26 +00001526 if( pLevel->pIdx!=0 ){
drh9012bcb2004-12-19 00:11:35 +00001527 sqlite3VdbeAddOp(v, OP_Close, pLevel->iIdxCur, 0);
1528 }
1529
drhacf3b982005-01-03 01:27:18 +00001530 /* Make cursor substitutions for cases where we want to use
drh9012bcb2004-12-19 00:11:35 +00001531 ** just the index and never reference the table.
1532 **
1533 ** Calls to the code generator in between sqlite3WhereBegin and
1534 ** sqlite3WhereEnd will have created code that references the table
1535 ** directly. This loop scans all that code looking for opcodes
1536 ** that reference the table and converts them into opcodes that
1537 ** reference the index.
1538 */
1539 if( pLevel->score & 1 ){
1540 int i, j, last;
1541 VdbeOp *pOp;
1542 Index *pIdx = pLevel->pIdx;
1543
1544 assert( pIdx!=0 );
1545 pOp = sqlite3VdbeGetOp(v, pWInfo->iTop);
1546 last = sqlite3VdbeCurrentAddr(v);
1547 for(i=pWInfo->iTop; i<last; i++, pOp++){
1548 if( pOp->p1!=pLevel->iTabCur ) continue;
1549 if( pOp->opcode==OP_Column ){
1550 pOp->p1 = pLevel->iIdxCur;
1551 for(j=0; j<pIdx->nColumn; j++){
1552 if( pOp->p2==pIdx->aiColumn[j] ){
1553 pOp->p2 = j;
1554 break;
1555 }
1556 }
drhf0863fe2005-06-12 21:35:51 +00001557 }else if( pOp->opcode==OP_Rowid ){
drh9012bcb2004-12-19 00:11:35 +00001558 pOp->p1 = pLevel->iIdxCur;
drhf0863fe2005-06-12 21:35:51 +00001559 pOp->opcode = OP_IdxRowid;
danielk19776c18b6e2005-01-30 09:17:58 +00001560 }else if( pOp->opcode==OP_NullRow ){
1561 pOp->opcode = OP_Noop;
drh9012bcb2004-12-19 00:11:35 +00001562 }
1563 }
drh6b563442001-11-07 16:48:26 +00001564 }
drh19a775c2000-06-05 18:54:46 +00001565 }
drh9012bcb2004-12-19 00:11:35 +00001566
1567 /* Final cleanup
1568 */
drh75897232000-05-29 14:26:00 +00001569 sqliteFree(pWInfo);
1570 return;
1571}