blob: 214193ca1093f62a71cf3085a58d0be14dacc30d [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**
drh49df6b72007-12-14 15:12:21 +000015** $Id: expr.c,v 1.320 2007/12/14 15:12:21 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/*
161** Return the P1 value that should be used for a binary comparison
162** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
163** If jumpIfNull is true, then set the low byte of the returned
164** P1 value to tell the opcode to jump if either expression
165** evaluates to NULL.
166*/
danielk1977e014a832004-05-17 10:48:57 +0000167static int binaryCompareP1(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){
danielk1977bf3b7212004-05-18 10:06:24 +0000168 char aff = sqlite3ExprAffinity(pExpr2);
drhf0863fe2005-06-12 21:35:51 +0000169 return ((int)sqlite3CompareAffinity(pExpr1, aff))+(jumpIfNull?0x100:0);
danielk1977a37cdde2004-05-16 11:15:36 +0000170}
171
drha2e00042002-01-22 03:13:42 +0000172/*
danielk19770202b292004-06-09 09:55:16 +0000173** Return a pointer to the collation sequence that should be used by
174** a binary comparison operator comparing pLeft and pRight.
175**
176** If the left hand expression has a collating sequence type, then it is
177** used. Otherwise the collation sequence for the right hand expression
178** is used, or the default (BINARY) if neither expression has a collating
179** type.
danielk1977bcbb04e2007-05-29 12:11:29 +0000180**
181** Argument pRight (but not pLeft) may be a null pointer. In this case,
182** it is not considered.
danielk19770202b292004-06-09 09:55:16 +0000183*/
drh0a0e1312007-08-07 17:04:59 +0000184CollSeq *sqlite3BinaryCompareCollSeq(
danielk1977bcbb04e2007-05-29 12:11:29 +0000185 Parse *pParse,
186 Expr *pLeft,
187 Expr *pRight
188){
drhec41dda2007-02-07 13:09:45 +0000189 CollSeq *pColl;
190 assert( pLeft );
drhec41dda2007-02-07 13:09:45 +0000191 if( pLeft->flags & EP_ExpCollate ){
192 assert( pLeft->pColl );
193 pColl = pLeft->pColl;
danielk1977bcbb04e2007-05-29 12:11:29 +0000194 }else if( pRight && pRight->flags & EP_ExpCollate ){
drhec41dda2007-02-07 13:09:45 +0000195 assert( pRight->pColl );
196 pColl = pRight->pColl;
197 }else{
198 pColl = sqlite3ExprCollSeq(pParse, pLeft);
199 if( !pColl ){
200 pColl = sqlite3ExprCollSeq(pParse, pRight);
201 }
danielk19770202b292004-06-09 09:55:16 +0000202 }
203 return pColl;
204}
205
206/*
drhbe5c89a2004-07-26 00:31:09 +0000207** Generate code for a comparison operator.
208*/
209static int codeCompare(
210 Parse *pParse, /* The parsing (and code generating) context */
211 Expr *pLeft, /* The left operand */
212 Expr *pRight, /* The right operand */
213 int opcode, /* The comparison opcode */
214 int dest, /* Jump here if true. */
215 int jumpIfNull /* If true, jump if either operand is NULL */
216){
217 int p1 = binaryCompareP1(pLeft, pRight, jumpIfNull);
danielk1977bcbb04e2007-05-29 12:11:29 +0000218 CollSeq *p3 = sqlite3BinaryCompareCollSeq(pParse, pLeft, pRight);
drh94a11212004-09-25 13:12:14 +0000219 return sqlite3VdbeOp3(pParse->pVdbe, opcode, p1, dest, (void*)p3, P3_COLLSEQ);
drhbe5c89a2004-07-26 00:31:09 +0000220}
221
222/*
drha76b5df2002-02-23 02:32:10 +0000223** Construct a new expression node and return a pointer to it. Memory
drh17435752007-08-16 04:30:38 +0000224** for this node is obtained from sqlite3_malloc(). The calling function
drha76b5df2002-02-23 02:32:10 +0000225** is responsible for making sure the node eventually gets freed.
226*/
drh17435752007-08-16 04:30:38 +0000227Expr *sqlite3Expr(
danielk1977a1644fd2007-08-29 12:31:25 +0000228 sqlite3 *db, /* Handle for sqlite3DbMallocZero() (may be null) */
drh17435752007-08-16 04:30:38 +0000229 int op, /* Expression opcode */
230 Expr *pLeft, /* Left operand */
231 Expr *pRight, /* Right operand */
232 const Token *pToken /* Argument token */
233){
drha76b5df2002-02-23 02:32:10 +0000234 Expr *pNew;
danielk1977a1644fd2007-08-29 12:31:25 +0000235 pNew = sqlite3DbMallocZero(db, sizeof(Expr));
drha76b5df2002-02-23 02:32:10 +0000236 if( pNew==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000237 /* When malloc fails, delete pLeft and pRight. Expressions passed to
238 ** this function must always be allocated with sqlite3Expr() for this
239 ** reason.
240 */
241 sqlite3ExprDelete(pLeft);
242 sqlite3ExprDelete(pRight);
drha76b5df2002-02-23 02:32:10 +0000243 return 0;
244 }
245 pNew->op = op;
246 pNew->pLeft = pLeft;
247 pNew->pRight = pRight;
danielk1977a58fdfb2005-02-08 07:50:40 +0000248 pNew->iAgg = -1;
drha76b5df2002-02-23 02:32:10 +0000249 if( pToken ){
drh4b59ab52002-08-24 18:24:51 +0000250 assert( pToken->dyn==0 );
drh145716b2004-09-24 12:24:06 +0000251 pNew->span = pNew->token = *pToken;
drha34001c2007-02-02 12:44:37 +0000252 }else if( pLeft ){
253 if( pRight ){
254 sqlite3ExprSpan(pNew, &pLeft->span, &pRight->span);
drh5ffb3ac2007-04-18 17:07:57 +0000255 if( pRight->flags & EP_ExpCollate ){
drha34001c2007-02-02 12:44:37 +0000256 pNew->flags |= EP_ExpCollate;
257 pNew->pColl = pRight->pColl;
258 }
259 }
drh5ffb3ac2007-04-18 17:07:57 +0000260 if( pLeft->flags & EP_ExpCollate ){
drha34001c2007-02-02 12:44:37 +0000261 pNew->flags |= EP_ExpCollate;
262 pNew->pColl = pLeft->pColl;
263 }
drha76b5df2002-02-23 02:32:10 +0000264 }
danielk1977fc976062007-05-10 10:46:56 +0000265
266 sqlite3ExprSetHeight(pNew);
drha76b5df2002-02-23 02:32:10 +0000267 return pNew;
268}
269
270/*
drh17435752007-08-16 04:30:38 +0000271** Works like sqlite3Expr() except that it takes an extra Parse*
272** argument and notifies the associated connection object if malloc fails.
drh206f3d92006-07-11 13:15:08 +0000273*/
drh17435752007-08-16 04:30:38 +0000274Expr *sqlite3PExpr(
275 Parse *pParse, /* Parsing context */
276 int op, /* Expression opcode */
277 Expr *pLeft, /* Left operand */
278 Expr *pRight, /* Right operand */
279 const Token *pToken /* Argument token */
280){
danielk1977a1644fd2007-08-29 12:31:25 +0000281 return sqlite3Expr(pParse->db, op, pLeft, pRight, pToken);
drh206f3d92006-07-11 13:15:08 +0000282}
283
284/*
drh4e0cff62004-11-05 05:10:28 +0000285** When doing a nested parse, you can include terms in an expression
286** that look like this: #0 #1 #2 ... These terms refer to elements
drh288d37f2005-06-22 08:48:06 +0000287** on the stack. "#0" means the top of the stack.
288** "#1" means the next down on the stack. And so forth.
drh4e0cff62004-11-05 05:10:28 +0000289**
290** This routine is called by the parser to deal with on of those terms.
291** It immediately generates code to store the value in a memory location.
292** The returns an expression that will code to extract the value from
293** that memory location as needed.
294*/
295Expr *sqlite3RegisterExpr(Parse *pParse, Token *pToken){
296 Vdbe *v = pParse->pVdbe;
297 Expr *p;
298 int depth;
drh4e0cff62004-11-05 05:10:28 +0000299 if( pParse->nested==0 ){
300 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", pToken);
danielk1977a1644fd2007-08-29 12:31:25 +0000301 return sqlite3PExpr(pParse, TK_NULL, 0, 0, 0);
drh4e0cff62004-11-05 05:10:28 +0000302 }
drhbb7ac002005-08-12 22:58:53 +0000303 if( v==0 ) return 0;
danielk1977a1644fd2007-08-29 12:31:25 +0000304 p = sqlite3PExpr(pParse, TK_REGISTER, 0, 0, pToken);
drh73c42a12004-11-20 18:13:10 +0000305 if( p==0 ){
306 return 0; /* Malloc failed */
307 }
drh2646da72005-12-09 20:02:05 +0000308 depth = atoi((char*)&pToken->z[1]);
drh288d37f2005-06-22 08:48:06 +0000309 p->iTable = pParse->nMem++;
310 sqlite3VdbeAddOp(v, OP_Dup, depth, 0);
311 sqlite3VdbeAddOp(v, OP_MemStore, p->iTable, 1);
drh4e0cff62004-11-05 05:10:28 +0000312 return p;
313}
314
315/*
drh91bb0ee2004-09-01 03:06:34 +0000316** Join two expressions using an AND operator. If either expression is
317** NULL, then just return the other expression.
318*/
danielk19771e536952007-08-16 10:09:01 +0000319Expr *sqlite3ExprAnd(sqlite3 *db, Expr *pLeft, Expr *pRight){
drh91bb0ee2004-09-01 03:06:34 +0000320 if( pLeft==0 ){
321 return pRight;
322 }else if( pRight==0 ){
323 return pLeft;
324 }else{
danielk1977880c15b2007-09-01 18:24:55 +0000325 return sqlite3Expr(db, TK_AND, pLeft, pRight, 0);
drh91bb0ee2004-09-01 03:06:34 +0000326 }
327}
328
329/*
drh6977fea2002-10-22 23:38:04 +0000330** Set the Expr.span field of the given expression to span all
drha76b5df2002-02-23 02:32:10 +0000331** text between the two given tokens.
332*/
danielk19774adee202004-05-08 08:23:19 +0000333void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
drh4efc4752004-01-16 15:55:37 +0000334 assert( pRight!=0 );
335 assert( pLeft!=0 );
drhf3a65f72007-08-22 20:18:21 +0000336 if( pExpr && pRight->z && pLeft->z ){
drhad6d9462004-09-19 02:15:24 +0000337 assert( pLeft->dyn==0 || pLeft->z[pLeft->n]==0 );
drh145716b2004-09-24 12:24:06 +0000338 if( pLeft->dyn==0 && pRight->dyn==0 ){
drh6977fea2002-10-22 23:38:04 +0000339 pExpr->span.z = pLeft->z;
drh97903fe2005-05-24 20:19:57 +0000340 pExpr->span.n = pRight->n + (pRight->z - pLeft->z);
drh4b59ab52002-08-24 18:24:51 +0000341 }else{
drh6977fea2002-10-22 23:38:04 +0000342 pExpr->span.z = 0;
drh4b59ab52002-08-24 18:24:51 +0000343 }
drha76b5df2002-02-23 02:32:10 +0000344 }
345}
346
347/*
348** Construct a new expression node for a function with multiple
349** arguments.
350*/
drh17435752007-08-16 04:30:38 +0000351Expr *sqlite3ExprFunction(Parse *pParse, ExprList *pList, Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000352 Expr *pNew;
danielk19774b202ae2006-01-23 05:50:58 +0000353 assert( pToken );
drh17435752007-08-16 04:30:38 +0000354 pNew = sqlite3DbMallocZero(pParse->db, sizeof(Expr) );
drha76b5df2002-02-23 02:32:10 +0000355 if( pNew==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000356 sqlite3ExprListDelete(pList); /* Avoid leaking memory when malloc fails */
drha76b5df2002-02-23 02:32:10 +0000357 return 0;
358 }
359 pNew->op = TK_FUNCTION;
360 pNew->pList = pList;
danielk19774b202ae2006-01-23 05:50:58 +0000361 assert( pToken->dyn==0 );
362 pNew->token = *pToken;
drh6977fea2002-10-22 23:38:04 +0000363 pNew->span = pNew->token;
danielk1977fc976062007-05-10 10:46:56 +0000364
365 sqlite3ExprSetHeight(pNew);
drha76b5df2002-02-23 02:32:10 +0000366 return pNew;
367}
368
369/*
drhfa6bc002004-09-07 16:19:52 +0000370** Assign a variable number to an expression that encodes a wildcard
371** in the original SQL statement.
372**
373** Wildcards consisting of a single "?" are assigned the next sequential
374** variable number.
375**
376** Wildcards of the form "?nnn" are assigned the number "nnn". We make
377** sure "nnn" is not too be to avoid a denial of service attack when
378** the SQL statement comes from an external source.
379**
380** Wildcards of the form ":aaa" or "$aaa" are assigned the same number
381** as the previous instance of the same wildcard. Or if this is the first
382** instance of the wildcard, the next sequenial variable number is
383** assigned.
384*/
385void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){
386 Token *pToken;
drh17435752007-08-16 04:30:38 +0000387 sqlite3 *db = pParse->db;
388
drhfa6bc002004-09-07 16:19:52 +0000389 if( pExpr==0 ) return;
390 pToken = &pExpr->token;
391 assert( pToken->n>=1 );
392 assert( pToken->z!=0 );
393 assert( pToken->z[0]!=0 );
394 if( pToken->n==1 ){
395 /* Wildcard of the form "?". Assign the next variable number */
396 pExpr->iTable = ++pParse->nVar;
397 }else if( pToken->z[0]=='?' ){
398 /* Wildcard of the form "?nnn". Convert "nnn" to an integer and
399 ** use it as the variable number */
400 int i;
drh2646da72005-12-09 20:02:05 +0000401 pExpr->iTable = i = atoi((char*)&pToken->z[1]);
drhfa6bc002004-09-07 16:19:52 +0000402 if( i<1 || i>SQLITE_MAX_VARIABLE_NUMBER ){
403 sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
404 SQLITE_MAX_VARIABLE_NUMBER);
405 }
406 if( i>pParse->nVar ){
407 pParse->nVar = i;
408 }
409 }else{
410 /* Wildcards of the form ":aaa" or "$aaa". Reuse the same variable
411 ** number as the prior appearance of the same name, or if the name
412 ** has never appeared before, reuse the same variable number
413 */
414 int i, n;
415 n = pToken->n;
416 for(i=0; i<pParse->nVarExpr; i++){
417 Expr *pE;
418 if( (pE = pParse->apVarExpr[i])!=0
419 && pE->token.n==n
420 && memcmp(pE->token.z, pToken->z, n)==0 ){
421 pExpr->iTable = pE->iTable;
422 break;
423 }
424 }
425 if( i>=pParse->nVarExpr ){
426 pExpr->iTable = ++pParse->nVar;
427 if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){
428 pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10;
drh17435752007-08-16 04:30:38 +0000429 pParse->apVarExpr =
430 sqlite3DbReallocOrFree(
431 db,
432 pParse->apVarExpr,
433 pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0])
434 );
drhfa6bc002004-09-07 16:19:52 +0000435 }
drh17435752007-08-16 04:30:38 +0000436 if( !db->mallocFailed ){
drhfa6bc002004-09-07 16:19:52 +0000437 assert( pParse->apVarExpr!=0 );
438 pParse->apVarExpr[pParse->nVarExpr++] = pExpr;
439 }
440 }
441 }
danielk1977832b2662007-05-09 11:37:22 +0000442 if( !pParse->nErr && pParse->nVar>SQLITE_MAX_VARIABLE_NUMBER ){
443 sqlite3ErrorMsg(pParse, "too many SQL variables");
444 }
drhfa6bc002004-09-07 16:19:52 +0000445}
446
447/*
drha2e00042002-01-22 03:13:42 +0000448** Recursively delete an expression tree.
449*/
danielk19774adee202004-05-08 08:23:19 +0000450void sqlite3ExprDelete(Expr *p){
drha2e00042002-01-22 03:13:42 +0000451 if( p==0 ) return;
drh17435752007-08-16 04:30:38 +0000452 if( p->span.dyn ) sqlite3_free((char*)p->span.z);
453 if( p->token.dyn ) sqlite3_free((char*)p->token.z);
danielk19774adee202004-05-08 08:23:19 +0000454 sqlite3ExprDelete(p->pLeft);
455 sqlite3ExprDelete(p->pRight);
456 sqlite3ExprListDelete(p->pList);
457 sqlite3SelectDelete(p->pSelect);
drh17435752007-08-16 04:30:38 +0000458 sqlite3_free(p);
drha2e00042002-01-22 03:13:42 +0000459}
460
drhd2687b72005-08-12 22:56:09 +0000461/*
462** The Expr.token field might be a string literal that is quoted.
463** If so, remove the quotation marks.
464*/
drh17435752007-08-16 04:30:38 +0000465void sqlite3DequoteExpr(sqlite3 *db, Expr *p){
drhd2687b72005-08-12 22:56:09 +0000466 if( ExprHasAnyProperty(p, EP_Dequoted) ){
467 return;
468 }
469 ExprSetProperty(p, EP_Dequoted);
470 if( p->token.dyn==0 ){
drh17435752007-08-16 04:30:38 +0000471 sqlite3TokenCopy(db, &p->token, &p->token);
drhd2687b72005-08-12 22:56:09 +0000472 }
473 sqlite3Dequote((char*)p->token.z);
474}
475
drha76b5df2002-02-23 02:32:10 +0000476
477/*
drhff78bd22002-02-27 01:47:11 +0000478** The following group of routines make deep copies of expressions,
479** expression lists, ID lists, and select statements. The copies can
480** be deleted (by being passed to their respective ...Delete() routines)
481** without effecting the originals.
482**
danielk19774adee202004-05-08 08:23:19 +0000483** The expression list, ID, and source lists return by sqlite3ExprListDup(),
484** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
drhad3cab52002-05-24 02:04:32 +0000485** by subsequent calls to sqlite*ListAppend() routines.
drhff78bd22002-02-27 01:47:11 +0000486**
drhad3cab52002-05-24 02:04:32 +0000487** Any tables that the SrcList might point to are not duplicated.
drhff78bd22002-02-27 01:47:11 +0000488*/
danielk19771e536952007-08-16 10:09:01 +0000489Expr *sqlite3ExprDup(sqlite3 *db, Expr *p){
drhff78bd22002-02-27 01:47:11 +0000490 Expr *pNew;
491 if( p==0 ) return 0;
drh17435752007-08-16 04:30:38 +0000492 pNew = sqlite3DbMallocRaw(db, sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000493 if( pNew==0 ) return 0;
drh3b167c72002-06-28 12:18:47 +0000494 memcpy(pNew, p, sizeof(*pNew));
drh6977fea2002-10-22 23:38:04 +0000495 if( p->token.z!=0 ){
drh17435752007-08-16 04:30:38 +0000496 pNew->token.z = (u8*)sqlite3DbStrNDup(db, (char*)p->token.z, p->token.n);
drh4b59ab52002-08-24 18:24:51 +0000497 pNew->token.dyn = 1;
498 }else{
drh4efc4752004-01-16 15:55:37 +0000499 assert( pNew->token.z==0 );
drh4b59ab52002-08-24 18:24:51 +0000500 }
drh6977fea2002-10-22 23:38:04 +0000501 pNew->span.z = 0;
drh17435752007-08-16 04:30:38 +0000502 pNew->pLeft = sqlite3ExprDup(db, p->pLeft);
503 pNew->pRight = sqlite3ExprDup(db, p->pRight);
504 pNew->pList = sqlite3ExprListDup(db, p->pList);
505 pNew->pSelect = sqlite3SelectDup(db, p->pSelect);
drhff78bd22002-02-27 01:47:11 +0000506 return pNew;
507}
drh17435752007-08-16 04:30:38 +0000508void sqlite3TokenCopy(sqlite3 *db, Token *pTo, Token *pFrom){
509 if( pTo->dyn ) sqlite3_free((char*)pTo->z);
drh4b59ab52002-08-24 18:24:51 +0000510 if( pFrom->z ){
511 pTo->n = pFrom->n;
drh17435752007-08-16 04:30:38 +0000512 pTo->z = (u8*)sqlite3DbStrNDup(db, (char*)pFrom->z, pFrom->n);
drh4b59ab52002-08-24 18:24:51 +0000513 pTo->dyn = 1;
514 }else{
drh4b59ab52002-08-24 18:24:51 +0000515 pTo->z = 0;
drh4b59ab52002-08-24 18:24:51 +0000516 }
517}
drh17435752007-08-16 04:30:38 +0000518ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p){
drhff78bd22002-02-27 01:47:11 +0000519 ExprList *pNew;
drh145716b2004-09-24 12:24:06 +0000520 struct ExprList_item *pItem, *pOldItem;
drhff78bd22002-02-27 01:47:11 +0000521 int i;
522 if( p==0 ) return 0;
drh17435752007-08-16 04:30:38 +0000523 pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +0000524 if( pNew==0 ) return 0;
danielk197731dad9d2007-08-16 11:36:15 +0000525 pNew->iECursor = 0;
drh4305d102003-07-30 12:34:12 +0000526 pNew->nExpr = pNew->nAlloc = p->nExpr;
drh17435752007-08-16 04:30:38 +0000527 pNew->a = pItem = sqlite3DbMallocRaw(db, p->nExpr*sizeof(p->a[0]) );
danielk1977e0048402004-06-15 16:51:01 +0000528 if( pItem==0 ){
drh17435752007-08-16 04:30:38 +0000529 sqlite3_free(pNew);
danielk1977e0048402004-06-15 16:51:01 +0000530 return 0;
531 }
drh145716b2004-09-24 12:24:06 +0000532 pOldItem = p->a;
533 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
drh4b59ab52002-08-24 18:24:51 +0000534 Expr *pNewExpr, *pOldExpr;
drh17435752007-08-16 04:30:38 +0000535 pItem->pExpr = pNewExpr = sqlite3ExprDup(db, pOldExpr = pOldItem->pExpr);
drh6977fea2002-10-22 23:38:04 +0000536 if( pOldExpr->span.z!=0 && pNewExpr ){
537 /* Always make a copy of the span for top-level expressions in the
drh4b59ab52002-08-24 18:24:51 +0000538 ** expression list. The logic in SELECT processing that determines
539 ** the names of columns in the result set needs this information */
drh17435752007-08-16 04:30:38 +0000540 sqlite3TokenCopy(db, &pNewExpr->span, &pOldExpr->span);
drh4b59ab52002-08-24 18:24:51 +0000541 }
drh1f3e9052002-10-31 00:09:39 +0000542 assert( pNewExpr==0 || pNewExpr->span.z!=0
drh6f7adc82006-01-11 21:41:20 +0000543 || pOldExpr->span.z==0
drh17435752007-08-16 04:30:38 +0000544 || db->mallocFailed );
545 pItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
drh145716b2004-09-24 12:24:06 +0000546 pItem->sortOrder = pOldItem->sortOrder;
547 pItem->isAgg = pOldItem->isAgg;
drh3e7bc9c2004-02-21 19:17:17 +0000548 pItem->done = 0;
drhff78bd22002-02-27 01:47:11 +0000549 }
550 return pNew;
551}
danielk197793758c82005-01-21 08:13:14 +0000552
553/*
554** If cursors, triggers, views and subqueries are all omitted from
555** the build, then none of the following routines, except for
556** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes
557** called with a NULL argument.
558*/
danielk19776a67fe82005-02-04 04:07:16 +0000559#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \
560 || !defined(SQLITE_OMIT_SUBQUERY)
drh17435752007-08-16 04:30:38 +0000561SrcList *sqlite3SrcListDup(sqlite3 *db, SrcList *p){
drhad3cab52002-05-24 02:04:32 +0000562 SrcList *pNew;
563 int i;
drh113088e2003-03-20 01:16:58 +0000564 int nByte;
drhad3cab52002-05-24 02:04:32 +0000565 if( p==0 ) return 0;
drh113088e2003-03-20 01:16:58 +0000566 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
drh17435752007-08-16 04:30:38 +0000567 pNew = sqlite3DbMallocRaw(db, nByte );
drhad3cab52002-05-24 02:04:32 +0000568 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000569 pNew->nSrc = pNew->nAlloc = p->nSrc;
drhad3cab52002-05-24 02:04:32 +0000570 for(i=0; i<p->nSrc; i++){
drh4efc4752004-01-16 15:55:37 +0000571 struct SrcList_item *pNewItem = &pNew->a[i];
572 struct SrcList_item *pOldItem = &p->a[i];
drhed8a3bb2005-06-06 21:19:56 +0000573 Table *pTab;
drh17435752007-08-16 04:30:38 +0000574 pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase);
575 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
576 pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias);
drh4efc4752004-01-16 15:55:37 +0000577 pNewItem->jointype = pOldItem->jointype;
578 pNewItem->iCursor = pOldItem->iCursor;
danielk19771787cca2006-02-10 07:07:14 +0000579 pNewItem->isPopulated = pOldItem->isPopulated;
drhed8a3bb2005-06-06 21:19:56 +0000580 pTab = pNewItem->pTab = pOldItem->pTab;
581 if( pTab ){
582 pTab->nRef++;
danielk1977a1cb1832005-02-12 08:59:55 +0000583 }
drh17435752007-08-16 04:30:38 +0000584 pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect);
585 pNewItem->pOn = sqlite3ExprDup(db, pOldItem->pOn);
586 pNewItem->pUsing = sqlite3IdListDup(db, pOldItem->pUsing);
danielk19776c18b6e2005-01-30 09:17:58 +0000587 pNewItem->colUsed = pOldItem->colUsed;
drhad3cab52002-05-24 02:04:32 +0000588 }
589 return pNew;
590}
drh17435752007-08-16 04:30:38 +0000591IdList *sqlite3IdListDup(sqlite3 *db, IdList *p){
drhff78bd22002-02-27 01:47:11 +0000592 IdList *pNew;
593 int i;
594 if( p==0 ) return 0;
drh17435752007-08-16 04:30:38 +0000595 pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +0000596 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000597 pNew->nId = pNew->nAlloc = p->nId;
drh17435752007-08-16 04:30:38 +0000598 pNew->a = sqlite3DbMallocRaw(db, p->nId*sizeof(p->a[0]) );
danielk1977d5d56522005-03-16 12:15:20 +0000599 if( pNew->a==0 ){
drh17435752007-08-16 04:30:38 +0000600 sqlite3_free(pNew);
danielk1977d5d56522005-03-16 12:15:20 +0000601 return 0;
602 }
drhff78bd22002-02-27 01:47:11 +0000603 for(i=0; i<p->nId; i++){
drh4efc4752004-01-16 15:55:37 +0000604 struct IdList_item *pNewItem = &pNew->a[i];
605 struct IdList_item *pOldItem = &p->a[i];
drh17435752007-08-16 04:30:38 +0000606 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
drh4efc4752004-01-16 15:55:37 +0000607 pNewItem->idx = pOldItem->idx;
drhff78bd22002-02-27 01:47:11 +0000608 }
609 return pNew;
610}
drh17435752007-08-16 04:30:38 +0000611Select *sqlite3SelectDup(sqlite3 *db, Select *p){
drhff78bd22002-02-27 01:47:11 +0000612 Select *pNew;
613 if( p==0 ) return 0;
drh17435752007-08-16 04:30:38 +0000614 pNew = sqlite3DbMallocRaw(db, sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000615 if( pNew==0 ) return 0;
616 pNew->isDistinct = p->isDistinct;
drh17435752007-08-16 04:30:38 +0000617 pNew->pEList = sqlite3ExprListDup(db, p->pEList);
618 pNew->pSrc = sqlite3SrcListDup(db, p->pSrc);
619 pNew->pWhere = sqlite3ExprDup(db, p->pWhere);
620 pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy);
621 pNew->pHaving = sqlite3ExprDup(db, p->pHaving);
622 pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy);
drhff78bd22002-02-27 01:47:11 +0000623 pNew->op = p->op;
drh17435752007-08-16 04:30:38 +0000624 pNew->pPrior = sqlite3SelectDup(db, p->pPrior);
625 pNew->pLimit = sqlite3ExprDup(db, p->pLimit);
626 pNew->pOffset = sqlite3ExprDup(db, p->pOffset);
drh7b58dae2003-07-20 01:16:46 +0000627 pNew->iLimit = -1;
628 pNew->iOffset = -1;
danielk1977a1cb1832005-02-12 08:59:55 +0000629 pNew->isResolved = p->isResolved;
630 pNew->isAgg = p->isAgg;
drhb9bb7c12006-06-11 23:41:55 +0000631 pNew->usesEphm = 0;
drh8e647b82005-09-23 21:11:53 +0000632 pNew->disallowOrderBy = 0;
drh0342b1f2005-09-01 03:07:44 +0000633 pNew->pRightmost = 0;
drhb9bb7c12006-06-11 23:41:55 +0000634 pNew->addrOpenEphm[0] = -1;
635 pNew->addrOpenEphm[1] = -1;
636 pNew->addrOpenEphm[2] = -1;
drhff78bd22002-02-27 01:47:11 +0000637 return pNew;
638}
danielk197793758c82005-01-21 08:13:14 +0000639#else
drh17435752007-08-16 04:30:38 +0000640Select *sqlite3SelectDup(sqlite3 *db, Select *p){
danielk197793758c82005-01-21 08:13:14 +0000641 assert( p==0 );
642 return 0;
643}
644#endif
drhff78bd22002-02-27 01:47:11 +0000645
646
647/*
drha76b5df2002-02-23 02:32:10 +0000648** Add a new element to the end of an expression list. If pList is
649** initially NULL, then create a new expression list.
650*/
drh17435752007-08-16 04:30:38 +0000651ExprList *sqlite3ExprListAppend(
652 Parse *pParse, /* Parsing context */
653 ExprList *pList, /* List to which to append. Might be NULL */
654 Expr *pExpr, /* Expression to be appended */
655 Token *pName /* AS keyword for the expression */
656){
657 sqlite3 *db = pParse->db;
drha76b5df2002-02-23 02:32:10 +0000658 if( pList==0 ){
drh17435752007-08-16 04:30:38 +0000659 pList = sqlite3DbMallocZero(db, sizeof(ExprList) );
drha76b5df2002-02-23 02:32:10 +0000660 if( pList==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000661 goto no_mem;
drha76b5df2002-02-23 02:32:10 +0000662 }
drh4efc4752004-01-16 15:55:37 +0000663 assert( pList->nAlloc==0 );
drha76b5df2002-02-23 02:32:10 +0000664 }
drh4305d102003-07-30 12:34:12 +0000665 if( pList->nAlloc<=pList->nExpr ){
danielk1977d5d56522005-03-16 12:15:20 +0000666 struct ExprList_item *a;
667 int n = pList->nAlloc*2 + 4;
danielk197726783a52007-08-29 14:06:22 +0000668 a = sqlite3DbRealloc(db, pList->a, n*sizeof(pList->a[0]));
danielk1977d5d56522005-03-16 12:15:20 +0000669 if( a==0 ){
670 goto no_mem;
drha76b5df2002-02-23 02:32:10 +0000671 }
danielk1977d5d56522005-03-16 12:15:20 +0000672 pList->a = a;
673 pList->nAlloc = n;
drha76b5df2002-02-23 02:32:10 +0000674 }
drh4efc4752004-01-16 15:55:37 +0000675 assert( pList->a!=0 );
676 if( pExpr || pName ){
677 struct ExprList_item *pItem = &pList->a[pList->nExpr++];
678 memset(pItem, 0, sizeof(*pItem));
drh17435752007-08-16 04:30:38 +0000679 pItem->zName = sqlite3NameFromToken(db, pName);
danielk1977e94ddc92005-03-21 03:53:38 +0000680 pItem->pExpr = pExpr;
drha76b5df2002-02-23 02:32:10 +0000681 }
682 return pList;
danielk1977d5d56522005-03-16 12:15:20 +0000683
684no_mem:
685 /* Avoid leaking memory if malloc has failed. */
686 sqlite3ExprDelete(pExpr);
687 sqlite3ExprListDelete(pList);
688 return 0;
drha76b5df2002-02-23 02:32:10 +0000689}
690
691/*
danielk19777a15a4b2007-05-08 17:54:43 +0000692** If the expression list pEList contains more than iLimit elements,
693** leave an error message in pParse.
694*/
695void sqlite3ExprListCheckLength(
696 Parse *pParse,
697 ExprList *pEList,
698 int iLimit,
699 const char *zObject
700){
danielk1977b4fc6792007-05-08 18:04:46 +0000701 if( pEList && pEList->nExpr>iLimit ){
danielk19777a15a4b2007-05-08 17:54:43 +0000702 sqlite3ErrorMsg(pParse, "too many columns in %s", zObject);
703 }
704}
705
danielk1977fc976062007-05-10 10:46:56 +0000706
danielk1977e6a58a42007-08-31 17:42:48 +0000707#if defined(SQLITE_TEST) || SQLITE_MAX_EXPR_DEPTH>0
danielk1977fc976062007-05-10 10:46:56 +0000708/* The following three functions, heightOfExpr(), heightOfExprList()
709** and heightOfSelect(), are used to determine the maximum height
710** of any expression tree referenced by the structure passed as the
711** first argument.
712**
713** If this maximum height is greater than the current value pointed
714** to by pnHeight, the second parameter, then set *pnHeight to that
715** value.
716*/
717static void heightOfExpr(Expr *p, int *pnHeight){
718 if( p ){
719 if( p->nHeight>*pnHeight ){
720 *pnHeight = p->nHeight;
721 }
722 }
723}
724static void heightOfExprList(ExprList *p, int *pnHeight){
725 if( p ){
726 int i;
727 for(i=0; i<p->nExpr; i++){
728 heightOfExpr(p->a[i].pExpr, pnHeight);
729 }
730 }
731}
732static void heightOfSelect(Select *p, int *pnHeight){
733 if( p ){
734 heightOfExpr(p->pWhere, pnHeight);
735 heightOfExpr(p->pHaving, pnHeight);
736 heightOfExpr(p->pLimit, pnHeight);
737 heightOfExpr(p->pOffset, pnHeight);
738 heightOfExprList(p->pEList, pnHeight);
739 heightOfExprList(p->pGroupBy, pnHeight);
740 heightOfExprList(p->pOrderBy, pnHeight);
741 heightOfSelect(p->pPrior, pnHeight);
742 }
743}
744
745/*
746** Set the Expr.nHeight variable in the structure passed as an
747** argument. An expression with no children, Expr.pList or
748** Expr.pSelect member has a height of 1. Any other expression
749** has a height equal to the maximum height of any other
750** referenced Expr plus one.
751*/
752void sqlite3ExprSetHeight(Expr *p){
753 int nHeight = 0;
754 heightOfExpr(p->pLeft, &nHeight);
755 heightOfExpr(p->pRight, &nHeight);
756 heightOfExprList(p->pList, &nHeight);
757 heightOfSelect(p->pSelect, &nHeight);
758 p->nHeight = nHeight + 1;
759}
760
761/*
762** Return the maximum height of any expression tree referenced
763** by the select statement passed as an argument.
764*/
765int sqlite3SelectExprHeight(Select *p){
766 int nHeight = 0;
767 heightOfSelect(p, &nHeight);
768 return nHeight;
769}
770#endif
771
danielk19777a15a4b2007-05-08 17:54:43 +0000772/*
drha76b5df2002-02-23 02:32:10 +0000773** Delete an entire expression list.
774*/
danielk19774adee202004-05-08 08:23:19 +0000775void sqlite3ExprListDelete(ExprList *pList){
drha76b5df2002-02-23 02:32:10 +0000776 int i;
drhbe5c89a2004-07-26 00:31:09 +0000777 struct ExprList_item *pItem;
drha76b5df2002-02-23 02:32:10 +0000778 if( pList==0 ) return;
drh1bdd9b52004-04-23 17:04:44 +0000779 assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
780 assert( pList->nExpr<=pList->nAlloc );
drhbe5c89a2004-07-26 00:31:09 +0000781 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
782 sqlite3ExprDelete(pItem->pExpr);
drh17435752007-08-16 04:30:38 +0000783 sqlite3_free(pItem->zName);
drha76b5df2002-02-23 02:32:10 +0000784 }
drh17435752007-08-16 04:30:38 +0000785 sqlite3_free(pList->a);
786 sqlite3_free(pList);
drha76b5df2002-02-23 02:32:10 +0000787}
788
789/*
drh626a8792005-01-17 22:08:19 +0000790** Walk an expression tree. Call xFunc for each node visited.
drh73b211a2005-01-18 04:00:42 +0000791**
drh626a8792005-01-17 22:08:19 +0000792** The return value from xFunc determines whether the tree walk continues.
793** 0 means continue walking the tree. 1 means do not walk children
794** of the current node but continue with siblings. 2 means abandon
795** the tree walk completely.
796**
797** The return value from this routine is 1 to abandon the tree walk
798** and 0 to continue.
drh87abf5c2005-08-25 12:45:04 +0000799**
800** NOTICE: This routine does *not* descend into subqueries.
drh626a8792005-01-17 22:08:19 +0000801*/
danielk1977a58fdfb2005-02-08 07:50:40 +0000802static int walkExprList(ExprList *, int (*)(void *, Expr*), void *);
drh626a8792005-01-17 22:08:19 +0000803static int walkExprTree(Expr *pExpr, int (*xFunc)(void*,Expr*), void *pArg){
drh626a8792005-01-17 22:08:19 +0000804 int rc;
805 if( pExpr==0 ) return 0;
806 rc = (*xFunc)(pArg, pExpr);
807 if( rc==0 ){
808 if( walkExprTree(pExpr->pLeft, xFunc, pArg) ) return 1;
809 if( walkExprTree(pExpr->pRight, xFunc, pArg) ) return 1;
danielk1977a58fdfb2005-02-08 07:50:40 +0000810 if( walkExprList(pExpr->pList, xFunc, pArg) ) return 1;
drh626a8792005-01-17 22:08:19 +0000811 }
812 return rc>1;
813}
814
815/*
danielk1977a58fdfb2005-02-08 07:50:40 +0000816** Call walkExprTree() for every expression in list p.
817*/
818static int walkExprList(ExprList *p, int (*xFunc)(void *, Expr*), void *pArg){
819 int i;
820 struct ExprList_item *pItem;
821 if( !p ) return 0;
822 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
823 if( walkExprTree(pItem->pExpr, xFunc, pArg) ) return 1;
824 }
825 return 0;
826}
827
828/*
829** Call walkExprTree() for every expression in Select p, not including
830** expressions that are part of sub-selects in any FROM clause or the LIMIT
831** or OFFSET expressions..
832*/
833static int walkSelectExpr(Select *p, int (*xFunc)(void *, Expr*), void *pArg){
834 walkExprList(p->pEList, xFunc, pArg);
835 walkExprTree(p->pWhere, xFunc, pArg);
836 walkExprList(p->pGroupBy, xFunc, pArg);
837 walkExprTree(p->pHaving, xFunc, pArg);
838 walkExprList(p->pOrderBy, xFunc, pArg);
danielk197715d79822007-05-15 07:00:34 +0000839 if( p->pPrior ){
840 walkSelectExpr(p->pPrior, xFunc, pArg);
841 }
danielk1977a58fdfb2005-02-08 07:50:40 +0000842 return 0;
843}
844
845
846/*
drh626a8792005-01-17 22:08:19 +0000847** This routine is designed as an xFunc for walkExprTree().
848**
849** pArg is really a pointer to an integer. If we can tell by looking
drh73b211a2005-01-18 04:00:42 +0000850** at pExpr that the expression that contains pExpr is not a constant
851** expression, then set *pArg to 0 and return 2 to abandon the tree walk.
852** If pExpr does does not disqualify the expression from being a constant
853** then do nothing.
854**
855** After walking the whole tree, if no nodes are found that disqualify
856** the expression as constant, then we assume the whole expression
857** is constant. See sqlite3ExprIsConstant() for additional information.
drh626a8792005-01-17 22:08:19 +0000858*/
859static int exprNodeIsConstant(void *pArg, Expr *pExpr){
drh0a168372007-06-08 00:20:47 +0000860 int *pN = (int*)pArg;
861
862 /* If *pArg is 3 then any term of the expression that comes from
863 ** the ON or USING clauses of a join disqualifies the expression
864 ** from being considered constant. */
865 if( (*pN)==3 && ExprHasAnyProperty(pExpr, EP_FromJoin) ){
866 *pN = 0;
867 return 2;
868 }
869
drh626a8792005-01-17 22:08:19 +0000870 switch( pExpr->op ){
drheb55bd22005-06-30 17:04:21 +0000871 /* Consider functions to be constant if all their arguments are constant
872 ** and *pArg==2 */
873 case TK_FUNCTION:
drh0a168372007-06-08 00:20:47 +0000874 if( (*pN)==2 ) return 0;
drheb55bd22005-06-30 17:04:21 +0000875 /* Fall through */
drh626a8792005-01-17 22:08:19 +0000876 case TK_ID:
877 case TK_COLUMN:
878 case TK_DOT:
879 case TK_AGG_FUNCTION:
drh13449892005-09-07 21:22:45 +0000880 case TK_AGG_COLUMN:
drhfe2093d2005-01-20 22:48:47 +0000881#ifndef SQLITE_OMIT_SUBQUERY
882 case TK_SELECT:
883 case TK_EXISTS:
884#endif
drh0a168372007-06-08 00:20:47 +0000885 *pN = 0;
drh626a8792005-01-17 22:08:19 +0000886 return 2;
drh87abf5c2005-08-25 12:45:04 +0000887 case TK_IN:
888 if( pExpr->pSelect ){
drh0a168372007-06-08 00:20:47 +0000889 *pN = 0;
drh87abf5c2005-08-25 12:45:04 +0000890 return 2;
891 }
drh626a8792005-01-17 22:08:19 +0000892 default:
893 return 0;
894 }
895}
896
897/*
drhfef52082000-06-06 01:50:43 +0000898** Walk an expression tree. Return 1 if the expression is constant
drheb55bd22005-06-30 17:04:21 +0000899** and 0 if it involves variables or function calls.
drh23989372002-05-21 13:43:04 +0000900**
901** For the purposes of this function, a double-quoted string (ex: "abc")
902** is considered a variable but a single-quoted string (ex: 'abc') is
903** a constant.
drhfef52082000-06-06 01:50:43 +0000904*/
danielk19774adee202004-05-08 08:23:19 +0000905int sqlite3ExprIsConstant(Expr *p){
drh626a8792005-01-17 22:08:19 +0000906 int isConst = 1;
907 walkExprTree(p, exprNodeIsConstant, &isConst);
908 return isConst;
drhfef52082000-06-06 01:50:43 +0000909}
910
911/*
drheb55bd22005-06-30 17:04:21 +0000912** Walk an expression tree. Return 1 if the expression is constant
drh0a168372007-06-08 00:20:47 +0000913** that does no originate from the ON or USING clauses of a join.
914** Return 0 if it involves variables or function calls or terms from
915** an ON or USING clause.
916*/
917int sqlite3ExprIsConstantNotJoin(Expr *p){
918 int isConst = 3;
919 walkExprTree(p, exprNodeIsConstant, &isConst);
920 return isConst!=0;
921}
922
923/*
924** Walk an expression tree. Return 1 if the expression is constant
drheb55bd22005-06-30 17:04:21 +0000925** or a function call with constant arguments. Return and 0 if there
926** are any variables.
927**
928** For the purposes of this function, a double-quoted string (ex: "abc")
929** is considered a variable but a single-quoted string (ex: 'abc') is
930** a constant.
931*/
932int sqlite3ExprIsConstantOrFunction(Expr *p){
933 int isConst = 2;
934 walkExprTree(p, exprNodeIsConstant, &isConst);
935 return isConst!=0;
936}
937
938/*
drh73b211a2005-01-18 04:00:42 +0000939** If the expression p codes a constant integer that is small enough
drh202b2df2004-01-06 01:13:46 +0000940** to fit in a 32-bit integer, return 1 and put the value of the integer
941** in *pValue. If the expression is not an integer or if it is too big
942** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
drhe4de1fe2002-06-02 16:09:01 +0000943*/
danielk19774adee202004-05-08 08:23:19 +0000944int sqlite3ExprIsInteger(Expr *p, int *pValue){
drhe4de1fe2002-06-02 16:09:01 +0000945 switch( p->op ){
946 case TK_INTEGER: {
drh2646da72005-12-09 20:02:05 +0000947 if( sqlite3GetInt32((char*)p->token.z, pValue) ){
drh202b2df2004-01-06 01:13:46 +0000948 return 1;
949 }
950 break;
drhe4de1fe2002-06-02 16:09:01 +0000951 }
drh4b59ab52002-08-24 18:24:51 +0000952 case TK_UPLUS: {
danielk19774adee202004-05-08 08:23:19 +0000953 return sqlite3ExprIsInteger(p->pLeft, pValue);
drh4b59ab52002-08-24 18:24:51 +0000954 }
drhe4de1fe2002-06-02 16:09:01 +0000955 case TK_UMINUS: {
956 int v;
danielk19774adee202004-05-08 08:23:19 +0000957 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
drhe4de1fe2002-06-02 16:09:01 +0000958 *pValue = -v;
959 return 1;
960 }
961 break;
962 }
963 default: break;
964 }
965 return 0;
966}
967
968/*
drhc4a3c772001-04-04 11:48:57 +0000969** Return TRUE if the given string is a row-id column name.
970*/
danielk19774adee202004-05-08 08:23:19 +0000971int sqlite3IsRowid(const char *z){
972 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
973 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
974 if( sqlite3StrICmp(z, "OID")==0 ) return 1;
drhc4a3c772001-04-04 11:48:57 +0000975 return 0;
976}
977
978/*
drh8141f612004-01-25 22:44:58 +0000979** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
980** that name in the set of source tables in pSrcList and make the pExpr
981** expression node refer back to that source column. The following changes
982** are made to pExpr:
983**
984** pExpr->iDb Set the index in db->aDb[] of the database holding
985** the table.
986** pExpr->iTable Set to the cursor number for the table obtained
987** from pSrcList.
988** pExpr->iColumn Set to the column number within the table.
drh8141f612004-01-25 22:44:58 +0000989** pExpr->op Set to TK_COLUMN.
990** pExpr->pLeft Any expression this points to is deleted
991** pExpr->pRight Any expression this points to is deleted.
992**
993** The pDbToken is the name of the database (the "X"). This value may be
994** NULL meaning that name is of the form Y.Z or Z. Any available database
995** can be used. The pTableToken is the name of the table (the "Y"). This
996** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it
997** means that the form of the name is Z and that columns from any table
998** can be used.
999**
1000** If the name cannot be resolved unambiguously, leave an error message
1001** in pParse and return non-zero. Return zero on success.
1002*/
1003static int lookupName(
drhffe07b22005-11-03 00:41:17 +00001004 Parse *pParse, /* The parsing context */
drh8141f612004-01-25 22:44:58 +00001005 Token *pDbToken, /* Name of the database containing table, or NULL */
1006 Token *pTableToken, /* Name of table containing column, or NULL */
1007 Token *pColumnToken, /* Name of the column. */
drh626a8792005-01-17 22:08:19 +00001008 NameContext *pNC, /* The name context used to resolve the name */
drh8141f612004-01-25 22:44:58 +00001009 Expr *pExpr /* Make this EXPR node point to the selected column */
1010){
1011 char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */
1012 char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */
1013 char *zCol = 0; /* Name of the column. The "Z" */
1014 int i, j; /* Loop counters */
1015 int cnt = 0; /* Number of matching column names */
1016 int cntTab = 0; /* Number of matching table names */
drh626a8792005-01-17 22:08:19 +00001017 sqlite3 *db = pParse->db; /* The database */
drh51669862004-12-18 18:40:26 +00001018 struct SrcList_item *pItem; /* Use for looping over pSrcList items */
1019 struct SrcList_item *pMatch = 0; /* The matching pSrcList item */
drh73b211a2005-01-18 04:00:42 +00001020 NameContext *pTopNC = pNC; /* First namecontext in the list */
drh728b5772007-09-18 15:55:07 +00001021 Schema *pSchema = 0; /* Schema of the expression */
drh8141f612004-01-25 22:44:58 +00001022
1023 assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
drh17435752007-08-16 04:30:38 +00001024 zDb = sqlite3NameFromToken(db, pDbToken);
1025 zTab = sqlite3NameFromToken(db, pTableToken);
1026 zCol = sqlite3NameFromToken(db, pColumnToken);
1027 if( db->mallocFailed ){
danielk1977d5d56522005-03-16 12:15:20 +00001028 goto lookupname_end;
drh8141f612004-01-25 22:44:58 +00001029 }
drh8141f612004-01-25 22:44:58 +00001030
1031 pExpr->iTable = -1;
drh626a8792005-01-17 22:08:19 +00001032 while( pNC && cnt==0 ){
drhffe07b22005-11-03 00:41:17 +00001033 ExprList *pEList;
drh626a8792005-01-17 22:08:19 +00001034 SrcList *pSrcList = pNC->pSrcList;
drh8141f612004-01-25 22:44:58 +00001035
danielk1977b3bce662005-01-29 08:32:43 +00001036 if( pSrcList ){
1037 for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
drh43617e92006-03-06 20:55:46 +00001038 Table *pTab;
1039 int iDb;
danielk1977b3bce662005-01-29 08:32:43 +00001040 Column *pCol;
1041
drh43617e92006-03-06 20:55:46 +00001042 pTab = pItem->pTab;
1043 assert( pTab!=0 );
1044 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
danielk1977b3bce662005-01-29 08:32:43 +00001045 assert( pTab->nCol>0 );
1046 if( zTab ){
1047 if( pItem->zAlias ){
1048 char *zTabName = pItem->zAlias;
1049 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
1050 }else{
1051 char *zTabName = pTab->zName;
1052 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
danielk1977da184232006-01-05 11:34:32 +00001053 if( zDb!=0 && sqlite3StrICmp(db->aDb[iDb].zName, zDb)!=0 ){
danielk1977b3bce662005-01-29 08:32:43 +00001054 continue;
1055 }
drh626a8792005-01-17 22:08:19 +00001056 }
drh8141f612004-01-25 22:44:58 +00001057 }
danielk1977b3bce662005-01-29 08:32:43 +00001058 if( 0==(cntTab++) ){
drh626a8792005-01-17 22:08:19 +00001059 pExpr->iTable = pItem->iCursor;
drh728b5772007-09-18 15:55:07 +00001060 pSchema = pTab->pSchema;
danielk1977b3bce662005-01-29 08:32:43 +00001061 pMatch = pItem;
1062 }
1063 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
1064 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
danielk1977b3bf5562006-01-10 17:58:23 +00001065 const char *zColl = pTab->aCol[j].zColl;
drh873fac02005-06-06 17:11:46 +00001066 IdList *pUsing;
danielk1977b3bce662005-01-29 08:32:43 +00001067 cnt++;
1068 pExpr->iTable = pItem->iCursor;
1069 pMatch = pItem;
drh728b5772007-09-18 15:55:07 +00001070 pSchema = pTab->pSchema;
danielk1977b3bce662005-01-29 08:32:43 +00001071 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
1072 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
1073 pExpr->affinity = pTab->aCol[j].affinity;
drh8b4c40d2007-02-01 23:02:45 +00001074 if( (pExpr->flags & EP_ExpCollate)==0 ){
1075 pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0);
1076 }
drh61dfc312006-12-16 16:25:15 +00001077 if( i<pSrcList->nSrc-1 ){
1078 if( pItem[1].jointype & JT_NATURAL ){
1079 /* If this match occurred in the left table of a natural join,
1080 ** then skip the right table to avoid a duplicate match */
1081 pItem++;
1082 i++;
1083 }else if( (pUsing = pItem[1].pUsing)!=0 ){
1084 /* If this match occurs on a column that is in the USING clause
1085 ** of a join, skip the search of the right table of the join
1086 ** to avoid a duplicate match there. */
1087 int k;
1088 for(k=0; k<pUsing->nId; k++){
1089 if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ){
1090 pItem++;
1091 i++;
1092 break;
1093 }
drh873fac02005-06-06 17:11:46 +00001094 }
1095 }
1096 }
danielk1977b3bce662005-01-29 08:32:43 +00001097 break;
1098 }
drh8141f612004-01-25 22:44:58 +00001099 }
1100 }
1101 }
drh626a8792005-01-17 22:08:19 +00001102
1103#ifndef SQLITE_OMIT_TRIGGER
1104 /* If we have not already resolved the name, then maybe
1105 ** it is a new.* or old.* trigger argument reference
1106 */
1107 if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
1108 TriggerStack *pTriggerStack = pParse->trigStack;
1109 Table *pTab = 0;
1110 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){
1111 pExpr->iTable = pTriggerStack->newIdx;
1112 assert( pTriggerStack->pTab );
1113 pTab = pTriggerStack->pTab;
1114 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab)==0 ){
1115 pExpr->iTable = pTriggerStack->oldIdx;
1116 assert( pTriggerStack->pTab );
1117 pTab = pTriggerStack->pTab;
1118 }
1119
1120 if( pTab ){
danielk1977f0113002006-01-24 12:09:17 +00001121 int iCol;
drh626a8792005-01-17 22:08:19 +00001122 Column *pCol = pTab->aCol;
1123
drh728b5772007-09-18 15:55:07 +00001124 pSchema = pTab->pSchema;
drh626a8792005-01-17 22:08:19 +00001125 cntTab++;
danielk1977f0113002006-01-24 12:09:17 +00001126 for(iCol=0; iCol < pTab->nCol; iCol++, pCol++) {
drh626a8792005-01-17 22:08:19 +00001127 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
danielk1977f0113002006-01-24 12:09:17 +00001128 const char *zColl = pTab->aCol[iCol].zColl;
drh626a8792005-01-17 22:08:19 +00001129 cnt++;
danielk1977f0113002006-01-24 12:09:17 +00001130 pExpr->iColumn = iCol==pTab->iPKey ? -1 : iCol;
1131 pExpr->affinity = pTab->aCol[iCol].affinity;
drh8b4c40d2007-02-01 23:02:45 +00001132 if( (pExpr->flags & EP_ExpCollate)==0 ){
1133 pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0);
1134 }
danielk1977aee18ef2005-03-09 12:26:50 +00001135 pExpr->pTab = pTab;
drh626a8792005-01-17 22:08:19 +00001136 break;
1137 }
1138 }
1139 }
1140 }
drhb7f91642004-10-31 02:22:47 +00001141#endif /* !defined(SQLITE_OMIT_TRIGGER) */
drh8141f612004-01-25 22:44:58 +00001142
drh626a8792005-01-17 22:08:19 +00001143 /*
1144 ** Perhaps the name is a reference to the ROWID
1145 */
1146 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
1147 cnt = 1;
1148 pExpr->iColumn = -1;
drh8a512562005-11-14 22:29:05 +00001149 pExpr->affinity = SQLITE_AFF_INTEGER;
drh626a8792005-01-17 22:08:19 +00001150 }
drh8141f612004-01-25 22:44:58 +00001151
drh626a8792005-01-17 22:08:19 +00001152 /*
1153 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
1154 ** might refer to an result-set alias. This happens, for example, when
1155 ** we are resolving names in the WHERE clause of the following command:
1156 **
1157 ** SELECT a+b AS x FROM table WHERE x<10;
1158 **
1159 ** In cases like this, replace pExpr with a copy of the expression that
1160 ** forms the result set entry ("a+b" in the example) and return immediately.
1161 ** Note that the expression in the result set should have already been
1162 ** resolved by the time the WHERE clause is resolved.
1163 */
drhffe07b22005-11-03 00:41:17 +00001164 if( cnt==0 && (pEList = pNC->pEList)!=0 && zTab==0 ){
drh626a8792005-01-17 22:08:19 +00001165 for(j=0; j<pEList->nExpr; j++){
1166 char *zAs = pEList->a[j].zName;
1167 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
drh36379e92007-07-23 22:51:15 +00001168 Expr *pDup, *pOrig;
drh626a8792005-01-17 22:08:19 +00001169 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
drh4f07e5f2007-05-14 11:34:46 +00001170 assert( pExpr->pList==0 );
1171 assert( pExpr->pSelect==0 );
drh36379e92007-07-23 22:51:15 +00001172 pOrig = pEList->a[j].pExpr;
1173 if( !pNC->allowAgg && ExprHasProperty(pOrig, EP_Agg) ){
1174 sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs);
drh17435752007-08-16 04:30:38 +00001175 sqlite3_free(zCol);
drh36379e92007-07-23 22:51:15 +00001176 return 2;
1177 }
danielk19771e536952007-08-16 10:09:01 +00001178 pDup = sqlite3ExprDup(db, pOrig);
drh4f07e5f2007-05-14 11:34:46 +00001179 if( pExpr->flags & EP_ExpCollate ){
1180 pDup->pColl = pExpr->pColl;
1181 pDup->flags |= EP_ExpCollate;
1182 }
drh17435752007-08-16 04:30:38 +00001183 if( pExpr->span.dyn ) sqlite3_free((char*)pExpr->span.z);
1184 if( pExpr->token.dyn ) sqlite3_free((char*)pExpr->token.z);
drh4f07e5f2007-05-14 11:34:46 +00001185 memcpy(pExpr, pDup, sizeof(*pExpr));
drh17435752007-08-16 04:30:38 +00001186 sqlite3_free(pDup);
drh15ccce12005-05-23 15:06:39 +00001187 cnt = 1;
danielk1977c9cf6e32007-06-25 16:29:33 +00001188 pMatch = 0;
drh626a8792005-01-17 22:08:19 +00001189 assert( zTab==0 && zDb==0 );
drh15ccce12005-05-23 15:06:39 +00001190 goto lookupname_end_2;
drh626a8792005-01-17 22:08:19 +00001191 }
1192 }
1193 }
1194
1195 /* Advance to the next name context. The loop will exit when either
1196 ** we have a match (cnt>0) or when we run out of name contexts.
1197 */
1198 if( cnt==0 ){
1199 pNC = pNC->pNext;
1200 }
drh8141f612004-01-25 22:44:58 +00001201 }
1202
1203 /*
1204 ** If X and Y are NULL (in other words if only the column name Z is
1205 ** supplied) and the value of Z is enclosed in double-quotes, then
1206 ** Z is a string literal if it doesn't match any column names. In that
1207 ** case, we need to return right away and not make any changes to
1208 ** pExpr.
drh15ccce12005-05-23 15:06:39 +00001209 **
1210 ** Because no reference was made to outer contexts, the pNC->nRef
1211 ** fields are not changed in any context.
drh8141f612004-01-25 22:44:58 +00001212 */
1213 if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
drh17435752007-08-16 04:30:38 +00001214 sqlite3_free(zCol);
drh8141f612004-01-25 22:44:58 +00001215 return 0;
1216 }
1217
1218 /*
1219 ** cnt==0 means there was not match. cnt>1 means there were two or
1220 ** more matches. Either way, we have an error.
1221 */
1222 if( cnt!=1 ){
1223 char *z = 0;
1224 char *zErr;
1225 zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s";
1226 if( zDb ){
drhf93339d2006-01-03 15:16:26 +00001227 sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, (char*)0);
drh8141f612004-01-25 22:44:58 +00001228 }else if( zTab ){
drhf93339d2006-01-03 15:16:26 +00001229 sqlite3SetString(&z, zTab, ".", zCol, (char*)0);
drh8141f612004-01-25 22:44:58 +00001230 }else{
drh17435752007-08-16 04:30:38 +00001231 z = sqlite3StrDup(zCol);
drh8141f612004-01-25 22:44:58 +00001232 }
danielk1977a1644fd2007-08-29 12:31:25 +00001233 if( z ){
1234 sqlite3ErrorMsg(pParse, zErr, z);
1235 sqlite3_free(z);
1236 pTopNC->nErr++;
1237 }else{
1238 db->mallocFailed = 1;
1239 }
drh8141f612004-01-25 22:44:58 +00001240 }
1241
drh51669862004-12-18 18:40:26 +00001242 /* If a column from a table in pSrcList is referenced, then record
1243 ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes
1244 ** bit 0 to be set. Column 1 sets bit 1. And so forth. If the
1245 ** column number is greater than the number of bits in the bitmask
1246 ** then set the high-order bit of the bitmask.
1247 */
1248 if( pExpr->iColumn>=0 && pMatch!=0 ){
1249 int n = pExpr->iColumn;
1250 if( n>=sizeof(Bitmask)*8 ){
1251 n = sizeof(Bitmask)*8-1;
1252 }
1253 assert( pMatch->iCursor==pExpr->iTable );
drhca83ac52007-02-01 01:40:44 +00001254 pMatch->colUsed |= ((Bitmask)1)<<n;
drh51669862004-12-18 18:40:26 +00001255 }
1256
danielk1977d5d56522005-03-16 12:15:20 +00001257lookupname_end:
drh8141f612004-01-25 22:44:58 +00001258 /* Clean up and return
1259 */
drh17435752007-08-16 04:30:38 +00001260 sqlite3_free(zDb);
1261 sqlite3_free(zTab);
danielk19774adee202004-05-08 08:23:19 +00001262 sqlite3ExprDelete(pExpr->pLeft);
drh8141f612004-01-25 22:44:58 +00001263 pExpr->pLeft = 0;
danielk19774adee202004-05-08 08:23:19 +00001264 sqlite3ExprDelete(pExpr->pRight);
drh8141f612004-01-25 22:44:58 +00001265 pExpr->pRight = 0;
1266 pExpr->op = TK_COLUMN;
drh15ccce12005-05-23 15:06:39 +00001267lookupname_end_2:
drh17435752007-08-16 04:30:38 +00001268 sqlite3_free(zCol);
drh626a8792005-01-17 22:08:19 +00001269 if( cnt==1 ){
danielk1977b3bce662005-01-29 08:32:43 +00001270 assert( pNC!=0 );
drh728b5772007-09-18 15:55:07 +00001271 sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList);
danielk1977aee18ef2005-03-09 12:26:50 +00001272 if( pMatch && !pMatch->pSelect ){
1273 pExpr->pTab = pMatch->pTab;
1274 }
drh15ccce12005-05-23 15:06:39 +00001275 /* Increment the nRef value on all name contexts from TopNC up to
1276 ** the point where the name matched. */
1277 for(;;){
1278 assert( pTopNC!=0 );
1279 pTopNC->nRef++;
1280 if( pTopNC==pNC ) break;
1281 pTopNC = pTopNC->pNext;
1282 }
1283 return 0;
1284 } else {
1285 return 1;
drh626a8792005-01-17 22:08:19 +00001286 }
drh8141f612004-01-25 22:44:58 +00001287}
1288
1289/*
drh626a8792005-01-17 22:08:19 +00001290** This routine is designed as an xFunc for walkExprTree().
1291**
drh73b211a2005-01-18 04:00:42 +00001292** Resolve symbolic names into TK_COLUMN operators for the current
drh626a8792005-01-17 22:08:19 +00001293** node in the expression tree. Return 0 to continue the search down
drh73b211a2005-01-18 04:00:42 +00001294** the tree or 2 to abort the tree walk.
1295**
1296** This routine also does error checking and name resolution for
1297** function names. The operator for aggregate functions is changed
1298** to TK_AGG_FUNCTION.
drh626a8792005-01-17 22:08:19 +00001299*/
1300static int nameResolverStep(void *pArg, Expr *pExpr){
1301 NameContext *pNC = (NameContext*)pArg;
drh626a8792005-01-17 22:08:19 +00001302 Parse *pParse;
drh626a8792005-01-17 22:08:19 +00001303
danielk1977b3bce662005-01-29 08:32:43 +00001304 if( pExpr==0 ) return 1;
drh626a8792005-01-17 22:08:19 +00001305 assert( pNC!=0 );
drh626a8792005-01-17 22:08:19 +00001306 pParse = pNC->pParse;
danielk1977b3bce662005-01-29 08:32:43 +00001307
drh626a8792005-01-17 22:08:19 +00001308 if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return 1;
1309 ExprSetProperty(pExpr, EP_Resolved);
1310#ifndef NDEBUG
danielk1977f0113002006-01-24 12:09:17 +00001311 if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
1312 SrcList *pSrcList = pNC->pSrcList;
danielk1977940fac92005-01-23 22:41:37 +00001313 int i;
danielk1977f0113002006-01-24 12:09:17 +00001314 for(i=0; i<pNC->pSrcList->nSrc; i++){
drh626a8792005-01-17 22:08:19 +00001315 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
1316 }
1317 }
1318#endif
1319 switch( pExpr->op ){
1320 /* Double-quoted strings (ex: "abc") are used as identifiers if
1321 ** possible. Otherwise they remain as strings. Single-quoted
1322 ** strings (ex: 'abc') are always string literals.
1323 */
1324 case TK_STRING: {
1325 if( pExpr->token.z[0]=='\'' ) break;
1326 /* Fall thru into the TK_ID case if this is a double-quoted string */
1327 }
1328 /* A lone identifier is the name of a column.
1329 */
1330 case TK_ID: {
drh626a8792005-01-17 22:08:19 +00001331 lookupName(pParse, 0, 0, &pExpr->token, pNC, pExpr);
1332 return 1;
1333 }
1334
1335 /* A table name and column name: ID.ID
1336 ** Or a database, table and column: ID.ID.ID
1337 */
1338 case TK_DOT: {
1339 Token *pColumn;
1340 Token *pTable;
1341 Token *pDb;
1342 Expr *pRight;
1343
danielk1977b3bce662005-01-29 08:32:43 +00001344 /* if( pSrcList==0 ) break; */
drh626a8792005-01-17 22:08:19 +00001345 pRight = pExpr->pRight;
1346 if( pRight->op==TK_ID ){
1347 pDb = 0;
1348 pTable = &pExpr->pLeft->token;
1349 pColumn = &pRight->token;
1350 }else{
1351 assert( pRight->op==TK_DOT );
1352 pDb = &pExpr->pLeft->token;
1353 pTable = &pRight->pLeft->token;
1354 pColumn = &pRight->pRight->token;
1355 }
1356 lookupName(pParse, pDb, pTable, pColumn, pNC, pExpr);
1357 return 1;
1358 }
1359
1360 /* Resolve function names
1361 */
drhb71090f2005-05-23 17:26:51 +00001362 case TK_CONST_FUNC:
drh626a8792005-01-17 22:08:19 +00001363 case TK_FUNCTION: {
1364 ExprList *pList = pExpr->pList; /* The argument list */
1365 int n = pList ? pList->nExpr : 0; /* Number of arguments */
1366 int no_such_func = 0; /* True if no such function exists */
1367 int wrong_num_args = 0; /* True if wrong number of arguments */
1368 int is_agg = 0; /* True if is an aggregate function */
1369 int i;
drh5169bbc2006-08-24 14:59:45 +00001370 int auth; /* Authorization to use the function */
drh626a8792005-01-17 22:08:19 +00001371 int nId; /* Number of characters in function name */
1372 const char *zId; /* The function name. */
drh73b211a2005-01-18 04:00:42 +00001373 FuncDef *pDef; /* Information about the function */
danielk197714db2662006-01-09 16:12:04 +00001374 int enc = ENC(pParse->db); /* The database encoding */
drh626a8792005-01-17 22:08:19 +00001375
drh2646da72005-12-09 20:02:05 +00001376 zId = (char*)pExpr->token.z;
drhb71090f2005-05-23 17:26:51 +00001377 nId = pExpr->token.n;
drh626a8792005-01-17 22:08:19 +00001378 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
1379 if( pDef==0 ){
1380 pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
1381 if( pDef==0 ){
1382 no_such_func = 1;
1383 }else{
1384 wrong_num_args = 1;
1385 }
1386 }else{
1387 is_agg = pDef->xFunc==0;
1388 }
drh2fca7fe2006-11-23 11:59:13 +00001389#ifndef SQLITE_OMIT_AUTHORIZATION
drh5169bbc2006-08-24 14:59:45 +00001390 if( pDef ){
1391 auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0);
1392 if( auth!=SQLITE_OK ){
1393 if( auth==SQLITE_DENY ){
1394 sqlite3ErrorMsg(pParse, "not authorized to use function: %s",
1395 pDef->zName);
1396 pNC->nErr++;
1397 }
1398 pExpr->op = TK_NULL;
1399 return 1;
1400 }
1401 }
drhb8b14212006-08-24 15:18:25 +00001402#endif
drh626a8792005-01-17 22:08:19 +00001403 if( is_agg && !pNC->allowAgg ){
1404 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
1405 pNC->nErr++;
1406 is_agg = 0;
1407 }else if( no_such_func ){
1408 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
1409 pNC->nErr++;
1410 }else if( wrong_num_args ){
1411 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
1412 nId, zId);
1413 pNC->nErr++;
1414 }
1415 if( is_agg ){
1416 pExpr->op = TK_AGG_FUNCTION;
1417 pNC->hasAgg = 1;
1418 }
drh73b211a2005-01-18 04:00:42 +00001419 if( is_agg ) pNC->allowAgg = 0;
drh626a8792005-01-17 22:08:19 +00001420 for(i=0; pNC->nErr==0 && i<n; i++){
drh73b211a2005-01-18 04:00:42 +00001421 walkExprTree(pList->a[i].pExpr, nameResolverStep, pNC);
drh626a8792005-01-17 22:08:19 +00001422 }
drh73b211a2005-01-18 04:00:42 +00001423 if( is_agg ) pNC->allowAgg = 1;
drh626a8792005-01-17 22:08:19 +00001424 /* FIX ME: Compute pExpr->affinity based on the expected return
1425 ** type of the function
1426 */
1427 return is_agg;
1428 }
danielk1977b3bce662005-01-29 08:32:43 +00001429#ifndef SQLITE_OMIT_SUBQUERY
1430 case TK_SELECT:
1431 case TK_EXISTS:
1432#endif
1433 case TK_IN: {
1434 if( pExpr->pSelect ){
drh8a9f38f2005-11-05 15:07:55 +00001435 int nRef = pNC->nRef;
drh06f65412005-11-03 02:03:13 +00001436#ifndef SQLITE_OMIT_CHECK
1437 if( pNC->isCheck ){
1438 sqlite3ErrorMsg(pParse,"subqueries prohibited in CHECK constraints");
1439 }
1440#endif
danielk1977b3bce662005-01-29 08:32:43 +00001441 sqlite3SelectResolve(pParse, pExpr->pSelect, pNC);
1442 assert( pNC->nRef>=nRef );
1443 if( nRef!=pNC->nRef ){
1444 ExprSetProperty(pExpr, EP_VarSelect);
1445 }
1446 }
drh4284fb02005-11-03 12:33:28 +00001447 break;
danielk1977b3bce662005-01-29 08:32:43 +00001448 }
drh4284fb02005-11-03 12:33:28 +00001449#ifndef SQLITE_OMIT_CHECK
1450 case TK_VARIABLE: {
1451 if( pNC->isCheck ){
1452 sqlite3ErrorMsg(pParse,"parameters prohibited in CHECK constraints");
1453 }
1454 break;
1455 }
1456#endif
drh626a8792005-01-17 22:08:19 +00001457 }
1458 return 0;
1459}
1460
1461/*
drhcce7d172000-05-31 15:34:51 +00001462** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +00001463** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +00001464** index to the table in the table list and a column offset. The
1465** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
1466** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +00001467** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +00001468** VDBE cursor number for a cursor that is pointing into the referenced
1469** table. The Expr.iColumn value is changed to the index of the column
1470** of the referenced table. The Expr.iColumn value for the special
1471** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
1472** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +00001473**
drh626a8792005-01-17 22:08:19 +00001474** Also resolve function names and check the functions for proper
1475** usage. Make sure all function names are recognized and all functions
1476** have the correct number of arguments. Leave an error message
1477** in pParse->zErrMsg if anything is amiss. Return the number of errors.
1478**
drh73b211a2005-01-18 04:00:42 +00001479** If the expression contains aggregate functions then set the EP_Agg
1480** property on the expression.
drh626a8792005-01-17 22:08:19 +00001481*/
danielk1977fc976062007-05-10 10:46:56 +00001482int sqlite3ExprResolveNames(
danielk1977b3bce662005-01-29 08:32:43 +00001483 NameContext *pNC, /* Namespace to resolve expressions in. */
1484 Expr *pExpr /* The expression to be analyzed. */
drh626a8792005-01-17 22:08:19 +00001485){
drh13449892005-09-07 21:22:45 +00001486 int savedHasAgg;
drh73b211a2005-01-18 04:00:42 +00001487 if( pExpr==0 ) return 0;
danielk1977e6a58a42007-08-31 17:42:48 +00001488#if defined(SQLITE_TEST) || SQLITE_MAX_EXPR_DEPTH>0
danielk1977fc976062007-05-10 10:46:56 +00001489 if( (pExpr->nHeight+pNC->pParse->nHeight)>SQLITE_MAX_EXPR_DEPTH ){
1490 sqlite3ErrorMsg(pNC->pParse,
1491 "Expression tree is too large (maximum depth %d)",
1492 SQLITE_MAX_EXPR_DEPTH
1493 );
1494 return 1;
1495 }
1496 pNC->pParse->nHeight += pExpr->nHeight;
1497#endif
drh13449892005-09-07 21:22:45 +00001498 savedHasAgg = pNC->hasAgg;
1499 pNC->hasAgg = 0;
danielk1977b3bce662005-01-29 08:32:43 +00001500 walkExprTree(pExpr, nameResolverStep, pNC);
danielk1977e6a58a42007-08-31 17:42:48 +00001501#if defined(SQLITE_TEST) || SQLITE_MAX_EXPR_DEPTH>0
danielk1977fc976062007-05-10 10:46:56 +00001502 pNC->pParse->nHeight -= pExpr->nHeight;
1503#endif
danielk1977b3bce662005-01-29 08:32:43 +00001504 if( pNC->nErr>0 ){
drh73b211a2005-01-18 04:00:42 +00001505 ExprSetProperty(pExpr, EP_Error);
drh73b211a2005-01-18 04:00:42 +00001506 }
drh13449892005-09-07 21:22:45 +00001507 if( pNC->hasAgg ){
1508 ExprSetProperty(pExpr, EP_Agg);
1509 }else if( savedHasAgg ){
1510 pNC->hasAgg = 1;
1511 }
drh73b211a2005-01-18 04:00:42 +00001512 return ExprHasProperty(pExpr, EP_Error);
drh626a8792005-01-17 22:08:19 +00001513}
1514
drh1398ad32005-01-19 23:24:50 +00001515/*
1516** A pointer instance of this structure is used to pass information
1517** through walkExprTree into codeSubqueryStep().
1518*/
1519typedef struct QueryCoder QueryCoder;
1520struct QueryCoder {
1521 Parse *pParse; /* The parsing context */
1522 NameContext *pNC; /* Namespace of first enclosing query */
1523};
1524
danielk19779a96b662007-11-29 17:05:18 +00001525#ifdef SQLITE_TEST
1526 int sqlite3_enable_in_opt = 1;
1527#else
1528 #define sqlite3_enable_in_opt 1
1529#endif
1530
1531/*
1532** This function is used by the implementation of the IN (...) operator.
1533** It's job is to find or create a b-tree structure that may be used
1534** either to test for membership of the (...) set or to iterate through
drh85b623f2007-12-13 21:54:09 +00001535** its members, skipping duplicates.
danielk19779a96b662007-11-29 17:05:18 +00001536**
1537** The cursor opened on the structure (database table, database index
1538** or ephermal table) is stored in pX->iTable before this function returns.
1539** The returned value indicates the structure type, as follows:
1540**
1541** IN_INDEX_ROWID - The cursor was opened on a database table.
1542** IN_INDEX_INDEX - The cursor was opened on a database indec.
1543** IN_INDEX_EPH - The cursor was opened on a specially created and
1544** populated epheremal table.
1545**
1546** An existing structure may only be used if the SELECT is of the simple
1547** form:
1548**
1549** SELECT <column> FROM <table>
1550**
1551** If the mustBeUnique parameter is false, the structure will be used
1552** for fast set membership tests. In this case an epheremal table must
1553** be used unless <column> is an INTEGER PRIMARY KEY or an index can
drh85b623f2007-12-13 21:54:09 +00001554** be found with <column> as its left-most column.
danielk19779a96b662007-11-29 17:05:18 +00001555**
1556** If mustBeUnique is true, then the structure will be used to iterate
1557** through the set members, skipping any duplicates. In this case an
1558** epheremal table must be used unless the selected <column> is guaranteed
1559** to be unique - either because it is an INTEGER PRIMARY KEY or it
1560** is unique by virtue of a constraint or implicit index.
1561*/
danielk1977284f4ac2007-12-10 05:03:46 +00001562#ifndef SQLITE_OMIT_SUBQUERY
danielk19779a96b662007-11-29 17:05:18 +00001563int sqlite3FindInIndex(Parse *pParse, Expr *pX, int mustBeUnique){
1564 Select *p;
1565 int eType = 0;
1566 int iTab = pParse->nTab++;
1567
1568 /* The follwing if(...) expression is true if the SELECT is of the
1569 ** simple form:
1570 **
1571 ** SELECT <column> FROM <table>
1572 **
1573 ** If this is the case, it may be possible to use an existing table
1574 ** or index instead of generating an epheremal table.
1575 */
1576 if( sqlite3_enable_in_opt
1577 && (p=pX->pSelect) && !p->pPrior
1578 && !p->isDistinct && !p->isAgg && !p->pGroupBy
1579 && p->pSrc && p->pSrc->nSrc==1 && !p->pSrc->a[0].pSelect
1580 && !p->pSrc->a[0].pTab->pSelect
1581 && p->pEList->nExpr==1 && p->pEList->a[0].pExpr->op==TK_COLUMN
1582 && !p->pLimit && !p->pOffset && !p->pWhere
1583 ){
1584 sqlite3 *db = pParse->db;
1585 Index *pIdx;
1586 Expr *pExpr = p->pEList->a[0].pExpr;
1587 int iCol = pExpr->iColumn;
1588 Vdbe *v = sqlite3GetVdbe(pParse);
1589
1590 /* This function is only called from two places. In both cases the vdbe
1591 ** has already been allocated. So assume sqlite3GetVdbe() is always
1592 ** successful here.
1593 */
1594 assert(v);
1595 if( iCol<0 ){
1596 int iMem = pParse->nMem++;
1597 int iAddr;
1598 Table *pTab = p->pSrc->a[0].pTab;
1599 int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
1600 sqlite3VdbeUsesBtree(v, iDb);
1601
1602 sqlite3VdbeAddOp(v, OP_MemLoad, iMem, 0);
1603 iAddr = sqlite3VdbeAddOp(v, OP_If, 0, iMem);
1604 sqlite3VdbeAddOp(v, OP_MemInt, 1, iMem);
1605
1606 sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
1607 eType = IN_INDEX_ROWID;
1608
1609 sqlite3VdbeJumpHere(v, iAddr);
1610 }else{
1611 /* The collation sequence used by the comparison. If an index is to
1612 ** be used in place of a temp-table, it must be ordered according
1613 ** to this collation sequence.
1614 */
1615 CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pExpr);
1616
1617 /* Check that the affinity that will be used to perform the
1618 ** comparison is the same as the affinity of the column. If
1619 ** it is not, it is not possible to use any index.
1620 */
1621 Table *pTab = p->pSrc->a[0].pTab;
1622 char aff = comparisonAffinity(pX);
1623 int affinity_ok = (pTab->aCol[iCol].affinity==aff||aff==SQLITE_AFF_NONE);
1624
1625 for(pIdx=pTab->pIndex; pIdx && eType==0 && affinity_ok; pIdx=pIdx->pNext){
1626 if( (pIdx->aiColumn[0]==iCol)
1627 && (pReq==sqlite3FindCollSeq(db, ENC(db), pIdx->azColl[0], -1, 0))
1628 && (!mustBeUnique || (pIdx->nColumn==1 && pIdx->onError!=OE_None))
1629 ){
1630 int iDb;
1631 int iMem = pParse->nMem++;
1632 int iAddr;
1633 char *pKey;
1634
1635 pKey = (char *)sqlite3IndexKeyinfo(pParse, pIdx);
1636 iDb = sqlite3SchemaToIndex(db, pIdx->pSchema);
1637 sqlite3VdbeUsesBtree(v, iDb);
1638
1639 sqlite3VdbeAddOp(v, OP_MemLoad, iMem, 0);
1640 iAddr = sqlite3VdbeAddOp(v, OP_If, 0, iMem);
1641 sqlite3VdbeAddOp(v, OP_MemInt, 1, iMem);
1642
1643 sqlite3VdbeAddOp(v, OP_Integer, iDb, 0);
1644 VdbeComment((v, "# %s", pIdx->zName));
1645 sqlite3VdbeOp3(v,OP_OpenRead,iTab,pIdx->tnum,pKey,P3_KEYINFO_HANDOFF);
1646 eType = IN_INDEX_INDEX;
1647 sqlite3VdbeAddOp(v, OP_SetNumColumns, iTab, pIdx->nColumn);
1648
1649 sqlite3VdbeJumpHere(v, iAddr);
1650 }
1651 }
1652 }
1653 }
1654
1655 if( eType==0 ){
1656 sqlite3CodeSubselect(pParse, pX);
1657 eType = IN_INDEX_EPH;
1658 }else{
1659 pX->iTable = iTab;
1660 }
1661 return eType;
1662}
danielk1977284f4ac2007-12-10 05:03:46 +00001663#endif
drh626a8792005-01-17 22:08:19 +00001664
1665/*
drh9cbe6352005-11-29 03:13:21 +00001666** Generate code for scalar subqueries used as an expression
1667** and IN operators. Examples:
drh626a8792005-01-17 22:08:19 +00001668**
drh9cbe6352005-11-29 03:13:21 +00001669** (SELECT a FROM b) -- subquery
1670** EXISTS (SELECT a FROM b) -- EXISTS subquery
1671** x IN (4,5,11) -- IN operator with list on right-hand side
1672** x IN (SELECT a FROM b) -- IN operator with subquery on the right
drhfef52082000-06-06 01:50:43 +00001673**
drh9cbe6352005-11-29 03:13:21 +00001674** The pExpr parameter describes the expression that contains the IN
1675** operator or subquery.
drhcce7d172000-05-31 15:34:51 +00001676*/
drh51522cd2005-01-20 13:36:19 +00001677#ifndef SQLITE_OMIT_SUBQUERY
danielk1977b3bce662005-01-29 08:32:43 +00001678void sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){
drh57dbd7b2005-07-08 18:25:26 +00001679 int testAddr = 0; /* One-time test address */
danielk1977b3bce662005-01-29 08:32:43 +00001680 Vdbe *v = sqlite3GetVdbe(pParse);
1681 if( v==0 ) return;
1682
danielk1977fc976062007-05-10 10:46:56 +00001683
drh57dbd7b2005-07-08 18:25:26 +00001684 /* This code must be run in its entirety every time it is encountered
1685 ** if any of the following is true:
1686 **
1687 ** * The right-hand side is a correlated subquery
1688 ** * The right-hand side is an expression list containing variables
1689 ** * We are inside a trigger
1690 **
1691 ** If all of the above are false, then we can run this code just once
1692 ** save the results, and reuse the same result on subsequent invocations.
danielk1977b3bce662005-01-29 08:32:43 +00001693 */
1694 if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->trigStack ){
1695 int mem = pParse->nMem++;
1696 sqlite3VdbeAddOp(v, OP_MemLoad, mem, 0);
drh57dbd7b2005-07-08 18:25:26 +00001697 testAddr = sqlite3VdbeAddOp(v, OP_If, 0, 0);
drh17435752007-08-16 04:30:38 +00001698 assert( testAddr>0 || pParse->db->mallocFailed );
drhd654be82005-09-20 17:42:23 +00001699 sqlite3VdbeAddOp(v, OP_MemInt, 1, mem);
danielk1977b3bce662005-01-29 08:32:43 +00001700 }
1701
drhcce7d172000-05-31 15:34:51 +00001702 switch( pExpr->op ){
drhfef52082000-06-06 01:50:43 +00001703 case TK_IN: {
danielk1977e014a832004-05-17 10:48:57 +00001704 char affinity;
drhd3d39e92004-05-20 22:16:29 +00001705 KeyInfo keyInfo;
drhb9bb7c12006-06-11 23:41:55 +00001706 int addr; /* Address of OP_OpenEphemeral instruction */
drhd3d39e92004-05-20 22:16:29 +00001707
danielk1977bf3b7212004-05-18 10:06:24 +00001708 affinity = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +00001709
1710 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
drh57dbd7b2005-07-08 18:25:26 +00001711 ** expression it is handled the same way. A virtual table is
danielk1977e014a832004-05-17 10:48:57 +00001712 ** filled with single-field index keys representing the results
1713 ** from the SELECT or the <exprlist>.
1714 **
1715 ** If the 'x' expression is a column value, or the SELECT...
1716 ** statement returns a column value, then the affinity of that
1717 ** column is used to build the index keys. If both 'x' and the
1718 ** SELECT... statement are columns, then numeric affinity is used
1719 ** if either column has NUMERIC or INTEGER affinity. If neither
1720 ** 'x' nor the SELECT... statement are columns, then numeric affinity
1721 ** is used.
1722 */
1723 pExpr->iTable = pParse->nTab++;
drhb9bb7c12006-06-11 23:41:55 +00001724 addr = sqlite3VdbeAddOp(v, OP_OpenEphemeral, pExpr->iTable, 0);
drhd3d39e92004-05-20 22:16:29 +00001725 memset(&keyInfo, 0, sizeof(keyInfo));
1726 keyInfo.nField = 1;
drhf3218fe2004-05-28 08:21:02 +00001727 sqlite3VdbeAddOp(v, OP_SetNumColumns, pExpr->iTable, 1);
danielk1977e014a832004-05-17 10:48:57 +00001728
drhfef52082000-06-06 01:50:43 +00001729 if( pExpr->pSelect ){
1730 /* Case 1: expr IN (SELECT ...)
1731 **
danielk1977e014a832004-05-17 10:48:57 +00001732 ** Generate code to write the results of the select into the temporary
1733 ** table allocated and opened above.
drhfef52082000-06-06 01:50:43 +00001734 */
danielk1977e014a832004-05-17 10:48:57 +00001735 int iParm = pExpr->iTable + (((int)affinity)<<16);
drhbe5c89a2004-07-26 00:31:09 +00001736 ExprList *pEList;
danielk1977e014a832004-05-17 10:48:57 +00001737 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
drh94ccde52007-04-13 16:06:32 +00001738 if( sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0) ){
1739 return;
1740 }
drhbe5c89a2004-07-26 00:31:09 +00001741 pEList = pExpr->pSelect->pEList;
1742 if( pEList && pEList->nExpr>0 ){
danielk1977bcbb04e2007-05-29 12:11:29 +00001743 keyInfo.aColl[0] = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft,
drhbe5c89a2004-07-26 00:31:09 +00001744 pEList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001745 }
drhfef52082000-06-06 01:50:43 +00001746 }else if( pExpr->pList ){
1747 /* Case 2: expr IN (exprlist)
1748 **
drhfd131da2007-08-07 17:13:03 +00001749 ** For each expression, build an index key from the evaluation and
danielk1977e014a832004-05-17 10:48:57 +00001750 ** store it in the temporary table. If <expr> is a column, then use
1751 ** that columns affinity when building index keys. If <expr> is not
1752 ** a column, use numeric affinity.
drhfef52082000-06-06 01:50:43 +00001753 */
danielk1977e014a832004-05-17 10:48:57 +00001754 int i;
drh57dbd7b2005-07-08 18:25:26 +00001755 ExprList *pList = pExpr->pList;
1756 struct ExprList_item *pItem;
1757
danielk1977e014a832004-05-17 10:48:57 +00001758 if( !affinity ){
drh8159a352006-05-23 23:22:29 +00001759 affinity = SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +00001760 }
danielk19770202b292004-06-09 09:55:16 +00001761 keyInfo.aColl[0] = pExpr->pLeft->pColl;
danielk1977e014a832004-05-17 10:48:57 +00001762
1763 /* Loop through each expression in <exprlist>. */
drh57dbd7b2005-07-08 18:25:26 +00001764 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
1765 Expr *pE2 = pItem->pExpr;
danielk1977e014a832004-05-17 10:48:57 +00001766
drh57dbd7b2005-07-08 18:25:26 +00001767 /* If the expression is not constant then we will need to
1768 ** disable the test that was generated above that makes sure
1769 ** this code only executes once. Because for a non-constant
1770 ** expression we need to rerun this code each time.
1771 */
drh6c30be82005-07-29 15:10:17 +00001772 if( testAddr>0 && !sqlite3ExprIsConstant(pE2) ){
drhf8875402006-03-17 13:56:34 +00001773 sqlite3VdbeChangeToNoop(v, testAddr-1, 3);
drh57dbd7b2005-07-08 18:25:26 +00001774 testAddr = 0;
drh4794b982000-06-06 13:54:14 +00001775 }
danielk1977e014a832004-05-17 10:48:57 +00001776
1777 /* Evaluate the expression and insert it into the temp table */
1778 sqlite3ExprCode(pParse, pE2);
drh94a11212004-09-25 13:12:14 +00001779 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1);
drhf0863fe2005-06-12 21:35:51 +00001780 sqlite3VdbeAddOp(v, OP_IdxInsert, pExpr->iTable, 0);
drhfef52082000-06-06 01:50:43 +00001781 }
1782 }
danielk19770202b292004-06-09 09:55:16 +00001783 sqlite3VdbeChangeP3(v, addr, (void *)&keyInfo, P3_KEYINFO);
danielk1977b3bce662005-01-29 08:32:43 +00001784 break;
drhfef52082000-06-06 01:50:43 +00001785 }
1786
drh51522cd2005-01-20 13:36:19 +00001787 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001788 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +00001789 /* This has to be a scalar SELECT. Generate code to put the
1790 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +00001791 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +00001792 */
drh2646da72005-12-09 20:02:05 +00001793 static const Token one = { (u8*)"1", 0, 1 };
drh51522cd2005-01-20 13:36:19 +00001794 Select *pSel;
drhec7429a2005-10-06 16:53:14 +00001795 int iMem;
1796 int sop;
drh1398ad32005-01-19 23:24:50 +00001797
drhec7429a2005-10-06 16:53:14 +00001798 pExpr->iColumn = iMem = pParse->nMem++;
drh51522cd2005-01-20 13:36:19 +00001799 pSel = pExpr->pSelect;
1800 if( pExpr->op==TK_SELECT ){
1801 sop = SRT_Mem;
drhec7429a2005-10-06 16:53:14 +00001802 sqlite3VdbeAddOp(v, OP_MemNull, iMem, 0);
1803 VdbeComment((v, "# Init subquery result"));
drh51522cd2005-01-20 13:36:19 +00001804 }else{
drh51522cd2005-01-20 13:36:19 +00001805 sop = SRT_Exists;
drhec7429a2005-10-06 16:53:14 +00001806 sqlite3VdbeAddOp(v, OP_MemInt, 0, iMem);
1807 VdbeComment((v, "# Init EXISTS result"));
drh51522cd2005-01-20 13:36:19 +00001808 }
drhec7429a2005-10-06 16:53:14 +00001809 sqlite3ExprDelete(pSel->pLimit);
danielk1977a1644fd2007-08-29 12:31:25 +00001810 pSel->pLimit = sqlite3PExpr(pParse, TK_INTEGER, 0, 0, &one);
drh94ccde52007-04-13 16:06:32 +00001811 if( sqlite3Select(pParse, pSel, sop, iMem, 0, 0, 0, 0) ){
1812 return;
1813 }
danielk1977b3bce662005-01-29 08:32:43 +00001814 break;
drhcce7d172000-05-31 15:34:51 +00001815 }
1816 }
danielk1977b3bce662005-01-29 08:32:43 +00001817
drh57dbd7b2005-07-08 18:25:26 +00001818 if( testAddr ){
drhd654be82005-09-20 17:42:23 +00001819 sqlite3VdbeJumpHere(v, testAddr);
danielk1977b3bce662005-01-29 08:32:43 +00001820 }
danielk1977fc976062007-05-10 10:46:56 +00001821
danielk1977b3bce662005-01-29 08:32:43 +00001822 return;
drhcce7d172000-05-31 15:34:51 +00001823}
drh51522cd2005-01-20 13:36:19 +00001824#endif /* SQLITE_OMIT_SUBQUERY */
drhcce7d172000-05-31 15:34:51 +00001825
drhcce7d172000-05-31 15:34:51 +00001826/*
drh598f1342007-10-23 15:39:45 +00001827** Duplicate an 8-byte value
1828*/
1829static char *dup8bytes(Vdbe *v, const char *in){
1830 char *out = sqlite3DbMallocRaw(sqlite3VdbeDb(v), 8);
1831 if( out ){
1832 memcpy(out, in, 8);
1833 }
1834 return out;
1835}
1836
1837/*
1838** Generate an instruction that will put the floating point
1839** value described by z[0..n-1] on the stack.
drh0cf19ed2007-10-23 18:55:48 +00001840**
1841** The z[] string will probably not be zero-terminated. But the
1842** z[n] character is guaranteed to be something that does not look
1843** like the continuation of the number.
drh598f1342007-10-23 15:39:45 +00001844*/
1845static void codeReal(Vdbe *v, const char *z, int n, int negateFlag){
1846 assert( z || v==0 || sqlite3VdbeDb(v)->mallocFailed );
1847 if( z ){
1848 double value;
1849 char *zV;
drh0cf19ed2007-10-23 18:55:48 +00001850 assert( !isdigit(z[n]) );
drh598f1342007-10-23 15:39:45 +00001851 sqlite3AtoF(z, &value);
1852 if( negateFlag ) value = -value;
1853 zV = dup8bytes(v, (char*)&value);
1854 sqlite3VdbeOp3(v, OP_Real, 0, 0, zV, P3_REAL);
1855 }
1856}
1857
1858
1859/*
drhfec19aa2004-05-19 20:41:03 +00001860** Generate an instruction that will put the integer describe by
1861** text z[0..n-1] on the stack.
drh0cf19ed2007-10-23 18:55:48 +00001862**
1863** The z[] string will probably not be zero-terminated. But the
1864** z[n] character is guaranteed to be something that does not look
1865** like the continuation of the number.
drhfec19aa2004-05-19 20:41:03 +00001866*/
drh598f1342007-10-23 15:39:45 +00001867static void codeInteger(Vdbe *v, const char *z, int n, int negateFlag){
drhabb6fca2007-08-16 12:24:01 +00001868 assert( z || v==0 || sqlite3VdbeDb(v)->mallocFailed );
danielk1977c9cf9012007-05-30 10:36:47 +00001869 if( z ){
1870 int i;
drh0cf19ed2007-10-23 18:55:48 +00001871 assert( !isdigit(z[n]) );
danielk1977c9cf9012007-05-30 10:36:47 +00001872 if( sqlite3GetInt32(z, &i) ){
drh598f1342007-10-23 15:39:45 +00001873 if( negateFlag ) i = -i;
danielk1977c9cf9012007-05-30 10:36:47 +00001874 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
drh598f1342007-10-23 15:39:45 +00001875 }else if( sqlite3FitsIn64Bits(z, negateFlag) ){
1876 i64 value;
1877 char *zV;
1878 sqlite3Atoi64(z, &value);
1879 if( negateFlag ) value = -value;
1880 zV = dup8bytes(v, (char*)&value);
1881 sqlite3VdbeOp3(v, OP_Int64, 0, 0, zV, P3_INT64);
danielk1977c9cf9012007-05-30 10:36:47 +00001882 }else{
drh598f1342007-10-23 15:39:45 +00001883 codeReal(v, z, n, negateFlag);
danielk1977c9cf9012007-05-30 10:36:47 +00001884 }
drhfec19aa2004-05-19 20:41:03 +00001885 }
1886}
1887
drh945498f2007-02-24 11:52:52 +00001888
1889/*
1890** Generate code that will extract the iColumn-th column from
1891** table pTab and push that column value on the stack. There
1892** is an open cursor to pTab in iTable. If iColumn<0 then
1893** code is generated that extracts the rowid.
1894*/
1895void sqlite3ExprCodeGetColumn(Vdbe *v, Table *pTab, int iColumn, int iTable){
1896 if( iColumn<0 ){
1897 int op = (pTab && IsVirtual(pTab)) ? OP_VRowid : OP_Rowid;
1898 sqlite3VdbeAddOp(v, op, iTable, 0);
1899 }else if( pTab==0 ){
1900 sqlite3VdbeAddOp(v, OP_Column, iTable, iColumn);
1901 }else{
1902 int op = IsVirtual(pTab) ? OP_VColumn : OP_Column;
1903 sqlite3VdbeAddOp(v, op, iTable, iColumn);
1904 sqlite3ColumnDefault(v, pTab, iColumn);
1905#ifndef SQLITE_OMIT_FLOATING_POINT
1906 if( pTab->aCol[iColumn].affinity==SQLITE_AFF_REAL ){
1907 sqlite3VdbeAddOp(v, OP_RealAffinity, 0, 0);
1908 }
1909#endif
1910 }
1911}
1912
drhfec19aa2004-05-19 20:41:03 +00001913/*
drhcce7d172000-05-31 15:34:51 +00001914** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +00001915** expression and leave the result on the top of stack.
drhf2bc0132004-10-04 13:19:23 +00001916**
1917** This code depends on the fact that certain token values (ex: TK_EQ)
1918** are the same as opcode values (ex: OP_Eq) that implement the corresponding
1919** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
1920** the make process cause these values to align. Assert()s in the code
1921** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00001922*/
danielk19774adee202004-05-08 08:23:19 +00001923void sqlite3ExprCode(Parse *pParse, Expr *pExpr){
drhcce7d172000-05-31 15:34:51 +00001924 Vdbe *v = pParse->pVdbe;
1925 int op;
drhffe07b22005-11-03 00:41:17 +00001926 int stackChng = 1; /* Amount of change to stack depth */
1927
danielk19777977a172004-11-09 12:44:37 +00001928 if( v==0 ) return;
1929 if( pExpr==0 ){
drhf0863fe2005-06-12 21:35:51 +00001930 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
danielk19777977a172004-11-09 12:44:37 +00001931 return;
1932 }
drhf2bc0132004-10-04 13:19:23 +00001933 op = pExpr->op;
1934 switch( op ){
drh13449892005-09-07 21:22:45 +00001935 case TK_AGG_COLUMN: {
1936 AggInfo *pAggInfo = pExpr->pAggInfo;
1937 struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg];
1938 if( !pAggInfo->directMode ){
1939 sqlite3VdbeAddOp(v, OP_MemLoad, pCol->iMem, 0);
1940 break;
1941 }else if( pAggInfo->useSortingIdx ){
1942 sqlite3VdbeAddOp(v, OP_Column, pAggInfo->sortingIdx,
1943 pCol->iSorterColumn);
1944 break;
1945 }
1946 /* Otherwise, fall thru into the TK_COLUMN case */
1947 }
drh967e8b72000-06-21 13:59:10 +00001948 case TK_COLUMN: {
drhffe07b22005-11-03 00:41:17 +00001949 if( pExpr->iTable<0 ){
1950 /* This only happens when coding check constraints */
1951 assert( pParse->ckOffset>0 );
1952 sqlite3VdbeAddOp(v, OP_Dup, pParse->ckOffset-pExpr->iColumn-1, 1);
drhc4a3c772001-04-04 11:48:57 +00001953 }else{
drh945498f2007-02-24 11:52:52 +00001954 sqlite3ExprCodeGetColumn(v, pExpr->pTab, pExpr->iColumn, pExpr->iTable);
drh22827922000-06-06 17:27:05 +00001955 }
drhcce7d172000-05-31 15:34:51 +00001956 break;
1957 }
1958 case TK_INTEGER: {
drh598f1342007-10-23 15:39:45 +00001959 codeInteger(v, (char*)pExpr->token.z, pExpr->token.n, 0);
drhfec19aa2004-05-19 20:41:03 +00001960 break;
1961 }
drh598f1342007-10-23 15:39:45 +00001962 case TK_FLOAT: {
1963 codeReal(v, (char*)pExpr->token.z, pExpr->token.n, 0);
1964 break;
1965 }
drhfec19aa2004-05-19 20:41:03 +00001966 case TK_STRING: {
danielk19771e536952007-08-16 10:09:01 +00001967 sqlite3DequoteExpr(pParse->db, pExpr);
drh598f1342007-10-23 15:39:45 +00001968 sqlite3VdbeOp3(v,OP_String8, 0, 0, (char*)pExpr->token.z, pExpr->token.n);
drhcce7d172000-05-31 15:34:51 +00001969 break;
1970 }
drhf0863fe2005-06-12 21:35:51 +00001971 case TK_NULL: {
1972 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
1973 break;
1974 }
danielk19775338a5f2005-01-20 13:03:10 +00001975#ifndef SQLITE_OMIT_BLOB_LITERAL
danielk1977c572ef72004-05-27 09:28:41 +00001976 case TK_BLOB: {
drh6c8c6ce2005-08-23 11:17:58 +00001977 int n;
1978 const char *z;
drhf2bc0132004-10-04 13:19:23 +00001979 assert( TK_BLOB==OP_HexBlob );
drh6c8c6ce2005-08-23 11:17:58 +00001980 n = pExpr->token.n - 3;
drh2646da72005-12-09 20:02:05 +00001981 z = (char*)pExpr->token.z + 2;
drh6c8c6ce2005-08-23 11:17:58 +00001982 assert( n>=0 );
1983 if( n==0 ){
1984 z = "";
1985 }
1986 sqlite3VdbeOp3(v, op, 0, 0, z, n);
danielk1977c572ef72004-05-27 09:28:41 +00001987 break;
1988 }
danielk19775338a5f2005-01-20 13:03:10 +00001989#endif
drh50457892003-09-06 01:10:47 +00001990 case TK_VARIABLE: {
danielk19774adee202004-05-08 08:23:19 +00001991 sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0);
drh895d7472004-08-20 16:02:39 +00001992 if( pExpr->token.n>1 ){
drh2646da72005-12-09 20:02:05 +00001993 sqlite3VdbeChangeP3(v, -1, (char*)pExpr->token.z, pExpr->token.n);
drh895d7472004-08-20 16:02:39 +00001994 }
drh50457892003-09-06 01:10:47 +00001995 break;
1996 }
drh4e0cff62004-11-05 05:10:28 +00001997 case TK_REGISTER: {
1998 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iTable, 0);
1999 break;
2000 }
drh487e2622005-06-25 18:42:14 +00002001#ifndef SQLITE_OMIT_CAST
2002 case TK_CAST: {
2003 /* Expressions of the form: CAST(pLeft AS token) */
danielk1977f0113002006-01-24 12:09:17 +00002004 int aff, to_op;
drh487e2622005-06-25 18:42:14 +00002005 sqlite3ExprCode(pParse, pExpr->pLeft);
drh8a512562005-11-14 22:29:05 +00002006 aff = sqlite3AffinityType(&pExpr->token);
danielk1977f0113002006-01-24 12:09:17 +00002007 to_op = aff - SQLITE_AFF_TEXT + OP_ToText;
2008 assert( to_op==OP_ToText || aff!=SQLITE_AFF_TEXT );
2009 assert( to_op==OP_ToBlob || aff!=SQLITE_AFF_NONE );
2010 assert( to_op==OP_ToNumeric || aff!=SQLITE_AFF_NUMERIC );
2011 assert( to_op==OP_ToInt || aff!=SQLITE_AFF_INTEGER );
2012 assert( to_op==OP_ToReal || aff!=SQLITE_AFF_REAL );
2013 sqlite3VdbeAddOp(v, to_op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00002014 stackChng = 0;
drh487e2622005-06-25 18:42:14 +00002015 break;
2016 }
2017#endif /* SQLITE_OMIT_CAST */
drhc9b84a12002-06-20 11:36:48 +00002018 case TK_LT:
2019 case TK_LE:
2020 case TK_GT:
2021 case TK_GE:
2022 case TK_NE:
2023 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00002024 assert( TK_LT==OP_Lt );
2025 assert( TK_LE==OP_Le );
2026 assert( TK_GT==OP_Gt );
2027 assert( TK_GE==OP_Ge );
2028 assert( TK_EQ==OP_Eq );
2029 assert( TK_NE==OP_Ne );
danielk1977a37cdde2004-05-16 11:15:36 +00002030 sqlite3ExprCode(pParse, pExpr->pLeft);
2031 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00002032 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00002033 stackChng = -1;
danielk1977a37cdde2004-05-16 11:15:36 +00002034 break;
drhc9b84a12002-06-20 11:36:48 +00002035 }
drhcce7d172000-05-31 15:34:51 +00002036 case TK_AND:
2037 case TK_OR:
2038 case TK_PLUS:
2039 case TK_STAR:
2040 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00002041 case TK_REM:
2042 case TK_BITAND:
2043 case TK_BITOR:
drh17c40292004-07-21 02:53:29 +00002044 case TK_SLASH:
drhbf4133c2001-10-13 02:59:08 +00002045 case TK_LSHIFT:
drh855eb1c2004-08-31 13:45:11 +00002046 case TK_RSHIFT:
drh00400772000-06-16 20:51:26 +00002047 case TK_CONCAT: {
drhf2bc0132004-10-04 13:19:23 +00002048 assert( TK_AND==OP_And );
2049 assert( TK_OR==OP_Or );
2050 assert( TK_PLUS==OP_Add );
2051 assert( TK_MINUS==OP_Subtract );
2052 assert( TK_REM==OP_Remainder );
2053 assert( TK_BITAND==OP_BitAnd );
2054 assert( TK_BITOR==OP_BitOr );
2055 assert( TK_SLASH==OP_Divide );
2056 assert( TK_LSHIFT==OP_ShiftLeft );
2057 assert( TK_RSHIFT==OP_ShiftRight );
2058 assert( TK_CONCAT==OP_Concat );
danielk19774adee202004-05-08 08:23:19 +00002059 sqlite3ExprCode(pParse, pExpr->pLeft);
2060 sqlite3ExprCode(pParse, pExpr->pRight);
drh855eb1c2004-08-31 13:45:11 +00002061 sqlite3VdbeAddOp(v, op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00002062 stackChng = -1;
drh00400772000-06-16 20:51:26 +00002063 break;
2064 }
drhcce7d172000-05-31 15:34:51 +00002065 case TK_UMINUS: {
drhfec19aa2004-05-19 20:41:03 +00002066 Expr *pLeft = pExpr->pLeft;
2067 assert( pLeft );
2068 if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
2069 Token *p = &pLeft->token;
drhfec19aa2004-05-19 20:41:03 +00002070 if( pLeft->op==TK_FLOAT ){
drh598f1342007-10-23 15:39:45 +00002071 codeReal(v, (char*)p->z, p->n, 1);
drhe6840902002-03-06 03:08:25 +00002072 }else{
drh598f1342007-10-23 15:39:45 +00002073 codeInteger(v, (char*)p->z, p->n, 1);
drhe6840902002-03-06 03:08:25 +00002074 }
drh6e142f52000-06-08 13:36:40 +00002075 break;
2076 }
drh1ccde152000-06-17 13:12:39 +00002077 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +00002078 }
drhbf4133c2001-10-13 02:59:08 +00002079 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00002080 case TK_NOT: {
drhf2bc0132004-10-04 13:19:23 +00002081 assert( TK_BITNOT==OP_BitNot );
2082 assert( TK_NOT==OP_Not );
danielk19774adee202004-05-08 08:23:19 +00002083 sqlite3ExprCode(pParse, pExpr->pLeft);
2084 sqlite3VdbeAddOp(v, op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00002085 stackChng = 0;
drhcce7d172000-05-31 15:34:51 +00002086 break;
2087 }
2088 case TK_ISNULL:
2089 case TK_NOTNULL: {
2090 int dest;
drhf2bc0132004-10-04 13:19:23 +00002091 assert( TK_ISNULL==OP_IsNull );
2092 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00002093 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
2094 sqlite3ExprCode(pParse, pExpr->pLeft);
2095 dest = sqlite3VdbeCurrentAddr(v) + 2;
2096 sqlite3VdbeAddOp(v, op, 1, dest);
2097 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);
drhffe07b22005-11-03 00:41:17 +00002098 stackChng = 0;
drhf2bc0132004-10-04 13:19:23 +00002099 break;
drhcce7d172000-05-31 15:34:51 +00002100 }
drh22827922000-06-06 17:27:05 +00002101 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00002102 AggInfo *pInfo = pExpr->pAggInfo;
drh7e56e712005-11-16 12:53:15 +00002103 if( pInfo==0 ){
2104 sqlite3ErrorMsg(pParse, "misuse of aggregate: %T",
2105 &pExpr->span);
2106 }else{
2107 sqlite3VdbeAddOp(v, OP_MemLoad, pInfo->aFunc[pExpr->iAgg].iMem, 0);
2108 }
drh22827922000-06-06 17:27:05 +00002109 break;
2110 }
drhb71090f2005-05-23 17:26:51 +00002111 case TK_CONST_FUNC:
drhcce7d172000-05-31 15:34:51 +00002112 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00002113 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00002114 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00002115 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00002116 int nId;
2117 const char *zId;
drh13449892005-09-07 21:22:45 +00002118 int constMask = 0;
danielk1977682f68b2004-06-05 10:22:17 +00002119 int i;
drh17435752007-08-16 04:30:38 +00002120 sqlite3 *db = pParse->db;
2121 u8 enc = ENC(db);
danielk1977dc1bdc42004-06-11 10:51:27 +00002122 CollSeq *pColl = 0;
drh17435752007-08-16 04:30:38 +00002123
drh2646da72005-12-09 20:02:05 +00002124 zId = (char*)pExpr->token.z;
drhb71090f2005-05-23 17:26:51 +00002125 nId = pExpr->token.n;
danielk1977d8123362004-06-12 09:25:12 +00002126 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0);
drh0bce8352002-02-28 00:41:10 +00002127 assert( pDef!=0 );
drhf9b596e2004-05-26 16:54:42 +00002128 nExpr = sqlite3ExprCodeExprList(pParse, pList);
drhb7f6f682006-07-08 17:06:43 +00002129#ifndef SQLITE_OMIT_VIRTUALTABLE
drha43fa222006-07-08 18:41:37 +00002130 /* Possibly overload the function if the first argument is
2131 ** a virtual table column.
2132 **
2133 ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the
2134 ** second argument, not the first, as the argument to test to
2135 ** see if it is a column in a virtual table. This is done because
2136 ** the left operand of infix functions (the operand we want to
2137 ** control overloading) ends up as the second argument to the
2138 ** function. The expression "A glob B" is equivalent to
2139 ** "glob(B,A). We want to use the A in "A glob B" to test
2140 ** for function overloading. But we use the B term in "glob(B,A)".
2141 */
drh6a03a1c2006-07-08 18:34:59 +00002142 if( nExpr>=2 && (pExpr->flags & EP_InfixFunc) ){
drh17435752007-08-16 04:30:38 +00002143 pDef = sqlite3VtabOverloadFunction(db, pDef, nExpr, pList->a[1].pExpr);
drh6a03a1c2006-07-08 18:34:59 +00002144 }else if( nExpr>0 ){
drh17435752007-08-16 04:30:38 +00002145 pDef = sqlite3VtabOverloadFunction(db, pDef, nExpr, pList->a[0].pExpr);
drhb7f6f682006-07-08 17:06:43 +00002146 }
2147#endif
danielk1977682f68b2004-06-05 10:22:17 +00002148 for(i=0; i<nExpr && i<32; i++){
danielk1977d02eb1f2004-06-06 09:44:03 +00002149 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
drh13449892005-09-07 21:22:45 +00002150 constMask |= (1<<i);
danielk1977d02eb1f2004-06-06 09:44:03 +00002151 }
danielk1977dc1bdc42004-06-11 10:51:27 +00002152 if( pDef->needCollSeq && !pColl ){
2153 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
2154 }
2155 }
2156 if( pDef->needCollSeq ){
2157 if( !pColl ) pColl = pParse->db->pDfltColl;
danielk1977d8123362004-06-12 09:25:12 +00002158 sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ);
danielk1977682f68b2004-06-05 10:22:17 +00002159 }
drh13449892005-09-07 21:22:45 +00002160 sqlite3VdbeOp3(v, OP_Function, constMask, nExpr, (char*)pDef, P3_FUNCDEF);
drhffe07b22005-11-03 00:41:17 +00002161 stackChng = 1-nExpr;
drhcce7d172000-05-31 15:34:51 +00002162 break;
2163 }
drhfe2093d2005-01-20 22:48:47 +00002164#ifndef SQLITE_OMIT_SUBQUERY
2165 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00002166 case TK_SELECT: {
drh41714d62006-03-02 04:44:23 +00002167 if( pExpr->iColumn==0 ){
2168 sqlite3CodeSubselect(pParse, pExpr);
2169 }
danielk19774adee202004-05-08 08:23:19 +00002170 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drhad6d9462004-09-19 02:15:24 +00002171 VdbeComment((v, "# load subquery result"));
drh19a775c2000-06-05 18:54:46 +00002172 break;
2173 }
drhfef52082000-06-06 01:50:43 +00002174 case TK_IN: {
2175 int addr;
drh94a11212004-09-25 13:12:14 +00002176 char affinity;
drhafa5f682006-01-30 14:36:59 +00002177 int ckOffset = pParse->ckOffset;
danielk19779a96b662007-11-29 17:05:18 +00002178 int eType;
2179 int iLabel = sqlite3VdbeMakeLabel(v);
2180
2181 eType = sqlite3FindInIndex(pParse, pExpr, 0);
danielk1977e014a832004-05-17 10:48:57 +00002182
2183 /* Figure out the affinity to use to create a key from the results
2184 ** of the expression. affinityStr stores a static string suitable for
danielk1977ededfd52004-06-17 07:53:01 +00002185 ** P3 of OP_MakeRecord.
danielk1977e014a832004-05-17 10:48:57 +00002186 */
drh94a11212004-09-25 13:12:14 +00002187 affinity = comparisonAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +00002188
danielk19774adee202004-05-08 08:23:19 +00002189 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
danielk1977cdbd8ef2007-05-12 06:11:12 +00002190 pParse->ckOffset = (ckOffset ? (ckOffset+1) : 0);
danielk1977e014a832004-05-17 10:48:57 +00002191
2192 /* Code the <expr> from "<expr> IN (...)". The temporary table
2193 ** pExpr->iTable contains the values that make up the (...) set.
2194 */
danielk19774adee202004-05-08 08:23:19 +00002195 sqlite3ExprCode(pParse, pExpr->pLeft);
2196 addr = sqlite3VdbeCurrentAddr(v);
danielk1977e014a832004-05-17 10:48:57 +00002197 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); /* addr + 0 */
danielk19774adee202004-05-08 08:23:19 +00002198 sqlite3VdbeAddOp(v, OP_Pop, 2, 0);
drhf0863fe2005-06-12 21:35:51 +00002199 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
danielk19779a96b662007-11-29 17:05:18 +00002200 sqlite3VdbeAddOp(v, OP_Goto, 0, iLabel);
2201 if( eType==IN_INDEX_ROWID ){
2202 int iAddr = sqlite3VdbeCurrentAddr(v)+3;
2203 sqlite3VdbeAddOp(v, OP_MustBeInt, 1, iAddr);
2204 sqlite3VdbeAddOp(v, OP_NotExists, pExpr->iTable, iAddr);
2205 sqlite3VdbeAddOp(v, OP_Goto, pExpr->iTable, iLabel);
2206 }else{
2207 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1); /* addr + 4 */
2208 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, iLabel);
2209 }
danielk1977e014a832004-05-17 10:48:57 +00002210 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); /* addr + 6 */
danielk19779a96b662007-11-29 17:05:18 +00002211 sqlite3VdbeResolveLabel(v, iLabel);
danielk1977e014a832004-05-17 10:48:57 +00002212
drhfef52082000-06-06 01:50:43 +00002213 break;
2214 }
danielk197793758c82005-01-21 08:13:14 +00002215#endif
drhfef52082000-06-06 01:50:43 +00002216 case TK_BETWEEN: {
drhbe5c89a2004-07-26 00:31:09 +00002217 Expr *pLeft = pExpr->pLeft;
2218 struct ExprList_item *pLItem = pExpr->pList->a;
2219 Expr *pRight = pLItem->pExpr;
2220 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00002221 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00002222 sqlite3ExprCode(pParse, pRight);
2223 codeCompare(pParse, pLeft, pRight, OP_Ge, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00002224 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
drhbe5c89a2004-07-26 00:31:09 +00002225 pLItem++;
2226 pRight = pLItem->pExpr;
2227 sqlite3ExprCode(pParse, pRight);
2228 codeCompare(pParse, pLeft, pRight, OP_Le, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00002229 sqlite3VdbeAddOp(v, OP_And, 0, 0);
drhfef52082000-06-06 01:50:43 +00002230 break;
2231 }
drh4f07e5f2007-05-14 11:34:46 +00002232 case TK_UPLUS: {
danielk19774adee202004-05-08 08:23:19 +00002233 sqlite3ExprCode(pParse, pExpr->pLeft);
drhffe07b22005-11-03 00:41:17 +00002234 stackChng = 0;
drha2e00042002-01-22 03:13:42 +00002235 break;
2236 }
drh17a7f8d2002-03-24 13:13:27 +00002237 case TK_CASE: {
2238 int expr_end_label;
drhf5905aa2002-05-26 20:54:33 +00002239 int jumpInst;
drhf5905aa2002-05-26 20:54:33 +00002240 int nExpr;
drh17a7f8d2002-03-24 13:13:27 +00002241 int i;
drhbe5c89a2004-07-26 00:31:09 +00002242 ExprList *pEList;
2243 struct ExprList_item *aListelem;
drh17a7f8d2002-03-24 13:13:27 +00002244
2245 assert(pExpr->pList);
2246 assert((pExpr->pList->nExpr % 2) == 0);
2247 assert(pExpr->pList->nExpr > 0);
drhbe5c89a2004-07-26 00:31:09 +00002248 pEList = pExpr->pList;
2249 aListelem = pEList->a;
2250 nExpr = pEList->nExpr;
danielk19774adee202004-05-08 08:23:19 +00002251 expr_end_label = sqlite3VdbeMakeLabel(v);
drh17a7f8d2002-03-24 13:13:27 +00002252 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00002253 sqlite3ExprCode(pParse, pExpr->pLeft);
drh17a7f8d2002-03-24 13:13:27 +00002254 }
drhf5905aa2002-05-26 20:54:33 +00002255 for(i=0; i<nExpr; i=i+2){
drhbe5c89a2004-07-26 00:31:09 +00002256 sqlite3ExprCode(pParse, aListelem[i].pExpr);
drh17a7f8d2002-03-24 13:13:27 +00002257 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00002258 sqlite3VdbeAddOp(v, OP_Dup, 1, 1);
drhbe5c89a2004-07-26 00:31:09 +00002259 jumpInst = codeCompare(pParse, pExpr->pLeft, aListelem[i].pExpr,
2260 OP_Ne, 0, 1);
danielk19774adee202004-05-08 08:23:19 +00002261 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00002262 }else{
danielk19774adee202004-05-08 08:23:19 +00002263 jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00002264 }
drhbe5c89a2004-07-26 00:31:09 +00002265 sqlite3ExprCode(pParse, aListelem[i+1].pExpr);
danielk19774adee202004-05-08 08:23:19 +00002266 sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label);
drhd654be82005-09-20 17:42:23 +00002267 sqlite3VdbeJumpHere(v, jumpInst);
drh17a7f8d2002-03-24 13:13:27 +00002268 }
drhf570f012002-05-31 15:51:25 +00002269 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00002270 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhf570f012002-05-31 15:51:25 +00002271 }
drh17a7f8d2002-03-24 13:13:27 +00002272 if( pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00002273 sqlite3ExprCode(pParse, pExpr->pRight);
drh17a7f8d2002-03-24 13:13:27 +00002274 }else{
drhf0863fe2005-06-12 21:35:51 +00002275 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
drh17a7f8d2002-03-24 13:13:27 +00002276 }
danielk19774adee202004-05-08 08:23:19 +00002277 sqlite3VdbeResolveLabel(v, expr_end_label);
danielk19776f349032002-06-11 02:25:40 +00002278 break;
2279 }
danielk19775338a5f2005-01-20 13:03:10 +00002280#ifndef SQLITE_OMIT_TRIGGER
danielk19776f349032002-06-11 02:25:40 +00002281 case TK_RAISE: {
2282 if( !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +00002283 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00002284 "RAISE() may only be used within a trigger-program");
drhfd131da2007-08-07 17:13:03 +00002285 return;
danielk19776f349032002-06-11 02:25:40 +00002286 }
drhad6d9462004-09-19 02:15:24 +00002287 if( pExpr->iColumn!=OE_Ignore ){
2288 assert( pExpr->iColumn==OE_Rollback ||
2289 pExpr->iColumn == OE_Abort ||
2290 pExpr->iColumn == OE_Fail );
danielk19771e536952007-08-16 10:09:01 +00002291 sqlite3DequoteExpr(pParse->db, pExpr);
drhad6d9462004-09-19 02:15:24 +00002292 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn,
drh2646da72005-12-09 20:02:05 +00002293 (char*)pExpr->token.z, pExpr->token.n);
danielk19776f349032002-06-11 02:25:40 +00002294 } else {
drhad6d9462004-09-19 02:15:24 +00002295 assert( pExpr->iColumn == OE_Ignore );
2296 sqlite3VdbeAddOp(v, OP_ContextPop, 0, 0);
2297 sqlite3VdbeAddOp(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
2298 VdbeComment((v, "# raise(IGNORE)"));
danielk19776f349032002-06-11 02:25:40 +00002299 }
drhffe07b22005-11-03 00:41:17 +00002300 stackChng = 0;
2301 break;
drh17a7f8d2002-03-24 13:13:27 +00002302 }
danielk19775338a5f2005-01-20 13:03:10 +00002303#endif
drhffe07b22005-11-03 00:41:17 +00002304 }
2305
2306 if( pParse->ckOffset ){
2307 pParse->ckOffset += stackChng;
2308 assert( pParse->ckOffset );
drhcce7d172000-05-31 15:34:51 +00002309 }
drhcce7d172000-05-31 15:34:51 +00002310}
2311
danielk197793758c82005-01-21 08:13:14 +00002312#ifndef SQLITE_OMIT_TRIGGER
drhcce7d172000-05-31 15:34:51 +00002313/*
drh25303782004-12-07 15:41:48 +00002314** Generate code that evalutes the given expression and leaves the result
2315** on the stack. See also sqlite3ExprCode().
2316**
2317** This routine might also cache the result and modify the pExpr tree
2318** so that it will make use of the cached result on subsequent evaluations
2319** rather than evaluate the whole expression again. Trivial expressions are
2320** not cached. If the expression is cached, its result is stored in a
2321** memory location.
2322*/
2323void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr){
2324 Vdbe *v = pParse->pVdbe;
drh49df6b72007-12-14 15:12:21 +00002325 VdbeOp *pOp;
drh25303782004-12-07 15:41:48 +00002326 int iMem;
2327 int addr1, addr2;
2328 if( v==0 ) return;
2329 addr1 = sqlite3VdbeCurrentAddr(v);
2330 sqlite3ExprCode(pParse, pExpr);
2331 addr2 = sqlite3VdbeCurrentAddr(v);
drh49df6b72007-12-14 15:12:21 +00002332 if( addr2>addr1+1
2333 || ((pOp = sqlite3VdbeGetOp(v, addr1))!=0 && pOp->opcode==OP_Function) ){
drh25303782004-12-07 15:41:48 +00002334 iMem = pExpr->iTable = pParse->nMem++;
2335 sqlite3VdbeAddOp(v, OP_MemStore, iMem, 0);
2336 pExpr->op = TK_REGISTER;
2337 }
2338}
danielk197793758c82005-01-21 08:13:14 +00002339#endif
drh25303782004-12-07 15:41:48 +00002340
2341/*
drh268380c2004-02-25 13:47:31 +00002342** Generate code that pushes the value of every element of the given
drhf9b596e2004-05-26 16:54:42 +00002343** expression list onto the stack.
drh268380c2004-02-25 13:47:31 +00002344**
2345** Return the number of elements pushed onto the stack.
2346*/
danielk19774adee202004-05-08 08:23:19 +00002347int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00002348 Parse *pParse, /* Parsing context */
drhf9b596e2004-05-26 16:54:42 +00002349 ExprList *pList /* The expression list to be coded */
drh268380c2004-02-25 13:47:31 +00002350){
2351 struct ExprList_item *pItem;
2352 int i, n;
drh268380c2004-02-25 13:47:31 +00002353 if( pList==0 ) return 0;
drh268380c2004-02-25 13:47:31 +00002354 n = pList->nExpr;
drhc182d162005-08-14 20:47:16 +00002355 for(pItem=pList->a, i=n; i>0; i--, pItem++){
danielk19774adee202004-05-08 08:23:19 +00002356 sqlite3ExprCode(pParse, pItem->pExpr);
drh268380c2004-02-25 13:47:31 +00002357 }
drhf9b596e2004-05-26 16:54:42 +00002358 return n;
drh268380c2004-02-25 13:47:31 +00002359}
2360
2361/*
drhcce7d172000-05-31 15:34:51 +00002362** Generate code for a boolean expression such that a jump is made
2363** to the label "dest" if the expression is true but execution
2364** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00002365**
2366** If the expression evaluates to NULL (neither true nor false), then
2367** take the jump if the jumpIfNull flag is true.
drhf2bc0132004-10-04 13:19:23 +00002368**
2369** This code depends on the fact that certain token values (ex: TK_EQ)
2370** are the same as opcode values (ex: OP_Eq) that implement the corresponding
2371** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
2372** the make process cause these values to align. Assert()s in the code
2373** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00002374*/
danielk19774adee202004-05-08 08:23:19 +00002375void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00002376 Vdbe *v = pParse->pVdbe;
2377 int op = 0;
drhffe07b22005-11-03 00:41:17 +00002378 int ckOffset = pParse->ckOffset;
drhdaffd0e2001-04-11 14:28:42 +00002379 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00002380 op = pExpr->op;
2381 switch( op ){
drhcce7d172000-05-31 15:34:51 +00002382 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00002383 int d2 = sqlite3VdbeMakeLabel(v);
2384 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
2385 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
2386 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00002387 break;
2388 }
2389 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00002390 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
2391 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002392 break;
2393 }
2394 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00002395 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002396 break;
2397 }
2398 case TK_LT:
2399 case TK_LE:
2400 case TK_GT:
2401 case TK_GE:
2402 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00002403 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00002404 assert( TK_LT==OP_Lt );
2405 assert( TK_LE==OP_Le );
2406 assert( TK_GT==OP_Gt );
2407 assert( TK_GE==OP_Ge );
2408 assert( TK_EQ==OP_Eq );
2409 assert( TK_NE==OP_Ne );
danielk19774adee202004-05-08 08:23:19 +00002410 sqlite3ExprCode(pParse, pExpr->pLeft);
2411 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00002412 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002413 break;
2414 }
2415 case TK_ISNULL:
2416 case TK_NOTNULL: {
drhf2bc0132004-10-04 13:19:23 +00002417 assert( TK_ISNULL==OP_IsNull );
2418 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00002419 sqlite3ExprCode(pParse, pExpr->pLeft);
2420 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00002421 break;
2422 }
drhfef52082000-06-06 01:50:43 +00002423 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00002424 /* The expression "x BETWEEN y AND z" is implemented as:
2425 **
2426 ** 1 IF (x < y) GOTO 3
2427 ** 2 IF (x <= z) GOTO <dest>
2428 ** 3 ...
2429 */
drhf5905aa2002-05-26 20:54:33 +00002430 int addr;
drhbe5c89a2004-07-26 00:31:09 +00002431 Expr *pLeft = pExpr->pLeft;
2432 Expr *pRight = pExpr->pList->a[0].pExpr;
2433 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00002434 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00002435 sqlite3ExprCode(pParse, pRight);
2436 addr = codeCompare(pParse, pLeft, pRight, OP_Lt, 0, !jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00002437
drhbe5c89a2004-07-26 00:31:09 +00002438 pRight = pExpr->pList->a[1].pExpr;
2439 sqlite3ExprCode(pParse, pRight);
2440 codeCompare(pParse, pLeft, pRight, OP_Le, dest, jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00002441
danielk19774adee202004-05-08 08:23:19 +00002442 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
drhd654be82005-09-20 17:42:23 +00002443 sqlite3VdbeJumpHere(v, addr);
danielk19774adee202004-05-08 08:23:19 +00002444 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00002445 break;
2446 }
drhcce7d172000-05-31 15:34:51 +00002447 default: {
danielk19774adee202004-05-08 08:23:19 +00002448 sqlite3ExprCode(pParse, pExpr);
2449 sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00002450 break;
2451 }
2452 }
drhffe07b22005-11-03 00:41:17 +00002453 pParse->ckOffset = ckOffset;
drhcce7d172000-05-31 15:34:51 +00002454}
2455
2456/*
drh66b89c82000-11-28 20:47:17 +00002457** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00002458** to the label "dest" if the expression is false but execution
2459** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00002460**
2461** If the expression evaluates to NULL (neither true nor false) then
2462** jump if jumpIfNull is true or fall through if jumpIfNull is false.
drhcce7d172000-05-31 15:34:51 +00002463*/
danielk19774adee202004-05-08 08:23:19 +00002464void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00002465 Vdbe *v = pParse->pVdbe;
2466 int op = 0;
drhffe07b22005-11-03 00:41:17 +00002467 int ckOffset = pParse->ckOffset;
drhdaffd0e2001-04-11 14:28:42 +00002468 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00002469
2470 /* The value of pExpr->op and op are related as follows:
2471 **
2472 ** pExpr->op op
2473 ** --------- ----------
2474 ** TK_ISNULL OP_NotNull
2475 ** TK_NOTNULL OP_IsNull
2476 ** TK_NE OP_Eq
2477 ** TK_EQ OP_Ne
2478 ** TK_GT OP_Le
2479 ** TK_LE OP_Gt
2480 ** TK_GE OP_Lt
2481 ** TK_LT OP_Ge
2482 **
2483 ** For other values of pExpr->op, op is undefined and unused.
2484 ** The value of TK_ and OP_ constants are arranged such that we
2485 ** can compute the mapping above using the following expression.
2486 ** Assert()s verify that the computation is correct.
2487 */
2488 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
2489
2490 /* Verify correct alignment of TK_ and OP_ constants
2491 */
2492 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
2493 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
2494 assert( pExpr->op!=TK_NE || op==OP_Eq );
2495 assert( pExpr->op!=TK_EQ || op==OP_Ne );
2496 assert( pExpr->op!=TK_LT || op==OP_Ge );
2497 assert( pExpr->op!=TK_LE || op==OP_Gt );
2498 assert( pExpr->op!=TK_GT || op==OP_Le );
2499 assert( pExpr->op!=TK_GE || op==OP_Lt );
2500
drhcce7d172000-05-31 15:34:51 +00002501 switch( pExpr->op ){
2502 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00002503 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
2504 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002505 break;
2506 }
2507 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00002508 int d2 = sqlite3VdbeMakeLabel(v);
2509 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
2510 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
2511 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00002512 break;
2513 }
2514 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00002515 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002516 break;
2517 }
2518 case TK_LT:
2519 case TK_LE:
2520 case TK_GT:
2521 case TK_GE:
2522 case TK_NE:
2523 case TK_EQ: {
danielk19774adee202004-05-08 08:23:19 +00002524 sqlite3ExprCode(pParse, pExpr->pLeft);
2525 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00002526 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002527 break;
2528 }
drhcce7d172000-05-31 15:34:51 +00002529 case TK_ISNULL:
2530 case TK_NOTNULL: {
danielk19774adee202004-05-08 08:23:19 +00002531 sqlite3ExprCode(pParse, pExpr->pLeft);
2532 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00002533 break;
2534 }
drhfef52082000-06-06 01:50:43 +00002535 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00002536 /* The expression is "x BETWEEN y AND z". It is implemented as:
2537 **
2538 ** 1 IF (x >= y) GOTO 3
2539 ** 2 GOTO <dest>
2540 ** 3 IF (x > z) GOTO <dest>
2541 */
drhfef52082000-06-06 01:50:43 +00002542 int addr;
drhbe5c89a2004-07-26 00:31:09 +00002543 Expr *pLeft = pExpr->pLeft;
2544 Expr *pRight = pExpr->pList->a[0].pExpr;
2545 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00002546 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00002547 sqlite3ExprCode(pParse, pRight);
danielk19774adee202004-05-08 08:23:19 +00002548 addr = sqlite3VdbeCurrentAddr(v);
drhbe5c89a2004-07-26 00:31:09 +00002549 codeCompare(pParse, pLeft, pRight, OP_Ge, addr+3, !jumpIfNull);
2550
danielk19774adee202004-05-08 08:23:19 +00002551 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
2552 sqlite3VdbeAddOp(v, OP_Goto, 0, dest);
drhbe5c89a2004-07-26 00:31:09 +00002553 pRight = pExpr->pList->a[1].pExpr;
2554 sqlite3ExprCode(pParse, pRight);
2555 codeCompare(pParse, pLeft, pRight, OP_Gt, dest, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00002556 break;
2557 }
drhcce7d172000-05-31 15:34:51 +00002558 default: {
danielk19774adee202004-05-08 08:23:19 +00002559 sqlite3ExprCode(pParse, pExpr);
2560 sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00002561 break;
2562 }
2563 }
drhffe07b22005-11-03 00:41:17 +00002564 pParse->ckOffset = ckOffset;
drhcce7d172000-05-31 15:34:51 +00002565}
drh22827922000-06-06 17:27:05 +00002566
2567/*
2568** Do a deep comparison of two expression trees. Return TRUE (non-zero)
2569** if they are identical and return FALSE if they differ in any way.
drhd40aab02007-02-24 15:29:03 +00002570**
2571** Sometimes this routine will return FALSE even if the two expressions
2572** really are equivalent. If we cannot prove that the expressions are
2573** identical, we return FALSE just to be safe. So if this routine
2574** returns false, then you do not really know for certain if the two
2575** expressions are the same. But if you get a TRUE return, then you
2576** can be sure the expressions are the same. In the places where
2577** this routine is used, it does not hurt to get an extra FALSE - that
2578** just might result in some slightly slower code. But returning
2579** an incorrect TRUE could lead to a malfunction.
drh22827922000-06-06 17:27:05 +00002580*/
danielk19774adee202004-05-08 08:23:19 +00002581int sqlite3ExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00002582 int i;
danielk19774b202ae2006-01-23 05:50:58 +00002583 if( pA==0||pB==0 ){
2584 return pB==pA;
drh22827922000-06-06 17:27:05 +00002585 }
2586 if( pA->op!=pB->op ) return 0;
drhfd357972005-09-09 01:33:19 +00002587 if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 0;
danielk19774adee202004-05-08 08:23:19 +00002588 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
2589 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00002590 if( pA->pList ){
2591 if( pB->pList==0 ) return 0;
2592 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
2593 for(i=0; i<pA->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00002594 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00002595 return 0;
2596 }
2597 }
2598 }else if( pB->pList ){
2599 return 0;
2600 }
2601 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00002602 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drhdd735212007-02-24 13:53:05 +00002603 if( pA->op!=TK_COLUMN && pA->token.z ){
drh22827922000-06-06 17:27:05 +00002604 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00002605 if( pB->token.n!=pA->token.n ) return 0;
drh2646da72005-12-09 20:02:05 +00002606 if( sqlite3StrNICmp((char*)pA->token.z,(char*)pB->token.z,pB->token.n)!=0 ){
2607 return 0;
2608 }
drh22827922000-06-06 17:27:05 +00002609 }
2610 return 1;
2611}
2612
drh13449892005-09-07 21:22:45 +00002613
drh22827922000-06-06 17:27:05 +00002614/*
drh13449892005-09-07 21:22:45 +00002615** Add a new element to the pAggInfo->aCol[] array. Return the index of
2616** the new element. Return a negative number if malloc fails.
drh22827922000-06-06 17:27:05 +00002617*/
drh17435752007-08-16 04:30:38 +00002618static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){
drh13449892005-09-07 21:22:45 +00002619 int i;
drhcf643722007-03-27 13:36:37 +00002620 pInfo->aCol = sqlite3ArrayAllocate(
drh17435752007-08-16 04:30:38 +00002621 db,
drhcf643722007-03-27 13:36:37 +00002622 pInfo->aCol,
2623 sizeof(pInfo->aCol[0]),
2624 3,
2625 &pInfo->nColumn,
2626 &pInfo->nColumnAlloc,
2627 &i
2628 );
drh13449892005-09-07 21:22:45 +00002629 return i;
2630}
2631
2632/*
2633** Add a new element to the pAggInfo->aFunc[] array. Return the index of
2634** the new element. Return a negative number if malloc fails.
2635*/
drh17435752007-08-16 04:30:38 +00002636static int addAggInfoFunc(sqlite3 *db, AggInfo *pInfo){
drh13449892005-09-07 21:22:45 +00002637 int i;
drhcf643722007-03-27 13:36:37 +00002638 pInfo->aFunc = sqlite3ArrayAllocate(
drh17435752007-08-16 04:30:38 +00002639 db,
drhcf643722007-03-27 13:36:37 +00002640 pInfo->aFunc,
2641 sizeof(pInfo->aFunc[0]),
2642 3,
2643 &pInfo->nFunc,
2644 &pInfo->nFuncAlloc,
2645 &i
2646 );
drh13449892005-09-07 21:22:45 +00002647 return i;
2648}
drh22827922000-06-06 17:27:05 +00002649
2650/*
drh626a8792005-01-17 22:08:19 +00002651** This is an xFunc for walkExprTree() used to implement
2652** sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates
2653** for additional information.
drh22827922000-06-06 17:27:05 +00002654**
drh626a8792005-01-17 22:08:19 +00002655** This routine analyzes the aggregate function at pExpr.
drh22827922000-06-06 17:27:05 +00002656*/
drh626a8792005-01-17 22:08:19 +00002657static int analyzeAggregate(void *pArg, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00002658 int i;
danielk1977a58fdfb2005-02-08 07:50:40 +00002659 NameContext *pNC = (NameContext *)pArg;
2660 Parse *pParse = pNC->pParse;
2661 SrcList *pSrcList = pNC->pSrcList;
drh13449892005-09-07 21:22:45 +00002662 AggInfo *pAggInfo = pNC->pAggInfo;
drh22827922000-06-06 17:27:05 +00002663
drh22827922000-06-06 17:27:05 +00002664 switch( pExpr->op ){
drh89c69d02007-01-04 01:20:28 +00002665 case TK_AGG_COLUMN:
drh967e8b72000-06-21 13:59:10 +00002666 case TK_COLUMN: {
drh13449892005-09-07 21:22:45 +00002667 /* Check to see if the column is in one of the tables in the FROM
2668 ** clause of the aggregate query */
2669 if( pSrcList ){
2670 struct SrcList_item *pItem = pSrcList->a;
2671 for(i=0; i<pSrcList->nSrc; i++, pItem++){
2672 struct AggInfo_col *pCol;
2673 if( pExpr->iTable==pItem->iCursor ){
2674 /* If we reach this point, it means that pExpr refers to a table
2675 ** that is in the FROM clause of the aggregate query.
2676 **
2677 ** Make an entry for the column in pAggInfo->aCol[] if there
2678 ** is not an entry there already.
2679 */
drh7f906d62007-03-12 23:48:52 +00002680 int k;
drh13449892005-09-07 21:22:45 +00002681 pCol = pAggInfo->aCol;
drh7f906d62007-03-12 23:48:52 +00002682 for(k=0; k<pAggInfo->nColumn; k++, pCol++){
drh13449892005-09-07 21:22:45 +00002683 if( pCol->iTable==pExpr->iTable &&
2684 pCol->iColumn==pExpr->iColumn ){
2685 break;
2686 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002687 }
danielk19771e536952007-08-16 10:09:01 +00002688 if( (k>=pAggInfo->nColumn)
2689 && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0
2690 ){
drh7f906d62007-03-12 23:48:52 +00002691 pCol = &pAggInfo->aCol[k];
danielk19770817d0d2007-02-14 09:19:36 +00002692 pCol->pTab = pExpr->pTab;
drh13449892005-09-07 21:22:45 +00002693 pCol->iTable = pExpr->iTable;
2694 pCol->iColumn = pExpr->iColumn;
2695 pCol->iMem = pParse->nMem++;
2696 pCol->iSorterColumn = -1;
drh5774b802005-09-07 22:48:16 +00002697 pCol->pExpr = pExpr;
drh13449892005-09-07 21:22:45 +00002698 if( pAggInfo->pGroupBy ){
2699 int j, n;
2700 ExprList *pGB = pAggInfo->pGroupBy;
2701 struct ExprList_item *pTerm = pGB->a;
2702 n = pGB->nExpr;
2703 for(j=0; j<n; j++, pTerm++){
2704 Expr *pE = pTerm->pExpr;
2705 if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable &&
2706 pE->iColumn==pExpr->iColumn ){
2707 pCol->iSorterColumn = j;
2708 break;
2709 }
2710 }
2711 }
2712 if( pCol->iSorterColumn<0 ){
2713 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
2714 }
2715 }
2716 /* There is now an entry for pExpr in pAggInfo->aCol[] (either
2717 ** because it was there before or because we just created it).
2718 ** Convert the pExpr to be a TK_AGG_COLUMN referring to that
2719 ** pAggInfo->aCol[] entry.
2720 */
2721 pExpr->pAggInfo = pAggInfo;
2722 pExpr->op = TK_AGG_COLUMN;
drh7f906d62007-03-12 23:48:52 +00002723 pExpr->iAgg = k;
drh13449892005-09-07 21:22:45 +00002724 break;
2725 } /* endif pExpr->iTable==pItem->iCursor */
2726 } /* end loop over pSrcList */
drh22827922000-06-06 17:27:05 +00002727 }
drh626a8792005-01-17 22:08:19 +00002728 return 1;
drh22827922000-06-06 17:27:05 +00002729 }
2730 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00002731 /* The pNC->nDepth==0 test causes aggregate functions in subqueries
2732 ** to be ignored */
danielk1977a58fdfb2005-02-08 07:50:40 +00002733 if( pNC->nDepth==0 ){
drh13449892005-09-07 21:22:45 +00002734 /* Check to see if pExpr is a duplicate of another aggregate
2735 ** function that is already in the pAggInfo structure
2736 */
2737 struct AggInfo_func *pItem = pAggInfo->aFunc;
2738 for(i=0; i<pAggInfo->nFunc; i++, pItem++){
2739 if( sqlite3ExprCompare(pItem->pExpr, pExpr) ){
danielk1977a58fdfb2005-02-08 07:50:40 +00002740 break;
2741 }
drh22827922000-06-06 17:27:05 +00002742 }
drh13449892005-09-07 21:22:45 +00002743 if( i>=pAggInfo->nFunc ){
2744 /* pExpr is original. Make a new entry in pAggInfo->aFunc[]
2745 */
danielk197714db2662006-01-09 16:12:04 +00002746 u8 enc = ENC(pParse->db);
danielk19771e536952007-08-16 10:09:01 +00002747 i = addAggInfoFunc(pParse->db, pAggInfo);
drh13449892005-09-07 21:22:45 +00002748 if( i>=0 ){
2749 pItem = &pAggInfo->aFunc[i];
2750 pItem->pExpr = pExpr;
2751 pItem->iMem = pParse->nMem++;
2752 pItem->pFunc = sqlite3FindFunction(pParse->db,
drh2646da72005-12-09 20:02:05 +00002753 (char*)pExpr->token.z, pExpr->token.n,
drh13449892005-09-07 21:22:45 +00002754 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
drhfd357972005-09-09 01:33:19 +00002755 if( pExpr->flags & EP_Distinct ){
2756 pItem->iDistinct = pParse->nTab++;
2757 }else{
2758 pItem->iDistinct = -1;
2759 }
drh13449892005-09-07 21:22:45 +00002760 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002761 }
drh13449892005-09-07 21:22:45 +00002762 /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
2763 */
danielk1977a58fdfb2005-02-08 07:50:40 +00002764 pExpr->iAgg = i;
drh13449892005-09-07 21:22:45 +00002765 pExpr->pAggInfo = pAggInfo;
danielk1977a58fdfb2005-02-08 07:50:40 +00002766 return 1;
drh22827922000-06-06 17:27:05 +00002767 }
drh22827922000-06-06 17:27:05 +00002768 }
2769 }
drh13449892005-09-07 21:22:45 +00002770
2771 /* Recursively walk subqueries looking for TK_COLUMN nodes that need
2772 ** to be changed to TK_AGG_COLUMN. But increment nDepth so that
2773 ** TK_AGG_FUNCTION nodes in subqueries will be unchanged.
2774 */
danielk1977a58fdfb2005-02-08 07:50:40 +00002775 if( pExpr->pSelect ){
2776 pNC->nDepth++;
2777 walkSelectExpr(pExpr->pSelect, analyzeAggregate, pNC);
2778 pNC->nDepth--;
2779 }
drh626a8792005-01-17 22:08:19 +00002780 return 0;
2781}
2782
2783/*
2784** Analyze the given expression looking for aggregate functions and
2785** for variables that need to be added to the pParse->aAgg[] array.
2786** Make additional entries to the pParse->aAgg[] array as necessary.
2787**
2788** This routine should only be called after the expression has been
2789** analyzed by sqlite3ExprResolveNames().
2790**
2791** If errors are seen, leave an error message in zErrMsg and return
2792** the number of errors.
2793*/
danielk1977a58fdfb2005-02-08 07:50:40 +00002794int sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
2795 int nErr = pNC->pParse->nErr;
2796 walkExprTree(pExpr, analyzeAggregate, pNC);
2797 return pNC->pParse->nErr - nErr;
drh22827922000-06-06 17:27:05 +00002798}
drh5d9a4af2005-08-30 00:54:01 +00002799
2800/*
2801** Call sqlite3ExprAnalyzeAggregates() for every expression in an
2802** expression list. Return the number of errors.
2803**
2804** If an error is found, the analysis is cut short.
2805*/
2806int sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){
2807 struct ExprList_item *pItem;
2808 int i;
2809 int nErr = 0;
2810 if( pList ){
2811 for(pItem=pList->a, i=0; nErr==0 && i<pList->nExpr; i++, pItem++){
2812 nErr += sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
2813 }
2814 }
2815 return nErr;
2816}