blob: b915059d4ee2ff32ac9a0a17c60b6e3870c2dd7f [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**
drh8df447f2005-11-01 15:48:24 +000015** $Id: expr.c,v 1.232 2005/11/01 15:48:24 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 ){
drh8df447f2005-11-01 15:48:24 +000046 return sqlite3AffinityType(&pExpr->token, 0);
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/*
danielk19770202b292004-06-09 09:55:16 +000053** Return the default collation sequence for the expression pExpr. If
54** there is no default collation type, return 0.
55*/
danielk19777cedc8d2004-06-10 10:50:08 +000056CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
57 CollSeq *pColl = 0;
danielk19770202b292004-06-09 09:55:16 +000058 if( pExpr ){
danielk19777cedc8d2004-06-10 10:50:08 +000059 pColl = pExpr->pColl;
drh487e2622005-06-25 18:42:14 +000060 if( (pExpr->op==TK_AS || pExpr->op==TK_CAST) && !pColl ){
danielk19777cedc8d2004-06-10 10:50:08 +000061 return sqlite3ExprCollSeq(pParse, pExpr->pLeft);
danielk19770202b292004-06-09 09:55:16 +000062 }
63 }
danielk19777cedc8d2004-06-10 10:50:08 +000064 if( sqlite3CheckCollSeq(pParse, pColl) ){
65 pColl = 0;
66 }
67 return pColl;
danielk19770202b292004-06-09 09:55:16 +000068}
69
70/*
drh626a8792005-01-17 22:08:19 +000071** pExpr is an operand of a comparison operator. aff2 is the
72** type affinity of the other operand. This routine returns the
drh53db1452004-05-20 13:54:53 +000073** type affinity that should be used for the comparison operator.
74*/
danielk1977e014a832004-05-17 10:48:57 +000075char sqlite3CompareAffinity(Expr *pExpr, char aff2){
danielk1977bf3b7212004-05-18 10:06:24 +000076 char aff1 = sqlite3ExprAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +000077 if( aff1 && aff2 ){
drh8df447f2005-11-01 15:48:24 +000078 /* Both sides of the comparison are columns. If one has numeric
79 ** affinity, use that. Otherwise use no affinity.
danielk1977e014a832004-05-17 10:48:57 +000080 */
drh8df447f2005-11-01 15:48:24 +000081 if( aff1==SQLITE_AFF_NUMERIC || aff2==SQLITE_AFF_NUMERIC ){
danielk1977e014a832004-05-17 10:48:57 +000082 return SQLITE_AFF_NUMERIC;
83 }else{
84 return SQLITE_AFF_NONE;
85 }
86 }else if( !aff1 && !aff2 ){
drh5f6a87b2004-07-19 00:39:45 +000087 /* Neither side of the comparison is a column. Compare the
88 ** results directly.
danielk1977e014a832004-05-17 10:48:57 +000089 */
drh5f6a87b2004-07-19 00:39:45 +000090 return SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +000091 }else{
92 /* One side is a column, the other is not. Use the columns affinity. */
drhfe05af82005-07-21 03:14:59 +000093 assert( aff1==0 || aff2==0 );
danielk1977e014a832004-05-17 10:48:57 +000094 return (aff1 + aff2);
95 }
96}
97
drh53db1452004-05-20 13:54:53 +000098/*
99** pExpr is a comparison operator. Return the type affinity that should
100** be applied to both operands prior to doing the comparison.
101*/
danielk1977e014a832004-05-17 10:48:57 +0000102static char comparisonAffinity(Expr *pExpr){
103 char aff;
104 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
105 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
106 pExpr->op==TK_NE );
107 assert( pExpr->pLeft );
danielk1977bf3b7212004-05-18 10:06:24 +0000108 aff = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +0000109 if( pExpr->pRight ){
110 aff = sqlite3CompareAffinity(pExpr->pRight, aff);
111 }
112 else if( pExpr->pSelect ){
113 aff = sqlite3CompareAffinity(pExpr->pSelect->pEList->a[0].pExpr, aff);
114 }
115 else if( !aff ){
116 aff = SQLITE_AFF_NUMERIC;
117 }
118 return aff;
119}
120
121/*
122** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
123** idx_affinity is the affinity of an indexed column. Return true
124** if the index with affinity idx_affinity may be used to implement
125** the comparison in pExpr.
126*/
127int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){
128 char aff = comparisonAffinity(pExpr);
drh8df447f2005-11-01 15:48:24 +0000129 return (aff==SQLITE_AFF_NONE) || (aff==idx_affinity);
danielk1977e014a832004-05-17 10:48:57 +0000130}
131
danielk1977a37cdde2004-05-16 11:15:36 +0000132/*
133** Return the P1 value that should be used for a binary comparison
134** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
135** If jumpIfNull is true, then set the low byte of the returned
136** P1 value to tell the opcode to jump if either expression
137** evaluates to NULL.
138*/
danielk1977e014a832004-05-17 10:48:57 +0000139static int binaryCompareP1(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){
danielk1977bf3b7212004-05-18 10:06:24 +0000140 char aff = sqlite3ExprAffinity(pExpr2);
drhf0863fe2005-06-12 21:35:51 +0000141 return ((int)sqlite3CompareAffinity(pExpr1, aff))+(jumpIfNull?0x100:0);
danielk1977a37cdde2004-05-16 11:15:36 +0000142}
143
drha2e00042002-01-22 03:13:42 +0000144/*
danielk19770202b292004-06-09 09:55:16 +0000145** Return a pointer to the collation sequence that should be used by
146** a binary comparison operator comparing pLeft and pRight.
147**
148** If the left hand expression has a collating sequence type, then it is
149** used. Otherwise the collation sequence for the right hand expression
150** is used, or the default (BINARY) if neither expression has a collating
151** type.
152*/
danielk19777cedc8d2004-06-10 10:50:08 +0000153static CollSeq* binaryCompareCollSeq(Parse *pParse, Expr *pLeft, Expr *pRight){
154 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pLeft);
danielk19770202b292004-06-09 09:55:16 +0000155 if( !pColl ){
danielk19777cedc8d2004-06-10 10:50:08 +0000156 pColl = sqlite3ExprCollSeq(pParse, pRight);
danielk19770202b292004-06-09 09:55:16 +0000157 }
158 return pColl;
159}
160
161/*
drhbe5c89a2004-07-26 00:31:09 +0000162** Generate code for a comparison operator.
163*/
164static int codeCompare(
165 Parse *pParse, /* The parsing (and code generating) context */
166 Expr *pLeft, /* The left operand */
167 Expr *pRight, /* The right operand */
168 int opcode, /* The comparison opcode */
169 int dest, /* Jump here if true. */
170 int jumpIfNull /* If true, jump if either operand is NULL */
171){
172 int p1 = binaryCompareP1(pLeft, pRight, jumpIfNull);
173 CollSeq *p3 = binaryCompareCollSeq(pParse, pLeft, pRight);
drh94a11212004-09-25 13:12:14 +0000174 return sqlite3VdbeOp3(pParse->pVdbe, opcode, p1, dest, (void*)p3, P3_COLLSEQ);
drhbe5c89a2004-07-26 00:31:09 +0000175}
176
177/*
drha76b5df2002-02-23 02:32:10 +0000178** Construct a new expression node and return a pointer to it. Memory
179** for this node is obtained from sqliteMalloc(). The calling function
180** is responsible for making sure the node eventually gets freed.
181*/
drhe4e72072004-11-23 01:47:30 +0000182Expr *sqlite3Expr(int op, Expr *pLeft, Expr *pRight, const Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000183 Expr *pNew;
184 pNew = sqliteMalloc( sizeof(Expr) );
185 if( pNew==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000186 /* When malloc fails, delete pLeft and pRight. Expressions passed to
187 ** this function must always be allocated with sqlite3Expr() for this
188 ** reason.
189 */
190 sqlite3ExprDelete(pLeft);
191 sqlite3ExprDelete(pRight);
drha76b5df2002-02-23 02:32:10 +0000192 return 0;
193 }
194 pNew->op = op;
195 pNew->pLeft = pLeft;
196 pNew->pRight = pRight;
danielk1977a58fdfb2005-02-08 07:50:40 +0000197 pNew->iAgg = -1;
drha76b5df2002-02-23 02:32:10 +0000198 if( pToken ){
drh4b59ab52002-08-24 18:24:51 +0000199 assert( pToken->dyn==0 );
drh145716b2004-09-24 12:24:06 +0000200 pNew->span = pNew->token = *pToken;
201 }else if( pLeft && pRight ){
202 sqlite3ExprSpan(pNew, &pLeft->span, &pRight->span);
drha76b5df2002-02-23 02:32:10 +0000203 }
drha76b5df2002-02-23 02:32:10 +0000204 return pNew;
205}
206
207/*
drh4e0cff62004-11-05 05:10:28 +0000208** When doing a nested parse, you can include terms in an expression
209** that look like this: #0 #1 #2 ... These terms refer to elements
drh288d37f2005-06-22 08:48:06 +0000210** on the stack. "#0" means the top of the stack.
211** "#1" means the next down on the stack. And so forth.
drh4e0cff62004-11-05 05:10:28 +0000212**
213** This routine is called by the parser to deal with on of those terms.
214** It immediately generates code to store the value in a memory location.
215** The returns an expression that will code to extract the value from
216** that memory location as needed.
217*/
218Expr *sqlite3RegisterExpr(Parse *pParse, Token *pToken){
219 Vdbe *v = pParse->pVdbe;
220 Expr *p;
221 int depth;
drh4e0cff62004-11-05 05:10:28 +0000222 if( pParse->nested==0 ){
223 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", pToken);
224 return 0;
225 }
drhbb7ac002005-08-12 22:58:53 +0000226 if( v==0 ) return 0;
drh4e0cff62004-11-05 05:10:28 +0000227 p = sqlite3Expr(TK_REGISTER, 0, 0, pToken);
drh73c42a12004-11-20 18:13:10 +0000228 if( p==0 ){
229 return 0; /* Malloc failed */
230 }
drh4e0cff62004-11-05 05:10:28 +0000231 depth = atoi(&pToken->z[1]);
drh288d37f2005-06-22 08:48:06 +0000232 p->iTable = pParse->nMem++;
233 sqlite3VdbeAddOp(v, OP_Dup, depth, 0);
234 sqlite3VdbeAddOp(v, OP_MemStore, p->iTable, 1);
drh4e0cff62004-11-05 05:10:28 +0000235 return p;
236}
237
238/*
drh91bb0ee2004-09-01 03:06:34 +0000239** Join two expressions using an AND operator. If either expression is
240** NULL, then just return the other expression.
241*/
242Expr *sqlite3ExprAnd(Expr *pLeft, Expr *pRight){
243 if( pLeft==0 ){
244 return pRight;
245 }else if( pRight==0 ){
246 return pLeft;
247 }else{
248 return sqlite3Expr(TK_AND, pLeft, pRight, 0);
249 }
250}
251
252/*
drh6977fea2002-10-22 23:38:04 +0000253** Set the Expr.span field of the given expression to span all
drha76b5df2002-02-23 02:32:10 +0000254** text between the two given tokens.
255*/
danielk19774adee202004-05-08 08:23:19 +0000256void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
drh4efc4752004-01-16 15:55:37 +0000257 assert( pRight!=0 );
258 assert( pLeft!=0 );
drh71c697e2004-08-08 23:39:19 +0000259 if( !sqlite3_malloc_failed && pRight->z && pLeft->z ){
drhad6d9462004-09-19 02:15:24 +0000260 assert( pLeft->dyn==0 || pLeft->z[pLeft->n]==0 );
drh145716b2004-09-24 12:24:06 +0000261 if( pLeft->dyn==0 && pRight->dyn==0 ){
drh6977fea2002-10-22 23:38:04 +0000262 pExpr->span.z = pLeft->z;
drh97903fe2005-05-24 20:19:57 +0000263 pExpr->span.n = pRight->n + (pRight->z - pLeft->z);
drh4b59ab52002-08-24 18:24:51 +0000264 }else{
drh6977fea2002-10-22 23:38:04 +0000265 pExpr->span.z = 0;
drh4b59ab52002-08-24 18:24:51 +0000266 }
drha76b5df2002-02-23 02:32:10 +0000267 }
268}
269
270/*
271** Construct a new expression node for a function with multiple
272** arguments.
273*/
danielk19774adee202004-05-08 08:23:19 +0000274Expr *sqlite3ExprFunction(ExprList *pList, Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000275 Expr *pNew;
276 pNew = sqliteMalloc( sizeof(Expr) );
277 if( pNew==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000278 sqlite3ExprListDelete(pList); /* Avoid leaking memory when malloc fails */
drha76b5df2002-02-23 02:32:10 +0000279 return 0;
280 }
281 pNew->op = TK_FUNCTION;
282 pNew->pList = pList;
283 if( pToken ){
drh4b59ab52002-08-24 18:24:51 +0000284 assert( pToken->dyn==0 );
drha76b5df2002-02-23 02:32:10 +0000285 pNew->token = *pToken;
286 }else{
287 pNew->token.z = 0;
drha76b5df2002-02-23 02:32:10 +0000288 }
drh6977fea2002-10-22 23:38:04 +0000289 pNew->span = pNew->token;
drha76b5df2002-02-23 02:32:10 +0000290 return pNew;
291}
292
293/*
drhfa6bc002004-09-07 16:19:52 +0000294** Assign a variable number to an expression that encodes a wildcard
295** in the original SQL statement.
296**
297** Wildcards consisting of a single "?" are assigned the next sequential
298** variable number.
299**
300** Wildcards of the form "?nnn" are assigned the number "nnn". We make
301** sure "nnn" is not too be to avoid a denial of service attack when
302** the SQL statement comes from an external source.
303**
304** Wildcards of the form ":aaa" or "$aaa" are assigned the same number
305** as the previous instance of the same wildcard. Or if this is the first
306** instance of the wildcard, the next sequenial variable number is
307** assigned.
308*/
309void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){
310 Token *pToken;
311 if( pExpr==0 ) return;
312 pToken = &pExpr->token;
313 assert( pToken->n>=1 );
314 assert( pToken->z!=0 );
315 assert( pToken->z[0]!=0 );
316 if( pToken->n==1 ){
317 /* Wildcard of the form "?". Assign the next variable number */
318 pExpr->iTable = ++pParse->nVar;
319 }else if( pToken->z[0]=='?' ){
320 /* Wildcard of the form "?nnn". Convert "nnn" to an integer and
321 ** use it as the variable number */
322 int i;
323 pExpr->iTable = i = atoi(&pToken->z[1]);
324 if( i<1 || i>SQLITE_MAX_VARIABLE_NUMBER ){
325 sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
326 SQLITE_MAX_VARIABLE_NUMBER);
327 }
328 if( i>pParse->nVar ){
329 pParse->nVar = i;
330 }
331 }else{
332 /* Wildcards of the form ":aaa" or "$aaa". Reuse the same variable
333 ** number as the prior appearance of the same name, or if the name
334 ** has never appeared before, reuse the same variable number
335 */
336 int i, n;
337 n = pToken->n;
338 for(i=0; i<pParse->nVarExpr; i++){
339 Expr *pE;
340 if( (pE = pParse->apVarExpr[i])!=0
341 && pE->token.n==n
342 && memcmp(pE->token.z, pToken->z, n)==0 ){
343 pExpr->iTable = pE->iTable;
344 break;
345 }
346 }
347 if( i>=pParse->nVarExpr ){
348 pExpr->iTable = ++pParse->nVar;
349 if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){
350 pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10;
drh53f733c2005-09-16 02:38:09 +0000351 sqlite3ReallocOrFree((void**)&pParse->apVarExpr,
drhfa6bc002004-09-07 16:19:52 +0000352 pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0]) );
353 }
354 if( !sqlite3_malloc_failed ){
355 assert( pParse->apVarExpr!=0 );
356 pParse->apVarExpr[pParse->nVarExpr++] = pExpr;
357 }
358 }
359 }
360}
361
362/*
drha2e00042002-01-22 03:13:42 +0000363** Recursively delete an expression tree.
364*/
danielk19774adee202004-05-08 08:23:19 +0000365void sqlite3ExprDelete(Expr *p){
drha2e00042002-01-22 03:13:42 +0000366 if( p==0 ) return;
drh4efc4752004-01-16 15:55:37 +0000367 if( p->span.dyn ) sqliteFree((char*)p->span.z);
368 if( p->token.dyn ) sqliteFree((char*)p->token.z);
danielk19774adee202004-05-08 08:23:19 +0000369 sqlite3ExprDelete(p->pLeft);
370 sqlite3ExprDelete(p->pRight);
371 sqlite3ExprListDelete(p->pList);
372 sqlite3SelectDelete(p->pSelect);
drha2e00042002-01-22 03:13:42 +0000373 sqliteFree(p);
374}
375
drhd2687b72005-08-12 22:56:09 +0000376/*
377** The Expr.token field might be a string literal that is quoted.
378** If so, remove the quotation marks.
379*/
380void sqlite3DequoteExpr(Expr *p){
381 if( ExprHasAnyProperty(p, EP_Dequoted) ){
382 return;
383 }
384 ExprSetProperty(p, EP_Dequoted);
385 if( p->token.dyn==0 ){
drhd2687b72005-08-12 22:56:09 +0000386 sqlite3TokenCopy(&p->token, &p->token);
387 }
388 sqlite3Dequote((char*)p->token.z);
389}
390
drha76b5df2002-02-23 02:32:10 +0000391
392/*
drhff78bd22002-02-27 01:47:11 +0000393** The following group of routines make deep copies of expressions,
394** expression lists, ID lists, and select statements. The copies can
395** be deleted (by being passed to their respective ...Delete() routines)
396** without effecting the originals.
397**
danielk19774adee202004-05-08 08:23:19 +0000398** The expression list, ID, and source lists return by sqlite3ExprListDup(),
399** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
drhad3cab52002-05-24 02:04:32 +0000400** by subsequent calls to sqlite*ListAppend() routines.
drhff78bd22002-02-27 01:47:11 +0000401**
drhad3cab52002-05-24 02:04:32 +0000402** Any tables that the SrcList might point to are not duplicated.
drhff78bd22002-02-27 01:47:11 +0000403*/
danielk19774adee202004-05-08 08:23:19 +0000404Expr *sqlite3ExprDup(Expr *p){
drhff78bd22002-02-27 01:47:11 +0000405 Expr *pNew;
406 if( p==0 ) return 0;
drhfcb78a42003-01-18 20:11:05 +0000407 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000408 if( pNew==0 ) return 0;
drh3b167c72002-06-28 12:18:47 +0000409 memcpy(pNew, p, sizeof(*pNew));
drh6977fea2002-10-22 23:38:04 +0000410 if( p->token.z!=0 ){
drhb9ecf6f2004-11-20 20:44:13 +0000411 pNew->token.z = sqliteStrNDup(p->token.z, p->token.n);
drh4b59ab52002-08-24 18:24:51 +0000412 pNew->token.dyn = 1;
413 }else{
drh4efc4752004-01-16 15:55:37 +0000414 assert( pNew->token.z==0 );
drh4b59ab52002-08-24 18:24:51 +0000415 }
drh6977fea2002-10-22 23:38:04 +0000416 pNew->span.z = 0;
danielk19774adee202004-05-08 08:23:19 +0000417 pNew->pLeft = sqlite3ExprDup(p->pLeft);
418 pNew->pRight = sqlite3ExprDup(p->pRight);
419 pNew->pList = sqlite3ExprListDup(p->pList);
420 pNew->pSelect = sqlite3SelectDup(p->pSelect);
danielk1977aee18ef2005-03-09 12:26:50 +0000421 pNew->pTab = p->pTab;
drhff78bd22002-02-27 01:47:11 +0000422 return pNew;
423}
danielk19774adee202004-05-08 08:23:19 +0000424void sqlite3TokenCopy(Token *pTo, Token *pFrom){
drh4b59ab52002-08-24 18:24:51 +0000425 if( pTo->dyn ) sqliteFree((char*)pTo->z);
drh4b59ab52002-08-24 18:24:51 +0000426 if( pFrom->z ){
427 pTo->n = pFrom->n;
428 pTo->z = sqliteStrNDup(pFrom->z, pFrom->n);
429 pTo->dyn = 1;
430 }else{
drh4b59ab52002-08-24 18:24:51 +0000431 pTo->z = 0;
drh4b59ab52002-08-24 18:24:51 +0000432 }
433}
danielk19774adee202004-05-08 08:23:19 +0000434ExprList *sqlite3ExprListDup(ExprList *p){
drhff78bd22002-02-27 01:47:11 +0000435 ExprList *pNew;
drh145716b2004-09-24 12:24:06 +0000436 struct ExprList_item *pItem, *pOldItem;
drhff78bd22002-02-27 01:47:11 +0000437 int i;
438 if( p==0 ) return 0;
439 pNew = sqliteMalloc( sizeof(*pNew) );
440 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000441 pNew->nExpr = pNew->nAlloc = p->nExpr;
drh3e7bc9c2004-02-21 19:17:17 +0000442 pNew->a = pItem = sqliteMalloc( p->nExpr*sizeof(p->a[0]) );
danielk1977e0048402004-06-15 16:51:01 +0000443 if( pItem==0 ){
444 sqliteFree(pNew);
445 return 0;
446 }
drh145716b2004-09-24 12:24:06 +0000447 pOldItem = p->a;
448 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
drh4b59ab52002-08-24 18:24:51 +0000449 Expr *pNewExpr, *pOldExpr;
drh145716b2004-09-24 12:24:06 +0000450 pItem->pExpr = pNewExpr = sqlite3ExprDup(pOldExpr = pOldItem->pExpr);
drh6977fea2002-10-22 23:38:04 +0000451 if( pOldExpr->span.z!=0 && pNewExpr ){
452 /* Always make a copy of the span for top-level expressions in the
drh4b59ab52002-08-24 18:24:51 +0000453 ** expression list. The logic in SELECT processing that determines
454 ** the names of columns in the result set needs this information */
danielk19774adee202004-05-08 08:23:19 +0000455 sqlite3TokenCopy(&pNewExpr->span, &pOldExpr->span);
drh4b59ab52002-08-24 18:24:51 +0000456 }
drh1f3e9052002-10-31 00:09:39 +0000457 assert( pNewExpr==0 || pNewExpr->span.z!=0
danielk197724b03fd2004-05-10 10:34:34 +0000458 || pOldExpr->span.z==0 || sqlite3_malloc_failed );
drh145716b2004-09-24 12:24:06 +0000459 pItem->zName = sqliteStrDup(pOldItem->zName);
460 pItem->sortOrder = pOldItem->sortOrder;
461 pItem->isAgg = pOldItem->isAgg;
drh3e7bc9c2004-02-21 19:17:17 +0000462 pItem->done = 0;
drhff78bd22002-02-27 01:47:11 +0000463 }
464 return pNew;
465}
danielk197793758c82005-01-21 08:13:14 +0000466
467/*
468** If cursors, triggers, views and subqueries are all omitted from
469** the build, then none of the following routines, except for
470** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes
471** called with a NULL argument.
472*/
danielk19776a67fe82005-02-04 04:07:16 +0000473#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \
474 || !defined(SQLITE_OMIT_SUBQUERY)
danielk19774adee202004-05-08 08:23:19 +0000475SrcList *sqlite3SrcListDup(SrcList *p){
drhad3cab52002-05-24 02:04:32 +0000476 SrcList *pNew;
477 int i;
drh113088e2003-03-20 01:16:58 +0000478 int nByte;
drhad3cab52002-05-24 02:04:32 +0000479 if( p==0 ) return 0;
drh113088e2003-03-20 01:16:58 +0000480 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
drh4efc4752004-01-16 15:55:37 +0000481 pNew = sqliteMallocRaw( nByte );
drhad3cab52002-05-24 02:04:32 +0000482 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000483 pNew->nSrc = pNew->nAlloc = p->nSrc;
drhad3cab52002-05-24 02:04:32 +0000484 for(i=0; i<p->nSrc; i++){
drh4efc4752004-01-16 15:55:37 +0000485 struct SrcList_item *pNewItem = &pNew->a[i];
486 struct SrcList_item *pOldItem = &p->a[i];
drhed8a3bb2005-06-06 21:19:56 +0000487 Table *pTab;
drh4efc4752004-01-16 15:55:37 +0000488 pNewItem->zDatabase = sqliteStrDup(pOldItem->zDatabase);
489 pNewItem->zName = sqliteStrDup(pOldItem->zName);
490 pNewItem->zAlias = sqliteStrDup(pOldItem->zAlias);
491 pNewItem->jointype = pOldItem->jointype;
492 pNewItem->iCursor = pOldItem->iCursor;
drhed8a3bb2005-06-06 21:19:56 +0000493 pTab = pNewItem->pTab = pOldItem->pTab;
494 if( pTab ){
495 pTab->nRef++;
danielk1977a1cb1832005-02-12 08:59:55 +0000496 }
danielk19774adee202004-05-08 08:23:19 +0000497 pNewItem->pSelect = sqlite3SelectDup(pOldItem->pSelect);
498 pNewItem->pOn = sqlite3ExprDup(pOldItem->pOn);
499 pNewItem->pUsing = sqlite3IdListDup(pOldItem->pUsing);
danielk19776c18b6e2005-01-30 09:17:58 +0000500 pNewItem->colUsed = pOldItem->colUsed;
drhad3cab52002-05-24 02:04:32 +0000501 }
502 return pNew;
503}
danielk19774adee202004-05-08 08:23:19 +0000504IdList *sqlite3IdListDup(IdList *p){
drhff78bd22002-02-27 01:47:11 +0000505 IdList *pNew;
506 int i;
507 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000508 pNew = sqliteMallocRaw( sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +0000509 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000510 pNew->nId = pNew->nAlloc = p->nId;
drh4efc4752004-01-16 15:55:37 +0000511 pNew->a = sqliteMallocRaw( p->nId*sizeof(p->a[0]) );
danielk1977d5d56522005-03-16 12:15:20 +0000512 if( pNew->a==0 ){
513 sqliteFree(pNew);
514 return 0;
515 }
drhff78bd22002-02-27 01:47:11 +0000516 for(i=0; i<p->nId; i++){
drh4efc4752004-01-16 15:55:37 +0000517 struct IdList_item *pNewItem = &pNew->a[i];
518 struct IdList_item *pOldItem = &p->a[i];
519 pNewItem->zName = sqliteStrDup(pOldItem->zName);
520 pNewItem->idx = pOldItem->idx;
drhff78bd22002-02-27 01:47:11 +0000521 }
522 return pNew;
523}
danielk19774adee202004-05-08 08:23:19 +0000524Select *sqlite3SelectDup(Select *p){
drhff78bd22002-02-27 01:47:11 +0000525 Select *pNew;
526 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000527 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000528 if( pNew==0 ) return 0;
529 pNew->isDistinct = p->isDistinct;
danielk19774adee202004-05-08 08:23:19 +0000530 pNew->pEList = sqlite3ExprListDup(p->pEList);
531 pNew->pSrc = sqlite3SrcListDup(p->pSrc);
532 pNew->pWhere = sqlite3ExprDup(p->pWhere);
533 pNew->pGroupBy = sqlite3ExprListDup(p->pGroupBy);
534 pNew->pHaving = sqlite3ExprDup(p->pHaving);
535 pNew->pOrderBy = sqlite3ExprListDup(p->pOrderBy);
drhff78bd22002-02-27 01:47:11 +0000536 pNew->op = p->op;
danielk19774adee202004-05-08 08:23:19 +0000537 pNew->pPrior = sqlite3SelectDup(p->pPrior);
danielk1977a2dc3b12005-02-05 12:48:48 +0000538 pNew->pLimit = sqlite3ExprDup(p->pLimit);
539 pNew->pOffset = sqlite3ExprDup(p->pOffset);
drh7b58dae2003-07-20 01:16:46 +0000540 pNew->iLimit = -1;
541 pNew->iOffset = -1;
danielk1977a1cb1832005-02-12 08:59:55 +0000542 pNew->isResolved = p->isResolved;
543 pNew->isAgg = p->isAgg;
drh8e647b82005-09-23 21:11:53 +0000544 pNew->usesVirt = 0;
545 pNew->disallowOrderBy = 0;
drh0342b1f2005-09-01 03:07:44 +0000546 pNew->pRightmost = 0;
547 pNew->addrOpenVirt[0] = -1;
548 pNew->addrOpenVirt[1] = -1;
549 pNew->addrOpenVirt[2] = -1;
drhff78bd22002-02-27 01:47:11 +0000550 return pNew;
551}
danielk197793758c82005-01-21 08:13:14 +0000552#else
553Select *sqlite3SelectDup(Select *p){
554 assert( p==0 );
555 return 0;
556}
557#endif
drhff78bd22002-02-27 01:47:11 +0000558
559
560/*
drha76b5df2002-02-23 02:32:10 +0000561** Add a new element to the end of an expression list. If pList is
562** initially NULL, then create a new expression list.
563*/
danielk19774adee202004-05-08 08:23:19 +0000564ExprList *sqlite3ExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){
drha76b5df2002-02-23 02:32:10 +0000565 if( pList==0 ){
566 pList = sqliteMalloc( sizeof(ExprList) );
567 if( pList==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000568 goto no_mem;
drha76b5df2002-02-23 02:32:10 +0000569 }
drh4efc4752004-01-16 15:55:37 +0000570 assert( pList->nAlloc==0 );
drha76b5df2002-02-23 02:32:10 +0000571 }
drh4305d102003-07-30 12:34:12 +0000572 if( pList->nAlloc<=pList->nExpr ){
danielk1977d5d56522005-03-16 12:15:20 +0000573 struct ExprList_item *a;
574 int n = pList->nAlloc*2 + 4;
575 a = sqliteRealloc(pList->a, n*sizeof(pList->a[0]));
576 if( a==0 ){
577 goto no_mem;
drha76b5df2002-02-23 02:32:10 +0000578 }
danielk1977d5d56522005-03-16 12:15:20 +0000579 pList->a = a;
580 pList->nAlloc = n;
drha76b5df2002-02-23 02:32:10 +0000581 }
drh4efc4752004-01-16 15:55:37 +0000582 assert( pList->a!=0 );
583 if( pExpr || pName ){
584 struct ExprList_item *pItem = &pList->a[pList->nExpr++];
585 memset(pItem, 0, sizeof(*pItem));
drha99db3b2004-06-19 14:49:12 +0000586 pItem->zName = sqlite3NameFromToken(pName);
danielk1977e94ddc92005-03-21 03:53:38 +0000587 pItem->pExpr = pExpr;
drha76b5df2002-02-23 02:32:10 +0000588 }
589 return pList;
danielk1977d5d56522005-03-16 12:15:20 +0000590
591no_mem:
592 /* Avoid leaking memory if malloc has failed. */
593 sqlite3ExprDelete(pExpr);
594 sqlite3ExprListDelete(pList);
595 return 0;
drha76b5df2002-02-23 02:32:10 +0000596}
597
598/*
599** Delete an entire expression list.
600*/
danielk19774adee202004-05-08 08:23:19 +0000601void sqlite3ExprListDelete(ExprList *pList){
drha76b5df2002-02-23 02:32:10 +0000602 int i;
drhbe5c89a2004-07-26 00:31:09 +0000603 struct ExprList_item *pItem;
drha76b5df2002-02-23 02:32:10 +0000604 if( pList==0 ) return;
drh1bdd9b52004-04-23 17:04:44 +0000605 assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
606 assert( pList->nExpr<=pList->nAlloc );
drhbe5c89a2004-07-26 00:31:09 +0000607 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
608 sqlite3ExprDelete(pItem->pExpr);
609 sqliteFree(pItem->zName);
drha76b5df2002-02-23 02:32:10 +0000610 }
611 sqliteFree(pList->a);
612 sqliteFree(pList);
613}
614
615/*
drh626a8792005-01-17 22:08:19 +0000616** Walk an expression tree. Call xFunc for each node visited.
drh73b211a2005-01-18 04:00:42 +0000617**
drh626a8792005-01-17 22:08:19 +0000618** The return value from xFunc determines whether the tree walk continues.
619** 0 means continue walking the tree. 1 means do not walk children
620** of the current node but continue with siblings. 2 means abandon
621** the tree walk completely.
622**
623** The return value from this routine is 1 to abandon the tree walk
624** and 0 to continue.
drh87abf5c2005-08-25 12:45:04 +0000625**
626** NOTICE: This routine does *not* descend into subqueries.
drh626a8792005-01-17 22:08:19 +0000627*/
danielk1977a58fdfb2005-02-08 07:50:40 +0000628static int walkExprList(ExprList *, int (*)(void *, Expr*), void *);
drh626a8792005-01-17 22:08:19 +0000629static int walkExprTree(Expr *pExpr, int (*xFunc)(void*,Expr*), void *pArg){
drh626a8792005-01-17 22:08:19 +0000630 int rc;
631 if( pExpr==0 ) return 0;
632 rc = (*xFunc)(pArg, pExpr);
633 if( rc==0 ){
634 if( walkExprTree(pExpr->pLeft, xFunc, pArg) ) return 1;
635 if( walkExprTree(pExpr->pRight, xFunc, pArg) ) return 1;
danielk1977a58fdfb2005-02-08 07:50:40 +0000636 if( walkExprList(pExpr->pList, xFunc, pArg) ) return 1;
drh626a8792005-01-17 22:08:19 +0000637 }
638 return rc>1;
639}
640
641/*
danielk1977a58fdfb2005-02-08 07:50:40 +0000642** Call walkExprTree() for every expression in list p.
643*/
644static int walkExprList(ExprList *p, int (*xFunc)(void *, Expr*), void *pArg){
645 int i;
646 struct ExprList_item *pItem;
647 if( !p ) return 0;
648 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
649 if( walkExprTree(pItem->pExpr, xFunc, pArg) ) return 1;
650 }
651 return 0;
652}
653
654/*
655** Call walkExprTree() for every expression in Select p, not including
656** expressions that are part of sub-selects in any FROM clause or the LIMIT
657** or OFFSET expressions..
658*/
659static int walkSelectExpr(Select *p, int (*xFunc)(void *, Expr*), void *pArg){
660 walkExprList(p->pEList, xFunc, pArg);
661 walkExprTree(p->pWhere, xFunc, pArg);
662 walkExprList(p->pGroupBy, xFunc, pArg);
663 walkExprTree(p->pHaving, xFunc, pArg);
664 walkExprList(p->pOrderBy, xFunc, pArg);
665 return 0;
666}
667
668
669/*
drh626a8792005-01-17 22:08:19 +0000670** This routine is designed as an xFunc for walkExprTree().
671**
672** pArg is really a pointer to an integer. If we can tell by looking
drh73b211a2005-01-18 04:00:42 +0000673** at pExpr that the expression that contains pExpr is not a constant
674** expression, then set *pArg to 0 and return 2 to abandon the tree walk.
675** If pExpr does does not disqualify the expression from being a constant
676** then do nothing.
677**
678** After walking the whole tree, if no nodes are found that disqualify
679** the expression as constant, then we assume the whole expression
680** is constant. See sqlite3ExprIsConstant() for additional information.
drh626a8792005-01-17 22:08:19 +0000681*/
682static int exprNodeIsConstant(void *pArg, Expr *pExpr){
683 switch( pExpr->op ){
drheb55bd22005-06-30 17:04:21 +0000684 /* Consider functions to be constant if all their arguments are constant
685 ** and *pArg==2 */
686 case TK_FUNCTION:
687 if( *((int*)pArg)==2 ) return 0;
688 /* Fall through */
drh626a8792005-01-17 22:08:19 +0000689 case TK_ID:
690 case TK_COLUMN:
691 case TK_DOT:
692 case TK_AGG_FUNCTION:
drh13449892005-09-07 21:22:45 +0000693 case TK_AGG_COLUMN:
drhfe2093d2005-01-20 22:48:47 +0000694#ifndef SQLITE_OMIT_SUBQUERY
695 case TK_SELECT:
696 case TK_EXISTS:
697#endif
drh626a8792005-01-17 22:08:19 +0000698 *((int*)pArg) = 0;
699 return 2;
drh87abf5c2005-08-25 12:45:04 +0000700 case TK_IN:
701 if( pExpr->pSelect ){
702 *((int*)pArg) = 0;
703 return 2;
704 }
drh626a8792005-01-17 22:08:19 +0000705 default:
706 return 0;
707 }
708}
709
710/*
drhfef52082000-06-06 01:50:43 +0000711** Walk an expression tree. Return 1 if the expression is constant
drheb55bd22005-06-30 17:04:21 +0000712** and 0 if it involves variables or function calls.
drh23989372002-05-21 13:43:04 +0000713**
714** For the purposes of this function, a double-quoted string (ex: "abc")
715** is considered a variable but a single-quoted string (ex: 'abc') is
716** a constant.
drhfef52082000-06-06 01:50:43 +0000717*/
danielk19774adee202004-05-08 08:23:19 +0000718int sqlite3ExprIsConstant(Expr *p){
drh626a8792005-01-17 22:08:19 +0000719 int isConst = 1;
720 walkExprTree(p, exprNodeIsConstant, &isConst);
721 return isConst;
drhfef52082000-06-06 01:50:43 +0000722}
723
724/*
drheb55bd22005-06-30 17:04:21 +0000725** Walk an expression tree. Return 1 if the expression is constant
726** or a function call with constant arguments. Return and 0 if there
727** are any variables.
728**
729** For the purposes of this function, a double-quoted string (ex: "abc")
730** is considered a variable but a single-quoted string (ex: 'abc') is
731** a constant.
732*/
733int sqlite3ExprIsConstantOrFunction(Expr *p){
734 int isConst = 2;
735 walkExprTree(p, exprNodeIsConstant, &isConst);
736 return isConst!=0;
737}
738
739/*
drh73b211a2005-01-18 04:00:42 +0000740** If the expression p codes a constant integer that is small enough
drh202b2df2004-01-06 01:13:46 +0000741** to fit in a 32-bit integer, return 1 and put the value of the integer
742** in *pValue. If the expression is not an integer or if it is too big
743** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
drhe4de1fe2002-06-02 16:09:01 +0000744*/
danielk19774adee202004-05-08 08:23:19 +0000745int sqlite3ExprIsInteger(Expr *p, int *pValue){
drhe4de1fe2002-06-02 16:09:01 +0000746 switch( p->op ){
747 case TK_INTEGER: {
drhfec19aa2004-05-19 20:41:03 +0000748 if( sqlite3GetInt32(p->token.z, pValue) ){
drh202b2df2004-01-06 01:13:46 +0000749 return 1;
750 }
751 break;
drhe4de1fe2002-06-02 16:09:01 +0000752 }
drh4b59ab52002-08-24 18:24:51 +0000753 case TK_UPLUS: {
danielk19774adee202004-05-08 08:23:19 +0000754 return sqlite3ExprIsInteger(p->pLeft, pValue);
drh4b59ab52002-08-24 18:24:51 +0000755 }
drhe4de1fe2002-06-02 16:09:01 +0000756 case TK_UMINUS: {
757 int v;
danielk19774adee202004-05-08 08:23:19 +0000758 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
drhe4de1fe2002-06-02 16:09:01 +0000759 *pValue = -v;
760 return 1;
761 }
762 break;
763 }
764 default: break;
765 }
766 return 0;
767}
768
769/*
drhc4a3c772001-04-04 11:48:57 +0000770** Return TRUE if the given string is a row-id column name.
771*/
danielk19774adee202004-05-08 08:23:19 +0000772int sqlite3IsRowid(const char *z){
773 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
774 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
775 if( sqlite3StrICmp(z, "OID")==0 ) return 1;
drhc4a3c772001-04-04 11:48:57 +0000776 return 0;
777}
778
779/*
drh8141f612004-01-25 22:44:58 +0000780** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
781** that name in the set of source tables in pSrcList and make the pExpr
782** expression node refer back to that source column. The following changes
783** are made to pExpr:
784**
785** pExpr->iDb Set the index in db->aDb[] of the database holding
786** the table.
787** pExpr->iTable Set to the cursor number for the table obtained
788** from pSrcList.
789** pExpr->iColumn Set to the column number within the table.
drh8141f612004-01-25 22:44:58 +0000790** pExpr->op Set to TK_COLUMN.
791** pExpr->pLeft Any expression this points to is deleted
792** pExpr->pRight Any expression this points to is deleted.
793**
794** The pDbToken is the name of the database (the "X"). This value may be
795** NULL meaning that name is of the form Y.Z or Z. Any available database
796** can be used. The pTableToken is the name of the table (the "Y"). This
797** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it
798** means that the form of the name is Z and that columns from any table
799** can be used.
800**
801** If the name cannot be resolved unambiguously, leave an error message
802** in pParse and return non-zero. Return zero on success.
803*/
804static int lookupName(
805 Parse *pParse, /* The parsing context */
806 Token *pDbToken, /* Name of the database containing table, or NULL */
807 Token *pTableToken, /* Name of table containing column, or NULL */
808 Token *pColumnToken, /* Name of the column. */
drh626a8792005-01-17 22:08:19 +0000809 NameContext *pNC, /* The name context used to resolve the name */
drh8141f612004-01-25 22:44:58 +0000810 Expr *pExpr /* Make this EXPR node point to the selected column */
811){
812 char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */
813 char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */
814 char *zCol = 0; /* Name of the column. The "Z" */
815 int i, j; /* Loop counters */
816 int cnt = 0; /* Number of matching column names */
817 int cntTab = 0; /* Number of matching table names */
drh626a8792005-01-17 22:08:19 +0000818 sqlite3 *db = pParse->db; /* The database */
drh51669862004-12-18 18:40:26 +0000819 struct SrcList_item *pItem; /* Use for looping over pSrcList items */
820 struct SrcList_item *pMatch = 0; /* The matching pSrcList item */
drh73b211a2005-01-18 04:00:42 +0000821 NameContext *pTopNC = pNC; /* First namecontext in the list */
drh8141f612004-01-25 22:44:58 +0000822
823 assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
drha99db3b2004-06-19 14:49:12 +0000824 zDb = sqlite3NameFromToken(pDbToken);
825 zTab = sqlite3NameFromToken(pTableToken);
826 zCol = sqlite3NameFromToken(pColumnToken);
danielk197724b03fd2004-05-10 10:34:34 +0000827 if( sqlite3_malloc_failed ){
danielk1977d5d56522005-03-16 12:15:20 +0000828 goto lookupname_end;
drh8141f612004-01-25 22:44:58 +0000829 }
drh8141f612004-01-25 22:44:58 +0000830
831 pExpr->iTable = -1;
drh626a8792005-01-17 22:08:19 +0000832 while( pNC && cnt==0 ){
833 SrcList *pSrcList = pNC->pSrcList;
834 ExprList *pEList = pNC->pEList;
drh8141f612004-01-25 22:44:58 +0000835
drh626a8792005-01-17 22:08:19 +0000836 /* assert( zTab==0 || pEList==0 ); */
danielk1977b3bce662005-01-29 08:32:43 +0000837 if( pSrcList ){
838 for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
839 Table *pTab = pItem->pTab;
840 Column *pCol;
841
842 if( pTab==0 ) continue;
843 assert( pTab->nCol>0 );
844 if( zTab ){
845 if( pItem->zAlias ){
846 char *zTabName = pItem->zAlias;
847 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
848 }else{
849 char *zTabName = pTab->zName;
850 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
851 if( zDb!=0 && sqlite3StrICmp(db->aDb[pTab->iDb].zName, zDb)!=0 ){
852 continue;
853 }
drh626a8792005-01-17 22:08:19 +0000854 }
drh8141f612004-01-25 22:44:58 +0000855 }
danielk1977b3bce662005-01-29 08:32:43 +0000856 if( 0==(cntTab++) ){
drh626a8792005-01-17 22:08:19 +0000857 pExpr->iTable = pItem->iCursor;
drh626a8792005-01-17 22:08:19 +0000858 pExpr->iDb = pTab->iDb;
danielk1977b3bce662005-01-29 08:32:43 +0000859 pMatch = pItem;
860 }
861 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
862 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
drh873fac02005-06-06 17:11:46 +0000863 IdList *pUsing;
danielk1977b3bce662005-01-29 08:32:43 +0000864 cnt++;
865 pExpr->iTable = pItem->iCursor;
866 pMatch = pItem;
867 pExpr->iDb = pTab->iDb;
868 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
869 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
870 pExpr->affinity = pTab->aCol[j].affinity;
871 pExpr->pColl = pTab->aCol[j].pColl;
drh355ef362005-06-06 16:59:24 +0000872 if( pItem->jointype & JT_NATURAL ){
873 /* If this match occurred in the left table of a natural join,
874 ** then skip the right table to avoid a duplicate match */
875 pItem++;
876 i++;
877 }
drh873fac02005-06-06 17:11:46 +0000878 if( (pUsing = pItem->pUsing)!=0 ){
879 /* If this match occurs on a column that is in the USING clause
880 ** of a join, skip the search of the right table of the join
881 ** to avoid a duplicate match there. */
882 int k;
883 for(k=0; k<pUsing->nId; k++){
884 if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ){
885 pItem++;
886 i++;
887 break;
888 }
889 }
890 }
danielk1977b3bce662005-01-29 08:32:43 +0000891 break;
892 }
drh8141f612004-01-25 22:44:58 +0000893 }
894 }
895 }
drh626a8792005-01-17 22:08:19 +0000896
897#ifndef SQLITE_OMIT_TRIGGER
898 /* If we have not already resolved the name, then maybe
899 ** it is a new.* or old.* trigger argument reference
900 */
901 if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
902 TriggerStack *pTriggerStack = pParse->trigStack;
903 Table *pTab = 0;
904 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){
905 pExpr->iTable = pTriggerStack->newIdx;
906 assert( pTriggerStack->pTab );
907 pTab = pTriggerStack->pTab;
908 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab)==0 ){
909 pExpr->iTable = pTriggerStack->oldIdx;
910 assert( pTriggerStack->pTab );
911 pTab = pTriggerStack->pTab;
912 }
913
914 if( pTab ){
915 int j;
916 Column *pCol = pTab->aCol;
917
918 pExpr->iDb = pTab->iDb;
919 cntTab++;
920 for(j=0; j < pTab->nCol; j++, pCol++) {
921 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
922 cnt++;
923 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
924 pExpr->affinity = pTab->aCol[j].affinity;
925 pExpr->pColl = pTab->aCol[j].pColl;
danielk1977aee18ef2005-03-09 12:26:50 +0000926 pExpr->pTab = pTab;
drh626a8792005-01-17 22:08:19 +0000927 break;
928 }
929 }
930 }
931 }
drhb7f91642004-10-31 02:22:47 +0000932#endif /* !defined(SQLITE_OMIT_TRIGGER) */
drh8141f612004-01-25 22:44:58 +0000933
drh626a8792005-01-17 22:08:19 +0000934 /*
935 ** Perhaps the name is a reference to the ROWID
936 */
937 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
938 cnt = 1;
939 pExpr->iColumn = -1;
drh8df447f2005-11-01 15:48:24 +0000940 pExpr->affinity = SQLITE_AFF_NUMERIC;
drh626a8792005-01-17 22:08:19 +0000941 }
drh8141f612004-01-25 22:44:58 +0000942
drh626a8792005-01-17 22:08:19 +0000943 /*
944 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
945 ** might refer to an result-set alias. This happens, for example, when
946 ** we are resolving names in the WHERE clause of the following command:
947 **
948 ** SELECT a+b AS x FROM table WHERE x<10;
949 **
950 ** In cases like this, replace pExpr with a copy of the expression that
951 ** forms the result set entry ("a+b" in the example) and return immediately.
952 ** Note that the expression in the result set should have already been
953 ** resolved by the time the WHERE clause is resolved.
954 */
drh79d5f632005-01-18 17:20:10 +0000955 if( cnt==0 && pEList!=0 && zTab==0 ){
drh626a8792005-01-17 22:08:19 +0000956 for(j=0; j<pEList->nExpr; j++){
957 char *zAs = pEList->a[j].zName;
958 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
959 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
960 pExpr->op = TK_AS;
961 pExpr->iColumn = j;
962 pExpr->pLeft = sqlite3ExprDup(pEList->a[j].pExpr);
drh15ccce12005-05-23 15:06:39 +0000963 cnt = 1;
drh626a8792005-01-17 22:08:19 +0000964 assert( zTab==0 && zDb==0 );
drh15ccce12005-05-23 15:06:39 +0000965 goto lookupname_end_2;
drh626a8792005-01-17 22:08:19 +0000966 }
967 }
968 }
969
970 /* Advance to the next name context. The loop will exit when either
971 ** we have a match (cnt>0) or when we run out of name contexts.
972 */
973 if( cnt==0 ){
974 pNC = pNC->pNext;
975 }
drh8141f612004-01-25 22:44:58 +0000976 }
977
978 /*
979 ** If X and Y are NULL (in other words if only the column name Z is
980 ** supplied) and the value of Z is enclosed in double-quotes, then
981 ** Z is a string literal if it doesn't match any column names. In that
982 ** case, we need to return right away and not make any changes to
983 ** pExpr.
drh15ccce12005-05-23 15:06:39 +0000984 **
985 ** Because no reference was made to outer contexts, the pNC->nRef
986 ** fields are not changed in any context.
drh8141f612004-01-25 22:44:58 +0000987 */
988 if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
989 sqliteFree(zCol);
990 return 0;
991 }
992
993 /*
994 ** cnt==0 means there was not match. cnt>1 means there were two or
995 ** more matches. Either way, we have an error.
996 */
997 if( cnt!=1 ){
998 char *z = 0;
999 char *zErr;
1000 zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s";
1001 if( zDb ){
danielk19774adee202004-05-08 08:23:19 +00001002 sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, 0);
drh8141f612004-01-25 22:44:58 +00001003 }else if( zTab ){
danielk19774adee202004-05-08 08:23:19 +00001004 sqlite3SetString(&z, zTab, ".", zCol, 0);
drh8141f612004-01-25 22:44:58 +00001005 }else{
1006 z = sqliteStrDup(zCol);
1007 }
danielk19774adee202004-05-08 08:23:19 +00001008 sqlite3ErrorMsg(pParse, zErr, z);
drh8141f612004-01-25 22:44:58 +00001009 sqliteFree(z);
drh73b211a2005-01-18 04:00:42 +00001010 pTopNC->nErr++;
drh8141f612004-01-25 22:44:58 +00001011 }
1012
drh51669862004-12-18 18:40:26 +00001013 /* If a column from a table in pSrcList is referenced, then record
1014 ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes
1015 ** bit 0 to be set. Column 1 sets bit 1. And so forth. If the
1016 ** column number is greater than the number of bits in the bitmask
1017 ** then set the high-order bit of the bitmask.
1018 */
1019 if( pExpr->iColumn>=0 && pMatch!=0 ){
1020 int n = pExpr->iColumn;
1021 if( n>=sizeof(Bitmask)*8 ){
1022 n = sizeof(Bitmask)*8-1;
1023 }
1024 assert( pMatch->iCursor==pExpr->iTable );
1025 pMatch->colUsed |= 1<<n;
1026 }
1027
danielk1977d5d56522005-03-16 12:15:20 +00001028lookupname_end:
drh8141f612004-01-25 22:44:58 +00001029 /* Clean up and return
1030 */
1031 sqliteFree(zDb);
1032 sqliteFree(zTab);
danielk19774adee202004-05-08 08:23:19 +00001033 sqlite3ExprDelete(pExpr->pLeft);
drh8141f612004-01-25 22:44:58 +00001034 pExpr->pLeft = 0;
danielk19774adee202004-05-08 08:23:19 +00001035 sqlite3ExprDelete(pExpr->pRight);
drh8141f612004-01-25 22:44:58 +00001036 pExpr->pRight = 0;
1037 pExpr->op = TK_COLUMN;
drh15ccce12005-05-23 15:06:39 +00001038lookupname_end_2:
1039 sqliteFree(zCol);
drh626a8792005-01-17 22:08:19 +00001040 if( cnt==1 ){
danielk1977b3bce662005-01-29 08:32:43 +00001041 assert( pNC!=0 );
drh626a8792005-01-17 22:08:19 +00001042 sqlite3AuthRead(pParse, pExpr, pNC->pSrcList);
danielk1977aee18ef2005-03-09 12:26:50 +00001043 if( pMatch && !pMatch->pSelect ){
1044 pExpr->pTab = pMatch->pTab;
1045 }
drh15ccce12005-05-23 15:06:39 +00001046 /* Increment the nRef value on all name contexts from TopNC up to
1047 ** the point where the name matched. */
1048 for(;;){
1049 assert( pTopNC!=0 );
1050 pTopNC->nRef++;
1051 if( pTopNC==pNC ) break;
1052 pTopNC = pTopNC->pNext;
1053 }
1054 return 0;
1055 } else {
1056 return 1;
drh626a8792005-01-17 22:08:19 +00001057 }
drh8141f612004-01-25 22:44:58 +00001058}
1059
1060/*
drh626a8792005-01-17 22:08:19 +00001061** This routine is designed as an xFunc for walkExprTree().
1062**
drh73b211a2005-01-18 04:00:42 +00001063** Resolve symbolic names into TK_COLUMN operators for the current
drh626a8792005-01-17 22:08:19 +00001064** node in the expression tree. Return 0 to continue the search down
drh73b211a2005-01-18 04:00:42 +00001065** the tree or 2 to abort the tree walk.
1066**
1067** This routine also does error checking and name resolution for
1068** function names. The operator for aggregate functions is changed
1069** to TK_AGG_FUNCTION.
drh626a8792005-01-17 22:08:19 +00001070*/
1071static int nameResolverStep(void *pArg, Expr *pExpr){
1072 NameContext *pNC = (NameContext*)pArg;
1073 SrcList *pSrcList;
1074 Parse *pParse;
drh626a8792005-01-17 22:08:19 +00001075
danielk1977b3bce662005-01-29 08:32:43 +00001076 if( pExpr==0 ) return 1;
drh626a8792005-01-17 22:08:19 +00001077 assert( pNC!=0 );
1078 pSrcList = pNC->pSrcList;
1079 pParse = pNC->pParse;
danielk1977b3bce662005-01-29 08:32:43 +00001080
drh626a8792005-01-17 22:08:19 +00001081 if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return 1;
1082 ExprSetProperty(pExpr, EP_Resolved);
1083#ifndef NDEBUG
1084 if( pSrcList ){
danielk1977940fac92005-01-23 22:41:37 +00001085 int i;
drh626a8792005-01-17 22:08:19 +00001086 for(i=0; i<pSrcList->nSrc; i++){
1087 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
1088 }
1089 }
1090#endif
1091 switch( pExpr->op ){
1092 /* Double-quoted strings (ex: "abc") are used as identifiers if
1093 ** possible. Otherwise they remain as strings. Single-quoted
1094 ** strings (ex: 'abc') are always string literals.
1095 */
1096 case TK_STRING: {
1097 if( pExpr->token.z[0]=='\'' ) break;
1098 /* Fall thru into the TK_ID case if this is a double-quoted string */
1099 }
1100 /* A lone identifier is the name of a column.
1101 */
1102 case TK_ID: {
drh626a8792005-01-17 22:08:19 +00001103 lookupName(pParse, 0, 0, &pExpr->token, pNC, pExpr);
1104 return 1;
1105 }
1106
1107 /* A table name and column name: ID.ID
1108 ** Or a database, table and column: ID.ID.ID
1109 */
1110 case TK_DOT: {
1111 Token *pColumn;
1112 Token *pTable;
1113 Token *pDb;
1114 Expr *pRight;
1115
danielk1977b3bce662005-01-29 08:32:43 +00001116 /* if( pSrcList==0 ) break; */
drh626a8792005-01-17 22:08:19 +00001117 pRight = pExpr->pRight;
1118 if( pRight->op==TK_ID ){
1119 pDb = 0;
1120 pTable = &pExpr->pLeft->token;
1121 pColumn = &pRight->token;
1122 }else{
1123 assert( pRight->op==TK_DOT );
1124 pDb = &pExpr->pLeft->token;
1125 pTable = &pRight->pLeft->token;
1126 pColumn = &pRight->pRight->token;
1127 }
1128 lookupName(pParse, pDb, pTable, pColumn, pNC, pExpr);
1129 return 1;
1130 }
1131
1132 /* Resolve function names
1133 */
drhb71090f2005-05-23 17:26:51 +00001134 case TK_CONST_FUNC:
drh626a8792005-01-17 22:08:19 +00001135 case TK_FUNCTION: {
1136 ExprList *pList = pExpr->pList; /* The argument list */
1137 int n = pList ? pList->nExpr : 0; /* Number of arguments */
1138 int no_such_func = 0; /* True if no such function exists */
1139 int wrong_num_args = 0; /* True if wrong number of arguments */
1140 int is_agg = 0; /* True if is an aggregate function */
1141 int i;
1142 int nId; /* Number of characters in function name */
1143 const char *zId; /* The function name. */
drh73b211a2005-01-18 04:00:42 +00001144 FuncDef *pDef; /* Information about the function */
1145 int enc = pParse->db->enc; /* The database encoding */
drh626a8792005-01-17 22:08:19 +00001146
drhb71090f2005-05-23 17:26:51 +00001147 zId = pExpr->token.z;
1148 nId = pExpr->token.n;
drh626a8792005-01-17 22:08:19 +00001149 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
1150 if( pDef==0 ){
1151 pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
1152 if( pDef==0 ){
1153 no_such_func = 1;
1154 }else{
1155 wrong_num_args = 1;
1156 }
1157 }else{
1158 is_agg = pDef->xFunc==0;
1159 }
1160 if( is_agg && !pNC->allowAgg ){
1161 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
1162 pNC->nErr++;
1163 is_agg = 0;
1164 }else if( no_such_func ){
1165 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
1166 pNC->nErr++;
1167 }else if( wrong_num_args ){
1168 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
1169 nId, zId);
1170 pNC->nErr++;
1171 }
1172 if( is_agg ){
1173 pExpr->op = TK_AGG_FUNCTION;
1174 pNC->hasAgg = 1;
1175 }
drh73b211a2005-01-18 04:00:42 +00001176 if( is_agg ) pNC->allowAgg = 0;
drh626a8792005-01-17 22:08:19 +00001177 for(i=0; pNC->nErr==0 && i<n; i++){
drh73b211a2005-01-18 04:00:42 +00001178 walkExprTree(pList->a[i].pExpr, nameResolverStep, pNC);
drh626a8792005-01-17 22:08:19 +00001179 }
drh73b211a2005-01-18 04:00:42 +00001180 if( is_agg ) pNC->allowAgg = 1;
drh626a8792005-01-17 22:08:19 +00001181 /* FIX ME: Compute pExpr->affinity based on the expected return
1182 ** type of the function
1183 */
1184 return is_agg;
1185 }
danielk1977b3bce662005-01-29 08:32:43 +00001186#ifndef SQLITE_OMIT_SUBQUERY
1187 case TK_SELECT:
1188 case TK_EXISTS:
1189#endif
1190 case TK_IN: {
1191 if( pExpr->pSelect ){
1192 int nRef = pNC->nRef;
1193 sqlite3SelectResolve(pParse, pExpr->pSelect, pNC);
1194 assert( pNC->nRef>=nRef );
1195 if( nRef!=pNC->nRef ){
1196 ExprSetProperty(pExpr, EP_VarSelect);
1197 }
1198 }
1199 }
drh626a8792005-01-17 22:08:19 +00001200 }
1201 return 0;
1202}
1203
1204/*
drhcce7d172000-05-31 15:34:51 +00001205** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +00001206** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +00001207** index to the table in the table list and a column offset. The
1208** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
1209** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +00001210** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +00001211** VDBE cursor number for a cursor that is pointing into the referenced
1212** table. The Expr.iColumn value is changed to the index of the column
1213** of the referenced table. The Expr.iColumn value for the special
1214** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
1215** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +00001216**
drh626a8792005-01-17 22:08:19 +00001217** Also resolve function names and check the functions for proper
1218** usage. Make sure all function names are recognized and all functions
1219** have the correct number of arguments. Leave an error message
1220** in pParse->zErrMsg if anything is amiss. Return the number of errors.
1221**
drh73b211a2005-01-18 04:00:42 +00001222** If the expression contains aggregate functions then set the EP_Agg
1223** property on the expression.
drh626a8792005-01-17 22:08:19 +00001224*/
1225int sqlite3ExprResolveNames(
danielk1977b3bce662005-01-29 08:32:43 +00001226 NameContext *pNC, /* Namespace to resolve expressions in. */
1227 Expr *pExpr /* The expression to be analyzed. */
drh626a8792005-01-17 22:08:19 +00001228){
drh13449892005-09-07 21:22:45 +00001229 int savedHasAgg;
drh73b211a2005-01-18 04:00:42 +00001230 if( pExpr==0 ) return 0;
drh13449892005-09-07 21:22:45 +00001231 savedHasAgg = pNC->hasAgg;
1232 pNC->hasAgg = 0;
danielk1977b3bce662005-01-29 08:32:43 +00001233 walkExprTree(pExpr, nameResolverStep, pNC);
1234 if( pNC->nErr>0 ){
drh73b211a2005-01-18 04:00:42 +00001235 ExprSetProperty(pExpr, EP_Error);
drh73b211a2005-01-18 04:00:42 +00001236 }
drh13449892005-09-07 21:22:45 +00001237 if( pNC->hasAgg ){
1238 ExprSetProperty(pExpr, EP_Agg);
1239 }else if( savedHasAgg ){
1240 pNC->hasAgg = 1;
1241 }
drh73b211a2005-01-18 04:00:42 +00001242 return ExprHasProperty(pExpr, EP_Error);
drh626a8792005-01-17 22:08:19 +00001243}
1244
drh1398ad32005-01-19 23:24:50 +00001245/*
1246** A pointer instance of this structure is used to pass information
1247** through walkExprTree into codeSubqueryStep().
1248*/
1249typedef struct QueryCoder QueryCoder;
1250struct QueryCoder {
1251 Parse *pParse; /* The parsing context */
1252 NameContext *pNC; /* Namespace of first enclosing query */
1253};
1254
drh626a8792005-01-17 22:08:19 +00001255
1256/*
1257** Generate code for subqueries and IN operators.
1258**
drh73b211a2005-01-18 04:00:42 +00001259** IN operators comes in two forms:
drhfef52082000-06-06 01:50:43 +00001260**
1261** expr IN (exprlist)
1262** and
1263** expr IN (SELECT ...)
1264**
1265** The first form is handled by creating a set holding the list
1266** of allowed values. The second form causes the SELECT to generate
1267** a temporary table.
drhcce7d172000-05-31 15:34:51 +00001268*/
drh51522cd2005-01-20 13:36:19 +00001269#ifndef SQLITE_OMIT_SUBQUERY
danielk1977b3bce662005-01-29 08:32:43 +00001270void sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){
drh57dbd7b2005-07-08 18:25:26 +00001271 int testAddr = 0; /* One-time test address */
danielk1977b3bce662005-01-29 08:32:43 +00001272 Vdbe *v = sqlite3GetVdbe(pParse);
1273 if( v==0 ) return;
1274
drh57dbd7b2005-07-08 18:25:26 +00001275 /* This code must be run in its entirety every time it is encountered
1276 ** if any of the following is true:
1277 **
1278 ** * The right-hand side is a correlated subquery
1279 ** * The right-hand side is an expression list containing variables
1280 ** * We are inside a trigger
1281 **
1282 ** If all of the above are false, then we can run this code just once
1283 ** save the results, and reuse the same result on subsequent invocations.
danielk1977b3bce662005-01-29 08:32:43 +00001284 */
1285 if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->trigStack ){
1286 int mem = pParse->nMem++;
1287 sqlite3VdbeAddOp(v, OP_MemLoad, mem, 0);
drh57dbd7b2005-07-08 18:25:26 +00001288 testAddr = sqlite3VdbeAddOp(v, OP_If, 0, 0);
drh53f733c2005-09-16 02:38:09 +00001289 assert( testAddr>0 || sqlite3_malloc_failed );
drhd654be82005-09-20 17:42:23 +00001290 sqlite3VdbeAddOp(v, OP_MemInt, 1, mem);
danielk1977b3bce662005-01-29 08:32:43 +00001291 }
1292
drhcce7d172000-05-31 15:34:51 +00001293 switch( pExpr->op ){
drhfef52082000-06-06 01:50:43 +00001294 case TK_IN: {
danielk1977e014a832004-05-17 10:48:57 +00001295 char affinity;
drhd3d39e92004-05-20 22:16:29 +00001296 KeyInfo keyInfo;
drh9170dd72005-07-08 17:13:46 +00001297 int addr; /* Address of OP_OpenVirtual instruction */
drhd3d39e92004-05-20 22:16:29 +00001298
danielk1977bf3b7212004-05-18 10:06:24 +00001299 affinity = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +00001300
1301 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
drh57dbd7b2005-07-08 18:25:26 +00001302 ** expression it is handled the same way. A virtual table is
danielk1977e014a832004-05-17 10:48:57 +00001303 ** filled with single-field index keys representing the results
1304 ** from the SELECT or the <exprlist>.
1305 **
1306 ** If the 'x' expression is a column value, or the SELECT...
1307 ** statement returns a column value, then the affinity of that
1308 ** column is used to build the index keys. If both 'x' and the
1309 ** SELECT... statement are columns, then numeric affinity is used
1310 ** if either column has NUMERIC or INTEGER affinity. If neither
1311 ** 'x' nor the SELECT... statement are columns, then numeric affinity
1312 ** is used.
1313 */
1314 pExpr->iTable = pParse->nTab++;
drh9170dd72005-07-08 17:13:46 +00001315 addr = sqlite3VdbeAddOp(v, OP_OpenVirtual, pExpr->iTable, 0);
drhd3d39e92004-05-20 22:16:29 +00001316 memset(&keyInfo, 0, sizeof(keyInfo));
1317 keyInfo.nField = 1;
drhf3218fe2004-05-28 08:21:02 +00001318 sqlite3VdbeAddOp(v, OP_SetNumColumns, pExpr->iTable, 1);
danielk1977e014a832004-05-17 10:48:57 +00001319
drhfef52082000-06-06 01:50:43 +00001320 if( pExpr->pSelect ){
1321 /* Case 1: expr IN (SELECT ...)
1322 **
danielk1977e014a832004-05-17 10:48:57 +00001323 ** Generate code to write the results of the select into the temporary
1324 ** table allocated and opened above.
drhfef52082000-06-06 01:50:43 +00001325 */
danielk1977e014a832004-05-17 10:48:57 +00001326 int iParm = pExpr->iTable + (((int)affinity)<<16);
drhbe5c89a2004-07-26 00:31:09 +00001327 ExprList *pEList;
danielk1977e014a832004-05-17 10:48:57 +00001328 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
danielk1977b3bce662005-01-29 08:32:43 +00001329 sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001330 pEList = pExpr->pSelect->pEList;
1331 if( pEList && pEList->nExpr>0 ){
danielk19777cedc8d2004-06-10 10:50:08 +00001332 keyInfo.aColl[0] = binaryCompareCollSeq(pParse, pExpr->pLeft,
drhbe5c89a2004-07-26 00:31:09 +00001333 pEList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001334 }
drhfef52082000-06-06 01:50:43 +00001335 }else if( pExpr->pList ){
1336 /* Case 2: expr IN (exprlist)
1337 **
danielk1977e014a832004-05-17 10:48:57 +00001338 ** For each expression, build an index key from the evaluation and
1339 ** store it in the temporary table. If <expr> is a column, then use
1340 ** that columns affinity when building index keys. If <expr> is not
1341 ** a column, use numeric affinity.
drhfef52082000-06-06 01:50:43 +00001342 */
danielk1977e014a832004-05-17 10:48:57 +00001343 int i;
drh57dbd7b2005-07-08 18:25:26 +00001344 ExprList *pList = pExpr->pList;
1345 struct ExprList_item *pItem;
1346
danielk1977e014a832004-05-17 10:48:57 +00001347 if( !affinity ){
1348 affinity = SQLITE_AFF_NUMERIC;
1349 }
danielk19770202b292004-06-09 09:55:16 +00001350 keyInfo.aColl[0] = pExpr->pLeft->pColl;
danielk1977e014a832004-05-17 10:48:57 +00001351
1352 /* Loop through each expression in <exprlist>. */
drh57dbd7b2005-07-08 18:25:26 +00001353 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
1354 Expr *pE2 = pItem->pExpr;
danielk1977e014a832004-05-17 10:48:57 +00001355
drh57dbd7b2005-07-08 18:25:26 +00001356 /* If the expression is not constant then we will need to
1357 ** disable the test that was generated above that makes sure
1358 ** this code only executes once. Because for a non-constant
1359 ** expression we need to rerun this code each time.
1360 */
drh6c30be82005-07-29 15:10:17 +00001361 if( testAddr>0 && !sqlite3ExprIsConstant(pE2) ){
drh57dbd7b2005-07-08 18:25:26 +00001362 VdbeOp *aOp = sqlite3VdbeGetOp(v, testAddr-1);
1363 int i;
drhd654be82005-09-20 17:42:23 +00001364 for(i=0; i<3; i++){
drh57dbd7b2005-07-08 18:25:26 +00001365 aOp[i].opcode = OP_Noop;
1366 }
1367 testAddr = 0;
drh4794b982000-06-06 13:54:14 +00001368 }
danielk1977e014a832004-05-17 10:48:57 +00001369
1370 /* Evaluate the expression and insert it into the temp table */
1371 sqlite3ExprCode(pParse, pE2);
drh94a11212004-09-25 13:12:14 +00001372 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1);
drhf0863fe2005-06-12 21:35:51 +00001373 sqlite3VdbeAddOp(v, OP_IdxInsert, pExpr->iTable, 0);
drhfef52082000-06-06 01:50:43 +00001374 }
1375 }
danielk19770202b292004-06-09 09:55:16 +00001376 sqlite3VdbeChangeP3(v, addr, (void *)&keyInfo, P3_KEYINFO);
danielk1977b3bce662005-01-29 08:32:43 +00001377 break;
drhfef52082000-06-06 01:50:43 +00001378 }
1379
drh51522cd2005-01-20 13:36:19 +00001380 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001381 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +00001382 /* This has to be a scalar SELECT. Generate code to put the
1383 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +00001384 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +00001385 */
drhec7429a2005-10-06 16:53:14 +00001386 static const Token one = { "1", 0, 1 };
drh51522cd2005-01-20 13:36:19 +00001387 Select *pSel;
drhec7429a2005-10-06 16:53:14 +00001388 int iMem;
1389 int sop;
drh1398ad32005-01-19 23:24:50 +00001390
drhec7429a2005-10-06 16:53:14 +00001391 pExpr->iColumn = iMem = pParse->nMem++;
drh51522cd2005-01-20 13:36:19 +00001392 pSel = pExpr->pSelect;
1393 if( pExpr->op==TK_SELECT ){
1394 sop = SRT_Mem;
drhec7429a2005-10-06 16:53:14 +00001395 sqlite3VdbeAddOp(v, OP_MemNull, iMem, 0);
1396 VdbeComment((v, "# Init subquery result"));
drh51522cd2005-01-20 13:36:19 +00001397 }else{
drh51522cd2005-01-20 13:36:19 +00001398 sop = SRT_Exists;
drhec7429a2005-10-06 16:53:14 +00001399 sqlite3VdbeAddOp(v, OP_MemInt, 0, iMem);
1400 VdbeComment((v, "# Init EXISTS result"));
drh51522cd2005-01-20 13:36:19 +00001401 }
drhec7429a2005-10-06 16:53:14 +00001402 sqlite3ExprDelete(pSel->pLimit);
1403 pSel->pLimit = sqlite3Expr(TK_INTEGER, 0, 0, &one);
1404 sqlite3Select(pParse, pSel, sop, iMem, 0, 0, 0, 0);
danielk1977b3bce662005-01-29 08:32:43 +00001405 break;
drhcce7d172000-05-31 15:34:51 +00001406 }
1407 }
danielk1977b3bce662005-01-29 08:32:43 +00001408
drh57dbd7b2005-07-08 18:25:26 +00001409 if( testAddr ){
drhd654be82005-09-20 17:42:23 +00001410 sqlite3VdbeJumpHere(v, testAddr);
danielk1977b3bce662005-01-29 08:32:43 +00001411 }
1412 return;
drhcce7d172000-05-31 15:34:51 +00001413}
drh51522cd2005-01-20 13:36:19 +00001414#endif /* SQLITE_OMIT_SUBQUERY */
drhcce7d172000-05-31 15:34:51 +00001415
drhcce7d172000-05-31 15:34:51 +00001416/*
drhfec19aa2004-05-19 20:41:03 +00001417** Generate an instruction that will put the integer describe by
1418** text z[0..n-1] on the stack.
1419*/
1420static void codeInteger(Vdbe *v, const char *z, int n){
1421 int i;
drh6fec0762004-05-30 01:38:43 +00001422 if( sqlite3GetInt32(z, &i) ){
1423 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
1424 }else if( sqlite3FitsIn64Bits(z) ){
drh29dda4a2005-07-21 18:23:20 +00001425 sqlite3VdbeOp3(v, OP_Int64, 0, 0, z, n);
drhfec19aa2004-05-19 20:41:03 +00001426 }else{
1427 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n);
1428 }
1429}
1430
1431/*
drhcce7d172000-05-31 15:34:51 +00001432** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +00001433** expression and leave the result on the top of stack.
drhf2bc0132004-10-04 13:19:23 +00001434**
1435** This code depends on the fact that certain token values (ex: TK_EQ)
1436** are the same as opcode values (ex: OP_Eq) that implement the corresponding
1437** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
1438** the make process cause these values to align. Assert()s in the code
1439** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00001440*/
danielk19774adee202004-05-08 08:23:19 +00001441void sqlite3ExprCode(Parse *pParse, Expr *pExpr){
drhcce7d172000-05-31 15:34:51 +00001442 Vdbe *v = pParse->pVdbe;
1443 int op;
danielk19777977a172004-11-09 12:44:37 +00001444 if( v==0 ) return;
1445 if( pExpr==0 ){
drhf0863fe2005-06-12 21:35:51 +00001446 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
danielk19777977a172004-11-09 12:44:37 +00001447 return;
1448 }
drhf2bc0132004-10-04 13:19:23 +00001449 op = pExpr->op;
1450 switch( op ){
drh13449892005-09-07 21:22:45 +00001451 case TK_AGG_COLUMN: {
1452 AggInfo *pAggInfo = pExpr->pAggInfo;
1453 struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg];
1454 if( !pAggInfo->directMode ){
1455 sqlite3VdbeAddOp(v, OP_MemLoad, pCol->iMem, 0);
1456 break;
1457 }else if( pAggInfo->useSortingIdx ){
1458 sqlite3VdbeAddOp(v, OP_Column, pAggInfo->sortingIdx,
1459 pCol->iSorterColumn);
1460 break;
1461 }
1462 /* Otherwise, fall thru into the TK_COLUMN case */
1463 }
drh967e8b72000-06-21 13:59:10 +00001464 case TK_COLUMN: {
drh13449892005-09-07 21:22:45 +00001465 if( pExpr->iColumn>=0 ){
danielk19774adee202004-05-08 08:23:19 +00001466 sqlite3VdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn);
danielk1977aee18ef2005-03-09 12:26:50 +00001467 sqlite3ColumnDefault(v, pExpr->pTab, pExpr->iColumn);
drhc4a3c772001-04-04 11:48:57 +00001468 }else{
drhf0863fe2005-06-12 21:35:51 +00001469 sqlite3VdbeAddOp(v, OP_Rowid, pExpr->iTable, 0);
drh22827922000-06-06 17:27:05 +00001470 }
drhcce7d172000-05-31 15:34:51 +00001471 break;
1472 }
1473 case TK_INTEGER: {
drhfec19aa2004-05-19 20:41:03 +00001474 codeInteger(v, pExpr->token.z, pExpr->token.n);
1475 break;
1476 }
1477 case TK_FLOAT:
1478 case TK_STRING: {
drhf2bc0132004-10-04 13:19:23 +00001479 assert( TK_FLOAT==OP_Real );
1480 assert( TK_STRING==OP_String8 );
drhd2687b72005-08-12 22:56:09 +00001481 sqlite3DequoteExpr(pExpr);
drhfec19aa2004-05-19 20:41:03 +00001482 sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z, pExpr->token.n);
drhcce7d172000-05-31 15:34:51 +00001483 break;
1484 }
drhf0863fe2005-06-12 21:35:51 +00001485 case TK_NULL: {
1486 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
1487 break;
1488 }
danielk19775338a5f2005-01-20 13:03:10 +00001489#ifndef SQLITE_OMIT_BLOB_LITERAL
danielk1977c572ef72004-05-27 09:28:41 +00001490 case TK_BLOB: {
drh6c8c6ce2005-08-23 11:17:58 +00001491 int n;
1492 const char *z;
drhf2bc0132004-10-04 13:19:23 +00001493 assert( TK_BLOB==OP_HexBlob );
drh6c8c6ce2005-08-23 11:17:58 +00001494 n = pExpr->token.n - 3;
1495 z = pExpr->token.z + 2;
1496 assert( n>=0 );
1497 if( n==0 ){
1498 z = "";
1499 }
1500 sqlite3VdbeOp3(v, op, 0, 0, z, n);
danielk1977c572ef72004-05-27 09:28:41 +00001501 break;
1502 }
danielk19775338a5f2005-01-20 13:03:10 +00001503#endif
drh50457892003-09-06 01:10:47 +00001504 case TK_VARIABLE: {
danielk19774adee202004-05-08 08:23:19 +00001505 sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0);
drh895d7472004-08-20 16:02:39 +00001506 if( pExpr->token.n>1 ){
1507 sqlite3VdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
1508 }
drh50457892003-09-06 01:10:47 +00001509 break;
1510 }
drh4e0cff62004-11-05 05:10:28 +00001511 case TK_REGISTER: {
1512 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iTable, 0);
1513 break;
1514 }
drh487e2622005-06-25 18:42:14 +00001515#ifndef SQLITE_OMIT_CAST
1516 case TK_CAST: {
1517 /* Expressions of the form: CAST(pLeft AS token) */
1518 int aff, op;
1519 sqlite3ExprCode(pParse, pExpr->pLeft);
drh8df447f2005-11-01 15:48:24 +00001520 aff = sqlite3AffinityType(&pExpr->token, 1);
drh487e2622005-06-25 18:42:14 +00001521 switch( aff ){
1522 case SQLITE_AFF_INTEGER: op = OP_ToInt; break;
1523 case SQLITE_AFF_NUMERIC: op = OP_ToNumeric; break;
1524 case SQLITE_AFF_TEXT: op = OP_ToText; break;
1525 case SQLITE_AFF_NONE: op = OP_ToBlob; break;
1526 }
1527 sqlite3VdbeAddOp(v, op, 0, 0);
1528 break;
1529 }
1530#endif /* SQLITE_OMIT_CAST */
drhc9b84a12002-06-20 11:36:48 +00001531 case TK_LT:
1532 case TK_LE:
1533 case TK_GT:
1534 case TK_GE:
1535 case TK_NE:
1536 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00001537 assert( TK_LT==OP_Lt );
1538 assert( TK_LE==OP_Le );
1539 assert( TK_GT==OP_Gt );
1540 assert( TK_GE==OP_Ge );
1541 assert( TK_EQ==OP_Eq );
1542 assert( TK_NE==OP_Ne );
danielk1977a37cdde2004-05-16 11:15:36 +00001543 sqlite3ExprCode(pParse, pExpr->pLeft);
1544 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001545 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 0, 0);
danielk1977a37cdde2004-05-16 11:15:36 +00001546 break;
drhc9b84a12002-06-20 11:36:48 +00001547 }
drhcce7d172000-05-31 15:34:51 +00001548 case TK_AND:
1549 case TK_OR:
1550 case TK_PLUS:
1551 case TK_STAR:
1552 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00001553 case TK_REM:
1554 case TK_BITAND:
1555 case TK_BITOR:
drh17c40292004-07-21 02:53:29 +00001556 case TK_SLASH:
drhbf4133c2001-10-13 02:59:08 +00001557 case TK_LSHIFT:
drh855eb1c2004-08-31 13:45:11 +00001558 case TK_RSHIFT:
drh00400772000-06-16 20:51:26 +00001559 case TK_CONCAT: {
drhf2bc0132004-10-04 13:19:23 +00001560 assert( TK_AND==OP_And );
1561 assert( TK_OR==OP_Or );
1562 assert( TK_PLUS==OP_Add );
1563 assert( TK_MINUS==OP_Subtract );
1564 assert( TK_REM==OP_Remainder );
1565 assert( TK_BITAND==OP_BitAnd );
1566 assert( TK_BITOR==OP_BitOr );
1567 assert( TK_SLASH==OP_Divide );
1568 assert( TK_LSHIFT==OP_ShiftLeft );
1569 assert( TK_RSHIFT==OP_ShiftRight );
1570 assert( TK_CONCAT==OP_Concat );
danielk19774adee202004-05-08 08:23:19 +00001571 sqlite3ExprCode(pParse, pExpr->pLeft);
1572 sqlite3ExprCode(pParse, pExpr->pRight);
drh855eb1c2004-08-31 13:45:11 +00001573 sqlite3VdbeAddOp(v, op, 0, 0);
drh00400772000-06-16 20:51:26 +00001574 break;
1575 }
drhcce7d172000-05-31 15:34:51 +00001576 case TK_UMINUS: {
drhfec19aa2004-05-19 20:41:03 +00001577 Expr *pLeft = pExpr->pLeft;
1578 assert( pLeft );
1579 if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
1580 Token *p = &pLeft->token;
drh6e142f52000-06-08 13:36:40 +00001581 char *z = sqliteMalloc( p->n + 2 );
1582 sprintf(z, "-%.*s", p->n, p->z);
drhfec19aa2004-05-19 20:41:03 +00001583 if( pLeft->op==TK_FLOAT ){
1584 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001585 }else{
drhfec19aa2004-05-19 20:41:03 +00001586 codeInteger(v, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001587 }
drh6e142f52000-06-08 13:36:40 +00001588 sqliteFree(z);
1589 break;
1590 }
drh1ccde152000-06-17 13:12:39 +00001591 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +00001592 }
drhbf4133c2001-10-13 02:59:08 +00001593 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00001594 case TK_NOT: {
drhf2bc0132004-10-04 13:19:23 +00001595 assert( TK_BITNOT==OP_BitNot );
1596 assert( TK_NOT==OP_Not );
danielk19774adee202004-05-08 08:23:19 +00001597 sqlite3ExprCode(pParse, pExpr->pLeft);
1598 sqlite3VdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001599 break;
1600 }
1601 case TK_ISNULL:
1602 case TK_NOTNULL: {
1603 int dest;
drhf2bc0132004-10-04 13:19:23 +00001604 assert( TK_ISNULL==OP_IsNull );
1605 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00001606 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1607 sqlite3ExprCode(pParse, pExpr->pLeft);
1608 dest = sqlite3VdbeCurrentAddr(v) + 2;
1609 sqlite3VdbeAddOp(v, op, 1, dest);
1610 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);
drhf2bc0132004-10-04 13:19:23 +00001611 break;
drhcce7d172000-05-31 15:34:51 +00001612 }
drh22827922000-06-06 17:27:05 +00001613 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00001614 AggInfo *pInfo = pExpr->pAggInfo;
1615 sqlite3VdbeAddOp(v, OP_MemLoad, pInfo->aFunc[pExpr->iAgg].iMem, 0);
drh22827922000-06-06 17:27:05 +00001616 break;
1617 }
drhb71090f2005-05-23 17:26:51 +00001618 case TK_CONST_FUNC:
drhcce7d172000-05-31 15:34:51 +00001619 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00001620 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00001621 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00001622 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00001623 int nId;
1624 const char *zId;
drh13449892005-09-07 21:22:45 +00001625 int constMask = 0;
danielk1977682f68b2004-06-05 10:22:17 +00001626 int i;
danielk1977d8123362004-06-12 09:25:12 +00001627 u8 enc = pParse->db->enc;
danielk1977dc1bdc42004-06-11 10:51:27 +00001628 CollSeq *pColl = 0;
drhb71090f2005-05-23 17:26:51 +00001629 zId = pExpr->token.z;
1630 nId = pExpr->token.n;
danielk1977d8123362004-06-12 09:25:12 +00001631 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001632 assert( pDef!=0 );
drhf9b596e2004-05-26 16:54:42 +00001633 nExpr = sqlite3ExprCodeExprList(pParse, pList);
danielk1977682f68b2004-06-05 10:22:17 +00001634 for(i=0; i<nExpr && i<32; i++){
danielk1977d02eb1f2004-06-06 09:44:03 +00001635 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
drh13449892005-09-07 21:22:45 +00001636 constMask |= (1<<i);
danielk1977d02eb1f2004-06-06 09:44:03 +00001637 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001638 if( pDef->needCollSeq && !pColl ){
1639 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
1640 }
1641 }
1642 if( pDef->needCollSeq ){
1643 if( !pColl ) pColl = pParse->db->pDfltColl;
danielk1977d8123362004-06-12 09:25:12 +00001644 sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ);
danielk1977682f68b2004-06-05 10:22:17 +00001645 }
drh13449892005-09-07 21:22:45 +00001646 sqlite3VdbeOp3(v, OP_Function, constMask, nExpr, (char*)pDef, P3_FUNCDEF);
drhcce7d172000-05-31 15:34:51 +00001647 break;
1648 }
drhfe2093d2005-01-20 22:48:47 +00001649#ifndef SQLITE_OMIT_SUBQUERY
1650 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001651 case TK_SELECT: {
danielk1977b3bce662005-01-29 08:32:43 +00001652 sqlite3CodeSubselect(pParse, pExpr);
danielk19774adee202004-05-08 08:23:19 +00001653 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drhad6d9462004-09-19 02:15:24 +00001654 VdbeComment((v, "# load subquery result"));
drh19a775c2000-06-05 18:54:46 +00001655 break;
1656 }
drhfef52082000-06-06 01:50:43 +00001657 case TK_IN: {
1658 int addr;
drh94a11212004-09-25 13:12:14 +00001659 char affinity;
danielk1977b3bce662005-01-29 08:32:43 +00001660 sqlite3CodeSubselect(pParse, pExpr);
danielk1977e014a832004-05-17 10:48:57 +00001661
1662 /* Figure out the affinity to use to create a key from the results
1663 ** of the expression. affinityStr stores a static string suitable for
danielk1977ededfd52004-06-17 07:53:01 +00001664 ** P3 of OP_MakeRecord.
danielk1977e014a832004-05-17 10:48:57 +00001665 */
drh94a11212004-09-25 13:12:14 +00001666 affinity = comparisonAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +00001667
danielk19774adee202004-05-08 08:23:19 +00001668 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
danielk1977e014a832004-05-17 10:48:57 +00001669
1670 /* Code the <expr> from "<expr> IN (...)". The temporary table
1671 ** pExpr->iTable contains the values that make up the (...) set.
1672 */
danielk19774adee202004-05-08 08:23:19 +00001673 sqlite3ExprCode(pParse, pExpr->pLeft);
1674 addr = sqlite3VdbeCurrentAddr(v);
danielk1977e014a832004-05-17 10:48:57 +00001675 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); /* addr + 0 */
danielk19774adee202004-05-08 08:23:19 +00001676 sqlite3VdbeAddOp(v, OP_Pop, 2, 0);
drhf0863fe2005-06-12 21:35:51 +00001677 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +00001678 sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7);
drh94a11212004-09-25 13:12:14 +00001679 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1); /* addr + 4 */
danielk1977e014a832004-05-17 10:48:57 +00001680 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7);
1681 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); /* addr + 6 */
1682
drhfef52082000-06-06 01:50:43 +00001683 break;
1684 }
danielk197793758c82005-01-21 08:13:14 +00001685#endif
drhfef52082000-06-06 01:50:43 +00001686 case TK_BETWEEN: {
drhbe5c89a2004-07-26 00:31:09 +00001687 Expr *pLeft = pExpr->pLeft;
1688 struct ExprList_item *pLItem = pExpr->pList->a;
1689 Expr *pRight = pLItem->pExpr;
1690 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001691 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001692 sqlite3ExprCode(pParse, pRight);
1693 codeCompare(pParse, pLeft, pRight, OP_Ge, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001694 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
drhbe5c89a2004-07-26 00:31:09 +00001695 pLItem++;
1696 pRight = pLItem->pExpr;
1697 sqlite3ExprCode(pParse, pRight);
1698 codeCompare(pParse, pLeft, pRight, OP_Le, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001699 sqlite3VdbeAddOp(v, OP_And, 0, 0);
drhfef52082000-06-06 01:50:43 +00001700 break;
1701 }
drh51e9a442004-01-16 16:42:53 +00001702 case TK_UPLUS:
drha2e00042002-01-22 03:13:42 +00001703 case TK_AS: {
danielk19774adee202004-05-08 08:23:19 +00001704 sqlite3ExprCode(pParse, pExpr->pLeft);
drha2e00042002-01-22 03:13:42 +00001705 break;
1706 }
drh17a7f8d2002-03-24 13:13:27 +00001707 case TK_CASE: {
1708 int expr_end_label;
drhf5905aa2002-05-26 20:54:33 +00001709 int jumpInst;
drhf5905aa2002-05-26 20:54:33 +00001710 int nExpr;
drh17a7f8d2002-03-24 13:13:27 +00001711 int i;
drhbe5c89a2004-07-26 00:31:09 +00001712 ExprList *pEList;
1713 struct ExprList_item *aListelem;
drh17a7f8d2002-03-24 13:13:27 +00001714
1715 assert(pExpr->pList);
1716 assert((pExpr->pList->nExpr % 2) == 0);
1717 assert(pExpr->pList->nExpr > 0);
drhbe5c89a2004-07-26 00:31:09 +00001718 pEList = pExpr->pList;
1719 aListelem = pEList->a;
1720 nExpr = pEList->nExpr;
danielk19774adee202004-05-08 08:23:19 +00001721 expr_end_label = sqlite3VdbeMakeLabel(v);
drh17a7f8d2002-03-24 13:13:27 +00001722 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001723 sqlite3ExprCode(pParse, pExpr->pLeft);
drh17a7f8d2002-03-24 13:13:27 +00001724 }
drhf5905aa2002-05-26 20:54:33 +00001725 for(i=0; i<nExpr; i=i+2){
drhbe5c89a2004-07-26 00:31:09 +00001726 sqlite3ExprCode(pParse, aListelem[i].pExpr);
drh17a7f8d2002-03-24 13:13:27 +00001727 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001728 sqlite3VdbeAddOp(v, OP_Dup, 1, 1);
drhbe5c89a2004-07-26 00:31:09 +00001729 jumpInst = codeCompare(pParse, pExpr->pLeft, aListelem[i].pExpr,
1730 OP_Ne, 0, 1);
danielk19774adee202004-05-08 08:23:19 +00001731 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001732 }else{
danielk19774adee202004-05-08 08:23:19 +00001733 jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001734 }
drhbe5c89a2004-07-26 00:31:09 +00001735 sqlite3ExprCode(pParse, aListelem[i+1].pExpr);
danielk19774adee202004-05-08 08:23:19 +00001736 sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label);
drhd654be82005-09-20 17:42:23 +00001737 sqlite3VdbeJumpHere(v, jumpInst);
drh17a7f8d2002-03-24 13:13:27 +00001738 }
drhf570f012002-05-31 15:51:25 +00001739 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001740 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhf570f012002-05-31 15:51:25 +00001741 }
drh17a7f8d2002-03-24 13:13:27 +00001742 if( pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001743 sqlite3ExprCode(pParse, pExpr->pRight);
drh17a7f8d2002-03-24 13:13:27 +00001744 }else{
drhf0863fe2005-06-12 21:35:51 +00001745 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
drh17a7f8d2002-03-24 13:13:27 +00001746 }
danielk19774adee202004-05-08 08:23:19 +00001747 sqlite3VdbeResolveLabel(v, expr_end_label);
danielk19776f349032002-06-11 02:25:40 +00001748 break;
1749 }
danielk19775338a5f2005-01-20 13:03:10 +00001750#ifndef SQLITE_OMIT_TRIGGER
danielk19776f349032002-06-11 02:25:40 +00001751 case TK_RAISE: {
1752 if( !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +00001753 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00001754 "RAISE() may only be used within a trigger-program");
danielk19776f349032002-06-11 02:25:40 +00001755 return;
1756 }
drhad6d9462004-09-19 02:15:24 +00001757 if( pExpr->iColumn!=OE_Ignore ){
1758 assert( pExpr->iColumn==OE_Rollback ||
1759 pExpr->iColumn == OE_Abort ||
1760 pExpr->iColumn == OE_Fail );
drhd2687b72005-08-12 22:56:09 +00001761 sqlite3DequoteExpr(pExpr);
drhad6d9462004-09-19 02:15:24 +00001762 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn,
1763 pExpr->token.z, pExpr->token.n);
danielk19776f349032002-06-11 02:25:40 +00001764 } else {
drhad6d9462004-09-19 02:15:24 +00001765 assert( pExpr->iColumn == OE_Ignore );
1766 sqlite3VdbeAddOp(v, OP_ContextPop, 0, 0);
1767 sqlite3VdbeAddOp(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
1768 VdbeComment((v, "# raise(IGNORE)"));
danielk19776f349032002-06-11 02:25:40 +00001769 }
drh17a7f8d2002-03-24 13:13:27 +00001770 }
danielk19775338a5f2005-01-20 13:03:10 +00001771#endif
drh17a7f8d2002-03-24 13:13:27 +00001772 break;
drhcce7d172000-05-31 15:34:51 +00001773 }
drhcce7d172000-05-31 15:34:51 +00001774}
1775
danielk197793758c82005-01-21 08:13:14 +00001776#ifndef SQLITE_OMIT_TRIGGER
drhcce7d172000-05-31 15:34:51 +00001777/*
drh25303782004-12-07 15:41:48 +00001778** Generate code that evalutes the given expression and leaves the result
1779** on the stack. See also sqlite3ExprCode().
1780**
1781** This routine might also cache the result and modify the pExpr tree
1782** so that it will make use of the cached result on subsequent evaluations
1783** rather than evaluate the whole expression again. Trivial expressions are
1784** not cached. If the expression is cached, its result is stored in a
1785** memory location.
1786*/
1787void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr){
1788 Vdbe *v = pParse->pVdbe;
1789 int iMem;
1790 int addr1, addr2;
1791 if( v==0 ) return;
1792 addr1 = sqlite3VdbeCurrentAddr(v);
1793 sqlite3ExprCode(pParse, pExpr);
1794 addr2 = sqlite3VdbeCurrentAddr(v);
1795 if( addr2>addr1+1 || sqlite3VdbeGetOp(v, addr1)->opcode==OP_Function ){
1796 iMem = pExpr->iTable = pParse->nMem++;
1797 sqlite3VdbeAddOp(v, OP_MemStore, iMem, 0);
1798 pExpr->op = TK_REGISTER;
1799 }
1800}
danielk197793758c82005-01-21 08:13:14 +00001801#endif
drh25303782004-12-07 15:41:48 +00001802
1803/*
drh268380c2004-02-25 13:47:31 +00001804** Generate code that pushes the value of every element of the given
drhf9b596e2004-05-26 16:54:42 +00001805** expression list onto the stack.
drh268380c2004-02-25 13:47:31 +00001806**
1807** Return the number of elements pushed onto the stack.
1808*/
danielk19774adee202004-05-08 08:23:19 +00001809int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00001810 Parse *pParse, /* Parsing context */
drhf9b596e2004-05-26 16:54:42 +00001811 ExprList *pList /* The expression list to be coded */
drh268380c2004-02-25 13:47:31 +00001812){
1813 struct ExprList_item *pItem;
1814 int i, n;
drh268380c2004-02-25 13:47:31 +00001815 if( pList==0 ) return 0;
drh268380c2004-02-25 13:47:31 +00001816 n = pList->nExpr;
drhc182d162005-08-14 20:47:16 +00001817 for(pItem=pList->a, i=n; i>0; i--, pItem++){
danielk19774adee202004-05-08 08:23:19 +00001818 sqlite3ExprCode(pParse, pItem->pExpr);
drh268380c2004-02-25 13:47:31 +00001819 }
drhf9b596e2004-05-26 16:54:42 +00001820 return n;
drh268380c2004-02-25 13:47:31 +00001821}
1822
1823/*
drhcce7d172000-05-31 15:34:51 +00001824** Generate code for a boolean expression such that a jump is made
1825** to the label "dest" if the expression is true but execution
1826** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00001827**
1828** If the expression evaluates to NULL (neither true nor false), then
1829** take the jump if the jumpIfNull flag is true.
drhf2bc0132004-10-04 13:19:23 +00001830**
1831** This code depends on the fact that certain token values (ex: TK_EQ)
1832** are the same as opcode values (ex: OP_Eq) that implement the corresponding
1833** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
1834** the make process cause these values to align. Assert()s in the code
1835** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00001836*/
danielk19774adee202004-05-08 08:23:19 +00001837void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001838 Vdbe *v = pParse->pVdbe;
1839 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001840 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00001841 op = pExpr->op;
1842 switch( op ){
drhcce7d172000-05-31 15:34:51 +00001843 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001844 int d2 = sqlite3VdbeMakeLabel(v);
1845 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
1846 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
1847 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001848 break;
1849 }
1850 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001851 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
1852 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001853 break;
1854 }
1855 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001856 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001857 break;
1858 }
1859 case TK_LT:
1860 case TK_LE:
1861 case TK_GT:
1862 case TK_GE:
1863 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00001864 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00001865 assert( TK_LT==OP_Lt );
1866 assert( TK_LE==OP_Le );
1867 assert( TK_GT==OP_Gt );
1868 assert( TK_GE==OP_Ge );
1869 assert( TK_EQ==OP_Eq );
1870 assert( TK_NE==OP_Ne );
danielk19774adee202004-05-08 08:23:19 +00001871 sqlite3ExprCode(pParse, pExpr->pLeft);
1872 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001873 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001874 break;
1875 }
1876 case TK_ISNULL:
1877 case TK_NOTNULL: {
drhf2bc0132004-10-04 13:19:23 +00001878 assert( TK_ISNULL==OP_IsNull );
1879 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00001880 sqlite3ExprCode(pParse, pExpr->pLeft);
1881 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001882 break;
1883 }
drhfef52082000-06-06 01:50:43 +00001884 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00001885 /* The expression "x BETWEEN y AND z" is implemented as:
1886 **
1887 ** 1 IF (x < y) GOTO 3
1888 ** 2 IF (x <= z) GOTO <dest>
1889 ** 3 ...
1890 */
drhf5905aa2002-05-26 20:54:33 +00001891 int addr;
drhbe5c89a2004-07-26 00:31:09 +00001892 Expr *pLeft = pExpr->pLeft;
1893 Expr *pRight = pExpr->pList->a[0].pExpr;
1894 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001895 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001896 sqlite3ExprCode(pParse, pRight);
1897 addr = codeCompare(pParse, pLeft, pRight, OP_Lt, 0, !jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00001898
drhbe5c89a2004-07-26 00:31:09 +00001899 pRight = pExpr->pList->a[1].pExpr;
1900 sqlite3ExprCode(pParse, pRight);
1901 codeCompare(pParse, pLeft, pRight, OP_Le, dest, jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00001902
danielk19774adee202004-05-08 08:23:19 +00001903 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
drhd654be82005-09-20 17:42:23 +00001904 sqlite3VdbeJumpHere(v, addr);
danielk19774adee202004-05-08 08:23:19 +00001905 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00001906 break;
1907 }
drhcce7d172000-05-31 15:34:51 +00001908 default: {
danielk19774adee202004-05-08 08:23:19 +00001909 sqlite3ExprCode(pParse, pExpr);
1910 sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001911 break;
1912 }
1913 }
1914}
1915
1916/*
drh66b89c82000-11-28 20:47:17 +00001917** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00001918** to the label "dest" if the expression is false but execution
1919** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00001920**
1921** If the expression evaluates to NULL (neither true nor false) then
1922** jump if jumpIfNull is true or fall through if jumpIfNull is false.
drhcce7d172000-05-31 15:34:51 +00001923*/
danielk19774adee202004-05-08 08:23:19 +00001924void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001925 Vdbe *v = pParse->pVdbe;
1926 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001927 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00001928
1929 /* The value of pExpr->op and op are related as follows:
1930 **
1931 ** pExpr->op op
1932 ** --------- ----------
1933 ** TK_ISNULL OP_NotNull
1934 ** TK_NOTNULL OP_IsNull
1935 ** TK_NE OP_Eq
1936 ** TK_EQ OP_Ne
1937 ** TK_GT OP_Le
1938 ** TK_LE OP_Gt
1939 ** TK_GE OP_Lt
1940 ** TK_LT OP_Ge
1941 **
1942 ** For other values of pExpr->op, op is undefined and unused.
1943 ** The value of TK_ and OP_ constants are arranged such that we
1944 ** can compute the mapping above using the following expression.
1945 ** Assert()s verify that the computation is correct.
1946 */
1947 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
1948
1949 /* Verify correct alignment of TK_ and OP_ constants
1950 */
1951 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
1952 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
1953 assert( pExpr->op!=TK_NE || op==OP_Eq );
1954 assert( pExpr->op!=TK_EQ || op==OP_Ne );
1955 assert( pExpr->op!=TK_LT || op==OP_Ge );
1956 assert( pExpr->op!=TK_LE || op==OP_Gt );
1957 assert( pExpr->op!=TK_GT || op==OP_Le );
1958 assert( pExpr->op!=TK_GE || op==OP_Lt );
1959
drhcce7d172000-05-31 15:34:51 +00001960 switch( pExpr->op ){
1961 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001962 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
1963 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001964 break;
1965 }
1966 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001967 int d2 = sqlite3VdbeMakeLabel(v);
1968 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
1969 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
1970 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001971 break;
1972 }
1973 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001974 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001975 break;
1976 }
1977 case TK_LT:
1978 case TK_LE:
1979 case TK_GT:
1980 case TK_GE:
1981 case TK_NE:
1982 case TK_EQ: {
danielk19774adee202004-05-08 08:23:19 +00001983 sqlite3ExprCode(pParse, pExpr->pLeft);
1984 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001985 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001986 break;
1987 }
drhcce7d172000-05-31 15:34:51 +00001988 case TK_ISNULL:
1989 case TK_NOTNULL: {
danielk19774adee202004-05-08 08:23:19 +00001990 sqlite3ExprCode(pParse, pExpr->pLeft);
1991 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001992 break;
1993 }
drhfef52082000-06-06 01:50:43 +00001994 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00001995 /* The expression is "x BETWEEN y AND z". It is implemented as:
1996 **
1997 ** 1 IF (x >= y) GOTO 3
1998 ** 2 GOTO <dest>
1999 ** 3 IF (x > z) GOTO <dest>
2000 */
drhfef52082000-06-06 01:50:43 +00002001 int addr;
drhbe5c89a2004-07-26 00:31:09 +00002002 Expr *pLeft = pExpr->pLeft;
2003 Expr *pRight = pExpr->pList->a[0].pExpr;
2004 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00002005 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00002006 sqlite3ExprCode(pParse, pRight);
danielk19774adee202004-05-08 08:23:19 +00002007 addr = sqlite3VdbeCurrentAddr(v);
drhbe5c89a2004-07-26 00:31:09 +00002008 codeCompare(pParse, pLeft, pRight, OP_Ge, addr+3, !jumpIfNull);
2009
danielk19774adee202004-05-08 08:23:19 +00002010 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
2011 sqlite3VdbeAddOp(v, OP_Goto, 0, dest);
drhbe5c89a2004-07-26 00:31:09 +00002012 pRight = pExpr->pList->a[1].pExpr;
2013 sqlite3ExprCode(pParse, pRight);
2014 codeCompare(pParse, pLeft, pRight, OP_Gt, dest, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00002015 break;
2016 }
drhcce7d172000-05-31 15:34:51 +00002017 default: {
danielk19774adee202004-05-08 08:23:19 +00002018 sqlite3ExprCode(pParse, pExpr);
2019 sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00002020 break;
2021 }
2022 }
2023}
drh22827922000-06-06 17:27:05 +00002024
2025/*
2026** Do a deep comparison of two expression trees. Return TRUE (non-zero)
2027** if they are identical and return FALSE if they differ in any way.
2028*/
danielk19774adee202004-05-08 08:23:19 +00002029int sqlite3ExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00002030 int i;
2031 if( pA==0 ){
2032 return pB==0;
2033 }else if( pB==0 ){
2034 return 0;
2035 }
2036 if( pA->op!=pB->op ) return 0;
drhfd357972005-09-09 01:33:19 +00002037 if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 0;
danielk19774adee202004-05-08 08:23:19 +00002038 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
2039 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00002040 if( pA->pList ){
2041 if( pB->pList==0 ) return 0;
2042 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
2043 for(i=0; i<pA->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00002044 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00002045 return 0;
2046 }
2047 }
2048 }else if( pB->pList ){
2049 return 0;
2050 }
2051 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00002052 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drh22827922000-06-06 17:27:05 +00002053 if( pA->token.z ){
2054 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00002055 if( pB->token.n!=pA->token.n ) return 0;
danielk19774adee202004-05-08 08:23:19 +00002056 if( sqlite3StrNICmp(pA->token.z, pB->token.z, pB->token.n)!=0 ) return 0;
drh22827922000-06-06 17:27:05 +00002057 }
2058 return 1;
2059}
2060
drh13449892005-09-07 21:22:45 +00002061
drh22827922000-06-06 17:27:05 +00002062/*
drh13449892005-09-07 21:22:45 +00002063** Add a new element to the pAggInfo->aCol[] array. Return the index of
2064** the new element. Return a negative number if malloc fails.
drh22827922000-06-06 17:27:05 +00002065*/
drh13449892005-09-07 21:22:45 +00002066static int addAggInfoColumn(AggInfo *pInfo){
2067 int i;
2068 i = sqlite3ArrayAllocate((void**)&pInfo->aCol, sizeof(pInfo->aCol[0]), 3);
2069 if( i<0 ){
2070 return -1;
drh22827922000-06-06 17:27:05 +00002071 }
drh13449892005-09-07 21:22:45 +00002072 return i;
2073}
2074
2075/*
2076** Add a new element to the pAggInfo->aFunc[] array. Return the index of
2077** the new element. Return a negative number if malloc fails.
2078*/
2079static int addAggInfoFunc(AggInfo *pInfo){
2080 int i;
2081 i = sqlite3ArrayAllocate((void**)&pInfo->aFunc, sizeof(pInfo->aFunc[0]), 2);
2082 if( i<0 ){
2083 return -1;
2084 }
2085 return i;
2086}
drh22827922000-06-06 17:27:05 +00002087
2088/*
drh626a8792005-01-17 22:08:19 +00002089** This is an xFunc for walkExprTree() used to implement
2090** sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates
2091** for additional information.
drh22827922000-06-06 17:27:05 +00002092**
drh626a8792005-01-17 22:08:19 +00002093** This routine analyzes the aggregate function at pExpr.
drh22827922000-06-06 17:27:05 +00002094*/
drh626a8792005-01-17 22:08:19 +00002095static int analyzeAggregate(void *pArg, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00002096 int i;
danielk1977a58fdfb2005-02-08 07:50:40 +00002097 NameContext *pNC = (NameContext *)pArg;
2098 Parse *pParse = pNC->pParse;
2099 SrcList *pSrcList = pNC->pSrcList;
drh13449892005-09-07 21:22:45 +00002100 AggInfo *pAggInfo = pNC->pAggInfo;
2101
drh22827922000-06-06 17:27:05 +00002102
drh22827922000-06-06 17:27:05 +00002103 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00002104 case TK_COLUMN: {
drh13449892005-09-07 21:22:45 +00002105 /* Check to see if the column is in one of the tables in the FROM
2106 ** clause of the aggregate query */
2107 if( pSrcList ){
2108 struct SrcList_item *pItem = pSrcList->a;
2109 for(i=0; i<pSrcList->nSrc; i++, pItem++){
2110 struct AggInfo_col *pCol;
2111 if( pExpr->iTable==pItem->iCursor ){
2112 /* If we reach this point, it means that pExpr refers to a table
2113 ** that is in the FROM clause of the aggregate query.
2114 **
2115 ** Make an entry for the column in pAggInfo->aCol[] if there
2116 ** is not an entry there already.
2117 */
2118 pCol = pAggInfo->aCol;
2119 for(i=0; i<pAggInfo->nColumn; i++, pCol++){
2120 if( pCol->iTable==pExpr->iTable &&
2121 pCol->iColumn==pExpr->iColumn ){
2122 break;
2123 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002124 }
drh13449892005-09-07 21:22:45 +00002125 if( i>=pAggInfo->nColumn && (i = addAggInfoColumn(pAggInfo))>=0 ){
2126 pCol = &pAggInfo->aCol[i];
2127 pCol->iTable = pExpr->iTable;
2128 pCol->iColumn = pExpr->iColumn;
2129 pCol->iMem = pParse->nMem++;
2130 pCol->iSorterColumn = -1;
drh5774b802005-09-07 22:48:16 +00002131 pCol->pExpr = pExpr;
drh13449892005-09-07 21:22:45 +00002132 if( pAggInfo->pGroupBy ){
2133 int j, n;
2134 ExprList *pGB = pAggInfo->pGroupBy;
2135 struct ExprList_item *pTerm = pGB->a;
2136 n = pGB->nExpr;
2137 for(j=0; j<n; j++, pTerm++){
2138 Expr *pE = pTerm->pExpr;
2139 if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable &&
2140 pE->iColumn==pExpr->iColumn ){
2141 pCol->iSorterColumn = j;
2142 break;
2143 }
2144 }
2145 }
2146 if( pCol->iSorterColumn<0 ){
2147 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
2148 }
2149 }
2150 /* There is now an entry for pExpr in pAggInfo->aCol[] (either
2151 ** because it was there before or because we just created it).
2152 ** Convert the pExpr to be a TK_AGG_COLUMN referring to that
2153 ** pAggInfo->aCol[] entry.
2154 */
2155 pExpr->pAggInfo = pAggInfo;
2156 pExpr->op = TK_AGG_COLUMN;
2157 pExpr->iAgg = i;
2158 break;
2159 } /* endif pExpr->iTable==pItem->iCursor */
2160 } /* end loop over pSrcList */
drh22827922000-06-06 17:27:05 +00002161 }
drh626a8792005-01-17 22:08:19 +00002162 return 1;
drh22827922000-06-06 17:27:05 +00002163 }
2164 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00002165 /* The pNC->nDepth==0 test causes aggregate functions in subqueries
2166 ** to be ignored */
danielk1977a58fdfb2005-02-08 07:50:40 +00002167 if( pNC->nDepth==0 ){
drh13449892005-09-07 21:22:45 +00002168 /* Check to see if pExpr is a duplicate of another aggregate
2169 ** function that is already in the pAggInfo structure
2170 */
2171 struct AggInfo_func *pItem = pAggInfo->aFunc;
2172 for(i=0; i<pAggInfo->nFunc; i++, pItem++){
2173 if( sqlite3ExprCompare(pItem->pExpr, pExpr) ){
danielk1977a58fdfb2005-02-08 07:50:40 +00002174 break;
2175 }
drh22827922000-06-06 17:27:05 +00002176 }
drh13449892005-09-07 21:22:45 +00002177 if( i>=pAggInfo->nFunc ){
2178 /* pExpr is original. Make a new entry in pAggInfo->aFunc[]
2179 */
danielk1977a58fdfb2005-02-08 07:50:40 +00002180 u8 enc = pParse->db->enc;
drh13449892005-09-07 21:22:45 +00002181 i = addAggInfoFunc(pAggInfo);
2182 if( i>=0 ){
2183 pItem = &pAggInfo->aFunc[i];
2184 pItem->pExpr = pExpr;
2185 pItem->iMem = pParse->nMem++;
2186 pItem->pFunc = sqlite3FindFunction(pParse->db,
2187 pExpr->token.z, pExpr->token.n,
2188 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
drhfd357972005-09-09 01:33:19 +00002189 if( pExpr->flags & EP_Distinct ){
2190 pItem->iDistinct = pParse->nTab++;
2191 }else{
2192 pItem->iDistinct = -1;
2193 }
drh13449892005-09-07 21:22:45 +00002194 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002195 }
drh13449892005-09-07 21:22:45 +00002196 /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
2197 */
danielk1977a58fdfb2005-02-08 07:50:40 +00002198 pExpr->iAgg = i;
drh13449892005-09-07 21:22:45 +00002199 pExpr->pAggInfo = pAggInfo;
danielk1977a58fdfb2005-02-08 07:50:40 +00002200 return 1;
drh22827922000-06-06 17:27:05 +00002201 }
drh22827922000-06-06 17:27:05 +00002202 }
2203 }
drh13449892005-09-07 21:22:45 +00002204
2205 /* Recursively walk subqueries looking for TK_COLUMN nodes that need
2206 ** to be changed to TK_AGG_COLUMN. But increment nDepth so that
2207 ** TK_AGG_FUNCTION nodes in subqueries will be unchanged.
2208 */
danielk1977a58fdfb2005-02-08 07:50:40 +00002209 if( pExpr->pSelect ){
2210 pNC->nDepth++;
2211 walkSelectExpr(pExpr->pSelect, analyzeAggregate, pNC);
2212 pNC->nDepth--;
2213 }
drh626a8792005-01-17 22:08:19 +00002214 return 0;
2215}
2216
2217/*
2218** Analyze the given expression looking for aggregate functions and
2219** for variables that need to be added to the pParse->aAgg[] array.
2220** Make additional entries to the pParse->aAgg[] array as necessary.
2221**
2222** This routine should only be called after the expression has been
2223** analyzed by sqlite3ExprResolveNames().
2224**
2225** If errors are seen, leave an error message in zErrMsg and return
2226** the number of errors.
2227*/
danielk1977a58fdfb2005-02-08 07:50:40 +00002228int sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
2229 int nErr = pNC->pParse->nErr;
2230 walkExprTree(pExpr, analyzeAggregate, pNC);
2231 return pNC->pParse->nErr - nErr;
drh22827922000-06-06 17:27:05 +00002232}
drh5d9a4af2005-08-30 00:54:01 +00002233
2234/*
2235** Call sqlite3ExprAnalyzeAggregates() for every expression in an
2236** expression list. Return the number of errors.
2237**
2238** If an error is found, the analysis is cut short.
2239*/
2240int sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){
2241 struct ExprList_item *pItem;
2242 int i;
2243 int nErr = 0;
2244 if( pList ){
2245 for(pItem=pList->a, i=0; nErr==0 && i<pList->nExpr; i++, pItem++){
2246 nErr += sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
2247 }
2248 }
2249 return nErr;
2250}