blob: 6724b39a1dee0d78fa4f93cb90840fb973e53c45 [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**
drhd40aab02007-02-24 15:29:03 +000015** $Id: expr.c,v 1.280 2007/02/24 15:29:04 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.
drha34001c2007-02-02 12:44:37 +000055** The collating sequence is marked as "explicit" using the EP_ExpCollate
56** flag. An explicit collating sequence will override implicit
57** collating sequences.
drh8b4c40d2007-02-01 23:02:45 +000058*/
59Expr *sqlite3ExprSetColl(Parse *pParse, Expr *pExpr, Token *pName){
60 CollSeq *pColl;
61 if( pExpr==0 ) return 0;
62 pColl = sqlite3LocateCollSeq(pParse, (char*)pName->z, pName->n);
63 if( pColl ){
64 pExpr->pColl = pColl;
65 pExpr->flags |= EP_ExpCollate;
66 }
67 return pExpr;
68}
69
70/*
danielk19770202b292004-06-09 09:55:16 +000071** Return the default collation sequence for the expression pExpr. If
72** there is no default collation type, return 0.
73*/
danielk19777cedc8d2004-06-10 10:50:08 +000074CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
75 CollSeq *pColl = 0;
danielk19770202b292004-06-09 09:55:16 +000076 if( pExpr ){
danielk19777cedc8d2004-06-10 10:50:08 +000077 pColl = pExpr->pColl;
drh487e2622005-06-25 18:42:14 +000078 if( (pExpr->op==TK_AS || pExpr->op==TK_CAST) && !pColl ){
danielk19777cedc8d2004-06-10 10:50:08 +000079 return sqlite3ExprCollSeq(pParse, pExpr->pLeft);
danielk19770202b292004-06-09 09:55:16 +000080 }
81 }
danielk19777cedc8d2004-06-10 10:50:08 +000082 if( sqlite3CheckCollSeq(pParse, pColl) ){
83 pColl = 0;
84 }
85 return pColl;
danielk19770202b292004-06-09 09:55:16 +000086}
87
88/*
drh626a8792005-01-17 22:08:19 +000089** pExpr is an operand of a comparison operator. aff2 is the
90** type affinity of the other operand. This routine returns the
drh53db1452004-05-20 13:54:53 +000091** type affinity that should be used for the comparison operator.
92*/
danielk1977e014a832004-05-17 10:48:57 +000093char sqlite3CompareAffinity(Expr *pExpr, char aff2){
danielk1977bf3b7212004-05-18 10:06:24 +000094 char aff1 = sqlite3ExprAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +000095 if( aff1 && aff2 ){
drh8df447f2005-11-01 15:48:24 +000096 /* Both sides of the comparison are columns. If one has numeric
97 ** affinity, use that. Otherwise use no affinity.
danielk1977e014a832004-05-17 10:48:57 +000098 */
drh8a512562005-11-14 22:29:05 +000099 if( sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2) ){
danielk1977e014a832004-05-17 10:48:57 +0000100 return SQLITE_AFF_NUMERIC;
101 }else{
102 return SQLITE_AFF_NONE;
103 }
104 }else if( !aff1 && !aff2 ){
drh5f6a87b2004-07-19 00:39:45 +0000105 /* Neither side of the comparison is a column. Compare the
106 ** results directly.
danielk1977e014a832004-05-17 10:48:57 +0000107 */
drh5f6a87b2004-07-19 00:39:45 +0000108 return SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +0000109 }else{
110 /* One side is a column, the other is not. Use the columns affinity. */
drhfe05af82005-07-21 03:14:59 +0000111 assert( aff1==0 || aff2==0 );
danielk1977e014a832004-05-17 10:48:57 +0000112 return (aff1 + aff2);
113 }
114}
115
drh53db1452004-05-20 13:54:53 +0000116/*
117** pExpr is a comparison operator. Return the type affinity that should
118** be applied to both operands prior to doing the comparison.
119*/
danielk1977e014a832004-05-17 10:48:57 +0000120static char comparisonAffinity(Expr *pExpr){
121 char aff;
122 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
123 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
124 pExpr->op==TK_NE );
125 assert( pExpr->pLeft );
danielk1977bf3b7212004-05-18 10:06:24 +0000126 aff = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +0000127 if( pExpr->pRight ){
128 aff = sqlite3CompareAffinity(pExpr->pRight, aff);
129 }
130 else if( pExpr->pSelect ){
131 aff = sqlite3CompareAffinity(pExpr->pSelect->pEList->a[0].pExpr, aff);
132 }
133 else if( !aff ){
drhde087bd2007-02-23 03:00:44 +0000134 aff = SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +0000135 }
136 return aff;
137}
138
139/*
140** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
141** idx_affinity is the affinity of an indexed column. Return true
142** if the index with affinity idx_affinity may be used to implement
143** the comparison in pExpr.
144*/
145int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){
146 char aff = comparisonAffinity(pExpr);
drh8a512562005-11-14 22:29:05 +0000147 switch( aff ){
148 case SQLITE_AFF_NONE:
149 return 1;
150 case SQLITE_AFF_TEXT:
151 return idx_affinity==SQLITE_AFF_TEXT;
152 default:
153 return sqlite3IsNumericAffinity(idx_affinity);
154 }
danielk1977e014a832004-05-17 10:48:57 +0000155}
156
danielk1977a37cdde2004-05-16 11:15:36 +0000157/*
158** Return the P1 value that should be used for a binary comparison
159** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
160** If jumpIfNull is true, then set the low byte of the returned
161** P1 value to tell the opcode to jump if either expression
162** evaluates to NULL.
163*/
danielk1977e014a832004-05-17 10:48:57 +0000164static int binaryCompareP1(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){
danielk1977bf3b7212004-05-18 10:06:24 +0000165 char aff = sqlite3ExprAffinity(pExpr2);
drhf0863fe2005-06-12 21:35:51 +0000166 return ((int)sqlite3CompareAffinity(pExpr1, aff))+(jumpIfNull?0x100:0);
danielk1977a37cdde2004-05-16 11:15:36 +0000167}
168
drha2e00042002-01-22 03:13:42 +0000169/*
danielk19770202b292004-06-09 09:55:16 +0000170** Return a pointer to the collation sequence that should be used by
171** a binary comparison operator comparing pLeft and pRight.
172**
173** If the left hand expression has a collating sequence type, then it is
174** used. Otherwise the collation sequence for the right hand expression
175** is used, or the default (BINARY) if neither expression has a collating
176** type.
177*/
danielk19777cedc8d2004-06-10 10:50:08 +0000178static CollSeq* binaryCompareCollSeq(Parse *pParse, Expr *pLeft, Expr *pRight){
drhec41dda2007-02-07 13:09:45 +0000179 CollSeq *pColl;
180 assert( pLeft );
181 assert( pRight );
182 if( pLeft->flags & EP_ExpCollate ){
183 assert( pLeft->pColl );
184 pColl = pLeft->pColl;
185 }else if( pRight->flags & EP_ExpCollate ){
186 assert( pRight->pColl );
187 pColl = pRight->pColl;
188 }else{
189 pColl = sqlite3ExprCollSeq(pParse, pLeft);
190 if( !pColl ){
191 pColl = sqlite3ExprCollSeq(pParse, pRight);
192 }
danielk19770202b292004-06-09 09:55:16 +0000193 }
194 return pColl;
195}
196
197/*
drhbe5c89a2004-07-26 00:31:09 +0000198** Generate code for a comparison operator.
199*/
200static int codeCompare(
201 Parse *pParse, /* The parsing (and code generating) context */
202 Expr *pLeft, /* The left operand */
203 Expr *pRight, /* The right operand */
204 int opcode, /* The comparison opcode */
205 int dest, /* Jump here if true. */
206 int jumpIfNull /* If true, jump if either operand is NULL */
207){
208 int p1 = binaryCompareP1(pLeft, pRight, jumpIfNull);
209 CollSeq *p3 = binaryCompareCollSeq(pParse, pLeft, pRight);
drh94a11212004-09-25 13:12:14 +0000210 return sqlite3VdbeOp3(pParse->pVdbe, opcode, p1, dest, (void*)p3, P3_COLLSEQ);
drhbe5c89a2004-07-26 00:31:09 +0000211}
212
213/*
drha76b5df2002-02-23 02:32:10 +0000214** Construct a new expression node and return a pointer to it. Memory
215** for this node is obtained from sqliteMalloc(). The calling function
216** is responsible for making sure the node eventually gets freed.
217*/
drhe4e72072004-11-23 01:47:30 +0000218Expr *sqlite3Expr(int op, Expr *pLeft, Expr *pRight, const Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000219 Expr *pNew;
220 pNew = sqliteMalloc( sizeof(Expr) );
221 if( pNew==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000222 /* When malloc fails, delete pLeft and pRight. Expressions passed to
223 ** this function must always be allocated with sqlite3Expr() for this
224 ** reason.
225 */
226 sqlite3ExprDelete(pLeft);
227 sqlite3ExprDelete(pRight);
drha76b5df2002-02-23 02:32:10 +0000228 return 0;
229 }
230 pNew->op = op;
231 pNew->pLeft = pLeft;
232 pNew->pRight = pRight;
danielk1977a58fdfb2005-02-08 07:50:40 +0000233 pNew->iAgg = -1;
drha76b5df2002-02-23 02:32:10 +0000234 if( pToken ){
drh4b59ab52002-08-24 18:24:51 +0000235 assert( pToken->dyn==0 );
drh145716b2004-09-24 12:24:06 +0000236 pNew->span = pNew->token = *pToken;
drha34001c2007-02-02 12:44:37 +0000237 }else if( pLeft ){
238 if( pRight ){
239 sqlite3ExprSpan(pNew, &pLeft->span, &pRight->span);
240 if( pRight->flags && EP_ExpCollate ){
241 pNew->flags |= EP_ExpCollate;
242 pNew->pColl = pRight->pColl;
243 }
244 }
245 if( pLeft->flags && EP_ExpCollate ){
246 pNew->flags |= EP_ExpCollate;
247 pNew->pColl = pLeft->pColl;
248 }
drha76b5df2002-02-23 02:32:10 +0000249 }
drha76b5df2002-02-23 02:32:10 +0000250 return pNew;
251}
252
253/*
drh206f3d92006-07-11 13:15:08 +0000254** Works like sqlite3Expr() but frees its pLeft and pRight arguments
255** if it fails due to a malloc problem.
256*/
257Expr *sqlite3ExprOrFree(int op, Expr *pLeft, Expr *pRight, const Token *pToken){
258 Expr *pNew = sqlite3Expr(op, pLeft, pRight, pToken);
259 if( pNew==0 ){
260 sqlite3ExprDelete(pLeft);
261 sqlite3ExprDelete(pRight);
262 }
263 return pNew;
264}
265
266/*
drh4e0cff62004-11-05 05:10:28 +0000267** When doing a nested parse, you can include terms in an expression
268** that look like this: #0 #1 #2 ... These terms refer to elements
drh288d37f2005-06-22 08:48:06 +0000269** on the stack. "#0" means the top of the stack.
270** "#1" means the next down on the stack. And so forth.
drh4e0cff62004-11-05 05:10:28 +0000271**
272** This routine is called by the parser to deal with on of those terms.
273** It immediately generates code to store the value in a memory location.
274** The returns an expression that will code to extract the value from
275** that memory location as needed.
276*/
277Expr *sqlite3RegisterExpr(Parse *pParse, Token *pToken){
278 Vdbe *v = pParse->pVdbe;
279 Expr *p;
280 int depth;
drh4e0cff62004-11-05 05:10:28 +0000281 if( pParse->nested==0 ){
282 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", pToken);
283 return 0;
284 }
drhbb7ac002005-08-12 22:58:53 +0000285 if( v==0 ) return 0;
drh4e0cff62004-11-05 05:10:28 +0000286 p = sqlite3Expr(TK_REGISTER, 0, 0, pToken);
drh73c42a12004-11-20 18:13:10 +0000287 if( p==0 ){
288 return 0; /* Malloc failed */
289 }
drh2646da72005-12-09 20:02:05 +0000290 depth = atoi((char*)&pToken->z[1]);
drh288d37f2005-06-22 08:48:06 +0000291 p->iTable = pParse->nMem++;
292 sqlite3VdbeAddOp(v, OP_Dup, depth, 0);
293 sqlite3VdbeAddOp(v, OP_MemStore, p->iTable, 1);
drh4e0cff62004-11-05 05:10:28 +0000294 return p;
295}
296
297/*
drh91bb0ee2004-09-01 03:06:34 +0000298** Join two expressions using an AND operator. If either expression is
299** NULL, then just return the other expression.
300*/
301Expr *sqlite3ExprAnd(Expr *pLeft, Expr *pRight){
302 if( pLeft==0 ){
303 return pRight;
304 }else if( pRight==0 ){
305 return pLeft;
306 }else{
307 return sqlite3Expr(TK_AND, pLeft, pRight, 0);
308 }
309}
310
311/*
drh6977fea2002-10-22 23:38:04 +0000312** Set the Expr.span field of the given expression to span all
drha76b5df2002-02-23 02:32:10 +0000313** text between the two given tokens.
314*/
danielk19774adee202004-05-08 08:23:19 +0000315void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
drh4efc4752004-01-16 15:55:37 +0000316 assert( pRight!=0 );
317 assert( pLeft!=0 );
danielk19779e128002006-01-18 16:51:35 +0000318 if( !sqlite3MallocFailed() && pRight->z && pLeft->z ){
drhad6d9462004-09-19 02:15:24 +0000319 assert( pLeft->dyn==0 || pLeft->z[pLeft->n]==0 );
drh145716b2004-09-24 12:24:06 +0000320 if( pLeft->dyn==0 && pRight->dyn==0 ){
drh6977fea2002-10-22 23:38:04 +0000321 pExpr->span.z = pLeft->z;
drh97903fe2005-05-24 20:19:57 +0000322 pExpr->span.n = pRight->n + (pRight->z - pLeft->z);
drh4b59ab52002-08-24 18:24:51 +0000323 }else{
drh6977fea2002-10-22 23:38:04 +0000324 pExpr->span.z = 0;
drh4b59ab52002-08-24 18:24:51 +0000325 }
drha76b5df2002-02-23 02:32:10 +0000326 }
327}
328
329/*
330** Construct a new expression node for a function with multiple
331** arguments.
332*/
danielk19774adee202004-05-08 08:23:19 +0000333Expr *sqlite3ExprFunction(ExprList *pList, Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000334 Expr *pNew;
danielk19774b202ae2006-01-23 05:50:58 +0000335 assert( pToken );
drha76b5df2002-02-23 02:32:10 +0000336 pNew = sqliteMalloc( sizeof(Expr) );
337 if( pNew==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000338 sqlite3ExprListDelete(pList); /* Avoid leaking memory when malloc fails */
drha76b5df2002-02-23 02:32:10 +0000339 return 0;
340 }
341 pNew->op = TK_FUNCTION;
342 pNew->pList = pList;
danielk19774b202ae2006-01-23 05:50:58 +0000343 assert( pToken->dyn==0 );
344 pNew->token = *pToken;
drh6977fea2002-10-22 23:38:04 +0000345 pNew->span = pNew->token;
drha76b5df2002-02-23 02:32:10 +0000346 return pNew;
347}
348
349/*
drhfa6bc002004-09-07 16:19:52 +0000350** Assign a variable number to an expression that encodes a wildcard
351** in the original SQL statement.
352**
353** Wildcards consisting of a single "?" are assigned the next sequential
354** variable number.
355**
356** Wildcards of the form "?nnn" are assigned the number "nnn". We make
357** sure "nnn" is not too be to avoid a denial of service attack when
358** the SQL statement comes from an external source.
359**
360** Wildcards of the form ":aaa" or "$aaa" are assigned the same number
361** as the previous instance of the same wildcard. Or if this is the first
362** instance of the wildcard, the next sequenial variable number is
363** assigned.
364*/
365void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){
366 Token *pToken;
367 if( pExpr==0 ) return;
368 pToken = &pExpr->token;
369 assert( pToken->n>=1 );
370 assert( pToken->z!=0 );
371 assert( pToken->z[0]!=0 );
372 if( pToken->n==1 ){
373 /* Wildcard of the form "?". Assign the next variable number */
374 pExpr->iTable = ++pParse->nVar;
375 }else if( pToken->z[0]=='?' ){
376 /* Wildcard of the form "?nnn". Convert "nnn" to an integer and
377 ** use it as the variable number */
378 int i;
drh2646da72005-12-09 20:02:05 +0000379 pExpr->iTable = i = atoi((char*)&pToken->z[1]);
drhfa6bc002004-09-07 16:19:52 +0000380 if( i<1 || i>SQLITE_MAX_VARIABLE_NUMBER ){
381 sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
382 SQLITE_MAX_VARIABLE_NUMBER);
383 }
384 if( i>pParse->nVar ){
385 pParse->nVar = i;
386 }
387 }else{
388 /* Wildcards of the form ":aaa" or "$aaa". Reuse the same variable
389 ** number as the prior appearance of the same name, or if the name
390 ** has never appeared before, reuse the same variable number
391 */
392 int i, n;
393 n = pToken->n;
394 for(i=0; i<pParse->nVarExpr; i++){
395 Expr *pE;
396 if( (pE = pParse->apVarExpr[i])!=0
397 && pE->token.n==n
398 && memcmp(pE->token.z, pToken->z, n)==0 ){
399 pExpr->iTable = pE->iTable;
400 break;
401 }
402 }
403 if( i>=pParse->nVarExpr ){
404 pExpr->iTable = ++pParse->nVar;
405 if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){
406 pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10;
danielk1977e7259292006-01-13 06:33:23 +0000407 sqliteReallocOrFree((void**)&pParse->apVarExpr,
drhfa6bc002004-09-07 16:19:52 +0000408 pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0]) );
409 }
danielk19779e128002006-01-18 16:51:35 +0000410 if( !sqlite3MallocFailed() ){
drhfa6bc002004-09-07 16:19:52 +0000411 assert( pParse->apVarExpr!=0 );
412 pParse->apVarExpr[pParse->nVarExpr++] = pExpr;
413 }
414 }
415 }
416}
417
418/*
drha2e00042002-01-22 03:13:42 +0000419** Recursively delete an expression tree.
420*/
danielk19774adee202004-05-08 08:23:19 +0000421void sqlite3ExprDelete(Expr *p){
drha2e00042002-01-22 03:13:42 +0000422 if( p==0 ) return;
drh4efc4752004-01-16 15:55:37 +0000423 if( p->span.dyn ) sqliteFree((char*)p->span.z);
424 if( p->token.dyn ) sqliteFree((char*)p->token.z);
danielk19774adee202004-05-08 08:23:19 +0000425 sqlite3ExprDelete(p->pLeft);
426 sqlite3ExprDelete(p->pRight);
427 sqlite3ExprListDelete(p->pList);
428 sqlite3SelectDelete(p->pSelect);
drha2e00042002-01-22 03:13:42 +0000429 sqliteFree(p);
430}
431
drhd2687b72005-08-12 22:56:09 +0000432/*
433** The Expr.token field might be a string literal that is quoted.
434** If so, remove the quotation marks.
435*/
436void sqlite3DequoteExpr(Expr *p){
437 if( ExprHasAnyProperty(p, EP_Dequoted) ){
438 return;
439 }
440 ExprSetProperty(p, EP_Dequoted);
441 if( p->token.dyn==0 ){
drhd2687b72005-08-12 22:56:09 +0000442 sqlite3TokenCopy(&p->token, &p->token);
443 }
444 sqlite3Dequote((char*)p->token.z);
445}
446
drha76b5df2002-02-23 02:32:10 +0000447
448/*
drhff78bd22002-02-27 01:47:11 +0000449** The following group of routines make deep copies of expressions,
450** expression lists, ID lists, and select statements. The copies can
451** be deleted (by being passed to their respective ...Delete() routines)
452** without effecting the originals.
453**
danielk19774adee202004-05-08 08:23:19 +0000454** The expression list, ID, and source lists return by sqlite3ExprListDup(),
455** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
drhad3cab52002-05-24 02:04:32 +0000456** by subsequent calls to sqlite*ListAppend() routines.
drhff78bd22002-02-27 01:47:11 +0000457**
drhad3cab52002-05-24 02:04:32 +0000458** Any tables that the SrcList might point to are not duplicated.
drhff78bd22002-02-27 01:47:11 +0000459*/
danielk19774adee202004-05-08 08:23:19 +0000460Expr *sqlite3ExprDup(Expr *p){
drhff78bd22002-02-27 01:47:11 +0000461 Expr *pNew;
462 if( p==0 ) return 0;
drhfcb78a42003-01-18 20:11:05 +0000463 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000464 if( pNew==0 ) return 0;
drh3b167c72002-06-28 12:18:47 +0000465 memcpy(pNew, p, sizeof(*pNew));
drh6977fea2002-10-22 23:38:04 +0000466 if( p->token.z!=0 ){
drh2646da72005-12-09 20:02:05 +0000467 pNew->token.z = (u8*)sqliteStrNDup((char*)p->token.z, p->token.n);
drh4b59ab52002-08-24 18:24:51 +0000468 pNew->token.dyn = 1;
469 }else{
drh4efc4752004-01-16 15:55:37 +0000470 assert( pNew->token.z==0 );
drh4b59ab52002-08-24 18:24:51 +0000471 }
drh6977fea2002-10-22 23:38:04 +0000472 pNew->span.z = 0;
danielk19774adee202004-05-08 08:23:19 +0000473 pNew->pLeft = sqlite3ExprDup(p->pLeft);
474 pNew->pRight = sqlite3ExprDup(p->pRight);
475 pNew->pList = sqlite3ExprListDup(p->pList);
476 pNew->pSelect = sqlite3SelectDup(p->pSelect);
danielk1977aee18ef2005-03-09 12:26:50 +0000477 pNew->pTab = p->pTab;
drhff78bd22002-02-27 01:47:11 +0000478 return pNew;
479}
danielk19774adee202004-05-08 08:23:19 +0000480void sqlite3TokenCopy(Token *pTo, Token *pFrom){
drh4b59ab52002-08-24 18:24:51 +0000481 if( pTo->dyn ) sqliteFree((char*)pTo->z);
drh4b59ab52002-08-24 18:24:51 +0000482 if( pFrom->z ){
483 pTo->n = pFrom->n;
drh2646da72005-12-09 20:02:05 +0000484 pTo->z = (u8*)sqliteStrNDup((char*)pFrom->z, pFrom->n);
drh4b59ab52002-08-24 18:24:51 +0000485 pTo->dyn = 1;
486 }else{
drh4b59ab52002-08-24 18:24:51 +0000487 pTo->z = 0;
drh4b59ab52002-08-24 18:24:51 +0000488 }
489}
danielk19774adee202004-05-08 08:23:19 +0000490ExprList *sqlite3ExprListDup(ExprList *p){
drhff78bd22002-02-27 01:47:11 +0000491 ExprList *pNew;
drh145716b2004-09-24 12:24:06 +0000492 struct ExprList_item *pItem, *pOldItem;
drhff78bd22002-02-27 01:47:11 +0000493 int i;
494 if( p==0 ) return 0;
495 pNew = sqliteMalloc( sizeof(*pNew) );
496 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000497 pNew->nExpr = pNew->nAlloc = p->nExpr;
drh3e7bc9c2004-02-21 19:17:17 +0000498 pNew->a = pItem = sqliteMalloc( p->nExpr*sizeof(p->a[0]) );
danielk1977e0048402004-06-15 16:51:01 +0000499 if( pItem==0 ){
500 sqliteFree(pNew);
501 return 0;
502 }
drh145716b2004-09-24 12:24:06 +0000503 pOldItem = p->a;
504 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
drh4b59ab52002-08-24 18:24:51 +0000505 Expr *pNewExpr, *pOldExpr;
drh145716b2004-09-24 12:24:06 +0000506 pItem->pExpr = pNewExpr = sqlite3ExprDup(pOldExpr = pOldItem->pExpr);
drh6977fea2002-10-22 23:38:04 +0000507 if( pOldExpr->span.z!=0 && pNewExpr ){
508 /* Always make a copy of the span for top-level expressions in the
drh4b59ab52002-08-24 18:24:51 +0000509 ** expression list. The logic in SELECT processing that determines
510 ** the names of columns in the result set needs this information */
danielk19774adee202004-05-08 08:23:19 +0000511 sqlite3TokenCopy(&pNewExpr->span, &pOldExpr->span);
drh4b59ab52002-08-24 18:24:51 +0000512 }
drh1f3e9052002-10-31 00:09:39 +0000513 assert( pNewExpr==0 || pNewExpr->span.z!=0
drh6f7adc82006-01-11 21:41:20 +0000514 || pOldExpr->span.z==0
danielk19779e128002006-01-18 16:51:35 +0000515 || sqlite3MallocFailed() );
drh145716b2004-09-24 12:24:06 +0000516 pItem->zName = sqliteStrDup(pOldItem->zName);
517 pItem->sortOrder = pOldItem->sortOrder;
518 pItem->isAgg = pOldItem->isAgg;
drh3e7bc9c2004-02-21 19:17:17 +0000519 pItem->done = 0;
drhff78bd22002-02-27 01:47:11 +0000520 }
521 return pNew;
522}
danielk197793758c82005-01-21 08:13:14 +0000523
524/*
525** If cursors, triggers, views and subqueries are all omitted from
526** the build, then none of the following routines, except for
527** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes
528** called with a NULL argument.
529*/
danielk19776a67fe82005-02-04 04:07:16 +0000530#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \
531 || !defined(SQLITE_OMIT_SUBQUERY)
danielk19774adee202004-05-08 08:23:19 +0000532SrcList *sqlite3SrcListDup(SrcList *p){
drhad3cab52002-05-24 02:04:32 +0000533 SrcList *pNew;
534 int i;
drh113088e2003-03-20 01:16:58 +0000535 int nByte;
drhad3cab52002-05-24 02:04:32 +0000536 if( p==0 ) return 0;
drh113088e2003-03-20 01:16:58 +0000537 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
drh4efc4752004-01-16 15:55:37 +0000538 pNew = sqliteMallocRaw( nByte );
drhad3cab52002-05-24 02:04:32 +0000539 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000540 pNew->nSrc = pNew->nAlloc = p->nSrc;
drhad3cab52002-05-24 02:04:32 +0000541 for(i=0; i<p->nSrc; i++){
drh4efc4752004-01-16 15:55:37 +0000542 struct SrcList_item *pNewItem = &pNew->a[i];
543 struct SrcList_item *pOldItem = &p->a[i];
drhed8a3bb2005-06-06 21:19:56 +0000544 Table *pTab;
drh4efc4752004-01-16 15:55:37 +0000545 pNewItem->zDatabase = sqliteStrDup(pOldItem->zDatabase);
546 pNewItem->zName = sqliteStrDup(pOldItem->zName);
547 pNewItem->zAlias = sqliteStrDup(pOldItem->zAlias);
548 pNewItem->jointype = pOldItem->jointype;
549 pNewItem->iCursor = pOldItem->iCursor;
danielk19771787cca2006-02-10 07:07:14 +0000550 pNewItem->isPopulated = pOldItem->isPopulated;
drhed8a3bb2005-06-06 21:19:56 +0000551 pTab = pNewItem->pTab = pOldItem->pTab;
552 if( pTab ){
553 pTab->nRef++;
danielk1977a1cb1832005-02-12 08:59:55 +0000554 }
danielk19774adee202004-05-08 08:23:19 +0000555 pNewItem->pSelect = sqlite3SelectDup(pOldItem->pSelect);
556 pNewItem->pOn = sqlite3ExprDup(pOldItem->pOn);
557 pNewItem->pUsing = sqlite3IdListDup(pOldItem->pUsing);
danielk19776c18b6e2005-01-30 09:17:58 +0000558 pNewItem->colUsed = pOldItem->colUsed;
drhad3cab52002-05-24 02:04:32 +0000559 }
560 return pNew;
561}
danielk19774adee202004-05-08 08:23:19 +0000562IdList *sqlite3IdListDup(IdList *p){
drhff78bd22002-02-27 01:47:11 +0000563 IdList *pNew;
564 int i;
565 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000566 pNew = sqliteMallocRaw( sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +0000567 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000568 pNew->nId = pNew->nAlloc = p->nId;
drh4efc4752004-01-16 15:55:37 +0000569 pNew->a = sqliteMallocRaw( p->nId*sizeof(p->a[0]) );
danielk1977d5d56522005-03-16 12:15:20 +0000570 if( pNew->a==0 ){
571 sqliteFree(pNew);
572 return 0;
573 }
drhff78bd22002-02-27 01:47:11 +0000574 for(i=0; i<p->nId; i++){
drh4efc4752004-01-16 15:55:37 +0000575 struct IdList_item *pNewItem = &pNew->a[i];
576 struct IdList_item *pOldItem = &p->a[i];
577 pNewItem->zName = sqliteStrDup(pOldItem->zName);
578 pNewItem->idx = pOldItem->idx;
drhff78bd22002-02-27 01:47:11 +0000579 }
580 return pNew;
581}
danielk19774adee202004-05-08 08:23:19 +0000582Select *sqlite3SelectDup(Select *p){
drhff78bd22002-02-27 01:47:11 +0000583 Select *pNew;
584 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000585 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000586 if( pNew==0 ) return 0;
587 pNew->isDistinct = p->isDistinct;
danielk19774adee202004-05-08 08:23:19 +0000588 pNew->pEList = sqlite3ExprListDup(p->pEList);
589 pNew->pSrc = sqlite3SrcListDup(p->pSrc);
590 pNew->pWhere = sqlite3ExprDup(p->pWhere);
591 pNew->pGroupBy = sqlite3ExprListDup(p->pGroupBy);
592 pNew->pHaving = sqlite3ExprDup(p->pHaving);
593 pNew->pOrderBy = sqlite3ExprListDup(p->pOrderBy);
drhff78bd22002-02-27 01:47:11 +0000594 pNew->op = p->op;
danielk19774adee202004-05-08 08:23:19 +0000595 pNew->pPrior = sqlite3SelectDup(p->pPrior);
danielk1977a2dc3b12005-02-05 12:48:48 +0000596 pNew->pLimit = sqlite3ExprDup(p->pLimit);
597 pNew->pOffset = sqlite3ExprDup(p->pOffset);
drh7b58dae2003-07-20 01:16:46 +0000598 pNew->iLimit = -1;
599 pNew->iOffset = -1;
danielk1977a1cb1832005-02-12 08:59:55 +0000600 pNew->isResolved = p->isResolved;
601 pNew->isAgg = p->isAgg;
drhb9bb7c12006-06-11 23:41:55 +0000602 pNew->usesEphm = 0;
drh8e647b82005-09-23 21:11:53 +0000603 pNew->disallowOrderBy = 0;
drh0342b1f2005-09-01 03:07:44 +0000604 pNew->pRightmost = 0;
drhb9bb7c12006-06-11 23:41:55 +0000605 pNew->addrOpenEphm[0] = -1;
606 pNew->addrOpenEphm[1] = -1;
607 pNew->addrOpenEphm[2] = -1;
drhff78bd22002-02-27 01:47:11 +0000608 return pNew;
609}
danielk197793758c82005-01-21 08:13:14 +0000610#else
611Select *sqlite3SelectDup(Select *p){
612 assert( p==0 );
613 return 0;
614}
615#endif
drhff78bd22002-02-27 01:47:11 +0000616
617
618/*
drha76b5df2002-02-23 02:32:10 +0000619** Add a new element to the end of an expression list. If pList is
620** initially NULL, then create a new expression list.
621*/
danielk19774adee202004-05-08 08:23:19 +0000622ExprList *sqlite3ExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){
drha76b5df2002-02-23 02:32:10 +0000623 if( pList==0 ){
624 pList = sqliteMalloc( sizeof(ExprList) );
625 if( pList==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000626 goto no_mem;
drha76b5df2002-02-23 02:32:10 +0000627 }
drh4efc4752004-01-16 15:55:37 +0000628 assert( pList->nAlloc==0 );
drha76b5df2002-02-23 02:32:10 +0000629 }
drh4305d102003-07-30 12:34:12 +0000630 if( pList->nAlloc<=pList->nExpr ){
danielk1977d5d56522005-03-16 12:15:20 +0000631 struct ExprList_item *a;
632 int n = pList->nAlloc*2 + 4;
633 a = sqliteRealloc(pList->a, n*sizeof(pList->a[0]));
634 if( a==0 ){
635 goto no_mem;
drha76b5df2002-02-23 02:32:10 +0000636 }
danielk1977d5d56522005-03-16 12:15:20 +0000637 pList->a = a;
638 pList->nAlloc = n;
drha76b5df2002-02-23 02:32:10 +0000639 }
drh4efc4752004-01-16 15:55:37 +0000640 assert( pList->a!=0 );
641 if( pExpr || pName ){
642 struct ExprList_item *pItem = &pList->a[pList->nExpr++];
643 memset(pItem, 0, sizeof(*pItem));
drha99db3b2004-06-19 14:49:12 +0000644 pItem->zName = sqlite3NameFromToken(pName);
danielk1977e94ddc92005-03-21 03:53:38 +0000645 pItem->pExpr = pExpr;
drha76b5df2002-02-23 02:32:10 +0000646 }
647 return pList;
danielk1977d5d56522005-03-16 12:15:20 +0000648
649no_mem:
650 /* Avoid leaking memory if malloc has failed. */
651 sqlite3ExprDelete(pExpr);
652 sqlite3ExprListDelete(pList);
653 return 0;
drha76b5df2002-02-23 02:32:10 +0000654}
655
656/*
657** Delete an entire expression list.
658*/
danielk19774adee202004-05-08 08:23:19 +0000659void sqlite3ExprListDelete(ExprList *pList){
drha76b5df2002-02-23 02:32:10 +0000660 int i;
drhbe5c89a2004-07-26 00:31:09 +0000661 struct ExprList_item *pItem;
drha76b5df2002-02-23 02:32:10 +0000662 if( pList==0 ) return;
drh1bdd9b52004-04-23 17:04:44 +0000663 assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
664 assert( pList->nExpr<=pList->nAlloc );
drhbe5c89a2004-07-26 00:31:09 +0000665 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
666 sqlite3ExprDelete(pItem->pExpr);
667 sqliteFree(pItem->zName);
drha76b5df2002-02-23 02:32:10 +0000668 }
669 sqliteFree(pList->a);
670 sqliteFree(pList);
671}
672
673/*
drh626a8792005-01-17 22:08:19 +0000674** Walk an expression tree. Call xFunc for each node visited.
drh73b211a2005-01-18 04:00:42 +0000675**
drh626a8792005-01-17 22:08:19 +0000676** The return value from xFunc determines whether the tree walk continues.
677** 0 means continue walking the tree. 1 means do not walk children
678** of the current node but continue with siblings. 2 means abandon
679** the tree walk completely.
680**
681** The return value from this routine is 1 to abandon the tree walk
682** and 0 to continue.
drh87abf5c2005-08-25 12:45:04 +0000683**
684** NOTICE: This routine does *not* descend into subqueries.
drh626a8792005-01-17 22:08:19 +0000685*/
danielk1977a58fdfb2005-02-08 07:50:40 +0000686static int walkExprList(ExprList *, int (*)(void *, Expr*), void *);
drh626a8792005-01-17 22:08:19 +0000687static int walkExprTree(Expr *pExpr, int (*xFunc)(void*,Expr*), void *pArg){
drh626a8792005-01-17 22:08:19 +0000688 int rc;
689 if( pExpr==0 ) return 0;
690 rc = (*xFunc)(pArg, pExpr);
691 if( rc==0 ){
692 if( walkExprTree(pExpr->pLeft, xFunc, pArg) ) return 1;
693 if( walkExprTree(pExpr->pRight, xFunc, pArg) ) return 1;
danielk1977a58fdfb2005-02-08 07:50:40 +0000694 if( walkExprList(pExpr->pList, xFunc, pArg) ) return 1;
drh626a8792005-01-17 22:08:19 +0000695 }
696 return rc>1;
697}
698
699/*
danielk1977a58fdfb2005-02-08 07:50:40 +0000700** Call walkExprTree() for every expression in list p.
701*/
702static int walkExprList(ExprList *p, int (*xFunc)(void *, Expr*), void *pArg){
703 int i;
704 struct ExprList_item *pItem;
705 if( !p ) return 0;
706 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
707 if( walkExprTree(pItem->pExpr, xFunc, pArg) ) return 1;
708 }
709 return 0;
710}
711
712/*
713** Call walkExprTree() for every expression in Select p, not including
714** expressions that are part of sub-selects in any FROM clause or the LIMIT
715** or OFFSET expressions..
716*/
717static int walkSelectExpr(Select *p, int (*xFunc)(void *, Expr*), void *pArg){
718 walkExprList(p->pEList, xFunc, pArg);
719 walkExprTree(p->pWhere, xFunc, pArg);
720 walkExprList(p->pGroupBy, xFunc, pArg);
721 walkExprTree(p->pHaving, xFunc, pArg);
722 walkExprList(p->pOrderBy, xFunc, pArg);
723 return 0;
724}
725
726
727/*
drh626a8792005-01-17 22:08:19 +0000728** This routine is designed as an xFunc for walkExprTree().
729**
730** pArg is really a pointer to an integer. If we can tell by looking
drh73b211a2005-01-18 04:00:42 +0000731** at pExpr that the expression that contains pExpr is not a constant
732** expression, then set *pArg to 0 and return 2 to abandon the tree walk.
733** If pExpr does does not disqualify the expression from being a constant
734** then do nothing.
735**
736** After walking the whole tree, if no nodes are found that disqualify
737** the expression as constant, then we assume the whole expression
738** is constant. See sqlite3ExprIsConstant() for additional information.
drh626a8792005-01-17 22:08:19 +0000739*/
740static int exprNodeIsConstant(void *pArg, Expr *pExpr){
741 switch( pExpr->op ){
drheb55bd22005-06-30 17:04:21 +0000742 /* Consider functions to be constant if all their arguments are constant
743 ** and *pArg==2 */
744 case TK_FUNCTION:
745 if( *((int*)pArg)==2 ) return 0;
746 /* Fall through */
drh626a8792005-01-17 22:08:19 +0000747 case TK_ID:
748 case TK_COLUMN:
749 case TK_DOT:
750 case TK_AGG_FUNCTION:
drh13449892005-09-07 21:22:45 +0000751 case TK_AGG_COLUMN:
drhfe2093d2005-01-20 22:48:47 +0000752#ifndef SQLITE_OMIT_SUBQUERY
753 case TK_SELECT:
754 case TK_EXISTS:
755#endif
drh626a8792005-01-17 22:08:19 +0000756 *((int*)pArg) = 0;
757 return 2;
drh87abf5c2005-08-25 12:45:04 +0000758 case TK_IN:
759 if( pExpr->pSelect ){
760 *((int*)pArg) = 0;
761 return 2;
762 }
drh626a8792005-01-17 22:08:19 +0000763 default:
764 return 0;
765 }
766}
767
768/*
drhfef52082000-06-06 01:50:43 +0000769** Walk an expression tree. Return 1 if the expression is constant
drheb55bd22005-06-30 17:04:21 +0000770** and 0 if it involves variables or function calls.
drh23989372002-05-21 13:43:04 +0000771**
772** For the purposes of this function, a double-quoted string (ex: "abc")
773** is considered a variable but a single-quoted string (ex: 'abc') is
774** a constant.
drhfef52082000-06-06 01:50:43 +0000775*/
danielk19774adee202004-05-08 08:23:19 +0000776int sqlite3ExprIsConstant(Expr *p){
drh626a8792005-01-17 22:08:19 +0000777 int isConst = 1;
778 walkExprTree(p, exprNodeIsConstant, &isConst);
779 return isConst;
drhfef52082000-06-06 01:50:43 +0000780}
781
782/*
drheb55bd22005-06-30 17:04:21 +0000783** Walk an expression tree. Return 1 if the expression is constant
784** or a function call with constant arguments. Return and 0 if there
785** are any variables.
786**
787** For the purposes of this function, a double-quoted string (ex: "abc")
788** is considered a variable but a single-quoted string (ex: 'abc') is
789** a constant.
790*/
791int sqlite3ExprIsConstantOrFunction(Expr *p){
792 int isConst = 2;
793 walkExprTree(p, exprNodeIsConstant, &isConst);
794 return isConst!=0;
795}
796
797/*
drh73b211a2005-01-18 04:00:42 +0000798** If the expression p codes a constant integer that is small enough
drh202b2df2004-01-06 01:13:46 +0000799** to fit in a 32-bit integer, return 1 and put the value of the integer
800** in *pValue. If the expression is not an integer or if it is too big
801** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
drhe4de1fe2002-06-02 16:09:01 +0000802*/
danielk19774adee202004-05-08 08:23:19 +0000803int sqlite3ExprIsInteger(Expr *p, int *pValue){
drhe4de1fe2002-06-02 16:09:01 +0000804 switch( p->op ){
805 case TK_INTEGER: {
drh2646da72005-12-09 20:02:05 +0000806 if( sqlite3GetInt32((char*)p->token.z, pValue) ){
drh202b2df2004-01-06 01:13:46 +0000807 return 1;
808 }
809 break;
drhe4de1fe2002-06-02 16:09:01 +0000810 }
drh4b59ab52002-08-24 18:24:51 +0000811 case TK_UPLUS: {
danielk19774adee202004-05-08 08:23:19 +0000812 return sqlite3ExprIsInteger(p->pLeft, pValue);
drh4b59ab52002-08-24 18:24:51 +0000813 }
drhe4de1fe2002-06-02 16:09:01 +0000814 case TK_UMINUS: {
815 int v;
danielk19774adee202004-05-08 08:23:19 +0000816 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
drhe4de1fe2002-06-02 16:09:01 +0000817 *pValue = -v;
818 return 1;
819 }
820 break;
821 }
822 default: break;
823 }
824 return 0;
825}
826
827/*
drhc4a3c772001-04-04 11:48:57 +0000828** Return TRUE if the given string is a row-id column name.
829*/
danielk19774adee202004-05-08 08:23:19 +0000830int sqlite3IsRowid(const char *z){
831 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
832 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
833 if( sqlite3StrICmp(z, "OID")==0 ) return 1;
drhc4a3c772001-04-04 11:48:57 +0000834 return 0;
835}
836
837/*
drh8141f612004-01-25 22:44:58 +0000838** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
839** that name in the set of source tables in pSrcList and make the pExpr
840** expression node refer back to that source column. The following changes
841** are made to pExpr:
842**
843** pExpr->iDb Set the index in db->aDb[] of the database holding
844** the table.
845** pExpr->iTable Set to the cursor number for the table obtained
846** from pSrcList.
847** pExpr->iColumn Set to the column number within the table.
drh8141f612004-01-25 22:44:58 +0000848** pExpr->op Set to TK_COLUMN.
849** pExpr->pLeft Any expression this points to is deleted
850** pExpr->pRight Any expression this points to is deleted.
851**
852** The pDbToken is the name of the database (the "X"). This value may be
853** NULL meaning that name is of the form Y.Z or Z. Any available database
854** can be used. The pTableToken is the name of the table (the "Y"). This
855** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it
856** means that the form of the name is Z and that columns from any table
857** can be used.
858**
859** If the name cannot be resolved unambiguously, leave an error message
860** in pParse and return non-zero. Return zero on success.
861*/
862static int lookupName(
drhffe07b22005-11-03 00:41:17 +0000863 Parse *pParse, /* The parsing context */
drh8141f612004-01-25 22:44:58 +0000864 Token *pDbToken, /* Name of the database containing table, or NULL */
865 Token *pTableToken, /* Name of table containing column, or NULL */
866 Token *pColumnToken, /* Name of the column. */
drh626a8792005-01-17 22:08:19 +0000867 NameContext *pNC, /* The name context used to resolve the name */
drh8141f612004-01-25 22:44:58 +0000868 Expr *pExpr /* Make this EXPR node point to the selected column */
869){
870 char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */
871 char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */
872 char *zCol = 0; /* Name of the column. The "Z" */
873 int i, j; /* Loop counters */
874 int cnt = 0; /* Number of matching column names */
875 int cntTab = 0; /* Number of matching table names */
drh626a8792005-01-17 22:08:19 +0000876 sqlite3 *db = pParse->db; /* The database */
drh51669862004-12-18 18:40:26 +0000877 struct SrcList_item *pItem; /* Use for looping over pSrcList items */
878 struct SrcList_item *pMatch = 0; /* The matching pSrcList item */
drh73b211a2005-01-18 04:00:42 +0000879 NameContext *pTopNC = pNC; /* First namecontext in the list */
drh8141f612004-01-25 22:44:58 +0000880
881 assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
drha99db3b2004-06-19 14:49:12 +0000882 zDb = sqlite3NameFromToken(pDbToken);
883 zTab = sqlite3NameFromToken(pTableToken);
884 zCol = sqlite3NameFromToken(pColumnToken);
danielk19779e128002006-01-18 16:51:35 +0000885 if( sqlite3MallocFailed() ){
danielk1977d5d56522005-03-16 12:15:20 +0000886 goto lookupname_end;
drh8141f612004-01-25 22:44:58 +0000887 }
drh8141f612004-01-25 22:44:58 +0000888
889 pExpr->iTable = -1;
drh626a8792005-01-17 22:08:19 +0000890 while( pNC && cnt==0 ){
drhffe07b22005-11-03 00:41:17 +0000891 ExprList *pEList;
drh626a8792005-01-17 22:08:19 +0000892 SrcList *pSrcList = pNC->pSrcList;
drh8141f612004-01-25 22:44:58 +0000893
danielk1977b3bce662005-01-29 08:32:43 +0000894 if( pSrcList ){
895 for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
drh43617e92006-03-06 20:55:46 +0000896 Table *pTab;
897 int iDb;
danielk1977b3bce662005-01-29 08:32:43 +0000898 Column *pCol;
899
drh43617e92006-03-06 20:55:46 +0000900 pTab = pItem->pTab;
901 assert( pTab!=0 );
902 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
danielk1977b3bce662005-01-29 08:32:43 +0000903 assert( pTab->nCol>0 );
904 if( zTab ){
905 if( pItem->zAlias ){
906 char *zTabName = pItem->zAlias;
907 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
908 }else{
909 char *zTabName = pTab->zName;
910 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
danielk1977da184232006-01-05 11:34:32 +0000911 if( zDb!=0 && sqlite3StrICmp(db->aDb[iDb].zName, zDb)!=0 ){
danielk1977b3bce662005-01-29 08:32:43 +0000912 continue;
913 }
drh626a8792005-01-17 22:08:19 +0000914 }
drh8141f612004-01-25 22:44:58 +0000915 }
danielk1977b3bce662005-01-29 08:32:43 +0000916 if( 0==(cntTab++) ){
drh626a8792005-01-17 22:08:19 +0000917 pExpr->iTable = pItem->iCursor;
danielk1977da184232006-01-05 11:34:32 +0000918 pExpr->pSchema = pTab->pSchema;
danielk1977b3bce662005-01-29 08:32:43 +0000919 pMatch = pItem;
920 }
921 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
922 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
danielk1977b3bf5562006-01-10 17:58:23 +0000923 const char *zColl = pTab->aCol[j].zColl;
drh873fac02005-06-06 17:11:46 +0000924 IdList *pUsing;
danielk1977b3bce662005-01-29 08:32:43 +0000925 cnt++;
926 pExpr->iTable = pItem->iCursor;
927 pMatch = pItem;
danielk1977da184232006-01-05 11:34:32 +0000928 pExpr->pSchema = pTab->pSchema;
danielk1977b3bce662005-01-29 08:32:43 +0000929 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
930 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
931 pExpr->affinity = pTab->aCol[j].affinity;
drh8b4c40d2007-02-01 23:02:45 +0000932 if( (pExpr->flags & EP_ExpCollate)==0 ){
933 pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0);
934 }
drh61dfc312006-12-16 16:25:15 +0000935 if( i<pSrcList->nSrc-1 ){
936 if( pItem[1].jointype & JT_NATURAL ){
937 /* If this match occurred in the left table of a natural join,
938 ** then skip the right table to avoid a duplicate match */
939 pItem++;
940 i++;
941 }else if( (pUsing = pItem[1].pUsing)!=0 ){
942 /* If this match occurs on a column that is in the USING clause
943 ** of a join, skip the search of the right table of the join
944 ** to avoid a duplicate match there. */
945 int k;
946 for(k=0; k<pUsing->nId; k++){
947 if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ){
948 pItem++;
949 i++;
950 break;
951 }
drh873fac02005-06-06 17:11:46 +0000952 }
953 }
954 }
danielk1977b3bce662005-01-29 08:32:43 +0000955 break;
956 }
drh8141f612004-01-25 22:44:58 +0000957 }
958 }
959 }
drh626a8792005-01-17 22:08:19 +0000960
961#ifndef SQLITE_OMIT_TRIGGER
962 /* If we have not already resolved the name, then maybe
963 ** it is a new.* or old.* trigger argument reference
964 */
965 if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
966 TriggerStack *pTriggerStack = pParse->trigStack;
967 Table *pTab = 0;
968 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){
969 pExpr->iTable = pTriggerStack->newIdx;
970 assert( pTriggerStack->pTab );
971 pTab = pTriggerStack->pTab;
972 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab)==0 ){
973 pExpr->iTable = pTriggerStack->oldIdx;
974 assert( pTriggerStack->pTab );
975 pTab = pTriggerStack->pTab;
976 }
977
978 if( pTab ){
danielk1977f0113002006-01-24 12:09:17 +0000979 int iCol;
drh626a8792005-01-17 22:08:19 +0000980 Column *pCol = pTab->aCol;
981
danielk1977da184232006-01-05 11:34:32 +0000982 pExpr->pSchema = pTab->pSchema;
drh626a8792005-01-17 22:08:19 +0000983 cntTab++;
danielk1977f0113002006-01-24 12:09:17 +0000984 for(iCol=0; iCol < pTab->nCol; iCol++, pCol++) {
drh626a8792005-01-17 22:08:19 +0000985 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
danielk1977f0113002006-01-24 12:09:17 +0000986 const char *zColl = pTab->aCol[iCol].zColl;
drh626a8792005-01-17 22:08:19 +0000987 cnt++;
danielk1977f0113002006-01-24 12:09:17 +0000988 pExpr->iColumn = iCol==pTab->iPKey ? -1 : iCol;
989 pExpr->affinity = pTab->aCol[iCol].affinity;
drh8b4c40d2007-02-01 23:02:45 +0000990 if( (pExpr->flags & EP_ExpCollate)==0 ){
991 pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0);
992 }
danielk1977aee18ef2005-03-09 12:26:50 +0000993 pExpr->pTab = pTab;
drh626a8792005-01-17 22:08:19 +0000994 break;
995 }
996 }
997 }
998 }
drhb7f91642004-10-31 02:22:47 +0000999#endif /* !defined(SQLITE_OMIT_TRIGGER) */
drh8141f612004-01-25 22:44:58 +00001000
drh626a8792005-01-17 22:08:19 +00001001 /*
1002 ** Perhaps the name is a reference to the ROWID
1003 */
1004 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
1005 cnt = 1;
1006 pExpr->iColumn = -1;
drh8a512562005-11-14 22:29:05 +00001007 pExpr->affinity = SQLITE_AFF_INTEGER;
drh626a8792005-01-17 22:08:19 +00001008 }
drh8141f612004-01-25 22:44:58 +00001009
drh626a8792005-01-17 22:08:19 +00001010 /*
1011 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
1012 ** might refer to an result-set alias. This happens, for example, when
1013 ** we are resolving names in the WHERE clause of the following command:
1014 **
1015 ** SELECT a+b AS x FROM table WHERE x<10;
1016 **
1017 ** In cases like this, replace pExpr with a copy of the expression that
1018 ** forms the result set entry ("a+b" in the example) and return immediately.
1019 ** Note that the expression in the result set should have already been
1020 ** resolved by the time the WHERE clause is resolved.
1021 */
drhffe07b22005-11-03 00:41:17 +00001022 if( cnt==0 && (pEList = pNC->pEList)!=0 && zTab==0 ){
drh626a8792005-01-17 22:08:19 +00001023 for(j=0; j<pEList->nExpr; j++){
1024 char *zAs = pEList->a[j].zName;
1025 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
1026 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
1027 pExpr->op = TK_AS;
1028 pExpr->iColumn = j;
1029 pExpr->pLeft = sqlite3ExprDup(pEList->a[j].pExpr);
drh15ccce12005-05-23 15:06:39 +00001030 cnt = 1;
drh626a8792005-01-17 22:08:19 +00001031 assert( zTab==0 && zDb==0 );
drh15ccce12005-05-23 15:06:39 +00001032 goto lookupname_end_2;
drh626a8792005-01-17 22:08:19 +00001033 }
1034 }
1035 }
1036
1037 /* Advance to the next name context. The loop will exit when either
1038 ** we have a match (cnt>0) or when we run out of name contexts.
1039 */
1040 if( cnt==0 ){
1041 pNC = pNC->pNext;
1042 }
drh8141f612004-01-25 22:44:58 +00001043 }
1044
1045 /*
1046 ** If X and Y are NULL (in other words if only the column name Z is
1047 ** supplied) and the value of Z is enclosed in double-quotes, then
1048 ** Z is a string literal if it doesn't match any column names. In that
1049 ** case, we need to return right away and not make any changes to
1050 ** pExpr.
drh15ccce12005-05-23 15:06:39 +00001051 **
1052 ** Because no reference was made to outer contexts, the pNC->nRef
1053 ** fields are not changed in any context.
drh8141f612004-01-25 22:44:58 +00001054 */
1055 if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
1056 sqliteFree(zCol);
1057 return 0;
1058 }
1059
1060 /*
1061 ** cnt==0 means there was not match. cnt>1 means there were two or
1062 ** more matches. Either way, we have an error.
1063 */
1064 if( cnt!=1 ){
1065 char *z = 0;
1066 char *zErr;
1067 zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s";
1068 if( zDb ){
drhf93339d2006-01-03 15:16:26 +00001069 sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, (char*)0);
drh8141f612004-01-25 22:44:58 +00001070 }else if( zTab ){
drhf93339d2006-01-03 15:16:26 +00001071 sqlite3SetString(&z, zTab, ".", zCol, (char*)0);
drh8141f612004-01-25 22:44:58 +00001072 }else{
1073 z = sqliteStrDup(zCol);
1074 }
danielk19774adee202004-05-08 08:23:19 +00001075 sqlite3ErrorMsg(pParse, zErr, z);
drh8141f612004-01-25 22:44:58 +00001076 sqliteFree(z);
drh73b211a2005-01-18 04:00:42 +00001077 pTopNC->nErr++;
drh8141f612004-01-25 22:44:58 +00001078 }
1079
drh51669862004-12-18 18:40:26 +00001080 /* If a column from a table in pSrcList is referenced, then record
1081 ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes
1082 ** bit 0 to be set. Column 1 sets bit 1. And so forth. If the
1083 ** column number is greater than the number of bits in the bitmask
1084 ** then set the high-order bit of the bitmask.
1085 */
1086 if( pExpr->iColumn>=0 && pMatch!=0 ){
1087 int n = pExpr->iColumn;
1088 if( n>=sizeof(Bitmask)*8 ){
1089 n = sizeof(Bitmask)*8-1;
1090 }
1091 assert( pMatch->iCursor==pExpr->iTable );
drhca83ac52007-02-01 01:40:44 +00001092 pMatch->colUsed |= ((Bitmask)1)<<n;
drh51669862004-12-18 18:40:26 +00001093 }
1094
danielk1977d5d56522005-03-16 12:15:20 +00001095lookupname_end:
drh8141f612004-01-25 22:44:58 +00001096 /* Clean up and return
1097 */
1098 sqliteFree(zDb);
1099 sqliteFree(zTab);
danielk19774adee202004-05-08 08:23:19 +00001100 sqlite3ExprDelete(pExpr->pLeft);
drh8141f612004-01-25 22:44:58 +00001101 pExpr->pLeft = 0;
danielk19774adee202004-05-08 08:23:19 +00001102 sqlite3ExprDelete(pExpr->pRight);
drh8141f612004-01-25 22:44:58 +00001103 pExpr->pRight = 0;
1104 pExpr->op = TK_COLUMN;
drh15ccce12005-05-23 15:06:39 +00001105lookupname_end_2:
1106 sqliteFree(zCol);
drh626a8792005-01-17 22:08:19 +00001107 if( cnt==1 ){
danielk1977b3bce662005-01-29 08:32:43 +00001108 assert( pNC!=0 );
drh626a8792005-01-17 22:08:19 +00001109 sqlite3AuthRead(pParse, pExpr, pNC->pSrcList);
danielk1977aee18ef2005-03-09 12:26:50 +00001110 if( pMatch && !pMatch->pSelect ){
1111 pExpr->pTab = pMatch->pTab;
1112 }
drh15ccce12005-05-23 15:06:39 +00001113 /* Increment the nRef value on all name contexts from TopNC up to
1114 ** the point where the name matched. */
1115 for(;;){
1116 assert( pTopNC!=0 );
1117 pTopNC->nRef++;
1118 if( pTopNC==pNC ) break;
1119 pTopNC = pTopNC->pNext;
1120 }
1121 return 0;
1122 } else {
1123 return 1;
drh626a8792005-01-17 22:08:19 +00001124 }
drh8141f612004-01-25 22:44:58 +00001125}
1126
1127/*
drh626a8792005-01-17 22:08:19 +00001128** This routine is designed as an xFunc for walkExprTree().
1129**
drh73b211a2005-01-18 04:00:42 +00001130** Resolve symbolic names into TK_COLUMN operators for the current
drh626a8792005-01-17 22:08:19 +00001131** node in the expression tree. Return 0 to continue the search down
drh73b211a2005-01-18 04:00:42 +00001132** the tree or 2 to abort the tree walk.
1133**
1134** This routine also does error checking and name resolution for
1135** function names. The operator for aggregate functions is changed
1136** to TK_AGG_FUNCTION.
drh626a8792005-01-17 22:08:19 +00001137*/
1138static int nameResolverStep(void *pArg, Expr *pExpr){
1139 NameContext *pNC = (NameContext*)pArg;
drh626a8792005-01-17 22:08:19 +00001140 Parse *pParse;
drh626a8792005-01-17 22:08:19 +00001141
danielk1977b3bce662005-01-29 08:32:43 +00001142 if( pExpr==0 ) return 1;
drh626a8792005-01-17 22:08:19 +00001143 assert( pNC!=0 );
drh626a8792005-01-17 22:08:19 +00001144 pParse = pNC->pParse;
danielk1977b3bce662005-01-29 08:32:43 +00001145
drh626a8792005-01-17 22:08:19 +00001146 if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return 1;
1147 ExprSetProperty(pExpr, EP_Resolved);
1148#ifndef NDEBUG
danielk1977f0113002006-01-24 12:09:17 +00001149 if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
1150 SrcList *pSrcList = pNC->pSrcList;
danielk1977940fac92005-01-23 22:41:37 +00001151 int i;
danielk1977f0113002006-01-24 12:09:17 +00001152 for(i=0; i<pNC->pSrcList->nSrc; i++){
drh626a8792005-01-17 22:08:19 +00001153 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
1154 }
1155 }
1156#endif
1157 switch( pExpr->op ){
1158 /* Double-quoted strings (ex: "abc") are used as identifiers if
1159 ** possible. Otherwise they remain as strings. Single-quoted
1160 ** strings (ex: 'abc') are always string literals.
1161 */
1162 case TK_STRING: {
1163 if( pExpr->token.z[0]=='\'' ) break;
1164 /* Fall thru into the TK_ID case if this is a double-quoted string */
1165 }
1166 /* A lone identifier is the name of a column.
1167 */
1168 case TK_ID: {
drh626a8792005-01-17 22:08:19 +00001169 lookupName(pParse, 0, 0, &pExpr->token, pNC, pExpr);
1170 return 1;
1171 }
1172
1173 /* A table name and column name: ID.ID
1174 ** Or a database, table and column: ID.ID.ID
1175 */
1176 case TK_DOT: {
1177 Token *pColumn;
1178 Token *pTable;
1179 Token *pDb;
1180 Expr *pRight;
1181
danielk1977b3bce662005-01-29 08:32:43 +00001182 /* if( pSrcList==0 ) break; */
drh626a8792005-01-17 22:08:19 +00001183 pRight = pExpr->pRight;
1184 if( pRight->op==TK_ID ){
1185 pDb = 0;
1186 pTable = &pExpr->pLeft->token;
1187 pColumn = &pRight->token;
1188 }else{
1189 assert( pRight->op==TK_DOT );
1190 pDb = &pExpr->pLeft->token;
1191 pTable = &pRight->pLeft->token;
1192 pColumn = &pRight->pRight->token;
1193 }
1194 lookupName(pParse, pDb, pTable, pColumn, pNC, pExpr);
1195 return 1;
1196 }
1197
1198 /* Resolve function names
1199 */
drhb71090f2005-05-23 17:26:51 +00001200 case TK_CONST_FUNC:
drh626a8792005-01-17 22:08:19 +00001201 case TK_FUNCTION: {
1202 ExprList *pList = pExpr->pList; /* The argument list */
1203 int n = pList ? pList->nExpr : 0; /* Number of arguments */
1204 int no_such_func = 0; /* True if no such function exists */
1205 int wrong_num_args = 0; /* True if wrong number of arguments */
1206 int is_agg = 0; /* True if is an aggregate function */
1207 int i;
drh5169bbc2006-08-24 14:59:45 +00001208 int auth; /* Authorization to use the function */
drh626a8792005-01-17 22:08:19 +00001209 int nId; /* Number of characters in function name */
1210 const char *zId; /* The function name. */
drh73b211a2005-01-18 04:00:42 +00001211 FuncDef *pDef; /* Information about the function */
danielk197714db2662006-01-09 16:12:04 +00001212 int enc = ENC(pParse->db); /* The database encoding */
drh626a8792005-01-17 22:08:19 +00001213
drh2646da72005-12-09 20:02:05 +00001214 zId = (char*)pExpr->token.z;
drhb71090f2005-05-23 17:26:51 +00001215 nId = pExpr->token.n;
drh626a8792005-01-17 22:08:19 +00001216 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
1217 if( pDef==0 ){
1218 pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
1219 if( pDef==0 ){
1220 no_such_func = 1;
1221 }else{
1222 wrong_num_args = 1;
1223 }
1224 }else{
1225 is_agg = pDef->xFunc==0;
1226 }
drh2fca7fe2006-11-23 11:59:13 +00001227#ifndef SQLITE_OMIT_AUTHORIZATION
drh5169bbc2006-08-24 14:59:45 +00001228 if( pDef ){
1229 auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0);
1230 if( auth!=SQLITE_OK ){
1231 if( auth==SQLITE_DENY ){
1232 sqlite3ErrorMsg(pParse, "not authorized to use function: %s",
1233 pDef->zName);
1234 pNC->nErr++;
1235 }
1236 pExpr->op = TK_NULL;
1237 return 1;
1238 }
1239 }
drhb8b14212006-08-24 15:18:25 +00001240#endif
drh626a8792005-01-17 22:08:19 +00001241 if( is_agg && !pNC->allowAgg ){
1242 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
1243 pNC->nErr++;
1244 is_agg = 0;
1245 }else if( no_such_func ){
1246 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
1247 pNC->nErr++;
1248 }else if( wrong_num_args ){
1249 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
1250 nId, zId);
1251 pNC->nErr++;
1252 }
1253 if( is_agg ){
1254 pExpr->op = TK_AGG_FUNCTION;
1255 pNC->hasAgg = 1;
1256 }
drh73b211a2005-01-18 04:00:42 +00001257 if( is_agg ) pNC->allowAgg = 0;
drh626a8792005-01-17 22:08:19 +00001258 for(i=0; pNC->nErr==0 && i<n; i++){
drh73b211a2005-01-18 04:00:42 +00001259 walkExprTree(pList->a[i].pExpr, nameResolverStep, pNC);
drh626a8792005-01-17 22:08:19 +00001260 }
drh73b211a2005-01-18 04:00:42 +00001261 if( is_agg ) pNC->allowAgg = 1;
drh626a8792005-01-17 22:08:19 +00001262 /* FIX ME: Compute pExpr->affinity based on the expected return
1263 ** type of the function
1264 */
1265 return is_agg;
1266 }
danielk1977b3bce662005-01-29 08:32:43 +00001267#ifndef SQLITE_OMIT_SUBQUERY
1268 case TK_SELECT:
1269 case TK_EXISTS:
1270#endif
1271 case TK_IN: {
1272 if( pExpr->pSelect ){
drh8a9f38f2005-11-05 15:07:55 +00001273 int nRef = pNC->nRef;
drh06f65412005-11-03 02:03:13 +00001274#ifndef SQLITE_OMIT_CHECK
1275 if( pNC->isCheck ){
1276 sqlite3ErrorMsg(pParse,"subqueries prohibited in CHECK constraints");
1277 }
1278#endif
danielk1977b3bce662005-01-29 08:32:43 +00001279 sqlite3SelectResolve(pParse, pExpr->pSelect, pNC);
1280 assert( pNC->nRef>=nRef );
1281 if( nRef!=pNC->nRef ){
1282 ExprSetProperty(pExpr, EP_VarSelect);
1283 }
1284 }
drh4284fb02005-11-03 12:33:28 +00001285 break;
danielk1977b3bce662005-01-29 08:32:43 +00001286 }
drh4284fb02005-11-03 12:33:28 +00001287#ifndef SQLITE_OMIT_CHECK
1288 case TK_VARIABLE: {
1289 if( pNC->isCheck ){
1290 sqlite3ErrorMsg(pParse,"parameters prohibited in CHECK constraints");
1291 }
1292 break;
1293 }
1294#endif
drh626a8792005-01-17 22:08:19 +00001295 }
1296 return 0;
1297}
1298
1299/*
drhcce7d172000-05-31 15:34:51 +00001300** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +00001301** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +00001302** index to the table in the table list and a column offset. The
1303** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
1304** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +00001305** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +00001306** VDBE cursor number for a cursor that is pointing into the referenced
1307** table. The Expr.iColumn value is changed to the index of the column
1308** of the referenced table. The Expr.iColumn value for the special
1309** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
1310** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +00001311**
drh626a8792005-01-17 22:08:19 +00001312** Also resolve function names and check the functions for proper
1313** usage. Make sure all function names are recognized and all functions
1314** have the correct number of arguments. Leave an error message
1315** in pParse->zErrMsg if anything is amiss. Return the number of errors.
1316**
drh73b211a2005-01-18 04:00:42 +00001317** If the expression contains aggregate functions then set the EP_Agg
1318** property on the expression.
drh626a8792005-01-17 22:08:19 +00001319*/
1320int sqlite3ExprResolveNames(
danielk1977b3bce662005-01-29 08:32:43 +00001321 NameContext *pNC, /* Namespace to resolve expressions in. */
1322 Expr *pExpr /* The expression to be analyzed. */
drh626a8792005-01-17 22:08:19 +00001323){
drh13449892005-09-07 21:22:45 +00001324 int savedHasAgg;
drh73b211a2005-01-18 04:00:42 +00001325 if( pExpr==0 ) return 0;
drh13449892005-09-07 21:22:45 +00001326 savedHasAgg = pNC->hasAgg;
1327 pNC->hasAgg = 0;
danielk1977b3bce662005-01-29 08:32:43 +00001328 walkExprTree(pExpr, nameResolverStep, pNC);
1329 if( pNC->nErr>0 ){
drh73b211a2005-01-18 04:00:42 +00001330 ExprSetProperty(pExpr, EP_Error);
drh73b211a2005-01-18 04:00:42 +00001331 }
drh13449892005-09-07 21:22:45 +00001332 if( pNC->hasAgg ){
1333 ExprSetProperty(pExpr, EP_Agg);
1334 }else if( savedHasAgg ){
1335 pNC->hasAgg = 1;
1336 }
drh73b211a2005-01-18 04:00:42 +00001337 return ExprHasProperty(pExpr, EP_Error);
drh626a8792005-01-17 22:08:19 +00001338}
1339
drh1398ad32005-01-19 23:24:50 +00001340/*
1341** A pointer instance of this structure is used to pass information
1342** through walkExprTree into codeSubqueryStep().
1343*/
1344typedef struct QueryCoder QueryCoder;
1345struct QueryCoder {
1346 Parse *pParse; /* The parsing context */
1347 NameContext *pNC; /* Namespace of first enclosing query */
1348};
1349
drh626a8792005-01-17 22:08:19 +00001350
1351/*
drh9cbe6352005-11-29 03:13:21 +00001352** Generate code for scalar subqueries used as an expression
1353** and IN operators. Examples:
drh626a8792005-01-17 22:08:19 +00001354**
drh9cbe6352005-11-29 03:13:21 +00001355** (SELECT a FROM b) -- subquery
1356** EXISTS (SELECT a FROM b) -- EXISTS subquery
1357** x IN (4,5,11) -- IN operator with list on right-hand side
1358** x IN (SELECT a FROM b) -- IN operator with subquery on the right
drhfef52082000-06-06 01:50:43 +00001359**
drh9cbe6352005-11-29 03:13:21 +00001360** The pExpr parameter describes the expression that contains the IN
1361** operator or subquery.
drhcce7d172000-05-31 15:34:51 +00001362*/
drh51522cd2005-01-20 13:36:19 +00001363#ifndef SQLITE_OMIT_SUBQUERY
danielk1977b3bce662005-01-29 08:32:43 +00001364void sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){
drh57dbd7b2005-07-08 18:25:26 +00001365 int testAddr = 0; /* One-time test address */
danielk1977b3bce662005-01-29 08:32:43 +00001366 Vdbe *v = sqlite3GetVdbe(pParse);
1367 if( v==0 ) return;
1368
drh57dbd7b2005-07-08 18:25:26 +00001369 /* This code must be run in its entirety every time it is encountered
1370 ** if any of the following is true:
1371 **
1372 ** * The right-hand side is a correlated subquery
1373 ** * The right-hand side is an expression list containing variables
1374 ** * We are inside a trigger
1375 **
1376 ** If all of the above are false, then we can run this code just once
1377 ** save the results, and reuse the same result on subsequent invocations.
danielk1977b3bce662005-01-29 08:32:43 +00001378 */
1379 if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->trigStack ){
1380 int mem = pParse->nMem++;
1381 sqlite3VdbeAddOp(v, OP_MemLoad, mem, 0);
drh57dbd7b2005-07-08 18:25:26 +00001382 testAddr = sqlite3VdbeAddOp(v, OP_If, 0, 0);
danielk19779e128002006-01-18 16:51:35 +00001383 assert( testAddr>0 || sqlite3MallocFailed() );
drhd654be82005-09-20 17:42:23 +00001384 sqlite3VdbeAddOp(v, OP_MemInt, 1, mem);
danielk1977b3bce662005-01-29 08:32:43 +00001385 }
1386
drhcce7d172000-05-31 15:34:51 +00001387 switch( pExpr->op ){
drhfef52082000-06-06 01:50:43 +00001388 case TK_IN: {
danielk1977e014a832004-05-17 10:48:57 +00001389 char affinity;
drhd3d39e92004-05-20 22:16:29 +00001390 KeyInfo keyInfo;
drhb9bb7c12006-06-11 23:41:55 +00001391 int addr; /* Address of OP_OpenEphemeral instruction */
drhd3d39e92004-05-20 22:16:29 +00001392
danielk1977bf3b7212004-05-18 10:06:24 +00001393 affinity = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +00001394
1395 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
drh57dbd7b2005-07-08 18:25:26 +00001396 ** expression it is handled the same way. A virtual table is
danielk1977e014a832004-05-17 10:48:57 +00001397 ** filled with single-field index keys representing the results
1398 ** from the SELECT or the <exprlist>.
1399 **
1400 ** If the 'x' expression is a column value, or the SELECT...
1401 ** statement returns a column value, then the affinity of that
1402 ** column is used to build the index keys. If both 'x' and the
1403 ** SELECT... statement are columns, then numeric affinity is used
1404 ** if either column has NUMERIC or INTEGER affinity. If neither
1405 ** 'x' nor the SELECT... statement are columns, then numeric affinity
1406 ** is used.
1407 */
1408 pExpr->iTable = pParse->nTab++;
drhb9bb7c12006-06-11 23:41:55 +00001409 addr = sqlite3VdbeAddOp(v, OP_OpenEphemeral, pExpr->iTable, 0);
drhd3d39e92004-05-20 22:16:29 +00001410 memset(&keyInfo, 0, sizeof(keyInfo));
1411 keyInfo.nField = 1;
drhf3218fe2004-05-28 08:21:02 +00001412 sqlite3VdbeAddOp(v, OP_SetNumColumns, pExpr->iTable, 1);
danielk1977e014a832004-05-17 10:48:57 +00001413
drhfef52082000-06-06 01:50:43 +00001414 if( pExpr->pSelect ){
1415 /* Case 1: expr IN (SELECT ...)
1416 **
danielk1977e014a832004-05-17 10:48:57 +00001417 ** Generate code to write the results of the select into the temporary
1418 ** table allocated and opened above.
drhfef52082000-06-06 01:50:43 +00001419 */
danielk1977e014a832004-05-17 10:48:57 +00001420 int iParm = pExpr->iTable + (((int)affinity)<<16);
drhbe5c89a2004-07-26 00:31:09 +00001421 ExprList *pEList;
danielk1977e014a832004-05-17 10:48:57 +00001422 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
danielk1977b3bce662005-01-29 08:32:43 +00001423 sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001424 pEList = pExpr->pSelect->pEList;
1425 if( pEList && pEList->nExpr>0 ){
danielk19777cedc8d2004-06-10 10:50:08 +00001426 keyInfo.aColl[0] = binaryCompareCollSeq(pParse, pExpr->pLeft,
drhbe5c89a2004-07-26 00:31:09 +00001427 pEList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001428 }
drhfef52082000-06-06 01:50:43 +00001429 }else if( pExpr->pList ){
1430 /* Case 2: expr IN (exprlist)
1431 **
danielk1977e014a832004-05-17 10:48:57 +00001432 ** For each expression, build an index key from the evaluation and
1433 ** store it in the temporary table. If <expr> is a column, then use
1434 ** that columns affinity when building index keys. If <expr> is not
1435 ** a column, use numeric affinity.
drhfef52082000-06-06 01:50:43 +00001436 */
danielk1977e014a832004-05-17 10:48:57 +00001437 int i;
drh57dbd7b2005-07-08 18:25:26 +00001438 ExprList *pList = pExpr->pList;
1439 struct ExprList_item *pItem;
1440
danielk1977e014a832004-05-17 10:48:57 +00001441 if( !affinity ){
drh8159a352006-05-23 23:22:29 +00001442 affinity = SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +00001443 }
danielk19770202b292004-06-09 09:55:16 +00001444 keyInfo.aColl[0] = pExpr->pLeft->pColl;
danielk1977e014a832004-05-17 10:48:57 +00001445
1446 /* Loop through each expression in <exprlist>. */
drh57dbd7b2005-07-08 18:25:26 +00001447 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
1448 Expr *pE2 = pItem->pExpr;
danielk1977e014a832004-05-17 10:48:57 +00001449
drh57dbd7b2005-07-08 18:25:26 +00001450 /* If the expression is not constant then we will need to
1451 ** disable the test that was generated above that makes sure
1452 ** this code only executes once. Because for a non-constant
1453 ** expression we need to rerun this code each time.
1454 */
drh6c30be82005-07-29 15:10:17 +00001455 if( testAddr>0 && !sqlite3ExprIsConstant(pE2) ){
drhf8875402006-03-17 13:56:34 +00001456 sqlite3VdbeChangeToNoop(v, testAddr-1, 3);
drh57dbd7b2005-07-08 18:25:26 +00001457 testAddr = 0;
drh4794b982000-06-06 13:54:14 +00001458 }
danielk1977e014a832004-05-17 10:48:57 +00001459
1460 /* Evaluate the expression and insert it into the temp table */
1461 sqlite3ExprCode(pParse, pE2);
drh94a11212004-09-25 13:12:14 +00001462 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1);
drhf0863fe2005-06-12 21:35:51 +00001463 sqlite3VdbeAddOp(v, OP_IdxInsert, pExpr->iTable, 0);
drhfef52082000-06-06 01:50:43 +00001464 }
1465 }
danielk19770202b292004-06-09 09:55:16 +00001466 sqlite3VdbeChangeP3(v, addr, (void *)&keyInfo, P3_KEYINFO);
danielk1977b3bce662005-01-29 08:32:43 +00001467 break;
drhfef52082000-06-06 01:50:43 +00001468 }
1469
drh51522cd2005-01-20 13:36:19 +00001470 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001471 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +00001472 /* This has to be a scalar SELECT. Generate code to put the
1473 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +00001474 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +00001475 */
drh2646da72005-12-09 20:02:05 +00001476 static const Token one = { (u8*)"1", 0, 1 };
drh51522cd2005-01-20 13:36:19 +00001477 Select *pSel;
drhec7429a2005-10-06 16:53:14 +00001478 int iMem;
1479 int sop;
drh1398ad32005-01-19 23:24:50 +00001480
drhec7429a2005-10-06 16:53:14 +00001481 pExpr->iColumn = iMem = pParse->nMem++;
drh51522cd2005-01-20 13:36:19 +00001482 pSel = pExpr->pSelect;
1483 if( pExpr->op==TK_SELECT ){
1484 sop = SRT_Mem;
drhec7429a2005-10-06 16:53:14 +00001485 sqlite3VdbeAddOp(v, OP_MemNull, iMem, 0);
1486 VdbeComment((v, "# Init subquery result"));
drh51522cd2005-01-20 13:36:19 +00001487 }else{
drh51522cd2005-01-20 13:36:19 +00001488 sop = SRT_Exists;
drhec7429a2005-10-06 16:53:14 +00001489 sqlite3VdbeAddOp(v, OP_MemInt, 0, iMem);
1490 VdbeComment((v, "# Init EXISTS result"));
drh51522cd2005-01-20 13:36:19 +00001491 }
drhec7429a2005-10-06 16:53:14 +00001492 sqlite3ExprDelete(pSel->pLimit);
1493 pSel->pLimit = sqlite3Expr(TK_INTEGER, 0, 0, &one);
1494 sqlite3Select(pParse, pSel, sop, iMem, 0, 0, 0, 0);
danielk1977b3bce662005-01-29 08:32:43 +00001495 break;
drhcce7d172000-05-31 15:34:51 +00001496 }
1497 }
danielk1977b3bce662005-01-29 08:32:43 +00001498
drh57dbd7b2005-07-08 18:25:26 +00001499 if( testAddr ){
drhd654be82005-09-20 17:42:23 +00001500 sqlite3VdbeJumpHere(v, testAddr);
danielk1977b3bce662005-01-29 08:32:43 +00001501 }
1502 return;
drhcce7d172000-05-31 15:34:51 +00001503}
drh51522cd2005-01-20 13:36:19 +00001504#endif /* SQLITE_OMIT_SUBQUERY */
drhcce7d172000-05-31 15:34:51 +00001505
drhcce7d172000-05-31 15:34:51 +00001506/*
drhfec19aa2004-05-19 20:41:03 +00001507** Generate an instruction that will put the integer describe by
1508** text z[0..n-1] on the stack.
1509*/
1510static void codeInteger(Vdbe *v, const char *z, int n){
1511 int i;
drh6fec0762004-05-30 01:38:43 +00001512 if( sqlite3GetInt32(z, &i) ){
1513 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
1514 }else if( sqlite3FitsIn64Bits(z) ){
drh29dda4a2005-07-21 18:23:20 +00001515 sqlite3VdbeOp3(v, OP_Int64, 0, 0, z, n);
drhfec19aa2004-05-19 20:41:03 +00001516 }else{
1517 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n);
1518 }
1519}
1520
drh945498f2007-02-24 11:52:52 +00001521
1522/*
1523** Generate code that will extract the iColumn-th column from
1524** table pTab and push that column value on the stack. There
1525** is an open cursor to pTab in iTable. If iColumn<0 then
1526** code is generated that extracts the rowid.
1527*/
1528void sqlite3ExprCodeGetColumn(Vdbe *v, Table *pTab, int iColumn, int iTable){
1529 if( iColumn<0 ){
1530 int op = (pTab && IsVirtual(pTab)) ? OP_VRowid : OP_Rowid;
1531 sqlite3VdbeAddOp(v, op, iTable, 0);
1532 }else if( pTab==0 ){
1533 sqlite3VdbeAddOp(v, OP_Column, iTable, iColumn);
1534 }else{
1535 int op = IsVirtual(pTab) ? OP_VColumn : OP_Column;
1536 sqlite3VdbeAddOp(v, op, iTable, iColumn);
1537 sqlite3ColumnDefault(v, pTab, iColumn);
1538#ifndef SQLITE_OMIT_FLOATING_POINT
1539 if( pTab->aCol[iColumn].affinity==SQLITE_AFF_REAL ){
1540 sqlite3VdbeAddOp(v, OP_RealAffinity, 0, 0);
1541 }
1542#endif
1543 }
1544}
1545
drhfec19aa2004-05-19 20:41:03 +00001546/*
drhcce7d172000-05-31 15:34:51 +00001547** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +00001548** expression and leave the result on the top of stack.
drhf2bc0132004-10-04 13:19:23 +00001549**
1550** This code depends on the fact that certain token values (ex: TK_EQ)
1551** are the same as opcode values (ex: OP_Eq) that implement the corresponding
1552** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
1553** the make process cause these values to align. Assert()s in the code
1554** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00001555*/
danielk19774adee202004-05-08 08:23:19 +00001556void sqlite3ExprCode(Parse *pParse, Expr *pExpr){
drhcce7d172000-05-31 15:34:51 +00001557 Vdbe *v = pParse->pVdbe;
1558 int op;
drhffe07b22005-11-03 00:41:17 +00001559 int stackChng = 1; /* Amount of change to stack depth */
1560
danielk19777977a172004-11-09 12:44:37 +00001561 if( v==0 ) return;
1562 if( pExpr==0 ){
drhf0863fe2005-06-12 21:35:51 +00001563 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
danielk19777977a172004-11-09 12:44:37 +00001564 return;
1565 }
drhf2bc0132004-10-04 13:19:23 +00001566 op = pExpr->op;
1567 switch( op ){
drh13449892005-09-07 21:22:45 +00001568 case TK_AGG_COLUMN: {
1569 AggInfo *pAggInfo = pExpr->pAggInfo;
1570 struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg];
1571 if( !pAggInfo->directMode ){
1572 sqlite3VdbeAddOp(v, OP_MemLoad, pCol->iMem, 0);
1573 break;
1574 }else if( pAggInfo->useSortingIdx ){
1575 sqlite3VdbeAddOp(v, OP_Column, pAggInfo->sortingIdx,
1576 pCol->iSorterColumn);
1577 break;
1578 }
1579 /* Otherwise, fall thru into the TK_COLUMN case */
1580 }
drh967e8b72000-06-21 13:59:10 +00001581 case TK_COLUMN: {
drhffe07b22005-11-03 00:41:17 +00001582 if( pExpr->iTable<0 ){
1583 /* This only happens when coding check constraints */
1584 assert( pParse->ckOffset>0 );
1585 sqlite3VdbeAddOp(v, OP_Dup, pParse->ckOffset-pExpr->iColumn-1, 1);
drhc4a3c772001-04-04 11:48:57 +00001586 }else{
drh945498f2007-02-24 11:52:52 +00001587 sqlite3ExprCodeGetColumn(v, pExpr->pTab, pExpr->iColumn, pExpr->iTable);
drh22827922000-06-06 17:27:05 +00001588 }
drhcce7d172000-05-31 15:34:51 +00001589 break;
1590 }
1591 case TK_INTEGER: {
drh2646da72005-12-09 20:02:05 +00001592 codeInteger(v, (char*)pExpr->token.z, pExpr->token.n);
drhfec19aa2004-05-19 20:41:03 +00001593 break;
1594 }
1595 case TK_FLOAT:
1596 case TK_STRING: {
drhf2bc0132004-10-04 13:19:23 +00001597 assert( TK_FLOAT==OP_Real );
1598 assert( TK_STRING==OP_String8 );
drhd2687b72005-08-12 22:56:09 +00001599 sqlite3DequoteExpr(pExpr);
drh2646da72005-12-09 20:02:05 +00001600 sqlite3VdbeOp3(v, op, 0, 0, (char*)pExpr->token.z, pExpr->token.n);
drhcce7d172000-05-31 15:34:51 +00001601 break;
1602 }
drhf0863fe2005-06-12 21:35:51 +00001603 case TK_NULL: {
1604 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
1605 break;
1606 }
danielk19775338a5f2005-01-20 13:03:10 +00001607#ifndef SQLITE_OMIT_BLOB_LITERAL
danielk1977c572ef72004-05-27 09:28:41 +00001608 case TK_BLOB: {
drh6c8c6ce2005-08-23 11:17:58 +00001609 int n;
1610 const char *z;
drhf2bc0132004-10-04 13:19:23 +00001611 assert( TK_BLOB==OP_HexBlob );
drh6c8c6ce2005-08-23 11:17:58 +00001612 n = pExpr->token.n - 3;
drh2646da72005-12-09 20:02:05 +00001613 z = (char*)pExpr->token.z + 2;
drh6c8c6ce2005-08-23 11:17:58 +00001614 assert( n>=0 );
1615 if( n==0 ){
1616 z = "";
1617 }
1618 sqlite3VdbeOp3(v, op, 0, 0, z, n);
danielk1977c572ef72004-05-27 09:28:41 +00001619 break;
1620 }
danielk19775338a5f2005-01-20 13:03:10 +00001621#endif
drh50457892003-09-06 01:10:47 +00001622 case TK_VARIABLE: {
danielk19774adee202004-05-08 08:23:19 +00001623 sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0);
drh895d7472004-08-20 16:02:39 +00001624 if( pExpr->token.n>1 ){
drh2646da72005-12-09 20:02:05 +00001625 sqlite3VdbeChangeP3(v, -1, (char*)pExpr->token.z, pExpr->token.n);
drh895d7472004-08-20 16:02:39 +00001626 }
drh50457892003-09-06 01:10:47 +00001627 break;
1628 }
drh4e0cff62004-11-05 05:10:28 +00001629 case TK_REGISTER: {
1630 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iTable, 0);
1631 break;
1632 }
drh487e2622005-06-25 18:42:14 +00001633#ifndef SQLITE_OMIT_CAST
1634 case TK_CAST: {
1635 /* Expressions of the form: CAST(pLeft AS token) */
danielk1977f0113002006-01-24 12:09:17 +00001636 int aff, to_op;
drh487e2622005-06-25 18:42:14 +00001637 sqlite3ExprCode(pParse, pExpr->pLeft);
drh8a512562005-11-14 22:29:05 +00001638 aff = sqlite3AffinityType(&pExpr->token);
danielk1977f0113002006-01-24 12:09:17 +00001639 to_op = aff - SQLITE_AFF_TEXT + OP_ToText;
1640 assert( to_op==OP_ToText || aff!=SQLITE_AFF_TEXT );
1641 assert( to_op==OP_ToBlob || aff!=SQLITE_AFF_NONE );
1642 assert( to_op==OP_ToNumeric || aff!=SQLITE_AFF_NUMERIC );
1643 assert( to_op==OP_ToInt || aff!=SQLITE_AFF_INTEGER );
1644 assert( to_op==OP_ToReal || aff!=SQLITE_AFF_REAL );
1645 sqlite3VdbeAddOp(v, to_op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001646 stackChng = 0;
drh487e2622005-06-25 18:42:14 +00001647 break;
1648 }
1649#endif /* SQLITE_OMIT_CAST */
drhc9b84a12002-06-20 11:36:48 +00001650 case TK_LT:
1651 case TK_LE:
1652 case TK_GT:
1653 case TK_GE:
1654 case TK_NE:
1655 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00001656 assert( TK_LT==OP_Lt );
1657 assert( TK_LE==OP_Le );
1658 assert( TK_GT==OP_Gt );
1659 assert( TK_GE==OP_Ge );
1660 assert( TK_EQ==OP_Eq );
1661 assert( TK_NE==OP_Ne );
danielk1977a37cdde2004-05-16 11:15:36 +00001662 sqlite3ExprCode(pParse, pExpr->pLeft);
1663 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001664 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001665 stackChng = -1;
danielk1977a37cdde2004-05-16 11:15:36 +00001666 break;
drhc9b84a12002-06-20 11:36:48 +00001667 }
drhcce7d172000-05-31 15:34:51 +00001668 case TK_AND:
1669 case TK_OR:
1670 case TK_PLUS:
1671 case TK_STAR:
1672 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00001673 case TK_REM:
1674 case TK_BITAND:
1675 case TK_BITOR:
drh17c40292004-07-21 02:53:29 +00001676 case TK_SLASH:
drhbf4133c2001-10-13 02:59:08 +00001677 case TK_LSHIFT:
drh855eb1c2004-08-31 13:45:11 +00001678 case TK_RSHIFT:
drh00400772000-06-16 20:51:26 +00001679 case TK_CONCAT: {
drhf2bc0132004-10-04 13:19:23 +00001680 assert( TK_AND==OP_And );
1681 assert( TK_OR==OP_Or );
1682 assert( TK_PLUS==OP_Add );
1683 assert( TK_MINUS==OP_Subtract );
1684 assert( TK_REM==OP_Remainder );
1685 assert( TK_BITAND==OP_BitAnd );
1686 assert( TK_BITOR==OP_BitOr );
1687 assert( TK_SLASH==OP_Divide );
1688 assert( TK_LSHIFT==OP_ShiftLeft );
1689 assert( TK_RSHIFT==OP_ShiftRight );
1690 assert( TK_CONCAT==OP_Concat );
danielk19774adee202004-05-08 08:23:19 +00001691 sqlite3ExprCode(pParse, pExpr->pLeft);
1692 sqlite3ExprCode(pParse, pExpr->pRight);
drh855eb1c2004-08-31 13:45:11 +00001693 sqlite3VdbeAddOp(v, op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001694 stackChng = -1;
drh00400772000-06-16 20:51:26 +00001695 break;
1696 }
drhcce7d172000-05-31 15:34:51 +00001697 case TK_UMINUS: {
drhfec19aa2004-05-19 20:41:03 +00001698 Expr *pLeft = pExpr->pLeft;
1699 assert( pLeft );
1700 if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
1701 Token *p = &pLeft->token;
drh9267bdc2005-11-28 12:36:35 +00001702 char *z = sqlite3MPrintf("-%.*s", p->n, p->z);
drhfec19aa2004-05-19 20:41:03 +00001703 if( pLeft->op==TK_FLOAT ){
1704 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001705 }else{
drhfec19aa2004-05-19 20:41:03 +00001706 codeInteger(v, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001707 }
drh6e142f52000-06-08 13:36:40 +00001708 sqliteFree(z);
1709 break;
1710 }
drh1ccde152000-06-17 13:12:39 +00001711 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +00001712 }
drhbf4133c2001-10-13 02:59:08 +00001713 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00001714 case TK_NOT: {
drhf2bc0132004-10-04 13:19:23 +00001715 assert( TK_BITNOT==OP_BitNot );
1716 assert( TK_NOT==OP_Not );
danielk19774adee202004-05-08 08:23:19 +00001717 sqlite3ExprCode(pParse, pExpr->pLeft);
1718 sqlite3VdbeAddOp(v, op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001719 stackChng = 0;
drhcce7d172000-05-31 15:34:51 +00001720 break;
1721 }
1722 case TK_ISNULL:
1723 case TK_NOTNULL: {
1724 int dest;
drhf2bc0132004-10-04 13:19:23 +00001725 assert( TK_ISNULL==OP_IsNull );
1726 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00001727 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1728 sqlite3ExprCode(pParse, pExpr->pLeft);
1729 dest = sqlite3VdbeCurrentAddr(v) + 2;
1730 sqlite3VdbeAddOp(v, op, 1, dest);
1731 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);
drhffe07b22005-11-03 00:41:17 +00001732 stackChng = 0;
drhf2bc0132004-10-04 13:19:23 +00001733 break;
drhcce7d172000-05-31 15:34:51 +00001734 }
drh22827922000-06-06 17:27:05 +00001735 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00001736 AggInfo *pInfo = pExpr->pAggInfo;
drh7e56e712005-11-16 12:53:15 +00001737 if( pInfo==0 ){
1738 sqlite3ErrorMsg(pParse, "misuse of aggregate: %T",
1739 &pExpr->span);
1740 }else{
1741 sqlite3VdbeAddOp(v, OP_MemLoad, pInfo->aFunc[pExpr->iAgg].iMem, 0);
1742 }
drh22827922000-06-06 17:27:05 +00001743 break;
1744 }
drhb71090f2005-05-23 17:26:51 +00001745 case TK_CONST_FUNC:
drhcce7d172000-05-31 15:34:51 +00001746 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00001747 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00001748 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00001749 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00001750 int nId;
1751 const char *zId;
drh13449892005-09-07 21:22:45 +00001752 int constMask = 0;
danielk1977682f68b2004-06-05 10:22:17 +00001753 int i;
danielk197714db2662006-01-09 16:12:04 +00001754 u8 enc = ENC(pParse->db);
danielk1977dc1bdc42004-06-11 10:51:27 +00001755 CollSeq *pColl = 0;
drh2646da72005-12-09 20:02:05 +00001756 zId = (char*)pExpr->token.z;
drhb71090f2005-05-23 17:26:51 +00001757 nId = pExpr->token.n;
danielk1977d8123362004-06-12 09:25:12 +00001758 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001759 assert( pDef!=0 );
drhf9b596e2004-05-26 16:54:42 +00001760 nExpr = sqlite3ExprCodeExprList(pParse, pList);
drhb7f6f682006-07-08 17:06:43 +00001761#ifndef SQLITE_OMIT_VIRTUALTABLE
drha43fa222006-07-08 18:41:37 +00001762 /* Possibly overload the function if the first argument is
1763 ** a virtual table column.
1764 **
1765 ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the
1766 ** second argument, not the first, as the argument to test to
1767 ** see if it is a column in a virtual table. This is done because
1768 ** the left operand of infix functions (the operand we want to
1769 ** control overloading) ends up as the second argument to the
1770 ** function. The expression "A glob B" is equivalent to
1771 ** "glob(B,A). We want to use the A in "A glob B" to test
1772 ** for function overloading. But we use the B term in "glob(B,A)".
1773 */
drh6a03a1c2006-07-08 18:34:59 +00001774 if( nExpr>=2 && (pExpr->flags & EP_InfixFunc) ){
drh6a03a1c2006-07-08 18:34:59 +00001775 pDef = sqlite3VtabOverloadFunction(pDef, nExpr, pList->a[1].pExpr);
1776 }else if( nExpr>0 ){
drhb7f6f682006-07-08 17:06:43 +00001777 pDef = sqlite3VtabOverloadFunction(pDef, nExpr, pList->a[0].pExpr);
1778 }
1779#endif
danielk1977682f68b2004-06-05 10:22:17 +00001780 for(i=0; i<nExpr && i<32; i++){
danielk1977d02eb1f2004-06-06 09:44:03 +00001781 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
drh13449892005-09-07 21:22:45 +00001782 constMask |= (1<<i);
danielk1977d02eb1f2004-06-06 09:44:03 +00001783 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001784 if( pDef->needCollSeq && !pColl ){
1785 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
1786 }
1787 }
1788 if( pDef->needCollSeq ){
1789 if( !pColl ) pColl = pParse->db->pDfltColl;
danielk1977d8123362004-06-12 09:25:12 +00001790 sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ);
danielk1977682f68b2004-06-05 10:22:17 +00001791 }
drh13449892005-09-07 21:22:45 +00001792 sqlite3VdbeOp3(v, OP_Function, constMask, nExpr, (char*)pDef, P3_FUNCDEF);
drhffe07b22005-11-03 00:41:17 +00001793 stackChng = 1-nExpr;
drhcce7d172000-05-31 15:34:51 +00001794 break;
1795 }
drhfe2093d2005-01-20 22:48:47 +00001796#ifndef SQLITE_OMIT_SUBQUERY
1797 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001798 case TK_SELECT: {
drh41714d62006-03-02 04:44:23 +00001799 if( pExpr->iColumn==0 ){
1800 sqlite3CodeSubselect(pParse, pExpr);
1801 }
danielk19774adee202004-05-08 08:23:19 +00001802 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drhad6d9462004-09-19 02:15:24 +00001803 VdbeComment((v, "# load subquery result"));
drh19a775c2000-06-05 18:54:46 +00001804 break;
1805 }
drhfef52082000-06-06 01:50:43 +00001806 case TK_IN: {
1807 int addr;
drh94a11212004-09-25 13:12:14 +00001808 char affinity;
drhafa5f682006-01-30 14:36:59 +00001809 int ckOffset = pParse->ckOffset;
danielk1977b3bce662005-01-29 08:32:43 +00001810 sqlite3CodeSubselect(pParse, pExpr);
danielk1977e014a832004-05-17 10:48:57 +00001811
1812 /* Figure out the affinity to use to create a key from the results
1813 ** of the expression. affinityStr stores a static string suitable for
danielk1977ededfd52004-06-17 07:53:01 +00001814 ** P3 of OP_MakeRecord.
danielk1977e014a832004-05-17 10:48:57 +00001815 */
drh94a11212004-09-25 13:12:14 +00001816 affinity = comparisonAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +00001817
danielk19774adee202004-05-08 08:23:19 +00001818 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
drhafa5f682006-01-30 14:36:59 +00001819 pParse->ckOffset = ckOffset+1;
danielk1977e014a832004-05-17 10:48:57 +00001820
1821 /* Code the <expr> from "<expr> IN (...)". The temporary table
1822 ** pExpr->iTable contains the values that make up the (...) set.
1823 */
danielk19774adee202004-05-08 08:23:19 +00001824 sqlite3ExprCode(pParse, pExpr->pLeft);
1825 addr = sqlite3VdbeCurrentAddr(v);
danielk1977e014a832004-05-17 10:48:57 +00001826 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); /* addr + 0 */
danielk19774adee202004-05-08 08:23:19 +00001827 sqlite3VdbeAddOp(v, OP_Pop, 2, 0);
drhf0863fe2005-06-12 21:35:51 +00001828 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +00001829 sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7);
drh94a11212004-09-25 13:12:14 +00001830 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1); /* addr + 4 */
danielk1977e014a832004-05-17 10:48:57 +00001831 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7);
1832 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); /* addr + 6 */
1833
drhfef52082000-06-06 01:50:43 +00001834 break;
1835 }
danielk197793758c82005-01-21 08:13:14 +00001836#endif
drhfef52082000-06-06 01:50:43 +00001837 case TK_BETWEEN: {
drhbe5c89a2004-07-26 00:31:09 +00001838 Expr *pLeft = pExpr->pLeft;
1839 struct ExprList_item *pLItem = pExpr->pList->a;
1840 Expr *pRight = pLItem->pExpr;
1841 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001842 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001843 sqlite3ExprCode(pParse, pRight);
1844 codeCompare(pParse, pLeft, pRight, OP_Ge, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001845 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
drhbe5c89a2004-07-26 00:31:09 +00001846 pLItem++;
1847 pRight = pLItem->pExpr;
1848 sqlite3ExprCode(pParse, pRight);
1849 codeCompare(pParse, pLeft, pRight, OP_Le, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001850 sqlite3VdbeAddOp(v, OP_And, 0, 0);
drhfef52082000-06-06 01:50:43 +00001851 break;
1852 }
drh51e9a442004-01-16 16:42:53 +00001853 case TK_UPLUS:
drha2e00042002-01-22 03:13:42 +00001854 case TK_AS: {
danielk19774adee202004-05-08 08:23:19 +00001855 sqlite3ExprCode(pParse, pExpr->pLeft);
drhffe07b22005-11-03 00:41:17 +00001856 stackChng = 0;
drha2e00042002-01-22 03:13:42 +00001857 break;
1858 }
drh17a7f8d2002-03-24 13:13:27 +00001859 case TK_CASE: {
1860 int expr_end_label;
drhf5905aa2002-05-26 20:54:33 +00001861 int jumpInst;
drhf5905aa2002-05-26 20:54:33 +00001862 int nExpr;
drh17a7f8d2002-03-24 13:13:27 +00001863 int i;
drhbe5c89a2004-07-26 00:31:09 +00001864 ExprList *pEList;
1865 struct ExprList_item *aListelem;
drh17a7f8d2002-03-24 13:13:27 +00001866
1867 assert(pExpr->pList);
1868 assert((pExpr->pList->nExpr % 2) == 0);
1869 assert(pExpr->pList->nExpr > 0);
drhbe5c89a2004-07-26 00:31:09 +00001870 pEList = pExpr->pList;
1871 aListelem = pEList->a;
1872 nExpr = pEList->nExpr;
danielk19774adee202004-05-08 08:23:19 +00001873 expr_end_label = sqlite3VdbeMakeLabel(v);
drh17a7f8d2002-03-24 13:13:27 +00001874 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001875 sqlite3ExprCode(pParse, pExpr->pLeft);
drh17a7f8d2002-03-24 13:13:27 +00001876 }
drhf5905aa2002-05-26 20:54:33 +00001877 for(i=0; i<nExpr; i=i+2){
drhbe5c89a2004-07-26 00:31:09 +00001878 sqlite3ExprCode(pParse, aListelem[i].pExpr);
drh17a7f8d2002-03-24 13:13:27 +00001879 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001880 sqlite3VdbeAddOp(v, OP_Dup, 1, 1);
drhbe5c89a2004-07-26 00:31:09 +00001881 jumpInst = codeCompare(pParse, pExpr->pLeft, aListelem[i].pExpr,
1882 OP_Ne, 0, 1);
danielk19774adee202004-05-08 08:23:19 +00001883 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001884 }else{
danielk19774adee202004-05-08 08:23:19 +00001885 jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001886 }
drhbe5c89a2004-07-26 00:31:09 +00001887 sqlite3ExprCode(pParse, aListelem[i+1].pExpr);
danielk19774adee202004-05-08 08:23:19 +00001888 sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label);
drhd654be82005-09-20 17:42:23 +00001889 sqlite3VdbeJumpHere(v, jumpInst);
drh17a7f8d2002-03-24 13:13:27 +00001890 }
drhf570f012002-05-31 15:51:25 +00001891 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001892 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhf570f012002-05-31 15:51:25 +00001893 }
drh17a7f8d2002-03-24 13:13:27 +00001894 if( pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001895 sqlite3ExprCode(pParse, pExpr->pRight);
drh17a7f8d2002-03-24 13:13:27 +00001896 }else{
drhf0863fe2005-06-12 21:35:51 +00001897 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
drh17a7f8d2002-03-24 13:13:27 +00001898 }
danielk19774adee202004-05-08 08:23:19 +00001899 sqlite3VdbeResolveLabel(v, expr_end_label);
danielk19776f349032002-06-11 02:25:40 +00001900 break;
1901 }
danielk19775338a5f2005-01-20 13:03:10 +00001902#ifndef SQLITE_OMIT_TRIGGER
danielk19776f349032002-06-11 02:25:40 +00001903 case TK_RAISE: {
1904 if( !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +00001905 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00001906 "RAISE() may only be used within a trigger-program");
danielk19776f349032002-06-11 02:25:40 +00001907 return;
1908 }
drhad6d9462004-09-19 02:15:24 +00001909 if( pExpr->iColumn!=OE_Ignore ){
1910 assert( pExpr->iColumn==OE_Rollback ||
1911 pExpr->iColumn == OE_Abort ||
1912 pExpr->iColumn == OE_Fail );
drhd2687b72005-08-12 22:56:09 +00001913 sqlite3DequoteExpr(pExpr);
drhad6d9462004-09-19 02:15:24 +00001914 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn,
drh2646da72005-12-09 20:02:05 +00001915 (char*)pExpr->token.z, pExpr->token.n);
danielk19776f349032002-06-11 02:25:40 +00001916 } else {
drhad6d9462004-09-19 02:15:24 +00001917 assert( pExpr->iColumn == OE_Ignore );
1918 sqlite3VdbeAddOp(v, OP_ContextPop, 0, 0);
1919 sqlite3VdbeAddOp(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
1920 VdbeComment((v, "# raise(IGNORE)"));
danielk19776f349032002-06-11 02:25:40 +00001921 }
drhffe07b22005-11-03 00:41:17 +00001922 stackChng = 0;
1923 break;
drh17a7f8d2002-03-24 13:13:27 +00001924 }
danielk19775338a5f2005-01-20 13:03:10 +00001925#endif
drhffe07b22005-11-03 00:41:17 +00001926 }
1927
1928 if( pParse->ckOffset ){
1929 pParse->ckOffset += stackChng;
1930 assert( pParse->ckOffset );
drhcce7d172000-05-31 15:34:51 +00001931 }
drhcce7d172000-05-31 15:34:51 +00001932}
1933
danielk197793758c82005-01-21 08:13:14 +00001934#ifndef SQLITE_OMIT_TRIGGER
drhcce7d172000-05-31 15:34:51 +00001935/*
drh25303782004-12-07 15:41:48 +00001936** Generate code that evalutes the given expression and leaves the result
1937** on the stack. See also sqlite3ExprCode().
1938**
1939** This routine might also cache the result and modify the pExpr tree
1940** so that it will make use of the cached result on subsequent evaluations
1941** rather than evaluate the whole expression again. Trivial expressions are
1942** not cached. If the expression is cached, its result is stored in a
1943** memory location.
1944*/
1945void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr){
1946 Vdbe *v = pParse->pVdbe;
1947 int iMem;
1948 int addr1, addr2;
1949 if( v==0 ) return;
1950 addr1 = sqlite3VdbeCurrentAddr(v);
1951 sqlite3ExprCode(pParse, pExpr);
1952 addr2 = sqlite3VdbeCurrentAddr(v);
1953 if( addr2>addr1+1 || sqlite3VdbeGetOp(v, addr1)->opcode==OP_Function ){
1954 iMem = pExpr->iTable = pParse->nMem++;
1955 sqlite3VdbeAddOp(v, OP_MemStore, iMem, 0);
1956 pExpr->op = TK_REGISTER;
1957 }
1958}
danielk197793758c82005-01-21 08:13:14 +00001959#endif
drh25303782004-12-07 15:41:48 +00001960
1961/*
drh268380c2004-02-25 13:47:31 +00001962** Generate code that pushes the value of every element of the given
drhf9b596e2004-05-26 16:54:42 +00001963** expression list onto the stack.
drh268380c2004-02-25 13:47:31 +00001964**
1965** Return the number of elements pushed onto the stack.
1966*/
danielk19774adee202004-05-08 08:23:19 +00001967int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00001968 Parse *pParse, /* Parsing context */
drhf9b596e2004-05-26 16:54:42 +00001969 ExprList *pList /* The expression list to be coded */
drh268380c2004-02-25 13:47:31 +00001970){
1971 struct ExprList_item *pItem;
1972 int i, n;
drh268380c2004-02-25 13:47:31 +00001973 if( pList==0 ) return 0;
drh268380c2004-02-25 13:47:31 +00001974 n = pList->nExpr;
drhc182d162005-08-14 20:47:16 +00001975 for(pItem=pList->a, i=n; i>0; i--, pItem++){
danielk19774adee202004-05-08 08:23:19 +00001976 sqlite3ExprCode(pParse, pItem->pExpr);
drh268380c2004-02-25 13:47:31 +00001977 }
drhf9b596e2004-05-26 16:54:42 +00001978 return n;
drh268380c2004-02-25 13:47:31 +00001979}
1980
1981/*
drhcce7d172000-05-31 15:34:51 +00001982** Generate code for a boolean expression such that a jump is made
1983** to the label "dest" if the expression is true but execution
1984** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00001985**
1986** If the expression evaluates to NULL (neither true nor false), then
1987** take the jump if the jumpIfNull flag is true.
drhf2bc0132004-10-04 13:19:23 +00001988**
1989** This code depends on the fact that certain token values (ex: TK_EQ)
1990** are the same as opcode values (ex: OP_Eq) that implement the corresponding
1991** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
1992** the make process cause these values to align. Assert()s in the code
1993** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00001994*/
danielk19774adee202004-05-08 08:23:19 +00001995void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001996 Vdbe *v = pParse->pVdbe;
1997 int op = 0;
drhffe07b22005-11-03 00:41:17 +00001998 int ckOffset = pParse->ckOffset;
drhdaffd0e2001-04-11 14:28:42 +00001999 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00002000 op = pExpr->op;
2001 switch( op ){
drhcce7d172000-05-31 15:34:51 +00002002 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00002003 int d2 = sqlite3VdbeMakeLabel(v);
2004 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
2005 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
2006 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00002007 break;
2008 }
2009 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00002010 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
2011 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002012 break;
2013 }
2014 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00002015 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002016 break;
2017 }
2018 case TK_LT:
2019 case TK_LE:
2020 case TK_GT:
2021 case TK_GE:
2022 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00002023 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00002024 assert( TK_LT==OP_Lt );
2025 assert( TK_LE==OP_Le );
2026 assert( TK_GT==OP_Gt );
2027 assert( TK_GE==OP_Ge );
2028 assert( TK_EQ==OP_Eq );
2029 assert( TK_NE==OP_Ne );
danielk19774adee202004-05-08 08:23:19 +00002030 sqlite3ExprCode(pParse, pExpr->pLeft);
2031 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00002032 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002033 break;
2034 }
2035 case TK_ISNULL:
2036 case TK_NOTNULL: {
drhf2bc0132004-10-04 13:19:23 +00002037 assert( TK_ISNULL==OP_IsNull );
2038 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00002039 sqlite3ExprCode(pParse, pExpr->pLeft);
2040 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00002041 break;
2042 }
drhfef52082000-06-06 01:50:43 +00002043 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00002044 /* The expression "x BETWEEN y AND z" is implemented as:
2045 **
2046 ** 1 IF (x < y) GOTO 3
2047 ** 2 IF (x <= z) GOTO <dest>
2048 ** 3 ...
2049 */
drhf5905aa2002-05-26 20:54:33 +00002050 int addr;
drhbe5c89a2004-07-26 00:31:09 +00002051 Expr *pLeft = pExpr->pLeft;
2052 Expr *pRight = pExpr->pList->a[0].pExpr;
2053 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00002054 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00002055 sqlite3ExprCode(pParse, pRight);
2056 addr = codeCompare(pParse, pLeft, pRight, OP_Lt, 0, !jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00002057
drhbe5c89a2004-07-26 00:31:09 +00002058 pRight = pExpr->pList->a[1].pExpr;
2059 sqlite3ExprCode(pParse, pRight);
2060 codeCompare(pParse, pLeft, pRight, OP_Le, dest, jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00002061
danielk19774adee202004-05-08 08:23:19 +00002062 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
drhd654be82005-09-20 17:42:23 +00002063 sqlite3VdbeJumpHere(v, addr);
danielk19774adee202004-05-08 08:23:19 +00002064 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00002065 break;
2066 }
drhcce7d172000-05-31 15:34:51 +00002067 default: {
danielk19774adee202004-05-08 08:23:19 +00002068 sqlite3ExprCode(pParse, pExpr);
2069 sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00002070 break;
2071 }
2072 }
drhffe07b22005-11-03 00:41:17 +00002073 pParse->ckOffset = ckOffset;
drhcce7d172000-05-31 15:34:51 +00002074}
2075
2076/*
drh66b89c82000-11-28 20:47:17 +00002077** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00002078** to the label "dest" if the expression is false but execution
2079** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00002080**
2081** If the expression evaluates to NULL (neither true nor false) then
2082** jump if jumpIfNull is true or fall through if jumpIfNull is false.
drhcce7d172000-05-31 15:34:51 +00002083*/
danielk19774adee202004-05-08 08:23:19 +00002084void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00002085 Vdbe *v = pParse->pVdbe;
2086 int op = 0;
drhffe07b22005-11-03 00:41:17 +00002087 int ckOffset = pParse->ckOffset;
drhdaffd0e2001-04-11 14:28:42 +00002088 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00002089
2090 /* The value of pExpr->op and op are related as follows:
2091 **
2092 ** pExpr->op op
2093 ** --------- ----------
2094 ** TK_ISNULL OP_NotNull
2095 ** TK_NOTNULL OP_IsNull
2096 ** TK_NE OP_Eq
2097 ** TK_EQ OP_Ne
2098 ** TK_GT OP_Le
2099 ** TK_LE OP_Gt
2100 ** TK_GE OP_Lt
2101 ** TK_LT OP_Ge
2102 **
2103 ** For other values of pExpr->op, op is undefined and unused.
2104 ** The value of TK_ and OP_ constants are arranged such that we
2105 ** can compute the mapping above using the following expression.
2106 ** Assert()s verify that the computation is correct.
2107 */
2108 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
2109
2110 /* Verify correct alignment of TK_ and OP_ constants
2111 */
2112 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
2113 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
2114 assert( pExpr->op!=TK_NE || op==OP_Eq );
2115 assert( pExpr->op!=TK_EQ || op==OP_Ne );
2116 assert( pExpr->op!=TK_LT || op==OP_Ge );
2117 assert( pExpr->op!=TK_LE || op==OP_Gt );
2118 assert( pExpr->op!=TK_GT || op==OP_Le );
2119 assert( pExpr->op!=TK_GE || op==OP_Lt );
2120
drhcce7d172000-05-31 15:34:51 +00002121 switch( pExpr->op ){
2122 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00002123 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
2124 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002125 break;
2126 }
2127 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00002128 int d2 = sqlite3VdbeMakeLabel(v);
2129 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
2130 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
2131 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00002132 break;
2133 }
2134 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00002135 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002136 break;
2137 }
2138 case TK_LT:
2139 case TK_LE:
2140 case TK_GT:
2141 case TK_GE:
2142 case TK_NE:
2143 case TK_EQ: {
danielk19774adee202004-05-08 08:23:19 +00002144 sqlite3ExprCode(pParse, pExpr->pLeft);
2145 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00002146 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002147 break;
2148 }
drhcce7d172000-05-31 15:34:51 +00002149 case TK_ISNULL:
2150 case TK_NOTNULL: {
danielk19774adee202004-05-08 08:23:19 +00002151 sqlite3ExprCode(pParse, pExpr->pLeft);
2152 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00002153 break;
2154 }
drhfef52082000-06-06 01:50:43 +00002155 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00002156 /* The expression is "x BETWEEN y AND z". It is implemented as:
2157 **
2158 ** 1 IF (x >= y) GOTO 3
2159 ** 2 GOTO <dest>
2160 ** 3 IF (x > z) GOTO <dest>
2161 */
drhfef52082000-06-06 01:50:43 +00002162 int addr;
drhbe5c89a2004-07-26 00:31:09 +00002163 Expr *pLeft = pExpr->pLeft;
2164 Expr *pRight = pExpr->pList->a[0].pExpr;
2165 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00002166 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00002167 sqlite3ExprCode(pParse, pRight);
danielk19774adee202004-05-08 08:23:19 +00002168 addr = sqlite3VdbeCurrentAddr(v);
drhbe5c89a2004-07-26 00:31:09 +00002169 codeCompare(pParse, pLeft, pRight, OP_Ge, addr+3, !jumpIfNull);
2170
danielk19774adee202004-05-08 08:23:19 +00002171 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
2172 sqlite3VdbeAddOp(v, OP_Goto, 0, dest);
drhbe5c89a2004-07-26 00:31:09 +00002173 pRight = pExpr->pList->a[1].pExpr;
2174 sqlite3ExprCode(pParse, pRight);
2175 codeCompare(pParse, pLeft, pRight, OP_Gt, dest, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00002176 break;
2177 }
drhcce7d172000-05-31 15:34:51 +00002178 default: {
danielk19774adee202004-05-08 08:23:19 +00002179 sqlite3ExprCode(pParse, pExpr);
2180 sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00002181 break;
2182 }
2183 }
drhffe07b22005-11-03 00:41:17 +00002184 pParse->ckOffset = ckOffset;
drhcce7d172000-05-31 15:34:51 +00002185}
drh22827922000-06-06 17:27:05 +00002186
2187/*
2188** Do a deep comparison of two expression trees. Return TRUE (non-zero)
2189** if they are identical and return FALSE if they differ in any way.
drhd40aab02007-02-24 15:29:03 +00002190**
2191** Sometimes this routine will return FALSE even if the two expressions
2192** really are equivalent. If we cannot prove that the expressions are
2193** identical, we return FALSE just to be safe. So if this routine
2194** returns false, then you do not really know for certain if the two
2195** expressions are the same. But if you get a TRUE return, then you
2196** can be sure the expressions are the same. In the places where
2197** this routine is used, it does not hurt to get an extra FALSE - that
2198** just might result in some slightly slower code. But returning
2199** an incorrect TRUE could lead to a malfunction.
drh22827922000-06-06 17:27:05 +00002200*/
danielk19774adee202004-05-08 08:23:19 +00002201int sqlite3ExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00002202 int i;
danielk19774b202ae2006-01-23 05:50:58 +00002203 if( pA==0||pB==0 ){
2204 return pB==pA;
drh22827922000-06-06 17:27:05 +00002205 }
2206 if( pA->op!=pB->op ) return 0;
drhfd357972005-09-09 01:33:19 +00002207 if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 0;
danielk19774adee202004-05-08 08:23:19 +00002208 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
2209 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00002210 if( pA->pList ){
2211 if( pB->pList==0 ) return 0;
2212 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
2213 for(i=0; i<pA->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00002214 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00002215 return 0;
2216 }
2217 }
2218 }else if( pB->pList ){
2219 return 0;
2220 }
2221 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00002222 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drhdd735212007-02-24 13:53:05 +00002223 if( pA->op!=TK_COLUMN && pA->token.z ){
drh22827922000-06-06 17:27:05 +00002224 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00002225 if( pB->token.n!=pA->token.n ) return 0;
drh2646da72005-12-09 20:02:05 +00002226 if( sqlite3StrNICmp((char*)pA->token.z,(char*)pB->token.z,pB->token.n)!=0 ){
2227 return 0;
2228 }
drh22827922000-06-06 17:27:05 +00002229 }
2230 return 1;
2231}
2232
drh13449892005-09-07 21:22:45 +00002233
drh22827922000-06-06 17:27:05 +00002234/*
drh13449892005-09-07 21:22:45 +00002235** Add a new element to the pAggInfo->aCol[] array. Return the index of
2236** the new element. Return a negative number if malloc fails.
drh22827922000-06-06 17:27:05 +00002237*/
drh13449892005-09-07 21:22:45 +00002238static int addAggInfoColumn(AggInfo *pInfo){
2239 int i;
2240 i = sqlite3ArrayAllocate((void**)&pInfo->aCol, sizeof(pInfo->aCol[0]), 3);
2241 if( i<0 ){
2242 return -1;
drh22827922000-06-06 17:27:05 +00002243 }
drh13449892005-09-07 21:22:45 +00002244 return i;
2245}
2246
2247/*
2248** Add a new element to the pAggInfo->aFunc[] array. Return the index of
2249** the new element. Return a negative number if malloc fails.
2250*/
2251static int addAggInfoFunc(AggInfo *pInfo){
2252 int i;
2253 i = sqlite3ArrayAllocate((void**)&pInfo->aFunc, sizeof(pInfo->aFunc[0]), 2);
2254 if( i<0 ){
2255 return -1;
2256 }
2257 return i;
2258}
drh22827922000-06-06 17:27:05 +00002259
2260/*
drh626a8792005-01-17 22:08:19 +00002261** This is an xFunc for walkExprTree() used to implement
2262** sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates
2263** for additional information.
drh22827922000-06-06 17:27:05 +00002264**
drh626a8792005-01-17 22:08:19 +00002265** This routine analyzes the aggregate function at pExpr.
drh22827922000-06-06 17:27:05 +00002266*/
drh626a8792005-01-17 22:08:19 +00002267static int analyzeAggregate(void *pArg, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00002268 int i;
danielk1977a58fdfb2005-02-08 07:50:40 +00002269 NameContext *pNC = (NameContext *)pArg;
2270 Parse *pParse = pNC->pParse;
2271 SrcList *pSrcList = pNC->pSrcList;
drh13449892005-09-07 21:22:45 +00002272 AggInfo *pAggInfo = pNC->pAggInfo;
2273
drh22827922000-06-06 17:27:05 +00002274
drh22827922000-06-06 17:27:05 +00002275 switch( pExpr->op ){
drh89c69d02007-01-04 01:20:28 +00002276 case TK_AGG_COLUMN:
drh967e8b72000-06-21 13:59:10 +00002277 case TK_COLUMN: {
drh13449892005-09-07 21:22:45 +00002278 /* Check to see if the column is in one of the tables in the FROM
2279 ** clause of the aggregate query */
2280 if( pSrcList ){
2281 struct SrcList_item *pItem = pSrcList->a;
2282 for(i=0; i<pSrcList->nSrc; i++, pItem++){
2283 struct AggInfo_col *pCol;
2284 if( pExpr->iTable==pItem->iCursor ){
2285 /* If we reach this point, it means that pExpr refers to a table
2286 ** that is in the FROM clause of the aggregate query.
2287 **
2288 ** Make an entry for the column in pAggInfo->aCol[] if there
2289 ** is not an entry there already.
2290 */
2291 pCol = pAggInfo->aCol;
2292 for(i=0; i<pAggInfo->nColumn; i++, pCol++){
2293 if( pCol->iTable==pExpr->iTable &&
2294 pCol->iColumn==pExpr->iColumn ){
2295 break;
2296 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002297 }
drh13449892005-09-07 21:22:45 +00002298 if( i>=pAggInfo->nColumn && (i = addAggInfoColumn(pAggInfo))>=0 ){
2299 pCol = &pAggInfo->aCol[i];
danielk19770817d0d2007-02-14 09:19:36 +00002300 pCol->pTab = pExpr->pTab;
drh13449892005-09-07 21:22:45 +00002301 pCol->iTable = pExpr->iTable;
2302 pCol->iColumn = pExpr->iColumn;
2303 pCol->iMem = pParse->nMem++;
2304 pCol->iSorterColumn = -1;
drh5774b802005-09-07 22:48:16 +00002305 pCol->pExpr = pExpr;
drh13449892005-09-07 21:22:45 +00002306 if( pAggInfo->pGroupBy ){
2307 int j, n;
2308 ExprList *pGB = pAggInfo->pGroupBy;
2309 struct ExprList_item *pTerm = pGB->a;
2310 n = pGB->nExpr;
2311 for(j=0; j<n; j++, pTerm++){
2312 Expr *pE = pTerm->pExpr;
2313 if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable &&
2314 pE->iColumn==pExpr->iColumn ){
2315 pCol->iSorterColumn = j;
2316 break;
2317 }
2318 }
2319 }
2320 if( pCol->iSorterColumn<0 ){
2321 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
2322 }
2323 }
2324 /* There is now an entry for pExpr in pAggInfo->aCol[] (either
2325 ** because it was there before or because we just created it).
2326 ** Convert the pExpr to be a TK_AGG_COLUMN referring to that
2327 ** pAggInfo->aCol[] entry.
2328 */
2329 pExpr->pAggInfo = pAggInfo;
2330 pExpr->op = TK_AGG_COLUMN;
2331 pExpr->iAgg = i;
2332 break;
2333 } /* endif pExpr->iTable==pItem->iCursor */
2334 } /* end loop over pSrcList */
drh22827922000-06-06 17:27:05 +00002335 }
drh626a8792005-01-17 22:08:19 +00002336 return 1;
drh22827922000-06-06 17:27:05 +00002337 }
2338 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00002339 /* The pNC->nDepth==0 test causes aggregate functions in subqueries
2340 ** to be ignored */
danielk1977a58fdfb2005-02-08 07:50:40 +00002341 if( pNC->nDepth==0 ){
drh13449892005-09-07 21:22:45 +00002342 /* Check to see if pExpr is a duplicate of another aggregate
2343 ** function that is already in the pAggInfo structure
2344 */
2345 struct AggInfo_func *pItem = pAggInfo->aFunc;
2346 for(i=0; i<pAggInfo->nFunc; i++, pItem++){
2347 if( sqlite3ExprCompare(pItem->pExpr, pExpr) ){
danielk1977a58fdfb2005-02-08 07:50:40 +00002348 break;
2349 }
drh22827922000-06-06 17:27:05 +00002350 }
drh13449892005-09-07 21:22:45 +00002351 if( i>=pAggInfo->nFunc ){
2352 /* pExpr is original. Make a new entry in pAggInfo->aFunc[]
2353 */
danielk197714db2662006-01-09 16:12:04 +00002354 u8 enc = ENC(pParse->db);
drh13449892005-09-07 21:22:45 +00002355 i = addAggInfoFunc(pAggInfo);
2356 if( i>=0 ){
2357 pItem = &pAggInfo->aFunc[i];
2358 pItem->pExpr = pExpr;
2359 pItem->iMem = pParse->nMem++;
2360 pItem->pFunc = sqlite3FindFunction(pParse->db,
drh2646da72005-12-09 20:02:05 +00002361 (char*)pExpr->token.z, pExpr->token.n,
drh13449892005-09-07 21:22:45 +00002362 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
drhfd357972005-09-09 01:33:19 +00002363 if( pExpr->flags & EP_Distinct ){
2364 pItem->iDistinct = pParse->nTab++;
2365 }else{
2366 pItem->iDistinct = -1;
2367 }
drh13449892005-09-07 21:22:45 +00002368 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002369 }
drh13449892005-09-07 21:22:45 +00002370 /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
2371 */
danielk1977a58fdfb2005-02-08 07:50:40 +00002372 pExpr->iAgg = i;
drh13449892005-09-07 21:22:45 +00002373 pExpr->pAggInfo = pAggInfo;
danielk1977a58fdfb2005-02-08 07:50:40 +00002374 return 1;
drh22827922000-06-06 17:27:05 +00002375 }
drh22827922000-06-06 17:27:05 +00002376 }
2377 }
drh13449892005-09-07 21:22:45 +00002378
2379 /* Recursively walk subqueries looking for TK_COLUMN nodes that need
2380 ** to be changed to TK_AGG_COLUMN. But increment nDepth so that
2381 ** TK_AGG_FUNCTION nodes in subqueries will be unchanged.
2382 */
danielk1977a58fdfb2005-02-08 07:50:40 +00002383 if( pExpr->pSelect ){
2384 pNC->nDepth++;
2385 walkSelectExpr(pExpr->pSelect, analyzeAggregate, pNC);
2386 pNC->nDepth--;
2387 }
drh626a8792005-01-17 22:08:19 +00002388 return 0;
2389}
2390
2391/*
2392** Analyze the given expression looking for aggregate functions and
2393** for variables that need to be added to the pParse->aAgg[] array.
2394** Make additional entries to the pParse->aAgg[] array as necessary.
2395**
2396** This routine should only be called after the expression has been
2397** analyzed by sqlite3ExprResolveNames().
2398**
2399** If errors are seen, leave an error message in zErrMsg and return
2400** the number of errors.
2401*/
danielk1977a58fdfb2005-02-08 07:50:40 +00002402int sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
2403 int nErr = pNC->pParse->nErr;
2404 walkExprTree(pExpr, analyzeAggregate, pNC);
2405 return pNC->pParse->nErr - nErr;
drh22827922000-06-06 17:27:05 +00002406}
drh5d9a4af2005-08-30 00:54:01 +00002407
2408/*
2409** Call sqlite3ExprAnalyzeAggregates() for every expression in an
2410** expression list. Return the number of errors.
2411**
2412** If an error is found, the analysis is cut short.
2413*/
2414int sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){
2415 struct ExprList_item *pItem;
2416 int i;
2417 int nErr = 0;
2418 if( pList ){
2419 for(pItem=pList->a, i=0; nErr==0 && i<pList->nExpr; i++, pItem++){
2420 nErr += sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
2421 }
2422 }
2423 return nErr;
2424}