blob: 7b34352c944319bf673b1d80854e25237c3902d4 [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**
drh8b4c40d2007-02-01 23:02:45 +000015** $Id: expr.c,v 1.273 2007/02/01 23:02:45 drh Exp $
drhcce7d172000-05-31 15:34:51 +000016*/
17#include "sqliteInt.h"
drh04738cb2002-06-02 18:19:00 +000018#include <ctype.h>
drha2e00042002-01-22 03:13:42 +000019
danielk1977e014a832004-05-17 10:48:57 +000020/*
21** Return the 'affinity' of the expression pExpr if any.
22**
23** If pExpr is a column, a reference to a column via an 'AS' alias,
24** or a sub-select with a column as the return value, then the
25** affinity of that column is returned. Otherwise, 0x00 is returned,
26** indicating no affinity for the expression.
27**
28** i.e. the WHERE clause expresssions in the following statements all
29** have an affinity:
30**
31** CREATE TABLE t1(a);
32** SELECT * FROM t1 WHERE a;
33** SELECT a AS b FROM t1 WHERE b;
34** SELECT * FROM t1 WHERE (select a from t1);
35*/
danielk1977bf3b7212004-05-18 10:06:24 +000036char sqlite3ExprAffinity(Expr *pExpr){
drh487e2622005-06-25 18:42:14 +000037 int op = pExpr->op;
38 if( op==TK_AS ){
danielk1977bf3b7212004-05-18 10:06:24 +000039 return sqlite3ExprAffinity(pExpr->pLeft);
danielk1977a37cdde2004-05-16 11:15:36 +000040 }
drh487e2622005-06-25 18:42:14 +000041 if( op==TK_SELECT ){
danielk1977bf3b7212004-05-18 10:06:24 +000042 return sqlite3ExprAffinity(pExpr->pSelect->pEList->a[0].pExpr);
danielk1977a37cdde2004-05-16 11:15:36 +000043 }
drh487e2622005-06-25 18:42:14 +000044#ifndef SQLITE_OMIT_CAST
45 if( op==TK_CAST ){
drh8a512562005-11-14 22:29:05 +000046 return sqlite3AffinityType(&pExpr->token);
drh487e2622005-06-25 18:42:14 +000047 }
48#endif
danielk1977a37cdde2004-05-16 11:15:36 +000049 return pExpr->affinity;
50}
51
drh53db1452004-05-20 13:54:53 +000052/*
drh8b4c40d2007-02-01 23:02:45 +000053** Set the collating sequence for expression pExpr to be the collating
54** sequence named by pToken. Return a pointer to the revised expression.
55*/
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 ){
danielk19777cedc8d2004-06-10 10:50:08 +000074 pColl = pExpr->pColl;
drh487e2622005-06-25 18:42:14 +000075 if( (pExpr->op==TK_AS || pExpr->op==TK_CAST) && !pColl ){
danielk19777cedc8d2004-06-10 10:50:08 +000076 return sqlite3ExprCollSeq(pParse, pExpr->pLeft);
danielk19770202b292004-06-09 09:55:16 +000077 }
78 }
danielk19777cedc8d2004-06-10 10:50:08 +000079 if( sqlite3CheckCollSeq(pParse, pColl) ){
80 pColl = 0;
81 }
82 return pColl;
danielk19770202b292004-06-09 09:55:16 +000083}
84
85/*
drh626a8792005-01-17 22:08:19 +000086** pExpr is an operand of a comparison operator. aff2 is the
87** type affinity of the other operand. This routine returns the
drh53db1452004-05-20 13:54:53 +000088** type affinity that should be used for the comparison operator.
89*/
danielk1977e014a832004-05-17 10:48:57 +000090char sqlite3CompareAffinity(Expr *pExpr, char aff2){
danielk1977bf3b7212004-05-18 10:06:24 +000091 char aff1 = sqlite3ExprAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +000092 if( aff1 && aff2 ){
drh8df447f2005-11-01 15:48:24 +000093 /* Both sides of the comparison are columns. If one has numeric
94 ** affinity, use that. Otherwise use no affinity.
danielk1977e014a832004-05-17 10:48:57 +000095 */
drh8a512562005-11-14 22:29:05 +000096 if( sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2) ){
danielk1977e014a832004-05-17 10:48:57 +000097 return SQLITE_AFF_NUMERIC;
98 }else{
99 return SQLITE_AFF_NONE;
100 }
101 }else if( !aff1 && !aff2 ){
drh5f6a87b2004-07-19 00:39:45 +0000102 /* Neither side of the comparison is a column. Compare the
103 ** results directly.
danielk1977e014a832004-05-17 10:48:57 +0000104 */
drh5f6a87b2004-07-19 00:39:45 +0000105 return SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +0000106 }else{
107 /* One side is a column, the other is not. Use the columns affinity. */
drhfe05af82005-07-21 03:14:59 +0000108 assert( aff1==0 || aff2==0 );
danielk1977e014a832004-05-17 10:48:57 +0000109 return (aff1 + aff2);
110 }
111}
112
drh53db1452004-05-20 13:54:53 +0000113/*
114** pExpr is a comparison operator. Return the type affinity that should
115** be applied to both operands prior to doing the comparison.
116*/
danielk1977e014a832004-05-17 10:48:57 +0000117static char comparisonAffinity(Expr *pExpr){
118 char aff;
119 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
120 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
121 pExpr->op==TK_NE );
122 assert( pExpr->pLeft );
danielk1977bf3b7212004-05-18 10:06:24 +0000123 aff = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +0000124 if( pExpr->pRight ){
125 aff = sqlite3CompareAffinity(pExpr->pRight, aff);
126 }
127 else if( pExpr->pSelect ){
128 aff = sqlite3CompareAffinity(pExpr->pSelect->pEList->a[0].pExpr, aff);
129 }
130 else if( !aff ){
131 aff = SQLITE_AFF_NUMERIC;
132 }
133 return aff;
134}
135
136/*
137** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
138** idx_affinity is the affinity of an indexed column. Return true
139** if the index with affinity idx_affinity may be used to implement
140** the comparison in pExpr.
141*/
142int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){
143 char aff = comparisonAffinity(pExpr);
drh8a512562005-11-14 22:29:05 +0000144 switch( aff ){
145 case SQLITE_AFF_NONE:
146 return 1;
147 case SQLITE_AFF_TEXT:
148 return idx_affinity==SQLITE_AFF_TEXT;
149 default:
150 return sqlite3IsNumericAffinity(idx_affinity);
151 }
danielk1977e014a832004-05-17 10:48:57 +0000152}
153
danielk1977a37cdde2004-05-16 11:15:36 +0000154/*
155** Return the P1 value that should be used for a binary comparison
156** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
157** If jumpIfNull is true, then set the low byte of the returned
158** P1 value to tell the opcode to jump if either expression
159** evaluates to NULL.
160*/
danielk1977e014a832004-05-17 10:48:57 +0000161static int binaryCompareP1(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){
danielk1977bf3b7212004-05-18 10:06:24 +0000162 char aff = sqlite3ExprAffinity(pExpr2);
drhf0863fe2005-06-12 21:35:51 +0000163 return ((int)sqlite3CompareAffinity(pExpr1, aff))+(jumpIfNull?0x100:0);
danielk1977a37cdde2004-05-16 11:15:36 +0000164}
165
drha2e00042002-01-22 03:13:42 +0000166/*
danielk19770202b292004-06-09 09:55:16 +0000167** Return a pointer to the collation sequence that should be used by
168** a binary comparison operator comparing pLeft and pRight.
169**
170** If the left hand expression has a collating sequence type, then it is
171** used. Otherwise the collation sequence for the right hand expression
172** is used, or the default (BINARY) if neither expression has a collating
173** type.
174*/
danielk19777cedc8d2004-06-10 10:50:08 +0000175static CollSeq* binaryCompareCollSeq(Parse *pParse, Expr *pLeft, Expr *pRight){
176 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pLeft);
danielk19770202b292004-06-09 09:55:16 +0000177 if( !pColl ){
danielk19777cedc8d2004-06-10 10:50:08 +0000178 pColl = sqlite3ExprCollSeq(pParse, pRight);
danielk19770202b292004-06-09 09:55:16 +0000179 }
180 return pColl;
181}
182
183/*
drhbe5c89a2004-07-26 00:31:09 +0000184** Generate code for a comparison operator.
185*/
186static int codeCompare(
187 Parse *pParse, /* The parsing (and code generating) context */
188 Expr *pLeft, /* The left operand */
189 Expr *pRight, /* The right operand */
190 int opcode, /* The comparison opcode */
191 int dest, /* Jump here if true. */
192 int jumpIfNull /* If true, jump if either operand is NULL */
193){
194 int p1 = binaryCompareP1(pLeft, pRight, jumpIfNull);
195 CollSeq *p3 = binaryCompareCollSeq(pParse, pLeft, pRight);
drh94a11212004-09-25 13:12:14 +0000196 return sqlite3VdbeOp3(pParse->pVdbe, opcode, p1, dest, (void*)p3, P3_COLLSEQ);
drhbe5c89a2004-07-26 00:31:09 +0000197}
198
199/*
drha76b5df2002-02-23 02:32:10 +0000200** Construct a new expression node and return a pointer to it. Memory
201** for this node is obtained from sqliteMalloc(). The calling function
202** is responsible for making sure the node eventually gets freed.
203*/
drhe4e72072004-11-23 01:47:30 +0000204Expr *sqlite3Expr(int op, Expr *pLeft, Expr *pRight, const Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000205 Expr *pNew;
206 pNew = sqliteMalloc( sizeof(Expr) );
207 if( pNew==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000208 /* When malloc fails, delete pLeft and pRight. Expressions passed to
209 ** this function must always be allocated with sqlite3Expr() for this
210 ** reason.
211 */
212 sqlite3ExprDelete(pLeft);
213 sqlite3ExprDelete(pRight);
drha76b5df2002-02-23 02:32:10 +0000214 return 0;
215 }
216 pNew->op = op;
217 pNew->pLeft = pLeft;
218 pNew->pRight = pRight;
danielk1977a58fdfb2005-02-08 07:50:40 +0000219 pNew->iAgg = -1;
drha76b5df2002-02-23 02:32:10 +0000220 if( pToken ){
drh4b59ab52002-08-24 18:24:51 +0000221 assert( pToken->dyn==0 );
drh145716b2004-09-24 12:24:06 +0000222 pNew->span = pNew->token = *pToken;
223 }else if( pLeft && pRight ){
224 sqlite3ExprSpan(pNew, &pLeft->span, &pRight->span);
drha76b5df2002-02-23 02:32:10 +0000225 }
drha76b5df2002-02-23 02:32:10 +0000226 return pNew;
227}
228
229/*
drh206f3d92006-07-11 13:15:08 +0000230** Works like sqlite3Expr() but frees its pLeft and pRight arguments
231** if it fails due to a malloc problem.
232*/
233Expr *sqlite3ExprOrFree(int op, Expr *pLeft, Expr *pRight, const Token *pToken){
234 Expr *pNew = sqlite3Expr(op, pLeft, pRight, pToken);
235 if( pNew==0 ){
236 sqlite3ExprDelete(pLeft);
237 sqlite3ExprDelete(pRight);
238 }
239 return pNew;
240}
241
242/*
drh4e0cff62004-11-05 05:10:28 +0000243** When doing a nested parse, you can include terms in an expression
244** that look like this: #0 #1 #2 ... These terms refer to elements
drh288d37f2005-06-22 08:48:06 +0000245** on the stack. "#0" means the top of the stack.
246** "#1" means the next down on the stack. And so forth.
drh4e0cff62004-11-05 05:10:28 +0000247**
248** This routine is called by the parser to deal with on of those terms.
249** It immediately generates code to store the value in a memory location.
250** The returns an expression that will code to extract the value from
251** that memory location as needed.
252*/
253Expr *sqlite3RegisterExpr(Parse *pParse, Token *pToken){
254 Vdbe *v = pParse->pVdbe;
255 Expr *p;
256 int depth;
drh4e0cff62004-11-05 05:10:28 +0000257 if( pParse->nested==0 ){
258 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", pToken);
259 return 0;
260 }
drhbb7ac002005-08-12 22:58:53 +0000261 if( v==0 ) return 0;
drh4e0cff62004-11-05 05:10:28 +0000262 p = sqlite3Expr(TK_REGISTER, 0, 0, pToken);
drh73c42a12004-11-20 18:13:10 +0000263 if( p==0 ){
264 return 0; /* Malloc failed */
265 }
drh2646da72005-12-09 20:02:05 +0000266 depth = atoi((char*)&pToken->z[1]);
drh288d37f2005-06-22 08:48:06 +0000267 p->iTable = pParse->nMem++;
268 sqlite3VdbeAddOp(v, OP_Dup, depth, 0);
269 sqlite3VdbeAddOp(v, OP_MemStore, p->iTable, 1);
drh4e0cff62004-11-05 05:10:28 +0000270 return p;
271}
272
273/*
drh91bb0ee2004-09-01 03:06:34 +0000274** Join two expressions using an AND operator. If either expression is
275** NULL, then just return the other expression.
276*/
277Expr *sqlite3ExprAnd(Expr *pLeft, Expr *pRight){
278 if( pLeft==0 ){
279 return pRight;
280 }else if( pRight==0 ){
281 return pLeft;
282 }else{
283 return sqlite3Expr(TK_AND, pLeft, pRight, 0);
284 }
285}
286
287/*
drh6977fea2002-10-22 23:38:04 +0000288** Set the Expr.span field of the given expression to span all
drha76b5df2002-02-23 02:32:10 +0000289** text between the two given tokens.
290*/
danielk19774adee202004-05-08 08:23:19 +0000291void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
drh4efc4752004-01-16 15:55:37 +0000292 assert( pRight!=0 );
293 assert( pLeft!=0 );
danielk19779e128002006-01-18 16:51:35 +0000294 if( !sqlite3MallocFailed() && pRight->z && pLeft->z ){
drhad6d9462004-09-19 02:15:24 +0000295 assert( pLeft->dyn==0 || pLeft->z[pLeft->n]==0 );
drh145716b2004-09-24 12:24:06 +0000296 if( pLeft->dyn==0 && pRight->dyn==0 ){
drh6977fea2002-10-22 23:38:04 +0000297 pExpr->span.z = pLeft->z;
drh97903fe2005-05-24 20:19:57 +0000298 pExpr->span.n = pRight->n + (pRight->z - pLeft->z);
drh4b59ab52002-08-24 18:24:51 +0000299 }else{
drh6977fea2002-10-22 23:38:04 +0000300 pExpr->span.z = 0;
drh4b59ab52002-08-24 18:24:51 +0000301 }
drha76b5df2002-02-23 02:32:10 +0000302 }
303}
304
305/*
306** Construct a new expression node for a function with multiple
307** arguments.
308*/
danielk19774adee202004-05-08 08:23:19 +0000309Expr *sqlite3ExprFunction(ExprList *pList, Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000310 Expr *pNew;
danielk19774b202ae2006-01-23 05:50:58 +0000311 assert( pToken );
drha76b5df2002-02-23 02:32:10 +0000312 pNew = sqliteMalloc( sizeof(Expr) );
313 if( pNew==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000314 sqlite3ExprListDelete(pList); /* Avoid leaking memory when malloc fails */
drha76b5df2002-02-23 02:32:10 +0000315 return 0;
316 }
317 pNew->op = TK_FUNCTION;
318 pNew->pList = pList;
danielk19774b202ae2006-01-23 05:50:58 +0000319 assert( pToken->dyn==0 );
320 pNew->token = *pToken;
drh6977fea2002-10-22 23:38:04 +0000321 pNew->span = pNew->token;
drha76b5df2002-02-23 02:32:10 +0000322 return pNew;
323}
324
325/*
drhfa6bc002004-09-07 16:19:52 +0000326** Assign a variable number to an expression that encodes a wildcard
327** in the original SQL statement.
328**
329** Wildcards consisting of a single "?" are assigned the next sequential
330** variable number.
331**
332** Wildcards of the form "?nnn" are assigned the number "nnn". We make
333** sure "nnn" is not too be to avoid a denial of service attack when
334** the SQL statement comes from an external source.
335**
336** Wildcards of the form ":aaa" or "$aaa" are assigned the same number
337** as the previous instance of the same wildcard. Or if this is the first
338** instance of the wildcard, the next sequenial variable number is
339** assigned.
340*/
341void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){
342 Token *pToken;
343 if( pExpr==0 ) return;
344 pToken = &pExpr->token;
345 assert( pToken->n>=1 );
346 assert( pToken->z!=0 );
347 assert( pToken->z[0]!=0 );
348 if( pToken->n==1 ){
349 /* Wildcard of the form "?". Assign the next variable number */
350 pExpr->iTable = ++pParse->nVar;
351 }else if( pToken->z[0]=='?' ){
352 /* Wildcard of the form "?nnn". Convert "nnn" to an integer and
353 ** use it as the variable number */
354 int i;
drh2646da72005-12-09 20:02:05 +0000355 pExpr->iTable = i = atoi((char*)&pToken->z[1]);
drhfa6bc002004-09-07 16:19:52 +0000356 if( i<1 || i>SQLITE_MAX_VARIABLE_NUMBER ){
357 sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
358 SQLITE_MAX_VARIABLE_NUMBER);
359 }
360 if( i>pParse->nVar ){
361 pParse->nVar = i;
362 }
363 }else{
364 /* Wildcards of the form ":aaa" or "$aaa". Reuse the same variable
365 ** number as the prior appearance of the same name, or if the name
366 ** has never appeared before, reuse the same variable number
367 */
368 int i, n;
369 n = pToken->n;
370 for(i=0; i<pParse->nVarExpr; i++){
371 Expr *pE;
372 if( (pE = pParse->apVarExpr[i])!=0
373 && pE->token.n==n
374 && memcmp(pE->token.z, pToken->z, n)==0 ){
375 pExpr->iTable = pE->iTable;
376 break;
377 }
378 }
379 if( i>=pParse->nVarExpr ){
380 pExpr->iTable = ++pParse->nVar;
381 if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){
382 pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10;
danielk1977e7259292006-01-13 06:33:23 +0000383 sqliteReallocOrFree((void**)&pParse->apVarExpr,
drhfa6bc002004-09-07 16:19:52 +0000384 pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0]) );
385 }
danielk19779e128002006-01-18 16:51:35 +0000386 if( !sqlite3MallocFailed() ){
drhfa6bc002004-09-07 16:19:52 +0000387 assert( pParse->apVarExpr!=0 );
388 pParse->apVarExpr[pParse->nVarExpr++] = pExpr;
389 }
390 }
391 }
392}
393
394/*
drha2e00042002-01-22 03:13:42 +0000395** Recursively delete an expression tree.
396*/
danielk19774adee202004-05-08 08:23:19 +0000397void sqlite3ExprDelete(Expr *p){
drha2e00042002-01-22 03:13:42 +0000398 if( p==0 ) return;
drh4efc4752004-01-16 15:55:37 +0000399 if( p->span.dyn ) sqliteFree((char*)p->span.z);
400 if( p->token.dyn ) sqliteFree((char*)p->token.z);
danielk19774adee202004-05-08 08:23:19 +0000401 sqlite3ExprDelete(p->pLeft);
402 sqlite3ExprDelete(p->pRight);
403 sqlite3ExprListDelete(p->pList);
404 sqlite3SelectDelete(p->pSelect);
drha2e00042002-01-22 03:13:42 +0000405 sqliteFree(p);
406}
407
drhd2687b72005-08-12 22:56:09 +0000408/*
409** The Expr.token field might be a string literal that is quoted.
410** If so, remove the quotation marks.
411*/
412void sqlite3DequoteExpr(Expr *p){
413 if( ExprHasAnyProperty(p, EP_Dequoted) ){
414 return;
415 }
416 ExprSetProperty(p, EP_Dequoted);
417 if( p->token.dyn==0 ){
drhd2687b72005-08-12 22:56:09 +0000418 sqlite3TokenCopy(&p->token, &p->token);
419 }
420 sqlite3Dequote((char*)p->token.z);
421}
422
drha76b5df2002-02-23 02:32:10 +0000423
424/*
drhff78bd22002-02-27 01:47:11 +0000425** The following group of routines make deep copies of expressions,
426** expression lists, ID lists, and select statements. The copies can
427** be deleted (by being passed to their respective ...Delete() routines)
428** without effecting the originals.
429**
danielk19774adee202004-05-08 08:23:19 +0000430** The expression list, ID, and source lists return by sqlite3ExprListDup(),
431** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
drhad3cab52002-05-24 02:04:32 +0000432** by subsequent calls to sqlite*ListAppend() routines.
drhff78bd22002-02-27 01:47:11 +0000433**
drhad3cab52002-05-24 02:04:32 +0000434** Any tables that the SrcList might point to are not duplicated.
drhff78bd22002-02-27 01:47:11 +0000435*/
danielk19774adee202004-05-08 08:23:19 +0000436Expr *sqlite3ExprDup(Expr *p){
drhff78bd22002-02-27 01:47:11 +0000437 Expr *pNew;
438 if( p==0 ) return 0;
drhfcb78a42003-01-18 20:11:05 +0000439 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000440 if( pNew==0 ) return 0;
drh3b167c72002-06-28 12:18:47 +0000441 memcpy(pNew, p, sizeof(*pNew));
drh6977fea2002-10-22 23:38:04 +0000442 if( p->token.z!=0 ){
drh2646da72005-12-09 20:02:05 +0000443 pNew->token.z = (u8*)sqliteStrNDup((char*)p->token.z, p->token.n);
drh4b59ab52002-08-24 18:24:51 +0000444 pNew->token.dyn = 1;
445 }else{
drh4efc4752004-01-16 15:55:37 +0000446 assert( pNew->token.z==0 );
drh4b59ab52002-08-24 18:24:51 +0000447 }
drh6977fea2002-10-22 23:38:04 +0000448 pNew->span.z = 0;
danielk19774adee202004-05-08 08:23:19 +0000449 pNew->pLeft = sqlite3ExprDup(p->pLeft);
450 pNew->pRight = sqlite3ExprDup(p->pRight);
451 pNew->pList = sqlite3ExprListDup(p->pList);
452 pNew->pSelect = sqlite3SelectDup(p->pSelect);
danielk1977aee18ef2005-03-09 12:26:50 +0000453 pNew->pTab = p->pTab;
drhff78bd22002-02-27 01:47:11 +0000454 return pNew;
455}
danielk19774adee202004-05-08 08:23:19 +0000456void sqlite3TokenCopy(Token *pTo, Token *pFrom){
drh4b59ab52002-08-24 18:24:51 +0000457 if( pTo->dyn ) sqliteFree((char*)pTo->z);
drh4b59ab52002-08-24 18:24:51 +0000458 if( pFrom->z ){
459 pTo->n = pFrom->n;
drh2646da72005-12-09 20:02:05 +0000460 pTo->z = (u8*)sqliteStrNDup((char*)pFrom->z, pFrom->n);
drh4b59ab52002-08-24 18:24:51 +0000461 pTo->dyn = 1;
462 }else{
drh4b59ab52002-08-24 18:24:51 +0000463 pTo->z = 0;
drh4b59ab52002-08-24 18:24:51 +0000464 }
465}
danielk19774adee202004-05-08 08:23:19 +0000466ExprList *sqlite3ExprListDup(ExprList *p){
drhff78bd22002-02-27 01:47:11 +0000467 ExprList *pNew;
drh145716b2004-09-24 12:24:06 +0000468 struct ExprList_item *pItem, *pOldItem;
drhff78bd22002-02-27 01:47:11 +0000469 int i;
470 if( p==0 ) return 0;
471 pNew = sqliteMalloc( sizeof(*pNew) );
472 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000473 pNew->nExpr = pNew->nAlloc = p->nExpr;
drh3e7bc9c2004-02-21 19:17:17 +0000474 pNew->a = pItem = sqliteMalloc( p->nExpr*sizeof(p->a[0]) );
danielk1977e0048402004-06-15 16:51:01 +0000475 if( pItem==0 ){
476 sqliteFree(pNew);
477 return 0;
478 }
drh145716b2004-09-24 12:24:06 +0000479 pOldItem = p->a;
480 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
drh4b59ab52002-08-24 18:24:51 +0000481 Expr *pNewExpr, *pOldExpr;
drh145716b2004-09-24 12:24:06 +0000482 pItem->pExpr = pNewExpr = sqlite3ExprDup(pOldExpr = pOldItem->pExpr);
drh6977fea2002-10-22 23:38:04 +0000483 if( pOldExpr->span.z!=0 && pNewExpr ){
484 /* Always make a copy of the span for top-level expressions in the
drh4b59ab52002-08-24 18:24:51 +0000485 ** expression list. The logic in SELECT processing that determines
486 ** the names of columns in the result set needs this information */
danielk19774adee202004-05-08 08:23:19 +0000487 sqlite3TokenCopy(&pNewExpr->span, &pOldExpr->span);
drh4b59ab52002-08-24 18:24:51 +0000488 }
drh1f3e9052002-10-31 00:09:39 +0000489 assert( pNewExpr==0 || pNewExpr->span.z!=0
drh6f7adc82006-01-11 21:41:20 +0000490 || pOldExpr->span.z==0
danielk19779e128002006-01-18 16:51:35 +0000491 || sqlite3MallocFailed() );
drh145716b2004-09-24 12:24:06 +0000492 pItem->zName = sqliteStrDup(pOldItem->zName);
493 pItem->sortOrder = pOldItem->sortOrder;
494 pItem->isAgg = pOldItem->isAgg;
drh3e7bc9c2004-02-21 19:17:17 +0000495 pItem->done = 0;
drhff78bd22002-02-27 01:47:11 +0000496 }
497 return pNew;
498}
danielk197793758c82005-01-21 08:13:14 +0000499
500/*
501** If cursors, triggers, views and subqueries are all omitted from
502** the build, then none of the following routines, except for
503** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes
504** called with a NULL argument.
505*/
danielk19776a67fe82005-02-04 04:07:16 +0000506#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \
507 || !defined(SQLITE_OMIT_SUBQUERY)
danielk19774adee202004-05-08 08:23:19 +0000508SrcList *sqlite3SrcListDup(SrcList *p){
drhad3cab52002-05-24 02:04:32 +0000509 SrcList *pNew;
510 int i;
drh113088e2003-03-20 01:16:58 +0000511 int nByte;
drhad3cab52002-05-24 02:04:32 +0000512 if( p==0 ) return 0;
drh113088e2003-03-20 01:16:58 +0000513 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
drh4efc4752004-01-16 15:55:37 +0000514 pNew = sqliteMallocRaw( nByte );
drhad3cab52002-05-24 02:04:32 +0000515 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000516 pNew->nSrc = pNew->nAlloc = p->nSrc;
drhad3cab52002-05-24 02:04:32 +0000517 for(i=0; i<p->nSrc; i++){
drh4efc4752004-01-16 15:55:37 +0000518 struct SrcList_item *pNewItem = &pNew->a[i];
519 struct SrcList_item *pOldItem = &p->a[i];
drhed8a3bb2005-06-06 21:19:56 +0000520 Table *pTab;
drh4efc4752004-01-16 15:55:37 +0000521 pNewItem->zDatabase = sqliteStrDup(pOldItem->zDatabase);
522 pNewItem->zName = sqliteStrDup(pOldItem->zName);
523 pNewItem->zAlias = sqliteStrDup(pOldItem->zAlias);
524 pNewItem->jointype = pOldItem->jointype;
525 pNewItem->iCursor = pOldItem->iCursor;
danielk19771787cca2006-02-10 07:07:14 +0000526 pNewItem->isPopulated = pOldItem->isPopulated;
drhed8a3bb2005-06-06 21:19:56 +0000527 pTab = pNewItem->pTab = pOldItem->pTab;
528 if( pTab ){
529 pTab->nRef++;
danielk1977a1cb1832005-02-12 08:59:55 +0000530 }
danielk19774adee202004-05-08 08:23:19 +0000531 pNewItem->pSelect = sqlite3SelectDup(pOldItem->pSelect);
532 pNewItem->pOn = sqlite3ExprDup(pOldItem->pOn);
533 pNewItem->pUsing = sqlite3IdListDup(pOldItem->pUsing);
danielk19776c18b6e2005-01-30 09:17:58 +0000534 pNewItem->colUsed = pOldItem->colUsed;
drhad3cab52002-05-24 02:04:32 +0000535 }
536 return pNew;
537}
danielk19774adee202004-05-08 08:23:19 +0000538IdList *sqlite3IdListDup(IdList *p){
drhff78bd22002-02-27 01:47:11 +0000539 IdList *pNew;
540 int i;
541 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000542 pNew = sqliteMallocRaw( sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +0000543 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000544 pNew->nId = pNew->nAlloc = p->nId;
drh4efc4752004-01-16 15:55:37 +0000545 pNew->a = sqliteMallocRaw( p->nId*sizeof(p->a[0]) );
danielk1977d5d56522005-03-16 12:15:20 +0000546 if( pNew->a==0 ){
547 sqliteFree(pNew);
548 return 0;
549 }
drhff78bd22002-02-27 01:47:11 +0000550 for(i=0; i<p->nId; i++){
drh4efc4752004-01-16 15:55:37 +0000551 struct IdList_item *pNewItem = &pNew->a[i];
552 struct IdList_item *pOldItem = &p->a[i];
553 pNewItem->zName = sqliteStrDup(pOldItem->zName);
554 pNewItem->idx = pOldItem->idx;
drhff78bd22002-02-27 01:47:11 +0000555 }
556 return pNew;
557}
danielk19774adee202004-05-08 08:23:19 +0000558Select *sqlite3SelectDup(Select *p){
drhff78bd22002-02-27 01:47:11 +0000559 Select *pNew;
560 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000561 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000562 if( pNew==0 ) return 0;
563 pNew->isDistinct = p->isDistinct;
danielk19774adee202004-05-08 08:23:19 +0000564 pNew->pEList = sqlite3ExprListDup(p->pEList);
565 pNew->pSrc = sqlite3SrcListDup(p->pSrc);
566 pNew->pWhere = sqlite3ExprDup(p->pWhere);
567 pNew->pGroupBy = sqlite3ExprListDup(p->pGroupBy);
568 pNew->pHaving = sqlite3ExprDup(p->pHaving);
569 pNew->pOrderBy = sqlite3ExprListDup(p->pOrderBy);
drhff78bd22002-02-27 01:47:11 +0000570 pNew->op = p->op;
danielk19774adee202004-05-08 08:23:19 +0000571 pNew->pPrior = sqlite3SelectDup(p->pPrior);
danielk1977a2dc3b12005-02-05 12:48:48 +0000572 pNew->pLimit = sqlite3ExprDup(p->pLimit);
573 pNew->pOffset = sqlite3ExprDup(p->pOffset);
drh7b58dae2003-07-20 01:16:46 +0000574 pNew->iLimit = -1;
575 pNew->iOffset = -1;
danielk1977a1cb1832005-02-12 08:59:55 +0000576 pNew->isResolved = p->isResolved;
577 pNew->isAgg = p->isAgg;
drhb9bb7c12006-06-11 23:41:55 +0000578 pNew->usesEphm = 0;
drh8e647b82005-09-23 21:11:53 +0000579 pNew->disallowOrderBy = 0;
drh0342b1f2005-09-01 03:07:44 +0000580 pNew->pRightmost = 0;
drhb9bb7c12006-06-11 23:41:55 +0000581 pNew->addrOpenEphm[0] = -1;
582 pNew->addrOpenEphm[1] = -1;
583 pNew->addrOpenEphm[2] = -1;
drhff78bd22002-02-27 01:47:11 +0000584 return pNew;
585}
danielk197793758c82005-01-21 08:13:14 +0000586#else
587Select *sqlite3SelectDup(Select *p){
588 assert( p==0 );
589 return 0;
590}
591#endif
drhff78bd22002-02-27 01:47:11 +0000592
593
594/*
drha76b5df2002-02-23 02:32:10 +0000595** Add a new element to the end of an expression list. If pList is
596** initially NULL, then create a new expression list.
597*/
danielk19774adee202004-05-08 08:23:19 +0000598ExprList *sqlite3ExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){
drha76b5df2002-02-23 02:32:10 +0000599 if( pList==0 ){
600 pList = sqliteMalloc( sizeof(ExprList) );
601 if( pList==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000602 goto no_mem;
drha76b5df2002-02-23 02:32:10 +0000603 }
drh4efc4752004-01-16 15:55:37 +0000604 assert( pList->nAlloc==0 );
drha76b5df2002-02-23 02:32:10 +0000605 }
drh4305d102003-07-30 12:34:12 +0000606 if( pList->nAlloc<=pList->nExpr ){
danielk1977d5d56522005-03-16 12:15:20 +0000607 struct ExprList_item *a;
608 int n = pList->nAlloc*2 + 4;
609 a = sqliteRealloc(pList->a, n*sizeof(pList->a[0]));
610 if( a==0 ){
611 goto no_mem;
drha76b5df2002-02-23 02:32:10 +0000612 }
danielk1977d5d56522005-03-16 12:15:20 +0000613 pList->a = a;
614 pList->nAlloc = n;
drha76b5df2002-02-23 02:32:10 +0000615 }
drh4efc4752004-01-16 15:55:37 +0000616 assert( pList->a!=0 );
617 if( pExpr || pName ){
618 struct ExprList_item *pItem = &pList->a[pList->nExpr++];
619 memset(pItem, 0, sizeof(*pItem));
drha99db3b2004-06-19 14:49:12 +0000620 pItem->zName = sqlite3NameFromToken(pName);
danielk1977e94ddc92005-03-21 03:53:38 +0000621 pItem->pExpr = pExpr;
drha76b5df2002-02-23 02:32:10 +0000622 }
623 return pList;
danielk1977d5d56522005-03-16 12:15:20 +0000624
625no_mem:
626 /* Avoid leaking memory if malloc has failed. */
627 sqlite3ExprDelete(pExpr);
628 sqlite3ExprListDelete(pList);
629 return 0;
drha76b5df2002-02-23 02:32:10 +0000630}
631
632/*
633** Delete an entire expression list.
634*/
danielk19774adee202004-05-08 08:23:19 +0000635void sqlite3ExprListDelete(ExprList *pList){
drha76b5df2002-02-23 02:32:10 +0000636 int i;
drhbe5c89a2004-07-26 00:31:09 +0000637 struct ExprList_item *pItem;
drha76b5df2002-02-23 02:32:10 +0000638 if( pList==0 ) return;
drh1bdd9b52004-04-23 17:04:44 +0000639 assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
640 assert( pList->nExpr<=pList->nAlloc );
drhbe5c89a2004-07-26 00:31:09 +0000641 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
642 sqlite3ExprDelete(pItem->pExpr);
643 sqliteFree(pItem->zName);
drha76b5df2002-02-23 02:32:10 +0000644 }
645 sqliteFree(pList->a);
646 sqliteFree(pList);
647}
648
649/*
drh626a8792005-01-17 22:08:19 +0000650** Walk an expression tree. Call xFunc for each node visited.
drh73b211a2005-01-18 04:00:42 +0000651**
drh626a8792005-01-17 22:08:19 +0000652** The return value from xFunc determines whether the tree walk continues.
653** 0 means continue walking the tree. 1 means do not walk children
654** of the current node but continue with siblings. 2 means abandon
655** the tree walk completely.
656**
657** The return value from this routine is 1 to abandon the tree walk
658** and 0 to continue.
drh87abf5c2005-08-25 12:45:04 +0000659**
660** NOTICE: This routine does *not* descend into subqueries.
drh626a8792005-01-17 22:08:19 +0000661*/
danielk1977a58fdfb2005-02-08 07:50:40 +0000662static int walkExprList(ExprList *, int (*)(void *, Expr*), void *);
drh626a8792005-01-17 22:08:19 +0000663static int walkExprTree(Expr *pExpr, int (*xFunc)(void*,Expr*), void *pArg){
drh626a8792005-01-17 22:08:19 +0000664 int rc;
665 if( pExpr==0 ) return 0;
666 rc = (*xFunc)(pArg, pExpr);
667 if( rc==0 ){
668 if( walkExprTree(pExpr->pLeft, xFunc, pArg) ) return 1;
669 if( walkExprTree(pExpr->pRight, xFunc, pArg) ) return 1;
danielk1977a58fdfb2005-02-08 07:50:40 +0000670 if( walkExprList(pExpr->pList, xFunc, pArg) ) return 1;
drh626a8792005-01-17 22:08:19 +0000671 }
672 return rc>1;
673}
674
675/*
danielk1977a58fdfb2005-02-08 07:50:40 +0000676** Call walkExprTree() for every expression in list p.
677*/
678static int walkExprList(ExprList *p, int (*xFunc)(void *, Expr*), void *pArg){
679 int i;
680 struct ExprList_item *pItem;
681 if( !p ) return 0;
682 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
683 if( walkExprTree(pItem->pExpr, xFunc, pArg) ) return 1;
684 }
685 return 0;
686}
687
688/*
689** Call walkExprTree() for every expression in Select p, not including
690** expressions that are part of sub-selects in any FROM clause or the LIMIT
691** or OFFSET expressions..
692*/
693static int walkSelectExpr(Select *p, int (*xFunc)(void *, Expr*), void *pArg){
694 walkExprList(p->pEList, xFunc, pArg);
695 walkExprTree(p->pWhere, xFunc, pArg);
696 walkExprList(p->pGroupBy, xFunc, pArg);
697 walkExprTree(p->pHaving, xFunc, pArg);
698 walkExprList(p->pOrderBy, xFunc, pArg);
699 return 0;
700}
701
702
703/*
drh626a8792005-01-17 22:08:19 +0000704** This routine is designed as an xFunc for walkExprTree().
705**
706** pArg is really a pointer to an integer. If we can tell by looking
drh73b211a2005-01-18 04:00:42 +0000707** at pExpr that the expression that contains pExpr is not a constant
708** expression, then set *pArg to 0 and return 2 to abandon the tree walk.
709** If pExpr does does not disqualify the expression from being a constant
710** then do nothing.
711**
712** After walking the whole tree, if no nodes are found that disqualify
713** the expression as constant, then we assume the whole expression
714** is constant. See sqlite3ExprIsConstant() for additional information.
drh626a8792005-01-17 22:08:19 +0000715*/
716static int exprNodeIsConstant(void *pArg, Expr *pExpr){
717 switch( pExpr->op ){
drheb55bd22005-06-30 17:04:21 +0000718 /* Consider functions to be constant if all their arguments are constant
719 ** and *pArg==2 */
720 case TK_FUNCTION:
721 if( *((int*)pArg)==2 ) return 0;
722 /* Fall through */
drh626a8792005-01-17 22:08:19 +0000723 case TK_ID:
724 case TK_COLUMN:
725 case TK_DOT:
726 case TK_AGG_FUNCTION:
drh13449892005-09-07 21:22:45 +0000727 case TK_AGG_COLUMN:
drhfe2093d2005-01-20 22:48:47 +0000728#ifndef SQLITE_OMIT_SUBQUERY
729 case TK_SELECT:
730 case TK_EXISTS:
731#endif
drh626a8792005-01-17 22:08:19 +0000732 *((int*)pArg) = 0;
733 return 2;
drh87abf5c2005-08-25 12:45:04 +0000734 case TK_IN:
735 if( pExpr->pSelect ){
736 *((int*)pArg) = 0;
737 return 2;
738 }
drh626a8792005-01-17 22:08:19 +0000739 default:
740 return 0;
741 }
742}
743
744/*
drhfef52082000-06-06 01:50:43 +0000745** Walk an expression tree. Return 1 if the expression is constant
drheb55bd22005-06-30 17:04:21 +0000746** and 0 if it involves variables or function calls.
drh23989372002-05-21 13:43:04 +0000747**
748** For the purposes of this function, a double-quoted string (ex: "abc")
749** is considered a variable but a single-quoted string (ex: 'abc') is
750** a constant.
drhfef52082000-06-06 01:50:43 +0000751*/
danielk19774adee202004-05-08 08:23:19 +0000752int sqlite3ExprIsConstant(Expr *p){
drh626a8792005-01-17 22:08:19 +0000753 int isConst = 1;
754 walkExprTree(p, exprNodeIsConstant, &isConst);
755 return isConst;
drhfef52082000-06-06 01:50:43 +0000756}
757
758/*
drheb55bd22005-06-30 17:04:21 +0000759** Walk an expression tree. Return 1 if the expression is constant
760** or a function call with constant arguments. Return and 0 if there
761** are any variables.
762**
763** For the purposes of this function, a double-quoted string (ex: "abc")
764** is considered a variable but a single-quoted string (ex: 'abc') is
765** a constant.
766*/
767int sqlite3ExprIsConstantOrFunction(Expr *p){
768 int isConst = 2;
769 walkExprTree(p, exprNodeIsConstant, &isConst);
770 return isConst!=0;
771}
772
773/*
drh73b211a2005-01-18 04:00:42 +0000774** If the expression p codes a constant integer that is small enough
drh202b2df2004-01-06 01:13:46 +0000775** to fit in a 32-bit integer, return 1 and put the value of the integer
776** in *pValue. If the expression is not an integer or if it is too big
777** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
drhe4de1fe2002-06-02 16:09:01 +0000778*/
danielk19774adee202004-05-08 08:23:19 +0000779int sqlite3ExprIsInteger(Expr *p, int *pValue){
drhe4de1fe2002-06-02 16:09:01 +0000780 switch( p->op ){
781 case TK_INTEGER: {
drh2646da72005-12-09 20:02:05 +0000782 if( sqlite3GetInt32((char*)p->token.z, pValue) ){
drh202b2df2004-01-06 01:13:46 +0000783 return 1;
784 }
785 break;
drhe4de1fe2002-06-02 16:09:01 +0000786 }
drh4b59ab52002-08-24 18:24:51 +0000787 case TK_UPLUS: {
danielk19774adee202004-05-08 08:23:19 +0000788 return sqlite3ExprIsInteger(p->pLeft, pValue);
drh4b59ab52002-08-24 18:24:51 +0000789 }
drhe4de1fe2002-06-02 16:09:01 +0000790 case TK_UMINUS: {
791 int v;
danielk19774adee202004-05-08 08:23:19 +0000792 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
drhe4de1fe2002-06-02 16:09:01 +0000793 *pValue = -v;
794 return 1;
795 }
796 break;
797 }
798 default: break;
799 }
800 return 0;
801}
802
803/*
drhc4a3c772001-04-04 11:48:57 +0000804** Return TRUE if the given string is a row-id column name.
805*/
danielk19774adee202004-05-08 08:23:19 +0000806int sqlite3IsRowid(const char *z){
807 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
808 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
809 if( sqlite3StrICmp(z, "OID")==0 ) return 1;
drhc4a3c772001-04-04 11:48:57 +0000810 return 0;
811}
812
813/*
drh8141f612004-01-25 22:44:58 +0000814** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
815** that name in the set of source tables in pSrcList and make the pExpr
816** expression node refer back to that source column. The following changes
817** are made to pExpr:
818**
819** pExpr->iDb Set the index in db->aDb[] of the database holding
820** the table.
821** pExpr->iTable Set to the cursor number for the table obtained
822** from pSrcList.
823** pExpr->iColumn Set to the column number within the table.
drh8141f612004-01-25 22:44:58 +0000824** pExpr->op Set to TK_COLUMN.
825** pExpr->pLeft Any expression this points to is deleted
826** pExpr->pRight Any expression this points to is deleted.
827**
828** The pDbToken is the name of the database (the "X"). This value may be
829** NULL meaning that name is of the form Y.Z or Z. Any available database
830** can be used. The pTableToken is the name of the table (the "Y"). This
831** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it
832** means that the form of the name is Z and that columns from any table
833** can be used.
834**
835** If the name cannot be resolved unambiguously, leave an error message
836** in pParse and return non-zero. Return zero on success.
837*/
838static int lookupName(
drhffe07b22005-11-03 00:41:17 +0000839 Parse *pParse, /* The parsing context */
drh8141f612004-01-25 22:44:58 +0000840 Token *pDbToken, /* Name of the database containing table, or NULL */
841 Token *pTableToken, /* Name of table containing column, or NULL */
842 Token *pColumnToken, /* Name of the column. */
drh626a8792005-01-17 22:08:19 +0000843 NameContext *pNC, /* The name context used to resolve the name */
drh8141f612004-01-25 22:44:58 +0000844 Expr *pExpr /* Make this EXPR node point to the selected column */
845){
846 char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */
847 char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */
848 char *zCol = 0; /* Name of the column. The "Z" */
849 int i, j; /* Loop counters */
850 int cnt = 0; /* Number of matching column names */
851 int cntTab = 0; /* Number of matching table names */
drh626a8792005-01-17 22:08:19 +0000852 sqlite3 *db = pParse->db; /* The database */
drh51669862004-12-18 18:40:26 +0000853 struct SrcList_item *pItem; /* Use for looping over pSrcList items */
854 struct SrcList_item *pMatch = 0; /* The matching pSrcList item */
drh73b211a2005-01-18 04:00:42 +0000855 NameContext *pTopNC = pNC; /* First namecontext in the list */
drh8141f612004-01-25 22:44:58 +0000856
857 assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
drha99db3b2004-06-19 14:49:12 +0000858 zDb = sqlite3NameFromToken(pDbToken);
859 zTab = sqlite3NameFromToken(pTableToken);
860 zCol = sqlite3NameFromToken(pColumnToken);
danielk19779e128002006-01-18 16:51:35 +0000861 if( sqlite3MallocFailed() ){
danielk1977d5d56522005-03-16 12:15:20 +0000862 goto lookupname_end;
drh8141f612004-01-25 22:44:58 +0000863 }
drh8141f612004-01-25 22:44:58 +0000864
865 pExpr->iTable = -1;
drh626a8792005-01-17 22:08:19 +0000866 while( pNC && cnt==0 ){
drhffe07b22005-11-03 00:41:17 +0000867 ExprList *pEList;
drh626a8792005-01-17 22:08:19 +0000868 SrcList *pSrcList = pNC->pSrcList;
drh8141f612004-01-25 22:44:58 +0000869
danielk1977b3bce662005-01-29 08:32:43 +0000870 if( pSrcList ){
871 for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
drh43617e92006-03-06 20:55:46 +0000872 Table *pTab;
873 int iDb;
danielk1977b3bce662005-01-29 08:32:43 +0000874 Column *pCol;
875
drh43617e92006-03-06 20:55:46 +0000876 pTab = pItem->pTab;
877 assert( pTab!=0 );
878 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
danielk1977b3bce662005-01-29 08:32:43 +0000879 assert( pTab->nCol>0 );
880 if( zTab ){
881 if( pItem->zAlias ){
882 char *zTabName = pItem->zAlias;
883 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
884 }else{
885 char *zTabName = pTab->zName;
886 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
danielk1977da184232006-01-05 11:34:32 +0000887 if( zDb!=0 && sqlite3StrICmp(db->aDb[iDb].zName, zDb)!=0 ){
danielk1977b3bce662005-01-29 08:32:43 +0000888 continue;
889 }
drh626a8792005-01-17 22:08:19 +0000890 }
drh8141f612004-01-25 22:44:58 +0000891 }
danielk1977b3bce662005-01-29 08:32:43 +0000892 if( 0==(cntTab++) ){
drh626a8792005-01-17 22:08:19 +0000893 pExpr->iTable = pItem->iCursor;
danielk1977da184232006-01-05 11:34:32 +0000894 pExpr->pSchema = pTab->pSchema;
danielk1977b3bce662005-01-29 08:32:43 +0000895 pMatch = pItem;
896 }
897 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
898 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
danielk1977b3bf5562006-01-10 17:58:23 +0000899 const char *zColl = pTab->aCol[j].zColl;
drh873fac02005-06-06 17:11:46 +0000900 IdList *pUsing;
danielk1977b3bce662005-01-29 08:32:43 +0000901 cnt++;
902 pExpr->iTable = pItem->iCursor;
903 pMatch = pItem;
danielk1977da184232006-01-05 11:34:32 +0000904 pExpr->pSchema = pTab->pSchema;
danielk1977b3bce662005-01-29 08:32:43 +0000905 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
906 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
907 pExpr->affinity = pTab->aCol[j].affinity;
drh8b4c40d2007-02-01 23:02:45 +0000908 if( (pExpr->flags & EP_ExpCollate)==0 ){
909 pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0);
910 }
drh61dfc312006-12-16 16:25:15 +0000911 if( i<pSrcList->nSrc-1 ){
912 if( pItem[1].jointype & JT_NATURAL ){
913 /* If this match occurred in the left table of a natural join,
914 ** then skip the right table to avoid a duplicate match */
915 pItem++;
916 i++;
917 }else if( (pUsing = pItem[1].pUsing)!=0 ){
918 /* If this match occurs on a column that is in the USING clause
919 ** of a join, skip the search of the right table of the join
920 ** to avoid a duplicate match there. */
921 int k;
922 for(k=0; k<pUsing->nId; k++){
923 if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ){
924 pItem++;
925 i++;
926 break;
927 }
drh873fac02005-06-06 17:11:46 +0000928 }
929 }
930 }
danielk1977b3bce662005-01-29 08:32:43 +0000931 break;
932 }
drh8141f612004-01-25 22:44:58 +0000933 }
934 }
935 }
drh626a8792005-01-17 22:08:19 +0000936
937#ifndef SQLITE_OMIT_TRIGGER
938 /* If we have not already resolved the name, then maybe
939 ** it is a new.* or old.* trigger argument reference
940 */
941 if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
942 TriggerStack *pTriggerStack = pParse->trigStack;
943 Table *pTab = 0;
944 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){
945 pExpr->iTable = pTriggerStack->newIdx;
946 assert( pTriggerStack->pTab );
947 pTab = pTriggerStack->pTab;
948 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab)==0 ){
949 pExpr->iTable = pTriggerStack->oldIdx;
950 assert( pTriggerStack->pTab );
951 pTab = pTriggerStack->pTab;
952 }
953
954 if( pTab ){
danielk1977f0113002006-01-24 12:09:17 +0000955 int iCol;
drh626a8792005-01-17 22:08:19 +0000956 Column *pCol = pTab->aCol;
957
danielk1977da184232006-01-05 11:34:32 +0000958 pExpr->pSchema = pTab->pSchema;
drh626a8792005-01-17 22:08:19 +0000959 cntTab++;
danielk1977f0113002006-01-24 12:09:17 +0000960 for(iCol=0; iCol < pTab->nCol; iCol++, pCol++) {
drh626a8792005-01-17 22:08:19 +0000961 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
danielk1977f0113002006-01-24 12:09:17 +0000962 const char *zColl = pTab->aCol[iCol].zColl;
drh626a8792005-01-17 22:08:19 +0000963 cnt++;
danielk1977f0113002006-01-24 12:09:17 +0000964 pExpr->iColumn = iCol==pTab->iPKey ? -1 : iCol;
965 pExpr->affinity = pTab->aCol[iCol].affinity;
drh8b4c40d2007-02-01 23:02:45 +0000966 if( (pExpr->flags & EP_ExpCollate)==0 ){
967 pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0);
968 }
danielk1977aee18ef2005-03-09 12:26:50 +0000969 pExpr->pTab = pTab;
drh626a8792005-01-17 22:08:19 +0000970 break;
971 }
972 }
973 }
974 }
drhb7f91642004-10-31 02:22:47 +0000975#endif /* !defined(SQLITE_OMIT_TRIGGER) */
drh8141f612004-01-25 22:44:58 +0000976
drh626a8792005-01-17 22:08:19 +0000977 /*
978 ** Perhaps the name is a reference to the ROWID
979 */
980 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
981 cnt = 1;
982 pExpr->iColumn = -1;
drh8a512562005-11-14 22:29:05 +0000983 pExpr->affinity = SQLITE_AFF_INTEGER;
drh626a8792005-01-17 22:08:19 +0000984 }
drh8141f612004-01-25 22:44:58 +0000985
drh626a8792005-01-17 22:08:19 +0000986 /*
987 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
988 ** might refer to an result-set alias. This happens, for example, when
989 ** we are resolving names in the WHERE clause of the following command:
990 **
991 ** SELECT a+b AS x FROM table WHERE x<10;
992 **
993 ** In cases like this, replace pExpr with a copy of the expression that
994 ** forms the result set entry ("a+b" in the example) and return immediately.
995 ** Note that the expression in the result set should have already been
996 ** resolved by the time the WHERE clause is resolved.
997 */
drhffe07b22005-11-03 00:41:17 +0000998 if( cnt==0 && (pEList = pNC->pEList)!=0 && zTab==0 ){
drh626a8792005-01-17 22:08:19 +0000999 for(j=0; j<pEList->nExpr; j++){
1000 char *zAs = pEList->a[j].zName;
1001 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
1002 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
1003 pExpr->op = TK_AS;
1004 pExpr->iColumn = j;
1005 pExpr->pLeft = sqlite3ExprDup(pEList->a[j].pExpr);
drh15ccce12005-05-23 15:06:39 +00001006 cnt = 1;
drh626a8792005-01-17 22:08:19 +00001007 assert( zTab==0 && zDb==0 );
drh15ccce12005-05-23 15:06:39 +00001008 goto lookupname_end_2;
drh626a8792005-01-17 22:08:19 +00001009 }
1010 }
1011 }
1012
1013 /* Advance to the next name context. The loop will exit when either
1014 ** we have a match (cnt>0) or when we run out of name contexts.
1015 */
1016 if( cnt==0 ){
1017 pNC = pNC->pNext;
1018 }
drh8141f612004-01-25 22:44:58 +00001019 }
1020
1021 /*
1022 ** If X and Y are NULL (in other words if only the column name Z is
1023 ** supplied) and the value of Z is enclosed in double-quotes, then
1024 ** Z is a string literal if it doesn't match any column names. In that
1025 ** case, we need to return right away and not make any changes to
1026 ** pExpr.
drh15ccce12005-05-23 15:06:39 +00001027 **
1028 ** Because no reference was made to outer contexts, the pNC->nRef
1029 ** fields are not changed in any context.
drh8141f612004-01-25 22:44:58 +00001030 */
1031 if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
1032 sqliteFree(zCol);
1033 return 0;
1034 }
1035
1036 /*
1037 ** cnt==0 means there was not match. cnt>1 means there were two or
1038 ** more matches. Either way, we have an error.
1039 */
1040 if( cnt!=1 ){
1041 char *z = 0;
1042 char *zErr;
1043 zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s";
1044 if( zDb ){
drhf93339d2006-01-03 15:16:26 +00001045 sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, (char*)0);
drh8141f612004-01-25 22:44:58 +00001046 }else if( zTab ){
drhf93339d2006-01-03 15:16:26 +00001047 sqlite3SetString(&z, zTab, ".", zCol, (char*)0);
drh8141f612004-01-25 22:44:58 +00001048 }else{
1049 z = sqliteStrDup(zCol);
1050 }
danielk19774adee202004-05-08 08:23:19 +00001051 sqlite3ErrorMsg(pParse, zErr, z);
drh8141f612004-01-25 22:44:58 +00001052 sqliteFree(z);
drh73b211a2005-01-18 04:00:42 +00001053 pTopNC->nErr++;
drh8141f612004-01-25 22:44:58 +00001054 }
1055
drh51669862004-12-18 18:40:26 +00001056 /* If a column from a table in pSrcList is referenced, then record
1057 ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes
1058 ** bit 0 to be set. Column 1 sets bit 1. And so forth. If the
1059 ** column number is greater than the number of bits in the bitmask
1060 ** then set the high-order bit of the bitmask.
1061 */
1062 if( pExpr->iColumn>=0 && pMatch!=0 ){
1063 int n = pExpr->iColumn;
1064 if( n>=sizeof(Bitmask)*8 ){
1065 n = sizeof(Bitmask)*8-1;
1066 }
1067 assert( pMatch->iCursor==pExpr->iTable );
drhca83ac52007-02-01 01:40:44 +00001068 pMatch->colUsed |= ((Bitmask)1)<<n;
drh51669862004-12-18 18:40:26 +00001069 }
1070
danielk1977d5d56522005-03-16 12:15:20 +00001071lookupname_end:
drh8141f612004-01-25 22:44:58 +00001072 /* Clean up and return
1073 */
1074 sqliteFree(zDb);
1075 sqliteFree(zTab);
danielk19774adee202004-05-08 08:23:19 +00001076 sqlite3ExprDelete(pExpr->pLeft);
drh8141f612004-01-25 22:44:58 +00001077 pExpr->pLeft = 0;
danielk19774adee202004-05-08 08:23:19 +00001078 sqlite3ExprDelete(pExpr->pRight);
drh8141f612004-01-25 22:44:58 +00001079 pExpr->pRight = 0;
1080 pExpr->op = TK_COLUMN;
drh15ccce12005-05-23 15:06:39 +00001081lookupname_end_2:
1082 sqliteFree(zCol);
drh626a8792005-01-17 22:08:19 +00001083 if( cnt==1 ){
danielk1977b3bce662005-01-29 08:32:43 +00001084 assert( pNC!=0 );
drh626a8792005-01-17 22:08:19 +00001085 sqlite3AuthRead(pParse, pExpr, pNC->pSrcList);
danielk1977aee18ef2005-03-09 12:26:50 +00001086 if( pMatch && !pMatch->pSelect ){
1087 pExpr->pTab = pMatch->pTab;
1088 }
drh15ccce12005-05-23 15:06:39 +00001089 /* Increment the nRef value on all name contexts from TopNC up to
1090 ** the point where the name matched. */
1091 for(;;){
1092 assert( pTopNC!=0 );
1093 pTopNC->nRef++;
1094 if( pTopNC==pNC ) break;
1095 pTopNC = pTopNC->pNext;
1096 }
1097 return 0;
1098 } else {
1099 return 1;
drh626a8792005-01-17 22:08:19 +00001100 }
drh8141f612004-01-25 22:44:58 +00001101}
1102
1103/*
drh626a8792005-01-17 22:08:19 +00001104** This routine is designed as an xFunc for walkExprTree().
1105**
drh73b211a2005-01-18 04:00:42 +00001106** Resolve symbolic names into TK_COLUMN operators for the current
drh626a8792005-01-17 22:08:19 +00001107** node in the expression tree. Return 0 to continue the search down
drh73b211a2005-01-18 04:00:42 +00001108** the tree or 2 to abort the tree walk.
1109**
1110** This routine also does error checking and name resolution for
1111** function names. The operator for aggregate functions is changed
1112** to TK_AGG_FUNCTION.
drh626a8792005-01-17 22:08:19 +00001113*/
1114static int nameResolverStep(void *pArg, Expr *pExpr){
1115 NameContext *pNC = (NameContext*)pArg;
drh626a8792005-01-17 22:08:19 +00001116 Parse *pParse;
drh626a8792005-01-17 22:08:19 +00001117
danielk1977b3bce662005-01-29 08:32:43 +00001118 if( pExpr==0 ) return 1;
drh626a8792005-01-17 22:08:19 +00001119 assert( pNC!=0 );
drh626a8792005-01-17 22:08:19 +00001120 pParse = pNC->pParse;
danielk1977b3bce662005-01-29 08:32:43 +00001121
drh626a8792005-01-17 22:08:19 +00001122 if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return 1;
1123 ExprSetProperty(pExpr, EP_Resolved);
1124#ifndef NDEBUG
danielk1977f0113002006-01-24 12:09:17 +00001125 if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
1126 SrcList *pSrcList = pNC->pSrcList;
danielk1977940fac92005-01-23 22:41:37 +00001127 int i;
danielk1977f0113002006-01-24 12:09:17 +00001128 for(i=0; i<pNC->pSrcList->nSrc; i++){
drh626a8792005-01-17 22:08:19 +00001129 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
1130 }
1131 }
1132#endif
1133 switch( pExpr->op ){
1134 /* Double-quoted strings (ex: "abc") are used as identifiers if
1135 ** possible. Otherwise they remain as strings. Single-quoted
1136 ** strings (ex: 'abc') are always string literals.
1137 */
1138 case TK_STRING: {
1139 if( pExpr->token.z[0]=='\'' ) break;
1140 /* Fall thru into the TK_ID case if this is a double-quoted string */
1141 }
1142 /* A lone identifier is the name of a column.
1143 */
1144 case TK_ID: {
drh626a8792005-01-17 22:08:19 +00001145 lookupName(pParse, 0, 0, &pExpr->token, pNC, pExpr);
1146 return 1;
1147 }
1148
1149 /* A table name and column name: ID.ID
1150 ** Or a database, table and column: ID.ID.ID
1151 */
1152 case TK_DOT: {
1153 Token *pColumn;
1154 Token *pTable;
1155 Token *pDb;
1156 Expr *pRight;
1157
danielk1977b3bce662005-01-29 08:32:43 +00001158 /* if( pSrcList==0 ) break; */
drh626a8792005-01-17 22:08:19 +00001159 pRight = pExpr->pRight;
1160 if( pRight->op==TK_ID ){
1161 pDb = 0;
1162 pTable = &pExpr->pLeft->token;
1163 pColumn = &pRight->token;
1164 }else{
1165 assert( pRight->op==TK_DOT );
1166 pDb = &pExpr->pLeft->token;
1167 pTable = &pRight->pLeft->token;
1168 pColumn = &pRight->pRight->token;
1169 }
1170 lookupName(pParse, pDb, pTable, pColumn, pNC, pExpr);
1171 return 1;
1172 }
1173
1174 /* Resolve function names
1175 */
drhb71090f2005-05-23 17:26:51 +00001176 case TK_CONST_FUNC:
drh626a8792005-01-17 22:08:19 +00001177 case TK_FUNCTION: {
1178 ExprList *pList = pExpr->pList; /* The argument list */
1179 int n = pList ? pList->nExpr : 0; /* Number of arguments */
1180 int no_such_func = 0; /* True if no such function exists */
1181 int wrong_num_args = 0; /* True if wrong number of arguments */
1182 int is_agg = 0; /* True if is an aggregate function */
1183 int i;
drh5169bbc2006-08-24 14:59:45 +00001184 int auth; /* Authorization to use the function */
drh626a8792005-01-17 22:08:19 +00001185 int nId; /* Number of characters in function name */
1186 const char *zId; /* The function name. */
drh73b211a2005-01-18 04:00:42 +00001187 FuncDef *pDef; /* Information about the function */
danielk197714db2662006-01-09 16:12:04 +00001188 int enc = ENC(pParse->db); /* The database encoding */
drh626a8792005-01-17 22:08:19 +00001189
drh2646da72005-12-09 20:02:05 +00001190 zId = (char*)pExpr->token.z;
drhb71090f2005-05-23 17:26:51 +00001191 nId = pExpr->token.n;
drh626a8792005-01-17 22:08:19 +00001192 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
1193 if( pDef==0 ){
1194 pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
1195 if( pDef==0 ){
1196 no_such_func = 1;
1197 }else{
1198 wrong_num_args = 1;
1199 }
1200 }else{
1201 is_agg = pDef->xFunc==0;
1202 }
drh2fca7fe2006-11-23 11:59:13 +00001203#ifndef SQLITE_OMIT_AUTHORIZATION
drh5169bbc2006-08-24 14:59:45 +00001204 if( pDef ){
1205 auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0);
1206 if( auth!=SQLITE_OK ){
1207 if( auth==SQLITE_DENY ){
1208 sqlite3ErrorMsg(pParse, "not authorized to use function: %s",
1209 pDef->zName);
1210 pNC->nErr++;
1211 }
1212 pExpr->op = TK_NULL;
1213 return 1;
1214 }
1215 }
drhb8b14212006-08-24 15:18:25 +00001216#endif
drh626a8792005-01-17 22:08:19 +00001217 if( is_agg && !pNC->allowAgg ){
1218 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
1219 pNC->nErr++;
1220 is_agg = 0;
1221 }else if( no_such_func ){
1222 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
1223 pNC->nErr++;
1224 }else if( wrong_num_args ){
1225 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
1226 nId, zId);
1227 pNC->nErr++;
1228 }
1229 if( is_agg ){
1230 pExpr->op = TK_AGG_FUNCTION;
1231 pNC->hasAgg = 1;
1232 }
drh73b211a2005-01-18 04:00:42 +00001233 if( is_agg ) pNC->allowAgg = 0;
drh626a8792005-01-17 22:08:19 +00001234 for(i=0; pNC->nErr==0 && i<n; i++){
drh73b211a2005-01-18 04:00:42 +00001235 walkExprTree(pList->a[i].pExpr, nameResolverStep, pNC);
drh626a8792005-01-17 22:08:19 +00001236 }
drh73b211a2005-01-18 04:00:42 +00001237 if( is_agg ) pNC->allowAgg = 1;
drh626a8792005-01-17 22:08:19 +00001238 /* FIX ME: Compute pExpr->affinity based on the expected return
1239 ** type of the function
1240 */
1241 return is_agg;
1242 }
danielk1977b3bce662005-01-29 08:32:43 +00001243#ifndef SQLITE_OMIT_SUBQUERY
1244 case TK_SELECT:
1245 case TK_EXISTS:
1246#endif
1247 case TK_IN: {
1248 if( pExpr->pSelect ){
drh8a9f38f2005-11-05 15:07:55 +00001249 int nRef = pNC->nRef;
drh06f65412005-11-03 02:03:13 +00001250#ifndef SQLITE_OMIT_CHECK
1251 if( pNC->isCheck ){
1252 sqlite3ErrorMsg(pParse,"subqueries prohibited in CHECK constraints");
1253 }
1254#endif
danielk1977b3bce662005-01-29 08:32:43 +00001255 sqlite3SelectResolve(pParse, pExpr->pSelect, pNC);
1256 assert( pNC->nRef>=nRef );
1257 if( nRef!=pNC->nRef ){
1258 ExprSetProperty(pExpr, EP_VarSelect);
1259 }
1260 }
drh4284fb02005-11-03 12:33:28 +00001261 break;
danielk1977b3bce662005-01-29 08:32:43 +00001262 }
drh4284fb02005-11-03 12:33:28 +00001263#ifndef SQLITE_OMIT_CHECK
1264 case TK_VARIABLE: {
1265 if( pNC->isCheck ){
1266 sqlite3ErrorMsg(pParse,"parameters prohibited in CHECK constraints");
1267 }
1268 break;
1269 }
1270#endif
drh626a8792005-01-17 22:08:19 +00001271 }
1272 return 0;
1273}
1274
1275/*
drhcce7d172000-05-31 15:34:51 +00001276** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +00001277** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +00001278** index to the table in the table list and a column offset. The
1279** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
1280** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +00001281** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +00001282** VDBE cursor number for a cursor that is pointing into the referenced
1283** table. The Expr.iColumn value is changed to the index of the column
1284** of the referenced table. The Expr.iColumn value for the special
1285** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
1286** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +00001287**
drh626a8792005-01-17 22:08:19 +00001288** Also resolve function names and check the functions for proper
1289** usage. Make sure all function names are recognized and all functions
1290** have the correct number of arguments. Leave an error message
1291** in pParse->zErrMsg if anything is amiss. Return the number of errors.
1292**
drh73b211a2005-01-18 04:00:42 +00001293** If the expression contains aggregate functions then set the EP_Agg
1294** property on the expression.
drh626a8792005-01-17 22:08:19 +00001295*/
1296int sqlite3ExprResolveNames(
danielk1977b3bce662005-01-29 08:32:43 +00001297 NameContext *pNC, /* Namespace to resolve expressions in. */
1298 Expr *pExpr /* The expression to be analyzed. */
drh626a8792005-01-17 22:08:19 +00001299){
drh13449892005-09-07 21:22:45 +00001300 int savedHasAgg;
drh73b211a2005-01-18 04:00:42 +00001301 if( pExpr==0 ) return 0;
drh13449892005-09-07 21:22:45 +00001302 savedHasAgg = pNC->hasAgg;
1303 pNC->hasAgg = 0;
danielk1977b3bce662005-01-29 08:32:43 +00001304 walkExprTree(pExpr, nameResolverStep, pNC);
1305 if( pNC->nErr>0 ){
drh73b211a2005-01-18 04:00:42 +00001306 ExprSetProperty(pExpr, EP_Error);
drh73b211a2005-01-18 04:00:42 +00001307 }
drh13449892005-09-07 21:22:45 +00001308 if( pNC->hasAgg ){
1309 ExprSetProperty(pExpr, EP_Agg);
1310 }else if( savedHasAgg ){
1311 pNC->hasAgg = 1;
1312 }
drh73b211a2005-01-18 04:00:42 +00001313 return ExprHasProperty(pExpr, EP_Error);
drh626a8792005-01-17 22:08:19 +00001314}
1315
drh1398ad32005-01-19 23:24:50 +00001316/*
1317** A pointer instance of this structure is used to pass information
1318** through walkExprTree into codeSubqueryStep().
1319*/
1320typedef struct QueryCoder QueryCoder;
1321struct QueryCoder {
1322 Parse *pParse; /* The parsing context */
1323 NameContext *pNC; /* Namespace of first enclosing query */
1324};
1325
drh626a8792005-01-17 22:08:19 +00001326
1327/*
drh9cbe6352005-11-29 03:13:21 +00001328** Generate code for scalar subqueries used as an expression
1329** and IN operators. Examples:
drh626a8792005-01-17 22:08:19 +00001330**
drh9cbe6352005-11-29 03:13:21 +00001331** (SELECT a FROM b) -- subquery
1332** EXISTS (SELECT a FROM b) -- EXISTS subquery
1333** x IN (4,5,11) -- IN operator with list on right-hand side
1334** x IN (SELECT a FROM b) -- IN operator with subquery on the right
drhfef52082000-06-06 01:50:43 +00001335**
drh9cbe6352005-11-29 03:13:21 +00001336** The pExpr parameter describes the expression that contains the IN
1337** operator or subquery.
drhcce7d172000-05-31 15:34:51 +00001338*/
drh51522cd2005-01-20 13:36:19 +00001339#ifndef SQLITE_OMIT_SUBQUERY
danielk1977b3bce662005-01-29 08:32:43 +00001340void sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){
drh57dbd7b2005-07-08 18:25:26 +00001341 int testAddr = 0; /* One-time test address */
danielk1977b3bce662005-01-29 08:32:43 +00001342 Vdbe *v = sqlite3GetVdbe(pParse);
1343 if( v==0 ) return;
1344
drh57dbd7b2005-07-08 18:25:26 +00001345 /* This code must be run in its entirety every time it is encountered
1346 ** if any of the following is true:
1347 **
1348 ** * The right-hand side is a correlated subquery
1349 ** * The right-hand side is an expression list containing variables
1350 ** * We are inside a trigger
1351 **
1352 ** If all of the above are false, then we can run this code just once
1353 ** save the results, and reuse the same result on subsequent invocations.
danielk1977b3bce662005-01-29 08:32:43 +00001354 */
1355 if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->trigStack ){
1356 int mem = pParse->nMem++;
1357 sqlite3VdbeAddOp(v, OP_MemLoad, mem, 0);
drh57dbd7b2005-07-08 18:25:26 +00001358 testAddr = sqlite3VdbeAddOp(v, OP_If, 0, 0);
danielk19779e128002006-01-18 16:51:35 +00001359 assert( testAddr>0 || sqlite3MallocFailed() );
drhd654be82005-09-20 17:42:23 +00001360 sqlite3VdbeAddOp(v, OP_MemInt, 1, mem);
danielk1977b3bce662005-01-29 08:32:43 +00001361 }
1362
drhcce7d172000-05-31 15:34:51 +00001363 switch( pExpr->op ){
drhfef52082000-06-06 01:50:43 +00001364 case TK_IN: {
danielk1977e014a832004-05-17 10:48:57 +00001365 char affinity;
drhd3d39e92004-05-20 22:16:29 +00001366 KeyInfo keyInfo;
drhb9bb7c12006-06-11 23:41:55 +00001367 int addr; /* Address of OP_OpenEphemeral instruction */
drhd3d39e92004-05-20 22:16:29 +00001368
danielk1977bf3b7212004-05-18 10:06:24 +00001369 affinity = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +00001370
1371 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
drh57dbd7b2005-07-08 18:25:26 +00001372 ** expression it is handled the same way. A virtual table is
danielk1977e014a832004-05-17 10:48:57 +00001373 ** filled with single-field index keys representing the results
1374 ** from the SELECT or the <exprlist>.
1375 **
1376 ** If the 'x' expression is a column value, or the SELECT...
1377 ** statement returns a column value, then the affinity of that
1378 ** column is used to build the index keys. If both 'x' and the
1379 ** SELECT... statement are columns, then numeric affinity is used
1380 ** if either column has NUMERIC or INTEGER affinity. If neither
1381 ** 'x' nor the SELECT... statement are columns, then numeric affinity
1382 ** is used.
1383 */
1384 pExpr->iTable = pParse->nTab++;
drhb9bb7c12006-06-11 23:41:55 +00001385 addr = sqlite3VdbeAddOp(v, OP_OpenEphemeral, pExpr->iTable, 0);
drhd3d39e92004-05-20 22:16:29 +00001386 memset(&keyInfo, 0, sizeof(keyInfo));
1387 keyInfo.nField = 1;
drhf3218fe2004-05-28 08:21:02 +00001388 sqlite3VdbeAddOp(v, OP_SetNumColumns, pExpr->iTable, 1);
danielk1977e014a832004-05-17 10:48:57 +00001389
drhfef52082000-06-06 01:50:43 +00001390 if( pExpr->pSelect ){
1391 /* Case 1: expr IN (SELECT ...)
1392 **
danielk1977e014a832004-05-17 10:48:57 +00001393 ** Generate code to write the results of the select into the temporary
1394 ** table allocated and opened above.
drhfef52082000-06-06 01:50:43 +00001395 */
danielk1977e014a832004-05-17 10:48:57 +00001396 int iParm = pExpr->iTable + (((int)affinity)<<16);
drhbe5c89a2004-07-26 00:31:09 +00001397 ExprList *pEList;
danielk1977e014a832004-05-17 10:48:57 +00001398 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
danielk1977b3bce662005-01-29 08:32:43 +00001399 sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001400 pEList = pExpr->pSelect->pEList;
1401 if( pEList && pEList->nExpr>0 ){
danielk19777cedc8d2004-06-10 10:50:08 +00001402 keyInfo.aColl[0] = binaryCompareCollSeq(pParse, pExpr->pLeft,
drhbe5c89a2004-07-26 00:31:09 +00001403 pEList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001404 }
drhfef52082000-06-06 01:50:43 +00001405 }else if( pExpr->pList ){
1406 /* Case 2: expr IN (exprlist)
1407 **
danielk1977e014a832004-05-17 10:48:57 +00001408 ** For each expression, build an index key from the evaluation and
1409 ** store it in the temporary table. If <expr> is a column, then use
1410 ** that columns affinity when building index keys. If <expr> is not
1411 ** a column, use numeric affinity.
drhfef52082000-06-06 01:50:43 +00001412 */
danielk1977e014a832004-05-17 10:48:57 +00001413 int i;
drh57dbd7b2005-07-08 18:25:26 +00001414 ExprList *pList = pExpr->pList;
1415 struct ExprList_item *pItem;
1416
danielk1977e014a832004-05-17 10:48:57 +00001417 if( !affinity ){
drh8159a352006-05-23 23:22:29 +00001418 affinity = SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +00001419 }
danielk19770202b292004-06-09 09:55:16 +00001420 keyInfo.aColl[0] = pExpr->pLeft->pColl;
danielk1977e014a832004-05-17 10:48:57 +00001421
1422 /* Loop through each expression in <exprlist>. */
drh57dbd7b2005-07-08 18:25:26 +00001423 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
1424 Expr *pE2 = pItem->pExpr;
danielk1977e014a832004-05-17 10:48:57 +00001425
drh57dbd7b2005-07-08 18:25:26 +00001426 /* If the expression is not constant then we will need to
1427 ** disable the test that was generated above that makes sure
1428 ** this code only executes once. Because for a non-constant
1429 ** expression we need to rerun this code each time.
1430 */
drh6c30be82005-07-29 15:10:17 +00001431 if( testAddr>0 && !sqlite3ExprIsConstant(pE2) ){
drhf8875402006-03-17 13:56:34 +00001432 sqlite3VdbeChangeToNoop(v, testAddr-1, 3);
drh57dbd7b2005-07-08 18:25:26 +00001433 testAddr = 0;
drh4794b982000-06-06 13:54:14 +00001434 }
danielk1977e014a832004-05-17 10:48:57 +00001435
1436 /* Evaluate the expression and insert it into the temp table */
1437 sqlite3ExprCode(pParse, pE2);
drh94a11212004-09-25 13:12:14 +00001438 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1);
drhf0863fe2005-06-12 21:35:51 +00001439 sqlite3VdbeAddOp(v, OP_IdxInsert, pExpr->iTable, 0);
drhfef52082000-06-06 01:50:43 +00001440 }
1441 }
danielk19770202b292004-06-09 09:55:16 +00001442 sqlite3VdbeChangeP3(v, addr, (void *)&keyInfo, P3_KEYINFO);
danielk1977b3bce662005-01-29 08:32:43 +00001443 break;
drhfef52082000-06-06 01:50:43 +00001444 }
1445
drh51522cd2005-01-20 13:36:19 +00001446 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001447 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +00001448 /* This has to be a scalar SELECT. Generate code to put the
1449 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +00001450 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +00001451 */
drh2646da72005-12-09 20:02:05 +00001452 static const Token one = { (u8*)"1", 0, 1 };
drh51522cd2005-01-20 13:36:19 +00001453 Select *pSel;
drhec7429a2005-10-06 16:53:14 +00001454 int iMem;
1455 int sop;
drh1398ad32005-01-19 23:24:50 +00001456
drhec7429a2005-10-06 16:53:14 +00001457 pExpr->iColumn = iMem = pParse->nMem++;
drh51522cd2005-01-20 13:36:19 +00001458 pSel = pExpr->pSelect;
1459 if( pExpr->op==TK_SELECT ){
1460 sop = SRT_Mem;
drhec7429a2005-10-06 16:53:14 +00001461 sqlite3VdbeAddOp(v, OP_MemNull, iMem, 0);
1462 VdbeComment((v, "# Init subquery result"));
drh51522cd2005-01-20 13:36:19 +00001463 }else{
drh51522cd2005-01-20 13:36:19 +00001464 sop = SRT_Exists;
drhec7429a2005-10-06 16:53:14 +00001465 sqlite3VdbeAddOp(v, OP_MemInt, 0, iMem);
1466 VdbeComment((v, "# Init EXISTS result"));
drh51522cd2005-01-20 13:36:19 +00001467 }
drhec7429a2005-10-06 16:53:14 +00001468 sqlite3ExprDelete(pSel->pLimit);
1469 pSel->pLimit = sqlite3Expr(TK_INTEGER, 0, 0, &one);
1470 sqlite3Select(pParse, pSel, sop, iMem, 0, 0, 0, 0);
danielk1977b3bce662005-01-29 08:32:43 +00001471 break;
drhcce7d172000-05-31 15:34:51 +00001472 }
1473 }
danielk1977b3bce662005-01-29 08:32:43 +00001474
drh57dbd7b2005-07-08 18:25:26 +00001475 if( testAddr ){
drhd654be82005-09-20 17:42:23 +00001476 sqlite3VdbeJumpHere(v, testAddr);
danielk1977b3bce662005-01-29 08:32:43 +00001477 }
1478 return;
drhcce7d172000-05-31 15:34:51 +00001479}
drh51522cd2005-01-20 13:36:19 +00001480#endif /* SQLITE_OMIT_SUBQUERY */
drhcce7d172000-05-31 15:34:51 +00001481
drhcce7d172000-05-31 15:34:51 +00001482/*
drhfec19aa2004-05-19 20:41:03 +00001483** Generate an instruction that will put the integer describe by
1484** text z[0..n-1] on the stack.
1485*/
1486static void codeInteger(Vdbe *v, const char *z, int n){
1487 int i;
drh6fec0762004-05-30 01:38:43 +00001488 if( sqlite3GetInt32(z, &i) ){
1489 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
1490 }else if( sqlite3FitsIn64Bits(z) ){
drh29dda4a2005-07-21 18:23:20 +00001491 sqlite3VdbeOp3(v, OP_Int64, 0, 0, z, n);
drhfec19aa2004-05-19 20:41:03 +00001492 }else{
1493 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n);
1494 }
1495}
1496
1497/*
drhcce7d172000-05-31 15:34:51 +00001498** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +00001499** expression and leave the result on the top of stack.
drhf2bc0132004-10-04 13:19:23 +00001500**
1501** This code depends on the fact that certain token values (ex: TK_EQ)
1502** are the same as opcode values (ex: OP_Eq) that implement the corresponding
1503** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
1504** the make process cause these values to align. Assert()s in the code
1505** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00001506*/
danielk19774adee202004-05-08 08:23:19 +00001507void sqlite3ExprCode(Parse *pParse, Expr *pExpr){
drhcce7d172000-05-31 15:34:51 +00001508 Vdbe *v = pParse->pVdbe;
1509 int op;
drhffe07b22005-11-03 00:41:17 +00001510 int stackChng = 1; /* Amount of change to stack depth */
1511
danielk19777977a172004-11-09 12:44:37 +00001512 if( v==0 ) return;
1513 if( pExpr==0 ){
drhf0863fe2005-06-12 21:35:51 +00001514 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
danielk19777977a172004-11-09 12:44:37 +00001515 return;
1516 }
drhf2bc0132004-10-04 13:19:23 +00001517 op = pExpr->op;
1518 switch( op ){
drh13449892005-09-07 21:22:45 +00001519 case TK_AGG_COLUMN: {
1520 AggInfo *pAggInfo = pExpr->pAggInfo;
1521 struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg];
1522 if( !pAggInfo->directMode ){
1523 sqlite3VdbeAddOp(v, OP_MemLoad, pCol->iMem, 0);
1524 break;
1525 }else if( pAggInfo->useSortingIdx ){
1526 sqlite3VdbeAddOp(v, OP_Column, pAggInfo->sortingIdx,
1527 pCol->iSorterColumn);
1528 break;
1529 }
1530 /* Otherwise, fall thru into the TK_COLUMN case */
1531 }
drh967e8b72000-06-21 13:59:10 +00001532 case TK_COLUMN: {
drhffe07b22005-11-03 00:41:17 +00001533 if( pExpr->iTable<0 ){
1534 /* This only happens when coding check constraints */
1535 assert( pParse->ckOffset>0 );
1536 sqlite3VdbeAddOp(v, OP_Dup, pParse->ckOffset-pExpr->iColumn-1, 1);
1537 }else if( pExpr->iColumn>=0 ){
drh8a512562005-11-14 22:29:05 +00001538 Table *pTab = pExpr->pTab;
1539 int iCol = pExpr->iColumn;
drh4cbdda92006-06-14 19:00:20 +00001540 int op = (pTab && IsVirtual(pTab)) ? OP_VColumn : OP_Column;
danielk19777dabaa12006-06-13 04:11:43 +00001541 sqlite3VdbeAddOp(v, op, pExpr->iTable, iCol);
drh8a512562005-11-14 22:29:05 +00001542 sqlite3ColumnDefault(v, pTab, iCol);
1543#ifndef SQLITE_OMIT_FLOATING_POINT
1544 if( pTab && pTab->aCol[iCol].affinity==SQLITE_AFF_REAL ){
1545 sqlite3VdbeAddOp(v, OP_RealAffinity, 0, 0);
1546 }
1547#endif
drhc4a3c772001-04-04 11:48:57 +00001548 }else{
danielk19777dabaa12006-06-13 04:11:43 +00001549 Table *pTab = pExpr->pTab;
drh4cbdda92006-06-14 19:00:20 +00001550 int op = (pTab && IsVirtual(pTab)) ? OP_VRowid : OP_Rowid;
danielk19777dabaa12006-06-13 04:11:43 +00001551 sqlite3VdbeAddOp(v, op, pExpr->iTable, 0);
drh22827922000-06-06 17:27:05 +00001552 }
drhcce7d172000-05-31 15:34:51 +00001553 break;
1554 }
1555 case TK_INTEGER: {
drh2646da72005-12-09 20:02:05 +00001556 codeInteger(v, (char*)pExpr->token.z, pExpr->token.n);
drhfec19aa2004-05-19 20:41:03 +00001557 break;
1558 }
1559 case TK_FLOAT:
1560 case TK_STRING: {
drhf2bc0132004-10-04 13:19:23 +00001561 assert( TK_FLOAT==OP_Real );
1562 assert( TK_STRING==OP_String8 );
drhd2687b72005-08-12 22:56:09 +00001563 sqlite3DequoteExpr(pExpr);
drh2646da72005-12-09 20:02:05 +00001564 sqlite3VdbeOp3(v, op, 0, 0, (char*)pExpr->token.z, pExpr->token.n);
drhcce7d172000-05-31 15:34:51 +00001565 break;
1566 }
drhf0863fe2005-06-12 21:35:51 +00001567 case TK_NULL: {
1568 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
1569 break;
1570 }
danielk19775338a5f2005-01-20 13:03:10 +00001571#ifndef SQLITE_OMIT_BLOB_LITERAL
danielk1977c572ef72004-05-27 09:28:41 +00001572 case TK_BLOB: {
drh6c8c6ce2005-08-23 11:17:58 +00001573 int n;
1574 const char *z;
drhf2bc0132004-10-04 13:19:23 +00001575 assert( TK_BLOB==OP_HexBlob );
drh6c8c6ce2005-08-23 11:17:58 +00001576 n = pExpr->token.n - 3;
drh2646da72005-12-09 20:02:05 +00001577 z = (char*)pExpr->token.z + 2;
drh6c8c6ce2005-08-23 11:17:58 +00001578 assert( n>=0 );
1579 if( n==0 ){
1580 z = "";
1581 }
1582 sqlite3VdbeOp3(v, op, 0, 0, z, n);
danielk1977c572ef72004-05-27 09:28:41 +00001583 break;
1584 }
danielk19775338a5f2005-01-20 13:03:10 +00001585#endif
drh50457892003-09-06 01:10:47 +00001586 case TK_VARIABLE: {
danielk19774adee202004-05-08 08:23:19 +00001587 sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0);
drh895d7472004-08-20 16:02:39 +00001588 if( pExpr->token.n>1 ){
drh2646da72005-12-09 20:02:05 +00001589 sqlite3VdbeChangeP3(v, -1, (char*)pExpr->token.z, pExpr->token.n);
drh895d7472004-08-20 16:02:39 +00001590 }
drh50457892003-09-06 01:10:47 +00001591 break;
1592 }
drh4e0cff62004-11-05 05:10:28 +00001593 case TK_REGISTER: {
1594 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iTable, 0);
1595 break;
1596 }
drh487e2622005-06-25 18:42:14 +00001597#ifndef SQLITE_OMIT_CAST
1598 case TK_CAST: {
1599 /* Expressions of the form: CAST(pLeft AS token) */
danielk1977f0113002006-01-24 12:09:17 +00001600 int aff, to_op;
drh487e2622005-06-25 18:42:14 +00001601 sqlite3ExprCode(pParse, pExpr->pLeft);
drh8a512562005-11-14 22:29:05 +00001602 aff = sqlite3AffinityType(&pExpr->token);
danielk1977f0113002006-01-24 12:09:17 +00001603 to_op = aff - SQLITE_AFF_TEXT + OP_ToText;
1604 assert( to_op==OP_ToText || aff!=SQLITE_AFF_TEXT );
1605 assert( to_op==OP_ToBlob || aff!=SQLITE_AFF_NONE );
1606 assert( to_op==OP_ToNumeric || aff!=SQLITE_AFF_NUMERIC );
1607 assert( to_op==OP_ToInt || aff!=SQLITE_AFF_INTEGER );
1608 assert( to_op==OP_ToReal || aff!=SQLITE_AFF_REAL );
1609 sqlite3VdbeAddOp(v, to_op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001610 stackChng = 0;
drh487e2622005-06-25 18:42:14 +00001611 break;
1612 }
1613#endif /* SQLITE_OMIT_CAST */
drhc9b84a12002-06-20 11:36:48 +00001614 case TK_LT:
1615 case TK_LE:
1616 case TK_GT:
1617 case TK_GE:
1618 case TK_NE:
1619 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00001620 assert( TK_LT==OP_Lt );
1621 assert( TK_LE==OP_Le );
1622 assert( TK_GT==OP_Gt );
1623 assert( TK_GE==OP_Ge );
1624 assert( TK_EQ==OP_Eq );
1625 assert( TK_NE==OP_Ne );
danielk1977a37cdde2004-05-16 11:15:36 +00001626 sqlite3ExprCode(pParse, pExpr->pLeft);
1627 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001628 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001629 stackChng = -1;
danielk1977a37cdde2004-05-16 11:15:36 +00001630 break;
drhc9b84a12002-06-20 11:36:48 +00001631 }
drhcce7d172000-05-31 15:34:51 +00001632 case TK_AND:
1633 case TK_OR:
1634 case TK_PLUS:
1635 case TK_STAR:
1636 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00001637 case TK_REM:
1638 case TK_BITAND:
1639 case TK_BITOR:
drh17c40292004-07-21 02:53:29 +00001640 case TK_SLASH:
drhbf4133c2001-10-13 02:59:08 +00001641 case TK_LSHIFT:
drh855eb1c2004-08-31 13:45:11 +00001642 case TK_RSHIFT:
drh00400772000-06-16 20:51:26 +00001643 case TK_CONCAT: {
drhf2bc0132004-10-04 13:19:23 +00001644 assert( TK_AND==OP_And );
1645 assert( TK_OR==OP_Or );
1646 assert( TK_PLUS==OP_Add );
1647 assert( TK_MINUS==OP_Subtract );
1648 assert( TK_REM==OP_Remainder );
1649 assert( TK_BITAND==OP_BitAnd );
1650 assert( TK_BITOR==OP_BitOr );
1651 assert( TK_SLASH==OP_Divide );
1652 assert( TK_LSHIFT==OP_ShiftLeft );
1653 assert( TK_RSHIFT==OP_ShiftRight );
1654 assert( TK_CONCAT==OP_Concat );
danielk19774adee202004-05-08 08:23:19 +00001655 sqlite3ExprCode(pParse, pExpr->pLeft);
1656 sqlite3ExprCode(pParse, pExpr->pRight);
drh855eb1c2004-08-31 13:45:11 +00001657 sqlite3VdbeAddOp(v, op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001658 stackChng = -1;
drh00400772000-06-16 20:51:26 +00001659 break;
1660 }
drhcce7d172000-05-31 15:34:51 +00001661 case TK_UMINUS: {
drhfec19aa2004-05-19 20:41:03 +00001662 Expr *pLeft = pExpr->pLeft;
1663 assert( pLeft );
1664 if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
1665 Token *p = &pLeft->token;
drh9267bdc2005-11-28 12:36:35 +00001666 char *z = sqlite3MPrintf("-%.*s", p->n, p->z);
drhfec19aa2004-05-19 20:41:03 +00001667 if( pLeft->op==TK_FLOAT ){
1668 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001669 }else{
drhfec19aa2004-05-19 20:41:03 +00001670 codeInteger(v, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001671 }
drh6e142f52000-06-08 13:36:40 +00001672 sqliteFree(z);
1673 break;
1674 }
drh1ccde152000-06-17 13:12:39 +00001675 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +00001676 }
drhbf4133c2001-10-13 02:59:08 +00001677 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00001678 case TK_NOT: {
drhf2bc0132004-10-04 13:19:23 +00001679 assert( TK_BITNOT==OP_BitNot );
1680 assert( TK_NOT==OP_Not );
danielk19774adee202004-05-08 08:23:19 +00001681 sqlite3ExprCode(pParse, pExpr->pLeft);
1682 sqlite3VdbeAddOp(v, op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001683 stackChng = 0;
drhcce7d172000-05-31 15:34:51 +00001684 break;
1685 }
1686 case TK_ISNULL:
1687 case TK_NOTNULL: {
1688 int dest;
drhf2bc0132004-10-04 13:19:23 +00001689 assert( TK_ISNULL==OP_IsNull );
1690 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00001691 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1692 sqlite3ExprCode(pParse, pExpr->pLeft);
1693 dest = sqlite3VdbeCurrentAddr(v) + 2;
1694 sqlite3VdbeAddOp(v, op, 1, dest);
1695 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);
drhffe07b22005-11-03 00:41:17 +00001696 stackChng = 0;
drhf2bc0132004-10-04 13:19:23 +00001697 break;
drhcce7d172000-05-31 15:34:51 +00001698 }
drh22827922000-06-06 17:27:05 +00001699 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00001700 AggInfo *pInfo = pExpr->pAggInfo;
drh7e56e712005-11-16 12:53:15 +00001701 if( pInfo==0 ){
1702 sqlite3ErrorMsg(pParse, "misuse of aggregate: %T",
1703 &pExpr->span);
1704 }else{
1705 sqlite3VdbeAddOp(v, OP_MemLoad, pInfo->aFunc[pExpr->iAgg].iMem, 0);
1706 }
drh22827922000-06-06 17:27:05 +00001707 break;
1708 }
drhb71090f2005-05-23 17:26:51 +00001709 case TK_CONST_FUNC:
drhcce7d172000-05-31 15:34:51 +00001710 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00001711 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00001712 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00001713 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00001714 int nId;
1715 const char *zId;
drh13449892005-09-07 21:22:45 +00001716 int constMask = 0;
danielk1977682f68b2004-06-05 10:22:17 +00001717 int i;
danielk197714db2662006-01-09 16:12:04 +00001718 u8 enc = ENC(pParse->db);
danielk1977dc1bdc42004-06-11 10:51:27 +00001719 CollSeq *pColl = 0;
drh2646da72005-12-09 20:02:05 +00001720 zId = (char*)pExpr->token.z;
drhb71090f2005-05-23 17:26:51 +00001721 nId = pExpr->token.n;
danielk1977d8123362004-06-12 09:25:12 +00001722 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001723 assert( pDef!=0 );
drhf9b596e2004-05-26 16:54:42 +00001724 nExpr = sqlite3ExprCodeExprList(pParse, pList);
drhb7f6f682006-07-08 17:06:43 +00001725#ifndef SQLITE_OMIT_VIRTUALTABLE
drha43fa222006-07-08 18:41:37 +00001726 /* Possibly overload the function if the first argument is
1727 ** a virtual table column.
1728 **
1729 ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the
1730 ** second argument, not the first, as the argument to test to
1731 ** see if it is a column in a virtual table. This is done because
1732 ** the left operand of infix functions (the operand we want to
1733 ** control overloading) ends up as the second argument to the
1734 ** function. The expression "A glob B" is equivalent to
1735 ** "glob(B,A). We want to use the A in "A glob B" to test
1736 ** for function overloading. But we use the B term in "glob(B,A)".
1737 */
drh6a03a1c2006-07-08 18:34:59 +00001738 if( nExpr>=2 && (pExpr->flags & EP_InfixFunc) ){
drh6a03a1c2006-07-08 18:34:59 +00001739 pDef = sqlite3VtabOverloadFunction(pDef, nExpr, pList->a[1].pExpr);
1740 }else if( nExpr>0 ){
drhb7f6f682006-07-08 17:06:43 +00001741 pDef = sqlite3VtabOverloadFunction(pDef, nExpr, pList->a[0].pExpr);
1742 }
1743#endif
danielk1977682f68b2004-06-05 10:22:17 +00001744 for(i=0; i<nExpr && i<32; i++){
danielk1977d02eb1f2004-06-06 09:44:03 +00001745 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
drh13449892005-09-07 21:22:45 +00001746 constMask |= (1<<i);
danielk1977d02eb1f2004-06-06 09:44:03 +00001747 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001748 if( pDef->needCollSeq && !pColl ){
1749 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
1750 }
1751 }
1752 if( pDef->needCollSeq ){
1753 if( !pColl ) pColl = pParse->db->pDfltColl;
danielk1977d8123362004-06-12 09:25:12 +00001754 sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ);
danielk1977682f68b2004-06-05 10:22:17 +00001755 }
drh13449892005-09-07 21:22:45 +00001756 sqlite3VdbeOp3(v, OP_Function, constMask, nExpr, (char*)pDef, P3_FUNCDEF);
drhffe07b22005-11-03 00:41:17 +00001757 stackChng = 1-nExpr;
drhcce7d172000-05-31 15:34:51 +00001758 break;
1759 }
drhfe2093d2005-01-20 22:48:47 +00001760#ifndef SQLITE_OMIT_SUBQUERY
1761 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001762 case TK_SELECT: {
drh41714d62006-03-02 04:44:23 +00001763 if( pExpr->iColumn==0 ){
1764 sqlite3CodeSubselect(pParse, pExpr);
1765 }
danielk19774adee202004-05-08 08:23:19 +00001766 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drhad6d9462004-09-19 02:15:24 +00001767 VdbeComment((v, "# load subquery result"));
drh19a775c2000-06-05 18:54:46 +00001768 break;
1769 }
drhfef52082000-06-06 01:50:43 +00001770 case TK_IN: {
1771 int addr;
drh94a11212004-09-25 13:12:14 +00001772 char affinity;
drhafa5f682006-01-30 14:36:59 +00001773 int ckOffset = pParse->ckOffset;
danielk1977b3bce662005-01-29 08:32:43 +00001774 sqlite3CodeSubselect(pParse, pExpr);
danielk1977e014a832004-05-17 10:48:57 +00001775
1776 /* Figure out the affinity to use to create a key from the results
1777 ** of the expression. affinityStr stores a static string suitable for
danielk1977ededfd52004-06-17 07:53:01 +00001778 ** P3 of OP_MakeRecord.
danielk1977e014a832004-05-17 10:48:57 +00001779 */
drh94a11212004-09-25 13:12:14 +00001780 affinity = comparisonAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +00001781
danielk19774adee202004-05-08 08:23:19 +00001782 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
drhafa5f682006-01-30 14:36:59 +00001783 pParse->ckOffset = ckOffset+1;
danielk1977e014a832004-05-17 10:48:57 +00001784
1785 /* Code the <expr> from "<expr> IN (...)". The temporary table
1786 ** pExpr->iTable contains the values that make up the (...) set.
1787 */
danielk19774adee202004-05-08 08:23:19 +00001788 sqlite3ExprCode(pParse, pExpr->pLeft);
1789 addr = sqlite3VdbeCurrentAddr(v);
danielk1977e014a832004-05-17 10:48:57 +00001790 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); /* addr + 0 */
danielk19774adee202004-05-08 08:23:19 +00001791 sqlite3VdbeAddOp(v, OP_Pop, 2, 0);
drhf0863fe2005-06-12 21:35:51 +00001792 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +00001793 sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7);
drh94a11212004-09-25 13:12:14 +00001794 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1); /* addr + 4 */
danielk1977e014a832004-05-17 10:48:57 +00001795 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7);
1796 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); /* addr + 6 */
1797
drhfef52082000-06-06 01:50:43 +00001798 break;
1799 }
danielk197793758c82005-01-21 08:13:14 +00001800#endif
drhfef52082000-06-06 01:50:43 +00001801 case TK_BETWEEN: {
drhbe5c89a2004-07-26 00:31:09 +00001802 Expr *pLeft = pExpr->pLeft;
1803 struct ExprList_item *pLItem = pExpr->pList->a;
1804 Expr *pRight = pLItem->pExpr;
1805 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001806 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001807 sqlite3ExprCode(pParse, pRight);
1808 codeCompare(pParse, pLeft, pRight, OP_Ge, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001809 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
drhbe5c89a2004-07-26 00:31:09 +00001810 pLItem++;
1811 pRight = pLItem->pExpr;
1812 sqlite3ExprCode(pParse, pRight);
1813 codeCompare(pParse, pLeft, pRight, OP_Le, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001814 sqlite3VdbeAddOp(v, OP_And, 0, 0);
drhfef52082000-06-06 01:50:43 +00001815 break;
1816 }
drh51e9a442004-01-16 16:42:53 +00001817 case TK_UPLUS:
drha2e00042002-01-22 03:13:42 +00001818 case TK_AS: {
danielk19774adee202004-05-08 08:23:19 +00001819 sqlite3ExprCode(pParse, pExpr->pLeft);
drhffe07b22005-11-03 00:41:17 +00001820 stackChng = 0;
drha2e00042002-01-22 03:13:42 +00001821 break;
1822 }
drh17a7f8d2002-03-24 13:13:27 +00001823 case TK_CASE: {
1824 int expr_end_label;
drhf5905aa2002-05-26 20:54:33 +00001825 int jumpInst;
drhf5905aa2002-05-26 20:54:33 +00001826 int nExpr;
drh17a7f8d2002-03-24 13:13:27 +00001827 int i;
drhbe5c89a2004-07-26 00:31:09 +00001828 ExprList *pEList;
1829 struct ExprList_item *aListelem;
drh17a7f8d2002-03-24 13:13:27 +00001830
1831 assert(pExpr->pList);
1832 assert((pExpr->pList->nExpr % 2) == 0);
1833 assert(pExpr->pList->nExpr > 0);
drhbe5c89a2004-07-26 00:31:09 +00001834 pEList = pExpr->pList;
1835 aListelem = pEList->a;
1836 nExpr = pEList->nExpr;
danielk19774adee202004-05-08 08:23:19 +00001837 expr_end_label = sqlite3VdbeMakeLabel(v);
drh17a7f8d2002-03-24 13:13:27 +00001838 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001839 sqlite3ExprCode(pParse, pExpr->pLeft);
drh17a7f8d2002-03-24 13:13:27 +00001840 }
drhf5905aa2002-05-26 20:54:33 +00001841 for(i=0; i<nExpr; i=i+2){
drhbe5c89a2004-07-26 00:31:09 +00001842 sqlite3ExprCode(pParse, aListelem[i].pExpr);
drh17a7f8d2002-03-24 13:13:27 +00001843 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001844 sqlite3VdbeAddOp(v, OP_Dup, 1, 1);
drhbe5c89a2004-07-26 00:31:09 +00001845 jumpInst = codeCompare(pParse, pExpr->pLeft, aListelem[i].pExpr,
1846 OP_Ne, 0, 1);
danielk19774adee202004-05-08 08:23:19 +00001847 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001848 }else{
danielk19774adee202004-05-08 08:23:19 +00001849 jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001850 }
drhbe5c89a2004-07-26 00:31:09 +00001851 sqlite3ExprCode(pParse, aListelem[i+1].pExpr);
danielk19774adee202004-05-08 08:23:19 +00001852 sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label);
drhd654be82005-09-20 17:42:23 +00001853 sqlite3VdbeJumpHere(v, jumpInst);
drh17a7f8d2002-03-24 13:13:27 +00001854 }
drhf570f012002-05-31 15:51:25 +00001855 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001856 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhf570f012002-05-31 15:51:25 +00001857 }
drh17a7f8d2002-03-24 13:13:27 +00001858 if( pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001859 sqlite3ExprCode(pParse, pExpr->pRight);
drh17a7f8d2002-03-24 13:13:27 +00001860 }else{
drhf0863fe2005-06-12 21:35:51 +00001861 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
drh17a7f8d2002-03-24 13:13:27 +00001862 }
danielk19774adee202004-05-08 08:23:19 +00001863 sqlite3VdbeResolveLabel(v, expr_end_label);
danielk19776f349032002-06-11 02:25:40 +00001864 break;
1865 }
danielk19775338a5f2005-01-20 13:03:10 +00001866#ifndef SQLITE_OMIT_TRIGGER
danielk19776f349032002-06-11 02:25:40 +00001867 case TK_RAISE: {
1868 if( !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +00001869 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00001870 "RAISE() may only be used within a trigger-program");
danielk19776f349032002-06-11 02:25:40 +00001871 return;
1872 }
drhad6d9462004-09-19 02:15:24 +00001873 if( pExpr->iColumn!=OE_Ignore ){
1874 assert( pExpr->iColumn==OE_Rollback ||
1875 pExpr->iColumn == OE_Abort ||
1876 pExpr->iColumn == OE_Fail );
drhd2687b72005-08-12 22:56:09 +00001877 sqlite3DequoteExpr(pExpr);
drhad6d9462004-09-19 02:15:24 +00001878 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn,
drh2646da72005-12-09 20:02:05 +00001879 (char*)pExpr->token.z, pExpr->token.n);
danielk19776f349032002-06-11 02:25:40 +00001880 } else {
drhad6d9462004-09-19 02:15:24 +00001881 assert( pExpr->iColumn == OE_Ignore );
1882 sqlite3VdbeAddOp(v, OP_ContextPop, 0, 0);
1883 sqlite3VdbeAddOp(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
1884 VdbeComment((v, "# raise(IGNORE)"));
danielk19776f349032002-06-11 02:25:40 +00001885 }
drhffe07b22005-11-03 00:41:17 +00001886 stackChng = 0;
1887 break;
drh17a7f8d2002-03-24 13:13:27 +00001888 }
danielk19775338a5f2005-01-20 13:03:10 +00001889#endif
drhffe07b22005-11-03 00:41:17 +00001890 }
1891
1892 if( pParse->ckOffset ){
1893 pParse->ckOffset += stackChng;
1894 assert( pParse->ckOffset );
drhcce7d172000-05-31 15:34:51 +00001895 }
drhcce7d172000-05-31 15:34:51 +00001896}
1897
danielk197793758c82005-01-21 08:13:14 +00001898#ifndef SQLITE_OMIT_TRIGGER
drhcce7d172000-05-31 15:34:51 +00001899/*
drh25303782004-12-07 15:41:48 +00001900** Generate code that evalutes the given expression and leaves the result
1901** on the stack. See also sqlite3ExprCode().
1902**
1903** This routine might also cache the result and modify the pExpr tree
1904** so that it will make use of the cached result on subsequent evaluations
1905** rather than evaluate the whole expression again. Trivial expressions are
1906** not cached. If the expression is cached, its result is stored in a
1907** memory location.
1908*/
1909void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr){
1910 Vdbe *v = pParse->pVdbe;
1911 int iMem;
1912 int addr1, addr2;
1913 if( v==0 ) return;
1914 addr1 = sqlite3VdbeCurrentAddr(v);
1915 sqlite3ExprCode(pParse, pExpr);
1916 addr2 = sqlite3VdbeCurrentAddr(v);
1917 if( addr2>addr1+1 || sqlite3VdbeGetOp(v, addr1)->opcode==OP_Function ){
1918 iMem = pExpr->iTable = pParse->nMem++;
1919 sqlite3VdbeAddOp(v, OP_MemStore, iMem, 0);
1920 pExpr->op = TK_REGISTER;
1921 }
1922}
danielk197793758c82005-01-21 08:13:14 +00001923#endif
drh25303782004-12-07 15:41:48 +00001924
1925/*
drh268380c2004-02-25 13:47:31 +00001926** Generate code that pushes the value of every element of the given
drhf9b596e2004-05-26 16:54:42 +00001927** expression list onto the stack.
drh268380c2004-02-25 13:47:31 +00001928**
1929** Return the number of elements pushed onto the stack.
1930*/
danielk19774adee202004-05-08 08:23:19 +00001931int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00001932 Parse *pParse, /* Parsing context */
drhf9b596e2004-05-26 16:54:42 +00001933 ExprList *pList /* The expression list to be coded */
drh268380c2004-02-25 13:47:31 +00001934){
1935 struct ExprList_item *pItem;
1936 int i, n;
drh268380c2004-02-25 13:47:31 +00001937 if( pList==0 ) return 0;
drh268380c2004-02-25 13:47:31 +00001938 n = pList->nExpr;
drhc182d162005-08-14 20:47:16 +00001939 for(pItem=pList->a, i=n; i>0; i--, pItem++){
danielk19774adee202004-05-08 08:23:19 +00001940 sqlite3ExprCode(pParse, pItem->pExpr);
drh268380c2004-02-25 13:47:31 +00001941 }
drhf9b596e2004-05-26 16:54:42 +00001942 return n;
drh268380c2004-02-25 13:47:31 +00001943}
1944
1945/*
drhcce7d172000-05-31 15:34:51 +00001946** Generate code for a boolean expression such that a jump is made
1947** to the label "dest" if the expression is true but execution
1948** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00001949**
1950** If the expression evaluates to NULL (neither true nor false), then
1951** take the jump if the jumpIfNull flag is true.
drhf2bc0132004-10-04 13:19:23 +00001952**
1953** This code depends on the fact that certain token values (ex: TK_EQ)
1954** are the same as opcode values (ex: OP_Eq) that implement the corresponding
1955** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
1956** the make process cause these values to align. Assert()s in the code
1957** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00001958*/
danielk19774adee202004-05-08 08:23:19 +00001959void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001960 Vdbe *v = pParse->pVdbe;
1961 int op = 0;
drhffe07b22005-11-03 00:41:17 +00001962 int ckOffset = pParse->ckOffset;
drhdaffd0e2001-04-11 14:28:42 +00001963 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00001964 op = pExpr->op;
1965 switch( op ){
drhcce7d172000-05-31 15:34:51 +00001966 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001967 int d2 = sqlite3VdbeMakeLabel(v);
1968 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
1969 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
1970 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001971 break;
1972 }
1973 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001974 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
1975 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001976 break;
1977 }
1978 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001979 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001980 break;
1981 }
1982 case TK_LT:
1983 case TK_LE:
1984 case TK_GT:
1985 case TK_GE:
1986 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00001987 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00001988 assert( TK_LT==OP_Lt );
1989 assert( TK_LE==OP_Le );
1990 assert( TK_GT==OP_Gt );
1991 assert( TK_GE==OP_Ge );
1992 assert( TK_EQ==OP_Eq );
1993 assert( TK_NE==OP_Ne );
danielk19774adee202004-05-08 08:23:19 +00001994 sqlite3ExprCode(pParse, pExpr->pLeft);
1995 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001996 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001997 break;
1998 }
1999 case TK_ISNULL:
2000 case TK_NOTNULL: {
drhf2bc0132004-10-04 13:19:23 +00002001 assert( TK_ISNULL==OP_IsNull );
2002 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00002003 sqlite3ExprCode(pParse, pExpr->pLeft);
2004 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00002005 break;
2006 }
drhfef52082000-06-06 01:50:43 +00002007 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00002008 /* The expression "x BETWEEN y AND z" is implemented as:
2009 **
2010 ** 1 IF (x < y) GOTO 3
2011 ** 2 IF (x <= z) GOTO <dest>
2012 ** 3 ...
2013 */
drhf5905aa2002-05-26 20:54:33 +00002014 int addr;
drhbe5c89a2004-07-26 00:31:09 +00002015 Expr *pLeft = pExpr->pLeft;
2016 Expr *pRight = pExpr->pList->a[0].pExpr;
2017 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00002018 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00002019 sqlite3ExprCode(pParse, pRight);
2020 addr = codeCompare(pParse, pLeft, pRight, OP_Lt, 0, !jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00002021
drhbe5c89a2004-07-26 00:31:09 +00002022 pRight = pExpr->pList->a[1].pExpr;
2023 sqlite3ExprCode(pParse, pRight);
2024 codeCompare(pParse, pLeft, pRight, OP_Le, dest, jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00002025
danielk19774adee202004-05-08 08:23:19 +00002026 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
drhd654be82005-09-20 17:42:23 +00002027 sqlite3VdbeJumpHere(v, addr);
danielk19774adee202004-05-08 08:23:19 +00002028 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00002029 break;
2030 }
drhcce7d172000-05-31 15:34:51 +00002031 default: {
danielk19774adee202004-05-08 08:23:19 +00002032 sqlite3ExprCode(pParse, pExpr);
2033 sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00002034 break;
2035 }
2036 }
drhffe07b22005-11-03 00:41:17 +00002037 pParse->ckOffset = ckOffset;
drhcce7d172000-05-31 15:34:51 +00002038}
2039
2040/*
drh66b89c82000-11-28 20:47:17 +00002041** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00002042** to the label "dest" if the expression is false but execution
2043** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00002044**
2045** If the expression evaluates to NULL (neither true nor false) then
2046** jump if jumpIfNull is true or fall through if jumpIfNull is false.
drhcce7d172000-05-31 15:34:51 +00002047*/
danielk19774adee202004-05-08 08:23:19 +00002048void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00002049 Vdbe *v = pParse->pVdbe;
2050 int op = 0;
drhffe07b22005-11-03 00:41:17 +00002051 int ckOffset = pParse->ckOffset;
drhdaffd0e2001-04-11 14:28:42 +00002052 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00002053
2054 /* The value of pExpr->op and op are related as follows:
2055 **
2056 ** pExpr->op op
2057 ** --------- ----------
2058 ** TK_ISNULL OP_NotNull
2059 ** TK_NOTNULL OP_IsNull
2060 ** TK_NE OP_Eq
2061 ** TK_EQ OP_Ne
2062 ** TK_GT OP_Le
2063 ** TK_LE OP_Gt
2064 ** TK_GE OP_Lt
2065 ** TK_LT OP_Ge
2066 **
2067 ** For other values of pExpr->op, op is undefined and unused.
2068 ** The value of TK_ and OP_ constants are arranged such that we
2069 ** can compute the mapping above using the following expression.
2070 ** Assert()s verify that the computation is correct.
2071 */
2072 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
2073
2074 /* Verify correct alignment of TK_ and OP_ constants
2075 */
2076 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
2077 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
2078 assert( pExpr->op!=TK_NE || op==OP_Eq );
2079 assert( pExpr->op!=TK_EQ || op==OP_Ne );
2080 assert( pExpr->op!=TK_LT || op==OP_Ge );
2081 assert( pExpr->op!=TK_LE || op==OP_Gt );
2082 assert( pExpr->op!=TK_GT || op==OP_Le );
2083 assert( pExpr->op!=TK_GE || op==OP_Lt );
2084
drhcce7d172000-05-31 15:34:51 +00002085 switch( pExpr->op ){
2086 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00002087 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
2088 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002089 break;
2090 }
2091 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00002092 int d2 = sqlite3VdbeMakeLabel(v);
2093 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
2094 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
2095 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00002096 break;
2097 }
2098 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00002099 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002100 break;
2101 }
2102 case TK_LT:
2103 case TK_LE:
2104 case TK_GT:
2105 case TK_GE:
2106 case TK_NE:
2107 case TK_EQ: {
danielk19774adee202004-05-08 08:23:19 +00002108 sqlite3ExprCode(pParse, pExpr->pLeft);
2109 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00002110 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002111 break;
2112 }
drhcce7d172000-05-31 15:34:51 +00002113 case TK_ISNULL:
2114 case TK_NOTNULL: {
danielk19774adee202004-05-08 08:23:19 +00002115 sqlite3ExprCode(pParse, pExpr->pLeft);
2116 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00002117 break;
2118 }
drhfef52082000-06-06 01:50:43 +00002119 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00002120 /* The expression is "x BETWEEN y AND z". It is implemented as:
2121 **
2122 ** 1 IF (x >= y) GOTO 3
2123 ** 2 GOTO <dest>
2124 ** 3 IF (x > z) GOTO <dest>
2125 */
drhfef52082000-06-06 01:50:43 +00002126 int addr;
drhbe5c89a2004-07-26 00:31:09 +00002127 Expr *pLeft = pExpr->pLeft;
2128 Expr *pRight = pExpr->pList->a[0].pExpr;
2129 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00002130 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00002131 sqlite3ExprCode(pParse, pRight);
danielk19774adee202004-05-08 08:23:19 +00002132 addr = sqlite3VdbeCurrentAddr(v);
drhbe5c89a2004-07-26 00:31:09 +00002133 codeCompare(pParse, pLeft, pRight, OP_Ge, addr+3, !jumpIfNull);
2134
danielk19774adee202004-05-08 08:23:19 +00002135 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
2136 sqlite3VdbeAddOp(v, OP_Goto, 0, dest);
drhbe5c89a2004-07-26 00:31:09 +00002137 pRight = pExpr->pList->a[1].pExpr;
2138 sqlite3ExprCode(pParse, pRight);
2139 codeCompare(pParse, pLeft, pRight, OP_Gt, dest, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00002140 break;
2141 }
drhcce7d172000-05-31 15:34:51 +00002142 default: {
danielk19774adee202004-05-08 08:23:19 +00002143 sqlite3ExprCode(pParse, pExpr);
2144 sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00002145 break;
2146 }
2147 }
drhffe07b22005-11-03 00:41:17 +00002148 pParse->ckOffset = ckOffset;
drhcce7d172000-05-31 15:34:51 +00002149}
drh22827922000-06-06 17:27:05 +00002150
2151/*
2152** Do a deep comparison of two expression trees. Return TRUE (non-zero)
2153** if they are identical and return FALSE if they differ in any way.
2154*/
danielk19774adee202004-05-08 08:23:19 +00002155int sqlite3ExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00002156 int i;
danielk19774b202ae2006-01-23 05:50:58 +00002157 if( pA==0||pB==0 ){
2158 return pB==pA;
drh22827922000-06-06 17:27:05 +00002159 }
2160 if( pA->op!=pB->op ) return 0;
drhfd357972005-09-09 01:33:19 +00002161 if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 0;
danielk19774adee202004-05-08 08:23:19 +00002162 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
2163 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00002164 if( pA->pList ){
2165 if( pB->pList==0 ) return 0;
2166 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
2167 for(i=0; i<pA->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00002168 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00002169 return 0;
2170 }
2171 }
2172 }else if( pB->pList ){
2173 return 0;
2174 }
2175 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00002176 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drh22827922000-06-06 17:27:05 +00002177 if( pA->token.z ){
2178 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00002179 if( pB->token.n!=pA->token.n ) return 0;
drh2646da72005-12-09 20:02:05 +00002180 if( sqlite3StrNICmp((char*)pA->token.z,(char*)pB->token.z,pB->token.n)!=0 ){
2181 return 0;
2182 }
drh22827922000-06-06 17:27:05 +00002183 }
2184 return 1;
2185}
2186
drh13449892005-09-07 21:22:45 +00002187
drh22827922000-06-06 17:27:05 +00002188/*
drh13449892005-09-07 21:22:45 +00002189** Add a new element to the pAggInfo->aCol[] array. Return the index of
2190** the new element. Return a negative number if malloc fails.
drh22827922000-06-06 17:27:05 +00002191*/
drh13449892005-09-07 21:22:45 +00002192static int addAggInfoColumn(AggInfo *pInfo){
2193 int i;
2194 i = sqlite3ArrayAllocate((void**)&pInfo->aCol, sizeof(pInfo->aCol[0]), 3);
2195 if( i<0 ){
2196 return -1;
drh22827922000-06-06 17:27:05 +00002197 }
drh13449892005-09-07 21:22:45 +00002198 return i;
2199}
2200
2201/*
2202** Add a new element to the pAggInfo->aFunc[] array. Return the index of
2203** the new element. Return a negative number if malloc fails.
2204*/
2205static int addAggInfoFunc(AggInfo *pInfo){
2206 int i;
2207 i = sqlite3ArrayAllocate((void**)&pInfo->aFunc, sizeof(pInfo->aFunc[0]), 2);
2208 if( i<0 ){
2209 return -1;
2210 }
2211 return i;
2212}
drh22827922000-06-06 17:27:05 +00002213
2214/*
drh626a8792005-01-17 22:08:19 +00002215** This is an xFunc for walkExprTree() used to implement
2216** sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates
2217** for additional information.
drh22827922000-06-06 17:27:05 +00002218**
drh626a8792005-01-17 22:08:19 +00002219** This routine analyzes the aggregate function at pExpr.
drh22827922000-06-06 17:27:05 +00002220*/
drh626a8792005-01-17 22:08:19 +00002221static int analyzeAggregate(void *pArg, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00002222 int i;
danielk1977a58fdfb2005-02-08 07:50:40 +00002223 NameContext *pNC = (NameContext *)pArg;
2224 Parse *pParse = pNC->pParse;
2225 SrcList *pSrcList = pNC->pSrcList;
drh13449892005-09-07 21:22:45 +00002226 AggInfo *pAggInfo = pNC->pAggInfo;
2227
drh22827922000-06-06 17:27:05 +00002228
drh22827922000-06-06 17:27:05 +00002229 switch( pExpr->op ){
drh89c69d02007-01-04 01:20:28 +00002230 case TK_AGG_COLUMN:
drh967e8b72000-06-21 13:59:10 +00002231 case TK_COLUMN: {
drh13449892005-09-07 21:22:45 +00002232 /* Check to see if the column is in one of the tables in the FROM
2233 ** clause of the aggregate query */
2234 if( pSrcList ){
2235 struct SrcList_item *pItem = pSrcList->a;
2236 for(i=0; i<pSrcList->nSrc; i++, pItem++){
2237 struct AggInfo_col *pCol;
2238 if( pExpr->iTable==pItem->iCursor ){
2239 /* If we reach this point, it means that pExpr refers to a table
2240 ** that is in the FROM clause of the aggregate query.
2241 **
2242 ** Make an entry for the column in pAggInfo->aCol[] if there
2243 ** is not an entry there already.
2244 */
2245 pCol = pAggInfo->aCol;
2246 for(i=0; i<pAggInfo->nColumn; i++, pCol++){
2247 if( pCol->iTable==pExpr->iTable &&
2248 pCol->iColumn==pExpr->iColumn ){
2249 break;
2250 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002251 }
drh13449892005-09-07 21:22:45 +00002252 if( i>=pAggInfo->nColumn && (i = addAggInfoColumn(pAggInfo))>=0 ){
2253 pCol = &pAggInfo->aCol[i];
2254 pCol->iTable = pExpr->iTable;
2255 pCol->iColumn = pExpr->iColumn;
2256 pCol->iMem = pParse->nMem++;
2257 pCol->iSorterColumn = -1;
drh5774b802005-09-07 22:48:16 +00002258 pCol->pExpr = pExpr;
drh13449892005-09-07 21:22:45 +00002259 if( pAggInfo->pGroupBy ){
2260 int j, n;
2261 ExprList *pGB = pAggInfo->pGroupBy;
2262 struct ExprList_item *pTerm = pGB->a;
2263 n = pGB->nExpr;
2264 for(j=0; j<n; j++, pTerm++){
2265 Expr *pE = pTerm->pExpr;
2266 if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable &&
2267 pE->iColumn==pExpr->iColumn ){
2268 pCol->iSorterColumn = j;
2269 break;
2270 }
2271 }
2272 }
2273 if( pCol->iSorterColumn<0 ){
2274 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
2275 }
2276 }
2277 /* There is now an entry for pExpr in pAggInfo->aCol[] (either
2278 ** because it was there before or because we just created it).
2279 ** Convert the pExpr to be a TK_AGG_COLUMN referring to that
2280 ** pAggInfo->aCol[] entry.
2281 */
2282 pExpr->pAggInfo = pAggInfo;
2283 pExpr->op = TK_AGG_COLUMN;
2284 pExpr->iAgg = i;
2285 break;
2286 } /* endif pExpr->iTable==pItem->iCursor */
2287 } /* end loop over pSrcList */
drh22827922000-06-06 17:27:05 +00002288 }
drh626a8792005-01-17 22:08:19 +00002289 return 1;
drh22827922000-06-06 17:27:05 +00002290 }
2291 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00002292 /* The pNC->nDepth==0 test causes aggregate functions in subqueries
2293 ** to be ignored */
danielk1977a58fdfb2005-02-08 07:50:40 +00002294 if( pNC->nDepth==0 ){
drh13449892005-09-07 21:22:45 +00002295 /* Check to see if pExpr is a duplicate of another aggregate
2296 ** function that is already in the pAggInfo structure
2297 */
2298 struct AggInfo_func *pItem = pAggInfo->aFunc;
2299 for(i=0; i<pAggInfo->nFunc; i++, pItem++){
2300 if( sqlite3ExprCompare(pItem->pExpr, pExpr) ){
danielk1977a58fdfb2005-02-08 07:50:40 +00002301 break;
2302 }
drh22827922000-06-06 17:27:05 +00002303 }
drh13449892005-09-07 21:22:45 +00002304 if( i>=pAggInfo->nFunc ){
2305 /* pExpr is original. Make a new entry in pAggInfo->aFunc[]
2306 */
danielk197714db2662006-01-09 16:12:04 +00002307 u8 enc = ENC(pParse->db);
drh13449892005-09-07 21:22:45 +00002308 i = addAggInfoFunc(pAggInfo);
2309 if( i>=0 ){
2310 pItem = &pAggInfo->aFunc[i];
2311 pItem->pExpr = pExpr;
2312 pItem->iMem = pParse->nMem++;
2313 pItem->pFunc = sqlite3FindFunction(pParse->db,
drh2646da72005-12-09 20:02:05 +00002314 (char*)pExpr->token.z, pExpr->token.n,
drh13449892005-09-07 21:22:45 +00002315 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
drhfd357972005-09-09 01:33:19 +00002316 if( pExpr->flags & EP_Distinct ){
2317 pItem->iDistinct = pParse->nTab++;
2318 }else{
2319 pItem->iDistinct = -1;
2320 }
drh13449892005-09-07 21:22:45 +00002321 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002322 }
drh13449892005-09-07 21:22:45 +00002323 /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
2324 */
danielk1977a58fdfb2005-02-08 07:50:40 +00002325 pExpr->iAgg = i;
drh13449892005-09-07 21:22:45 +00002326 pExpr->pAggInfo = pAggInfo;
danielk1977a58fdfb2005-02-08 07:50:40 +00002327 return 1;
drh22827922000-06-06 17:27:05 +00002328 }
drh22827922000-06-06 17:27:05 +00002329 }
2330 }
drh13449892005-09-07 21:22:45 +00002331
2332 /* Recursively walk subqueries looking for TK_COLUMN nodes that need
2333 ** to be changed to TK_AGG_COLUMN. But increment nDepth so that
2334 ** TK_AGG_FUNCTION nodes in subqueries will be unchanged.
2335 */
danielk1977a58fdfb2005-02-08 07:50:40 +00002336 if( pExpr->pSelect ){
2337 pNC->nDepth++;
2338 walkSelectExpr(pExpr->pSelect, analyzeAggregate, pNC);
2339 pNC->nDepth--;
2340 }
drh626a8792005-01-17 22:08:19 +00002341 return 0;
2342}
2343
2344/*
2345** Analyze the given expression looking for aggregate functions and
2346** for variables that need to be added to the pParse->aAgg[] array.
2347** Make additional entries to the pParse->aAgg[] array as necessary.
2348**
2349** This routine should only be called after the expression has been
2350** analyzed by sqlite3ExprResolveNames().
2351**
2352** If errors are seen, leave an error message in zErrMsg and return
2353** the number of errors.
2354*/
danielk1977a58fdfb2005-02-08 07:50:40 +00002355int sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
2356 int nErr = pNC->pParse->nErr;
2357 walkExprTree(pExpr, analyzeAggregate, pNC);
2358 return pNC->pParse->nErr - nErr;
drh22827922000-06-06 17:27:05 +00002359}
drh5d9a4af2005-08-30 00:54:01 +00002360
2361/*
2362** Call sqlite3ExprAnalyzeAggregates() for every expression in an
2363** expression list. Return the number of errors.
2364**
2365** If an error is found, the analysis is cut short.
2366*/
2367int sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){
2368 struct ExprList_item *pItem;
2369 int i;
2370 int nErr = 0;
2371 if( pList ){
2372 for(pItem=pList->a, i=0; nErr==0 && i<pList->nExpr; i++, pItem++){
2373 nErr += sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
2374 }
2375 }
2376 return nErr;
2377}