blob: 4110431e423202270c1be9ba4a28f9a10b2d2e80 [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**
drh94a11212004-09-25 13:12:14 +000015** $Id: where.c,v 1.115 2004/09/25 13:12:16 drh 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){
151 switch( op ){
152 case TK_LT:
153 case TK_LE:
154 case TK_GT:
155 case TK_GE:
156 case TK_EQ:
drhd99f7062002-06-08 23:25:08 +0000157 case TK_IN:
drh487ab3c2001-11-08 00:45:21 +0000158 return 1;
159 default:
160 return 0;
161 }
162}
163
164/*
drh193bd772004-07-20 18:23:14 +0000165** Swap two integers.
166*/
167#define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;}
168
169/*
170** Return the index in the SrcList that uses cursor iCur. If iCur is
171** used by the first entry in SrcList return 0. If iCur is used by
172** the second entry return 1. And so forth.
173**
174** SrcList is the set of tables in the FROM clause in the order that
175** they will be processed. The value returned here gives us an index
176** of which tables will be processed first.
177*/
178static int tableOrder(SrcList *pList, int iCur){
179 int i;
180 for(i=0; i<pList->nSrc; i++){
181 if( pList->a[i].iCursor==iCur ) return i;
182 }
183 return -1;
184}
185
186/*
drh75897232000-05-29 14:26:00 +0000187** The input to this routine is an ExprInfo structure with only the
188** "p" field filled in. The job of this routine is to analyze the
189** subexpression and populate all the other fields of the ExprInfo
190** structure.
191*/
drh193bd772004-07-20 18:23:14 +0000192static void exprAnalyze(SrcList *pSrc, ExprMaskSet *pMaskSet, ExprInfo *pInfo){
drh75897232000-05-29 14:26:00 +0000193 Expr *pExpr = pInfo->p;
drh6a3ea0e2003-05-02 14:32:12 +0000194 pInfo->prereqLeft = exprTableUsage(pMaskSet, pExpr->pLeft);
195 pInfo->prereqRight = exprTableUsage(pMaskSet, pExpr->pRight);
196 pInfo->prereqAll = exprTableUsage(pMaskSet, pExpr);
drh75897232000-05-29 14:26:00 +0000197 pInfo->indexable = 0;
198 pInfo->idxLeft = -1;
199 pInfo->idxRight = -1;
drh487ab3c2001-11-08 00:45:21 +0000200 if( allowedOp(pExpr->op) && (pInfo->prereqRight & pInfo->prereqLeft)==0 ){
drhd99f7062002-06-08 23:25:08 +0000201 if( pExpr->pRight && pExpr->pRight->op==TK_COLUMN ){
drh6a3ea0e2003-05-02 14:32:12 +0000202 pInfo->idxRight = pExpr->pRight->iTable;
drh75897232000-05-29 14:26:00 +0000203 pInfo->indexable = 1;
204 }
drh967e8b72000-06-21 13:59:10 +0000205 if( pExpr->pLeft->op==TK_COLUMN ){
drh6a3ea0e2003-05-02 14:32:12 +0000206 pInfo->idxLeft = pExpr->pLeft->iTable;
drh75897232000-05-29 14:26:00 +0000207 pInfo->indexable = 1;
208 }
209 }
drh193bd772004-07-20 18:23:14 +0000210 if( pInfo->indexable ){
211 assert( pInfo->idxLeft!=pInfo->idxRight );
212
213 /* We want the expression to be of the form "X = expr", not "expr = X".
214 ** So flip it over if necessary. If the expression is "X = Y", then
215 ** we want Y to come from an earlier table than X.
216 **
217 ** The collating sequence rule is to always choose the left expression.
218 ** So if we do a flip, we also have to move the collating sequence.
219 */
220 if( tableOrder(pSrc,pInfo->idxLeft)<tableOrder(pSrc,pInfo->idxRight) ){
221 assert( pExpr->op!=TK_IN );
222 SWAP(CollSeq*,pExpr->pRight->pColl,pExpr->pLeft->pColl);
223 SWAP(Expr*,pExpr->pRight,pExpr->pLeft);
224 switch( pExpr->op ){
225 case TK_LT: pExpr->op = TK_GT; break;
226 case TK_LE: pExpr->op = TK_GE; break;
227 case TK_GT: pExpr->op = TK_LT; break;
228 case TK_GE: pExpr->op = TK_LE; break;
229 default: break;
230 }
231 SWAP(unsigned, pInfo->prereqLeft, pInfo->prereqRight);
232 SWAP(short int, pInfo->idxLeft, pInfo->idxRight);
233 }
234 }
235
drh75897232000-05-29 14:26:00 +0000236}
237
238/*
drhe3184742002-06-19 14:27:05 +0000239** pOrderBy is an ORDER BY clause from a SELECT statement. pTab is the
240** left-most table in the FROM clause of that same SELECT statement and
241** the table has a cursor number of "base".
242**
243** This routine attempts to find an index for pTab that generates the
244** correct record sequence for the given ORDER BY clause. The return value
245** is a pointer to an index that does the job. NULL is returned if the
246** table has no index that will generate the correct sort order.
247**
248** If there are two or more indices that generate the correct sort order
249** and pPreferredIdx is one of those indices, then return pPreferredIdx.
drhdd4852c2002-12-04 21:50:16 +0000250**
251** nEqCol is the number of columns of pPreferredIdx that are used as
252** equality constraints. Any index returned must have exactly this same
253** set of columns. The ORDER BY clause only matches index columns beyond the
254** the first nEqCol columns.
255**
256** All terms of the ORDER BY clause must be either ASC or DESC. The
257** *pbRev value is set to 1 if the ORDER BY clause is all DESC and it is
258** set to 0 if the ORDER BY clause is all ASC.
drhe3184742002-06-19 14:27:05 +0000259*/
260static Index *findSortingIndex(
danielk1977d2b65b92004-06-10 10:51:47 +0000261 Parse *pParse,
drhe3184742002-06-19 14:27:05 +0000262 Table *pTab, /* The table to be sorted */
263 int base, /* Cursor number for pTab */
264 ExprList *pOrderBy, /* The ORDER BY clause */
drhc045ec52002-12-04 20:01:06 +0000265 Index *pPreferredIdx, /* Use this index, if possible and not NULL */
drhdd4852c2002-12-04 21:50:16 +0000266 int nEqCol, /* Number of index columns used with == constraints */
drhc045ec52002-12-04 20:01:06 +0000267 int *pbRev /* Set to 1 if ORDER BY is DESC */
drhe3184742002-06-19 14:27:05 +0000268){
drhdd4852c2002-12-04 21:50:16 +0000269 int i, j;
drhe3184742002-06-19 14:27:05 +0000270 Index *pMatch;
271 Index *pIdx;
drhc045ec52002-12-04 20:01:06 +0000272 int sortOrder;
drh9bb575f2004-09-06 17:24:11 +0000273 sqlite3 *db = pParse->db;
drhe3184742002-06-19 14:27:05 +0000274
275 assert( pOrderBy!=0 );
276 assert( pOrderBy->nExpr>0 );
drhd3d39e92004-05-20 22:16:29 +0000277 sortOrder = pOrderBy->a[0].sortOrder;
drhe3184742002-06-19 14:27:05 +0000278 for(i=0; i<pOrderBy->nExpr; i++){
279 Expr *p;
drhd3d39e92004-05-20 22:16:29 +0000280 if( pOrderBy->a[i].sortOrder!=sortOrder ){
drhc045ec52002-12-04 20:01:06 +0000281 /* Indices can only be used if all ORDER BY terms are either
282 ** DESC or ASC. Indices cannot be used on a mixture. */
drhe3184742002-06-19 14:27:05 +0000283 return 0;
284 }
285 p = pOrderBy->a[i].pExpr;
286 if( p->op!=TK_COLUMN || p->iTable!=base ){
287 /* Can not use an index sort on anything that is not a column in the
288 ** left-most table of the FROM clause */
289 return 0;
290 }
291 }
danielk19770202b292004-06-09 09:55:16 +0000292
drhe3184742002-06-19 14:27:05 +0000293 /* If we get this far, it means the ORDER BY clause consists only of
294 ** ascending columns in the left-most table of the FROM clause. Now
295 ** check for a matching index.
296 */
297 pMatch = 0;
298 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drhdd4852c2002-12-04 21:50:16 +0000299 int nExpr = pOrderBy->nExpr;
300 if( pIdx->nColumn < nEqCol || pIdx->nColumn < nExpr ) continue;
301 for(i=j=0; i<nEqCol; i++){
danielk1977d2b65b92004-06-10 10:51:47 +0000302 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pOrderBy->a[j].pExpr);
danielk19770202b292004-06-09 09:55:16 +0000303 if( !pColl ) pColl = db->pDfltColl;
drhdd4852c2002-12-04 21:50:16 +0000304 if( pPreferredIdx->aiColumn[i]!=pIdx->aiColumn[i] ) break;
danielk19770202b292004-06-09 09:55:16 +0000305 if( pPreferredIdx->keyInfo.aColl[i]!=pIdx->keyInfo.aColl[i] ) break;
306 if( j<nExpr &&
307 pOrderBy->a[j].pExpr->iColumn==pIdx->aiColumn[i] &&
308 pColl==pIdx->keyInfo.aColl[i]
309 ){
310 j++;
311 }
drhe3184742002-06-19 14:27:05 +0000312 }
drhdd4852c2002-12-04 21:50:16 +0000313 if( i<nEqCol ) continue;
314 for(i=0; i+j<nExpr; i++){
danielk1977d2b65b92004-06-10 10:51:47 +0000315 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pOrderBy->a[i+j].pExpr);
danielk19770202b292004-06-09 09:55:16 +0000316 if( !pColl ) pColl = db->pDfltColl;
317 if( pOrderBy->a[i+j].pExpr->iColumn!=pIdx->aiColumn[i+nEqCol] ||
318 pColl!=pIdx->keyInfo.aColl[i+nEqCol] ) break;
drhdd4852c2002-12-04 21:50:16 +0000319 }
320 if( i+j>=nExpr ){
drhe3184742002-06-19 14:27:05 +0000321 pMatch = pIdx;
322 if( pIdx==pPreferredIdx ) break;
323 }
324 }
drhc045ec52002-12-04 20:01:06 +0000325 if( pMatch && pbRev ){
326 *pbRev = sortOrder==SQLITE_SO_DESC;
327 }
drhe3184742002-06-19 14:27:05 +0000328 return pMatch;
329}
330
331/*
drh2ffb1182004-07-19 19:14:01 +0000332** Disable a term in the WHERE clause. Except, do not disable the term
333** if it controls a LEFT OUTER JOIN and it did not originate in the ON
334** or USING clause of that join.
335**
336** Consider the term t2.z='ok' in the following queries:
337**
338** (1) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x WHERE t2.z='ok'
339** (2) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x AND t2.z='ok'
340** (3) SELECT * FROM t1, t2 WHERE t1.a=t2.x AND t2.z='ok'
341**
342** The t2.z='ok' is disabled in the in (2) because it did not originate
343** in the ON clause. The term is disabled in (3) because it is not part
344** of a LEFT OUTER JOIN. In (1), the term is not disabled.
345**
346** Disabling a term causes that term to not be tested in the inner loop
347** of the join. Disabling is an optimization. We would get the correct
348** results if nothing were ever disabled, but joins might run a little
349** slower. The trick is to disable as much as we can without disabling
350** too much. If we disabled in (1), we'd get the wrong answer.
351** See ticket #813.
352*/
353static void disableTerm(WhereLevel *pLevel, Expr **ppExpr){
354 Expr *pExpr = *ppExpr;
355 if( pLevel->iLeftJoin==0 || ExprHasProperty(pExpr, EP_FromJoin) ){
356 *ppExpr = 0;
357 }
358}
359
360/*
drh94a11212004-09-25 13:12:14 +0000361** Generate code that builds a probe for an index. Details:
362**
363** * Check the top nColumn entries on the stack. If any
364** of those entries are NULL, jump immediately to brk,
365** which is the loop exit, since no index entry will match
366** if any part of the key is NULL.
367**
368** * Construct a probe entry from the top nColumn entries in
369** the stack with affinities appropriate for index pIdx.
370*/
371static void buildIndexProbe(Vdbe *v, int nColumn, int brk, Index *pIdx){
372 sqlite3VdbeAddOp(v, OP_NotNull, -nColumn, sqlite3VdbeCurrentAddr(v)+3);
373 sqlite3VdbeAddOp(v, OP_Pop, nColumn, 0);
374 sqlite3VdbeAddOp(v, OP_Goto, 0, brk);
375 sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, 0);
376 sqlite3IndexAffinityStr(v, pIdx);
377}
378
379/*
380** Generate code for an equality term of the WHERE clause. An equality
381** term can be either X=expr or X IN (...). pTerm is the X.
382*/
383static void codeEqualityTerm(
384 Parse *pParse, /* The parsing context */
385 ExprInfo *pTerm, /* The term of the WHERE clause to be coded */
386 int brk, /* Jump here to abandon the loop */
387 WhereLevel *pLevel /* When level of the FROM clause we are working on */
388){
389 Expr *pX = pTerm->p;
390 if( pX->op!=TK_IN ){
391 assert( pX->op==TK_EQ );
392 sqlite3ExprCode(pParse, pX->pRight);
393 }else{
394 int iTab = pX->iTable;
395 Vdbe *v = pParse->pVdbe;
396 sqlite3VdbeAddOp(v, OP_Rewind, iTab, brk);
397 sqlite3VdbeAddOp(v, OP_KeyAsData, iTab, 1);
398 pLevel->inP2 = sqlite3VdbeAddOp(v, OP_IdxColumn, iTab, 0);
399 pLevel->inOp = OP_Next;
400 pLevel->inP1 = iTab;
401 }
402 disableTerm(pLevel, &pTerm->p);
403}
404
405
406/*
drhe3184742002-06-19 14:27:05 +0000407** Generate the beginning of the loop used for WHERE clause processing.
drh75897232000-05-29 14:26:00 +0000408** The return value is a pointer to an (opaque) structure that contains
409** information needed to terminate the loop. Later, the calling routine
danielk19774adee202004-05-08 08:23:19 +0000410** should invoke sqlite3WhereEnd() with the return value of this function
drh75897232000-05-29 14:26:00 +0000411** in order to complete the WHERE clause processing.
412**
413** If an error occurs, this routine returns NULL.
drhc27a1ce2002-06-14 20:58:45 +0000414**
415** The basic idea is to do a nested loop, one loop for each table in
416** the FROM clause of a select. (INSERT and UPDATE statements are the
417** same as a SELECT with only a single table in the FROM clause.) For
418** example, if the SQL is this:
419**
420** SELECT * FROM t1, t2, t3 WHERE ...;
421**
422** Then the code generated is conceptually like the following:
423**
424** foreach row1 in t1 do \ Code generated
danielk19774adee202004-05-08 08:23:19 +0000425** foreach row2 in t2 do |-- by sqlite3WhereBegin()
drhc27a1ce2002-06-14 20:58:45 +0000426** foreach row3 in t3 do /
427** ...
428** end \ Code generated
danielk19774adee202004-05-08 08:23:19 +0000429** end |-- by sqlite3WhereEnd()
drhc27a1ce2002-06-14 20:58:45 +0000430** end /
431**
432** There are Btree cursors associated with each table. t1 uses cursor
drh6a3ea0e2003-05-02 14:32:12 +0000433** number pTabList->a[0].iCursor. t2 uses the cursor pTabList->a[1].iCursor.
434** And so forth. This routine generates code to open those VDBE cursors
danielk19774adee202004-05-08 08:23:19 +0000435** and sqlite3WhereEnd() generates the code to close them.
drhc27a1ce2002-06-14 20:58:45 +0000436**
437** If the WHERE clause is empty, the foreach loops must each scan their
438** entire tables. Thus a three-way join is an O(N^3) operation. But if
439** the tables have indices and there are terms in the WHERE clause that
440** refer to those indices, a complete table scan can be avoided and the
441** code will run much faster. Most of the work of this routine is checking
442** to see if there are indices that can be used to speed up the loop.
443**
444** Terms of the WHERE clause are also used to limit which rows actually
445** make it to the "..." in the middle of the loop. After each "foreach",
446** terms of the WHERE clause that use only terms in that loop and outer
447** loops are evaluated and if false a jump is made around all subsequent
448** inner loops (or around the "..." if the test occurs within the inner-
449** most loop)
450**
451** OUTER JOINS
452**
453** An outer join of tables t1 and t2 is conceptally coded as follows:
454**
455** foreach row1 in t1 do
456** flag = 0
457** foreach row2 in t2 do
458** start:
459** ...
460** flag = 1
461** end
drhe3184742002-06-19 14:27:05 +0000462** if flag==0 then
463** move the row2 cursor to a null row
464** goto start
465** fi
drhc27a1ce2002-06-14 20:58:45 +0000466** end
467**
drhe3184742002-06-19 14:27:05 +0000468** ORDER BY CLAUSE PROCESSING
469**
470** *ppOrderBy is a pointer to the ORDER BY clause of a SELECT statement,
471** if there is one. If there is no ORDER BY clause or if this routine
472** is called from an UPDATE or DELETE statement, then ppOrderBy is NULL.
473**
474** If an index can be used so that the natural output order of the table
475** scan is correct for the ORDER BY clause, then that index is used and
476** *ppOrderBy is set to NULL. This is an optimization that prevents an
477** unnecessary sort of the result set if an index appropriate for the
478** ORDER BY clause already exists.
479**
480** If the where clause loops cannot be arranged to provide the correct
481** output order, then the *ppOrderBy is unchanged.
drh75897232000-05-29 14:26:00 +0000482*/
danielk19774adee202004-05-08 08:23:19 +0000483WhereInfo *sqlite3WhereBegin(
drh75897232000-05-29 14:26:00 +0000484 Parse *pParse, /* The parser context */
drhad3cab52002-05-24 02:04:32 +0000485 SrcList *pTabList, /* A list of all tables to be scanned */
drh75897232000-05-29 14:26:00 +0000486 Expr *pWhere, /* The WHERE clause */
drhe3184742002-06-19 14:27:05 +0000487 int pushKey, /* If TRUE, leave the table key on the stack */
488 ExprList **ppOrderBy /* An ORDER BY clause, or NULL */
drh75897232000-05-29 14:26:00 +0000489){
490 int i; /* Loop counter */
491 WhereInfo *pWInfo; /* Will become the return value of this function */
492 Vdbe *v = pParse->pVdbe; /* The virtual database engine */
drhd4f5ee22003-07-16 00:54:31 +0000493 int brk, cont = 0; /* Addresses used during code generation */
drh75897232000-05-29 14:26:00 +0000494 int nExpr; /* Number of subexpressions in the WHERE clause */
495 int loopMask; /* One bit set for each outer loop */
danielk1977f7df9cc2004-06-16 12:02:47 +0000496 int haveKey = 0; /* True if KEY is on the stack */
drh193bd772004-07-20 18:23:14 +0000497 ExprInfo *pTerm; /* A single term in the WHERE clause; ptr to aExpr[] */
drh6a3ea0e2003-05-02 14:32:12 +0000498 ExprMaskSet maskSet; /* The expression mask set */
drh8aff1012001-12-22 14:49:24 +0000499 int iDirectEq[32]; /* Term of the form ROWID==X for the N-th table */
500 int iDirectLt[32]; /* Term of the form ROWID<X or ROWID<=X */
501 int iDirectGt[32]; /* Term of the form ROWID>X or ROWID>=X */
drh193bd772004-07-20 18:23:14 +0000502 ExprInfo aExpr[101]; /* The WHERE clause is divided into these terms */
drh75897232000-05-29 14:26:00 +0000503
drhc27a1ce2002-06-14 20:58:45 +0000504 /* pushKey is only allowed if there is a single table (as in an INSERT or
505 ** UPDATE statement)
506 */
507 assert( pushKey==0 || pTabList->nSrc==1 );
drh83dcb1a2002-06-28 01:02:38 +0000508
509 /* Split the WHERE clause into separate subexpressions where each
510 ** subexpression is separated by an AND operator. If the aExpr[]
511 ** array fills up, the last entry might point to an expression which
512 ** contains additional unfactored AND operators.
513 */
drh6a3ea0e2003-05-02 14:32:12 +0000514 initMaskSet(&maskSet);
drh83dcb1a2002-06-28 01:02:38 +0000515 memset(aExpr, 0, sizeof(aExpr));
516 nExpr = exprSplit(ARRAYSIZE(aExpr), aExpr, pWhere);
517 if( nExpr==ARRAYSIZE(aExpr) ){
danielk19774adee202004-05-08 08:23:19 +0000518 sqlite3ErrorMsg(pParse, "WHERE clause too complex - no more "
drhf7a9e1a2004-02-22 18:40:56 +0000519 "than %d terms allowed", (int)ARRAYSIZE(aExpr)-1);
drh83dcb1a2002-06-28 01:02:38 +0000520 return 0;
521 }
drhc27a1ce2002-06-14 20:58:45 +0000522
drh75897232000-05-29 14:26:00 +0000523 /* Allocate and initialize the WhereInfo structure that will become the
524 ** return value.
525 */
drhad3cab52002-05-24 02:04:32 +0000526 pWInfo = sqliteMalloc( sizeof(WhereInfo) + pTabList->nSrc*sizeof(WhereLevel));
danielk1977132872b2004-05-10 10:37:18 +0000527 if( sqlite3_malloc_failed ){
drh193bd772004-07-20 18:23:14 +0000528 /* sqliteFree(pWInfo); // Leak memory when malloc fails */
drh75897232000-05-29 14:26:00 +0000529 return 0;
530 }
531 pWInfo->pParse = pParse;
532 pWInfo->pTabList = pTabList;
danielk19774adee202004-05-08 08:23:19 +0000533 pWInfo->iBreak = sqlite3VdbeMakeLabel(v);
drh08192d52002-04-30 19:20:28 +0000534
535 /* Special case: a WHERE clause that is constant. Evaluate the
536 ** expression and either jump over all of the code or fall thru.
537 */
danielk19774adee202004-05-08 08:23:19 +0000538 if( pWhere && (pTabList->nSrc==0 || sqlite3ExprIsConstant(pWhere)) ){
539 sqlite3ExprIfFalse(pParse, pWhere, pWInfo->iBreak, 1);
drhdf199a22002-06-14 22:38:41 +0000540 pWhere = 0;
drh08192d52002-04-30 19:20:28 +0000541 }
drh75897232000-05-29 14:26:00 +0000542
drh75897232000-05-29 14:26:00 +0000543 /* Analyze all of the subexpressions.
544 */
drh193bd772004-07-20 18:23:14 +0000545 for(pTerm=aExpr, i=0; i<nExpr; i++, pTerm++){
546 TriggerStack *pStack;
547 exprAnalyze(pTabList, &maskSet, pTerm);
drh1d1f3052002-05-21 13:18:25 +0000548
549 /* If we are executing a trigger body, remove all references to
550 ** new.* and old.* tables from the prerequisite masks.
551 */
drh193bd772004-07-20 18:23:14 +0000552 if( (pStack = pParse->trigStack)!=0 ){
drh1d1f3052002-05-21 13:18:25 +0000553 int x;
drh193bd772004-07-20 18:23:14 +0000554 if( (x=pStack->newIdx) >= 0 ){
drh6a3ea0e2003-05-02 14:32:12 +0000555 int mask = ~getMask(&maskSet, x);
drh193bd772004-07-20 18:23:14 +0000556 pTerm->prereqRight &= mask;
557 pTerm->prereqLeft &= mask;
558 pTerm->prereqAll &= mask;
drh1d1f3052002-05-21 13:18:25 +0000559 }
drh193bd772004-07-20 18:23:14 +0000560 if( (x=pStack->oldIdx) >= 0 ){
drh6a3ea0e2003-05-02 14:32:12 +0000561 int mask = ~getMask(&maskSet, x);
drh193bd772004-07-20 18:23:14 +0000562 pTerm->prereqRight &= mask;
563 pTerm->prereqLeft &= mask;
564 pTerm->prereqAll &= mask;
drh1d1f3052002-05-21 13:18:25 +0000565 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000566 }
drh75897232000-05-29 14:26:00 +0000567 }
568
drh75897232000-05-29 14:26:00 +0000569 /* Figure out what index to use (if any) for each nested loop.
drh6b563442001-11-07 16:48:26 +0000570 ** Make pWInfo->a[i].pIdx point to the index to use for the i-th nested
drhad3cab52002-05-24 02:04:32 +0000571 ** loop where i==0 is the outer loop and i==pTabList->nSrc-1 is the inner
drh8aff1012001-12-22 14:49:24 +0000572 ** loop.
573 **
574 ** If terms exist that use the ROWID of any table, then set the
575 ** iDirectEq[], iDirectLt[], or iDirectGt[] elements for that table
576 ** to the index of the term containing the ROWID. We always prefer
577 ** to use a ROWID which can directly access a table rather than an
drh0a36c572002-02-18 22:49:59 +0000578 ** index which requires reading an index first to get the rowid then
579 ** doing a second read of the actual database table.
drh75897232000-05-29 14:26:00 +0000580 **
581 ** Actually, if there are more than 32 tables in the join, only the
drh0a36c572002-02-18 22:49:59 +0000582 ** first 32 tables are candidates for indices. This is (again) due
583 ** to the limit of 32 bits in an integer bitmask.
drh75897232000-05-29 14:26:00 +0000584 */
585 loopMask = 0;
drhcb485882002-08-15 13:50:48 +0000586 for(i=0; i<pTabList->nSrc && i<ARRAYSIZE(iDirectEq); i++){
drhc4a3c772001-04-04 11:48:57 +0000587 int j;
drh94a11212004-09-25 13:12:14 +0000588 WhereLevel *pLevel = &pWInfo->a[i];
drh6a3ea0e2003-05-02 14:32:12 +0000589 int iCur = pTabList->a[i].iCursor; /* The cursor for this table */
590 int mask = getMask(&maskSet, iCur); /* Cursor mask for this table */
591 Table *pTab = pTabList->a[i].pTab;
drh75897232000-05-29 14:26:00 +0000592 Index *pIdx;
593 Index *pBestIdx = 0;
drh487ab3c2001-11-08 00:45:21 +0000594 int bestScore = 0;
drh75897232000-05-29 14:26:00 +0000595
drhc4a3c772001-04-04 11:48:57 +0000596 /* Check to see if there is an expression that uses only the
drh8aff1012001-12-22 14:49:24 +0000597 ** ROWID field of this table. For terms of the form ROWID==expr
598 ** set iDirectEq[i] to the index of the term. For terms of the
599 ** form ROWID<expr or ROWID<=expr set iDirectLt[i] to the term index.
600 ** For terms like ROWID>expr or ROWID>=expr set iDirectGt[i].
drh174b6192002-12-03 02:22:52 +0000601 **
602 ** (Added:) Treat ROWID IN expr like ROWID=expr.
drhc4a3c772001-04-04 11:48:57 +0000603 */
drh94a11212004-09-25 13:12:14 +0000604 pLevel->iCur = -1;
drh8aff1012001-12-22 14:49:24 +0000605 iDirectEq[i] = -1;
606 iDirectLt[i] = -1;
607 iDirectGt[i] = -1;
drh193bd772004-07-20 18:23:14 +0000608 for(pTerm=aExpr, j=0; j<nExpr; j++, pTerm++){
609 Expr *pX = pTerm->p;
610 if( pTerm->idxLeft==iCur && pX->pLeft->iColumn<0
611 && (pTerm->prereqRight & loopMask)==pTerm->prereqRight ){
612 switch( pX->op ){
drhd99f7062002-06-08 23:25:08 +0000613 case TK_IN:
drh8aff1012001-12-22 14:49:24 +0000614 case TK_EQ: iDirectEq[i] = j; break;
615 case TK_LE:
616 case TK_LT: iDirectLt[i] = j; break;
617 case TK_GE:
618 case TK_GT: iDirectGt[i] = j; break;
619 }
drhc4a3c772001-04-04 11:48:57 +0000620 }
drhc4a3c772001-04-04 11:48:57 +0000621 }
drh8aff1012001-12-22 14:49:24 +0000622 if( iDirectEq[i]>=0 ){
drh6a3ea0e2003-05-02 14:32:12 +0000623 loopMask |= mask;
drh94a11212004-09-25 13:12:14 +0000624 pLevel->pIdx = 0;
drhc4a3c772001-04-04 11:48:57 +0000625 continue;
626 }
627
drh75897232000-05-29 14:26:00 +0000628 /* Do a search for usable indices. Leave pBestIdx pointing to
drh487ab3c2001-11-08 00:45:21 +0000629 ** the "best" index. pBestIdx is left set to NULL if no indices
630 ** are usable.
drh75897232000-05-29 14:26:00 +0000631 **
drh487ab3c2001-11-08 00:45:21 +0000632 ** The best index is determined as follows. For each of the
633 ** left-most terms that is fixed by an equality operator, add
drhc045ec52002-12-04 20:01:06 +0000634 ** 8 to the score. The right-most term of the index may be
drh487ab3c2001-11-08 00:45:21 +0000635 ** constrained by an inequality. Add 1 if for an "x<..." constraint
636 ** and add 2 for an "x>..." constraint. Chose the index that
637 ** gives the best score.
638 **
639 ** This scoring system is designed so that the score can later be
drhc045ec52002-12-04 20:01:06 +0000640 ** used to determine how the index is used. If the score&7 is 0
drh487ab3c2001-11-08 00:45:21 +0000641 ** then all constraints are equalities. If score&1 is not 0 then
642 ** there is an inequality used as a termination key. (ex: "x<...")
643 ** If score&2 is not 0 then there is an inequality used as the
drhc045ec52002-12-04 20:01:06 +0000644 ** start key. (ex: "x>..."). A score or 4 is the special case
645 ** of an IN operator constraint. (ex: "x IN ...").
drhd99f7062002-06-08 23:25:08 +0000646 **
drhc27a1ce2002-06-14 20:58:45 +0000647 ** The IN operator (as in "<expr> IN (...)") is treated the same as
648 ** an equality comparison except that it can only be used on the
649 ** left-most column of an index and other terms of the WHERE clause
650 ** cannot be used in conjunction with the IN operator to help satisfy
651 ** other columns of the index.
drh75897232000-05-29 14:26:00 +0000652 */
653 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
drhc27a1ce2002-06-14 20:58:45 +0000654 int eqMask = 0; /* Index columns covered by an x=... term */
655 int ltMask = 0; /* Index columns covered by an x<... term */
656 int gtMask = 0; /* Index columns covered by an x>... term */
657 int inMask = 0; /* Index columns covered by an x IN .. term */
drh487ab3c2001-11-08 00:45:21 +0000658 int nEq, m, score;
drh75897232000-05-29 14:26:00 +0000659
drh487ab3c2001-11-08 00:45:21 +0000660 if( pIdx->nColumn>32 ) continue; /* Ignore indices too many columns */
drh193bd772004-07-20 18:23:14 +0000661 for(pTerm=aExpr, j=0; j<nExpr; j++, pTerm++){
drh193bd772004-07-20 18:23:14 +0000662 Expr *pX = pTerm->p;
drh94a11212004-09-25 13:12:14 +0000663 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pX->pLeft);
drh193bd772004-07-20 18:23:14 +0000664 if( !pColl && pX->pRight ){
665 pColl = sqlite3ExprCollSeq(pParse, pX->pRight);
danielk19770202b292004-06-09 09:55:16 +0000666 }
667 if( !pColl ){
668 pColl = pParse->db->pDfltColl;
669 }
drh193bd772004-07-20 18:23:14 +0000670 if( pTerm->idxLeft==iCur
671 && (pTerm->prereqRight & loopMask)==pTerm->prereqRight ){
672 int iColumn = pX->pLeft->iColumn;
drh75897232000-05-29 14:26:00 +0000673 int k;
danielk1977e014a832004-05-17 10:48:57 +0000674 char idxaff = pIdx->pTable->aCol[iColumn].affinity;
drh967e8b72000-06-21 13:59:10 +0000675 for(k=0; k<pIdx->nColumn; k++){
danielk19770202b292004-06-09 09:55:16 +0000676 /* If the collating sequences or affinities don't match,
677 ** ignore this index. */
678 if( pColl!=pIdx->keyInfo.aColl[k] ) continue;
drh193bd772004-07-20 18:23:14 +0000679 if( !sqlite3IndexAffinityOk(pX, idxaff) ) continue;
danielk19770202b292004-06-09 09:55:16 +0000680 if( pIdx->aiColumn[k]==iColumn ){
drh193bd772004-07-20 18:23:14 +0000681 switch( pX->op ){
drh48185c12002-06-09 01:55:20 +0000682 case TK_IN: {
683 if( k==0 ) inMask |= 1;
684 break;
685 }
drh487ab3c2001-11-08 00:45:21 +0000686 case TK_EQ: {
687 eqMask |= 1<<k;
688 break;
689 }
690 case TK_LE:
691 case TK_LT: {
692 ltMask |= 1<<k;
693 break;
694 }
695 case TK_GE:
696 case TK_GT: {
697 gtMask |= 1<<k;
698 break;
699 }
700 default: {
701 /* CANT_HAPPEN */
702 assert( 0 );
703 break;
704 }
705 }
drh75897232000-05-29 14:26:00 +0000706 break;
707 }
708 }
709 }
drh75897232000-05-29 14:26:00 +0000710 }
drhc045ec52002-12-04 20:01:06 +0000711
712 /* The following loop ends with nEq set to the number of columns
713 ** on the left of the index with == constraints.
714 */
drh487ab3c2001-11-08 00:45:21 +0000715 for(nEq=0; nEq<pIdx->nColumn; nEq++){
716 m = (1<<(nEq+1))-1;
717 if( (m & eqMask)!=m ) break;
718 }
drhc045ec52002-12-04 20:01:06 +0000719 score = nEq*8; /* Base score is 8 times number of == constraints */
drh487ab3c2001-11-08 00:45:21 +0000720 m = 1<<nEq;
drhc045ec52002-12-04 20:01:06 +0000721 if( m & ltMask ) score++; /* Increase score for a < constraint */
722 if( m & gtMask ) score+=2; /* Increase score for a > constraint */
723 if( score==0 && inMask ) score = 4; /* Default score for IN constraint */
drh487ab3c2001-11-08 00:45:21 +0000724 if( score>bestScore ){
725 pBestIdx = pIdx;
726 bestScore = score;
drh75897232000-05-29 14:26:00 +0000727 }
728 }
drh94a11212004-09-25 13:12:14 +0000729 pLevel->pIdx = pBestIdx;
730 pLevel->score = bestScore;
731 pLevel->bRev = 0;
drh6a3ea0e2003-05-02 14:32:12 +0000732 loopMask |= mask;
drh6b563442001-11-07 16:48:26 +0000733 if( pBestIdx ){
drh94a11212004-09-25 13:12:14 +0000734 pLevel->iCur = pParse->nTab++;
drh6b563442001-11-07 16:48:26 +0000735 }
drh75897232000-05-29 14:26:00 +0000736 }
737
drhe3184742002-06-19 14:27:05 +0000738 /* Check to see if the ORDER BY clause is or can be satisfied by the
739 ** use of an index on the first table.
740 */
741 if( ppOrderBy && *ppOrderBy && pTabList->nSrc>0 ){
742 Index *pSortIdx;
743 Index *pIdx;
744 Table *pTab;
drhc045ec52002-12-04 20:01:06 +0000745 int bRev = 0;
drhe3184742002-06-19 14:27:05 +0000746
747 pTab = pTabList->a[0].pTab;
748 pIdx = pWInfo->a[0].pIdx;
749 if( pIdx && pWInfo->a[0].score==4 ){
drhc045ec52002-12-04 20:01:06 +0000750 /* If there is already an IN index on the left-most table,
751 ** it will not give the correct sort order.
752 ** So, pretend that no suitable index is found.
drhe3184742002-06-19 14:27:05 +0000753 */
754 pSortIdx = 0;
755 }else if( iDirectEq[0]>=0 || iDirectLt[0]>=0 || iDirectGt[0]>=0 ){
756 /* If the left-most column is accessed using its ROWID, then do
757 ** not try to sort by index.
758 */
759 pSortIdx = 0;
760 }else{
drhdd4852c2002-12-04 21:50:16 +0000761 int nEqCol = (pWInfo->a[0].score+4)/8;
danielk1977d2b65b92004-06-10 10:51:47 +0000762 pSortIdx = findSortingIndex(pParse, pTab, pTabList->a[0].iCursor,
drh6a3ea0e2003-05-02 14:32:12 +0000763 *ppOrderBy, pIdx, nEqCol, &bRev);
drhe3184742002-06-19 14:27:05 +0000764 }
765 if( pSortIdx && (pIdx==0 || pIdx==pSortIdx) ){
766 if( pIdx==0 ){
767 pWInfo->a[0].pIdx = pSortIdx;
768 pWInfo->a[0].iCur = pParse->nTab++;
drhe3184742002-06-19 14:27:05 +0000769 }
drhc045ec52002-12-04 20:01:06 +0000770 pWInfo->a[0].bRev = bRev;
drhe3184742002-06-19 14:27:05 +0000771 *ppOrderBy = 0;
772 }
773 }
774
drh6b563442001-11-07 16:48:26 +0000775 /* Open all tables in the pTabList and all indices used by those tables.
drh75897232000-05-29 14:26:00 +0000776 */
drhc275b4e2004-07-19 17:25:24 +0000777 sqlite3CodeVerifySchema(pParse, -1); /* Insert the cookie verifier Goto */
drhad3cab52002-05-24 02:04:32 +0000778 for(i=0; i<pTabList->nSrc; i++){
drhf57b3392001-10-08 13:22:32 +0000779 Table *pTab;
drh701a0ae2004-02-22 20:05:00 +0000780 Index *pIx;
drhf57b3392001-10-08 13:22:32 +0000781
782 pTab = pTabList->a[i].pTab;
drha76b5df2002-02-23 02:32:10 +0000783 if( pTab->isTransient || pTab->pSelect ) continue;
drhad6d9462004-09-19 02:15:24 +0000784 sqlite3OpenTableForReading(v, pTabList->a[i].iCursor, pTab);
danielk1977f9d19a62004-06-14 08:26:35 +0000785 sqlite3CodeVerifySchema(pParse, pTab->iDb);
drh701a0ae2004-02-22 20:05:00 +0000786 if( (pIx = pWInfo->a[i].pIdx)!=0 ){
danielk19774adee202004-05-08 08:23:19 +0000787 sqlite3VdbeAddOp(v, OP_Integer, pIx->iDb, 0);
drhd3d39e92004-05-20 22:16:29 +0000788 sqlite3VdbeOp3(v, OP_OpenRead, pWInfo->a[i].iCur, pIx->tnum,
789 (char*)&pIx->keyInfo, P3_KEYINFO);
drh75897232000-05-29 14:26:00 +0000790 }
791 }
792
793 /* Generate the code to do the search
794 */
drh75897232000-05-29 14:26:00 +0000795 loopMask = 0;
drhad3cab52002-05-24 02:04:32 +0000796 for(i=0; i<pTabList->nSrc; i++){
drh75897232000-05-29 14:26:00 +0000797 int j, k;
drh6a3ea0e2003-05-02 14:32:12 +0000798 int iCur = pTabList->a[i].iCursor;
drhc4a3c772001-04-04 11:48:57 +0000799 Index *pIdx;
drh6b563442001-11-07 16:48:26 +0000800 WhereLevel *pLevel = &pWInfo->a[i];
drh75897232000-05-29 14:26:00 +0000801
drhad2d8302002-05-24 20:31:36 +0000802 /* If this is the right table of a LEFT OUTER JOIN, allocate and
drh174b6192002-12-03 02:22:52 +0000803 ** initialize a memory cell that records if this table matches any
drhc27a1ce2002-06-14 20:58:45 +0000804 ** row of the left table of the join.
drhad2d8302002-05-24 20:31:36 +0000805 */
806 if( i>0 && (pTabList->a[i-1].jointype & JT_LEFT)!=0 ){
807 if( !pParse->nMem ) pParse->nMem++;
808 pLevel->iLeftJoin = pParse->nMem++;
danielk19770f69c1e2004-05-29 11:24:50 +0000809 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk19774adee202004-05-08 08:23:19 +0000810 sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iLeftJoin, 1);
drhad6d9462004-09-19 02:15:24 +0000811 VdbeComment((v, "# init LEFT JOIN no-match flag"));
drhad2d8302002-05-24 20:31:36 +0000812 }
813
drh8aff1012001-12-22 14:49:24 +0000814 pIdx = pLevel->pIdx;
drhd99f7062002-06-08 23:25:08 +0000815 pLevel->inOp = OP_Noop;
drh94a11212004-09-25 13:12:14 +0000816 if( i<ARRAYSIZE(iDirectEq) && (k = iDirectEq[i])>=0 ){
drh8aff1012001-12-22 14:49:24 +0000817 /* Case 1: We can directly reference a single row using an
drhc27a1ce2002-06-14 20:58:45 +0000818 ** equality comparison against the ROWID field. Or
819 ** we reference multiple rows using a "rowid IN (...)"
820 ** construct.
drhc4a3c772001-04-04 11:48:57 +0000821 */
drh8aff1012001-12-22 14:49:24 +0000822 assert( k<nExpr );
drh193bd772004-07-20 18:23:14 +0000823 pTerm = &aExpr[k];
824 assert( pTerm->p!=0 );
drh193bd772004-07-20 18:23:14 +0000825 assert( pTerm->idxLeft==iCur );
drh94a11212004-09-25 13:12:14 +0000826 brk = pLevel->brk = sqlite3VdbeMakeLabel(v);
827 codeEqualityTerm(pParse, pTerm, brk, pLevel);
danielk19774adee202004-05-08 08:23:19 +0000828 cont = pLevel->cont = sqlite3VdbeMakeLabel(v);
829 sqlite3VdbeAddOp(v, OP_MustBeInt, 1, brk);
drhd99f7062002-06-08 23:25:08 +0000830 haveKey = 0;
danielk19774adee202004-05-08 08:23:19 +0000831 sqlite3VdbeAddOp(v, OP_NotExists, iCur, brk);
drh6b563442001-11-07 16:48:26 +0000832 pLevel->op = OP_Noop;
drhe3184742002-06-19 14:27:05 +0000833 }else if( pIdx!=0 && pLevel->score>0 && pLevel->score%4==0 ){
drhc27a1ce2002-06-14 20:58:45 +0000834 /* Case 2: There is an index and all terms of the WHERE clause that
835 ** refer to the index use the "==" or "IN" operators.
drh75897232000-05-29 14:26:00 +0000836 */
drh6b563442001-11-07 16:48:26 +0000837 int start;
drhc045ec52002-12-04 20:01:06 +0000838 int nColumn = (pLevel->score+4)/8;
danielk19774adee202004-05-08 08:23:19 +0000839 brk = pLevel->brk = sqlite3VdbeMakeLabel(v);
drh772ae622004-05-19 13:13:08 +0000840
841 /* For each column of the index, find the term of the WHERE clause that
842 ** constraints that column. If the WHERE clause term is X=expr, then
843 ** evaluation expr and leave the result on the stack */
drh487ab3c2001-11-08 00:45:21 +0000844 for(j=0; j<nColumn; j++){
drh193bd772004-07-20 18:23:14 +0000845 for(pTerm=aExpr, k=0; k<nExpr; k++, pTerm++){
846 Expr *pX = pTerm->p;
drhd99f7062002-06-08 23:25:08 +0000847 if( pX==0 ) continue;
drh193bd772004-07-20 18:23:14 +0000848 if( pTerm->idxLeft==iCur
849 && (pTerm->prereqRight & loopMask)==pTerm->prereqRight
drhd99f7062002-06-08 23:25:08 +0000850 && pX->pLeft->iColumn==pIdx->aiColumn[j]
drh75897232000-05-29 14:26:00 +0000851 ){
danielk1977e014a832004-05-17 10:48:57 +0000852 char idxaff = pIdx->pTable->aCol[pX->pLeft->iColumn].affinity;
drh94a11212004-09-25 13:12:14 +0000853 if( sqlite3IndexAffinityOk(pX, idxaff) ){
854 codeEqualityTerm(pParse, pTerm, brk, pLevel);
855 break;
drhd99f7062002-06-08 23:25:08 +0000856 }
drh75897232000-05-29 14:26:00 +0000857 }
drh75897232000-05-29 14:26:00 +0000858 }
859 }
drh6b563442001-11-07 16:48:26 +0000860 pLevel->iMem = pParse->nMem++;
danielk19774adee202004-05-08 08:23:19 +0000861 cont = pLevel->cont = sqlite3VdbeMakeLabel(v);
drh94a11212004-09-25 13:12:14 +0000862 buildIndexProbe(v, nColumn, brk, pIdx);
danielk19773d1bfea2004-05-14 11:00:53 +0000863 sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iMem, 0);
drh772ae622004-05-19 13:13:08 +0000864
drh772ae622004-05-19 13:13:08 +0000865 /* Generate code (1) to move to the first matching element of the table.
866 ** Then generate code (2) that jumps to "brk" after the cursor is past
867 ** the last matching element of the table. The code (1) is executed
868 ** once to initialize the search, the code (2) is executed before each
869 ** iteration of the scan to see if the scan has finished. */
drhc045ec52002-12-04 20:01:06 +0000870 if( pLevel->bRev ){
871 /* Scan in reverse order */
drh7cf6e4d2004-05-19 14:56:55 +0000872 sqlite3VdbeAddOp(v, OP_MoveLe, pLevel->iCur, brk);
danielk19774adee202004-05-08 08:23:19 +0000873 start = sqlite3VdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
874 sqlite3VdbeAddOp(v, OP_IdxLT, pLevel->iCur, brk);
drhc045ec52002-12-04 20:01:06 +0000875 pLevel->op = OP_Prev;
876 }else{
877 /* Scan in the forward order */
drh7cf6e4d2004-05-19 14:56:55 +0000878 sqlite3VdbeAddOp(v, OP_MoveGe, pLevel->iCur, brk);
danielk19774adee202004-05-08 08:23:19 +0000879 start = sqlite3VdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
drhfec19aa2004-05-19 20:41:03 +0000880 sqlite3VdbeOp3(v, OP_IdxGE, pLevel->iCur, brk, "+", P3_STATIC);
drhc045ec52002-12-04 20:01:06 +0000881 pLevel->op = OP_Next;
882 }
danielk19774adee202004-05-08 08:23:19 +0000883 sqlite3VdbeAddOp(v, OP_RowKey, pLevel->iCur, 0);
884 sqlite3VdbeAddOp(v, OP_IdxIsNull, nColumn, cont);
885 sqlite3VdbeAddOp(v, OP_IdxRecno, pLevel->iCur, 0);
drhad3cab52002-05-24 02:04:32 +0000886 if( i==pTabList->nSrc-1 && pushKey ){
drh75897232000-05-29 14:26:00 +0000887 haveKey = 1;
888 }else{
drh7cf6e4d2004-05-19 14:56:55 +0000889 sqlite3VdbeAddOp(v, OP_MoveGe, iCur, 0);
drh75897232000-05-29 14:26:00 +0000890 haveKey = 0;
891 }
drh6b563442001-11-07 16:48:26 +0000892 pLevel->p1 = pLevel->iCur;
893 pLevel->p2 = start;
drh8aff1012001-12-22 14:49:24 +0000894 }else if( i<ARRAYSIZE(iDirectLt) && (iDirectLt[i]>=0 || iDirectGt[i]>=0) ){
895 /* Case 3: We have an inequality comparison against the ROWID field.
896 */
897 int testOp = OP_Noop;
898 int start;
899
danielk19774adee202004-05-08 08:23:19 +0000900 brk = pLevel->brk = sqlite3VdbeMakeLabel(v);
901 cont = pLevel->cont = sqlite3VdbeMakeLabel(v);
drh8aff1012001-12-22 14:49:24 +0000902 if( iDirectGt[i]>=0 ){
drh94a11212004-09-25 13:12:14 +0000903 Expr *pX;
drh8aff1012001-12-22 14:49:24 +0000904 k = iDirectGt[i];
905 assert( k<nExpr );
drh193bd772004-07-20 18:23:14 +0000906 pTerm = &aExpr[k];
drh94a11212004-09-25 13:12:14 +0000907 pX = pTerm->p;
908 assert( pX!=0 );
drh193bd772004-07-20 18:23:14 +0000909 assert( pTerm->idxLeft==iCur );
drh94a11212004-09-25 13:12:14 +0000910 sqlite3ExprCode(pParse, pX->pRight);
911 sqlite3VdbeAddOp(v, OP_ForceInt, pX->op==TK_LT || pX->op==TK_GT, brk);
drh7cf6e4d2004-05-19 14:56:55 +0000912 sqlite3VdbeAddOp(v, OP_MoveGe, iCur, brk);
drh193bd772004-07-20 18:23:14 +0000913 disableTerm(pLevel, &pTerm->p);
drh8aff1012001-12-22 14:49:24 +0000914 }else{
danielk19774adee202004-05-08 08:23:19 +0000915 sqlite3VdbeAddOp(v, OP_Rewind, iCur, brk);
drh8aff1012001-12-22 14:49:24 +0000916 }
917 if( iDirectLt[i]>=0 ){
drh94a11212004-09-25 13:12:14 +0000918 Expr *pX;
drh8aff1012001-12-22 14:49:24 +0000919 k = iDirectLt[i];
920 assert( k<nExpr );
drh193bd772004-07-20 18:23:14 +0000921 pTerm = &aExpr[k];
drh94a11212004-09-25 13:12:14 +0000922 pX = pTerm->p;
923 assert( pX!=0 );
drh193bd772004-07-20 18:23:14 +0000924 assert( pTerm->idxLeft==iCur );
drh94a11212004-09-25 13:12:14 +0000925 sqlite3ExprCode(pParse, pX->pRight);
drh8aff1012001-12-22 14:49:24 +0000926 pLevel->iMem = pParse->nMem++;
danielk19774adee202004-05-08 08:23:19 +0000927 sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
drh94a11212004-09-25 13:12:14 +0000928 if( pX->op==TK_LT || pX->op==TK_GT ){
drh8aff1012001-12-22 14:49:24 +0000929 testOp = OP_Ge;
930 }else{
931 testOp = OP_Gt;
932 }
drh193bd772004-07-20 18:23:14 +0000933 disableTerm(pLevel, &pTerm->p);
drh8aff1012001-12-22 14:49:24 +0000934 }
danielk19774adee202004-05-08 08:23:19 +0000935 start = sqlite3VdbeCurrentAddr(v);
drh8aff1012001-12-22 14:49:24 +0000936 pLevel->op = OP_Next;
drh6a3ea0e2003-05-02 14:32:12 +0000937 pLevel->p1 = iCur;
drh8aff1012001-12-22 14:49:24 +0000938 pLevel->p2 = start;
939 if( testOp!=OP_Noop ){
danielk19774adee202004-05-08 08:23:19 +0000940 sqlite3VdbeAddOp(v, OP_Recno, iCur, 0);
941 sqlite3VdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
942 sqlite3VdbeAddOp(v, testOp, 0, brk);
drh8aff1012001-12-22 14:49:24 +0000943 }
944 haveKey = 0;
945 }else if( pIdx==0 ){
drhc27a1ce2002-06-14 20:58:45 +0000946 /* Case 4: There is no usable index. We must do a complete
drh8aff1012001-12-22 14:49:24 +0000947 ** scan of the entire database table.
948 */
949 int start;
950
danielk19774adee202004-05-08 08:23:19 +0000951 brk = pLevel->brk = sqlite3VdbeMakeLabel(v);
952 cont = pLevel->cont = sqlite3VdbeMakeLabel(v);
953 sqlite3VdbeAddOp(v, OP_Rewind, iCur, brk);
954 start = sqlite3VdbeCurrentAddr(v);
drh8aff1012001-12-22 14:49:24 +0000955 pLevel->op = OP_Next;
drh6a3ea0e2003-05-02 14:32:12 +0000956 pLevel->p1 = iCur;
drh8aff1012001-12-22 14:49:24 +0000957 pLevel->p2 = start;
958 haveKey = 0;
drh487ab3c2001-11-08 00:45:21 +0000959 }else{
drhc27a1ce2002-06-14 20:58:45 +0000960 /* Case 5: The WHERE clause term that refers to the right-most
961 ** column of the index is an inequality. For example, if
962 ** the index is on (x,y,z) and the WHERE clause is of the
963 ** form "x=5 AND y<10" then this case is used. Only the
964 ** right-most column can be an inequality - the rest must
965 ** use the "==" operator.
drhe3184742002-06-19 14:27:05 +0000966 **
967 ** This case is also used when there are no WHERE clause
968 ** constraints but an index is selected anyway, in order
969 ** to force the output order to conform to an ORDER BY.
drh487ab3c2001-11-08 00:45:21 +0000970 */
971 int score = pLevel->score;
drhc045ec52002-12-04 20:01:06 +0000972 int nEqColumn = score/8;
drh487ab3c2001-11-08 00:45:21 +0000973 int start;
danielk1977f7df9cc2004-06-16 12:02:47 +0000974 int leFlag=0, geFlag=0;
drh487ab3c2001-11-08 00:45:21 +0000975 int testOp;
976
977 /* Evaluate the equality constraints
978 */
979 for(j=0; j<nEqColumn; j++){
drh94a11212004-09-25 13:12:14 +0000980 int iIdxCol = pIdx->aiColumn[j];
drh193bd772004-07-20 18:23:14 +0000981 for(pTerm=aExpr, k=0; k<nExpr; k++, pTerm++){
drh94a11212004-09-25 13:12:14 +0000982 Expr *pX = pTerm->p;
983 if( pX==0 ) continue;
drh193bd772004-07-20 18:23:14 +0000984 if( pTerm->idxLeft==iCur
drh94a11212004-09-25 13:12:14 +0000985 && pX->op==TK_EQ
drh193bd772004-07-20 18:23:14 +0000986 && (pTerm->prereqRight & loopMask)==pTerm->prereqRight
drh94a11212004-09-25 13:12:14 +0000987 && pX->pLeft->iColumn==iIdxCol
drh487ab3c2001-11-08 00:45:21 +0000988 ){
drh94a11212004-09-25 13:12:14 +0000989 sqlite3ExprCode(pParse, pX->pRight);
drh193bd772004-07-20 18:23:14 +0000990 disableTerm(pLevel, &pTerm->p);
drh487ab3c2001-11-08 00:45:21 +0000991 break;
992 }
993 }
994 }
995
drhc27a1ce2002-06-14 20:58:45 +0000996 /* Duplicate the equality term values because they will all be
drh487ab3c2001-11-08 00:45:21 +0000997 ** used twice: once to make the termination key and once to make the
998 ** start key.
999 */
1000 for(j=0; j<nEqColumn; j++){
danielk19774adee202004-05-08 08:23:19 +00001001 sqlite3VdbeAddOp(v, OP_Dup, nEqColumn-1, 0);
drh487ab3c2001-11-08 00:45:21 +00001002 }
1003
drhc045ec52002-12-04 20:01:06 +00001004 /* Labels for the beginning and end of the loop
1005 */
danielk19774adee202004-05-08 08:23:19 +00001006 cont = pLevel->cont = sqlite3VdbeMakeLabel(v);
1007 brk = pLevel->brk = sqlite3VdbeMakeLabel(v);
drhc045ec52002-12-04 20:01:06 +00001008
drh487ab3c2001-11-08 00:45:21 +00001009 /* Generate the termination key. This is the key value that
1010 ** will end the search. There is no termination key if there
drhc27a1ce2002-06-14 20:58:45 +00001011 ** are no equality terms and no "X<..." term.
drhc045ec52002-12-04 20:01:06 +00001012 **
1013 ** 2002-Dec-04: On a reverse-order scan, the so-called "termination"
1014 ** key computed here really ends up being the start key.
drh487ab3c2001-11-08 00:45:21 +00001015 */
1016 if( (score & 1)!=0 ){
drh193bd772004-07-20 18:23:14 +00001017 for(pTerm=aExpr, k=0; k<nExpr; k++, pTerm++){
drh94a11212004-09-25 13:12:14 +00001018 Expr *pX = pTerm->p;
1019 if( pX==0 ) continue;
drh193bd772004-07-20 18:23:14 +00001020 if( pTerm->idxLeft==iCur
drh94a11212004-09-25 13:12:14 +00001021 && (pX->op==TK_LT || pX->op==TK_LE)
drh193bd772004-07-20 18:23:14 +00001022 && (pTerm->prereqRight & loopMask)==pTerm->prereqRight
drh94a11212004-09-25 13:12:14 +00001023 && pX->pLeft->iColumn==pIdx->aiColumn[j]
drh487ab3c2001-11-08 00:45:21 +00001024 ){
drh94a11212004-09-25 13:12:14 +00001025 sqlite3ExprCode(pParse, pX->pRight);
1026 leFlag = pX->op==TK_LE;
drh193bd772004-07-20 18:23:14 +00001027 disableTerm(pLevel, &pTerm->p);
drh487ab3c2001-11-08 00:45:21 +00001028 break;
1029 }
1030 }
1031 testOp = OP_IdxGE;
1032 }else{
1033 testOp = nEqColumn>0 ? OP_IdxGE : OP_Noop;
1034 leFlag = 1;
1035 }
1036 if( testOp!=OP_Noop ){
drh143f3c42004-01-07 20:37:52 +00001037 int nCol = nEqColumn + (score & 1);
drh487ab3c2001-11-08 00:45:21 +00001038 pLevel->iMem = pParse->nMem++;
drh94a11212004-09-25 13:12:14 +00001039 buildIndexProbe(v, nCol, brk, pIdx);
drhc045ec52002-12-04 20:01:06 +00001040 if( pLevel->bRev ){
drh7cf6e4d2004-05-19 14:56:55 +00001041 int op = leFlag ? OP_MoveLe : OP_MoveLt;
1042 sqlite3VdbeAddOp(v, op, pLevel->iCur, brk);
drhc045ec52002-12-04 20:01:06 +00001043 }else{
danielk19774adee202004-05-08 08:23:19 +00001044 sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
drhc045ec52002-12-04 20:01:06 +00001045 }
1046 }else if( pLevel->bRev ){
danielk19774adee202004-05-08 08:23:19 +00001047 sqlite3VdbeAddOp(v, OP_Last, pLevel->iCur, brk);
drh487ab3c2001-11-08 00:45:21 +00001048 }
1049
1050 /* Generate the start key. This is the key that defines the lower
drhc27a1ce2002-06-14 20:58:45 +00001051 ** bound on the search. There is no start key if there are no
1052 ** equality terms and if there is no "X>..." term. In
drh487ab3c2001-11-08 00:45:21 +00001053 ** that case, generate a "Rewind" instruction in place of the
1054 ** start key search.
drhc045ec52002-12-04 20:01:06 +00001055 **
1056 ** 2002-Dec-04: In the case of a reverse-order search, the so-called
1057 ** "start" key really ends up being used as the termination key.
drh487ab3c2001-11-08 00:45:21 +00001058 */
1059 if( (score & 2)!=0 ){
drh193bd772004-07-20 18:23:14 +00001060 for(pTerm=aExpr, k=0; k<nExpr; k++, pTerm++){
drh94a11212004-09-25 13:12:14 +00001061 Expr *pX = pTerm->p;
1062 if( pX==0 ) continue;
drh193bd772004-07-20 18:23:14 +00001063 if( pTerm->idxLeft==iCur
drh94a11212004-09-25 13:12:14 +00001064 && (pX->op==TK_GT || pX->op==TK_GE)
drh193bd772004-07-20 18:23:14 +00001065 && (pTerm->prereqRight & loopMask)==pTerm->prereqRight
drh94a11212004-09-25 13:12:14 +00001066 && pX->pLeft->iColumn==pIdx->aiColumn[j]
drh487ab3c2001-11-08 00:45:21 +00001067 ){
drh94a11212004-09-25 13:12:14 +00001068 sqlite3ExprCode(pParse, pX->pRight);
1069 geFlag = pX->op==TK_GE;
drh193bd772004-07-20 18:23:14 +00001070 disableTerm(pLevel, &pTerm->p);
drh487ab3c2001-11-08 00:45:21 +00001071 break;
1072 }
1073 }
drh7900ead2001-11-12 13:51:43 +00001074 }else{
1075 geFlag = 1;
drh487ab3c2001-11-08 00:45:21 +00001076 }
drh487ab3c2001-11-08 00:45:21 +00001077 if( nEqColumn>0 || (score&2)!=0 ){
drh143f3c42004-01-07 20:37:52 +00001078 int nCol = nEqColumn + ((score&2)!=0);
drh94a11212004-09-25 13:12:14 +00001079 buildIndexProbe(v, nCol, brk, pIdx);
drhc045ec52002-12-04 20:01:06 +00001080 if( pLevel->bRev ){
1081 pLevel->iMem = pParse->nMem++;
danielk19774adee202004-05-08 08:23:19 +00001082 sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
drhc045ec52002-12-04 20:01:06 +00001083 testOp = OP_IdxLT;
1084 }else{
drh7cf6e4d2004-05-19 14:56:55 +00001085 int op = geFlag ? OP_MoveGe : OP_MoveGt;
1086 sqlite3VdbeAddOp(v, op, pLevel->iCur, brk);
drhc045ec52002-12-04 20:01:06 +00001087 }
1088 }else if( pLevel->bRev ){
1089 testOp = OP_Noop;
drh487ab3c2001-11-08 00:45:21 +00001090 }else{
danielk19774adee202004-05-08 08:23:19 +00001091 sqlite3VdbeAddOp(v, OP_Rewind, pLevel->iCur, brk);
drh487ab3c2001-11-08 00:45:21 +00001092 }
1093
1094 /* Generate the the top of the loop. If there is a termination
1095 ** key we have to test for that key and abort at the top of the
1096 ** loop.
1097 */
danielk19774adee202004-05-08 08:23:19 +00001098 start = sqlite3VdbeCurrentAddr(v);
drh487ab3c2001-11-08 00:45:21 +00001099 if( testOp!=OP_Noop ){
danielk19774adee202004-05-08 08:23:19 +00001100 sqlite3VdbeAddOp(v, OP_MemLoad, pLevel->iMem, 0);
1101 sqlite3VdbeAddOp(v, testOp, pLevel->iCur, brk);
danielk19773d1bfea2004-05-14 11:00:53 +00001102 if( (leFlag && !pLevel->bRev) || (!geFlag && pLevel->bRev) ){
1103 sqlite3VdbeChangeP3(v, -1, "+", P3_STATIC);
1104 }
drh487ab3c2001-11-08 00:45:21 +00001105 }
danielk19774adee202004-05-08 08:23:19 +00001106 sqlite3VdbeAddOp(v, OP_RowKey, pLevel->iCur, 0);
1107 sqlite3VdbeAddOp(v, OP_IdxIsNull, nEqColumn + (score & 1), cont);
1108 sqlite3VdbeAddOp(v, OP_IdxRecno, pLevel->iCur, 0);
drhad3cab52002-05-24 02:04:32 +00001109 if( i==pTabList->nSrc-1 && pushKey ){
drh487ab3c2001-11-08 00:45:21 +00001110 haveKey = 1;
1111 }else{
drh7cf6e4d2004-05-19 14:56:55 +00001112 sqlite3VdbeAddOp(v, OP_MoveGe, iCur, 0);
drh487ab3c2001-11-08 00:45:21 +00001113 haveKey = 0;
1114 }
1115
1116 /* Record the instruction used to terminate the loop.
1117 */
drhc045ec52002-12-04 20:01:06 +00001118 pLevel->op = pLevel->bRev ? OP_Prev : OP_Next;
drh487ab3c2001-11-08 00:45:21 +00001119 pLevel->p1 = pLevel->iCur;
1120 pLevel->p2 = start;
drh75897232000-05-29 14:26:00 +00001121 }
drh6a3ea0e2003-05-02 14:32:12 +00001122 loopMask |= getMask(&maskSet, iCur);
drh75897232000-05-29 14:26:00 +00001123
1124 /* Insert code to test every subexpression that can be completely
1125 ** computed using the current set of tables.
1126 */
drh193bd772004-07-20 18:23:14 +00001127 for(pTerm=aExpr, j=0; j<nExpr; j++, pTerm++){
1128 if( pTerm->p==0 ) continue;
1129 if( (pTerm->prereqAll & loopMask)!=pTerm->prereqAll ) continue;
1130 if( pLevel->iLeftJoin && !ExprHasProperty(pTerm->p,EP_FromJoin) ){
drh1f162302002-10-27 19:35:33 +00001131 continue;
1132 }
drh75897232000-05-29 14:26:00 +00001133 if( haveKey ){
drh573bd272001-02-19 23:23:38 +00001134 haveKey = 0;
drh7cf6e4d2004-05-19 14:56:55 +00001135 sqlite3VdbeAddOp(v, OP_MoveGe, iCur, 0);
drh75897232000-05-29 14:26:00 +00001136 }
drh193bd772004-07-20 18:23:14 +00001137 sqlite3ExprIfFalse(pParse, pTerm->p, cont, 1);
1138 pTerm->p = 0;
drh75897232000-05-29 14:26:00 +00001139 }
1140 brk = cont;
drhad2d8302002-05-24 20:31:36 +00001141
1142 /* For a LEFT OUTER JOIN, generate code that will record the fact that
1143 ** at least one row of the right table has matched the left table.
1144 */
1145 if( pLevel->iLeftJoin ){
danielk19774adee202004-05-08 08:23:19 +00001146 pLevel->top = sqlite3VdbeCurrentAddr(v);
1147 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1148 sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iLeftJoin, 1);
drhad6d9462004-09-19 02:15:24 +00001149 VdbeComment((v, "# record LEFT JOIN hit"));
drh193bd772004-07-20 18:23:14 +00001150 for(pTerm=aExpr, j=0; j<nExpr; j++, pTerm++){
1151 if( pTerm->p==0 ) continue;
1152 if( (pTerm->prereqAll & loopMask)!=pTerm->prereqAll ) continue;
drh1cc093c2002-06-24 22:01:57 +00001153 if( haveKey ){
drh3b167c72002-06-28 12:18:47 +00001154 /* Cannot happen. "haveKey" can only be true if pushKey is true
1155 ** an pushKey can only be true for DELETE and UPDATE and there are
1156 ** no outer joins with DELETE and UPDATE.
1157 */
drh1cc093c2002-06-24 22:01:57 +00001158 haveKey = 0;
drh7cf6e4d2004-05-19 14:56:55 +00001159 sqlite3VdbeAddOp(v, OP_MoveGe, iCur, 0);
drh1cc093c2002-06-24 22:01:57 +00001160 }
drh193bd772004-07-20 18:23:14 +00001161 sqlite3ExprIfFalse(pParse, pTerm->p, cont, 1);
1162 pTerm->p = 0;
drh1cc093c2002-06-24 22:01:57 +00001163 }
drhad2d8302002-05-24 20:31:36 +00001164 }
drh75897232000-05-29 14:26:00 +00001165 }
1166 pWInfo->iContinue = cont;
1167 if( pushKey && !haveKey ){
danielk19774adee202004-05-08 08:23:19 +00001168 sqlite3VdbeAddOp(v, OP_Recno, pTabList->a[0].iCursor, 0);
drh75897232000-05-29 14:26:00 +00001169 }
drh6a3ea0e2003-05-02 14:32:12 +00001170 freeMaskSet(&maskSet);
drh75897232000-05-29 14:26:00 +00001171 return pWInfo;
1172}
1173
1174/*
drhc27a1ce2002-06-14 20:58:45 +00001175** Generate the end of the WHERE loop. See comments on
danielk19774adee202004-05-08 08:23:19 +00001176** sqlite3WhereBegin() for additional information.
drh75897232000-05-29 14:26:00 +00001177*/
danielk19774adee202004-05-08 08:23:19 +00001178void sqlite3WhereEnd(WhereInfo *pWInfo){
drh75897232000-05-29 14:26:00 +00001179 Vdbe *v = pWInfo->pParse->pVdbe;
drh19a775c2000-06-05 18:54:46 +00001180 int i;
drh6b563442001-11-07 16:48:26 +00001181 WhereLevel *pLevel;
drhad3cab52002-05-24 02:04:32 +00001182 SrcList *pTabList = pWInfo->pTabList;
drh19a775c2000-06-05 18:54:46 +00001183
drhad3cab52002-05-24 02:04:32 +00001184 for(i=pTabList->nSrc-1; i>=0; i--){
drh6b563442001-11-07 16:48:26 +00001185 pLevel = &pWInfo->a[i];
danielk19774adee202004-05-08 08:23:19 +00001186 sqlite3VdbeResolveLabel(v, pLevel->cont);
drh6b563442001-11-07 16:48:26 +00001187 if( pLevel->op!=OP_Noop ){
danielk19774adee202004-05-08 08:23:19 +00001188 sqlite3VdbeAddOp(v, pLevel->op, pLevel->p1, pLevel->p2);
drh19a775c2000-06-05 18:54:46 +00001189 }
danielk19774adee202004-05-08 08:23:19 +00001190 sqlite3VdbeResolveLabel(v, pLevel->brk);
drhd99f7062002-06-08 23:25:08 +00001191 if( pLevel->inOp!=OP_Noop ){
danielk19774adee202004-05-08 08:23:19 +00001192 sqlite3VdbeAddOp(v, pLevel->inOp, pLevel->inP1, pLevel->inP2);
drhd99f7062002-06-08 23:25:08 +00001193 }
drhad2d8302002-05-24 20:31:36 +00001194 if( pLevel->iLeftJoin ){
1195 int addr;
danielk19774adee202004-05-08 08:23:19 +00001196 addr = sqlite3VdbeAddOp(v, OP_MemLoad, pLevel->iLeftJoin, 0);
1197 sqlite3VdbeAddOp(v, OP_NotNull, 1, addr+4 + (pLevel->iCur>=0));
1198 sqlite3VdbeAddOp(v, OP_NullRow, pTabList->a[i].iCursor, 0);
drh7f09b3e2002-08-13 13:15:49 +00001199 if( pLevel->iCur>=0 ){
danielk19774adee202004-05-08 08:23:19 +00001200 sqlite3VdbeAddOp(v, OP_NullRow, pLevel->iCur, 0);
drh7f09b3e2002-08-13 13:15:49 +00001201 }
danielk19774adee202004-05-08 08:23:19 +00001202 sqlite3VdbeAddOp(v, OP_Goto, 0, pLevel->top);
drhad2d8302002-05-24 20:31:36 +00001203 }
drh19a775c2000-06-05 18:54:46 +00001204 }
danielk19774adee202004-05-08 08:23:19 +00001205 sqlite3VdbeResolveLabel(v, pWInfo->iBreak);
drhad3cab52002-05-24 02:04:32 +00001206 for(i=0; i<pTabList->nSrc; i++){
drh5cf590c2003-04-24 01:45:04 +00001207 Table *pTab = pTabList->a[i].pTab;
1208 assert( pTab!=0 );
1209 if( pTab->isTransient || pTab->pSelect ) continue;
drh6b563442001-11-07 16:48:26 +00001210 pLevel = &pWInfo->a[i];
danielk19774adee202004-05-08 08:23:19 +00001211 sqlite3VdbeAddOp(v, OP_Close, pTabList->a[i].iCursor, 0);
drh6b563442001-11-07 16:48:26 +00001212 if( pLevel->pIdx!=0 ){
danielk19774adee202004-05-08 08:23:19 +00001213 sqlite3VdbeAddOp(v, OP_Close, pLevel->iCur, 0);
drh6b563442001-11-07 16:48:26 +00001214 }
drh19a775c2000-06-05 18:54:46 +00001215 }
drh75897232000-05-29 14:26:00 +00001216 sqliteFree(pWInfo);
1217 return;
1218}