xref: /sqlite-3.40.0/src/where.c (revision 7c68d60b)
1 /*
2 ** Copyright (c) 1999, 2000 D. Richard Hipp
3 **
4 ** This program is free software; you can redistribute it and/or
5 ** modify it under the terms of the GNU General Public
6 ** License as published by the Free Software Foundation; either
7 ** version 2 of the License, or (at your option) any later version.
8 **
9 ** This program is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 ** General Public License for more details.
13 **
14 ** You should have received a copy of the GNU General Public
15 ** License along with this library; if not, write to the
16 ** Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 ** Boston, MA  02111-1307, USA.
18 **
19 ** Author contact information:
20 **   [email protected]
21 **   http://www.hwaci.com/drh/
22 **
23 *************************************************************************
24 ** This module contains C code that generates VDBE code used to process
25 ** the WHERE clause of SQL statements.  Also found here are subroutines
26 ** to generate VDBE code to evaluate expressions.
27 **
28 ** $Id: where.c,v 1.9 2000/08/22 18:29:34 drh Exp $
29 */
30 #include "sqliteInt.h"
31 
32 /*
33 ** The query generator uses an array of instances of this structure to
34 ** help it analyze the subexpressions of the WHERE clause.  Each WHERE
35 ** clause subexpression is separated from the others by an AND operator.
36 */
37 typedef struct ExprInfo ExprInfo;
38 struct ExprInfo {
39   Expr *p;                /* Pointer to the subexpression */
40   int indexable;          /* True if this subexprssion is usable by an index */
41   int idxLeft;            /* p->pLeft is a column in this table number. -1 if
42                           ** p->pLeft is not the column of any table */
43   int idxRight;           /* p->pRight is a column in this table number. -1 if
44                           ** p->pRight is not the column of any table */
45   unsigned prereqLeft;    /* Tables referenced by p->pLeft */
46   unsigned prereqRight;   /* Tables referenced by p->pRight */
47 };
48 
49 /*
50 ** Determine the number of elements in an array.
51 */
52 #define ARRAYSIZE(X)  (sizeof(X)/sizeof(X[0]))
53 
54 /*
55 ** This routine is used to divide the WHERE expression into subexpressions
56 ** separated by the AND operator.
57 **
58 ** aSlot[] is an array of subexpressions structures.
59 ** There are nSlot spaces left in this array.  This routine attempts to
60 ** split pExpr into subexpressions and fills aSlot[] with those subexpressions.
61 ** The return value is the number of slots filled.
62 */
63 static int exprSplit(int nSlot, ExprInfo *aSlot, Expr *pExpr){
64   int cnt = 0;
65   if( pExpr==0 || nSlot<1 ) return 0;
66   if( nSlot==1 || pExpr->op!=TK_AND ){
67     aSlot[0].p = pExpr;
68     return 1;
69   }
70   if( pExpr->pLeft->op!=TK_AND ){
71     aSlot[0].p = pExpr->pLeft;
72     cnt = 1 + exprSplit(nSlot-1, &aSlot[1], pExpr->pRight);
73   }else{
74     cnt = exprSplit(nSlot, aSlot, pExpr->pRight);
75     cnt += exprSplit(nSlot-cnt, &aSlot[cnt], pExpr->pLeft);
76   }
77   return cnt;
78 }
79 
80 /*
81 ** This routine walks (recursively) an expression tree and generates
82 ** a bitmask indicating which tables are used in that expression
83 ** tree.  Bit 0 of the mask is set if table 0 is used.  But 1 is set
84 ** if table 1 is used.  And so forth.
85 **
86 ** In order for this routine to work, the calling function must have
87 ** previously invoked sqliteExprResolveIds() on the expression.  See
88 ** the header comment on that routine for additional information.
89 **
90 ** "base" is the cursor number (the value of the iTable field) that
91 ** corresponds to the first entry in the table list.  This is the
92 ** same as pParse->nTab.
93 */
94 static int exprTableUsage(int base, Expr *p){
95   unsigned int mask = 0;
96   if( p==0 ) return 0;
97   if( p->op==TK_COLUMN ){
98     return 1<< (p->iTable - base);
99   }
100   if( p->pRight ){
101     mask = exprTableUsage(base, p->pRight);
102   }
103   if( p->pLeft ){
104     mask |= exprTableUsage(base, p->pLeft);
105   }
106   return mask;
107 }
108 
109 /*
110 ** The input to this routine is an ExprInfo structure with only the
111 ** "p" field filled in.  The job of this routine is to analyze the
112 ** subexpression and populate all the other fields of the ExprInfo
113 ** structure.
114 **
115 ** "base" is the cursor number (the value of the iTable field) that
116 ** corresponds to the first entyr in the table list.  This is the
117 ** same as pParse->nTab.
118 */
119 static void exprAnalyze(int base, ExprInfo *pInfo){
120   Expr *pExpr = pInfo->p;
121   pInfo->prereqLeft = exprTableUsage(base, pExpr->pLeft);
122   pInfo->prereqRight = exprTableUsage(base, pExpr->pRight);
123   pInfo->indexable = 0;
124   pInfo->idxLeft = -1;
125   pInfo->idxRight = -1;
126   if( pExpr->op==TK_EQ && (pInfo->prereqRight & pInfo->prereqLeft)==0 ){
127     if( pExpr->pRight->op==TK_COLUMN ){
128       pInfo->idxRight = pExpr->pRight->iTable - base;
129       pInfo->indexable = 1;
130     }
131     if( pExpr->pLeft->op==TK_COLUMN ){
132       pInfo->idxLeft = pExpr->pLeft->iTable - base;
133       pInfo->indexable = 1;
134     }
135   }
136 }
137 
138 /*
139 ** Generating the beginning of the loop used for WHERE clause processing.
140 ** The return value is a pointer to an (opaque) structure that contains
141 ** information needed to terminate the loop.  Later, the calling routine
142 ** should invoke sqliteWhereEnd() with the return value of this function
143 ** in order to complete the WHERE clause processing.
144 **
145 ** If an error occurs, this routine returns NULL.
146 */
147 WhereInfo *sqliteWhereBegin(
148   Parse *pParse,       /* The parser context */
149   IdList *pTabList,    /* A list of all tables */
150   Expr *pWhere,        /* The WHERE clause */
151   int pushKey          /* If TRUE, leave the table key on the stack */
152 ){
153   int i;                     /* Loop counter */
154   WhereInfo *pWInfo;         /* Will become the return value of this function */
155   Vdbe *v = pParse->pVdbe;   /* The virtual database engine */
156   int brk, cont;             /* Addresses used during code generation */
157   int *aOrder;         /* Order in which pTabList entries are searched */
158   int nExpr;           /* Number of subexpressions in the WHERE clause */
159   int loopMask;        /* One bit set for each outer loop */
160   int haveKey;         /* True if KEY is on the stack */
161   int base;            /* First available index for OP_Open opcodes */
162   Index *aIdx[32];     /* Index to use on each nested loop.  */
163   ExprInfo aExpr[50];  /* The WHERE clause is divided into these expressions */
164 
165   /* Allocate space for aOrder[]. */
166   aOrder = sqliteMalloc( sizeof(int) * pTabList->nId );
167 
168   /* Allocate and initialize the WhereInfo structure that will become the
169   ** return value.
170   */
171   pWInfo = sqliteMalloc( sizeof(WhereInfo) );
172   if( pWInfo==0 ){
173     sqliteFree(aOrder);
174     return 0;
175   }
176   pWInfo->pParse = pParse;
177   pWInfo->pTabList = pTabList;
178   base = pWInfo->base = pParse->nTab;
179 
180   /* Split the WHERE clause into as many as 32 separate subexpressions
181   ** where each subexpression is separated by an AND operator.  Any additional
182   ** subexpressions are attached in the aExpr[32] and will not enter
183   ** into the query optimizer computations.  32 is chosen as the cutoff
184   ** since that is the number of bits in an integer that we use for an
185   ** expression-used mask.
186   */
187   memset(aExpr, 0, sizeof(aExpr));
188   nExpr = exprSplit(ARRAYSIZE(aExpr), aExpr, pWhere);
189 
190   /* Analyze all of the subexpressions.
191   */
192   for(i=0; i<nExpr; i++){
193     exprAnalyze(pParse->nTab, &aExpr[i]);
194   }
195 
196   /* Figure out a good nesting order for the tables.  aOrder[0] will
197   ** be the index in pTabList of the outermost table.  aOrder[1] will
198   ** be the first nested loop and so on.  aOrder[pTabList->nId-1] will
199   ** be the innermost loop.
200   **
201   ** Someday will put in a good algorithm here to reorder the loops
202   ** for an effiecient query.  But for now, just use whatever order the
203   ** tables appear in in the pTabList.
204   */
205   for(i=0; i<pTabList->nId; i++){
206     aOrder[i] = i;
207   }
208 
209   /* Figure out what index to use (if any) for each nested loop.
210   ** Make aIdx[i] point to the index to use for the i-th nested loop
211   ** where i==0 is the outer loop and i==pTabList->nId-1 is the inner
212   ** loop.
213   **
214   ** Actually, if there are more than 32 tables in the join, only the
215   ** first 32 tables are candidates for indices.
216   */
217   loopMask = 0;
218   for(i=0; i<pTabList->nId && i<ARRAYSIZE(aIdx); i++){
219     int idx = aOrder[i];
220     Table *pTab = pTabList->a[idx].pTab;
221     Index *pIdx;
222     Index *pBestIdx = 0;
223 
224     /* Do a search for usable indices.  Leave pBestIdx pointing to
225     ** the most specific usable index.
226     **
227     ** "Most specific" means that pBestIdx is the usable index that
228     ** has the largest value for nColumn.  A usable index is one for
229     ** which there are subexpressions to compute every column of the
230     ** index.
231     */
232     for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
233       int j;
234       int columnMask = 0;
235 
236       if( pIdx->nColumn>32 ) continue;
237       for(j=0; j<nExpr; j++){
238         if( aExpr[j].idxLeft==idx
239              && (aExpr[j].prereqRight & loopMask)==aExpr[j].prereqRight ){
240           int iColumn = aExpr[j].p->pLeft->iColumn;
241           int k;
242           for(k=0; k<pIdx->nColumn; k++){
243             if( pIdx->aiColumn[k]==iColumn ){
244               columnMask |= 1<<k;
245               break;
246             }
247           }
248         }
249         if( aExpr[j].idxRight==idx
250              && (aExpr[j].prereqLeft & loopMask)==aExpr[j].prereqLeft ){
251           int iColumn = aExpr[j].p->pRight->iColumn;
252           int k;
253           for(k=0; k<pIdx->nColumn; k++){
254             if( pIdx->aiColumn[k]==iColumn ){
255               columnMask |= 1<<k;
256               break;
257             }
258           }
259         }
260       }
261       if( columnMask + 1 == (1<<pIdx->nColumn) ){
262         if( pBestIdx==0 || pBestIdx->nColumn<pIdx->nColumn ){
263           pBestIdx = pIdx;
264         }
265       }
266     }
267     aIdx[i] = pBestIdx;
268     loopMask |= 1<<idx;
269   }
270 
271   /* Open all tables in the pTabList and all indices in aIdx[].
272   */
273   for(i=0; i<pTabList->nId; i++){
274     sqliteVdbeAddOp(v, OP_Open, base+i, 0, pTabList->a[i].pTab->zName, 0);
275     if( i<ARRAYSIZE(aIdx) && aIdx[i]!=0 ){
276       sqliteVdbeAddOp(v, OP_Open, base+pTabList->nId+i, 0, aIdx[i]->zName, 0);
277     }
278   }
279   memcpy(pWInfo->aIdx, aIdx, sizeof(aIdx));
280 
281   /* Generate the code to do the search
282   */
283   pWInfo->iBreak = brk = sqliteVdbeMakeLabel(v);
284   loopMask = 0;
285   for(i=0; i<pTabList->nId; i++){
286     int j, k;
287     int idx = aOrder[i];
288     Index *pIdx = i<ARRAYSIZE(aIdx) ? aIdx[i] : 0;
289 
290     cont = sqliteVdbeMakeLabel(v);
291     if( pIdx==0 ){
292       /* Case 1:  There was no usable index.  We must do a complete
293       ** scan of the table.
294       */
295       sqliteVdbeAddOp(v, OP_Next, base+idx, brk, 0, cont);
296       haveKey = 0;
297     }else{
298       /* Case 2:  We do have a usable index in pIdx.
299       */
300       for(j=0; j<pIdx->nColumn; j++){
301         for(k=0; k<nExpr; k++){
302           if( aExpr[k].p==0 ) continue;
303           if( aExpr[k].idxLeft==idx
304              && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
305              && aExpr[k].p->pLeft->iColumn==pIdx->aiColumn[j]
306           ){
307             sqliteExprCode(pParse, aExpr[k].p->pRight);
308             aExpr[k].p = 0;
309             break;
310           }
311           if( aExpr[k].idxRight==idx
312              && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
313              && aExpr[k].p->pRight->iColumn==pIdx->aiColumn[j]
314           ){
315             sqliteExprCode(pParse, aExpr[k].p->pLeft);
316             aExpr[k].p = 0;
317             break;
318           }
319         }
320       }
321       sqliteVdbeAddOp(v, OP_MakeKey, pIdx->nColumn, 0, 0, 0);
322       sqliteVdbeAddOp(v, OP_Fetch, base+pTabList->nId+i, 0, 0, 0);
323       sqliteVdbeAddOp(v, OP_NextIdx, base+pTabList->nId+i, brk, 0, cont);
324       if( i==pTabList->nId-1 && pushKey ){
325         haveKey = 1;
326       }else{
327         sqliteVdbeAddOp(v, OP_Fetch, base+idx, 0, 0, 0);
328         haveKey = 0;
329       }
330     }
331     loopMask |= 1<<idx;
332 
333     /* Insert code to test every subexpression that can be completely
334     ** computed using the current set of tables.
335     */
336     for(j=0; j<nExpr; j++){
337       if( aExpr[j].p==0 ) continue;
338       if( (aExpr[j].prereqRight & loopMask)!=aExpr[j].prereqRight ) continue;
339       if( (aExpr[j].prereqLeft & loopMask)!=aExpr[j].prereqLeft ) continue;
340       if( haveKey ){
341         sqliteVdbeAddOp(v, OP_Fetch, base+idx, 0, 0, 0);
342         haveKey = 0;
343       }
344       sqliteExprIfFalse(pParse, aExpr[j].p, cont);
345       aExpr[j].p = 0;
346     }
347     brk = cont;
348   }
349   pWInfo->iContinue = cont;
350   if( pushKey && !haveKey ){
351     sqliteVdbeAddOp(v, OP_Key, base, 0, 0, 0);
352   }
353   sqliteFree(aOrder);
354   return pWInfo;
355 }
356 
357 /*
358 ** Generate the end of the WHERE loop.
359 */
360 void sqliteWhereEnd(WhereInfo *pWInfo){
361   Vdbe *v = pWInfo->pParse->pVdbe;
362   int i;
363   int brk = pWInfo->iBreak;
364   int base = pWInfo->base;
365 
366   sqliteVdbeAddOp(v, OP_Goto, 0, pWInfo->iContinue, 0, 0);
367   for(i=0; i<pWInfo->pTabList->nId; i++){
368     sqliteVdbeAddOp(v, OP_Close, base+i, 0, 0, brk);
369     brk = 0;
370     if( i<ARRAYSIZE(pWInfo->aIdx) && pWInfo->aIdx[i]!=0 ){
371       sqliteVdbeAddOp(v, OP_Close, base+pWInfo->pTabList->nId+i, 0, 0, 0);
372     }
373   }
374   if( brk!=0 ){
375     sqliteVdbeAddOp(v, OP_Noop, 0, 0, 0, brk);
376   }
377   sqliteFree(pWInfo);
378   return;
379 }
380