blob: 4bb161044e715878413f37a1c29efe1ba90498ff [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){
197 const char *z = 0; /* String on RHS of LIKE operator */
198 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 */
202 char wc[3]; /* Wildcard characters */
203 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;
216 if( pLeft->op!=TK_COLUMN
217 || sqlite3ExprAffinity(pLeft)!=SQLITE_AFF_TEXT
218 || IsVirtual(pLeft->pTab) /* Value might be numeric */
219 ){
220 /* IMP: R-02065-49465 The left-hand side of the LIKE or GLOB operator must
221 ** be the name of an indexed column with TEXT affinity. */
222 return 0;
223 }
224 assert( pLeft->iColumn!=(-1) ); /* Because IPK never has AFF_TEXT */
225
226 pRight = sqlite3ExprSkipCollate(pList->a[0].pExpr);
227 op = pRight->op;
228 if( op==TK_VARIABLE ){
229 Vdbe *pReprepare = pParse->pReprepare;
230 int iCol = pRight->iColumn;
231 pVal = sqlite3VdbeGetBoundValue(pReprepare, iCol, SQLITE_AFF_BLOB);
232 if( pVal && sqlite3_value_type(pVal)==SQLITE_TEXT ){
233 z = (char *)sqlite3_value_text(pVal);
234 }
235 sqlite3VdbeSetVarmask(pParse->pVdbe, iCol);
236 assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER );
237 }else if( op==TK_STRING ){
238 z = pRight->u.zToken;
239 }
240 if( z ){
241 cnt = 0;
242 while( (c=z[cnt])!=0 && c!=wc[0] && c!=wc[1] && c!=wc[2] ){
243 cnt++;
244 }
245 if( cnt!=0 && 255!=(u8)z[cnt-1] ){
246 Expr *pPrefix;
247 *pisComplete = c==wc[0] && z[cnt+1]==0;
248 pPrefix = sqlite3Expr(db, TK_STRING, z);
249 if( pPrefix ) pPrefix->u.zToken[cnt] = 0;
250 *ppPrefix = pPrefix;
251 if( op==TK_VARIABLE ){
252 Vdbe *v = pParse->pVdbe;
253 sqlite3VdbeSetVarmask(v, pRight->iColumn);
254 if( *pisComplete && pRight->u.zToken[1] ){
255 /* If the rhs of the LIKE expression is a variable, and the current
256 ** value of the variable means there is no need to invoke the LIKE
257 ** function, then no OP_Variable will be added to the program.
258 ** This causes problems for the sqlite3_bind_parameter_name()
259 ** API. To work around them, add a dummy OP_Variable here.
260 */
261 int r1 = sqlite3GetTempReg(pParse);
262 sqlite3ExprCodeTarget(pParse, pRight, r1);
263 sqlite3VdbeChangeP3(v, sqlite3VdbeCurrentAddr(v)-1, 0);
264 sqlite3ReleaseTempReg(pParse, r1);
265 }
266 }
267 }else{
268 z = 0;
269 }
270 }
271
drhb8763632016-01-19 17:54:21 +0000272 rc = (z!=0);
drh6c1f4ef2015-06-08 14:23:15 +0000273 sqlite3ValueFree(pVal);
drhb8763632016-01-19 17:54:21 +0000274 return rc;
drh6c1f4ef2015-06-08 14:23:15 +0000275}
276#endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
277
278
279#ifndef SQLITE_OMIT_VIRTUALTABLE
280/*
281** Check to see if the given expression is of the form
282**
dan43970dd2015-11-24 17:39:01 +0000283** column OP expr
284**
285** where OP is one of MATCH, GLOB, LIKE or REGEXP and "column" is a
286** column of a virtual table.
drh6c1f4ef2015-06-08 14:23:15 +0000287**
288** If it is then return TRUE. If not, return FALSE.
289*/
290static int isMatchOfColumn(
dan07bdba82015-11-23 21:09:54 +0000291 Expr *pExpr, /* Test this expression */
292 unsigned char *peOp2 /* OUT: 0 for MATCH, or else an op2 value */
drh6c1f4ef2015-06-08 14:23:15 +0000293){
drhe104dd32016-08-10 19:43:29 +0000294 static const struct Op2 {
dan07bdba82015-11-23 21:09:54 +0000295 const char *zOp;
296 unsigned char eOp2;
297 } aOp[] = {
dan43970dd2015-11-24 17:39:01 +0000298 { "match", SQLITE_INDEX_CONSTRAINT_MATCH },
299 { "glob", SQLITE_INDEX_CONSTRAINT_GLOB },
300 { "like", SQLITE_INDEX_CONSTRAINT_LIKE },
301 { "regexp", SQLITE_INDEX_CONSTRAINT_REGEXP }
dan07bdba82015-11-23 21:09:54 +0000302 };
drh6c1f4ef2015-06-08 14:23:15 +0000303 ExprList *pList;
dan43970dd2015-11-24 17:39:01 +0000304 Expr *pCol; /* Column reference */
dan07bdba82015-11-23 21:09:54 +0000305 int i;
drh6c1f4ef2015-06-08 14:23:15 +0000306
307 if( pExpr->op!=TK_FUNCTION ){
308 return 0;
309 }
drh6c1f4ef2015-06-08 14:23:15 +0000310 pList = pExpr->x.pList;
danff7b22b2015-11-24 18:16:15 +0000311 if( pList==0 || pList->nExpr!=2 ){
drh6c1f4ef2015-06-08 14:23:15 +0000312 return 0;
313 }
dan43970dd2015-11-24 17:39:01 +0000314 pCol = pList->a[1].pExpr;
315 if( pCol->op!=TK_COLUMN || !IsVirtual(pCol->pTab) ){
drh6c1f4ef2015-06-08 14:23:15 +0000316 return 0;
317 }
dan07bdba82015-11-23 21:09:54 +0000318 for(i=0; i<ArraySize(aOp); i++){
319 if( sqlite3StrICmp(pExpr->u.zToken, aOp[i].zOp)==0 ){
320 *peOp2 = aOp[i].eOp2;
321 return 1;
322 }
323 }
324 return 0;
drh6c1f4ef2015-06-08 14:23:15 +0000325}
326#endif /* SQLITE_OMIT_VIRTUALTABLE */
327
328/*
329** If the pBase expression originated in the ON or USING clause of
330** a join, then transfer the appropriate markings over to derived.
331*/
332static void transferJoinMarkings(Expr *pDerived, Expr *pBase){
333 if( pDerived ){
334 pDerived->flags |= pBase->flags & EP_FromJoin;
335 pDerived->iRightJoinTable = pBase->iRightJoinTable;
336 }
337}
338
339/*
340** Mark term iChild as being a child of term iParent
341*/
342static void markTermAsChild(WhereClause *pWC, int iChild, int iParent){
343 pWC->a[iChild].iParent = iParent;
344 pWC->a[iChild].truthProb = pWC->a[iParent].truthProb;
345 pWC->a[iParent].nChild++;
346}
347
348/*
349** Return the N-th AND-connected subterm of pTerm. Or if pTerm is not
350** a conjunction, then return just pTerm when N==0. If N is exceeds
351** the number of available subterms, return NULL.
352*/
353static WhereTerm *whereNthSubterm(WhereTerm *pTerm, int N){
354 if( pTerm->eOperator!=WO_AND ){
355 return N==0 ? pTerm : 0;
356 }
357 if( N<pTerm->u.pAndInfo->wc.nTerm ){
358 return &pTerm->u.pAndInfo->wc.a[N];
359 }
360 return 0;
361}
362
363/*
364** Subterms pOne and pTwo are contained within WHERE clause pWC. The
365** two subterms are in disjunction - they are OR-ed together.
366**
367** If these two terms are both of the form: "A op B" with the same
368** A and B values but different operators and if the operators are
369** compatible (if one is = and the other is <, for example) then
370** add a new virtual AND term to pWC that is the combination of the
371** two.
372**
373** Some examples:
374**
375** x<y OR x=y --> x<=y
376** x=y OR x=y --> x=y
377** x<=y OR x<y --> x<=y
378**
379** The following is NOT generated:
380**
381** x<y OR x>y --> x!=y
382*/
383static void whereCombineDisjuncts(
384 SrcList *pSrc, /* the FROM clause */
385 WhereClause *pWC, /* The complete WHERE clause */
386 WhereTerm *pOne, /* First disjunct */
387 WhereTerm *pTwo /* Second disjunct */
388){
389 u16 eOp = pOne->eOperator | pTwo->eOperator;
390 sqlite3 *db; /* Database connection (for malloc) */
391 Expr *pNew; /* New virtual expression */
392 int op; /* Operator for the combined expression */
393 int idxNew; /* Index in pWC of the next virtual term */
394
395 if( (pOne->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return;
396 if( (pTwo->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return;
397 if( (eOp & (WO_EQ|WO_LT|WO_LE))!=eOp
398 && (eOp & (WO_EQ|WO_GT|WO_GE))!=eOp ) return;
399 assert( pOne->pExpr->pLeft!=0 && pOne->pExpr->pRight!=0 );
400 assert( pTwo->pExpr->pLeft!=0 && pTwo->pExpr->pRight!=0 );
401 if( sqlite3ExprCompare(pOne->pExpr->pLeft, pTwo->pExpr->pLeft, -1) ) return;
402 if( sqlite3ExprCompare(pOne->pExpr->pRight, pTwo->pExpr->pRight, -1) )return;
403 /* If we reach this point, it means the two subterms can be combined */
404 if( (eOp & (eOp-1))!=0 ){
405 if( eOp & (WO_LT|WO_LE) ){
406 eOp = WO_LE;
407 }else{
408 assert( eOp & (WO_GT|WO_GE) );
409 eOp = WO_GE;
410 }
411 }
412 db = pWC->pWInfo->pParse->db;
413 pNew = sqlite3ExprDup(db, pOne->pExpr, 0);
414 if( pNew==0 ) return;
415 for(op=TK_EQ; eOp!=(WO_EQ<<(op-TK_EQ)); op++){ assert( op<TK_GE ); }
416 pNew->op = op;
417 idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC);
418 exprAnalyze(pSrc, pWC, idxNew);
419}
420
421#if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
422/*
423** Analyze a term that consists of two or more OR-connected
424** subterms. So in:
425**
426** ... WHERE (a=5) AND (b=7 OR c=9 OR d=13) AND (d=13)
427** ^^^^^^^^^^^^^^^^^^^^
428**
429** This routine analyzes terms such as the middle term in the above example.
430** A WhereOrTerm object is computed and attached to the term under
431** analysis, regardless of the outcome of the analysis. Hence:
432**
433** WhereTerm.wtFlags |= TERM_ORINFO
434** WhereTerm.u.pOrInfo = a dynamically allocated WhereOrTerm object
435**
436** The term being analyzed must have two or more of OR-connected subterms.
437** A single subterm might be a set of AND-connected sub-subterms.
438** Examples of terms under analysis:
439**
440** (A) t1.x=t2.y OR t1.x=t2.z OR t1.y=15 OR t1.z=t3.a+5
441** (B) x=expr1 OR expr2=x OR x=expr3
442** (C) t1.x=t2.y OR (t1.x=t2.z AND t1.y=15)
443** (D) x=expr1 OR (y>11 AND y<22 AND z LIKE '*hello*')
444** (E) (p.a=1 AND q.b=2 AND r.c=3) OR (p.x=4 AND q.y=5 AND r.z=6)
445** (F) x>A OR (x=A AND y>=B)
446**
447** CASE 1:
448**
449** If all subterms are of the form T.C=expr for some single column of C and
450** a single table T (as shown in example B above) then create a new virtual
451** term that is an equivalent IN expression. In other words, if the term
452** being analyzed is:
453**
454** x = expr1 OR expr2 = x OR x = expr3
455**
456** then create a new virtual term like this:
457**
458** x IN (expr1,expr2,expr3)
459**
460** CASE 2:
461**
462** If there are exactly two disjuncts and one side has x>A and the other side
463** has x=A (for the same x and A) then add a new virtual conjunct term to the
464** WHERE clause of the form "x>=A". Example:
465**
466** x>A OR (x=A AND y>B) adds: x>=A
467**
468** The added conjunct can sometimes be helpful in query planning.
469**
470** CASE 3:
471**
472** If all subterms are indexable by a single table T, then set
473**
474** WhereTerm.eOperator = WO_OR
475** WhereTerm.u.pOrInfo->indexable |= the cursor number for table T
476**
477** A subterm is "indexable" if it is of the form
478** "T.C <op> <expr>" where C is any column of table T and
479** <op> is one of "=", "<", "<=", ">", ">=", "IS NULL", or "IN".
480** A subterm is also indexable if it is an AND of two or more
481** subsubterms at least one of which is indexable. Indexable AND
482** subterms have their eOperator set to WO_AND and they have
483** u.pAndInfo set to a dynamically allocated WhereAndTerm object.
484**
485** From another point of view, "indexable" means that the subterm could
486** potentially be used with an index if an appropriate index exists.
487** This analysis does not consider whether or not the index exists; that
488** is decided elsewhere. This analysis only looks at whether subterms
489** appropriate for indexing exist.
490**
491** All examples A through E above satisfy case 3. But if a term
492** also satisfies case 1 (such as B) we know that the optimizer will
493** always prefer case 1, so in that case we pretend that case 3 is not
494** satisfied.
495**
496** It might be the case that multiple tables are indexable. For example,
497** (E) above is indexable on tables P, Q, and R.
498**
499** Terms that satisfy case 3 are candidates for lookup by using
500** separate indices to find rowids for each subterm and composing
501** the union of all rowids using a RowSet object. This is similar
502** to "bitmap indices" in other database engines.
503**
504** OTHERWISE:
505**
506** If none of cases 1, 2, or 3 apply, then leave the eOperator set to
507** zero. This term is not useful for search.
508*/
509static void exprAnalyzeOrTerm(
510 SrcList *pSrc, /* the FROM clause */
511 WhereClause *pWC, /* the complete WHERE clause */
512 int idxTerm /* Index of the OR-term to be analyzed */
513){
514 WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */
515 Parse *pParse = pWInfo->pParse; /* Parser context */
516 sqlite3 *db = pParse->db; /* Database connection */
517 WhereTerm *pTerm = &pWC->a[idxTerm]; /* The term to be analyzed */
518 Expr *pExpr = pTerm->pExpr; /* The expression of the term */
519 int i; /* Loop counters */
520 WhereClause *pOrWc; /* Breakup of pTerm into subterms */
521 WhereTerm *pOrTerm; /* A Sub-term within the pOrWc */
522 WhereOrInfo *pOrInfo; /* Additional information associated with pTerm */
523 Bitmask chngToIN; /* Tables that might satisfy case 1 */
524 Bitmask indexable; /* Tables that are indexable, satisfying case 2 */
525
526 /*
527 ** Break the OR clause into its separate subterms. The subterms are
528 ** stored in a WhereClause structure containing within the WhereOrInfo
529 ** object that is attached to the original OR clause term.
530 */
531 assert( (pTerm->wtFlags & (TERM_DYNAMIC|TERM_ORINFO|TERM_ANDINFO))==0 );
532 assert( pExpr->op==TK_OR );
533 pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, sizeof(*pOrInfo));
534 if( pOrInfo==0 ) return;
535 pTerm->wtFlags |= TERM_ORINFO;
536 pOrWc = &pOrInfo->wc;
drh81fd3492016-02-19 14:10:44 +0000537 memset(pOrWc->aStatic, 0, sizeof(pOrWc->aStatic));
drh6c1f4ef2015-06-08 14:23:15 +0000538 sqlite3WhereClauseInit(pOrWc, pWInfo);
539 sqlite3WhereSplit(pOrWc, pExpr, TK_OR);
540 sqlite3WhereExprAnalyze(pSrc, pOrWc);
541 if( db->mallocFailed ) return;
542 assert( pOrWc->nTerm>=2 );
543
544 /*
545 ** Compute the set of tables that might satisfy cases 1 or 3.
546 */
547 indexable = ~(Bitmask)0;
548 chngToIN = ~(Bitmask)0;
549 for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0 && indexable; i--, pOrTerm++){
550 if( (pOrTerm->eOperator & WO_SINGLE)==0 ){
551 WhereAndInfo *pAndInfo;
552 assert( (pOrTerm->wtFlags & (TERM_ANDINFO|TERM_ORINFO))==0 );
553 chngToIN = 0;
drh575fad62016-02-05 13:38:36 +0000554 pAndInfo = sqlite3DbMallocRawNN(db, sizeof(*pAndInfo));
drh6c1f4ef2015-06-08 14:23:15 +0000555 if( pAndInfo ){
556 WhereClause *pAndWC;
557 WhereTerm *pAndTerm;
558 int j;
559 Bitmask b = 0;
560 pOrTerm->u.pAndInfo = pAndInfo;
561 pOrTerm->wtFlags |= TERM_ANDINFO;
562 pOrTerm->eOperator = WO_AND;
563 pAndWC = &pAndInfo->wc;
drh81fd3492016-02-19 14:10:44 +0000564 memset(pAndWC->aStatic, 0, sizeof(pAndWC->aStatic));
drh6c1f4ef2015-06-08 14:23:15 +0000565 sqlite3WhereClauseInit(pAndWC, pWC->pWInfo);
566 sqlite3WhereSplit(pAndWC, pOrTerm->pExpr, TK_AND);
567 sqlite3WhereExprAnalyze(pSrc, pAndWC);
568 pAndWC->pOuter = pWC;
drh6c1f4ef2015-06-08 14:23:15 +0000569 if( !db->mallocFailed ){
570 for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){
571 assert( pAndTerm->pExpr );
dandbd2dcb2016-05-28 18:53:55 +0000572 if( allowedOp(pAndTerm->pExpr->op)
573 || pAndTerm->eOperator==WO_MATCH
574 ){
drh6c1f4ef2015-06-08 14:23:15 +0000575 b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pAndTerm->leftCursor);
576 }
577 }
578 }
579 indexable &= b;
580 }
581 }else if( pOrTerm->wtFlags & TERM_COPIED ){
582 /* Skip this term for now. We revisit it when we process the
583 ** corresponding TERM_VIRTUAL term */
584 }else{
585 Bitmask b;
586 b = sqlite3WhereGetMask(&pWInfo->sMaskSet, pOrTerm->leftCursor);
587 if( pOrTerm->wtFlags & TERM_VIRTUAL ){
588 WhereTerm *pOther = &pOrWc->a[pOrTerm->iParent];
589 b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pOther->leftCursor);
590 }
591 indexable &= b;
592 if( (pOrTerm->eOperator & WO_EQ)==0 ){
593 chngToIN = 0;
594 }else{
595 chngToIN &= b;
596 }
597 }
598 }
599
600 /*
601 ** Record the set of tables that satisfy case 3. The set might be
602 ** empty.
603 */
604 pOrInfo->indexable = indexable;
605 pTerm->eOperator = indexable==0 ? 0 : WO_OR;
606
607 /* For a two-way OR, attempt to implementation case 2.
608 */
609 if( indexable && pOrWc->nTerm==2 ){
610 int iOne = 0;
611 WhereTerm *pOne;
612 while( (pOne = whereNthSubterm(&pOrWc->a[0],iOne++))!=0 ){
613 int iTwo = 0;
614 WhereTerm *pTwo;
615 while( (pTwo = whereNthSubterm(&pOrWc->a[1],iTwo++))!=0 ){
616 whereCombineDisjuncts(pSrc, pWC, pOne, pTwo);
617 }
618 }
619 }
620
621 /*
622 ** chngToIN holds a set of tables that *might* satisfy case 1. But
623 ** we have to do some additional checking to see if case 1 really
624 ** is satisfied.
625 **
626 ** chngToIN will hold either 0, 1, or 2 bits. The 0-bit case means
627 ** that there is no possibility of transforming the OR clause into an
628 ** IN operator because one or more terms in the OR clause contain
629 ** something other than == on a column in the single table. The 1-bit
630 ** case means that every term of the OR clause is of the form
631 ** "table.column=expr" for some single table. The one bit that is set
632 ** will correspond to the common table. We still need to check to make
633 ** sure the same column is used on all terms. The 2-bit case is when
634 ** the all terms are of the form "table1.column=table2.column". It
635 ** might be possible to form an IN operator with either table1.column
636 ** or table2.column as the LHS if either is common to every term of
637 ** the OR clause.
638 **
639 ** Note that terms of the form "table.column1=table.column2" (the
640 ** same table on both sizes of the ==) cannot be optimized.
641 */
642 if( chngToIN ){
643 int okToChngToIN = 0; /* True if the conversion to IN is valid */
644 int iColumn = -1; /* Column index on lhs of IN operator */
645 int iCursor = -1; /* Table cursor common to all terms */
646 int j = 0; /* Loop counter */
647
648 /* Search for a table and column that appears on one side or the
649 ** other of the == operator in every subterm. That table and column
650 ** will be recorded in iCursor and iColumn. There might not be any
651 ** such table and column. Set okToChngToIN if an appropriate table
652 ** and column is found but leave okToChngToIN false if not found.
653 */
654 for(j=0; j<2 && !okToChngToIN; j++){
655 pOrTerm = pOrWc->a;
656 for(i=pOrWc->nTerm-1; i>=0; i--, pOrTerm++){
657 assert( pOrTerm->eOperator & WO_EQ );
658 pOrTerm->wtFlags &= ~TERM_OR_OK;
659 if( pOrTerm->leftCursor==iCursor ){
660 /* This is the 2-bit case and we are on the second iteration and
661 ** current term is from the first iteration. So skip this term. */
662 assert( j==1 );
663 continue;
664 }
665 if( (chngToIN & sqlite3WhereGetMask(&pWInfo->sMaskSet,
666 pOrTerm->leftCursor))==0 ){
667 /* This term must be of the form t1.a==t2.b where t2 is in the
668 ** chngToIN set but t1 is not. This term will be either preceded
669 ** or follwed by an inverted copy (t2.b==t1.a). Skip this term
670 ** and use its inversion. */
671 testcase( pOrTerm->wtFlags & TERM_COPIED );
672 testcase( pOrTerm->wtFlags & TERM_VIRTUAL );
673 assert( pOrTerm->wtFlags & (TERM_COPIED|TERM_VIRTUAL) );
674 continue;
675 }
676 iColumn = pOrTerm->u.leftColumn;
677 iCursor = pOrTerm->leftCursor;
678 break;
679 }
680 if( i<0 ){
681 /* No candidate table+column was found. This can only occur
682 ** on the second iteration */
683 assert( j==1 );
684 assert( IsPowerOfTwo(chngToIN) );
685 assert( chngToIN==sqlite3WhereGetMask(&pWInfo->sMaskSet, iCursor) );
686 break;
687 }
688 testcase( j==1 );
689
690 /* We have found a candidate table and column. Check to see if that
691 ** table and column is common to every term in the OR clause */
692 okToChngToIN = 1;
693 for(; i>=0 && okToChngToIN; i--, pOrTerm++){
694 assert( pOrTerm->eOperator & WO_EQ );
695 if( pOrTerm->leftCursor!=iCursor ){
696 pOrTerm->wtFlags &= ~TERM_OR_OK;
697 }else if( pOrTerm->u.leftColumn!=iColumn ){
698 okToChngToIN = 0;
699 }else{
700 int affLeft, affRight;
701 /* If the right-hand side is also a column, then the affinities
702 ** of both right and left sides must be such that no type
703 ** conversions are required on the right. (Ticket #2249)
704 */
705 affRight = sqlite3ExprAffinity(pOrTerm->pExpr->pRight);
706 affLeft = sqlite3ExprAffinity(pOrTerm->pExpr->pLeft);
707 if( affRight!=0 && affRight!=affLeft ){
708 okToChngToIN = 0;
709 }else{
710 pOrTerm->wtFlags |= TERM_OR_OK;
711 }
712 }
713 }
714 }
715
716 /* At this point, okToChngToIN is true if original pTerm satisfies
717 ** case 1. In that case, construct a new virtual term that is
718 ** pTerm converted into an IN operator.
719 */
720 if( okToChngToIN ){
721 Expr *pDup; /* A transient duplicate expression */
722 ExprList *pList = 0; /* The RHS of the IN operator */
723 Expr *pLeft = 0; /* The LHS of the IN operator */
724 Expr *pNew; /* The complete IN operator */
725
726 for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0; i--, pOrTerm++){
727 if( (pOrTerm->wtFlags & TERM_OR_OK)==0 ) continue;
728 assert( pOrTerm->eOperator & WO_EQ );
729 assert( pOrTerm->leftCursor==iCursor );
730 assert( pOrTerm->u.leftColumn==iColumn );
731 pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight, 0);
732 pList = sqlite3ExprListAppend(pWInfo->pParse, pList, pDup);
733 pLeft = pOrTerm->pExpr->pLeft;
734 }
735 assert( pLeft!=0 );
736 pDup = sqlite3ExprDup(db, pLeft, 0);
drhabfd35e2016-12-06 22:47:23 +0000737 pNew = sqlite3PExpr(pParse, TK_IN, pDup, 0);
drh6c1f4ef2015-06-08 14:23:15 +0000738 if( pNew ){
739 int idxNew;
740 transferJoinMarkings(pNew, pExpr);
741 assert( !ExprHasProperty(pNew, EP_xIsSelect) );
742 pNew->x.pList = pList;
743 idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC);
744 testcase( idxNew==0 );
745 exprAnalyze(pSrc, pWC, idxNew);
746 pTerm = &pWC->a[idxTerm];
747 markTermAsChild(pWC, idxNew, idxTerm);
748 }else{
749 sqlite3ExprListDelete(db, pList);
750 }
751 pTerm->eOperator = WO_NOOP; /* case 1 trumps case 3 */
752 }
753 }
754}
755#endif /* !SQLITE_OMIT_OR_OPTIMIZATION && !SQLITE_OMIT_SUBQUERY */
756
757/*
758** We already know that pExpr is a binary operator where both operands are
759** column references. This routine checks to see if pExpr is an equivalence
760** relation:
761** 1. The SQLITE_Transitive optimization must be enabled
762** 2. Must be either an == or an IS operator
763** 3. Not originating in the ON clause of an OUTER JOIN
764** 4. The affinities of A and B must be compatible
765** 5a. Both operands use the same collating sequence OR
766** 5b. The overall collating sequence is BINARY
767** If this routine returns TRUE, that means that the RHS can be substituted
768** for the LHS anyplace else in the WHERE clause where the LHS column occurs.
769** This is an optimization. No harm comes from returning 0. But if 1 is
770** returned when it should not be, then incorrect answers might result.
771*/
772static int termIsEquivalence(Parse *pParse, Expr *pExpr){
773 char aff1, aff2;
774 CollSeq *pColl;
775 const char *zColl1, *zColl2;
776 if( !OptimizationEnabled(pParse->db, SQLITE_Transitive) ) return 0;
777 if( pExpr->op!=TK_EQ && pExpr->op!=TK_IS ) return 0;
778 if( ExprHasProperty(pExpr, EP_FromJoin) ) return 0;
779 aff1 = sqlite3ExprAffinity(pExpr->pLeft);
780 aff2 = sqlite3ExprAffinity(pExpr->pRight);
781 if( aff1!=aff2
782 && (!sqlite3IsNumericAffinity(aff1) || !sqlite3IsNumericAffinity(aff2))
783 ){
784 return 0;
785 }
786 pColl = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft, pExpr->pRight);
787 if( pColl==0 || sqlite3StrICmp(pColl->zName, "BINARY")==0 ) return 1;
788 pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
drha2fbe3b2016-06-28 22:27:56 +0000789 zColl1 = pColl ? pColl->zName : 0;
drh6c1f4ef2015-06-08 14:23:15 +0000790 pColl = sqlite3ExprCollSeq(pParse, pExpr->pRight);
drha2fbe3b2016-06-28 22:27:56 +0000791 zColl2 = pColl ? pColl->zName : 0;
792 return sqlite3_stricmp(zColl1, zColl2)==0;
drh6c1f4ef2015-06-08 14:23:15 +0000793}
794
795/*
796** Recursively walk the expressions of a SELECT statement and generate
797** a bitmask indicating which tables are used in that expression
798** tree.
799*/
800static Bitmask exprSelectUsage(WhereMaskSet *pMaskSet, Select *pS){
801 Bitmask mask = 0;
802 while( pS ){
803 SrcList *pSrc = pS->pSrc;
804 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pEList);
805 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pGroupBy);
806 mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pOrderBy);
807 mask |= sqlite3WhereExprUsage(pMaskSet, pS->pWhere);
808 mask |= sqlite3WhereExprUsage(pMaskSet, pS->pHaving);
809 if( ALWAYS(pSrc!=0) ){
810 int i;
811 for(i=0; i<pSrc->nSrc; i++){
812 mask |= exprSelectUsage(pMaskSet, pSrc->a[i].pSelect);
813 mask |= sqlite3WhereExprUsage(pMaskSet, pSrc->a[i].pOn);
814 }
815 }
816 pS = pS->pPrior;
817 }
818 return mask;
819}
820
821/*
drh47991422015-08-31 15:58:06 +0000822** Expression pExpr is one operand of a comparison operator that might
823** be useful for indexing. This routine checks to see if pExpr appears
824** in any index. Return TRUE (1) if pExpr is an indexed term and return
825** FALSE (0) if not. If TRUE is returned, also set *piCur to the cursor
826** number of the table that is indexed and *piColumn to the column number
drh8d25cb92016-08-19 19:58:06 +0000827** of the column that is indexed, or XN_EXPR (-2) if an expression is being
828** indexed.
drh47991422015-08-31 15:58:06 +0000829**
830** If pExpr is a TK_COLUMN column reference, then this routine always returns
831** true even if that particular column is not indexed, because the column
832** might be added to an automatic index later.
833*/
834static int exprMightBeIndexed(
835 SrcList *pFrom, /* The FROM clause */
dan95a08c02016-08-02 16:18:35 +0000836 int op, /* The specific comparison operator */
drh47991422015-08-31 15:58:06 +0000837 Bitmask mPrereq, /* Bitmask of FROM clause terms referenced by pExpr */
838 Expr *pExpr, /* An operand of a comparison operator */
839 int *piCur, /* Write the referenced table cursor number here */
840 int *piColumn /* Write the referenced table column number here */
841){
842 Index *pIdx;
843 int i;
844 int iCur;
dan71c57db2016-07-09 20:23:55 +0000845
846 /* If this expression is a vector to the left or right of a
847 ** inequality constraint (>, <, >= or <=), perform the processing
848 ** on the first element of the vector. */
849 assert( TK_GT+1==TK_LE && TK_GT+2==TK_LT && TK_GT+3==TK_GE );
drh64bcb8c2016-08-26 03:42:57 +0000850 assert( TK_IS<TK_GE && TK_ISNULL<TK_GE && TK_IN<TK_GE );
851 assert( op<=TK_GE );
852 if( pExpr->op==TK_VECTOR && (op>=TK_GT && ALWAYS(op<=TK_GE)) ){
dan71c57db2016-07-09 20:23:55 +0000853 pExpr = pExpr->x.pList->a[0].pExpr;
854 }
855
drh47991422015-08-31 15:58:06 +0000856 if( pExpr->op==TK_COLUMN ){
857 *piCur = pExpr->iTable;
858 *piColumn = pExpr->iColumn;
859 return 1;
860 }
861 if( mPrereq==0 ) return 0; /* No table references */
862 if( (mPrereq&(mPrereq-1))!=0 ) return 0; /* Refs more than one table */
863 for(i=0; mPrereq>1; i++, mPrereq>>=1){}
864 iCur = pFrom->a[i].iCursor;
865 for(pIdx=pFrom->a[i].pTab->pIndex; pIdx; pIdx=pIdx->pNext){
866 if( pIdx->aColExpr==0 ) continue;
867 for(i=0; i<pIdx->nKeyCol; i++){
drh8d25cb92016-08-19 19:58:06 +0000868 if( pIdx->aiColumn[i]!=XN_EXPR ) continue;
drh47991422015-08-31 15:58:06 +0000869 if( sqlite3ExprCompare(pExpr, pIdx->aColExpr->a[i].pExpr, iCur)==0 ){
870 *piCur = iCur;
drh8d25cb92016-08-19 19:58:06 +0000871 *piColumn = XN_EXPR;
drh47991422015-08-31 15:58:06 +0000872 return 1;
873 }
874 }
875 }
876 return 0;
877}
878
dan870a0702016-08-01 16:37:43 +0000879/*
drh6c1f4ef2015-06-08 14:23:15 +0000880** The input to this routine is an WhereTerm structure with only the
881** "pExpr" field filled in. The job of this routine is to analyze the
882** subexpression and populate all the other fields of the WhereTerm
883** structure.
884**
885** If the expression is of the form "<expr> <op> X" it gets commuted
886** to the standard form of "X <op> <expr>".
887**
888** If the expression is of the form "X <op> Y" where both X and Y are
889** columns, then the original expression is unchanged and a new virtual
890** term of the form "Y <op> X" is added to the WHERE clause and
891** analyzed separately. The original term is marked with TERM_COPIED
892** and the new term is marked with TERM_DYNAMIC (because it's pExpr
893** needs to be freed with the WhereClause) and TERM_VIRTUAL (because it
894** is a commuted copy of a prior term.) The original term has nChild=1
895** and the copy has idxParent set to the index of the original term.
896*/
897static void exprAnalyze(
898 SrcList *pSrc, /* the FROM clause */
899 WhereClause *pWC, /* the WHERE clause */
900 int idxTerm /* Index of the term to be analyzed */
901){
902 WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */
903 WhereTerm *pTerm; /* The term to be analyzed */
904 WhereMaskSet *pMaskSet; /* Set of table index masks */
905 Expr *pExpr; /* The expression to be analyzed */
906 Bitmask prereqLeft; /* Prerequesites of the pExpr->pLeft */
907 Bitmask prereqAll; /* Prerequesites of pExpr */
908 Bitmask extraRight = 0; /* Extra dependencies on LEFT JOIN */
909 Expr *pStr1 = 0; /* RHS of LIKE/GLOB operator */
910 int isComplete = 0; /* RHS of LIKE/GLOB ends with wildcard */
911 int noCase = 0; /* uppercase equivalent to lowercase */
912 int op; /* Top-level operator. pExpr->op */
913 Parse *pParse = pWInfo->pParse; /* Parsing context */
914 sqlite3 *db = pParse->db; /* Database connection */
dan07bdba82015-11-23 21:09:54 +0000915 unsigned char eOp2; /* op2 value for LIKE/REGEXP/GLOB */
drh6c1f4ef2015-06-08 14:23:15 +0000916
917 if( db->mallocFailed ){
918 return;
919 }
920 pTerm = &pWC->a[idxTerm];
921 pMaskSet = &pWInfo->sMaskSet;
922 pExpr = pTerm->pExpr;
923 assert( pExpr->op!=TK_AS && pExpr->op!=TK_COLLATE );
924 prereqLeft = sqlite3WhereExprUsage(pMaskSet, pExpr->pLeft);
925 op = pExpr->op;
926 if( op==TK_IN ){
927 assert( pExpr->pRight==0 );
dan7b35a772016-07-28 19:47:15 +0000928 if( sqlite3ExprCheckIN(pParse, pExpr) ) return;
drh6c1f4ef2015-06-08 14:23:15 +0000929 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
930 pTerm->prereqRight = exprSelectUsage(pMaskSet, pExpr->x.pSelect);
931 }else{
932 pTerm->prereqRight = sqlite3WhereExprListUsage(pMaskSet, pExpr->x.pList);
933 }
934 }else if( op==TK_ISNULL ){
935 pTerm->prereqRight = 0;
936 }else{
937 pTerm->prereqRight = sqlite3WhereExprUsage(pMaskSet, pExpr->pRight);
938 }
939 prereqAll = sqlite3WhereExprUsage(pMaskSet, pExpr);
940 if( ExprHasProperty(pExpr, EP_FromJoin) ){
941 Bitmask x = sqlite3WhereGetMask(pMaskSet, pExpr->iRightJoinTable);
942 prereqAll |= x;
943 extraRight = x-1; /* ON clause terms may not be used with an index
944 ** on left table of a LEFT JOIN. Ticket #3015 */
945 }
946 pTerm->prereqAll = prereqAll;
947 pTerm->leftCursor = -1;
948 pTerm->iParent = -1;
949 pTerm->eOperator = 0;
950 if( allowedOp(op) ){
drh47991422015-08-31 15:58:06 +0000951 int iCur, iColumn;
drh6c1f4ef2015-06-08 14:23:15 +0000952 Expr *pLeft = sqlite3ExprSkipCollate(pExpr->pLeft);
953 Expr *pRight = sqlite3ExprSkipCollate(pExpr->pRight);
954 u16 opMask = (pTerm->prereqRight & prereqLeft)==0 ? WO_ALL : WO_EQUIV;
dan8da209b2016-07-26 18:06:08 +0000955
dan145b4ea2016-07-29 18:12:12 +0000956 if( pTerm->iField>0 ){
957 assert( op==TK_IN );
dan8da209b2016-07-26 18:06:08 +0000958 assert( pLeft->op==TK_VECTOR );
959 pLeft = pLeft->x.pList->a[pTerm->iField-1].pExpr;
960 }
961
dan71c57db2016-07-09 20:23:55 +0000962 if( exprMightBeIndexed(pSrc, op, prereqLeft, pLeft, &iCur, &iColumn) ){
drh47991422015-08-31 15:58:06 +0000963 pTerm->leftCursor = iCur;
964 pTerm->u.leftColumn = iColumn;
drh6860e6f2015-08-27 18:24:02 +0000965 pTerm->eOperator = operatorMask(op) & opMask;
drh6c1f4ef2015-06-08 14:23:15 +0000966 }
967 if( op==TK_IS ) pTerm->wtFlags |= TERM_IS;
drh47991422015-08-31 15:58:06 +0000968 if( pRight
dan71c57db2016-07-09 20:23:55 +0000969 && exprMightBeIndexed(pSrc, op, pTerm->prereqRight, pRight, &iCur,&iColumn)
drh47991422015-08-31 15:58:06 +0000970 ){
drh6c1f4ef2015-06-08 14:23:15 +0000971 WhereTerm *pNew;
972 Expr *pDup;
973 u16 eExtraOp = 0; /* Extra bits for pNew->eOperator */
dan145b4ea2016-07-29 18:12:12 +0000974 assert( pTerm->iField==0 );
drh6c1f4ef2015-06-08 14:23:15 +0000975 if( pTerm->leftCursor>=0 ){
976 int idxNew;
977 pDup = sqlite3ExprDup(db, pExpr, 0);
978 if( db->mallocFailed ){
979 sqlite3ExprDelete(db, pDup);
980 return;
981 }
982 idxNew = whereClauseInsert(pWC, pDup, TERM_VIRTUAL|TERM_DYNAMIC);
983 if( idxNew==0 ) return;
984 pNew = &pWC->a[idxNew];
985 markTermAsChild(pWC, idxNew, idxTerm);
986 if( op==TK_IS ) pNew->wtFlags |= TERM_IS;
987 pTerm = &pWC->a[idxTerm];
988 pTerm->wtFlags |= TERM_COPIED;
989
990 if( termIsEquivalence(pParse, pDup) ){
991 pTerm->eOperator |= WO_EQUIV;
992 eExtraOp = WO_EQUIV;
993 }
994 }else{
995 pDup = pExpr;
996 pNew = pTerm;
997 }
998 exprCommute(pParse, pDup);
drh47991422015-08-31 15:58:06 +0000999 pNew->leftCursor = iCur;
1000 pNew->u.leftColumn = iColumn;
drh6c1f4ef2015-06-08 14:23:15 +00001001 testcase( (prereqLeft | extraRight) != prereqLeft );
1002 pNew->prereqRight = prereqLeft | extraRight;
1003 pNew->prereqAll = prereqAll;
1004 pNew->eOperator = (operatorMask(pDup->op) + eExtraOp) & opMask;
1005 }
1006 }
1007
1008#ifndef SQLITE_OMIT_BETWEEN_OPTIMIZATION
1009 /* If a term is the BETWEEN operator, create two new virtual terms
1010 ** that define the range that the BETWEEN implements. For example:
1011 **
1012 ** a BETWEEN b AND c
1013 **
1014 ** is converted into:
1015 **
1016 ** (a BETWEEN b AND c) AND (a>=b) AND (a<=c)
1017 **
1018 ** The two new terms are added onto the end of the WhereClause object.
1019 ** The new terms are "dynamic" and are children of the original BETWEEN
1020 ** term. That means that if the BETWEEN term is coded, the children are
1021 ** skipped. Or, if the children are satisfied by an index, the original
1022 ** BETWEEN term is skipped.
1023 */
1024 else if( pExpr->op==TK_BETWEEN && pWC->op==TK_AND ){
1025 ExprList *pList = pExpr->x.pList;
1026 int i;
1027 static const u8 ops[] = {TK_GE, TK_LE};
1028 assert( pList!=0 );
1029 assert( pList->nExpr==2 );
1030 for(i=0; i<2; i++){
1031 Expr *pNewExpr;
1032 int idxNew;
1033 pNewExpr = sqlite3PExpr(pParse, ops[i],
1034 sqlite3ExprDup(db, pExpr->pLeft, 0),
drhabfd35e2016-12-06 22:47:23 +00001035 sqlite3ExprDup(db, pList->a[i].pExpr, 0));
drh6c1f4ef2015-06-08 14:23:15 +00001036 transferJoinMarkings(pNewExpr, pExpr);
1037 idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
1038 testcase( idxNew==0 );
1039 exprAnalyze(pSrc, pWC, idxNew);
1040 pTerm = &pWC->a[idxTerm];
1041 markTermAsChild(pWC, idxNew, idxTerm);
1042 }
1043 }
1044#endif /* SQLITE_OMIT_BETWEEN_OPTIMIZATION */
1045
1046#if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
1047 /* Analyze a term that is composed of two or more subterms connected by
1048 ** an OR operator.
1049 */
1050 else if( pExpr->op==TK_OR ){
1051 assert( pWC->op==TK_AND );
1052 exprAnalyzeOrTerm(pSrc, pWC, idxTerm);
1053 pTerm = &pWC->a[idxTerm];
1054 }
1055#endif /* SQLITE_OMIT_OR_OPTIMIZATION */
1056
1057#ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
1058 /* Add constraints to reduce the search space on a LIKE or GLOB
1059 ** operator.
1060 **
1061 ** A like pattern of the form "x LIKE 'aBc%'" is changed into constraints
1062 **
1063 ** x>='ABC' AND x<'abd' AND x LIKE 'aBc%'
1064 **
1065 ** The last character of the prefix "abc" is incremented to form the
1066 ** termination condition "abd". If case is not significant (the default
1067 ** for LIKE) then the lower-bound is made all uppercase and the upper-
1068 ** bound is made all lowercase so that the bounds also work when comparing
1069 ** BLOBs.
1070 */
1071 if( pWC->op==TK_AND
1072 && isLikeOrGlob(pParse, pExpr, &pStr1, &isComplete, &noCase)
1073 ){
1074 Expr *pLeft; /* LHS of LIKE/GLOB operator */
1075 Expr *pStr2; /* Copy of pStr1 - RHS of LIKE/GLOB operator */
1076 Expr *pNewExpr1;
1077 Expr *pNewExpr2;
1078 int idxNew1;
1079 int idxNew2;
1080 const char *zCollSeqName; /* Name of collating sequence */
1081 const u16 wtFlags = TERM_LIKEOPT | TERM_VIRTUAL | TERM_DYNAMIC;
1082
1083 pLeft = pExpr->x.pList->a[1].pExpr;
1084 pStr2 = sqlite3ExprDup(db, pStr1, 0);
1085
1086 /* Convert the lower bound to upper-case and the upper bound to
1087 ** lower-case (upper-case is less than lower-case in ASCII) so that
1088 ** the range constraints also work for BLOBs
1089 */
1090 if( noCase && !pParse->db->mallocFailed ){
1091 int i;
1092 char c;
1093 pTerm->wtFlags |= TERM_LIKE;
1094 for(i=0; (c = pStr1->u.zToken[i])!=0; i++){
1095 pStr1->u.zToken[i] = sqlite3Toupper(c);
1096 pStr2->u.zToken[i] = sqlite3Tolower(c);
1097 }
1098 }
1099
1100 if( !db->mallocFailed ){
1101 u8 c, *pC; /* Last character before the first wildcard */
1102 pC = (u8*)&pStr2->u.zToken[sqlite3Strlen30(pStr2->u.zToken)-1];
1103 c = *pC;
1104 if( noCase ){
1105 /* The point is to increment the last character before the first
1106 ** wildcard. But if we increment '@', that will push it into the
1107 ** alphabetic range where case conversions will mess up the
1108 ** inequality. To avoid this, make sure to also run the full
1109 ** LIKE on all candidate expressions by clearing the isComplete flag
1110 */
1111 if( c=='A'-1 ) isComplete = 0;
1112 c = sqlite3UpperToLower[c];
1113 }
1114 *pC = c + 1;
1115 }
1116 zCollSeqName = noCase ? "NOCASE" : "BINARY";
1117 pNewExpr1 = sqlite3ExprDup(db, pLeft, 0);
1118 pNewExpr1 = sqlite3PExpr(pParse, TK_GE,
1119 sqlite3ExprAddCollateString(pParse,pNewExpr1,zCollSeqName),
drhabfd35e2016-12-06 22:47:23 +00001120 pStr1);
drh6c1f4ef2015-06-08 14:23:15 +00001121 transferJoinMarkings(pNewExpr1, pExpr);
1122 idxNew1 = whereClauseInsert(pWC, pNewExpr1, wtFlags);
1123 testcase( idxNew1==0 );
1124 exprAnalyze(pSrc, pWC, idxNew1);
1125 pNewExpr2 = sqlite3ExprDup(db, pLeft, 0);
1126 pNewExpr2 = sqlite3PExpr(pParse, TK_LT,
1127 sqlite3ExprAddCollateString(pParse,pNewExpr2,zCollSeqName),
drhabfd35e2016-12-06 22:47:23 +00001128 pStr2);
drh6c1f4ef2015-06-08 14:23:15 +00001129 transferJoinMarkings(pNewExpr2, pExpr);
1130 idxNew2 = whereClauseInsert(pWC, pNewExpr2, wtFlags);
1131 testcase( idxNew2==0 );
1132 exprAnalyze(pSrc, pWC, idxNew2);
1133 pTerm = &pWC->a[idxTerm];
1134 if( isComplete ){
1135 markTermAsChild(pWC, idxNew1, idxTerm);
1136 markTermAsChild(pWC, idxNew2, idxTerm);
1137 }
1138 }
1139#endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
1140
1141#ifndef SQLITE_OMIT_VIRTUALTABLE
1142 /* Add a WO_MATCH auxiliary term to the constraint set if the
1143 ** current expression is of the form: column MATCH expr.
1144 ** This information is used by the xBestIndex methods of
1145 ** virtual tables. The native query optimizer does not attempt
1146 ** to do anything with MATCH functions.
1147 */
dandbd2dcb2016-05-28 18:53:55 +00001148 if( pWC->op==TK_AND && isMatchOfColumn(pExpr, &eOp2) ){
drh6c1f4ef2015-06-08 14:23:15 +00001149 int idxNew;
1150 Expr *pRight, *pLeft;
1151 WhereTerm *pNewTerm;
1152 Bitmask prereqColumn, prereqExpr;
1153
1154 pRight = pExpr->x.pList->a[0].pExpr;
1155 pLeft = pExpr->x.pList->a[1].pExpr;
1156 prereqExpr = sqlite3WhereExprUsage(pMaskSet, pRight);
1157 prereqColumn = sqlite3WhereExprUsage(pMaskSet, pLeft);
1158 if( (prereqExpr & prereqColumn)==0 ){
1159 Expr *pNewExpr;
1160 pNewExpr = sqlite3PExpr(pParse, TK_MATCH,
drhabfd35e2016-12-06 22:47:23 +00001161 0, sqlite3ExprDup(db, pRight, 0));
drh6c1f4ef2015-06-08 14:23:15 +00001162 idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
1163 testcase( idxNew==0 );
1164 pNewTerm = &pWC->a[idxNew];
1165 pNewTerm->prereqRight = prereqExpr;
1166 pNewTerm->leftCursor = pLeft->iTable;
1167 pNewTerm->u.leftColumn = pLeft->iColumn;
1168 pNewTerm->eOperator = WO_MATCH;
dan07bdba82015-11-23 21:09:54 +00001169 pNewTerm->eMatchOp = eOp2;
drh6c1f4ef2015-06-08 14:23:15 +00001170 markTermAsChild(pWC, idxNew, idxTerm);
1171 pTerm = &pWC->a[idxTerm];
1172 pTerm->wtFlags |= TERM_COPIED;
1173 pNewTerm->prereqAll = pTerm->prereqAll;
1174 }
1175 }
1176#endif /* SQLITE_OMIT_VIRTUALTABLE */
1177
dan95a08c02016-08-02 16:18:35 +00001178 /* If there is a vector == or IS term - e.g. "(a, b) == (?, ?)" - create
drh9e730f02016-08-20 12:00:05 +00001179 ** new terms for each component comparison - "a = ?" and "b = ?". The
1180 ** new terms completely replace the original vector comparison, which is
1181 ** no longer used.
1182 **
dan95a08c02016-08-02 16:18:35 +00001183 ** This is only required if at least one side of the comparison operation
1184 ** is not a sub-select. */
dan71c57db2016-07-09 20:23:55 +00001185 if( pWC->op==TK_AND
1186 && (pExpr->op==TK_EQ || pExpr->op==TK_IS)
dan625015e2016-07-30 16:39:28 +00001187 && sqlite3ExprIsVector(pExpr->pLeft)
dan71c57db2016-07-09 20:23:55 +00001188 && ( (pExpr->pLeft->flags & EP_xIsSelect)==0
1189 || (pExpr->pRight->flags & EP_xIsSelect)==0
1190 )){
dan19ff12d2016-07-29 20:58:19 +00001191 int nLeft = sqlite3ExprVectorSize(pExpr->pLeft);
drhb29e60c2016-09-05 12:02:34 +00001192 int i;
1193 assert( nLeft==sqlite3ExprVectorSize(pExpr->pRight) );
1194 for(i=0; i<nLeft; i++){
1195 int idxNew;
1196 Expr *pNew;
1197 Expr *pLeft = sqlite3ExprForVectorField(pParse, pExpr->pLeft, i);
1198 Expr *pRight = sqlite3ExprForVectorField(pParse, pExpr->pRight, i);
dan71c57db2016-07-09 20:23:55 +00001199
drhabfd35e2016-12-06 22:47:23 +00001200 pNew = sqlite3PExpr(pParse, pExpr->op, pLeft, pRight);
drhc52496f2016-10-27 01:02:20 +00001201 transferJoinMarkings(pNew, pExpr);
drhb29e60c2016-09-05 12:02:34 +00001202 idxNew = whereClauseInsert(pWC, pNew, TERM_DYNAMIC);
1203 exprAnalyze(pSrc, pWC, idxNew);
dan71c57db2016-07-09 20:23:55 +00001204 }
drhb29e60c2016-09-05 12:02:34 +00001205 pTerm = &pWC->a[idxTerm];
1206 pTerm->wtFlags = TERM_CODED|TERM_VIRTUAL; /* Disable the original */
1207 pTerm->eOperator = 0;
dan71c57db2016-07-09 20:23:55 +00001208 }
1209
dan95a08c02016-08-02 16:18:35 +00001210 /* If there is a vector IN term - e.g. "(a, b) IN (SELECT ...)" - create
1211 ** a virtual term for each vector component. The expression object
1212 ** used by each such virtual term is pExpr (the full vector IN(...)
1213 ** expression). The WhereTerm.iField variable identifies the index within
drh14318072016-09-06 18:51:25 +00001214 ** the vector on the LHS that the virtual term represents.
1215 **
1216 ** This only works if the RHS is a simple SELECT, not a compound
1217 */
dan8da209b2016-07-26 18:06:08 +00001218 if( pWC->op==TK_AND && pExpr->op==TK_IN && pTerm->iField==0
1219 && pExpr->pLeft->op==TK_VECTOR
drh14318072016-09-06 18:51:25 +00001220 && pExpr->x.pSelect->pPrior==0
dan8da209b2016-07-26 18:06:08 +00001221 ){
1222 int i;
1223 for(i=0; i<sqlite3ExprVectorSize(pExpr->pLeft); i++){
1224 int idxNew;
1225 idxNew = whereClauseInsert(pWC, pExpr, TERM_VIRTUAL);
1226 pWC->a[idxNew].iField = i+1;
1227 exprAnalyze(pSrc, pWC, idxNew);
1228 markTermAsChild(pWC, idxNew, idxTerm);
1229 }
1230 }
1231
drh6c1f4ef2015-06-08 14:23:15 +00001232#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
1233 /* When sqlite_stat3 histogram data is available an operator of the
1234 ** form "x IS NOT NULL" can sometimes be evaluated more efficiently
1235 ** as "x>NULL" if x is not an INTEGER PRIMARY KEY. So construct a
1236 ** virtual term of that form.
1237 **
1238 ** Note that the virtual term must be tagged with TERM_VNULL.
1239 */
1240 if( pExpr->op==TK_NOTNULL
1241 && pExpr->pLeft->op==TK_COLUMN
1242 && pExpr->pLeft->iColumn>=0
1243 && OptimizationEnabled(db, SQLITE_Stat34)
1244 ){
1245 Expr *pNewExpr;
1246 Expr *pLeft = pExpr->pLeft;
1247 int idxNew;
1248 WhereTerm *pNewTerm;
1249
1250 pNewExpr = sqlite3PExpr(pParse, TK_GT,
1251 sqlite3ExprDup(db, pLeft, 0),
drhabfd35e2016-12-06 22:47:23 +00001252 sqlite3ExprAlloc(db, TK_NULL, 0, 0));
drh6c1f4ef2015-06-08 14:23:15 +00001253
1254 idxNew = whereClauseInsert(pWC, pNewExpr,
1255 TERM_VIRTUAL|TERM_DYNAMIC|TERM_VNULL);
1256 if( idxNew ){
1257 pNewTerm = &pWC->a[idxNew];
1258 pNewTerm->prereqRight = 0;
1259 pNewTerm->leftCursor = pLeft->iTable;
1260 pNewTerm->u.leftColumn = pLeft->iColumn;
1261 pNewTerm->eOperator = WO_GT;
1262 markTermAsChild(pWC, idxNew, idxTerm);
1263 pTerm = &pWC->a[idxTerm];
1264 pTerm->wtFlags |= TERM_COPIED;
1265 pNewTerm->prereqAll = pTerm->prereqAll;
1266 }
1267 }
1268#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
1269
1270 /* Prevent ON clause terms of a LEFT JOIN from being used to drive
1271 ** an index for tables to the left of the join.
1272 */
drh0f85b2f2016-11-20 12:00:27 +00001273 testcase( pTerm!=&pWC->a[idxTerm] );
1274 pTerm = &pWC->a[idxTerm];
drh6c1f4ef2015-06-08 14:23:15 +00001275 pTerm->prereqRight |= extraRight;
1276}
1277
1278/***************************************************************************
1279** Routines with file scope above. Interface to the rest of the where.c
1280** subsystem follows.
1281***************************************************************************/
1282
1283/*
1284** This routine identifies subexpressions in the WHERE clause where
1285** each subexpression is separated by the AND operator or some other
1286** operator specified in the op parameter. The WhereClause structure
1287** is filled with pointers to subexpressions. For example:
1288**
1289** WHERE a=='hello' AND coalesce(b,11)<10 AND (c+12!=d OR c==22)
1290** \________/ \_______________/ \________________/
1291** slot[0] slot[1] slot[2]
1292**
1293** The original WHERE clause in pExpr is unaltered. All this routine
1294** does is make slot[] entries point to substructure within pExpr.
1295**
1296** In the previous sentence and in the diagram, "slot[]" refers to
1297** the WhereClause.a[] array. The slot[] array grows as needed to contain
1298** all terms of the WHERE clause.
1299*/
1300void sqlite3WhereSplit(WhereClause *pWC, Expr *pExpr, u8 op){
1301 Expr *pE2 = sqlite3ExprSkipCollate(pExpr);
1302 pWC->op = op;
1303 if( pE2==0 ) return;
1304 if( pE2->op!=op ){
1305 whereClauseInsert(pWC, pExpr, 0);
1306 }else{
1307 sqlite3WhereSplit(pWC, pE2->pLeft, op);
1308 sqlite3WhereSplit(pWC, pE2->pRight, op);
1309 }
1310}
1311
1312/*
1313** Initialize a preallocated WhereClause structure.
1314*/
1315void sqlite3WhereClauseInit(
1316 WhereClause *pWC, /* The WhereClause to be initialized */
1317 WhereInfo *pWInfo /* The WHERE processing context */
1318){
1319 pWC->pWInfo = pWInfo;
1320 pWC->pOuter = 0;
1321 pWC->nTerm = 0;
1322 pWC->nSlot = ArraySize(pWC->aStatic);
1323 pWC->a = pWC->aStatic;
1324}
1325
1326/*
1327** Deallocate a WhereClause structure. The WhereClause structure
drh62aaa6c2015-11-21 17:27:42 +00001328** itself is not freed. This routine is the inverse of
1329** sqlite3WhereClauseInit().
drh6c1f4ef2015-06-08 14:23:15 +00001330*/
1331void sqlite3WhereClauseClear(WhereClause *pWC){
1332 int i;
1333 WhereTerm *a;
1334 sqlite3 *db = pWC->pWInfo->pParse->db;
1335 for(i=pWC->nTerm-1, a=pWC->a; i>=0; i--, a++){
1336 if( a->wtFlags & TERM_DYNAMIC ){
1337 sqlite3ExprDelete(db, a->pExpr);
1338 }
1339 if( a->wtFlags & TERM_ORINFO ){
1340 whereOrInfoDelete(db, a->u.pOrInfo);
1341 }else if( a->wtFlags & TERM_ANDINFO ){
1342 whereAndInfoDelete(db, a->u.pAndInfo);
1343 }
1344 }
1345 if( pWC->a!=pWC->aStatic ){
1346 sqlite3DbFree(db, pWC->a);
1347 }
1348}
1349
1350
1351/*
1352** These routines walk (recursively) an expression tree and generate
1353** a bitmask indicating which tables are used in that expression
1354** tree.
1355*/
1356Bitmask sqlite3WhereExprUsage(WhereMaskSet *pMaskSet, Expr *p){
drh93ca3932016-08-10 20:02:21 +00001357 Bitmask mask;
drh6c1f4ef2015-06-08 14:23:15 +00001358 if( p==0 ) return 0;
1359 if( p->op==TK_COLUMN ){
1360 mask = sqlite3WhereGetMask(pMaskSet, p->iTable);
1361 return mask;
1362 }
drh93ca3932016-08-10 20:02:21 +00001363 assert( !ExprHasProperty(p, EP_TokenOnly) );
1364 mask = p->pRight ? sqlite3WhereExprUsage(pMaskSet, p->pRight) : 0;
drh926957f2016-04-12 00:00:33 +00001365 if( p->pLeft ) mask |= sqlite3WhereExprUsage(pMaskSet, p->pLeft);
drh6c1f4ef2015-06-08 14:23:15 +00001366 if( ExprHasProperty(p, EP_xIsSelect) ){
1367 mask |= exprSelectUsage(pMaskSet, p->x.pSelect);
drh926957f2016-04-12 00:00:33 +00001368 }else if( p->x.pList ){
drh6c1f4ef2015-06-08 14:23:15 +00001369 mask |= sqlite3WhereExprListUsage(pMaskSet, p->x.pList);
1370 }
1371 return mask;
1372}
1373Bitmask sqlite3WhereExprListUsage(WhereMaskSet *pMaskSet, ExprList *pList){
1374 int i;
1375 Bitmask mask = 0;
1376 if( pList ){
1377 for(i=0; i<pList->nExpr; i++){
1378 mask |= sqlite3WhereExprUsage(pMaskSet, pList->a[i].pExpr);
1379 }
1380 }
1381 return mask;
1382}
1383
1384
1385/*
1386** Call exprAnalyze on all terms in a WHERE clause.
1387**
1388** Note that exprAnalyze() might add new virtual terms onto the
1389** end of the WHERE clause. We do not want to analyze these new
1390** virtual terms, so start analyzing at the end and work forward
1391** so that the added virtual terms are never processed.
1392*/
1393void sqlite3WhereExprAnalyze(
1394 SrcList *pTabList, /* the FROM clause */
1395 WhereClause *pWC /* the WHERE clause to be analyzed */
1396){
1397 int i;
1398 for(i=pWC->nTerm-1; i>=0; i--){
1399 exprAnalyze(pTabList, pWC, i);
1400 }
1401}
drh01d230c2015-08-19 17:11:37 +00001402
1403/*
1404** For table-valued-functions, transform the function arguments into
1405** new WHERE clause terms.
1406**
1407** Each function argument translates into an equality constraint against
1408** a HIDDEN column in the table.
1409*/
1410void sqlite3WhereTabFuncArgs(
1411 Parse *pParse, /* Parsing context */
1412 struct SrcList_item *pItem, /* The FROM clause term to process */
1413 WhereClause *pWC /* Xfer function arguments to here */
1414){
1415 Table *pTab;
1416 int j, k;
1417 ExprList *pArgs;
1418 Expr *pColRef;
1419 Expr *pTerm;
1420 if( pItem->fg.isTabFunc==0 ) return;
1421 pTab = pItem->pTab;
1422 assert( pTab!=0 );
1423 pArgs = pItem->u1.pFuncArg;
drh20292312015-11-21 13:24:46 +00001424 if( pArgs==0 ) return;
drh01d230c2015-08-19 17:11:37 +00001425 for(j=k=0; j<pArgs->nExpr; j++){
drh62aaa6c2015-11-21 17:27:42 +00001426 while( k<pTab->nCol && (pTab->aCol[k].colFlags & COLFLAG_HIDDEN)==0 ){k++;}
drh01d230c2015-08-19 17:11:37 +00001427 if( k>=pTab->nCol ){
drhd8b1bfc2015-08-20 23:21:34 +00001428 sqlite3ErrorMsg(pParse, "too many arguments on %s() - max %d",
drh01d230c2015-08-19 17:11:37 +00001429 pTab->zName, j);
1430 return;
1431 }
drhe1c03b62016-09-23 20:59:31 +00001432 pColRef = sqlite3ExprAlloc(pParse->db, TK_COLUMN, 0, 0);
drh01d230c2015-08-19 17:11:37 +00001433 if( pColRef==0 ) return;
1434 pColRef->iTable = pItem->iCursor;
1435 pColRef->iColumn = k++;
drh1f2fc282015-08-21 17:14:48 +00001436 pColRef->pTab = pTab;
drh01d230c2015-08-19 17:11:37 +00001437 pTerm = sqlite3PExpr(pParse, TK_EQ, pColRef,
drhabfd35e2016-12-06 22:47:23 +00001438 sqlite3ExprDup(pParse->db, pArgs->a[j].pExpr, 0));
drh01d230c2015-08-19 17:11:37 +00001439 whereClauseInsert(pWC, pTerm, TERM_DYNAMIC);
1440 }
1441}