blob: 496c7a317676fd2657955f190f26c9fdf06d0775 [file] [log] [blame]
drhcce7d172000-05-31 15:34:51 +00001/*
drhb19a2bc2001-09-16 00:13:26 +00002** 2001 September 15
drhcce7d172000-05-31 15:34:51 +00003**
drhb19a2bc2001-09-16 00:13:26 +00004** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
drhcce7d172000-05-31 15:34:51 +00006**
drhb19a2bc2001-09-16 00:13:26 +00007** 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.
drhcce7d172000-05-31 15:34:51 +000010**
11*************************************************************************
drh1ccde152000-06-17 13:12:39 +000012** This file contains routines used for analyzing expressions and
drhb19a2bc2001-09-16 00:13:26 +000013** for generating VDBE code that evaluates expressions in SQLite.
drhcce7d172000-05-31 15:34:51 +000014*/
15#include "sqliteInt.h"
drha2e00042002-01-22 03:13:42 +000016
danielk1977e014a832004-05-17 10:48:57 +000017/*
18** Return the 'affinity' of the expression pExpr if any.
19**
20** If pExpr is a column, a reference to a column via an 'AS' alias,
21** or a sub-select with a column as the return value, then the
22** affinity of that column is returned. Otherwise, 0x00 is returned,
23** indicating no affinity for the expression.
24**
peter.d.reid60ec9142014-09-06 16:39:46 +000025** i.e. the WHERE clause expressions in the following statements all
danielk1977e014a832004-05-17 10:48:57 +000026** have an affinity:
27**
28** CREATE TABLE t1(a);
29** SELECT * FROM t1 WHERE a;
30** SELECT a AS b FROM t1 WHERE b;
31** SELECT * FROM t1 WHERE (select a from t1);
32*/
danielk1977bf3b7212004-05-18 10:06:24 +000033char sqlite3ExprAffinity(Expr *pExpr){
drh580c8c12012-12-08 03:34:04 +000034 int op;
35 pExpr = sqlite3ExprSkipCollate(pExpr);
mistachkin9bec6fb2014-06-26 21:28:21 +000036 if( pExpr->flags & EP_Generic ) return 0;
drh580c8c12012-12-08 03:34:04 +000037 op = pExpr->op;
drh487e2622005-06-25 18:42:14 +000038 if( op==TK_SELECT ){
danielk19776ab3a2e2009-02-19 14:39:25 +000039 assert( pExpr->flags&EP_xIsSelect );
40 return sqlite3ExprAffinity(pExpr->x.pSelect->pEList->a[0].pExpr);
danielk1977a37cdde2004-05-16 11:15:36 +000041 }
drh487e2622005-06-25 18:42:14 +000042#ifndef SQLITE_OMIT_CAST
43 if( op==TK_CAST ){
drh33e619f2009-05-28 01:00:55 +000044 assert( !ExprHasProperty(pExpr, EP_IntValue) );
drhfdaac672013-10-04 15:30:21 +000045 return sqlite3AffinityType(pExpr->u.zToken, 0);
drh487e2622005-06-25 18:42:14 +000046 }
47#endif
danielk1977259a4552008-11-12 08:07:12 +000048 if( (op==TK_AGG_COLUMN || op==TK_COLUMN || op==TK_REGISTER)
49 && pExpr->pTab!=0
50 ){
drh7d10d5a2008-08-20 16:35:10 +000051 /* op==TK_REGISTER && pExpr->pTab!=0 happens when pExpr was originally
52 ** a TK_COLUMN but was previously evaluated and cached in a register */
53 int j = pExpr->iColumn;
54 if( j<0 ) return SQLITE_AFF_INTEGER;
55 assert( pExpr->pTab && j<pExpr->pTab->nCol );
56 return pExpr->pTab->aCol[j].affinity;
57 }
danielk1977a37cdde2004-05-16 11:15:36 +000058 return pExpr->affinity;
59}
60
drh53db1452004-05-20 13:54:53 +000061/*
drh8b4c40d2007-02-01 23:02:45 +000062** Set the collating sequence for expression pExpr to be the collating
drhae80dde2012-12-06 21:16:43 +000063** sequence named by pToken. Return a pointer to a new Expr node that
64** implements the COLLATE operator.
drh0a8a4062012-12-07 18:38:16 +000065**
66** If a memory allocation error occurs, that fact is recorded in pParse->db
67** and the pExpr parameter is returned unchanged.
drh8b4c40d2007-02-01 23:02:45 +000068*/
drh4ef7efa2014-03-20 15:14:08 +000069Expr *sqlite3ExprAddCollateToken(
70 Parse *pParse, /* Parsing context */
71 Expr *pExpr, /* Add the "COLLATE" clause to this expression */
72 const Token *pCollName /* Name of collating sequence */
73){
drh0a8a4062012-12-07 18:38:16 +000074 if( pCollName->n>0 ){
75 Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLLATE, pCollName, 1);
76 if( pNew ){
77 pNew->pLeft = pExpr;
drha4c3c872013-09-12 17:29:25 +000078 pNew->flags |= EP_Collate|EP_Skip;
drh0a8a4062012-12-07 18:38:16 +000079 pExpr = pNew;
80 }
drhae80dde2012-12-06 21:16:43 +000081 }
drh0a8a4062012-12-07 18:38:16 +000082 return pExpr;
83}
84Expr *sqlite3ExprAddCollateString(Parse *pParse, Expr *pExpr, const char *zC){
drh261d8a52012-12-08 21:36:26 +000085 Token s;
86 assert( zC!=0 );
87 s.z = zC;
88 s.n = sqlite3Strlen30(s.z);
89 return sqlite3ExprAddCollateToken(pParse, pExpr, &s);
drh0a8a4062012-12-07 18:38:16 +000090}
91
92/*
drha4c3c872013-09-12 17:29:25 +000093** Skip over any TK_COLLATE or TK_AS operators and any unlikely()
94** or likelihood() function at the root of an expression.
drh0a8a4062012-12-07 18:38:16 +000095*/
96Expr *sqlite3ExprSkipCollate(Expr *pExpr){
drha4c3c872013-09-12 17:29:25 +000097 while( pExpr && ExprHasProperty(pExpr, EP_Skip) ){
98 if( ExprHasProperty(pExpr, EP_Unlikely) ){
drhcca9f3d2013-09-06 15:23:29 +000099 assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
100 assert( pExpr->x.pList->nExpr>0 );
drha4c3c872013-09-12 17:29:25 +0000101 assert( pExpr->op==TK_FUNCTION );
drhcca9f3d2013-09-06 15:23:29 +0000102 pExpr = pExpr->x.pList->a[0].pExpr;
103 }else{
drha4c3c872013-09-12 17:29:25 +0000104 assert( pExpr->op==TK_COLLATE || pExpr->op==TK_AS );
105 pExpr = pExpr->pLeft;
drhcca9f3d2013-09-06 15:23:29 +0000106 }
drha4c3c872013-09-12 17:29:25 +0000107 }
drh0a8a4062012-12-07 18:38:16 +0000108 return pExpr;
drh8b4c40d2007-02-01 23:02:45 +0000109}
110
111/*
drhae80dde2012-12-06 21:16:43 +0000112** Return the collation sequence for the expression pExpr. If
113** there is no defined collating sequence, return NULL.
114**
115** The collating sequence might be determined by a COLLATE operator
116** or by the presence of a column with a defined collating sequence.
117** COLLATE operators take first precedence. Left operands take
118** precedence over right operands.
danielk19770202b292004-06-09 09:55:16 +0000119*/
danielk19777cedc8d2004-06-10 10:50:08 +0000120CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
drhae80dde2012-12-06 21:16:43 +0000121 sqlite3 *db = pParse->db;
danielk19777cedc8d2004-06-10 10:50:08 +0000122 CollSeq *pColl = 0;
drh7d10d5a2008-08-20 16:35:10 +0000123 Expr *p = pExpr;
drh261d8a52012-12-08 21:36:26 +0000124 while( p ){
drhae80dde2012-12-06 21:16:43 +0000125 int op = p->op;
drhfbb24d12014-03-20 17:03:30 +0000126 if( p->flags & EP_Generic ) break;
drhae80dde2012-12-06 21:16:43 +0000127 if( op==TK_CAST || op==TK_UPLUS ){
128 p = p->pLeft;
129 continue;
130 }
dan36e78302013-08-21 12:04:32 +0000131 if( op==TK_COLLATE || (op==TK_REGISTER && p->op2==TK_COLLATE) ){
drh6fdd3d82013-05-15 17:47:12 +0000132 pColl = sqlite3GetCollSeq(pParse, ENC(db), 0, p->u.zToken);
drhae80dde2012-12-06 21:16:43 +0000133 break;
134 }
drha58d4a92015-01-27 13:17:05 +0000135 if( (op==TK_AGG_COLUMN || op==TK_COLUMN
drhae80dde2012-12-06 21:16:43 +0000136 || op==TK_REGISTER || op==TK_TRIGGER)
drha58d4a92015-01-27 13:17:05 +0000137 && p->pTab!=0
drhae80dde2012-12-06 21:16:43 +0000138 ){
drh7d10d5a2008-08-20 16:35:10 +0000139 /* op==TK_REGISTER && p->pTab!=0 happens when pExpr was originally
140 ** a TK_COLUMN but was previously evaluated and cached in a register */
drh7d10d5a2008-08-20 16:35:10 +0000141 int j = p->iColumn;
142 if( j>=0 ){
drhae80dde2012-12-06 21:16:43 +0000143 const char *zColl = p->pTab->aCol[j].zColl;
drhc4a64fa2009-05-11 20:53:28 +0000144 pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0);
drh7d10d5a2008-08-20 16:35:10 +0000145 }
146 break;
danielk19770202b292004-06-09 09:55:16 +0000147 }
drhae80dde2012-12-06 21:16:43 +0000148 if( p->flags & EP_Collate ){
drh2308ed32015-02-09 16:09:34 +0000149 if( p->pLeft && (p->pLeft->flags & EP_Collate)!=0 ){
drhae80dde2012-12-06 21:16:43 +0000150 p = p->pLeft;
151 }else{
drh2308ed32015-02-09 16:09:34 +0000152 Expr *pNext = p->pRight;
153 if( p->x.pList!=0 && !ExprHasProperty(p, EP_xIsSelect) ){
154 int i;
155 for(i=0; i<p->x.pList->nExpr; i++){
156 if( ExprHasProperty(p->x.pList->a[i].pExpr, EP_Collate) ){
157 pNext = p->x.pList->a[i].pExpr;
158 break;
159 }
160 }
161 }
162 p = pNext;
drhae80dde2012-12-06 21:16:43 +0000163 }
164 }else{
drh7d10d5a2008-08-20 16:35:10 +0000165 break;
166 }
danielk19770202b292004-06-09 09:55:16 +0000167 }
danielk19777cedc8d2004-06-10 10:50:08 +0000168 if( sqlite3CheckCollSeq(pParse, pColl) ){
169 pColl = 0;
170 }
171 return pColl;
danielk19770202b292004-06-09 09:55:16 +0000172}
173
174/*
drh626a8792005-01-17 22:08:19 +0000175** pExpr is an operand of a comparison operator. aff2 is the
176** type affinity of the other operand. This routine returns the
drh53db1452004-05-20 13:54:53 +0000177** type affinity that should be used for the comparison operator.
178*/
danielk1977e014a832004-05-17 10:48:57 +0000179char sqlite3CompareAffinity(Expr *pExpr, char aff2){
danielk1977bf3b7212004-05-18 10:06:24 +0000180 char aff1 = sqlite3ExprAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +0000181 if( aff1 && aff2 ){
drh8df447f2005-11-01 15:48:24 +0000182 /* Both sides of the comparison are columns. If one has numeric
183 ** affinity, use that. Otherwise use no affinity.
danielk1977e014a832004-05-17 10:48:57 +0000184 */
drh8a512562005-11-14 22:29:05 +0000185 if( sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2) ){
danielk1977e014a832004-05-17 10:48:57 +0000186 return SQLITE_AFF_NUMERIC;
187 }else{
188 return SQLITE_AFF_NONE;
189 }
190 }else if( !aff1 && !aff2 ){
drh5f6a87b2004-07-19 00:39:45 +0000191 /* Neither side of the comparison is a column. Compare the
192 ** results directly.
danielk1977e014a832004-05-17 10:48:57 +0000193 */
drh5f6a87b2004-07-19 00:39:45 +0000194 return SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +0000195 }else{
196 /* One side is a column, the other is not. Use the columns affinity. */
drhfe05af82005-07-21 03:14:59 +0000197 assert( aff1==0 || aff2==0 );
danielk1977e014a832004-05-17 10:48:57 +0000198 return (aff1 + aff2);
199 }
200}
201
drh53db1452004-05-20 13:54:53 +0000202/*
203** pExpr is a comparison operator. Return the type affinity that should
204** be applied to both operands prior to doing the comparison.
205*/
danielk1977e014a832004-05-17 10:48:57 +0000206static char comparisonAffinity(Expr *pExpr){
207 char aff;
208 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
209 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
drh6a2fe092009-09-23 02:29:36 +0000210 pExpr->op==TK_NE || pExpr->op==TK_IS || pExpr->op==TK_ISNOT );
danielk1977e014a832004-05-17 10:48:57 +0000211 assert( pExpr->pLeft );
danielk1977bf3b7212004-05-18 10:06:24 +0000212 aff = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +0000213 if( pExpr->pRight ){
214 aff = sqlite3CompareAffinity(pExpr->pRight, aff);
danielk19776ab3a2e2009-02-19 14:39:25 +0000215 }else if( ExprHasProperty(pExpr, EP_xIsSelect) ){
216 aff = sqlite3CompareAffinity(pExpr->x.pSelect->pEList->a[0].pExpr, aff);
217 }else if( !aff ){
drhde087bd2007-02-23 03:00:44 +0000218 aff = SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +0000219 }
220 return aff;
221}
222
223/*
224** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
225** idx_affinity is the affinity of an indexed column. Return true
226** if the index with affinity idx_affinity may be used to implement
227** the comparison in pExpr.
228*/
229int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){
230 char aff = comparisonAffinity(pExpr);
drh8a512562005-11-14 22:29:05 +0000231 switch( aff ){
232 case SQLITE_AFF_NONE:
233 return 1;
234 case SQLITE_AFF_TEXT:
235 return idx_affinity==SQLITE_AFF_TEXT;
236 default:
237 return sqlite3IsNumericAffinity(idx_affinity);
238 }
danielk1977e014a832004-05-17 10:48:57 +0000239}
240
danielk1977a37cdde2004-05-16 11:15:36 +0000241/*
drh35573352008-01-08 23:54:25 +0000242** Return the P5 value that should be used for a binary comparison
danielk1977a37cdde2004-05-16 11:15:36 +0000243** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
danielk1977a37cdde2004-05-16 11:15:36 +0000244*/
drh35573352008-01-08 23:54:25 +0000245static u8 binaryCompareP5(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){
246 u8 aff = (char)sqlite3ExprAffinity(pExpr2);
drh1bd10f82008-12-10 21:19:56 +0000247 aff = (u8)sqlite3CompareAffinity(pExpr1, aff) | (u8)jumpIfNull;
drh35573352008-01-08 23:54:25 +0000248 return aff;
danielk1977a37cdde2004-05-16 11:15:36 +0000249}
250
drha2e00042002-01-22 03:13:42 +0000251/*
danielk19770202b292004-06-09 09:55:16 +0000252** Return a pointer to the collation sequence that should be used by
253** a binary comparison operator comparing pLeft and pRight.
254**
255** If the left hand expression has a collating sequence type, then it is
256** used. Otherwise the collation sequence for the right hand expression
257** is used, or the default (BINARY) if neither expression has a collating
258** type.
danielk1977bcbb04e2007-05-29 12:11:29 +0000259**
260** Argument pRight (but not pLeft) may be a null pointer. In this case,
261** it is not considered.
danielk19770202b292004-06-09 09:55:16 +0000262*/
drh0a0e1312007-08-07 17:04:59 +0000263CollSeq *sqlite3BinaryCompareCollSeq(
danielk1977bcbb04e2007-05-29 12:11:29 +0000264 Parse *pParse,
265 Expr *pLeft,
266 Expr *pRight
267){
drhec41dda2007-02-07 13:09:45 +0000268 CollSeq *pColl;
269 assert( pLeft );
drhae80dde2012-12-06 21:16:43 +0000270 if( pLeft->flags & EP_Collate ){
271 pColl = sqlite3ExprCollSeq(pParse, pLeft);
272 }else if( pRight && (pRight->flags & EP_Collate)!=0 ){
273 pColl = sqlite3ExprCollSeq(pParse, pRight);
drhec41dda2007-02-07 13:09:45 +0000274 }else{
275 pColl = sqlite3ExprCollSeq(pParse, pLeft);
276 if( !pColl ){
277 pColl = sqlite3ExprCollSeq(pParse, pRight);
278 }
danielk19770202b292004-06-09 09:55:16 +0000279 }
280 return pColl;
281}
282
283/*
drhbe5c89a2004-07-26 00:31:09 +0000284** Generate code for a comparison operator.
285*/
286static int codeCompare(
287 Parse *pParse, /* The parsing (and code generating) context */
288 Expr *pLeft, /* The left operand */
289 Expr *pRight, /* The right operand */
290 int opcode, /* The comparison opcode */
drh35573352008-01-08 23:54:25 +0000291 int in1, int in2, /* Register holding operands */
drhbe5c89a2004-07-26 00:31:09 +0000292 int dest, /* Jump here if true. */
293 int jumpIfNull /* If true, jump if either operand is NULL */
294){
drh35573352008-01-08 23:54:25 +0000295 int p5;
296 int addr;
297 CollSeq *p4;
298
299 p4 = sqlite3BinaryCompareCollSeq(pParse, pLeft, pRight);
300 p5 = binaryCompareP5(pLeft, pRight, jumpIfNull);
301 addr = sqlite3VdbeAddOp4(pParse->pVdbe, opcode, in2, dest, in1,
302 (void*)p4, P4_COLLSEQ);
drh1bd10f82008-12-10 21:19:56 +0000303 sqlite3VdbeChangeP5(pParse->pVdbe, (u8)p5);
drh35573352008-01-08 23:54:25 +0000304 return addr;
drhbe5c89a2004-07-26 00:31:09 +0000305}
306
danielk19774b5255a2008-06-05 16:47:39 +0000307#if SQLITE_MAX_EXPR_DEPTH>0
308/*
309** Check that argument nHeight is less than or equal to the maximum
310** expression depth allowed. If it is not, leave an error message in
311** pParse.
312*/
drh7d10d5a2008-08-20 16:35:10 +0000313int sqlite3ExprCheckHeight(Parse *pParse, int nHeight){
danielk19774b5255a2008-06-05 16:47:39 +0000314 int rc = SQLITE_OK;
315 int mxHeight = pParse->db->aLimit[SQLITE_LIMIT_EXPR_DEPTH];
316 if( nHeight>mxHeight ){
317 sqlite3ErrorMsg(pParse,
318 "Expression tree is too large (maximum depth %d)", mxHeight
319 );
320 rc = SQLITE_ERROR;
321 }
322 return rc;
323}
324
325/* The following three functions, heightOfExpr(), heightOfExprList()
326** and heightOfSelect(), are used to determine the maximum height
327** of any expression tree referenced by the structure passed as the
328** first argument.
329**
330** If this maximum height is greater than the current value pointed
331** to by pnHeight, the second parameter, then set *pnHeight to that
332** value.
333*/
334static void heightOfExpr(Expr *p, int *pnHeight){
335 if( p ){
336 if( p->nHeight>*pnHeight ){
337 *pnHeight = p->nHeight;
338 }
339 }
340}
341static void heightOfExprList(ExprList *p, int *pnHeight){
342 if( p ){
343 int i;
344 for(i=0; i<p->nExpr; i++){
345 heightOfExpr(p->a[i].pExpr, pnHeight);
346 }
347 }
348}
349static void heightOfSelect(Select *p, int *pnHeight){
350 if( p ){
351 heightOfExpr(p->pWhere, pnHeight);
352 heightOfExpr(p->pHaving, pnHeight);
353 heightOfExpr(p->pLimit, pnHeight);
354 heightOfExpr(p->pOffset, pnHeight);
355 heightOfExprList(p->pEList, pnHeight);
356 heightOfExprList(p->pGroupBy, pnHeight);
357 heightOfExprList(p->pOrderBy, pnHeight);
358 heightOfSelect(p->pPrior, pnHeight);
359 }
360}
361
362/*
363** Set the Expr.nHeight variable in the structure passed as an
364** argument. An expression with no children, Expr.pList or
365** Expr.pSelect member has a height of 1. Any other expression
366** has a height equal to the maximum height of any other
367** referenced Expr plus one.
drh2308ed32015-02-09 16:09:34 +0000368**
369** Also propagate EP_Propagate flags up from Expr.x.pList to Expr.flags,
370** if appropriate.
danielk19774b5255a2008-06-05 16:47:39 +0000371*/
372static void exprSetHeight(Expr *p){
373 int nHeight = 0;
374 heightOfExpr(p->pLeft, &nHeight);
375 heightOfExpr(p->pRight, &nHeight);
danielk19776ab3a2e2009-02-19 14:39:25 +0000376 if( ExprHasProperty(p, EP_xIsSelect) ){
377 heightOfSelect(p->x.pSelect, &nHeight);
drh2308ed32015-02-09 16:09:34 +0000378 }else if( p->x.pList ){
danielk19776ab3a2e2009-02-19 14:39:25 +0000379 heightOfExprList(p->x.pList, &nHeight);
drh2308ed32015-02-09 16:09:34 +0000380 p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList);
danielk19776ab3a2e2009-02-19 14:39:25 +0000381 }
danielk19774b5255a2008-06-05 16:47:39 +0000382 p->nHeight = nHeight + 1;
383}
384
385/*
386** Set the Expr.nHeight variable using the exprSetHeight() function. If
387** the height is greater than the maximum allowed expression depth,
388** leave an error in pParse.
drh2308ed32015-02-09 16:09:34 +0000389**
390** Also propagate all EP_Propagate flags from the Expr.x.pList into
391** Expr.flags.
danielk19774b5255a2008-06-05 16:47:39 +0000392*/
drh2308ed32015-02-09 16:09:34 +0000393void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p){
danielk19774b5255a2008-06-05 16:47:39 +0000394 exprSetHeight(p);
drh7d10d5a2008-08-20 16:35:10 +0000395 sqlite3ExprCheckHeight(pParse, p->nHeight);
danielk19774b5255a2008-06-05 16:47:39 +0000396}
397
398/*
399** Return the maximum height of any expression tree referenced
400** by the select statement passed as an argument.
401*/
402int sqlite3SelectExprHeight(Select *p){
403 int nHeight = 0;
404 heightOfSelect(p, &nHeight);
405 return nHeight;
406}
drh2308ed32015-02-09 16:09:34 +0000407#else /* ABOVE: Height enforcement enabled. BELOW: Height enforcement off */
408/*
409** Propagate all EP_Propagate flags from the Expr.x.pList into
410** Expr.flags.
411*/
412void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p){
413 if( p && p->x.pList && !ExprHasProperty(p, EP_xIsSelect) ){
414 p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList);
415 }
416}
417#define exprSetHeight(y)
danielk19774b5255a2008-06-05 16:47:39 +0000418#endif /* SQLITE_MAX_EXPR_DEPTH>0 */
419
drhbe5c89a2004-07-26 00:31:09 +0000420/*
drhb7916a72009-05-27 10:31:29 +0000421** This routine is the core allocator for Expr nodes.
422**
drha76b5df2002-02-23 02:32:10 +0000423** Construct a new expression node and return a pointer to it. Memory
drhb7916a72009-05-27 10:31:29 +0000424** for this node and for the pToken argument is a single allocation
425** obtained from sqlite3DbMalloc(). The calling function
drha76b5df2002-02-23 02:32:10 +0000426** is responsible for making sure the node eventually gets freed.
drhb7916a72009-05-27 10:31:29 +0000427**
428** If dequote is true, then the token (if it exists) is dequoted.
429** If dequote is false, no dequoting is performance. The deQuote
430** parameter is ignored if pToken is NULL or if the token does not
431** appear to be quoted. If the quotes were of the form "..." (double-quotes)
432** then the EP_DblQuoted flag is set on the expression node.
drh33e619f2009-05-28 01:00:55 +0000433**
434** Special case: If op==TK_INTEGER and pToken points to a string that
435** can be translated into a 32-bit integer, then the token is not
436** stored in u.zToken. Instead, the integer values is written
437** into u.iValue and the EP_IntValue flag is set. No extra storage
438** is allocated to hold the integer text and the dequote flag is ignored.
drha76b5df2002-02-23 02:32:10 +0000439*/
drhb7916a72009-05-27 10:31:29 +0000440Expr *sqlite3ExprAlloc(
danielk1977a1644fd2007-08-29 12:31:25 +0000441 sqlite3 *db, /* Handle for sqlite3DbMallocZero() (may be null) */
drh17435752007-08-16 04:30:38 +0000442 int op, /* Expression opcode */
drhb7916a72009-05-27 10:31:29 +0000443 const Token *pToken, /* Token argument. Might be NULL */
444 int dequote /* True to dequote */
drh17435752007-08-16 04:30:38 +0000445){
drha76b5df2002-02-23 02:32:10 +0000446 Expr *pNew;
drh33e619f2009-05-28 01:00:55 +0000447 int nExtra = 0;
shanecf697392009-06-01 16:53:09 +0000448 int iValue = 0;
danielk1977fc976062007-05-10 10:46:56 +0000449
drhb7916a72009-05-27 10:31:29 +0000450 if( pToken ){
drh33e619f2009-05-28 01:00:55 +0000451 if( op!=TK_INTEGER || pToken->z==0
452 || sqlite3GetInt32(pToken->z, &iValue)==0 ){
453 nExtra = pToken->n+1;
drhd50ffc42011-03-08 02:38:28 +0000454 assert( iValue>=0 );
drh33e619f2009-05-28 01:00:55 +0000455 }
drhb7916a72009-05-27 10:31:29 +0000456 }
457 pNew = sqlite3DbMallocZero(db, sizeof(Expr)+nExtra);
458 if( pNew ){
459 pNew->op = (u8)op;
460 pNew->iAgg = -1;
461 if( pToken ){
drh33e619f2009-05-28 01:00:55 +0000462 if( nExtra==0 ){
463 pNew->flags |= EP_IntValue;
464 pNew->u.iValue = iValue;
465 }else{
466 int c;
467 pNew->u.zToken = (char*)&pNew[1];
drhb07028f2011-10-14 21:49:18 +0000468 assert( pToken->z!=0 || pToken->n==0 );
469 if( pToken->n ) memcpy(pNew->u.zToken, pToken->z, pToken->n);
drh33e619f2009-05-28 01:00:55 +0000470 pNew->u.zToken[pToken->n] = 0;
471 if( dequote && nExtra>=3
472 && ((c = pToken->z[0])=='\'' || c=='"' || c=='[' || c=='`') ){
473 sqlite3Dequote(pNew->u.zToken);
474 if( c=='"' ) pNew->flags |= EP_DblQuoted;
475 }
drhb7916a72009-05-27 10:31:29 +0000476 }
477 }
478#if SQLITE_MAX_EXPR_DEPTH>0
479 pNew->nHeight = 1;
480#endif
481 }
drha76b5df2002-02-23 02:32:10 +0000482 return pNew;
483}
484
485/*
drhb7916a72009-05-27 10:31:29 +0000486** Allocate a new expression node from a zero-terminated token that has
487** already been dequoted.
488*/
489Expr *sqlite3Expr(
490 sqlite3 *db, /* Handle for sqlite3DbMallocZero() (may be null) */
491 int op, /* Expression opcode */
492 const char *zToken /* Token argument. Might be NULL */
493){
494 Token x;
495 x.z = zToken;
496 x.n = zToken ? sqlite3Strlen30(zToken) : 0;
497 return sqlite3ExprAlloc(db, op, &x, 0);
498}
499
500/*
501** Attach subtrees pLeft and pRight to the Expr node pRoot.
502**
503** If pRoot==NULL that means that a memory allocation error has occurred.
504** In that case, delete the subtrees pLeft and pRight.
505*/
506void sqlite3ExprAttachSubtrees(
507 sqlite3 *db,
508 Expr *pRoot,
509 Expr *pLeft,
510 Expr *pRight
511){
512 if( pRoot==0 ){
513 assert( db->mallocFailed );
514 sqlite3ExprDelete(db, pLeft);
515 sqlite3ExprDelete(db, pRight);
516 }else{
517 if( pRight ){
518 pRoot->pRight = pRight;
drh885a5b02015-02-09 15:21:36 +0000519 pRoot->flags |= EP_Propagate & pRight->flags;
drhb7916a72009-05-27 10:31:29 +0000520 }
521 if( pLeft ){
522 pRoot->pLeft = pLeft;
drh885a5b02015-02-09 15:21:36 +0000523 pRoot->flags |= EP_Propagate & pLeft->flags;
drhb7916a72009-05-27 10:31:29 +0000524 }
525 exprSetHeight(pRoot);
526 }
527}
528
529/*
peter.d.reid60ec9142014-09-06 16:39:46 +0000530** Allocate an Expr node which joins as many as two subtrees.
drhb7916a72009-05-27 10:31:29 +0000531**
drhbf664462009-06-19 18:32:54 +0000532** One or both of the subtrees can be NULL. Return a pointer to the new
533** Expr node. Or, if an OOM error occurs, set pParse->db->mallocFailed,
534** free the subtrees and return NULL.
drh206f3d92006-07-11 13:15:08 +0000535*/
drh17435752007-08-16 04:30:38 +0000536Expr *sqlite3PExpr(
537 Parse *pParse, /* Parsing context */
538 int op, /* Expression opcode */
539 Expr *pLeft, /* Left operand */
540 Expr *pRight, /* Right operand */
541 const Token *pToken /* Argument token */
542){
drh5fb52ca2012-03-31 02:34:35 +0000543 Expr *p;
drh655814d2015-01-09 01:27:29 +0000544 if( op==TK_AND && pLeft && pRight && pParse->nErr==0 ){
drh5fb52ca2012-03-31 02:34:35 +0000545 /* Take advantage of short-circuit false optimization for AND */
546 p = sqlite3ExprAnd(pParse->db, pLeft, pRight);
547 }else{
548 p = sqlite3ExprAlloc(pParse->db, op, pToken, 1);
549 sqlite3ExprAttachSubtrees(pParse->db, p, pLeft, pRight);
550 }
dan2b359bd2010-10-28 11:31:23 +0000551 if( p ) {
552 sqlite3ExprCheckHeight(pParse, p->nHeight);
553 }
drh4e0cff62004-11-05 05:10:28 +0000554 return p;
555}
556
557/*
drh991a1982014-01-02 17:57:16 +0000558** If the expression is always either TRUE or FALSE (respectively),
559** then return 1. If one cannot determine the truth value of the
560** expression at compile-time return 0.
561**
562** This is an optimization. If is OK to return 0 here even if
563** the expression really is always false or false (a false negative).
564** But it is a bug to return 1 if the expression might have different
565** boolean values in different circumstances (a false positive.)
drh5fb52ca2012-03-31 02:34:35 +0000566**
567** Note that if the expression is part of conditional for a
568** LEFT JOIN, then we cannot determine at compile-time whether or not
569** is it true or false, so always return 0.
570*/
drh991a1982014-01-02 17:57:16 +0000571static int exprAlwaysTrue(Expr *p){
572 int v = 0;
573 if( ExprHasProperty(p, EP_FromJoin) ) return 0;
574 if( !sqlite3ExprIsInteger(p, &v) ) return 0;
575 return v!=0;
576}
drh5fb52ca2012-03-31 02:34:35 +0000577static int exprAlwaysFalse(Expr *p){
578 int v = 0;
579 if( ExprHasProperty(p, EP_FromJoin) ) return 0;
580 if( !sqlite3ExprIsInteger(p, &v) ) return 0;
581 return v==0;
582}
583
584/*
drh91bb0ee2004-09-01 03:06:34 +0000585** Join two expressions using an AND operator. If either expression is
586** NULL, then just return the other expression.
drh5fb52ca2012-03-31 02:34:35 +0000587**
588** If one side or the other of the AND is known to be false, then instead
589** of returning an AND expression, just return a constant expression with
590** a value of false.
drh91bb0ee2004-09-01 03:06:34 +0000591*/
danielk19771e536952007-08-16 10:09:01 +0000592Expr *sqlite3ExprAnd(sqlite3 *db, Expr *pLeft, Expr *pRight){
drh91bb0ee2004-09-01 03:06:34 +0000593 if( pLeft==0 ){
594 return pRight;
595 }else if( pRight==0 ){
596 return pLeft;
drh5fb52ca2012-03-31 02:34:35 +0000597 }else if( exprAlwaysFalse(pLeft) || exprAlwaysFalse(pRight) ){
598 sqlite3ExprDelete(db, pLeft);
599 sqlite3ExprDelete(db, pRight);
600 return sqlite3ExprAlloc(db, TK_INTEGER, &sqlite3IntTokens[0], 0);
drh91bb0ee2004-09-01 03:06:34 +0000601 }else{
drhb7916a72009-05-27 10:31:29 +0000602 Expr *pNew = sqlite3ExprAlloc(db, TK_AND, 0, 0);
603 sqlite3ExprAttachSubtrees(db, pNew, pLeft, pRight);
604 return pNew;
drha76b5df2002-02-23 02:32:10 +0000605 }
606}
607
608/*
609** Construct a new expression node for a function with multiple
610** arguments.
611*/
drh17435752007-08-16 04:30:38 +0000612Expr *sqlite3ExprFunction(Parse *pParse, ExprList *pList, Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000613 Expr *pNew;
drh633e6d52008-07-28 19:34:53 +0000614 sqlite3 *db = pParse->db;
danielk19774b202ae2006-01-23 05:50:58 +0000615 assert( pToken );
drhb7916a72009-05-27 10:31:29 +0000616 pNew = sqlite3ExprAlloc(db, TK_FUNCTION, pToken, 1);
drha76b5df2002-02-23 02:32:10 +0000617 if( pNew==0 ){
drhd9da78a2009-03-24 15:08:09 +0000618 sqlite3ExprListDelete(db, pList); /* Avoid memory leak when malloc fails */
drha76b5df2002-02-23 02:32:10 +0000619 return 0;
620 }
danielk19776ab3a2e2009-02-19 14:39:25 +0000621 pNew->x.pList = pList;
622 assert( !ExprHasProperty(pNew, EP_xIsSelect) );
drh2308ed32015-02-09 16:09:34 +0000623 sqlite3ExprSetHeightAndFlags(pParse, pNew);
drha76b5df2002-02-23 02:32:10 +0000624 return pNew;
625}
626
627/*
drhfa6bc002004-09-07 16:19:52 +0000628** Assign a variable number to an expression that encodes a wildcard
629** in the original SQL statement.
630**
631** Wildcards consisting of a single "?" are assigned the next sequential
632** variable number.
633**
634** Wildcards of the form "?nnn" are assigned the number "nnn". We make
635** sure "nnn" is not too be to avoid a denial of service attack when
636** the SQL statement comes from an external source.
637**
drh51f49f12009-05-21 20:41:32 +0000638** Wildcards of the form ":aaa", "@aaa", or "$aaa" are assigned the same number
drhfa6bc002004-09-07 16:19:52 +0000639** as the previous instance of the same wildcard. Or if this is the first
peter.d.reid60ec9142014-09-06 16:39:46 +0000640** instance of the wildcard, the next sequential variable number is
drhfa6bc002004-09-07 16:19:52 +0000641** assigned.
642*/
643void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){
drh17435752007-08-16 04:30:38 +0000644 sqlite3 *db = pParse->db;
drhb7916a72009-05-27 10:31:29 +0000645 const char *z;
drh17435752007-08-16 04:30:38 +0000646
drhfa6bc002004-09-07 16:19:52 +0000647 if( pExpr==0 ) return;
drhc5cd1242013-09-12 16:50:49 +0000648 assert( !ExprHasProperty(pExpr, EP_IntValue|EP_Reduced|EP_TokenOnly) );
drh33e619f2009-05-28 01:00:55 +0000649 z = pExpr->u.zToken;
drhb7916a72009-05-27 10:31:29 +0000650 assert( z!=0 );
651 assert( z[0]!=0 );
652 if( z[1]==0 ){
drhfa6bc002004-09-07 16:19:52 +0000653 /* Wildcard of the form "?". Assign the next variable number */
drhb7916a72009-05-27 10:31:29 +0000654 assert( z[0]=='?' );
drh8677d302009-11-04 13:17:14 +0000655 pExpr->iColumn = (ynVar)(++pParse->nVar);
drhfa6bc002004-09-07 16:19:52 +0000656 }else{
drh124c0b42011-06-01 18:15:55 +0000657 ynVar x = 0;
658 u32 n = sqlite3Strlen30(z);
659 if( z[0]=='?' ){
660 /* Wildcard of the form "?nnn". Convert "nnn" to an integer and
661 ** use it as the variable number */
662 i64 i;
663 int bOk = 0==sqlite3Atoi64(&z[1], &i, n-1, SQLITE_UTF8);
664 pExpr->iColumn = x = (ynVar)i;
665 testcase( i==0 );
666 testcase( i==1 );
667 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 );
668 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] );
669 if( bOk==0 || i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
670 sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
671 db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]);
672 x = 0;
drhfa6bc002004-09-07 16:19:52 +0000673 }
drh124c0b42011-06-01 18:15:55 +0000674 if( i>pParse->nVar ){
675 pParse->nVar = (int)i;
676 }
677 }else{
678 /* Wildcards like ":aaa", "$aaa" or "@aaa". Reuse the same variable
679 ** number as the prior appearance of the same name, or if the name
680 ** has never appeared before, reuse the same variable number
681 */
682 ynVar i;
683 for(i=0; i<pParse->nzVar; i++){
drh503a6862013-03-01 01:07:17 +0000684 if( pParse->azVar[i] && strcmp(pParse->azVar[i],z)==0 ){
drh124c0b42011-06-01 18:15:55 +0000685 pExpr->iColumn = x = (ynVar)i+1;
686 break;
687 }
688 }
689 if( x==0 ) x = pExpr->iColumn = (ynVar)(++pParse->nVar);
drhfa6bc002004-09-07 16:19:52 +0000690 }
drh124c0b42011-06-01 18:15:55 +0000691 if( x>0 ){
692 if( x>pParse->nzVar ){
693 char **a;
694 a = sqlite3DbRealloc(db, pParse->azVar, x*sizeof(a[0]));
695 if( a==0 ) return; /* Error reported through db->mallocFailed */
696 pParse->azVar = a;
697 memset(&a[pParse->nzVar], 0, (x-pParse->nzVar)*sizeof(a[0]));
698 pParse->nzVar = x;
drhfa6bc002004-09-07 16:19:52 +0000699 }
drh124c0b42011-06-01 18:15:55 +0000700 if( z[0]!='?' || pParse->azVar[x-1]==0 ){
701 sqlite3DbFree(db, pParse->azVar[x-1]);
702 pParse->azVar[x-1] = sqlite3DbStrNDup(db, z, n);
drhfa6bc002004-09-07 16:19:52 +0000703 }
704 }
705 }
drhbb4957f2008-03-20 14:03:29 +0000706 if( !pParse->nErr && pParse->nVar>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
danielk1977832b2662007-05-09 11:37:22 +0000707 sqlite3ErrorMsg(pParse, "too many SQL variables");
708 }
drhfa6bc002004-09-07 16:19:52 +0000709}
710
711/*
danf6963f92009-11-23 14:39:14 +0000712** Recursively delete an expression tree.
drha2e00042002-01-22 03:13:42 +0000713*/
danf6963f92009-11-23 14:39:14 +0000714void sqlite3ExprDelete(sqlite3 *db, Expr *p){
715 if( p==0 ) return;
drhd50ffc42011-03-08 02:38:28 +0000716 /* Sanity check: Assert that the IntValue is non-negative if it exists */
717 assert( !ExprHasProperty(p, EP_IntValue) || p->u.iValue>=0 );
drhc5cd1242013-09-12 16:50:49 +0000718 if( !ExprHasProperty(p, EP_TokenOnly) ){
719 /* The Expr.x union is never used at the same time as Expr.pRight */
720 assert( p->x.pList==0 || p->pRight==0 );
drh33e619f2009-05-28 01:00:55 +0000721 sqlite3ExprDelete(db, p->pLeft);
722 sqlite3ExprDelete(db, p->pRight);
drhc5cd1242013-09-12 16:50:49 +0000723 if( ExprHasProperty(p, EP_MemToken) ) sqlite3DbFree(db, p->u.zToken);
danielk19776ab3a2e2009-02-19 14:39:25 +0000724 if( ExprHasProperty(p, EP_xIsSelect) ){
725 sqlite3SelectDelete(db, p->x.pSelect);
726 }else{
727 sqlite3ExprListDelete(db, p->x.pList);
728 }
729 }
drh33e619f2009-05-28 01:00:55 +0000730 if( !ExprHasProperty(p, EP_Static) ){
731 sqlite3DbFree(db, p);
732 }
drha2e00042002-01-22 03:13:42 +0000733}
734
drhd2687b72005-08-12 22:56:09 +0000735/*
danielk19776ab3a2e2009-02-19 14:39:25 +0000736** Return the number of bytes allocated for the expression structure
737** passed as the first argument. This is always one of EXPR_FULLSIZE,
738** EXPR_REDUCEDSIZE or EXPR_TOKENONLYSIZE.
739*/
740static int exprStructSize(Expr *p){
741 if( ExprHasProperty(p, EP_TokenOnly) ) return EXPR_TOKENONLYSIZE;
danielk19776ab3a2e2009-02-19 14:39:25 +0000742 if( ExprHasProperty(p, EP_Reduced) ) return EXPR_REDUCEDSIZE;
743 return EXPR_FULLSIZE;
744}
745
746/*
drh33e619f2009-05-28 01:00:55 +0000747** The dupedExpr*Size() routines each return the number of bytes required
748** to store a copy of an expression or expression tree. They differ in
749** how much of the tree is measured.
750**
751** dupedExprStructSize() Size of only the Expr structure
752** dupedExprNodeSize() Size of Expr + space for token
753** dupedExprSize() Expr + token + subtree components
754**
755***************************************************************************
756**
757** The dupedExprStructSize() function returns two values OR-ed together:
758** (1) the space required for a copy of the Expr structure only and
759** (2) the EP_xxx flags that indicate what the structure size should be.
760** The return values is always one of:
761**
762** EXPR_FULLSIZE
763** EXPR_REDUCEDSIZE | EP_Reduced
764** EXPR_TOKENONLYSIZE | EP_TokenOnly
765**
766** The size of the structure can be found by masking the return value
767** of this routine with 0xfff. The flags can be found by masking the
768** return value with EP_Reduced|EP_TokenOnly.
769**
770** Note that with flags==EXPRDUP_REDUCE, this routines works on full-size
771** (unreduced) Expr objects as they or originally constructed by the parser.
772** During expression analysis, extra information is computed and moved into
773** later parts of teh Expr object and that extra information might get chopped
774** off if the expression is reduced. Note also that it does not work to
peter.d.reid60ec9142014-09-06 16:39:46 +0000775** make an EXPRDUP_REDUCE copy of a reduced expression. It is only legal
drh33e619f2009-05-28 01:00:55 +0000776** to reduce a pristine expression tree from the parser. The implementation
777** of dupedExprStructSize() contain multiple assert() statements that attempt
778** to enforce this constraint.
danielk19776ab3a2e2009-02-19 14:39:25 +0000779*/
780static int dupedExprStructSize(Expr *p, int flags){
781 int nSize;
drh33e619f2009-05-28 01:00:55 +0000782 assert( flags==EXPRDUP_REDUCE || flags==0 ); /* Only one flag value allowed */
drhaecd8022013-09-13 18:15:15 +0000783 assert( EXPR_FULLSIZE<=0xfff );
784 assert( (0xfff & (EP_Reduced|EP_TokenOnly))==0 );
danielk19776ab3a2e2009-02-19 14:39:25 +0000785 if( 0==(flags&EXPRDUP_REDUCE) ){
786 nSize = EXPR_FULLSIZE;
danielk19776ab3a2e2009-02-19 14:39:25 +0000787 }else{
drhc5cd1242013-09-12 16:50:49 +0000788 assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) );
drh33e619f2009-05-28 01:00:55 +0000789 assert( !ExprHasProperty(p, EP_FromJoin) );
drhc5cd1242013-09-12 16:50:49 +0000790 assert( !ExprHasProperty(p, EP_MemToken) );
drhebb6a652013-09-12 23:42:22 +0000791 assert( !ExprHasProperty(p, EP_NoReduce) );
drhaecd8022013-09-13 18:15:15 +0000792 if( p->pLeft || p->x.pList ){
drh33e619f2009-05-28 01:00:55 +0000793 nSize = EXPR_REDUCEDSIZE | EP_Reduced;
794 }else{
drhaecd8022013-09-13 18:15:15 +0000795 assert( p->pRight==0 );
drh33e619f2009-05-28 01:00:55 +0000796 nSize = EXPR_TOKENONLYSIZE | EP_TokenOnly;
797 }
danielk19776ab3a2e2009-02-19 14:39:25 +0000798 }
799 return nSize;
800}
801
802/*
drh33e619f2009-05-28 01:00:55 +0000803** This function returns the space in bytes required to store the copy
804** of the Expr structure and a copy of the Expr.u.zToken string (if that
805** string is defined.)
danielk19776ab3a2e2009-02-19 14:39:25 +0000806*/
807static int dupedExprNodeSize(Expr *p, int flags){
drh33e619f2009-05-28 01:00:55 +0000808 int nByte = dupedExprStructSize(p, flags) & 0xfff;
809 if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
810 nByte += sqlite3Strlen30(p->u.zToken)+1;
danielk19776ab3a2e2009-02-19 14:39:25 +0000811 }
danielk1977bc739712009-03-23 04:33:32 +0000812 return ROUND8(nByte);
danielk19776ab3a2e2009-02-19 14:39:25 +0000813}
814
815/*
816** Return the number of bytes required to create a duplicate of the
817** expression passed as the first argument. The second argument is a
818** mask containing EXPRDUP_XXX flags.
819**
820** The value returned includes space to create a copy of the Expr struct
drh33e619f2009-05-28 01:00:55 +0000821** itself and the buffer referred to by Expr.u.zToken, if any.
danielk19776ab3a2e2009-02-19 14:39:25 +0000822**
823** If the EXPRDUP_REDUCE flag is set, then the return value includes
824** space to duplicate all Expr nodes in the tree formed by Expr.pLeft
825** and Expr.pRight variables (but not for any structures pointed to or
826** descended from the Expr.x.pList or Expr.x.pSelect variables).
827*/
828static int dupedExprSize(Expr *p, int flags){
829 int nByte = 0;
830 if( p ){
831 nByte = dupedExprNodeSize(p, flags);
832 if( flags&EXPRDUP_REDUCE ){
drhb7916a72009-05-27 10:31:29 +0000833 nByte += dupedExprSize(p->pLeft, flags) + dupedExprSize(p->pRight, flags);
danielk19776ab3a2e2009-02-19 14:39:25 +0000834 }
835 }
836 return nByte;
837}
838
839/*
840** This function is similar to sqlite3ExprDup(), except that if pzBuffer
841** is not NULL then *pzBuffer is assumed to point to a buffer large enough
drh33e619f2009-05-28 01:00:55 +0000842** to store the copy of expression p, the copies of p->u.zToken
danielk19776ab3a2e2009-02-19 14:39:25 +0000843** (if applicable), and the copies of the p->pLeft and p->pRight expressions,
peter.d.reid60ec9142014-09-06 16:39:46 +0000844** if any. Before returning, *pzBuffer is set to the first byte past the
danielk19776ab3a2e2009-02-19 14:39:25 +0000845** portion of the buffer copied into by this function.
846*/
847static Expr *exprDup(sqlite3 *db, Expr *p, int flags, u8 **pzBuffer){
848 Expr *pNew = 0; /* Value to return */
849 if( p ){
danielk19776ab3a2e2009-02-19 14:39:25 +0000850 const int isReduced = (flags&EXPRDUP_REDUCE);
851 u8 *zAlloc;
drh33e619f2009-05-28 01:00:55 +0000852 u32 staticFlag = 0;
danielk19776ab3a2e2009-02-19 14:39:25 +0000853
854 assert( pzBuffer==0 || isReduced );
855
856 /* Figure out where to write the new Expr structure. */
857 if( pzBuffer ){
858 zAlloc = *pzBuffer;
drh33e619f2009-05-28 01:00:55 +0000859 staticFlag = EP_Static;
danielk19776ab3a2e2009-02-19 14:39:25 +0000860 }else{
861 zAlloc = sqlite3DbMallocRaw(db, dupedExprSize(p, flags));
862 }
863 pNew = (Expr *)zAlloc;
864
865 if( pNew ){
866 /* Set nNewSize to the size allocated for the structure pointed to
867 ** by pNew. This is either EXPR_FULLSIZE, EXPR_REDUCEDSIZE or
868 ** EXPR_TOKENONLYSIZE. nToken is set to the number of bytes consumed
drh33e619f2009-05-28 01:00:55 +0000869 ** by the copy of the p->u.zToken string (if any).
danielk19776ab3a2e2009-02-19 14:39:25 +0000870 */
drh33e619f2009-05-28 01:00:55 +0000871 const unsigned nStructSize = dupedExprStructSize(p, flags);
872 const int nNewSize = nStructSize & 0xfff;
873 int nToken;
874 if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
875 nToken = sqlite3Strlen30(p->u.zToken) + 1;
876 }else{
877 nToken = 0;
878 }
danielk19776ab3a2e2009-02-19 14:39:25 +0000879 if( isReduced ){
880 assert( ExprHasProperty(p, EP_Reduced)==0 );
881 memcpy(zAlloc, p, nNewSize);
882 }else{
883 int nSize = exprStructSize(p);
884 memcpy(zAlloc, p, nSize);
885 memset(&zAlloc[nSize], 0, EXPR_FULLSIZE-nSize);
886 }
887
drh33e619f2009-05-28 01:00:55 +0000888 /* Set the EP_Reduced, EP_TokenOnly, and EP_Static flags appropriately. */
drhc5cd1242013-09-12 16:50:49 +0000889 pNew->flags &= ~(EP_Reduced|EP_TokenOnly|EP_Static|EP_MemToken);
drh33e619f2009-05-28 01:00:55 +0000890 pNew->flags |= nStructSize & (EP_Reduced|EP_TokenOnly);
891 pNew->flags |= staticFlag;
danielk19776ab3a2e2009-02-19 14:39:25 +0000892
drh33e619f2009-05-28 01:00:55 +0000893 /* Copy the p->u.zToken string, if any. */
danielk19776ab3a2e2009-02-19 14:39:25 +0000894 if( nToken ){
drh33e619f2009-05-28 01:00:55 +0000895 char *zToken = pNew->u.zToken = (char*)&zAlloc[nNewSize];
896 memcpy(zToken, p->u.zToken, nToken);
danielk19776ab3a2e2009-02-19 14:39:25 +0000897 }
898
899 if( 0==((p->flags|pNew->flags) & EP_TokenOnly) ){
danielk19776ab3a2e2009-02-19 14:39:25 +0000900 /* Fill in the pNew->x.pSelect or pNew->x.pList member. */
901 if( ExprHasProperty(p, EP_xIsSelect) ){
902 pNew->x.pSelect = sqlite3SelectDup(db, p->x.pSelect, isReduced);
903 }else{
904 pNew->x.pList = sqlite3ExprListDup(db, p->x.pList, isReduced);
905 }
906 }
907
908 /* Fill in pNew->pLeft and pNew->pRight. */
drhc5cd1242013-09-12 16:50:49 +0000909 if( ExprHasProperty(pNew, EP_Reduced|EP_TokenOnly) ){
danielk19776ab3a2e2009-02-19 14:39:25 +0000910 zAlloc += dupedExprNodeSize(p, flags);
911 if( ExprHasProperty(pNew, EP_Reduced) ){
912 pNew->pLeft = exprDup(db, p->pLeft, EXPRDUP_REDUCE, &zAlloc);
913 pNew->pRight = exprDup(db, p->pRight, EXPRDUP_REDUCE, &zAlloc);
914 }
915 if( pzBuffer ){
916 *pzBuffer = zAlloc;
917 }
drhb7916a72009-05-27 10:31:29 +0000918 }else{
drhc5cd1242013-09-12 16:50:49 +0000919 if( !ExprHasProperty(p, EP_TokenOnly) ){
drhb7916a72009-05-27 10:31:29 +0000920 pNew->pLeft = sqlite3ExprDup(db, p->pLeft, 0);
921 pNew->pRight = sqlite3ExprDup(db, p->pRight, 0);
922 }
danielk19776ab3a2e2009-02-19 14:39:25 +0000923 }
drhb7916a72009-05-27 10:31:29 +0000924
danielk19776ab3a2e2009-02-19 14:39:25 +0000925 }
926 }
927 return pNew;
928}
929
danbfe31e72014-01-15 14:17:31 +0000930/*
931** Create and return a deep copy of the object passed as the second
932** argument. If an OOM condition is encountered, NULL is returned
933** and the db->mallocFailed flag set.
934*/
daneede6a52014-01-15 19:42:23 +0000935#ifndef SQLITE_OMIT_CTE
danbfe31e72014-01-15 14:17:31 +0000936static With *withDup(sqlite3 *db, With *p){
dan4e9119d2014-01-13 15:12:23 +0000937 With *pRet = 0;
938 if( p ){
939 int nByte = sizeof(*p) + sizeof(p->a[0]) * (p->nCte-1);
940 pRet = sqlite3DbMallocZero(db, nByte);
941 if( pRet ){
942 int i;
943 pRet->nCte = p->nCte;
944 for(i=0; i<p->nCte; i++){
945 pRet->a[i].pSelect = sqlite3SelectDup(db, p->a[i].pSelect, 0);
946 pRet->a[i].pCols = sqlite3ExprListDup(db, p->a[i].pCols, 0);
947 pRet->a[i].zName = sqlite3DbStrDup(db, p->a[i].zName);
948 }
949 }
950 }
951 return pRet;
952}
daneede6a52014-01-15 19:42:23 +0000953#else
954# define withDup(x,y) 0
955#endif
dan4e9119d2014-01-13 15:12:23 +0000956
danielk19776ab3a2e2009-02-19 14:39:25 +0000957/*
drhff78bd22002-02-27 01:47:11 +0000958** The following group of routines make deep copies of expressions,
959** expression lists, ID lists, and select statements. The copies can
960** be deleted (by being passed to their respective ...Delete() routines)
961** without effecting the originals.
962**
danielk19774adee202004-05-08 08:23:19 +0000963** The expression list, ID, and source lists return by sqlite3ExprListDup(),
964** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
drhad3cab52002-05-24 02:04:32 +0000965** by subsequent calls to sqlite*ListAppend() routines.
drhff78bd22002-02-27 01:47:11 +0000966**
drhad3cab52002-05-24 02:04:32 +0000967** Any tables that the SrcList might point to are not duplicated.
danielk19776ab3a2e2009-02-19 14:39:25 +0000968**
drhb7916a72009-05-27 10:31:29 +0000969** The flags parameter contains a combination of the EXPRDUP_XXX flags.
danielk19776ab3a2e2009-02-19 14:39:25 +0000970** If the EXPRDUP_REDUCE flag is set, then the structure returned is a
971** truncated version of the usual Expr structure that will be stored as
972** part of the in-memory representation of the database schema.
drhff78bd22002-02-27 01:47:11 +0000973*/
danielk19776ab3a2e2009-02-19 14:39:25 +0000974Expr *sqlite3ExprDup(sqlite3 *db, Expr *p, int flags){
975 return exprDup(db, p, flags, 0);
drhff78bd22002-02-27 01:47:11 +0000976}
danielk19776ab3a2e2009-02-19 14:39:25 +0000977ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p, int flags){
drhff78bd22002-02-27 01:47:11 +0000978 ExprList *pNew;
drh145716b2004-09-24 12:24:06 +0000979 struct ExprList_item *pItem, *pOldItem;
drhff78bd22002-02-27 01:47:11 +0000980 int i;
981 if( p==0 ) return 0;
drh17435752007-08-16 04:30:38 +0000982 pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +0000983 if( pNew==0 ) return 0;
drhd872bb12012-02-02 01:58:08 +0000984 pNew->nExpr = i = p->nExpr;
985 if( (flags & EXPRDUP_REDUCE)==0 ) for(i=1; i<p->nExpr; i+=i){}
986 pNew->a = pItem = sqlite3DbMallocRaw(db, i*sizeof(p->a[0]) );
danielk1977e0048402004-06-15 16:51:01 +0000987 if( pItem==0 ){
drh633e6d52008-07-28 19:34:53 +0000988 sqlite3DbFree(db, pNew);
danielk1977e0048402004-06-15 16:51:01 +0000989 return 0;
990 }
drh145716b2004-09-24 12:24:06 +0000991 pOldItem = p->a;
992 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
danielk19776ab3a2e2009-02-19 14:39:25 +0000993 Expr *pOldExpr = pOldItem->pExpr;
drhb5526ea2009-07-16 12:41:05 +0000994 pItem->pExpr = sqlite3ExprDup(db, pOldExpr, flags);
drh17435752007-08-16 04:30:38 +0000995 pItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
drhb7916a72009-05-27 10:31:29 +0000996 pItem->zSpan = sqlite3DbStrDup(db, pOldItem->zSpan);
drh145716b2004-09-24 12:24:06 +0000997 pItem->sortOrder = pOldItem->sortOrder;
drh3e7bc9c2004-02-21 19:17:17 +0000998 pItem->done = 0;
drh2c036cf2013-06-26 00:34:13 +0000999 pItem->bSpanIsTab = pOldItem->bSpanIsTab;
drhc2acc4e2013-11-15 18:15:19 +00001000 pItem->u = pOldItem->u;
drhff78bd22002-02-27 01:47:11 +00001001 }
1002 return pNew;
1003}
danielk197793758c82005-01-21 08:13:14 +00001004
1005/*
1006** If cursors, triggers, views and subqueries are all omitted from
1007** the build, then none of the following routines, except for
1008** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes
1009** called with a NULL argument.
1010*/
danielk19776a67fe82005-02-04 04:07:16 +00001011#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \
1012 || !defined(SQLITE_OMIT_SUBQUERY)
danielk19776ab3a2e2009-02-19 14:39:25 +00001013SrcList *sqlite3SrcListDup(sqlite3 *db, SrcList *p, int flags){
drhad3cab52002-05-24 02:04:32 +00001014 SrcList *pNew;
1015 int i;
drh113088e2003-03-20 01:16:58 +00001016 int nByte;
drhad3cab52002-05-24 02:04:32 +00001017 if( p==0 ) return 0;
drh113088e2003-03-20 01:16:58 +00001018 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
drh17435752007-08-16 04:30:38 +00001019 pNew = sqlite3DbMallocRaw(db, nByte );
drhad3cab52002-05-24 02:04:32 +00001020 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +00001021 pNew->nSrc = pNew->nAlloc = p->nSrc;
drhad3cab52002-05-24 02:04:32 +00001022 for(i=0; i<p->nSrc; i++){
drh4efc4752004-01-16 15:55:37 +00001023 struct SrcList_item *pNewItem = &pNew->a[i];
1024 struct SrcList_item *pOldItem = &p->a[i];
drhed8a3bb2005-06-06 21:19:56 +00001025 Table *pTab;
dan41fb5cd2012-10-04 19:33:00 +00001026 pNewItem->pSchema = pOldItem->pSchema;
drh17435752007-08-16 04:30:38 +00001027 pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase);
1028 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
1029 pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias);
drh4efc4752004-01-16 15:55:37 +00001030 pNewItem->jointype = pOldItem->jointype;
1031 pNewItem->iCursor = pOldItem->iCursor;
drh5b6a9ed2011-09-15 23:58:14 +00001032 pNewItem->addrFillSub = pOldItem->addrFillSub;
1033 pNewItem->regReturn = pOldItem->regReturn;
danda79cf02011-07-08 16:10:54 +00001034 pNewItem->isCorrelated = pOldItem->isCorrelated;
drh21172c42012-10-30 00:29:07 +00001035 pNewItem->viaCoroutine = pOldItem->viaCoroutine;
danf43fe6e2014-01-15 18:12:00 +00001036 pNewItem->isRecursive = pOldItem->isRecursive;
danielk197785574e32008-10-06 05:32:18 +00001037 pNewItem->zIndex = sqlite3DbStrDup(db, pOldItem->zIndex);
1038 pNewItem->notIndexed = pOldItem->notIndexed;
1039 pNewItem->pIndex = pOldItem->pIndex;
drhed8a3bb2005-06-06 21:19:56 +00001040 pTab = pNewItem->pTab = pOldItem->pTab;
1041 if( pTab ){
1042 pTab->nRef++;
danielk1977a1cb1832005-02-12 08:59:55 +00001043 }
danielk19776ab3a2e2009-02-19 14:39:25 +00001044 pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect, flags);
1045 pNewItem->pOn = sqlite3ExprDup(db, pOldItem->pOn, flags);
drh17435752007-08-16 04:30:38 +00001046 pNewItem->pUsing = sqlite3IdListDup(db, pOldItem->pUsing);
danielk19776c18b6e2005-01-30 09:17:58 +00001047 pNewItem->colUsed = pOldItem->colUsed;
drhad3cab52002-05-24 02:04:32 +00001048 }
1049 return pNew;
1050}
drh17435752007-08-16 04:30:38 +00001051IdList *sqlite3IdListDup(sqlite3 *db, IdList *p){
drhff78bd22002-02-27 01:47:11 +00001052 IdList *pNew;
1053 int i;
1054 if( p==0 ) return 0;
drh17435752007-08-16 04:30:38 +00001055 pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +00001056 if( pNew==0 ) return 0;
drh6c535152012-02-02 03:38:30 +00001057 pNew->nId = p->nId;
drh17435752007-08-16 04:30:38 +00001058 pNew->a = sqlite3DbMallocRaw(db, p->nId*sizeof(p->a[0]) );
danielk1977d5d56522005-03-16 12:15:20 +00001059 if( pNew->a==0 ){
drh633e6d52008-07-28 19:34:53 +00001060 sqlite3DbFree(db, pNew);
danielk1977d5d56522005-03-16 12:15:20 +00001061 return 0;
1062 }
drh6c535152012-02-02 03:38:30 +00001063 /* Note that because the size of the allocation for p->a[] is not
1064 ** necessarily a power of two, sqlite3IdListAppend() may not be called
1065 ** on the duplicate created by this function. */
drhff78bd22002-02-27 01:47:11 +00001066 for(i=0; i<p->nId; i++){
drh4efc4752004-01-16 15:55:37 +00001067 struct IdList_item *pNewItem = &pNew->a[i];
1068 struct IdList_item *pOldItem = &p->a[i];
drh17435752007-08-16 04:30:38 +00001069 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
drh4efc4752004-01-16 15:55:37 +00001070 pNewItem->idx = pOldItem->idx;
drhff78bd22002-02-27 01:47:11 +00001071 }
1072 return pNew;
1073}
danielk19776ab3a2e2009-02-19 14:39:25 +00001074Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){
drh23b1b372011-12-07 01:55:51 +00001075 Select *pNew, *pPrior;
drhff78bd22002-02-27 01:47:11 +00001076 if( p==0 ) return 0;
drh17435752007-08-16 04:30:38 +00001077 pNew = sqlite3DbMallocRaw(db, sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +00001078 if( pNew==0 ) return 0;
drhb7916a72009-05-27 10:31:29 +00001079 pNew->pEList = sqlite3ExprListDup(db, p->pEList, flags);
danielk19776ab3a2e2009-02-19 14:39:25 +00001080 pNew->pSrc = sqlite3SrcListDup(db, p->pSrc, flags);
1081 pNew->pWhere = sqlite3ExprDup(db, p->pWhere, flags);
1082 pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags);
1083 pNew->pHaving = sqlite3ExprDup(db, p->pHaving, flags);
1084 pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, flags);
drhff78bd22002-02-27 01:47:11 +00001085 pNew->op = p->op;
drh23b1b372011-12-07 01:55:51 +00001086 pNew->pPrior = pPrior = sqlite3SelectDup(db, p->pPrior, flags);
1087 if( pPrior ) pPrior->pNext = pNew;
1088 pNew->pNext = 0;
danielk19776ab3a2e2009-02-19 14:39:25 +00001089 pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags);
1090 pNew->pOffset = sqlite3ExprDup(db, p->pOffset, flags);
drh92b01d52008-06-24 00:32:35 +00001091 pNew->iLimit = 0;
1092 pNew->iOffset = 0;
drh7d10d5a2008-08-20 16:35:10 +00001093 pNew->selFlags = p->selFlags & ~SF_UsesEphemeral;
drhb9bb7c12006-06-11 23:41:55 +00001094 pNew->addrOpenEphm[0] = -1;
1095 pNew->addrOpenEphm[1] = -1;
drhec2da852014-01-29 01:46:12 +00001096 pNew->nSelectRow = p->nSelectRow;
dan4e9119d2014-01-13 15:12:23 +00001097 pNew->pWith = withDup(db, p->pWith);
drheb9b8842014-09-21 00:27:26 +00001098 sqlite3SelectSetName(pNew, p->zSelName);
drhff78bd22002-02-27 01:47:11 +00001099 return pNew;
1100}
danielk197793758c82005-01-21 08:13:14 +00001101#else
danielk19776ab3a2e2009-02-19 14:39:25 +00001102Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){
danielk197793758c82005-01-21 08:13:14 +00001103 assert( p==0 );
1104 return 0;
1105}
1106#endif
drhff78bd22002-02-27 01:47:11 +00001107
1108
1109/*
drha76b5df2002-02-23 02:32:10 +00001110** Add a new element to the end of an expression list. If pList is
1111** initially NULL, then create a new expression list.
drhb7916a72009-05-27 10:31:29 +00001112**
1113** If a memory allocation error occurs, the entire list is freed and
1114** NULL is returned. If non-NULL is returned, then it is guaranteed
1115** that the new entry was successfully appended.
drha76b5df2002-02-23 02:32:10 +00001116*/
drh17435752007-08-16 04:30:38 +00001117ExprList *sqlite3ExprListAppend(
1118 Parse *pParse, /* Parsing context */
1119 ExprList *pList, /* List to which to append. Might be NULL */
drhb7916a72009-05-27 10:31:29 +00001120 Expr *pExpr /* Expression to be appended. Might be NULL */
drh17435752007-08-16 04:30:38 +00001121){
1122 sqlite3 *db = pParse->db;
drha76b5df2002-02-23 02:32:10 +00001123 if( pList==0 ){
drh17435752007-08-16 04:30:38 +00001124 pList = sqlite3DbMallocZero(db, sizeof(ExprList) );
drha76b5df2002-02-23 02:32:10 +00001125 if( pList==0 ){
danielk1977d5d56522005-03-16 12:15:20 +00001126 goto no_mem;
drha76b5df2002-02-23 02:32:10 +00001127 }
drhd872bb12012-02-02 01:58:08 +00001128 pList->a = sqlite3DbMallocRaw(db, sizeof(pList->a[0]));
1129 if( pList->a==0 ) goto no_mem;
1130 }else if( (pList->nExpr & (pList->nExpr-1))==0 ){
danielk1977d5d56522005-03-16 12:15:20 +00001131 struct ExprList_item *a;
drhd872bb12012-02-02 01:58:08 +00001132 assert( pList->nExpr>0 );
1133 a = sqlite3DbRealloc(db, pList->a, pList->nExpr*2*sizeof(pList->a[0]));
danielk1977d5d56522005-03-16 12:15:20 +00001134 if( a==0 ){
1135 goto no_mem;
drha76b5df2002-02-23 02:32:10 +00001136 }
danielk1977d5d56522005-03-16 12:15:20 +00001137 pList->a = a;
drha76b5df2002-02-23 02:32:10 +00001138 }
drh4efc4752004-01-16 15:55:37 +00001139 assert( pList->a!=0 );
drhb7916a72009-05-27 10:31:29 +00001140 if( 1 ){
drh4efc4752004-01-16 15:55:37 +00001141 struct ExprList_item *pItem = &pList->a[pList->nExpr++];
1142 memset(pItem, 0, sizeof(*pItem));
danielk1977e94ddc92005-03-21 03:53:38 +00001143 pItem->pExpr = pExpr;
drha76b5df2002-02-23 02:32:10 +00001144 }
1145 return pList;
danielk1977d5d56522005-03-16 12:15:20 +00001146
1147no_mem:
1148 /* Avoid leaking memory if malloc has failed. */
drh633e6d52008-07-28 19:34:53 +00001149 sqlite3ExprDelete(db, pExpr);
1150 sqlite3ExprListDelete(db, pList);
danielk1977d5d56522005-03-16 12:15:20 +00001151 return 0;
drha76b5df2002-02-23 02:32:10 +00001152}
1153
1154/*
drhb7916a72009-05-27 10:31:29 +00001155** Set the ExprList.a[].zName element of the most recently added item
1156** on the expression list.
1157**
1158** pList might be NULL following an OOM error. But pName should never be
1159** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag
1160** is set.
1161*/
1162void sqlite3ExprListSetName(
1163 Parse *pParse, /* Parsing context */
1164 ExprList *pList, /* List to which to add the span. */
1165 Token *pName, /* Name to be added */
1166 int dequote /* True to cause the name to be dequoted */
1167){
1168 assert( pList!=0 || pParse->db->mallocFailed!=0 );
1169 if( pList ){
1170 struct ExprList_item *pItem;
1171 assert( pList->nExpr>0 );
1172 pItem = &pList->a[pList->nExpr-1];
1173 assert( pItem->zName==0 );
1174 pItem->zName = sqlite3DbStrNDup(pParse->db, pName->z, pName->n);
1175 if( dequote && pItem->zName ) sqlite3Dequote(pItem->zName);
1176 }
1177}
1178
1179/*
1180** Set the ExprList.a[].zSpan element of the most recently added item
1181** on the expression list.
1182**
1183** pList might be NULL following an OOM error. But pSpan should never be
1184** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag
1185** is set.
1186*/
1187void sqlite3ExprListSetSpan(
1188 Parse *pParse, /* Parsing context */
1189 ExprList *pList, /* List to which to add the span. */
1190 ExprSpan *pSpan /* The span to be added */
1191){
1192 sqlite3 *db = pParse->db;
1193 assert( pList!=0 || db->mallocFailed!=0 );
1194 if( pList ){
1195 struct ExprList_item *pItem = &pList->a[pList->nExpr-1];
1196 assert( pList->nExpr>0 );
1197 assert( db->mallocFailed || pItem->pExpr==pSpan->pExpr );
1198 sqlite3DbFree(db, pItem->zSpan);
1199 pItem->zSpan = sqlite3DbStrNDup(db, (char*)pSpan->zStart,
shanecf697392009-06-01 16:53:09 +00001200 (int)(pSpan->zEnd - pSpan->zStart));
drhb7916a72009-05-27 10:31:29 +00001201 }
1202}
1203
1204/*
danielk19777a15a4b2007-05-08 17:54:43 +00001205** If the expression list pEList contains more than iLimit elements,
1206** leave an error message in pParse.
1207*/
1208void sqlite3ExprListCheckLength(
1209 Parse *pParse,
1210 ExprList *pEList,
danielk19777a15a4b2007-05-08 17:54:43 +00001211 const char *zObject
1212){
drhb1a6c3c2008-03-20 16:30:17 +00001213 int mx = pParse->db->aLimit[SQLITE_LIMIT_COLUMN];
drhc5499be2008-04-01 15:06:33 +00001214 testcase( pEList && pEList->nExpr==mx );
1215 testcase( pEList && pEList->nExpr==mx+1 );
drhb1a6c3c2008-03-20 16:30:17 +00001216 if( pEList && pEList->nExpr>mx ){
danielk19777a15a4b2007-05-08 17:54:43 +00001217 sqlite3ErrorMsg(pParse, "too many columns in %s", zObject);
1218 }
1219}
1220
1221/*
drha76b5df2002-02-23 02:32:10 +00001222** Delete an entire expression list.
1223*/
drh633e6d52008-07-28 19:34:53 +00001224void sqlite3ExprListDelete(sqlite3 *db, ExprList *pList){
drha76b5df2002-02-23 02:32:10 +00001225 int i;
drhbe5c89a2004-07-26 00:31:09 +00001226 struct ExprList_item *pItem;
drha76b5df2002-02-23 02:32:10 +00001227 if( pList==0 ) return;
drhd872bb12012-02-02 01:58:08 +00001228 assert( pList->a!=0 || pList->nExpr==0 );
drhbe5c89a2004-07-26 00:31:09 +00001229 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
drh633e6d52008-07-28 19:34:53 +00001230 sqlite3ExprDelete(db, pItem->pExpr);
1231 sqlite3DbFree(db, pItem->zName);
drhb7916a72009-05-27 10:31:29 +00001232 sqlite3DbFree(db, pItem->zSpan);
drha76b5df2002-02-23 02:32:10 +00001233 }
drh633e6d52008-07-28 19:34:53 +00001234 sqlite3DbFree(db, pList->a);
1235 sqlite3DbFree(db, pList);
drha76b5df2002-02-23 02:32:10 +00001236}
1237
1238/*
drh2308ed32015-02-09 16:09:34 +00001239** Return the bitwise-OR of all Expr.flags fields in the given
1240** ExprList.
drh885a5b02015-02-09 15:21:36 +00001241*/
drh2308ed32015-02-09 16:09:34 +00001242u32 sqlite3ExprListFlags(const ExprList *pList){
drh885a5b02015-02-09 15:21:36 +00001243 int i;
drh2308ed32015-02-09 16:09:34 +00001244 u32 m = 0;
1245 if( pList ){
1246 for(i=0; i<pList->nExpr; i++){
1247 m |= pList->a[i].pExpr->flags;
1248 }
drh885a5b02015-02-09 15:21:36 +00001249 }
drh2308ed32015-02-09 16:09:34 +00001250 return m;
drh885a5b02015-02-09 15:21:36 +00001251}
1252
1253/*
drh059b2d52014-10-24 19:28:09 +00001254** These routines are Walker callbacks used to check expressions to
1255** see if they are "constant" for some definition of constant. The
1256** Walker.eCode value determines the type of "constant" we are looking
1257** for.
drh73b211a2005-01-18 04:00:42 +00001258**
drh7d10d5a2008-08-20 16:35:10 +00001259** These callback routines are used to implement the following:
drh626a8792005-01-17 22:08:19 +00001260**
drh059b2d52014-10-24 19:28:09 +00001261** sqlite3ExprIsConstant() pWalker->eCode==1
1262** sqlite3ExprIsConstantNotJoin() pWalker->eCode==2
1263** sqlite3ExprRefOneTableOnly() pWalker->eCode==3
1264** sqlite3ExprIsConstantOrFunction() pWalker->eCode==4 or 5
1265**
1266** In all cases, the callbacks set Walker.eCode=0 and abort if the expression
1267** is found to not be a constant.
drh87abf5c2005-08-25 12:45:04 +00001268**
drhfeada2d2014-09-24 13:20:22 +00001269** The sqlite3ExprIsConstantOrFunction() is used for evaluating expressions
drh059b2d52014-10-24 19:28:09 +00001270** in a CREATE TABLE statement. The Walker.eCode value is 5 when parsing
1271** an existing schema and 4 when processing a new statement. A bound
drhfeada2d2014-09-24 13:20:22 +00001272** parameter raises an error for new statements, but is silently converted
1273** to NULL for existing schemas. This allows sqlite_master tables that
1274** contain a bound parameter because they were generated by older versions
1275** of SQLite to be parsed by newer versions of SQLite without raising a
1276** malformed schema error.
drh626a8792005-01-17 22:08:19 +00001277*/
drh7d10d5a2008-08-20 16:35:10 +00001278static int exprNodeIsConstant(Walker *pWalker, Expr *pExpr){
drh626a8792005-01-17 22:08:19 +00001279
drh059b2d52014-10-24 19:28:09 +00001280 /* If pWalker->eCode is 2 then any term of the expression that comes from
1281 ** the ON or USING clauses of a left join disqualifies the expression
drh0a168372007-06-08 00:20:47 +00001282 ** from being considered constant. */
drh059b2d52014-10-24 19:28:09 +00001283 if( pWalker->eCode==2 && ExprHasProperty(pExpr, EP_FromJoin) ){
1284 pWalker->eCode = 0;
drh7d10d5a2008-08-20 16:35:10 +00001285 return WRC_Abort;
drh0a168372007-06-08 00:20:47 +00001286 }
1287
drh626a8792005-01-17 22:08:19 +00001288 switch( pExpr->op ){
drheb55bd22005-06-30 17:04:21 +00001289 /* Consider functions to be constant if all their arguments are constant
drh059b2d52014-10-24 19:28:09 +00001290 ** and either pWalker->eCode==4 or 5 or the function has the
1291 ** SQLITE_FUNC_CONST flag. */
drheb55bd22005-06-30 17:04:21 +00001292 case TK_FUNCTION:
drh63f84572015-02-09 14:07:07 +00001293 if( pWalker->eCode>=4 || ExprHasProperty(pExpr,EP_ConstFunc) ){
drhb1fba282013-11-21 14:33:48 +00001294 return WRC_Continue;
drh059b2d52014-10-24 19:28:09 +00001295 }else{
1296 pWalker->eCode = 0;
1297 return WRC_Abort;
drhb1fba282013-11-21 14:33:48 +00001298 }
drh626a8792005-01-17 22:08:19 +00001299 case TK_ID:
1300 case TK_COLUMN:
drh626a8792005-01-17 22:08:19 +00001301 case TK_AGG_FUNCTION:
drh13449892005-09-07 21:22:45 +00001302 case TK_AGG_COLUMN:
drhc5499be2008-04-01 15:06:33 +00001303 testcase( pExpr->op==TK_ID );
1304 testcase( pExpr->op==TK_COLUMN );
drhc5499be2008-04-01 15:06:33 +00001305 testcase( pExpr->op==TK_AGG_FUNCTION );
1306 testcase( pExpr->op==TK_AGG_COLUMN );
drh059b2d52014-10-24 19:28:09 +00001307 if( pWalker->eCode==3 && pExpr->iTable==pWalker->u.iCur ){
1308 return WRC_Continue;
1309 }else{
1310 pWalker->eCode = 0;
1311 return WRC_Abort;
1312 }
drhfeada2d2014-09-24 13:20:22 +00001313 case TK_VARIABLE:
drh059b2d52014-10-24 19:28:09 +00001314 if( pWalker->eCode==5 ){
drhfeada2d2014-09-24 13:20:22 +00001315 /* Silently convert bound parameters that appear inside of CREATE
1316 ** statements into a NULL when parsing the CREATE statement text out
1317 ** of the sqlite_master table */
1318 pExpr->op = TK_NULL;
drh059b2d52014-10-24 19:28:09 +00001319 }else if( pWalker->eCode==4 ){
drhfeada2d2014-09-24 13:20:22 +00001320 /* A bound parameter in a CREATE statement that originates from
1321 ** sqlite3_prepare() causes an error */
drh059b2d52014-10-24 19:28:09 +00001322 pWalker->eCode = 0;
drhfeada2d2014-09-24 13:20:22 +00001323 return WRC_Abort;
1324 }
1325 /* Fall through */
drh626a8792005-01-17 22:08:19 +00001326 default:
drhb74b1012009-05-28 21:04:37 +00001327 testcase( pExpr->op==TK_SELECT ); /* selectNodeIsConstant will disallow */
1328 testcase( pExpr->op==TK_EXISTS ); /* selectNodeIsConstant will disallow */
drh7d10d5a2008-08-20 16:35:10 +00001329 return WRC_Continue;
drh626a8792005-01-17 22:08:19 +00001330 }
1331}
danielk197762c14b32008-11-19 09:05:26 +00001332static int selectNodeIsConstant(Walker *pWalker, Select *NotUsed){
1333 UNUSED_PARAMETER(NotUsed);
drh059b2d52014-10-24 19:28:09 +00001334 pWalker->eCode = 0;
drh7d10d5a2008-08-20 16:35:10 +00001335 return WRC_Abort;
1336}
drh059b2d52014-10-24 19:28:09 +00001337static int exprIsConst(Expr *p, int initFlag, int iCur){
drh7d10d5a2008-08-20 16:35:10 +00001338 Walker w;
drhaa87f9a2013-04-25 00:57:10 +00001339 memset(&w, 0, sizeof(w));
drh059b2d52014-10-24 19:28:09 +00001340 w.eCode = initFlag;
drh7d10d5a2008-08-20 16:35:10 +00001341 w.xExprCallback = exprNodeIsConstant;
1342 w.xSelectCallback = selectNodeIsConstant;
drh059b2d52014-10-24 19:28:09 +00001343 w.u.iCur = iCur;
drh7d10d5a2008-08-20 16:35:10 +00001344 sqlite3WalkExpr(&w, p);
drh059b2d52014-10-24 19:28:09 +00001345 return w.eCode;
drh7d10d5a2008-08-20 16:35:10 +00001346}
drh626a8792005-01-17 22:08:19 +00001347
1348/*
drh059b2d52014-10-24 19:28:09 +00001349** Walk an expression tree. Return non-zero if the expression is constant
drheb55bd22005-06-30 17:04:21 +00001350** and 0 if it involves variables or function calls.
drh23989372002-05-21 13:43:04 +00001351**
1352** For the purposes of this function, a double-quoted string (ex: "abc")
1353** is considered a variable but a single-quoted string (ex: 'abc') is
1354** a constant.
drhfef52082000-06-06 01:50:43 +00001355*/
danielk19774adee202004-05-08 08:23:19 +00001356int sqlite3ExprIsConstant(Expr *p){
drh059b2d52014-10-24 19:28:09 +00001357 return exprIsConst(p, 1, 0);
drhfef52082000-06-06 01:50:43 +00001358}
1359
1360/*
drh059b2d52014-10-24 19:28:09 +00001361** Walk an expression tree. Return non-zero if the expression is constant
drh0a168372007-06-08 00:20:47 +00001362** that does no originate from the ON or USING clauses of a join.
1363** Return 0 if it involves variables or function calls or terms from
1364** an ON or USING clause.
1365*/
1366int sqlite3ExprIsConstantNotJoin(Expr *p){
drh059b2d52014-10-24 19:28:09 +00001367 return exprIsConst(p, 2, 0);
drh0a168372007-06-08 00:20:47 +00001368}
1369
1370/*
drh059b2d52014-10-24 19:28:09 +00001371** Walk an expression tree. Return non-zero if the expression constant
1372** for any single row of the table with cursor iCur. In other words, the
1373** expression must not refer to any non-deterministic function nor any
1374** table other than iCur.
1375*/
1376int sqlite3ExprIsTableConstant(Expr *p, int iCur){
1377 return exprIsConst(p, 3, iCur);
1378}
1379
1380/*
1381** Walk an expression tree. Return non-zero if the expression is constant
drheb55bd22005-06-30 17:04:21 +00001382** or a function call with constant arguments. Return and 0 if there
1383** are any variables.
1384**
1385** For the purposes of this function, a double-quoted string (ex: "abc")
1386** is considered a variable but a single-quoted string (ex: 'abc') is
1387** a constant.
1388*/
drhfeada2d2014-09-24 13:20:22 +00001389int sqlite3ExprIsConstantOrFunction(Expr *p, u8 isInit){
1390 assert( isInit==0 || isInit==1 );
drh059b2d52014-10-24 19:28:09 +00001391 return exprIsConst(p, 4+isInit, 0);
drheb55bd22005-06-30 17:04:21 +00001392}
1393
1394/*
drh73b211a2005-01-18 04:00:42 +00001395** If the expression p codes a constant integer that is small enough
drh202b2df2004-01-06 01:13:46 +00001396** to fit in a 32-bit integer, return 1 and put the value of the integer
1397** in *pValue. If the expression is not an integer or if it is too big
1398** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
drhe4de1fe2002-06-02 16:09:01 +00001399*/
danielk19774adee202004-05-08 08:23:19 +00001400int sqlite3ExprIsInteger(Expr *p, int *pValue){
drh92b01d52008-06-24 00:32:35 +00001401 int rc = 0;
drhcd92e842011-02-17 15:58:20 +00001402
1403 /* If an expression is an integer literal that fits in a signed 32-bit
1404 ** integer, then the EP_IntValue flag will have already been set */
1405 assert( p->op!=TK_INTEGER || (p->flags & EP_IntValue)!=0
1406 || sqlite3GetInt32(p->u.zToken, &rc)==0 );
1407
drh92b01d52008-06-24 00:32:35 +00001408 if( p->flags & EP_IntValue ){
drh33e619f2009-05-28 01:00:55 +00001409 *pValue = p->u.iValue;
drh92b01d52008-06-24 00:32:35 +00001410 return 1;
1411 }
drhe4de1fe2002-06-02 16:09:01 +00001412 switch( p->op ){
drh4b59ab52002-08-24 18:24:51 +00001413 case TK_UPLUS: {
drh92b01d52008-06-24 00:32:35 +00001414 rc = sqlite3ExprIsInteger(p->pLeft, pValue);
drhf6e369a2008-06-24 12:46:30 +00001415 break;
drh4b59ab52002-08-24 18:24:51 +00001416 }
drhe4de1fe2002-06-02 16:09:01 +00001417 case TK_UMINUS: {
1418 int v;
danielk19774adee202004-05-08 08:23:19 +00001419 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
mistachkinf6418892013-08-28 01:54:12 +00001420 assert( v!=(-2147483647-1) );
drhe4de1fe2002-06-02 16:09:01 +00001421 *pValue = -v;
drh92b01d52008-06-24 00:32:35 +00001422 rc = 1;
drhe4de1fe2002-06-02 16:09:01 +00001423 }
1424 break;
1425 }
1426 default: break;
1427 }
drh92b01d52008-06-24 00:32:35 +00001428 return rc;
drhe4de1fe2002-06-02 16:09:01 +00001429}
1430
1431/*
drh039fc322009-11-17 18:31:47 +00001432** Return FALSE if there is no chance that the expression can be NULL.
1433**
1434** If the expression might be NULL or if the expression is too complex
1435** to tell return TRUE.
1436**
1437** This routine is used as an optimization, to skip OP_IsNull opcodes
1438** when we know that a value cannot be NULL. Hence, a false positive
1439** (returning TRUE when in fact the expression can never be NULL) might
1440** be a small performance hit but is otherwise harmless. On the other
1441** hand, a false negative (returning FALSE when the result could be NULL)
1442** will likely result in an incorrect answer. So when in doubt, return
1443** TRUE.
1444*/
1445int sqlite3ExprCanBeNull(const Expr *p){
1446 u8 op;
drhcd7f4572009-11-19 14:48:40 +00001447 while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ p = p->pLeft; }
drh039fc322009-11-17 18:31:47 +00001448 op = p->op;
1449 if( op==TK_REGISTER ) op = p->op2;
1450 switch( op ){
1451 case TK_INTEGER:
1452 case TK_STRING:
1453 case TK_FLOAT:
1454 case TK_BLOB:
1455 return 0;
drh7248a8b2014-08-04 18:50:54 +00001456 case TK_COLUMN:
1457 assert( p->pTab!=0 );
drh72673a22014-12-04 16:27:17 +00001458 return ExprHasProperty(p, EP_CanBeNull) ||
1459 (p->iColumn>=0 && p->pTab->aCol[p->iColumn].notNull==0);
drh039fc322009-11-17 18:31:47 +00001460 default:
1461 return 1;
1462 }
1463}
1464
1465/*
1466** Return TRUE if the given expression is a constant which would be
1467** unchanged by OP_Affinity with the affinity given in the second
1468** argument.
1469**
1470** This routine is used to determine if the OP_Affinity operation
1471** can be omitted. When in doubt return FALSE. A false negative
1472** is harmless. A false positive, however, can result in the wrong
1473** answer.
1474*/
1475int sqlite3ExprNeedsNoAffinityChange(const Expr *p, char aff){
1476 u8 op;
1477 if( aff==SQLITE_AFF_NONE ) return 1;
drhcd7f4572009-11-19 14:48:40 +00001478 while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ p = p->pLeft; }
drh039fc322009-11-17 18:31:47 +00001479 op = p->op;
1480 if( op==TK_REGISTER ) op = p->op2;
1481 switch( op ){
1482 case TK_INTEGER: {
1483 return aff==SQLITE_AFF_INTEGER || aff==SQLITE_AFF_NUMERIC;
1484 }
1485 case TK_FLOAT: {
1486 return aff==SQLITE_AFF_REAL || aff==SQLITE_AFF_NUMERIC;
1487 }
1488 case TK_STRING: {
1489 return aff==SQLITE_AFF_TEXT;
1490 }
1491 case TK_BLOB: {
1492 return 1;
1493 }
drh2f2855b2009-11-18 01:25:26 +00001494 case TK_COLUMN: {
drh88376ca2009-11-19 15:44:53 +00001495 assert( p->iTable>=0 ); /* p cannot be part of a CHECK constraint */
1496 return p->iColumn<0
1497 && (aff==SQLITE_AFF_INTEGER || aff==SQLITE_AFF_NUMERIC);
drh2f2855b2009-11-18 01:25:26 +00001498 }
drh039fc322009-11-17 18:31:47 +00001499 default: {
1500 return 0;
1501 }
1502 }
1503}
1504
1505/*
drhc4a3c772001-04-04 11:48:57 +00001506** Return TRUE if the given string is a row-id column name.
1507*/
danielk19774adee202004-05-08 08:23:19 +00001508int sqlite3IsRowid(const char *z){
1509 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
1510 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
1511 if( sqlite3StrICmp(z, "OID")==0 ) return 1;
drhc4a3c772001-04-04 11:48:57 +00001512 return 0;
1513}
1514
danielk19779a96b662007-11-29 17:05:18 +00001515/*
drhb74b1012009-05-28 21:04:37 +00001516** Return true if we are able to the IN operator optimization on a
1517** query of the form
drhb287f4b2008-04-25 00:08:38 +00001518**
drhb74b1012009-05-28 21:04:37 +00001519** x IN (SELECT ...)
drhb287f4b2008-04-25 00:08:38 +00001520**
drhb74b1012009-05-28 21:04:37 +00001521** Where the SELECT... clause is as specified by the parameter to this
1522** routine.
1523**
1524** The Select object passed in has already been preprocessed and no
1525** errors have been found.
drhb287f4b2008-04-25 00:08:38 +00001526*/
1527#ifndef SQLITE_OMIT_SUBQUERY
1528static int isCandidateForInOpt(Select *p){
1529 SrcList *pSrc;
1530 ExprList *pEList;
1531 Table *pTab;
drhb287f4b2008-04-25 00:08:38 +00001532 if( p==0 ) return 0; /* right-hand side of IN is SELECT */
1533 if( p->pPrior ) return 0; /* Not a compound SELECT */
drh7d10d5a2008-08-20 16:35:10 +00001534 if( p->selFlags & (SF_Distinct|SF_Aggregate) ){
drhb74b1012009-05-28 21:04:37 +00001535 testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
1536 testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
1537 return 0; /* No DISTINCT keyword and no aggregate functions */
drh7d10d5a2008-08-20 16:35:10 +00001538 }
drhb74b1012009-05-28 21:04:37 +00001539 assert( p->pGroupBy==0 ); /* Has no GROUP BY clause */
drhb287f4b2008-04-25 00:08:38 +00001540 if( p->pLimit ) return 0; /* Has no LIMIT clause */
drhb74b1012009-05-28 21:04:37 +00001541 assert( p->pOffset==0 ); /* No LIMIT means no OFFSET */
drhb287f4b2008-04-25 00:08:38 +00001542 if( p->pWhere ) return 0; /* Has no WHERE clause */
1543 pSrc = p->pSrc;
drhd1fa7bc2009-01-10 13:24:50 +00001544 assert( pSrc!=0 );
1545 if( pSrc->nSrc!=1 ) return 0; /* Single term in FROM clause */
drhb74b1012009-05-28 21:04:37 +00001546 if( pSrc->a[0].pSelect ) return 0; /* FROM is not a subquery or view */
drhb287f4b2008-04-25 00:08:38 +00001547 pTab = pSrc->a[0].pTab;
drhb74b1012009-05-28 21:04:37 +00001548 if( NEVER(pTab==0) ) return 0;
1549 assert( pTab->pSelect==0 ); /* FROM clause is not a view */
drhb287f4b2008-04-25 00:08:38 +00001550 if( IsVirtual(pTab) ) return 0; /* FROM clause not a virtual table */
1551 pEList = p->pEList;
1552 if( pEList->nExpr!=1 ) return 0; /* One column in the result set */
1553 if( pEList->a[0].pExpr->op!=TK_COLUMN ) return 0; /* Result is a column */
1554 return 1;
1555}
1556#endif /* SQLITE_OMIT_SUBQUERY */
1557
1558/*
dan1d8cb212011-12-09 13:24:16 +00001559** Code an OP_Once instruction and allocate space for its flag. Return the
1560** address of the new instruction.
1561*/
1562int sqlite3CodeOnce(Parse *pParse){
1563 Vdbe *v = sqlite3GetVdbe(pParse); /* Virtual machine being coded */
drh7d176102014-02-18 03:07:12 +00001564 return sqlite3VdbeAddOp1(v, OP_Once, pParse->nOnce++);
dan1d8cb212011-12-09 13:24:16 +00001565}
1566
1567/*
drh4c259e92014-08-01 21:12:35 +00001568** Generate code that checks the left-most column of index table iCur to see if
1569** it contains any NULL entries. Cause the register at regHasNull to be set
drh6be515e2014-08-01 21:00:53 +00001570** to a non-NULL value if iCur contains no NULLs. Cause register regHasNull
1571** to be set to NULL if iCur contains one or more NULL values.
1572*/
1573static void sqlite3SetHasNullFlag(Vdbe *v, int iCur, int regHasNull){
1574 int j1;
1575 sqlite3VdbeAddOp2(v, OP_Integer, 0, regHasNull);
1576 j1 = sqlite3VdbeAddOp1(v, OP_Rewind, iCur); VdbeCoverage(v);
1577 sqlite3VdbeAddOp3(v, OP_Column, iCur, 0, regHasNull);
1578 sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG);
drh4c259e92014-08-01 21:12:35 +00001579 VdbeComment((v, "first_entry_in(%d)", iCur));
drh6be515e2014-08-01 21:00:53 +00001580 sqlite3VdbeJumpHere(v, j1);
1581}
1582
drhbb53ecb2014-08-02 21:03:33 +00001583
1584#ifndef SQLITE_OMIT_SUBQUERY
1585/*
1586** The argument is an IN operator with a list (not a subquery) on the
1587** right-hand side. Return TRUE if that list is constant.
1588*/
1589static int sqlite3InRhsIsConstant(Expr *pIn){
1590 Expr *pLHS;
1591 int res;
1592 assert( !ExprHasProperty(pIn, EP_xIsSelect) );
1593 pLHS = pIn->pLeft;
1594 pIn->pLeft = 0;
1595 res = sqlite3ExprIsConstant(pIn);
1596 pIn->pLeft = pLHS;
1597 return res;
1598}
1599#endif
1600
drh6be515e2014-08-01 21:00:53 +00001601/*
danielk19779a96b662007-11-29 17:05:18 +00001602** This function is used by the implementation of the IN (...) operator.
drhd4305ca2012-09-18 17:08:33 +00001603** The pX parameter is the expression on the RHS of the IN operator, which
1604** might be either a list of expressions or a subquery.
danielk19779a96b662007-11-29 17:05:18 +00001605**
drhd4305ca2012-09-18 17:08:33 +00001606** The job of this routine is to find or create a b-tree object that can
1607** be used either to test for membership in the RHS set or to iterate through
1608** all members of the RHS set, skipping duplicates.
1609**
drh3a856252014-08-01 14:46:57 +00001610** A cursor is opened on the b-tree object that is the RHS of the IN operator
drhd4305ca2012-09-18 17:08:33 +00001611** and pX->iTable is set to the index of that cursor.
1612**
drhb74b1012009-05-28 21:04:37 +00001613** The returned value of this function indicates the b-tree type, as follows:
danielk19779a96b662007-11-29 17:05:18 +00001614**
drh1ccce442013-03-12 20:38:51 +00001615** IN_INDEX_ROWID - The cursor was opened on a database table.
1616** IN_INDEX_INDEX_ASC - The cursor was opened on an ascending index.
1617** IN_INDEX_INDEX_DESC - The cursor was opened on a descending index.
1618** IN_INDEX_EPH - The cursor was opened on a specially created and
1619** populated epheremal table.
drhbb53ecb2014-08-02 21:03:33 +00001620** IN_INDEX_NOOP - No cursor was allocated. The IN operator must be
1621** implemented as a sequence of comparisons.
danielk19779a96b662007-11-29 17:05:18 +00001622**
drhd4305ca2012-09-18 17:08:33 +00001623** An existing b-tree might be used if the RHS expression pX is a simple
1624** subquery such as:
danielk19779a96b662007-11-29 17:05:18 +00001625**
1626** SELECT <column> FROM <table>
1627**
drhd4305ca2012-09-18 17:08:33 +00001628** If the RHS of the IN operator is a list or a more complex subquery, then
1629** an ephemeral table might need to be generated from the RHS and then
peter.d.reid60ec9142014-09-06 16:39:46 +00001630** pX->iTable made to point to the ephemeral table instead of an
drh3a856252014-08-01 14:46:57 +00001631** existing table.
drhd4305ca2012-09-18 17:08:33 +00001632**
drh3a856252014-08-01 14:46:57 +00001633** The inFlags parameter must contain exactly one of the bits
1634** IN_INDEX_MEMBERSHIP or IN_INDEX_LOOP. If inFlags contains
1635** IN_INDEX_MEMBERSHIP, then the generated table will be used for a
1636** fast membership test. When the IN_INDEX_LOOP bit is set, the
1637** IN index will be used to loop over all values of the RHS of the
1638** IN operator.
1639**
1640** When IN_INDEX_LOOP is used (and the b-tree will be used to iterate
1641** through the set members) then the b-tree must not contain duplicates.
1642** An epheremal table must be used unless the selected <column> is guaranteed
danielk19779a96b662007-11-29 17:05:18 +00001643** to be unique - either because it is an INTEGER PRIMARY KEY or it
drhb74b1012009-05-28 21:04:37 +00001644** has a UNIQUE constraint or UNIQUE index.
danielk19770cdc0222008-06-26 18:04:03 +00001645**
drh3a856252014-08-01 14:46:57 +00001646** When IN_INDEX_MEMBERSHIP is used (and the b-tree will be used
1647** for fast set membership tests) then an epheremal table must
danielk19770cdc0222008-06-26 18:04:03 +00001648** be used unless <column> is an INTEGER PRIMARY KEY or an index can
1649** be found with <column> as its left-most column.
1650**
drhbb53ecb2014-08-02 21:03:33 +00001651** If the IN_INDEX_NOOP_OK and IN_INDEX_MEMBERSHIP are both set and
1652** if the RHS of the IN operator is a list (not a subquery) then this
1653** routine might decide that creating an ephemeral b-tree for membership
1654** testing is too expensive and return IN_INDEX_NOOP. In that case, the
1655** calling routine should implement the IN operator using a sequence
1656** of Eq or Ne comparison operations.
1657**
drhb74b1012009-05-28 21:04:37 +00001658** When the b-tree is being used for membership tests, the calling function
drh3a856252014-08-01 14:46:57 +00001659** might need to know whether or not the RHS side of the IN operator
drhe21a6e12014-08-01 18:00:24 +00001660** contains a NULL. If prRhsHasNull is not a NULL pointer and
drh3a856252014-08-01 14:46:57 +00001661** if there is any chance that the (...) might contain a NULL value at
danielk19770cdc0222008-06-26 18:04:03 +00001662** runtime, then a register is allocated and the register number written
drhe21a6e12014-08-01 18:00:24 +00001663** to *prRhsHasNull. If there is no chance that the (...) contains a
1664** NULL value, then *prRhsHasNull is left unchanged.
danielk19770cdc0222008-06-26 18:04:03 +00001665**
drhe21a6e12014-08-01 18:00:24 +00001666** If a register is allocated and its location stored in *prRhsHasNull, then
drh6be515e2014-08-01 21:00:53 +00001667** the value in that register will be NULL if the b-tree contains one or more
1668** NULL values, and it will be some non-NULL value if the b-tree contains no
1669** NULL values.
danielk19779a96b662007-11-29 17:05:18 +00001670*/
danielk1977284f4ac2007-12-10 05:03:46 +00001671#ifndef SQLITE_OMIT_SUBQUERY
drhe21a6e12014-08-01 18:00:24 +00001672int sqlite3FindInIndex(Parse *pParse, Expr *pX, u32 inFlags, int *prRhsHasNull){
drhb74b1012009-05-28 21:04:37 +00001673 Select *p; /* SELECT to the right of IN operator */
1674 int eType = 0; /* Type of RHS table. IN_INDEX_* */
1675 int iTab = pParse->nTab++; /* Cursor of the RHS table */
drh3a856252014-08-01 14:46:57 +00001676 int mustBeUnique; /* True if RHS must be unique */
drhb8475df2011-12-09 16:21:19 +00001677 Vdbe *v = sqlite3GetVdbe(pParse); /* Virtual machine being coded */
danielk19779a96b662007-11-29 17:05:18 +00001678
drh1450bc62009-10-30 13:25:56 +00001679 assert( pX->op==TK_IN );
drh3a856252014-08-01 14:46:57 +00001680 mustBeUnique = (inFlags & IN_INDEX_LOOP)!=0;
drh1450bc62009-10-30 13:25:56 +00001681
drhb74b1012009-05-28 21:04:37 +00001682 /* Check to see if an existing table or index can be used to
1683 ** satisfy the query. This is preferable to generating a new
1684 ** ephemeral table.
danielk19779a96b662007-11-29 17:05:18 +00001685 */
danielk19776ab3a2e2009-02-19 14:39:25 +00001686 p = (ExprHasProperty(pX, EP_xIsSelect) ? pX->x.pSelect : 0);
drhfd773cf2009-05-29 14:39:07 +00001687 if( ALWAYS(pParse->nErr==0) && isCandidateForInOpt(p) ){
danielk1977e1fb65a2009-04-02 17:23:32 +00001688 sqlite3 *db = pParse->db; /* Database connection */
drhb07028f2011-10-14 21:49:18 +00001689 Table *pTab; /* Table <table>. */
1690 Expr *pExpr; /* Expression <column> */
drhbbbdc832013-10-22 18:01:40 +00001691 i16 iCol; /* Index of column <column> */
1692 i16 iDb; /* Database idx for pTab */
drhb07028f2011-10-14 21:49:18 +00001693
1694 assert( p ); /* Because of isCandidateForInOpt(p) */
1695 assert( p->pEList!=0 ); /* Because of isCandidateForInOpt(p) */
1696 assert( p->pEList->a[0].pExpr!=0 ); /* Because of isCandidateForInOpt(p) */
1697 assert( p->pSrc!=0 ); /* Because of isCandidateForInOpt(p) */
1698 pTab = p->pSrc->a[0].pTab;
1699 pExpr = p->pEList->a[0].pExpr;
drhbbbdc832013-10-22 18:01:40 +00001700 iCol = (i16)pExpr->iColumn;
danielk1977e1fb65a2009-04-02 17:23:32 +00001701
drhb22f7c82014-02-06 23:56:27 +00001702 /* Code an OP_Transaction and OP_TableLock for <table>. */
drhedf83d12014-01-22 18:31:27 +00001703 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
1704 sqlite3CodeVerifySchema(pParse, iDb);
1705 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
danielk19779a96b662007-11-29 17:05:18 +00001706
1707 /* This function is only called from two places. In both cases the vdbe
1708 ** has already been allocated. So assume sqlite3GetVdbe() is always
1709 ** successful here.
1710 */
1711 assert(v);
1712 if( iCol<0 ){
drh7d176102014-02-18 03:07:12 +00001713 int iAddr = sqlite3CodeOnce(pParse);
1714 VdbeCoverage(v);
danielk19779a96b662007-11-29 17:05:18 +00001715
1716 sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
1717 eType = IN_INDEX_ROWID;
1718
1719 sqlite3VdbeJumpHere(v, iAddr);
1720 }else{
danielk1977e1fb65a2009-04-02 17:23:32 +00001721 Index *pIdx; /* Iterator variable */
1722
drhb74b1012009-05-28 21:04:37 +00001723 /* The collation sequence used by the comparison. If an index is to
danielk19779a96b662007-11-29 17:05:18 +00001724 ** be used in place of a temp-table, it must be ordered according
danielk1977e1fb65a2009-04-02 17:23:32 +00001725 ** to this collation sequence. */
danielk19779a96b662007-11-29 17:05:18 +00001726 CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pExpr);
1727
1728 /* Check that the affinity that will be used to perform the
1729 ** comparison is the same as the affinity of the column. If
1730 ** it is not, it is not possible to use any index.
1731 */
drhdbaee5e2012-09-18 19:29:06 +00001732 int affinity_ok = sqlite3IndexAffinityOk(pX, pTab->aCol[iCol].affinity);
danielk19779a96b662007-11-29 17:05:18 +00001733
1734 for(pIdx=pTab->pIndex; pIdx && eType==0 && affinity_ok; pIdx=pIdx->pNext){
1735 if( (pIdx->aiColumn[0]==iCol)
drhb74b1012009-05-28 21:04:37 +00001736 && sqlite3FindCollSeq(db, ENC(db), pIdx->azColl[0], 0)==pReq
drh5f1d1d92014-07-31 22:59:04 +00001737 && (!mustBeUnique || (pIdx->nKeyCol==1 && IsUniqueIndex(pIdx)))
danielk19779a96b662007-11-29 17:05:18 +00001738 ){
drh7d176102014-02-18 03:07:12 +00001739 int iAddr = sqlite3CodeOnce(pParse); VdbeCoverage(v);
drh2ec2fb22013-11-06 19:59:23 +00001740 sqlite3VdbeAddOp3(v, OP_OpenRead, iTab, pIdx->tnum, iDb);
1741 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
danielk1977207872a2008-01-03 07:54:23 +00001742 VdbeComment((v, "%s", pIdx->zName));
drh1ccce442013-03-12 20:38:51 +00001743 assert( IN_INDEX_INDEX_DESC == IN_INDEX_INDEX_ASC+1 );
1744 eType = IN_INDEX_INDEX_ASC + pIdx->aSortOrder[0];
danielk19779a96b662007-11-29 17:05:18 +00001745
drhe21a6e12014-08-01 18:00:24 +00001746 if( prRhsHasNull && !pTab->aCol[iCol].notNull ){
1747 *prRhsHasNull = ++pParse->nMem;
drh6be515e2014-08-01 21:00:53 +00001748 sqlite3SetHasNullFlag(v, iTab, *prRhsHasNull);
danielk19770cdc0222008-06-26 18:04:03 +00001749 }
drh552fd452014-02-18 01:07:38 +00001750 sqlite3VdbeJumpHere(v, iAddr);
danielk19779a96b662007-11-29 17:05:18 +00001751 }
1752 }
1753 }
1754 }
1755
drhbb53ecb2014-08-02 21:03:33 +00001756 /* If no preexisting index is available for the IN clause
1757 ** and IN_INDEX_NOOP is an allowed reply
1758 ** and the RHS of the IN operator is a list, not a subquery
1759 ** and the RHS is not contant or has two or fewer terms,
peter.d.reid60ec9142014-09-06 16:39:46 +00001760 ** then it is not worth creating an ephemeral table to evaluate
drhbb53ecb2014-08-02 21:03:33 +00001761 ** the IN operator so return IN_INDEX_NOOP.
1762 */
1763 if( eType==0
1764 && (inFlags & IN_INDEX_NOOP_OK)
1765 && !ExprHasProperty(pX, EP_xIsSelect)
1766 && (!sqlite3InRhsIsConstant(pX) || pX->x.pList->nExpr<=2)
1767 ){
1768 eType = IN_INDEX_NOOP;
1769 }
1770
1771
danielk19779a96b662007-11-29 17:05:18 +00001772 if( eType==0 ){
drh43870062014-07-31 15:44:44 +00001773 /* Could not find an existing table or index to use as the RHS b-tree.
drhb74b1012009-05-28 21:04:37 +00001774 ** We will have to generate an ephemeral table to do the job.
1775 */
drh8e23daf2013-06-11 13:30:04 +00001776 u32 savedNQueryLoop = pParse->nQueryLoop;
danielk19770cdc0222008-06-26 18:04:03 +00001777 int rMayHaveNull = 0;
danielk197741a05b72008-10-02 13:50:55 +00001778 eType = IN_INDEX_EPH;
drh3a856252014-08-01 14:46:57 +00001779 if( inFlags & IN_INDEX_LOOP ){
drh4a5acf82013-06-18 20:06:23 +00001780 pParse->nQueryLoop = 0;
drhc5cd1242013-09-12 16:50:49 +00001781 if( pX->pLeft->iColumn<0 && !ExprHasProperty(pX, EP_xIsSelect) ){
drhcf4d38a2010-07-28 02:53:36 +00001782 eType = IN_INDEX_ROWID;
1783 }
drhe21a6e12014-08-01 18:00:24 +00001784 }else if( prRhsHasNull ){
1785 *prRhsHasNull = rMayHaveNull = ++pParse->nMem;
danielk19770cdc0222008-06-26 18:04:03 +00001786 }
danielk197741a05b72008-10-02 13:50:55 +00001787 sqlite3CodeSubselect(pParse, pX, rMayHaveNull, eType==IN_INDEX_ROWID);
drhcf4d38a2010-07-28 02:53:36 +00001788 pParse->nQueryLoop = savedNQueryLoop;
danielk19779a96b662007-11-29 17:05:18 +00001789 }else{
1790 pX->iTable = iTab;
1791 }
1792 return eType;
1793}
danielk1977284f4ac2007-12-10 05:03:46 +00001794#endif
drh626a8792005-01-17 22:08:19 +00001795
1796/*
drhd4187c72010-08-30 22:15:45 +00001797** Generate code for scalar subqueries used as a subquery expression, EXISTS,
1798** or IN operators. Examples:
drh626a8792005-01-17 22:08:19 +00001799**
drh9cbe6352005-11-29 03:13:21 +00001800** (SELECT a FROM b) -- subquery
1801** EXISTS (SELECT a FROM b) -- EXISTS subquery
1802** x IN (4,5,11) -- IN operator with list on right-hand side
1803** x IN (SELECT a FROM b) -- IN operator with subquery on the right
drhfef52082000-06-06 01:50:43 +00001804**
drh9cbe6352005-11-29 03:13:21 +00001805** The pExpr parameter describes the expression that contains the IN
1806** operator or subquery.
danielk197741a05b72008-10-02 13:50:55 +00001807**
1808** If parameter isRowid is non-zero, then expression pExpr is guaranteed
1809** to be of the form "<rowid> IN (?, ?, ?)", where <rowid> is a reference
1810** to some integer key column of a table B-Tree. In this case, use an
1811** intkey B-Tree to store the set of IN(...) values instead of the usual
1812** (slower) variable length keys B-Tree.
drhfd773cf2009-05-29 14:39:07 +00001813**
1814** If rMayHaveNull is non-zero, that means that the operation is an IN
1815** (not a SELECT or EXISTS) and that the RHS might contains NULLs.
drh3a856252014-08-01 14:46:57 +00001816** All this routine does is initialize the register given by rMayHaveNull
1817** to NULL. Calling routines will take care of changing this register
1818** value to non-NULL if the RHS is NULL-free.
drh1450bc62009-10-30 13:25:56 +00001819**
1820** For a SELECT or EXISTS operator, return the register that holds the
1821** result. For IN operators or if an error occurs, the return value is 0.
drhcce7d172000-05-31 15:34:51 +00001822*/
drh51522cd2005-01-20 13:36:19 +00001823#ifndef SQLITE_OMIT_SUBQUERY
drh1450bc62009-10-30 13:25:56 +00001824int sqlite3CodeSubselect(
drhfd773cf2009-05-29 14:39:07 +00001825 Parse *pParse, /* Parsing context */
1826 Expr *pExpr, /* The IN, SELECT, or EXISTS operator */
drh6be515e2014-08-01 21:00:53 +00001827 int rHasNullFlag, /* Register that records whether NULLs exist in RHS */
drhfd773cf2009-05-29 14:39:07 +00001828 int isRowid /* If true, LHS of IN operator is a rowid */
danielk197741a05b72008-10-02 13:50:55 +00001829){
drh6be515e2014-08-01 21:00:53 +00001830 int jmpIfDynamic = -1; /* One-time test address */
drh1450bc62009-10-30 13:25:56 +00001831 int rReg = 0; /* Register storing resulting */
danielk1977b3bce662005-01-29 08:32:43 +00001832 Vdbe *v = sqlite3GetVdbe(pParse);
drh1450bc62009-10-30 13:25:56 +00001833 if( NEVER(v==0) ) return 0;
drhceea3322009-04-23 13:22:42 +00001834 sqlite3ExprCachePush(pParse);
danielk1977fc976062007-05-10 10:46:56 +00001835
drh57dbd7b2005-07-08 18:25:26 +00001836 /* This code must be run in its entirety every time it is encountered
1837 ** if any of the following is true:
1838 **
1839 ** * The right-hand side is a correlated subquery
1840 ** * The right-hand side is an expression list containing variables
1841 ** * We are inside a trigger
1842 **
1843 ** If all of the above are false, then we can run this code just once
1844 ** save the results, and reuse the same result on subsequent invocations.
danielk1977b3bce662005-01-29 08:32:43 +00001845 */
drhc5cd1242013-09-12 16:50:49 +00001846 if( !ExprHasProperty(pExpr, EP_VarSelect) ){
drh6be515e2014-08-01 21:00:53 +00001847 jmpIfDynamic = sqlite3CodeOnce(pParse); VdbeCoverage(v);
danielk1977b3bce662005-01-29 08:32:43 +00001848 }
1849
dan4a07e3d2010-11-09 14:48:59 +00001850#ifndef SQLITE_OMIT_EXPLAIN
1851 if( pParse->explain==2 ){
1852 char *zMsg = sqlite3MPrintf(
drh6be515e2014-08-01 21:00:53 +00001853 pParse->db, "EXECUTE %s%s SUBQUERY %d", jmpIfDynamic>=0?"":"CORRELATED ",
dan4a07e3d2010-11-09 14:48:59 +00001854 pExpr->op==TK_IN?"LIST":"SCALAR", pParse->iNextSelectId
1855 );
1856 sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC);
1857 }
1858#endif
1859
drhcce7d172000-05-31 15:34:51 +00001860 switch( pExpr->op ){
drhfef52082000-06-06 01:50:43 +00001861 case TK_IN: {
drhd4187c72010-08-30 22:15:45 +00001862 char affinity; /* Affinity of the LHS of the IN */
drhd4187c72010-08-30 22:15:45 +00001863 int addr; /* Address of OP_OpenEphemeral instruction */
1864 Expr *pLeft = pExpr->pLeft; /* the LHS of the IN operator */
drh323df792013-08-05 19:11:29 +00001865 KeyInfo *pKeyInfo = 0; /* Key information */
drhd3d39e92004-05-20 22:16:29 +00001866
danielk197741a05b72008-10-02 13:50:55 +00001867 affinity = sqlite3ExprAffinity(pLeft);
danielk1977e014a832004-05-17 10:48:57 +00001868
1869 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
drh8cff69d2009-11-12 19:59:44 +00001870 ** expression it is handled the same way. An ephemeral table is
danielk1977e014a832004-05-17 10:48:57 +00001871 ** filled with single-field index keys representing the results
1872 ** from the SELECT or the <exprlist>.
1873 **
1874 ** If the 'x' expression is a column value, or the SELECT...
1875 ** statement returns a column value, then the affinity of that
1876 ** column is used to build the index keys. If both 'x' and the
1877 ** SELECT... statement are columns, then numeric affinity is used
1878 ** if either column has NUMERIC or INTEGER affinity. If neither
1879 ** 'x' nor the SELECT... statement are columns, then numeric affinity
1880 ** is used.
1881 */
1882 pExpr->iTable = pParse->nTab++;
danielk197741a05b72008-10-02 13:50:55 +00001883 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pExpr->iTable, !isRowid);
drhad124322013-10-23 13:30:58 +00001884 pKeyInfo = isRowid ? 0 : sqlite3KeyInfoAlloc(pParse->db, 1, 1);
danielk1977e014a832004-05-17 10:48:57 +00001885
danielk19776ab3a2e2009-02-19 14:39:25 +00001886 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
drhfef52082000-06-06 01:50:43 +00001887 /* Case 1: expr IN (SELECT ...)
1888 **
danielk1977e014a832004-05-17 10:48:57 +00001889 ** Generate code to write the results of the select into the temporary
1890 ** table allocated and opened above.
drhfef52082000-06-06 01:50:43 +00001891 */
drh43870062014-07-31 15:44:44 +00001892 Select *pSelect = pExpr->x.pSelect;
drh1013c932008-01-06 00:25:21 +00001893 SelectDest dest;
drhbe5c89a2004-07-26 00:31:09 +00001894 ExprList *pEList;
drh1013c932008-01-06 00:25:21 +00001895
danielk197741a05b72008-10-02 13:50:55 +00001896 assert( !isRowid );
drh1013c932008-01-06 00:25:21 +00001897 sqlite3SelectDestInit(&dest, SRT_Set, pExpr->iTable);
drh2b596da2012-07-23 21:43:19 +00001898 dest.affSdst = (u8)affinity;
danielk1977e014a832004-05-17 10:48:57 +00001899 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
drh43870062014-07-31 15:44:44 +00001900 pSelect->iLimit = 0;
1901 testcase( pSelect->selFlags & SF_Distinct );
drh812ea832013-08-06 17:24:23 +00001902 testcase( pKeyInfo==0 ); /* Caused by OOM in sqlite3KeyInfoAlloc() */
drh43870062014-07-31 15:44:44 +00001903 if( sqlite3Select(pParse, pSelect, &dest) ){
drh2ec2fb22013-11-06 19:59:23 +00001904 sqlite3KeyInfoUnref(pKeyInfo);
drh1450bc62009-10-30 13:25:56 +00001905 return 0;
drh94ccde52007-04-13 16:06:32 +00001906 }
drh43870062014-07-31 15:44:44 +00001907 pEList = pSelect->pEList;
drh812ea832013-08-06 17:24:23 +00001908 assert( pKeyInfo!=0 ); /* OOM will cause exit after sqlite3Select() */
drh3535ec32013-08-06 16:56:44 +00001909 assert( pEList!=0 );
1910 assert( pEList->nExpr>0 );
drh2ec2fb22013-11-06 19:59:23 +00001911 assert( sqlite3KeyInfoIsWriteable(pKeyInfo) );
drh3535ec32013-08-06 16:56:44 +00001912 pKeyInfo->aColl[0] = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft,
1913 pEList->a[0].pExpr);
drha7d2db12010-07-14 20:23:52 +00001914 }else if( ALWAYS(pExpr->x.pList!=0) ){
drhfef52082000-06-06 01:50:43 +00001915 /* Case 2: expr IN (exprlist)
1916 **
drhfd131da2007-08-07 17:13:03 +00001917 ** For each expression, build an index key from the evaluation and
danielk1977e014a832004-05-17 10:48:57 +00001918 ** store it in the temporary table. If <expr> is a column, then use
1919 ** that columns affinity when building index keys. If <expr> is not
1920 ** a column, use numeric affinity.
drhfef52082000-06-06 01:50:43 +00001921 */
danielk1977e014a832004-05-17 10:48:57 +00001922 int i;
danielk19776ab3a2e2009-02-19 14:39:25 +00001923 ExprList *pList = pExpr->x.pList;
drh57dbd7b2005-07-08 18:25:26 +00001924 struct ExprList_item *pItem;
drhecc31802008-06-26 20:06:06 +00001925 int r1, r2, r3;
drh57dbd7b2005-07-08 18:25:26 +00001926
danielk1977e014a832004-05-17 10:48:57 +00001927 if( !affinity ){
drh8159a352006-05-23 23:22:29 +00001928 affinity = SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +00001929 }
drh323df792013-08-05 19:11:29 +00001930 if( pKeyInfo ){
drh2ec2fb22013-11-06 19:59:23 +00001931 assert( sqlite3KeyInfoIsWriteable(pKeyInfo) );
drh323df792013-08-05 19:11:29 +00001932 pKeyInfo->aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
1933 }
danielk1977e014a832004-05-17 10:48:57 +00001934
1935 /* Loop through each expression in <exprlist>. */
drh2d401ab2008-01-10 23:50:11 +00001936 r1 = sqlite3GetTempReg(pParse);
1937 r2 = sqlite3GetTempReg(pParse);
drh37e08082014-07-31 20:16:08 +00001938 if( isRowid ) sqlite3VdbeAddOp2(v, OP_Null, 0, r2);
drh57dbd7b2005-07-08 18:25:26 +00001939 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
1940 Expr *pE2 = pItem->pExpr;
drhe05c9292009-10-29 13:48:10 +00001941 int iValToIns;
danielk1977e014a832004-05-17 10:48:57 +00001942
drh57dbd7b2005-07-08 18:25:26 +00001943 /* If the expression is not constant then we will need to
1944 ** disable the test that was generated above that makes sure
1945 ** this code only executes once. Because for a non-constant
1946 ** expression we need to rerun this code each time.
1947 */
drh6be515e2014-08-01 21:00:53 +00001948 if( jmpIfDynamic>=0 && !sqlite3ExprIsConstant(pE2) ){
1949 sqlite3VdbeChangeToNoop(v, jmpIfDynamic);
1950 jmpIfDynamic = -1;
drh4794b982000-06-06 13:54:14 +00001951 }
danielk1977e014a832004-05-17 10:48:57 +00001952
1953 /* Evaluate the expression and insert it into the temp table */
drhe05c9292009-10-29 13:48:10 +00001954 if( isRowid && sqlite3ExprIsInteger(pE2, &iValToIns) ){
1955 sqlite3VdbeAddOp3(v, OP_InsertInt, pExpr->iTable, r2, iValToIns);
danielk197741a05b72008-10-02 13:50:55 +00001956 }else{
drhe05c9292009-10-29 13:48:10 +00001957 r3 = sqlite3ExprCodeTarget(pParse, pE2, r1);
1958 if( isRowid ){
1959 sqlite3VdbeAddOp2(v, OP_MustBeInt, r3,
1960 sqlite3VdbeCurrentAddr(v)+2);
drh688852a2014-02-17 22:40:43 +00001961 VdbeCoverage(v);
drhe05c9292009-10-29 13:48:10 +00001962 sqlite3VdbeAddOp3(v, OP_Insert, pExpr->iTable, r2, r3);
1963 }else{
1964 sqlite3VdbeAddOp4(v, OP_MakeRecord, r3, 1, r2, &affinity, 1);
1965 sqlite3ExprCacheAffinityChange(pParse, r3, 1);
1966 sqlite3VdbeAddOp2(v, OP_IdxInsert, pExpr->iTable, r2);
1967 }
danielk197741a05b72008-10-02 13:50:55 +00001968 }
drhfef52082000-06-06 01:50:43 +00001969 }
drh2d401ab2008-01-10 23:50:11 +00001970 sqlite3ReleaseTempReg(pParse, r1);
1971 sqlite3ReleaseTempReg(pParse, r2);
drhfef52082000-06-06 01:50:43 +00001972 }
drh323df792013-08-05 19:11:29 +00001973 if( pKeyInfo ){
drh2ec2fb22013-11-06 19:59:23 +00001974 sqlite3VdbeChangeP4(v, addr, (void *)pKeyInfo, P4_KEYINFO);
danielk197741a05b72008-10-02 13:50:55 +00001975 }
danielk1977b3bce662005-01-29 08:32:43 +00001976 break;
drhfef52082000-06-06 01:50:43 +00001977 }
1978
drh51522cd2005-01-20 13:36:19 +00001979 case TK_EXISTS:
drhfd773cf2009-05-29 14:39:07 +00001980 case TK_SELECT:
1981 default: {
drhfd773cf2009-05-29 14:39:07 +00001982 /* If this has to be a scalar SELECT. Generate code to put the
drhfef52082000-06-06 01:50:43 +00001983 ** value of this select in a memory cell and record the number
drhfd773cf2009-05-29 14:39:07 +00001984 ** of the memory cell in iColumn. If this is an EXISTS, write
1985 ** an integer 0 (not exists) or 1 (exists) into a memory cell
1986 ** and record that memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +00001987 */
drhfd773cf2009-05-29 14:39:07 +00001988 Select *pSel; /* SELECT statement to encode */
1989 SelectDest dest; /* How to deal with SELECt result */
drh1398ad32005-01-19 23:24:50 +00001990
shanecf697392009-06-01 16:53:09 +00001991 testcase( pExpr->op==TK_EXISTS );
1992 testcase( pExpr->op==TK_SELECT );
1993 assert( pExpr->op==TK_EXISTS || pExpr->op==TK_SELECT );
1994
danielk19776ab3a2e2009-02-19 14:39:25 +00001995 assert( ExprHasProperty(pExpr, EP_xIsSelect) );
1996 pSel = pExpr->x.pSelect;
drh1013c932008-01-06 00:25:21 +00001997 sqlite3SelectDestInit(&dest, 0, ++pParse->nMem);
drh51522cd2005-01-20 13:36:19 +00001998 if( pExpr->op==TK_SELECT ){
danielk19776c8c8ce2008-01-02 16:27:09 +00001999 dest.eDest = SRT_Mem;
drh53932ce2014-08-29 12:29:39 +00002000 dest.iSdst = dest.iSDParm;
drh2b596da2012-07-23 21:43:19 +00002001 sqlite3VdbeAddOp2(v, OP_Null, 0, dest.iSDParm);
drhd4e70eb2008-01-02 00:34:36 +00002002 VdbeComment((v, "Init subquery result"));
drh51522cd2005-01-20 13:36:19 +00002003 }else{
danielk19776c8c8ce2008-01-02 16:27:09 +00002004 dest.eDest = SRT_Exists;
drh2b596da2012-07-23 21:43:19 +00002005 sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iSDParm);
drhd4e70eb2008-01-02 00:34:36 +00002006 VdbeComment((v, "Init EXISTS result"));
drh51522cd2005-01-20 13:36:19 +00002007 }
drh633e6d52008-07-28 19:34:53 +00002008 sqlite3ExprDelete(pParse->db, pSel->pLimit);
drh094430e2010-07-14 18:24:06 +00002009 pSel->pLimit = sqlite3PExpr(pParse, TK_INTEGER, 0, 0,
2010 &sqlite3IntTokens[1]);
drh48b5b042010-12-06 18:50:32 +00002011 pSel->iLimit = 0;
drh7d10d5a2008-08-20 16:35:10 +00002012 if( sqlite3Select(pParse, pSel, &dest) ){
drh1450bc62009-10-30 13:25:56 +00002013 return 0;
drh94ccde52007-04-13 16:06:32 +00002014 }
drh2b596da2012-07-23 21:43:19 +00002015 rReg = dest.iSDParm;
drhebb6a652013-09-12 23:42:22 +00002016 ExprSetVVAProperty(pExpr, EP_NoReduce);
danielk1977b3bce662005-01-29 08:32:43 +00002017 break;
drhcce7d172000-05-31 15:34:51 +00002018 }
2019 }
danielk1977b3bce662005-01-29 08:32:43 +00002020
drh6be515e2014-08-01 21:00:53 +00002021 if( rHasNullFlag ){
2022 sqlite3SetHasNullFlag(v, pExpr->iTable, rHasNullFlag);
2023 }
2024
2025 if( jmpIfDynamic>=0 ){
2026 sqlite3VdbeJumpHere(v, jmpIfDynamic);
danielk1977b3bce662005-01-29 08:32:43 +00002027 }
drhd2490902014-04-13 19:28:15 +00002028 sqlite3ExprCachePop(pParse);
danielk1977fc976062007-05-10 10:46:56 +00002029
drh1450bc62009-10-30 13:25:56 +00002030 return rReg;
drhcce7d172000-05-31 15:34:51 +00002031}
drh51522cd2005-01-20 13:36:19 +00002032#endif /* SQLITE_OMIT_SUBQUERY */
drhcce7d172000-05-31 15:34:51 +00002033
drhe3365e62009-11-12 17:52:24 +00002034#ifndef SQLITE_OMIT_SUBQUERY
2035/*
2036** Generate code for an IN expression.
2037**
2038** x IN (SELECT ...)
2039** x IN (value, value, ...)
2040**
2041** The left-hand side (LHS) is a scalar expression. The right-hand side (RHS)
2042** is an array of zero or more values. The expression is true if the LHS is
2043** contained within the RHS. The value of the expression is unknown (NULL)
2044** if the LHS is NULL or if the LHS is not contained within the RHS and the
2045** RHS contains one or more NULL values.
2046**
drh6be515e2014-08-01 21:00:53 +00002047** This routine generates code that jumps to destIfFalse if the LHS is not
drhe3365e62009-11-12 17:52:24 +00002048** contained within the RHS. If due to NULLs we cannot determine if the LHS
2049** is contained in the RHS then jump to destIfNull. If the LHS is contained
2050** within the RHS then fall through.
2051*/
2052static void sqlite3ExprCodeIN(
2053 Parse *pParse, /* Parsing and code generating context */
2054 Expr *pExpr, /* The IN expression */
2055 int destIfFalse, /* Jump here if LHS is not contained in the RHS */
2056 int destIfNull /* Jump here if the results are unknown due to NULLs */
2057){
2058 int rRhsHasNull = 0; /* Register that is true if RHS contains NULL values */
2059 char affinity; /* Comparison affinity to use */
2060 int eType; /* Type of the RHS */
2061 int r1; /* Temporary use register */
2062 Vdbe *v; /* Statement under construction */
2063
2064 /* Compute the RHS. After this step, the table with cursor
2065 ** pExpr->iTable will contains the values that make up the RHS.
2066 */
2067 v = pParse->pVdbe;
2068 assert( v!=0 ); /* OOM detected prior to this routine */
2069 VdbeNoopComment((v, "begin IN expr"));
drhbb53ecb2014-08-02 21:03:33 +00002070 eType = sqlite3FindInIndex(pParse, pExpr,
2071 IN_INDEX_MEMBERSHIP | IN_INDEX_NOOP_OK,
drh3a856252014-08-01 14:46:57 +00002072 destIfFalse==destIfNull ? 0 : &rRhsHasNull);
drhe3365e62009-11-12 17:52:24 +00002073
2074 /* Figure out the affinity to use to create a key from the results
2075 ** of the expression. affinityStr stores a static string suitable for
2076 ** P4 of OP_MakeRecord.
2077 */
2078 affinity = comparisonAffinity(pExpr);
2079
2080 /* Code the LHS, the <expr> from "<expr> IN (...)".
2081 */
2082 sqlite3ExprCachePush(pParse);
2083 r1 = sqlite3GetTempReg(pParse);
2084 sqlite3ExprCode(pParse, pExpr->pLeft, r1);
drhe3365e62009-11-12 17:52:24 +00002085
drhbb53ecb2014-08-02 21:03:33 +00002086 /* If sqlite3FindInIndex() did not find or create an index that is
2087 ** suitable for evaluating the IN operator, then evaluate using a
2088 ** sequence of comparisons.
drh094430e2010-07-14 18:24:06 +00002089 */
drhbb53ecb2014-08-02 21:03:33 +00002090 if( eType==IN_INDEX_NOOP ){
2091 ExprList *pList = pExpr->x.pList;
2092 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
2093 int labelOk = sqlite3VdbeMakeLabel(v);
2094 int r2, regToFree;
2095 int regCkNull = 0;
2096 int ii;
2097 assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
drhbb53ecb2014-08-02 21:03:33 +00002098 if( destIfNull!=destIfFalse ){
2099 regCkNull = sqlite3GetTempReg(pParse);
drha9769792014-08-04 16:39:39 +00002100 sqlite3VdbeAddOp3(v, OP_BitAnd, r1, r1, regCkNull);
drhbb53ecb2014-08-02 21:03:33 +00002101 }
2102 for(ii=0; ii<pList->nExpr; ii++){
2103 r2 = sqlite3ExprCodeTemp(pParse, pList->a[ii].pExpr, &regToFree);
drha9769792014-08-04 16:39:39 +00002104 if( regCkNull && sqlite3ExprCanBeNull(pList->a[ii].pExpr) ){
drhbb53ecb2014-08-02 21:03:33 +00002105 sqlite3VdbeAddOp3(v, OP_BitAnd, regCkNull, r2, regCkNull);
2106 }
2107 if( ii<pList->nExpr-1 || destIfNull!=destIfFalse ){
2108 sqlite3VdbeAddOp4(v, OP_Eq, r1, labelOk, r2,
drh4336b0e2014-08-05 00:53:51 +00002109 (void*)pColl, P4_COLLSEQ);
2110 VdbeCoverageIf(v, ii<pList->nExpr-1);
2111 VdbeCoverageIf(v, ii==pList->nExpr-1);
drhbb53ecb2014-08-02 21:03:33 +00002112 sqlite3VdbeChangeP5(v, affinity);
2113 }else{
2114 assert( destIfNull==destIfFalse );
2115 sqlite3VdbeAddOp4(v, OP_Ne, r1, destIfFalse, r2,
2116 (void*)pColl, P4_COLLSEQ); VdbeCoverage(v);
2117 sqlite3VdbeChangeP5(v, affinity | SQLITE_JUMPIFNULL);
2118 }
2119 sqlite3ReleaseTempReg(pParse, regToFree);
2120 }
2121 if( regCkNull ){
2122 sqlite3VdbeAddOp2(v, OP_IsNull, regCkNull, destIfNull); VdbeCoverage(v);
2123 sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfFalse);
2124 }
2125 sqlite3VdbeResolveLabel(v, labelOk);
2126 sqlite3ReleaseTempReg(pParse, regCkNull);
drh094430e2010-07-14 18:24:06 +00002127 }else{
drhbb53ecb2014-08-02 21:03:33 +00002128
2129 /* If the LHS is NULL, then the result is either false or NULL depending
2130 ** on whether the RHS is empty or not, respectively.
drhe3365e62009-11-12 17:52:24 +00002131 */
drh7248a8b2014-08-04 18:50:54 +00002132 if( sqlite3ExprCanBeNull(pExpr->pLeft) ){
2133 if( destIfNull==destIfFalse ){
2134 /* Shortcut for the common case where the false and NULL outcomes are
2135 ** the same. */
2136 sqlite3VdbeAddOp2(v, OP_IsNull, r1, destIfNull); VdbeCoverage(v);
2137 }else{
2138 int addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, r1); VdbeCoverage(v);
2139 sqlite3VdbeAddOp2(v, OP_Rewind, pExpr->iTable, destIfFalse);
2140 VdbeCoverage(v);
2141 sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfNull);
2142 sqlite3VdbeJumpHere(v, addr1);
2143 }
drhbb53ecb2014-08-02 21:03:33 +00002144 }
2145
2146 if( eType==IN_INDEX_ROWID ){
2147 /* In this case, the RHS is the ROWID of table b-tree
drhe3365e62009-11-12 17:52:24 +00002148 */
drhbb53ecb2014-08-02 21:03:33 +00002149 sqlite3VdbeAddOp2(v, OP_MustBeInt, r1, destIfFalse); VdbeCoverage(v);
2150 sqlite3VdbeAddOp3(v, OP_NotExists, pExpr->iTable, destIfFalse, r1);
drh688852a2014-02-17 22:40:43 +00002151 VdbeCoverage(v);
drhe3365e62009-11-12 17:52:24 +00002152 }else{
drhbb53ecb2014-08-02 21:03:33 +00002153 /* In this case, the RHS is an index b-tree.
drhe3365e62009-11-12 17:52:24 +00002154 */
drhbb53ecb2014-08-02 21:03:33 +00002155 sqlite3VdbeAddOp4(v, OP_Affinity, r1, 1, 0, &affinity, 1);
2156
2157 /* If the set membership test fails, then the result of the
2158 ** "x IN (...)" expression must be either 0 or NULL. If the set
2159 ** contains no NULL values, then the result is 0. If the set
2160 ** contains one or more NULL values, then the result of the
2161 ** expression is also NULL.
drhe3365e62009-11-12 17:52:24 +00002162 */
drhbb53ecb2014-08-02 21:03:33 +00002163 assert( destIfFalse!=destIfNull || rRhsHasNull==0 );
2164 if( rRhsHasNull==0 ){
2165 /* This branch runs if it is known at compile time that the RHS
2166 ** cannot contain NULL values. This happens as the result
2167 ** of a "NOT NULL" constraint in the database schema.
2168 **
2169 ** Also run this branch if NULL is equivalent to FALSE
2170 ** for this particular IN operator.
2171 */
2172 sqlite3VdbeAddOp4Int(v, OP_NotFound, pExpr->iTable, destIfFalse, r1, 1);
2173 VdbeCoverage(v);
2174 }else{
2175 /* In this branch, the RHS of the IN might contain a NULL and
2176 ** the presence of a NULL on the RHS makes a difference in the
2177 ** outcome.
2178 */
2179 int j1;
2180
2181 /* First check to see if the LHS is contained in the RHS. If so,
2182 ** then the answer is TRUE the presence of NULLs in the RHS does
2183 ** not matter. If the LHS is not contained in the RHS, then the
2184 ** answer is NULL if the RHS contains NULLs and the answer is
2185 ** FALSE if the RHS is NULL-free.
2186 */
2187 j1 = sqlite3VdbeAddOp4Int(v, OP_Found, pExpr->iTable, 0, r1, 1);
2188 VdbeCoverage(v);
2189 sqlite3VdbeAddOp2(v, OP_IsNull, rRhsHasNull, destIfNull);
2190 VdbeCoverage(v);
2191 sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfFalse);
2192 sqlite3VdbeJumpHere(v, j1);
2193 }
drhe3365e62009-11-12 17:52:24 +00002194 }
drhe3365e62009-11-12 17:52:24 +00002195 }
2196 sqlite3ReleaseTempReg(pParse, r1);
drhd2490902014-04-13 19:28:15 +00002197 sqlite3ExprCachePop(pParse);
drhe3365e62009-11-12 17:52:24 +00002198 VdbeComment((v, "end IN expr"));
2199}
2200#endif /* SQLITE_OMIT_SUBQUERY */
2201
drhcce7d172000-05-31 15:34:51 +00002202/*
drh598f1342007-10-23 15:39:45 +00002203** Duplicate an 8-byte value
2204*/
2205static char *dup8bytes(Vdbe *v, const char *in){
2206 char *out = sqlite3DbMallocRaw(sqlite3VdbeDb(v), 8);
2207 if( out ){
2208 memcpy(out, in, 8);
2209 }
2210 return out;
2211}
2212
drh13573c72010-01-12 17:04:07 +00002213#ifndef SQLITE_OMIT_FLOATING_POINT
drh598f1342007-10-23 15:39:45 +00002214/*
2215** Generate an instruction that will put the floating point
drh9cbf3422008-01-17 16:22:13 +00002216** value described by z[0..n-1] into register iMem.
drh0cf19ed2007-10-23 18:55:48 +00002217**
2218** The z[] string will probably not be zero-terminated. But the
2219** z[n] character is guaranteed to be something that does not look
2220** like the continuation of the number.
drh598f1342007-10-23 15:39:45 +00002221*/
drhb7916a72009-05-27 10:31:29 +00002222static void codeReal(Vdbe *v, const char *z, int negateFlag, int iMem){
drhfd773cf2009-05-29 14:39:07 +00002223 if( ALWAYS(z!=0) ){
drh598f1342007-10-23 15:39:45 +00002224 double value;
2225 char *zV;
drh9339da12010-09-30 00:50:49 +00002226 sqlite3AtoF(z, &value, sqlite3Strlen30(z), SQLITE_UTF8);
drhd0015162009-08-21 13:22:25 +00002227 assert( !sqlite3IsNaN(value) ); /* The new AtoF never returns NaN */
2228 if( negateFlag ) value = -value;
2229 zV = dup8bytes(v, (char*)&value);
2230 sqlite3VdbeAddOp4(v, OP_Real, 0, iMem, 0, zV, P4_REAL);
drh598f1342007-10-23 15:39:45 +00002231 }
2232}
drh13573c72010-01-12 17:04:07 +00002233#endif
drh598f1342007-10-23 15:39:45 +00002234
2235
2236/*
drhfec19aa2004-05-19 20:41:03 +00002237** Generate an instruction that will put the integer describe by
drh9cbf3422008-01-17 16:22:13 +00002238** text z[0..n-1] into register iMem.
drh0cf19ed2007-10-23 18:55:48 +00002239**
shaneh5f1d6b62010-09-30 16:51:25 +00002240** Expr.u.zToken is always UTF8 and zero-terminated.
drhfec19aa2004-05-19 20:41:03 +00002241*/
drh13573c72010-01-12 17:04:07 +00002242static void codeInteger(Parse *pParse, Expr *pExpr, int negFlag, int iMem){
2243 Vdbe *v = pParse->pVdbe;
drh92b01d52008-06-24 00:32:35 +00002244 if( pExpr->flags & EP_IntValue ){
drh33e619f2009-05-28 01:00:55 +00002245 int i = pExpr->u.iValue;
drhd50ffc42011-03-08 02:38:28 +00002246 assert( i>=0 );
drh92b01d52008-06-24 00:32:35 +00002247 if( negFlag ) i = -i;
2248 sqlite3VdbeAddOp2(v, OP_Integer, i, iMem);
drhfd773cf2009-05-29 14:39:07 +00002249 }else{
shaneh5f1d6b62010-09-30 16:51:25 +00002250 int c;
2251 i64 value;
drhfd773cf2009-05-29 14:39:07 +00002252 const char *z = pExpr->u.zToken;
2253 assert( z!=0 );
drh9296c182014-07-23 13:40:49 +00002254 c = sqlite3DecOrHexToI64(z, &value);
shaneh5f1d6b62010-09-30 16:51:25 +00002255 if( c==0 || (c==2 && negFlag) ){
drh598f1342007-10-23 15:39:45 +00002256 char *zV;
drh158b9cb2011-03-05 20:59:46 +00002257 if( negFlag ){ value = c==2 ? SMALLEST_INT64 : -value; }
drh598f1342007-10-23 15:39:45 +00002258 zV = dup8bytes(v, (char*)&value);
drh9de221d2008-01-05 06:51:30 +00002259 sqlite3VdbeAddOp4(v, OP_Int64, 0, iMem, 0, zV, P4_INT64);
danielk1977c9cf9012007-05-30 10:36:47 +00002260 }else{
drh13573c72010-01-12 17:04:07 +00002261#ifdef SQLITE_OMIT_FLOATING_POINT
2262 sqlite3ErrorMsg(pParse, "oversized integer: %s%s", negFlag ? "-" : "", z);
2263#else
drh1b7ddc52014-07-23 14:52:05 +00002264#ifndef SQLITE_OMIT_HEX_INTEGER
drh9296c182014-07-23 13:40:49 +00002265 if( sqlite3_strnicmp(z,"0x",2)==0 ){
2266 sqlite3ErrorMsg(pParse, "hex literal too big: %s", z);
drh1b7ddc52014-07-23 14:52:05 +00002267 }else
2268#endif
2269 {
drh9296c182014-07-23 13:40:49 +00002270 codeReal(v, z, negFlag, iMem);
2271 }
drh13573c72010-01-12 17:04:07 +00002272#endif
danielk1977c9cf9012007-05-30 10:36:47 +00002273 }
drhfec19aa2004-05-19 20:41:03 +00002274 }
2275}
2276
drhceea3322009-04-23 13:22:42 +00002277/*
2278** Clear a cache entry.
2279*/
2280static void cacheEntryClear(Parse *pParse, struct yColCache *p){
2281 if( p->tempReg ){
2282 if( pParse->nTempReg<ArraySize(pParse->aTempReg) ){
2283 pParse->aTempReg[pParse->nTempReg++] = p->iReg;
2284 }
2285 p->tempReg = 0;
2286 }
2287}
2288
2289
2290/*
2291** Record in the column cache that a particular column from a
2292** particular table is stored in a particular register.
2293*/
2294void sqlite3ExprCacheStore(Parse *pParse, int iTab, int iCol, int iReg){
2295 int i;
2296 int minLru;
2297 int idxLru;
2298 struct yColCache *p;
2299
dance8f53d2015-01-21 17:00:57 +00002300 /* Unless an error has occurred, register numbers are always positive. */
2301 assert( iReg>0 || pParse->nErr || pParse->db->mallocFailed );
drh20411ea2009-05-29 19:00:12 +00002302 assert( iCol>=-1 && iCol<32768 ); /* Finite column numbers */
2303
drhb6da74e2009-12-24 16:00:28 +00002304 /* The SQLITE_ColumnCache flag disables the column cache. This is used
2305 ** for testing only - to verify that SQLite always gets the same answer
2306 ** with and without the column cache.
2307 */
drh7e5418e2012-09-27 15:05:54 +00002308 if( OptimizationDisabled(pParse->db, SQLITE_ColumnCache) ) return;
drhb6da74e2009-12-24 16:00:28 +00002309
drh27ee4062009-12-30 01:13:11 +00002310 /* First replace any existing entry.
2311 **
2312 ** Actually, the way the column cache is currently used, we are guaranteed
2313 ** that the object will never already be in cache. Verify this guarantee.
2314 */
2315#ifndef NDEBUG
drhceea3322009-04-23 13:22:42 +00002316 for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
drh27ee4062009-12-30 01:13:11 +00002317 assert( p->iReg==0 || p->iTable!=iTab || p->iColumn!=iCol );
drhceea3322009-04-23 13:22:42 +00002318 }
drh27ee4062009-12-30 01:13:11 +00002319#endif
drhceea3322009-04-23 13:22:42 +00002320
2321 /* Find an empty slot and replace it */
2322 for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
2323 if( p->iReg==0 ){
2324 p->iLevel = pParse->iCacheLevel;
2325 p->iTable = iTab;
2326 p->iColumn = iCol;
2327 p->iReg = iReg;
drhceea3322009-04-23 13:22:42 +00002328 p->tempReg = 0;
2329 p->lru = pParse->iCacheCnt++;
2330 return;
2331 }
2332 }
2333
2334 /* Replace the last recently used */
2335 minLru = 0x7fffffff;
2336 idxLru = -1;
2337 for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
2338 if( p->lru<minLru ){
2339 idxLru = i;
2340 minLru = p->lru;
2341 }
2342 }
drh20411ea2009-05-29 19:00:12 +00002343 if( ALWAYS(idxLru>=0) ){
drhceea3322009-04-23 13:22:42 +00002344 p = &pParse->aColCache[idxLru];
2345 p->iLevel = pParse->iCacheLevel;
2346 p->iTable = iTab;
2347 p->iColumn = iCol;
2348 p->iReg = iReg;
drhceea3322009-04-23 13:22:42 +00002349 p->tempReg = 0;
2350 p->lru = pParse->iCacheCnt++;
2351 return;
2352 }
2353}
2354
2355/*
drhf49f3522009-12-30 14:12:38 +00002356** Indicate that registers between iReg..iReg+nReg-1 are being overwritten.
2357** Purge the range of registers from the column cache.
drhceea3322009-04-23 13:22:42 +00002358*/
drhf49f3522009-12-30 14:12:38 +00002359void sqlite3ExprCacheRemove(Parse *pParse, int iReg, int nReg){
drhceea3322009-04-23 13:22:42 +00002360 int i;
drhf49f3522009-12-30 14:12:38 +00002361 int iLast = iReg + nReg - 1;
drhceea3322009-04-23 13:22:42 +00002362 struct yColCache *p;
2363 for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
drhf49f3522009-12-30 14:12:38 +00002364 int r = p->iReg;
2365 if( r>=iReg && r<=iLast ){
drhceea3322009-04-23 13:22:42 +00002366 cacheEntryClear(pParse, p);
2367 p->iReg = 0;
2368 }
2369 }
2370}
2371
2372/*
2373** Remember the current column cache context. Any new entries added
2374** added to the column cache after this call are removed when the
2375** corresponding pop occurs.
2376*/
2377void sqlite3ExprCachePush(Parse *pParse){
2378 pParse->iCacheLevel++;
drh9ac79622013-12-18 15:11:47 +00002379#ifdef SQLITE_DEBUG
2380 if( pParse->db->flags & SQLITE_VdbeAddopTrace ){
2381 printf("PUSH to %d\n", pParse->iCacheLevel);
2382 }
2383#endif
drhceea3322009-04-23 13:22:42 +00002384}
2385
2386/*
2387** Remove from the column cache any entries that were added since the
drhd2490902014-04-13 19:28:15 +00002388** the previous sqlite3ExprCachePush operation. In other words, restore
2389** the cache to the state it was in prior the most recent Push.
drhceea3322009-04-23 13:22:42 +00002390*/
drhd2490902014-04-13 19:28:15 +00002391void sqlite3ExprCachePop(Parse *pParse){
drhceea3322009-04-23 13:22:42 +00002392 int i;
2393 struct yColCache *p;
drhd2490902014-04-13 19:28:15 +00002394 assert( pParse->iCacheLevel>=1 );
2395 pParse->iCacheLevel--;
drh9ac79622013-12-18 15:11:47 +00002396#ifdef SQLITE_DEBUG
2397 if( pParse->db->flags & SQLITE_VdbeAddopTrace ){
2398 printf("POP to %d\n", pParse->iCacheLevel);
2399 }
2400#endif
drhceea3322009-04-23 13:22:42 +00002401 for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
2402 if( p->iReg && p->iLevel>pParse->iCacheLevel ){
2403 cacheEntryClear(pParse, p);
2404 p->iReg = 0;
2405 }
2406 }
2407}
drh945498f2007-02-24 11:52:52 +00002408
2409/*
drh5cd79232009-05-25 11:46:29 +00002410** When a cached column is reused, make sure that its register is
2411** no longer available as a temp register. ticket #3879: that same
2412** register might be in the cache in multiple places, so be sure to
2413** get them all.
2414*/
2415static void sqlite3ExprCachePinRegister(Parse *pParse, int iReg){
2416 int i;
2417 struct yColCache *p;
2418 for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
2419 if( p->iReg==iReg ){
2420 p->tempReg = 0;
2421 }
2422 }
2423}
2424
2425/*
drh5c092e82010-05-14 19:24:02 +00002426** Generate code to extract the value of the iCol-th column of a table.
2427*/
2428void sqlite3ExprCodeGetColumnOfTable(
2429 Vdbe *v, /* The VDBE under construction */
2430 Table *pTab, /* The table containing the value */
drh313619f2013-10-31 20:34:06 +00002431 int iTabCur, /* The table cursor. Or the PK cursor for WITHOUT ROWID */
drh5c092e82010-05-14 19:24:02 +00002432 int iCol, /* Index of the column to extract */
drh313619f2013-10-31 20:34:06 +00002433 int regOut /* Extract the value into this register */
drh5c092e82010-05-14 19:24:02 +00002434){
2435 if( iCol<0 || iCol==pTab->iPKey ){
2436 sqlite3VdbeAddOp2(v, OP_Rowid, iTabCur, regOut);
2437 }else{
2438 int op = IsVirtual(pTab) ? OP_VColumn : OP_Column;
drhee0ec8e2013-10-31 17:38:01 +00002439 int x = iCol;
2440 if( !HasRowid(pTab) ){
2441 x = sqlite3ColumnOfIndex(sqlite3PrimaryKeyIndex(pTab), iCol);
2442 }
2443 sqlite3VdbeAddOp3(v, op, iTabCur, x, regOut);
drh5c092e82010-05-14 19:24:02 +00002444 }
2445 if( iCol>=0 ){
2446 sqlite3ColumnDefault(v, pTab, iCol, regOut);
2447 }
2448}
2449
2450/*
drh945498f2007-02-24 11:52:52 +00002451** Generate code that will extract the iColumn-th column from
drhe55cbd72008-03-31 23:48:03 +00002452** table pTab and store the column value in a register. An effort
2453** is made to store the column value in register iReg, but this is
2454** not guaranteed. The location of the column value is returned.
2455**
2456** There must be an open cursor to pTab in iTable when this routine
2457** is called. If iColumn<0 then code is generated that extracts the rowid.
drh945498f2007-02-24 11:52:52 +00002458*/
drhe55cbd72008-03-31 23:48:03 +00002459int sqlite3ExprCodeGetColumn(
2460 Parse *pParse, /* Parsing and code generating context */
drh2133d822008-01-03 18:44:59 +00002461 Table *pTab, /* Description of the table we are reading from */
2462 int iColumn, /* Index of the table column */
2463 int iTable, /* The cursor pointing to the table */
drha748fdc2012-03-28 01:34:47 +00002464 int iReg, /* Store results here */
2465 u8 p5 /* P5 value for OP_Column */
drh2133d822008-01-03 18:44:59 +00002466){
drhe55cbd72008-03-31 23:48:03 +00002467 Vdbe *v = pParse->pVdbe;
2468 int i;
drhda250ea2008-04-01 05:07:14 +00002469 struct yColCache *p;
drhe55cbd72008-03-31 23:48:03 +00002470
drhceea3322009-04-23 13:22:42 +00002471 for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
drhb6da74e2009-12-24 16:00:28 +00002472 if( p->iReg>0 && p->iTable==iTable && p->iColumn==iColumn ){
drhceea3322009-04-23 13:22:42 +00002473 p->lru = pParse->iCacheCnt++;
drh5cd79232009-05-25 11:46:29 +00002474 sqlite3ExprCachePinRegister(pParse, p->iReg);
drhda250ea2008-04-01 05:07:14 +00002475 return p->iReg;
drhe55cbd72008-03-31 23:48:03 +00002476 }
2477 }
2478 assert( v!=0 );
drh5c092e82010-05-14 19:24:02 +00002479 sqlite3ExprCodeGetColumnOfTable(v, pTab, iTable, iColumn, iReg);
drha748fdc2012-03-28 01:34:47 +00002480 if( p5 ){
2481 sqlite3VdbeChangeP5(v, p5);
2482 }else{
2483 sqlite3ExprCacheStore(pParse, iTable, iColumn, iReg);
2484 }
drhe55cbd72008-03-31 23:48:03 +00002485 return iReg;
2486}
2487
2488/*
drhceea3322009-04-23 13:22:42 +00002489** Clear all column cache entries.
drhe55cbd72008-03-31 23:48:03 +00002490*/
drhceea3322009-04-23 13:22:42 +00002491void sqlite3ExprCacheClear(Parse *pParse){
2492 int i;
2493 struct yColCache *p;
2494
drh9ac79622013-12-18 15:11:47 +00002495#if SQLITE_DEBUG
2496 if( pParse->db->flags & SQLITE_VdbeAddopTrace ){
2497 printf("CLEAR\n");
2498 }
2499#endif
drhceea3322009-04-23 13:22:42 +00002500 for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
2501 if( p->iReg ){
2502 cacheEntryClear(pParse, p);
2503 p->iReg = 0;
drhe55cbd72008-03-31 23:48:03 +00002504 }
drhe55cbd72008-03-31 23:48:03 +00002505 }
2506}
2507
2508/*
drhda250ea2008-04-01 05:07:14 +00002509** Record the fact that an affinity change has occurred on iCount
2510** registers starting with iStart.
drhe55cbd72008-03-31 23:48:03 +00002511*/
drhda250ea2008-04-01 05:07:14 +00002512void sqlite3ExprCacheAffinityChange(Parse *pParse, int iStart, int iCount){
drhf49f3522009-12-30 14:12:38 +00002513 sqlite3ExprCacheRemove(pParse, iStart, iCount);
drhe55cbd72008-03-31 23:48:03 +00002514}
2515
2516/*
drhb21e7c72008-06-22 12:37:57 +00002517** Generate code to move content from registers iFrom...iFrom+nReg-1
2518** over to iTo..iTo+nReg-1. Keep the column cache up-to-date.
drhe55cbd72008-03-31 23:48:03 +00002519*/
drhb21e7c72008-06-22 12:37:57 +00002520void sqlite3ExprCodeMove(Parse *pParse, int iFrom, int iTo, int nReg){
drhe8e4af72012-09-21 00:04:28 +00002521 assert( iFrom>=iTo+nReg || iFrom+nReg<=iTo );
drh079a3072014-03-19 14:10:55 +00002522 sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move, iFrom, iTo, nReg);
drh236241a2014-09-12 17:41:30 +00002523 sqlite3ExprCacheRemove(pParse, iFrom, nReg);
drh945498f2007-02-24 11:52:52 +00002524}
2525
drhf49f3522009-12-30 14:12:38 +00002526#if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
drh92b01d52008-06-24 00:32:35 +00002527/*
drh652fbf52008-04-01 01:42:41 +00002528** Return true if any register in the range iFrom..iTo (inclusive)
2529** is used as part of the column cache.
drhf49f3522009-12-30 14:12:38 +00002530**
2531** This routine is used within assert() and testcase() macros only
2532** and does not appear in a normal build.
drh652fbf52008-04-01 01:42:41 +00002533*/
2534static int usedAsColumnCache(Parse *pParse, int iFrom, int iTo){
2535 int i;
drhceea3322009-04-23 13:22:42 +00002536 struct yColCache *p;
2537 for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
2538 int r = p->iReg;
drhf49f3522009-12-30 14:12:38 +00002539 if( r>=iFrom && r<=iTo ) return 1; /*NO_TEST*/
drh652fbf52008-04-01 01:42:41 +00002540 }
2541 return 0;
2542}
drhf49f3522009-12-30 14:12:38 +00002543#endif /* SQLITE_DEBUG || SQLITE_COVERAGE_TEST */
drh652fbf52008-04-01 01:42:41 +00002544
2545/*
drha4c3c872013-09-12 17:29:25 +00002546** Convert an expression node to a TK_REGISTER
2547*/
2548static void exprToRegister(Expr *p, int iReg){
2549 p->op2 = p->op;
2550 p->op = TK_REGISTER;
2551 p->iTable = iReg;
2552 ExprClearProperty(p, EP_Skip);
2553}
2554
2555/*
drhcce7d172000-05-31 15:34:51 +00002556** Generate code into the current Vdbe to evaluate the given
drh2dcef112008-01-12 19:03:48 +00002557** expression. Attempt to store the results in register "target".
2558** Return the register where results are stored.
drh389a1ad2008-01-03 23:44:53 +00002559**
drh8b213892008-08-29 02:14:02 +00002560** With this routine, there is no guarantee that results will
drh2dcef112008-01-12 19:03:48 +00002561** be stored in target. The result might be stored in some other
2562** register if it is convenient to do so. The calling function
2563** must check the return code and move the results to the desired
2564** register.
drhcce7d172000-05-31 15:34:51 +00002565*/
drh678ccce2008-03-31 18:19:54 +00002566int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){
drh2dcef112008-01-12 19:03:48 +00002567 Vdbe *v = pParse->pVdbe; /* The VM under construction */
2568 int op; /* The opcode being coded */
2569 int inReg = target; /* Results stored in register inReg */
2570 int regFree1 = 0; /* If non-zero free this temporary register */
2571 int regFree2 = 0; /* If non-zero free this temporary register */
drh678ccce2008-03-31 18:19:54 +00002572 int r1, r2, r3, r4; /* Various register numbers */
drh20411ea2009-05-29 19:00:12 +00002573 sqlite3 *db = pParse->db; /* The database connection */
drh10d1edf2013-11-15 15:52:39 +00002574 Expr tempX; /* Temporary expression node */
drhffe07b22005-11-03 00:41:17 +00002575
drh9cbf3422008-01-17 16:22:13 +00002576 assert( target>0 && target<=pParse->nMem );
drh20411ea2009-05-29 19:00:12 +00002577 if( v==0 ){
2578 assert( pParse->db->mallocFailed );
2579 return 0;
2580 }
drh389a1ad2008-01-03 23:44:53 +00002581
2582 if( pExpr==0 ){
2583 op = TK_NULL;
2584 }else{
2585 op = pExpr->op;
2586 }
drhf2bc0132004-10-04 13:19:23 +00002587 switch( op ){
drh13449892005-09-07 21:22:45 +00002588 case TK_AGG_COLUMN: {
2589 AggInfo *pAggInfo = pExpr->pAggInfo;
2590 struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg];
2591 if( !pAggInfo->directMode ){
drh9de221d2008-01-05 06:51:30 +00002592 assert( pCol->iMem>0 );
2593 inReg = pCol->iMem;
drh13449892005-09-07 21:22:45 +00002594 break;
2595 }else if( pAggInfo->useSortingIdx ){
dan5134d132011-09-02 10:31:11 +00002596 sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab,
drh389a1ad2008-01-03 23:44:53 +00002597 pCol->iSorterColumn, target);
drh13449892005-09-07 21:22:45 +00002598 break;
2599 }
2600 /* Otherwise, fall thru into the TK_COLUMN case */
2601 }
drh967e8b72000-06-21 13:59:10 +00002602 case TK_COLUMN: {
drhb2b9d3d2013-08-01 01:14:43 +00002603 int iTab = pExpr->iTable;
2604 if( iTab<0 ){
2605 if( pParse->ckBase>0 ){
2606 /* Generating CHECK constraints or inserting into partial index */
2607 inReg = pExpr->iColumn + pParse->ckBase;
2608 break;
2609 }else{
2610 /* Deleting from a partial index */
2611 iTab = pParse->iPartIdxTab;
2612 }
drh22827922000-06-06 17:27:05 +00002613 }
drhb2b9d3d2013-08-01 01:14:43 +00002614 inReg = sqlite3ExprCodeGetColumn(pParse, pExpr->pTab,
2615 pExpr->iColumn, iTab, target,
2616 pExpr->op2);
drhcce7d172000-05-31 15:34:51 +00002617 break;
2618 }
2619 case TK_INTEGER: {
drh13573c72010-01-12 17:04:07 +00002620 codeInteger(pParse, pExpr, 0, target);
drhfec19aa2004-05-19 20:41:03 +00002621 break;
2622 }
drh13573c72010-01-12 17:04:07 +00002623#ifndef SQLITE_OMIT_FLOATING_POINT
drh598f1342007-10-23 15:39:45 +00002624 case TK_FLOAT: {
drh33e619f2009-05-28 01:00:55 +00002625 assert( !ExprHasProperty(pExpr, EP_IntValue) );
2626 codeReal(v, pExpr->u.zToken, 0, target);
drh598f1342007-10-23 15:39:45 +00002627 break;
2628 }
drh13573c72010-01-12 17:04:07 +00002629#endif
drhfec19aa2004-05-19 20:41:03 +00002630 case TK_STRING: {
drh33e619f2009-05-28 01:00:55 +00002631 assert( !ExprHasProperty(pExpr, EP_IntValue) );
2632 sqlite3VdbeAddOp4(v, OP_String8, 0, target, 0, pExpr->u.zToken, 0);
drhcce7d172000-05-31 15:34:51 +00002633 break;
2634 }
drhf0863fe2005-06-12 21:35:51 +00002635 case TK_NULL: {
drh9de221d2008-01-05 06:51:30 +00002636 sqlite3VdbeAddOp2(v, OP_Null, 0, target);
drhf0863fe2005-06-12 21:35:51 +00002637 break;
2638 }
danielk19775338a5f2005-01-20 13:03:10 +00002639#ifndef SQLITE_OMIT_BLOB_LITERAL
danielk1977c572ef72004-05-27 09:28:41 +00002640 case TK_BLOB: {
drh6c8c6ce2005-08-23 11:17:58 +00002641 int n;
2642 const char *z;
drhca48c902008-01-18 14:08:24 +00002643 char *zBlob;
drh33e619f2009-05-28 01:00:55 +00002644 assert( !ExprHasProperty(pExpr, EP_IntValue) );
2645 assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
2646 assert( pExpr->u.zToken[1]=='\'' );
2647 z = &pExpr->u.zToken[2];
drhb7916a72009-05-27 10:31:29 +00002648 n = sqlite3Strlen30(z) - 1;
2649 assert( z[n]=='\'' );
drhca48c902008-01-18 14:08:24 +00002650 zBlob = sqlite3HexToBlob(sqlite3VdbeDb(v), z, n);
2651 sqlite3VdbeAddOp4(v, OP_Blob, n/2, target, 0, zBlob, P4_DYNAMIC);
danielk1977c572ef72004-05-27 09:28:41 +00002652 break;
2653 }
danielk19775338a5f2005-01-20 13:03:10 +00002654#endif
drh50457892003-09-06 01:10:47 +00002655 case TK_VARIABLE: {
drh33e619f2009-05-28 01:00:55 +00002656 assert( !ExprHasProperty(pExpr, EP_IntValue) );
2657 assert( pExpr->u.zToken!=0 );
2658 assert( pExpr->u.zToken[0]!=0 );
drheaf52d82010-05-12 13:50:23 +00002659 sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iColumn, target);
2660 if( pExpr->u.zToken[1]!=0 ){
drh04e9eea2011-06-01 19:16:06 +00002661 assert( pExpr->u.zToken[0]=='?'
2662 || strcmp(pExpr->u.zToken, pParse->azVar[pExpr->iColumn-1])==0 );
2663 sqlite3VdbeChangeP4(v, -1, pParse->azVar[pExpr->iColumn-1], P4_STATIC);
drh895d7472004-08-20 16:02:39 +00002664 }
drh50457892003-09-06 01:10:47 +00002665 break;
2666 }
drh4e0cff62004-11-05 05:10:28 +00002667 case TK_REGISTER: {
drh9de221d2008-01-05 06:51:30 +00002668 inReg = pExpr->iTable;
drh4e0cff62004-11-05 05:10:28 +00002669 break;
2670 }
drh8b213892008-08-29 02:14:02 +00002671 case TK_AS: {
drh7445ffe2010-09-27 18:14:12 +00002672 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
drh8b213892008-08-29 02:14:02 +00002673 break;
2674 }
drh487e2622005-06-25 18:42:14 +00002675#ifndef SQLITE_OMIT_CAST
2676 case TK_CAST: {
2677 /* Expressions of the form: CAST(pLeft AS token) */
drh2dcef112008-01-12 19:03:48 +00002678 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
drh1735fa82008-11-06 15:33:03 +00002679 if( inReg!=target ){
2680 sqlite3VdbeAddOp2(v, OP_SCopy, inReg, target);
2681 inReg = target;
2682 }
drh4169e432014-08-25 20:11:52 +00002683 sqlite3VdbeAddOp2(v, OP_Cast, target,
2684 sqlite3AffinityType(pExpr->u.zToken, 0));
drhc5499be2008-04-01 15:06:33 +00002685 testcase( usedAsColumnCache(pParse, inReg, inReg) );
drhb3843a82008-04-01 12:24:11 +00002686 sqlite3ExprCacheAffinityChange(pParse, inReg, 1);
drh487e2622005-06-25 18:42:14 +00002687 break;
2688 }
2689#endif /* SQLITE_OMIT_CAST */
drhc9b84a12002-06-20 11:36:48 +00002690 case TK_LT:
2691 case TK_LE:
2692 case TK_GT:
2693 case TK_GE:
2694 case TK_NE:
2695 case TK_EQ: {
drhb6da74e2009-12-24 16:00:28 +00002696 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
2697 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
drh35573352008-01-08 23:54:25 +00002698 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
drh7d176102014-02-18 03:07:12 +00002699 r1, r2, inReg, SQLITE_STOREP2);
2700 assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
2701 assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
2702 assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
2703 assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
2704 assert(TK_EQ==OP_Eq); testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq);
2705 assert(TK_NE==OP_Ne); testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne);
drhc5499be2008-04-01 15:06:33 +00002706 testcase( regFree1==0 );
2707 testcase( regFree2==0 );
danielk1977a37cdde2004-05-16 11:15:36 +00002708 break;
drhc9b84a12002-06-20 11:36:48 +00002709 }
drh6a2fe092009-09-23 02:29:36 +00002710 case TK_IS:
2711 case TK_ISNOT: {
2712 testcase( op==TK_IS );
2713 testcase( op==TK_ISNOT );
drhb6da74e2009-12-24 16:00:28 +00002714 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
2715 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
drh6a2fe092009-09-23 02:29:36 +00002716 op = (op==TK_IS) ? TK_EQ : TK_NE;
2717 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
2718 r1, r2, inReg, SQLITE_STOREP2 | SQLITE_NULLEQ);
drh7d176102014-02-18 03:07:12 +00002719 VdbeCoverageIf(v, op==TK_EQ);
2720 VdbeCoverageIf(v, op==TK_NE);
drh6a2fe092009-09-23 02:29:36 +00002721 testcase( regFree1==0 );
2722 testcase( regFree2==0 );
2723 break;
2724 }
drhcce7d172000-05-31 15:34:51 +00002725 case TK_AND:
2726 case TK_OR:
2727 case TK_PLUS:
2728 case TK_STAR:
2729 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00002730 case TK_REM:
2731 case TK_BITAND:
2732 case TK_BITOR:
drh17c40292004-07-21 02:53:29 +00002733 case TK_SLASH:
drhbf4133c2001-10-13 02:59:08 +00002734 case TK_LSHIFT:
drh855eb1c2004-08-31 13:45:11 +00002735 case TK_RSHIFT:
drh00400772000-06-16 20:51:26 +00002736 case TK_CONCAT: {
drh7d176102014-02-18 03:07:12 +00002737 assert( TK_AND==OP_And ); testcase( op==TK_AND );
2738 assert( TK_OR==OP_Or ); testcase( op==TK_OR );
2739 assert( TK_PLUS==OP_Add ); testcase( op==TK_PLUS );
2740 assert( TK_MINUS==OP_Subtract ); testcase( op==TK_MINUS );
2741 assert( TK_REM==OP_Remainder ); testcase( op==TK_REM );
2742 assert( TK_BITAND==OP_BitAnd ); testcase( op==TK_BITAND );
2743 assert( TK_BITOR==OP_BitOr ); testcase( op==TK_BITOR );
2744 assert( TK_SLASH==OP_Divide ); testcase( op==TK_SLASH );
2745 assert( TK_LSHIFT==OP_ShiftLeft ); testcase( op==TK_LSHIFT );
2746 assert( TK_RSHIFT==OP_ShiftRight ); testcase( op==TK_RSHIFT );
2747 assert( TK_CONCAT==OP_Concat ); testcase( op==TK_CONCAT );
drh2dcef112008-01-12 19:03:48 +00002748 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
2749 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
drh5b6afba2008-01-05 16:29:28 +00002750 sqlite3VdbeAddOp3(v, op, r2, r1, target);
drhc5499be2008-04-01 15:06:33 +00002751 testcase( regFree1==0 );
2752 testcase( regFree2==0 );
drh00400772000-06-16 20:51:26 +00002753 break;
2754 }
drhcce7d172000-05-31 15:34:51 +00002755 case TK_UMINUS: {
drhfec19aa2004-05-19 20:41:03 +00002756 Expr *pLeft = pExpr->pLeft;
2757 assert( pLeft );
drh13573c72010-01-12 17:04:07 +00002758 if( pLeft->op==TK_INTEGER ){
2759 codeInteger(pParse, pLeft, 1, target);
2760#ifndef SQLITE_OMIT_FLOATING_POINT
2761 }else if( pLeft->op==TK_FLOAT ){
drh33e619f2009-05-28 01:00:55 +00002762 assert( !ExprHasProperty(pExpr, EP_IntValue) );
2763 codeReal(v, pLeft->u.zToken, 1, target);
drh13573c72010-01-12 17:04:07 +00002764#endif
drh3c84ddf2008-01-09 02:15:38 +00002765 }else{
drh10d1edf2013-11-15 15:52:39 +00002766 tempX.op = TK_INTEGER;
2767 tempX.flags = EP_IntValue|EP_TokenOnly;
2768 tempX.u.iValue = 0;
2769 r1 = sqlite3ExprCodeTemp(pParse, &tempX, &regFree1);
drhe55cbd72008-03-31 23:48:03 +00002770 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree2);
drh2dcef112008-01-12 19:03:48 +00002771 sqlite3VdbeAddOp3(v, OP_Subtract, r2, r1, target);
drhc5499be2008-04-01 15:06:33 +00002772 testcase( regFree2==0 );
drh6e142f52000-06-08 13:36:40 +00002773 }
drh3c84ddf2008-01-09 02:15:38 +00002774 inReg = target;
2775 break;
drh6e142f52000-06-08 13:36:40 +00002776 }
drhbf4133c2001-10-13 02:59:08 +00002777 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00002778 case TK_NOT: {
drh7d176102014-02-18 03:07:12 +00002779 assert( TK_BITNOT==OP_BitNot ); testcase( op==TK_BITNOT );
2780 assert( TK_NOT==OP_Not ); testcase( op==TK_NOT );
drhe99fa2a2008-12-15 15:27:51 +00002781 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
2782 testcase( regFree1==0 );
2783 inReg = target;
2784 sqlite3VdbeAddOp2(v, op, r1, inReg);
drhcce7d172000-05-31 15:34:51 +00002785 break;
2786 }
2787 case TK_ISNULL:
2788 case TK_NOTNULL: {
drh6a288a32008-01-07 19:20:24 +00002789 int addr;
drh7d176102014-02-18 03:07:12 +00002790 assert( TK_ISNULL==OP_IsNull ); testcase( op==TK_ISNULL );
2791 assert( TK_NOTNULL==OP_NotNull ); testcase( op==TK_NOTNULL );
drh9de221d2008-01-05 06:51:30 +00002792 sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
drh2dcef112008-01-12 19:03:48 +00002793 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
drhc5499be2008-04-01 15:06:33 +00002794 testcase( regFree1==0 );
drh7d176102014-02-18 03:07:12 +00002795 addr = sqlite3VdbeAddOp1(v, op, r1);
2796 VdbeCoverageIf(v, op==TK_ISNULL);
2797 VdbeCoverageIf(v, op==TK_NOTNULL);
drha9769792014-08-04 16:39:39 +00002798 sqlite3VdbeAddOp2(v, OP_Integer, 0, target);
drh6a288a32008-01-07 19:20:24 +00002799 sqlite3VdbeJumpHere(v, addr);
drhf2bc0132004-10-04 13:19:23 +00002800 break;
drhcce7d172000-05-31 15:34:51 +00002801 }
drh22827922000-06-06 17:27:05 +00002802 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00002803 AggInfo *pInfo = pExpr->pAggInfo;
drh7e56e712005-11-16 12:53:15 +00002804 if( pInfo==0 ){
drh33e619f2009-05-28 01:00:55 +00002805 assert( !ExprHasProperty(pExpr, EP_IntValue) );
2806 sqlite3ErrorMsg(pParse, "misuse of aggregate: %s()", pExpr->u.zToken);
drh7e56e712005-11-16 12:53:15 +00002807 }else{
drh9de221d2008-01-05 06:51:30 +00002808 inReg = pInfo->aFunc[pExpr->iAgg].iMem;
drh7e56e712005-11-16 12:53:15 +00002809 }
drh22827922000-06-06 17:27:05 +00002810 break;
2811 }
drhcce7d172000-05-31 15:34:51 +00002812 case TK_FUNCTION: {
drh12ffee82009-04-08 13:51:51 +00002813 ExprList *pFarg; /* List of function arguments */
2814 int nFarg; /* Number of function arguments */
2815 FuncDef *pDef; /* The function definition object */
2816 int nId; /* Length of the function name in bytes */
2817 const char *zId; /* The function name */
drh693e6712014-01-24 22:58:00 +00002818 u32 constMask = 0; /* Mask of function arguments that are constant */
drh12ffee82009-04-08 13:51:51 +00002819 int i; /* Loop counter */
2820 u8 enc = ENC(db); /* The text encoding used by this database */
2821 CollSeq *pColl = 0; /* A collating sequence */
drh17435752007-08-16 04:30:38 +00002822
danielk19776ab3a2e2009-02-19 14:39:25 +00002823 assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
drhc5cd1242013-09-12 16:50:49 +00002824 if( ExprHasProperty(pExpr, EP_TokenOnly) ){
drh12ffee82009-04-08 13:51:51 +00002825 pFarg = 0;
2826 }else{
2827 pFarg = pExpr->x.pList;
2828 }
2829 nFarg = pFarg ? pFarg->nExpr : 0;
drh33e619f2009-05-28 01:00:55 +00002830 assert( !ExprHasProperty(pExpr, EP_IntValue) );
2831 zId = pExpr->u.zToken;
drhb7916a72009-05-27 10:31:29 +00002832 nId = sqlite3Strlen30(zId);
drh12ffee82009-04-08 13:51:51 +00002833 pDef = sqlite3FindFunction(db, zId, nId, nFarg, enc, 0);
drh0c4de2d2014-08-06 00:29:06 +00002834 if( pDef==0 || pDef->xFunc==0 ){
drhfeb306f2009-08-18 16:05:46 +00002835 sqlite3ErrorMsg(pParse, "unknown function: %.*s()", nId, zId);
2836 break;
2837 }
drhae6bb952009-11-11 00:24:31 +00002838
2839 /* Attempt a direct implementation of the built-in COALESCE() and
peter.d.reid60ec9142014-09-06 16:39:46 +00002840 ** IFNULL() functions. This avoids unnecessary evaluation of
drhae6bb952009-11-11 00:24:31 +00002841 ** arguments past the first non-NULL argument.
2842 */
drhd36e1042013-09-06 13:10:12 +00002843 if( pDef->funcFlags & SQLITE_FUNC_COALESCE ){
drhae6bb952009-11-11 00:24:31 +00002844 int endCoalesce = sqlite3VdbeMakeLabel(v);
2845 assert( nFarg>=2 );
2846 sqlite3ExprCode(pParse, pFarg->a[0].pExpr, target);
2847 for(i=1; i<nFarg; i++){
2848 sqlite3VdbeAddOp2(v, OP_NotNull, target, endCoalesce);
drh688852a2014-02-17 22:40:43 +00002849 VdbeCoverage(v);
drhf49f3522009-12-30 14:12:38 +00002850 sqlite3ExprCacheRemove(pParse, target, 1);
drhae6bb952009-11-11 00:24:31 +00002851 sqlite3ExprCachePush(pParse);
2852 sqlite3ExprCode(pParse, pFarg->a[i].pExpr, target);
drhd2490902014-04-13 19:28:15 +00002853 sqlite3ExprCachePop(pParse);
drhae6bb952009-11-11 00:24:31 +00002854 }
2855 sqlite3VdbeResolveLabel(v, endCoalesce);
2856 break;
2857 }
2858
drhcca9f3d2013-09-06 15:23:29 +00002859 /* The UNLIKELY() function is a no-op. The result is the value
2860 ** of the first argument.
2861 */
2862 if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){
2863 assert( nFarg>=1 );
2864 sqlite3ExprCode(pParse, pFarg->a[0].pExpr, target);
2865 break;
2866 }
drhae6bb952009-11-11 00:24:31 +00002867
drhd1a01ed2013-11-21 16:08:52 +00002868 for(i=0; i<nFarg; i++){
2869 if( i<32 && sqlite3ExprIsConstant(pFarg->a[i].pExpr) ){
drh693e6712014-01-24 22:58:00 +00002870 testcase( i==31 );
2871 constMask |= MASKBIT32(i);
drhd1a01ed2013-11-21 16:08:52 +00002872 }
2873 if( (pDef->funcFlags & SQLITE_FUNC_NEEDCOLL)!=0 && !pColl ){
2874 pColl = sqlite3ExprCollSeq(pParse, pFarg->a[i].pExpr);
2875 }
2876 }
drh12ffee82009-04-08 13:51:51 +00002877 if( pFarg ){
drhd1a01ed2013-11-21 16:08:52 +00002878 if( constMask ){
2879 r1 = pParse->nMem+1;
2880 pParse->nMem += nFarg;
2881 }else{
2882 r1 = sqlite3GetTempRange(pParse, nFarg);
2883 }
drha748fdc2012-03-28 01:34:47 +00002884
2885 /* For length() and typeof() functions with a column argument,
2886 ** set the P5 parameter to the OP_Column opcode to OPFLAG_LENGTHARG
2887 ** or OPFLAG_TYPEOFARG respectively, to avoid unnecessary data
2888 ** loading.
2889 */
drhd36e1042013-09-06 13:10:12 +00002890 if( (pDef->funcFlags & (SQLITE_FUNC_LENGTH|SQLITE_FUNC_TYPEOF))!=0 ){
drh4e245a42012-03-30 00:00:36 +00002891 u8 exprOp;
drha748fdc2012-03-28 01:34:47 +00002892 assert( nFarg==1 );
2893 assert( pFarg->a[0].pExpr!=0 );
drh4e245a42012-03-30 00:00:36 +00002894 exprOp = pFarg->a[0].pExpr->op;
2895 if( exprOp==TK_COLUMN || exprOp==TK_AGG_COLUMN ){
drha748fdc2012-03-28 01:34:47 +00002896 assert( SQLITE_FUNC_LENGTH==OPFLAG_LENGTHARG );
2897 assert( SQLITE_FUNC_TYPEOF==OPFLAG_TYPEOFARG );
drhb1fba282013-11-21 14:33:48 +00002898 testcase( pDef->funcFlags & OPFLAG_LENGTHARG );
2899 pFarg->a[0].pExpr->op2 =
2900 pDef->funcFlags & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG);
drha748fdc2012-03-28 01:34:47 +00002901 }
2902 }
2903
drhd7d385d2009-09-03 01:18:00 +00002904 sqlite3ExprCachePush(pParse); /* Ticket 2ea2425d34be */
drh079a3072014-03-19 14:10:55 +00002905 sqlite3ExprCodeExprList(pParse, pFarg, r1,
drhd1a01ed2013-11-21 16:08:52 +00002906 SQLITE_ECEL_DUP|SQLITE_ECEL_FACTOR);
drhd2490902014-04-13 19:28:15 +00002907 sqlite3ExprCachePop(pParse); /* Ticket 2ea2425d34be */
drh892d3172008-01-10 03:46:36 +00002908 }else{
drh12ffee82009-04-08 13:51:51 +00002909 r1 = 0;
drh892d3172008-01-10 03:46:36 +00002910 }
drhb7f6f682006-07-08 17:06:43 +00002911#ifndef SQLITE_OMIT_VIRTUALTABLE
drha43fa222006-07-08 18:41:37 +00002912 /* Possibly overload the function if the first argument is
2913 ** a virtual table column.
2914 **
2915 ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the
2916 ** second argument, not the first, as the argument to test to
2917 ** see if it is a column in a virtual table. This is done because
2918 ** the left operand of infix functions (the operand we want to
2919 ** control overloading) ends up as the second argument to the
2920 ** function. The expression "A glob B" is equivalent to
2921 ** "glob(B,A). We want to use the A in "A glob B" to test
2922 ** for function overloading. But we use the B term in "glob(B,A)".
2923 */
drh12ffee82009-04-08 13:51:51 +00002924 if( nFarg>=2 && (pExpr->flags & EP_InfixFunc) ){
2925 pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[1].pExpr);
2926 }else if( nFarg>0 ){
2927 pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[0].pExpr);
drhb7f6f682006-07-08 17:06:43 +00002928 }
2929#endif
drhd36e1042013-09-06 13:10:12 +00002930 if( pDef->funcFlags & SQLITE_FUNC_NEEDCOLL ){
drh8b213892008-08-29 02:14:02 +00002931 if( !pColl ) pColl = db->pDfltColl;
drh66a51672008-01-03 00:01:23 +00002932 sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ);
danielk1977682f68b2004-06-05 10:22:17 +00002933 }
drh2dcef112008-01-12 19:03:48 +00002934 sqlite3VdbeAddOp4(v, OP_Function, constMask, r1, target,
drh66a51672008-01-03 00:01:23 +00002935 (char*)pDef, P4_FUNCDEF);
drh12ffee82009-04-08 13:51:51 +00002936 sqlite3VdbeChangeP5(v, (u8)nFarg);
drhd1a01ed2013-11-21 16:08:52 +00002937 if( nFarg && constMask==0 ){
drh12ffee82009-04-08 13:51:51 +00002938 sqlite3ReleaseTempRange(pParse, r1, nFarg);
drh2dcef112008-01-12 19:03:48 +00002939 }
drhcce7d172000-05-31 15:34:51 +00002940 break;
2941 }
drhfe2093d2005-01-20 22:48:47 +00002942#ifndef SQLITE_OMIT_SUBQUERY
2943 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00002944 case TK_SELECT: {
drhc5499be2008-04-01 15:06:33 +00002945 testcase( op==TK_EXISTS );
2946 testcase( op==TK_SELECT );
drh1450bc62009-10-30 13:25:56 +00002947 inReg = sqlite3CodeSubselect(pParse, pExpr, 0, 0);
drh19a775c2000-06-05 18:54:46 +00002948 break;
2949 }
drhfef52082000-06-06 01:50:43 +00002950 case TK_IN: {
drhe3365e62009-11-12 17:52:24 +00002951 int destIfFalse = sqlite3VdbeMakeLabel(v);
2952 int destIfNull = sqlite3VdbeMakeLabel(v);
2953 sqlite3VdbeAddOp2(v, OP_Null, 0, target);
2954 sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull);
2955 sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
2956 sqlite3VdbeResolveLabel(v, destIfFalse);
2957 sqlite3VdbeAddOp2(v, OP_AddImm, target, 0);
2958 sqlite3VdbeResolveLabel(v, destIfNull);
drhfef52082000-06-06 01:50:43 +00002959 break;
2960 }
drhe3365e62009-11-12 17:52:24 +00002961#endif /* SQLITE_OMIT_SUBQUERY */
2962
2963
drh2dcef112008-01-12 19:03:48 +00002964 /*
2965 ** x BETWEEN y AND z
2966 **
2967 ** This is equivalent to
2968 **
2969 ** x>=y AND x<=z
2970 **
2971 ** X is stored in pExpr->pLeft.
2972 ** Y is stored in pExpr->pList->a[0].pExpr.
2973 ** Z is stored in pExpr->pList->a[1].pExpr.
2974 */
drhfef52082000-06-06 01:50:43 +00002975 case TK_BETWEEN: {
drhbe5c89a2004-07-26 00:31:09 +00002976 Expr *pLeft = pExpr->pLeft;
danielk19776ab3a2e2009-02-19 14:39:25 +00002977 struct ExprList_item *pLItem = pExpr->x.pList->a;
drhbe5c89a2004-07-26 00:31:09 +00002978 Expr *pRight = pLItem->pExpr;
drh35573352008-01-08 23:54:25 +00002979
drhb6da74e2009-12-24 16:00:28 +00002980 r1 = sqlite3ExprCodeTemp(pParse, pLeft, &regFree1);
2981 r2 = sqlite3ExprCodeTemp(pParse, pRight, &regFree2);
drhc5499be2008-04-01 15:06:33 +00002982 testcase( regFree1==0 );
2983 testcase( regFree2==0 );
drh2dcef112008-01-12 19:03:48 +00002984 r3 = sqlite3GetTempReg(pParse);
drh678ccce2008-03-31 18:19:54 +00002985 r4 = sqlite3GetTempReg(pParse);
drh35573352008-01-08 23:54:25 +00002986 codeCompare(pParse, pLeft, pRight, OP_Ge,
drh7d176102014-02-18 03:07:12 +00002987 r1, r2, r3, SQLITE_STOREP2); VdbeCoverage(v);
drhbe5c89a2004-07-26 00:31:09 +00002988 pLItem++;
2989 pRight = pLItem->pExpr;
drh2dcef112008-01-12 19:03:48 +00002990 sqlite3ReleaseTempReg(pParse, regFree2);
2991 r2 = sqlite3ExprCodeTemp(pParse, pRight, &regFree2);
drhc5499be2008-04-01 15:06:33 +00002992 testcase( regFree2==0 );
drh678ccce2008-03-31 18:19:54 +00002993 codeCompare(pParse, pLeft, pRight, OP_Le, r1, r2, r4, SQLITE_STOREP2);
drh688852a2014-02-17 22:40:43 +00002994 VdbeCoverage(v);
drh678ccce2008-03-31 18:19:54 +00002995 sqlite3VdbeAddOp3(v, OP_And, r3, r4, target);
drh2dcef112008-01-12 19:03:48 +00002996 sqlite3ReleaseTempReg(pParse, r3);
drh678ccce2008-03-31 18:19:54 +00002997 sqlite3ReleaseTempReg(pParse, r4);
drhfef52082000-06-06 01:50:43 +00002998 break;
2999 }
drhae80dde2012-12-06 21:16:43 +00003000 case TK_COLLATE:
drh4f07e5f2007-05-14 11:34:46 +00003001 case TK_UPLUS: {
drh2dcef112008-01-12 19:03:48 +00003002 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
drha2e00042002-01-22 03:13:42 +00003003 break;
3004 }
drh2dcef112008-01-12 19:03:48 +00003005
dan165921a2009-08-28 18:53:45 +00003006 case TK_TRIGGER: {
dan65a7cd12009-09-01 12:16:01 +00003007 /* If the opcode is TK_TRIGGER, then the expression is a reference
3008 ** to a column in the new.* or old.* pseudo-tables available to
3009 ** trigger programs. In this case Expr.iTable is set to 1 for the
3010 ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
3011 ** is set to the column of the pseudo-table to read, or to -1 to
3012 ** read the rowid field.
3013 **
3014 ** The expression is implemented using an OP_Param opcode. The p1
3015 ** parameter is set to 0 for an old.rowid reference, or to (i+1)
3016 ** to reference another column of the old.* pseudo-table, where
3017 ** i is the index of the column. For a new.rowid reference, p1 is
3018 ** set to (n+1), where n is the number of columns in each pseudo-table.
3019 ** For a reference to any other column in the new.* pseudo-table, p1
3020 ** is set to (n+2+i), where n and i are as defined previously. For
3021 ** example, if the table on which triggers are being fired is
3022 ** declared as:
3023 **
3024 ** CREATE TABLE t1(a, b);
3025 **
3026 ** Then p1 is interpreted as follows:
3027 **
3028 ** p1==0 -> old.rowid p1==3 -> new.rowid
3029 ** p1==1 -> old.a p1==4 -> new.a
3030 ** p1==2 -> old.b p1==5 -> new.b
3031 */
dan2832ad42009-08-31 15:27:27 +00003032 Table *pTab = pExpr->pTab;
dan65a7cd12009-09-01 12:16:01 +00003033 int p1 = pExpr->iTable * (pTab->nCol+1) + 1 + pExpr->iColumn;
3034
3035 assert( pExpr->iTable==0 || pExpr->iTable==1 );
3036 assert( pExpr->iColumn>=-1 && pExpr->iColumn<pTab->nCol );
3037 assert( pTab->iPKey<0 || pExpr->iColumn!=pTab->iPKey );
3038 assert( p1>=0 && p1<(pTab->nCol*2+2) );
3039
3040 sqlite3VdbeAddOp2(v, OP_Param, p1, target);
dan2bd93512009-08-31 08:22:46 +00003041 VdbeComment((v, "%s.%s -> $%d",
3042 (pExpr->iTable ? "new" : "old"),
dan76d462e2009-08-30 11:42:51 +00003043 (pExpr->iColumn<0 ? "rowid" : pExpr->pTab->aCol[pExpr->iColumn].zName),
dan2bd93512009-08-31 08:22:46 +00003044 target
dan165921a2009-08-28 18:53:45 +00003045 ));
dan65a7cd12009-09-01 12:16:01 +00003046
drh44dbca82010-01-13 04:22:20 +00003047#ifndef SQLITE_OMIT_FLOATING_POINT
dan65a7cd12009-09-01 12:16:01 +00003048 /* If the column has REAL affinity, it may currently be stored as an
drh113762a2014-11-19 16:36:25 +00003049 ** integer. Use OP_RealAffinity to make sure it is really real.
3050 **
3051 ** EVIDENCE-OF: R-60985-57662 SQLite will convert the value back to
3052 ** floating point when extracting it from the record. */
dan2832ad42009-08-31 15:27:27 +00003053 if( pExpr->iColumn>=0
3054 && pTab->aCol[pExpr->iColumn].affinity==SQLITE_AFF_REAL
3055 ){
3056 sqlite3VdbeAddOp1(v, OP_RealAffinity, target);
3057 }
drh44dbca82010-01-13 04:22:20 +00003058#endif
dan165921a2009-08-28 18:53:45 +00003059 break;
3060 }
3061
3062
drh2dcef112008-01-12 19:03:48 +00003063 /*
3064 ** Form A:
3065 ** CASE x WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
3066 **
3067 ** Form B:
3068 ** CASE WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
3069 **
3070 ** Form A is can be transformed into the equivalent form B as follows:
3071 ** CASE WHEN x=e1 THEN r1 WHEN x=e2 THEN r2 ...
3072 ** WHEN x=eN THEN rN ELSE y END
3073 **
3074 ** X (if it exists) is in pExpr->pLeft.
drhc5cd1242013-09-12 16:50:49 +00003075 ** Y is in the last element of pExpr->x.pList if pExpr->x.pList->nExpr is
3076 ** odd. The Y is also optional. If the number of elements in x.pList
3077 ** is even, then Y is omitted and the "otherwise" result is NULL.
drh2dcef112008-01-12 19:03:48 +00003078 ** Ei is in pExpr->pList->a[i*2] and Ri is pExpr->pList->a[i*2+1].
3079 **
3080 ** The result of the expression is the Ri for the first matching Ei,
3081 ** or if there is no matching Ei, the ELSE term Y, or if there is
3082 ** no ELSE term, NULL.
3083 */
drh33cd4902009-05-30 20:49:20 +00003084 default: assert( op==TK_CASE ); {
drh2dcef112008-01-12 19:03:48 +00003085 int endLabel; /* GOTO label for end of CASE stmt */
3086 int nextCase; /* GOTO label for next WHEN clause */
3087 int nExpr; /* 2x number of WHEN terms */
3088 int i; /* Loop counter */
3089 ExprList *pEList; /* List of WHEN terms */
3090 struct ExprList_item *aListelem; /* Array of WHEN terms */
3091 Expr opCompare; /* The X==Ei expression */
drh2dcef112008-01-12 19:03:48 +00003092 Expr *pX; /* The X expression */
drh1bd10f82008-12-10 21:19:56 +00003093 Expr *pTest = 0; /* X==Ei (form A) or just Ei (form B) */
drhceea3322009-04-23 13:22:42 +00003094 VVA_ONLY( int iCacheLevel = pParse->iCacheLevel; )
drh17a7f8d2002-03-24 13:13:27 +00003095
danielk19776ab3a2e2009-02-19 14:39:25 +00003096 assert( !ExprHasProperty(pExpr, EP_xIsSelect) && pExpr->x.pList );
danielk19776ab3a2e2009-02-19 14:39:25 +00003097 assert(pExpr->x.pList->nExpr > 0);
3098 pEList = pExpr->x.pList;
drhbe5c89a2004-07-26 00:31:09 +00003099 aListelem = pEList->a;
3100 nExpr = pEList->nExpr;
drh2dcef112008-01-12 19:03:48 +00003101 endLabel = sqlite3VdbeMakeLabel(v);
3102 if( (pX = pExpr->pLeft)!=0 ){
drh10d1edf2013-11-15 15:52:39 +00003103 tempX = *pX;
drh33cd4902009-05-30 20:49:20 +00003104 testcase( pX->op==TK_COLUMN );
drh10d1edf2013-11-15 15:52:39 +00003105 exprToRegister(&tempX, sqlite3ExprCodeTemp(pParse, pX, &regFree1));
drhc5499be2008-04-01 15:06:33 +00003106 testcase( regFree1==0 );
drh2dcef112008-01-12 19:03:48 +00003107 opCompare.op = TK_EQ;
drh10d1edf2013-11-15 15:52:39 +00003108 opCompare.pLeft = &tempX;
drh2dcef112008-01-12 19:03:48 +00003109 pTest = &opCompare;
drh8b1db072010-09-28 04:14:03 +00003110 /* Ticket b351d95f9cd5ef17e9d9dbae18f5ca8611190001:
3111 ** The value in regFree1 might get SCopy-ed into the file result.
3112 ** So make sure that the regFree1 register is not reused for other
3113 ** purposes and possibly overwritten. */
3114 regFree1 = 0;
drh17a7f8d2002-03-24 13:13:27 +00003115 }
drhc5cd1242013-09-12 16:50:49 +00003116 for(i=0; i<nExpr-1; i=i+2){
drhceea3322009-04-23 13:22:42 +00003117 sqlite3ExprCachePush(pParse);
drh2dcef112008-01-12 19:03:48 +00003118 if( pX ){
drh1bd10f82008-12-10 21:19:56 +00003119 assert( pTest!=0 );
drh2dcef112008-01-12 19:03:48 +00003120 opCompare.pRight = aListelem[i].pExpr;
drh17a7f8d2002-03-24 13:13:27 +00003121 }else{
drh2dcef112008-01-12 19:03:48 +00003122 pTest = aListelem[i].pExpr;
drh17a7f8d2002-03-24 13:13:27 +00003123 }
drh2dcef112008-01-12 19:03:48 +00003124 nextCase = sqlite3VdbeMakeLabel(v);
drh33cd4902009-05-30 20:49:20 +00003125 testcase( pTest->op==TK_COLUMN );
drh2dcef112008-01-12 19:03:48 +00003126 sqlite3ExprIfFalse(pParse, pTest, nextCase, SQLITE_JUMPIFNULL);
drhc5499be2008-04-01 15:06:33 +00003127 testcase( aListelem[i+1].pExpr->op==TK_COLUMN );
drh9de221d2008-01-05 06:51:30 +00003128 sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target);
drh2dcef112008-01-12 19:03:48 +00003129 sqlite3VdbeAddOp2(v, OP_Goto, 0, endLabel);
drhd2490902014-04-13 19:28:15 +00003130 sqlite3ExprCachePop(pParse);
drh2dcef112008-01-12 19:03:48 +00003131 sqlite3VdbeResolveLabel(v, nextCase);
drhf570f012002-05-31 15:51:25 +00003132 }
drhc5cd1242013-09-12 16:50:49 +00003133 if( (nExpr&1)!=0 ){
drhceea3322009-04-23 13:22:42 +00003134 sqlite3ExprCachePush(pParse);
drhc5cd1242013-09-12 16:50:49 +00003135 sqlite3ExprCode(pParse, pEList->a[nExpr-1].pExpr, target);
drhd2490902014-04-13 19:28:15 +00003136 sqlite3ExprCachePop(pParse);
drh17a7f8d2002-03-24 13:13:27 +00003137 }else{
drh9de221d2008-01-05 06:51:30 +00003138 sqlite3VdbeAddOp2(v, OP_Null, 0, target);
drh17a7f8d2002-03-24 13:13:27 +00003139 }
danielk1977c1f4a192009-04-28 12:08:15 +00003140 assert( db->mallocFailed || pParse->nErr>0
3141 || pParse->iCacheLevel==iCacheLevel );
drh2dcef112008-01-12 19:03:48 +00003142 sqlite3VdbeResolveLabel(v, endLabel);
danielk19776f349032002-06-11 02:25:40 +00003143 break;
3144 }
danielk19775338a5f2005-01-20 13:03:10 +00003145#ifndef SQLITE_OMIT_TRIGGER
danielk19776f349032002-06-11 02:25:40 +00003146 case TK_RAISE: {
dan165921a2009-08-28 18:53:45 +00003147 assert( pExpr->affinity==OE_Rollback
3148 || pExpr->affinity==OE_Abort
3149 || pExpr->affinity==OE_Fail
3150 || pExpr->affinity==OE_Ignore
3151 );
dane0af83a2009-09-08 19:15:01 +00003152 if( !pParse->pTriggerTab ){
3153 sqlite3ErrorMsg(pParse,
3154 "RAISE() may only be used within a trigger-program");
3155 return 0;
3156 }
3157 if( pExpr->affinity==OE_Abort ){
3158 sqlite3MayAbort(pParse);
3159 }
dan165921a2009-08-28 18:53:45 +00003160 assert( !ExprHasProperty(pExpr, EP_IntValue) );
dane0af83a2009-09-08 19:15:01 +00003161 if( pExpr->affinity==OE_Ignore ){
3162 sqlite3VdbeAddOp4(
3163 v, OP_Halt, SQLITE_OK, OE_Ignore, 0, pExpr->u.zToken,0);
drh688852a2014-02-17 22:40:43 +00003164 VdbeCoverage(v);
dane0af83a2009-09-08 19:15:01 +00003165 }else{
drh433dccf2013-02-09 15:37:11 +00003166 sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_TRIGGER,
drhf9c8ce32013-11-05 13:33:55 +00003167 pExpr->affinity, pExpr->u.zToken, 0, 0);
dane0af83a2009-09-08 19:15:01 +00003168 }
3169
drhffe07b22005-11-03 00:41:17 +00003170 break;
drh17a7f8d2002-03-24 13:13:27 +00003171 }
danielk19775338a5f2005-01-20 13:03:10 +00003172#endif
drhffe07b22005-11-03 00:41:17 +00003173 }
drh2dcef112008-01-12 19:03:48 +00003174 sqlite3ReleaseTempReg(pParse, regFree1);
3175 sqlite3ReleaseTempReg(pParse, regFree2);
3176 return inReg;
3177}
3178
3179/*
drhd1a01ed2013-11-21 16:08:52 +00003180** Factor out the code of the given expression to initialization time.
3181*/
drhd673cdd2013-11-21 21:23:31 +00003182void sqlite3ExprCodeAtInit(
3183 Parse *pParse, /* Parsing context */
3184 Expr *pExpr, /* The expression to code when the VDBE initializes */
3185 int regDest, /* Store the value in this register */
3186 u8 reusable /* True if this expression is reusable */
3187){
drhd1a01ed2013-11-21 16:08:52 +00003188 ExprList *p;
drhd9f158e2013-11-21 20:48:42 +00003189 assert( ConstFactorOk(pParse) );
drhd1a01ed2013-11-21 16:08:52 +00003190 p = pParse->pConstExpr;
3191 pExpr = sqlite3ExprDup(pParse->db, pExpr, 0);
3192 p = sqlite3ExprListAppend(pParse, p, pExpr);
drhd673cdd2013-11-21 21:23:31 +00003193 if( p ){
3194 struct ExprList_item *pItem = &p->a[p->nExpr-1];
3195 pItem->u.iConstExprReg = regDest;
3196 pItem->reusable = reusable;
3197 }
drhd1a01ed2013-11-21 16:08:52 +00003198 pParse->pConstExpr = p;
3199}
3200
3201/*
drh2dcef112008-01-12 19:03:48 +00003202** Generate code to evaluate an expression and store the results
3203** into a register. Return the register number where the results
3204** are stored.
3205**
3206** If the register is a temporary register that can be deallocated,
drh678ccce2008-03-31 18:19:54 +00003207** then write its number into *pReg. If the result register is not
drh2dcef112008-01-12 19:03:48 +00003208** a temporary, then set *pReg to zero.
drhf30a9692013-11-15 01:10:18 +00003209**
3210** If pExpr is a constant, then this routine might generate this
3211** code to fill the register in the initialization section of the
3212** VDBE program, in order to factor it out of the evaluation loop.
drh2dcef112008-01-12 19:03:48 +00003213*/
3214int sqlite3ExprCodeTemp(Parse *pParse, Expr *pExpr, int *pReg){
drhf30a9692013-11-15 01:10:18 +00003215 int r2;
3216 pExpr = sqlite3ExprSkipCollate(pExpr);
drhd9f158e2013-11-21 20:48:42 +00003217 if( ConstFactorOk(pParse)
drhf30a9692013-11-15 01:10:18 +00003218 && pExpr->op!=TK_REGISTER
3219 && sqlite3ExprIsConstantNotJoin(pExpr)
3220 ){
3221 ExprList *p = pParse->pConstExpr;
3222 int i;
3223 *pReg = 0;
3224 if( p ){
drhd673cdd2013-11-21 21:23:31 +00003225 struct ExprList_item *pItem;
3226 for(pItem=p->a, i=p->nExpr; i>0; pItem++, i--){
3227 if( pItem->reusable && sqlite3ExprCompare(pItem->pExpr,pExpr,-1)==0 ){
3228 return pItem->u.iConstExprReg;
drhf30a9692013-11-15 01:10:18 +00003229 }
3230 }
3231 }
drhf30a9692013-11-15 01:10:18 +00003232 r2 = ++pParse->nMem;
drhd673cdd2013-11-21 21:23:31 +00003233 sqlite3ExprCodeAtInit(pParse, pExpr, r2, 1);
drh2dcef112008-01-12 19:03:48 +00003234 }else{
drhf30a9692013-11-15 01:10:18 +00003235 int r1 = sqlite3GetTempReg(pParse);
3236 r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
3237 if( r2==r1 ){
3238 *pReg = r1;
3239 }else{
3240 sqlite3ReleaseTempReg(pParse, r1);
3241 *pReg = 0;
3242 }
drh2dcef112008-01-12 19:03:48 +00003243 }
3244 return r2;
3245}
3246
3247/*
3248** Generate code that will evaluate expression pExpr and store the
3249** results in register target. The results are guaranteed to appear
3250** in register target.
3251*/
drh05a86c52014-02-16 01:55:49 +00003252void sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){
drh9cbf3422008-01-17 16:22:13 +00003253 int inReg;
3254
3255 assert( target>0 && target<=pParse->nMem );
drhebc16712010-09-28 00:25:58 +00003256 if( pExpr && pExpr->op==TK_REGISTER ){
3257 sqlite3VdbeAddOp2(pParse->pVdbe, OP_Copy, pExpr->iTable, target);
3258 }else{
3259 inReg = sqlite3ExprCodeTarget(pParse, pExpr, target);
3260 assert( pParse->pVdbe || pParse->db->mallocFailed );
3261 if( inReg!=target && pParse->pVdbe ){
3262 sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, inReg, target);
3263 }
drhcce7d172000-05-31 15:34:51 +00003264 }
drh05a86c52014-02-16 01:55:49 +00003265}
3266
3267/*
3268** Generate code that will evaluate expression pExpr and store the
3269** results in register target. The results are guaranteed to appear
3270** in register target. If the expression is constant, then this routine
3271** might choose to code the expression at initialization time.
3272*/
3273void sqlite3ExprCodeFactorable(Parse *pParse, Expr *pExpr, int target){
3274 if( pParse->okConstFactor && sqlite3ExprIsConstant(pExpr) ){
3275 sqlite3ExprCodeAtInit(pParse, pExpr, target, 0);
3276 }else{
3277 sqlite3ExprCode(pParse, pExpr, target);
3278 }
drhcce7d172000-05-31 15:34:51 +00003279}
3280
3281/*
peter.d.reid60ec9142014-09-06 16:39:46 +00003282** Generate code that evaluates the given expression and puts the result
drhde4fcfd2008-01-19 23:50:26 +00003283** in register target.
drh25303782004-12-07 15:41:48 +00003284**
drh2dcef112008-01-12 19:03:48 +00003285** Also make a copy of the expression results into another "cache" register
3286** and modify the expression so that the next time it is evaluated,
3287** the result is a copy of the cache register.
3288**
3289** This routine is used for expressions that are used multiple
3290** times. They are evaluated once and the results of the expression
3291** are reused.
drh25303782004-12-07 15:41:48 +00003292*/
drh05a86c52014-02-16 01:55:49 +00003293void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr, int target){
drh25303782004-12-07 15:41:48 +00003294 Vdbe *v = pParse->pVdbe;
drh05a86c52014-02-16 01:55:49 +00003295 int iMem;
3296
drhde4fcfd2008-01-19 23:50:26 +00003297 assert( target>0 );
drh05a86c52014-02-16 01:55:49 +00003298 assert( pExpr->op!=TK_REGISTER );
3299 sqlite3ExprCode(pParse, pExpr, target);
3300 iMem = ++pParse->nMem;
3301 sqlite3VdbeAddOp2(v, OP_Copy, target, iMem);
3302 exprToRegister(pExpr, iMem);
drh25303782004-12-07 15:41:48 +00003303}
drh2dcef112008-01-12 19:03:48 +00003304
drh4fa4a542014-09-30 12:33:33 +00003305#ifdef SQLITE_DEBUG
drh7e02e5e2011-12-06 19:44:51 +00003306/*
3307** Generate a human-readable explanation of an expression tree.
3308*/
drh4fa4a542014-09-30 12:33:33 +00003309void sqlite3TreeViewExpr(TreeView *pView, const Expr *pExpr, u8 moreToFollow){
drha84203a2011-12-07 01:23:51 +00003310 const char *zBinOp = 0; /* Binary operator */
3311 const char *zUniOp = 0; /* Unary operator */
drh4fa4a542014-09-30 12:33:33 +00003312 pView = sqlite3TreeViewPush(pView, moreToFollow);
drha84203a2011-12-07 01:23:51 +00003313 if( pExpr==0 ){
drh4fa4a542014-09-30 12:33:33 +00003314 sqlite3TreeViewLine(pView, "nil");
3315 sqlite3TreeViewPop(pView);
3316 return;
drh7e02e5e2011-12-06 19:44:51 +00003317 }
drh4fa4a542014-09-30 12:33:33 +00003318 switch( pExpr->op ){
drha84203a2011-12-07 01:23:51 +00003319 case TK_AGG_COLUMN: {
drh4fa4a542014-09-30 12:33:33 +00003320 sqlite3TreeViewLine(pView, "AGG{%d:%d}",
drh04b83422011-12-07 15:33:14 +00003321 pExpr->iTable, pExpr->iColumn);
drha84203a2011-12-07 01:23:51 +00003322 break;
3323 }
3324 case TK_COLUMN: {
3325 if( pExpr->iTable<0 ){
3326 /* This only happens when coding check constraints */
drh4fa4a542014-09-30 12:33:33 +00003327 sqlite3TreeViewLine(pView, "COLUMN(%d)", pExpr->iColumn);
drha84203a2011-12-07 01:23:51 +00003328 }else{
drh4fa4a542014-09-30 12:33:33 +00003329 sqlite3TreeViewLine(pView, "{%d:%d}",
drh04b83422011-12-07 15:33:14 +00003330 pExpr->iTable, pExpr->iColumn);
drha84203a2011-12-07 01:23:51 +00003331 }
3332 break;
3333 }
3334 case TK_INTEGER: {
3335 if( pExpr->flags & EP_IntValue ){
drh4fa4a542014-09-30 12:33:33 +00003336 sqlite3TreeViewLine(pView, "%d", pExpr->u.iValue);
drha84203a2011-12-07 01:23:51 +00003337 }else{
drh4fa4a542014-09-30 12:33:33 +00003338 sqlite3TreeViewLine(pView, "%s", pExpr->u.zToken);
drha84203a2011-12-07 01:23:51 +00003339 }
3340 break;
3341 }
3342#ifndef SQLITE_OMIT_FLOATING_POINT
3343 case TK_FLOAT: {
drh4fa4a542014-09-30 12:33:33 +00003344 sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
drha84203a2011-12-07 01:23:51 +00003345 break;
3346 }
3347#endif
3348 case TK_STRING: {
drh4fa4a542014-09-30 12:33:33 +00003349 sqlite3TreeViewLine(pView,"%Q", pExpr->u.zToken);
drha84203a2011-12-07 01:23:51 +00003350 break;
3351 }
3352 case TK_NULL: {
drh4fa4a542014-09-30 12:33:33 +00003353 sqlite3TreeViewLine(pView,"NULL");
drha84203a2011-12-07 01:23:51 +00003354 break;
3355 }
3356#ifndef SQLITE_OMIT_BLOB_LITERAL
3357 case TK_BLOB: {
drh4fa4a542014-09-30 12:33:33 +00003358 sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
drha84203a2011-12-07 01:23:51 +00003359 break;
3360 }
3361#endif
3362 case TK_VARIABLE: {
drh4fa4a542014-09-30 12:33:33 +00003363 sqlite3TreeViewLine(pView,"VARIABLE(%s,%d)",
3364 pExpr->u.zToken, pExpr->iColumn);
drha84203a2011-12-07 01:23:51 +00003365 break;
3366 }
3367 case TK_REGISTER: {
drh4fa4a542014-09-30 12:33:33 +00003368 sqlite3TreeViewLine(pView,"REGISTER(%d)", pExpr->iTable);
drha84203a2011-12-07 01:23:51 +00003369 break;
3370 }
3371 case TK_AS: {
drh4fa4a542014-09-30 12:33:33 +00003372 sqlite3TreeViewLine(pView,"AS %Q", pExpr->u.zToken);
3373 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
3374 break;
3375 }
3376 case TK_ID: {
3377 sqlite3TreeViewLine(pView,"ID %Q", pExpr->u.zToken);
drha84203a2011-12-07 01:23:51 +00003378 break;
3379 }
3380#ifndef SQLITE_OMIT_CAST
3381 case TK_CAST: {
3382 /* Expressions of the form: CAST(pLeft AS token) */
drh4fa4a542014-09-30 12:33:33 +00003383 sqlite3TreeViewLine(pView,"CAST %Q", pExpr->u.zToken);
3384 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
drha84203a2011-12-07 01:23:51 +00003385 break;
3386 }
3387#endif /* SQLITE_OMIT_CAST */
3388 case TK_LT: zBinOp = "LT"; break;
3389 case TK_LE: zBinOp = "LE"; break;
3390 case TK_GT: zBinOp = "GT"; break;
3391 case TK_GE: zBinOp = "GE"; break;
3392 case TK_NE: zBinOp = "NE"; break;
3393 case TK_EQ: zBinOp = "EQ"; break;
3394 case TK_IS: zBinOp = "IS"; break;
3395 case TK_ISNOT: zBinOp = "ISNOT"; break;
3396 case TK_AND: zBinOp = "AND"; break;
3397 case TK_OR: zBinOp = "OR"; break;
3398 case TK_PLUS: zBinOp = "ADD"; break;
3399 case TK_STAR: zBinOp = "MUL"; break;
3400 case TK_MINUS: zBinOp = "SUB"; break;
3401 case TK_REM: zBinOp = "REM"; break;
3402 case TK_BITAND: zBinOp = "BITAND"; break;
3403 case TK_BITOR: zBinOp = "BITOR"; break;
3404 case TK_SLASH: zBinOp = "DIV"; break;
3405 case TK_LSHIFT: zBinOp = "LSHIFT"; break;
3406 case TK_RSHIFT: zBinOp = "RSHIFT"; break;
3407 case TK_CONCAT: zBinOp = "CONCAT"; break;
drhccaba812014-10-01 13:17:34 +00003408 case TK_DOT: zBinOp = "DOT"; break;
drha84203a2011-12-07 01:23:51 +00003409
3410 case TK_UMINUS: zUniOp = "UMINUS"; break;
3411 case TK_UPLUS: zUniOp = "UPLUS"; break;
3412 case TK_BITNOT: zUniOp = "BITNOT"; break;
3413 case TK_NOT: zUniOp = "NOT"; break;
3414 case TK_ISNULL: zUniOp = "ISNULL"; break;
3415 case TK_NOTNULL: zUniOp = "NOTNULL"; break;
3416
drh7a66da12012-12-07 20:31:11 +00003417 case TK_COLLATE: {
drh4fa4a542014-09-30 12:33:33 +00003418 sqlite3TreeViewLine(pView, "COLLATE %Q", pExpr->u.zToken);
3419 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
drh7a66da12012-12-07 20:31:11 +00003420 break;
3421 }
3422
drha84203a2011-12-07 01:23:51 +00003423 case TK_AGG_FUNCTION:
drha84203a2011-12-07 01:23:51 +00003424 case TK_FUNCTION: {
3425 ExprList *pFarg; /* List of function arguments */
drhc5cd1242013-09-12 16:50:49 +00003426 if( ExprHasProperty(pExpr, EP_TokenOnly) ){
drha84203a2011-12-07 01:23:51 +00003427 pFarg = 0;
3428 }else{
3429 pFarg = pExpr->x.pList;
3430 }
drh4fa4a542014-09-30 12:33:33 +00003431 if( pExpr->op==TK_AGG_FUNCTION ){
3432 sqlite3TreeViewLine(pView, "AGG_FUNCTION%d %Q",
drhed551b92012-08-23 19:46:11 +00003433 pExpr->op2, pExpr->u.zToken);
3434 }else{
drh4fa4a542014-09-30 12:33:33 +00003435 sqlite3TreeViewLine(pView, "FUNCTION %Q", pExpr->u.zToken);
drhed551b92012-08-23 19:46:11 +00003436 }
drha84203a2011-12-07 01:23:51 +00003437 if( pFarg ){
drh4fa4a542014-09-30 12:33:33 +00003438 sqlite3TreeViewExprList(pView, pFarg, 0, 0);
drha84203a2011-12-07 01:23:51 +00003439 }
drha84203a2011-12-07 01:23:51 +00003440 break;
3441 }
3442#ifndef SQLITE_OMIT_SUBQUERY
3443 case TK_EXISTS: {
drh4fa4a542014-09-30 12:33:33 +00003444 sqlite3TreeViewLine(pView, "EXISTS-expr");
3445 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
drha84203a2011-12-07 01:23:51 +00003446 break;
3447 }
3448 case TK_SELECT: {
drh4fa4a542014-09-30 12:33:33 +00003449 sqlite3TreeViewLine(pView, "SELECT-expr");
3450 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
drha84203a2011-12-07 01:23:51 +00003451 break;
3452 }
3453 case TK_IN: {
drh4fa4a542014-09-30 12:33:33 +00003454 sqlite3TreeViewLine(pView, "IN");
3455 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
drha84203a2011-12-07 01:23:51 +00003456 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
drh4fa4a542014-09-30 12:33:33 +00003457 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
drha84203a2011-12-07 01:23:51 +00003458 }else{
drh4fa4a542014-09-30 12:33:33 +00003459 sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
drha84203a2011-12-07 01:23:51 +00003460 }
drha84203a2011-12-07 01:23:51 +00003461 break;
3462 }
3463#endif /* SQLITE_OMIT_SUBQUERY */
3464
3465 /*
3466 ** x BETWEEN y AND z
3467 **
3468 ** This is equivalent to
3469 **
3470 ** x>=y AND x<=z
3471 **
3472 ** X is stored in pExpr->pLeft.
3473 ** Y is stored in pExpr->pList->a[0].pExpr.
3474 ** Z is stored in pExpr->pList->a[1].pExpr.
3475 */
3476 case TK_BETWEEN: {
3477 Expr *pX = pExpr->pLeft;
3478 Expr *pY = pExpr->x.pList->a[0].pExpr;
3479 Expr *pZ = pExpr->x.pList->a[1].pExpr;
drh4fa4a542014-09-30 12:33:33 +00003480 sqlite3TreeViewLine(pView, "BETWEEN");
3481 sqlite3TreeViewExpr(pView, pX, 1);
3482 sqlite3TreeViewExpr(pView, pY, 1);
3483 sqlite3TreeViewExpr(pView, pZ, 0);
drha84203a2011-12-07 01:23:51 +00003484 break;
3485 }
3486 case TK_TRIGGER: {
3487 /* If the opcode is TK_TRIGGER, then the expression is a reference
3488 ** to a column in the new.* or old.* pseudo-tables available to
3489 ** trigger programs. In this case Expr.iTable is set to 1 for the
3490 ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
3491 ** is set to the column of the pseudo-table to read, or to -1 to
3492 ** read the rowid field.
3493 */
drh4fa4a542014-09-30 12:33:33 +00003494 sqlite3TreeViewLine(pView, "%s(%d)",
drha84203a2011-12-07 01:23:51 +00003495 pExpr->iTable ? "NEW" : "OLD", pExpr->iColumn);
3496 break;
3497 }
3498 case TK_CASE: {
drh4fa4a542014-09-30 12:33:33 +00003499 sqlite3TreeViewLine(pView, "CASE");
3500 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
3501 sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
drha84203a2011-12-07 01:23:51 +00003502 break;
3503 }
3504#ifndef SQLITE_OMIT_TRIGGER
3505 case TK_RAISE: {
3506 const char *zType = "unk";
3507 switch( pExpr->affinity ){
3508 case OE_Rollback: zType = "rollback"; break;
3509 case OE_Abort: zType = "abort"; break;
3510 case OE_Fail: zType = "fail"; break;
3511 case OE_Ignore: zType = "ignore"; break;
3512 }
drh4fa4a542014-09-30 12:33:33 +00003513 sqlite3TreeViewLine(pView, "RAISE %s(%Q)", zType, pExpr->u.zToken);
drha84203a2011-12-07 01:23:51 +00003514 break;
3515 }
3516#endif
drh4fa4a542014-09-30 12:33:33 +00003517 default: {
3518 sqlite3TreeViewLine(pView, "op=%d", pExpr->op);
3519 break;
3520 }
drh7e02e5e2011-12-06 19:44:51 +00003521 }
drha84203a2011-12-07 01:23:51 +00003522 if( zBinOp ){
drh4fa4a542014-09-30 12:33:33 +00003523 sqlite3TreeViewLine(pView, "%s", zBinOp);
3524 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
3525 sqlite3TreeViewExpr(pView, pExpr->pRight, 0);
drha84203a2011-12-07 01:23:51 +00003526 }else if( zUniOp ){
drh4fa4a542014-09-30 12:33:33 +00003527 sqlite3TreeViewLine(pView, "%s", zUniOp);
3528 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
drh7e02e5e2011-12-06 19:44:51 +00003529 }
drh4fa4a542014-09-30 12:33:33 +00003530 sqlite3TreeViewPop(pView);
drh7e02e5e2011-12-06 19:44:51 +00003531}
drh4fa4a542014-09-30 12:33:33 +00003532#endif /* SQLITE_DEBUG */
drh7e02e5e2011-12-06 19:44:51 +00003533
drh4fa4a542014-09-30 12:33:33 +00003534#ifdef SQLITE_DEBUG
drh7e02e5e2011-12-06 19:44:51 +00003535/*
3536** Generate a human-readable explanation of an expression list.
3537*/
drh4fa4a542014-09-30 12:33:33 +00003538void sqlite3TreeViewExprList(
3539 TreeView *pView,
3540 const ExprList *pList,
3541 u8 moreToFollow,
3542 const char *zLabel
3543){
drh7e02e5e2011-12-06 19:44:51 +00003544 int i;
drh4fa4a542014-09-30 12:33:33 +00003545 pView = sqlite3TreeViewPush(pView, moreToFollow);
3546 if( zLabel==0 || zLabel[0]==0 ) zLabel = "LIST";
3547 if( pList==0 ){
3548 sqlite3TreeViewLine(pView, "%s (empty)", zLabel);
drha84203a2011-12-07 01:23:51 +00003549 }else{
drh4fa4a542014-09-30 12:33:33 +00003550 sqlite3TreeViewLine(pView, "%s", zLabel);
drha84203a2011-12-07 01:23:51 +00003551 for(i=0; i<pList->nExpr; i++){
drh4fa4a542014-09-30 12:33:33 +00003552 sqlite3TreeViewExpr(pView, pList->a[i].pExpr, i<pList->nExpr-1);
3553#if 0
3554 if( pList->a[i].zName ){
drh3e3f1a52013-01-03 00:45:56 +00003555 sqlite3ExplainPrintf(pOut, " AS %s", pList->a[i].zName);
3556 }
3557 if( pList->a[i].bSpanIsTab ){
3558 sqlite3ExplainPrintf(pOut, " (%s)", pList->a[i].zSpan);
3559 }
drh4fa4a542014-09-30 12:33:33 +00003560#endif
drh7e02e5e2011-12-06 19:44:51 +00003561 }
3562 }
drh4fa4a542014-09-30 12:33:33 +00003563 sqlite3TreeViewPop(pView);
drh7e02e5e2011-12-06 19:44:51 +00003564}
3565#endif /* SQLITE_DEBUG */
3566
drh678ccce2008-03-31 18:19:54 +00003567/*
drh268380c2004-02-25 13:47:31 +00003568** Generate code that pushes the value of every element of the given
drh9cbf3422008-01-17 16:22:13 +00003569** expression list into a sequence of registers beginning at target.
drh268380c2004-02-25 13:47:31 +00003570**
drh892d3172008-01-10 03:46:36 +00003571** Return the number of elements evaluated.
drhd1a01ed2013-11-21 16:08:52 +00003572**
3573** The SQLITE_ECEL_DUP flag prevents the arguments from being
3574** filled using OP_SCopy. OP_Copy must be used instead.
3575**
3576** The SQLITE_ECEL_FACTOR argument allows constant arguments to be
3577** factored out into initialization code.
drh268380c2004-02-25 13:47:31 +00003578*/
danielk19774adee202004-05-08 08:23:19 +00003579int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00003580 Parse *pParse, /* Parsing context */
drh389a1ad2008-01-03 23:44:53 +00003581 ExprList *pList, /* The expression list to be coded */
drh191b54c2008-04-15 12:14:21 +00003582 int target, /* Where to write results */
drhd1a01ed2013-11-21 16:08:52 +00003583 u8 flags /* SQLITE_ECEL_* flags */
drh268380c2004-02-25 13:47:31 +00003584){
3585 struct ExprList_item *pItem;
drh9cbf3422008-01-17 16:22:13 +00003586 int i, n;
drhd1a01ed2013-11-21 16:08:52 +00003587 u8 copyOp = (flags & SQLITE_ECEL_DUP) ? OP_Copy : OP_SCopy;
drh9d8b3072008-08-22 16:29:51 +00003588 assert( pList!=0 );
drh9cbf3422008-01-17 16:22:13 +00003589 assert( target>0 );
drhd81a1422010-09-28 07:11:24 +00003590 assert( pParse->pVdbe!=0 ); /* Never gets this far otherwise */
drh268380c2004-02-25 13:47:31 +00003591 n = pList->nExpr;
drhd9f158e2013-11-21 20:48:42 +00003592 if( !ConstFactorOk(pParse) ) flags &= ~SQLITE_ECEL_FACTOR;
drh191b54c2008-04-15 12:14:21 +00003593 for(pItem=pList->a, i=0; i<n; i++, pItem++){
drh7445ffe2010-09-27 18:14:12 +00003594 Expr *pExpr = pItem->pExpr;
drhd1a01ed2013-11-21 16:08:52 +00003595 if( (flags & SQLITE_ECEL_FACTOR)!=0 && sqlite3ExprIsConstant(pExpr) ){
drhd673cdd2013-11-21 21:23:31 +00003596 sqlite3ExprCodeAtInit(pParse, pExpr, target+i, 0);
drhd1a01ed2013-11-21 16:08:52 +00003597 }else{
3598 int inReg = sqlite3ExprCodeTarget(pParse, pExpr, target+i);
3599 if( inReg!=target+i ){
drh4eded602013-12-20 15:59:20 +00003600 VdbeOp *pOp;
3601 Vdbe *v = pParse->pVdbe;
3602 if( copyOp==OP_Copy
3603 && (pOp=sqlite3VdbeGetOp(v, -1))->opcode==OP_Copy
3604 && pOp->p1+pOp->p3+1==inReg
3605 && pOp->p2+pOp->p3+1==target+i
3606 ){
3607 pOp->p3++;
3608 }else{
3609 sqlite3VdbeAddOp2(v, copyOp, inReg, target+i);
3610 }
drhd1a01ed2013-11-21 16:08:52 +00003611 }
drhd1766112008-09-17 00:13:12 +00003612 }
drh268380c2004-02-25 13:47:31 +00003613 }
drhf9b596e2004-05-26 16:54:42 +00003614 return n;
drh268380c2004-02-25 13:47:31 +00003615}
3616
3617/*
drh36c563a2009-11-12 13:32:22 +00003618** Generate code for a BETWEEN operator.
3619**
3620** x BETWEEN y AND z
3621**
3622** The above is equivalent to
3623**
3624** x>=y AND x<=z
3625**
3626** Code it as such, taking care to do the common subexpression
peter.d.reid60ec9142014-09-06 16:39:46 +00003627** elimination of x.
drh36c563a2009-11-12 13:32:22 +00003628*/
3629static void exprCodeBetween(
3630 Parse *pParse, /* Parsing and code generating context */
3631 Expr *pExpr, /* The BETWEEN expression */
3632 int dest, /* Jump here if the jump is taken */
3633 int jumpIfTrue, /* Take the jump if the BETWEEN is true */
3634 int jumpIfNull /* Take the jump if the BETWEEN is NULL */
3635){
3636 Expr exprAnd; /* The AND operator in x>=y AND x<=z */
3637 Expr compLeft; /* The x>=y term */
3638 Expr compRight; /* The x<=z term */
3639 Expr exprX; /* The x subexpression */
3640 int regFree1 = 0; /* Temporary use register */
3641
3642 assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
3643 exprX = *pExpr->pLeft;
3644 exprAnd.op = TK_AND;
3645 exprAnd.pLeft = &compLeft;
3646 exprAnd.pRight = &compRight;
3647 compLeft.op = TK_GE;
3648 compLeft.pLeft = &exprX;
3649 compLeft.pRight = pExpr->x.pList->a[0].pExpr;
3650 compRight.op = TK_LE;
3651 compRight.pLeft = &exprX;
3652 compRight.pRight = pExpr->x.pList->a[1].pExpr;
drha4c3c872013-09-12 17:29:25 +00003653 exprToRegister(&exprX, sqlite3ExprCodeTemp(pParse, &exprX, &regFree1));
drh36c563a2009-11-12 13:32:22 +00003654 if( jumpIfTrue ){
3655 sqlite3ExprIfTrue(pParse, &exprAnd, dest, jumpIfNull);
3656 }else{
3657 sqlite3ExprIfFalse(pParse, &exprAnd, dest, jumpIfNull);
3658 }
3659 sqlite3ReleaseTempReg(pParse, regFree1);
3660
3661 /* Ensure adequate test coverage */
3662 testcase( jumpIfTrue==0 && jumpIfNull==0 && regFree1==0 );
3663 testcase( jumpIfTrue==0 && jumpIfNull==0 && regFree1!=0 );
3664 testcase( jumpIfTrue==0 && jumpIfNull!=0 && regFree1==0 );
3665 testcase( jumpIfTrue==0 && jumpIfNull!=0 && regFree1!=0 );
3666 testcase( jumpIfTrue!=0 && jumpIfNull==0 && regFree1==0 );
3667 testcase( jumpIfTrue!=0 && jumpIfNull==0 && regFree1!=0 );
3668 testcase( jumpIfTrue!=0 && jumpIfNull!=0 && regFree1==0 );
3669 testcase( jumpIfTrue!=0 && jumpIfNull!=0 && regFree1!=0 );
3670}
3671
3672/*
drhcce7d172000-05-31 15:34:51 +00003673** Generate code for a boolean expression such that a jump is made
3674** to the label "dest" if the expression is true but execution
3675** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00003676**
3677** If the expression evaluates to NULL (neither true nor false), then
drh35573352008-01-08 23:54:25 +00003678** take the jump if the jumpIfNull flag is SQLITE_JUMPIFNULL.
drhf2bc0132004-10-04 13:19:23 +00003679**
3680** This code depends on the fact that certain token values (ex: TK_EQ)
3681** are the same as opcode values (ex: OP_Eq) that implement the corresponding
3682** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
3683** the make process cause these values to align. Assert()s in the code
3684** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00003685*/
danielk19774adee202004-05-08 08:23:19 +00003686void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00003687 Vdbe *v = pParse->pVdbe;
3688 int op = 0;
drh2dcef112008-01-12 19:03:48 +00003689 int regFree1 = 0;
3690 int regFree2 = 0;
3691 int r1, r2;
3692
drh35573352008-01-08 23:54:25 +00003693 assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
mistachkin48864df2013-03-21 21:20:32 +00003694 if( NEVER(v==0) ) return; /* Existence of VDBE checked by caller */
drh33cd4902009-05-30 20:49:20 +00003695 if( NEVER(pExpr==0) ) return; /* No way this can happen */
drhf2bc0132004-10-04 13:19:23 +00003696 op = pExpr->op;
3697 switch( op ){
drhcce7d172000-05-31 15:34:51 +00003698 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00003699 int d2 = sqlite3VdbeMakeLabel(v);
drhc5499be2008-04-01 15:06:33 +00003700 testcase( jumpIfNull==0 );
drh35573352008-01-08 23:54:25 +00003701 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,jumpIfNull^SQLITE_JUMPIFNULL);
drh54e2adb2014-01-04 15:17:04 +00003702 sqlite3ExprCachePush(pParse);
danielk19774adee202004-05-08 08:23:19 +00003703 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
3704 sqlite3VdbeResolveLabel(v, d2);
drhd2490902014-04-13 19:28:15 +00003705 sqlite3ExprCachePop(pParse);
drhcce7d172000-05-31 15:34:51 +00003706 break;
3707 }
3708 case TK_OR: {
drhc5499be2008-04-01 15:06:33 +00003709 testcase( jumpIfNull==0 );
danielk19774adee202004-05-08 08:23:19 +00003710 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drh54e2adb2014-01-04 15:17:04 +00003711 sqlite3ExprCachePush(pParse);
danielk19774adee202004-05-08 08:23:19 +00003712 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhd2490902014-04-13 19:28:15 +00003713 sqlite3ExprCachePop(pParse);
drhcce7d172000-05-31 15:34:51 +00003714 break;
3715 }
3716 case TK_NOT: {
drhc5499be2008-04-01 15:06:33 +00003717 testcase( jumpIfNull==0 );
danielk19774adee202004-05-08 08:23:19 +00003718 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00003719 break;
3720 }
3721 case TK_LT:
3722 case TK_LE:
3723 case TK_GT:
3724 case TK_GE:
3725 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00003726 case TK_EQ: {
drhc5499be2008-04-01 15:06:33 +00003727 testcase( jumpIfNull==0 );
drhb6da74e2009-12-24 16:00:28 +00003728 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
3729 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
drh35573352008-01-08 23:54:25 +00003730 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
drh7d176102014-02-18 03:07:12 +00003731 r1, r2, dest, jumpIfNull);
3732 assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
3733 assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
3734 assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
3735 assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
3736 assert(TK_EQ==OP_Eq); testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq);
3737 assert(TK_NE==OP_Ne); testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne);
drhc5499be2008-04-01 15:06:33 +00003738 testcase( regFree1==0 );
3739 testcase( regFree2==0 );
drhcce7d172000-05-31 15:34:51 +00003740 break;
3741 }
drh6a2fe092009-09-23 02:29:36 +00003742 case TK_IS:
3743 case TK_ISNOT: {
3744 testcase( op==TK_IS );
3745 testcase( op==TK_ISNOT );
drhb6da74e2009-12-24 16:00:28 +00003746 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
3747 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
drh6a2fe092009-09-23 02:29:36 +00003748 op = (op==TK_IS) ? TK_EQ : TK_NE;
3749 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
drh7d176102014-02-18 03:07:12 +00003750 r1, r2, dest, SQLITE_NULLEQ);
3751 VdbeCoverageIf(v, op==TK_EQ);
3752 VdbeCoverageIf(v, op==TK_NE);
drh6a2fe092009-09-23 02:29:36 +00003753 testcase( regFree1==0 );
3754 testcase( regFree2==0 );
3755 break;
3756 }
drhcce7d172000-05-31 15:34:51 +00003757 case TK_ISNULL:
3758 case TK_NOTNULL: {
drh7d176102014-02-18 03:07:12 +00003759 assert( TK_ISNULL==OP_IsNull ); testcase( op==TK_ISNULL );
3760 assert( TK_NOTNULL==OP_NotNull ); testcase( op==TK_NOTNULL );
drh2dcef112008-01-12 19:03:48 +00003761 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
drh7d176102014-02-18 03:07:12 +00003762 sqlite3VdbeAddOp2(v, op, r1, dest);
3763 VdbeCoverageIf(v, op==TK_ISNULL);
3764 VdbeCoverageIf(v, op==TK_NOTNULL);
drhc5499be2008-04-01 15:06:33 +00003765 testcase( regFree1==0 );
drhcce7d172000-05-31 15:34:51 +00003766 break;
3767 }
drhfef52082000-06-06 01:50:43 +00003768 case TK_BETWEEN: {
drh5c03f302009-11-13 15:03:59 +00003769 testcase( jumpIfNull==0 );
drh36c563a2009-11-12 13:32:22 +00003770 exprCodeBetween(pParse, pExpr, dest, 1, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00003771 break;
3772 }
drh84e30ca2011-02-10 17:46:14 +00003773#ifndef SQLITE_OMIT_SUBQUERY
drhe3365e62009-11-12 17:52:24 +00003774 case TK_IN: {
3775 int destIfFalse = sqlite3VdbeMakeLabel(v);
3776 int destIfNull = jumpIfNull ? dest : destIfFalse;
3777 sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull);
3778 sqlite3VdbeAddOp2(v, OP_Goto, 0, dest);
3779 sqlite3VdbeResolveLabel(v, destIfFalse);
3780 break;
3781 }
shanehbb201342011-02-09 19:55:20 +00003782#endif
drhcce7d172000-05-31 15:34:51 +00003783 default: {
drh991a1982014-01-02 17:57:16 +00003784 if( exprAlwaysTrue(pExpr) ){
3785 sqlite3VdbeAddOp2(v, OP_Goto, 0, dest);
3786 }else if( exprAlwaysFalse(pExpr) ){
3787 /* No-op */
3788 }else{
3789 r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
3790 sqlite3VdbeAddOp3(v, OP_If, r1, dest, jumpIfNull!=0);
drh688852a2014-02-17 22:40:43 +00003791 VdbeCoverage(v);
drh991a1982014-01-02 17:57:16 +00003792 testcase( regFree1==0 );
3793 testcase( jumpIfNull==0 );
3794 }
drhcce7d172000-05-31 15:34:51 +00003795 break;
3796 }
3797 }
drh2dcef112008-01-12 19:03:48 +00003798 sqlite3ReleaseTempReg(pParse, regFree1);
3799 sqlite3ReleaseTempReg(pParse, regFree2);
drhcce7d172000-05-31 15:34:51 +00003800}
3801
3802/*
drh66b89c82000-11-28 20:47:17 +00003803** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00003804** to the label "dest" if the expression is false but execution
3805** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00003806**
3807** If the expression evaluates to NULL (neither true nor false) then
drh35573352008-01-08 23:54:25 +00003808** jump if jumpIfNull is SQLITE_JUMPIFNULL or fall through if jumpIfNull
3809** is 0.
drhcce7d172000-05-31 15:34:51 +00003810*/
danielk19774adee202004-05-08 08:23:19 +00003811void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00003812 Vdbe *v = pParse->pVdbe;
3813 int op = 0;
drh2dcef112008-01-12 19:03:48 +00003814 int regFree1 = 0;
3815 int regFree2 = 0;
3816 int r1, r2;
3817
drh35573352008-01-08 23:54:25 +00003818 assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
mistachkin48864df2013-03-21 21:20:32 +00003819 if( NEVER(v==0) ) return; /* Existence of VDBE checked by caller */
drh33cd4902009-05-30 20:49:20 +00003820 if( pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00003821
3822 /* The value of pExpr->op and op are related as follows:
3823 **
3824 ** pExpr->op op
3825 ** --------- ----------
3826 ** TK_ISNULL OP_NotNull
3827 ** TK_NOTNULL OP_IsNull
3828 ** TK_NE OP_Eq
3829 ** TK_EQ OP_Ne
3830 ** TK_GT OP_Le
3831 ** TK_LE OP_Gt
3832 ** TK_GE OP_Lt
3833 ** TK_LT OP_Ge
3834 **
3835 ** For other values of pExpr->op, op is undefined and unused.
3836 ** The value of TK_ and OP_ constants are arranged such that we
3837 ** can compute the mapping above using the following expression.
3838 ** Assert()s verify that the computation is correct.
3839 */
3840 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
3841
3842 /* Verify correct alignment of TK_ and OP_ constants
3843 */
3844 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
3845 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
3846 assert( pExpr->op!=TK_NE || op==OP_Eq );
3847 assert( pExpr->op!=TK_EQ || op==OP_Ne );
3848 assert( pExpr->op!=TK_LT || op==OP_Ge );
3849 assert( pExpr->op!=TK_LE || op==OP_Gt );
3850 assert( pExpr->op!=TK_GT || op==OP_Le );
3851 assert( pExpr->op!=TK_GE || op==OP_Lt );
3852
drhcce7d172000-05-31 15:34:51 +00003853 switch( pExpr->op ){
3854 case TK_AND: {
drhc5499be2008-04-01 15:06:33 +00003855 testcase( jumpIfNull==0 );
danielk19774adee202004-05-08 08:23:19 +00003856 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drh54e2adb2014-01-04 15:17:04 +00003857 sqlite3ExprCachePush(pParse);
danielk19774adee202004-05-08 08:23:19 +00003858 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhd2490902014-04-13 19:28:15 +00003859 sqlite3ExprCachePop(pParse);
drhcce7d172000-05-31 15:34:51 +00003860 break;
3861 }
3862 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00003863 int d2 = sqlite3VdbeMakeLabel(v);
drhc5499be2008-04-01 15:06:33 +00003864 testcase( jumpIfNull==0 );
drh35573352008-01-08 23:54:25 +00003865 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, jumpIfNull^SQLITE_JUMPIFNULL);
drh54e2adb2014-01-04 15:17:04 +00003866 sqlite3ExprCachePush(pParse);
danielk19774adee202004-05-08 08:23:19 +00003867 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
3868 sqlite3VdbeResolveLabel(v, d2);
drhd2490902014-04-13 19:28:15 +00003869 sqlite3ExprCachePop(pParse);
drhcce7d172000-05-31 15:34:51 +00003870 break;
3871 }
3872 case TK_NOT: {
drh5c03f302009-11-13 15:03:59 +00003873 testcase( jumpIfNull==0 );
danielk19774adee202004-05-08 08:23:19 +00003874 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00003875 break;
3876 }
3877 case TK_LT:
3878 case TK_LE:
3879 case TK_GT:
3880 case TK_GE:
3881 case TK_NE:
3882 case TK_EQ: {
drhc5499be2008-04-01 15:06:33 +00003883 testcase( jumpIfNull==0 );
drhb6da74e2009-12-24 16:00:28 +00003884 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
3885 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
drh35573352008-01-08 23:54:25 +00003886 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
drh7d176102014-02-18 03:07:12 +00003887 r1, r2, dest, jumpIfNull);
3888 assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
3889 assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
3890 assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
3891 assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
3892 assert(TK_EQ==OP_Eq); testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq);
3893 assert(TK_NE==OP_Ne); testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne);
drhc5499be2008-04-01 15:06:33 +00003894 testcase( regFree1==0 );
3895 testcase( regFree2==0 );
drhcce7d172000-05-31 15:34:51 +00003896 break;
3897 }
drh6a2fe092009-09-23 02:29:36 +00003898 case TK_IS:
3899 case TK_ISNOT: {
drh6d4486a2009-09-23 14:45:05 +00003900 testcase( pExpr->op==TK_IS );
3901 testcase( pExpr->op==TK_ISNOT );
drhb6da74e2009-12-24 16:00:28 +00003902 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
3903 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
drh6a2fe092009-09-23 02:29:36 +00003904 op = (pExpr->op==TK_IS) ? TK_NE : TK_EQ;
3905 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
drh7d176102014-02-18 03:07:12 +00003906 r1, r2, dest, SQLITE_NULLEQ);
3907 VdbeCoverageIf(v, op==TK_EQ);
3908 VdbeCoverageIf(v, op==TK_NE);
drh6a2fe092009-09-23 02:29:36 +00003909 testcase( regFree1==0 );
3910 testcase( regFree2==0 );
3911 break;
3912 }
drhcce7d172000-05-31 15:34:51 +00003913 case TK_ISNULL:
3914 case TK_NOTNULL: {
drh2dcef112008-01-12 19:03:48 +00003915 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
drh7d176102014-02-18 03:07:12 +00003916 sqlite3VdbeAddOp2(v, op, r1, dest);
3917 testcase( op==TK_ISNULL ); VdbeCoverageIf(v, op==TK_ISNULL);
3918 testcase( op==TK_NOTNULL ); VdbeCoverageIf(v, op==TK_NOTNULL);
drhc5499be2008-04-01 15:06:33 +00003919 testcase( regFree1==0 );
drhcce7d172000-05-31 15:34:51 +00003920 break;
3921 }
drhfef52082000-06-06 01:50:43 +00003922 case TK_BETWEEN: {
drh5c03f302009-11-13 15:03:59 +00003923 testcase( jumpIfNull==0 );
drh36c563a2009-11-12 13:32:22 +00003924 exprCodeBetween(pParse, pExpr, dest, 0, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00003925 break;
3926 }
drh84e30ca2011-02-10 17:46:14 +00003927#ifndef SQLITE_OMIT_SUBQUERY
drhe3365e62009-11-12 17:52:24 +00003928 case TK_IN: {
3929 if( jumpIfNull ){
3930 sqlite3ExprCodeIN(pParse, pExpr, dest, dest);
3931 }else{
3932 int destIfNull = sqlite3VdbeMakeLabel(v);
3933 sqlite3ExprCodeIN(pParse, pExpr, dest, destIfNull);
3934 sqlite3VdbeResolveLabel(v, destIfNull);
3935 }
3936 break;
3937 }
shanehbb201342011-02-09 19:55:20 +00003938#endif
drhcce7d172000-05-31 15:34:51 +00003939 default: {
drh991a1982014-01-02 17:57:16 +00003940 if( exprAlwaysFalse(pExpr) ){
3941 sqlite3VdbeAddOp2(v, OP_Goto, 0, dest);
3942 }else if( exprAlwaysTrue(pExpr) ){
3943 /* no-op */
3944 }else{
3945 r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
3946 sqlite3VdbeAddOp3(v, OP_IfNot, r1, dest, jumpIfNull!=0);
drh688852a2014-02-17 22:40:43 +00003947 VdbeCoverage(v);
drh991a1982014-01-02 17:57:16 +00003948 testcase( regFree1==0 );
3949 testcase( jumpIfNull==0 );
3950 }
drhcce7d172000-05-31 15:34:51 +00003951 break;
3952 }
3953 }
drh2dcef112008-01-12 19:03:48 +00003954 sqlite3ReleaseTempReg(pParse, regFree1);
3955 sqlite3ReleaseTempReg(pParse, regFree2);
drhcce7d172000-05-31 15:34:51 +00003956}
drh22827922000-06-06 17:27:05 +00003957
3958/*
drh1d9da702010-01-07 15:17:02 +00003959** Do a deep comparison of two expression trees. Return 0 if the two
3960** expressions are completely identical. Return 1 if they differ only
3961** by a COLLATE operator at the top level. Return 2 if there are differences
3962** other than the top-level COLLATE operator.
drhd40aab02007-02-24 15:29:03 +00003963**
drh619a1302013-08-01 13:04:46 +00003964** If any subelement of pB has Expr.iTable==(-1) then it is allowed
3965** to compare equal to an equivalent element in pA with Expr.iTable==iTab.
3966**
drh66518ca2013-08-01 15:09:57 +00003967** The pA side might be using TK_REGISTER. If that is the case and pB is
3968** not using TK_REGISTER but is otherwise equivalent, then still return 0.
3969**
drh1d9da702010-01-07 15:17:02 +00003970** Sometimes this routine will return 2 even if the two expressions
drhd40aab02007-02-24 15:29:03 +00003971** really are equivalent. If we cannot prove that the expressions are
drh1d9da702010-01-07 15:17:02 +00003972** identical, we return 2 just to be safe. So if this routine
3973** returns 2, then you do not really know for certain if the two
3974** expressions are the same. But if you get a 0 or 1 return, then you
drhd40aab02007-02-24 15:29:03 +00003975** can be sure the expressions are the same. In the places where
drh1d9da702010-01-07 15:17:02 +00003976** this routine is used, it does not hurt to get an extra 2 - that
drhd40aab02007-02-24 15:29:03 +00003977** just might result in some slightly slower code. But returning
drh1d9da702010-01-07 15:17:02 +00003978** an incorrect 0 or 1 could lead to a malfunction.
drh22827922000-06-06 17:27:05 +00003979*/
drh619a1302013-08-01 13:04:46 +00003980int sqlite3ExprCompare(Expr *pA, Expr *pB, int iTab){
drh10d1edf2013-11-15 15:52:39 +00003981 u32 combinedFlags;
3982 if( pA==0 || pB==0 ){
drh1d9da702010-01-07 15:17:02 +00003983 return pB==pA ? 0 : 2;
drh22827922000-06-06 17:27:05 +00003984 }
drh10d1edf2013-11-15 15:52:39 +00003985 combinedFlags = pA->flags | pB->flags;
3986 if( combinedFlags & EP_IntValue ){
3987 if( (pA->flags&pB->flags&EP_IntValue)!=0 && pA->u.iValue==pB->u.iValue ){
3988 return 0;
3989 }
drh1d9da702010-01-07 15:17:02 +00003990 return 2;
drh22827922000-06-06 17:27:05 +00003991 }
drhc2acc4e2013-11-15 18:15:19 +00003992 if( pA->op!=pB->op ){
drh619a1302013-08-01 13:04:46 +00003993 if( pA->op==TK_COLLATE && sqlite3ExprCompare(pA->pLeft, pB, iTab)<2 ){
drhae80dde2012-12-06 21:16:43 +00003994 return 1;
3995 }
drh619a1302013-08-01 13:04:46 +00003996 if( pB->op==TK_COLLATE && sqlite3ExprCompare(pA, pB->pLeft, iTab)<2 ){
drhae80dde2012-12-06 21:16:43 +00003997 return 1;
3998 }
3999 return 2;
4000 }
drh10d1edf2013-11-15 15:52:39 +00004001 if( pA->op!=TK_COLUMN && ALWAYS(pA->op!=TK_AGG_COLUMN) && pA->u.zToken ){
drh6b93c9a2011-10-13 15:35:52 +00004002 if( strcmp(pA->u.zToken,pB->u.zToken)!=0 ){
drhae80dde2012-12-06 21:16:43 +00004003 return pA->op==TK_COLLATE ? 1 : 2;
drh2646da72005-12-09 20:02:05 +00004004 }
drh22827922000-06-06 17:27:05 +00004005 }
drh10d1edf2013-11-15 15:52:39 +00004006 if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 2;
drh85f8aa72013-11-15 20:06:26 +00004007 if( ALWAYS((combinedFlags & EP_TokenOnly)==0) ){
drh10d1edf2013-11-15 15:52:39 +00004008 if( combinedFlags & EP_xIsSelect ) return 2;
4009 if( sqlite3ExprCompare(pA->pLeft, pB->pLeft, iTab) ) return 2;
4010 if( sqlite3ExprCompare(pA->pRight, pB->pRight, iTab) ) return 2;
4011 if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList, iTab) ) return 2;
drh85f8aa72013-11-15 20:06:26 +00004012 if( ALWAYS((combinedFlags & EP_Reduced)==0) ){
drh10d1edf2013-11-15 15:52:39 +00004013 if( pA->iColumn!=pB->iColumn ) return 2;
4014 if( pA->iTable!=pB->iTable
drh85f8aa72013-11-15 20:06:26 +00004015 && (pA->iTable!=iTab || NEVER(pB->iTable>=0)) ) return 2;
drh10d1edf2013-11-15 15:52:39 +00004016 }
4017 }
drh1d9da702010-01-07 15:17:02 +00004018 return 0;
drh22827922000-06-06 17:27:05 +00004019}
4020
drh8c6f6662010-04-26 19:17:26 +00004021/*
4022** Compare two ExprList objects. Return 0 if they are identical and
4023** non-zero if they differ in any way.
4024**
drh619a1302013-08-01 13:04:46 +00004025** If any subelement of pB has Expr.iTable==(-1) then it is allowed
4026** to compare equal to an equivalent element in pA with Expr.iTable==iTab.
4027**
drh8c6f6662010-04-26 19:17:26 +00004028** This routine might return non-zero for equivalent ExprLists. The
4029** only consequence will be disabled optimizations. But this routine
4030** must never return 0 if the two ExprList objects are different, or
4031** a malfunction will result.
4032**
4033** Two NULL pointers are considered to be the same. But a NULL pointer
4034** always differs from a non-NULL pointer.
4035*/
drh619a1302013-08-01 13:04:46 +00004036int sqlite3ExprListCompare(ExprList *pA, ExprList *pB, int iTab){
drh8c6f6662010-04-26 19:17:26 +00004037 int i;
4038 if( pA==0 && pB==0 ) return 0;
4039 if( pA==0 || pB==0 ) return 1;
4040 if( pA->nExpr!=pB->nExpr ) return 1;
4041 for(i=0; i<pA->nExpr; i++){
4042 Expr *pExprA = pA->a[i].pExpr;
4043 Expr *pExprB = pB->a[i].pExpr;
4044 if( pA->a[i].sortOrder!=pB->a[i].sortOrder ) return 1;
drh619a1302013-08-01 13:04:46 +00004045 if( sqlite3ExprCompare(pExprA, pExprB, iTab) ) return 1;
drh8c6f6662010-04-26 19:17:26 +00004046 }
4047 return 0;
4048}
drh13449892005-09-07 21:22:45 +00004049
drh22827922000-06-06 17:27:05 +00004050/*
drh4bd5f732013-07-31 23:22:39 +00004051** Return true if we can prove the pE2 will always be true if pE1 is
4052** true. Return false if we cannot complete the proof or if pE2 might
4053** be false. Examples:
4054**
drh619a1302013-08-01 13:04:46 +00004055** pE1: x==5 pE2: x==5 Result: true
4056** pE1: x>0 pE2: x==5 Result: false
4057** pE1: x=21 pE2: x=21 OR y=43 Result: true
4058** pE1: x!=123 pE2: x IS NOT NULL Result: true
4059** pE1: x!=?1 pE2: x IS NOT NULL Result: true
4060** pE1: x IS NULL pE2: x IS NOT NULL Result: false
4061** pE1: x IS ?2 pE2: x IS NOT NULL Reuslt: false
drh4bd5f732013-07-31 23:22:39 +00004062**
4063** When comparing TK_COLUMN nodes between pE1 and pE2, if pE2 has
4064** Expr.iTable<0 then assume a table number given by iTab.
4065**
4066** When in doubt, return false. Returning true might give a performance
4067** improvement. Returning false might cause a performance reduction, but
4068** it will always give the correct answer and is hence always safe.
4069*/
4070int sqlite3ExprImpliesExpr(Expr *pE1, Expr *pE2, int iTab){
drh619a1302013-08-01 13:04:46 +00004071 if( sqlite3ExprCompare(pE1, pE2, iTab)==0 ){
4072 return 1;
4073 }
4074 if( pE2->op==TK_OR
4075 && (sqlite3ExprImpliesExpr(pE1, pE2->pLeft, iTab)
4076 || sqlite3ExprImpliesExpr(pE1, pE2->pRight, iTab) )
4077 ){
4078 return 1;
4079 }
4080 if( pE2->op==TK_NOTNULL
4081 && sqlite3ExprCompare(pE1->pLeft, pE2->pLeft, iTab)==0
4082 && (pE1->op!=TK_ISNULL && pE1->op!=TK_IS)
4083 ){
4084 return 1;
4085 }
4086 return 0;
drh4bd5f732013-07-31 23:22:39 +00004087}
4088
4089/*
drh030796d2012-08-23 16:18:10 +00004090** An instance of the following structure is used by the tree walker
4091** to count references to table columns in the arguments of an
drhed551b92012-08-23 19:46:11 +00004092** aggregate function, in order to implement the
4093** sqlite3FunctionThisSrc() routine.
drh374fdce2012-04-17 16:38:53 +00004094*/
drh030796d2012-08-23 16:18:10 +00004095struct SrcCount {
4096 SrcList *pSrc; /* One particular FROM clause in a nested query */
4097 int nThis; /* Number of references to columns in pSrcList */
4098 int nOther; /* Number of references to columns in other FROM clauses */
4099};
4100
4101/*
4102** Count the number of references to columns.
4103*/
4104static int exprSrcCount(Walker *pWalker, Expr *pExpr){
drhfb0a6082012-08-24 01:07:52 +00004105 /* The NEVER() on the second term is because sqlite3FunctionUsesThisSrc()
4106 ** is always called before sqlite3ExprAnalyzeAggregates() and so the
4107 ** TK_COLUMNs have not yet been converted into TK_AGG_COLUMN. If
4108 ** sqlite3FunctionUsesThisSrc() is used differently in the future, the
4109 ** NEVER() will need to be removed. */
4110 if( pExpr->op==TK_COLUMN || NEVER(pExpr->op==TK_AGG_COLUMN) ){
drh374fdce2012-04-17 16:38:53 +00004111 int i;
drh030796d2012-08-23 16:18:10 +00004112 struct SrcCount *p = pWalker->u.pSrcCount;
4113 SrcList *pSrc = p->pSrc;
drh655814d2015-01-09 01:27:29 +00004114 int nSrc = pSrc ? pSrc->nSrc : 0;
4115 for(i=0; i<nSrc; i++){
drh030796d2012-08-23 16:18:10 +00004116 if( pExpr->iTable==pSrc->a[i].iCursor ) break;
drh374fdce2012-04-17 16:38:53 +00004117 }
drh655814d2015-01-09 01:27:29 +00004118 if( i<nSrc ){
drh030796d2012-08-23 16:18:10 +00004119 p->nThis++;
4120 }else{
4121 p->nOther++;
4122 }
drh374fdce2012-04-17 16:38:53 +00004123 }
drh030796d2012-08-23 16:18:10 +00004124 return WRC_Continue;
drh374fdce2012-04-17 16:38:53 +00004125}
4126
4127/*
drh030796d2012-08-23 16:18:10 +00004128** Determine if any of the arguments to the pExpr Function reference
4129** pSrcList. Return true if they do. Also return true if the function
4130** has no arguments or has only constant arguments. Return false if pExpr
4131** references columns but not columns of tables found in pSrcList.
drh374fdce2012-04-17 16:38:53 +00004132*/
drh030796d2012-08-23 16:18:10 +00004133int sqlite3FunctionUsesThisSrc(Expr *pExpr, SrcList *pSrcList){
drh374fdce2012-04-17 16:38:53 +00004134 Walker w;
drh030796d2012-08-23 16:18:10 +00004135 struct SrcCount cnt;
drh374fdce2012-04-17 16:38:53 +00004136 assert( pExpr->op==TK_AGG_FUNCTION );
4137 memset(&w, 0, sizeof(w));
drh030796d2012-08-23 16:18:10 +00004138 w.xExprCallback = exprSrcCount;
4139 w.u.pSrcCount = &cnt;
4140 cnt.pSrc = pSrcList;
4141 cnt.nThis = 0;
4142 cnt.nOther = 0;
4143 sqlite3WalkExprList(&w, pExpr->x.pList);
4144 return cnt.nThis>0 || cnt.nOther==0;
drh374fdce2012-04-17 16:38:53 +00004145}
4146
4147/*
drh13449892005-09-07 21:22:45 +00004148** Add a new element to the pAggInfo->aCol[] array. Return the index of
4149** the new element. Return a negative number if malloc fails.
drh22827922000-06-06 17:27:05 +00004150*/
drh17435752007-08-16 04:30:38 +00004151static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){
drh13449892005-09-07 21:22:45 +00004152 int i;
drhcf643722007-03-27 13:36:37 +00004153 pInfo->aCol = sqlite3ArrayAllocate(
drh17435752007-08-16 04:30:38 +00004154 db,
drhcf643722007-03-27 13:36:37 +00004155 pInfo->aCol,
4156 sizeof(pInfo->aCol[0]),
drhcf643722007-03-27 13:36:37 +00004157 &pInfo->nColumn,
drhcf643722007-03-27 13:36:37 +00004158 &i
4159 );
drh13449892005-09-07 21:22:45 +00004160 return i;
4161}
4162
4163/*
4164** Add a new element to the pAggInfo->aFunc[] array. Return the index of
4165** the new element. Return a negative number if malloc fails.
4166*/
drh17435752007-08-16 04:30:38 +00004167static int addAggInfoFunc(sqlite3 *db, AggInfo *pInfo){
drh13449892005-09-07 21:22:45 +00004168 int i;
drhcf643722007-03-27 13:36:37 +00004169 pInfo->aFunc = sqlite3ArrayAllocate(
drh17435752007-08-16 04:30:38 +00004170 db,
drhcf643722007-03-27 13:36:37 +00004171 pInfo->aFunc,
4172 sizeof(pInfo->aFunc[0]),
drhcf643722007-03-27 13:36:37 +00004173 &pInfo->nFunc,
drhcf643722007-03-27 13:36:37 +00004174 &i
4175 );
drh13449892005-09-07 21:22:45 +00004176 return i;
4177}
drh22827922000-06-06 17:27:05 +00004178
4179/*
drh7d10d5a2008-08-20 16:35:10 +00004180** This is the xExprCallback for a tree walker. It is used to
4181** implement sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates
drh626a8792005-01-17 22:08:19 +00004182** for additional information.
drh22827922000-06-06 17:27:05 +00004183*/
drh7d10d5a2008-08-20 16:35:10 +00004184static int analyzeAggregate(Walker *pWalker, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00004185 int i;
drh7d10d5a2008-08-20 16:35:10 +00004186 NameContext *pNC = pWalker->u.pNC;
danielk1977a58fdfb2005-02-08 07:50:40 +00004187 Parse *pParse = pNC->pParse;
4188 SrcList *pSrcList = pNC->pSrcList;
drh13449892005-09-07 21:22:45 +00004189 AggInfo *pAggInfo = pNC->pAggInfo;
drh22827922000-06-06 17:27:05 +00004190
drh22827922000-06-06 17:27:05 +00004191 switch( pExpr->op ){
drh89c69d02007-01-04 01:20:28 +00004192 case TK_AGG_COLUMN:
drh967e8b72000-06-21 13:59:10 +00004193 case TK_COLUMN: {
drh8b213892008-08-29 02:14:02 +00004194 testcase( pExpr->op==TK_AGG_COLUMN );
4195 testcase( pExpr->op==TK_COLUMN );
drh13449892005-09-07 21:22:45 +00004196 /* Check to see if the column is in one of the tables in the FROM
4197 ** clause of the aggregate query */
drh20bc3932009-05-30 23:35:43 +00004198 if( ALWAYS(pSrcList!=0) ){
drh13449892005-09-07 21:22:45 +00004199 struct SrcList_item *pItem = pSrcList->a;
4200 for(i=0; i<pSrcList->nSrc; i++, pItem++){
4201 struct AggInfo_col *pCol;
drhc5cd1242013-09-12 16:50:49 +00004202 assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) );
drh13449892005-09-07 21:22:45 +00004203 if( pExpr->iTable==pItem->iCursor ){
4204 /* If we reach this point, it means that pExpr refers to a table
4205 ** that is in the FROM clause of the aggregate query.
4206 **
4207 ** Make an entry for the column in pAggInfo->aCol[] if there
4208 ** is not an entry there already.
4209 */
drh7f906d62007-03-12 23:48:52 +00004210 int k;
drh13449892005-09-07 21:22:45 +00004211 pCol = pAggInfo->aCol;
drh7f906d62007-03-12 23:48:52 +00004212 for(k=0; k<pAggInfo->nColumn; k++, pCol++){
drh13449892005-09-07 21:22:45 +00004213 if( pCol->iTable==pExpr->iTable &&
4214 pCol->iColumn==pExpr->iColumn ){
4215 break;
4216 }
danielk1977a58fdfb2005-02-08 07:50:40 +00004217 }
danielk19771e536952007-08-16 10:09:01 +00004218 if( (k>=pAggInfo->nColumn)
4219 && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0
4220 ){
drh7f906d62007-03-12 23:48:52 +00004221 pCol = &pAggInfo->aCol[k];
danielk19770817d0d2007-02-14 09:19:36 +00004222 pCol->pTab = pExpr->pTab;
drh13449892005-09-07 21:22:45 +00004223 pCol->iTable = pExpr->iTable;
4224 pCol->iColumn = pExpr->iColumn;
drh0a07c102008-01-03 18:03:08 +00004225 pCol->iMem = ++pParse->nMem;
drh13449892005-09-07 21:22:45 +00004226 pCol->iSorterColumn = -1;
drh5774b802005-09-07 22:48:16 +00004227 pCol->pExpr = pExpr;
drh13449892005-09-07 21:22:45 +00004228 if( pAggInfo->pGroupBy ){
4229 int j, n;
4230 ExprList *pGB = pAggInfo->pGroupBy;
4231 struct ExprList_item *pTerm = pGB->a;
4232 n = pGB->nExpr;
4233 for(j=0; j<n; j++, pTerm++){
4234 Expr *pE = pTerm->pExpr;
4235 if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable &&
4236 pE->iColumn==pExpr->iColumn ){
4237 pCol->iSorterColumn = j;
4238 break;
4239 }
4240 }
4241 }
4242 if( pCol->iSorterColumn<0 ){
4243 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
4244 }
4245 }
4246 /* There is now an entry for pExpr in pAggInfo->aCol[] (either
4247 ** because it was there before or because we just created it).
4248 ** Convert the pExpr to be a TK_AGG_COLUMN referring to that
4249 ** pAggInfo->aCol[] entry.
4250 */
drhebb6a652013-09-12 23:42:22 +00004251 ExprSetVVAProperty(pExpr, EP_NoReduce);
drh13449892005-09-07 21:22:45 +00004252 pExpr->pAggInfo = pAggInfo;
4253 pExpr->op = TK_AGG_COLUMN;
shanecf697392009-06-01 16:53:09 +00004254 pExpr->iAgg = (i16)k;
drh13449892005-09-07 21:22:45 +00004255 break;
4256 } /* endif pExpr->iTable==pItem->iCursor */
4257 } /* end loop over pSrcList */
drh22827922000-06-06 17:27:05 +00004258 }
drh7d10d5a2008-08-20 16:35:10 +00004259 return WRC_Prune;
drh22827922000-06-06 17:27:05 +00004260 }
4261 case TK_AGG_FUNCTION: {
drh3a8c4be2012-05-21 20:13:39 +00004262 if( (pNC->ncFlags & NC_InAggFunc)==0
drhed551b92012-08-23 19:46:11 +00004263 && pWalker->walkerDepth==pExpr->op2
drh3a8c4be2012-05-21 20:13:39 +00004264 ){
drh13449892005-09-07 21:22:45 +00004265 /* Check to see if pExpr is a duplicate of another aggregate
4266 ** function that is already in the pAggInfo structure
4267 */
4268 struct AggInfo_func *pItem = pAggInfo->aFunc;
4269 for(i=0; i<pAggInfo->nFunc; i++, pItem++){
drh619a1302013-08-01 13:04:46 +00004270 if( sqlite3ExprCompare(pItem->pExpr, pExpr, -1)==0 ){
danielk1977a58fdfb2005-02-08 07:50:40 +00004271 break;
4272 }
drh22827922000-06-06 17:27:05 +00004273 }
drh13449892005-09-07 21:22:45 +00004274 if( i>=pAggInfo->nFunc ){
4275 /* pExpr is original. Make a new entry in pAggInfo->aFunc[]
4276 */
danielk197714db2662006-01-09 16:12:04 +00004277 u8 enc = ENC(pParse->db);
danielk19771e536952007-08-16 10:09:01 +00004278 i = addAggInfoFunc(pParse->db, pAggInfo);
drh13449892005-09-07 21:22:45 +00004279 if( i>=0 ){
danielk19776ab3a2e2009-02-19 14:39:25 +00004280 assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
drh13449892005-09-07 21:22:45 +00004281 pItem = &pAggInfo->aFunc[i];
4282 pItem->pExpr = pExpr;
drh0a07c102008-01-03 18:03:08 +00004283 pItem->iMem = ++pParse->nMem;
drh33e619f2009-05-28 01:00:55 +00004284 assert( !ExprHasProperty(pExpr, EP_IntValue) );
drh13449892005-09-07 21:22:45 +00004285 pItem->pFunc = sqlite3FindFunction(pParse->db,
drh33e619f2009-05-28 01:00:55 +00004286 pExpr->u.zToken, sqlite3Strlen30(pExpr->u.zToken),
danielk19776ab3a2e2009-02-19 14:39:25 +00004287 pExpr->x.pList ? pExpr->x.pList->nExpr : 0, enc, 0);
drhfd357972005-09-09 01:33:19 +00004288 if( pExpr->flags & EP_Distinct ){
4289 pItem->iDistinct = pParse->nTab++;
4290 }else{
4291 pItem->iDistinct = -1;
4292 }
drh13449892005-09-07 21:22:45 +00004293 }
danielk1977a58fdfb2005-02-08 07:50:40 +00004294 }
drh13449892005-09-07 21:22:45 +00004295 /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
4296 */
drhc5cd1242013-09-12 16:50:49 +00004297 assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) );
drhebb6a652013-09-12 23:42:22 +00004298 ExprSetVVAProperty(pExpr, EP_NoReduce);
shanecf697392009-06-01 16:53:09 +00004299 pExpr->iAgg = (i16)i;
drh13449892005-09-07 21:22:45 +00004300 pExpr->pAggInfo = pAggInfo;
drh6e83a572012-11-02 18:48:49 +00004301 return WRC_Prune;
4302 }else{
4303 return WRC_Continue;
drh22827922000-06-06 17:27:05 +00004304 }
drh22827922000-06-06 17:27:05 +00004305 }
4306 }
drh7d10d5a2008-08-20 16:35:10 +00004307 return WRC_Continue;
4308}
4309static int analyzeAggregatesInSelect(Walker *pWalker, Select *pSelect){
drhd5a336e2012-04-19 15:49:19 +00004310 UNUSED_PARAMETER(pWalker);
4311 UNUSED_PARAMETER(pSelect);
drh374fdce2012-04-17 16:38:53 +00004312 return WRC_Continue;
drh626a8792005-01-17 22:08:19 +00004313}
4314
4315/*
drhe8abb4c2012-11-02 18:24:57 +00004316** Analyze the pExpr expression looking for aggregate functions and
4317** for variables that need to be added to AggInfo object that pNC->pAggInfo
4318** points to. Additional entries are made on the AggInfo object as
4319** necessary.
drh626a8792005-01-17 22:08:19 +00004320**
4321** This routine should only be called after the expression has been
drh7d10d5a2008-08-20 16:35:10 +00004322** analyzed by sqlite3ResolveExprNames().
drh626a8792005-01-17 22:08:19 +00004323*/
drhd2b3e232008-01-23 14:51:49 +00004324void sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
drh7d10d5a2008-08-20 16:35:10 +00004325 Walker w;
drh374fdce2012-04-17 16:38:53 +00004326 memset(&w, 0, sizeof(w));
drh7d10d5a2008-08-20 16:35:10 +00004327 w.xExprCallback = analyzeAggregate;
4328 w.xSelectCallback = analyzeAggregatesInSelect;
4329 w.u.pNC = pNC;
drh20bc3932009-05-30 23:35:43 +00004330 assert( pNC->pSrcList!=0 );
drh7d10d5a2008-08-20 16:35:10 +00004331 sqlite3WalkExpr(&w, pExpr);
drh22827922000-06-06 17:27:05 +00004332}
drh5d9a4af2005-08-30 00:54:01 +00004333
4334/*
4335** Call sqlite3ExprAnalyzeAggregates() for every expression in an
4336** expression list. Return the number of errors.
4337**
4338** If an error is found, the analysis is cut short.
4339*/
drhd2b3e232008-01-23 14:51:49 +00004340void sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){
drh5d9a4af2005-08-30 00:54:01 +00004341 struct ExprList_item *pItem;
4342 int i;
drh5d9a4af2005-08-30 00:54:01 +00004343 if( pList ){
drhd2b3e232008-01-23 14:51:49 +00004344 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
4345 sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
drh5d9a4af2005-08-30 00:54:01 +00004346 }
4347 }
drh5d9a4af2005-08-30 00:54:01 +00004348}
drh892d3172008-01-10 03:46:36 +00004349
4350/*
drhceea3322009-04-23 13:22:42 +00004351** Allocate a single new register for use to hold some intermediate result.
drh892d3172008-01-10 03:46:36 +00004352*/
4353int sqlite3GetTempReg(Parse *pParse){
drhe55cbd72008-03-31 23:48:03 +00004354 if( pParse->nTempReg==0 ){
drh892d3172008-01-10 03:46:36 +00004355 return ++pParse->nMem;
4356 }
danielk19772f425f62008-07-04 09:41:39 +00004357 return pParse->aTempReg[--pParse->nTempReg];
drh892d3172008-01-10 03:46:36 +00004358}
drhceea3322009-04-23 13:22:42 +00004359
4360/*
4361** Deallocate a register, making available for reuse for some other
4362** purpose.
4363**
4364** If a register is currently being used by the column cache, then
peter.d.reid60ec9142014-09-06 16:39:46 +00004365** the deallocation is deferred until the column cache line that uses
drhceea3322009-04-23 13:22:42 +00004366** the register becomes stale.
4367*/
drh892d3172008-01-10 03:46:36 +00004368void sqlite3ReleaseTempReg(Parse *pParse, int iReg){
drh2dcef112008-01-12 19:03:48 +00004369 if( iReg && pParse->nTempReg<ArraySize(pParse->aTempReg) ){
drhceea3322009-04-23 13:22:42 +00004370 int i;
4371 struct yColCache *p;
4372 for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
4373 if( p->iReg==iReg ){
4374 p->tempReg = 1;
4375 return;
4376 }
4377 }
drh892d3172008-01-10 03:46:36 +00004378 pParse->aTempReg[pParse->nTempReg++] = iReg;
4379 }
4380}
4381
4382/*
4383** Allocate or deallocate a block of nReg consecutive registers
4384*/
4385int sqlite3GetTempRange(Parse *pParse, int nReg){
drhe55cbd72008-03-31 23:48:03 +00004386 int i, n;
4387 i = pParse->iRangeReg;
4388 n = pParse->nRangeReg;
drhf49f3522009-12-30 14:12:38 +00004389 if( nReg<=n ){
4390 assert( !usedAsColumnCache(pParse, i, i+n-1) );
drh892d3172008-01-10 03:46:36 +00004391 pParse->iRangeReg += nReg;
4392 pParse->nRangeReg -= nReg;
4393 }else{
4394 i = pParse->nMem+1;
4395 pParse->nMem += nReg;
4396 }
4397 return i;
4398}
4399void sqlite3ReleaseTempRange(Parse *pParse, int iReg, int nReg){
drhf49f3522009-12-30 14:12:38 +00004400 sqlite3ExprCacheRemove(pParse, iReg, nReg);
drh892d3172008-01-10 03:46:36 +00004401 if( nReg>pParse->nRangeReg ){
4402 pParse->nRangeReg = nReg;
4403 pParse->iRangeReg = iReg;
4404 }
4405}
drhcdc69552011-12-06 13:24:59 +00004406
4407/*
4408** Mark all temporary registers as being unavailable for reuse.
4409*/
4410void sqlite3ClearTempRegCache(Parse *pParse){
4411 pParse->nTempReg = 0;
4412 pParse->nRangeReg = 0;
4413}