blob: 88c48cfba8fed35b79e67a4a8d56fc410060ad92 [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**
drhffe07b22005-11-03 00:41:17 +000015** $Id: expr.c,v 1.233 2005/11/03 00:41:17 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 ){
1191 int nRef = pNC->nRef;
1192 sqlite3SelectResolve(pParse, pExpr->pSelect, pNC);
1193 assert( pNC->nRef>=nRef );
1194 if( nRef!=pNC->nRef ){
1195 ExprSetProperty(pExpr, EP_VarSelect);
1196 }
1197 }
1198 }
drh626a8792005-01-17 22:08:19 +00001199 }
1200 return 0;
1201}
1202
1203/*
drhcce7d172000-05-31 15:34:51 +00001204** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +00001205** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +00001206** index to the table in the table list and a column offset. The
1207** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
1208** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +00001209** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +00001210** VDBE cursor number for a cursor that is pointing into the referenced
1211** table. The Expr.iColumn value is changed to the index of the column
1212** of the referenced table. The Expr.iColumn value for the special
1213** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
1214** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +00001215**
drh626a8792005-01-17 22:08:19 +00001216** Also resolve function names and check the functions for proper
1217** usage. Make sure all function names are recognized and all functions
1218** have the correct number of arguments. Leave an error message
1219** in pParse->zErrMsg if anything is amiss. Return the number of errors.
1220**
drh73b211a2005-01-18 04:00:42 +00001221** If the expression contains aggregate functions then set the EP_Agg
1222** property on the expression.
drh626a8792005-01-17 22:08:19 +00001223*/
1224int sqlite3ExprResolveNames(
danielk1977b3bce662005-01-29 08:32:43 +00001225 NameContext *pNC, /* Namespace to resolve expressions in. */
1226 Expr *pExpr /* The expression to be analyzed. */
drh626a8792005-01-17 22:08:19 +00001227){
drh13449892005-09-07 21:22:45 +00001228 int savedHasAgg;
drh73b211a2005-01-18 04:00:42 +00001229 if( pExpr==0 ) return 0;
drh13449892005-09-07 21:22:45 +00001230 savedHasAgg = pNC->hasAgg;
1231 pNC->hasAgg = 0;
danielk1977b3bce662005-01-29 08:32:43 +00001232 walkExprTree(pExpr, nameResolverStep, pNC);
1233 if( pNC->nErr>0 ){
drh73b211a2005-01-18 04:00:42 +00001234 ExprSetProperty(pExpr, EP_Error);
drh73b211a2005-01-18 04:00:42 +00001235 }
drh13449892005-09-07 21:22:45 +00001236 if( pNC->hasAgg ){
1237 ExprSetProperty(pExpr, EP_Agg);
1238 }else if( savedHasAgg ){
1239 pNC->hasAgg = 1;
1240 }
drh73b211a2005-01-18 04:00:42 +00001241 return ExprHasProperty(pExpr, EP_Error);
drh626a8792005-01-17 22:08:19 +00001242}
1243
drh1398ad32005-01-19 23:24:50 +00001244/*
1245** A pointer instance of this structure is used to pass information
1246** through walkExprTree into codeSubqueryStep().
1247*/
1248typedef struct QueryCoder QueryCoder;
1249struct QueryCoder {
1250 Parse *pParse; /* The parsing context */
1251 NameContext *pNC; /* Namespace of first enclosing query */
1252};
1253
drh626a8792005-01-17 22:08:19 +00001254
1255/*
1256** Generate code for subqueries and IN operators.
1257**
drh73b211a2005-01-18 04:00:42 +00001258** IN operators comes in two forms:
drhfef52082000-06-06 01:50:43 +00001259**
1260** expr IN (exprlist)
1261** and
1262** expr IN (SELECT ...)
1263**
1264** The first form is handled by creating a set holding the list
1265** of allowed values. The second form causes the SELECT to generate
1266** a temporary table.
drhcce7d172000-05-31 15:34:51 +00001267*/
drh51522cd2005-01-20 13:36:19 +00001268#ifndef SQLITE_OMIT_SUBQUERY
danielk1977b3bce662005-01-29 08:32:43 +00001269void sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){
drh57dbd7b2005-07-08 18:25:26 +00001270 int testAddr = 0; /* One-time test address */
danielk1977b3bce662005-01-29 08:32:43 +00001271 Vdbe *v = sqlite3GetVdbe(pParse);
1272 if( v==0 ) return;
1273
drh57dbd7b2005-07-08 18:25:26 +00001274 /* This code must be run in its entirety every time it is encountered
1275 ** if any of the following is true:
1276 **
1277 ** * The right-hand side is a correlated subquery
1278 ** * The right-hand side is an expression list containing variables
1279 ** * We are inside a trigger
1280 **
1281 ** If all of the above are false, then we can run this code just once
1282 ** save the results, and reuse the same result on subsequent invocations.
danielk1977b3bce662005-01-29 08:32:43 +00001283 */
1284 if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->trigStack ){
1285 int mem = pParse->nMem++;
1286 sqlite3VdbeAddOp(v, OP_MemLoad, mem, 0);
drh57dbd7b2005-07-08 18:25:26 +00001287 testAddr = sqlite3VdbeAddOp(v, OP_If, 0, 0);
drh53f733c2005-09-16 02:38:09 +00001288 assert( testAddr>0 || sqlite3_malloc_failed );
drhd654be82005-09-20 17:42:23 +00001289 sqlite3VdbeAddOp(v, OP_MemInt, 1, mem);
danielk1977b3bce662005-01-29 08:32:43 +00001290 }
1291
drhcce7d172000-05-31 15:34:51 +00001292 switch( pExpr->op ){
drhfef52082000-06-06 01:50:43 +00001293 case TK_IN: {
danielk1977e014a832004-05-17 10:48:57 +00001294 char affinity;
drhd3d39e92004-05-20 22:16:29 +00001295 KeyInfo keyInfo;
drh9170dd72005-07-08 17:13:46 +00001296 int addr; /* Address of OP_OpenVirtual instruction */
drhd3d39e92004-05-20 22:16:29 +00001297
danielk1977bf3b7212004-05-18 10:06:24 +00001298 affinity = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +00001299
1300 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
drh57dbd7b2005-07-08 18:25:26 +00001301 ** expression it is handled the same way. A virtual table is
danielk1977e014a832004-05-17 10:48:57 +00001302 ** filled with single-field index keys representing the results
1303 ** from the SELECT or the <exprlist>.
1304 **
1305 ** If the 'x' expression is a column value, or the SELECT...
1306 ** statement returns a column value, then the affinity of that
1307 ** column is used to build the index keys. If both 'x' and the
1308 ** SELECT... statement are columns, then numeric affinity is used
1309 ** if either column has NUMERIC or INTEGER affinity. If neither
1310 ** 'x' nor the SELECT... statement are columns, then numeric affinity
1311 ** is used.
1312 */
1313 pExpr->iTable = pParse->nTab++;
drh9170dd72005-07-08 17:13:46 +00001314 addr = sqlite3VdbeAddOp(v, OP_OpenVirtual, pExpr->iTable, 0);
drhd3d39e92004-05-20 22:16:29 +00001315 memset(&keyInfo, 0, sizeof(keyInfo));
1316 keyInfo.nField = 1;
drhf3218fe2004-05-28 08:21:02 +00001317 sqlite3VdbeAddOp(v, OP_SetNumColumns, pExpr->iTable, 1);
danielk1977e014a832004-05-17 10:48:57 +00001318
drhfef52082000-06-06 01:50:43 +00001319 if( pExpr->pSelect ){
1320 /* Case 1: expr IN (SELECT ...)
1321 **
danielk1977e014a832004-05-17 10:48:57 +00001322 ** Generate code to write the results of the select into the temporary
1323 ** table allocated and opened above.
drhfef52082000-06-06 01:50:43 +00001324 */
danielk1977e014a832004-05-17 10:48:57 +00001325 int iParm = pExpr->iTable + (((int)affinity)<<16);
drhbe5c89a2004-07-26 00:31:09 +00001326 ExprList *pEList;
danielk1977e014a832004-05-17 10:48:57 +00001327 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
danielk1977b3bce662005-01-29 08:32:43 +00001328 sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001329 pEList = pExpr->pSelect->pEList;
1330 if( pEList && pEList->nExpr>0 ){
danielk19777cedc8d2004-06-10 10:50:08 +00001331 keyInfo.aColl[0] = binaryCompareCollSeq(pParse, pExpr->pLeft,
drhbe5c89a2004-07-26 00:31:09 +00001332 pEList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001333 }
drhfef52082000-06-06 01:50:43 +00001334 }else if( pExpr->pList ){
1335 /* Case 2: expr IN (exprlist)
1336 **
danielk1977e014a832004-05-17 10:48:57 +00001337 ** For each expression, build an index key from the evaluation and
1338 ** store it in the temporary table. If <expr> is a column, then use
1339 ** that columns affinity when building index keys. If <expr> is not
1340 ** a column, use numeric affinity.
drhfef52082000-06-06 01:50:43 +00001341 */
danielk1977e014a832004-05-17 10:48:57 +00001342 int i;
drh57dbd7b2005-07-08 18:25:26 +00001343 ExprList *pList = pExpr->pList;
1344 struct ExprList_item *pItem;
1345
danielk1977e014a832004-05-17 10:48:57 +00001346 if( !affinity ){
1347 affinity = SQLITE_AFF_NUMERIC;
1348 }
danielk19770202b292004-06-09 09:55:16 +00001349 keyInfo.aColl[0] = pExpr->pLeft->pColl;
danielk1977e014a832004-05-17 10:48:57 +00001350
1351 /* Loop through each expression in <exprlist>. */
drh57dbd7b2005-07-08 18:25:26 +00001352 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
1353 Expr *pE2 = pItem->pExpr;
danielk1977e014a832004-05-17 10:48:57 +00001354
drh57dbd7b2005-07-08 18:25:26 +00001355 /* If the expression is not constant then we will need to
1356 ** disable the test that was generated above that makes sure
1357 ** this code only executes once. Because for a non-constant
1358 ** expression we need to rerun this code each time.
1359 */
drh6c30be82005-07-29 15:10:17 +00001360 if( testAddr>0 && !sqlite3ExprIsConstant(pE2) ){
drh57dbd7b2005-07-08 18:25:26 +00001361 VdbeOp *aOp = sqlite3VdbeGetOp(v, testAddr-1);
1362 int i;
drhd654be82005-09-20 17:42:23 +00001363 for(i=0; i<3; i++){
drh57dbd7b2005-07-08 18:25:26 +00001364 aOp[i].opcode = OP_Noop;
1365 }
1366 testAddr = 0;
drh4794b982000-06-06 13:54:14 +00001367 }
danielk1977e014a832004-05-17 10:48:57 +00001368
1369 /* Evaluate the expression and insert it into the temp table */
1370 sqlite3ExprCode(pParse, pE2);
drh94a11212004-09-25 13:12:14 +00001371 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1);
drhf0863fe2005-06-12 21:35:51 +00001372 sqlite3VdbeAddOp(v, OP_IdxInsert, pExpr->iTable, 0);
drhfef52082000-06-06 01:50:43 +00001373 }
1374 }
danielk19770202b292004-06-09 09:55:16 +00001375 sqlite3VdbeChangeP3(v, addr, (void *)&keyInfo, P3_KEYINFO);
danielk1977b3bce662005-01-29 08:32:43 +00001376 break;
drhfef52082000-06-06 01:50:43 +00001377 }
1378
drh51522cd2005-01-20 13:36:19 +00001379 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001380 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +00001381 /* This has to be a scalar SELECT. Generate code to put the
1382 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +00001383 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +00001384 */
drhec7429a2005-10-06 16:53:14 +00001385 static const Token one = { "1", 0, 1 };
drh51522cd2005-01-20 13:36:19 +00001386 Select *pSel;
drhec7429a2005-10-06 16:53:14 +00001387 int iMem;
1388 int sop;
drh1398ad32005-01-19 23:24:50 +00001389
drhec7429a2005-10-06 16:53:14 +00001390 pExpr->iColumn = iMem = pParse->nMem++;
drh51522cd2005-01-20 13:36:19 +00001391 pSel = pExpr->pSelect;
1392 if( pExpr->op==TK_SELECT ){
1393 sop = SRT_Mem;
drhec7429a2005-10-06 16:53:14 +00001394 sqlite3VdbeAddOp(v, OP_MemNull, iMem, 0);
1395 VdbeComment((v, "# Init subquery result"));
drh51522cd2005-01-20 13:36:19 +00001396 }else{
drh51522cd2005-01-20 13:36:19 +00001397 sop = SRT_Exists;
drhec7429a2005-10-06 16:53:14 +00001398 sqlite3VdbeAddOp(v, OP_MemInt, 0, iMem);
1399 VdbeComment((v, "# Init EXISTS result"));
drh51522cd2005-01-20 13:36:19 +00001400 }
drhec7429a2005-10-06 16:53:14 +00001401 sqlite3ExprDelete(pSel->pLimit);
1402 pSel->pLimit = sqlite3Expr(TK_INTEGER, 0, 0, &one);
1403 sqlite3Select(pParse, pSel, sop, iMem, 0, 0, 0, 0);
danielk1977b3bce662005-01-29 08:32:43 +00001404 break;
drhcce7d172000-05-31 15:34:51 +00001405 }
1406 }
danielk1977b3bce662005-01-29 08:32:43 +00001407
drh57dbd7b2005-07-08 18:25:26 +00001408 if( testAddr ){
drhd654be82005-09-20 17:42:23 +00001409 sqlite3VdbeJumpHere(v, testAddr);
danielk1977b3bce662005-01-29 08:32:43 +00001410 }
1411 return;
drhcce7d172000-05-31 15:34:51 +00001412}
drh51522cd2005-01-20 13:36:19 +00001413#endif /* SQLITE_OMIT_SUBQUERY */
drhcce7d172000-05-31 15:34:51 +00001414
drhcce7d172000-05-31 15:34:51 +00001415/*
drhfec19aa2004-05-19 20:41:03 +00001416** Generate an instruction that will put the integer describe by
1417** text z[0..n-1] on the stack.
1418*/
1419static void codeInteger(Vdbe *v, const char *z, int n){
1420 int i;
drh6fec0762004-05-30 01:38:43 +00001421 if( sqlite3GetInt32(z, &i) ){
1422 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
1423 }else if( sqlite3FitsIn64Bits(z) ){
drh29dda4a2005-07-21 18:23:20 +00001424 sqlite3VdbeOp3(v, OP_Int64, 0, 0, z, n);
drhfec19aa2004-05-19 20:41:03 +00001425 }else{
1426 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n);
1427 }
1428}
1429
1430/*
drhcce7d172000-05-31 15:34:51 +00001431** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +00001432** expression and leave the result on the top of stack.
drhf2bc0132004-10-04 13:19:23 +00001433**
1434** This code depends on the fact that certain token values (ex: TK_EQ)
1435** are the same as opcode values (ex: OP_Eq) that implement the corresponding
1436** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
1437** the make process cause these values to align. Assert()s in the code
1438** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00001439*/
danielk19774adee202004-05-08 08:23:19 +00001440void sqlite3ExprCode(Parse *pParse, Expr *pExpr){
drhcce7d172000-05-31 15:34:51 +00001441 Vdbe *v = pParse->pVdbe;
1442 int op;
drhffe07b22005-11-03 00:41:17 +00001443 int stackChng = 1; /* Amount of change to stack depth */
1444
danielk19777977a172004-11-09 12:44:37 +00001445 if( v==0 ) return;
1446 if( pExpr==0 ){
drhf0863fe2005-06-12 21:35:51 +00001447 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
danielk19777977a172004-11-09 12:44:37 +00001448 return;
1449 }
drhf2bc0132004-10-04 13:19:23 +00001450 op = pExpr->op;
1451 switch( op ){
drh13449892005-09-07 21:22:45 +00001452 case TK_AGG_COLUMN: {
1453 AggInfo *pAggInfo = pExpr->pAggInfo;
1454 struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg];
1455 if( !pAggInfo->directMode ){
1456 sqlite3VdbeAddOp(v, OP_MemLoad, pCol->iMem, 0);
1457 break;
1458 }else if( pAggInfo->useSortingIdx ){
1459 sqlite3VdbeAddOp(v, OP_Column, pAggInfo->sortingIdx,
1460 pCol->iSorterColumn);
1461 break;
1462 }
1463 /* Otherwise, fall thru into the TK_COLUMN case */
1464 }
drh967e8b72000-06-21 13:59:10 +00001465 case TK_COLUMN: {
drhffe07b22005-11-03 00:41:17 +00001466 if( pExpr->iTable<0 ){
1467 /* This only happens when coding check constraints */
1468 assert( pParse->ckOffset>0 );
1469 sqlite3VdbeAddOp(v, OP_Dup, pParse->ckOffset-pExpr->iColumn-1, 1);
1470 }else if( pExpr->iColumn>=0 ){
danielk19774adee202004-05-08 08:23:19 +00001471 sqlite3VdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn);
danielk1977aee18ef2005-03-09 12:26:50 +00001472 sqlite3ColumnDefault(v, pExpr->pTab, pExpr->iColumn);
drhc4a3c772001-04-04 11:48:57 +00001473 }else{
drhf0863fe2005-06-12 21:35:51 +00001474 sqlite3VdbeAddOp(v, OP_Rowid, pExpr->iTable, 0);
drh22827922000-06-06 17:27:05 +00001475 }
drhcce7d172000-05-31 15:34:51 +00001476 break;
1477 }
1478 case TK_INTEGER: {
drhfec19aa2004-05-19 20:41:03 +00001479 codeInteger(v, pExpr->token.z, pExpr->token.n);
1480 break;
1481 }
1482 case TK_FLOAT:
1483 case TK_STRING: {
drhf2bc0132004-10-04 13:19:23 +00001484 assert( TK_FLOAT==OP_Real );
1485 assert( TK_STRING==OP_String8 );
drhd2687b72005-08-12 22:56:09 +00001486 sqlite3DequoteExpr(pExpr);
drhfec19aa2004-05-19 20:41:03 +00001487 sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z, pExpr->token.n);
drhcce7d172000-05-31 15:34:51 +00001488 break;
1489 }
drhf0863fe2005-06-12 21:35:51 +00001490 case TK_NULL: {
1491 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
1492 break;
1493 }
danielk19775338a5f2005-01-20 13:03:10 +00001494#ifndef SQLITE_OMIT_BLOB_LITERAL
danielk1977c572ef72004-05-27 09:28:41 +00001495 case TK_BLOB: {
drh6c8c6ce2005-08-23 11:17:58 +00001496 int n;
1497 const char *z;
drhf2bc0132004-10-04 13:19:23 +00001498 assert( TK_BLOB==OP_HexBlob );
drh6c8c6ce2005-08-23 11:17:58 +00001499 n = pExpr->token.n - 3;
1500 z = pExpr->token.z + 2;
1501 assert( n>=0 );
1502 if( n==0 ){
1503 z = "";
1504 }
1505 sqlite3VdbeOp3(v, op, 0, 0, z, n);
danielk1977c572ef72004-05-27 09:28:41 +00001506 break;
1507 }
danielk19775338a5f2005-01-20 13:03:10 +00001508#endif
drh50457892003-09-06 01:10:47 +00001509 case TK_VARIABLE: {
danielk19774adee202004-05-08 08:23:19 +00001510 sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0);
drh895d7472004-08-20 16:02:39 +00001511 if( pExpr->token.n>1 ){
1512 sqlite3VdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
1513 }
drh50457892003-09-06 01:10:47 +00001514 break;
1515 }
drh4e0cff62004-11-05 05:10:28 +00001516 case TK_REGISTER: {
1517 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iTable, 0);
1518 break;
1519 }
drh487e2622005-06-25 18:42:14 +00001520#ifndef SQLITE_OMIT_CAST
1521 case TK_CAST: {
1522 /* Expressions of the form: CAST(pLeft AS token) */
1523 int aff, op;
1524 sqlite3ExprCode(pParse, pExpr->pLeft);
drh8df447f2005-11-01 15:48:24 +00001525 aff = sqlite3AffinityType(&pExpr->token, 1);
drh487e2622005-06-25 18:42:14 +00001526 switch( aff ){
1527 case SQLITE_AFF_INTEGER: op = OP_ToInt; break;
1528 case SQLITE_AFF_NUMERIC: op = OP_ToNumeric; break;
1529 case SQLITE_AFF_TEXT: op = OP_ToText; break;
1530 case SQLITE_AFF_NONE: op = OP_ToBlob; break;
1531 }
1532 sqlite3VdbeAddOp(v, op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001533 stackChng = 0;
drh487e2622005-06-25 18:42:14 +00001534 break;
1535 }
1536#endif /* SQLITE_OMIT_CAST */
drhc9b84a12002-06-20 11:36:48 +00001537 case TK_LT:
1538 case TK_LE:
1539 case TK_GT:
1540 case TK_GE:
1541 case TK_NE:
1542 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00001543 assert( TK_LT==OP_Lt );
1544 assert( TK_LE==OP_Le );
1545 assert( TK_GT==OP_Gt );
1546 assert( TK_GE==OP_Ge );
1547 assert( TK_EQ==OP_Eq );
1548 assert( TK_NE==OP_Ne );
danielk1977a37cdde2004-05-16 11:15:36 +00001549 sqlite3ExprCode(pParse, pExpr->pLeft);
1550 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001551 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001552 stackChng = -1;
danielk1977a37cdde2004-05-16 11:15:36 +00001553 break;
drhc9b84a12002-06-20 11:36:48 +00001554 }
drhcce7d172000-05-31 15:34:51 +00001555 case TK_AND:
1556 case TK_OR:
1557 case TK_PLUS:
1558 case TK_STAR:
1559 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00001560 case TK_REM:
1561 case TK_BITAND:
1562 case TK_BITOR:
drh17c40292004-07-21 02:53:29 +00001563 case TK_SLASH:
drhbf4133c2001-10-13 02:59:08 +00001564 case TK_LSHIFT:
drh855eb1c2004-08-31 13:45:11 +00001565 case TK_RSHIFT:
drh00400772000-06-16 20:51:26 +00001566 case TK_CONCAT: {
drhf2bc0132004-10-04 13:19:23 +00001567 assert( TK_AND==OP_And );
1568 assert( TK_OR==OP_Or );
1569 assert( TK_PLUS==OP_Add );
1570 assert( TK_MINUS==OP_Subtract );
1571 assert( TK_REM==OP_Remainder );
1572 assert( TK_BITAND==OP_BitAnd );
1573 assert( TK_BITOR==OP_BitOr );
1574 assert( TK_SLASH==OP_Divide );
1575 assert( TK_LSHIFT==OP_ShiftLeft );
1576 assert( TK_RSHIFT==OP_ShiftRight );
1577 assert( TK_CONCAT==OP_Concat );
danielk19774adee202004-05-08 08:23:19 +00001578 sqlite3ExprCode(pParse, pExpr->pLeft);
1579 sqlite3ExprCode(pParse, pExpr->pRight);
drh855eb1c2004-08-31 13:45:11 +00001580 sqlite3VdbeAddOp(v, op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001581 stackChng = -1;
drh00400772000-06-16 20:51:26 +00001582 break;
1583 }
drhcce7d172000-05-31 15:34:51 +00001584 case TK_UMINUS: {
drhfec19aa2004-05-19 20:41:03 +00001585 Expr *pLeft = pExpr->pLeft;
1586 assert( pLeft );
1587 if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
1588 Token *p = &pLeft->token;
drh6e142f52000-06-08 13:36:40 +00001589 char *z = sqliteMalloc( p->n + 2 );
1590 sprintf(z, "-%.*s", p->n, p->z);
drhfec19aa2004-05-19 20:41:03 +00001591 if( pLeft->op==TK_FLOAT ){
1592 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001593 }else{
drhfec19aa2004-05-19 20:41:03 +00001594 codeInteger(v, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001595 }
drh6e142f52000-06-08 13:36:40 +00001596 sqliteFree(z);
1597 break;
1598 }
drh1ccde152000-06-17 13:12:39 +00001599 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +00001600 }
drhbf4133c2001-10-13 02:59:08 +00001601 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00001602 case TK_NOT: {
drhf2bc0132004-10-04 13:19:23 +00001603 assert( TK_BITNOT==OP_BitNot );
1604 assert( TK_NOT==OP_Not );
danielk19774adee202004-05-08 08:23:19 +00001605 sqlite3ExprCode(pParse, pExpr->pLeft);
1606 sqlite3VdbeAddOp(v, op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001607 stackChng = 0;
drhcce7d172000-05-31 15:34:51 +00001608 break;
1609 }
1610 case TK_ISNULL:
1611 case TK_NOTNULL: {
1612 int dest;
drhf2bc0132004-10-04 13:19:23 +00001613 assert( TK_ISNULL==OP_IsNull );
1614 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00001615 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1616 sqlite3ExprCode(pParse, pExpr->pLeft);
1617 dest = sqlite3VdbeCurrentAddr(v) + 2;
1618 sqlite3VdbeAddOp(v, op, 1, dest);
1619 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);
drhffe07b22005-11-03 00:41:17 +00001620 stackChng = 0;
drhf2bc0132004-10-04 13:19:23 +00001621 break;
drhcce7d172000-05-31 15:34:51 +00001622 }
drh22827922000-06-06 17:27:05 +00001623 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00001624 AggInfo *pInfo = pExpr->pAggInfo;
1625 sqlite3VdbeAddOp(v, OP_MemLoad, pInfo->aFunc[pExpr->iAgg].iMem, 0);
drh22827922000-06-06 17:27:05 +00001626 break;
1627 }
drhb71090f2005-05-23 17:26:51 +00001628 case TK_CONST_FUNC:
drhcce7d172000-05-31 15:34:51 +00001629 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00001630 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00001631 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00001632 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00001633 int nId;
1634 const char *zId;
drh13449892005-09-07 21:22:45 +00001635 int constMask = 0;
danielk1977682f68b2004-06-05 10:22:17 +00001636 int i;
danielk1977d8123362004-06-12 09:25:12 +00001637 u8 enc = pParse->db->enc;
danielk1977dc1bdc42004-06-11 10:51:27 +00001638 CollSeq *pColl = 0;
drhb71090f2005-05-23 17:26:51 +00001639 zId = pExpr->token.z;
1640 nId = pExpr->token.n;
danielk1977d8123362004-06-12 09:25:12 +00001641 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001642 assert( pDef!=0 );
drhf9b596e2004-05-26 16:54:42 +00001643 nExpr = sqlite3ExprCodeExprList(pParse, pList);
danielk1977682f68b2004-06-05 10:22:17 +00001644 for(i=0; i<nExpr && i<32; i++){
danielk1977d02eb1f2004-06-06 09:44:03 +00001645 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
drh13449892005-09-07 21:22:45 +00001646 constMask |= (1<<i);
danielk1977d02eb1f2004-06-06 09:44:03 +00001647 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001648 if( pDef->needCollSeq && !pColl ){
1649 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
1650 }
1651 }
1652 if( pDef->needCollSeq ){
1653 if( !pColl ) pColl = pParse->db->pDfltColl;
danielk1977d8123362004-06-12 09:25:12 +00001654 sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ);
danielk1977682f68b2004-06-05 10:22:17 +00001655 }
drh13449892005-09-07 21:22:45 +00001656 sqlite3VdbeOp3(v, OP_Function, constMask, nExpr, (char*)pDef, P3_FUNCDEF);
drhffe07b22005-11-03 00:41:17 +00001657 stackChng = 1-nExpr;
drhcce7d172000-05-31 15:34:51 +00001658 break;
1659 }
drhfe2093d2005-01-20 22:48:47 +00001660#ifndef SQLITE_OMIT_SUBQUERY
1661 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001662 case TK_SELECT: {
danielk1977b3bce662005-01-29 08:32:43 +00001663 sqlite3CodeSubselect(pParse, pExpr);
danielk19774adee202004-05-08 08:23:19 +00001664 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drhad6d9462004-09-19 02:15:24 +00001665 VdbeComment((v, "# load subquery result"));
drh19a775c2000-06-05 18:54:46 +00001666 break;
1667 }
drhfef52082000-06-06 01:50:43 +00001668 case TK_IN: {
1669 int addr;
drh94a11212004-09-25 13:12:14 +00001670 char affinity;
danielk1977b3bce662005-01-29 08:32:43 +00001671 sqlite3CodeSubselect(pParse, pExpr);
danielk1977e014a832004-05-17 10:48:57 +00001672
1673 /* Figure out the affinity to use to create a key from the results
1674 ** of the expression. affinityStr stores a static string suitable for
danielk1977ededfd52004-06-17 07:53:01 +00001675 ** P3 of OP_MakeRecord.
danielk1977e014a832004-05-17 10:48:57 +00001676 */
drh94a11212004-09-25 13:12:14 +00001677 affinity = comparisonAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +00001678
danielk19774adee202004-05-08 08:23:19 +00001679 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
danielk1977e014a832004-05-17 10:48:57 +00001680
1681 /* Code the <expr> from "<expr> IN (...)". The temporary table
1682 ** pExpr->iTable contains the values that make up the (...) set.
1683 */
danielk19774adee202004-05-08 08:23:19 +00001684 sqlite3ExprCode(pParse, pExpr->pLeft);
1685 addr = sqlite3VdbeCurrentAddr(v);
danielk1977e014a832004-05-17 10:48:57 +00001686 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); /* addr + 0 */
danielk19774adee202004-05-08 08:23:19 +00001687 sqlite3VdbeAddOp(v, OP_Pop, 2, 0);
drhf0863fe2005-06-12 21:35:51 +00001688 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +00001689 sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7);
drh94a11212004-09-25 13:12:14 +00001690 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1); /* addr + 4 */
danielk1977e014a832004-05-17 10:48:57 +00001691 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7);
1692 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); /* addr + 6 */
1693
drhfef52082000-06-06 01:50:43 +00001694 break;
1695 }
danielk197793758c82005-01-21 08:13:14 +00001696#endif
drhfef52082000-06-06 01:50:43 +00001697 case TK_BETWEEN: {
drhbe5c89a2004-07-26 00:31:09 +00001698 Expr *pLeft = pExpr->pLeft;
1699 struct ExprList_item *pLItem = pExpr->pList->a;
1700 Expr *pRight = pLItem->pExpr;
1701 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001702 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001703 sqlite3ExprCode(pParse, pRight);
1704 codeCompare(pParse, pLeft, pRight, OP_Ge, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001705 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
drhbe5c89a2004-07-26 00:31:09 +00001706 pLItem++;
1707 pRight = pLItem->pExpr;
1708 sqlite3ExprCode(pParse, pRight);
1709 codeCompare(pParse, pLeft, pRight, OP_Le, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001710 sqlite3VdbeAddOp(v, OP_And, 0, 0);
drhfef52082000-06-06 01:50:43 +00001711 break;
1712 }
drh51e9a442004-01-16 16:42:53 +00001713 case TK_UPLUS:
drha2e00042002-01-22 03:13:42 +00001714 case TK_AS: {
danielk19774adee202004-05-08 08:23:19 +00001715 sqlite3ExprCode(pParse, pExpr->pLeft);
drhffe07b22005-11-03 00:41:17 +00001716 stackChng = 0;
drha2e00042002-01-22 03:13:42 +00001717 break;
1718 }
drh17a7f8d2002-03-24 13:13:27 +00001719 case TK_CASE: {
1720 int expr_end_label;
drhf5905aa2002-05-26 20:54:33 +00001721 int jumpInst;
drhf5905aa2002-05-26 20:54:33 +00001722 int nExpr;
drh17a7f8d2002-03-24 13:13:27 +00001723 int i;
drhbe5c89a2004-07-26 00:31:09 +00001724 ExprList *pEList;
1725 struct ExprList_item *aListelem;
drh17a7f8d2002-03-24 13:13:27 +00001726
1727 assert(pExpr->pList);
1728 assert((pExpr->pList->nExpr % 2) == 0);
1729 assert(pExpr->pList->nExpr > 0);
drhbe5c89a2004-07-26 00:31:09 +00001730 pEList = pExpr->pList;
1731 aListelem = pEList->a;
1732 nExpr = pEList->nExpr;
danielk19774adee202004-05-08 08:23:19 +00001733 expr_end_label = sqlite3VdbeMakeLabel(v);
drh17a7f8d2002-03-24 13:13:27 +00001734 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001735 sqlite3ExprCode(pParse, pExpr->pLeft);
drh17a7f8d2002-03-24 13:13:27 +00001736 }
drhf5905aa2002-05-26 20:54:33 +00001737 for(i=0; i<nExpr; i=i+2){
drhbe5c89a2004-07-26 00:31:09 +00001738 sqlite3ExprCode(pParse, aListelem[i].pExpr);
drh17a7f8d2002-03-24 13:13:27 +00001739 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001740 sqlite3VdbeAddOp(v, OP_Dup, 1, 1);
drhbe5c89a2004-07-26 00:31:09 +00001741 jumpInst = codeCompare(pParse, pExpr->pLeft, aListelem[i].pExpr,
1742 OP_Ne, 0, 1);
danielk19774adee202004-05-08 08:23:19 +00001743 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001744 }else{
danielk19774adee202004-05-08 08:23:19 +00001745 jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001746 }
drhbe5c89a2004-07-26 00:31:09 +00001747 sqlite3ExprCode(pParse, aListelem[i+1].pExpr);
danielk19774adee202004-05-08 08:23:19 +00001748 sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label);
drhd654be82005-09-20 17:42:23 +00001749 sqlite3VdbeJumpHere(v, jumpInst);
drh17a7f8d2002-03-24 13:13:27 +00001750 }
drhf570f012002-05-31 15:51:25 +00001751 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001752 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhf570f012002-05-31 15:51:25 +00001753 }
drh17a7f8d2002-03-24 13:13:27 +00001754 if( pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001755 sqlite3ExprCode(pParse, pExpr->pRight);
drh17a7f8d2002-03-24 13:13:27 +00001756 }else{
drhf0863fe2005-06-12 21:35:51 +00001757 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
drh17a7f8d2002-03-24 13:13:27 +00001758 }
danielk19774adee202004-05-08 08:23:19 +00001759 sqlite3VdbeResolveLabel(v, expr_end_label);
danielk19776f349032002-06-11 02:25:40 +00001760 break;
1761 }
danielk19775338a5f2005-01-20 13:03:10 +00001762#ifndef SQLITE_OMIT_TRIGGER
danielk19776f349032002-06-11 02:25:40 +00001763 case TK_RAISE: {
1764 if( !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +00001765 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00001766 "RAISE() may only be used within a trigger-program");
danielk19776f349032002-06-11 02:25:40 +00001767 return;
1768 }
drhad6d9462004-09-19 02:15:24 +00001769 if( pExpr->iColumn!=OE_Ignore ){
1770 assert( pExpr->iColumn==OE_Rollback ||
1771 pExpr->iColumn == OE_Abort ||
1772 pExpr->iColumn == OE_Fail );
drhd2687b72005-08-12 22:56:09 +00001773 sqlite3DequoteExpr(pExpr);
drhad6d9462004-09-19 02:15:24 +00001774 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn,
1775 pExpr->token.z, pExpr->token.n);
danielk19776f349032002-06-11 02:25:40 +00001776 } else {
drhad6d9462004-09-19 02:15:24 +00001777 assert( pExpr->iColumn == OE_Ignore );
1778 sqlite3VdbeAddOp(v, OP_ContextPop, 0, 0);
1779 sqlite3VdbeAddOp(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
1780 VdbeComment((v, "# raise(IGNORE)"));
danielk19776f349032002-06-11 02:25:40 +00001781 }
drhffe07b22005-11-03 00:41:17 +00001782 stackChng = 0;
1783 break;
drh17a7f8d2002-03-24 13:13:27 +00001784 }
danielk19775338a5f2005-01-20 13:03:10 +00001785#endif
drhffe07b22005-11-03 00:41:17 +00001786 }
1787
1788 if( pParse->ckOffset ){
1789 pParse->ckOffset += stackChng;
1790 assert( pParse->ckOffset );
drhcce7d172000-05-31 15:34:51 +00001791 }
drhcce7d172000-05-31 15:34:51 +00001792}
1793
danielk197793758c82005-01-21 08:13:14 +00001794#ifndef SQLITE_OMIT_TRIGGER
drhcce7d172000-05-31 15:34:51 +00001795/*
drh25303782004-12-07 15:41:48 +00001796** Generate code that evalutes the given expression and leaves the result
1797** on the stack. See also sqlite3ExprCode().
1798**
1799** This routine might also cache the result and modify the pExpr tree
1800** so that it will make use of the cached result on subsequent evaluations
1801** rather than evaluate the whole expression again. Trivial expressions are
1802** not cached. If the expression is cached, its result is stored in a
1803** memory location.
1804*/
1805void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr){
1806 Vdbe *v = pParse->pVdbe;
1807 int iMem;
1808 int addr1, addr2;
1809 if( v==0 ) return;
1810 addr1 = sqlite3VdbeCurrentAddr(v);
1811 sqlite3ExprCode(pParse, pExpr);
1812 addr2 = sqlite3VdbeCurrentAddr(v);
1813 if( addr2>addr1+1 || sqlite3VdbeGetOp(v, addr1)->opcode==OP_Function ){
1814 iMem = pExpr->iTable = pParse->nMem++;
1815 sqlite3VdbeAddOp(v, OP_MemStore, iMem, 0);
1816 pExpr->op = TK_REGISTER;
1817 }
1818}
danielk197793758c82005-01-21 08:13:14 +00001819#endif
drh25303782004-12-07 15:41:48 +00001820
1821/*
drh268380c2004-02-25 13:47:31 +00001822** Generate code that pushes the value of every element of the given
drhf9b596e2004-05-26 16:54:42 +00001823** expression list onto the stack.
drh268380c2004-02-25 13:47:31 +00001824**
1825** Return the number of elements pushed onto the stack.
1826*/
danielk19774adee202004-05-08 08:23:19 +00001827int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00001828 Parse *pParse, /* Parsing context */
drhf9b596e2004-05-26 16:54:42 +00001829 ExprList *pList /* The expression list to be coded */
drh268380c2004-02-25 13:47:31 +00001830){
1831 struct ExprList_item *pItem;
1832 int i, n;
drh268380c2004-02-25 13:47:31 +00001833 if( pList==0 ) return 0;
drh268380c2004-02-25 13:47:31 +00001834 n = pList->nExpr;
drhc182d162005-08-14 20:47:16 +00001835 for(pItem=pList->a, i=n; i>0; i--, pItem++){
danielk19774adee202004-05-08 08:23:19 +00001836 sqlite3ExprCode(pParse, pItem->pExpr);
drh268380c2004-02-25 13:47:31 +00001837 }
drhf9b596e2004-05-26 16:54:42 +00001838 return n;
drh268380c2004-02-25 13:47:31 +00001839}
1840
1841/*
drhcce7d172000-05-31 15:34:51 +00001842** Generate code for a boolean expression such that a jump is made
1843** to the label "dest" if the expression is true but execution
1844** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00001845**
1846** If the expression evaluates to NULL (neither true nor false), then
1847** take the jump if the jumpIfNull flag is true.
drhf2bc0132004-10-04 13:19:23 +00001848**
1849** This code depends on the fact that certain token values (ex: TK_EQ)
1850** are the same as opcode values (ex: OP_Eq) that implement the corresponding
1851** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
1852** the make process cause these values to align. Assert()s in the code
1853** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00001854*/
danielk19774adee202004-05-08 08:23:19 +00001855void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001856 Vdbe *v = pParse->pVdbe;
1857 int op = 0;
drhffe07b22005-11-03 00:41:17 +00001858 int ckOffset = pParse->ckOffset;
drhdaffd0e2001-04-11 14:28:42 +00001859 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00001860 op = pExpr->op;
1861 switch( op ){
drhcce7d172000-05-31 15:34:51 +00001862 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001863 int d2 = sqlite3VdbeMakeLabel(v);
1864 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
1865 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
1866 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001867 break;
1868 }
1869 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001870 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
1871 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001872 break;
1873 }
1874 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001875 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001876 break;
1877 }
1878 case TK_LT:
1879 case TK_LE:
1880 case TK_GT:
1881 case TK_GE:
1882 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00001883 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00001884 assert( TK_LT==OP_Lt );
1885 assert( TK_LE==OP_Le );
1886 assert( TK_GT==OP_Gt );
1887 assert( TK_GE==OP_Ge );
1888 assert( TK_EQ==OP_Eq );
1889 assert( TK_NE==OP_Ne );
danielk19774adee202004-05-08 08:23:19 +00001890 sqlite3ExprCode(pParse, pExpr->pLeft);
1891 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001892 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001893 break;
1894 }
1895 case TK_ISNULL:
1896 case TK_NOTNULL: {
drhf2bc0132004-10-04 13:19:23 +00001897 assert( TK_ISNULL==OP_IsNull );
1898 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00001899 sqlite3ExprCode(pParse, pExpr->pLeft);
1900 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001901 break;
1902 }
drhfef52082000-06-06 01:50:43 +00001903 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00001904 /* The expression "x BETWEEN y AND z" is implemented as:
1905 **
1906 ** 1 IF (x < y) GOTO 3
1907 ** 2 IF (x <= z) GOTO <dest>
1908 ** 3 ...
1909 */
drhf5905aa2002-05-26 20:54:33 +00001910 int addr;
drhbe5c89a2004-07-26 00:31:09 +00001911 Expr *pLeft = pExpr->pLeft;
1912 Expr *pRight = pExpr->pList->a[0].pExpr;
1913 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001914 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001915 sqlite3ExprCode(pParse, pRight);
1916 addr = codeCompare(pParse, pLeft, pRight, OP_Lt, 0, !jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00001917
drhbe5c89a2004-07-26 00:31:09 +00001918 pRight = pExpr->pList->a[1].pExpr;
1919 sqlite3ExprCode(pParse, pRight);
1920 codeCompare(pParse, pLeft, pRight, OP_Le, dest, jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00001921
danielk19774adee202004-05-08 08:23:19 +00001922 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
drhd654be82005-09-20 17:42:23 +00001923 sqlite3VdbeJumpHere(v, addr);
danielk19774adee202004-05-08 08:23:19 +00001924 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00001925 break;
1926 }
drhcce7d172000-05-31 15:34:51 +00001927 default: {
danielk19774adee202004-05-08 08:23:19 +00001928 sqlite3ExprCode(pParse, pExpr);
1929 sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001930 break;
1931 }
1932 }
drhffe07b22005-11-03 00:41:17 +00001933 pParse->ckOffset = ckOffset;
drhcce7d172000-05-31 15:34:51 +00001934}
1935
1936/*
drh66b89c82000-11-28 20:47:17 +00001937** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00001938** to the label "dest" if the expression is false but execution
1939** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00001940**
1941** If the expression evaluates to NULL (neither true nor false) then
1942** jump if jumpIfNull is true or fall through if jumpIfNull is false.
drhcce7d172000-05-31 15:34:51 +00001943*/
danielk19774adee202004-05-08 08:23:19 +00001944void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001945 Vdbe *v = pParse->pVdbe;
1946 int op = 0;
drhffe07b22005-11-03 00:41:17 +00001947 int ckOffset = pParse->ckOffset;
drhdaffd0e2001-04-11 14:28:42 +00001948 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00001949
1950 /* The value of pExpr->op and op are related as follows:
1951 **
1952 ** pExpr->op op
1953 ** --------- ----------
1954 ** TK_ISNULL OP_NotNull
1955 ** TK_NOTNULL OP_IsNull
1956 ** TK_NE OP_Eq
1957 ** TK_EQ OP_Ne
1958 ** TK_GT OP_Le
1959 ** TK_LE OP_Gt
1960 ** TK_GE OP_Lt
1961 ** TK_LT OP_Ge
1962 **
1963 ** For other values of pExpr->op, op is undefined and unused.
1964 ** The value of TK_ and OP_ constants are arranged such that we
1965 ** can compute the mapping above using the following expression.
1966 ** Assert()s verify that the computation is correct.
1967 */
1968 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
1969
1970 /* Verify correct alignment of TK_ and OP_ constants
1971 */
1972 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
1973 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
1974 assert( pExpr->op!=TK_NE || op==OP_Eq );
1975 assert( pExpr->op!=TK_EQ || op==OP_Ne );
1976 assert( pExpr->op!=TK_LT || op==OP_Ge );
1977 assert( pExpr->op!=TK_LE || op==OP_Gt );
1978 assert( pExpr->op!=TK_GT || op==OP_Le );
1979 assert( pExpr->op!=TK_GE || op==OP_Lt );
1980
drhcce7d172000-05-31 15:34:51 +00001981 switch( pExpr->op ){
1982 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001983 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
1984 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001985 break;
1986 }
1987 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001988 int d2 = sqlite3VdbeMakeLabel(v);
1989 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
1990 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
1991 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001992 break;
1993 }
1994 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001995 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001996 break;
1997 }
1998 case TK_LT:
1999 case TK_LE:
2000 case TK_GT:
2001 case TK_GE:
2002 case TK_NE:
2003 case TK_EQ: {
danielk19774adee202004-05-08 08:23:19 +00002004 sqlite3ExprCode(pParse, pExpr->pLeft);
2005 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00002006 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002007 break;
2008 }
drhcce7d172000-05-31 15:34:51 +00002009 case TK_ISNULL:
2010 case TK_NOTNULL: {
danielk19774adee202004-05-08 08:23:19 +00002011 sqlite3ExprCode(pParse, pExpr->pLeft);
2012 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00002013 break;
2014 }
drhfef52082000-06-06 01:50:43 +00002015 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00002016 /* The expression is "x BETWEEN y AND z". It is implemented as:
2017 **
2018 ** 1 IF (x >= y) GOTO 3
2019 ** 2 GOTO <dest>
2020 ** 3 IF (x > z) GOTO <dest>
2021 */
drhfef52082000-06-06 01:50:43 +00002022 int addr;
drhbe5c89a2004-07-26 00:31:09 +00002023 Expr *pLeft = pExpr->pLeft;
2024 Expr *pRight = pExpr->pList->a[0].pExpr;
2025 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00002026 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00002027 sqlite3ExprCode(pParse, pRight);
danielk19774adee202004-05-08 08:23:19 +00002028 addr = sqlite3VdbeCurrentAddr(v);
drhbe5c89a2004-07-26 00:31:09 +00002029 codeCompare(pParse, pLeft, pRight, OP_Ge, addr+3, !jumpIfNull);
2030
danielk19774adee202004-05-08 08:23:19 +00002031 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
2032 sqlite3VdbeAddOp(v, OP_Goto, 0, dest);
drhbe5c89a2004-07-26 00:31:09 +00002033 pRight = pExpr->pList->a[1].pExpr;
2034 sqlite3ExprCode(pParse, pRight);
2035 codeCompare(pParse, pLeft, pRight, OP_Gt, dest, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00002036 break;
2037 }
drhcce7d172000-05-31 15:34:51 +00002038 default: {
danielk19774adee202004-05-08 08:23:19 +00002039 sqlite3ExprCode(pParse, pExpr);
2040 sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00002041 break;
2042 }
2043 }
drhffe07b22005-11-03 00:41:17 +00002044 pParse->ckOffset = ckOffset;
drhcce7d172000-05-31 15:34:51 +00002045}
drh22827922000-06-06 17:27:05 +00002046
2047/*
2048** Do a deep comparison of two expression trees. Return TRUE (non-zero)
2049** if they are identical and return FALSE if they differ in any way.
2050*/
danielk19774adee202004-05-08 08:23:19 +00002051int sqlite3ExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00002052 int i;
2053 if( pA==0 ){
2054 return pB==0;
2055 }else if( pB==0 ){
2056 return 0;
2057 }
2058 if( pA->op!=pB->op ) return 0;
drhfd357972005-09-09 01:33:19 +00002059 if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 0;
danielk19774adee202004-05-08 08:23:19 +00002060 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
2061 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00002062 if( pA->pList ){
2063 if( pB->pList==0 ) return 0;
2064 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
2065 for(i=0; i<pA->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00002066 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00002067 return 0;
2068 }
2069 }
2070 }else if( pB->pList ){
2071 return 0;
2072 }
2073 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00002074 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drh22827922000-06-06 17:27:05 +00002075 if( pA->token.z ){
2076 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00002077 if( pB->token.n!=pA->token.n ) return 0;
danielk19774adee202004-05-08 08:23:19 +00002078 if( sqlite3StrNICmp(pA->token.z, pB->token.z, pB->token.n)!=0 ) return 0;
drh22827922000-06-06 17:27:05 +00002079 }
2080 return 1;
2081}
2082
drh13449892005-09-07 21:22:45 +00002083
drh22827922000-06-06 17:27:05 +00002084/*
drh13449892005-09-07 21:22:45 +00002085** Add a new element to the pAggInfo->aCol[] array. Return the index of
2086** the new element. Return a negative number if malloc fails.
drh22827922000-06-06 17:27:05 +00002087*/
drh13449892005-09-07 21:22:45 +00002088static int addAggInfoColumn(AggInfo *pInfo){
2089 int i;
2090 i = sqlite3ArrayAllocate((void**)&pInfo->aCol, sizeof(pInfo->aCol[0]), 3);
2091 if( i<0 ){
2092 return -1;
drh22827922000-06-06 17:27:05 +00002093 }
drh13449892005-09-07 21:22:45 +00002094 return i;
2095}
2096
2097/*
2098** Add a new element to the pAggInfo->aFunc[] array. Return the index of
2099** the new element. Return a negative number if malloc fails.
2100*/
2101static int addAggInfoFunc(AggInfo *pInfo){
2102 int i;
2103 i = sqlite3ArrayAllocate((void**)&pInfo->aFunc, sizeof(pInfo->aFunc[0]), 2);
2104 if( i<0 ){
2105 return -1;
2106 }
2107 return i;
2108}
drh22827922000-06-06 17:27:05 +00002109
2110/*
drh626a8792005-01-17 22:08:19 +00002111** This is an xFunc for walkExprTree() used to implement
2112** sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates
2113** for additional information.
drh22827922000-06-06 17:27:05 +00002114**
drh626a8792005-01-17 22:08:19 +00002115** This routine analyzes the aggregate function at pExpr.
drh22827922000-06-06 17:27:05 +00002116*/
drh626a8792005-01-17 22:08:19 +00002117static int analyzeAggregate(void *pArg, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00002118 int i;
danielk1977a58fdfb2005-02-08 07:50:40 +00002119 NameContext *pNC = (NameContext *)pArg;
2120 Parse *pParse = pNC->pParse;
2121 SrcList *pSrcList = pNC->pSrcList;
drh13449892005-09-07 21:22:45 +00002122 AggInfo *pAggInfo = pNC->pAggInfo;
2123
drh22827922000-06-06 17:27:05 +00002124
drh22827922000-06-06 17:27:05 +00002125 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00002126 case TK_COLUMN: {
drh13449892005-09-07 21:22:45 +00002127 /* Check to see if the column is in one of the tables in the FROM
2128 ** clause of the aggregate query */
2129 if( pSrcList ){
2130 struct SrcList_item *pItem = pSrcList->a;
2131 for(i=0; i<pSrcList->nSrc; i++, pItem++){
2132 struct AggInfo_col *pCol;
2133 if( pExpr->iTable==pItem->iCursor ){
2134 /* If we reach this point, it means that pExpr refers to a table
2135 ** that is in the FROM clause of the aggregate query.
2136 **
2137 ** Make an entry for the column in pAggInfo->aCol[] if there
2138 ** is not an entry there already.
2139 */
2140 pCol = pAggInfo->aCol;
2141 for(i=0; i<pAggInfo->nColumn; i++, pCol++){
2142 if( pCol->iTable==pExpr->iTable &&
2143 pCol->iColumn==pExpr->iColumn ){
2144 break;
2145 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002146 }
drh13449892005-09-07 21:22:45 +00002147 if( i>=pAggInfo->nColumn && (i = addAggInfoColumn(pAggInfo))>=0 ){
2148 pCol = &pAggInfo->aCol[i];
2149 pCol->iTable = pExpr->iTable;
2150 pCol->iColumn = pExpr->iColumn;
2151 pCol->iMem = pParse->nMem++;
2152 pCol->iSorterColumn = -1;
drh5774b802005-09-07 22:48:16 +00002153 pCol->pExpr = pExpr;
drh13449892005-09-07 21:22:45 +00002154 if( pAggInfo->pGroupBy ){
2155 int j, n;
2156 ExprList *pGB = pAggInfo->pGroupBy;
2157 struct ExprList_item *pTerm = pGB->a;
2158 n = pGB->nExpr;
2159 for(j=0; j<n; j++, pTerm++){
2160 Expr *pE = pTerm->pExpr;
2161 if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable &&
2162 pE->iColumn==pExpr->iColumn ){
2163 pCol->iSorterColumn = j;
2164 break;
2165 }
2166 }
2167 }
2168 if( pCol->iSorterColumn<0 ){
2169 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
2170 }
2171 }
2172 /* There is now an entry for pExpr in pAggInfo->aCol[] (either
2173 ** because it was there before or because we just created it).
2174 ** Convert the pExpr to be a TK_AGG_COLUMN referring to that
2175 ** pAggInfo->aCol[] entry.
2176 */
2177 pExpr->pAggInfo = pAggInfo;
2178 pExpr->op = TK_AGG_COLUMN;
2179 pExpr->iAgg = i;
2180 break;
2181 } /* endif pExpr->iTable==pItem->iCursor */
2182 } /* end loop over pSrcList */
drh22827922000-06-06 17:27:05 +00002183 }
drh626a8792005-01-17 22:08:19 +00002184 return 1;
drh22827922000-06-06 17:27:05 +00002185 }
2186 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00002187 /* The pNC->nDepth==0 test causes aggregate functions in subqueries
2188 ** to be ignored */
danielk1977a58fdfb2005-02-08 07:50:40 +00002189 if( pNC->nDepth==0 ){
drh13449892005-09-07 21:22:45 +00002190 /* Check to see if pExpr is a duplicate of another aggregate
2191 ** function that is already in the pAggInfo structure
2192 */
2193 struct AggInfo_func *pItem = pAggInfo->aFunc;
2194 for(i=0; i<pAggInfo->nFunc; i++, pItem++){
2195 if( sqlite3ExprCompare(pItem->pExpr, pExpr) ){
danielk1977a58fdfb2005-02-08 07:50:40 +00002196 break;
2197 }
drh22827922000-06-06 17:27:05 +00002198 }
drh13449892005-09-07 21:22:45 +00002199 if( i>=pAggInfo->nFunc ){
2200 /* pExpr is original. Make a new entry in pAggInfo->aFunc[]
2201 */
danielk1977a58fdfb2005-02-08 07:50:40 +00002202 u8 enc = pParse->db->enc;
drh13449892005-09-07 21:22:45 +00002203 i = addAggInfoFunc(pAggInfo);
2204 if( i>=0 ){
2205 pItem = &pAggInfo->aFunc[i];
2206 pItem->pExpr = pExpr;
2207 pItem->iMem = pParse->nMem++;
2208 pItem->pFunc = sqlite3FindFunction(pParse->db,
2209 pExpr->token.z, pExpr->token.n,
2210 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
drhfd357972005-09-09 01:33:19 +00002211 if( pExpr->flags & EP_Distinct ){
2212 pItem->iDistinct = pParse->nTab++;
2213 }else{
2214 pItem->iDistinct = -1;
2215 }
drh13449892005-09-07 21:22:45 +00002216 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002217 }
drh13449892005-09-07 21:22:45 +00002218 /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
2219 */
danielk1977a58fdfb2005-02-08 07:50:40 +00002220 pExpr->iAgg = i;
drh13449892005-09-07 21:22:45 +00002221 pExpr->pAggInfo = pAggInfo;
danielk1977a58fdfb2005-02-08 07:50:40 +00002222 return 1;
drh22827922000-06-06 17:27:05 +00002223 }
drh22827922000-06-06 17:27:05 +00002224 }
2225 }
drh13449892005-09-07 21:22:45 +00002226
2227 /* Recursively walk subqueries looking for TK_COLUMN nodes that need
2228 ** to be changed to TK_AGG_COLUMN. But increment nDepth so that
2229 ** TK_AGG_FUNCTION nodes in subqueries will be unchanged.
2230 */
danielk1977a58fdfb2005-02-08 07:50:40 +00002231 if( pExpr->pSelect ){
2232 pNC->nDepth++;
2233 walkSelectExpr(pExpr->pSelect, analyzeAggregate, pNC);
2234 pNC->nDepth--;
2235 }
drh626a8792005-01-17 22:08:19 +00002236 return 0;
2237}
2238
2239/*
2240** Analyze the given expression looking for aggregate functions and
2241** for variables that need to be added to the pParse->aAgg[] array.
2242** Make additional entries to the pParse->aAgg[] array as necessary.
2243**
2244** This routine should only be called after the expression has been
2245** analyzed by sqlite3ExprResolveNames().
2246**
2247** If errors are seen, leave an error message in zErrMsg and return
2248** the number of errors.
2249*/
danielk1977a58fdfb2005-02-08 07:50:40 +00002250int sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
2251 int nErr = pNC->pParse->nErr;
2252 walkExprTree(pExpr, analyzeAggregate, pNC);
2253 return pNC->pParse->nErr - nErr;
drh22827922000-06-06 17:27:05 +00002254}
drh5d9a4af2005-08-30 00:54:01 +00002255
2256/*
2257** Call sqlite3ExprAnalyzeAggregates() for every expression in an
2258** expression list. Return the number of errors.
2259**
2260** If an error is found, the analysis is cut short.
2261*/
2262int sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){
2263 struct ExprList_item *pItem;
2264 int i;
2265 int nErr = 0;
2266 if( pList ){
2267 for(pItem=pList->a, i=0; nErr==0 && i<pList->nExpr; i++, pItem++){
2268 nErr += sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
2269 }
2270 }
2271 return nErr;
2272}