blob: 169fdf94293a0b9252d69c42db3fb1261134a709 [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**
danielk19774b202ae2006-01-23 05:50:58 +000015** $Id: expr.c,v 1.251 2006/01/23 05:50:58 danielk1977 Exp $
drhcce7d172000-05-31 15:34:51 +000016*/
17#include "sqliteInt.h"
drh04738cb2002-06-02 18:19:00 +000018#include <ctype.h>
drha2e00042002-01-22 03:13:42 +000019
danielk1977e014a832004-05-17 10:48:57 +000020/*
21** Return the 'affinity' of the expression pExpr if any.
22**
23** If pExpr is a column, a reference to a column via an 'AS' alias,
24** or a sub-select with a column as the return value, then the
25** affinity of that column is returned. Otherwise, 0x00 is returned,
26** indicating no affinity for the expression.
27**
28** i.e. the WHERE clause expresssions in the following statements all
29** have an affinity:
30**
31** CREATE TABLE t1(a);
32** SELECT * FROM t1 WHERE a;
33** SELECT a AS b FROM t1 WHERE b;
34** SELECT * FROM t1 WHERE (select a from t1);
35*/
danielk1977bf3b7212004-05-18 10:06:24 +000036char sqlite3ExprAffinity(Expr *pExpr){
drh487e2622005-06-25 18:42:14 +000037 int op = pExpr->op;
38 if( op==TK_AS ){
danielk1977bf3b7212004-05-18 10:06:24 +000039 return sqlite3ExprAffinity(pExpr->pLeft);
danielk1977a37cdde2004-05-16 11:15:36 +000040 }
drh487e2622005-06-25 18:42:14 +000041 if( op==TK_SELECT ){
danielk1977bf3b7212004-05-18 10:06:24 +000042 return sqlite3ExprAffinity(pExpr->pSelect->pEList->a[0].pExpr);
danielk1977a37cdde2004-05-16 11:15:36 +000043 }
drh487e2622005-06-25 18:42:14 +000044#ifndef SQLITE_OMIT_CAST
45 if( op==TK_CAST ){
drh8a512562005-11-14 22:29:05 +000046 return sqlite3AffinityType(&pExpr->token);
drh487e2622005-06-25 18:42:14 +000047 }
48#endif
danielk1977a37cdde2004-05-16 11:15:36 +000049 return pExpr->affinity;
50}
51
drh53db1452004-05-20 13:54:53 +000052/*
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 */
drh8a512562005-11-14 22:29:05 +000081 if( sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2) ){
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);
drh8a512562005-11-14 22:29:05 +0000129 switch( aff ){
130 case SQLITE_AFF_NONE:
131 return 1;
132 case SQLITE_AFF_TEXT:
133 return idx_affinity==SQLITE_AFF_TEXT;
134 default:
135 return sqlite3IsNumericAffinity(idx_affinity);
136 }
danielk1977e014a832004-05-17 10:48:57 +0000137}
138
danielk1977a37cdde2004-05-16 11:15:36 +0000139/*
140** Return the P1 value that should be used for a binary comparison
141** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
142** If jumpIfNull is true, then set the low byte of the returned
143** P1 value to tell the opcode to jump if either expression
144** evaluates to NULL.
145*/
danielk1977e014a832004-05-17 10:48:57 +0000146static int binaryCompareP1(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){
danielk1977bf3b7212004-05-18 10:06:24 +0000147 char aff = sqlite3ExprAffinity(pExpr2);
drhf0863fe2005-06-12 21:35:51 +0000148 return ((int)sqlite3CompareAffinity(pExpr1, aff))+(jumpIfNull?0x100:0);
danielk1977a37cdde2004-05-16 11:15:36 +0000149}
150
drha2e00042002-01-22 03:13:42 +0000151/*
danielk19770202b292004-06-09 09:55:16 +0000152** Return a pointer to the collation sequence that should be used by
153** a binary comparison operator comparing pLeft and pRight.
154**
155** If the left hand expression has a collating sequence type, then it is
156** used. Otherwise the collation sequence for the right hand expression
157** is used, or the default (BINARY) if neither expression has a collating
158** type.
159*/
danielk19777cedc8d2004-06-10 10:50:08 +0000160static CollSeq* binaryCompareCollSeq(Parse *pParse, Expr *pLeft, Expr *pRight){
161 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pLeft);
danielk19770202b292004-06-09 09:55:16 +0000162 if( !pColl ){
danielk19777cedc8d2004-06-10 10:50:08 +0000163 pColl = sqlite3ExprCollSeq(pParse, pRight);
danielk19770202b292004-06-09 09:55:16 +0000164 }
165 return pColl;
166}
167
168/*
drhbe5c89a2004-07-26 00:31:09 +0000169** Generate code for a comparison operator.
170*/
171static int codeCompare(
172 Parse *pParse, /* The parsing (and code generating) context */
173 Expr *pLeft, /* The left operand */
174 Expr *pRight, /* The right operand */
175 int opcode, /* The comparison opcode */
176 int dest, /* Jump here if true. */
177 int jumpIfNull /* If true, jump if either operand is NULL */
178){
179 int p1 = binaryCompareP1(pLeft, pRight, jumpIfNull);
180 CollSeq *p3 = binaryCompareCollSeq(pParse, pLeft, pRight);
drh94a11212004-09-25 13:12:14 +0000181 return sqlite3VdbeOp3(pParse->pVdbe, opcode, p1, dest, (void*)p3, P3_COLLSEQ);
drhbe5c89a2004-07-26 00:31:09 +0000182}
183
184/*
drha76b5df2002-02-23 02:32:10 +0000185** Construct a new expression node and return a pointer to it. Memory
186** for this node is obtained from sqliteMalloc(). The calling function
187** is responsible for making sure the node eventually gets freed.
188*/
drhe4e72072004-11-23 01:47:30 +0000189Expr *sqlite3Expr(int op, Expr *pLeft, Expr *pRight, const Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000190 Expr *pNew;
191 pNew = sqliteMalloc( sizeof(Expr) );
192 if( pNew==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000193 /* When malloc fails, delete pLeft and pRight. Expressions passed to
194 ** this function must always be allocated with sqlite3Expr() for this
195 ** reason.
196 */
197 sqlite3ExprDelete(pLeft);
198 sqlite3ExprDelete(pRight);
drha76b5df2002-02-23 02:32:10 +0000199 return 0;
200 }
201 pNew->op = op;
202 pNew->pLeft = pLeft;
203 pNew->pRight = pRight;
danielk1977a58fdfb2005-02-08 07:50:40 +0000204 pNew->iAgg = -1;
drha76b5df2002-02-23 02:32:10 +0000205 if( pToken ){
drh4b59ab52002-08-24 18:24:51 +0000206 assert( pToken->dyn==0 );
drh145716b2004-09-24 12:24:06 +0000207 pNew->span = pNew->token = *pToken;
208 }else if( pLeft && pRight ){
209 sqlite3ExprSpan(pNew, &pLeft->span, &pRight->span);
drha76b5df2002-02-23 02:32:10 +0000210 }
drha76b5df2002-02-23 02:32:10 +0000211 return pNew;
212}
213
214/*
drh4e0cff62004-11-05 05:10:28 +0000215** When doing a nested parse, you can include terms in an expression
216** that look like this: #0 #1 #2 ... These terms refer to elements
drh288d37f2005-06-22 08:48:06 +0000217** on the stack. "#0" means the top of the stack.
218** "#1" means the next down on the stack. And so forth.
drh4e0cff62004-11-05 05:10:28 +0000219**
220** This routine is called by the parser to deal with on of those terms.
221** It immediately generates code to store the value in a memory location.
222** The returns an expression that will code to extract the value from
223** that memory location as needed.
224*/
225Expr *sqlite3RegisterExpr(Parse *pParse, Token *pToken){
226 Vdbe *v = pParse->pVdbe;
227 Expr *p;
228 int depth;
drh4e0cff62004-11-05 05:10:28 +0000229 if( pParse->nested==0 ){
230 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", pToken);
231 return 0;
232 }
drhbb7ac002005-08-12 22:58:53 +0000233 if( v==0 ) return 0;
drh4e0cff62004-11-05 05:10:28 +0000234 p = sqlite3Expr(TK_REGISTER, 0, 0, pToken);
drh73c42a12004-11-20 18:13:10 +0000235 if( p==0 ){
236 return 0; /* Malloc failed */
237 }
drh2646da72005-12-09 20:02:05 +0000238 depth = atoi((char*)&pToken->z[1]);
drh288d37f2005-06-22 08:48:06 +0000239 p->iTable = pParse->nMem++;
240 sqlite3VdbeAddOp(v, OP_Dup, depth, 0);
241 sqlite3VdbeAddOp(v, OP_MemStore, p->iTable, 1);
drh4e0cff62004-11-05 05:10:28 +0000242 return p;
243}
244
245/*
drh91bb0ee2004-09-01 03:06:34 +0000246** Join two expressions using an AND operator. If either expression is
247** NULL, then just return the other expression.
248*/
249Expr *sqlite3ExprAnd(Expr *pLeft, Expr *pRight){
250 if( pLeft==0 ){
251 return pRight;
252 }else if( pRight==0 ){
253 return pLeft;
254 }else{
255 return sqlite3Expr(TK_AND, pLeft, pRight, 0);
256 }
257}
258
259/*
drh6977fea2002-10-22 23:38:04 +0000260** Set the Expr.span field of the given expression to span all
drha76b5df2002-02-23 02:32:10 +0000261** text between the two given tokens.
262*/
danielk19774adee202004-05-08 08:23:19 +0000263void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
drh4efc4752004-01-16 15:55:37 +0000264 assert( pRight!=0 );
265 assert( pLeft!=0 );
danielk19779e128002006-01-18 16:51:35 +0000266 if( !sqlite3MallocFailed() && pRight->z && pLeft->z ){
drhad6d9462004-09-19 02:15:24 +0000267 assert( pLeft->dyn==0 || pLeft->z[pLeft->n]==0 );
drh145716b2004-09-24 12:24:06 +0000268 if( pLeft->dyn==0 && pRight->dyn==0 ){
drh6977fea2002-10-22 23:38:04 +0000269 pExpr->span.z = pLeft->z;
drh97903fe2005-05-24 20:19:57 +0000270 pExpr->span.n = pRight->n + (pRight->z - pLeft->z);
drh4b59ab52002-08-24 18:24:51 +0000271 }else{
drh6977fea2002-10-22 23:38:04 +0000272 pExpr->span.z = 0;
drh4b59ab52002-08-24 18:24:51 +0000273 }
drha76b5df2002-02-23 02:32:10 +0000274 }
275}
276
277/*
278** Construct a new expression node for a function with multiple
279** arguments.
280*/
danielk19774adee202004-05-08 08:23:19 +0000281Expr *sqlite3ExprFunction(ExprList *pList, Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000282 Expr *pNew;
danielk19774b202ae2006-01-23 05:50:58 +0000283 assert( pToken );
drha76b5df2002-02-23 02:32:10 +0000284 pNew = sqliteMalloc( sizeof(Expr) );
285 if( pNew==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000286 sqlite3ExprListDelete(pList); /* Avoid leaking memory when malloc fails */
drha76b5df2002-02-23 02:32:10 +0000287 return 0;
288 }
289 pNew->op = TK_FUNCTION;
290 pNew->pList = pList;
danielk19774b202ae2006-01-23 05:50:58 +0000291 assert( pToken->dyn==0 );
292 pNew->token = *pToken;
drh6977fea2002-10-22 23:38:04 +0000293 pNew->span = pNew->token;
drha76b5df2002-02-23 02:32:10 +0000294 return pNew;
295}
296
297/*
drhfa6bc002004-09-07 16:19:52 +0000298** Assign a variable number to an expression that encodes a wildcard
299** in the original SQL statement.
300**
301** Wildcards consisting of a single "?" are assigned the next sequential
302** variable number.
303**
304** Wildcards of the form "?nnn" are assigned the number "nnn". We make
305** sure "nnn" is not too be to avoid a denial of service attack when
306** the SQL statement comes from an external source.
307**
308** Wildcards of the form ":aaa" or "$aaa" are assigned the same number
309** as the previous instance of the same wildcard. Or if this is the first
310** instance of the wildcard, the next sequenial variable number is
311** assigned.
312*/
313void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){
314 Token *pToken;
315 if( pExpr==0 ) return;
316 pToken = &pExpr->token;
317 assert( pToken->n>=1 );
318 assert( pToken->z!=0 );
319 assert( pToken->z[0]!=0 );
320 if( pToken->n==1 ){
321 /* Wildcard of the form "?". Assign the next variable number */
322 pExpr->iTable = ++pParse->nVar;
323 }else if( pToken->z[0]=='?' ){
324 /* Wildcard of the form "?nnn". Convert "nnn" to an integer and
325 ** use it as the variable number */
326 int i;
drh2646da72005-12-09 20:02:05 +0000327 pExpr->iTable = i = atoi((char*)&pToken->z[1]);
drhfa6bc002004-09-07 16:19:52 +0000328 if( i<1 || i>SQLITE_MAX_VARIABLE_NUMBER ){
329 sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
330 SQLITE_MAX_VARIABLE_NUMBER);
331 }
332 if( i>pParse->nVar ){
333 pParse->nVar = i;
334 }
335 }else{
336 /* Wildcards of the form ":aaa" or "$aaa". Reuse the same variable
337 ** number as the prior appearance of the same name, or if the name
338 ** has never appeared before, reuse the same variable number
339 */
340 int i, n;
341 n = pToken->n;
342 for(i=0; i<pParse->nVarExpr; i++){
343 Expr *pE;
344 if( (pE = pParse->apVarExpr[i])!=0
345 && pE->token.n==n
346 && memcmp(pE->token.z, pToken->z, n)==0 ){
347 pExpr->iTable = pE->iTable;
348 break;
349 }
350 }
351 if( i>=pParse->nVarExpr ){
352 pExpr->iTable = ++pParse->nVar;
353 if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){
354 pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10;
danielk1977e7259292006-01-13 06:33:23 +0000355 sqliteReallocOrFree((void**)&pParse->apVarExpr,
drhfa6bc002004-09-07 16:19:52 +0000356 pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0]) );
357 }
danielk19779e128002006-01-18 16:51:35 +0000358 if( !sqlite3MallocFailed() ){
drhfa6bc002004-09-07 16:19:52 +0000359 assert( pParse->apVarExpr!=0 );
360 pParse->apVarExpr[pParse->nVarExpr++] = pExpr;
361 }
362 }
363 }
364}
365
366/*
drha2e00042002-01-22 03:13:42 +0000367** Recursively delete an expression tree.
368*/
danielk19774adee202004-05-08 08:23:19 +0000369void sqlite3ExprDelete(Expr *p){
drha2e00042002-01-22 03:13:42 +0000370 if( p==0 ) return;
drh4efc4752004-01-16 15:55:37 +0000371 if( p->span.dyn ) sqliteFree((char*)p->span.z);
372 if( p->token.dyn ) sqliteFree((char*)p->token.z);
danielk19774adee202004-05-08 08:23:19 +0000373 sqlite3ExprDelete(p->pLeft);
374 sqlite3ExprDelete(p->pRight);
375 sqlite3ExprListDelete(p->pList);
376 sqlite3SelectDelete(p->pSelect);
drha2e00042002-01-22 03:13:42 +0000377 sqliteFree(p);
378}
379
drhd2687b72005-08-12 22:56:09 +0000380/*
381** The Expr.token field might be a string literal that is quoted.
382** If so, remove the quotation marks.
383*/
384void sqlite3DequoteExpr(Expr *p){
385 if( ExprHasAnyProperty(p, EP_Dequoted) ){
386 return;
387 }
388 ExprSetProperty(p, EP_Dequoted);
389 if( p->token.dyn==0 ){
drhd2687b72005-08-12 22:56:09 +0000390 sqlite3TokenCopy(&p->token, &p->token);
391 }
392 sqlite3Dequote((char*)p->token.z);
393}
394
drha76b5df2002-02-23 02:32:10 +0000395
396/*
drhff78bd22002-02-27 01:47:11 +0000397** The following group of routines make deep copies of expressions,
398** expression lists, ID lists, and select statements. The copies can
399** be deleted (by being passed to their respective ...Delete() routines)
400** without effecting the originals.
401**
danielk19774adee202004-05-08 08:23:19 +0000402** The expression list, ID, and source lists return by sqlite3ExprListDup(),
403** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
drhad3cab52002-05-24 02:04:32 +0000404** by subsequent calls to sqlite*ListAppend() routines.
drhff78bd22002-02-27 01:47:11 +0000405**
drhad3cab52002-05-24 02:04:32 +0000406** Any tables that the SrcList might point to are not duplicated.
drhff78bd22002-02-27 01:47:11 +0000407*/
danielk19774adee202004-05-08 08:23:19 +0000408Expr *sqlite3ExprDup(Expr *p){
drhff78bd22002-02-27 01:47:11 +0000409 Expr *pNew;
410 if( p==0 ) return 0;
drhfcb78a42003-01-18 20:11:05 +0000411 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000412 if( pNew==0 ) return 0;
drh3b167c72002-06-28 12:18:47 +0000413 memcpy(pNew, p, sizeof(*pNew));
drh6977fea2002-10-22 23:38:04 +0000414 if( p->token.z!=0 ){
drh2646da72005-12-09 20:02:05 +0000415 pNew->token.z = (u8*)sqliteStrNDup((char*)p->token.z, p->token.n);
drh4b59ab52002-08-24 18:24:51 +0000416 pNew->token.dyn = 1;
417 }else{
drh4efc4752004-01-16 15:55:37 +0000418 assert( pNew->token.z==0 );
drh4b59ab52002-08-24 18:24:51 +0000419 }
drh6977fea2002-10-22 23:38:04 +0000420 pNew->span.z = 0;
danielk19774adee202004-05-08 08:23:19 +0000421 pNew->pLeft = sqlite3ExprDup(p->pLeft);
422 pNew->pRight = sqlite3ExprDup(p->pRight);
423 pNew->pList = sqlite3ExprListDup(p->pList);
424 pNew->pSelect = sqlite3SelectDup(p->pSelect);
danielk1977aee18ef2005-03-09 12:26:50 +0000425 pNew->pTab = p->pTab;
drhff78bd22002-02-27 01:47:11 +0000426 return pNew;
427}
danielk19774adee202004-05-08 08:23:19 +0000428void sqlite3TokenCopy(Token *pTo, Token *pFrom){
drh4b59ab52002-08-24 18:24:51 +0000429 if( pTo->dyn ) sqliteFree((char*)pTo->z);
drh4b59ab52002-08-24 18:24:51 +0000430 if( pFrom->z ){
431 pTo->n = pFrom->n;
drh2646da72005-12-09 20:02:05 +0000432 pTo->z = (u8*)sqliteStrNDup((char*)pFrom->z, pFrom->n);
drh4b59ab52002-08-24 18:24:51 +0000433 pTo->dyn = 1;
434 }else{
drh4b59ab52002-08-24 18:24:51 +0000435 pTo->z = 0;
drh4b59ab52002-08-24 18:24:51 +0000436 }
437}
danielk19774adee202004-05-08 08:23:19 +0000438ExprList *sqlite3ExprListDup(ExprList *p){
drhff78bd22002-02-27 01:47:11 +0000439 ExprList *pNew;
drh145716b2004-09-24 12:24:06 +0000440 struct ExprList_item *pItem, *pOldItem;
drhff78bd22002-02-27 01:47:11 +0000441 int i;
442 if( p==0 ) return 0;
443 pNew = sqliteMalloc( sizeof(*pNew) );
444 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000445 pNew->nExpr = pNew->nAlloc = p->nExpr;
drh3e7bc9c2004-02-21 19:17:17 +0000446 pNew->a = pItem = sqliteMalloc( p->nExpr*sizeof(p->a[0]) );
danielk1977e0048402004-06-15 16:51:01 +0000447 if( pItem==0 ){
448 sqliteFree(pNew);
449 return 0;
450 }
drh145716b2004-09-24 12:24:06 +0000451 pOldItem = p->a;
452 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
drh4b59ab52002-08-24 18:24:51 +0000453 Expr *pNewExpr, *pOldExpr;
drh145716b2004-09-24 12:24:06 +0000454 pItem->pExpr = pNewExpr = sqlite3ExprDup(pOldExpr = pOldItem->pExpr);
drh6977fea2002-10-22 23:38:04 +0000455 if( pOldExpr->span.z!=0 && pNewExpr ){
456 /* Always make a copy of the span for top-level expressions in the
drh4b59ab52002-08-24 18:24:51 +0000457 ** expression list. The logic in SELECT processing that determines
458 ** the names of columns in the result set needs this information */
danielk19774adee202004-05-08 08:23:19 +0000459 sqlite3TokenCopy(&pNewExpr->span, &pOldExpr->span);
drh4b59ab52002-08-24 18:24:51 +0000460 }
drh1f3e9052002-10-31 00:09:39 +0000461 assert( pNewExpr==0 || pNewExpr->span.z!=0
drh6f7adc82006-01-11 21:41:20 +0000462 || pOldExpr->span.z==0
danielk19779e128002006-01-18 16:51:35 +0000463 || sqlite3MallocFailed() );
drh145716b2004-09-24 12:24:06 +0000464 pItem->zName = sqliteStrDup(pOldItem->zName);
465 pItem->sortOrder = pOldItem->sortOrder;
466 pItem->isAgg = pOldItem->isAgg;
drh3e7bc9c2004-02-21 19:17:17 +0000467 pItem->done = 0;
drhff78bd22002-02-27 01:47:11 +0000468 }
469 return pNew;
470}
danielk197793758c82005-01-21 08:13:14 +0000471
472/*
473** If cursors, triggers, views and subqueries are all omitted from
474** the build, then none of the following routines, except for
475** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes
476** called with a NULL argument.
477*/
danielk19776a67fe82005-02-04 04:07:16 +0000478#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \
479 || !defined(SQLITE_OMIT_SUBQUERY)
danielk19774adee202004-05-08 08:23:19 +0000480SrcList *sqlite3SrcListDup(SrcList *p){
drhad3cab52002-05-24 02:04:32 +0000481 SrcList *pNew;
482 int i;
drh113088e2003-03-20 01:16:58 +0000483 int nByte;
drhad3cab52002-05-24 02:04:32 +0000484 if( p==0 ) return 0;
drh113088e2003-03-20 01:16:58 +0000485 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
drh4efc4752004-01-16 15:55:37 +0000486 pNew = sqliteMallocRaw( nByte );
drhad3cab52002-05-24 02:04:32 +0000487 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000488 pNew->nSrc = pNew->nAlloc = p->nSrc;
drhad3cab52002-05-24 02:04:32 +0000489 for(i=0; i<p->nSrc; i++){
drh4efc4752004-01-16 15:55:37 +0000490 struct SrcList_item *pNewItem = &pNew->a[i];
491 struct SrcList_item *pOldItem = &p->a[i];
drhed8a3bb2005-06-06 21:19:56 +0000492 Table *pTab;
drh4efc4752004-01-16 15:55:37 +0000493 pNewItem->zDatabase = sqliteStrDup(pOldItem->zDatabase);
494 pNewItem->zName = sqliteStrDup(pOldItem->zName);
495 pNewItem->zAlias = sqliteStrDup(pOldItem->zAlias);
496 pNewItem->jointype = pOldItem->jointype;
497 pNewItem->iCursor = pOldItem->iCursor;
drhed8a3bb2005-06-06 21:19:56 +0000498 pTab = pNewItem->pTab = pOldItem->pTab;
499 if( pTab ){
500 pTab->nRef++;
danielk1977a1cb1832005-02-12 08:59:55 +0000501 }
danielk19774adee202004-05-08 08:23:19 +0000502 pNewItem->pSelect = sqlite3SelectDup(pOldItem->pSelect);
503 pNewItem->pOn = sqlite3ExprDup(pOldItem->pOn);
504 pNewItem->pUsing = sqlite3IdListDup(pOldItem->pUsing);
danielk19776c18b6e2005-01-30 09:17:58 +0000505 pNewItem->colUsed = pOldItem->colUsed;
drhad3cab52002-05-24 02:04:32 +0000506 }
507 return pNew;
508}
danielk19774adee202004-05-08 08:23:19 +0000509IdList *sqlite3IdListDup(IdList *p){
drhff78bd22002-02-27 01:47:11 +0000510 IdList *pNew;
511 int i;
512 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000513 pNew = sqliteMallocRaw( sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +0000514 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000515 pNew->nId = pNew->nAlloc = p->nId;
drh4efc4752004-01-16 15:55:37 +0000516 pNew->a = sqliteMallocRaw( p->nId*sizeof(p->a[0]) );
danielk1977d5d56522005-03-16 12:15:20 +0000517 if( pNew->a==0 ){
518 sqliteFree(pNew);
519 return 0;
520 }
drhff78bd22002-02-27 01:47:11 +0000521 for(i=0; i<p->nId; i++){
drh4efc4752004-01-16 15:55:37 +0000522 struct IdList_item *pNewItem = &pNew->a[i];
523 struct IdList_item *pOldItem = &p->a[i];
524 pNewItem->zName = sqliteStrDup(pOldItem->zName);
525 pNewItem->idx = pOldItem->idx;
drhff78bd22002-02-27 01:47:11 +0000526 }
527 return pNew;
528}
danielk19774adee202004-05-08 08:23:19 +0000529Select *sqlite3SelectDup(Select *p){
drhff78bd22002-02-27 01:47:11 +0000530 Select *pNew;
531 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000532 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000533 if( pNew==0 ) return 0;
534 pNew->isDistinct = p->isDistinct;
danielk19774adee202004-05-08 08:23:19 +0000535 pNew->pEList = sqlite3ExprListDup(p->pEList);
536 pNew->pSrc = sqlite3SrcListDup(p->pSrc);
537 pNew->pWhere = sqlite3ExprDup(p->pWhere);
538 pNew->pGroupBy = sqlite3ExprListDup(p->pGroupBy);
539 pNew->pHaving = sqlite3ExprDup(p->pHaving);
540 pNew->pOrderBy = sqlite3ExprListDup(p->pOrderBy);
drhff78bd22002-02-27 01:47:11 +0000541 pNew->op = p->op;
danielk19774adee202004-05-08 08:23:19 +0000542 pNew->pPrior = sqlite3SelectDup(p->pPrior);
danielk1977a2dc3b12005-02-05 12:48:48 +0000543 pNew->pLimit = sqlite3ExprDup(p->pLimit);
544 pNew->pOffset = sqlite3ExprDup(p->pOffset);
drh7b58dae2003-07-20 01:16:46 +0000545 pNew->iLimit = -1;
546 pNew->iOffset = -1;
danielk1977a1cb1832005-02-12 08:59:55 +0000547 pNew->isResolved = p->isResolved;
548 pNew->isAgg = p->isAgg;
drh8e647b82005-09-23 21:11:53 +0000549 pNew->usesVirt = 0;
550 pNew->disallowOrderBy = 0;
drh0342b1f2005-09-01 03:07:44 +0000551 pNew->pRightmost = 0;
552 pNew->addrOpenVirt[0] = -1;
553 pNew->addrOpenVirt[1] = -1;
554 pNew->addrOpenVirt[2] = -1;
drhff78bd22002-02-27 01:47:11 +0000555 return pNew;
556}
danielk197793758c82005-01-21 08:13:14 +0000557#else
558Select *sqlite3SelectDup(Select *p){
559 assert( p==0 );
560 return 0;
561}
562#endif
drhff78bd22002-02-27 01:47:11 +0000563
564
565/*
drha76b5df2002-02-23 02:32:10 +0000566** Add a new element to the end of an expression list. If pList is
567** initially NULL, then create a new expression list.
568*/
danielk19774adee202004-05-08 08:23:19 +0000569ExprList *sqlite3ExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){
drha76b5df2002-02-23 02:32:10 +0000570 if( pList==0 ){
571 pList = sqliteMalloc( sizeof(ExprList) );
572 if( pList==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000573 goto no_mem;
drha76b5df2002-02-23 02:32:10 +0000574 }
drh4efc4752004-01-16 15:55:37 +0000575 assert( pList->nAlloc==0 );
drha76b5df2002-02-23 02:32:10 +0000576 }
drh4305d102003-07-30 12:34:12 +0000577 if( pList->nAlloc<=pList->nExpr ){
danielk1977d5d56522005-03-16 12:15:20 +0000578 struct ExprList_item *a;
579 int n = pList->nAlloc*2 + 4;
580 a = sqliteRealloc(pList->a, n*sizeof(pList->a[0]));
581 if( a==0 ){
582 goto no_mem;
drha76b5df2002-02-23 02:32:10 +0000583 }
danielk1977d5d56522005-03-16 12:15:20 +0000584 pList->a = a;
585 pList->nAlloc = n;
drha76b5df2002-02-23 02:32:10 +0000586 }
drh4efc4752004-01-16 15:55:37 +0000587 assert( pList->a!=0 );
588 if( pExpr || pName ){
589 struct ExprList_item *pItem = &pList->a[pList->nExpr++];
590 memset(pItem, 0, sizeof(*pItem));
drha99db3b2004-06-19 14:49:12 +0000591 pItem->zName = sqlite3NameFromToken(pName);
danielk1977e94ddc92005-03-21 03:53:38 +0000592 pItem->pExpr = pExpr;
drha76b5df2002-02-23 02:32:10 +0000593 }
594 return pList;
danielk1977d5d56522005-03-16 12:15:20 +0000595
596no_mem:
597 /* Avoid leaking memory if malloc has failed. */
598 sqlite3ExprDelete(pExpr);
599 sqlite3ExprListDelete(pList);
600 return 0;
drha76b5df2002-02-23 02:32:10 +0000601}
602
603/*
604** Delete an entire expression list.
605*/
danielk19774adee202004-05-08 08:23:19 +0000606void sqlite3ExprListDelete(ExprList *pList){
drha76b5df2002-02-23 02:32:10 +0000607 int i;
drhbe5c89a2004-07-26 00:31:09 +0000608 struct ExprList_item *pItem;
drha76b5df2002-02-23 02:32:10 +0000609 if( pList==0 ) return;
drh1bdd9b52004-04-23 17:04:44 +0000610 assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
611 assert( pList->nExpr<=pList->nAlloc );
drhbe5c89a2004-07-26 00:31:09 +0000612 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
613 sqlite3ExprDelete(pItem->pExpr);
614 sqliteFree(pItem->zName);
drha76b5df2002-02-23 02:32:10 +0000615 }
616 sqliteFree(pList->a);
617 sqliteFree(pList);
618}
619
620/*
drh626a8792005-01-17 22:08:19 +0000621** Walk an expression tree. Call xFunc for each node visited.
drh73b211a2005-01-18 04:00:42 +0000622**
drh626a8792005-01-17 22:08:19 +0000623** The return value from xFunc determines whether the tree walk continues.
624** 0 means continue walking the tree. 1 means do not walk children
625** of the current node but continue with siblings. 2 means abandon
626** the tree walk completely.
627**
628** The return value from this routine is 1 to abandon the tree walk
629** and 0 to continue.
drh87abf5c2005-08-25 12:45:04 +0000630**
631** NOTICE: This routine does *not* descend into subqueries.
drh626a8792005-01-17 22:08:19 +0000632*/
danielk1977a58fdfb2005-02-08 07:50:40 +0000633static int walkExprList(ExprList *, int (*)(void *, Expr*), void *);
drh626a8792005-01-17 22:08:19 +0000634static int walkExprTree(Expr *pExpr, int (*xFunc)(void*,Expr*), void *pArg){
drh626a8792005-01-17 22:08:19 +0000635 int rc;
636 if( pExpr==0 ) return 0;
637 rc = (*xFunc)(pArg, pExpr);
638 if( rc==0 ){
639 if( walkExprTree(pExpr->pLeft, xFunc, pArg) ) return 1;
640 if( walkExprTree(pExpr->pRight, xFunc, pArg) ) return 1;
danielk1977a58fdfb2005-02-08 07:50:40 +0000641 if( walkExprList(pExpr->pList, xFunc, pArg) ) return 1;
drh626a8792005-01-17 22:08:19 +0000642 }
643 return rc>1;
644}
645
646/*
danielk1977a58fdfb2005-02-08 07:50:40 +0000647** Call walkExprTree() for every expression in list p.
648*/
649static int walkExprList(ExprList *p, int (*xFunc)(void *, Expr*), void *pArg){
650 int i;
651 struct ExprList_item *pItem;
652 if( !p ) return 0;
653 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
654 if( walkExprTree(pItem->pExpr, xFunc, pArg) ) return 1;
655 }
656 return 0;
657}
658
659/*
660** Call walkExprTree() for every expression in Select p, not including
661** expressions that are part of sub-selects in any FROM clause or the LIMIT
662** or OFFSET expressions..
663*/
664static int walkSelectExpr(Select *p, int (*xFunc)(void *, Expr*), void *pArg){
665 walkExprList(p->pEList, xFunc, pArg);
666 walkExprTree(p->pWhere, xFunc, pArg);
667 walkExprList(p->pGroupBy, xFunc, pArg);
668 walkExprTree(p->pHaving, xFunc, pArg);
669 walkExprList(p->pOrderBy, xFunc, pArg);
670 return 0;
671}
672
673
674/*
drh626a8792005-01-17 22:08:19 +0000675** This routine is designed as an xFunc for walkExprTree().
676**
677** pArg is really a pointer to an integer. If we can tell by looking
drh73b211a2005-01-18 04:00:42 +0000678** at pExpr that the expression that contains pExpr is not a constant
679** expression, then set *pArg to 0 and return 2 to abandon the tree walk.
680** If pExpr does does not disqualify the expression from being a constant
681** then do nothing.
682**
683** After walking the whole tree, if no nodes are found that disqualify
684** the expression as constant, then we assume the whole expression
685** is constant. See sqlite3ExprIsConstant() for additional information.
drh626a8792005-01-17 22:08:19 +0000686*/
687static int exprNodeIsConstant(void *pArg, Expr *pExpr){
688 switch( pExpr->op ){
drheb55bd22005-06-30 17:04:21 +0000689 /* Consider functions to be constant if all their arguments are constant
690 ** and *pArg==2 */
691 case TK_FUNCTION:
692 if( *((int*)pArg)==2 ) return 0;
693 /* Fall through */
drh626a8792005-01-17 22:08:19 +0000694 case TK_ID:
695 case TK_COLUMN:
696 case TK_DOT:
697 case TK_AGG_FUNCTION:
drh13449892005-09-07 21:22:45 +0000698 case TK_AGG_COLUMN:
drhfe2093d2005-01-20 22:48:47 +0000699#ifndef SQLITE_OMIT_SUBQUERY
700 case TK_SELECT:
701 case TK_EXISTS:
702#endif
drh626a8792005-01-17 22:08:19 +0000703 *((int*)pArg) = 0;
704 return 2;
drh87abf5c2005-08-25 12:45:04 +0000705 case TK_IN:
706 if( pExpr->pSelect ){
707 *((int*)pArg) = 0;
708 return 2;
709 }
drh626a8792005-01-17 22:08:19 +0000710 default:
711 return 0;
712 }
713}
714
715/*
drhfef52082000-06-06 01:50:43 +0000716** Walk an expression tree. Return 1 if the expression is constant
drheb55bd22005-06-30 17:04:21 +0000717** and 0 if it involves variables or function calls.
drh23989372002-05-21 13:43:04 +0000718**
719** For the purposes of this function, a double-quoted string (ex: "abc")
720** is considered a variable but a single-quoted string (ex: 'abc') is
721** a constant.
drhfef52082000-06-06 01:50:43 +0000722*/
danielk19774adee202004-05-08 08:23:19 +0000723int sqlite3ExprIsConstant(Expr *p){
drh626a8792005-01-17 22:08:19 +0000724 int isConst = 1;
725 walkExprTree(p, exprNodeIsConstant, &isConst);
726 return isConst;
drhfef52082000-06-06 01:50:43 +0000727}
728
729/*
drheb55bd22005-06-30 17:04:21 +0000730** Walk an expression tree. Return 1 if the expression is constant
731** or a function call with constant arguments. Return and 0 if there
732** are any variables.
733**
734** For the purposes of this function, a double-quoted string (ex: "abc")
735** is considered a variable but a single-quoted string (ex: 'abc') is
736** a constant.
737*/
738int sqlite3ExprIsConstantOrFunction(Expr *p){
739 int isConst = 2;
740 walkExprTree(p, exprNodeIsConstant, &isConst);
741 return isConst!=0;
742}
743
744/*
drh73b211a2005-01-18 04:00:42 +0000745** If the expression p codes a constant integer that is small enough
drh202b2df2004-01-06 01:13:46 +0000746** to fit in a 32-bit integer, return 1 and put the value of the integer
747** in *pValue. If the expression is not an integer or if it is too big
748** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
drhe4de1fe2002-06-02 16:09:01 +0000749*/
danielk19774adee202004-05-08 08:23:19 +0000750int sqlite3ExprIsInteger(Expr *p, int *pValue){
drhe4de1fe2002-06-02 16:09:01 +0000751 switch( p->op ){
752 case TK_INTEGER: {
drh2646da72005-12-09 20:02:05 +0000753 if( sqlite3GetInt32((char*)p->token.z, pValue) ){
drh202b2df2004-01-06 01:13:46 +0000754 return 1;
755 }
756 break;
drhe4de1fe2002-06-02 16:09:01 +0000757 }
drh4b59ab52002-08-24 18:24:51 +0000758 case TK_UPLUS: {
danielk19774adee202004-05-08 08:23:19 +0000759 return sqlite3ExprIsInteger(p->pLeft, pValue);
drh4b59ab52002-08-24 18:24:51 +0000760 }
drhe4de1fe2002-06-02 16:09:01 +0000761 case TK_UMINUS: {
762 int v;
danielk19774adee202004-05-08 08:23:19 +0000763 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
drhe4de1fe2002-06-02 16:09:01 +0000764 *pValue = -v;
765 return 1;
766 }
767 break;
768 }
769 default: break;
770 }
771 return 0;
772}
773
774/*
drhc4a3c772001-04-04 11:48:57 +0000775** Return TRUE if the given string is a row-id column name.
776*/
danielk19774adee202004-05-08 08:23:19 +0000777int sqlite3IsRowid(const char *z){
778 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
779 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
780 if( sqlite3StrICmp(z, "OID")==0 ) return 1;
drhc4a3c772001-04-04 11:48:57 +0000781 return 0;
782}
783
784/*
drh8141f612004-01-25 22:44:58 +0000785** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
786** that name in the set of source tables in pSrcList and make the pExpr
787** expression node refer back to that source column. The following changes
788** are made to pExpr:
789**
790** pExpr->iDb Set the index in db->aDb[] of the database holding
791** the table.
792** pExpr->iTable Set to the cursor number for the table obtained
793** from pSrcList.
794** pExpr->iColumn Set to the column number within the table.
drh8141f612004-01-25 22:44:58 +0000795** pExpr->op Set to TK_COLUMN.
796** pExpr->pLeft Any expression this points to is deleted
797** pExpr->pRight Any expression this points to is deleted.
798**
799** The pDbToken is the name of the database (the "X"). This value may be
800** NULL meaning that name is of the form Y.Z or Z. Any available database
801** can be used. The pTableToken is the name of the table (the "Y"). This
802** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it
803** means that the form of the name is Z and that columns from any table
804** can be used.
805**
806** If the name cannot be resolved unambiguously, leave an error message
807** in pParse and return non-zero. Return zero on success.
808*/
809static int lookupName(
drhffe07b22005-11-03 00:41:17 +0000810 Parse *pParse, /* The parsing context */
drh8141f612004-01-25 22:44:58 +0000811 Token *pDbToken, /* Name of the database containing table, or NULL */
812 Token *pTableToken, /* Name of table containing column, or NULL */
813 Token *pColumnToken, /* Name of the column. */
drh626a8792005-01-17 22:08:19 +0000814 NameContext *pNC, /* The name context used to resolve the name */
drh8141f612004-01-25 22:44:58 +0000815 Expr *pExpr /* Make this EXPR node point to the selected column */
816){
817 char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */
818 char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */
819 char *zCol = 0; /* Name of the column. The "Z" */
820 int i, j; /* Loop counters */
821 int cnt = 0; /* Number of matching column names */
822 int cntTab = 0; /* Number of matching table names */
drh626a8792005-01-17 22:08:19 +0000823 sqlite3 *db = pParse->db; /* The database */
drh51669862004-12-18 18:40:26 +0000824 struct SrcList_item *pItem; /* Use for looping over pSrcList items */
825 struct SrcList_item *pMatch = 0; /* The matching pSrcList item */
drh73b211a2005-01-18 04:00:42 +0000826 NameContext *pTopNC = pNC; /* First namecontext in the list */
drh8141f612004-01-25 22:44:58 +0000827
828 assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
drha99db3b2004-06-19 14:49:12 +0000829 zDb = sqlite3NameFromToken(pDbToken);
830 zTab = sqlite3NameFromToken(pTableToken);
831 zCol = sqlite3NameFromToken(pColumnToken);
danielk19779e128002006-01-18 16:51:35 +0000832 if( sqlite3MallocFailed() ){
danielk1977d5d56522005-03-16 12:15:20 +0000833 goto lookupname_end;
drh8141f612004-01-25 22:44:58 +0000834 }
drh8141f612004-01-25 22:44:58 +0000835
836 pExpr->iTable = -1;
drh626a8792005-01-17 22:08:19 +0000837 while( pNC && cnt==0 ){
drhffe07b22005-11-03 00:41:17 +0000838 ExprList *pEList;
drh626a8792005-01-17 22:08:19 +0000839 SrcList *pSrcList = pNC->pSrcList;
drh8141f612004-01-25 22:44:58 +0000840
danielk1977b3bce662005-01-29 08:32:43 +0000841 if( pSrcList ){
842 for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
843 Table *pTab = pItem->pTab;
danielk1977da184232006-01-05 11:34:32 +0000844 int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
danielk1977b3bce662005-01-29 08:32:43 +0000845 Column *pCol;
846
847 if( pTab==0 ) continue;
848 assert( pTab->nCol>0 );
849 if( zTab ){
850 if( pItem->zAlias ){
851 char *zTabName = pItem->zAlias;
852 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
853 }else{
854 char *zTabName = pTab->zName;
855 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
danielk1977da184232006-01-05 11:34:32 +0000856 if( zDb!=0 && sqlite3StrICmp(db->aDb[iDb].zName, zDb)!=0 ){
danielk1977b3bce662005-01-29 08:32:43 +0000857 continue;
858 }
drh626a8792005-01-17 22:08:19 +0000859 }
drh8141f612004-01-25 22:44:58 +0000860 }
danielk1977b3bce662005-01-29 08:32:43 +0000861 if( 0==(cntTab++) ){
drh626a8792005-01-17 22:08:19 +0000862 pExpr->iTable = pItem->iCursor;
danielk1977da184232006-01-05 11:34:32 +0000863 pExpr->pSchema = pTab->pSchema;
danielk1977b3bce662005-01-29 08:32:43 +0000864 pMatch = pItem;
865 }
866 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
867 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
danielk1977b3bf5562006-01-10 17:58:23 +0000868 const char *zColl = pTab->aCol[j].zColl;
drh873fac02005-06-06 17:11:46 +0000869 IdList *pUsing;
danielk1977b3bce662005-01-29 08:32:43 +0000870 cnt++;
871 pExpr->iTable = pItem->iCursor;
872 pMatch = pItem;
danielk1977da184232006-01-05 11:34:32 +0000873 pExpr->pSchema = pTab->pSchema;
danielk1977b3bce662005-01-29 08:32:43 +0000874 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
875 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
876 pExpr->affinity = pTab->aCol[j].affinity;
danielk1977b3bf5562006-01-10 17:58:23 +0000877 pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0);
drh355ef362005-06-06 16:59:24 +0000878 if( pItem->jointype & JT_NATURAL ){
879 /* If this match occurred in the left table of a natural join,
880 ** then skip the right table to avoid a duplicate match */
881 pItem++;
882 i++;
883 }
drh873fac02005-06-06 17:11:46 +0000884 if( (pUsing = pItem->pUsing)!=0 ){
885 /* If this match occurs on a column that is in the USING clause
886 ** of a join, skip the search of the right table of the join
887 ** to avoid a duplicate match there. */
888 int k;
889 for(k=0; k<pUsing->nId; k++){
890 if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ){
891 pItem++;
892 i++;
893 break;
894 }
895 }
896 }
danielk1977b3bce662005-01-29 08:32:43 +0000897 break;
898 }
drh8141f612004-01-25 22:44:58 +0000899 }
900 }
901 }
drh626a8792005-01-17 22:08:19 +0000902
903#ifndef SQLITE_OMIT_TRIGGER
904 /* If we have not already resolved the name, then maybe
905 ** it is a new.* or old.* trigger argument reference
906 */
907 if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
908 TriggerStack *pTriggerStack = pParse->trigStack;
909 Table *pTab = 0;
910 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){
911 pExpr->iTable = pTriggerStack->newIdx;
912 assert( pTriggerStack->pTab );
913 pTab = pTriggerStack->pTab;
914 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab)==0 ){
915 pExpr->iTable = pTriggerStack->oldIdx;
916 assert( pTriggerStack->pTab );
917 pTab = pTriggerStack->pTab;
918 }
919
920 if( pTab ){
921 int j;
922 Column *pCol = pTab->aCol;
923
danielk1977da184232006-01-05 11:34:32 +0000924 pExpr->pSchema = pTab->pSchema;
drh626a8792005-01-17 22:08:19 +0000925 cntTab++;
926 for(j=0; j < pTab->nCol; j++, pCol++) {
927 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
danielk1977b3bf5562006-01-10 17:58:23 +0000928 const char *zColl = pTab->aCol[j].zColl;
drh626a8792005-01-17 22:08:19 +0000929 cnt++;
930 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
931 pExpr->affinity = pTab->aCol[j].affinity;
danielk1977b3bf5562006-01-10 17:58:23 +0000932 pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0);
danielk1977aee18ef2005-03-09 12:26:50 +0000933 pExpr->pTab = pTab;
drh626a8792005-01-17 22:08:19 +0000934 break;
935 }
936 }
937 }
938 }
drhb7f91642004-10-31 02:22:47 +0000939#endif /* !defined(SQLITE_OMIT_TRIGGER) */
drh8141f612004-01-25 22:44:58 +0000940
drh626a8792005-01-17 22:08:19 +0000941 /*
942 ** Perhaps the name is a reference to the ROWID
943 */
944 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
945 cnt = 1;
946 pExpr->iColumn = -1;
drh8a512562005-11-14 22:29:05 +0000947 pExpr->affinity = SQLITE_AFF_INTEGER;
drh626a8792005-01-17 22:08:19 +0000948 }
drh8141f612004-01-25 22:44:58 +0000949
drh626a8792005-01-17 22:08:19 +0000950 /*
951 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
952 ** might refer to an result-set alias. This happens, for example, when
953 ** we are resolving names in the WHERE clause of the following command:
954 **
955 ** SELECT a+b AS x FROM table WHERE x<10;
956 **
957 ** In cases like this, replace pExpr with a copy of the expression that
958 ** forms the result set entry ("a+b" in the example) and return immediately.
959 ** Note that the expression in the result set should have already been
960 ** resolved by the time the WHERE clause is resolved.
961 */
drhffe07b22005-11-03 00:41:17 +0000962 if( cnt==0 && (pEList = pNC->pEList)!=0 && zTab==0 ){
drh626a8792005-01-17 22:08:19 +0000963 for(j=0; j<pEList->nExpr; j++){
964 char *zAs = pEList->a[j].zName;
965 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
966 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
967 pExpr->op = TK_AS;
968 pExpr->iColumn = j;
969 pExpr->pLeft = sqlite3ExprDup(pEList->a[j].pExpr);
drh15ccce12005-05-23 15:06:39 +0000970 cnt = 1;
drh626a8792005-01-17 22:08:19 +0000971 assert( zTab==0 && zDb==0 );
drh15ccce12005-05-23 15:06:39 +0000972 goto lookupname_end_2;
drh626a8792005-01-17 22:08:19 +0000973 }
974 }
975 }
976
977 /* Advance to the next name context. The loop will exit when either
978 ** we have a match (cnt>0) or when we run out of name contexts.
979 */
980 if( cnt==0 ){
981 pNC = pNC->pNext;
982 }
drh8141f612004-01-25 22:44:58 +0000983 }
984
985 /*
986 ** If X and Y are NULL (in other words if only the column name Z is
987 ** supplied) and the value of Z is enclosed in double-quotes, then
988 ** Z is a string literal if it doesn't match any column names. In that
989 ** case, we need to return right away and not make any changes to
990 ** pExpr.
drh15ccce12005-05-23 15:06:39 +0000991 **
992 ** Because no reference was made to outer contexts, the pNC->nRef
993 ** fields are not changed in any context.
drh8141f612004-01-25 22:44:58 +0000994 */
995 if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
996 sqliteFree(zCol);
997 return 0;
998 }
999
1000 /*
1001 ** cnt==0 means there was not match. cnt>1 means there were two or
1002 ** more matches. Either way, we have an error.
1003 */
1004 if( cnt!=1 ){
1005 char *z = 0;
1006 char *zErr;
1007 zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s";
1008 if( zDb ){
drhf93339d2006-01-03 15:16:26 +00001009 sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, (char*)0);
drh8141f612004-01-25 22:44:58 +00001010 }else if( zTab ){
drhf93339d2006-01-03 15:16:26 +00001011 sqlite3SetString(&z, zTab, ".", zCol, (char*)0);
drh8141f612004-01-25 22:44:58 +00001012 }else{
1013 z = sqliteStrDup(zCol);
1014 }
danielk19774adee202004-05-08 08:23:19 +00001015 sqlite3ErrorMsg(pParse, zErr, z);
drh8141f612004-01-25 22:44:58 +00001016 sqliteFree(z);
drh73b211a2005-01-18 04:00:42 +00001017 pTopNC->nErr++;
drh8141f612004-01-25 22:44:58 +00001018 }
1019
drh51669862004-12-18 18:40:26 +00001020 /* If a column from a table in pSrcList is referenced, then record
1021 ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes
1022 ** bit 0 to be set. Column 1 sets bit 1. And so forth. If the
1023 ** column number is greater than the number of bits in the bitmask
1024 ** then set the high-order bit of the bitmask.
1025 */
1026 if( pExpr->iColumn>=0 && pMatch!=0 ){
1027 int n = pExpr->iColumn;
1028 if( n>=sizeof(Bitmask)*8 ){
1029 n = sizeof(Bitmask)*8-1;
1030 }
1031 assert( pMatch->iCursor==pExpr->iTable );
1032 pMatch->colUsed |= 1<<n;
1033 }
1034
danielk1977d5d56522005-03-16 12:15:20 +00001035lookupname_end:
drh8141f612004-01-25 22:44:58 +00001036 /* Clean up and return
1037 */
1038 sqliteFree(zDb);
1039 sqliteFree(zTab);
danielk19774adee202004-05-08 08:23:19 +00001040 sqlite3ExprDelete(pExpr->pLeft);
drh8141f612004-01-25 22:44:58 +00001041 pExpr->pLeft = 0;
danielk19774adee202004-05-08 08:23:19 +00001042 sqlite3ExprDelete(pExpr->pRight);
drh8141f612004-01-25 22:44:58 +00001043 pExpr->pRight = 0;
1044 pExpr->op = TK_COLUMN;
drh15ccce12005-05-23 15:06:39 +00001045lookupname_end_2:
1046 sqliteFree(zCol);
drh626a8792005-01-17 22:08:19 +00001047 if( cnt==1 ){
danielk1977b3bce662005-01-29 08:32:43 +00001048 assert( pNC!=0 );
drh626a8792005-01-17 22:08:19 +00001049 sqlite3AuthRead(pParse, pExpr, pNC->pSrcList);
danielk1977aee18ef2005-03-09 12:26:50 +00001050 if( pMatch && !pMatch->pSelect ){
1051 pExpr->pTab = pMatch->pTab;
1052 }
drh15ccce12005-05-23 15:06:39 +00001053 /* Increment the nRef value on all name contexts from TopNC up to
1054 ** the point where the name matched. */
1055 for(;;){
1056 assert( pTopNC!=0 );
1057 pTopNC->nRef++;
1058 if( pTopNC==pNC ) break;
1059 pTopNC = pTopNC->pNext;
1060 }
1061 return 0;
1062 } else {
1063 return 1;
drh626a8792005-01-17 22:08:19 +00001064 }
drh8141f612004-01-25 22:44:58 +00001065}
1066
1067/*
drh626a8792005-01-17 22:08:19 +00001068** This routine is designed as an xFunc for walkExprTree().
1069**
drh73b211a2005-01-18 04:00:42 +00001070** Resolve symbolic names into TK_COLUMN operators for the current
drh626a8792005-01-17 22:08:19 +00001071** node in the expression tree. Return 0 to continue the search down
drh73b211a2005-01-18 04:00:42 +00001072** the tree or 2 to abort the tree walk.
1073**
1074** This routine also does error checking and name resolution for
1075** function names. The operator for aggregate functions is changed
1076** to TK_AGG_FUNCTION.
drh626a8792005-01-17 22:08:19 +00001077*/
1078static int nameResolverStep(void *pArg, Expr *pExpr){
1079 NameContext *pNC = (NameContext*)pArg;
1080 SrcList *pSrcList;
1081 Parse *pParse;
drh626a8792005-01-17 22:08:19 +00001082
danielk1977b3bce662005-01-29 08:32:43 +00001083 if( pExpr==0 ) return 1;
drh626a8792005-01-17 22:08:19 +00001084 assert( pNC!=0 );
1085 pSrcList = pNC->pSrcList;
1086 pParse = pNC->pParse;
danielk1977b3bce662005-01-29 08:32:43 +00001087
drh626a8792005-01-17 22:08:19 +00001088 if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return 1;
1089 ExprSetProperty(pExpr, EP_Resolved);
1090#ifndef NDEBUG
drhffe07b22005-11-03 00:41:17 +00001091 if( pSrcList && pSrcList->nAlloc>0 ){
danielk1977940fac92005-01-23 22:41:37 +00001092 int i;
drh626a8792005-01-17 22:08:19 +00001093 for(i=0; i<pSrcList->nSrc; i++){
1094 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
1095 }
1096 }
1097#endif
1098 switch( pExpr->op ){
1099 /* Double-quoted strings (ex: "abc") are used as identifiers if
1100 ** possible. Otherwise they remain as strings. Single-quoted
1101 ** strings (ex: 'abc') are always string literals.
1102 */
1103 case TK_STRING: {
1104 if( pExpr->token.z[0]=='\'' ) break;
1105 /* Fall thru into the TK_ID case if this is a double-quoted string */
1106 }
1107 /* A lone identifier is the name of a column.
1108 */
1109 case TK_ID: {
drh626a8792005-01-17 22:08:19 +00001110 lookupName(pParse, 0, 0, &pExpr->token, pNC, pExpr);
1111 return 1;
1112 }
1113
1114 /* A table name and column name: ID.ID
1115 ** Or a database, table and column: ID.ID.ID
1116 */
1117 case TK_DOT: {
1118 Token *pColumn;
1119 Token *pTable;
1120 Token *pDb;
1121 Expr *pRight;
1122
danielk1977b3bce662005-01-29 08:32:43 +00001123 /* if( pSrcList==0 ) break; */
drh626a8792005-01-17 22:08:19 +00001124 pRight = pExpr->pRight;
1125 if( pRight->op==TK_ID ){
1126 pDb = 0;
1127 pTable = &pExpr->pLeft->token;
1128 pColumn = &pRight->token;
1129 }else{
1130 assert( pRight->op==TK_DOT );
1131 pDb = &pExpr->pLeft->token;
1132 pTable = &pRight->pLeft->token;
1133 pColumn = &pRight->pRight->token;
1134 }
1135 lookupName(pParse, pDb, pTable, pColumn, pNC, pExpr);
1136 return 1;
1137 }
1138
1139 /* Resolve function names
1140 */
drhb71090f2005-05-23 17:26:51 +00001141 case TK_CONST_FUNC:
drh626a8792005-01-17 22:08:19 +00001142 case TK_FUNCTION: {
1143 ExprList *pList = pExpr->pList; /* The argument list */
1144 int n = pList ? pList->nExpr : 0; /* Number of arguments */
1145 int no_such_func = 0; /* True if no such function exists */
1146 int wrong_num_args = 0; /* True if wrong number of arguments */
1147 int is_agg = 0; /* True if is an aggregate function */
1148 int i;
1149 int nId; /* Number of characters in function name */
1150 const char *zId; /* The function name. */
drh73b211a2005-01-18 04:00:42 +00001151 FuncDef *pDef; /* Information about the function */
danielk197714db2662006-01-09 16:12:04 +00001152 int enc = ENC(pParse->db); /* The database encoding */
drh626a8792005-01-17 22:08:19 +00001153
drh2646da72005-12-09 20:02:05 +00001154 zId = (char*)pExpr->token.z;
drhb71090f2005-05-23 17:26:51 +00001155 nId = pExpr->token.n;
drh626a8792005-01-17 22:08:19 +00001156 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
1157 if( pDef==0 ){
1158 pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
1159 if( pDef==0 ){
1160 no_such_func = 1;
1161 }else{
1162 wrong_num_args = 1;
1163 }
1164 }else{
1165 is_agg = pDef->xFunc==0;
1166 }
1167 if( is_agg && !pNC->allowAgg ){
1168 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
1169 pNC->nErr++;
1170 is_agg = 0;
1171 }else if( no_such_func ){
1172 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
1173 pNC->nErr++;
1174 }else if( wrong_num_args ){
1175 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
1176 nId, zId);
1177 pNC->nErr++;
1178 }
1179 if( is_agg ){
1180 pExpr->op = TK_AGG_FUNCTION;
1181 pNC->hasAgg = 1;
1182 }
drh73b211a2005-01-18 04:00:42 +00001183 if( is_agg ) pNC->allowAgg = 0;
drh626a8792005-01-17 22:08:19 +00001184 for(i=0; pNC->nErr==0 && i<n; i++){
drh73b211a2005-01-18 04:00:42 +00001185 walkExprTree(pList->a[i].pExpr, nameResolverStep, pNC);
drh626a8792005-01-17 22:08:19 +00001186 }
drh73b211a2005-01-18 04:00:42 +00001187 if( is_agg ) pNC->allowAgg = 1;
drh626a8792005-01-17 22:08:19 +00001188 /* FIX ME: Compute pExpr->affinity based on the expected return
1189 ** type of the function
1190 */
1191 return is_agg;
1192 }
danielk1977b3bce662005-01-29 08:32:43 +00001193#ifndef SQLITE_OMIT_SUBQUERY
1194 case TK_SELECT:
1195 case TK_EXISTS:
1196#endif
1197 case TK_IN: {
1198 if( pExpr->pSelect ){
drh8a9f38f2005-11-05 15:07:55 +00001199 int nRef = pNC->nRef;
drh06f65412005-11-03 02:03:13 +00001200#ifndef SQLITE_OMIT_CHECK
1201 if( pNC->isCheck ){
1202 sqlite3ErrorMsg(pParse,"subqueries prohibited in CHECK constraints");
1203 }
1204#endif
danielk1977b3bce662005-01-29 08:32:43 +00001205 sqlite3SelectResolve(pParse, pExpr->pSelect, pNC);
1206 assert( pNC->nRef>=nRef );
1207 if( nRef!=pNC->nRef ){
1208 ExprSetProperty(pExpr, EP_VarSelect);
1209 }
1210 }
drh4284fb02005-11-03 12:33:28 +00001211 break;
danielk1977b3bce662005-01-29 08:32:43 +00001212 }
drh4284fb02005-11-03 12:33:28 +00001213#ifndef SQLITE_OMIT_CHECK
1214 case TK_VARIABLE: {
1215 if( pNC->isCheck ){
1216 sqlite3ErrorMsg(pParse,"parameters prohibited in CHECK constraints");
1217 }
1218 break;
1219 }
1220#endif
drh626a8792005-01-17 22:08:19 +00001221 }
1222 return 0;
1223}
1224
1225/*
drhcce7d172000-05-31 15:34:51 +00001226** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +00001227** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +00001228** index to the table in the table list and a column offset. The
1229** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
1230** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +00001231** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +00001232** VDBE cursor number for a cursor that is pointing into the referenced
1233** table. The Expr.iColumn value is changed to the index of the column
1234** of the referenced table. The Expr.iColumn value for the special
1235** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
1236** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +00001237**
drh626a8792005-01-17 22:08:19 +00001238** Also resolve function names and check the functions for proper
1239** usage. Make sure all function names are recognized and all functions
1240** have the correct number of arguments. Leave an error message
1241** in pParse->zErrMsg if anything is amiss. Return the number of errors.
1242**
drh73b211a2005-01-18 04:00:42 +00001243** If the expression contains aggregate functions then set the EP_Agg
1244** property on the expression.
drh626a8792005-01-17 22:08:19 +00001245*/
1246int sqlite3ExprResolveNames(
danielk1977b3bce662005-01-29 08:32:43 +00001247 NameContext *pNC, /* Namespace to resolve expressions in. */
1248 Expr *pExpr /* The expression to be analyzed. */
drh626a8792005-01-17 22:08:19 +00001249){
drh13449892005-09-07 21:22:45 +00001250 int savedHasAgg;
drh73b211a2005-01-18 04:00:42 +00001251 if( pExpr==0 ) return 0;
drh13449892005-09-07 21:22:45 +00001252 savedHasAgg = pNC->hasAgg;
1253 pNC->hasAgg = 0;
danielk1977b3bce662005-01-29 08:32:43 +00001254 walkExprTree(pExpr, nameResolverStep, pNC);
1255 if( pNC->nErr>0 ){
drh73b211a2005-01-18 04:00:42 +00001256 ExprSetProperty(pExpr, EP_Error);
drh73b211a2005-01-18 04:00:42 +00001257 }
drh13449892005-09-07 21:22:45 +00001258 if( pNC->hasAgg ){
1259 ExprSetProperty(pExpr, EP_Agg);
1260 }else if( savedHasAgg ){
1261 pNC->hasAgg = 1;
1262 }
drh73b211a2005-01-18 04:00:42 +00001263 return ExprHasProperty(pExpr, EP_Error);
drh626a8792005-01-17 22:08:19 +00001264}
1265
drh1398ad32005-01-19 23:24:50 +00001266/*
1267** A pointer instance of this structure is used to pass information
1268** through walkExprTree into codeSubqueryStep().
1269*/
1270typedef struct QueryCoder QueryCoder;
1271struct QueryCoder {
1272 Parse *pParse; /* The parsing context */
1273 NameContext *pNC; /* Namespace of first enclosing query */
1274};
1275
drh626a8792005-01-17 22:08:19 +00001276
1277/*
drh9cbe6352005-11-29 03:13:21 +00001278** Generate code for scalar subqueries used as an expression
1279** and IN operators. Examples:
drh626a8792005-01-17 22:08:19 +00001280**
drh9cbe6352005-11-29 03:13:21 +00001281** (SELECT a FROM b) -- subquery
1282** EXISTS (SELECT a FROM b) -- EXISTS subquery
1283** x IN (4,5,11) -- IN operator with list on right-hand side
1284** x IN (SELECT a FROM b) -- IN operator with subquery on the right
drhfef52082000-06-06 01:50:43 +00001285**
drh9cbe6352005-11-29 03:13:21 +00001286** The pExpr parameter describes the expression that contains the IN
1287** operator or subquery.
drhcce7d172000-05-31 15:34:51 +00001288*/
drh51522cd2005-01-20 13:36:19 +00001289#ifndef SQLITE_OMIT_SUBQUERY
danielk1977b3bce662005-01-29 08:32:43 +00001290void sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){
drh57dbd7b2005-07-08 18:25:26 +00001291 int testAddr = 0; /* One-time test address */
danielk1977b3bce662005-01-29 08:32:43 +00001292 Vdbe *v = sqlite3GetVdbe(pParse);
1293 if( v==0 ) return;
1294
drh57dbd7b2005-07-08 18:25:26 +00001295 /* This code must be run in its entirety every time it is encountered
1296 ** if any of the following is true:
1297 **
1298 ** * The right-hand side is a correlated subquery
1299 ** * The right-hand side is an expression list containing variables
1300 ** * We are inside a trigger
1301 **
1302 ** If all of the above are false, then we can run this code just once
1303 ** save the results, and reuse the same result on subsequent invocations.
danielk1977b3bce662005-01-29 08:32:43 +00001304 */
1305 if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->trigStack ){
1306 int mem = pParse->nMem++;
1307 sqlite3VdbeAddOp(v, OP_MemLoad, mem, 0);
drh57dbd7b2005-07-08 18:25:26 +00001308 testAddr = sqlite3VdbeAddOp(v, OP_If, 0, 0);
danielk19779e128002006-01-18 16:51:35 +00001309 assert( testAddr>0 || sqlite3MallocFailed() );
drhd654be82005-09-20 17:42:23 +00001310 sqlite3VdbeAddOp(v, OP_MemInt, 1, mem);
danielk1977b3bce662005-01-29 08:32:43 +00001311 }
1312
drhcce7d172000-05-31 15:34:51 +00001313 switch( pExpr->op ){
drhfef52082000-06-06 01:50:43 +00001314 case TK_IN: {
danielk1977e014a832004-05-17 10:48:57 +00001315 char affinity;
drhd3d39e92004-05-20 22:16:29 +00001316 KeyInfo keyInfo;
drh9170dd72005-07-08 17:13:46 +00001317 int addr; /* Address of OP_OpenVirtual instruction */
drhd3d39e92004-05-20 22:16:29 +00001318
danielk1977bf3b7212004-05-18 10:06:24 +00001319 affinity = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +00001320
1321 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
drh57dbd7b2005-07-08 18:25:26 +00001322 ** expression it is handled the same way. A virtual table is
danielk1977e014a832004-05-17 10:48:57 +00001323 ** filled with single-field index keys representing the results
1324 ** from the SELECT or the <exprlist>.
1325 **
1326 ** If the 'x' expression is a column value, or the SELECT...
1327 ** statement returns a column value, then the affinity of that
1328 ** column is used to build the index keys. If both 'x' and the
1329 ** SELECT... statement are columns, then numeric affinity is used
1330 ** if either column has NUMERIC or INTEGER affinity. If neither
1331 ** 'x' nor the SELECT... statement are columns, then numeric affinity
1332 ** is used.
1333 */
1334 pExpr->iTable = pParse->nTab++;
drh9170dd72005-07-08 17:13:46 +00001335 addr = sqlite3VdbeAddOp(v, OP_OpenVirtual, pExpr->iTable, 0);
drhd3d39e92004-05-20 22:16:29 +00001336 memset(&keyInfo, 0, sizeof(keyInfo));
1337 keyInfo.nField = 1;
drhf3218fe2004-05-28 08:21:02 +00001338 sqlite3VdbeAddOp(v, OP_SetNumColumns, pExpr->iTable, 1);
danielk1977e014a832004-05-17 10:48:57 +00001339
drhfef52082000-06-06 01:50:43 +00001340 if( pExpr->pSelect ){
1341 /* Case 1: expr IN (SELECT ...)
1342 **
danielk1977e014a832004-05-17 10:48:57 +00001343 ** Generate code to write the results of the select into the temporary
1344 ** table allocated and opened above.
drhfef52082000-06-06 01:50:43 +00001345 */
danielk1977e014a832004-05-17 10:48:57 +00001346 int iParm = pExpr->iTable + (((int)affinity)<<16);
drhbe5c89a2004-07-26 00:31:09 +00001347 ExprList *pEList;
danielk1977e014a832004-05-17 10:48:57 +00001348 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
danielk1977b3bce662005-01-29 08:32:43 +00001349 sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001350 pEList = pExpr->pSelect->pEList;
1351 if( pEList && pEList->nExpr>0 ){
danielk19777cedc8d2004-06-10 10:50:08 +00001352 keyInfo.aColl[0] = binaryCompareCollSeq(pParse, pExpr->pLeft,
drhbe5c89a2004-07-26 00:31:09 +00001353 pEList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001354 }
drhfef52082000-06-06 01:50:43 +00001355 }else if( pExpr->pList ){
1356 /* Case 2: expr IN (exprlist)
1357 **
danielk1977e014a832004-05-17 10:48:57 +00001358 ** For each expression, build an index key from the evaluation and
1359 ** store it in the temporary table. If <expr> is a column, then use
1360 ** that columns affinity when building index keys. If <expr> is not
1361 ** a column, use numeric affinity.
drhfef52082000-06-06 01:50:43 +00001362 */
danielk1977e014a832004-05-17 10:48:57 +00001363 int i;
drh57dbd7b2005-07-08 18:25:26 +00001364 ExprList *pList = pExpr->pList;
1365 struct ExprList_item *pItem;
1366
danielk1977e014a832004-05-17 10:48:57 +00001367 if( !affinity ){
1368 affinity = SQLITE_AFF_NUMERIC;
1369 }
danielk19770202b292004-06-09 09:55:16 +00001370 keyInfo.aColl[0] = pExpr->pLeft->pColl;
danielk1977e014a832004-05-17 10:48:57 +00001371
1372 /* Loop through each expression in <exprlist>. */
drh57dbd7b2005-07-08 18:25:26 +00001373 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
1374 Expr *pE2 = pItem->pExpr;
danielk1977e014a832004-05-17 10:48:57 +00001375
drh57dbd7b2005-07-08 18:25:26 +00001376 /* If the expression is not constant then we will need to
1377 ** disable the test that was generated above that makes sure
1378 ** this code only executes once. Because for a non-constant
1379 ** expression we need to rerun this code each time.
1380 */
drh6c30be82005-07-29 15:10:17 +00001381 if( testAddr>0 && !sqlite3ExprIsConstant(pE2) ){
drh57dbd7b2005-07-08 18:25:26 +00001382 VdbeOp *aOp = sqlite3VdbeGetOp(v, testAddr-1);
1383 int i;
drhd654be82005-09-20 17:42:23 +00001384 for(i=0; i<3; i++){
drh57dbd7b2005-07-08 18:25:26 +00001385 aOp[i].opcode = OP_Noop;
1386 }
1387 testAddr = 0;
drh4794b982000-06-06 13:54:14 +00001388 }
danielk1977e014a832004-05-17 10:48:57 +00001389
1390 /* Evaluate the expression and insert it into the temp table */
1391 sqlite3ExprCode(pParse, pE2);
drh94a11212004-09-25 13:12:14 +00001392 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1);
drhf0863fe2005-06-12 21:35:51 +00001393 sqlite3VdbeAddOp(v, OP_IdxInsert, pExpr->iTable, 0);
drhfef52082000-06-06 01:50:43 +00001394 }
1395 }
danielk19770202b292004-06-09 09:55:16 +00001396 sqlite3VdbeChangeP3(v, addr, (void *)&keyInfo, P3_KEYINFO);
danielk1977b3bce662005-01-29 08:32:43 +00001397 break;
drhfef52082000-06-06 01:50:43 +00001398 }
1399
drh51522cd2005-01-20 13:36:19 +00001400 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001401 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +00001402 /* This has to be a scalar SELECT. Generate code to put the
1403 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +00001404 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +00001405 */
drh2646da72005-12-09 20:02:05 +00001406 static const Token one = { (u8*)"1", 0, 1 };
drh51522cd2005-01-20 13:36:19 +00001407 Select *pSel;
drhec7429a2005-10-06 16:53:14 +00001408 int iMem;
1409 int sop;
drh1398ad32005-01-19 23:24:50 +00001410
drhec7429a2005-10-06 16:53:14 +00001411 pExpr->iColumn = iMem = pParse->nMem++;
drh51522cd2005-01-20 13:36:19 +00001412 pSel = pExpr->pSelect;
1413 if( pExpr->op==TK_SELECT ){
1414 sop = SRT_Mem;
drhec7429a2005-10-06 16:53:14 +00001415 sqlite3VdbeAddOp(v, OP_MemNull, iMem, 0);
1416 VdbeComment((v, "# Init subquery result"));
drh51522cd2005-01-20 13:36:19 +00001417 }else{
drh51522cd2005-01-20 13:36:19 +00001418 sop = SRT_Exists;
drhec7429a2005-10-06 16:53:14 +00001419 sqlite3VdbeAddOp(v, OP_MemInt, 0, iMem);
1420 VdbeComment((v, "# Init EXISTS result"));
drh51522cd2005-01-20 13:36:19 +00001421 }
drhec7429a2005-10-06 16:53:14 +00001422 sqlite3ExprDelete(pSel->pLimit);
1423 pSel->pLimit = sqlite3Expr(TK_INTEGER, 0, 0, &one);
1424 sqlite3Select(pParse, pSel, sop, iMem, 0, 0, 0, 0);
danielk1977b3bce662005-01-29 08:32:43 +00001425 break;
drhcce7d172000-05-31 15:34:51 +00001426 }
1427 }
danielk1977b3bce662005-01-29 08:32:43 +00001428
drh57dbd7b2005-07-08 18:25:26 +00001429 if( testAddr ){
drhd654be82005-09-20 17:42:23 +00001430 sqlite3VdbeJumpHere(v, testAddr);
danielk1977b3bce662005-01-29 08:32:43 +00001431 }
1432 return;
drhcce7d172000-05-31 15:34:51 +00001433}
drh51522cd2005-01-20 13:36:19 +00001434#endif /* SQLITE_OMIT_SUBQUERY */
drhcce7d172000-05-31 15:34:51 +00001435
drhcce7d172000-05-31 15:34:51 +00001436/*
drhfec19aa2004-05-19 20:41:03 +00001437** Generate an instruction that will put the integer describe by
1438** text z[0..n-1] on the stack.
1439*/
1440static void codeInteger(Vdbe *v, const char *z, int n){
1441 int i;
drh6fec0762004-05-30 01:38:43 +00001442 if( sqlite3GetInt32(z, &i) ){
1443 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
1444 }else if( sqlite3FitsIn64Bits(z) ){
drh29dda4a2005-07-21 18:23:20 +00001445 sqlite3VdbeOp3(v, OP_Int64, 0, 0, z, n);
drhfec19aa2004-05-19 20:41:03 +00001446 }else{
1447 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n);
1448 }
1449}
1450
1451/*
drhcce7d172000-05-31 15:34:51 +00001452** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +00001453** expression and leave the result on the top of stack.
drhf2bc0132004-10-04 13:19:23 +00001454**
1455** This code depends on the fact that certain token values (ex: TK_EQ)
1456** are the same as opcode values (ex: OP_Eq) that implement the corresponding
1457** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
1458** the make process cause these values to align. Assert()s in the code
1459** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00001460*/
danielk19774adee202004-05-08 08:23:19 +00001461void sqlite3ExprCode(Parse *pParse, Expr *pExpr){
drhcce7d172000-05-31 15:34:51 +00001462 Vdbe *v = pParse->pVdbe;
1463 int op;
drhffe07b22005-11-03 00:41:17 +00001464 int stackChng = 1; /* Amount of change to stack depth */
1465
danielk19777977a172004-11-09 12:44:37 +00001466 if( v==0 ) return;
1467 if( pExpr==0 ){
drhf0863fe2005-06-12 21:35:51 +00001468 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
danielk19777977a172004-11-09 12:44:37 +00001469 return;
1470 }
drhf2bc0132004-10-04 13:19:23 +00001471 op = pExpr->op;
1472 switch( op ){
drh13449892005-09-07 21:22:45 +00001473 case TK_AGG_COLUMN: {
1474 AggInfo *pAggInfo = pExpr->pAggInfo;
1475 struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg];
1476 if( !pAggInfo->directMode ){
1477 sqlite3VdbeAddOp(v, OP_MemLoad, pCol->iMem, 0);
1478 break;
1479 }else if( pAggInfo->useSortingIdx ){
1480 sqlite3VdbeAddOp(v, OP_Column, pAggInfo->sortingIdx,
1481 pCol->iSorterColumn);
1482 break;
1483 }
1484 /* Otherwise, fall thru into the TK_COLUMN case */
1485 }
drh967e8b72000-06-21 13:59:10 +00001486 case TK_COLUMN: {
drhffe07b22005-11-03 00:41:17 +00001487 if( pExpr->iTable<0 ){
1488 /* This only happens when coding check constraints */
1489 assert( pParse->ckOffset>0 );
1490 sqlite3VdbeAddOp(v, OP_Dup, pParse->ckOffset-pExpr->iColumn-1, 1);
1491 }else if( pExpr->iColumn>=0 ){
drh8a512562005-11-14 22:29:05 +00001492 Table *pTab = pExpr->pTab;
1493 int iCol = pExpr->iColumn;
1494 sqlite3VdbeAddOp(v, OP_Column, pExpr->iTable, iCol);
1495 sqlite3ColumnDefault(v, pTab, iCol);
1496#ifndef SQLITE_OMIT_FLOATING_POINT
1497 if( pTab && pTab->aCol[iCol].affinity==SQLITE_AFF_REAL ){
1498 sqlite3VdbeAddOp(v, OP_RealAffinity, 0, 0);
1499 }
1500#endif
drhc4a3c772001-04-04 11:48:57 +00001501 }else{
drhf0863fe2005-06-12 21:35:51 +00001502 sqlite3VdbeAddOp(v, OP_Rowid, pExpr->iTable, 0);
drh22827922000-06-06 17:27:05 +00001503 }
drhcce7d172000-05-31 15:34:51 +00001504 break;
1505 }
1506 case TK_INTEGER: {
drh2646da72005-12-09 20:02:05 +00001507 codeInteger(v, (char*)pExpr->token.z, pExpr->token.n);
drhfec19aa2004-05-19 20:41:03 +00001508 break;
1509 }
1510 case TK_FLOAT:
1511 case TK_STRING: {
drhf2bc0132004-10-04 13:19:23 +00001512 assert( TK_FLOAT==OP_Real );
1513 assert( TK_STRING==OP_String8 );
drhd2687b72005-08-12 22:56:09 +00001514 sqlite3DequoteExpr(pExpr);
drh2646da72005-12-09 20:02:05 +00001515 sqlite3VdbeOp3(v, op, 0, 0, (char*)pExpr->token.z, pExpr->token.n);
drhcce7d172000-05-31 15:34:51 +00001516 break;
1517 }
drhf0863fe2005-06-12 21:35:51 +00001518 case TK_NULL: {
1519 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
1520 break;
1521 }
danielk19775338a5f2005-01-20 13:03:10 +00001522#ifndef SQLITE_OMIT_BLOB_LITERAL
danielk1977c572ef72004-05-27 09:28:41 +00001523 case TK_BLOB: {
drh6c8c6ce2005-08-23 11:17:58 +00001524 int n;
1525 const char *z;
drhf2bc0132004-10-04 13:19:23 +00001526 assert( TK_BLOB==OP_HexBlob );
drh6c8c6ce2005-08-23 11:17:58 +00001527 n = pExpr->token.n - 3;
drh2646da72005-12-09 20:02:05 +00001528 z = (char*)pExpr->token.z + 2;
drh6c8c6ce2005-08-23 11:17:58 +00001529 assert( n>=0 );
1530 if( n==0 ){
1531 z = "";
1532 }
1533 sqlite3VdbeOp3(v, op, 0, 0, z, n);
danielk1977c572ef72004-05-27 09:28:41 +00001534 break;
1535 }
danielk19775338a5f2005-01-20 13:03:10 +00001536#endif
drh50457892003-09-06 01:10:47 +00001537 case TK_VARIABLE: {
danielk19774adee202004-05-08 08:23:19 +00001538 sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0);
drh895d7472004-08-20 16:02:39 +00001539 if( pExpr->token.n>1 ){
drh2646da72005-12-09 20:02:05 +00001540 sqlite3VdbeChangeP3(v, -1, (char*)pExpr->token.z, pExpr->token.n);
drh895d7472004-08-20 16:02:39 +00001541 }
drh50457892003-09-06 01:10:47 +00001542 break;
1543 }
drh4e0cff62004-11-05 05:10:28 +00001544 case TK_REGISTER: {
1545 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iTable, 0);
1546 break;
1547 }
drh487e2622005-06-25 18:42:14 +00001548#ifndef SQLITE_OMIT_CAST
1549 case TK_CAST: {
1550 /* Expressions of the form: CAST(pLeft AS token) */
1551 int aff, op;
1552 sqlite3ExprCode(pParse, pExpr->pLeft);
drh8a512562005-11-14 22:29:05 +00001553 aff = sqlite3AffinityType(&pExpr->token);
1554 op = aff - SQLITE_AFF_TEXT + OP_ToText;
1555 assert( op==OP_ToText || aff!=SQLITE_AFF_TEXT );
1556 assert( op==OP_ToBlob || aff!=SQLITE_AFF_NONE );
1557 assert( op==OP_ToNumeric || aff!=SQLITE_AFF_NUMERIC );
1558 assert( op==OP_ToInt || aff!=SQLITE_AFF_INTEGER );
1559 assert( op==OP_ToReal || aff!=SQLITE_AFF_REAL );
drh487e2622005-06-25 18:42:14 +00001560 sqlite3VdbeAddOp(v, op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001561 stackChng = 0;
drh487e2622005-06-25 18:42:14 +00001562 break;
1563 }
1564#endif /* SQLITE_OMIT_CAST */
drhc9b84a12002-06-20 11:36:48 +00001565 case TK_LT:
1566 case TK_LE:
1567 case TK_GT:
1568 case TK_GE:
1569 case TK_NE:
1570 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00001571 assert( TK_LT==OP_Lt );
1572 assert( TK_LE==OP_Le );
1573 assert( TK_GT==OP_Gt );
1574 assert( TK_GE==OP_Ge );
1575 assert( TK_EQ==OP_Eq );
1576 assert( TK_NE==OP_Ne );
danielk1977a37cdde2004-05-16 11:15:36 +00001577 sqlite3ExprCode(pParse, pExpr->pLeft);
1578 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001579 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001580 stackChng = -1;
danielk1977a37cdde2004-05-16 11:15:36 +00001581 break;
drhc9b84a12002-06-20 11:36:48 +00001582 }
drhcce7d172000-05-31 15:34:51 +00001583 case TK_AND:
1584 case TK_OR:
1585 case TK_PLUS:
1586 case TK_STAR:
1587 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00001588 case TK_REM:
1589 case TK_BITAND:
1590 case TK_BITOR:
drh17c40292004-07-21 02:53:29 +00001591 case TK_SLASH:
drhbf4133c2001-10-13 02:59:08 +00001592 case TK_LSHIFT:
drh855eb1c2004-08-31 13:45:11 +00001593 case TK_RSHIFT:
drh00400772000-06-16 20:51:26 +00001594 case TK_CONCAT: {
drhf2bc0132004-10-04 13:19:23 +00001595 assert( TK_AND==OP_And );
1596 assert( TK_OR==OP_Or );
1597 assert( TK_PLUS==OP_Add );
1598 assert( TK_MINUS==OP_Subtract );
1599 assert( TK_REM==OP_Remainder );
1600 assert( TK_BITAND==OP_BitAnd );
1601 assert( TK_BITOR==OP_BitOr );
1602 assert( TK_SLASH==OP_Divide );
1603 assert( TK_LSHIFT==OP_ShiftLeft );
1604 assert( TK_RSHIFT==OP_ShiftRight );
1605 assert( TK_CONCAT==OP_Concat );
danielk19774adee202004-05-08 08:23:19 +00001606 sqlite3ExprCode(pParse, pExpr->pLeft);
1607 sqlite3ExprCode(pParse, pExpr->pRight);
drh855eb1c2004-08-31 13:45:11 +00001608 sqlite3VdbeAddOp(v, op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001609 stackChng = -1;
drh00400772000-06-16 20:51:26 +00001610 break;
1611 }
drhcce7d172000-05-31 15:34:51 +00001612 case TK_UMINUS: {
drhfec19aa2004-05-19 20:41:03 +00001613 Expr *pLeft = pExpr->pLeft;
1614 assert( pLeft );
1615 if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
1616 Token *p = &pLeft->token;
drh9267bdc2005-11-28 12:36:35 +00001617 char *z = sqlite3MPrintf("-%.*s", p->n, p->z);
drhfec19aa2004-05-19 20:41:03 +00001618 if( pLeft->op==TK_FLOAT ){
1619 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001620 }else{
drhfec19aa2004-05-19 20:41:03 +00001621 codeInteger(v, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001622 }
drh6e142f52000-06-08 13:36:40 +00001623 sqliteFree(z);
1624 break;
1625 }
drh1ccde152000-06-17 13:12:39 +00001626 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +00001627 }
drhbf4133c2001-10-13 02:59:08 +00001628 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00001629 case TK_NOT: {
drhf2bc0132004-10-04 13:19:23 +00001630 assert( TK_BITNOT==OP_BitNot );
1631 assert( TK_NOT==OP_Not );
danielk19774adee202004-05-08 08:23:19 +00001632 sqlite3ExprCode(pParse, pExpr->pLeft);
1633 sqlite3VdbeAddOp(v, op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001634 stackChng = 0;
drhcce7d172000-05-31 15:34:51 +00001635 break;
1636 }
1637 case TK_ISNULL:
1638 case TK_NOTNULL: {
1639 int dest;
drhf2bc0132004-10-04 13:19:23 +00001640 assert( TK_ISNULL==OP_IsNull );
1641 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00001642 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1643 sqlite3ExprCode(pParse, pExpr->pLeft);
1644 dest = sqlite3VdbeCurrentAddr(v) + 2;
1645 sqlite3VdbeAddOp(v, op, 1, dest);
1646 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);
drhffe07b22005-11-03 00:41:17 +00001647 stackChng = 0;
drhf2bc0132004-10-04 13:19:23 +00001648 break;
drhcce7d172000-05-31 15:34:51 +00001649 }
drh22827922000-06-06 17:27:05 +00001650 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00001651 AggInfo *pInfo = pExpr->pAggInfo;
drh7e56e712005-11-16 12:53:15 +00001652 if( pInfo==0 ){
1653 sqlite3ErrorMsg(pParse, "misuse of aggregate: %T",
1654 &pExpr->span);
1655 }else{
1656 sqlite3VdbeAddOp(v, OP_MemLoad, pInfo->aFunc[pExpr->iAgg].iMem, 0);
1657 }
drh22827922000-06-06 17:27:05 +00001658 break;
1659 }
drhb71090f2005-05-23 17:26:51 +00001660 case TK_CONST_FUNC:
drhcce7d172000-05-31 15:34:51 +00001661 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00001662 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00001663 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00001664 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00001665 int nId;
1666 const char *zId;
drh13449892005-09-07 21:22:45 +00001667 int constMask = 0;
danielk1977682f68b2004-06-05 10:22:17 +00001668 int i;
danielk197714db2662006-01-09 16:12:04 +00001669 u8 enc = ENC(pParse->db);
danielk1977dc1bdc42004-06-11 10:51:27 +00001670 CollSeq *pColl = 0;
drh2646da72005-12-09 20:02:05 +00001671 zId = (char*)pExpr->token.z;
drhb71090f2005-05-23 17:26:51 +00001672 nId = pExpr->token.n;
danielk1977d8123362004-06-12 09:25:12 +00001673 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001674 assert( pDef!=0 );
drhf9b596e2004-05-26 16:54:42 +00001675 nExpr = sqlite3ExprCodeExprList(pParse, pList);
danielk1977682f68b2004-06-05 10:22:17 +00001676 for(i=0; i<nExpr && i<32; i++){
danielk1977d02eb1f2004-06-06 09:44:03 +00001677 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
drh13449892005-09-07 21:22:45 +00001678 constMask |= (1<<i);
danielk1977d02eb1f2004-06-06 09:44:03 +00001679 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001680 if( pDef->needCollSeq && !pColl ){
1681 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
1682 }
1683 }
1684 if( pDef->needCollSeq ){
1685 if( !pColl ) pColl = pParse->db->pDfltColl;
danielk1977d8123362004-06-12 09:25:12 +00001686 sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ);
danielk1977682f68b2004-06-05 10:22:17 +00001687 }
drh13449892005-09-07 21:22:45 +00001688 sqlite3VdbeOp3(v, OP_Function, constMask, nExpr, (char*)pDef, P3_FUNCDEF);
drhffe07b22005-11-03 00:41:17 +00001689 stackChng = 1-nExpr;
drhcce7d172000-05-31 15:34:51 +00001690 break;
1691 }
drhfe2093d2005-01-20 22:48:47 +00001692#ifndef SQLITE_OMIT_SUBQUERY
1693 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001694 case TK_SELECT: {
danielk1977b3bce662005-01-29 08:32:43 +00001695 sqlite3CodeSubselect(pParse, pExpr);
danielk19774adee202004-05-08 08:23:19 +00001696 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drhad6d9462004-09-19 02:15:24 +00001697 VdbeComment((v, "# load subquery result"));
drh19a775c2000-06-05 18:54:46 +00001698 break;
1699 }
drhfef52082000-06-06 01:50:43 +00001700 case TK_IN: {
1701 int addr;
drh94a11212004-09-25 13:12:14 +00001702 char affinity;
danielk1977b3bce662005-01-29 08:32:43 +00001703 sqlite3CodeSubselect(pParse, pExpr);
danielk1977e014a832004-05-17 10:48:57 +00001704
1705 /* Figure out the affinity to use to create a key from the results
1706 ** of the expression. affinityStr stores a static string suitable for
danielk1977ededfd52004-06-17 07:53:01 +00001707 ** P3 of OP_MakeRecord.
danielk1977e014a832004-05-17 10:48:57 +00001708 */
drh94a11212004-09-25 13:12:14 +00001709 affinity = comparisonAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +00001710
danielk19774adee202004-05-08 08:23:19 +00001711 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
danielk1977e014a832004-05-17 10:48:57 +00001712
1713 /* Code the <expr> from "<expr> IN (...)". The temporary table
1714 ** pExpr->iTable contains the values that make up the (...) set.
1715 */
danielk19774adee202004-05-08 08:23:19 +00001716 sqlite3ExprCode(pParse, pExpr->pLeft);
1717 addr = sqlite3VdbeCurrentAddr(v);
danielk1977e014a832004-05-17 10:48:57 +00001718 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); /* addr + 0 */
danielk19774adee202004-05-08 08:23:19 +00001719 sqlite3VdbeAddOp(v, OP_Pop, 2, 0);
drhf0863fe2005-06-12 21:35:51 +00001720 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +00001721 sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7);
drh94a11212004-09-25 13:12:14 +00001722 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1); /* addr + 4 */
danielk1977e014a832004-05-17 10:48:57 +00001723 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7);
1724 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); /* addr + 6 */
1725
drhfef52082000-06-06 01:50:43 +00001726 break;
1727 }
danielk197793758c82005-01-21 08:13:14 +00001728#endif
drhfef52082000-06-06 01:50:43 +00001729 case TK_BETWEEN: {
drhbe5c89a2004-07-26 00:31:09 +00001730 Expr *pLeft = pExpr->pLeft;
1731 struct ExprList_item *pLItem = pExpr->pList->a;
1732 Expr *pRight = pLItem->pExpr;
1733 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001734 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001735 sqlite3ExprCode(pParse, pRight);
1736 codeCompare(pParse, pLeft, pRight, OP_Ge, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001737 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
drhbe5c89a2004-07-26 00:31:09 +00001738 pLItem++;
1739 pRight = pLItem->pExpr;
1740 sqlite3ExprCode(pParse, pRight);
1741 codeCompare(pParse, pLeft, pRight, OP_Le, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001742 sqlite3VdbeAddOp(v, OP_And, 0, 0);
drhfef52082000-06-06 01:50:43 +00001743 break;
1744 }
drh51e9a442004-01-16 16:42:53 +00001745 case TK_UPLUS:
drha2e00042002-01-22 03:13:42 +00001746 case TK_AS: {
danielk19774adee202004-05-08 08:23:19 +00001747 sqlite3ExprCode(pParse, pExpr->pLeft);
drhffe07b22005-11-03 00:41:17 +00001748 stackChng = 0;
drha2e00042002-01-22 03:13:42 +00001749 break;
1750 }
drh17a7f8d2002-03-24 13:13:27 +00001751 case TK_CASE: {
1752 int expr_end_label;
drhf5905aa2002-05-26 20:54:33 +00001753 int jumpInst;
drhf5905aa2002-05-26 20:54:33 +00001754 int nExpr;
drh17a7f8d2002-03-24 13:13:27 +00001755 int i;
drhbe5c89a2004-07-26 00:31:09 +00001756 ExprList *pEList;
1757 struct ExprList_item *aListelem;
drh17a7f8d2002-03-24 13:13:27 +00001758
1759 assert(pExpr->pList);
1760 assert((pExpr->pList->nExpr % 2) == 0);
1761 assert(pExpr->pList->nExpr > 0);
drhbe5c89a2004-07-26 00:31:09 +00001762 pEList = pExpr->pList;
1763 aListelem = pEList->a;
1764 nExpr = pEList->nExpr;
danielk19774adee202004-05-08 08:23:19 +00001765 expr_end_label = sqlite3VdbeMakeLabel(v);
drh17a7f8d2002-03-24 13:13:27 +00001766 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001767 sqlite3ExprCode(pParse, pExpr->pLeft);
drh17a7f8d2002-03-24 13:13:27 +00001768 }
drhf5905aa2002-05-26 20:54:33 +00001769 for(i=0; i<nExpr; i=i+2){
drhbe5c89a2004-07-26 00:31:09 +00001770 sqlite3ExprCode(pParse, aListelem[i].pExpr);
drh17a7f8d2002-03-24 13:13:27 +00001771 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001772 sqlite3VdbeAddOp(v, OP_Dup, 1, 1);
drhbe5c89a2004-07-26 00:31:09 +00001773 jumpInst = codeCompare(pParse, pExpr->pLeft, aListelem[i].pExpr,
1774 OP_Ne, 0, 1);
danielk19774adee202004-05-08 08:23:19 +00001775 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001776 }else{
danielk19774adee202004-05-08 08:23:19 +00001777 jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001778 }
drhbe5c89a2004-07-26 00:31:09 +00001779 sqlite3ExprCode(pParse, aListelem[i+1].pExpr);
danielk19774adee202004-05-08 08:23:19 +00001780 sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label);
drhd654be82005-09-20 17:42:23 +00001781 sqlite3VdbeJumpHere(v, jumpInst);
drh17a7f8d2002-03-24 13:13:27 +00001782 }
drhf570f012002-05-31 15:51:25 +00001783 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001784 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhf570f012002-05-31 15:51:25 +00001785 }
drh17a7f8d2002-03-24 13:13:27 +00001786 if( pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001787 sqlite3ExprCode(pParse, pExpr->pRight);
drh17a7f8d2002-03-24 13:13:27 +00001788 }else{
drhf0863fe2005-06-12 21:35:51 +00001789 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
drh17a7f8d2002-03-24 13:13:27 +00001790 }
danielk19774adee202004-05-08 08:23:19 +00001791 sqlite3VdbeResolveLabel(v, expr_end_label);
danielk19776f349032002-06-11 02:25:40 +00001792 break;
1793 }
danielk19775338a5f2005-01-20 13:03:10 +00001794#ifndef SQLITE_OMIT_TRIGGER
danielk19776f349032002-06-11 02:25:40 +00001795 case TK_RAISE: {
1796 if( !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +00001797 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00001798 "RAISE() may only be used within a trigger-program");
danielk19776f349032002-06-11 02:25:40 +00001799 return;
1800 }
drhad6d9462004-09-19 02:15:24 +00001801 if( pExpr->iColumn!=OE_Ignore ){
1802 assert( pExpr->iColumn==OE_Rollback ||
1803 pExpr->iColumn == OE_Abort ||
1804 pExpr->iColumn == OE_Fail );
drhd2687b72005-08-12 22:56:09 +00001805 sqlite3DequoteExpr(pExpr);
drhad6d9462004-09-19 02:15:24 +00001806 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn,
drh2646da72005-12-09 20:02:05 +00001807 (char*)pExpr->token.z, pExpr->token.n);
danielk19776f349032002-06-11 02:25:40 +00001808 } else {
drhad6d9462004-09-19 02:15:24 +00001809 assert( pExpr->iColumn == OE_Ignore );
1810 sqlite3VdbeAddOp(v, OP_ContextPop, 0, 0);
1811 sqlite3VdbeAddOp(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
1812 VdbeComment((v, "# raise(IGNORE)"));
danielk19776f349032002-06-11 02:25:40 +00001813 }
drhffe07b22005-11-03 00:41:17 +00001814 stackChng = 0;
1815 break;
drh17a7f8d2002-03-24 13:13:27 +00001816 }
danielk19775338a5f2005-01-20 13:03:10 +00001817#endif
drhffe07b22005-11-03 00:41:17 +00001818 }
1819
1820 if( pParse->ckOffset ){
1821 pParse->ckOffset += stackChng;
1822 assert( pParse->ckOffset );
drhcce7d172000-05-31 15:34:51 +00001823 }
drhcce7d172000-05-31 15:34:51 +00001824}
1825
danielk197793758c82005-01-21 08:13:14 +00001826#ifndef SQLITE_OMIT_TRIGGER
drhcce7d172000-05-31 15:34:51 +00001827/*
drh25303782004-12-07 15:41:48 +00001828** Generate code that evalutes the given expression and leaves the result
1829** on the stack. See also sqlite3ExprCode().
1830**
1831** This routine might also cache the result and modify the pExpr tree
1832** so that it will make use of the cached result on subsequent evaluations
1833** rather than evaluate the whole expression again. Trivial expressions are
1834** not cached. If the expression is cached, its result is stored in a
1835** memory location.
1836*/
1837void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr){
1838 Vdbe *v = pParse->pVdbe;
1839 int iMem;
1840 int addr1, addr2;
1841 if( v==0 ) return;
1842 addr1 = sqlite3VdbeCurrentAddr(v);
1843 sqlite3ExprCode(pParse, pExpr);
1844 addr2 = sqlite3VdbeCurrentAddr(v);
1845 if( addr2>addr1+1 || sqlite3VdbeGetOp(v, addr1)->opcode==OP_Function ){
1846 iMem = pExpr->iTable = pParse->nMem++;
1847 sqlite3VdbeAddOp(v, OP_MemStore, iMem, 0);
1848 pExpr->op = TK_REGISTER;
1849 }
1850}
danielk197793758c82005-01-21 08:13:14 +00001851#endif
drh25303782004-12-07 15:41:48 +00001852
1853/*
drh268380c2004-02-25 13:47:31 +00001854** Generate code that pushes the value of every element of the given
drhf9b596e2004-05-26 16:54:42 +00001855** expression list onto the stack.
drh268380c2004-02-25 13:47:31 +00001856**
1857** Return the number of elements pushed onto the stack.
1858*/
danielk19774adee202004-05-08 08:23:19 +00001859int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00001860 Parse *pParse, /* Parsing context */
drhf9b596e2004-05-26 16:54:42 +00001861 ExprList *pList /* The expression list to be coded */
drh268380c2004-02-25 13:47:31 +00001862){
1863 struct ExprList_item *pItem;
1864 int i, n;
drh268380c2004-02-25 13:47:31 +00001865 if( pList==0 ) return 0;
drh268380c2004-02-25 13:47:31 +00001866 n = pList->nExpr;
drhc182d162005-08-14 20:47:16 +00001867 for(pItem=pList->a, i=n; i>0; i--, pItem++){
danielk19774adee202004-05-08 08:23:19 +00001868 sqlite3ExprCode(pParse, pItem->pExpr);
drh268380c2004-02-25 13:47:31 +00001869 }
drhf9b596e2004-05-26 16:54:42 +00001870 return n;
drh268380c2004-02-25 13:47:31 +00001871}
1872
1873/*
drhcce7d172000-05-31 15:34:51 +00001874** Generate code for a boolean expression such that a jump is made
1875** to the label "dest" if the expression is true but execution
1876** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00001877**
1878** If the expression evaluates to NULL (neither true nor false), then
1879** take the jump if the jumpIfNull flag is true.
drhf2bc0132004-10-04 13:19:23 +00001880**
1881** This code depends on the fact that certain token values (ex: TK_EQ)
1882** are the same as opcode values (ex: OP_Eq) that implement the corresponding
1883** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
1884** the make process cause these values to align. Assert()s in the code
1885** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00001886*/
danielk19774adee202004-05-08 08:23:19 +00001887void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001888 Vdbe *v = pParse->pVdbe;
1889 int op = 0;
drhffe07b22005-11-03 00:41:17 +00001890 int ckOffset = pParse->ckOffset;
drhdaffd0e2001-04-11 14:28:42 +00001891 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00001892 op = pExpr->op;
1893 switch( op ){
drhcce7d172000-05-31 15:34:51 +00001894 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001895 int d2 = sqlite3VdbeMakeLabel(v);
1896 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
1897 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
1898 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001899 break;
1900 }
1901 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001902 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
1903 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001904 break;
1905 }
1906 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001907 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001908 break;
1909 }
1910 case TK_LT:
1911 case TK_LE:
1912 case TK_GT:
1913 case TK_GE:
1914 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00001915 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00001916 assert( TK_LT==OP_Lt );
1917 assert( TK_LE==OP_Le );
1918 assert( TK_GT==OP_Gt );
1919 assert( TK_GE==OP_Ge );
1920 assert( TK_EQ==OP_Eq );
1921 assert( TK_NE==OP_Ne );
danielk19774adee202004-05-08 08:23:19 +00001922 sqlite3ExprCode(pParse, pExpr->pLeft);
1923 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001924 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001925 break;
1926 }
1927 case TK_ISNULL:
1928 case TK_NOTNULL: {
drhf2bc0132004-10-04 13:19:23 +00001929 assert( TK_ISNULL==OP_IsNull );
1930 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00001931 sqlite3ExprCode(pParse, pExpr->pLeft);
1932 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001933 break;
1934 }
drhfef52082000-06-06 01:50:43 +00001935 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00001936 /* The expression "x BETWEEN y AND z" is implemented as:
1937 **
1938 ** 1 IF (x < y) GOTO 3
1939 ** 2 IF (x <= z) GOTO <dest>
1940 ** 3 ...
1941 */
drhf5905aa2002-05-26 20:54:33 +00001942 int addr;
drhbe5c89a2004-07-26 00:31:09 +00001943 Expr *pLeft = pExpr->pLeft;
1944 Expr *pRight = pExpr->pList->a[0].pExpr;
1945 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001946 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001947 sqlite3ExprCode(pParse, pRight);
1948 addr = codeCompare(pParse, pLeft, pRight, OP_Lt, 0, !jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00001949
drhbe5c89a2004-07-26 00:31:09 +00001950 pRight = pExpr->pList->a[1].pExpr;
1951 sqlite3ExprCode(pParse, pRight);
1952 codeCompare(pParse, pLeft, pRight, OP_Le, dest, jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00001953
danielk19774adee202004-05-08 08:23:19 +00001954 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
drhd654be82005-09-20 17:42:23 +00001955 sqlite3VdbeJumpHere(v, addr);
danielk19774adee202004-05-08 08:23:19 +00001956 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00001957 break;
1958 }
drhcce7d172000-05-31 15:34:51 +00001959 default: {
danielk19774adee202004-05-08 08:23:19 +00001960 sqlite3ExprCode(pParse, pExpr);
1961 sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001962 break;
1963 }
1964 }
drhffe07b22005-11-03 00:41:17 +00001965 pParse->ckOffset = ckOffset;
drhcce7d172000-05-31 15:34:51 +00001966}
1967
1968/*
drh66b89c82000-11-28 20:47:17 +00001969** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00001970** to the label "dest" if the expression is false but execution
1971** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00001972**
1973** If the expression evaluates to NULL (neither true nor false) then
1974** jump if jumpIfNull is true or fall through if jumpIfNull is false.
drhcce7d172000-05-31 15:34:51 +00001975*/
danielk19774adee202004-05-08 08:23:19 +00001976void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001977 Vdbe *v = pParse->pVdbe;
1978 int op = 0;
drhffe07b22005-11-03 00:41:17 +00001979 int ckOffset = pParse->ckOffset;
drhdaffd0e2001-04-11 14:28:42 +00001980 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00001981
1982 /* The value of pExpr->op and op are related as follows:
1983 **
1984 ** pExpr->op op
1985 ** --------- ----------
1986 ** TK_ISNULL OP_NotNull
1987 ** TK_NOTNULL OP_IsNull
1988 ** TK_NE OP_Eq
1989 ** TK_EQ OP_Ne
1990 ** TK_GT OP_Le
1991 ** TK_LE OP_Gt
1992 ** TK_GE OP_Lt
1993 ** TK_LT OP_Ge
1994 **
1995 ** For other values of pExpr->op, op is undefined and unused.
1996 ** The value of TK_ and OP_ constants are arranged such that we
1997 ** can compute the mapping above using the following expression.
1998 ** Assert()s verify that the computation is correct.
1999 */
2000 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
2001
2002 /* Verify correct alignment of TK_ and OP_ constants
2003 */
2004 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
2005 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
2006 assert( pExpr->op!=TK_NE || op==OP_Eq );
2007 assert( pExpr->op!=TK_EQ || op==OP_Ne );
2008 assert( pExpr->op!=TK_LT || op==OP_Ge );
2009 assert( pExpr->op!=TK_LE || op==OP_Gt );
2010 assert( pExpr->op!=TK_GT || op==OP_Le );
2011 assert( pExpr->op!=TK_GE || op==OP_Lt );
2012
drhcce7d172000-05-31 15:34:51 +00002013 switch( pExpr->op ){
2014 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00002015 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
2016 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002017 break;
2018 }
2019 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00002020 int d2 = sqlite3VdbeMakeLabel(v);
2021 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
2022 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
2023 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00002024 break;
2025 }
2026 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00002027 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002028 break;
2029 }
2030 case TK_LT:
2031 case TK_LE:
2032 case TK_GT:
2033 case TK_GE:
2034 case TK_NE:
2035 case TK_EQ: {
danielk19774adee202004-05-08 08:23:19 +00002036 sqlite3ExprCode(pParse, pExpr->pLeft);
2037 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00002038 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002039 break;
2040 }
drhcce7d172000-05-31 15:34:51 +00002041 case TK_ISNULL:
2042 case TK_NOTNULL: {
danielk19774adee202004-05-08 08:23:19 +00002043 sqlite3ExprCode(pParse, pExpr->pLeft);
2044 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00002045 break;
2046 }
drhfef52082000-06-06 01:50:43 +00002047 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00002048 /* The expression is "x BETWEEN y AND z". It is implemented as:
2049 **
2050 ** 1 IF (x >= y) GOTO 3
2051 ** 2 GOTO <dest>
2052 ** 3 IF (x > z) GOTO <dest>
2053 */
drhfef52082000-06-06 01:50:43 +00002054 int addr;
drhbe5c89a2004-07-26 00:31:09 +00002055 Expr *pLeft = pExpr->pLeft;
2056 Expr *pRight = pExpr->pList->a[0].pExpr;
2057 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00002058 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00002059 sqlite3ExprCode(pParse, pRight);
danielk19774adee202004-05-08 08:23:19 +00002060 addr = sqlite3VdbeCurrentAddr(v);
drhbe5c89a2004-07-26 00:31:09 +00002061 codeCompare(pParse, pLeft, pRight, OP_Ge, addr+3, !jumpIfNull);
2062
danielk19774adee202004-05-08 08:23:19 +00002063 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
2064 sqlite3VdbeAddOp(v, OP_Goto, 0, dest);
drhbe5c89a2004-07-26 00:31:09 +00002065 pRight = pExpr->pList->a[1].pExpr;
2066 sqlite3ExprCode(pParse, pRight);
2067 codeCompare(pParse, pLeft, pRight, OP_Gt, dest, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00002068 break;
2069 }
drhcce7d172000-05-31 15:34:51 +00002070 default: {
danielk19774adee202004-05-08 08:23:19 +00002071 sqlite3ExprCode(pParse, pExpr);
2072 sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00002073 break;
2074 }
2075 }
drhffe07b22005-11-03 00:41:17 +00002076 pParse->ckOffset = ckOffset;
drhcce7d172000-05-31 15:34:51 +00002077}
drh22827922000-06-06 17:27:05 +00002078
2079/*
2080** Do a deep comparison of two expression trees. Return TRUE (non-zero)
2081** if they are identical and return FALSE if they differ in any way.
2082*/
danielk19774adee202004-05-08 08:23:19 +00002083int sqlite3ExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00002084 int i;
danielk19774b202ae2006-01-23 05:50:58 +00002085 if( pA==0||pB==0 ){
2086 return pB==pA;
drh22827922000-06-06 17:27:05 +00002087 }
2088 if( pA->op!=pB->op ) return 0;
drhfd357972005-09-09 01:33:19 +00002089 if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 0;
danielk19774adee202004-05-08 08:23:19 +00002090 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
2091 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00002092 if( pA->pList ){
2093 if( pB->pList==0 ) return 0;
2094 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
2095 for(i=0; i<pA->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00002096 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00002097 return 0;
2098 }
2099 }
2100 }else if( pB->pList ){
2101 return 0;
2102 }
2103 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00002104 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drh22827922000-06-06 17:27:05 +00002105 if( pA->token.z ){
2106 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00002107 if( pB->token.n!=pA->token.n ) return 0;
drh2646da72005-12-09 20:02:05 +00002108 if( sqlite3StrNICmp((char*)pA->token.z,(char*)pB->token.z,pB->token.n)!=0 ){
2109 return 0;
2110 }
drh22827922000-06-06 17:27:05 +00002111 }
2112 return 1;
2113}
2114
drh13449892005-09-07 21:22:45 +00002115
drh22827922000-06-06 17:27:05 +00002116/*
drh13449892005-09-07 21:22:45 +00002117** Add a new element to the pAggInfo->aCol[] array. Return the index of
2118** the new element. Return a negative number if malloc fails.
drh22827922000-06-06 17:27:05 +00002119*/
drh13449892005-09-07 21:22:45 +00002120static int addAggInfoColumn(AggInfo *pInfo){
2121 int i;
2122 i = sqlite3ArrayAllocate((void**)&pInfo->aCol, sizeof(pInfo->aCol[0]), 3);
2123 if( i<0 ){
2124 return -1;
drh22827922000-06-06 17:27:05 +00002125 }
drh13449892005-09-07 21:22:45 +00002126 return i;
2127}
2128
2129/*
2130** Add a new element to the pAggInfo->aFunc[] array. Return the index of
2131** the new element. Return a negative number if malloc fails.
2132*/
2133static int addAggInfoFunc(AggInfo *pInfo){
2134 int i;
2135 i = sqlite3ArrayAllocate((void**)&pInfo->aFunc, sizeof(pInfo->aFunc[0]), 2);
2136 if( i<0 ){
2137 return -1;
2138 }
2139 return i;
2140}
drh22827922000-06-06 17:27:05 +00002141
2142/*
drh626a8792005-01-17 22:08:19 +00002143** This is an xFunc for walkExprTree() used to implement
2144** sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates
2145** for additional information.
drh22827922000-06-06 17:27:05 +00002146**
drh626a8792005-01-17 22:08:19 +00002147** This routine analyzes the aggregate function at pExpr.
drh22827922000-06-06 17:27:05 +00002148*/
drh626a8792005-01-17 22:08:19 +00002149static int analyzeAggregate(void *pArg, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00002150 int i;
danielk1977a58fdfb2005-02-08 07:50:40 +00002151 NameContext *pNC = (NameContext *)pArg;
2152 Parse *pParse = pNC->pParse;
2153 SrcList *pSrcList = pNC->pSrcList;
drh13449892005-09-07 21:22:45 +00002154 AggInfo *pAggInfo = pNC->pAggInfo;
2155
drh22827922000-06-06 17:27:05 +00002156
drh22827922000-06-06 17:27:05 +00002157 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00002158 case TK_COLUMN: {
drh13449892005-09-07 21:22:45 +00002159 /* Check to see if the column is in one of the tables in the FROM
2160 ** clause of the aggregate query */
2161 if( pSrcList ){
2162 struct SrcList_item *pItem = pSrcList->a;
2163 for(i=0; i<pSrcList->nSrc; i++, pItem++){
2164 struct AggInfo_col *pCol;
2165 if( pExpr->iTable==pItem->iCursor ){
2166 /* If we reach this point, it means that pExpr refers to a table
2167 ** that is in the FROM clause of the aggregate query.
2168 **
2169 ** Make an entry for the column in pAggInfo->aCol[] if there
2170 ** is not an entry there already.
2171 */
2172 pCol = pAggInfo->aCol;
2173 for(i=0; i<pAggInfo->nColumn; i++, pCol++){
2174 if( pCol->iTable==pExpr->iTable &&
2175 pCol->iColumn==pExpr->iColumn ){
2176 break;
2177 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002178 }
drh13449892005-09-07 21:22:45 +00002179 if( i>=pAggInfo->nColumn && (i = addAggInfoColumn(pAggInfo))>=0 ){
2180 pCol = &pAggInfo->aCol[i];
2181 pCol->iTable = pExpr->iTable;
2182 pCol->iColumn = pExpr->iColumn;
2183 pCol->iMem = pParse->nMem++;
2184 pCol->iSorterColumn = -1;
drh5774b802005-09-07 22:48:16 +00002185 pCol->pExpr = pExpr;
drh13449892005-09-07 21:22:45 +00002186 if( pAggInfo->pGroupBy ){
2187 int j, n;
2188 ExprList *pGB = pAggInfo->pGroupBy;
2189 struct ExprList_item *pTerm = pGB->a;
2190 n = pGB->nExpr;
2191 for(j=0; j<n; j++, pTerm++){
2192 Expr *pE = pTerm->pExpr;
2193 if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable &&
2194 pE->iColumn==pExpr->iColumn ){
2195 pCol->iSorterColumn = j;
2196 break;
2197 }
2198 }
2199 }
2200 if( pCol->iSorterColumn<0 ){
2201 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
2202 }
2203 }
2204 /* There is now an entry for pExpr in pAggInfo->aCol[] (either
2205 ** because it was there before or because we just created it).
2206 ** Convert the pExpr to be a TK_AGG_COLUMN referring to that
2207 ** pAggInfo->aCol[] entry.
2208 */
2209 pExpr->pAggInfo = pAggInfo;
2210 pExpr->op = TK_AGG_COLUMN;
2211 pExpr->iAgg = i;
2212 break;
2213 } /* endif pExpr->iTable==pItem->iCursor */
2214 } /* end loop over pSrcList */
drh22827922000-06-06 17:27:05 +00002215 }
drh626a8792005-01-17 22:08:19 +00002216 return 1;
drh22827922000-06-06 17:27:05 +00002217 }
2218 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00002219 /* The pNC->nDepth==0 test causes aggregate functions in subqueries
2220 ** to be ignored */
danielk1977a58fdfb2005-02-08 07:50:40 +00002221 if( pNC->nDepth==0 ){
drh13449892005-09-07 21:22:45 +00002222 /* Check to see if pExpr is a duplicate of another aggregate
2223 ** function that is already in the pAggInfo structure
2224 */
2225 struct AggInfo_func *pItem = pAggInfo->aFunc;
2226 for(i=0; i<pAggInfo->nFunc; i++, pItem++){
2227 if( sqlite3ExprCompare(pItem->pExpr, pExpr) ){
danielk1977a58fdfb2005-02-08 07:50:40 +00002228 break;
2229 }
drh22827922000-06-06 17:27:05 +00002230 }
drh13449892005-09-07 21:22:45 +00002231 if( i>=pAggInfo->nFunc ){
2232 /* pExpr is original. Make a new entry in pAggInfo->aFunc[]
2233 */
danielk197714db2662006-01-09 16:12:04 +00002234 u8 enc = ENC(pParse->db);
drh13449892005-09-07 21:22:45 +00002235 i = addAggInfoFunc(pAggInfo);
2236 if( i>=0 ){
2237 pItem = &pAggInfo->aFunc[i];
2238 pItem->pExpr = pExpr;
2239 pItem->iMem = pParse->nMem++;
2240 pItem->pFunc = sqlite3FindFunction(pParse->db,
drh2646da72005-12-09 20:02:05 +00002241 (char*)pExpr->token.z, pExpr->token.n,
drh13449892005-09-07 21:22:45 +00002242 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
drhfd357972005-09-09 01:33:19 +00002243 if( pExpr->flags & EP_Distinct ){
2244 pItem->iDistinct = pParse->nTab++;
2245 }else{
2246 pItem->iDistinct = -1;
2247 }
drh13449892005-09-07 21:22:45 +00002248 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002249 }
drh13449892005-09-07 21:22:45 +00002250 /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
2251 */
danielk1977a58fdfb2005-02-08 07:50:40 +00002252 pExpr->iAgg = i;
drh13449892005-09-07 21:22:45 +00002253 pExpr->pAggInfo = pAggInfo;
danielk1977a58fdfb2005-02-08 07:50:40 +00002254 return 1;
drh22827922000-06-06 17:27:05 +00002255 }
drh22827922000-06-06 17:27:05 +00002256 }
2257 }
drh13449892005-09-07 21:22:45 +00002258
2259 /* Recursively walk subqueries looking for TK_COLUMN nodes that need
2260 ** to be changed to TK_AGG_COLUMN. But increment nDepth so that
2261 ** TK_AGG_FUNCTION nodes in subqueries will be unchanged.
2262 */
danielk1977a58fdfb2005-02-08 07:50:40 +00002263 if( pExpr->pSelect ){
2264 pNC->nDepth++;
2265 walkSelectExpr(pExpr->pSelect, analyzeAggregate, pNC);
2266 pNC->nDepth--;
2267 }
drh626a8792005-01-17 22:08:19 +00002268 return 0;
2269}
2270
2271/*
2272** Analyze the given expression looking for aggregate functions and
2273** for variables that need to be added to the pParse->aAgg[] array.
2274** Make additional entries to the pParse->aAgg[] array as necessary.
2275**
2276** This routine should only be called after the expression has been
2277** analyzed by sqlite3ExprResolveNames().
2278**
2279** If errors are seen, leave an error message in zErrMsg and return
2280** the number of errors.
2281*/
danielk1977a58fdfb2005-02-08 07:50:40 +00002282int sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
2283 int nErr = pNC->pParse->nErr;
2284 walkExprTree(pExpr, analyzeAggregate, pNC);
2285 return pNC->pParse->nErr - nErr;
drh22827922000-06-06 17:27:05 +00002286}
drh5d9a4af2005-08-30 00:54:01 +00002287
2288/*
2289** Call sqlite3ExprAnalyzeAggregates() for every expression in an
2290** expression list. Return the number of errors.
2291**
2292** If an error is found, the analysis is cut short.
2293*/
2294int sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){
2295 struct ExprList_item *pItem;
2296 int i;
2297 int nErr = 0;
2298 if( pList ){
2299 for(pItem=pList->a, i=0; nErr==0 && i<pList->nExpr; i++, pItem++){
2300 nErr += sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
2301 }
2302 }
2303 return nErr;
2304}