blob: 03855f11baa40469a934f5563b3b5bd1aeef748a [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**
drh2f7794c2008-04-01 03:27:39 +000015** $Id: expr.c,v 1.361 2008/04/01 03:27:39 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
danielk1977a37cdde2004-05-16 11:15:36 +000046 return pExpr->affinity;
47}
48
drh53db1452004-05-20 13:54:53 +000049/*
drh8b4c40d2007-02-01 23:02:45 +000050** Set the collating sequence for expression pExpr to be the collating
51** sequence named by pToken. Return a pointer to the revised expression.
drha34001c2007-02-02 12:44:37 +000052** The collating sequence is marked as "explicit" using the EP_ExpCollate
53** flag. An explicit collating sequence will override implicit
54** collating sequences.
drh8b4c40d2007-02-01 23:02:45 +000055*/
56Expr *sqlite3ExprSetColl(Parse *pParse, Expr *pExpr, Token *pName){
danielk197739002502007-11-12 09:50:26 +000057 char *zColl = 0; /* Dequoted name of collation sequence */
drh8b4c40d2007-02-01 23:02:45 +000058 CollSeq *pColl;
danielk197739002502007-11-12 09:50:26 +000059 zColl = sqlite3NameFromToken(pParse->db, pName);
60 if( pExpr && zColl ){
61 pColl = sqlite3LocateCollSeq(pParse, zColl, -1);
62 if( pColl ){
63 pExpr->pColl = pColl;
64 pExpr->flags |= EP_ExpCollate;
65 }
drh8b4c40d2007-02-01 23:02:45 +000066 }
danielk197739002502007-11-12 09:50:26 +000067 sqlite3_free(zColl);
drh8b4c40d2007-02-01 23:02:45 +000068 return pExpr;
69}
70
71/*
danielk19770202b292004-06-09 09:55:16 +000072** Return the default collation sequence for the expression pExpr. If
73** there is no default collation type, return 0.
74*/
danielk19777cedc8d2004-06-10 10:50:08 +000075CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
76 CollSeq *pColl = 0;
danielk19770202b292004-06-09 09:55:16 +000077 if( pExpr ){
drh7e09fe02007-06-20 16:13:23 +000078 int op;
danielk19777cedc8d2004-06-10 10:50:08 +000079 pColl = pExpr->pColl;
drh7e09fe02007-06-20 16:13:23 +000080 op = pExpr->op;
81 if( (op==TK_CAST || op==TK_UPLUS) && !pColl ){
danielk19777cedc8d2004-06-10 10:50:08 +000082 return sqlite3ExprCollSeq(pParse, pExpr->pLeft);
danielk19770202b292004-06-09 09:55:16 +000083 }
84 }
danielk19777cedc8d2004-06-10 10:50:08 +000085 if( sqlite3CheckCollSeq(pParse, pColl) ){
86 pColl = 0;
87 }
88 return pColl;
danielk19770202b292004-06-09 09:55:16 +000089}
90
91/*
drh626a8792005-01-17 22:08:19 +000092** pExpr is an operand of a comparison operator. aff2 is the
93** type affinity of the other operand. This routine returns the
drh53db1452004-05-20 13:54:53 +000094** type affinity that should be used for the comparison operator.
95*/
danielk1977e014a832004-05-17 10:48:57 +000096char sqlite3CompareAffinity(Expr *pExpr, char aff2){
danielk1977bf3b7212004-05-18 10:06:24 +000097 char aff1 = sqlite3ExprAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +000098 if( aff1 && aff2 ){
drh8df447f2005-11-01 15:48:24 +000099 /* Both sides of the comparison are columns. If one has numeric
100 ** affinity, use that. Otherwise use no affinity.
danielk1977e014a832004-05-17 10:48:57 +0000101 */
drh8a512562005-11-14 22:29:05 +0000102 if( sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2) ){
danielk1977e014a832004-05-17 10:48:57 +0000103 return SQLITE_AFF_NUMERIC;
104 }else{
105 return SQLITE_AFF_NONE;
106 }
107 }else if( !aff1 && !aff2 ){
drh5f6a87b2004-07-19 00:39:45 +0000108 /* Neither side of the comparison is a column. Compare the
109 ** results directly.
danielk1977e014a832004-05-17 10:48:57 +0000110 */
drh5f6a87b2004-07-19 00:39:45 +0000111 return SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +0000112 }else{
113 /* One side is a column, the other is not. Use the columns affinity. */
drhfe05af82005-07-21 03:14:59 +0000114 assert( aff1==0 || aff2==0 );
danielk1977e014a832004-05-17 10:48:57 +0000115 return (aff1 + aff2);
116 }
117}
118
drh53db1452004-05-20 13:54:53 +0000119/*
120** pExpr is a comparison operator. Return the type affinity that should
121** be applied to both operands prior to doing the comparison.
122*/
danielk1977e014a832004-05-17 10:48:57 +0000123static char comparisonAffinity(Expr *pExpr){
124 char aff;
125 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
126 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
127 pExpr->op==TK_NE );
128 assert( pExpr->pLeft );
danielk1977bf3b7212004-05-18 10:06:24 +0000129 aff = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +0000130 if( pExpr->pRight ){
131 aff = sqlite3CompareAffinity(pExpr->pRight, aff);
132 }
133 else if( pExpr->pSelect ){
134 aff = sqlite3CompareAffinity(pExpr->pSelect->pEList->a[0].pExpr, aff);
135 }
136 else if( !aff ){
drhde087bd2007-02-23 03:00:44 +0000137 aff = SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +0000138 }
139 return aff;
140}
141
142/*
143** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
144** idx_affinity is the affinity of an indexed column. Return true
145** if the index with affinity idx_affinity may be used to implement
146** the comparison in pExpr.
147*/
148int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){
149 char aff = comparisonAffinity(pExpr);
drh8a512562005-11-14 22:29:05 +0000150 switch( aff ){
151 case SQLITE_AFF_NONE:
152 return 1;
153 case SQLITE_AFF_TEXT:
154 return idx_affinity==SQLITE_AFF_TEXT;
155 default:
156 return sqlite3IsNumericAffinity(idx_affinity);
157 }
danielk1977e014a832004-05-17 10:48:57 +0000158}
159
danielk1977a37cdde2004-05-16 11:15:36 +0000160/*
drh35573352008-01-08 23:54:25 +0000161** Return the P5 value that should be used for a binary comparison
danielk1977a37cdde2004-05-16 11:15:36 +0000162** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
danielk1977a37cdde2004-05-16 11:15:36 +0000163*/
drh35573352008-01-08 23:54:25 +0000164static u8 binaryCompareP5(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){
165 u8 aff = (char)sqlite3ExprAffinity(pExpr2);
166 aff = sqlite3CompareAffinity(pExpr1, aff) | jumpIfNull;
167 return aff;
danielk1977a37cdde2004-05-16 11:15:36 +0000168}
169
drha2e00042002-01-22 03:13:42 +0000170/*
danielk19770202b292004-06-09 09:55:16 +0000171** Return a pointer to the collation sequence that should be used by
172** a binary comparison operator comparing pLeft and pRight.
173**
174** If the left hand expression has a collating sequence type, then it is
175** used. Otherwise the collation sequence for the right hand expression
176** is used, or the default (BINARY) if neither expression has a collating
177** type.
danielk1977bcbb04e2007-05-29 12:11:29 +0000178**
179** Argument pRight (but not pLeft) may be a null pointer. In this case,
180** it is not considered.
danielk19770202b292004-06-09 09:55:16 +0000181*/
drh0a0e1312007-08-07 17:04:59 +0000182CollSeq *sqlite3BinaryCompareCollSeq(
danielk1977bcbb04e2007-05-29 12:11:29 +0000183 Parse *pParse,
184 Expr *pLeft,
185 Expr *pRight
186){
drhec41dda2007-02-07 13:09:45 +0000187 CollSeq *pColl;
188 assert( pLeft );
drhec41dda2007-02-07 13:09:45 +0000189 if( pLeft->flags & EP_ExpCollate ){
190 assert( pLeft->pColl );
191 pColl = pLeft->pColl;
danielk1977bcbb04e2007-05-29 12:11:29 +0000192 }else if( pRight && pRight->flags & EP_ExpCollate ){
drhec41dda2007-02-07 13:09:45 +0000193 assert( pRight->pColl );
194 pColl = pRight->pColl;
195 }else{
196 pColl = sqlite3ExprCollSeq(pParse, pLeft);
197 if( !pColl ){
198 pColl = sqlite3ExprCollSeq(pParse, pRight);
199 }
danielk19770202b292004-06-09 09:55:16 +0000200 }
201 return pColl;
202}
203
204/*
drhbe5c89a2004-07-26 00:31:09 +0000205** Generate code for a comparison operator.
206*/
207static int codeCompare(
208 Parse *pParse, /* The parsing (and code generating) context */
209 Expr *pLeft, /* The left operand */
210 Expr *pRight, /* The right operand */
211 int opcode, /* The comparison opcode */
drh35573352008-01-08 23:54:25 +0000212 int in1, int in2, /* Register holding operands */
drhbe5c89a2004-07-26 00:31:09 +0000213 int dest, /* Jump here if true. */
214 int jumpIfNull /* If true, jump if either operand is NULL */
215){
drh35573352008-01-08 23:54:25 +0000216 int p5;
217 int addr;
218 CollSeq *p4;
219
220 p4 = sqlite3BinaryCompareCollSeq(pParse, pLeft, pRight);
221 p5 = binaryCompareP5(pLeft, pRight, jumpIfNull);
222 addr = sqlite3VdbeAddOp4(pParse->pVdbe, opcode, in2, dest, in1,
223 (void*)p4, P4_COLLSEQ);
224 sqlite3VdbeChangeP5(pParse->pVdbe, p5);
drh2f7794c2008-04-01 03:27:39 +0000225 if( p5 & SQLITE_AFF_MASK ){
226 sqlite3ExprExpireColumnCacheLines(pParse, in1, in1);
227 sqlite3ExprExpireColumnCacheLines(pParse, in2, in2);
228 }
drh35573352008-01-08 23:54:25 +0000229 return addr;
drhbe5c89a2004-07-26 00:31:09 +0000230}
231
232/*
drha76b5df2002-02-23 02:32:10 +0000233** Construct a new expression node and return a pointer to it. Memory
drh17435752007-08-16 04:30:38 +0000234** for this node is obtained from sqlite3_malloc(). The calling function
drha76b5df2002-02-23 02:32:10 +0000235** is responsible for making sure the node eventually gets freed.
236*/
drh17435752007-08-16 04:30:38 +0000237Expr *sqlite3Expr(
danielk1977a1644fd2007-08-29 12:31:25 +0000238 sqlite3 *db, /* Handle for sqlite3DbMallocZero() (may be null) */
drh17435752007-08-16 04:30:38 +0000239 int op, /* Expression opcode */
240 Expr *pLeft, /* Left operand */
241 Expr *pRight, /* Right operand */
242 const Token *pToken /* Argument token */
243){
drha76b5df2002-02-23 02:32:10 +0000244 Expr *pNew;
danielk1977a1644fd2007-08-29 12:31:25 +0000245 pNew = sqlite3DbMallocZero(db, sizeof(Expr));
drha76b5df2002-02-23 02:32:10 +0000246 if( pNew==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000247 /* When malloc fails, delete pLeft and pRight. Expressions passed to
248 ** this function must always be allocated with sqlite3Expr() for this
249 ** reason.
250 */
251 sqlite3ExprDelete(pLeft);
252 sqlite3ExprDelete(pRight);
drha76b5df2002-02-23 02:32:10 +0000253 return 0;
254 }
255 pNew->op = op;
256 pNew->pLeft = pLeft;
257 pNew->pRight = pRight;
danielk1977a58fdfb2005-02-08 07:50:40 +0000258 pNew->iAgg = -1;
drha76b5df2002-02-23 02:32:10 +0000259 if( pToken ){
drh4b59ab52002-08-24 18:24:51 +0000260 assert( pToken->dyn==0 );
drh145716b2004-09-24 12:24:06 +0000261 pNew->span = pNew->token = *pToken;
drha34001c2007-02-02 12:44:37 +0000262 }else if( pLeft ){
263 if( pRight ){
264 sqlite3ExprSpan(pNew, &pLeft->span, &pRight->span);
drh5ffb3ac2007-04-18 17:07:57 +0000265 if( pRight->flags & EP_ExpCollate ){
drha34001c2007-02-02 12:44:37 +0000266 pNew->flags |= EP_ExpCollate;
267 pNew->pColl = pRight->pColl;
268 }
269 }
drh5ffb3ac2007-04-18 17:07:57 +0000270 if( pLeft->flags & EP_ExpCollate ){
drha34001c2007-02-02 12:44:37 +0000271 pNew->flags |= EP_ExpCollate;
272 pNew->pColl = pLeft->pColl;
273 }
drha76b5df2002-02-23 02:32:10 +0000274 }
danielk1977fc976062007-05-10 10:46:56 +0000275
276 sqlite3ExprSetHeight(pNew);
drha76b5df2002-02-23 02:32:10 +0000277 return pNew;
278}
279
280/*
drh17435752007-08-16 04:30:38 +0000281** Works like sqlite3Expr() except that it takes an extra Parse*
282** argument and notifies the associated connection object if malloc fails.
drh206f3d92006-07-11 13:15:08 +0000283*/
drh17435752007-08-16 04:30:38 +0000284Expr *sqlite3PExpr(
285 Parse *pParse, /* Parsing context */
286 int op, /* Expression opcode */
287 Expr *pLeft, /* Left operand */
288 Expr *pRight, /* Right operand */
289 const Token *pToken /* Argument token */
290){
danielk1977a1644fd2007-08-29 12:31:25 +0000291 return sqlite3Expr(pParse->db, op, pLeft, pRight, pToken);
drh206f3d92006-07-11 13:15:08 +0000292}
293
294/*
drh4e0cff62004-11-05 05:10:28 +0000295** When doing a nested parse, you can include terms in an expression
drhb7654112008-01-12 12:48:07 +0000296** that look like this: #1 #2 ... These terms refer to registers
297** in the virtual machine. #N is the N-th register.
drh4e0cff62004-11-05 05:10:28 +0000298**
299** This routine is called by the parser to deal with on of those terms.
300** It immediately generates code to store the value in a memory location.
301** The returns an expression that will code to extract the value from
302** that memory location as needed.
303*/
304Expr *sqlite3RegisterExpr(Parse *pParse, Token *pToken){
305 Vdbe *v = pParse->pVdbe;
306 Expr *p;
drh4e0cff62004-11-05 05:10:28 +0000307 if( pParse->nested==0 ){
308 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", pToken);
danielk1977a1644fd2007-08-29 12:31:25 +0000309 return sqlite3PExpr(pParse, TK_NULL, 0, 0, 0);
drh4e0cff62004-11-05 05:10:28 +0000310 }
drhbb7ac002005-08-12 22:58:53 +0000311 if( v==0 ) return 0;
danielk1977a1644fd2007-08-29 12:31:25 +0000312 p = sqlite3PExpr(pParse, TK_REGISTER, 0, 0, pToken);
drh73c42a12004-11-20 18:13:10 +0000313 if( p==0 ){
314 return 0; /* Malloc failed */
315 }
drhb7654112008-01-12 12:48:07 +0000316 p->iTable = atoi((char*)&pToken->z[1]);
drh4e0cff62004-11-05 05:10:28 +0000317 return p;
318}
319
320/*
drh91bb0ee2004-09-01 03:06:34 +0000321** Join two expressions using an AND operator. If either expression is
322** NULL, then just return the other expression.
323*/
danielk19771e536952007-08-16 10:09:01 +0000324Expr *sqlite3ExprAnd(sqlite3 *db, Expr *pLeft, Expr *pRight){
drh91bb0ee2004-09-01 03:06:34 +0000325 if( pLeft==0 ){
326 return pRight;
327 }else if( pRight==0 ){
328 return pLeft;
329 }else{
danielk1977880c15b2007-09-01 18:24:55 +0000330 return sqlite3Expr(db, TK_AND, pLeft, pRight, 0);
drh91bb0ee2004-09-01 03:06:34 +0000331 }
332}
333
334/*
drh6977fea2002-10-22 23:38:04 +0000335** Set the Expr.span field of the given expression to span all
drha76b5df2002-02-23 02:32:10 +0000336** text between the two given tokens.
337*/
danielk19774adee202004-05-08 08:23:19 +0000338void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
drh4efc4752004-01-16 15:55:37 +0000339 assert( pRight!=0 );
340 assert( pLeft!=0 );
drhf3a65f72007-08-22 20:18:21 +0000341 if( pExpr && pRight->z && pLeft->z ){
drhad6d9462004-09-19 02:15:24 +0000342 assert( pLeft->dyn==0 || pLeft->z[pLeft->n]==0 );
drh145716b2004-09-24 12:24:06 +0000343 if( pLeft->dyn==0 && pRight->dyn==0 ){
drh6977fea2002-10-22 23:38:04 +0000344 pExpr->span.z = pLeft->z;
drh97903fe2005-05-24 20:19:57 +0000345 pExpr->span.n = pRight->n + (pRight->z - pLeft->z);
drh4b59ab52002-08-24 18:24:51 +0000346 }else{
drh6977fea2002-10-22 23:38:04 +0000347 pExpr->span.z = 0;
drh4b59ab52002-08-24 18:24:51 +0000348 }
drha76b5df2002-02-23 02:32:10 +0000349 }
350}
351
352/*
353** Construct a new expression node for a function with multiple
354** arguments.
355*/
drh17435752007-08-16 04:30:38 +0000356Expr *sqlite3ExprFunction(Parse *pParse, ExprList *pList, Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000357 Expr *pNew;
danielk19774b202ae2006-01-23 05:50:58 +0000358 assert( pToken );
drh17435752007-08-16 04:30:38 +0000359 pNew = sqlite3DbMallocZero(pParse->db, sizeof(Expr) );
drha76b5df2002-02-23 02:32:10 +0000360 if( pNew==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000361 sqlite3ExprListDelete(pList); /* Avoid leaking memory when malloc fails */
drha76b5df2002-02-23 02:32:10 +0000362 return 0;
363 }
364 pNew->op = TK_FUNCTION;
365 pNew->pList = pList;
danielk19774b202ae2006-01-23 05:50:58 +0000366 assert( pToken->dyn==0 );
367 pNew->token = *pToken;
drh6977fea2002-10-22 23:38:04 +0000368 pNew->span = pNew->token;
danielk1977fc976062007-05-10 10:46:56 +0000369
370 sqlite3ExprSetHeight(pNew);
drha76b5df2002-02-23 02:32:10 +0000371 return pNew;
372}
373
374/*
drhfa6bc002004-09-07 16:19:52 +0000375** Assign a variable number to an expression that encodes a wildcard
376** in the original SQL statement.
377**
378** Wildcards consisting of a single "?" are assigned the next sequential
379** variable number.
380**
381** Wildcards of the form "?nnn" are assigned the number "nnn". We make
382** sure "nnn" is not too be to avoid a denial of service attack when
383** the SQL statement comes from an external source.
384**
385** Wildcards of the form ":aaa" or "$aaa" are assigned the same number
386** as the previous instance of the same wildcard. Or if this is the first
387** instance of the wildcard, the next sequenial variable number is
388** assigned.
389*/
390void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){
391 Token *pToken;
drh17435752007-08-16 04:30:38 +0000392 sqlite3 *db = pParse->db;
393
drhfa6bc002004-09-07 16:19:52 +0000394 if( pExpr==0 ) return;
395 pToken = &pExpr->token;
396 assert( pToken->n>=1 );
397 assert( pToken->z!=0 );
398 assert( pToken->z[0]!=0 );
399 if( pToken->n==1 ){
400 /* Wildcard of the form "?". Assign the next variable number */
401 pExpr->iTable = ++pParse->nVar;
402 }else if( pToken->z[0]=='?' ){
403 /* Wildcard of the form "?nnn". Convert "nnn" to an integer and
404 ** use it as the variable number */
405 int i;
drh2646da72005-12-09 20:02:05 +0000406 pExpr->iTable = i = atoi((char*)&pToken->z[1]);
drhbb4957f2008-03-20 14:03:29 +0000407 if( i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
drhfa6bc002004-09-07 16:19:52 +0000408 sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
drhbb4957f2008-03-20 14:03:29 +0000409 db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]);
drhfa6bc002004-09-07 16:19:52 +0000410 }
411 if( i>pParse->nVar ){
412 pParse->nVar = i;
413 }
414 }else{
415 /* Wildcards of the form ":aaa" or "$aaa". Reuse the same variable
416 ** number as the prior appearance of the same name, or if the name
417 ** has never appeared before, reuse the same variable number
418 */
419 int i, n;
420 n = pToken->n;
421 for(i=0; i<pParse->nVarExpr; i++){
422 Expr *pE;
423 if( (pE = pParse->apVarExpr[i])!=0
424 && pE->token.n==n
425 && memcmp(pE->token.z, pToken->z, n)==0 ){
426 pExpr->iTable = pE->iTable;
427 break;
428 }
429 }
430 if( i>=pParse->nVarExpr ){
431 pExpr->iTable = ++pParse->nVar;
432 if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){
433 pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10;
drh17435752007-08-16 04:30:38 +0000434 pParse->apVarExpr =
435 sqlite3DbReallocOrFree(
436 db,
437 pParse->apVarExpr,
438 pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0])
439 );
drhfa6bc002004-09-07 16:19:52 +0000440 }
drh17435752007-08-16 04:30:38 +0000441 if( !db->mallocFailed ){
drhfa6bc002004-09-07 16:19:52 +0000442 assert( pParse->apVarExpr!=0 );
443 pParse->apVarExpr[pParse->nVarExpr++] = pExpr;
444 }
445 }
446 }
drhbb4957f2008-03-20 14:03:29 +0000447 if( !pParse->nErr && pParse->nVar>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
danielk1977832b2662007-05-09 11:37:22 +0000448 sqlite3ErrorMsg(pParse, "too many SQL variables");
449 }
drhfa6bc002004-09-07 16:19:52 +0000450}
451
452/*
drha2e00042002-01-22 03:13:42 +0000453** Recursively delete an expression tree.
454*/
danielk19774adee202004-05-08 08:23:19 +0000455void sqlite3ExprDelete(Expr *p){
drha2e00042002-01-22 03:13:42 +0000456 if( p==0 ) return;
drh17435752007-08-16 04:30:38 +0000457 if( p->span.dyn ) sqlite3_free((char*)p->span.z);
458 if( p->token.dyn ) sqlite3_free((char*)p->token.z);
danielk19774adee202004-05-08 08:23:19 +0000459 sqlite3ExprDelete(p->pLeft);
460 sqlite3ExprDelete(p->pRight);
461 sqlite3ExprListDelete(p->pList);
462 sqlite3SelectDelete(p->pSelect);
drh17435752007-08-16 04:30:38 +0000463 sqlite3_free(p);
drha2e00042002-01-22 03:13:42 +0000464}
465
drhd2687b72005-08-12 22:56:09 +0000466/*
467** The Expr.token field might be a string literal that is quoted.
468** If so, remove the quotation marks.
469*/
drh17435752007-08-16 04:30:38 +0000470void sqlite3DequoteExpr(sqlite3 *db, Expr *p){
drhd2687b72005-08-12 22:56:09 +0000471 if( ExprHasAnyProperty(p, EP_Dequoted) ){
472 return;
473 }
474 ExprSetProperty(p, EP_Dequoted);
475 if( p->token.dyn==0 ){
drh17435752007-08-16 04:30:38 +0000476 sqlite3TokenCopy(db, &p->token, &p->token);
drhd2687b72005-08-12 22:56:09 +0000477 }
478 sqlite3Dequote((char*)p->token.z);
479}
480
drha76b5df2002-02-23 02:32:10 +0000481
482/*
drhff78bd22002-02-27 01:47:11 +0000483** The following group of routines make deep copies of expressions,
484** expression lists, ID lists, and select statements. The copies can
485** be deleted (by being passed to their respective ...Delete() routines)
486** without effecting the originals.
487**
danielk19774adee202004-05-08 08:23:19 +0000488** The expression list, ID, and source lists return by sqlite3ExprListDup(),
489** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
drhad3cab52002-05-24 02:04:32 +0000490** by subsequent calls to sqlite*ListAppend() routines.
drhff78bd22002-02-27 01:47:11 +0000491**
drhad3cab52002-05-24 02:04:32 +0000492** Any tables that the SrcList might point to are not duplicated.
drhff78bd22002-02-27 01:47:11 +0000493*/
danielk19771e536952007-08-16 10:09:01 +0000494Expr *sqlite3ExprDup(sqlite3 *db, Expr *p){
drhff78bd22002-02-27 01:47:11 +0000495 Expr *pNew;
496 if( p==0 ) return 0;
drh17435752007-08-16 04:30:38 +0000497 pNew = sqlite3DbMallocRaw(db, sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000498 if( pNew==0 ) return 0;
drh3b167c72002-06-28 12:18:47 +0000499 memcpy(pNew, p, sizeof(*pNew));
drh6977fea2002-10-22 23:38:04 +0000500 if( p->token.z!=0 ){
drh17435752007-08-16 04:30:38 +0000501 pNew->token.z = (u8*)sqlite3DbStrNDup(db, (char*)p->token.z, p->token.n);
drh4b59ab52002-08-24 18:24:51 +0000502 pNew->token.dyn = 1;
503 }else{
drh4efc4752004-01-16 15:55:37 +0000504 assert( pNew->token.z==0 );
drh4b59ab52002-08-24 18:24:51 +0000505 }
drh6977fea2002-10-22 23:38:04 +0000506 pNew->span.z = 0;
drh17435752007-08-16 04:30:38 +0000507 pNew->pLeft = sqlite3ExprDup(db, p->pLeft);
508 pNew->pRight = sqlite3ExprDup(db, p->pRight);
509 pNew->pList = sqlite3ExprListDup(db, p->pList);
510 pNew->pSelect = sqlite3SelectDup(db, p->pSelect);
drhff78bd22002-02-27 01:47:11 +0000511 return pNew;
512}
drh17435752007-08-16 04:30:38 +0000513void sqlite3TokenCopy(sqlite3 *db, Token *pTo, Token *pFrom){
514 if( pTo->dyn ) sqlite3_free((char*)pTo->z);
drh4b59ab52002-08-24 18:24:51 +0000515 if( pFrom->z ){
516 pTo->n = pFrom->n;
drh17435752007-08-16 04:30:38 +0000517 pTo->z = (u8*)sqlite3DbStrNDup(db, (char*)pFrom->z, pFrom->n);
drh4b59ab52002-08-24 18:24:51 +0000518 pTo->dyn = 1;
519 }else{
drh4b59ab52002-08-24 18:24:51 +0000520 pTo->z = 0;
drh4b59ab52002-08-24 18:24:51 +0000521 }
522}
drh17435752007-08-16 04:30:38 +0000523ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p){
drhff78bd22002-02-27 01:47:11 +0000524 ExprList *pNew;
drh145716b2004-09-24 12:24:06 +0000525 struct ExprList_item *pItem, *pOldItem;
drhff78bd22002-02-27 01:47:11 +0000526 int i;
527 if( p==0 ) return 0;
drh17435752007-08-16 04:30:38 +0000528 pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +0000529 if( pNew==0 ) return 0;
danielk197731dad9d2007-08-16 11:36:15 +0000530 pNew->iECursor = 0;
drh4305d102003-07-30 12:34:12 +0000531 pNew->nExpr = pNew->nAlloc = p->nExpr;
drh17435752007-08-16 04:30:38 +0000532 pNew->a = pItem = sqlite3DbMallocRaw(db, p->nExpr*sizeof(p->a[0]) );
danielk1977e0048402004-06-15 16:51:01 +0000533 if( pItem==0 ){
drh17435752007-08-16 04:30:38 +0000534 sqlite3_free(pNew);
danielk1977e0048402004-06-15 16:51:01 +0000535 return 0;
536 }
drh145716b2004-09-24 12:24:06 +0000537 pOldItem = p->a;
538 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
drh4b59ab52002-08-24 18:24:51 +0000539 Expr *pNewExpr, *pOldExpr;
drh17435752007-08-16 04:30:38 +0000540 pItem->pExpr = pNewExpr = sqlite3ExprDup(db, pOldExpr = pOldItem->pExpr);
drh6977fea2002-10-22 23:38:04 +0000541 if( pOldExpr->span.z!=0 && pNewExpr ){
542 /* Always make a copy of the span for top-level expressions in the
drh4b59ab52002-08-24 18:24:51 +0000543 ** expression list. The logic in SELECT processing that determines
544 ** the names of columns in the result set needs this information */
drh17435752007-08-16 04:30:38 +0000545 sqlite3TokenCopy(db, &pNewExpr->span, &pOldExpr->span);
drh4b59ab52002-08-24 18:24:51 +0000546 }
drh1f3e9052002-10-31 00:09:39 +0000547 assert( pNewExpr==0 || pNewExpr->span.z!=0
drh6f7adc82006-01-11 21:41:20 +0000548 || pOldExpr->span.z==0
drh17435752007-08-16 04:30:38 +0000549 || db->mallocFailed );
550 pItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
drh145716b2004-09-24 12:24:06 +0000551 pItem->sortOrder = pOldItem->sortOrder;
552 pItem->isAgg = pOldItem->isAgg;
drh3e7bc9c2004-02-21 19:17:17 +0000553 pItem->done = 0;
drhff78bd22002-02-27 01:47:11 +0000554 }
555 return pNew;
556}
danielk197793758c82005-01-21 08:13:14 +0000557
558/*
559** If cursors, triggers, views and subqueries are all omitted from
560** the build, then none of the following routines, except for
561** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes
562** called with a NULL argument.
563*/
danielk19776a67fe82005-02-04 04:07:16 +0000564#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \
565 || !defined(SQLITE_OMIT_SUBQUERY)
drh17435752007-08-16 04:30:38 +0000566SrcList *sqlite3SrcListDup(sqlite3 *db, SrcList *p){
drhad3cab52002-05-24 02:04:32 +0000567 SrcList *pNew;
568 int i;
drh113088e2003-03-20 01:16:58 +0000569 int nByte;
drhad3cab52002-05-24 02:04:32 +0000570 if( p==0 ) return 0;
drh113088e2003-03-20 01:16:58 +0000571 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
drh17435752007-08-16 04:30:38 +0000572 pNew = sqlite3DbMallocRaw(db, nByte );
drhad3cab52002-05-24 02:04:32 +0000573 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000574 pNew->nSrc = pNew->nAlloc = p->nSrc;
drhad3cab52002-05-24 02:04:32 +0000575 for(i=0; i<p->nSrc; i++){
drh4efc4752004-01-16 15:55:37 +0000576 struct SrcList_item *pNewItem = &pNew->a[i];
577 struct SrcList_item *pOldItem = &p->a[i];
drhed8a3bb2005-06-06 21:19:56 +0000578 Table *pTab;
drh17435752007-08-16 04:30:38 +0000579 pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase);
580 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
581 pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias);
drh4efc4752004-01-16 15:55:37 +0000582 pNewItem->jointype = pOldItem->jointype;
583 pNewItem->iCursor = pOldItem->iCursor;
danielk19771787cca2006-02-10 07:07:14 +0000584 pNewItem->isPopulated = pOldItem->isPopulated;
drhed8a3bb2005-06-06 21:19:56 +0000585 pTab = pNewItem->pTab = pOldItem->pTab;
586 if( pTab ){
587 pTab->nRef++;
danielk1977a1cb1832005-02-12 08:59:55 +0000588 }
drh17435752007-08-16 04:30:38 +0000589 pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect);
590 pNewItem->pOn = sqlite3ExprDup(db, pOldItem->pOn);
591 pNewItem->pUsing = sqlite3IdListDup(db, pOldItem->pUsing);
danielk19776c18b6e2005-01-30 09:17:58 +0000592 pNewItem->colUsed = pOldItem->colUsed;
drhad3cab52002-05-24 02:04:32 +0000593 }
594 return pNew;
595}
drh17435752007-08-16 04:30:38 +0000596IdList *sqlite3IdListDup(sqlite3 *db, IdList *p){
drhff78bd22002-02-27 01:47:11 +0000597 IdList *pNew;
598 int i;
599 if( p==0 ) return 0;
drh17435752007-08-16 04:30:38 +0000600 pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +0000601 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000602 pNew->nId = pNew->nAlloc = p->nId;
drh17435752007-08-16 04:30:38 +0000603 pNew->a = sqlite3DbMallocRaw(db, p->nId*sizeof(p->a[0]) );
danielk1977d5d56522005-03-16 12:15:20 +0000604 if( pNew->a==0 ){
drh17435752007-08-16 04:30:38 +0000605 sqlite3_free(pNew);
danielk1977d5d56522005-03-16 12:15:20 +0000606 return 0;
607 }
drhff78bd22002-02-27 01:47:11 +0000608 for(i=0; i<p->nId; i++){
drh4efc4752004-01-16 15:55:37 +0000609 struct IdList_item *pNewItem = &pNew->a[i];
610 struct IdList_item *pOldItem = &p->a[i];
drh17435752007-08-16 04:30:38 +0000611 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
drh4efc4752004-01-16 15:55:37 +0000612 pNewItem->idx = pOldItem->idx;
drhff78bd22002-02-27 01:47:11 +0000613 }
614 return pNew;
615}
drh17435752007-08-16 04:30:38 +0000616Select *sqlite3SelectDup(sqlite3 *db, Select *p){
drhff78bd22002-02-27 01:47:11 +0000617 Select *pNew;
618 if( p==0 ) return 0;
drh17435752007-08-16 04:30:38 +0000619 pNew = sqlite3DbMallocRaw(db, sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000620 if( pNew==0 ) return 0;
621 pNew->isDistinct = p->isDistinct;
drh17435752007-08-16 04:30:38 +0000622 pNew->pEList = sqlite3ExprListDup(db, p->pEList);
623 pNew->pSrc = sqlite3SrcListDup(db, p->pSrc);
624 pNew->pWhere = sqlite3ExprDup(db, p->pWhere);
625 pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy);
626 pNew->pHaving = sqlite3ExprDup(db, p->pHaving);
627 pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy);
drhff78bd22002-02-27 01:47:11 +0000628 pNew->op = p->op;
drh17435752007-08-16 04:30:38 +0000629 pNew->pPrior = sqlite3SelectDup(db, p->pPrior);
630 pNew->pLimit = sqlite3ExprDup(db, p->pLimit);
631 pNew->pOffset = sqlite3ExprDup(db, p->pOffset);
drh7b58dae2003-07-20 01:16:46 +0000632 pNew->iLimit = -1;
633 pNew->iOffset = -1;
danielk1977a1cb1832005-02-12 08:59:55 +0000634 pNew->isResolved = p->isResolved;
635 pNew->isAgg = p->isAgg;
drhb9bb7c12006-06-11 23:41:55 +0000636 pNew->usesEphm = 0;
drh8e647b82005-09-23 21:11:53 +0000637 pNew->disallowOrderBy = 0;
drh0342b1f2005-09-01 03:07:44 +0000638 pNew->pRightmost = 0;
drhb9bb7c12006-06-11 23:41:55 +0000639 pNew->addrOpenEphm[0] = -1;
640 pNew->addrOpenEphm[1] = -1;
641 pNew->addrOpenEphm[2] = -1;
drhff78bd22002-02-27 01:47:11 +0000642 return pNew;
643}
danielk197793758c82005-01-21 08:13:14 +0000644#else
drh17435752007-08-16 04:30:38 +0000645Select *sqlite3SelectDup(sqlite3 *db, Select *p){
danielk197793758c82005-01-21 08:13:14 +0000646 assert( p==0 );
647 return 0;
648}
649#endif
drhff78bd22002-02-27 01:47:11 +0000650
651
652/*
drha76b5df2002-02-23 02:32:10 +0000653** Add a new element to the end of an expression list. If pList is
654** initially NULL, then create a new expression list.
655*/
drh17435752007-08-16 04:30:38 +0000656ExprList *sqlite3ExprListAppend(
657 Parse *pParse, /* Parsing context */
658 ExprList *pList, /* List to which to append. Might be NULL */
659 Expr *pExpr, /* Expression to be appended */
660 Token *pName /* AS keyword for the expression */
661){
662 sqlite3 *db = pParse->db;
drha76b5df2002-02-23 02:32:10 +0000663 if( pList==0 ){
drh17435752007-08-16 04:30:38 +0000664 pList = sqlite3DbMallocZero(db, sizeof(ExprList) );
drha76b5df2002-02-23 02:32:10 +0000665 if( pList==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000666 goto no_mem;
drha76b5df2002-02-23 02:32:10 +0000667 }
drh4efc4752004-01-16 15:55:37 +0000668 assert( pList->nAlloc==0 );
drha76b5df2002-02-23 02:32:10 +0000669 }
drh4305d102003-07-30 12:34:12 +0000670 if( pList->nAlloc<=pList->nExpr ){
danielk1977d5d56522005-03-16 12:15:20 +0000671 struct ExprList_item *a;
672 int n = pList->nAlloc*2 + 4;
danielk197726783a52007-08-29 14:06:22 +0000673 a = sqlite3DbRealloc(db, pList->a, n*sizeof(pList->a[0]));
danielk1977d5d56522005-03-16 12:15:20 +0000674 if( a==0 ){
675 goto no_mem;
drha76b5df2002-02-23 02:32:10 +0000676 }
danielk1977d5d56522005-03-16 12:15:20 +0000677 pList->a = a;
678 pList->nAlloc = n;
drha76b5df2002-02-23 02:32:10 +0000679 }
drh4efc4752004-01-16 15:55:37 +0000680 assert( pList->a!=0 );
681 if( pExpr || pName ){
682 struct ExprList_item *pItem = &pList->a[pList->nExpr++];
683 memset(pItem, 0, sizeof(*pItem));
drh17435752007-08-16 04:30:38 +0000684 pItem->zName = sqlite3NameFromToken(db, pName);
danielk1977e94ddc92005-03-21 03:53:38 +0000685 pItem->pExpr = pExpr;
drha76b5df2002-02-23 02:32:10 +0000686 }
687 return pList;
danielk1977d5d56522005-03-16 12:15:20 +0000688
689no_mem:
690 /* Avoid leaking memory if malloc has failed. */
691 sqlite3ExprDelete(pExpr);
692 sqlite3ExprListDelete(pList);
693 return 0;
drha76b5df2002-02-23 02:32:10 +0000694}
695
696/*
danielk19777a15a4b2007-05-08 17:54:43 +0000697** If the expression list pEList contains more than iLimit elements,
698** leave an error message in pParse.
699*/
700void sqlite3ExprListCheckLength(
701 Parse *pParse,
702 ExprList *pEList,
danielk19777a15a4b2007-05-08 17:54:43 +0000703 const char *zObject
704){
drhb1a6c3c2008-03-20 16:30:17 +0000705 int mx = pParse->db->aLimit[SQLITE_LIMIT_COLUMN];
706 if( pEList && pEList->nExpr>mx ){
danielk19777a15a4b2007-05-08 17:54:43 +0000707 sqlite3ErrorMsg(pParse, "too many columns in %s", zObject);
708 }
709}
710
danielk1977fc976062007-05-10 10:46:56 +0000711
danielk1977fc976062007-05-10 10:46:56 +0000712/* The following three functions, heightOfExpr(), heightOfExprList()
713** and heightOfSelect(), are used to determine the maximum height
714** of any expression tree referenced by the structure passed as the
715** first argument.
716**
717** If this maximum height is greater than the current value pointed
718** to by pnHeight, the second parameter, then set *pnHeight to that
719** value.
720*/
721static void heightOfExpr(Expr *p, int *pnHeight){
722 if( p ){
723 if( p->nHeight>*pnHeight ){
724 *pnHeight = p->nHeight;
725 }
726 }
727}
728static void heightOfExprList(ExprList *p, int *pnHeight){
729 if( p ){
730 int i;
731 for(i=0; i<p->nExpr; i++){
732 heightOfExpr(p->a[i].pExpr, pnHeight);
733 }
734 }
735}
736static void heightOfSelect(Select *p, int *pnHeight){
737 if( p ){
738 heightOfExpr(p->pWhere, pnHeight);
739 heightOfExpr(p->pHaving, pnHeight);
740 heightOfExpr(p->pLimit, pnHeight);
741 heightOfExpr(p->pOffset, pnHeight);
742 heightOfExprList(p->pEList, pnHeight);
743 heightOfExprList(p->pGroupBy, pnHeight);
744 heightOfExprList(p->pOrderBy, pnHeight);
745 heightOfSelect(p->pPrior, pnHeight);
746 }
747}
748
749/*
750** Set the Expr.nHeight variable in the structure passed as an
751** argument. An expression with no children, Expr.pList or
752** Expr.pSelect member has a height of 1. Any other expression
753** has a height equal to the maximum height of any other
754** referenced Expr plus one.
755*/
756void sqlite3ExprSetHeight(Expr *p){
757 int nHeight = 0;
758 heightOfExpr(p->pLeft, &nHeight);
759 heightOfExpr(p->pRight, &nHeight);
760 heightOfExprList(p->pList, &nHeight);
761 heightOfSelect(p->pSelect, &nHeight);
762 p->nHeight = nHeight + 1;
763}
764
765/*
766** Return the maximum height of any expression tree referenced
767** by the select statement passed as an argument.
768*/
769int sqlite3SelectExprHeight(Select *p){
770 int nHeight = 0;
771 heightOfSelect(p, &nHeight);
772 return nHeight;
773}
danielk1977fc976062007-05-10 10:46:56 +0000774
danielk19777a15a4b2007-05-08 17:54:43 +0000775/*
drha76b5df2002-02-23 02:32:10 +0000776** Delete an entire expression list.
777*/
danielk19774adee202004-05-08 08:23:19 +0000778void sqlite3ExprListDelete(ExprList *pList){
drha76b5df2002-02-23 02:32:10 +0000779 int i;
drhbe5c89a2004-07-26 00:31:09 +0000780 struct ExprList_item *pItem;
drha76b5df2002-02-23 02:32:10 +0000781 if( pList==0 ) return;
drh1bdd9b52004-04-23 17:04:44 +0000782 assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
783 assert( pList->nExpr<=pList->nAlloc );
drhbe5c89a2004-07-26 00:31:09 +0000784 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
785 sqlite3ExprDelete(pItem->pExpr);
drh17435752007-08-16 04:30:38 +0000786 sqlite3_free(pItem->zName);
drha76b5df2002-02-23 02:32:10 +0000787 }
drh17435752007-08-16 04:30:38 +0000788 sqlite3_free(pList->a);
789 sqlite3_free(pList);
drha76b5df2002-02-23 02:32:10 +0000790}
791
792/*
drh678ccce2008-03-31 18:19:54 +0000793** Walk an expression tree. Call xFunc for each node visited. xFunc
794** is called on the node before xFunc is called on the nodes children.
drh73b211a2005-01-18 04:00:42 +0000795**
drh626a8792005-01-17 22:08:19 +0000796** The return value from xFunc determines whether the tree walk continues.
797** 0 means continue walking the tree. 1 means do not walk children
798** of the current node but continue with siblings. 2 means abandon
799** the tree walk completely.
800**
801** The return value from this routine is 1 to abandon the tree walk
802** and 0 to continue.
drh87abf5c2005-08-25 12:45:04 +0000803**
804** NOTICE: This routine does *not* descend into subqueries.
drh626a8792005-01-17 22:08:19 +0000805*/
danielk1977a58fdfb2005-02-08 07:50:40 +0000806static int walkExprList(ExprList *, int (*)(void *, Expr*), void *);
drh626a8792005-01-17 22:08:19 +0000807static int walkExprTree(Expr *pExpr, int (*xFunc)(void*,Expr*), void *pArg){
drh626a8792005-01-17 22:08:19 +0000808 int rc;
809 if( pExpr==0 ) return 0;
810 rc = (*xFunc)(pArg, pExpr);
811 if( rc==0 ){
812 if( walkExprTree(pExpr->pLeft, xFunc, pArg) ) return 1;
813 if( walkExprTree(pExpr->pRight, xFunc, pArg) ) return 1;
danielk1977a58fdfb2005-02-08 07:50:40 +0000814 if( walkExprList(pExpr->pList, xFunc, pArg) ) return 1;
drh626a8792005-01-17 22:08:19 +0000815 }
816 return rc>1;
817}
818
819/*
danielk1977a58fdfb2005-02-08 07:50:40 +0000820** Call walkExprTree() for every expression in list p.
821*/
822static int walkExprList(ExprList *p, int (*xFunc)(void *, Expr*), void *pArg){
823 int i;
824 struct ExprList_item *pItem;
825 if( !p ) return 0;
826 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
827 if( walkExprTree(pItem->pExpr, xFunc, pArg) ) return 1;
828 }
829 return 0;
830}
831
832/*
833** Call walkExprTree() for every expression in Select p, not including
834** expressions that are part of sub-selects in any FROM clause or the LIMIT
835** or OFFSET expressions..
836*/
837static int walkSelectExpr(Select *p, int (*xFunc)(void *, Expr*), void *pArg){
838 walkExprList(p->pEList, xFunc, pArg);
839 walkExprTree(p->pWhere, xFunc, pArg);
840 walkExprList(p->pGroupBy, xFunc, pArg);
841 walkExprTree(p->pHaving, xFunc, pArg);
842 walkExprList(p->pOrderBy, xFunc, pArg);
danielk197715d79822007-05-15 07:00:34 +0000843 if( p->pPrior ){
844 walkSelectExpr(p->pPrior, xFunc, pArg);
845 }
danielk1977a58fdfb2005-02-08 07:50:40 +0000846 return 0;
847}
848
849
850/*
drh626a8792005-01-17 22:08:19 +0000851** This routine is designed as an xFunc for walkExprTree().
852**
853** pArg is really a pointer to an integer. If we can tell by looking
drh73b211a2005-01-18 04:00:42 +0000854** at pExpr that the expression that contains pExpr is not a constant
855** expression, then set *pArg to 0 and return 2 to abandon the tree walk.
856** If pExpr does does not disqualify the expression from being a constant
857** then do nothing.
858**
859** After walking the whole tree, if no nodes are found that disqualify
860** the expression as constant, then we assume the whole expression
861** is constant. See sqlite3ExprIsConstant() for additional information.
drh626a8792005-01-17 22:08:19 +0000862*/
863static int exprNodeIsConstant(void *pArg, Expr *pExpr){
drh0a168372007-06-08 00:20:47 +0000864 int *pN = (int*)pArg;
865
866 /* If *pArg is 3 then any term of the expression that comes from
867 ** the ON or USING clauses of a join disqualifies the expression
868 ** from being considered constant. */
869 if( (*pN)==3 && ExprHasAnyProperty(pExpr, EP_FromJoin) ){
870 *pN = 0;
871 return 2;
872 }
873
drh626a8792005-01-17 22:08:19 +0000874 switch( pExpr->op ){
drheb55bd22005-06-30 17:04:21 +0000875 /* Consider functions to be constant if all their arguments are constant
876 ** and *pArg==2 */
877 case TK_FUNCTION:
drh0a168372007-06-08 00:20:47 +0000878 if( (*pN)==2 ) return 0;
drheb55bd22005-06-30 17:04:21 +0000879 /* Fall through */
drh626a8792005-01-17 22:08:19 +0000880 case TK_ID:
881 case TK_COLUMN:
882 case TK_DOT:
883 case TK_AGG_FUNCTION:
drh13449892005-09-07 21:22:45 +0000884 case TK_AGG_COLUMN:
drhfe2093d2005-01-20 22:48:47 +0000885#ifndef SQLITE_OMIT_SUBQUERY
886 case TK_SELECT:
887 case TK_EXISTS:
888#endif
drh0a168372007-06-08 00:20:47 +0000889 *pN = 0;
drh626a8792005-01-17 22:08:19 +0000890 return 2;
drh87abf5c2005-08-25 12:45:04 +0000891 case TK_IN:
892 if( pExpr->pSelect ){
drh0a168372007-06-08 00:20:47 +0000893 *pN = 0;
drh87abf5c2005-08-25 12:45:04 +0000894 return 2;
895 }
drh626a8792005-01-17 22:08:19 +0000896 default:
897 return 0;
898 }
899}
900
901/*
drhfef52082000-06-06 01:50:43 +0000902** Walk an expression tree. Return 1 if the expression is constant
drheb55bd22005-06-30 17:04:21 +0000903** and 0 if it involves variables or function calls.
drh23989372002-05-21 13:43:04 +0000904**
905** For the purposes of this function, a double-quoted string (ex: "abc")
906** is considered a variable but a single-quoted string (ex: 'abc') is
907** a constant.
drhfef52082000-06-06 01:50:43 +0000908*/
danielk19774adee202004-05-08 08:23:19 +0000909int sqlite3ExprIsConstant(Expr *p){
drh626a8792005-01-17 22:08:19 +0000910 int isConst = 1;
911 walkExprTree(p, exprNodeIsConstant, &isConst);
912 return isConst;
drhfef52082000-06-06 01:50:43 +0000913}
914
915/*
drheb55bd22005-06-30 17:04:21 +0000916** Walk an expression tree. Return 1 if the expression is constant
drh0a168372007-06-08 00:20:47 +0000917** that does no originate from the ON or USING clauses of a join.
918** Return 0 if it involves variables or function calls or terms from
919** an ON or USING clause.
920*/
921int sqlite3ExprIsConstantNotJoin(Expr *p){
922 int isConst = 3;
923 walkExprTree(p, exprNodeIsConstant, &isConst);
924 return isConst!=0;
925}
926
927/*
928** Walk an expression tree. Return 1 if the expression is constant
drheb55bd22005-06-30 17:04:21 +0000929** or a function call with constant arguments. Return and 0 if there
930** are any variables.
931**
932** For the purposes of this function, a double-quoted string (ex: "abc")
933** is considered a variable but a single-quoted string (ex: 'abc') is
934** a constant.
935*/
936int sqlite3ExprIsConstantOrFunction(Expr *p){
937 int isConst = 2;
938 walkExprTree(p, exprNodeIsConstant, &isConst);
939 return isConst!=0;
940}
941
942/*
drh73b211a2005-01-18 04:00:42 +0000943** If the expression p codes a constant integer that is small enough
drh202b2df2004-01-06 01:13:46 +0000944** to fit in a 32-bit integer, return 1 and put the value of the integer
945** in *pValue. If the expression is not an integer or if it is too big
946** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
drhe4de1fe2002-06-02 16:09:01 +0000947*/
danielk19774adee202004-05-08 08:23:19 +0000948int sqlite3ExprIsInteger(Expr *p, int *pValue){
drhe4de1fe2002-06-02 16:09:01 +0000949 switch( p->op ){
950 case TK_INTEGER: {
drh2646da72005-12-09 20:02:05 +0000951 if( sqlite3GetInt32((char*)p->token.z, pValue) ){
drh202b2df2004-01-06 01:13:46 +0000952 return 1;
953 }
954 break;
drhe4de1fe2002-06-02 16:09:01 +0000955 }
drh4b59ab52002-08-24 18:24:51 +0000956 case TK_UPLUS: {
danielk19774adee202004-05-08 08:23:19 +0000957 return sqlite3ExprIsInteger(p->pLeft, pValue);
drh4b59ab52002-08-24 18:24:51 +0000958 }
drhe4de1fe2002-06-02 16:09:01 +0000959 case TK_UMINUS: {
960 int v;
danielk19774adee202004-05-08 08:23:19 +0000961 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
drhe4de1fe2002-06-02 16:09:01 +0000962 *pValue = -v;
963 return 1;
964 }
965 break;
966 }
967 default: break;
968 }
969 return 0;
970}
971
972/*
drhc4a3c772001-04-04 11:48:57 +0000973** Return TRUE if the given string is a row-id column name.
974*/
danielk19774adee202004-05-08 08:23:19 +0000975int sqlite3IsRowid(const char *z){
976 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
977 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
978 if( sqlite3StrICmp(z, "OID")==0 ) return 1;
drhc4a3c772001-04-04 11:48:57 +0000979 return 0;
980}
981
982/*
drh8141f612004-01-25 22:44:58 +0000983** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
984** that name in the set of source tables in pSrcList and make the pExpr
985** expression node refer back to that source column. The following changes
986** are made to pExpr:
987**
988** pExpr->iDb Set the index in db->aDb[] of the database holding
989** the table.
990** pExpr->iTable Set to the cursor number for the table obtained
991** from pSrcList.
992** pExpr->iColumn Set to the column number within the table.
drh8141f612004-01-25 22:44:58 +0000993** pExpr->op Set to TK_COLUMN.
994** pExpr->pLeft Any expression this points to is deleted
995** pExpr->pRight Any expression this points to is deleted.
996**
997** The pDbToken is the name of the database (the "X"). This value may be
998** NULL meaning that name is of the form Y.Z or Z. Any available database
999** can be used. The pTableToken is the name of the table (the "Y"). This
1000** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it
1001** means that the form of the name is Z and that columns from any table
1002** can be used.
1003**
1004** If the name cannot be resolved unambiguously, leave an error message
1005** in pParse and return non-zero. Return zero on success.
1006*/
1007static int lookupName(
drhffe07b22005-11-03 00:41:17 +00001008 Parse *pParse, /* The parsing context */
drh8141f612004-01-25 22:44:58 +00001009 Token *pDbToken, /* Name of the database containing table, or NULL */
1010 Token *pTableToken, /* Name of table containing column, or NULL */
1011 Token *pColumnToken, /* Name of the column. */
drh626a8792005-01-17 22:08:19 +00001012 NameContext *pNC, /* The name context used to resolve the name */
drh8141f612004-01-25 22:44:58 +00001013 Expr *pExpr /* Make this EXPR node point to the selected column */
1014){
1015 char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */
1016 char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */
1017 char *zCol = 0; /* Name of the column. The "Z" */
1018 int i, j; /* Loop counters */
1019 int cnt = 0; /* Number of matching column names */
1020 int cntTab = 0; /* Number of matching table names */
drh626a8792005-01-17 22:08:19 +00001021 sqlite3 *db = pParse->db; /* The database */
drh51669862004-12-18 18:40:26 +00001022 struct SrcList_item *pItem; /* Use for looping over pSrcList items */
1023 struct SrcList_item *pMatch = 0; /* The matching pSrcList item */
drh73b211a2005-01-18 04:00:42 +00001024 NameContext *pTopNC = pNC; /* First namecontext in the list */
drh728b5772007-09-18 15:55:07 +00001025 Schema *pSchema = 0; /* Schema of the expression */
drh8141f612004-01-25 22:44:58 +00001026
1027 assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
drh17435752007-08-16 04:30:38 +00001028 zDb = sqlite3NameFromToken(db, pDbToken);
1029 zTab = sqlite3NameFromToken(db, pTableToken);
1030 zCol = sqlite3NameFromToken(db, pColumnToken);
1031 if( db->mallocFailed ){
danielk1977d5d56522005-03-16 12:15:20 +00001032 goto lookupname_end;
drh8141f612004-01-25 22:44:58 +00001033 }
drh8141f612004-01-25 22:44:58 +00001034
1035 pExpr->iTable = -1;
drh626a8792005-01-17 22:08:19 +00001036 while( pNC && cnt==0 ){
drhffe07b22005-11-03 00:41:17 +00001037 ExprList *pEList;
drh626a8792005-01-17 22:08:19 +00001038 SrcList *pSrcList = pNC->pSrcList;
drh8141f612004-01-25 22:44:58 +00001039
danielk1977b3bce662005-01-29 08:32:43 +00001040 if( pSrcList ){
1041 for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
drh43617e92006-03-06 20:55:46 +00001042 Table *pTab;
1043 int iDb;
danielk1977b3bce662005-01-29 08:32:43 +00001044 Column *pCol;
1045
drh43617e92006-03-06 20:55:46 +00001046 pTab = pItem->pTab;
1047 assert( pTab!=0 );
1048 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
danielk1977b3bce662005-01-29 08:32:43 +00001049 assert( pTab->nCol>0 );
1050 if( zTab ){
1051 if( pItem->zAlias ){
1052 char *zTabName = pItem->zAlias;
1053 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
1054 }else{
1055 char *zTabName = pTab->zName;
1056 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
danielk1977da184232006-01-05 11:34:32 +00001057 if( zDb!=0 && sqlite3StrICmp(db->aDb[iDb].zName, zDb)!=0 ){
danielk1977b3bce662005-01-29 08:32:43 +00001058 continue;
1059 }
drh626a8792005-01-17 22:08:19 +00001060 }
drh8141f612004-01-25 22:44:58 +00001061 }
danielk1977b3bce662005-01-29 08:32:43 +00001062 if( 0==(cntTab++) ){
drh626a8792005-01-17 22:08:19 +00001063 pExpr->iTable = pItem->iCursor;
drh728b5772007-09-18 15:55:07 +00001064 pSchema = pTab->pSchema;
danielk1977b3bce662005-01-29 08:32:43 +00001065 pMatch = pItem;
1066 }
1067 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
1068 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
danielk1977b3bf5562006-01-10 17:58:23 +00001069 const char *zColl = pTab->aCol[j].zColl;
drh873fac02005-06-06 17:11:46 +00001070 IdList *pUsing;
danielk1977b3bce662005-01-29 08:32:43 +00001071 cnt++;
1072 pExpr->iTable = pItem->iCursor;
1073 pMatch = pItem;
drh728b5772007-09-18 15:55:07 +00001074 pSchema = pTab->pSchema;
danielk1977b3bce662005-01-29 08:32:43 +00001075 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
1076 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
1077 pExpr->affinity = pTab->aCol[j].affinity;
drh8b4c40d2007-02-01 23:02:45 +00001078 if( (pExpr->flags & EP_ExpCollate)==0 ){
1079 pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0);
1080 }
drh61dfc312006-12-16 16:25:15 +00001081 if( i<pSrcList->nSrc-1 ){
1082 if( pItem[1].jointype & JT_NATURAL ){
1083 /* If this match occurred in the left table of a natural join,
1084 ** then skip the right table to avoid a duplicate match */
1085 pItem++;
1086 i++;
1087 }else if( (pUsing = pItem[1].pUsing)!=0 ){
1088 /* If this match occurs on a column that is in the USING clause
1089 ** of a join, skip the search of the right table of the join
1090 ** to avoid a duplicate match there. */
1091 int k;
1092 for(k=0; k<pUsing->nId; k++){
1093 if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ){
1094 pItem++;
1095 i++;
1096 break;
1097 }
drh873fac02005-06-06 17:11:46 +00001098 }
1099 }
1100 }
danielk1977b3bce662005-01-29 08:32:43 +00001101 break;
1102 }
drh8141f612004-01-25 22:44:58 +00001103 }
1104 }
1105 }
drh626a8792005-01-17 22:08:19 +00001106
1107#ifndef SQLITE_OMIT_TRIGGER
1108 /* If we have not already resolved the name, then maybe
1109 ** it is a new.* or old.* trigger argument reference
1110 */
1111 if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
1112 TriggerStack *pTriggerStack = pParse->trigStack;
1113 Table *pTab = 0;
danielk19778f2c54e2008-01-01 19:02:09 +00001114 u32 *piColMask;
drh626a8792005-01-17 22:08:19 +00001115 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){
1116 pExpr->iTable = pTriggerStack->newIdx;
1117 assert( pTriggerStack->pTab );
1118 pTab = pTriggerStack->pTab;
danielk19778f2c54e2008-01-01 19:02:09 +00001119 piColMask = &(pTriggerStack->newColMask);
drh626a8792005-01-17 22:08:19 +00001120 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab)==0 ){
1121 pExpr->iTable = pTriggerStack->oldIdx;
1122 assert( pTriggerStack->pTab );
1123 pTab = pTriggerStack->pTab;
danielk19778f2c54e2008-01-01 19:02:09 +00001124 piColMask = &(pTriggerStack->oldColMask);
drh626a8792005-01-17 22:08:19 +00001125 }
1126
1127 if( pTab ){
danielk1977f0113002006-01-24 12:09:17 +00001128 int iCol;
drh626a8792005-01-17 22:08:19 +00001129 Column *pCol = pTab->aCol;
1130
drh728b5772007-09-18 15:55:07 +00001131 pSchema = pTab->pSchema;
drh626a8792005-01-17 22:08:19 +00001132 cntTab++;
danielk1977f0113002006-01-24 12:09:17 +00001133 for(iCol=0; iCol < pTab->nCol; iCol++, pCol++) {
drh626a8792005-01-17 22:08:19 +00001134 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
danielk1977f0113002006-01-24 12:09:17 +00001135 const char *zColl = pTab->aCol[iCol].zColl;
drh626a8792005-01-17 22:08:19 +00001136 cnt++;
danielk1977f0113002006-01-24 12:09:17 +00001137 pExpr->iColumn = iCol==pTab->iPKey ? -1 : iCol;
1138 pExpr->affinity = pTab->aCol[iCol].affinity;
drh8b4c40d2007-02-01 23:02:45 +00001139 if( (pExpr->flags & EP_ExpCollate)==0 ){
1140 pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0);
1141 }
danielk1977aee18ef2005-03-09 12:26:50 +00001142 pExpr->pTab = pTab;
danielk19778f2c54e2008-01-01 19:02:09 +00001143 if( iCol>=0 ){
1144 *piColMask |= ((u32)1<<iCol) | (iCol>=32?0xffffffff:0);
1145 }
drh626a8792005-01-17 22:08:19 +00001146 break;
1147 }
1148 }
1149 }
1150 }
drhb7f91642004-10-31 02:22:47 +00001151#endif /* !defined(SQLITE_OMIT_TRIGGER) */
drh8141f612004-01-25 22:44:58 +00001152
drh626a8792005-01-17 22:08:19 +00001153 /*
1154 ** Perhaps the name is a reference to the ROWID
1155 */
1156 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
1157 cnt = 1;
1158 pExpr->iColumn = -1;
drh8a512562005-11-14 22:29:05 +00001159 pExpr->affinity = SQLITE_AFF_INTEGER;
drh626a8792005-01-17 22:08:19 +00001160 }
drh8141f612004-01-25 22:44:58 +00001161
drh626a8792005-01-17 22:08:19 +00001162 /*
1163 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
1164 ** might refer to an result-set alias. This happens, for example, when
1165 ** we are resolving names in the WHERE clause of the following command:
1166 **
1167 ** SELECT a+b AS x FROM table WHERE x<10;
1168 **
1169 ** In cases like this, replace pExpr with a copy of the expression that
1170 ** forms the result set entry ("a+b" in the example) and return immediately.
1171 ** Note that the expression in the result set should have already been
1172 ** resolved by the time the WHERE clause is resolved.
1173 */
drhffe07b22005-11-03 00:41:17 +00001174 if( cnt==0 && (pEList = pNC->pEList)!=0 && zTab==0 ){
drh626a8792005-01-17 22:08:19 +00001175 for(j=0; j<pEList->nExpr; j++){
1176 char *zAs = pEList->a[j].zName;
1177 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
drh36379e92007-07-23 22:51:15 +00001178 Expr *pDup, *pOrig;
drh626a8792005-01-17 22:08:19 +00001179 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
drh4f07e5f2007-05-14 11:34:46 +00001180 assert( pExpr->pList==0 );
1181 assert( pExpr->pSelect==0 );
drh36379e92007-07-23 22:51:15 +00001182 pOrig = pEList->a[j].pExpr;
1183 if( !pNC->allowAgg && ExprHasProperty(pOrig, EP_Agg) ){
1184 sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs);
drh17435752007-08-16 04:30:38 +00001185 sqlite3_free(zCol);
drh36379e92007-07-23 22:51:15 +00001186 return 2;
1187 }
danielk19771e536952007-08-16 10:09:01 +00001188 pDup = sqlite3ExprDup(db, pOrig);
drh4f07e5f2007-05-14 11:34:46 +00001189 if( pExpr->flags & EP_ExpCollate ){
1190 pDup->pColl = pExpr->pColl;
1191 pDup->flags |= EP_ExpCollate;
1192 }
drh17435752007-08-16 04:30:38 +00001193 if( pExpr->span.dyn ) sqlite3_free((char*)pExpr->span.z);
1194 if( pExpr->token.dyn ) sqlite3_free((char*)pExpr->token.z);
drh4f07e5f2007-05-14 11:34:46 +00001195 memcpy(pExpr, pDup, sizeof(*pExpr));
drh17435752007-08-16 04:30:38 +00001196 sqlite3_free(pDup);
drh15ccce12005-05-23 15:06:39 +00001197 cnt = 1;
danielk1977c9cf6e32007-06-25 16:29:33 +00001198 pMatch = 0;
drh626a8792005-01-17 22:08:19 +00001199 assert( zTab==0 && zDb==0 );
drh15ccce12005-05-23 15:06:39 +00001200 goto lookupname_end_2;
drh626a8792005-01-17 22:08:19 +00001201 }
1202 }
1203 }
1204
1205 /* Advance to the next name context. The loop will exit when either
1206 ** we have a match (cnt>0) or when we run out of name contexts.
1207 */
1208 if( cnt==0 ){
1209 pNC = pNC->pNext;
1210 }
drh8141f612004-01-25 22:44:58 +00001211 }
1212
1213 /*
1214 ** If X and Y are NULL (in other words if only the column name Z is
1215 ** supplied) and the value of Z is enclosed in double-quotes, then
1216 ** Z is a string literal if it doesn't match any column names. In that
1217 ** case, we need to return right away and not make any changes to
1218 ** pExpr.
drh15ccce12005-05-23 15:06:39 +00001219 **
1220 ** Because no reference was made to outer contexts, the pNC->nRef
1221 ** fields are not changed in any context.
drh8141f612004-01-25 22:44:58 +00001222 */
1223 if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
drh17435752007-08-16 04:30:38 +00001224 sqlite3_free(zCol);
drh8141f612004-01-25 22:44:58 +00001225 return 0;
1226 }
1227
1228 /*
1229 ** cnt==0 means there was not match. cnt>1 means there were two or
1230 ** more matches. Either way, we have an error.
1231 */
1232 if( cnt!=1 ){
drhde4fcfd2008-01-19 23:50:26 +00001233 const char *zErr;
1234 zErr = cnt==0 ? "no such column" : "ambiguous column name";
drh8141f612004-01-25 22:44:58 +00001235 if( zDb ){
drhde4fcfd2008-01-19 23:50:26 +00001236 sqlite3ErrorMsg(pParse, "%s: %s.%s.%s", zErr, zDb, zTab, zCol);
drh8141f612004-01-25 22:44:58 +00001237 }else if( zTab ){
drhde4fcfd2008-01-19 23:50:26 +00001238 sqlite3ErrorMsg(pParse, "%s: %s.%s", zErr, zTab, zCol);
drh8141f612004-01-25 22:44:58 +00001239 }else{
drhde4fcfd2008-01-19 23:50:26 +00001240 sqlite3ErrorMsg(pParse, "%s: %s", zErr, zCol);
drh8141f612004-01-25 22:44:58 +00001241 }
drhde4fcfd2008-01-19 23:50:26 +00001242 pTopNC->nErr++;
drh8141f612004-01-25 22:44:58 +00001243 }
1244
drh51669862004-12-18 18:40:26 +00001245 /* If a column from a table in pSrcList is referenced, then record
1246 ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes
1247 ** bit 0 to be set. Column 1 sets bit 1. And so forth. If the
1248 ** column number is greater than the number of bits in the bitmask
1249 ** then set the high-order bit of the bitmask.
1250 */
1251 if( pExpr->iColumn>=0 && pMatch!=0 ){
1252 int n = pExpr->iColumn;
1253 if( n>=sizeof(Bitmask)*8 ){
1254 n = sizeof(Bitmask)*8-1;
1255 }
1256 assert( pMatch->iCursor==pExpr->iTable );
drhca83ac52007-02-01 01:40:44 +00001257 pMatch->colUsed |= ((Bitmask)1)<<n;
drh51669862004-12-18 18:40:26 +00001258 }
1259
danielk1977d5d56522005-03-16 12:15:20 +00001260lookupname_end:
drh8141f612004-01-25 22:44:58 +00001261 /* Clean up and return
1262 */
drh17435752007-08-16 04:30:38 +00001263 sqlite3_free(zDb);
1264 sqlite3_free(zTab);
danielk19774adee202004-05-08 08:23:19 +00001265 sqlite3ExprDelete(pExpr->pLeft);
drh8141f612004-01-25 22:44:58 +00001266 pExpr->pLeft = 0;
danielk19774adee202004-05-08 08:23:19 +00001267 sqlite3ExprDelete(pExpr->pRight);
drh8141f612004-01-25 22:44:58 +00001268 pExpr->pRight = 0;
1269 pExpr->op = TK_COLUMN;
drh15ccce12005-05-23 15:06:39 +00001270lookupname_end_2:
drh17435752007-08-16 04:30:38 +00001271 sqlite3_free(zCol);
drh626a8792005-01-17 22:08:19 +00001272 if( cnt==1 ){
danielk1977b3bce662005-01-29 08:32:43 +00001273 assert( pNC!=0 );
drh728b5772007-09-18 15:55:07 +00001274 sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList);
danielk1977aee18ef2005-03-09 12:26:50 +00001275 if( pMatch && !pMatch->pSelect ){
1276 pExpr->pTab = pMatch->pTab;
1277 }
drh15ccce12005-05-23 15:06:39 +00001278 /* Increment the nRef value on all name contexts from TopNC up to
1279 ** the point where the name matched. */
1280 for(;;){
1281 assert( pTopNC!=0 );
1282 pTopNC->nRef++;
1283 if( pTopNC==pNC ) break;
1284 pTopNC = pTopNC->pNext;
1285 }
1286 return 0;
1287 } else {
1288 return 1;
drh626a8792005-01-17 22:08:19 +00001289 }
drh8141f612004-01-25 22:44:58 +00001290}
1291
1292/*
drh626a8792005-01-17 22:08:19 +00001293** This routine is designed as an xFunc for walkExprTree().
1294**
drh73b211a2005-01-18 04:00:42 +00001295** Resolve symbolic names into TK_COLUMN operators for the current
drh626a8792005-01-17 22:08:19 +00001296** node in the expression tree. Return 0 to continue the search down
drh73b211a2005-01-18 04:00:42 +00001297** the tree or 2 to abort the tree walk.
1298**
1299** This routine also does error checking and name resolution for
1300** function names. The operator for aggregate functions is changed
1301** to TK_AGG_FUNCTION.
drh626a8792005-01-17 22:08:19 +00001302*/
1303static int nameResolverStep(void *pArg, Expr *pExpr){
1304 NameContext *pNC = (NameContext*)pArg;
drh626a8792005-01-17 22:08:19 +00001305 Parse *pParse;
drh626a8792005-01-17 22:08:19 +00001306
danielk1977b3bce662005-01-29 08:32:43 +00001307 if( pExpr==0 ) return 1;
drh626a8792005-01-17 22:08:19 +00001308 assert( pNC!=0 );
drh626a8792005-01-17 22:08:19 +00001309 pParse = pNC->pParse;
danielk1977b3bce662005-01-29 08:32:43 +00001310
drh626a8792005-01-17 22:08:19 +00001311 if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return 1;
1312 ExprSetProperty(pExpr, EP_Resolved);
1313#ifndef NDEBUG
danielk1977f0113002006-01-24 12:09:17 +00001314 if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
1315 SrcList *pSrcList = pNC->pSrcList;
danielk1977940fac92005-01-23 22:41:37 +00001316 int i;
danielk1977f0113002006-01-24 12:09:17 +00001317 for(i=0; i<pNC->pSrcList->nSrc; i++){
drh626a8792005-01-17 22:08:19 +00001318 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
1319 }
1320 }
1321#endif
1322 switch( pExpr->op ){
1323 /* Double-quoted strings (ex: "abc") are used as identifiers if
1324 ** possible. Otherwise they remain as strings. Single-quoted
1325 ** strings (ex: 'abc') are always string literals.
1326 */
1327 case TK_STRING: {
1328 if( pExpr->token.z[0]=='\'' ) break;
1329 /* Fall thru into the TK_ID case if this is a double-quoted string */
1330 }
1331 /* A lone identifier is the name of a column.
1332 */
1333 case TK_ID: {
drh626a8792005-01-17 22:08:19 +00001334 lookupName(pParse, 0, 0, &pExpr->token, pNC, pExpr);
1335 return 1;
1336 }
1337
1338 /* A table name and column name: ID.ID
1339 ** Or a database, table and column: ID.ID.ID
1340 */
1341 case TK_DOT: {
1342 Token *pColumn;
1343 Token *pTable;
1344 Token *pDb;
1345 Expr *pRight;
1346
danielk1977b3bce662005-01-29 08:32:43 +00001347 /* if( pSrcList==0 ) break; */
drh626a8792005-01-17 22:08:19 +00001348 pRight = pExpr->pRight;
1349 if( pRight->op==TK_ID ){
1350 pDb = 0;
1351 pTable = &pExpr->pLeft->token;
1352 pColumn = &pRight->token;
1353 }else{
1354 assert( pRight->op==TK_DOT );
1355 pDb = &pExpr->pLeft->token;
1356 pTable = &pRight->pLeft->token;
1357 pColumn = &pRight->pRight->token;
1358 }
1359 lookupName(pParse, pDb, pTable, pColumn, pNC, pExpr);
1360 return 1;
1361 }
1362
1363 /* Resolve function names
1364 */
drhb71090f2005-05-23 17:26:51 +00001365 case TK_CONST_FUNC:
drh626a8792005-01-17 22:08:19 +00001366 case TK_FUNCTION: {
1367 ExprList *pList = pExpr->pList; /* The argument list */
1368 int n = pList ? pList->nExpr : 0; /* Number of arguments */
1369 int no_such_func = 0; /* True if no such function exists */
1370 int wrong_num_args = 0; /* True if wrong number of arguments */
1371 int is_agg = 0; /* True if is an aggregate function */
1372 int i;
drh5169bbc2006-08-24 14:59:45 +00001373 int auth; /* Authorization to use the function */
drh626a8792005-01-17 22:08:19 +00001374 int nId; /* Number of characters in function name */
1375 const char *zId; /* The function name. */
drh73b211a2005-01-18 04:00:42 +00001376 FuncDef *pDef; /* Information about the function */
danielk197714db2662006-01-09 16:12:04 +00001377 int enc = ENC(pParse->db); /* The database encoding */
drh626a8792005-01-17 22:08:19 +00001378
drh2646da72005-12-09 20:02:05 +00001379 zId = (char*)pExpr->token.z;
drhb71090f2005-05-23 17:26:51 +00001380 nId = pExpr->token.n;
drh626a8792005-01-17 22:08:19 +00001381 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
1382 if( pDef==0 ){
1383 pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
1384 if( pDef==0 ){
1385 no_such_func = 1;
1386 }else{
1387 wrong_num_args = 1;
1388 }
1389 }else{
1390 is_agg = pDef->xFunc==0;
1391 }
drh2fca7fe2006-11-23 11:59:13 +00001392#ifndef SQLITE_OMIT_AUTHORIZATION
drh5169bbc2006-08-24 14:59:45 +00001393 if( pDef ){
1394 auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0);
1395 if( auth!=SQLITE_OK ){
1396 if( auth==SQLITE_DENY ){
1397 sqlite3ErrorMsg(pParse, "not authorized to use function: %s",
1398 pDef->zName);
1399 pNC->nErr++;
1400 }
1401 pExpr->op = TK_NULL;
1402 return 1;
1403 }
1404 }
drhb8b14212006-08-24 15:18:25 +00001405#endif
drh626a8792005-01-17 22:08:19 +00001406 if( is_agg && !pNC->allowAgg ){
1407 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
1408 pNC->nErr++;
1409 is_agg = 0;
1410 }else if( no_such_func ){
1411 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
1412 pNC->nErr++;
1413 }else if( wrong_num_args ){
1414 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
1415 nId, zId);
1416 pNC->nErr++;
1417 }
1418 if( is_agg ){
1419 pExpr->op = TK_AGG_FUNCTION;
1420 pNC->hasAgg = 1;
1421 }
drh73b211a2005-01-18 04:00:42 +00001422 if( is_agg ) pNC->allowAgg = 0;
drh626a8792005-01-17 22:08:19 +00001423 for(i=0; pNC->nErr==0 && i<n; i++){
drh73b211a2005-01-18 04:00:42 +00001424 walkExprTree(pList->a[i].pExpr, nameResolverStep, pNC);
drh626a8792005-01-17 22:08:19 +00001425 }
drh73b211a2005-01-18 04:00:42 +00001426 if( is_agg ) pNC->allowAgg = 1;
drh626a8792005-01-17 22:08:19 +00001427 /* FIX ME: Compute pExpr->affinity based on the expected return
1428 ** type of the function
1429 */
1430 return is_agg;
1431 }
danielk1977b3bce662005-01-29 08:32:43 +00001432#ifndef SQLITE_OMIT_SUBQUERY
1433 case TK_SELECT:
1434 case TK_EXISTS:
1435#endif
1436 case TK_IN: {
1437 if( pExpr->pSelect ){
drh8a9f38f2005-11-05 15:07:55 +00001438 int nRef = pNC->nRef;
drh06f65412005-11-03 02:03:13 +00001439#ifndef SQLITE_OMIT_CHECK
1440 if( pNC->isCheck ){
1441 sqlite3ErrorMsg(pParse,"subqueries prohibited in CHECK constraints");
1442 }
1443#endif
danielk1977b3bce662005-01-29 08:32:43 +00001444 sqlite3SelectResolve(pParse, pExpr->pSelect, pNC);
1445 assert( pNC->nRef>=nRef );
1446 if( nRef!=pNC->nRef ){
1447 ExprSetProperty(pExpr, EP_VarSelect);
1448 }
1449 }
drh4284fb02005-11-03 12:33:28 +00001450 break;
danielk1977b3bce662005-01-29 08:32:43 +00001451 }
drh4284fb02005-11-03 12:33:28 +00001452#ifndef SQLITE_OMIT_CHECK
1453 case TK_VARIABLE: {
1454 if( pNC->isCheck ){
1455 sqlite3ErrorMsg(pParse,"parameters prohibited in CHECK constraints");
1456 }
1457 break;
1458 }
1459#endif
drh626a8792005-01-17 22:08:19 +00001460 }
1461 return 0;
1462}
1463
1464/*
drhcce7d172000-05-31 15:34:51 +00001465** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +00001466** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +00001467** index to the table in the table list and a column offset. The
1468** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
1469** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +00001470** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +00001471** VDBE cursor number for a cursor that is pointing into the referenced
1472** table. The Expr.iColumn value is changed to the index of the column
1473** of the referenced table. The Expr.iColumn value for the special
1474** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
1475** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +00001476**
drh626a8792005-01-17 22:08:19 +00001477** Also resolve function names and check the functions for proper
1478** usage. Make sure all function names are recognized and all functions
1479** have the correct number of arguments. Leave an error message
1480** in pParse->zErrMsg if anything is amiss. Return the number of errors.
1481**
drh73b211a2005-01-18 04:00:42 +00001482** If the expression contains aggregate functions then set the EP_Agg
1483** property on the expression.
drh626a8792005-01-17 22:08:19 +00001484*/
danielk1977fc976062007-05-10 10:46:56 +00001485int sqlite3ExprResolveNames(
danielk1977b3bce662005-01-29 08:32:43 +00001486 NameContext *pNC, /* Namespace to resolve expressions in. */
1487 Expr *pExpr /* The expression to be analyzed. */
drh626a8792005-01-17 22:08:19 +00001488){
drh13449892005-09-07 21:22:45 +00001489 int savedHasAgg;
drhbb4957f2008-03-20 14:03:29 +00001490
drh73b211a2005-01-18 04:00:42 +00001491 if( pExpr==0 ) return 0;
drhbb4957f2008-03-20 14:03:29 +00001492#if SQLITE_MAX_EXPR_DEPTH>0
1493 {
1494 int mxDepth = pNC->pParse->db->aLimit[SQLITE_LIMIT_EXPR_DEPTH];
1495 if( (pExpr->nHeight+pNC->pParse->nHeight)>mxDepth ){
1496 sqlite3ErrorMsg(pNC->pParse,
1497 "Expression tree is too large (maximum depth %d)", mxDepth
1498 );
1499 return 1;
1500 }
1501 pNC->pParse->nHeight += pExpr->nHeight;
danielk1977fc976062007-05-10 10:46:56 +00001502 }
danielk1977fc976062007-05-10 10:46:56 +00001503#endif
drh13449892005-09-07 21:22:45 +00001504 savedHasAgg = pNC->hasAgg;
1505 pNC->hasAgg = 0;
danielk1977b3bce662005-01-29 08:32:43 +00001506 walkExprTree(pExpr, nameResolverStep, pNC);
drhbb4957f2008-03-20 14:03:29 +00001507#if SQLITE_MAX_EXPR_DEPTH>0
danielk1977fc976062007-05-10 10:46:56 +00001508 pNC->pParse->nHeight -= pExpr->nHeight;
1509#endif
danielk1977b3bce662005-01-29 08:32:43 +00001510 if( pNC->nErr>0 ){
drh73b211a2005-01-18 04:00:42 +00001511 ExprSetProperty(pExpr, EP_Error);
drh73b211a2005-01-18 04:00:42 +00001512 }
drh13449892005-09-07 21:22:45 +00001513 if( pNC->hasAgg ){
1514 ExprSetProperty(pExpr, EP_Agg);
1515 }else if( savedHasAgg ){
1516 pNC->hasAgg = 1;
1517 }
drh73b211a2005-01-18 04:00:42 +00001518 return ExprHasProperty(pExpr, EP_Error);
drh626a8792005-01-17 22:08:19 +00001519}
1520
drh1398ad32005-01-19 23:24:50 +00001521/*
1522** A pointer instance of this structure is used to pass information
1523** through walkExprTree into codeSubqueryStep().
1524*/
1525typedef struct QueryCoder QueryCoder;
1526struct QueryCoder {
1527 Parse *pParse; /* The parsing context */
1528 NameContext *pNC; /* Namespace of first enclosing query */
1529};
1530
danielk19779a96b662007-11-29 17:05:18 +00001531#ifdef SQLITE_TEST
1532 int sqlite3_enable_in_opt = 1;
1533#else
1534 #define sqlite3_enable_in_opt 1
1535#endif
1536
1537/*
1538** This function is used by the implementation of the IN (...) operator.
1539** It's job is to find or create a b-tree structure that may be used
1540** either to test for membership of the (...) set or to iterate through
drh85b623f2007-12-13 21:54:09 +00001541** its members, skipping duplicates.
danielk19779a96b662007-11-29 17:05:18 +00001542**
1543** The cursor opened on the structure (database table, database index
1544** or ephermal table) is stored in pX->iTable before this function returns.
1545** The returned value indicates the structure type, as follows:
1546**
1547** IN_INDEX_ROWID - The cursor was opened on a database table.
drh2d401ab2008-01-10 23:50:11 +00001548** IN_INDEX_INDEX - The cursor was opened on a database index.
danielk19779a96b662007-11-29 17:05:18 +00001549** IN_INDEX_EPH - The cursor was opened on a specially created and
1550** populated epheremal table.
1551**
1552** An existing structure may only be used if the SELECT is of the simple
1553** form:
1554**
1555** SELECT <column> FROM <table>
1556**
1557** If the mustBeUnique parameter is false, the structure will be used
1558** for fast set membership tests. In this case an epheremal table must
1559** be used unless <column> is an INTEGER PRIMARY KEY or an index can
drh85b623f2007-12-13 21:54:09 +00001560** be found with <column> as its left-most column.
danielk19779a96b662007-11-29 17:05:18 +00001561**
1562** If mustBeUnique is true, then the structure will be used to iterate
1563** through the set members, skipping any duplicates. In this case an
1564** epheremal table must be used unless the selected <column> is guaranteed
1565** to be unique - either because it is an INTEGER PRIMARY KEY or it
1566** is unique by virtue of a constraint or implicit index.
1567*/
danielk1977284f4ac2007-12-10 05:03:46 +00001568#ifndef SQLITE_OMIT_SUBQUERY
danielk19779a96b662007-11-29 17:05:18 +00001569int sqlite3FindInIndex(Parse *pParse, Expr *pX, int mustBeUnique){
1570 Select *p;
1571 int eType = 0;
1572 int iTab = pParse->nTab++;
1573
1574 /* The follwing if(...) expression is true if the SELECT is of the
1575 ** simple form:
1576 **
1577 ** SELECT <column> FROM <table>
1578 **
1579 ** If this is the case, it may be possible to use an existing table
1580 ** or index instead of generating an epheremal table.
1581 */
1582 if( sqlite3_enable_in_opt
drhc81945e2008-03-10 14:12:53 +00001583 && (p=pX->pSelect)!=0 && !p->pPrior
danielk19779a96b662007-11-29 17:05:18 +00001584 && !p->isDistinct && !p->isAgg && !p->pGroupBy
1585 && p->pSrc && p->pSrc->nSrc==1 && !p->pSrc->a[0].pSelect
danielk1977b2b95d42008-03-12 10:39:00 +00001586 && p->pSrc->a[0].pTab && !p->pSrc->a[0].pTab->pSelect
danielk19779a96b662007-11-29 17:05:18 +00001587 && p->pEList->nExpr==1 && p->pEList->a[0].pExpr->op==TK_COLUMN
1588 && !p->pLimit && !p->pOffset && !p->pWhere
1589 ){
1590 sqlite3 *db = pParse->db;
1591 Index *pIdx;
1592 Expr *pExpr = p->pEList->a[0].pExpr;
1593 int iCol = pExpr->iColumn;
1594 Vdbe *v = sqlite3GetVdbe(pParse);
1595
1596 /* This function is only called from two places. In both cases the vdbe
1597 ** has already been allocated. So assume sqlite3GetVdbe() is always
1598 ** successful here.
1599 */
1600 assert(v);
1601 if( iCol<0 ){
drh0a07c102008-01-03 18:03:08 +00001602 int iMem = ++pParse->nMem;
danielk19779a96b662007-11-29 17:05:18 +00001603 int iAddr;
1604 Table *pTab = p->pSrc->a[0].pTab;
1605 int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
1606 sqlite3VdbeUsesBtree(v, iDb);
1607
drh892d3172008-01-10 03:46:36 +00001608 iAddr = sqlite3VdbeAddOp1(v, OP_If, iMem);
drh4c583122008-01-04 22:01:03 +00001609 sqlite3VdbeAddOp2(v, OP_Integer, 1, iMem);
danielk19779a96b662007-11-29 17:05:18 +00001610
1611 sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
1612 eType = IN_INDEX_ROWID;
1613
1614 sqlite3VdbeJumpHere(v, iAddr);
1615 }else{
1616 /* The collation sequence used by the comparison. If an index is to
1617 ** be used in place of a temp-table, it must be ordered according
1618 ** to this collation sequence.
1619 */
1620 CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pExpr);
1621
1622 /* Check that the affinity that will be used to perform the
1623 ** comparison is the same as the affinity of the column. If
1624 ** it is not, it is not possible to use any index.
1625 */
1626 Table *pTab = p->pSrc->a[0].pTab;
1627 char aff = comparisonAffinity(pX);
1628 int affinity_ok = (pTab->aCol[iCol].affinity==aff||aff==SQLITE_AFF_NONE);
1629
1630 for(pIdx=pTab->pIndex; pIdx && eType==0 && affinity_ok; pIdx=pIdx->pNext){
1631 if( (pIdx->aiColumn[0]==iCol)
1632 && (pReq==sqlite3FindCollSeq(db, ENC(db), pIdx->azColl[0], -1, 0))
1633 && (!mustBeUnique || (pIdx->nColumn==1 && pIdx->onError!=OE_None))
1634 ){
1635 int iDb;
drh0a07c102008-01-03 18:03:08 +00001636 int iMem = ++pParse->nMem;
danielk19779a96b662007-11-29 17:05:18 +00001637 int iAddr;
1638 char *pKey;
1639
1640 pKey = (char *)sqlite3IndexKeyinfo(pParse, pIdx);
1641 iDb = sqlite3SchemaToIndex(db, pIdx->pSchema);
1642 sqlite3VdbeUsesBtree(v, iDb);
1643
drh892d3172008-01-10 03:46:36 +00001644 iAddr = sqlite3VdbeAddOp1(v, OP_If, iMem);
drh4c583122008-01-04 22:01:03 +00001645 sqlite3VdbeAddOp2(v, OP_Integer, 1, iMem);
danielk19779a96b662007-11-29 17:05:18 +00001646
danielk1977cd3e8f72008-03-25 09:47:35 +00001647 sqlite3VdbeAddOp2(v, OP_SetNumColumns, 0, pIdx->nColumn);
danielk1977207872a2008-01-03 07:54:23 +00001648 sqlite3VdbeAddOp4(v, OP_OpenRead, iTab, pIdx->tnum, iDb,
drh66a51672008-01-03 00:01:23 +00001649 pKey,P4_KEYINFO_HANDOFF);
danielk1977207872a2008-01-03 07:54:23 +00001650 VdbeComment((v, "%s", pIdx->zName));
danielk19779a96b662007-11-29 17:05:18 +00001651 eType = IN_INDEX_INDEX;
danielk19779a96b662007-11-29 17:05:18 +00001652
1653 sqlite3VdbeJumpHere(v, iAddr);
1654 }
1655 }
1656 }
1657 }
1658
1659 if( eType==0 ){
1660 sqlite3CodeSubselect(pParse, pX);
1661 eType = IN_INDEX_EPH;
1662 }else{
1663 pX->iTable = iTab;
1664 }
1665 return eType;
1666}
danielk1977284f4ac2007-12-10 05:03:46 +00001667#endif
drh626a8792005-01-17 22:08:19 +00001668
1669/*
drh9cbe6352005-11-29 03:13:21 +00001670** Generate code for scalar subqueries used as an expression
1671** and IN operators. Examples:
drh626a8792005-01-17 22:08:19 +00001672**
drh9cbe6352005-11-29 03:13:21 +00001673** (SELECT a FROM b) -- subquery
1674** EXISTS (SELECT a FROM b) -- EXISTS subquery
1675** x IN (4,5,11) -- IN operator with list on right-hand side
1676** x IN (SELECT a FROM b) -- IN operator with subquery on the right
drhfef52082000-06-06 01:50:43 +00001677**
drh9cbe6352005-11-29 03:13:21 +00001678** The pExpr parameter describes the expression that contains the IN
1679** operator or subquery.
drhcce7d172000-05-31 15:34:51 +00001680*/
drh51522cd2005-01-20 13:36:19 +00001681#ifndef SQLITE_OMIT_SUBQUERY
danielk1977b3bce662005-01-29 08:32:43 +00001682void sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){
drh57dbd7b2005-07-08 18:25:26 +00001683 int testAddr = 0; /* One-time test address */
danielk1977b3bce662005-01-29 08:32:43 +00001684 Vdbe *v = sqlite3GetVdbe(pParse);
1685 if( v==0 ) return;
1686
danielk1977fc976062007-05-10 10:46:56 +00001687
drh57dbd7b2005-07-08 18:25:26 +00001688 /* This code must be run in its entirety every time it is encountered
1689 ** if any of the following is true:
1690 **
1691 ** * The right-hand side is a correlated subquery
1692 ** * The right-hand side is an expression list containing variables
1693 ** * We are inside a trigger
1694 **
1695 ** If all of the above are false, then we can run this code just once
1696 ** save the results, and reuse the same result on subsequent invocations.
danielk1977b3bce662005-01-29 08:32:43 +00001697 */
1698 if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->trigStack ){
drh0a07c102008-01-03 18:03:08 +00001699 int mem = ++pParse->nMem;
drh892d3172008-01-10 03:46:36 +00001700 sqlite3VdbeAddOp1(v, OP_If, mem);
1701 testAddr = sqlite3VdbeAddOp2(v, OP_Integer, 1, mem);
drh17435752007-08-16 04:30:38 +00001702 assert( testAddr>0 || pParse->db->mallocFailed );
danielk1977b3bce662005-01-29 08:32:43 +00001703 }
1704
drhcce7d172000-05-31 15:34:51 +00001705 switch( pExpr->op ){
drhfef52082000-06-06 01:50:43 +00001706 case TK_IN: {
danielk1977e014a832004-05-17 10:48:57 +00001707 char affinity;
drhd3d39e92004-05-20 22:16:29 +00001708 KeyInfo keyInfo;
drhb9bb7c12006-06-11 23:41:55 +00001709 int addr; /* Address of OP_OpenEphemeral instruction */
drhd3d39e92004-05-20 22:16:29 +00001710
danielk1977bf3b7212004-05-18 10:06:24 +00001711 affinity = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +00001712
1713 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
drh57dbd7b2005-07-08 18:25:26 +00001714 ** expression it is handled the same way. A virtual table is
danielk1977e014a832004-05-17 10:48:57 +00001715 ** filled with single-field index keys representing the results
1716 ** from the SELECT or the <exprlist>.
1717 **
1718 ** If the 'x' expression is a column value, or the SELECT...
1719 ** statement returns a column value, then the affinity of that
1720 ** column is used to build the index keys. If both 'x' and the
1721 ** SELECT... statement are columns, then numeric affinity is used
1722 ** if either column has NUMERIC or INTEGER affinity. If neither
1723 ** 'x' nor the SELECT... statement are columns, then numeric affinity
1724 ** is used.
1725 */
1726 pExpr->iTable = pParse->nTab++;
danielk1977cd3e8f72008-03-25 09:47:35 +00001727 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pExpr->iTable, 1);
drhd3d39e92004-05-20 22:16:29 +00001728 memset(&keyInfo, 0, sizeof(keyInfo));
1729 keyInfo.nField = 1;
danielk1977e014a832004-05-17 10:48:57 +00001730
drhfef52082000-06-06 01:50:43 +00001731 if( pExpr->pSelect ){
1732 /* Case 1: expr IN (SELECT ...)
1733 **
danielk1977e014a832004-05-17 10:48:57 +00001734 ** Generate code to write the results of the select into the temporary
1735 ** table allocated and opened above.
drhfef52082000-06-06 01:50:43 +00001736 */
drh1013c932008-01-06 00:25:21 +00001737 SelectDest dest;
drhbe5c89a2004-07-26 00:31:09 +00001738 ExprList *pEList;
drh1013c932008-01-06 00:25:21 +00001739
1740 sqlite3SelectDestInit(&dest, SRT_Set, pExpr->iTable);
1741 dest.affinity = (int)affinity;
danielk1977e014a832004-05-17 10:48:57 +00001742 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
danielk19776c8c8ce2008-01-02 16:27:09 +00001743 if( sqlite3Select(pParse, pExpr->pSelect, &dest, 0, 0, 0, 0) ){
drh94ccde52007-04-13 16:06:32 +00001744 return;
1745 }
drhbe5c89a2004-07-26 00:31:09 +00001746 pEList = pExpr->pSelect->pEList;
1747 if( pEList && pEList->nExpr>0 ){
danielk1977bcbb04e2007-05-29 12:11:29 +00001748 keyInfo.aColl[0] = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft,
drhbe5c89a2004-07-26 00:31:09 +00001749 pEList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001750 }
drhfef52082000-06-06 01:50:43 +00001751 }else if( pExpr->pList ){
1752 /* Case 2: expr IN (exprlist)
1753 **
drhfd131da2007-08-07 17:13:03 +00001754 ** For each expression, build an index key from the evaluation and
danielk1977e014a832004-05-17 10:48:57 +00001755 ** store it in the temporary table. If <expr> is a column, then use
1756 ** that columns affinity when building index keys. If <expr> is not
1757 ** a column, use numeric affinity.
drhfef52082000-06-06 01:50:43 +00001758 */
danielk1977e014a832004-05-17 10:48:57 +00001759 int i;
drh57dbd7b2005-07-08 18:25:26 +00001760 ExprList *pList = pExpr->pList;
1761 struct ExprList_item *pItem;
drh2d401ab2008-01-10 23:50:11 +00001762 int r1, r2;
drh57dbd7b2005-07-08 18:25:26 +00001763
danielk1977e014a832004-05-17 10:48:57 +00001764 if( !affinity ){
drh8159a352006-05-23 23:22:29 +00001765 affinity = SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +00001766 }
danielk19770202b292004-06-09 09:55:16 +00001767 keyInfo.aColl[0] = pExpr->pLeft->pColl;
danielk1977e014a832004-05-17 10:48:57 +00001768
1769 /* Loop through each expression in <exprlist>. */
drh2d401ab2008-01-10 23:50:11 +00001770 r1 = sqlite3GetTempReg(pParse);
1771 r2 = sqlite3GetTempReg(pParse);
drh57dbd7b2005-07-08 18:25:26 +00001772 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
1773 Expr *pE2 = pItem->pExpr;
danielk1977e014a832004-05-17 10:48:57 +00001774
drh57dbd7b2005-07-08 18:25:26 +00001775 /* If the expression is not constant then we will need to
1776 ** disable the test that was generated above that makes sure
1777 ** this code only executes once. Because for a non-constant
1778 ** expression we need to rerun this code each time.
1779 */
drh892d3172008-01-10 03:46:36 +00001780 if( testAddr && !sqlite3ExprIsConstant(pE2) ){
1781 sqlite3VdbeChangeToNoop(v, testAddr-1, 2);
drh57dbd7b2005-07-08 18:25:26 +00001782 testAddr = 0;
drh4794b982000-06-06 13:54:14 +00001783 }
danielk1977e014a832004-05-17 10:48:57 +00001784
1785 /* Evaluate the expression and insert it into the temp table */
drhe55cbd72008-03-31 23:48:03 +00001786 pParse->disableColCache++;
drh2d401ab2008-01-10 23:50:11 +00001787 sqlite3ExprCode(pParse, pE2, r1);
drhe55cbd72008-03-31 23:48:03 +00001788 pParse->disableColCache--;
drh1db639c2008-01-17 02:36:28 +00001789 sqlite3VdbeAddOp4(v, OP_MakeRecord, r1, 1, r2, &affinity, 1);
drhe55cbd72008-03-31 23:48:03 +00001790 sqlite3ExprExpireColumnCacheLines(pParse, r1, r1);
drh2d401ab2008-01-10 23:50:11 +00001791 sqlite3VdbeAddOp2(v, OP_IdxInsert, pExpr->iTable, r2);
drhfef52082000-06-06 01:50:43 +00001792 }
drh2d401ab2008-01-10 23:50:11 +00001793 sqlite3ReleaseTempReg(pParse, r1);
1794 sqlite3ReleaseTempReg(pParse, r2);
drhfef52082000-06-06 01:50:43 +00001795 }
drh66a51672008-01-03 00:01:23 +00001796 sqlite3VdbeChangeP4(v, addr, (void *)&keyInfo, P4_KEYINFO);
danielk1977b3bce662005-01-29 08:32:43 +00001797 break;
drhfef52082000-06-06 01:50:43 +00001798 }
1799
drh51522cd2005-01-20 13:36:19 +00001800 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001801 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +00001802 /* This has to be a scalar SELECT. Generate code to put the
1803 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +00001804 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +00001805 */
drh2646da72005-12-09 20:02:05 +00001806 static const Token one = { (u8*)"1", 0, 1 };
drh51522cd2005-01-20 13:36:19 +00001807 Select *pSel;
danielk19776c8c8ce2008-01-02 16:27:09 +00001808 SelectDest dest;
drh1398ad32005-01-19 23:24:50 +00001809
drh51522cd2005-01-20 13:36:19 +00001810 pSel = pExpr->pSelect;
drh1013c932008-01-06 00:25:21 +00001811 sqlite3SelectDestInit(&dest, 0, ++pParse->nMem);
drh51522cd2005-01-20 13:36:19 +00001812 if( pExpr->op==TK_SELECT ){
danielk19776c8c8ce2008-01-02 16:27:09 +00001813 dest.eDest = SRT_Mem;
drh4c583122008-01-04 22:01:03 +00001814 sqlite3VdbeAddOp2(v, OP_Null, 0, dest.iParm);
drhd4e70eb2008-01-02 00:34:36 +00001815 VdbeComment((v, "Init subquery result"));
drh51522cd2005-01-20 13:36:19 +00001816 }else{
danielk19776c8c8ce2008-01-02 16:27:09 +00001817 dest.eDest = SRT_Exists;
drh4c583122008-01-04 22:01:03 +00001818 sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iParm);
drhd4e70eb2008-01-02 00:34:36 +00001819 VdbeComment((v, "Init EXISTS result"));
drh51522cd2005-01-20 13:36:19 +00001820 }
drhec7429a2005-10-06 16:53:14 +00001821 sqlite3ExprDelete(pSel->pLimit);
danielk1977a1644fd2007-08-29 12:31:25 +00001822 pSel->pLimit = sqlite3PExpr(pParse, TK_INTEGER, 0, 0, &one);
danielk19776c8c8ce2008-01-02 16:27:09 +00001823 if( sqlite3Select(pParse, pSel, &dest, 0, 0, 0, 0) ){
drh94ccde52007-04-13 16:06:32 +00001824 return;
1825 }
danielk19776c8c8ce2008-01-02 16:27:09 +00001826 pExpr->iColumn = dest.iParm;
danielk1977b3bce662005-01-29 08:32:43 +00001827 break;
drhcce7d172000-05-31 15:34:51 +00001828 }
1829 }
danielk1977b3bce662005-01-29 08:32:43 +00001830
drh57dbd7b2005-07-08 18:25:26 +00001831 if( testAddr ){
drh892d3172008-01-10 03:46:36 +00001832 sqlite3VdbeJumpHere(v, testAddr-1);
danielk1977b3bce662005-01-29 08:32:43 +00001833 }
danielk1977fc976062007-05-10 10:46:56 +00001834
danielk1977b3bce662005-01-29 08:32:43 +00001835 return;
drhcce7d172000-05-31 15:34:51 +00001836}
drh51522cd2005-01-20 13:36:19 +00001837#endif /* SQLITE_OMIT_SUBQUERY */
drhcce7d172000-05-31 15:34:51 +00001838
drhcce7d172000-05-31 15:34:51 +00001839/*
drh598f1342007-10-23 15:39:45 +00001840** Duplicate an 8-byte value
1841*/
1842static char *dup8bytes(Vdbe *v, const char *in){
1843 char *out = sqlite3DbMallocRaw(sqlite3VdbeDb(v), 8);
1844 if( out ){
1845 memcpy(out, in, 8);
1846 }
1847 return out;
1848}
1849
1850/*
1851** Generate an instruction that will put the floating point
drh9cbf3422008-01-17 16:22:13 +00001852** value described by z[0..n-1] into register iMem.
drh0cf19ed2007-10-23 18:55:48 +00001853**
1854** The z[] string will probably not be zero-terminated. But the
1855** z[n] character is guaranteed to be something that does not look
1856** like the continuation of the number.
drh598f1342007-10-23 15:39:45 +00001857*/
drh9de221d2008-01-05 06:51:30 +00001858static void codeReal(Vdbe *v, const char *z, int n, int negateFlag, int iMem){
drh598f1342007-10-23 15:39:45 +00001859 assert( z || v==0 || sqlite3VdbeDb(v)->mallocFailed );
1860 if( z ){
1861 double value;
1862 char *zV;
drh0cf19ed2007-10-23 18:55:48 +00001863 assert( !isdigit(z[n]) );
drh598f1342007-10-23 15:39:45 +00001864 sqlite3AtoF(z, &value);
1865 if( negateFlag ) value = -value;
1866 zV = dup8bytes(v, (char*)&value);
drh9de221d2008-01-05 06:51:30 +00001867 sqlite3VdbeAddOp4(v, OP_Real, 0, iMem, 0, zV, P4_REAL);
drh598f1342007-10-23 15:39:45 +00001868 }
1869}
1870
1871
1872/*
drhfec19aa2004-05-19 20:41:03 +00001873** Generate an instruction that will put the integer describe by
drh9cbf3422008-01-17 16:22:13 +00001874** text z[0..n-1] into register iMem.
drh0cf19ed2007-10-23 18:55:48 +00001875**
1876** The z[] string will probably not be zero-terminated. But the
1877** z[n] character is guaranteed to be something that does not look
1878** like the continuation of the number.
drhfec19aa2004-05-19 20:41:03 +00001879*/
drh9de221d2008-01-05 06:51:30 +00001880static void codeInteger(Vdbe *v, const char *z, int n, int negFlag, int iMem){
drhabb6fca2007-08-16 12:24:01 +00001881 assert( z || v==0 || sqlite3VdbeDb(v)->mallocFailed );
danielk1977c9cf9012007-05-30 10:36:47 +00001882 if( z ){
1883 int i;
drh0cf19ed2007-10-23 18:55:48 +00001884 assert( !isdigit(z[n]) );
danielk1977c9cf9012007-05-30 10:36:47 +00001885 if( sqlite3GetInt32(z, &i) ){
drh9de221d2008-01-05 06:51:30 +00001886 if( negFlag ) i = -i;
1887 sqlite3VdbeAddOp2(v, OP_Integer, i, iMem);
1888 }else if( sqlite3FitsIn64Bits(z, negFlag) ){
drh598f1342007-10-23 15:39:45 +00001889 i64 value;
1890 char *zV;
1891 sqlite3Atoi64(z, &value);
drh9de221d2008-01-05 06:51:30 +00001892 if( negFlag ) value = -value;
drh598f1342007-10-23 15:39:45 +00001893 zV = dup8bytes(v, (char*)&value);
drh9de221d2008-01-05 06:51:30 +00001894 sqlite3VdbeAddOp4(v, OP_Int64, 0, iMem, 0, zV, P4_INT64);
danielk1977c9cf9012007-05-30 10:36:47 +00001895 }else{
drh9de221d2008-01-05 06:51:30 +00001896 codeReal(v, z, n, negFlag, iMem);
danielk1977c9cf9012007-05-30 10:36:47 +00001897 }
drhfec19aa2004-05-19 20:41:03 +00001898 }
1899}
1900
drh945498f2007-02-24 11:52:52 +00001901
1902/*
1903** Generate code that will extract the iColumn-th column from
drhe55cbd72008-03-31 23:48:03 +00001904** table pTab and store the column value in a register. An effort
1905** is made to store the column value in register iReg, but this is
1906** not guaranteed. The location of the column value is returned.
1907**
1908** There must be an open cursor to pTab in iTable when this routine
1909** is called. If iColumn<0 then code is generated that extracts the rowid.
drh945498f2007-02-24 11:52:52 +00001910*/
drhe55cbd72008-03-31 23:48:03 +00001911int sqlite3ExprCodeGetColumn(
1912 Parse *pParse, /* Parsing and code generating context */
drh2133d822008-01-03 18:44:59 +00001913 Table *pTab, /* Description of the table we are reading from */
1914 int iColumn, /* Index of the table column */
1915 int iTable, /* The cursor pointing to the table */
1916 int iReg /* Store results here */
1917){
drhe55cbd72008-03-31 23:48:03 +00001918 Vdbe *v = pParse->pVdbe;
1919 int i;
1920
1921 for(i=0; i<pParse->nColCache; i++){
1922 if( pParse->aColCache[i].iTable==iTable
1923 && pParse->aColCache[i].iColumn==iColumn ){
1924#if 0
1925 sqlite3VdbeAddOp0(v, OP_Noop);
1926 VdbeComment((v, "OPT: tab%d.col%d -> r%d",
1927 iTable, iColumn, pParse->aColCache[i].iReg));
1928#endif
1929 return pParse->aColCache[i].iReg;
1930 }
1931 }
1932 assert( v!=0 );
drh945498f2007-02-24 11:52:52 +00001933 if( iColumn<0 ){
1934 int op = (pTab && IsVirtual(pTab)) ? OP_VRowid : OP_Rowid;
drh2133d822008-01-03 18:44:59 +00001935 sqlite3VdbeAddOp2(v, op, iTable, iReg);
drh945498f2007-02-24 11:52:52 +00001936 }else if( pTab==0 ){
drh2133d822008-01-03 18:44:59 +00001937 sqlite3VdbeAddOp3(v, OP_Column, iTable, iColumn, iReg);
drh945498f2007-02-24 11:52:52 +00001938 }else{
1939 int op = IsVirtual(pTab) ? OP_VColumn : OP_Column;
drh2133d822008-01-03 18:44:59 +00001940 sqlite3VdbeAddOp3(v, op, iTable, iColumn, iReg);
drh945498f2007-02-24 11:52:52 +00001941 sqlite3ColumnDefault(v, pTab, iColumn);
1942#ifndef SQLITE_OMIT_FLOATING_POINT
1943 if( pTab->aCol[iColumn].affinity==SQLITE_AFF_REAL ){
drh2133d822008-01-03 18:44:59 +00001944 sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg);
drh945498f2007-02-24 11:52:52 +00001945 }
1946#endif
1947 }
drhe55cbd72008-03-31 23:48:03 +00001948 if( pParse->disableColCache==0 ){
1949 i = pParse->iColCache;
1950 pParse->aColCache[i].iTable = iTable;
1951 pParse->aColCache[i].iColumn = iColumn;
1952 pParse->aColCache[i].iReg = iReg;
1953 i++;
drh2f7794c2008-04-01 03:27:39 +00001954 if( i>=ArraySize(pParse->aColCache) ) i = 0;
drhe55cbd72008-03-31 23:48:03 +00001955 if( i>pParse->nColCache ) pParse->nColCache = i;
drh2f7794c2008-04-01 03:27:39 +00001956 pParse->iColCache = i;
drhe55cbd72008-03-31 23:48:03 +00001957 }
1958 return iReg;
1959}
1960
1961/*
1962** Disable (+1) or enable (-1) the adding of new column cache entries.
1963*/
1964void sqlite3ExprColumnCacheDisable(Parse *pParse, int disable){
1965 assert( disable==-1 || disable==+1 );
1966 assert( pParse->disableColCache>0 || disable==1 );
1967 pParse->disableColCache += disable;
1968}
1969
1970/*
1971** Clear all column cache entries associated with the vdbe
1972** cursor with cursor number iTable.
1973*/
1974void sqlite3ExprClearColumnCache(Parse *pParse, int iTable){
1975 if( iTable<0 ){
1976 pParse->nColCache = 0;
1977 pParse->iColCache = 0;
1978 }else{
1979 int i;
1980 for(i=0; i<pParse->nColCache; i++){
1981 if( pParse->aColCache[i].iTable==iTable ){
1982 pParse->aColCache[i] = pParse->aColCache[--pParse->nColCache];
1983 }
1984 }
1985 pParse->iColCache = pParse->nColCache;
1986 }
1987}
1988
1989/*
1990** Expire all column cache entry associated with register between
1991** iFrom and iTo, inclusive. If there are no column cache entries
1992** on those registers then this routine is a no-op.
1993**
1994** Call this routine when register contents are overwritten to
1995** make sure the new register value is not used in place of the
1996** value that was overwritten.
1997*/
1998void sqlite3ExprExpireColumnCacheLines(Parse *pParse, int iFrom, int iTo){
1999 int i;
2000 for(i=0; i<pParse->nColCache; i++){
2001 int r = pParse->aColCache[i].iReg;
2002 if( r>=iFrom && r<=iTo ){
2003 pParse->aColCache[i] = pParse->aColCache[--pParse->nColCache];
2004 }
2005 }
2006 pParse->iColCache = pParse->nColCache;
2007}
2008
2009/*
2010** Generate code to moves content from one register to another.
2011** Keep the column cache up-to-date.
2012*/
2013void sqlite3ExprCodeMove(Parse *pParse, int iFrom, int iTo){
2014 int i;
2015 if( iFrom==iTo ) return;
2016 sqlite3VdbeAddOp2(pParse->pVdbe, OP_Move, iFrom, iTo);
2017 for(i=0; i<pParse->nColCache; i++){
2018 if( pParse->aColCache[i].iReg==iFrom ){
2019 pParse->aColCache[i].iReg = iTo;
2020 }
2021 }
drh945498f2007-02-24 11:52:52 +00002022}
2023
drhfec19aa2004-05-19 20:41:03 +00002024/*
drh652fbf52008-04-01 01:42:41 +00002025** Return true if any register in the range iFrom..iTo (inclusive)
2026** is used as part of the column cache.
2027*/
2028static int usedAsColumnCache(Parse *pParse, int iFrom, int iTo){
2029 int i;
2030 for(i=0; i<pParse->nColCache; i++){
2031 int r = pParse->aColCache[i].iReg;
2032 if( r>=iFrom && r<=iTo ) return 1;
2033 }
2034 return 0;
2035}
2036
2037/*
2038** Theres is a value in register iCurrent. We ultimately want
2039** the value to be in register iTarget. It might be that
2040** iCurrent and iTarget are the same register.
2041**
2042** We are going to modify the value, so we need to make sure it
2043** is not a cached register. If iCurrent is a cached register,
2044** then try to move the value over to iTarget. If iTarget is a
2045** cached register, then clear the corresponding cache line.
2046**
2047** Return the register that the value ends up in.
2048*/
2049int sqlite3ExprWritableRegister(Parse *pParse, int iCurrent, int iTarget){
2050 assert( pParse->pVdbe!=0 );
2051 if( !usedAsColumnCache(pParse, iCurrent, iCurrent) ){
2052 return iCurrent;
2053 }
drh2f7794c2008-04-01 03:27:39 +00002054 if( iCurrent!=iTarget ){
2055 sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, iCurrent, iTarget);
2056 }
drh652fbf52008-04-01 01:42:41 +00002057 sqlite3ExprExpireColumnCacheLines(pParse, iTarget, iTarget);
2058 return iTarget;
2059}
2060
2061/*
drhcce7d172000-05-31 15:34:51 +00002062** Generate code into the current Vdbe to evaluate the given
drh2dcef112008-01-12 19:03:48 +00002063** expression. Attempt to store the results in register "target".
2064** Return the register where results are stored.
drh389a1ad2008-01-03 23:44:53 +00002065**
drh2dcef112008-01-12 19:03:48 +00002066** With this routine, there is no guaranteed that results will
2067** be stored in target. The result might be stored in some other
2068** register if it is convenient to do so. The calling function
2069** must check the return code and move the results to the desired
2070** register.
drhcce7d172000-05-31 15:34:51 +00002071*/
drh678ccce2008-03-31 18:19:54 +00002072int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){
drh2dcef112008-01-12 19:03:48 +00002073 Vdbe *v = pParse->pVdbe; /* The VM under construction */
2074 int op; /* The opcode being coded */
2075 int inReg = target; /* Results stored in register inReg */
2076 int regFree1 = 0; /* If non-zero free this temporary register */
2077 int regFree2 = 0; /* If non-zero free this temporary register */
drh678ccce2008-03-31 18:19:54 +00002078 int r1, r2, r3, r4; /* Various register numbers */
drhffe07b22005-11-03 00:41:17 +00002079
drh389a1ad2008-01-03 23:44:53 +00002080 assert( v!=0 || pParse->db->mallocFailed );
drh9cbf3422008-01-17 16:22:13 +00002081 assert( target>0 && target<=pParse->nMem );
drh389a1ad2008-01-03 23:44:53 +00002082 if( v==0 ) return 0;
drh389a1ad2008-01-03 23:44:53 +00002083
2084 if( pExpr==0 ){
2085 op = TK_NULL;
2086 }else{
2087 op = pExpr->op;
2088 }
drhf2bc0132004-10-04 13:19:23 +00002089 switch( op ){
drh13449892005-09-07 21:22:45 +00002090 case TK_AGG_COLUMN: {
2091 AggInfo *pAggInfo = pExpr->pAggInfo;
2092 struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg];
2093 if( !pAggInfo->directMode ){
drh9de221d2008-01-05 06:51:30 +00002094 assert( pCol->iMem>0 );
2095 inReg = pCol->iMem;
drh13449892005-09-07 21:22:45 +00002096 break;
2097 }else if( pAggInfo->useSortingIdx ){
drh389a1ad2008-01-03 23:44:53 +00002098 sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdx,
2099 pCol->iSorterColumn, target);
drh13449892005-09-07 21:22:45 +00002100 break;
2101 }
2102 /* Otherwise, fall thru into the TK_COLUMN case */
2103 }
drh967e8b72000-06-21 13:59:10 +00002104 case TK_COLUMN: {
drhffe07b22005-11-03 00:41:17 +00002105 if( pExpr->iTable<0 ){
2106 /* This only happens when coding check constraints */
drhaa9b8962008-01-08 02:57:55 +00002107 assert( pParse->ckBase>0 );
2108 inReg = pExpr->iColumn + pParse->ckBase;
drhc4a3c772001-04-04 11:48:57 +00002109 }else{
drhe55cbd72008-03-31 23:48:03 +00002110 inReg = sqlite3ExprCodeGetColumn(pParse, pExpr->pTab,
drh389a1ad2008-01-03 23:44:53 +00002111 pExpr->iColumn, pExpr->iTable, target);
drh22827922000-06-06 17:27:05 +00002112 }
drhcce7d172000-05-31 15:34:51 +00002113 break;
2114 }
2115 case TK_INTEGER: {
drh9de221d2008-01-05 06:51:30 +00002116 codeInteger(v, (char*)pExpr->token.z, pExpr->token.n, 0, target);
drhfec19aa2004-05-19 20:41:03 +00002117 break;
2118 }
drh598f1342007-10-23 15:39:45 +00002119 case TK_FLOAT: {
drh9de221d2008-01-05 06:51:30 +00002120 codeReal(v, (char*)pExpr->token.z, pExpr->token.n, 0, target);
drh598f1342007-10-23 15:39:45 +00002121 break;
2122 }
drhfec19aa2004-05-19 20:41:03 +00002123 case TK_STRING: {
danielk19771e536952007-08-16 10:09:01 +00002124 sqlite3DequoteExpr(pParse->db, pExpr);
drh9de221d2008-01-05 06:51:30 +00002125 sqlite3VdbeAddOp4(v,OP_String8, 0, target, 0,
drh66a51672008-01-03 00:01:23 +00002126 (char*)pExpr->token.z, pExpr->token.n);
drhcce7d172000-05-31 15:34:51 +00002127 break;
2128 }
drhf0863fe2005-06-12 21:35:51 +00002129 case TK_NULL: {
drh9de221d2008-01-05 06:51:30 +00002130 sqlite3VdbeAddOp2(v, OP_Null, 0, target);
drhf0863fe2005-06-12 21:35:51 +00002131 break;
2132 }
danielk19775338a5f2005-01-20 13:03:10 +00002133#ifndef SQLITE_OMIT_BLOB_LITERAL
danielk1977c572ef72004-05-27 09:28:41 +00002134 case TK_BLOB: {
drh6c8c6ce2005-08-23 11:17:58 +00002135 int n;
2136 const char *z;
drhca48c902008-01-18 14:08:24 +00002137 char *zBlob;
2138 assert( pExpr->token.n>=3 );
2139 assert( pExpr->token.z[0]=='x' || pExpr->token.z[0]=='X' );
2140 assert( pExpr->token.z[1]=='\'' );
2141 assert( pExpr->token.z[pExpr->token.n-1]=='\'' );
drh6c8c6ce2005-08-23 11:17:58 +00002142 n = pExpr->token.n - 3;
drh2646da72005-12-09 20:02:05 +00002143 z = (char*)pExpr->token.z + 2;
drhca48c902008-01-18 14:08:24 +00002144 zBlob = sqlite3HexToBlob(sqlite3VdbeDb(v), z, n);
2145 sqlite3VdbeAddOp4(v, OP_Blob, n/2, target, 0, zBlob, P4_DYNAMIC);
danielk1977c572ef72004-05-27 09:28:41 +00002146 break;
2147 }
danielk19775338a5f2005-01-20 13:03:10 +00002148#endif
drh50457892003-09-06 01:10:47 +00002149 case TK_VARIABLE: {
drh9de221d2008-01-05 06:51:30 +00002150 sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iTable, target);
drh895d7472004-08-20 16:02:39 +00002151 if( pExpr->token.n>1 ){
drh66a51672008-01-03 00:01:23 +00002152 sqlite3VdbeChangeP4(v, -1, (char*)pExpr->token.z, pExpr->token.n);
drh895d7472004-08-20 16:02:39 +00002153 }
drh50457892003-09-06 01:10:47 +00002154 break;
2155 }
drh4e0cff62004-11-05 05:10:28 +00002156 case TK_REGISTER: {
drh9de221d2008-01-05 06:51:30 +00002157 inReg = pExpr->iTable;
drh4e0cff62004-11-05 05:10:28 +00002158 break;
2159 }
drh487e2622005-06-25 18:42:14 +00002160#ifndef SQLITE_OMIT_CAST
2161 case TK_CAST: {
2162 /* Expressions of the form: CAST(pLeft AS token) */
danielk1977f0113002006-01-24 12:09:17 +00002163 int aff, to_op;
drh2dcef112008-01-12 19:03:48 +00002164 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
drh8a512562005-11-14 22:29:05 +00002165 aff = sqlite3AffinityType(&pExpr->token);
danielk1977f0113002006-01-24 12:09:17 +00002166 to_op = aff - SQLITE_AFF_TEXT + OP_ToText;
2167 assert( to_op==OP_ToText || aff!=SQLITE_AFF_TEXT );
2168 assert( to_op==OP_ToBlob || aff!=SQLITE_AFF_NONE );
2169 assert( to_op==OP_ToNumeric || aff!=SQLITE_AFF_NUMERIC );
2170 assert( to_op==OP_ToInt || aff!=SQLITE_AFF_INTEGER );
2171 assert( to_op==OP_ToReal || aff!=SQLITE_AFF_REAL );
drh2dcef112008-01-12 19:03:48 +00002172 sqlite3VdbeAddOp1(v, to_op, inReg);
drh487e2622005-06-25 18:42:14 +00002173 break;
2174 }
2175#endif /* SQLITE_OMIT_CAST */
drhc9b84a12002-06-20 11:36:48 +00002176 case TK_LT:
2177 case TK_LE:
2178 case TK_GT:
2179 case TK_GE:
2180 case TK_NE:
2181 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00002182 assert( TK_LT==OP_Lt );
2183 assert( TK_LE==OP_Le );
2184 assert( TK_GT==OP_Gt );
2185 assert( TK_GE==OP_Ge );
2186 assert( TK_EQ==OP_Eq );
2187 assert( TK_NE==OP_Ne );
drh2dcef112008-01-12 19:03:48 +00002188 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
2189 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
drh35573352008-01-08 23:54:25 +00002190 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
2191 r1, r2, inReg, SQLITE_STOREP2);
danielk1977a37cdde2004-05-16 11:15:36 +00002192 break;
drhc9b84a12002-06-20 11:36:48 +00002193 }
drhcce7d172000-05-31 15:34:51 +00002194 case TK_AND:
2195 case TK_OR:
2196 case TK_PLUS:
2197 case TK_STAR:
2198 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00002199 case TK_REM:
2200 case TK_BITAND:
2201 case TK_BITOR:
drh17c40292004-07-21 02:53:29 +00002202 case TK_SLASH:
drhbf4133c2001-10-13 02:59:08 +00002203 case TK_LSHIFT:
drh855eb1c2004-08-31 13:45:11 +00002204 case TK_RSHIFT:
drh00400772000-06-16 20:51:26 +00002205 case TK_CONCAT: {
drhf2bc0132004-10-04 13:19:23 +00002206 assert( TK_AND==OP_And );
2207 assert( TK_OR==OP_Or );
2208 assert( TK_PLUS==OP_Add );
2209 assert( TK_MINUS==OP_Subtract );
2210 assert( TK_REM==OP_Remainder );
2211 assert( TK_BITAND==OP_BitAnd );
2212 assert( TK_BITOR==OP_BitOr );
2213 assert( TK_SLASH==OP_Divide );
2214 assert( TK_LSHIFT==OP_ShiftLeft );
2215 assert( TK_RSHIFT==OP_ShiftRight );
2216 assert( TK_CONCAT==OP_Concat );
drh2dcef112008-01-12 19:03:48 +00002217 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
2218 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
drh5b6afba2008-01-05 16:29:28 +00002219 sqlite3VdbeAddOp3(v, op, r2, r1, target);
drh00400772000-06-16 20:51:26 +00002220 break;
2221 }
drhcce7d172000-05-31 15:34:51 +00002222 case TK_UMINUS: {
drhfec19aa2004-05-19 20:41:03 +00002223 Expr *pLeft = pExpr->pLeft;
2224 assert( pLeft );
2225 if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
2226 Token *p = &pLeft->token;
drhfec19aa2004-05-19 20:41:03 +00002227 if( pLeft->op==TK_FLOAT ){
drh9de221d2008-01-05 06:51:30 +00002228 codeReal(v, (char*)p->z, p->n, 1, target);
drhe6840902002-03-06 03:08:25 +00002229 }else{
drh9de221d2008-01-05 06:51:30 +00002230 codeInteger(v, (char*)p->z, p->n, 1, target);
drhe6840902002-03-06 03:08:25 +00002231 }
drh3c84ddf2008-01-09 02:15:38 +00002232 }else{
drh2dcef112008-01-12 19:03:48 +00002233 regFree1 = r1 = sqlite3GetTempReg(pParse);
drh3c84ddf2008-01-09 02:15:38 +00002234 sqlite3VdbeAddOp2(v, OP_Integer, 0, r1);
drhe55cbd72008-03-31 23:48:03 +00002235 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree2);
drh2dcef112008-01-12 19:03:48 +00002236 sqlite3VdbeAddOp3(v, OP_Subtract, r2, r1, target);
drh6e142f52000-06-08 13:36:40 +00002237 }
drh3c84ddf2008-01-09 02:15:38 +00002238 inReg = target;
2239 break;
drh6e142f52000-06-08 13:36:40 +00002240 }
drhbf4133c2001-10-13 02:59:08 +00002241 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00002242 case TK_NOT: {
drhf2bc0132004-10-04 13:19:23 +00002243 assert( TK_BITNOT==OP_BitNot );
2244 assert( TK_NOT==OP_Not );
drh2dcef112008-01-12 19:03:48 +00002245 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
drh652fbf52008-04-01 01:42:41 +00002246 inReg = sqlite3ExprWritableRegister(pParse, inReg, target);
drh2dcef112008-01-12 19:03:48 +00002247 sqlite3VdbeAddOp1(v, op, inReg);
drhcce7d172000-05-31 15:34:51 +00002248 break;
2249 }
2250 case TK_ISNULL:
2251 case TK_NOTNULL: {
drh6a288a32008-01-07 19:20:24 +00002252 int addr;
drhf2bc0132004-10-04 13:19:23 +00002253 assert( TK_ISNULL==OP_IsNull );
2254 assert( TK_NOTNULL==OP_NotNull );
drh9de221d2008-01-05 06:51:30 +00002255 sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
drh2dcef112008-01-12 19:03:48 +00002256 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
2257 addr = sqlite3VdbeAddOp1(v, op, r1);
drh9de221d2008-01-05 06:51:30 +00002258 sqlite3VdbeAddOp2(v, OP_AddImm, target, -1);
drh6a288a32008-01-07 19:20:24 +00002259 sqlite3VdbeJumpHere(v, addr);
drhf2bc0132004-10-04 13:19:23 +00002260 break;
drhcce7d172000-05-31 15:34:51 +00002261 }
drh22827922000-06-06 17:27:05 +00002262 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00002263 AggInfo *pInfo = pExpr->pAggInfo;
drh7e56e712005-11-16 12:53:15 +00002264 if( pInfo==0 ){
2265 sqlite3ErrorMsg(pParse, "misuse of aggregate: %T",
2266 &pExpr->span);
2267 }else{
drh9de221d2008-01-05 06:51:30 +00002268 inReg = pInfo->aFunc[pExpr->iAgg].iMem;
drh7e56e712005-11-16 12:53:15 +00002269 }
drh22827922000-06-06 17:27:05 +00002270 break;
2271 }
drhb71090f2005-05-23 17:26:51 +00002272 case TK_CONST_FUNC:
drhcce7d172000-05-31 15:34:51 +00002273 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00002274 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00002275 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00002276 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00002277 int nId;
2278 const char *zId;
drh13449892005-09-07 21:22:45 +00002279 int constMask = 0;
danielk1977682f68b2004-06-05 10:22:17 +00002280 int i;
drh17435752007-08-16 04:30:38 +00002281 sqlite3 *db = pParse->db;
2282 u8 enc = ENC(db);
danielk1977dc1bdc42004-06-11 10:51:27 +00002283 CollSeq *pColl = 0;
drh17435752007-08-16 04:30:38 +00002284
drh2646da72005-12-09 20:02:05 +00002285 zId = (char*)pExpr->token.z;
drhb71090f2005-05-23 17:26:51 +00002286 nId = pExpr->token.n;
danielk1977d8123362004-06-12 09:25:12 +00002287 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0);
drh0bce8352002-02-28 00:41:10 +00002288 assert( pDef!=0 );
drh892d3172008-01-10 03:46:36 +00002289 if( pList ){
2290 nExpr = pList->nExpr;
drh2dcef112008-01-12 19:03:48 +00002291 r1 = sqlite3GetTempRange(pParse, nExpr);
2292 sqlite3ExprCodeExprList(pParse, pList, r1);
drh892d3172008-01-10 03:46:36 +00002293 }else{
drhd847eaa2008-01-17 17:15:56 +00002294 nExpr = r1 = 0;
drh892d3172008-01-10 03:46:36 +00002295 }
drhb7f6f682006-07-08 17:06:43 +00002296#ifndef SQLITE_OMIT_VIRTUALTABLE
drha43fa222006-07-08 18:41:37 +00002297 /* Possibly overload the function if the first argument is
2298 ** a virtual table column.
2299 **
2300 ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the
2301 ** second argument, not the first, as the argument to test to
2302 ** see if it is a column in a virtual table. This is done because
2303 ** the left operand of infix functions (the operand we want to
2304 ** control overloading) ends up as the second argument to the
2305 ** function. The expression "A glob B" is equivalent to
2306 ** "glob(B,A). We want to use the A in "A glob B" to test
2307 ** for function overloading. But we use the B term in "glob(B,A)".
2308 */
drh6a03a1c2006-07-08 18:34:59 +00002309 if( nExpr>=2 && (pExpr->flags & EP_InfixFunc) ){
drh17435752007-08-16 04:30:38 +00002310 pDef = sqlite3VtabOverloadFunction(db, pDef, nExpr, pList->a[1].pExpr);
drh6a03a1c2006-07-08 18:34:59 +00002311 }else if( nExpr>0 ){
drh17435752007-08-16 04:30:38 +00002312 pDef = sqlite3VtabOverloadFunction(db, pDef, nExpr, pList->a[0].pExpr);
drhb7f6f682006-07-08 17:06:43 +00002313 }
2314#endif
danielk1977682f68b2004-06-05 10:22:17 +00002315 for(i=0; i<nExpr && i<32; i++){
danielk1977d02eb1f2004-06-06 09:44:03 +00002316 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
drh13449892005-09-07 21:22:45 +00002317 constMask |= (1<<i);
danielk1977d02eb1f2004-06-06 09:44:03 +00002318 }
danielk1977dc1bdc42004-06-11 10:51:27 +00002319 if( pDef->needCollSeq && !pColl ){
2320 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
2321 }
2322 }
2323 if( pDef->needCollSeq ){
2324 if( !pColl ) pColl = pParse->db->pDfltColl;
drh66a51672008-01-03 00:01:23 +00002325 sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ);
danielk1977682f68b2004-06-05 10:22:17 +00002326 }
drh2dcef112008-01-12 19:03:48 +00002327 sqlite3VdbeAddOp4(v, OP_Function, constMask, r1, target,
drh66a51672008-01-03 00:01:23 +00002328 (char*)pDef, P4_FUNCDEF);
drh98757152008-01-09 23:04:12 +00002329 sqlite3VdbeChangeP5(v, nExpr);
drh2dcef112008-01-12 19:03:48 +00002330 if( nExpr ){
2331 sqlite3ReleaseTempRange(pParse, r1, nExpr);
2332 }
drhe55cbd72008-03-31 23:48:03 +00002333 sqlite3ExprExpireColumnCacheLines(pParse, r1, r1+nExpr-1);
drhcce7d172000-05-31 15:34:51 +00002334 break;
2335 }
drhfe2093d2005-01-20 22:48:47 +00002336#ifndef SQLITE_OMIT_SUBQUERY
2337 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00002338 case TK_SELECT: {
drh41714d62006-03-02 04:44:23 +00002339 if( pExpr->iColumn==0 ){
2340 sqlite3CodeSubselect(pParse, pExpr);
2341 }
drh9de221d2008-01-05 06:51:30 +00002342 inReg = pExpr->iColumn;
drh19a775c2000-06-05 18:54:46 +00002343 break;
2344 }
drhfef52082000-06-06 01:50:43 +00002345 case TK_IN: {
drh6a288a32008-01-07 19:20:24 +00002346 int j1, j2, j3, j4, j5;
drh94a11212004-09-25 13:12:14 +00002347 char affinity;
danielk19779a96b662007-11-29 17:05:18 +00002348 int eType;
danielk19779a96b662007-11-29 17:05:18 +00002349
2350 eType = sqlite3FindInIndex(pParse, pExpr, 0);
danielk1977e014a832004-05-17 10:48:57 +00002351
2352 /* Figure out the affinity to use to create a key from the results
2353 ** of the expression. affinityStr stores a static string suitable for
drh66a51672008-01-03 00:01:23 +00002354 ** P4 of OP_MakeRecord.
danielk1977e014a832004-05-17 10:48:57 +00002355 */
drh94a11212004-09-25 13:12:14 +00002356 affinity = comparisonAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +00002357
drh2dcef112008-01-12 19:03:48 +00002358 sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
danielk1977e014a832004-05-17 10:48:57 +00002359
2360 /* Code the <expr> from "<expr> IN (...)". The temporary table
2361 ** pExpr->iTable contains the values that make up the (...) set.
2362 */
drh2dcef112008-01-12 19:03:48 +00002363 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
2364 j1 = sqlite3VdbeAddOp1(v, OP_NotNull, r1);
2365 sqlite3VdbeAddOp2(v, OP_Null, 0, target);
drh6a288a32008-01-07 19:20:24 +00002366 j2 = sqlite3VdbeAddOp0(v, OP_Goto);
2367 sqlite3VdbeJumpHere(v, j1);
danielk19779a96b662007-11-29 17:05:18 +00002368 if( eType==IN_INDEX_ROWID ){
drh678ccce2008-03-31 18:19:54 +00002369 j3 = sqlite3VdbeAddOp1(v, OP_MustBeInt, r1);
drh2dcef112008-01-12 19:03:48 +00002370 j4 = sqlite3VdbeAddOp3(v, OP_NotExists, pExpr->iTable, 0, r1);
drh6a288a32008-01-07 19:20:24 +00002371 j5 = sqlite3VdbeAddOp0(v, OP_Goto);
2372 sqlite3VdbeJumpHere(v, j3);
2373 sqlite3VdbeJumpHere(v, j4);
danielk19779a96b662007-11-29 17:05:18 +00002374 }else{
drh2dcef112008-01-12 19:03:48 +00002375 r2 = regFree2 = sqlite3GetTempReg(pParse);
drh1db639c2008-01-17 02:36:28 +00002376 sqlite3VdbeAddOp4(v, OP_MakeRecord, r1, 1, r2, &affinity, 1);
drhe55cbd72008-03-31 23:48:03 +00002377 sqlite3ExprExpireColumnCacheLines(pParse, r1, r1);
drh2dcef112008-01-12 19:03:48 +00002378 j5 = sqlite3VdbeAddOp3(v, OP_Found, pExpr->iTable, 0, r2);
danielk19779a96b662007-11-29 17:05:18 +00002379 }
drh2dcef112008-01-12 19:03:48 +00002380 sqlite3VdbeAddOp2(v, OP_AddImm, target, -1);
drh6a288a32008-01-07 19:20:24 +00002381 sqlite3VdbeJumpHere(v, j2);
2382 sqlite3VdbeJumpHere(v, j5);
drhfef52082000-06-06 01:50:43 +00002383 break;
2384 }
danielk197793758c82005-01-21 08:13:14 +00002385#endif
drh2dcef112008-01-12 19:03:48 +00002386 /*
2387 ** x BETWEEN y AND z
2388 **
2389 ** This is equivalent to
2390 **
2391 ** x>=y AND x<=z
2392 **
2393 ** X is stored in pExpr->pLeft.
2394 ** Y is stored in pExpr->pList->a[0].pExpr.
2395 ** Z is stored in pExpr->pList->a[1].pExpr.
2396 */
drhfef52082000-06-06 01:50:43 +00002397 case TK_BETWEEN: {
drhbe5c89a2004-07-26 00:31:09 +00002398 Expr *pLeft = pExpr->pLeft;
2399 struct ExprList_item *pLItem = pExpr->pList->a;
2400 Expr *pRight = pLItem->pExpr;
drh35573352008-01-08 23:54:25 +00002401
drh2dcef112008-01-12 19:03:48 +00002402 r1 = sqlite3ExprCodeTemp(pParse, pLeft, &regFree1);
2403 r2 = sqlite3ExprCodeTemp(pParse, pRight, &regFree2);
2404 r3 = sqlite3GetTempReg(pParse);
drh678ccce2008-03-31 18:19:54 +00002405 r4 = sqlite3GetTempReg(pParse);
drh35573352008-01-08 23:54:25 +00002406 codeCompare(pParse, pLeft, pRight, OP_Ge,
2407 r1, r2, r3, SQLITE_STOREP2);
drhbe5c89a2004-07-26 00:31:09 +00002408 pLItem++;
2409 pRight = pLItem->pExpr;
drh2dcef112008-01-12 19:03:48 +00002410 sqlite3ReleaseTempReg(pParse, regFree2);
2411 r2 = sqlite3ExprCodeTemp(pParse, pRight, &regFree2);
drh678ccce2008-03-31 18:19:54 +00002412 codeCompare(pParse, pLeft, pRight, OP_Le, r1, r2, r4, SQLITE_STOREP2);
2413 sqlite3VdbeAddOp3(v, OP_And, r3, r4, target);
drh2dcef112008-01-12 19:03:48 +00002414 sqlite3ReleaseTempReg(pParse, r3);
drh678ccce2008-03-31 18:19:54 +00002415 sqlite3ReleaseTempReg(pParse, r4);
drhfef52082000-06-06 01:50:43 +00002416 break;
2417 }
drh4f07e5f2007-05-14 11:34:46 +00002418 case TK_UPLUS: {
drh2dcef112008-01-12 19:03:48 +00002419 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
drha2e00042002-01-22 03:13:42 +00002420 break;
2421 }
drh2dcef112008-01-12 19:03:48 +00002422
2423 /*
2424 ** Form A:
2425 ** CASE x WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
2426 **
2427 ** Form B:
2428 ** CASE WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
2429 **
2430 ** Form A is can be transformed into the equivalent form B as follows:
2431 ** CASE WHEN x=e1 THEN r1 WHEN x=e2 THEN r2 ...
2432 ** WHEN x=eN THEN rN ELSE y END
2433 **
2434 ** X (if it exists) is in pExpr->pLeft.
2435 ** Y is in pExpr->pRight. The Y is also optional. If there is no
2436 ** ELSE clause and no other term matches, then the result of the
2437 ** exprssion is NULL.
2438 ** Ei is in pExpr->pList->a[i*2] and Ri is pExpr->pList->a[i*2+1].
2439 **
2440 ** The result of the expression is the Ri for the first matching Ei,
2441 ** or if there is no matching Ei, the ELSE term Y, or if there is
2442 ** no ELSE term, NULL.
2443 */
drh17a7f8d2002-03-24 13:13:27 +00002444 case TK_CASE: {
drh2dcef112008-01-12 19:03:48 +00002445 int endLabel; /* GOTO label for end of CASE stmt */
2446 int nextCase; /* GOTO label for next WHEN clause */
2447 int nExpr; /* 2x number of WHEN terms */
2448 int i; /* Loop counter */
2449 ExprList *pEList; /* List of WHEN terms */
2450 struct ExprList_item *aListelem; /* Array of WHEN terms */
2451 Expr opCompare; /* The X==Ei expression */
2452 Expr cacheX; /* Cached expression X */
2453 Expr *pX; /* The X expression */
2454 Expr *pTest; /* X==Ei (form A) or just Ei (form B) */
drh17a7f8d2002-03-24 13:13:27 +00002455
2456 assert(pExpr->pList);
2457 assert((pExpr->pList->nExpr % 2) == 0);
2458 assert(pExpr->pList->nExpr > 0);
drhbe5c89a2004-07-26 00:31:09 +00002459 pEList = pExpr->pList;
2460 aListelem = pEList->a;
2461 nExpr = pEList->nExpr;
drh2dcef112008-01-12 19:03:48 +00002462 endLabel = sqlite3VdbeMakeLabel(v);
2463 if( (pX = pExpr->pLeft)!=0 ){
2464 cacheX = *pX;
2465 cacheX.iTable = sqlite3ExprCodeTemp(pParse, pX, &regFree1);
2466 cacheX.op = TK_REGISTER;
drh678ccce2008-03-31 18:19:54 +00002467 cacheX.iColumn = 0;
drh2dcef112008-01-12 19:03:48 +00002468 opCompare.op = TK_EQ;
2469 opCompare.pLeft = &cacheX;
2470 pTest = &opCompare;
drh17a7f8d2002-03-24 13:13:27 +00002471 }
drh652fbf52008-04-01 01:42:41 +00002472 sqlite3ExprColumnCacheDisable(pParse, 1);
drhf5905aa2002-05-26 20:54:33 +00002473 for(i=0; i<nExpr; i=i+2){
drh2dcef112008-01-12 19:03:48 +00002474 if( pX ){
2475 opCompare.pRight = aListelem[i].pExpr;
drh17a7f8d2002-03-24 13:13:27 +00002476 }else{
drh2dcef112008-01-12 19:03:48 +00002477 pTest = aListelem[i].pExpr;
drh17a7f8d2002-03-24 13:13:27 +00002478 }
drh2dcef112008-01-12 19:03:48 +00002479 nextCase = sqlite3VdbeMakeLabel(v);
2480 sqlite3ExprIfFalse(pParse, pTest, nextCase, SQLITE_JUMPIFNULL);
drh9de221d2008-01-05 06:51:30 +00002481 sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target);
drh2dcef112008-01-12 19:03:48 +00002482 sqlite3VdbeAddOp2(v, OP_Goto, 0, endLabel);
2483 sqlite3VdbeResolveLabel(v, nextCase);
drhf570f012002-05-31 15:51:25 +00002484 }
drh17a7f8d2002-03-24 13:13:27 +00002485 if( pExpr->pRight ){
drh9de221d2008-01-05 06:51:30 +00002486 sqlite3ExprCode(pParse, pExpr->pRight, target);
drh17a7f8d2002-03-24 13:13:27 +00002487 }else{
drh9de221d2008-01-05 06:51:30 +00002488 sqlite3VdbeAddOp2(v, OP_Null, 0, target);
drh17a7f8d2002-03-24 13:13:27 +00002489 }
drh2dcef112008-01-12 19:03:48 +00002490 sqlite3VdbeResolveLabel(v, endLabel);
drhe55cbd72008-03-31 23:48:03 +00002491 sqlite3ExprColumnCacheDisable(pParse, -1);
danielk19776f349032002-06-11 02:25:40 +00002492 break;
2493 }
danielk19775338a5f2005-01-20 13:03:10 +00002494#ifndef SQLITE_OMIT_TRIGGER
danielk19776f349032002-06-11 02:25:40 +00002495 case TK_RAISE: {
2496 if( !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +00002497 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00002498 "RAISE() may only be used within a trigger-program");
drh389a1ad2008-01-03 23:44:53 +00002499 return 0;
danielk19776f349032002-06-11 02:25:40 +00002500 }
drhad6d9462004-09-19 02:15:24 +00002501 if( pExpr->iColumn!=OE_Ignore ){
2502 assert( pExpr->iColumn==OE_Rollback ||
2503 pExpr->iColumn == OE_Abort ||
2504 pExpr->iColumn == OE_Fail );
danielk19771e536952007-08-16 10:09:01 +00002505 sqlite3DequoteExpr(pParse->db, pExpr);
drh66a51672008-01-03 00:01:23 +00002506 sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn, 0,
drh2646da72005-12-09 20:02:05 +00002507 (char*)pExpr->token.z, pExpr->token.n);
danielk19776f349032002-06-11 02:25:40 +00002508 } else {
drhad6d9462004-09-19 02:15:24 +00002509 assert( pExpr->iColumn == OE_Ignore );
drh66a51672008-01-03 00:01:23 +00002510 sqlite3VdbeAddOp2(v, OP_ContextPop, 0, 0);
2511 sqlite3VdbeAddOp2(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
drhd4e70eb2008-01-02 00:34:36 +00002512 VdbeComment((v, "raise(IGNORE)"));
danielk19776f349032002-06-11 02:25:40 +00002513 }
drhffe07b22005-11-03 00:41:17 +00002514 break;
drh17a7f8d2002-03-24 13:13:27 +00002515 }
danielk19775338a5f2005-01-20 13:03:10 +00002516#endif
drhffe07b22005-11-03 00:41:17 +00002517 }
drh2dcef112008-01-12 19:03:48 +00002518 sqlite3ReleaseTempReg(pParse, regFree1);
2519 sqlite3ReleaseTempReg(pParse, regFree2);
2520 return inReg;
2521}
2522
2523/*
2524** Generate code to evaluate an expression and store the results
2525** into a register. Return the register number where the results
2526** are stored.
2527**
2528** If the register is a temporary register that can be deallocated,
drh678ccce2008-03-31 18:19:54 +00002529** then write its number into *pReg. If the result register is not
drh2dcef112008-01-12 19:03:48 +00002530** a temporary, then set *pReg to zero.
2531*/
2532int sqlite3ExprCodeTemp(Parse *pParse, Expr *pExpr, int *pReg){
2533 int r1 = sqlite3GetTempReg(pParse);
2534 int r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
2535 if( r2==r1 ){
2536 *pReg = r1;
2537 }else{
2538 sqlite3ReleaseTempReg(pParse, r1);
2539 *pReg = 0;
2540 }
2541 return r2;
2542}
2543
2544/*
2545** Generate code that will evaluate expression pExpr and store the
2546** results in register target. The results are guaranteed to appear
2547** in register target.
2548*/
2549int sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){
drh9cbf3422008-01-17 16:22:13 +00002550 int inReg;
2551
2552 assert( target>0 && target<=pParse->nMem );
2553 inReg = sqlite3ExprCodeTarget(pParse, pExpr, target);
drh0e359b32008-01-13 19:02:11 +00002554 assert( pParse->pVdbe || pParse->db->mallocFailed );
2555 if( inReg!=target && pParse->pVdbe ){
drh9cbf3422008-01-17 16:22:13 +00002556 sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, inReg, target);
drhcce7d172000-05-31 15:34:51 +00002557 }
drh389a1ad2008-01-03 23:44:53 +00002558 return target;
drhcce7d172000-05-31 15:34:51 +00002559}
2560
2561/*
drh2dcef112008-01-12 19:03:48 +00002562** Generate code that evalutes the given expression and puts the result
drhde4fcfd2008-01-19 23:50:26 +00002563** in register target.
drh25303782004-12-07 15:41:48 +00002564**
drh2dcef112008-01-12 19:03:48 +00002565** Also make a copy of the expression results into another "cache" register
2566** and modify the expression so that the next time it is evaluated,
2567** the result is a copy of the cache register.
2568**
2569** This routine is used for expressions that are used multiple
2570** times. They are evaluated once and the results of the expression
2571** are reused.
drh25303782004-12-07 15:41:48 +00002572*/
drh2dcef112008-01-12 19:03:48 +00002573int sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr, int target){
drh25303782004-12-07 15:41:48 +00002574 Vdbe *v = pParse->pVdbe;
drh2dcef112008-01-12 19:03:48 +00002575 int inReg;
2576 inReg = sqlite3ExprCode(pParse, pExpr, target);
drhde4fcfd2008-01-19 23:50:26 +00002577 assert( target>0 );
drh2dcef112008-01-12 19:03:48 +00002578 if( pExpr->op!=TK_REGISTER ){
2579 int iMem;
drhde4fcfd2008-01-19 23:50:26 +00002580 iMem = ++pParse->nMem;
2581 sqlite3VdbeAddOp2(v, OP_Copy, inReg, iMem);
drh2dcef112008-01-12 19:03:48 +00002582 pExpr->iTable = iMem;
drh678ccce2008-03-31 18:19:54 +00002583 pExpr->iColumn = pExpr->op;
drh25303782004-12-07 15:41:48 +00002584 pExpr->op = TK_REGISTER;
2585 }
drh2dcef112008-01-12 19:03:48 +00002586 return inReg;
drh25303782004-12-07 15:41:48 +00002587}
drh2dcef112008-01-12 19:03:48 +00002588
drh678ccce2008-03-31 18:19:54 +00002589/*
2590** If pExpr is a constant expression, then evaluate the expression
2591** into a register and convert the expression into a TK_REGISTER
2592** expression.
2593*/
2594static int evalConstExpr(void *pArg, Expr *pExpr){
2595 Parse *pParse = (Parse*)pArg;
2596 if( pExpr->op==TK_REGISTER ){
2597 return 1;
2598 }
2599 if( sqlite3ExprIsConstantNotJoin(pExpr) ){
2600 int r1 = ++pParse->nMem;
2601 int r2;
2602 r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
2603 if( r1!=r2 ) pParse->nMem--;
2604 pExpr->iColumn = pExpr->op;
2605 pExpr->op = TK_REGISTER;
2606 pExpr->iTable = r2;
2607 return 1;
2608 }
2609 return 0;
2610}
2611
2612/*
2613** Preevaluate constant subexpressions within pExpr and store the
2614** results in registers. Modify pExpr so that the constant subexpresions
2615** are TK_REGISTER opcodes that refer to the precomputed values.
2616*/
2617void sqlite3ExprCodeConstants(Parse *pParse, Expr *pExpr){
2618 walkExprTree(pExpr, evalConstExpr, pParse);
2619}
2620
drh25303782004-12-07 15:41:48 +00002621
2622/*
drh268380c2004-02-25 13:47:31 +00002623** Generate code that pushes the value of every element of the given
drh9cbf3422008-01-17 16:22:13 +00002624** expression list into a sequence of registers beginning at target.
drh268380c2004-02-25 13:47:31 +00002625**
drh892d3172008-01-10 03:46:36 +00002626** Return the number of elements evaluated.
drh268380c2004-02-25 13:47:31 +00002627*/
danielk19774adee202004-05-08 08:23:19 +00002628int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00002629 Parse *pParse, /* Parsing context */
drh389a1ad2008-01-03 23:44:53 +00002630 ExprList *pList, /* The expression list to be coded */
2631 int target /* Where to write results */
drh268380c2004-02-25 13:47:31 +00002632){
2633 struct ExprList_item *pItem;
drh9cbf3422008-01-17 16:22:13 +00002634 int i, n;
drh892d3172008-01-10 03:46:36 +00002635 assert( pList!=0 || pParse->db->mallocFailed );
2636 if( pList==0 ){
2637 return 0;
2638 }
drh9cbf3422008-01-17 16:22:13 +00002639 assert( target>0 );
drh268380c2004-02-25 13:47:31 +00002640 n = pList->nExpr;
drhc182d162005-08-14 20:47:16 +00002641 for(pItem=pList->a, i=n; i>0; i--, pItem++){
drh389a1ad2008-01-03 23:44:53 +00002642 sqlite3ExprCode(pParse, pItem->pExpr, target);
drh9cbf3422008-01-17 16:22:13 +00002643 target++;
drh268380c2004-02-25 13:47:31 +00002644 }
drhf9b596e2004-05-26 16:54:42 +00002645 return n;
drh268380c2004-02-25 13:47:31 +00002646}
2647
2648/*
drhcce7d172000-05-31 15:34:51 +00002649** Generate code for a boolean expression such that a jump is made
2650** to the label "dest" if the expression is true but execution
2651** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00002652**
2653** If the expression evaluates to NULL (neither true nor false), then
drh35573352008-01-08 23:54:25 +00002654** take the jump if the jumpIfNull flag is SQLITE_JUMPIFNULL.
drhf2bc0132004-10-04 13:19:23 +00002655**
2656** This code depends on the fact that certain token values (ex: TK_EQ)
2657** are the same as opcode values (ex: OP_Eq) that implement the corresponding
2658** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
2659** the make process cause these values to align. Assert()s in the code
2660** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00002661*/
danielk19774adee202004-05-08 08:23:19 +00002662void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00002663 Vdbe *v = pParse->pVdbe;
2664 int op = 0;
drh2dcef112008-01-12 19:03:48 +00002665 int regFree1 = 0;
2666 int regFree2 = 0;
2667 int r1, r2;
2668
drh35573352008-01-08 23:54:25 +00002669 assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
drhdaffd0e2001-04-11 14:28:42 +00002670 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00002671 op = pExpr->op;
2672 switch( op ){
drhcce7d172000-05-31 15:34:51 +00002673 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00002674 int d2 = sqlite3VdbeMakeLabel(v);
drh35573352008-01-08 23:54:25 +00002675 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,jumpIfNull^SQLITE_JUMPIFNULL);
drhe55cbd72008-03-31 23:48:03 +00002676 pParse->disableColCache++;
danielk19774adee202004-05-08 08:23:19 +00002677 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhe55cbd72008-03-31 23:48:03 +00002678 pParse->disableColCache--;
danielk19774adee202004-05-08 08:23:19 +00002679 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00002680 break;
2681 }
2682 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00002683 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhe55cbd72008-03-31 23:48:03 +00002684 pParse->disableColCache++;
danielk19774adee202004-05-08 08:23:19 +00002685 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhe55cbd72008-03-31 23:48:03 +00002686 pParse->disableColCache--;
drhcce7d172000-05-31 15:34:51 +00002687 break;
2688 }
2689 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00002690 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002691 break;
2692 }
2693 case TK_LT:
2694 case TK_LE:
2695 case TK_GT:
2696 case TK_GE:
2697 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00002698 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00002699 assert( TK_LT==OP_Lt );
2700 assert( TK_LE==OP_Le );
2701 assert( TK_GT==OP_Gt );
2702 assert( TK_GE==OP_Ge );
2703 assert( TK_EQ==OP_Eq );
2704 assert( TK_NE==OP_Ne );
drh2dcef112008-01-12 19:03:48 +00002705 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
2706 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
drh35573352008-01-08 23:54:25 +00002707 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
drh2dcef112008-01-12 19:03:48 +00002708 r1, r2, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002709 break;
2710 }
2711 case TK_ISNULL:
2712 case TK_NOTNULL: {
drhf2bc0132004-10-04 13:19:23 +00002713 assert( TK_ISNULL==OP_IsNull );
2714 assert( TK_NOTNULL==OP_NotNull );
drh2dcef112008-01-12 19:03:48 +00002715 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
2716 sqlite3VdbeAddOp2(v, op, r1, dest);
drhcce7d172000-05-31 15:34:51 +00002717 break;
2718 }
drhfef52082000-06-06 01:50:43 +00002719 case TK_BETWEEN: {
drh2dcef112008-01-12 19:03:48 +00002720 /* x BETWEEN y AND z
danielk19770202b292004-06-09 09:55:16 +00002721 **
drh2dcef112008-01-12 19:03:48 +00002722 ** Is equivalent to
2723 **
2724 ** x>=y AND x<=z
2725 **
2726 ** Code it as such, taking care to do the common subexpression
2727 ** elementation of x.
danielk19770202b292004-06-09 09:55:16 +00002728 */
drh2dcef112008-01-12 19:03:48 +00002729 Expr exprAnd;
2730 Expr compLeft;
2731 Expr compRight;
2732 Expr exprX;
danielk19770202b292004-06-09 09:55:16 +00002733
drh2dcef112008-01-12 19:03:48 +00002734 exprX = *pExpr->pLeft;
2735 exprAnd.op = TK_AND;
2736 exprAnd.pLeft = &compLeft;
2737 exprAnd.pRight = &compRight;
2738 compLeft.op = TK_GE;
2739 compLeft.pLeft = &exprX;
2740 compLeft.pRight = pExpr->pList->a[0].pExpr;
2741 compRight.op = TK_LE;
2742 compRight.pLeft = &exprX;
2743 compRight.pRight = pExpr->pList->a[1].pExpr;
2744 exprX.iTable = sqlite3ExprCodeTemp(pParse, &exprX, &regFree1);
2745 exprX.op = TK_REGISTER;
2746 sqlite3ExprIfTrue(pParse, &exprAnd, dest, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00002747 break;
2748 }
drhcce7d172000-05-31 15:34:51 +00002749 default: {
drh2dcef112008-01-12 19:03:48 +00002750 r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
2751 sqlite3VdbeAddOp3(v, OP_If, r1, dest, jumpIfNull!=0);
drhcce7d172000-05-31 15:34:51 +00002752 break;
2753 }
2754 }
drh2dcef112008-01-12 19:03:48 +00002755 sqlite3ReleaseTempReg(pParse, regFree1);
2756 sqlite3ReleaseTempReg(pParse, regFree2);
drhcce7d172000-05-31 15:34:51 +00002757}
2758
2759/*
drh66b89c82000-11-28 20:47:17 +00002760** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00002761** to the label "dest" if the expression is false but execution
2762** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00002763**
2764** If the expression evaluates to NULL (neither true nor false) then
drh35573352008-01-08 23:54:25 +00002765** jump if jumpIfNull is SQLITE_JUMPIFNULL or fall through if jumpIfNull
2766** is 0.
drhcce7d172000-05-31 15:34:51 +00002767*/
danielk19774adee202004-05-08 08:23:19 +00002768void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00002769 Vdbe *v = pParse->pVdbe;
2770 int op = 0;
drh2dcef112008-01-12 19:03:48 +00002771 int regFree1 = 0;
2772 int regFree2 = 0;
2773 int r1, r2;
2774
drh35573352008-01-08 23:54:25 +00002775 assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
drhdaffd0e2001-04-11 14:28:42 +00002776 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00002777
2778 /* The value of pExpr->op and op are related as follows:
2779 **
2780 ** pExpr->op op
2781 ** --------- ----------
2782 ** TK_ISNULL OP_NotNull
2783 ** TK_NOTNULL OP_IsNull
2784 ** TK_NE OP_Eq
2785 ** TK_EQ OP_Ne
2786 ** TK_GT OP_Le
2787 ** TK_LE OP_Gt
2788 ** TK_GE OP_Lt
2789 ** TK_LT OP_Ge
2790 **
2791 ** For other values of pExpr->op, op is undefined and unused.
2792 ** The value of TK_ and OP_ constants are arranged such that we
2793 ** can compute the mapping above using the following expression.
2794 ** Assert()s verify that the computation is correct.
2795 */
2796 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
2797
2798 /* Verify correct alignment of TK_ and OP_ constants
2799 */
2800 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
2801 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
2802 assert( pExpr->op!=TK_NE || op==OP_Eq );
2803 assert( pExpr->op!=TK_EQ || op==OP_Ne );
2804 assert( pExpr->op!=TK_LT || op==OP_Ge );
2805 assert( pExpr->op!=TK_LE || op==OP_Gt );
2806 assert( pExpr->op!=TK_GT || op==OP_Le );
2807 assert( pExpr->op!=TK_GE || op==OP_Lt );
2808
drhcce7d172000-05-31 15:34:51 +00002809 switch( pExpr->op ){
2810 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00002811 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhe55cbd72008-03-31 23:48:03 +00002812 pParse->disableColCache++;
danielk19774adee202004-05-08 08:23:19 +00002813 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhe55cbd72008-03-31 23:48:03 +00002814 pParse->disableColCache--;
drhcce7d172000-05-31 15:34:51 +00002815 break;
2816 }
2817 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00002818 int d2 = sqlite3VdbeMakeLabel(v);
drh35573352008-01-08 23:54:25 +00002819 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, jumpIfNull^SQLITE_JUMPIFNULL);
drhe55cbd72008-03-31 23:48:03 +00002820 pParse->disableColCache++;
danielk19774adee202004-05-08 08:23:19 +00002821 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhe55cbd72008-03-31 23:48:03 +00002822 pParse->disableColCache--;
danielk19774adee202004-05-08 08:23:19 +00002823 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00002824 break;
2825 }
2826 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00002827 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002828 break;
2829 }
2830 case TK_LT:
2831 case TK_LE:
2832 case TK_GT:
2833 case TK_GE:
2834 case TK_NE:
2835 case TK_EQ: {
drh2dcef112008-01-12 19:03:48 +00002836 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
2837 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
drh35573352008-01-08 23:54:25 +00002838 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
drh2dcef112008-01-12 19:03:48 +00002839 r1, r2, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002840 break;
2841 }
drhcce7d172000-05-31 15:34:51 +00002842 case TK_ISNULL:
2843 case TK_NOTNULL: {
drh2dcef112008-01-12 19:03:48 +00002844 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
2845 sqlite3VdbeAddOp2(v, op, r1, dest);
drhcce7d172000-05-31 15:34:51 +00002846 break;
2847 }
drhfef52082000-06-06 01:50:43 +00002848 case TK_BETWEEN: {
drh2dcef112008-01-12 19:03:48 +00002849 /* x BETWEEN y AND z
danielk19770202b292004-06-09 09:55:16 +00002850 **
drh2dcef112008-01-12 19:03:48 +00002851 ** Is equivalent to
2852 **
2853 ** x>=y AND x<=z
2854 **
2855 ** Code it as such, taking care to do the common subexpression
2856 ** elementation of x.
danielk19770202b292004-06-09 09:55:16 +00002857 */
drh2dcef112008-01-12 19:03:48 +00002858 Expr exprAnd;
2859 Expr compLeft;
2860 Expr compRight;
2861 Expr exprX;
drhbe5c89a2004-07-26 00:31:09 +00002862
drh2dcef112008-01-12 19:03:48 +00002863 exprX = *pExpr->pLeft;
2864 exprAnd.op = TK_AND;
2865 exprAnd.pLeft = &compLeft;
2866 exprAnd.pRight = &compRight;
2867 compLeft.op = TK_GE;
2868 compLeft.pLeft = &exprX;
2869 compLeft.pRight = pExpr->pList->a[0].pExpr;
2870 compRight.op = TK_LE;
2871 compRight.pLeft = &exprX;
2872 compRight.pRight = pExpr->pList->a[1].pExpr;
2873 exprX.iTable = sqlite3ExprCodeTemp(pParse, &exprX, &regFree1);
2874 exprX.op = TK_REGISTER;
2875 sqlite3ExprIfFalse(pParse, &exprAnd, dest, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00002876 break;
2877 }
drhcce7d172000-05-31 15:34:51 +00002878 default: {
drh2dcef112008-01-12 19:03:48 +00002879 r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
2880 sqlite3VdbeAddOp3(v, OP_IfNot, r1, dest, jumpIfNull!=0);
drhcce7d172000-05-31 15:34:51 +00002881 break;
2882 }
2883 }
drh2dcef112008-01-12 19:03:48 +00002884 sqlite3ReleaseTempReg(pParse, regFree1);
2885 sqlite3ReleaseTempReg(pParse, regFree2);
drhcce7d172000-05-31 15:34:51 +00002886}
drh22827922000-06-06 17:27:05 +00002887
2888/*
2889** Do a deep comparison of two expression trees. Return TRUE (non-zero)
2890** if they are identical and return FALSE if they differ in any way.
drhd40aab02007-02-24 15:29:03 +00002891**
2892** Sometimes this routine will return FALSE even if the two expressions
2893** really are equivalent. If we cannot prove that the expressions are
2894** identical, we return FALSE just to be safe. So if this routine
2895** returns false, then you do not really know for certain if the two
2896** expressions are the same. But if you get a TRUE return, then you
2897** can be sure the expressions are the same. In the places where
2898** this routine is used, it does not hurt to get an extra FALSE - that
2899** just might result in some slightly slower code. But returning
2900** an incorrect TRUE could lead to a malfunction.
drh22827922000-06-06 17:27:05 +00002901*/
danielk19774adee202004-05-08 08:23:19 +00002902int sqlite3ExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00002903 int i;
danielk19774b202ae2006-01-23 05:50:58 +00002904 if( pA==0||pB==0 ){
2905 return pB==pA;
drh22827922000-06-06 17:27:05 +00002906 }
2907 if( pA->op!=pB->op ) return 0;
drhfd357972005-09-09 01:33:19 +00002908 if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 0;
danielk19774adee202004-05-08 08:23:19 +00002909 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
2910 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00002911 if( pA->pList ){
2912 if( pB->pList==0 ) return 0;
2913 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
2914 for(i=0; i<pA->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00002915 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00002916 return 0;
2917 }
2918 }
2919 }else if( pB->pList ){
2920 return 0;
2921 }
2922 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00002923 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drhdd735212007-02-24 13:53:05 +00002924 if( pA->op!=TK_COLUMN && pA->token.z ){
drh22827922000-06-06 17:27:05 +00002925 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00002926 if( pB->token.n!=pA->token.n ) return 0;
drh2646da72005-12-09 20:02:05 +00002927 if( sqlite3StrNICmp((char*)pA->token.z,(char*)pB->token.z,pB->token.n)!=0 ){
2928 return 0;
2929 }
drh22827922000-06-06 17:27:05 +00002930 }
2931 return 1;
2932}
2933
drh13449892005-09-07 21:22:45 +00002934
drh22827922000-06-06 17:27:05 +00002935/*
drh13449892005-09-07 21:22:45 +00002936** Add a new element to the pAggInfo->aCol[] array. Return the index of
2937** the new element. Return a negative number if malloc fails.
drh22827922000-06-06 17:27:05 +00002938*/
drh17435752007-08-16 04:30:38 +00002939static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){
drh13449892005-09-07 21:22:45 +00002940 int i;
drhcf643722007-03-27 13:36:37 +00002941 pInfo->aCol = sqlite3ArrayAllocate(
drh17435752007-08-16 04:30:38 +00002942 db,
drhcf643722007-03-27 13:36:37 +00002943 pInfo->aCol,
2944 sizeof(pInfo->aCol[0]),
2945 3,
2946 &pInfo->nColumn,
2947 &pInfo->nColumnAlloc,
2948 &i
2949 );
drh13449892005-09-07 21:22:45 +00002950 return i;
2951}
2952
2953/*
2954** Add a new element to the pAggInfo->aFunc[] array. Return the index of
2955** the new element. Return a negative number if malloc fails.
2956*/
drh17435752007-08-16 04:30:38 +00002957static int addAggInfoFunc(sqlite3 *db, AggInfo *pInfo){
drh13449892005-09-07 21:22:45 +00002958 int i;
drhcf643722007-03-27 13:36:37 +00002959 pInfo->aFunc = sqlite3ArrayAllocate(
drh17435752007-08-16 04:30:38 +00002960 db,
drhcf643722007-03-27 13:36:37 +00002961 pInfo->aFunc,
2962 sizeof(pInfo->aFunc[0]),
2963 3,
2964 &pInfo->nFunc,
2965 &pInfo->nFuncAlloc,
2966 &i
2967 );
drh13449892005-09-07 21:22:45 +00002968 return i;
2969}
drh22827922000-06-06 17:27:05 +00002970
2971/*
drh626a8792005-01-17 22:08:19 +00002972** This is an xFunc for walkExprTree() used to implement
2973** sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates
2974** for additional information.
drh22827922000-06-06 17:27:05 +00002975**
drh626a8792005-01-17 22:08:19 +00002976** This routine analyzes the aggregate function at pExpr.
drh22827922000-06-06 17:27:05 +00002977*/
drh626a8792005-01-17 22:08:19 +00002978static int analyzeAggregate(void *pArg, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00002979 int i;
danielk1977a58fdfb2005-02-08 07:50:40 +00002980 NameContext *pNC = (NameContext *)pArg;
2981 Parse *pParse = pNC->pParse;
2982 SrcList *pSrcList = pNC->pSrcList;
drh13449892005-09-07 21:22:45 +00002983 AggInfo *pAggInfo = pNC->pAggInfo;
drh22827922000-06-06 17:27:05 +00002984
drh22827922000-06-06 17:27:05 +00002985 switch( pExpr->op ){
drh89c69d02007-01-04 01:20:28 +00002986 case TK_AGG_COLUMN:
drh967e8b72000-06-21 13:59:10 +00002987 case TK_COLUMN: {
drh13449892005-09-07 21:22:45 +00002988 /* Check to see if the column is in one of the tables in the FROM
2989 ** clause of the aggregate query */
2990 if( pSrcList ){
2991 struct SrcList_item *pItem = pSrcList->a;
2992 for(i=0; i<pSrcList->nSrc; i++, pItem++){
2993 struct AggInfo_col *pCol;
2994 if( pExpr->iTable==pItem->iCursor ){
2995 /* If we reach this point, it means that pExpr refers to a table
2996 ** that is in the FROM clause of the aggregate query.
2997 **
2998 ** Make an entry for the column in pAggInfo->aCol[] if there
2999 ** is not an entry there already.
3000 */
drh7f906d62007-03-12 23:48:52 +00003001 int k;
drh13449892005-09-07 21:22:45 +00003002 pCol = pAggInfo->aCol;
drh7f906d62007-03-12 23:48:52 +00003003 for(k=0; k<pAggInfo->nColumn; k++, pCol++){
drh13449892005-09-07 21:22:45 +00003004 if( pCol->iTable==pExpr->iTable &&
3005 pCol->iColumn==pExpr->iColumn ){
3006 break;
3007 }
danielk1977a58fdfb2005-02-08 07:50:40 +00003008 }
danielk19771e536952007-08-16 10:09:01 +00003009 if( (k>=pAggInfo->nColumn)
3010 && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0
3011 ){
drh7f906d62007-03-12 23:48:52 +00003012 pCol = &pAggInfo->aCol[k];
danielk19770817d0d2007-02-14 09:19:36 +00003013 pCol->pTab = pExpr->pTab;
drh13449892005-09-07 21:22:45 +00003014 pCol->iTable = pExpr->iTable;
3015 pCol->iColumn = pExpr->iColumn;
drh0a07c102008-01-03 18:03:08 +00003016 pCol->iMem = ++pParse->nMem;
drh13449892005-09-07 21:22:45 +00003017 pCol->iSorterColumn = -1;
drh5774b802005-09-07 22:48:16 +00003018 pCol->pExpr = pExpr;
drh13449892005-09-07 21:22:45 +00003019 if( pAggInfo->pGroupBy ){
3020 int j, n;
3021 ExprList *pGB = pAggInfo->pGroupBy;
3022 struct ExprList_item *pTerm = pGB->a;
3023 n = pGB->nExpr;
3024 for(j=0; j<n; j++, pTerm++){
3025 Expr *pE = pTerm->pExpr;
3026 if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable &&
3027 pE->iColumn==pExpr->iColumn ){
3028 pCol->iSorterColumn = j;
3029 break;
3030 }
3031 }
3032 }
3033 if( pCol->iSorterColumn<0 ){
3034 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
3035 }
3036 }
3037 /* There is now an entry for pExpr in pAggInfo->aCol[] (either
3038 ** because it was there before or because we just created it).
3039 ** Convert the pExpr to be a TK_AGG_COLUMN referring to that
3040 ** pAggInfo->aCol[] entry.
3041 */
3042 pExpr->pAggInfo = pAggInfo;
3043 pExpr->op = TK_AGG_COLUMN;
drh7f906d62007-03-12 23:48:52 +00003044 pExpr->iAgg = k;
drh13449892005-09-07 21:22:45 +00003045 break;
3046 } /* endif pExpr->iTable==pItem->iCursor */
3047 } /* end loop over pSrcList */
drh22827922000-06-06 17:27:05 +00003048 }
drh626a8792005-01-17 22:08:19 +00003049 return 1;
drh22827922000-06-06 17:27:05 +00003050 }
3051 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00003052 /* The pNC->nDepth==0 test causes aggregate functions in subqueries
3053 ** to be ignored */
danielk1977a58fdfb2005-02-08 07:50:40 +00003054 if( pNC->nDepth==0 ){
drh13449892005-09-07 21:22:45 +00003055 /* Check to see if pExpr is a duplicate of another aggregate
3056 ** function that is already in the pAggInfo structure
3057 */
3058 struct AggInfo_func *pItem = pAggInfo->aFunc;
3059 for(i=0; i<pAggInfo->nFunc; i++, pItem++){
3060 if( sqlite3ExprCompare(pItem->pExpr, pExpr) ){
danielk1977a58fdfb2005-02-08 07:50:40 +00003061 break;
3062 }
drh22827922000-06-06 17:27:05 +00003063 }
drh13449892005-09-07 21:22:45 +00003064 if( i>=pAggInfo->nFunc ){
3065 /* pExpr is original. Make a new entry in pAggInfo->aFunc[]
3066 */
danielk197714db2662006-01-09 16:12:04 +00003067 u8 enc = ENC(pParse->db);
danielk19771e536952007-08-16 10:09:01 +00003068 i = addAggInfoFunc(pParse->db, pAggInfo);
drh13449892005-09-07 21:22:45 +00003069 if( i>=0 ){
3070 pItem = &pAggInfo->aFunc[i];
3071 pItem->pExpr = pExpr;
drh0a07c102008-01-03 18:03:08 +00003072 pItem->iMem = ++pParse->nMem;
drh13449892005-09-07 21:22:45 +00003073 pItem->pFunc = sqlite3FindFunction(pParse->db,
drh2646da72005-12-09 20:02:05 +00003074 (char*)pExpr->token.z, pExpr->token.n,
drh13449892005-09-07 21:22:45 +00003075 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
drhfd357972005-09-09 01:33:19 +00003076 if( pExpr->flags & EP_Distinct ){
3077 pItem->iDistinct = pParse->nTab++;
3078 }else{
3079 pItem->iDistinct = -1;
3080 }
drh13449892005-09-07 21:22:45 +00003081 }
danielk1977a58fdfb2005-02-08 07:50:40 +00003082 }
drh13449892005-09-07 21:22:45 +00003083 /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
3084 */
danielk1977a58fdfb2005-02-08 07:50:40 +00003085 pExpr->iAgg = i;
drh13449892005-09-07 21:22:45 +00003086 pExpr->pAggInfo = pAggInfo;
danielk1977a58fdfb2005-02-08 07:50:40 +00003087 return 1;
drh22827922000-06-06 17:27:05 +00003088 }
drh22827922000-06-06 17:27:05 +00003089 }
3090 }
drh13449892005-09-07 21:22:45 +00003091
3092 /* Recursively walk subqueries looking for TK_COLUMN nodes that need
3093 ** to be changed to TK_AGG_COLUMN. But increment nDepth so that
3094 ** TK_AGG_FUNCTION nodes in subqueries will be unchanged.
3095 */
danielk1977a58fdfb2005-02-08 07:50:40 +00003096 if( pExpr->pSelect ){
3097 pNC->nDepth++;
3098 walkSelectExpr(pExpr->pSelect, analyzeAggregate, pNC);
3099 pNC->nDepth--;
3100 }
drh626a8792005-01-17 22:08:19 +00003101 return 0;
3102}
3103
3104/*
3105** Analyze the given expression looking for aggregate functions and
3106** for variables that need to be added to the pParse->aAgg[] array.
3107** Make additional entries to the pParse->aAgg[] array as necessary.
3108**
3109** This routine should only be called after the expression has been
3110** analyzed by sqlite3ExprResolveNames().
drh626a8792005-01-17 22:08:19 +00003111*/
drhd2b3e232008-01-23 14:51:49 +00003112void sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
danielk1977a58fdfb2005-02-08 07:50:40 +00003113 walkExprTree(pExpr, analyzeAggregate, pNC);
drh22827922000-06-06 17:27:05 +00003114}
drh5d9a4af2005-08-30 00:54:01 +00003115
3116/*
3117** Call sqlite3ExprAnalyzeAggregates() for every expression in an
3118** expression list. Return the number of errors.
3119**
3120** If an error is found, the analysis is cut short.
3121*/
drhd2b3e232008-01-23 14:51:49 +00003122void sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){
drh5d9a4af2005-08-30 00:54:01 +00003123 struct ExprList_item *pItem;
3124 int i;
drh5d9a4af2005-08-30 00:54:01 +00003125 if( pList ){
drhd2b3e232008-01-23 14:51:49 +00003126 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
3127 sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
drh5d9a4af2005-08-30 00:54:01 +00003128 }
3129 }
drh5d9a4af2005-08-30 00:54:01 +00003130}
drh892d3172008-01-10 03:46:36 +00003131
3132/*
3133** Allocate or deallocate temporary use registers during code generation.
3134*/
3135int sqlite3GetTempReg(Parse *pParse){
drhe55cbd72008-03-31 23:48:03 +00003136 int i, r;
3137 if( pParse->nTempReg==0 ){
drh892d3172008-01-10 03:46:36 +00003138 return ++pParse->nMem;
3139 }
drhe55cbd72008-03-31 23:48:03 +00003140 for(i=0; i<pParse->nTempReg; i++){
3141 r = pParse->aTempReg[i];
3142 if( usedAsColumnCache(pParse, r, r) ) continue;
3143 }
3144 if( i>=pParse->nTempReg ){
3145 return ++pParse->nMem;
3146 }
3147 while( i<pParse->nTempReg-1 ){
3148 pParse->aTempReg[i] = pParse->aTempReg[i+1];
3149 }
3150 pParse->nTempReg--;
3151 return r;
drh892d3172008-01-10 03:46:36 +00003152}
3153void sqlite3ReleaseTempReg(Parse *pParse, int iReg){
drh2dcef112008-01-12 19:03:48 +00003154 if( iReg && pParse->nTempReg<ArraySize(pParse->aTempReg) ){
3155 assert( iReg>0 );
drh892d3172008-01-10 03:46:36 +00003156 pParse->aTempReg[pParse->nTempReg++] = iReg;
3157 }
3158}
3159
3160/*
3161** Allocate or deallocate a block of nReg consecutive registers
3162*/
3163int sqlite3GetTempRange(Parse *pParse, int nReg){
drhe55cbd72008-03-31 23:48:03 +00003164 int i, n;
3165 i = pParse->iRangeReg;
3166 n = pParse->nRangeReg;
3167 if( nReg<=n && !usedAsColumnCache(pParse, i, i+n-1) ){
drh892d3172008-01-10 03:46:36 +00003168 pParse->iRangeReg += nReg;
3169 pParse->nRangeReg -= nReg;
3170 }else{
3171 i = pParse->nMem+1;
3172 pParse->nMem += nReg;
3173 }
3174 return i;
3175}
3176void sqlite3ReleaseTempRange(Parse *pParse, int iReg, int nReg){
3177 if( nReg>pParse->nRangeReg ){
3178 pParse->nRangeReg = nReg;
3179 pParse->iRangeReg = iReg;
3180 }
3181}