blob: 52f7cdb7de9b3623210c6fe704784ff41b8681e9 [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**
drh10fe8402008-10-11 16:47:35 +000015** $Id: expr.c,v 1.399 2008/10/11 16:47:36 drh Exp $
drhcce7d172000-05-31 15:34:51 +000016*/
17#include "sqliteInt.h"
drh04738cb2002-06-02 18:19:00 +000018#include <ctype.h>
drha2e00042002-01-22 03:13:42 +000019
danielk1977e014a832004-05-17 10:48:57 +000020/*
21** Return the 'affinity' of the expression pExpr if any.
22**
23** If pExpr is a column, a reference to a column via an 'AS' alias,
24** or a sub-select with a column as the return value, then the
25** affinity of that column is returned. Otherwise, 0x00 is returned,
26** indicating no affinity for the expression.
27**
28** i.e. the WHERE clause expresssions in the following statements all
29** have an affinity:
30**
31** CREATE TABLE t1(a);
32** SELECT * FROM t1 WHERE a;
33** SELECT a AS b FROM t1 WHERE b;
34** SELECT * FROM t1 WHERE (select a from t1);
35*/
danielk1977bf3b7212004-05-18 10:06:24 +000036char sqlite3ExprAffinity(Expr *pExpr){
drh487e2622005-06-25 18:42:14 +000037 int op = pExpr->op;
drh487e2622005-06-25 18:42:14 +000038 if( op==TK_SELECT ){
danielk1977bf3b7212004-05-18 10:06:24 +000039 return sqlite3ExprAffinity(pExpr->pSelect->pEList->a[0].pExpr);
danielk1977a37cdde2004-05-16 11:15:36 +000040 }
drh487e2622005-06-25 18:42:14 +000041#ifndef SQLITE_OMIT_CAST
42 if( op==TK_CAST ){
drh8a512562005-11-14 22:29:05 +000043 return sqlite3AffinityType(&pExpr->token);
drh487e2622005-06-25 18:42:14 +000044 }
45#endif
drh7d10d5a2008-08-20 16:35:10 +000046 if( (op==TK_COLUMN || op==TK_REGISTER) && pExpr->pTab!=0 ){
47 /* op==TK_REGISTER && pExpr->pTab!=0 happens when pExpr was originally
48 ** a TK_COLUMN but was previously evaluated and cached in a register */
49 int j = pExpr->iColumn;
50 if( j<0 ) return SQLITE_AFF_INTEGER;
51 assert( pExpr->pTab && j<pExpr->pTab->nCol );
52 return pExpr->pTab->aCol[j].affinity;
53 }
danielk1977a37cdde2004-05-16 11:15:36 +000054 return pExpr->affinity;
55}
56
drh53db1452004-05-20 13:54:53 +000057/*
drh8b4c40d2007-02-01 23:02:45 +000058** Set the collating sequence for expression pExpr to be the collating
59** sequence named by pToken. Return a pointer to the revised expression.
drha34001c2007-02-02 12:44:37 +000060** The collating sequence is marked as "explicit" using the EP_ExpCollate
61** flag. An explicit collating sequence will override implicit
62** collating sequences.
drh8b4c40d2007-02-01 23:02:45 +000063*/
drh7d10d5a2008-08-20 16:35:10 +000064Expr *sqlite3ExprSetColl(Parse *pParse, Expr *pExpr, Token *pCollName){
danielk197739002502007-11-12 09:50:26 +000065 char *zColl = 0; /* Dequoted name of collation sequence */
drh8b4c40d2007-02-01 23:02:45 +000066 CollSeq *pColl;
drh633e6d52008-07-28 19:34:53 +000067 sqlite3 *db = pParse->db;
drh7d10d5a2008-08-20 16:35:10 +000068 zColl = sqlite3NameFromToken(db, pCollName);
danielk197739002502007-11-12 09:50:26 +000069 if( pExpr && zColl ){
70 pColl = sqlite3LocateCollSeq(pParse, zColl, -1);
71 if( pColl ){
72 pExpr->pColl = pColl;
73 pExpr->flags |= EP_ExpCollate;
74 }
drh8b4c40d2007-02-01 23:02:45 +000075 }
drh633e6d52008-07-28 19:34:53 +000076 sqlite3DbFree(db, zColl);
drh8b4c40d2007-02-01 23:02:45 +000077 return pExpr;
78}
79
80/*
danielk19770202b292004-06-09 09:55:16 +000081** Return the default collation sequence for the expression pExpr. If
82** there is no default collation type, return 0.
83*/
danielk19777cedc8d2004-06-10 10:50:08 +000084CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
85 CollSeq *pColl = 0;
drh7d10d5a2008-08-20 16:35:10 +000086 Expr *p = pExpr;
87 while( p ){
drh7e09fe02007-06-20 16:13:23 +000088 int op;
drh7d10d5a2008-08-20 16:35:10 +000089 pColl = p->pColl;
90 if( pColl ) break;
91 op = p->op;
92 if( (op==TK_COLUMN || op==TK_REGISTER) && p->pTab!=0 ){
93 /* op==TK_REGISTER && p->pTab!=0 happens when pExpr was originally
94 ** a TK_COLUMN but was previously evaluated and cached in a register */
95 const char *zColl;
96 int j = p->iColumn;
97 if( j>=0 ){
98 sqlite3 *db = pParse->db;
99 zColl = p->pTab->aCol[j].zColl;
100 pColl = sqlite3FindCollSeq(db, ENC(db), zColl, -1, 0);
101 pExpr->pColl = pColl;
102 }
103 break;
danielk19770202b292004-06-09 09:55:16 +0000104 }
drh7d10d5a2008-08-20 16:35:10 +0000105 if( op!=TK_CAST && op!=TK_UPLUS ){
106 break;
107 }
108 p = p->pLeft;
danielk19770202b292004-06-09 09:55:16 +0000109 }
danielk19777cedc8d2004-06-10 10:50:08 +0000110 if( sqlite3CheckCollSeq(pParse, pColl) ){
111 pColl = 0;
112 }
113 return pColl;
danielk19770202b292004-06-09 09:55:16 +0000114}
115
116/*
drh626a8792005-01-17 22:08:19 +0000117** pExpr is an operand of a comparison operator. aff2 is the
118** type affinity of the other operand. This routine returns the
drh53db1452004-05-20 13:54:53 +0000119** type affinity that should be used for the comparison operator.
120*/
danielk1977e014a832004-05-17 10:48:57 +0000121char sqlite3CompareAffinity(Expr *pExpr, char aff2){
danielk1977bf3b7212004-05-18 10:06:24 +0000122 char aff1 = sqlite3ExprAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +0000123 if( aff1 && aff2 ){
drh8df447f2005-11-01 15:48:24 +0000124 /* Both sides of the comparison are columns. If one has numeric
125 ** affinity, use that. Otherwise use no affinity.
danielk1977e014a832004-05-17 10:48:57 +0000126 */
drh8a512562005-11-14 22:29:05 +0000127 if( sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2) ){
danielk1977e014a832004-05-17 10:48:57 +0000128 return SQLITE_AFF_NUMERIC;
129 }else{
130 return SQLITE_AFF_NONE;
131 }
132 }else if( !aff1 && !aff2 ){
drh5f6a87b2004-07-19 00:39:45 +0000133 /* Neither side of the comparison is a column. Compare the
134 ** results directly.
danielk1977e014a832004-05-17 10:48:57 +0000135 */
drh5f6a87b2004-07-19 00:39:45 +0000136 return SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +0000137 }else{
138 /* One side is a column, the other is not. Use the columns affinity. */
drhfe05af82005-07-21 03:14:59 +0000139 assert( aff1==0 || aff2==0 );
danielk1977e014a832004-05-17 10:48:57 +0000140 return (aff1 + aff2);
141 }
142}
143
drh53db1452004-05-20 13:54:53 +0000144/*
145** pExpr is a comparison operator. Return the type affinity that should
146** be applied to both operands prior to doing the comparison.
147*/
danielk1977e014a832004-05-17 10:48:57 +0000148static char comparisonAffinity(Expr *pExpr){
149 char aff;
150 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
151 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
152 pExpr->op==TK_NE );
153 assert( pExpr->pLeft );
danielk1977bf3b7212004-05-18 10:06:24 +0000154 aff = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +0000155 if( pExpr->pRight ){
156 aff = sqlite3CompareAffinity(pExpr->pRight, aff);
157 }
158 else if( pExpr->pSelect ){
159 aff = sqlite3CompareAffinity(pExpr->pSelect->pEList->a[0].pExpr, aff);
160 }
161 else if( !aff ){
drhde087bd2007-02-23 03:00:44 +0000162 aff = SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +0000163 }
164 return aff;
165}
166
167/*
168** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
169** idx_affinity is the affinity of an indexed column. Return true
170** if the index with affinity idx_affinity may be used to implement
171** the comparison in pExpr.
172*/
173int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){
174 char aff = comparisonAffinity(pExpr);
drh8a512562005-11-14 22:29:05 +0000175 switch( aff ){
176 case SQLITE_AFF_NONE:
177 return 1;
178 case SQLITE_AFF_TEXT:
179 return idx_affinity==SQLITE_AFF_TEXT;
180 default:
181 return sqlite3IsNumericAffinity(idx_affinity);
182 }
danielk1977e014a832004-05-17 10:48:57 +0000183}
184
danielk1977a37cdde2004-05-16 11:15:36 +0000185/*
drh35573352008-01-08 23:54:25 +0000186** Return the P5 value that should be used for a binary comparison
danielk1977a37cdde2004-05-16 11:15:36 +0000187** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
danielk1977a37cdde2004-05-16 11:15:36 +0000188*/
drh35573352008-01-08 23:54:25 +0000189static u8 binaryCompareP5(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){
190 u8 aff = (char)sqlite3ExprAffinity(pExpr2);
191 aff = sqlite3CompareAffinity(pExpr1, aff) | jumpIfNull;
192 return aff;
danielk1977a37cdde2004-05-16 11:15:36 +0000193}
194
drha2e00042002-01-22 03:13:42 +0000195/*
danielk19770202b292004-06-09 09:55:16 +0000196** Return a pointer to the collation sequence that should be used by
197** a binary comparison operator comparing pLeft and pRight.
198**
199** If the left hand expression has a collating sequence type, then it is
200** used. Otherwise the collation sequence for the right hand expression
201** is used, or the default (BINARY) if neither expression has a collating
202** type.
danielk1977bcbb04e2007-05-29 12:11:29 +0000203**
204** Argument pRight (but not pLeft) may be a null pointer. In this case,
205** it is not considered.
danielk19770202b292004-06-09 09:55:16 +0000206*/
drh0a0e1312007-08-07 17:04:59 +0000207CollSeq *sqlite3BinaryCompareCollSeq(
danielk1977bcbb04e2007-05-29 12:11:29 +0000208 Parse *pParse,
209 Expr *pLeft,
210 Expr *pRight
211){
drhec41dda2007-02-07 13:09:45 +0000212 CollSeq *pColl;
213 assert( pLeft );
drhec41dda2007-02-07 13:09:45 +0000214 if( pLeft->flags & EP_ExpCollate ){
215 assert( pLeft->pColl );
216 pColl = pLeft->pColl;
danielk1977bcbb04e2007-05-29 12:11:29 +0000217 }else if( pRight && pRight->flags & EP_ExpCollate ){
drhec41dda2007-02-07 13:09:45 +0000218 assert( pRight->pColl );
219 pColl = pRight->pColl;
220 }else{
221 pColl = sqlite3ExprCollSeq(pParse, pLeft);
222 if( !pColl ){
223 pColl = sqlite3ExprCollSeq(pParse, pRight);
224 }
danielk19770202b292004-06-09 09:55:16 +0000225 }
226 return pColl;
227}
228
229/*
drhda250ea2008-04-01 05:07:14 +0000230** Generate the operands for a comparison operation. Before
231** generating the code for each operand, set the EP_AnyAff
232** flag on the expression so that it will be able to used a
233** cached column value that has previously undergone an
234** affinity change.
235*/
236static void codeCompareOperands(
237 Parse *pParse, /* Parsing and code generating context */
238 Expr *pLeft, /* The left operand */
239 int *pRegLeft, /* Register where left operand is stored */
240 int *pFreeLeft, /* Free this register when done */
241 Expr *pRight, /* The right operand */
242 int *pRegRight, /* Register where right operand is stored */
243 int *pFreeRight /* Write temp register for right operand there */
244){
245 while( pLeft->op==TK_UPLUS ) pLeft = pLeft->pLeft;
246 pLeft->flags |= EP_AnyAff;
247 *pRegLeft = sqlite3ExprCodeTemp(pParse, pLeft, pFreeLeft);
248 while( pRight->op==TK_UPLUS ) pRight = pRight->pLeft;
249 pRight->flags |= EP_AnyAff;
250 *pRegRight = sqlite3ExprCodeTemp(pParse, pRight, pFreeRight);
251}
252
253/*
drhbe5c89a2004-07-26 00:31:09 +0000254** Generate code for a comparison operator.
255*/
256static int codeCompare(
257 Parse *pParse, /* The parsing (and code generating) context */
258 Expr *pLeft, /* The left operand */
259 Expr *pRight, /* The right operand */
260 int opcode, /* The comparison opcode */
drh35573352008-01-08 23:54:25 +0000261 int in1, int in2, /* Register holding operands */
drhbe5c89a2004-07-26 00:31:09 +0000262 int dest, /* Jump here if true. */
263 int jumpIfNull /* If true, jump if either operand is NULL */
264){
drh35573352008-01-08 23:54:25 +0000265 int p5;
266 int addr;
267 CollSeq *p4;
268
269 p4 = sqlite3BinaryCompareCollSeq(pParse, pLeft, pRight);
270 p5 = binaryCompareP5(pLeft, pRight, jumpIfNull);
271 addr = sqlite3VdbeAddOp4(pParse->pVdbe, opcode, in2, dest, in1,
272 (void*)p4, P4_COLLSEQ);
273 sqlite3VdbeChangeP5(pParse->pVdbe, p5);
drhe49b1462008-07-09 01:39:44 +0000274 if( (p5 & SQLITE_AFF_MASK)!=SQLITE_AFF_NONE ){
drhda250ea2008-04-01 05:07:14 +0000275 sqlite3ExprCacheAffinityChange(pParse, in1, 1);
276 sqlite3ExprCacheAffinityChange(pParse, in2, 1);
drh2f7794c2008-04-01 03:27:39 +0000277 }
drh35573352008-01-08 23:54:25 +0000278 return addr;
drhbe5c89a2004-07-26 00:31:09 +0000279}
280
danielk19774b5255a2008-06-05 16:47:39 +0000281#if SQLITE_MAX_EXPR_DEPTH>0
282/*
283** Check that argument nHeight is less than or equal to the maximum
284** expression depth allowed. If it is not, leave an error message in
285** pParse.
286*/
drh7d10d5a2008-08-20 16:35:10 +0000287int sqlite3ExprCheckHeight(Parse *pParse, int nHeight){
danielk19774b5255a2008-06-05 16:47:39 +0000288 int rc = SQLITE_OK;
289 int mxHeight = pParse->db->aLimit[SQLITE_LIMIT_EXPR_DEPTH];
290 if( nHeight>mxHeight ){
291 sqlite3ErrorMsg(pParse,
292 "Expression tree is too large (maximum depth %d)", mxHeight
293 );
294 rc = SQLITE_ERROR;
295 }
296 return rc;
297}
298
299/* The following three functions, heightOfExpr(), heightOfExprList()
300** and heightOfSelect(), are used to determine the maximum height
301** of any expression tree referenced by the structure passed as the
302** first argument.
303**
304** If this maximum height is greater than the current value pointed
305** to by pnHeight, the second parameter, then set *pnHeight to that
306** value.
307*/
308static void heightOfExpr(Expr *p, int *pnHeight){
309 if( p ){
310 if( p->nHeight>*pnHeight ){
311 *pnHeight = p->nHeight;
312 }
313 }
314}
315static void heightOfExprList(ExprList *p, int *pnHeight){
316 if( p ){
317 int i;
318 for(i=0; i<p->nExpr; i++){
319 heightOfExpr(p->a[i].pExpr, pnHeight);
320 }
321 }
322}
323static void heightOfSelect(Select *p, int *pnHeight){
324 if( p ){
325 heightOfExpr(p->pWhere, pnHeight);
326 heightOfExpr(p->pHaving, pnHeight);
327 heightOfExpr(p->pLimit, pnHeight);
328 heightOfExpr(p->pOffset, pnHeight);
329 heightOfExprList(p->pEList, pnHeight);
330 heightOfExprList(p->pGroupBy, pnHeight);
331 heightOfExprList(p->pOrderBy, pnHeight);
332 heightOfSelect(p->pPrior, pnHeight);
333 }
334}
335
336/*
337** Set the Expr.nHeight variable in the structure passed as an
338** argument. An expression with no children, Expr.pList or
339** Expr.pSelect member has a height of 1. Any other expression
340** has a height equal to the maximum height of any other
341** referenced Expr plus one.
342*/
343static void exprSetHeight(Expr *p){
344 int nHeight = 0;
345 heightOfExpr(p->pLeft, &nHeight);
346 heightOfExpr(p->pRight, &nHeight);
347 heightOfExprList(p->pList, &nHeight);
348 heightOfSelect(p->pSelect, &nHeight);
349 p->nHeight = nHeight + 1;
350}
351
352/*
353** Set the Expr.nHeight variable using the exprSetHeight() function. If
354** the height is greater than the maximum allowed expression depth,
355** leave an error in pParse.
356*/
357void sqlite3ExprSetHeight(Parse *pParse, Expr *p){
358 exprSetHeight(p);
drh7d10d5a2008-08-20 16:35:10 +0000359 sqlite3ExprCheckHeight(pParse, p->nHeight);
danielk19774b5255a2008-06-05 16:47:39 +0000360}
361
362/*
363** Return the maximum height of any expression tree referenced
364** by the select statement passed as an argument.
365*/
366int sqlite3SelectExprHeight(Select *p){
367 int nHeight = 0;
368 heightOfSelect(p, &nHeight);
369 return nHeight;
370}
371#else
danielk19774b5255a2008-06-05 16:47:39 +0000372 #define exprSetHeight(y)
373#endif /* SQLITE_MAX_EXPR_DEPTH>0 */
374
drhbe5c89a2004-07-26 00:31:09 +0000375/*
drha76b5df2002-02-23 02:32:10 +0000376** Construct a new expression node and return a pointer to it. Memory
drh17435752007-08-16 04:30:38 +0000377** for this node is obtained from sqlite3_malloc(). The calling function
drha76b5df2002-02-23 02:32:10 +0000378** is responsible for making sure the node eventually gets freed.
379*/
drh17435752007-08-16 04:30:38 +0000380Expr *sqlite3Expr(
danielk1977a1644fd2007-08-29 12:31:25 +0000381 sqlite3 *db, /* Handle for sqlite3DbMallocZero() (may be null) */
drh17435752007-08-16 04:30:38 +0000382 int op, /* Expression opcode */
383 Expr *pLeft, /* Left operand */
384 Expr *pRight, /* Right operand */
385 const Token *pToken /* Argument token */
386){
drha76b5df2002-02-23 02:32:10 +0000387 Expr *pNew;
drh26e4a8b2008-05-01 17:16:52 +0000388 pNew = sqlite3DbMallocZero(db, sizeof(Expr));
drha76b5df2002-02-23 02:32:10 +0000389 if( pNew==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000390 /* When malloc fails, delete pLeft and pRight. Expressions passed to
391 ** this function must always be allocated with sqlite3Expr() for this
392 ** reason.
393 */
drh633e6d52008-07-28 19:34:53 +0000394 sqlite3ExprDelete(db, pLeft);
395 sqlite3ExprDelete(db, pRight);
drha76b5df2002-02-23 02:32:10 +0000396 return 0;
397 }
398 pNew->op = op;
399 pNew->pLeft = pLeft;
400 pNew->pRight = pRight;
danielk1977a58fdfb2005-02-08 07:50:40 +0000401 pNew->iAgg = -1;
drhe49b1462008-07-09 01:39:44 +0000402 pNew->span.z = (u8*)"";
drha76b5df2002-02-23 02:32:10 +0000403 if( pToken ){
drh4b59ab52002-08-24 18:24:51 +0000404 assert( pToken->dyn==0 );
drh145716b2004-09-24 12:24:06 +0000405 pNew->span = pNew->token = *pToken;
drha34001c2007-02-02 12:44:37 +0000406 }else if( pLeft ){
407 if( pRight ){
drhe49b1462008-07-09 01:39:44 +0000408 if( pRight->span.dyn==0 && pLeft->span.dyn==0 ){
409 sqlite3ExprSpan(pNew, &pLeft->span, &pRight->span);
410 }
drh5ffb3ac2007-04-18 17:07:57 +0000411 if( pRight->flags & EP_ExpCollate ){
drha34001c2007-02-02 12:44:37 +0000412 pNew->flags |= EP_ExpCollate;
413 pNew->pColl = pRight->pColl;
414 }
415 }
drh5ffb3ac2007-04-18 17:07:57 +0000416 if( pLeft->flags & EP_ExpCollate ){
drha34001c2007-02-02 12:44:37 +0000417 pNew->flags |= EP_ExpCollate;
418 pNew->pColl = pLeft->pColl;
419 }
drha76b5df2002-02-23 02:32:10 +0000420 }
danielk1977fc976062007-05-10 10:46:56 +0000421
danielk19774b5255a2008-06-05 16:47:39 +0000422 exprSetHeight(pNew);
drha76b5df2002-02-23 02:32:10 +0000423 return pNew;
424}
425
426/*
drh17435752007-08-16 04:30:38 +0000427** Works like sqlite3Expr() except that it takes an extra Parse*
428** argument and notifies the associated connection object if malloc fails.
drh206f3d92006-07-11 13:15:08 +0000429*/
drh17435752007-08-16 04:30:38 +0000430Expr *sqlite3PExpr(
431 Parse *pParse, /* Parsing context */
432 int op, /* Expression opcode */
433 Expr *pLeft, /* Left operand */
434 Expr *pRight, /* Right operand */
435 const Token *pToken /* Argument token */
436){
danielk19774b5255a2008-06-05 16:47:39 +0000437 Expr *p = sqlite3Expr(pParse->db, op, pLeft, pRight, pToken);
438 if( p ){
drh7d10d5a2008-08-20 16:35:10 +0000439 sqlite3ExprCheckHeight(pParse, p->nHeight);
danielk19774b5255a2008-06-05 16:47:39 +0000440 }
441 return p;
drh206f3d92006-07-11 13:15:08 +0000442}
443
444/*
drh4e0cff62004-11-05 05:10:28 +0000445** When doing a nested parse, you can include terms in an expression
drhb7654112008-01-12 12:48:07 +0000446** that look like this: #1 #2 ... These terms refer to registers
447** in the virtual machine. #N is the N-th register.
drh4e0cff62004-11-05 05:10:28 +0000448**
449** This routine is called by the parser to deal with on of those terms.
450** It immediately generates code to store the value in a memory location.
451** The returns an expression that will code to extract the value from
452** that memory location as needed.
453*/
454Expr *sqlite3RegisterExpr(Parse *pParse, Token *pToken){
455 Vdbe *v = pParse->pVdbe;
456 Expr *p;
drh4e0cff62004-11-05 05:10:28 +0000457 if( pParse->nested==0 ){
458 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", pToken);
danielk1977a1644fd2007-08-29 12:31:25 +0000459 return sqlite3PExpr(pParse, TK_NULL, 0, 0, 0);
drh4e0cff62004-11-05 05:10:28 +0000460 }
drhbb7ac002005-08-12 22:58:53 +0000461 if( v==0 ) return 0;
danielk1977a1644fd2007-08-29 12:31:25 +0000462 p = sqlite3PExpr(pParse, TK_REGISTER, 0, 0, pToken);
drh73c42a12004-11-20 18:13:10 +0000463 if( p==0 ){
464 return 0; /* Malloc failed */
465 }
drhb7654112008-01-12 12:48:07 +0000466 p->iTable = atoi((char*)&pToken->z[1]);
drh4e0cff62004-11-05 05:10:28 +0000467 return p;
468}
469
470/*
drh91bb0ee2004-09-01 03:06:34 +0000471** Join two expressions using an AND operator. If either expression is
472** NULL, then just return the other expression.
473*/
danielk19771e536952007-08-16 10:09:01 +0000474Expr *sqlite3ExprAnd(sqlite3 *db, Expr *pLeft, Expr *pRight){
drh91bb0ee2004-09-01 03:06:34 +0000475 if( pLeft==0 ){
476 return pRight;
477 }else if( pRight==0 ){
478 return pLeft;
479 }else{
danielk1977880c15b2007-09-01 18:24:55 +0000480 return sqlite3Expr(db, TK_AND, pLeft, pRight, 0);
drh91bb0ee2004-09-01 03:06:34 +0000481 }
482}
483
484/*
drh6977fea2002-10-22 23:38:04 +0000485** Set the Expr.span field of the given expression to span all
drhe49b1462008-07-09 01:39:44 +0000486** text between the two given tokens. Both tokens must be pointing
487** at the same string.
drha76b5df2002-02-23 02:32:10 +0000488*/
danielk19774adee202004-05-08 08:23:19 +0000489void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
drh4efc4752004-01-16 15:55:37 +0000490 assert( pRight!=0 );
491 assert( pLeft!=0 );
drhe54a62a2008-07-18 17:03:52 +0000492 if( pExpr ){
drhe49b1462008-07-09 01:39:44 +0000493 pExpr->span.z = pLeft->z;
494 pExpr->span.n = pRight->n + (pRight->z - pLeft->z);
drha76b5df2002-02-23 02:32:10 +0000495 }
496}
497
498/*
499** Construct a new expression node for a function with multiple
500** arguments.
501*/
drh17435752007-08-16 04:30:38 +0000502Expr *sqlite3ExprFunction(Parse *pParse, ExprList *pList, Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000503 Expr *pNew;
drh633e6d52008-07-28 19:34:53 +0000504 sqlite3 *db = pParse->db;
danielk19774b202ae2006-01-23 05:50:58 +0000505 assert( pToken );
drh633e6d52008-07-28 19:34:53 +0000506 pNew = sqlite3DbMallocZero(db, sizeof(Expr) );
drha76b5df2002-02-23 02:32:10 +0000507 if( pNew==0 ){
drh633e6d52008-07-28 19:34:53 +0000508 sqlite3ExprListDelete(db, pList); /* Avoid leaking memory when malloc fails */
drha76b5df2002-02-23 02:32:10 +0000509 return 0;
510 }
511 pNew->op = TK_FUNCTION;
512 pNew->pList = pList;
danielk19774b202ae2006-01-23 05:50:58 +0000513 assert( pToken->dyn==0 );
514 pNew->token = *pToken;
drh6977fea2002-10-22 23:38:04 +0000515 pNew->span = pNew->token;
danielk1977fc976062007-05-10 10:46:56 +0000516
danielk19774b5255a2008-06-05 16:47:39 +0000517 sqlite3ExprSetHeight(pParse, pNew);
drha76b5df2002-02-23 02:32:10 +0000518 return pNew;
519}
520
521/*
drhfa6bc002004-09-07 16:19:52 +0000522** Assign a variable number to an expression that encodes a wildcard
523** in the original SQL statement.
524**
525** Wildcards consisting of a single "?" are assigned the next sequential
526** variable number.
527**
528** Wildcards of the form "?nnn" are assigned the number "nnn". We make
529** sure "nnn" is not too be to avoid a denial of service attack when
530** the SQL statement comes from an external source.
531**
532** Wildcards of the form ":aaa" or "$aaa" are assigned the same number
533** as the previous instance of the same wildcard. Or if this is the first
534** instance of the wildcard, the next sequenial variable number is
535** assigned.
536*/
537void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){
538 Token *pToken;
drh17435752007-08-16 04:30:38 +0000539 sqlite3 *db = pParse->db;
540
drhfa6bc002004-09-07 16:19:52 +0000541 if( pExpr==0 ) return;
542 pToken = &pExpr->token;
543 assert( pToken->n>=1 );
544 assert( pToken->z!=0 );
545 assert( pToken->z[0]!=0 );
546 if( pToken->n==1 ){
547 /* Wildcard of the form "?". Assign the next variable number */
548 pExpr->iTable = ++pParse->nVar;
549 }else if( pToken->z[0]=='?' ){
550 /* Wildcard of the form "?nnn". Convert "nnn" to an integer and
551 ** use it as the variable number */
552 int i;
drh2646da72005-12-09 20:02:05 +0000553 pExpr->iTable = i = atoi((char*)&pToken->z[1]);
drhc5499be2008-04-01 15:06:33 +0000554 testcase( i==0 );
555 testcase( i==1 );
556 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 );
557 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] );
drhbb4957f2008-03-20 14:03:29 +0000558 if( i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
drhfa6bc002004-09-07 16:19:52 +0000559 sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
drhbb4957f2008-03-20 14:03:29 +0000560 db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]);
drhfa6bc002004-09-07 16:19:52 +0000561 }
562 if( i>pParse->nVar ){
563 pParse->nVar = i;
564 }
565 }else{
566 /* Wildcards of the form ":aaa" or "$aaa". Reuse the same variable
567 ** number as the prior appearance of the same name, or if the name
568 ** has never appeared before, reuse the same variable number
569 */
570 int i, n;
571 n = pToken->n;
572 for(i=0; i<pParse->nVarExpr; i++){
573 Expr *pE;
574 if( (pE = pParse->apVarExpr[i])!=0
575 && pE->token.n==n
576 && memcmp(pE->token.z, pToken->z, n)==0 ){
577 pExpr->iTable = pE->iTable;
578 break;
579 }
580 }
581 if( i>=pParse->nVarExpr ){
582 pExpr->iTable = ++pParse->nVar;
583 if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){
584 pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10;
drh17435752007-08-16 04:30:38 +0000585 pParse->apVarExpr =
586 sqlite3DbReallocOrFree(
587 db,
588 pParse->apVarExpr,
589 pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0])
590 );
drhfa6bc002004-09-07 16:19:52 +0000591 }
drh17435752007-08-16 04:30:38 +0000592 if( !db->mallocFailed ){
drhfa6bc002004-09-07 16:19:52 +0000593 assert( pParse->apVarExpr!=0 );
594 pParse->apVarExpr[pParse->nVarExpr++] = pExpr;
595 }
596 }
597 }
drhbb4957f2008-03-20 14:03:29 +0000598 if( !pParse->nErr && pParse->nVar>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
danielk1977832b2662007-05-09 11:37:22 +0000599 sqlite3ErrorMsg(pParse, "too many SQL variables");
600 }
drhfa6bc002004-09-07 16:19:52 +0000601}
602
603/*
drh10fe8402008-10-11 16:47:35 +0000604** Clear an expression structure without deleting the structure itself.
605** Substructure is deleted.
drha2e00042002-01-22 03:13:42 +0000606*/
drh10fe8402008-10-11 16:47:35 +0000607void sqlite3ExprClear(sqlite3 *db, Expr *p){
drh633e6d52008-07-28 19:34:53 +0000608 if( p->span.dyn ) sqlite3DbFree(db, (char*)p->span.z);
609 if( p->token.dyn ) sqlite3DbFree(db, (char*)p->token.z);
610 sqlite3ExprDelete(db, p->pLeft);
611 sqlite3ExprDelete(db, p->pRight);
612 sqlite3ExprListDelete(db, p->pList);
613 sqlite3SelectDelete(db, p->pSelect);
drh10fe8402008-10-11 16:47:35 +0000614}
615
616/*
617** Recursively delete an expression tree.
618*/
619void sqlite3ExprDelete(sqlite3 *db, Expr *p){
620 if( p==0 ) return;
621 sqlite3ExprClear(db, p);
drh633e6d52008-07-28 19:34:53 +0000622 sqlite3DbFree(db, p);
drha2e00042002-01-22 03:13:42 +0000623}
624
drhd2687b72005-08-12 22:56:09 +0000625/*
626** The Expr.token field might be a string literal that is quoted.
627** If so, remove the quotation marks.
628*/
drh17435752007-08-16 04:30:38 +0000629void sqlite3DequoteExpr(sqlite3 *db, Expr *p){
drhd2687b72005-08-12 22:56:09 +0000630 if( ExprHasAnyProperty(p, EP_Dequoted) ){
631 return;
632 }
633 ExprSetProperty(p, EP_Dequoted);
634 if( p->token.dyn==0 ){
drh17435752007-08-16 04:30:38 +0000635 sqlite3TokenCopy(db, &p->token, &p->token);
drhd2687b72005-08-12 22:56:09 +0000636 }
637 sqlite3Dequote((char*)p->token.z);
638}
639
drha76b5df2002-02-23 02:32:10 +0000640/*
drhff78bd22002-02-27 01:47:11 +0000641** The following group of routines make deep copies of expressions,
642** expression lists, ID lists, and select statements. The copies can
643** be deleted (by being passed to their respective ...Delete() routines)
644** without effecting the originals.
645**
danielk19774adee202004-05-08 08:23:19 +0000646** The expression list, ID, and source lists return by sqlite3ExprListDup(),
647** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
drhad3cab52002-05-24 02:04:32 +0000648** by subsequent calls to sqlite*ListAppend() routines.
drhff78bd22002-02-27 01:47:11 +0000649**
drhad3cab52002-05-24 02:04:32 +0000650** Any tables that the SrcList might point to are not duplicated.
drhff78bd22002-02-27 01:47:11 +0000651*/
danielk19771e536952007-08-16 10:09:01 +0000652Expr *sqlite3ExprDup(sqlite3 *db, Expr *p){
drhff78bd22002-02-27 01:47:11 +0000653 Expr *pNew;
654 if( p==0 ) return 0;
drh17435752007-08-16 04:30:38 +0000655 pNew = sqlite3DbMallocRaw(db, sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000656 if( pNew==0 ) return 0;
drh3b167c72002-06-28 12:18:47 +0000657 memcpy(pNew, p, sizeof(*pNew));
drh6977fea2002-10-22 23:38:04 +0000658 if( p->token.z!=0 ){
drh17435752007-08-16 04:30:38 +0000659 pNew->token.z = (u8*)sqlite3DbStrNDup(db, (char*)p->token.z, p->token.n);
drh4b59ab52002-08-24 18:24:51 +0000660 pNew->token.dyn = 1;
661 }else{
drh4efc4752004-01-16 15:55:37 +0000662 assert( pNew->token.z==0 );
drh4b59ab52002-08-24 18:24:51 +0000663 }
drh6977fea2002-10-22 23:38:04 +0000664 pNew->span.z = 0;
drh17435752007-08-16 04:30:38 +0000665 pNew->pLeft = sqlite3ExprDup(db, p->pLeft);
666 pNew->pRight = sqlite3ExprDup(db, p->pRight);
667 pNew->pList = sqlite3ExprListDup(db, p->pList);
668 pNew->pSelect = sqlite3SelectDup(db, p->pSelect);
drhff78bd22002-02-27 01:47:11 +0000669 return pNew;
670}
drh17435752007-08-16 04:30:38 +0000671void sqlite3TokenCopy(sqlite3 *db, Token *pTo, Token *pFrom){
drh633e6d52008-07-28 19:34:53 +0000672 if( pTo->dyn ) sqlite3DbFree(db, (char*)pTo->z);
drh4b59ab52002-08-24 18:24:51 +0000673 if( pFrom->z ){
674 pTo->n = pFrom->n;
drh17435752007-08-16 04:30:38 +0000675 pTo->z = (u8*)sqlite3DbStrNDup(db, (char*)pFrom->z, pFrom->n);
drh4b59ab52002-08-24 18:24:51 +0000676 pTo->dyn = 1;
677 }else{
drh4b59ab52002-08-24 18:24:51 +0000678 pTo->z = 0;
drh4b59ab52002-08-24 18:24:51 +0000679 }
680}
drh17435752007-08-16 04:30:38 +0000681ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p){
drhff78bd22002-02-27 01:47:11 +0000682 ExprList *pNew;
drh145716b2004-09-24 12:24:06 +0000683 struct ExprList_item *pItem, *pOldItem;
drhff78bd22002-02-27 01:47:11 +0000684 int i;
685 if( p==0 ) return 0;
drh17435752007-08-16 04:30:38 +0000686 pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +0000687 if( pNew==0 ) return 0;
danielk197731dad9d2007-08-16 11:36:15 +0000688 pNew->iECursor = 0;
drh4305d102003-07-30 12:34:12 +0000689 pNew->nExpr = pNew->nAlloc = p->nExpr;
drh17435752007-08-16 04:30:38 +0000690 pNew->a = pItem = sqlite3DbMallocRaw(db, p->nExpr*sizeof(p->a[0]) );
danielk1977e0048402004-06-15 16:51:01 +0000691 if( pItem==0 ){
drh633e6d52008-07-28 19:34:53 +0000692 sqlite3DbFree(db, pNew);
danielk1977e0048402004-06-15 16:51:01 +0000693 return 0;
694 }
drh145716b2004-09-24 12:24:06 +0000695 pOldItem = p->a;
696 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
drh4b59ab52002-08-24 18:24:51 +0000697 Expr *pNewExpr, *pOldExpr;
drh17435752007-08-16 04:30:38 +0000698 pItem->pExpr = pNewExpr = sqlite3ExprDup(db, pOldExpr = pOldItem->pExpr);
drh6977fea2002-10-22 23:38:04 +0000699 if( pOldExpr->span.z!=0 && pNewExpr ){
700 /* Always make a copy of the span for top-level expressions in the
drh4b59ab52002-08-24 18:24:51 +0000701 ** expression list. The logic in SELECT processing that determines
702 ** the names of columns in the result set needs this information */
drh17435752007-08-16 04:30:38 +0000703 sqlite3TokenCopy(db, &pNewExpr->span, &pOldExpr->span);
drh4b59ab52002-08-24 18:24:51 +0000704 }
drh1f3e9052002-10-31 00:09:39 +0000705 assert( pNewExpr==0 || pNewExpr->span.z!=0
drh6f7adc82006-01-11 21:41:20 +0000706 || pOldExpr->span.z==0
drh17435752007-08-16 04:30:38 +0000707 || db->mallocFailed );
708 pItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
drh145716b2004-09-24 12:24:06 +0000709 pItem->sortOrder = pOldItem->sortOrder;
drh3e7bc9c2004-02-21 19:17:17 +0000710 pItem->done = 0;
drh7d10d5a2008-08-20 16:35:10 +0000711 pItem->iCol = pOldItem->iCol;
drh8b213892008-08-29 02:14:02 +0000712 pItem->iAlias = pOldItem->iAlias;
drhff78bd22002-02-27 01:47:11 +0000713 }
714 return pNew;
715}
danielk197793758c82005-01-21 08:13:14 +0000716
717/*
718** If cursors, triggers, views and subqueries are all omitted from
719** the build, then none of the following routines, except for
720** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes
721** called with a NULL argument.
722*/
danielk19776a67fe82005-02-04 04:07:16 +0000723#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \
724 || !defined(SQLITE_OMIT_SUBQUERY)
drh17435752007-08-16 04:30:38 +0000725SrcList *sqlite3SrcListDup(sqlite3 *db, SrcList *p){
drhad3cab52002-05-24 02:04:32 +0000726 SrcList *pNew;
727 int i;
drh113088e2003-03-20 01:16:58 +0000728 int nByte;
drhad3cab52002-05-24 02:04:32 +0000729 if( p==0 ) return 0;
drh113088e2003-03-20 01:16:58 +0000730 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
drh17435752007-08-16 04:30:38 +0000731 pNew = sqlite3DbMallocRaw(db, nByte );
drhad3cab52002-05-24 02:04:32 +0000732 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000733 pNew->nSrc = pNew->nAlloc = p->nSrc;
drhad3cab52002-05-24 02:04:32 +0000734 for(i=0; i<p->nSrc; i++){
drh4efc4752004-01-16 15:55:37 +0000735 struct SrcList_item *pNewItem = &pNew->a[i];
736 struct SrcList_item *pOldItem = &p->a[i];
drhed8a3bb2005-06-06 21:19:56 +0000737 Table *pTab;
drh17435752007-08-16 04:30:38 +0000738 pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase);
739 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
740 pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias);
drh4efc4752004-01-16 15:55:37 +0000741 pNewItem->jointype = pOldItem->jointype;
742 pNewItem->iCursor = pOldItem->iCursor;
danielk19771787cca2006-02-10 07:07:14 +0000743 pNewItem->isPopulated = pOldItem->isPopulated;
danielk197785574e32008-10-06 05:32:18 +0000744 pNewItem->zIndex = sqlite3DbStrDup(db, pOldItem->zIndex);
745 pNewItem->notIndexed = pOldItem->notIndexed;
746 pNewItem->pIndex = pOldItem->pIndex;
drhed8a3bb2005-06-06 21:19:56 +0000747 pTab = pNewItem->pTab = pOldItem->pTab;
748 if( pTab ){
749 pTab->nRef++;
danielk1977a1cb1832005-02-12 08:59:55 +0000750 }
drh17435752007-08-16 04:30:38 +0000751 pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect);
752 pNewItem->pOn = sqlite3ExprDup(db, pOldItem->pOn);
753 pNewItem->pUsing = sqlite3IdListDup(db, pOldItem->pUsing);
danielk19776c18b6e2005-01-30 09:17:58 +0000754 pNewItem->colUsed = pOldItem->colUsed;
drhad3cab52002-05-24 02:04:32 +0000755 }
756 return pNew;
757}
drh17435752007-08-16 04:30:38 +0000758IdList *sqlite3IdListDup(sqlite3 *db, IdList *p){
drhff78bd22002-02-27 01:47:11 +0000759 IdList *pNew;
760 int i;
761 if( p==0 ) return 0;
drh17435752007-08-16 04:30:38 +0000762 pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +0000763 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000764 pNew->nId = pNew->nAlloc = p->nId;
drh17435752007-08-16 04:30:38 +0000765 pNew->a = sqlite3DbMallocRaw(db, p->nId*sizeof(p->a[0]) );
danielk1977d5d56522005-03-16 12:15:20 +0000766 if( pNew->a==0 ){
drh633e6d52008-07-28 19:34:53 +0000767 sqlite3DbFree(db, pNew);
danielk1977d5d56522005-03-16 12:15:20 +0000768 return 0;
769 }
drhff78bd22002-02-27 01:47:11 +0000770 for(i=0; i<p->nId; i++){
drh4efc4752004-01-16 15:55:37 +0000771 struct IdList_item *pNewItem = &pNew->a[i];
772 struct IdList_item *pOldItem = &p->a[i];
drh17435752007-08-16 04:30:38 +0000773 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
drh4efc4752004-01-16 15:55:37 +0000774 pNewItem->idx = pOldItem->idx;
drhff78bd22002-02-27 01:47:11 +0000775 }
776 return pNew;
777}
drh17435752007-08-16 04:30:38 +0000778Select *sqlite3SelectDup(sqlite3 *db, Select *p){
drhff78bd22002-02-27 01:47:11 +0000779 Select *pNew;
780 if( p==0 ) return 0;
drh17435752007-08-16 04:30:38 +0000781 pNew = sqlite3DbMallocRaw(db, sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000782 if( pNew==0 ) return 0;
drh17435752007-08-16 04:30:38 +0000783 pNew->pEList = sqlite3ExprListDup(db, p->pEList);
784 pNew->pSrc = sqlite3SrcListDup(db, p->pSrc);
785 pNew->pWhere = sqlite3ExprDup(db, p->pWhere);
786 pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy);
787 pNew->pHaving = sqlite3ExprDup(db, p->pHaving);
788 pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy);
drhff78bd22002-02-27 01:47:11 +0000789 pNew->op = p->op;
drh17435752007-08-16 04:30:38 +0000790 pNew->pPrior = sqlite3SelectDup(db, p->pPrior);
791 pNew->pLimit = sqlite3ExprDup(db, p->pLimit);
792 pNew->pOffset = sqlite3ExprDup(db, p->pOffset);
drh92b01d52008-06-24 00:32:35 +0000793 pNew->iLimit = 0;
794 pNew->iOffset = 0;
drh7d10d5a2008-08-20 16:35:10 +0000795 pNew->selFlags = p->selFlags & ~SF_UsesEphemeral;
drh0342b1f2005-09-01 03:07:44 +0000796 pNew->pRightmost = 0;
drhb9bb7c12006-06-11 23:41:55 +0000797 pNew->addrOpenEphm[0] = -1;
798 pNew->addrOpenEphm[1] = -1;
799 pNew->addrOpenEphm[2] = -1;
drhff78bd22002-02-27 01:47:11 +0000800 return pNew;
801}
danielk197793758c82005-01-21 08:13:14 +0000802#else
drh17435752007-08-16 04:30:38 +0000803Select *sqlite3SelectDup(sqlite3 *db, Select *p){
danielk197793758c82005-01-21 08:13:14 +0000804 assert( p==0 );
805 return 0;
806}
807#endif
drhff78bd22002-02-27 01:47:11 +0000808
809
810/*
drha76b5df2002-02-23 02:32:10 +0000811** Add a new element to the end of an expression list. If pList is
812** initially NULL, then create a new expression list.
813*/
drh17435752007-08-16 04:30:38 +0000814ExprList *sqlite3ExprListAppend(
815 Parse *pParse, /* Parsing context */
816 ExprList *pList, /* List to which to append. Might be NULL */
817 Expr *pExpr, /* Expression to be appended */
818 Token *pName /* AS keyword for the expression */
819){
820 sqlite3 *db = pParse->db;
drha76b5df2002-02-23 02:32:10 +0000821 if( pList==0 ){
drh17435752007-08-16 04:30:38 +0000822 pList = sqlite3DbMallocZero(db, sizeof(ExprList) );
drha76b5df2002-02-23 02:32:10 +0000823 if( pList==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000824 goto no_mem;
drha76b5df2002-02-23 02:32:10 +0000825 }
drh4efc4752004-01-16 15:55:37 +0000826 assert( pList->nAlloc==0 );
drha76b5df2002-02-23 02:32:10 +0000827 }
drh4305d102003-07-30 12:34:12 +0000828 if( pList->nAlloc<=pList->nExpr ){
danielk1977d5d56522005-03-16 12:15:20 +0000829 struct ExprList_item *a;
830 int n = pList->nAlloc*2 + 4;
danielk197726783a52007-08-29 14:06:22 +0000831 a = sqlite3DbRealloc(db, pList->a, n*sizeof(pList->a[0]));
danielk1977d5d56522005-03-16 12:15:20 +0000832 if( a==0 ){
833 goto no_mem;
drha76b5df2002-02-23 02:32:10 +0000834 }
danielk1977d5d56522005-03-16 12:15:20 +0000835 pList->a = a;
836 pList->nAlloc = n;
drha76b5df2002-02-23 02:32:10 +0000837 }
drh4efc4752004-01-16 15:55:37 +0000838 assert( pList->a!=0 );
839 if( pExpr || pName ){
840 struct ExprList_item *pItem = &pList->a[pList->nExpr++];
841 memset(pItem, 0, sizeof(*pItem));
drh17435752007-08-16 04:30:38 +0000842 pItem->zName = sqlite3NameFromToken(db, pName);
danielk1977e94ddc92005-03-21 03:53:38 +0000843 pItem->pExpr = pExpr;
drh8b213892008-08-29 02:14:02 +0000844 pItem->iAlias = 0;
drha76b5df2002-02-23 02:32:10 +0000845 }
846 return pList;
danielk1977d5d56522005-03-16 12:15:20 +0000847
848no_mem:
849 /* Avoid leaking memory if malloc has failed. */
drh633e6d52008-07-28 19:34:53 +0000850 sqlite3ExprDelete(db, pExpr);
851 sqlite3ExprListDelete(db, pList);
danielk1977d5d56522005-03-16 12:15:20 +0000852 return 0;
drha76b5df2002-02-23 02:32:10 +0000853}
854
855/*
danielk19777a15a4b2007-05-08 17:54:43 +0000856** If the expression list pEList contains more than iLimit elements,
857** leave an error message in pParse.
858*/
859void sqlite3ExprListCheckLength(
860 Parse *pParse,
861 ExprList *pEList,
danielk19777a15a4b2007-05-08 17:54:43 +0000862 const char *zObject
863){
drhb1a6c3c2008-03-20 16:30:17 +0000864 int mx = pParse->db->aLimit[SQLITE_LIMIT_COLUMN];
drhc5499be2008-04-01 15:06:33 +0000865 testcase( pEList && pEList->nExpr==mx );
866 testcase( pEList && pEList->nExpr==mx+1 );
drhb1a6c3c2008-03-20 16:30:17 +0000867 if( pEList && pEList->nExpr>mx ){
danielk19777a15a4b2007-05-08 17:54:43 +0000868 sqlite3ErrorMsg(pParse, "too many columns in %s", zObject);
869 }
870}
871
872/*
drha76b5df2002-02-23 02:32:10 +0000873** Delete an entire expression list.
874*/
drh633e6d52008-07-28 19:34:53 +0000875void sqlite3ExprListDelete(sqlite3 *db, ExprList *pList){
drha76b5df2002-02-23 02:32:10 +0000876 int i;
drhbe5c89a2004-07-26 00:31:09 +0000877 struct ExprList_item *pItem;
drha76b5df2002-02-23 02:32:10 +0000878 if( pList==0 ) return;
drh1bdd9b52004-04-23 17:04:44 +0000879 assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
880 assert( pList->nExpr<=pList->nAlloc );
drhbe5c89a2004-07-26 00:31:09 +0000881 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
drh633e6d52008-07-28 19:34:53 +0000882 sqlite3ExprDelete(db, pItem->pExpr);
883 sqlite3DbFree(db, pItem->zName);
drha76b5df2002-02-23 02:32:10 +0000884 }
drh633e6d52008-07-28 19:34:53 +0000885 sqlite3DbFree(db, pList->a);
886 sqlite3DbFree(db, pList);
drha76b5df2002-02-23 02:32:10 +0000887}
888
889/*
drh7d10d5a2008-08-20 16:35:10 +0000890** These routines are Walker callbacks. Walker.u.pi is a pointer
891** to an integer. These routines are checking an expression to see
892** if it is a constant. Set *Walker.u.pi to 0 if the expression is
893** not constant.
drh73b211a2005-01-18 04:00:42 +0000894**
drh7d10d5a2008-08-20 16:35:10 +0000895** These callback routines are used to implement the following:
drh626a8792005-01-17 22:08:19 +0000896**
drh7d10d5a2008-08-20 16:35:10 +0000897** sqlite3ExprIsConstant()
898** sqlite3ExprIsConstantNotJoin()
899** sqlite3ExprIsConstantOrFunction()
drh87abf5c2005-08-25 12:45:04 +0000900**
drh626a8792005-01-17 22:08:19 +0000901*/
drh7d10d5a2008-08-20 16:35:10 +0000902static int exprNodeIsConstant(Walker *pWalker, Expr *pExpr){
drh626a8792005-01-17 22:08:19 +0000903
drh7d10d5a2008-08-20 16:35:10 +0000904 /* If pWalker->u.i is 3 then any term of the expression that comes from
drh0a168372007-06-08 00:20:47 +0000905 ** the ON or USING clauses of a join disqualifies the expression
906 ** from being considered constant. */
drh7d10d5a2008-08-20 16:35:10 +0000907 if( pWalker->u.i==3 && ExprHasAnyProperty(pExpr, EP_FromJoin) ){
908 pWalker->u.i = 0;
909 return WRC_Abort;
drh0a168372007-06-08 00:20:47 +0000910 }
911
drh626a8792005-01-17 22:08:19 +0000912 switch( pExpr->op ){
drheb55bd22005-06-30 17:04:21 +0000913 /* Consider functions to be constant if all their arguments are constant
drh7d10d5a2008-08-20 16:35:10 +0000914 ** and pWalker->u.i==2 */
drheb55bd22005-06-30 17:04:21 +0000915 case TK_FUNCTION:
drh7d10d5a2008-08-20 16:35:10 +0000916 if( pWalker->u.i==2 ) return 0;
drheb55bd22005-06-30 17:04:21 +0000917 /* Fall through */
drh626a8792005-01-17 22:08:19 +0000918 case TK_ID:
919 case TK_COLUMN:
920 case TK_DOT:
921 case TK_AGG_FUNCTION:
drh13449892005-09-07 21:22:45 +0000922 case TK_AGG_COLUMN:
drhfe2093d2005-01-20 22:48:47 +0000923#ifndef SQLITE_OMIT_SUBQUERY
924 case TK_SELECT:
925 case TK_EXISTS:
drhc5499be2008-04-01 15:06:33 +0000926 testcase( pExpr->op==TK_SELECT );
927 testcase( pExpr->op==TK_EXISTS );
drhfe2093d2005-01-20 22:48:47 +0000928#endif
drhc5499be2008-04-01 15:06:33 +0000929 testcase( pExpr->op==TK_ID );
930 testcase( pExpr->op==TK_COLUMN );
931 testcase( pExpr->op==TK_DOT );
932 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}
drh7d10d5a2008-08-20 16:35:10 +0000940static int selectNodeIsConstant(Walker *pWalker, Select *pSelect){
941 pWalker->u.i = 0;
942 return WRC_Abort;
943}
944static int exprIsConst(Expr *p, int initFlag){
945 Walker w;
946 w.u.i = initFlag;
947 w.xExprCallback = exprNodeIsConstant;
948 w.xSelectCallback = selectNodeIsConstant;
949 sqlite3WalkExpr(&w, p);
950 return w.u.i;
951}
drh626a8792005-01-17 22:08:19 +0000952
953/*
drhfef52082000-06-06 01:50:43 +0000954** Walk an expression tree. Return 1 if the expression is constant
drheb55bd22005-06-30 17:04:21 +0000955** and 0 if it involves variables or function calls.
drh23989372002-05-21 13:43:04 +0000956**
957** For the purposes of this function, a double-quoted string (ex: "abc")
958** is considered a variable but a single-quoted string (ex: 'abc') is
959** a constant.
drhfef52082000-06-06 01:50:43 +0000960*/
danielk19774adee202004-05-08 08:23:19 +0000961int sqlite3ExprIsConstant(Expr *p){
drh7d10d5a2008-08-20 16:35:10 +0000962 return exprIsConst(p, 1);
drhfef52082000-06-06 01:50:43 +0000963}
964
965/*
drheb55bd22005-06-30 17:04:21 +0000966** Walk an expression tree. Return 1 if the expression is constant
drh0a168372007-06-08 00:20:47 +0000967** that does no originate from the ON or USING clauses of a join.
968** Return 0 if it involves variables or function calls or terms from
969** an ON or USING clause.
970*/
971int sqlite3ExprIsConstantNotJoin(Expr *p){
drh7d10d5a2008-08-20 16:35:10 +0000972 return exprIsConst(p, 3);
drh0a168372007-06-08 00:20:47 +0000973}
974
975/*
976** Walk an expression tree. Return 1 if the expression is constant
drheb55bd22005-06-30 17:04:21 +0000977** or a function call with constant arguments. Return and 0 if there
978** are any variables.
979**
980** For the purposes of this function, a double-quoted string (ex: "abc")
981** is considered a variable but a single-quoted string (ex: 'abc') is
982** a constant.
983*/
984int sqlite3ExprIsConstantOrFunction(Expr *p){
drh7d10d5a2008-08-20 16:35:10 +0000985 return exprIsConst(p, 2);
drheb55bd22005-06-30 17:04:21 +0000986}
987
988/*
drh73b211a2005-01-18 04:00:42 +0000989** If the expression p codes a constant integer that is small enough
drh202b2df2004-01-06 01:13:46 +0000990** to fit in a 32-bit integer, return 1 and put the value of the integer
991** in *pValue. If the expression is not an integer or if it is too big
992** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
drhe4de1fe2002-06-02 16:09:01 +0000993*/
danielk19774adee202004-05-08 08:23:19 +0000994int sqlite3ExprIsInteger(Expr *p, int *pValue){
drh92b01d52008-06-24 00:32:35 +0000995 int rc = 0;
996 if( p->flags & EP_IntValue ){
997 *pValue = p->iTable;
998 return 1;
999 }
drhe4de1fe2002-06-02 16:09:01 +00001000 switch( p->op ){
1001 case TK_INTEGER: {
drh92b01d52008-06-24 00:32:35 +00001002 rc = sqlite3GetInt32((char*)p->token.z, pValue);
drh202b2df2004-01-06 01:13:46 +00001003 break;
drhe4de1fe2002-06-02 16:09:01 +00001004 }
drh4b59ab52002-08-24 18:24:51 +00001005 case TK_UPLUS: {
drh92b01d52008-06-24 00:32:35 +00001006 rc = sqlite3ExprIsInteger(p->pLeft, pValue);
drhf6e369a2008-06-24 12:46:30 +00001007 break;
drh4b59ab52002-08-24 18:24:51 +00001008 }
drhe4de1fe2002-06-02 16:09:01 +00001009 case TK_UMINUS: {
1010 int v;
danielk19774adee202004-05-08 08:23:19 +00001011 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
drhe4de1fe2002-06-02 16:09:01 +00001012 *pValue = -v;
drh92b01d52008-06-24 00:32:35 +00001013 rc = 1;
drhe4de1fe2002-06-02 16:09:01 +00001014 }
1015 break;
1016 }
1017 default: break;
1018 }
drh92b01d52008-06-24 00:32:35 +00001019 if( rc ){
1020 p->op = TK_INTEGER;
1021 p->flags |= EP_IntValue;
1022 p->iTable = *pValue;
1023 }
1024 return rc;
drhe4de1fe2002-06-02 16:09:01 +00001025}
1026
1027/*
drhc4a3c772001-04-04 11:48:57 +00001028** Return TRUE if the given string is a row-id column name.
1029*/
danielk19774adee202004-05-08 08:23:19 +00001030int sqlite3IsRowid(const char *z){
1031 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
1032 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
1033 if( sqlite3StrICmp(z, "OID")==0 ) return 1;
drhc4a3c772001-04-04 11:48:57 +00001034 return 0;
1035}
1036
danielk19779a96b662007-11-29 17:05:18 +00001037#ifdef SQLITE_TEST
1038 int sqlite3_enable_in_opt = 1;
1039#else
1040 #define sqlite3_enable_in_opt 1
1041#endif
1042
1043/*
drhb287f4b2008-04-25 00:08:38 +00001044** Return true if the IN operator optimization is enabled and
1045** the SELECT statement p exists and is of the
1046** simple form:
1047**
1048** SELECT <column> FROM <table>
1049**
1050** If this is the case, it may be possible to use an existing table
1051** or index instead of generating an epheremal table.
1052*/
1053#ifndef SQLITE_OMIT_SUBQUERY
1054static int isCandidateForInOpt(Select *p){
1055 SrcList *pSrc;
1056 ExprList *pEList;
1057 Table *pTab;
1058 if( !sqlite3_enable_in_opt ) return 0; /* IN optimization must be enabled */
1059 if( p==0 ) return 0; /* right-hand side of IN is SELECT */
1060 if( p->pPrior ) return 0; /* Not a compound SELECT */
drh7d10d5a2008-08-20 16:35:10 +00001061 if( p->selFlags & (SF_Distinct|SF_Aggregate) ){
1062 return 0; /* No DISTINCT keyword and no aggregate functions */
1063 }
drhb287f4b2008-04-25 00:08:38 +00001064 if( p->pGroupBy ) return 0; /* Has no GROUP BY clause */
1065 if( p->pLimit ) return 0; /* Has no LIMIT clause */
1066 if( p->pOffset ) return 0;
1067 if( p->pWhere ) return 0; /* Has no WHERE clause */
1068 pSrc = p->pSrc;
1069 if( pSrc==0 ) return 0; /* A single table in the FROM clause */
1070 if( pSrc->nSrc!=1 ) return 0;
1071 if( pSrc->a[0].pSelect ) return 0; /* FROM clause is not a subquery */
1072 pTab = pSrc->a[0].pTab;
1073 if( pTab==0 ) return 0;
1074 if( pTab->pSelect ) return 0; /* FROM clause is not a view */
1075 if( IsVirtual(pTab) ) return 0; /* FROM clause not a virtual table */
1076 pEList = p->pEList;
1077 if( pEList->nExpr!=1 ) return 0; /* One column in the result set */
1078 if( pEList->a[0].pExpr->op!=TK_COLUMN ) return 0; /* Result is a column */
1079 return 1;
1080}
1081#endif /* SQLITE_OMIT_SUBQUERY */
1082
1083/*
danielk19779a96b662007-11-29 17:05:18 +00001084** This function is used by the implementation of the IN (...) operator.
1085** It's job is to find or create a b-tree structure that may be used
1086** either to test for membership of the (...) set or to iterate through
drh85b623f2007-12-13 21:54:09 +00001087** its members, skipping duplicates.
danielk19779a96b662007-11-29 17:05:18 +00001088**
1089** The cursor opened on the structure (database table, database index
1090** or ephermal table) is stored in pX->iTable before this function returns.
1091** The returned value indicates the structure type, as follows:
1092**
1093** IN_INDEX_ROWID - The cursor was opened on a database table.
drh2d401ab2008-01-10 23:50:11 +00001094** IN_INDEX_INDEX - The cursor was opened on a database index.
danielk19779a96b662007-11-29 17:05:18 +00001095** IN_INDEX_EPH - The cursor was opened on a specially created and
1096** populated epheremal table.
1097**
1098** An existing structure may only be used if the SELECT is of the simple
1099** form:
1100**
1101** SELECT <column> FROM <table>
1102**
danielk19770cdc0222008-06-26 18:04:03 +00001103** If prNotFound parameter is 0, then the structure will be used to iterate
danielk19779a96b662007-11-29 17:05:18 +00001104** through the set members, skipping any duplicates. In this case an
1105** epheremal table must be used unless the selected <column> is guaranteed
1106** to be unique - either because it is an INTEGER PRIMARY KEY or it
1107** is unique by virtue of a constraint or implicit index.
danielk19770cdc0222008-06-26 18:04:03 +00001108**
1109** If the prNotFound parameter is not 0, then the structure will be used
1110** for fast set membership tests. In this case an epheremal table must
1111** be used unless <column> is an INTEGER PRIMARY KEY or an index can
1112** be found with <column> as its left-most column.
1113**
1114** When the structure is being used for set membership tests, the user
1115** needs to know whether or not the structure contains an SQL NULL
1116** value in order to correctly evaluate expressions like "X IN (Y, Z)".
1117** If there is a chance that the structure may contain a NULL value at
1118** runtime, then a register is allocated and the register number written
1119** to *prNotFound. If there is no chance that the structure contains a
1120** NULL value, then *prNotFound is left unchanged.
1121**
1122** If a register is allocated and its location stored in *prNotFound, then
1123** its initial value is NULL. If the structure does not remain constant
1124** for the duration of the query (i.e. the set is a correlated sub-select),
1125** the value of the allocated register is reset to NULL each time the
1126** structure is repopulated. This allows the caller to use vdbe code
1127** equivalent to the following:
1128**
1129** if( register==NULL ){
1130** has_null = <test if data structure contains null>
1131** register = 1
1132** }
1133**
1134** in order to avoid running the <test if data structure contains null>
1135** test more often than is necessary.
danielk19779a96b662007-11-29 17:05:18 +00001136*/
danielk1977284f4ac2007-12-10 05:03:46 +00001137#ifndef SQLITE_OMIT_SUBQUERY
danielk19770cdc0222008-06-26 18:04:03 +00001138int sqlite3FindInIndex(Parse *pParse, Expr *pX, int *prNotFound){
danielk19779a96b662007-11-29 17:05:18 +00001139 Select *p;
1140 int eType = 0;
1141 int iTab = pParse->nTab++;
danielk19770cdc0222008-06-26 18:04:03 +00001142 int mustBeUnique = !prNotFound;
danielk19779a96b662007-11-29 17:05:18 +00001143
1144 /* The follwing if(...) expression is true if the SELECT is of the
1145 ** simple form:
1146 **
1147 ** SELECT <column> FROM <table>
1148 **
1149 ** If this is the case, it may be possible to use an existing table
1150 ** or index instead of generating an epheremal table.
1151 */
drhb287f4b2008-04-25 00:08:38 +00001152 p = pX->pSelect;
1153 if( isCandidateForInOpt(p) ){
danielk19779a96b662007-11-29 17:05:18 +00001154 sqlite3 *db = pParse->db;
1155 Index *pIdx;
1156 Expr *pExpr = p->pEList->a[0].pExpr;
1157 int iCol = pExpr->iColumn;
1158 Vdbe *v = sqlite3GetVdbe(pParse);
1159
1160 /* This function is only called from two places. In both cases the vdbe
1161 ** has already been allocated. So assume sqlite3GetVdbe() is always
1162 ** successful here.
1163 */
1164 assert(v);
1165 if( iCol<0 ){
drh0a07c102008-01-03 18:03:08 +00001166 int iMem = ++pParse->nMem;
danielk19779a96b662007-11-29 17:05:18 +00001167 int iAddr;
1168 Table *pTab = p->pSrc->a[0].pTab;
1169 int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
1170 sqlite3VdbeUsesBtree(v, iDb);
1171
drh892d3172008-01-10 03:46:36 +00001172 iAddr = sqlite3VdbeAddOp1(v, OP_If, iMem);
drh4c583122008-01-04 22:01:03 +00001173 sqlite3VdbeAddOp2(v, OP_Integer, 1, iMem);
danielk19779a96b662007-11-29 17:05:18 +00001174
1175 sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
1176 eType = IN_INDEX_ROWID;
1177
1178 sqlite3VdbeJumpHere(v, iAddr);
1179 }else{
1180 /* The collation sequence used by the comparison. If an index is to
1181 ** be used in place of a temp-table, it must be ordered according
1182 ** to this collation sequence.
1183 */
1184 CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pExpr);
1185
1186 /* Check that the affinity that will be used to perform the
1187 ** comparison is the same as the affinity of the column. If
1188 ** it is not, it is not possible to use any index.
1189 */
1190 Table *pTab = p->pSrc->a[0].pTab;
1191 char aff = comparisonAffinity(pX);
1192 int affinity_ok = (pTab->aCol[iCol].affinity==aff||aff==SQLITE_AFF_NONE);
1193
1194 for(pIdx=pTab->pIndex; pIdx && eType==0 && affinity_ok; pIdx=pIdx->pNext){
1195 if( (pIdx->aiColumn[0]==iCol)
1196 && (pReq==sqlite3FindCollSeq(db, ENC(db), pIdx->azColl[0], -1, 0))
1197 && (!mustBeUnique || (pIdx->nColumn==1 && pIdx->onError!=OE_None))
1198 ){
1199 int iDb;
drh0a07c102008-01-03 18:03:08 +00001200 int iMem = ++pParse->nMem;
danielk19779a96b662007-11-29 17:05:18 +00001201 int iAddr;
1202 char *pKey;
1203
1204 pKey = (char *)sqlite3IndexKeyinfo(pParse, pIdx);
1205 iDb = sqlite3SchemaToIndex(db, pIdx->pSchema);
1206 sqlite3VdbeUsesBtree(v, iDb);
1207
drh892d3172008-01-10 03:46:36 +00001208 iAddr = sqlite3VdbeAddOp1(v, OP_If, iMem);
drh4c583122008-01-04 22:01:03 +00001209 sqlite3VdbeAddOp2(v, OP_Integer, 1, iMem);
danielk19779a96b662007-11-29 17:05:18 +00001210
danielk1977cd3e8f72008-03-25 09:47:35 +00001211 sqlite3VdbeAddOp2(v, OP_SetNumColumns, 0, pIdx->nColumn);
danielk1977207872a2008-01-03 07:54:23 +00001212 sqlite3VdbeAddOp4(v, OP_OpenRead, iTab, pIdx->tnum, iDb,
drh66a51672008-01-03 00:01:23 +00001213 pKey,P4_KEYINFO_HANDOFF);
danielk1977207872a2008-01-03 07:54:23 +00001214 VdbeComment((v, "%s", pIdx->zName));
danielk19779a96b662007-11-29 17:05:18 +00001215 eType = IN_INDEX_INDEX;
danielk19779a96b662007-11-29 17:05:18 +00001216
1217 sqlite3VdbeJumpHere(v, iAddr);
danielk19770cdc0222008-06-26 18:04:03 +00001218 if( prNotFound && !pTab->aCol[iCol].notNull ){
1219 *prNotFound = ++pParse->nMem;
1220 }
danielk19779a96b662007-11-29 17:05:18 +00001221 }
1222 }
1223 }
1224 }
1225
1226 if( eType==0 ){
danielk19770cdc0222008-06-26 18:04:03 +00001227 int rMayHaveNull = 0;
danielk197741a05b72008-10-02 13:50:55 +00001228 eType = IN_INDEX_EPH;
danielk19770cdc0222008-06-26 18:04:03 +00001229 if( prNotFound ){
1230 *prNotFound = rMayHaveNull = ++pParse->nMem;
danielk197741a05b72008-10-02 13:50:55 +00001231 }else if( pX->pLeft->iColumn<0 && pX->pSelect==0 ){
1232 eType = IN_INDEX_ROWID;
danielk19770cdc0222008-06-26 18:04:03 +00001233 }
danielk197741a05b72008-10-02 13:50:55 +00001234 sqlite3CodeSubselect(pParse, pX, rMayHaveNull, eType==IN_INDEX_ROWID);
danielk19779a96b662007-11-29 17:05:18 +00001235 }else{
1236 pX->iTable = iTab;
1237 }
1238 return eType;
1239}
danielk1977284f4ac2007-12-10 05:03:46 +00001240#endif
drh626a8792005-01-17 22:08:19 +00001241
1242/*
drh9cbe6352005-11-29 03:13:21 +00001243** Generate code for scalar subqueries used as an expression
1244** and IN operators. Examples:
drh626a8792005-01-17 22:08:19 +00001245**
drh9cbe6352005-11-29 03:13:21 +00001246** (SELECT a FROM b) -- subquery
1247** EXISTS (SELECT a FROM b) -- EXISTS subquery
1248** x IN (4,5,11) -- IN operator with list on right-hand side
1249** x IN (SELECT a FROM b) -- IN operator with subquery on the right
drhfef52082000-06-06 01:50:43 +00001250**
drh9cbe6352005-11-29 03:13:21 +00001251** The pExpr parameter describes the expression that contains the IN
1252** operator or subquery.
danielk197741a05b72008-10-02 13:50:55 +00001253**
1254** If parameter isRowid is non-zero, then expression pExpr is guaranteed
1255** to be of the form "<rowid> IN (?, ?, ?)", where <rowid> is a reference
1256** to some integer key column of a table B-Tree. In this case, use an
1257** intkey B-Tree to store the set of IN(...) values instead of the usual
1258** (slower) variable length keys B-Tree.
drhcce7d172000-05-31 15:34:51 +00001259*/
drh51522cd2005-01-20 13:36:19 +00001260#ifndef SQLITE_OMIT_SUBQUERY
danielk197741a05b72008-10-02 13:50:55 +00001261void sqlite3CodeSubselect(
1262 Parse *pParse,
1263 Expr *pExpr,
1264 int rMayHaveNull,
1265 int isRowid
1266){
drh57dbd7b2005-07-08 18:25:26 +00001267 int testAddr = 0; /* One-time test address */
danielk1977b3bce662005-01-29 08:32:43 +00001268 Vdbe *v = sqlite3GetVdbe(pParse);
1269 if( v==0 ) return;
1270
danielk1977fc976062007-05-10 10:46:56 +00001271
drh57dbd7b2005-07-08 18:25:26 +00001272 /* This code must be run in its entirety every time it is encountered
1273 ** if any of the following is true:
1274 **
1275 ** * The right-hand side is a correlated subquery
1276 ** * The right-hand side is an expression list containing variables
1277 ** * We are inside a trigger
1278 **
1279 ** If all of the above are false, then we can run this code just once
1280 ** save the results, and reuse the same result on subsequent invocations.
danielk1977b3bce662005-01-29 08:32:43 +00001281 */
1282 if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->trigStack ){
drh0a07c102008-01-03 18:03:08 +00001283 int mem = ++pParse->nMem;
drh892d3172008-01-10 03:46:36 +00001284 sqlite3VdbeAddOp1(v, OP_If, mem);
1285 testAddr = sqlite3VdbeAddOp2(v, OP_Integer, 1, mem);
drh17435752007-08-16 04:30:38 +00001286 assert( testAddr>0 || pParse->db->mallocFailed );
danielk1977b3bce662005-01-29 08:32:43 +00001287 }
1288
drhcce7d172000-05-31 15:34:51 +00001289 switch( pExpr->op ){
drhfef52082000-06-06 01:50:43 +00001290 case TK_IN: {
danielk1977e014a832004-05-17 10:48:57 +00001291 char affinity;
drhd3d39e92004-05-20 22:16:29 +00001292 KeyInfo keyInfo;
drhb9bb7c12006-06-11 23:41:55 +00001293 int addr; /* Address of OP_OpenEphemeral instruction */
danielk197741a05b72008-10-02 13:50:55 +00001294 Expr *pLeft = pExpr->pLeft;
drhd3d39e92004-05-20 22:16:29 +00001295
danielk19770cdc0222008-06-26 18:04:03 +00001296 if( rMayHaveNull ){
1297 sqlite3VdbeAddOp2(v, OP_Null, 0, rMayHaveNull);
1298 }
1299
danielk197741a05b72008-10-02 13:50:55 +00001300 affinity = sqlite3ExprAffinity(pLeft);
danielk1977e014a832004-05-17 10:48:57 +00001301
1302 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
drh57dbd7b2005-07-08 18:25:26 +00001303 ** expression it is handled the same way. A virtual table is
danielk1977e014a832004-05-17 10:48:57 +00001304 ** filled with single-field index keys representing the results
1305 ** from the SELECT or the <exprlist>.
1306 **
1307 ** If the 'x' expression is a column value, or the SELECT...
1308 ** statement returns a column value, then the affinity of that
1309 ** column is used to build the index keys. If both 'x' and the
1310 ** SELECT... statement are columns, then numeric affinity is used
1311 ** if either column has NUMERIC or INTEGER affinity. If neither
1312 ** 'x' nor the SELECT... statement are columns, then numeric affinity
1313 ** is used.
1314 */
1315 pExpr->iTable = pParse->nTab++;
danielk197741a05b72008-10-02 13:50:55 +00001316 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pExpr->iTable, !isRowid);
drhd3d39e92004-05-20 22:16:29 +00001317 memset(&keyInfo, 0, sizeof(keyInfo));
1318 keyInfo.nField = 1;
danielk1977e014a832004-05-17 10:48:57 +00001319
drhfef52082000-06-06 01:50:43 +00001320 if( pExpr->pSelect ){
1321 /* Case 1: expr IN (SELECT ...)
1322 **
danielk1977e014a832004-05-17 10:48:57 +00001323 ** Generate code to write the results of the select into the temporary
1324 ** table allocated and opened above.
drhfef52082000-06-06 01:50:43 +00001325 */
drh1013c932008-01-06 00:25:21 +00001326 SelectDest dest;
drhbe5c89a2004-07-26 00:31:09 +00001327 ExprList *pEList;
drh1013c932008-01-06 00:25:21 +00001328
danielk197741a05b72008-10-02 13:50:55 +00001329 assert( !isRowid );
drh1013c932008-01-06 00:25:21 +00001330 sqlite3SelectDestInit(&dest, SRT_Set, pExpr->iTable);
1331 dest.affinity = (int)affinity;
danielk1977e014a832004-05-17 10:48:57 +00001332 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
drh7d10d5a2008-08-20 16:35:10 +00001333 if( sqlite3Select(pParse, pExpr->pSelect, &dest) ){
drh94ccde52007-04-13 16:06:32 +00001334 return;
1335 }
drhbe5c89a2004-07-26 00:31:09 +00001336 pEList = pExpr->pSelect->pEList;
1337 if( pEList && pEList->nExpr>0 ){
danielk1977bcbb04e2007-05-29 12:11:29 +00001338 keyInfo.aColl[0] = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft,
drhbe5c89a2004-07-26 00:31:09 +00001339 pEList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001340 }
drhfef52082000-06-06 01:50:43 +00001341 }else if( pExpr->pList ){
1342 /* Case 2: expr IN (exprlist)
1343 **
drhfd131da2007-08-07 17:13:03 +00001344 ** For each expression, build an index key from the evaluation and
danielk1977e014a832004-05-17 10:48:57 +00001345 ** store it in the temporary table. If <expr> is a column, then use
1346 ** that columns affinity when building index keys. If <expr> is not
1347 ** a column, use numeric affinity.
drhfef52082000-06-06 01:50:43 +00001348 */
danielk1977e014a832004-05-17 10:48:57 +00001349 int i;
drh57dbd7b2005-07-08 18:25:26 +00001350 ExprList *pList = pExpr->pList;
1351 struct ExprList_item *pItem;
drhecc31802008-06-26 20:06:06 +00001352 int r1, r2, r3;
drh57dbd7b2005-07-08 18:25:26 +00001353
danielk1977e014a832004-05-17 10:48:57 +00001354 if( !affinity ){
drh8159a352006-05-23 23:22:29 +00001355 affinity = SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +00001356 }
drh7d10d5a2008-08-20 16:35:10 +00001357 keyInfo.aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +00001358
1359 /* Loop through each expression in <exprlist>. */
drh2d401ab2008-01-10 23:50:11 +00001360 r1 = sqlite3GetTempReg(pParse);
1361 r2 = sqlite3GetTempReg(pParse);
danielk19774e7f36a2008-10-02 16:42:06 +00001362 sqlite3VdbeAddOp2(v, OP_Null, 0, r2);
drh57dbd7b2005-07-08 18:25:26 +00001363 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
1364 Expr *pE2 = pItem->pExpr;
danielk1977e014a832004-05-17 10:48:57 +00001365
drh57dbd7b2005-07-08 18:25:26 +00001366 /* If the expression is not constant then we will need to
1367 ** disable the test that was generated above that makes sure
1368 ** this code only executes once. Because for a non-constant
1369 ** expression we need to rerun this code each time.
1370 */
drh892d3172008-01-10 03:46:36 +00001371 if( testAddr && !sqlite3ExprIsConstant(pE2) ){
1372 sqlite3VdbeChangeToNoop(v, testAddr-1, 2);
drh57dbd7b2005-07-08 18:25:26 +00001373 testAddr = 0;
drh4794b982000-06-06 13:54:14 +00001374 }
danielk1977e014a832004-05-17 10:48:57 +00001375
1376 /* Evaluate the expression and insert it into the temp table */
drhe55cbd72008-03-31 23:48:03 +00001377 pParse->disableColCache++;
drhecc31802008-06-26 20:06:06 +00001378 r3 = sqlite3ExprCodeTarget(pParse, pE2, r1);
drhc5499be2008-04-01 15:06:33 +00001379 assert( pParse->disableColCache>0 );
drhe55cbd72008-03-31 23:48:03 +00001380 pParse->disableColCache--;
danielk197741a05b72008-10-02 13:50:55 +00001381
1382 if( isRowid ){
danielk197741a05b72008-10-02 13:50:55 +00001383 sqlite3VdbeAddOp2(v, OP_MustBeInt, r3, sqlite3VdbeCurrentAddr(v)+2);
1384 sqlite3VdbeAddOp3(v, OP_Insert, pExpr->iTable, r2, r3);
1385 }else{
1386 sqlite3VdbeAddOp4(v, OP_MakeRecord, r3, 1, r2, &affinity, 1);
1387 sqlite3ExprCacheAffinityChange(pParse, r3, 1);
1388 sqlite3VdbeAddOp2(v, OP_IdxInsert, pExpr->iTable, r2);
1389 }
drhfef52082000-06-06 01:50:43 +00001390 }
drh2d401ab2008-01-10 23:50:11 +00001391 sqlite3ReleaseTempReg(pParse, r1);
1392 sqlite3ReleaseTempReg(pParse, r2);
drhfef52082000-06-06 01:50:43 +00001393 }
danielk197741a05b72008-10-02 13:50:55 +00001394 if( !isRowid ){
1395 sqlite3VdbeChangeP4(v, addr, (void *)&keyInfo, P4_KEYINFO);
1396 }
danielk1977b3bce662005-01-29 08:32:43 +00001397 break;
drhfef52082000-06-06 01:50:43 +00001398 }
1399
drh51522cd2005-01-20 13:36:19 +00001400 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001401 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +00001402 /* This has to be a scalar SELECT. Generate code to put the
1403 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +00001404 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +00001405 */
drh2646da72005-12-09 20:02:05 +00001406 static const Token one = { (u8*)"1", 0, 1 };
drh51522cd2005-01-20 13:36:19 +00001407 Select *pSel;
danielk19776c8c8ce2008-01-02 16:27:09 +00001408 SelectDest dest;
drh1398ad32005-01-19 23:24:50 +00001409
drh51522cd2005-01-20 13:36:19 +00001410 pSel = pExpr->pSelect;
drh1013c932008-01-06 00:25:21 +00001411 sqlite3SelectDestInit(&dest, 0, ++pParse->nMem);
drh51522cd2005-01-20 13:36:19 +00001412 if( pExpr->op==TK_SELECT ){
danielk19776c8c8ce2008-01-02 16:27:09 +00001413 dest.eDest = SRT_Mem;
drh4c583122008-01-04 22:01:03 +00001414 sqlite3VdbeAddOp2(v, OP_Null, 0, dest.iParm);
drhd4e70eb2008-01-02 00:34:36 +00001415 VdbeComment((v, "Init subquery result"));
drh51522cd2005-01-20 13:36:19 +00001416 }else{
danielk19776c8c8ce2008-01-02 16:27:09 +00001417 dest.eDest = SRT_Exists;
drh4c583122008-01-04 22:01:03 +00001418 sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iParm);
drhd4e70eb2008-01-02 00:34:36 +00001419 VdbeComment((v, "Init EXISTS result"));
drh51522cd2005-01-20 13:36:19 +00001420 }
drh633e6d52008-07-28 19:34:53 +00001421 sqlite3ExprDelete(pParse->db, pSel->pLimit);
danielk1977a1644fd2007-08-29 12:31:25 +00001422 pSel->pLimit = sqlite3PExpr(pParse, TK_INTEGER, 0, 0, &one);
drh7d10d5a2008-08-20 16:35:10 +00001423 if( sqlite3Select(pParse, pSel, &dest) ){
drh94ccde52007-04-13 16:06:32 +00001424 return;
1425 }
danielk19776c8c8ce2008-01-02 16:27:09 +00001426 pExpr->iColumn = dest.iParm;
danielk1977b3bce662005-01-29 08:32:43 +00001427 break;
drhcce7d172000-05-31 15:34:51 +00001428 }
1429 }
danielk1977b3bce662005-01-29 08:32:43 +00001430
drh57dbd7b2005-07-08 18:25:26 +00001431 if( testAddr ){
drh892d3172008-01-10 03:46:36 +00001432 sqlite3VdbeJumpHere(v, testAddr-1);
danielk1977b3bce662005-01-29 08:32:43 +00001433 }
danielk1977fc976062007-05-10 10:46:56 +00001434
danielk1977b3bce662005-01-29 08:32:43 +00001435 return;
drhcce7d172000-05-31 15:34:51 +00001436}
drh51522cd2005-01-20 13:36:19 +00001437#endif /* SQLITE_OMIT_SUBQUERY */
drhcce7d172000-05-31 15:34:51 +00001438
drhcce7d172000-05-31 15:34:51 +00001439/*
drh598f1342007-10-23 15:39:45 +00001440** Duplicate an 8-byte value
1441*/
1442static char *dup8bytes(Vdbe *v, const char *in){
1443 char *out = sqlite3DbMallocRaw(sqlite3VdbeDb(v), 8);
1444 if( out ){
1445 memcpy(out, in, 8);
1446 }
1447 return out;
1448}
1449
1450/*
1451** Generate an instruction that will put the floating point
drh9cbf3422008-01-17 16:22:13 +00001452** value described by z[0..n-1] into register iMem.
drh0cf19ed2007-10-23 18:55:48 +00001453**
1454** The z[] string will probably not be zero-terminated. But the
1455** z[n] character is guaranteed to be something that does not look
1456** like the continuation of the number.
drh598f1342007-10-23 15:39:45 +00001457*/
drh9de221d2008-01-05 06:51:30 +00001458static void codeReal(Vdbe *v, const char *z, int n, int negateFlag, int iMem){
drh598f1342007-10-23 15:39:45 +00001459 assert( z || v==0 || sqlite3VdbeDb(v)->mallocFailed );
1460 if( z ){
1461 double value;
1462 char *zV;
drh0cf19ed2007-10-23 18:55:48 +00001463 assert( !isdigit(z[n]) );
drh598f1342007-10-23 15:39:45 +00001464 sqlite3AtoF(z, &value);
drh2eaf93d2008-04-29 00:15:20 +00001465 if( sqlite3IsNaN(value) ){
1466 sqlite3VdbeAddOp2(v, OP_Null, 0, iMem);
1467 }else{
1468 if( negateFlag ) value = -value;
1469 zV = dup8bytes(v, (char*)&value);
1470 sqlite3VdbeAddOp4(v, OP_Real, 0, iMem, 0, zV, P4_REAL);
1471 }
drh598f1342007-10-23 15:39:45 +00001472 }
1473}
1474
1475
1476/*
drhfec19aa2004-05-19 20:41:03 +00001477** Generate an instruction that will put the integer describe by
drh9cbf3422008-01-17 16:22:13 +00001478** text z[0..n-1] into register iMem.
drh0cf19ed2007-10-23 18:55:48 +00001479**
1480** The z[] string will probably not be zero-terminated. But the
1481** z[n] character is guaranteed to be something that does not look
1482** like the continuation of the number.
drhfec19aa2004-05-19 20:41:03 +00001483*/
drh92b01d52008-06-24 00:32:35 +00001484static void codeInteger(Vdbe *v, Expr *pExpr, int negFlag, int iMem){
1485 const char *z;
1486 if( pExpr->flags & EP_IntValue ){
1487 int i = pExpr->iTable;
1488 if( negFlag ) i = -i;
1489 sqlite3VdbeAddOp2(v, OP_Integer, i, iMem);
1490 }else if( (z = (char*)pExpr->token.z)!=0 ){
danielk1977c9cf9012007-05-30 10:36:47 +00001491 int i;
drh92b01d52008-06-24 00:32:35 +00001492 int n = pExpr->token.n;
drh0cf19ed2007-10-23 18:55:48 +00001493 assert( !isdigit(z[n]) );
danielk1977c9cf9012007-05-30 10:36:47 +00001494 if( sqlite3GetInt32(z, &i) ){
drh9de221d2008-01-05 06:51:30 +00001495 if( negFlag ) i = -i;
1496 sqlite3VdbeAddOp2(v, OP_Integer, i, iMem);
1497 }else if( sqlite3FitsIn64Bits(z, negFlag) ){
drh598f1342007-10-23 15:39:45 +00001498 i64 value;
1499 char *zV;
1500 sqlite3Atoi64(z, &value);
drh9de221d2008-01-05 06:51:30 +00001501 if( negFlag ) value = -value;
drh598f1342007-10-23 15:39:45 +00001502 zV = dup8bytes(v, (char*)&value);
drh9de221d2008-01-05 06:51:30 +00001503 sqlite3VdbeAddOp4(v, OP_Int64, 0, iMem, 0, zV, P4_INT64);
danielk1977c9cf9012007-05-30 10:36:47 +00001504 }else{
drh9de221d2008-01-05 06:51:30 +00001505 codeReal(v, z, n, negFlag, iMem);
danielk1977c9cf9012007-05-30 10:36:47 +00001506 }
drhfec19aa2004-05-19 20:41:03 +00001507 }
1508}
1509
drh945498f2007-02-24 11:52:52 +00001510
1511/*
1512** Generate code that will extract the iColumn-th column from
drhe55cbd72008-03-31 23:48:03 +00001513** table pTab and store the column value in a register. An effort
1514** is made to store the column value in register iReg, but this is
1515** not guaranteed. The location of the column value is returned.
1516**
1517** There must be an open cursor to pTab in iTable when this routine
1518** is called. If iColumn<0 then code is generated that extracts the rowid.
drhda250ea2008-04-01 05:07:14 +00001519**
1520** This routine might attempt to reuse the value of the column that
1521** has already been loaded into a register. The value will always
1522** be used if it has not undergone any affinity changes. But if
1523** an affinity change has occurred, then the cached value will only be
1524** used if allowAffChng is true.
drh945498f2007-02-24 11:52:52 +00001525*/
drhe55cbd72008-03-31 23:48:03 +00001526int sqlite3ExprCodeGetColumn(
1527 Parse *pParse, /* Parsing and code generating context */
drh2133d822008-01-03 18:44:59 +00001528 Table *pTab, /* Description of the table we are reading from */
1529 int iColumn, /* Index of the table column */
1530 int iTable, /* The cursor pointing to the table */
drhda250ea2008-04-01 05:07:14 +00001531 int iReg, /* Store results here */
1532 int allowAffChng /* True if prior affinity changes are OK */
drh2133d822008-01-03 18:44:59 +00001533){
drhe55cbd72008-03-31 23:48:03 +00001534 Vdbe *v = pParse->pVdbe;
1535 int i;
drhda250ea2008-04-01 05:07:14 +00001536 struct yColCache *p;
drhe55cbd72008-03-31 23:48:03 +00001537
drhda250ea2008-04-01 05:07:14 +00001538 for(i=0, p=pParse->aColCache; i<pParse->nColCache; i++, p++){
1539 if( p->iTable==iTable && p->iColumn==iColumn
1540 && (!p->affChange || allowAffChng) ){
drhe55cbd72008-03-31 23:48:03 +00001541#if 0
1542 sqlite3VdbeAddOp0(v, OP_Noop);
drhda250ea2008-04-01 05:07:14 +00001543 VdbeComment((v, "OPT: tab%d.col%d -> r%d", iTable, iColumn, p->iReg));
drhe55cbd72008-03-31 23:48:03 +00001544#endif
drhda250ea2008-04-01 05:07:14 +00001545 return p->iReg;
drhe55cbd72008-03-31 23:48:03 +00001546 }
1547 }
1548 assert( v!=0 );
drh945498f2007-02-24 11:52:52 +00001549 if( iColumn<0 ){
1550 int op = (pTab && IsVirtual(pTab)) ? OP_VRowid : OP_Rowid;
drh2133d822008-01-03 18:44:59 +00001551 sqlite3VdbeAddOp2(v, op, iTable, iReg);
drh945498f2007-02-24 11:52:52 +00001552 }else if( pTab==0 ){
drh2133d822008-01-03 18:44:59 +00001553 sqlite3VdbeAddOp3(v, OP_Column, iTable, iColumn, iReg);
drh945498f2007-02-24 11:52:52 +00001554 }else{
1555 int op = IsVirtual(pTab) ? OP_VColumn : OP_Column;
drh2133d822008-01-03 18:44:59 +00001556 sqlite3VdbeAddOp3(v, op, iTable, iColumn, iReg);
drh945498f2007-02-24 11:52:52 +00001557 sqlite3ColumnDefault(v, pTab, iColumn);
1558#ifndef SQLITE_OMIT_FLOATING_POINT
1559 if( pTab->aCol[iColumn].affinity==SQLITE_AFF_REAL ){
drh2133d822008-01-03 18:44:59 +00001560 sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg);
drh945498f2007-02-24 11:52:52 +00001561 }
1562#endif
1563 }
drhe55cbd72008-03-31 23:48:03 +00001564 if( pParse->disableColCache==0 ){
1565 i = pParse->iColCache;
drhda250ea2008-04-01 05:07:14 +00001566 p = &pParse->aColCache[i];
1567 p->iTable = iTable;
1568 p->iColumn = iColumn;
1569 p->iReg = iReg;
drhc5499be2008-04-01 15:06:33 +00001570 p->affChange = 0;
drhe55cbd72008-03-31 23:48:03 +00001571 i++;
drh2f7794c2008-04-01 03:27:39 +00001572 if( i>=ArraySize(pParse->aColCache) ) i = 0;
drhe55cbd72008-03-31 23:48:03 +00001573 if( i>pParse->nColCache ) pParse->nColCache = i;
drh2f7794c2008-04-01 03:27:39 +00001574 pParse->iColCache = i;
drhe55cbd72008-03-31 23:48:03 +00001575 }
1576 return iReg;
1577}
1578
1579/*
drhe55cbd72008-03-31 23:48:03 +00001580** Clear all column cache entries associated with the vdbe
1581** cursor with cursor number iTable.
1582*/
1583void sqlite3ExprClearColumnCache(Parse *pParse, int iTable){
1584 if( iTable<0 ){
1585 pParse->nColCache = 0;
1586 pParse->iColCache = 0;
1587 }else{
1588 int i;
1589 for(i=0; i<pParse->nColCache; i++){
1590 if( pParse->aColCache[i].iTable==iTable ){
drhc5499be2008-04-01 15:06:33 +00001591 testcase( i==pParse->nColCache-1 );
drhe55cbd72008-03-31 23:48:03 +00001592 pParse->aColCache[i] = pParse->aColCache[--pParse->nColCache];
drhda250ea2008-04-01 05:07:14 +00001593 pParse->iColCache = pParse->nColCache;
drhe55cbd72008-03-31 23:48:03 +00001594 }
1595 }
drhe55cbd72008-03-31 23:48:03 +00001596 }
1597}
1598
1599/*
drhda250ea2008-04-01 05:07:14 +00001600** Record the fact that an affinity change has occurred on iCount
1601** registers starting with iStart.
drhe55cbd72008-03-31 23:48:03 +00001602*/
drhda250ea2008-04-01 05:07:14 +00001603void sqlite3ExprCacheAffinityChange(Parse *pParse, int iStart, int iCount){
1604 int iEnd = iStart + iCount - 1;
drhe55cbd72008-03-31 23:48:03 +00001605 int i;
1606 for(i=0; i<pParse->nColCache; i++){
1607 int r = pParse->aColCache[i].iReg;
drhda250ea2008-04-01 05:07:14 +00001608 if( r>=iStart && r<=iEnd ){
1609 pParse->aColCache[i].affChange = 1;
drhe55cbd72008-03-31 23:48:03 +00001610 }
1611 }
drhe55cbd72008-03-31 23:48:03 +00001612}
1613
1614/*
drhb21e7c72008-06-22 12:37:57 +00001615** Generate code to move content from registers iFrom...iFrom+nReg-1
1616** over to iTo..iTo+nReg-1. Keep the column cache up-to-date.
drhe55cbd72008-03-31 23:48:03 +00001617*/
drhb21e7c72008-06-22 12:37:57 +00001618void sqlite3ExprCodeMove(Parse *pParse, int iFrom, int iTo, int nReg){
drhe55cbd72008-03-31 23:48:03 +00001619 int i;
1620 if( iFrom==iTo ) return;
drhb21e7c72008-06-22 12:37:57 +00001621 sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move, iFrom, iTo, nReg);
drhe55cbd72008-03-31 23:48:03 +00001622 for(i=0; i<pParse->nColCache; i++){
drhb21e7c72008-06-22 12:37:57 +00001623 int x = pParse->aColCache[i].iReg;
1624 if( x>=iFrom && x<iFrom+nReg ){
1625 pParse->aColCache[i].iReg += iTo-iFrom;
drhe55cbd72008-03-31 23:48:03 +00001626 }
1627 }
drh945498f2007-02-24 11:52:52 +00001628}
1629
drhfec19aa2004-05-19 20:41:03 +00001630/*
drh92b01d52008-06-24 00:32:35 +00001631** Generate code to copy content from registers iFrom...iFrom+nReg-1
1632** over to iTo..iTo+nReg-1.
1633*/
1634void sqlite3ExprCodeCopy(Parse *pParse, int iFrom, int iTo, int nReg){
1635 int i;
1636 if( iFrom==iTo ) return;
1637 for(i=0; i<nReg; i++){
1638 sqlite3VdbeAddOp2(pParse->pVdbe, OP_Copy, iFrom+i, iTo+i);
1639 }
1640}
1641
1642/*
drh652fbf52008-04-01 01:42:41 +00001643** Return true if any register in the range iFrom..iTo (inclusive)
1644** is used as part of the column cache.
1645*/
1646static int usedAsColumnCache(Parse *pParse, int iFrom, int iTo){
1647 int i;
1648 for(i=0; i<pParse->nColCache; i++){
1649 int r = pParse->aColCache[i].iReg;
1650 if( r>=iFrom && r<=iTo ) return 1;
1651 }
1652 return 0;
1653}
1654
1655/*
1656** Theres is a value in register iCurrent. We ultimately want
1657** the value to be in register iTarget. It might be that
1658** iCurrent and iTarget are the same register.
1659**
1660** We are going to modify the value, so we need to make sure it
1661** is not a cached register. If iCurrent is a cached register,
1662** then try to move the value over to iTarget. If iTarget is a
1663** cached register, then clear the corresponding cache line.
1664**
1665** Return the register that the value ends up in.
1666*/
1667int sqlite3ExprWritableRegister(Parse *pParse, int iCurrent, int iTarget){
drhda250ea2008-04-01 05:07:14 +00001668 int i;
drh652fbf52008-04-01 01:42:41 +00001669 assert( pParse->pVdbe!=0 );
1670 if( !usedAsColumnCache(pParse, iCurrent, iCurrent) ){
1671 return iCurrent;
1672 }
drh2f7794c2008-04-01 03:27:39 +00001673 if( iCurrent!=iTarget ){
1674 sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, iCurrent, iTarget);
1675 }
drhda250ea2008-04-01 05:07:14 +00001676 for(i=0; i<pParse->nColCache; i++){
1677 if( pParse->aColCache[i].iReg==iTarget ){
1678 pParse->aColCache[i] = pParse->aColCache[--pParse->nColCache];
1679 pParse->iColCache = pParse->nColCache;
1680 }
1681 }
drh652fbf52008-04-01 01:42:41 +00001682 return iTarget;
1683}
1684
1685/*
drh191b54c2008-04-15 12:14:21 +00001686** If the last instruction coded is an ephemeral copy of any of
1687** the registers in the nReg registers beginning with iReg, then
1688** convert the last instruction from OP_SCopy to OP_Copy.
1689*/
1690void sqlite3ExprHardCopy(Parse *pParse, int iReg, int nReg){
1691 int addr;
1692 VdbeOp *pOp;
1693 Vdbe *v;
1694
1695 v = pParse->pVdbe;
1696 addr = sqlite3VdbeCurrentAddr(v);
1697 pOp = sqlite3VdbeGetOp(v, addr-1);
danielk1977d7eb2ed2008-04-24 12:36:35 +00001698 assert( pOp || pParse->db->mallocFailed );
1699 if( pOp && pOp->opcode==OP_SCopy && pOp->p1>=iReg && pOp->p1<iReg+nReg ){
drh191b54c2008-04-15 12:14:21 +00001700 pOp->opcode = OP_Copy;
1701 }
1702}
1703
1704/*
drh8b213892008-08-29 02:14:02 +00001705** Generate code to store the value of the iAlias-th alias in register
1706** target. The first time this is called, pExpr is evaluated to compute
1707** the value of the alias. The value is stored in an auxiliary register
1708** and the number of that register is returned. On subsequent calls,
1709** the register number is returned without generating any code.
1710**
1711** Note that in order for this to work, code must be generated in the
1712** same order that it is executed.
1713**
1714** Aliases are numbered starting with 1. So iAlias is in the range
1715** of 1 to pParse->nAlias inclusive.
1716**
1717** pParse->aAlias[iAlias-1] records the register number where the value
1718** of the iAlias-th alias is stored. If zero, that means that the
1719** alias has not yet been computed.
1720*/
1721static int codeAlias(Parse *pParse, int iAlias, Expr *pExpr){
1722 sqlite3 *db = pParse->db;
1723 int iReg;
1724 if( pParse->aAlias==0 ){
1725 pParse->aAlias = sqlite3DbMallocZero(db,
1726 sizeof(pParse->aAlias[0])*pParse->nAlias );
1727 if( db->mallocFailed ) return 0;
1728 }
1729 assert( iAlias>0 && iAlias<=pParse->nAlias );
1730 iReg = pParse->aAlias[iAlias-1];
1731 if( iReg==0 ){
1732 iReg = ++pParse->nMem;
1733 sqlite3ExprCode(pParse, pExpr, iReg);
1734 pParse->aAlias[iAlias-1] = iReg;
1735 }
1736 return iReg;
1737}
1738
1739/*
drhcce7d172000-05-31 15:34:51 +00001740** Generate code into the current Vdbe to evaluate the given
drh2dcef112008-01-12 19:03:48 +00001741** expression. Attempt to store the results in register "target".
1742** Return the register where results are stored.
drh389a1ad2008-01-03 23:44:53 +00001743**
drh8b213892008-08-29 02:14:02 +00001744** With this routine, there is no guarantee that results will
drh2dcef112008-01-12 19:03:48 +00001745** be stored in target. The result might be stored in some other
1746** register if it is convenient to do so. The calling function
1747** must check the return code and move the results to the desired
1748** register.
drhcce7d172000-05-31 15:34:51 +00001749*/
drh678ccce2008-03-31 18:19:54 +00001750int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){
drh2dcef112008-01-12 19:03:48 +00001751 Vdbe *v = pParse->pVdbe; /* The VM under construction */
1752 int op; /* The opcode being coded */
1753 int inReg = target; /* Results stored in register inReg */
1754 int regFree1 = 0; /* If non-zero free this temporary register */
1755 int regFree2 = 0; /* If non-zero free this temporary register */
drh678ccce2008-03-31 18:19:54 +00001756 int r1, r2, r3, r4; /* Various register numbers */
drh8b213892008-08-29 02:14:02 +00001757 sqlite3 *db;
drhffe07b22005-11-03 00:41:17 +00001758
drh8b213892008-08-29 02:14:02 +00001759 db = pParse->db;
1760 assert( v!=0 || db->mallocFailed );
drh9cbf3422008-01-17 16:22:13 +00001761 assert( target>0 && target<=pParse->nMem );
drh389a1ad2008-01-03 23:44:53 +00001762 if( v==0 ) return 0;
drh389a1ad2008-01-03 23:44:53 +00001763
1764 if( pExpr==0 ){
1765 op = TK_NULL;
1766 }else{
1767 op = pExpr->op;
1768 }
drhf2bc0132004-10-04 13:19:23 +00001769 switch( op ){
drh13449892005-09-07 21:22:45 +00001770 case TK_AGG_COLUMN: {
1771 AggInfo *pAggInfo = pExpr->pAggInfo;
1772 struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg];
1773 if( !pAggInfo->directMode ){
drh9de221d2008-01-05 06:51:30 +00001774 assert( pCol->iMem>0 );
1775 inReg = pCol->iMem;
drh13449892005-09-07 21:22:45 +00001776 break;
1777 }else if( pAggInfo->useSortingIdx ){
drh389a1ad2008-01-03 23:44:53 +00001778 sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdx,
1779 pCol->iSorterColumn, target);
drh13449892005-09-07 21:22:45 +00001780 break;
1781 }
1782 /* Otherwise, fall thru into the TK_COLUMN case */
1783 }
drh967e8b72000-06-21 13:59:10 +00001784 case TK_COLUMN: {
drhffe07b22005-11-03 00:41:17 +00001785 if( pExpr->iTable<0 ){
1786 /* This only happens when coding check constraints */
drhaa9b8962008-01-08 02:57:55 +00001787 assert( pParse->ckBase>0 );
1788 inReg = pExpr->iColumn + pParse->ckBase;
drhc4a3c772001-04-04 11:48:57 +00001789 }else{
drhc5499be2008-04-01 15:06:33 +00001790 testcase( (pExpr->flags & EP_AnyAff)!=0 );
drhe55cbd72008-03-31 23:48:03 +00001791 inReg = sqlite3ExprCodeGetColumn(pParse, pExpr->pTab,
drhda250ea2008-04-01 05:07:14 +00001792 pExpr->iColumn, pExpr->iTable, target,
1793 pExpr->flags & EP_AnyAff);
drh22827922000-06-06 17:27:05 +00001794 }
drhcce7d172000-05-31 15:34:51 +00001795 break;
1796 }
1797 case TK_INTEGER: {
drh92b01d52008-06-24 00:32:35 +00001798 codeInteger(v, pExpr, 0, target);
drhfec19aa2004-05-19 20:41:03 +00001799 break;
1800 }
drh598f1342007-10-23 15:39:45 +00001801 case TK_FLOAT: {
drh9de221d2008-01-05 06:51:30 +00001802 codeReal(v, (char*)pExpr->token.z, pExpr->token.n, 0, target);
drh598f1342007-10-23 15:39:45 +00001803 break;
1804 }
drhfec19aa2004-05-19 20:41:03 +00001805 case TK_STRING: {
drh8b213892008-08-29 02:14:02 +00001806 sqlite3DequoteExpr(db, pExpr);
drh9de221d2008-01-05 06:51:30 +00001807 sqlite3VdbeAddOp4(v,OP_String8, 0, target, 0,
drh66a51672008-01-03 00:01:23 +00001808 (char*)pExpr->token.z, pExpr->token.n);
drhcce7d172000-05-31 15:34:51 +00001809 break;
1810 }
drhf0863fe2005-06-12 21:35:51 +00001811 case TK_NULL: {
drh9de221d2008-01-05 06:51:30 +00001812 sqlite3VdbeAddOp2(v, OP_Null, 0, target);
drhf0863fe2005-06-12 21:35:51 +00001813 break;
1814 }
danielk19775338a5f2005-01-20 13:03:10 +00001815#ifndef SQLITE_OMIT_BLOB_LITERAL
danielk1977c572ef72004-05-27 09:28:41 +00001816 case TK_BLOB: {
drh6c8c6ce2005-08-23 11:17:58 +00001817 int n;
1818 const char *z;
drhca48c902008-01-18 14:08:24 +00001819 char *zBlob;
1820 assert( pExpr->token.n>=3 );
1821 assert( pExpr->token.z[0]=='x' || pExpr->token.z[0]=='X' );
1822 assert( pExpr->token.z[1]=='\'' );
1823 assert( pExpr->token.z[pExpr->token.n-1]=='\'' );
drh6c8c6ce2005-08-23 11:17:58 +00001824 n = pExpr->token.n - 3;
drh2646da72005-12-09 20:02:05 +00001825 z = (char*)pExpr->token.z + 2;
drhca48c902008-01-18 14:08:24 +00001826 zBlob = sqlite3HexToBlob(sqlite3VdbeDb(v), z, n);
1827 sqlite3VdbeAddOp4(v, OP_Blob, n/2, target, 0, zBlob, P4_DYNAMIC);
danielk1977c572ef72004-05-27 09:28:41 +00001828 break;
1829 }
danielk19775338a5f2005-01-20 13:03:10 +00001830#endif
drh50457892003-09-06 01:10:47 +00001831 case TK_VARIABLE: {
drh9de221d2008-01-05 06:51:30 +00001832 sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iTable, target);
drh895d7472004-08-20 16:02:39 +00001833 if( pExpr->token.n>1 ){
drh66a51672008-01-03 00:01:23 +00001834 sqlite3VdbeChangeP4(v, -1, (char*)pExpr->token.z, pExpr->token.n);
drh895d7472004-08-20 16:02:39 +00001835 }
drh50457892003-09-06 01:10:47 +00001836 break;
1837 }
drh4e0cff62004-11-05 05:10:28 +00001838 case TK_REGISTER: {
drh9de221d2008-01-05 06:51:30 +00001839 inReg = pExpr->iTable;
drh4e0cff62004-11-05 05:10:28 +00001840 break;
1841 }
drh8b213892008-08-29 02:14:02 +00001842 case TK_AS: {
1843 inReg = codeAlias(pParse, pExpr->iTable, pExpr->pLeft);
1844 break;
1845 }
drh487e2622005-06-25 18:42:14 +00001846#ifndef SQLITE_OMIT_CAST
1847 case TK_CAST: {
1848 /* Expressions of the form: CAST(pLeft AS token) */
danielk1977f0113002006-01-24 12:09:17 +00001849 int aff, to_op;
drh2dcef112008-01-12 19:03:48 +00001850 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
drh8a512562005-11-14 22:29:05 +00001851 aff = sqlite3AffinityType(&pExpr->token);
danielk1977f0113002006-01-24 12:09:17 +00001852 to_op = aff - SQLITE_AFF_TEXT + OP_ToText;
1853 assert( to_op==OP_ToText || aff!=SQLITE_AFF_TEXT );
1854 assert( to_op==OP_ToBlob || aff!=SQLITE_AFF_NONE );
1855 assert( to_op==OP_ToNumeric || aff!=SQLITE_AFF_NUMERIC );
1856 assert( to_op==OP_ToInt || aff!=SQLITE_AFF_INTEGER );
1857 assert( to_op==OP_ToReal || aff!=SQLITE_AFF_REAL );
drhc5499be2008-04-01 15:06:33 +00001858 testcase( to_op==OP_ToText );
1859 testcase( to_op==OP_ToBlob );
1860 testcase( to_op==OP_ToNumeric );
1861 testcase( to_op==OP_ToInt );
1862 testcase( to_op==OP_ToReal );
drh2dcef112008-01-12 19:03:48 +00001863 sqlite3VdbeAddOp1(v, to_op, inReg);
drhc5499be2008-04-01 15:06:33 +00001864 testcase( usedAsColumnCache(pParse, inReg, inReg) );
drhb3843a82008-04-01 12:24:11 +00001865 sqlite3ExprCacheAffinityChange(pParse, inReg, 1);
drh487e2622005-06-25 18:42:14 +00001866 break;
1867 }
1868#endif /* SQLITE_OMIT_CAST */
drhc9b84a12002-06-20 11:36:48 +00001869 case TK_LT:
1870 case TK_LE:
1871 case TK_GT:
1872 case TK_GE:
1873 case TK_NE:
1874 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00001875 assert( TK_LT==OP_Lt );
1876 assert( TK_LE==OP_Le );
1877 assert( TK_GT==OP_Gt );
1878 assert( TK_GE==OP_Ge );
1879 assert( TK_EQ==OP_Eq );
1880 assert( TK_NE==OP_Ne );
drhc5499be2008-04-01 15:06:33 +00001881 testcase( op==TK_LT );
1882 testcase( op==TK_LE );
1883 testcase( op==TK_GT );
1884 testcase( op==TK_GE );
1885 testcase( op==TK_EQ );
1886 testcase( op==TK_NE );
drhda250ea2008-04-01 05:07:14 +00001887 codeCompareOperands(pParse, pExpr->pLeft, &r1, &regFree1,
1888 pExpr->pRight, &r2, &regFree2);
drh35573352008-01-08 23:54:25 +00001889 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
1890 r1, r2, inReg, SQLITE_STOREP2);
drhc5499be2008-04-01 15:06:33 +00001891 testcase( regFree1==0 );
1892 testcase( regFree2==0 );
danielk1977a37cdde2004-05-16 11:15:36 +00001893 break;
drhc9b84a12002-06-20 11:36:48 +00001894 }
drhcce7d172000-05-31 15:34:51 +00001895 case TK_AND:
1896 case TK_OR:
1897 case TK_PLUS:
1898 case TK_STAR:
1899 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00001900 case TK_REM:
1901 case TK_BITAND:
1902 case TK_BITOR:
drh17c40292004-07-21 02:53:29 +00001903 case TK_SLASH:
drhbf4133c2001-10-13 02:59:08 +00001904 case TK_LSHIFT:
drh855eb1c2004-08-31 13:45:11 +00001905 case TK_RSHIFT:
drh00400772000-06-16 20:51:26 +00001906 case TK_CONCAT: {
drhf2bc0132004-10-04 13:19:23 +00001907 assert( TK_AND==OP_And );
1908 assert( TK_OR==OP_Or );
1909 assert( TK_PLUS==OP_Add );
1910 assert( TK_MINUS==OP_Subtract );
1911 assert( TK_REM==OP_Remainder );
1912 assert( TK_BITAND==OP_BitAnd );
1913 assert( TK_BITOR==OP_BitOr );
1914 assert( TK_SLASH==OP_Divide );
1915 assert( TK_LSHIFT==OP_ShiftLeft );
1916 assert( TK_RSHIFT==OP_ShiftRight );
1917 assert( TK_CONCAT==OP_Concat );
drhc5499be2008-04-01 15:06:33 +00001918 testcase( op==TK_AND );
1919 testcase( op==TK_OR );
1920 testcase( op==TK_PLUS );
1921 testcase( op==TK_MINUS );
1922 testcase( op==TK_REM );
1923 testcase( op==TK_BITAND );
1924 testcase( op==TK_BITOR );
1925 testcase( op==TK_SLASH );
1926 testcase( op==TK_LSHIFT );
1927 testcase( op==TK_RSHIFT );
1928 testcase( op==TK_CONCAT );
drh2dcef112008-01-12 19:03:48 +00001929 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
1930 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
drh5b6afba2008-01-05 16:29:28 +00001931 sqlite3VdbeAddOp3(v, op, r2, r1, target);
drhc5499be2008-04-01 15:06:33 +00001932 testcase( regFree1==0 );
1933 testcase( regFree2==0 );
drh00400772000-06-16 20:51:26 +00001934 break;
1935 }
drhcce7d172000-05-31 15:34:51 +00001936 case TK_UMINUS: {
drhfec19aa2004-05-19 20:41:03 +00001937 Expr *pLeft = pExpr->pLeft;
1938 assert( pLeft );
1939 if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
drhfec19aa2004-05-19 20:41:03 +00001940 if( pLeft->op==TK_FLOAT ){
drh92b01d52008-06-24 00:32:35 +00001941 codeReal(v, (char*)pLeft->token.z, pLeft->token.n, 1, target);
drhe6840902002-03-06 03:08:25 +00001942 }else{
drh92b01d52008-06-24 00:32:35 +00001943 codeInteger(v, pLeft, 1, target);
drhe6840902002-03-06 03:08:25 +00001944 }
drh3c84ddf2008-01-09 02:15:38 +00001945 }else{
drh2dcef112008-01-12 19:03:48 +00001946 regFree1 = r1 = sqlite3GetTempReg(pParse);
drh3c84ddf2008-01-09 02:15:38 +00001947 sqlite3VdbeAddOp2(v, OP_Integer, 0, r1);
drhe55cbd72008-03-31 23:48:03 +00001948 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree2);
drh2dcef112008-01-12 19:03:48 +00001949 sqlite3VdbeAddOp3(v, OP_Subtract, r2, r1, target);
drhc5499be2008-04-01 15:06:33 +00001950 testcase( regFree2==0 );
drh6e142f52000-06-08 13:36:40 +00001951 }
drh3c84ddf2008-01-09 02:15:38 +00001952 inReg = target;
1953 break;
drh6e142f52000-06-08 13:36:40 +00001954 }
drhbf4133c2001-10-13 02:59:08 +00001955 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00001956 case TK_NOT: {
drhf2bc0132004-10-04 13:19:23 +00001957 assert( TK_BITNOT==OP_BitNot );
1958 assert( TK_NOT==OP_Not );
drhc5499be2008-04-01 15:06:33 +00001959 testcase( op==TK_BITNOT );
1960 testcase( op==TK_NOT );
drh2dcef112008-01-12 19:03:48 +00001961 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
drhc5499be2008-04-01 15:06:33 +00001962 testcase( inReg==target );
1963 testcase( usedAsColumnCache(pParse, inReg, inReg) );
drh652fbf52008-04-01 01:42:41 +00001964 inReg = sqlite3ExprWritableRegister(pParse, inReg, target);
drh2dcef112008-01-12 19:03:48 +00001965 sqlite3VdbeAddOp1(v, op, inReg);
drhcce7d172000-05-31 15:34:51 +00001966 break;
1967 }
1968 case TK_ISNULL:
1969 case TK_NOTNULL: {
drh6a288a32008-01-07 19:20:24 +00001970 int addr;
drhf2bc0132004-10-04 13:19:23 +00001971 assert( TK_ISNULL==OP_IsNull );
1972 assert( TK_NOTNULL==OP_NotNull );
drhc5499be2008-04-01 15:06:33 +00001973 testcase( op==TK_ISNULL );
1974 testcase( op==TK_NOTNULL );
drh9de221d2008-01-05 06:51:30 +00001975 sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
drh2dcef112008-01-12 19:03:48 +00001976 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
drhc5499be2008-04-01 15:06:33 +00001977 testcase( regFree1==0 );
drh2dcef112008-01-12 19:03:48 +00001978 addr = sqlite3VdbeAddOp1(v, op, r1);
drh9de221d2008-01-05 06:51:30 +00001979 sqlite3VdbeAddOp2(v, OP_AddImm, target, -1);
drh6a288a32008-01-07 19:20:24 +00001980 sqlite3VdbeJumpHere(v, addr);
drhf2bc0132004-10-04 13:19:23 +00001981 break;
drhcce7d172000-05-31 15:34:51 +00001982 }
drh22827922000-06-06 17:27:05 +00001983 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00001984 AggInfo *pInfo = pExpr->pAggInfo;
drh7e56e712005-11-16 12:53:15 +00001985 if( pInfo==0 ){
1986 sqlite3ErrorMsg(pParse, "misuse of aggregate: %T",
1987 &pExpr->span);
1988 }else{
drh9de221d2008-01-05 06:51:30 +00001989 inReg = pInfo->aFunc[pExpr->iAgg].iMem;
drh7e56e712005-11-16 12:53:15 +00001990 }
drh22827922000-06-06 17:27:05 +00001991 break;
1992 }
drhb71090f2005-05-23 17:26:51 +00001993 case TK_CONST_FUNC:
drhcce7d172000-05-31 15:34:51 +00001994 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00001995 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00001996 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00001997 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00001998 int nId;
1999 const char *zId;
drh13449892005-09-07 21:22:45 +00002000 int constMask = 0;
danielk1977682f68b2004-06-05 10:22:17 +00002001 int i;
drh17435752007-08-16 04:30:38 +00002002 u8 enc = ENC(db);
danielk1977dc1bdc42004-06-11 10:51:27 +00002003 CollSeq *pColl = 0;
drh17435752007-08-16 04:30:38 +00002004
drhc5499be2008-04-01 15:06:33 +00002005 testcase( op==TK_CONST_FUNC );
2006 testcase( op==TK_FUNCTION );
drh2646da72005-12-09 20:02:05 +00002007 zId = (char*)pExpr->token.z;
drhb71090f2005-05-23 17:26:51 +00002008 nId = pExpr->token.n;
drh8b213892008-08-29 02:14:02 +00002009 pDef = sqlite3FindFunction(db, zId, nId, nExpr, enc, 0);
drh0bce8352002-02-28 00:41:10 +00002010 assert( pDef!=0 );
drh892d3172008-01-10 03:46:36 +00002011 if( pList ){
2012 nExpr = pList->nExpr;
drh2dcef112008-01-12 19:03:48 +00002013 r1 = sqlite3GetTempRange(pParse, nExpr);
drh191b54c2008-04-15 12:14:21 +00002014 sqlite3ExprCodeExprList(pParse, pList, r1, 1);
drh892d3172008-01-10 03:46:36 +00002015 }else{
drhd847eaa2008-01-17 17:15:56 +00002016 nExpr = r1 = 0;
drh892d3172008-01-10 03:46:36 +00002017 }
drhb7f6f682006-07-08 17:06:43 +00002018#ifndef SQLITE_OMIT_VIRTUALTABLE
drha43fa222006-07-08 18:41:37 +00002019 /* Possibly overload the function if the first argument is
2020 ** a virtual table column.
2021 **
2022 ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the
2023 ** second argument, not the first, as the argument to test to
2024 ** see if it is a column in a virtual table. This is done because
2025 ** the left operand of infix functions (the operand we want to
2026 ** control overloading) ends up as the second argument to the
2027 ** function. The expression "A glob B" is equivalent to
2028 ** "glob(B,A). We want to use the A in "A glob B" to test
2029 ** for function overloading. But we use the B term in "glob(B,A)".
2030 */
drh6a03a1c2006-07-08 18:34:59 +00002031 if( nExpr>=2 && (pExpr->flags & EP_InfixFunc) ){
drh17435752007-08-16 04:30:38 +00002032 pDef = sqlite3VtabOverloadFunction(db, pDef, nExpr, pList->a[1].pExpr);
drh6a03a1c2006-07-08 18:34:59 +00002033 }else if( nExpr>0 ){
drh17435752007-08-16 04:30:38 +00002034 pDef = sqlite3VtabOverloadFunction(db, pDef, nExpr, pList->a[0].pExpr);
drhb7f6f682006-07-08 17:06:43 +00002035 }
2036#endif
danielk1977682f68b2004-06-05 10:22:17 +00002037 for(i=0; i<nExpr && i<32; i++){
danielk1977d02eb1f2004-06-06 09:44:03 +00002038 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
drh13449892005-09-07 21:22:45 +00002039 constMask |= (1<<i);
danielk1977d02eb1f2004-06-06 09:44:03 +00002040 }
drhe82f5d02008-10-07 19:53:14 +00002041 if( (pDef->flags & SQLITE_FUNC_NEEDCOLL)!=0 && !pColl ){
danielk1977dc1bdc42004-06-11 10:51:27 +00002042 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
2043 }
2044 }
drhe82f5d02008-10-07 19:53:14 +00002045 if( pDef->flags & SQLITE_FUNC_NEEDCOLL ){
drh8b213892008-08-29 02:14:02 +00002046 if( !pColl ) pColl = db->pDfltColl;
drh66a51672008-01-03 00:01:23 +00002047 sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ);
danielk1977682f68b2004-06-05 10:22:17 +00002048 }
drh2dcef112008-01-12 19:03:48 +00002049 sqlite3VdbeAddOp4(v, OP_Function, constMask, r1, target,
drh66a51672008-01-03 00:01:23 +00002050 (char*)pDef, P4_FUNCDEF);
drh98757152008-01-09 23:04:12 +00002051 sqlite3VdbeChangeP5(v, nExpr);
drh2dcef112008-01-12 19:03:48 +00002052 if( nExpr ){
2053 sqlite3ReleaseTempRange(pParse, r1, nExpr);
2054 }
drhda250ea2008-04-01 05:07:14 +00002055 sqlite3ExprCacheAffinityChange(pParse, r1, nExpr);
drhcce7d172000-05-31 15:34:51 +00002056 break;
2057 }
drhfe2093d2005-01-20 22:48:47 +00002058#ifndef SQLITE_OMIT_SUBQUERY
2059 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00002060 case TK_SELECT: {
drhc5499be2008-04-01 15:06:33 +00002061 testcase( op==TK_EXISTS );
2062 testcase( op==TK_SELECT );
drh41714d62006-03-02 04:44:23 +00002063 if( pExpr->iColumn==0 ){
danielk197741a05b72008-10-02 13:50:55 +00002064 sqlite3CodeSubselect(pParse, pExpr, 0, 0);
drh41714d62006-03-02 04:44:23 +00002065 }
drh9de221d2008-01-05 06:51:30 +00002066 inReg = pExpr->iColumn;
drh19a775c2000-06-05 18:54:46 +00002067 break;
2068 }
drhfef52082000-06-06 01:50:43 +00002069 case TK_IN: {
danielk19770cdc0222008-06-26 18:04:03 +00002070 int rNotFound = 0;
2071 int rMayHaveNull = 0;
drh6fccc352008-06-27 00:52:45 +00002072 int j2, j3, j4, j5;
drh94a11212004-09-25 13:12:14 +00002073 char affinity;
danielk19779a96b662007-11-29 17:05:18 +00002074 int eType;
danielk19779a96b662007-11-29 17:05:18 +00002075
drh3c31fc22008-06-26 21:45:26 +00002076 VdbeNoopComment((v, "begin IN expr r%d", target));
danielk19770cdc0222008-06-26 18:04:03 +00002077 eType = sqlite3FindInIndex(pParse, pExpr, &rMayHaveNull);
2078 if( rMayHaveNull ){
2079 rNotFound = ++pParse->nMem;
2080 }
danielk1977e014a832004-05-17 10:48:57 +00002081
2082 /* Figure out the affinity to use to create a key from the results
2083 ** of the expression. affinityStr stores a static string suitable for
drh66a51672008-01-03 00:01:23 +00002084 ** P4 of OP_MakeRecord.
danielk1977e014a832004-05-17 10:48:57 +00002085 */
drh94a11212004-09-25 13:12:14 +00002086 affinity = comparisonAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +00002087
danielk1977e014a832004-05-17 10:48:57 +00002088
2089 /* Code the <expr> from "<expr> IN (...)". The temporary table
2090 ** pExpr->iTable contains the values that make up the (...) set.
2091 */
drh66ba23c2008-06-27 00:47:28 +00002092 pParse->disableColCache++;
2093 sqlite3ExprCode(pParse, pExpr->pLeft, target);
2094 pParse->disableColCache--;
2095 j2 = sqlite3VdbeAddOp1(v, OP_IsNull, target);
danielk19779a96b662007-11-29 17:05:18 +00002096 if( eType==IN_INDEX_ROWID ){
drh66ba23c2008-06-27 00:47:28 +00002097 j3 = sqlite3VdbeAddOp1(v, OP_MustBeInt, target);
2098 j4 = sqlite3VdbeAddOp3(v, OP_NotExists, pExpr->iTable, 0, target);
2099 sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
drh6a288a32008-01-07 19:20:24 +00002100 j5 = sqlite3VdbeAddOp0(v, OP_Goto);
2101 sqlite3VdbeJumpHere(v, j3);
2102 sqlite3VdbeJumpHere(v, j4);
danielk19770cdc0222008-06-26 18:04:03 +00002103 sqlite3VdbeAddOp2(v, OP_Integer, 0, target);
danielk19779a96b662007-11-29 17:05:18 +00002104 }else{
drh2dcef112008-01-12 19:03:48 +00002105 r2 = regFree2 = sqlite3GetTempReg(pParse);
danielk19770cdc0222008-06-26 18:04:03 +00002106
2107 /* Create a record and test for set membership. If the set contains
2108 ** the value, then jump to the end of the test code. The target
2109 ** register still contains the true (1) value written to it earlier.
2110 */
drh66ba23c2008-06-27 00:47:28 +00002111 sqlite3VdbeAddOp4(v, OP_MakeRecord, target, 1, r2, &affinity, 1);
2112 sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
drh2dcef112008-01-12 19:03:48 +00002113 j5 = sqlite3VdbeAddOp3(v, OP_Found, pExpr->iTable, 0, r2);
danielk19770cdc0222008-06-26 18:04:03 +00002114
2115 /* If the set membership test fails, then the result of the
2116 ** "x IN (...)" expression must be either 0 or NULL. If the set
2117 ** contains no NULL values, then the result is 0. If the set
2118 ** contains one or more NULL values, then the result of the
2119 ** expression is also NULL.
2120 */
2121 if( rNotFound==0 ){
2122 /* This branch runs if it is known at compile time (now) that
2123 ** the set contains no NULL values. This happens as the result
2124 ** of a "NOT NULL" constraint in the database schema. No need
2125 ** to test the data structure at runtime in this case.
2126 */
2127 sqlite3VdbeAddOp2(v, OP_Integer, 0, target);
2128 }else{
2129 /* This block populates the rNotFound register with either NULL
2130 ** or 0 (an integer value). If the data structure contains one
2131 ** or more NULLs, then set rNotFound to NULL. Otherwise, set it
2132 ** to 0. If register rMayHaveNull is already set to some value
2133 ** other than NULL, then the test has already been run and
2134 ** rNotFound is already populated.
2135 */
drh66ba23c2008-06-27 00:47:28 +00002136 static const char nullRecord[] = { 0x02, 0x00 };
danielk19770cdc0222008-06-26 18:04:03 +00002137 j3 = sqlite3VdbeAddOp1(v, OP_NotNull, rMayHaveNull);
2138 sqlite3VdbeAddOp2(v, OP_Null, 0, rNotFound);
drh66ba23c2008-06-27 00:47:28 +00002139 sqlite3VdbeAddOp4(v, OP_Blob, 2, rMayHaveNull, 0,
2140 nullRecord, P4_STATIC);
2141 j4 = sqlite3VdbeAddOp3(v, OP_Found, pExpr->iTable, 0, rMayHaveNull);
danielk19770cdc0222008-06-26 18:04:03 +00002142 sqlite3VdbeAddOp2(v, OP_Integer, 0, rNotFound);
2143 sqlite3VdbeJumpHere(v, j4);
2144 sqlite3VdbeJumpHere(v, j3);
2145
2146 /* Copy the value of register rNotFound (which is either NULL or 0)
danielk197741a05b72008-10-02 13:50:55 +00002147 ** into the target register. This will be the result of the
danielk19770cdc0222008-06-26 18:04:03 +00002148 ** expression.
2149 */
2150 sqlite3VdbeAddOp2(v, OP_Copy, rNotFound, target);
2151 }
danielk19779a96b662007-11-29 17:05:18 +00002152 }
drh6a288a32008-01-07 19:20:24 +00002153 sqlite3VdbeJumpHere(v, j2);
2154 sqlite3VdbeJumpHere(v, j5);
drh3c31fc22008-06-26 21:45:26 +00002155 VdbeComment((v, "end IN expr r%d", target));
drhfef52082000-06-06 01:50:43 +00002156 break;
2157 }
danielk197793758c82005-01-21 08:13:14 +00002158#endif
drh2dcef112008-01-12 19:03:48 +00002159 /*
2160 ** x BETWEEN y AND z
2161 **
2162 ** This is equivalent to
2163 **
2164 ** x>=y AND x<=z
2165 **
2166 ** X is stored in pExpr->pLeft.
2167 ** Y is stored in pExpr->pList->a[0].pExpr.
2168 ** Z is stored in pExpr->pList->a[1].pExpr.
2169 */
drhfef52082000-06-06 01:50:43 +00002170 case TK_BETWEEN: {
drhbe5c89a2004-07-26 00:31:09 +00002171 Expr *pLeft = pExpr->pLeft;
2172 struct ExprList_item *pLItem = pExpr->pList->a;
2173 Expr *pRight = pLItem->pExpr;
drh35573352008-01-08 23:54:25 +00002174
drhda250ea2008-04-01 05:07:14 +00002175 codeCompareOperands(pParse, pLeft, &r1, &regFree1,
2176 pRight, &r2, &regFree2);
drhc5499be2008-04-01 15:06:33 +00002177 testcase( regFree1==0 );
2178 testcase( regFree2==0 );
drh2dcef112008-01-12 19:03:48 +00002179 r3 = sqlite3GetTempReg(pParse);
drh678ccce2008-03-31 18:19:54 +00002180 r4 = sqlite3GetTempReg(pParse);
drh35573352008-01-08 23:54:25 +00002181 codeCompare(pParse, pLeft, pRight, OP_Ge,
2182 r1, r2, r3, SQLITE_STOREP2);
drhbe5c89a2004-07-26 00:31:09 +00002183 pLItem++;
2184 pRight = pLItem->pExpr;
drh2dcef112008-01-12 19:03:48 +00002185 sqlite3ReleaseTempReg(pParse, regFree2);
2186 r2 = sqlite3ExprCodeTemp(pParse, pRight, &regFree2);
drhc5499be2008-04-01 15:06:33 +00002187 testcase( regFree2==0 );
drh678ccce2008-03-31 18:19:54 +00002188 codeCompare(pParse, pLeft, pRight, OP_Le, r1, r2, r4, SQLITE_STOREP2);
2189 sqlite3VdbeAddOp3(v, OP_And, r3, r4, target);
drh2dcef112008-01-12 19:03:48 +00002190 sqlite3ReleaseTempReg(pParse, r3);
drh678ccce2008-03-31 18:19:54 +00002191 sqlite3ReleaseTempReg(pParse, r4);
drhfef52082000-06-06 01:50:43 +00002192 break;
2193 }
drh4f07e5f2007-05-14 11:34:46 +00002194 case TK_UPLUS: {
drh2dcef112008-01-12 19:03:48 +00002195 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
drha2e00042002-01-22 03:13:42 +00002196 break;
2197 }
drh2dcef112008-01-12 19:03:48 +00002198
2199 /*
2200 ** Form A:
2201 ** CASE x WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
2202 **
2203 ** Form B:
2204 ** CASE WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
2205 **
2206 ** Form A is can be transformed into the equivalent form B as follows:
2207 ** CASE WHEN x=e1 THEN r1 WHEN x=e2 THEN r2 ...
2208 ** WHEN x=eN THEN rN ELSE y END
2209 **
2210 ** X (if it exists) is in pExpr->pLeft.
2211 ** Y is in pExpr->pRight. The Y is also optional. If there is no
2212 ** ELSE clause and no other term matches, then the result of the
2213 ** exprssion is NULL.
2214 ** Ei is in pExpr->pList->a[i*2] and Ri is pExpr->pList->a[i*2+1].
2215 **
2216 ** The result of the expression is the Ri for the first matching Ei,
2217 ** or if there is no matching Ei, the ELSE term Y, or if there is
2218 ** no ELSE term, NULL.
2219 */
drh17a7f8d2002-03-24 13:13:27 +00002220 case TK_CASE: {
drh2dcef112008-01-12 19:03:48 +00002221 int endLabel; /* GOTO label for end of CASE stmt */
2222 int nextCase; /* GOTO label for next WHEN clause */
2223 int nExpr; /* 2x number of WHEN terms */
2224 int i; /* Loop counter */
2225 ExprList *pEList; /* List of WHEN terms */
2226 struct ExprList_item *aListelem; /* Array of WHEN terms */
2227 Expr opCompare; /* The X==Ei expression */
2228 Expr cacheX; /* Cached expression X */
2229 Expr *pX; /* The X expression */
2230 Expr *pTest; /* X==Ei (form A) or just Ei (form B) */
drh17a7f8d2002-03-24 13:13:27 +00002231
2232 assert(pExpr->pList);
2233 assert((pExpr->pList->nExpr % 2) == 0);
2234 assert(pExpr->pList->nExpr > 0);
drhbe5c89a2004-07-26 00:31:09 +00002235 pEList = pExpr->pList;
2236 aListelem = pEList->a;
2237 nExpr = pEList->nExpr;
drh2dcef112008-01-12 19:03:48 +00002238 endLabel = sqlite3VdbeMakeLabel(v);
2239 if( (pX = pExpr->pLeft)!=0 ){
2240 cacheX = *pX;
drhc5499be2008-04-01 15:06:33 +00002241 testcase( pX->op==TK_COLUMN || pX->op==TK_REGISTER );
drh2dcef112008-01-12 19:03:48 +00002242 cacheX.iTable = sqlite3ExprCodeTemp(pParse, pX, &regFree1);
drhc5499be2008-04-01 15:06:33 +00002243 testcase( regFree1==0 );
drh2dcef112008-01-12 19:03:48 +00002244 cacheX.op = TK_REGISTER;
2245 opCompare.op = TK_EQ;
2246 opCompare.pLeft = &cacheX;
2247 pTest = &opCompare;
drh17a7f8d2002-03-24 13:13:27 +00002248 }
drhc5499be2008-04-01 15:06:33 +00002249 pParse->disableColCache++;
drhf5905aa2002-05-26 20:54:33 +00002250 for(i=0; i<nExpr; i=i+2){
drh2dcef112008-01-12 19:03:48 +00002251 if( pX ){
2252 opCompare.pRight = aListelem[i].pExpr;
drh17a7f8d2002-03-24 13:13:27 +00002253 }else{
drh2dcef112008-01-12 19:03:48 +00002254 pTest = aListelem[i].pExpr;
drh17a7f8d2002-03-24 13:13:27 +00002255 }
drh2dcef112008-01-12 19:03:48 +00002256 nextCase = sqlite3VdbeMakeLabel(v);
drhc5499be2008-04-01 15:06:33 +00002257 testcase( pTest->op==TK_COLUMN || pTest->op==TK_REGISTER );
drh2dcef112008-01-12 19:03:48 +00002258 sqlite3ExprIfFalse(pParse, pTest, nextCase, SQLITE_JUMPIFNULL);
drhc5499be2008-04-01 15:06:33 +00002259 testcase( aListelem[i+1].pExpr->op==TK_COLUMN );
2260 testcase( aListelem[i+1].pExpr->op==TK_REGISTER );
drh9de221d2008-01-05 06:51:30 +00002261 sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target);
drh2dcef112008-01-12 19:03:48 +00002262 sqlite3VdbeAddOp2(v, OP_Goto, 0, endLabel);
2263 sqlite3VdbeResolveLabel(v, nextCase);
drhf570f012002-05-31 15:51:25 +00002264 }
drh17a7f8d2002-03-24 13:13:27 +00002265 if( pExpr->pRight ){
drh9de221d2008-01-05 06:51:30 +00002266 sqlite3ExprCode(pParse, pExpr->pRight, target);
drh17a7f8d2002-03-24 13:13:27 +00002267 }else{
drh9de221d2008-01-05 06:51:30 +00002268 sqlite3VdbeAddOp2(v, OP_Null, 0, target);
drh17a7f8d2002-03-24 13:13:27 +00002269 }
drh2dcef112008-01-12 19:03:48 +00002270 sqlite3VdbeResolveLabel(v, endLabel);
drhc5499be2008-04-01 15:06:33 +00002271 assert( pParse->disableColCache>0 );
2272 pParse->disableColCache--;
danielk19776f349032002-06-11 02:25:40 +00002273 break;
2274 }
danielk19775338a5f2005-01-20 13:03:10 +00002275#ifndef SQLITE_OMIT_TRIGGER
danielk19776f349032002-06-11 02:25:40 +00002276 case TK_RAISE: {
2277 if( !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +00002278 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00002279 "RAISE() may only be used within a trigger-program");
drh389a1ad2008-01-03 23:44:53 +00002280 return 0;
danielk19776f349032002-06-11 02:25:40 +00002281 }
drhad6d9462004-09-19 02:15:24 +00002282 if( pExpr->iColumn!=OE_Ignore ){
2283 assert( pExpr->iColumn==OE_Rollback ||
2284 pExpr->iColumn == OE_Abort ||
2285 pExpr->iColumn == OE_Fail );
drh8b213892008-08-29 02:14:02 +00002286 sqlite3DequoteExpr(db, pExpr);
drh66a51672008-01-03 00:01:23 +00002287 sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn, 0,
drh2646da72005-12-09 20:02:05 +00002288 (char*)pExpr->token.z, pExpr->token.n);
danielk19776f349032002-06-11 02:25:40 +00002289 } else {
drhad6d9462004-09-19 02:15:24 +00002290 assert( pExpr->iColumn == OE_Ignore );
drh66a51672008-01-03 00:01:23 +00002291 sqlite3VdbeAddOp2(v, OP_ContextPop, 0, 0);
2292 sqlite3VdbeAddOp2(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
drhd4e70eb2008-01-02 00:34:36 +00002293 VdbeComment((v, "raise(IGNORE)"));
danielk19776f349032002-06-11 02:25:40 +00002294 }
drhffe07b22005-11-03 00:41:17 +00002295 break;
drh17a7f8d2002-03-24 13:13:27 +00002296 }
danielk19775338a5f2005-01-20 13:03:10 +00002297#endif
drhffe07b22005-11-03 00:41:17 +00002298 }
drh2dcef112008-01-12 19:03:48 +00002299 sqlite3ReleaseTempReg(pParse, regFree1);
2300 sqlite3ReleaseTempReg(pParse, regFree2);
2301 return inReg;
2302}
2303
2304/*
2305** Generate code to evaluate an expression and store the results
2306** into a register. Return the register number where the results
2307** are stored.
2308**
2309** If the register is a temporary register that can be deallocated,
drh678ccce2008-03-31 18:19:54 +00002310** then write its number into *pReg. If the result register is not
drh2dcef112008-01-12 19:03:48 +00002311** a temporary, then set *pReg to zero.
2312*/
2313int sqlite3ExprCodeTemp(Parse *pParse, Expr *pExpr, int *pReg){
2314 int r1 = sqlite3GetTempReg(pParse);
2315 int r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
2316 if( r2==r1 ){
2317 *pReg = r1;
2318 }else{
2319 sqlite3ReleaseTempReg(pParse, r1);
2320 *pReg = 0;
2321 }
2322 return r2;
2323}
2324
2325/*
2326** Generate code that will evaluate expression pExpr and store the
2327** results in register target. The results are guaranteed to appear
2328** in register target.
2329*/
2330int sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){
drh9cbf3422008-01-17 16:22:13 +00002331 int inReg;
2332
2333 assert( target>0 && target<=pParse->nMem );
2334 inReg = sqlite3ExprCodeTarget(pParse, pExpr, target);
drh0e359b32008-01-13 19:02:11 +00002335 assert( pParse->pVdbe || pParse->db->mallocFailed );
2336 if( inReg!=target && pParse->pVdbe ){
drh9cbf3422008-01-17 16:22:13 +00002337 sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, inReg, target);
drhcce7d172000-05-31 15:34:51 +00002338 }
drh389a1ad2008-01-03 23:44:53 +00002339 return target;
drhcce7d172000-05-31 15:34:51 +00002340}
2341
2342/*
drh2dcef112008-01-12 19:03:48 +00002343** Generate code that evalutes the given expression and puts the result
drhde4fcfd2008-01-19 23:50:26 +00002344** in register target.
drh25303782004-12-07 15:41:48 +00002345**
drh2dcef112008-01-12 19:03:48 +00002346** Also make a copy of the expression results into another "cache" register
2347** and modify the expression so that the next time it is evaluated,
2348** the result is a copy of the cache register.
2349**
2350** This routine is used for expressions that are used multiple
2351** times. They are evaluated once and the results of the expression
2352** are reused.
drh25303782004-12-07 15:41:48 +00002353*/
drh2dcef112008-01-12 19:03:48 +00002354int sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr, int target){
drh25303782004-12-07 15:41:48 +00002355 Vdbe *v = pParse->pVdbe;
drh2dcef112008-01-12 19:03:48 +00002356 int inReg;
2357 inReg = sqlite3ExprCode(pParse, pExpr, target);
drhde4fcfd2008-01-19 23:50:26 +00002358 assert( target>0 );
drh2dcef112008-01-12 19:03:48 +00002359 if( pExpr->op!=TK_REGISTER ){
2360 int iMem;
drhde4fcfd2008-01-19 23:50:26 +00002361 iMem = ++pParse->nMem;
2362 sqlite3VdbeAddOp2(v, OP_Copy, inReg, iMem);
drh2dcef112008-01-12 19:03:48 +00002363 pExpr->iTable = iMem;
drh25303782004-12-07 15:41:48 +00002364 pExpr->op = TK_REGISTER;
2365 }
drh2dcef112008-01-12 19:03:48 +00002366 return inReg;
drh25303782004-12-07 15:41:48 +00002367}
drh2dcef112008-01-12 19:03:48 +00002368
drh678ccce2008-03-31 18:19:54 +00002369/*
drh47de9552008-04-01 18:04:11 +00002370** Return TRUE if pExpr is an constant expression that is appropriate
2371** for factoring out of a loop. Appropriate expressions are:
2372**
2373** * Any expression that evaluates to two or more opcodes.
2374**
2375** * Any OP_Integer, OP_Real, OP_String, OP_Blob, OP_Null,
2376** or OP_Variable that does not need to be placed in a
2377** specific register.
2378**
2379** There is no point in factoring out single-instruction constant
2380** expressions that need to be placed in a particular register.
2381** We could factor them out, but then we would end up adding an
2382** OP_SCopy instruction to move the value into the correct register
2383** later. We might as well just use the original instruction and
2384** avoid the OP_SCopy.
2385*/
2386static int isAppropriateForFactoring(Expr *p){
2387 if( !sqlite3ExprIsConstantNotJoin(p) ){
2388 return 0; /* Only constant expressions are appropriate for factoring */
2389 }
2390 if( (p->flags & EP_FixedDest)==0 ){
2391 return 1; /* Any constant without a fixed destination is appropriate */
2392 }
2393 while( p->op==TK_UPLUS ) p = p->pLeft;
2394 switch( p->op ){
2395#ifndef SQLITE_OMIT_BLOB_LITERAL
2396 case TK_BLOB:
2397#endif
2398 case TK_VARIABLE:
2399 case TK_INTEGER:
2400 case TK_FLOAT:
2401 case TK_NULL:
2402 case TK_STRING: {
2403 testcase( p->op==TK_BLOB );
2404 testcase( p->op==TK_VARIABLE );
2405 testcase( p->op==TK_INTEGER );
2406 testcase( p->op==TK_FLOAT );
2407 testcase( p->op==TK_NULL );
2408 testcase( p->op==TK_STRING );
2409 /* Single-instruction constants with a fixed destination are
2410 ** better done in-line. If we factor them, they will just end
2411 ** up generating an OP_SCopy to move the value to the destination
2412 ** register. */
2413 return 0;
2414 }
2415 case TK_UMINUS: {
2416 if( p->pLeft->op==TK_FLOAT || p->pLeft->op==TK_INTEGER ){
2417 return 0;
2418 }
2419 break;
2420 }
2421 default: {
2422 break;
2423 }
2424 }
2425 return 1;
2426}
2427
2428/*
2429** If pExpr is a constant expression that is appropriate for
2430** factoring out of a loop, then evaluate the expression
drh678ccce2008-03-31 18:19:54 +00002431** into a register and convert the expression into a TK_REGISTER
2432** expression.
2433*/
drh7d10d5a2008-08-20 16:35:10 +00002434static int evalConstExpr(Walker *pWalker, Expr *pExpr){
2435 Parse *pParse = pWalker->pParse;
drh47de9552008-04-01 18:04:11 +00002436 switch( pExpr->op ){
2437 case TK_REGISTER: {
2438 return 1;
2439 }
2440 case TK_FUNCTION:
2441 case TK_AGG_FUNCTION:
2442 case TK_CONST_FUNC: {
2443 /* The arguments to a function have a fixed destination.
2444 ** Mark them this way to avoid generated unneeded OP_SCopy
2445 ** instructions.
2446 */
2447 ExprList *pList = pExpr->pList;
2448 if( pList ){
2449 int i = pList->nExpr;
2450 struct ExprList_item *pItem = pList->a;
2451 for(; i>0; i--, pItem++){
2452 if( pItem->pExpr ) pItem->pExpr->flags |= EP_FixedDest;
2453 }
2454 }
2455 break;
2456 }
drh678ccce2008-03-31 18:19:54 +00002457 }
drh47de9552008-04-01 18:04:11 +00002458 if( isAppropriateForFactoring(pExpr) ){
drh678ccce2008-03-31 18:19:54 +00002459 int r1 = ++pParse->nMem;
2460 int r2;
2461 r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
drhc5499be2008-04-01 15:06:33 +00002462 if( r1!=r2 ) sqlite3ReleaseTempReg(pParse, r1);
drh678ccce2008-03-31 18:19:54 +00002463 pExpr->op = TK_REGISTER;
2464 pExpr->iTable = r2;
drh7d10d5a2008-08-20 16:35:10 +00002465 return WRC_Prune;
drh678ccce2008-03-31 18:19:54 +00002466 }
drh7d10d5a2008-08-20 16:35:10 +00002467 return WRC_Continue;
drh678ccce2008-03-31 18:19:54 +00002468}
2469
2470/*
2471** Preevaluate constant subexpressions within pExpr and store the
2472** results in registers. Modify pExpr so that the constant subexpresions
2473** are TK_REGISTER opcodes that refer to the precomputed values.
2474*/
2475void sqlite3ExprCodeConstants(Parse *pParse, Expr *pExpr){
drh7d10d5a2008-08-20 16:35:10 +00002476 Walker w;
2477 w.xExprCallback = evalConstExpr;
2478 w.xSelectCallback = 0;
2479 w.pParse = pParse;
2480 sqlite3WalkExpr(&w, pExpr);
drh678ccce2008-03-31 18:19:54 +00002481}
2482
drh25303782004-12-07 15:41:48 +00002483
2484/*
drh268380c2004-02-25 13:47:31 +00002485** Generate code that pushes the value of every element of the given
drh9cbf3422008-01-17 16:22:13 +00002486** expression list into a sequence of registers beginning at target.
drh268380c2004-02-25 13:47:31 +00002487**
drh892d3172008-01-10 03:46:36 +00002488** Return the number of elements evaluated.
drh268380c2004-02-25 13:47:31 +00002489*/
danielk19774adee202004-05-08 08:23:19 +00002490int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00002491 Parse *pParse, /* Parsing context */
drh389a1ad2008-01-03 23:44:53 +00002492 ExprList *pList, /* The expression list to be coded */
drh191b54c2008-04-15 12:14:21 +00002493 int target, /* Where to write results */
drhd1766112008-09-17 00:13:12 +00002494 int doHardCopy /* Make a hard copy of every element */
drh268380c2004-02-25 13:47:31 +00002495){
2496 struct ExprList_item *pItem;
drh9cbf3422008-01-17 16:22:13 +00002497 int i, n;
drh9d8b3072008-08-22 16:29:51 +00002498 assert( pList!=0 );
drh9cbf3422008-01-17 16:22:13 +00002499 assert( target>0 );
drh268380c2004-02-25 13:47:31 +00002500 n = pList->nExpr;
drh191b54c2008-04-15 12:14:21 +00002501 for(pItem=pList->a, i=0; i<n; i++, pItem++){
drh8b213892008-08-29 02:14:02 +00002502 if( pItem->iAlias ){
2503 int iReg = codeAlias(pParse, pItem->iAlias, pItem->pExpr);
2504 Vdbe *v = sqlite3GetVdbe(pParse);
2505 sqlite3VdbeAddOp2(v, OP_SCopy, iReg, target+i);
drhd1766112008-09-17 00:13:12 +00002506 }else{
drh8b213892008-08-29 02:14:02 +00002507 sqlite3ExprCode(pParse, pItem->pExpr, target+i);
2508 }
drhd1766112008-09-17 00:13:12 +00002509 if( doHardCopy ){
2510 sqlite3ExprHardCopy(pParse, target, n);
2511 }
drh268380c2004-02-25 13:47:31 +00002512 }
drhf9b596e2004-05-26 16:54:42 +00002513 return n;
drh268380c2004-02-25 13:47:31 +00002514}
2515
2516/*
drhcce7d172000-05-31 15:34:51 +00002517** Generate code for a boolean expression such that a jump is made
2518** to the label "dest" if the expression is true but execution
2519** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00002520**
2521** If the expression evaluates to NULL (neither true nor false), then
drh35573352008-01-08 23:54:25 +00002522** take the jump if the jumpIfNull flag is SQLITE_JUMPIFNULL.
drhf2bc0132004-10-04 13:19:23 +00002523**
2524** This code depends on the fact that certain token values (ex: TK_EQ)
2525** are the same as opcode values (ex: OP_Eq) that implement the corresponding
2526** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
2527** the make process cause these values to align. Assert()s in the code
2528** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00002529*/
danielk19774adee202004-05-08 08:23:19 +00002530void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00002531 Vdbe *v = pParse->pVdbe;
2532 int op = 0;
drh2dcef112008-01-12 19:03:48 +00002533 int regFree1 = 0;
2534 int regFree2 = 0;
2535 int r1, r2;
2536
drh35573352008-01-08 23:54:25 +00002537 assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
drhdaffd0e2001-04-11 14:28:42 +00002538 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00002539 op = pExpr->op;
2540 switch( op ){
drhcce7d172000-05-31 15:34:51 +00002541 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00002542 int d2 = sqlite3VdbeMakeLabel(v);
drhc5499be2008-04-01 15:06:33 +00002543 testcase( jumpIfNull==0 );
2544 testcase( pParse->disableColCache==0 );
drh35573352008-01-08 23:54:25 +00002545 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,jumpIfNull^SQLITE_JUMPIFNULL);
drhe55cbd72008-03-31 23:48:03 +00002546 pParse->disableColCache++;
danielk19774adee202004-05-08 08:23:19 +00002547 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhc5499be2008-04-01 15:06:33 +00002548 assert( pParse->disableColCache>0 );
drhe55cbd72008-03-31 23:48:03 +00002549 pParse->disableColCache--;
danielk19774adee202004-05-08 08:23:19 +00002550 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00002551 break;
2552 }
2553 case TK_OR: {
drhc5499be2008-04-01 15:06:33 +00002554 testcase( jumpIfNull==0 );
2555 testcase( pParse->disableColCache==0 );
danielk19774adee202004-05-08 08:23:19 +00002556 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhe55cbd72008-03-31 23:48:03 +00002557 pParse->disableColCache++;
danielk19774adee202004-05-08 08:23:19 +00002558 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhc5499be2008-04-01 15:06:33 +00002559 assert( pParse->disableColCache>0 );
drhe55cbd72008-03-31 23:48:03 +00002560 pParse->disableColCache--;
drhcce7d172000-05-31 15:34:51 +00002561 break;
2562 }
2563 case TK_NOT: {
drhc5499be2008-04-01 15:06:33 +00002564 testcase( jumpIfNull==0 );
danielk19774adee202004-05-08 08:23:19 +00002565 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002566 break;
2567 }
2568 case TK_LT:
2569 case TK_LE:
2570 case TK_GT:
2571 case TK_GE:
2572 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00002573 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00002574 assert( TK_LT==OP_Lt );
2575 assert( TK_LE==OP_Le );
2576 assert( TK_GT==OP_Gt );
2577 assert( TK_GE==OP_Ge );
2578 assert( TK_EQ==OP_Eq );
2579 assert( TK_NE==OP_Ne );
drhc5499be2008-04-01 15:06:33 +00002580 testcase( op==TK_LT );
2581 testcase( op==TK_LE );
2582 testcase( op==TK_GT );
2583 testcase( op==TK_GE );
2584 testcase( op==TK_EQ );
2585 testcase( op==TK_NE );
2586 testcase( jumpIfNull==0 );
drhda250ea2008-04-01 05:07:14 +00002587 codeCompareOperands(pParse, pExpr->pLeft, &r1, &regFree1,
2588 pExpr->pRight, &r2, &regFree2);
drh35573352008-01-08 23:54:25 +00002589 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
drh2dcef112008-01-12 19:03:48 +00002590 r1, r2, dest, jumpIfNull);
drhc5499be2008-04-01 15:06:33 +00002591 testcase( regFree1==0 );
2592 testcase( regFree2==0 );
drhcce7d172000-05-31 15:34:51 +00002593 break;
2594 }
2595 case TK_ISNULL:
2596 case TK_NOTNULL: {
drhf2bc0132004-10-04 13:19:23 +00002597 assert( TK_ISNULL==OP_IsNull );
2598 assert( TK_NOTNULL==OP_NotNull );
drhc5499be2008-04-01 15:06:33 +00002599 testcase( op==TK_ISNULL );
2600 testcase( op==TK_NOTNULL );
drh2dcef112008-01-12 19:03:48 +00002601 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
2602 sqlite3VdbeAddOp2(v, op, r1, dest);
drhc5499be2008-04-01 15:06:33 +00002603 testcase( regFree1==0 );
drhcce7d172000-05-31 15:34:51 +00002604 break;
2605 }
drhfef52082000-06-06 01:50:43 +00002606 case TK_BETWEEN: {
drh2dcef112008-01-12 19:03:48 +00002607 /* x BETWEEN y AND z
danielk19770202b292004-06-09 09:55:16 +00002608 **
drh2dcef112008-01-12 19:03:48 +00002609 ** Is equivalent to
2610 **
2611 ** x>=y AND x<=z
2612 **
2613 ** Code it as such, taking care to do the common subexpression
2614 ** elementation of x.
danielk19770202b292004-06-09 09:55:16 +00002615 */
drh2dcef112008-01-12 19:03:48 +00002616 Expr exprAnd;
2617 Expr compLeft;
2618 Expr compRight;
2619 Expr exprX;
danielk19770202b292004-06-09 09:55:16 +00002620
drh2dcef112008-01-12 19:03:48 +00002621 exprX = *pExpr->pLeft;
2622 exprAnd.op = TK_AND;
2623 exprAnd.pLeft = &compLeft;
2624 exprAnd.pRight = &compRight;
2625 compLeft.op = TK_GE;
2626 compLeft.pLeft = &exprX;
2627 compLeft.pRight = pExpr->pList->a[0].pExpr;
2628 compRight.op = TK_LE;
2629 compRight.pLeft = &exprX;
2630 compRight.pRight = pExpr->pList->a[1].pExpr;
2631 exprX.iTable = sqlite3ExprCodeTemp(pParse, &exprX, &regFree1);
drhc5499be2008-04-01 15:06:33 +00002632 testcase( regFree1==0 );
drh2dcef112008-01-12 19:03:48 +00002633 exprX.op = TK_REGISTER;
drhc5499be2008-04-01 15:06:33 +00002634 testcase( jumpIfNull==0 );
drh2dcef112008-01-12 19:03:48 +00002635 sqlite3ExprIfTrue(pParse, &exprAnd, dest, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00002636 break;
2637 }
drhcce7d172000-05-31 15:34:51 +00002638 default: {
drh2dcef112008-01-12 19:03:48 +00002639 r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
2640 sqlite3VdbeAddOp3(v, OP_If, r1, dest, jumpIfNull!=0);
drhc5499be2008-04-01 15:06:33 +00002641 testcase( regFree1==0 );
2642 testcase( jumpIfNull==0 );
drhcce7d172000-05-31 15:34:51 +00002643 break;
2644 }
2645 }
drh2dcef112008-01-12 19:03:48 +00002646 sqlite3ReleaseTempReg(pParse, regFree1);
2647 sqlite3ReleaseTempReg(pParse, regFree2);
drhcce7d172000-05-31 15:34:51 +00002648}
2649
2650/*
drh66b89c82000-11-28 20:47:17 +00002651** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00002652** to the label "dest" if the expression is false but execution
2653** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00002654**
2655** If the expression evaluates to NULL (neither true nor false) then
drh35573352008-01-08 23:54:25 +00002656** jump if jumpIfNull is SQLITE_JUMPIFNULL or fall through if jumpIfNull
2657** is 0.
drhcce7d172000-05-31 15:34:51 +00002658*/
danielk19774adee202004-05-08 08:23:19 +00002659void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00002660 Vdbe *v = pParse->pVdbe;
2661 int op = 0;
drh2dcef112008-01-12 19:03:48 +00002662 int regFree1 = 0;
2663 int regFree2 = 0;
2664 int r1, r2;
2665
drh35573352008-01-08 23:54:25 +00002666 assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
drhdaffd0e2001-04-11 14:28:42 +00002667 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00002668
2669 /* The value of pExpr->op and op are related as follows:
2670 **
2671 ** pExpr->op op
2672 ** --------- ----------
2673 ** TK_ISNULL OP_NotNull
2674 ** TK_NOTNULL OP_IsNull
2675 ** TK_NE OP_Eq
2676 ** TK_EQ OP_Ne
2677 ** TK_GT OP_Le
2678 ** TK_LE OP_Gt
2679 ** TK_GE OP_Lt
2680 ** TK_LT OP_Ge
2681 **
2682 ** For other values of pExpr->op, op is undefined and unused.
2683 ** The value of TK_ and OP_ constants are arranged such that we
2684 ** can compute the mapping above using the following expression.
2685 ** Assert()s verify that the computation is correct.
2686 */
2687 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
2688
2689 /* Verify correct alignment of TK_ and OP_ constants
2690 */
2691 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
2692 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
2693 assert( pExpr->op!=TK_NE || op==OP_Eq );
2694 assert( pExpr->op!=TK_EQ || op==OP_Ne );
2695 assert( pExpr->op!=TK_LT || op==OP_Ge );
2696 assert( pExpr->op!=TK_LE || op==OP_Gt );
2697 assert( pExpr->op!=TK_GT || op==OP_Le );
2698 assert( pExpr->op!=TK_GE || op==OP_Lt );
2699
drhcce7d172000-05-31 15:34:51 +00002700 switch( pExpr->op ){
2701 case TK_AND: {
drhc5499be2008-04-01 15:06:33 +00002702 testcase( jumpIfNull==0 );
2703 testcase( pParse->disableColCache==0 );
danielk19774adee202004-05-08 08:23:19 +00002704 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhe55cbd72008-03-31 23:48:03 +00002705 pParse->disableColCache++;
danielk19774adee202004-05-08 08:23:19 +00002706 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhc5499be2008-04-01 15:06:33 +00002707 assert( pParse->disableColCache>0 );
drhe55cbd72008-03-31 23:48:03 +00002708 pParse->disableColCache--;
drhcce7d172000-05-31 15:34:51 +00002709 break;
2710 }
2711 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00002712 int d2 = sqlite3VdbeMakeLabel(v);
drhc5499be2008-04-01 15:06:33 +00002713 testcase( jumpIfNull==0 );
2714 testcase( pParse->disableColCache==0 );
drh35573352008-01-08 23:54:25 +00002715 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, jumpIfNull^SQLITE_JUMPIFNULL);
drhe55cbd72008-03-31 23:48:03 +00002716 pParse->disableColCache++;
danielk19774adee202004-05-08 08:23:19 +00002717 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhc5499be2008-04-01 15:06:33 +00002718 assert( pParse->disableColCache>0 );
drhe55cbd72008-03-31 23:48:03 +00002719 pParse->disableColCache--;
danielk19774adee202004-05-08 08:23:19 +00002720 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00002721 break;
2722 }
2723 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00002724 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002725 break;
2726 }
2727 case TK_LT:
2728 case TK_LE:
2729 case TK_GT:
2730 case TK_GE:
2731 case TK_NE:
2732 case TK_EQ: {
drhc5499be2008-04-01 15:06:33 +00002733 testcase( op==TK_LT );
2734 testcase( op==TK_LE );
2735 testcase( op==TK_GT );
2736 testcase( op==TK_GE );
2737 testcase( op==TK_EQ );
2738 testcase( op==TK_NE );
2739 testcase( jumpIfNull==0 );
drhda250ea2008-04-01 05:07:14 +00002740 codeCompareOperands(pParse, pExpr->pLeft, &r1, &regFree1,
2741 pExpr->pRight, &r2, &regFree2);
drh35573352008-01-08 23:54:25 +00002742 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
drh2dcef112008-01-12 19:03:48 +00002743 r1, r2, dest, jumpIfNull);
drhc5499be2008-04-01 15:06:33 +00002744 testcase( regFree1==0 );
2745 testcase( regFree2==0 );
drhcce7d172000-05-31 15:34:51 +00002746 break;
2747 }
drhcce7d172000-05-31 15:34:51 +00002748 case TK_ISNULL:
2749 case TK_NOTNULL: {
drhc5499be2008-04-01 15:06:33 +00002750 testcase( op==TK_ISNULL );
2751 testcase( op==TK_NOTNULL );
drh2dcef112008-01-12 19:03:48 +00002752 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
2753 sqlite3VdbeAddOp2(v, op, r1, dest);
drhc5499be2008-04-01 15:06:33 +00002754 testcase( regFree1==0 );
drhcce7d172000-05-31 15:34:51 +00002755 break;
2756 }
drhfef52082000-06-06 01:50:43 +00002757 case TK_BETWEEN: {
drh2dcef112008-01-12 19:03:48 +00002758 /* x BETWEEN y AND z
danielk19770202b292004-06-09 09:55:16 +00002759 **
drh2dcef112008-01-12 19:03:48 +00002760 ** Is equivalent to
2761 **
2762 ** x>=y AND x<=z
2763 **
2764 ** Code it as such, taking care to do the common subexpression
2765 ** elementation of x.
danielk19770202b292004-06-09 09:55:16 +00002766 */
drh2dcef112008-01-12 19:03:48 +00002767 Expr exprAnd;
2768 Expr compLeft;
2769 Expr compRight;
2770 Expr exprX;
drhbe5c89a2004-07-26 00:31:09 +00002771
drh2dcef112008-01-12 19:03:48 +00002772 exprX = *pExpr->pLeft;
2773 exprAnd.op = TK_AND;
2774 exprAnd.pLeft = &compLeft;
2775 exprAnd.pRight = &compRight;
2776 compLeft.op = TK_GE;
2777 compLeft.pLeft = &exprX;
2778 compLeft.pRight = pExpr->pList->a[0].pExpr;
2779 compRight.op = TK_LE;
2780 compRight.pLeft = &exprX;
2781 compRight.pRight = pExpr->pList->a[1].pExpr;
2782 exprX.iTable = sqlite3ExprCodeTemp(pParse, &exprX, &regFree1);
drhc5499be2008-04-01 15:06:33 +00002783 testcase( regFree1==0 );
drh2dcef112008-01-12 19:03:48 +00002784 exprX.op = TK_REGISTER;
drhc5499be2008-04-01 15:06:33 +00002785 testcase( jumpIfNull==0 );
drh2dcef112008-01-12 19:03:48 +00002786 sqlite3ExprIfFalse(pParse, &exprAnd, dest, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00002787 break;
2788 }
drhcce7d172000-05-31 15:34:51 +00002789 default: {
drh2dcef112008-01-12 19:03:48 +00002790 r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
2791 sqlite3VdbeAddOp3(v, OP_IfNot, r1, dest, jumpIfNull!=0);
drhc5499be2008-04-01 15:06:33 +00002792 testcase( regFree1==0 );
2793 testcase( jumpIfNull==0 );
drhcce7d172000-05-31 15:34:51 +00002794 break;
2795 }
2796 }
drh2dcef112008-01-12 19:03:48 +00002797 sqlite3ReleaseTempReg(pParse, regFree1);
2798 sqlite3ReleaseTempReg(pParse, regFree2);
drhcce7d172000-05-31 15:34:51 +00002799}
drh22827922000-06-06 17:27:05 +00002800
2801/*
2802** Do a deep comparison of two expression trees. Return TRUE (non-zero)
2803** if they are identical and return FALSE if they differ in any way.
drhd40aab02007-02-24 15:29:03 +00002804**
2805** Sometimes this routine will return FALSE even if the two expressions
2806** really are equivalent. If we cannot prove that the expressions are
2807** identical, we return FALSE just to be safe. So if this routine
2808** returns false, then you do not really know for certain if the two
2809** expressions are the same. But if you get a TRUE return, then you
2810** can be sure the expressions are the same. In the places where
2811** this routine is used, it does not hurt to get an extra FALSE - that
2812** just might result in some slightly slower code. But returning
2813** an incorrect TRUE could lead to a malfunction.
drh22827922000-06-06 17:27:05 +00002814*/
danielk19774adee202004-05-08 08:23:19 +00002815int sqlite3ExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00002816 int i;
danielk19774b202ae2006-01-23 05:50:58 +00002817 if( pA==0||pB==0 ){
2818 return pB==pA;
drh22827922000-06-06 17:27:05 +00002819 }
2820 if( pA->op!=pB->op ) return 0;
drhfd357972005-09-09 01:33:19 +00002821 if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 0;
danielk19774adee202004-05-08 08:23:19 +00002822 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
2823 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00002824 if( pA->pList ){
2825 if( pB->pList==0 ) return 0;
2826 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
2827 for(i=0; i<pA->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00002828 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00002829 return 0;
2830 }
2831 }
2832 }else if( pB->pList ){
2833 return 0;
2834 }
2835 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00002836 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drhdd735212007-02-24 13:53:05 +00002837 if( pA->op!=TK_COLUMN && pA->token.z ){
drh22827922000-06-06 17:27:05 +00002838 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00002839 if( pB->token.n!=pA->token.n ) return 0;
drh2646da72005-12-09 20:02:05 +00002840 if( sqlite3StrNICmp((char*)pA->token.z,(char*)pB->token.z,pB->token.n)!=0 ){
2841 return 0;
2842 }
drh22827922000-06-06 17:27:05 +00002843 }
2844 return 1;
2845}
2846
drh13449892005-09-07 21:22:45 +00002847
drh22827922000-06-06 17:27:05 +00002848/*
drh13449892005-09-07 21:22:45 +00002849** Add a new element to the pAggInfo->aCol[] array. Return the index of
2850** the new element. Return a negative number if malloc fails.
drh22827922000-06-06 17:27:05 +00002851*/
drh17435752007-08-16 04:30:38 +00002852static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){
drh13449892005-09-07 21:22:45 +00002853 int i;
drhcf643722007-03-27 13:36:37 +00002854 pInfo->aCol = sqlite3ArrayAllocate(
drh17435752007-08-16 04:30:38 +00002855 db,
drhcf643722007-03-27 13:36:37 +00002856 pInfo->aCol,
2857 sizeof(pInfo->aCol[0]),
2858 3,
2859 &pInfo->nColumn,
2860 &pInfo->nColumnAlloc,
2861 &i
2862 );
drh13449892005-09-07 21:22:45 +00002863 return i;
2864}
2865
2866/*
2867** Add a new element to the pAggInfo->aFunc[] array. Return the index of
2868** the new element. Return a negative number if malloc fails.
2869*/
drh17435752007-08-16 04:30:38 +00002870static int addAggInfoFunc(sqlite3 *db, AggInfo *pInfo){
drh13449892005-09-07 21:22:45 +00002871 int i;
drhcf643722007-03-27 13:36:37 +00002872 pInfo->aFunc = sqlite3ArrayAllocate(
drh17435752007-08-16 04:30:38 +00002873 db,
drhcf643722007-03-27 13:36:37 +00002874 pInfo->aFunc,
2875 sizeof(pInfo->aFunc[0]),
2876 3,
2877 &pInfo->nFunc,
2878 &pInfo->nFuncAlloc,
2879 &i
2880 );
drh13449892005-09-07 21:22:45 +00002881 return i;
2882}
drh22827922000-06-06 17:27:05 +00002883
2884/*
drh7d10d5a2008-08-20 16:35:10 +00002885** This is the xExprCallback for a tree walker. It is used to
2886** implement sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates
drh626a8792005-01-17 22:08:19 +00002887** for additional information.
drh22827922000-06-06 17:27:05 +00002888*/
drh7d10d5a2008-08-20 16:35:10 +00002889static int analyzeAggregate(Walker *pWalker, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00002890 int i;
drh7d10d5a2008-08-20 16:35:10 +00002891 NameContext *pNC = pWalker->u.pNC;
danielk1977a58fdfb2005-02-08 07:50:40 +00002892 Parse *pParse = pNC->pParse;
2893 SrcList *pSrcList = pNC->pSrcList;
drh13449892005-09-07 21:22:45 +00002894 AggInfo *pAggInfo = pNC->pAggInfo;
drh22827922000-06-06 17:27:05 +00002895
drh22827922000-06-06 17:27:05 +00002896 switch( pExpr->op ){
drh89c69d02007-01-04 01:20:28 +00002897 case TK_AGG_COLUMN:
drh967e8b72000-06-21 13:59:10 +00002898 case TK_COLUMN: {
drh8b213892008-08-29 02:14:02 +00002899 testcase( pExpr->op==TK_AGG_COLUMN );
2900 testcase( pExpr->op==TK_COLUMN );
drh13449892005-09-07 21:22:45 +00002901 /* Check to see if the column is in one of the tables in the FROM
2902 ** clause of the aggregate query */
2903 if( pSrcList ){
2904 struct SrcList_item *pItem = pSrcList->a;
2905 for(i=0; i<pSrcList->nSrc; i++, pItem++){
2906 struct AggInfo_col *pCol;
2907 if( pExpr->iTable==pItem->iCursor ){
2908 /* If we reach this point, it means that pExpr refers to a table
2909 ** that is in the FROM clause of the aggregate query.
2910 **
2911 ** Make an entry for the column in pAggInfo->aCol[] if there
2912 ** is not an entry there already.
2913 */
drh7f906d62007-03-12 23:48:52 +00002914 int k;
drh13449892005-09-07 21:22:45 +00002915 pCol = pAggInfo->aCol;
drh7f906d62007-03-12 23:48:52 +00002916 for(k=0; k<pAggInfo->nColumn; k++, pCol++){
drh13449892005-09-07 21:22:45 +00002917 if( pCol->iTable==pExpr->iTable &&
2918 pCol->iColumn==pExpr->iColumn ){
2919 break;
2920 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002921 }
danielk19771e536952007-08-16 10:09:01 +00002922 if( (k>=pAggInfo->nColumn)
2923 && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0
2924 ){
drh7f906d62007-03-12 23:48:52 +00002925 pCol = &pAggInfo->aCol[k];
danielk19770817d0d2007-02-14 09:19:36 +00002926 pCol->pTab = pExpr->pTab;
drh13449892005-09-07 21:22:45 +00002927 pCol->iTable = pExpr->iTable;
2928 pCol->iColumn = pExpr->iColumn;
drh0a07c102008-01-03 18:03:08 +00002929 pCol->iMem = ++pParse->nMem;
drh13449892005-09-07 21:22:45 +00002930 pCol->iSorterColumn = -1;
drh5774b802005-09-07 22:48:16 +00002931 pCol->pExpr = pExpr;
drh13449892005-09-07 21:22:45 +00002932 if( pAggInfo->pGroupBy ){
2933 int j, n;
2934 ExprList *pGB = pAggInfo->pGroupBy;
2935 struct ExprList_item *pTerm = pGB->a;
2936 n = pGB->nExpr;
2937 for(j=0; j<n; j++, pTerm++){
2938 Expr *pE = pTerm->pExpr;
2939 if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable &&
2940 pE->iColumn==pExpr->iColumn ){
2941 pCol->iSorterColumn = j;
2942 break;
2943 }
2944 }
2945 }
2946 if( pCol->iSorterColumn<0 ){
2947 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
2948 }
2949 }
2950 /* There is now an entry for pExpr in pAggInfo->aCol[] (either
2951 ** because it was there before or because we just created it).
2952 ** Convert the pExpr to be a TK_AGG_COLUMN referring to that
2953 ** pAggInfo->aCol[] entry.
2954 */
2955 pExpr->pAggInfo = pAggInfo;
2956 pExpr->op = TK_AGG_COLUMN;
drh7f906d62007-03-12 23:48:52 +00002957 pExpr->iAgg = k;
drh13449892005-09-07 21:22:45 +00002958 break;
2959 } /* endif pExpr->iTable==pItem->iCursor */
2960 } /* end loop over pSrcList */
drh22827922000-06-06 17:27:05 +00002961 }
drh7d10d5a2008-08-20 16:35:10 +00002962 return WRC_Prune;
drh22827922000-06-06 17:27:05 +00002963 }
2964 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00002965 /* The pNC->nDepth==0 test causes aggregate functions in subqueries
2966 ** to be ignored */
danielk1977a58fdfb2005-02-08 07:50:40 +00002967 if( pNC->nDepth==0 ){
drh13449892005-09-07 21:22:45 +00002968 /* Check to see if pExpr is a duplicate of another aggregate
2969 ** function that is already in the pAggInfo structure
2970 */
2971 struct AggInfo_func *pItem = pAggInfo->aFunc;
2972 for(i=0; i<pAggInfo->nFunc; i++, pItem++){
2973 if( sqlite3ExprCompare(pItem->pExpr, pExpr) ){
danielk1977a58fdfb2005-02-08 07:50:40 +00002974 break;
2975 }
drh22827922000-06-06 17:27:05 +00002976 }
drh13449892005-09-07 21:22:45 +00002977 if( i>=pAggInfo->nFunc ){
2978 /* pExpr is original. Make a new entry in pAggInfo->aFunc[]
2979 */
danielk197714db2662006-01-09 16:12:04 +00002980 u8 enc = ENC(pParse->db);
danielk19771e536952007-08-16 10:09:01 +00002981 i = addAggInfoFunc(pParse->db, pAggInfo);
drh13449892005-09-07 21:22:45 +00002982 if( i>=0 ){
2983 pItem = &pAggInfo->aFunc[i];
2984 pItem->pExpr = pExpr;
drh0a07c102008-01-03 18:03:08 +00002985 pItem->iMem = ++pParse->nMem;
drh13449892005-09-07 21:22:45 +00002986 pItem->pFunc = sqlite3FindFunction(pParse->db,
drh2646da72005-12-09 20:02:05 +00002987 (char*)pExpr->token.z, pExpr->token.n,
drh13449892005-09-07 21:22:45 +00002988 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
drhfd357972005-09-09 01:33:19 +00002989 if( pExpr->flags & EP_Distinct ){
2990 pItem->iDistinct = pParse->nTab++;
2991 }else{
2992 pItem->iDistinct = -1;
2993 }
drh13449892005-09-07 21:22:45 +00002994 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002995 }
drh13449892005-09-07 21:22:45 +00002996 /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
2997 */
danielk1977a58fdfb2005-02-08 07:50:40 +00002998 pExpr->iAgg = i;
drh13449892005-09-07 21:22:45 +00002999 pExpr->pAggInfo = pAggInfo;
drh7d10d5a2008-08-20 16:35:10 +00003000 return WRC_Prune;
drh22827922000-06-06 17:27:05 +00003001 }
drh22827922000-06-06 17:27:05 +00003002 }
3003 }
drh7d10d5a2008-08-20 16:35:10 +00003004 return WRC_Continue;
3005}
3006static int analyzeAggregatesInSelect(Walker *pWalker, Select *pSelect){
3007 NameContext *pNC = pWalker->u.pNC;
3008 if( pNC->nDepth==0 ){
danielk1977a58fdfb2005-02-08 07:50:40 +00003009 pNC->nDepth++;
drh7d10d5a2008-08-20 16:35:10 +00003010 sqlite3WalkSelect(pWalker, pSelect);
danielk1977a58fdfb2005-02-08 07:50:40 +00003011 pNC->nDepth--;
drh7d10d5a2008-08-20 16:35:10 +00003012 return WRC_Prune;
3013 }else{
3014 return WRC_Continue;
danielk1977a58fdfb2005-02-08 07:50:40 +00003015 }
drh626a8792005-01-17 22:08:19 +00003016}
3017
3018/*
3019** Analyze the given expression looking for aggregate functions and
3020** for variables that need to be added to the pParse->aAgg[] array.
3021** Make additional entries to the pParse->aAgg[] array as necessary.
3022**
3023** This routine should only be called after the expression has been
drh7d10d5a2008-08-20 16:35:10 +00003024** analyzed by sqlite3ResolveExprNames().
drh626a8792005-01-17 22:08:19 +00003025*/
drhd2b3e232008-01-23 14:51:49 +00003026void sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
drh7d10d5a2008-08-20 16:35:10 +00003027 Walker w;
3028 w.xExprCallback = analyzeAggregate;
3029 w.xSelectCallback = analyzeAggregatesInSelect;
3030 w.u.pNC = pNC;
3031 sqlite3WalkExpr(&w, pExpr);
drh22827922000-06-06 17:27:05 +00003032}
drh5d9a4af2005-08-30 00:54:01 +00003033
3034/*
3035** Call sqlite3ExprAnalyzeAggregates() for every expression in an
3036** expression list. Return the number of errors.
3037**
3038** If an error is found, the analysis is cut short.
3039*/
drhd2b3e232008-01-23 14:51:49 +00003040void sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){
drh5d9a4af2005-08-30 00:54:01 +00003041 struct ExprList_item *pItem;
3042 int i;
drh5d9a4af2005-08-30 00:54:01 +00003043 if( pList ){
drhd2b3e232008-01-23 14:51:49 +00003044 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
3045 sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
drh5d9a4af2005-08-30 00:54:01 +00003046 }
3047 }
drh5d9a4af2005-08-30 00:54:01 +00003048}
drh892d3172008-01-10 03:46:36 +00003049
3050/*
3051** Allocate or deallocate temporary use registers during code generation.
3052*/
3053int sqlite3GetTempReg(Parse *pParse){
drhe55cbd72008-03-31 23:48:03 +00003054 if( pParse->nTempReg==0 ){
drh892d3172008-01-10 03:46:36 +00003055 return ++pParse->nMem;
3056 }
danielk19772f425f62008-07-04 09:41:39 +00003057 return pParse->aTempReg[--pParse->nTempReg];
drh892d3172008-01-10 03:46:36 +00003058}
3059void sqlite3ReleaseTempReg(Parse *pParse, int iReg){
drh2dcef112008-01-12 19:03:48 +00003060 if( iReg && pParse->nTempReg<ArraySize(pParse->aTempReg) ){
danielk1977a7d8b852008-07-04 09:15:11 +00003061 sqlite3ExprWritableRegister(pParse, iReg, iReg);
drh892d3172008-01-10 03:46:36 +00003062 pParse->aTempReg[pParse->nTempReg++] = iReg;
3063 }
3064}
3065
3066/*
3067** Allocate or deallocate a block of nReg consecutive registers
3068*/
3069int sqlite3GetTempRange(Parse *pParse, int nReg){
drhe55cbd72008-03-31 23:48:03 +00003070 int i, n;
3071 i = pParse->iRangeReg;
3072 n = pParse->nRangeReg;
3073 if( nReg<=n && !usedAsColumnCache(pParse, i, i+n-1) ){
drh892d3172008-01-10 03:46:36 +00003074 pParse->iRangeReg += nReg;
3075 pParse->nRangeReg -= nReg;
3076 }else{
3077 i = pParse->nMem+1;
3078 pParse->nMem += nReg;
3079 }
3080 return i;
3081}
3082void sqlite3ReleaseTempRange(Parse *pParse, int iReg, int nReg){
3083 if( nReg>pParse->nRangeReg ){
3084 pParse->nRangeReg = nReg;
3085 pParse->iRangeReg = iReg;
3086 }
3087}