blob: 1e5dc61612680dee7078720596ae4aa3c959d43c [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**
drhe8b97272005-07-19 22:22:12 +000019** $Id: where.c,v 1.147 2005/07/19 22:22:13 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/*
drhe8b97272005-07-19 22:22:12 +0000555** Search for a term in the WHERE clause that is of the form "X <op> <expr>"
556** where X is a reference to the iColumn of table iCur and <op> is either
557** op1 or op2. Return a pointer to the term.
558*/
559static WhereTerm *findTerm(
560 WhereClause *pWC, /* The WHERE clause to be searched */
561 int iCur, /* Cursor number of LHS */
562 int iColumn, /* Column number of LHS */
563 Bitmask loopMask, /* RHS must not overlap with this mask */
564 u8 op1, u8 op2 /* Expression must use either of these opcodes */
565){
566 WhereTerm *pTerm;
567 int k;
568 for(pTerm=pWC->a, k=pWC->nTerm; k; k--, pTerm++){
569 u8 op = pTerm->pExpr->op;
570 if( pTerm->leftCursor==iCur
571 && (pTerm->prereqRight & loopMask)==0
572 && pTerm->leftColumn==iColumn
573 && (op==op1 || op==op2)
574 ){
575 break;
576 }
577 }
578 assert( k>0 ); /* The search is always successful */
579 return pTerm;
580}
581
582
583/*
drh94a11212004-09-25 13:12:14 +0000584** Generate code for an equality term of the WHERE clause. An equality
585** term can be either X=expr or X IN (...). pTerm is the X.
586*/
587static void codeEqualityTerm(
588 Parse *pParse, /* The parsing context */
drh0aa74ed2005-07-16 13:33:20 +0000589 WhereTerm *pTerm, /* The term of the WHERE clause to be coded */
drh94a11212004-09-25 13:12:14 +0000590 int brk, /* Jump here to abandon the loop */
591 WhereLevel *pLevel /* When level of the FROM clause we are working on */
592){
drh0fcef5e2005-07-19 17:38:22 +0000593 Expr *pX = pTerm->pExpr;
drh94a11212004-09-25 13:12:14 +0000594 if( pX->op!=TK_IN ){
595 assert( pX->op==TK_EQ );
596 sqlite3ExprCode(pParse, pX->pRight);
danielk1977b3bce662005-01-29 08:32:43 +0000597#ifndef SQLITE_OMIT_SUBQUERY
drh94a11212004-09-25 13:12:14 +0000598 }else{
danielk1977b3bce662005-01-29 08:32:43 +0000599 int iTab;
drh94a11212004-09-25 13:12:14 +0000600 Vdbe *v = pParse->pVdbe;
danielk1977b3bce662005-01-29 08:32:43 +0000601
602 sqlite3CodeSubselect(pParse, pX);
603 iTab = pX->iTable;
drh94a11212004-09-25 13:12:14 +0000604 sqlite3VdbeAddOp(v, OP_Rewind, iTab, brk);
danielk1977b3bce662005-01-29 08:32:43 +0000605 VdbeComment((v, "# %.*s", pX->span.n, pX->span.z));
drh9012bcb2004-12-19 00:11:35 +0000606 pLevel->inP2 = sqlite3VdbeAddOp(v, OP_Column, iTab, 0);
drh94a11212004-09-25 13:12:14 +0000607 pLevel->inOp = OP_Next;
608 pLevel->inP1 = iTab;
danielk1977b3bce662005-01-29 08:32:43 +0000609#endif
drh94a11212004-09-25 13:12:14 +0000610 }
drh0fcef5e2005-07-19 17:38:22 +0000611 disableTerm(pLevel, pTerm);
drh94a11212004-09-25 13:12:14 +0000612}
613
drh84bfda42005-07-15 13:05:21 +0000614#ifdef SQLITE_TEST
615/*
616** The following variable holds a text description of query plan generated
617** by the most recent call to sqlite3WhereBegin(). Each call to WhereBegin
618** overwrites the previous. This information is used for testing and
619** analysis only.
620*/
621char sqlite3_query_plan[BMS*2*40]; /* Text of the join */
622static int nQPlan = 0; /* Next free slow in _query_plan[] */
623
624#endif /* SQLITE_TEST */
625
626
drh94a11212004-09-25 13:12:14 +0000627
628/*
drhe3184742002-06-19 14:27:05 +0000629** Generate the beginning of the loop used for WHERE clause processing.
drhacf3b982005-01-03 01:27:18 +0000630** The return value is a pointer to an opaque structure that contains
drh75897232000-05-29 14:26:00 +0000631** information needed to terminate the loop. Later, the calling routine
danielk19774adee202004-05-08 08:23:19 +0000632** should invoke sqlite3WhereEnd() with the return value of this function
drh75897232000-05-29 14:26:00 +0000633** in order to complete the WHERE clause processing.
634**
635** If an error occurs, this routine returns NULL.
drhc27a1ce2002-06-14 20:58:45 +0000636**
637** The basic idea is to do a nested loop, one loop for each table in
638** the FROM clause of a select. (INSERT and UPDATE statements are the
639** same as a SELECT with only a single table in the FROM clause.) For
640** example, if the SQL is this:
641**
642** SELECT * FROM t1, t2, t3 WHERE ...;
643**
644** Then the code generated is conceptually like the following:
645**
646** foreach row1 in t1 do \ Code generated
danielk19774adee202004-05-08 08:23:19 +0000647** foreach row2 in t2 do |-- by sqlite3WhereBegin()
drhc27a1ce2002-06-14 20:58:45 +0000648** foreach row3 in t3 do /
649** ...
650** end \ Code generated
danielk19774adee202004-05-08 08:23:19 +0000651** end |-- by sqlite3WhereEnd()
drhc27a1ce2002-06-14 20:58:45 +0000652** end /
653**
654** There are Btree cursors associated with each table. t1 uses cursor
drh6a3ea0e2003-05-02 14:32:12 +0000655** number pTabList->a[0].iCursor. t2 uses the cursor pTabList->a[1].iCursor.
656** And so forth. This routine generates code to open those VDBE cursors
danielk19774adee202004-05-08 08:23:19 +0000657** and sqlite3WhereEnd() generates the code to close them.
drhc27a1ce2002-06-14 20:58:45 +0000658**
drhe6f85e72004-12-25 01:03:13 +0000659** The code that sqlite3WhereBegin() generates leaves the cursors named
660** in pTabList pointing at their appropriate entries. The [...] code
drhf0863fe2005-06-12 21:35:51 +0000661** can use OP_Column and OP_Rowid opcodes on these cursors to extract
drhe6f85e72004-12-25 01:03:13 +0000662** data from the various tables of the loop.
663**
drhc27a1ce2002-06-14 20:58:45 +0000664** If the WHERE clause is empty, the foreach loops must each scan their
665** entire tables. Thus a three-way join is an O(N^3) operation. But if
666** the tables have indices and there are terms in the WHERE clause that
667** refer to those indices, a complete table scan can be avoided and the
668** code will run much faster. Most of the work of this routine is checking
669** to see if there are indices that can be used to speed up the loop.
670**
671** Terms of the WHERE clause are also used to limit which rows actually
672** make it to the "..." in the middle of the loop. After each "foreach",
673** terms of the WHERE clause that use only terms in that loop and outer
674** loops are evaluated and if false a jump is made around all subsequent
675** inner loops (or around the "..." if the test occurs within the inner-
676** most loop)
677**
678** OUTER JOINS
679**
680** An outer join of tables t1 and t2 is conceptally coded as follows:
681**
682** foreach row1 in t1 do
683** flag = 0
684** foreach row2 in t2 do
685** start:
686** ...
687** flag = 1
688** end
drhe3184742002-06-19 14:27:05 +0000689** if flag==0 then
690** move the row2 cursor to a null row
691** goto start
692** fi
drhc27a1ce2002-06-14 20:58:45 +0000693** end
694**
drhe3184742002-06-19 14:27:05 +0000695** ORDER BY CLAUSE PROCESSING
696**
697** *ppOrderBy is a pointer to the ORDER BY clause of a SELECT statement,
698** if there is one. If there is no ORDER BY clause or if this routine
699** is called from an UPDATE or DELETE statement, then ppOrderBy is NULL.
700**
701** If an index can be used so that the natural output order of the table
702** scan is correct for the ORDER BY clause, then that index is used and
703** *ppOrderBy is set to NULL. This is an optimization that prevents an
704** unnecessary sort of the result set if an index appropriate for the
705** ORDER BY clause already exists.
706**
707** If the where clause loops cannot be arranged to provide the correct
708** output order, then the *ppOrderBy is unchanged.
drh75897232000-05-29 14:26:00 +0000709*/
danielk19774adee202004-05-08 08:23:19 +0000710WhereInfo *sqlite3WhereBegin(
danielk1977ed326d72004-11-16 15:50:19 +0000711 Parse *pParse, /* The parser context */
712 SrcList *pTabList, /* A list of all tables to be scanned */
713 Expr *pWhere, /* The WHERE clause */
drhf8db1bc2005-04-22 02:38:37 +0000714 ExprList **ppOrderBy /* An ORDER BY clause, or NULL */
drh75897232000-05-29 14:26:00 +0000715){
716 int i; /* Loop counter */
717 WhereInfo *pWInfo; /* Will become the return value of this function */
718 Vdbe *v = pParse->pVdbe; /* The virtual database engine */
drhd4f5ee22003-07-16 00:54:31 +0000719 int brk, cont = 0; /* Addresses used during code generation */
drh0fcef5e2005-07-19 17:38:22 +0000720 Bitmask loopMask; /* One bit cleared for each outer loop */
drh0aa74ed2005-07-16 13:33:20 +0000721 WhereTerm *pTerm; /* A single term in the WHERE clause */
722 ExprMaskSet maskSet; /* The expression mask set */
723 int iDirectEq[BMS]; /* Term of the form ROWID==X for the N-th table */
724 int iDirectLt[BMS]; /* Term of the form ROWID<X or ROWID<=X */
725 int iDirectGt[BMS]; /* Term of the form ROWID>X or ROWID>=X */
726 WhereClause wc; /* The WHERE clause is divided into these terms */
drh9012bcb2004-12-19 00:11:35 +0000727 struct SrcList_item *pTabItem; /* A single entry from pTabList */
728 WhereLevel *pLevel; /* A single level in the pWInfo list */
drh75897232000-05-29 14:26:00 +0000729
drh1398ad32005-01-19 23:24:50 +0000730 /* The number of terms in the FROM clause is limited by the number of
731 ** bits in a Bitmask
732 */
733 if( pTabList->nSrc>sizeof(Bitmask)*8 ){
734 sqlite3ErrorMsg(pParse, "at most %d tables in a join",
735 sizeof(Bitmask)*8);
736 return 0;
737 }
738
drh83dcb1a2002-06-28 01:02:38 +0000739 /* Split the WHERE clause into separate subexpressions where each
drh0aa74ed2005-07-16 13:33:20 +0000740 ** subexpression is separated by an AND operator. If the wc.a[]
drh83dcb1a2002-06-28 01:02:38 +0000741 ** array fills up, the last entry might point to an expression which
742 ** contains additional unfactored AND operators.
743 */
drh6a3ea0e2003-05-02 14:32:12 +0000744 initMaskSet(&maskSet);
drh0aa74ed2005-07-16 13:33:20 +0000745 whereClauseInit(&wc);
746 whereSplit(&wc, pWhere);
drh1398ad32005-01-19 23:24:50 +0000747
drh75897232000-05-29 14:26:00 +0000748 /* Allocate and initialize the WhereInfo structure that will become the
749 ** return value.
750 */
drhad3cab52002-05-24 02:04:32 +0000751 pWInfo = sqliteMalloc( sizeof(WhereInfo) + pTabList->nSrc*sizeof(WhereLevel));
danielk1977132872b2004-05-10 10:37:18 +0000752 if( sqlite3_malloc_failed ){
danielk1977d5d56522005-03-16 12:15:20 +0000753 sqliteFree(pWInfo); /* Avoid leaking memory when malloc fails */
drh0aa74ed2005-07-16 13:33:20 +0000754 whereClauseClear(&wc);
drh75897232000-05-29 14:26:00 +0000755 return 0;
756 }
757 pWInfo->pParse = pParse;
758 pWInfo->pTabList = pTabList;
danielk19774adee202004-05-08 08:23:19 +0000759 pWInfo->iBreak = sqlite3VdbeMakeLabel(v);
drh08192d52002-04-30 19:20:28 +0000760
761 /* Special case: a WHERE clause that is constant. Evaluate the
762 ** expression and either jump over all of the code or fall thru.
763 */
danielk19774adee202004-05-08 08:23:19 +0000764 if( pWhere && (pTabList->nSrc==0 || sqlite3ExprIsConstant(pWhere)) ){
765 sqlite3ExprIfFalse(pParse, pWhere, pWInfo->iBreak, 1);
drhdf199a22002-06-14 22:38:41 +0000766 pWhere = 0;
drh08192d52002-04-30 19:20:28 +0000767 }
drh75897232000-05-29 14:26:00 +0000768
drh75897232000-05-29 14:26:00 +0000769 /* Analyze all of the subexpressions.
770 */
drh1398ad32005-01-19 23:24:50 +0000771 for(i=0; i<pTabList->nSrc; i++){
772 createMask(&maskSet, pTabList->a[i].iCursor);
773 }
drh0fcef5e2005-07-19 17:38:22 +0000774 for(i=wc.nTerm-1; i>=0; i--){
775 exprAnalyze(pTabList, &maskSet, &wc.a[i]);
drh75897232000-05-29 14:26:00 +0000776 }
777
drh75897232000-05-29 14:26:00 +0000778 /* Figure out what index to use (if any) for each nested loop.
drh6b563442001-11-07 16:48:26 +0000779 ** Make pWInfo->a[i].pIdx point to the index to use for the i-th nested
drhad3cab52002-05-24 02:04:32 +0000780 ** loop where i==0 is the outer loop and i==pTabList->nSrc-1 is the inner
drh8aff1012001-12-22 14:49:24 +0000781 ** loop.
782 **
783 ** If terms exist that use the ROWID of any table, then set the
784 ** iDirectEq[], iDirectLt[], or iDirectGt[] elements for that table
785 ** to the index of the term containing the ROWID. We always prefer
786 ** to use a ROWID which can directly access a table rather than an
drh0a36c572002-02-18 22:49:59 +0000787 ** index which requires reading an index first to get the rowid then
788 ** doing a second read of the actual database table.
drh75897232000-05-29 14:26:00 +0000789 **
790 ** Actually, if there are more than 32 tables in the join, only the
drh0a36c572002-02-18 22:49:59 +0000791 ** first 32 tables are candidates for indices. This is (again) due
792 ** to the limit of 32 bits in an integer bitmask.
drh75897232000-05-29 14:26:00 +0000793 */
drh0fcef5e2005-07-19 17:38:22 +0000794 loopMask = ~(Bitmask)0;
drh9012bcb2004-12-19 00:11:35 +0000795 pTabItem = pTabList->a;
796 pLevel = pWInfo->a;
797 for(i=0; i<pTabList->nSrc && i<ARRAYSIZE(iDirectEq); i++,pTabItem++,pLevel++){
drhc4a3c772001-04-04 11:48:57 +0000798 int j;
drh9012bcb2004-12-19 00:11:35 +0000799 int iCur = pTabItem->iCursor; /* The cursor for this table */
drh51669862004-12-18 18:40:26 +0000800 Bitmask mask = getMask(&maskSet, iCur); /* Cursor mask for this table */
drh9012bcb2004-12-19 00:11:35 +0000801 Table *pTab = pTabItem->pTab;
drh75897232000-05-29 14:26:00 +0000802 Index *pIdx;
803 Index *pBestIdx = 0;
drh487ab3c2001-11-08 00:45:21 +0000804 int bestScore = 0;
drh51669862004-12-18 18:40:26 +0000805 int bestRev = 0;
drh75897232000-05-29 14:26:00 +0000806
drhc4a3c772001-04-04 11:48:57 +0000807 /* Check to see if there is an expression that uses only the
drh8aff1012001-12-22 14:49:24 +0000808 ** ROWID field of this table. For terms of the form ROWID==expr
809 ** set iDirectEq[i] to the index of the term. For terms of the
810 ** form ROWID<expr or ROWID<=expr set iDirectLt[i] to the term index.
811 ** For terms like ROWID>expr or ROWID>=expr set iDirectGt[i].
drh174b6192002-12-03 02:22:52 +0000812 **
813 ** (Added:) Treat ROWID IN expr like ROWID=expr.
drhc4a3c772001-04-04 11:48:57 +0000814 */
drh9012bcb2004-12-19 00:11:35 +0000815 pLevel->iIdxCur = -1;
drh8aff1012001-12-22 14:49:24 +0000816 iDirectEq[i] = -1;
817 iDirectLt[i] = -1;
818 iDirectGt[i] = -1;
drh0aa74ed2005-07-16 13:33:20 +0000819 for(pTerm=wc.a, j=0; j<wc.nTerm; j++, pTerm++){
drh0fcef5e2005-07-19 17:38:22 +0000820 if( pTerm->leftCursor==iCur && pTerm->leftColumn<0
821 && (pTerm->prereqRight & loopMask)==0 ){
822 switch( pTerm->pExpr->op ){
drhd99f7062002-06-08 23:25:08 +0000823 case TK_IN:
drh8aff1012001-12-22 14:49:24 +0000824 case TK_EQ: iDirectEq[i] = j; break;
825 case TK_LE:
826 case TK_LT: iDirectLt[i] = j; break;
827 case TK_GE:
828 case TK_GT: iDirectGt[i] = j; break;
829 }
drhc4a3c772001-04-04 11:48:57 +0000830 }
drhc4a3c772001-04-04 11:48:57 +0000831 }
drhb6c29892004-11-22 19:12:19 +0000832
833 /* If we found a term that tests ROWID with == or IN, that term
834 ** will be used to locate the rows in the database table. There
drh7bac7002005-07-01 11:38:44 +0000835 ** is no need to continue into the code below that looks for
drhb6c29892004-11-22 19:12:19 +0000836 ** an index. We will always use the ROWID over an index.
837 */
drh8aff1012001-12-22 14:49:24 +0000838 if( iDirectEq[i]>=0 ){
drh0fcef5e2005-07-19 17:38:22 +0000839 loopMask &= ~mask;
drh94a11212004-09-25 13:12:14 +0000840 pLevel->pIdx = 0;
drhc4a3c772001-04-04 11:48:57 +0000841 continue;
842 }
843
drh75897232000-05-29 14:26:00 +0000844 /* Do a search for usable indices. Leave pBestIdx pointing to
drh487ab3c2001-11-08 00:45:21 +0000845 ** the "best" index. pBestIdx is left set to NULL if no indices
846 ** are usable.
drh75897232000-05-29 14:26:00 +0000847 **
drhacf3b982005-01-03 01:27:18 +0000848 ** The best index is the one with the highest score. The score
849 ** for the index is determined as follows. For each of the
drh487ab3c2001-11-08 00:45:21 +0000850 ** left-most terms that is fixed by an equality operator, add
drh51669862004-12-18 18:40:26 +0000851 ** 32 to the score. The right-most term of the index may be
852 ** constrained by an inequality. Add 4 if for an "x<..." constraint
853 ** and add 8 for an "x>..." constraint. If both constraints
854 ** are present, add 12.
855 **
856 ** If the left-most term of the index uses an IN operator
857 ** (ex: "x IN (...)") then add 16 to the score.
858 **
859 ** If an index can be used for sorting, add 2 to the score.
860 ** If an index contains all the terms of a table that are ever
861 ** used by any expression in the SQL statement, then add 1 to
862 ** the score.
drh487ab3c2001-11-08 00:45:21 +0000863 **
864 ** This scoring system is designed so that the score can later be
drh51669862004-12-18 18:40:26 +0000865 ** used to determine how the index is used. If the score&0x1c is 0
866 ** then all constraints are equalities. If score&0x4 is not 0 then
drh487ab3c2001-11-08 00:45:21 +0000867 ** there is an inequality used as a termination key. (ex: "x<...")
drh51669862004-12-18 18:40:26 +0000868 ** If score&0x8 is not 0 then there is an inequality used as the
869 ** start key. (ex: "x>..."). A score or 0x10 is the special case
drhc045ec52002-12-04 20:01:06 +0000870 ** of an IN operator constraint. (ex: "x IN ...").
drhd99f7062002-06-08 23:25:08 +0000871 **
drhc27a1ce2002-06-14 20:58:45 +0000872 ** The IN operator (as in "<expr> IN (...)") is treated the same as
873 ** an equality comparison except that it can only be used on the
874 ** left-most column of an index and other terms of the WHERE clause
875 ** cannot be used in conjunction with the IN operator to help satisfy
876 ** other columns of the index.
drh75897232000-05-29 14:26:00 +0000877 */
878 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drh51669862004-12-18 18:40:26 +0000879 Bitmask eqMask = 0; /* Index columns covered by an x=... term */
880 Bitmask ltMask = 0; /* Index columns covered by an x<... term */
881 Bitmask gtMask = 0; /* Index columns covered by an x>... term */
882 Bitmask inMask = 0; /* Index columns covered by an x IN .. term */
883 Bitmask m;
884 int nEq, score, bRev = 0;
drh75897232000-05-29 14:26:00 +0000885
drh51669862004-12-18 18:40:26 +0000886 if( pIdx->nColumn>sizeof(eqMask)*8 ){
887 continue; /* Ignore indices with too many columns to analyze */
888 }
drh0aa74ed2005-07-16 13:33:20 +0000889 for(pTerm=wc.a, j=0; j<wc.nTerm; j++, pTerm++){
drh0fcef5e2005-07-19 17:38:22 +0000890 Expr *pX = pTerm->pExpr;
drh94a11212004-09-25 13:12:14 +0000891 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pX->pLeft);
drh193bd772004-07-20 18:23:14 +0000892 if( !pColl && pX->pRight ){
893 pColl = sqlite3ExprCollSeq(pParse, pX->pRight);
danielk19770202b292004-06-09 09:55:16 +0000894 }
895 if( !pColl ){
896 pColl = pParse->db->pDfltColl;
897 }
drh0fcef5e2005-07-19 17:38:22 +0000898 if( pTerm->leftCursor==iCur && (pTerm->prereqRight & loopMask)==0 ){
899 int iColumn = pTerm->leftColumn;
drh75897232000-05-29 14:26:00 +0000900 int k;
drhdd9f8b42005-05-19 01:26:14 +0000901 char idxaff = iColumn>=0 ? pIdx->pTable->aCol[iColumn].affinity : 0;
drh967e8b72000-06-21 13:59:10 +0000902 for(k=0; k<pIdx->nColumn; k++){
danielk19770202b292004-06-09 09:55:16 +0000903 /* If the collating sequences or affinities don't match,
904 ** ignore this index. */
905 if( pColl!=pIdx->keyInfo.aColl[k] ) continue;
drh193bd772004-07-20 18:23:14 +0000906 if( !sqlite3IndexAffinityOk(pX, idxaff) ) continue;
danielk19770202b292004-06-09 09:55:16 +0000907 if( pIdx->aiColumn[k]==iColumn ){
drh193bd772004-07-20 18:23:14 +0000908 switch( pX->op ){
drh48185c12002-06-09 01:55:20 +0000909 case TK_IN: {
910 if( k==0 ) inMask |= 1;
911 break;
912 }
drh487ab3c2001-11-08 00:45:21 +0000913 case TK_EQ: {
drh51669862004-12-18 18:40:26 +0000914 eqMask |= ((Bitmask)1)<<k;
drh487ab3c2001-11-08 00:45:21 +0000915 break;
916 }
917 case TK_LE:
918 case TK_LT: {
drh51669862004-12-18 18:40:26 +0000919 ltMask |= ((Bitmask)1)<<k;
drh487ab3c2001-11-08 00:45:21 +0000920 break;
921 }
922 case TK_GE:
923 case TK_GT: {
drh51669862004-12-18 18:40:26 +0000924 gtMask |= ((Bitmask)1)<<k;
drh487ab3c2001-11-08 00:45:21 +0000925 break;
926 }
927 default: {
928 /* CANT_HAPPEN */
929 assert( 0 );
930 break;
931 }
932 }
drh75897232000-05-29 14:26:00 +0000933 break;
934 }
935 }
936 }
drh75897232000-05-29 14:26:00 +0000937 }
drhc045ec52002-12-04 20:01:06 +0000938
939 /* The following loop ends with nEq set to the number of columns
940 ** on the left of the index with == constraints.
941 */
drh487ab3c2001-11-08 00:45:21 +0000942 for(nEq=0; nEq<pIdx->nColumn; nEq++){
drh51669862004-12-18 18:40:26 +0000943 m = (((Bitmask)1)<<(nEq+1))-1;
drh487ab3c2001-11-08 00:45:21 +0000944 if( (m & eqMask)!=m ) break;
945 }
drh51669862004-12-18 18:40:26 +0000946
drh7bac7002005-07-01 11:38:44 +0000947 /* Begin assembling the score
drh51669862004-12-18 18:40:26 +0000948 */
949 score = nEq*32; /* Base score is 32 times number of == constraints */
950 m = ((Bitmask)1)<<nEq;
951 if( m & ltMask ) score+=4; /* Increase score for a < constraint */
952 if( m & gtMask ) score+=8; /* Increase score for a > constraint */
953 if( score==0 && inMask ) score = 16; /* Default score for IN constraint */
954
955 /* Give bonus points if this index can be used for sorting
956 */
drh9012bcb2004-12-19 00:11:35 +0000957 if( i==0 && score!=16 && ppOrderBy && *ppOrderBy ){
drh51669862004-12-18 18:40:26 +0000958 int base = pTabList->a[0].iCursor;
959 if( isSortingIndex(pParse, pIdx, pTab, base, *ppOrderBy, nEq, &bRev) ){
960 score += 2;
961 }
962 }
963
drh9012bcb2004-12-19 00:11:35 +0000964 /* Check to see if we can get away with using just the index without
965 ** ever reading the table. If that is the case, then add one bonus
966 ** point to the score.
967 */
968 if( score && pTabItem->colUsed < (((Bitmask)1)<<(BMS-1)) ){
969 for(m=0, j=0; j<pIdx->nColumn; j++){
970 int x = pIdx->aiColumn[j];
971 if( x<BMS-1 ){
972 m |= ((Bitmask)1)<<x;
973 }
974 }
975 if( (pTabItem->colUsed & m)==pTabItem->colUsed ){
976 score++;
977 }
978 }
979
drh51669862004-12-18 18:40:26 +0000980 /* If the score for this index is the best we have seen so far, then
981 ** save it
982 */
drh487ab3c2001-11-08 00:45:21 +0000983 if( score>bestScore ){
984 pBestIdx = pIdx;
985 bestScore = score;
drh51669862004-12-18 18:40:26 +0000986 bestRev = bRev;
drh75897232000-05-29 14:26:00 +0000987 }
988 }
drh94a11212004-09-25 13:12:14 +0000989 pLevel->pIdx = pBestIdx;
990 pLevel->score = bestScore;
drh51669862004-12-18 18:40:26 +0000991 pLevel->bRev = bestRev;
drh0fcef5e2005-07-19 17:38:22 +0000992 loopMask &= ~mask;
drh6b563442001-11-07 16:48:26 +0000993 if( pBestIdx ){
drh9012bcb2004-12-19 00:11:35 +0000994 pLevel->iIdxCur = pParse->nTab++;
drh6b563442001-11-07 16:48:26 +0000995 }
drh75897232000-05-29 14:26:00 +0000996 }
997
drhe3184742002-06-19 14:27:05 +0000998 /* Check to see if the ORDER BY clause is or can be satisfied by the
999 ** use of an index on the first table.
1000 */
1001 if( ppOrderBy && *ppOrderBy && pTabList->nSrc>0 ){
drh9012bcb2004-12-19 00:11:35 +00001002 Index *pIdx; /* Index derived from the WHERE clause */
1003 Table *pTab; /* Left-most table in the FROM clause */
1004 int bRev = 0; /* True to reverse the output order */
1005 int iCur; /* Btree-cursor that will be used by pTab */
1006 WhereLevel *pLevel0 = &pWInfo->a[0];
drhe3184742002-06-19 14:27:05 +00001007
drh9012bcb2004-12-19 00:11:35 +00001008 pTab = pTabList->a[0].pTab;
1009 pIdx = pLevel0->pIdx;
1010 iCur = pTabList->a[0].iCursor;
1011 if( pIdx==0 && sortableByRowid(iCur, *ppOrderBy, &bRev) ){
1012 /* The ORDER BY clause specifies ROWID order, which is what we
1013 ** were going to be doing anyway...
1014 */
1015 *ppOrderBy = 0;
1016 pLevel0->bRev = bRev;
1017 }else if( pLevel0->score==16 ){
1018 /* If there is already an IN index on the left-most table,
1019 ** it will not give the correct sort order.
1020 ** So, pretend that no suitable index is found.
1021 */
1022 }else if( iDirectEq[0]>=0 || iDirectLt[0]>=0 || iDirectGt[0]>=0 ){
1023 /* If the left-most column is accessed using its ROWID, then do
1024 ** not try to sort by index. But do delete the ORDER BY clause
1025 ** if it is redundant.
1026 */
1027 }else if( (pLevel0->score&2)!=0 ){
1028 /* The index that was selected for searching will cause rows to
1029 ** appear in sorted order.
1030 */
1031 *ppOrderBy = 0;
drh75897232000-05-29 14:26:00 +00001032 }
1033 }
1034
drh9012bcb2004-12-19 00:11:35 +00001035 /* Open all tables in the pTabList and any indices selected for
1036 ** searching those tables.
1037 */
1038 sqlite3CodeVerifySchema(pParse, -1); /* Insert the cookie verifier Goto */
1039 pLevel = pWInfo->a;
1040 for(i=0, pTabItem=pTabList->a; i<pTabList->nSrc; i++, pTabItem++, pLevel++){
1041 Table *pTab;
1042 Index *pIx;
1043 int iIdxCur = pLevel->iIdxCur;
1044
1045 pTab = pTabItem->pTab;
1046 if( pTab->isTransient || pTab->pSelect ) continue;
1047 if( (pLevel->score & 1)==0 ){
1048 sqlite3OpenTableForReading(v, pTabItem->iCursor, pTab);
1049 }
1050 pLevel->iTabCur = pTabItem->iCursor;
1051 if( (pIx = pLevel->pIdx)!=0 ){
1052 sqlite3VdbeAddOp(v, OP_Integer, pIx->iDb, 0);
1053 sqlite3VdbeOp3(v, OP_OpenRead, iIdxCur, pIx->tnum,
1054 (char*)&pIx->keyInfo, P3_KEYINFO);
1055 }
1056 if( (pLevel->score & 1)!=0 ){
drh9012bcb2004-12-19 00:11:35 +00001057 sqlite3VdbeAddOp(v, OP_SetNumColumns, iIdxCur, pIx->nColumn+1);
1058 }
1059 sqlite3CodeVerifySchema(pParse, pTab->iDb);
drh84bfda42005-07-15 13:05:21 +00001060
1061#ifdef SQLITE_TEST
1062 /* Record in the query plan information about the current table
1063 ** and the index used to access it (if any). If the table itself
drh9042f392005-07-15 23:24:23 +00001064 ** is not used, its name is just '{}'. If no index is used
drh84bfda42005-07-15 13:05:21 +00001065 ** the index is listed as "{}"
1066 */
1067 {
drh9042f392005-07-15 23:24:23 +00001068 char *z = pTabItem->zAlias;
1069 int n;
1070 if( z==0 ) z = pTab->zName;
1071 n = strlen(z);
drh84bfda42005-07-15 13:05:21 +00001072 if( n+nQPlan < sizeof(sqlite3_query_plan)-10 ){
drh9042f392005-07-15 23:24:23 +00001073 if( (pLevel->score & 1)!=0 ){
1074 strcpy(&sqlite3_query_plan[nQPlan], "{}");
1075 nQPlan += 2;
1076 }else{
1077 strcpy(&sqlite3_query_plan[nQPlan], z);
1078 nQPlan += n;
drh84bfda42005-07-15 13:05:21 +00001079 }
1080 sqlite3_query_plan[nQPlan++] = ' ';
1081 }
1082 if( pIx==0 ){
1083 strcpy(&sqlite3_query_plan[nQPlan], " {}");
1084 nQPlan += 3;
1085 }else{
1086 n = strlen(pIx->zName);
1087 if( n+nQPlan < sizeof(sqlite3_query_plan)-2 ){
1088 strcpy(&sqlite3_query_plan[nQPlan], pIx->zName);
1089 nQPlan += n;
1090 sqlite3_query_plan[nQPlan++] = ' ';
1091 }
1092 }
1093 }
1094#endif
drh9012bcb2004-12-19 00:11:35 +00001095 }
1096 pWInfo->iTop = sqlite3VdbeCurrentAddr(v);
1097
drh84bfda42005-07-15 13:05:21 +00001098#ifdef SQLITE_TEST
1099 /* Terminate the query plan description
1100 */
1101 while( nQPlan>0 && sqlite3_query_plan[nQPlan-1]==' ' ){
1102 sqlite3_query_plan[--nQPlan] = 0;
1103 }
1104 sqlite3_query_plan[nQPlan] = 0;
1105 nQPlan = 0;
1106#endif
1107
drh75897232000-05-29 14:26:00 +00001108 /* Generate the code to do the search
1109 */
drh0fcef5e2005-07-19 17:38:22 +00001110 loopMask = ~(Bitmask)0;
drh9012bcb2004-12-19 00:11:35 +00001111 pLevel = pWInfo->a;
1112 pTabItem = pTabList->a;
1113 for(i=0; i<pTabList->nSrc; i++, pTabItem++, pLevel++){
drh75897232000-05-29 14:26:00 +00001114 int j, k;
drh9012bcb2004-12-19 00:11:35 +00001115 int iCur = pTabItem->iCursor; /* The VDBE cursor for the table */
1116 Index *pIdx; /* The index we will be using */
1117 int iIdxCur; /* The VDBE cursor for the index */
1118 int omitTable; /* True if we use the index only */
1119
1120 pIdx = pLevel->pIdx;
1121 iIdxCur = pLevel->iIdxCur;
1122 pLevel->inOp = OP_Noop;
1123
1124 /* Check to see if it is appropriate to omit the use of the table
1125 ** here and use its index instead.
1126 */
1127 omitTable = (pLevel->score&1)!=0;
drh75897232000-05-29 14:26:00 +00001128
drhad2d8302002-05-24 20:31:36 +00001129 /* If this is the right table of a LEFT OUTER JOIN, allocate and
drh174b6192002-12-03 02:22:52 +00001130 ** initialize a memory cell that records if this table matches any
drhc27a1ce2002-06-14 20:58:45 +00001131 ** row of the left table of the join.
drhad2d8302002-05-24 20:31:36 +00001132 */
1133 if( i>0 && (pTabList->a[i-1].jointype & JT_LEFT)!=0 ){
1134 if( !pParse->nMem ) pParse->nMem++;
1135 pLevel->iLeftJoin = pParse->nMem++;
drhf0863fe2005-06-12 21:35:51 +00001136 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001137 sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iLeftJoin, 1);
drhad6d9462004-09-19 02:15:24 +00001138 VdbeComment((v, "# init LEFT JOIN no-match flag"));
drhad2d8302002-05-24 20:31:36 +00001139 }
1140
drh94a11212004-09-25 13:12:14 +00001141 if( i<ARRAYSIZE(iDirectEq) && (k = iDirectEq[i])>=0 ){
drh8aff1012001-12-22 14:49:24 +00001142 /* Case 1: We can directly reference a single row using an
drhc27a1ce2002-06-14 20:58:45 +00001143 ** equality comparison against the ROWID field. Or
1144 ** we reference multiple rows using a "rowid IN (...)"
1145 ** construct.
drhc4a3c772001-04-04 11:48:57 +00001146 */
drh0aa74ed2005-07-16 13:33:20 +00001147 assert( k<wc.nTerm );
1148 pTerm = &wc.a[k];
drh0fcef5e2005-07-19 17:38:22 +00001149 assert( pTerm->pExpr!=0 );
1150 assert( pTerm->leftCursor==iCur );
drh9012bcb2004-12-19 00:11:35 +00001151 assert( omitTable==0 );
drh94a11212004-09-25 13:12:14 +00001152 brk = pLevel->brk = sqlite3VdbeMakeLabel(v);
1153 codeEqualityTerm(pParse, pTerm, brk, pLevel);
danielk19774adee202004-05-08 08:23:19 +00001154 cont = pLevel->cont = sqlite3VdbeMakeLabel(v);
1155 sqlite3VdbeAddOp(v, OP_MustBeInt, 1, brk);
danielk19774adee202004-05-08 08:23:19 +00001156 sqlite3VdbeAddOp(v, OP_NotExists, iCur, brk);
tpoindex7a9b1612005-01-03 18:13:18 +00001157 VdbeComment((v, "pk"));
drh6b563442001-11-07 16:48:26 +00001158 pLevel->op = OP_Noop;
drh9012bcb2004-12-19 00:11:35 +00001159 }else if( pIdx!=0 && pLevel->score>3 && (pLevel->score&0x0c)==0 ){
drhc27a1ce2002-06-14 20:58:45 +00001160 /* Case 2: There is an index and all terms of the WHERE clause that
drhb6c29892004-11-22 19:12:19 +00001161 ** refer to the index using the "==" or "IN" operators.
drh75897232000-05-29 14:26:00 +00001162 */
drh6b563442001-11-07 16:48:26 +00001163 int start;
drh51669862004-12-18 18:40:26 +00001164 int nColumn = (pLevel->score+16)/32;
danielk19774adee202004-05-08 08:23:19 +00001165 brk = pLevel->brk = sqlite3VdbeMakeLabel(v);
drh772ae622004-05-19 13:13:08 +00001166
1167 /* For each column of the index, find the term of the WHERE clause that
1168 ** constraints that column. If the WHERE clause term is X=expr, then
drh0aa74ed2005-07-16 13:33:20 +00001169 ** generate code to evaluate expr and leave the result on the stack */
drh487ab3c2001-11-08 00:45:21 +00001170 for(j=0; j<nColumn; j++){
drhe8b97272005-07-19 22:22:12 +00001171 pTerm = findTerm(&wc, iCur, pIdx->aiColumn[j], loopMask, TK_EQ, TK_IN);
1172 assert( pTerm!=0 );
1173 assert( (pTerm->flags & TERM_CODED)==0 );
1174 codeEqualityTerm(pParse, pTerm, brk, pLevel);
drh75897232000-05-29 14:26:00 +00001175 }
drh6b563442001-11-07 16:48:26 +00001176 pLevel->iMem = pParse->nMem++;
danielk19774adee202004-05-08 08:23:19 +00001177 cont = pLevel->cont = sqlite3VdbeMakeLabel(v);
drh94a11212004-09-25 13:12:14 +00001178 buildIndexProbe(v, nColumn, brk, pIdx);
danielk19773d1bfea2004-05-14 11:00:53 +00001179 sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iMem, 0);
drh772ae622004-05-19 13:13:08 +00001180
drh772ae622004-05-19 13:13:08 +00001181 /* Generate code (1) to move to the first matching element of the table.
1182 ** Then generate code (2) that jumps to "brk" after the cursor is past
1183 ** the last matching element of the table. The code (1) is executed
1184 ** once to initialize the search, the code (2) is executed before each
1185 ** iteration of the scan to see if the scan has finished. */
drhc045ec52002-12-04 20:01:06 +00001186 if( pLevel->bRev ){
1187 /* Scan in reverse order */
drh9012bcb2004-12-19 00:11:35 +00001188 sqlite3VdbeAddOp(v, OP_MoveLe, iIdxCur, brk);
danielk19774adee202004-05-08 08:23:19 +00001189 start = sqlite3VdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
drh9012bcb2004-12-19 00:11:35 +00001190 sqlite3VdbeAddOp(v, OP_IdxLT, iIdxCur, brk);
drhc045ec52002-12-04 20:01:06 +00001191 pLevel->op = OP_Prev;
1192 }else{
1193 /* Scan in the forward order */
drh9012bcb2004-12-19 00:11:35 +00001194 sqlite3VdbeAddOp(v, OP_MoveGe, iIdxCur, brk);
danielk19774adee202004-05-08 08:23:19 +00001195 start = sqlite3VdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
drh9012bcb2004-12-19 00:11:35 +00001196 sqlite3VdbeOp3(v, OP_IdxGE, iIdxCur, brk, "+", P3_STATIC);
drhc045ec52002-12-04 20:01:06 +00001197 pLevel->op = OP_Next;
1198 }
drh9012bcb2004-12-19 00:11:35 +00001199 sqlite3VdbeAddOp(v, OP_RowKey, iIdxCur, 0);
danielk19774adee202004-05-08 08:23:19 +00001200 sqlite3VdbeAddOp(v, OP_IdxIsNull, nColumn, cont);
drhe6f85e72004-12-25 01:03:13 +00001201 if( !omitTable ){
drhf0863fe2005-06-12 21:35:51 +00001202 sqlite3VdbeAddOp(v, OP_IdxRowid, iIdxCur, 0);
drhe6f85e72004-12-25 01:03:13 +00001203 sqlite3VdbeAddOp(v, OP_MoveGe, iCur, 0);
drh75897232000-05-29 14:26:00 +00001204 }
drh9012bcb2004-12-19 00:11:35 +00001205 pLevel->p1 = iIdxCur;
drh6b563442001-11-07 16:48:26 +00001206 pLevel->p2 = start;
drh8aff1012001-12-22 14:49:24 +00001207 }else if( i<ARRAYSIZE(iDirectLt) && (iDirectLt[i]>=0 || iDirectGt[i]>=0) ){
1208 /* Case 3: We have an inequality comparison against the ROWID field.
1209 */
1210 int testOp = OP_Noop;
1211 int start;
drhb6c29892004-11-22 19:12:19 +00001212 int bRev = pLevel->bRev;
drh8aff1012001-12-22 14:49:24 +00001213
drh9012bcb2004-12-19 00:11:35 +00001214 assert( omitTable==0 );
danielk19774adee202004-05-08 08:23:19 +00001215 brk = pLevel->brk = sqlite3VdbeMakeLabel(v);
1216 cont = pLevel->cont = sqlite3VdbeMakeLabel(v);
drhb6c29892004-11-22 19:12:19 +00001217 if( bRev ){
1218 int t = iDirectGt[i];
1219 iDirectGt[i] = iDirectLt[i];
1220 iDirectLt[i] = t;
1221 }
drh8aff1012001-12-22 14:49:24 +00001222 if( iDirectGt[i]>=0 ){
drh94a11212004-09-25 13:12:14 +00001223 Expr *pX;
drh8aff1012001-12-22 14:49:24 +00001224 k = iDirectGt[i];
drh0aa74ed2005-07-16 13:33:20 +00001225 assert( k<wc.nTerm );
1226 pTerm = &wc.a[k];
drh0fcef5e2005-07-19 17:38:22 +00001227 pX = pTerm->pExpr;
drh94a11212004-09-25 13:12:14 +00001228 assert( pX!=0 );
drh0fcef5e2005-07-19 17:38:22 +00001229 assert( pTerm->leftCursor==iCur );
drh94a11212004-09-25 13:12:14 +00001230 sqlite3ExprCode(pParse, pX->pRight);
danielk1977d0a69322005-02-02 01:10:44 +00001231 sqlite3VdbeAddOp(v, OP_ForceInt, pX->op==TK_LE || pX->op==TK_GT, brk);
drhb6c29892004-11-22 19:12:19 +00001232 sqlite3VdbeAddOp(v, bRev ? OP_MoveLt : OP_MoveGe, iCur, brk);
tpoindex7a9b1612005-01-03 18:13:18 +00001233 VdbeComment((v, "pk"));
drh0fcef5e2005-07-19 17:38:22 +00001234 disableTerm(pLevel, pTerm);
drh8aff1012001-12-22 14:49:24 +00001235 }else{
drhb6c29892004-11-22 19:12:19 +00001236 sqlite3VdbeAddOp(v, bRev ? OP_Last : OP_Rewind, iCur, brk);
drh8aff1012001-12-22 14:49:24 +00001237 }
1238 if( iDirectLt[i]>=0 ){
drh94a11212004-09-25 13:12:14 +00001239 Expr *pX;
drh8aff1012001-12-22 14:49:24 +00001240 k = iDirectLt[i];
drh0aa74ed2005-07-16 13:33:20 +00001241 assert( k<wc.nTerm );
1242 pTerm = &wc.a[k];
drh0fcef5e2005-07-19 17:38:22 +00001243 pX = pTerm->pExpr;
drh94a11212004-09-25 13:12:14 +00001244 assert( pX!=0 );
drh0fcef5e2005-07-19 17:38:22 +00001245 assert( pTerm->leftCursor==iCur );
drh94a11212004-09-25 13:12:14 +00001246 sqlite3ExprCode(pParse, pX->pRight);
drh8aff1012001-12-22 14:49:24 +00001247 pLevel->iMem = pParse->nMem++;
danielk19774adee202004-05-08 08:23:19 +00001248 sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
drh94a11212004-09-25 13:12:14 +00001249 if( pX->op==TK_LT || pX->op==TK_GT ){
drhb6c29892004-11-22 19:12:19 +00001250 testOp = bRev ? OP_Le : OP_Ge;
drh8aff1012001-12-22 14:49:24 +00001251 }else{
drhb6c29892004-11-22 19:12:19 +00001252 testOp = bRev ? OP_Lt : OP_Gt;
drh8aff1012001-12-22 14:49:24 +00001253 }
drh0fcef5e2005-07-19 17:38:22 +00001254 disableTerm(pLevel, pTerm);
drh8aff1012001-12-22 14:49:24 +00001255 }
danielk19774adee202004-05-08 08:23:19 +00001256 start = sqlite3VdbeCurrentAddr(v);
drhb6c29892004-11-22 19:12:19 +00001257 pLevel->op = bRev ? OP_Prev : OP_Next;
drh6a3ea0e2003-05-02 14:32:12 +00001258 pLevel->p1 = iCur;
drh8aff1012001-12-22 14:49:24 +00001259 pLevel->p2 = start;
1260 if( testOp!=OP_Noop ){
drhf0863fe2005-06-12 21:35:51 +00001261 sqlite3VdbeAddOp(v, OP_Rowid, iCur, 0);
danielk19774adee202004-05-08 08:23:19 +00001262 sqlite3VdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
drhf0863fe2005-06-12 21:35:51 +00001263 sqlite3VdbeAddOp(v, testOp, 'n', brk);
drh8aff1012001-12-22 14:49:24 +00001264 }
drh8aff1012001-12-22 14:49:24 +00001265 }else if( pIdx==0 ){
drhc27a1ce2002-06-14 20:58:45 +00001266 /* Case 4: There is no usable index. We must do a complete
drh8aff1012001-12-22 14:49:24 +00001267 ** scan of the entire database table.
1268 */
1269 int start;
drhb6c29892004-11-22 19:12:19 +00001270 int opRewind;
drh8aff1012001-12-22 14:49:24 +00001271
drh9012bcb2004-12-19 00:11:35 +00001272 assert( omitTable==0 );
danielk19774adee202004-05-08 08:23:19 +00001273 brk = pLevel->brk = sqlite3VdbeMakeLabel(v);
1274 cont = pLevel->cont = sqlite3VdbeMakeLabel(v);
drhb6c29892004-11-22 19:12:19 +00001275 if( pLevel->bRev ){
1276 opRewind = OP_Last;
1277 pLevel->op = OP_Prev;
1278 }else{
1279 opRewind = OP_Rewind;
1280 pLevel->op = OP_Next;
1281 }
1282 sqlite3VdbeAddOp(v, opRewind, iCur, brk);
danielk19774adee202004-05-08 08:23:19 +00001283 start = sqlite3VdbeCurrentAddr(v);
drh6a3ea0e2003-05-02 14:32:12 +00001284 pLevel->p1 = iCur;
drh8aff1012001-12-22 14:49:24 +00001285 pLevel->p2 = start;
drh487ab3c2001-11-08 00:45:21 +00001286 }else{
drhc27a1ce2002-06-14 20:58:45 +00001287 /* Case 5: The WHERE clause term that refers to the right-most
1288 ** column of the index is an inequality. For example, if
1289 ** the index is on (x,y,z) and the WHERE clause is of the
1290 ** form "x=5 AND y<10" then this case is used. Only the
1291 ** right-most column can be an inequality - the rest must
1292 ** use the "==" operator.
drhe3184742002-06-19 14:27:05 +00001293 **
1294 ** This case is also used when there are no WHERE clause
1295 ** constraints but an index is selected anyway, in order
1296 ** to force the output order to conform to an ORDER BY.
drh487ab3c2001-11-08 00:45:21 +00001297 */
1298 int score = pLevel->score;
drh51669862004-12-18 18:40:26 +00001299 int nEqColumn = score/32;
drh487ab3c2001-11-08 00:45:21 +00001300 int start;
danielk1977f7df9cc2004-06-16 12:02:47 +00001301 int leFlag=0, geFlag=0;
drh487ab3c2001-11-08 00:45:21 +00001302 int testOp;
1303
1304 /* Evaluate the equality constraints
1305 */
1306 for(j=0; j<nEqColumn; j++){
drhe8b97272005-07-19 22:22:12 +00001307 pTerm = findTerm(&wc, iCur, pIdx->aiColumn[j], loopMask, TK_EQ, TK_EQ);
1308 assert( pTerm!=0 );
1309 assert( (pTerm->flags & TERM_CODED)==0 );
1310 sqlite3ExprCode(pParse, pTerm->pExpr->pRight);
1311 disableTerm(pLevel, pTerm);
drh487ab3c2001-11-08 00:45:21 +00001312 }
1313
drhc27a1ce2002-06-14 20:58:45 +00001314 /* Duplicate the equality term values because they will all be
drh487ab3c2001-11-08 00:45:21 +00001315 ** used twice: once to make the termination key and once to make the
1316 ** start key.
1317 */
1318 for(j=0; j<nEqColumn; j++){
danielk19774adee202004-05-08 08:23:19 +00001319 sqlite3VdbeAddOp(v, OP_Dup, nEqColumn-1, 0);
drh487ab3c2001-11-08 00:45:21 +00001320 }
1321
drhc045ec52002-12-04 20:01:06 +00001322 /* Labels for the beginning and end of the loop
1323 */
danielk19774adee202004-05-08 08:23:19 +00001324 cont = pLevel->cont = sqlite3VdbeMakeLabel(v);
1325 brk = pLevel->brk = sqlite3VdbeMakeLabel(v);
drhc045ec52002-12-04 20:01:06 +00001326
drh487ab3c2001-11-08 00:45:21 +00001327 /* Generate the termination key. This is the key value that
1328 ** will end the search. There is no termination key if there
drhc27a1ce2002-06-14 20:58:45 +00001329 ** are no equality terms and no "X<..." term.
drhc045ec52002-12-04 20:01:06 +00001330 **
1331 ** 2002-Dec-04: On a reverse-order scan, the so-called "termination"
1332 ** key computed here really ends up being the start key.
drh487ab3c2001-11-08 00:45:21 +00001333 */
drh51669862004-12-18 18:40:26 +00001334 if( (score & 4)!=0 ){
drhe8b97272005-07-19 22:22:12 +00001335 Expr *pX;
1336 pTerm = findTerm(&wc, iCur, pIdx->aiColumn[j], loopMask, TK_LT, TK_LE);
1337 assert( pTerm!=0 );
1338 pX = pTerm->pExpr;
1339 assert( (pTerm->flags & TERM_CODED)==0 );
1340 sqlite3ExprCode(pParse, pX->pRight);
1341 leFlag = pX->op==TK_LE;
1342 disableTerm(pLevel, pTerm);
drh487ab3c2001-11-08 00:45:21 +00001343 testOp = OP_IdxGE;
1344 }else{
1345 testOp = nEqColumn>0 ? OP_IdxGE : OP_Noop;
1346 leFlag = 1;
1347 }
1348 if( testOp!=OP_Noop ){
drh51669862004-12-18 18:40:26 +00001349 int nCol = nEqColumn + ((score & 4)!=0);
drh487ab3c2001-11-08 00:45:21 +00001350 pLevel->iMem = pParse->nMem++;
drh94a11212004-09-25 13:12:14 +00001351 buildIndexProbe(v, nCol, brk, pIdx);
drhc045ec52002-12-04 20:01:06 +00001352 if( pLevel->bRev ){
drh7cf6e4d2004-05-19 14:56:55 +00001353 int op = leFlag ? OP_MoveLe : OP_MoveLt;
drh9012bcb2004-12-19 00:11:35 +00001354 sqlite3VdbeAddOp(v, op, iIdxCur, brk);
drhc045ec52002-12-04 20:01:06 +00001355 }else{
danielk19774adee202004-05-08 08:23:19 +00001356 sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
drhc045ec52002-12-04 20:01:06 +00001357 }
1358 }else if( pLevel->bRev ){
drh9012bcb2004-12-19 00:11:35 +00001359 sqlite3VdbeAddOp(v, OP_Last, iIdxCur, brk);
drh487ab3c2001-11-08 00:45:21 +00001360 }
1361
1362 /* Generate the start key. This is the key that defines the lower
drhc27a1ce2002-06-14 20:58:45 +00001363 ** bound on the search. There is no start key if there are no
1364 ** equality terms and if there is no "X>..." term. In
drh487ab3c2001-11-08 00:45:21 +00001365 ** that case, generate a "Rewind" instruction in place of the
1366 ** start key search.
drhc045ec52002-12-04 20:01:06 +00001367 **
1368 ** 2002-Dec-04: In the case of a reverse-order search, the so-called
1369 ** "start" key really ends up being used as the termination key.
drh487ab3c2001-11-08 00:45:21 +00001370 */
drh51669862004-12-18 18:40:26 +00001371 if( (score & 8)!=0 ){
drhe8b97272005-07-19 22:22:12 +00001372 Expr *pX;
1373 pTerm = findTerm(&wc, iCur, pIdx->aiColumn[j], loopMask, TK_GT, TK_GE);
1374 assert( pTerm!=0 );
1375 pX = pTerm->pExpr;
1376 assert( (pTerm->flags & TERM_CODED)==0 );
1377 sqlite3ExprCode(pParse, pX->pRight);
1378 geFlag = pX->op==TK_GE;
1379 disableTerm(pLevel, pTerm);
drh7900ead2001-11-12 13:51:43 +00001380 }else{
1381 geFlag = 1;
drh487ab3c2001-11-08 00:45:21 +00001382 }
drh51669862004-12-18 18:40:26 +00001383 if( nEqColumn>0 || (score&8)!=0 ){
1384 int nCol = nEqColumn + ((score&8)!=0);
drh94a11212004-09-25 13:12:14 +00001385 buildIndexProbe(v, nCol, brk, pIdx);
drhc045ec52002-12-04 20:01:06 +00001386 if( pLevel->bRev ){
1387 pLevel->iMem = pParse->nMem++;
danielk19774adee202004-05-08 08:23:19 +00001388 sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
drhc045ec52002-12-04 20:01:06 +00001389 testOp = OP_IdxLT;
1390 }else{
drh7cf6e4d2004-05-19 14:56:55 +00001391 int op = geFlag ? OP_MoveGe : OP_MoveGt;
drh9012bcb2004-12-19 00:11:35 +00001392 sqlite3VdbeAddOp(v, op, iIdxCur, brk);
drhc045ec52002-12-04 20:01:06 +00001393 }
1394 }else if( pLevel->bRev ){
1395 testOp = OP_Noop;
drh487ab3c2001-11-08 00:45:21 +00001396 }else{
drh9012bcb2004-12-19 00:11:35 +00001397 sqlite3VdbeAddOp(v, OP_Rewind, iIdxCur, brk);
drh487ab3c2001-11-08 00:45:21 +00001398 }
1399
1400 /* Generate the the top of the loop. If there is a termination
1401 ** key we have to test for that key and abort at the top of the
1402 ** loop.
1403 */
danielk19774adee202004-05-08 08:23:19 +00001404 start = sqlite3VdbeCurrentAddr(v);
drh487ab3c2001-11-08 00:45:21 +00001405 if( testOp!=OP_Noop ){
danielk19774adee202004-05-08 08:23:19 +00001406 sqlite3VdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
drh9012bcb2004-12-19 00:11:35 +00001407 sqlite3VdbeAddOp(v, testOp, iIdxCur, brk);
danielk19773d1bfea2004-05-14 11:00:53 +00001408 if( (leFlag && !pLevel->bRev) || (!geFlag && pLevel->bRev) ){
1409 sqlite3VdbeChangeP3(v, -1, "+", P3_STATIC);
1410 }
drh487ab3c2001-11-08 00:45:21 +00001411 }
drh9012bcb2004-12-19 00:11:35 +00001412 sqlite3VdbeAddOp(v, OP_RowKey, iIdxCur, 0);
drh51669862004-12-18 18:40:26 +00001413 sqlite3VdbeAddOp(v, OP_IdxIsNull, nEqColumn + ((score&4)!=0), cont);
drhe6f85e72004-12-25 01:03:13 +00001414 if( !omitTable ){
drhf0863fe2005-06-12 21:35:51 +00001415 sqlite3VdbeAddOp(v, OP_IdxRowid, iIdxCur, 0);
drhe6f85e72004-12-25 01:03:13 +00001416 sqlite3VdbeAddOp(v, OP_MoveGe, iCur, 0);
drh487ab3c2001-11-08 00:45:21 +00001417 }
1418
1419 /* Record the instruction used to terminate the loop.
1420 */
drhc045ec52002-12-04 20:01:06 +00001421 pLevel->op = pLevel->bRev ? OP_Prev : OP_Next;
drh9012bcb2004-12-19 00:11:35 +00001422 pLevel->p1 = iIdxCur;
drh487ab3c2001-11-08 00:45:21 +00001423 pLevel->p2 = start;
drh75897232000-05-29 14:26:00 +00001424 }
drh0fcef5e2005-07-19 17:38:22 +00001425 loopMask &= ~getMask(&maskSet, iCur);
drh75897232000-05-29 14:26:00 +00001426
1427 /* Insert code to test every subexpression that can be completely
1428 ** computed using the current set of tables.
1429 */
drh0fcef5e2005-07-19 17:38:22 +00001430 for(pTerm=wc.a, j=wc.nTerm; j>0; j--, pTerm++){
1431 Expr *pE;
1432 if( pTerm->flags & (TERM_VIRTUAL|TERM_CODED) ) continue;
1433 if( (pTerm->prereqAll & loopMask)!=0 ) continue;
1434 pE = pTerm->pExpr;
1435 assert( pE!=0 );
drh392e5972005-07-08 14:14:22 +00001436 if( pLevel->iLeftJoin && !ExprHasProperty(pE, EP_FromJoin) ){
drh1f162302002-10-27 19:35:33 +00001437 continue;
1438 }
drh392e5972005-07-08 14:14:22 +00001439 sqlite3ExprIfFalse(pParse, pE, cont, 1);
drh0fcef5e2005-07-19 17:38:22 +00001440 pTerm->flags |= TERM_CODED;
drh75897232000-05-29 14:26:00 +00001441 }
1442 brk = cont;
drhad2d8302002-05-24 20:31:36 +00001443
1444 /* For a LEFT OUTER JOIN, generate code that will record the fact that
1445 ** at least one row of the right table has matched the left table.
1446 */
1447 if( pLevel->iLeftJoin ){
danielk19774adee202004-05-08 08:23:19 +00001448 pLevel->top = sqlite3VdbeCurrentAddr(v);
1449 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1450 sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iLeftJoin, 1);
drhad6d9462004-09-19 02:15:24 +00001451 VdbeComment((v, "# record LEFT JOIN hit"));
drh0aa74ed2005-07-16 13:33:20 +00001452 for(pTerm=wc.a, j=0; j<wc.nTerm; j++, pTerm++){
drh0fcef5e2005-07-19 17:38:22 +00001453 if( pTerm->flags & (TERM_VIRTUAL|TERM_CODED) ) continue;
1454 if( (pTerm->prereqAll & loopMask)!=0 ) continue;
1455 assert( pTerm->pExpr );
1456 sqlite3ExprIfFalse(pParse, pTerm->pExpr, cont, 1);
1457 pTerm->flags |= TERM_CODED;
drh1cc093c2002-06-24 22:01:57 +00001458 }
drhad2d8302002-05-24 20:31:36 +00001459 }
drh75897232000-05-29 14:26:00 +00001460 }
1461 pWInfo->iContinue = cont;
drh6a3ea0e2003-05-02 14:32:12 +00001462 freeMaskSet(&maskSet);
drh0aa74ed2005-07-16 13:33:20 +00001463 whereClauseClear(&wc);
drh75897232000-05-29 14:26:00 +00001464 return pWInfo;
1465}
1466
1467/*
drhc27a1ce2002-06-14 20:58:45 +00001468** Generate the end of the WHERE loop. See comments on
danielk19774adee202004-05-08 08:23:19 +00001469** sqlite3WhereBegin() for additional information.
drh75897232000-05-29 14:26:00 +00001470*/
danielk19774adee202004-05-08 08:23:19 +00001471void sqlite3WhereEnd(WhereInfo *pWInfo){
drh75897232000-05-29 14:26:00 +00001472 Vdbe *v = pWInfo->pParse->pVdbe;
drh19a775c2000-06-05 18:54:46 +00001473 int i;
drh6b563442001-11-07 16:48:26 +00001474 WhereLevel *pLevel;
drhad3cab52002-05-24 02:04:32 +00001475 SrcList *pTabList = pWInfo->pTabList;
drh9012bcb2004-12-19 00:11:35 +00001476 struct SrcList_item *pTabItem;
drh19a775c2000-06-05 18:54:46 +00001477
drh9012bcb2004-12-19 00:11:35 +00001478 /* Generate loop termination code.
1479 */
drhad3cab52002-05-24 02:04:32 +00001480 for(i=pTabList->nSrc-1; i>=0; i--){
drh6b563442001-11-07 16:48:26 +00001481 pLevel = &pWInfo->a[i];
danielk19774adee202004-05-08 08:23:19 +00001482 sqlite3VdbeResolveLabel(v, pLevel->cont);
drh6b563442001-11-07 16:48:26 +00001483 if( pLevel->op!=OP_Noop ){
danielk19774adee202004-05-08 08:23:19 +00001484 sqlite3VdbeAddOp(v, pLevel->op, pLevel->p1, pLevel->p2);
drh19a775c2000-06-05 18:54:46 +00001485 }
danielk19774adee202004-05-08 08:23:19 +00001486 sqlite3VdbeResolveLabel(v, pLevel->brk);
drhd99f7062002-06-08 23:25:08 +00001487 if( pLevel->inOp!=OP_Noop ){
danielk19774adee202004-05-08 08:23:19 +00001488 sqlite3VdbeAddOp(v, pLevel->inOp, pLevel->inP1, pLevel->inP2);
drhd99f7062002-06-08 23:25:08 +00001489 }
drhad2d8302002-05-24 20:31:36 +00001490 if( pLevel->iLeftJoin ){
1491 int addr;
danielk19774adee202004-05-08 08:23:19 +00001492 addr = sqlite3VdbeAddOp(v, OP_MemLoad, pLevel->iLeftJoin, 0);
drh9012bcb2004-12-19 00:11:35 +00001493 sqlite3VdbeAddOp(v, OP_NotNull, 1, addr+4 + (pLevel->iIdxCur>=0));
danielk19774adee202004-05-08 08:23:19 +00001494 sqlite3VdbeAddOp(v, OP_NullRow, pTabList->a[i].iCursor, 0);
drh9012bcb2004-12-19 00:11:35 +00001495 if( pLevel->iIdxCur>=0 ){
1496 sqlite3VdbeAddOp(v, OP_NullRow, pLevel->iIdxCur, 0);
drh7f09b3e2002-08-13 13:15:49 +00001497 }
danielk19774adee202004-05-08 08:23:19 +00001498 sqlite3VdbeAddOp(v, OP_Goto, 0, pLevel->top);
drhad2d8302002-05-24 20:31:36 +00001499 }
drh19a775c2000-06-05 18:54:46 +00001500 }
drh9012bcb2004-12-19 00:11:35 +00001501
1502 /* The "break" point is here, just past the end of the outer loop.
1503 ** Set it.
1504 */
danielk19774adee202004-05-08 08:23:19 +00001505 sqlite3VdbeResolveLabel(v, pWInfo->iBreak);
drh9012bcb2004-12-19 00:11:35 +00001506
drhacf3b982005-01-03 01:27:18 +00001507 /* Close all of the cursors that were opend by sqlite3WhereBegin.
drh9012bcb2004-12-19 00:11:35 +00001508 */
1509 pLevel = pWInfo->a;
1510 pTabItem = pTabList->a;
1511 for(i=0; i<pTabList->nSrc; i++, pTabItem++, pLevel++){
1512 Table *pTab = pTabItem->pTab;
drh5cf590c2003-04-24 01:45:04 +00001513 assert( pTab!=0 );
1514 if( pTab->isTransient || pTab->pSelect ) continue;
drh9012bcb2004-12-19 00:11:35 +00001515 if( (pLevel->score & 1)==0 ){
1516 sqlite3VdbeAddOp(v, OP_Close, pTabItem->iCursor, 0);
1517 }
drh6b563442001-11-07 16:48:26 +00001518 if( pLevel->pIdx!=0 ){
drh9012bcb2004-12-19 00:11:35 +00001519 sqlite3VdbeAddOp(v, OP_Close, pLevel->iIdxCur, 0);
1520 }
1521
drhacf3b982005-01-03 01:27:18 +00001522 /* Make cursor substitutions for cases where we want to use
drh9012bcb2004-12-19 00:11:35 +00001523 ** just the index and never reference the table.
1524 **
1525 ** Calls to the code generator in between sqlite3WhereBegin and
1526 ** sqlite3WhereEnd will have created code that references the table
1527 ** directly. This loop scans all that code looking for opcodes
1528 ** that reference the table and converts them into opcodes that
1529 ** reference the index.
1530 */
1531 if( pLevel->score & 1 ){
1532 int i, j, last;
1533 VdbeOp *pOp;
1534 Index *pIdx = pLevel->pIdx;
1535
1536 assert( pIdx!=0 );
1537 pOp = sqlite3VdbeGetOp(v, pWInfo->iTop);
1538 last = sqlite3VdbeCurrentAddr(v);
1539 for(i=pWInfo->iTop; i<last; i++, pOp++){
1540 if( pOp->p1!=pLevel->iTabCur ) continue;
1541 if( pOp->opcode==OP_Column ){
1542 pOp->p1 = pLevel->iIdxCur;
1543 for(j=0; j<pIdx->nColumn; j++){
1544 if( pOp->p2==pIdx->aiColumn[j] ){
1545 pOp->p2 = j;
1546 break;
1547 }
1548 }
drhf0863fe2005-06-12 21:35:51 +00001549 }else if( pOp->opcode==OP_Rowid ){
drh9012bcb2004-12-19 00:11:35 +00001550 pOp->p1 = pLevel->iIdxCur;
drhf0863fe2005-06-12 21:35:51 +00001551 pOp->opcode = OP_IdxRowid;
danielk19776c18b6e2005-01-30 09:17:58 +00001552 }else if( pOp->opcode==OP_NullRow ){
1553 pOp->opcode = OP_Noop;
drh9012bcb2004-12-19 00:11:35 +00001554 }
1555 }
drh6b563442001-11-07 16:48:26 +00001556 }
drh19a775c2000-06-05 18:54:46 +00001557 }
drh9012bcb2004-12-19 00:11:35 +00001558
1559 /* Final cleanup
1560 */
drh75897232000-05-29 14:26:00 +00001561 sqliteFree(pWInfo);
1562 return;
1563}