blob: 88636411e6476fa924b271759cb4e7a07725ec45 [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**
drh2d401ab2008-01-10 23:50:11 +000015** $Id: expr.c,v 1.342 2008/01/10 23:50:11 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);
225 return addr;
drhbe5c89a2004-07-26 00:31:09 +0000226}
227
228/*
drha76b5df2002-02-23 02:32:10 +0000229** Construct a new expression node and return a pointer to it. Memory
drh17435752007-08-16 04:30:38 +0000230** for this node is obtained from sqlite3_malloc(). The calling function
drha76b5df2002-02-23 02:32:10 +0000231** is responsible for making sure the node eventually gets freed.
232*/
drh17435752007-08-16 04:30:38 +0000233Expr *sqlite3Expr(
danielk1977a1644fd2007-08-29 12:31:25 +0000234 sqlite3 *db, /* Handle for sqlite3DbMallocZero() (may be null) */
drh17435752007-08-16 04:30:38 +0000235 int op, /* Expression opcode */
236 Expr *pLeft, /* Left operand */
237 Expr *pRight, /* Right operand */
238 const Token *pToken /* Argument token */
239){
drha76b5df2002-02-23 02:32:10 +0000240 Expr *pNew;
danielk1977a1644fd2007-08-29 12:31:25 +0000241 pNew = sqlite3DbMallocZero(db, sizeof(Expr));
drha76b5df2002-02-23 02:32:10 +0000242 if( pNew==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000243 /* When malloc fails, delete pLeft and pRight. Expressions passed to
244 ** this function must always be allocated with sqlite3Expr() for this
245 ** reason.
246 */
247 sqlite3ExprDelete(pLeft);
248 sqlite3ExprDelete(pRight);
drha76b5df2002-02-23 02:32:10 +0000249 return 0;
250 }
251 pNew->op = op;
252 pNew->pLeft = pLeft;
253 pNew->pRight = pRight;
danielk1977a58fdfb2005-02-08 07:50:40 +0000254 pNew->iAgg = -1;
drha76b5df2002-02-23 02:32:10 +0000255 if( pToken ){
drh4b59ab52002-08-24 18:24:51 +0000256 assert( pToken->dyn==0 );
drh145716b2004-09-24 12:24:06 +0000257 pNew->span = pNew->token = *pToken;
drha34001c2007-02-02 12:44:37 +0000258 }else if( pLeft ){
259 if( pRight ){
260 sqlite3ExprSpan(pNew, &pLeft->span, &pRight->span);
drh5ffb3ac2007-04-18 17:07:57 +0000261 if( pRight->flags & EP_ExpCollate ){
drha34001c2007-02-02 12:44:37 +0000262 pNew->flags |= EP_ExpCollate;
263 pNew->pColl = pRight->pColl;
264 }
265 }
drh5ffb3ac2007-04-18 17:07:57 +0000266 if( pLeft->flags & EP_ExpCollate ){
drha34001c2007-02-02 12:44:37 +0000267 pNew->flags |= EP_ExpCollate;
268 pNew->pColl = pLeft->pColl;
269 }
drha76b5df2002-02-23 02:32:10 +0000270 }
danielk1977fc976062007-05-10 10:46:56 +0000271
272 sqlite3ExprSetHeight(pNew);
drha76b5df2002-02-23 02:32:10 +0000273 return pNew;
274}
275
276/*
drh17435752007-08-16 04:30:38 +0000277** Works like sqlite3Expr() except that it takes an extra Parse*
278** argument and notifies the associated connection object if malloc fails.
drh206f3d92006-07-11 13:15:08 +0000279*/
drh17435752007-08-16 04:30:38 +0000280Expr *sqlite3PExpr(
281 Parse *pParse, /* Parsing context */
282 int op, /* Expression opcode */
283 Expr *pLeft, /* Left operand */
284 Expr *pRight, /* Right operand */
285 const Token *pToken /* Argument token */
286){
danielk1977a1644fd2007-08-29 12:31:25 +0000287 return sqlite3Expr(pParse->db, op, pLeft, pRight, pToken);
drh206f3d92006-07-11 13:15:08 +0000288}
289
290/*
drh4e0cff62004-11-05 05:10:28 +0000291** When doing a nested parse, you can include terms in an expression
292** that look like this: #0 #1 #2 ... These terms refer to elements
drh288d37f2005-06-22 08:48:06 +0000293** on the stack. "#0" means the top of the stack.
294** "#1" means the next down on the stack. And so forth.
drh4e0cff62004-11-05 05:10:28 +0000295**
296** This routine is called by the parser to deal with on of those terms.
297** It immediately generates code to store the value in a memory location.
298** The returns an expression that will code to extract the value from
299** that memory location as needed.
300*/
301Expr *sqlite3RegisterExpr(Parse *pParse, Token *pToken){
302 Vdbe *v = pParse->pVdbe;
303 Expr *p;
304 int depth;
drh4e0cff62004-11-05 05:10:28 +0000305 if( pParse->nested==0 ){
306 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", pToken);
danielk1977a1644fd2007-08-29 12:31:25 +0000307 return sqlite3PExpr(pParse, TK_NULL, 0, 0, 0);
drh4e0cff62004-11-05 05:10:28 +0000308 }
drhbb7ac002005-08-12 22:58:53 +0000309 if( v==0 ) return 0;
danielk1977a1644fd2007-08-29 12:31:25 +0000310 p = sqlite3PExpr(pParse, TK_REGISTER, 0, 0, pToken);
drh73c42a12004-11-20 18:13:10 +0000311 if( p==0 ){
312 return 0; /* Malloc failed */
313 }
drh2646da72005-12-09 20:02:05 +0000314 depth = atoi((char*)&pToken->z[1]);
drh0a07c102008-01-03 18:03:08 +0000315 p->iTable = ++pParse->nMem;
drhb1fdb2a2008-01-05 04:06:03 +0000316 sqlite3VdbeAddOp2(v, OP_Copy, -depth, p->iTable);
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]);
drhfa6bc002004-09-07 16:19:52 +0000407 if( i<1 || i>SQLITE_MAX_VARIABLE_NUMBER ){
408 sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
409 SQLITE_MAX_VARIABLE_NUMBER);
410 }
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 }
danielk1977832b2662007-05-09 11:37:22 +0000447 if( !pParse->nErr && pParse->nVar>SQLITE_MAX_VARIABLE_NUMBER ){
448 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,
703 int iLimit,
704 const char *zObject
705){
danielk1977b4fc6792007-05-08 18:04:46 +0000706 if( pEList && pEList->nExpr>iLimit ){
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
danielk1977e6a58a42007-08-31 17:42:48 +0000712#if defined(SQLITE_TEST) || SQLITE_MAX_EXPR_DEPTH>0
danielk1977fc976062007-05-10 10:46:56 +0000713/* The following three functions, heightOfExpr(), heightOfExprList()
714** and heightOfSelect(), are used to determine the maximum height
715** of any expression tree referenced by the structure passed as the
716** first argument.
717**
718** If this maximum height is greater than the current value pointed
719** to by pnHeight, the second parameter, then set *pnHeight to that
720** value.
721*/
722static void heightOfExpr(Expr *p, int *pnHeight){
723 if( p ){
724 if( p->nHeight>*pnHeight ){
725 *pnHeight = p->nHeight;
726 }
727 }
728}
729static void heightOfExprList(ExprList *p, int *pnHeight){
730 if( p ){
731 int i;
732 for(i=0; i<p->nExpr; i++){
733 heightOfExpr(p->a[i].pExpr, pnHeight);
734 }
735 }
736}
737static void heightOfSelect(Select *p, int *pnHeight){
738 if( p ){
739 heightOfExpr(p->pWhere, pnHeight);
740 heightOfExpr(p->pHaving, pnHeight);
741 heightOfExpr(p->pLimit, pnHeight);
742 heightOfExpr(p->pOffset, pnHeight);
743 heightOfExprList(p->pEList, pnHeight);
744 heightOfExprList(p->pGroupBy, pnHeight);
745 heightOfExprList(p->pOrderBy, pnHeight);
746 heightOfSelect(p->pPrior, pnHeight);
747 }
748}
749
750/*
751** Set the Expr.nHeight variable in the structure passed as an
752** argument. An expression with no children, Expr.pList or
753** Expr.pSelect member has a height of 1. Any other expression
754** has a height equal to the maximum height of any other
755** referenced Expr plus one.
756*/
757void sqlite3ExprSetHeight(Expr *p){
758 int nHeight = 0;
759 heightOfExpr(p->pLeft, &nHeight);
760 heightOfExpr(p->pRight, &nHeight);
761 heightOfExprList(p->pList, &nHeight);
762 heightOfSelect(p->pSelect, &nHeight);
763 p->nHeight = nHeight + 1;
764}
765
766/*
767** Return the maximum height of any expression tree referenced
768** by the select statement passed as an argument.
769*/
770int sqlite3SelectExprHeight(Select *p){
771 int nHeight = 0;
772 heightOfSelect(p, &nHeight);
773 return nHeight;
774}
775#endif
776
danielk19777a15a4b2007-05-08 17:54:43 +0000777/*
drha76b5df2002-02-23 02:32:10 +0000778** Delete an entire expression list.
779*/
danielk19774adee202004-05-08 08:23:19 +0000780void sqlite3ExprListDelete(ExprList *pList){
drha76b5df2002-02-23 02:32:10 +0000781 int i;
drhbe5c89a2004-07-26 00:31:09 +0000782 struct ExprList_item *pItem;
drha76b5df2002-02-23 02:32:10 +0000783 if( pList==0 ) return;
drh1bdd9b52004-04-23 17:04:44 +0000784 assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
785 assert( pList->nExpr<=pList->nAlloc );
drhbe5c89a2004-07-26 00:31:09 +0000786 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
787 sqlite3ExprDelete(pItem->pExpr);
drh17435752007-08-16 04:30:38 +0000788 sqlite3_free(pItem->zName);
drha76b5df2002-02-23 02:32:10 +0000789 }
drh17435752007-08-16 04:30:38 +0000790 sqlite3_free(pList->a);
791 sqlite3_free(pList);
drha76b5df2002-02-23 02:32:10 +0000792}
793
794/*
drh626a8792005-01-17 22:08:19 +0000795** Walk an expression tree. Call xFunc for each node visited.
drh73b211a2005-01-18 04:00:42 +0000796**
drh626a8792005-01-17 22:08:19 +0000797** The return value from xFunc determines whether the tree walk continues.
798** 0 means continue walking the tree. 1 means do not walk children
799** of the current node but continue with siblings. 2 means abandon
800** the tree walk completely.
801**
802** The return value from this routine is 1 to abandon the tree walk
803** and 0 to continue.
drh87abf5c2005-08-25 12:45:04 +0000804**
805** NOTICE: This routine does *not* descend into subqueries.
drh626a8792005-01-17 22:08:19 +0000806*/
danielk1977a58fdfb2005-02-08 07:50:40 +0000807static int walkExprList(ExprList *, int (*)(void *, Expr*), void *);
drh626a8792005-01-17 22:08:19 +0000808static int walkExprTree(Expr *pExpr, int (*xFunc)(void*,Expr*), void *pArg){
drh626a8792005-01-17 22:08:19 +0000809 int rc;
810 if( pExpr==0 ) return 0;
811 rc = (*xFunc)(pArg, pExpr);
812 if( rc==0 ){
813 if( walkExprTree(pExpr->pLeft, xFunc, pArg) ) return 1;
814 if( walkExprTree(pExpr->pRight, xFunc, pArg) ) return 1;
danielk1977a58fdfb2005-02-08 07:50:40 +0000815 if( walkExprList(pExpr->pList, xFunc, pArg) ) return 1;
drh626a8792005-01-17 22:08:19 +0000816 }
817 return rc>1;
818}
819
820/*
danielk1977a58fdfb2005-02-08 07:50:40 +0000821** Call walkExprTree() for every expression in list p.
822*/
823static int walkExprList(ExprList *p, int (*xFunc)(void *, Expr*), void *pArg){
824 int i;
825 struct ExprList_item *pItem;
826 if( !p ) return 0;
827 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
828 if( walkExprTree(pItem->pExpr, xFunc, pArg) ) return 1;
829 }
830 return 0;
831}
832
833/*
834** Call walkExprTree() for every expression in Select p, not including
835** expressions that are part of sub-selects in any FROM clause or the LIMIT
836** or OFFSET expressions..
837*/
838static int walkSelectExpr(Select *p, int (*xFunc)(void *, Expr*), void *pArg){
839 walkExprList(p->pEList, xFunc, pArg);
840 walkExprTree(p->pWhere, xFunc, pArg);
841 walkExprList(p->pGroupBy, xFunc, pArg);
842 walkExprTree(p->pHaving, xFunc, pArg);
843 walkExprList(p->pOrderBy, xFunc, pArg);
danielk197715d79822007-05-15 07:00:34 +0000844 if( p->pPrior ){
845 walkSelectExpr(p->pPrior, xFunc, pArg);
846 }
danielk1977a58fdfb2005-02-08 07:50:40 +0000847 return 0;
848}
849
850
851/*
drh626a8792005-01-17 22:08:19 +0000852** This routine is designed as an xFunc for walkExprTree().
853**
854** pArg is really a pointer to an integer. If we can tell by looking
drh73b211a2005-01-18 04:00:42 +0000855** at pExpr that the expression that contains pExpr is not a constant
856** expression, then set *pArg to 0 and return 2 to abandon the tree walk.
857** If pExpr does does not disqualify the expression from being a constant
858** then do nothing.
859**
860** After walking the whole tree, if no nodes are found that disqualify
861** the expression as constant, then we assume the whole expression
862** is constant. See sqlite3ExprIsConstant() for additional information.
drh626a8792005-01-17 22:08:19 +0000863*/
864static int exprNodeIsConstant(void *pArg, Expr *pExpr){
drh0a168372007-06-08 00:20:47 +0000865 int *pN = (int*)pArg;
866
867 /* If *pArg is 3 then any term of the expression that comes from
868 ** the ON or USING clauses of a join disqualifies the expression
869 ** from being considered constant. */
870 if( (*pN)==3 && ExprHasAnyProperty(pExpr, EP_FromJoin) ){
871 *pN = 0;
872 return 2;
873 }
874
drh626a8792005-01-17 22:08:19 +0000875 switch( pExpr->op ){
drheb55bd22005-06-30 17:04:21 +0000876 /* Consider functions to be constant if all their arguments are constant
877 ** and *pArg==2 */
878 case TK_FUNCTION:
drh0a168372007-06-08 00:20:47 +0000879 if( (*pN)==2 ) return 0;
drheb55bd22005-06-30 17:04:21 +0000880 /* Fall through */
drh626a8792005-01-17 22:08:19 +0000881 case TK_ID:
882 case TK_COLUMN:
883 case TK_DOT:
884 case TK_AGG_FUNCTION:
drh13449892005-09-07 21:22:45 +0000885 case TK_AGG_COLUMN:
drhfe2093d2005-01-20 22:48:47 +0000886#ifndef SQLITE_OMIT_SUBQUERY
887 case TK_SELECT:
888 case TK_EXISTS:
889#endif
drh0a168372007-06-08 00:20:47 +0000890 *pN = 0;
drh626a8792005-01-17 22:08:19 +0000891 return 2;
drh87abf5c2005-08-25 12:45:04 +0000892 case TK_IN:
893 if( pExpr->pSelect ){
drh0a168372007-06-08 00:20:47 +0000894 *pN = 0;
drh87abf5c2005-08-25 12:45:04 +0000895 return 2;
896 }
drh626a8792005-01-17 22:08:19 +0000897 default:
898 return 0;
899 }
900}
901
902/*
drhfef52082000-06-06 01:50:43 +0000903** Walk an expression tree. Return 1 if the expression is constant
drheb55bd22005-06-30 17:04:21 +0000904** and 0 if it involves variables or function calls.
drh23989372002-05-21 13:43:04 +0000905**
906** For the purposes of this function, a double-quoted string (ex: "abc")
907** is considered a variable but a single-quoted string (ex: 'abc') is
908** a constant.
drhfef52082000-06-06 01:50:43 +0000909*/
danielk19774adee202004-05-08 08:23:19 +0000910int sqlite3ExprIsConstant(Expr *p){
drh626a8792005-01-17 22:08:19 +0000911 int isConst = 1;
912 walkExprTree(p, exprNodeIsConstant, &isConst);
913 return isConst;
drhfef52082000-06-06 01:50:43 +0000914}
915
916/*
drheb55bd22005-06-30 17:04:21 +0000917** Walk an expression tree. Return 1 if the expression is constant
drh0a168372007-06-08 00:20:47 +0000918** that does no originate from the ON or USING clauses of a join.
919** Return 0 if it involves variables or function calls or terms from
920** an ON or USING clause.
921*/
922int sqlite3ExprIsConstantNotJoin(Expr *p){
923 int isConst = 3;
924 walkExprTree(p, exprNodeIsConstant, &isConst);
925 return isConst!=0;
926}
927
928/*
929** Walk an expression tree. Return 1 if the expression is constant
drheb55bd22005-06-30 17:04:21 +0000930** or a function call with constant arguments. Return and 0 if there
931** are any variables.
932**
933** For the purposes of this function, a double-quoted string (ex: "abc")
934** is considered a variable but a single-quoted string (ex: 'abc') is
935** a constant.
936*/
937int sqlite3ExprIsConstantOrFunction(Expr *p){
938 int isConst = 2;
939 walkExprTree(p, exprNodeIsConstant, &isConst);
940 return isConst!=0;
941}
942
943/*
drh73b211a2005-01-18 04:00:42 +0000944** If the expression p codes a constant integer that is small enough
drh202b2df2004-01-06 01:13:46 +0000945** to fit in a 32-bit integer, return 1 and put the value of the integer
946** in *pValue. If the expression is not an integer or if it is too big
947** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
drhe4de1fe2002-06-02 16:09:01 +0000948*/
danielk19774adee202004-05-08 08:23:19 +0000949int sqlite3ExprIsInteger(Expr *p, int *pValue){
drhe4de1fe2002-06-02 16:09:01 +0000950 switch( p->op ){
951 case TK_INTEGER: {
drh2646da72005-12-09 20:02:05 +0000952 if( sqlite3GetInt32((char*)p->token.z, pValue) ){
drh202b2df2004-01-06 01:13:46 +0000953 return 1;
954 }
955 break;
drhe4de1fe2002-06-02 16:09:01 +0000956 }
drh4b59ab52002-08-24 18:24:51 +0000957 case TK_UPLUS: {
danielk19774adee202004-05-08 08:23:19 +0000958 return sqlite3ExprIsInteger(p->pLeft, pValue);
drh4b59ab52002-08-24 18:24:51 +0000959 }
drhe4de1fe2002-06-02 16:09:01 +0000960 case TK_UMINUS: {
961 int v;
danielk19774adee202004-05-08 08:23:19 +0000962 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
drhe4de1fe2002-06-02 16:09:01 +0000963 *pValue = -v;
964 return 1;
965 }
966 break;
967 }
968 default: break;
969 }
970 return 0;
971}
972
973/*
drhc4a3c772001-04-04 11:48:57 +0000974** Return TRUE if the given string is a row-id column name.
975*/
danielk19774adee202004-05-08 08:23:19 +0000976int sqlite3IsRowid(const char *z){
977 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
978 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
979 if( sqlite3StrICmp(z, "OID")==0 ) return 1;
drhc4a3c772001-04-04 11:48:57 +0000980 return 0;
981}
982
983/*
drh8141f612004-01-25 22:44:58 +0000984** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
985** that name in the set of source tables in pSrcList and make the pExpr
986** expression node refer back to that source column. The following changes
987** are made to pExpr:
988**
989** pExpr->iDb Set the index in db->aDb[] of the database holding
990** the table.
991** pExpr->iTable Set to the cursor number for the table obtained
992** from pSrcList.
993** pExpr->iColumn Set to the column number within the table.
drh8141f612004-01-25 22:44:58 +0000994** pExpr->op Set to TK_COLUMN.
995** pExpr->pLeft Any expression this points to is deleted
996** pExpr->pRight Any expression this points to is deleted.
997**
998** The pDbToken is the name of the database (the "X"). This value may be
999** NULL meaning that name is of the form Y.Z or Z. Any available database
1000** can be used. The pTableToken is the name of the table (the "Y"). This
1001** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it
1002** means that the form of the name is Z and that columns from any table
1003** can be used.
1004**
1005** If the name cannot be resolved unambiguously, leave an error message
1006** in pParse and return non-zero. Return zero on success.
1007*/
1008static int lookupName(
drhffe07b22005-11-03 00:41:17 +00001009 Parse *pParse, /* The parsing context */
drh8141f612004-01-25 22:44:58 +00001010 Token *pDbToken, /* Name of the database containing table, or NULL */
1011 Token *pTableToken, /* Name of table containing column, or NULL */
1012 Token *pColumnToken, /* Name of the column. */
drh626a8792005-01-17 22:08:19 +00001013 NameContext *pNC, /* The name context used to resolve the name */
drh8141f612004-01-25 22:44:58 +00001014 Expr *pExpr /* Make this EXPR node point to the selected column */
1015){
1016 char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */
1017 char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */
1018 char *zCol = 0; /* Name of the column. The "Z" */
1019 int i, j; /* Loop counters */
1020 int cnt = 0; /* Number of matching column names */
1021 int cntTab = 0; /* Number of matching table names */
drh626a8792005-01-17 22:08:19 +00001022 sqlite3 *db = pParse->db; /* The database */
drh51669862004-12-18 18:40:26 +00001023 struct SrcList_item *pItem; /* Use for looping over pSrcList items */
1024 struct SrcList_item *pMatch = 0; /* The matching pSrcList item */
drh73b211a2005-01-18 04:00:42 +00001025 NameContext *pTopNC = pNC; /* First namecontext in the list */
drh728b5772007-09-18 15:55:07 +00001026 Schema *pSchema = 0; /* Schema of the expression */
drh8141f612004-01-25 22:44:58 +00001027
1028 assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
drh17435752007-08-16 04:30:38 +00001029 zDb = sqlite3NameFromToken(db, pDbToken);
1030 zTab = sqlite3NameFromToken(db, pTableToken);
1031 zCol = sqlite3NameFromToken(db, pColumnToken);
1032 if( db->mallocFailed ){
danielk1977d5d56522005-03-16 12:15:20 +00001033 goto lookupname_end;
drh8141f612004-01-25 22:44:58 +00001034 }
drh8141f612004-01-25 22:44:58 +00001035
1036 pExpr->iTable = -1;
drh626a8792005-01-17 22:08:19 +00001037 while( pNC && cnt==0 ){
drhffe07b22005-11-03 00:41:17 +00001038 ExprList *pEList;
drh626a8792005-01-17 22:08:19 +00001039 SrcList *pSrcList = pNC->pSrcList;
drh8141f612004-01-25 22:44:58 +00001040
danielk1977b3bce662005-01-29 08:32:43 +00001041 if( pSrcList ){
1042 for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
drh43617e92006-03-06 20:55:46 +00001043 Table *pTab;
1044 int iDb;
danielk1977b3bce662005-01-29 08:32:43 +00001045 Column *pCol;
1046
drh43617e92006-03-06 20:55:46 +00001047 pTab = pItem->pTab;
1048 assert( pTab!=0 );
1049 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
danielk1977b3bce662005-01-29 08:32:43 +00001050 assert( pTab->nCol>0 );
1051 if( zTab ){
1052 if( pItem->zAlias ){
1053 char *zTabName = pItem->zAlias;
1054 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
1055 }else{
1056 char *zTabName = pTab->zName;
1057 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
danielk1977da184232006-01-05 11:34:32 +00001058 if( zDb!=0 && sqlite3StrICmp(db->aDb[iDb].zName, zDb)!=0 ){
danielk1977b3bce662005-01-29 08:32:43 +00001059 continue;
1060 }
drh626a8792005-01-17 22:08:19 +00001061 }
drh8141f612004-01-25 22:44:58 +00001062 }
danielk1977b3bce662005-01-29 08:32:43 +00001063 if( 0==(cntTab++) ){
drh626a8792005-01-17 22:08:19 +00001064 pExpr->iTable = pItem->iCursor;
drh728b5772007-09-18 15:55:07 +00001065 pSchema = pTab->pSchema;
danielk1977b3bce662005-01-29 08:32:43 +00001066 pMatch = pItem;
1067 }
1068 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
1069 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
danielk1977b3bf5562006-01-10 17:58:23 +00001070 const char *zColl = pTab->aCol[j].zColl;
drh873fac02005-06-06 17:11:46 +00001071 IdList *pUsing;
danielk1977b3bce662005-01-29 08:32:43 +00001072 cnt++;
1073 pExpr->iTable = pItem->iCursor;
1074 pMatch = pItem;
drh728b5772007-09-18 15:55:07 +00001075 pSchema = pTab->pSchema;
danielk1977b3bce662005-01-29 08:32:43 +00001076 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
1077 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
1078 pExpr->affinity = pTab->aCol[j].affinity;
drh8b4c40d2007-02-01 23:02:45 +00001079 if( (pExpr->flags & EP_ExpCollate)==0 ){
1080 pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0);
1081 }
drh61dfc312006-12-16 16:25:15 +00001082 if( i<pSrcList->nSrc-1 ){
1083 if( pItem[1].jointype & JT_NATURAL ){
1084 /* If this match occurred in the left table of a natural join,
1085 ** then skip the right table to avoid a duplicate match */
1086 pItem++;
1087 i++;
1088 }else if( (pUsing = pItem[1].pUsing)!=0 ){
1089 /* If this match occurs on a column that is in the USING clause
1090 ** of a join, skip the search of the right table of the join
1091 ** to avoid a duplicate match there. */
1092 int k;
1093 for(k=0; k<pUsing->nId; k++){
1094 if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ){
1095 pItem++;
1096 i++;
1097 break;
1098 }
drh873fac02005-06-06 17:11:46 +00001099 }
1100 }
1101 }
danielk1977b3bce662005-01-29 08:32:43 +00001102 break;
1103 }
drh8141f612004-01-25 22:44:58 +00001104 }
1105 }
1106 }
drh626a8792005-01-17 22:08:19 +00001107
1108#ifndef SQLITE_OMIT_TRIGGER
1109 /* If we have not already resolved the name, then maybe
1110 ** it is a new.* or old.* trigger argument reference
1111 */
1112 if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
1113 TriggerStack *pTriggerStack = pParse->trigStack;
1114 Table *pTab = 0;
danielk19778f2c54e2008-01-01 19:02:09 +00001115 u32 *piColMask;
drh626a8792005-01-17 22:08:19 +00001116 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){
1117 pExpr->iTable = pTriggerStack->newIdx;
1118 assert( pTriggerStack->pTab );
1119 pTab = pTriggerStack->pTab;
danielk19778f2c54e2008-01-01 19:02:09 +00001120 piColMask = &(pTriggerStack->newColMask);
drh626a8792005-01-17 22:08:19 +00001121 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab)==0 ){
1122 pExpr->iTable = pTriggerStack->oldIdx;
1123 assert( pTriggerStack->pTab );
1124 pTab = pTriggerStack->pTab;
danielk19778f2c54e2008-01-01 19:02:09 +00001125 piColMask = &(pTriggerStack->oldColMask);
drh626a8792005-01-17 22:08:19 +00001126 }
1127
1128 if( pTab ){
danielk1977f0113002006-01-24 12:09:17 +00001129 int iCol;
drh626a8792005-01-17 22:08:19 +00001130 Column *pCol = pTab->aCol;
1131
drh728b5772007-09-18 15:55:07 +00001132 pSchema = pTab->pSchema;
drh626a8792005-01-17 22:08:19 +00001133 cntTab++;
danielk1977f0113002006-01-24 12:09:17 +00001134 for(iCol=0; iCol < pTab->nCol; iCol++, pCol++) {
drh626a8792005-01-17 22:08:19 +00001135 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
danielk1977f0113002006-01-24 12:09:17 +00001136 const char *zColl = pTab->aCol[iCol].zColl;
drh626a8792005-01-17 22:08:19 +00001137 cnt++;
danielk1977f0113002006-01-24 12:09:17 +00001138 pExpr->iColumn = iCol==pTab->iPKey ? -1 : iCol;
1139 pExpr->affinity = pTab->aCol[iCol].affinity;
drh8b4c40d2007-02-01 23:02:45 +00001140 if( (pExpr->flags & EP_ExpCollate)==0 ){
1141 pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0);
1142 }
danielk1977aee18ef2005-03-09 12:26:50 +00001143 pExpr->pTab = pTab;
danielk19778f2c54e2008-01-01 19:02:09 +00001144 if( iCol>=0 ){
1145 *piColMask |= ((u32)1<<iCol) | (iCol>=32?0xffffffff:0);
1146 }
drh626a8792005-01-17 22:08:19 +00001147 break;
1148 }
1149 }
1150 }
1151 }
drhb7f91642004-10-31 02:22:47 +00001152#endif /* !defined(SQLITE_OMIT_TRIGGER) */
drh8141f612004-01-25 22:44:58 +00001153
drh626a8792005-01-17 22:08:19 +00001154 /*
1155 ** Perhaps the name is a reference to the ROWID
1156 */
1157 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
1158 cnt = 1;
1159 pExpr->iColumn = -1;
drh8a512562005-11-14 22:29:05 +00001160 pExpr->affinity = SQLITE_AFF_INTEGER;
drh626a8792005-01-17 22:08:19 +00001161 }
drh8141f612004-01-25 22:44:58 +00001162
drh626a8792005-01-17 22:08:19 +00001163 /*
1164 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
1165 ** might refer to an result-set alias. This happens, for example, when
1166 ** we are resolving names in the WHERE clause of the following command:
1167 **
1168 ** SELECT a+b AS x FROM table WHERE x<10;
1169 **
1170 ** In cases like this, replace pExpr with a copy of the expression that
1171 ** forms the result set entry ("a+b" in the example) and return immediately.
1172 ** Note that the expression in the result set should have already been
1173 ** resolved by the time the WHERE clause is resolved.
1174 */
drhffe07b22005-11-03 00:41:17 +00001175 if( cnt==0 && (pEList = pNC->pEList)!=0 && zTab==0 ){
drh626a8792005-01-17 22:08:19 +00001176 for(j=0; j<pEList->nExpr; j++){
1177 char *zAs = pEList->a[j].zName;
1178 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
drh36379e92007-07-23 22:51:15 +00001179 Expr *pDup, *pOrig;
drh626a8792005-01-17 22:08:19 +00001180 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
drh4f07e5f2007-05-14 11:34:46 +00001181 assert( pExpr->pList==0 );
1182 assert( pExpr->pSelect==0 );
drh36379e92007-07-23 22:51:15 +00001183 pOrig = pEList->a[j].pExpr;
1184 if( !pNC->allowAgg && ExprHasProperty(pOrig, EP_Agg) ){
1185 sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs);
drh17435752007-08-16 04:30:38 +00001186 sqlite3_free(zCol);
drh36379e92007-07-23 22:51:15 +00001187 return 2;
1188 }
danielk19771e536952007-08-16 10:09:01 +00001189 pDup = sqlite3ExprDup(db, pOrig);
drh4f07e5f2007-05-14 11:34:46 +00001190 if( pExpr->flags & EP_ExpCollate ){
1191 pDup->pColl = pExpr->pColl;
1192 pDup->flags |= EP_ExpCollate;
1193 }
drh17435752007-08-16 04:30:38 +00001194 if( pExpr->span.dyn ) sqlite3_free((char*)pExpr->span.z);
1195 if( pExpr->token.dyn ) sqlite3_free((char*)pExpr->token.z);
drh4f07e5f2007-05-14 11:34:46 +00001196 memcpy(pExpr, pDup, sizeof(*pExpr));
drh17435752007-08-16 04:30:38 +00001197 sqlite3_free(pDup);
drh15ccce12005-05-23 15:06:39 +00001198 cnt = 1;
danielk1977c9cf6e32007-06-25 16:29:33 +00001199 pMatch = 0;
drh626a8792005-01-17 22:08:19 +00001200 assert( zTab==0 && zDb==0 );
drh15ccce12005-05-23 15:06:39 +00001201 goto lookupname_end_2;
drh626a8792005-01-17 22:08:19 +00001202 }
1203 }
1204 }
1205
1206 /* Advance to the next name context. The loop will exit when either
1207 ** we have a match (cnt>0) or when we run out of name contexts.
1208 */
1209 if( cnt==0 ){
1210 pNC = pNC->pNext;
1211 }
drh8141f612004-01-25 22:44:58 +00001212 }
1213
1214 /*
1215 ** If X and Y are NULL (in other words if only the column name Z is
1216 ** supplied) and the value of Z is enclosed in double-quotes, then
1217 ** Z is a string literal if it doesn't match any column names. In that
1218 ** case, we need to return right away and not make any changes to
1219 ** pExpr.
drh15ccce12005-05-23 15:06:39 +00001220 **
1221 ** Because no reference was made to outer contexts, the pNC->nRef
1222 ** fields are not changed in any context.
drh8141f612004-01-25 22:44:58 +00001223 */
1224 if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
drh17435752007-08-16 04:30:38 +00001225 sqlite3_free(zCol);
drh8141f612004-01-25 22:44:58 +00001226 return 0;
1227 }
1228
1229 /*
1230 ** cnt==0 means there was not match. cnt>1 means there were two or
1231 ** more matches. Either way, we have an error.
1232 */
1233 if( cnt!=1 ){
1234 char *z = 0;
1235 char *zErr;
1236 zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s";
1237 if( zDb ){
drhf93339d2006-01-03 15:16:26 +00001238 sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, (char*)0);
drh8141f612004-01-25 22:44:58 +00001239 }else if( zTab ){
drhf93339d2006-01-03 15:16:26 +00001240 sqlite3SetString(&z, zTab, ".", zCol, (char*)0);
drh8141f612004-01-25 22:44:58 +00001241 }else{
drh17435752007-08-16 04:30:38 +00001242 z = sqlite3StrDup(zCol);
drh8141f612004-01-25 22:44:58 +00001243 }
danielk1977a1644fd2007-08-29 12:31:25 +00001244 if( z ){
1245 sqlite3ErrorMsg(pParse, zErr, z);
1246 sqlite3_free(z);
1247 pTopNC->nErr++;
1248 }else{
1249 db->mallocFailed = 1;
1250 }
drh8141f612004-01-25 22:44:58 +00001251 }
1252
drh51669862004-12-18 18:40:26 +00001253 /* If a column from a table in pSrcList is referenced, then record
1254 ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes
1255 ** bit 0 to be set. Column 1 sets bit 1. And so forth. If the
1256 ** column number is greater than the number of bits in the bitmask
1257 ** then set the high-order bit of the bitmask.
1258 */
1259 if( pExpr->iColumn>=0 && pMatch!=0 ){
1260 int n = pExpr->iColumn;
1261 if( n>=sizeof(Bitmask)*8 ){
1262 n = sizeof(Bitmask)*8-1;
1263 }
1264 assert( pMatch->iCursor==pExpr->iTable );
drhca83ac52007-02-01 01:40:44 +00001265 pMatch->colUsed |= ((Bitmask)1)<<n;
drh51669862004-12-18 18:40:26 +00001266 }
1267
danielk1977d5d56522005-03-16 12:15:20 +00001268lookupname_end:
drh8141f612004-01-25 22:44:58 +00001269 /* Clean up and return
1270 */
drh17435752007-08-16 04:30:38 +00001271 sqlite3_free(zDb);
1272 sqlite3_free(zTab);
danielk19774adee202004-05-08 08:23:19 +00001273 sqlite3ExprDelete(pExpr->pLeft);
drh8141f612004-01-25 22:44:58 +00001274 pExpr->pLeft = 0;
danielk19774adee202004-05-08 08:23:19 +00001275 sqlite3ExprDelete(pExpr->pRight);
drh8141f612004-01-25 22:44:58 +00001276 pExpr->pRight = 0;
1277 pExpr->op = TK_COLUMN;
drh15ccce12005-05-23 15:06:39 +00001278lookupname_end_2:
drh17435752007-08-16 04:30:38 +00001279 sqlite3_free(zCol);
drh626a8792005-01-17 22:08:19 +00001280 if( cnt==1 ){
danielk1977b3bce662005-01-29 08:32:43 +00001281 assert( pNC!=0 );
drh728b5772007-09-18 15:55:07 +00001282 sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList);
danielk1977aee18ef2005-03-09 12:26:50 +00001283 if( pMatch && !pMatch->pSelect ){
1284 pExpr->pTab = pMatch->pTab;
1285 }
drh15ccce12005-05-23 15:06:39 +00001286 /* Increment the nRef value on all name contexts from TopNC up to
1287 ** the point where the name matched. */
1288 for(;;){
1289 assert( pTopNC!=0 );
1290 pTopNC->nRef++;
1291 if( pTopNC==pNC ) break;
1292 pTopNC = pTopNC->pNext;
1293 }
1294 return 0;
1295 } else {
1296 return 1;
drh626a8792005-01-17 22:08:19 +00001297 }
drh8141f612004-01-25 22:44:58 +00001298}
1299
1300/*
drh626a8792005-01-17 22:08:19 +00001301** This routine is designed as an xFunc for walkExprTree().
1302**
drh73b211a2005-01-18 04:00:42 +00001303** Resolve symbolic names into TK_COLUMN operators for the current
drh626a8792005-01-17 22:08:19 +00001304** node in the expression tree. Return 0 to continue the search down
drh73b211a2005-01-18 04:00:42 +00001305** the tree or 2 to abort the tree walk.
1306**
1307** This routine also does error checking and name resolution for
1308** function names. The operator for aggregate functions is changed
1309** to TK_AGG_FUNCTION.
drh626a8792005-01-17 22:08:19 +00001310*/
1311static int nameResolverStep(void *pArg, Expr *pExpr){
1312 NameContext *pNC = (NameContext*)pArg;
drh626a8792005-01-17 22:08:19 +00001313 Parse *pParse;
drh626a8792005-01-17 22:08:19 +00001314
danielk1977b3bce662005-01-29 08:32:43 +00001315 if( pExpr==0 ) return 1;
drh626a8792005-01-17 22:08:19 +00001316 assert( pNC!=0 );
drh626a8792005-01-17 22:08:19 +00001317 pParse = pNC->pParse;
danielk1977b3bce662005-01-29 08:32:43 +00001318
drh626a8792005-01-17 22:08:19 +00001319 if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return 1;
1320 ExprSetProperty(pExpr, EP_Resolved);
1321#ifndef NDEBUG
danielk1977f0113002006-01-24 12:09:17 +00001322 if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
1323 SrcList *pSrcList = pNC->pSrcList;
danielk1977940fac92005-01-23 22:41:37 +00001324 int i;
danielk1977f0113002006-01-24 12:09:17 +00001325 for(i=0; i<pNC->pSrcList->nSrc; i++){
drh626a8792005-01-17 22:08:19 +00001326 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
1327 }
1328 }
1329#endif
1330 switch( pExpr->op ){
1331 /* Double-quoted strings (ex: "abc") are used as identifiers if
1332 ** possible. Otherwise they remain as strings. Single-quoted
1333 ** strings (ex: 'abc') are always string literals.
1334 */
1335 case TK_STRING: {
1336 if( pExpr->token.z[0]=='\'' ) break;
1337 /* Fall thru into the TK_ID case if this is a double-quoted string */
1338 }
1339 /* A lone identifier is the name of a column.
1340 */
1341 case TK_ID: {
drh626a8792005-01-17 22:08:19 +00001342 lookupName(pParse, 0, 0, &pExpr->token, pNC, pExpr);
1343 return 1;
1344 }
1345
1346 /* A table name and column name: ID.ID
1347 ** Or a database, table and column: ID.ID.ID
1348 */
1349 case TK_DOT: {
1350 Token *pColumn;
1351 Token *pTable;
1352 Token *pDb;
1353 Expr *pRight;
1354
danielk1977b3bce662005-01-29 08:32:43 +00001355 /* if( pSrcList==0 ) break; */
drh626a8792005-01-17 22:08:19 +00001356 pRight = pExpr->pRight;
1357 if( pRight->op==TK_ID ){
1358 pDb = 0;
1359 pTable = &pExpr->pLeft->token;
1360 pColumn = &pRight->token;
1361 }else{
1362 assert( pRight->op==TK_DOT );
1363 pDb = &pExpr->pLeft->token;
1364 pTable = &pRight->pLeft->token;
1365 pColumn = &pRight->pRight->token;
1366 }
1367 lookupName(pParse, pDb, pTable, pColumn, pNC, pExpr);
1368 return 1;
1369 }
1370
1371 /* Resolve function names
1372 */
drhb71090f2005-05-23 17:26:51 +00001373 case TK_CONST_FUNC:
drh626a8792005-01-17 22:08:19 +00001374 case TK_FUNCTION: {
1375 ExprList *pList = pExpr->pList; /* The argument list */
1376 int n = pList ? pList->nExpr : 0; /* Number of arguments */
1377 int no_such_func = 0; /* True if no such function exists */
1378 int wrong_num_args = 0; /* True if wrong number of arguments */
1379 int is_agg = 0; /* True if is an aggregate function */
1380 int i;
drh5169bbc2006-08-24 14:59:45 +00001381 int auth; /* Authorization to use the function */
drh626a8792005-01-17 22:08:19 +00001382 int nId; /* Number of characters in function name */
1383 const char *zId; /* The function name. */
drh73b211a2005-01-18 04:00:42 +00001384 FuncDef *pDef; /* Information about the function */
danielk197714db2662006-01-09 16:12:04 +00001385 int enc = ENC(pParse->db); /* The database encoding */
drh626a8792005-01-17 22:08:19 +00001386
drh2646da72005-12-09 20:02:05 +00001387 zId = (char*)pExpr->token.z;
drhb71090f2005-05-23 17:26:51 +00001388 nId = pExpr->token.n;
drh626a8792005-01-17 22:08:19 +00001389 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
1390 if( pDef==0 ){
1391 pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
1392 if( pDef==0 ){
1393 no_such_func = 1;
1394 }else{
1395 wrong_num_args = 1;
1396 }
1397 }else{
1398 is_agg = pDef->xFunc==0;
1399 }
drh2fca7fe2006-11-23 11:59:13 +00001400#ifndef SQLITE_OMIT_AUTHORIZATION
drh5169bbc2006-08-24 14:59:45 +00001401 if( pDef ){
1402 auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0);
1403 if( auth!=SQLITE_OK ){
1404 if( auth==SQLITE_DENY ){
1405 sqlite3ErrorMsg(pParse, "not authorized to use function: %s",
1406 pDef->zName);
1407 pNC->nErr++;
1408 }
1409 pExpr->op = TK_NULL;
1410 return 1;
1411 }
1412 }
drhb8b14212006-08-24 15:18:25 +00001413#endif
drh626a8792005-01-17 22:08:19 +00001414 if( is_agg && !pNC->allowAgg ){
1415 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
1416 pNC->nErr++;
1417 is_agg = 0;
1418 }else if( no_such_func ){
1419 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
1420 pNC->nErr++;
1421 }else if( wrong_num_args ){
1422 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
1423 nId, zId);
1424 pNC->nErr++;
1425 }
1426 if( is_agg ){
1427 pExpr->op = TK_AGG_FUNCTION;
1428 pNC->hasAgg = 1;
1429 }
drh73b211a2005-01-18 04:00:42 +00001430 if( is_agg ) pNC->allowAgg = 0;
drh626a8792005-01-17 22:08:19 +00001431 for(i=0; pNC->nErr==0 && i<n; i++){
drh73b211a2005-01-18 04:00:42 +00001432 walkExprTree(pList->a[i].pExpr, nameResolverStep, pNC);
drh626a8792005-01-17 22:08:19 +00001433 }
drh73b211a2005-01-18 04:00:42 +00001434 if( is_agg ) pNC->allowAgg = 1;
drh626a8792005-01-17 22:08:19 +00001435 /* FIX ME: Compute pExpr->affinity based on the expected return
1436 ** type of the function
1437 */
1438 return is_agg;
1439 }
danielk1977b3bce662005-01-29 08:32:43 +00001440#ifndef SQLITE_OMIT_SUBQUERY
1441 case TK_SELECT:
1442 case TK_EXISTS:
1443#endif
1444 case TK_IN: {
1445 if( pExpr->pSelect ){
drh8a9f38f2005-11-05 15:07:55 +00001446 int nRef = pNC->nRef;
drh06f65412005-11-03 02:03:13 +00001447#ifndef SQLITE_OMIT_CHECK
1448 if( pNC->isCheck ){
1449 sqlite3ErrorMsg(pParse,"subqueries prohibited in CHECK constraints");
1450 }
1451#endif
danielk1977b3bce662005-01-29 08:32:43 +00001452 sqlite3SelectResolve(pParse, pExpr->pSelect, pNC);
1453 assert( pNC->nRef>=nRef );
1454 if( nRef!=pNC->nRef ){
1455 ExprSetProperty(pExpr, EP_VarSelect);
1456 }
1457 }
drh4284fb02005-11-03 12:33:28 +00001458 break;
danielk1977b3bce662005-01-29 08:32:43 +00001459 }
drh4284fb02005-11-03 12:33:28 +00001460#ifndef SQLITE_OMIT_CHECK
1461 case TK_VARIABLE: {
1462 if( pNC->isCheck ){
1463 sqlite3ErrorMsg(pParse,"parameters prohibited in CHECK constraints");
1464 }
1465 break;
1466 }
1467#endif
drh626a8792005-01-17 22:08:19 +00001468 }
1469 return 0;
1470}
1471
1472/*
drhcce7d172000-05-31 15:34:51 +00001473** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +00001474** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +00001475** index to the table in the table list and a column offset. The
1476** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
1477** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +00001478** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +00001479** VDBE cursor number for a cursor that is pointing into the referenced
1480** table. The Expr.iColumn value is changed to the index of the column
1481** of the referenced table. The Expr.iColumn value for the special
1482** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
1483** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +00001484**
drh626a8792005-01-17 22:08:19 +00001485** Also resolve function names and check the functions for proper
1486** usage. Make sure all function names are recognized and all functions
1487** have the correct number of arguments. Leave an error message
1488** in pParse->zErrMsg if anything is amiss. Return the number of errors.
1489**
drh73b211a2005-01-18 04:00:42 +00001490** If the expression contains aggregate functions then set the EP_Agg
1491** property on the expression.
drh626a8792005-01-17 22:08:19 +00001492*/
danielk1977fc976062007-05-10 10:46:56 +00001493int sqlite3ExprResolveNames(
danielk1977b3bce662005-01-29 08:32:43 +00001494 NameContext *pNC, /* Namespace to resolve expressions in. */
1495 Expr *pExpr /* The expression to be analyzed. */
drh626a8792005-01-17 22:08:19 +00001496){
drh13449892005-09-07 21:22:45 +00001497 int savedHasAgg;
drh73b211a2005-01-18 04:00:42 +00001498 if( pExpr==0 ) return 0;
danielk1977e6a58a42007-08-31 17:42:48 +00001499#if defined(SQLITE_TEST) || SQLITE_MAX_EXPR_DEPTH>0
danielk1977fc976062007-05-10 10:46:56 +00001500 if( (pExpr->nHeight+pNC->pParse->nHeight)>SQLITE_MAX_EXPR_DEPTH ){
1501 sqlite3ErrorMsg(pNC->pParse,
1502 "Expression tree is too large (maximum depth %d)",
1503 SQLITE_MAX_EXPR_DEPTH
1504 );
1505 return 1;
1506 }
1507 pNC->pParse->nHeight += pExpr->nHeight;
1508#endif
drh13449892005-09-07 21:22:45 +00001509 savedHasAgg = pNC->hasAgg;
1510 pNC->hasAgg = 0;
danielk1977b3bce662005-01-29 08:32:43 +00001511 walkExprTree(pExpr, nameResolverStep, pNC);
danielk1977e6a58a42007-08-31 17:42:48 +00001512#if defined(SQLITE_TEST) || SQLITE_MAX_EXPR_DEPTH>0
danielk1977fc976062007-05-10 10:46:56 +00001513 pNC->pParse->nHeight -= pExpr->nHeight;
1514#endif
danielk1977b3bce662005-01-29 08:32:43 +00001515 if( pNC->nErr>0 ){
drh73b211a2005-01-18 04:00:42 +00001516 ExprSetProperty(pExpr, EP_Error);
drh73b211a2005-01-18 04:00:42 +00001517 }
drh13449892005-09-07 21:22:45 +00001518 if( pNC->hasAgg ){
1519 ExprSetProperty(pExpr, EP_Agg);
1520 }else if( savedHasAgg ){
1521 pNC->hasAgg = 1;
1522 }
drh73b211a2005-01-18 04:00:42 +00001523 return ExprHasProperty(pExpr, EP_Error);
drh626a8792005-01-17 22:08:19 +00001524}
1525
drh1398ad32005-01-19 23:24:50 +00001526/*
1527** A pointer instance of this structure is used to pass information
1528** through walkExprTree into codeSubqueryStep().
1529*/
1530typedef struct QueryCoder QueryCoder;
1531struct QueryCoder {
1532 Parse *pParse; /* The parsing context */
1533 NameContext *pNC; /* Namespace of first enclosing query */
1534};
1535
danielk19779a96b662007-11-29 17:05:18 +00001536#ifdef SQLITE_TEST
1537 int sqlite3_enable_in_opt = 1;
1538#else
1539 #define sqlite3_enable_in_opt 1
1540#endif
1541
1542/*
1543** This function is used by the implementation of the IN (...) operator.
1544** It's job is to find or create a b-tree structure that may be used
1545** either to test for membership of the (...) set or to iterate through
drh85b623f2007-12-13 21:54:09 +00001546** its members, skipping duplicates.
danielk19779a96b662007-11-29 17:05:18 +00001547**
1548** The cursor opened on the structure (database table, database index
1549** or ephermal table) is stored in pX->iTable before this function returns.
1550** The returned value indicates the structure type, as follows:
1551**
1552** IN_INDEX_ROWID - The cursor was opened on a database table.
drh2d401ab2008-01-10 23:50:11 +00001553** IN_INDEX_INDEX - The cursor was opened on a database index.
danielk19779a96b662007-11-29 17:05:18 +00001554** IN_INDEX_EPH - The cursor was opened on a specially created and
1555** populated epheremal table.
1556**
1557** An existing structure may only be used if the SELECT is of the simple
1558** form:
1559**
1560** SELECT <column> FROM <table>
1561**
1562** If the mustBeUnique parameter is false, the structure will be used
1563** for fast set membership tests. In this case an epheremal table must
1564** be used unless <column> is an INTEGER PRIMARY KEY or an index can
drh85b623f2007-12-13 21:54:09 +00001565** be found with <column> as its left-most column.
danielk19779a96b662007-11-29 17:05:18 +00001566**
1567** If mustBeUnique is true, then the structure will be used to iterate
1568** through the set members, skipping any duplicates. In this case an
1569** epheremal table must be used unless the selected <column> is guaranteed
1570** to be unique - either because it is an INTEGER PRIMARY KEY or it
1571** is unique by virtue of a constraint or implicit index.
1572*/
danielk1977284f4ac2007-12-10 05:03:46 +00001573#ifndef SQLITE_OMIT_SUBQUERY
danielk19779a96b662007-11-29 17:05:18 +00001574int sqlite3FindInIndex(Parse *pParse, Expr *pX, int mustBeUnique){
1575 Select *p;
1576 int eType = 0;
1577 int iTab = pParse->nTab++;
1578
1579 /* The follwing if(...) expression is true if the SELECT is of the
1580 ** simple form:
1581 **
1582 ** SELECT <column> FROM <table>
1583 **
1584 ** If this is the case, it may be possible to use an existing table
1585 ** or index instead of generating an epheremal table.
1586 */
1587 if( sqlite3_enable_in_opt
1588 && (p=pX->pSelect) && !p->pPrior
1589 && !p->isDistinct && !p->isAgg && !p->pGroupBy
1590 && p->pSrc && p->pSrc->nSrc==1 && !p->pSrc->a[0].pSelect
1591 && !p->pSrc->a[0].pTab->pSelect
1592 && p->pEList->nExpr==1 && p->pEList->a[0].pExpr->op==TK_COLUMN
1593 && !p->pLimit && !p->pOffset && !p->pWhere
1594 ){
1595 sqlite3 *db = pParse->db;
1596 Index *pIdx;
1597 Expr *pExpr = p->pEList->a[0].pExpr;
1598 int iCol = pExpr->iColumn;
1599 Vdbe *v = sqlite3GetVdbe(pParse);
1600
1601 /* This function is only called from two places. In both cases the vdbe
1602 ** has already been allocated. So assume sqlite3GetVdbe() is always
1603 ** successful here.
1604 */
1605 assert(v);
1606 if( iCol<0 ){
drh0a07c102008-01-03 18:03:08 +00001607 int iMem = ++pParse->nMem;
danielk19779a96b662007-11-29 17:05:18 +00001608 int iAddr;
1609 Table *pTab = p->pSrc->a[0].pTab;
1610 int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
1611 sqlite3VdbeUsesBtree(v, iDb);
1612
drh892d3172008-01-10 03:46:36 +00001613 iAddr = sqlite3VdbeAddOp1(v, OP_If, iMem);
drh4c583122008-01-04 22:01:03 +00001614 sqlite3VdbeAddOp2(v, OP_Integer, 1, iMem);
danielk19779a96b662007-11-29 17:05:18 +00001615
1616 sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
1617 eType = IN_INDEX_ROWID;
1618
1619 sqlite3VdbeJumpHere(v, iAddr);
1620 }else{
1621 /* The collation sequence used by the comparison. If an index is to
1622 ** be used in place of a temp-table, it must be ordered according
1623 ** to this collation sequence.
1624 */
1625 CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pExpr);
1626
1627 /* Check that the affinity that will be used to perform the
1628 ** comparison is the same as the affinity of the column. If
1629 ** it is not, it is not possible to use any index.
1630 */
1631 Table *pTab = p->pSrc->a[0].pTab;
1632 char aff = comparisonAffinity(pX);
1633 int affinity_ok = (pTab->aCol[iCol].affinity==aff||aff==SQLITE_AFF_NONE);
1634
1635 for(pIdx=pTab->pIndex; pIdx && eType==0 && affinity_ok; pIdx=pIdx->pNext){
1636 if( (pIdx->aiColumn[0]==iCol)
1637 && (pReq==sqlite3FindCollSeq(db, ENC(db), pIdx->azColl[0], -1, 0))
1638 && (!mustBeUnique || (pIdx->nColumn==1 && pIdx->onError!=OE_None))
1639 ){
1640 int iDb;
drh0a07c102008-01-03 18:03:08 +00001641 int iMem = ++pParse->nMem;
danielk19779a96b662007-11-29 17:05:18 +00001642 int iAddr;
1643 char *pKey;
1644
1645 pKey = (char *)sqlite3IndexKeyinfo(pParse, pIdx);
1646 iDb = sqlite3SchemaToIndex(db, pIdx->pSchema);
1647 sqlite3VdbeUsesBtree(v, iDb);
1648
drh892d3172008-01-10 03:46:36 +00001649 iAddr = sqlite3VdbeAddOp1(v, OP_If, iMem);
drh4c583122008-01-04 22:01:03 +00001650 sqlite3VdbeAddOp2(v, OP_Integer, 1, iMem);
danielk19779a96b662007-11-29 17:05:18 +00001651
danielk1977207872a2008-01-03 07:54:23 +00001652 sqlite3VdbeAddOp4(v, OP_OpenRead, iTab, pIdx->tnum, iDb,
drh66a51672008-01-03 00:01:23 +00001653 pKey,P4_KEYINFO_HANDOFF);
danielk1977207872a2008-01-03 07:54:23 +00001654 VdbeComment((v, "%s", pIdx->zName));
danielk19779a96b662007-11-29 17:05:18 +00001655 eType = IN_INDEX_INDEX;
drh66a51672008-01-03 00:01:23 +00001656 sqlite3VdbeAddOp2(v, OP_SetNumColumns, iTab, pIdx->nColumn);
danielk19779a96b662007-11-29 17:05:18 +00001657
1658 sqlite3VdbeJumpHere(v, iAddr);
1659 }
1660 }
1661 }
1662 }
1663
1664 if( eType==0 ){
1665 sqlite3CodeSubselect(pParse, pX);
1666 eType = IN_INDEX_EPH;
1667 }else{
1668 pX->iTable = iTab;
1669 }
1670 return eType;
1671}
danielk1977284f4ac2007-12-10 05:03:46 +00001672#endif
drh626a8792005-01-17 22:08:19 +00001673
1674/*
drh9cbe6352005-11-29 03:13:21 +00001675** Generate code for scalar subqueries used as an expression
1676** and IN operators. Examples:
drh626a8792005-01-17 22:08:19 +00001677**
drh9cbe6352005-11-29 03:13:21 +00001678** (SELECT a FROM b) -- subquery
1679** EXISTS (SELECT a FROM b) -- EXISTS subquery
1680** x IN (4,5,11) -- IN operator with list on right-hand side
1681** x IN (SELECT a FROM b) -- IN operator with subquery on the right
drhfef52082000-06-06 01:50:43 +00001682**
drh9cbe6352005-11-29 03:13:21 +00001683** The pExpr parameter describes the expression that contains the IN
1684** operator or subquery.
drhcce7d172000-05-31 15:34:51 +00001685*/
drh51522cd2005-01-20 13:36:19 +00001686#ifndef SQLITE_OMIT_SUBQUERY
danielk1977b3bce662005-01-29 08:32:43 +00001687void sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){
drh57dbd7b2005-07-08 18:25:26 +00001688 int testAddr = 0; /* One-time test address */
danielk1977b3bce662005-01-29 08:32:43 +00001689 Vdbe *v = sqlite3GetVdbe(pParse);
1690 if( v==0 ) return;
1691
danielk1977fc976062007-05-10 10:46:56 +00001692
drh57dbd7b2005-07-08 18:25:26 +00001693 /* This code must be run in its entirety every time it is encountered
1694 ** if any of the following is true:
1695 **
1696 ** * The right-hand side is a correlated subquery
1697 ** * The right-hand side is an expression list containing variables
1698 ** * We are inside a trigger
1699 **
1700 ** If all of the above are false, then we can run this code just once
1701 ** save the results, and reuse the same result on subsequent invocations.
danielk1977b3bce662005-01-29 08:32:43 +00001702 */
1703 if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->trigStack ){
drh0a07c102008-01-03 18:03:08 +00001704 int mem = ++pParse->nMem;
drh892d3172008-01-10 03:46:36 +00001705 sqlite3VdbeAddOp1(v, OP_If, mem);
1706 testAddr = sqlite3VdbeAddOp2(v, OP_Integer, 1, mem);
drh17435752007-08-16 04:30:38 +00001707 assert( testAddr>0 || pParse->db->mallocFailed );
danielk1977b3bce662005-01-29 08:32:43 +00001708 }
1709
drhcce7d172000-05-31 15:34:51 +00001710 switch( pExpr->op ){
drhfef52082000-06-06 01:50:43 +00001711 case TK_IN: {
danielk1977e014a832004-05-17 10:48:57 +00001712 char affinity;
drhd3d39e92004-05-20 22:16:29 +00001713 KeyInfo keyInfo;
drhb9bb7c12006-06-11 23:41:55 +00001714 int addr; /* Address of OP_OpenEphemeral instruction */
drhd3d39e92004-05-20 22:16:29 +00001715
danielk1977bf3b7212004-05-18 10:06:24 +00001716 affinity = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +00001717
1718 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
drh57dbd7b2005-07-08 18:25:26 +00001719 ** expression it is handled the same way. A virtual table is
danielk1977e014a832004-05-17 10:48:57 +00001720 ** filled with single-field index keys representing the results
1721 ** from the SELECT or the <exprlist>.
1722 **
1723 ** If the 'x' expression is a column value, or the SELECT...
1724 ** statement returns a column value, then the affinity of that
1725 ** column is used to build the index keys. If both 'x' and the
1726 ** SELECT... statement are columns, then numeric affinity is used
1727 ** if either column has NUMERIC or INTEGER affinity. If neither
1728 ** 'x' nor the SELECT... statement are columns, then numeric affinity
1729 ** is used.
1730 */
1731 pExpr->iTable = pParse->nTab++;
drh66a51672008-01-03 00:01:23 +00001732 addr = sqlite3VdbeAddOp1(v, OP_OpenEphemeral, pExpr->iTable);
drhd3d39e92004-05-20 22:16:29 +00001733 memset(&keyInfo, 0, sizeof(keyInfo));
1734 keyInfo.nField = 1;
drh66a51672008-01-03 00:01:23 +00001735 sqlite3VdbeAddOp2(v, OP_SetNumColumns, pExpr->iTable, 1);
danielk1977e014a832004-05-17 10:48:57 +00001736
drhfef52082000-06-06 01:50:43 +00001737 if( pExpr->pSelect ){
1738 /* Case 1: expr IN (SELECT ...)
1739 **
danielk1977e014a832004-05-17 10:48:57 +00001740 ** Generate code to write the results of the select into the temporary
1741 ** table allocated and opened above.
drhfef52082000-06-06 01:50:43 +00001742 */
drh1013c932008-01-06 00:25:21 +00001743 SelectDest dest;
drhbe5c89a2004-07-26 00:31:09 +00001744 ExprList *pEList;
drh1013c932008-01-06 00:25:21 +00001745
1746 sqlite3SelectDestInit(&dest, SRT_Set, pExpr->iTable);
1747 dest.affinity = (int)affinity;
danielk1977e014a832004-05-17 10:48:57 +00001748 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
danielk19776c8c8ce2008-01-02 16:27:09 +00001749 if( sqlite3Select(pParse, pExpr->pSelect, &dest, 0, 0, 0, 0) ){
drh94ccde52007-04-13 16:06:32 +00001750 return;
1751 }
drhbe5c89a2004-07-26 00:31:09 +00001752 pEList = pExpr->pSelect->pEList;
1753 if( pEList && pEList->nExpr>0 ){
danielk1977bcbb04e2007-05-29 12:11:29 +00001754 keyInfo.aColl[0] = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft,
drhbe5c89a2004-07-26 00:31:09 +00001755 pEList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001756 }
drhfef52082000-06-06 01:50:43 +00001757 }else if( pExpr->pList ){
1758 /* Case 2: expr IN (exprlist)
1759 **
drhfd131da2007-08-07 17:13:03 +00001760 ** For each expression, build an index key from the evaluation and
danielk1977e014a832004-05-17 10:48:57 +00001761 ** store it in the temporary table. If <expr> is a column, then use
1762 ** that columns affinity when building index keys. If <expr> is not
1763 ** a column, use numeric affinity.
drhfef52082000-06-06 01:50:43 +00001764 */
danielk1977e014a832004-05-17 10:48:57 +00001765 int i;
drh57dbd7b2005-07-08 18:25:26 +00001766 ExprList *pList = pExpr->pList;
1767 struct ExprList_item *pItem;
drh2d401ab2008-01-10 23:50:11 +00001768 int r1, r2;
drh57dbd7b2005-07-08 18:25:26 +00001769
danielk1977e014a832004-05-17 10:48:57 +00001770 if( !affinity ){
drh8159a352006-05-23 23:22:29 +00001771 affinity = SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +00001772 }
danielk19770202b292004-06-09 09:55:16 +00001773 keyInfo.aColl[0] = pExpr->pLeft->pColl;
danielk1977e014a832004-05-17 10:48:57 +00001774
1775 /* Loop through each expression in <exprlist>. */
drh2d401ab2008-01-10 23:50:11 +00001776 r1 = sqlite3GetTempReg(pParse);
1777 r2 = sqlite3GetTempReg(pParse);
drh57dbd7b2005-07-08 18:25:26 +00001778 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
1779 Expr *pE2 = pItem->pExpr;
danielk1977e014a832004-05-17 10:48:57 +00001780
drh57dbd7b2005-07-08 18:25:26 +00001781 /* If the expression is not constant then we will need to
1782 ** disable the test that was generated above that makes sure
1783 ** this code only executes once. Because for a non-constant
1784 ** expression we need to rerun this code each time.
1785 */
drh892d3172008-01-10 03:46:36 +00001786 if( testAddr && !sqlite3ExprIsConstant(pE2) ){
1787 sqlite3VdbeChangeToNoop(v, testAddr-1, 2);
drh57dbd7b2005-07-08 18:25:26 +00001788 testAddr = 0;
drh4794b982000-06-06 13:54:14 +00001789 }
danielk1977e014a832004-05-17 10:48:57 +00001790
1791 /* Evaluate the expression and insert it into the temp table */
drh2d401ab2008-01-10 23:50:11 +00001792 sqlite3ExprCode(pParse, pE2, r1);
1793 sqlite3VdbeAddOp4(v, OP_RegMakeRec, r1, 1, r2, &affinity, 1);
1794 sqlite3VdbeAddOp2(v, OP_IdxInsert, pExpr->iTable, r2);
drhfef52082000-06-06 01:50:43 +00001795 }
drh2d401ab2008-01-10 23:50:11 +00001796 sqlite3ReleaseTempReg(pParse, r1);
1797 sqlite3ReleaseTempReg(pParse, r2);
drhfef52082000-06-06 01:50:43 +00001798 }
drh66a51672008-01-03 00:01:23 +00001799 sqlite3VdbeChangeP4(v, addr, (void *)&keyInfo, P4_KEYINFO);
danielk1977b3bce662005-01-29 08:32:43 +00001800 break;
drhfef52082000-06-06 01:50:43 +00001801 }
1802
drh51522cd2005-01-20 13:36:19 +00001803 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001804 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +00001805 /* This has to be a scalar SELECT. Generate code to put the
1806 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +00001807 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +00001808 */
drh2646da72005-12-09 20:02:05 +00001809 static const Token one = { (u8*)"1", 0, 1 };
drh51522cd2005-01-20 13:36:19 +00001810 Select *pSel;
danielk19776c8c8ce2008-01-02 16:27:09 +00001811 SelectDest dest;
drh1398ad32005-01-19 23:24:50 +00001812
drh51522cd2005-01-20 13:36:19 +00001813 pSel = pExpr->pSelect;
drh1013c932008-01-06 00:25:21 +00001814 sqlite3SelectDestInit(&dest, 0, ++pParse->nMem);
drh51522cd2005-01-20 13:36:19 +00001815 if( pExpr->op==TK_SELECT ){
danielk19776c8c8ce2008-01-02 16:27:09 +00001816 dest.eDest = SRT_Mem;
drh4c583122008-01-04 22:01:03 +00001817 sqlite3VdbeAddOp2(v, OP_Null, 0, dest.iParm);
drhd4e70eb2008-01-02 00:34:36 +00001818 VdbeComment((v, "Init subquery result"));
drh51522cd2005-01-20 13:36:19 +00001819 }else{
danielk19776c8c8ce2008-01-02 16:27:09 +00001820 dest.eDest = SRT_Exists;
drh4c583122008-01-04 22:01:03 +00001821 sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iParm);
drhd4e70eb2008-01-02 00:34:36 +00001822 VdbeComment((v, "Init EXISTS result"));
drh51522cd2005-01-20 13:36:19 +00001823 }
drhec7429a2005-10-06 16:53:14 +00001824 sqlite3ExprDelete(pSel->pLimit);
danielk1977a1644fd2007-08-29 12:31:25 +00001825 pSel->pLimit = sqlite3PExpr(pParse, TK_INTEGER, 0, 0, &one);
danielk19776c8c8ce2008-01-02 16:27:09 +00001826 if( sqlite3Select(pParse, pSel, &dest, 0, 0, 0, 0) ){
drh94ccde52007-04-13 16:06:32 +00001827 return;
1828 }
danielk19776c8c8ce2008-01-02 16:27:09 +00001829 pExpr->iColumn = dest.iParm;
danielk1977b3bce662005-01-29 08:32:43 +00001830 break;
drhcce7d172000-05-31 15:34:51 +00001831 }
1832 }
danielk1977b3bce662005-01-29 08:32:43 +00001833
drh57dbd7b2005-07-08 18:25:26 +00001834 if( testAddr ){
drh892d3172008-01-10 03:46:36 +00001835 sqlite3VdbeJumpHere(v, testAddr-1);
danielk1977b3bce662005-01-29 08:32:43 +00001836 }
danielk1977fc976062007-05-10 10:46:56 +00001837
danielk1977b3bce662005-01-29 08:32:43 +00001838 return;
drhcce7d172000-05-31 15:34:51 +00001839}
drh51522cd2005-01-20 13:36:19 +00001840#endif /* SQLITE_OMIT_SUBQUERY */
drhcce7d172000-05-31 15:34:51 +00001841
drhcce7d172000-05-31 15:34:51 +00001842/*
drh598f1342007-10-23 15:39:45 +00001843** Duplicate an 8-byte value
1844*/
1845static char *dup8bytes(Vdbe *v, const char *in){
1846 char *out = sqlite3DbMallocRaw(sqlite3VdbeDb(v), 8);
1847 if( out ){
1848 memcpy(out, in, 8);
1849 }
1850 return out;
1851}
1852
1853/*
1854** Generate an instruction that will put the floating point
1855** value described by z[0..n-1] on the stack.
drh0cf19ed2007-10-23 18:55:48 +00001856**
1857** The z[] string will probably not be zero-terminated. But the
1858** z[n] character is guaranteed to be something that does not look
1859** like the continuation of the number.
drh598f1342007-10-23 15:39:45 +00001860*/
drh9de221d2008-01-05 06:51:30 +00001861static void codeReal(Vdbe *v, const char *z, int n, int negateFlag, int iMem){
drh598f1342007-10-23 15:39:45 +00001862 assert( z || v==0 || sqlite3VdbeDb(v)->mallocFailed );
1863 if( z ){
1864 double value;
1865 char *zV;
drh0cf19ed2007-10-23 18:55:48 +00001866 assert( !isdigit(z[n]) );
drh598f1342007-10-23 15:39:45 +00001867 sqlite3AtoF(z, &value);
1868 if( negateFlag ) value = -value;
1869 zV = dup8bytes(v, (char*)&value);
drh9de221d2008-01-05 06:51:30 +00001870 sqlite3VdbeAddOp4(v, OP_Real, 0, iMem, 0, zV, P4_REAL);
drh598f1342007-10-23 15:39:45 +00001871 }
1872}
1873
1874
1875/*
drhfec19aa2004-05-19 20:41:03 +00001876** Generate an instruction that will put the integer describe by
1877** text z[0..n-1] on the stack.
drh0cf19ed2007-10-23 18:55:48 +00001878**
1879** The z[] string will probably not be zero-terminated. But the
1880** z[n] character is guaranteed to be something that does not look
1881** like the continuation of the number.
drhfec19aa2004-05-19 20:41:03 +00001882*/
drh9de221d2008-01-05 06:51:30 +00001883static void codeInteger(Vdbe *v, const char *z, int n, int negFlag, int iMem){
drhabb6fca2007-08-16 12:24:01 +00001884 assert( z || v==0 || sqlite3VdbeDb(v)->mallocFailed );
danielk1977c9cf9012007-05-30 10:36:47 +00001885 if( z ){
1886 int i;
drh0cf19ed2007-10-23 18:55:48 +00001887 assert( !isdigit(z[n]) );
danielk1977c9cf9012007-05-30 10:36:47 +00001888 if( sqlite3GetInt32(z, &i) ){
drh9de221d2008-01-05 06:51:30 +00001889 if( negFlag ) i = -i;
1890 sqlite3VdbeAddOp2(v, OP_Integer, i, iMem);
1891 }else if( sqlite3FitsIn64Bits(z, negFlag) ){
drh598f1342007-10-23 15:39:45 +00001892 i64 value;
1893 char *zV;
1894 sqlite3Atoi64(z, &value);
drh9de221d2008-01-05 06:51:30 +00001895 if( negFlag ) value = -value;
drh598f1342007-10-23 15:39:45 +00001896 zV = dup8bytes(v, (char*)&value);
drh9de221d2008-01-05 06:51:30 +00001897 sqlite3VdbeAddOp4(v, OP_Int64, 0, iMem, 0, zV, P4_INT64);
danielk1977c9cf9012007-05-30 10:36:47 +00001898 }else{
drh9de221d2008-01-05 06:51:30 +00001899 codeReal(v, z, n, negFlag, iMem);
danielk1977c9cf9012007-05-30 10:36:47 +00001900 }
drhfec19aa2004-05-19 20:41:03 +00001901 }
1902}
1903
drh945498f2007-02-24 11:52:52 +00001904
1905/*
1906** Generate code that will extract the iColumn-th column from
drh2133d822008-01-03 18:44:59 +00001907** table pTab and store the column value in register iMem, or on
1908** the stack if iMem==0. There is an open cursor to pTab in
1909** iTable. If iColumn<0 then code is generated that extracts the rowid.
drh945498f2007-02-24 11:52:52 +00001910*/
drh2133d822008-01-03 18:44:59 +00001911void sqlite3ExprCodeGetColumn(
1912 Vdbe *v, /* The VM being created */
1913 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){
drh945498f2007-02-24 11:52:52 +00001918 if( iColumn<0 ){
1919 int op = (pTab && IsVirtual(pTab)) ? OP_VRowid : OP_Rowid;
drh2133d822008-01-03 18:44:59 +00001920 sqlite3VdbeAddOp2(v, op, iTable, iReg);
drh945498f2007-02-24 11:52:52 +00001921 }else if( pTab==0 ){
drh2133d822008-01-03 18:44:59 +00001922 sqlite3VdbeAddOp3(v, OP_Column, iTable, iColumn, iReg);
drh945498f2007-02-24 11:52:52 +00001923 }else{
1924 int op = IsVirtual(pTab) ? OP_VColumn : OP_Column;
drh2133d822008-01-03 18:44:59 +00001925 sqlite3VdbeAddOp3(v, op, iTable, iColumn, iReg);
drh945498f2007-02-24 11:52:52 +00001926 sqlite3ColumnDefault(v, pTab, iColumn);
1927#ifndef SQLITE_OMIT_FLOATING_POINT
1928 if( pTab->aCol[iColumn].affinity==SQLITE_AFF_REAL ){
drh2133d822008-01-03 18:44:59 +00001929 sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg);
drh945498f2007-02-24 11:52:52 +00001930 }
1931#endif
1932 }
1933}
1934
drhfec19aa2004-05-19 20:41:03 +00001935/*
drhcce7d172000-05-31 15:34:51 +00001936** Generate code into the current Vdbe to evaluate the given
drh389a1ad2008-01-03 23:44:53 +00001937** expression and leaves the result in a register on on the stack.
1938**
1939** If the target register number is negative, allocate a new
1940** register to store the result. If the target register number
1941** is zero then push the result onto the stack. Return the target
1942** register number regardless.
drhf2bc0132004-10-04 13:19:23 +00001943**
1944** This code depends on the fact that certain token values (ex: TK_EQ)
1945** are the same as opcode values (ex: OP_Eq) that implement the corresponding
1946** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
1947** the make process cause these values to align. Assert()s in the code
1948** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00001949*/
drh389a1ad2008-01-03 23:44:53 +00001950int sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){
drhcce7d172000-05-31 15:34:51 +00001951 Vdbe *v = pParse->pVdbe;
1952 int op;
drh389a1ad2008-01-03 23:44:53 +00001953 int inReg = 0;
drh9de221d2008-01-05 06:51:30 +00001954 int origTarget = target;
drhffe07b22005-11-03 00:41:17 +00001955
drh389a1ad2008-01-03 23:44:53 +00001956 assert( v!=0 || pParse->db->mallocFailed );
1957 if( v==0 ) return 0;
1958 if( target<0 ){
1959 target = ++pParse->nMem;
danielk19777977a172004-11-09 12:44:37 +00001960 }
drh389a1ad2008-01-03 23:44:53 +00001961
1962 if( pExpr==0 ){
1963 op = TK_NULL;
1964 }else{
1965 op = pExpr->op;
1966 }
drhf2bc0132004-10-04 13:19:23 +00001967 switch( op ){
drh13449892005-09-07 21:22:45 +00001968 case TK_AGG_COLUMN: {
1969 AggInfo *pAggInfo = pExpr->pAggInfo;
1970 struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg];
1971 if( !pAggInfo->directMode ){
drh9de221d2008-01-05 06:51:30 +00001972 assert( pCol->iMem>0 );
1973 inReg = pCol->iMem;
drh13449892005-09-07 21:22:45 +00001974 break;
1975 }else if( pAggInfo->useSortingIdx ){
drh389a1ad2008-01-03 23:44:53 +00001976 sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdx,
1977 pCol->iSorterColumn, target);
drh9de221d2008-01-05 06:51:30 +00001978 inReg = target;
drh13449892005-09-07 21:22:45 +00001979 break;
1980 }
1981 /* Otherwise, fall thru into the TK_COLUMN case */
1982 }
drh967e8b72000-06-21 13:59:10 +00001983 case TK_COLUMN: {
drhffe07b22005-11-03 00:41:17 +00001984 if( pExpr->iTable<0 ){
1985 /* This only happens when coding check constraints */
drhaa9b8962008-01-08 02:57:55 +00001986 assert( pParse->ckBase>0 );
1987 inReg = pExpr->iColumn + pParse->ckBase;
drhc4a3c772001-04-04 11:48:57 +00001988 }else{
drh2133d822008-01-03 18:44:59 +00001989 sqlite3ExprCodeGetColumn(v, pExpr->pTab,
drh389a1ad2008-01-03 23:44:53 +00001990 pExpr->iColumn, pExpr->iTable, target);
drh9de221d2008-01-05 06:51:30 +00001991 inReg = target;
drh22827922000-06-06 17:27:05 +00001992 }
drhcce7d172000-05-31 15:34:51 +00001993 break;
1994 }
1995 case TK_INTEGER: {
drh9de221d2008-01-05 06:51:30 +00001996 codeInteger(v, (char*)pExpr->token.z, pExpr->token.n, 0, target);
1997 inReg = target;
drhfec19aa2004-05-19 20:41:03 +00001998 break;
1999 }
drh598f1342007-10-23 15:39:45 +00002000 case TK_FLOAT: {
drh9de221d2008-01-05 06:51:30 +00002001 codeReal(v, (char*)pExpr->token.z, pExpr->token.n, 0, target);
2002 inReg = target;
drh598f1342007-10-23 15:39:45 +00002003 break;
2004 }
drhfec19aa2004-05-19 20:41:03 +00002005 case TK_STRING: {
danielk19771e536952007-08-16 10:09:01 +00002006 sqlite3DequoteExpr(pParse->db, pExpr);
drh9de221d2008-01-05 06:51:30 +00002007 sqlite3VdbeAddOp4(v,OP_String8, 0, target, 0,
drh66a51672008-01-03 00:01:23 +00002008 (char*)pExpr->token.z, pExpr->token.n);
drh9de221d2008-01-05 06:51:30 +00002009 inReg = target;
drhcce7d172000-05-31 15:34:51 +00002010 break;
2011 }
drhf0863fe2005-06-12 21:35:51 +00002012 case TK_NULL: {
drh9de221d2008-01-05 06:51:30 +00002013 sqlite3VdbeAddOp2(v, OP_Null, 0, target);
2014 inReg = target;
drhf0863fe2005-06-12 21:35:51 +00002015 break;
2016 }
danielk19775338a5f2005-01-20 13:03:10 +00002017#ifndef SQLITE_OMIT_BLOB_LITERAL
danielk1977c572ef72004-05-27 09:28:41 +00002018 case TK_BLOB: {
drh6c8c6ce2005-08-23 11:17:58 +00002019 int n;
2020 const char *z;
drhf2bc0132004-10-04 13:19:23 +00002021 assert( TK_BLOB==OP_HexBlob );
drh6c8c6ce2005-08-23 11:17:58 +00002022 n = pExpr->token.n - 3;
drh2646da72005-12-09 20:02:05 +00002023 z = (char*)pExpr->token.z + 2;
drh6c8c6ce2005-08-23 11:17:58 +00002024 assert( n>=0 );
2025 if( n==0 ){
2026 z = "";
2027 }
drh9de221d2008-01-05 06:51:30 +00002028 sqlite3VdbeAddOp4(v, op, 0, target, 0, z, n);
2029 inReg = target;
danielk1977c572ef72004-05-27 09:28:41 +00002030 break;
2031 }
danielk19775338a5f2005-01-20 13:03:10 +00002032#endif
drh50457892003-09-06 01:10:47 +00002033 case TK_VARIABLE: {
drh9de221d2008-01-05 06:51:30 +00002034 sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iTable, target);
drh895d7472004-08-20 16:02:39 +00002035 if( pExpr->token.n>1 ){
drh66a51672008-01-03 00:01:23 +00002036 sqlite3VdbeChangeP4(v, -1, (char*)pExpr->token.z, pExpr->token.n);
drh895d7472004-08-20 16:02:39 +00002037 }
drh9de221d2008-01-05 06:51:30 +00002038 inReg = target;
drh50457892003-09-06 01:10:47 +00002039 break;
2040 }
drh4e0cff62004-11-05 05:10:28 +00002041 case TK_REGISTER: {
drh9de221d2008-01-05 06:51:30 +00002042 inReg = pExpr->iTable;
drh4e0cff62004-11-05 05:10:28 +00002043 break;
2044 }
drh487e2622005-06-25 18:42:14 +00002045#ifndef SQLITE_OMIT_CAST
2046 case TK_CAST: {
2047 /* Expressions of the form: CAST(pLeft AS token) */
danielk1977f0113002006-01-24 12:09:17 +00002048 int aff, to_op;
drh9de221d2008-01-05 06:51:30 +00002049 sqlite3ExprCode(pParse, pExpr->pLeft, target);
drh8a512562005-11-14 22:29:05 +00002050 aff = sqlite3AffinityType(&pExpr->token);
danielk1977f0113002006-01-24 12:09:17 +00002051 to_op = aff - SQLITE_AFF_TEXT + OP_ToText;
2052 assert( to_op==OP_ToText || aff!=SQLITE_AFF_TEXT );
2053 assert( to_op==OP_ToBlob || aff!=SQLITE_AFF_NONE );
2054 assert( to_op==OP_ToNumeric || aff!=SQLITE_AFF_NUMERIC );
2055 assert( to_op==OP_ToInt || aff!=SQLITE_AFF_INTEGER );
2056 assert( to_op==OP_ToReal || aff!=SQLITE_AFF_REAL );
drh9de221d2008-01-05 06:51:30 +00002057 sqlite3VdbeAddOp1(v, to_op, target);
drh9de221d2008-01-05 06:51:30 +00002058 inReg = target;
drh487e2622005-06-25 18:42:14 +00002059 break;
2060 }
2061#endif /* SQLITE_OMIT_CAST */
drhc9b84a12002-06-20 11:36:48 +00002062 case TK_LT:
2063 case TK_LE:
2064 case TK_GT:
2065 case TK_GE:
2066 case TK_NE:
2067 case TK_EQ: {
drh35573352008-01-08 23:54:25 +00002068 int r1, r2;
drhf2bc0132004-10-04 13:19:23 +00002069 assert( TK_LT==OP_Lt );
2070 assert( TK_LE==OP_Le );
2071 assert( TK_GT==OP_Gt );
2072 assert( TK_GE==OP_Ge );
2073 assert( TK_EQ==OP_Eq );
2074 assert( TK_NE==OP_Ne );
drh35573352008-01-08 23:54:25 +00002075 if( target>0 ){
2076 inReg = target;
2077 }else{
2078 inReg = ++pParse->nMem;
2079 }
2080 r1 = sqlite3ExprCode(pParse, pExpr->pLeft, -1);
2081 r2 = sqlite3ExprCode(pParse, pExpr->pRight, -1);
2082 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
2083 r1, r2, inReg, SQLITE_STOREP2);
danielk1977a37cdde2004-05-16 11:15:36 +00002084 break;
drhc9b84a12002-06-20 11:36:48 +00002085 }
drhcce7d172000-05-31 15:34:51 +00002086 case TK_AND:
2087 case TK_OR:
2088 case TK_PLUS:
2089 case TK_STAR:
2090 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00002091 case TK_REM:
2092 case TK_BITAND:
2093 case TK_BITOR:
drh17c40292004-07-21 02:53:29 +00002094 case TK_SLASH:
drhbf4133c2001-10-13 02:59:08 +00002095 case TK_LSHIFT:
drh855eb1c2004-08-31 13:45:11 +00002096 case TK_RSHIFT:
drh00400772000-06-16 20:51:26 +00002097 case TK_CONCAT: {
drh5b6afba2008-01-05 16:29:28 +00002098 int r1, r2;
drhf2bc0132004-10-04 13:19:23 +00002099 assert( TK_AND==OP_And );
2100 assert( TK_OR==OP_Or );
2101 assert( TK_PLUS==OP_Add );
2102 assert( TK_MINUS==OP_Subtract );
2103 assert( TK_REM==OP_Remainder );
2104 assert( TK_BITAND==OP_BitAnd );
2105 assert( TK_BITOR==OP_BitOr );
2106 assert( TK_SLASH==OP_Divide );
2107 assert( TK_LSHIFT==OP_ShiftLeft );
2108 assert( TK_RSHIFT==OP_ShiftRight );
2109 assert( TK_CONCAT==OP_Concat );
drhaa9b8962008-01-08 02:57:55 +00002110 r1 = sqlite3ExprCode(pParse, pExpr->pLeft, -1);
2111 r2 = sqlite3ExprCode(pParse, pExpr->pRight, -1);
drh5b6afba2008-01-05 16:29:28 +00002112 sqlite3VdbeAddOp3(v, op, r2, r1, target);
drh5b6afba2008-01-05 16:29:28 +00002113 inReg = target;
drh00400772000-06-16 20:51:26 +00002114 break;
2115 }
drhcce7d172000-05-31 15:34:51 +00002116 case TK_UMINUS: {
drhfec19aa2004-05-19 20:41:03 +00002117 Expr *pLeft = pExpr->pLeft;
2118 assert( pLeft );
2119 if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
2120 Token *p = &pLeft->token;
drhfec19aa2004-05-19 20:41:03 +00002121 if( pLeft->op==TK_FLOAT ){
drh9de221d2008-01-05 06:51:30 +00002122 codeReal(v, (char*)p->z, p->n, 1, target);
drhe6840902002-03-06 03:08:25 +00002123 }else{
drh9de221d2008-01-05 06:51:30 +00002124 codeInteger(v, (char*)p->z, p->n, 1, target);
drhe6840902002-03-06 03:08:25 +00002125 }
drh3c84ddf2008-01-09 02:15:38 +00002126 }else{
2127 int r1 = ++pParse->nMem;
2128 sqlite3VdbeAddOp2(v, OP_Integer, 0, r1);
2129 sqlite3ExprCode(pParse, pExpr->pLeft, target);
2130 sqlite3VdbeAddOp3(v, OP_Subtract, target, r1, target);
drh6e142f52000-06-08 13:36:40 +00002131 }
drh3c84ddf2008-01-09 02:15:38 +00002132 inReg = target;
2133 break;
drh6e142f52000-06-08 13:36:40 +00002134 }
drhbf4133c2001-10-13 02:59:08 +00002135 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00002136 case TK_NOT: {
drhf2bc0132004-10-04 13:19:23 +00002137 assert( TK_BITNOT==OP_BitNot );
2138 assert( TK_NOT==OP_Not );
drh389a1ad2008-01-03 23:44:53 +00002139 sqlite3ExprCode(pParse, pExpr->pLeft, 0);
drh66a51672008-01-03 00:01:23 +00002140 sqlite3VdbeAddOp0(v, op);
drhcce7d172000-05-31 15:34:51 +00002141 break;
2142 }
2143 case TK_ISNULL:
2144 case TK_NOTNULL: {
drh6a288a32008-01-07 19:20:24 +00002145 int addr;
drhf2bc0132004-10-04 13:19:23 +00002146 assert( TK_ISNULL==OP_IsNull );
2147 assert( TK_NOTNULL==OP_NotNull );
drh9de221d2008-01-05 06:51:30 +00002148 sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
drh389a1ad2008-01-03 23:44:53 +00002149 sqlite3ExprCode(pParse, pExpr->pLeft, 0);
drh6a288a32008-01-07 19:20:24 +00002150 addr = sqlite3VdbeAddOp0(v, op);
drh9de221d2008-01-05 06:51:30 +00002151 sqlite3VdbeAddOp2(v, OP_AddImm, target, -1);
drh6a288a32008-01-07 19:20:24 +00002152 sqlite3VdbeJumpHere(v, addr);
drh9de221d2008-01-05 06:51:30 +00002153 inReg = target;
drhf2bc0132004-10-04 13:19:23 +00002154 break;
drhcce7d172000-05-31 15:34:51 +00002155 }
drh22827922000-06-06 17:27:05 +00002156 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00002157 AggInfo *pInfo = pExpr->pAggInfo;
drh7e56e712005-11-16 12:53:15 +00002158 if( pInfo==0 ){
2159 sqlite3ErrorMsg(pParse, "misuse of aggregate: %T",
2160 &pExpr->span);
2161 }else{
drh9de221d2008-01-05 06:51:30 +00002162 inReg = pInfo->aFunc[pExpr->iAgg].iMem;
drh7e56e712005-11-16 12:53:15 +00002163 }
drh22827922000-06-06 17:27:05 +00002164 break;
2165 }
drhb71090f2005-05-23 17:26:51 +00002166 case TK_CONST_FUNC:
drhcce7d172000-05-31 15:34:51 +00002167 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00002168 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00002169 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00002170 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00002171 int nId;
2172 const char *zId;
drh13449892005-09-07 21:22:45 +00002173 int constMask = 0;
danielk1977682f68b2004-06-05 10:22:17 +00002174 int i;
drh17435752007-08-16 04:30:38 +00002175 sqlite3 *db = pParse->db;
2176 u8 enc = ENC(db);
danielk1977dc1bdc42004-06-11 10:51:27 +00002177 CollSeq *pColl = 0;
drh17435752007-08-16 04:30:38 +00002178
drh2646da72005-12-09 20:02:05 +00002179 zId = (char*)pExpr->token.z;
drhb71090f2005-05-23 17:26:51 +00002180 nId = pExpr->token.n;
danielk1977d8123362004-06-12 09:25:12 +00002181 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0);
drh0bce8352002-02-28 00:41:10 +00002182 assert( pDef!=0 );
drh892d3172008-01-10 03:46:36 +00002183 if( pList ){
2184 nExpr = pList->nExpr;
2185 sqlite3ExprCodeExprList(pParse, pList, 0);
2186 }else{
2187 nExpr = 0;
2188 }
drhb7f6f682006-07-08 17:06:43 +00002189#ifndef SQLITE_OMIT_VIRTUALTABLE
drha43fa222006-07-08 18:41:37 +00002190 /* Possibly overload the function if the first argument is
2191 ** a virtual table column.
2192 **
2193 ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the
2194 ** second argument, not the first, as the argument to test to
2195 ** see if it is a column in a virtual table. This is done because
2196 ** the left operand of infix functions (the operand we want to
2197 ** control overloading) ends up as the second argument to the
2198 ** function. The expression "A glob B" is equivalent to
2199 ** "glob(B,A). We want to use the A in "A glob B" to test
2200 ** for function overloading. But we use the B term in "glob(B,A)".
2201 */
drh6a03a1c2006-07-08 18:34:59 +00002202 if( nExpr>=2 && (pExpr->flags & EP_InfixFunc) ){
drh17435752007-08-16 04:30:38 +00002203 pDef = sqlite3VtabOverloadFunction(db, pDef, nExpr, pList->a[1].pExpr);
drh6a03a1c2006-07-08 18:34:59 +00002204 }else if( nExpr>0 ){
drh17435752007-08-16 04:30:38 +00002205 pDef = sqlite3VtabOverloadFunction(db, pDef, nExpr, pList->a[0].pExpr);
drhb7f6f682006-07-08 17:06:43 +00002206 }
2207#endif
danielk1977682f68b2004-06-05 10:22:17 +00002208 for(i=0; i<nExpr && i<32; i++){
danielk1977d02eb1f2004-06-06 09:44:03 +00002209 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
drh13449892005-09-07 21:22:45 +00002210 constMask |= (1<<i);
danielk1977d02eb1f2004-06-06 09:44:03 +00002211 }
danielk1977dc1bdc42004-06-11 10:51:27 +00002212 if( pDef->needCollSeq && !pColl ){
2213 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
2214 }
2215 }
2216 if( pDef->needCollSeq ){
2217 if( !pColl ) pColl = pParse->db->pDfltColl;
drh66a51672008-01-03 00:01:23 +00002218 sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ);
danielk1977682f68b2004-06-05 10:22:17 +00002219 }
drh98757152008-01-09 23:04:12 +00002220 sqlite3VdbeAddOp4(v, OP_Function, constMask, 0, 0,
drh66a51672008-01-03 00:01:23 +00002221 (char*)pDef, P4_FUNCDEF);
drh98757152008-01-09 23:04:12 +00002222 sqlite3VdbeChangeP5(v, nExpr);
drhcce7d172000-05-31 15:34:51 +00002223 break;
2224 }
drhfe2093d2005-01-20 22:48:47 +00002225#ifndef SQLITE_OMIT_SUBQUERY
2226 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00002227 case TK_SELECT: {
drh41714d62006-03-02 04:44:23 +00002228 if( pExpr->iColumn==0 ){
2229 sqlite3CodeSubselect(pParse, pExpr);
2230 }
drh9de221d2008-01-05 06:51:30 +00002231 inReg = pExpr->iColumn;
drh19a775c2000-06-05 18:54:46 +00002232 break;
2233 }
drhfef52082000-06-06 01:50:43 +00002234 case TK_IN: {
drh6a288a32008-01-07 19:20:24 +00002235 int j1, j2, j3, j4, j5;
drh94a11212004-09-25 13:12:14 +00002236 char affinity;
danielk19779a96b662007-11-29 17:05:18 +00002237 int eType;
drh2d401ab2008-01-10 23:50:11 +00002238 int r1, r2, r3;
danielk19779a96b662007-11-29 17:05:18 +00002239
2240 eType = sqlite3FindInIndex(pParse, pExpr, 0);
danielk1977e014a832004-05-17 10:48:57 +00002241
2242 /* Figure out the affinity to use to create a key from the results
2243 ** of the expression. affinityStr stores a static string suitable for
drh66a51672008-01-03 00:01:23 +00002244 ** P4 of OP_MakeRecord.
danielk1977e014a832004-05-17 10:48:57 +00002245 */
drh94a11212004-09-25 13:12:14 +00002246 affinity = comparisonAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +00002247
drh2d401ab2008-01-10 23:50:11 +00002248 if( target ){
2249 r1 = target;
2250 }else{
2251 r1 = sqlite3GetTempReg(pParse);
2252 }
2253 inReg = r1;
2254 sqlite3VdbeAddOp2(v, OP_Integer, 1, r1);
danielk1977e014a832004-05-17 10:48:57 +00002255
2256 /* Code the <expr> from "<expr> IN (...)". The temporary table
2257 ** pExpr->iTable contains the values that make up the (...) set.
2258 */
drh2d401ab2008-01-10 23:50:11 +00002259 r2 = sqlite3ExprCode(pParse, pExpr->pLeft, -1);
2260 j1 = sqlite3VdbeAddOp1(v, OP_NotNull, r2);
2261 sqlite3VdbeAddOp2(v, OP_Null, 0, r1);
drh6a288a32008-01-07 19:20:24 +00002262 j2 = sqlite3VdbeAddOp0(v, OP_Goto);
2263 sqlite3VdbeJumpHere(v, j1);
danielk19779a96b662007-11-29 17:05:18 +00002264 if( eType==IN_INDEX_ROWID ){
drh2d401ab2008-01-10 23:50:11 +00002265 j3 = sqlite3VdbeAddOp3(v, OP_MustBeInt, r2, 0, 1);
2266 j4 = sqlite3VdbeAddOp3(v, OP_NotExists, pExpr->iTable, 0, r2);
drh6a288a32008-01-07 19:20:24 +00002267 j5 = sqlite3VdbeAddOp0(v, OP_Goto);
2268 sqlite3VdbeJumpHere(v, j3);
2269 sqlite3VdbeJumpHere(v, j4);
danielk19779a96b662007-11-29 17:05:18 +00002270 }else{
drh2d401ab2008-01-10 23:50:11 +00002271 r3 = sqlite3GetTempReg(pParse);
2272 sqlite3VdbeAddOp4(v, OP_RegMakeRec, r2, 1, r3, &affinity, 1);
2273 j5 = sqlite3VdbeAddOp3(v, OP_Found, pExpr->iTable, 0, r3);
2274 sqlite3ReleaseTempReg(pParse, r3);
danielk19779a96b662007-11-29 17:05:18 +00002275 }
drh2d401ab2008-01-10 23:50:11 +00002276 sqlite3VdbeAddOp2(v, OP_AddImm, r1, -1);
drh6a288a32008-01-07 19:20:24 +00002277 sqlite3VdbeJumpHere(v, j2);
2278 sqlite3VdbeJumpHere(v, j5);
drhfef52082000-06-06 01:50:43 +00002279 break;
2280 }
danielk197793758c82005-01-21 08:13:14 +00002281#endif
drhfef52082000-06-06 01:50:43 +00002282 case TK_BETWEEN: {
drhbe5c89a2004-07-26 00:31:09 +00002283 Expr *pLeft = pExpr->pLeft;
2284 struct ExprList_item *pLItem = pExpr->pList->a;
2285 Expr *pRight = pLItem->pExpr;
drh35573352008-01-08 23:54:25 +00002286 int r1, r2, r3, r4, r5;
2287
2288 if( target>0 ){
2289 inReg = target;
2290 }else{
2291 inReg = ++pParse->nMem;
2292 }
2293 r1 = sqlite3ExprCode(pParse, pLeft, -1);
2294 r2 = sqlite3ExprCode(pParse, pRight, -1);
2295 r3 = ++pParse->nMem;
2296 codeCompare(pParse, pLeft, pRight, OP_Ge,
2297 r1, r2, r3, SQLITE_STOREP2);
drhbe5c89a2004-07-26 00:31:09 +00002298 pLItem++;
2299 pRight = pLItem->pExpr;
drh35573352008-01-08 23:54:25 +00002300 r4 = sqlite3ExprCode(pParse, pRight, -1);
2301 r5 = ++pParse->nMem;
2302 codeCompare(pParse, pLeft, pRight, OP_Le, r1, r4, r5, SQLITE_STOREP2);
2303 sqlite3VdbeAddOp3(v, OP_And, r3, r5, inReg);
drhfef52082000-06-06 01:50:43 +00002304 break;
2305 }
drh4f07e5f2007-05-14 11:34:46 +00002306 case TK_UPLUS: {
drh9de221d2008-01-05 06:51:30 +00002307 inReg = sqlite3ExprCode(pParse, pExpr->pLeft, origTarget);
drha2e00042002-01-22 03:13:42 +00002308 break;
2309 }
drh17a7f8d2002-03-24 13:13:27 +00002310 case TK_CASE: {
2311 int expr_end_label;
drhf5905aa2002-05-26 20:54:33 +00002312 int jumpInst;
drhf5905aa2002-05-26 20:54:33 +00002313 int nExpr;
drh17a7f8d2002-03-24 13:13:27 +00002314 int i;
drhbe5c89a2004-07-26 00:31:09 +00002315 ExprList *pEList;
2316 struct ExprList_item *aListelem;
drh17a7f8d2002-03-24 13:13:27 +00002317
2318 assert(pExpr->pList);
2319 assert((pExpr->pList->nExpr % 2) == 0);
2320 assert(pExpr->pList->nExpr > 0);
drhbe5c89a2004-07-26 00:31:09 +00002321 pEList = pExpr->pList;
2322 aListelem = pEList->a;
2323 nExpr = pEList->nExpr;
danielk19774adee202004-05-08 08:23:19 +00002324 expr_end_label = sqlite3VdbeMakeLabel(v);
drh17a7f8d2002-03-24 13:13:27 +00002325 if( pExpr->pLeft ){
drh389a1ad2008-01-03 23:44:53 +00002326 sqlite3ExprCode(pParse, pExpr->pLeft, 0);
drh17a7f8d2002-03-24 13:13:27 +00002327 }
drhf5905aa2002-05-26 20:54:33 +00002328 for(i=0; i<nExpr; i=i+2){
drh389a1ad2008-01-03 23:44:53 +00002329 sqlite3ExprCode(pParse, aListelem[i].pExpr, 0);
drh17a7f8d2002-03-24 13:13:27 +00002330 if( pExpr->pLeft ){
drhb1fdb2a2008-01-05 04:06:03 +00002331 sqlite3VdbeAddOp1(v, OP_SCopy, -1);
drhbe5c89a2004-07-26 00:31:09 +00002332 jumpInst = codeCompare(pParse, pExpr->pLeft, aListelem[i].pExpr,
drh35573352008-01-08 23:54:25 +00002333 OP_Ne, 0, 0, 0, SQLITE_JUMPIFNULL);
drh66a51672008-01-03 00:01:23 +00002334 sqlite3VdbeAddOp1(v, OP_Pop, 1);
drh17a7f8d2002-03-24 13:13:27 +00002335 }else{
drh3c84ddf2008-01-09 02:15:38 +00002336 jumpInst = sqlite3VdbeAddOp3(v, OP_IfNot, 0, 0, 1);
drh17a7f8d2002-03-24 13:13:27 +00002337 }
drh9de221d2008-01-05 06:51:30 +00002338 sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target);
drh66a51672008-01-03 00:01:23 +00002339 sqlite3VdbeAddOp2(v, OP_Goto, 0, expr_end_label);
drhd654be82005-09-20 17:42:23 +00002340 sqlite3VdbeJumpHere(v, jumpInst);
drh17a7f8d2002-03-24 13:13:27 +00002341 }
drhf570f012002-05-31 15:51:25 +00002342 if( pExpr->pLeft ){
drh66a51672008-01-03 00:01:23 +00002343 sqlite3VdbeAddOp2(v, OP_Pop, 1, 0);
drhf570f012002-05-31 15:51:25 +00002344 }
drh17a7f8d2002-03-24 13:13:27 +00002345 if( pExpr->pRight ){
drh9de221d2008-01-05 06:51:30 +00002346 sqlite3ExprCode(pParse, pExpr->pRight, target);
drh17a7f8d2002-03-24 13:13:27 +00002347 }else{
drh9de221d2008-01-05 06:51:30 +00002348 sqlite3VdbeAddOp2(v, OP_Null, 0, target);
drh17a7f8d2002-03-24 13:13:27 +00002349 }
danielk19774adee202004-05-08 08:23:19 +00002350 sqlite3VdbeResolveLabel(v, expr_end_label);
drh9de221d2008-01-05 06:51:30 +00002351 inReg = target;
danielk19776f349032002-06-11 02:25:40 +00002352 break;
2353 }
danielk19775338a5f2005-01-20 13:03:10 +00002354#ifndef SQLITE_OMIT_TRIGGER
danielk19776f349032002-06-11 02:25:40 +00002355 case TK_RAISE: {
2356 if( !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +00002357 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00002358 "RAISE() may only be used within a trigger-program");
drh389a1ad2008-01-03 23:44:53 +00002359 return 0;
danielk19776f349032002-06-11 02:25:40 +00002360 }
drhad6d9462004-09-19 02:15:24 +00002361 if( pExpr->iColumn!=OE_Ignore ){
2362 assert( pExpr->iColumn==OE_Rollback ||
2363 pExpr->iColumn == OE_Abort ||
2364 pExpr->iColumn == OE_Fail );
danielk19771e536952007-08-16 10:09:01 +00002365 sqlite3DequoteExpr(pParse->db, pExpr);
drh66a51672008-01-03 00:01:23 +00002366 sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn, 0,
drh2646da72005-12-09 20:02:05 +00002367 (char*)pExpr->token.z, pExpr->token.n);
danielk19776f349032002-06-11 02:25:40 +00002368 } else {
drhad6d9462004-09-19 02:15:24 +00002369 assert( pExpr->iColumn == OE_Ignore );
drh66a51672008-01-03 00:01:23 +00002370 sqlite3VdbeAddOp2(v, OP_ContextPop, 0, 0);
2371 sqlite3VdbeAddOp2(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
drhd4e70eb2008-01-02 00:34:36 +00002372 VdbeComment((v, "raise(IGNORE)"));
danielk19776f349032002-06-11 02:25:40 +00002373 }
drhffe07b22005-11-03 00:41:17 +00002374 break;
drh17a7f8d2002-03-24 13:13:27 +00002375 }
danielk19775338a5f2005-01-20 13:03:10 +00002376#endif
drhffe07b22005-11-03 00:41:17 +00002377 }
drh5b6afba2008-01-05 16:29:28 +00002378 if( inReg!=target ){
2379 if( origTarget!=-1 ){
2380 sqlite3VdbeAddOp2(v, (inReg>0 ? OP_SCopy : OP_Move), inReg, target);
2381 }else{
2382 target = inReg;
2383 }
drhcce7d172000-05-31 15:34:51 +00002384 }
drh389a1ad2008-01-03 23:44:53 +00002385 return target;
drhcce7d172000-05-31 15:34:51 +00002386}
2387
danielk197793758c82005-01-21 08:13:14 +00002388#ifndef SQLITE_OMIT_TRIGGER
drhcce7d172000-05-31 15:34:51 +00002389/*
drh25303782004-12-07 15:41:48 +00002390** Generate code that evalutes the given expression and leaves the result
2391** on the stack. See also sqlite3ExprCode().
2392**
2393** This routine might also cache the result and modify the pExpr tree
2394** so that it will make use of the cached result on subsequent evaluations
2395** rather than evaluate the whole expression again. Trivial expressions are
2396** not cached. If the expression is cached, its result is stored in a
2397** memory location.
2398*/
drh2d401ab2008-01-10 23:50:11 +00002399void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr, int target){
drh25303782004-12-07 15:41:48 +00002400 Vdbe *v = pParse->pVdbe;
drh49df6b72007-12-14 15:12:21 +00002401 VdbeOp *pOp;
drh25303782004-12-07 15:41:48 +00002402 int iMem;
2403 int addr1, addr2;
2404 if( v==0 ) return;
2405 addr1 = sqlite3VdbeCurrentAddr(v);
drh2d401ab2008-01-10 23:50:11 +00002406 sqlite3ExprCode(pParse, pExpr, target);
drh25303782004-12-07 15:41:48 +00002407 addr2 = sqlite3VdbeCurrentAddr(v);
drh49df6b72007-12-14 15:12:21 +00002408 if( addr2>addr1+1
2409 || ((pOp = sqlite3VdbeGetOp(v, addr1))!=0 && pOp->opcode==OP_Function) ){
drh0a07c102008-01-03 18:03:08 +00002410 iMem = pExpr->iTable = ++pParse->nMem;
drh2d401ab2008-01-10 23:50:11 +00002411 sqlite3VdbeAddOp2(v, OP_Copy, target, iMem);
drh25303782004-12-07 15:41:48 +00002412 pExpr->op = TK_REGISTER;
2413 }
2414}
danielk197793758c82005-01-21 08:13:14 +00002415#endif
drh25303782004-12-07 15:41:48 +00002416
2417/*
drh268380c2004-02-25 13:47:31 +00002418** Generate code that pushes the value of every element of the given
drh892d3172008-01-10 03:46:36 +00002419** expression list onto the stack if target==0 or into a sequence of
2420** registers beginning at target.
drh268380c2004-02-25 13:47:31 +00002421**
drh892d3172008-01-10 03:46:36 +00002422** Return the number of elements evaluated.
drh268380c2004-02-25 13:47:31 +00002423*/
danielk19774adee202004-05-08 08:23:19 +00002424int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00002425 Parse *pParse, /* Parsing context */
drh389a1ad2008-01-03 23:44:53 +00002426 ExprList *pList, /* The expression list to be coded */
2427 int target /* Where to write results */
drh268380c2004-02-25 13:47:31 +00002428){
2429 struct ExprList_item *pItem;
drh389a1ad2008-01-03 23:44:53 +00002430 int i, n, incr = 1;
drh892d3172008-01-10 03:46:36 +00002431 assert( pList!=0 || pParse->db->mallocFailed );
2432 if( pList==0 ){
2433 return 0;
2434 }
2435 assert( target>=0 );
drh268380c2004-02-25 13:47:31 +00002436 n = pList->nExpr;
drh892d3172008-01-10 03:46:36 +00002437 if( target==0 ){
drh389a1ad2008-01-03 23:44:53 +00002438 incr = 0;
2439 }
drhc182d162005-08-14 20:47:16 +00002440 for(pItem=pList->a, i=n; i>0; i--, pItem++){
drh389a1ad2008-01-03 23:44:53 +00002441 sqlite3ExprCode(pParse, pItem->pExpr, target);
2442 target += incr;
drh268380c2004-02-25 13:47:31 +00002443 }
drhf9b596e2004-05-26 16:54:42 +00002444 return n;
drh268380c2004-02-25 13:47:31 +00002445}
2446
2447/*
drhcce7d172000-05-31 15:34:51 +00002448** Generate code for a boolean expression such that a jump is made
2449** to the label "dest" if the expression is true but execution
2450** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00002451**
2452** If the expression evaluates to NULL (neither true nor false), then
drh35573352008-01-08 23:54:25 +00002453** take the jump if the jumpIfNull flag is SQLITE_JUMPIFNULL.
drhf2bc0132004-10-04 13:19:23 +00002454**
2455** This code depends on the fact that certain token values (ex: TK_EQ)
2456** are the same as opcode values (ex: OP_Eq) that implement the corresponding
2457** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
2458** the make process cause these values to align. Assert()s in the code
2459** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00002460*/
danielk19774adee202004-05-08 08:23:19 +00002461void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00002462 Vdbe *v = pParse->pVdbe;
2463 int op = 0;
drh35573352008-01-08 23:54:25 +00002464 assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
drhdaffd0e2001-04-11 14:28:42 +00002465 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00002466 op = pExpr->op;
2467 switch( op ){
drhcce7d172000-05-31 15:34:51 +00002468 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00002469 int d2 = sqlite3VdbeMakeLabel(v);
drh35573352008-01-08 23:54:25 +00002470 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,jumpIfNull^SQLITE_JUMPIFNULL);
danielk19774adee202004-05-08 08:23:19 +00002471 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
2472 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00002473 break;
2474 }
2475 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00002476 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
2477 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002478 break;
2479 }
2480 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00002481 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002482 break;
2483 }
2484 case TK_LT:
2485 case TK_LE:
2486 case TK_GT:
2487 case TK_GE:
2488 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00002489 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00002490 assert( TK_LT==OP_Lt );
2491 assert( TK_LE==OP_Le );
2492 assert( TK_GT==OP_Gt );
2493 assert( TK_GE==OP_Ge );
2494 assert( TK_EQ==OP_Eq );
2495 assert( TK_NE==OP_Ne );
drh389a1ad2008-01-03 23:44:53 +00002496 sqlite3ExprCode(pParse, pExpr->pLeft, 0);
2497 sqlite3ExprCode(pParse, pExpr->pRight, 0);
drh35573352008-01-08 23:54:25 +00002498 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
2499 0, 0, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002500 break;
2501 }
2502 case TK_ISNULL:
2503 case TK_NOTNULL: {
drhf2bc0132004-10-04 13:19:23 +00002504 assert( TK_ISNULL==OP_IsNull );
2505 assert( TK_NOTNULL==OP_NotNull );
drh389a1ad2008-01-03 23:44:53 +00002506 sqlite3ExprCode(pParse, pExpr->pLeft, 0);
drh6a288a32008-01-07 19:20:24 +00002507 sqlite3VdbeAddOp2(v, op, 0, dest);
drhcce7d172000-05-31 15:34:51 +00002508 break;
2509 }
drhfef52082000-06-06 01:50:43 +00002510 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00002511 /* The expression "x BETWEEN y AND z" is implemented as:
2512 **
2513 ** 1 IF (x < y) GOTO 3
2514 ** 2 IF (x <= z) GOTO <dest>
2515 ** 3 ...
2516 */
drhf5905aa2002-05-26 20:54:33 +00002517 int addr;
drhbe5c89a2004-07-26 00:31:09 +00002518 Expr *pLeft = pExpr->pLeft;
2519 Expr *pRight = pExpr->pList->a[0].pExpr;
drh389a1ad2008-01-03 23:44:53 +00002520 sqlite3ExprCode(pParse, pLeft, 0);
drhb1fdb2a2008-01-05 04:06:03 +00002521 sqlite3VdbeAddOp0(v, OP_Copy);
drh389a1ad2008-01-03 23:44:53 +00002522 sqlite3ExprCode(pParse, pRight, 0);
drh35573352008-01-08 23:54:25 +00002523 addr = codeCompare(pParse, pLeft, pRight, OP_Lt, 0, 0, 0,
2524 jumpIfNull ^ SQLITE_JUMPIFNULL);
danielk19770202b292004-06-09 09:55:16 +00002525
drhbe5c89a2004-07-26 00:31:09 +00002526 pRight = pExpr->pList->a[1].pExpr;
drh389a1ad2008-01-03 23:44:53 +00002527 sqlite3ExprCode(pParse, pRight, 0);
drh35573352008-01-08 23:54:25 +00002528 codeCompare(pParse, pLeft, pRight, OP_Le, 0, 0, dest, jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00002529
drh66a51672008-01-03 00:01:23 +00002530 sqlite3VdbeAddOp2(v, OP_Integer, 0, 0);
drhd654be82005-09-20 17:42:23 +00002531 sqlite3VdbeJumpHere(v, addr);
drh66a51672008-01-03 00:01:23 +00002532 sqlite3VdbeAddOp2(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00002533 break;
2534 }
drhcce7d172000-05-31 15:34:51 +00002535 default: {
drh389a1ad2008-01-03 23:44:53 +00002536 sqlite3ExprCode(pParse, pExpr, 0);
drh3c84ddf2008-01-09 02:15:38 +00002537 sqlite3VdbeAddOp3(v, OP_If, 0, dest, jumpIfNull!=0);
drhcce7d172000-05-31 15:34:51 +00002538 break;
2539 }
2540 }
2541}
2542
2543/*
drh66b89c82000-11-28 20:47:17 +00002544** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00002545** to the label "dest" if the expression is false but execution
2546** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00002547**
2548** If the expression evaluates to NULL (neither true nor false) then
drh35573352008-01-08 23:54:25 +00002549** jump if jumpIfNull is SQLITE_JUMPIFNULL or fall through if jumpIfNull
2550** is 0.
drhcce7d172000-05-31 15:34:51 +00002551*/
danielk19774adee202004-05-08 08:23:19 +00002552void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00002553 Vdbe *v = pParse->pVdbe;
2554 int op = 0;
drh35573352008-01-08 23:54:25 +00002555 assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
drhdaffd0e2001-04-11 14:28:42 +00002556 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00002557
2558 /* The value of pExpr->op and op are related as follows:
2559 **
2560 ** pExpr->op op
2561 ** --------- ----------
2562 ** TK_ISNULL OP_NotNull
2563 ** TK_NOTNULL OP_IsNull
2564 ** TK_NE OP_Eq
2565 ** TK_EQ OP_Ne
2566 ** TK_GT OP_Le
2567 ** TK_LE OP_Gt
2568 ** TK_GE OP_Lt
2569 ** TK_LT OP_Ge
2570 **
2571 ** For other values of pExpr->op, op is undefined and unused.
2572 ** The value of TK_ and OP_ constants are arranged such that we
2573 ** can compute the mapping above using the following expression.
2574 ** Assert()s verify that the computation is correct.
2575 */
2576 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
2577
2578 /* Verify correct alignment of TK_ and OP_ constants
2579 */
2580 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
2581 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
2582 assert( pExpr->op!=TK_NE || op==OP_Eq );
2583 assert( pExpr->op!=TK_EQ || op==OP_Ne );
2584 assert( pExpr->op!=TK_LT || op==OP_Ge );
2585 assert( pExpr->op!=TK_LE || op==OP_Gt );
2586 assert( pExpr->op!=TK_GT || op==OP_Le );
2587 assert( pExpr->op!=TK_GE || op==OP_Lt );
2588
drhcce7d172000-05-31 15:34:51 +00002589 switch( pExpr->op ){
2590 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00002591 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
2592 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002593 break;
2594 }
2595 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00002596 int d2 = sqlite3VdbeMakeLabel(v);
drh35573352008-01-08 23:54:25 +00002597 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, jumpIfNull^SQLITE_JUMPIFNULL);
danielk19774adee202004-05-08 08:23:19 +00002598 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
2599 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00002600 break;
2601 }
2602 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00002603 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002604 break;
2605 }
2606 case TK_LT:
2607 case TK_LE:
2608 case TK_GT:
2609 case TK_GE:
2610 case TK_NE:
2611 case TK_EQ: {
drh389a1ad2008-01-03 23:44:53 +00002612 sqlite3ExprCode(pParse, pExpr->pLeft, 0);
2613 sqlite3ExprCode(pParse, pExpr->pRight, 0);
drh35573352008-01-08 23:54:25 +00002614 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
2615 0, 0, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002616 break;
2617 }
drhcce7d172000-05-31 15:34:51 +00002618 case TK_ISNULL:
2619 case TK_NOTNULL: {
drh389a1ad2008-01-03 23:44:53 +00002620 sqlite3ExprCode(pParse, pExpr->pLeft, 0);
drh6a288a32008-01-07 19:20:24 +00002621 sqlite3VdbeAddOp2(v, op, 0, dest);
drhcce7d172000-05-31 15:34:51 +00002622 break;
2623 }
drhfef52082000-06-06 01:50:43 +00002624 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00002625 /* The expression is "x BETWEEN y AND z". It is implemented as:
2626 **
2627 ** 1 IF (x >= y) GOTO 3
2628 ** 2 GOTO <dest>
2629 ** 3 IF (x > z) GOTO <dest>
2630 */
drhfef52082000-06-06 01:50:43 +00002631 int addr;
drhbe5c89a2004-07-26 00:31:09 +00002632 Expr *pLeft = pExpr->pLeft;
2633 Expr *pRight = pExpr->pList->a[0].pExpr;
drh389a1ad2008-01-03 23:44:53 +00002634 sqlite3ExprCode(pParse, pLeft, 0);
drhb1fdb2a2008-01-05 04:06:03 +00002635 sqlite3VdbeAddOp0(v, OP_Copy);
drh389a1ad2008-01-03 23:44:53 +00002636 sqlite3ExprCode(pParse, pRight, 0);
danielk19774adee202004-05-08 08:23:19 +00002637 addr = sqlite3VdbeCurrentAddr(v);
drh35573352008-01-08 23:54:25 +00002638 codeCompare(pParse, pLeft, pRight, OP_Ge,
2639 0, 0, addr+3, jumpIfNull ^ SQLITE_JUMPIFNULL);
drhbe5c89a2004-07-26 00:31:09 +00002640
drh66a51672008-01-03 00:01:23 +00002641 sqlite3VdbeAddOp2(v, OP_Pop, 1, 0);
2642 sqlite3VdbeAddOp2(v, OP_Goto, 0, dest);
drhbe5c89a2004-07-26 00:31:09 +00002643 pRight = pExpr->pList->a[1].pExpr;
drh389a1ad2008-01-03 23:44:53 +00002644 sqlite3ExprCode(pParse, pRight, 0);
drh35573352008-01-08 23:54:25 +00002645 codeCompare(pParse, pLeft, pRight, OP_Gt,
2646 0, 0, dest, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00002647 break;
2648 }
drhcce7d172000-05-31 15:34:51 +00002649 default: {
drh389a1ad2008-01-03 23:44:53 +00002650 sqlite3ExprCode(pParse, pExpr, 0);
drh3c84ddf2008-01-09 02:15:38 +00002651 sqlite3VdbeAddOp3(v, OP_IfNot, 0, dest, jumpIfNull!=0);
drhcce7d172000-05-31 15:34:51 +00002652 break;
2653 }
2654 }
2655}
drh22827922000-06-06 17:27:05 +00002656
2657/*
2658** Do a deep comparison of two expression trees. Return TRUE (non-zero)
2659** if they are identical and return FALSE if they differ in any way.
drhd40aab02007-02-24 15:29:03 +00002660**
2661** Sometimes this routine will return FALSE even if the two expressions
2662** really are equivalent. If we cannot prove that the expressions are
2663** identical, we return FALSE just to be safe. So if this routine
2664** returns false, then you do not really know for certain if the two
2665** expressions are the same. But if you get a TRUE return, then you
2666** can be sure the expressions are the same. In the places where
2667** this routine is used, it does not hurt to get an extra FALSE - that
2668** just might result in some slightly slower code. But returning
2669** an incorrect TRUE could lead to a malfunction.
drh22827922000-06-06 17:27:05 +00002670*/
danielk19774adee202004-05-08 08:23:19 +00002671int sqlite3ExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00002672 int i;
danielk19774b202ae2006-01-23 05:50:58 +00002673 if( pA==0||pB==0 ){
2674 return pB==pA;
drh22827922000-06-06 17:27:05 +00002675 }
2676 if( pA->op!=pB->op ) return 0;
drhfd357972005-09-09 01:33:19 +00002677 if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 0;
danielk19774adee202004-05-08 08:23:19 +00002678 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
2679 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00002680 if( pA->pList ){
2681 if( pB->pList==0 ) return 0;
2682 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
2683 for(i=0; i<pA->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00002684 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00002685 return 0;
2686 }
2687 }
2688 }else if( pB->pList ){
2689 return 0;
2690 }
2691 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00002692 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drhdd735212007-02-24 13:53:05 +00002693 if( pA->op!=TK_COLUMN && pA->token.z ){
drh22827922000-06-06 17:27:05 +00002694 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00002695 if( pB->token.n!=pA->token.n ) return 0;
drh2646da72005-12-09 20:02:05 +00002696 if( sqlite3StrNICmp((char*)pA->token.z,(char*)pB->token.z,pB->token.n)!=0 ){
2697 return 0;
2698 }
drh22827922000-06-06 17:27:05 +00002699 }
2700 return 1;
2701}
2702
drh13449892005-09-07 21:22:45 +00002703
drh22827922000-06-06 17:27:05 +00002704/*
drh13449892005-09-07 21:22:45 +00002705** Add a new element to the pAggInfo->aCol[] array. Return the index of
2706** the new element. Return a negative number if malloc fails.
drh22827922000-06-06 17:27:05 +00002707*/
drh17435752007-08-16 04:30:38 +00002708static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){
drh13449892005-09-07 21:22:45 +00002709 int i;
drhcf643722007-03-27 13:36:37 +00002710 pInfo->aCol = sqlite3ArrayAllocate(
drh17435752007-08-16 04:30:38 +00002711 db,
drhcf643722007-03-27 13:36:37 +00002712 pInfo->aCol,
2713 sizeof(pInfo->aCol[0]),
2714 3,
2715 &pInfo->nColumn,
2716 &pInfo->nColumnAlloc,
2717 &i
2718 );
drh13449892005-09-07 21:22:45 +00002719 return i;
2720}
2721
2722/*
2723** Add a new element to the pAggInfo->aFunc[] array. Return the index of
2724** the new element. Return a negative number if malloc fails.
2725*/
drh17435752007-08-16 04:30:38 +00002726static int addAggInfoFunc(sqlite3 *db, AggInfo *pInfo){
drh13449892005-09-07 21:22:45 +00002727 int i;
drhcf643722007-03-27 13:36:37 +00002728 pInfo->aFunc = sqlite3ArrayAllocate(
drh17435752007-08-16 04:30:38 +00002729 db,
drhcf643722007-03-27 13:36:37 +00002730 pInfo->aFunc,
2731 sizeof(pInfo->aFunc[0]),
2732 3,
2733 &pInfo->nFunc,
2734 &pInfo->nFuncAlloc,
2735 &i
2736 );
drh13449892005-09-07 21:22:45 +00002737 return i;
2738}
drh22827922000-06-06 17:27:05 +00002739
2740/*
drh626a8792005-01-17 22:08:19 +00002741** This is an xFunc for walkExprTree() used to implement
2742** sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates
2743** for additional information.
drh22827922000-06-06 17:27:05 +00002744**
drh626a8792005-01-17 22:08:19 +00002745** This routine analyzes the aggregate function at pExpr.
drh22827922000-06-06 17:27:05 +00002746*/
drh626a8792005-01-17 22:08:19 +00002747static int analyzeAggregate(void *pArg, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00002748 int i;
danielk1977a58fdfb2005-02-08 07:50:40 +00002749 NameContext *pNC = (NameContext *)pArg;
2750 Parse *pParse = pNC->pParse;
2751 SrcList *pSrcList = pNC->pSrcList;
drh13449892005-09-07 21:22:45 +00002752 AggInfo *pAggInfo = pNC->pAggInfo;
drh22827922000-06-06 17:27:05 +00002753
drh22827922000-06-06 17:27:05 +00002754 switch( pExpr->op ){
drh89c69d02007-01-04 01:20:28 +00002755 case TK_AGG_COLUMN:
drh967e8b72000-06-21 13:59:10 +00002756 case TK_COLUMN: {
drh13449892005-09-07 21:22:45 +00002757 /* Check to see if the column is in one of the tables in the FROM
2758 ** clause of the aggregate query */
2759 if( pSrcList ){
2760 struct SrcList_item *pItem = pSrcList->a;
2761 for(i=0; i<pSrcList->nSrc; i++, pItem++){
2762 struct AggInfo_col *pCol;
2763 if( pExpr->iTable==pItem->iCursor ){
2764 /* If we reach this point, it means that pExpr refers to a table
2765 ** that is in the FROM clause of the aggregate query.
2766 **
2767 ** Make an entry for the column in pAggInfo->aCol[] if there
2768 ** is not an entry there already.
2769 */
drh7f906d62007-03-12 23:48:52 +00002770 int k;
drh13449892005-09-07 21:22:45 +00002771 pCol = pAggInfo->aCol;
drh7f906d62007-03-12 23:48:52 +00002772 for(k=0; k<pAggInfo->nColumn; k++, pCol++){
drh13449892005-09-07 21:22:45 +00002773 if( pCol->iTable==pExpr->iTable &&
2774 pCol->iColumn==pExpr->iColumn ){
2775 break;
2776 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002777 }
danielk19771e536952007-08-16 10:09:01 +00002778 if( (k>=pAggInfo->nColumn)
2779 && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0
2780 ){
drh7f906d62007-03-12 23:48:52 +00002781 pCol = &pAggInfo->aCol[k];
danielk19770817d0d2007-02-14 09:19:36 +00002782 pCol->pTab = pExpr->pTab;
drh13449892005-09-07 21:22:45 +00002783 pCol->iTable = pExpr->iTable;
2784 pCol->iColumn = pExpr->iColumn;
drh0a07c102008-01-03 18:03:08 +00002785 pCol->iMem = ++pParse->nMem;
drh13449892005-09-07 21:22:45 +00002786 pCol->iSorterColumn = -1;
drh5774b802005-09-07 22:48:16 +00002787 pCol->pExpr = pExpr;
drh13449892005-09-07 21:22:45 +00002788 if( pAggInfo->pGroupBy ){
2789 int j, n;
2790 ExprList *pGB = pAggInfo->pGroupBy;
2791 struct ExprList_item *pTerm = pGB->a;
2792 n = pGB->nExpr;
2793 for(j=0; j<n; j++, pTerm++){
2794 Expr *pE = pTerm->pExpr;
2795 if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable &&
2796 pE->iColumn==pExpr->iColumn ){
2797 pCol->iSorterColumn = j;
2798 break;
2799 }
2800 }
2801 }
2802 if( pCol->iSorterColumn<0 ){
2803 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
2804 }
2805 }
2806 /* There is now an entry for pExpr in pAggInfo->aCol[] (either
2807 ** because it was there before or because we just created it).
2808 ** Convert the pExpr to be a TK_AGG_COLUMN referring to that
2809 ** pAggInfo->aCol[] entry.
2810 */
2811 pExpr->pAggInfo = pAggInfo;
2812 pExpr->op = TK_AGG_COLUMN;
drh7f906d62007-03-12 23:48:52 +00002813 pExpr->iAgg = k;
drh13449892005-09-07 21:22:45 +00002814 break;
2815 } /* endif pExpr->iTable==pItem->iCursor */
2816 } /* end loop over pSrcList */
drh22827922000-06-06 17:27:05 +00002817 }
drh626a8792005-01-17 22:08:19 +00002818 return 1;
drh22827922000-06-06 17:27:05 +00002819 }
2820 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00002821 /* The pNC->nDepth==0 test causes aggregate functions in subqueries
2822 ** to be ignored */
danielk1977a58fdfb2005-02-08 07:50:40 +00002823 if( pNC->nDepth==0 ){
drh13449892005-09-07 21:22:45 +00002824 /* Check to see if pExpr is a duplicate of another aggregate
2825 ** function that is already in the pAggInfo structure
2826 */
2827 struct AggInfo_func *pItem = pAggInfo->aFunc;
2828 for(i=0; i<pAggInfo->nFunc; i++, pItem++){
2829 if( sqlite3ExprCompare(pItem->pExpr, pExpr) ){
danielk1977a58fdfb2005-02-08 07:50:40 +00002830 break;
2831 }
drh22827922000-06-06 17:27:05 +00002832 }
drh13449892005-09-07 21:22:45 +00002833 if( i>=pAggInfo->nFunc ){
2834 /* pExpr is original. Make a new entry in pAggInfo->aFunc[]
2835 */
danielk197714db2662006-01-09 16:12:04 +00002836 u8 enc = ENC(pParse->db);
danielk19771e536952007-08-16 10:09:01 +00002837 i = addAggInfoFunc(pParse->db, pAggInfo);
drh13449892005-09-07 21:22:45 +00002838 if( i>=0 ){
2839 pItem = &pAggInfo->aFunc[i];
2840 pItem->pExpr = pExpr;
drh0a07c102008-01-03 18:03:08 +00002841 pItem->iMem = ++pParse->nMem;
drh13449892005-09-07 21:22:45 +00002842 pItem->pFunc = sqlite3FindFunction(pParse->db,
drh2646da72005-12-09 20:02:05 +00002843 (char*)pExpr->token.z, pExpr->token.n,
drh13449892005-09-07 21:22:45 +00002844 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
drhfd357972005-09-09 01:33:19 +00002845 if( pExpr->flags & EP_Distinct ){
2846 pItem->iDistinct = pParse->nTab++;
2847 }else{
2848 pItem->iDistinct = -1;
2849 }
drh13449892005-09-07 21:22:45 +00002850 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002851 }
drh13449892005-09-07 21:22:45 +00002852 /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
2853 */
danielk1977a58fdfb2005-02-08 07:50:40 +00002854 pExpr->iAgg = i;
drh13449892005-09-07 21:22:45 +00002855 pExpr->pAggInfo = pAggInfo;
danielk1977a58fdfb2005-02-08 07:50:40 +00002856 return 1;
drh22827922000-06-06 17:27:05 +00002857 }
drh22827922000-06-06 17:27:05 +00002858 }
2859 }
drh13449892005-09-07 21:22:45 +00002860
2861 /* Recursively walk subqueries looking for TK_COLUMN nodes that need
2862 ** to be changed to TK_AGG_COLUMN. But increment nDepth so that
2863 ** TK_AGG_FUNCTION nodes in subqueries will be unchanged.
2864 */
danielk1977a58fdfb2005-02-08 07:50:40 +00002865 if( pExpr->pSelect ){
2866 pNC->nDepth++;
2867 walkSelectExpr(pExpr->pSelect, analyzeAggregate, pNC);
2868 pNC->nDepth--;
2869 }
drh626a8792005-01-17 22:08:19 +00002870 return 0;
2871}
2872
2873/*
2874** Analyze the given expression looking for aggregate functions and
2875** for variables that need to be added to the pParse->aAgg[] array.
2876** Make additional entries to the pParse->aAgg[] array as necessary.
2877**
2878** This routine should only be called after the expression has been
2879** analyzed by sqlite3ExprResolveNames().
2880**
2881** If errors are seen, leave an error message in zErrMsg and return
2882** the number of errors.
2883*/
danielk1977a58fdfb2005-02-08 07:50:40 +00002884int sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
2885 int nErr = pNC->pParse->nErr;
2886 walkExprTree(pExpr, analyzeAggregate, pNC);
2887 return pNC->pParse->nErr - nErr;
drh22827922000-06-06 17:27:05 +00002888}
drh5d9a4af2005-08-30 00:54:01 +00002889
2890/*
2891** Call sqlite3ExprAnalyzeAggregates() for every expression in an
2892** expression list. Return the number of errors.
2893**
2894** If an error is found, the analysis is cut short.
2895*/
2896int sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){
2897 struct ExprList_item *pItem;
2898 int i;
2899 int nErr = 0;
2900 if( pList ){
2901 for(pItem=pList->a, i=0; nErr==0 && i<pList->nExpr; i++, pItem++){
2902 nErr += sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
2903 }
2904 }
2905 return nErr;
2906}
drh892d3172008-01-10 03:46:36 +00002907
2908/*
2909** Allocate or deallocate temporary use registers during code generation.
2910*/
2911int sqlite3GetTempReg(Parse *pParse){
2912 if( pParse->nTempReg ){
2913 return pParse->aTempReg[--pParse->nTempReg];
2914 }else{
2915 return ++pParse->nMem;
2916 }
2917}
2918void sqlite3ReleaseTempReg(Parse *pParse, int iReg){
2919 if( pParse->nTempReg<sizeof(pParse->aTempReg)/sizeof(pParse->aTempReg[0]) ){
2920 pParse->aTempReg[pParse->nTempReg++] = iReg;
2921 }
2922}
2923
2924/*
2925** Allocate or deallocate a block of nReg consecutive registers
2926*/
2927int sqlite3GetTempRange(Parse *pParse, int nReg){
2928 int i;
2929 if( nReg<=pParse->nRangeReg ){
2930 i = pParse->iRangeReg;
2931 pParse->iRangeReg += nReg;
2932 pParse->nRangeReg -= nReg;
2933 }else{
2934 i = pParse->nMem+1;
2935 pParse->nMem += nReg;
2936 }
2937 return i;
2938}
2939void sqlite3ReleaseTempRange(Parse *pParse, int iReg, int nReg){
2940 if( nReg>pParse->nRangeReg ){
2941 pParse->nRangeReg = nReg;
2942 pParse->iRangeReg = iReg;
2943 }
2944}