blob: 4e8d322b92dc488bdedee4613bb2adfb2233ca21 [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**
drh206f3d92006-07-11 13:15:08 +000015** $Id: expr.c,v 1.266 2006/07/11 13:15:08 drh Exp $
drhcce7d172000-05-31 15:34:51 +000016*/
17#include "sqliteInt.h"
drh04738cb2002-06-02 18:19:00 +000018#include <ctype.h>
drha2e00042002-01-22 03:13:42 +000019
danielk1977e014a832004-05-17 10:48:57 +000020/*
21** Return the 'affinity' of the expression pExpr if any.
22**
23** If pExpr is a column, a reference to a column via an 'AS' alias,
24** or a sub-select with a column as the return value, then the
25** affinity of that column is returned. Otherwise, 0x00 is returned,
26** indicating no affinity for the expression.
27**
28** i.e. the WHERE clause expresssions in the following statements all
29** have an affinity:
30**
31** CREATE TABLE t1(a);
32** SELECT * FROM t1 WHERE a;
33** SELECT a AS b FROM t1 WHERE b;
34** SELECT * FROM t1 WHERE (select a from t1);
35*/
danielk1977bf3b7212004-05-18 10:06:24 +000036char sqlite3ExprAffinity(Expr *pExpr){
drh487e2622005-06-25 18:42:14 +000037 int op = pExpr->op;
38 if( op==TK_AS ){
danielk1977bf3b7212004-05-18 10:06:24 +000039 return sqlite3ExprAffinity(pExpr->pLeft);
danielk1977a37cdde2004-05-16 11:15:36 +000040 }
drh487e2622005-06-25 18:42:14 +000041 if( op==TK_SELECT ){
danielk1977bf3b7212004-05-18 10:06:24 +000042 return sqlite3ExprAffinity(pExpr->pSelect->pEList->a[0].pExpr);
danielk1977a37cdde2004-05-16 11:15:36 +000043 }
drh487e2622005-06-25 18:42:14 +000044#ifndef SQLITE_OMIT_CAST
45 if( op==TK_CAST ){
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/*
drh206f3d92006-07-11 13:15:08 +0000215** Works like sqlite3Expr() but frees its pLeft and pRight arguments
216** if it fails due to a malloc problem.
217*/
218Expr *sqlite3ExprOrFree(int op, Expr *pLeft, Expr *pRight, const Token *pToken){
219 Expr *pNew = sqlite3Expr(op, pLeft, pRight, pToken);
220 if( pNew==0 ){
221 sqlite3ExprDelete(pLeft);
222 sqlite3ExprDelete(pRight);
223 }
224 return pNew;
225}
226
227/*
drh4e0cff62004-11-05 05:10:28 +0000228** When doing a nested parse, you can include terms in an expression
229** that look like this: #0 #1 #2 ... These terms refer to elements
drh288d37f2005-06-22 08:48:06 +0000230** on the stack. "#0" means the top of the stack.
231** "#1" means the next down on the stack. And so forth.
drh4e0cff62004-11-05 05:10:28 +0000232**
233** This routine is called by the parser to deal with on of those terms.
234** It immediately generates code to store the value in a memory location.
235** The returns an expression that will code to extract the value from
236** that memory location as needed.
237*/
238Expr *sqlite3RegisterExpr(Parse *pParse, Token *pToken){
239 Vdbe *v = pParse->pVdbe;
240 Expr *p;
241 int depth;
drh4e0cff62004-11-05 05:10:28 +0000242 if( pParse->nested==0 ){
243 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", pToken);
244 return 0;
245 }
drhbb7ac002005-08-12 22:58:53 +0000246 if( v==0 ) return 0;
drh4e0cff62004-11-05 05:10:28 +0000247 p = sqlite3Expr(TK_REGISTER, 0, 0, pToken);
drh73c42a12004-11-20 18:13:10 +0000248 if( p==0 ){
249 return 0; /* Malloc failed */
250 }
drh2646da72005-12-09 20:02:05 +0000251 depth = atoi((char*)&pToken->z[1]);
drh288d37f2005-06-22 08:48:06 +0000252 p->iTable = pParse->nMem++;
253 sqlite3VdbeAddOp(v, OP_Dup, depth, 0);
254 sqlite3VdbeAddOp(v, OP_MemStore, p->iTable, 1);
drh4e0cff62004-11-05 05:10:28 +0000255 return p;
256}
257
258/*
drh91bb0ee2004-09-01 03:06:34 +0000259** Join two expressions using an AND operator. If either expression is
260** NULL, then just return the other expression.
261*/
262Expr *sqlite3ExprAnd(Expr *pLeft, Expr *pRight){
263 if( pLeft==0 ){
264 return pRight;
265 }else if( pRight==0 ){
266 return pLeft;
267 }else{
268 return sqlite3Expr(TK_AND, pLeft, pRight, 0);
269 }
270}
271
272/*
drh6977fea2002-10-22 23:38:04 +0000273** Set the Expr.span field of the given expression to span all
drha76b5df2002-02-23 02:32:10 +0000274** text between the two given tokens.
275*/
danielk19774adee202004-05-08 08:23:19 +0000276void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
drh4efc4752004-01-16 15:55:37 +0000277 assert( pRight!=0 );
278 assert( pLeft!=0 );
danielk19779e128002006-01-18 16:51:35 +0000279 if( !sqlite3MallocFailed() && pRight->z && pLeft->z ){
drhad6d9462004-09-19 02:15:24 +0000280 assert( pLeft->dyn==0 || pLeft->z[pLeft->n]==0 );
drh145716b2004-09-24 12:24:06 +0000281 if( pLeft->dyn==0 && pRight->dyn==0 ){
drh6977fea2002-10-22 23:38:04 +0000282 pExpr->span.z = pLeft->z;
drh97903fe2005-05-24 20:19:57 +0000283 pExpr->span.n = pRight->n + (pRight->z - pLeft->z);
drh4b59ab52002-08-24 18:24:51 +0000284 }else{
drh6977fea2002-10-22 23:38:04 +0000285 pExpr->span.z = 0;
drh4b59ab52002-08-24 18:24:51 +0000286 }
drha76b5df2002-02-23 02:32:10 +0000287 }
288}
289
290/*
291** Construct a new expression node for a function with multiple
292** arguments.
293*/
danielk19774adee202004-05-08 08:23:19 +0000294Expr *sqlite3ExprFunction(ExprList *pList, Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000295 Expr *pNew;
danielk19774b202ae2006-01-23 05:50:58 +0000296 assert( pToken );
drha76b5df2002-02-23 02:32:10 +0000297 pNew = sqliteMalloc( sizeof(Expr) );
298 if( pNew==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000299 sqlite3ExprListDelete(pList); /* Avoid leaking memory when malloc fails */
drha76b5df2002-02-23 02:32:10 +0000300 return 0;
301 }
302 pNew->op = TK_FUNCTION;
303 pNew->pList = pList;
danielk19774b202ae2006-01-23 05:50:58 +0000304 assert( pToken->dyn==0 );
305 pNew->token = *pToken;
drh6977fea2002-10-22 23:38:04 +0000306 pNew->span = pNew->token;
drha76b5df2002-02-23 02:32:10 +0000307 return pNew;
308}
309
310/*
drhfa6bc002004-09-07 16:19:52 +0000311** Assign a variable number to an expression that encodes a wildcard
312** in the original SQL statement.
313**
314** Wildcards consisting of a single "?" are assigned the next sequential
315** variable number.
316**
317** Wildcards of the form "?nnn" are assigned the number "nnn". We make
318** sure "nnn" is not too be to avoid a denial of service attack when
319** the SQL statement comes from an external source.
320**
321** Wildcards of the form ":aaa" or "$aaa" are assigned the same number
322** as the previous instance of the same wildcard. Or if this is the first
323** instance of the wildcard, the next sequenial variable number is
324** assigned.
325*/
326void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){
327 Token *pToken;
328 if( pExpr==0 ) return;
329 pToken = &pExpr->token;
330 assert( pToken->n>=1 );
331 assert( pToken->z!=0 );
332 assert( pToken->z[0]!=0 );
333 if( pToken->n==1 ){
334 /* Wildcard of the form "?". Assign the next variable number */
335 pExpr->iTable = ++pParse->nVar;
336 }else if( pToken->z[0]=='?' ){
337 /* Wildcard of the form "?nnn". Convert "nnn" to an integer and
338 ** use it as the variable number */
339 int i;
drh2646da72005-12-09 20:02:05 +0000340 pExpr->iTable = i = atoi((char*)&pToken->z[1]);
drhfa6bc002004-09-07 16:19:52 +0000341 if( i<1 || i>SQLITE_MAX_VARIABLE_NUMBER ){
342 sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
343 SQLITE_MAX_VARIABLE_NUMBER);
344 }
345 if( i>pParse->nVar ){
346 pParse->nVar = i;
347 }
348 }else{
349 /* Wildcards of the form ":aaa" or "$aaa". Reuse the same variable
350 ** number as the prior appearance of the same name, or if the name
351 ** has never appeared before, reuse the same variable number
352 */
353 int i, n;
354 n = pToken->n;
355 for(i=0; i<pParse->nVarExpr; i++){
356 Expr *pE;
357 if( (pE = pParse->apVarExpr[i])!=0
358 && pE->token.n==n
359 && memcmp(pE->token.z, pToken->z, n)==0 ){
360 pExpr->iTable = pE->iTable;
361 break;
362 }
363 }
364 if( i>=pParse->nVarExpr ){
365 pExpr->iTable = ++pParse->nVar;
366 if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){
367 pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10;
danielk1977e7259292006-01-13 06:33:23 +0000368 sqliteReallocOrFree((void**)&pParse->apVarExpr,
drhfa6bc002004-09-07 16:19:52 +0000369 pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0]) );
370 }
danielk19779e128002006-01-18 16:51:35 +0000371 if( !sqlite3MallocFailed() ){
drhfa6bc002004-09-07 16:19:52 +0000372 assert( pParse->apVarExpr!=0 );
373 pParse->apVarExpr[pParse->nVarExpr++] = pExpr;
374 }
375 }
376 }
377}
378
379/*
drha2e00042002-01-22 03:13:42 +0000380** Recursively delete an expression tree.
381*/
danielk19774adee202004-05-08 08:23:19 +0000382void sqlite3ExprDelete(Expr *p){
drha2e00042002-01-22 03:13:42 +0000383 if( p==0 ) return;
drh4efc4752004-01-16 15:55:37 +0000384 if( p->span.dyn ) sqliteFree((char*)p->span.z);
385 if( p->token.dyn ) sqliteFree((char*)p->token.z);
danielk19774adee202004-05-08 08:23:19 +0000386 sqlite3ExprDelete(p->pLeft);
387 sqlite3ExprDelete(p->pRight);
388 sqlite3ExprListDelete(p->pList);
389 sqlite3SelectDelete(p->pSelect);
drha2e00042002-01-22 03:13:42 +0000390 sqliteFree(p);
391}
392
drhd2687b72005-08-12 22:56:09 +0000393/*
394** The Expr.token field might be a string literal that is quoted.
395** If so, remove the quotation marks.
396*/
397void sqlite3DequoteExpr(Expr *p){
398 if( ExprHasAnyProperty(p, EP_Dequoted) ){
399 return;
400 }
401 ExprSetProperty(p, EP_Dequoted);
402 if( p->token.dyn==0 ){
drhd2687b72005-08-12 22:56:09 +0000403 sqlite3TokenCopy(&p->token, &p->token);
404 }
405 sqlite3Dequote((char*)p->token.z);
406}
407
drha76b5df2002-02-23 02:32:10 +0000408
409/*
drhff78bd22002-02-27 01:47:11 +0000410** The following group of routines make deep copies of expressions,
411** expression lists, ID lists, and select statements. The copies can
412** be deleted (by being passed to their respective ...Delete() routines)
413** without effecting the originals.
414**
danielk19774adee202004-05-08 08:23:19 +0000415** The expression list, ID, and source lists return by sqlite3ExprListDup(),
416** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
drhad3cab52002-05-24 02:04:32 +0000417** by subsequent calls to sqlite*ListAppend() routines.
drhff78bd22002-02-27 01:47:11 +0000418**
drhad3cab52002-05-24 02:04:32 +0000419** Any tables that the SrcList might point to are not duplicated.
drhff78bd22002-02-27 01:47:11 +0000420*/
danielk19774adee202004-05-08 08:23:19 +0000421Expr *sqlite3ExprDup(Expr *p){
drhff78bd22002-02-27 01:47:11 +0000422 Expr *pNew;
423 if( p==0 ) return 0;
drhfcb78a42003-01-18 20:11:05 +0000424 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000425 if( pNew==0 ) return 0;
drh3b167c72002-06-28 12:18:47 +0000426 memcpy(pNew, p, sizeof(*pNew));
drh6977fea2002-10-22 23:38:04 +0000427 if( p->token.z!=0 ){
drh2646da72005-12-09 20:02:05 +0000428 pNew->token.z = (u8*)sqliteStrNDup((char*)p->token.z, p->token.n);
drh4b59ab52002-08-24 18:24:51 +0000429 pNew->token.dyn = 1;
430 }else{
drh4efc4752004-01-16 15:55:37 +0000431 assert( pNew->token.z==0 );
drh4b59ab52002-08-24 18:24:51 +0000432 }
drh6977fea2002-10-22 23:38:04 +0000433 pNew->span.z = 0;
danielk19774adee202004-05-08 08:23:19 +0000434 pNew->pLeft = sqlite3ExprDup(p->pLeft);
435 pNew->pRight = sqlite3ExprDup(p->pRight);
436 pNew->pList = sqlite3ExprListDup(p->pList);
437 pNew->pSelect = sqlite3SelectDup(p->pSelect);
danielk1977aee18ef2005-03-09 12:26:50 +0000438 pNew->pTab = p->pTab;
drhff78bd22002-02-27 01:47:11 +0000439 return pNew;
440}
danielk19774adee202004-05-08 08:23:19 +0000441void sqlite3TokenCopy(Token *pTo, Token *pFrom){
drh4b59ab52002-08-24 18:24:51 +0000442 if( pTo->dyn ) sqliteFree((char*)pTo->z);
drh4b59ab52002-08-24 18:24:51 +0000443 if( pFrom->z ){
444 pTo->n = pFrom->n;
drh2646da72005-12-09 20:02:05 +0000445 pTo->z = (u8*)sqliteStrNDup((char*)pFrom->z, pFrom->n);
drh4b59ab52002-08-24 18:24:51 +0000446 pTo->dyn = 1;
447 }else{
drh4b59ab52002-08-24 18:24:51 +0000448 pTo->z = 0;
drh4b59ab52002-08-24 18:24:51 +0000449 }
450}
danielk19774adee202004-05-08 08:23:19 +0000451ExprList *sqlite3ExprListDup(ExprList *p){
drhff78bd22002-02-27 01:47:11 +0000452 ExprList *pNew;
drh145716b2004-09-24 12:24:06 +0000453 struct ExprList_item *pItem, *pOldItem;
drhff78bd22002-02-27 01:47:11 +0000454 int i;
455 if( p==0 ) return 0;
456 pNew = sqliteMalloc( sizeof(*pNew) );
457 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000458 pNew->nExpr = pNew->nAlloc = p->nExpr;
drh3e7bc9c2004-02-21 19:17:17 +0000459 pNew->a = pItem = sqliteMalloc( p->nExpr*sizeof(p->a[0]) );
danielk1977e0048402004-06-15 16:51:01 +0000460 if( pItem==0 ){
461 sqliteFree(pNew);
462 return 0;
463 }
drh145716b2004-09-24 12:24:06 +0000464 pOldItem = p->a;
465 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
drh4b59ab52002-08-24 18:24:51 +0000466 Expr *pNewExpr, *pOldExpr;
drh145716b2004-09-24 12:24:06 +0000467 pItem->pExpr = pNewExpr = sqlite3ExprDup(pOldExpr = pOldItem->pExpr);
drh6977fea2002-10-22 23:38:04 +0000468 if( pOldExpr->span.z!=0 && pNewExpr ){
469 /* Always make a copy of the span for top-level expressions in the
drh4b59ab52002-08-24 18:24:51 +0000470 ** expression list. The logic in SELECT processing that determines
471 ** the names of columns in the result set needs this information */
danielk19774adee202004-05-08 08:23:19 +0000472 sqlite3TokenCopy(&pNewExpr->span, &pOldExpr->span);
drh4b59ab52002-08-24 18:24:51 +0000473 }
drh1f3e9052002-10-31 00:09:39 +0000474 assert( pNewExpr==0 || pNewExpr->span.z!=0
drh6f7adc82006-01-11 21:41:20 +0000475 || pOldExpr->span.z==0
danielk19779e128002006-01-18 16:51:35 +0000476 || sqlite3MallocFailed() );
drh145716b2004-09-24 12:24:06 +0000477 pItem->zName = sqliteStrDup(pOldItem->zName);
478 pItem->sortOrder = pOldItem->sortOrder;
479 pItem->isAgg = pOldItem->isAgg;
drh3e7bc9c2004-02-21 19:17:17 +0000480 pItem->done = 0;
drhff78bd22002-02-27 01:47:11 +0000481 }
482 return pNew;
483}
danielk197793758c82005-01-21 08:13:14 +0000484
485/*
486** If cursors, triggers, views and subqueries are all omitted from
487** the build, then none of the following routines, except for
488** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes
489** called with a NULL argument.
490*/
danielk19776a67fe82005-02-04 04:07:16 +0000491#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \
492 || !defined(SQLITE_OMIT_SUBQUERY)
danielk19774adee202004-05-08 08:23:19 +0000493SrcList *sqlite3SrcListDup(SrcList *p){
drhad3cab52002-05-24 02:04:32 +0000494 SrcList *pNew;
495 int i;
drh113088e2003-03-20 01:16:58 +0000496 int nByte;
drhad3cab52002-05-24 02:04:32 +0000497 if( p==0 ) return 0;
drh113088e2003-03-20 01:16:58 +0000498 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
drh4efc4752004-01-16 15:55:37 +0000499 pNew = sqliteMallocRaw( nByte );
drhad3cab52002-05-24 02:04:32 +0000500 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000501 pNew->nSrc = pNew->nAlloc = p->nSrc;
drhad3cab52002-05-24 02:04:32 +0000502 for(i=0; i<p->nSrc; i++){
drh4efc4752004-01-16 15:55:37 +0000503 struct SrcList_item *pNewItem = &pNew->a[i];
504 struct SrcList_item *pOldItem = &p->a[i];
drhed8a3bb2005-06-06 21:19:56 +0000505 Table *pTab;
drh4efc4752004-01-16 15:55:37 +0000506 pNewItem->zDatabase = sqliteStrDup(pOldItem->zDatabase);
507 pNewItem->zName = sqliteStrDup(pOldItem->zName);
508 pNewItem->zAlias = sqliteStrDup(pOldItem->zAlias);
509 pNewItem->jointype = pOldItem->jointype;
510 pNewItem->iCursor = pOldItem->iCursor;
danielk19771787cca2006-02-10 07:07:14 +0000511 pNewItem->isPopulated = pOldItem->isPopulated;
drhed8a3bb2005-06-06 21:19:56 +0000512 pTab = pNewItem->pTab = pOldItem->pTab;
513 if( pTab ){
514 pTab->nRef++;
danielk1977a1cb1832005-02-12 08:59:55 +0000515 }
danielk19774adee202004-05-08 08:23:19 +0000516 pNewItem->pSelect = sqlite3SelectDup(pOldItem->pSelect);
517 pNewItem->pOn = sqlite3ExprDup(pOldItem->pOn);
518 pNewItem->pUsing = sqlite3IdListDup(pOldItem->pUsing);
danielk19776c18b6e2005-01-30 09:17:58 +0000519 pNewItem->colUsed = pOldItem->colUsed;
drhad3cab52002-05-24 02:04:32 +0000520 }
521 return pNew;
522}
danielk19774adee202004-05-08 08:23:19 +0000523IdList *sqlite3IdListDup(IdList *p){
drhff78bd22002-02-27 01:47:11 +0000524 IdList *pNew;
525 int i;
526 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000527 pNew = sqliteMallocRaw( sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +0000528 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000529 pNew->nId = pNew->nAlloc = p->nId;
drh4efc4752004-01-16 15:55:37 +0000530 pNew->a = sqliteMallocRaw( p->nId*sizeof(p->a[0]) );
danielk1977d5d56522005-03-16 12:15:20 +0000531 if( pNew->a==0 ){
532 sqliteFree(pNew);
533 return 0;
534 }
drhff78bd22002-02-27 01:47:11 +0000535 for(i=0; i<p->nId; i++){
drh4efc4752004-01-16 15:55:37 +0000536 struct IdList_item *pNewItem = &pNew->a[i];
537 struct IdList_item *pOldItem = &p->a[i];
538 pNewItem->zName = sqliteStrDup(pOldItem->zName);
539 pNewItem->idx = pOldItem->idx;
drhff78bd22002-02-27 01:47:11 +0000540 }
541 return pNew;
542}
danielk19774adee202004-05-08 08:23:19 +0000543Select *sqlite3SelectDup(Select *p){
drhff78bd22002-02-27 01:47:11 +0000544 Select *pNew;
545 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000546 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000547 if( pNew==0 ) return 0;
548 pNew->isDistinct = p->isDistinct;
danielk19774adee202004-05-08 08:23:19 +0000549 pNew->pEList = sqlite3ExprListDup(p->pEList);
550 pNew->pSrc = sqlite3SrcListDup(p->pSrc);
551 pNew->pWhere = sqlite3ExprDup(p->pWhere);
552 pNew->pGroupBy = sqlite3ExprListDup(p->pGroupBy);
553 pNew->pHaving = sqlite3ExprDup(p->pHaving);
554 pNew->pOrderBy = sqlite3ExprListDup(p->pOrderBy);
drhff78bd22002-02-27 01:47:11 +0000555 pNew->op = p->op;
danielk19774adee202004-05-08 08:23:19 +0000556 pNew->pPrior = sqlite3SelectDup(p->pPrior);
danielk1977a2dc3b12005-02-05 12:48:48 +0000557 pNew->pLimit = sqlite3ExprDup(p->pLimit);
558 pNew->pOffset = sqlite3ExprDup(p->pOffset);
drh7b58dae2003-07-20 01:16:46 +0000559 pNew->iLimit = -1;
560 pNew->iOffset = -1;
danielk1977a1cb1832005-02-12 08:59:55 +0000561 pNew->isResolved = p->isResolved;
562 pNew->isAgg = p->isAgg;
drhb9bb7c12006-06-11 23:41:55 +0000563 pNew->usesEphm = 0;
drh8e647b82005-09-23 21:11:53 +0000564 pNew->disallowOrderBy = 0;
drh0342b1f2005-09-01 03:07:44 +0000565 pNew->pRightmost = 0;
drhb9bb7c12006-06-11 23:41:55 +0000566 pNew->addrOpenEphm[0] = -1;
567 pNew->addrOpenEphm[1] = -1;
568 pNew->addrOpenEphm[2] = -1;
drhff78bd22002-02-27 01:47:11 +0000569 return pNew;
570}
danielk197793758c82005-01-21 08:13:14 +0000571#else
572Select *sqlite3SelectDup(Select *p){
573 assert( p==0 );
574 return 0;
575}
576#endif
drhff78bd22002-02-27 01:47:11 +0000577
578
579/*
drha76b5df2002-02-23 02:32:10 +0000580** Add a new element to the end of an expression list. If pList is
581** initially NULL, then create a new expression list.
582*/
danielk19774adee202004-05-08 08:23:19 +0000583ExprList *sqlite3ExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){
drha76b5df2002-02-23 02:32:10 +0000584 if( pList==0 ){
585 pList = sqliteMalloc( sizeof(ExprList) );
586 if( pList==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000587 goto no_mem;
drha76b5df2002-02-23 02:32:10 +0000588 }
drh4efc4752004-01-16 15:55:37 +0000589 assert( pList->nAlloc==0 );
drha76b5df2002-02-23 02:32:10 +0000590 }
drh4305d102003-07-30 12:34:12 +0000591 if( pList->nAlloc<=pList->nExpr ){
danielk1977d5d56522005-03-16 12:15:20 +0000592 struct ExprList_item *a;
593 int n = pList->nAlloc*2 + 4;
594 a = sqliteRealloc(pList->a, n*sizeof(pList->a[0]));
595 if( a==0 ){
596 goto no_mem;
drha76b5df2002-02-23 02:32:10 +0000597 }
danielk1977d5d56522005-03-16 12:15:20 +0000598 pList->a = a;
599 pList->nAlloc = n;
drha76b5df2002-02-23 02:32:10 +0000600 }
drh4efc4752004-01-16 15:55:37 +0000601 assert( pList->a!=0 );
602 if( pExpr || pName ){
603 struct ExprList_item *pItem = &pList->a[pList->nExpr++];
604 memset(pItem, 0, sizeof(*pItem));
drha99db3b2004-06-19 14:49:12 +0000605 pItem->zName = sqlite3NameFromToken(pName);
danielk1977e94ddc92005-03-21 03:53:38 +0000606 pItem->pExpr = pExpr;
drha76b5df2002-02-23 02:32:10 +0000607 }
608 return pList;
danielk1977d5d56522005-03-16 12:15:20 +0000609
610no_mem:
611 /* Avoid leaking memory if malloc has failed. */
612 sqlite3ExprDelete(pExpr);
613 sqlite3ExprListDelete(pList);
614 return 0;
drha76b5df2002-02-23 02:32:10 +0000615}
616
617/*
618** Delete an entire expression list.
619*/
danielk19774adee202004-05-08 08:23:19 +0000620void sqlite3ExprListDelete(ExprList *pList){
drha76b5df2002-02-23 02:32:10 +0000621 int i;
drhbe5c89a2004-07-26 00:31:09 +0000622 struct ExprList_item *pItem;
drha76b5df2002-02-23 02:32:10 +0000623 if( pList==0 ) return;
drh1bdd9b52004-04-23 17:04:44 +0000624 assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
625 assert( pList->nExpr<=pList->nAlloc );
drhbe5c89a2004-07-26 00:31:09 +0000626 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
627 sqlite3ExprDelete(pItem->pExpr);
628 sqliteFree(pItem->zName);
drha76b5df2002-02-23 02:32:10 +0000629 }
630 sqliteFree(pList->a);
631 sqliteFree(pList);
632}
633
634/*
drh626a8792005-01-17 22:08:19 +0000635** Walk an expression tree. Call xFunc for each node visited.
drh73b211a2005-01-18 04:00:42 +0000636**
drh626a8792005-01-17 22:08:19 +0000637** The return value from xFunc determines whether the tree walk continues.
638** 0 means continue walking the tree. 1 means do not walk children
639** of the current node but continue with siblings. 2 means abandon
640** the tree walk completely.
641**
642** The return value from this routine is 1 to abandon the tree walk
643** and 0 to continue.
drh87abf5c2005-08-25 12:45:04 +0000644**
645** NOTICE: This routine does *not* descend into subqueries.
drh626a8792005-01-17 22:08:19 +0000646*/
danielk1977a58fdfb2005-02-08 07:50:40 +0000647static int walkExprList(ExprList *, int (*)(void *, Expr*), void *);
drh626a8792005-01-17 22:08:19 +0000648static int walkExprTree(Expr *pExpr, int (*xFunc)(void*,Expr*), void *pArg){
drh626a8792005-01-17 22:08:19 +0000649 int rc;
650 if( pExpr==0 ) return 0;
651 rc = (*xFunc)(pArg, pExpr);
652 if( rc==0 ){
653 if( walkExprTree(pExpr->pLeft, xFunc, pArg) ) return 1;
654 if( walkExprTree(pExpr->pRight, xFunc, pArg) ) return 1;
danielk1977a58fdfb2005-02-08 07:50:40 +0000655 if( walkExprList(pExpr->pList, xFunc, pArg) ) return 1;
drh626a8792005-01-17 22:08:19 +0000656 }
657 return rc>1;
658}
659
660/*
danielk1977a58fdfb2005-02-08 07:50:40 +0000661** Call walkExprTree() for every expression in list p.
662*/
663static int walkExprList(ExprList *p, int (*xFunc)(void *, Expr*), void *pArg){
664 int i;
665 struct ExprList_item *pItem;
666 if( !p ) return 0;
667 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
668 if( walkExprTree(pItem->pExpr, xFunc, pArg) ) return 1;
669 }
670 return 0;
671}
672
673/*
674** Call walkExprTree() for every expression in Select p, not including
675** expressions that are part of sub-selects in any FROM clause or the LIMIT
676** or OFFSET expressions..
677*/
678static int walkSelectExpr(Select *p, int (*xFunc)(void *, Expr*), void *pArg){
679 walkExprList(p->pEList, xFunc, pArg);
680 walkExprTree(p->pWhere, xFunc, pArg);
681 walkExprList(p->pGroupBy, xFunc, pArg);
682 walkExprTree(p->pHaving, xFunc, pArg);
683 walkExprList(p->pOrderBy, xFunc, pArg);
684 return 0;
685}
686
687
688/*
drh626a8792005-01-17 22:08:19 +0000689** This routine is designed as an xFunc for walkExprTree().
690**
691** pArg is really a pointer to an integer. If we can tell by looking
drh73b211a2005-01-18 04:00:42 +0000692** at pExpr that the expression that contains pExpr is not a constant
693** expression, then set *pArg to 0 and return 2 to abandon the tree walk.
694** If pExpr does does not disqualify the expression from being a constant
695** then do nothing.
696**
697** After walking the whole tree, if no nodes are found that disqualify
698** the expression as constant, then we assume the whole expression
699** is constant. See sqlite3ExprIsConstant() for additional information.
drh626a8792005-01-17 22:08:19 +0000700*/
701static int exprNodeIsConstant(void *pArg, Expr *pExpr){
702 switch( pExpr->op ){
drheb55bd22005-06-30 17:04:21 +0000703 /* Consider functions to be constant if all their arguments are constant
704 ** and *pArg==2 */
705 case TK_FUNCTION:
706 if( *((int*)pArg)==2 ) return 0;
707 /* Fall through */
drh626a8792005-01-17 22:08:19 +0000708 case TK_ID:
709 case TK_COLUMN:
710 case TK_DOT:
711 case TK_AGG_FUNCTION:
drh13449892005-09-07 21:22:45 +0000712 case TK_AGG_COLUMN:
drhfe2093d2005-01-20 22:48:47 +0000713#ifndef SQLITE_OMIT_SUBQUERY
714 case TK_SELECT:
715 case TK_EXISTS:
716#endif
drh626a8792005-01-17 22:08:19 +0000717 *((int*)pArg) = 0;
718 return 2;
drh87abf5c2005-08-25 12:45:04 +0000719 case TK_IN:
720 if( pExpr->pSelect ){
721 *((int*)pArg) = 0;
722 return 2;
723 }
drh626a8792005-01-17 22:08:19 +0000724 default:
725 return 0;
726 }
727}
728
729/*
drhfef52082000-06-06 01:50:43 +0000730** Walk an expression tree. Return 1 if the expression is constant
drheb55bd22005-06-30 17:04:21 +0000731** and 0 if it involves variables or function calls.
drh23989372002-05-21 13:43:04 +0000732**
733** For the purposes of this function, a double-quoted string (ex: "abc")
734** is considered a variable but a single-quoted string (ex: 'abc') is
735** a constant.
drhfef52082000-06-06 01:50:43 +0000736*/
danielk19774adee202004-05-08 08:23:19 +0000737int sqlite3ExprIsConstant(Expr *p){
drh626a8792005-01-17 22:08:19 +0000738 int isConst = 1;
739 walkExprTree(p, exprNodeIsConstant, &isConst);
740 return isConst;
drhfef52082000-06-06 01:50:43 +0000741}
742
743/*
drheb55bd22005-06-30 17:04:21 +0000744** Walk an expression tree. Return 1 if the expression is constant
745** or a function call with constant arguments. Return and 0 if there
746** are any variables.
747**
748** For the purposes of this function, a double-quoted string (ex: "abc")
749** is considered a variable but a single-quoted string (ex: 'abc') is
750** a constant.
751*/
752int sqlite3ExprIsConstantOrFunction(Expr *p){
753 int isConst = 2;
754 walkExprTree(p, exprNodeIsConstant, &isConst);
755 return isConst!=0;
756}
757
758/*
drh73b211a2005-01-18 04:00:42 +0000759** If the expression p codes a constant integer that is small enough
drh202b2df2004-01-06 01:13:46 +0000760** to fit in a 32-bit integer, return 1 and put the value of the integer
761** in *pValue. If the expression is not an integer or if it is too big
762** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
drhe4de1fe2002-06-02 16:09:01 +0000763*/
danielk19774adee202004-05-08 08:23:19 +0000764int sqlite3ExprIsInteger(Expr *p, int *pValue){
drhe4de1fe2002-06-02 16:09:01 +0000765 switch( p->op ){
766 case TK_INTEGER: {
drh2646da72005-12-09 20:02:05 +0000767 if( sqlite3GetInt32((char*)p->token.z, pValue) ){
drh202b2df2004-01-06 01:13:46 +0000768 return 1;
769 }
770 break;
drhe4de1fe2002-06-02 16:09:01 +0000771 }
drh4b59ab52002-08-24 18:24:51 +0000772 case TK_UPLUS: {
danielk19774adee202004-05-08 08:23:19 +0000773 return sqlite3ExprIsInteger(p->pLeft, pValue);
drh4b59ab52002-08-24 18:24:51 +0000774 }
drhe4de1fe2002-06-02 16:09:01 +0000775 case TK_UMINUS: {
776 int v;
danielk19774adee202004-05-08 08:23:19 +0000777 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
drhe4de1fe2002-06-02 16:09:01 +0000778 *pValue = -v;
779 return 1;
780 }
781 break;
782 }
783 default: break;
784 }
785 return 0;
786}
787
788/*
drhc4a3c772001-04-04 11:48:57 +0000789** Return TRUE if the given string is a row-id column name.
790*/
danielk19774adee202004-05-08 08:23:19 +0000791int sqlite3IsRowid(const char *z){
792 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
793 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
794 if( sqlite3StrICmp(z, "OID")==0 ) return 1;
drhc4a3c772001-04-04 11:48:57 +0000795 return 0;
796}
797
798/*
drh8141f612004-01-25 22:44:58 +0000799** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
800** that name in the set of source tables in pSrcList and make the pExpr
801** expression node refer back to that source column. The following changes
802** are made to pExpr:
803**
804** pExpr->iDb Set the index in db->aDb[] of the database holding
805** the table.
806** pExpr->iTable Set to the cursor number for the table obtained
807** from pSrcList.
808** pExpr->iColumn Set to the column number within the table.
drh8141f612004-01-25 22:44:58 +0000809** pExpr->op Set to TK_COLUMN.
810** pExpr->pLeft Any expression this points to is deleted
811** pExpr->pRight Any expression this points to is deleted.
812**
813** The pDbToken is the name of the database (the "X"). This value may be
814** NULL meaning that name is of the form Y.Z or Z. Any available database
815** can be used. The pTableToken is the name of the table (the "Y"). This
816** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it
817** means that the form of the name is Z and that columns from any table
818** can be used.
819**
820** If the name cannot be resolved unambiguously, leave an error message
821** in pParse and return non-zero. Return zero on success.
822*/
823static int lookupName(
drhffe07b22005-11-03 00:41:17 +0000824 Parse *pParse, /* The parsing context */
drh8141f612004-01-25 22:44:58 +0000825 Token *pDbToken, /* Name of the database containing table, or NULL */
826 Token *pTableToken, /* Name of table containing column, or NULL */
827 Token *pColumnToken, /* Name of the column. */
drh626a8792005-01-17 22:08:19 +0000828 NameContext *pNC, /* The name context used to resolve the name */
drh8141f612004-01-25 22:44:58 +0000829 Expr *pExpr /* Make this EXPR node point to the selected column */
830){
831 char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */
832 char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */
833 char *zCol = 0; /* Name of the column. The "Z" */
834 int i, j; /* Loop counters */
835 int cnt = 0; /* Number of matching column names */
836 int cntTab = 0; /* Number of matching table names */
drh626a8792005-01-17 22:08:19 +0000837 sqlite3 *db = pParse->db; /* The database */
drh51669862004-12-18 18:40:26 +0000838 struct SrcList_item *pItem; /* Use for looping over pSrcList items */
839 struct SrcList_item *pMatch = 0; /* The matching pSrcList item */
drh73b211a2005-01-18 04:00:42 +0000840 NameContext *pTopNC = pNC; /* First namecontext in the list */
drh8141f612004-01-25 22:44:58 +0000841
842 assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
drha99db3b2004-06-19 14:49:12 +0000843 zDb = sqlite3NameFromToken(pDbToken);
844 zTab = sqlite3NameFromToken(pTableToken);
845 zCol = sqlite3NameFromToken(pColumnToken);
danielk19779e128002006-01-18 16:51:35 +0000846 if( sqlite3MallocFailed() ){
danielk1977d5d56522005-03-16 12:15:20 +0000847 goto lookupname_end;
drh8141f612004-01-25 22:44:58 +0000848 }
drh8141f612004-01-25 22:44:58 +0000849
850 pExpr->iTable = -1;
drh626a8792005-01-17 22:08:19 +0000851 while( pNC && cnt==0 ){
drhffe07b22005-11-03 00:41:17 +0000852 ExprList *pEList;
drh626a8792005-01-17 22:08:19 +0000853 SrcList *pSrcList = pNC->pSrcList;
drh8141f612004-01-25 22:44:58 +0000854
danielk1977b3bce662005-01-29 08:32:43 +0000855 if( pSrcList ){
856 for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
drh43617e92006-03-06 20:55:46 +0000857 Table *pTab;
858 int iDb;
danielk1977b3bce662005-01-29 08:32:43 +0000859 Column *pCol;
860
drh43617e92006-03-06 20:55:46 +0000861 pTab = pItem->pTab;
862 assert( pTab!=0 );
863 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
danielk1977b3bce662005-01-29 08:32:43 +0000864 assert( pTab->nCol>0 );
865 if( zTab ){
866 if( pItem->zAlias ){
867 char *zTabName = pItem->zAlias;
868 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
869 }else{
870 char *zTabName = pTab->zName;
871 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
danielk1977da184232006-01-05 11:34:32 +0000872 if( zDb!=0 && sqlite3StrICmp(db->aDb[iDb].zName, zDb)!=0 ){
danielk1977b3bce662005-01-29 08:32:43 +0000873 continue;
874 }
drh626a8792005-01-17 22:08:19 +0000875 }
drh8141f612004-01-25 22:44:58 +0000876 }
danielk1977b3bce662005-01-29 08:32:43 +0000877 if( 0==(cntTab++) ){
drh626a8792005-01-17 22:08:19 +0000878 pExpr->iTable = pItem->iCursor;
danielk1977da184232006-01-05 11:34:32 +0000879 pExpr->pSchema = pTab->pSchema;
danielk1977b3bce662005-01-29 08:32:43 +0000880 pMatch = pItem;
881 }
882 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
883 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
danielk1977b3bf5562006-01-10 17:58:23 +0000884 const char *zColl = pTab->aCol[j].zColl;
drh873fac02005-06-06 17:11:46 +0000885 IdList *pUsing;
danielk1977b3bce662005-01-29 08:32:43 +0000886 cnt++;
887 pExpr->iTable = pItem->iCursor;
888 pMatch = pItem;
danielk1977da184232006-01-05 11:34:32 +0000889 pExpr->pSchema = pTab->pSchema;
danielk1977b3bce662005-01-29 08:32:43 +0000890 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
891 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
892 pExpr->affinity = pTab->aCol[j].affinity;
danielk1977b3bf5562006-01-10 17:58:23 +0000893 pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0);
drh355ef362005-06-06 16:59:24 +0000894 if( pItem->jointype & JT_NATURAL ){
895 /* If this match occurred in the left table of a natural join,
896 ** then skip the right table to avoid a duplicate match */
897 pItem++;
898 i++;
899 }
drh873fac02005-06-06 17:11:46 +0000900 if( (pUsing = pItem->pUsing)!=0 ){
901 /* If this match occurs on a column that is in the USING clause
902 ** of a join, skip the search of the right table of the join
903 ** to avoid a duplicate match there. */
904 int k;
905 for(k=0; k<pUsing->nId; k++){
906 if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ){
907 pItem++;
908 i++;
909 break;
910 }
911 }
912 }
danielk1977b3bce662005-01-29 08:32:43 +0000913 break;
914 }
drh8141f612004-01-25 22:44:58 +0000915 }
916 }
917 }
drh626a8792005-01-17 22:08:19 +0000918
919#ifndef SQLITE_OMIT_TRIGGER
920 /* If we have not already resolved the name, then maybe
921 ** it is a new.* or old.* trigger argument reference
922 */
923 if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
924 TriggerStack *pTriggerStack = pParse->trigStack;
925 Table *pTab = 0;
926 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){
927 pExpr->iTable = pTriggerStack->newIdx;
928 assert( pTriggerStack->pTab );
929 pTab = pTriggerStack->pTab;
930 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab)==0 ){
931 pExpr->iTable = pTriggerStack->oldIdx;
932 assert( pTriggerStack->pTab );
933 pTab = pTriggerStack->pTab;
934 }
935
936 if( pTab ){
danielk1977f0113002006-01-24 12:09:17 +0000937 int iCol;
drh626a8792005-01-17 22:08:19 +0000938 Column *pCol = pTab->aCol;
939
danielk1977da184232006-01-05 11:34:32 +0000940 pExpr->pSchema = pTab->pSchema;
drh626a8792005-01-17 22:08:19 +0000941 cntTab++;
danielk1977f0113002006-01-24 12:09:17 +0000942 for(iCol=0; iCol < pTab->nCol; iCol++, pCol++) {
drh626a8792005-01-17 22:08:19 +0000943 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
danielk1977f0113002006-01-24 12:09:17 +0000944 const char *zColl = pTab->aCol[iCol].zColl;
drh626a8792005-01-17 22:08:19 +0000945 cnt++;
danielk1977f0113002006-01-24 12:09:17 +0000946 pExpr->iColumn = iCol==pTab->iPKey ? -1 : iCol;
947 pExpr->affinity = pTab->aCol[iCol].affinity;
danielk1977b3bf5562006-01-10 17:58:23 +0000948 pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0);
danielk1977aee18ef2005-03-09 12:26:50 +0000949 pExpr->pTab = pTab;
drh626a8792005-01-17 22:08:19 +0000950 break;
951 }
952 }
953 }
954 }
drhb7f91642004-10-31 02:22:47 +0000955#endif /* !defined(SQLITE_OMIT_TRIGGER) */
drh8141f612004-01-25 22:44:58 +0000956
drh626a8792005-01-17 22:08:19 +0000957 /*
958 ** Perhaps the name is a reference to the ROWID
959 */
960 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
961 cnt = 1;
962 pExpr->iColumn = -1;
drh8a512562005-11-14 22:29:05 +0000963 pExpr->affinity = SQLITE_AFF_INTEGER;
drh626a8792005-01-17 22:08:19 +0000964 }
drh8141f612004-01-25 22:44:58 +0000965
drh626a8792005-01-17 22:08:19 +0000966 /*
967 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
968 ** might refer to an result-set alias. This happens, for example, when
969 ** we are resolving names in the WHERE clause of the following command:
970 **
971 ** SELECT a+b AS x FROM table WHERE x<10;
972 **
973 ** In cases like this, replace pExpr with a copy of the expression that
974 ** forms the result set entry ("a+b" in the example) and return immediately.
975 ** Note that the expression in the result set should have already been
976 ** resolved by the time the WHERE clause is resolved.
977 */
drhffe07b22005-11-03 00:41:17 +0000978 if( cnt==0 && (pEList = pNC->pEList)!=0 && zTab==0 ){
drh626a8792005-01-17 22:08:19 +0000979 for(j=0; j<pEList->nExpr; j++){
980 char *zAs = pEList->a[j].zName;
981 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
982 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
983 pExpr->op = TK_AS;
984 pExpr->iColumn = j;
985 pExpr->pLeft = sqlite3ExprDup(pEList->a[j].pExpr);
drh15ccce12005-05-23 15:06:39 +0000986 cnt = 1;
drh626a8792005-01-17 22:08:19 +0000987 assert( zTab==0 && zDb==0 );
drh15ccce12005-05-23 15:06:39 +0000988 goto lookupname_end_2;
drh626a8792005-01-17 22:08:19 +0000989 }
990 }
991 }
992
993 /* Advance to the next name context. The loop will exit when either
994 ** we have a match (cnt>0) or when we run out of name contexts.
995 */
996 if( cnt==0 ){
997 pNC = pNC->pNext;
998 }
drh8141f612004-01-25 22:44:58 +0000999 }
1000
1001 /*
1002 ** If X and Y are NULL (in other words if only the column name Z is
1003 ** supplied) and the value of Z is enclosed in double-quotes, then
1004 ** Z is a string literal if it doesn't match any column names. In that
1005 ** case, we need to return right away and not make any changes to
1006 ** pExpr.
drh15ccce12005-05-23 15:06:39 +00001007 **
1008 ** Because no reference was made to outer contexts, the pNC->nRef
1009 ** fields are not changed in any context.
drh8141f612004-01-25 22:44:58 +00001010 */
1011 if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
1012 sqliteFree(zCol);
1013 return 0;
1014 }
1015
1016 /*
1017 ** cnt==0 means there was not match. cnt>1 means there were two or
1018 ** more matches. Either way, we have an error.
1019 */
1020 if( cnt!=1 ){
1021 char *z = 0;
1022 char *zErr;
1023 zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s";
1024 if( zDb ){
drhf93339d2006-01-03 15:16:26 +00001025 sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, (char*)0);
drh8141f612004-01-25 22:44:58 +00001026 }else if( zTab ){
drhf93339d2006-01-03 15:16:26 +00001027 sqlite3SetString(&z, zTab, ".", zCol, (char*)0);
drh8141f612004-01-25 22:44:58 +00001028 }else{
1029 z = sqliteStrDup(zCol);
1030 }
danielk19774adee202004-05-08 08:23:19 +00001031 sqlite3ErrorMsg(pParse, zErr, z);
drh8141f612004-01-25 22:44:58 +00001032 sqliteFree(z);
drh73b211a2005-01-18 04:00:42 +00001033 pTopNC->nErr++;
drh8141f612004-01-25 22:44:58 +00001034 }
1035
drh51669862004-12-18 18:40:26 +00001036 /* If a column from a table in pSrcList is referenced, then record
1037 ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes
1038 ** bit 0 to be set. Column 1 sets bit 1. And so forth. If the
1039 ** column number is greater than the number of bits in the bitmask
1040 ** then set the high-order bit of the bitmask.
1041 */
1042 if( pExpr->iColumn>=0 && pMatch!=0 ){
1043 int n = pExpr->iColumn;
1044 if( n>=sizeof(Bitmask)*8 ){
1045 n = sizeof(Bitmask)*8-1;
1046 }
1047 assert( pMatch->iCursor==pExpr->iTable );
1048 pMatch->colUsed |= 1<<n;
1049 }
1050
danielk1977d5d56522005-03-16 12:15:20 +00001051lookupname_end:
drh8141f612004-01-25 22:44:58 +00001052 /* Clean up and return
1053 */
1054 sqliteFree(zDb);
1055 sqliteFree(zTab);
danielk19774adee202004-05-08 08:23:19 +00001056 sqlite3ExprDelete(pExpr->pLeft);
drh8141f612004-01-25 22:44:58 +00001057 pExpr->pLeft = 0;
danielk19774adee202004-05-08 08:23:19 +00001058 sqlite3ExprDelete(pExpr->pRight);
drh8141f612004-01-25 22:44:58 +00001059 pExpr->pRight = 0;
1060 pExpr->op = TK_COLUMN;
drh15ccce12005-05-23 15:06:39 +00001061lookupname_end_2:
1062 sqliteFree(zCol);
drh626a8792005-01-17 22:08:19 +00001063 if( cnt==1 ){
danielk1977b3bce662005-01-29 08:32:43 +00001064 assert( pNC!=0 );
drh626a8792005-01-17 22:08:19 +00001065 sqlite3AuthRead(pParse, pExpr, pNC->pSrcList);
danielk1977aee18ef2005-03-09 12:26:50 +00001066 if( pMatch && !pMatch->pSelect ){
1067 pExpr->pTab = pMatch->pTab;
1068 }
drh15ccce12005-05-23 15:06:39 +00001069 /* Increment the nRef value on all name contexts from TopNC up to
1070 ** the point where the name matched. */
1071 for(;;){
1072 assert( pTopNC!=0 );
1073 pTopNC->nRef++;
1074 if( pTopNC==pNC ) break;
1075 pTopNC = pTopNC->pNext;
1076 }
1077 return 0;
1078 } else {
1079 return 1;
drh626a8792005-01-17 22:08:19 +00001080 }
drh8141f612004-01-25 22:44:58 +00001081}
1082
1083/*
drh626a8792005-01-17 22:08:19 +00001084** This routine is designed as an xFunc for walkExprTree().
1085**
drh73b211a2005-01-18 04:00:42 +00001086** Resolve symbolic names into TK_COLUMN operators for the current
drh626a8792005-01-17 22:08:19 +00001087** node in the expression tree. Return 0 to continue the search down
drh73b211a2005-01-18 04:00:42 +00001088** the tree or 2 to abort the tree walk.
1089**
1090** This routine also does error checking and name resolution for
1091** function names. The operator for aggregate functions is changed
1092** to TK_AGG_FUNCTION.
drh626a8792005-01-17 22:08:19 +00001093*/
1094static int nameResolverStep(void *pArg, Expr *pExpr){
1095 NameContext *pNC = (NameContext*)pArg;
drh626a8792005-01-17 22:08:19 +00001096 Parse *pParse;
drh626a8792005-01-17 22:08:19 +00001097
danielk1977b3bce662005-01-29 08:32:43 +00001098 if( pExpr==0 ) return 1;
drh626a8792005-01-17 22:08:19 +00001099 assert( pNC!=0 );
drh626a8792005-01-17 22:08:19 +00001100 pParse = pNC->pParse;
danielk1977b3bce662005-01-29 08:32:43 +00001101
drh626a8792005-01-17 22:08:19 +00001102 if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return 1;
1103 ExprSetProperty(pExpr, EP_Resolved);
1104#ifndef NDEBUG
danielk1977f0113002006-01-24 12:09:17 +00001105 if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
1106 SrcList *pSrcList = pNC->pSrcList;
danielk1977940fac92005-01-23 22:41:37 +00001107 int i;
danielk1977f0113002006-01-24 12:09:17 +00001108 for(i=0; i<pNC->pSrcList->nSrc; i++){
drh626a8792005-01-17 22:08:19 +00001109 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
1110 }
1111 }
1112#endif
1113 switch( pExpr->op ){
1114 /* Double-quoted strings (ex: "abc") are used as identifiers if
1115 ** possible. Otherwise they remain as strings. Single-quoted
1116 ** strings (ex: 'abc') are always string literals.
1117 */
1118 case TK_STRING: {
1119 if( pExpr->token.z[0]=='\'' ) break;
1120 /* Fall thru into the TK_ID case if this is a double-quoted string */
1121 }
1122 /* A lone identifier is the name of a column.
1123 */
1124 case TK_ID: {
drh626a8792005-01-17 22:08:19 +00001125 lookupName(pParse, 0, 0, &pExpr->token, pNC, pExpr);
1126 return 1;
1127 }
1128
1129 /* A table name and column name: ID.ID
1130 ** Or a database, table and column: ID.ID.ID
1131 */
1132 case TK_DOT: {
1133 Token *pColumn;
1134 Token *pTable;
1135 Token *pDb;
1136 Expr *pRight;
1137
danielk1977b3bce662005-01-29 08:32:43 +00001138 /* if( pSrcList==0 ) break; */
drh626a8792005-01-17 22:08:19 +00001139 pRight = pExpr->pRight;
1140 if( pRight->op==TK_ID ){
1141 pDb = 0;
1142 pTable = &pExpr->pLeft->token;
1143 pColumn = &pRight->token;
1144 }else{
1145 assert( pRight->op==TK_DOT );
1146 pDb = &pExpr->pLeft->token;
1147 pTable = &pRight->pLeft->token;
1148 pColumn = &pRight->pRight->token;
1149 }
1150 lookupName(pParse, pDb, pTable, pColumn, pNC, pExpr);
1151 return 1;
1152 }
1153
1154 /* Resolve function names
1155 */
drhb71090f2005-05-23 17:26:51 +00001156 case TK_CONST_FUNC:
drh626a8792005-01-17 22:08:19 +00001157 case TK_FUNCTION: {
1158 ExprList *pList = pExpr->pList; /* The argument list */
1159 int n = pList ? pList->nExpr : 0; /* Number of arguments */
1160 int no_such_func = 0; /* True if no such function exists */
1161 int wrong_num_args = 0; /* True if wrong number of arguments */
1162 int is_agg = 0; /* True if is an aggregate function */
1163 int i;
1164 int nId; /* Number of characters in function name */
1165 const char *zId; /* The function name. */
drh73b211a2005-01-18 04:00:42 +00001166 FuncDef *pDef; /* Information about the function */
danielk197714db2662006-01-09 16:12:04 +00001167 int enc = ENC(pParse->db); /* The database encoding */
drh626a8792005-01-17 22:08:19 +00001168
drh2646da72005-12-09 20:02:05 +00001169 zId = (char*)pExpr->token.z;
drhb71090f2005-05-23 17:26:51 +00001170 nId = pExpr->token.n;
drh626a8792005-01-17 22:08:19 +00001171 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
1172 if( pDef==0 ){
1173 pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
1174 if( pDef==0 ){
1175 no_such_func = 1;
1176 }else{
1177 wrong_num_args = 1;
1178 }
1179 }else{
1180 is_agg = pDef->xFunc==0;
1181 }
1182 if( is_agg && !pNC->allowAgg ){
1183 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
1184 pNC->nErr++;
1185 is_agg = 0;
1186 }else if( no_such_func ){
1187 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
1188 pNC->nErr++;
1189 }else if( wrong_num_args ){
1190 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
1191 nId, zId);
1192 pNC->nErr++;
1193 }
1194 if( is_agg ){
1195 pExpr->op = TK_AGG_FUNCTION;
1196 pNC->hasAgg = 1;
1197 }
drh73b211a2005-01-18 04:00:42 +00001198 if( is_agg ) pNC->allowAgg = 0;
drh626a8792005-01-17 22:08:19 +00001199 for(i=0; pNC->nErr==0 && i<n; i++){
drh73b211a2005-01-18 04:00:42 +00001200 walkExprTree(pList->a[i].pExpr, nameResolverStep, pNC);
drh626a8792005-01-17 22:08:19 +00001201 }
drh73b211a2005-01-18 04:00:42 +00001202 if( is_agg ) pNC->allowAgg = 1;
drh626a8792005-01-17 22:08:19 +00001203 /* FIX ME: Compute pExpr->affinity based on the expected return
1204 ** type of the function
1205 */
1206 return is_agg;
1207 }
danielk1977b3bce662005-01-29 08:32:43 +00001208#ifndef SQLITE_OMIT_SUBQUERY
1209 case TK_SELECT:
1210 case TK_EXISTS:
1211#endif
1212 case TK_IN: {
1213 if( pExpr->pSelect ){
drh8a9f38f2005-11-05 15:07:55 +00001214 int nRef = pNC->nRef;
drh06f65412005-11-03 02:03:13 +00001215#ifndef SQLITE_OMIT_CHECK
1216 if( pNC->isCheck ){
1217 sqlite3ErrorMsg(pParse,"subqueries prohibited in CHECK constraints");
1218 }
1219#endif
danielk1977b3bce662005-01-29 08:32:43 +00001220 sqlite3SelectResolve(pParse, pExpr->pSelect, pNC);
1221 assert( pNC->nRef>=nRef );
1222 if( nRef!=pNC->nRef ){
1223 ExprSetProperty(pExpr, EP_VarSelect);
1224 }
1225 }
drh4284fb02005-11-03 12:33:28 +00001226 break;
danielk1977b3bce662005-01-29 08:32:43 +00001227 }
drh4284fb02005-11-03 12:33:28 +00001228#ifndef SQLITE_OMIT_CHECK
1229 case TK_VARIABLE: {
1230 if( pNC->isCheck ){
1231 sqlite3ErrorMsg(pParse,"parameters prohibited in CHECK constraints");
1232 }
1233 break;
1234 }
1235#endif
drh626a8792005-01-17 22:08:19 +00001236 }
1237 return 0;
1238}
1239
1240/*
drhcce7d172000-05-31 15:34:51 +00001241** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +00001242** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +00001243** index to the table in the table list and a column offset. The
1244** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
1245** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +00001246** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +00001247** VDBE cursor number for a cursor that is pointing into the referenced
1248** table. The Expr.iColumn value is changed to the index of the column
1249** of the referenced table. The Expr.iColumn value for the special
1250** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
1251** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +00001252**
drh626a8792005-01-17 22:08:19 +00001253** Also resolve function names and check the functions for proper
1254** usage. Make sure all function names are recognized and all functions
1255** have the correct number of arguments. Leave an error message
1256** in pParse->zErrMsg if anything is amiss. Return the number of errors.
1257**
drh73b211a2005-01-18 04:00:42 +00001258** If the expression contains aggregate functions then set the EP_Agg
1259** property on the expression.
drh626a8792005-01-17 22:08:19 +00001260*/
1261int sqlite3ExprResolveNames(
danielk1977b3bce662005-01-29 08:32:43 +00001262 NameContext *pNC, /* Namespace to resolve expressions in. */
1263 Expr *pExpr /* The expression to be analyzed. */
drh626a8792005-01-17 22:08:19 +00001264){
drh13449892005-09-07 21:22:45 +00001265 int savedHasAgg;
drh73b211a2005-01-18 04:00:42 +00001266 if( pExpr==0 ) return 0;
drh13449892005-09-07 21:22:45 +00001267 savedHasAgg = pNC->hasAgg;
1268 pNC->hasAgg = 0;
danielk1977b3bce662005-01-29 08:32:43 +00001269 walkExprTree(pExpr, nameResolverStep, pNC);
1270 if( pNC->nErr>0 ){
drh73b211a2005-01-18 04:00:42 +00001271 ExprSetProperty(pExpr, EP_Error);
drh73b211a2005-01-18 04:00:42 +00001272 }
drh13449892005-09-07 21:22:45 +00001273 if( pNC->hasAgg ){
1274 ExprSetProperty(pExpr, EP_Agg);
1275 }else if( savedHasAgg ){
1276 pNC->hasAgg = 1;
1277 }
drh73b211a2005-01-18 04:00:42 +00001278 return ExprHasProperty(pExpr, EP_Error);
drh626a8792005-01-17 22:08:19 +00001279}
1280
drh1398ad32005-01-19 23:24:50 +00001281/*
1282** A pointer instance of this structure is used to pass information
1283** through walkExprTree into codeSubqueryStep().
1284*/
1285typedef struct QueryCoder QueryCoder;
1286struct QueryCoder {
1287 Parse *pParse; /* The parsing context */
1288 NameContext *pNC; /* Namespace of first enclosing query */
1289};
1290
drh626a8792005-01-17 22:08:19 +00001291
1292/*
drh9cbe6352005-11-29 03:13:21 +00001293** Generate code for scalar subqueries used as an expression
1294** and IN operators. Examples:
drh626a8792005-01-17 22:08:19 +00001295**
drh9cbe6352005-11-29 03:13:21 +00001296** (SELECT a FROM b) -- subquery
1297** EXISTS (SELECT a FROM b) -- EXISTS subquery
1298** x IN (4,5,11) -- IN operator with list on right-hand side
1299** x IN (SELECT a FROM b) -- IN operator with subquery on the right
drhfef52082000-06-06 01:50:43 +00001300**
drh9cbe6352005-11-29 03:13:21 +00001301** The pExpr parameter describes the expression that contains the IN
1302** operator or subquery.
drhcce7d172000-05-31 15:34:51 +00001303*/
drh51522cd2005-01-20 13:36:19 +00001304#ifndef SQLITE_OMIT_SUBQUERY
danielk1977b3bce662005-01-29 08:32:43 +00001305void sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){
drh57dbd7b2005-07-08 18:25:26 +00001306 int testAddr = 0; /* One-time test address */
danielk1977b3bce662005-01-29 08:32:43 +00001307 Vdbe *v = sqlite3GetVdbe(pParse);
1308 if( v==0 ) return;
1309
drh57dbd7b2005-07-08 18:25:26 +00001310 /* This code must be run in its entirety every time it is encountered
1311 ** if any of the following is true:
1312 **
1313 ** * The right-hand side is a correlated subquery
1314 ** * The right-hand side is an expression list containing variables
1315 ** * We are inside a trigger
1316 **
1317 ** If all of the above are false, then we can run this code just once
1318 ** save the results, and reuse the same result on subsequent invocations.
danielk1977b3bce662005-01-29 08:32:43 +00001319 */
1320 if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->trigStack ){
1321 int mem = pParse->nMem++;
1322 sqlite3VdbeAddOp(v, OP_MemLoad, mem, 0);
drh57dbd7b2005-07-08 18:25:26 +00001323 testAddr = sqlite3VdbeAddOp(v, OP_If, 0, 0);
danielk19779e128002006-01-18 16:51:35 +00001324 assert( testAddr>0 || sqlite3MallocFailed() );
drhd654be82005-09-20 17:42:23 +00001325 sqlite3VdbeAddOp(v, OP_MemInt, 1, mem);
danielk1977b3bce662005-01-29 08:32:43 +00001326 }
1327
drhcce7d172000-05-31 15:34:51 +00001328 switch( pExpr->op ){
drhfef52082000-06-06 01:50:43 +00001329 case TK_IN: {
danielk1977e014a832004-05-17 10:48:57 +00001330 char affinity;
drhd3d39e92004-05-20 22:16:29 +00001331 KeyInfo keyInfo;
drhb9bb7c12006-06-11 23:41:55 +00001332 int addr; /* Address of OP_OpenEphemeral instruction */
drhd3d39e92004-05-20 22:16:29 +00001333
danielk1977bf3b7212004-05-18 10:06:24 +00001334 affinity = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +00001335
1336 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
drh57dbd7b2005-07-08 18:25:26 +00001337 ** expression it is handled the same way. A virtual table is
danielk1977e014a832004-05-17 10:48:57 +00001338 ** filled with single-field index keys representing the results
1339 ** from the SELECT or the <exprlist>.
1340 **
1341 ** If the 'x' expression is a column value, or the SELECT...
1342 ** statement returns a column value, then the affinity of that
1343 ** column is used to build the index keys. If both 'x' and the
1344 ** SELECT... statement are columns, then numeric affinity is used
1345 ** if either column has NUMERIC or INTEGER affinity. If neither
1346 ** 'x' nor the SELECT... statement are columns, then numeric affinity
1347 ** is used.
1348 */
1349 pExpr->iTable = pParse->nTab++;
drhb9bb7c12006-06-11 23:41:55 +00001350 addr = sqlite3VdbeAddOp(v, OP_OpenEphemeral, pExpr->iTable, 0);
drhd3d39e92004-05-20 22:16:29 +00001351 memset(&keyInfo, 0, sizeof(keyInfo));
1352 keyInfo.nField = 1;
drhf3218fe2004-05-28 08:21:02 +00001353 sqlite3VdbeAddOp(v, OP_SetNumColumns, pExpr->iTable, 1);
danielk1977e014a832004-05-17 10:48:57 +00001354
drhfef52082000-06-06 01:50:43 +00001355 if( pExpr->pSelect ){
1356 /* Case 1: expr IN (SELECT ...)
1357 **
danielk1977e014a832004-05-17 10:48:57 +00001358 ** Generate code to write the results of the select into the temporary
1359 ** table allocated and opened above.
drhfef52082000-06-06 01:50:43 +00001360 */
danielk1977e014a832004-05-17 10:48:57 +00001361 int iParm = pExpr->iTable + (((int)affinity)<<16);
drhbe5c89a2004-07-26 00:31:09 +00001362 ExprList *pEList;
danielk1977e014a832004-05-17 10:48:57 +00001363 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
danielk1977b3bce662005-01-29 08:32:43 +00001364 sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001365 pEList = pExpr->pSelect->pEList;
1366 if( pEList && pEList->nExpr>0 ){
danielk19777cedc8d2004-06-10 10:50:08 +00001367 keyInfo.aColl[0] = binaryCompareCollSeq(pParse, pExpr->pLeft,
drhbe5c89a2004-07-26 00:31:09 +00001368 pEList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001369 }
drhfef52082000-06-06 01:50:43 +00001370 }else if( pExpr->pList ){
1371 /* Case 2: expr IN (exprlist)
1372 **
danielk1977e014a832004-05-17 10:48:57 +00001373 ** For each expression, build an index key from the evaluation and
1374 ** store it in the temporary table. If <expr> is a column, then use
1375 ** that columns affinity when building index keys. If <expr> is not
1376 ** a column, use numeric affinity.
drhfef52082000-06-06 01:50:43 +00001377 */
danielk1977e014a832004-05-17 10:48:57 +00001378 int i;
drh57dbd7b2005-07-08 18:25:26 +00001379 ExprList *pList = pExpr->pList;
1380 struct ExprList_item *pItem;
1381
danielk1977e014a832004-05-17 10:48:57 +00001382 if( !affinity ){
drh8159a352006-05-23 23:22:29 +00001383 affinity = SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +00001384 }
danielk19770202b292004-06-09 09:55:16 +00001385 keyInfo.aColl[0] = pExpr->pLeft->pColl;
danielk1977e014a832004-05-17 10:48:57 +00001386
1387 /* Loop through each expression in <exprlist>. */
drh57dbd7b2005-07-08 18:25:26 +00001388 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
1389 Expr *pE2 = pItem->pExpr;
danielk1977e014a832004-05-17 10:48:57 +00001390
drh57dbd7b2005-07-08 18:25:26 +00001391 /* If the expression is not constant then we will need to
1392 ** disable the test that was generated above that makes sure
1393 ** this code only executes once. Because for a non-constant
1394 ** expression we need to rerun this code each time.
1395 */
drh6c30be82005-07-29 15:10:17 +00001396 if( testAddr>0 && !sqlite3ExprIsConstant(pE2) ){
drhf8875402006-03-17 13:56:34 +00001397 sqlite3VdbeChangeToNoop(v, testAddr-1, 3);
drh57dbd7b2005-07-08 18:25:26 +00001398 testAddr = 0;
drh4794b982000-06-06 13:54:14 +00001399 }
danielk1977e014a832004-05-17 10:48:57 +00001400
1401 /* Evaluate the expression and insert it into the temp table */
1402 sqlite3ExprCode(pParse, pE2);
drh94a11212004-09-25 13:12:14 +00001403 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1);
drhf0863fe2005-06-12 21:35:51 +00001404 sqlite3VdbeAddOp(v, OP_IdxInsert, pExpr->iTable, 0);
drhfef52082000-06-06 01:50:43 +00001405 }
1406 }
danielk19770202b292004-06-09 09:55:16 +00001407 sqlite3VdbeChangeP3(v, addr, (void *)&keyInfo, P3_KEYINFO);
danielk1977b3bce662005-01-29 08:32:43 +00001408 break;
drhfef52082000-06-06 01:50:43 +00001409 }
1410
drh51522cd2005-01-20 13:36:19 +00001411 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001412 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +00001413 /* This has to be a scalar SELECT. Generate code to put the
1414 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +00001415 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +00001416 */
drh2646da72005-12-09 20:02:05 +00001417 static const Token one = { (u8*)"1", 0, 1 };
drh51522cd2005-01-20 13:36:19 +00001418 Select *pSel;
drhec7429a2005-10-06 16:53:14 +00001419 int iMem;
1420 int sop;
drh1398ad32005-01-19 23:24:50 +00001421
drhec7429a2005-10-06 16:53:14 +00001422 pExpr->iColumn = iMem = pParse->nMem++;
drh51522cd2005-01-20 13:36:19 +00001423 pSel = pExpr->pSelect;
1424 if( pExpr->op==TK_SELECT ){
1425 sop = SRT_Mem;
drhec7429a2005-10-06 16:53:14 +00001426 sqlite3VdbeAddOp(v, OP_MemNull, iMem, 0);
1427 VdbeComment((v, "# Init subquery result"));
drh51522cd2005-01-20 13:36:19 +00001428 }else{
drh51522cd2005-01-20 13:36:19 +00001429 sop = SRT_Exists;
drhec7429a2005-10-06 16:53:14 +00001430 sqlite3VdbeAddOp(v, OP_MemInt, 0, iMem);
1431 VdbeComment((v, "# Init EXISTS result"));
drh51522cd2005-01-20 13:36:19 +00001432 }
drhec7429a2005-10-06 16:53:14 +00001433 sqlite3ExprDelete(pSel->pLimit);
1434 pSel->pLimit = sqlite3Expr(TK_INTEGER, 0, 0, &one);
1435 sqlite3Select(pParse, pSel, sop, iMem, 0, 0, 0, 0);
danielk1977b3bce662005-01-29 08:32:43 +00001436 break;
drhcce7d172000-05-31 15:34:51 +00001437 }
1438 }
danielk1977b3bce662005-01-29 08:32:43 +00001439
drh57dbd7b2005-07-08 18:25:26 +00001440 if( testAddr ){
drhd654be82005-09-20 17:42:23 +00001441 sqlite3VdbeJumpHere(v, testAddr);
danielk1977b3bce662005-01-29 08:32:43 +00001442 }
1443 return;
drhcce7d172000-05-31 15:34:51 +00001444}
drh51522cd2005-01-20 13:36:19 +00001445#endif /* SQLITE_OMIT_SUBQUERY */
drhcce7d172000-05-31 15:34:51 +00001446
drhcce7d172000-05-31 15:34:51 +00001447/*
drhfec19aa2004-05-19 20:41:03 +00001448** Generate an instruction that will put the integer describe by
1449** text z[0..n-1] on the stack.
1450*/
1451static void codeInteger(Vdbe *v, const char *z, int n){
1452 int i;
drh6fec0762004-05-30 01:38:43 +00001453 if( sqlite3GetInt32(z, &i) ){
1454 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
1455 }else if( sqlite3FitsIn64Bits(z) ){
drh29dda4a2005-07-21 18:23:20 +00001456 sqlite3VdbeOp3(v, OP_Int64, 0, 0, z, n);
drhfec19aa2004-05-19 20:41:03 +00001457 }else{
1458 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n);
1459 }
1460}
1461
1462/*
drhcce7d172000-05-31 15:34:51 +00001463** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +00001464** expression and leave the result on the top of stack.
drhf2bc0132004-10-04 13:19:23 +00001465**
1466** This code depends on the fact that certain token values (ex: TK_EQ)
1467** are the same as opcode values (ex: OP_Eq) that implement the corresponding
1468** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
1469** the make process cause these values to align. Assert()s in the code
1470** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00001471*/
danielk19774adee202004-05-08 08:23:19 +00001472void sqlite3ExprCode(Parse *pParse, Expr *pExpr){
drhcce7d172000-05-31 15:34:51 +00001473 Vdbe *v = pParse->pVdbe;
1474 int op;
drhffe07b22005-11-03 00:41:17 +00001475 int stackChng = 1; /* Amount of change to stack depth */
1476
danielk19777977a172004-11-09 12:44:37 +00001477 if( v==0 ) return;
1478 if( pExpr==0 ){
drhf0863fe2005-06-12 21:35:51 +00001479 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
danielk19777977a172004-11-09 12:44:37 +00001480 return;
1481 }
drhf2bc0132004-10-04 13:19:23 +00001482 op = pExpr->op;
1483 switch( op ){
drh13449892005-09-07 21:22:45 +00001484 case TK_AGG_COLUMN: {
1485 AggInfo *pAggInfo = pExpr->pAggInfo;
1486 struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg];
1487 if( !pAggInfo->directMode ){
1488 sqlite3VdbeAddOp(v, OP_MemLoad, pCol->iMem, 0);
1489 break;
1490 }else if( pAggInfo->useSortingIdx ){
1491 sqlite3VdbeAddOp(v, OP_Column, pAggInfo->sortingIdx,
1492 pCol->iSorterColumn);
1493 break;
1494 }
1495 /* Otherwise, fall thru into the TK_COLUMN case */
1496 }
drh967e8b72000-06-21 13:59:10 +00001497 case TK_COLUMN: {
drhffe07b22005-11-03 00:41:17 +00001498 if( pExpr->iTable<0 ){
1499 /* This only happens when coding check constraints */
1500 assert( pParse->ckOffset>0 );
1501 sqlite3VdbeAddOp(v, OP_Dup, pParse->ckOffset-pExpr->iColumn-1, 1);
1502 }else if( pExpr->iColumn>=0 ){
drh8a512562005-11-14 22:29:05 +00001503 Table *pTab = pExpr->pTab;
1504 int iCol = pExpr->iColumn;
drh4cbdda92006-06-14 19:00:20 +00001505 int op = (pTab && IsVirtual(pTab)) ? OP_VColumn : OP_Column;
danielk19777dabaa12006-06-13 04:11:43 +00001506 sqlite3VdbeAddOp(v, op, pExpr->iTable, iCol);
drh8a512562005-11-14 22:29:05 +00001507 sqlite3ColumnDefault(v, pTab, iCol);
1508#ifndef SQLITE_OMIT_FLOATING_POINT
1509 if( pTab && pTab->aCol[iCol].affinity==SQLITE_AFF_REAL ){
1510 sqlite3VdbeAddOp(v, OP_RealAffinity, 0, 0);
1511 }
1512#endif
drhc4a3c772001-04-04 11:48:57 +00001513 }else{
danielk19777dabaa12006-06-13 04:11:43 +00001514 Table *pTab = pExpr->pTab;
drh4cbdda92006-06-14 19:00:20 +00001515 int op = (pTab && IsVirtual(pTab)) ? OP_VRowid : OP_Rowid;
danielk19777dabaa12006-06-13 04:11:43 +00001516 sqlite3VdbeAddOp(v, op, pExpr->iTable, 0);
drh22827922000-06-06 17:27:05 +00001517 }
drhcce7d172000-05-31 15:34:51 +00001518 break;
1519 }
1520 case TK_INTEGER: {
drh2646da72005-12-09 20:02:05 +00001521 codeInteger(v, (char*)pExpr->token.z, pExpr->token.n);
drhfec19aa2004-05-19 20:41:03 +00001522 break;
1523 }
1524 case TK_FLOAT:
1525 case TK_STRING: {
drhf2bc0132004-10-04 13:19:23 +00001526 assert( TK_FLOAT==OP_Real );
1527 assert( TK_STRING==OP_String8 );
drhd2687b72005-08-12 22:56:09 +00001528 sqlite3DequoteExpr(pExpr);
drh2646da72005-12-09 20:02:05 +00001529 sqlite3VdbeOp3(v, op, 0, 0, (char*)pExpr->token.z, pExpr->token.n);
drhcce7d172000-05-31 15:34:51 +00001530 break;
1531 }
drhf0863fe2005-06-12 21:35:51 +00001532 case TK_NULL: {
1533 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
1534 break;
1535 }
danielk19775338a5f2005-01-20 13:03:10 +00001536#ifndef SQLITE_OMIT_BLOB_LITERAL
danielk1977c572ef72004-05-27 09:28:41 +00001537 case TK_BLOB: {
drh6c8c6ce2005-08-23 11:17:58 +00001538 int n;
1539 const char *z;
drhf2bc0132004-10-04 13:19:23 +00001540 assert( TK_BLOB==OP_HexBlob );
drh6c8c6ce2005-08-23 11:17:58 +00001541 n = pExpr->token.n - 3;
drh2646da72005-12-09 20:02:05 +00001542 z = (char*)pExpr->token.z + 2;
drh6c8c6ce2005-08-23 11:17:58 +00001543 assert( n>=0 );
1544 if( n==0 ){
1545 z = "";
1546 }
1547 sqlite3VdbeOp3(v, op, 0, 0, z, n);
danielk1977c572ef72004-05-27 09:28:41 +00001548 break;
1549 }
danielk19775338a5f2005-01-20 13:03:10 +00001550#endif
drh50457892003-09-06 01:10:47 +00001551 case TK_VARIABLE: {
danielk19774adee202004-05-08 08:23:19 +00001552 sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0);
drh895d7472004-08-20 16:02:39 +00001553 if( pExpr->token.n>1 ){
drh2646da72005-12-09 20:02:05 +00001554 sqlite3VdbeChangeP3(v, -1, (char*)pExpr->token.z, pExpr->token.n);
drh895d7472004-08-20 16:02:39 +00001555 }
drh50457892003-09-06 01:10:47 +00001556 break;
1557 }
drh4e0cff62004-11-05 05:10:28 +00001558 case TK_REGISTER: {
1559 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iTable, 0);
1560 break;
1561 }
drh487e2622005-06-25 18:42:14 +00001562#ifndef SQLITE_OMIT_CAST
1563 case TK_CAST: {
1564 /* Expressions of the form: CAST(pLeft AS token) */
danielk1977f0113002006-01-24 12:09:17 +00001565 int aff, to_op;
drh487e2622005-06-25 18:42:14 +00001566 sqlite3ExprCode(pParse, pExpr->pLeft);
drh8a512562005-11-14 22:29:05 +00001567 aff = sqlite3AffinityType(&pExpr->token);
danielk1977f0113002006-01-24 12:09:17 +00001568 to_op = aff - SQLITE_AFF_TEXT + OP_ToText;
1569 assert( to_op==OP_ToText || aff!=SQLITE_AFF_TEXT );
1570 assert( to_op==OP_ToBlob || aff!=SQLITE_AFF_NONE );
1571 assert( to_op==OP_ToNumeric || aff!=SQLITE_AFF_NUMERIC );
1572 assert( to_op==OP_ToInt || aff!=SQLITE_AFF_INTEGER );
1573 assert( to_op==OP_ToReal || aff!=SQLITE_AFF_REAL );
1574 sqlite3VdbeAddOp(v, to_op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001575 stackChng = 0;
drh487e2622005-06-25 18:42:14 +00001576 break;
1577 }
1578#endif /* SQLITE_OMIT_CAST */
drhc9b84a12002-06-20 11:36:48 +00001579 case TK_LT:
1580 case TK_LE:
1581 case TK_GT:
1582 case TK_GE:
1583 case TK_NE:
1584 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00001585 assert( TK_LT==OP_Lt );
1586 assert( TK_LE==OP_Le );
1587 assert( TK_GT==OP_Gt );
1588 assert( TK_GE==OP_Ge );
1589 assert( TK_EQ==OP_Eq );
1590 assert( TK_NE==OP_Ne );
danielk1977a37cdde2004-05-16 11:15:36 +00001591 sqlite3ExprCode(pParse, pExpr->pLeft);
1592 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001593 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001594 stackChng = -1;
danielk1977a37cdde2004-05-16 11:15:36 +00001595 break;
drhc9b84a12002-06-20 11:36:48 +00001596 }
drhcce7d172000-05-31 15:34:51 +00001597 case TK_AND:
1598 case TK_OR:
1599 case TK_PLUS:
1600 case TK_STAR:
1601 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00001602 case TK_REM:
1603 case TK_BITAND:
1604 case TK_BITOR:
drh17c40292004-07-21 02:53:29 +00001605 case TK_SLASH:
drhbf4133c2001-10-13 02:59:08 +00001606 case TK_LSHIFT:
drh855eb1c2004-08-31 13:45:11 +00001607 case TK_RSHIFT:
drh00400772000-06-16 20:51:26 +00001608 case TK_CONCAT: {
drhf2bc0132004-10-04 13:19:23 +00001609 assert( TK_AND==OP_And );
1610 assert( TK_OR==OP_Or );
1611 assert( TK_PLUS==OP_Add );
1612 assert( TK_MINUS==OP_Subtract );
1613 assert( TK_REM==OP_Remainder );
1614 assert( TK_BITAND==OP_BitAnd );
1615 assert( TK_BITOR==OP_BitOr );
1616 assert( TK_SLASH==OP_Divide );
1617 assert( TK_LSHIFT==OP_ShiftLeft );
1618 assert( TK_RSHIFT==OP_ShiftRight );
1619 assert( TK_CONCAT==OP_Concat );
danielk19774adee202004-05-08 08:23:19 +00001620 sqlite3ExprCode(pParse, pExpr->pLeft);
1621 sqlite3ExprCode(pParse, pExpr->pRight);
drh855eb1c2004-08-31 13:45:11 +00001622 sqlite3VdbeAddOp(v, op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001623 stackChng = -1;
drh00400772000-06-16 20:51:26 +00001624 break;
1625 }
drhcce7d172000-05-31 15:34:51 +00001626 case TK_UMINUS: {
drhfec19aa2004-05-19 20:41:03 +00001627 Expr *pLeft = pExpr->pLeft;
1628 assert( pLeft );
1629 if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
1630 Token *p = &pLeft->token;
drh9267bdc2005-11-28 12:36:35 +00001631 char *z = sqlite3MPrintf("-%.*s", p->n, p->z);
drhfec19aa2004-05-19 20:41:03 +00001632 if( pLeft->op==TK_FLOAT ){
1633 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001634 }else{
drhfec19aa2004-05-19 20:41:03 +00001635 codeInteger(v, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001636 }
drh6e142f52000-06-08 13:36:40 +00001637 sqliteFree(z);
1638 break;
1639 }
drh1ccde152000-06-17 13:12:39 +00001640 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +00001641 }
drhbf4133c2001-10-13 02:59:08 +00001642 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00001643 case TK_NOT: {
drhf2bc0132004-10-04 13:19:23 +00001644 assert( TK_BITNOT==OP_BitNot );
1645 assert( TK_NOT==OP_Not );
danielk19774adee202004-05-08 08:23:19 +00001646 sqlite3ExprCode(pParse, pExpr->pLeft);
1647 sqlite3VdbeAddOp(v, op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001648 stackChng = 0;
drhcce7d172000-05-31 15:34:51 +00001649 break;
1650 }
1651 case TK_ISNULL:
1652 case TK_NOTNULL: {
1653 int dest;
drhf2bc0132004-10-04 13:19:23 +00001654 assert( TK_ISNULL==OP_IsNull );
1655 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00001656 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1657 sqlite3ExprCode(pParse, pExpr->pLeft);
1658 dest = sqlite3VdbeCurrentAddr(v) + 2;
1659 sqlite3VdbeAddOp(v, op, 1, dest);
1660 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);
drhffe07b22005-11-03 00:41:17 +00001661 stackChng = 0;
drhf2bc0132004-10-04 13:19:23 +00001662 break;
drhcce7d172000-05-31 15:34:51 +00001663 }
drh22827922000-06-06 17:27:05 +00001664 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00001665 AggInfo *pInfo = pExpr->pAggInfo;
drh7e56e712005-11-16 12:53:15 +00001666 if( pInfo==0 ){
1667 sqlite3ErrorMsg(pParse, "misuse of aggregate: %T",
1668 &pExpr->span);
1669 }else{
1670 sqlite3VdbeAddOp(v, OP_MemLoad, pInfo->aFunc[pExpr->iAgg].iMem, 0);
1671 }
drh22827922000-06-06 17:27:05 +00001672 break;
1673 }
drhb71090f2005-05-23 17:26:51 +00001674 case TK_CONST_FUNC:
drhcce7d172000-05-31 15:34:51 +00001675 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00001676 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00001677 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00001678 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00001679 int nId;
1680 const char *zId;
drh13449892005-09-07 21:22:45 +00001681 int constMask = 0;
danielk1977682f68b2004-06-05 10:22:17 +00001682 int i;
danielk197714db2662006-01-09 16:12:04 +00001683 u8 enc = ENC(pParse->db);
danielk1977dc1bdc42004-06-11 10:51:27 +00001684 CollSeq *pColl = 0;
drh2646da72005-12-09 20:02:05 +00001685 zId = (char*)pExpr->token.z;
drhb71090f2005-05-23 17:26:51 +00001686 nId = pExpr->token.n;
danielk1977d8123362004-06-12 09:25:12 +00001687 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001688 assert( pDef!=0 );
drhf9b596e2004-05-26 16:54:42 +00001689 nExpr = sqlite3ExprCodeExprList(pParse, pList);
drhb7f6f682006-07-08 17:06:43 +00001690#ifndef SQLITE_OMIT_VIRTUALTABLE
drha43fa222006-07-08 18:41:37 +00001691 /* Possibly overload the function if the first argument is
1692 ** a virtual table column.
1693 **
1694 ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the
1695 ** second argument, not the first, as the argument to test to
1696 ** see if it is a column in a virtual table. This is done because
1697 ** the left operand of infix functions (the operand we want to
1698 ** control overloading) ends up as the second argument to the
1699 ** function. The expression "A glob B" is equivalent to
1700 ** "glob(B,A). We want to use the A in "A glob B" to test
1701 ** for function overloading. But we use the B term in "glob(B,A)".
1702 */
drh6a03a1c2006-07-08 18:34:59 +00001703 if( nExpr>=2 && (pExpr->flags & EP_InfixFunc) ){
drh6a03a1c2006-07-08 18:34:59 +00001704 pDef = sqlite3VtabOverloadFunction(pDef, nExpr, pList->a[1].pExpr);
1705 }else if( nExpr>0 ){
drhb7f6f682006-07-08 17:06:43 +00001706 pDef = sqlite3VtabOverloadFunction(pDef, nExpr, pList->a[0].pExpr);
1707 }
1708#endif
danielk1977682f68b2004-06-05 10:22:17 +00001709 for(i=0; i<nExpr && i<32; i++){
danielk1977d02eb1f2004-06-06 09:44:03 +00001710 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
drh13449892005-09-07 21:22:45 +00001711 constMask |= (1<<i);
danielk1977d02eb1f2004-06-06 09:44:03 +00001712 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001713 if( pDef->needCollSeq && !pColl ){
1714 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
1715 }
1716 }
1717 if( pDef->needCollSeq ){
1718 if( !pColl ) pColl = pParse->db->pDfltColl;
danielk1977d8123362004-06-12 09:25:12 +00001719 sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ);
danielk1977682f68b2004-06-05 10:22:17 +00001720 }
drh13449892005-09-07 21:22:45 +00001721 sqlite3VdbeOp3(v, OP_Function, constMask, nExpr, (char*)pDef, P3_FUNCDEF);
drhffe07b22005-11-03 00:41:17 +00001722 stackChng = 1-nExpr;
drhcce7d172000-05-31 15:34:51 +00001723 break;
1724 }
drhfe2093d2005-01-20 22:48:47 +00001725#ifndef SQLITE_OMIT_SUBQUERY
1726 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001727 case TK_SELECT: {
drh41714d62006-03-02 04:44:23 +00001728 if( pExpr->iColumn==0 ){
1729 sqlite3CodeSubselect(pParse, pExpr);
1730 }
danielk19774adee202004-05-08 08:23:19 +00001731 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drhad6d9462004-09-19 02:15:24 +00001732 VdbeComment((v, "# load subquery result"));
drh19a775c2000-06-05 18:54:46 +00001733 break;
1734 }
drhfef52082000-06-06 01:50:43 +00001735 case TK_IN: {
1736 int addr;
drh94a11212004-09-25 13:12:14 +00001737 char affinity;
drhafa5f682006-01-30 14:36:59 +00001738 int ckOffset = pParse->ckOffset;
danielk1977b3bce662005-01-29 08:32:43 +00001739 sqlite3CodeSubselect(pParse, pExpr);
danielk1977e014a832004-05-17 10:48:57 +00001740
1741 /* Figure out the affinity to use to create a key from the results
1742 ** of the expression. affinityStr stores a static string suitable for
danielk1977ededfd52004-06-17 07:53:01 +00001743 ** P3 of OP_MakeRecord.
danielk1977e014a832004-05-17 10:48:57 +00001744 */
drh94a11212004-09-25 13:12:14 +00001745 affinity = comparisonAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +00001746
danielk19774adee202004-05-08 08:23:19 +00001747 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
drhafa5f682006-01-30 14:36:59 +00001748 pParse->ckOffset = ckOffset+1;
danielk1977e014a832004-05-17 10:48:57 +00001749
1750 /* Code the <expr> from "<expr> IN (...)". The temporary table
1751 ** pExpr->iTable contains the values that make up the (...) set.
1752 */
danielk19774adee202004-05-08 08:23:19 +00001753 sqlite3ExprCode(pParse, pExpr->pLeft);
1754 addr = sqlite3VdbeCurrentAddr(v);
danielk1977e014a832004-05-17 10:48:57 +00001755 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); /* addr + 0 */
danielk19774adee202004-05-08 08:23:19 +00001756 sqlite3VdbeAddOp(v, OP_Pop, 2, 0);
drhf0863fe2005-06-12 21:35:51 +00001757 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +00001758 sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7);
drh94a11212004-09-25 13:12:14 +00001759 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1); /* addr + 4 */
danielk1977e014a832004-05-17 10:48:57 +00001760 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7);
1761 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); /* addr + 6 */
1762
drhfef52082000-06-06 01:50:43 +00001763 break;
1764 }
danielk197793758c82005-01-21 08:13:14 +00001765#endif
drhfef52082000-06-06 01:50:43 +00001766 case TK_BETWEEN: {
drhbe5c89a2004-07-26 00:31:09 +00001767 Expr *pLeft = pExpr->pLeft;
1768 struct ExprList_item *pLItem = pExpr->pList->a;
1769 Expr *pRight = pLItem->pExpr;
1770 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001771 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001772 sqlite3ExprCode(pParse, pRight);
1773 codeCompare(pParse, pLeft, pRight, OP_Ge, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001774 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
drhbe5c89a2004-07-26 00:31:09 +00001775 pLItem++;
1776 pRight = pLItem->pExpr;
1777 sqlite3ExprCode(pParse, pRight);
1778 codeCompare(pParse, pLeft, pRight, OP_Le, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001779 sqlite3VdbeAddOp(v, OP_And, 0, 0);
drhfef52082000-06-06 01:50:43 +00001780 break;
1781 }
drh51e9a442004-01-16 16:42:53 +00001782 case TK_UPLUS:
drha2e00042002-01-22 03:13:42 +00001783 case TK_AS: {
danielk19774adee202004-05-08 08:23:19 +00001784 sqlite3ExprCode(pParse, pExpr->pLeft);
drhffe07b22005-11-03 00:41:17 +00001785 stackChng = 0;
drha2e00042002-01-22 03:13:42 +00001786 break;
1787 }
drh17a7f8d2002-03-24 13:13:27 +00001788 case TK_CASE: {
1789 int expr_end_label;
drhf5905aa2002-05-26 20:54:33 +00001790 int jumpInst;
drhf5905aa2002-05-26 20:54:33 +00001791 int nExpr;
drh17a7f8d2002-03-24 13:13:27 +00001792 int i;
drhbe5c89a2004-07-26 00:31:09 +00001793 ExprList *pEList;
1794 struct ExprList_item *aListelem;
drh17a7f8d2002-03-24 13:13:27 +00001795
1796 assert(pExpr->pList);
1797 assert((pExpr->pList->nExpr % 2) == 0);
1798 assert(pExpr->pList->nExpr > 0);
drhbe5c89a2004-07-26 00:31:09 +00001799 pEList = pExpr->pList;
1800 aListelem = pEList->a;
1801 nExpr = pEList->nExpr;
danielk19774adee202004-05-08 08:23:19 +00001802 expr_end_label = sqlite3VdbeMakeLabel(v);
drh17a7f8d2002-03-24 13:13:27 +00001803 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001804 sqlite3ExprCode(pParse, pExpr->pLeft);
drh17a7f8d2002-03-24 13:13:27 +00001805 }
drhf5905aa2002-05-26 20:54:33 +00001806 for(i=0; i<nExpr; i=i+2){
drhbe5c89a2004-07-26 00:31:09 +00001807 sqlite3ExprCode(pParse, aListelem[i].pExpr);
drh17a7f8d2002-03-24 13:13:27 +00001808 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001809 sqlite3VdbeAddOp(v, OP_Dup, 1, 1);
drhbe5c89a2004-07-26 00:31:09 +00001810 jumpInst = codeCompare(pParse, pExpr->pLeft, aListelem[i].pExpr,
1811 OP_Ne, 0, 1);
danielk19774adee202004-05-08 08:23:19 +00001812 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001813 }else{
danielk19774adee202004-05-08 08:23:19 +00001814 jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001815 }
drhbe5c89a2004-07-26 00:31:09 +00001816 sqlite3ExprCode(pParse, aListelem[i+1].pExpr);
danielk19774adee202004-05-08 08:23:19 +00001817 sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label);
drhd654be82005-09-20 17:42:23 +00001818 sqlite3VdbeJumpHere(v, jumpInst);
drh17a7f8d2002-03-24 13:13:27 +00001819 }
drhf570f012002-05-31 15:51:25 +00001820 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001821 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhf570f012002-05-31 15:51:25 +00001822 }
drh17a7f8d2002-03-24 13:13:27 +00001823 if( pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001824 sqlite3ExprCode(pParse, pExpr->pRight);
drh17a7f8d2002-03-24 13:13:27 +00001825 }else{
drhf0863fe2005-06-12 21:35:51 +00001826 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
drh17a7f8d2002-03-24 13:13:27 +00001827 }
danielk19774adee202004-05-08 08:23:19 +00001828 sqlite3VdbeResolveLabel(v, expr_end_label);
danielk19776f349032002-06-11 02:25:40 +00001829 break;
1830 }
danielk19775338a5f2005-01-20 13:03:10 +00001831#ifndef SQLITE_OMIT_TRIGGER
danielk19776f349032002-06-11 02:25:40 +00001832 case TK_RAISE: {
1833 if( !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +00001834 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00001835 "RAISE() may only be used within a trigger-program");
danielk19776f349032002-06-11 02:25:40 +00001836 return;
1837 }
drhad6d9462004-09-19 02:15:24 +00001838 if( pExpr->iColumn!=OE_Ignore ){
1839 assert( pExpr->iColumn==OE_Rollback ||
1840 pExpr->iColumn == OE_Abort ||
1841 pExpr->iColumn == OE_Fail );
drhd2687b72005-08-12 22:56:09 +00001842 sqlite3DequoteExpr(pExpr);
drhad6d9462004-09-19 02:15:24 +00001843 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn,
drh2646da72005-12-09 20:02:05 +00001844 (char*)pExpr->token.z, pExpr->token.n);
danielk19776f349032002-06-11 02:25:40 +00001845 } else {
drhad6d9462004-09-19 02:15:24 +00001846 assert( pExpr->iColumn == OE_Ignore );
1847 sqlite3VdbeAddOp(v, OP_ContextPop, 0, 0);
1848 sqlite3VdbeAddOp(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
1849 VdbeComment((v, "# raise(IGNORE)"));
danielk19776f349032002-06-11 02:25:40 +00001850 }
drhffe07b22005-11-03 00:41:17 +00001851 stackChng = 0;
1852 break;
drh17a7f8d2002-03-24 13:13:27 +00001853 }
danielk19775338a5f2005-01-20 13:03:10 +00001854#endif
drhffe07b22005-11-03 00:41:17 +00001855 }
1856
1857 if( pParse->ckOffset ){
1858 pParse->ckOffset += stackChng;
1859 assert( pParse->ckOffset );
drhcce7d172000-05-31 15:34:51 +00001860 }
drhcce7d172000-05-31 15:34:51 +00001861}
1862
danielk197793758c82005-01-21 08:13:14 +00001863#ifndef SQLITE_OMIT_TRIGGER
drhcce7d172000-05-31 15:34:51 +00001864/*
drh25303782004-12-07 15:41:48 +00001865** Generate code that evalutes the given expression and leaves the result
1866** on the stack. See also sqlite3ExprCode().
1867**
1868** This routine might also cache the result and modify the pExpr tree
1869** so that it will make use of the cached result on subsequent evaluations
1870** rather than evaluate the whole expression again. Trivial expressions are
1871** not cached. If the expression is cached, its result is stored in a
1872** memory location.
1873*/
1874void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr){
1875 Vdbe *v = pParse->pVdbe;
1876 int iMem;
1877 int addr1, addr2;
1878 if( v==0 ) return;
1879 addr1 = sqlite3VdbeCurrentAddr(v);
1880 sqlite3ExprCode(pParse, pExpr);
1881 addr2 = sqlite3VdbeCurrentAddr(v);
1882 if( addr2>addr1+1 || sqlite3VdbeGetOp(v, addr1)->opcode==OP_Function ){
1883 iMem = pExpr->iTable = pParse->nMem++;
1884 sqlite3VdbeAddOp(v, OP_MemStore, iMem, 0);
1885 pExpr->op = TK_REGISTER;
1886 }
1887}
danielk197793758c82005-01-21 08:13:14 +00001888#endif
drh25303782004-12-07 15:41:48 +00001889
1890/*
drh268380c2004-02-25 13:47:31 +00001891** Generate code that pushes the value of every element of the given
drhf9b596e2004-05-26 16:54:42 +00001892** expression list onto the stack.
drh268380c2004-02-25 13:47:31 +00001893**
1894** Return the number of elements pushed onto the stack.
1895*/
danielk19774adee202004-05-08 08:23:19 +00001896int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00001897 Parse *pParse, /* Parsing context */
drhf9b596e2004-05-26 16:54:42 +00001898 ExprList *pList /* The expression list to be coded */
drh268380c2004-02-25 13:47:31 +00001899){
1900 struct ExprList_item *pItem;
1901 int i, n;
drh268380c2004-02-25 13:47:31 +00001902 if( pList==0 ) return 0;
drh268380c2004-02-25 13:47:31 +00001903 n = pList->nExpr;
drhc182d162005-08-14 20:47:16 +00001904 for(pItem=pList->a, i=n; i>0; i--, pItem++){
danielk19774adee202004-05-08 08:23:19 +00001905 sqlite3ExprCode(pParse, pItem->pExpr);
drh268380c2004-02-25 13:47:31 +00001906 }
drhf9b596e2004-05-26 16:54:42 +00001907 return n;
drh268380c2004-02-25 13:47:31 +00001908}
1909
1910/*
drhcce7d172000-05-31 15:34:51 +00001911** Generate code for a boolean expression such that a jump is made
1912** to the label "dest" if the expression is true but execution
1913** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00001914**
1915** If the expression evaluates to NULL (neither true nor false), then
1916** take the jump if the jumpIfNull flag is true.
drhf2bc0132004-10-04 13:19:23 +00001917**
1918** This code depends on the fact that certain token values (ex: TK_EQ)
1919** are the same as opcode values (ex: OP_Eq) that implement the corresponding
1920** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
1921** the make process cause these values to align. Assert()s in the code
1922** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00001923*/
danielk19774adee202004-05-08 08:23:19 +00001924void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001925 Vdbe *v = pParse->pVdbe;
1926 int op = 0;
drhffe07b22005-11-03 00:41:17 +00001927 int ckOffset = pParse->ckOffset;
drhdaffd0e2001-04-11 14:28:42 +00001928 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00001929 op = pExpr->op;
1930 switch( op ){
drhcce7d172000-05-31 15:34:51 +00001931 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001932 int d2 = sqlite3VdbeMakeLabel(v);
1933 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
1934 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
1935 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001936 break;
1937 }
1938 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001939 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
1940 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001941 break;
1942 }
1943 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001944 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001945 break;
1946 }
1947 case TK_LT:
1948 case TK_LE:
1949 case TK_GT:
1950 case TK_GE:
1951 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00001952 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00001953 assert( TK_LT==OP_Lt );
1954 assert( TK_LE==OP_Le );
1955 assert( TK_GT==OP_Gt );
1956 assert( TK_GE==OP_Ge );
1957 assert( TK_EQ==OP_Eq );
1958 assert( TK_NE==OP_Ne );
danielk19774adee202004-05-08 08:23:19 +00001959 sqlite3ExprCode(pParse, pExpr->pLeft);
1960 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001961 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001962 break;
1963 }
1964 case TK_ISNULL:
1965 case TK_NOTNULL: {
drhf2bc0132004-10-04 13:19:23 +00001966 assert( TK_ISNULL==OP_IsNull );
1967 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00001968 sqlite3ExprCode(pParse, pExpr->pLeft);
1969 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001970 break;
1971 }
drhfef52082000-06-06 01:50:43 +00001972 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00001973 /* The expression "x BETWEEN y AND z" is implemented as:
1974 **
1975 ** 1 IF (x < y) GOTO 3
1976 ** 2 IF (x <= z) GOTO <dest>
1977 ** 3 ...
1978 */
drhf5905aa2002-05-26 20:54:33 +00001979 int addr;
drhbe5c89a2004-07-26 00:31:09 +00001980 Expr *pLeft = pExpr->pLeft;
1981 Expr *pRight = pExpr->pList->a[0].pExpr;
1982 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001983 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001984 sqlite3ExprCode(pParse, pRight);
1985 addr = codeCompare(pParse, pLeft, pRight, OP_Lt, 0, !jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00001986
drhbe5c89a2004-07-26 00:31:09 +00001987 pRight = pExpr->pList->a[1].pExpr;
1988 sqlite3ExprCode(pParse, pRight);
1989 codeCompare(pParse, pLeft, pRight, OP_Le, dest, jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00001990
danielk19774adee202004-05-08 08:23:19 +00001991 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
drhd654be82005-09-20 17:42:23 +00001992 sqlite3VdbeJumpHere(v, addr);
danielk19774adee202004-05-08 08:23:19 +00001993 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00001994 break;
1995 }
drhcce7d172000-05-31 15:34:51 +00001996 default: {
danielk19774adee202004-05-08 08:23:19 +00001997 sqlite3ExprCode(pParse, pExpr);
1998 sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001999 break;
2000 }
2001 }
drhffe07b22005-11-03 00:41:17 +00002002 pParse->ckOffset = ckOffset;
drhcce7d172000-05-31 15:34:51 +00002003}
2004
2005/*
drh66b89c82000-11-28 20:47:17 +00002006** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00002007** to the label "dest" if the expression is false but execution
2008** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00002009**
2010** If the expression evaluates to NULL (neither true nor false) then
2011** jump if jumpIfNull is true or fall through if jumpIfNull is false.
drhcce7d172000-05-31 15:34:51 +00002012*/
danielk19774adee202004-05-08 08:23:19 +00002013void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00002014 Vdbe *v = pParse->pVdbe;
2015 int op = 0;
drhffe07b22005-11-03 00:41:17 +00002016 int ckOffset = pParse->ckOffset;
drhdaffd0e2001-04-11 14:28:42 +00002017 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00002018
2019 /* The value of pExpr->op and op are related as follows:
2020 **
2021 ** pExpr->op op
2022 ** --------- ----------
2023 ** TK_ISNULL OP_NotNull
2024 ** TK_NOTNULL OP_IsNull
2025 ** TK_NE OP_Eq
2026 ** TK_EQ OP_Ne
2027 ** TK_GT OP_Le
2028 ** TK_LE OP_Gt
2029 ** TK_GE OP_Lt
2030 ** TK_LT OP_Ge
2031 **
2032 ** For other values of pExpr->op, op is undefined and unused.
2033 ** The value of TK_ and OP_ constants are arranged such that we
2034 ** can compute the mapping above using the following expression.
2035 ** Assert()s verify that the computation is correct.
2036 */
2037 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
2038
2039 /* Verify correct alignment of TK_ and OP_ constants
2040 */
2041 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
2042 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
2043 assert( pExpr->op!=TK_NE || op==OP_Eq );
2044 assert( pExpr->op!=TK_EQ || op==OP_Ne );
2045 assert( pExpr->op!=TK_LT || op==OP_Ge );
2046 assert( pExpr->op!=TK_LE || op==OP_Gt );
2047 assert( pExpr->op!=TK_GT || op==OP_Le );
2048 assert( pExpr->op!=TK_GE || op==OP_Lt );
2049
drhcce7d172000-05-31 15:34:51 +00002050 switch( pExpr->op ){
2051 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00002052 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
2053 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002054 break;
2055 }
2056 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00002057 int d2 = sqlite3VdbeMakeLabel(v);
2058 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
2059 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
2060 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00002061 break;
2062 }
2063 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00002064 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002065 break;
2066 }
2067 case TK_LT:
2068 case TK_LE:
2069 case TK_GT:
2070 case TK_GE:
2071 case TK_NE:
2072 case TK_EQ: {
danielk19774adee202004-05-08 08:23:19 +00002073 sqlite3ExprCode(pParse, pExpr->pLeft);
2074 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00002075 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002076 break;
2077 }
drhcce7d172000-05-31 15:34:51 +00002078 case TK_ISNULL:
2079 case TK_NOTNULL: {
danielk19774adee202004-05-08 08:23:19 +00002080 sqlite3ExprCode(pParse, pExpr->pLeft);
2081 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00002082 break;
2083 }
drhfef52082000-06-06 01:50:43 +00002084 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00002085 /* The expression is "x BETWEEN y AND z". It is implemented as:
2086 **
2087 ** 1 IF (x >= y) GOTO 3
2088 ** 2 GOTO <dest>
2089 ** 3 IF (x > z) GOTO <dest>
2090 */
drhfef52082000-06-06 01:50:43 +00002091 int addr;
drhbe5c89a2004-07-26 00:31:09 +00002092 Expr *pLeft = pExpr->pLeft;
2093 Expr *pRight = pExpr->pList->a[0].pExpr;
2094 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00002095 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00002096 sqlite3ExprCode(pParse, pRight);
danielk19774adee202004-05-08 08:23:19 +00002097 addr = sqlite3VdbeCurrentAddr(v);
drhbe5c89a2004-07-26 00:31:09 +00002098 codeCompare(pParse, pLeft, pRight, OP_Ge, addr+3, !jumpIfNull);
2099
danielk19774adee202004-05-08 08:23:19 +00002100 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
2101 sqlite3VdbeAddOp(v, OP_Goto, 0, dest);
drhbe5c89a2004-07-26 00:31:09 +00002102 pRight = pExpr->pList->a[1].pExpr;
2103 sqlite3ExprCode(pParse, pRight);
2104 codeCompare(pParse, pLeft, pRight, OP_Gt, dest, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00002105 break;
2106 }
drhcce7d172000-05-31 15:34:51 +00002107 default: {
danielk19774adee202004-05-08 08:23:19 +00002108 sqlite3ExprCode(pParse, pExpr);
2109 sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00002110 break;
2111 }
2112 }
drhffe07b22005-11-03 00:41:17 +00002113 pParse->ckOffset = ckOffset;
drhcce7d172000-05-31 15:34:51 +00002114}
drh22827922000-06-06 17:27:05 +00002115
2116/*
2117** Do a deep comparison of two expression trees. Return TRUE (non-zero)
2118** if they are identical and return FALSE if they differ in any way.
2119*/
danielk19774adee202004-05-08 08:23:19 +00002120int sqlite3ExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00002121 int i;
danielk19774b202ae2006-01-23 05:50:58 +00002122 if( pA==0||pB==0 ){
2123 return pB==pA;
drh22827922000-06-06 17:27:05 +00002124 }
2125 if( pA->op!=pB->op ) return 0;
drhfd357972005-09-09 01:33:19 +00002126 if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 0;
danielk19774adee202004-05-08 08:23:19 +00002127 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
2128 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00002129 if( pA->pList ){
2130 if( pB->pList==0 ) return 0;
2131 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
2132 for(i=0; i<pA->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00002133 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00002134 return 0;
2135 }
2136 }
2137 }else if( pB->pList ){
2138 return 0;
2139 }
2140 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00002141 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drh22827922000-06-06 17:27:05 +00002142 if( pA->token.z ){
2143 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00002144 if( pB->token.n!=pA->token.n ) return 0;
drh2646da72005-12-09 20:02:05 +00002145 if( sqlite3StrNICmp((char*)pA->token.z,(char*)pB->token.z,pB->token.n)!=0 ){
2146 return 0;
2147 }
drh22827922000-06-06 17:27:05 +00002148 }
2149 return 1;
2150}
2151
drh13449892005-09-07 21:22:45 +00002152
drh22827922000-06-06 17:27:05 +00002153/*
drh13449892005-09-07 21:22:45 +00002154** Add a new element to the pAggInfo->aCol[] array. Return the index of
2155** the new element. Return a negative number if malloc fails.
drh22827922000-06-06 17:27:05 +00002156*/
drh13449892005-09-07 21:22:45 +00002157static int addAggInfoColumn(AggInfo *pInfo){
2158 int i;
2159 i = sqlite3ArrayAllocate((void**)&pInfo->aCol, sizeof(pInfo->aCol[0]), 3);
2160 if( i<0 ){
2161 return -1;
drh22827922000-06-06 17:27:05 +00002162 }
drh13449892005-09-07 21:22:45 +00002163 return i;
2164}
2165
2166/*
2167** Add a new element to the pAggInfo->aFunc[] array. Return the index of
2168** the new element. Return a negative number if malloc fails.
2169*/
2170static int addAggInfoFunc(AggInfo *pInfo){
2171 int i;
2172 i = sqlite3ArrayAllocate((void**)&pInfo->aFunc, sizeof(pInfo->aFunc[0]), 2);
2173 if( i<0 ){
2174 return -1;
2175 }
2176 return i;
2177}
drh22827922000-06-06 17:27:05 +00002178
2179/*
drh626a8792005-01-17 22:08:19 +00002180** This is an xFunc for walkExprTree() used to implement
2181** sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates
2182** for additional information.
drh22827922000-06-06 17:27:05 +00002183**
drh626a8792005-01-17 22:08:19 +00002184** This routine analyzes the aggregate function at pExpr.
drh22827922000-06-06 17:27:05 +00002185*/
drh626a8792005-01-17 22:08:19 +00002186static int analyzeAggregate(void *pArg, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00002187 int i;
danielk1977a58fdfb2005-02-08 07:50:40 +00002188 NameContext *pNC = (NameContext *)pArg;
2189 Parse *pParse = pNC->pParse;
2190 SrcList *pSrcList = pNC->pSrcList;
drh13449892005-09-07 21:22:45 +00002191 AggInfo *pAggInfo = pNC->pAggInfo;
2192
drh22827922000-06-06 17:27:05 +00002193
drh22827922000-06-06 17:27:05 +00002194 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00002195 case TK_COLUMN: {
drh13449892005-09-07 21:22:45 +00002196 /* Check to see if the column is in one of the tables in the FROM
2197 ** clause of the aggregate query */
2198 if( pSrcList ){
2199 struct SrcList_item *pItem = pSrcList->a;
2200 for(i=0; i<pSrcList->nSrc; i++, pItem++){
2201 struct AggInfo_col *pCol;
2202 if( pExpr->iTable==pItem->iCursor ){
2203 /* If we reach this point, it means that pExpr refers to a table
2204 ** that is in the FROM clause of the aggregate query.
2205 **
2206 ** Make an entry for the column in pAggInfo->aCol[] if there
2207 ** is not an entry there already.
2208 */
2209 pCol = pAggInfo->aCol;
2210 for(i=0; i<pAggInfo->nColumn; i++, pCol++){
2211 if( pCol->iTable==pExpr->iTable &&
2212 pCol->iColumn==pExpr->iColumn ){
2213 break;
2214 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002215 }
drh13449892005-09-07 21:22:45 +00002216 if( i>=pAggInfo->nColumn && (i = addAggInfoColumn(pAggInfo))>=0 ){
2217 pCol = &pAggInfo->aCol[i];
2218 pCol->iTable = pExpr->iTable;
2219 pCol->iColumn = pExpr->iColumn;
2220 pCol->iMem = pParse->nMem++;
2221 pCol->iSorterColumn = -1;
drh5774b802005-09-07 22:48:16 +00002222 pCol->pExpr = pExpr;
drh13449892005-09-07 21:22:45 +00002223 if( pAggInfo->pGroupBy ){
2224 int j, n;
2225 ExprList *pGB = pAggInfo->pGroupBy;
2226 struct ExprList_item *pTerm = pGB->a;
2227 n = pGB->nExpr;
2228 for(j=0; j<n; j++, pTerm++){
2229 Expr *pE = pTerm->pExpr;
2230 if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable &&
2231 pE->iColumn==pExpr->iColumn ){
2232 pCol->iSorterColumn = j;
2233 break;
2234 }
2235 }
2236 }
2237 if( pCol->iSorterColumn<0 ){
2238 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
2239 }
2240 }
2241 /* There is now an entry for pExpr in pAggInfo->aCol[] (either
2242 ** because it was there before or because we just created it).
2243 ** Convert the pExpr to be a TK_AGG_COLUMN referring to that
2244 ** pAggInfo->aCol[] entry.
2245 */
2246 pExpr->pAggInfo = pAggInfo;
2247 pExpr->op = TK_AGG_COLUMN;
2248 pExpr->iAgg = i;
2249 break;
2250 } /* endif pExpr->iTable==pItem->iCursor */
2251 } /* end loop over pSrcList */
drh22827922000-06-06 17:27:05 +00002252 }
drh626a8792005-01-17 22:08:19 +00002253 return 1;
drh22827922000-06-06 17:27:05 +00002254 }
2255 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00002256 /* The pNC->nDepth==0 test causes aggregate functions in subqueries
2257 ** to be ignored */
danielk1977a58fdfb2005-02-08 07:50:40 +00002258 if( pNC->nDepth==0 ){
drh13449892005-09-07 21:22:45 +00002259 /* Check to see if pExpr is a duplicate of another aggregate
2260 ** function that is already in the pAggInfo structure
2261 */
2262 struct AggInfo_func *pItem = pAggInfo->aFunc;
2263 for(i=0; i<pAggInfo->nFunc; i++, pItem++){
2264 if( sqlite3ExprCompare(pItem->pExpr, pExpr) ){
danielk1977a58fdfb2005-02-08 07:50:40 +00002265 break;
2266 }
drh22827922000-06-06 17:27:05 +00002267 }
drh13449892005-09-07 21:22:45 +00002268 if( i>=pAggInfo->nFunc ){
2269 /* pExpr is original. Make a new entry in pAggInfo->aFunc[]
2270 */
danielk197714db2662006-01-09 16:12:04 +00002271 u8 enc = ENC(pParse->db);
drh13449892005-09-07 21:22:45 +00002272 i = addAggInfoFunc(pAggInfo);
2273 if( i>=0 ){
2274 pItem = &pAggInfo->aFunc[i];
2275 pItem->pExpr = pExpr;
2276 pItem->iMem = pParse->nMem++;
2277 pItem->pFunc = sqlite3FindFunction(pParse->db,
drh2646da72005-12-09 20:02:05 +00002278 (char*)pExpr->token.z, pExpr->token.n,
drh13449892005-09-07 21:22:45 +00002279 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
drhfd357972005-09-09 01:33:19 +00002280 if( pExpr->flags & EP_Distinct ){
2281 pItem->iDistinct = pParse->nTab++;
2282 }else{
2283 pItem->iDistinct = -1;
2284 }
drh13449892005-09-07 21:22:45 +00002285 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002286 }
drh13449892005-09-07 21:22:45 +00002287 /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
2288 */
danielk1977a58fdfb2005-02-08 07:50:40 +00002289 pExpr->iAgg = i;
drh13449892005-09-07 21:22:45 +00002290 pExpr->pAggInfo = pAggInfo;
danielk1977a58fdfb2005-02-08 07:50:40 +00002291 return 1;
drh22827922000-06-06 17:27:05 +00002292 }
drh22827922000-06-06 17:27:05 +00002293 }
2294 }
drh13449892005-09-07 21:22:45 +00002295
2296 /* Recursively walk subqueries looking for TK_COLUMN nodes that need
2297 ** to be changed to TK_AGG_COLUMN. But increment nDepth so that
2298 ** TK_AGG_FUNCTION nodes in subqueries will be unchanged.
2299 */
danielk1977a58fdfb2005-02-08 07:50:40 +00002300 if( pExpr->pSelect ){
2301 pNC->nDepth++;
2302 walkSelectExpr(pExpr->pSelect, analyzeAggregate, pNC);
2303 pNC->nDepth--;
2304 }
drh626a8792005-01-17 22:08:19 +00002305 return 0;
2306}
2307
2308/*
2309** Analyze the given expression looking for aggregate functions and
2310** for variables that need to be added to the pParse->aAgg[] array.
2311** Make additional entries to the pParse->aAgg[] array as necessary.
2312**
2313** This routine should only be called after the expression has been
2314** analyzed by sqlite3ExprResolveNames().
2315**
2316** If errors are seen, leave an error message in zErrMsg and return
2317** the number of errors.
2318*/
danielk1977a58fdfb2005-02-08 07:50:40 +00002319int sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
2320 int nErr = pNC->pParse->nErr;
2321 walkExprTree(pExpr, analyzeAggregate, pNC);
2322 return pNC->pParse->nErr - nErr;
drh22827922000-06-06 17:27:05 +00002323}
drh5d9a4af2005-08-30 00:54:01 +00002324
2325/*
2326** Call sqlite3ExprAnalyzeAggregates() for every expression in an
2327** expression list. Return the number of errors.
2328**
2329** If an error is found, the analysis is cut short.
2330*/
2331int sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){
2332 struct ExprList_item *pItem;
2333 int i;
2334 int nErr = 0;
2335 if( pList ){
2336 for(pItem=pList->a, i=0; nErr==0 && i<pList->nExpr; i++, pItem++){
2337 nErr += sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
2338 }
2339 }
2340 return nErr;
2341}