blob: cce2a6681063a3b5e32ad183e9120c387cbb867f [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**
drh4284fb02005-11-03 12:33:28 +000015** $Id: expr.c,v 1.235 2005/11/03 12:33:28 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 }
drh4284fb02005-11-03 12:33:28 +00001203 break;
danielk1977b3bce662005-01-29 08:32:43 +00001204 }
drh4284fb02005-11-03 12:33:28 +00001205#ifndef SQLITE_OMIT_CHECK
1206 case TK_VARIABLE: {
1207 if( pNC->isCheck ){
1208 sqlite3ErrorMsg(pParse,"parameters prohibited in CHECK constraints");
1209 }
1210 break;
1211 }
1212#endif
drh626a8792005-01-17 22:08:19 +00001213 }
1214 return 0;
1215}
1216
1217/*
drhcce7d172000-05-31 15:34:51 +00001218** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +00001219** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +00001220** index to the table in the table list and a column offset. The
1221** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
1222** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +00001223** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +00001224** VDBE cursor number for a cursor that is pointing into the referenced
1225** table. The Expr.iColumn value is changed to the index of the column
1226** of the referenced table. The Expr.iColumn value for the special
1227** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
1228** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +00001229**
drh626a8792005-01-17 22:08:19 +00001230** Also resolve function names and check the functions for proper
1231** usage. Make sure all function names are recognized and all functions
1232** have the correct number of arguments. Leave an error message
1233** in pParse->zErrMsg if anything is amiss. Return the number of errors.
1234**
drh73b211a2005-01-18 04:00:42 +00001235** If the expression contains aggregate functions then set the EP_Agg
1236** property on the expression.
drh626a8792005-01-17 22:08:19 +00001237*/
1238int sqlite3ExprResolveNames(
danielk1977b3bce662005-01-29 08:32:43 +00001239 NameContext *pNC, /* Namespace to resolve expressions in. */
1240 Expr *pExpr /* The expression to be analyzed. */
drh626a8792005-01-17 22:08:19 +00001241){
drh13449892005-09-07 21:22:45 +00001242 int savedHasAgg;
drh73b211a2005-01-18 04:00:42 +00001243 if( pExpr==0 ) return 0;
drh13449892005-09-07 21:22:45 +00001244 savedHasAgg = pNC->hasAgg;
1245 pNC->hasAgg = 0;
danielk1977b3bce662005-01-29 08:32:43 +00001246 walkExprTree(pExpr, nameResolverStep, pNC);
1247 if( pNC->nErr>0 ){
drh73b211a2005-01-18 04:00:42 +00001248 ExprSetProperty(pExpr, EP_Error);
drh73b211a2005-01-18 04:00:42 +00001249 }
drh13449892005-09-07 21:22:45 +00001250 if( pNC->hasAgg ){
1251 ExprSetProperty(pExpr, EP_Agg);
1252 }else if( savedHasAgg ){
1253 pNC->hasAgg = 1;
1254 }
drh73b211a2005-01-18 04:00:42 +00001255 return ExprHasProperty(pExpr, EP_Error);
drh626a8792005-01-17 22:08:19 +00001256}
1257
drh1398ad32005-01-19 23:24:50 +00001258/*
1259** A pointer instance of this structure is used to pass information
1260** through walkExprTree into codeSubqueryStep().
1261*/
1262typedef struct QueryCoder QueryCoder;
1263struct QueryCoder {
1264 Parse *pParse; /* The parsing context */
1265 NameContext *pNC; /* Namespace of first enclosing query */
1266};
1267
drh626a8792005-01-17 22:08:19 +00001268
1269/*
1270** Generate code for subqueries and IN operators.
1271**
drh73b211a2005-01-18 04:00:42 +00001272** IN operators comes in two forms:
drhfef52082000-06-06 01:50:43 +00001273**
1274** expr IN (exprlist)
1275** and
1276** expr IN (SELECT ...)
1277**
1278** The first form is handled by creating a set holding the list
1279** of allowed values. The second form causes the SELECT to generate
1280** a temporary table.
drhcce7d172000-05-31 15:34:51 +00001281*/
drh51522cd2005-01-20 13:36:19 +00001282#ifndef SQLITE_OMIT_SUBQUERY
danielk1977b3bce662005-01-29 08:32:43 +00001283void sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){
drh57dbd7b2005-07-08 18:25:26 +00001284 int testAddr = 0; /* One-time test address */
danielk1977b3bce662005-01-29 08:32:43 +00001285 Vdbe *v = sqlite3GetVdbe(pParse);
1286 if( v==0 ) return;
1287
drh57dbd7b2005-07-08 18:25:26 +00001288 /* This code must be run in its entirety every time it is encountered
1289 ** if any of the following is true:
1290 **
1291 ** * The right-hand side is a correlated subquery
1292 ** * The right-hand side is an expression list containing variables
1293 ** * We are inside a trigger
1294 **
1295 ** If all of the above are false, then we can run this code just once
1296 ** save the results, and reuse the same result on subsequent invocations.
danielk1977b3bce662005-01-29 08:32:43 +00001297 */
1298 if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->trigStack ){
1299 int mem = pParse->nMem++;
1300 sqlite3VdbeAddOp(v, OP_MemLoad, mem, 0);
drh57dbd7b2005-07-08 18:25:26 +00001301 testAddr = sqlite3VdbeAddOp(v, OP_If, 0, 0);
drh53f733c2005-09-16 02:38:09 +00001302 assert( testAddr>0 || sqlite3_malloc_failed );
drhd654be82005-09-20 17:42:23 +00001303 sqlite3VdbeAddOp(v, OP_MemInt, 1, mem);
danielk1977b3bce662005-01-29 08:32:43 +00001304 }
1305
drhcce7d172000-05-31 15:34:51 +00001306 switch( pExpr->op ){
drhfef52082000-06-06 01:50:43 +00001307 case TK_IN: {
danielk1977e014a832004-05-17 10:48:57 +00001308 char affinity;
drhd3d39e92004-05-20 22:16:29 +00001309 KeyInfo keyInfo;
drh9170dd72005-07-08 17:13:46 +00001310 int addr; /* Address of OP_OpenVirtual instruction */
drhd3d39e92004-05-20 22:16:29 +00001311
danielk1977bf3b7212004-05-18 10:06:24 +00001312 affinity = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +00001313
1314 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
drh57dbd7b2005-07-08 18:25:26 +00001315 ** expression it is handled the same way. A virtual table is
danielk1977e014a832004-05-17 10:48:57 +00001316 ** filled with single-field index keys representing the results
1317 ** from the SELECT or the <exprlist>.
1318 **
1319 ** If the 'x' expression is a column value, or the SELECT...
1320 ** statement returns a column value, then the affinity of that
1321 ** column is used to build the index keys. If both 'x' and the
1322 ** SELECT... statement are columns, then numeric affinity is used
1323 ** if either column has NUMERIC or INTEGER affinity. If neither
1324 ** 'x' nor the SELECT... statement are columns, then numeric affinity
1325 ** is used.
1326 */
1327 pExpr->iTable = pParse->nTab++;
drh9170dd72005-07-08 17:13:46 +00001328 addr = sqlite3VdbeAddOp(v, OP_OpenVirtual, pExpr->iTable, 0);
drhd3d39e92004-05-20 22:16:29 +00001329 memset(&keyInfo, 0, sizeof(keyInfo));
1330 keyInfo.nField = 1;
drhf3218fe2004-05-28 08:21:02 +00001331 sqlite3VdbeAddOp(v, OP_SetNumColumns, pExpr->iTable, 1);
danielk1977e014a832004-05-17 10:48:57 +00001332
drhfef52082000-06-06 01:50:43 +00001333 if( pExpr->pSelect ){
1334 /* Case 1: expr IN (SELECT ...)
1335 **
danielk1977e014a832004-05-17 10:48:57 +00001336 ** Generate code to write the results of the select into the temporary
1337 ** table allocated and opened above.
drhfef52082000-06-06 01:50:43 +00001338 */
danielk1977e014a832004-05-17 10:48:57 +00001339 int iParm = pExpr->iTable + (((int)affinity)<<16);
drhbe5c89a2004-07-26 00:31:09 +00001340 ExprList *pEList;
danielk1977e014a832004-05-17 10:48:57 +00001341 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
danielk1977b3bce662005-01-29 08:32:43 +00001342 sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001343 pEList = pExpr->pSelect->pEList;
1344 if( pEList && pEList->nExpr>0 ){
danielk19777cedc8d2004-06-10 10:50:08 +00001345 keyInfo.aColl[0] = binaryCompareCollSeq(pParse, pExpr->pLeft,
drhbe5c89a2004-07-26 00:31:09 +00001346 pEList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001347 }
drhfef52082000-06-06 01:50:43 +00001348 }else if( pExpr->pList ){
1349 /* Case 2: expr IN (exprlist)
1350 **
danielk1977e014a832004-05-17 10:48:57 +00001351 ** For each expression, build an index key from the evaluation and
1352 ** store it in the temporary table. If <expr> is a column, then use
1353 ** that columns affinity when building index keys. If <expr> is not
1354 ** a column, use numeric affinity.
drhfef52082000-06-06 01:50:43 +00001355 */
danielk1977e014a832004-05-17 10:48:57 +00001356 int i;
drh57dbd7b2005-07-08 18:25:26 +00001357 ExprList *pList = pExpr->pList;
1358 struct ExprList_item *pItem;
1359
danielk1977e014a832004-05-17 10:48:57 +00001360 if( !affinity ){
1361 affinity = SQLITE_AFF_NUMERIC;
1362 }
danielk19770202b292004-06-09 09:55:16 +00001363 keyInfo.aColl[0] = pExpr->pLeft->pColl;
danielk1977e014a832004-05-17 10:48:57 +00001364
1365 /* Loop through each expression in <exprlist>. */
drh57dbd7b2005-07-08 18:25:26 +00001366 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
1367 Expr *pE2 = pItem->pExpr;
danielk1977e014a832004-05-17 10:48:57 +00001368
drh57dbd7b2005-07-08 18:25:26 +00001369 /* If the expression is not constant then we will need to
1370 ** disable the test that was generated above that makes sure
1371 ** this code only executes once. Because for a non-constant
1372 ** expression we need to rerun this code each time.
1373 */
drh6c30be82005-07-29 15:10:17 +00001374 if( testAddr>0 && !sqlite3ExprIsConstant(pE2) ){
drh57dbd7b2005-07-08 18:25:26 +00001375 VdbeOp *aOp = sqlite3VdbeGetOp(v, testAddr-1);
1376 int i;
drhd654be82005-09-20 17:42:23 +00001377 for(i=0; i<3; i++){
drh57dbd7b2005-07-08 18:25:26 +00001378 aOp[i].opcode = OP_Noop;
1379 }
1380 testAddr = 0;
drh4794b982000-06-06 13:54:14 +00001381 }
danielk1977e014a832004-05-17 10:48:57 +00001382
1383 /* Evaluate the expression and insert it into the temp table */
1384 sqlite3ExprCode(pParse, pE2);
drh94a11212004-09-25 13:12:14 +00001385 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1);
drhf0863fe2005-06-12 21:35:51 +00001386 sqlite3VdbeAddOp(v, OP_IdxInsert, pExpr->iTable, 0);
drhfef52082000-06-06 01:50:43 +00001387 }
1388 }
danielk19770202b292004-06-09 09:55:16 +00001389 sqlite3VdbeChangeP3(v, addr, (void *)&keyInfo, P3_KEYINFO);
danielk1977b3bce662005-01-29 08:32:43 +00001390 break;
drhfef52082000-06-06 01:50:43 +00001391 }
1392
drh51522cd2005-01-20 13:36:19 +00001393 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001394 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +00001395 /* This has to be a scalar SELECT. Generate code to put the
1396 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +00001397 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +00001398 */
drhec7429a2005-10-06 16:53:14 +00001399 static const Token one = { "1", 0, 1 };
drh51522cd2005-01-20 13:36:19 +00001400 Select *pSel;
drhec7429a2005-10-06 16:53:14 +00001401 int iMem;
1402 int sop;
drh1398ad32005-01-19 23:24:50 +00001403
drhec7429a2005-10-06 16:53:14 +00001404 pExpr->iColumn = iMem = pParse->nMem++;
drh51522cd2005-01-20 13:36:19 +00001405 pSel = pExpr->pSelect;
1406 if( pExpr->op==TK_SELECT ){
1407 sop = SRT_Mem;
drhec7429a2005-10-06 16:53:14 +00001408 sqlite3VdbeAddOp(v, OP_MemNull, iMem, 0);
1409 VdbeComment((v, "# Init subquery result"));
drh51522cd2005-01-20 13:36:19 +00001410 }else{
drh51522cd2005-01-20 13:36:19 +00001411 sop = SRT_Exists;
drhec7429a2005-10-06 16:53:14 +00001412 sqlite3VdbeAddOp(v, OP_MemInt, 0, iMem);
1413 VdbeComment((v, "# Init EXISTS result"));
drh51522cd2005-01-20 13:36:19 +00001414 }
drhec7429a2005-10-06 16:53:14 +00001415 sqlite3ExprDelete(pSel->pLimit);
1416 pSel->pLimit = sqlite3Expr(TK_INTEGER, 0, 0, &one);
1417 sqlite3Select(pParse, pSel, sop, iMem, 0, 0, 0, 0);
danielk1977b3bce662005-01-29 08:32:43 +00001418 break;
drhcce7d172000-05-31 15:34:51 +00001419 }
1420 }
danielk1977b3bce662005-01-29 08:32:43 +00001421
drh57dbd7b2005-07-08 18:25:26 +00001422 if( testAddr ){
drhd654be82005-09-20 17:42:23 +00001423 sqlite3VdbeJumpHere(v, testAddr);
danielk1977b3bce662005-01-29 08:32:43 +00001424 }
1425 return;
drhcce7d172000-05-31 15:34:51 +00001426}
drh51522cd2005-01-20 13:36:19 +00001427#endif /* SQLITE_OMIT_SUBQUERY */
drhcce7d172000-05-31 15:34:51 +00001428
drhcce7d172000-05-31 15:34:51 +00001429/*
drhfec19aa2004-05-19 20:41:03 +00001430** Generate an instruction that will put the integer describe by
1431** text z[0..n-1] on the stack.
1432*/
1433static void codeInteger(Vdbe *v, const char *z, int n){
1434 int i;
drh6fec0762004-05-30 01:38:43 +00001435 if( sqlite3GetInt32(z, &i) ){
1436 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
1437 }else if( sqlite3FitsIn64Bits(z) ){
drh29dda4a2005-07-21 18:23:20 +00001438 sqlite3VdbeOp3(v, OP_Int64, 0, 0, z, n);
drhfec19aa2004-05-19 20:41:03 +00001439 }else{
1440 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n);
1441 }
1442}
1443
1444/*
drhcce7d172000-05-31 15:34:51 +00001445** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +00001446** expression and leave the result on the top of stack.
drhf2bc0132004-10-04 13:19:23 +00001447**
1448** This code depends on the fact that certain token values (ex: TK_EQ)
1449** are the same as opcode values (ex: OP_Eq) that implement the corresponding
1450** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
1451** the make process cause these values to align. Assert()s in the code
1452** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00001453*/
danielk19774adee202004-05-08 08:23:19 +00001454void sqlite3ExprCode(Parse *pParse, Expr *pExpr){
drhcce7d172000-05-31 15:34:51 +00001455 Vdbe *v = pParse->pVdbe;
1456 int op;
drhffe07b22005-11-03 00:41:17 +00001457 int stackChng = 1; /* Amount of change to stack depth */
1458
danielk19777977a172004-11-09 12:44:37 +00001459 if( v==0 ) return;
1460 if( pExpr==0 ){
drhf0863fe2005-06-12 21:35:51 +00001461 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
danielk19777977a172004-11-09 12:44:37 +00001462 return;
1463 }
drhf2bc0132004-10-04 13:19:23 +00001464 op = pExpr->op;
1465 switch( op ){
drh13449892005-09-07 21:22:45 +00001466 case TK_AGG_COLUMN: {
1467 AggInfo *pAggInfo = pExpr->pAggInfo;
1468 struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg];
1469 if( !pAggInfo->directMode ){
1470 sqlite3VdbeAddOp(v, OP_MemLoad, pCol->iMem, 0);
1471 break;
1472 }else if( pAggInfo->useSortingIdx ){
1473 sqlite3VdbeAddOp(v, OP_Column, pAggInfo->sortingIdx,
1474 pCol->iSorterColumn);
1475 break;
1476 }
1477 /* Otherwise, fall thru into the TK_COLUMN case */
1478 }
drh967e8b72000-06-21 13:59:10 +00001479 case TK_COLUMN: {
drhffe07b22005-11-03 00:41:17 +00001480 if( pExpr->iTable<0 ){
1481 /* This only happens when coding check constraints */
1482 assert( pParse->ckOffset>0 );
1483 sqlite3VdbeAddOp(v, OP_Dup, pParse->ckOffset-pExpr->iColumn-1, 1);
1484 }else if( pExpr->iColumn>=0 ){
danielk19774adee202004-05-08 08:23:19 +00001485 sqlite3VdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn);
danielk1977aee18ef2005-03-09 12:26:50 +00001486 sqlite3ColumnDefault(v, pExpr->pTab, pExpr->iColumn);
drhc4a3c772001-04-04 11:48:57 +00001487 }else{
drhf0863fe2005-06-12 21:35:51 +00001488 sqlite3VdbeAddOp(v, OP_Rowid, pExpr->iTable, 0);
drh22827922000-06-06 17:27:05 +00001489 }
drhcce7d172000-05-31 15:34:51 +00001490 break;
1491 }
1492 case TK_INTEGER: {
drhfec19aa2004-05-19 20:41:03 +00001493 codeInteger(v, pExpr->token.z, pExpr->token.n);
1494 break;
1495 }
1496 case TK_FLOAT:
1497 case TK_STRING: {
drhf2bc0132004-10-04 13:19:23 +00001498 assert( TK_FLOAT==OP_Real );
1499 assert( TK_STRING==OP_String8 );
drhd2687b72005-08-12 22:56:09 +00001500 sqlite3DequoteExpr(pExpr);
drhfec19aa2004-05-19 20:41:03 +00001501 sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z, pExpr->token.n);
drhcce7d172000-05-31 15:34:51 +00001502 break;
1503 }
drhf0863fe2005-06-12 21:35:51 +00001504 case TK_NULL: {
1505 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
1506 break;
1507 }
danielk19775338a5f2005-01-20 13:03:10 +00001508#ifndef SQLITE_OMIT_BLOB_LITERAL
danielk1977c572ef72004-05-27 09:28:41 +00001509 case TK_BLOB: {
drh6c8c6ce2005-08-23 11:17:58 +00001510 int n;
1511 const char *z;
drhf2bc0132004-10-04 13:19:23 +00001512 assert( TK_BLOB==OP_HexBlob );
drh6c8c6ce2005-08-23 11:17:58 +00001513 n = pExpr->token.n - 3;
1514 z = pExpr->token.z + 2;
1515 assert( n>=0 );
1516 if( n==0 ){
1517 z = "";
1518 }
1519 sqlite3VdbeOp3(v, op, 0, 0, z, n);
danielk1977c572ef72004-05-27 09:28:41 +00001520 break;
1521 }
danielk19775338a5f2005-01-20 13:03:10 +00001522#endif
drh50457892003-09-06 01:10:47 +00001523 case TK_VARIABLE: {
danielk19774adee202004-05-08 08:23:19 +00001524 sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0);
drh895d7472004-08-20 16:02:39 +00001525 if( pExpr->token.n>1 ){
1526 sqlite3VdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
1527 }
drh50457892003-09-06 01:10:47 +00001528 break;
1529 }
drh4e0cff62004-11-05 05:10:28 +00001530 case TK_REGISTER: {
1531 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iTable, 0);
1532 break;
1533 }
drh487e2622005-06-25 18:42:14 +00001534#ifndef SQLITE_OMIT_CAST
1535 case TK_CAST: {
1536 /* Expressions of the form: CAST(pLeft AS token) */
1537 int aff, op;
1538 sqlite3ExprCode(pParse, pExpr->pLeft);
drh8df447f2005-11-01 15:48:24 +00001539 aff = sqlite3AffinityType(&pExpr->token, 1);
drh487e2622005-06-25 18:42:14 +00001540 switch( aff ){
1541 case SQLITE_AFF_INTEGER: op = OP_ToInt; break;
1542 case SQLITE_AFF_NUMERIC: op = OP_ToNumeric; break;
1543 case SQLITE_AFF_TEXT: op = OP_ToText; break;
1544 case SQLITE_AFF_NONE: op = OP_ToBlob; break;
1545 }
1546 sqlite3VdbeAddOp(v, op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001547 stackChng = 0;
drh487e2622005-06-25 18:42:14 +00001548 break;
1549 }
1550#endif /* SQLITE_OMIT_CAST */
drhc9b84a12002-06-20 11:36:48 +00001551 case TK_LT:
1552 case TK_LE:
1553 case TK_GT:
1554 case TK_GE:
1555 case TK_NE:
1556 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00001557 assert( TK_LT==OP_Lt );
1558 assert( TK_LE==OP_Le );
1559 assert( TK_GT==OP_Gt );
1560 assert( TK_GE==OP_Ge );
1561 assert( TK_EQ==OP_Eq );
1562 assert( TK_NE==OP_Ne );
danielk1977a37cdde2004-05-16 11:15:36 +00001563 sqlite3ExprCode(pParse, pExpr->pLeft);
1564 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001565 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001566 stackChng = -1;
danielk1977a37cdde2004-05-16 11:15:36 +00001567 break;
drhc9b84a12002-06-20 11:36:48 +00001568 }
drhcce7d172000-05-31 15:34:51 +00001569 case TK_AND:
1570 case TK_OR:
1571 case TK_PLUS:
1572 case TK_STAR:
1573 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00001574 case TK_REM:
1575 case TK_BITAND:
1576 case TK_BITOR:
drh17c40292004-07-21 02:53:29 +00001577 case TK_SLASH:
drhbf4133c2001-10-13 02:59:08 +00001578 case TK_LSHIFT:
drh855eb1c2004-08-31 13:45:11 +00001579 case TK_RSHIFT:
drh00400772000-06-16 20:51:26 +00001580 case TK_CONCAT: {
drhf2bc0132004-10-04 13:19:23 +00001581 assert( TK_AND==OP_And );
1582 assert( TK_OR==OP_Or );
1583 assert( TK_PLUS==OP_Add );
1584 assert( TK_MINUS==OP_Subtract );
1585 assert( TK_REM==OP_Remainder );
1586 assert( TK_BITAND==OP_BitAnd );
1587 assert( TK_BITOR==OP_BitOr );
1588 assert( TK_SLASH==OP_Divide );
1589 assert( TK_LSHIFT==OP_ShiftLeft );
1590 assert( TK_RSHIFT==OP_ShiftRight );
1591 assert( TK_CONCAT==OP_Concat );
danielk19774adee202004-05-08 08:23:19 +00001592 sqlite3ExprCode(pParse, pExpr->pLeft);
1593 sqlite3ExprCode(pParse, pExpr->pRight);
drh855eb1c2004-08-31 13:45:11 +00001594 sqlite3VdbeAddOp(v, op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001595 stackChng = -1;
drh00400772000-06-16 20:51:26 +00001596 break;
1597 }
drhcce7d172000-05-31 15:34:51 +00001598 case TK_UMINUS: {
drhfec19aa2004-05-19 20:41:03 +00001599 Expr *pLeft = pExpr->pLeft;
1600 assert( pLeft );
1601 if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
1602 Token *p = &pLeft->token;
drh6e142f52000-06-08 13:36:40 +00001603 char *z = sqliteMalloc( p->n + 2 );
1604 sprintf(z, "-%.*s", p->n, p->z);
drhfec19aa2004-05-19 20:41:03 +00001605 if( pLeft->op==TK_FLOAT ){
1606 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001607 }else{
drhfec19aa2004-05-19 20:41:03 +00001608 codeInteger(v, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001609 }
drh6e142f52000-06-08 13:36:40 +00001610 sqliteFree(z);
1611 break;
1612 }
drh1ccde152000-06-17 13:12:39 +00001613 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +00001614 }
drhbf4133c2001-10-13 02:59:08 +00001615 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00001616 case TK_NOT: {
drhf2bc0132004-10-04 13:19:23 +00001617 assert( TK_BITNOT==OP_BitNot );
1618 assert( TK_NOT==OP_Not );
danielk19774adee202004-05-08 08:23:19 +00001619 sqlite3ExprCode(pParse, pExpr->pLeft);
1620 sqlite3VdbeAddOp(v, op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001621 stackChng = 0;
drhcce7d172000-05-31 15:34:51 +00001622 break;
1623 }
1624 case TK_ISNULL:
1625 case TK_NOTNULL: {
1626 int dest;
drhf2bc0132004-10-04 13:19:23 +00001627 assert( TK_ISNULL==OP_IsNull );
1628 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00001629 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1630 sqlite3ExprCode(pParse, pExpr->pLeft);
1631 dest = sqlite3VdbeCurrentAddr(v) + 2;
1632 sqlite3VdbeAddOp(v, op, 1, dest);
1633 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);
drhffe07b22005-11-03 00:41:17 +00001634 stackChng = 0;
drhf2bc0132004-10-04 13:19:23 +00001635 break;
drhcce7d172000-05-31 15:34:51 +00001636 }
drh22827922000-06-06 17:27:05 +00001637 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00001638 AggInfo *pInfo = pExpr->pAggInfo;
1639 sqlite3VdbeAddOp(v, OP_MemLoad, pInfo->aFunc[pExpr->iAgg].iMem, 0);
drh22827922000-06-06 17:27:05 +00001640 break;
1641 }
drhb71090f2005-05-23 17:26:51 +00001642 case TK_CONST_FUNC:
drhcce7d172000-05-31 15:34:51 +00001643 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00001644 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00001645 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00001646 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00001647 int nId;
1648 const char *zId;
drh13449892005-09-07 21:22:45 +00001649 int constMask = 0;
danielk1977682f68b2004-06-05 10:22:17 +00001650 int i;
danielk1977d8123362004-06-12 09:25:12 +00001651 u8 enc = pParse->db->enc;
danielk1977dc1bdc42004-06-11 10:51:27 +00001652 CollSeq *pColl = 0;
drhb71090f2005-05-23 17:26:51 +00001653 zId = pExpr->token.z;
1654 nId = pExpr->token.n;
danielk1977d8123362004-06-12 09:25:12 +00001655 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001656 assert( pDef!=0 );
drhf9b596e2004-05-26 16:54:42 +00001657 nExpr = sqlite3ExprCodeExprList(pParse, pList);
danielk1977682f68b2004-06-05 10:22:17 +00001658 for(i=0; i<nExpr && i<32; i++){
danielk1977d02eb1f2004-06-06 09:44:03 +00001659 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
drh13449892005-09-07 21:22:45 +00001660 constMask |= (1<<i);
danielk1977d02eb1f2004-06-06 09:44:03 +00001661 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001662 if( pDef->needCollSeq && !pColl ){
1663 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
1664 }
1665 }
1666 if( pDef->needCollSeq ){
1667 if( !pColl ) pColl = pParse->db->pDfltColl;
danielk1977d8123362004-06-12 09:25:12 +00001668 sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ);
danielk1977682f68b2004-06-05 10:22:17 +00001669 }
drh13449892005-09-07 21:22:45 +00001670 sqlite3VdbeOp3(v, OP_Function, constMask, nExpr, (char*)pDef, P3_FUNCDEF);
drhffe07b22005-11-03 00:41:17 +00001671 stackChng = 1-nExpr;
drhcce7d172000-05-31 15:34:51 +00001672 break;
1673 }
drhfe2093d2005-01-20 22:48:47 +00001674#ifndef SQLITE_OMIT_SUBQUERY
1675 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001676 case TK_SELECT: {
danielk1977b3bce662005-01-29 08:32:43 +00001677 sqlite3CodeSubselect(pParse, pExpr);
danielk19774adee202004-05-08 08:23:19 +00001678 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drhad6d9462004-09-19 02:15:24 +00001679 VdbeComment((v, "# load subquery result"));
drh19a775c2000-06-05 18:54:46 +00001680 break;
1681 }
drhfef52082000-06-06 01:50:43 +00001682 case TK_IN: {
1683 int addr;
drh94a11212004-09-25 13:12:14 +00001684 char affinity;
danielk1977b3bce662005-01-29 08:32:43 +00001685 sqlite3CodeSubselect(pParse, pExpr);
danielk1977e014a832004-05-17 10:48:57 +00001686
1687 /* Figure out the affinity to use to create a key from the results
1688 ** of the expression. affinityStr stores a static string suitable for
danielk1977ededfd52004-06-17 07:53:01 +00001689 ** P3 of OP_MakeRecord.
danielk1977e014a832004-05-17 10:48:57 +00001690 */
drh94a11212004-09-25 13:12:14 +00001691 affinity = comparisonAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +00001692
danielk19774adee202004-05-08 08:23:19 +00001693 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
danielk1977e014a832004-05-17 10:48:57 +00001694
1695 /* Code the <expr> from "<expr> IN (...)". The temporary table
1696 ** pExpr->iTable contains the values that make up the (...) set.
1697 */
danielk19774adee202004-05-08 08:23:19 +00001698 sqlite3ExprCode(pParse, pExpr->pLeft);
1699 addr = sqlite3VdbeCurrentAddr(v);
danielk1977e014a832004-05-17 10:48:57 +00001700 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); /* addr + 0 */
danielk19774adee202004-05-08 08:23:19 +00001701 sqlite3VdbeAddOp(v, OP_Pop, 2, 0);
drhf0863fe2005-06-12 21:35:51 +00001702 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +00001703 sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7);
drh94a11212004-09-25 13:12:14 +00001704 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1); /* addr + 4 */
danielk1977e014a832004-05-17 10:48:57 +00001705 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7);
1706 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); /* addr + 6 */
1707
drhfef52082000-06-06 01:50:43 +00001708 break;
1709 }
danielk197793758c82005-01-21 08:13:14 +00001710#endif
drhfef52082000-06-06 01:50:43 +00001711 case TK_BETWEEN: {
drhbe5c89a2004-07-26 00:31:09 +00001712 Expr *pLeft = pExpr->pLeft;
1713 struct ExprList_item *pLItem = pExpr->pList->a;
1714 Expr *pRight = pLItem->pExpr;
1715 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001716 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001717 sqlite3ExprCode(pParse, pRight);
1718 codeCompare(pParse, pLeft, pRight, OP_Ge, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001719 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
drhbe5c89a2004-07-26 00:31:09 +00001720 pLItem++;
1721 pRight = pLItem->pExpr;
1722 sqlite3ExprCode(pParse, pRight);
1723 codeCompare(pParse, pLeft, pRight, OP_Le, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001724 sqlite3VdbeAddOp(v, OP_And, 0, 0);
drhfef52082000-06-06 01:50:43 +00001725 break;
1726 }
drh51e9a442004-01-16 16:42:53 +00001727 case TK_UPLUS:
drha2e00042002-01-22 03:13:42 +00001728 case TK_AS: {
danielk19774adee202004-05-08 08:23:19 +00001729 sqlite3ExprCode(pParse, pExpr->pLeft);
drhffe07b22005-11-03 00:41:17 +00001730 stackChng = 0;
drha2e00042002-01-22 03:13:42 +00001731 break;
1732 }
drh17a7f8d2002-03-24 13:13:27 +00001733 case TK_CASE: {
1734 int expr_end_label;
drhf5905aa2002-05-26 20:54:33 +00001735 int jumpInst;
drhf5905aa2002-05-26 20:54:33 +00001736 int nExpr;
drh17a7f8d2002-03-24 13:13:27 +00001737 int i;
drhbe5c89a2004-07-26 00:31:09 +00001738 ExprList *pEList;
1739 struct ExprList_item *aListelem;
drh17a7f8d2002-03-24 13:13:27 +00001740
1741 assert(pExpr->pList);
1742 assert((pExpr->pList->nExpr % 2) == 0);
1743 assert(pExpr->pList->nExpr > 0);
drhbe5c89a2004-07-26 00:31:09 +00001744 pEList = pExpr->pList;
1745 aListelem = pEList->a;
1746 nExpr = pEList->nExpr;
danielk19774adee202004-05-08 08:23:19 +00001747 expr_end_label = sqlite3VdbeMakeLabel(v);
drh17a7f8d2002-03-24 13:13:27 +00001748 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001749 sqlite3ExprCode(pParse, pExpr->pLeft);
drh17a7f8d2002-03-24 13:13:27 +00001750 }
drhf5905aa2002-05-26 20:54:33 +00001751 for(i=0; i<nExpr; i=i+2){
drhbe5c89a2004-07-26 00:31:09 +00001752 sqlite3ExprCode(pParse, aListelem[i].pExpr);
drh17a7f8d2002-03-24 13:13:27 +00001753 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001754 sqlite3VdbeAddOp(v, OP_Dup, 1, 1);
drhbe5c89a2004-07-26 00:31:09 +00001755 jumpInst = codeCompare(pParse, pExpr->pLeft, aListelem[i].pExpr,
1756 OP_Ne, 0, 1);
danielk19774adee202004-05-08 08:23:19 +00001757 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001758 }else{
danielk19774adee202004-05-08 08:23:19 +00001759 jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001760 }
drhbe5c89a2004-07-26 00:31:09 +00001761 sqlite3ExprCode(pParse, aListelem[i+1].pExpr);
danielk19774adee202004-05-08 08:23:19 +00001762 sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label);
drhd654be82005-09-20 17:42:23 +00001763 sqlite3VdbeJumpHere(v, jumpInst);
drh17a7f8d2002-03-24 13:13:27 +00001764 }
drhf570f012002-05-31 15:51:25 +00001765 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001766 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhf570f012002-05-31 15:51:25 +00001767 }
drh17a7f8d2002-03-24 13:13:27 +00001768 if( pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001769 sqlite3ExprCode(pParse, pExpr->pRight);
drh17a7f8d2002-03-24 13:13:27 +00001770 }else{
drhf0863fe2005-06-12 21:35:51 +00001771 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
drh17a7f8d2002-03-24 13:13:27 +00001772 }
danielk19774adee202004-05-08 08:23:19 +00001773 sqlite3VdbeResolveLabel(v, expr_end_label);
danielk19776f349032002-06-11 02:25:40 +00001774 break;
1775 }
danielk19775338a5f2005-01-20 13:03:10 +00001776#ifndef SQLITE_OMIT_TRIGGER
danielk19776f349032002-06-11 02:25:40 +00001777 case TK_RAISE: {
1778 if( !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +00001779 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00001780 "RAISE() may only be used within a trigger-program");
danielk19776f349032002-06-11 02:25:40 +00001781 return;
1782 }
drhad6d9462004-09-19 02:15:24 +00001783 if( pExpr->iColumn!=OE_Ignore ){
1784 assert( pExpr->iColumn==OE_Rollback ||
1785 pExpr->iColumn == OE_Abort ||
1786 pExpr->iColumn == OE_Fail );
drhd2687b72005-08-12 22:56:09 +00001787 sqlite3DequoteExpr(pExpr);
drhad6d9462004-09-19 02:15:24 +00001788 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn,
1789 pExpr->token.z, pExpr->token.n);
danielk19776f349032002-06-11 02:25:40 +00001790 } else {
drhad6d9462004-09-19 02:15:24 +00001791 assert( pExpr->iColumn == OE_Ignore );
1792 sqlite3VdbeAddOp(v, OP_ContextPop, 0, 0);
1793 sqlite3VdbeAddOp(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
1794 VdbeComment((v, "# raise(IGNORE)"));
danielk19776f349032002-06-11 02:25:40 +00001795 }
drhffe07b22005-11-03 00:41:17 +00001796 stackChng = 0;
1797 break;
drh17a7f8d2002-03-24 13:13:27 +00001798 }
danielk19775338a5f2005-01-20 13:03:10 +00001799#endif
drhffe07b22005-11-03 00:41:17 +00001800 }
1801
1802 if( pParse->ckOffset ){
1803 pParse->ckOffset += stackChng;
1804 assert( pParse->ckOffset );
drhcce7d172000-05-31 15:34:51 +00001805 }
drhcce7d172000-05-31 15:34:51 +00001806}
1807
danielk197793758c82005-01-21 08:13:14 +00001808#ifndef SQLITE_OMIT_TRIGGER
drhcce7d172000-05-31 15:34:51 +00001809/*
drh25303782004-12-07 15:41:48 +00001810** Generate code that evalutes the given expression and leaves the result
1811** on the stack. See also sqlite3ExprCode().
1812**
1813** This routine might also cache the result and modify the pExpr tree
1814** so that it will make use of the cached result on subsequent evaluations
1815** rather than evaluate the whole expression again. Trivial expressions are
1816** not cached. If the expression is cached, its result is stored in a
1817** memory location.
1818*/
1819void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr){
1820 Vdbe *v = pParse->pVdbe;
1821 int iMem;
1822 int addr1, addr2;
1823 if( v==0 ) return;
1824 addr1 = sqlite3VdbeCurrentAddr(v);
1825 sqlite3ExprCode(pParse, pExpr);
1826 addr2 = sqlite3VdbeCurrentAddr(v);
1827 if( addr2>addr1+1 || sqlite3VdbeGetOp(v, addr1)->opcode==OP_Function ){
1828 iMem = pExpr->iTable = pParse->nMem++;
1829 sqlite3VdbeAddOp(v, OP_MemStore, iMem, 0);
1830 pExpr->op = TK_REGISTER;
1831 }
1832}
danielk197793758c82005-01-21 08:13:14 +00001833#endif
drh25303782004-12-07 15:41:48 +00001834
1835/*
drh268380c2004-02-25 13:47:31 +00001836** Generate code that pushes the value of every element of the given
drhf9b596e2004-05-26 16:54:42 +00001837** expression list onto the stack.
drh268380c2004-02-25 13:47:31 +00001838**
1839** Return the number of elements pushed onto the stack.
1840*/
danielk19774adee202004-05-08 08:23:19 +00001841int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00001842 Parse *pParse, /* Parsing context */
drhf9b596e2004-05-26 16:54:42 +00001843 ExprList *pList /* The expression list to be coded */
drh268380c2004-02-25 13:47:31 +00001844){
1845 struct ExprList_item *pItem;
1846 int i, n;
drh268380c2004-02-25 13:47:31 +00001847 if( pList==0 ) return 0;
drh268380c2004-02-25 13:47:31 +00001848 n = pList->nExpr;
drhc182d162005-08-14 20:47:16 +00001849 for(pItem=pList->a, i=n; i>0; i--, pItem++){
danielk19774adee202004-05-08 08:23:19 +00001850 sqlite3ExprCode(pParse, pItem->pExpr);
drh268380c2004-02-25 13:47:31 +00001851 }
drhf9b596e2004-05-26 16:54:42 +00001852 return n;
drh268380c2004-02-25 13:47:31 +00001853}
1854
1855/*
drhcce7d172000-05-31 15:34:51 +00001856** Generate code for a boolean expression such that a jump is made
1857** to the label "dest" if the expression is true but execution
1858** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00001859**
1860** If the expression evaluates to NULL (neither true nor false), then
1861** take the jump if the jumpIfNull flag is true.
drhf2bc0132004-10-04 13:19:23 +00001862**
1863** This code depends on the fact that certain token values (ex: TK_EQ)
1864** are the same as opcode values (ex: OP_Eq) that implement the corresponding
1865** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
1866** the make process cause these values to align. Assert()s in the code
1867** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00001868*/
danielk19774adee202004-05-08 08:23:19 +00001869void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001870 Vdbe *v = pParse->pVdbe;
1871 int op = 0;
drhffe07b22005-11-03 00:41:17 +00001872 int ckOffset = pParse->ckOffset;
drhdaffd0e2001-04-11 14:28:42 +00001873 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00001874 op = pExpr->op;
1875 switch( op ){
drhcce7d172000-05-31 15:34:51 +00001876 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001877 int d2 = sqlite3VdbeMakeLabel(v);
1878 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
1879 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
1880 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001881 break;
1882 }
1883 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001884 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
1885 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001886 break;
1887 }
1888 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001889 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001890 break;
1891 }
1892 case TK_LT:
1893 case TK_LE:
1894 case TK_GT:
1895 case TK_GE:
1896 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00001897 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00001898 assert( TK_LT==OP_Lt );
1899 assert( TK_LE==OP_Le );
1900 assert( TK_GT==OP_Gt );
1901 assert( TK_GE==OP_Ge );
1902 assert( TK_EQ==OP_Eq );
1903 assert( TK_NE==OP_Ne );
danielk19774adee202004-05-08 08:23:19 +00001904 sqlite3ExprCode(pParse, pExpr->pLeft);
1905 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001906 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001907 break;
1908 }
1909 case TK_ISNULL:
1910 case TK_NOTNULL: {
drhf2bc0132004-10-04 13:19:23 +00001911 assert( TK_ISNULL==OP_IsNull );
1912 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00001913 sqlite3ExprCode(pParse, pExpr->pLeft);
1914 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001915 break;
1916 }
drhfef52082000-06-06 01:50:43 +00001917 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00001918 /* The expression "x BETWEEN y AND z" is implemented as:
1919 **
1920 ** 1 IF (x < y) GOTO 3
1921 ** 2 IF (x <= z) GOTO <dest>
1922 ** 3 ...
1923 */
drhf5905aa2002-05-26 20:54:33 +00001924 int addr;
drhbe5c89a2004-07-26 00:31:09 +00001925 Expr *pLeft = pExpr->pLeft;
1926 Expr *pRight = pExpr->pList->a[0].pExpr;
1927 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001928 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001929 sqlite3ExprCode(pParse, pRight);
1930 addr = codeCompare(pParse, pLeft, pRight, OP_Lt, 0, !jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00001931
drhbe5c89a2004-07-26 00:31:09 +00001932 pRight = pExpr->pList->a[1].pExpr;
1933 sqlite3ExprCode(pParse, pRight);
1934 codeCompare(pParse, pLeft, pRight, OP_Le, dest, jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00001935
danielk19774adee202004-05-08 08:23:19 +00001936 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
drhd654be82005-09-20 17:42:23 +00001937 sqlite3VdbeJumpHere(v, addr);
danielk19774adee202004-05-08 08:23:19 +00001938 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00001939 break;
1940 }
drhcce7d172000-05-31 15:34:51 +00001941 default: {
danielk19774adee202004-05-08 08:23:19 +00001942 sqlite3ExprCode(pParse, pExpr);
1943 sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001944 break;
1945 }
1946 }
drhffe07b22005-11-03 00:41:17 +00001947 pParse->ckOffset = ckOffset;
drhcce7d172000-05-31 15:34:51 +00001948}
1949
1950/*
drh66b89c82000-11-28 20:47:17 +00001951** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00001952** to the label "dest" if the expression is false but execution
1953** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00001954**
1955** If the expression evaluates to NULL (neither true nor false) then
1956** jump if jumpIfNull is true or fall through if jumpIfNull is false.
drhcce7d172000-05-31 15:34:51 +00001957*/
danielk19774adee202004-05-08 08:23:19 +00001958void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001959 Vdbe *v = pParse->pVdbe;
1960 int op = 0;
drhffe07b22005-11-03 00:41:17 +00001961 int ckOffset = pParse->ckOffset;
drhdaffd0e2001-04-11 14:28:42 +00001962 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00001963
1964 /* The value of pExpr->op and op are related as follows:
1965 **
1966 ** pExpr->op op
1967 ** --------- ----------
1968 ** TK_ISNULL OP_NotNull
1969 ** TK_NOTNULL OP_IsNull
1970 ** TK_NE OP_Eq
1971 ** TK_EQ OP_Ne
1972 ** TK_GT OP_Le
1973 ** TK_LE OP_Gt
1974 ** TK_GE OP_Lt
1975 ** TK_LT OP_Ge
1976 **
1977 ** For other values of pExpr->op, op is undefined and unused.
1978 ** The value of TK_ and OP_ constants are arranged such that we
1979 ** can compute the mapping above using the following expression.
1980 ** Assert()s verify that the computation is correct.
1981 */
1982 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
1983
1984 /* Verify correct alignment of TK_ and OP_ constants
1985 */
1986 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
1987 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
1988 assert( pExpr->op!=TK_NE || op==OP_Eq );
1989 assert( pExpr->op!=TK_EQ || op==OP_Ne );
1990 assert( pExpr->op!=TK_LT || op==OP_Ge );
1991 assert( pExpr->op!=TK_LE || op==OP_Gt );
1992 assert( pExpr->op!=TK_GT || op==OP_Le );
1993 assert( pExpr->op!=TK_GE || op==OP_Lt );
1994
drhcce7d172000-05-31 15:34:51 +00001995 switch( pExpr->op ){
1996 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001997 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
1998 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001999 break;
2000 }
2001 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00002002 int d2 = sqlite3VdbeMakeLabel(v);
2003 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
2004 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
2005 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00002006 break;
2007 }
2008 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00002009 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002010 break;
2011 }
2012 case TK_LT:
2013 case TK_LE:
2014 case TK_GT:
2015 case TK_GE:
2016 case TK_NE:
2017 case TK_EQ: {
danielk19774adee202004-05-08 08:23:19 +00002018 sqlite3ExprCode(pParse, pExpr->pLeft);
2019 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00002020 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002021 break;
2022 }
drhcce7d172000-05-31 15:34:51 +00002023 case TK_ISNULL:
2024 case TK_NOTNULL: {
danielk19774adee202004-05-08 08:23:19 +00002025 sqlite3ExprCode(pParse, pExpr->pLeft);
2026 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00002027 break;
2028 }
drhfef52082000-06-06 01:50:43 +00002029 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00002030 /* The expression is "x BETWEEN y AND z". It is implemented as:
2031 **
2032 ** 1 IF (x >= y) GOTO 3
2033 ** 2 GOTO <dest>
2034 ** 3 IF (x > z) GOTO <dest>
2035 */
drhfef52082000-06-06 01:50:43 +00002036 int addr;
drhbe5c89a2004-07-26 00:31:09 +00002037 Expr *pLeft = pExpr->pLeft;
2038 Expr *pRight = pExpr->pList->a[0].pExpr;
2039 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00002040 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00002041 sqlite3ExprCode(pParse, pRight);
danielk19774adee202004-05-08 08:23:19 +00002042 addr = sqlite3VdbeCurrentAddr(v);
drhbe5c89a2004-07-26 00:31:09 +00002043 codeCompare(pParse, pLeft, pRight, OP_Ge, addr+3, !jumpIfNull);
2044
danielk19774adee202004-05-08 08:23:19 +00002045 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
2046 sqlite3VdbeAddOp(v, OP_Goto, 0, dest);
drhbe5c89a2004-07-26 00:31:09 +00002047 pRight = pExpr->pList->a[1].pExpr;
2048 sqlite3ExprCode(pParse, pRight);
2049 codeCompare(pParse, pLeft, pRight, OP_Gt, dest, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00002050 break;
2051 }
drhcce7d172000-05-31 15:34:51 +00002052 default: {
danielk19774adee202004-05-08 08:23:19 +00002053 sqlite3ExprCode(pParse, pExpr);
2054 sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00002055 break;
2056 }
2057 }
drhffe07b22005-11-03 00:41:17 +00002058 pParse->ckOffset = ckOffset;
drhcce7d172000-05-31 15:34:51 +00002059}
drh22827922000-06-06 17:27:05 +00002060
2061/*
2062** Do a deep comparison of two expression trees. Return TRUE (non-zero)
2063** if they are identical and return FALSE if they differ in any way.
2064*/
danielk19774adee202004-05-08 08:23:19 +00002065int sqlite3ExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00002066 int i;
2067 if( pA==0 ){
2068 return pB==0;
2069 }else if( pB==0 ){
2070 return 0;
2071 }
2072 if( pA->op!=pB->op ) return 0;
drhfd357972005-09-09 01:33:19 +00002073 if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 0;
danielk19774adee202004-05-08 08:23:19 +00002074 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
2075 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00002076 if( pA->pList ){
2077 if( pB->pList==0 ) return 0;
2078 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
2079 for(i=0; i<pA->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00002080 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00002081 return 0;
2082 }
2083 }
2084 }else if( pB->pList ){
2085 return 0;
2086 }
2087 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00002088 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drh22827922000-06-06 17:27:05 +00002089 if( pA->token.z ){
2090 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00002091 if( pB->token.n!=pA->token.n ) return 0;
danielk19774adee202004-05-08 08:23:19 +00002092 if( sqlite3StrNICmp(pA->token.z, pB->token.z, pB->token.n)!=0 ) return 0;
drh22827922000-06-06 17:27:05 +00002093 }
2094 return 1;
2095}
2096
drh13449892005-09-07 21:22:45 +00002097
drh22827922000-06-06 17:27:05 +00002098/*
drh13449892005-09-07 21:22:45 +00002099** Add a new element to the pAggInfo->aCol[] array. Return the index of
2100** the new element. Return a negative number if malloc fails.
drh22827922000-06-06 17:27:05 +00002101*/
drh13449892005-09-07 21:22:45 +00002102static int addAggInfoColumn(AggInfo *pInfo){
2103 int i;
2104 i = sqlite3ArrayAllocate((void**)&pInfo->aCol, sizeof(pInfo->aCol[0]), 3);
2105 if( i<0 ){
2106 return -1;
drh22827922000-06-06 17:27:05 +00002107 }
drh13449892005-09-07 21:22:45 +00002108 return i;
2109}
2110
2111/*
2112** Add a new element to the pAggInfo->aFunc[] array. Return the index of
2113** the new element. Return a negative number if malloc fails.
2114*/
2115static int addAggInfoFunc(AggInfo *pInfo){
2116 int i;
2117 i = sqlite3ArrayAllocate((void**)&pInfo->aFunc, sizeof(pInfo->aFunc[0]), 2);
2118 if( i<0 ){
2119 return -1;
2120 }
2121 return i;
2122}
drh22827922000-06-06 17:27:05 +00002123
2124/*
drh626a8792005-01-17 22:08:19 +00002125** This is an xFunc for walkExprTree() used to implement
2126** sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates
2127** for additional information.
drh22827922000-06-06 17:27:05 +00002128**
drh626a8792005-01-17 22:08:19 +00002129** This routine analyzes the aggregate function at pExpr.
drh22827922000-06-06 17:27:05 +00002130*/
drh626a8792005-01-17 22:08:19 +00002131static int analyzeAggregate(void *pArg, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00002132 int i;
danielk1977a58fdfb2005-02-08 07:50:40 +00002133 NameContext *pNC = (NameContext *)pArg;
2134 Parse *pParse = pNC->pParse;
2135 SrcList *pSrcList = pNC->pSrcList;
drh13449892005-09-07 21:22:45 +00002136 AggInfo *pAggInfo = pNC->pAggInfo;
2137
drh22827922000-06-06 17:27:05 +00002138
drh22827922000-06-06 17:27:05 +00002139 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00002140 case TK_COLUMN: {
drh13449892005-09-07 21:22:45 +00002141 /* Check to see if the column is in one of the tables in the FROM
2142 ** clause of the aggregate query */
2143 if( pSrcList ){
2144 struct SrcList_item *pItem = pSrcList->a;
2145 for(i=0; i<pSrcList->nSrc; i++, pItem++){
2146 struct AggInfo_col *pCol;
2147 if( pExpr->iTable==pItem->iCursor ){
2148 /* If we reach this point, it means that pExpr refers to a table
2149 ** that is in the FROM clause of the aggregate query.
2150 **
2151 ** Make an entry for the column in pAggInfo->aCol[] if there
2152 ** is not an entry there already.
2153 */
2154 pCol = pAggInfo->aCol;
2155 for(i=0; i<pAggInfo->nColumn; i++, pCol++){
2156 if( pCol->iTable==pExpr->iTable &&
2157 pCol->iColumn==pExpr->iColumn ){
2158 break;
2159 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002160 }
drh13449892005-09-07 21:22:45 +00002161 if( i>=pAggInfo->nColumn && (i = addAggInfoColumn(pAggInfo))>=0 ){
2162 pCol = &pAggInfo->aCol[i];
2163 pCol->iTable = pExpr->iTable;
2164 pCol->iColumn = pExpr->iColumn;
2165 pCol->iMem = pParse->nMem++;
2166 pCol->iSorterColumn = -1;
drh5774b802005-09-07 22:48:16 +00002167 pCol->pExpr = pExpr;
drh13449892005-09-07 21:22:45 +00002168 if( pAggInfo->pGroupBy ){
2169 int j, n;
2170 ExprList *pGB = pAggInfo->pGroupBy;
2171 struct ExprList_item *pTerm = pGB->a;
2172 n = pGB->nExpr;
2173 for(j=0; j<n; j++, pTerm++){
2174 Expr *pE = pTerm->pExpr;
2175 if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable &&
2176 pE->iColumn==pExpr->iColumn ){
2177 pCol->iSorterColumn = j;
2178 break;
2179 }
2180 }
2181 }
2182 if( pCol->iSorterColumn<0 ){
2183 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
2184 }
2185 }
2186 /* There is now an entry for pExpr in pAggInfo->aCol[] (either
2187 ** because it was there before or because we just created it).
2188 ** Convert the pExpr to be a TK_AGG_COLUMN referring to that
2189 ** pAggInfo->aCol[] entry.
2190 */
2191 pExpr->pAggInfo = pAggInfo;
2192 pExpr->op = TK_AGG_COLUMN;
2193 pExpr->iAgg = i;
2194 break;
2195 } /* endif pExpr->iTable==pItem->iCursor */
2196 } /* end loop over pSrcList */
drh22827922000-06-06 17:27:05 +00002197 }
drh626a8792005-01-17 22:08:19 +00002198 return 1;
drh22827922000-06-06 17:27:05 +00002199 }
2200 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00002201 /* The pNC->nDepth==0 test causes aggregate functions in subqueries
2202 ** to be ignored */
danielk1977a58fdfb2005-02-08 07:50:40 +00002203 if( pNC->nDepth==0 ){
drh13449892005-09-07 21:22:45 +00002204 /* Check to see if pExpr is a duplicate of another aggregate
2205 ** function that is already in the pAggInfo structure
2206 */
2207 struct AggInfo_func *pItem = pAggInfo->aFunc;
2208 for(i=0; i<pAggInfo->nFunc; i++, pItem++){
2209 if( sqlite3ExprCompare(pItem->pExpr, pExpr) ){
danielk1977a58fdfb2005-02-08 07:50:40 +00002210 break;
2211 }
drh22827922000-06-06 17:27:05 +00002212 }
drh13449892005-09-07 21:22:45 +00002213 if( i>=pAggInfo->nFunc ){
2214 /* pExpr is original. Make a new entry in pAggInfo->aFunc[]
2215 */
danielk1977a58fdfb2005-02-08 07:50:40 +00002216 u8 enc = pParse->db->enc;
drh13449892005-09-07 21:22:45 +00002217 i = addAggInfoFunc(pAggInfo);
2218 if( i>=0 ){
2219 pItem = &pAggInfo->aFunc[i];
2220 pItem->pExpr = pExpr;
2221 pItem->iMem = pParse->nMem++;
2222 pItem->pFunc = sqlite3FindFunction(pParse->db,
2223 pExpr->token.z, pExpr->token.n,
2224 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
drhfd357972005-09-09 01:33:19 +00002225 if( pExpr->flags & EP_Distinct ){
2226 pItem->iDistinct = pParse->nTab++;
2227 }else{
2228 pItem->iDistinct = -1;
2229 }
drh13449892005-09-07 21:22:45 +00002230 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002231 }
drh13449892005-09-07 21:22:45 +00002232 /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
2233 */
danielk1977a58fdfb2005-02-08 07:50:40 +00002234 pExpr->iAgg = i;
drh13449892005-09-07 21:22:45 +00002235 pExpr->pAggInfo = pAggInfo;
danielk1977a58fdfb2005-02-08 07:50:40 +00002236 return 1;
drh22827922000-06-06 17:27:05 +00002237 }
drh22827922000-06-06 17:27:05 +00002238 }
2239 }
drh13449892005-09-07 21:22:45 +00002240
2241 /* Recursively walk subqueries looking for TK_COLUMN nodes that need
2242 ** to be changed to TK_AGG_COLUMN. But increment nDepth so that
2243 ** TK_AGG_FUNCTION nodes in subqueries will be unchanged.
2244 */
danielk1977a58fdfb2005-02-08 07:50:40 +00002245 if( pExpr->pSelect ){
2246 pNC->nDepth++;
2247 walkSelectExpr(pExpr->pSelect, analyzeAggregate, pNC);
2248 pNC->nDepth--;
2249 }
drh626a8792005-01-17 22:08:19 +00002250 return 0;
2251}
2252
2253/*
2254** Analyze the given expression looking for aggregate functions and
2255** for variables that need to be added to the pParse->aAgg[] array.
2256** Make additional entries to the pParse->aAgg[] array as necessary.
2257**
2258** This routine should only be called after the expression has been
2259** analyzed by sqlite3ExprResolveNames().
2260**
2261** If errors are seen, leave an error message in zErrMsg and return
2262** the number of errors.
2263*/
danielk1977a58fdfb2005-02-08 07:50:40 +00002264int sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
2265 int nErr = pNC->pParse->nErr;
2266 walkExprTree(pExpr, analyzeAggregate, pNC);
2267 return pNC->pParse->nErr - nErr;
drh22827922000-06-06 17:27:05 +00002268}
drh5d9a4af2005-08-30 00:54:01 +00002269
2270/*
2271** Call sqlite3ExprAnalyzeAggregates() for every expression in an
2272** expression list. Return the number of errors.
2273**
2274** If an error is found, the analysis is cut short.
2275*/
2276int sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){
2277 struct ExprList_item *pItem;
2278 int i;
2279 int nErr = 0;
2280 if( pList ){
2281 for(pItem=pList->a, i=0; nErr==0 && i<pList->nExpr; i++, pItem++){
2282 nErr += sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
2283 }
2284 }
2285 return nErr;
2286}