blob: 7ac83934938ec06e95315e8f7ff1dbf54ae2cb8c [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**
drh06f65412005-11-03 02:03:13 +000015** $Id: expr.c,v 1.234 2005/11/03 02:03:13 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(
drhffe07b22005-11-03 00:41:17 +0000805 Parse *pParse, /* The parsing context */
drh8141f612004-01-25 22:44:58 +0000806 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 ){
drhffe07b22005-11-03 00:41:17 +0000833 ExprList *pEList;
drh626a8792005-01-17 22:08:19 +0000834 SrcList *pSrcList = pNC->pSrcList;
drh8141f612004-01-25 22:44:58 +0000835
danielk1977b3bce662005-01-29 08:32:43 +0000836 if( pSrcList ){
837 for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
838 Table *pTab = pItem->pTab;
839 Column *pCol;
840
841 if( pTab==0 ) continue;
842 assert( pTab->nCol>0 );
843 if( zTab ){
844 if( pItem->zAlias ){
845 char *zTabName = pItem->zAlias;
846 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
847 }else{
848 char *zTabName = pTab->zName;
849 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
850 if( zDb!=0 && sqlite3StrICmp(db->aDb[pTab->iDb].zName, zDb)!=0 ){
851 continue;
852 }
drh626a8792005-01-17 22:08:19 +0000853 }
drh8141f612004-01-25 22:44:58 +0000854 }
danielk1977b3bce662005-01-29 08:32:43 +0000855 if( 0==(cntTab++) ){
drh626a8792005-01-17 22:08:19 +0000856 pExpr->iTable = pItem->iCursor;
drh626a8792005-01-17 22:08:19 +0000857 pExpr->iDb = pTab->iDb;
danielk1977b3bce662005-01-29 08:32:43 +0000858 pMatch = pItem;
859 }
860 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
861 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
drh873fac02005-06-06 17:11:46 +0000862 IdList *pUsing;
danielk1977b3bce662005-01-29 08:32:43 +0000863 cnt++;
864 pExpr->iTable = pItem->iCursor;
865 pMatch = pItem;
866 pExpr->iDb = pTab->iDb;
867 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
868 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
869 pExpr->affinity = pTab->aCol[j].affinity;
870 pExpr->pColl = pTab->aCol[j].pColl;
drh355ef362005-06-06 16:59:24 +0000871 if( pItem->jointype & JT_NATURAL ){
872 /* If this match occurred in the left table of a natural join,
873 ** then skip the right table to avoid a duplicate match */
874 pItem++;
875 i++;
876 }
drh873fac02005-06-06 17:11:46 +0000877 if( (pUsing = pItem->pUsing)!=0 ){
878 /* If this match occurs on a column that is in the USING clause
879 ** of a join, skip the search of the right table of the join
880 ** to avoid a duplicate match there. */
881 int k;
882 for(k=0; k<pUsing->nId; k++){
883 if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ){
884 pItem++;
885 i++;
886 break;
887 }
888 }
889 }
danielk1977b3bce662005-01-29 08:32:43 +0000890 break;
891 }
drh8141f612004-01-25 22:44:58 +0000892 }
893 }
894 }
drh626a8792005-01-17 22:08:19 +0000895
896#ifndef SQLITE_OMIT_TRIGGER
897 /* If we have not already resolved the name, then maybe
898 ** it is a new.* or old.* trigger argument reference
899 */
900 if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
901 TriggerStack *pTriggerStack = pParse->trigStack;
902 Table *pTab = 0;
903 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){
904 pExpr->iTable = pTriggerStack->newIdx;
905 assert( pTriggerStack->pTab );
906 pTab = pTriggerStack->pTab;
907 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab)==0 ){
908 pExpr->iTable = pTriggerStack->oldIdx;
909 assert( pTriggerStack->pTab );
910 pTab = pTriggerStack->pTab;
911 }
912
913 if( pTab ){
914 int j;
915 Column *pCol = pTab->aCol;
916
917 pExpr->iDb = pTab->iDb;
918 cntTab++;
919 for(j=0; j < pTab->nCol; j++, pCol++) {
920 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
921 cnt++;
922 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
923 pExpr->affinity = pTab->aCol[j].affinity;
924 pExpr->pColl = pTab->aCol[j].pColl;
danielk1977aee18ef2005-03-09 12:26:50 +0000925 pExpr->pTab = pTab;
drh626a8792005-01-17 22:08:19 +0000926 break;
927 }
928 }
929 }
930 }
drhb7f91642004-10-31 02:22:47 +0000931#endif /* !defined(SQLITE_OMIT_TRIGGER) */
drh8141f612004-01-25 22:44:58 +0000932
drh626a8792005-01-17 22:08:19 +0000933 /*
934 ** Perhaps the name is a reference to the ROWID
935 */
936 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
937 cnt = 1;
938 pExpr->iColumn = -1;
drh8df447f2005-11-01 15:48:24 +0000939 pExpr->affinity = SQLITE_AFF_NUMERIC;
drh626a8792005-01-17 22:08:19 +0000940 }
drh8141f612004-01-25 22:44:58 +0000941
drh626a8792005-01-17 22:08:19 +0000942 /*
943 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
944 ** might refer to an result-set alias. This happens, for example, when
945 ** we are resolving names in the WHERE clause of the following command:
946 **
947 ** SELECT a+b AS x FROM table WHERE x<10;
948 **
949 ** In cases like this, replace pExpr with a copy of the expression that
950 ** forms the result set entry ("a+b" in the example) and return immediately.
951 ** Note that the expression in the result set should have already been
952 ** resolved by the time the WHERE clause is resolved.
953 */
drhffe07b22005-11-03 00:41:17 +0000954 if( cnt==0 && (pEList = pNC->pEList)!=0 && zTab==0 ){
drh626a8792005-01-17 22:08:19 +0000955 for(j=0; j<pEList->nExpr; j++){
956 char *zAs = pEList->a[j].zName;
957 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
958 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
959 pExpr->op = TK_AS;
960 pExpr->iColumn = j;
961 pExpr->pLeft = sqlite3ExprDup(pEList->a[j].pExpr);
drh15ccce12005-05-23 15:06:39 +0000962 cnt = 1;
drh626a8792005-01-17 22:08:19 +0000963 assert( zTab==0 && zDb==0 );
drh15ccce12005-05-23 15:06:39 +0000964 goto lookupname_end_2;
drh626a8792005-01-17 22:08:19 +0000965 }
966 }
967 }
968
969 /* Advance to the next name context. The loop will exit when either
970 ** we have a match (cnt>0) or when we run out of name contexts.
971 */
972 if( cnt==0 ){
973 pNC = pNC->pNext;
974 }
drh8141f612004-01-25 22:44:58 +0000975 }
976
977 /*
978 ** If X and Y are NULL (in other words if only the column name Z is
979 ** supplied) and the value of Z is enclosed in double-quotes, then
980 ** Z is a string literal if it doesn't match any column names. In that
981 ** case, we need to return right away and not make any changes to
982 ** pExpr.
drh15ccce12005-05-23 15:06:39 +0000983 **
984 ** Because no reference was made to outer contexts, the pNC->nRef
985 ** fields are not changed in any context.
drh8141f612004-01-25 22:44:58 +0000986 */
987 if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
988 sqliteFree(zCol);
989 return 0;
990 }
991
992 /*
993 ** cnt==0 means there was not match. cnt>1 means there were two or
994 ** more matches. Either way, we have an error.
995 */
996 if( cnt!=1 ){
997 char *z = 0;
998 char *zErr;
999 zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s";
1000 if( zDb ){
danielk19774adee202004-05-08 08:23:19 +00001001 sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, 0);
drh8141f612004-01-25 22:44:58 +00001002 }else if( zTab ){
danielk19774adee202004-05-08 08:23:19 +00001003 sqlite3SetString(&z, zTab, ".", zCol, 0);
drh8141f612004-01-25 22:44:58 +00001004 }else{
1005 z = sqliteStrDup(zCol);
1006 }
danielk19774adee202004-05-08 08:23:19 +00001007 sqlite3ErrorMsg(pParse, zErr, z);
drh8141f612004-01-25 22:44:58 +00001008 sqliteFree(z);
drh73b211a2005-01-18 04:00:42 +00001009 pTopNC->nErr++;
drh8141f612004-01-25 22:44:58 +00001010 }
1011
drh51669862004-12-18 18:40:26 +00001012 /* If a column from a table in pSrcList is referenced, then record
1013 ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes
1014 ** bit 0 to be set. Column 1 sets bit 1. And so forth. If the
1015 ** column number is greater than the number of bits in the bitmask
1016 ** then set the high-order bit of the bitmask.
1017 */
1018 if( pExpr->iColumn>=0 && pMatch!=0 ){
1019 int n = pExpr->iColumn;
1020 if( n>=sizeof(Bitmask)*8 ){
1021 n = sizeof(Bitmask)*8-1;
1022 }
1023 assert( pMatch->iCursor==pExpr->iTable );
1024 pMatch->colUsed |= 1<<n;
1025 }
1026
danielk1977d5d56522005-03-16 12:15:20 +00001027lookupname_end:
drh8141f612004-01-25 22:44:58 +00001028 /* Clean up and return
1029 */
1030 sqliteFree(zDb);
1031 sqliteFree(zTab);
danielk19774adee202004-05-08 08:23:19 +00001032 sqlite3ExprDelete(pExpr->pLeft);
drh8141f612004-01-25 22:44:58 +00001033 pExpr->pLeft = 0;
danielk19774adee202004-05-08 08:23:19 +00001034 sqlite3ExprDelete(pExpr->pRight);
drh8141f612004-01-25 22:44:58 +00001035 pExpr->pRight = 0;
1036 pExpr->op = TK_COLUMN;
drh15ccce12005-05-23 15:06:39 +00001037lookupname_end_2:
1038 sqliteFree(zCol);
drh626a8792005-01-17 22:08:19 +00001039 if( cnt==1 ){
danielk1977b3bce662005-01-29 08:32:43 +00001040 assert( pNC!=0 );
drh626a8792005-01-17 22:08:19 +00001041 sqlite3AuthRead(pParse, pExpr, pNC->pSrcList);
danielk1977aee18ef2005-03-09 12:26:50 +00001042 if( pMatch && !pMatch->pSelect ){
1043 pExpr->pTab = pMatch->pTab;
1044 }
drh15ccce12005-05-23 15:06:39 +00001045 /* Increment the nRef value on all name contexts from TopNC up to
1046 ** the point where the name matched. */
1047 for(;;){
1048 assert( pTopNC!=0 );
1049 pTopNC->nRef++;
1050 if( pTopNC==pNC ) break;
1051 pTopNC = pTopNC->pNext;
1052 }
1053 return 0;
1054 } else {
1055 return 1;
drh626a8792005-01-17 22:08:19 +00001056 }
drh8141f612004-01-25 22:44:58 +00001057}
1058
1059/*
drh626a8792005-01-17 22:08:19 +00001060** This routine is designed as an xFunc for walkExprTree().
1061**
drh73b211a2005-01-18 04:00:42 +00001062** Resolve symbolic names into TK_COLUMN operators for the current
drh626a8792005-01-17 22:08:19 +00001063** node in the expression tree. Return 0 to continue the search down
drh73b211a2005-01-18 04:00:42 +00001064** the tree or 2 to abort the tree walk.
1065**
1066** This routine also does error checking and name resolution for
1067** function names. The operator for aggregate functions is changed
1068** to TK_AGG_FUNCTION.
drh626a8792005-01-17 22:08:19 +00001069*/
1070static int nameResolverStep(void *pArg, Expr *pExpr){
1071 NameContext *pNC = (NameContext*)pArg;
1072 SrcList *pSrcList;
1073 Parse *pParse;
drh626a8792005-01-17 22:08:19 +00001074
danielk1977b3bce662005-01-29 08:32:43 +00001075 if( pExpr==0 ) return 1;
drh626a8792005-01-17 22:08:19 +00001076 assert( pNC!=0 );
1077 pSrcList = pNC->pSrcList;
1078 pParse = pNC->pParse;
danielk1977b3bce662005-01-29 08:32:43 +00001079
drh626a8792005-01-17 22:08:19 +00001080 if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return 1;
1081 ExprSetProperty(pExpr, EP_Resolved);
1082#ifndef NDEBUG
drhffe07b22005-11-03 00:41:17 +00001083 if( pSrcList && pSrcList->nAlloc>0 ){
danielk1977940fac92005-01-23 22:41:37 +00001084 int i;
drh626a8792005-01-17 22:08:19 +00001085 for(i=0; i<pSrcList->nSrc; i++){
1086 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
1087 }
1088 }
1089#endif
1090 switch( pExpr->op ){
1091 /* Double-quoted strings (ex: "abc") are used as identifiers if
1092 ** possible. Otherwise they remain as strings. Single-quoted
1093 ** strings (ex: 'abc') are always string literals.
1094 */
1095 case TK_STRING: {
1096 if( pExpr->token.z[0]=='\'' ) break;
1097 /* Fall thru into the TK_ID case if this is a double-quoted string */
1098 }
1099 /* A lone identifier is the name of a column.
1100 */
1101 case TK_ID: {
drh626a8792005-01-17 22:08:19 +00001102 lookupName(pParse, 0, 0, &pExpr->token, pNC, pExpr);
1103 return 1;
1104 }
1105
1106 /* A table name and column name: ID.ID
1107 ** Or a database, table and column: ID.ID.ID
1108 */
1109 case TK_DOT: {
1110 Token *pColumn;
1111 Token *pTable;
1112 Token *pDb;
1113 Expr *pRight;
1114
danielk1977b3bce662005-01-29 08:32:43 +00001115 /* if( pSrcList==0 ) break; */
drh626a8792005-01-17 22:08:19 +00001116 pRight = pExpr->pRight;
1117 if( pRight->op==TK_ID ){
1118 pDb = 0;
1119 pTable = &pExpr->pLeft->token;
1120 pColumn = &pRight->token;
1121 }else{
1122 assert( pRight->op==TK_DOT );
1123 pDb = &pExpr->pLeft->token;
1124 pTable = &pRight->pLeft->token;
1125 pColumn = &pRight->pRight->token;
1126 }
1127 lookupName(pParse, pDb, pTable, pColumn, pNC, pExpr);
1128 return 1;
1129 }
1130
1131 /* Resolve function names
1132 */
drhb71090f2005-05-23 17:26:51 +00001133 case TK_CONST_FUNC:
drh626a8792005-01-17 22:08:19 +00001134 case TK_FUNCTION: {
1135 ExprList *pList = pExpr->pList; /* The argument list */
1136 int n = pList ? pList->nExpr : 0; /* Number of arguments */
1137 int no_such_func = 0; /* True if no such function exists */
1138 int wrong_num_args = 0; /* True if wrong number of arguments */
1139 int is_agg = 0; /* True if is an aggregate function */
1140 int i;
1141 int nId; /* Number of characters in function name */
1142 const char *zId; /* The function name. */
drh73b211a2005-01-18 04:00:42 +00001143 FuncDef *pDef; /* Information about the function */
1144 int enc = pParse->db->enc; /* The database encoding */
drh626a8792005-01-17 22:08:19 +00001145
drhb71090f2005-05-23 17:26:51 +00001146 zId = pExpr->token.z;
1147 nId = pExpr->token.n;
drh626a8792005-01-17 22:08:19 +00001148 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
1149 if( pDef==0 ){
1150 pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
1151 if( pDef==0 ){
1152 no_such_func = 1;
1153 }else{
1154 wrong_num_args = 1;
1155 }
1156 }else{
1157 is_agg = pDef->xFunc==0;
1158 }
1159 if( is_agg && !pNC->allowAgg ){
1160 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
1161 pNC->nErr++;
1162 is_agg = 0;
1163 }else if( no_such_func ){
1164 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
1165 pNC->nErr++;
1166 }else if( wrong_num_args ){
1167 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
1168 nId, zId);
1169 pNC->nErr++;
1170 }
1171 if( is_agg ){
1172 pExpr->op = TK_AGG_FUNCTION;
1173 pNC->hasAgg = 1;
1174 }
drh73b211a2005-01-18 04:00:42 +00001175 if( is_agg ) pNC->allowAgg = 0;
drh626a8792005-01-17 22:08:19 +00001176 for(i=0; pNC->nErr==0 && i<n; i++){
drh73b211a2005-01-18 04:00:42 +00001177 walkExprTree(pList->a[i].pExpr, nameResolverStep, pNC);
drh626a8792005-01-17 22:08:19 +00001178 }
drh73b211a2005-01-18 04:00:42 +00001179 if( is_agg ) pNC->allowAgg = 1;
drh626a8792005-01-17 22:08:19 +00001180 /* FIX ME: Compute pExpr->affinity based on the expected return
1181 ** type of the function
1182 */
1183 return is_agg;
1184 }
danielk1977b3bce662005-01-29 08:32:43 +00001185#ifndef SQLITE_OMIT_SUBQUERY
1186 case TK_SELECT:
1187 case TK_EXISTS:
1188#endif
1189 case TK_IN: {
1190 if( pExpr->pSelect ){
drh06f65412005-11-03 02:03:13 +00001191#ifndef SQLITE_OMIT_CHECK
1192 if( pNC->isCheck ){
1193 sqlite3ErrorMsg(pParse,"subqueries prohibited in CHECK constraints");
1194 }
1195#endif
danielk1977b3bce662005-01-29 08:32:43 +00001196 int nRef = pNC->nRef;
1197 sqlite3SelectResolve(pParse, pExpr->pSelect, pNC);
1198 assert( pNC->nRef>=nRef );
1199 if( nRef!=pNC->nRef ){
1200 ExprSetProperty(pExpr, EP_VarSelect);
1201 }
1202 }
1203 }
drh626a8792005-01-17 22:08:19 +00001204 }
1205 return 0;
1206}
1207
1208/*
drhcce7d172000-05-31 15:34:51 +00001209** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +00001210** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +00001211** index to the table in the table list and a column offset. The
1212** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
1213** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +00001214** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +00001215** VDBE cursor number for a cursor that is pointing into the referenced
1216** table. The Expr.iColumn value is changed to the index of the column
1217** of the referenced table. The Expr.iColumn value for the special
1218** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
1219** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +00001220**
drh626a8792005-01-17 22:08:19 +00001221** Also resolve function names and check the functions for proper
1222** usage. Make sure all function names are recognized and all functions
1223** have the correct number of arguments. Leave an error message
1224** in pParse->zErrMsg if anything is amiss. Return the number of errors.
1225**
drh73b211a2005-01-18 04:00:42 +00001226** If the expression contains aggregate functions then set the EP_Agg
1227** property on the expression.
drh626a8792005-01-17 22:08:19 +00001228*/
1229int sqlite3ExprResolveNames(
danielk1977b3bce662005-01-29 08:32:43 +00001230 NameContext *pNC, /* Namespace to resolve expressions in. */
1231 Expr *pExpr /* The expression to be analyzed. */
drh626a8792005-01-17 22:08:19 +00001232){
drh13449892005-09-07 21:22:45 +00001233 int savedHasAgg;
drh73b211a2005-01-18 04:00:42 +00001234 if( pExpr==0 ) return 0;
drh13449892005-09-07 21:22:45 +00001235 savedHasAgg = pNC->hasAgg;
1236 pNC->hasAgg = 0;
danielk1977b3bce662005-01-29 08:32:43 +00001237 walkExprTree(pExpr, nameResolverStep, pNC);
1238 if( pNC->nErr>0 ){
drh73b211a2005-01-18 04:00:42 +00001239 ExprSetProperty(pExpr, EP_Error);
drh73b211a2005-01-18 04:00:42 +00001240 }
drh13449892005-09-07 21:22:45 +00001241 if( pNC->hasAgg ){
1242 ExprSetProperty(pExpr, EP_Agg);
1243 }else if( savedHasAgg ){
1244 pNC->hasAgg = 1;
1245 }
drh73b211a2005-01-18 04:00:42 +00001246 return ExprHasProperty(pExpr, EP_Error);
drh626a8792005-01-17 22:08:19 +00001247}
1248
drh1398ad32005-01-19 23:24:50 +00001249/*
1250** A pointer instance of this structure is used to pass information
1251** through walkExprTree into codeSubqueryStep().
1252*/
1253typedef struct QueryCoder QueryCoder;
1254struct QueryCoder {
1255 Parse *pParse; /* The parsing context */
1256 NameContext *pNC; /* Namespace of first enclosing query */
1257};
1258
drh626a8792005-01-17 22:08:19 +00001259
1260/*
1261** Generate code for subqueries and IN operators.
1262**
drh73b211a2005-01-18 04:00:42 +00001263** IN operators comes in two forms:
drhfef52082000-06-06 01:50:43 +00001264**
1265** expr IN (exprlist)
1266** and
1267** expr IN (SELECT ...)
1268**
1269** The first form is handled by creating a set holding the list
1270** of allowed values. The second form causes the SELECT to generate
1271** a temporary table.
drhcce7d172000-05-31 15:34:51 +00001272*/
drh51522cd2005-01-20 13:36:19 +00001273#ifndef SQLITE_OMIT_SUBQUERY
danielk1977b3bce662005-01-29 08:32:43 +00001274void sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){
drh57dbd7b2005-07-08 18:25:26 +00001275 int testAddr = 0; /* One-time test address */
danielk1977b3bce662005-01-29 08:32:43 +00001276 Vdbe *v = sqlite3GetVdbe(pParse);
1277 if( v==0 ) return;
1278
drh57dbd7b2005-07-08 18:25:26 +00001279 /* This code must be run in its entirety every time it is encountered
1280 ** if any of the following is true:
1281 **
1282 ** * The right-hand side is a correlated subquery
1283 ** * The right-hand side is an expression list containing variables
1284 ** * We are inside a trigger
1285 **
1286 ** If all of the above are false, then we can run this code just once
1287 ** save the results, and reuse the same result on subsequent invocations.
danielk1977b3bce662005-01-29 08:32:43 +00001288 */
1289 if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->trigStack ){
1290 int mem = pParse->nMem++;
1291 sqlite3VdbeAddOp(v, OP_MemLoad, mem, 0);
drh57dbd7b2005-07-08 18:25:26 +00001292 testAddr = sqlite3VdbeAddOp(v, OP_If, 0, 0);
drh53f733c2005-09-16 02:38:09 +00001293 assert( testAddr>0 || sqlite3_malloc_failed );
drhd654be82005-09-20 17:42:23 +00001294 sqlite3VdbeAddOp(v, OP_MemInt, 1, mem);
danielk1977b3bce662005-01-29 08:32:43 +00001295 }
1296
drhcce7d172000-05-31 15:34:51 +00001297 switch( pExpr->op ){
drhfef52082000-06-06 01:50:43 +00001298 case TK_IN: {
danielk1977e014a832004-05-17 10:48:57 +00001299 char affinity;
drhd3d39e92004-05-20 22:16:29 +00001300 KeyInfo keyInfo;
drh9170dd72005-07-08 17:13:46 +00001301 int addr; /* Address of OP_OpenVirtual instruction */
drhd3d39e92004-05-20 22:16:29 +00001302
danielk1977bf3b7212004-05-18 10:06:24 +00001303 affinity = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +00001304
1305 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
drh57dbd7b2005-07-08 18:25:26 +00001306 ** expression it is handled the same way. A virtual table is
danielk1977e014a832004-05-17 10:48:57 +00001307 ** filled with single-field index keys representing the results
1308 ** from the SELECT or the <exprlist>.
1309 **
1310 ** If the 'x' expression is a column value, or the SELECT...
1311 ** statement returns a column value, then the affinity of that
1312 ** column is used to build the index keys. If both 'x' and the
1313 ** SELECT... statement are columns, then numeric affinity is used
1314 ** if either column has NUMERIC or INTEGER affinity. If neither
1315 ** 'x' nor the SELECT... statement are columns, then numeric affinity
1316 ** is used.
1317 */
1318 pExpr->iTable = pParse->nTab++;
drh9170dd72005-07-08 17:13:46 +00001319 addr = sqlite3VdbeAddOp(v, OP_OpenVirtual, pExpr->iTable, 0);
drhd3d39e92004-05-20 22:16:29 +00001320 memset(&keyInfo, 0, sizeof(keyInfo));
1321 keyInfo.nField = 1;
drhf3218fe2004-05-28 08:21:02 +00001322 sqlite3VdbeAddOp(v, OP_SetNumColumns, pExpr->iTable, 1);
danielk1977e014a832004-05-17 10:48:57 +00001323
drhfef52082000-06-06 01:50:43 +00001324 if( pExpr->pSelect ){
1325 /* Case 1: expr IN (SELECT ...)
1326 **
danielk1977e014a832004-05-17 10:48:57 +00001327 ** Generate code to write the results of the select into the temporary
1328 ** table allocated and opened above.
drhfef52082000-06-06 01:50:43 +00001329 */
danielk1977e014a832004-05-17 10:48:57 +00001330 int iParm = pExpr->iTable + (((int)affinity)<<16);
drhbe5c89a2004-07-26 00:31:09 +00001331 ExprList *pEList;
danielk1977e014a832004-05-17 10:48:57 +00001332 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
danielk1977b3bce662005-01-29 08:32:43 +00001333 sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001334 pEList = pExpr->pSelect->pEList;
1335 if( pEList && pEList->nExpr>0 ){
danielk19777cedc8d2004-06-10 10:50:08 +00001336 keyInfo.aColl[0] = binaryCompareCollSeq(pParse, pExpr->pLeft,
drhbe5c89a2004-07-26 00:31:09 +00001337 pEList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001338 }
drhfef52082000-06-06 01:50:43 +00001339 }else if( pExpr->pList ){
1340 /* Case 2: expr IN (exprlist)
1341 **
danielk1977e014a832004-05-17 10:48:57 +00001342 ** For each expression, build an index key from the evaluation and
1343 ** store it in the temporary table. If <expr> is a column, then use
1344 ** that columns affinity when building index keys. If <expr> is not
1345 ** a column, use numeric affinity.
drhfef52082000-06-06 01:50:43 +00001346 */
danielk1977e014a832004-05-17 10:48:57 +00001347 int i;
drh57dbd7b2005-07-08 18:25:26 +00001348 ExprList *pList = pExpr->pList;
1349 struct ExprList_item *pItem;
1350
danielk1977e014a832004-05-17 10:48:57 +00001351 if( !affinity ){
1352 affinity = SQLITE_AFF_NUMERIC;
1353 }
danielk19770202b292004-06-09 09:55:16 +00001354 keyInfo.aColl[0] = pExpr->pLeft->pColl;
danielk1977e014a832004-05-17 10:48:57 +00001355
1356 /* Loop through each expression in <exprlist>. */
drh57dbd7b2005-07-08 18:25:26 +00001357 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
1358 Expr *pE2 = pItem->pExpr;
danielk1977e014a832004-05-17 10:48:57 +00001359
drh57dbd7b2005-07-08 18:25:26 +00001360 /* If the expression is not constant then we will need to
1361 ** disable the test that was generated above that makes sure
1362 ** this code only executes once. Because for a non-constant
1363 ** expression we need to rerun this code each time.
1364 */
drh6c30be82005-07-29 15:10:17 +00001365 if( testAddr>0 && !sqlite3ExprIsConstant(pE2) ){
drh57dbd7b2005-07-08 18:25:26 +00001366 VdbeOp *aOp = sqlite3VdbeGetOp(v, testAddr-1);
1367 int i;
drhd654be82005-09-20 17:42:23 +00001368 for(i=0; i<3; i++){
drh57dbd7b2005-07-08 18:25:26 +00001369 aOp[i].opcode = OP_Noop;
1370 }
1371 testAddr = 0;
drh4794b982000-06-06 13:54:14 +00001372 }
danielk1977e014a832004-05-17 10:48:57 +00001373
1374 /* Evaluate the expression and insert it into the temp table */
1375 sqlite3ExprCode(pParse, pE2);
drh94a11212004-09-25 13:12:14 +00001376 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1);
drhf0863fe2005-06-12 21:35:51 +00001377 sqlite3VdbeAddOp(v, OP_IdxInsert, pExpr->iTable, 0);
drhfef52082000-06-06 01:50:43 +00001378 }
1379 }
danielk19770202b292004-06-09 09:55:16 +00001380 sqlite3VdbeChangeP3(v, addr, (void *)&keyInfo, P3_KEYINFO);
danielk1977b3bce662005-01-29 08:32:43 +00001381 break;
drhfef52082000-06-06 01:50:43 +00001382 }
1383
drh51522cd2005-01-20 13:36:19 +00001384 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001385 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +00001386 /* This has to be a scalar SELECT. Generate code to put the
1387 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +00001388 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +00001389 */
drhec7429a2005-10-06 16:53:14 +00001390 static const Token one = { "1", 0, 1 };
drh51522cd2005-01-20 13:36:19 +00001391 Select *pSel;
drhec7429a2005-10-06 16:53:14 +00001392 int iMem;
1393 int sop;
drh1398ad32005-01-19 23:24:50 +00001394
drhec7429a2005-10-06 16:53:14 +00001395 pExpr->iColumn = iMem = pParse->nMem++;
drh51522cd2005-01-20 13:36:19 +00001396 pSel = pExpr->pSelect;
1397 if( pExpr->op==TK_SELECT ){
1398 sop = SRT_Mem;
drhec7429a2005-10-06 16:53:14 +00001399 sqlite3VdbeAddOp(v, OP_MemNull, iMem, 0);
1400 VdbeComment((v, "# Init subquery result"));
drh51522cd2005-01-20 13:36:19 +00001401 }else{
drh51522cd2005-01-20 13:36:19 +00001402 sop = SRT_Exists;
drhec7429a2005-10-06 16:53:14 +00001403 sqlite3VdbeAddOp(v, OP_MemInt, 0, iMem);
1404 VdbeComment((v, "# Init EXISTS result"));
drh51522cd2005-01-20 13:36:19 +00001405 }
drhec7429a2005-10-06 16:53:14 +00001406 sqlite3ExprDelete(pSel->pLimit);
1407 pSel->pLimit = sqlite3Expr(TK_INTEGER, 0, 0, &one);
1408 sqlite3Select(pParse, pSel, sop, iMem, 0, 0, 0, 0);
danielk1977b3bce662005-01-29 08:32:43 +00001409 break;
drhcce7d172000-05-31 15:34:51 +00001410 }
1411 }
danielk1977b3bce662005-01-29 08:32:43 +00001412
drh57dbd7b2005-07-08 18:25:26 +00001413 if( testAddr ){
drhd654be82005-09-20 17:42:23 +00001414 sqlite3VdbeJumpHere(v, testAddr);
danielk1977b3bce662005-01-29 08:32:43 +00001415 }
1416 return;
drhcce7d172000-05-31 15:34:51 +00001417}
drh51522cd2005-01-20 13:36:19 +00001418#endif /* SQLITE_OMIT_SUBQUERY */
drhcce7d172000-05-31 15:34:51 +00001419
drhcce7d172000-05-31 15:34:51 +00001420/*
drhfec19aa2004-05-19 20:41:03 +00001421** Generate an instruction that will put the integer describe by
1422** text z[0..n-1] on the stack.
1423*/
1424static void codeInteger(Vdbe *v, const char *z, int n){
1425 int i;
drh6fec0762004-05-30 01:38:43 +00001426 if( sqlite3GetInt32(z, &i) ){
1427 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
1428 }else if( sqlite3FitsIn64Bits(z) ){
drh29dda4a2005-07-21 18:23:20 +00001429 sqlite3VdbeOp3(v, OP_Int64, 0, 0, z, n);
drhfec19aa2004-05-19 20:41:03 +00001430 }else{
1431 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n);
1432 }
1433}
1434
1435/*
drhcce7d172000-05-31 15:34:51 +00001436** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +00001437** expression and leave the result on the top of stack.
drhf2bc0132004-10-04 13:19:23 +00001438**
1439** This code depends on the fact that certain token values (ex: TK_EQ)
1440** are the same as opcode values (ex: OP_Eq) that implement the corresponding
1441** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
1442** the make process cause these values to align. Assert()s in the code
1443** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00001444*/
danielk19774adee202004-05-08 08:23:19 +00001445void sqlite3ExprCode(Parse *pParse, Expr *pExpr){
drhcce7d172000-05-31 15:34:51 +00001446 Vdbe *v = pParse->pVdbe;
1447 int op;
drhffe07b22005-11-03 00:41:17 +00001448 int stackChng = 1; /* Amount of change to stack depth */
1449
danielk19777977a172004-11-09 12:44:37 +00001450 if( v==0 ) return;
1451 if( pExpr==0 ){
drhf0863fe2005-06-12 21:35:51 +00001452 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
danielk19777977a172004-11-09 12:44:37 +00001453 return;
1454 }
drhf2bc0132004-10-04 13:19:23 +00001455 op = pExpr->op;
1456 switch( op ){
drh13449892005-09-07 21:22:45 +00001457 case TK_AGG_COLUMN: {
1458 AggInfo *pAggInfo = pExpr->pAggInfo;
1459 struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg];
1460 if( !pAggInfo->directMode ){
1461 sqlite3VdbeAddOp(v, OP_MemLoad, pCol->iMem, 0);
1462 break;
1463 }else if( pAggInfo->useSortingIdx ){
1464 sqlite3VdbeAddOp(v, OP_Column, pAggInfo->sortingIdx,
1465 pCol->iSorterColumn);
1466 break;
1467 }
1468 /* Otherwise, fall thru into the TK_COLUMN case */
1469 }
drh967e8b72000-06-21 13:59:10 +00001470 case TK_COLUMN: {
drhffe07b22005-11-03 00:41:17 +00001471 if( pExpr->iTable<0 ){
1472 /* This only happens when coding check constraints */
1473 assert( pParse->ckOffset>0 );
1474 sqlite3VdbeAddOp(v, OP_Dup, pParse->ckOffset-pExpr->iColumn-1, 1);
1475 }else if( pExpr->iColumn>=0 ){
danielk19774adee202004-05-08 08:23:19 +00001476 sqlite3VdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn);
danielk1977aee18ef2005-03-09 12:26:50 +00001477 sqlite3ColumnDefault(v, pExpr->pTab, pExpr->iColumn);
drhc4a3c772001-04-04 11:48:57 +00001478 }else{
drhf0863fe2005-06-12 21:35:51 +00001479 sqlite3VdbeAddOp(v, OP_Rowid, pExpr->iTable, 0);
drh22827922000-06-06 17:27:05 +00001480 }
drhcce7d172000-05-31 15:34:51 +00001481 break;
1482 }
1483 case TK_INTEGER: {
drhfec19aa2004-05-19 20:41:03 +00001484 codeInteger(v, pExpr->token.z, pExpr->token.n);
1485 break;
1486 }
1487 case TK_FLOAT:
1488 case TK_STRING: {
drhf2bc0132004-10-04 13:19:23 +00001489 assert( TK_FLOAT==OP_Real );
1490 assert( TK_STRING==OP_String8 );
drhd2687b72005-08-12 22:56:09 +00001491 sqlite3DequoteExpr(pExpr);
drhfec19aa2004-05-19 20:41:03 +00001492 sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z, pExpr->token.n);
drhcce7d172000-05-31 15:34:51 +00001493 break;
1494 }
drhf0863fe2005-06-12 21:35:51 +00001495 case TK_NULL: {
1496 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
1497 break;
1498 }
danielk19775338a5f2005-01-20 13:03:10 +00001499#ifndef SQLITE_OMIT_BLOB_LITERAL
danielk1977c572ef72004-05-27 09:28:41 +00001500 case TK_BLOB: {
drh6c8c6ce2005-08-23 11:17:58 +00001501 int n;
1502 const char *z;
drhf2bc0132004-10-04 13:19:23 +00001503 assert( TK_BLOB==OP_HexBlob );
drh6c8c6ce2005-08-23 11:17:58 +00001504 n = pExpr->token.n - 3;
1505 z = pExpr->token.z + 2;
1506 assert( n>=0 );
1507 if( n==0 ){
1508 z = "";
1509 }
1510 sqlite3VdbeOp3(v, op, 0, 0, z, n);
danielk1977c572ef72004-05-27 09:28:41 +00001511 break;
1512 }
danielk19775338a5f2005-01-20 13:03:10 +00001513#endif
drh50457892003-09-06 01:10:47 +00001514 case TK_VARIABLE: {
danielk19774adee202004-05-08 08:23:19 +00001515 sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0);
drh895d7472004-08-20 16:02:39 +00001516 if( pExpr->token.n>1 ){
1517 sqlite3VdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
1518 }
drh50457892003-09-06 01:10:47 +00001519 break;
1520 }
drh4e0cff62004-11-05 05:10:28 +00001521 case TK_REGISTER: {
1522 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iTable, 0);
1523 break;
1524 }
drh487e2622005-06-25 18:42:14 +00001525#ifndef SQLITE_OMIT_CAST
1526 case TK_CAST: {
1527 /* Expressions of the form: CAST(pLeft AS token) */
1528 int aff, op;
1529 sqlite3ExprCode(pParse, pExpr->pLeft);
drh8df447f2005-11-01 15:48:24 +00001530 aff = sqlite3AffinityType(&pExpr->token, 1);
drh487e2622005-06-25 18:42:14 +00001531 switch( aff ){
1532 case SQLITE_AFF_INTEGER: op = OP_ToInt; break;
1533 case SQLITE_AFF_NUMERIC: op = OP_ToNumeric; break;
1534 case SQLITE_AFF_TEXT: op = OP_ToText; break;
1535 case SQLITE_AFF_NONE: op = OP_ToBlob; break;
1536 }
1537 sqlite3VdbeAddOp(v, op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001538 stackChng = 0;
drh487e2622005-06-25 18:42:14 +00001539 break;
1540 }
1541#endif /* SQLITE_OMIT_CAST */
drhc9b84a12002-06-20 11:36:48 +00001542 case TK_LT:
1543 case TK_LE:
1544 case TK_GT:
1545 case TK_GE:
1546 case TK_NE:
1547 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00001548 assert( TK_LT==OP_Lt );
1549 assert( TK_LE==OP_Le );
1550 assert( TK_GT==OP_Gt );
1551 assert( TK_GE==OP_Ge );
1552 assert( TK_EQ==OP_Eq );
1553 assert( TK_NE==OP_Ne );
danielk1977a37cdde2004-05-16 11:15:36 +00001554 sqlite3ExprCode(pParse, pExpr->pLeft);
1555 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001556 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001557 stackChng = -1;
danielk1977a37cdde2004-05-16 11:15:36 +00001558 break;
drhc9b84a12002-06-20 11:36:48 +00001559 }
drhcce7d172000-05-31 15:34:51 +00001560 case TK_AND:
1561 case TK_OR:
1562 case TK_PLUS:
1563 case TK_STAR:
1564 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00001565 case TK_REM:
1566 case TK_BITAND:
1567 case TK_BITOR:
drh17c40292004-07-21 02:53:29 +00001568 case TK_SLASH:
drhbf4133c2001-10-13 02:59:08 +00001569 case TK_LSHIFT:
drh855eb1c2004-08-31 13:45:11 +00001570 case TK_RSHIFT:
drh00400772000-06-16 20:51:26 +00001571 case TK_CONCAT: {
drhf2bc0132004-10-04 13:19:23 +00001572 assert( TK_AND==OP_And );
1573 assert( TK_OR==OP_Or );
1574 assert( TK_PLUS==OP_Add );
1575 assert( TK_MINUS==OP_Subtract );
1576 assert( TK_REM==OP_Remainder );
1577 assert( TK_BITAND==OP_BitAnd );
1578 assert( TK_BITOR==OP_BitOr );
1579 assert( TK_SLASH==OP_Divide );
1580 assert( TK_LSHIFT==OP_ShiftLeft );
1581 assert( TK_RSHIFT==OP_ShiftRight );
1582 assert( TK_CONCAT==OP_Concat );
danielk19774adee202004-05-08 08:23:19 +00001583 sqlite3ExprCode(pParse, pExpr->pLeft);
1584 sqlite3ExprCode(pParse, pExpr->pRight);
drh855eb1c2004-08-31 13:45:11 +00001585 sqlite3VdbeAddOp(v, op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001586 stackChng = -1;
drh00400772000-06-16 20:51:26 +00001587 break;
1588 }
drhcce7d172000-05-31 15:34:51 +00001589 case TK_UMINUS: {
drhfec19aa2004-05-19 20:41:03 +00001590 Expr *pLeft = pExpr->pLeft;
1591 assert( pLeft );
1592 if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
1593 Token *p = &pLeft->token;
drh6e142f52000-06-08 13:36:40 +00001594 char *z = sqliteMalloc( p->n + 2 );
1595 sprintf(z, "-%.*s", p->n, p->z);
drhfec19aa2004-05-19 20:41:03 +00001596 if( pLeft->op==TK_FLOAT ){
1597 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001598 }else{
drhfec19aa2004-05-19 20:41:03 +00001599 codeInteger(v, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001600 }
drh6e142f52000-06-08 13:36:40 +00001601 sqliteFree(z);
1602 break;
1603 }
drh1ccde152000-06-17 13:12:39 +00001604 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +00001605 }
drhbf4133c2001-10-13 02:59:08 +00001606 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00001607 case TK_NOT: {
drhf2bc0132004-10-04 13:19:23 +00001608 assert( TK_BITNOT==OP_BitNot );
1609 assert( TK_NOT==OP_Not );
danielk19774adee202004-05-08 08:23:19 +00001610 sqlite3ExprCode(pParse, pExpr->pLeft);
1611 sqlite3VdbeAddOp(v, op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001612 stackChng = 0;
drhcce7d172000-05-31 15:34:51 +00001613 break;
1614 }
1615 case TK_ISNULL:
1616 case TK_NOTNULL: {
1617 int dest;
drhf2bc0132004-10-04 13:19:23 +00001618 assert( TK_ISNULL==OP_IsNull );
1619 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00001620 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1621 sqlite3ExprCode(pParse, pExpr->pLeft);
1622 dest = sqlite3VdbeCurrentAddr(v) + 2;
1623 sqlite3VdbeAddOp(v, op, 1, dest);
1624 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);
drhffe07b22005-11-03 00:41:17 +00001625 stackChng = 0;
drhf2bc0132004-10-04 13:19:23 +00001626 break;
drhcce7d172000-05-31 15:34:51 +00001627 }
drh22827922000-06-06 17:27:05 +00001628 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00001629 AggInfo *pInfo = pExpr->pAggInfo;
1630 sqlite3VdbeAddOp(v, OP_MemLoad, pInfo->aFunc[pExpr->iAgg].iMem, 0);
drh22827922000-06-06 17:27:05 +00001631 break;
1632 }
drhb71090f2005-05-23 17:26:51 +00001633 case TK_CONST_FUNC:
drhcce7d172000-05-31 15:34:51 +00001634 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00001635 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00001636 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00001637 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00001638 int nId;
1639 const char *zId;
drh13449892005-09-07 21:22:45 +00001640 int constMask = 0;
danielk1977682f68b2004-06-05 10:22:17 +00001641 int i;
danielk1977d8123362004-06-12 09:25:12 +00001642 u8 enc = pParse->db->enc;
danielk1977dc1bdc42004-06-11 10:51:27 +00001643 CollSeq *pColl = 0;
drhb71090f2005-05-23 17:26:51 +00001644 zId = pExpr->token.z;
1645 nId = pExpr->token.n;
danielk1977d8123362004-06-12 09:25:12 +00001646 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001647 assert( pDef!=0 );
drhf9b596e2004-05-26 16:54:42 +00001648 nExpr = sqlite3ExprCodeExprList(pParse, pList);
danielk1977682f68b2004-06-05 10:22:17 +00001649 for(i=0; i<nExpr && i<32; i++){
danielk1977d02eb1f2004-06-06 09:44:03 +00001650 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
drh13449892005-09-07 21:22:45 +00001651 constMask |= (1<<i);
danielk1977d02eb1f2004-06-06 09:44:03 +00001652 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001653 if( pDef->needCollSeq && !pColl ){
1654 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
1655 }
1656 }
1657 if( pDef->needCollSeq ){
1658 if( !pColl ) pColl = pParse->db->pDfltColl;
danielk1977d8123362004-06-12 09:25:12 +00001659 sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ);
danielk1977682f68b2004-06-05 10:22:17 +00001660 }
drh13449892005-09-07 21:22:45 +00001661 sqlite3VdbeOp3(v, OP_Function, constMask, nExpr, (char*)pDef, P3_FUNCDEF);
drhffe07b22005-11-03 00:41:17 +00001662 stackChng = 1-nExpr;
drhcce7d172000-05-31 15:34:51 +00001663 break;
1664 }
drhfe2093d2005-01-20 22:48:47 +00001665#ifndef SQLITE_OMIT_SUBQUERY
1666 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001667 case TK_SELECT: {
danielk1977b3bce662005-01-29 08:32:43 +00001668 sqlite3CodeSubselect(pParse, pExpr);
danielk19774adee202004-05-08 08:23:19 +00001669 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drhad6d9462004-09-19 02:15:24 +00001670 VdbeComment((v, "# load subquery result"));
drh19a775c2000-06-05 18:54:46 +00001671 break;
1672 }
drhfef52082000-06-06 01:50:43 +00001673 case TK_IN: {
1674 int addr;
drh94a11212004-09-25 13:12:14 +00001675 char affinity;
danielk1977b3bce662005-01-29 08:32:43 +00001676 sqlite3CodeSubselect(pParse, pExpr);
danielk1977e014a832004-05-17 10:48:57 +00001677
1678 /* Figure out the affinity to use to create a key from the results
1679 ** of the expression. affinityStr stores a static string suitable for
danielk1977ededfd52004-06-17 07:53:01 +00001680 ** P3 of OP_MakeRecord.
danielk1977e014a832004-05-17 10:48:57 +00001681 */
drh94a11212004-09-25 13:12:14 +00001682 affinity = comparisonAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +00001683
danielk19774adee202004-05-08 08:23:19 +00001684 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
danielk1977e014a832004-05-17 10:48:57 +00001685
1686 /* Code the <expr> from "<expr> IN (...)". The temporary table
1687 ** pExpr->iTable contains the values that make up the (...) set.
1688 */
danielk19774adee202004-05-08 08:23:19 +00001689 sqlite3ExprCode(pParse, pExpr->pLeft);
1690 addr = sqlite3VdbeCurrentAddr(v);
danielk1977e014a832004-05-17 10:48:57 +00001691 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); /* addr + 0 */
danielk19774adee202004-05-08 08:23:19 +00001692 sqlite3VdbeAddOp(v, OP_Pop, 2, 0);
drhf0863fe2005-06-12 21:35:51 +00001693 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +00001694 sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7);
drh94a11212004-09-25 13:12:14 +00001695 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1); /* addr + 4 */
danielk1977e014a832004-05-17 10:48:57 +00001696 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7);
1697 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); /* addr + 6 */
1698
drhfef52082000-06-06 01:50:43 +00001699 break;
1700 }
danielk197793758c82005-01-21 08:13:14 +00001701#endif
drhfef52082000-06-06 01:50:43 +00001702 case TK_BETWEEN: {
drhbe5c89a2004-07-26 00:31:09 +00001703 Expr *pLeft = pExpr->pLeft;
1704 struct ExprList_item *pLItem = pExpr->pList->a;
1705 Expr *pRight = pLItem->pExpr;
1706 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001707 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001708 sqlite3ExprCode(pParse, pRight);
1709 codeCompare(pParse, pLeft, pRight, OP_Ge, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001710 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
drhbe5c89a2004-07-26 00:31:09 +00001711 pLItem++;
1712 pRight = pLItem->pExpr;
1713 sqlite3ExprCode(pParse, pRight);
1714 codeCompare(pParse, pLeft, pRight, OP_Le, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001715 sqlite3VdbeAddOp(v, OP_And, 0, 0);
drhfef52082000-06-06 01:50:43 +00001716 break;
1717 }
drh51e9a442004-01-16 16:42:53 +00001718 case TK_UPLUS:
drha2e00042002-01-22 03:13:42 +00001719 case TK_AS: {
danielk19774adee202004-05-08 08:23:19 +00001720 sqlite3ExprCode(pParse, pExpr->pLeft);
drhffe07b22005-11-03 00:41:17 +00001721 stackChng = 0;
drha2e00042002-01-22 03:13:42 +00001722 break;
1723 }
drh17a7f8d2002-03-24 13:13:27 +00001724 case TK_CASE: {
1725 int expr_end_label;
drhf5905aa2002-05-26 20:54:33 +00001726 int jumpInst;
drhf5905aa2002-05-26 20:54:33 +00001727 int nExpr;
drh17a7f8d2002-03-24 13:13:27 +00001728 int i;
drhbe5c89a2004-07-26 00:31:09 +00001729 ExprList *pEList;
1730 struct ExprList_item *aListelem;
drh17a7f8d2002-03-24 13:13:27 +00001731
1732 assert(pExpr->pList);
1733 assert((pExpr->pList->nExpr % 2) == 0);
1734 assert(pExpr->pList->nExpr > 0);
drhbe5c89a2004-07-26 00:31:09 +00001735 pEList = pExpr->pList;
1736 aListelem = pEList->a;
1737 nExpr = pEList->nExpr;
danielk19774adee202004-05-08 08:23:19 +00001738 expr_end_label = sqlite3VdbeMakeLabel(v);
drh17a7f8d2002-03-24 13:13:27 +00001739 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001740 sqlite3ExprCode(pParse, pExpr->pLeft);
drh17a7f8d2002-03-24 13:13:27 +00001741 }
drhf5905aa2002-05-26 20:54:33 +00001742 for(i=0; i<nExpr; i=i+2){
drhbe5c89a2004-07-26 00:31:09 +00001743 sqlite3ExprCode(pParse, aListelem[i].pExpr);
drh17a7f8d2002-03-24 13:13:27 +00001744 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001745 sqlite3VdbeAddOp(v, OP_Dup, 1, 1);
drhbe5c89a2004-07-26 00:31:09 +00001746 jumpInst = codeCompare(pParse, pExpr->pLeft, aListelem[i].pExpr,
1747 OP_Ne, 0, 1);
danielk19774adee202004-05-08 08:23:19 +00001748 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001749 }else{
danielk19774adee202004-05-08 08:23:19 +00001750 jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001751 }
drhbe5c89a2004-07-26 00:31:09 +00001752 sqlite3ExprCode(pParse, aListelem[i+1].pExpr);
danielk19774adee202004-05-08 08:23:19 +00001753 sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label);
drhd654be82005-09-20 17:42:23 +00001754 sqlite3VdbeJumpHere(v, jumpInst);
drh17a7f8d2002-03-24 13:13:27 +00001755 }
drhf570f012002-05-31 15:51:25 +00001756 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001757 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhf570f012002-05-31 15:51:25 +00001758 }
drh17a7f8d2002-03-24 13:13:27 +00001759 if( pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001760 sqlite3ExprCode(pParse, pExpr->pRight);
drh17a7f8d2002-03-24 13:13:27 +00001761 }else{
drhf0863fe2005-06-12 21:35:51 +00001762 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
drh17a7f8d2002-03-24 13:13:27 +00001763 }
danielk19774adee202004-05-08 08:23:19 +00001764 sqlite3VdbeResolveLabel(v, expr_end_label);
danielk19776f349032002-06-11 02:25:40 +00001765 break;
1766 }
danielk19775338a5f2005-01-20 13:03:10 +00001767#ifndef SQLITE_OMIT_TRIGGER
danielk19776f349032002-06-11 02:25:40 +00001768 case TK_RAISE: {
1769 if( !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +00001770 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00001771 "RAISE() may only be used within a trigger-program");
danielk19776f349032002-06-11 02:25:40 +00001772 return;
1773 }
drhad6d9462004-09-19 02:15:24 +00001774 if( pExpr->iColumn!=OE_Ignore ){
1775 assert( pExpr->iColumn==OE_Rollback ||
1776 pExpr->iColumn == OE_Abort ||
1777 pExpr->iColumn == OE_Fail );
drhd2687b72005-08-12 22:56:09 +00001778 sqlite3DequoteExpr(pExpr);
drhad6d9462004-09-19 02:15:24 +00001779 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn,
1780 pExpr->token.z, pExpr->token.n);
danielk19776f349032002-06-11 02:25:40 +00001781 } else {
drhad6d9462004-09-19 02:15:24 +00001782 assert( pExpr->iColumn == OE_Ignore );
1783 sqlite3VdbeAddOp(v, OP_ContextPop, 0, 0);
1784 sqlite3VdbeAddOp(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
1785 VdbeComment((v, "# raise(IGNORE)"));
danielk19776f349032002-06-11 02:25:40 +00001786 }
drhffe07b22005-11-03 00:41:17 +00001787 stackChng = 0;
1788 break;
drh17a7f8d2002-03-24 13:13:27 +00001789 }
danielk19775338a5f2005-01-20 13:03:10 +00001790#endif
drhffe07b22005-11-03 00:41:17 +00001791 }
1792
1793 if( pParse->ckOffset ){
1794 pParse->ckOffset += stackChng;
1795 assert( pParse->ckOffset );
drhcce7d172000-05-31 15:34:51 +00001796 }
drhcce7d172000-05-31 15:34:51 +00001797}
1798
danielk197793758c82005-01-21 08:13:14 +00001799#ifndef SQLITE_OMIT_TRIGGER
drhcce7d172000-05-31 15:34:51 +00001800/*
drh25303782004-12-07 15:41:48 +00001801** Generate code that evalutes the given expression and leaves the result
1802** on the stack. See also sqlite3ExprCode().
1803**
1804** This routine might also cache the result and modify the pExpr tree
1805** so that it will make use of the cached result on subsequent evaluations
1806** rather than evaluate the whole expression again. Trivial expressions are
1807** not cached. If the expression is cached, its result is stored in a
1808** memory location.
1809*/
1810void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr){
1811 Vdbe *v = pParse->pVdbe;
1812 int iMem;
1813 int addr1, addr2;
1814 if( v==0 ) return;
1815 addr1 = sqlite3VdbeCurrentAddr(v);
1816 sqlite3ExprCode(pParse, pExpr);
1817 addr2 = sqlite3VdbeCurrentAddr(v);
1818 if( addr2>addr1+1 || sqlite3VdbeGetOp(v, addr1)->opcode==OP_Function ){
1819 iMem = pExpr->iTable = pParse->nMem++;
1820 sqlite3VdbeAddOp(v, OP_MemStore, iMem, 0);
1821 pExpr->op = TK_REGISTER;
1822 }
1823}
danielk197793758c82005-01-21 08:13:14 +00001824#endif
drh25303782004-12-07 15:41:48 +00001825
1826/*
drh268380c2004-02-25 13:47:31 +00001827** Generate code that pushes the value of every element of the given
drhf9b596e2004-05-26 16:54:42 +00001828** expression list onto the stack.
drh268380c2004-02-25 13:47:31 +00001829**
1830** Return the number of elements pushed onto the stack.
1831*/
danielk19774adee202004-05-08 08:23:19 +00001832int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00001833 Parse *pParse, /* Parsing context */
drhf9b596e2004-05-26 16:54:42 +00001834 ExprList *pList /* The expression list to be coded */
drh268380c2004-02-25 13:47:31 +00001835){
1836 struct ExprList_item *pItem;
1837 int i, n;
drh268380c2004-02-25 13:47:31 +00001838 if( pList==0 ) return 0;
drh268380c2004-02-25 13:47:31 +00001839 n = pList->nExpr;
drhc182d162005-08-14 20:47:16 +00001840 for(pItem=pList->a, i=n; i>0; i--, pItem++){
danielk19774adee202004-05-08 08:23:19 +00001841 sqlite3ExprCode(pParse, pItem->pExpr);
drh268380c2004-02-25 13:47:31 +00001842 }
drhf9b596e2004-05-26 16:54:42 +00001843 return n;
drh268380c2004-02-25 13:47:31 +00001844}
1845
1846/*
drhcce7d172000-05-31 15:34:51 +00001847** Generate code for a boolean expression such that a jump is made
1848** to the label "dest" if the expression is true but execution
1849** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00001850**
1851** If the expression evaluates to NULL (neither true nor false), then
1852** take the jump if the jumpIfNull flag is true.
drhf2bc0132004-10-04 13:19:23 +00001853**
1854** This code depends on the fact that certain token values (ex: TK_EQ)
1855** are the same as opcode values (ex: OP_Eq) that implement the corresponding
1856** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
1857** the make process cause these values to align. Assert()s in the code
1858** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00001859*/
danielk19774adee202004-05-08 08:23:19 +00001860void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001861 Vdbe *v = pParse->pVdbe;
1862 int op = 0;
drhffe07b22005-11-03 00:41:17 +00001863 int ckOffset = pParse->ckOffset;
drhdaffd0e2001-04-11 14:28:42 +00001864 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00001865 op = pExpr->op;
1866 switch( op ){
drhcce7d172000-05-31 15:34:51 +00001867 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001868 int d2 = sqlite3VdbeMakeLabel(v);
1869 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
1870 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
1871 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001872 break;
1873 }
1874 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001875 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
1876 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001877 break;
1878 }
1879 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001880 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001881 break;
1882 }
1883 case TK_LT:
1884 case TK_LE:
1885 case TK_GT:
1886 case TK_GE:
1887 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00001888 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00001889 assert( TK_LT==OP_Lt );
1890 assert( TK_LE==OP_Le );
1891 assert( TK_GT==OP_Gt );
1892 assert( TK_GE==OP_Ge );
1893 assert( TK_EQ==OP_Eq );
1894 assert( TK_NE==OP_Ne );
danielk19774adee202004-05-08 08:23:19 +00001895 sqlite3ExprCode(pParse, pExpr->pLeft);
1896 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001897 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001898 break;
1899 }
1900 case TK_ISNULL:
1901 case TK_NOTNULL: {
drhf2bc0132004-10-04 13:19:23 +00001902 assert( TK_ISNULL==OP_IsNull );
1903 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00001904 sqlite3ExprCode(pParse, pExpr->pLeft);
1905 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001906 break;
1907 }
drhfef52082000-06-06 01:50:43 +00001908 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00001909 /* The expression "x BETWEEN y AND z" is implemented as:
1910 **
1911 ** 1 IF (x < y) GOTO 3
1912 ** 2 IF (x <= z) GOTO <dest>
1913 ** 3 ...
1914 */
drhf5905aa2002-05-26 20:54:33 +00001915 int addr;
drhbe5c89a2004-07-26 00:31:09 +00001916 Expr *pLeft = pExpr->pLeft;
1917 Expr *pRight = pExpr->pList->a[0].pExpr;
1918 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001919 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001920 sqlite3ExprCode(pParse, pRight);
1921 addr = codeCompare(pParse, pLeft, pRight, OP_Lt, 0, !jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00001922
drhbe5c89a2004-07-26 00:31:09 +00001923 pRight = pExpr->pList->a[1].pExpr;
1924 sqlite3ExprCode(pParse, pRight);
1925 codeCompare(pParse, pLeft, pRight, OP_Le, dest, jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00001926
danielk19774adee202004-05-08 08:23:19 +00001927 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
drhd654be82005-09-20 17:42:23 +00001928 sqlite3VdbeJumpHere(v, addr);
danielk19774adee202004-05-08 08:23:19 +00001929 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00001930 break;
1931 }
drhcce7d172000-05-31 15:34:51 +00001932 default: {
danielk19774adee202004-05-08 08:23:19 +00001933 sqlite3ExprCode(pParse, pExpr);
1934 sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001935 break;
1936 }
1937 }
drhffe07b22005-11-03 00:41:17 +00001938 pParse->ckOffset = ckOffset;
drhcce7d172000-05-31 15:34:51 +00001939}
1940
1941/*
drh66b89c82000-11-28 20:47:17 +00001942** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00001943** to the label "dest" if the expression is false but execution
1944** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00001945**
1946** If the expression evaluates to NULL (neither true nor false) then
1947** jump if jumpIfNull is true or fall through if jumpIfNull is false.
drhcce7d172000-05-31 15:34:51 +00001948*/
danielk19774adee202004-05-08 08:23:19 +00001949void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001950 Vdbe *v = pParse->pVdbe;
1951 int op = 0;
drhffe07b22005-11-03 00:41:17 +00001952 int ckOffset = pParse->ckOffset;
drhdaffd0e2001-04-11 14:28:42 +00001953 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00001954
1955 /* The value of pExpr->op and op are related as follows:
1956 **
1957 ** pExpr->op op
1958 ** --------- ----------
1959 ** TK_ISNULL OP_NotNull
1960 ** TK_NOTNULL OP_IsNull
1961 ** TK_NE OP_Eq
1962 ** TK_EQ OP_Ne
1963 ** TK_GT OP_Le
1964 ** TK_LE OP_Gt
1965 ** TK_GE OP_Lt
1966 ** TK_LT OP_Ge
1967 **
1968 ** For other values of pExpr->op, op is undefined and unused.
1969 ** The value of TK_ and OP_ constants are arranged such that we
1970 ** can compute the mapping above using the following expression.
1971 ** Assert()s verify that the computation is correct.
1972 */
1973 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
1974
1975 /* Verify correct alignment of TK_ and OP_ constants
1976 */
1977 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
1978 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
1979 assert( pExpr->op!=TK_NE || op==OP_Eq );
1980 assert( pExpr->op!=TK_EQ || op==OP_Ne );
1981 assert( pExpr->op!=TK_LT || op==OP_Ge );
1982 assert( pExpr->op!=TK_LE || op==OP_Gt );
1983 assert( pExpr->op!=TK_GT || op==OP_Le );
1984 assert( pExpr->op!=TK_GE || op==OP_Lt );
1985
drhcce7d172000-05-31 15:34:51 +00001986 switch( pExpr->op ){
1987 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001988 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
1989 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001990 break;
1991 }
1992 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001993 int d2 = sqlite3VdbeMakeLabel(v);
1994 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
1995 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
1996 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001997 break;
1998 }
1999 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00002000 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002001 break;
2002 }
2003 case TK_LT:
2004 case TK_LE:
2005 case TK_GT:
2006 case TK_GE:
2007 case TK_NE:
2008 case TK_EQ: {
danielk19774adee202004-05-08 08:23:19 +00002009 sqlite3ExprCode(pParse, pExpr->pLeft);
2010 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00002011 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002012 break;
2013 }
drhcce7d172000-05-31 15:34:51 +00002014 case TK_ISNULL:
2015 case TK_NOTNULL: {
danielk19774adee202004-05-08 08:23:19 +00002016 sqlite3ExprCode(pParse, pExpr->pLeft);
2017 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00002018 break;
2019 }
drhfef52082000-06-06 01:50:43 +00002020 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00002021 /* The expression is "x BETWEEN y AND z". It is implemented as:
2022 **
2023 ** 1 IF (x >= y) GOTO 3
2024 ** 2 GOTO <dest>
2025 ** 3 IF (x > z) GOTO <dest>
2026 */
drhfef52082000-06-06 01:50:43 +00002027 int addr;
drhbe5c89a2004-07-26 00:31:09 +00002028 Expr *pLeft = pExpr->pLeft;
2029 Expr *pRight = pExpr->pList->a[0].pExpr;
2030 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00002031 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00002032 sqlite3ExprCode(pParse, pRight);
danielk19774adee202004-05-08 08:23:19 +00002033 addr = sqlite3VdbeCurrentAddr(v);
drhbe5c89a2004-07-26 00:31:09 +00002034 codeCompare(pParse, pLeft, pRight, OP_Ge, addr+3, !jumpIfNull);
2035
danielk19774adee202004-05-08 08:23:19 +00002036 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
2037 sqlite3VdbeAddOp(v, OP_Goto, 0, dest);
drhbe5c89a2004-07-26 00:31:09 +00002038 pRight = pExpr->pList->a[1].pExpr;
2039 sqlite3ExprCode(pParse, pRight);
2040 codeCompare(pParse, pLeft, pRight, OP_Gt, dest, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00002041 break;
2042 }
drhcce7d172000-05-31 15:34:51 +00002043 default: {
danielk19774adee202004-05-08 08:23:19 +00002044 sqlite3ExprCode(pParse, pExpr);
2045 sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00002046 break;
2047 }
2048 }
drhffe07b22005-11-03 00:41:17 +00002049 pParse->ckOffset = ckOffset;
drhcce7d172000-05-31 15:34:51 +00002050}
drh22827922000-06-06 17:27:05 +00002051
2052/*
2053** Do a deep comparison of two expression trees. Return TRUE (non-zero)
2054** if they are identical and return FALSE if they differ in any way.
2055*/
danielk19774adee202004-05-08 08:23:19 +00002056int sqlite3ExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00002057 int i;
2058 if( pA==0 ){
2059 return pB==0;
2060 }else if( pB==0 ){
2061 return 0;
2062 }
2063 if( pA->op!=pB->op ) return 0;
drhfd357972005-09-09 01:33:19 +00002064 if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 0;
danielk19774adee202004-05-08 08:23:19 +00002065 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
2066 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00002067 if( pA->pList ){
2068 if( pB->pList==0 ) return 0;
2069 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
2070 for(i=0; i<pA->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00002071 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00002072 return 0;
2073 }
2074 }
2075 }else if( pB->pList ){
2076 return 0;
2077 }
2078 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00002079 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drh22827922000-06-06 17:27:05 +00002080 if( pA->token.z ){
2081 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00002082 if( pB->token.n!=pA->token.n ) return 0;
danielk19774adee202004-05-08 08:23:19 +00002083 if( sqlite3StrNICmp(pA->token.z, pB->token.z, pB->token.n)!=0 ) return 0;
drh22827922000-06-06 17:27:05 +00002084 }
2085 return 1;
2086}
2087
drh13449892005-09-07 21:22:45 +00002088
drh22827922000-06-06 17:27:05 +00002089/*
drh13449892005-09-07 21:22:45 +00002090** Add a new element to the pAggInfo->aCol[] array. Return the index of
2091** the new element. Return a negative number if malloc fails.
drh22827922000-06-06 17:27:05 +00002092*/
drh13449892005-09-07 21:22:45 +00002093static int addAggInfoColumn(AggInfo *pInfo){
2094 int i;
2095 i = sqlite3ArrayAllocate((void**)&pInfo->aCol, sizeof(pInfo->aCol[0]), 3);
2096 if( i<0 ){
2097 return -1;
drh22827922000-06-06 17:27:05 +00002098 }
drh13449892005-09-07 21:22:45 +00002099 return i;
2100}
2101
2102/*
2103** Add a new element to the pAggInfo->aFunc[] array. Return the index of
2104** the new element. Return a negative number if malloc fails.
2105*/
2106static int addAggInfoFunc(AggInfo *pInfo){
2107 int i;
2108 i = sqlite3ArrayAllocate((void**)&pInfo->aFunc, sizeof(pInfo->aFunc[0]), 2);
2109 if( i<0 ){
2110 return -1;
2111 }
2112 return i;
2113}
drh22827922000-06-06 17:27:05 +00002114
2115/*
drh626a8792005-01-17 22:08:19 +00002116** This is an xFunc for walkExprTree() used to implement
2117** sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates
2118** for additional information.
drh22827922000-06-06 17:27:05 +00002119**
drh626a8792005-01-17 22:08:19 +00002120** This routine analyzes the aggregate function at pExpr.
drh22827922000-06-06 17:27:05 +00002121*/
drh626a8792005-01-17 22:08:19 +00002122static int analyzeAggregate(void *pArg, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00002123 int i;
danielk1977a58fdfb2005-02-08 07:50:40 +00002124 NameContext *pNC = (NameContext *)pArg;
2125 Parse *pParse = pNC->pParse;
2126 SrcList *pSrcList = pNC->pSrcList;
drh13449892005-09-07 21:22:45 +00002127 AggInfo *pAggInfo = pNC->pAggInfo;
2128
drh22827922000-06-06 17:27:05 +00002129
drh22827922000-06-06 17:27:05 +00002130 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00002131 case TK_COLUMN: {
drh13449892005-09-07 21:22:45 +00002132 /* Check to see if the column is in one of the tables in the FROM
2133 ** clause of the aggregate query */
2134 if( pSrcList ){
2135 struct SrcList_item *pItem = pSrcList->a;
2136 for(i=0; i<pSrcList->nSrc; i++, pItem++){
2137 struct AggInfo_col *pCol;
2138 if( pExpr->iTable==pItem->iCursor ){
2139 /* If we reach this point, it means that pExpr refers to a table
2140 ** that is in the FROM clause of the aggregate query.
2141 **
2142 ** Make an entry for the column in pAggInfo->aCol[] if there
2143 ** is not an entry there already.
2144 */
2145 pCol = pAggInfo->aCol;
2146 for(i=0; i<pAggInfo->nColumn; i++, pCol++){
2147 if( pCol->iTable==pExpr->iTable &&
2148 pCol->iColumn==pExpr->iColumn ){
2149 break;
2150 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002151 }
drh13449892005-09-07 21:22:45 +00002152 if( i>=pAggInfo->nColumn && (i = addAggInfoColumn(pAggInfo))>=0 ){
2153 pCol = &pAggInfo->aCol[i];
2154 pCol->iTable = pExpr->iTable;
2155 pCol->iColumn = pExpr->iColumn;
2156 pCol->iMem = pParse->nMem++;
2157 pCol->iSorterColumn = -1;
drh5774b802005-09-07 22:48:16 +00002158 pCol->pExpr = pExpr;
drh13449892005-09-07 21:22:45 +00002159 if( pAggInfo->pGroupBy ){
2160 int j, n;
2161 ExprList *pGB = pAggInfo->pGroupBy;
2162 struct ExprList_item *pTerm = pGB->a;
2163 n = pGB->nExpr;
2164 for(j=0; j<n; j++, pTerm++){
2165 Expr *pE = pTerm->pExpr;
2166 if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable &&
2167 pE->iColumn==pExpr->iColumn ){
2168 pCol->iSorterColumn = j;
2169 break;
2170 }
2171 }
2172 }
2173 if( pCol->iSorterColumn<0 ){
2174 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
2175 }
2176 }
2177 /* There is now an entry for pExpr in pAggInfo->aCol[] (either
2178 ** because it was there before or because we just created it).
2179 ** Convert the pExpr to be a TK_AGG_COLUMN referring to that
2180 ** pAggInfo->aCol[] entry.
2181 */
2182 pExpr->pAggInfo = pAggInfo;
2183 pExpr->op = TK_AGG_COLUMN;
2184 pExpr->iAgg = i;
2185 break;
2186 } /* endif pExpr->iTable==pItem->iCursor */
2187 } /* end loop over pSrcList */
drh22827922000-06-06 17:27:05 +00002188 }
drh626a8792005-01-17 22:08:19 +00002189 return 1;
drh22827922000-06-06 17:27:05 +00002190 }
2191 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00002192 /* The pNC->nDepth==0 test causes aggregate functions in subqueries
2193 ** to be ignored */
danielk1977a58fdfb2005-02-08 07:50:40 +00002194 if( pNC->nDepth==0 ){
drh13449892005-09-07 21:22:45 +00002195 /* Check to see if pExpr is a duplicate of another aggregate
2196 ** function that is already in the pAggInfo structure
2197 */
2198 struct AggInfo_func *pItem = pAggInfo->aFunc;
2199 for(i=0; i<pAggInfo->nFunc; i++, pItem++){
2200 if( sqlite3ExprCompare(pItem->pExpr, pExpr) ){
danielk1977a58fdfb2005-02-08 07:50:40 +00002201 break;
2202 }
drh22827922000-06-06 17:27:05 +00002203 }
drh13449892005-09-07 21:22:45 +00002204 if( i>=pAggInfo->nFunc ){
2205 /* pExpr is original. Make a new entry in pAggInfo->aFunc[]
2206 */
danielk1977a58fdfb2005-02-08 07:50:40 +00002207 u8 enc = pParse->db->enc;
drh13449892005-09-07 21:22:45 +00002208 i = addAggInfoFunc(pAggInfo);
2209 if( i>=0 ){
2210 pItem = &pAggInfo->aFunc[i];
2211 pItem->pExpr = pExpr;
2212 pItem->iMem = pParse->nMem++;
2213 pItem->pFunc = sqlite3FindFunction(pParse->db,
2214 pExpr->token.z, pExpr->token.n,
2215 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
drhfd357972005-09-09 01:33:19 +00002216 if( pExpr->flags & EP_Distinct ){
2217 pItem->iDistinct = pParse->nTab++;
2218 }else{
2219 pItem->iDistinct = -1;
2220 }
drh13449892005-09-07 21:22:45 +00002221 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002222 }
drh13449892005-09-07 21:22:45 +00002223 /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
2224 */
danielk1977a58fdfb2005-02-08 07:50:40 +00002225 pExpr->iAgg = i;
drh13449892005-09-07 21:22:45 +00002226 pExpr->pAggInfo = pAggInfo;
danielk1977a58fdfb2005-02-08 07:50:40 +00002227 return 1;
drh22827922000-06-06 17:27:05 +00002228 }
drh22827922000-06-06 17:27:05 +00002229 }
2230 }
drh13449892005-09-07 21:22:45 +00002231
2232 /* Recursively walk subqueries looking for TK_COLUMN nodes that need
2233 ** to be changed to TK_AGG_COLUMN. But increment nDepth so that
2234 ** TK_AGG_FUNCTION nodes in subqueries will be unchanged.
2235 */
danielk1977a58fdfb2005-02-08 07:50:40 +00002236 if( pExpr->pSelect ){
2237 pNC->nDepth++;
2238 walkSelectExpr(pExpr->pSelect, analyzeAggregate, pNC);
2239 pNC->nDepth--;
2240 }
drh626a8792005-01-17 22:08:19 +00002241 return 0;
2242}
2243
2244/*
2245** Analyze the given expression looking for aggregate functions and
2246** for variables that need to be added to the pParse->aAgg[] array.
2247** Make additional entries to the pParse->aAgg[] array as necessary.
2248**
2249** This routine should only be called after the expression has been
2250** analyzed by sqlite3ExprResolveNames().
2251**
2252** If errors are seen, leave an error message in zErrMsg and return
2253** the number of errors.
2254*/
danielk1977a58fdfb2005-02-08 07:50:40 +00002255int sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
2256 int nErr = pNC->pParse->nErr;
2257 walkExprTree(pExpr, analyzeAggregate, pNC);
2258 return pNC->pParse->nErr - nErr;
drh22827922000-06-06 17:27:05 +00002259}
drh5d9a4af2005-08-30 00:54:01 +00002260
2261/*
2262** Call sqlite3ExprAnalyzeAggregates() for every expression in an
2263** expression list. Return the number of errors.
2264**
2265** If an error is found, the analysis is cut short.
2266*/
2267int sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){
2268 struct ExprList_item *pItem;
2269 int i;
2270 int nErr = 0;
2271 if( pList ){
2272 for(pItem=pList->a, i=0; nErr==0 && i<pList->nExpr; i++, pItem++){
2273 nErr += sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
2274 }
2275 }
2276 return nErr;
2277}