blob: c02748102536696e8ec95197d5588ca51d6cfb0a [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;
drh575fad62016-02-05 13:38:36 +000067 pWC->a = sqlite3DbMallocRawNN(db, sizeof(pWC->a[0])*pWC->nSlot*2 );
drh6c1f4ef2015-06-08 14:23:15 +000068 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]);
drh6c1f4ef2015-06-08 14:23:15 +000080 }
81 pTerm = &pWC->a[idx = pWC->nTerm++];
82 if( p && ExprHasProperty(p, EP_Unlikely) ){
83 pTerm->truthProb = sqlite3LogEst(p->iTable) - 270;
84 }else{
85 pTerm->truthProb = 1;
86 }
87 pTerm->pExpr = sqlite3ExprSkipCollate(p);
88 pTerm->wtFlags = wtFlags;
89 pTerm->pWC = pWC;
90 pTerm->iParent = -1;
drh87c05f02016-10-03 14:44:47 +000091 memset(&pTerm->eOperator, 0,
92 sizeof(WhereTerm) - offsetof(WhereTerm,eOperator));
drh6c1f4ef2015-06-08 14:23:15 +000093 return idx;
94}
95
96/*
97** Return TRUE if the given operator is one of the operators that is
98** allowed for an indexable WHERE clause term. The allowed operators are
dan71c57db2016-07-09 20:23:55 +000099** "=", "<", ">", "<=", ">=", "IN", "IS", and "IS NULL"
drh6c1f4ef2015-06-08 14:23:15 +0000100*/
101static int allowedOp(int op){
102 assert( TK_GT>TK_EQ && TK_GT<TK_GE );
103 assert( TK_LT>TK_EQ && TK_LT<TK_GE );
104 assert( TK_LE>TK_EQ && TK_LE<TK_GE );
105 assert( TK_GE==TK_EQ+4 );
106 return op==TK_IN || (op>=TK_EQ && op<=TK_GE) || op==TK_ISNULL || op==TK_IS;
107}
108
109/*
110** Commute a comparison operator. Expressions of the form "X op Y"
111** are converted into "Y op X".
112**
113** If left/right precedence rules come into play when determining the
114** collating sequence, then COLLATE operators are adjusted to ensure
115** that the collating sequence does not change. For example:
116** "Y collate NOCASE op X" becomes "X op Y" because any collation sequence on
117** the left hand side of a comparison overrides any collation sequence
118** attached to the right. For the same reason the EP_Collate flag
119** is not commuted.
120*/
121static void exprCommute(Parse *pParse, Expr *pExpr){
122 u16 expRight = (pExpr->pRight->flags & EP_Collate);
123 u16 expLeft = (pExpr->pLeft->flags & EP_Collate);
124 assert( allowedOp(pExpr->op) && pExpr->op!=TK_IN );
125 if( expRight==expLeft ){
126 /* Either X and Y both have COLLATE operator or neither do */
127 if( expRight ){
128 /* Both X and Y have COLLATE operators. Make sure X is always
129 ** used by clearing the EP_Collate flag from Y. */
130 pExpr->pRight->flags &= ~EP_Collate;
131 }else if( sqlite3ExprCollSeq(pParse, pExpr->pLeft)!=0 ){
132 /* Neither X nor Y have COLLATE operators, but X has a non-default
133 ** collating sequence. So add the EP_Collate marker on X to cause
134 ** it to be searched first. */
135 pExpr->pLeft->flags |= EP_Collate;
136 }
137 }
138 SWAP(Expr*,pExpr->pRight,pExpr->pLeft);
139 if( pExpr->op>=TK_GT ){
140 assert( TK_LT==TK_GT+2 );
141 assert( TK_GE==TK_LE+2 );
142 assert( TK_GT>TK_EQ );
143 assert( TK_GT<TK_LE );
144 assert( pExpr->op>=TK_GT && pExpr->op<=TK_GE );
145 pExpr->op = ((pExpr->op-TK_GT)^2)+TK_GT;
146 }
147}
148
149/*
150** Translate from TK_xx operator to WO_xx bitmask.
151*/
152static u16 operatorMask(int op){
153 u16 c;
154 assert( allowedOp(op) );
155 if( op==TK_IN ){
156 c = WO_IN;
157 }else if( op==TK_ISNULL ){
158 c = WO_ISNULL;
159 }else if( op==TK_IS ){
160 c = WO_IS;
161 }else{
162 assert( (WO_EQ<<(op-TK_EQ)) < 0x7fff );
163 c = (u16)(WO_EQ<<(op-TK_EQ));
164 }
165 assert( op!=TK_ISNULL || c==WO_ISNULL );
166 assert( op!=TK_IN || c==WO_IN );
167 assert( op!=TK_EQ || c==WO_EQ );
168 assert( op!=TK_LT || c==WO_LT );
169 assert( op!=TK_LE || c==WO_LE );
170 assert( op!=TK_GT || c==WO_GT );
171 assert( op!=TK_GE || c==WO_GE );
172 assert( op!=TK_IS || c==WO_IS );
173 return c;
174}
175
176
177#ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
178/*
179** Check to see if the given expression is a LIKE or GLOB operator that
180** can be optimized using inequality constraints. Return TRUE if it is
181** so and false if not.
182**
183** In order for the operator to be optimizible, the RHS must be a string
184** literal that does not begin with a wildcard. The LHS must be a column
185** that may only be NULL, a string, or a BLOB, never a number. (This means
186** that virtual tables cannot participate in the LIKE optimization.) The
187** collating sequence for the column on the LHS must be appropriate for
188** the operator.
189*/
190static int isLikeOrGlob(
191 Parse *pParse, /* Parsing and code generating context */
192 Expr *pExpr, /* Test this expression */
193 Expr **ppPrefix, /* Pointer to TK_STRING expression with pattern prefix */
194 int *pisComplete, /* True if the only wildcard is % in the last character */
195 int *pnoCase /* True if uppercase is equivalent to lowercase */
196){
drhb8313cc2017-08-08 21:30:43 +0000197 const u8 *z = 0; /* String on RHS of LIKE operator */
drh6c1f4ef2015-06-08 14:23:15 +0000198 Expr *pRight, *pLeft; /* Right and left size of LIKE operator */
199 ExprList *pList; /* List of operands to the LIKE operator */
200 int c; /* One character in z[] */
201 int cnt; /* Number of non-wildcard prefix characters */
drh1d42ea72017-07-27 20:24:29 +0000202 char wc[4]; /* Wildcard characters */
drh6c1f4ef2015-06-08 14:23:15 +0000203 sqlite3 *db = pParse->db; /* Database connection */
204 sqlite3_value *pVal = 0;
205 int op; /* Opcode of pRight */
drhb8763632016-01-19 17:54:21 +0000206 int rc; /* Result code to return */
drh6c1f4ef2015-06-08 14:23:15 +0000207
208 if( !sqlite3IsLikeFunction(db, pExpr, pnoCase, wc) ){
209 return 0;
210 }
211#ifdef SQLITE_EBCDIC
212 if( *pnoCase ) return 0;
213#endif
214 pList = pExpr->x.pList;
215 pLeft = pList->a[1].pExpr;
drh6c1f4ef2015-06-08 14:23:15 +0000216
217 pRight = sqlite3ExprSkipCollate(pList->a[0].pExpr);
218 op = pRight->op;
drh7df74752017-06-26 14:46:05 +0000219 if( op==TK_VARIABLE && (db->flags & SQLITE_EnableQPSG)==0 ){
drh6c1f4ef2015-06-08 14:23:15 +0000220 Vdbe *pReprepare = pParse->pReprepare;
221 int iCol = pRight->iColumn;
222 pVal = sqlite3VdbeGetBoundValue(pReprepare, iCol, SQLITE_AFF_BLOB);
223 if( pVal && sqlite3_value_type(pVal)==SQLITE_TEXT ){
drhb8313cc2017-08-08 21:30:43 +0000224 z = sqlite3_value_text(pVal);
drh6c1f4ef2015-06-08 14:23:15 +0000225 }
226 sqlite3VdbeSetVarmask(pParse->pVdbe, iCol);
227 assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER );
228 }else if( op==TK_STRING ){
drhb8313cc2017-08-08 21:30:43 +0000229 z = (u8*)pRight->u.zToken;
drh6c1f4ef2015-06-08 14:23:15 +0000230 }
231 if( z ){
drh1c84bd42017-02-10 21:37:57 +0000232
233 /* If the RHS begins with a digit or a minus sign, then the LHS must
234 ** be an ordinary column (not a virtual table column) with TEXT affinity.
235 ** Otherwise the LHS might be numeric and "lhs >= rhs" would be false
236 ** even though "lhs LIKE rhs" is true. But if the RHS does not start
237 ** with a digit or '-', then "lhs LIKE rhs" will always be false if
238 ** the LHS is numeric and so the optimization still works.
239 */
240 if( sqlite3Isdigit(z[0]) || z[0]=='-' ){
241 if( pLeft->op!=TK_COLUMN
242 || sqlite3ExprAffinity(pLeft)!=SQLITE_AFF_TEXT
243 || IsVirtual(pLeft->pTab) /* Value might be numeric */
244 ){
245 sqlite3ValueFree(pVal);
246 return 0;
247 }
248 }
drh1d42ea72017-07-27 20:24:29 +0000249
250 /* Count the number of prefix characters prior to the first wildcard */
drh6c1f4ef2015-06-08 14:23:15 +0000251 cnt = 0;
252 while( (c=z[cnt])!=0 && c!=wc[0] && c!=wc[1] && c!=wc[2] ){
253 cnt++;
drhf41a8d32017-08-11 03:47:21 +0000254 if( c==wc[3] && z[cnt]!=0 ) cnt++;
drh6c1f4ef2015-06-08 14:23:15 +0000255 }
drh1d42ea72017-07-27 20:24:29 +0000256
257 /* The optimization is possible only if (1) the pattern does not begin
258 ** with a wildcard and if (2) the non-wildcard prefix does not end with
259 ** an (illegal 0xff) character. The second condition is necessary so
260 ** that we can increment the prefix key to find an upper bound for the
261 ** range search.
262 */
drh6c1f4ef2015-06-08 14:23:15 +0000263 if( cnt!=0 && 255!=(u8)z[cnt-1] ){
264 Expr *pPrefix;
drh1d42ea72017-07-27 20:24:29 +0000265
266 /* A "complete" match if the pattern ends with "*" or "%" */
drh6c1f4ef2015-06-08 14:23:15 +0000267 *pisComplete = c==wc[0] && z[cnt+1]==0;
drh1d42ea72017-07-27 20:24:29 +0000268
269 /* Get the pattern prefix. Remove all escapes from the prefix. */
drhb8313cc2017-08-08 21:30:43 +0000270 pPrefix = sqlite3Expr(db, TK_STRING, (char*)z);
drh1d42ea72017-07-27 20:24:29 +0000271 if( pPrefix ){
272 int iFrom, iTo;
273 char *zNew = pPrefix->u.zToken;
274 zNew[cnt] = 0;
275 for(iFrom=iTo=0; iFrom<cnt; iFrom++){
276 if( zNew[iFrom]==wc[3] ) iFrom++;
277 zNew[iTo++] = zNew[iFrom];
278 }
279 zNew[iTo] = 0;
280 }
drh6c1f4ef2015-06-08 14:23:15 +0000281 *ppPrefix = pPrefix;
drh1d42ea72017-07-27 20:24:29 +0000282
283 /* If the RHS pattern is a bound parameter, make arrangements to
284 ** reprepare the statement when that parameter is rebound */
drh6c1f4ef2015-06-08 14:23:15 +0000285 if( op==TK_VARIABLE ){
286 Vdbe *v = pParse->pVdbe;
287 sqlite3VdbeSetVarmask(v, pRight->iColumn);
288 if( *pisComplete && pRight->u.zToken[1] ){
289 /* If the rhs of the LIKE expression is a variable, and the current
290 ** value of the variable means there is no need to invoke the LIKE
291 ** function, then no OP_Variable will be added to the program.
292 ** This causes problems for the sqlite3_bind_parameter_name()
293 ** API. To work around them, add a dummy OP_Variable here.
294 */
295 int r1 = sqlite3GetTempReg(pParse);
296 sqlite3ExprCodeTarget(pParse, pRight, r1);
297 sqlite3VdbeChangeP3(v, sqlite3VdbeCurrentAddr(v)-1, 0);
298 sqlite3ReleaseTempReg(pParse, r1);
299 }
300 }
301 }else{
302 z = 0;
303 }
304 }
305
drhb8763632016-01-19 17:54:21 +0000306 rc = (z!=0);
drh6c1f4ef2015-06-08 14:23:15 +0000307 sqlite3ValueFree(pVal);
drhb8763632016-01-19 17:54:21 +0000308 return rc;
drh6c1f4ef2015-06-08 14:23:15 +0000309}
310#endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
311
312
313#ifndef SQLITE_OMIT_VIRTUALTABLE
314/*
drh303a69b2017-09-11 19:47:37 +0000315** Check to see if the pExpr expression is a form that needs to be passed
316** to the xBestIndex method of virtual tables. Forms of interest include:
drh6c1f4ef2015-06-08 14:23:15 +0000317**
drh303a69b2017-09-11 19:47:37 +0000318** Expression Virtual Table Operator
319** ----------------------- ---------------------------------
320** 1. column MATCH expr SQLITE_INDEX_CONSTRAINT_MATCH
321** 2. column GLOB expr SQLITE_INDEX_CONSTRAINT_GLOB
322** 3. column LIKE expr SQLITE_INDEX_CONSTRAINT_LIKE
323** 4. column REGEXP expr SQLITE_INDEX_CONSTRAINT_REGEXP
324** 5. column != expr SQLITE_INDEX_CONSTRAINT_NE
325** 6. expr != column SQLITE_INDEX_CONSTRAINT_NE
326** 7. column IS NOT expr SQLITE_INDEX_CONSTRAINT_ISNOT
327** 8. expr IS NOT column SQLITE_INDEX_CONSTRAINT_ISNOT
328** 9. column IS NOT NULL SQLITE_INDEX_CONSTRAINT_ISNOTNULL
dan43970dd2015-11-24 17:39:01 +0000329**
drh303a69b2017-09-11 19:47:37 +0000330** In every case, "column" must be a column of a virtual table. If there
331** is a match, set *ppLeft to the "column" expression, set *ppRight to the
332** "expr" expression (even though in forms (6) and (8) the column is on the
333** right and the expression is on the left). Also set *peOp2 to the
334** appropriate virtual table operator. The return value is 1 or 2 if there
335** is a match. The usual return is 1, but if the RHS is also a column
336** of virtual table in forms (5) or (7) then return 2.
dand03024d2017-09-09 19:41:12 +0000337**
338** If the expression matches none of the patterns above, return 0.
drh6c1f4ef2015-06-08 14:23:15 +0000339*/
drh303a69b2017-09-11 19:47:37 +0000340static int isAuxiliaryVtabOperator(
dan07bdba82015-11-23 21:09:54 +0000341 Expr *pExpr, /* Test this expression */
dand03024d2017-09-09 19:41:12 +0000342 unsigned char *peOp2, /* OUT: 0 for MATCH, or else an op2 value */
343 Expr **ppLeft, /* Column expression to left of MATCH/op2 */
344 Expr **ppRight /* Expression to left of MATCH/op2 */
drh6c1f4ef2015-06-08 14:23:15 +0000345){
dand03024d2017-09-09 19:41:12 +0000346 if( pExpr->op==TK_FUNCTION ){
347 static const struct Op2 {
348 const char *zOp;
349 unsigned char eOp2;
350 } aOp[] = {
351 { "match", SQLITE_INDEX_CONSTRAINT_MATCH },
352 { "glob", SQLITE_INDEX_CONSTRAINT_GLOB },
353 { "like", SQLITE_INDEX_CONSTRAINT_LIKE },
354 { "regexp", SQLITE_INDEX_CONSTRAINT_REGEXP }
355 };
356 ExprList *pList;
357 Expr *pCol; /* Column reference */
358 int i;
drh6c1f4ef2015-06-08 14:23:15 +0000359
dand03024d2017-09-09 19:41:12 +0000360 pList = pExpr->x.pList;
361 if( pList==0 || pList->nExpr!=2 ){
362 return 0;
363 }
364 pCol = pList->a[1].pExpr;
365 if( pCol->op!=TK_COLUMN || !IsVirtual(pCol->pTab) ){
366 return 0;
367 }
368 for(i=0; i<ArraySize(aOp); i++){
369 if( sqlite3StrICmp(pExpr->u.zToken, aOp[i].zOp)==0 ){
370 *peOp2 = aOp[i].eOp2;
371 *ppRight = pList->a[0].pExpr;
372 *ppLeft = pCol;
373 return 1;
374 }
375 }
376 }else if( pExpr->op==TK_NE || pExpr->op==TK_ISNOT || pExpr->op==TK_NOTNULL ){
377 int res = 0;
378 Expr *pLeft = pExpr->pLeft;
379 Expr *pRight = pExpr->pRight;
380 if( pLeft->op==TK_COLUMN && IsVirtual(pLeft->pTab) ){
381 res++;
382 }
383 if( pRight && pRight->op==TK_COLUMN && IsVirtual(pRight->pTab) ){
384 res++;
385 SWAP(Expr*, pLeft, pRight);
386 }
387 *ppLeft = pLeft;
388 *ppRight = pRight;
389 if( pExpr->op==TK_NE ) *peOp2 = SQLITE_INDEX_CONSTRAINT_NE;
390 if( pExpr->op==TK_ISNOT ) *peOp2 = SQLITE_INDEX_CONSTRAINT_ISNOT;
391 if( pExpr->op==TK_NOTNULL ) *peOp2 = SQLITE_INDEX_CONSTRAINT_ISNOTNULL;
392 return res;
dan07bdba82015-11-23 21:09:54 +0000393 }
394 return 0;
drh6c1f4ef2015-06-08 14:23:15 +0000395}
396#endif /* SQLITE_OMIT_VIRTUALTABLE */
397
398/*
399** If the pBase expression originated in the ON or USING clause of
400** a join, then transfer the appropriate markings over to derived.
401*/
402static void transferJoinMarkings(Expr *pDerived, Expr *pBase){
403 if( pDerived ){
404 pDerived->flags |= pBase->flags & EP_FromJoin;
405 pDerived->iRightJoinTable = pBase->iRightJoinTable;
406 }
407}
408
409/*
410** Mark term iChild as being a child of term iParent
411*/
412static void markTermAsChild(WhereClause *pWC, int iChild, int iParent){
413 pWC->a[iChild].iParent = iParent;
414 pWC->a[iChild].truthProb = pWC->a[iParent].truthProb;
415 pWC->a[iParent].nChild++;
416}
417
418/*
419** Return the N-th AND-connected subterm of pTerm. Or if pTerm is not
420** a conjunction, then return just pTerm when N==0. If N is exceeds
421** the number of available subterms, return NULL.
422*/
423static WhereTerm *whereNthSubterm(WhereTerm *pTerm, int N){
424 if( pTerm->eOperator!=WO_AND ){
425 return N==0 ? pTerm : 0;
426 }
427 if( N<pTerm->u.pAndInfo->wc.nTerm ){
428 return &pTerm->u.pAndInfo->wc.a[N];
429 }
430 return 0;
431}
432
433/*
434** Subterms pOne and pTwo are contained within WHERE clause pWC. The
435** two subterms are in disjunction - they are OR-ed together.
436**
437** If these two terms are both of the form: "A op B" with the same
438** A and B values but different operators and if the operators are
439** compatible (if one is = and the other is <, for example) then
440** add a new virtual AND term to pWC that is the combination of the
441** two.
442**
443** Some examples:
444**
445** x<y OR x=y --> x<=y
446** x=y OR x=y --> x=y
447** x<=y OR x<y --> x<=y
448**
449** The following is NOT generated:
450**
451** x<y OR x>y --> x!=y
452*/
453static void whereCombineDisjuncts(
454 SrcList *pSrc, /* the FROM clause */
455 WhereClause *pWC, /* The complete WHERE clause */
456 WhereTerm *pOne, /* First disjunct */
457 WhereTerm *pTwo /* Second disjunct */
458){
459 u16 eOp = pOne->eOperator | pTwo->eOperator;
460 sqlite3 *db; /* Database connection (for malloc) */
461 Expr *pNew; /* New virtual expression */
462 int op; /* Operator for the combined expression */
463 int idxNew; /* Index in pWC of the next virtual term */
464
465 if( (pOne->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return;
466 if( (pTwo->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return;
467 if( (eOp & (WO_EQ|WO_LT|WO_LE))!=eOp
468 && (eOp & (WO_EQ|WO_GT|WO_GE))!=eOp ) return;
469 assert( pOne->pExpr->pLeft!=0 && pOne->pExpr->pRight!=0 );
470 assert( pTwo->pExpr->pLeft!=0 && pTwo->pExpr->pRight!=0 );
dan5aa550c2017-06-24 18:10:29 +0000471 if( sqlite3ExprCompare(0,pOne->pExpr->pLeft, pTwo->pExpr->pLeft, -1) ) return;
472 if( sqlite3ExprCompare(0,pOne->pExpr->pRight, pTwo->pExpr->pRight,-1) )return;
drh6c1f4ef2015-06-08 14:23:15 +0000473 /* If we reach this point, it means the two subterms can be combined */
474 if( (eOp & (eOp-1))!=0 ){
475 if( eOp & (WO_LT|WO_LE) ){
476 eOp = WO_LE;
477 }else{
478 assert( eOp & (WO_GT|WO_GE) );
479 eOp = WO_GE;
480 }
481 }
482 db = pWC->pWInfo->pParse->db;
483 pNew = sqlite3ExprDup(db, pOne->pExpr, 0);
484 if( pNew==0 ) return;
485 for(op=TK_EQ; eOp!=(WO_EQ<<(op-TK_EQ)); op++){ assert( op<TK_GE ); }
486 pNew->op = op;
487 idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC);
488 exprAnalyze(pSrc, pWC, idxNew);
489}
490
491#if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
492/*
493** Analyze a term that consists of two or more OR-connected
494** subterms. So in:
495**
496** ... WHERE (a=5) AND (b=7 OR c=9 OR d=13) AND (d=13)
497** ^^^^^^^^^^^^^^^^^^^^
498**
499** This routine analyzes terms such as the middle term in the above example.
500** A WhereOrTerm object is computed and attached to the term under
501** analysis, regardless of the outcome of the analysis. Hence:
502**
503** WhereTerm.wtFlags |= TERM_ORINFO
504** WhereTerm.u.pOrInfo = a dynamically allocated WhereOrTerm object
505**
506** The term being analyzed must have two or more of OR-connected subterms.
507** A single subterm might be a set of AND-connected sub-subterms.
508** Examples of terms under analysis:
509**
510** (A) t1.x=t2.y OR t1.x=t2.z OR t1.y=15 OR t1.z=t3.a+5
511** (B) x=expr1 OR expr2=x OR x=expr3
512** (C) t1.x=t2.y OR (t1.x=t2.z AND t1.y=15)
513** (D) x=expr1 OR (y>11 AND y<22 AND z LIKE '*hello*')
514** (E) (p.a=1 AND q.b=2 AND r.c=3) OR (p.x=4 AND q.y=5 AND r.z=6)
515** (F) x>A OR (x=A AND y>=B)
516**
517** CASE 1:
518**
519** If all subterms are of the form T.C=expr for some single column of C and
520** a single table T (as shown in example B above) then create a new virtual
521** term that is an equivalent IN expression. In other words, if the term
522** being analyzed is:
523**
524** x = expr1 OR expr2 = x OR x = expr3
525**
526** then create a new virtual term like this:
527**
528** x IN (expr1,expr2,expr3)
529**
530** CASE 2:
531**
532** If there are exactly two disjuncts and one side has x>A and the other side
533** has x=A (for the same x and A) then add a new virtual conjunct term to the
534** WHERE clause of the form "x>=A". Example:
535**
536** x>A OR (x=A AND y>B) adds: x>=A
537**
538** The added conjunct can sometimes be helpful in query planning.
539**
540** CASE 3:
541**
542** If all subterms are indexable by a single table T, then set
543**
544** WhereTerm.eOperator = WO_OR
545** WhereTerm.u.pOrInfo->indexable |= the cursor number for table T
546**
547** A subterm is "indexable" if it is of the form
548** "T.C <op> <expr>" where C is any column of table T and
549** <op> is one of "=", "<", "<=", ">", ">=", "IS NULL", or "IN".
550** A subterm is also indexable if it is an AND of two or more
551** subsubterms at least one of which is indexable. Indexable AND
552** subterms have their eOperator set to WO_AND and they have
553** u.pAndInfo set to a dynamically allocated WhereAndTerm object.
554**
555** From another point of view, "indexable" means that the subterm could
556** potentially be used with an index if an appropriate index exists.
557** This analysis does not consider whether or not the index exists; that
558** is decided elsewhere. This analysis only looks at whether subterms
559** appropriate for indexing exist.
560**
561** All examples A through E above satisfy case 3. But if a term
562** also satisfies case 1 (such as B) we know that the optimizer will
563** always prefer case 1, so in that case we pretend that case 3 is not
564** satisfied.
565**
566** It might be the case that multiple tables are indexable. For example,
567** (E) above is indexable on tables P, Q, and R.
568**
569** Terms that satisfy case 3 are candidates for lookup by using
570** separate indices to find rowids for each subterm and composing
571** the union of all rowids using a RowSet object. This is similar
572** to "bitmap indices" in other database engines.
573**
574** OTHERWISE:
575**
576** If none of cases 1, 2, or 3 apply, then leave the eOperator set to
577** zero. This term is not useful for search.
578*/
579static void exprAnalyzeOrTerm(
580 SrcList *pSrc, /* the FROM clause */
581 WhereClause *pWC, /* the complete WHERE clause */
582 int idxTerm /* Index of the OR-term to be analyzed */
583){
584 WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */
585 Parse *pParse = pWInfo->pParse; /* Parser context */
586 sqlite3 *db = pParse->db; /* Database connection */
587 WhereTerm *pTerm = &pWC->a[idxTerm]; /* The term to be analyzed */
588 Expr *pExpr = pTerm->pExpr; /* The expression of the term */
589 int i; /* Loop counters */
590 WhereClause *pOrWc; /* Breakup of pTerm into subterms */
591 WhereTerm *pOrTerm; /* A Sub-term within the pOrWc */
592 WhereOrInfo *pOrInfo; /* Additional information associated with pTerm */
593 Bitmask chngToIN; /* Tables that might satisfy case 1 */
594 Bitmask indexable; /* Tables that are indexable, satisfying case 2 */
595
596 /*
597 ** Break the OR clause into its separate subterms. The subterms are
598 ** stored in a WhereClause structure containing within the WhereOrInfo
599 ** object that is attached to the original OR clause term.
600 */
601 assert( (pTerm->wtFlags & (TERM_DYNAMIC|TERM_ORINFO|TERM_ANDINFO))==0 );
602 assert( pExpr->op==TK_OR );
603 pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, sizeof(*pOrInfo));
604 if( pOrInfo==0 ) return;
605 pTerm->wtFlags |= TERM_ORINFO;
606 pOrWc = &pOrInfo->wc;
drh81fd3492016-02-19 14:10:44 +0000607 memset(pOrWc->aStatic, 0, sizeof(pOrWc->aStatic));
drh6c1f4ef2015-06-08 14:23:15 +0000608 sqlite3WhereClauseInit(pOrWc, pWInfo);
609 sqlite3WhereSplit(pOrWc, pExpr, TK_OR);
610 sqlite3WhereExprAnalyze(pSrc, pOrWc);
611 if( db->mallocFailed ) return;
612 assert( pOrWc->nTerm>=2 );
613
614 /*
615 ** Compute the set of tables that might satisfy cases 1 or 3.
616 */
617 indexable = ~(Bitmask)0;
618 chngToIN = ~(Bitmask)0;
619 for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0 && indexable; i--, pOrTerm++){
620 if( (pOrTerm->eOperator & WO_SINGLE)==0 ){
621 WhereAndInfo *pAndInfo;
622 assert( (pOrTerm->wtFlags & (TERM_ANDINFO|TERM_ORINFO))==0 );
623 chngToIN = 0;
drh575fad62016-02-05 13:38:36 +0000624 pAndInfo = sqlite3DbMallocRawNN(db, sizeof(*pAndInfo));
drh6c1f4ef2015-06-08 14:23:15 +0000625 if( pAndInfo ){
626 WhereClause *pAndWC;
627 WhereTerm *pAndTerm;
628 int j;
629 Bitmask b = 0;
630 pOrTerm->u.pAndInfo = pAndInfo;
631 pOrTerm->wtFlags |= TERM_ANDINFO;
632 pOrTerm->eOperator = WO_AND;
633 pAndWC = &pAndInfo->wc;
drh81fd3492016-02-19 14:10:44 +0000634 memset(pAndWC->aStatic, 0, sizeof(pAndWC->aStatic));
drh6c1f4ef2015-06-08 14:23:15 +0000635 sqlite3WhereClauseInit(pAndWC, pWC->pWInfo);
636 sqlite3WhereSplit(pAndWC, pOrTerm->pExpr, TK_AND);
637 sqlite3WhereExprAnalyze(pSrc, pAndWC);
638 pAndWC->pOuter = pWC;
drh6c1f4ef2015-06-08 14:23:15 +0000639 if( !db->mallocFailed ){
640 for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){
641 assert( pAndTerm->pExpr );
dandbd2dcb2016-05-28 18:53:55 +0000642 if( allowedOp(pAndTerm->pExpr->op)
drh303a69b2017-09-11 19:47:37 +0000643 || pAndTerm->eOperator==WO_AUX
dandbd2dcb2016-05-28 18:53:55 +0000644 ){
drh6c1f4ef2015-06-08 14:23:15 +0000645 b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pAndTerm->leftCursor);
646 }
647 }
648 }
649 indexable &= b;
650 }
651 }else if( pOrTerm->wtFlags & TERM_COPIED ){
652 /* Skip this term for now. We revisit it when we process the
653 ** corresponding TERM_VIRTUAL term */
654 }else{
655 Bitmask b;
656 b = sqlite3WhereGetMask(&pWInfo->sMaskSet, pOrTerm->leftCursor);
657 if( pOrTerm->wtFlags & TERM_VIRTUAL ){
658 WhereTerm *pOther = &pOrWc->a[pOrTerm->iParent];
659 b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pOther->leftCursor);
660 }
661 indexable &= b;
662 if( (pOrTerm->eOperator & WO_EQ)==0 ){
663 chngToIN = 0;
664 }else{
665 chngToIN &= b;
666 }
667 }
668 }
669
670 /*
671 ** Record the set of tables that satisfy case 3. The set might be
672 ** empty.
673 */
674 pOrInfo->indexable = indexable;
drhda230bd2018-06-09 00:09:58 +0000675 if( indexable ){
676 pTerm->eOperator = WO_OR;
677 pWC->hasOr = 1;
678 }else{
679 pTerm->eOperator = WO_OR;
680 }
drh6c1f4ef2015-06-08 14:23:15 +0000681
682 /* For a two-way OR, attempt to implementation case 2.
683 */
684 if( indexable && pOrWc->nTerm==2 ){
685 int iOne = 0;
686 WhereTerm *pOne;
687 while( (pOne = whereNthSubterm(&pOrWc->a[0],iOne++))!=0 ){
688 int iTwo = 0;
689 WhereTerm *pTwo;
690 while( (pTwo = whereNthSubterm(&pOrWc->a[1],iTwo++))!=0 ){
691 whereCombineDisjuncts(pSrc, pWC, pOne, pTwo);
692 }
693 }
694 }
695
696 /*
697 ** chngToIN holds a set of tables that *might* satisfy case 1. But
698 ** we have to do some additional checking to see if case 1 really
699 ** is satisfied.
700 **
701 ** chngToIN will hold either 0, 1, or 2 bits. The 0-bit case means
702 ** that there is no possibility of transforming the OR clause into an
703 ** IN operator because one or more terms in the OR clause contain
704 ** something other than == on a column in the single table. The 1-bit
705 ** case means that every term of the OR clause is of the form
706 ** "table.column=expr" for some single table. The one bit that is set
707 ** will correspond to the common table. We still need to check to make
708 ** sure the same column is used on all terms. The 2-bit case is when
709 ** the all terms are of the form "table1.column=table2.column". It
710 ** might be possible to form an IN operator with either table1.column
711 ** or table2.column as the LHS if either is common to every term of
712 ** the OR clause.
713 **
714 ** Note that terms of the form "table.column1=table.column2" (the
715 ** same table on both sizes of the ==) cannot be optimized.
716 */
717 if( chngToIN ){
718 int okToChngToIN = 0; /* True if the conversion to IN is valid */
719 int iColumn = -1; /* Column index on lhs of IN operator */
720 int iCursor = -1; /* Table cursor common to all terms */
721 int j = 0; /* Loop counter */
722
723 /* Search for a table and column that appears on one side or the
724 ** other of the == operator in every subterm. That table and column
725 ** will be recorded in iCursor and iColumn. There might not be any
726 ** such table and column. Set okToChngToIN if an appropriate table
727 ** and column is found but leave okToChngToIN false if not found.
728 */
729 for(j=0; j<2 && !okToChngToIN; j++){
730 pOrTerm = pOrWc->a;
731 for(i=pOrWc->nTerm-1; i>=0; i--, pOrTerm++){
732 assert( pOrTerm->eOperator & WO_EQ );
733 pOrTerm->wtFlags &= ~TERM_OR_OK;
734 if( pOrTerm->leftCursor==iCursor ){
735 /* This is the 2-bit case and we are on the second iteration and
736 ** current term is from the first iteration. So skip this term. */
737 assert( j==1 );
738 continue;
739 }
740 if( (chngToIN & sqlite3WhereGetMask(&pWInfo->sMaskSet,
741 pOrTerm->leftCursor))==0 ){
742 /* This term must be of the form t1.a==t2.b where t2 is in the
743 ** chngToIN set but t1 is not. This term will be either preceded
744 ** or follwed by an inverted copy (t2.b==t1.a). Skip this term
745 ** and use its inversion. */
746 testcase( pOrTerm->wtFlags & TERM_COPIED );
747 testcase( pOrTerm->wtFlags & TERM_VIRTUAL );
748 assert( pOrTerm->wtFlags & (TERM_COPIED|TERM_VIRTUAL) );
749 continue;
750 }
751 iColumn = pOrTerm->u.leftColumn;
752 iCursor = pOrTerm->leftCursor;
753 break;
754 }
755 if( i<0 ){
756 /* No candidate table+column was found. This can only occur
757 ** on the second iteration */
758 assert( j==1 );
759 assert( IsPowerOfTwo(chngToIN) );
760 assert( chngToIN==sqlite3WhereGetMask(&pWInfo->sMaskSet, iCursor) );
761 break;
762 }
763 testcase( j==1 );
764
765 /* We have found a candidate table and column. Check to see if that
766 ** table and column is common to every term in the OR clause */
767 okToChngToIN = 1;
768 for(; i>=0 && okToChngToIN; i--, pOrTerm++){
769 assert( pOrTerm->eOperator & WO_EQ );
770 if( pOrTerm->leftCursor!=iCursor ){
771 pOrTerm->wtFlags &= ~TERM_OR_OK;
772 }else if( pOrTerm->u.leftColumn!=iColumn ){
773 okToChngToIN = 0;
774 }else{
775 int affLeft, affRight;
776 /* If the right-hand side is also a column, then the affinities
777 ** of both right and left sides must be such that no type
778 ** conversions are required on the right. (Ticket #2249)
779 */
780 affRight = sqlite3ExprAffinity(pOrTerm->pExpr->pRight);
781 affLeft = sqlite3ExprAffinity(pOrTerm->pExpr->pLeft);
782 if( affRight!=0 && affRight!=affLeft ){
783 okToChngToIN = 0;
784 }else{
785 pOrTerm->wtFlags |= TERM_OR_OK;
786 }
787 }
788 }
789 }
790
791 /* At this point, okToChngToIN is true if original pTerm satisfies
792 ** case 1. In that case, construct a new virtual term that is
793 ** pTerm converted into an IN operator.
794 */
795 if( okToChngToIN ){
796 Expr *pDup; /* A transient duplicate expression */
797 ExprList *pList = 0; /* The RHS of the IN operator */
798 Expr *pLeft = 0; /* The LHS of the IN operator */
799 Expr *pNew; /* The complete IN operator */
800
801 for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0; i--, pOrTerm++){
802 if( (pOrTerm->wtFlags & TERM_OR_OK)==0 ) continue;
803 assert( pOrTerm->eOperator & WO_EQ );
804 assert( pOrTerm->leftCursor==iCursor );
805 assert( pOrTerm->u.leftColumn==iColumn );
806 pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight, 0);
807 pList = sqlite3ExprListAppend(pWInfo->pParse, pList, pDup);
808 pLeft = pOrTerm->pExpr->pLeft;
809 }
810 assert( pLeft!=0 );
811 pDup = sqlite3ExprDup(db, pLeft, 0);
drhabfd35e2016-12-06 22:47:23 +0000812 pNew = sqlite3PExpr(pParse, TK_IN, pDup, 0);
drh6c1f4ef2015-06-08 14:23:15 +0000813 if( pNew ){
814 int idxNew;
815 transferJoinMarkings(pNew, pExpr);
816 assert( !ExprHasProperty(pNew, EP_xIsSelect) );
817 pNew->x.pList = pList;
818 idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC);
819 testcase( idxNew==0 );
820 exprAnalyze(pSrc, pWC, idxNew);
821 pTerm = &pWC->a[idxTerm];
822 markTermAsChild(pWC, idxNew, idxTerm);
823 }else{
824 sqlite3ExprListDelete(db, pList);
825 }
drh6c1f4ef2015-06-08 14:23:15 +0000826 }
827 }
828}
829#endif /* !SQLITE_OMIT_OR_OPTIMIZATION && !SQLITE_OMIT_SUBQUERY */
830
831/*
832** We already know that pExpr is a binary operator where both operands are
833** column references. This routine checks to see if pExpr is an equivalence
834** relation:
835** 1. The SQLITE_Transitive optimization must be enabled
836** 2. Must be either an == or an IS operator
837** 3. Not originating in the ON clause of an OUTER JOIN
838** 4. The affinities of A and B must be compatible
839** 5a. Both operands use the same collating sequence OR
840** 5b. The overall collating sequence is BINARY
841** If this routine returns TRUE, that means that the RHS can be substituted
842** for the LHS anyplace else in the WHERE clause where the LHS column occurs.
843** This is an optimization. No harm comes from returning 0. But if 1 is
844** returned when it should not be, then incorrect answers might result.
845*/
846static int termIsEquivalence(Parse *pParse, Expr *pExpr){
847 char aff1, aff2;
848 CollSeq *pColl;
drh6c1f4ef2015-06-08 14:23:15 +0000849 if( !OptimizationEnabled(pParse->db, SQLITE_Transitive) ) return 0;
850 if( pExpr->op!=TK_EQ && pExpr->op!=TK_IS ) return 0;
851 if( ExprHasProperty(pExpr, EP_FromJoin) ) return 0;
852 aff1 = sqlite3ExprAffinity(pExpr->pLeft);
853 aff2 = sqlite3ExprAffinity(pExpr->pRight);
854 if( aff1!=aff2
855 && (!sqlite3IsNumericAffinity(aff1) || !sqlite3IsNumericAffinity(aff2))
856 ){
857 return 0;
858 }
859 pColl = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft, pExpr->pRight);
860 if( pColl==0 || sqlite3StrICmp(pColl->zName, "BINARY")==0 ) return 1;
drh70efa842017-09-28 01:58:23 +0000861 return sqlite3ExprCollSeqMatch(pParse, pExpr->pLeft, pExpr->pRight);
drh6c1f4ef2015-06-08 14:23:15 +0000862}
863
864/*
865** Recursively walk the expressions of a SELECT statement and generate
866** a bitmask indicating which tables are used in that expression
867** tree.
868*/
869static Bitmask exprSelectUsage(WhereMaskSet *pMaskSet, Select *pS){
870 Bitmask mask = 0;
871 while( pS ){
872 SrcList *pSrc = pS->pSrc;
873 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pEList);
874 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pGroupBy);
875 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pOrderBy);
876 mask |= sqlite3WhereExprUsage(pMaskSet, pS->pWhere);
877 mask |= sqlite3WhereExprUsage(pMaskSet, pS->pHaving);
878 if( ALWAYS(pSrc!=0) ){
879 int i;
880 for(i=0; i<pSrc->nSrc; i++){
881 mask |= exprSelectUsage(pMaskSet, pSrc->a[i].pSelect);
882 mask |= sqlite3WhereExprUsage(pMaskSet, pSrc->a[i].pOn);
drh33f763d2018-01-26 22:41:59 +0000883 if( pSrc->a[i].fg.isTabFunc ){
884 mask |= sqlite3WhereExprListUsage(pMaskSet, pSrc->a[i].u1.pFuncArg);
885 }
drh6c1f4ef2015-06-08 14:23:15 +0000886 }
887 }
888 pS = pS->pPrior;
889 }
890 return mask;
891}
892
893/*
drh47991422015-08-31 15:58:06 +0000894** Expression pExpr is one operand of a comparison operator that might
895** be useful for indexing. This routine checks to see if pExpr appears
896** in any index. Return TRUE (1) if pExpr is an indexed term and return
drhe97c9ff2017-04-11 18:06:48 +0000897** FALSE (0) if not. If TRUE is returned, also set aiCurCol[0] to the cursor
898** number of the table that is indexed and aiCurCol[1] to the column number
drh8d25cb92016-08-19 19:58:06 +0000899** of the column that is indexed, or XN_EXPR (-2) if an expression is being
900** indexed.
drh47991422015-08-31 15:58:06 +0000901**
902** If pExpr is a TK_COLUMN column reference, then this routine always returns
903** true even if that particular column is not indexed, because the column
904** might be added to an automatic index later.
905*/
drhe97c9ff2017-04-11 18:06:48 +0000906static SQLITE_NOINLINE int exprMightBeIndexed2(
drh47991422015-08-31 15:58:06 +0000907 SrcList *pFrom, /* The FROM clause */
908 Bitmask mPrereq, /* Bitmask of FROM clause terms referenced by pExpr */
drhe97c9ff2017-04-11 18:06:48 +0000909 int *aiCurCol, /* Write the referenced table cursor and column here */
910 Expr *pExpr /* An operand of a comparison operator */
drh47991422015-08-31 15:58:06 +0000911){
912 Index *pIdx;
913 int i;
914 int iCur;
drhe97c9ff2017-04-11 18:06:48 +0000915 for(i=0; mPrereq>1; i++, mPrereq>>=1){}
916 iCur = pFrom->a[i].iCursor;
917 for(pIdx=pFrom->a[i].pTab->pIndex; pIdx; pIdx=pIdx->pNext){
918 if( pIdx->aColExpr==0 ) continue;
919 for(i=0; i<pIdx->nKeyCol; i++){
920 if( pIdx->aiColumn[i]!=XN_EXPR ) continue;
921 if( sqlite3ExprCompareSkip(pExpr, pIdx->aColExpr->a[i].pExpr, iCur)==0 ){
922 aiCurCol[0] = iCur;
923 aiCurCol[1] = XN_EXPR;
924 return 1;
925 }
926 }
927 }
928 return 0;
929}
930static int exprMightBeIndexed(
931 SrcList *pFrom, /* The FROM clause */
932 Bitmask mPrereq, /* Bitmask of FROM clause terms referenced by pExpr */
933 int *aiCurCol, /* Write the referenced table cursor & column here */
934 Expr *pExpr, /* An operand of a comparison operator */
935 int op /* The specific comparison operator */
936){
dan71c57db2016-07-09 20:23:55 +0000937 /* If this expression is a vector to the left or right of a
938 ** inequality constraint (>, <, >= or <=), perform the processing
939 ** on the first element of the vector. */
940 assert( TK_GT+1==TK_LE && TK_GT+2==TK_LT && TK_GT+3==TK_GE );
drh64bcb8c2016-08-26 03:42:57 +0000941 assert( TK_IS<TK_GE && TK_ISNULL<TK_GE && TK_IN<TK_GE );
942 assert( op<=TK_GE );
943 if( pExpr->op==TK_VECTOR && (op>=TK_GT && ALWAYS(op<=TK_GE)) ){
dan71c57db2016-07-09 20:23:55 +0000944 pExpr = pExpr->x.pList->a[0].pExpr;
945 }
946
drh47991422015-08-31 15:58:06 +0000947 if( pExpr->op==TK_COLUMN ){
drhe97c9ff2017-04-11 18:06:48 +0000948 aiCurCol[0] = pExpr->iTable;
949 aiCurCol[1] = pExpr->iColumn;
drh47991422015-08-31 15:58:06 +0000950 return 1;
951 }
952 if( mPrereq==0 ) return 0; /* No table references */
953 if( (mPrereq&(mPrereq-1))!=0 ) return 0; /* Refs more than one table */
drhe97c9ff2017-04-11 18:06:48 +0000954 return exprMightBeIndexed2(pFrom,mPrereq,aiCurCol,pExpr);
drh47991422015-08-31 15:58:06 +0000955}
956
dan870a0702016-08-01 16:37:43 +0000957/*
drh6c1f4ef2015-06-08 14:23:15 +0000958** The input to this routine is an WhereTerm structure with only the
959** "pExpr" field filled in. The job of this routine is to analyze the
960** subexpression and populate all the other fields of the WhereTerm
961** structure.
962**
963** If the expression is of the form "<expr> <op> X" it gets commuted
964** to the standard form of "X <op> <expr>".
965**
966** If the expression is of the form "X <op> Y" where both X and Y are
967** columns, then the original expression is unchanged and a new virtual
968** term of the form "Y <op> X" is added to the WHERE clause and
969** analyzed separately. The original term is marked with TERM_COPIED
970** and the new term is marked with TERM_DYNAMIC (because it's pExpr
971** needs to be freed with the WhereClause) and TERM_VIRTUAL (because it
972** is a commuted copy of a prior term.) The original term has nChild=1
973** and the copy has idxParent set to the index of the original term.
974*/
975static void exprAnalyze(
976 SrcList *pSrc, /* the FROM clause */
977 WhereClause *pWC, /* the WHERE clause */
978 int idxTerm /* Index of the term to be analyzed */
979){
980 WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */
981 WhereTerm *pTerm; /* The term to be analyzed */
982 WhereMaskSet *pMaskSet; /* Set of table index masks */
983 Expr *pExpr; /* The expression to be analyzed */
984 Bitmask prereqLeft; /* Prerequesites of the pExpr->pLeft */
985 Bitmask prereqAll; /* Prerequesites of pExpr */
986 Bitmask extraRight = 0; /* Extra dependencies on LEFT JOIN */
987 Expr *pStr1 = 0; /* RHS of LIKE/GLOB operator */
988 int isComplete = 0; /* RHS of LIKE/GLOB ends with wildcard */
989 int noCase = 0; /* uppercase equivalent to lowercase */
990 int op; /* Top-level operator. pExpr->op */
991 Parse *pParse = pWInfo->pParse; /* Parsing context */
992 sqlite3 *db = pParse->db; /* Database connection */
mistachkin53be36b2017-11-03 06:45:37 +0000993 unsigned char eOp2 = 0; /* op2 value for LIKE/REGEXP/GLOB */
drhd9bcb322017-01-10 15:08:06 +0000994 int nLeft; /* Number of elements on left side vector */
drh6c1f4ef2015-06-08 14:23:15 +0000995
996 if( db->mallocFailed ){
997 return;
998 }
999 pTerm = &pWC->a[idxTerm];
1000 pMaskSet = &pWInfo->sMaskSet;
1001 pExpr = pTerm->pExpr;
1002 assert( pExpr->op!=TK_AS && pExpr->op!=TK_COLLATE );
1003 prereqLeft = sqlite3WhereExprUsage(pMaskSet, pExpr->pLeft);
1004 op = pExpr->op;
1005 if( op==TK_IN ){
1006 assert( pExpr->pRight==0 );
dan7b35a772016-07-28 19:47:15 +00001007 if( sqlite3ExprCheckIN(pParse, pExpr) ) return;
drh6c1f4ef2015-06-08 14:23:15 +00001008 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
1009 pTerm->prereqRight = exprSelectUsage(pMaskSet, pExpr->x.pSelect);
1010 }else{
1011 pTerm->prereqRight = sqlite3WhereExprListUsage(pMaskSet, pExpr->x.pList);
1012 }
1013 }else if( op==TK_ISNULL ){
1014 pTerm->prereqRight = 0;
1015 }else{
1016 pTerm->prereqRight = sqlite3WhereExprUsage(pMaskSet, pExpr->pRight);
1017 }
dand3930b12017-07-10 15:17:30 +00001018 pMaskSet->bVarSelect = 0;
drhccf6db52018-06-09 02:49:11 +00001019 prereqAll = sqlite3WhereExprUsageNN(pMaskSet, pExpr);
dand3930b12017-07-10 15:17:30 +00001020 if( pMaskSet->bVarSelect ) pTerm->wtFlags |= TERM_VARSELECT;
drh6c1f4ef2015-06-08 14:23:15 +00001021 if( ExprHasProperty(pExpr, EP_FromJoin) ){
1022 Bitmask x = sqlite3WhereGetMask(pMaskSet, pExpr->iRightJoinTable);
1023 prereqAll |= x;
1024 extraRight = x-1; /* ON clause terms may not be used with an index
1025 ** on left table of a LEFT JOIN. Ticket #3015 */
drh8e36ddd2017-01-10 17:33:43 +00001026 if( (prereqAll>>1)>=x ){
1027 sqlite3ErrorMsg(pParse, "ON clause references tables to its right");
1028 return;
1029 }
drh6c1f4ef2015-06-08 14:23:15 +00001030 }
1031 pTerm->prereqAll = prereqAll;
1032 pTerm->leftCursor = -1;
1033 pTerm->iParent = -1;
1034 pTerm->eOperator = 0;
1035 if( allowedOp(op) ){
drhe97c9ff2017-04-11 18:06:48 +00001036 int aiCurCol[2];
drh6c1f4ef2015-06-08 14:23:15 +00001037 Expr *pLeft = sqlite3ExprSkipCollate(pExpr->pLeft);
1038 Expr *pRight = sqlite3ExprSkipCollate(pExpr->pRight);
1039 u16 opMask = (pTerm->prereqRight & prereqLeft)==0 ? WO_ALL : WO_EQUIV;
dan8da209b2016-07-26 18:06:08 +00001040
dan145b4ea2016-07-29 18:12:12 +00001041 if( pTerm->iField>0 ){
1042 assert( op==TK_IN );
dan8da209b2016-07-26 18:06:08 +00001043 assert( pLeft->op==TK_VECTOR );
1044 pLeft = pLeft->x.pList->a[pTerm->iField-1].pExpr;
1045 }
1046
drhe97c9ff2017-04-11 18:06:48 +00001047 if( exprMightBeIndexed(pSrc, prereqLeft, aiCurCol, pLeft, op) ){
1048 pTerm->leftCursor = aiCurCol[0];
1049 pTerm->u.leftColumn = aiCurCol[1];
drh6860e6f2015-08-27 18:24:02 +00001050 pTerm->eOperator = operatorMask(op) & opMask;
drh6c1f4ef2015-06-08 14:23:15 +00001051 }
1052 if( op==TK_IS ) pTerm->wtFlags |= TERM_IS;
drh47991422015-08-31 15:58:06 +00001053 if( pRight
drhe97c9ff2017-04-11 18:06:48 +00001054 && exprMightBeIndexed(pSrc, pTerm->prereqRight, aiCurCol, pRight, op)
drh47991422015-08-31 15:58:06 +00001055 ){
drh6c1f4ef2015-06-08 14:23:15 +00001056 WhereTerm *pNew;
1057 Expr *pDup;
1058 u16 eExtraOp = 0; /* Extra bits for pNew->eOperator */
dan145b4ea2016-07-29 18:12:12 +00001059 assert( pTerm->iField==0 );
drh6c1f4ef2015-06-08 14:23:15 +00001060 if( pTerm->leftCursor>=0 ){
1061 int idxNew;
1062 pDup = sqlite3ExprDup(db, pExpr, 0);
1063 if( db->mallocFailed ){
1064 sqlite3ExprDelete(db, pDup);
1065 return;
1066 }
1067 idxNew = whereClauseInsert(pWC, pDup, TERM_VIRTUAL|TERM_DYNAMIC);
1068 if( idxNew==0 ) return;
1069 pNew = &pWC->a[idxNew];
1070 markTermAsChild(pWC, idxNew, idxTerm);
1071 if( op==TK_IS ) pNew->wtFlags |= TERM_IS;
1072 pTerm = &pWC->a[idxTerm];
1073 pTerm->wtFlags |= TERM_COPIED;
1074
1075 if( termIsEquivalence(pParse, pDup) ){
1076 pTerm->eOperator |= WO_EQUIV;
1077 eExtraOp = WO_EQUIV;
1078 }
1079 }else{
1080 pDup = pExpr;
1081 pNew = pTerm;
1082 }
1083 exprCommute(pParse, pDup);
drhe97c9ff2017-04-11 18:06:48 +00001084 pNew->leftCursor = aiCurCol[0];
1085 pNew->u.leftColumn = aiCurCol[1];
drh6c1f4ef2015-06-08 14:23:15 +00001086 testcase( (prereqLeft | extraRight) != prereqLeft );
1087 pNew->prereqRight = prereqLeft | extraRight;
1088 pNew->prereqAll = prereqAll;
1089 pNew->eOperator = (operatorMask(pDup->op) + eExtraOp) & opMask;
1090 }
1091 }
1092
1093#ifndef SQLITE_OMIT_BETWEEN_OPTIMIZATION
1094 /* If a term is the BETWEEN operator, create two new virtual terms
1095 ** that define the range that the BETWEEN implements. For example:
1096 **
1097 ** a BETWEEN b AND c
1098 **
1099 ** is converted into:
1100 **
1101 ** (a BETWEEN b AND c) AND (a>=b) AND (a<=c)
1102 **
1103 ** The two new terms are added onto the end of the WhereClause object.
1104 ** The new terms are "dynamic" and are children of the original BETWEEN
1105 ** term. That means that if the BETWEEN term is coded, the children are
1106 ** skipped. Or, if the children are satisfied by an index, the original
1107 ** BETWEEN term is skipped.
1108 */
1109 else if( pExpr->op==TK_BETWEEN && pWC->op==TK_AND ){
1110 ExprList *pList = pExpr->x.pList;
1111 int i;
1112 static const u8 ops[] = {TK_GE, TK_LE};
1113 assert( pList!=0 );
1114 assert( pList->nExpr==2 );
1115 for(i=0; i<2; i++){
1116 Expr *pNewExpr;
1117 int idxNew;
1118 pNewExpr = sqlite3PExpr(pParse, ops[i],
1119 sqlite3ExprDup(db, pExpr->pLeft, 0),
drhabfd35e2016-12-06 22:47:23 +00001120 sqlite3ExprDup(db, pList->a[i].pExpr, 0));
drh6c1f4ef2015-06-08 14:23:15 +00001121 transferJoinMarkings(pNewExpr, pExpr);
1122 idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
1123 testcase( idxNew==0 );
1124 exprAnalyze(pSrc, pWC, idxNew);
1125 pTerm = &pWC->a[idxTerm];
1126 markTermAsChild(pWC, idxNew, idxTerm);
1127 }
1128 }
1129#endif /* SQLITE_OMIT_BETWEEN_OPTIMIZATION */
1130
1131#if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
1132 /* Analyze a term that is composed of two or more subterms connected by
1133 ** an OR operator.
1134 */
1135 else if( pExpr->op==TK_OR ){
1136 assert( pWC->op==TK_AND );
1137 exprAnalyzeOrTerm(pSrc, pWC, idxTerm);
1138 pTerm = &pWC->a[idxTerm];
1139 }
1140#endif /* SQLITE_OMIT_OR_OPTIMIZATION */
1141
1142#ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
1143 /* Add constraints to reduce the search space on a LIKE or GLOB
1144 ** operator.
1145 **
1146 ** A like pattern of the form "x LIKE 'aBc%'" is changed into constraints
1147 **
1148 ** x>='ABC' AND x<'abd' AND x LIKE 'aBc%'
1149 **
1150 ** The last character of the prefix "abc" is incremented to form the
1151 ** termination condition "abd". If case is not significant (the default
1152 ** for LIKE) then the lower-bound is made all uppercase and the upper-
1153 ** bound is made all lowercase so that the bounds also work when comparing
1154 ** BLOBs.
1155 */
1156 if( pWC->op==TK_AND
1157 && isLikeOrGlob(pParse, pExpr, &pStr1, &isComplete, &noCase)
1158 ){
1159 Expr *pLeft; /* LHS of LIKE/GLOB operator */
1160 Expr *pStr2; /* Copy of pStr1 - RHS of LIKE/GLOB operator */
1161 Expr *pNewExpr1;
1162 Expr *pNewExpr2;
1163 int idxNew1;
1164 int idxNew2;
1165 const char *zCollSeqName; /* Name of collating sequence */
1166 const u16 wtFlags = TERM_LIKEOPT | TERM_VIRTUAL | TERM_DYNAMIC;
1167
1168 pLeft = pExpr->x.pList->a[1].pExpr;
1169 pStr2 = sqlite3ExprDup(db, pStr1, 0);
1170
1171 /* Convert the lower bound to upper-case and the upper bound to
1172 ** lower-case (upper-case is less than lower-case in ASCII) so that
1173 ** the range constraints also work for BLOBs
1174 */
1175 if( noCase && !pParse->db->mallocFailed ){
1176 int i;
1177 char c;
1178 pTerm->wtFlags |= TERM_LIKE;
1179 for(i=0; (c = pStr1->u.zToken[i])!=0; i++){
1180 pStr1->u.zToken[i] = sqlite3Toupper(c);
1181 pStr2->u.zToken[i] = sqlite3Tolower(c);
1182 }
1183 }
1184
1185 if( !db->mallocFailed ){
1186 u8 c, *pC; /* Last character before the first wildcard */
1187 pC = (u8*)&pStr2->u.zToken[sqlite3Strlen30(pStr2->u.zToken)-1];
1188 c = *pC;
1189 if( noCase ){
1190 /* The point is to increment the last character before the first
1191 ** wildcard. But if we increment '@', that will push it into the
1192 ** alphabetic range where case conversions will mess up the
1193 ** inequality. To avoid this, make sure to also run the full
1194 ** LIKE on all candidate expressions by clearing the isComplete flag
1195 */
1196 if( c=='A'-1 ) isComplete = 0;
1197 c = sqlite3UpperToLower[c];
1198 }
1199 *pC = c + 1;
1200 }
1201 zCollSeqName = noCase ? "NOCASE" : "BINARY";
1202 pNewExpr1 = sqlite3ExprDup(db, pLeft, 0);
1203 pNewExpr1 = sqlite3PExpr(pParse, TK_GE,
1204 sqlite3ExprAddCollateString(pParse,pNewExpr1,zCollSeqName),
drhabfd35e2016-12-06 22:47:23 +00001205 pStr1);
drh6c1f4ef2015-06-08 14:23:15 +00001206 transferJoinMarkings(pNewExpr1, pExpr);
1207 idxNew1 = whereClauseInsert(pWC, pNewExpr1, wtFlags);
1208 testcase( idxNew1==0 );
1209 exprAnalyze(pSrc, pWC, idxNew1);
1210 pNewExpr2 = sqlite3ExprDup(db, pLeft, 0);
1211 pNewExpr2 = sqlite3PExpr(pParse, TK_LT,
1212 sqlite3ExprAddCollateString(pParse,pNewExpr2,zCollSeqName),
drhabfd35e2016-12-06 22:47:23 +00001213 pStr2);
drh6c1f4ef2015-06-08 14:23:15 +00001214 transferJoinMarkings(pNewExpr2, pExpr);
1215 idxNew2 = whereClauseInsert(pWC, pNewExpr2, wtFlags);
1216 testcase( idxNew2==0 );
1217 exprAnalyze(pSrc, pWC, idxNew2);
1218 pTerm = &pWC->a[idxTerm];
1219 if( isComplete ){
1220 markTermAsChild(pWC, idxNew1, idxTerm);
1221 markTermAsChild(pWC, idxNew2, idxTerm);
1222 }
1223 }
1224#endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
1225
1226#ifndef SQLITE_OMIT_VIRTUALTABLE
drh303a69b2017-09-11 19:47:37 +00001227 /* Add a WO_AUX auxiliary term to the constraint set if the
1228 ** current expression is of the form "column OP expr" where OP
1229 ** is an operator that gets passed into virtual tables but which is
1230 ** not normally optimized for ordinary tables. In other words, OP
1231 ** is one of MATCH, LIKE, GLOB, REGEXP, !=, IS, IS NOT, or NOT NULL.
drh6c1f4ef2015-06-08 14:23:15 +00001232 ** This information is used by the xBestIndex methods of
1233 ** virtual tables. The native query optimizer does not attempt
1234 ** to do anything with MATCH functions.
1235 */
dand03024d2017-09-09 19:41:12 +00001236 if( pWC->op==TK_AND ){
mistachkin53be36b2017-11-03 06:45:37 +00001237 Expr *pRight = 0, *pLeft = 0;
drh303a69b2017-09-11 19:47:37 +00001238 int res = isAuxiliaryVtabOperator(pExpr, &eOp2, &pLeft, &pRight);
1239 while( res-- > 0 ){
dand03024d2017-09-09 19:41:12 +00001240 int idxNew;
1241 WhereTerm *pNewTerm;
1242 Bitmask prereqColumn, prereqExpr;
drh6c1f4ef2015-06-08 14:23:15 +00001243
dand03024d2017-09-09 19:41:12 +00001244 prereqExpr = sqlite3WhereExprUsage(pMaskSet, pRight);
1245 prereqColumn = sqlite3WhereExprUsage(pMaskSet, pLeft);
1246 if( (prereqExpr & prereqColumn)==0 ){
1247 Expr *pNewExpr;
1248 pNewExpr = sqlite3PExpr(pParse, TK_MATCH,
1249 0, sqlite3ExprDup(db, pRight, 0));
1250 if( ExprHasProperty(pExpr, EP_FromJoin) && pNewExpr ){
1251 ExprSetProperty(pNewExpr, EP_FromJoin);
1252 }
1253 idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
1254 testcase( idxNew==0 );
1255 pNewTerm = &pWC->a[idxNew];
1256 pNewTerm->prereqRight = prereqExpr;
1257 pNewTerm->leftCursor = pLeft->iTable;
1258 pNewTerm->u.leftColumn = pLeft->iColumn;
drh303a69b2017-09-11 19:47:37 +00001259 pNewTerm->eOperator = WO_AUX;
dand03024d2017-09-09 19:41:12 +00001260 pNewTerm->eMatchOp = eOp2;
1261 markTermAsChild(pWC, idxNew, idxTerm);
1262 pTerm = &pWC->a[idxTerm];
1263 pTerm->wtFlags |= TERM_COPIED;
1264 pNewTerm->prereqAll = pTerm->prereqAll;
dan210ec4c2017-06-27 16:39:01 +00001265 }
dand03024d2017-09-09 19:41:12 +00001266 SWAP(Expr*, pLeft, pRight);
drh6c1f4ef2015-06-08 14:23:15 +00001267 }
1268 }
1269#endif /* SQLITE_OMIT_VIRTUALTABLE */
1270
dan95a08c02016-08-02 16:18:35 +00001271 /* If there is a vector == or IS term - e.g. "(a, b) == (?, ?)" - create
drh9e730f02016-08-20 12:00:05 +00001272 ** new terms for each component comparison - "a = ?" and "b = ?". The
1273 ** new terms completely replace the original vector comparison, which is
1274 ** no longer used.
1275 **
dan95a08c02016-08-02 16:18:35 +00001276 ** This is only required if at least one side of the comparison operation
1277 ** is not a sub-select. */
dan71c57db2016-07-09 20:23:55 +00001278 if( pWC->op==TK_AND
1279 && (pExpr->op==TK_EQ || pExpr->op==TK_IS)
drhd9bcb322017-01-10 15:08:06 +00001280 && (nLeft = sqlite3ExprVectorSize(pExpr->pLeft))>1
1281 && sqlite3ExprVectorSize(pExpr->pRight)==nLeft
dan71c57db2016-07-09 20:23:55 +00001282 && ( (pExpr->pLeft->flags & EP_xIsSelect)==0
drhd9bcb322017-01-10 15:08:06 +00001283 || (pExpr->pRight->flags & EP_xIsSelect)==0)
1284 ){
drhb29e60c2016-09-05 12:02:34 +00001285 int i;
drhb29e60c2016-09-05 12:02:34 +00001286 for(i=0; i<nLeft; i++){
1287 int idxNew;
1288 Expr *pNew;
1289 Expr *pLeft = sqlite3ExprForVectorField(pParse, pExpr->pLeft, i);
1290 Expr *pRight = sqlite3ExprForVectorField(pParse, pExpr->pRight, i);
dan71c57db2016-07-09 20:23:55 +00001291
drhabfd35e2016-12-06 22:47:23 +00001292 pNew = sqlite3PExpr(pParse, pExpr->op, pLeft, pRight);
drhc52496f2016-10-27 01:02:20 +00001293 transferJoinMarkings(pNew, pExpr);
drhb29e60c2016-09-05 12:02:34 +00001294 idxNew = whereClauseInsert(pWC, pNew, TERM_DYNAMIC);
1295 exprAnalyze(pSrc, pWC, idxNew);
dan71c57db2016-07-09 20:23:55 +00001296 }
drhb29e60c2016-09-05 12:02:34 +00001297 pTerm = &pWC->a[idxTerm];
drhe28eb642018-02-18 17:50:03 +00001298 pTerm->wtFlags |= TERM_CODED|TERM_VIRTUAL; /* Disable the original */
drhb29e60c2016-09-05 12:02:34 +00001299 pTerm->eOperator = 0;
dan71c57db2016-07-09 20:23:55 +00001300 }
1301
dan95a08c02016-08-02 16:18:35 +00001302 /* If there is a vector IN term - e.g. "(a, b) IN (SELECT ...)" - create
1303 ** a virtual term for each vector component. The expression object
1304 ** used by each such virtual term is pExpr (the full vector IN(...)
1305 ** expression). The WhereTerm.iField variable identifies the index within
drh14318072016-09-06 18:51:25 +00001306 ** the vector on the LHS that the virtual term represents.
1307 **
1308 ** This only works if the RHS is a simple SELECT, not a compound
1309 */
dan8da209b2016-07-26 18:06:08 +00001310 if( pWC->op==TK_AND && pExpr->op==TK_IN && pTerm->iField==0
1311 && pExpr->pLeft->op==TK_VECTOR
drh14318072016-09-06 18:51:25 +00001312 && pExpr->x.pSelect->pPrior==0
dan8da209b2016-07-26 18:06:08 +00001313 ){
1314 int i;
1315 for(i=0; i<sqlite3ExprVectorSize(pExpr->pLeft); i++){
1316 int idxNew;
1317 idxNew = whereClauseInsert(pWC, pExpr, TERM_VIRTUAL);
1318 pWC->a[idxNew].iField = i+1;
1319 exprAnalyze(pSrc, pWC, idxNew);
1320 markTermAsChild(pWC, idxNew, idxTerm);
1321 }
1322 }
1323
drh6c1f4ef2015-06-08 14:23:15 +00001324#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
1325 /* When sqlite_stat3 histogram data is available an operator of the
1326 ** form "x IS NOT NULL" can sometimes be evaluated more efficiently
1327 ** as "x>NULL" if x is not an INTEGER PRIMARY KEY. So construct a
1328 ** virtual term of that form.
1329 **
1330 ** Note that the virtual term must be tagged with TERM_VNULL.
1331 */
1332 if( pExpr->op==TK_NOTNULL
1333 && pExpr->pLeft->op==TK_COLUMN
1334 && pExpr->pLeft->iColumn>=0
1335 && OptimizationEnabled(db, SQLITE_Stat34)
1336 ){
1337 Expr *pNewExpr;
1338 Expr *pLeft = pExpr->pLeft;
1339 int idxNew;
1340 WhereTerm *pNewTerm;
1341
1342 pNewExpr = sqlite3PExpr(pParse, TK_GT,
1343 sqlite3ExprDup(db, pLeft, 0),
drhabfd35e2016-12-06 22:47:23 +00001344 sqlite3ExprAlloc(db, TK_NULL, 0, 0));
drh6c1f4ef2015-06-08 14:23:15 +00001345
1346 idxNew = whereClauseInsert(pWC, pNewExpr,
1347 TERM_VIRTUAL|TERM_DYNAMIC|TERM_VNULL);
1348 if( idxNew ){
1349 pNewTerm = &pWC->a[idxNew];
1350 pNewTerm->prereqRight = 0;
1351 pNewTerm->leftCursor = pLeft->iTable;
1352 pNewTerm->u.leftColumn = pLeft->iColumn;
1353 pNewTerm->eOperator = WO_GT;
1354 markTermAsChild(pWC, idxNew, idxTerm);
1355 pTerm = &pWC->a[idxTerm];
1356 pTerm->wtFlags |= TERM_COPIED;
1357 pNewTerm->prereqAll = pTerm->prereqAll;
1358 }
1359 }
1360#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
1361
1362 /* Prevent ON clause terms of a LEFT JOIN from being used to drive
1363 ** an index for tables to the left of the join.
1364 */
drh0f85b2f2016-11-20 12:00:27 +00001365 testcase( pTerm!=&pWC->a[idxTerm] );
1366 pTerm = &pWC->a[idxTerm];
drh6c1f4ef2015-06-08 14:23:15 +00001367 pTerm->prereqRight |= extraRight;
1368}
1369
1370/***************************************************************************
1371** Routines with file scope above. Interface to the rest of the where.c
1372** subsystem follows.
1373***************************************************************************/
1374
1375/*
1376** This routine identifies subexpressions in the WHERE clause where
1377** each subexpression is separated by the AND operator or some other
1378** operator specified in the op parameter. The WhereClause structure
1379** is filled with pointers to subexpressions. For example:
1380**
1381** WHERE a=='hello' AND coalesce(b,11)<10 AND (c+12!=d OR c==22)
1382** \________/ \_______________/ \________________/
1383** slot[0] slot[1] slot[2]
1384**
1385** The original WHERE clause in pExpr is unaltered. All this routine
1386** does is make slot[] entries point to substructure within pExpr.
1387**
1388** In the previous sentence and in the diagram, "slot[]" refers to
1389** the WhereClause.a[] array. The slot[] array grows as needed to contain
1390** all terms of the WHERE clause.
1391*/
1392void sqlite3WhereSplit(WhereClause *pWC, Expr *pExpr, u8 op){
1393 Expr *pE2 = sqlite3ExprSkipCollate(pExpr);
1394 pWC->op = op;
1395 if( pE2==0 ) return;
1396 if( pE2->op!=op ){
1397 whereClauseInsert(pWC, pExpr, 0);
1398 }else{
1399 sqlite3WhereSplit(pWC, pE2->pLeft, op);
1400 sqlite3WhereSplit(pWC, pE2->pRight, op);
1401 }
1402}
1403
1404/*
1405** Initialize a preallocated WhereClause structure.
1406*/
1407void sqlite3WhereClauseInit(
1408 WhereClause *pWC, /* The WhereClause to be initialized */
1409 WhereInfo *pWInfo /* The WHERE processing context */
1410){
1411 pWC->pWInfo = pWInfo;
drh9c3549a2018-06-11 01:30:03 +00001412 pWC->hasOr = 0;
drh6c1f4ef2015-06-08 14:23:15 +00001413 pWC->pOuter = 0;
1414 pWC->nTerm = 0;
1415 pWC->nSlot = ArraySize(pWC->aStatic);
1416 pWC->a = pWC->aStatic;
1417}
1418
1419/*
1420** Deallocate a WhereClause structure. The WhereClause structure
drh62aaa6c2015-11-21 17:27:42 +00001421** itself is not freed. This routine is the inverse of
1422** sqlite3WhereClauseInit().
drh6c1f4ef2015-06-08 14:23:15 +00001423*/
1424void sqlite3WhereClauseClear(WhereClause *pWC){
1425 int i;
1426 WhereTerm *a;
1427 sqlite3 *db = pWC->pWInfo->pParse->db;
1428 for(i=pWC->nTerm-1, a=pWC->a; i>=0; i--, a++){
1429 if( a->wtFlags & TERM_DYNAMIC ){
1430 sqlite3ExprDelete(db, a->pExpr);
1431 }
1432 if( a->wtFlags & TERM_ORINFO ){
1433 whereOrInfoDelete(db, a->u.pOrInfo);
1434 }else if( a->wtFlags & TERM_ANDINFO ){
1435 whereAndInfoDelete(db, a->u.pAndInfo);
1436 }
1437 }
1438 if( pWC->a!=pWC->aStatic ){
1439 sqlite3DbFree(db, pWC->a);
1440 }
1441}
1442
1443
1444/*
1445** These routines walk (recursively) an expression tree and generate
1446** a bitmask indicating which tables are used in that expression
1447** tree.
1448*/
drhccf6db52018-06-09 02:49:11 +00001449Bitmask sqlite3WhereExprUsageNN(WhereMaskSet *pMaskSet, Expr *p){
drh93ca3932016-08-10 20:02:21 +00001450 Bitmask mask;
drh6c1f4ef2015-06-08 14:23:15 +00001451 if( p->op==TK_COLUMN ){
drhf43ce0b2017-05-25 00:08:48 +00001452 return sqlite3WhereGetMask(pMaskSet, p->iTable);
drhccf6db52018-06-09 02:49:11 +00001453 }else if( ExprHasProperty(p, EP_TokenOnly|EP_Leaf) ){
1454 assert( p->op!=TK_IF_NULL_ROW );
1455 return 0;
drh6c1f4ef2015-06-08 14:23:15 +00001456 }
drhf43ce0b2017-05-25 00:08:48 +00001457 mask = (p->op==TK_IF_NULL_ROW) ? sqlite3WhereGetMask(pMaskSet, p->iTable) : 0;
drhccf6db52018-06-09 02:49:11 +00001458 if( p->pLeft ) mask |= sqlite3WhereExprUsageNN(pMaskSet, p->pLeft);
drhe24b92b2017-07-10 15:26:09 +00001459 if( p->pRight ){
drhccf6db52018-06-09 02:49:11 +00001460 mask |= sqlite3WhereExprUsageNN(pMaskSet, p->pRight);
drhe24b92b2017-07-10 15:26:09 +00001461 assert( p->x.pList==0 );
1462 }else if( ExprHasProperty(p, EP_xIsSelect) ){
dand3930b12017-07-10 15:17:30 +00001463 if( ExprHasProperty(p, EP_VarSelect) ) pMaskSet->bVarSelect = 1;
drh6c1f4ef2015-06-08 14:23:15 +00001464 mask |= exprSelectUsage(pMaskSet, p->x.pSelect);
drh926957f2016-04-12 00:00:33 +00001465 }else if( p->x.pList ){
drh6c1f4ef2015-06-08 14:23:15 +00001466 mask |= sqlite3WhereExprListUsage(pMaskSet, p->x.pList);
1467 }
1468 return mask;
1469}
drhccf6db52018-06-09 02:49:11 +00001470Bitmask sqlite3WhereExprUsage(WhereMaskSet *pMaskSet, Expr *p){
1471 return p ? sqlite3WhereExprUsageNN(pMaskSet,p) : 0;
1472}
drh6c1f4ef2015-06-08 14:23:15 +00001473Bitmask sqlite3WhereExprListUsage(WhereMaskSet *pMaskSet, ExprList *pList){
1474 int i;
1475 Bitmask mask = 0;
1476 if( pList ){
1477 for(i=0; i<pList->nExpr; i++){
1478 mask |= sqlite3WhereExprUsage(pMaskSet, pList->a[i].pExpr);
1479 }
1480 }
1481 return mask;
1482}
1483
1484
1485/*
1486** Call exprAnalyze on all terms in a WHERE clause.
1487**
1488** Note that exprAnalyze() might add new virtual terms onto the
1489** end of the WHERE clause. We do not want to analyze these new
1490** virtual terms, so start analyzing at the end and work forward
1491** so that the added virtual terms are never processed.
1492*/
1493void sqlite3WhereExprAnalyze(
1494 SrcList *pTabList, /* the FROM clause */
1495 WhereClause *pWC /* the WHERE clause to be analyzed */
1496){
1497 int i;
1498 for(i=pWC->nTerm-1; i>=0; i--){
1499 exprAnalyze(pTabList, pWC, i);
1500 }
1501}
drh01d230c2015-08-19 17:11:37 +00001502
1503/*
1504** For table-valued-functions, transform the function arguments into
1505** new WHERE clause terms.
1506**
1507** Each function argument translates into an equality constraint against
1508** a HIDDEN column in the table.
1509*/
1510void sqlite3WhereTabFuncArgs(
1511 Parse *pParse, /* Parsing context */
1512 struct SrcList_item *pItem, /* The FROM clause term to process */
1513 WhereClause *pWC /* Xfer function arguments to here */
1514){
1515 Table *pTab;
1516 int j, k;
1517 ExprList *pArgs;
1518 Expr *pColRef;
1519 Expr *pTerm;
1520 if( pItem->fg.isTabFunc==0 ) return;
1521 pTab = pItem->pTab;
1522 assert( pTab!=0 );
1523 pArgs = pItem->u1.pFuncArg;
drh20292312015-11-21 13:24:46 +00001524 if( pArgs==0 ) return;
drh01d230c2015-08-19 17:11:37 +00001525 for(j=k=0; j<pArgs->nExpr; j++){
drh62aaa6c2015-11-21 17:27:42 +00001526 while( k<pTab->nCol && (pTab->aCol[k].colFlags & COLFLAG_HIDDEN)==0 ){k++;}
drh01d230c2015-08-19 17:11:37 +00001527 if( k>=pTab->nCol ){
drhd8b1bfc2015-08-20 23:21:34 +00001528 sqlite3ErrorMsg(pParse, "too many arguments on %s() - max %d",
drh01d230c2015-08-19 17:11:37 +00001529 pTab->zName, j);
1530 return;
1531 }
drhe1c03b62016-09-23 20:59:31 +00001532 pColRef = sqlite3ExprAlloc(pParse->db, TK_COLUMN, 0, 0);
drh01d230c2015-08-19 17:11:37 +00001533 if( pColRef==0 ) return;
1534 pColRef->iTable = pItem->iCursor;
1535 pColRef->iColumn = k++;
drh1f2fc282015-08-21 17:14:48 +00001536 pColRef->pTab = pTab;
drh01d230c2015-08-19 17:11:37 +00001537 pTerm = sqlite3PExpr(pParse, TK_EQ, pColRef,
drhabfd35e2016-12-06 22:47:23 +00001538 sqlite3ExprDup(pParse->db, pArgs->a[j].pExpr, 0));
drh01d230c2015-08-19 17:11:37 +00001539 whereClauseInsert(pWC, pTerm, TERM_DYNAMIC);
1540 }
1541}