blob: 99a97079be378d654311847305e2b4cd93460208 [file] [log] [blame]
drh6c1f4ef2015-06-08 14:23:15 +00001/*
2** 2015-06-08
3**
4** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
6**
7** 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.
10**
11*************************************************************************
12** This module contains C code that generates VDBE code used to process
13** the WHERE clause of SQL statements.
14**
15** This file was originally part of where.c but was split out to improve
16** readability and editabiliity. This file contains utility routines for
17** analyzing Expr objects in the WHERE clause.
18*/
19#include "sqliteInt.h"
20#include "whereInt.h"
21
22/* Forward declarations */
23static void exprAnalyze(SrcList*, WhereClause*, int);
24
25/*
26** Deallocate all memory associated with a WhereOrInfo object.
27*/
28static void whereOrInfoDelete(sqlite3 *db, WhereOrInfo *p){
29 sqlite3WhereClauseClear(&p->wc);
30 sqlite3DbFree(db, p);
31}
32
33/*
34** Deallocate all memory associated with a WhereAndInfo object.
35*/
36static void whereAndInfoDelete(sqlite3 *db, WhereAndInfo *p){
37 sqlite3WhereClauseClear(&p->wc);
38 sqlite3DbFree(db, p);
39}
40
41/*
42** Add a single new WhereTerm entry to the WhereClause object pWC.
43** The new WhereTerm object is constructed from Expr p and with wtFlags.
44** The index in pWC->a[] of the new WhereTerm is returned on success.
45** 0 is returned if the new WhereTerm could not be added due to a memory
46** allocation error. The memory allocation failure will be recorded in
47** the db->mallocFailed flag so that higher-level functions can detect it.
48**
49** This routine will increase the size of the pWC->a[] array as necessary.
50**
51** If the wtFlags argument includes TERM_DYNAMIC, then responsibility
52** for freeing the expression p is assumed by the WhereClause object pWC.
53** This is true even if this routine fails to allocate a new WhereTerm.
54**
55** WARNING: This routine might reallocate the space used to store
56** WhereTerms. All pointers to WhereTerms should be invalidated after
57** calling this routine. Such pointers may be reinitialized by referencing
58** the pWC->a[] array.
59*/
60static int whereClauseInsert(WhereClause *pWC, Expr *p, u16 wtFlags){
61 WhereTerm *pTerm;
62 int idx;
63 testcase( wtFlags & TERM_VIRTUAL );
64 if( pWC->nTerm>=pWC->nSlot ){
65 WhereTerm *pOld = pWC->a;
66 sqlite3 *db = pWC->pWInfo->pParse->db;
67 pWC->a = sqlite3DbMallocRaw(db, sizeof(pWC->a[0])*pWC->nSlot*2 );
68 if( pWC->a==0 ){
69 if( wtFlags & TERM_DYNAMIC ){
70 sqlite3ExprDelete(db, p);
71 }
72 pWC->a = pOld;
73 return 0;
74 }
75 memcpy(pWC->a, pOld, sizeof(pWC->a[0])*pWC->nTerm);
76 if( pOld!=pWC->aStatic ){
77 sqlite3DbFree(db, pOld);
78 }
79 pWC->nSlot = sqlite3DbMallocSize(db, pWC->a)/sizeof(pWC->a[0]);
80 memset(&pWC->a[pWC->nTerm], 0, sizeof(pWC->a[0])*(pWC->nSlot-pWC->nTerm));
81 }
82 pTerm = &pWC->a[idx = pWC->nTerm++];
83 if( p && ExprHasProperty(p, EP_Unlikely) ){
84 pTerm->truthProb = sqlite3LogEst(p->iTable) - 270;
85 }else{
86 pTerm->truthProb = 1;
87 }
88 pTerm->pExpr = sqlite3ExprSkipCollate(p);
89 pTerm->wtFlags = wtFlags;
90 pTerm->pWC = pWC;
91 pTerm->iParent = -1;
92 return idx;
93}
94
95/*
96** Return TRUE if the given operator is one of the operators that is
97** allowed for an indexable WHERE clause term. The allowed operators are
98** "=", "<", ">", "<=", ">=", "IN", and "IS NULL"
99*/
100static int allowedOp(int op){
101 assert( TK_GT>TK_EQ && TK_GT<TK_GE );
102 assert( TK_LT>TK_EQ && TK_LT<TK_GE );
103 assert( TK_LE>TK_EQ && TK_LE<TK_GE );
104 assert( TK_GE==TK_EQ+4 );
105 return op==TK_IN || (op>=TK_EQ && op<=TK_GE) || op==TK_ISNULL || op==TK_IS;
106}
107
108/*
109** Commute a comparison operator. Expressions of the form "X op Y"
110** are converted into "Y op X".
111**
112** If left/right precedence rules come into play when determining the
113** collating sequence, then COLLATE operators are adjusted to ensure
114** that the collating sequence does not change. For example:
115** "Y collate NOCASE op X" becomes "X op Y" because any collation sequence on
116** the left hand side of a comparison overrides any collation sequence
117** attached to the right. For the same reason the EP_Collate flag
118** is not commuted.
119*/
120static void exprCommute(Parse *pParse, Expr *pExpr){
121 u16 expRight = (pExpr->pRight->flags & EP_Collate);
122 u16 expLeft = (pExpr->pLeft->flags & EP_Collate);
123 assert( allowedOp(pExpr->op) && pExpr->op!=TK_IN );
124 if( expRight==expLeft ){
125 /* Either X and Y both have COLLATE operator or neither do */
126 if( expRight ){
127 /* Both X and Y have COLLATE operators. Make sure X is always
128 ** used by clearing the EP_Collate flag from Y. */
129 pExpr->pRight->flags &= ~EP_Collate;
130 }else if( sqlite3ExprCollSeq(pParse, pExpr->pLeft)!=0 ){
131 /* Neither X nor Y have COLLATE operators, but X has a non-default
132 ** collating sequence. So add the EP_Collate marker on X to cause
133 ** it to be searched first. */
134 pExpr->pLeft->flags |= EP_Collate;
135 }
136 }
137 SWAP(Expr*,pExpr->pRight,pExpr->pLeft);
138 if( pExpr->op>=TK_GT ){
139 assert( TK_LT==TK_GT+2 );
140 assert( TK_GE==TK_LE+2 );
141 assert( TK_GT>TK_EQ );
142 assert( TK_GT<TK_LE );
143 assert( pExpr->op>=TK_GT && pExpr->op<=TK_GE );
144 pExpr->op = ((pExpr->op-TK_GT)^2)+TK_GT;
145 }
146}
147
148/*
149** Translate from TK_xx operator to WO_xx bitmask.
150*/
151static u16 operatorMask(int op){
152 u16 c;
153 assert( allowedOp(op) );
154 if( op==TK_IN ){
155 c = WO_IN;
156 }else if( op==TK_ISNULL ){
157 c = WO_ISNULL;
158 }else if( op==TK_IS ){
159 c = WO_IS;
160 }else{
161 assert( (WO_EQ<<(op-TK_EQ)) < 0x7fff );
162 c = (u16)(WO_EQ<<(op-TK_EQ));
163 }
164 assert( op!=TK_ISNULL || c==WO_ISNULL );
165 assert( op!=TK_IN || c==WO_IN );
166 assert( op!=TK_EQ || c==WO_EQ );
167 assert( op!=TK_LT || c==WO_LT );
168 assert( op!=TK_LE || c==WO_LE );
169 assert( op!=TK_GT || c==WO_GT );
170 assert( op!=TK_GE || c==WO_GE );
171 assert( op!=TK_IS || c==WO_IS );
172 return c;
173}
174
175
176#ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
177/*
178** Check to see if the given expression is a LIKE or GLOB operator that
179** can be optimized using inequality constraints. Return TRUE if it is
180** so and false if not.
181**
182** In order for the operator to be optimizible, the RHS must be a string
183** literal that does not begin with a wildcard. The LHS must be a column
184** that may only be NULL, a string, or a BLOB, never a number. (This means
185** that virtual tables cannot participate in the LIKE optimization.) The
186** collating sequence for the column on the LHS must be appropriate for
187** the operator.
188*/
189static int isLikeOrGlob(
190 Parse *pParse, /* Parsing and code generating context */
191 Expr *pExpr, /* Test this expression */
192 Expr **ppPrefix, /* Pointer to TK_STRING expression with pattern prefix */
193 int *pisComplete, /* True if the only wildcard is % in the last character */
194 int *pnoCase /* True if uppercase is equivalent to lowercase */
195){
196 const char *z = 0; /* String on RHS of LIKE operator */
197 Expr *pRight, *pLeft; /* Right and left size of LIKE operator */
198 ExprList *pList; /* List of operands to the LIKE operator */
199 int c; /* One character in z[] */
200 int cnt; /* Number of non-wildcard prefix characters */
201 char wc[3]; /* Wildcard characters */
202 sqlite3 *db = pParse->db; /* Database connection */
203 sqlite3_value *pVal = 0;
204 int op; /* Opcode of pRight */
205
206 if( !sqlite3IsLikeFunction(db, pExpr, pnoCase, wc) ){
207 return 0;
208 }
209#ifdef SQLITE_EBCDIC
210 if( *pnoCase ) return 0;
211#endif
212 pList = pExpr->x.pList;
213 pLeft = pList->a[1].pExpr;
214 if( pLeft->op!=TK_COLUMN
215 || sqlite3ExprAffinity(pLeft)!=SQLITE_AFF_TEXT
216 || IsVirtual(pLeft->pTab) /* Value might be numeric */
217 ){
218 /* IMP: R-02065-49465 The left-hand side of the LIKE or GLOB operator must
219 ** be the name of an indexed column with TEXT affinity. */
220 return 0;
221 }
222 assert( pLeft->iColumn!=(-1) ); /* Because IPK never has AFF_TEXT */
223
224 pRight = sqlite3ExprSkipCollate(pList->a[0].pExpr);
225 op = pRight->op;
226 if( op==TK_VARIABLE ){
227 Vdbe *pReprepare = pParse->pReprepare;
228 int iCol = pRight->iColumn;
229 pVal = sqlite3VdbeGetBoundValue(pReprepare, iCol, SQLITE_AFF_BLOB);
230 if( pVal && sqlite3_value_type(pVal)==SQLITE_TEXT ){
231 z = (char *)sqlite3_value_text(pVal);
232 }
233 sqlite3VdbeSetVarmask(pParse->pVdbe, iCol);
234 assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER );
235 }else if( op==TK_STRING ){
236 z = pRight->u.zToken;
237 }
238 if( z ){
239 cnt = 0;
240 while( (c=z[cnt])!=0 && c!=wc[0] && c!=wc[1] && c!=wc[2] ){
241 cnt++;
242 }
243 if( cnt!=0 && 255!=(u8)z[cnt-1] ){
244 Expr *pPrefix;
245 *pisComplete = c==wc[0] && z[cnt+1]==0;
246 pPrefix = sqlite3Expr(db, TK_STRING, z);
247 if( pPrefix ) pPrefix->u.zToken[cnt] = 0;
248 *ppPrefix = pPrefix;
249 if( op==TK_VARIABLE ){
250 Vdbe *v = pParse->pVdbe;
251 sqlite3VdbeSetVarmask(v, pRight->iColumn);
252 if( *pisComplete && pRight->u.zToken[1] ){
253 /* If the rhs of the LIKE expression is a variable, and the current
254 ** value of the variable means there is no need to invoke the LIKE
255 ** function, then no OP_Variable will be added to the program.
256 ** This causes problems for the sqlite3_bind_parameter_name()
257 ** API. To work around them, add a dummy OP_Variable here.
258 */
259 int r1 = sqlite3GetTempReg(pParse);
260 sqlite3ExprCodeTarget(pParse, pRight, r1);
261 sqlite3VdbeChangeP3(v, sqlite3VdbeCurrentAddr(v)-1, 0);
262 sqlite3ReleaseTempReg(pParse, r1);
263 }
264 }
265 }else{
266 z = 0;
267 }
268 }
269
270 sqlite3ValueFree(pVal);
271 return (z!=0);
272}
273#endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
274
275
276#ifndef SQLITE_OMIT_VIRTUALTABLE
277/*
278** Check to see if the given expression is of the form
279**
dan43970dd2015-11-24 17:39:01 +0000280** column OP expr
281**
282** where OP is one of MATCH, GLOB, LIKE or REGEXP and "column" is a
283** column of a virtual table.
drh6c1f4ef2015-06-08 14:23:15 +0000284**
285** If it is then return TRUE. If not, return FALSE.
286*/
287static int isMatchOfColumn(
dan07bdba82015-11-23 21:09:54 +0000288 Expr *pExpr, /* Test this expression */
289 unsigned char *peOp2 /* OUT: 0 for MATCH, or else an op2 value */
drh6c1f4ef2015-06-08 14:23:15 +0000290){
dan07bdba82015-11-23 21:09:54 +0000291 struct Op2 {
292 const char *zOp;
293 unsigned char eOp2;
294 } aOp[] = {
dan43970dd2015-11-24 17:39:01 +0000295 { "match", SQLITE_INDEX_CONSTRAINT_MATCH },
296 { "glob", SQLITE_INDEX_CONSTRAINT_GLOB },
297 { "like", SQLITE_INDEX_CONSTRAINT_LIKE },
298 { "regexp", SQLITE_INDEX_CONSTRAINT_REGEXP }
dan07bdba82015-11-23 21:09:54 +0000299 };
drh6c1f4ef2015-06-08 14:23:15 +0000300 ExprList *pList;
dan43970dd2015-11-24 17:39:01 +0000301 Expr *pCol; /* Column reference */
dan07bdba82015-11-23 21:09:54 +0000302 int i;
drh6c1f4ef2015-06-08 14:23:15 +0000303
304 if( pExpr->op!=TK_FUNCTION ){
305 return 0;
306 }
drh6c1f4ef2015-06-08 14:23:15 +0000307 pList = pExpr->x.pList;
danff7b22b2015-11-24 18:16:15 +0000308 if( pList==0 || pList->nExpr!=2 ){
drh6c1f4ef2015-06-08 14:23:15 +0000309 return 0;
310 }
dan43970dd2015-11-24 17:39:01 +0000311 pCol = pList->a[1].pExpr;
312 if( pCol->op!=TK_COLUMN || !IsVirtual(pCol->pTab) ){
drh6c1f4ef2015-06-08 14:23:15 +0000313 return 0;
314 }
dan07bdba82015-11-23 21:09:54 +0000315 for(i=0; i<ArraySize(aOp); i++){
316 if( sqlite3StrICmp(pExpr->u.zToken, aOp[i].zOp)==0 ){
317 *peOp2 = aOp[i].eOp2;
318 return 1;
319 }
320 }
321 return 0;
drh6c1f4ef2015-06-08 14:23:15 +0000322}
323#endif /* SQLITE_OMIT_VIRTUALTABLE */
324
325/*
326** If the pBase expression originated in the ON or USING clause of
327** a join, then transfer the appropriate markings over to derived.
328*/
329static void transferJoinMarkings(Expr *pDerived, Expr *pBase){
330 if( pDerived ){
331 pDerived->flags |= pBase->flags & EP_FromJoin;
332 pDerived->iRightJoinTable = pBase->iRightJoinTable;
333 }
334}
335
336/*
337** Mark term iChild as being a child of term iParent
338*/
339static void markTermAsChild(WhereClause *pWC, int iChild, int iParent){
340 pWC->a[iChild].iParent = iParent;
341 pWC->a[iChild].truthProb = pWC->a[iParent].truthProb;
342 pWC->a[iParent].nChild++;
343}
344
345/*
346** Return the N-th AND-connected subterm of pTerm. Or if pTerm is not
347** a conjunction, then return just pTerm when N==0. If N is exceeds
348** the number of available subterms, return NULL.
349*/
350static WhereTerm *whereNthSubterm(WhereTerm *pTerm, int N){
351 if( pTerm->eOperator!=WO_AND ){
352 return N==0 ? pTerm : 0;
353 }
354 if( N<pTerm->u.pAndInfo->wc.nTerm ){
355 return &pTerm->u.pAndInfo->wc.a[N];
356 }
357 return 0;
358}
359
360/*
361** Subterms pOne and pTwo are contained within WHERE clause pWC. The
362** two subterms are in disjunction - they are OR-ed together.
363**
364** If these two terms are both of the form: "A op B" with the same
365** A and B values but different operators and if the operators are
366** compatible (if one is = and the other is <, for example) then
367** add a new virtual AND term to pWC that is the combination of the
368** two.
369**
370** Some examples:
371**
372** x<y OR x=y --> x<=y
373** x=y OR x=y --> x=y
374** x<=y OR x<y --> x<=y
375**
376** The following is NOT generated:
377**
378** x<y OR x>y --> x!=y
379*/
380static void whereCombineDisjuncts(
381 SrcList *pSrc, /* the FROM clause */
382 WhereClause *pWC, /* The complete WHERE clause */
383 WhereTerm *pOne, /* First disjunct */
384 WhereTerm *pTwo /* Second disjunct */
385){
386 u16 eOp = pOne->eOperator | pTwo->eOperator;
387 sqlite3 *db; /* Database connection (for malloc) */
388 Expr *pNew; /* New virtual expression */
389 int op; /* Operator for the combined expression */
390 int idxNew; /* Index in pWC of the next virtual term */
391
392 if( (pOne->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return;
393 if( (pTwo->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return;
394 if( (eOp & (WO_EQ|WO_LT|WO_LE))!=eOp
395 && (eOp & (WO_EQ|WO_GT|WO_GE))!=eOp ) return;
396 assert( pOne->pExpr->pLeft!=0 && pOne->pExpr->pRight!=0 );
397 assert( pTwo->pExpr->pLeft!=0 && pTwo->pExpr->pRight!=0 );
398 if( sqlite3ExprCompare(pOne->pExpr->pLeft, pTwo->pExpr->pLeft, -1) ) return;
399 if( sqlite3ExprCompare(pOne->pExpr->pRight, pTwo->pExpr->pRight, -1) )return;
400 /* If we reach this point, it means the two subterms can be combined */
401 if( (eOp & (eOp-1))!=0 ){
402 if( eOp & (WO_LT|WO_LE) ){
403 eOp = WO_LE;
404 }else{
405 assert( eOp & (WO_GT|WO_GE) );
406 eOp = WO_GE;
407 }
408 }
409 db = pWC->pWInfo->pParse->db;
410 pNew = sqlite3ExprDup(db, pOne->pExpr, 0);
411 if( pNew==0 ) return;
412 for(op=TK_EQ; eOp!=(WO_EQ<<(op-TK_EQ)); op++){ assert( op<TK_GE ); }
413 pNew->op = op;
414 idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC);
415 exprAnalyze(pSrc, pWC, idxNew);
416}
417
418#if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
419/*
420** Analyze a term that consists of two or more OR-connected
421** subterms. So in:
422**
423** ... WHERE (a=5) AND (b=7 OR c=9 OR d=13) AND (d=13)
424** ^^^^^^^^^^^^^^^^^^^^
425**
426** This routine analyzes terms such as the middle term in the above example.
427** A WhereOrTerm object is computed and attached to the term under
428** analysis, regardless of the outcome of the analysis. Hence:
429**
430** WhereTerm.wtFlags |= TERM_ORINFO
431** WhereTerm.u.pOrInfo = a dynamically allocated WhereOrTerm object
432**
433** The term being analyzed must have two or more of OR-connected subterms.
434** A single subterm might be a set of AND-connected sub-subterms.
435** Examples of terms under analysis:
436**
437** (A) t1.x=t2.y OR t1.x=t2.z OR t1.y=15 OR t1.z=t3.a+5
438** (B) x=expr1 OR expr2=x OR x=expr3
439** (C) t1.x=t2.y OR (t1.x=t2.z AND t1.y=15)
440** (D) x=expr1 OR (y>11 AND y<22 AND z LIKE '*hello*')
441** (E) (p.a=1 AND q.b=2 AND r.c=3) OR (p.x=4 AND q.y=5 AND r.z=6)
442** (F) x>A OR (x=A AND y>=B)
443**
444** CASE 1:
445**
446** If all subterms are of the form T.C=expr for some single column of C and
447** a single table T (as shown in example B above) then create a new virtual
448** term that is an equivalent IN expression. In other words, if the term
449** being analyzed is:
450**
451** x = expr1 OR expr2 = x OR x = expr3
452**
453** then create a new virtual term like this:
454**
455** x IN (expr1,expr2,expr3)
456**
457** CASE 2:
458**
459** If there are exactly two disjuncts and one side has x>A and the other side
460** has x=A (for the same x and A) then add a new virtual conjunct term to the
461** WHERE clause of the form "x>=A". Example:
462**
463** x>A OR (x=A AND y>B) adds: x>=A
464**
465** The added conjunct can sometimes be helpful in query planning.
466**
467** CASE 3:
468**
469** If all subterms are indexable by a single table T, then set
470**
471** WhereTerm.eOperator = WO_OR
472** WhereTerm.u.pOrInfo->indexable |= the cursor number for table T
473**
474** A subterm is "indexable" if it is of the form
475** "T.C <op> <expr>" where C is any column of table T and
476** <op> is one of "=", "<", "<=", ">", ">=", "IS NULL", or "IN".
477** A subterm is also indexable if it is an AND of two or more
478** subsubterms at least one of which is indexable. Indexable AND
479** subterms have their eOperator set to WO_AND and they have
480** u.pAndInfo set to a dynamically allocated WhereAndTerm object.
481**
482** From another point of view, "indexable" means that the subterm could
483** potentially be used with an index if an appropriate index exists.
484** This analysis does not consider whether or not the index exists; that
485** is decided elsewhere. This analysis only looks at whether subterms
486** appropriate for indexing exist.
487**
488** All examples A through E above satisfy case 3. But if a term
489** also satisfies case 1 (such as B) we know that the optimizer will
490** always prefer case 1, so in that case we pretend that case 3 is not
491** satisfied.
492**
493** It might be the case that multiple tables are indexable. For example,
494** (E) above is indexable on tables P, Q, and R.
495**
496** Terms that satisfy case 3 are candidates for lookup by using
497** separate indices to find rowids for each subterm and composing
498** the union of all rowids using a RowSet object. This is similar
499** to "bitmap indices" in other database engines.
500**
501** OTHERWISE:
502**
503** If none of cases 1, 2, or 3 apply, then leave the eOperator set to
504** zero. This term is not useful for search.
505*/
506static void exprAnalyzeOrTerm(
507 SrcList *pSrc, /* the FROM clause */
508 WhereClause *pWC, /* the complete WHERE clause */
509 int idxTerm /* Index of the OR-term to be analyzed */
510){
511 WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */
512 Parse *pParse = pWInfo->pParse; /* Parser context */
513 sqlite3 *db = pParse->db; /* Database connection */
514 WhereTerm *pTerm = &pWC->a[idxTerm]; /* The term to be analyzed */
515 Expr *pExpr = pTerm->pExpr; /* The expression of the term */
516 int i; /* Loop counters */
517 WhereClause *pOrWc; /* Breakup of pTerm into subterms */
518 WhereTerm *pOrTerm; /* A Sub-term within the pOrWc */
519 WhereOrInfo *pOrInfo; /* Additional information associated with pTerm */
520 Bitmask chngToIN; /* Tables that might satisfy case 1 */
521 Bitmask indexable; /* Tables that are indexable, satisfying case 2 */
522
523 /*
524 ** Break the OR clause into its separate subterms. The subterms are
525 ** stored in a WhereClause structure containing within the WhereOrInfo
526 ** object that is attached to the original OR clause term.
527 */
528 assert( (pTerm->wtFlags & (TERM_DYNAMIC|TERM_ORINFO|TERM_ANDINFO))==0 );
529 assert( pExpr->op==TK_OR );
530 pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, sizeof(*pOrInfo));
531 if( pOrInfo==0 ) return;
532 pTerm->wtFlags |= TERM_ORINFO;
533 pOrWc = &pOrInfo->wc;
534 sqlite3WhereClauseInit(pOrWc, pWInfo);
535 sqlite3WhereSplit(pOrWc, pExpr, TK_OR);
536 sqlite3WhereExprAnalyze(pSrc, pOrWc);
537 if( db->mallocFailed ) return;
538 assert( pOrWc->nTerm>=2 );
539
540 /*
541 ** Compute the set of tables that might satisfy cases 1 or 3.
542 */
543 indexable = ~(Bitmask)0;
544 chngToIN = ~(Bitmask)0;
545 for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0 && indexable; i--, pOrTerm++){
546 if( (pOrTerm->eOperator & WO_SINGLE)==0 ){
547 WhereAndInfo *pAndInfo;
548 assert( (pOrTerm->wtFlags & (TERM_ANDINFO|TERM_ORINFO))==0 );
549 chngToIN = 0;
550 pAndInfo = sqlite3DbMallocRaw(db, sizeof(*pAndInfo));
551 if( pAndInfo ){
552 WhereClause *pAndWC;
553 WhereTerm *pAndTerm;
554 int j;
555 Bitmask b = 0;
556 pOrTerm->u.pAndInfo = pAndInfo;
557 pOrTerm->wtFlags |= TERM_ANDINFO;
558 pOrTerm->eOperator = WO_AND;
559 pAndWC = &pAndInfo->wc;
560 sqlite3WhereClauseInit(pAndWC, pWC->pWInfo);
561 sqlite3WhereSplit(pAndWC, pOrTerm->pExpr, TK_AND);
562 sqlite3WhereExprAnalyze(pSrc, pAndWC);
563 pAndWC->pOuter = pWC;
564 testcase( db->mallocFailed );
565 if( !db->mallocFailed ){
566 for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){
567 assert( pAndTerm->pExpr );
568 if( allowedOp(pAndTerm->pExpr->op) ){
569 b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pAndTerm->leftCursor);
570 }
571 }
572 }
573 indexable &= b;
574 }
575 }else if( pOrTerm->wtFlags & TERM_COPIED ){
576 /* Skip this term for now. We revisit it when we process the
577 ** corresponding TERM_VIRTUAL term */
578 }else{
579 Bitmask b;
580 b = sqlite3WhereGetMask(&pWInfo->sMaskSet, pOrTerm->leftCursor);
581 if( pOrTerm->wtFlags & TERM_VIRTUAL ){
582 WhereTerm *pOther = &pOrWc->a[pOrTerm->iParent];
583 b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pOther->leftCursor);
584 }
585 indexable &= b;
586 if( (pOrTerm->eOperator & WO_EQ)==0 ){
587 chngToIN = 0;
588 }else{
589 chngToIN &= b;
590 }
591 }
592 }
593
594 /*
595 ** Record the set of tables that satisfy case 3. The set might be
596 ** empty.
597 */
598 pOrInfo->indexable = indexable;
599 pTerm->eOperator = indexable==0 ? 0 : WO_OR;
600
601 /* For a two-way OR, attempt to implementation case 2.
602 */
603 if( indexable && pOrWc->nTerm==2 ){
604 int iOne = 0;
605 WhereTerm *pOne;
606 while( (pOne = whereNthSubterm(&pOrWc->a[0],iOne++))!=0 ){
607 int iTwo = 0;
608 WhereTerm *pTwo;
609 while( (pTwo = whereNthSubterm(&pOrWc->a[1],iTwo++))!=0 ){
610 whereCombineDisjuncts(pSrc, pWC, pOne, pTwo);
611 }
612 }
613 }
614
615 /*
616 ** chngToIN holds a set of tables that *might* satisfy case 1. But
617 ** we have to do some additional checking to see if case 1 really
618 ** is satisfied.
619 **
620 ** chngToIN will hold either 0, 1, or 2 bits. The 0-bit case means
621 ** that there is no possibility of transforming the OR clause into an
622 ** IN operator because one or more terms in the OR clause contain
623 ** something other than == on a column in the single table. The 1-bit
624 ** case means that every term of the OR clause is of the form
625 ** "table.column=expr" for some single table. The one bit that is set
626 ** will correspond to the common table. We still need to check to make
627 ** sure the same column is used on all terms. The 2-bit case is when
628 ** the all terms are of the form "table1.column=table2.column". It
629 ** might be possible to form an IN operator with either table1.column
630 ** or table2.column as the LHS if either is common to every term of
631 ** the OR clause.
632 **
633 ** Note that terms of the form "table.column1=table.column2" (the
634 ** same table on both sizes of the ==) cannot be optimized.
635 */
636 if( chngToIN ){
637 int okToChngToIN = 0; /* True if the conversion to IN is valid */
638 int iColumn = -1; /* Column index on lhs of IN operator */
639 int iCursor = -1; /* Table cursor common to all terms */
640 int j = 0; /* Loop counter */
641
642 /* Search for a table and column that appears on one side or the
643 ** other of the == operator in every subterm. That table and column
644 ** will be recorded in iCursor and iColumn. There might not be any
645 ** such table and column. Set okToChngToIN if an appropriate table
646 ** and column is found but leave okToChngToIN false if not found.
647 */
648 for(j=0; j<2 && !okToChngToIN; j++){
649 pOrTerm = pOrWc->a;
650 for(i=pOrWc->nTerm-1; i>=0; i--, pOrTerm++){
651 assert( pOrTerm->eOperator & WO_EQ );
652 pOrTerm->wtFlags &= ~TERM_OR_OK;
653 if( pOrTerm->leftCursor==iCursor ){
654 /* This is the 2-bit case and we are on the second iteration and
655 ** current term is from the first iteration. So skip this term. */
656 assert( j==1 );
657 continue;
658 }
659 if( (chngToIN & sqlite3WhereGetMask(&pWInfo->sMaskSet,
660 pOrTerm->leftCursor))==0 ){
661 /* This term must be of the form t1.a==t2.b where t2 is in the
662 ** chngToIN set but t1 is not. This term will be either preceded
663 ** or follwed by an inverted copy (t2.b==t1.a). Skip this term
664 ** and use its inversion. */
665 testcase( pOrTerm->wtFlags & TERM_COPIED );
666 testcase( pOrTerm->wtFlags & TERM_VIRTUAL );
667 assert( pOrTerm->wtFlags & (TERM_COPIED|TERM_VIRTUAL) );
668 continue;
669 }
670 iColumn = pOrTerm->u.leftColumn;
671 iCursor = pOrTerm->leftCursor;
672 break;
673 }
674 if( i<0 ){
675 /* No candidate table+column was found. This can only occur
676 ** on the second iteration */
677 assert( j==1 );
678 assert( IsPowerOfTwo(chngToIN) );
679 assert( chngToIN==sqlite3WhereGetMask(&pWInfo->sMaskSet, iCursor) );
680 break;
681 }
682 testcase( j==1 );
683
684 /* We have found a candidate table and column. Check to see if that
685 ** table and column is common to every term in the OR clause */
686 okToChngToIN = 1;
687 for(; i>=0 && okToChngToIN; i--, pOrTerm++){
688 assert( pOrTerm->eOperator & WO_EQ );
689 if( pOrTerm->leftCursor!=iCursor ){
690 pOrTerm->wtFlags &= ~TERM_OR_OK;
691 }else if( pOrTerm->u.leftColumn!=iColumn ){
692 okToChngToIN = 0;
693 }else{
694 int affLeft, affRight;
695 /* If the right-hand side is also a column, then the affinities
696 ** of both right and left sides must be such that no type
697 ** conversions are required on the right. (Ticket #2249)
698 */
699 affRight = sqlite3ExprAffinity(pOrTerm->pExpr->pRight);
700 affLeft = sqlite3ExprAffinity(pOrTerm->pExpr->pLeft);
701 if( affRight!=0 && affRight!=affLeft ){
702 okToChngToIN = 0;
703 }else{
704 pOrTerm->wtFlags |= TERM_OR_OK;
705 }
706 }
707 }
708 }
709
710 /* At this point, okToChngToIN is true if original pTerm satisfies
711 ** case 1. In that case, construct a new virtual term that is
712 ** pTerm converted into an IN operator.
713 */
714 if( okToChngToIN ){
715 Expr *pDup; /* A transient duplicate expression */
716 ExprList *pList = 0; /* The RHS of the IN operator */
717 Expr *pLeft = 0; /* The LHS of the IN operator */
718 Expr *pNew; /* The complete IN operator */
719
720 for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0; i--, pOrTerm++){
721 if( (pOrTerm->wtFlags & TERM_OR_OK)==0 ) continue;
722 assert( pOrTerm->eOperator & WO_EQ );
723 assert( pOrTerm->leftCursor==iCursor );
724 assert( pOrTerm->u.leftColumn==iColumn );
725 pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight, 0);
726 pList = sqlite3ExprListAppend(pWInfo->pParse, pList, pDup);
727 pLeft = pOrTerm->pExpr->pLeft;
728 }
729 assert( pLeft!=0 );
730 pDup = sqlite3ExprDup(db, pLeft, 0);
731 pNew = sqlite3PExpr(pParse, TK_IN, pDup, 0, 0);
732 if( pNew ){
733 int idxNew;
734 transferJoinMarkings(pNew, pExpr);
735 assert( !ExprHasProperty(pNew, EP_xIsSelect) );
736 pNew->x.pList = pList;
737 idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC);
738 testcase( idxNew==0 );
739 exprAnalyze(pSrc, pWC, idxNew);
740 pTerm = &pWC->a[idxTerm];
741 markTermAsChild(pWC, idxNew, idxTerm);
742 }else{
743 sqlite3ExprListDelete(db, pList);
744 }
745 pTerm->eOperator = WO_NOOP; /* case 1 trumps case 3 */
746 }
747 }
748}
749#endif /* !SQLITE_OMIT_OR_OPTIMIZATION && !SQLITE_OMIT_SUBQUERY */
750
751/*
752** We already know that pExpr is a binary operator where both operands are
753** column references. This routine checks to see if pExpr is an equivalence
754** relation:
755** 1. The SQLITE_Transitive optimization must be enabled
756** 2. Must be either an == or an IS operator
757** 3. Not originating in the ON clause of an OUTER JOIN
758** 4. The affinities of A and B must be compatible
759** 5a. Both operands use the same collating sequence OR
760** 5b. The overall collating sequence is BINARY
761** If this routine returns TRUE, that means that the RHS can be substituted
762** for the LHS anyplace else in the WHERE clause where the LHS column occurs.
763** This is an optimization. No harm comes from returning 0. But if 1 is
764** returned when it should not be, then incorrect answers might result.
765*/
766static int termIsEquivalence(Parse *pParse, Expr *pExpr){
767 char aff1, aff2;
768 CollSeq *pColl;
769 const char *zColl1, *zColl2;
770 if( !OptimizationEnabled(pParse->db, SQLITE_Transitive) ) return 0;
771 if( pExpr->op!=TK_EQ && pExpr->op!=TK_IS ) return 0;
772 if( ExprHasProperty(pExpr, EP_FromJoin) ) return 0;
773 aff1 = sqlite3ExprAffinity(pExpr->pLeft);
774 aff2 = sqlite3ExprAffinity(pExpr->pRight);
775 if( aff1!=aff2
776 && (!sqlite3IsNumericAffinity(aff1) || !sqlite3IsNumericAffinity(aff2))
777 ){
778 return 0;
779 }
780 pColl = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft, pExpr->pRight);
781 if( pColl==0 || sqlite3StrICmp(pColl->zName, "BINARY")==0 ) return 1;
782 pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
783 /* Since pLeft and pRight are both a column references, their collating
784 ** sequence should always be defined. */
785 zColl1 = ALWAYS(pColl) ? pColl->zName : 0;
786 pColl = sqlite3ExprCollSeq(pParse, pExpr->pRight);
787 zColl2 = ALWAYS(pColl) ? pColl->zName : 0;
788 return sqlite3StrICmp(zColl1, zColl2)==0;
789}
790
791/*
792** Recursively walk the expressions of a SELECT statement and generate
793** a bitmask indicating which tables are used in that expression
794** tree.
795*/
796static Bitmask exprSelectUsage(WhereMaskSet *pMaskSet, Select *pS){
797 Bitmask mask = 0;
798 while( pS ){
799 SrcList *pSrc = pS->pSrc;
800 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pEList);
801 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pGroupBy);
802 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pOrderBy);
803 mask |= sqlite3WhereExprUsage(pMaskSet, pS->pWhere);
804 mask |= sqlite3WhereExprUsage(pMaskSet, pS->pHaving);
805 if( ALWAYS(pSrc!=0) ){
806 int i;
807 for(i=0; i<pSrc->nSrc; i++){
808 mask |= exprSelectUsage(pMaskSet, pSrc->a[i].pSelect);
809 mask |= sqlite3WhereExprUsage(pMaskSet, pSrc->a[i].pOn);
810 }
811 }
812 pS = pS->pPrior;
813 }
814 return mask;
815}
816
817/*
drh47991422015-08-31 15:58:06 +0000818** Expression pExpr is one operand of a comparison operator that might
819** be useful for indexing. This routine checks to see if pExpr appears
820** in any index. Return TRUE (1) if pExpr is an indexed term and return
821** FALSE (0) if not. If TRUE is returned, also set *piCur to the cursor
822** number of the table that is indexed and *piColumn to the column number
823** of the column that is indexed, or -2 if an expression is being indexed.
824**
825** If pExpr is a TK_COLUMN column reference, then this routine always returns
826** true even if that particular column is not indexed, because the column
827** might be added to an automatic index later.
828*/
829static int exprMightBeIndexed(
830 SrcList *pFrom, /* The FROM clause */
831 Bitmask mPrereq, /* Bitmask of FROM clause terms referenced by pExpr */
832 Expr *pExpr, /* An operand of a comparison operator */
833 int *piCur, /* Write the referenced table cursor number here */
834 int *piColumn /* Write the referenced table column number here */
835){
836 Index *pIdx;
837 int i;
838 int iCur;
839 if( pExpr->op==TK_COLUMN ){
840 *piCur = pExpr->iTable;
841 *piColumn = pExpr->iColumn;
842 return 1;
843 }
844 if( mPrereq==0 ) return 0; /* No table references */
845 if( (mPrereq&(mPrereq-1))!=0 ) return 0; /* Refs more than one table */
846 for(i=0; mPrereq>1; i++, mPrereq>>=1){}
847 iCur = pFrom->a[i].iCursor;
848 for(pIdx=pFrom->a[i].pTab->pIndex; pIdx; pIdx=pIdx->pNext){
849 if( pIdx->aColExpr==0 ) continue;
850 for(i=0; i<pIdx->nKeyCol; i++){
851 if( pIdx->aiColumn[i]!=(-2) ) continue;
852 if( sqlite3ExprCompare(pExpr, pIdx->aColExpr->a[i].pExpr, iCur)==0 ){
853 *piCur = iCur;
854 *piColumn = -2;
855 return 1;
856 }
857 }
858 }
859 return 0;
860}
861
862/*
drh6c1f4ef2015-06-08 14:23:15 +0000863** The input to this routine is an WhereTerm structure with only the
864** "pExpr" field filled in. The job of this routine is to analyze the
865** subexpression and populate all the other fields of the WhereTerm
866** structure.
867**
868** If the expression is of the form "<expr> <op> X" it gets commuted
869** to the standard form of "X <op> <expr>".
870**
871** If the expression is of the form "X <op> Y" where both X and Y are
872** columns, then the original expression is unchanged and a new virtual
873** term of the form "Y <op> X" is added to the WHERE clause and
874** analyzed separately. The original term is marked with TERM_COPIED
875** and the new term is marked with TERM_DYNAMIC (because it's pExpr
876** needs to be freed with the WhereClause) and TERM_VIRTUAL (because it
877** is a commuted copy of a prior term.) The original term has nChild=1
878** and the copy has idxParent set to the index of the original term.
879*/
880static void exprAnalyze(
881 SrcList *pSrc, /* the FROM clause */
882 WhereClause *pWC, /* the WHERE clause */
883 int idxTerm /* Index of the term to be analyzed */
884){
885 WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */
886 WhereTerm *pTerm; /* The term to be analyzed */
887 WhereMaskSet *pMaskSet; /* Set of table index masks */
888 Expr *pExpr; /* The expression to be analyzed */
889 Bitmask prereqLeft; /* Prerequesites of the pExpr->pLeft */
890 Bitmask prereqAll; /* Prerequesites of pExpr */
891 Bitmask extraRight = 0; /* Extra dependencies on LEFT JOIN */
892 Expr *pStr1 = 0; /* RHS of LIKE/GLOB operator */
893 int isComplete = 0; /* RHS of LIKE/GLOB ends with wildcard */
894 int noCase = 0; /* uppercase equivalent to lowercase */
895 int op; /* Top-level operator. pExpr->op */
896 Parse *pParse = pWInfo->pParse; /* Parsing context */
897 sqlite3 *db = pParse->db; /* Database connection */
dan07bdba82015-11-23 21:09:54 +0000898 unsigned char eOp2; /* op2 value for LIKE/REGEXP/GLOB */
drh6c1f4ef2015-06-08 14:23:15 +0000899
900 if( db->mallocFailed ){
901 return;
902 }
903 pTerm = &pWC->a[idxTerm];
904 pMaskSet = &pWInfo->sMaskSet;
905 pExpr = pTerm->pExpr;
906 assert( pExpr->op!=TK_AS && pExpr->op!=TK_COLLATE );
907 prereqLeft = sqlite3WhereExprUsage(pMaskSet, pExpr->pLeft);
908 op = pExpr->op;
909 if( op==TK_IN ){
910 assert( pExpr->pRight==0 );
911 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
912 pTerm->prereqRight = exprSelectUsage(pMaskSet, pExpr->x.pSelect);
913 }else{
914 pTerm->prereqRight = sqlite3WhereExprListUsage(pMaskSet, pExpr->x.pList);
915 }
916 }else if( op==TK_ISNULL ){
917 pTerm->prereqRight = 0;
918 }else{
919 pTerm->prereqRight = sqlite3WhereExprUsage(pMaskSet, pExpr->pRight);
920 }
921 prereqAll = sqlite3WhereExprUsage(pMaskSet, pExpr);
922 if( ExprHasProperty(pExpr, EP_FromJoin) ){
923 Bitmask x = sqlite3WhereGetMask(pMaskSet, pExpr->iRightJoinTable);
924 prereqAll |= x;
925 extraRight = x-1; /* ON clause terms may not be used with an index
926 ** on left table of a LEFT JOIN. Ticket #3015 */
927 }
928 pTerm->prereqAll = prereqAll;
929 pTerm->leftCursor = -1;
930 pTerm->iParent = -1;
931 pTerm->eOperator = 0;
932 if( allowedOp(op) ){
drh47991422015-08-31 15:58:06 +0000933 int iCur, iColumn;
drh6c1f4ef2015-06-08 14:23:15 +0000934 Expr *pLeft = sqlite3ExprSkipCollate(pExpr->pLeft);
935 Expr *pRight = sqlite3ExprSkipCollate(pExpr->pRight);
936 u16 opMask = (pTerm->prereqRight & prereqLeft)==0 ? WO_ALL : WO_EQUIV;
drh47991422015-08-31 15:58:06 +0000937 if( exprMightBeIndexed(pSrc, prereqLeft, pLeft, &iCur, &iColumn) ){
938 pTerm->leftCursor = iCur;
939 pTerm->u.leftColumn = iColumn;
drh6860e6f2015-08-27 18:24:02 +0000940 pTerm->eOperator = operatorMask(op) & opMask;
drh6c1f4ef2015-06-08 14:23:15 +0000941 }
942 if( op==TK_IS ) pTerm->wtFlags |= TERM_IS;
drh47991422015-08-31 15:58:06 +0000943 if( pRight
944 && exprMightBeIndexed(pSrc, pTerm->prereqRight, pRight, &iCur, &iColumn)
945 ){
drh6c1f4ef2015-06-08 14:23:15 +0000946 WhereTerm *pNew;
947 Expr *pDup;
948 u16 eExtraOp = 0; /* Extra bits for pNew->eOperator */
949 if( pTerm->leftCursor>=0 ){
950 int idxNew;
951 pDup = sqlite3ExprDup(db, pExpr, 0);
952 if( db->mallocFailed ){
953 sqlite3ExprDelete(db, pDup);
954 return;
955 }
956 idxNew = whereClauseInsert(pWC, pDup, TERM_VIRTUAL|TERM_DYNAMIC);
957 if( idxNew==0 ) return;
958 pNew = &pWC->a[idxNew];
959 markTermAsChild(pWC, idxNew, idxTerm);
960 if( op==TK_IS ) pNew->wtFlags |= TERM_IS;
961 pTerm = &pWC->a[idxTerm];
962 pTerm->wtFlags |= TERM_COPIED;
963
964 if( termIsEquivalence(pParse, pDup) ){
965 pTerm->eOperator |= WO_EQUIV;
966 eExtraOp = WO_EQUIV;
967 }
968 }else{
969 pDup = pExpr;
970 pNew = pTerm;
971 }
972 exprCommute(pParse, pDup);
drh47991422015-08-31 15:58:06 +0000973 pNew->leftCursor = iCur;
974 pNew->u.leftColumn = iColumn;
drh6c1f4ef2015-06-08 14:23:15 +0000975 testcase( (prereqLeft | extraRight) != prereqLeft );
976 pNew->prereqRight = prereqLeft | extraRight;
977 pNew->prereqAll = prereqAll;
978 pNew->eOperator = (operatorMask(pDup->op) + eExtraOp) & opMask;
979 }
980 }
981
982#ifndef SQLITE_OMIT_BETWEEN_OPTIMIZATION
983 /* If a term is the BETWEEN operator, create two new virtual terms
984 ** that define the range that the BETWEEN implements. For example:
985 **
986 ** a BETWEEN b AND c
987 **
988 ** is converted into:
989 **
990 ** (a BETWEEN b AND c) AND (a>=b) AND (a<=c)
991 **
992 ** The two new terms are added onto the end of the WhereClause object.
993 ** The new terms are "dynamic" and are children of the original BETWEEN
994 ** term. That means that if the BETWEEN term is coded, the children are
995 ** skipped. Or, if the children are satisfied by an index, the original
996 ** BETWEEN term is skipped.
997 */
998 else if( pExpr->op==TK_BETWEEN && pWC->op==TK_AND ){
999 ExprList *pList = pExpr->x.pList;
1000 int i;
1001 static const u8 ops[] = {TK_GE, TK_LE};
1002 assert( pList!=0 );
1003 assert( pList->nExpr==2 );
1004 for(i=0; i<2; i++){
1005 Expr *pNewExpr;
1006 int idxNew;
1007 pNewExpr = sqlite3PExpr(pParse, ops[i],
1008 sqlite3ExprDup(db, pExpr->pLeft, 0),
1009 sqlite3ExprDup(db, pList->a[i].pExpr, 0), 0);
1010 transferJoinMarkings(pNewExpr, pExpr);
1011 idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
1012 testcase( idxNew==0 );
1013 exprAnalyze(pSrc, pWC, idxNew);
1014 pTerm = &pWC->a[idxTerm];
1015 markTermAsChild(pWC, idxNew, idxTerm);
1016 }
1017 }
1018#endif /* SQLITE_OMIT_BETWEEN_OPTIMIZATION */
1019
1020#if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
1021 /* Analyze a term that is composed of two or more subterms connected by
1022 ** an OR operator.
1023 */
1024 else if( pExpr->op==TK_OR ){
1025 assert( pWC->op==TK_AND );
1026 exprAnalyzeOrTerm(pSrc, pWC, idxTerm);
1027 pTerm = &pWC->a[idxTerm];
1028 }
1029#endif /* SQLITE_OMIT_OR_OPTIMIZATION */
1030
1031#ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
1032 /* Add constraints to reduce the search space on a LIKE or GLOB
1033 ** operator.
1034 **
1035 ** A like pattern of the form "x LIKE 'aBc%'" is changed into constraints
1036 **
1037 ** x>='ABC' AND x<'abd' AND x LIKE 'aBc%'
1038 **
1039 ** The last character of the prefix "abc" is incremented to form the
1040 ** termination condition "abd". If case is not significant (the default
1041 ** for LIKE) then the lower-bound is made all uppercase and the upper-
1042 ** bound is made all lowercase so that the bounds also work when comparing
1043 ** BLOBs.
1044 */
1045 if( pWC->op==TK_AND
1046 && isLikeOrGlob(pParse, pExpr, &pStr1, &isComplete, &noCase)
1047 ){
1048 Expr *pLeft; /* LHS of LIKE/GLOB operator */
1049 Expr *pStr2; /* Copy of pStr1 - RHS of LIKE/GLOB operator */
1050 Expr *pNewExpr1;
1051 Expr *pNewExpr2;
1052 int idxNew1;
1053 int idxNew2;
1054 const char *zCollSeqName; /* Name of collating sequence */
1055 const u16 wtFlags = TERM_LIKEOPT | TERM_VIRTUAL | TERM_DYNAMIC;
1056
1057 pLeft = pExpr->x.pList->a[1].pExpr;
1058 pStr2 = sqlite3ExprDup(db, pStr1, 0);
1059
1060 /* Convert the lower bound to upper-case and the upper bound to
1061 ** lower-case (upper-case is less than lower-case in ASCII) so that
1062 ** the range constraints also work for BLOBs
1063 */
1064 if( noCase && !pParse->db->mallocFailed ){
1065 int i;
1066 char c;
1067 pTerm->wtFlags |= TERM_LIKE;
1068 for(i=0; (c = pStr1->u.zToken[i])!=0; i++){
1069 pStr1->u.zToken[i] = sqlite3Toupper(c);
1070 pStr2->u.zToken[i] = sqlite3Tolower(c);
1071 }
1072 }
1073
1074 if( !db->mallocFailed ){
1075 u8 c, *pC; /* Last character before the first wildcard */
1076 pC = (u8*)&pStr2->u.zToken[sqlite3Strlen30(pStr2->u.zToken)-1];
1077 c = *pC;
1078 if( noCase ){
1079 /* The point is to increment the last character before the first
1080 ** wildcard. But if we increment '@', that will push it into the
1081 ** alphabetic range where case conversions will mess up the
1082 ** inequality. To avoid this, make sure to also run the full
1083 ** LIKE on all candidate expressions by clearing the isComplete flag
1084 */
1085 if( c=='A'-1 ) isComplete = 0;
1086 c = sqlite3UpperToLower[c];
1087 }
1088 *pC = c + 1;
1089 }
1090 zCollSeqName = noCase ? "NOCASE" : "BINARY";
1091 pNewExpr1 = sqlite3ExprDup(db, pLeft, 0);
1092 pNewExpr1 = sqlite3PExpr(pParse, TK_GE,
1093 sqlite3ExprAddCollateString(pParse,pNewExpr1,zCollSeqName),
1094 pStr1, 0);
1095 transferJoinMarkings(pNewExpr1, pExpr);
1096 idxNew1 = whereClauseInsert(pWC, pNewExpr1, wtFlags);
1097 testcase( idxNew1==0 );
1098 exprAnalyze(pSrc, pWC, idxNew1);
1099 pNewExpr2 = sqlite3ExprDup(db, pLeft, 0);
1100 pNewExpr2 = sqlite3PExpr(pParse, TK_LT,
1101 sqlite3ExprAddCollateString(pParse,pNewExpr2,zCollSeqName),
1102 pStr2, 0);
1103 transferJoinMarkings(pNewExpr2, pExpr);
1104 idxNew2 = whereClauseInsert(pWC, pNewExpr2, wtFlags);
1105 testcase( idxNew2==0 );
1106 exprAnalyze(pSrc, pWC, idxNew2);
1107 pTerm = &pWC->a[idxTerm];
1108 if( isComplete ){
1109 markTermAsChild(pWC, idxNew1, idxTerm);
1110 markTermAsChild(pWC, idxNew2, idxTerm);
1111 }
1112 }
1113#endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
1114
1115#ifndef SQLITE_OMIT_VIRTUALTABLE
1116 /* Add a WO_MATCH auxiliary term to the constraint set if the
1117 ** current expression is of the form: column MATCH expr.
1118 ** This information is used by the xBestIndex methods of
1119 ** virtual tables. The native query optimizer does not attempt
1120 ** to do anything with MATCH functions.
1121 */
dan07bdba82015-11-23 21:09:54 +00001122 if( isMatchOfColumn(pExpr, &eOp2) ){
drh6c1f4ef2015-06-08 14:23:15 +00001123 int idxNew;
1124 Expr *pRight, *pLeft;
1125 WhereTerm *pNewTerm;
1126 Bitmask prereqColumn, prereqExpr;
1127
1128 pRight = pExpr->x.pList->a[0].pExpr;
1129 pLeft = pExpr->x.pList->a[1].pExpr;
1130 prereqExpr = sqlite3WhereExprUsage(pMaskSet, pRight);
1131 prereqColumn = sqlite3WhereExprUsage(pMaskSet, pLeft);
1132 if( (prereqExpr & prereqColumn)==0 ){
1133 Expr *pNewExpr;
1134 pNewExpr = sqlite3PExpr(pParse, TK_MATCH,
1135 0, sqlite3ExprDup(db, pRight, 0), 0);
1136 idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
1137 testcase( idxNew==0 );
1138 pNewTerm = &pWC->a[idxNew];
1139 pNewTerm->prereqRight = prereqExpr;
1140 pNewTerm->leftCursor = pLeft->iTable;
1141 pNewTerm->u.leftColumn = pLeft->iColumn;
1142 pNewTerm->eOperator = WO_MATCH;
dan07bdba82015-11-23 21:09:54 +00001143 pNewTerm->eMatchOp = eOp2;
drh6c1f4ef2015-06-08 14:23:15 +00001144 markTermAsChild(pWC, idxNew, idxTerm);
1145 pTerm = &pWC->a[idxTerm];
1146 pTerm->wtFlags |= TERM_COPIED;
1147 pNewTerm->prereqAll = pTerm->prereqAll;
1148 }
1149 }
1150#endif /* SQLITE_OMIT_VIRTUALTABLE */
1151
1152#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
1153 /* When sqlite_stat3 histogram data is available an operator of the
1154 ** form "x IS NOT NULL" can sometimes be evaluated more efficiently
1155 ** as "x>NULL" if x is not an INTEGER PRIMARY KEY. So construct a
1156 ** virtual term of that form.
1157 **
1158 ** Note that the virtual term must be tagged with TERM_VNULL.
1159 */
1160 if( pExpr->op==TK_NOTNULL
1161 && pExpr->pLeft->op==TK_COLUMN
1162 && pExpr->pLeft->iColumn>=0
1163 && OptimizationEnabled(db, SQLITE_Stat34)
1164 ){
1165 Expr *pNewExpr;
1166 Expr *pLeft = pExpr->pLeft;
1167 int idxNew;
1168 WhereTerm *pNewTerm;
1169
1170 pNewExpr = sqlite3PExpr(pParse, TK_GT,
1171 sqlite3ExprDup(db, pLeft, 0),
1172 sqlite3PExpr(pParse, TK_NULL, 0, 0, 0), 0);
1173
1174 idxNew = whereClauseInsert(pWC, pNewExpr,
1175 TERM_VIRTUAL|TERM_DYNAMIC|TERM_VNULL);
1176 if( idxNew ){
1177 pNewTerm = &pWC->a[idxNew];
1178 pNewTerm->prereqRight = 0;
1179 pNewTerm->leftCursor = pLeft->iTable;
1180 pNewTerm->u.leftColumn = pLeft->iColumn;
1181 pNewTerm->eOperator = WO_GT;
1182 markTermAsChild(pWC, idxNew, idxTerm);
1183 pTerm = &pWC->a[idxTerm];
1184 pTerm->wtFlags |= TERM_COPIED;
1185 pNewTerm->prereqAll = pTerm->prereqAll;
1186 }
1187 }
1188#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
1189
1190 /* Prevent ON clause terms of a LEFT JOIN from being used to drive
1191 ** an index for tables to the left of the join.
1192 */
1193 pTerm->prereqRight |= extraRight;
1194}
1195
1196/***************************************************************************
1197** Routines with file scope above. Interface to the rest of the where.c
1198** subsystem follows.
1199***************************************************************************/
1200
1201/*
1202** This routine identifies subexpressions in the WHERE clause where
1203** each subexpression is separated by the AND operator or some other
1204** operator specified in the op parameter. The WhereClause structure
1205** is filled with pointers to subexpressions. For example:
1206**
1207** WHERE a=='hello' AND coalesce(b,11)<10 AND (c+12!=d OR c==22)
1208** \________/ \_______________/ \________________/
1209** slot[0] slot[1] slot[2]
1210**
1211** The original WHERE clause in pExpr is unaltered. All this routine
1212** does is make slot[] entries point to substructure within pExpr.
1213**
1214** In the previous sentence and in the diagram, "slot[]" refers to
1215** the WhereClause.a[] array. The slot[] array grows as needed to contain
1216** all terms of the WHERE clause.
1217*/
1218void sqlite3WhereSplit(WhereClause *pWC, Expr *pExpr, u8 op){
1219 Expr *pE2 = sqlite3ExprSkipCollate(pExpr);
1220 pWC->op = op;
1221 if( pE2==0 ) return;
1222 if( pE2->op!=op ){
1223 whereClauseInsert(pWC, pExpr, 0);
1224 }else{
1225 sqlite3WhereSplit(pWC, pE2->pLeft, op);
1226 sqlite3WhereSplit(pWC, pE2->pRight, op);
1227 }
1228}
1229
1230/*
1231** Initialize a preallocated WhereClause structure.
1232*/
1233void sqlite3WhereClauseInit(
1234 WhereClause *pWC, /* The WhereClause to be initialized */
1235 WhereInfo *pWInfo /* The WHERE processing context */
1236){
1237 pWC->pWInfo = pWInfo;
1238 pWC->pOuter = 0;
1239 pWC->nTerm = 0;
1240 pWC->nSlot = ArraySize(pWC->aStatic);
1241 pWC->a = pWC->aStatic;
1242}
1243
1244/*
1245** Deallocate a WhereClause structure. The WhereClause structure
drh62aaa6c2015-11-21 17:27:42 +00001246** itself is not freed. This routine is the inverse of
1247** sqlite3WhereClauseInit().
drh6c1f4ef2015-06-08 14:23:15 +00001248*/
1249void sqlite3WhereClauseClear(WhereClause *pWC){
1250 int i;
1251 WhereTerm *a;
1252 sqlite3 *db = pWC->pWInfo->pParse->db;
1253 for(i=pWC->nTerm-1, a=pWC->a; i>=0; i--, a++){
1254 if( a->wtFlags & TERM_DYNAMIC ){
1255 sqlite3ExprDelete(db, a->pExpr);
1256 }
1257 if( a->wtFlags & TERM_ORINFO ){
1258 whereOrInfoDelete(db, a->u.pOrInfo);
1259 }else if( a->wtFlags & TERM_ANDINFO ){
1260 whereAndInfoDelete(db, a->u.pAndInfo);
1261 }
1262 }
1263 if( pWC->a!=pWC->aStatic ){
1264 sqlite3DbFree(db, pWC->a);
1265 }
1266}
1267
1268
1269/*
1270** These routines walk (recursively) an expression tree and generate
1271** a bitmask indicating which tables are used in that expression
1272** tree.
1273*/
1274Bitmask sqlite3WhereExprUsage(WhereMaskSet *pMaskSet, Expr *p){
1275 Bitmask mask = 0;
1276 if( p==0 ) return 0;
1277 if( p->op==TK_COLUMN ){
1278 mask = sqlite3WhereGetMask(pMaskSet, p->iTable);
1279 return mask;
1280 }
1281 mask = sqlite3WhereExprUsage(pMaskSet, p->pRight);
1282 mask |= sqlite3WhereExprUsage(pMaskSet, p->pLeft);
1283 if( ExprHasProperty(p, EP_xIsSelect) ){
1284 mask |= exprSelectUsage(pMaskSet, p->x.pSelect);
1285 }else{
1286 mask |= sqlite3WhereExprListUsage(pMaskSet, p->x.pList);
1287 }
1288 return mask;
1289}
1290Bitmask sqlite3WhereExprListUsage(WhereMaskSet *pMaskSet, ExprList *pList){
1291 int i;
1292 Bitmask mask = 0;
1293 if( pList ){
1294 for(i=0; i<pList->nExpr; i++){
1295 mask |= sqlite3WhereExprUsage(pMaskSet, pList->a[i].pExpr);
1296 }
1297 }
1298 return mask;
1299}
1300
1301
1302/*
1303** Call exprAnalyze on all terms in a WHERE clause.
1304**
1305** Note that exprAnalyze() might add new virtual terms onto the
1306** end of the WHERE clause. We do not want to analyze these new
1307** virtual terms, so start analyzing at the end and work forward
1308** so that the added virtual terms are never processed.
1309*/
1310void sqlite3WhereExprAnalyze(
1311 SrcList *pTabList, /* the FROM clause */
1312 WhereClause *pWC /* the WHERE clause to be analyzed */
1313){
1314 int i;
1315 for(i=pWC->nTerm-1; i>=0; i--){
1316 exprAnalyze(pTabList, pWC, i);
1317 }
1318}
drh01d230c2015-08-19 17:11:37 +00001319
1320/*
1321** For table-valued-functions, transform the function arguments into
1322** new WHERE clause terms.
1323**
1324** Each function argument translates into an equality constraint against
1325** a HIDDEN column in the table.
1326*/
1327void sqlite3WhereTabFuncArgs(
1328 Parse *pParse, /* Parsing context */
1329 struct SrcList_item *pItem, /* The FROM clause term to process */
1330 WhereClause *pWC /* Xfer function arguments to here */
1331){
1332 Table *pTab;
1333 int j, k;
1334 ExprList *pArgs;
1335 Expr *pColRef;
1336 Expr *pTerm;
1337 if( pItem->fg.isTabFunc==0 ) return;
1338 pTab = pItem->pTab;
1339 assert( pTab!=0 );
1340 pArgs = pItem->u1.pFuncArg;
drh20292312015-11-21 13:24:46 +00001341 if( pArgs==0 ) return;
drh01d230c2015-08-19 17:11:37 +00001342 for(j=k=0; j<pArgs->nExpr; j++){
drh62aaa6c2015-11-21 17:27:42 +00001343 while( k<pTab->nCol && (pTab->aCol[k].colFlags & COLFLAG_HIDDEN)==0 ){k++;}
drh01d230c2015-08-19 17:11:37 +00001344 if( k>=pTab->nCol ){
drhd8b1bfc2015-08-20 23:21:34 +00001345 sqlite3ErrorMsg(pParse, "too many arguments on %s() - max %d",
drh01d230c2015-08-19 17:11:37 +00001346 pTab->zName, j);
1347 return;
1348 }
1349 pColRef = sqlite3PExpr(pParse, TK_COLUMN, 0, 0, 0);
1350 if( pColRef==0 ) return;
1351 pColRef->iTable = pItem->iCursor;
1352 pColRef->iColumn = k++;
drh1f2fc282015-08-21 17:14:48 +00001353 pColRef->pTab = pTab;
drh01d230c2015-08-19 17:11:37 +00001354 pTerm = sqlite3PExpr(pParse, TK_EQ, pColRef,
1355 sqlite3ExprDup(pParse->db, pArgs->a[j].pExpr, 0), 0);
1356 whereClauseInsert(pWC, pTerm, TERM_DYNAMIC);
1357 }
1358}