blob: 8960a7ad93367cbedf636b231cab8082d182adcc [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**
shanefbd60f82009-02-04 03:59:25 +000015** $Id: expr.c,v 1.411 2009/02/04 03:59:25 shane Exp $
drhcce7d172000-05-31 15:34:51 +000016*/
17#include "sqliteInt.h"
drha2e00042002-01-22 03:13:42 +000018
danielk1977e014a832004-05-17 10:48:57 +000019/*
20** Return the 'affinity' of the expression pExpr if any.
21**
22** If pExpr is a column, a reference to a column via an 'AS' alias,
23** or a sub-select with a column as the return value, then the
24** affinity of that column is returned. Otherwise, 0x00 is returned,
25** indicating no affinity for the expression.
26**
27** i.e. the WHERE clause expresssions in the following statements all
28** have an affinity:
29**
30** CREATE TABLE t1(a);
31** SELECT * FROM t1 WHERE a;
32** SELECT a AS b FROM t1 WHERE b;
33** SELECT * FROM t1 WHERE (select a from t1);
34*/
danielk1977bf3b7212004-05-18 10:06:24 +000035char sqlite3ExprAffinity(Expr *pExpr){
drh487e2622005-06-25 18:42:14 +000036 int op = pExpr->op;
drh487e2622005-06-25 18:42:14 +000037 if( op==TK_SELECT ){
danielk1977bf3b7212004-05-18 10:06:24 +000038 return sqlite3ExprAffinity(pExpr->pSelect->pEList->a[0].pExpr);
danielk1977a37cdde2004-05-16 11:15:36 +000039 }
drh487e2622005-06-25 18:42:14 +000040#ifndef SQLITE_OMIT_CAST
41 if( op==TK_CAST ){
drh8a512562005-11-14 22:29:05 +000042 return sqlite3AffinityType(&pExpr->token);
drh487e2622005-06-25 18:42:14 +000043 }
44#endif
danielk1977259a4552008-11-12 08:07:12 +000045 if( (op==TK_AGG_COLUMN || op==TK_COLUMN || op==TK_REGISTER)
46 && pExpr->pTab!=0
47 ){
drh7d10d5a2008-08-20 16:35:10 +000048 /* op==TK_REGISTER && pExpr->pTab!=0 happens when pExpr was originally
49 ** a TK_COLUMN but was previously evaluated and cached in a register */
50 int j = pExpr->iColumn;
51 if( j<0 ) return SQLITE_AFF_INTEGER;
52 assert( pExpr->pTab && j<pExpr->pTab->nCol );
53 return pExpr->pTab->aCol[j].affinity;
54 }
danielk1977a37cdde2004-05-16 11:15:36 +000055 return pExpr->affinity;
56}
57
drh53db1452004-05-20 13:54:53 +000058/*
drh8b4c40d2007-02-01 23:02:45 +000059** Set the collating sequence for expression pExpr to be the collating
60** sequence named by pToken. Return a pointer to the revised expression.
drha34001c2007-02-02 12:44:37 +000061** The collating sequence is marked as "explicit" using the EP_ExpCollate
62** flag. An explicit collating sequence will override implicit
63** collating sequences.
drh8b4c40d2007-02-01 23:02:45 +000064*/
drh7d10d5a2008-08-20 16:35:10 +000065Expr *sqlite3ExprSetColl(Parse *pParse, Expr *pExpr, Token *pCollName){
danielk197739002502007-11-12 09:50:26 +000066 char *zColl = 0; /* Dequoted name of collation sequence */
drh8b4c40d2007-02-01 23:02:45 +000067 CollSeq *pColl;
drh633e6d52008-07-28 19:34:53 +000068 sqlite3 *db = pParse->db;
drh7d10d5a2008-08-20 16:35:10 +000069 zColl = sqlite3NameFromToken(db, pCollName);
danielk197739002502007-11-12 09:50:26 +000070 if( pExpr && zColl ){
71 pColl = sqlite3LocateCollSeq(pParse, zColl, -1);
72 if( pColl ){
73 pExpr->pColl = pColl;
74 pExpr->flags |= EP_ExpCollate;
75 }
drh8b4c40d2007-02-01 23:02:45 +000076 }
drh633e6d52008-07-28 19:34:53 +000077 sqlite3DbFree(db, zColl);
drh8b4c40d2007-02-01 23:02:45 +000078 return pExpr;
79}
80
81/*
danielk19770202b292004-06-09 09:55:16 +000082** Return the default collation sequence for the expression pExpr. If
83** there is no default collation type, return 0.
84*/
danielk19777cedc8d2004-06-10 10:50:08 +000085CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
86 CollSeq *pColl = 0;
drh7d10d5a2008-08-20 16:35:10 +000087 Expr *p = pExpr;
88 while( p ){
drh7e09fe02007-06-20 16:13:23 +000089 int op;
drh7d10d5a2008-08-20 16:35:10 +000090 pColl = p->pColl;
91 if( pColl ) break;
92 op = p->op;
danielk1977259a4552008-11-12 08:07:12 +000093 if( (op==TK_AGG_COLUMN || op==TK_COLUMN || op==TK_REGISTER) && p->pTab!=0 ){
drh7d10d5a2008-08-20 16:35:10 +000094 /* op==TK_REGISTER && p->pTab!=0 happens when pExpr was originally
95 ** a TK_COLUMN but was previously evaluated and cached in a register */
96 const char *zColl;
97 int j = p->iColumn;
98 if( j>=0 ){
99 sqlite3 *db = pParse->db;
100 zColl = p->pTab->aCol[j].zColl;
101 pColl = sqlite3FindCollSeq(db, ENC(db), zColl, -1, 0);
102 pExpr->pColl = pColl;
103 }
104 break;
danielk19770202b292004-06-09 09:55:16 +0000105 }
drh7d10d5a2008-08-20 16:35:10 +0000106 if( op!=TK_CAST && op!=TK_UPLUS ){
107 break;
108 }
109 p = p->pLeft;
danielk19770202b292004-06-09 09:55:16 +0000110 }
danielk19777cedc8d2004-06-10 10:50:08 +0000111 if( sqlite3CheckCollSeq(pParse, pColl) ){
112 pColl = 0;
113 }
114 return pColl;
danielk19770202b292004-06-09 09:55:16 +0000115}
116
117/*
drh626a8792005-01-17 22:08:19 +0000118** pExpr is an operand of a comparison operator. aff2 is the
119** type affinity of the other operand. This routine returns the
drh53db1452004-05-20 13:54:53 +0000120** type affinity that should be used for the comparison operator.
121*/
danielk1977e014a832004-05-17 10:48:57 +0000122char sqlite3CompareAffinity(Expr *pExpr, char aff2){
danielk1977bf3b7212004-05-18 10:06:24 +0000123 char aff1 = sqlite3ExprAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +0000124 if( aff1 && aff2 ){
drh8df447f2005-11-01 15:48:24 +0000125 /* Both sides of the comparison are columns. If one has numeric
126 ** affinity, use that. Otherwise use no affinity.
danielk1977e014a832004-05-17 10:48:57 +0000127 */
drh8a512562005-11-14 22:29:05 +0000128 if( sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2) ){
danielk1977e014a832004-05-17 10:48:57 +0000129 return SQLITE_AFF_NUMERIC;
130 }else{
131 return SQLITE_AFF_NONE;
132 }
133 }else if( !aff1 && !aff2 ){
drh5f6a87b2004-07-19 00:39:45 +0000134 /* Neither side of the comparison is a column. Compare the
135 ** results directly.
danielk1977e014a832004-05-17 10:48:57 +0000136 */
drh5f6a87b2004-07-19 00:39:45 +0000137 return SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +0000138 }else{
139 /* One side is a column, the other is not. Use the columns affinity. */
drhfe05af82005-07-21 03:14:59 +0000140 assert( aff1==0 || aff2==0 );
danielk1977e014a832004-05-17 10:48:57 +0000141 return (aff1 + aff2);
142 }
143}
144
drh53db1452004-05-20 13:54:53 +0000145/*
146** pExpr is a comparison operator. Return the type affinity that should
147** be applied to both operands prior to doing the comparison.
148*/
danielk1977e014a832004-05-17 10:48:57 +0000149static char comparisonAffinity(Expr *pExpr){
150 char aff;
151 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
152 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
153 pExpr->op==TK_NE );
154 assert( pExpr->pLeft );
danielk1977bf3b7212004-05-18 10:06:24 +0000155 aff = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +0000156 if( pExpr->pRight ){
157 aff = sqlite3CompareAffinity(pExpr->pRight, aff);
158 }
159 else if( pExpr->pSelect ){
160 aff = sqlite3CompareAffinity(pExpr->pSelect->pEList->a[0].pExpr, aff);
161 }
162 else if( !aff ){
drhde087bd2007-02-23 03:00:44 +0000163 aff = SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +0000164 }
165 return aff;
166}
167
168/*
169** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
170** idx_affinity is the affinity of an indexed column. Return true
171** if the index with affinity idx_affinity may be used to implement
172** the comparison in pExpr.
173*/
174int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){
175 char aff = comparisonAffinity(pExpr);
drh8a512562005-11-14 22:29:05 +0000176 switch( aff ){
177 case SQLITE_AFF_NONE:
178 return 1;
179 case SQLITE_AFF_TEXT:
180 return idx_affinity==SQLITE_AFF_TEXT;
181 default:
182 return sqlite3IsNumericAffinity(idx_affinity);
183 }
danielk1977e014a832004-05-17 10:48:57 +0000184}
185
danielk1977a37cdde2004-05-16 11:15:36 +0000186/*
drh35573352008-01-08 23:54:25 +0000187** Return the P5 value that should be used for a binary comparison
danielk1977a37cdde2004-05-16 11:15:36 +0000188** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
danielk1977a37cdde2004-05-16 11:15:36 +0000189*/
drh35573352008-01-08 23:54:25 +0000190static u8 binaryCompareP5(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){
191 u8 aff = (char)sqlite3ExprAffinity(pExpr2);
drh1bd10f82008-12-10 21:19:56 +0000192 aff = (u8)sqlite3CompareAffinity(pExpr1, aff) | (u8)jumpIfNull;
drh35573352008-01-08 23:54:25 +0000193 return aff;
danielk1977a37cdde2004-05-16 11:15:36 +0000194}
195
drha2e00042002-01-22 03:13:42 +0000196/*
danielk19770202b292004-06-09 09:55:16 +0000197** Return a pointer to the collation sequence that should be used by
198** a binary comparison operator comparing pLeft and pRight.
199**
200** If the left hand expression has a collating sequence type, then it is
201** used. Otherwise the collation sequence for the right hand expression
202** is used, or the default (BINARY) if neither expression has a collating
203** type.
danielk1977bcbb04e2007-05-29 12:11:29 +0000204**
205** Argument pRight (but not pLeft) may be a null pointer. In this case,
206** it is not considered.
danielk19770202b292004-06-09 09:55:16 +0000207*/
drh0a0e1312007-08-07 17:04:59 +0000208CollSeq *sqlite3BinaryCompareCollSeq(
danielk1977bcbb04e2007-05-29 12:11:29 +0000209 Parse *pParse,
210 Expr *pLeft,
211 Expr *pRight
212){
drhec41dda2007-02-07 13:09:45 +0000213 CollSeq *pColl;
214 assert( pLeft );
drhec41dda2007-02-07 13:09:45 +0000215 if( pLeft->flags & EP_ExpCollate ){
216 assert( pLeft->pColl );
217 pColl = pLeft->pColl;
danielk1977bcbb04e2007-05-29 12:11:29 +0000218 }else if( pRight && pRight->flags & EP_ExpCollate ){
drhec41dda2007-02-07 13:09:45 +0000219 assert( pRight->pColl );
220 pColl = pRight->pColl;
221 }else{
222 pColl = sqlite3ExprCollSeq(pParse, pLeft);
223 if( !pColl ){
224 pColl = sqlite3ExprCollSeq(pParse, pRight);
225 }
danielk19770202b292004-06-09 09:55:16 +0000226 }
227 return pColl;
228}
229
230/*
drhda250ea2008-04-01 05:07:14 +0000231** Generate the operands for a comparison operation. Before
232** generating the code for each operand, set the EP_AnyAff
233** flag on the expression so that it will be able to used a
234** cached column value that has previously undergone an
235** affinity change.
236*/
237static void codeCompareOperands(
238 Parse *pParse, /* Parsing and code generating context */
239 Expr *pLeft, /* The left operand */
240 int *pRegLeft, /* Register where left operand is stored */
241 int *pFreeLeft, /* Free this register when done */
242 Expr *pRight, /* The right operand */
243 int *pRegRight, /* Register where right operand is stored */
244 int *pFreeRight /* Write temp register for right operand there */
245){
246 while( pLeft->op==TK_UPLUS ) pLeft = pLeft->pLeft;
247 pLeft->flags |= EP_AnyAff;
248 *pRegLeft = sqlite3ExprCodeTemp(pParse, pLeft, pFreeLeft);
249 while( pRight->op==TK_UPLUS ) pRight = pRight->pLeft;
250 pRight->flags |= EP_AnyAff;
251 *pRegRight = sqlite3ExprCodeTemp(pParse, pRight, pFreeRight);
252}
253
254/*
drhbe5c89a2004-07-26 00:31:09 +0000255** Generate code for a comparison operator.
256*/
257static int codeCompare(
258 Parse *pParse, /* The parsing (and code generating) context */
259 Expr *pLeft, /* The left operand */
260 Expr *pRight, /* The right operand */
261 int opcode, /* The comparison opcode */
drh35573352008-01-08 23:54:25 +0000262 int in1, int in2, /* Register holding operands */
drhbe5c89a2004-07-26 00:31:09 +0000263 int dest, /* Jump here if true. */
264 int jumpIfNull /* If true, jump if either operand is NULL */
265){
drh35573352008-01-08 23:54:25 +0000266 int p5;
267 int addr;
268 CollSeq *p4;
269
270 p4 = sqlite3BinaryCompareCollSeq(pParse, pLeft, pRight);
271 p5 = binaryCompareP5(pLeft, pRight, jumpIfNull);
272 addr = sqlite3VdbeAddOp4(pParse->pVdbe, opcode, in2, dest, in1,
273 (void*)p4, P4_COLLSEQ);
drh1bd10f82008-12-10 21:19:56 +0000274 sqlite3VdbeChangeP5(pParse->pVdbe, (u8)p5);
drhe49b1462008-07-09 01:39:44 +0000275 if( (p5 & SQLITE_AFF_MASK)!=SQLITE_AFF_NONE ){
drhda250ea2008-04-01 05:07:14 +0000276 sqlite3ExprCacheAffinityChange(pParse, in1, 1);
277 sqlite3ExprCacheAffinityChange(pParse, in2, 1);
drh2f7794c2008-04-01 03:27:39 +0000278 }
drh35573352008-01-08 23:54:25 +0000279 return addr;
drhbe5c89a2004-07-26 00:31:09 +0000280}
281
danielk19774b5255a2008-06-05 16:47:39 +0000282#if SQLITE_MAX_EXPR_DEPTH>0
283/*
284** Check that argument nHeight is less than or equal to the maximum
285** expression depth allowed. If it is not, leave an error message in
286** pParse.
287*/
drh7d10d5a2008-08-20 16:35:10 +0000288int sqlite3ExprCheckHeight(Parse *pParse, int nHeight){
danielk19774b5255a2008-06-05 16:47:39 +0000289 int rc = SQLITE_OK;
290 int mxHeight = pParse->db->aLimit[SQLITE_LIMIT_EXPR_DEPTH];
291 if( nHeight>mxHeight ){
292 sqlite3ErrorMsg(pParse,
293 "Expression tree is too large (maximum depth %d)", mxHeight
294 );
295 rc = SQLITE_ERROR;
296 }
297 return rc;
298}
299
300/* The following three functions, heightOfExpr(), heightOfExprList()
301** and heightOfSelect(), are used to determine the maximum height
302** of any expression tree referenced by the structure passed as the
303** first argument.
304**
305** If this maximum height is greater than the current value pointed
306** to by pnHeight, the second parameter, then set *pnHeight to that
307** value.
308*/
309static void heightOfExpr(Expr *p, int *pnHeight){
310 if( p ){
311 if( p->nHeight>*pnHeight ){
312 *pnHeight = p->nHeight;
313 }
314 }
315}
316static void heightOfExprList(ExprList *p, int *pnHeight){
317 if( p ){
318 int i;
319 for(i=0; i<p->nExpr; i++){
320 heightOfExpr(p->a[i].pExpr, pnHeight);
321 }
322 }
323}
324static void heightOfSelect(Select *p, int *pnHeight){
325 if( p ){
326 heightOfExpr(p->pWhere, pnHeight);
327 heightOfExpr(p->pHaving, pnHeight);
328 heightOfExpr(p->pLimit, pnHeight);
329 heightOfExpr(p->pOffset, pnHeight);
330 heightOfExprList(p->pEList, pnHeight);
331 heightOfExprList(p->pGroupBy, pnHeight);
332 heightOfExprList(p->pOrderBy, pnHeight);
333 heightOfSelect(p->pPrior, pnHeight);
334 }
335}
336
337/*
338** Set the Expr.nHeight variable in the structure passed as an
339** argument. An expression with no children, Expr.pList or
340** Expr.pSelect member has a height of 1. Any other expression
341** has a height equal to the maximum height of any other
342** referenced Expr plus one.
343*/
344static void exprSetHeight(Expr *p){
345 int nHeight = 0;
346 heightOfExpr(p->pLeft, &nHeight);
347 heightOfExpr(p->pRight, &nHeight);
348 heightOfExprList(p->pList, &nHeight);
349 heightOfSelect(p->pSelect, &nHeight);
350 p->nHeight = nHeight + 1;
351}
352
353/*
354** Set the Expr.nHeight variable using the exprSetHeight() function. If
355** the height is greater than the maximum allowed expression depth,
356** leave an error in pParse.
357*/
358void sqlite3ExprSetHeight(Parse *pParse, Expr *p){
359 exprSetHeight(p);
drh7d10d5a2008-08-20 16:35:10 +0000360 sqlite3ExprCheckHeight(pParse, p->nHeight);
danielk19774b5255a2008-06-05 16:47:39 +0000361}
362
363/*
364** Return the maximum height of any expression tree referenced
365** by the select statement passed as an argument.
366*/
367int sqlite3SelectExprHeight(Select *p){
368 int nHeight = 0;
369 heightOfSelect(p, &nHeight);
370 return nHeight;
371}
372#else
danielk19774b5255a2008-06-05 16:47:39 +0000373 #define exprSetHeight(y)
374#endif /* SQLITE_MAX_EXPR_DEPTH>0 */
375
drhbe5c89a2004-07-26 00:31:09 +0000376/*
drha76b5df2002-02-23 02:32:10 +0000377** Construct a new expression node and return a pointer to it. Memory
drh17435752007-08-16 04:30:38 +0000378** for this node is obtained from sqlite3_malloc(). The calling function
drha76b5df2002-02-23 02:32:10 +0000379** is responsible for making sure the node eventually gets freed.
380*/
drh17435752007-08-16 04:30:38 +0000381Expr *sqlite3Expr(
danielk1977a1644fd2007-08-29 12:31:25 +0000382 sqlite3 *db, /* Handle for sqlite3DbMallocZero() (may be null) */
drh17435752007-08-16 04:30:38 +0000383 int op, /* Expression opcode */
384 Expr *pLeft, /* Left operand */
385 Expr *pRight, /* Right operand */
386 const Token *pToken /* Argument token */
387){
drha76b5df2002-02-23 02:32:10 +0000388 Expr *pNew;
drh26e4a8b2008-05-01 17:16:52 +0000389 pNew = sqlite3DbMallocZero(db, sizeof(Expr));
drha76b5df2002-02-23 02:32:10 +0000390 if( pNew==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000391 /* When malloc fails, delete pLeft and pRight. Expressions passed to
392 ** this function must always be allocated with sqlite3Expr() for this
393 ** reason.
394 */
drh633e6d52008-07-28 19:34:53 +0000395 sqlite3ExprDelete(db, pLeft);
396 sqlite3ExprDelete(db, pRight);
drha76b5df2002-02-23 02:32:10 +0000397 return 0;
398 }
drh1bd10f82008-12-10 21:19:56 +0000399 pNew->op = (u8)op;
drha76b5df2002-02-23 02:32:10 +0000400 pNew->pLeft = pLeft;
401 pNew->pRight = pRight;
danielk1977a58fdfb2005-02-08 07:50:40 +0000402 pNew->iAgg = -1;
drhe49b1462008-07-09 01:39:44 +0000403 pNew->span.z = (u8*)"";
drha76b5df2002-02-23 02:32:10 +0000404 if( pToken ){
drh4b59ab52002-08-24 18:24:51 +0000405 assert( pToken->dyn==0 );
drh145716b2004-09-24 12:24:06 +0000406 pNew->span = pNew->token = *pToken;
drha34001c2007-02-02 12:44:37 +0000407 }else if( pLeft ){
408 if( pRight ){
drhe49b1462008-07-09 01:39:44 +0000409 if( pRight->span.dyn==0 && pLeft->span.dyn==0 ){
410 sqlite3ExprSpan(pNew, &pLeft->span, &pRight->span);
411 }
drh5ffb3ac2007-04-18 17:07:57 +0000412 if( pRight->flags & EP_ExpCollate ){
drha34001c2007-02-02 12:44:37 +0000413 pNew->flags |= EP_ExpCollate;
414 pNew->pColl = pRight->pColl;
415 }
416 }
drh5ffb3ac2007-04-18 17:07:57 +0000417 if( pLeft->flags & EP_ExpCollate ){
drha34001c2007-02-02 12:44:37 +0000418 pNew->flags |= EP_ExpCollate;
419 pNew->pColl = pLeft->pColl;
420 }
drha76b5df2002-02-23 02:32:10 +0000421 }
danielk1977fc976062007-05-10 10:46:56 +0000422
danielk19774b5255a2008-06-05 16:47:39 +0000423 exprSetHeight(pNew);
drha76b5df2002-02-23 02:32:10 +0000424 return pNew;
425}
426
427/*
drh17435752007-08-16 04:30:38 +0000428** Works like sqlite3Expr() except that it takes an extra Parse*
429** argument and notifies the associated connection object if malloc fails.
drh206f3d92006-07-11 13:15:08 +0000430*/
drh17435752007-08-16 04:30:38 +0000431Expr *sqlite3PExpr(
432 Parse *pParse, /* Parsing context */
433 int op, /* Expression opcode */
434 Expr *pLeft, /* Left operand */
435 Expr *pRight, /* Right operand */
436 const Token *pToken /* Argument token */
437){
danielk19774b5255a2008-06-05 16:47:39 +0000438 Expr *p = sqlite3Expr(pParse->db, op, pLeft, pRight, pToken);
439 if( p ){
drh7d10d5a2008-08-20 16:35:10 +0000440 sqlite3ExprCheckHeight(pParse, p->nHeight);
danielk19774b5255a2008-06-05 16:47:39 +0000441 }
442 return p;
drh206f3d92006-07-11 13:15:08 +0000443}
444
445/*
drh4e0cff62004-11-05 05:10:28 +0000446** When doing a nested parse, you can include terms in an expression
drhb7654112008-01-12 12:48:07 +0000447** that look like this: #1 #2 ... These terms refer to registers
448** in the virtual machine. #N is the N-th register.
drh4e0cff62004-11-05 05:10:28 +0000449**
450** This routine is called by the parser to deal with on of those terms.
451** It immediately generates code to store the value in a memory location.
452** The returns an expression that will code to extract the value from
453** that memory location as needed.
454*/
455Expr *sqlite3RegisterExpr(Parse *pParse, Token *pToken){
456 Vdbe *v = pParse->pVdbe;
457 Expr *p;
drh4e0cff62004-11-05 05:10:28 +0000458 if( pParse->nested==0 ){
459 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", pToken);
danielk1977a1644fd2007-08-29 12:31:25 +0000460 return sqlite3PExpr(pParse, TK_NULL, 0, 0, 0);
drh4e0cff62004-11-05 05:10:28 +0000461 }
drhbb7ac002005-08-12 22:58:53 +0000462 if( v==0 ) return 0;
danielk1977a1644fd2007-08-29 12:31:25 +0000463 p = sqlite3PExpr(pParse, TK_REGISTER, 0, 0, pToken);
drh73c42a12004-11-20 18:13:10 +0000464 if( p==0 ){
465 return 0; /* Malloc failed */
466 }
drhb7654112008-01-12 12:48:07 +0000467 p->iTable = atoi((char*)&pToken->z[1]);
drh4e0cff62004-11-05 05:10:28 +0000468 return p;
469}
470
471/*
drh91bb0ee2004-09-01 03:06:34 +0000472** Join two expressions using an AND operator. If either expression is
473** NULL, then just return the other expression.
474*/
danielk19771e536952007-08-16 10:09:01 +0000475Expr *sqlite3ExprAnd(sqlite3 *db, Expr *pLeft, Expr *pRight){
drh91bb0ee2004-09-01 03:06:34 +0000476 if( pLeft==0 ){
477 return pRight;
478 }else if( pRight==0 ){
479 return pLeft;
480 }else{
danielk1977880c15b2007-09-01 18:24:55 +0000481 return sqlite3Expr(db, TK_AND, pLeft, pRight, 0);
drh91bb0ee2004-09-01 03:06:34 +0000482 }
483}
484
485/*
drh6977fea2002-10-22 23:38:04 +0000486** Set the Expr.span field of the given expression to span all
drhe49b1462008-07-09 01:39:44 +0000487** text between the two given tokens. Both tokens must be pointing
488** at the same string.
drha76b5df2002-02-23 02:32:10 +0000489*/
danielk19774adee202004-05-08 08:23:19 +0000490void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
drh4efc4752004-01-16 15:55:37 +0000491 assert( pRight!=0 );
492 assert( pLeft!=0 );
drhe54a62a2008-07-18 17:03:52 +0000493 if( pExpr ){
drhe49b1462008-07-09 01:39:44 +0000494 pExpr->span.z = pLeft->z;
495 pExpr->span.n = pRight->n + (pRight->z - pLeft->z);
drha76b5df2002-02-23 02:32:10 +0000496 }
497}
498
499/*
500** Construct a new expression node for a function with multiple
501** arguments.
502*/
drh17435752007-08-16 04:30:38 +0000503Expr *sqlite3ExprFunction(Parse *pParse, ExprList *pList, Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000504 Expr *pNew;
drh633e6d52008-07-28 19:34:53 +0000505 sqlite3 *db = pParse->db;
danielk19774b202ae2006-01-23 05:50:58 +0000506 assert( pToken );
drh633e6d52008-07-28 19:34:53 +0000507 pNew = sqlite3DbMallocZero(db, sizeof(Expr) );
drha76b5df2002-02-23 02:32:10 +0000508 if( pNew==0 ){
drh633e6d52008-07-28 19:34:53 +0000509 sqlite3ExprListDelete(db, pList); /* Avoid leaking memory when malloc fails */
drha76b5df2002-02-23 02:32:10 +0000510 return 0;
511 }
512 pNew->op = TK_FUNCTION;
513 pNew->pList = pList;
danielk19774b202ae2006-01-23 05:50:58 +0000514 assert( pToken->dyn==0 );
515 pNew->token = *pToken;
drh6977fea2002-10-22 23:38:04 +0000516 pNew->span = pNew->token;
danielk1977fc976062007-05-10 10:46:56 +0000517
danielk19774b5255a2008-06-05 16:47:39 +0000518 sqlite3ExprSetHeight(pParse, pNew);
drha76b5df2002-02-23 02:32:10 +0000519 return pNew;
520}
521
522/*
drhfa6bc002004-09-07 16:19:52 +0000523** Assign a variable number to an expression that encodes a wildcard
524** in the original SQL statement.
525**
526** Wildcards consisting of a single "?" are assigned the next sequential
527** variable number.
528**
529** Wildcards of the form "?nnn" are assigned the number "nnn". We make
530** sure "nnn" is not too be to avoid a denial of service attack when
531** the SQL statement comes from an external source.
532**
533** Wildcards of the form ":aaa" or "$aaa" are assigned the same number
534** as the previous instance of the same wildcard. Or if this is the first
535** instance of the wildcard, the next sequenial variable number is
536** assigned.
537*/
538void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){
539 Token *pToken;
drh17435752007-08-16 04:30:38 +0000540 sqlite3 *db = pParse->db;
541
drhfa6bc002004-09-07 16:19:52 +0000542 if( pExpr==0 ) return;
543 pToken = &pExpr->token;
544 assert( pToken->n>=1 );
545 assert( pToken->z!=0 );
546 assert( pToken->z[0]!=0 );
547 if( pToken->n==1 ){
548 /* Wildcard of the form "?". Assign the next variable number */
549 pExpr->iTable = ++pParse->nVar;
550 }else if( pToken->z[0]=='?' ){
551 /* Wildcard of the form "?nnn". Convert "nnn" to an integer and
552 ** use it as the variable number */
553 int i;
drh2646da72005-12-09 20:02:05 +0000554 pExpr->iTable = i = atoi((char*)&pToken->z[1]);
drhc5499be2008-04-01 15:06:33 +0000555 testcase( i==0 );
556 testcase( i==1 );
557 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 );
558 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] );
drhbb4957f2008-03-20 14:03:29 +0000559 if( i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
drhfa6bc002004-09-07 16:19:52 +0000560 sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
drhbb4957f2008-03-20 14:03:29 +0000561 db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]);
drhfa6bc002004-09-07 16:19:52 +0000562 }
563 if( i>pParse->nVar ){
564 pParse->nVar = i;
565 }
566 }else{
567 /* Wildcards of the form ":aaa" or "$aaa". Reuse the same variable
568 ** number as the prior appearance of the same name, or if the name
569 ** has never appeared before, reuse the same variable number
570 */
drh1bd10f82008-12-10 21:19:56 +0000571 int i;
572 u32 n;
drhfa6bc002004-09-07 16:19:52 +0000573 n = pToken->n;
574 for(i=0; i<pParse->nVarExpr; i++){
575 Expr *pE;
576 if( (pE = pParse->apVarExpr[i])!=0
577 && pE->token.n==n
578 && memcmp(pE->token.z, pToken->z, n)==0 ){
579 pExpr->iTable = pE->iTable;
580 break;
581 }
582 }
583 if( i>=pParse->nVarExpr ){
584 pExpr->iTable = ++pParse->nVar;
585 if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){
586 pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10;
drh17435752007-08-16 04:30:38 +0000587 pParse->apVarExpr =
588 sqlite3DbReallocOrFree(
589 db,
590 pParse->apVarExpr,
591 pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0])
592 );
drhfa6bc002004-09-07 16:19:52 +0000593 }
drh17435752007-08-16 04:30:38 +0000594 if( !db->mallocFailed ){
drhfa6bc002004-09-07 16:19:52 +0000595 assert( pParse->apVarExpr!=0 );
596 pParse->apVarExpr[pParse->nVarExpr++] = pExpr;
597 }
598 }
599 }
drhbb4957f2008-03-20 14:03:29 +0000600 if( !pParse->nErr && pParse->nVar>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
danielk1977832b2662007-05-09 11:37:22 +0000601 sqlite3ErrorMsg(pParse, "too many SQL variables");
602 }
drhfa6bc002004-09-07 16:19:52 +0000603}
604
605/*
drh10fe8402008-10-11 16:47:35 +0000606** Clear an expression structure without deleting the structure itself.
607** Substructure is deleted.
drha2e00042002-01-22 03:13:42 +0000608*/
drh10fe8402008-10-11 16:47:35 +0000609void sqlite3ExprClear(sqlite3 *db, Expr *p){
drh633e6d52008-07-28 19:34:53 +0000610 if( p->span.dyn ) sqlite3DbFree(db, (char*)p->span.z);
611 if( p->token.dyn ) sqlite3DbFree(db, (char*)p->token.z);
612 sqlite3ExprDelete(db, p->pLeft);
613 sqlite3ExprDelete(db, p->pRight);
614 sqlite3ExprListDelete(db, p->pList);
615 sqlite3SelectDelete(db, p->pSelect);
drh10fe8402008-10-11 16:47:35 +0000616}
617
618/*
619** Recursively delete an expression tree.
620*/
621void sqlite3ExprDelete(sqlite3 *db, Expr *p){
622 if( p==0 ) return;
623 sqlite3ExprClear(db, p);
drh633e6d52008-07-28 19:34:53 +0000624 sqlite3DbFree(db, p);
drha2e00042002-01-22 03:13:42 +0000625}
626
drhd2687b72005-08-12 22:56:09 +0000627/*
628** The Expr.token field might be a string literal that is quoted.
629** If so, remove the quotation marks.
630*/
drh17435752007-08-16 04:30:38 +0000631void sqlite3DequoteExpr(sqlite3 *db, Expr *p){
drhd2687b72005-08-12 22:56:09 +0000632 if( ExprHasAnyProperty(p, EP_Dequoted) ){
633 return;
634 }
635 ExprSetProperty(p, EP_Dequoted);
636 if( p->token.dyn==0 ){
drh17435752007-08-16 04:30:38 +0000637 sqlite3TokenCopy(db, &p->token, &p->token);
drhd2687b72005-08-12 22:56:09 +0000638 }
639 sqlite3Dequote((char*)p->token.z);
640}
641
drha76b5df2002-02-23 02:32:10 +0000642/*
drhff78bd22002-02-27 01:47:11 +0000643** The following group of routines make deep copies of expressions,
644** expression lists, ID lists, and select statements. The copies can
645** be deleted (by being passed to their respective ...Delete() routines)
646** without effecting the originals.
647**
danielk19774adee202004-05-08 08:23:19 +0000648** The expression list, ID, and source lists return by sqlite3ExprListDup(),
649** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
drhad3cab52002-05-24 02:04:32 +0000650** by subsequent calls to sqlite*ListAppend() routines.
drhff78bd22002-02-27 01:47:11 +0000651**
drhad3cab52002-05-24 02:04:32 +0000652** Any tables that the SrcList might point to are not duplicated.
drhff78bd22002-02-27 01:47:11 +0000653*/
danielk19771e536952007-08-16 10:09:01 +0000654Expr *sqlite3ExprDup(sqlite3 *db, Expr *p){
drhff78bd22002-02-27 01:47:11 +0000655 Expr *pNew;
656 if( p==0 ) return 0;
drh17435752007-08-16 04:30:38 +0000657 pNew = sqlite3DbMallocRaw(db, sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000658 if( pNew==0 ) return 0;
drh3b167c72002-06-28 12:18:47 +0000659 memcpy(pNew, p, sizeof(*pNew));
drh6977fea2002-10-22 23:38:04 +0000660 if( p->token.z!=0 ){
drh17435752007-08-16 04:30:38 +0000661 pNew->token.z = (u8*)sqlite3DbStrNDup(db, (char*)p->token.z, p->token.n);
drh4b59ab52002-08-24 18:24:51 +0000662 pNew->token.dyn = 1;
663 }else{
drh4efc4752004-01-16 15:55:37 +0000664 assert( pNew->token.z==0 );
drh4b59ab52002-08-24 18:24:51 +0000665 }
drh6977fea2002-10-22 23:38:04 +0000666 pNew->span.z = 0;
drh17435752007-08-16 04:30:38 +0000667 pNew->pLeft = sqlite3ExprDup(db, p->pLeft);
668 pNew->pRight = sqlite3ExprDup(db, p->pRight);
669 pNew->pList = sqlite3ExprListDup(db, p->pList);
670 pNew->pSelect = sqlite3SelectDup(db, p->pSelect);
drhff78bd22002-02-27 01:47:11 +0000671 return pNew;
672}
drh17435752007-08-16 04:30:38 +0000673void sqlite3TokenCopy(sqlite3 *db, Token *pTo, Token *pFrom){
drh633e6d52008-07-28 19:34:53 +0000674 if( pTo->dyn ) sqlite3DbFree(db, (char*)pTo->z);
drh4b59ab52002-08-24 18:24:51 +0000675 if( pFrom->z ){
676 pTo->n = pFrom->n;
drh17435752007-08-16 04:30:38 +0000677 pTo->z = (u8*)sqlite3DbStrNDup(db, (char*)pFrom->z, pFrom->n);
drh4b59ab52002-08-24 18:24:51 +0000678 pTo->dyn = 1;
679 }else{
drh4b59ab52002-08-24 18:24:51 +0000680 pTo->z = 0;
drh4b59ab52002-08-24 18:24:51 +0000681 }
682}
drh17435752007-08-16 04:30:38 +0000683ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p){
drhff78bd22002-02-27 01:47:11 +0000684 ExprList *pNew;
drh145716b2004-09-24 12:24:06 +0000685 struct ExprList_item *pItem, *pOldItem;
drhff78bd22002-02-27 01:47:11 +0000686 int i;
687 if( p==0 ) return 0;
drh17435752007-08-16 04:30:38 +0000688 pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +0000689 if( pNew==0 ) return 0;
danielk197731dad9d2007-08-16 11:36:15 +0000690 pNew->iECursor = 0;
drh4305d102003-07-30 12:34:12 +0000691 pNew->nExpr = pNew->nAlloc = p->nExpr;
drh17435752007-08-16 04:30:38 +0000692 pNew->a = pItem = sqlite3DbMallocRaw(db, p->nExpr*sizeof(p->a[0]) );
danielk1977e0048402004-06-15 16:51:01 +0000693 if( pItem==0 ){
drh633e6d52008-07-28 19:34:53 +0000694 sqlite3DbFree(db, pNew);
danielk1977e0048402004-06-15 16:51:01 +0000695 return 0;
696 }
drh145716b2004-09-24 12:24:06 +0000697 pOldItem = p->a;
698 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
drh4b59ab52002-08-24 18:24:51 +0000699 Expr *pNewExpr, *pOldExpr;
drh17435752007-08-16 04:30:38 +0000700 pItem->pExpr = pNewExpr = sqlite3ExprDup(db, pOldExpr = pOldItem->pExpr);
drh6977fea2002-10-22 23:38:04 +0000701 if( pOldExpr->span.z!=0 && pNewExpr ){
702 /* Always make a copy of the span for top-level expressions in the
drh4b59ab52002-08-24 18:24:51 +0000703 ** expression list. The logic in SELECT processing that determines
704 ** the names of columns in the result set needs this information */
drh17435752007-08-16 04:30:38 +0000705 sqlite3TokenCopy(db, &pNewExpr->span, &pOldExpr->span);
drh4b59ab52002-08-24 18:24:51 +0000706 }
drh1f3e9052002-10-31 00:09:39 +0000707 assert( pNewExpr==0 || pNewExpr->span.z!=0
drh6f7adc82006-01-11 21:41:20 +0000708 || pOldExpr->span.z==0
drh17435752007-08-16 04:30:38 +0000709 || db->mallocFailed );
710 pItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
drh145716b2004-09-24 12:24:06 +0000711 pItem->sortOrder = pOldItem->sortOrder;
drh3e7bc9c2004-02-21 19:17:17 +0000712 pItem->done = 0;
drh7d10d5a2008-08-20 16:35:10 +0000713 pItem->iCol = pOldItem->iCol;
drh8b213892008-08-29 02:14:02 +0000714 pItem->iAlias = pOldItem->iAlias;
drhff78bd22002-02-27 01:47:11 +0000715 }
716 return pNew;
717}
danielk197793758c82005-01-21 08:13:14 +0000718
719/*
720** If cursors, triggers, views and subqueries are all omitted from
721** the build, then none of the following routines, except for
722** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes
723** called with a NULL argument.
724*/
danielk19776a67fe82005-02-04 04:07:16 +0000725#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \
726 || !defined(SQLITE_OMIT_SUBQUERY)
drh17435752007-08-16 04:30:38 +0000727SrcList *sqlite3SrcListDup(sqlite3 *db, SrcList *p){
drhad3cab52002-05-24 02:04:32 +0000728 SrcList *pNew;
729 int i;
drh113088e2003-03-20 01:16:58 +0000730 int nByte;
drhad3cab52002-05-24 02:04:32 +0000731 if( p==0 ) return 0;
drh113088e2003-03-20 01:16:58 +0000732 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
drh17435752007-08-16 04:30:38 +0000733 pNew = sqlite3DbMallocRaw(db, nByte );
drhad3cab52002-05-24 02:04:32 +0000734 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000735 pNew->nSrc = pNew->nAlloc = p->nSrc;
drhad3cab52002-05-24 02:04:32 +0000736 for(i=0; i<p->nSrc; i++){
drh4efc4752004-01-16 15:55:37 +0000737 struct SrcList_item *pNewItem = &pNew->a[i];
738 struct SrcList_item *pOldItem = &p->a[i];
drhed8a3bb2005-06-06 21:19:56 +0000739 Table *pTab;
drh17435752007-08-16 04:30:38 +0000740 pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase);
741 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
742 pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias);
drh4efc4752004-01-16 15:55:37 +0000743 pNewItem->jointype = pOldItem->jointype;
744 pNewItem->iCursor = pOldItem->iCursor;
danielk19771787cca2006-02-10 07:07:14 +0000745 pNewItem->isPopulated = pOldItem->isPopulated;
danielk197785574e32008-10-06 05:32:18 +0000746 pNewItem->zIndex = sqlite3DbStrDup(db, pOldItem->zIndex);
747 pNewItem->notIndexed = pOldItem->notIndexed;
748 pNewItem->pIndex = pOldItem->pIndex;
drhed8a3bb2005-06-06 21:19:56 +0000749 pTab = pNewItem->pTab = pOldItem->pTab;
750 if( pTab ){
751 pTab->nRef++;
danielk1977a1cb1832005-02-12 08:59:55 +0000752 }
drh17435752007-08-16 04:30:38 +0000753 pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect);
754 pNewItem->pOn = sqlite3ExprDup(db, pOldItem->pOn);
755 pNewItem->pUsing = sqlite3IdListDup(db, pOldItem->pUsing);
danielk19776c18b6e2005-01-30 09:17:58 +0000756 pNewItem->colUsed = pOldItem->colUsed;
drhad3cab52002-05-24 02:04:32 +0000757 }
758 return pNew;
759}
drh17435752007-08-16 04:30:38 +0000760IdList *sqlite3IdListDup(sqlite3 *db, IdList *p){
drhff78bd22002-02-27 01:47:11 +0000761 IdList *pNew;
762 int i;
763 if( p==0 ) return 0;
drh17435752007-08-16 04:30:38 +0000764 pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +0000765 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000766 pNew->nId = pNew->nAlloc = p->nId;
drh17435752007-08-16 04:30:38 +0000767 pNew->a = sqlite3DbMallocRaw(db, p->nId*sizeof(p->a[0]) );
danielk1977d5d56522005-03-16 12:15:20 +0000768 if( pNew->a==0 ){
drh633e6d52008-07-28 19:34:53 +0000769 sqlite3DbFree(db, pNew);
danielk1977d5d56522005-03-16 12:15:20 +0000770 return 0;
771 }
drhff78bd22002-02-27 01:47:11 +0000772 for(i=0; i<p->nId; i++){
drh4efc4752004-01-16 15:55:37 +0000773 struct IdList_item *pNewItem = &pNew->a[i];
774 struct IdList_item *pOldItem = &p->a[i];
drh17435752007-08-16 04:30:38 +0000775 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
drh4efc4752004-01-16 15:55:37 +0000776 pNewItem->idx = pOldItem->idx;
drhff78bd22002-02-27 01:47:11 +0000777 }
778 return pNew;
779}
drh17435752007-08-16 04:30:38 +0000780Select *sqlite3SelectDup(sqlite3 *db, Select *p){
drhff78bd22002-02-27 01:47:11 +0000781 Select *pNew;
782 if( p==0 ) return 0;
drh17435752007-08-16 04:30:38 +0000783 pNew = sqlite3DbMallocRaw(db, sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000784 if( pNew==0 ) return 0;
drh17435752007-08-16 04:30:38 +0000785 pNew->pEList = sqlite3ExprListDup(db, p->pEList);
786 pNew->pSrc = sqlite3SrcListDup(db, p->pSrc);
787 pNew->pWhere = sqlite3ExprDup(db, p->pWhere);
788 pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy);
789 pNew->pHaving = sqlite3ExprDup(db, p->pHaving);
790 pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy);
drhff78bd22002-02-27 01:47:11 +0000791 pNew->op = p->op;
drh17435752007-08-16 04:30:38 +0000792 pNew->pPrior = sqlite3SelectDup(db, p->pPrior);
793 pNew->pLimit = sqlite3ExprDup(db, p->pLimit);
794 pNew->pOffset = sqlite3ExprDup(db, p->pOffset);
drh92b01d52008-06-24 00:32:35 +0000795 pNew->iLimit = 0;
796 pNew->iOffset = 0;
drh7d10d5a2008-08-20 16:35:10 +0000797 pNew->selFlags = p->selFlags & ~SF_UsesEphemeral;
drh0342b1f2005-09-01 03:07:44 +0000798 pNew->pRightmost = 0;
drhb9bb7c12006-06-11 23:41:55 +0000799 pNew->addrOpenEphm[0] = -1;
800 pNew->addrOpenEphm[1] = -1;
801 pNew->addrOpenEphm[2] = -1;
drhff78bd22002-02-27 01:47:11 +0000802 return pNew;
803}
danielk197793758c82005-01-21 08:13:14 +0000804#else
drh17435752007-08-16 04:30:38 +0000805Select *sqlite3SelectDup(sqlite3 *db, Select *p){
danielk197793758c82005-01-21 08:13:14 +0000806 assert( p==0 );
807 return 0;
808}
809#endif
drhff78bd22002-02-27 01:47:11 +0000810
811
812/*
drha76b5df2002-02-23 02:32:10 +0000813** Add a new element to the end of an expression list. If pList is
814** initially NULL, then create a new expression list.
815*/
drh17435752007-08-16 04:30:38 +0000816ExprList *sqlite3ExprListAppend(
817 Parse *pParse, /* Parsing context */
818 ExprList *pList, /* List to which to append. Might be NULL */
819 Expr *pExpr, /* Expression to be appended */
820 Token *pName /* AS keyword for the expression */
821){
822 sqlite3 *db = pParse->db;
drha76b5df2002-02-23 02:32:10 +0000823 if( pList==0 ){
drh17435752007-08-16 04:30:38 +0000824 pList = sqlite3DbMallocZero(db, sizeof(ExprList) );
drha76b5df2002-02-23 02:32:10 +0000825 if( pList==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000826 goto no_mem;
drha76b5df2002-02-23 02:32:10 +0000827 }
drh4efc4752004-01-16 15:55:37 +0000828 assert( pList->nAlloc==0 );
drha76b5df2002-02-23 02:32:10 +0000829 }
drh4305d102003-07-30 12:34:12 +0000830 if( pList->nAlloc<=pList->nExpr ){
danielk1977d5d56522005-03-16 12:15:20 +0000831 struct ExprList_item *a;
832 int n = pList->nAlloc*2 + 4;
danielk197726783a52007-08-29 14:06:22 +0000833 a = sqlite3DbRealloc(db, pList->a, n*sizeof(pList->a[0]));
danielk1977d5d56522005-03-16 12:15:20 +0000834 if( a==0 ){
835 goto no_mem;
drha76b5df2002-02-23 02:32:10 +0000836 }
danielk1977d5d56522005-03-16 12:15:20 +0000837 pList->a = a;
drh6a1e0712008-12-05 15:24:15 +0000838 pList->nAlloc = sqlite3DbMallocSize(db, a)/sizeof(a[0]);
drha76b5df2002-02-23 02:32:10 +0000839 }
drh4efc4752004-01-16 15:55:37 +0000840 assert( pList->a!=0 );
841 if( pExpr || pName ){
842 struct ExprList_item *pItem = &pList->a[pList->nExpr++];
843 memset(pItem, 0, sizeof(*pItem));
drh17435752007-08-16 04:30:38 +0000844 pItem->zName = sqlite3NameFromToken(db, pName);
danielk1977e94ddc92005-03-21 03:53:38 +0000845 pItem->pExpr = pExpr;
drh8b213892008-08-29 02:14:02 +0000846 pItem->iAlias = 0;
drha76b5df2002-02-23 02:32:10 +0000847 }
848 return pList;
danielk1977d5d56522005-03-16 12:15:20 +0000849
850no_mem:
851 /* Avoid leaking memory if malloc has failed. */
drh633e6d52008-07-28 19:34:53 +0000852 sqlite3ExprDelete(db, pExpr);
853 sqlite3ExprListDelete(db, pList);
danielk1977d5d56522005-03-16 12:15:20 +0000854 return 0;
drha76b5df2002-02-23 02:32:10 +0000855}
856
857/*
danielk19777a15a4b2007-05-08 17:54:43 +0000858** If the expression list pEList contains more than iLimit elements,
859** leave an error message in pParse.
860*/
861void sqlite3ExprListCheckLength(
862 Parse *pParse,
863 ExprList *pEList,
danielk19777a15a4b2007-05-08 17:54:43 +0000864 const char *zObject
865){
drhb1a6c3c2008-03-20 16:30:17 +0000866 int mx = pParse->db->aLimit[SQLITE_LIMIT_COLUMN];
drhc5499be2008-04-01 15:06:33 +0000867 testcase( pEList && pEList->nExpr==mx );
868 testcase( pEList && pEList->nExpr==mx+1 );
drhb1a6c3c2008-03-20 16:30:17 +0000869 if( pEList && pEList->nExpr>mx ){
danielk19777a15a4b2007-05-08 17:54:43 +0000870 sqlite3ErrorMsg(pParse, "too many columns in %s", zObject);
871 }
872}
873
874/*
drha76b5df2002-02-23 02:32:10 +0000875** Delete an entire expression list.
876*/
drh633e6d52008-07-28 19:34:53 +0000877void sqlite3ExprListDelete(sqlite3 *db, ExprList *pList){
drha76b5df2002-02-23 02:32:10 +0000878 int i;
drhbe5c89a2004-07-26 00:31:09 +0000879 struct ExprList_item *pItem;
drha76b5df2002-02-23 02:32:10 +0000880 if( pList==0 ) return;
drh1bdd9b52004-04-23 17:04:44 +0000881 assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
882 assert( pList->nExpr<=pList->nAlloc );
drhbe5c89a2004-07-26 00:31:09 +0000883 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
drh633e6d52008-07-28 19:34:53 +0000884 sqlite3ExprDelete(db, pItem->pExpr);
885 sqlite3DbFree(db, pItem->zName);
drha76b5df2002-02-23 02:32:10 +0000886 }
drh633e6d52008-07-28 19:34:53 +0000887 sqlite3DbFree(db, pList->a);
888 sqlite3DbFree(db, pList);
drha76b5df2002-02-23 02:32:10 +0000889}
890
891/*
drh7d10d5a2008-08-20 16:35:10 +0000892** These routines are Walker callbacks. Walker.u.pi is a pointer
893** to an integer. These routines are checking an expression to see
894** if it is a constant. Set *Walker.u.pi to 0 if the expression is
895** not constant.
drh73b211a2005-01-18 04:00:42 +0000896**
drh7d10d5a2008-08-20 16:35:10 +0000897** These callback routines are used to implement the following:
drh626a8792005-01-17 22:08:19 +0000898**
drh7d10d5a2008-08-20 16:35:10 +0000899** sqlite3ExprIsConstant()
900** sqlite3ExprIsConstantNotJoin()
901** sqlite3ExprIsConstantOrFunction()
drh87abf5c2005-08-25 12:45:04 +0000902**
drh626a8792005-01-17 22:08:19 +0000903*/
drh7d10d5a2008-08-20 16:35:10 +0000904static int exprNodeIsConstant(Walker *pWalker, Expr *pExpr){
drh626a8792005-01-17 22:08:19 +0000905
drh7d10d5a2008-08-20 16:35:10 +0000906 /* If pWalker->u.i is 3 then any term of the expression that comes from
drh0a168372007-06-08 00:20:47 +0000907 ** the ON or USING clauses of a join disqualifies the expression
908 ** from being considered constant. */
drh7d10d5a2008-08-20 16:35:10 +0000909 if( pWalker->u.i==3 && ExprHasAnyProperty(pExpr, EP_FromJoin) ){
910 pWalker->u.i = 0;
911 return WRC_Abort;
drh0a168372007-06-08 00:20:47 +0000912 }
913
drh626a8792005-01-17 22:08:19 +0000914 switch( pExpr->op ){
drheb55bd22005-06-30 17:04:21 +0000915 /* Consider functions to be constant if all their arguments are constant
drh7d10d5a2008-08-20 16:35:10 +0000916 ** and pWalker->u.i==2 */
drheb55bd22005-06-30 17:04:21 +0000917 case TK_FUNCTION:
drh7d10d5a2008-08-20 16:35:10 +0000918 if( pWalker->u.i==2 ) return 0;
drheb55bd22005-06-30 17:04:21 +0000919 /* Fall through */
drh626a8792005-01-17 22:08:19 +0000920 case TK_ID:
921 case TK_COLUMN:
drh626a8792005-01-17 22:08:19 +0000922 case TK_AGG_FUNCTION:
drh13449892005-09-07 21:22:45 +0000923 case TK_AGG_COLUMN:
drhfe2093d2005-01-20 22:48:47 +0000924#ifndef SQLITE_OMIT_SUBQUERY
925 case TK_SELECT:
926 case TK_EXISTS:
drhc5499be2008-04-01 15:06:33 +0000927 testcase( pExpr->op==TK_SELECT );
928 testcase( pExpr->op==TK_EXISTS );
drhfe2093d2005-01-20 22:48:47 +0000929#endif
drhc5499be2008-04-01 15:06:33 +0000930 testcase( pExpr->op==TK_ID );
931 testcase( pExpr->op==TK_COLUMN );
drhc5499be2008-04-01 15:06:33 +0000932 testcase( pExpr->op==TK_AGG_FUNCTION );
933 testcase( pExpr->op==TK_AGG_COLUMN );
drh7d10d5a2008-08-20 16:35:10 +0000934 pWalker->u.i = 0;
935 return WRC_Abort;
drh626a8792005-01-17 22:08:19 +0000936 default:
drh7d10d5a2008-08-20 16:35:10 +0000937 return WRC_Continue;
drh626a8792005-01-17 22:08:19 +0000938 }
939}
danielk197762c14b32008-11-19 09:05:26 +0000940static int selectNodeIsConstant(Walker *pWalker, Select *NotUsed){
941 UNUSED_PARAMETER(NotUsed);
drh7d10d5a2008-08-20 16:35:10 +0000942 pWalker->u.i = 0;
943 return WRC_Abort;
944}
945static int exprIsConst(Expr *p, int initFlag){
946 Walker w;
947 w.u.i = initFlag;
948 w.xExprCallback = exprNodeIsConstant;
949 w.xSelectCallback = selectNodeIsConstant;
950 sqlite3WalkExpr(&w, p);
951 return w.u.i;
952}
drh626a8792005-01-17 22:08:19 +0000953
954/*
drhfef52082000-06-06 01:50:43 +0000955** Walk an expression tree. Return 1 if the expression is constant
drheb55bd22005-06-30 17:04:21 +0000956** and 0 if it involves variables or function calls.
drh23989372002-05-21 13:43:04 +0000957**
958** For the purposes of this function, a double-quoted string (ex: "abc")
959** is considered a variable but a single-quoted string (ex: 'abc') is
960** a constant.
drhfef52082000-06-06 01:50:43 +0000961*/
danielk19774adee202004-05-08 08:23:19 +0000962int sqlite3ExprIsConstant(Expr *p){
drh7d10d5a2008-08-20 16:35:10 +0000963 return exprIsConst(p, 1);
drhfef52082000-06-06 01:50:43 +0000964}
965
966/*
drheb55bd22005-06-30 17:04:21 +0000967** Walk an expression tree. Return 1 if the expression is constant
drh0a168372007-06-08 00:20:47 +0000968** that does no originate from the ON or USING clauses of a join.
969** Return 0 if it involves variables or function calls or terms from
970** an ON or USING clause.
971*/
972int sqlite3ExprIsConstantNotJoin(Expr *p){
drh7d10d5a2008-08-20 16:35:10 +0000973 return exprIsConst(p, 3);
drh0a168372007-06-08 00:20:47 +0000974}
975
976/*
977** Walk an expression tree. Return 1 if the expression is constant
drheb55bd22005-06-30 17:04:21 +0000978** or a function call with constant arguments. Return and 0 if there
979** are any variables.
980**
981** For the purposes of this function, a double-quoted string (ex: "abc")
982** is considered a variable but a single-quoted string (ex: 'abc') is
983** a constant.
984*/
985int sqlite3ExprIsConstantOrFunction(Expr *p){
drh7d10d5a2008-08-20 16:35:10 +0000986 return exprIsConst(p, 2);
drheb55bd22005-06-30 17:04:21 +0000987}
988
989/*
drh73b211a2005-01-18 04:00:42 +0000990** If the expression p codes a constant integer that is small enough
drh202b2df2004-01-06 01:13:46 +0000991** to fit in a 32-bit integer, return 1 and put the value of the integer
992** in *pValue. If the expression is not an integer or if it is too big
993** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
drhe4de1fe2002-06-02 16:09:01 +0000994*/
danielk19774adee202004-05-08 08:23:19 +0000995int sqlite3ExprIsInteger(Expr *p, int *pValue){
drh92b01d52008-06-24 00:32:35 +0000996 int rc = 0;
997 if( p->flags & EP_IntValue ){
998 *pValue = p->iTable;
999 return 1;
1000 }
drhe4de1fe2002-06-02 16:09:01 +00001001 switch( p->op ){
1002 case TK_INTEGER: {
drh92b01d52008-06-24 00:32:35 +00001003 rc = sqlite3GetInt32((char*)p->token.z, pValue);
drh202b2df2004-01-06 01:13:46 +00001004 break;
drhe4de1fe2002-06-02 16:09:01 +00001005 }
drh4b59ab52002-08-24 18:24:51 +00001006 case TK_UPLUS: {
drh92b01d52008-06-24 00:32:35 +00001007 rc = sqlite3ExprIsInteger(p->pLeft, pValue);
drhf6e369a2008-06-24 12:46:30 +00001008 break;
drh4b59ab52002-08-24 18:24:51 +00001009 }
drhe4de1fe2002-06-02 16:09:01 +00001010 case TK_UMINUS: {
1011 int v;
danielk19774adee202004-05-08 08:23:19 +00001012 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
drhe4de1fe2002-06-02 16:09:01 +00001013 *pValue = -v;
drh92b01d52008-06-24 00:32:35 +00001014 rc = 1;
drhe4de1fe2002-06-02 16:09:01 +00001015 }
1016 break;
1017 }
1018 default: break;
1019 }
drh92b01d52008-06-24 00:32:35 +00001020 if( rc ){
1021 p->op = TK_INTEGER;
1022 p->flags |= EP_IntValue;
1023 p->iTable = *pValue;
1024 }
1025 return rc;
drhe4de1fe2002-06-02 16:09:01 +00001026}
1027
1028/*
drhc4a3c772001-04-04 11:48:57 +00001029** Return TRUE if the given string is a row-id column name.
1030*/
danielk19774adee202004-05-08 08:23:19 +00001031int sqlite3IsRowid(const char *z){
1032 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
1033 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
1034 if( sqlite3StrICmp(z, "OID")==0 ) return 1;
drhc4a3c772001-04-04 11:48:57 +00001035 return 0;
1036}
1037
danielk19779a96b662007-11-29 17:05:18 +00001038/*
drhb287f4b2008-04-25 00:08:38 +00001039** Return true if the IN operator optimization is enabled and
1040** the SELECT statement p exists and is of the
1041** simple form:
1042**
1043** SELECT <column> FROM <table>
1044**
1045** If this is the case, it may be possible to use an existing table
1046** or index instead of generating an epheremal table.
1047*/
1048#ifndef SQLITE_OMIT_SUBQUERY
1049static int isCandidateForInOpt(Select *p){
1050 SrcList *pSrc;
1051 ExprList *pEList;
1052 Table *pTab;
drhb287f4b2008-04-25 00:08:38 +00001053 if( p==0 ) return 0; /* right-hand side of IN is SELECT */
1054 if( p->pPrior ) return 0; /* Not a compound SELECT */
drh7d10d5a2008-08-20 16:35:10 +00001055 if( p->selFlags & (SF_Distinct|SF_Aggregate) ){
1056 return 0; /* No DISTINCT keyword and no aggregate functions */
1057 }
drhb287f4b2008-04-25 00:08:38 +00001058 if( p->pGroupBy ) return 0; /* Has no GROUP BY clause */
1059 if( p->pLimit ) return 0; /* Has no LIMIT clause */
1060 if( p->pOffset ) return 0;
1061 if( p->pWhere ) return 0; /* Has no WHERE clause */
1062 pSrc = p->pSrc;
drhd1fa7bc2009-01-10 13:24:50 +00001063 assert( pSrc!=0 );
1064 if( pSrc->nSrc!=1 ) return 0; /* Single term in FROM clause */
drhb287f4b2008-04-25 00:08:38 +00001065 if( pSrc->a[0].pSelect ) return 0; /* FROM clause is not a subquery */
1066 pTab = pSrc->a[0].pTab;
1067 if( pTab==0 ) return 0;
1068 if( pTab->pSelect ) return 0; /* FROM clause is not a view */
1069 if( IsVirtual(pTab) ) return 0; /* FROM clause not a virtual table */
1070 pEList = p->pEList;
1071 if( pEList->nExpr!=1 ) return 0; /* One column in the result set */
1072 if( pEList->a[0].pExpr->op!=TK_COLUMN ) return 0; /* Result is a column */
1073 return 1;
1074}
1075#endif /* SQLITE_OMIT_SUBQUERY */
1076
1077/*
danielk19779a96b662007-11-29 17:05:18 +00001078** This function is used by the implementation of the IN (...) operator.
1079** It's job is to find or create a b-tree structure that may be used
1080** either to test for membership of the (...) set or to iterate through
drh85b623f2007-12-13 21:54:09 +00001081** its members, skipping duplicates.
danielk19779a96b662007-11-29 17:05:18 +00001082**
1083** The cursor opened on the structure (database table, database index
1084** or ephermal table) is stored in pX->iTable before this function returns.
1085** The returned value indicates the structure type, as follows:
1086**
1087** IN_INDEX_ROWID - The cursor was opened on a database table.
drh2d401ab2008-01-10 23:50:11 +00001088** IN_INDEX_INDEX - The cursor was opened on a database index.
danielk19779a96b662007-11-29 17:05:18 +00001089** IN_INDEX_EPH - The cursor was opened on a specially created and
1090** populated epheremal table.
1091**
1092** An existing structure may only be used if the SELECT is of the simple
1093** form:
1094**
1095** SELECT <column> FROM <table>
1096**
danielk19770cdc0222008-06-26 18:04:03 +00001097** If prNotFound parameter is 0, then the structure will be used to iterate
danielk19779a96b662007-11-29 17:05:18 +00001098** through the set members, skipping any duplicates. In this case an
1099** epheremal table must be used unless the selected <column> is guaranteed
1100** to be unique - either because it is an INTEGER PRIMARY KEY or it
1101** is unique by virtue of a constraint or implicit index.
danielk19770cdc0222008-06-26 18:04:03 +00001102**
1103** If the prNotFound parameter is not 0, then the structure will be used
1104** for fast set membership tests. In this case an epheremal table must
1105** be used unless <column> is an INTEGER PRIMARY KEY or an index can
1106** be found with <column> as its left-most column.
1107**
1108** When the structure is being used for set membership tests, the user
1109** needs to know whether or not the structure contains an SQL NULL
1110** value in order to correctly evaluate expressions like "X IN (Y, Z)".
1111** If there is a chance that the structure may contain a NULL value at
1112** runtime, then a register is allocated and the register number written
1113** to *prNotFound. If there is no chance that the structure contains a
1114** NULL value, then *prNotFound is left unchanged.
1115**
1116** If a register is allocated and its location stored in *prNotFound, then
1117** its initial value is NULL. If the structure does not remain constant
1118** for the duration of the query (i.e. the set is a correlated sub-select),
1119** the value of the allocated register is reset to NULL each time the
1120** structure is repopulated. This allows the caller to use vdbe code
1121** equivalent to the following:
1122**
1123** if( register==NULL ){
1124** has_null = <test if data structure contains null>
1125** register = 1
1126** }
1127**
1128** in order to avoid running the <test if data structure contains null>
1129** test more often than is necessary.
danielk19779a96b662007-11-29 17:05:18 +00001130*/
danielk1977284f4ac2007-12-10 05:03:46 +00001131#ifndef SQLITE_OMIT_SUBQUERY
danielk19770cdc0222008-06-26 18:04:03 +00001132int sqlite3FindInIndex(Parse *pParse, Expr *pX, int *prNotFound){
danielk19779a96b662007-11-29 17:05:18 +00001133 Select *p;
1134 int eType = 0;
1135 int iTab = pParse->nTab++;
danielk19770cdc0222008-06-26 18:04:03 +00001136 int mustBeUnique = !prNotFound;
danielk19779a96b662007-11-29 17:05:18 +00001137
1138 /* The follwing if(...) expression is true if the SELECT is of the
1139 ** simple form:
1140 **
1141 ** SELECT <column> FROM <table>
1142 **
1143 ** If this is the case, it may be possible to use an existing table
1144 ** or index instead of generating an epheremal table.
1145 */
drhb287f4b2008-04-25 00:08:38 +00001146 p = pX->pSelect;
1147 if( isCandidateForInOpt(p) ){
danielk19779a96b662007-11-29 17:05:18 +00001148 sqlite3 *db = pParse->db;
1149 Index *pIdx;
1150 Expr *pExpr = p->pEList->a[0].pExpr;
1151 int iCol = pExpr->iColumn;
1152 Vdbe *v = sqlite3GetVdbe(pParse);
1153
1154 /* This function is only called from two places. In both cases the vdbe
1155 ** has already been allocated. So assume sqlite3GetVdbe() is always
1156 ** successful here.
1157 */
1158 assert(v);
1159 if( iCol<0 ){
drh0a07c102008-01-03 18:03:08 +00001160 int iMem = ++pParse->nMem;
danielk19779a96b662007-11-29 17:05:18 +00001161 int iAddr;
1162 Table *pTab = p->pSrc->a[0].pTab;
1163 int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
1164 sqlite3VdbeUsesBtree(v, iDb);
1165
drh892d3172008-01-10 03:46:36 +00001166 iAddr = sqlite3VdbeAddOp1(v, OP_If, iMem);
drh4c583122008-01-04 22:01:03 +00001167 sqlite3VdbeAddOp2(v, OP_Integer, 1, iMem);
danielk19779a96b662007-11-29 17:05:18 +00001168
1169 sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
1170 eType = IN_INDEX_ROWID;
1171
1172 sqlite3VdbeJumpHere(v, iAddr);
1173 }else{
1174 /* The collation sequence used by the comparison. If an index is to
1175 ** be used in place of a temp-table, it must be ordered according
1176 ** to this collation sequence.
1177 */
1178 CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pExpr);
1179
1180 /* Check that the affinity that will be used to perform the
1181 ** comparison is the same as the affinity of the column. If
1182 ** it is not, it is not possible to use any index.
1183 */
1184 Table *pTab = p->pSrc->a[0].pTab;
1185 char aff = comparisonAffinity(pX);
1186 int affinity_ok = (pTab->aCol[iCol].affinity==aff||aff==SQLITE_AFF_NONE);
1187
1188 for(pIdx=pTab->pIndex; pIdx && eType==0 && affinity_ok; pIdx=pIdx->pNext){
1189 if( (pIdx->aiColumn[0]==iCol)
1190 && (pReq==sqlite3FindCollSeq(db, ENC(db), pIdx->azColl[0], -1, 0))
1191 && (!mustBeUnique || (pIdx->nColumn==1 && pIdx->onError!=OE_None))
1192 ){
1193 int iDb;
drh0a07c102008-01-03 18:03:08 +00001194 int iMem = ++pParse->nMem;
danielk19779a96b662007-11-29 17:05:18 +00001195 int iAddr;
1196 char *pKey;
1197
1198 pKey = (char *)sqlite3IndexKeyinfo(pParse, pIdx);
1199 iDb = sqlite3SchemaToIndex(db, pIdx->pSchema);
1200 sqlite3VdbeUsesBtree(v, iDb);
1201
drh892d3172008-01-10 03:46:36 +00001202 iAddr = sqlite3VdbeAddOp1(v, OP_If, iMem);
drh4c583122008-01-04 22:01:03 +00001203 sqlite3VdbeAddOp2(v, OP_Integer, 1, iMem);
danielk19779a96b662007-11-29 17:05:18 +00001204
danielk1977cd3e8f72008-03-25 09:47:35 +00001205 sqlite3VdbeAddOp2(v, OP_SetNumColumns, 0, pIdx->nColumn);
danielk1977207872a2008-01-03 07:54:23 +00001206 sqlite3VdbeAddOp4(v, OP_OpenRead, iTab, pIdx->tnum, iDb,
drh66a51672008-01-03 00:01:23 +00001207 pKey,P4_KEYINFO_HANDOFF);
danielk1977207872a2008-01-03 07:54:23 +00001208 VdbeComment((v, "%s", pIdx->zName));
danielk19779a96b662007-11-29 17:05:18 +00001209 eType = IN_INDEX_INDEX;
danielk19779a96b662007-11-29 17:05:18 +00001210
1211 sqlite3VdbeJumpHere(v, iAddr);
danielk19770cdc0222008-06-26 18:04:03 +00001212 if( prNotFound && !pTab->aCol[iCol].notNull ){
1213 *prNotFound = ++pParse->nMem;
1214 }
danielk19779a96b662007-11-29 17:05:18 +00001215 }
1216 }
1217 }
1218 }
1219
1220 if( eType==0 ){
danielk19770cdc0222008-06-26 18:04:03 +00001221 int rMayHaveNull = 0;
danielk197741a05b72008-10-02 13:50:55 +00001222 eType = IN_INDEX_EPH;
danielk19770cdc0222008-06-26 18:04:03 +00001223 if( prNotFound ){
1224 *prNotFound = rMayHaveNull = ++pParse->nMem;
danielk197741a05b72008-10-02 13:50:55 +00001225 }else if( pX->pLeft->iColumn<0 && pX->pSelect==0 ){
1226 eType = IN_INDEX_ROWID;
danielk19770cdc0222008-06-26 18:04:03 +00001227 }
danielk197741a05b72008-10-02 13:50:55 +00001228 sqlite3CodeSubselect(pParse, pX, rMayHaveNull, eType==IN_INDEX_ROWID);
danielk19779a96b662007-11-29 17:05:18 +00001229 }else{
1230 pX->iTable = iTab;
1231 }
1232 return eType;
1233}
danielk1977284f4ac2007-12-10 05:03:46 +00001234#endif
drh626a8792005-01-17 22:08:19 +00001235
1236/*
drh9cbe6352005-11-29 03:13:21 +00001237** Generate code for scalar subqueries used as an expression
1238** and IN operators. Examples:
drh626a8792005-01-17 22:08:19 +00001239**
drh9cbe6352005-11-29 03:13:21 +00001240** (SELECT a FROM b) -- subquery
1241** EXISTS (SELECT a FROM b) -- EXISTS subquery
1242** x IN (4,5,11) -- IN operator with list on right-hand side
1243** x IN (SELECT a FROM b) -- IN operator with subquery on the right
drhfef52082000-06-06 01:50:43 +00001244**
drh9cbe6352005-11-29 03:13:21 +00001245** The pExpr parameter describes the expression that contains the IN
1246** operator or subquery.
danielk197741a05b72008-10-02 13:50:55 +00001247**
1248** If parameter isRowid is non-zero, then expression pExpr is guaranteed
1249** to be of the form "<rowid> IN (?, ?, ?)", where <rowid> is a reference
1250** to some integer key column of a table B-Tree. In this case, use an
1251** intkey B-Tree to store the set of IN(...) values instead of the usual
1252** (slower) variable length keys B-Tree.
drhcce7d172000-05-31 15:34:51 +00001253*/
drh51522cd2005-01-20 13:36:19 +00001254#ifndef SQLITE_OMIT_SUBQUERY
danielk197741a05b72008-10-02 13:50:55 +00001255void sqlite3CodeSubselect(
1256 Parse *pParse,
1257 Expr *pExpr,
1258 int rMayHaveNull,
1259 int isRowid
1260){
drh57dbd7b2005-07-08 18:25:26 +00001261 int testAddr = 0; /* One-time test address */
danielk1977b3bce662005-01-29 08:32:43 +00001262 Vdbe *v = sqlite3GetVdbe(pParse);
1263 if( v==0 ) return;
1264
danielk1977fc976062007-05-10 10:46:56 +00001265
drh57dbd7b2005-07-08 18:25:26 +00001266 /* This code must be run in its entirety every time it is encountered
1267 ** if any of the following is true:
1268 **
1269 ** * The right-hand side is a correlated subquery
1270 ** * The right-hand side is an expression list containing variables
1271 ** * We are inside a trigger
1272 **
1273 ** If all of the above are false, then we can run this code just once
1274 ** save the results, and reuse the same result on subsequent invocations.
danielk1977b3bce662005-01-29 08:32:43 +00001275 */
1276 if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->trigStack ){
drh0a07c102008-01-03 18:03:08 +00001277 int mem = ++pParse->nMem;
drh892d3172008-01-10 03:46:36 +00001278 sqlite3VdbeAddOp1(v, OP_If, mem);
1279 testAddr = sqlite3VdbeAddOp2(v, OP_Integer, 1, mem);
drh17435752007-08-16 04:30:38 +00001280 assert( testAddr>0 || pParse->db->mallocFailed );
danielk1977b3bce662005-01-29 08:32:43 +00001281 }
1282
drhcce7d172000-05-31 15:34:51 +00001283 switch( pExpr->op ){
drhfef52082000-06-06 01:50:43 +00001284 case TK_IN: {
danielk1977e014a832004-05-17 10:48:57 +00001285 char affinity;
drhd3d39e92004-05-20 22:16:29 +00001286 KeyInfo keyInfo;
drhb9bb7c12006-06-11 23:41:55 +00001287 int addr; /* Address of OP_OpenEphemeral instruction */
danielk197741a05b72008-10-02 13:50:55 +00001288 Expr *pLeft = pExpr->pLeft;
drhd3d39e92004-05-20 22:16:29 +00001289
danielk19770cdc0222008-06-26 18:04:03 +00001290 if( rMayHaveNull ){
1291 sqlite3VdbeAddOp2(v, OP_Null, 0, rMayHaveNull);
1292 }
1293
danielk197741a05b72008-10-02 13:50:55 +00001294 affinity = sqlite3ExprAffinity(pLeft);
danielk1977e014a832004-05-17 10:48:57 +00001295
1296 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
drh57dbd7b2005-07-08 18:25:26 +00001297 ** expression it is handled the same way. A virtual table is
danielk1977e014a832004-05-17 10:48:57 +00001298 ** filled with single-field index keys representing the results
1299 ** from the SELECT or the <exprlist>.
1300 **
1301 ** If the 'x' expression is a column value, or the SELECT...
1302 ** statement returns a column value, then the affinity of that
1303 ** column is used to build the index keys. If both 'x' and the
1304 ** SELECT... statement are columns, then numeric affinity is used
1305 ** if either column has NUMERIC or INTEGER affinity. If neither
1306 ** 'x' nor the SELECT... statement are columns, then numeric affinity
1307 ** is used.
1308 */
1309 pExpr->iTable = pParse->nTab++;
danielk197741a05b72008-10-02 13:50:55 +00001310 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pExpr->iTable, !isRowid);
drhd3d39e92004-05-20 22:16:29 +00001311 memset(&keyInfo, 0, sizeof(keyInfo));
1312 keyInfo.nField = 1;
danielk1977e014a832004-05-17 10:48:57 +00001313
drhfef52082000-06-06 01:50:43 +00001314 if( pExpr->pSelect ){
1315 /* Case 1: expr IN (SELECT ...)
1316 **
danielk1977e014a832004-05-17 10:48:57 +00001317 ** Generate code to write the results of the select into the temporary
1318 ** table allocated and opened above.
drhfef52082000-06-06 01:50:43 +00001319 */
drh1013c932008-01-06 00:25:21 +00001320 SelectDest dest;
drhbe5c89a2004-07-26 00:31:09 +00001321 ExprList *pEList;
drh1013c932008-01-06 00:25:21 +00001322
danielk197741a05b72008-10-02 13:50:55 +00001323 assert( !isRowid );
drh1013c932008-01-06 00:25:21 +00001324 sqlite3SelectDestInit(&dest, SRT_Set, pExpr->iTable);
drh1bd10f82008-12-10 21:19:56 +00001325 dest.affinity = (u8)affinity;
danielk1977e014a832004-05-17 10:48:57 +00001326 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
drh7d10d5a2008-08-20 16:35:10 +00001327 if( sqlite3Select(pParse, pExpr->pSelect, &dest) ){
drh94ccde52007-04-13 16:06:32 +00001328 return;
1329 }
drhbe5c89a2004-07-26 00:31:09 +00001330 pEList = pExpr->pSelect->pEList;
1331 if( pEList && pEList->nExpr>0 ){
danielk1977bcbb04e2007-05-29 12:11:29 +00001332 keyInfo.aColl[0] = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft,
drhbe5c89a2004-07-26 00:31:09 +00001333 pEList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001334 }
drhfef52082000-06-06 01:50:43 +00001335 }else if( pExpr->pList ){
1336 /* Case 2: expr IN (exprlist)
1337 **
drhfd131da2007-08-07 17:13:03 +00001338 ** For each expression, build an index key from the evaluation and
danielk1977e014a832004-05-17 10:48:57 +00001339 ** store it in the temporary table. If <expr> is a column, then use
1340 ** that columns affinity when building index keys. If <expr> is not
1341 ** a column, use numeric affinity.
drhfef52082000-06-06 01:50:43 +00001342 */
danielk1977e014a832004-05-17 10:48:57 +00001343 int i;
drh57dbd7b2005-07-08 18:25:26 +00001344 ExprList *pList = pExpr->pList;
1345 struct ExprList_item *pItem;
drhecc31802008-06-26 20:06:06 +00001346 int r1, r2, r3;
drh57dbd7b2005-07-08 18:25:26 +00001347
danielk1977e014a832004-05-17 10:48:57 +00001348 if( !affinity ){
drh8159a352006-05-23 23:22:29 +00001349 affinity = SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +00001350 }
drh7d10d5a2008-08-20 16:35:10 +00001351 keyInfo.aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +00001352
1353 /* Loop through each expression in <exprlist>. */
drh2d401ab2008-01-10 23:50:11 +00001354 r1 = sqlite3GetTempReg(pParse);
1355 r2 = sqlite3GetTempReg(pParse);
danielk19774e7f36a2008-10-02 16:42:06 +00001356 sqlite3VdbeAddOp2(v, OP_Null, 0, r2);
drh57dbd7b2005-07-08 18:25:26 +00001357 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
1358 Expr *pE2 = pItem->pExpr;
danielk1977e014a832004-05-17 10:48:57 +00001359
drh57dbd7b2005-07-08 18:25:26 +00001360 /* If the expression is not constant then we will need to
1361 ** disable the test that was generated above that makes sure
1362 ** this code only executes once. Because for a non-constant
1363 ** expression we need to rerun this code each time.
1364 */
drh892d3172008-01-10 03:46:36 +00001365 if( testAddr && !sqlite3ExprIsConstant(pE2) ){
1366 sqlite3VdbeChangeToNoop(v, testAddr-1, 2);
drh57dbd7b2005-07-08 18:25:26 +00001367 testAddr = 0;
drh4794b982000-06-06 13:54:14 +00001368 }
danielk1977e014a832004-05-17 10:48:57 +00001369
1370 /* Evaluate the expression and insert it into the temp table */
drhe55cbd72008-03-31 23:48:03 +00001371 pParse->disableColCache++;
drhecc31802008-06-26 20:06:06 +00001372 r3 = sqlite3ExprCodeTarget(pParse, pE2, r1);
drhc5499be2008-04-01 15:06:33 +00001373 assert( pParse->disableColCache>0 );
drhe55cbd72008-03-31 23:48:03 +00001374 pParse->disableColCache--;
danielk197741a05b72008-10-02 13:50:55 +00001375
1376 if( isRowid ){
danielk197741a05b72008-10-02 13:50:55 +00001377 sqlite3VdbeAddOp2(v, OP_MustBeInt, r3, sqlite3VdbeCurrentAddr(v)+2);
1378 sqlite3VdbeAddOp3(v, OP_Insert, pExpr->iTable, r2, r3);
1379 }else{
1380 sqlite3VdbeAddOp4(v, OP_MakeRecord, r3, 1, r2, &affinity, 1);
1381 sqlite3ExprCacheAffinityChange(pParse, r3, 1);
1382 sqlite3VdbeAddOp2(v, OP_IdxInsert, pExpr->iTable, r2);
1383 }
drhfef52082000-06-06 01:50:43 +00001384 }
drh2d401ab2008-01-10 23:50:11 +00001385 sqlite3ReleaseTempReg(pParse, r1);
1386 sqlite3ReleaseTempReg(pParse, r2);
drhfef52082000-06-06 01:50:43 +00001387 }
danielk197741a05b72008-10-02 13:50:55 +00001388 if( !isRowid ){
1389 sqlite3VdbeChangeP4(v, addr, (void *)&keyInfo, P4_KEYINFO);
1390 }
danielk1977b3bce662005-01-29 08:32:43 +00001391 break;
drhfef52082000-06-06 01:50:43 +00001392 }
1393
drh51522cd2005-01-20 13:36:19 +00001394 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001395 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +00001396 /* This has to be a scalar SELECT. Generate code to put the
1397 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +00001398 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +00001399 */
drh2646da72005-12-09 20:02:05 +00001400 static const Token one = { (u8*)"1", 0, 1 };
drh51522cd2005-01-20 13:36:19 +00001401 Select *pSel;
danielk19776c8c8ce2008-01-02 16:27:09 +00001402 SelectDest dest;
drh1398ad32005-01-19 23:24:50 +00001403
drh51522cd2005-01-20 13:36:19 +00001404 pSel = pExpr->pSelect;
drh1013c932008-01-06 00:25:21 +00001405 sqlite3SelectDestInit(&dest, 0, ++pParse->nMem);
drh51522cd2005-01-20 13:36:19 +00001406 if( pExpr->op==TK_SELECT ){
danielk19776c8c8ce2008-01-02 16:27:09 +00001407 dest.eDest = SRT_Mem;
drh4c583122008-01-04 22:01:03 +00001408 sqlite3VdbeAddOp2(v, OP_Null, 0, dest.iParm);
drhd4e70eb2008-01-02 00:34:36 +00001409 VdbeComment((v, "Init subquery result"));
drh51522cd2005-01-20 13:36:19 +00001410 }else{
danielk19776c8c8ce2008-01-02 16:27:09 +00001411 dest.eDest = SRT_Exists;
drh4c583122008-01-04 22:01:03 +00001412 sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iParm);
drhd4e70eb2008-01-02 00:34:36 +00001413 VdbeComment((v, "Init EXISTS result"));
drh51522cd2005-01-20 13:36:19 +00001414 }
drh633e6d52008-07-28 19:34:53 +00001415 sqlite3ExprDelete(pParse->db, pSel->pLimit);
danielk1977a1644fd2007-08-29 12:31:25 +00001416 pSel->pLimit = sqlite3PExpr(pParse, TK_INTEGER, 0, 0, &one);
drh7d10d5a2008-08-20 16:35:10 +00001417 if( sqlite3Select(pParse, pSel, &dest) ){
drh94ccde52007-04-13 16:06:32 +00001418 return;
1419 }
danielk19776c8c8ce2008-01-02 16:27:09 +00001420 pExpr->iColumn = dest.iParm;
danielk1977b3bce662005-01-29 08:32:43 +00001421 break;
drhcce7d172000-05-31 15:34:51 +00001422 }
1423 }
danielk1977b3bce662005-01-29 08:32:43 +00001424
drh57dbd7b2005-07-08 18:25:26 +00001425 if( testAddr ){
drh892d3172008-01-10 03:46:36 +00001426 sqlite3VdbeJumpHere(v, testAddr-1);
danielk1977b3bce662005-01-29 08:32:43 +00001427 }
danielk1977fc976062007-05-10 10:46:56 +00001428
danielk1977b3bce662005-01-29 08:32:43 +00001429 return;
drhcce7d172000-05-31 15:34:51 +00001430}
drh51522cd2005-01-20 13:36:19 +00001431#endif /* SQLITE_OMIT_SUBQUERY */
drhcce7d172000-05-31 15:34:51 +00001432
drhcce7d172000-05-31 15:34:51 +00001433/*
drh598f1342007-10-23 15:39:45 +00001434** Duplicate an 8-byte value
1435*/
1436static char *dup8bytes(Vdbe *v, const char *in){
1437 char *out = sqlite3DbMallocRaw(sqlite3VdbeDb(v), 8);
1438 if( out ){
1439 memcpy(out, in, 8);
1440 }
1441 return out;
1442}
1443
1444/*
1445** Generate an instruction that will put the floating point
drh9cbf3422008-01-17 16:22:13 +00001446** value described by z[0..n-1] into register iMem.
drh0cf19ed2007-10-23 18:55:48 +00001447**
1448** The z[] string will probably not be zero-terminated. But the
1449** z[n] character is guaranteed to be something that does not look
1450** like the continuation of the number.
drh598f1342007-10-23 15:39:45 +00001451*/
drh9de221d2008-01-05 06:51:30 +00001452static void codeReal(Vdbe *v, const char *z, int n, int negateFlag, int iMem){
drh598f1342007-10-23 15:39:45 +00001453 assert( z || v==0 || sqlite3VdbeDb(v)->mallocFailed );
danielk197778ca0e72009-01-20 16:53:39 +00001454 assert( !z || !sqlite3Isdigit(z[n]) );
danielk1977f3d3c272008-11-19 16:52:44 +00001455 UNUSED_PARAMETER(n);
drh598f1342007-10-23 15:39:45 +00001456 if( z ){
1457 double value;
1458 char *zV;
1459 sqlite3AtoF(z, &value);
drh2eaf93d2008-04-29 00:15:20 +00001460 if( sqlite3IsNaN(value) ){
1461 sqlite3VdbeAddOp2(v, OP_Null, 0, iMem);
1462 }else{
1463 if( negateFlag ) value = -value;
1464 zV = dup8bytes(v, (char*)&value);
1465 sqlite3VdbeAddOp4(v, OP_Real, 0, iMem, 0, zV, P4_REAL);
1466 }
drh598f1342007-10-23 15:39:45 +00001467 }
1468}
1469
1470
1471/*
drhfec19aa2004-05-19 20:41:03 +00001472** Generate an instruction that will put the integer describe by
drh9cbf3422008-01-17 16:22:13 +00001473** text z[0..n-1] into register iMem.
drh0cf19ed2007-10-23 18:55:48 +00001474**
1475** The z[] string will probably not be zero-terminated. But the
1476** z[n] character is guaranteed to be something that does not look
1477** like the continuation of the number.
drhfec19aa2004-05-19 20:41:03 +00001478*/
drh92b01d52008-06-24 00:32:35 +00001479static void codeInteger(Vdbe *v, Expr *pExpr, int negFlag, int iMem){
1480 const char *z;
1481 if( pExpr->flags & EP_IntValue ){
1482 int i = pExpr->iTable;
1483 if( negFlag ) i = -i;
1484 sqlite3VdbeAddOp2(v, OP_Integer, i, iMem);
1485 }else if( (z = (char*)pExpr->token.z)!=0 ){
danielk1977c9cf9012007-05-30 10:36:47 +00001486 int i;
drh92b01d52008-06-24 00:32:35 +00001487 int n = pExpr->token.n;
danielk197778ca0e72009-01-20 16:53:39 +00001488 assert( !sqlite3Isdigit(z[n]) );
danielk1977c9cf9012007-05-30 10:36:47 +00001489 if( sqlite3GetInt32(z, &i) ){
drh9de221d2008-01-05 06:51:30 +00001490 if( negFlag ) i = -i;
1491 sqlite3VdbeAddOp2(v, OP_Integer, i, iMem);
1492 }else if( sqlite3FitsIn64Bits(z, negFlag) ){
drh598f1342007-10-23 15:39:45 +00001493 i64 value;
1494 char *zV;
1495 sqlite3Atoi64(z, &value);
drh9de221d2008-01-05 06:51:30 +00001496 if( negFlag ) value = -value;
drh598f1342007-10-23 15:39:45 +00001497 zV = dup8bytes(v, (char*)&value);
drh9de221d2008-01-05 06:51:30 +00001498 sqlite3VdbeAddOp4(v, OP_Int64, 0, iMem, 0, zV, P4_INT64);
danielk1977c9cf9012007-05-30 10:36:47 +00001499 }else{
drh9de221d2008-01-05 06:51:30 +00001500 codeReal(v, z, n, negFlag, iMem);
danielk1977c9cf9012007-05-30 10:36:47 +00001501 }
drhfec19aa2004-05-19 20:41:03 +00001502 }
1503}
1504
drh945498f2007-02-24 11:52:52 +00001505
1506/*
1507** Generate code that will extract the iColumn-th column from
drhe55cbd72008-03-31 23:48:03 +00001508** table pTab and store the column value in a register. An effort
1509** is made to store the column value in register iReg, but this is
1510** not guaranteed. The location of the column value is returned.
1511**
1512** There must be an open cursor to pTab in iTable when this routine
1513** is called. If iColumn<0 then code is generated that extracts the rowid.
drhda250ea2008-04-01 05:07:14 +00001514**
1515** This routine might attempt to reuse the value of the column that
1516** has already been loaded into a register. The value will always
1517** be used if it has not undergone any affinity changes. But if
1518** an affinity change has occurred, then the cached value will only be
1519** used if allowAffChng is true.
drh945498f2007-02-24 11:52:52 +00001520*/
drhe55cbd72008-03-31 23:48:03 +00001521int sqlite3ExprCodeGetColumn(
1522 Parse *pParse, /* Parsing and code generating context */
drh2133d822008-01-03 18:44:59 +00001523 Table *pTab, /* Description of the table we are reading from */
1524 int iColumn, /* Index of the table column */
1525 int iTable, /* The cursor pointing to the table */
drhda250ea2008-04-01 05:07:14 +00001526 int iReg, /* Store results here */
1527 int allowAffChng /* True if prior affinity changes are OK */
drh2133d822008-01-03 18:44:59 +00001528){
drhe55cbd72008-03-31 23:48:03 +00001529 Vdbe *v = pParse->pVdbe;
1530 int i;
drhda250ea2008-04-01 05:07:14 +00001531 struct yColCache *p;
drhe55cbd72008-03-31 23:48:03 +00001532
drhda250ea2008-04-01 05:07:14 +00001533 for(i=0, p=pParse->aColCache; i<pParse->nColCache; i++, p++){
1534 if( p->iTable==iTable && p->iColumn==iColumn
1535 && (!p->affChange || allowAffChng) ){
drhe55cbd72008-03-31 23:48:03 +00001536#if 0
1537 sqlite3VdbeAddOp0(v, OP_Noop);
drhda250ea2008-04-01 05:07:14 +00001538 VdbeComment((v, "OPT: tab%d.col%d -> r%d", iTable, iColumn, p->iReg));
drhe55cbd72008-03-31 23:48:03 +00001539#endif
drhda250ea2008-04-01 05:07:14 +00001540 return p->iReg;
drhe55cbd72008-03-31 23:48:03 +00001541 }
1542 }
1543 assert( v!=0 );
drh945498f2007-02-24 11:52:52 +00001544 if( iColumn<0 ){
1545 int op = (pTab && IsVirtual(pTab)) ? OP_VRowid : OP_Rowid;
drh2133d822008-01-03 18:44:59 +00001546 sqlite3VdbeAddOp2(v, op, iTable, iReg);
drh945498f2007-02-24 11:52:52 +00001547 }else if( pTab==0 ){
drh2133d822008-01-03 18:44:59 +00001548 sqlite3VdbeAddOp3(v, OP_Column, iTable, iColumn, iReg);
drh945498f2007-02-24 11:52:52 +00001549 }else{
1550 int op = IsVirtual(pTab) ? OP_VColumn : OP_Column;
drh2133d822008-01-03 18:44:59 +00001551 sqlite3VdbeAddOp3(v, op, iTable, iColumn, iReg);
drh945498f2007-02-24 11:52:52 +00001552 sqlite3ColumnDefault(v, pTab, iColumn);
1553#ifndef SQLITE_OMIT_FLOATING_POINT
1554 if( pTab->aCol[iColumn].affinity==SQLITE_AFF_REAL ){
drh2133d822008-01-03 18:44:59 +00001555 sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg);
drh945498f2007-02-24 11:52:52 +00001556 }
1557#endif
1558 }
drhe55cbd72008-03-31 23:48:03 +00001559 if( pParse->disableColCache==0 ){
1560 i = pParse->iColCache;
drhda250ea2008-04-01 05:07:14 +00001561 p = &pParse->aColCache[i];
1562 p->iTable = iTable;
1563 p->iColumn = iColumn;
1564 p->iReg = iReg;
drhc5499be2008-04-01 15:06:33 +00001565 p->affChange = 0;
drhe55cbd72008-03-31 23:48:03 +00001566 i++;
drh2f7794c2008-04-01 03:27:39 +00001567 if( i>=ArraySize(pParse->aColCache) ) i = 0;
drhe55cbd72008-03-31 23:48:03 +00001568 if( i>pParse->nColCache ) pParse->nColCache = i;
drh2f7794c2008-04-01 03:27:39 +00001569 pParse->iColCache = i;
drhe55cbd72008-03-31 23:48:03 +00001570 }
1571 return iReg;
1572}
1573
1574/*
drhe55cbd72008-03-31 23:48:03 +00001575** Clear all column cache entries associated with the vdbe
1576** cursor with cursor number iTable.
1577*/
1578void sqlite3ExprClearColumnCache(Parse *pParse, int iTable){
1579 if( iTable<0 ){
1580 pParse->nColCache = 0;
1581 pParse->iColCache = 0;
1582 }else{
1583 int i;
1584 for(i=0; i<pParse->nColCache; i++){
1585 if( pParse->aColCache[i].iTable==iTable ){
drhc5499be2008-04-01 15:06:33 +00001586 testcase( i==pParse->nColCache-1 );
drhe55cbd72008-03-31 23:48:03 +00001587 pParse->aColCache[i] = pParse->aColCache[--pParse->nColCache];
drhda250ea2008-04-01 05:07:14 +00001588 pParse->iColCache = pParse->nColCache;
drhe55cbd72008-03-31 23:48:03 +00001589 }
1590 }
drhe55cbd72008-03-31 23:48:03 +00001591 }
1592}
1593
1594/*
drhda250ea2008-04-01 05:07:14 +00001595** Record the fact that an affinity change has occurred on iCount
1596** registers starting with iStart.
drhe55cbd72008-03-31 23:48:03 +00001597*/
drhda250ea2008-04-01 05:07:14 +00001598void sqlite3ExprCacheAffinityChange(Parse *pParse, int iStart, int iCount){
1599 int iEnd = iStart + iCount - 1;
drhe55cbd72008-03-31 23:48:03 +00001600 int i;
1601 for(i=0; i<pParse->nColCache; i++){
1602 int r = pParse->aColCache[i].iReg;
drhda250ea2008-04-01 05:07:14 +00001603 if( r>=iStart && r<=iEnd ){
1604 pParse->aColCache[i].affChange = 1;
drhe55cbd72008-03-31 23:48:03 +00001605 }
1606 }
drhe55cbd72008-03-31 23:48:03 +00001607}
1608
1609/*
drhb21e7c72008-06-22 12:37:57 +00001610** Generate code to move content from registers iFrom...iFrom+nReg-1
1611** over to iTo..iTo+nReg-1. Keep the column cache up-to-date.
drhe55cbd72008-03-31 23:48:03 +00001612*/
drhb21e7c72008-06-22 12:37:57 +00001613void sqlite3ExprCodeMove(Parse *pParse, int iFrom, int iTo, int nReg){
drhe55cbd72008-03-31 23:48:03 +00001614 int i;
1615 if( iFrom==iTo ) return;
drhb21e7c72008-06-22 12:37:57 +00001616 sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move, iFrom, iTo, nReg);
drhe55cbd72008-03-31 23:48:03 +00001617 for(i=0; i<pParse->nColCache; i++){
drhb21e7c72008-06-22 12:37:57 +00001618 int x = pParse->aColCache[i].iReg;
1619 if( x>=iFrom && x<iFrom+nReg ){
1620 pParse->aColCache[i].iReg += iTo-iFrom;
drhe55cbd72008-03-31 23:48:03 +00001621 }
1622 }
drh945498f2007-02-24 11:52:52 +00001623}
1624
drhfec19aa2004-05-19 20:41:03 +00001625/*
drh92b01d52008-06-24 00:32:35 +00001626** Generate code to copy content from registers iFrom...iFrom+nReg-1
1627** over to iTo..iTo+nReg-1.
1628*/
1629void sqlite3ExprCodeCopy(Parse *pParse, int iFrom, int iTo, int nReg){
1630 int i;
1631 if( iFrom==iTo ) return;
1632 for(i=0; i<nReg; i++){
1633 sqlite3VdbeAddOp2(pParse->pVdbe, OP_Copy, iFrom+i, iTo+i);
1634 }
1635}
1636
1637/*
drh652fbf52008-04-01 01:42:41 +00001638** Return true if any register in the range iFrom..iTo (inclusive)
1639** is used as part of the column cache.
1640*/
1641static int usedAsColumnCache(Parse *pParse, int iFrom, int iTo){
1642 int i;
1643 for(i=0; i<pParse->nColCache; i++){
1644 int r = pParse->aColCache[i].iReg;
1645 if( r>=iFrom && r<=iTo ) return 1;
1646 }
1647 return 0;
1648}
1649
1650/*
drhd1fa7bc2009-01-10 13:24:50 +00001651** There is a value in register iReg.
drh652fbf52008-04-01 01:42:41 +00001652**
1653** We are going to modify the value, so we need to make sure it
drhd1fa7bc2009-01-10 13:24:50 +00001654** is not a cached register. If iReg is a cached register,
1655** then clear the corresponding cache line.
drh652fbf52008-04-01 01:42:41 +00001656*/
drhd1fa7bc2009-01-10 13:24:50 +00001657void sqlite3ExprWritableRegister(Parse *pParse, int iReg){
drhda250ea2008-04-01 05:07:14 +00001658 int i;
drhd1fa7bc2009-01-10 13:24:50 +00001659 if( usedAsColumnCache(pParse, iReg, iReg) ){
1660 for(i=0; i<pParse->nColCache; i++){
1661 if( pParse->aColCache[i].iReg==iReg ){
1662 pParse->aColCache[i] = pParse->aColCache[--pParse->nColCache];
1663 pParse->iColCache = pParse->nColCache;
1664 }
drhda250ea2008-04-01 05:07:14 +00001665 }
1666 }
drh652fbf52008-04-01 01:42:41 +00001667}
1668
1669/*
drh191b54c2008-04-15 12:14:21 +00001670** If the last instruction coded is an ephemeral copy of any of
1671** the registers in the nReg registers beginning with iReg, then
1672** convert the last instruction from OP_SCopy to OP_Copy.
1673*/
1674void sqlite3ExprHardCopy(Parse *pParse, int iReg, int nReg){
1675 int addr;
1676 VdbeOp *pOp;
1677 Vdbe *v;
1678
1679 v = pParse->pVdbe;
1680 addr = sqlite3VdbeCurrentAddr(v);
1681 pOp = sqlite3VdbeGetOp(v, addr-1);
danielk1977d7eb2ed2008-04-24 12:36:35 +00001682 assert( pOp || pParse->db->mallocFailed );
1683 if( pOp && pOp->opcode==OP_SCopy && pOp->p1>=iReg && pOp->p1<iReg+nReg ){
drh191b54c2008-04-15 12:14:21 +00001684 pOp->opcode = OP_Copy;
1685 }
1686}
1687
1688/*
drh8b213892008-08-29 02:14:02 +00001689** Generate code to store the value of the iAlias-th alias in register
1690** target. The first time this is called, pExpr is evaluated to compute
1691** the value of the alias. The value is stored in an auxiliary register
1692** and the number of that register is returned. On subsequent calls,
1693** the register number is returned without generating any code.
1694**
1695** Note that in order for this to work, code must be generated in the
1696** same order that it is executed.
1697**
1698** Aliases are numbered starting with 1. So iAlias is in the range
1699** of 1 to pParse->nAlias inclusive.
1700**
1701** pParse->aAlias[iAlias-1] records the register number where the value
1702** of the iAlias-th alias is stored. If zero, that means that the
1703** alias has not yet been computed.
1704*/
drh31daa632008-10-25 15:03:20 +00001705static int codeAlias(Parse *pParse, int iAlias, Expr *pExpr, int target){
drh8b213892008-08-29 02:14:02 +00001706 sqlite3 *db = pParse->db;
1707 int iReg;
drh555f8de2008-12-08 13:42:36 +00001708 if( pParse->nAliasAlloc<pParse->nAlias ){
1709 pParse->aAlias = sqlite3DbReallocOrFree(db, pParse->aAlias,
drh8b213892008-08-29 02:14:02 +00001710 sizeof(pParse->aAlias[0])*pParse->nAlias );
drh555f8de2008-12-08 13:42:36 +00001711 testcase( db->mallocFailed && pParse->nAliasAlloc>0 );
drh8b213892008-08-29 02:14:02 +00001712 if( db->mallocFailed ) return 0;
drh555f8de2008-12-08 13:42:36 +00001713 memset(&pParse->aAlias[pParse->nAliasAlloc], 0,
1714 (pParse->nAlias-pParse->nAliasAlloc)*sizeof(pParse->aAlias[0]));
1715 pParse->nAliasAlloc = pParse->nAlias;
drh8b213892008-08-29 02:14:02 +00001716 }
1717 assert( iAlias>0 && iAlias<=pParse->nAlias );
1718 iReg = pParse->aAlias[iAlias-1];
1719 if( iReg==0 ){
drh31daa632008-10-25 15:03:20 +00001720 if( pParse->disableColCache ){
1721 iReg = sqlite3ExprCodeTarget(pParse, pExpr, target);
1722 }else{
1723 iReg = ++pParse->nMem;
1724 sqlite3ExprCode(pParse, pExpr, iReg);
1725 pParse->aAlias[iAlias-1] = iReg;
1726 }
drh8b213892008-08-29 02:14:02 +00001727 }
1728 return iReg;
1729}
1730
1731/*
drhcce7d172000-05-31 15:34:51 +00001732** Generate code into the current Vdbe to evaluate the given
drh2dcef112008-01-12 19:03:48 +00001733** expression. Attempt to store the results in register "target".
1734** Return the register where results are stored.
drh389a1ad2008-01-03 23:44:53 +00001735**
drh8b213892008-08-29 02:14:02 +00001736** With this routine, there is no guarantee that results will
drh2dcef112008-01-12 19:03:48 +00001737** be stored in target. The result might be stored in some other
1738** register if it is convenient to do so. The calling function
1739** must check the return code and move the results to the desired
1740** register.
drhcce7d172000-05-31 15:34:51 +00001741*/
drh678ccce2008-03-31 18:19:54 +00001742int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){
drh2dcef112008-01-12 19:03:48 +00001743 Vdbe *v = pParse->pVdbe; /* The VM under construction */
1744 int op; /* The opcode being coded */
1745 int inReg = target; /* Results stored in register inReg */
1746 int regFree1 = 0; /* If non-zero free this temporary register */
1747 int regFree2 = 0; /* If non-zero free this temporary register */
drh678ccce2008-03-31 18:19:54 +00001748 int r1, r2, r3, r4; /* Various register numbers */
drh8b213892008-08-29 02:14:02 +00001749 sqlite3 *db;
drhffe07b22005-11-03 00:41:17 +00001750
drh8b213892008-08-29 02:14:02 +00001751 db = pParse->db;
1752 assert( v!=0 || db->mallocFailed );
drh9cbf3422008-01-17 16:22:13 +00001753 assert( target>0 && target<=pParse->nMem );
drh389a1ad2008-01-03 23:44:53 +00001754 if( v==0 ) return 0;
drh389a1ad2008-01-03 23:44:53 +00001755
1756 if( pExpr==0 ){
1757 op = TK_NULL;
1758 }else{
1759 op = pExpr->op;
1760 }
drhf2bc0132004-10-04 13:19:23 +00001761 switch( op ){
drh13449892005-09-07 21:22:45 +00001762 case TK_AGG_COLUMN: {
1763 AggInfo *pAggInfo = pExpr->pAggInfo;
1764 struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg];
1765 if( !pAggInfo->directMode ){
drh9de221d2008-01-05 06:51:30 +00001766 assert( pCol->iMem>0 );
1767 inReg = pCol->iMem;
drh13449892005-09-07 21:22:45 +00001768 break;
1769 }else if( pAggInfo->useSortingIdx ){
drh389a1ad2008-01-03 23:44:53 +00001770 sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdx,
1771 pCol->iSorterColumn, target);
drh13449892005-09-07 21:22:45 +00001772 break;
1773 }
1774 /* Otherwise, fall thru into the TK_COLUMN case */
1775 }
drh967e8b72000-06-21 13:59:10 +00001776 case TK_COLUMN: {
drhffe07b22005-11-03 00:41:17 +00001777 if( pExpr->iTable<0 ){
1778 /* This only happens when coding check constraints */
drhaa9b8962008-01-08 02:57:55 +00001779 assert( pParse->ckBase>0 );
1780 inReg = pExpr->iColumn + pParse->ckBase;
drhc4a3c772001-04-04 11:48:57 +00001781 }else{
drhc5499be2008-04-01 15:06:33 +00001782 testcase( (pExpr->flags & EP_AnyAff)!=0 );
drhe55cbd72008-03-31 23:48:03 +00001783 inReg = sqlite3ExprCodeGetColumn(pParse, pExpr->pTab,
drhda250ea2008-04-01 05:07:14 +00001784 pExpr->iColumn, pExpr->iTable, target,
1785 pExpr->flags & EP_AnyAff);
drh22827922000-06-06 17:27:05 +00001786 }
drhcce7d172000-05-31 15:34:51 +00001787 break;
1788 }
1789 case TK_INTEGER: {
drh92b01d52008-06-24 00:32:35 +00001790 codeInteger(v, pExpr, 0, target);
drhfec19aa2004-05-19 20:41:03 +00001791 break;
1792 }
drh598f1342007-10-23 15:39:45 +00001793 case TK_FLOAT: {
drh9de221d2008-01-05 06:51:30 +00001794 codeReal(v, (char*)pExpr->token.z, pExpr->token.n, 0, target);
drh598f1342007-10-23 15:39:45 +00001795 break;
1796 }
drhfec19aa2004-05-19 20:41:03 +00001797 case TK_STRING: {
drh8b213892008-08-29 02:14:02 +00001798 sqlite3DequoteExpr(db, pExpr);
drh9de221d2008-01-05 06:51:30 +00001799 sqlite3VdbeAddOp4(v,OP_String8, 0, target, 0,
drh66a51672008-01-03 00:01:23 +00001800 (char*)pExpr->token.z, pExpr->token.n);
drhcce7d172000-05-31 15:34:51 +00001801 break;
1802 }
drhf0863fe2005-06-12 21:35:51 +00001803 case TK_NULL: {
drh9de221d2008-01-05 06:51:30 +00001804 sqlite3VdbeAddOp2(v, OP_Null, 0, target);
drhf0863fe2005-06-12 21:35:51 +00001805 break;
1806 }
danielk19775338a5f2005-01-20 13:03:10 +00001807#ifndef SQLITE_OMIT_BLOB_LITERAL
danielk1977c572ef72004-05-27 09:28:41 +00001808 case TK_BLOB: {
drh6c8c6ce2005-08-23 11:17:58 +00001809 int n;
1810 const char *z;
drhca48c902008-01-18 14:08:24 +00001811 char *zBlob;
1812 assert( pExpr->token.n>=3 );
1813 assert( pExpr->token.z[0]=='x' || pExpr->token.z[0]=='X' );
1814 assert( pExpr->token.z[1]=='\'' );
1815 assert( pExpr->token.z[pExpr->token.n-1]=='\'' );
drh6c8c6ce2005-08-23 11:17:58 +00001816 n = pExpr->token.n - 3;
drh2646da72005-12-09 20:02:05 +00001817 z = (char*)pExpr->token.z + 2;
drhca48c902008-01-18 14:08:24 +00001818 zBlob = sqlite3HexToBlob(sqlite3VdbeDb(v), z, n);
1819 sqlite3VdbeAddOp4(v, OP_Blob, n/2, target, 0, zBlob, P4_DYNAMIC);
danielk1977c572ef72004-05-27 09:28:41 +00001820 break;
1821 }
danielk19775338a5f2005-01-20 13:03:10 +00001822#endif
drh50457892003-09-06 01:10:47 +00001823 case TK_VARIABLE: {
drh9de221d2008-01-05 06:51:30 +00001824 sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iTable, target);
drh895d7472004-08-20 16:02:39 +00001825 if( pExpr->token.n>1 ){
drh66a51672008-01-03 00:01:23 +00001826 sqlite3VdbeChangeP4(v, -1, (char*)pExpr->token.z, pExpr->token.n);
drh895d7472004-08-20 16:02:39 +00001827 }
drh50457892003-09-06 01:10:47 +00001828 break;
1829 }
drh4e0cff62004-11-05 05:10:28 +00001830 case TK_REGISTER: {
drh9de221d2008-01-05 06:51:30 +00001831 inReg = pExpr->iTable;
drh4e0cff62004-11-05 05:10:28 +00001832 break;
1833 }
drh8b213892008-08-29 02:14:02 +00001834 case TK_AS: {
drh31daa632008-10-25 15:03:20 +00001835 inReg = codeAlias(pParse, pExpr->iTable, pExpr->pLeft, target);
drh8b213892008-08-29 02:14:02 +00001836 break;
1837 }
drh487e2622005-06-25 18:42:14 +00001838#ifndef SQLITE_OMIT_CAST
1839 case TK_CAST: {
1840 /* Expressions of the form: CAST(pLeft AS token) */
danielk1977f0113002006-01-24 12:09:17 +00001841 int aff, to_op;
drh2dcef112008-01-12 19:03:48 +00001842 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
drh8a512562005-11-14 22:29:05 +00001843 aff = sqlite3AffinityType(&pExpr->token);
danielk1977f0113002006-01-24 12:09:17 +00001844 to_op = aff - SQLITE_AFF_TEXT + OP_ToText;
1845 assert( to_op==OP_ToText || aff!=SQLITE_AFF_TEXT );
1846 assert( to_op==OP_ToBlob || aff!=SQLITE_AFF_NONE );
1847 assert( to_op==OP_ToNumeric || aff!=SQLITE_AFF_NUMERIC );
1848 assert( to_op==OP_ToInt || aff!=SQLITE_AFF_INTEGER );
1849 assert( to_op==OP_ToReal || aff!=SQLITE_AFF_REAL );
drhc5499be2008-04-01 15:06:33 +00001850 testcase( to_op==OP_ToText );
1851 testcase( to_op==OP_ToBlob );
1852 testcase( to_op==OP_ToNumeric );
1853 testcase( to_op==OP_ToInt );
1854 testcase( to_op==OP_ToReal );
drh1735fa82008-11-06 15:33:03 +00001855 if( inReg!=target ){
1856 sqlite3VdbeAddOp2(v, OP_SCopy, inReg, target);
1857 inReg = target;
1858 }
drh2dcef112008-01-12 19:03:48 +00001859 sqlite3VdbeAddOp1(v, to_op, inReg);
drhc5499be2008-04-01 15:06:33 +00001860 testcase( usedAsColumnCache(pParse, inReg, inReg) );
drhb3843a82008-04-01 12:24:11 +00001861 sqlite3ExprCacheAffinityChange(pParse, inReg, 1);
drh487e2622005-06-25 18:42:14 +00001862 break;
1863 }
1864#endif /* SQLITE_OMIT_CAST */
drhc9b84a12002-06-20 11:36:48 +00001865 case TK_LT:
1866 case TK_LE:
1867 case TK_GT:
1868 case TK_GE:
1869 case TK_NE:
1870 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00001871 assert( TK_LT==OP_Lt );
1872 assert( TK_LE==OP_Le );
1873 assert( TK_GT==OP_Gt );
1874 assert( TK_GE==OP_Ge );
1875 assert( TK_EQ==OP_Eq );
1876 assert( TK_NE==OP_Ne );
drhc5499be2008-04-01 15:06:33 +00001877 testcase( op==TK_LT );
1878 testcase( op==TK_LE );
1879 testcase( op==TK_GT );
1880 testcase( op==TK_GE );
1881 testcase( op==TK_EQ );
1882 testcase( op==TK_NE );
drhda250ea2008-04-01 05:07:14 +00001883 codeCompareOperands(pParse, pExpr->pLeft, &r1, &regFree1,
1884 pExpr->pRight, &r2, &regFree2);
drh35573352008-01-08 23:54:25 +00001885 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
1886 r1, r2, inReg, SQLITE_STOREP2);
drhc5499be2008-04-01 15:06:33 +00001887 testcase( regFree1==0 );
1888 testcase( regFree2==0 );
danielk1977a37cdde2004-05-16 11:15:36 +00001889 break;
drhc9b84a12002-06-20 11:36:48 +00001890 }
drhcce7d172000-05-31 15:34:51 +00001891 case TK_AND:
1892 case TK_OR:
1893 case TK_PLUS:
1894 case TK_STAR:
1895 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00001896 case TK_REM:
1897 case TK_BITAND:
1898 case TK_BITOR:
drh17c40292004-07-21 02:53:29 +00001899 case TK_SLASH:
drhbf4133c2001-10-13 02:59:08 +00001900 case TK_LSHIFT:
drh855eb1c2004-08-31 13:45:11 +00001901 case TK_RSHIFT:
drh00400772000-06-16 20:51:26 +00001902 case TK_CONCAT: {
drhf2bc0132004-10-04 13:19:23 +00001903 assert( TK_AND==OP_And );
1904 assert( TK_OR==OP_Or );
1905 assert( TK_PLUS==OP_Add );
1906 assert( TK_MINUS==OP_Subtract );
1907 assert( TK_REM==OP_Remainder );
1908 assert( TK_BITAND==OP_BitAnd );
1909 assert( TK_BITOR==OP_BitOr );
1910 assert( TK_SLASH==OP_Divide );
1911 assert( TK_LSHIFT==OP_ShiftLeft );
1912 assert( TK_RSHIFT==OP_ShiftRight );
1913 assert( TK_CONCAT==OP_Concat );
drhc5499be2008-04-01 15:06:33 +00001914 testcase( op==TK_AND );
1915 testcase( op==TK_OR );
1916 testcase( op==TK_PLUS );
1917 testcase( op==TK_MINUS );
1918 testcase( op==TK_REM );
1919 testcase( op==TK_BITAND );
1920 testcase( op==TK_BITOR );
1921 testcase( op==TK_SLASH );
1922 testcase( op==TK_LSHIFT );
1923 testcase( op==TK_RSHIFT );
1924 testcase( op==TK_CONCAT );
drh2dcef112008-01-12 19:03:48 +00001925 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
1926 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
drh5b6afba2008-01-05 16:29:28 +00001927 sqlite3VdbeAddOp3(v, op, r2, r1, target);
drhc5499be2008-04-01 15:06:33 +00001928 testcase( regFree1==0 );
1929 testcase( regFree2==0 );
drh00400772000-06-16 20:51:26 +00001930 break;
1931 }
drhcce7d172000-05-31 15:34:51 +00001932 case TK_UMINUS: {
drhfec19aa2004-05-19 20:41:03 +00001933 Expr *pLeft = pExpr->pLeft;
1934 assert( pLeft );
shanefbd60f82009-02-04 03:59:25 +00001935 if( pLeft->op==TK_FLOAT ){
1936 codeReal(v, (char*)pLeft->token.z, pLeft->token.n, 1, target);
1937 }else if( pLeft->op==TK_INTEGER ){
1938 codeInteger(v, pLeft, 1, target);
drh3c84ddf2008-01-09 02:15:38 +00001939 }else{
drh2dcef112008-01-12 19:03:48 +00001940 regFree1 = r1 = sqlite3GetTempReg(pParse);
drh3c84ddf2008-01-09 02:15:38 +00001941 sqlite3VdbeAddOp2(v, OP_Integer, 0, r1);
drhe55cbd72008-03-31 23:48:03 +00001942 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree2);
drh2dcef112008-01-12 19:03:48 +00001943 sqlite3VdbeAddOp3(v, OP_Subtract, r2, r1, target);
drhc5499be2008-04-01 15:06:33 +00001944 testcase( regFree2==0 );
drh6e142f52000-06-08 13:36:40 +00001945 }
drh3c84ddf2008-01-09 02:15:38 +00001946 inReg = target;
1947 break;
drh6e142f52000-06-08 13:36:40 +00001948 }
drhbf4133c2001-10-13 02:59:08 +00001949 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00001950 case TK_NOT: {
drhf2bc0132004-10-04 13:19:23 +00001951 assert( TK_BITNOT==OP_BitNot );
1952 assert( TK_NOT==OP_Not );
drhc5499be2008-04-01 15:06:33 +00001953 testcase( op==TK_BITNOT );
1954 testcase( op==TK_NOT );
drhe99fa2a2008-12-15 15:27:51 +00001955 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
1956 testcase( regFree1==0 );
1957 inReg = target;
1958 sqlite3VdbeAddOp2(v, op, r1, inReg);
drhcce7d172000-05-31 15:34:51 +00001959 break;
1960 }
1961 case TK_ISNULL:
1962 case TK_NOTNULL: {
drh6a288a32008-01-07 19:20:24 +00001963 int addr;
drhf2bc0132004-10-04 13:19:23 +00001964 assert( TK_ISNULL==OP_IsNull );
1965 assert( TK_NOTNULL==OP_NotNull );
drhc5499be2008-04-01 15:06:33 +00001966 testcase( op==TK_ISNULL );
1967 testcase( op==TK_NOTNULL );
drh9de221d2008-01-05 06:51:30 +00001968 sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
drh2dcef112008-01-12 19:03:48 +00001969 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
drhc5499be2008-04-01 15:06:33 +00001970 testcase( regFree1==0 );
drh2dcef112008-01-12 19:03:48 +00001971 addr = sqlite3VdbeAddOp1(v, op, r1);
drh9de221d2008-01-05 06:51:30 +00001972 sqlite3VdbeAddOp2(v, OP_AddImm, target, -1);
drh6a288a32008-01-07 19:20:24 +00001973 sqlite3VdbeJumpHere(v, addr);
drhf2bc0132004-10-04 13:19:23 +00001974 break;
drhcce7d172000-05-31 15:34:51 +00001975 }
drh22827922000-06-06 17:27:05 +00001976 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00001977 AggInfo *pInfo = pExpr->pAggInfo;
drh7e56e712005-11-16 12:53:15 +00001978 if( pInfo==0 ){
1979 sqlite3ErrorMsg(pParse, "misuse of aggregate: %T",
1980 &pExpr->span);
1981 }else{
drh9de221d2008-01-05 06:51:30 +00001982 inReg = pInfo->aFunc[pExpr->iAgg].iMem;
drh7e56e712005-11-16 12:53:15 +00001983 }
drh22827922000-06-06 17:27:05 +00001984 break;
1985 }
drhb71090f2005-05-23 17:26:51 +00001986 case TK_CONST_FUNC:
drhcce7d172000-05-31 15:34:51 +00001987 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00001988 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00001989 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00001990 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00001991 int nId;
1992 const char *zId;
drh13449892005-09-07 21:22:45 +00001993 int constMask = 0;
danielk1977682f68b2004-06-05 10:22:17 +00001994 int i;
drh17435752007-08-16 04:30:38 +00001995 u8 enc = ENC(db);
danielk1977dc1bdc42004-06-11 10:51:27 +00001996 CollSeq *pColl = 0;
drh17435752007-08-16 04:30:38 +00001997
drhc5499be2008-04-01 15:06:33 +00001998 testcase( op==TK_CONST_FUNC );
1999 testcase( op==TK_FUNCTION );
drh2646da72005-12-09 20:02:05 +00002000 zId = (char*)pExpr->token.z;
drhb71090f2005-05-23 17:26:51 +00002001 nId = pExpr->token.n;
drh8b213892008-08-29 02:14:02 +00002002 pDef = sqlite3FindFunction(db, zId, nId, nExpr, enc, 0);
drh0bce8352002-02-28 00:41:10 +00002003 assert( pDef!=0 );
drh892d3172008-01-10 03:46:36 +00002004 if( pList ){
2005 nExpr = pList->nExpr;
drh2dcef112008-01-12 19:03:48 +00002006 r1 = sqlite3GetTempRange(pParse, nExpr);
drh191b54c2008-04-15 12:14:21 +00002007 sqlite3ExprCodeExprList(pParse, pList, r1, 1);
drh892d3172008-01-10 03:46:36 +00002008 }else{
drhd847eaa2008-01-17 17:15:56 +00002009 nExpr = r1 = 0;
drh892d3172008-01-10 03:46:36 +00002010 }
drhb7f6f682006-07-08 17:06:43 +00002011#ifndef SQLITE_OMIT_VIRTUALTABLE
drha43fa222006-07-08 18:41:37 +00002012 /* Possibly overload the function if the first argument is
2013 ** a virtual table column.
2014 **
2015 ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the
2016 ** second argument, not the first, as the argument to test to
2017 ** see if it is a column in a virtual table. This is done because
2018 ** the left operand of infix functions (the operand we want to
2019 ** control overloading) ends up as the second argument to the
2020 ** function. The expression "A glob B" is equivalent to
2021 ** "glob(B,A). We want to use the A in "A glob B" to test
2022 ** for function overloading. But we use the B term in "glob(B,A)".
2023 */
drh6a03a1c2006-07-08 18:34:59 +00002024 if( nExpr>=2 && (pExpr->flags & EP_InfixFunc) ){
drh17435752007-08-16 04:30:38 +00002025 pDef = sqlite3VtabOverloadFunction(db, pDef, nExpr, pList->a[1].pExpr);
drh6a03a1c2006-07-08 18:34:59 +00002026 }else if( nExpr>0 ){
drh17435752007-08-16 04:30:38 +00002027 pDef = sqlite3VtabOverloadFunction(db, pDef, nExpr, pList->a[0].pExpr);
drhb7f6f682006-07-08 17:06:43 +00002028 }
2029#endif
danielk1977682f68b2004-06-05 10:22:17 +00002030 for(i=0; i<nExpr && i<32; i++){
danielk1977d02eb1f2004-06-06 09:44:03 +00002031 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
drh13449892005-09-07 21:22:45 +00002032 constMask |= (1<<i);
danielk1977d02eb1f2004-06-06 09:44:03 +00002033 }
drhe82f5d02008-10-07 19:53:14 +00002034 if( (pDef->flags & SQLITE_FUNC_NEEDCOLL)!=0 && !pColl ){
danielk1977dc1bdc42004-06-11 10:51:27 +00002035 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
2036 }
2037 }
drhe82f5d02008-10-07 19:53:14 +00002038 if( pDef->flags & SQLITE_FUNC_NEEDCOLL ){
drh8b213892008-08-29 02:14:02 +00002039 if( !pColl ) pColl = db->pDfltColl;
drh66a51672008-01-03 00:01:23 +00002040 sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ);
danielk1977682f68b2004-06-05 10:22:17 +00002041 }
drh2dcef112008-01-12 19:03:48 +00002042 sqlite3VdbeAddOp4(v, OP_Function, constMask, r1, target,
drh66a51672008-01-03 00:01:23 +00002043 (char*)pDef, P4_FUNCDEF);
drh1bd10f82008-12-10 21:19:56 +00002044 sqlite3VdbeChangeP5(v, (u8)nExpr);
drh2dcef112008-01-12 19:03:48 +00002045 if( nExpr ){
2046 sqlite3ReleaseTempRange(pParse, r1, nExpr);
2047 }
drhda250ea2008-04-01 05:07:14 +00002048 sqlite3ExprCacheAffinityChange(pParse, r1, nExpr);
drhcce7d172000-05-31 15:34:51 +00002049 break;
2050 }
drhfe2093d2005-01-20 22:48:47 +00002051#ifndef SQLITE_OMIT_SUBQUERY
2052 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00002053 case TK_SELECT: {
drhc5499be2008-04-01 15:06:33 +00002054 testcase( op==TK_EXISTS );
2055 testcase( op==TK_SELECT );
drh41714d62006-03-02 04:44:23 +00002056 if( pExpr->iColumn==0 ){
danielk197741a05b72008-10-02 13:50:55 +00002057 sqlite3CodeSubselect(pParse, pExpr, 0, 0);
drh41714d62006-03-02 04:44:23 +00002058 }
drh9de221d2008-01-05 06:51:30 +00002059 inReg = pExpr->iColumn;
drh19a775c2000-06-05 18:54:46 +00002060 break;
2061 }
drhfef52082000-06-06 01:50:43 +00002062 case TK_IN: {
danielk19770cdc0222008-06-26 18:04:03 +00002063 int rNotFound = 0;
2064 int rMayHaveNull = 0;
drh6fccc352008-06-27 00:52:45 +00002065 int j2, j3, j4, j5;
drh94a11212004-09-25 13:12:14 +00002066 char affinity;
danielk19779a96b662007-11-29 17:05:18 +00002067 int eType;
danielk19779a96b662007-11-29 17:05:18 +00002068
drh3c31fc22008-06-26 21:45:26 +00002069 VdbeNoopComment((v, "begin IN expr r%d", target));
danielk19770cdc0222008-06-26 18:04:03 +00002070 eType = sqlite3FindInIndex(pParse, pExpr, &rMayHaveNull);
2071 if( rMayHaveNull ){
2072 rNotFound = ++pParse->nMem;
2073 }
danielk1977e014a832004-05-17 10:48:57 +00002074
2075 /* Figure out the affinity to use to create a key from the results
2076 ** of the expression. affinityStr stores a static string suitable for
drh66a51672008-01-03 00:01:23 +00002077 ** P4 of OP_MakeRecord.
danielk1977e014a832004-05-17 10:48:57 +00002078 */
drh94a11212004-09-25 13:12:14 +00002079 affinity = comparisonAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +00002080
danielk1977e014a832004-05-17 10:48:57 +00002081
2082 /* Code the <expr> from "<expr> IN (...)". The temporary table
2083 ** pExpr->iTable contains the values that make up the (...) set.
2084 */
drh66ba23c2008-06-27 00:47:28 +00002085 pParse->disableColCache++;
2086 sqlite3ExprCode(pParse, pExpr->pLeft, target);
2087 pParse->disableColCache--;
2088 j2 = sqlite3VdbeAddOp1(v, OP_IsNull, target);
danielk19779a96b662007-11-29 17:05:18 +00002089 if( eType==IN_INDEX_ROWID ){
drh66ba23c2008-06-27 00:47:28 +00002090 j3 = sqlite3VdbeAddOp1(v, OP_MustBeInt, target);
2091 j4 = sqlite3VdbeAddOp3(v, OP_NotExists, pExpr->iTable, 0, target);
2092 sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
drh6a288a32008-01-07 19:20:24 +00002093 j5 = sqlite3VdbeAddOp0(v, OP_Goto);
2094 sqlite3VdbeJumpHere(v, j3);
2095 sqlite3VdbeJumpHere(v, j4);
danielk19770cdc0222008-06-26 18:04:03 +00002096 sqlite3VdbeAddOp2(v, OP_Integer, 0, target);
danielk19779a96b662007-11-29 17:05:18 +00002097 }else{
drh2dcef112008-01-12 19:03:48 +00002098 r2 = regFree2 = sqlite3GetTempReg(pParse);
danielk19770cdc0222008-06-26 18:04:03 +00002099
2100 /* Create a record and test for set membership. If the set contains
2101 ** the value, then jump to the end of the test code. The target
2102 ** register still contains the true (1) value written to it earlier.
2103 */
drh66ba23c2008-06-27 00:47:28 +00002104 sqlite3VdbeAddOp4(v, OP_MakeRecord, target, 1, r2, &affinity, 1);
2105 sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
drh2dcef112008-01-12 19:03:48 +00002106 j5 = sqlite3VdbeAddOp3(v, OP_Found, pExpr->iTable, 0, r2);
danielk19770cdc0222008-06-26 18:04:03 +00002107
2108 /* If the set membership test fails, then the result of the
2109 ** "x IN (...)" expression must be either 0 or NULL. If the set
2110 ** contains no NULL values, then the result is 0. If the set
2111 ** contains one or more NULL values, then the result of the
2112 ** expression is also NULL.
2113 */
2114 if( rNotFound==0 ){
2115 /* This branch runs if it is known at compile time (now) that
2116 ** the set contains no NULL values. This happens as the result
2117 ** of a "NOT NULL" constraint in the database schema. No need
2118 ** to test the data structure at runtime in this case.
2119 */
2120 sqlite3VdbeAddOp2(v, OP_Integer, 0, target);
2121 }else{
2122 /* This block populates the rNotFound register with either NULL
2123 ** or 0 (an integer value). If the data structure contains one
2124 ** or more NULLs, then set rNotFound to NULL. Otherwise, set it
2125 ** to 0. If register rMayHaveNull is already set to some value
2126 ** other than NULL, then the test has already been run and
2127 ** rNotFound is already populated.
2128 */
drh66ba23c2008-06-27 00:47:28 +00002129 static const char nullRecord[] = { 0x02, 0x00 };
danielk19770cdc0222008-06-26 18:04:03 +00002130 j3 = sqlite3VdbeAddOp1(v, OP_NotNull, rMayHaveNull);
2131 sqlite3VdbeAddOp2(v, OP_Null, 0, rNotFound);
drh66ba23c2008-06-27 00:47:28 +00002132 sqlite3VdbeAddOp4(v, OP_Blob, 2, rMayHaveNull, 0,
2133 nullRecord, P4_STATIC);
2134 j4 = sqlite3VdbeAddOp3(v, OP_Found, pExpr->iTable, 0, rMayHaveNull);
danielk19770cdc0222008-06-26 18:04:03 +00002135 sqlite3VdbeAddOp2(v, OP_Integer, 0, rNotFound);
2136 sqlite3VdbeJumpHere(v, j4);
2137 sqlite3VdbeJumpHere(v, j3);
2138
2139 /* Copy the value of register rNotFound (which is either NULL or 0)
danielk197741a05b72008-10-02 13:50:55 +00002140 ** into the target register. This will be the result of the
danielk19770cdc0222008-06-26 18:04:03 +00002141 ** expression.
2142 */
2143 sqlite3VdbeAddOp2(v, OP_Copy, rNotFound, target);
2144 }
danielk19779a96b662007-11-29 17:05:18 +00002145 }
drh6a288a32008-01-07 19:20:24 +00002146 sqlite3VdbeJumpHere(v, j2);
2147 sqlite3VdbeJumpHere(v, j5);
drh3c31fc22008-06-26 21:45:26 +00002148 VdbeComment((v, "end IN expr r%d", target));
drhfef52082000-06-06 01:50:43 +00002149 break;
2150 }
danielk197793758c82005-01-21 08:13:14 +00002151#endif
drh2dcef112008-01-12 19:03:48 +00002152 /*
2153 ** x BETWEEN y AND z
2154 **
2155 ** This is equivalent to
2156 **
2157 ** x>=y AND x<=z
2158 **
2159 ** X is stored in pExpr->pLeft.
2160 ** Y is stored in pExpr->pList->a[0].pExpr.
2161 ** Z is stored in pExpr->pList->a[1].pExpr.
2162 */
drhfef52082000-06-06 01:50:43 +00002163 case TK_BETWEEN: {
drhbe5c89a2004-07-26 00:31:09 +00002164 Expr *pLeft = pExpr->pLeft;
2165 struct ExprList_item *pLItem = pExpr->pList->a;
2166 Expr *pRight = pLItem->pExpr;
drh35573352008-01-08 23:54:25 +00002167
drhda250ea2008-04-01 05:07:14 +00002168 codeCompareOperands(pParse, pLeft, &r1, &regFree1,
2169 pRight, &r2, &regFree2);
drhc5499be2008-04-01 15:06:33 +00002170 testcase( regFree1==0 );
2171 testcase( regFree2==0 );
drh2dcef112008-01-12 19:03:48 +00002172 r3 = sqlite3GetTempReg(pParse);
drh678ccce2008-03-31 18:19:54 +00002173 r4 = sqlite3GetTempReg(pParse);
drh35573352008-01-08 23:54:25 +00002174 codeCompare(pParse, pLeft, pRight, OP_Ge,
2175 r1, r2, r3, SQLITE_STOREP2);
drhbe5c89a2004-07-26 00:31:09 +00002176 pLItem++;
2177 pRight = pLItem->pExpr;
drh2dcef112008-01-12 19:03:48 +00002178 sqlite3ReleaseTempReg(pParse, regFree2);
2179 r2 = sqlite3ExprCodeTemp(pParse, pRight, &regFree2);
drhc5499be2008-04-01 15:06:33 +00002180 testcase( regFree2==0 );
drh678ccce2008-03-31 18:19:54 +00002181 codeCompare(pParse, pLeft, pRight, OP_Le, r1, r2, r4, SQLITE_STOREP2);
2182 sqlite3VdbeAddOp3(v, OP_And, r3, r4, target);
drh2dcef112008-01-12 19:03:48 +00002183 sqlite3ReleaseTempReg(pParse, r3);
drh678ccce2008-03-31 18:19:54 +00002184 sqlite3ReleaseTempReg(pParse, r4);
drhfef52082000-06-06 01:50:43 +00002185 break;
2186 }
drh4f07e5f2007-05-14 11:34:46 +00002187 case TK_UPLUS: {
drh2dcef112008-01-12 19:03:48 +00002188 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
drha2e00042002-01-22 03:13:42 +00002189 break;
2190 }
drh2dcef112008-01-12 19:03:48 +00002191
2192 /*
2193 ** Form A:
2194 ** CASE x WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
2195 **
2196 ** Form B:
2197 ** CASE WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
2198 **
2199 ** Form A is can be transformed into the equivalent form B as follows:
2200 ** CASE WHEN x=e1 THEN r1 WHEN x=e2 THEN r2 ...
2201 ** WHEN x=eN THEN rN ELSE y END
2202 **
2203 ** X (if it exists) is in pExpr->pLeft.
2204 ** Y is in pExpr->pRight. The Y is also optional. If there is no
2205 ** ELSE clause and no other term matches, then the result of the
2206 ** exprssion is NULL.
2207 ** Ei is in pExpr->pList->a[i*2] and Ri is pExpr->pList->a[i*2+1].
2208 **
2209 ** The result of the expression is the Ri for the first matching Ei,
2210 ** or if there is no matching Ei, the ELSE term Y, or if there is
2211 ** no ELSE term, NULL.
2212 */
drh17a7f8d2002-03-24 13:13:27 +00002213 case TK_CASE: {
drh2dcef112008-01-12 19:03:48 +00002214 int endLabel; /* GOTO label for end of CASE stmt */
2215 int nextCase; /* GOTO label for next WHEN clause */
2216 int nExpr; /* 2x number of WHEN terms */
2217 int i; /* Loop counter */
2218 ExprList *pEList; /* List of WHEN terms */
2219 struct ExprList_item *aListelem; /* Array of WHEN terms */
2220 Expr opCompare; /* The X==Ei expression */
2221 Expr cacheX; /* Cached expression X */
2222 Expr *pX; /* The X expression */
drh1bd10f82008-12-10 21:19:56 +00002223 Expr *pTest = 0; /* X==Ei (form A) or just Ei (form B) */
drh17a7f8d2002-03-24 13:13:27 +00002224
2225 assert(pExpr->pList);
2226 assert((pExpr->pList->nExpr % 2) == 0);
2227 assert(pExpr->pList->nExpr > 0);
drhbe5c89a2004-07-26 00:31:09 +00002228 pEList = pExpr->pList;
2229 aListelem = pEList->a;
2230 nExpr = pEList->nExpr;
drh2dcef112008-01-12 19:03:48 +00002231 endLabel = sqlite3VdbeMakeLabel(v);
2232 if( (pX = pExpr->pLeft)!=0 ){
2233 cacheX = *pX;
drhc5499be2008-04-01 15:06:33 +00002234 testcase( pX->op==TK_COLUMN || pX->op==TK_REGISTER );
drh2dcef112008-01-12 19:03:48 +00002235 cacheX.iTable = sqlite3ExprCodeTemp(pParse, pX, &regFree1);
drhc5499be2008-04-01 15:06:33 +00002236 testcase( regFree1==0 );
drh2dcef112008-01-12 19:03:48 +00002237 cacheX.op = TK_REGISTER;
2238 opCompare.op = TK_EQ;
2239 opCompare.pLeft = &cacheX;
2240 pTest = &opCompare;
drh17a7f8d2002-03-24 13:13:27 +00002241 }
drhc5499be2008-04-01 15:06:33 +00002242 pParse->disableColCache++;
drhf5905aa2002-05-26 20:54:33 +00002243 for(i=0; i<nExpr; i=i+2){
drh2dcef112008-01-12 19:03:48 +00002244 if( pX ){
drh1bd10f82008-12-10 21:19:56 +00002245 assert( pTest!=0 );
drh2dcef112008-01-12 19:03:48 +00002246 opCompare.pRight = aListelem[i].pExpr;
drh17a7f8d2002-03-24 13:13:27 +00002247 }else{
drh2dcef112008-01-12 19:03:48 +00002248 pTest = aListelem[i].pExpr;
drh17a7f8d2002-03-24 13:13:27 +00002249 }
drh2dcef112008-01-12 19:03:48 +00002250 nextCase = sqlite3VdbeMakeLabel(v);
drhc5499be2008-04-01 15:06:33 +00002251 testcase( pTest->op==TK_COLUMN || pTest->op==TK_REGISTER );
drh2dcef112008-01-12 19:03:48 +00002252 sqlite3ExprIfFalse(pParse, pTest, nextCase, SQLITE_JUMPIFNULL);
drhc5499be2008-04-01 15:06:33 +00002253 testcase( aListelem[i+1].pExpr->op==TK_COLUMN );
2254 testcase( aListelem[i+1].pExpr->op==TK_REGISTER );
drh9de221d2008-01-05 06:51:30 +00002255 sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target);
drh2dcef112008-01-12 19:03:48 +00002256 sqlite3VdbeAddOp2(v, OP_Goto, 0, endLabel);
2257 sqlite3VdbeResolveLabel(v, nextCase);
drhf570f012002-05-31 15:51:25 +00002258 }
drh17a7f8d2002-03-24 13:13:27 +00002259 if( pExpr->pRight ){
drh9de221d2008-01-05 06:51:30 +00002260 sqlite3ExprCode(pParse, pExpr->pRight, target);
drh17a7f8d2002-03-24 13:13:27 +00002261 }else{
drh9de221d2008-01-05 06:51:30 +00002262 sqlite3VdbeAddOp2(v, OP_Null, 0, target);
drh17a7f8d2002-03-24 13:13:27 +00002263 }
drh2dcef112008-01-12 19:03:48 +00002264 sqlite3VdbeResolveLabel(v, endLabel);
drhc5499be2008-04-01 15:06:33 +00002265 assert( pParse->disableColCache>0 );
2266 pParse->disableColCache--;
danielk19776f349032002-06-11 02:25:40 +00002267 break;
2268 }
danielk19775338a5f2005-01-20 13:03:10 +00002269#ifndef SQLITE_OMIT_TRIGGER
danielk19776f349032002-06-11 02:25:40 +00002270 case TK_RAISE: {
2271 if( !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +00002272 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00002273 "RAISE() may only be used within a trigger-program");
drh389a1ad2008-01-03 23:44:53 +00002274 return 0;
danielk19776f349032002-06-11 02:25:40 +00002275 }
drhad6d9462004-09-19 02:15:24 +00002276 if( pExpr->iColumn!=OE_Ignore ){
2277 assert( pExpr->iColumn==OE_Rollback ||
2278 pExpr->iColumn == OE_Abort ||
2279 pExpr->iColumn == OE_Fail );
drh8b213892008-08-29 02:14:02 +00002280 sqlite3DequoteExpr(db, pExpr);
drh66a51672008-01-03 00:01:23 +00002281 sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn, 0,
drh2646da72005-12-09 20:02:05 +00002282 (char*)pExpr->token.z, pExpr->token.n);
danielk19776f349032002-06-11 02:25:40 +00002283 } else {
drhad6d9462004-09-19 02:15:24 +00002284 assert( pExpr->iColumn == OE_Ignore );
drh66a51672008-01-03 00:01:23 +00002285 sqlite3VdbeAddOp2(v, OP_ContextPop, 0, 0);
2286 sqlite3VdbeAddOp2(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
drhd4e70eb2008-01-02 00:34:36 +00002287 VdbeComment((v, "raise(IGNORE)"));
danielk19776f349032002-06-11 02:25:40 +00002288 }
drhffe07b22005-11-03 00:41:17 +00002289 break;
drh17a7f8d2002-03-24 13:13:27 +00002290 }
danielk19775338a5f2005-01-20 13:03:10 +00002291#endif
drhffe07b22005-11-03 00:41:17 +00002292 }
drh2dcef112008-01-12 19:03:48 +00002293 sqlite3ReleaseTempReg(pParse, regFree1);
2294 sqlite3ReleaseTempReg(pParse, regFree2);
2295 return inReg;
2296}
2297
2298/*
2299** Generate code to evaluate an expression and store the results
2300** into a register. Return the register number where the results
2301** are stored.
2302**
2303** If the register is a temporary register that can be deallocated,
drh678ccce2008-03-31 18:19:54 +00002304** then write its number into *pReg. If the result register is not
drh2dcef112008-01-12 19:03:48 +00002305** a temporary, then set *pReg to zero.
2306*/
2307int sqlite3ExprCodeTemp(Parse *pParse, Expr *pExpr, int *pReg){
2308 int r1 = sqlite3GetTempReg(pParse);
2309 int r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
2310 if( r2==r1 ){
2311 *pReg = r1;
2312 }else{
2313 sqlite3ReleaseTempReg(pParse, r1);
2314 *pReg = 0;
2315 }
2316 return r2;
2317}
2318
2319/*
2320** Generate code that will evaluate expression pExpr and store the
2321** results in register target. The results are guaranteed to appear
2322** in register target.
2323*/
2324int sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){
drh9cbf3422008-01-17 16:22:13 +00002325 int inReg;
2326
2327 assert( target>0 && target<=pParse->nMem );
2328 inReg = sqlite3ExprCodeTarget(pParse, pExpr, target);
drh0e359b32008-01-13 19:02:11 +00002329 assert( pParse->pVdbe || pParse->db->mallocFailed );
2330 if( inReg!=target && pParse->pVdbe ){
drh9cbf3422008-01-17 16:22:13 +00002331 sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, inReg, target);
drhcce7d172000-05-31 15:34:51 +00002332 }
drh389a1ad2008-01-03 23:44:53 +00002333 return target;
drhcce7d172000-05-31 15:34:51 +00002334}
2335
2336/*
drh2dcef112008-01-12 19:03:48 +00002337** Generate code that evalutes the given expression and puts the result
drhde4fcfd2008-01-19 23:50:26 +00002338** in register target.
drh25303782004-12-07 15:41:48 +00002339**
drh2dcef112008-01-12 19:03:48 +00002340** Also make a copy of the expression results into another "cache" register
2341** and modify the expression so that the next time it is evaluated,
2342** the result is a copy of the cache register.
2343**
2344** This routine is used for expressions that are used multiple
2345** times. They are evaluated once and the results of the expression
2346** are reused.
drh25303782004-12-07 15:41:48 +00002347*/
drh2dcef112008-01-12 19:03:48 +00002348int sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr, int target){
drh25303782004-12-07 15:41:48 +00002349 Vdbe *v = pParse->pVdbe;
drh2dcef112008-01-12 19:03:48 +00002350 int inReg;
2351 inReg = sqlite3ExprCode(pParse, pExpr, target);
drhde4fcfd2008-01-19 23:50:26 +00002352 assert( target>0 );
drh2dcef112008-01-12 19:03:48 +00002353 if( pExpr->op!=TK_REGISTER ){
2354 int iMem;
drhde4fcfd2008-01-19 23:50:26 +00002355 iMem = ++pParse->nMem;
2356 sqlite3VdbeAddOp2(v, OP_Copy, inReg, iMem);
drh2dcef112008-01-12 19:03:48 +00002357 pExpr->iTable = iMem;
drh25303782004-12-07 15:41:48 +00002358 pExpr->op = TK_REGISTER;
2359 }
drh2dcef112008-01-12 19:03:48 +00002360 return inReg;
drh25303782004-12-07 15:41:48 +00002361}
drh2dcef112008-01-12 19:03:48 +00002362
drh678ccce2008-03-31 18:19:54 +00002363/*
drh47de9552008-04-01 18:04:11 +00002364** Return TRUE if pExpr is an constant expression that is appropriate
2365** for factoring out of a loop. Appropriate expressions are:
2366**
2367** * Any expression that evaluates to two or more opcodes.
2368**
2369** * Any OP_Integer, OP_Real, OP_String, OP_Blob, OP_Null,
2370** or OP_Variable that does not need to be placed in a
2371** specific register.
2372**
2373** There is no point in factoring out single-instruction constant
2374** expressions that need to be placed in a particular register.
2375** We could factor them out, but then we would end up adding an
2376** OP_SCopy instruction to move the value into the correct register
2377** later. We might as well just use the original instruction and
2378** avoid the OP_SCopy.
2379*/
2380static int isAppropriateForFactoring(Expr *p){
2381 if( !sqlite3ExprIsConstantNotJoin(p) ){
2382 return 0; /* Only constant expressions are appropriate for factoring */
2383 }
2384 if( (p->flags & EP_FixedDest)==0 ){
2385 return 1; /* Any constant without a fixed destination is appropriate */
2386 }
2387 while( p->op==TK_UPLUS ) p = p->pLeft;
2388 switch( p->op ){
2389#ifndef SQLITE_OMIT_BLOB_LITERAL
2390 case TK_BLOB:
2391#endif
2392 case TK_VARIABLE:
2393 case TK_INTEGER:
2394 case TK_FLOAT:
2395 case TK_NULL:
2396 case TK_STRING: {
2397 testcase( p->op==TK_BLOB );
2398 testcase( p->op==TK_VARIABLE );
2399 testcase( p->op==TK_INTEGER );
2400 testcase( p->op==TK_FLOAT );
2401 testcase( p->op==TK_NULL );
2402 testcase( p->op==TK_STRING );
2403 /* Single-instruction constants with a fixed destination are
2404 ** better done in-line. If we factor them, they will just end
2405 ** up generating an OP_SCopy to move the value to the destination
2406 ** register. */
2407 return 0;
2408 }
2409 case TK_UMINUS: {
2410 if( p->pLeft->op==TK_FLOAT || p->pLeft->op==TK_INTEGER ){
2411 return 0;
2412 }
2413 break;
2414 }
2415 default: {
2416 break;
2417 }
2418 }
2419 return 1;
2420}
2421
2422/*
2423** If pExpr is a constant expression that is appropriate for
2424** factoring out of a loop, then evaluate the expression
drh678ccce2008-03-31 18:19:54 +00002425** into a register and convert the expression into a TK_REGISTER
2426** expression.
2427*/
drh7d10d5a2008-08-20 16:35:10 +00002428static int evalConstExpr(Walker *pWalker, Expr *pExpr){
2429 Parse *pParse = pWalker->pParse;
drh47de9552008-04-01 18:04:11 +00002430 switch( pExpr->op ){
2431 case TK_REGISTER: {
2432 return 1;
2433 }
2434 case TK_FUNCTION:
2435 case TK_AGG_FUNCTION:
2436 case TK_CONST_FUNC: {
2437 /* The arguments to a function have a fixed destination.
2438 ** Mark them this way to avoid generated unneeded OP_SCopy
2439 ** instructions.
2440 */
2441 ExprList *pList = pExpr->pList;
2442 if( pList ){
2443 int i = pList->nExpr;
2444 struct ExprList_item *pItem = pList->a;
2445 for(; i>0; i--, pItem++){
2446 if( pItem->pExpr ) pItem->pExpr->flags |= EP_FixedDest;
2447 }
2448 }
2449 break;
2450 }
drh678ccce2008-03-31 18:19:54 +00002451 }
drh47de9552008-04-01 18:04:11 +00002452 if( isAppropriateForFactoring(pExpr) ){
drh678ccce2008-03-31 18:19:54 +00002453 int r1 = ++pParse->nMem;
2454 int r2;
2455 r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
drhc5499be2008-04-01 15:06:33 +00002456 if( r1!=r2 ) sqlite3ReleaseTempReg(pParse, r1);
drh678ccce2008-03-31 18:19:54 +00002457 pExpr->op = TK_REGISTER;
2458 pExpr->iTable = r2;
drh7d10d5a2008-08-20 16:35:10 +00002459 return WRC_Prune;
drh678ccce2008-03-31 18:19:54 +00002460 }
drh7d10d5a2008-08-20 16:35:10 +00002461 return WRC_Continue;
drh678ccce2008-03-31 18:19:54 +00002462}
2463
2464/*
2465** Preevaluate constant subexpressions within pExpr and store the
2466** results in registers. Modify pExpr so that the constant subexpresions
2467** are TK_REGISTER opcodes that refer to the precomputed values.
2468*/
2469void sqlite3ExprCodeConstants(Parse *pParse, Expr *pExpr){
drh7d10d5a2008-08-20 16:35:10 +00002470 Walker w;
2471 w.xExprCallback = evalConstExpr;
2472 w.xSelectCallback = 0;
2473 w.pParse = pParse;
2474 sqlite3WalkExpr(&w, pExpr);
drh678ccce2008-03-31 18:19:54 +00002475}
2476
drh25303782004-12-07 15:41:48 +00002477
2478/*
drh268380c2004-02-25 13:47:31 +00002479** Generate code that pushes the value of every element of the given
drh9cbf3422008-01-17 16:22:13 +00002480** expression list into a sequence of registers beginning at target.
drh268380c2004-02-25 13:47:31 +00002481**
drh892d3172008-01-10 03:46:36 +00002482** Return the number of elements evaluated.
drh268380c2004-02-25 13:47:31 +00002483*/
danielk19774adee202004-05-08 08:23:19 +00002484int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00002485 Parse *pParse, /* Parsing context */
drh389a1ad2008-01-03 23:44:53 +00002486 ExprList *pList, /* The expression list to be coded */
drh191b54c2008-04-15 12:14:21 +00002487 int target, /* Where to write results */
drhd1766112008-09-17 00:13:12 +00002488 int doHardCopy /* Make a hard copy of every element */
drh268380c2004-02-25 13:47:31 +00002489){
2490 struct ExprList_item *pItem;
drh9cbf3422008-01-17 16:22:13 +00002491 int i, n;
drh9d8b3072008-08-22 16:29:51 +00002492 assert( pList!=0 );
drh9cbf3422008-01-17 16:22:13 +00002493 assert( target>0 );
drh268380c2004-02-25 13:47:31 +00002494 n = pList->nExpr;
drh191b54c2008-04-15 12:14:21 +00002495 for(pItem=pList->a, i=0; i<n; i++, pItem++){
drh8b213892008-08-29 02:14:02 +00002496 if( pItem->iAlias ){
drh31daa632008-10-25 15:03:20 +00002497 int iReg = codeAlias(pParse, pItem->iAlias, pItem->pExpr, target+i);
drh8b213892008-08-29 02:14:02 +00002498 Vdbe *v = sqlite3GetVdbe(pParse);
drh31daa632008-10-25 15:03:20 +00002499 if( iReg!=target+i ){
2500 sqlite3VdbeAddOp2(v, OP_SCopy, iReg, target+i);
2501 }
drhd1766112008-09-17 00:13:12 +00002502 }else{
drh8b213892008-08-29 02:14:02 +00002503 sqlite3ExprCode(pParse, pItem->pExpr, target+i);
2504 }
drhd1766112008-09-17 00:13:12 +00002505 if( doHardCopy ){
2506 sqlite3ExprHardCopy(pParse, target, n);
2507 }
drh268380c2004-02-25 13:47:31 +00002508 }
drhf9b596e2004-05-26 16:54:42 +00002509 return n;
drh268380c2004-02-25 13:47:31 +00002510}
2511
2512/*
drhcce7d172000-05-31 15:34:51 +00002513** Generate code for a boolean expression such that a jump is made
2514** to the label "dest" if the expression is true but execution
2515** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00002516**
2517** If the expression evaluates to NULL (neither true nor false), then
drh35573352008-01-08 23:54:25 +00002518** take the jump if the jumpIfNull flag is SQLITE_JUMPIFNULL.
drhf2bc0132004-10-04 13:19:23 +00002519**
2520** This code depends on the fact that certain token values (ex: TK_EQ)
2521** are the same as opcode values (ex: OP_Eq) that implement the corresponding
2522** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
2523** the make process cause these values to align. Assert()s in the code
2524** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00002525*/
danielk19774adee202004-05-08 08:23:19 +00002526void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00002527 Vdbe *v = pParse->pVdbe;
2528 int op = 0;
drh2dcef112008-01-12 19:03:48 +00002529 int regFree1 = 0;
2530 int regFree2 = 0;
2531 int r1, r2;
2532
drh35573352008-01-08 23:54:25 +00002533 assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
drhdaffd0e2001-04-11 14:28:42 +00002534 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00002535 op = pExpr->op;
2536 switch( op ){
drhcce7d172000-05-31 15:34:51 +00002537 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00002538 int d2 = sqlite3VdbeMakeLabel(v);
drhc5499be2008-04-01 15:06:33 +00002539 testcase( jumpIfNull==0 );
2540 testcase( pParse->disableColCache==0 );
drh35573352008-01-08 23:54:25 +00002541 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,jumpIfNull^SQLITE_JUMPIFNULL);
drhe55cbd72008-03-31 23:48:03 +00002542 pParse->disableColCache++;
danielk19774adee202004-05-08 08:23:19 +00002543 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhc5499be2008-04-01 15:06:33 +00002544 assert( pParse->disableColCache>0 );
drhe55cbd72008-03-31 23:48:03 +00002545 pParse->disableColCache--;
danielk19774adee202004-05-08 08:23:19 +00002546 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00002547 break;
2548 }
2549 case TK_OR: {
drhc5499be2008-04-01 15:06:33 +00002550 testcase( jumpIfNull==0 );
2551 testcase( pParse->disableColCache==0 );
danielk19774adee202004-05-08 08:23:19 +00002552 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhe55cbd72008-03-31 23:48:03 +00002553 pParse->disableColCache++;
danielk19774adee202004-05-08 08:23:19 +00002554 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhc5499be2008-04-01 15:06:33 +00002555 assert( pParse->disableColCache>0 );
drhe55cbd72008-03-31 23:48:03 +00002556 pParse->disableColCache--;
drhcce7d172000-05-31 15:34:51 +00002557 break;
2558 }
2559 case TK_NOT: {
drhc5499be2008-04-01 15:06:33 +00002560 testcase( jumpIfNull==0 );
danielk19774adee202004-05-08 08:23:19 +00002561 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002562 break;
2563 }
2564 case TK_LT:
2565 case TK_LE:
2566 case TK_GT:
2567 case TK_GE:
2568 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00002569 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00002570 assert( TK_LT==OP_Lt );
2571 assert( TK_LE==OP_Le );
2572 assert( TK_GT==OP_Gt );
2573 assert( TK_GE==OP_Ge );
2574 assert( TK_EQ==OP_Eq );
2575 assert( TK_NE==OP_Ne );
drhc5499be2008-04-01 15:06:33 +00002576 testcase( op==TK_LT );
2577 testcase( op==TK_LE );
2578 testcase( op==TK_GT );
2579 testcase( op==TK_GE );
2580 testcase( op==TK_EQ );
2581 testcase( op==TK_NE );
2582 testcase( jumpIfNull==0 );
drhda250ea2008-04-01 05:07:14 +00002583 codeCompareOperands(pParse, pExpr->pLeft, &r1, &regFree1,
2584 pExpr->pRight, &r2, &regFree2);
drh35573352008-01-08 23:54:25 +00002585 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
drh2dcef112008-01-12 19:03:48 +00002586 r1, r2, dest, jumpIfNull);
drhc5499be2008-04-01 15:06:33 +00002587 testcase( regFree1==0 );
2588 testcase( regFree2==0 );
drhcce7d172000-05-31 15:34:51 +00002589 break;
2590 }
2591 case TK_ISNULL:
2592 case TK_NOTNULL: {
drhf2bc0132004-10-04 13:19:23 +00002593 assert( TK_ISNULL==OP_IsNull );
2594 assert( TK_NOTNULL==OP_NotNull );
drhc5499be2008-04-01 15:06:33 +00002595 testcase( op==TK_ISNULL );
2596 testcase( op==TK_NOTNULL );
drh2dcef112008-01-12 19:03:48 +00002597 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
2598 sqlite3VdbeAddOp2(v, op, r1, dest);
drhc5499be2008-04-01 15:06:33 +00002599 testcase( regFree1==0 );
drhcce7d172000-05-31 15:34:51 +00002600 break;
2601 }
drhfef52082000-06-06 01:50:43 +00002602 case TK_BETWEEN: {
drh2dcef112008-01-12 19:03:48 +00002603 /* x BETWEEN y AND z
danielk19770202b292004-06-09 09:55:16 +00002604 **
drh2dcef112008-01-12 19:03:48 +00002605 ** Is equivalent to
2606 **
2607 ** x>=y AND x<=z
2608 **
2609 ** Code it as such, taking care to do the common subexpression
2610 ** elementation of x.
danielk19770202b292004-06-09 09:55:16 +00002611 */
drh2dcef112008-01-12 19:03:48 +00002612 Expr exprAnd;
2613 Expr compLeft;
2614 Expr compRight;
2615 Expr exprX;
danielk19770202b292004-06-09 09:55:16 +00002616
drh2dcef112008-01-12 19:03:48 +00002617 exprX = *pExpr->pLeft;
2618 exprAnd.op = TK_AND;
2619 exprAnd.pLeft = &compLeft;
2620 exprAnd.pRight = &compRight;
2621 compLeft.op = TK_GE;
2622 compLeft.pLeft = &exprX;
2623 compLeft.pRight = pExpr->pList->a[0].pExpr;
2624 compRight.op = TK_LE;
2625 compRight.pLeft = &exprX;
2626 compRight.pRight = pExpr->pList->a[1].pExpr;
2627 exprX.iTable = sqlite3ExprCodeTemp(pParse, &exprX, &regFree1);
drhc5499be2008-04-01 15:06:33 +00002628 testcase( regFree1==0 );
drh2dcef112008-01-12 19:03:48 +00002629 exprX.op = TK_REGISTER;
drhc5499be2008-04-01 15:06:33 +00002630 testcase( jumpIfNull==0 );
drh2dcef112008-01-12 19:03:48 +00002631 sqlite3ExprIfTrue(pParse, &exprAnd, dest, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00002632 break;
2633 }
drhcce7d172000-05-31 15:34:51 +00002634 default: {
drh2dcef112008-01-12 19:03:48 +00002635 r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
2636 sqlite3VdbeAddOp3(v, OP_If, r1, dest, jumpIfNull!=0);
drhc5499be2008-04-01 15:06:33 +00002637 testcase( regFree1==0 );
2638 testcase( jumpIfNull==0 );
drhcce7d172000-05-31 15:34:51 +00002639 break;
2640 }
2641 }
drh2dcef112008-01-12 19:03:48 +00002642 sqlite3ReleaseTempReg(pParse, regFree1);
2643 sqlite3ReleaseTempReg(pParse, regFree2);
drhcce7d172000-05-31 15:34:51 +00002644}
2645
2646/*
drh66b89c82000-11-28 20:47:17 +00002647** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00002648** to the label "dest" if the expression is false but execution
2649** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00002650**
2651** If the expression evaluates to NULL (neither true nor false) then
drh35573352008-01-08 23:54:25 +00002652** jump if jumpIfNull is SQLITE_JUMPIFNULL or fall through if jumpIfNull
2653** is 0.
drhcce7d172000-05-31 15:34:51 +00002654*/
danielk19774adee202004-05-08 08:23:19 +00002655void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00002656 Vdbe *v = pParse->pVdbe;
2657 int op = 0;
drh2dcef112008-01-12 19:03:48 +00002658 int regFree1 = 0;
2659 int regFree2 = 0;
2660 int r1, r2;
2661
drh35573352008-01-08 23:54:25 +00002662 assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
drhdaffd0e2001-04-11 14:28:42 +00002663 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00002664
2665 /* The value of pExpr->op and op are related as follows:
2666 **
2667 ** pExpr->op op
2668 ** --------- ----------
2669 ** TK_ISNULL OP_NotNull
2670 ** TK_NOTNULL OP_IsNull
2671 ** TK_NE OP_Eq
2672 ** TK_EQ OP_Ne
2673 ** TK_GT OP_Le
2674 ** TK_LE OP_Gt
2675 ** TK_GE OP_Lt
2676 ** TK_LT OP_Ge
2677 **
2678 ** For other values of pExpr->op, op is undefined and unused.
2679 ** The value of TK_ and OP_ constants are arranged such that we
2680 ** can compute the mapping above using the following expression.
2681 ** Assert()s verify that the computation is correct.
2682 */
2683 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
2684
2685 /* Verify correct alignment of TK_ and OP_ constants
2686 */
2687 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
2688 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
2689 assert( pExpr->op!=TK_NE || op==OP_Eq );
2690 assert( pExpr->op!=TK_EQ || op==OP_Ne );
2691 assert( pExpr->op!=TK_LT || op==OP_Ge );
2692 assert( pExpr->op!=TK_LE || op==OP_Gt );
2693 assert( pExpr->op!=TK_GT || op==OP_Le );
2694 assert( pExpr->op!=TK_GE || op==OP_Lt );
2695
drhcce7d172000-05-31 15:34:51 +00002696 switch( pExpr->op ){
2697 case TK_AND: {
drhc5499be2008-04-01 15:06:33 +00002698 testcase( jumpIfNull==0 );
2699 testcase( pParse->disableColCache==0 );
danielk19774adee202004-05-08 08:23:19 +00002700 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhe55cbd72008-03-31 23:48:03 +00002701 pParse->disableColCache++;
danielk19774adee202004-05-08 08:23:19 +00002702 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhc5499be2008-04-01 15:06:33 +00002703 assert( pParse->disableColCache>0 );
drhe55cbd72008-03-31 23:48:03 +00002704 pParse->disableColCache--;
drhcce7d172000-05-31 15:34:51 +00002705 break;
2706 }
2707 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00002708 int d2 = sqlite3VdbeMakeLabel(v);
drhc5499be2008-04-01 15:06:33 +00002709 testcase( jumpIfNull==0 );
2710 testcase( pParse->disableColCache==0 );
drh35573352008-01-08 23:54:25 +00002711 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, jumpIfNull^SQLITE_JUMPIFNULL);
drhe55cbd72008-03-31 23:48:03 +00002712 pParse->disableColCache++;
danielk19774adee202004-05-08 08:23:19 +00002713 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhc5499be2008-04-01 15:06:33 +00002714 assert( pParse->disableColCache>0 );
drhe55cbd72008-03-31 23:48:03 +00002715 pParse->disableColCache--;
danielk19774adee202004-05-08 08:23:19 +00002716 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00002717 break;
2718 }
2719 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00002720 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002721 break;
2722 }
2723 case TK_LT:
2724 case TK_LE:
2725 case TK_GT:
2726 case TK_GE:
2727 case TK_NE:
2728 case TK_EQ: {
drhc5499be2008-04-01 15:06:33 +00002729 testcase( op==TK_LT );
2730 testcase( op==TK_LE );
2731 testcase( op==TK_GT );
2732 testcase( op==TK_GE );
2733 testcase( op==TK_EQ );
2734 testcase( op==TK_NE );
2735 testcase( jumpIfNull==0 );
drhda250ea2008-04-01 05:07:14 +00002736 codeCompareOperands(pParse, pExpr->pLeft, &r1, &regFree1,
2737 pExpr->pRight, &r2, &regFree2);
drh35573352008-01-08 23:54:25 +00002738 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
drh2dcef112008-01-12 19:03:48 +00002739 r1, r2, dest, jumpIfNull);
drhc5499be2008-04-01 15:06:33 +00002740 testcase( regFree1==0 );
2741 testcase( regFree2==0 );
drhcce7d172000-05-31 15:34:51 +00002742 break;
2743 }
drhcce7d172000-05-31 15:34:51 +00002744 case TK_ISNULL:
2745 case TK_NOTNULL: {
drhc5499be2008-04-01 15:06:33 +00002746 testcase( op==TK_ISNULL );
2747 testcase( op==TK_NOTNULL );
drh2dcef112008-01-12 19:03:48 +00002748 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
2749 sqlite3VdbeAddOp2(v, op, r1, dest);
drhc5499be2008-04-01 15:06:33 +00002750 testcase( regFree1==0 );
drhcce7d172000-05-31 15:34:51 +00002751 break;
2752 }
drhfef52082000-06-06 01:50:43 +00002753 case TK_BETWEEN: {
drh2dcef112008-01-12 19:03:48 +00002754 /* x BETWEEN y AND z
danielk19770202b292004-06-09 09:55:16 +00002755 **
drh2dcef112008-01-12 19:03:48 +00002756 ** Is equivalent to
2757 **
2758 ** x>=y AND x<=z
2759 **
2760 ** Code it as such, taking care to do the common subexpression
2761 ** elementation of x.
danielk19770202b292004-06-09 09:55:16 +00002762 */
drh2dcef112008-01-12 19:03:48 +00002763 Expr exprAnd;
2764 Expr compLeft;
2765 Expr compRight;
2766 Expr exprX;
drhbe5c89a2004-07-26 00:31:09 +00002767
drh2dcef112008-01-12 19:03:48 +00002768 exprX = *pExpr->pLeft;
2769 exprAnd.op = TK_AND;
2770 exprAnd.pLeft = &compLeft;
2771 exprAnd.pRight = &compRight;
2772 compLeft.op = TK_GE;
2773 compLeft.pLeft = &exprX;
2774 compLeft.pRight = pExpr->pList->a[0].pExpr;
2775 compRight.op = TK_LE;
2776 compRight.pLeft = &exprX;
2777 compRight.pRight = pExpr->pList->a[1].pExpr;
2778 exprX.iTable = sqlite3ExprCodeTemp(pParse, &exprX, &regFree1);
drhc5499be2008-04-01 15:06:33 +00002779 testcase( regFree1==0 );
drh2dcef112008-01-12 19:03:48 +00002780 exprX.op = TK_REGISTER;
drhc5499be2008-04-01 15:06:33 +00002781 testcase( jumpIfNull==0 );
drh2dcef112008-01-12 19:03:48 +00002782 sqlite3ExprIfFalse(pParse, &exprAnd, dest, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00002783 break;
2784 }
drhcce7d172000-05-31 15:34:51 +00002785 default: {
drh2dcef112008-01-12 19:03:48 +00002786 r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
2787 sqlite3VdbeAddOp3(v, OP_IfNot, r1, dest, jumpIfNull!=0);
drhc5499be2008-04-01 15:06:33 +00002788 testcase( regFree1==0 );
2789 testcase( jumpIfNull==0 );
drhcce7d172000-05-31 15:34:51 +00002790 break;
2791 }
2792 }
drh2dcef112008-01-12 19:03:48 +00002793 sqlite3ReleaseTempReg(pParse, regFree1);
2794 sqlite3ReleaseTempReg(pParse, regFree2);
drhcce7d172000-05-31 15:34:51 +00002795}
drh22827922000-06-06 17:27:05 +00002796
2797/*
2798** Do a deep comparison of two expression trees. Return TRUE (non-zero)
2799** if they are identical and return FALSE if they differ in any way.
drhd40aab02007-02-24 15:29:03 +00002800**
2801** Sometimes this routine will return FALSE even if the two expressions
2802** really are equivalent. If we cannot prove that the expressions are
2803** identical, we return FALSE just to be safe. So if this routine
2804** returns false, then you do not really know for certain if the two
2805** expressions are the same. But if you get a TRUE return, then you
2806** can be sure the expressions are the same. In the places where
2807** this routine is used, it does not hurt to get an extra FALSE - that
2808** just might result in some slightly slower code. But returning
2809** an incorrect TRUE could lead to a malfunction.
drh22827922000-06-06 17:27:05 +00002810*/
danielk19774adee202004-05-08 08:23:19 +00002811int sqlite3ExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00002812 int i;
danielk19774b202ae2006-01-23 05:50:58 +00002813 if( pA==0||pB==0 ){
2814 return pB==pA;
drh22827922000-06-06 17:27:05 +00002815 }
2816 if( pA->op!=pB->op ) return 0;
drhfd357972005-09-09 01:33:19 +00002817 if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 0;
danielk19774adee202004-05-08 08:23:19 +00002818 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
2819 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00002820 if( pA->pList ){
2821 if( pB->pList==0 ) return 0;
2822 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
2823 for(i=0; i<pA->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00002824 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00002825 return 0;
2826 }
2827 }
2828 }else if( pB->pList ){
2829 return 0;
2830 }
2831 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00002832 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drhdd735212007-02-24 13:53:05 +00002833 if( pA->op!=TK_COLUMN && pA->token.z ){
drh22827922000-06-06 17:27:05 +00002834 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00002835 if( pB->token.n!=pA->token.n ) return 0;
drh2646da72005-12-09 20:02:05 +00002836 if( sqlite3StrNICmp((char*)pA->token.z,(char*)pB->token.z,pB->token.n)!=0 ){
2837 return 0;
2838 }
drh22827922000-06-06 17:27:05 +00002839 }
2840 return 1;
2841}
2842
drh13449892005-09-07 21:22:45 +00002843
drh22827922000-06-06 17:27:05 +00002844/*
drh13449892005-09-07 21:22:45 +00002845** Add a new element to the pAggInfo->aCol[] array. Return the index of
2846** the new element. Return a negative number if malloc fails.
drh22827922000-06-06 17:27:05 +00002847*/
drh17435752007-08-16 04:30:38 +00002848static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){
drh13449892005-09-07 21:22:45 +00002849 int i;
drhcf643722007-03-27 13:36:37 +00002850 pInfo->aCol = sqlite3ArrayAllocate(
drh17435752007-08-16 04:30:38 +00002851 db,
drhcf643722007-03-27 13:36:37 +00002852 pInfo->aCol,
2853 sizeof(pInfo->aCol[0]),
2854 3,
2855 &pInfo->nColumn,
2856 &pInfo->nColumnAlloc,
2857 &i
2858 );
drh13449892005-09-07 21:22:45 +00002859 return i;
2860}
2861
2862/*
2863** Add a new element to the pAggInfo->aFunc[] array. Return the index of
2864** the new element. Return a negative number if malloc fails.
2865*/
drh17435752007-08-16 04:30:38 +00002866static int addAggInfoFunc(sqlite3 *db, AggInfo *pInfo){
drh13449892005-09-07 21:22:45 +00002867 int i;
drhcf643722007-03-27 13:36:37 +00002868 pInfo->aFunc = sqlite3ArrayAllocate(
drh17435752007-08-16 04:30:38 +00002869 db,
drhcf643722007-03-27 13:36:37 +00002870 pInfo->aFunc,
2871 sizeof(pInfo->aFunc[0]),
2872 3,
2873 &pInfo->nFunc,
2874 &pInfo->nFuncAlloc,
2875 &i
2876 );
drh13449892005-09-07 21:22:45 +00002877 return i;
2878}
drh22827922000-06-06 17:27:05 +00002879
2880/*
drh7d10d5a2008-08-20 16:35:10 +00002881** This is the xExprCallback for a tree walker. It is used to
2882** implement sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates
drh626a8792005-01-17 22:08:19 +00002883** for additional information.
drh22827922000-06-06 17:27:05 +00002884*/
drh7d10d5a2008-08-20 16:35:10 +00002885static int analyzeAggregate(Walker *pWalker, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00002886 int i;
drh7d10d5a2008-08-20 16:35:10 +00002887 NameContext *pNC = pWalker->u.pNC;
danielk1977a58fdfb2005-02-08 07:50:40 +00002888 Parse *pParse = pNC->pParse;
2889 SrcList *pSrcList = pNC->pSrcList;
drh13449892005-09-07 21:22:45 +00002890 AggInfo *pAggInfo = pNC->pAggInfo;
drh22827922000-06-06 17:27:05 +00002891
drh22827922000-06-06 17:27:05 +00002892 switch( pExpr->op ){
drh89c69d02007-01-04 01:20:28 +00002893 case TK_AGG_COLUMN:
drh967e8b72000-06-21 13:59:10 +00002894 case TK_COLUMN: {
drh8b213892008-08-29 02:14:02 +00002895 testcase( pExpr->op==TK_AGG_COLUMN );
2896 testcase( pExpr->op==TK_COLUMN );
drh13449892005-09-07 21:22:45 +00002897 /* Check to see if the column is in one of the tables in the FROM
2898 ** clause of the aggregate query */
2899 if( pSrcList ){
2900 struct SrcList_item *pItem = pSrcList->a;
2901 for(i=0; i<pSrcList->nSrc; i++, pItem++){
2902 struct AggInfo_col *pCol;
2903 if( pExpr->iTable==pItem->iCursor ){
2904 /* If we reach this point, it means that pExpr refers to a table
2905 ** that is in the FROM clause of the aggregate query.
2906 **
2907 ** Make an entry for the column in pAggInfo->aCol[] if there
2908 ** is not an entry there already.
2909 */
drh7f906d62007-03-12 23:48:52 +00002910 int k;
drh13449892005-09-07 21:22:45 +00002911 pCol = pAggInfo->aCol;
drh7f906d62007-03-12 23:48:52 +00002912 for(k=0; k<pAggInfo->nColumn; k++, pCol++){
drh13449892005-09-07 21:22:45 +00002913 if( pCol->iTable==pExpr->iTable &&
2914 pCol->iColumn==pExpr->iColumn ){
2915 break;
2916 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002917 }
danielk19771e536952007-08-16 10:09:01 +00002918 if( (k>=pAggInfo->nColumn)
2919 && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0
2920 ){
drh7f906d62007-03-12 23:48:52 +00002921 pCol = &pAggInfo->aCol[k];
danielk19770817d0d2007-02-14 09:19:36 +00002922 pCol->pTab = pExpr->pTab;
drh13449892005-09-07 21:22:45 +00002923 pCol->iTable = pExpr->iTable;
2924 pCol->iColumn = pExpr->iColumn;
drh0a07c102008-01-03 18:03:08 +00002925 pCol->iMem = ++pParse->nMem;
drh13449892005-09-07 21:22:45 +00002926 pCol->iSorterColumn = -1;
drh5774b802005-09-07 22:48:16 +00002927 pCol->pExpr = pExpr;
drh13449892005-09-07 21:22:45 +00002928 if( pAggInfo->pGroupBy ){
2929 int j, n;
2930 ExprList *pGB = pAggInfo->pGroupBy;
2931 struct ExprList_item *pTerm = pGB->a;
2932 n = pGB->nExpr;
2933 for(j=0; j<n; j++, pTerm++){
2934 Expr *pE = pTerm->pExpr;
2935 if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable &&
2936 pE->iColumn==pExpr->iColumn ){
2937 pCol->iSorterColumn = j;
2938 break;
2939 }
2940 }
2941 }
2942 if( pCol->iSorterColumn<0 ){
2943 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
2944 }
2945 }
2946 /* There is now an entry for pExpr in pAggInfo->aCol[] (either
2947 ** because it was there before or because we just created it).
2948 ** Convert the pExpr to be a TK_AGG_COLUMN referring to that
2949 ** pAggInfo->aCol[] entry.
2950 */
2951 pExpr->pAggInfo = pAggInfo;
2952 pExpr->op = TK_AGG_COLUMN;
drh7f906d62007-03-12 23:48:52 +00002953 pExpr->iAgg = k;
drh13449892005-09-07 21:22:45 +00002954 break;
2955 } /* endif pExpr->iTable==pItem->iCursor */
2956 } /* end loop over pSrcList */
drh22827922000-06-06 17:27:05 +00002957 }
drh7d10d5a2008-08-20 16:35:10 +00002958 return WRC_Prune;
drh22827922000-06-06 17:27:05 +00002959 }
2960 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00002961 /* The pNC->nDepth==0 test causes aggregate functions in subqueries
2962 ** to be ignored */
danielk1977a58fdfb2005-02-08 07:50:40 +00002963 if( pNC->nDepth==0 ){
drh13449892005-09-07 21:22:45 +00002964 /* Check to see if pExpr is a duplicate of another aggregate
2965 ** function that is already in the pAggInfo structure
2966 */
2967 struct AggInfo_func *pItem = pAggInfo->aFunc;
2968 for(i=0; i<pAggInfo->nFunc; i++, pItem++){
2969 if( sqlite3ExprCompare(pItem->pExpr, pExpr) ){
danielk1977a58fdfb2005-02-08 07:50:40 +00002970 break;
2971 }
drh22827922000-06-06 17:27:05 +00002972 }
drh13449892005-09-07 21:22:45 +00002973 if( i>=pAggInfo->nFunc ){
2974 /* pExpr is original. Make a new entry in pAggInfo->aFunc[]
2975 */
danielk197714db2662006-01-09 16:12:04 +00002976 u8 enc = ENC(pParse->db);
danielk19771e536952007-08-16 10:09:01 +00002977 i = addAggInfoFunc(pParse->db, pAggInfo);
drh13449892005-09-07 21:22:45 +00002978 if( i>=0 ){
2979 pItem = &pAggInfo->aFunc[i];
2980 pItem->pExpr = pExpr;
drh0a07c102008-01-03 18:03:08 +00002981 pItem->iMem = ++pParse->nMem;
drh13449892005-09-07 21:22:45 +00002982 pItem->pFunc = sqlite3FindFunction(pParse->db,
drh2646da72005-12-09 20:02:05 +00002983 (char*)pExpr->token.z, pExpr->token.n,
drh13449892005-09-07 21:22:45 +00002984 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
drhfd357972005-09-09 01:33:19 +00002985 if( pExpr->flags & EP_Distinct ){
2986 pItem->iDistinct = pParse->nTab++;
2987 }else{
2988 pItem->iDistinct = -1;
2989 }
drh13449892005-09-07 21:22:45 +00002990 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002991 }
drh13449892005-09-07 21:22:45 +00002992 /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
2993 */
danielk1977a58fdfb2005-02-08 07:50:40 +00002994 pExpr->iAgg = i;
drh13449892005-09-07 21:22:45 +00002995 pExpr->pAggInfo = pAggInfo;
drh7d10d5a2008-08-20 16:35:10 +00002996 return WRC_Prune;
drh22827922000-06-06 17:27:05 +00002997 }
drh22827922000-06-06 17:27:05 +00002998 }
2999 }
drh7d10d5a2008-08-20 16:35:10 +00003000 return WRC_Continue;
3001}
3002static int analyzeAggregatesInSelect(Walker *pWalker, Select *pSelect){
3003 NameContext *pNC = pWalker->u.pNC;
3004 if( pNC->nDepth==0 ){
danielk1977a58fdfb2005-02-08 07:50:40 +00003005 pNC->nDepth++;
drh7d10d5a2008-08-20 16:35:10 +00003006 sqlite3WalkSelect(pWalker, pSelect);
danielk1977a58fdfb2005-02-08 07:50:40 +00003007 pNC->nDepth--;
drh7d10d5a2008-08-20 16:35:10 +00003008 return WRC_Prune;
3009 }else{
3010 return WRC_Continue;
danielk1977a58fdfb2005-02-08 07:50:40 +00003011 }
drh626a8792005-01-17 22:08:19 +00003012}
3013
3014/*
3015** Analyze the given expression looking for aggregate functions and
3016** for variables that need to be added to the pParse->aAgg[] array.
3017** Make additional entries to the pParse->aAgg[] array as necessary.
3018**
3019** This routine should only be called after the expression has been
drh7d10d5a2008-08-20 16:35:10 +00003020** analyzed by sqlite3ResolveExprNames().
drh626a8792005-01-17 22:08:19 +00003021*/
drhd2b3e232008-01-23 14:51:49 +00003022void sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
drh7d10d5a2008-08-20 16:35:10 +00003023 Walker w;
3024 w.xExprCallback = analyzeAggregate;
3025 w.xSelectCallback = analyzeAggregatesInSelect;
3026 w.u.pNC = pNC;
3027 sqlite3WalkExpr(&w, pExpr);
drh22827922000-06-06 17:27:05 +00003028}
drh5d9a4af2005-08-30 00:54:01 +00003029
3030/*
3031** Call sqlite3ExprAnalyzeAggregates() for every expression in an
3032** expression list. Return the number of errors.
3033**
3034** If an error is found, the analysis is cut short.
3035*/
drhd2b3e232008-01-23 14:51:49 +00003036void sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){
drh5d9a4af2005-08-30 00:54:01 +00003037 struct ExprList_item *pItem;
3038 int i;
drh5d9a4af2005-08-30 00:54:01 +00003039 if( pList ){
drhd2b3e232008-01-23 14:51:49 +00003040 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
3041 sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
drh5d9a4af2005-08-30 00:54:01 +00003042 }
3043 }
drh5d9a4af2005-08-30 00:54:01 +00003044}
drh892d3172008-01-10 03:46:36 +00003045
3046/*
3047** Allocate or deallocate temporary use registers during code generation.
3048*/
3049int sqlite3GetTempReg(Parse *pParse){
drhe55cbd72008-03-31 23:48:03 +00003050 if( pParse->nTempReg==0 ){
drh892d3172008-01-10 03:46:36 +00003051 return ++pParse->nMem;
3052 }
danielk19772f425f62008-07-04 09:41:39 +00003053 return pParse->aTempReg[--pParse->nTempReg];
drh892d3172008-01-10 03:46:36 +00003054}
3055void sqlite3ReleaseTempReg(Parse *pParse, int iReg){
drh2dcef112008-01-12 19:03:48 +00003056 if( iReg && pParse->nTempReg<ArraySize(pParse->aTempReg) ){
drhd1fa7bc2009-01-10 13:24:50 +00003057 sqlite3ExprWritableRegister(pParse, iReg);
drh892d3172008-01-10 03:46:36 +00003058 pParse->aTempReg[pParse->nTempReg++] = iReg;
3059 }
3060}
3061
3062/*
3063** Allocate or deallocate a block of nReg consecutive registers
3064*/
3065int sqlite3GetTempRange(Parse *pParse, int nReg){
drhe55cbd72008-03-31 23:48:03 +00003066 int i, n;
3067 i = pParse->iRangeReg;
3068 n = pParse->nRangeReg;
3069 if( nReg<=n && !usedAsColumnCache(pParse, i, i+n-1) ){
drh892d3172008-01-10 03:46:36 +00003070 pParse->iRangeReg += nReg;
3071 pParse->nRangeReg -= nReg;
3072 }else{
3073 i = pParse->nMem+1;
3074 pParse->nMem += nReg;
3075 }
3076 return i;
3077}
3078void sqlite3ReleaseTempRange(Parse *pParse, int iReg, int nReg){
3079 if( nReg>pParse->nRangeReg ){
3080 pParse->nRangeReg = nReg;
3081 pParse->iRangeReg = iReg;
3082 }
3083}