blob: 9a44c122d31cc8ef753c1ab1dd0d6c0c6ad25112 [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**
danielk1977c9cf6e32007-06-25 16:29:33 +000015** $Id: expr.c,v 1.300 2007/06/25 16:29:34 danielk1977 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){
57 CollSeq *pColl;
58 if( pExpr==0 ) return 0;
59 pColl = sqlite3LocateCollSeq(pParse, (char*)pName->z, pName->n);
60 if( pColl ){
61 pExpr->pColl = pColl;
62 pExpr->flags |= EP_ExpCollate;
63 }
64 return pExpr;
65}
66
67/*
danielk19770202b292004-06-09 09:55:16 +000068** Return the default collation sequence for the expression pExpr. If
69** there is no default collation type, return 0.
70*/
danielk19777cedc8d2004-06-10 10:50:08 +000071CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
72 CollSeq *pColl = 0;
danielk19770202b292004-06-09 09:55:16 +000073 if( pExpr ){
drh7e09fe02007-06-20 16:13:23 +000074 int op;
danielk19777cedc8d2004-06-10 10:50:08 +000075 pColl = pExpr->pColl;
drh7e09fe02007-06-20 16:13:23 +000076 op = pExpr->op;
77 if( (op==TK_CAST || op==TK_UPLUS) && !pColl ){
danielk19777cedc8d2004-06-10 10:50:08 +000078 return sqlite3ExprCollSeq(pParse, pExpr->pLeft);
danielk19770202b292004-06-09 09:55:16 +000079 }
80 }
danielk19777cedc8d2004-06-10 10:50:08 +000081 if( sqlite3CheckCollSeq(pParse, pColl) ){
82 pColl = 0;
83 }
84 return pColl;
danielk19770202b292004-06-09 09:55:16 +000085}
86
87/*
drh626a8792005-01-17 22:08:19 +000088** pExpr is an operand of a comparison operator. aff2 is the
89** type affinity of the other operand. This routine returns the
drh53db1452004-05-20 13:54:53 +000090** type affinity that should be used for the comparison operator.
91*/
danielk1977e014a832004-05-17 10:48:57 +000092char sqlite3CompareAffinity(Expr *pExpr, char aff2){
danielk1977bf3b7212004-05-18 10:06:24 +000093 char aff1 = sqlite3ExprAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +000094 if( aff1 && aff2 ){
drh8df447f2005-11-01 15:48:24 +000095 /* Both sides of the comparison are columns. If one has numeric
96 ** affinity, use that. Otherwise use no affinity.
danielk1977e014a832004-05-17 10:48:57 +000097 */
drh8a512562005-11-14 22:29:05 +000098 if( sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2) ){
danielk1977e014a832004-05-17 10:48:57 +000099 return SQLITE_AFF_NUMERIC;
100 }else{
101 return SQLITE_AFF_NONE;
102 }
103 }else if( !aff1 && !aff2 ){
drh5f6a87b2004-07-19 00:39:45 +0000104 /* Neither side of the comparison is a column. Compare the
105 ** results directly.
danielk1977e014a832004-05-17 10:48:57 +0000106 */
drh5f6a87b2004-07-19 00:39:45 +0000107 return SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +0000108 }else{
109 /* One side is a column, the other is not. Use the columns affinity. */
drhfe05af82005-07-21 03:14:59 +0000110 assert( aff1==0 || aff2==0 );
danielk1977e014a832004-05-17 10:48:57 +0000111 return (aff1 + aff2);
112 }
113}
114
drh53db1452004-05-20 13:54:53 +0000115/*
116** pExpr is a comparison operator. Return the type affinity that should
117** be applied to both operands prior to doing the comparison.
118*/
danielk1977e014a832004-05-17 10:48:57 +0000119static char comparisonAffinity(Expr *pExpr){
120 char aff;
121 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
122 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
123 pExpr->op==TK_NE );
124 assert( pExpr->pLeft );
danielk1977bf3b7212004-05-18 10:06:24 +0000125 aff = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +0000126 if( pExpr->pRight ){
127 aff = sqlite3CompareAffinity(pExpr->pRight, aff);
128 }
129 else if( pExpr->pSelect ){
130 aff = sqlite3CompareAffinity(pExpr->pSelect->pEList->a[0].pExpr, aff);
131 }
132 else if( !aff ){
drhde087bd2007-02-23 03:00:44 +0000133 aff = SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +0000134 }
135 return aff;
136}
137
138/*
139** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
140** idx_affinity is the affinity of an indexed column. Return true
141** if the index with affinity idx_affinity may be used to implement
142** the comparison in pExpr.
143*/
144int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){
145 char aff = comparisonAffinity(pExpr);
drh8a512562005-11-14 22:29:05 +0000146 switch( aff ){
147 case SQLITE_AFF_NONE:
148 return 1;
149 case SQLITE_AFF_TEXT:
150 return idx_affinity==SQLITE_AFF_TEXT;
151 default:
152 return sqlite3IsNumericAffinity(idx_affinity);
153 }
danielk1977e014a832004-05-17 10:48:57 +0000154}
155
danielk1977a37cdde2004-05-16 11:15:36 +0000156/*
157** Return the P1 value that should be used for a binary comparison
158** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
159** If jumpIfNull is true, then set the low byte of the returned
160** P1 value to tell the opcode to jump if either expression
161** evaluates to NULL.
162*/
danielk1977e014a832004-05-17 10:48:57 +0000163static int binaryCompareP1(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){
danielk1977bf3b7212004-05-18 10:06:24 +0000164 char aff = sqlite3ExprAffinity(pExpr2);
drhf0863fe2005-06-12 21:35:51 +0000165 return ((int)sqlite3CompareAffinity(pExpr1, aff))+(jumpIfNull?0x100:0);
danielk1977a37cdde2004-05-16 11:15:36 +0000166}
167
drha2e00042002-01-22 03:13:42 +0000168/*
danielk19770202b292004-06-09 09:55:16 +0000169** Return a pointer to the collation sequence that should be used by
170** a binary comparison operator comparing pLeft and pRight.
171**
172** If the left hand expression has a collating sequence type, then it is
173** used. Otherwise the collation sequence for the right hand expression
174** is used, or the default (BINARY) if neither expression has a collating
175** type.
danielk1977bcbb04e2007-05-29 12:11:29 +0000176**
177** Argument pRight (but not pLeft) may be a null pointer. In this case,
178** it is not considered.
danielk19770202b292004-06-09 09:55:16 +0000179*/
danielk1977bcbb04e2007-05-29 12:11:29 +0000180CollSeq* sqlite3BinaryCompareCollSeq(
181 Parse *pParse,
182 Expr *pLeft,
183 Expr *pRight
184){
drhec41dda2007-02-07 13:09:45 +0000185 CollSeq *pColl;
186 assert( pLeft );
drhec41dda2007-02-07 13:09:45 +0000187 if( pLeft->flags & EP_ExpCollate ){
188 assert( pLeft->pColl );
189 pColl = pLeft->pColl;
danielk1977bcbb04e2007-05-29 12:11:29 +0000190 }else if( pRight && pRight->flags & EP_ExpCollate ){
drhec41dda2007-02-07 13:09:45 +0000191 assert( pRight->pColl );
192 pColl = pRight->pColl;
193 }else{
194 pColl = sqlite3ExprCollSeq(pParse, pLeft);
195 if( !pColl ){
196 pColl = sqlite3ExprCollSeq(pParse, pRight);
197 }
danielk19770202b292004-06-09 09:55:16 +0000198 }
199 return pColl;
200}
201
202/*
drhbe5c89a2004-07-26 00:31:09 +0000203** Generate code for a comparison operator.
204*/
205static int codeCompare(
206 Parse *pParse, /* The parsing (and code generating) context */
207 Expr *pLeft, /* The left operand */
208 Expr *pRight, /* The right operand */
209 int opcode, /* The comparison opcode */
210 int dest, /* Jump here if true. */
211 int jumpIfNull /* If true, jump if either operand is NULL */
212){
213 int p1 = binaryCompareP1(pLeft, pRight, jumpIfNull);
danielk1977bcbb04e2007-05-29 12:11:29 +0000214 CollSeq *p3 = sqlite3BinaryCompareCollSeq(pParse, pLeft, pRight);
drh94a11212004-09-25 13:12:14 +0000215 return sqlite3VdbeOp3(pParse->pVdbe, opcode, p1, dest, (void*)p3, P3_COLLSEQ);
drhbe5c89a2004-07-26 00:31:09 +0000216}
217
218/*
drha76b5df2002-02-23 02:32:10 +0000219** Construct a new expression node and return a pointer to it. Memory
220** for this node is obtained from sqliteMalloc(). The calling function
221** is responsible for making sure the node eventually gets freed.
222*/
drhe4e72072004-11-23 01:47:30 +0000223Expr *sqlite3Expr(int op, Expr *pLeft, Expr *pRight, const Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000224 Expr *pNew;
225 pNew = sqliteMalloc( sizeof(Expr) );
226 if( pNew==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000227 /* When malloc fails, delete pLeft and pRight. Expressions passed to
228 ** this function must always be allocated with sqlite3Expr() for this
229 ** reason.
230 */
231 sqlite3ExprDelete(pLeft);
232 sqlite3ExprDelete(pRight);
drha76b5df2002-02-23 02:32:10 +0000233 return 0;
234 }
235 pNew->op = op;
236 pNew->pLeft = pLeft;
237 pNew->pRight = pRight;
danielk1977a58fdfb2005-02-08 07:50:40 +0000238 pNew->iAgg = -1;
drha76b5df2002-02-23 02:32:10 +0000239 if( pToken ){
drh4b59ab52002-08-24 18:24:51 +0000240 assert( pToken->dyn==0 );
drh145716b2004-09-24 12:24:06 +0000241 pNew->span = pNew->token = *pToken;
drha34001c2007-02-02 12:44:37 +0000242 }else if( pLeft ){
243 if( pRight ){
244 sqlite3ExprSpan(pNew, &pLeft->span, &pRight->span);
drh5ffb3ac2007-04-18 17:07:57 +0000245 if( pRight->flags & EP_ExpCollate ){
drha34001c2007-02-02 12:44:37 +0000246 pNew->flags |= EP_ExpCollate;
247 pNew->pColl = pRight->pColl;
248 }
249 }
drh5ffb3ac2007-04-18 17:07:57 +0000250 if( pLeft->flags & EP_ExpCollate ){
drha34001c2007-02-02 12:44:37 +0000251 pNew->flags |= EP_ExpCollate;
252 pNew->pColl = pLeft->pColl;
253 }
drha76b5df2002-02-23 02:32:10 +0000254 }
danielk1977fc976062007-05-10 10:46:56 +0000255
256 sqlite3ExprSetHeight(pNew);
drha76b5df2002-02-23 02:32:10 +0000257 return pNew;
258}
259
260/*
drh206f3d92006-07-11 13:15:08 +0000261** Works like sqlite3Expr() but frees its pLeft and pRight arguments
262** if it fails due to a malloc problem.
263*/
264Expr *sqlite3ExprOrFree(int op, Expr *pLeft, Expr *pRight, const Token *pToken){
265 Expr *pNew = sqlite3Expr(op, pLeft, pRight, pToken);
266 if( pNew==0 ){
267 sqlite3ExprDelete(pLeft);
268 sqlite3ExprDelete(pRight);
269 }
270 return pNew;
271}
272
273/*
drh4e0cff62004-11-05 05:10:28 +0000274** When doing a nested parse, you can include terms in an expression
275** that look like this: #0 #1 #2 ... These terms refer to elements
drh288d37f2005-06-22 08:48:06 +0000276** on the stack. "#0" means the top of the stack.
277** "#1" means the next down on the stack. And so forth.
drh4e0cff62004-11-05 05:10:28 +0000278**
279** This routine is called by the parser to deal with on of those terms.
280** It immediately generates code to store the value in a memory location.
281** The returns an expression that will code to extract the value from
282** that memory location as needed.
283*/
284Expr *sqlite3RegisterExpr(Parse *pParse, Token *pToken){
285 Vdbe *v = pParse->pVdbe;
286 Expr *p;
287 int depth;
drh4e0cff62004-11-05 05:10:28 +0000288 if( pParse->nested==0 ){
289 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", pToken);
drh4e05c832007-05-11 01:44:50 +0000290 return sqlite3Expr(TK_NULL, 0, 0, 0);
drh4e0cff62004-11-05 05:10:28 +0000291 }
drhbb7ac002005-08-12 22:58:53 +0000292 if( v==0 ) return 0;
drh4e0cff62004-11-05 05:10:28 +0000293 p = sqlite3Expr(TK_REGISTER, 0, 0, pToken);
drh73c42a12004-11-20 18:13:10 +0000294 if( p==0 ){
295 return 0; /* Malloc failed */
296 }
drh2646da72005-12-09 20:02:05 +0000297 depth = atoi((char*)&pToken->z[1]);
drh288d37f2005-06-22 08:48:06 +0000298 p->iTable = pParse->nMem++;
299 sqlite3VdbeAddOp(v, OP_Dup, depth, 0);
300 sqlite3VdbeAddOp(v, OP_MemStore, p->iTable, 1);
drh4e0cff62004-11-05 05:10:28 +0000301 return p;
302}
303
304/*
drh91bb0ee2004-09-01 03:06:34 +0000305** Join two expressions using an AND operator. If either expression is
306** NULL, then just return the other expression.
307*/
308Expr *sqlite3ExprAnd(Expr *pLeft, Expr *pRight){
309 if( pLeft==0 ){
310 return pRight;
311 }else if( pRight==0 ){
312 return pLeft;
313 }else{
314 return sqlite3Expr(TK_AND, pLeft, pRight, 0);
315 }
316}
317
318/*
drh6977fea2002-10-22 23:38:04 +0000319** Set the Expr.span field of the given expression to span all
drha76b5df2002-02-23 02:32:10 +0000320** text between the two given tokens.
321*/
danielk19774adee202004-05-08 08:23:19 +0000322void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
drh4efc4752004-01-16 15:55:37 +0000323 assert( pRight!=0 );
324 assert( pLeft!=0 );
danielk19779e128002006-01-18 16:51:35 +0000325 if( !sqlite3MallocFailed() && pRight->z && pLeft->z ){
drhad6d9462004-09-19 02:15:24 +0000326 assert( pLeft->dyn==0 || pLeft->z[pLeft->n]==0 );
drh145716b2004-09-24 12:24:06 +0000327 if( pLeft->dyn==0 && pRight->dyn==0 ){
drh6977fea2002-10-22 23:38:04 +0000328 pExpr->span.z = pLeft->z;
drh97903fe2005-05-24 20:19:57 +0000329 pExpr->span.n = pRight->n + (pRight->z - pLeft->z);
drh4b59ab52002-08-24 18:24:51 +0000330 }else{
drh6977fea2002-10-22 23:38:04 +0000331 pExpr->span.z = 0;
drh4b59ab52002-08-24 18:24:51 +0000332 }
drha76b5df2002-02-23 02:32:10 +0000333 }
334}
335
336/*
337** Construct a new expression node for a function with multiple
338** arguments.
339*/
danielk19774adee202004-05-08 08:23:19 +0000340Expr *sqlite3ExprFunction(ExprList *pList, Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000341 Expr *pNew;
danielk19774b202ae2006-01-23 05:50:58 +0000342 assert( pToken );
drha76b5df2002-02-23 02:32:10 +0000343 pNew = sqliteMalloc( sizeof(Expr) );
344 if( pNew==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000345 sqlite3ExprListDelete(pList); /* Avoid leaking memory when malloc fails */
drha76b5df2002-02-23 02:32:10 +0000346 return 0;
347 }
348 pNew->op = TK_FUNCTION;
349 pNew->pList = pList;
danielk19774b202ae2006-01-23 05:50:58 +0000350 assert( pToken->dyn==0 );
351 pNew->token = *pToken;
drh6977fea2002-10-22 23:38:04 +0000352 pNew->span = pNew->token;
danielk1977fc976062007-05-10 10:46:56 +0000353
354 sqlite3ExprSetHeight(pNew);
drha76b5df2002-02-23 02:32:10 +0000355 return pNew;
356}
357
358/*
drhfa6bc002004-09-07 16:19:52 +0000359** Assign a variable number to an expression that encodes a wildcard
360** in the original SQL statement.
361**
362** Wildcards consisting of a single "?" are assigned the next sequential
363** variable number.
364**
365** Wildcards of the form "?nnn" are assigned the number "nnn". We make
366** sure "nnn" is not too be to avoid a denial of service attack when
367** the SQL statement comes from an external source.
368**
369** Wildcards of the form ":aaa" or "$aaa" are assigned the same number
370** as the previous instance of the same wildcard. Or if this is the first
371** instance of the wildcard, the next sequenial variable number is
372** assigned.
373*/
374void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){
375 Token *pToken;
376 if( pExpr==0 ) return;
377 pToken = &pExpr->token;
378 assert( pToken->n>=1 );
379 assert( pToken->z!=0 );
380 assert( pToken->z[0]!=0 );
381 if( pToken->n==1 ){
382 /* Wildcard of the form "?". Assign the next variable number */
383 pExpr->iTable = ++pParse->nVar;
384 }else if( pToken->z[0]=='?' ){
385 /* Wildcard of the form "?nnn". Convert "nnn" to an integer and
386 ** use it as the variable number */
387 int i;
drh2646da72005-12-09 20:02:05 +0000388 pExpr->iTable = i = atoi((char*)&pToken->z[1]);
drhfa6bc002004-09-07 16:19:52 +0000389 if( i<1 || i>SQLITE_MAX_VARIABLE_NUMBER ){
390 sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
391 SQLITE_MAX_VARIABLE_NUMBER);
392 }
393 if( i>pParse->nVar ){
394 pParse->nVar = i;
395 }
396 }else{
397 /* Wildcards of the form ":aaa" or "$aaa". Reuse the same variable
398 ** number as the prior appearance of the same name, or if the name
399 ** has never appeared before, reuse the same variable number
400 */
401 int i, n;
402 n = pToken->n;
403 for(i=0; i<pParse->nVarExpr; i++){
404 Expr *pE;
405 if( (pE = pParse->apVarExpr[i])!=0
406 && pE->token.n==n
407 && memcmp(pE->token.z, pToken->z, n)==0 ){
408 pExpr->iTable = pE->iTable;
409 break;
410 }
411 }
412 if( i>=pParse->nVarExpr ){
413 pExpr->iTable = ++pParse->nVar;
414 if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){
415 pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10;
drhcf643722007-03-27 13:36:37 +0000416 pParse->apVarExpr = sqliteReallocOrFree(pParse->apVarExpr,
drhfa6bc002004-09-07 16:19:52 +0000417 pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0]) );
418 }
danielk19779e128002006-01-18 16:51:35 +0000419 if( !sqlite3MallocFailed() ){
drhfa6bc002004-09-07 16:19:52 +0000420 assert( pParse->apVarExpr!=0 );
421 pParse->apVarExpr[pParse->nVarExpr++] = pExpr;
422 }
423 }
424 }
danielk1977832b2662007-05-09 11:37:22 +0000425 if( !pParse->nErr && pParse->nVar>SQLITE_MAX_VARIABLE_NUMBER ){
426 sqlite3ErrorMsg(pParse, "too many SQL variables");
427 }
drhfa6bc002004-09-07 16:19:52 +0000428}
429
430/*
drha2e00042002-01-22 03:13:42 +0000431** Recursively delete an expression tree.
432*/
danielk19774adee202004-05-08 08:23:19 +0000433void sqlite3ExprDelete(Expr *p){
drha2e00042002-01-22 03:13:42 +0000434 if( p==0 ) return;
drh4efc4752004-01-16 15:55:37 +0000435 if( p->span.dyn ) sqliteFree((char*)p->span.z);
436 if( p->token.dyn ) sqliteFree((char*)p->token.z);
danielk19774adee202004-05-08 08:23:19 +0000437 sqlite3ExprDelete(p->pLeft);
438 sqlite3ExprDelete(p->pRight);
439 sqlite3ExprListDelete(p->pList);
440 sqlite3SelectDelete(p->pSelect);
drha2e00042002-01-22 03:13:42 +0000441 sqliteFree(p);
442}
443
drhd2687b72005-08-12 22:56:09 +0000444/*
445** The Expr.token field might be a string literal that is quoted.
446** If so, remove the quotation marks.
447*/
448void sqlite3DequoteExpr(Expr *p){
449 if( ExprHasAnyProperty(p, EP_Dequoted) ){
450 return;
451 }
452 ExprSetProperty(p, EP_Dequoted);
453 if( p->token.dyn==0 ){
drhd2687b72005-08-12 22:56:09 +0000454 sqlite3TokenCopy(&p->token, &p->token);
455 }
456 sqlite3Dequote((char*)p->token.z);
457}
458
drha76b5df2002-02-23 02:32:10 +0000459
460/*
drhff78bd22002-02-27 01:47:11 +0000461** The following group of routines make deep copies of expressions,
462** expression lists, ID lists, and select statements. The copies can
463** be deleted (by being passed to their respective ...Delete() routines)
464** without effecting the originals.
465**
danielk19774adee202004-05-08 08:23:19 +0000466** The expression list, ID, and source lists return by sqlite3ExprListDup(),
467** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
drhad3cab52002-05-24 02:04:32 +0000468** by subsequent calls to sqlite*ListAppend() routines.
drhff78bd22002-02-27 01:47:11 +0000469**
drhad3cab52002-05-24 02:04:32 +0000470** Any tables that the SrcList might point to are not duplicated.
drhff78bd22002-02-27 01:47:11 +0000471*/
danielk19774adee202004-05-08 08:23:19 +0000472Expr *sqlite3ExprDup(Expr *p){
drhff78bd22002-02-27 01:47:11 +0000473 Expr *pNew;
474 if( p==0 ) return 0;
drhfcb78a42003-01-18 20:11:05 +0000475 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000476 if( pNew==0 ) return 0;
drh3b167c72002-06-28 12:18:47 +0000477 memcpy(pNew, p, sizeof(*pNew));
drh6977fea2002-10-22 23:38:04 +0000478 if( p->token.z!=0 ){
drh2646da72005-12-09 20:02:05 +0000479 pNew->token.z = (u8*)sqliteStrNDup((char*)p->token.z, p->token.n);
drh4b59ab52002-08-24 18:24:51 +0000480 pNew->token.dyn = 1;
481 }else{
drh4efc4752004-01-16 15:55:37 +0000482 assert( pNew->token.z==0 );
drh4b59ab52002-08-24 18:24:51 +0000483 }
drh6977fea2002-10-22 23:38:04 +0000484 pNew->span.z = 0;
danielk19774adee202004-05-08 08:23:19 +0000485 pNew->pLeft = sqlite3ExprDup(p->pLeft);
486 pNew->pRight = sqlite3ExprDup(p->pRight);
487 pNew->pList = sqlite3ExprListDup(p->pList);
488 pNew->pSelect = sqlite3SelectDup(p->pSelect);
drhff78bd22002-02-27 01:47:11 +0000489 return pNew;
490}
danielk19774adee202004-05-08 08:23:19 +0000491void sqlite3TokenCopy(Token *pTo, Token *pFrom){
drh4b59ab52002-08-24 18:24:51 +0000492 if( pTo->dyn ) sqliteFree((char*)pTo->z);
drh4b59ab52002-08-24 18:24:51 +0000493 if( pFrom->z ){
494 pTo->n = pFrom->n;
drh2646da72005-12-09 20:02:05 +0000495 pTo->z = (u8*)sqliteStrNDup((char*)pFrom->z, pFrom->n);
drh4b59ab52002-08-24 18:24:51 +0000496 pTo->dyn = 1;
497 }else{
drh4b59ab52002-08-24 18:24:51 +0000498 pTo->z = 0;
drh4b59ab52002-08-24 18:24:51 +0000499 }
500}
danielk19774adee202004-05-08 08:23:19 +0000501ExprList *sqlite3ExprListDup(ExprList *p){
drhff78bd22002-02-27 01:47:11 +0000502 ExprList *pNew;
drh145716b2004-09-24 12:24:06 +0000503 struct ExprList_item *pItem, *pOldItem;
drhff78bd22002-02-27 01:47:11 +0000504 int i;
505 if( p==0 ) return 0;
506 pNew = sqliteMalloc( sizeof(*pNew) );
507 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000508 pNew->nExpr = pNew->nAlloc = p->nExpr;
drh3e7bc9c2004-02-21 19:17:17 +0000509 pNew->a = pItem = sqliteMalloc( p->nExpr*sizeof(p->a[0]) );
danielk1977e0048402004-06-15 16:51:01 +0000510 if( pItem==0 ){
511 sqliteFree(pNew);
512 return 0;
513 }
drh145716b2004-09-24 12:24:06 +0000514 pOldItem = p->a;
515 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
drh4b59ab52002-08-24 18:24:51 +0000516 Expr *pNewExpr, *pOldExpr;
drh145716b2004-09-24 12:24:06 +0000517 pItem->pExpr = pNewExpr = sqlite3ExprDup(pOldExpr = pOldItem->pExpr);
drh6977fea2002-10-22 23:38:04 +0000518 if( pOldExpr->span.z!=0 && pNewExpr ){
519 /* Always make a copy of the span for top-level expressions in the
drh4b59ab52002-08-24 18:24:51 +0000520 ** expression list. The logic in SELECT processing that determines
521 ** the names of columns in the result set needs this information */
danielk19774adee202004-05-08 08:23:19 +0000522 sqlite3TokenCopy(&pNewExpr->span, &pOldExpr->span);
drh4b59ab52002-08-24 18:24:51 +0000523 }
drh1f3e9052002-10-31 00:09:39 +0000524 assert( pNewExpr==0 || pNewExpr->span.z!=0
drh6f7adc82006-01-11 21:41:20 +0000525 || pOldExpr->span.z==0
danielk19779e128002006-01-18 16:51:35 +0000526 || sqlite3MallocFailed() );
drh145716b2004-09-24 12:24:06 +0000527 pItem->zName = sqliteStrDup(pOldItem->zName);
528 pItem->sortOrder = pOldItem->sortOrder;
529 pItem->isAgg = pOldItem->isAgg;
drh3e7bc9c2004-02-21 19:17:17 +0000530 pItem->done = 0;
drhff78bd22002-02-27 01:47:11 +0000531 }
532 return pNew;
533}
danielk197793758c82005-01-21 08:13:14 +0000534
535/*
536** If cursors, triggers, views and subqueries are all omitted from
537** the build, then none of the following routines, except for
538** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes
539** called with a NULL argument.
540*/
danielk19776a67fe82005-02-04 04:07:16 +0000541#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \
542 || !defined(SQLITE_OMIT_SUBQUERY)
danielk19774adee202004-05-08 08:23:19 +0000543SrcList *sqlite3SrcListDup(SrcList *p){
drhad3cab52002-05-24 02:04:32 +0000544 SrcList *pNew;
545 int i;
drh113088e2003-03-20 01:16:58 +0000546 int nByte;
drhad3cab52002-05-24 02:04:32 +0000547 if( p==0 ) return 0;
drh113088e2003-03-20 01:16:58 +0000548 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
drh4efc4752004-01-16 15:55:37 +0000549 pNew = sqliteMallocRaw( nByte );
drhad3cab52002-05-24 02:04:32 +0000550 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000551 pNew->nSrc = pNew->nAlloc = p->nSrc;
drhad3cab52002-05-24 02:04:32 +0000552 for(i=0; i<p->nSrc; i++){
drh4efc4752004-01-16 15:55:37 +0000553 struct SrcList_item *pNewItem = &pNew->a[i];
554 struct SrcList_item *pOldItem = &p->a[i];
drhed8a3bb2005-06-06 21:19:56 +0000555 Table *pTab;
drh4efc4752004-01-16 15:55:37 +0000556 pNewItem->zDatabase = sqliteStrDup(pOldItem->zDatabase);
557 pNewItem->zName = sqliteStrDup(pOldItem->zName);
558 pNewItem->zAlias = sqliteStrDup(pOldItem->zAlias);
559 pNewItem->jointype = pOldItem->jointype;
560 pNewItem->iCursor = pOldItem->iCursor;
danielk19771787cca2006-02-10 07:07:14 +0000561 pNewItem->isPopulated = pOldItem->isPopulated;
drhed8a3bb2005-06-06 21:19:56 +0000562 pTab = pNewItem->pTab = pOldItem->pTab;
563 if( pTab ){
564 pTab->nRef++;
danielk1977a1cb1832005-02-12 08:59:55 +0000565 }
danielk19774adee202004-05-08 08:23:19 +0000566 pNewItem->pSelect = sqlite3SelectDup(pOldItem->pSelect);
567 pNewItem->pOn = sqlite3ExprDup(pOldItem->pOn);
568 pNewItem->pUsing = sqlite3IdListDup(pOldItem->pUsing);
danielk19776c18b6e2005-01-30 09:17:58 +0000569 pNewItem->colUsed = pOldItem->colUsed;
drhad3cab52002-05-24 02:04:32 +0000570 }
571 return pNew;
572}
danielk19774adee202004-05-08 08:23:19 +0000573IdList *sqlite3IdListDup(IdList *p){
drhff78bd22002-02-27 01:47:11 +0000574 IdList *pNew;
575 int i;
576 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000577 pNew = sqliteMallocRaw( sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +0000578 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000579 pNew->nId = pNew->nAlloc = p->nId;
drh4efc4752004-01-16 15:55:37 +0000580 pNew->a = sqliteMallocRaw( p->nId*sizeof(p->a[0]) );
danielk1977d5d56522005-03-16 12:15:20 +0000581 if( pNew->a==0 ){
582 sqliteFree(pNew);
583 return 0;
584 }
drhff78bd22002-02-27 01:47:11 +0000585 for(i=0; i<p->nId; i++){
drh4efc4752004-01-16 15:55:37 +0000586 struct IdList_item *pNewItem = &pNew->a[i];
587 struct IdList_item *pOldItem = &p->a[i];
588 pNewItem->zName = sqliteStrDup(pOldItem->zName);
589 pNewItem->idx = pOldItem->idx;
drhff78bd22002-02-27 01:47:11 +0000590 }
591 return pNew;
592}
danielk19774adee202004-05-08 08:23:19 +0000593Select *sqlite3SelectDup(Select *p){
drhff78bd22002-02-27 01:47:11 +0000594 Select *pNew;
595 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000596 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000597 if( pNew==0 ) return 0;
598 pNew->isDistinct = p->isDistinct;
danielk19774adee202004-05-08 08:23:19 +0000599 pNew->pEList = sqlite3ExprListDup(p->pEList);
600 pNew->pSrc = sqlite3SrcListDup(p->pSrc);
601 pNew->pWhere = sqlite3ExprDup(p->pWhere);
602 pNew->pGroupBy = sqlite3ExprListDup(p->pGroupBy);
603 pNew->pHaving = sqlite3ExprDup(p->pHaving);
604 pNew->pOrderBy = sqlite3ExprListDup(p->pOrderBy);
drhff78bd22002-02-27 01:47:11 +0000605 pNew->op = p->op;
danielk19774adee202004-05-08 08:23:19 +0000606 pNew->pPrior = sqlite3SelectDup(p->pPrior);
danielk1977a2dc3b12005-02-05 12:48:48 +0000607 pNew->pLimit = sqlite3ExprDup(p->pLimit);
608 pNew->pOffset = sqlite3ExprDup(p->pOffset);
drh7b58dae2003-07-20 01:16:46 +0000609 pNew->iLimit = -1;
610 pNew->iOffset = -1;
danielk1977a1cb1832005-02-12 08:59:55 +0000611 pNew->isResolved = p->isResolved;
612 pNew->isAgg = p->isAgg;
drhb9bb7c12006-06-11 23:41:55 +0000613 pNew->usesEphm = 0;
drh8e647b82005-09-23 21:11:53 +0000614 pNew->disallowOrderBy = 0;
drh0342b1f2005-09-01 03:07:44 +0000615 pNew->pRightmost = 0;
drhb9bb7c12006-06-11 23:41:55 +0000616 pNew->addrOpenEphm[0] = -1;
617 pNew->addrOpenEphm[1] = -1;
618 pNew->addrOpenEphm[2] = -1;
drhff78bd22002-02-27 01:47:11 +0000619 return pNew;
620}
danielk197793758c82005-01-21 08:13:14 +0000621#else
622Select *sqlite3SelectDup(Select *p){
623 assert( p==0 );
624 return 0;
625}
626#endif
drhff78bd22002-02-27 01:47:11 +0000627
628
629/*
drha76b5df2002-02-23 02:32:10 +0000630** Add a new element to the end of an expression list. If pList is
631** initially NULL, then create a new expression list.
632*/
danielk19774adee202004-05-08 08:23:19 +0000633ExprList *sqlite3ExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){
drha76b5df2002-02-23 02:32:10 +0000634 if( pList==0 ){
635 pList = sqliteMalloc( sizeof(ExprList) );
636 if( pList==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000637 goto no_mem;
drha76b5df2002-02-23 02:32:10 +0000638 }
drh4efc4752004-01-16 15:55:37 +0000639 assert( pList->nAlloc==0 );
drha76b5df2002-02-23 02:32:10 +0000640 }
drh4305d102003-07-30 12:34:12 +0000641 if( pList->nAlloc<=pList->nExpr ){
danielk1977d5d56522005-03-16 12:15:20 +0000642 struct ExprList_item *a;
643 int n = pList->nAlloc*2 + 4;
644 a = sqliteRealloc(pList->a, n*sizeof(pList->a[0]));
645 if( a==0 ){
646 goto no_mem;
drha76b5df2002-02-23 02:32:10 +0000647 }
danielk1977d5d56522005-03-16 12:15:20 +0000648 pList->a = a;
649 pList->nAlloc = n;
drha76b5df2002-02-23 02:32:10 +0000650 }
drh4efc4752004-01-16 15:55:37 +0000651 assert( pList->a!=0 );
652 if( pExpr || pName ){
653 struct ExprList_item *pItem = &pList->a[pList->nExpr++];
654 memset(pItem, 0, sizeof(*pItem));
drha99db3b2004-06-19 14:49:12 +0000655 pItem->zName = sqlite3NameFromToken(pName);
danielk1977e94ddc92005-03-21 03:53:38 +0000656 pItem->pExpr = pExpr;
drha76b5df2002-02-23 02:32:10 +0000657 }
658 return pList;
danielk1977d5d56522005-03-16 12:15:20 +0000659
660no_mem:
661 /* Avoid leaking memory if malloc has failed. */
662 sqlite3ExprDelete(pExpr);
663 sqlite3ExprListDelete(pList);
664 return 0;
drha76b5df2002-02-23 02:32:10 +0000665}
666
667/*
danielk19777a15a4b2007-05-08 17:54:43 +0000668** If the expression list pEList contains more than iLimit elements,
669** leave an error message in pParse.
670*/
671void sqlite3ExprListCheckLength(
672 Parse *pParse,
673 ExprList *pEList,
674 int iLimit,
675 const char *zObject
676){
danielk1977b4fc6792007-05-08 18:04:46 +0000677 if( pEList && pEList->nExpr>iLimit ){
danielk19777a15a4b2007-05-08 17:54:43 +0000678 sqlite3ErrorMsg(pParse, "too many columns in %s", zObject);
679 }
680}
681
danielk1977fc976062007-05-10 10:46:56 +0000682
683#if SQLITE_MAX_EXPR_DEPTH>0
684/* The following three functions, heightOfExpr(), heightOfExprList()
685** and heightOfSelect(), are used to determine the maximum height
686** of any expression tree referenced by the structure passed as the
687** first argument.
688**
689** If this maximum height is greater than the current value pointed
690** to by pnHeight, the second parameter, then set *pnHeight to that
691** value.
692*/
693static void heightOfExpr(Expr *p, int *pnHeight){
694 if( p ){
695 if( p->nHeight>*pnHeight ){
696 *pnHeight = p->nHeight;
697 }
698 }
699}
700static void heightOfExprList(ExprList *p, int *pnHeight){
701 if( p ){
702 int i;
703 for(i=0; i<p->nExpr; i++){
704 heightOfExpr(p->a[i].pExpr, pnHeight);
705 }
706 }
707}
708static void heightOfSelect(Select *p, int *pnHeight){
709 if( p ){
710 heightOfExpr(p->pWhere, pnHeight);
711 heightOfExpr(p->pHaving, pnHeight);
712 heightOfExpr(p->pLimit, pnHeight);
713 heightOfExpr(p->pOffset, pnHeight);
714 heightOfExprList(p->pEList, pnHeight);
715 heightOfExprList(p->pGroupBy, pnHeight);
716 heightOfExprList(p->pOrderBy, pnHeight);
717 heightOfSelect(p->pPrior, pnHeight);
718 }
719}
720
721/*
722** Set the Expr.nHeight variable in the structure passed as an
723** argument. An expression with no children, Expr.pList or
724** Expr.pSelect member has a height of 1. Any other expression
725** has a height equal to the maximum height of any other
726** referenced Expr plus one.
727*/
728void sqlite3ExprSetHeight(Expr *p){
729 int nHeight = 0;
730 heightOfExpr(p->pLeft, &nHeight);
731 heightOfExpr(p->pRight, &nHeight);
732 heightOfExprList(p->pList, &nHeight);
733 heightOfSelect(p->pSelect, &nHeight);
734 p->nHeight = nHeight + 1;
735}
736
737/*
738** Return the maximum height of any expression tree referenced
739** by the select statement passed as an argument.
740*/
741int sqlite3SelectExprHeight(Select *p){
742 int nHeight = 0;
743 heightOfSelect(p, &nHeight);
744 return nHeight;
745}
746#endif
747
danielk19777a15a4b2007-05-08 17:54:43 +0000748/*
drha76b5df2002-02-23 02:32:10 +0000749** Delete an entire expression list.
750*/
danielk19774adee202004-05-08 08:23:19 +0000751void sqlite3ExprListDelete(ExprList *pList){
drha76b5df2002-02-23 02:32:10 +0000752 int i;
drhbe5c89a2004-07-26 00:31:09 +0000753 struct ExprList_item *pItem;
drha76b5df2002-02-23 02:32:10 +0000754 if( pList==0 ) return;
drh1bdd9b52004-04-23 17:04:44 +0000755 assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
756 assert( pList->nExpr<=pList->nAlloc );
drhbe5c89a2004-07-26 00:31:09 +0000757 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
758 sqlite3ExprDelete(pItem->pExpr);
759 sqliteFree(pItem->zName);
drha76b5df2002-02-23 02:32:10 +0000760 }
761 sqliteFree(pList->a);
762 sqliteFree(pList);
763}
764
765/*
drh626a8792005-01-17 22:08:19 +0000766** Walk an expression tree. Call xFunc for each node visited.
drh73b211a2005-01-18 04:00:42 +0000767**
drh626a8792005-01-17 22:08:19 +0000768** The return value from xFunc determines whether the tree walk continues.
769** 0 means continue walking the tree. 1 means do not walk children
770** of the current node but continue with siblings. 2 means abandon
771** the tree walk completely.
772**
773** The return value from this routine is 1 to abandon the tree walk
774** and 0 to continue.
drh87abf5c2005-08-25 12:45:04 +0000775**
776** NOTICE: This routine does *not* descend into subqueries.
drh626a8792005-01-17 22:08:19 +0000777*/
danielk1977a58fdfb2005-02-08 07:50:40 +0000778static int walkExprList(ExprList *, int (*)(void *, Expr*), void *);
drh626a8792005-01-17 22:08:19 +0000779static int walkExprTree(Expr *pExpr, int (*xFunc)(void*,Expr*), void *pArg){
drh626a8792005-01-17 22:08:19 +0000780 int rc;
781 if( pExpr==0 ) return 0;
782 rc = (*xFunc)(pArg, pExpr);
783 if( rc==0 ){
784 if( walkExprTree(pExpr->pLeft, xFunc, pArg) ) return 1;
785 if( walkExprTree(pExpr->pRight, xFunc, pArg) ) return 1;
danielk1977a58fdfb2005-02-08 07:50:40 +0000786 if( walkExprList(pExpr->pList, xFunc, pArg) ) return 1;
drh626a8792005-01-17 22:08:19 +0000787 }
788 return rc>1;
789}
790
791/*
danielk1977a58fdfb2005-02-08 07:50:40 +0000792** Call walkExprTree() for every expression in list p.
793*/
794static int walkExprList(ExprList *p, int (*xFunc)(void *, Expr*), void *pArg){
795 int i;
796 struct ExprList_item *pItem;
797 if( !p ) return 0;
798 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
799 if( walkExprTree(pItem->pExpr, xFunc, pArg) ) return 1;
800 }
801 return 0;
802}
803
804/*
805** Call walkExprTree() for every expression in Select p, not including
806** expressions that are part of sub-selects in any FROM clause or the LIMIT
807** or OFFSET expressions..
808*/
809static int walkSelectExpr(Select *p, int (*xFunc)(void *, Expr*), void *pArg){
810 walkExprList(p->pEList, xFunc, pArg);
811 walkExprTree(p->pWhere, xFunc, pArg);
812 walkExprList(p->pGroupBy, xFunc, pArg);
813 walkExprTree(p->pHaving, xFunc, pArg);
814 walkExprList(p->pOrderBy, xFunc, pArg);
danielk197715d79822007-05-15 07:00:34 +0000815 if( p->pPrior ){
816 walkSelectExpr(p->pPrior, xFunc, pArg);
817 }
danielk1977a58fdfb2005-02-08 07:50:40 +0000818 return 0;
819}
820
821
822/*
drh626a8792005-01-17 22:08:19 +0000823** This routine is designed as an xFunc for walkExprTree().
824**
825** pArg is really a pointer to an integer. If we can tell by looking
drh73b211a2005-01-18 04:00:42 +0000826** at pExpr that the expression that contains pExpr is not a constant
827** expression, then set *pArg to 0 and return 2 to abandon the tree walk.
828** If pExpr does does not disqualify the expression from being a constant
829** then do nothing.
830**
831** After walking the whole tree, if no nodes are found that disqualify
832** the expression as constant, then we assume the whole expression
833** is constant. See sqlite3ExprIsConstant() for additional information.
drh626a8792005-01-17 22:08:19 +0000834*/
835static int exprNodeIsConstant(void *pArg, Expr *pExpr){
drh0a168372007-06-08 00:20:47 +0000836 int *pN = (int*)pArg;
837
838 /* If *pArg is 3 then any term of the expression that comes from
839 ** the ON or USING clauses of a join disqualifies the expression
840 ** from being considered constant. */
841 if( (*pN)==3 && ExprHasAnyProperty(pExpr, EP_FromJoin) ){
842 *pN = 0;
843 return 2;
844 }
845
drh626a8792005-01-17 22:08:19 +0000846 switch( pExpr->op ){
drheb55bd22005-06-30 17:04:21 +0000847 /* Consider functions to be constant if all their arguments are constant
848 ** and *pArg==2 */
849 case TK_FUNCTION:
drh0a168372007-06-08 00:20:47 +0000850 if( (*pN)==2 ) return 0;
drheb55bd22005-06-30 17:04:21 +0000851 /* Fall through */
drh626a8792005-01-17 22:08:19 +0000852 case TK_ID:
853 case TK_COLUMN:
854 case TK_DOT:
855 case TK_AGG_FUNCTION:
drh13449892005-09-07 21:22:45 +0000856 case TK_AGG_COLUMN:
drhfe2093d2005-01-20 22:48:47 +0000857#ifndef SQLITE_OMIT_SUBQUERY
858 case TK_SELECT:
859 case TK_EXISTS:
860#endif
drh0a168372007-06-08 00:20:47 +0000861 *pN = 0;
drh626a8792005-01-17 22:08:19 +0000862 return 2;
drh87abf5c2005-08-25 12:45:04 +0000863 case TK_IN:
864 if( pExpr->pSelect ){
drh0a168372007-06-08 00:20:47 +0000865 *pN = 0;
drh87abf5c2005-08-25 12:45:04 +0000866 return 2;
867 }
drh626a8792005-01-17 22:08:19 +0000868 default:
869 return 0;
870 }
871}
872
873/*
drhfef52082000-06-06 01:50:43 +0000874** Walk an expression tree. Return 1 if the expression is constant
drheb55bd22005-06-30 17:04:21 +0000875** and 0 if it involves variables or function calls.
drh23989372002-05-21 13:43:04 +0000876**
877** For the purposes of this function, a double-quoted string (ex: "abc")
878** is considered a variable but a single-quoted string (ex: 'abc') is
879** a constant.
drhfef52082000-06-06 01:50:43 +0000880*/
danielk19774adee202004-05-08 08:23:19 +0000881int sqlite3ExprIsConstant(Expr *p){
drh626a8792005-01-17 22:08:19 +0000882 int isConst = 1;
883 walkExprTree(p, exprNodeIsConstant, &isConst);
884 return isConst;
drhfef52082000-06-06 01:50:43 +0000885}
886
887/*
drheb55bd22005-06-30 17:04:21 +0000888** Walk an expression tree. Return 1 if the expression is constant
drh0a168372007-06-08 00:20:47 +0000889** that does no originate from the ON or USING clauses of a join.
890** Return 0 if it involves variables or function calls or terms from
891** an ON or USING clause.
892*/
893int sqlite3ExprIsConstantNotJoin(Expr *p){
894 int isConst = 3;
895 walkExprTree(p, exprNodeIsConstant, &isConst);
896 return isConst!=0;
897}
898
899/*
900** Walk an expression tree. Return 1 if the expression is constant
drheb55bd22005-06-30 17:04:21 +0000901** or a function call with constant arguments. Return and 0 if there
902** are any variables.
903**
904** For the purposes of this function, a double-quoted string (ex: "abc")
905** is considered a variable but a single-quoted string (ex: 'abc') is
906** a constant.
907*/
908int sqlite3ExprIsConstantOrFunction(Expr *p){
909 int isConst = 2;
910 walkExprTree(p, exprNodeIsConstant, &isConst);
911 return isConst!=0;
912}
913
914/*
drh73b211a2005-01-18 04:00:42 +0000915** If the expression p codes a constant integer that is small enough
drh202b2df2004-01-06 01:13:46 +0000916** to fit in a 32-bit integer, return 1 and put the value of the integer
917** in *pValue. If the expression is not an integer or if it is too big
918** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
drhe4de1fe2002-06-02 16:09:01 +0000919*/
danielk19774adee202004-05-08 08:23:19 +0000920int sqlite3ExprIsInteger(Expr *p, int *pValue){
drhe4de1fe2002-06-02 16:09:01 +0000921 switch( p->op ){
922 case TK_INTEGER: {
drh2646da72005-12-09 20:02:05 +0000923 if( sqlite3GetInt32((char*)p->token.z, pValue) ){
drh202b2df2004-01-06 01:13:46 +0000924 return 1;
925 }
926 break;
drhe4de1fe2002-06-02 16:09:01 +0000927 }
drh4b59ab52002-08-24 18:24:51 +0000928 case TK_UPLUS: {
danielk19774adee202004-05-08 08:23:19 +0000929 return sqlite3ExprIsInteger(p->pLeft, pValue);
drh4b59ab52002-08-24 18:24:51 +0000930 }
drhe4de1fe2002-06-02 16:09:01 +0000931 case TK_UMINUS: {
932 int v;
danielk19774adee202004-05-08 08:23:19 +0000933 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
drhe4de1fe2002-06-02 16:09:01 +0000934 *pValue = -v;
935 return 1;
936 }
937 break;
938 }
939 default: break;
940 }
941 return 0;
942}
943
944/*
drhc4a3c772001-04-04 11:48:57 +0000945** Return TRUE if the given string is a row-id column name.
946*/
danielk19774adee202004-05-08 08:23:19 +0000947int sqlite3IsRowid(const char *z){
948 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
949 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
950 if( sqlite3StrICmp(z, "OID")==0 ) return 1;
drhc4a3c772001-04-04 11:48:57 +0000951 return 0;
952}
953
954/*
drh8141f612004-01-25 22:44:58 +0000955** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
956** that name in the set of source tables in pSrcList and make the pExpr
957** expression node refer back to that source column. The following changes
958** are made to pExpr:
959**
960** pExpr->iDb Set the index in db->aDb[] of the database holding
961** the table.
962** pExpr->iTable Set to the cursor number for the table obtained
963** from pSrcList.
964** pExpr->iColumn Set to the column number within the table.
drh8141f612004-01-25 22:44:58 +0000965** pExpr->op Set to TK_COLUMN.
966** pExpr->pLeft Any expression this points to is deleted
967** pExpr->pRight Any expression this points to is deleted.
968**
969** The pDbToken is the name of the database (the "X"). This value may be
970** NULL meaning that name is of the form Y.Z or Z. Any available database
971** can be used. The pTableToken is the name of the table (the "Y"). This
972** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it
973** means that the form of the name is Z and that columns from any table
974** can be used.
975**
976** If the name cannot be resolved unambiguously, leave an error message
977** in pParse and return non-zero. Return zero on success.
978*/
979static int lookupName(
drhffe07b22005-11-03 00:41:17 +0000980 Parse *pParse, /* The parsing context */
drh8141f612004-01-25 22:44:58 +0000981 Token *pDbToken, /* Name of the database containing table, or NULL */
982 Token *pTableToken, /* Name of table containing column, or NULL */
983 Token *pColumnToken, /* Name of the column. */
drh626a8792005-01-17 22:08:19 +0000984 NameContext *pNC, /* The name context used to resolve the name */
drh8141f612004-01-25 22:44:58 +0000985 Expr *pExpr /* Make this EXPR node point to the selected column */
986){
987 char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */
988 char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */
989 char *zCol = 0; /* Name of the column. The "Z" */
990 int i, j; /* Loop counters */
991 int cnt = 0; /* Number of matching column names */
992 int cntTab = 0; /* Number of matching table names */
drh626a8792005-01-17 22:08:19 +0000993 sqlite3 *db = pParse->db; /* The database */
drh51669862004-12-18 18:40:26 +0000994 struct SrcList_item *pItem; /* Use for looping over pSrcList items */
995 struct SrcList_item *pMatch = 0; /* The matching pSrcList item */
drh73b211a2005-01-18 04:00:42 +0000996 NameContext *pTopNC = pNC; /* First namecontext in the list */
drh8141f612004-01-25 22:44:58 +0000997
998 assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
drha99db3b2004-06-19 14:49:12 +0000999 zDb = sqlite3NameFromToken(pDbToken);
1000 zTab = sqlite3NameFromToken(pTableToken);
1001 zCol = sqlite3NameFromToken(pColumnToken);
danielk19779e128002006-01-18 16:51:35 +00001002 if( sqlite3MallocFailed() ){
danielk1977d5d56522005-03-16 12:15:20 +00001003 goto lookupname_end;
drh8141f612004-01-25 22:44:58 +00001004 }
drh8141f612004-01-25 22:44:58 +00001005
1006 pExpr->iTable = -1;
drh626a8792005-01-17 22:08:19 +00001007 while( pNC && cnt==0 ){
drhffe07b22005-11-03 00:41:17 +00001008 ExprList *pEList;
drh626a8792005-01-17 22:08:19 +00001009 SrcList *pSrcList = pNC->pSrcList;
drh8141f612004-01-25 22:44:58 +00001010
danielk1977b3bce662005-01-29 08:32:43 +00001011 if( pSrcList ){
1012 for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
drh43617e92006-03-06 20:55:46 +00001013 Table *pTab;
1014 int iDb;
danielk1977b3bce662005-01-29 08:32:43 +00001015 Column *pCol;
1016
drh43617e92006-03-06 20:55:46 +00001017 pTab = pItem->pTab;
1018 assert( pTab!=0 );
1019 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
danielk1977b3bce662005-01-29 08:32:43 +00001020 assert( pTab->nCol>0 );
1021 if( zTab ){
1022 if( pItem->zAlias ){
1023 char *zTabName = pItem->zAlias;
1024 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
1025 }else{
1026 char *zTabName = pTab->zName;
1027 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
danielk1977da184232006-01-05 11:34:32 +00001028 if( zDb!=0 && sqlite3StrICmp(db->aDb[iDb].zName, zDb)!=0 ){
danielk1977b3bce662005-01-29 08:32:43 +00001029 continue;
1030 }
drh626a8792005-01-17 22:08:19 +00001031 }
drh8141f612004-01-25 22:44:58 +00001032 }
danielk1977b3bce662005-01-29 08:32:43 +00001033 if( 0==(cntTab++) ){
drh626a8792005-01-17 22:08:19 +00001034 pExpr->iTable = pItem->iCursor;
danielk1977da184232006-01-05 11:34:32 +00001035 pExpr->pSchema = pTab->pSchema;
danielk1977b3bce662005-01-29 08:32:43 +00001036 pMatch = pItem;
1037 }
1038 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
1039 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
danielk1977b3bf5562006-01-10 17:58:23 +00001040 const char *zColl = pTab->aCol[j].zColl;
drh873fac02005-06-06 17:11:46 +00001041 IdList *pUsing;
danielk1977b3bce662005-01-29 08:32:43 +00001042 cnt++;
1043 pExpr->iTable = pItem->iCursor;
1044 pMatch = pItem;
danielk1977da184232006-01-05 11:34:32 +00001045 pExpr->pSchema = pTab->pSchema;
danielk1977b3bce662005-01-29 08:32:43 +00001046 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
1047 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
1048 pExpr->affinity = pTab->aCol[j].affinity;
drh8b4c40d2007-02-01 23:02:45 +00001049 if( (pExpr->flags & EP_ExpCollate)==0 ){
1050 pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0);
1051 }
drh61dfc312006-12-16 16:25:15 +00001052 if( i<pSrcList->nSrc-1 ){
1053 if( pItem[1].jointype & JT_NATURAL ){
1054 /* If this match occurred in the left table of a natural join,
1055 ** then skip the right table to avoid a duplicate match */
1056 pItem++;
1057 i++;
1058 }else if( (pUsing = pItem[1].pUsing)!=0 ){
1059 /* If this match occurs on a column that is in the USING clause
1060 ** of a join, skip the search of the right table of the join
1061 ** to avoid a duplicate match there. */
1062 int k;
1063 for(k=0; k<pUsing->nId; k++){
1064 if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ){
1065 pItem++;
1066 i++;
1067 break;
1068 }
drh873fac02005-06-06 17:11:46 +00001069 }
1070 }
1071 }
danielk1977b3bce662005-01-29 08:32:43 +00001072 break;
1073 }
drh8141f612004-01-25 22:44:58 +00001074 }
1075 }
1076 }
drh626a8792005-01-17 22:08:19 +00001077
1078#ifndef SQLITE_OMIT_TRIGGER
1079 /* If we have not already resolved the name, then maybe
1080 ** it is a new.* or old.* trigger argument reference
1081 */
1082 if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
1083 TriggerStack *pTriggerStack = pParse->trigStack;
1084 Table *pTab = 0;
1085 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){
1086 pExpr->iTable = pTriggerStack->newIdx;
1087 assert( pTriggerStack->pTab );
1088 pTab = pTriggerStack->pTab;
1089 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab)==0 ){
1090 pExpr->iTable = pTriggerStack->oldIdx;
1091 assert( pTriggerStack->pTab );
1092 pTab = pTriggerStack->pTab;
1093 }
1094
1095 if( pTab ){
danielk1977f0113002006-01-24 12:09:17 +00001096 int iCol;
drh626a8792005-01-17 22:08:19 +00001097 Column *pCol = pTab->aCol;
1098
danielk1977da184232006-01-05 11:34:32 +00001099 pExpr->pSchema = pTab->pSchema;
drh626a8792005-01-17 22:08:19 +00001100 cntTab++;
danielk1977f0113002006-01-24 12:09:17 +00001101 for(iCol=0; iCol < pTab->nCol; iCol++, pCol++) {
drh626a8792005-01-17 22:08:19 +00001102 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
danielk1977f0113002006-01-24 12:09:17 +00001103 const char *zColl = pTab->aCol[iCol].zColl;
drh626a8792005-01-17 22:08:19 +00001104 cnt++;
danielk1977f0113002006-01-24 12:09:17 +00001105 pExpr->iColumn = iCol==pTab->iPKey ? -1 : iCol;
1106 pExpr->affinity = pTab->aCol[iCol].affinity;
drh8b4c40d2007-02-01 23:02:45 +00001107 if( (pExpr->flags & EP_ExpCollate)==0 ){
1108 pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0);
1109 }
danielk1977aee18ef2005-03-09 12:26:50 +00001110 pExpr->pTab = pTab;
drh626a8792005-01-17 22:08:19 +00001111 break;
1112 }
1113 }
1114 }
1115 }
drhb7f91642004-10-31 02:22:47 +00001116#endif /* !defined(SQLITE_OMIT_TRIGGER) */
drh8141f612004-01-25 22:44:58 +00001117
drh626a8792005-01-17 22:08:19 +00001118 /*
1119 ** Perhaps the name is a reference to the ROWID
1120 */
1121 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
1122 cnt = 1;
1123 pExpr->iColumn = -1;
drh8a512562005-11-14 22:29:05 +00001124 pExpr->affinity = SQLITE_AFF_INTEGER;
drh626a8792005-01-17 22:08:19 +00001125 }
drh8141f612004-01-25 22:44:58 +00001126
drh626a8792005-01-17 22:08:19 +00001127 /*
1128 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
1129 ** might refer to an result-set alias. This happens, for example, when
1130 ** we are resolving names in the WHERE clause of the following command:
1131 **
1132 ** SELECT a+b AS x FROM table WHERE x<10;
1133 **
1134 ** In cases like this, replace pExpr with a copy of the expression that
1135 ** forms the result set entry ("a+b" in the example) and return immediately.
1136 ** Note that the expression in the result set should have already been
1137 ** resolved by the time the WHERE clause is resolved.
1138 */
drhffe07b22005-11-03 00:41:17 +00001139 if( cnt==0 && (pEList = pNC->pEList)!=0 && zTab==0 ){
drh626a8792005-01-17 22:08:19 +00001140 for(j=0; j<pEList->nExpr; j++){
1141 char *zAs = pEList->a[j].zName;
1142 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
drh4f07e5f2007-05-14 11:34:46 +00001143 Expr *pDup;
drh626a8792005-01-17 22:08:19 +00001144 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
drh4f07e5f2007-05-14 11:34:46 +00001145 assert( pExpr->pList==0 );
1146 assert( pExpr->pSelect==0 );
1147 pDup = sqlite3ExprDup(pEList->a[j].pExpr);
1148 if( pExpr->flags & EP_ExpCollate ){
1149 pDup->pColl = pExpr->pColl;
1150 pDup->flags |= EP_ExpCollate;
1151 }
danielk1977edaaec22007-06-15 16:37:29 +00001152 if( pExpr->span.dyn ) sqliteFree((char*)pExpr->span.z);
1153 if( pExpr->token.dyn ) sqliteFree((char*)pExpr->token.z);
drh4f07e5f2007-05-14 11:34:46 +00001154 memcpy(pExpr, pDup, sizeof(*pExpr));
1155 sqliteFree(pDup);
drh15ccce12005-05-23 15:06:39 +00001156 cnt = 1;
danielk1977c9cf6e32007-06-25 16:29:33 +00001157 pMatch = 0;
drh626a8792005-01-17 22:08:19 +00001158 assert( zTab==0 && zDb==0 );
drh15ccce12005-05-23 15:06:39 +00001159 goto lookupname_end_2;
drh626a8792005-01-17 22:08:19 +00001160 }
1161 }
1162 }
1163
1164 /* Advance to the next name context. The loop will exit when either
1165 ** we have a match (cnt>0) or when we run out of name contexts.
1166 */
1167 if( cnt==0 ){
1168 pNC = pNC->pNext;
1169 }
drh8141f612004-01-25 22:44:58 +00001170 }
1171
1172 /*
1173 ** If X and Y are NULL (in other words if only the column name Z is
1174 ** supplied) and the value of Z is enclosed in double-quotes, then
1175 ** Z is a string literal if it doesn't match any column names. In that
1176 ** case, we need to return right away and not make any changes to
1177 ** pExpr.
drh15ccce12005-05-23 15:06:39 +00001178 **
1179 ** Because no reference was made to outer contexts, the pNC->nRef
1180 ** fields are not changed in any context.
drh8141f612004-01-25 22:44:58 +00001181 */
1182 if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
1183 sqliteFree(zCol);
1184 return 0;
1185 }
1186
1187 /*
1188 ** cnt==0 means there was not match. cnt>1 means there were two or
1189 ** more matches. Either way, we have an error.
1190 */
1191 if( cnt!=1 ){
1192 char *z = 0;
1193 char *zErr;
1194 zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s";
1195 if( zDb ){
drhf93339d2006-01-03 15:16:26 +00001196 sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, (char*)0);
drh8141f612004-01-25 22:44:58 +00001197 }else if( zTab ){
drhf93339d2006-01-03 15:16:26 +00001198 sqlite3SetString(&z, zTab, ".", zCol, (char*)0);
drh8141f612004-01-25 22:44:58 +00001199 }else{
1200 z = sqliteStrDup(zCol);
1201 }
danielk19774adee202004-05-08 08:23:19 +00001202 sqlite3ErrorMsg(pParse, zErr, z);
drh8141f612004-01-25 22:44:58 +00001203 sqliteFree(z);
drh73b211a2005-01-18 04:00:42 +00001204 pTopNC->nErr++;
drh8141f612004-01-25 22:44:58 +00001205 }
1206
drh51669862004-12-18 18:40:26 +00001207 /* If a column from a table in pSrcList is referenced, then record
1208 ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes
1209 ** bit 0 to be set. Column 1 sets bit 1. And so forth. If the
1210 ** column number is greater than the number of bits in the bitmask
1211 ** then set the high-order bit of the bitmask.
1212 */
1213 if( pExpr->iColumn>=0 && pMatch!=0 ){
1214 int n = pExpr->iColumn;
1215 if( n>=sizeof(Bitmask)*8 ){
1216 n = sizeof(Bitmask)*8-1;
1217 }
1218 assert( pMatch->iCursor==pExpr->iTable );
drhca83ac52007-02-01 01:40:44 +00001219 pMatch->colUsed |= ((Bitmask)1)<<n;
drh51669862004-12-18 18:40:26 +00001220 }
1221
danielk1977d5d56522005-03-16 12:15:20 +00001222lookupname_end:
drh8141f612004-01-25 22:44:58 +00001223 /* Clean up and return
1224 */
1225 sqliteFree(zDb);
1226 sqliteFree(zTab);
danielk19774adee202004-05-08 08:23:19 +00001227 sqlite3ExprDelete(pExpr->pLeft);
drh8141f612004-01-25 22:44:58 +00001228 pExpr->pLeft = 0;
danielk19774adee202004-05-08 08:23:19 +00001229 sqlite3ExprDelete(pExpr->pRight);
drh8141f612004-01-25 22:44:58 +00001230 pExpr->pRight = 0;
1231 pExpr->op = TK_COLUMN;
drh15ccce12005-05-23 15:06:39 +00001232lookupname_end_2:
1233 sqliteFree(zCol);
drh626a8792005-01-17 22:08:19 +00001234 if( cnt==1 ){
danielk1977b3bce662005-01-29 08:32:43 +00001235 assert( pNC!=0 );
drh626a8792005-01-17 22:08:19 +00001236 sqlite3AuthRead(pParse, pExpr, pNC->pSrcList);
danielk1977aee18ef2005-03-09 12:26:50 +00001237 if( pMatch && !pMatch->pSelect ){
1238 pExpr->pTab = pMatch->pTab;
1239 }
drh15ccce12005-05-23 15:06:39 +00001240 /* Increment the nRef value on all name contexts from TopNC up to
1241 ** the point where the name matched. */
1242 for(;;){
1243 assert( pTopNC!=0 );
1244 pTopNC->nRef++;
1245 if( pTopNC==pNC ) break;
1246 pTopNC = pTopNC->pNext;
1247 }
1248 return 0;
1249 } else {
1250 return 1;
drh626a8792005-01-17 22:08:19 +00001251 }
drh8141f612004-01-25 22:44:58 +00001252}
1253
1254/*
drh626a8792005-01-17 22:08:19 +00001255** This routine is designed as an xFunc for walkExprTree().
1256**
drh73b211a2005-01-18 04:00:42 +00001257** Resolve symbolic names into TK_COLUMN operators for the current
drh626a8792005-01-17 22:08:19 +00001258** node in the expression tree. Return 0 to continue the search down
drh73b211a2005-01-18 04:00:42 +00001259** the tree or 2 to abort the tree walk.
1260**
1261** This routine also does error checking and name resolution for
1262** function names. The operator for aggregate functions is changed
1263** to TK_AGG_FUNCTION.
drh626a8792005-01-17 22:08:19 +00001264*/
1265static int nameResolverStep(void *pArg, Expr *pExpr){
1266 NameContext *pNC = (NameContext*)pArg;
drh626a8792005-01-17 22:08:19 +00001267 Parse *pParse;
drh626a8792005-01-17 22:08:19 +00001268
danielk1977b3bce662005-01-29 08:32:43 +00001269 if( pExpr==0 ) return 1;
drh626a8792005-01-17 22:08:19 +00001270 assert( pNC!=0 );
drh626a8792005-01-17 22:08:19 +00001271 pParse = pNC->pParse;
danielk1977b3bce662005-01-29 08:32:43 +00001272
drh626a8792005-01-17 22:08:19 +00001273 if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return 1;
1274 ExprSetProperty(pExpr, EP_Resolved);
1275#ifndef NDEBUG
danielk1977f0113002006-01-24 12:09:17 +00001276 if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
1277 SrcList *pSrcList = pNC->pSrcList;
danielk1977940fac92005-01-23 22:41:37 +00001278 int i;
danielk1977f0113002006-01-24 12:09:17 +00001279 for(i=0; i<pNC->pSrcList->nSrc; i++){
drh626a8792005-01-17 22:08:19 +00001280 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
1281 }
1282 }
1283#endif
1284 switch( pExpr->op ){
1285 /* Double-quoted strings (ex: "abc") are used as identifiers if
1286 ** possible. Otherwise they remain as strings. Single-quoted
1287 ** strings (ex: 'abc') are always string literals.
1288 */
1289 case TK_STRING: {
1290 if( pExpr->token.z[0]=='\'' ) break;
1291 /* Fall thru into the TK_ID case if this is a double-quoted string */
1292 }
1293 /* A lone identifier is the name of a column.
1294 */
1295 case TK_ID: {
drh626a8792005-01-17 22:08:19 +00001296 lookupName(pParse, 0, 0, &pExpr->token, pNC, pExpr);
1297 return 1;
1298 }
1299
1300 /* A table name and column name: ID.ID
1301 ** Or a database, table and column: ID.ID.ID
1302 */
1303 case TK_DOT: {
1304 Token *pColumn;
1305 Token *pTable;
1306 Token *pDb;
1307 Expr *pRight;
1308
danielk1977b3bce662005-01-29 08:32:43 +00001309 /* if( pSrcList==0 ) break; */
drh626a8792005-01-17 22:08:19 +00001310 pRight = pExpr->pRight;
1311 if( pRight->op==TK_ID ){
1312 pDb = 0;
1313 pTable = &pExpr->pLeft->token;
1314 pColumn = &pRight->token;
1315 }else{
1316 assert( pRight->op==TK_DOT );
1317 pDb = &pExpr->pLeft->token;
1318 pTable = &pRight->pLeft->token;
1319 pColumn = &pRight->pRight->token;
1320 }
1321 lookupName(pParse, pDb, pTable, pColumn, pNC, pExpr);
1322 return 1;
1323 }
1324
1325 /* Resolve function names
1326 */
drhb71090f2005-05-23 17:26:51 +00001327 case TK_CONST_FUNC:
drh626a8792005-01-17 22:08:19 +00001328 case TK_FUNCTION: {
1329 ExprList *pList = pExpr->pList; /* The argument list */
1330 int n = pList ? pList->nExpr : 0; /* Number of arguments */
1331 int no_such_func = 0; /* True if no such function exists */
1332 int wrong_num_args = 0; /* True if wrong number of arguments */
1333 int is_agg = 0; /* True if is an aggregate function */
1334 int i;
drh5169bbc2006-08-24 14:59:45 +00001335 int auth; /* Authorization to use the function */
drh626a8792005-01-17 22:08:19 +00001336 int nId; /* Number of characters in function name */
1337 const char *zId; /* The function name. */
drh73b211a2005-01-18 04:00:42 +00001338 FuncDef *pDef; /* Information about the function */
danielk197714db2662006-01-09 16:12:04 +00001339 int enc = ENC(pParse->db); /* The database encoding */
drh626a8792005-01-17 22:08:19 +00001340
drh2646da72005-12-09 20:02:05 +00001341 zId = (char*)pExpr->token.z;
drhb71090f2005-05-23 17:26:51 +00001342 nId = pExpr->token.n;
drh626a8792005-01-17 22:08:19 +00001343 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
1344 if( pDef==0 ){
1345 pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
1346 if( pDef==0 ){
1347 no_such_func = 1;
1348 }else{
1349 wrong_num_args = 1;
1350 }
1351 }else{
1352 is_agg = pDef->xFunc==0;
1353 }
drh2fca7fe2006-11-23 11:59:13 +00001354#ifndef SQLITE_OMIT_AUTHORIZATION
drh5169bbc2006-08-24 14:59:45 +00001355 if( pDef ){
1356 auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0);
1357 if( auth!=SQLITE_OK ){
1358 if( auth==SQLITE_DENY ){
1359 sqlite3ErrorMsg(pParse, "not authorized to use function: %s",
1360 pDef->zName);
1361 pNC->nErr++;
1362 }
1363 pExpr->op = TK_NULL;
1364 return 1;
1365 }
1366 }
drhb8b14212006-08-24 15:18:25 +00001367#endif
drh626a8792005-01-17 22:08:19 +00001368 if( is_agg && !pNC->allowAgg ){
1369 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
1370 pNC->nErr++;
1371 is_agg = 0;
1372 }else if( no_such_func ){
1373 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
1374 pNC->nErr++;
1375 }else if( wrong_num_args ){
1376 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
1377 nId, zId);
1378 pNC->nErr++;
1379 }
1380 if( is_agg ){
1381 pExpr->op = TK_AGG_FUNCTION;
1382 pNC->hasAgg = 1;
1383 }
drh73b211a2005-01-18 04:00:42 +00001384 if( is_agg ) pNC->allowAgg = 0;
drh626a8792005-01-17 22:08:19 +00001385 for(i=0; pNC->nErr==0 && i<n; i++){
drh73b211a2005-01-18 04:00:42 +00001386 walkExprTree(pList->a[i].pExpr, nameResolverStep, pNC);
drh626a8792005-01-17 22:08:19 +00001387 }
drh73b211a2005-01-18 04:00:42 +00001388 if( is_agg ) pNC->allowAgg = 1;
drh626a8792005-01-17 22:08:19 +00001389 /* FIX ME: Compute pExpr->affinity based on the expected return
1390 ** type of the function
1391 */
1392 return is_agg;
1393 }
danielk1977b3bce662005-01-29 08:32:43 +00001394#ifndef SQLITE_OMIT_SUBQUERY
1395 case TK_SELECT:
1396 case TK_EXISTS:
1397#endif
1398 case TK_IN: {
1399 if( pExpr->pSelect ){
drh8a9f38f2005-11-05 15:07:55 +00001400 int nRef = pNC->nRef;
drh06f65412005-11-03 02:03:13 +00001401#ifndef SQLITE_OMIT_CHECK
1402 if( pNC->isCheck ){
1403 sqlite3ErrorMsg(pParse,"subqueries prohibited in CHECK constraints");
1404 }
1405#endif
danielk1977b3bce662005-01-29 08:32:43 +00001406 sqlite3SelectResolve(pParse, pExpr->pSelect, pNC);
1407 assert( pNC->nRef>=nRef );
1408 if( nRef!=pNC->nRef ){
1409 ExprSetProperty(pExpr, EP_VarSelect);
1410 }
1411 }
drh4284fb02005-11-03 12:33:28 +00001412 break;
danielk1977b3bce662005-01-29 08:32:43 +00001413 }
drh4284fb02005-11-03 12:33:28 +00001414#ifndef SQLITE_OMIT_CHECK
1415 case TK_VARIABLE: {
1416 if( pNC->isCheck ){
1417 sqlite3ErrorMsg(pParse,"parameters prohibited in CHECK constraints");
1418 }
1419 break;
1420 }
1421#endif
drh626a8792005-01-17 22:08:19 +00001422 }
1423 return 0;
1424}
1425
1426/*
drhcce7d172000-05-31 15:34:51 +00001427** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +00001428** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +00001429** index to the table in the table list and a column offset. The
1430** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
1431** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +00001432** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +00001433** VDBE cursor number for a cursor that is pointing into the referenced
1434** table. The Expr.iColumn value is changed to the index of the column
1435** of the referenced table. The Expr.iColumn value for the special
1436** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
1437** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +00001438**
drh626a8792005-01-17 22:08:19 +00001439** Also resolve function names and check the functions for proper
1440** usage. Make sure all function names are recognized and all functions
1441** have the correct number of arguments. Leave an error message
1442** in pParse->zErrMsg if anything is amiss. Return the number of errors.
1443**
drh73b211a2005-01-18 04:00:42 +00001444** If the expression contains aggregate functions then set the EP_Agg
1445** property on the expression.
drh626a8792005-01-17 22:08:19 +00001446*/
danielk1977fc976062007-05-10 10:46:56 +00001447int sqlite3ExprResolveNames(
danielk1977b3bce662005-01-29 08:32:43 +00001448 NameContext *pNC, /* Namespace to resolve expressions in. */
1449 Expr *pExpr /* The expression to be analyzed. */
drh626a8792005-01-17 22:08:19 +00001450){
drh13449892005-09-07 21:22:45 +00001451 int savedHasAgg;
drh73b211a2005-01-18 04:00:42 +00001452 if( pExpr==0 ) return 0;
danielk1977fc976062007-05-10 10:46:56 +00001453#if SQLITE_MAX_EXPR_DEPTH>0
1454 if( (pExpr->nHeight+pNC->pParse->nHeight)>SQLITE_MAX_EXPR_DEPTH ){
1455 sqlite3ErrorMsg(pNC->pParse,
1456 "Expression tree is too large (maximum depth %d)",
1457 SQLITE_MAX_EXPR_DEPTH
1458 );
1459 return 1;
1460 }
1461 pNC->pParse->nHeight += pExpr->nHeight;
1462#endif
drh13449892005-09-07 21:22:45 +00001463 savedHasAgg = pNC->hasAgg;
1464 pNC->hasAgg = 0;
danielk1977b3bce662005-01-29 08:32:43 +00001465 walkExprTree(pExpr, nameResolverStep, pNC);
danielk1977fc976062007-05-10 10:46:56 +00001466#if SQLITE_MAX_EXPR_DEPTH>0
1467 pNC->pParse->nHeight -= pExpr->nHeight;
1468#endif
danielk1977b3bce662005-01-29 08:32:43 +00001469 if( pNC->nErr>0 ){
drh73b211a2005-01-18 04:00:42 +00001470 ExprSetProperty(pExpr, EP_Error);
drh73b211a2005-01-18 04:00:42 +00001471 }
drh13449892005-09-07 21:22:45 +00001472 if( pNC->hasAgg ){
1473 ExprSetProperty(pExpr, EP_Agg);
1474 }else if( savedHasAgg ){
1475 pNC->hasAgg = 1;
1476 }
drh73b211a2005-01-18 04:00:42 +00001477 return ExprHasProperty(pExpr, EP_Error);
drh626a8792005-01-17 22:08:19 +00001478}
1479
drh1398ad32005-01-19 23:24:50 +00001480/*
1481** A pointer instance of this structure is used to pass information
1482** through walkExprTree into codeSubqueryStep().
1483*/
1484typedef struct QueryCoder QueryCoder;
1485struct QueryCoder {
1486 Parse *pParse; /* The parsing context */
1487 NameContext *pNC; /* Namespace of first enclosing query */
1488};
1489
drh626a8792005-01-17 22:08:19 +00001490
1491/*
drh9cbe6352005-11-29 03:13:21 +00001492** Generate code for scalar subqueries used as an expression
1493** and IN operators. Examples:
drh626a8792005-01-17 22:08:19 +00001494**
drh9cbe6352005-11-29 03:13:21 +00001495** (SELECT a FROM b) -- subquery
1496** EXISTS (SELECT a FROM b) -- EXISTS subquery
1497** x IN (4,5,11) -- IN operator with list on right-hand side
1498** x IN (SELECT a FROM b) -- IN operator with subquery on the right
drhfef52082000-06-06 01:50:43 +00001499**
drh9cbe6352005-11-29 03:13:21 +00001500** The pExpr parameter describes the expression that contains the IN
1501** operator or subquery.
drhcce7d172000-05-31 15:34:51 +00001502*/
drh51522cd2005-01-20 13:36:19 +00001503#ifndef SQLITE_OMIT_SUBQUERY
danielk1977b3bce662005-01-29 08:32:43 +00001504void sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){
drh57dbd7b2005-07-08 18:25:26 +00001505 int testAddr = 0; /* One-time test address */
danielk1977b3bce662005-01-29 08:32:43 +00001506 Vdbe *v = sqlite3GetVdbe(pParse);
1507 if( v==0 ) return;
1508
danielk1977fc976062007-05-10 10:46:56 +00001509
drh57dbd7b2005-07-08 18:25:26 +00001510 /* This code must be run in its entirety every time it is encountered
1511 ** if any of the following is true:
1512 **
1513 ** * The right-hand side is a correlated subquery
1514 ** * The right-hand side is an expression list containing variables
1515 ** * We are inside a trigger
1516 **
1517 ** If all of the above are false, then we can run this code just once
1518 ** save the results, and reuse the same result on subsequent invocations.
danielk1977b3bce662005-01-29 08:32:43 +00001519 */
1520 if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->trigStack ){
1521 int mem = pParse->nMem++;
1522 sqlite3VdbeAddOp(v, OP_MemLoad, mem, 0);
drh57dbd7b2005-07-08 18:25:26 +00001523 testAddr = sqlite3VdbeAddOp(v, OP_If, 0, 0);
danielk19779e128002006-01-18 16:51:35 +00001524 assert( testAddr>0 || sqlite3MallocFailed() );
drhd654be82005-09-20 17:42:23 +00001525 sqlite3VdbeAddOp(v, OP_MemInt, 1, mem);
danielk1977b3bce662005-01-29 08:32:43 +00001526 }
1527
drhcce7d172000-05-31 15:34:51 +00001528 switch( pExpr->op ){
drhfef52082000-06-06 01:50:43 +00001529 case TK_IN: {
danielk1977e014a832004-05-17 10:48:57 +00001530 char affinity;
drhd3d39e92004-05-20 22:16:29 +00001531 KeyInfo keyInfo;
drhb9bb7c12006-06-11 23:41:55 +00001532 int addr; /* Address of OP_OpenEphemeral instruction */
drhd3d39e92004-05-20 22:16:29 +00001533
danielk1977bf3b7212004-05-18 10:06:24 +00001534 affinity = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +00001535
1536 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
drh57dbd7b2005-07-08 18:25:26 +00001537 ** expression it is handled the same way. A virtual table is
danielk1977e014a832004-05-17 10:48:57 +00001538 ** filled with single-field index keys representing the results
1539 ** from the SELECT or the <exprlist>.
1540 **
1541 ** If the 'x' expression is a column value, or the SELECT...
1542 ** statement returns a column value, then the affinity of that
1543 ** column is used to build the index keys. If both 'x' and the
1544 ** SELECT... statement are columns, then numeric affinity is used
1545 ** if either column has NUMERIC or INTEGER affinity. If neither
1546 ** 'x' nor the SELECT... statement are columns, then numeric affinity
1547 ** is used.
1548 */
1549 pExpr->iTable = pParse->nTab++;
drhb9bb7c12006-06-11 23:41:55 +00001550 addr = sqlite3VdbeAddOp(v, OP_OpenEphemeral, pExpr->iTable, 0);
drhd3d39e92004-05-20 22:16:29 +00001551 memset(&keyInfo, 0, sizeof(keyInfo));
1552 keyInfo.nField = 1;
drhf3218fe2004-05-28 08:21:02 +00001553 sqlite3VdbeAddOp(v, OP_SetNumColumns, pExpr->iTable, 1);
danielk1977e014a832004-05-17 10:48:57 +00001554
drhfef52082000-06-06 01:50:43 +00001555 if( pExpr->pSelect ){
1556 /* Case 1: expr IN (SELECT ...)
1557 **
danielk1977e014a832004-05-17 10:48:57 +00001558 ** Generate code to write the results of the select into the temporary
1559 ** table allocated and opened above.
drhfef52082000-06-06 01:50:43 +00001560 */
danielk1977e014a832004-05-17 10:48:57 +00001561 int iParm = pExpr->iTable + (((int)affinity)<<16);
drhbe5c89a2004-07-26 00:31:09 +00001562 ExprList *pEList;
danielk1977e014a832004-05-17 10:48:57 +00001563 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
drh94ccde52007-04-13 16:06:32 +00001564 if( sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0) ){
1565 return;
1566 }
drhbe5c89a2004-07-26 00:31:09 +00001567 pEList = pExpr->pSelect->pEList;
1568 if( pEList && pEList->nExpr>0 ){
danielk1977bcbb04e2007-05-29 12:11:29 +00001569 keyInfo.aColl[0] = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft,
drhbe5c89a2004-07-26 00:31:09 +00001570 pEList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001571 }
drhfef52082000-06-06 01:50:43 +00001572 }else if( pExpr->pList ){
1573 /* Case 2: expr IN (exprlist)
1574 **
danielk1977e014a832004-05-17 10:48:57 +00001575 ** For each expression, build an index key from the evaluation and
1576 ** store it in the temporary table. If <expr> is a column, then use
1577 ** that columns affinity when building index keys. If <expr> is not
1578 ** a column, use numeric affinity.
drhfef52082000-06-06 01:50:43 +00001579 */
danielk1977e014a832004-05-17 10:48:57 +00001580 int i;
drh57dbd7b2005-07-08 18:25:26 +00001581 ExprList *pList = pExpr->pList;
1582 struct ExprList_item *pItem;
1583
danielk1977e014a832004-05-17 10:48:57 +00001584 if( !affinity ){
drh8159a352006-05-23 23:22:29 +00001585 affinity = SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +00001586 }
danielk19770202b292004-06-09 09:55:16 +00001587 keyInfo.aColl[0] = pExpr->pLeft->pColl;
danielk1977e014a832004-05-17 10:48:57 +00001588
1589 /* Loop through each expression in <exprlist>. */
drh57dbd7b2005-07-08 18:25:26 +00001590 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
1591 Expr *pE2 = pItem->pExpr;
danielk1977e014a832004-05-17 10:48:57 +00001592
drh57dbd7b2005-07-08 18:25:26 +00001593 /* If the expression is not constant then we will need to
1594 ** disable the test that was generated above that makes sure
1595 ** this code only executes once. Because for a non-constant
1596 ** expression we need to rerun this code each time.
1597 */
drh6c30be82005-07-29 15:10:17 +00001598 if( testAddr>0 && !sqlite3ExprIsConstant(pE2) ){
drhf8875402006-03-17 13:56:34 +00001599 sqlite3VdbeChangeToNoop(v, testAddr-1, 3);
drh57dbd7b2005-07-08 18:25:26 +00001600 testAddr = 0;
drh4794b982000-06-06 13:54:14 +00001601 }
danielk1977e014a832004-05-17 10:48:57 +00001602
1603 /* Evaluate the expression and insert it into the temp table */
1604 sqlite3ExprCode(pParse, pE2);
drh94a11212004-09-25 13:12:14 +00001605 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1);
drhf0863fe2005-06-12 21:35:51 +00001606 sqlite3VdbeAddOp(v, OP_IdxInsert, pExpr->iTable, 0);
drhfef52082000-06-06 01:50:43 +00001607 }
1608 }
danielk19770202b292004-06-09 09:55:16 +00001609 sqlite3VdbeChangeP3(v, addr, (void *)&keyInfo, P3_KEYINFO);
danielk1977b3bce662005-01-29 08:32:43 +00001610 break;
drhfef52082000-06-06 01:50:43 +00001611 }
1612
drh51522cd2005-01-20 13:36:19 +00001613 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001614 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +00001615 /* This has to be a scalar SELECT. Generate code to put the
1616 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +00001617 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +00001618 */
drh2646da72005-12-09 20:02:05 +00001619 static const Token one = { (u8*)"1", 0, 1 };
drh51522cd2005-01-20 13:36:19 +00001620 Select *pSel;
drhec7429a2005-10-06 16:53:14 +00001621 int iMem;
1622 int sop;
drh1398ad32005-01-19 23:24:50 +00001623
drhec7429a2005-10-06 16:53:14 +00001624 pExpr->iColumn = iMem = pParse->nMem++;
drh51522cd2005-01-20 13:36:19 +00001625 pSel = pExpr->pSelect;
1626 if( pExpr->op==TK_SELECT ){
1627 sop = SRT_Mem;
drhec7429a2005-10-06 16:53:14 +00001628 sqlite3VdbeAddOp(v, OP_MemNull, iMem, 0);
1629 VdbeComment((v, "# Init subquery result"));
drh51522cd2005-01-20 13:36:19 +00001630 }else{
drh51522cd2005-01-20 13:36:19 +00001631 sop = SRT_Exists;
drhec7429a2005-10-06 16:53:14 +00001632 sqlite3VdbeAddOp(v, OP_MemInt, 0, iMem);
1633 VdbeComment((v, "# Init EXISTS result"));
drh51522cd2005-01-20 13:36:19 +00001634 }
drhec7429a2005-10-06 16:53:14 +00001635 sqlite3ExprDelete(pSel->pLimit);
1636 pSel->pLimit = sqlite3Expr(TK_INTEGER, 0, 0, &one);
drh94ccde52007-04-13 16:06:32 +00001637 if( sqlite3Select(pParse, pSel, sop, iMem, 0, 0, 0, 0) ){
1638 return;
1639 }
danielk1977b3bce662005-01-29 08:32:43 +00001640 break;
drhcce7d172000-05-31 15:34:51 +00001641 }
1642 }
danielk1977b3bce662005-01-29 08:32:43 +00001643
drh57dbd7b2005-07-08 18:25:26 +00001644 if( testAddr ){
drhd654be82005-09-20 17:42:23 +00001645 sqlite3VdbeJumpHere(v, testAddr);
danielk1977b3bce662005-01-29 08:32:43 +00001646 }
danielk1977fc976062007-05-10 10:46:56 +00001647
danielk1977b3bce662005-01-29 08:32:43 +00001648 return;
drhcce7d172000-05-31 15:34:51 +00001649}
drh51522cd2005-01-20 13:36:19 +00001650#endif /* SQLITE_OMIT_SUBQUERY */
drhcce7d172000-05-31 15:34:51 +00001651
drhcce7d172000-05-31 15:34:51 +00001652/*
drhfec19aa2004-05-19 20:41:03 +00001653** Generate an instruction that will put the integer describe by
1654** text z[0..n-1] on the stack.
1655*/
1656static void codeInteger(Vdbe *v, const char *z, int n){
danielk1977c9cf9012007-05-30 10:36:47 +00001657 assert( z || sqlite3MallocFailed() );
1658 if( z ){
1659 int i;
1660 if( sqlite3GetInt32(z, &i) ){
1661 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
1662 }else if( sqlite3FitsIn64Bits(z) ){
1663 sqlite3VdbeOp3(v, OP_Int64, 0, 0, z, n);
1664 }else{
1665 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n);
1666 }
drhfec19aa2004-05-19 20:41:03 +00001667 }
1668}
1669
drh945498f2007-02-24 11:52:52 +00001670
1671/*
1672** Generate code that will extract the iColumn-th column from
1673** table pTab and push that column value on the stack. There
1674** is an open cursor to pTab in iTable. If iColumn<0 then
1675** code is generated that extracts the rowid.
1676*/
1677void sqlite3ExprCodeGetColumn(Vdbe *v, Table *pTab, int iColumn, int iTable){
1678 if( iColumn<0 ){
1679 int op = (pTab && IsVirtual(pTab)) ? OP_VRowid : OP_Rowid;
1680 sqlite3VdbeAddOp(v, op, iTable, 0);
1681 }else if( pTab==0 ){
1682 sqlite3VdbeAddOp(v, OP_Column, iTable, iColumn);
1683 }else{
1684 int op = IsVirtual(pTab) ? OP_VColumn : OP_Column;
1685 sqlite3VdbeAddOp(v, op, iTable, iColumn);
1686 sqlite3ColumnDefault(v, pTab, iColumn);
1687#ifndef SQLITE_OMIT_FLOATING_POINT
1688 if( pTab->aCol[iColumn].affinity==SQLITE_AFF_REAL ){
1689 sqlite3VdbeAddOp(v, OP_RealAffinity, 0, 0);
1690 }
1691#endif
1692 }
1693}
1694
drhfec19aa2004-05-19 20:41:03 +00001695/*
drhcce7d172000-05-31 15:34:51 +00001696** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +00001697** expression and leave the result on the top of stack.
drhf2bc0132004-10-04 13:19:23 +00001698**
1699** This code depends on the fact that certain token values (ex: TK_EQ)
1700** are the same as opcode values (ex: OP_Eq) that implement the corresponding
1701** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
1702** the make process cause these values to align. Assert()s in the code
1703** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00001704*/
danielk19774adee202004-05-08 08:23:19 +00001705void sqlite3ExprCode(Parse *pParse, Expr *pExpr){
drhcce7d172000-05-31 15:34:51 +00001706 Vdbe *v = pParse->pVdbe;
1707 int op;
drhffe07b22005-11-03 00:41:17 +00001708 int stackChng = 1; /* Amount of change to stack depth */
1709
danielk19777977a172004-11-09 12:44:37 +00001710 if( v==0 ) return;
1711 if( pExpr==0 ){
drhf0863fe2005-06-12 21:35:51 +00001712 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
danielk19777977a172004-11-09 12:44:37 +00001713 return;
1714 }
drhf2bc0132004-10-04 13:19:23 +00001715 op = pExpr->op;
1716 switch( op ){
drh13449892005-09-07 21:22:45 +00001717 case TK_AGG_COLUMN: {
1718 AggInfo *pAggInfo = pExpr->pAggInfo;
1719 struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg];
1720 if( !pAggInfo->directMode ){
1721 sqlite3VdbeAddOp(v, OP_MemLoad, pCol->iMem, 0);
1722 break;
1723 }else if( pAggInfo->useSortingIdx ){
1724 sqlite3VdbeAddOp(v, OP_Column, pAggInfo->sortingIdx,
1725 pCol->iSorterColumn);
1726 break;
1727 }
1728 /* Otherwise, fall thru into the TK_COLUMN case */
1729 }
drh967e8b72000-06-21 13:59:10 +00001730 case TK_COLUMN: {
drhffe07b22005-11-03 00:41:17 +00001731 if( pExpr->iTable<0 ){
1732 /* This only happens when coding check constraints */
1733 assert( pParse->ckOffset>0 );
1734 sqlite3VdbeAddOp(v, OP_Dup, pParse->ckOffset-pExpr->iColumn-1, 1);
drhc4a3c772001-04-04 11:48:57 +00001735 }else{
drh945498f2007-02-24 11:52:52 +00001736 sqlite3ExprCodeGetColumn(v, pExpr->pTab, pExpr->iColumn, pExpr->iTable);
drh22827922000-06-06 17:27:05 +00001737 }
drhcce7d172000-05-31 15:34:51 +00001738 break;
1739 }
1740 case TK_INTEGER: {
drh2646da72005-12-09 20:02:05 +00001741 codeInteger(v, (char*)pExpr->token.z, pExpr->token.n);
drhfec19aa2004-05-19 20:41:03 +00001742 break;
1743 }
1744 case TK_FLOAT:
1745 case TK_STRING: {
drhf2bc0132004-10-04 13:19:23 +00001746 assert( TK_FLOAT==OP_Real );
1747 assert( TK_STRING==OP_String8 );
drhd2687b72005-08-12 22:56:09 +00001748 sqlite3DequoteExpr(pExpr);
drh2646da72005-12-09 20:02:05 +00001749 sqlite3VdbeOp3(v, op, 0, 0, (char*)pExpr->token.z, pExpr->token.n);
drhcce7d172000-05-31 15:34:51 +00001750 break;
1751 }
drhf0863fe2005-06-12 21:35:51 +00001752 case TK_NULL: {
1753 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
1754 break;
1755 }
danielk19775338a5f2005-01-20 13:03:10 +00001756#ifndef SQLITE_OMIT_BLOB_LITERAL
danielk1977c572ef72004-05-27 09:28:41 +00001757 case TK_BLOB: {
drh6c8c6ce2005-08-23 11:17:58 +00001758 int n;
1759 const char *z;
drhf2bc0132004-10-04 13:19:23 +00001760 assert( TK_BLOB==OP_HexBlob );
drh6c8c6ce2005-08-23 11:17:58 +00001761 n = pExpr->token.n - 3;
drh2646da72005-12-09 20:02:05 +00001762 z = (char*)pExpr->token.z + 2;
drh6c8c6ce2005-08-23 11:17:58 +00001763 assert( n>=0 );
1764 if( n==0 ){
1765 z = "";
1766 }
1767 sqlite3VdbeOp3(v, op, 0, 0, z, n);
danielk1977c572ef72004-05-27 09:28:41 +00001768 break;
1769 }
danielk19775338a5f2005-01-20 13:03:10 +00001770#endif
drh50457892003-09-06 01:10:47 +00001771 case TK_VARIABLE: {
danielk19774adee202004-05-08 08:23:19 +00001772 sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0);
drh895d7472004-08-20 16:02:39 +00001773 if( pExpr->token.n>1 ){
drh2646da72005-12-09 20:02:05 +00001774 sqlite3VdbeChangeP3(v, -1, (char*)pExpr->token.z, pExpr->token.n);
drh895d7472004-08-20 16:02:39 +00001775 }
drh50457892003-09-06 01:10:47 +00001776 break;
1777 }
drh4e0cff62004-11-05 05:10:28 +00001778 case TK_REGISTER: {
1779 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iTable, 0);
1780 break;
1781 }
drh487e2622005-06-25 18:42:14 +00001782#ifndef SQLITE_OMIT_CAST
1783 case TK_CAST: {
1784 /* Expressions of the form: CAST(pLeft AS token) */
danielk1977f0113002006-01-24 12:09:17 +00001785 int aff, to_op;
drh487e2622005-06-25 18:42:14 +00001786 sqlite3ExprCode(pParse, pExpr->pLeft);
drh8a512562005-11-14 22:29:05 +00001787 aff = sqlite3AffinityType(&pExpr->token);
danielk1977f0113002006-01-24 12:09:17 +00001788 to_op = aff - SQLITE_AFF_TEXT + OP_ToText;
1789 assert( to_op==OP_ToText || aff!=SQLITE_AFF_TEXT );
1790 assert( to_op==OP_ToBlob || aff!=SQLITE_AFF_NONE );
1791 assert( to_op==OP_ToNumeric || aff!=SQLITE_AFF_NUMERIC );
1792 assert( to_op==OP_ToInt || aff!=SQLITE_AFF_INTEGER );
1793 assert( to_op==OP_ToReal || aff!=SQLITE_AFF_REAL );
1794 sqlite3VdbeAddOp(v, to_op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001795 stackChng = 0;
drh487e2622005-06-25 18:42:14 +00001796 break;
1797 }
1798#endif /* SQLITE_OMIT_CAST */
drhc9b84a12002-06-20 11:36:48 +00001799 case TK_LT:
1800 case TK_LE:
1801 case TK_GT:
1802 case TK_GE:
1803 case TK_NE:
1804 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00001805 assert( TK_LT==OP_Lt );
1806 assert( TK_LE==OP_Le );
1807 assert( TK_GT==OP_Gt );
1808 assert( TK_GE==OP_Ge );
1809 assert( TK_EQ==OP_Eq );
1810 assert( TK_NE==OP_Ne );
danielk1977a37cdde2004-05-16 11:15:36 +00001811 sqlite3ExprCode(pParse, pExpr->pLeft);
1812 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001813 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001814 stackChng = -1;
danielk1977a37cdde2004-05-16 11:15:36 +00001815 break;
drhc9b84a12002-06-20 11:36:48 +00001816 }
drhcce7d172000-05-31 15:34:51 +00001817 case TK_AND:
1818 case TK_OR:
1819 case TK_PLUS:
1820 case TK_STAR:
1821 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00001822 case TK_REM:
1823 case TK_BITAND:
1824 case TK_BITOR:
drh17c40292004-07-21 02:53:29 +00001825 case TK_SLASH:
drhbf4133c2001-10-13 02:59:08 +00001826 case TK_LSHIFT:
drh855eb1c2004-08-31 13:45:11 +00001827 case TK_RSHIFT:
drh00400772000-06-16 20:51:26 +00001828 case TK_CONCAT: {
drhf2bc0132004-10-04 13:19:23 +00001829 assert( TK_AND==OP_And );
1830 assert( TK_OR==OP_Or );
1831 assert( TK_PLUS==OP_Add );
1832 assert( TK_MINUS==OP_Subtract );
1833 assert( TK_REM==OP_Remainder );
1834 assert( TK_BITAND==OP_BitAnd );
1835 assert( TK_BITOR==OP_BitOr );
1836 assert( TK_SLASH==OP_Divide );
1837 assert( TK_LSHIFT==OP_ShiftLeft );
1838 assert( TK_RSHIFT==OP_ShiftRight );
1839 assert( TK_CONCAT==OP_Concat );
danielk19774adee202004-05-08 08:23:19 +00001840 sqlite3ExprCode(pParse, pExpr->pLeft);
1841 sqlite3ExprCode(pParse, pExpr->pRight);
drh855eb1c2004-08-31 13:45:11 +00001842 sqlite3VdbeAddOp(v, op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001843 stackChng = -1;
drh00400772000-06-16 20:51:26 +00001844 break;
1845 }
drhcce7d172000-05-31 15:34:51 +00001846 case TK_UMINUS: {
drhfec19aa2004-05-19 20:41:03 +00001847 Expr *pLeft = pExpr->pLeft;
1848 assert( pLeft );
1849 if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
1850 Token *p = &pLeft->token;
drh9267bdc2005-11-28 12:36:35 +00001851 char *z = sqlite3MPrintf("-%.*s", p->n, p->z);
drhfec19aa2004-05-19 20:41:03 +00001852 if( pLeft->op==TK_FLOAT ){
1853 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001854 }else{
drhfec19aa2004-05-19 20:41:03 +00001855 codeInteger(v, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001856 }
drh6e142f52000-06-08 13:36:40 +00001857 sqliteFree(z);
1858 break;
1859 }
drh1ccde152000-06-17 13:12:39 +00001860 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +00001861 }
drhbf4133c2001-10-13 02:59:08 +00001862 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00001863 case TK_NOT: {
drhf2bc0132004-10-04 13:19:23 +00001864 assert( TK_BITNOT==OP_BitNot );
1865 assert( TK_NOT==OP_Not );
danielk19774adee202004-05-08 08:23:19 +00001866 sqlite3ExprCode(pParse, pExpr->pLeft);
1867 sqlite3VdbeAddOp(v, op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001868 stackChng = 0;
drhcce7d172000-05-31 15:34:51 +00001869 break;
1870 }
1871 case TK_ISNULL:
1872 case TK_NOTNULL: {
1873 int dest;
drhf2bc0132004-10-04 13:19:23 +00001874 assert( TK_ISNULL==OP_IsNull );
1875 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00001876 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1877 sqlite3ExprCode(pParse, pExpr->pLeft);
1878 dest = sqlite3VdbeCurrentAddr(v) + 2;
1879 sqlite3VdbeAddOp(v, op, 1, dest);
1880 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);
drhffe07b22005-11-03 00:41:17 +00001881 stackChng = 0;
drhf2bc0132004-10-04 13:19:23 +00001882 break;
drhcce7d172000-05-31 15:34:51 +00001883 }
drh22827922000-06-06 17:27:05 +00001884 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00001885 AggInfo *pInfo = pExpr->pAggInfo;
drh7e56e712005-11-16 12:53:15 +00001886 if( pInfo==0 ){
1887 sqlite3ErrorMsg(pParse, "misuse of aggregate: %T",
1888 &pExpr->span);
1889 }else{
1890 sqlite3VdbeAddOp(v, OP_MemLoad, pInfo->aFunc[pExpr->iAgg].iMem, 0);
1891 }
drh22827922000-06-06 17:27:05 +00001892 break;
1893 }
drhb71090f2005-05-23 17:26:51 +00001894 case TK_CONST_FUNC:
drhcce7d172000-05-31 15:34:51 +00001895 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00001896 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00001897 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00001898 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00001899 int nId;
1900 const char *zId;
drh13449892005-09-07 21:22:45 +00001901 int constMask = 0;
danielk1977682f68b2004-06-05 10:22:17 +00001902 int i;
danielk197714db2662006-01-09 16:12:04 +00001903 u8 enc = ENC(pParse->db);
danielk1977dc1bdc42004-06-11 10:51:27 +00001904 CollSeq *pColl = 0;
drh2646da72005-12-09 20:02:05 +00001905 zId = (char*)pExpr->token.z;
drhb71090f2005-05-23 17:26:51 +00001906 nId = pExpr->token.n;
danielk1977d8123362004-06-12 09:25:12 +00001907 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001908 assert( pDef!=0 );
drhf9b596e2004-05-26 16:54:42 +00001909 nExpr = sqlite3ExprCodeExprList(pParse, pList);
drhb7f6f682006-07-08 17:06:43 +00001910#ifndef SQLITE_OMIT_VIRTUALTABLE
drha43fa222006-07-08 18:41:37 +00001911 /* Possibly overload the function if the first argument is
1912 ** a virtual table column.
1913 **
1914 ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the
1915 ** second argument, not the first, as the argument to test to
1916 ** see if it is a column in a virtual table. This is done because
1917 ** the left operand of infix functions (the operand we want to
1918 ** control overloading) ends up as the second argument to the
1919 ** function. The expression "A glob B" is equivalent to
1920 ** "glob(B,A). We want to use the A in "A glob B" to test
1921 ** for function overloading. But we use the B term in "glob(B,A)".
1922 */
drh6a03a1c2006-07-08 18:34:59 +00001923 if( nExpr>=2 && (pExpr->flags & EP_InfixFunc) ){
drh6a03a1c2006-07-08 18:34:59 +00001924 pDef = sqlite3VtabOverloadFunction(pDef, nExpr, pList->a[1].pExpr);
1925 }else if( nExpr>0 ){
drhb7f6f682006-07-08 17:06:43 +00001926 pDef = sqlite3VtabOverloadFunction(pDef, nExpr, pList->a[0].pExpr);
1927 }
1928#endif
danielk1977682f68b2004-06-05 10:22:17 +00001929 for(i=0; i<nExpr && i<32; i++){
danielk1977d02eb1f2004-06-06 09:44:03 +00001930 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
drh13449892005-09-07 21:22:45 +00001931 constMask |= (1<<i);
danielk1977d02eb1f2004-06-06 09:44:03 +00001932 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001933 if( pDef->needCollSeq && !pColl ){
1934 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
1935 }
1936 }
1937 if( pDef->needCollSeq ){
1938 if( !pColl ) pColl = pParse->db->pDfltColl;
danielk1977d8123362004-06-12 09:25:12 +00001939 sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ);
danielk1977682f68b2004-06-05 10:22:17 +00001940 }
drh13449892005-09-07 21:22:45 +00001941 sqlite3VdbeOp3(v, OP_Function, constMask, nExpr, (char*)pDef, P3_FUNCDEF);
drhffe07b22005-11-03 00:41:17 +00001942 stackChng = 1-nExpr;
drhcce7d172000-05-31 15:34:51 +00001943 break;
1944 }
drhfe2093d2005-01-20 22:48:47 +00001945#ifndef SQLITE_OMIT_SUBQUERY
1946 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001947 case TK_SELECT: {
drh41714d62006-03-02 04:44:23 +00001948 if( pExpr->iColumn==0 ){
1949 sqlite3CodeSubselect(pParse, pExpr);
1950 }
danielk19774adee202004-05-08 08:23:19 +00001951 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drhad6d9462004-09-19 02:15:24 +00001952 VdbeComment((v, "# load subquery result"));
drh19a775c2000-06-05 18:54:46 +00001953 break;
1954 }
drhfef52082000-06-06 01:50:43 +00001955 case TK_IN: {
1956 int addr;
drh94a11212004-09-25 13:12:14 +00001957 char affinity;
drhafa5f682006-01-30 14:36:59 +00001958 int ckOffset = pParse->ckOffset;
danielk1977b3bce662005-01-29 08:32:43 +00001959 sqlite3CodeSubselect(pParse, pExpr);
danielk1977e014a832004-05-17 10:48:57 +00001960
1961 /* Figure out the affinity to use to create a key from the results
1962 ** of the expression. affinityStr stores a static string suitable for
danielk1977ededfd52004-06-17 07:53:01 +00001963 ** P3 of OP_MakeRecord.
danielk1977e014a832004-05-17 10:48:57 +00001964 */
drh94a11212004-09-25 13:12:14 +00001965 affinity = comparisonAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +00001966
danielk19774adee202004-05-08 08:23:19 +00001967 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
danielk1977cdbd8ef2007-05-12 06:11:12 +00001968 pParse->ckOffset = (ckOffset ? (ckOffset+1) : 0);
danielk1977e014a832004-05-17 10:48:57 +00001969
1970 /* Code the <expr> from "<expr> IN (...)". The temporary table
1971 ** pExpr->iTable contains the values that make up the (...) set.
1972 */
danielk19774adee202004-05-08 08:23:19 +00001973 sqlite3ExprCode(pParse, pExpr->pLeft);
1974 addr = sqlite3VdbeCurrentAddr(v);
danielk1977e014a832004-05-17 10:48:57 +00001975 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); /* addr + 0 */
danielk19774adee202004-05-08 08:23:19 +00001976 sqlite3VdbeAddOp(v, OP_Pop, 2, 0);
drhf0863fe2005-06-12 21:35:51 +00001977 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +00001978 sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7);
drh94a11212004-09-25 13:12:14 +00001979 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1); /* addr + 4 */
danielk1977e014a832004-05-17 10:48:57 +00001980 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7);
1981 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); /* addr + 6 */
1982
drhfef52082000-06-06 01:50:43 +00001983 break;
1984 }
danielk197793758c82005-01-21 08:13:14 +00001985#endif
drhfef52082000-06-06 01:50:43 +00001986 case TK_BETWEEN: {
drhbe5c89a2004-07-26 00:31:09 +00001987 Expr *pLeft = pExpr->pLeft;
1988 struct ExprList_item *pLItem = pExpr->pList->a;
1989 Expr *pRight = pLItem->pExpr;
1990 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001991 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001992 sqlite3ExprCode(pParse, pRight);
1993 codeCompare(pParse, pLeft, pRight, OP_Ge, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001994 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
drhbe5c89a2004-07-26 00:31:09 +00001995 pLItem++;
1996 pRight = pLItem->pExpr;
1997 sqlite3ExprCode(pParse, pRight);
1998 codeCompare(pParse, pLeft, pRight, OP_Le, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001999 sqlite3VdbeAddOp(v, OP_And, 0, 0);
drhfef52082000-06-06 01:50:43 +00002000 break;
2001 }
drh4f07e5f2007-05-14 11:34:46 +00002002 case TK_UPLUS: {
danielk19774adee202004-05-08 08:23:19 +00002003 sqlite3ExprCode(pParse, pExpr->pLeft);
drhffe07b22005-11-03 00:41:17 +00002004 stackChng = 0;
drha2e00042002-01-22 03:13:42 +00002005 break;
2006 }
drh17a7f8d2002-03-24 13:13:27 +00002007 case TK_CASE: {
2008 int expr_end_label;
drhf5905aa2002-05-26 20:54:33 +00002009 int jumpInst;
drhf5905aa2002-05-26 20:54:33 +00002010 int nExpr;
drh17a7f8d2002-03-24 13:13:27 +00002011 int i;
drhbe5c89a2004-07-26 00:31:09 +00002012 ExprList *pEList;
2013 struct ExprList_item *aListelem;
drh17a7f8d2002-03-24 13:13:27 +00002014
2015 assert(pExpr->pList);
2016 assert((pExpr->pList->nExpr % 2) == 0);
2017 assert(pExpr->pList->nExpr > 0);
drhbe5c89a2004-07-26 00:31:09 +00002018 pEList = pExpr->pList;
2019 aListelem = pEList->a;
2020 nExpr = pEList->nExpr;
danielk19774adee202004-05-08 08:23:19 +00002021 expr_end_label = sqlite3VdbeMakeLabel(v);
drh17a7f8d2002-03-24 13:13:27 +00002022 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00002023 sqlite3ExprCode(pParse, pExpr->pLeft);
drh17a7f8d2002-03-24 13:13:27 +00002024 }
drhf5905aa2002-05-26 20:54:33 +00002025 for(i=0; i<nExpr; i=i+2){
drhbe5c89a2004-07-26 00:31:09 +00002026 sqlite3ExprCode(pParse, aListelem[i].pExpr);
drh17a7f8d2002-03-24 13:13:27 +00002027 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00002028 sqlite3VdbeAddOp(v, OP_Dup, 1, 1);
drhbe5c89a2004-07-26 00:31:09 +00002029 jumpInst = codeCompare(pParse, pExpr->pLeft, aListelem[i].pExpr,
2030 OP_Ne, 0, 1);
danielk19774adee202004-05-08 08:23:19 +00002031 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00002032 }else{
danielk19774adee202004-05-08 08:23:19 +00002033 jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00002034 }
drhbe5c89a2004-07-26 00:31:09 +00002035 sqlite3ExprCode(pParse, aListelem[i+1].pExpr);
danielk19774adee202004-05-08 08:23:19 +00002036 sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label);
drhd654be82005-09-20 17:42:23 +00002037 sqlite3VdbeJumpHere(v, jumpInst);
drh17a7f8d2002-03-24 13:13:27 +00002038 }
drhf570f012002-05-31 15:51:25 +00002039 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00002040 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhf570f012002-05-31 15:51:25 +00002041 }
drh17a7f8d2002-03-24 13:13:27 +00002042 if( pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00002043 sqlite3ExprCode(pParse, pExpr->pRight);
drh17a7f8d2002-03-24 13:13:27 +00002044 }else{
drhf0863fe2005-06-12 21:35:51 +00002045 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
drh17a7f8d2002-03-24 13:13:27 +00002046 }
danielk19774adee202004-05-08 08:23:19 +00002047 sqlite3VdbeResolveLabel(v, expr_end_label);
danielk19776f349032002-06-11 02:25:40 +00002048 break;
2049 }
danielk19775338a5f2005-01-20 13:03:10 +00002050#ifndef SQLITE_OMIT_TRIGGER
danielk19776f349032002-06-11 02:25:40 +00002051 case TK_RAISE: {
2052 if( !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +00002053 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00002054 "RAISE() may only be used within a trigger-program");
danielk19776f349032002-06-11 02:25:40 +00002055 return;
2056 }
drhad6d9462004-09-19 02:15:24 +00002057 if( pExpr->iColumn!=OE_Ignore ){
2058 assert( pExpr->iColumn==OE_Rollback ||
2059 pExpr->iColumn == OE_Abort ||
2060 pExpr->iColumn == OE_Fail );
drhd2687b72005-08-12 22:56:09 +00002061 sqlite3DequoteExpr(pExpr);
drhad6d9462004-09-19 02:15:24 +00002062 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn,
drh2646da72005-12-09 20:02:05 +00002063 (char*)pExpr->token.z, pExpr->token.n);
danielk19776f349032002-06-11 02:25:40 +00002064 } else {
drhad6d9462004-09-19 02:15:24 +00002065 assert( pExpr->iColumn == OE_Ignore );
2066 sqlite3VdbeAddOp(v, OP_ContextPop, 0, 0);
2067 sqlite3VdbeAddOp(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
2068 VdbeComment((v, "# raise(IGNORE)"));
danielk19776f349032002-06-11 02:25:40 +00002069 }
drhffe07b22005-11-03 00:41:17 +00002070 stackChng = 0;
2071 break;
drh17a7f8d2002-03-24 13:13:27 +00002072 }
danielk19775338a5f2005-01-20 13:03:10 +00002073#endif
drhffe07b22005-11-03 00:41:17 +00002074 }
2075
2076 if( pParse->ckOffset ){
2077 pParse->ckOffset += stackChng;
2078 assert( pParse->ckOffset );
drhcce7d172000-05-31 15:34:51 +00002079 }
drhcce7d172000-05-31 15:34:51 +00002080}
2081
danielk197793758c82005-01-21 08:13:14 +00002082#ifndef SQLITE_OMIT_TRIGGER
drhcce7d172000-05-31 15:34:51 +00002083/*
drh25303782004-12-07 15:41:48 +00002084** Generate code that evalutes the given expression and leaves the result
2085** on the stack. See also sqlite3ExprCode().
2086**
2087** This routine might also cache the result and modify the pExpr tree
2088** so that it will make use of the cached result on subsequent evaluations
2089** rather than evaluate the whole expression again. Trivial expressions are
2090** not cached. If the expression is cached, its result is stored in a
2091** memory location.
2092*/
2093void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr){
2094 Vdbe *v = pParse->pVdbe;
2095 int iMem;
2096 int addr1, addr2;
2097 if( v==0 ) return;
2098 addr1 = sqlite3VdbeCurrentAddr(v);
2099 sqlite3ExprCode(pParse, pExpr);
2100 addr2 = sqlite3VdbeCurrentAddr(v);
2101 if( addr2>addr1+1 || sqlite3VdbeGetOp(v, addr1)->opcode==OP_Function ){
2102 iMem = pExpr->iTable = pParse->nMem++;
2103 sqlite3VdbeAddOp(v, OP_MemStore, iMem, 0);
2104 pExpr->op = TK_REGISTER;
2105 }
2106}
danielk197793758c82005-01-21 08:13:14 +00002107#endif
drh25303782004-12-07 15:41:48 +00002108
2109/*
drh268380c2004-02-25 13:47:31 +00002110** Generate code that pushes the value of every element of the given
drhf9b596e2004-05-26 16:54:42 +00002111** expression list onto the stack.
drh268380c2004-02-25 13:47:31 +00002112**
2113** Return the number of elements pushed onto the stack.
2114*/
danielk19774adee202004-05-08 08:23:19 +00002115int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00002116 Parse *pParse, /* Parsing context */
drhf9b596e2004-05-26 16:54:42 +00002117 ExprList *pList /* The expression list to be coded */
drh268380c2004-02-25 13:47:31 +00002118){
2119 struct ExprList_item *pItem;
2120 int i, n;
drh268380c2004-02-25 13:47:31 +00002121 if( pList==0 ) return 0;
drh268380c2004-02-25 13:47:31 +00002122 n = pList->nExpr;
drhc182d162005-08-14 20:47:16 +00002123 for(pItem=pList->a, i=n; i>0; i--, pItem++){
danielk19774adee202004-05-08 08:23:19 +00002124 sqlite3ExprCode(pParse, pItem->pExpr);
drh268380c2004-02-25 13:47:31 +00002125 }
drhf9b596e2004-05-26 16:54:42 +00002126 return n;
drh268380c2004-02-25 13:47:31 +00002127}
2128
2129/*
drhcce7d172000-05-31 15:34:51 +00002130** Generate code for a boolean expression such that a jump is made
2131** to the label "dest" if the expression is true but execution
2132** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00002133**
2134** If the expression evaluates to NULL (neither true nor false), then
2135** take the jump if the jumpIfNull flag is true.
drhf2bc0132004-10-04 13:19:23 +00002136**
2137** This code depends on the fact that certain token values (ex: TK_EQ)
2138** are the same as opcode values (ex: OP_Eq) that implement the corresponding
2139** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
2140** the make process cause these values to align. Assert()s in the code
2141** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00002142*/
danielk19774adee202004-05-08 08:23:19 +00002143void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00002144 Vdbe *v = pParse->pVdbe;
2145 int op = 0;
drhffe07b22005-11-03 00:41:17 +00002146 int ckOffset = pParse->ckOffset;
drhdaffd0e2001-04-11 14:28:42 +00002147 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00002148 op = pExpr->op;
2149 switch( op ){
drhcce7d172000-05-31 15:34:51 +00002150 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00002151 int d2 = sqlite3VdbeMakeLabel(v);
2152 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
2153 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
2154 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00002155 break;
2156 }
2157 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00002158 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
2159 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002160 break;
2161 }
2162 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00002163 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002164 break;
2165 }
2166 case TK_LT:
2167 case TK_LE:
2168 case TK_GT:
2169 case TK_GE:
2170 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00002171 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00002172 assert( TK_LT==OP_Lt );
2173 assert( TK_LE==OP_Le );
2174 assert( TK_GT==OP_Gt );
2175 assert( TK_GE==OP_Ge );
2176 assert( TK_EQ==OP_Eq );
2177 assert( TK_NE==OP_Ne );
danielk19774adee202004-05-08 08:23:19 +00002178 sqlite3ExprCode(pParse, pExpr->pLeft);
2179 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00002180 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002181 break;
2182 }
2183 case TK_ISNULL:
2184 case TK_NOTNULL: {
drhf2bc0132004-10-04 13:19:23 +00002185 assert( TK_ISNULL==OP_IsNull );
2186 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00002187 sqlite3ExprCode(pParse, pExpr->pLeft);
2188 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00002189 break;
2190 }
drhfef52082000-06-06 01:50:43 +00002191 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00002192 /* The expression "x BETWEEN y AND z" is implemented as:
2193 **
2194 ** 1 IF (x < y) GOTO 3
2195 ** 2 IF (x <= z) GOTO <dest>
2196 ** 3 ...
2197 */
drhf5905aa2002-05-26 20:54:33 +00002198 int addr;
drhbe5c89a2004-07-26 00:31:09 +00002199 Expr *pLeft = pExpr->pLeft;
2200 Expr *pRight = pExpr->pList->a[0].pExpr;
2201 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00002202 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00002203 sqlite3ExprCode(pParse, pRight);
2204 addr = codeCompare(pParse, pLeft, pRight, OP_Lt, 0, !jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00002205
drhbe5c89a2004-07-26 00:31:09 +00002206 pRight = pExpr->pList->a[1].pExpr;
2207 sqlite3ExprCode(pParse, pRight);
2208 codeCompare(pParse, pLeft, pRight, OP_Le, dest, jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00002209
danielk19774adee202004-05-08 08:23:19 +00002210 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
drhd654be82005-09-20 17:42:23 +00002211 sqlite3VdbeJumpHere(v, addr);
danielk19774adee202004-05-08 08:23:19 +00002212 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00002213 break;
2214 }
drhcce7d172000-05-31 15:34:51 +00002215 default: {
danielk19774adee202004-05-08 08:23:19 +00002216 sqlite3ExprCode(pParse, pExpr);
2217 sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00002218 break;
2219 }
2220 }
drhffe07b22005-11-03 00:41:17 +00002221 pParse->ckOffset = ckOffset;
drhcce7d172000-05-31 15:34:51 +00002222}
2223
2224/*
drh66b89c82000-11-28 20:47:17 +00002225** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00002226** to the label "dest" if the expression is false but execution
2227** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00002228**
2229** If the expression evaluates to NULL (neither true nor false) then
2230** jump if jumpIfNull is true or fall through if jumpIfNull is false.
drhcce7d172000-05-31 15:34:51 +00002231*/
danielk19774adee202004-05-08 08:23:19 +00002232void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00002233 Vdbe *v = pParse->pVdbe;
2234 int op = 0;
drhffe07b22005-11-03 00:41:17 +00002235 int ckOffset = pParse->ckOffset;
drhdaffd0e2001-04-11 14:28:42 +00002236 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00002237
2238 /* The value of pExpr->op and op are related as follows:
2239 **
2240 ** pExpr->op op
2241 ** --------- ----------
2242 ** TK_ISNULL OP_NotNull
2243 ** TK_NOTNULL OP_IsNull
2244 ** TK_NE OP_Eq
2245 ** TK_EQ OP_Ne
2246 ** TK_GT OP_Le
2247 ** TK_LE OP_Gt
2248 ** TK_GE OP_Lt
2249 ** TK_LT OP_Ge
2250 **
2251 ** For other values of pExpr->op, op is undefined and unused.
2252 ** The value of TK_ and OP_ constants are arranged such that we
2253 ** can compute the mapping above using the following expression.
2254 ** Assert()s verify that the computation is correct.
2255 */
2256 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
2257
2258 /* Verify correct alignment of TK_ and OP_ constants
2259 */
2260 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
2261 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
2262 assert( pExpr->op!=TK_NE || op==OP_Eq );
2263 assert( pExpr->op!=TK_EQ || op==OP_Ne );
2264 assert( pExpr->op!=TK_LT || op==OP_Ge );
2265 assert( pExpr->op!=TK_LE || op==OP_Gt );
2266 assert( pExpr->op!=TK_GT || op==OP_Le );
2267 assert( pExpr->op!=TK_GE || op==OP_Lt );
2268
drhcce7d172000-05-31 15:34:51 +00002269 switch( pExpr->op ){
2270 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00002271 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
2272 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002273 break;
2274 }
2275 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00002276 int d2 = sqlite3VdbeMakeLabel(v);
2277 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
2278 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
2279 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00002280 break;
2281 }
2282 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00002283 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002284 break;
2285 }
2286 case TK_LT:
2287 case TK_LE:
2288 case TK_GT:
2289 case TK_GE:
2290 case TK_NE:
2291 case TK_EQ: {
danielk19774adee202004-05-08 08:23:19 +00002292 sqlite3ExprCode(pParse, pExpr->pLeft);
2293 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00002294 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002295 break;
2296 }
drhcce7d172000-05-31 15:34:51 +00002297 case TK_ISNULL:
2298 case TK_NOTNULL: {
danielk19774adee202004-05-08 08:23:19 +00002299 sqlite3ExprCode(pParse, pExpr->pLeft);
2300 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00002301 break;
2302 }
drhfef52082000-06-06 01:50:43 +00002303 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00002304 /* The expression is "x BETWEEN y AND z". It is implemented as:
2305 **
2306 ** 1 IF (x >= y) GOTO 3
2307 ** 2 GOTO <dest>
2308 ** 3 IF (x > z) GOTO <dest>
2309 */
drhfef52082000-06-06 01:50:43 +00002310 int addr;
drhbe5c89a2004-07-26 00:31:09 +00002311 Expr *pLeft = pExpr->pLeft;
2312 Expr *pRight = pExpr->pList->a[0].pExpr;
2313 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00002314 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00002315 sqlite3ExprCode(pParse, pRight);
danielk19774adee202004-05-08 08:23:19 +00002316 addr = sqlite3VdbeCurrentAddr(v);
drhbe5c89a2004-07-26 00:31:09 +00002317 codeCompare(pParse, pLeft, pRight, OP_Ge, addr+3, !jumpIfNull);
2318
danielk19774adee202004-05-08 08:23:19 +00002319 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
2320 sqlite3VdbeAddOp(v, OP_Goto, 0, dest);
drhbe5c89a2004-07-26 00:31:09 +00002321 pRight = pExpr->pList->a[1].pExpr;
2322 sqlite3ExprCode(pParse, pRight);
2323 codeCompare(pParse, pLeft, pRight, OP_Gt, dest, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00002324 break;
2325 }
drhcce7d172000-05-31 15:34:51 +00002326 default: {
danielk19774adee202004-05-08 08:23:19 +00002327 sqlite3ExprCode(pParse, pExpr);
2328 sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00002329 break;
2330 }
2331 }
drhffe07b22005-11-03 00:41:17 +00002332 pParse->ckOffset = ckOffset;
drhcce7d172000-05-31 15:34:51 +00002333}
drh22827922000-06-06 17:27:05 +00002334
2335/*
2336** Do a deep comparison of two expression trees. Return TRUE (non-zero)
2337** if they are identical and return FALSE if they differ in any way.
drhd40aab02007-02-24 15:29:03 +00002338**
2339** Sometimes this routine will return FALSE even if the two expressions
2340** really are equivalent. If we cannot prove that the expressions are
2341** identical, we return FALSE just to be safe. So if this routine
2342** returns false, then you do not really know for certain if the two
2343** expressions are the same. But if you get a TRUE return, then you
2344** can be sure the expressions are the same. In the places where
2345** this routine is used, it does not hurt to get an extra FALSE - that
2346** just might result in some slightly slower code. But returning
2347** an incorrect TRUE could lead to a malfunction.
drh22827922000-06-06 17:27:05 +00002348*/
danielk19774adee202004-05-08 08:23:19 +00002349int sqlite3ExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00002350 int i;
danielk19774b202ae2006-01-23 05:50:58 +00002351 if( pA==0||pB==0 ){
2352 return pB==pA;
drh22827922000-06-06 17:27:05 +00002353 }
2354 if( pA->op!=pB->op ) return 0;
drhfd357972005-09-09 01:33:19 +00002355 if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 0;
danielk19774adee202004-05-08 08:23:19 +00002356 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
2357 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00002358 if( pA->pList ){
2359 if( pB->pList==0 ) return 0;
2360 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
2361 for(i=0; i<pA->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00002362 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00002363 return 0;
2364 }
2365 }
2366 }else if( pB->pList ){
2367 return 0;
2368 }
2369 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00002370 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drhdd735212007-02-24 13:53:05 +00002371 if( pA->op!=TK_COLUMN && pA->token.z ){
drh22827922000-06-06 17:27:05 +00002372 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00002373 if( pB->token.n!=pA->token.n ) return 0;
drh2646da72005-12-09 20:02:05 +00002374 if( sqlite3StrNICmp((char*)pA->token.z,(char*)pB->token.z,pB->token.n)!=0 ){
2375 return 0;
2376 }
drh22827922000-06-06 17:27:05 +00002377 }
2378 return 1;
2379}
2380
drh13449892005-09-07 21:22:45 +00002381
drh22827922000-06-06 17:27:05 +00002382/*
drh13449892005-09-07 21:22:45 +00002383** Add a new element to the pAggInfo->aCol[] array. Return the index of
2384** the new element. Return a negative number if malloc fails.
drh22827922000-06-06 17:27:05 +00002385*/
drh13449892005-09-07 21:22:45 +00002386static int addAggInfoColumn(AggInfo *pInfo){
2387 int i;
drhcf643722007-03-27 13:36:37 +00002388 pInfo->aCol = sqlite3ArrayAllocate(
2389 pInfo->aCol,
2390 sizeof(pInfo->aCol[0]),
2391 3,
2392 &pInfo->nColumn,
2393 &pInfo->nColumnAlloc,
2394 &i
2395 );
drh13449892005-09-07 21:22:45 +00002396 return i;
2397}
2398
2399/*
2400** Add a new element to the pAggInfo->aFunc[] array. Return the index of
2401** the new element. Return a negative number if malloc fails.
2402*/
2403static int addAggInfoFunc(AggInfo *pInfo){
2404 int i;
drhcf643722007-03-27 13:36:37 +00002405 pInfo->aFunc = sqlite3ArrayAllocate(
2406 pInfo->aFunc,
2407 sizeof(pInfo->aFunc[0]),
2408 3,
2409 &pInfo->nFunc,
2410 &pInfo->nFuncAlloc,
2411 &i
2412 );
drh13449892005-09-07 21:22:45 +00002413 return i;
2414}
drh22827922000-06-06 17:27:05 +00002415
2416/*
drh626a8792005-01-17 22:08:19 +00002417** This is an xFunc for walkExprTree() used to implement
2418** sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates
2419** for additional information.
drh22827922000-06-06 17:27:05 +00002420**
drh626a8792005-01-17 22:08:19 +00002421** This routine analyzes the aggregate function at pExpr.
drh22827922000-06-06 17:27:05 +00002422*/
drh626a8792005-01-17 22:08:19 +00002423static int analyzeAggregate(void *pArg, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00002424 int i;
danielk1977a58fdfb2005-02-08 07:50:40 +00002425 NameContext *pNC = (NameContext *)pArg;
2426 Parse *pParse = pNC->pParse;
2427 SrcList *pSrcList = pNC->pSrcList;
drh13449892005-09-07 21:22:45 +00002428 AggInfo *pAggInfo = pNC->pAggInfo;
2429
drh22827922000-06-06 17:27:05 +00002430
drh22827922000-06-06 17:27:05 +00002431 switch( pExpr->op ){
drh89c69d02007-01-04 01:20:28 +00002432 case TK_AGG_COLUMN:
drh967e8b72000-06-21 13:59:10 +00002433 case TK_COLUMN: {
drh13449892005-09-07 21:22:45 +00002434 /* Check to see if the column is in one of the tables in the FROM
2435 ** clause of the aggregate query */
2436 if( pSrcList ){
2437 struct SrcList_item *pItem = pSrcList->a;
2438 for(i=0; i<pSrcList->nSrc; i++, pItem++){
2439 struct AggInfo_col *pCol;
2440 if( pExpr->iTable==pItem->iCursor ){
2441 /* If we reach this point, it means that pExpr refers to a table
2442 ** that is in the FROM clause of the aggregate query.
2443 **
2444 ** Make an entry for the column in pAggInfo->aCol[] if there
2445 ** is not an entry there already.
2446 */
drh7f906d62007-03-12 23:48:52 +00002447 int k;
drh13449892005-09-07 21:22:45 +00002448 pCol = pAggInfo->aCol;
drh7f906d62007-03-12 23:48:52 +00002449 for(k=0; k<pAggInfo->nColumn; k++, pCol++){
drh13449892005-09-07 21:22:45 +00002450 if( pCol->iTable==pExpr->iTable &&
2451 pCol->iColumn==pExpr->iColumn ){
2452 break;
2453 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002454 }
drh7f906d62007-03-12 23:48:52 +00002455 if( k>=pAggInfo->nColumn && (k = addAggInfoColumn(pAggInfo))>=0 ){
2456 pCol = &pAggInfo->aCol[k];
danielk19770817d0d2007-02-14 09:19:36 +00002457 pCol->pTab = pExpr->pTab;
drh13449892005-09-07 21:22:45 +00002458 pCol->iTable = pExpr->iTable;
2459 pCol->iColumn = pExpr->iColumn;
2460 pCol->iMem = pParse->nMem++;
2461 pCol->iSorterColumn = -1;
drh5774b802005-09-07 22:48:16 +00002462 pCol->pExpr = pExpr;
drh13449892005-09-07 21:22:45 +00002463 if( pAggInfo->pGroupBy ){
2464 int j, n;
2465 ExprList *pGB = pAggInfo->pGroupBy;
2466 struct ExprList_item *pTerm = pGB->a;
2467 n = pGB->nExpr;
2468 for(j=0; j<n; j++, pTerm++){
2469 Expr *pE = pTerm->pExpr;
2470 if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable &&
2471 pE->iColumn==pExpr->iColumn ){
2472 pCol->iSorterColumn = j;
2473 break;
2474 }
2475 }
2476 }
2477 if( pCol->iSorterColumn<0 ){
2478 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
2479 }
2480 }
2481 /* There is now an entry for pExpr in pAggInfo->aCol[] (either
2482 ** because it was there before or because we just created it).
2483 ** Convert the pExpr to be a TK_AGG_COLUMN referring to that
2484 ** pAggInfo->aCol[] entry.
2485 */
2486 pExpr->pAggInfo = pAggInfo;
2487 pExpr->op = TK_AGG_COLUMN;
drh7f906d62007-03-12 23:48:52 +00002488 pExpr->iAgg = k;
drh13449892005-09-07 21:22:45 +00002489 break;
2490 } /* endif pExpr->iTable==pItem->iCursor */
2491 } /* end loop over pSrcList */
drh22827922000-06-06 17:27:05 +00002492 }
drh626a8792005-01-17 22:08:19 +00002493 return 1;
drh22827922000-06-06 17:27:05 +00002494 }
2495 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00002496 /* The pNC->nDepth==0 test causes aggregate functions in subqueries
2497 ** to be ignored */
danielk1977a58fdfb2005-02-08 07:50:40 +00002498 if( pNC->nDepth==0 ){
drh13449892005-09-07 21:22:45 +00002499 /* Check to see if pExpr is a duplicate of another aggregate
2500 ** function that is already in the pAggInfo structure
2501 */
2502 struct AggInfo_func *pItem = pAggInfo->aFunc;
2503 for(i=0; i<pAggInfo->nFunc; i++, pItem++){
2504 if( sqlite3ExprCompare(pItem->pExpr, pExpr) ){
danielk1977a58fdfb2005-02-08 07:50:40 +00002505 break;
2506 }
drh22827922000-06-06 17:27:05 +00002507 }
drh13449892005-09-07 21:22:45 +00002508 if( i>=pAggInfo->nFunc ){
2509 /* pExpr is original. Make a new entry in pAggInfo->aFunc[]
2510 */
danielk197714db2662006-01-09 16:12:04 +00002511 u8 enc = ENC(pParse->db);
drh13449892005-09-07 21:22:45 +00002512 i = addAggInfoFunc(pAggInfo);
2513 if( i>=0 ){
2514 pItem = &pAggInfo->aFunc[i];
2515 pItem->pExpr = pExpr;
2516 pItem->iMem = pParse->nMem++;
2517 pItem->pFunc = sqlite3FindFunction(pParse->db,
drh2646da72005-12-09 20:02:05 +00002518 (char*)pExpr->token.z, pExpr->token.n,
drh13449892005-09-07 21:22:45 +00002519 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
drhfd357972005-09-09 01:33:19 +00002520 if( pExpr->flags & EP_Distinct ){
2521 pItem->iDistinct = pParse->nTab++;
2522 }else{
2523 pItem->iDistinct = -1;
2524 }
drh13449892005-09-07 21:22:45 +00002525 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002526 }
drh13449892005-09-07 21:22:45 +00002527 /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
2528 */
danielk1977a58fdfb2005-02-08 07:50:40 +00002529 pExpr->iAgg = i;
drh13449892005-09-07 21:22:45 +00002530 pExpr->pAggInfo = pAggInfo;
danielk1977a58fdfb2005-02-08 07:50:40 +00002531 return 1;
drh22827922000-06-06 17:27:05 +00002532 }
drh22827922000-06-06 17:27:05 +00002533 }
2534 }
drh13449892005-09-07 21:22:45 +00002535
2536 /* Recursively walk subqueries looking for TK_COLUMN nodes that need
2537 ** to be changed to TK_AGG_COLUMN. But increment nDepth so that
2538 ** TK_AGG_FUNCTION nodes in subqueries will be unchanged.
2539 */
danielk1977a58fdfb2005-02-08 07:50:40 +00002540 if( pExpr->pSelect ){
2541 pNC->nDepth++;
2542 walkSelectExpr(pExpr->pSelect, analyzeAggregate, pNC);
2543 pNC->nDepth--;
2544 }
drh626a8792005-01-17 22:08:19 +00002545 return 0;
2546}
2547
2548/*
2549** Analyze the given expression looking for aggregate functions and
2550** for variables that need to be added to the pParse->aAgg[] array.
2551** Make additional entries to the pParse->aAgg[] array as necessary.
2552**
2553** This routine should only be called after the expression has been
2554** analyzed by sqlite3ExprResolveNames().
2555**
2556** If errors are seen, leave an error message in zErrMsg and return
2557** the number of errors.
2558*/
danielk1977a58fdfb2005-02-08 07:50:40 +00002559int sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
2560 int nErr = pNC->pParse->nErr;
2561 walkExprTree(pExpr, analyzeAggregate, pNC);
2562 return pNC->pParse->nErr - nErr;
drh22827922000-06-06 17:27:05 +00002563}
drh5d9a4af2005-08-30 00:54:01 +00002564
2565/*
2566** Call sqlite3ExprAnalyzeAggregates() for every expression in an
2567** expression list. Return the number of errors.
2568**
2569** If an error is found, the analysis is cut short.
2570*/
2571int sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){
2572 struct ExprList_item *pItem;
2573 int i;
2574 int nErr = 0;
2575 if( pList ){
2576 for(pItem=pList->a, i=0; nErr==0 && i<pList->nExpr; i++, pItem++){
2577 nErr += sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
2578 }
2579 }
2580 return nErr;
2581}