blob: ea15a5eeb3680149c16ec528ab897d9539619211 [file] [log] [blame]
drh75897232000-05-29 14:26:00 +00001/*
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** drh@hwaci.com
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.1 2000/05/29 14:26:02 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*/
37typedef struct ExprInfo ExprInfo;
38struct 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 field in this table number. -1 if
42 ** p->pLeft is not the field of any table */
43 int idxRight; /* p->pRight is a field in this table number. -1 if
44 ** p->pRight is not the field 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*/
63static 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*/
90static int exprTableUsage(Expr *p){
91 unsigned int mask = 0;
92 if( p==0 ) return 0;
93 if( p->op==TK_FIELD ){
94 return 1<<p->iTable;
95 }
96 if( p->pRight ){
97 mask = exprTableUsage(p->pRight);
98 }
99 if( p->pLeft ){
100 mask |= exprTableUsage(p->pLeft);
101 }
102 return mask;
103}
104
105/*
106** The input to this routine is an ExprInfo structure with only the
107** "p" field filled in. The job of this routine is to analyze the
108** subexpression and populate all the other fields of the ExprInfo
109** structure.
110*/
111static void exprAnalyze(ExprInfo *pInfo){
112 Expr *pExpr = pInfo->p;
113 pInfo->prereqLeft = exprTableUsage(pExpr->pLeft);
114 pInfo->prereqRight = exprTableUsage(pExpr->pRight);
115 pInfo->indexable = 0;
116 pInfo->idxLeft = -1;
117 pInfo->idxRight = -1;
118 if( pExpr->op==TK_EQ && (pInfo->prereqRight & pInfo->prereqLeft)==0 ){
119 if( pExpr->pRight->op==TK_FIELD ){
120 pInfo->idxRight = pExpr->pRight->iTable;
121 pInfo->indexable = 1;
122 }
123 if( pExpr->pLeft->op==TK_FIELD ){
124 pInfo->idxLeft = pExpr->pLeft->iTable;
125 pInfo->indexable = 1;
126 }
127 }
128}
129
130/*
131** Generating the beginning of the loop used for WHERE clause processing.
132** The return value is a pointer to an (opaque) structure that contains
133** information needed to terminate the loop. Later, the calling routine
134** should invoke sqliteWhereEnd() with the return value of this function
135** in order to complete the WHERE clause processing.
136**
137** If an error occurs, this routine returns NULL.
138*/
139WhereInfo *sqliteWhereBegin(
140 Parse *pParse, /* The parser context */
141 IdList *pTabList, /* A list of all tables */
142 Expr *pWhere, /* The WHERE clause */
143 int pushKey /* If TRUE, leave the table key on the stack */
144){
145 int i; /* Loop counter */
146 WhereInfo *pWInfo; /* Will become the return value of this function */
147 Vdbe *v = pParse->pVdbe; /* The virtual database engine */
148 int brk, cont; /* Addresses used during code generation */
149 int *aOrder; /* Order in which pTabList entries are searched */
150 int nExpr; /* Number of subexpressions in the WHERE clause */
151 int loopMask; /* One bit set for each outer loop */
152 int haveKey; /* True if KEY is on the stack */
153 Index *aIdx[32]; /* Index to use on each nested loop. */
154 ExprInfo aExpr[50]; /* The WHERE clause is divided into these expressions */
155
156 /* Allocate space for aOrder[]. */
157 aOrder = sqliteMalloc( sizeof(int) * pTabList->nId );
158
159 /* Allocate and initialize the WhereInfo structure that will become the
160 ** return value.
161 */
162 pWInfo = sqliteMalloc( sizeof(WhereInfo) );
163 if( pWInfo==0 ){
164 sqliteFree(aOrder);
165 return 0;
166 }
167 pWInfo->pParse = pParse;
168 pWInfo->pTabList = pTabList;
169
170 /* Split the WHERE clause into as many as 32 separate subexpressions
171 ** where each subexpression is separated by an AND operator. Any additional
172 ** subexpressions are attached in the aExpr[32] and will not enter
173 ** into the query optimizer computations. 32 is chosen as the cutoff
174 ** since that is the number of bits in an integer that we use for an
175 ** expression-used mask.
176 */
177 memset(aExpr, 0, sizeof(aExpr));
178 nExpr = exprSplit(ARRAYSIZE(aExpr), aExpr, pWhere);
179
180 /* Analyze all of the subexpressions.
181 */
182 for(i=0; i<nExpr; i++){
183 exprAnalyze(&aExpr[i]);
184 }
185
186 /* Figure out a good nesting order for the tables. aOrder[0] will
187 ** be the index in pTabList of the outermost table. aOrder[1] will
188 ** be the first nested loop and so on. aOrder[pTabList->nId-1] will
189 ** be the innermost loop.
190 **
191 ** Someday will put in a good algorithm here to reorder to the loops
192 ** for an effiecient query. But for now, just use whatever order the
193 ** tables appear in in the pTabList.
194 */
195 for(i=0; i<pTabList->nId; i++){
196 aOrder[i] = i;
197 }
198
199 /* Figure out what index to use (if any) for each nested loop.
200 ** Make aIdx[i] point to the index to use for the i-th nested loop
201 ** where i==0 is the outer loop and i==pTabList->nId-1 is the inner
202 ** loop.
203 **
204 ** Actually, if there are more than 32 tables in the join, only the
205 ** first 32 tables are candidates for indices.
206 */
207 loopMask = 0;
208 for(i=0; i<pTabList->nId && i<ARRAYSIZE(aIdx); i++){
209 int idx = aOrder[i];
210 Table *pTab = pTabList->a[idx].pTab;
211 Index *pIdx;
212 Index *pBestIdx = 0;
213
214 /* Do a search for usable indices. Leave pBestIdx pointing to
215 ** most specific usable index.
216 **
217 ** "Most specific" means that pBestIdx is the usable index that
218 ** has the largest value for nField. A usable index is one for
219 ** which there are subexpressions to compute every field of the
220 ** index.
221 */
222 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
223 int j;
224 int fieldMask = 0;
225
226 if( pIdx->nField>32 ) continue;
227 for(j=0; j<nExpr; j++){
228 if( aExpr[j].idxLeft==idx
229 && (aExpr[j].prereqRight & loopMask)==aExpr[j].prereqRight ){
230 int iField = aExpr[j].p->pLeft->iField;
231 int k;
232 for(k=0; k<pIdx->nField; k++){
233 if( pIdx->aiField[k]==iField ){
234 fieldMask |= 1<<k;
235 break;
236 }
237 }
238 }
239 if( aExpr[j].idxRight==idx
240 && (aExpr[j].prereqLeft & loopMask)==aExpr[j].prereqLeft ){
241 int iField = aExpr[j].p->pRight->iField;
242 int k;
243 for(k=0; k<pIdx->nField; k++){
244 if( pIdx->aiField[k]==iField ){
245 fieldMask |= 1<<k;
246 break;
247 }
248 }
249 }
250 }
251 if( fieldMask + 1 == (1<<pIdx->nField) ){
252 if( pBestIdx==0 || pBestIdx->nField<pIdx->nField ){
253 pBestIdx = pIdx;
254 }
255 }
256 }
257 aIdx[i] = pBestIdx;
258 }
259
260 /* Open all tables in the pTabList and all indices in aIdx[].
261 */
262 for(i=0; i<pTabList->nId; i++){
263 sqliteVdbeAddOp(v, OP_Open, i, 0, pTabList->a[i].pTab->zName, 0);
264 if( i<ARRAYSIZE(aIdx) && aIdx[i]!=0 ){
265 sqliteVdbeAddOp(v, OP_Open, pTabList->nId+i, 0, aIdx[i]->zName, 0);
266 }
267 }
268
269 /* Generate the code to do the search
270 */
271 pWInfo->iBreak = brk = sqliteVdbeMakeLabel(v);
272 loopMask = 0;
273 for(i=0; i<pTabList->nId; i++){
274 int j, k;
275 int idx = aOrder[i];
276 Index *pIdx = i<ARRAYSIZE(aIdx) ? aIdx[i] : 0;
277
278 cont = sqliteVdbeMakeLabel(v);
279 if( pIdx==0 ){
280 /* Case 1: There was no usable index. We must do a complete
281 ** scan of the table.
282 */
283 sqliteVdbeAddOp(v, OP_Next, idx, brk, 0, cont);
284 haveKey = 0;
285 }else{
286 /* Case 2: We do have a usable index in pIdx.
287 */
288 for(j=0; j<pIdx->nField; j++){
289 for(k=0; k<nExpr; k++){
290 if( aExpr[k].p==0 ) continue;
291 if( aExpr[k].idxLeft==idx
292 && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight
293 && aExpr[k].p->pLeft->iField==pIdx->aiField[j]
294 ){
295 sqliteExprCode(pParse, aExpr[k].p->pRight);
296 aExpr[k].p = 0;
297 break;
298 }
299 if( aExpr[k].idxRight==idx
300 && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
301 && aExpr[k].p->pRight->iField==pIdx->aiField[j]
302 ){
303 sqliteExprCode(pParse, aExpr[k].p->pLeft);
304 aExpr[k].p = 0;
305 break;
306 }
307 }
308 }
309 sqliteVdbeAddOp(v, OP_MakeKey, pIdx->nField, 0, 0, 0);
310 sqliteVdbeAddOp(v, OP_Fetch, pTabList->nId+i, 0, 0, 0);
311 sqliteVdbeAddOp(v, OP_NextIdx, pTabList->nId+i, brk, 0, cont);
312 if( i==pTabList->nId-1 && pushKey ){
313 haveKey = 1;
314 }else{
315 sqliteVdbeAddOp(v, OP_Fetch, idx, 0, 0, 0);
316 haveKey = 0;
317 }
318 }
319 loopMask |= 1<<idx;
320
321 /* Insert code to test every subexpression that can be completely
322 ** computed using the current set of tables.
323 */
324 for(j=0; j<nExpr; j++){
325 if( aExpr[j].p==0 ) continue;
326 if( (aExpr[j].prereqRight & loopMask)!=aExpr[j].prereqRight ) continue;
327 if( (aExpr[j].prereqLeft & loopMask)!=aExpr[j].prereqLeft ) continue;
328 if( haveKey ){
329 sqliteVdbeAddOp(v, OP_Fetch, idx, 0, 0, 0);
330 haveKey = 0;
331 }
332 sqliteExprIfFalse(pParse, aExpr[j].p, cont);
333 aExpr[j].p = 0;
334 }
335 brk = cont;
336 }
337 pWInfo->iContinue = cont;
338 if( pushKey && !haveKey ){
339 sqliteVdbeAddOp(v, OP_Key, 0, 0, 0, 0);
340 }
341 sqliteFree(aOrder);
342 return pWInfo;
343}
344
345/*
346** Generate the end of the WHERE loop.
347*/
348void sqliteWhereEnd(WhereInfo *pWInfo){
349 Vdbe *v = pWInfo->pParse->pVdbe;
350 sqliteVdbeAddOp(v, OP_Goto, 0, pWInfo->iContinue, 0, 0);
351 sqliteVdbeAddOp(v, OP_Noop, 0, 0, 0, pWInfo->iBreak);
352 sqliteFree(pWInfo);
353 return;
354}
355
356/*
357** Generate code into the current Vdbe to evaluate the given
358** expression and leave the result on the stack.
359*/
360void sqliteExprCode(Parse *pParse, Expr *pExpr){
361 Vdbe *v = pParse->pVdbe;
362 int op;
363 switch( pExpr->op ){
364 case TK_PLUS: op = OP_Add; break;
365 case TK_MINUS: op = OP_Subtract; break;
366 case TK_STAR: op = OP_Multiply; break;
367 case TK_SLASH: op = OP_Divide; break;
368 case TK_AND: op = OP_And; break;
369 case TK_OR: op = OP_Or; break;
370 case TK_LT: op = OP_Lt; break;
371 case TK_LE: op = OP_Le; break;
372 case TK_GT: op = OP_Gt; break;
373 case TK_GE: op = OP_Ge; break;
374 case TK_NE: op = OP_Ne; break;
375 case TK_EQ: op = OP_Eq; break;
376 case TK_ISNULL: op = OP_IsNull; break;
377 case TK_NOTNULL: op = OP_NotNull; break;
378 case TK_NOT: op = OP_Not; break;
379 case TK_UMINUS: op = OP_Negative; break;
380 default: break;
381 }
382 switch( pExpr->op ){
383 case TK_FIELD: {
384 sqliteVdbeAddOp(v, OP_Field, pExpr->iTable, pExpr->iField, 0, 0);
385 break;
386 }
387 case TK_INTEGER: {
388 int i = atoi(pExpr->token.z);
389 sqliteVdbeAddOp(v, OP_Integer, i, 0, 0, 0);
390 break;
391 }
392 case TK_FLOAT: {
393 int addr = sqliteVdbeAddOp(v, OP_String, 0, 0, 0, 0);
394 sqliteVdbeChangeP3(v, addr, pExpr->token.z, pExpr->token.n);
395 break;
396 }
397 case TK_STRING: {
398 int addr = sqliteVdbeAddOp(v, OP_String, 0, 0, 0, 0);
399 sqliteVdbeChangeP3(v, addr, pExpr->token.z, pExpr->token.n);
400 sqliteVdbeDequoteP3(v, addr);
401 break;
402 }
403 case TK_NULL: {
404 sqliteVdbeAddOp(v, OP_String, 0, 0, "", 0);
405 break;
406 }
407 case TK_AND:
408 case TK_OR:
409 case TK_PLUS:
410 case TK_STAR:
411 case TK_MINUS:
412 case TK_SLASH: {
413 sqliteExprCode(pParse, pExpr->pLeft);
414 sqliteExprCode(pParse, pExpr->pRight);
415 sqliteVdbeAddOp(v, op, 0, 0, 0, 0);
416 break;
417 }
418 case TK_LT:
419 case TK_LE:
420 case TK_GT:
421 case TK_GE:
422 case TK_NE:
423 case TK_EQ: {
424 int dest;
425 sqliteVdbeAddOp(v, OP_Integer, 0, 0, 0, 0);
426 sqliteExprCode(pParse, pExpr->pLeft);
427 sqliteExprCode(pParse, pExpr->pRight);
428 dest = sqliteVdbeCurrentAddr(v) + 2;
429 sqliteVdbeAddOp(v, op, 0, dest, 0, 0);
430 sqliteVdbeAddOp(v, OP_AddImm, 1, 0, 0, 0);
431 break;
432 }
433 case TK_NOT:
434 case TK_UMINUS: {
435 sqliteExprCode(pParse, pExpr->pLeft);
436 sqliteVdbeAddOp(v, op, 0, 0, 0, 0);
437 break;
438 }
439 case TK_ISNULL:
440 case TK_NOTNULL: {
441 int dest;
442 sqliteVdbeAddOp(v, OP_Integer, 0, 0, 0, 0);
443 sqliteExprCode(pParse, pExpr->pLeft);
444 dest = sqliteVdbeCurrentAddr(v) + 2;
445 sqliteVdbeAddOp(v, op, 0, dest, 0, 0);
446 sqliteVdbeAddOp(v, OP_AddImm, 1, 0, 0, 0);
447 break;
448 }
449 }
450 return;
451}
452
453/*
454** Generate code for a boolean expression such that a jump is made
455** to the label "dest" if the expression is true but execution
456** continues straight thru if the expression is false.
457*/
458void sqliteExprIfTrue(Parse *pParse, Expr *pExpr, int dest){
459 Vdbe *v = pParse->pVdbe;
460 int op = 0;
461 switch( pExpr->op ){
462 case TK_LT: op = OP_Lt; break;
463 case TK_LE: op = OP_Le; break;
464 case TK_GT: op = OP_Gt; break;
465 case TK_GE: op = OP_Ge; break;
466 case TK_NE: op = OP_Ne; break;
467 case TK_EQ: op = OP_Eq; break;
468 case TK_ISNULL: op = OP_IsNull; break;
469 case TK_NOTNULL: op = OP_NotNull; break;
470 default: break;
471 }
472 switch( pExpr->op ){
473 case TK_AND: {
474 int d2 = sqliteVdbeMakeLabel(v);
475 sqliteExprIfFalse(pParse, pExpr->pLeft, d2);
476 sqliteExprIfTrue(pParse, pExpr->pRight, dest);
477 sqliteVdbeResolveLabel(v, d2);
478 break;
479 }
480 case TK_OR: {
481 sqliteExprIfTrue(pParse, pExpr->pLeft, dest);
482 sqliteExprIfTrue(pParse, pExpr->pRight, dest);
483 break;
484 }
485 case TK_LT:
486 case TK_LE:
487 case TK_GT:
488 case TK_GE:
489 case TK_NE:
490 case TK_EQ: {
491 sqliteExprCode(pParse, pExpr->pLeft);
492 sqliteExprCode(pParse, pExpr->pRight);
493 sqliteVdbeAddOp(v, op, 0, dest, 0, 0);
494 break;
495 }
496 case TK_ISNULL:
497 case TK_NOTNULL: {
498 sqliteExprCode(pParse, pExpr->pLeft);
499 sqliteVdbeAddOp(v, op, 0, dest, 0, 0);
500 break;
501 }
502 default: {
503 sqliteExprCode(pParse, pExpr);
504 sqliteVdbeAddOp(v, OP_If, 0, dest, 0, 0);
505 break;
506 }
507 }
508}
509
510/*
511** Generate code for boolean expression such that a jump is made
512** to the label "dest" if the expression is false but execution
513** continues straight thru if the expression is true.
514*/
515void sqliteExprIfFalse(Parse *pParse, Expr *pExpr, int dest){
516 Vdbe *v = pParse->pVdbe;
517 int op = 0;
518 switch( pExpr->op ){
519 case TK_LT: op = OP_Ge; break;
520 case TK_LE: op = OP_Gt; break;
521 case TK_GT: op = OP_Le; break;
522 case TK_GE: op = OP_Lt; break;
523 case TK_NE: op = OP_Eq; break;
524 case TK_EQ: op = OP_Ne; break;
525 case TK_ISNULL: op = OP_NotNull; break;
526 case TK_NOTNULL: op = OP_IsNull; break;
527 default: break;
528 }
529 switch( pExpr->op ){
530 case TK_AND: {
531 sqliteExprIfFalse(pParse, pExpr->pLeft, dest);
532 sqliteExprIfFalse(pParse, pExpr->pRight, dest);
533 break;
534 }
535 case TK_OR: {
536 int d2 = sqliteVdbeMakeLabel(v);
537 sqliteExprIfTrue(pParse, pExpr->pLeft, d2);
538 sqliteExprIfFalse(pParse, pExpr->pRight, dest);
539 sqliteVdbeResolveLabel(v, d2);
540 break;
541 }
542 case TK_LT:
543 case TK_LE:
544 case TK_GT:
545 case TK_GE:
546 case TK_NE:
547 case TK_EQ: {
548 sqliteExprCode(pParse, pExpr->pLeft);
549 sqliteExprCode(pParse, pExpr->pRight);
550 sqliteVdbeAddOp(v, op, 0, dest, 0, 0);
551 break;
552 }
553 case TK_ISNULL:
554 case TK_NOTNULL: {
555 sqliteExprCode(pParse, pExpr->pLeft);
556 sqliteVdbeAddOp(v, op, 0, dest, 0, 0);
557 break;
558 }
559 default: {
560 sqliteExprCode(pParse, pExpr);
561 sqliteVdbeAddOp(v, OP_Not, 0, 0, 0, 0);
562 sqliteVdbeAddOp(v, OP_If, 0, dest, 0, 0);
563 break;
564 }
565 }
566}