blob: c3f8082fc2be9e496dea5cf4b9a8f10a718a95dc [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
drhb2fe7d82003-04-20 17:29:23 +000013** the WHERE clause of SQL statements.
drh75897232000-05-29 14:26:00 +000014**
danielk1977299b1872004-11-22 10:02:10 +000015** $Id: where.c,v 1.118 2004/11/22 10:02:20 danielk1977 Exp $
drh75897232000-05-29 14:26:00 +000016*/
17#include "sqliteInt.h"
18
19/*
20** The query generator uses an array of instances of this structure to
21** help it analyze the subexpressions of the WHERE clause. Each WHERE
22** clause subexpression is separated from the others by an AND operator.
23*/
24typedef struct ExprInfo ExprInfo;
25struct ExprInfo {
26 Expr *p; /* Pointer to the subexpression */
drhe3184742002-06-19 14:27:05 +000027 u8 indexable; /* True if this subexprssion is usable by an index */
28 short int idxLeft; /* p->pLeft is a column in this table number. -1 if
drh967e8b72000-06-21 13:59:10 +000029 ** p->pLeft is not the column of any table */
drhe3184742002-06-19 14:27:05 +000030 short int idxRight; /* p->pRight is a column in this table number. -1 if
drh967e8b72000-06-21 13:59:10 +000031 ** p->pRight is not the column of any table */
drhe3184742002-06-19 14:27:05 +000032 unsigned prereqLeft; /* Bitmask of tables referenced by p->pLeft */
33 unsigned prereqRight; /* Bitmask of tables referenced by p->pRight */
34 unsigned prereqAll; /* Bitmask of tables referenced by p */
drh75897232000-05-29 14:26:00 +000035};
36
37/*
drh6a3ea0e2003-05-02 14:32:12 +000038** An instance of the following structure keeps track of a mapping
39** between VDBE cursor numbers and bitmasks. The VDBE cursor numbers
40** are small integers contained in SrcList_item.iCursor and Expr.iTable
41** fields. For any given WHERE clause, we want to track which cursors
42** are being used, so we assign a single bit in a 32-bit word to track
43** that cursor. Then a 32-bit integer is able to show the set of all
44** cursors being used.
45*/
46typedef struct ExprMaskSet ExprMaskSet;
47struct ExprMaskSet {
48 int n; /* Number of assigned cursor values */
drh8feb4b12004-07-19 02:12:14 +000049 int ix[31]; /* Cursor assigned to each bit */
drh6a3ea0e2003-05-02 14:32:12 +000050};
51
52/*
drh75897232000-05-29 14:26:00 +000053** Determine the number of elements in an array.
54*/
55#define ARRAYSIZE(X) (sizeof(X)/sizeof(X[0]))
56
57/*
58** This routine is used to divide the WHERE expression into subexpressions
59** separated by the AND operator.
60**
61** aSlot[] is an array of subexpressions structures.
62** There are nSlot spaces left in this array. This routine attempts to
63** split pExpr into subexpressions and fills aSlot[] with those subexpressions.
64** The return value is the number of slots filled.
65*/
66static int exprSplit(int nSlot, ExprInfo *aSlot, Expr *pExpr){
67 int cnt = 0;
68 if( pExpr==0 || nSlot<1 ) return 0;
69 if( nSlot==1 || pExpr->op!=TK_AND ){
70 aSlot[0].p = pExpr;
71 return 1;
72 }
73 if( pExpr->pLeft->op!=TK_AND ){
74 aSlot[0].p = pExpr->pLeft;
75 cnt = 1 + exprSplit(nSlot-1, &aSlot[1], pExpr->pRight);
76 }else{
drhdcd997e2003-01-31 17:21:49 +000077 cnt = exprSplit(nSlot, aSlot, pExpr->pLeft);
78 cnt += exprSplit(nSlot-cnt, &aSlot[cnt], pExpr->pRight);
drh75897232000-05-29 14:26:00 +000079 }
80 return cnt;
81}
82
83/*
drh6a3ea0e2003-05-02 14:32:12 +000084** Initialize an expression mask set
85*/
86#define initMaskSet(P) memset(P, 0, sizeof(*P))
87
88/*
89** Return the bitmask for the given cursor. Assign a new bitmask
90** if this is the first time the cursor has been seen.
91*/
92static int getMask(ExprMaskSet *pMaskSet, int iCursor){
93 int i;
94 for(i=0; i<pMaskSet->n; i++){
95 if( pMaskSet->ix[i]==iCursor ) return 1<<i;
96 }
97 if( i==pMaskSet->n && i<ARRAYSIZE(pMaskSet->ix) ){
98 pMaskSet->n++;
99 pMaskSet->ix[i] = iCursor;
100 return 1<<i;
101 }
102 return 0;
103}
104
105/*
106** Destroy an expression mask set
107*/
108#define freeMaskSet(P) /* NO-OP */
109
110/*
drh75897232000-05-29 14:26:00 +0000111** This routine walks (recursively) an expression tree and generates
112** a bitmask indicating which tables are used in that expression
drh6a3ea0e2003-05-02 14:32:12 +0000113** tree.
drh75897232000-05-29 14:26:00 +0000114**
115** In order for this routine to work, the calling function must have
danielk19774adee202004-05-08 08:23:19 +0000116** previously invoked sqlite3ExprResolveIds() on the expression. See
drh75897232000-05-29 14:26:00 +0000117** the header comment on that routine for additional information.
danielk19774adee202004-05-08 08:23:19 +0000118** The sqlite3ExprResolveIds() routines looks for column names and
drh6a3ea0e2003-05-02 14:32:12 +0000119** sets their opcodes to TK_COLUMN and their Expr.iTable fields to
120** the VDBE cursor number of the table.
drh75897232000-05-29 14:26:00 +0000121*/
drh6a3ea0e2003-05-02 14:32:12 +0000122static int exprTableUsage(ExprMaskSet *pMaskSet, Expr *p){
drh75897232000-05-29 14:26:00 +0000123 unsigned int mask = 0;
124 if( p==0 ) return 0;
drh967e8b72000-06-21 13:59:10 +0000125 if( p->op==TK_COLUMN ){
drh8feb4b12004-07-19 02:12:14 +0000126 mask = getMask(pMaskSet, p->iTable);
127 if( mask==0 ) mask = -1;
128 return mask;
drh75897232000-05-29 14:26:00 +0000129 }
130 if( p->pRight ){
drh6a3ea0e2003-05-02 14:32:12 +0000131 mask = exprTableUsage(pMaskSet, p->pRight);
drh75897232000-05-29 14:26:00 +0000132 }
133 if( p->pLeft ){
drh6a3ea0e2003-05-02 14:32:12 +0000134 mask |= exprTableUsage(pMaskSet, p->pLeft);
drh75897232000-05-29 14:26:00 +0000135 }
drhdd579122002-04-02 01:58:57 +0000136 if( p->pList ){
137 int i;
138 for(i=0; i<p->pList->nExpr; i++){
drh6a3ea0e2003-05-02 14:32:12 +0000139 mask |= exprTableUsage(pMaskSet, p->pList->a[i].pExpr);
drhdd579122002-04-02 01:58:57 +0000140 }
141 }
drh75897232000-05-29 14:26:00 +0000142 return mask;
143}
144
145/*
drh487ab3c2001-11-08 00:45:21 +0000146** Return TRUE if the given operator is one of the operators that is
147** allowed for an indexable WHERE clause. The allowed operators are
drhc27a1ce2002-06-14 20:58:45 +0000148** "=", "<", ">", "<=", ">=", and "IN".
drh487ab3c2001-11-08 00:45:21 +0000149*/
150static int allowedOp(int op){
drh9a432672004-10-04 13:38:09 +0000151 assert( TK_GT==TK_LE-1 && TK_LE==TK_LT-1 && TK_LT==TK_GE-1 && TK_EQ==TK_GT-1);
152 return op==TK_IN || (op>=TK_EQ && op<=TK_GE);
drh487ab3c2001-11-08 00:45:21 +0000153}
154
155/*
drh193bd772004-07-20 18:23:14 +0000156** Swap two integers.
157*/
158#define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;}
159
160/*
161** Return the index in the SrcList that uses cursor iCur. If iCur is
162** used by the first entry in SrcList return 0. If iCur is used by
163** the second entry return 1. And so forth.
164**
165** SrcList is the set of tables in the FROM clause in the order that
166** they will be processed. The value returned here gives us an index
167** of which tables will be processed first.
168*/
169static int tableOrder(SrcList *pList, int iCur){
170 int i;
171 for(i=0; i<pList->nSrc; i++){
172 if( pList->a[i].iCursor==iCur ) return i;
173 }
174 return -1;
175}
176
177/*
drh75897232000-05-29 14:26:00 +0000178** The input to this routine is an ExprInfo structure with only the
179** "p" field filled in. The job of this routine is to analyze the
180** subexpression and populate all the other fields of the ExprInfo
181** structure.
182*/
drh193bd772004-07-20 18:23:14 +0000183static void exprAnalyze(SrcList *pSrc, ExprMaskSet *pMaskSet, ExprInfo *pInfo){
drh75897232000-05-29 14:26:00 +0000184 Expr *pExpr = pInfo->p;
drh6a3ea0e2003-05-02 14:32:12 +0000185 pInfo->prereqLeft = exprTableUsage(pMaskSet, pExpr->pLeft);
186 pInfo->prereqRight = exprTableUsage(pMaskSet, pExpr->pRight);
187 pInfo->prereqAll = exprTableUsage(pMaskSet, pExpr);
drh75897232000-05-29 14:26:00 +0000188 pInfo->indexable = 0;
189 pInfo->idxLeft = -1;
190 pInfo->idxRight = -1;
drh487ab3c2001-11-08 00:45:21 +0000191 if( allowedOp(pExpr->op) && (pInfo->prereqRight & pInfo->prereqLeft)==0 ){
drhd99f7062002-06-08 23:25:08 +0000192 if( pExpr->pRight && pExpr->pRight->op==TK_COLUMN ){
drh6a3ea0e2003-05-02 14:32:12 +0000193 pInfo->idxRight = pExpr->pRight->iTable;
drh75897232000-05-29 14:26:00 +0000194 pInfo->indexable = 1;
195 }
drh967e8b72000-06-21 13:59:10 +0000196 if( pExpr->pLeft->op==TK_COLUMN ){
drh6a3ea0e2003-05-02 14:32:12 +0000197 pInfo->idxLeft = pExpr->pLeft->iTable;
drh75897232000-05-29 14:26:00 +0000198 pInfo->indexable = 1;
199 }
200 }
drh193bd772004-07-20 18:23:14 +0000201 if( pInfo->indexable ){
202 assert( pInfo->idxLeft!=pInfo->idxRight );
203
204 /* We want the expression to be of the form "X = expr", not "expr = X".
205 ** So flip it over if necessary. If the expression is "X = Y", then
206 ** we want Y to come from an earlier table than X.
207 **
208 ** The collating sequence rule is to always choose the left expression.
209 ** So if we do a flip, we also have to move the collating sequence.
210 */
211 if( tableOrder(pSrc,pInfo->idxLeft)<tableOrder(pSrc,pInfo->idxRight) ){
212 assert( pExpr->op!=TK_IN );
213 SWAP(CollSeq*,pExpr->pRight->pColl,pExpr->pLeft->pColl);
214 SWAP(Expr*,pExpr->pRight,pExpr->pLeft);
drh9a432672004-10-04 13:38:09 +0000215 if( pExpr->op>=TK_GT ){
216 assert( TK_LT==TK_GT+2 );
217 assert( TK_GE==TK_LE+2 );
218 assert( TK_GT>TK_EQ );
219 assert( TK_GT<TK_LE );
220 assert( pExpr->op>=TK_GT && pExpr->op<=TK_GE );
221 pExpr->op = ((pExpr->op-TK_GT)^2)+TK_GT;
drh193bd772004-07-20 18:23:14 +0000222 }
223 SWAP(unsigned, pInfo->prereqLeft, pInfo->prereqRight);
224 SWAP(short int, pInfo->idxLeft, pInfo->idxRight);
225 }
226 }
227
drh75897232000-05-29 14:26:00 +0000228}
229
230/*
drhe3184742002-06-19 14:27:05 +0000231** pOrderBy is an ORDER BY clause from a SELECT statement. pTab is the
232** left-most table in the FROM clause of that same SELECT statement and
233** the table has a cursor number of "base".
234**
235** This routine attempts to find an index for pTab that generates the
236** correct record sequence for the given ORDER BY clause. The return value
237** is a pointer to an index that does the job. NULL is returned if the
238** table has no index that will generate the correct sort order.
239**
240** If there are two or more indices that generate the correct sort order
241** and pPreferredIdx is one of those indices, then return pPreferredIdx.
drhdd4852c2002-12-04 21:50:16 +0000242**
243** nEqCol is the number of columns of pPreferredIdx that are used as
244** equality constraints. Any index returned must have exactly this same
245** set of columns. The ORDER BY clause only matches index columns beyond the
246** the first nEqCol columns.
247**
248** All terms of the ORDER BY clause must be either ASC or DESC. The
249** *pbRev value is set to 1 if the ORDER BY clause is all DESC and it is
250** set to 0 if the ORDER BY clause is all ASC.
drhe3184742002-06-19 14:27:05 +0000251*/
252static Index *findSortingIndex(
danielk1977d2b65b92004-06-10 10:51:47 +0000253 Parse *pParse,
drhe3184742002-06-19 14:27:05 +0000254 Table *pTab, /* The table to be sorted */
255 int base, /* Cursor number for pTab */
256 ExprList *pOrderBy, /* The ORDER BY clause */
drhc045ec52002-12-04 20:01:06 +0000257 Index *pPreferredIdx, /* Use this index, if possible and not NULL */
drhdd4852c2002-12-04 21:50:16 +0000258 int nEqCol, /* Number of index columns used with == constraints */
drhc045ec52002-12-04 20:01:06 +0000259 int *pbRev /* Set to 1 if ORDER BY is DESC */
drhe3184742002-06-19 14:27:05 +0000260){
drhdd4852c2002-12-04 21:50:16 +0000261 int i, j;
drhe3184742002-06-19 14:27:05 +0000262 Index *pMatch;
263 Index *pIdx;
drhc045ec52002-12-04 20:01:06 +0000264 int sortOrder;
drh9bb575f2004-09-06 17:24:11 +0000265 sqlite3 *db = pParse->db;
drhe3184742002-06-19 14:27:05 +0000266
267 assert( pOrderBy!=0 );
268 assert( pOrderBy->nExpr>0 );
drhd3d39e92004-05-20 22:16:29 +0000269 sortOrder = pOrderBy->a[0].sortOrder;
drhe3184742002-06-19 14:27:05 +0000270 for(i=0; i<pOrderBy->nExpr; i++){
271 Expr *p;
drhd3d39e92004-05-20 22:16:29 +0000272 if( pOrderBy->a[i].sortOrder!=sortOrder ){
drhc045ec52002-12-04 20:01:06 +0000273 /* Indices can only be used if all ORDER BY terms are either
274 ** DESC or ASC. Indices cannot be used on a mixture. */
drhe3184742002-06-19 14:27:05 +0000275 return 0;
276 }
277 p = pOrderBy->a[i].pExpr;
278 if( p->op!=TK_COLUMN || p->iTable!=base ){
279 /* Can not use an index sort on anything that is not a column in the
280 ** left-most table of the FROM clause */
281 return 0;
282 }
283 }
danielk19770202b292004-06-09 09:55:16 +0000284
drhe3184742002-06-19 14:27:05 +0000285 /* If we get this far, it means the ORDER BY clause consists only of
286 ** ascending columns in the left-most table of the FROM clause. Now
287 ** check for a matching index.
288 */
289 pMatch = 0;
290 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drhdd4852c2002-12-04 21:50:16 +0000291 int nExpr = pOrderBy->nExpr;
292 if( pIdx->nColumn < nEqCol || pIdx->nColumn < nExpr ) continue;
293 for(i=j=0; i<nEqCol; i++){
danielk1977d2b65b92004-06-10 10:51:47 +0000294 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pOrderBy->a[j].pExpr);
danielk19770202b292004-06-09 09:55:16 +0000295 if( !pColl ) pColl = db->pDfltColl;
drhdd4852c2002-12-04 21:50:16 +0000296 if( pPreferredIdx->aiColumn[i]!=pIdx->aiColumn[i] ) break;
danielk19770202b292004-06-09 09:55:16 +0000297 if( pPreferredIdx->keyInfo.aColl[i]!=pIdx->keyInfo.aColl[i] ) break;
298 if( j<nExpr &&
299 pOrderBy->a[j].pExpr->iColumn==pIdx->aiColumn[i] &&
300 pColl==pIdx->keyInfo.aColl[i]
301 ){
302 j++;
303 }
drhe3184742002-06-19 14:27:05 +0000304 }
drhdd4852c2002-12-04 21:50:16 +0000305 if( i<nEqCol ) continue;
306 for(i=0; i+j<nExpr; i++){
danielk1977d2b65b92004-06-10 10:51:47 +0000307 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pOrderBy->a[i+j].pExpr);
danielk19770202b292004-06-09 09:55:16 +0000308 if( !pColl ) pColl = db->pDfltColl;
309 if( pOrderBy->a[i+j].pExpr->iColumn!=pIdx->aiColumn[i+nEqCol] ||
310 pColl!=pIdx->keyInfo.aColl[i+nEqCol] ) break;
drhdd4852c2002-12-04 21:50:16 +0000311 }
312 if( i+j>=nExpr ){
drhe3184742002-06-19 14:27:05 +0000313 pMatch = pIdx;
314 if( pIdx==pPreferredIdx ) break;
315 }
316 }
drhc045ec52002-12-04 20:01:06 +0000317 if( pMatch && pbRev ){
318 *pbRev = sortOrder==SQLITE_SO_DESC;
319 }
drhe3184742002-06-19 14:27:05 +0000320 return pMatch;
321}
322
323/*
drh2ffb1182004-07-19 19:14:01 +0000324** Disable a term in the WHERE clause. Except, do not disable the term
325** if it controls a LEFT OUTER JOIN and it did not originate in the ON
326** or USING clause of that join.
327**
328** Consider the term t2.z='ok' in the following queries:
329**
330** (1) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x WHERE t2.z='ok'
331** (2) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x AND t2.z='ok'
332** (3) SELECT * FROM t1, t2 WHERE t1.a=t2.x AND t2.z='ok'
333**
334** The t2.z='ok' is disabled in the in (2) because it did not originate
335** in the ON clause. The term is disabled in (3) because it is not part
336** of a LEFT OUTER JOIN. In (1), the term is not disabled.
337**
338** Disabling a term causes that term to not be tested in the inner loop
339** of the join. Disabling is an optimization. We would get the correct
340** results if nothing were ever disabled, but joins might run a little
341** slower. The trick is to disable as much as we can without disabling
342** too much. If we disabled in (1), we'd get the wrong answer.
343** See ticket #813.
344*/
345static void disableTerm(WhereLevel *pLevel, Expr **ppExpr){
346 Expr *pExpr = *ppExpr;
347 if( pLevel->iLeftJoin==0 || ExprHasProperty(pExpr, EP_FromJoin) ){
348 *ppExpr = 0;
349 }
350}
351
352/*
drh94a11212004-09-25 13:12:14 +0000353** Generate code that builds a probe for an index. Details:
354**
355** * Check the top nColumn entries on the stack. If any
356** of those entries are NULL, jump immediately to brk,
357** which is the loop exit, since no index entry will match
358** if any part of the key is NULL.
359**
360** * Construct a probe entry from the top nColumn entries in
361** the stack with affinities appropriate for index pIdx.
362*/
363static void buildIndexProbe(Vdbe *v, int nColumn, int brk, Index *pIdx){
364 sqlite3VdbeAddOp(v, OP_NotNull, -nColumn, sqlite3VdbeCurrentAddr(v)+3);
365 sqlite3VdbeAddOp(v, OP_Pop, nColumn, 0);
366 sqlite3VdbeAddOp(v, OP_Goto, 0, brk);
367 sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, 0);
368 sqlite3IndexAffinityStr(v, pIdx);
369}
370
371/*
372** Generate code for an equality term of the WHERE clause. An equality
373** term can be either X=expr or X IN (...). pTerm is the X.
374*/
375static void codeEqualityTerm(
376 Parse *pParse, /* The parsing context */
377 ExprInfo *pTerm, /* The term of the WHERE clause to be coded */
378 int brk, /* Jump here to abandon the loop */
379 WhereLevel *pLevel /* When level of the FROM clause we are working on */
380){
381 Expr *pX = pTerm->p;
382 if( pX->op!=TK_IN ){
383 assert( pX->op==TK_EQ );
384 sqlite3ExprCode(pParse, pX->pRight);
385 }else{
386 int iTab = pX->iTable;
387 Vdbe *v = pParse->pVdbe;
388 sqlite3VdbeAddOp(v, OP_Rewind, iTab, brk);
389 sqlite3VdbeAddOp(v, OP_KeyAsData, iTab, 1);
390 pLevel->inP2 = sqlite3VdbeAddOp(v, OP_IdxColumn, iTab, 0);
391 pLevel->inOp = OP_Next;
392 pLevel->inP1 = iTab;
393 }
394 disableTerm(pLevel, &pTerm->p);
395}
396
397
398/*
drhe3184742002-06-19 14:27:05 +0000399** Generate the beginning of the loop used for WHERE clause processing.
drh75897232000-05-29 14:26:00 +0000400** The return value is a pointer to an (opaque) structure that contains
401** information needed to terminate the loop. Later, the calling routine
danielk19774adee202004-05-08 08:23:19 +0000402** should invoke sqlite3WhereEnd() with the return value of this function
drh75897232000-05-29 14:26:00 +0000403** in order to complete the WHERE clause processing.
404**
405** If an error occurs, this routine returns NULL.
drhc27a1ce2002-06-14 20:58:45 +0000406**
407** The basic idea is to do a nested loop, one loop for each table in
408** the FROM clause of a select. (INSERT and UPDATE statements are the
409** same as a SELECT with only a single table in the FROM clause.) For
410** example, if the SQL is this:
411**
412** SELECT * FROM t1, t2, t3 WHERE ...;
413**
414** Then the code generated is conceptually like the following:
415**
416** foreach row1 in t1 do \ Code generated
danielk19774adee202004-05-08 08:23:19 +0000417** foreach row2 in t2 do |-- by sqlite3WhereBegin()
drhc27a1ce2002-06-14 20:58:45 +0000418** foreach row3 in t3 do /
419** ...
420** end \ Code generated
danielk19774adee202004-05-08 08:23:19 +0000421** end |-- by sqlite3WhereEnd()
drhc27a1ce2002-06-14 20:58:45 +0000422** end /
423**
424** There are Btree cursors associated with each table. t1 uses cursor
drh6a3ea0e2003-05-02 14:32:12 +0000425** number pTabList->a[0].iCursor. t2 uses the cursor pTabList->a[1].iCursor.
426** And so forth. This routine generates code to open those VDBE cursors
danielk19774adee202004-05-08 08:23:19 +0000427** and sqlite3WhereEnd() generates the code to close them.
drhc27a1ce2002-06-14 20:58:45 +0000428**
429** If the WHERE clause is empty, the foreach loops must each scan their
430** entire tables. Thus a three-way join is an O(N^3) operation. But if
431** the tables have indices and there are terms in the WHERE clause that
432** refer to those indices, a complete table scan can be avoided and the
433** code will run much faster. Most of the work of this routine is checking
434** to see if there are indices that can be used to speed up the loop.
435**
436** Terms of the WHERE clause are also used to limit which rows actually
437** make it to the "..." in the middle of the loop. After each "foreach",
438** terms of the WHERE clause that use only terms in that loop and outer
439** loops are evaluated and if false a jump is made around all subsequent
440** inner loops (or around the "..." if the test occurs within the inner-
441** most loop)
442**
443** OUTER JOINS
444**
445** An outer join of tables t1 and t2 is conceptally coded as follows:
446**
447** foreach row1 in t1 do
448** flag = 0
449** foreach row2 in t2 do
450** start:
451** ...
452** flag = 1
453** end
drhe3184742002-06-19 14:27:05 +0000454** if flag==0 then
455** move the row2 cursor to a null row
456** goto start
457** fi
drhc27a1ce2002-06-14 20:58:45 +0000458** end
459**
drhe3184742002-06-19 14:27:05 +0000460** ORDER BY CLAUSE PROCESSING
461**
462** *ppOrderBy is a pointer to the ORDER BY clause of a SELECT statement,
463** if there is one. If there is no ORDER BY clause or if this routine
464** is called from an UPDATE or DELETE statement, then ppOrderBy is NULL.
465**
466** If an index can be used so that the natural output order of the table
467** scan is correct for the ORDER BY clause, then that index is used and
468** *ppOrderBy is set to NULL. This is an optimization that prevents an
469** unnecessary sort of the result set if an index appropriate for the
470** ORDER BY clause already exists.
471**
472** If the where clause loops cannot be arranged to provide the correct
473** output order, then the *ppOrderBy is unchanged.
danielk1977ed326d72004-11-16 15:50:19 +0000474**
475** If parameter iTabCur is non-negative, then it is a cursor already open
476** on table pTabList->aSrc[0]. Use this cursor instead of opening a new
477** one.
drh75897232000-05-29 14:26:00 +0000478*/
danielk19774adee202004-05-08 08:23:19 +0000479WhereInfo *sqlite3WhereBegin(
danielk1977ed326d72004-11-16 15:50:19 +0000480 Parse *pParse, /* The parser context */
481 SrcList *pTabList, /* A list of all tables to be scanned */
482 Expr *pWhere, /* The WHERE clause */
483 int pushKey, /* If TRUE, leave the table key on the stack */
danielk1977299b1872004-11-22 10:02:10 +0000484 ExprList **ppOrderBy /* An ORDER BY clause, or NULL */
drh75897232000-05-29 14:26:00 +0000485){
486 int i; /* Loop counter */
487 WhereInfo *pWInfo; /* Will become the return value of this function */
488 Vdbe *v = pParse->pVdbe; /* The virtual database engine */
drhd4f5ee22003-07-16 00:54:31 +0000489 int brk, cont = 0; /* Addresses used during code generation */
drh75897232000-05-29 14:26:00 +0000490 int nExpr; /* Number of subexpressions in the WHERE clause */
491 int loopMask; /* One bit set for each outer loop */
danielk1977f7df9cc2004-06-16 12:02:47 +0000492 int haveKey = 0; /* True if KEY is on the stack */
drh193bd772004-07-20 18:23:14 +0000493 ExprInfo *pTerm; /* A single term in the WHERE clause; ptr to aExpr[] */
drh6a3ea0e2003-05-02 14:32:12 +0000494 ExprMaskSet maskSet; /* The expression mask set */
drh8aff1012001-12-22 14:49:24 +0000495 int iDirectEq[32]; /* Term of the form ROWID==X for the N-th table */
496 int iDirectLt[32]; /* Term of the form ROWID<X or ROWID<=X */
497 int iDirectGt[32]; /* Term of the form ROWID>X or ROWID>=X */
drh193bd772004-07-20 18:23:14 +0000498 ExprInfo aExpr[101]; /* The WHERE clause is divided into these terms */
drh75897232000-05-29 14:26:00 +0000499
drhc27a1ce2002-06-14 20:58:45 +0000500 /* pushKey is only allowed if there is a single table (as in an INSERT or
501 ** UPDATE statement)
502 */
503 assert( pushKey==0 || pTabList->nSrc==1 );
drh83dcb1a2002-06-28 01:02:38 +0000504
505 /* Split the WHERE clause into separate subexpressions where each
506 ** subexpression is separated by an AND operator. If the aExpr[]
507 ** array fills up, the last entry might point to an expression which
508 ** contains additional unfactored AND operators.
509 */
drh6a3ea0e2003-05-02 14:32:12 +0000510 initMaskSet(&maskSet);
drh83dcb1a2002-06-28 01:02:38 +0000511 memset(aExpr, 0, sizeof(aExpr));
512 nExpr = exprSplit(ARRAYSIZE(aExpr), aExpr, pWhere);
513 if( nExpr==ARRAYSIZE(aExpr) ){
danielk19774adee202004-05-08 08:23:19 +0000514 sqlite3ErrorMsg(pParse, "WHERE clause too complex - no more "
drhf7a9e1a2004-02-22 18:40:56 +0000515 "than %d terms allowed", (int)ARRAYSIZE(aExpr)-1);
drh83dcb1a2002-06-28 01:02:38 +0000516 return 0;
517 }
drhc27a1ce2002-06-14 20:58:45 +0000518
drh75897232000-05-29 14:26:00 +0000519 /* Allocate and initialize the WhereInfo structure that will become the
520 ** return value.
521 */
drhad3cab52002-05-24 02:04:32 +0000522 pWInfo = sqliteMalloc( sizeof(WhereInfo) + pTabList->nSrc*sizeof(WhereLevel));
danielk1977132872b2004-05-10 10:37:18 +0000523 if( sqlite3_malloc_failed ){
drh193bd772004-07-20 18:23:14 +0000524 /* sqliteFree(pWInfo); // Leak memory when malloc fails */
drh75897232000-05-29 14:26:00 +0000525 return 0;
526 }
527 pWInfo->pParse = pParse;
528 pWInfo->pTabList = pTabList;
danielk19774adee202004-05-08 08:23:19 +0000529 pWInfo->iBreak = sqlite3VdbeMakeLabel(v);
drh08192d52002-04-30 19:20:28 +0000530
531 /* Special case: a WHERE clause that is constant. Evaluate the
532 ** expression and either jump over all of the code or fall thru.
533 */
danielk19774adee202004-05-08 08:23:19 +0000534 if( pWhere && (pTabList->nSrc==0 || sqlite3ExprIsConstant(pWhere)) ){
535 sqlite3ExprIfFalse(pParse, pWhere, pWInfo->iBreak, 1);
drhdf199a22002-06-14 22:38:41 +0000536 pWhere = 0;
drh08192d52002-04-30 19:20:28 +0000537 }
drh75897232000-05-29 14:26:00 +0000538
drh75897232000-05-29 14:26:00 +0000539 /* Analyze all of the subexpressions.
540 */
drh193bd772004-07-20 18:23:14 +0000541 for(pTerm=aExpr, i=0; i<nExpr; i++, pTerm++){
542 TriggerStack *pStack;
543 exprAnalyze(pTabList, &maskSet, pTerm);
drh1d1f3052002-05-21 13:18:25 +0000544
545 /* If we are executing a trigger body, remove all references to
546 ** new.* and old.* tables from the prerequisite masks.
547 */
drh193bd772004-07-20 18:23:14 +0000548 if( (pStack = pParse->trigStack)!=0 ){
drh1d1f3052002-05-21 13:18:25 +0000549 int x;
drh193bd772004-07-20 18:23:14 +0000550 if( (x=pStack->newIdx) >= 0 ){
drh6a3ea0e2003-05-02 14:32:12 +0000551 int mask = ~getMask(&maskSet, x);
drh193bd772004-07-20 18:23:14 +0000552 pTerm->prereqRight &= mask;
553 pTerm->prereqLeft &= mask;
554 pTerm->prereqAll &= mask;
drh1d1f3052002-05-21 13:18:25 +0000555 }
drh193bd772004-07-20 18:23:14 +0000556 if( (x=pStack->oldIdx) >= 0 ){
drh6a3ea0e2003-05-02 14:32:12 +0000557 int mask = ~getMask(&maskSet, x);
drh193bd772004-07-20 18:23:14 +0000558 pTerm->prereqRight &= mask;
559 pTerm->prereqLeft &= mask;
560 pTerm->prereqAll &= mask;
drh1d1f3052002-05-21 13:18:25 +0000561 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000562 }
drh75897232000-05-29 14:26:00 +0000563 }
564
drh75897232000-05-29 14:26:00 +0000565 /* Figure out what index to use (if any) for each nested loop.
drh6b563442001-11-07 16:48:26 +0000566 ** Make pWInfo->a[i].pIdx point to the index to use for the i-th nested
drhad3cab52002-05-24 02:04:32 +0000567 ** loop where i==0 is the outer loop and i==pTabList->nSrc-1 is the inner
drh8aff1012001-12-22 14:49:24 +0000568 ** loop.
569 **
570 ** If terms exist that use the ROWID of any table, then set the
571 ** iDirectEq[], iDirectLt[], or iDirectGt[] elements for that table
572 ** to the index of the term containing the ROWID. We always prefer
573 ** to use a ROWID which can directly access a table rather than an
drh0a36c572002-02-18 22:49:59 +0000574 ** index which requires reading an index first to get the rowid then
575 ** doing a second read of the actual database table.
drh75897232000-05-29 14:26:00 +0000576 **
577 ** Actually, if there are more than 32 tables in the join, only the
drh0a36c572002-02-18 22:49:59 +0000578 ** first 32 tables are candidates for indices. This is (again) due
579 ** to the limit of 32 bits in an integer bitmask.
drh75897232000-05-29 14:26:00 +0000580 */
581 loopMask = 0;
drhcb485882002-08-15 13:50:48 +0000582 for(i=0; i<pTabList->nSrc && i<ARRAYSIZE(iDirectEq); i++){
drhc4a3c772001-04-04 11:48:57 +0000583 int j;
drh94a11212004-09-25 13:12:14 +0000584 WhereLevel *pLevel = &pWInfo->a[i];
drh6a3ea0e2003-05-02 14:32:12 +0000585 int iCur = pTabList->a[i].iCursor; /* The cursor for this table */
586 int mask = getMask(&maskSet, iCur); /* Cursor mask for this table */
587 Table *pTab = pTabList->a[i].pTab;
drh75897232000-05-29 14:26:00 +0000588 Index *pIdx;
589 Index *pBestIdx = 0;
drh487ab3c2001-11-08 00:45:21 +0000590 int bestScore = 0;
drh75897232000-05-29 14:26:00 +0000591
drhc4a3c772001-04-04 11:48:57 +0000592 /* Check to see if there is an expression that uses only the
drh8aff1012001-12-22 14:49:24 +0000593 ** ROWID field of this table. For terms of the form ROWID==expr
594 ** set iDirectEq[i] to the index of the term. For terms of the
595 ** form ROWID<expr or ROWID<=expr set iDirectLt[i] to the term index.
596 ** For terms like ROWID>expr or ROWID>=expr set iDirectGt[i].
drh174b6192002-12-03 02:22:52 +0000597 **
598 ** (Added:) Treat ROWID IN expr like ROWID=expr.
drhc4a3c772001-04-04 11:48:57 +0000599 */
drh94a11212004-09-25 13:12:14 +0000600 pLevel->iCur = -1;
drh8aff1012001-12-22 14:49:24 +0000601 iDirectEq[i] = -1;
602 iDirectLt[i] = -1;
603 iDirectGt[i] = -1;
drh193bd772004-07-20 18:23:14 +0000604 for(pTerm=aExpr, j=0; j<nExpr; j++, pTerm++){
605 Expr *pX = pTerm->p;
606 if( pTerm->idxLeft==iCur && pX->pLeft->iColumn<0
607 && (pTerm->prereqRight & loopMask)==pTerm->prereqRight ){
608 switch( pX->op ){
drhd99f7062002-06-08 23:25:08 +0000609 case TK_IN:
drh8aff1012001-12-22 14:49:24 +0000610 case TK_EQ: iDirectEq[i] = j; break;
611 case TK_LE:
612 case TK_LT: iDirectLt[i] = j; break;
613 case TK_GE:
614 case TK_GT: iDirectGt[i] = j; break;
615 }
drhc4a3c772001-04-04 11:48:57 +0000616 }
drhc4a3c772001-04-04 11:48:57 +0000617 }
drh8aff1012001-12-22 14:49:24 +0000618 if( iDirectEq[i]>=0 ){
drh6a3ea0e2003-05-02 14:32:12 +0000619 loopMask |= mask;
drh94a11212004-09-25 13:12:14 +0000620 pLevel->pIdx = 0;
drhc4a3c772001-04-04 11:48:57 +0000621 continue;
622 }
623
drh75897232000-05-29 14:26:00 +0000624 /* Do a search for usable indices. Leave pBestIdx pointing to
drh487ab3c2001-11-08 00:45:21 +0000625 ** the "best" index. pBestIdx is left set to NULL if no indices
626 ** are usable.
drh75897232000-05-29 14:26:00 +0000627 **
drh487ab3c2001-11-08 00:45:21 +0000628 ** The best index is determined as follows. For each of the
629 ** left-most terms that is fixed by an equality operator, add
drhc045ec52002-12-04 20:01:06 +0000630 ** 8 to the score. The right-most term of the index may be
drh487ab3c2001-11-08 00:45:21 +0000631 ** constrained by an inequality. Add 1 if for an "x<..." constraint
632 ** and add 2 for an "x>..." constraint. Chose the index that
633 ** gives the best score.
634 **
635 ** This scoring system is designed so that the score can later be
drhc045ec52002-12-04 20:01:06 +0000636 ** used to determine how the index is used. If the score&7 is 0
drh487ab3c2001-11-08 00:45:21 +0000637 ** then all constraints are equalities. If score&1 is not 0 then
638 ** there is an inequality used as a termination key. (ex: "x<...")
639 ** If score&2 is not 0 then there is an inequality used as the
drhc045ec52002-12-04 20:01:06 +0000640 ** start key. (ex: "x>..."). A score or 4 is the special case
641 ** of an IN operator constraint. (ex: "x IN ...").
drhd99f7062002-06-08 23:25:08 +0000642 **
drhc27a1ce2002-06-14 20:58:45 +0000643 ** The IN operator (as in "<expr> IN (...)") is treated the same as
644 ** an equality comparison except that it can only be used on the
645 ** left-most column of an index and other terms of the WHERE clause
646 ** cannot be used in conjunction with the IN operator to help satisfy
647 ** other columns of the index.
drh75897232000-05-29 14:26:00 +0000648 */
649 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drhc27a1ce2002-06-14 20:58:45 +0000650 int eqMask = 0; /* Index columns covered by an x=... term */
651 int ltMask = 0; /* Index columns covered by an x<... term */
652 int gtMask = 0; /* Index columns covered by an x>... term */
653 int inMask = 0; /* Index columns covered by an x IN .. term */
drh487ab3c2001-11-08 00:45:21 +0000654 int nEq, m, score;
drh75897232000-05-29 14:26:00 +0000655
drh487ab3c2001-11-08 00:45:21 +0000656 if( pIdx->nColumn>32 ) continue; /* Ignore indices too many columns */
drh193bd772004-07-20 18:23:14 +0000657 for(pTerm=aExpr, j=0; j<nExpr; j++, pTerm++){
drh193bd772004-07-20 18:23:14 +0000658 Expr *pX = pTerm->p;
drh94a11212004-09-25 13:12:14 +0000659 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pX->pLeft);
drh193bd772004-07-20 18:23:14 +0000660 if( !pColl && pX->pRight ){
661 pColl = sqlite3ExprCollSeq(pParse, pX->pRight);
danielk19770202b292004-06-09 09:55:16 +0000662 }
663 if( !pColl ){
664 pColl = pParse->db->pDfltColl;
665 }
drh193bd772004-07-20 18:23:14 +0000666 if( pTerm->idxLeft==iCur
667 && (pTerm->prereqRight & loopMask)==pTerm->prereqRight ){
668 int iColumn = pX->pLeft->iColumn;
drh75897232000-05-29 14:26:00 +0000669 int k;
danielk1977e014a832004-05-17 10:48:57 +0000670 char idxaff = pIdx->pTable->aCol[iColumn].affinity;
drh967e8b72000-06-21 13:59:10 +0000671 for(k=0; k<pIdx->nColumn; k++){
danielk19770202b292004-06-09 09:55:16 +0000672 /* If the collating sequences or affinities don't match,
673 ** ignore this index. */
674 if( pColl!=pIdx->keyInfo.aColl[k] ) continue;
drh193bd772004-07-20 18:23:14 +0000675 if( !sqlite3IndexAffinityOk(pX, idxaff) ) continue;
danielk19770202b292004-06-09 09:55:16 +0000676 if( pIdx->aiColumn[k]==iColumn ){
drh193bd772004-07-20 18:23:14 +0000677 switch( pX->op ){
drh48185c12002-06-09 01:55:20 +0000678 case TK_IN: {
679 if( k==0 ) inMask |= 1;
680 break;
681 }
drh487ab3c2001-11-08 00:45:21 +0000682 case TK_EQ: {
683 eqMask |= 1<<k;
684 break;
685 }
686 case TK_LE:
687 case TK_LT: {
688 ltMask |= 1<<k;
689 break;
690 }
691 case TK_GE:
692 case TK_GT: {
693 gtMask |= 1<<k;
694 break;
695 }
696 default: {
697 /* CANT_HAPPEN */
698 assert( 0 );
699 break;
700 }
701 }
drh75897232000-05-29 14:26:00 +0000702 break;
703 }
704 }
705 }
drh75897232000-05-29 14:26:00 +0000706 }
drhc045ec52002-12-04 20:01:06 +0000707
708 /* The following loop ends with nEq set to the number of columns
709 ** on the left of the index with == constraints.
710 */
drh487ab3c2001-11-08 00:45:21 +0000711 for(nEq=0; nEq<pIdx->nColumn; nEq++){
712 m = (1<<(nEq+1))-1;
713 if( (m & eqMask)!=m ) break;
714 }
drhc045ec52002-12-04 20:01:06 +0000715 score = nEq*8; /* Base score is 8 times number of == constraints */
drh487ab3c2001-11-08 00:45:21 +0000716 m = 1<<nEq;
drhc045ec52002-12-04 20:01:06 +0000717 if( m & ltMask ) score++; /* Increase score for a < constraint */
718 if( m & gtMask ) score+=2; /* Increase score for a > constraint */
719 if( score==0 && inMask ) score = 4; /* Default score for IN constraint */
drh487ab3c2001-11-08 00:45:21 +0000720 if( score>bestScore ){
721 pBestIdx = pIdx;
722 bestScore = score;
drh75897232000-05-29 14:26:00 +0000723 }
724 }
drh94a11212004-09-25 13:12:14 +0000725 pLevel->pIdx = pBestIdx;
726 pLevel->score = bestScore;
727 pLevel->bRev = 0;
drh6a3ea0e2003-05-02 14:32:12 +0000728 loopMask |= mask;
drh6b563442001-11-07 16:48:26 +0000729 if( pBestIdx ){
drh94a11212004-09-25 13:12:14 +0000730 pLevel->iCur = pParse->nTab++;
drh6b563442001-11-07 16:48:26 +0000731 }
drh75897232000-05-29 14:26:00 +0000732 }
733
drhe3184742002-06-19 14:27:05 +0000734 /* Check to see if the ORDER BY clause is or can be satisfied by the
735 ** use of an index on the first table.
736 */
737 if( ppOrderBy && *ppOrderBy && pTabList->nSrc>0 ){
738 Index *pSortIdx;
739 Index *pIdx;
740 Table *pTab;
drhc045ec52002-12-04 20:01:06 +0000741 int bRev = 0;
drhe3184742002-06-19 14:27:05 +0000742
743 pTab = pTabList->a[0].pTab;
744 pIdx = pWInfo->a[0].pIdx;
745 if( pIdx && pWInfo->a[0].score==4 ){
drhc045ec52002-12-04 20:01:06 +0000746 /* If there is already an IN index on the left-most table,
747 ** it will not give the correct sort order.
748 ** So, pretend that no suitable index is found.
drhe3184742002-06-19 14:27:05 +0000749 */
750 pSortIdx = 0;
751 }else if( iDirectEq[0]>=0 || iDirectLt[0]>=0 || iDirectGt[0]>=0 ){
752 /* If the left-most column is accessed using its ROWID, then do
753 ** not try to sort by index.
754 */
755 pSortIdx = 0;
756 }else{
drhdd4852c2002-12-04 21:50:16 +0000757 int nEqCol = (pWInfo->a[0].score+4)/8;
danielk1977d2b65b92004-06-10 10:51:47 +0000758 pSortIdx = findSortingIndex(pParse, pTab, pTabList->a[0].iCursor,
drh6a3ea0e2003-05-02 14:32:12 +0000759 *ppOrderBy, pIdx, nEqCol, &bRev);
drhe3184742002-06-19 14:27:05 +0000760 }
761 if( pSortIdx && (pIdx==0 || pIdx==pSortIdx) ){
762 if( pIdx==0 ){
763 pWInfo->a[0].pIdx = pSortIdx;
764 pWInfo->a[0].iCur = pParse->nTab++;
drhe3184742002-06-19 14:27:05 +0000765 }
drhc045ec52002-12-04 20:01:06 +0000766 pWInfo->a[0].bRev = bRev;
drhe3184742002-06-19 14:27:05 +0000767 *ppOrderBy = 0;
768 }
769 }
770
drh6b563442001-11-07 16:48:26 +0000771 /* Open all tables in the pTabList and all indices used by those tables.
drh75897232000-05-29 14:26:00 +0000772 */
drhc275b4e2004-07-19 17:25:24 +0000773 sqlite3CodeVerifySchema(pParse, -1); /* Insert the cookie verifier Goto */
drhad3cab52002-05-24 02:04:32 +0000774 for(i=0; i<pTabList->nSrc; i++){
drhf57b3392001-10-08 13:22:32 +0000775 Table *pTab;
drh701a0ae2004-02-22 20:05:00 +0000776 Index *pIx;
drhf57b3392001-10-08 13:22:32 +0000777
778 pTab = pTabList->a[i].pTab;
drha76b5df2002-02-23 02:32:10 +0000779 if( pTab->isTransient || pTab->pSelect ) continue;
danielk1977299b1872004-11-22 10:02:10 +0000780 sqlite3OpenTableForReading(v, pTabList->a[i].iCursor, pTab);
danielk1977f9d19a62004-06-14 08:26:35 +0000781 sqlite3CodeVerifySchema(pParse, pTab->iDb);
drh701a0ae2004-02-22 20:05:00 +0000782 if( (pIx = pWInfo->a[i].pIdx)!=0 ){
danielk19774adee202004-05-08 08:23:19 +0000783 sqlite3VdbeAddOp(v, OP_Integer, pIx->iDb, 0);
drhd3d39e92004-05-20 22:16:29 +0000784 sqlite3VdbeOp3(v, OP_OpenRead, pWInfo->a[i].iCur, pIx->tnum,
785 (char*)&pIx->keyInfo, P3_KEYINFO);
drh75897232000-05-29 14:26:00 +0000786 }
787 }
788
789 /* Generate the code to do the search
790 */
drh75897232000-05-29 14:26:00 +0000791 loopMask = 0;
drhad3cab52002-05-24 02:04:32 +0000792 for(i=0; i<pTabList->nSrc; i++){
drh75897232000-05-29 14:26:00 +0000793 int j, k;
drh6a3ea0e2003-05-02 14:32:12 +0000794 int iCur = pTabList->a[i].iCursor;
drhc4a3c772001-04-04 11:48:57 +0000795 Index *pIdx;
drh6b563442001-11-07 16:48:26 +0000796 WhereLevel *pLevel = &pWInfo->a[i];
drh75897232000-05-29 14:26:00 +0000797
drhad2d8302002-05-24 20:31:36 +0000798 /* If this is the right table of a LEFT OUTER JOIN, allocate and
drh174b6192002-12-03 02:22:52 +0000799 ** initialize a memory cell that records if this table matches any
drhc27a1ce2002-06-14 20:58:45 +0000800 ** row of the left table of the join.
drhad2d8302002-05-24 20:31:36 +0000801 */
802 if( i>0 && (pTabList->a[i-1].jointype & JT_LEFT)!=0 ){
803 if( !pParse->nMem ) pParse->nMem++;
804 pLevel->iLeftJoin = pParse->nMem++;
danielk19770f69c1e2004-05-29 11:24:50 +0000805 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk19774adee202004-05-08 08:23:19 +0000806 sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iLeftJoin, 1);
drhad6d9462004-09-19 02:15:24 +0000807 VdbeComment((v, "# init LEFT JOIN no-match flag"));
drhad2d8302002-05-24 20:31:36 +0000808 }
809
drh8aff1012001-12-22 14:49:24 +0000810 pIdx = pLevel->pIdx;
drhd99f7062002-06-08 23:25:08 +0000811 pLevel->inOp = OP_Noop;
drh94a11212004-09-25 13:12:14 +0000812 if( i<ARRAYSIZE(iDirectEq) && (k = iDirectEq[i])>=0 ){
drh8aff1012001-12-22 14:49:24 +0000813 /* Case 1: We can directly reference a single row using an
drhc27a1ce2002-06-14 20:58:45 +0000814 ** equality comparison against the ROWID field. Or
815 ** we reference multiple rows using a "rowid IN (...)"
816 ** construct.
drhc4a3c772001-04-04 11:48:57 +0000817 */
drh8aff1012001-12-22 14:49:24 +0000818 assert( k<nExpr );
drh193bd772004-07-20 18:23:14 +0000819 pTerm = &aExpr[k];
820 assert( pTerm->p!=0 );
drh193bd772004-07-20 18:23:14 +0000821 assert( pTerm->idxLeft==iCur );
drh94a11212004-09-25 13:12:14 +0000822 brk = pLevel->brk = sqlite3VdbeMakeLabel(v);
823 codeEqualityTerm(pParse, pTerm, brk, pLevel);
danielk19774adee202004-05-08 08:23:19 +0000824 cont = pLevel->cont = sqlite3VdbeMakeLabel(v);
825 sqlite3VdbeAddOp(v, OP_MustBeInt, 1, brk);
drhd99f7062002-06-08 23:25:08 +0000826 haveKey = 0;
danielk19774adee202004-05-08 08:23:19 +0000827 sqlite3VdbeAddOp(v, OP_NotExists, iCur, brk);
drh6b563442001-11-07 16:48:26 +0000828 pLevel->op = OP_Noop;
drhe3184742002-06-19 14:27:05 +0000829 }else if( pIdx!=0 && pLevel->score>0 && pLevel->score%4==0 ){
drhc27a1ce2002-06-14 20:58:45 +0000830 /* Case 2: There is an index and all terms of the WHERE clause that
831 ** refer to the index use the "==" or "IN" operators.
drh75897232000-05-29 14:26:00 +0000832 */
drh6b563442001-11-07 16:48:26 +0000833 int start;
drhc045ec52002-12-04 20:01:06 +0000834 int nColumn = (pLevel->score+4)/8;
danielk19774adee202004-05-08 08:23:19 +0000835 brk = pLevel->brk = sqlite3VdbeMakeLabel(v);
drh772ae622004-05-19 13:13:08 +0000836
837 /* For each column of the index, find the term of the WHERE clause that
838 ** constraints that column. If the WHERE clause term is X=expr, then
839 ** evaluation expr and leave the result on the stack */
drh487ab3c2001-11-08 00:45:21 +0000840 for(j=0; j<nColumn; j++){
drh193bd772004-07-20 18:23:14 +0000841 for(pTerm=aExpr, k=0; k<nExpr; k++, pTerm++){
842 Expr *pX = pTerm->p;
drhd99f7062002-06-08 23:25:08 +0000843 if( pX==0 ) continue;
drh193bd772004-07-20 18:23:14 +0000844 if( pTerm->idxLeft==iCur
845 && (pTerm->prereqRight & loopMask)==pTerm->prereqRight
drhd99f7062002-06-08 23:25:08 +0000846 && pX->pLeft->iColumn==pIdx->aiColumn[j]
drh75897232000-05-29 14:26:00 +0000847 ){
danielk1977e014a832004-05-17 10:48:57 +0000848 char idxaff = pIdx->pTable->aCol[pX->pLeft->iColumn].affinity;
drh94a11212004-09-25 13:12:14 +0000849 if( sqlite3IndexAffinityOk(pX, idxaff) ){
850 codeEqualityTerm(pParse, pTerm, brk, pLevel);
851 break;
drhd99f7062002-06-08 23:25:08 +0000852 }
drh75897232000-05-29 14:26:00 +0000853 }
drh75897232000-05-29 14:26:00 +0000854 }
855 }
drh6b563442001-11-07 16:48:26 +0000856 pLevel->iMem = pParse->nMem++;
danielk19774adee202004-05-08 08:23:19 +0000857 cont = pLevel->cont = sqlite3VdbeMakeLabel(v);
drh94a11212004-09-25 13:12:14 +0000858 buildIndexProbe(v, nColumn, brk, pIdx);
danielk19773d1bfea2004-05-14 11:00:53 +0000859 sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iMem, 0);
drh772ae622004-05-19 13:13:08 +0000860
drh772ae622004-05-19 13:13:08 +0000861 /* Generate code (1) to move to the first matching element of the table.
862 ** Then generate code (2) that jumps to "brk" after the cursor is past
863 ** the last matching element of the table. The code (1) is executed
864 ** once to initialize the search, the code (2) is executed before each
865 ** iteration of the scan to see if the scan has finished. */
drhc045ec52002-12-04 20:01:06 +0000866 if( pLevel->bRev ){
867 /* Scan in reverse order */
drh7cf6e4d2004-05-19 14:56:55 +0000868 sqlite3VdbeAddOp(v, OP_MoveLe, pLevel->iCur, brk);
danielk19774adee202004-05-08 08:23:19 +0000869 start = sqlite3VdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
870 sqlite3VdbeAddOp(v, OP_IdxLT, pLevel->iCur, brk);
drhc045ec52002-12-04 20:01:06 +0000871 pLevel->op = OP_Prev;
872 }else{
873 /* Scan in the forward order */
drh7cf6e4d2004-05-19 14:56:55 +0000874 sqlite3VdbeAddOp(v, OP_MoveGe, pLevel->iCur, brk);
danielk19774adee202004-05-08 08:23:19 +0000875 start = sqlite3VdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
drhfec19aa2004-05-19 20:41:03 +0000876 sqlite3VdbeOp3(v, OP_IdxGE, pLevel->iCur, brk, "+", P3_STATIC);
drhc045ec52002-12-04 20:01:06 +0000877 pLevel->op = OP_Next;
878 }
danielk19774adee202004-05-08 08:23:19 +0000879 sqlite3VdbeAddOp(v, OP_RowKey, pLevel->iCur, 0);
880 sqlite3VdbeAddOp(v, OP_IdxIsNull, nColumn, cont);
881 sqlite3VdbeAddOp(v, OP_IdxRecno, pLevel->iCur, 0);
drhad3cab52002-05-24 02:04:32 +0000882 if( i==pTabList->nSrc-1 && pushKey ){
drh75897232000-05-29 14:26:00 +0000883 haveKey = 1;
884 }else{
drh7cf6e4d2004-05-19 14:56:55 +0000885 sqlite3VdbeAddOp(v, OP_MoveGe, iCur, 0);
drh75897232000-05-29 14:26:00 +0000886 haveKey = 0;
887 }
drh6b563442001-11-07 16:48:26 +0000888 pLevel->p1 = pLevel->iCur;
889 pLevel->p2 = start;
drh8aff1012001-12-22 14:49:24 +0000890 }else if( i<ARRAYSIZE(iDirectLt) && (iDirectLt[i]>=0 || iDirectGt[i]>=0) ){
891 /* Case 3: We have an inequality comparison against the ROWID field.
892 */
893 int testOp = OP_Noop;
894 int start;
895
danielk19774adee202004-05-08 08:23:19 +0000896 brk = pLevel->brk = sqlite3VdbeMakeLabel(v);
897 cont = pLevel->cont = sqlite3VdbeMakeLabel(v);
drh8aff1012001-12-22 14:49:24 +0000898 if( iDirectGt[i]>=0 ){
drh94a11212004-09-25 13:12:14 +0000899 Expr *pX;
drh8aff1012001-12-22 14:49:24 +0000900 k = iDirectGt[i];
901 assert( k<nExpr );
drh193bd772004-07-20 18:23:14 +0000902 pTerm = &aExpr[k];
drh94a11212004-09-25 13:12:14 +0000903 pX = pTerm->p;
904 assert( pX!=0 );
drh193bd772004-07-20 18:23:14 +0000905 assert( pTerm->idxLeft==iCur );
drh94a11212004-09-25 13:12:14 +0000906 sqlite3ExprCode(pParse, pX->pRight);
907 sqlite3VdbeAddOp(v, OP_ForceInt, pX->op==TK_LT || pX->op==TK_GT, brk);
drh7cf6e4d2004-05-19 14:56:55 +0000908 sqlite3VdbeAddOp(v, OP_MoveGe, iCur, brk);
drh193bd772004-07-20 18:23:14 +0000909 disableTerm(pLevel, &pTerm->p);
drh8aff1012001-12-22 14:49:24 +0000910 }else{
danielk19774adee202004-05-08 08:23:19 +0000911 sqlite3VdbeAddOp(v, OP_Rewind, iCur, brk);
drh8aff1012001-12-22 14:49:24 +0000912 }
913 if( iDirectLt[i]>=0 ){
drh94a11212004-09-25 13:12:14 +0000914 Expr *pX;
drh8aff1012001-12-22 14:49:24 +0000915 k = iDirectLt[i];
916 assert( k<nExpr );
drh193bd772004-07-20 18:23:14 +0000917 pTerm = &aExpr[k];
drh94a11212004-09-25 13:12:14 +0000918 pX = pTerm->p;
919 assert( pX!=0 );
drh193bd772004-07-20 18:23:14 +0000920 assert( pTerm->idxLeft==iCur );
drh94a11212004-09-25 13:12:14 +0000921 sqlite3ExprCode(pParse, pX->pRight);
drh8aff1012001-12-22 14:49:24 +0000922 pLevel->iMem = pParse->nMem++;
danielk19774adee202004-05-08 08:23:19 +0000923 sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
drh94a11212004-09-25 13:12:14 +0000924 if( pX->op==TK_LT || pX->op==TK_GT ){
drh8aff1012001-12-22 14:49:24 +0000925 testOp = OP_Ge;
926 }else{
927 testOp = OP_Gt;
928 }
drh193bd772004-07-20 18:23:14 +0000929 disableTerm(pLevel, &pTerm->p);
drh8aff1012001-12-22 14:49:24 +0000930 }
danielk19774adee202004-05-08 08:23:19 +0000931 start = sqlite3VdbeCurrentAddr(v);
drh8aff1012001-12-22 14:49:24 +0000932 pLevel->op = OP_Next;
drh6a3ea0e2003-05-02 14:32:12 +0000933 pLevel->p1 = iCur;
drh8aff1012001-12-22 14:49:24 +0000934 pLevel->p2 = start;
935 if( testOp!=OP_Noop ){
danielk19774adee202004-05-08 08:23:19 +0000936 sqlite3VdbeAddOp(v, OP_Recno, iCur, 0);
937 sqlite3VdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
938 sqlite3VdbeAddOp(v, testOp, 0, brk);
drh8aff1012001-12-22 14:49:24 +0000939 }
940 haveKey = 0;
941 }else if( pIdx==0 ){
drhc27a1ce2002-06-14 20:58:45 +0000942 /* Case 4: There is no usable index. We must do a complete
drh8aff1012001-12-22 14:49:24 +0000943 ** scan of the entire database table.
944 */
945 int start;
946
danielk19774adee202004-05-08 08:23:19 +0000947 brk = pLevel->brk = sqlite3VdbeMakeLabel(v);
948 cont = pLevel->cont = sqlite3VdbeMakeLabel(v);
949 sqlite3VdbeAddOp(v, OP_Rewind, iCur, brk);
950 start = sqlite3VdbeCurrentAddr(v);
drh8aff1012001-12-22 14:49:24 +0000951 pLevel->op = OP_Next;
drh6a3ea0e2003-05-02 14:32:12 +0000952 pLevel->p1 = iCur;
drh8aff1012001-12-22 14:49:24 +0000953 pLevel->p2 = start;
954 haveKey = 0;
drh487ab3c2001-11-08 00:45:21 +0000955 }else{
drhc27a1ce2002-06-14 20:58:45 +0000956 /* Case 5: The WHERE clause term that refers to the right-most
957 ** column of the index is an inequality. For example, if
958 ** the index is on (x,y,z) and the WHERE clause is of the
959 ** form "x=5 AND y<10" then this case is used. Only the
960 ** right-most column can be an inequality - the rest must
961 ** use the "==" operator.
drhe3184742002-06-19 14:27:05 +0000962 **
963 ** This case is also used when there are no WHERE clause
964 ** constraints but an index is selected anyway, in order
965 ** to force the output order to conform to an ORDER BY.
drh487ab3c2001-11-08 00:45:21 +0000966 */
967 int score = pLevel->score;
drhc045ec52002-12-04 20:01:06 +0000968 int nEqColumn = score/8;
drh487ab3c2001-11-08 00:45:21 +0000969 int start;
danielk1977f7df9cc2004-06-16 12:02:47 +0000970 int leFlag=0, geFlag=0;
drh487ab3c2001-11-08 00:45:21 +0000971 int testOp;
972
973 /* Evaluate the equality constraints
974 */
975 for(j=0; j<nEqColumn; j++){
drh94a11212004-09-25 13:12:14 +0000976 int iIdxCol = pIdx->aiColumn[j];
drh193bd772004-07-20 18:23:14 +0000977 for(pTerm=aExpr, k=0; k<nExpr; k++, pTerm++){
drh94a11212004-09-25 13:12:14 +0000978 Expr *pX = pTerm->p;
979 if( pX==0 ) continue;
drh193bd772004-07-20 18:23:14 +0000980 if( pTerm->idxLeft==iCur
drh94a11212004-09-25 13:12:14 +0000981 && pX->op==TK_EQ
drh193bd772004-07-20 18:23:14 +0000982 && (pTerm->prereqRight & loopMask)==pTerm->prereqRight
drh94a11212004-09-25 13:12:14 +0000983 && pX->pLeft->iColumn==iIdxCol
drh487ab3c2001-11-08 00:45:21 +0000984 ){
drh94a11212004-09-25 13:12:14 +0000985 sqlite3ExprCode(pParse, pX->pRight);
drh193bd772004-07-20 18:23:14 +0000986 disableTerm(pLevel, &pTerm->p);
drh487ab3c2001-11-08 00:45:21 +0000987 break;
988 }
989 }
990 }
991
drhc27a1ce2002-06-14 20:58:45 +0000992 /* Duplicate the equality term values because they will all be
drh487ab3c2001-11-08 00:45:21 +0000993 ** used twice: once to make the termination key and once to make the
994 ** start key.
995 */
996 for(j=0; j<nEqColumn; j++){
danielk19774adee202004-05-08 08:23:19 +0000997 sqlite3VdbeAddOp(v, OP_Dup, nEqColumn-1, 0);
drh487ab3c2001-11-08 00:45:21 +0000998 }
999
drhc045ec52002-12-04 20:01:06 +00001000 /* Labels for the beginning and end of the loop
1001 */
danielk19774adee202004-05-08 08:23:19 +00001002 cont = pLevel->cont = sqlite3VdbeMakeLabel(v);
1003 brk = pLevel->brk = sqlite3VdbeMakeLabel(v);
drhc045ec52002-12-04 20:01:06 +00001004
drh487ab3c2001-11-08 00:45:21 +00001005 /* Generate the termination key. This is the key value that
1006 ** will end the search. There is no termination key if there
drhc27a1ce2002-06-14 20:58:45 +00001007 ** are no equality terms and no "X<..." term.
drhc045ec52002-12-04 20:01:06 +00001008 **
1009 ** 2002-Dec-04: On a reverse-order scan, the so-called "termination"
1010 ** key computed here really ends up being the start key.
drh487ab3c2001-11-08 00:45:21 +00001011 */
1012 if( (score & 1)!=0 ){
drh193bd772004-07-20 18:23:14 +00001013 for(pTerm=aExpr, k=0; k<nExpr; k++, pTerm++){
drh94a11212004-09-25 13:12:14 +00001014 Expr *pX = pTerm->p;
1015 if( pX==0 ) continue;
drh193bd772004-07-20 18:23:14 +00001016 if( pTerm->idxLeft==iCur
drh94a11212004-09-25 13:12:14 +00001017 && (pX->op==TK_LT || pX->op==TK_LE)
drh193bd772004-07-20 18:23:14 +00001018 && (pTerm->prereqRight & loopMask)==pTerm->prereqRight
drh94a11212004-09-25 13:12:14 +00001019 && pX->pLeft->iColumn==pIdx->aiColumn[j]
drh487ab3c2001-11-08 00:45:21 +00001020 ){
drh94a11212004-09-25 13:12:14 +00001021 sqlite3ExprCode(pParse, pX->pRight);
1022 leFlag = pX->op==TK_LE;
drh193bd772004-07-20 18:23:14 +00001023 disableTerm(pLevel, &pTerm->p);
drh487ab3c2001-11-08 00:45:21 +00001024 break;
1025 }
1026 }
1027 testOp = OP_IdxGE;
1028 }else{
1029 testOp = nEqColumn>0 ? OP_IdxGE : OP_Noop;
1030 leFlag = 1;
1031 }
1032 if( testOp!=OP_Noop ){
drh143f3c42004-01-07 20:37:52 +00001033 int nCol = nEqColumn + (score & 1);
drh487ab3c2001-11-08 00:45:21 +00001034 pLevel->iMem = pParse->nMem++;
drh94a11212004-09-25 13:12:14 +00001035 buildIndexProbe(v, nCol, brk, pIdx);
drhc045ec52002-12-04 20:01:06 +00001036 if( pLevel->bRev ){
drh7cf6e4d2004-05-19 14:56:55 +00001037 int op = leFlag ? OP_MoveLe : OP_MoveLt;
1038 sqlite3VdbeAddOp(v, op, pLevel->iCur, brk);
drhc045ec52002-12-04 20:01:06 +00001039 }else{
danielk19774adee202004-05-08 08:23:19 +00001040 sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
drhc045ec52002-12-04 20:01:06 +00001041 }
1042 }else if( pLevel->bRev ){
danielk19774adee202004-05-08 08:23:19 +00001043 sqlite3VdbeAddOp(v, OP_Last, pLevel->iCur, brk);
drh487ab3c2001-11-08 00:45:21 +00001044 }
1045
1046 /* Generate the start key. This is the key that defines the lower
drhc27a1ce2002-06-14 20:58:45 +00001047 ** bound on the search. There is no start key if there are no
1048 ** equality terms and if there is no "X>..." term. In
drh487ab3c2001-11-08 00:45:21 +00001049 ** that case, generate a "Rewind" instruction in place of the
1050 ** start key search.
drhc045ec52002-12-04 20:01:06 +00001051 **
1052 ** 2002-Dec-04: In the case of a reverse-order search, the so-called
1053 ** "start" key really ends up being used as the termination key.
drh487ab3c2001-11-08 00:45:21 +00001054 */
1055 if( (score & 2)!=0 ){
drh193bd772004-07-20 18:23:14 +00001056 for(pTerm=aExpr, k=0; k<nExpr; k++, pTerm++){
drh94a11212004-09-25 13:12:14 +00001057 Expr *pX = pTerm->p;
1058 if( pX==0 ) continue;
drh193bd772004-07-20 18:23:14 +00001059 if( pTerm->idxLeft==iCur
drh94a11212004-09-25 13:12:14 +00001060 && (pX->op==TK_GT || pX->op==TK_GE)
drh193bd772004-07-20 18:23:14 +00001061 && (pTerm->prereqRight & loopMask)==pTerm->prereqRight
drh94a11212004-09-25 13:12:14 +00001062 && pX->pLeft->iColumn==pIdx->aiColumn[j]
drh487ab3c2001-11-08 00:45:21 +00001063 ){
drh94a11212004-09-25 13:12:14 +00001064 sqlite3ExprCode(pParse, pX->pRight);
1065 geFlag = pX->op==TK_GE;
drh193bd772004-07-20 18:23:14 +00001066 disableTerm(pLevel, &pTerm->p);
drh487ab3c2001-11-08 00:45:21 +00001067 break;
1068 }
1069 }
drh7900ead2001-11-12 13:51:43 +00001070 }else{
1071 geFlag = 1;
drh487ab3c2001-11-08 00:45:21 +00001072 }
drh487ab3c2001-11-08 00:45:21 +00001073 if( nEqColumn>0 || (score&2)!=0 ){
drh143f3c42004-01-07 20:37:52 +00001074 int nCol = nEqColumn + ((score&2)!=0);
drh94a11212004-09-25 13:12:14 +00001075 buildIndexProbe(v, nCol, brk, pIdx);
drhc045ec52002-12-04 20:01:06 +00001076 if( pLevel->bRev ){
1077 pLevel->iMem = pParse->nMem++;
danielk19774adee202004-05-08 08:23:19 +00001078 sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
drhc045ec52002-12-04 20:01:06 +00001079 testOp = OP_IdxLT;
1080 }else{
drh7cf6e4d2004-05-19 14:56:55 +00001081 int op = geFlag ? OP_MoveGe : OP_MoveGt;
1082 sqlite3VdbeAddOp(v, op, pLevel->iCur, brk);
drhc045ec52002-12-04 20:01:06 +00001083 }
1084 }else if( pLevel->bRev ){
1085 testOp = OP_Noop;
drh487ab3c2001-11-08 00:45:21 +00001086 }else{
danielk19774adee202004-05-08 08:23:19 +00001087 sqlite3VdbeAddOp(v, OP_Rewind, pLevel->iCur, brk);
drh487ab3c2001-11-08 00:45:21 +00001088 }
1089
1090 /* Generate the the top of the loop. If there is a termination
1091 ** key we have to test for that key and abort at the top of the
1092 ** loop.
1093 */
danielk19774adee202004-05-08 08:23:19 +00001094 start = sqlite3VdbeCurrentAddr(v);
drh487ab3c2001-11-08 00:45:21 +00001095 if( testOp!=OP_Noop ){
danielk19774adee202004-05-08 08:23:19 +00001096 sqlite3VdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
1097 sqlite3VdbeAddOp(v, testOp, pLevel->iCur, brk);
danielk19773d1bfea2004-05-14 11:00:53 +00001098 if( (leFlag && !pLevel->bRev) || (!geFlag && pLevel->bRev) ){
1099 sqlite3VdbeChangeP3(v, -1, "+", P3_STATIC);
1100 }
drh487ab3c2001-11-08 00:45:21 +00001101 }
danielk19774adee202004-05-08 08:23:19 +00001102 sqlite3VdbeAddOp(v, OP_RowKey, pLevel->iCur, 0);
1103 sqlite3VdbeAddOp(v, OP_IdxIsNull, nEqColumn + (score & 1), cont);
1104 sqlite3VdbeAddOp(v, OP_IdxRecno, pLevel->iCur, 0);
drhad3cab52002-05-24 02:04:32 +00001105 if( i==pTabList->nSrc-1 && pushKey ){
drh487ab3c2001-11-08 00:45:21 +00001106 haveKey = 1;
1107 }else{
drh7cf6e4d2004-05-19 14:56:55 +00001108 sqlite3VdbeAddOp(v, OP_MoveGe, iCur, 0);
drh487ab3c2001-11-08 00:45:21 +00001109 haveKey = 0;
1110 }
1111
1112 /* Record the instruction used to terminate the loop.
1113 */
drhc045ec52002-12-04 20:01:06 +00001114 pLevel->op = pLevel->bRev ? OP_Prev : OP_Next;
drh487ab3c2001-11-08 00:45:21 +00001115 pLevel->p1 = pLevel->iCur;
1116 pLevel->p2 = start;
drh75897232000-05-29 14:26:00 +00001117 }
drh6a3ea0e2003-05-02 14:32:12 +00001118 loopMask |= getMask(&maskSet, iCur);
drh75897232000-05-29 14:26:00 +00001119
1120 /* Insert code to test every subexpression that can be completely
1121 ** computed using the current set of tables.
1122 */
drh193bd772004-07-20 18:23:14 +00001123 for(pTerm=aExpr, j=0; j<nExpr; j++, pTerm++){
1124 if( pTerm->p==0 ) continue;
1125 if( (pTerm->prereqAll & loopMask)!=pTerm->prereqAll ) continue;
1126 if( pLevel->iLeftJoin && !ExprHasProperty(pTerm->p,EP_FromJoin) ){
drh1f162302002-10-27 19:35:33 +00001127 continue;
1128 }
drh75897232000-05-29 14:26:00 +00001129 if( haveKey ){
drh573bd272001-02-19 23:23:38 +00001130 haveKey = 0;
drh7cf6e4d2004-05-19 14:56:55 +00001131 sqlite3VdbeAddOp(v, OP_MoveGe, iCur, 0);
drh75897232000-05-29 14:26:00 +00001132 }
drh193bd772004-07-20 18:23:14 +00001133 sqlite3ExprIfFalse(pParse, pTerm->p, cont, 1);
1134 pTerm->p = 0;
drh75897232000-05-29 14:26:00 +00001135 }
1136 brk = cont;
drhad2d8302002-05-24 20:31:36 +00001137
1138 /* For a LEFT OUTER JOIN, generate code that will record the fact that
1139 ** at least one row of the right table has matched the left table.
1140 */
1141 if( pLevel->iLeftJoin ){
danielk19774adee202004-05-08 08:23:19 +00001142 pLevel->top = sqlite3VdbeCurrentAddr(v);
1143 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1144 sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iLeftJoin, 1);
drhad6d9462004-09-19 02:15:24 +00001145 VdbeComment((v, "# record LEFT JOIN hit"));
drh193bd772004-07-20 18:23:14 +00001146 for(pTerm=aExpr, j=0; j<nExpr; j++, pTerm++){
1147 if( pTerm->p==0 ) continue;
1148 if( (pTerm->prereqAll & loopMask)!=pTerm->prereqAll ) continue;
drh1cc093c2002-06-24 22:01:57 +00001149 if( haveKey ){
drh3b167c72002-06-28 12:18:47 +00001150 /* Cannot happen. "haveKey" can only be true if pushKey is true
1151 ** an pushKey can only be true for DELETE and UPDATE and there are
1152 ** no outer joins with DELETE and UPDATE.
1153 */
drh1cc093c2002-06-24 22:01:57 +00001154 haveKey = 0;
drh7cf6e4d2004-05-19 14:56:55 +00001155 sqlite3VdbeAddOp(v, OP_MoveGe, iCur, 0);
drh1cc093c2002-06-24 22:01:57 +00001156 }
drh193bd772004-07-20 18:23:14 +00001157 sqlite3ExprIfFalse(pParse, pTerm->p, cont, 1);
1158 pTerm->p = 0;
drh1cc093c2002-06-24 22:01:57 +00001159 }
drhad2d8302002-05-24 20:31:36 +00001160 }
drh75897232000-05-29 14:26:00 +00001161 }
1162 pWInfo->iContinue = cont;
1163 if( pushKey && !haveKey ){
danielk19774adee202004-05-08 08:23:19 +00001164 sqlite3VdbeAddOp(v, OP_Recno, pTabList->a[0].iCursor, 0);
drh75897232000-05-29 14:26:00 +00001165 }
drh6a3ea0e2003-05-02 14:32:12 +00001166 freeMaskSet(&maskSet);
drh75897232000-05-29 14:26:00 +00001167 return pWInfo;
1168}
1169
1170/*
drhc27a1ce2002-06-14 20:58:45 +00001171** Generate the end of the WHERE loop. See comments on
danielk19774adee202004-05-08 08:23:19 +00001172** sqlite3WhereBegin() for additional information.
drh75897232000-05-29 14:26:00 +00001173*/
danielk19774adee202004-05-08 08:23:19 +00001174void sqlite3WhereEnd(WhereInfo *pWInfo){
drh75897232000-05-29 14:26:00 +00001175 Vdbe *v = pWInfo->pParse->pVdbe;
drh19a775c2000-06-05 18:54:46 +00001176 int i;
drh6b563442001-11-07 16:48:26 +00001177 WhereLevel *pLevel;
drhad3cab52002-05-24 02:04:32 +00001178 SrcList *pTabList = pWInfo->pTabList;
drh19a775c2000-06-05 18:54:46 +00001179
drhad3cab52002-05-24 02:04:32 +00001180 for(i=pTabList->nSrc-1; i>=0; i--){
drh6b563442001-11-07 16:48:26 +00001181 pLevel = &pWInfo->a[i];
danielk19774adee202004-05-08 08:23:19 +00001182 sqlite3VdbeResolveLabel(v, pLevel->cont);
drh6b563442001-11-07 16:48:26 +00001183 if( pLevel->op!=OP_Noop ){
danielk19774adee202004-05-08 08:23:19 +00001184 sqlite3VdbeAddOp(v, pLevel->op, pLevel->p1, pLevel->p2);
drh19a775c2000-06-05 18:54:46 +00001185 }
danielk19774adee202004-05-08 08:23:19 +00001186 sqlite3VdbeResolveLabel(v, pLevel->brk);
drhd99f7062002-06-08 23:25:08 +00001187 if( pLevel->inOp!=OP_Noop ){
danielk19774adee202004-05-08 08:23:19 +00001188 sqlite3VdbeAddOp(v, pLevel->inOp, pLevel->inP1, pLevel->inP2);
drhd99f7062002-06-08 23:25:08 +00001189 }
drhad2d8302002-05-24 20:31:36 +00001190 if( pLevel->iLeftJoin ){
1191 int addr;
danielk19774adee202004-05-08 08:23:19 +00001192 addr = sqlite3VdbeAddOp(v, OP_MemLoad, pLevel->iLeftJoin, 0);
1193 sqlite3VdbeAddOp(v, OP_NotNull, 1, addr+4 + (pLevel->iCur>=0));
1194 sqlite3VdbeAddOp(v, OP_NullRow, pTabList->a[i].iCursor, 0);
drh7f09b3e2002-08-13 13:15:49 +00001195 if( pLevel->iCur>=0 ){
danielk19774adee202004-05-08 08:23:19 +00001196 sqlite3VdbeAddOp(v, OP_NullRow, pLevel->iCur, 0);
drh7f09b3e2002-08-13 13:15:49 +00001197 }
danielk19774adee202004-05-08 08:23:19 +00001198 sqlite3VdbeAddOp(v, OP_Goto, 0, pLevel->top);
drhad2d8302002-05-24 20:31:36 +00001199 }
drh19a775c2000-06-05 18:54:46 +00001200 }
danielk19774adee202004-05-08 08:23:19 +00001201 sqlite3VdbeResolveLabel(v, pWInfo->iBreak);
drhad3cab52002-05-24 02:04:32 +00001202 for(i=0; i<pTabList->nSrc; i++){
drh5cf590c2003-04-24 01:45:04 +00001203 Table *pTab = pTabList->a[i].pTab;
1204 assert( pTab!=0 );
1205 if( pTab->isTransient || pTab->pSelect ) continue;
drh6b563442001-11-07 16:48:26 +00001206 pLevel = &pWInfo->a[i];
danielk19774adee202004-05-08 08:23:19 +00001207 sqlite3VdbeAddOp(v, OP_Close, pTabList->a[i].iCursor, 0);
drh6b563442001-11-07 16:48:26 +00001208 if( pLevel->pIdx!=0 ){
danielk19774adee202004-05-08 08:23:19 +00001209 sqlite3VdbeAddOp(v, OP_Close, pLevel->iCur, 0);
drh6b563442001-11-07 16:48:26 +00001210 }
drh19a775c2000-06-05 18:54:46 +00001211 }
drh75897232000-05-29 14:26:00 +00001212 sqliteFree(pWInfo);
1213 return;
1214}