blob: b332f4531a042b7edf2bc0baecaea8e8e21573be [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**
drh3c84ddf2008-01-09 02:15:38 +000015** $Id: expr.c,v 1.339 2008/01/09 02:15:39 drh Exp $
drhcce7d172000-05-31 15:34:51 +000016*/
17#include "sqliteInt.h"
drh04738cb2002-06-02 18:19:00 +000018#include <ctype.h>
drha2e00042002-01-22 03:13:42 +000019
danielk1977e014a832004-05-17 10:48:57 +000020/*
21** Return the 'affinity' of the expression pExpr if any.
22**
23** If pExpr is a column, a reference to a column via an 'AS' alias,
24** or a sub-select with a column as the return value, then the
25** affinity of that column is returned. Otherwise, 0x00 is returned,
26** indicating no affinity for the expression.
27**
28** i.e. the WHERE clause expresssions in the following statements all
29** have an affinity:
30**
31** CREATE TABLE t1(a);
32** SELECT * FROM t1 WHERE a;
33** SELECT a AS b FROM t1 WHERE b;
34** SELECT * FROM t1 WHERE (select a from t1);
35*/
danielk1977bf3b7212004-05-18 10:06:24 +000036char sqlite3ExprAffinity(Expr *pExpr){
drh487e2622005-06-25 18:42:14 +000037 int op = pExpr->op;
drh487e2622005-06-25 18:42:14 +000038 if( op==TK_SELECT ){
danielk1977bf3b7212004-05-18 10:06:24 +000039 return sqlite3ExprAffinity(pExpr->pSelect->pEList->a[0].pExpr);
danielk1977a37cdde2004-05-16 11:15:36 +000040 }
drh487e2622005-06-25 18:42:14 +000041#ifndef SQLITE_OMIT_CAST
42 if( op==TK_CAST ){
drh8a512562005-11-14 22:29:05 +000043 return sqlite3AffinityType(&pExpr->token);
drh487e2622005-06-25 18:42:14 +000044 }
45#endif
danielk1977a37cdde2004-05-16 11:15:36 +000046 return pExpr->affinity;
47}
48
drh53db1452004-05-20 13:54:53 +000049/*
drh8b4c40d2007-02-01 23:02:45 +000050** Set the collating sequence for expression pExpr to be the collating
51** sequence named by pToken. Return a pointer to the revised expression.
drha34001c2007-02-02 12:44:37 +000052** The collating sequence is marked as "explicit" using the EP_ExpCollate
53** flag. An explicit collating sequence will override implicit
54** collating sequences.
drh8b4c40d2007-02-01 23:02:45 +000055*/
56Expr *sqlite3ExprSetColl(Parse *pParse, Expr *pExpr, Token *pName){
danielk197739002502007-11-12 09:50:26 +000057 char *zColl = 0; /* Dequoted name of collation sequence */
drh8b4c40d2007-02-01 23:02:45 +000058 CollSeq *pColl;
danielk197739002502007-11-12 09:50:26 +000059 zColl = sqlite3NameFromToken(pParse->db, pName);
60 if( pExpr && zColl ){
61 pColl = sqlite3LocateCollSeq(pParse, zColl, -1);
62 if( pColl ){
63 pExpr->pColl = pColl;
64 pExpr->flags |= EP_ExpCollate;
65 }
drh8b4c40d2007-02-01 23:02:45 +000066 }
danielk197739002502007-11-12 09:50:26 +000067 sqlite3_free(zColl);
drh8b4c40d2007-02-01 23:02:45 +000068 return pExpr;
69}
70
71/*
danielk19770202b292004-06-09 09:55:16 +000072** Return the default collation sequence for the expression pExpr. If
73** there is no default collation type, return 0.
74*/
danielk19777cedc8d2004-06-10 10:50:08 +000075CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
76 CollSeq *pColl = 0;
danielk19770202b292004-06-09 09:55:16 +000077 if( pExpr ){
drh7e09fe02007-06-20 16:13:23 +000078 int op;
danielk19777cedc8d2004-06-10 10:50:08 +000079 pColl = pExpr->pColl;
drh7e09fe02007-06-20 16:13:23 +000080 op = pExpr->op;
81 if( (op==TK_CAST || op==TK_UPLUS) && !pColl ){
danielk19777cedc8d2004-06-10 10:50:08 +000082 return sqlite3ExprCollSeq(pParse, pExpr->pLeft);
danielk19770202b292004-06-09 09:55:16 +000083 }
84 }
danielk19777cedc8d2004-06-10 10:50:08 +000085 if( sqlite3CheckCollSeq(pParse, pColl) ){
86 pColl = 0;
87 }
88 return pColl;
danielk19770202b292004-06-09 09:55:16 +000089}
90
91/*
drh626a8792005-01-17 22:08:19 +000092** pExpr is an operand of a comparison operator. aff2 is the
93** type affinity of the other operand. This routine returns the
drh53db1452004-05-20 13:54:53 +000094** type affinity that should be used for the comparison operator.
95*/
danielk1977e014a832004-05-17 10:48:57 +000096char sqlite3CompareAffinity(Expr *pExpr, char aff2){
danielk1977bf3b7212004-05-18 10:06:24 +000097 char aff1 = sqlite3ExprAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +000098 if( aff1 && aff2 ){
drh8df447f2005-11-01 15:48:24 +000099 /* Both sides of the comparison are columns. If one has numeric
100 ** affinity, use that. Otherwise use no affinity.
danielk1977e014a832004-05-17 10:48:57 +0000101 */
drh8a512562005-11-14 22:29:05 +0000102 if( sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2) ){
danielk1977e014a832004-05-17 10:48:57 +0000103 return SQLITE_AFF_NUMERIC;
104 }else{
105 return SQLITE_AFF_NONE;
106 }
107 }else if( !aff1 && !aff2 ){
drh5f6a87b2004-07-19 00:39:45 +0000108 /* Neither side of the comparison is a column. Compare the
109 ** results directly.
danielk1977e014a832004-05-17 10:48:57 +0000110 */
drh5f6a87b2004-07-19 00:39:45 +0000111 return SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +0000112 }else{
113 /* One side is a column, the other is not. Use the columns affinity. */
drhfe05af82005-07-21 03:14:59 +0000114 assert( aff1==0 || aff2==0 );
danielk1977e014a832004-05-17 10:48:57 +0000115 return (aff1 + aff2);
116 }
117}
118
drh53db1452004-05-20 13:54:53 +0000119/*
120** pExpr is a comparison operator. Return the type affinity that should
121** be applied to both operands prior to doing the comparison.
122*/
danielk1977e014a832004-05-17 10:48:57 +0000123static char comparisonAffinity(Expr *pExpr){
124 char aff;
125 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
126 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
127 pExpr->op==TK_NE );
128 assert( pExpr->pLeft );
danielk1977bf3b7212004-05-18 10:06:24 +0000129 aff = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +0000130 if( pExpr->pRight ){
131 aff = sqlite3CompareAffinity(pExpr->pRight, aff);
132 }
133 else if( pExpr->pSelect ){
134 aff = sqlite3CompareAffinity(pExpr->pSelect->pEList->a[0].pExpr, aff);
135 }
136 else if( !aff ){
drhde087bd2007-02-23 03:00:44 +0000137 aff = SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +0000138 }
139 return aff;
140}
141
142/*
143** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
144** idx_affinity is the affinity of an indexed column. Return true
145** if the index with affinity idx_affinity may be used to implement
146** the comparison in pExpr.
147*/
148int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){
149 char aff = comparisonAffinity(pExpr);
drh8a512562005-11-14 22:29:05 +0000150 switch( aff ){
151 case SQLITE_AFF_NONE:
152 return 1;
153 case SQLITE_AFF_TEXT:
154 return idx_affinity==SQLITE_AFF_TEXT;
155 default:
156 return sqlite3IsNumericAffinity(idx_affinity);
157 }
danielk1977e014a832004-05-17 10:48:57 +0000158}
159
danielk1977a37cdde2004-05-16 11:15:36 +0000160/*
drh35573352008-01-08 23:54:25 +0000161** Return the P5 value that should be used for a binary comparison
danielk1977a37cdde2004-05-16 11:15:36 +0000162** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
danielk1977a37cdde2004-05-16 11:15:36 +0000163*/
drh35573352008-01-08 23:54:25 +0000164static u8 binaryCompareP5(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){
165 u8 aff = (char)sqlite3ExprAffinity(pExpr2);
166 aff = sqlite3CompareAffinity(pExpr1, aff) | jumpIfNull;
167 return aff;
danielk1977a37cdde2004-05-16 11:15:36 +0000168}
169
drha2e00042002-01-22 03:13:42 +0000170/*
danielk19770202b292004-06-09 09:55:16 +0000171** Return a pointer to the collation sequence that should be used by
172** a binary comparison operator comparing pLeft and pRight.
173**
174** If the left hand expression has a collating sequence type, then it is
175** used. Otherwise the collation sequence for the right hand expression
176** is used, or the default (BINARY) if neither expression has a collating
177** type.
danielk1977bcbb04e2007-05-29 12:11:29 +0000178**
179** Argument pRight (but not pLeft) may be a null pointer. In this case,
180** it is not considered.
danielk19770202b292004-06-09 09:55:16 +0000181*/
drh0a0e1312007-08-07 17:04:59 +0000182CollSeq *sqlite3BinaryCompareCollSeq(
danielk1977bcbb04e2007-05-29 12:11:29 +0000183 Parse *pParse,
184 Expr *pLeft,
185 Expr *pRight
186){
drhec41dda2007-02-07 13:09:45 +0000187 CollSeq *pColl;
188 assert( pLeft );
drhec41dda2007-02-07 13:09:45 +0000189 if( pLeft->flags & EP_ExpCollate ){
190 assert( pLeft->pColl );
191 pColl = pLeft->pColl;
danielk1977bcbb04e2007-05-29 12:11:29 +0000192 }else if( pRight && pRight->flags & EP_ExpCollate ){
drhec41dda2007-02-07 13:09:45 +0000193 assert( pRight->pColl );
194 pColl = pRight->pColl;
195 }else{
196 pColl = sqlite3ExprCollSeq(pParse, pLeft);
197 if( !pColl ){
198 pColl = sqlite3ExprCollSeq(pParse, pRight);
199 }
danielk19770202b292004-06-09 09:55:16 +0000200 }
201 return pColl;
202}
203
204/*
drhbe5c89a2004-07-26 00:31:09 +0000205** Generate code for a comparison operator.
206*/
207static int codeCompare(
208 Parse *pParse, /* The parsing (and code generating) context */
209 Expr *pLeft, /* The left operand */
210 Expr *pRight, /* The right operand */
211 int opcode, /* The comparison opcode */
drh35573352008-01-08 23:54:25 +0000212 int in1, int in2, /* Register holding operands */
drhbe5c89a2004-07-26 00:31:09 +0000213 int dest, /* Jump here if true. */
214 int jumpIfNull /* If true, jump if either operand is NULL */
215){
drh35573352008-01-08 23:54:25 +0000216 int p5;
217 int addr;
218 CollSeq *p4;
219
220 p4 = sqlite3BinaryCompareCollSeq(pParse, pLeft, pRight);
221 p5 = binaryCompareP5(pLeft, pRight, jumpIfNull);
222 addr = sqlite3VdbeAddOp4(pParse->pVdbe, opcode, in2, dest, in1,
223 (void*)p4, P4_COLLSEQ);
224 sqlite3VdbeChangeP5(pParse->pVdbe, p5);
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.
1553** IN_INDEX_INDEX - The cursor was opened on a database indec.
1554** 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
drhb1fdb2a2008-01-05 04:06:03 +00001613 sqlite3VdbeAddOp1(v, OP_SCopy, iMem);
drh66a51672008-01-03 00:01:23 +00001614 iAddr = sqlite3VdbeAddOp2(v, OP_If, 0, iMem);
drh4c583122008-01-04 22:01:03 +00001615 sqlite3VdbeAddOp2(v, OP_Integer, 1, iMem);
danielk19779a96b662007-11-29 17:05:18 +00001616
1617 sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
1618 eType = IN_INDEX_ROWID;
1619
1620 sqlite3VdbeJumpHere(v, iAddr);
1621 }else{
1622 /* The collation sequence used by the comparison. If an index is to
1623 ** be used in place of a temp-table, it must be ordered according
1624 ** to this collation sequence.
1625 */
1626 CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pExpr);
1627
1628 /* Check that the affinity that will be used to perform the
1629 ** comparison is the same as the affinity of the column. If
1630 ** it is not, it is not possible to use any index.
1631 */
1632 Table *pTab = p->pSrc->a[0].pTab;
1633 char aff = comparisonAffinity(pX);
1634 int affinity_ok = (pTab->aCol[iCol].affinity==aff||aff==SQLITE_AFF_NONE);
1635
1636 for(pIdx=pTab->pIndex; pIdx && eType==0 && affinity_ok; pIdx=pIdx->pNext){
1637 if( (pIdx->aiColumn[0]==iCol)
1638 && (pReq==sqlite3FindCollSeq(db, ENC(db), pIdx->azColl[0], -1, 0))
1639 && (!mustBeUnique || (pIdx->nColumn==1 && pIdx->onError!=OE_None))
1640 ){
1641 int iDb;
drh0a07c102008-01-03 18:03:08 +00001642 int iMem = ++pParse->nMem;
danielk19779a96b662007-11-29 17:05:18 +00001643 int iAddr;
1644 char *pKey;
1645
1646 pKey = (char *)sqlite3IndexKeyinfo(pParse, pIdx);
1647 iDb = sqlite3SchemaToIndex(db, pIdx->pSchema);
1648 sqlite3VdbeUsesBtree(v, iDb);
1649
drhb1fdb2a2008-01-05 04:06:03 +00001650 sqlite3VdbeAddOp1(v, OP_SCopy, iMem);
drh66a51672008-01-03 00:01:23 +00001651 iAddr = sqlite3VdbeAddOp2(v, OP_If, 0, iMem);
drh4c583122008-01-04 22:01:03 +00001652 sqlite3VdbeAddOp2(v, OP_Integer, 1, iMem);
danielk19779a96b662007-11-29 17:05:18 +00001653
danielk1977207872a2008-01-03 07:54:23 +00001654 sqlite3VdbeAddOp4(v, OP_OpenRead, iTab, pIdx->tnum, iDb,
drh66a51672008-01-03 00:01:23 +00001655 pKey,P4_KEYINFO_HANDOFF);
danielk1977207872a2008-01-03 07:54:23 +00001656 VdbeComment((v, "%s", pIdx->zName));
danielk19779a96b662007-11-29 17:05:18 +00001657 eType = IN_INDEX_INDEX;
drh66a51672008-01-03 00:01:23 +00001658 sqlite3VdbeAddOp2(v, OP_SetNumColumns, iTab, pIdx->nColumn);
danielk19779a96b662007-11-29 17:05:18 +00001659
1660 sqlite3VdbeJumpHere(v, iAddr);
1661 }
1662 }
1663 }
1664 }
1665
1666 if( eType==0 ){
1667 sqlite3CodeSubselect(pParse, pX);
1668 eType = IN_INDEX_EPH;
1669 }else{
1670 pX->iTable = iTab;
1671 }
1672 return eType;
1673}
danielk1977284f4ac2007-12-10 05:03:46 +00001674#endif
drh626a8792005-01-17 22:08:19 +00001675
1676/*
drh9cbe6352005-11-29 03:13:21 +00001677** Generate code for scalar subqueries used as an expression
1678** and IN operators. Examples:
drh626a8792005-01-17 22:08:19 +00001679**
drh9cbe6352005-11-29 03:13:21 +00001680** (SELECT a FROM b) -- subquery
1681** EXISTS (SELECT a FROM b) -- EXISTS subquery
1682** x IN (4,5,11) -- IN operator with list on right-hand side
1683** x IN (SELECT a FROM b) -- IN operator with subquery on the right
drhfef52082000-06-06 01:50:43 +00001684**
drh9cbe6352005-11-29 03:13:21 +00001685** The pExpr parameter describes the expression that contains the IN
1686** operator or subquery.
drhcce7d172000-05-31 15:34:51 +00001687*/
drh51522cd2005-01-20 13:36:19 +00001688#ifndef SQLITE_OMIT_SUBQUERY
danielk1977b3bce662005-01-29 08:32:43 +00001689void sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){
drh57dbd7b2005-07-08 18:25:26 +00001690 int testAddr = 0; /* One-time test address */
danielk1977b3bce662005-01-29 08:32:43 +00001691 Vdbe *v = sqlite3GetVdbe(pParse);
1692 if( v==0 ) return;
1693
danielk1977fc976062007-05-10 10:46:56 +00001694
drh57dbd7b2005-07-08 18:25:26 +00001695 /* This code must be run in its entirety every time it is encountered
1696 ** if any of the following is true:
1697 **
1698 ** * The right-hand side is a correlated subquery
1699 ** * The right-hand side is an expression list containing variables
1700 ** * We are inside a trigger
1701 **
1702 ** If all of the above are false, then we can run this code just once
1703 ** save the results, and reuse the same result on subsequent invocations.
danielk1977b3bce662005-01-29 08:32:43 +00001704 */
1705 if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->trigStack ){
drh0a07c102008-01-03 18:03:08 +00001706 int mem = ++pParse->nMem;
drhb1fdb2a2008-01-05 04:06:03 +00001707 sqlite3VdbeAddOp1(v, OP_SCopy, mem);
drh66a51672008-01-03 00:01:23 +00001708 testAddr = sqlite3VdbeAddOp0(v, OP_If);
drh17435752007-08-16 04:30:38 +00001709 assert( testAddr>0 || pParse->db->mallocFailed );
drh4c583122008-01-04 22:01:03 +00001710 sqlite3VdbeAddOp2(v, OP_Integer, 1, mem);
danielk1977b3bce662005-01-29 08:32:43 +00001711 }
1712
drhcce7d172000-05-31 15:34:51 +00001713 switch( pExpr->op ){
drhfef52082000-06-06 01:50:43 +00001714 case TK_IN: {
danielk1977e014a832004-05-17 10:48:57 +00001715 char affinity;
drhd3d39e92004-05-20 22:16:29 +00001716 KeyInfo keyInfo;
drhb9bb7c12006-06-11 23:41:55 +00001717 int addr; /* Address of OP_OpenEphemeral instruction */
drhd3d39e92004-05-20 22:16:29 +00001718
danielk1977bf3b7212004-05-18 10:06:24 +00001719 affinity = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +00001720
1721 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
drh57dbd7b2005-07-08 18:25:26 +00001722 ** expression it is handled the same way. A virtual table is
danielk1977e014a832004-05-17 10:48:57 +00001723 ** filled with single-field index keys representing the results
1724 ** from the SELECT or the <exprlist>.
1725 **
1726 ** If the 'x' expression is a column value, or the SELECT...
1727 ** statement returns a column value, then the affinity of that
1728 ** column is used to build the index keys. If both 'x' and the
1729 ** SELECT... statement are columns, then numeric affinity is used
1730 ** if either column has NUMERIC or INTEGER affinity. If neither
1731 ** 'x' nor the SELECT... statement are columns, then numeric affinity
1732 ** is used.
1733 */
1734 pExpr->iTable = pParse->nTab++;
drh66a51672008-01-03 00:01:23 +00001735 addr = sqlite3VdbeAddOp1(v, OP_OpenEphemeral, pExpr->iTable);
drhd3d39e92004-05-20 22:16:29 +00001736 memset(&keyInfo, 0, sizeof(keyInfo));
1737 keyInfo.nField = 1;
drh66a51672008-01-03 00:01:23 +00001738 sqlite3VdbeAddOp2(v, OP_SetNumColumns, pExpr->iTable, 1);
danielk1977e014a832004-05-17 10:48:57 +00001739
drhfef52082000-06-06 01:50:43 +00001740 if( pExpr->pSelect ){
1741 /* Case 1: expr IN (SELECT ...)
1742 **
danielk1977e014a832004-05-17 10:48:57 +00001743 ** Generate code to write the results of the select into the temporary
1744 ** table allocated and opened above.
drhfef52082000-06-06 01:50:43 +00001745 */
drh1013c932008-01-06 00:25:21 +00001746 SelectDest dest;
drhbe5c89a2004-07-26 00:31:09 +00001747 ExprList *pEList;
drh1013c932008-01-06 00:25:21 +00001748
1749 sqlite3SelectDestInit(&dest, SRT_Set, pExpr->iTable);
1750 dest.affinity = (int)affinity;
danielk1977e014a832004-05-17 10:48:57 +00001751 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
danielk19776c8c8ce2008-01-02 16:27:09 +00001752 if( sqlite3Select(pParse, pExpr->pSelect, &dest, 0, 0, 0, 0) ){
drh94ccde52007-04-13 16:06:32 +00001753 return;
1754 }
drhbe5c89a2004-07-26 00:31:09 +00001755 pEList = pExpr->pSelect->pEList;
1756 if( pEList && pEList->nExpr>0 ){
danielk1977bcbb04e2007-05-29 12:11:29 +00001757 keyInfo.aColl[0] = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft,
drhbe5c89a2004-07-26 00:31:09 +00001758 pEList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001759 }
drhfef52082000-06-06 01:50:43 +00001760 }else if( pExpr->pList ){
1761 /* Case 2: expr IN (exprlist)
1762 **
drhfd131da2007-08-07 17:13:03 +00001763 ** For each expression, build an index key from the evaluation and
danielk1977e014a832004-05-17 10:48:57 +00001764 ** store it in the temporary table. If <expr> is a column, then use
1765 ** that columns affinity when building index keys. If <expr> is not
1766 ** a column, use numeric affinity.
drhfef52082000-06-06 01:50:43 +00001767 */
danielk1977e014a832004-05-17 10:48:57 +00001768 int i;
drh57dbd7b2005-07-08 18:25:26 +00001769 ExprList *pList = pExpr->pList;
1770 struct ExprList_item *pItem;
1771
danielk1977e014a832004-05-17 10:48:57 +00001772 if( !affinity ){
drh8159a352006-05-23 23:22:29 +00001773 affinity = SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +00001774 }
danielk19770202b292004-06-09 09:55:16 +00001775 keyInfo.aColl[0] = pExpr->pLeft->pColl;
danielk1977e014a832004-05-17 10:48:57 +00001776
1777 /* Loop through each expression in <exprlist>. */
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 */
drh6c30be82005-07-29 15:10:17 +00001786 if( testAddr>0 && !sqlite3ExprIsConstant(pE2) ){
drhf8875402006-03-17 13:56:34 +00001787 sqlite3VdbeChangeToNoop(v, testAddr-1, 3);
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 */
drh389a1ad2008-01-03 23:44:53 +00001792 sqlite3ExprCode(pParse, pE2, 0);
drh66a51672008-01-03 00:01:23 +00001793 sqlite3VdbeAddOp4(v, OP_MakeRecord, 1, 0, 0, &affinity, 1);
1794 sqlite3VdbeAddOp1(v, OP_IdxInsert, pExpr->iTable);
drhfef52082000-06-06 01:50:43 +00001795 }
1796 }
drh66a51672008-01-03 00:01:23 +00001797 sqlite3VdbeChangeP4(v, addr, (void *)&keyInfo, P4_KEYINFO);
danielk1977b3bce662005-01-29 08:32:43 +00001798 break;
drhfef52082000-06-06 01:50:43 +00001799 }
1800
drh51522cd2005-01-20 13:36:19 +00001801 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001802 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +00001803 /* This has to be a scalar SELECT. Generate code to put the
1804 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +00001805 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +00001806 */
drh2646da72005-12-09 20:02:05 +00001807 static const Token one = { (u8*)"1", 0, 1 };
drh51522cd2005-01-20 13:36:19 +00001808 Select *pSel;
danielk19776c8c8ce2008-01-02 16:27:09 +00001809 SelectDest dest;
drh1398ad32005-01-19 23:24:50 +00001810
drh51522cd2005-01-20 13:36:19 +00001811 pSel = pExpr->pSelect;
drh1013c932008-01-06 00:25:21 +00001812 sqlite3SelectDestInit(&dest, 0, ++pParse->nMem);
drh51522cd2005-01-20 13:36:19 +00001813 if( pExpr->op==TK_SELECT ){
danielk19776c8c8ce2008-01-02 16:27:09 +00001814 dest.eDest = SRT_Mem;
drh4c583122008-01-04 22:01:03 +00001815 sqlite3VdbeAddOp2(v, OP_Null, 0, dest.iParm);
drhd4e70eb2008-01-02 00:34:36 +00001816 VdbeComment((v, "Init subquery result"));
drh51522cd2005-01-20 13:36:19 +00001817 }else{
danielk19776c8c8ce2008-01-02 16:27:09 +00001818 dest.eDest = SRT_Exists;
drh4c583122008-01-04 22:01:03 +00001819 sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iParm);
drhd4e70eb2008-01-02 00:34:36 +00001820 VdbeComment((v, "Init EXISTS result"));
drh51522cd2005-01-20 13:36:19 +00001821 }
drhec7429a2005-10-06 16:53:14 +00001822 sqlite3ExprDelete(pSel->pLimit);
danielk1977a1644fd2007-08-29 12:31:25 +00001823 pSel->pLimit = sqlite3PExpr(pParse, TK_INTEGER, 0, 0, &one);
danielk19776c8c8ce2008-01-02 16:27:09 +00001824 if( sqlite3Select(pParse, pSel, &dest, 0, 0, 0, 0) ){
drh94ccde52007-04-13 16:06:32 +00001825 return;
1826 }
danielk19776c8c8ce2008-01-02 16:27:09 +00001827 pExpr->iColumn = dest.iParm;
danielk1977b3bce662005-01-29 08:32:43 +00001828 break;
drhcce7d172000-05-31 15:34:51 +00001829 }
1830 }
danielk1977b3bce662005-01-29 08:32:43 +00001831
drh57dbd7b2005-07-08 18:25:26 +00001832 if( testAddr ){
drhd654be82005-09-20 17:42:23 +00001833 sqlite3VdbeJumpHere(v, testAddr);
danielk1977b3bce662005-01-29 08:32:43 +00001834 }
danielk1977fc976062007-05-10 10:46:56 +00001835
danielk1977b3bce662005-01-29 08:32:43 +00001836 return;
drhcce7d172000-05-31 15:34:51 +00001837}
drh51522cd2005-01-20 13:36:19 +00001838#endif /* SQLITE_OMIT_SUBQUERY */
drhcce7d172000-05-31 15:34:51 +00001839
drhcce7d172000-05-31 15:34:51 +00001840/*
drh598f1342007-10-23 15:39:45 +00001841** Duplicate an 8-byte value
1842*/
1843static char *dup8bytes(Vdbe *v, const char *in){
1844 char *out = sqlite3DbMallocRaw(sqlite3VdbeDb(v), 8);
1845 if( out ){
1846 memcpy(out, in, 8);
1847 }
1848 return out;
1849}
1850
1851/*
1852** Generate an instruction that will put the floating point
1853** value described by z[0..n-1] on the stack.
drh0cf19ed2007-10-23 18:55:48 +00001854**
1855** The z[] string will probably not be zero-terminated. But the
1856** z[n] character is guaranteed to be something that does not look
1857** like the continuation of the number.
drh598f1342007-10-23 15:39:45 +00001858*/
drh9de221d2008-01-05 06:51:30 +00001859static void codeReal(Vdbe *v, const char *z, int n, int negateFlag, int iMem){
drh598f1342007-10-23 15:39:45 +00001860 assert( z || v==0 || sqlite3VdbeDb(v)->mallocFailed );
1861 if( z ){
1862 double value;
1863 char *zV;
drh0cf19ed2007-10-23 18:55:48 +00001864 assert( !isdigit(z[n]) );
drh598f1342007-10-23 15:39:45 +00001865 sqlite3AtoF(z, &value);
1866 if( negateFlag ) value = -value;
1867 zV = dup8bytes(v, (char*)&value);
drh9de221d2008-01-05 06:51:30 +00001868 sqlite3VdbeAddOp4(v, OP_Real, 0, iMem, 0, zV, P4_REAL);
drh598f1342007-10-23 15:39:45 +00001869 }
1870}
1871
1872
1873/*
drhfec19aa2004-05-19 20:41:03 +00001874** Generate an instruction that will put the integer describe by
1875** text z[0..n-1] on the stack.
drh0cf19ed2007-10-23 18:55:48 +00001876**
1877** The z[] string will probably not be zero-terminated. But the
1878** z[n] character is guaranteed to be something that does not look
1879** like the continuation of the number.
drhfec19aa2004-05-19 20:41:03 +00001880*/
drh9de221d2008-01-05 06:51:30 +00001881static void codeInteger(Vdbe *v, const char *z, int n, int negFlag, int iMem){
drhabb6fca2007-08-16 12:24:01 +00001882 assert( z || v==0 || sqlite3VdbeDb(v)->mallocFailed );
danielk1977c9cf9012007-05-30 10:36:47 +00001883 if( z ){
1884 int i;
drh0cf19ed2007-10-23 18:55:48 +00001885 assert( !isdigit(z[n]) );
danielk1977c9cf9012007-05-30 10:36:47 +00001886 if( sqlite3GetInt32(z, &i) ){
drh9de221d2008-01-05 06:51:30 +00001887 if( negFlag ) i = -i;
1888 sqlite3VdbeAddOp2(v, OP_Integer, i, iMem);
1889 }else if( sqlite3FitsIn64Bits(z, negFlag) ){
drh598f1342007-10-23 15:39:45 +00001890 i64 value;
1891 char *zV;
1892 sqlite3Atoi64(z, &value);
drh9de221d2008-01-05 06:51:30 +00001893 if( negFlag ) value = -value;
drh598f1342007-10-23 15:39:45 +00001894 zV = dup8bytes(v, (char*)&value);
drh9de221d2008-01-05 06:51:30 +00001895 sqlite3VdbeAddOp4(v, OP_Int64, 0, iMem, 0, zV, P4_INT64);
danielk1977c9cf9012007-05-30 10:36:47 +00001896 }else{
drh9de221d2008-01-05 06:51:30 +00001897 codeReal(v, z, n, negFlag, iMem);
danielk1977c9cf9012007-05-30 10:36:47 +00001898 }
drhfec19aa2004-05-19 20:41:03 +00001899 }
1900}
1901
drh945498f2007-02-24 11:52:52 +00001902
1903/*
1904** Generate code that will extract the iColumn-th column from
drh2133d822008-01-03 18:44:59 +00001905** table pTab and store the column value in register iMem, or on
1906** the stack if iMem==0. There is an open cursor to pTab in
1907** iTable. If iColumn<0 then code is generated that extracts the rowid.
drh945498f2007-02-24 11:52:52 +00001908*/
drh2133d822008-01-03 18:44:59 +00001909void sqlite3ExprCodeGetColumn(
1910 Vdbe *v, /* The VM being created */
1911 Table *pTab, /* Description of the table we are reading from */
1912 int iColumn, /* Index of the table column */
1913 int iTable, /* The cursor pointing to the table */
1914 int iReg /* Store results here */
1915){
drh945498f2007-02-24 11:52:52 +00001916 if( iColumn<0 ){
1917 int op = (pTab && IsVirtual(pTab)) ? OP_VRowid : OP_Rowid;
drh2133d822008-01-03 18:44:59 +00001918 sqlite3VdbeAddOp2(v, op, iTable, iReg);
drh945498f2007-02-24 11:52:52 +00001919 }else if( pTab==0 ){
drh2133d822008-01-03 18:44:59 +00001920 sqlite3VdbeAddOp3(v, OP_Column, iTable, iColumn, iReg);
drh945498f2007-02-24 11:52:52 +00001921 }else{
1922 int op = IsVirtual(pTab) ? OP_VColumn : OP_Column;
drh2133d822008-01-03 18:44:59 +00001923 sqlite3VdbeAddOp3(v, op, iTable, iColumn, iReg);
drh945498f2007-02-24 11:52:52 +00001924 sqlite3ColumnDefault(v, pTab, iColumn);
1925#ifndef SQLITE_OMIT_FLOATING_POINT
1926 if( pTab->aCol[iColumn].affinity==SQLITE_AFF_REAL ){
drh2133d822008-01-03 18:44:59 +00001927 sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg);
drh945498f2007-02-24 11:52:52 +00001928 }
1929#endif
1930 }
1931}
1932
drhfec19aa2004-05-19 20:41:03 +00001933/*
drhcce7d172000-05-31 15:34:51 +00001934** Generate code into the current Vdbe to evaluate the given
drh389a1ad2008-01-03 23:44:53 +00001935** expression and leaves the result in a register on on the stack.
1936**
1937** If the target register number is negative, allocate a new
1938** register to store the result. If the target register number
1939** is zero then push the result onto the stack. Return the target
1940** register number regardless.
drhf2bc0132004-10-04 13:19:23 +00001941**
1942** This code depends on the fact that certain token values (ex: TK_EQ)
1943** are the same as opcode values (ex: OP_Eq) that implement the corresponding
1944** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
1945** the make process cause these values to align. Assert()s in the code
1946** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00001947*/
drh389a1ad2008-01-03 23:44:53 +00001948int sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){
drhcce7d172000-05-31 15:34:51 +00001949 Vdbe *v = pParse->pVdbe;
1950 int op;
drh389a1ad2008-01-03 23:44:53 +00001951 int inReg = 0;
drh9de221d2008-01-05 06:51:30 +00001952 int origTarget = target;
drhffe07b22005-11-03 00:41:17 +00001953
drh389a1ad2008-01-03 23:44:53 +00001954 assert( v!=0 || pParse->db->mallocFailed );
1955 if( v==0 ) return 0;
1956 if( target<0 ){
1957 target = ++pParse->nMem;
danielk19777977a172004-11-09 12:44:37 +00001958 }
drh389a1ad2008-01-03 23:44:53 +00001959
1960 if( pExpr==0 ){
1961 op = TK_NULL;
1962 }else{
1963 op = pExpr->op;
1964 }
drhf2bc0132004-10-04 13:19:23 +00001965 switch( op ){
drh13449892005-09-07 21:22:45 +00001966 case TK_AGG_COLUMN: {
1967 AggInfo *pAggInfo = pExpr->pAggInfo;
1968 struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg];
1969 if( !pAggInfo->directMode ){
drh9de221d2008-01-05 06:51:30 +00001970 assert( pCol->iMem>0 );
1971 inReg = pCol->iMem;
drh13449892005-09-07 21:22:45 +00001972 break;
1973 }else if( pAggInfo->useSortingIdx ){
drh389a1ad2008-01-03 23:44:53 +00001974 sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdx,
1975 pCol->iSorterColumn, target);
drh9de221d2008-01-05 06:51:30 +00001976 inReg = target;
drh13449892005-09-07 21:22:45 +00001977 break;
1978 }
1979 /* Otherwise, fall thru into the TK_COLUMN case */
1980 }
drh967e8b72000-06-21 13:59:10 +00001981 case TK_COLUMN: {
drhffe07b22005-11-03 00:41:17 +00001982 if( pExpr->iTable<0 ){
1983 /* This only happens when coding check constraints */
drhaa9b8962008-01-08 02:57:55 +00001984 assert( pParse->ckBase>0 );
1985 inReg = pExpr->iColumn + pParse->ckBase;
drhc4a3c772001-04-04 11:48:57 +00001986 }else{
drh2133d822008-01-03 18:44:59 +00001987 sqlite3ExprCodeGetColumn(v, pExpr->pTab,
drh389a1ad2008-01-03 23:44:53 +00001988 pExpr->iColumn, pExpr->iTable, target);
drh9de221d2008-01-05 06:51:30 +00001989 inReg = target;
drh22827922000-06-06 17:27:05 +00001990 }
drhcce7d172000-05-31 15:34:51 +00001991 break;
1992 }
1993 case TK_INTEGER: {
drh9de221d2008-01-05 06:51:30 +00001994 codeInteger(v, (char*)pExpr->token.z, pExpr->token.n, 0, target);
1995 inReg = target;
drhfec19aa2004-05-19 20:41:03 +00001996 break;
1997 }
drh598f1342007-10-23 15:39:45 +00001998 case TK_FLOAT: {
drh9de221d2008-01-05 06:51:30 +00001999 codeReal(v, (char*)pExpr->token.z, pExpr->token.n, 0, target);
2000 inReg = target;
drh598f1342007-10-23 15:39:45 +00002001 break;
2002 }
drhfec19aa2004-05-19 20:41:03 +00002003 case TK_STRING: {
danielk19771e536952007-08-16 10:09:01 +00002004 sqlite3DequoteExpr(pParse->db, pExpr);
drh9de221d2008-01-05 06:51:30 +00002005 sqlite3VdbeAddOp4(v,OP_String8, 0, target, 0,
drh66a51672008-01-03 00:01:23 +00002006 (char*)pExpr->token.z, pExpr->token.n);
drh9de221d2008-01-05 06:51:30 +00002007 inReg = target;
drhcce7d172000-05-31 15:34:51 +00002008 break;
2009 }
drhf0863fe2005-06-12 21:35:51 +00002010 case TK_NULL: {
drh9de221d2008-01-05 06:51:30 +00002011 sqlite3VdbeAddOp2(v, OP_Null, 0, target);
2012 inReg = target;
drhf0863fe2005-06-12 21:35:51 +00002013 break;
2014 }
danielk19775338a5f2005-01-20 13:03:10 +00002015#ifndef SQLITE_OMIT_BLOB_LITERAL
danielk1977c572ef72004-05-27 09:28:41 +00002016 case TK_BLOB: {
drh6c8c6ce2005-08-23 11:17:58 +00002017 int n;
2018 const char *z;
drhf2bc0132004-10-04 13:19:23 +00002019 assert( TK_BLOB==OP_HexBlob );
drh6c8c6ce2005-08-23 11:17:58 +00002020 n = pExpr->token.n - 3;
drh2646da72005-12-09 20:02:05 +00002021 z = (char*)pExpr->token.z + 2;
drh6c8c6ce2005-08-23 11:17:58 +00002022 assert( n>=0 );
2023 if( n==0 ){
2024 z = "";
2025 }
drh9de221d2008-01-05 06:51:30 +00002026 sqlite3VdbeAddOp4(v, op, 0, target, 0, z, n);
2027 inReg = target;
danielk1977c572ef72004-05-27 09:28:41 +00002028 break;
2029 }
danielk19775338a5f2005-01-20 13:03:10 +00002030#endif
drh50457892003-09-06 01:10:47 +00002031 case TK_VARIABLE: {
drh9de221d2008-01-05 06:51:30 +00002032 sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iTable, target);
drh895d7472004-08-20 16:02:39 +00002033 if( pExpr->token.n>1 ){
drh66a51672008-01-03 00:01:23 +00002034 sqlite3VdbeChangeP4(v, -1, (char*)pExpr->token.z, pExpr->token.n);
drh895d7472004-08-20 16:02:39 +00002035 }
drh9de221d2008-01-05 06:51:30 +00002036 inReg = target;
drh50457892003-09-06 01:10:47 +00002037 break;
2038 }
drh4e0cff62004-11-05 05:10:28 +00002039 case TK_REGISTER: {
drh9de221d2008-01-05 06:51:30 +00002040 inReg = pExpr->iTable;
drh4e0cff62004-11-05 05:10:28 +00002041 break;
2042 }
drh487e2622005-06-25 18:42:14 +00002043#ifndef SQLITE_OMIT_CAST
2044 case TK_CAST: {
2045 /* Expressions of the form: CAST(pLeft AS token) */
danielk1977f0113002006-01-24 12:09:17 +00002046 int aff, to_op;
drh9de221d2008-01-05 06:51:30 +00002047 sqlite3ExprCode(pParse, pExpr->pLeft, target);
drh8a512562005-11-14 22:29:05 +00002048 aff = sqlite3AffinityType(&pExpr->token);
danielk1977f0113002006-01-24 12:09:17 +00002049 to_op = aff - SQLITE_AFF_TEXT + OP_ToText;
2050 assert( to_op==OP_ToText || aff!=SQLITE_AFF_TEXT );
2051 assert( to_op==OP_ToBlob || aff!=SQLITE_AFF_NONE );
2052 assert( to_op==OP_ToNumeric || aff!=SQLITE_AFF_NUMERIC );
2053 assert( to_op==OP_ToInt || aff!=SQLITE_AFF_INTEGER );
2054 assert( to_op==OP_ToReal || aff!=SQLITE_AFF_REAL );
drh9de221d2008-01-05 06:51:30 +00002055 sqlite3VdbeAddOp1(v, to_op, target);
drh9de221d2008-01-05 06:51:30 +00002056 inReg = target;
drh487e2622005-06-25 18:42:14 +00002057 break;
2058 }
2059#endif /* SQLITE_OMIT_CAST */
drhc9b84a12002-06-20 11:36:48 +00002060 case TK_LT:
2061 case TK_LE:
2062 case TK_GT:
2063 case TK_GE:
2064 case TK_NE:
2065 case TK_EQ: {
drh35573352008-01-08 23:54:25 +00002066 int r1, r2;
drhf2bc0132004-10-04 13:19:23 +00002067 assert( TK_LT==OP_Lt );
2068 assert( TK_LE==OP_Le );
2069 assert( TK_GT==OP_Gt );
2070 assert( TK_GE==OP_Ge );
2071 assert( TK_EQ==OP_Eq );
2072 assert( TK_NE==OP_Ne );
drh35573352008-01-08 23:54:25 +00002073 if( target>0 ){
2074 inReg = target;
2075 }else{
2076 inReg = ++pParse->nMem;
2077 }
2078 r1 = sqlite3ExprCode(pParse, pExpr->pLeft, -1);
2079 r2 = sqlite3ExprCode(pParse, pExpr->pRight, -1);
2080 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
2081 r1, r2, inReg, SQLITE_STOREP2);
danielk1977a37cdde2004-05-16 11:15:36 +00002082 break;
drhc9b84a12002-06-20 11:36:48 +00002083 }
drhcce7d172000-05-31 15:34:51 +00002084 case TK_AND:
2085 case TK_OR:
2086 case TK_PLUS:
2087 case TK_STAR:
2088 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00002089 case TK_REM:
2090 case TK_BITAND:
2091 case TK_BITOR:
drh17c40292004-07-21 02:53:29 +00002092 case TK_SLASH:
drhbf4133c2001-10-13 02:59:08 +00002093 case TK_LSHIFT:
drh855eb1c2004-08-31 13:45:11 +00002094 case TK_RSHIFT:
drh00400772000-06-16 20:51:26 +00002095 case TK_CONCAT: {
drh5b6afba2008-01-05 16:29:28 +00002096 int r1, r2;
drhf2bc0132004-10-04 13:19:23 +00002097 assert( TK_AND==OP_And );
2098 assert( TK_OR==OP_Or );
2099 assert( TK_PLUS==OP_Add );
2100 assert( TK_MINUS==OP_Subtract );
2101 assert( TK_REM==OP_Remainder );
2102 assert( TK_BITAND==OP_BitAnd );
2103 assert( TK_BITOR==OP_BitOr );
2104 assert( TK_SLASH==OP_Divide );
2105 assert( TK_LSHIFT==OP_ShiftLeft );
2106 assert( TK_RSHIFT==OP_ShiftRight );
2107 assert( TK_CONCAT==OP_Concat );
drhaa9b8962008-01-08 02:57:55 +00002108 r1 = sqlite3ExprCode(pParse, pExpr->pLeft, -1);
2109 r2 = sqlite3ExprCode(pParse, pExpr->pRight, -1);
drh5b6afba2008-01-05 16:29:28 +00002110 sqlite3VdbeAddOp3(v, op, r2, r1, target);
drh5b6afba2008-01-05 16:29:28 +00002111 inReg = target;
drh00400772000-06-16 20:51:26 +00002112 break;
2113 }
drhcce7d172000-05-31 15:34:51 +00002114 case TK_UMINUS: {
drhfec19aa2004-05-19 20:41:03 +00002115 Expr *pLeft = pExpr->pLeft;
2116 assert( pLeft );
2117 if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
2118 Token *p = &pLeft->token;
drhfec19aa2004-05-19 20:41:03 +00002119 if( pLeft->op==TK_FLOAT ){
drh9de221d2008-01-05 06:51:30 +00002120 codeReal(v, (char*)p->z, p->n, 1, target);
drhe6840902002-03-06 03:08:25 +00002121 }else{
drh9de221d2008-01-05 06:51:30 +00002122 codeInteger(v, (char*)p->z, p->n, 1, target);
drhe6840902002-03-06 03:08:25 +00002123 }
drh3c84ddf2008-01-09 02:15:38 +00002124 }else{
2125 int r1 = ++pParse->nMem;
2126 sqlite3VdbeAddOp2(v, OP_Integer, 0, r1);
2127 sqlite3ExprCode(pParse, pExpr->pLeft, target);
2128 sqlite3VdbeAddOp3(v, OP_Subtract, target, r1, target);
drh6e142f52000-06-08 13:36:40 +00002129 }
drh3c84ddf2008-01-09 02:15:38 +00002130 inReg = target;
2131 break;
drh6e142f52000-06-08 13:36:40 +00002132 }
drhbf4133c2001-10-13 02:59:08 +00002133 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00002134 case TK_NOT: {
drhf2bc0132004-10-04 13:19:23 +00002135 assert( TK_BITNOT==OP_BitNot );
2136 assert( TK_NOT==OP_Not );
drh389a1ad2008-01-03 23:44:53 +00002137 sqlite3ExprCode(pParse, pExpr->pLeft, 0);
drh66a51672008-01-03 00:01:23 +00002138 sqlite3VdbeAddOp0(v, op);
drhcce7d172000-05-31 15:34:51 +00002139 break;
2140 }
2141 case TK_ISNULL:
2142 case TK_NOTNULL: {
drh6a288a32008-01-07 19:20:24 +00002143 int addr;
drhf2bc0132004-10-04 13:19:23 +00002144 assert( TK_ISNULL==OP_IsNull );
2145 assert( TK_NOTNULL==OP_NotNull );
drh9de221d2008-01-05 06:51:30 +00002146 sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
drh389a1ad2008-01-03 23:44:53 +00002147 sqlite3ExprCode(pParse, pExpr->pLeft, 0);
drh6a288a32008-01-07 19:20:24 +00002148 addr = sqlite3VdbeAddOp0(v, op);
drh9de221d2008-01-05 06:51:30 +00002149 sqlite3VdbeAddOp2(v, OP_AddImm, target, -1);
drh6a288a32008-01-07 19:20:24 +00002150 sqlite3VdbeJumpHere(v, addr);
drh9de221d2008-01-05 06:51:30 +00002151 inReg = target;
drhf2bc0132004-10-04 13:19:23 +00002152 break;
drhcce7d172000-05-31 15:34:51 +00002153 }
drh22827922000-06-06 17:27:05 +00002154 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00002155 AggInfo *pInfo = pExpr->pAggInfo;
drh7e56e712005-11-16 12:53:15 +00002156 if( pInfo==0 ){
2157 sqlite3ErrorMsg(pParse, "misuse of aggregate: %T",
2158 &pExpr->span);
2159 }else{
drh9de221d2008-01-05 06:51:30 +00002160 inReg = pInfo->aFunc[pExpr->iAgg].iMem;
drh7e56e712005-11-16 12:53:15 +00002161 }
drh22827922000-06-06 17:27:05 +00002162 break;
2163 }
drhb71090f2005-05-23 17:26:51 +00002164 case TK_CONST_FUNC:
drhcce7d172000-05-31 15:34:51 +00002165 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00002166 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00002167 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00002168 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00002169 int nId;
2170 const char *zId;
drh13449892005-09-07 21:22:45 +00002171 int constMask = 0;
danielk1977682f68b2004-06-05 10:22:17 +00002172 int i;
drh17435752007-08-16 04:30:38 +00002173 sqlite3 *db = pParse->db;
2174 u8 enc = ENC(db);
danielk1977dc1bdc42004-06-11 10:51:27 +00002175 CollSeq *pColl = 0;
drh17435752007-08-16 04:30:38 +00002176
drh2646da72005-12-09 20:02:05 +00002177 zId = (char*)pExpr->token.z;
drhb71090f2005-05-23 17:26:51 +00002178 nId = pExpr->token.n;
danielk1977d8123362004-06-12 09:25:12 +00002179 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0);
drh0bce8352002-02-28 00:41:10 +00002180 assert( pDef!=0 );
drh389a1ad2008-01-03 23:44:53 +00002181 nExpr = sqlite3ExprCodeExprList(pParse, pList, 0);
drhb7f6f682006-07-08 17:06:43 +00002182#ifndef SQLITE_OMIT_VIRTUALTABLE
drha43fa222006-07-08 18:41:37 +00002183 /* Possibly overload the function if the first argument is
2184 ** a virtual table column.
2185 **
2186 ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the
2187 ** second argument, not the first, as the argument to test to
2188 ** see if it is a column in a virtual table. This is done because
2189 ** the left operand of infix functions (the operand we want to
2190 ** control overloading) ends up as the second argument to the
2191 ** function. The expression "A glob B" is equivalent to
2192 ** "glob(B,A). We want to use the A in "A glob B" to test
2193 ** for function overloading. But we use the B term in "glob(B,A)".
2194 */
drh6a03a1c2006-07-08 18:34:59 +00002195 if( nExpr>=2 && (pExpr->flags & EP_InfixFunc) ){
drh17435752007-08-16 04:30:38 +00002196 pDef = sqlite3VtabOverloadFunction(db, pDef, nExpr, pList->a[1].pExpr);
drh6a03a1c2006-07-08 18:34:59 +00002197 }else if( nExpr>0 ){
drh17435752007-08-16 04:30:38 +00002198 pDef = sqlite3VtabOverloadFunction(db, pDef, nExpr, pList->a[0].pExpr);
drhb7f6f682006-07-08 17:06:43 +00002199 }
2200#endif
danielk1977682f68b2004-06-05 10:22:17 +00002201 for(i=0; i<nExpr && i<32; i++){
danielk1977d02eb1f2004-06-06 09:44:03 +00002202 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
drh13449892005-09-07 21:22:45 +00002203 constMask |= (1<<i);
danielk1977d02eb1f2004-06-06 09:44:03 +00002204 }
danielk1977dc1bdc42004-06-11 10:51:27 +00002205 if( pDef->needCollSeq && !pColl ){
2206 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
2207 }
2208 }
2209 if( pDef->needCollSeq ){
2210 if( !pColl ) pColl = pParse->db->pDfltColl;
drh66a51672008-01-03 00:01:23 +00002211 sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ);
danielk1977682f68b2004-06-05 10:22:17 +00002212 }
drh66a51672008-01-03 00:01:23 +00002213 sqlite3VdbeAddOp4(v, OP_Function, constMask, nExpr, 0,
2214 (char*)pDef, P4_FUNCDEF);
drhcce7d172000-05-31 15:34:51 +00002215 break;
2216 }
drhfe2093d2005-01-20 22:48:47 +00002217#ifndef SQLITE_OMIT_SUBQUERY
2218 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00002219 case TK_SELECT: {
drh41714d62006-03-02 04:44:23 +00002220 if( pExpr->iColumn==0 ){
2221 sqlite3CodeSubselect(pParse, pExpr);
2222 }
drh9de221d2008-01-05 06:51:30 +00002223 inReg = pExpr->iColumn;
drh19a775c2000-06-05 18:54:46 +00002224 break;
2225 }
drhfef52082000-06-06 01:50:43 +00002226 case TK_IN: {
drh6a288a32008-01-07 19:20:24 +00002227 int j1, j2, j3, j4, j5;
drh94a11212004-09-25 13:12:14 +00002228 char affinity;
danielk19779a96b662007-11-29 17:05:18 +00002229 int eType;
danielk19779a96b662007-11-29 17:05:18 +00002230
2231 eType = sqlite3FindInIndex(pParse, pExpr, 0);
danielk1977e014a832004-05-17 10:48:57 +00002232
2233 /* Figure out the affinity to use to create a key from the results
2234 ** of the expression. affinityStr stores a static string suitable for
drh66a51672008-01-03 00:01:23 +00002235 ** P4 of OP_MakeRecord.
danielk1977e014a832004-05-17 10:48:57 +00002236 */
drh94a11212004-09-25 13:12:14 +00002237 affinity = comparisonAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +00002238
drh66a51672008-01-03 00:01:23 +00002239 sqlite3VdbeAddOp1(v, OP_Integer, 1);
danielk1977e014a832004-05-17 10:48:57 +00002240
2241 /* Code the <expr> from "<expr> IN (...)". The temporary table
2242 ** pExpr->iTable contains the values that make up the (...) set.
2243 */
drh389a1ad2008-01-03 23:44:53 +00002244 sqlite3ExprCode(pParse, pExpr->pLeft, 0);
drh6a288a32008-01-07 19:20:24 +00002245 sqlite3VdbeAddOp0(v, OP_SCopy);
2246 j1 = sqlite3VdbeAddOp0(v, OP_NotNull);
drh66a51672008-01-03 00:01:23 +00002247 sqlite3VdbeAddOp1(v, OP_Pop, 2);
2248 sqlite3VdbeAddOp0(v, OP_Null);
drh6a288a32008-01-07 19:20:24 +00002249 j2 = sqlite3VdbeAddOp0(v, OP_Goto);
2250 sqlite3VdbeJumpHere(v, j1);
danielk19779a96b662007-11-29 17:05:18 +00002251 if( eType==IN_INDEX_ROWID ){
drh3c84ddf2008-01-09 02:15:38 +00002252 j3 = sqlite3VdbeAddOp3(v, OP_MustBeInt, 0, 0, 1);
drh6a288a32008-01-07 19:20:24 +00002253 j4 = sqlite3VdbeAddOp1(v, OP_NotExists, pExpr->iTable);
2254 j5 = sqlite3VdbeAddOp0(v, OP_Goto);
2255 sqlite3VdbeJumpHere(v, j3);
2256 sqlite3VdbeJumpHere(v, j4);
danielk19779a96b662007-11-29 17:05:18 +00002257 }else{
drh6a288a32008-01-07 19:20:24 +00002258 sqlite3VdbeAddOp4(v, OP_MakeRecord, 1, 0, 0, &affinity, 1);
2259 j5 = sqlite3VdbeAddOp1(v, OP_Found, pExpr->iTable);
danielk19779a96b662007-11-29 17:05:18 +00002260 }
drh6a288a32008-01-07 19:20:24 +00002261 sqlite3VdbeAddOp2(v, OP_AddImm, 0, -1);
2262 sqlite3VdbeJumpHere(v, j2);
2263 sqlite3VdbeJumpHere(v, j5);
drhfef52082000-06-06 01:50:43 +00002264 break;
2265 }
danielk197793758c82005-01-21 08:13:14 +00002266#endif
drhfef52082000-06-06 01:50:43 +00002267 case TK_BETWEEN: {
drhbe5c89a2004-07-26 00:31:09 +00002268 Expr *pLeft = pExpr->pLeft;
2269 struct ExprList_item *pLItem = pExpr->pList->a;
2270 Expr *pRight = pLItem->pExpr;
drh35573352008-01-08 23:54:25 +00002271 int r1, r2, r3, r4, r5;
2272
2273 if( target>0 ){
2274 inReg = target;
2275 }else{
2276 inReg = ++pParse->nMem;
2277 }
2278 r1 = sqlite3ExprCode(pParse, pLeft, -1);
2279 r2 = sqlite3ExprCode(pParse, pRight, -1);
2280 r3 = ++pParse->nMem;
2281 codeCompare(pParse, pLeft, pRight, OP_Ge,
2282 r1, r2, r3, SQLITE_STOREP2);
drhbe5c89a2004-07-26 00:31:09 +00002283 pLItem++;
2284 pRight = pLItem->pExpr;
drh35573352008-01-08 23:54:25 +00002285 r4 = sqlite3ExprCode(pParse, pRight, -1);
2286 r5 = ++pParse->nMem;
2287 codeCompare(pParse, pLeft, pRight, OP_Le, r1, r4, r5, SQLITE_STOREP2);
2288 sqlite3VdbeAddOp3(v, OP_And, r3, r5, inReg);
drhfef52082000-06-06 01:50:43 +00002289 break;
2290 }
drh4f07e5f2007-05-14 11:34:46 +00002291 case TK_UPLUS: {
drh9de221d2008-01-05 06:51:30 +00002292 inReg = sqlite3ExprCode(pParse, pExpr->pLeft, origTarget);
drha2e00042002-01-22 03:13:42 +00002293 break;
2294 }
drh17a7f8d2002-03-24 13:13:27 +00002295 case TK_CASE: {
2296 int expr_end_label;
drhf5905aa2002-05-26 20:54:33 +00002297 int jumpInst;
drhf5905aa2002-05-26 20:54:33 +00002298 int nExpr;
drh17a7f8d2002-03-24 13:13:27 +00002299 int i;
drhbe5c89a2004-07-26 00:31:09 +00002300 ExprList *pEList;
2301 struct ExprList_item *aListelem;
drh17a7f8d2002-03-24 13:13:27 +00002302
2303 assert(pExpr->pList);
2304 assert((pExpr->pList->nExpr % 2) == 0);
2305 assert(pExpr->pList->nExpr > 0);
drhbe5c89a2004-07-26 00:31:09 +00002306 pEList = pExpr->pList;
2307 aListelem = pEList->a;
2308 nExpr = pEList->nExpr;
danielk19774adee202004-05-08 08:23:19 +00002309 expr_end_label = sqlite3VdbeMakeLabel(v);
drh17a7f8d2002-03-24 13:13:27 +00002310 if( pExpr->pLeft ){
drh389a1ad2008-01-03 23:44:53 +00002311 sqlite3ExprCode(pParse, pExpr->pLeft, 0);
drh17a7f8d2002-03-24 13:13:27 +00002312 }
drhf5905aa2002-05-26 20:54:33 +00002313 for(i=0; i<nExpr; i=i+2){
drh389a1ad2008-01-03 23:44:53 +00002314 sqlite3ExprCode(pParse, aListelem[i].pExpr, 0);
drh17a7f8d2002-03-24 13:13:27 +00002315 if( pExpr->pLeft ){
drhb1fdb2a2008-01-05 04:06:03 +00002316 sqlite3VdbeAddOp1(v, OP_SCopy, -1);
drhbe5c89a2004-07-26 00:31:09 +00002317 jumpInst = codeCompare(pParse, pExpr->pLeft, aListelem[i].pExpr,
drh35573352008-01-08 23:54:25 +00002318 OP_Ne, 0, 0, 0, SQLITE_JUMPIFNULL);
drh66a51672008-01-03 00:01:23 +00002319 sqlite3VdbeAddOp1(v, OP_Pop, 1);
drh17a7f8d2002-03-24 13:13:27 +00002320 }else{
drh3c84ddf2008-01-09 02:15:38 +00002321 jumpInst = sqlite3VdbeAddOp3(v, OP_IfNot, 0, 0, 1);
drh17a7f8d2002-03-24 13:13:27 +00002322 }
drh9de221d2008-01-05 06:51:30 +00002323 sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target);
drh66a51672008-01-03 00:01:23 +00002324 sqlite3VdbeAddOp2(v, OP_Goto, 0, expr_end_label);
drhd654be82005-09-20 17:42:23 +00002325 sqlite3VdbeJumpHere(v, jumpInst);
drh17a7f8d2002-03-24 13:13:27 +00002326 }
drhf570f012002-05-31 15:51:25 +00002327 if( pExpr->pLeft ){
drh66a51672008-01-03 00:01:23 +00002328 sqlite3VdbeAddOp2(v, OP_Pop, 1, 0);
drhf570f012002-05-31 15:51:25 +00002329 }
drh17a7f8d2002-03-24 13:13:27 +00002330 if( pExpr->pRight ){
drh9de221d2008-01-05 06:51:30 +00002331 sqlite3ExprCode(pParse, pExpr->pRight, target);
drh17a7f8d2002-03-24 13:13:27 +00002332 }else{
drh9de221d2008-01-05 06:51:30 +00002333 sqlite3VdbeAddOp2(v, OP_Null, 0, target);
drh17a7f8d2002-03-24 13:13:27 +00002334 }
danielk19774adee202004-05-08 08:23:19 +00002335 sqlite3VdbeResolveLabel(v, expr_end_label);
drh9de221d2008-01-05 06:51:30 +00002336 inReg = target;
danielk19776f349032002-06-11 02:25:40 +00002337 break;
2338 }
danielk19775338a5f2005-01-20 13:03:10 +00002339#ifndef SQLITE_OMIT_TRIGGER
danielk19776f349032002-06-11 02:25:40 +00002340 case TK_RAISE: {
2341 if( !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +00002342 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00002343 "RAISE() may only be used within a trigger-program");
drh389a1ad2008-01-03 23:44:53 +00002344 return 0;
danielk19776f349032002-06-11 02:25:40 +00002345 }
drhad6d9462004-09-19 02:15:24 +00002346 if( pExpr->iColumn!=OE_Ignore ){
2347 assert( pExpr->iColumn==OE_Rollback ||
2348 pExpr->iColumn == OE_Abort ||
2349 pExpr->iColumn == OE_Fail );
danielk19771e536952007-08-16 10:09:01 +00002350 sqlite3DequoteExpr(pParse->db, pExpr);
drh66a51672008-01-03 00:01:23 +00002351 sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn, 0,
drh2646da72005-12-09 20:02:05 +00002352 (char*)pExpr->token.z, pExpr->token.n);
danielk19776f349032002-06-11 02:25:40 +00002353 } else {
drhad6d9462004-09-19 02:15:24 +00002354 assert( pExpr->iColumn == OE_Ignore );
drh66a51672008-01-03 00:01:23 +00002355 sqlite3VdbeAddOp2(v, OP_ContextPop, 0, 0);
2356 sqlite3VdbeAddOp2(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
drhd4e70eb2008-01-02 00:34:36 +00002357 VdbeComment((v, "raise(IGNORE)"));
danielk19776f349032002-06-11 02:25:40 +00002358 }
drhffe07b22005-11-03 00:41:17 +00002359 break;
drh17a7f8d2002-03-24 13:13:27 +00002360 }
danielk19775338a5f2005-01-20 13:03:10 +00002361#endif
drhffe07b22005-11-03 00:41:17 +00002362 }
drh5b6afba2008-01-05 16:29:28 +00002363 if( inReg!=target ){
2364 if( origTarget!=-1 ){
2365 sqlite3VdbeAddOp2(v, (inReg>0 ? OP_SCopy : OP_Move), inReg, target);
2366 }else{
2367 target = inReg;
2368 }
drhcce7d172000-05-31 15:34:51 +00002369 }
drh389a1ad2008-01-03 23:44:53 +00002370 return target;
drhcce7d172000-05-31 15:34:51 +00002371}
2372
danielk197793758c82005-01-21 08:13:14 +00002373#ifndef SQLITE_OMIT_TRIGGER
drhcce7d172000-05-31 15:34:51 +00002374/*
drh25303782004-12-07 15:41:48 +00002375** Generate code that evalutes the given expression and leaves the result
2376** on the stack. See also sqlite3ExprCode().
2377**
2378** This routine might also cache the result and modify the pExpr tree
2379** so that it will make use of the cached result on subsequent evaluations
2380** rather than evaluate the whole expression again. Trivial expressions are
2381** not cached. If the expression is cached, its result is stored in a
2382** memory location.
2383*/
2384void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr){
2385 Vdbe *v = pParse->pVdbe;
drh49df6b72007-12-14 15:12:21 +00002386 VdbeOp *pOp;
drh25303782004-12-07 15:41:48 +00002387 int iMem;
2388 int addr1, addr2;
2389 if( v==0 ) return;
2390 addr1 = sqlite3VdbeCurrentAddr(v);
drh389a1ad2008-01-03 23:44:53 +00002391 sqlite3ExprCode(pParse, pExpr, 0);
drh25303782004-12-07 15:41:48 +00002392 addr2 = sqlite3VdbeCurrentAddr(v);
drh49df6b72007-12-14 15:12:21 +00002393 if( addr2>addr1+1
2394 || ((pOp = sqlite3VdbeGetOp(v, addr1))!=0 && pOp->opcode==OP_Function) ){
drh0a07c102008-01-03 18:03:08 +00002395 iMem = pExpr->iTable = ++pParse->nMem;
drhb1fdb2a2008-01-05 04:06:03 +00002396 sqlite3VdbeAddOp2(v, OP_Copy, 0, iMem);
drh25303782004-12-07 15:41:48 +00002397 pExpr->op = TK_REGISTER;
2398 }
2399}
danielk197793758c82005-01-21 08:13:14 +00002400#endif
drh25303782004-12-07 15:41:48 +00002401
2402/*
drh268380c2004-02-25 13:47:31 +00002403** Generate code that pushes the value of every element of the given
drhf9b596e2004-05-26 16:54:42 +00002404** expression list onto the stack.
drh268380c2004-02-25 13:47:31 +00002405**
2406** Return the number of elements pushed onto the stack.
2407*/
danielk19774adee202004-05-08 08:23:19 +00002408int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00002409 Parse *pParse, /* Parsing context */
drh389a1ad2008-01-03 23:44:53 +00002410 ExprList *pList, /* The expression list to be coded */
2411 int target /* Where to write results */
drh268380c2004-02-25 13:47:31 +00002412){
2413 struct ExprList_item *pItem;
drh389a1ad2008-01-03 23:44:53 +00002414 int i, n, incr = 1;
drh268380c2004-02-25 13:47:31 +00002415 if( pList==0 ) return 0;
drh268380c2004-02-25 13:47:31 +00002416 n = pList->nExpr;
drh389a1ad2008-01-03 23:44:53 +00002417 if( target<0 ){
2418 target = pParse->nMem+1;
2419 pParse->nMem += n;
2420 }else if( target==0 ){
2421 incr = 0;
2422 }
drhc182d162005-08-14 20:47:16 +00002423 for(pItem=pList->a, i=n; i>0; i--, pItem++){
drh389a1ad2008-01-03 23:44:53 +00002424 sqlite3ExprCode(pParse, pItem->pExpr, target);
2425 target += incr;
drh268380c2004-02-25 13:47:31 +00002426 }
drhf9b596e2004-05-26 16:54:42 +00002427 return n;
drh268380c2004-02-25 13:47:31 +00002428}
2429
2430/*
drhcce7d172000-05-31 15:34:51 +00002431** Generate code for a boolean expression such that a jump is made
2432** to the label "dest" if the expression is true but execution
2433** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00002434**
2435** If the expression evaluates to NULL (neither true nor false), then
drh35573352008-01-08 23:54:25 +00002436** take the jump if the jumpIfNull flag is SQLITE_JUMPIFNULL.
drhf2bc0132004-10-04 13:19:23 +00002437**
2438** This code depends on the fact that certain token values (ex: TK_EQ)
2439** are the same as opcode values (ex: OP_Eq) that implement the corresponding
2440** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
2441** the make process cause these values to align. Assert()s in the code
2442** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00002443*/
danielk19774adee202004-05-08 08:23:19 +00002444void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00002445 Vdbe *v = pParse->pVdbe;
2446 int op = 0;
drh35573352008-01-08 23:54:25 +00002447 assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
drhdaffd0e2001-04-11 14:28:42 +00002448 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00002449 op = pExpr->op;
2450 switch( op ){
drhcce7d172000-05-31 15:34:51 +00002451 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00002452 int d2 = sqlite3VdbeMakeLabel(v);
drh35573352008-01-08 23:54:25 +00002453 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,jumpIfNull^SQLITE_JUMPIFNULL);
danielk19774adee202004-05-08 08:23:19 +00002454 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
2455 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00002456 break;
2457 }
2458 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00002459 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
2460 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002461 break;
2462 }
2463 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00002464 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002465 break;
2466 }
2467 case TK_LT:
2468 case TK_LE:
2469 case TK_GT:
2470 case TK_GE:
2471 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00002472 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00002473 assert( TK_LT==OP_Lt );
2474 assert( TK_LE==OP_Le );
2475 assert( TK_GT==OP_Gt );
2476 assert( TK_GE==OP_Ge );
2477 assert( TK_EQ==OP_Eq );
2478 assert( TK_NE==OP_Ne );
drh389a1ad2008-01-03 23:44:53 +00002479 sqlite3ExprCode(pParse, pExpr->pLeft, 0);
2480 sqlite3ExprCode(pParse, pExpr->pRight, 0);
drh35573352008-01-08 23:54:25 +00002481 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
2482 0, 0, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002483 break;
2484 }
2485 case TK_ISNULL:
2486 case TK_NOTNULL: {
drhf2bc0132004-10-04 13:19:23 +00002487 assert( TK_ISNULL==OP_IsNull );
2488 assert( TK_NOTNULL==OP_NotNull );
drh389a1ad2008-01-03 23:44:53 +00002489 sqlite3ExprCode(pParse, pExpr->pLeft, 0);
drh6a288a32008-01-07 19:20:24 +00002490 sqlite3VdbeAddOp2(v, op, 0, dest);
drhcce7d172000-05-31 15:34:51 +00002491 break;
2492 }
drhfef52082000-06-06 01:50:43 +00002493 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00002494 /* The expression "x BETWEEN y AND z" is implemented as:
2495 **
2496 ** 1 IF (x < y) GOTO 3
2497 ** 2 IF (x <= z) GOTO <dest>
2498 ** 3 ...
2499 */
drhf5905aa2002-05-26 20:54:33 +00002500 int addr;
drhbe5c89a2004-07-26 00:31:09 +00002501 Expr *pLeft = pExpr->pLeft;
2502 Expr *pRight = pExpr->pList->a[0].pExpr;
drh389a1ad2008-01-03 23:44:53 +00002503 sqlite3ExprCode(pParse, pLeft, 0);
drhb1fdb2a2008-01-05 04:06:03 +00002504 sqlite3VdbeAddOp0(v, OP_Copy);
drh389a1ad2008-01-03 23:44:53 +00002505 sqlite3ExprCode(pParse, pRight, 0);
drh35573352008-01-08 23:54:25 +00002506 addr = codeCompare(pParse, pLeft, pRight, OP_Lt, 0, 0, 0,
2507 jumpIfNull ^ SQLITE_JUMPIFNULL);
danielk19770202b292004-06-09 09:55:16 +00002508
drhbe5c89a2004-07-26 00:31:09 +00002509 pRight = pExpr->pList->a[1].pExpr;
drh389a1ad2008-01-03 23:44:53 +00002510 sqlite3ExprCode(pParse, pRight, 0);
drh35573352008-01-08 23:54:25 +00002511 codeCompare(pParse, pLeft, pRight, OP_Le, 0, 0, dest, jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00002512
drh66a51672008-01-03 00:01:23 +00002513 sqlite3VdbeAddOp2(v, OP_Integer, 0, 0);
drhd654be82005-09-20 17:42:23 +00002514 sqlite3VdbeJumpHere(v, addr);
drh66a51672008-01-03 00:01:23 +00002515 sqlite3VdbeAddOp2(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00002516 break;
2517 }
drhcce7d172000-05-31 15:34:51 +00002518 default: {
drh389a1ad2008-01-03 23:44:53 +00002519 sqlite3ExprCode(pParse, pExpr, 0);
drh3c84ddf2008-01-09 02:15:38 +00002520 sqlite3VdbeAddOp3(v, OP_If, 0, dest, jumpIfNull!=0);
drhcce7d172000-05-31 15:34:51 +00002521 break;
2522 }
2523 }
2524}
2525
2526/*
drh66b89c82000-11-28 20:47:17 +00002527** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00002528** to the label "dest" if the expression is false but execution
2529** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00002530**
2531** If the expression evaluates to NULL (neither true nor false) then
drh35573352008-01-08 23:54:25 +00002532** jump if jumpIfNull is SQLITE_JUMPIFNULL or fall through if jumpIfNull
2533** is 0.
drhcce7d172000-05-31 15:34:51 +00002534*/
danielk19774adee202004-05-08 08:23:19 +00002535void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00002536 Vdbe *v = pParse->pVdbe;
2537 int op = 0;
drh35573352008-01-08 23:54:25 +00002538 assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
drhdaffd0e2001-04-11 14:28:42 +00002539 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00002540
2541 /* The value of pExpr->op and op are related as follows:
2542 **
2543 ** pExpr->op op
2544 ** --------- ----------
2545 ** TK_ISNULL OP_NotNull
2546 ** TK_NOTNULL OP_IsNull
2547 ** TK_NE OP_Eq
2548 ** TK_EQ OP_Ne
2549 ** TK_GT OP_Le
2550 ** TK_LE OP_Gt
2551 ** TK_GE OP_Lt
2552 ** TK_LT OP_Ge
2553 **
2554 ** For other values of pExpr->op, op is undefined and unused.
2555 ** The value of TK_ and OP_ constants are arranged such that we
2556 ** can compute the mapping above using the following expression.
2557 ** Assert()s verify that the computation is correct.
2558 */
2559 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
2560
2561 /* Verify correct alignment of TK_ and OP_ constants
2562 */
2563 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
2564 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
2565 assert( pExpr->op!=TK_NE || op==OP_Eq );
2566 assert( pExpr->op!=TK_EQ || op==OP_Ne );
2567 assert( pExpr->op!=TK_LT || op==OP_Ge );
2568 assert( pExpr->op!=TK_LE || op==OP_Gt );
2569 assert( pExpr->op!=TK_GT || op==OP_Le );
2570 assert( pExpr->op!=TK_GE || op==OP_Lt );
2571
drhcce7d172000-05-31 15:34:51 +00002572 switch( pExpr->op ){
2573 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00002574 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
2575 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002576 break;
2577 }
2578 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00002579 int d2 = sqlite3VdbeMakeLabel(v);
drh35573352008-01-08 23:54:25 +00002580 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, jumpIfNull^SQLITE_JUMPIFNULL);
danielk19774adee202004-05-08 08:23:19 +00002581 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
2582 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00002583 break;
2584 }
2585 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00002586 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002587 break;
2588 }
2589 case TK_LT:
2590 case TK_LE:
2591 case TK_GT:
2592 case TK_GE:
2593 case TK_NE:
2594 case TK_EQ: {
drh389a1ad2008-01-03 23:44:53 +00002595 sqlite3ExprCode(pParse, pExpr->pLeft, 0);
2596 sqlite3ExprCode(pParse, pExpr->pRight, 0);
drh35573352008-01-08 23:54:25 +00002597 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
2598 0, 0, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002599 break;
2600 }
drhcce7d172000-05-31 15:34:51 +00002601 case TK_ISNULL:
2602 case TK_NOTNULL: {
drh389a1ad2008-01-03 23:44:53 +00002603 sqlite3ExprCode(pParse, pExpr->pLeft, 0);
drh6a288a32008-01-07 19:20:24 +00002604 sqlite3VdbeAddOp2(v, op, 0, dest);
drhcce7d172000-05-31 15:34:51 +00002605 break;
2606 }
drhfef52082000-06-06 01:50:43 +00002607 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00002608 /* The expression is "x BETWEEN y AND z". It is implemented as:
2609 **
2610 ** 1 IF (x >= y) GOTO 3
2611 ** 2 GOTO <dest>
2612 ** 3 IF (x > z) GOTO <dest>
2613 */
drhfef52082000-06-06 01:50:43 +00002614 int addr;
drhbe5c89a2004-07-26 00:31:09 +00002615 Expr *pLeft = pExpr->pLeft;
2616 Expr *pRight = pExpr->pList->a[0].pExpr;
drh389a1ad2008-01-03 23:44:53 +00002617 sqlite3ExprCode(pParse, pLeft, 0);
drhb1fdb2a2008-01-05 04:06:03 +00002618 sqlite3VdbeAddOp0(v, OP_Copy);
drh389a1ad2008-01-03 23:44:53 +00002619 sqlite3ExprCode(pParse, pRight, 0);
danielk19774adee202004-05-08 08:23:19 +00002620 addr = sqlite3VdbeCurrentAddr(v);
drh35573352008-01-08 23:54:25 +00002621 codeCompare(pParse, pLeft, pRight, OP_Ge,
2622 0, 0, addr+3, jumpIfNull ^ SQLITE_JUMPIFNULL);
drhbe5c89a2004-07-26 00:31:09 +00002623
drh66a51672008-01-03 00:01:23 +00002624 sqlite3VdbeAddOp2(v, OP_Pop, 1, 0);
2625 sqlite3VdbeAddOp2(v, OP_Goto, 0, dest);
drhbe5c89a2004-07-26 00:31:09 +00002626 pRight = pExpr->pList->a[1].pExpr;
drh389a1ad2008-01-03 23:44:53 +00002627 sqlite3ExprCode(pParse, pRight, 0);
drh35573352008-01-08 23:54:25 +00002628 codeCompare(pParse, pLeft, pRight, OP_Gt,
2629 0, 0, dest, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00002630 break;
2631 }
drhcce7d172000-05-31 15:34:51 +00002632 default: {
drh389a1ad2008-01-03 23:44:53 +00002633 sqlite3ExprCode(pParse, pExpr, 0);
drh3c84ddf2008-01-09 02:15:38 +00002634 sqlite3VdbeAddOp3(v, OP_IfNot, 0, dest, jumpIfNull!=0);
drhcce7d172000-05-31 15:34:51 +00002635 break;
2636 }
2637 }
2638}
drh22827922000-06-06 17:27:05 +00002639
2640/*
2641** Do a deep comparison of two expression trees. Return TRUE (non-zero)
2642** if they are identical and return FALSE if they differ in any way.
drhd40aab02007-02-24 15:29:03 +00002643**
2644** Sometimes this routine will return FALSE even if the two expressions
2645** really are equivalent. If we cannot prove that the expressions are
2646** identical, we return FALSE just to be safe. So if this routine
2647** returns false, then you do not really know for certain if the two
2648** expressions are the same. But if you get a TRUE return, then you
2649** can be sure the expressions are the same. In the places where
2650** this routine is used, it does not hurt to get an extra FALSE - that
2651** just might result in some slightly slower code. But returning
2652** an incorrect TRUE could lead to a malfunction.
drh22827922000-06-06 17:27:05 +00002653*/
danielk19774adee202004-05-08 08:23:19 +00002654int sqlite3ExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00002655 int i;
danielk19774b202ae2006-01-23 05:50:58 +00002656 if( pA==0||pB==0 ){
2657 return pB==pA;
drh22827922000-06-06 17:27:05 +00002658 }
2659 if( pA->op!=pB->op ) return 0;
drhfd357972005-09-09 01:33:19 +00002660 if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 0;
danielk19774adee202004-05-08 08:23:19 +00002661 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
2662 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00002663 if( pA->pList ){
2664 if( pB->pList==0 ) return 0;
2665 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
2666 for(i=0; i<pA->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00002667 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00002668 return 0;
2669 }
2670 }
2671 }else if( pB->pList ){
2672 return 0;
2673 }
2674 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00002675 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drhdd735212007-02-24 13:53:05 +00002676 if( pA->op!=TK_COLUMN && pA->token.z ){
drh22827922000-06-06 17:27:05 +00002677 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00002678 if( pB->token.n!=pA->token.n ) return 0;
drh2646da72005-12-09 20:02:05 +00002679 if( sqlite3StrNICmp((char*)pA->token.z,(char*)pB->token.z,pB->token.n)!=0 ){
2680 return 0;
2681 }
drh22827922000-06-06 17:27:05 +00002682 }
2683 return 1;
2684}
2685
drh13449892005-09-07 21:22:45 +00002686
drh22827922000-06-06 17:27:05 +00002687/*
drh13449892005-09-07 21:22:45 +00002688** Add a new element to the pAggInfo->aCol[] array. Return the index of
2689** the new element. Return a negative number if malloc fails.
drh22827922000-06-06 17:27:05 +00002690*/
drh17435752007-08-16 04:30:38 +00002691static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){
drh13449892005-09-07 21:22:45 +00002692 int i;
drhcf643722007-03-27 13:36:37 +00002693 pInfo->aCol = sqlite3ArrayAllocate(
drh17435752007-08-16 04:30:38 +00002694 db,
drhcf643722007-03-27 13:36:37 +00002695 pInfo->aCol,
2696 sizeof(pInfo->aCol[0]),
2697 3,
2698 &pInfo->nColumn,
2699 &pInfo->nColumnAlloc,
2700 &i
2701 );
drh13449892005-09-07 21:22:45 +00002702 return i;
2703}
2704
2705/*
2706** Add a new element to the pAggInfo->aFunc[] array. Return the index of
2707** the new element. Return a negative number if malloc fails.
2708*/
drh17435752007-08-16 04:30:38 +00002709static int addAggInfoFunc(sqlite3 *db, AggInfo *pInfo){
drh13449892005-09-07 21:22:45 +00002710 int i;
drhcf643722007-03-27 13:36:37 +00002711 pInfo->aFunc = sqlite3ArrayAllocate(
drh17435752007-08-16 04:30:38 +00002712 db,
drhcf643722007-03-27 13:36:37 +00002713 pInfo->aFunc,
2714 sizeof(pInfo->aFunc[0]),
2715 3,
2716 &pInfo->nFunc,
2717 &pInfo->nFuncAlloc,
2718 &i
2719 );
drh13449892005-09-07 21:22:45 +00002720 return i;
2721}
drh22827922000-06-06 17:27:05 +00002722
2723/*
drh626a8792005-01-17 22:08:19 +00002724** This is an xFunc for walkExprTree() used to implement
2725** sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates
2726** for additional information.
drh22827922000-06-06 17:27:05 +00002727**
drh626a8792005-01-17 22:08:19 +00002728** This routine analyzes the aggregate function at pExpr.
drh22827922000-06-06 17:27:05 +00002729*/
drh626a8792005-01-17 22:08:19 +00002730static int analyzeAggregate(void *pArg, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00002731 int i;
danielk1977a58fdfb2005-02-08 07:50:40 +00002732 NameContext *pNC = (NameContext *)pArg;
2733 Parse *pParse = pNC->pParse;
2734 SrcList *pSrcList = pNC->pSrcList;
drh13449892005-09-07 21:22:45 +00002735 AggInfo *pAggInfo = pNC->pAggInfo;
drh22827922000-06-06 17:27:05 +00002736
drh22827922000-06-06 17:27:05 +00002737 switch( pExpr->op ){
drh89c69d02007-01-04 01:20:28 +00002738 case TK_AGG_COLUMN:
drh967e8b72000-06-21 13:59:10 +00002739 case TK_COLUMN: {
drh13449892005-09-07 21:22:45 +00002740 /* Check to see if the column is in one of the tables in the FROM
2741 ** clause of the aggregate query */
2742 if( pSrcList ){
2743 struct SrcList_item *pItem = pSrcList->a;
2744 for(i=0; i<pSrcList->nSrc; i++, pItem++){
2745 struct AggInfo_col *pCol;
2746 if( pExpr->iTable==pItem->iCursor ){
2747 /* If we reach this point, it means that pExpr refers to a table
2748 ** that is in the FROM clause of the aggregate query.
2749 **
2750 ** Make an entry for the column in pAggInfo->aCol[] if there
2751 ** is not an entry there already.
2752 */
drh7f906d62007-03-12 23:48:52 +00002753 int k;
drh13449892005-09-07 21:22:45 +00002754 pCol = pAggInfo->aCol;
drh7f906d62007-03-12 23:48:52 +00002755 for(k=0; k<pAggInfo->nColumn; k++, pCol++){
drh13449892005-09-07 21:22:45 +00002756 if( pCol->iTable==pExpr->iTable &&
2757 pCol->iColumn==pExpr->iColumn ){
2758 break;
2759 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002760 }
danielk19771e536952007-08-16 10:09:01 +00002761 if( (k>=pAggInfo->nColumn)
2762 && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0
2763 ){
drh7f906d62007-03-12 23:48:52 +00002764 pCol = &pAggInfo->aCol[k];
danielk19770817d0d2007-02-14 09:19:36 +00002765 pCol->pTab = pExpr->pTab;
drh13449892005-09-07 21:22:45 +00002766 pCol->iTable = pExpr->iTable;
2767 pCol->iColumn = pExpr->iColumn;
drh0a07c102008-01-03 18:03:08 +00002768 pCol->iMem = ++pParse->nMem;
drh13449892005-09-07 21:22:45 +00002769 pCol->iSorterColumn = -1;
drh5774b802005-09-07 22:48:16 +00002770 pCol->pExpr = pExpr;
drh13449892005-09-07 21:22:45 +00002771 if( pAggInfo->pGroupBy ){
2772 int j, n;
2773 ExprList *pGB = pAggInfo->pGroupBy;
2774 struct ExprList_item *pTerm = pGB->a;
2775 n = pGB->nExpr;
2776 for(j=0; j<n; j++, pTerm++){
2777 Expr *pE = pTerm->pExpr;
2778 if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable &&
2779 pE->iColumn==pExpr->iColumn ){
2780 pCol->iSorterColumn = j;
2781 break;
2782 }
2783 }
2784 }
2785 if( pCol->iSorterColumn<0 ){
2786 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
2787 }
2788 }
2789 /* There is now an entry for pExpr in pAggInfo->aCol[] (either
2790 ** because it was there before or because we just created it).
2791 ** Convert the pExpr to be a TK_AGG_COLUMN referring to that
2792 ** pAggInfo->aCol[] entry.
2793 */
2794 pExpr->pAggInfo = pAggInfo;
2795 pExpr->op = TK_AGG_COLUMN;
drh7f906d62007-03-12 23:48:52 +00002796 pExpr->iAgg = k;
drh13449892005-09-07 21:22:45 +00002797 break;
2798 } /* endif pExpr->iTable==pItem->iCursor */
2799 } /* end loop over pSrcList */
drh22827922000-06-06 17:27:05 +00002800 }
drh626a8792005-01-17 22:08:19 +00002801 return 1;
drh22827922000-06-06 17:27:05 +00002802 }
2803 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00002804 /* The pNC->nDepth==0 test causes aggregate functions in subqueries
2805 ** to be ignored */
danielk1977a58fdfb2005-02-08 07:50:40 +00002806 if( pNC->nDepth==0 ){
drh13449892005-09-07 21:22:45 +00002807 /* Check to see if pExpr is a duplicate of another aggregate
2808 ** function that is already in the pAggInfo structure
2809 */
2810 struct AggInfo_func *pItem = pAggInfo->aFunc;
2811 for(i=0; i<pAggInfo->nFunc; i++, pItem++){
2812 if( sqlite3ExprCompare(pItem->pExpr, pExpr) ){
danielk1977a58fdfb2005-02-08 07:50:40 +00002813 break;
2814 }
drh22827922000-06-06 17:27:05 +00002815 }
drh13449892005-09-07 21:22:45 +00002816 if( i>=pAggInfo->nFunc ){
2817 /* pExpr is original. Make a new entry in pAggInfo->aFunc[]
2818 */
danielk197714db2662006-01-09 16:12:04 +00002819 u8 enc = ENC(pParse->db);
danielk19771e536952007-08-16 10:09:01 +00002820 i = addAggInfoFunc(pParse->db, pAggInfo);
drh13449892005-09-07 21:22:45 +00002821 if( i>=0 ){
2822 pItem = &pAggInfo->aFunc[i];
2823 pItem->pExpr = pExpr;
drh0a07c102008-01-03 18:03:08 +00002824 pItem->iMem = ++pParse->nMem;
drh13449892005-09-07 21:22:45 +00002825 pItem->pFunc = sqlite3FindFunction(pParse->db,
drh2646da72005-12-09 20:02:05 +00002826 (char*)pExpr->token.z, pExpr->token.n,
drh13449892005-09-07 21:22:45 +00002827 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
drhfd357972005-09-09 01:33:19 +00002828 if( pExpr->flags & EP_Distinct ){
2829 pItem->iDistinct = pParse->nTab++;
2830 }else{
2831 pItem->iDistinct = -1;
2832 }
drh13449892005-09-07 21:22:45 +00002833 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002834 }
drh13449892005-09-07 21:22:45 +00002835 /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
2836 */
danielk1977a58fdfb2005-02-08 07:50:40 +00002837 pExpr->iAgg = i;
drh13449892005-09-07 21:22:45 +00002838 pExpr->pAggInfo = pAggInfo;
danielk1977a58fdfb2005-02-08 07:50:40 +00002839 return 1;
drh22827922000-06-06 17:27:05 +00002840 }
drh22827922000-06-06 17:27:05 +00002841 }
2842 }
drh13449892005-09-07 21:22:45 +00002843
2844 /* Recursively walk subqueries looking for TK_COLUMN nodes that need
2845 ** to be changed to TK_AGG_COLUMN. But increment nDepth so that
2846 ** TK_AGG_FUNCTION nodes in subqueries will be unchanged.
2847 */
danielk1977a58fdfb2005-02-08 07:50:40 +00002848 if( pExpr->pSelect ){
2849 pNC->nDepth++;
2850 walkSelectExpr(pExpr->pSelect, analyzeAggregate, pNC);
2851 pNC->nDepth--;
2852 }
drh626a8792005-01-17 22:08:19 +00002853 return 0;
2854}
2855
2856/*
2857** Analyze the given expression looking for aggregate functions and
2858** for variables that need to be added to the pParse->aAgg[] array.
2859** Make additional entries to the pParse->aAgg[] array as necessary.
2860**
2861** This routine should only be called after the expression has been
2862** analyzed by sqlite3ExprResolveNames().
2863**
2864** If errors are seen, leave an error message in zErrMsg and return
2865** the number of errors.
2866*/
danielk1977a58fdfb2005-02-08 07:50:40 +00002867int sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
2868 int nErr = pNC->pParse->nErr;
2869 walkExprTree(pExpr, analyzeAggregate, pNC);
2870 return pNC->pParse->nErr - nErr;
drh22827922000-06-06 17:27:05 +00002871}
drh5d9a4af2005-08-30 00:54:01 +00002872
2873/*
2874** Call sqlite3ExprAnalyzeAggregates() for every expression in an
2875** expression list. Return the number of errors.
2876**
2877** If an error is found, the analysis is cut short.
2878*/
2879int sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){
2880 struct ExprList_item *pItem;
2881 int i;
2882 int nErr = 0;
2883 if( pList ){
2884 for(pItem=pList->a, i=0; nErr==0 && i<pList->nExpr; i++, pItem++){
2885 nErr += sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
2886 }
2887 }
2888 return nErr;
2889}