blob: 6e114719d27f2119aaa77b81fb3eb2aed2856fbd [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**
drhd1fa7bc2009-01-10 13:24:50 +000015** $Id: expr.c,v 1.409 2009/01/10 13:24:51 drh Exp $
drhcce7d172000-05-31 15:34:51 +000016*/
17#include "sqliteInt.h"
drh04738cb2002-06-02 18:19:00 +000018#include <ctype.h>
drha2e00042002-01-22 03:13:42 +000019
danielk1977e014a832004-05-17 10:48:57 +000020/*
21** Return the 'affinity' of the expression pExpr if any.
22**
23** If pExpr is a column, a reference to a column via an 'AS' alias,
24** or a sub-select with a column as the return value, then the
25** affinity of that column is returned. Otherwise, 0x00 is returned,
26** indicating no affinity for the expression.
27**
28** i.e. the WHERE clause expresssions in the following statements all
29** have an affinity:
30**
31** CREATE TABLE t1(a);
32** SELECT * FROM t1 WHERE a;
33** SELECT a AS b FROM t1 WHERE b;
34** SELECT * FROM t1 WHERE (select a from t1);
35*/
danielk1977bf3b7212004-05-18 10:06:24 +000036char sqlite3ExprAffinity(Expr *pExpr){
drh487e2622005-06-25 18:42:14 +000037 int op = pExpr->op;
drh487e2622005-06-25 18:42:14 +000038 if( op==TK_SELECT ){
danielk1977bf3b7212004-05-18 10:06:24 +000039 return sqlite3ExprAffinity(pExpr->pSelect->pEList->a[0].pExpr);
danielk1977a37cdde2004-05-16 11:15:36 +000040 }
drh487e2622005-06-25 18:42:14 +000041#ifndef SQLITE_OMIT_CAST
42 if( op==TK_CAST ){
drh8a512562005-11-14 22:29:05 +000043 return sqlite3AffinityType(&pExpr->token);
drh487e2622005-06-25 18:42:14 +000044 }
45#endif
danielk1977259a4552008-11-12 08:07:12 +000046 if( (op==TK_AGG_COLUMN || op==TK_COLUMN || op==TK_REGISTER)
47 && pExpr->pTab!=0
48 ){
drh7d10d5a2008-08-20 16:35:10 +000049 /* op==TK_REGISTER && pExpr->pTab!=0 happens when pExpr was originally
50 ** a TK_COLUMN but was previously evaluated and cached in a register */
51 int j = pExpr->iColumn;
52 if( j<0 ) return SQLITE_AFF_INTEGER;
53 assert( pExpr->pTab && j<pExpr->pTab->nCol );
54 return pExpr->pTab->aCol[j].affinity;
55 }
danielk1977a37cdde2004-05-16 11:15:36 +000056 return pExpr->affinity;
57}
58
drh53db1452004-05-20 13:54:53 +000059/*
drh8b4c40d2007-02-01 23:02:45 +000060** Set the collating sequence for expression pExpr to be the collating
61** sequence named by pToken. Return a pointer to the revised expression.
drha34001c2007-02-02 12:44:37 +000062** The collating sequence is marked as "explicit" using the EP_ExpCollate
63** flag. An explicit collating sequence will override implicit
64** collating sequences.
drh8b4c40d2007-02-01 23:02:45 +000065*/
drh7d10d5a2008-08-20 16:35:10 +000066Expr *sqlite3ExprSetColl(Parse *pParse, Expr *pExpr, Token *pCollName){
danielk197739002502007-11-12 09:50:26 +000067 char *zColl = 0; /* Dequoted name of collation sequence */
drh8b4c40d2007-02-01 23:02:45 +000068 CollSeq *pColl;
drh633e6d52008-07-28 19:34:53 +000069 sqlite3 *db = pParse->db;
drh7d10d5a2008-08-20 16:35:10 +000070 zColl = sqlite3NameFromToken(db, pCollName);
danielk197739002502007-11-12 09:50:26 +000071 if( pExpr && zColl ){
72 pColl = sqlite3LocateCollSeq(pParse, zColl, -1);
73 if( pColl ){
74 pExpr->pColl = pColl;
75 pExpr->flags |= EP_ExpCollate;
76 }
drh8b4c40d2007-02-01 23:02:45 +000077 }
drh633e6d52008-07-28 19:34:53 +000078 sqlite3DbFree(db, zColl);
drh8b4c40d2007-02-01 23:02:45 +000079 return pExpr;
80}
81
82/*
danielk19770202b292004-06-09 09:55:16 +000083** Return the default collation sequence for the expression pExpr. If
84** there is no default collation type, return 0.
85*/
danielk19777cedc8d2004-06-10 10:50:08 +000086CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
87 CollSeq *pColl = 0;
drh7d10d5a2008-08-20 16:35:10 +000088 Expr *p = pExpr;
89 while( p ){
drh7e09fe02007-06-20 16:13:23 +000090 int op;
drh7d10d5a2008-08-20 16:35:10 +000091 pColl = p->pColl;
92 if( pColl ) break;
93 op = p->op;
danielk1977259a4552008-11-12 08:07:12 +000094 if( (op==TK_AGG_COLUMN || op==TK_COLUMN || op==TK_REGISTER) && p->pTab!=0 ){
drh7d10d5a2008-08-20 16:35:10 +000095 /* op==TK_REGISTER && p->pTab!=0 happens when pExpr was originally
96 ** a TK_COLUMN but was previously evaluated and cached in a register */
97 const char *zColl;
98 int j = p->iColumn;
99 if( j>=0 ){
100 sqlite3 *db = pParse->db;
101 zColl = p->pTab->aCol[j].zColl;
102 pColl = sqlite3FindCollSeq(db, ENC(db), zColl, -1, 0);
103 pExpr->pColl = pColl;
104 }
105 break;
danielk19770202b292004-06-09 09:55:16 +0000106 }
drh7d10d5a2008-08-20 16:35:10 +0000107 if( op!=TK_CAST && op!=TK_UPLUS ){
108 break;
109 }
110 p = p->pLeft;
danielk19770202b292004-06-09 09:55:16 +0000111 }
danielk19777cedc8d2004-06-10 10:50:08 +0000112 if( sqlite3CheckCollSeq(pParse, pColl) ){
113 pColl = 0;
114 }
115 return pColl;
danielk19770202b292004-06-09 09:55:16 +0000116}
117
118/*
drh626a8792005-01-17 22:08:19 +0000119** pExpr is an operand of a comparison operator. aff2 is the
120** type affinity of the other operand. This routine returns the
drh53db1452004-05-20 13:54:53 +0000121** type affinity that should be used for the comparison operator.
122*/
danielk1977e014a832004-05-17 10:48:57 +0000123char sqlite3CompareAffinity(Expr *pExpr, char aff2){
danielk1977bf3b7212004-05-18 10:06:24 +0000124 char aff1 = sqlite3ExprAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +0000125 if( aff1 && aff2 ){
drh8df447f2005-11-01 15:48:24 +0000126 /* Both sides of the comparison are columns. If one has numeric
127 ** affinity, use that. Otherwise use no affinity.
danielk1977e014a832004-05-17 10:48:57 +0000128 */
drh8a512562005-11-14 22:29:05 +0000129 if( sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2) ){
danielk1977e014a832004-05-17 10:48:57 +0000130 return SQLITE_AFF_NUMERIC;
131 }else{
132 return SQLITE_AFF_NONE;
133 }
134 }else if( !aff1 && !aff2 ){
drh5f6a87b2004-07-19 00:39:45 +0000135 /* Neither side of the comparison is a column. Compare the
136 ** results directly.
danielk1977e014a832004-05-17 10:48:57 +0000137 */
drh5f6a87b2004-07-19 00:39:45 +0000138 return SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +0000139 }else{
140 /* One side is a column, the other is not. Use the columns affinity. */
drhfe05af82005-07-21 03:14:59 +0000141 assert( aff1==0 || aff2==0 );
danielk1977e014a832004-05-17 10:48:57 +0000142 return (aff1 + aff2);
143 }
144}
145
drh53db1452004-05-20 13:54:53 +0000146/*
147** pExpr is a comparison operator. Return the type affinity that should
148** be applied to both operands prior to doing the comparison.
149*/
danielk1977e014a832004-05-17 10:48:57 +0000150static char comparisonAffinity(Expr *pExpr){
151 char aff;
152 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
153 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
154 pExpr->op==TK_NE );
155 assert( pExpr->pLeft );
danielk1977bf3b7212004-05-18 10:06:24 +0000156 aff = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +0000157 if( pExpr->pRight ){
158 aff = sqlite3CompareAffinity(pExpr->pRight, aff);
159 }
160 else if( pExpr->pSelect ){
161 aff = sqlite3CompareAffinity(pExpr->pSelect->pEList->a[0].pExpr, aff);
162 }
163 else if( !aff ){
drhde087bd2007-02-23 03:00:44 +0000164 aff = SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +0000165 }
166 return aff;
167}
168
169/*
170** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
171** idx_affinity is the affinity of an indexed column. Return true
172** if the index with affinity idx_affinity may be used to implement
173** the comparison in pExpr.
174*/
175int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){
176 char aff = comparisonAffinity(pExpr);
drh8a512562005-11-14 22:29:05 +0000177 switch( aff ){
178 case SQLITE_AFF_NONE:
179 return 1;
180 case SQLITE_AFF_TEXT:
181 return idx_affinity==SQLITE_AFF_TEXT;
182 default:
183 return sqlite3IsNumericAffinity(idx_affinity);
184 }
danielk1977e014a832004-05-17 10:48:57 +0000185}
186
danielk1977a37cdde2004-05-16 11:15:36 +0000187/*
drh35573352008-01-08 23:54:25 +0000188** Return the P5 value that should be used for a binary comparison
danielk1977a37cdde2004-05-16 11:15:36 +0000189** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
danielk1977a37cdde2004-05-16 11:15:36 +0000190*/
drh35573352008-01-08 23:54:25 +0000191static u8 binaryCompareP5(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){
192 u8 aff = (char)sqlite3ExprAffinity(pExpr2);
drh1bd10f82008-12-10 21:19:56 +0000193 aff = (u8)sqlite3CompareAffinity(pExpr1, aff) | (u8)jumpIfNull;
drh35573352008-01-08 23:54:25 +0000194 return aff;
danielk1977a37cdde2004-05-16 11:15:36 +0000195}
196
drha2e00042002-01-22 03:13:42 +0000197/*
danielk19770202b292004-06-09 09:55:16 +0000198** Return a pointer to the collation sequence that should be used by
199** a binary comparison operator comparing pLeft and pRight.
200**
201** If the left hand expression has a collating sequence type, then it is
202** used. Otherwise the collation sequence for the right hand expression
203** is used, or the default (BINARY) if neither expression has a collating
204** type.
danielk1977bcbb04e2007-05-29 12:11:29 +0000205**
206** Argument pRight (but not pLeft) may be a null pointer. In this case,
207** it is not considered.
danielk19770202b292004-06-09 09:55:16 +0000208*/
drh0a0e1312007-08-07 17:04:59 +0000209CollSeq *sqlite3BinaryCompareCollSeq(
danielk1977bcbb04e2007-05-29 12:11:29 +0000210 Parse *pParse,
211 Expr *pLeft,
212 Expr *pRight
213){
drhec41dda2007-02-07 13:09:45 +0000214 CollSeq *pColl;
215 assert( pLeft );
drhec41dda2007-02-07 13:09:45 +0000216 if( pLeft->flags & EP_ExpCollate ){
217 assert( pLeft->pColl );
218 pColl = pLeft->pColl;
danielk1977bcbb04e2007-05-29 12:11:29 +0000219 }else if( pRight && pRight->flags & EP_ExpCollate ){
drhec41dda2007-02-07 13:09:45 +0000220 assert( pRight->pColl );
221 pColl = pRight->pColl;
222 }else{
223 pColl = sqlite3ExprCollSeq(pParse, pLeft);
224 if( !pColl ){
225 pColl = sqlite3ExprCollSeq(pParse, pRight);
226 }
danielk19770202b292004-06-09 09:55:16 +0000227 }
228 return pColl;
229}
230
231/*
drhda250ea2008-04-01 05:07:14 +0000232** Generate the operands for a comparison operation. Before
233** generating the code for each operand, set the EP_AnyAff
234** flag on the expression so that it will be able to used a
235** cached column value that has previously undergone an
236** affinity change.
237*/
238static void codeCompareOperands(
239 Parse *pParse, /* Parsing and code generating context */
240 Expr *pLeft, /* The left operand */
241 int *pRegLeft, /* Register where left operand is stored */
242 int *pFreeLeft, /* Free this register when done */
243 Expr *pRight, /* The right operand */
244 int *pRegRight, /* Register where right operand is stored */
245 int *pFreeRight /* Write temp register for right operand there */
246){
247 while( pLeft->op==TK_UPLUS ) pLeft = pLeft->pLeft;
248 pLeft->flags |= EP_AnyAff;
249 *pRegLeft = sqlite3ExprCodeTemp(pParse, pLeft, pFreeLeft);
250 while( pRight->op==TK_UPLUS ) pRight = pRight->pLeft;
251 pRight->flags |= EP_AnyAff;
252 *pRegRight = sqlite3ExprCodeTemp(pParse, pRight, pFreeRight);
253}
254
255/*
drhbe5c89a2004-07-26 00:31:09 +0000256** Generate code for a comparison operator.
257*/
258static int codeCompare(
259 Parse *pParse, /* The parsing (and code generating) context */
260 Expr *pLeft, /* The left operand */
261 Expr *pRight, /* The right operand */
262 int opcode, /* The comparison opcode */
drh35573352008-01-08 23:54:25 +0000263 int in1, int in2, /* Register holding operands */
drhbe5c89a2004-07-26 00:31:09 +0000264 int dest, /* Jump here if true. */
265 int jumpIfNull /* If true, jump if either operand is NULL */
266){
drh35573352008-01-08 23:54:25 +0000267 int p5;
268 int addr;
269 CollSeq *p4;
270
271 p4 = sqlite3BinaryCompareCollSeq(pParse, pLeft, pRight);
272 p5 = binaryCompareP5(pLeft, pRight, jumpIfNull);
273 addr = sqlite3VdbeAddOp4(pParse->pVdbe, opcode, in2, dest, in1,
274 (void*)p4, P4_COLLSEQ);
drh1bd10f82008-12-10 21:19:56 +0000275 sqlite3VdbeChangeP5(pParse->pVdbe, (u8)p5);
drhe49b1462008-07-09 01:39:44 +0000276 if( (p5 & SQLITE_AFF_MASK)!=SQLITE_AFF_NONE ){
drhda250ea2008-04-01 05:07:14 +0000277 sqlite3ExprCacheAffinityChange(pParse, in1, 1);
278 sqlite3ExprCacheAffinityChange(pParse, in2, 1);
drh2f7794c2008-04-01 03:27:39 +0000279 }
drh35573352008-01-08 23:54:25 +0000280 return addr;
drhbe5c89a2004-07-26 00:31:09 +0000281}
282
danielk19774b5255a2008-06-05 16:47:39 +0000283#if SQLITE_MAX_EXPR_DEPTH>0
284/*
285** Check that argument nHeight is less than or equal to the maximum
286** expression depth allowed. If it is not, leave an error message in
287** pParse.
288*/
drh7d10d5a2008-08-20 16:35:10 +0000289int sqlite3ExprCheckHeight(Parse *pParse, int nHeight){
danielk19774b5255a2008-06-05 16:47:39 +0000290 int rc = SQLITE_OK;
291 int mxHeight = pParse->db->aLimit[SQLITE_LIMIT_EXPR_DEPTH];
292 if( nHeight>mxHeight ){
293 sqlite3ErrorMsg(pParse,
294 "Expression tree is too large (maximum depth %d)", mxHeight
295 );
296 rc = SQLITE_ERROR;
297 }
298 return rc;
299}
300
301/* The following three functions, heightOfExpr(), heightOfExprList()
302** and heightOfSelect(), are used to determine the maximum height
303** of any expression tree referenced by the structure passed as the
304** first argument.
305**
306** If this maximum height is greater than the current value pointed
307** to by pnHeight, the second parameter, then set *pnHeight to that
308** value.
309*/
310static void heightOfExpr(Expr *p, int *pnHeight){
311 if( p ){
312 if( p->nHeight>*pnHeight ){
313 *pnHeight = p->nHeight;
314 }
315 }
316}
317static void heightOfExprList(ExprList *p, int *pnHeight){
318 if( p ){
319 int i;
320 for(i=0; i<p->nExpr; i++){
321 heightOfExpr(p->a[i].pExpr, pnHeight);
322 }
323 }
324}
325static void heightOfSelect(Select *p, int *pnHeight){
326 if( p ){
327 heightOfExpr(p->pWhere, pnHeight);
328 heightOfExpr(p->pHaving, pnHeight);
329 heightOfExpr(p->pLimit, pnHeight);
330 heightOfExpr(p->pOffset, pnHeight);
331 heightOfExprList(p->pEList, pnHeight);
332 heightOfExprList(p->pGroupBy, pnHeight);
333 heightOfExprList(p->pOrderBy, pnHeight);
334 heightOfSelect(p->pPrior, pnHeight);
335 }
336}
337
338/*
339** Set the Expr.nHeight variable in the structure passed as an
340** argument. An expression with no children, Expr.pList or
341** Expr.pSelect member has a height of 1. Any other expression
342** has a height equal to the maximum height of any other
343** referenced Expr plus one.
344*/
345static void exprSetHeight(Expr *p){
346 int nHeight = 0;
347 heightOfExpr(p->pLeft, &nHeight);
348 heightOfExpr(p->pRight, &nHeight);
349 heightOfExprList(p->pList, &nHeight);
350 heightOfSelect(p->pSelect, &nHeight);
351 p->nHeight = nHeight + 1;
352}
353
354/*
355** Set the Expr.nHeight variable using the exprSetHeight() function. If
356** the height is greater than the maximum allowed expression depth,
357** leave an error in pParse.
358*/
359void sqlite3ExprSetHeight(Parse *pParse, Expr *p){
360 exprSetHeight(p);
drh7d10d5a2008-08-20 16:35:10 +0000361 sqlite3ExprCheckHeight(pParse, p->nHeight);
danielk19774b5255a2008-06-05 16:47:39 +0000362}
363
364/*
365** Return the maximum height of any expression tree referenced
366** by the select statement passed as an argument.
367*/
368int sqlite3SelectExprHeight(Select *p){
369 int nHeight = 0;
370 heightOfSelect(p, &nHeight);
371 return nHeight;
372}
373#else
danielk19774b5255a2008-06-05 16:47:39 +0000374 #define exprSetHeight(y)
375#endif /* SQLITE_MAX_EXPR_DEPTH>0 */
376
drhbe5c89a2004-07-26 00:31:09 +0000377/*
drha76b5df2002-02-23 02:32:10 +0000378** Construct a new expression node and return a pointer to it. Memory
drh17435752007-08-16 04:30:38 +0000379** for this node is obtained from sqlite3_malloc(). The calling function
drha76b5df2002-02-23 02:32:10 +0000380** is responsible for making sure the node eventually gets freed.
381*/
drh17435752007-08-16 04:30:38 +0000382Expr *sqlite3Expr(
danielk1977a1644fd2007-08-29 12:31:25 +0000383 sqlite3 *db, /* Handle for sqlite3DbMallocZero() (may be null) */
drh17435752007-08-16 04:30:38 +0000384 int op, /* Expression opcode */
385 Expr *pLeft, /* Left operand */
386 Expr *pRight, /* Right operand */
387 const Token *pToken /* Argument token */
388){
drha76b5df2002-02-23 02:32:10 +0000389 Expr *pNew;
drh26e4a8b2008-05-01 17:16:52 +0000390 pNew = sqlite3DbMallocZero(db, sizeof(Expr));
drha76b5df2002-02-23 02:32:10 +0000391 if( pNew==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000392 /* When malloc fails, delete pLeft and pRight. Expressions passed to
393 ** this function must always be allocated with sqlite3Expr() for this
394 ** reason.
395 */
drh633e6d52008-07-28 19:34:53 +0000396 sqlite3ExprDelete(db, pLeft);
397 sqlite3ExprDelete(db, pRight);
drha76b5df2002-02-23 02:32:10 +0000398 return 0;
399 }
drh1bd10f82008-12-10 21:19:56 +0000400 pNew->op = (u8)op;
drha76b5df2002-02-23 02:32:10 +0000401 pNew->pLeft = pLeft;
402 pNew->pRight = pRight;
danielk1977a58fdfb2005-02-08 07:50:40 +0000403 pNew->iAgg = -1;
drhe49b1462008-07-09 01:39:44 +0000404 pNew->span.z = (u8*)"";
drha76b5df2002-02-23 02:32:10 +0000405 if( pToken ){
drh4b59ab52002-08-24 18:24:51 +0000406 assert( pToken->dyn==0 );
drh145716b2004-09-24 12:24:06 +0000407 pNew->span = pNew->token = *pToken;
drha34001c2007-02-02 12:44:37 +0000408 }else if( pLeft ){
409 if( pRight ){
drhe49b1462008-07-09 01:39:44 +0000410 if( pRight->span.dyn==0 && pLeft->span.dyn==0 ){
411 sqlite3ExprSpan(pNew, &pLeft->span, &pRight->span);
412 }
drh5ffb3ac2007-04-18 17:07:57 +0000413 if( pRight->flags & EP_ExpCollate ){
drha34001c2007-02-02 12:44:37 +0000414 pNew->flags |= EP_ExpCollate;
415 pNew->pColl = pRight->pColl;
416 }
417 }
drh5ffb3ac2007-04-18 17:07:57 +0000418 if( pLeft->flags & EP_ExpCollate ){
drha34001c2007-02-02 12:44:37 +0000419 pNew->flags |= EP_ExpCollate;
420 pNew->pColl = pLeft->pColl;
421 }
drha76b5df2002-02-23 02:32:10 +0000422 }
danielk1977fc976062007-05-10 10:46:56 +0000423
danielk19774b5255a2008-06-05 16:47:39 +0000424 exprSetHeight(pNew);
drha76b5df2002-02-23 02:32:10 +0000425 return pNew;
426}
427
428/*
drh17435752007-08-16 04:30:38 +0000429** Works like sqlite3Expr() except that it takes an extra Parse*
430** argument and notifies the associated connection object if malloc fails.
drh206f3d92006-07-11 13:15:08 +0000431*/
drh17435752007-08-16 04:30:38 +0000432Expr *sqlite3PExpr(
433 Parse *pParse, /* Parsing context */
434 int op, /* Expression opcode */
435 Expr *pLeft, /* Left operand */
436 Expr *pRight, /* Right operand */
437 const Token *pToken /* Argument token */
438){
danielk19774b5255a2008-06-05 16:47:39 +0000439 Expr *p = sqlite3Expr(pParse->db, op, pLeft, pRight, pToken);
440 if( p ){
drh7d10d5a2008-08-20 16:35:10 +0000441 sqlite3ExprCheckHeight(pParse, p->nHeight);
danielk19774b5255a2008-06-05 16:47:39 +0000442 }
443 return p;
drh206f3d92006-07-11 13:15:08 +0000444}
445
446/*
drh4e0cff62004-11-05 05:10:28 +0000447** When doing a nested parse, you can include terms in an expression
drhb7654112008-01-12 12:48:07 +0000448** that look like this: #1 #2 ... These terms refer to registers
449** in the virtual machine. #N is the N-th register.
drh4e0cff62004-11-05 05:10:28 +0000450**
451** This routine is called by the parser to deal with on of those terms.
452** It immediately generates code to store the value in a memory location.
453** The returns an expression that will code to extract the value from
454** that memory location as needed.
455*/
456Expr *sqlite3RegisterExpr(Parse *pParse, Token *pToken){
457 Vdbe *v = pParse->pVdbe;
458 Expr *p;
drh4e0cff62004-11-05 05:10:28 +0000459 if( pParse->nested==0 ){
460 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", pToken);
danielk1977a1644fd2007-08-29 12:31:25 +0000461 return sqlite3PExpr(pParse, TK_NULL, 0, 0, 0);
drh4e0cff62004-11-05 05:10:28 +0000462 }
drhbb7ac002005-08-12 22:58:53 +0000463 if( v==0 ) return 0;
danielk1977a1644fd2007-08-29 12:31:25 +0000464 p = sqlite3PExpr(pParse, TK_REGISTER, 0, 0, pToken);
drh73c42a12004-11-20 18:13:10 +0000465 if( p==0 ){
466 return 0; /* Malloc failed */
467 }
drhb7654112008-01-12 12:48:07 +0000468 p->iTable = atoi((char*)&pToken->z[1]);
drh4e0cff62004-11-05 05:10:28 +0000469 return p;
470}
471
472/*
drh91bb0ee2004-09-01 03:06:34 +0000473** Join two expressions using an AND operator. If either expression is
474** NULL, then just return the other expression.
475*/
danielk19771e536952007-08-16 10:09:01 +0000476Expr *sqlite3ExprAnd(sqlite3 *db, Expr *pLeft, Expr *pRight){
drh91bb0ee2004-09-01 03:06:34 +0000477 if( pLeft==0 ){
478 return pRight;
479 }else if( pRight==0 ){
480 return pLeft;
481 }else{
danielk1977880c15b2007-09-01 18:24:55 +0000482 return sqlite3Expr(db, TK_AND, pLeft, pRight, 0);
drh91bb0ee2004-09-01 03:06:34 +0000483 }
484}
485
486/*
drh6977fea2002-10-22 23:38:04 +0000487** Set the Expr.span field of the given expression to span all
drhe49b1462008-07-09 01:39:44 +0000488** text between the two given tokens. Both tokens must be pointing
489** at the same string.
drha76b5df2002-02-23 02:32:10 +0000490*/
danielk19774adee202004-05-08 08:23:19 +0000491void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
drh4efc4752004-01-16 15:55:37 +0000492 assert( pRight!=0 );
493 assert( pLeft!=0 );
drhe54a62a2008-07-18 17:03:52 +0000494 if( pExpr ){
drhe49b1462008-07-09 01:39:44 +0000495 pExpr->span.z = pLeft->z;
496 pExpr->span.n = pRight->n + (pRight->z - pLeft->z);
drha76b5df2002-02-23 02:32:10 +0000497 }
498}
499
500/*
501** Construct a new expression node for a function with multiple
502** arguments.
503*/
drh17435752007-08-16 04:30:38 +0000504Expr *sqlite3ExprFunction(Parse *pParse, ExprList *pList, Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000505 Expr *pNew;
drh633e6d52008-07-28 19:34:53 +0000506 sqlite3 *db = pParse->db;
danielk19774b202ae2006-01-23 05:50:58 +0000507 assert( pToken );
drh633e6d52008-07-28 19:34:53 +0000508 pNew = sqlite3DbMallocZero(db, sizeof(Expr) );
drha76b5df2002-02-23 02:32:10 +0000509 if( pNew==0 ){
drh633e6d52008-07-28 19:34:53 +0000510 sqlite3ExprListDelete(db, pList); /* Avoid leaking memory when malloc fails */
drha76b5df2002-02-23 02:32:10 +0000511 return 0;
512 }
513 pNew->op = TK_FUNCTION;
514 pNew->pList = pList;
danielk19774b202ae2006-01-23 05:50:58 +0000515 assert( pToken->dyn==0 );
516 pNew->token = *pToken;
drh6977fea2002-10-22 23:38:04 +0000517 pNew->span = pNew->token;
danielk1977fc976062007-05-10 10:46:56 +0000518
danielk19774b5255a2008-06-05 16:47:39 +0000519 sqlite3ExprSetHeight(pParse, pNew);
drha76b5df2002-02-23 02:32:10 +0000520 return pNew;
521}
522
523/*
drhfa6bc002004-09-07 16:19:52 +0000524** Assign a variable number to an expression that encodes a wildcard
525** in the original SQL statement.
526**
527** Wildcards consisting of a single "?" are assigned the next sequential
528** variable number.
529**
530** Wildcards of the form "?nnn" are assigned the number "nnn". We make
531** sure "nnn" is not too be to avoid a denial of service attack when
532** the SQL statement comes from an external source.
533**
534** Wildcards of the form ":aaa" or "$aaa" are assigned the same number
535** as the previous instance of the same wildcard. Or if this is the first
536** instance of the wildcard, the next sequenial variable number is
537** assigned.
538*/
539void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){
540 Token *pToken;
drh17435752007-08-16 04:30:38 +0000541 sqlite3 *db = pParse->db;
542
drhfa6bc002004-09-07 16:19:52 +0000543 if( pExpr==0 ) return;
544 pToken = &pExpr->token;
545 assert( pToken->n>=1 );
546 assert( pToken->z!=0 );
547 assert( pToken->z[0]!=0 );
548 if( pToken->n==1 ){
549 /* Wildcard of the form "?". Assign the next variable number */
550 pExpr->iTable = ++pParse->nVar;
551 }else if( pToken->z[0]=='?' ){
552 /* Wildcard of the form "?nnn". Convert "nnn" to an integer and
553 ** use it as the variable number */
554 int i;
drh2646da72005-12-09 20:02:05 +0000555 pExpr->iTable = i = atoi((char*)&pToken->z[1]);
drhc5499be2008-04-01 15:06:33 +0000556 testcase( i==0 );
557 testcase( i==1 );
558 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 );
559 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] );
drhbb4957f2008-03-20 14:03:29 +0000560 if( i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
drhfa6bc002004-09-07 16:19:52 +0000561 sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
drhbb4957f2008-03-20 14:03:29 +0000562 db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]);
drhfa6bc002004-09-07 16:19:52 +0000563 }
564 if( i>pParse->nVar ){
565 pParse->nVar = i;
566 }
567 }else{
568 /* Wildcards of the form ":aaa" or "$aaa". Reuse the same variable
569 ** number as the prior appearance of the same name, or if the name
570 ** has never appeared before, reuse the same variable number
571 */
drh1bd10f82008-12-10 21:19:56 +0000572 int i;
573 u32 n;
drhfa6bc002004-09-07 16:19:52 +0000574 n = pToken->n;
575 for(i=0; i<pParse->nVarExpr; i++){
576 Expr *pE;
577 if( (pE = pParse->apVarExpr[i])!=0
578 && pE->token.n==n
579 && memcmp(pE->token.z, pToken->z, n)==0 ){
580 pExpr->iTable = pE->iTable;
581 break;
582 }
583 }
584 if( i>=pParse->nVarExpr ){
585 pExpr->iTable = ++pParse->nVar;
586 if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){
587 pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10;
drh17435752007-08-16 04:30:38 +0000588 pParse->apVarExpr =
589 sqlite3DbReallocOrFree(
590 db,
591 pParse->apVarExpr,
592 pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0])
593 );
drhfa6bc002004-09-07 16:19:52 +0000594 }
drh17435752007-08-16 04:30:38 +0000595 if( !db->mallocFailed ){
drhfa6bc002004-09-07 16:19:52 +0000596 assert( pParse->apVarExpr!=0 );
597 pParse->apVarExpr[pParse->nVarExpr++] = pExpr;
598 }
599 }
600 }
drhbb4957f2008-03-20 14:03:29 +0000601 if( !pParse->nErr && pParse->nVar>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
danielk1977832b2662007-05-09 11:37:22 +0000602 sqlite3ErrorMsg(pParse, "too many SQL variables");
603 }
drhfa6bc002004-09-07 16:19:52 +0000604}
605
606/*
drh10fe8402008-10-11 16:47:35 +0000607** Clear an expression structure without deleting the structure itself.
608** Substructure is deleted.
drha2e00042002-01-22 03:13:42 +0000609*/
drh10fe8402008-10-11 16:47:35 +0000610void sqlite3ExprClear(sqlite3 *db, Expr *p){
drh633e6d52008-07-28 19:34:53 +0000611 if( p->span.dyn ) sqlite3DbFree(db, (char*)p->span.z);
612 if( p->token.dyn ) sqlite3DbFree(db, (char*)p->token.z);
613 sqlite3ExprDelete(db, p->pLeft);
614 sqlite3ExprDelete(db, p->pRight);
615 sqlite3ExprListDelete(db, p->pList);
616 sqlite3SelectDelete(db, p->pSelect);
drh10fe8402008-10-11 16:47:35 +0000617}
618
619/*
620** Recursively delete an expression tree.
621*/
622void sqlite3ExprDelete(sqlite3 *db, Expr *p){
623 if( p==0 ) return;
624 sqlite3ExprClear(db, p);
drh633e6d52008-07-28 19:34:53 +0000625 sqlite3DbFree(db, p);
drha2e00042002-01-22 03:13:42 +0000626}
627
drhd2687b72005-08-12 22:56:09 +0000628/*
629** The Expr.token field might be a string literal that is quoted.
630** If so, remove the quotation marks.
631*/
drh17435752007-08-16 04:30:38 +0000632void sqlite3DequoteExpr(sqlite3 *db, Expr *p){
drhd2687b72005-08-12 22:56:09 +0000633 if( ExprHasAnyProperty(p, EP_Dequoted) ){
634 return;
635 }
636 ExprSetProperty(p, EP_Dequoted);
637 if( p->token.dyn==0 ){
drh17435752007-08-16 04:30:38 +0000638 sqlite3TokenCopy(db, &p->token, &p->token);
drhd2687b72005-08-12 22:56:09 +0000639 }
640 sqlite3Dequote((char*)p->token.z);
641}
642
drha76b5df2002-02-23 02:32:10 +0000643/*
drhff78bd22002-02-27 01:47:11 +0000644** The following group of routines make deep copies of expressions,
645** expression lists, ID lists, and select statements. The copies can
646** be deleted (by being passed to their respective ...Delete() routines)
647** without effecting the originals.
648**
danielk19774adee202004-05-08 08:23:19 +0000649** The expression list, ID, and source lists return by sqlite3ExprListDup(),
650** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
drhad3cab52002-05-24 02:04:32 +0000651** by subsequent calls to sqlite*ListAppend() routines.
drhff78bd22002-02-27 01:47:11 +0000652**
drhad3cab52002-05-24 02:04:32 +0000653** Any tables that the SrcList might point to are not duplicated.
drhff78bd22002-02-27 01:47:11 +0000654*/
danielk19771e536952007-08-16 10:09:01 +0000655Expr *sqlite3ExprDup(sqlite3 *db, Expr *p){
drhff78bd22002-02-27 01:47:11 +0000656 Expr *pNew;
657 if( p==0 ) return 0;
drh17435752007-08-16 04:30:38 +0000658 pNew = sqlite3DbMallocRaw(db, sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000659 if( pNew==0 ) return 0;
drh3b167c72002-06-28 12:18:47 +0000660 memcpy(pNew, p, sizeof(*pNew));
drh6977fea2002-10-22 23:38:04 +0000661 if( p->token.z!=0 ){
drh17435752007-08-16 04:30:38 +0000662 pNew->token.z = (u8*)sqlite3DbStrNDup(db, (char*)p->token.z, p->token.n);
drh4b59ab52002-08-24 18:24:51 +0000663 pNew->token.dyn = 1;
664 }else{
drh4efc4752004-01-16 15:55:37 +0000665 assert( pNew->token.z==0 );
drh4b59ab52002-08-24 18:24:51 +0000666 }
drh6977fea2002-10-22 23:38:04 +0000667 pNew->span.z = 0;
drh17435752007-08-16 04:30:38 +0000668 pNew->pLeft = sqlite3ExprDup(db, p->pLeft);
669 pNew->pRight = sqlite3ExprDup(db, p->pRight);
670 pNew->pList = sqlite3ExprListDup(db, p->pList);
671 pNew->pSelect = sqlite3SelectDup(db, p->pSelect);
drhff78bd22002-02-27 01:47:11 +0000672 return pNew;
673}
drh17435752007-08-16 04:30:38 +0000674void sqlite3TokenCopy(sqlite3 *db, Token *pTo, Token *pFrom){
drh633e6d52008-07-28 19:34:53 +0000675 if( pTo->dyn ) sqlite3DbFree(db, (char*)pTo->z);
drh4b59ab52002-08-24 18:24:51 +0000676 if( pFrom->z ){
677 pTo->n = pFrom->n;
drh17435752007-08-16 04:30:38 +0000678 pTo->z = (u8*)sqlite3DbStrNDup(db, (char*)pFrom->z, pFrom->n);
drh4b59ab52002-08-24 18:24:51 +0000679 pTo->dyn = 1;
680 }else{
drh4b59ab52002-08-24 18:24:51 +0000681 pTo->z = 0;
drh4b59ab52002-08-24 18:24:51 +0000682 }
683}
drh17435752007-08-16 04:30:38 +0000684ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p){
drhff78bd22002-02-27 01:47:11 +0000685 ExprList *pNew;
drh145716b2004-09-24 12:24:06 +0000686 struct ExprList_item *pItem, *pOldItem;
drhff78bd22002-02-27 01:47:11 +0000687 int i;
688 if( p==0 ) return 0;
drh17435752007-08-16 04:30:38 +0000689 pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +0000690 if( pNew==0 ) return 0;
danielk197731dad9d2007-08-16 11:36:15 +0000691 pNew->iECursor = 0;
drh4305d102003-07-30 12:34:12 +0000692 pNew->nExpr = pNew->nAlloc = p->nExpr;
drh17435752007-08-16 04:30:38 +0000693 pNew->a = pItem = sqlite3DbMallocRaw(db, p->nExpr*sizeof(p->a[0]) );
danielk1977e0048402004-06-15 16:51:01 +0000694 if( pItem==0 ){
drh633e6d52008-07-28 19:34:53 +0000695 sqlite3DbFree(db, pNew);
danielk1977e0048402004-06-15 16:51:01 +0000696 return 0;
697 }
drh145716b2004-09-24 12:24:06 +0000698 pOldItem = p->a;
699 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
drh4b59ab52002-08-24 18:24:51 +0000700 Expr *pNewExpr, *pOldExpr;
drh17435752007-08-16 04:30:38 +0000701 pItem->pExpr = pNewExpr = sqlite3ExprDup(db, pOldExpr = pOldItem->pExpr);
drh6977fea2002-10-22 23:38:04 +0000702 if( pOldExpr->span.z!=0 && pNewExpr ){
703 /* Always make a copy of the span for top-level expressions in the
drh4b59ab52002-08-24 18:24:51 +0000704 ** expression list. The logic in SELECT processing that determines
705 ** the names of columns in the result set needs this information */
drh17435752007-08-16 04:30:38 +0000706 sqlite3TokenCopy(db, &pNewExpr->span, &pOldExpr->span);
drh4b59ab52002-08-24 18:24:51 +0000707 }
drh1f3e9052002-10-31 00:09:39 +0000708 assert( pNewExpr==0 || pNewExpr->span.z!=0
drh6f7adc82006-01-11 21:41:20 +0000709 || pOldExpr->span.z==0
drh17435752007-08-16 04:30:38 +0000710 || db->mallocFailed );
711 pItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
drh145716b2004-09-24 12:24:06 +0000712 pItem->sortOrder = pOldItem->sortOrder;
drh3e7bc9c2004-02-21 19:17:17 +0000713 pItem->done = 0;
drh7d10d5a2008-08-20 16:35:10 +0000714 pItem->iCol = pOldItem->iCol;
drh8b213892008-08-29 02:14:02 +0000715 pItem->iAlias = pOldItem->iAlias;
drhff78bd22002-02-27 01:47:11 +0000716 }
717 return pNew;
718}
danielk197793758c82005-01-21 08:13:14 +0000719
720/*
721** If cursors, triggers, views and subqueries are all omitted from
722** the build, then none of the following routines, except for
723** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes
724** called with a NULL argument.
725*/
danielk19776a67fe82005-02-04 04:07:16 +0000726#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \
727 || !defined(SQLITE_OMIT_SUBQUERY)
drh17435752007-08-16 04:30:38 +0000728SrcList *sqlite3SrcListDup(sqlite3 *db, SrcList *p){
drhad3cab52002-05-24 02:04:32 +0000729 SrcList *pNew;
730 int i;
drh113088e2003-03-20 01:16:58 +0000731 int nByte;
drhad3cab52002-05-24 02:04:32 +0000732 if( p==0 ) return 0;
drh113088e2003-03-20 01:16:58 +0000733 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
drh17435752007-08-16 04:30:38 +0000734 pNew = sqlite3DbMallocRaw(db, nByte );
drhad3cab52002-05-24 02:04:32 +0000735 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000736 pNew->nSrc = pNew->nAlloc = p->nSrc;
drhad3cab52002-05-24 02:04:32 +0000737 for(i=0; i<p->nSrc; i++){
drh4efc4752004-01-16 15:55:37 +0000738 struct SrcList_item *pNewItem = &pNew->a[i];
739 struct SrcList_item *pOldItem = &p->a[i];
drhed8a3bb2005-06-06 21:19:56 +0000740 Table *pTab;
drh17435752007-08-16 04:30:38 +0000741 pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase);
742 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
743 pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias);
drh4efc4752004-01-16 15:55:37 +0000744 pNewItem->jointype = pOldItem->jointype;
745 pNewItem->iCursor = pOldItem->iCursor;
danielk19771787cca2006-02-10 07:07:14 +0000746 pNewItem->isPopulated = pOldItem->isPopulated;
danielk197785574e32008-10-06 05:32:18 +0000747 pNewItem->zIndex = sqlite3DbStrDup(db, pOldItem->zIndex);
748 pNewItem->notIndexed = pOldItem->notIndexed;
749 pNewItem->pIndex = pOldItem->pIndex;
drhed8a3bb2005-06-06 21:19:56 +0000750 pTab = pNewItem->pTab = pOldItem->pTab;
751 if( pTab ){
752 pTab->nRef++;
danielk1977a1cb1832005-02-12 08:59:55 +0000753 }
drh17435752007-08-16 04:30:38 +0000754 pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect);
755 pNewItem->pOn = sqlite3ExprDup(db, pOldItem->pOn);
756 pNewItem->pUsing = sqlite3IdListDup(db, pOldItem->pUsing);
danielk19776c18b6e2005-01-30 09:17:58 +0000757 pNewItem->colUsed = pOldItem->colUsed;
drhad3cab52002-05-24 02:04:32 +0000758 }
759 return pNew;
760}
drh17435752007-08-16 04:30:38 +0000761IdList *sqlite3IdListDup(sqlite3 *db, IdList *p){
drhff78bd22002-02-27 01:47:11 +0000762 IdList *pNew;
763 int i;
764 if( p==0 ) return 0;
drh17435752007-08-16 04:30:38 +0000765 pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +0000766 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000767 pNew->nId = pNew->nAlloc = p->nId;
drh17435752007-08-16 04:30:38 +0000768 pNew->a = sqlite3DbMallocRaw(db, p->nId*sizeof(p->a[0]) );
danielk1977d5d56522005-03-16 12:15:20 +0000769 if( pNew->a==0 ){
drh633e6d52008-07-28 19:34:53 +0000770 sqlite3DbFree(db, pNew);
danielk1977d5d56522005-03-16 12:15:20 +0000771 return 0;
772 }
drhff78bd22002-02-27 01:47:11 +0000773 for(i=0; i<p->nId; i++){
drh4efc4752004-01-16 15:55:37 +0000774 struct IdList_item *pNewItem = &pNew->a[i];
775 struct IdList_item *pOldItem = &p->a[i];
drh17435752007-08-16 04:30:38 +0000776 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
drh4efc4752004-01-16 15:55:37 +0000777 pNewItem->idx = pOldItem->idx;
drhff78bd22002-02-27 01:47:11 +0000778 }
779 return pNew;
780}
drh17435752007-08-16 04:30:38 +0000781Select *sqlite3SelectDup(sqlite3 *db, Select *p){
drhff78bd22002-02-27 01:47:11 +0000782 Select *pNew;
783 if( p==0 ) return 0;
drh17435752007-08-16 04:30:38 +0000784 pNew = sqlite3DbMallocRaw(db, sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000785 if( pNew==0 ) return 0;
drh17435752007-08-16 04:30:38 +0000786 pNew->pEList = sqlite3ExprListDup(db, p->pEList);
787 pNew->pSrc = sqlite3SrcListDup(db, p->pSrc);
788 pNew->pWhere = sqlite3ExprDup(db, p->pWhere);
789 pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy);
790 pNew->pHaving = sqlite3ExprDup(db, p->pHaving);
791 pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy);
drhff78bd22002-02-27 01:47:11 +0000792 pNew->op = p->op;
drh17435752007-08-16 04:30:38 +0000793 pNew->pPrior = sqlite3SelectDup(db, p->pPrior);
794 pNew->pLimit = sqlite3ExprDup(db, p->pLimit);
795 pNew->pOffset = sqlite3ExprDup(db, p->pOffset);
drh92b01d52008-06-24 00:32:35 +0000796 pNew->iLimit = 0;
797 pNew->iOffset = 0;
drh7d10d5a2008-08-20 16:35:10 +0000798 pNew->selFlags = p->selFlags & ~SF_UsesEphemeral;
drh0342b1f2005-09-01 03:07:44 +0000799 pNew->pRightmost = 0;
drhb9bb7c12006-06-11 23:41:55 +0000800 pNew->addrOpenEphm[0] = -1;
801 pNew->addrOpenEphm[1] = -1;
802 pNew->addrOpenEphm[2] = -1;
drhff78bd22002-02-27 01:47:11 +0000803 return pNew;
804}
danielk197793758c82005-01-21 08:13:14 +0000805#else
drh17435752007-08-16 04:30:38 +0000806Select *sqlite3SelectDup(sqlite3 *db, Select *p){
danielk197793758c82005-01-21 08:13:14 +0000807 assert( p==0 );
808 return 0;
809}
810#endif
drhff78bd22002-02-27 01:47:11 +0000811
812
813/*
drha76b5df2002-02-23 02:32:10 +0000814** Add a new element to the end of an expression list. If pList is
815** initially NULL, then create a new expression list.
816*/
drh17435752007-08-16 04:30:38 +0000817ExprList *sqlite3ExprListAppend(
818 Parse *pParse, /* Parsing context */
819 ExprList *pList, /* List to which to append. Might be NULL */
820 Expr *pExpr, /* Expression to be appended */
821 Token *pName /* AS keyword for the expression */
822){
823 sqlite3 *db = pParse->db;
drha76b5df2002-02-23 02:32:10 +0000824 if( pList==0 ){
drh17435752007-08-16 04:30:38 +0000825 pList = sqlite3DbMallocZero(db, sizeof(ExprList) );
drha76b5df2002-02-23 02:32:10 +0000826 if( pList==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000827 goto no_mem;
drha76b5df2002-02-23 02:32:10 +0000828 }
drh4efc4752004-01-16 15:55:37 +0000829 assert( pList->nAlloc==0 );
drha76b5df2002-02-23 02:32:10 +0000830 }
drh4305d102003-07-30 12:34:12 +0000831 if( pList->nAlloc<=pList->nExpr ){
danielk1977d5d56522005-03-16 12:15:20 +0000832 struct ExprList_item *a;
833 int n = pList->nAlloc*2 + 4;
danielk197726783a52007-08-29 14:06:22 +0000834 a = sqlite3DbRealloc(db, pList->a, n*sizeof(pList->a[0]));
danielk1977d5d56522005-03-16 12:15:20 +0000835 if( a==0 ){
836 goto no_mem;
drha76b5df2002-02-23 02:32:10 +0000837 }
danielk1977d5d56522005-03-16 12:15:20 +0000838 pList->a = a;
drh6a1e0712008-12-05 15:24:15 +0000839 pList->nAlloc = sqlite3DbMallocSize(db, a)/sizeof(a[0]);
drha76b5df2002-02-23 02:32:10 +0000840 }
drh4efc4752004-01-16 15:55:37 +0000841 assert( pList->a!=0 );
842 if( pExpr || pName ){
843 struct ExprList_item *pItem = &pList->a[pList->nExpr++];
844 memset(pItem, 0, sizeof(*pItem));
drh17435752007-08-16 04:30:38 +0000845 pItem->zName = sqlite3NameFromToken(db, pName);
danielk1977e94ddc92005-03-21 03:53:38 +0000846 pItem->pExpr = pExpr;
drh8b213892008-08-29 02:14:02 +0000847 pItem->iAlias = 0;
drha76b5df2002-02-23 02:32:10 +0000848 }
849 return pList;
danielk1977d5d56522005-03-16 12:15:20 +0000850
851no_mem:
852 /* Avoid leaking memory if malloc has failed. */
drh633e6d52008-07-28 19:34:53 +0000853 sqlite3ExprDelete(db, pExpr);
854 sqlite3ExprListDelete(db, pList);
danielk1977d5d56522005-03-16 12:15:20 +0000855 return 0;
drha76b5df2002-02-23 02:32:10 +0000856}
857
858/*
danielk19777a15a4b2007-05-08 17:54:43 +0000859** If the expression list pEList contains more than iLimit elements,
860** leave an error message in pParse.
861*/
862void sqlite3ExprListCheckLength(
863 Parse *pParse,
864 ExprList *pEList,
danielk19777a15a4b2007-05-08 17:54:43 +0000865 const char *zObject
866){
drhb1a6c3c2008-03-20 16:30:17 +0000867 int mx = pParse->db->aLimit[SQLITE_LIMIT_COLUMN];
drhc5499be2008-04-01 15:06:33 +0000868 testcase( pEList && pEList->nExpr==mx );
869 testcase( pEList && pEList->nExpr==mx+1 );
drhb1a6c3c2008-03-20 16:30:17 +0000870 if( pEList && pEList->nExpr>mx ){
danielk19777a15a4b2007-05-08 17:54:43 +0000871 sqlite3ErrorMsg(pParse, "too many columns in %s", zObject);
872 }
873}
874
875/*
drha76b5df2002-02-23 02:32:10 +0000876** Delete an entire expression list.
877*/
drh633e6d52008-07-28 19:34:53 +0000878void sqlite3ExprListDelete(sqlite3 *db, ExprList *pList){
drha76b5df2002-02-23 02:32:10 +0000879 int i;
drhbe5c89a2004-07-26 00:31:09 +0000880 struct ExprList_item *pItem;
drha76b5df2002-02-23 02:32:10 +0000881 if( pList==0 ) return;
drh1bdd9b52004-04-23 17:04:44 +0000882 assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
883 assert( pList->nExpr<=pList->nAlloc );
drhbe5c89a2004-07-26 00:31:09 +0000884 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
drh633e6d52008-07-28 19:34:53 +0000885 sqlite3ExprDelete(db, pItem->pExpr);
886 sqlite3DbFree(db, pItem->zName);
drha76b5df2002-02-23 02:32:10 +0000887 }
drh633e6d52008-07-28 19:34:53 +0000888 sqlite3DbFree(db, pList->a);
889 sqlite3DbFree(db, pList);
drha76b5df2002-02-23 02:32:10 +0000890}
891
892/*
drh7d10d5a2008-08-20 16:35:10 +0000893** These routines are Walker callbacks. Walker.u.pi is a pointer
894** to an integer. These routines are checking an expression to see
895** if it is a constant. Set *Walker.u.pi to 0 if the expression is
896** not constant.
drh73b211a2005-01-18 04:00:42 +0000897**
drh7d10d5a2008-08-20 16:35:10 +0000898** These callback routines are used to implement the following:
drh626a8792005-01-17 22:08:19 +0000899**
drh7d10d5a2008-08-20 16:35:10 +0000900** sqlite3ExprIsConstant()
901** sqlite3ExprIsConstantNotJoin()
902** sqlite3ExprIsConstantOrFunction()
drh87abf5c2005-08-25 12:45:04 +0000903**
drh626a8792005-01-17 22:08:19 +0000904*/
drh7d10d5a2008-08-20 16:35:10 +0000905static int exprNodeIsConstant(Walker *pWalker, Expr *pExpr){
drh626a8792005-01-17 22:08:19 +0000906
drh7d10d5a2008-08-20 16:35:10 +0000907 /* If pWalker->u.i is 3 then any term of the expression that comes from
drh0a168372007-06-08 00:20:47 +0000908 ** the ON or USING clauses of a join disqualifies the expression
909 ** from being considered constant. */
drh7d10d5a2008-08-20 16:35:10 +0000910 if( pWalker->u.i==3 && ExprHasAnyProperty(pExpr, EP_FromJoin) ){
911 pWalker->u.i = 0;
912 return WRC_Abort;
drh0a168372007-06-08 00:20:47 +0000913 }
914
drh626a8792005-01-17 22:08:19 +0000915 switch( pExpr->op ){
drheb55bd22005-06-30 17:04:21 +0000916 /* Consider functions to be constant if all their arguments are constant
drh7d10d5a2008-08-20 16:35:10 +0000917 ** and pWalker->u.i==2 */
drheb55bd22005-06-30 17:04:21 +0000918 case TK_FUNCTION:
drh7d10d5a2008-08-20 16:35:10 +0000919 if( pWalker->u.i==2 ) return 0;
drheb55bd22005-06-30 17:04:21 +0000920 /* Fall through */
drh626a8792005-01-17 22:08:19 +0000921 case TK_ID:
922 case TK_COLUMN:
drh626a8792005-01-17 22:08:19 +0000923 case TK_AGG_FUNCTION:
drh13449892005-09-07 21:22:45 +0000924 case TK_AGG_COLUMN:
drhfe2093d2005-01-20 22:48:47 +0000925#ifndef SQLITE_OMIT_SUBQUERY
926 case TK_SELECT:
927 case TK_EXISTS:
drhc5499be2008-04-01 15:06:33 +0000928 testcase( pExpr->op==TK_SELECT );
929 testcase( pExpr->op==TK_EXISTS );
drhfe2093d2005-01-20 22:48:47 +0000930#endif
drhc5499be2008-04-01 15:06:33 +0000931 testcase( pExpr->op==TK_ID );
932 testcase( pExpr->op==TK_COLUMN );
drhc5499be2008-04-01 15:06:33 +0000933 testcase( pExpr->op==TK_AGG_FUNCTION );
934 testcase( pExpr->op==TK_AGG_COLUMN );
drh7d10d5a2008-08-20 16:35:10 +0000935 pWalker->u.i = 0;
936 return WRC_Abort;
drh626a8792005-01-17 22:08:19 +0000937 default:
drh7d10d5a2008-08-20 16:35:10 +0000938 return WRC_Continue;
drh626a8792005-01-17 22:08:19 +0000939 }
940}
danielk197762c14b32008-11-19 09:05:26 +0000941static int selectNodeIsConstant(Walker *pWalker, Select *NotUsed){
942 UNUSED_PARAMETER(NotUsed);
drh7d10d5a2008-08-20 16:35:10 +0000943 pWalker->u.i = 0;
944 return WRC_Abort;
945}
946static int exprIsConst(Expr *p, int initFlag){
947 Walker w;
948 w.u.i = initFlag;
949 w.xExprCallback = exprNodeIsConstant;
950 w.xSelectCallback = selectNodeIsConstant;
951 sqlite3WalkExpr(&w, p);
952 return w.u.i;
953}
drh626a8792005-01-17 22:08:19 +0000954
955/*
drhfef52082000-06-06 01:50:43 +0000956** Walk an expression tree. Return 1 if the expression is constant
drheb55bd22005-06-30 17:04:21 +0000957** and 0 if it involves variables or function calls.
drh23989372002-05-21 13:43:04 +0000958**
959** For the purposes of this function, a double-quoted string (ex: "abc")
960** is considered a variable but a single-quoted string (ex: 'abc') is
961** a constant.
drhfef52082000-06-06 01:50:43 +0000962*/
danielk19774adee202004-05-08 08:23:19 +0000963int sqlite3ExprIsConstant(Expr *p){
drh7d10d5a2008-08-20 16:35:10 +0000964 return exprIsConst(p, 1);
drhfef52082000-06-06 01:50:43 +0000965}
966
967/*
drheb55bd22005-06-30 17:04:21 +0000968** Walk an expression tree. Return 1 if the expression is constant
drh0a168372007-06-08 00:20:47 +0000969** that does no originate from the ON or USING clauses of a join.
970** Return 0 if it involves variables or function calls or terms from
971** an ON or USING clause.
972*/
973int sqlite3ExprIsConstantNotJoin(Expr *p){
drh7d10d5a2008-08-20 16:35:10 +0000974 return exprIsConst(p, 3);
drh0a168372007-06-08 00:20:47 +0000975}
976
977/*
978** Walk an expression tree. Return 1 if the expression is constant
drheb55bd22005-06-30 17:04:21 +0000979** or a function call with constant arguments. Return and 0 if there
980** are any variables.
981**
982** For the purposes of this function, a double-quoted string (ex: "abc")
983** is considered a variable but a single-quoted string (ex: 'abc') is
984** a constant.
985*/
986int sqlite3ExprIsConstantOrFunction(Expr *p){
drh7d10d5a2008-08-20 16:35:10 +0000987 return exprIsConst(p, 2);
drheb55bd22005-06-30 17:04:21 +0000988}
989
990/*
drh73b211a2005-01-18 04:00:42 +0000991** If the expression p codes a constant integer that is small enough
drh202b2df2004-01-06 01:13:46 +0000992** to fit in a 32-bit integer, return 1 and put the value of the integer
993** in *pValue. If the expression is not an integer or if it is too big
994** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
drhe4de1fe2002-06-02 16:09:01 +0000995*/
danielk19774adee202004-05-08 08:23:19 +0000996int sqlite3ExprIsInteger(Expr *p, int *pValue){
drh92b01d52008-06-24 00:32:35 +0000997 int rc = 0;
998 if( p->flags & EP_IntValue ){
999 *pValue = p->iTable;
1000 return 1;
1001 }
drhe4de1fe2002-06-02 16:09:01 +00001002 switch( p->op ){
1003 case TK_INTEGER: {
drh92b01d52008-06-24 00:32:35 +00001004 rc = sqlite3GetInt32((char*)p->token.z, pValue);
drh202b2df2004-01-06 01:13:46 +00001005 break;
drhe4de1fe2002-06-02 16:09:01 +00001006 }
drh4b59ab52002-08-24 18:24:51 +00001007 case TK_UPLUS: {
drh92b01d52008-06-24 00:32:35 +00001008 rc = sqlite3ExprIsInteger(p->pLeft, pValue);
drhf6e369a2008-06-24 12:46:30 +00001009 break;
drh4b59ab52002-08-24 18:24:51 +00001010 }
drhe4de1fe2002-06-02 16:09:01 +00001011 case TK_UMINUS: {
1012 int v;
danielk19774adee202004-05-08 08:23:19 +00001013 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
drhe4de1fe2002-06-02 16:09:01 +00001014 *pValue = -v;
drh92b01d52008-06-24 00:32:35 +00001015 rc = 1;
drhe4de1fe2002-06-02 16:09:01 +00001016 }
1017 break;
1018 }
1019 default: break;
1020 }
drh92b01d52008-06-24 00:32:35 +00001021 if( rc ){
1022 p->op = TK_INTEGER;
1023 p->flags |= EP_IntValue;
1024 p->iTable = *pValue;
1025 }
1026 return rc;
drhe4de1fe2002-06-02 16:09:01 +00001027}
1028
1029/*
drhc4a3c772001-04-04 11:48:57 +00001030** Return TRUE if the given string is a row-id column name.
1031*/
danielk19774adee202004-05-08 08:23:19 +00001032int sqlite3IsRowid(const char *z){
1033 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
1034 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
1035 if( sqlite3StrICmp(z, "OID")==0 ) return 1;
drhc4a3c772001-04-04 11:48:57 +00001036 return 0;
1037}
1038
danielk19779a96b662007-11-29 17:05:18 +00001039/*
drhb287f4b2008-04-25 00:08:38 +00001040** Return true if the IN operator optimization is enabled and
1041** the SELECT statement p exists and is of the
1042** simple form:
1043**
1044** SELECT <column> FROM <table>
1045**
1046** If this is the case, it may be possible to use an existing table
1047** or index instead of generating an epheremal table.
1048*/
1049#ifndef SQLITE_OMIT_SUBQUERY
1050static int isCandidateForInOpt(Select *p){
1051 SrcList *pSrc;
1052 ExprList *pEList;
1053 Table *pTab;
drhb287f4b2008-04-25 00:08:38 +00001054 if( p==0 ) return 0; /* right-hand side of IN is SELECT */
1055 if( p->pPrior ) return 0; /* Not a compound SELECT */
drh7d10d5a2008-08-20 16:35:10 +00001056 if( p->selFlags & (SF_Distinct|SF_Aggregate) ){
1057 return 0; /* No DISTINCT keyword and no aggregate functions */
1058 }
drhb287f4b2008-04-25 00:08:38 +00001059 if( p->pGroupBy ) return 0; /* Has no GROUP BY clause */
1060 if( p->pLimit ) return 0; /* Has no LIMIT clause */
1061 if( p->pOffset ) return 0;
1062 if( p->pWhere ) return 0; /* Has no WHERE clause */
1063 pSrc = p->pSrc;
drhd1fa7bc2009-01-10 13:24:50 +00001064 assert( pSrc!=0 );
1065 if( pSrc->nSrc!=1 ) return 0; /* Single term in FROM clause */
drhb287f4b2008-04-25 00:08:38 +00001066 if( pSrc->a[0].pSelect ) return 0; /* FROM clause is not a subquery */
1067 pTab = pSrc->a[0].pTab;
1068 if( pTab==0 ) return 0;
1069 if( pTab->pSelect ) return 0; /* FROM clause is not a view */
1070 if( IsVirtual(pTab) ) return 0; /* FROM clause not a virtual table */
1071 pEList = p->pEList;
1072 if( pEList->nExpr!=1 ) return 0; /* One column in the result set */
1073 if( pEList->a[0].pExpr->op!=TK_COLUMN ) return 0; /* Result is a column */
1074 return 1;
1075}
1076#endif /* SQLITE_OMIT_SUBQUERY */
1077
1078/*
danielk19779a96b662007-11-29 17:05:18 +00001079** This function is used by the implementation of the IN (...) operator.
1080** It's job is to find or create a b-tree structure that may be used
1081** either to test for membership of the (...) set or to iterate through
drh85b623f2007-12-13 21:54:09 +00001082** its members, skipping duplicates.
danielk19779a96b662007-11-29 17:05:18 +00001083**
1084** The cursor opened on the structure (database table, database index
1085** or ephermal table) is stored in pX->iTable before this function returns.
1086** The returned value indicates the structure type, as follows:
1087**
1088** IN_INDEX_ROWID - The cursor was opened on a database table.
drh2d401ab2008-01-10 23:50:11 +00001089** IN_INDEX_INDEX - The cursor was opened on a database index.
danielk19779a96b662007-11-29 17:05:18 +00001090** IN_INDEX_EPH - The cursor was opened on a specially created and
1091** populated epheremal table.
1092**
1093** An existing structure may only be used if the SELECT is of the simple
1094** form:
1095**
1096** SELECT <column> FROM <table>
1097**
danielk19770cdc0222008-06-26 18:04:03 +00001098** If prNotFound parameter is 0, then the structure will be used to iterate
danielk19779a96b662007-11-29 17:05:18 +00001099** through the set members, skipping any duplicates. In this case an
1100** epheremal table must be used unless the selected <column> is guaranteed
1101** to be unique - either because it is an INTEGER PRIMARY KEY or it
1102** is unique by virtue of a constraint or implicit index.
danielk19770cdc0222008-06-26 18:04:03 +00001103**
1104** If the prNotFound parameter is not 0, then the structure will be used
1105** for fast set membership tests. In this case an epheremal table must
1106** be used unless <column> is an INTEGER PRIMARY KEY or an index can
1107** be found with <column> as its left-most column.
1108**
1109** When the structure is being used for set membership tests, the user
1110** needs to know whether or not the structure contains an SQL NULL
1111** value in order to correctly evaluate expressions like "X IN (Y, Z)".
1112** If there is a chance that the structure may contain a NULL value at
1113** runtime, then a register is allocated and the register number written
1114** to *prNotFound. If there is no chance that the structure contains a
1115** NULL value, then *prNotFound is left unchanged.
1116**
1117** If a register is allocated and its location stored in *prNotFound, then
1118** its initial value is NULL. If the structure does not remain constant
1119** for the duration of the query (i.e. the set is a correlated sub-select),
1120** the value of the allocated register is reset to NULL each time the
1121** structure is repopulated. This allows the caller to use vdbe code
1122** equivalent to the following:
1123**
1124** if( register==NULL ){
1125** has_null = <test if data structure contains null>
1126** register = 1
1127** }
1128**
1129** in order to avoid running the <test if data structure contains null>
1130** test more often than is necessary.
danielk19779a96b662007-11-29 17:05:18 +00001131*/
danielk1977284f4ac2007-12-10 05:03:46 +00001132#ifndef SQLITE_OMIT_SUBQUERY
danielk19770cdc0222008-06-26 18:04:03 +00001133int sqlite3FindInIndex(Parse *pParse, Expr *pX, int *prNotFound){
danielk19779a96b662007-11-29 17:05:18 +00001134 Select *p;
1135 int eType = 0;
1136 int iTab = pParse->nTab++;
danielk19770cdc0222008-06-26 18:04:03 +00001137 int mustBeUnique = !prNotFound;
danielk19779a96b662007-11-29 17:05:18 +00001138
1139 /* The follwing if(...) expression is true if the SELECT is of the
1140 ** simple form:
1141 **
1142 ** SELECT <column> FROM <table>
1143 **
1144 ** If this is the case, it may be possible to use an existing table
1145 ** or index instead of generating an epheremal table.
1146 */
drhb287f4b2008-04-25 00:08:38 +00001147 p = pX->pSelect;
1148 if( isCandidateForInOpt(p) ){
danielk19779a96b662007-11-29 17:05:18 +00001149 sqlite3 *db = pParse->db;
1150 Index *pIdx;
1151 Expr *pExpr = p->pEList->a[0].pExpr;
1152 int iCol = pExpr->iColumn;
1153 Vdbe *v = sqlite3GetVdbe(pParse);
1154
1155 /* This function is only called from two places. In both cases the vdbe
1156 ** has already been allocated. So assume sqlite3GetVdbe() is always
1157 ** successful here.
1158 */
1159 assert(v);
1160 if( iCol<0 ){
drh0a07c102008-01-03 18:03:08 +00001161 int iMem = ++pParse->nMem;
danielk19779a96b662007-11-29 17:05:18 +00001162 int iAddr;
1163 Table *pTab = p->pSrc->a[0].pTab;
1164 int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
1165 sqlite3VdbeUsesBtree(v, iDb);
1166
drh892d3172008-01-10 03:46:36 +00001167 iAddr = sqlite3VdbeAddOp1(v, OP_If, iMem);
drh4c583122008-01-04 22:01:03 +00001168 sqlite3VdbeAddOp2(v, OP_Integer, 1, iMem);
danielk19779a96b662007-11-29 17:05:18 +00001169
1170 sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
1171 eType = IN_INDEX_ROWID;
1172
1173 sqlite3VdbeJumpHere(v, iAddr);
1174 }else{
1175 /* The collation sequence used by the comparison. If an index is to
1176 ** be used in place of a temp-table, it must be ordered according
1177 ** to this collation sequence.
1178 */
1179 CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pExpr);
1180
1181 /* Check that the affinity that will be used to perform the
1182 ** comparison is the same as the affinity of the column. If
1183 ** it is not, it is not possible to use any index.
1184 */
1185 Table *pTab = p->pSrc->a[0].pTab;
1186 char aff = comparisonAffinity(pX);
1187 int affinity_ok = (pTab->aCol[iCol].affinity==aff||aff==SQLITE_AFF_NONE);
1188
1189 for(pIdx=pTab->pIndex; pIdx && eType==0 && affinity_ok; pIdx=pIdx->pNext){
1190 if( (pIdx->aiColumn[0]==iCol)
1191 && (pReq==sqlite3FindCollSeq(db, ENC(db), pIdx->azColl[0], -1, 0))
1192 && (!mustBeUnique || (pIdx->nColumn==1 && pIdx->onError!=OE_None))
1193 ){
1194 int iDb;
drh0a07c102008-01-03 18:03:08 +00001195 int iMem = ++pParse->nMem;
danielk19779a96b662007-11-29 17:05:18 +00001196 int iAddr;
1197 char *pKey;
1198
1199 pKey = (char *)sqlite3IndexKeyinfo(pParse, pIdx);
1200 iDb = sqlite3SchemaToIndex(db, pIdx->pSchema);
1201 sqlite3VdbeUsesBtree(v, iDb);
1202
drh892d3172008-01-10 03:46:36 +00001203 iAddr = sqlite3VdbeAddOp1(v, OP_If, iMem);
drh4c583122008-01-04 22:01:03 +00001204 sqlite3VdbeAddOp2(v, OP_Integer, 1, iMem);
danielk19779a96b662007-11-29 17:05:18 +00001205
danielk1977cd3e8f72008-03-25 09:47:35 +00001206 sqlite3VdbeAddOp2(v, OP_SetNumColumns, 0, pIdx->nColumn);
danielk1977207872a2008-01-03 07:54:23 +00001207 sqlite3VdbeAddOp4(v, OP_OpenRead, iTab, pIdx->tnum, iDb,
drh66a51672008-01-03 00:01:23 +00001208 pKey,P4_KEYINFO_HANDOFF);
danielk1977207872a2008-01-03 07:54:23 +00001209 VdbeComment((v, "%s", pIdx->zName));
danielk19779a96b662007-11-29 17:05:18 +00001210 eType = IN_INDEX_INDEX;
danielk19779a96b662007-11-29 17:05:18 +00001211
1212 sqlite3VdbeJumpHere(v, iAddr);
danielk19770cdc0222008-06-26 18:04:03 +00001213 if( prNotFound && !pTab->aCol[iCol].notNull ){
1214 *prNotFound = ++pParse->nMem;
1215 }
danielk19779a96b662007-11-29 17:05:18 +00001216 }
1217 }
1218 }
1219 }
1220
1221 if( eType==0 ){
danielk19770cdc0222008-06-26 18:04:03 +00001222 int rMayHaveNull = 0;
danielk197741a05b72008-10-02 13:50:55 +00001223 eType = IN_INDEX_EPH;
danielk19770cdc0222008-06-26 18:04:03 +00001224 if( prNotFound ){
1225 *prNotFound = rMayHaveNull = ++pParse->nMem;
danielk197741a05b72008-10-02 13:50:55 +00001226 }else if( pX->pLeft->iColumn<0 && pX->pSelect==0 ){
1227 eType = IN_INDEX_ROWID;
danielk19770cdc0222008-06-26 18:04:03 +00001228 }
danielk197741a05b72008-10-02 13:50:55 +00001229 sqlite3CodeSubselect(pParse, pX, rMayHaveNull, eType==IN_INDEX_ROWID);
danielk19779a96b662007-11-29 17:05:18 +00001230 }else{
1231 pX->iTable = iTab;
1232 }
1233 return eType;
1234}
danielk1977284f4ac2007-12-10 05:03:46 +00001235#endif
drh626a8792005-01-17 22:08:19 +00001236
1237/*
drh9cbe6352005-11-29 03:13:21 +00001238** Generate code for scalar subqueries used as an expression
1239** and IN operators. Examples:
drh626a8792005-01-17 22:08:19 +00001240**
drh9cbe6352005-11-29 03:13:21 +00001241** (SELECT a FROM b) -- subquery
1242** EXISTS (SELECT a FROM b) -- EXISTS subquery
1243** x IN (4,5,11) -- IN operator with list on right-hand side
1244** x IN (SELECT a FROM b) -- IN operator with subquery on the right
drhfef52082000-06-06 01:50:43 +00001245**
drh9cbe6352005-11-29 03:13:21 +00001246** The pExpr parameter describes the expression that contains the IN
1247** operator or subquery.
danielk197741a05b72008-10-02 13:50:55 +00001248**
1249** If parameter isRowid is non-zero, then expression pExpr is guaranteed
1250** to be of the form "<rowid> IN (?, ?, ?)", where <rowid> is a reference
1251** to some integer key column of a table B-Tree. In this case, use an
1252** intkey B-Tree to store the set of IN(...) values instead of the usual
1253** (slower) variable length keys B-Tree.
drhcce7d172000-05-31 15:34:51 +00001254*/
drh51522cd2005-01-20 13:36:19 +00001255#ifndef SQLITE_OMIT_SUBQUERY
danielk197741a05b72008-10-02 13:50:55 +00001256void sqlite3CodeSubselect(
1257 Parse *pParse,
1258 Expr *pExpr,
1259 int rMayHaveNull,
1260 int isRowid
1261){
drh57dbd7b2005-07-08 18:25:26 +00001262 int testAddr = 0; /* One-time test address */
danielk1977b3bce662005-01-29 08:32:43 +00001263 Vdbe *v = sqlite3GetVdbe(pParse);
1264 if( v==0 ) return;
1265
danielk1977fc976062007-05-10 10:46:56 +00001266
drh57dbd7b2005-07-08 18:25:26 +00001267 /* This code must be run in its entirety every time it is encountered
1268 ** if any of the following is true:
1269 **
1270 ** * The right-hand side is a correlated subquery
1271 ** * The right-hand side is an expression list containing variables
1272 ** * We are inside a trigger
1273 **
1274 ** If all of the above are false, then we can run this code just once
1275 ** save the results, and reuse the same result on subsequent invocations.
danielk1977b3bce662005-01-29 08:32:43 +00001276 */
1277 if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->trigStack ){
drh0a07c102008-01-03 18:03:08 +00001278 int mem = ++pParse->nMem;
drh892d3172008-01-10 03:46:36 +00001279 sqlite3VdbeAddOp1(v, OP_If, mem);
1280 testAddr = sqlite3VdbeAddOp2(v, OP_Integer, 1, mem);
drh17435752007-08-16 04:30:38 +00001281 assert( testAddr>0 || pParse->db->mallocFailed );
danielk1977b3bce662005-01-29 08:32:43 +00001282 }
1283
drhcce7d172000-05-31 15:34:51 +00001284 switch( pExpr->op ){
drhfef52082000-06-06 01:50:43 +00001285 case TK_IN: {
danielk1977e014a832004-05-17 10:48:57 +00001286 char affinity;
drhd3d39e92004-05-20 22:16:29 +00001287 KeyInfo keyInfo;
drhb9bb7c12006-06-11 23:41:55 +00001288 int addr; /* Address of OP_OpenEphemeral instruction */
danielk197741a05b72008-10-02 13:50:55 +00001289 Expr *pLeft = pExpr->pLeft;
drhd3d39e92004-05-20 22:16:29 +00001290
danielk19770cdc0222008-06-26 18:04:03 +00001291 if( rMayHaveNull ){
1292 sqlite3VdbeAddOp2(v, OP_Null, 0, rMayHaveNull);
1293 }
1294
danielk197741a05b72008-10-02 13:50:55 +00001295 affinity = sqlite3ExprAffinity(pLeft);
danielk1977e014a832004-05-17 10:48:57 +00001296
1297 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
drh57dbd7b2005-07-08 18:25:26 +00001298 ** expression it is handled the same way. A virtual table is
danielk1977e014a832004-05-17 10:48:57 +00001299 ** filled with single-field index keys representing the results
1300 ** from the SELECT or the <exprlist>.
1301 **
1302 ** If the 'x' expression is a column value, or the SELECT...
1303 ** statement returns a column value, then the affinity of that
1304 ** column is used to build the index keys. If both 'x' and the
1305 ** SELECT... statement are columns, then numeric affinity is used
1306 ** if either column has NUMERIC or INTEGER affinity. If neither
1307 ** 'x' nor the SELECT... statement are columns, then numeric affinity
1308 ** is used.
1309 */
1310 pExpr->iTable = pParse->nTab++;
danielk197741a05b72008-10-02 13:50:55 +00001311 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pExpr->iTable, !isRowid);
drhd3d39e92004-05-20 22:16:29 +00001312 memset(&keyInfo, 0, sizeof(keyInfo));
1313 keyInfo.nField = 1;
danielk1977e014a832004-05-17 10:48:57 +00001314
drhfef52082000-06-06 01:50:43 +00001315 if( pExpr->pSelect ){
1316 /* Case 1: expr IN (SELECT ...)
1317 **
danielk1977e014a832004-05-17 10:48:57 +00001318 ** Generate code to write the results of the select into the temporary
1319 ** table allocated and opened above.
drhfef52082000-06-06 01:50:43 +00001320 */
drh1013c932008-01-06 00:25:21 +00001321 SelectDest dest;
drhbe5c89a2004-07-26 00:31:09 +00001322 ExprList *pEList;
drh1013c932008-01-06 00:25:21 +00001323
danielk197741a05b72008-10-02 13:50:55 +00001324 assert( !isRowid );
drh1013c932008-01-06 00:25:21 +00001325 sqlite3SelectDestInit(&dest, SRT_Set, pExpr->iTable);
drh1bd10f82008-12-10 21:19:56 +00001326 dest.affinity = (u8)affinity;
danielk1977e014a832004-05-17 10:48:57 +00001327 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
drh7d10d5a2008-08-20 16:35:10 +00001328 if( sqlite3Select(pParse, pExpr->pSelect, &dest) ){
drh94ccde52007-04-13 16:06:32 +00001329 return;
1330 }
drhbe5c89a2004-07-26 00:31:09 +00001331 pEList = pExpr->pSelect->pEList;
1332 if( pEList && pEList->nExpr>0 ){
danielk1977bcbb04e2007-05-29 12:11:29 +00001333 keyInfo.aColl[0] = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft,
drhbe5c89a2004-07-26 00:31:09 +00001334 pEList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001335 }
drhfef52082000-06-06 01:50:43 +00001336 }else if( pExpr->pList ){
1337 /* Case 2: expr IN (exprlist)
1338 **
drhfd131da2007-08-07 17:13:03 +00001339 ** For each expression, build an index key from the evaluation and
danielk1977e014a832004-05-17 10:48:57 +00001340 ** store it in the temporary table. If <expr> is a column, then use
1341 ** that columns affinity when building index keys. If <expr> is not
1342 ** a column, use numeric affinity.
drhfef52082000-06-06 01:50:43 +00001343 */
danielk1977e014a832004-05-17 10:48:57 +00001344 int i;
drh57dbd7b2005-07-08 18:25:26 +00001345 ExprList *pList = pExpr->pList;
1346 struct ExprList_item *pItem;
drhecc31802008-06-26 20:06:06 +00001347 int r1, r2, r3;
drh57dbd7b2005-07-08 18:25:26 +00001348
danielk1977e014a832004-05-17 10:48:57 +00001349 if( !affinity ){
drh8159a352006-05-23 23:22:29 +00001350 affinity = SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +00001351 }
drh7d10d5a2008-08-20 16:35:10 +00001352 keyInfo.aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +00001353
1354 /* Loop through each expression in <exprlist>. */
drh2d401ab2008-01-10 23:50:11 +00001355 r1 = sqlite3GetTempReg(pParse);
1356 r2 = sqlite3GetTempReg(pParse);
danielk19774e7f36a2008-10-02 16:42:06 +00001357 sqlite3VdbeAddOp2(v, OP_Null, 0, r2);
drh57dbd7b2005-07-08 18:25:26 +00001358 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
1359 Expr *pE2 = pItem->pExpr;
danielk1977e014a832004-05-17 10:48:57 +00001360
drh57dbd7b2005-07-08 18:25:26 +00001361 /* If the expression is not constant then we will need to
1362 ** disable the test that was generated above that makes sure
1363 ** this code only executes once. Because for a non-constant
1364 ** expression we need to rerun this code each time.
1365 */
drh892d3172008-01-10 03:46:36 +00001366 if( testAddr && !sqlite3ExprIsConstant(pE2) ){
1367 sqlite3VdbeChangeToNoop(v, testAddr-1, 2);
drh57dbd7b2005-07-08 18:25:26 +00001368 testAddr = 0;
drh4794b982000-06-06 13:54:14 +00001369 }
danielk1977e014a832004-05-17 10:48:57 +00001370
1371 /* Evaluate the expression and insert it into the temp table */
drhe55cbd72008-03-31 23:48:03 +00001372 pParse->disableColCache++;
drhecc31802008-06-26 20:06:06 +00001373 r3 = sqlite3ExprCodeTarget(pParse, pE2, r1);
drhc5499be2008-04-01 15:06:33 +00001374 assert( pParse->disableColCache>0 );
drhe55cbd72008-03-31 23:48:03 +00001375 pParse->disableColCache--;
danielk197741a05b72008-10-02 13:50:55 +00001376
1377 if( isRowid ){
danielk197741a05b72008-10-02 13:50:55 +00001378 sqlite3VdbeAddOp2(v, OP_MustBeInt, r3, sqlite3VdbeCurrentAddr(v)+2);
1379 sqlite3VdbeAddOp3(v, OP_Insert, pExpr->iTable, r2, r3);
1380 }else{
1381 sqlite3VdbeAddOp4(v, OP_MakeRecord, r3, 1, r2, &affinity, 1);
1382 sqlite3ExprCacheAffinityChange(pParse, r3, 1);
1383 sqlite3VdbeAddOp2(v, OP_IdxInsert, pExpr->iTable, r2);
1384 }
drhfef52082000-06-06 01:50:43 +00001385 }
drh2d401ab2008-01-10 23:50:11 +00001386 sqlite3ReleaseTempReg(pParse, r1);
1387 sqlite3ReleaseTempReg(pParse, r2);
drhfef52082000-06-06 01:50:43 +00001388 }
danielk197741a05b72008-10-02 13:50:55 +00001389 if( !isRowid ){
1390 sqlite3VdbeChangeP4(v, addr, (void *)&keyInfo, P4_KEYINFO);
1391 }
danielk1977b3bce662005-01-29 08:32:43 +00001392 break;
drhfef52082000-06-06 01:50:43 +00001393 }
1394
drh51522cd2005-01-20 13:36:19 +00001395 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001396 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +00001397 /* This has to be a scalar SELECT. Generate code to put the
1398 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +00001399 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +00001400 */
drh2646da72005-12-09 20:02:05 +00001401 static const Token one = { (u8*)"1", 0, 1 };
drh51522cd2005-01-20 13:36:19 +00001402 Select *pSel;
danielk19776c8c8ce2008-01-02 16:27:09 +00001403 SelectDest dest;
drh1398ad32005-01-19 23:24:50 +00001404
drh51522cd2005-01-20 13:36:19 +00001405 pSel = pExpr->pSelect;
drh1013c932008-01-06 00:25:21 +00001406 sqlite3SelectDestInit(&dest, 0, ++pParse->nMem);
drh51522cd2005-01-20 13:36:19 +00001407 if( pExpr->op==TK_SELECT ){
danielk19776c8c8ce2008-01-02 16:27:09 +00001408 dest.eDest = SRT_Mem;
drh4c583122008-01-04 22:01:03 +00001409 sqlite3VdbeAddOp2(v, OP_Null, 0, dest.iParm);
drhd4e70eb2008-01-02 00:34:36 +00001410 VdbeComment((v, "Init subquery result"));
drh51522cd2005-01-20 13:36:19 +00001411 }else{
danielk19776c8c8ce2008-01-02 16:27:09 +00001412 dest.eDest = SRT_Exists;
drh4c583122008-01-04 22:01:03 +00001413 sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iParm);
drhd4e70eb2008-01-02 00:34:36 +00001414 VdbeComment((v, "Init EXISTS result"));
drh51522cd2005-01-20 13:36:19 +00001415 }
drh633e6d52008-07-28 19:34:53 +00001416 sqlite3ExprDelete(pParse->db, pSel->pLimit);
danielk1977a1644fd2007-08-29 12:31:25 +00001417 pSel->pLimit = sqlite3PExpr(pParse, TK_INTEGER, 0, 0, &one);
drh7d10d5a2008-08-20 16:35:10 +00001418 if( sqlite3Select(pParse, pSel, &dest) ){
drh94ccde52007-04-13 16:06:32 +00001419 return;
1420 }
danielk19776c8c8ce2008-01-02 16:27:09 +00001421 pExpr->iColumn = dest.iParm;
danielk1977b3bce662005-01-29 08:32:43 +00001422 break;
drhcce7d172000-05-31 15:34:51 +00001423 }
1424 }
danielk1977b3bce662005-01-29 08:32:43 +00001425
drh57dbd7b2005-07-08 18:25:26 +00001426 if( testAddr ){
drh892d3172008-01-10 03:46:36 +00001427 sqlite3VdbeJumpHere(v, testAddr-1);
danielk1977b3bce662005-01-29 08:32:43 +00001428 }
danielk1977fc976062007-05-10 10:46:56 +00001429
danielk1977b3bce662005-01-29 08:32:43 +00001430 return;
drhcce7d172000-05-31 15:34:51 +00001431}
drh51522cd2005-01-20 13:36:19 +00001432#endif /* SQLITE_OMIT_SUBQUERY */
drhcce7d172000-05-31 15:34:51 +00001433
drhcce7d172000-05-31 15:34:51 +00001434/*
drh598f1342007-10-23 15:39:45 +00001435** Duplicate an 8-byte value
1436*/
1437static char *dup8bytes(Vdbe *v, const char *in){
1438 char *out = sqlite3DbMallocRaw(sqlite3VdbeDb(v), 8);
1439 if( out ){
1440 memcpy(out, in, 8);
1441 }
1442 return out;
1443}
1444
1445/*
1446** Generate an instruction that will put the floating point
drh9cbf3422008-01-17 16:22:13 +00001447** value described by z[0..n-1] into register iMem.
drh0cf19ed2007-10-23 18:55:48 +00001448**
1449** The z[] string will probably not be zero-terminated. But the
1450** z[n] character is guaranteed to be something that does not look
1451** like the continuation of the number.
drh598f1342007-10-23 15:39:45 +00001452*/
drh9de221d2008-01-05 06:51:30 +00001453static void codeReal(Vdbe *v, const char *z, int n, int negateFlag, int iMem){
drh598f1342007-10-23 15:39:45 +00001454 assert( z || v==0 || sqlite3VdbeDb(v)->mallocFailed );
danielk1977f3d3c272008-11-19 16:52:44 +00001455 assert( !z || !isdigit(z[n]) );
1456 UNUSED_PARAMETER(n);
drh598f1342007-10-23 15:39:45 +00001457 if( z ){
1458 double value;
1459 char *zV;
1460 sqlite3AtoF(z, &value);
drh2eaf93d2008-04-29 00:15:20 +00001461 if( sqlite3IsNaN(value) ){
1462 sqlite3VdbeAddOp2(v, OP_Null, 0, iMem);
1463 }else{
1464 if( negateFlag ) value = -value;
1465 zV = dup8bytes(v, (char*)&value);
1466 sqlite3VdbeAddOp4(v, OP_Real, 0, iMem, 0, zV, P4_REAL);
1467 }
drh598f1342007-10-23 15:39:45 +00001468 }
1469}
1470
1471
1472/*
drhfec19aa2004-05-19 20:41:03 +00001473** Generate an instruction that will put the integer describe by
drh9cbf3422008-01-17 16:22:13 +00001474** text z[0..n-1] into register iMem.
drh0cf19ed2007-10-23 18:55:48 +00001475**
1476** The z[] string will probably not be zero-terminated. But the
1477** z[n] character is guaranteed to be something that does not look
1478** like the continuation of the number.
drhfec19aa2004-05-19 20:41:03 +00001479*/
drh92b01d52008-06-24 00:32:35 +00001480static void codeInteger(Vdbe *v, Expr *pExpr, int negFlag, int iMem){
1481 const char *z;
1482 if( pExpr->flags & EP_IntValue ){
1483 int i = pExpr->iTable;
1484 if( negFlag ) i = -i;
1485 sqlite3VdbeAddOp2(v, OP_Integer, i, iMem);
1486 }else if( (z = (char*)pExpr->token.z)!=0 ){
danielk1977c9cf9012007-05-30 10:36:47 +00001487 int i;
drh92b01d52008-06-24 00:32:35 +00001488 int n = pExpr->token.n;
drh0cf19ed2007-10-23 18:55:48 +00001489 assert( !isdigit(z[n]) );
danielk1977c9cf9012007-05-30 10:36:47 +00001490 if( sqlite3GetInt32(z, &i) ){
drh9de221d2008-01-05 06:51:30 +00001491 if( negFlag ) i = -i;
1492 sqlite3VdbeAddOp2(v, OP_Integer, i, iMem);
1493 }else if( sqlite3FitsIn64Bits(z, negFlag) ){
drh598f1342007-10-23 15:39:45 +00001494 i64 value;
1495 char *zV;
1496 sqlite3Atoi64(z, &value);
drh9de221d2008-01-05 06:51:30 +00001497 if( negFlag ) value = -value;
drh598f1342007-10-23 15:39:45 +00001498 zV = dup8bytes(v, (char*)&value);
drh9de221d2008-01-05 06:51:30 +00001499 sqlite3VdbeAddOp4(v, OP_Int64, 0, iMem, 0, zV, P4_INT64);
danielk1977c9cf9012007-05-30 10:36:47 +00001500 }else{
drh9de221d2008-01-05 06:51:30 +00001501 codeReal(v, z, n, negFlag, iMem);
danielk1977c9cf9012007-05-30 10:36:47 +00001502 }
drhfec19aa2004-05-19 20:41:03 +00001503 }
1504}
1505
drh945498f2007-02-24 11:52:52 +00001506
1507/*
1508** Generate code that will extract the iColumn-th column from
drhe55cbd72008-03-31 23:48:03 +00001509** table pTab and store the column value in a register. An effort
1510** is made to store the column value in register iReg, but this is
1511** not guaranteed. The location of the column value is returned.
1512**
1513** There must be an open cursor to pTab in iTable when this routine
1514** is called. If iColumn<0 then code is generated that extracts the rowid.
drhda250ea2008-04-01 05:07:14 +00001515**
1516** This routine might attempt to reuse the value of the column that
1517** has already been loaded into a register. The value will always
1518** be used if it has not undergone any affinity changes. But if
1519** an affinity change has occurred, then the cached value will only be
1520** used if allowAffChng is true.
drh945498f2007-02-24 11:52:52 +00001521*/
drhe55cbd72008-03-31 23:48:03 +00001522int sqlite3ExprCodeGetColumn(
1523 Parse *pParse, /* Parsing and code generating context */
drh2133d822008-01-03 18:44:59 +00001524 Table *pTab, /* Description of the table we are reading from */
1525 int iColumn, /* Index of the table column */
1526 int iTable, /* The cursor pointing to the table */
drhda250ea2008-04-01 05:07:14 +00001527 int iReg, /* Store results here */
1528 int allowAffChng /* True if prior affinity changes are OK */
drh2133d822008-01-03 18:44:59 +00001529){
drhe55cbd72008-03-31 23:48:03 +00001530 Vdbe *v = pParse->pVdbe;
1531 int i;
drhda250ea2008-04-01 05:07:14 +00001532 struct yColCache *p;
drhe55cbd72008-03-31 23:48:03 +00001533
drhda250ea2008-04-01 05:07:14 +00001534 for(i=0, p=pParse->aColCache; i<pParse->nColCache; i++, p++){
1535 if( p->iTable==iTable && p->iColumn==iColumn
1536 && (!p->affChange || allowAffChng) ){
drhe55cbd72008-03-31 23:48:03 +00001537#if 0
1538 sqlite3VdbeAddOp0(v, OP_Noop);
drhda250ea2008-04-01 05:07:14 +00001539 VdbeComment((v, "OPT: tab%d.col%d -> r%d", iTable, iColumn, p->iReg));
drhe55cbd72008-03-31 23:48:03 +00001540#endif
drhda250ea2008-04-01 05:07:14 +00001541 return p->iReg;
drhe55cbd72008-03-31 23:48:03 +00001542 }
1543 }
1544 assert( v!=0 );
drh945498f2007-02-24 11:52:52 +00001545 if( iColumn<0 ){
1546 int op = (pTab && IsVirtual(pTab)) ? OP_VRowid : OP_Rowid;
drh2133d822008-01-03 18:44:59 +00001547 sqlite3VdbeAddOp2(v, op, iTable, iReg);
drh945498f2007-02-24 11:52:52 +00001548 }else if( pTab==0 ){
drh2133d822008-01-03 18:44:59 +00001549 sqlite3VdbeAddOp3(v, OP_Column, iTable, iColumn, iReg);
drh945498f2007-02-24 11:52:52 +00001550 }else{
1551 int op = IsVirtual(pTab) ? OP_VColumn : OP_Column;
drh2133d822008-01-03 18:44:59 +00001552 sqlite3VdbeAddOp3(v, op, iTable, iColumn, iReg);
drh945498f2007-02-24 11:52:52 +00001553 sqlite3ColumnDefault(v, pTab, iColumn);
1554#ifndef SQLITE_OMIT_FLOATING_POINT
1555 if( pTab->aCol[iColumn].affinity==SQLITE_AFF_REAL ){
drh2133d822008-01-03 18:44:59 +00001556 sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg);
drh945498f2007-02-24 11:52:52 +00001557 }
1558#endif
1559 }
drhe55cbd72008-03-31 23:48:03 +00001560 if( pParse->disableColCache==0 ){
1561 i = pParse->iColCache;
drhda250ea2008-04-01 05:07:14 +00001562 p = &pParse->aColCache[i];
1563 p->iTable = iTable;
1564 p->iColumn = iColumn;
1565 p->iReg = iReg;
drhc5499be2008-04-01 15:06:33 +00001566 p->affChange = 0;
drhe55cbd72008-03-31 23:48:03 +00001567 i++;
drh2f7794c2008-04-01 03:27:39 +00001568 if( i>=ArraySize(pParse->aColCache) ) i = 0;
drhe55cbd72008-03-31 23:48:03 +00001569 if( i>pParse->nColCache ) pParse->nColCache = i;
drh2f7794c2008-04-01 03:27:39 +00001570 pParse->iColCache = i;
drhe55cbd72008-03-31 23:48:03 +00001571 }
1572 return iReg;
1573}
1574
1575/*
drhe55cbd72008-03-31 23:48:03 +00001576** Clear all column cache entries associated with the vdbe
1577** cursor with cursor number iTable.
1578*/
1579void sqlite3ExprClearColumnCache(Parse *pParse, int iTable){
1580 if( iTable<0 ){
1581 pParse->nColCache = 0;
1582 pParse->iColCache = 0;
1583 }else{
1584 int i;
1585 for(i=0; i<pParse->nColCache; i++){
1586 if( pParse->aColCache[i].iTable==iTable ){
drhc5499be2008-04-01 15:06:33 +00001587 testcase( i==pParse->nColCache-1 );
drhe55cbd72008-03-31 23:48:03 +00001588 pParse->aColCache[i] = pParse->aColCache[--pParse->nColCache];
drhda250ea2008-04-01 05:07:14 +00001589 pParse->iColCache = pParse->nColCache;
drhe55cbd72008-03-31 23:48:03 +00001590 }
1591 }
drhe55cbd72008-03-31 23:48:03 +00001592 }
1593}
1594
1595/*
drhda250ea2008-04-01 05:07:14 +00001596** Record the fact that an affinity change has occurred on iCount
1597** registers starting with iStart.
drhe55cbd72008-03-31 23:48:03 +00001598*/
drhda250ea2008-04-01 05:07:14 +00001599void sqlite3ExprCacheAffinityChange(Parse *pParse, int iStart, int iCount){
1600 int iEnd = iStart + iCount - 1;
drhe55cbd72008-03-31 23:48:03 +00001601 int i;
1602 for(i=0; i<pParse->nColCache; i++){
1603 int r = pParse->aColCache[i].iReg;
drhda250ea2008-04-01 05:07:14 +00001604 if( r>=iStart && r<=iEnd ){
1605 pParse->aColCache[i].affChange = 1;
drhe55cbd72008-03-31 23:48:03 +00001606 }
1607 }
drhe55cbd72008-03-31 23:48:03 +00001608}
1609
1610/*
drhb21e7c72008-06-22 12:37:57 +00001611** Generate code to move content from registers iFrom...iFrom+nReg-1
1612** over to iTo..iTo+nReg-1. Keep the column cache up-to-date.
drhe55cbd72008-03-31 23:48:03 +00001613*/
drhb21e7c72008-06-22 12:37:57 +00001614void sqlite3ExprCodeMove(Parse *pParse, int iFrom, int iTo, int nReg){
drhe55cbd72008-03-31 23:48:03 +00001615 int i;
1616 if( iFrom==iTo ) return;
drhb21e7c72008-06-22 12:37:57 +00001617 sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move, iFrom, iTo, nReg);
drhe55cbd72008-03-31 23:48:03 +00001618 for(i=0; i<pParse->nColCache; i++){
drhb21e7c72008-06-22 12:37:57 +00001619 int x = pParse->aColCache[i].iReg;
1620 if( x>=iFrom && x<iFrom+nReg ){
1621 pParse->aColCache[i].iReg += iTo-iFrom;
drhe55cbd72008-03-31 23:48:03 +00001622 }
1623 }
drh945498f2007-02-24 11:52:52 +00001624}
1625
drhfec19aa2004-05-19 20:41:03 +00001626/*
drh92b01d52008-06-24 00:32:35 +00001627** Generate code to copy content from registers iFrom...iFrom+nReg-1
1628** over to iTo..iTo+nReg-1.
1629*/
1630void sqlite3ExprCodeCopy(Parse *pParse, int iFrom, int iTo, int nReg){
1631 int i;
1632 if( iFrom==iTo ) return;
1633 for(i=0; i<nReg; i++){
1634 sqlite3VdbeAddOp2(pParse->pVdbe, OP_Copy, iFrom+i, iTo+i);
1635 }
1636}
1637
1638/*
drh652fbf52008-04-01 01:42:41 +00001639** Return true if any register in the range iFrom..iTo (inclusive)
1640** is used as part of the column cache.
1641*/
1642static int usedAsColumnCache(Parse *pParse, int iFrom, int iTo){
1643 int i;
1644 for(i=0; i<pParse->nColCache; i++){
1645 int r = pParse->aColCache[i].iReg;
1646 if( r>=iFrom && r<=iTo ) return 1;
1647 }
1648 return 0;
1649}
1650
1651/*
drhd1fa7bc2009-01-10 13:24:50 +00001652** There is a value in register iReg.
drh652fbf52008-04-01 01:42:41 +00001653**
1654** We are going to modify the value, so we need to make sure it
drhd1fa7bc2009-01-10 13:24:50 +00001655** is not a cached register. If iReg is a cached register,
1656** then clear the corresponding cache line.
drh652fbf52008-04-01 01:42:41 +00001657*/
drhd1fa7bc2009-01-10 13:24:50 +00001658void sqlite3ExprWritableRegister(Parse *pParse, int iReg){
drhda250ea2008-04-01 05:07:14 +00001659 int i;
drhd1fa7bc2009-01-10 13:24:50 +00001660 if( usedAsColumnCache(pParse, iReg, iReg) ){
1661 for(i=0; i<pParse->nColCache; i++){
1662 if( pParse->aColCache[i].iReg==iReg ){
1663 pParse->aColCache[i] = pParse->aColCache[--pParse->nColCache];
1664 pParse->iColCache = pParse->nColCache;
1665 }
drhda250ea2008-04-01 05:07:14 +00001666 }
1667 }
drh652fbf52008-04-01 01:42:41 +00001668}
1669
1670/*
drh191b54c2008-04-15 12:14:21 +00001671** If the last instruction coded is an ephemeral copy of any of
1672** the registers in the nReg registers beginning with iReg, then
1673** convert the last instruction from OP_SCopy to OP_Copy.
1674*/
1675void sqlite3ExprHardCopy(Parse *pParse, int iReg, int nReg){
1676 int addr;
1677 VdbeOp *pOp;
1678 Vdbe *v;
1679
1680 v = pParse->pVdbe;
1681 addr = sqlite3VdbeCurrentAddr(v);
1682 pOp = sqlite3VdbeGetOp(v, addr-1);
danielk1977d7eb2ed2008-04-24 12:36:35 +00001683 assert( pOp || pParse->db->mallocFailed );
1684 if( pOp && pOp->opcode==OP_SCopy && pOp->p1>=iReg && pOp->p1<iReg+nReg ){
drh191b54c2008-04-15 12:14:21 +00001685 pOp->opcode = OP_Copy;
1686 }
1687}
1688
1689/*
drh8b213892008-08-29 02:14:02 +00001690** Generate code to store the value of the iAlias-th alias in register
1691** target. The first time this is called, pExpr is evaluated to compute
1692** the value of the alias. The value is stored in an auxiliary register
1693** and the number of that register is returned. On subsequent calls,
1694** the register number is returned without generating any code.
1695**
1696** Note that in order for this to work, code must be generated in the
1697** same order that it is executed.
1698**
1699** Aliases are numbered starting with 1. So iAlias is in the range
1700** of 1 to pParse->nAlias inclusive.
1701**
1702** pParse->aAlias[iAlias-1] records the register number where the value
1703** of the iAlias-th alias is stored. If zero, that means that the
1704** alias has not yet been computed.
1705*/
drh31daa632008-10-25 15:03:20 +00001706static int codeAlias(Parse *pParse, int iAlias, Expr *pExpr, int target){
drh8b213892008-08-29 02:14:02 +00001707 sqlite3 *db = pParse->db;
1708 int iReg;
drh555f8de2008-12-08 13:42:36 +00001709 if( pParse->nAliasAlloc<pParse->nAlias ){
1710 pParse->aAlias = sqlite3DbReallocOrFree(db, pParse->aAlias,
drh8b213892008-08-29 02:14:02 +00001711 sizeof(pParse->aAlias[0])*pParse->nAlias );
drh555f8de2008-12-08 13:42:36 +00001712 testcase( db->mallocFailed && pParse->nAliasAlloc>0 );
drh8b213892008-08-29 02:14:02 +00001713 if( db->mallocFailed ) return 0;
drh555f8de2008-12-08 13:42:36 +00001714 memset(&pParse->aAlias[pParse->nAliasAlloc], 0,
1715 (pParse->nAlias-pParse->nAliasAlloc)*sizeof(pParse->aAlias[0]));
1716 pParse->nAliasAlloc = pParse->nAlias;
drh8b213892008-08-29 02:14:02 +00001717 }
1718 assert( iAlias>0 && iAlias<=pParse->nAlias );
1719 iReg = pParse->aAlias[iAlias-1];
1720 if( iReg==0 ){
drh31daa632008-10-25 15:03:20 +00001721 if( pParse->disableColCache ){
1722 iReg = sqlite3ExprCodeTarget(pParse, pExpr, target);
1723 }else{
1724 iReg = ++pParse->nMem;
1725 sqlite3ExprCode(pParse, pExpr, iReg);
1726 pParse->aAlias[iAlias-1] = iReg;
1727 }
drh8b213892008-08-29 02:14:02 +00001728 }
1729 return iReg;
1730}
1731
1732/*
drhcce7d172000-05-31 15:34:51 +00001733** Generate code into the current Vdbe to evaluate the given
drh2dcef112008-01-12 19:03:48 +00001734** expression. Attempt to store the results in register "target".
1735** Return the register where results are stored.
drh389a1ad2008-01-03 23:44:53 +00001736**
drh8b213892008-08-29 02:14:02 +00001737** With this routine, there is no guarantee that results will
drh2dcef112008-01-12 19:03:48 +00001738** be stored in target. The result might be stored in some other
1739** register if it is convenient to do so. The calling function
1740** must check the return code and move the results to the desired
1741** register.
drhcce7d172000-05-31 15:34:51 +00001742*/
drh678ccce2008-03-31 18:19:54 +00001743int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){
drh2dcef112008-01-12 19:03:48 +00001744 Vdbe *v = pParse->pVdbe; /* The VM under construction */
1745 int op; /* The opcode being coded */
1746 int inReg = target; /* Results stored in register inReg */
1747 int regFree1 = 0; /* If non-zero free this temporary register */
1748 int regFree2 = 0; /* If non-zero free this temporary register */
drh678ccce2008-03-31 18:19:54 +00001749 int r1, r2, r3, r4; /* Various register numbers */
drh8b213892008-08-29 02:14:02 +00001750 sqlite3 *db;
drhffe07b22005-11-03 00:41:17 +00001751
drh8b213892008-08-29 02:14:02 +00001752 db = pParse->db;
1753 assert( v!=0 || db->mallocFailed );
drh9cbf3422008-01-17 16:22:13 +00001754 assert( target>0 && target<=pParse->nMem );
drh389a1ad2008-01-03 23:44:53 +00001755 if( v==0 ) return 0;
drh389a1ad2008-01-03 23:44:53 +00001756
1757 if( pExpr==0 ){
1758 op = TK_NULL;
1759 }else{
1760 op = pExpr->op;
1761 }
drhf2bc0132004-10-04 13:19:23 +00001762 switch( op ){
drh13449892005-09-07 21:22:45 +00001763 case TK_AGG_COLUMN: {
1764 AggInfo *pAggInfo = pExpr->pAggInfo;
1765 struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg];
1766 if( !pAggInfo->directMode ){
drh9de221d2008-01-05 06:51:30 +00001767 assert( pCol->iMem>0 );
1768 inReg = pCol->iMem;
drh13449892005-09-07 21:22:45 +00001769 break;
1770 }else if( pAggInfo->useSortingIdx ){
drh389a1ad2008-01-03 23:44:53 +00001771 sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdx,
1772 pCol->iSorterColumn, target);
drh13449892005-09-07 21:22:45 +00001773 break;
1774 }
1775 /* Otherwise, fall thru into the TK_COLUMN case */
1776 }
drh967e8b72000-06-21 13:59:10 +00001777 case TK_COLUMN: {
drhffe07b22005-11-03 00:41:17 +00001778 if( pExpr->iTable<0 ){
1779 /* This only happens when coding check constraints */
drhaa9b8962008-01-08 02:57:55 +00001780 assert( pParse->ckBase>0 );
1781 inReg = pExpr->iColumn + pParse->ckBase;
drhc4a3c772001-04-04 11:48:57 +00001782 }else{
drhc5499be2008-04-01 15:06:33 +00001783 testcase( (pExpr->flags & EP_AnyAff)!=0 );
drhe55cbd72008-03-31 23:48:03 +00001784 inReg = sqlite3ExprCodeGetColumn(pParse, pExpr->pTab,
drhda250ea2008-04-01 05:07:14 +00001785 pExpr->iColumn, pExpr->iTable, target,
1786 pExpr->flags & EP_AnyAff);
drh22827922000-06-06 17:27:05 +00001787 }
drhcce7d172000-05-31 15:34:51 +00001788 break;
1789 }
1790 case TK_INTEGER: {
drh92b01d52008-06-24 00:32:35 +00001791 codeInteger(v, pExpr, 0, target);
drhfec19aa2004-05-19 20:41:03 +00001792 break;
1793 }
drh598f1342007-10-23 15:39:45 +00001794 case TK_FLOAT: {
drh9de221d2008-01-05 06:51:30 +00001795 codeReal(v, (char*)pExpr->token.z, pExpr->token.n, 0, target);
drh598f1342007-10-23 15:39:45 +00001796 break;
1797 }
drhfec19aa2004-05-19 20:41:03 +00001798 case TK_STRING: {
drh8b213892008-08-29 02:14:02 +00001799 sqlite3DequoteExpr(db, pExpr);
drh9de221d2008-01-05 06:51:30 +00001800 sqlite3VdbeAddOp4(v,OP_String8, 0, target, 0,
drh66a51672008-01-03 00:01:23 +00001801 (char*)pExpr->token.z, pExpr->token.n);
drhcce7d172000-05-31 15:34:51 +00001802 break;
1803 }
drhf0863fe2005-06-12 21:35:51 +00001804 case TK_NULL: {
drh9de221d2008-01-05 06:51:30 +00001805 sqlite3VdbeAddOp2(v, OP_Null, 0, target);
drhf0863fe2005-06-12 21:35:51 +00001806 break;
1807 }
danielk19775338a5f2005-01-20 13:03:10 +00001808#ifndef SQLITE_OMIT_BLOB_LITERAL
danielk1977c572ef72004-05-27 09:28:41 +00001809 case TK_BLOB: {
drh6c8c6ce2005-08-23 11:17:58 +00001810 int n;
1811 const char *z;
drhca48c902008-01-18 14:08:24 +00001812 char *zBlob;
1813 assert( pExpr->token.n>=3 );
1814 assert( pExpr->token.z[0]=='x' || pExpr->token.z[0]=='X' );
1815 assert( pExpr->token.z[1]=='\'' );
1816 assert( pExpr->token.z[pExpr->token.n-1]=='\'' );
drh6c8c6ce2005-08-23 11:17:58 +00001817 n = pExpr->token.n - 3;
drh2646da72005-12-09 20:02:05 +00001818 z = (char*)pExpr->token.z + 2;
drhca48c902008-01-18 14:08:24 +00001819 zBlob = sqlite3HexToBlob(sqlite3VdbeDb(v), z, n);
1820 sqlite3VdbeAddOp4(v, OP_Blob, n/2, target, 0, zBlob, P4_DYNAMIC);
danielk1977c572ef72004-05-27 09:28:41 +00001821 break;
1822 }
danielk19775338a5f2005-01-20 13:03:10 +00001823#endif
drh50457892003-09-06 01:10:47 +00001824 case TK_VARIABLE: {
drh9de221d2008-01-05 06:51:30 +00001825 sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iTable, target);
drh895d7472004-08-20 16:02:39 +00001826 if( pExpr->token.n>1 ){
drh66a51672008-01-03 00:01:23 +00001827 sqlite3VdbeChangeP4(v, -1, (char*)pExpr->token.z, pExpr->token.n);
drh895d7472004-08-20 16:02:39 +00001828 }
drh50457892003-09-06 01:10:47 +00001829 break;
1830 }
drh4e0cff62004-11-05 05:10:28 +00001831 case TK_REGISTER: {
drh9de221d2008-01-05 06:51:30 +00001832 inReg = pExpr->iTable;
drh4e0cff62004-11-05 05:10:28 +00001833 break;
1834 }
drh8b213892008-08-29 02:14:02 +00001835 case TK_AS: {
drh31daa632008-10-25 15:03:20 +00001836 inReg = codeAlias(pParse, pExpr->iTable, pExpr->pLeft, target);
drh8b213892008-08-29 02:14:02 +00001837 break;
1838 }
drh487e2622005-06-25 18:42:14 +00001839#ifndef SQLITE_OMIT_CAST
1840 case TK_CAST: {
1841 /* Expressions of the form: CAST(pLeft AS token) */
danielk1977f0113002006-01-24 12:09:17 +00001842 int aff, to_op;
drh2dcef112008-01-12 19:03:48 +00001843 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
drh8a512562005-11-14 22:29:05 +00001844 aff = sqlite3AffinityType(&pExpr->token);
danielk1977f0113002006-01-24 12:09:17 +00001845 to_op = aff - SQLITE_AFF_TEXT + OP_ToText;
1846 assert( to_op==OP_ToText || aff!=SQLITE_AFF_TEXT );
1847 assert( to_op==OP_ToBlob || aff!=SQLITE_AFF_NONE );
1848 assert( to_op==OP_ToNumeric || aff!=SQLITE_AFF_NUMERIC );
1849 assert( to_op==OP_ToInt || aff!=SQLITE_AFF_INTEGER );
1850 assert( to_op==OP_ToReal || aff!=SQLITE_AFF_REAL );
drhc5499be2008-04-01 15:06:33 +00001851 testcase( to_op==OP_ToText );
1852 testcase( to_op==OP_ToBlob );
1853 testcase( to_op==OP_ToNumeric );
1854 testcase( to_op==OP_ToInt );
1855 testcase( to_op==OP_ToReal );
drh1735fa82008-11-06 15:33:03 +00001856 if( inReg!=target ){
1857 sqlite3VdbeAddOp2(v, OP_SCopy, inReg, target);
1858 inReg = target;
1859 }
drh2dcef112008-01-12 19:03:48 +00001860 sqlite3VdbeAddOp1(v, to_op, inReg);
drhc5499be2008-04-01 15:06:33 +00001861 testcase( usedAsColumnCache(pParse, inReg, inReg) );
drhb3843a82008-04-01 12:24:11 +00001862 sqlite3ExprCacheAffinityChange(pParse, inReg, 1);
drh487e2622005-06-25 18:42:14 +00001863 break;
1864 }
1865#endif /* SQLITE_OMIT_CAST */
drhc9b84a12002-06-20 11:36:48 +00001866 case TK_LT:
1867 case TK_LE:
1868 case TK_GT:
1869 case TK_GE:
1870 case TK_NE:
1871 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00001872 assert( TK_LT==OP_Lt );
1873 assert( TK_LE==OP_Le );
1874 assert( TK_GT==OP_Gt );
1875 assert( TK_GE==OP_Ge );
1876 assert( TK_EQ==OP_Eq );
1877 assert( TK_NE==OP_Ne );
drhc5499be2008-04-01 15:06:33 +00001878 testcase( op==TK_LT );
1879 testcase( op==TK_LE );
1880 testcase( op==TK_GT );
1881 testcase( op==TK_GE );
1882 testcase( op==TK_EQ );
1883 testcase( op==TK_NE );
drhda250ea2008-04-01 05:07:14 +00001884 codeCompareOperands(pParse, pExpr->pLeft, &r1, &regFree1,
1885 pExpr->pRight, &r2, &regFree2);
drh35573352008-01-08 23:54:25 +00001886 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
1887 r1, r2, inReg, SQLITE_STOREP2);
drhc5499be2008-04-01 15:06:33 +00001888 testcase( regFree1==0 );
1889 testcase( regFree2==0 );
danielk1977a37cdde2004-05-16 11:15:36 +00001890 break;
drhc9b84a12002-06-20 11:36:48 +00001891 }
drhcce7d172000-05-31 15:34:51 +00001892 case TK_AND:
1893 case TK_OR:
1894 case TK_PLUS:
1895 case TK_STAR:
1896 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00001897 case TK_REM:
1898 case TK_BITAND:
1899 case TK_BITOR:
drh17c40292004-07-21 02:53:29 +00001900 case TK_SLASH:
drhbf4133c2001-10-13 02:59:08 +00001901 case TK_LSHIFT:
drh855eb1c2004-08-31 13:45:11 +00001902 case TK_RSHIFT:
drh00400772000-06-16 20:51:26 +00001903 case TK_CONCAT: {
drhf2bc0132004-10-04 13:19:23 +00001904 assert( TK_AND==OP_And );
1905 assert( TK_OR==OP_Or );
1906 assert( TK_PLUS==OP_Add );
1907 assert( TK_MINUS==OP_Subtract );
1908 assert( TK_REM==OP_Remainder );
1909 assert( TK_BITAND==OP_BitAnd );
1910 assert( TK_BITOR==OP_BitOr );
1911 assert( TK_SLASH==OP_Divide );
1912 assert( TK_LSHIFT==OP_ShiftLeft );
1913 assert( TK_RSHIFT==OP_ShiftRight );
1914 assert( TK_CONCAT==OP_Concat );
drhc5499be2008-04-01 15:06:33 +00001915 testcase( op==TK_AND );
1916 testcase( op==TK_OR );
1917 testcase( op==TK_PLUS );
1918 testcase( op==TK_MINUS );
1919 testcase( op==TK_REM );
1920 testcase( op==TK_BITAND );
1921 testcase( op==TK_BITOR );
1922 testcase( op==TK_SLASH );
1923 testcase( op==TK_LSHIFT );
1924 testcase( op==TK_RSHIFT );
1925 testcase( op==TK_CONCAT );
drh2dcef112008-01-12 19:03:48 +00001926 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
1927 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
drh5b6afba2008-01-05 16:29:28 +00001928 sqlite3VdbeAddOp3(v, op, r2, r1, target);
drhc5499be2008-04-01 15:06:33 +00001929 testcase( regFree1==0 );
1930 testcase( regFree2==0 );
drh00400772000-06-16 20:51:26 +00001931 break;
1932 }
drhcce7d172000-05-31 15:34:51 +00001933 case TK_UMINUS: {
drhfec19aa2004-05-19 20:41:03 +00001934 Expr *pLeft = pExpr->pLeft;
1935 assert( pLeft );
1936 if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
drhfec19aa2004-05-19 20:41:03 +00001937 if( pLeft->op==TK_FLOAT ){
drh92b01d52008-06-24 00:32:35 +00001938 codeReal(v, (char*)pLeft->token.z, pLeft->token.n, 1, target);
drhe6840902002-03-06 03:08:25 +00001939 }else{
drh92b01d52008-06-24 00:32:35 +00001940 codeInteger(v, pLeft, 1, target);
drhe6840902002-03-06 03:08:25 +00001941 }
drh3c84ddf2008-01-09 02:15:38 +00001942 }else{
drh2dcef112008-01-12 19:03:48 +00001943 regFree1 = r1 = sqlite3GetTempReg(pParse);
drh3c84ddf2008-01-09 02:15:38 +00001944 sqlite3VdbeAddOp2(v, OP_Integer, 0, r1);
drhe55cbd72008-03-31 23:48:03 +00001945 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree2);
drh2dcef112008-01-12 19:03:48 +00001946 sqlite3VdbeAddOp3(v, OP_Subtract, r2, r1, target);
drhc5499be2008-04-01 15:06:33 +00001947 testcase( regFree2==0 );
drh6e142f52000-06-08 13:36:40 +00001948 }
drh3c84ddf2008-01-09 02:15:38 +00001949 inReg = target;
1950 break;
drh6e142f52000-06-08 13:36:40 +00001951 }
drhbf4133c2001-10-13 02:59:08 +00001952 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00001953 case TK_NOT: {
drhf2bc0132004-10-04 13:19:23 +00001954 assert( TK_BITNOT==OP_BitNot );
1955 assert( TK_NOT==OP_Not );
drhc5499be2008-04-01 15:06:33 +00001956 testcase( op==TK_BITNOT );
1957 testcase( op==TK_NOT );
drhe99fa2a2008-12-15 15:27:51 +00001958 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
1959 testcase( regFree1==0 );
1960 inReg = target;
1961 sqlite3VdbeAddOp2(v, op, r1, inReg);
drhcce7d172000-05-31 15:34:51 +00001962 break;
1963 }
1964 case TK_ISNULL:
1965 case TK_NOTNULL: {
drh6a288a32008-01-07 19:20:24 +00001966 int addr;
drhf2bc0132004-10-04 13:19:23 +00001967 assert( TK_ISNULL==OP_IsNull );
1968 assert( TK_NOTNULL==OP_NotNull );
drhc5499be2008-04-01 15:06:33 +00001969 testcase( op==TK_ISNULL );
1970 testcase( op==TK_NOTNULL );
drh9de221d2008-01-05 06:51:30 +00001971 sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
drh2dcef112008-01-12 19:03:48 +00001972 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
drhc5499be2008-04-01 15:06:33 +00001973 testcase( regFree1==0 );
drh2dcef112008-01-12 19:03:48 +00001974 addr = sqlite3VdbeAddOp1(v, op, r1);
drh9de221d2008-01-05 06:51:30 +00001975 sqlite3VdbeAddOp2(v, OP_AddImm, target, -1);
drh6a288a32008-01-07 19:20:24 +00001976 sqlite3VdbeJumpHere(v, addr);
drhf2bc0132004-10-04 13:19:23 +00001977 break;
drhcce7d172000-05-31 15:34:51 +00001978 }
drh22827922000-06-06 17:27:05 +00001979 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00001980 AggInfo *pInfo = pExpr->pAggInfo;
drh7e56e712005-11-16 12:53:15 +00001981 if( pInfo==0 ){
1982 sqlite3ErrorMsg(pParse, "misuse of aggregate: %T",
1983 &pExpr->span);
1984 }else{
drh9de221d2008-01-05 06:51:30 +00001985 inReg = pInfo->aFunc[pExpr->iAgg].iMem;
drh7e56e712005-11-16 12:53:15 +00001986 }
drh22827922000-06-06 17:27:05 +00001987 break;
1988 }
drhb71090f2005-05-23 17:26:51 +00001989 case TK_CONST_FUNC:
drhcce7d172000-05-31 15:34:51 +00001990 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00001991 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00001992 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00001993 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00001994 int nId;
1995 const char *zId;
drh13449892005-09-07 21:22:45 +00001996 int constMask = 0;
danielk1977682f68b2004-06-05 10:22:17 +00001997 int i;
drh17435752007-08-16 04:30:38 +00001998 u8 enc = ENC(db);
danielk1977dc1bdc42004-06-11 10:51:27 +00001999 CollSeq *pColl = 0;
drh17435752007-08-16 04:30:38 +00002000
drhc5499be2008-04-01 15:06:33 +00002001 testcase( op==TK_CONST_FUNC );
2002 testcase( op==TK_FUNCTION );
drh2646da72005-12-09 20:02:05 +00002003 zId = (char*)pExpr->token.z;
drhb71090f2005-05-23 17:26:51 +00002004 nId = pExpr->token.n;
drh8b213892008-08-29 02:14:02 +00002005 pDef = sqlite3FindFunction(db, zId, nId, nExpr, enc, 0);
drh0bce8352002-02-28 00:41:10 +00002006 assert( pDef!=0 );
drh892d3172008-01-10 03:46:36 +00002007 if( pList ){
2008 nExpr = pList->nExpr;
drh2dcef112008-01-12 19:03:48 +00002009 r1 = sqlite3GetTempRange(pParse, nExpr);
drh191b54c2008-04-15 12:14:21 +00002010 sqlite3ExprCodeExprList(pParse, pList, r1, 1);
drh892d3172008-01-10 03:46:36 +00002011 }else{
drhd847eaa2008-01-17 17:15:56 +00002012 nExpr = r1 = 0;
drh892d3172008-01-10 03:46:36 +00002013 }
drhb7f6f682006-07-08 17:06:43 +00002014#ifndef SQLITE_OMIT_VIRTUALTABLE
drha43fa222006-07-08 18:41:37 +00002015 /* Possibly overload the function if the first argument is
2016 ** a virtual table column.
2017 **
2018 ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the
2019 ** second argument, not the first, as the argument to test to
2020 ** see if it is a column in a virtual table. This is done because
2021 ** the left operand of infix functions (the operand we want to
2022 ** control overloading) ends up as the second argument to the
2023 ** function. The expression "A glob B" is equivalent to
2024 ** "glob(B,A). We want to use the A in "A glob B" to test
2025 ** for function overloading. But we use the B term in "glob(B,A)".
2026 */
drh6a03a1c2006-07-08 18:34:59 +00002027 if( nExpr>=2 && (pExpr->flags & EP_InfixFunc) ){
drh17435752007-08-16 04:30:38 +00002028 pDef = sqlite3VtabOverloadFunction(db, pDef, nExpr, pList->a[1].pExpr);
drh6a03a1c2006-07-08 18:34:59 +00002029 }else if( nExpr>0 ){
drh17435752007-08-16 04:30:38 +00002030 pDef = sqlite3VtabOverloadFunction(db, pDef, nExpr, pList->a[0].pExpr);
drhb7f6f682006-07-08 17:06:43 +00002031 }
2032#endif
danielk1977682f68b2004-06-05 10:22:17 +00002033 for(i=0; i<nExpr && i<32; i++){
danielk1977d02eb1f2004-06-06 09:44:03 +00002034 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
drh13449892005-09-07 21:22:45 +00002035 constMask |= (1<<i);
danielk1977d02eb1f2004-06-06 09:44:03 +00002036 }
drhe82f5d02008-10-07 19:53:14 +00002037 if( (pDef->flags & SQLITE_FUNC_NEEDCOLL)!=0 && !pColl ){
danielk1977dc1bdc42004-06-11 10:51:27 +00002038 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
2039 }
2040 }
drhe82f5d02008-10-07 19:53:14 +00002041 if( pDef->flags & SQLITE_FUNC_NEEDCOLL ){
drh8b213892008-08-29 02:14:02 +00002042 if( !pColl ) pColl = db->pDfltColl;
drh66a51672008-01-03 00:01:23 +00002043 sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ);
danielk1977682f68b2004-06-05 10:22:17 +00002044 }
drh2dcef112008-01-12 19:03:48 +00002045 sqlite3VdbeAddOp4(v, OP_Function, constMask, r1, target,
drh66a51672008-01-03 00:01:23 +00002046 (char*)pDef, P4_FUNCDEF);
drh1bd10f82008-12-10 21:19:56 +00002047 sqlite3VdbeChangeP5(v, (u8)nExpr);
drh2dcef112008-01-12 19:03:48 +00002048 if( nExpr ){
2049 sqlite3ReleaseTempRange(pParse, r1, nExpr);
2050 }
drhda250ea2008-04-01 05:07:14 +00002051 sqlite3ExprCacheAffinityChange(pParse, r1, nExpr);
drhcce7d172000-05-31 15:34:51 +00002052 break;
2053 }
drhfe2093d2005-01-20 22:48:47 +00002054#ifndef SQLITE_OMIT_SUBQUERY
2055 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00002056 case TK_SELECT: {
drhc5499be2008-04-01 15:06:33 +00002057 testcase( op==TK_EXISTS );
2058 testcase( op==TK_SELECT );
drh41714d62006-03-02 04:44:23 +00002059 if( pExpr->iColumn==0 ){
danielk197741a05b72008-10-02 13:50:55 +00002060 sqlite3CodeSubselect(pParse, pExpr, 0, 0);
drh41714d62006-03-02 04:44:23 +00002061 }
drh9de221d2008-01-05 06:51:30 +00002062 inReg = pExpr->iColumn;
drh19a775c2000-06-05 18:54:46 +00002063 break;
2064 }
drhfef52082000-06-06 01:50:43 +00002065 case TK_IN: {
danielk19770cdc0222008-06-26 18:04:03 +00002066 int rNotFound = 0;
2067 int rMayHaveNull = 0;
drh6fccc352008-06-27 00:52:45 +00002068 int j2, j3, j4, j5;
drh94a11212004-09-25 13:12:14 +00002069 char affinity;
danielk19779a96b662007-11-29 17:05:18 +00002070 int eType;
danielk19779a96b662007-11-29 17:05:18 +00002071
drh3c31fc22008-06-26 21:45:26 +00002072 VdbeNoopComment((v, "begin IN expr r%d", target));
danielk19770cdc0222008-06-26 18:04:03 +00002073 eType = sqlite3FindInIndex(pParse, pExpr, &rMayHaveNull);
2074 if( rMayHaveNull ){
2075 rNotFound = ++pParse->nMem;
2076 }
danielk1977e014a832004-05-17 10:48:57 +00002077
2078 /* Figure out the affinity to use to create a key from the results
2079 ** of the expression. affinityStr stores a static string suitable for
drh66a51672008-01-03 00:01:23 +00002080 ** P4 of OP_MakeRecord.
danielk1977e014a832004-05-17 10:48:57 +00002081 */
drh94a11212004-09-25 13:12:14 +00002082 affinity = comparisonAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +00002083
danielk1977e014a832004-05-17 10:48:57 +00002084
2085 /* Code the <expr> from "<expr> IN (...)". The temporary table
2086 ** pExpr->iTable contains the values that make up the (...) set.
2087 */
drh66ba23c2008-06-27 00:47:28 +00002088 pParse->disableColCache++;
2089 sqlite3ExprCode(pParse, pExpr->pLeft, target);
2090 pParse->disableColCache--;
2091 j2 = sqlite3VdbeAddOp1(v, OP_IsNull, target);
danielk19779a96b662007-11-29 17:05:18 +00002092 if( eType==IN_INDEX_ROWID ){
drh66ba23c2008-06-27 00:47:28 +00002093 j3 = sqlite3VdbeAddOp1(v, OP_MustBeInt, target);
2094 j4 = sqlite3VdbeAddOp3(v, OP_NotExists, pExpr->iTable, 0, target);
2095 sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
drh6a288a32008-01-07 19:20:24 +00002096 j5 = sqlite3VdbeAddOp0(v, OP_Goto);
2097 sqlite3VdbeJumpHere(v, j3);
2098 sqlite3VdbeJumpHere(v, j4);
danielk19770cdc0222008-06-26 18:04:03 +00002099 sqlite3VdbeAddOp2(v, OP_Integer, 0, target);
danielk19779a96b662007-11-29 17:05:18 +00002100 }else{
drh2dcef112008-01-12 19:03:48 +00002101 r2 = regFree2 = sqlite3GetTempReg(pParse);
danielk19770cdc0222008-06-26 18:04:03 +00002102
2103 /* Create a record and test for set membership. If the set contains
2104 ** the value, then jump to the end of the test code. The target
2105 ** register still contains the true (1) value written to it earlier.
2106 */
drh66ba23c2008-06-27 00:47:28 +00002107 sqlite3VdbeAddOp4(v, OP_MakeRecord, target, 1, r2, &affinity, 1);
2108 sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
drh2dcef112008-01-12 19:03:48 +00002109 j5 = sqlite3VdbeAddOp3(v, OP_Found, pExpr->iTable, 0, r2);
danielk19770cdc0222008-06-26 18:04:03 +00002110
2111 /* If the set membership test fails, then the result of the
2112 ** "x IN (...)" expression must be either 0 or NULL. If the set
2113 ** contains no NULL values, then the result is 0. If the set
2114 ** contains one or more NULL values, then the result of the
2115 ** expression is also NULL.
2116 */
2117 if( rNotFound==0 ){
2118 /* This branch runs if it is known at compile time (now) that
2119 ** the set contains no NULL values. This happens as the result
2120 ** of a "NOT NULL" constraint in the database schema. No need
2121 ** to test the data structure at runtime in this case.
2122 */
2123 sqlite3VdbeAddOp2(v, OP_Integer, 0, target);
2124 }else{
2125 /* This block populates the rNotFound register with either NULL
2126 ** or 0 (an integer value). If the data structure contains one
2127 ** or more NULLs, then set rNotFound to NULL. Otherwise, set it
2128 ** to 0. If register rMayHaveNull is already set to some value
2129 ** other than NULL, then the test has already been run and
2130 ** rNotFound is already populated.
2131 */
drh66ba23c2008-06-27 00:47:28 +00002132 static const char nullRecord[] = { 0x02, 0x00 };
danielk19770cdc0222008-06-26 18:04:03 +00002133 j3 = sqlite3VdbeAddOp1(v, OP_NotNull, rMayHaveNull);
2134 sqlite3VdbeAddOp2(v, OP_Null, 0, rNotFound);
drh66ba23c2008-06-27 00:47:28 +00002135 sqlite3VdbeAddOp4(v, OP_Blob, 2, rMayHaveNull, 0,
2136 nullRecord, P4_STATIC);
2137 j4 = sqlite3VdbeAddOp3(v, OP_Found, pExpr->iTable, 0, rMayHaveNull);
danielk19770cdc0222008-06-26 18:04:03 +00002138 sqlite3VdbeAddOp2(v, OP_Integer, 0, rNotFound);
2139 sqlite3VdbeJumpHere(v, j4);
2140 sqlite3VdbeJumpHere(v, j3);
2141
2142 /* Copy the value of register rNotFound (which is either NULL or 0)
danielk197741a05b72008-10-02 13:50:55 +00002143 ** into the target register. This will be the result of the
danielk19770cdc0222008-06-26 18:04:03 +00002144 ** expression.
2145 */
2146 sqlite3VdbeAddOp2(v, OP_Copy, rNotFound, target);
2147 }
danielk19779a96b662007-11-29 17:05:18 +00002148 }
drh6a288a32008-01-07 19:20:24 +00002149 sqlite3VdbeJumpHere(v, j2);
2150 sqlite3VdbeJumpHere(v, j5);
drh3c31fc22008-06-26 21:45:26 +00002151 VdbeComment((v, "end IN expr r%d", target));
drhfef52082000-06-06 01:50:43 +00002152 break;
2153 }
danielk197793758c82005-01-21 08:13:14 +00002154#endif
drh2dcef112008-01-12 19:03:48 +00002155 /*
2156 ** x BETWEEN y AND z
2157 **
2158 ** This is equivalent to
2159 **
2160 ** x>=y AND x<=z
2161 **
2162 ** X is stored in pExpr->pLeft.
2163 ** Y is stored in pExpr->pList->a[0].pExpr.
2164 ** Z is stored in pExpr->pList->a[1].pExpr.
2165 */
drhfef52082000-06-06 01:50:43 +00002166 case TK_BETWEEN: {
drhbe5c89a2004-07-26 00:31:09 +00002167 Expr *pLeft = pExpr->pLeft;
2168 struct ExprList_item *pLItem = pExpr->pList->a;
2169 Expr *pRight = pLItem->pExpr;
drh35573352008-01-08 23:54:25 +00002170
drhda250ea2008-04-01 05:07:14 +00002171 codeCompareOperands(pParse, pLeft, &r1, &regFree1,
2172 pRight, &r2, &regFree2);
drhc5499be2008-04-01 15:06:33 +00002173 testcase( regFree1==0 );
2174 testcase( regFree2==0 );
drh2dcef112008-01-12 19:03:48 +00002175 r3 = sqlite3GetTempReg(pParse);
drh678ccce2008-03-31 18:19:54 +00002176 r4 = sqlite3GetTempReg(pParse);
drh35573352008-01-08 23:54:25 +00002177 codeCompare(pParse, pLeft, pRight, OP_Ge,
2178 r1, r2, r3, SQLITE_STOREP2);
drhbe5c89a2004-07-26 00:31:09 +00002179 pLItem++;
2180 pRight = pLItem->pExpr;
drh2dcef112008-01-12 19:03:48 +00002181 sqlite3ReleaseTempReg(pParse, regFree2);
2182 r2 = sqlite3ExprCodeTemp(pParse, pRight, &regFree2);
drhc5499be2008-04-01 15:06:33 +00002183 testcase( regFree2==0 );
drh678ccce2008-03-31 18:19:54 +00002184 codeCompare(pParse, pLeft, pRight, OP_Le, r1, r2, r4, SQLITE_STOREP2);
2185 sqlite3VdbeAddOp3(v, OP_And, r3, r4, target);
drh2dcef112008-01-12 19:03:48 +00002186 sqlite3ReleaseTempReg(pParse, r3);
drh678ccce2008-03-31 18:19:54 +00002187 sqlite3ReleaseTempReg(pParse, r4);
drhfef52082000-06-06 01:50:43 +00002188 break;
2189 }
drh4f07e5f2007-05-14 11:34:46 +00002190 case TK_UPLUS: {
drh2dcef112008-01-12 19:03:48 +00002191 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
drha2e00042002-01-22 03:13:42 +00002192 break;
2193 }
drh2dcef112008-01-12 19:03:48 +00002194
2195 /*
2196 ** Form A:
2197 ** CASE x WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
2198 **
2199 ** Form B:
2200 ** CASE WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
2201 **
2202 ** Form A is can be transformed into the equivalent form B as follows:
2203 ** CASE WHEN x=e1 THEN r1 WHEN x=e2 THEN r2 ...
2204 ** WHEN x=eN THEN rN ELSE y END
2205 **
2206 ** X (if it exists) is in pExpr->pLeft.
2207 ** Y is in pExpr->pRight. The Y is also optional. If there is no
2208 ** ELSE clause and no other term matches, then the result of the
2209 ** exprssion is NULL.
2210 ** Ei is in pExpr->pList->a[i*2] and Ri is pExpr->pList->a[i*2+1].
2211 **
2212 ** The result of the expression is the Ri for the first matching Ei,
2213 ** or if there is no matching Ei, the ELSE term Y, or if there is
2214 ** no ELSE term, NULL.
2215 */
drh17a7f8d2002-03-24 13:13:27 +00002216 case TK_CASE: {
drh2dcef112008-01-12 19:03:48 +00002217 int endLabel; /* GOTO label for end of CASE stmt */
2218 int nextCase; /* GOTO label for next WHEN clause */
2219 int nExpr; /* 2x number of WHEN terms */
2220 int i; /* Loop counter */
2221 ExprList *pEList; /* List of WHEN terms */
2222 struct ExprList_item *aListelem; /* Array of WHEN terms */
2223 Expr opCompare; /* The X==Ei expression */
2224 Expr cacheX; /* Cached expression X */
2225 Expr *pX; /* The X expression */
drh1bd10f82008-12-10 21:19:56 +00002226 Expr *pTest = 0; /* X==Ei (form A) or just Ei (form B) */
drh17a7f8d2002-03-24 13:13:27 +00002227
2228 assert(pExpr->pList);
2229 assert((pExpr->pList->nExpr % 2) == 0);
2230 assert(pExpr->pList->nExpr > 0);
drhbe5c89a2004-07-26 00:31:09 +00002231 pEList = pExpr->pList;
2232 aListelem = pEList->a;
2233 nExpr = pEList->nExpr;
drh2dcef112008-01-12 19:03:48 +00002234 endLabel = sqlite3VdbeMakeLabel(v);
2235 if( (pX = pExpr->pLeft)!=0 ){
2236 cacheX = *pX;
drhc5499be2008-04-01 15:06:33 +00002237 testcase( pX->op==TK_COLUMN || pX->op==TK_REGISTER );
drh2dcef112008-01-12 19:03:48 +00002238 cacheX.iTable = sqlite3ExprCodeTemp(pParse, pX, &regFree1);
drhc5499be2008-04-01 15:06:33 +00002239 testcase( regFree1==0 );
drh2dcef112008-01-12 19:03:48 +00002240 cacheX.op = TK_REGISTER;
2241 opCompare.op = TK_EQ;
2242 opCompare.pLeft = &cacheX;
2243 pTest = &opCompare;
drh17a7f8d2002-03-24 13:13:27 +00002244 }
drhc5499be2008-04-01 15:06:33 +00002245 pParse->disableColCache++;
drhf5905aa2002-05-26 20:54:33 +00002246 for(i=0; i<nExpr; i=i+2){
drh2dcef112008-01-12 19:03:48 +00002247 if( pX ){
drh1bd10f82008-12-10 21:19:56 +00002248 assert( pTest!=0 );
drh2dcef112008-01-12 19:03:48 +00002249 opCompare.pRight = aListelem[i].pExpr;
drh17a7f8d2002-03-24 13:13:27 +00002250 }else{
drh2dcef112008-01-12 19:03:48 +00002251 pTest = aListelem[i].pExpr;
drh17a7f8d2002-03-24 13:13:27 +00002252 }
drh2dcef112008-01-12 19:03:48 +00002253 nextCase = sqlite3VdbeMakeLabel(v);
drhc5499be2008-04-01 15:06:33 +00002254 testcase( pTest->op==TK_COLUMN || pTest->op==TK_REGISTER );
drh2dcef112008-01-12 19:03:48 +00002255 sqlite3ExprIfFalse(pParse, pTest, nextCase, SQLITE_JUMPIFNULL);
drhc5499be2008-04-01 15:06:33 +00002256 testcase( aListelem[i+1].pExpr->op==TK_COLUMN );
2257 testcase( aListelem[i+1].pExpr->op==TK_REGISTER );
drh9de221d2008-01-05 06:51:30 +00002258 sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target);
drh2dcef112008-01-12 19:03:48 +00002259 sqlite3VdbeAddOp2(v, OP_Goto, 0, endLabel);
2260 sqlite3VdbeResolveLabel(v, nextCase);
drhf570f012002-05-31 15:51:25 +00002261 }
drh17a7f8d2002-03-24 13:13:27 +00002262 if( pExpr->pRight ){
drh9de221d2008-01-05 06:51:30 +00002263 sqlite3ExprCode(pParse, pExpr->pRight, target);
drh17a7f8d2002-03-24 13:13:27 +00002264 }else{
drh9de221d2008-01-05 06:51:30 +00002265 sqlite3VdbeAddOp2(v, OP_Null, 0, target);
drh17a7f8d2002-03-24 13:13:27 +00002266 }
drh2dcef112008-01-12 19:03:48 +00002267 sqlite3VdbeResolveLabel(v, endLabel);
drhc5499be2008-04-01 15:06:33 +00002268 assert( pParse->disableColCache>0 );
2269 pParse->disableColCache--;
danielk19776f349032002-06-11 02:25:40 +00002270 break;
2271 }
danielk19775338a5f2005-01-20 13:03:10 +00002272#ifndef SQLITE_OMIT_TRIGGER
danielk19776f349032002-06-11 02:25:40 +00002273 case TK_RAISE: {
2274 if( !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +00002275 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00002276 "RAISE() may only be used within a trigger-program");
drh389a1ad2008-01-03 23:44:53 +00002277 return 0;
danielk19776f349032002-06-11 02:25:40 +00002278 }
drhad6d9462004-09-19 02:15:24 +00002279 if( pExpr->iColumn!=OE_Ignore ){
2280 assert( pExpr->iColumn==OE_Rollback ||
2281 pExpr->iColumn == OE_Abort ||
2282 pExpr->iColumn == OE_Fail );
drh8b213892008-08-29 02:14:02 +00002283 sqlite3DequoteExpr(db, pExpr);
drh66a51672008-01-03 00:01:23 +00002284 sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn, 0,
drh2646da72005-12-09 20:02:05 +00002285 (char*)pExpr->token.z, pExpr->token.n);
danielk19776f349032002-06-11 02:25:40 +00002286 } else {
drhad6d9462004-09-19 02:15:24 +00002287 assert( pExpr->iColumn == OE_Ignore );
drh66a51672008-01-03 00:01:23 +00002288 sqlite3VdbeAddOp2(v, OP_ContextPop, 0, 0);
2289 sqlite3VdbeAddOp2(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
drhd4e70eb2008-01-02 00:34:36 +00002290 VdbeComment((v, "raise(IGNORE)"));
danielk19776f349032002-06-11 02:25:40 +00002291 }
drhffe07b22005-11-03 00:41:17 +00002292 break;
drh17a7f8d2002-03-24 13:13:27 +00002293 }
danielk19775338a5f2005-01-20 13:03:10 +00002294#endif
drhffe07b22005-11-03 00:41:17 +00002295 }
drh2dcef112008-01-12 19:03:48 +00002296 sqlite3ReleaseTempReg(pParse, regFree1);
2297 sqlite3ReleaseTempReg(pParse, regFree2);
2298 return inReg;
2299}
2300
2301/*
2302** Generate code to evaluate an expression and store the results
2303** into a register. Return the register number where the results
2304** are stored.
2305**
2306** If the register is a temporary register that can be deallocated,
drh678ccce2008-03-31 18:19:54 +00002307** then write its number into *pReg. If the result register is not
drh2dcef112008-01-12 19:03:48 +00002308** a temporary, then set *pReg to zero.
2309*/
2310int sqlite3ExprCodeTemp(Parse *pParse, Expr *pExpr, int *pReg){
2311 int r1 = sqlite3GetTempReg(pParse);
2312 int r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
2313 if( r2==r1 ){
2314 *pReg = r1;
2315 }else{
2316 sqlite3ReleaseTempReg(pParse, r1);
2317 *pReg = 0;
2318 }
2319 return r2;
2320}
2321
2322/*
2323** Generate code that will evaluate expression pExpr and store the
2324** results in register target. The results are guaranteed to appear
2325** in register target.
2326*/
2327int sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){
drh9cbf3422008-01-17 16:22:13 +00002328 int inReg;
2329
2330 assert( target>0 && target<=pParse->nMem );
2331 inReg = sqlite3ExprCodeTarget(pParse, pExpr, target);
drh0e359b32008-01-13 19:02:11 +00002332 assert( pParse->pVdbe || pParse->db->mallocFailed );
2333 if( inReg!=target && pParse->pVdbe ){
drh9cbf3422008-01-17 16:22:13 +00002334 sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, inReg, target);
drhcce7d172000-05-31 15:34:51 +00002335 }
drh389a1ad2008-01-03 23:44:53 +00002336 return target;
drhcce7d172000-05-31 15:34:51 +00002337}
2338
2339/*
drh2dcef112008-01-12 19:03:48 +00002340** Generate code that evalutes the given expression and puts the result
drhde4fcfd2008-01-19 23:50:26 +00002341** in register target.
drh25303782004-12-07 15:41:48 +00002342**
drh2dcef112008-01-12 19:03:48 +00002343** Also make a copy of the expression results into another "cache" register
2344** and modify the expression so that the next time it is evaluated,
2345** the result is a copy of the cache register.
2346**
2347** This routine is used for expressions that are used multiple
2348** times. They are evaluated once and the results of the expression
2349** are reused.
drh25303782004-12-07 15:41:48 +00002350*/
drh2dcef112008-01-12 19:03:48 +00002351int sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr, int target){
drh25303782004-12-07 15:41:48 +00002352 Vdbe *v = pParse->pVdbe;
drh2dcef112008-01-12 19:03:48 +00002353 int inReg;
2354 inReg = sqlite3ExprCode(pParse, pExpr, target);
drhde4fcfd2008-01-19 23:50:26 +00002355 assert( target>0 );
drh2dcef112008-01-12 19:03:48 +00002356 if( pExpr->op!=TK_REGISTER ){
2357 int iMem;
drhde4fcfd2008-01-19 23:50:26 +00002358 iMem = ++pParse->nMem;
2359 sqlite3VdbeAddOp2(v, OP_Copy, inReg, iMem);
drh2dcef112008-01-12 19:03:48 +00002360 pExpr->iTable = iMem;
drh25303782004-12-07 15:41:48 +00002361 pExpr->op = TK_REGISTER;
2362 }
drh2dcef112008-01-12 19:03:48 +00002363 return inReg;
drh25303782004-12-07 15:41:48 +00002364}
drh2dcef112008-01-12 19:03:48 +00002365
drh678ccce2008-03-31 18:19:54 +00002366/*
drh47de9552008-04-01 18:04:11 +00002367** Return TRUE if pExpr is an constant expression that is appropriate
2368** for factoring out of a loop. Appropriate expressions are:
2369**
2370** * Any expression that evaluates to two or more opcodes.
2371**
2372** * Any OP_Integer, OP_Real, OP_String, OP_Blob, OP_Null,
2373** or OP_Variable that does not need to be placed in a
2374** specific register.
2375**
2376** There is no point in factoring out single-instruction constant
2377** expressions that need to be placed in a particular register.
2378** We could factor them out, but then we would end up adding an
2379** OP_SCopy instruction to move the value into the correct register
2380** later. We might as well just use the original instruction and
2381** avoid the OP_SCopy.
2382*/
2383static int isAppropriateForFactoring(Expr *p){
2384 if( !sqlite3ExprIsConstantNotJoin(p) ){
2385 return 0; /* Only constant expressions are appropriate for factoring */
2386 }
2387 if( (p->flags & EP_FixedDest)==0 ){
2388 return 1; /* Any constant without a fixed destination is appropriate */
2389 }
2390 while( p->op==TK_UPLUS ) p = p->pLeft;
2391 switch( p->op ){
2392#ifndef SQLITE_OMIT_BLOB_LITERAL
2393 case TK_BLOB:
2394#endif
2395 case TK_VARIABLE:
2396 case TK_INTEGER:
2397 case TK_FLOAT:
2398 case TK_NULL:
2399 case TK_STRING: {
2400 testcase( p->op==TK_BLOB );
2401 testcase( p->op==TK_VARIABLE );
2402 testcase( p->op==TK_INTEGER );
2403 testcase( p->op==TK_FLOAT );
2404 testcase( p->op==TK_NULL );
2405 testcase( p->op==TK_STRING );
2406 /* Single-instruction constants with a fixed destination are
2407 ** better done in-line. If we factor them, they will just end
2408 ** up generating an OP_SCopy to move the value to the destination
2409 ** register. */
2410 return 0;
2411 }
2412 case TK_UMINUS: {
2413 if( p->pLeft->op==TK_FLOAT || p->pLeft->op==TK_INTEGER ){
2414 return 0;
2415 }
2416 break;
2417 }
2418 default: {
2419 break;
2420 }
2421 }
2422 return 1;
2423}
2424
2425/*
2426** If pExpr is a constant expression that is appropriate for
2427** factoring out of a loop, then evaluate the expression
drh678ccce2008-03-31 18:19:54 +00002428** into a register and convert the expression into a TK_REGISTER
2429** expression.
2430*/
drh7d10d5a2008-08-20 16:35:10 +00002431static int evalConstExpr(Walker *pWalker, Expr *pExpr){
2432 Parse *pParse = pWalker->pParse;
drh47de9552008-04-01 18:04:11 +00002433 switch( pExpr->op ){
2434 case TK_REGISTER: {
2435 return 1;
2436 }
2437 case TK_FUNCTION:
2438 case TK_AGG_FUNCTION:
2439 case TK_CONST_FUNC: {
2440 /* The arguments to a function have a fixed destination.
2441 ** Mark them this way to avoid generated unneeded OP_SCopy
2442 ** instructions.
2443 */
2444 ExprList *pList = pExpr->pList;
2445 if( pList ){
2446 int i = pList->nExpr;
2447 struct ExprList_item *pItem = pList->a;
2448 for(; i>0; i--, pItem++){
2449 if( pItem->pExpr ) pItem->pExpr->flags |= EP_FixedDest;
2450 }
2451 }
2452 break;
2453 }
drh678ccce2008-03-31 18:19:54 +00002454 }
drh47de9552008-04-01 18:04:11 +00002455 if( isAppropriateForFactoring(pExpr) ){
drh678ccce2008-03-31 18:19:54 +00002456 int r1 = ++pParse->nMem;
2457 int r2;
2458 r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
drhc5499be2008-04-01 15:06:33 +00002459 if( r1!=r2 ) sqlite3ReleaseTempReg(pParse, r1);
drh678ccce2008-03-31 18:19:54 +00002460 pExpr->op = TK_REGISTER;
2461 pExpr->iTable = r2;
drh7d10d5a2008-08-20 16:35:10 +00002462 return WRC_Prune;
drh678ccce2008-03-31 18:19:54 +00002463 }
drh7d10d5a2008-08-20 16:35:10 +00002464 return WRC_Continue;
drh678ccce2008-03-31 18:19:54 +00002465}
2466
2467/*
2468** Preevaluate constant subexpressions within pExpr and store the
2469** results in registers. Modify pExpr so that the constant subexpresions
2470** are TK_REGISTER opcodes that refer to the precomputed values.
2471*/
2472void sqlite3ExprCodeConstants(Parse *pParse, Expr *pExpr){
drh7d10d5a2008-08-20 16:35:10 +00002473 Walker w;
2474 w.xExprCallback = evalConstExpr;
2475 w.xSelectCallback = 0;
2476 w.pParse = pParse;
2477 sqlite3WalkExpr(&w, pExpr);
drh678ccce2008-03-31 18:19:54 +00002478}
2479
drh25303782004-12-07 15:41:48 +00002480
2481/*
drh268380c2004-02-25 13:47:31 +00002482** Generate code that pushes the value of every element of the given
drh9cbf3422008-01-17 16:22:13 +00002483** expression list into a sequence of registers beginning at target.
drh268380c2004-02-25 13:47:31 +00002484**
drh892d3172008-01-10 03:46:36 +00002485** Return the number of elements evaluated.
drh268380c2004-02-25 13:47:31 +00002486*/
danielk19774adee202004-05-08 08:23:19 +00002487int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00002488 Parse *pParse, /* Parsing context */
drh389a1ad2008-01-03 23:44:53 +00002489 ExprList *pList, /* The expression list to be coded */
drh191b54c2008-04-15 12:14:21 +00002490 int target, /* Where to write results */
drhd1766112008-09-17 00:13:12 +00002491 int doHardCopy /* Make a hard copy of every element */
drh268380c2004-02-25 13:47:31 +00002492){
2493 struct ExprList_item *pItem;
drh9cbf3422008-01-17 16:22:13 +00002494 int i, n;
drh9d8b3072008-08-22 16:29:51 +00002495 assert( pList!=0 );
drh9cbf3422008-01-17 16:22:13 +00002496 assert( target>0 );
drh268380c2004-02-25 13:47:31 +00002497 n = pList->nExpr;
drh191b54c2008-04-15 12:14:21 +00002498 for(pItem=pList->a, i=0; i<n; i++, pItem++){
drh8b213892008-08-29 02:14:02 +00002499 if( pItem->iAlias ){
drh31daa632008-10-25 15:03:20 +00002500 int iReg = codeAlias(pParse, pItem->iAlias, pItem->pExpr, target+i);
drh8b213892008-08-29 02:14:02 +00002501 Vdbe *v = sqlite3GetVdbe(pParse);
drh31daa632008-10-25 15:03:20 +00002502 if( iReg!=target+i ){
2503 sqlite3VdbeAddOp2(v, OP_SCopy, iReg, target+i);
2504 }
drhd1766112008-09-17 00:13:12 +00002505 }else{
drh8b213892008-08-29 02:14:02 +00002506 sqlite3ExprCode(pParse, pItem->pExpr, target+i);
2507 }
drhd1766112008-09-17 00:13:12 +00002508 if( doHardCopy ){
2509 sqlite3ExprHardCopy(pParse, target, n);
2510 }
drh268380c2004-02-25 13:47:31 +00002511 }
drhf9b596e2004-05-26 16:54:42 +00002512 return n;
drh268380c2004-02-25 13:47:31 +00002513}
2514
2515/*
drhcce7d172000-05-31 15:34:51 +00002516** Generate code for a boolean expression such that a jump is made
2517** to the label "dest" if the expression is true but execution
2518** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00002519**
2520** If the expression evaluates to NULL (neither true nor false), then
drh35573352008-01-08 23:54:25 +00002521** take the jump if the jumpIfNull flag is SQLITE_JUMPIFNULL.
drhf2bc0132004-10-04 13:19:23 +00002522**
2523** This code depends on the fact that certain token values (ex: TK_EQ)
2524** are the same as opcode values (ex: OP_Eq) that implement the corresponding
2525** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
2526** the make process cause these values to align. Assert()s in the code
2527** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00002528*/
danielk19774adee202004-05-08 08:23:19 +00002529void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00002530 Vdbe *v = pParse->pVdbe;
2531 int op = 0;
drh2dcef112008-01-12 19:03:48 +00002532 int regFree1 = 0;
2533 int regFree2 = 0;
2534 int r1, r2;
2535
drh35573352008-01-08 23:54:25 +00002536 assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
drhdaffd0e2001-04-11 14:28:42 +00002537 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00002538 op = pExpr->op;
2539 switch( op ){
drhcce7d172000-05-31 15:34:51 +00002540 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00002541 int d2 = sqlite3VdbeMakeLabel(v);
drhc5499be2008-04-01 15:06:33 +00002542 testcase( jumpIfNull==0 );
2543 testcase( pParse->disableColCache==0 );
drh35573352008-01-08 23:54:25 +00002544 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,jumpIfNull^SQLITE_JUMPIFNULL);
drhe55cbd72008-03-31 23:48:03 +00002545 pParse->disableColCache++;
danielk19774adee202004-05-08 08:23:19 +00002546 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhc5499be2008-04-01 15:06:33 +00002547 assert( pParse->disableColCache>0 );
drhe55cbd72008-03-31 23:48:03 +00002548 pParse->disableColCache--;
danielk19774adee202004-05-08 08:23:19 +00002549 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00002550 break;
2551 }
2552 case TK_OR: {
drhc5499be2008-04-01 15:06:33 +00002553 testcase( jumpIfNull==0 );
2554 testcase( pParse->disableColCache==0 );
danielk19774adee202004-05-08 08:23:19 +00002555 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhe55cbd72008-03-31 23:48:03 +00002556 pParse->disableColCache++;
danielk19774adee202004-05-08 08:23:19 +00002557 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhc5499be2008-04-01 15:06:33 +00002558 assert( pParse->disableColCache>0 );
drhe55cbd72008-03-31 23:48:03 +00002559 pParse->disableColCache--;
drhcce7d172000-05-31 15:34:51 +00002560 break;
2561 }
2562 case TK_NOT: {
drhc5499be2008-04-01 15:06:33 +00002563 testcase( jumpIfNull==0 );
danielk19774adee202004-05-08 08:23:19 +00002564 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002565 break;
2566 }
2567 case TK_LT:
2568 case TK_LE:
2569 case TK_GT:
2570 case TK_GE:
2571 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00002572 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00002573 assert( TK_LT==OP_Lt );
2574 assert( TK_LE==OP_Le );
2575 assert( TK_GT==OP_Gt );
2576 assert( TK_GE==OP_Ge );
2577 assert( TK_EQ==OP_Eq );
2578 assert( TK_NE==OP_Ne );
drhc5499be2008-04-01 15:06:33 +00002579 testcase( op==TK_LT );
2580 testcase( op==TK_LE );
2581 testcase( op==TK_GT );
2582 testcase( op==TK_GE );
2583 testcase( op==TK_EQ );
2584 testcase( op==TK_NE );
2585 testcase( jumpIfNull==0 );
drhda250ea2008-04-01 05:07:14 +00002586 codeCompareOperands(pParse, pExpr->pLeft, &r1, &regFree1,
2587 pExpr->pRight, &r2, &regFree2);
drh35573352008-01-08 23:54:25 +00002588 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
drh2dcef112008-01-12 19:03:48 +00002589 r1, r2, dest, jumpIfNull);
drhc5499be2008-04-01 15:06:33 +00002590 testcase( regFree1==0 );
2591 testcase( regFree2==0 );
drhcce7d172000-05-31 15:34:51 +00002592 break;
2593 }
2594 case TK_ISNULL:
2595 case TK_NOTNULL: {
drhf2bc0132004-10-04 13:19:23 +00002596 assert( TK_ISNULL==OP_IsNull );
2597 assert( TK_NOTNULL==OP_NotNull );
drhc5499be2008-04-01 15:06:33 +00002598 testcase( op==TK_ISNULL );
2599 testcase( op==TK_NOTNULL );
drh2dcef112008-01-12 19:03:48 +00002600 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
2601 sqlite3VdbeAddOp2(v, op, r1, dest);
drhc5499be2008-04-01 15:06:33 +00002602 testcase( regFree1==0 );
drhcce7d172000-05-31 15:34:51 +00002603 break;
2604 }
drhfef52082000-06-06 01:50:43 +00002605 case TK_BETWEEN: {
drh2dcef112008-01-12 19:03:48 +00002606 /* x BETWEEN y AND z
danielk19770202b292004-06-09 09:55:16 +00002607 **
drh2dcef112008-01-12 19:03:48 +00002608 ** Is equivalent to
2609 **
2610 ** x>=y AND x<=z
2611 **
2612 ** Code it as such, taking care to do the common subexpression
2613 ** elementation of x.
danielk19770202b292004-06-09 09:55:16 +00002614 */
drh2dcef112008-01-12 19:03:48 +00002615 Expr exprAnd;
2616 Expr compLeft;
2617 Expr compRight;
2618 Expr exprX;
danielk19770202b292004-06-09 09:55:16 +00002619
drh2dcef112008-01-12 19:03:48 +00002620 exprX = *pExpr->pLeft;
2621 exprAnd.op = TK_AND;
2622 exprAnd.pLeft = &compLeft;
2623 exprAnd.pRight = &compRight;
2624 compLeft.op = TK_GE;
2625 compLeft.pLeft = &exprX;
2626 compLeft.pRight = pExpr->pList->a[0].pExpr;
2627 compRight.op = TK_LE;
2628 compRight.pLeft = &exprX;
2629 compRight.pRight = pExpr->pList->a[1].pExpr;
2630 exprX.iTable = sqlite3ExprCodeTemp(pParse, &exprX, &regFree1);
drhc5499be2008-04-01 15:06:33 +00002631 testcase( regFree1==0 );
drh2dcef112008-01-12 19:03:48 +00002632 exprX.op = TK_REGISTER;
drhc5499be2008-04-01 15:06:33 +00002633 testcase( jumpIfNull==0 );
drh2dcef112008-01-12 19:03:48 +00002634 sqlite3ExprIfTrue(pParse, &exprAnd, dest, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00002635 break;
2636 }
drhcce7d172000-05-31 15:34:51 +00002637 default: {
drh2dcef112008-01-12 19:03:48 +00002638 r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
2639 sqlite3VdbeAddOp3(v, OP_If, r1, dest, jumpIfNull!=0);
drhc5499be2008-04-01 15:06:33 +00002640 testcase( regFree1==0 );
2641 testcase( jumpIfNull==0 );
drhcce7d172000-05-31 15:34:51 +00002642 break;
2643 }
2644 }
drh2dcef112008-01-12 19:03:48 +00002645 sqlite3ReleaseTempReg(pParse, regFree1);
2646 sqlite3ReleaseTempReg(pParse, regFree2);
drhcce7d172000-05-31 15:34:51 +00002647}
2648
2649/*
drh66b89c82000-11-28 20:47:17 +00002650** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00002651** to the label "dest" if the expression is false but execution
2652** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00002653**
2654** If the expression evaluates to NULL (neither true nor false) then
drh35573352008-01-08 23:54:25 +00002655** jump if jumpIfNull is SQLITE_JUMPIFNULL or fall through if jumpIfNull
2656** is 0.
drhcce7d172000-05-31 15:34:51 +00002657*/
danielk19774adee202004-05-08 08:23:19 +00002658void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00002659 Vdbe *v = pParse->pVdbe;
2660 int op = 0;
drh2dcef112008-01-12 19:03:48 +00002661 int regFree1 = 0;
2662 int regFree2 = 0;
2663 int r1, r2;
2664
drh35573352008-01-08 23:54:25 +00002665 assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
drhdaffd0e2001-04-11 14:28:42 +00002666 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00002667
2668 /* The value of pExpr->op and op are related as follows:
2669 **
2670 ** pExpr->op op
2671 ** --------- ----------
2672 ** TK_ISNULL OP_NotNull
2673 ** TK_NOTNULL OP_IsNull
2674 ** TK_NE OP_Eq
2675 ** TK_EQ OP_Ne
2676 ** TK_GT OP_Le
2677 ** TK_LE OP_Gt
2678 ** TK_GE OP_Lt
2679 ** TK_LT OP_Ge
2680 **
2681 ** For other values of pExpr->op, op is undefined and unused.
2682 ** The value of TK_ and OP_ constants are arranged such that we
2683 ** can compute the mapping above using the following expression.
2684 ** Assert()s verify that the computation is correct.
2685 */
2686 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
2687
2688 /* Verify correct alignment of TK_ and OP_ constants
2689 */
2690 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
2691 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
2692 assert( pExpr->op!=TK_NE || op==OP_Eq );
2693 assert( pExpr->op!=TK_EQ || op==OP_Ne );
2694 assert( pExpr->op!=TK_LT || op==OP_Ge );
2695 assert( pExpr->op!=TK_LE || op==OP_Gt );
2696 assert( pExpr->op!=TK_GT || op==OP_Le );
2697 assert( pExpr->op!=TK_GE || op==OP_Lt );
2698
drhcce7d172000-05-31 15:34:51 +00002699 switch( pExpr->op ){
2700 case TK_AND: {
drhc5499be2008-04-01 15:06:33 +00002701 testcase( jumpIfNull==0 );
2702 testcase( pParse->disableColCache==0 );
danielk19774adee202004-05-08 08:23:19 +00002703 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhe55cbd72008-03-31 23:48:03 +00002704 pParse->disableColCache++;
danielk19774adee202004-05-08 08:23:19 +00002705 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhc5499be2008-04-01 15:06:33 +00002706 assert( pParse->disableColCache>0 );
drhe55cbd72008-03-31 23:48:03 +00002707 pParse->disableColCache--;
drhcce7d172000-05-31 15:34:51 +00002708 break;
2709 }
2710 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00002711 int d2 = sqlite3VdbeMakeLabel(v);
drhc5499be2008-04-01 15:06:33 +00002712 testcase( jumpIfNull==0 );
2713 testcase( pParse->disableColCache==0 );
drh35573352008-01-08 23:54:25 +00002714 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, jumpIfNull^SQLITE_JUMPIFNULL);
drhe55cbd72008-03-31 23:48:03 +00002715 pParse->disableColCache++;
danielk19774adee202004-05-08 08:23:19 +00002716 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhc5499be2008-04-01 15:06:33 +00002717 assert( pParse->disableColCache>0 );
drhe55cbd72008-03-31 23:48:03 +00002718 pParse->disableColCache--;
danielk19774adee202004-05-08 08:23:19 +00002719 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00002720 break;
2721 }
2722 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00002723 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002724 break;
2725 }
2726 case TK_LT:
2727 case TK_LE:
2728 case TK_GT:
2729 case TK_GE:
2730 case TK_NE:
2731 case TK_EQ: {
drhc5499be2008-04-01 15:06:33 +00002732 testcase( op==TK_LT );
2733 testcase( op==TK_LE );
2734 testcase( op==TK_GT );
2735 testcase( op==TK_GE );
2736 testcase( op==TK_EQ );
2737 testcase( op==TK_NE );
2738 testcase( jumpIfNull==0 );
drhda250ea2008-04-01 05:07:14 +00002739 codeCompareOperands(pParse, pExpr->pLeft, &r1, &regFree1,
2740 pExpr->pRight, &r2, &regFree2);
drh35573352008-01-08 23:54:25 +00002741 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
drh2dcef112008-01-12 19:03:48 +00002742 r1, r2, dest, jumpIfNull);
drhc5499be2008-04-01 15:06:33 +00002743 testcase( regFree1==0 );
2744 testcase( regFree2==0 );
drhcce7d172000-05-31 15:34:51 +00002745 break;
2746 }
drhcce7d172000-05-31 15:34:51 +00002747 case TK_ISNULL:
2748 case TK_NOTNULL: {
drhc5499be2008-04-01 15:06:33 +00002749 testcase( op==TK_ISNULL );
2750 testcase( op==TK_NOTNULL );
drh2dcef112008-01-12 19:03:48 +00002751 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
2752 sqlite3VdbeAddOp2(v, op, r1, dest);
drhc5499be2008-04-01 15:06:33 +00002753 testcase( regFree1==0 );
drhcce7d172000-05-31 15:34:51 +00002754 break;
2755 }
drhfef52082000-06-06 01:50:43 +00002756 case TK_BETWEEN: {
drh2dcef112008-01-12 19:03:48 +00002757 /* x BETWEEN y AND z
danielk19770202b292004-06-09 09:55:16 +00002758 **
drh2dcef112008-01-12 19:03:48 +00002759 ** Is equivalent to
2760 **
2761 ** x>=y AND x<=z
2762 **
2763 ** Code it as such, taking care to do the common subexpression
2764 ** elementation of x.
danielk19770202b292004-06-09 09:55:16 +00002765 */
drh2dcef112008-01-12 19:03:48 +00002766 Expr exprAnd;
2767 Expr compLeft;
2768 Expr compRight;
2769 Expr exprX;
drhbe5c89a2004-07-26 00:31:09 +00002770
drh2dcef112008-01-12 19:03:48 +00002771 exprX = *pExpr->pLeft;
2772 exprAnd.op = TK_AND;
2773 exprAnd.pLeft = &compLeft;
2774 exprAnd.pRight = &compRight;
2775 compLeft.op = TK_GE;
2776 compLeft.pLeft = &exprX;
2777 compLeft.pRight = pExpr->pList->a[0].pExpr;
2778 compRight.op = TK_LE;
2779 compRight.pLeft = &exprX;
2780 compRight.pRight = pExpr->pList->a[1].pExpr;
2781 exprX.iTable = sqlite3ExprCodeTemp(pParse, &exprX, &regFree1);
drhc5499be2008-04-01 15:06:33 +00002782 testcase( regFree1==0 );
drh2dcef112008-01-12 19:03:48 +00002783 exprX.op = TK_REGISTER;
drhc5499be2008-04-01 15:06:33 +00002784 testcase( jumpIfNull==0 );
drh2dcef112008-01-12 19:03:48 +00002785 sqlite3ExprIfFalse(pParse, &exprAnd, dest, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00002786 break;
2787 }
drhcce7d172000-05-31 15:34:51 +00002788 default: {
drh2dcef112008-01-12 19:03:48 +00002789 r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
2790 sqlite3VdbeAddOp3(v, OP_IfNot, r1, dest, jumpIfNull!=0);
drhc5499be2008-04-01 15:06:33 +00002791 testcase( regFree1==0 );
2792 testcase( jumpIfNull==0 );
drhcce7d172000-05-31 15:34:51 +00002793 break;
2794 }
2795 }
drh2dcef112008-01-12 19:03:48 +00002796 sqlite3ReleaseTempReg(pParse, regFree1);
2797 sqlite3ReleaseTempReg(pParse, regFree2);
drhcce7d172000-05-31 15:34:51 +00002798}
drh22827922000-06-06 17:27:05 +00002799
2800/*
2801** Do a deep comparison of two expression trees. Return TRUE (non-zero)
2802** if they are identical and return FALSE if they differ in any way.
drhd40aab02007-02-24 15:29:03 +00002803**
2804** Sometimes this routine will return FALSE even if the two expressions
2805** really are equivalent. If we cannot prove that the expressions are
2806** identical, we return FALSE just to be safe. So if this routine
2807** returns false, then you do not really know for certain if the two
2808** expressions are the same. But if you get a TRUE return, then you
2809** can be sure the expressions are the same. In the places where
2810** this routine is used, it does not hurt to get an extra FALSE - that
2811** just might result in some slightly slower code. But returning
2812** an incorrect TRUE could lead to a malfunction.
drh22827922000-06-06 17:27:05 +00002813*/
danielk19774adee202004-05-08 08:23:19 +00002814int sqlite3ExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00002815 int i;
danielk19774b202ae2006-01-23 05:50:58 +00002816 if( pA==0||pB==0 ){
2817 return pB==pA;
drh22827922000-06-06 17:27:05 +00002818 }
2819 if( pA->op!=pB->op ) return 0;
drhfd357972005-09-09 01:33:19 +00002820 if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 0;
danielk19774adee202004-05-08 08:23:19 +00002821 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
2822 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00002823 if( pA->pList ){
2824 if( pB->pList==0 ) return 0;
2825 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
2826 for(i=0; i<pA->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00002827 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00002828 return 0;
2829 }
2830 }
2831 }else if( pB->pList ){
2832 return 0;
2833 }
2834 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00002835 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drhdd735212007-02-24 13:53:05 +00002836 if( pA->op!=TK_COLUMN && pA->token.z ){
drh22827922000-06-06 17:27:05 +00002837 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00002838 if( pB->token.n!=pA->token.n ) return 0;
drh2646da72005-12-09 20:02:05 +00002839 if( sqlite3StrNICmp((char*)pA->token.z,(char*)pB->token.z,pB->token.n)!=0 ){
2840 return 0;
2841 }
drh22827922000-06-06 17:27:05 +00002842 }
2843 return 1;
2844}
2845
drh13449892005-09-07 21:22:45 +00002846
drh22827922000-06-06 17:27:05 +00002847/*
drh13449892005-09-07 21:22:45 +00002848** Add a new element to the pAggInfo->aCol[] array. Return the index of
2849** the new element. Return a negative number if malloc fails.
drh22827922000-06-06 17:27:05 +00002850*/
drh17435752007-08-16 04:30:38 +00002851static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){
drh13449892005-09-07 21:22:45 +00002852 int i;
drhcf643722007-03-27 13:36:37 +00002853 pInfo->aCol = sqlite3ArrayAllocate(
drh17435752007-08-16 04:30:38 +00002854 db,
drhcf643722007-03-27 13:36:37 +00002855 pInfo->aCol,
2856 sizeof(pInfo->aCol[0]),
2857 3,
2858 &pInfo->nColumn,
2859 &pInfo->nColumnAlloc,
2860 &i
2861 );
drh13449892005-09-07 21:22:45 +00002862 return i;
2863}
2864
2865/*
2866** Add a new element to the pAggInfo->aFunc[] array. Return the index of
2867** the new element. Return a negative number if malloc fails.
2868*/
drh17435752007-08-16 04:30:38 +00002869static int addAggInfoFunc(sqlite3 *db, AggInfo *pInfo){
drh13449892005-09-07 21:22:45 +00002870 int i;
drhcf643722007-03-27 13:36:37 +00002871 pInfo->aFunc = sqlite3ArrayAllocate(
drh17435752007-08-16 04:30:38 +00002872 db,
drhcf643722007-03-27 13:36:37 +00002873 pInfo->aFunc,
2874 sizeof(pInfo->aFunc[0]),
2875 3,
2876 &pInfo->nFunc,
2877 &pInfo->nFuncAlloc,
2878 &i
2879 );
drh13449892005-09-07 21:22:45 +00002880 return i;
2881}
drh22827922000-06-06 17:27:05 +00002882
2883/*
drh7d10d5a2008-08-20 16:35:10 +00002884** This is the xExprCallback for a tree walker. It is used to
2885** implement sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates
drh626a8792005-01-17 22:08:19 +00002886** for additional information.
drh22827922000-06-06 17:27:05 +00002887*/
drh7d10d5a2008-08-20 16:35:10 +00002888static int analyzeAggregate(Walker *pWalker, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00002889 int i;
drh7d10d5a2008-08-20 16:35:10 +00002890 NameContext *pNC = pWalker->u.pNC;
danielk1977a58fdfb2005-02-08 07:50:40 +00002891 Parse *pParse = pNC->pParse;
2892 SrcList *pSrcList = pNC->pSrcList;
drh13449892005-09-07 21:22:45 +00002893 AggInfo *pAggInfo = pNC->pAggInfo;
drh22827922000-06-06 17:27:05 +00002894
drh22827922000-06-06 17:27:05 +00002895 switch( pExpr->op ){
drh89c69d02007-01-04 01:20:28 +00002896 case TK_AGG_COLUMN:
drh967e8b72000-06-21 13:59:10 +00002897 case TK_COLUMN: {
drh8b213892008-08-29 02:14:02 +00002898 testcase( pExpr->op==TK_AGG_COLUMN );
2899 testcase( pExpr->op==TK_COLUMN );
drh13449892005-09-07 21:22:45 +00002900 /* Check to see if the column is in one of the tables in the FROM
2901 ** clause of the aggregate query */
2902 if( pSrcList ){
2903 struct SrcList_item *pItem = pSrcList->a;
2904 for(i=0; i<pSrcList->nSrc; i++, pItem++){
2905 struct AggInfo_col *pCol;
2906 if( pExpr->iTable==pItem->iCursor ){
2907 /* If we reach this point, it means that pExpr refers to a table
2908 ** that is in the FROM clause of the aggregate query.
2909 **
2910 ** Make an entry for the column in pAggInfo->aCol[] if there
2911 ** is not an entry there already.
2912 */
drh7f906d62007-03-12 23:48:52 +00002913 int k;
drh13449892005-09-07 21:22:45 +00002914 pCol = pAggInfo->aCol;
drh7f906d62007-03-12 23:48:52 +00002915 for(k=0; k<pAggInfo->nColumn; k++, pCol++){
drh13449892005-09-07 21:22:45 +00002916 if( pCol->iTable==pExpr->iTable &&
2917 pCol->iColumn==pExpr->iColumn ){
2918 break;
2919 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002920 }
danielk19771e536952007-08-16 10:09:01 +00002921 if( (k>=pAggInfo->nColumn)
2922 && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0
2923 ){
drh7f906d62007-03-12 23:48:52 +00002924 pCol = &pAggInfo->aCol[k];
danielk19770817d0d2007-02-14 09:19:36 +00002925 pCol->pTab = pExpr->pTab;
drh13449892005-09-07 21:22:45 +00002926 pCol->iTable = pExpr->iTable;
2927 pCol->iColumn = pExpr->iColumn;
drh0a07c102008-01-03 18:03:08 +00002928 pCol->iMem = ++pParse->nMem;
drh13449892005-09-07 21:22:45 +00002929 pCol->iSorterColumn = -1;
drh5774b802005-09-07 22:48:16 +00002930 pCol->pExpr = pExpr;
drh13449892005-09-07 21:22:45 +00002931 if( pAggInfo->pGroupBy ){
2932 int j, n;
2933 ExprList *pGB = pAggInfo->pGroupBy;
2934 struct ExprList_item *pTerm = pGB->a;
2935 n = pGB->nExpr;
2936 for(j=0; j<n; j++, pTerm++){
2937 Expr *pE = pTerm->pExpr;
2938 if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable &&
2939 pE->iColumn==pExpr->iColumn ){
2940 pCol->iSorterColumn = j;
2941 break;
2942 }
2943 }
2944 }
2945 if( pCol->iSorterColumn<0 ){
2946 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
2947 }
2948 }
2949 /* There is now an entry for pExpr in pAggInfo->aCol[] (either
2950 ** because it was there before or because we just created it).
2951 ** Convert the pExpr to be a TK_AGG_COLUMN referring to that
2952 ** pAggInfo->aCol[] entry.
2953 */
2954 pExpr->pAggInfo = pAggInfo;
2955 pExpr->op = TK_AGG_COLUMN;
drh7f906d62007-03-12 23:48:52 +00002956 pExpr->iAgg = k;
drh13449892005-09-07 21:22:45 +00002957 break;
2958 } /* endif pExpr->iTable==pItem->iCursor */
2959 } /* end loop over pSrcList */
drh22827922000-06-06 17:27:05 +00002960 }
drh7d10d5a2008-08-20 16:35:10 +00002961 return WRC_Prune;
drh22827922000-06-06 17:27:05 +00002962 }
2963 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00002964 /* The pNC->nDepth==0 test causes aggregate functions in subqueries
2965 ** to be ignored */
danielk1977a58fdfb2005-02-08 07:50:40 +00002966 if( pNC->nDepth==0 ){
drh13449892005-09-07 21:22:45 +00002967 /* Check to see if pExpr is a duplicate of another aggregate
2968 ** function that is already in the pAggInfo structure
2969 */
2970 struct AggInfo_func *pItem = pAggInfo->aFunc;
2971 for(i=0; i<pAggInfo->nFunc; i++, pItem++){
2972 if( sqlite3ExprCompare(pItem->pExpr, pExpr) ){
danielk1977a58fdfb2005-02-08 07:50:40 +00002973 break;
2974 }
drh22827922000-06-06 17:27:05 +00002975 }
drh13449892005-09-07 21:22:45 +00002976 if( i>=pAggInfo->nFunc ){
2977 /* pExpr is original. Make a new entry in pAggInfo->aFunc[]
2978 */
danielk197714db2662006-01-09 16:12:04 +00002979 u8 enc = ENC(pParse->db);
danielk19771e536952007-08-16 10:09:01 +00002980 i = addAggInfoFunc(pParse->db, pAggInfo);
drh13449892005-09-07 21:22:45 +00002981 if( i>=0 ){
2982 pItem = &pAggInfo->aFunc[i];
2983 pItem->pExpr = pExpr;
drh0a07c102008-01-03 18:03:08 +00002984 pItem->iMem = ++pParse->nMem;
drh13449892005-09-07 21:22:45 +00002985 pItem->pFunc = sqlite3FindFunction(pParse->db,
drh2646da72005-12-09 20:02:05 +00002986 (char*)pExpr->token.z, pExpr->token.n,
drh13449892005-09-07 21:22:45 +00002987 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
drhfd357972005-09-09 01:33:19 +00002988 if( pExpr->flags & EP_Distinct ){
2989 pItem->iDistinct = pParse->nTab++;
2990 }else{
2991 pItem->iDistinct = -1;
2992 }
drh13449892005-09-07 21:22:45 +00002993 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002994 }
drh13449892005-09-07 21:22:45 +00002995 /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
2996 */
danielk1977a58fdfb2005-02-08 07:50:40 +00002997 pExpr->iAgg = i;
drh13449892005-09-07 21:22:45 +00002998 pExpr->pAggInfo = pAggInfo;
drh7d10d5a2008-08-20 16:35:10 +00002999 return WRC_Prune;
drh22827922000-06-06 17:27:05 +00003000 }
drh22827922000-06-06 17:27:05 +00003001 }
3002 }
drh7d10d5a2008-08-20 16:35:10 +00003003 return WRC_Continue;
3004}
3005static int analyzeAggregatesInSelect(Walker *pWalker, Select *pSelect){
3006 NameContext *pNC = pWalker->u.pNC;
3007 if( pNC->nDepth==0 ){
danielk1977a58fdfb2005-02-08 07:50:40 +00003008 pNC->nDepth++;
drh7d10d5a2008-08-20 16:35:10 +00003009 sqlite3WalkSelect(pWalker, pSelect);
danielk1977a58fdfb2005-02-08 07:50:40 +00003010 pNC->nDepth--;
drh7d10d5a2008-08-20 16:35:10 +00003011 return WRC_Prune;
3012 }else{
3013 return WRC_Continue;
danielk1977a58fdfb2005-02-08 07:50:40 +00003014 }
drh626a8792005-01-17 22:08:19 +00003015}
3016
3017/*
3018** Analyze the given expression looking for aggregate functions and
3019** for variables that need to be added to the pParse->aAgg[] array.
3020** Make additional entries to the pParse->aAgg[] array as necessary.
3021**
3022** This routine should only be called after the expression has been
drh7d10d5a2008-08-20 16:35:10 +00003023** analyzed by sqlite3ResolveExprNames().
drh626a8792005-01-17 22:08:19 +00003024*/
drhd2b3e232008-01-23 14:51:49 +00003025void sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
drh7d10d5a2008-08-20 16:35:10 +00003026 Walker w;
3027 w.xExprCallback = analyzeAggregate;
3028 w.xSelectCallback = analyzeAggregatesInSelect;
3029 w.u.pNC = pNC;
3030 sqlite3WalkExpr(&w, pExpr);
drh22827922000-06-06 17:27:05 +00003031}
drh5d9a4af2005-08-30 00:54:01 +00003032
3033/*
3034** Call sqlite3ExprAnalyzeAggregates() for every expression in an
3035** expression list. Return the number of errors.
3036**
3037** If an error is found, the analysis is cut short.
3038*/
drhd2b3e232008-01-23 14:51:49 +00003039void sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){
drh5d9a4af2005-08-30 00:54:01 +00003040 struct ExprList_item *pItem;
3041 int i;
drh5d9a4af2005-08-30 00:54:01 +00003042 if( pList ){
drhd2b3e232008-01-23 14:51:49 +00003043 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
3044 sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
drh5d9a4af2005-08-30 00:54:01 +00003045 }
3046 }
drh5d9a4af2005-08-30 00:54:01 +00003047}
drh892d3172008-01-10 03:46:36 +00003048
3049/*
3050** Allocate or deallocate temporary use registers during code generation.
3051*/
3052int sqlite3GetTempReg(Parse *pParse){
drhe55cbd72008-03-31 23:48:03 +00003053 if( pParse->nTempReg==0 ){
drh892d3172008-01-10 03:46:36 +00003054 return ++pParse->nMem;
3055 }
danielk19772f425f62008-07-04 09:41:39 +00003056 return pParse->aTempReg[--pParse->nTempReg];
drh892d3172008-01-10 03:46:36 +00003057}
3058void sqlite3ReleaseTempReg(Parse *pParse, int iReg){
drh2dcef112008-01-12 19:03:48 +00003059 if( iReg && pParse->nTempReg<ArraySize(pParse->aTempReg) ){
drhd1fa7bc2009-01-10 13:24:50 +00003060 sqlite3ExprWritableRegister(pParse, iReg);
drh892d3172008-01-10 03:46:36 +00003061 pParse->aTempReg[pParse->nTempReg++] = iReg;
3062 }
3063}
3064
3065/*
3066** Allocate or deallocate a block of nReg consecutive registers
3067*/
3068int sqlite3GetTempRange(Parse *pParse, int nReg){
drhe55cbd72008-03-31 23:48:03 +00003069 int i, n;
3070 i = pParse->iRangeReg;
3071 n = pParse->nRangeReg;
3072 if( nReg<=n && !usedAsColumnCache(pParse, i, i+n-1) ){
drh892d3172008-01-10 03:46:36 +00003073 pParse->iRangeReg += nReg;
3074 pParse->nRangeReg -= nReg;
3075 }else{
3076 i = pParse->nMem+1;
3077 pParse->nMem += nReg;
3078 }
3079 return i;
3080}
3081void sqlite3ReleaseTempRange(Parse *pParse, int iReg, int nReg){
3082 if( nReg>pParse->nRangeReg ){
3083 pParse->nRangeReg = nReg;
3084 pParse->iRangeReg = iReg;
3085 }
3086}