blob: daf7dc49299d85fdfcfca649aee1a659b8bb5a5a [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**
drhb7f91642004-10-31 02:22:47 +000015** $Id: expr.c,v 1.167 2004/10/31 02:22:49 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){
danielk1977a37cdde2004-05-16 11:15:36 +000037 if( pExpr->op==TK_AS ){
danielk1977bf3b7212004-05-18 10:06:24 +000038 return sqlite3ExprAffinity(pExpr->pLeft);
danielk1977a37cdde2004-05-16 11:15:36 +000039 }
40 if( pExpr->op==TK_SELECT ){
danielk1977bf3b7212004-05-18 10:06:24 +000041 return sqlite3ExprAffinity(pExpr->pSelect->pEList->a[0].pExpr);
danielk1977a37cdde2004-05-16 11:15:36 +000042 }
43 return pExpr->affinity;
44}
45
drh53db1452004-05-20 13:54:53 +000046/*
danielk19770202b292004-06-09 09:55:16 +000047** Return the default collation sequence for the expression pExpr. If
48** there is no default collation type, return 0.
49*/
danielk19777cedc8d2004-06-10 10:50:08 +000050CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
51 CollSeq *pColl = 0;
danielk19770202b292004-06-09 09:55:16 +000052 if( pExpr ){
danielk19777cedc8d2004-06-10 10:50:08 +000053 pColl = pExpr->pColl;
54 if( pExpr->op==TK_AS && !pColl ){
55 return sqlite3ExprCollSeq(pParse, pExpr->pLeft);
danielk19770202b292004-06-09 09:55:16 +000056 }
57 }
danielk19777cedc8d2004-06-10 10:50:08 +000058 if( sqlite3CheckCollSeq(pParse, pColl) ){
59 pColl = 0;
60 }
61 return pColl;
danielk19770202b292004-06-09 09:55:16 +000062}
63
64/*
drh53db1452004-05-20 13:54:53 +000065** pExpr is the left operand of a comparison operator. aff2 is the
66** type affinity of the right operand. This routine returns the
67** type affinity that should be used for the comparison operator.
68*/
danielk1977e014a832004-05-17 10:48:57 +000069char sqlite3CompareAffinity(Expr *pExpr, char aff2){
danielk1977bf3b7212004-05-18 10:06:24 +000070 char aff1 = sqlite3ExprAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +000071 if( aff1 && aff2 ){
72 /* Both sides of the comparison are columns. If one has numeric or
73 ** integer affinity, use that. Otherwise use no affinity.
74 */
75 if( aff1==SQLITE_AFF_INTEGER || aff2==SQLITE_AFF_INTEGER ){
76 return SQLITE_AFF_INTEGER;
77 }else if( aff1==SQLITE_AFF_NUMERIC || aff2==SQLITE_AFF_NUMERIC ){
78 return SQLITE_AFF_NUMERIC;
79 }else{
80 return SQLITE_AFF_NONE;
81 }
82 }else if( !aff1 && !aff2 ){
drh5f6a87b2004-07-19 00:39:45 +000083 /* Neither side of the comparison is a column. Compare the
84 ** results directly.
danielk1977e014a832004-05-17 10:48:57 +000085 */
drh5f6a87b2004-07-19 00:39:45 +000086 /* return SQLITE_AFF_NUMERIC; // Ticket #805 */
87 return SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +000088 }else{
89 /* One side is a column, the other is not. Use the columns affinity. */
90 return (aff1 + aff2);
91 }
92}
93
drh53db1452004-05-20 13:54:53 +000094/*
95** pExpr is a comparison operator. Return the type affinity that should
96** be applied to both operands prior to doing the comparison.
97*/
danielk1977e014a832004-05-17 10:48:57 +000098static char comparisonAffinity(Expr *pExpr){
99 char aff;
100 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
101 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
102 pExpr->op==TK_NE );
103 assert( pExpr->pLeft );
danielk1977bf3b7212004-05-18 10:06:24 +0000104 aff = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +0000105 if( pExpr->pRight ){
106 aff = sqlite3CompareAffinity(pExpr->pRight, aff);
107 }
108 else if( pExpr->pSelect ){
109 aff = sqlite3CompareAffinity(pExpr->pSelect->pEList->a[0].pExpr, aff);
110 }
111 else if( !aff ){
112 aff = SQLITE_AFF_NUMERIC;
113 }
114 return aff;
115}
116
117/*
118** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
119** idx_affinity is the affinity of an indexed column. Return true
120** if the index with affinity idx_affinity may be used to implement
121** the comparison in pExpr.
122*/
123int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){
124 char aff = comparisonAffinity(pExpr);
125 return
126 (aff==SQLITE_AFF_NONE) ||
127 (aff==SQLITE_AFF_NUMERIC && idx_affinity==SQLITE_AFF_INTEGER) ||
128 (aff==SQLITE_AFF_INTEGER && idx_affinity==SQLITE_AFF_NUMERIC) ||
129 (aff==idx_affinity);
130}
131
danielk1977a37cdde2004-05-16 11:15:36 +0000132/*
133** Return the P1 value that should be used for a binary comparison
134** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
135** If jumpIfNull is true, then set the low byte of the returned
136** P1 value to tell the opcode to jump if either expression
137** evaluates to NULL.
138*/
danielk1977e014a832004-05-17 10:48:57 +0000139static int binaryCompareP1(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){
danielk1977bf3b7212004-05-18 10:06:24 +0000140 char aff = sqlite3ExprAffinity(pExpr2);
danielk1977e014a832004-05-17 10:48:57 +0000141 return (((int)sqlite3CompareAffinity(pExpr1, aff))<<8)+(jumpIfNull?1:0);
danielk1977a37cdde2004-05-16 11:15:36 +0000142}
143
drha2e00042002-01-22 03:13:42 +0000144/*
danielk19770202b292004-06-09 09:55:16 +0000145** Return a pointer to the collation sequence that should be used by
146** a binary comparison operator comparing pLeft and pRight.
147**
148** If the left hand expression has a collating sequence type, then it is
149** used. Otherwise the collation sequence for the right hand expression
150** is used, or the default (BINARY) if neither expression has a collating
151** type.
152*/
danielk19777cedc8d2004-06-10 10:50:08 +0000153static CollSeq* binaryCompareCollSeq(Parse *pParse, Expr *pLeft, Expr *pRight){
154 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pLeft);
danielk19770202b292004-06-09 09:55:16 +0000155 if( !pColl ){
danielk19777cedc8d2004-06-10 10:50:08 +0000156 pColl = sqlite3ExprCollSeq(pParse, pRight);
danielk19770202b292004-06-09 09:55:16 +0000157 }
158 return pColl;
159}
160
161/*
drhbe5c89a2004-07-26 00:31:09 +0000162** Generate code for a comparison operator.
163*/
164static int codeCompare(
165 Parse *pParse, /* The parsing (and code generating) context */
166 Expr *pLeft, /* The left operand */
167 Expr *pRight, /* The right operand */
168 int opcode, /* The comparison opcode */
169 int dest, /* Jump here if true. */
170 int jumpIfNull /* If true, jump if either operand is NULL */
171){
172 int p1 = binaryCompareP1(pLeft, pRight, jumpIfNull);
173 CollSeq *p3 = binaryCompareCollSeq(pParse, pLeft, pRight);
drh94a11212004-09-25 13:12:14 +0000174 return sqlite3VdbeOp3(pParse->pVdbe, opcode, p1, dest, (void*)p3, P3_COLLSEQ);
drhbe5c89a2004-07-26 00:31:09 +0000175}
176
177/*
drha76b5df2002-02-23 02:32:10 +0000178** Construct a new expression node and return a pointer to it. Memory
179** for this node is obtained from sqliteMalloc(). The calling function
180** is responsible for making sure the node eventually gets freed.
181*/
danielk19774adee202004-05-08 08:23:19 +0000182Expr *sqlite3Expr(int op, Expr *pLeft, Expr *pRight, Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000183 Expr *pNew;
184 pNew = sqliteMalloc( sizeof(Expr) );
185 if( pNew==0 ){
drh4efc4752004-01-16 15:55:37 +0000186 /* When malloc fails, we leak memory from pLeft and pRight */
drha76b5df2002-02-23 02:32:10 +0000187 return 0;
188 }
189 pNew->op = op;
190 pNew->pLeft = pLeft;
191 pNew->pRight = pRight;
192 if( pToken ){
drh4b59ab52002-08-24 18:24:51 +0000193 assert( pToken->dyn==0 );
drh145716b2004-09-24 12:24:06 +0000194 pNew->span = pNew->token = *pToken;
195 }else if( pLeft && pRight ){
196 sqlite3ExprSpan(pNew, &pLeft->span, &pRight->span);
drha76b5df2002-02-23 02:32:10 +0000197 }
drha76b5df2002-02-23 02:32:10 +0000198 return pNew;
199}
200
201/*
drh91bb0ee2004-09-01 03:06:34 +0000202** Join two expressions using an AND operator. If either expression is
203** NULL, then just return the other expression.
204*/
205Expr *sqlite3ExprAnd(Expr *pLeft, Expr *pRight){
206 if( pLeft==0 ){
207 return pRight;
208 }else if( pRight==0 ){
209 return pLeft;
210 }else{
211 return sqlite3Expr(TK_AND, pLeft, pRight, 0);
212 }
213}
214
215/*
drh6977fea2002-10-22 23:38:04 +0000216** Set the Expr.span field of the given expression to span all
drha76b5df2002-02-23 02:32:10 +0000217** text between the two given tokens.
218*/
danielk19774adee202004-05-08 08:23:19 +0000219void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
drh4efc4752004-01-16 15:55:37 +0000220 assert( pRight!=0 );
221 assert( pLeft!=0 );
drh71c697e2004-08-08 23:39:19 +0000222 if( !sqlite3_malloc_failed && pRight->z && pLeft->z ){
drhad6d9462004-09-19 02:15:24 +0000223 assert( pLeft->dyn==0 || pLeft->z[pLeft->n]==0 );
drh145716b2004-09-24 12:24:06 +0000224 if( pLeft->dyn==0 && pRight->dyn==0 ){
drh6977fea2002-10-22 23:38:04 +0000225 pExpr->span.z = pLeft->z;
226 pExpr->span.n = pRight->n + Addr(pRight->z) - Addr(pLeft->z);
drh4b59ab52002-08-24 18:24:51 +0000227 }else{
drh6977fea2002-10-22 23:38:04 +0000228 pExpr->span.z = 0;
drh4b59ab52002-08-24 18:24:51 +0000229 }
drha76b5df2002-02-23 02:32:10 +0000230 }
231}
232
233/*
234** Construct a new expression node for a function with multiple
235** arguments.
236*/
danielk19774adee202004-05-08 08:23:19 +0000237Expr *sqlite3ExprFunction(ExprList *pList, Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000238 Expr *pNew;
239 pNew = sqliteMalloc( sizeof(Expr) );
240 if( pNew==0 ){
danielk19774adee202004-05-08 08:23:19 +0000241 /* sqlite3ExprListDelete(pList); // Leak pList when malloc fails */
drha76b5df2002-02-23 02:32:10 +0000242 return 0;
243 }
244 pNew->op = TK_FUNCTION;
245 pNew->pList = pList;
246 if( pToken ){
drh4b59ab52002-08-24 18:24:51 +0000247 assert( pToken->dyn==0 );
drha76b5df2002-02-23 02:32:10 +0000248 pNew->token = *pToken;
249 }else{
250 pNew->token.z = 0;
drha76b5df2002-02-23 02:32:10 +0000251 }
drh6977fea2002-10-22 23:38:04 +0000252 pNew->span = pNew->token;
drha76b5df2002-02-23 02:32:10 +0000253 return pNew;
254}
255
256/*
drhfa6bc002004-09-07 16:19:52 +0000257** Assign a variable number to an expression that encodes a wildcard
258** in the original SQL statement.
259**
260** Wildcards consisting of a single "?" are assigned the next sequential
261** variable number.
262**
263** Wildcards of the form "?nnn" are assigned the number "nnn". We make
264** sure "nnn" is not too be to avoid a denial of service attack when
265** the SQL statement comes from an external source.
266**
267** Wildcards of the form ":aaa" or "$aaa" are assigned the same number
268** as the previous instance of the same wildcard. Or if this is the first
269** instance of the wildcard, the next sequenial variable number is
270** assigned.
271*/
272void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){
273 Token *pToken;
274 if( pExpr==0 ) return;
275 pToken = &pExpr->token;
276 assert( pToken->n>=1 );
277 assert( pToken->z!=0 );
278 assert( pToken->z[0]!=0 );
279 if( pToken->n==1 ){
280 /* Wildcard of the form "?". Assign the next variable number */
281 pExpr->iTable = ++pParse->nVar;
282 }else if( pToken->z[0]=='?' ){
283 /* Wildcard of the form "?nnn". Convert "nnn" to an integer and
284 ** use it as the variable number */
285 int i;
286 pExpr->iTable = i = atoi(&pToken->z[1]);
287 if( i<1 || i>SQLITE_MAX_VARIABLE_NUMBER ){
288 sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
289 SQLITE_MAX_VARIABLE_NUMBER);
290 }
291 if( i>pParse->nVar ){
292 pParse->nVar = i;
293 }
294 }else{
295 /* Wildcards of the form ":aaa" or "$aaa". Reuse the same variable
296 ** number as the prior appearance of the same name, or if the name
297 ** has never appeared before, reuse the same variable number
298 */
299 int i, n;
300 n = pToken->n;
301 for(i=0; i<pParse->nVarExpr; i++){
302 Expr *pE;
303 if( (pE = pParse->apVarExpr[i])!=0
304 && pE->token.n==n
305 && memcmp(pE->token.z, pToken->z, n)==0 ){
306 pExpr->iTable = pE->iTable;
307 break;
308 }
309 }
310 if( i>=pParse->nVarExpr ){
311 pExpr->iTable = ++pParse->nVar;
312 if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){
313 pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10;
314 pParse->apVarExpr = sqliteRealloc(pParse->apVarExpr,
315 pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0]) );
316 }
317 if( !sqlite3_malloc_failed ){
318 assert( pParse->apVarExpr!=0 );
319 pParse->apVarExpr[pParse->nVarExpr++] = pExpr;
320 }
321 }
322 }
323}
324
325/*
drha2e00042002-01-22 03:13:42 +0000326** Recursively delete an expression tree.
327*/
danielk19774adee202004-05-08 08:23:19 +0000328void sqlite3ExprDelete(Expr *p){
drha2e00042002-01-22 03:13:42 +0000329 if( p==0 ) return;
drh4efc4752004-01-16 15:55:37 +0000330 if( p->span.dyn ) sqliteFree((char*)p->span.z);
331 if( p->token.dyn ) sqliteFree((char*)p->token.z);
danielk19774adee202004-05-08 08:23:19 +0000332 sqlite3ExprDelete(p->pLeft);
333 sqlite3ExprDelete(p->pRight);
334 sqlite3ExprListDelete(p->pList);
335 sqlite3SelectDelete(p->pSelect);
drha2e00042002-01-22 03:13:42 +0000336 sqliteFree(p);
337}
338
drha76b5df2002-02-23 02:32:10 +0000339
340/*
drhff78bd22002-02-27 01:47:11 +0000341** The following group of routines make deep copies of expressions,
342** expression lists, ID lists, and select statements. The copies can
343** be deleted (by being passed to their respective ...Delete() routines)
344** without effecting the originals.
345**
danielk19774adee202004-05-08 08:23:19 +0000346** The expression list, ID, and source lists return by sqlite3ExprListDup(),
347** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
drhad3cab52002-05-24 02:04:32 +0000348** by subsequent calls to sqlite*ListAppend() routines.
drhff78bd22002-02-27 01:47:11 +0000349**
drhad3cab52002-05-24 02:04:32 +0000350** Any tables that the SrcList might point to are not duplicated.
drhff78bd22002-02-27 01:47:11 +0000351*/
danielk19774adee202004-05-08 08:23:19 +0000352Expr *sqlite3ExprDup(Expr *p){
drhff78bd22002-02-27 01:47:11 +0000353 Expr *pNew;
354 if( p==0 ) return 0;
drhfcb78a42003-01-18 20:11:05 +0000355 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000356 if( pNew==0 ) return 0;
drh3b167c72002-06-28 12:18:47 +0000357 memcpy(pNew, p, sizeof(*pNew));
drh6977fea2002-10-22 23:38:04 +0000358 if( p->token.z!=0 ){
drh4b59ab52002-08-24 18:24:51 +0000359 pNew->token.z = sqliteStrDup(p->token.z);
360 pNew->token.dyn = 1;
361 }else{
drh4efc4752004-01-16 15:55:37 +0000362 assert( pNew->token.z==0 );
drh4b59ab52002-08-24 18:24:51 +0000363 }
drh6977fea2002-10-22 23:38:04 +0000364 pNew->span.z = 0;
danielk19774adee202004-05-08 08:23:19 +0000365 pNew->pLeft = sqlite3ExprDup(p->pLeft);
366 pNew->pRight = sqlite3ExprDup(p->pRight);
367 pNew->pList = sqlite3ExprListDup(p->pList);
368 pNew->pSelect = sqlite3SelectDup(p->pSelect);
drhff78bd22002-02-27 01:47:11 +0000369 return pNew;
370}
danielk19774adee202004-05-08 08:23:19 +0000371void sqlite3TokenCopy(Token *pTo, Token *pFrom){
drh4b59ab52002-08-24 18:24:51 +0000372 if( pTo->dyn ) sqliteFree((char*)pTo->z);
drh4b59ab52002-08-24 18:24:51 +0000373 if( pFrom->z ){
374 pTo->n = pFrom->n;
375 pTo->z = sqliteStrNDup(pFrom->z, pFrom->n);
376 pTo->dyn = 1;
377 }else{
drh4b59ab52002-08-24 18:24:51 +0000378 pTo->z = 0;
drh4b59ab52002-08-24 18:24:51 +0000379 }
380}
danielk19774adee202004-05-08 08:23:19 +0000381ExprList *sqlite3ExprListDup(ExprList *p){
drhff78bd22002-02-27 01:47:11 +0000382 ExprList *pNew;
drh145716b2004-09-24 12:24:06 +0000383 struct ExprList_item *pItem, *pOldItem;
drhff78bd22002-02-27 01:47:11 +0000384 int i;
385 if( p==0 ) return 0;
386 pNew = sqliteMalloc( sizeof(*pNew) );
387 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000388 pNew->nExpr = pNew->nAlloc = p->nExpr;
drh3e7bc9c2004-02-21 19:17:17 +0000389 pNew->a = pItem = sqliteMalloc( p->nExpr*sizeof(p->a[0]) );
danielk1977e0048402004-06-15 16:51:01 +0000390 if( pItem==0 ){
391 sqliteFree(pNew);
392 return 0;
393 }
drh145716b2004-09-24 12:24:06 +0000394 pOldItem = p->a;
395 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
drh4b59ab52002-08-24 18:24:51 +0000396 Expr *pNewExpr, *pOldExpr;
drh145716b2004-09-24 12:24:06 +0000397 pItem->pExpr = pNewExpr = sqlite3ExprDup(pOldExpr = pOldItem->pExpr);
drh6977fea2002-10-22 23:38:04 +0000398 if( pOldExpr->span.z!=0 && pNewExpr ){
399 /* Always make a copy of the span for top-level expressions in the
drh4b59ab52002-08-24 18:24:51 +0000400 ** expression list. The logic in SELECT processing that determines
401 ** the names of columns in the result set needs this information */
danielk19774adee202004-05-08 08:23:19 +0000402 sqlite3TokenCopy(&pNewExpr->span, &pOldExpr->span);
drh4b59ab52002-08-24 18:24:51 +0000403 }
drh1f3e9052002-10-31 00:09:39 +0000404 assert( pNewExpr==0 || pNewExpr->span.z!=0
danielk197724b03fd2004-05-10 10:34:34 +0000405 || pOldExpr->span.z==0 || sqlite3_malloc_failed );
drh145716b2004-09-24 12:24:06 +0000406 pItem->zName = sqliteStrDup(pOldItem->zName);
407 pItem->sortOrder = pOldItem->sortOrder;
408 pItem->isAgg = pOldItem->isAgg;
drh3e7bc9c2004-02-21 19:17:17 +0000409 pItem->done = 0;
drhff78bd22002-02-27 01:47:11 +0000410 }
411 return pNew;
412}
danielk19774adee202004-05-08 08:23:19 +0000413SrcList *sqlite3SrcListDup(SrcList *p){
drhad3cab52002-05-24 02:04:32 +0000414 SrcList *pNew;
415 int i;
drh113088e2003-03-20 01:16:58 +0000416 int nByte;
drhad3cab52002-05-24 02:04:32 +0000417 if( p==0 ) return 0;
drh113088e2003-03-20 01:16:58 +0000418 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
drh4efc4752004-01-16 15:55:37 +0000419 pNew = sqliteMallocRaw( nByte );
drhad3cab52002-05-24 02:04:32 +0000420 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000421 pNew->nSrc = pNew->nAlloc = p->nSrc;
drhad3cab52002-05-24 02:04:32 +0000422 for(i=0; i<p->nSrc; i++){
drh4efc4752004-01-16 15:55:37 +0000423 struct SrcList_item *pNewItem = &pNew->a[i];
424 struct SrcList_item *pOldItem = &p->a[i];
425 pNewItem->zDatabase = sqliteStrDup(pOldItem->zDatabase);
426 pNewItem->zName = sqliteStrDup(pOldItem->zName);
427 pNewItem->zAlias = sqliteStrDup(pOldItem->zAlias);
428 pNewItem->jointype = pOldItem->jointype;
429 pNewItem->iCursor = pOldItem->iCursor;
430 pNewItem->pTab = 0;
danielk19774adee202004-05-08 08:23:19 +0000431 pNewItem->pSelect = sqlite3SelectDup(pOldItem->pSelect);
432 pNewItem->pOn = sqlite3ExprDup(pOldItem->pOn);
433 pNewItem->pUsing = sqlite3IdListDup(pOldItem->pUsing);
drhad3cab52002-05-24 02:04:32 +0000434 }
435 return pNew;
436}
danielk19774adee202004-05-08 08:23:19 +0000437IdList *sqlite3IdListDup(IdList *p){
drhff78bd22002-02-27 01:47:11 +0000438 IdList *pNew;
439 int i;
440 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000441 pNew = sqliteMallocRaw( sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +0000442 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000443 pNew->nId = pNew->nAlloc = p->nId;
drh4efc4752004-01-16 15:55:37 +0000444 pNew->a = sqliteMallocRaw( p->nId*sizeof(p->a[0]) );
drhe4697f52002-05-23 02:09:03 +0000445 if( pNew->a==0 ) return 0;
drhff78bd22002-02-27 01:47:11 +0000446 for(i=0; i<p->nId; i++){
drh4efc4752004-01-16 15:55:37 +0000447 struct IdList_item *pNewItem = &pNew->a[i];
448 struct IdList_item *pOldItem = &p->a[i];
449 pNewItem->zName = sqliteStrDup(pOldItem->zName);
450 pNewItem->idx = pOldItem->idx;
drhff78bd22002-02-27 01:47:11 +0000451 }
452 return pNew;
453}
danielk19774adee202004-05-08 08:23:19 +0000454Select *sqlite3SelectDup(Select *p){
drhff78bd22002-02-27 01:47:11 +0000455 Select *pNew;
456 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000457 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000458 if( pNew==0 ) return 0;
459 pNew->isDistinct = p->isDistinct;
danielk19774adee202004-05-08 08:23:19 +0000460 pNew->pEList = sqlite3ExprListDup(p->pEList);
461 pNew->pSrc = sqlite3SrcListDup(p->pSrc);
462 pNew->pWhere = sqlite3ExprDup(p->pWhere);
463 pNew->pGroupBy = sqlite3ExprListDup(p->pGroupBy);
464 pNew->pHaving = sqlite3ExprDup(p->pHaving);
465 pNew->pOrderBy = sqlite3ExprListDup(p->pOrderBy);
drhff78bd22002-02-27 01:47:11 +0000466 pNew->op = p->op;
danielk19774adee202004-05-08 08:23:19 +0000467 pNew->pPrior = sqlite3SelectDup(p->pPrior);
drhff78bd22002-02-27 01:47:11 +0000468 pNew->nLimit = p->nLimit;
469 pNew->nOffset = p->nOffset;
470 pNew->zSelect = 0;
drh7b58dae2003-07-20 01:16:46 +0000471 pNew->iLimit = -1;
472 pNew->iOffset = -1;
danielk1977dc1bdc42004-06-11 10:51:27 +0000473 pNew->ppOpenTemp = 0;
drhff78bd22002-02-27 01:47:11 +0000474 return pNew;
475}
476
477
478/*
drha76b5df2002-02-23 02:32:10 +0000479** Add a new element to the end of an expression list. If pList is
480** initially NULL, then create a new expression list.
481*/
danielk19774adee202004-05-08 08:23:19 +0000482ExprList *sqlite3ExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){
drha76b5df2002-02-23 02:32:10 +0000483 if( pList==0 ){
484 pList = sqliteMalloc( sizeof(ExprList) );
485 if( pList==0 ){
danielk19774adee202004-05-08 08:23:19 +0000486 /* sqlite3ExprDelete(pExpr); // Leak memory if malloc fails */
drha76b5df2002-02-23 02:32:10 +0000487 return 0;
488 }
drh4efc4752004-01-16 15:55:37 +0000489 assert( pList->nAlloc==0 );
drha76b5df2002-02-23 02:32:10 +0000490 }
drh4305d102003-07-30 12:34:12 +0000491 if( pList->nAlloc<=pList->nExpr ){
drh4305d102003-07-30 12:34:12 +0000492 pList->nAlloc = pList->nAlloc*2 + 4;
drh4efc4752004-01-16 15:55:37 +0000493 pList->a = sqliteRealloc(pList->a, pList->nAlloc*sizeof(pList->a[0]));
494 if( pList->a==0 ){
danielk19774adee202004-05-08 08:23:19 +0000495 /* sqlite3ExprDelete(pExpr); // Leak memory if malloc fails */
drh4efc4752004-01-16 15:55:37 +0000496 pList->nExpr = pList->nAlloc = 0;
drha76b5df2002-02-23 02:32:10 +0000497 return pList;
498 }
drha76b5df2002-02-23 02:32:10 +0000499 }
drh4efc4752004-01-16 15:55:37 +0000500 assert( pList->a!=0 );
501 if( pExpr || pName ){
502 struct ExprList_item *pItem = &pList->a[pList->nExpr++];
503 memset(pItem, 0, sizeof(*pItem));
504 pItem->pExpr = pExpr;
drha99db3b2004-06-19 14:49:12 +0000505 pItem->zName = sqlite3NameFromToken(pName);
drha76b5df2002-02-23 02:32:10 +0000506 }
507 return pList;
508}
509
510/*
511** Delete an entire expression list.
512*/
danielk19774adee202004-05-08 08:23:19 +0000513void sqlite3ExprListDelete(ExprList *pList){
drha76b5df2002-02-23 02:32:10 +0000514 int i;
drhbe5c89a2004-07-26 00:31:09 +0000515 struct ExprList_item *pItem;
drha76b5df2002-02-23 02:32:10 +0000516 if( pList==0 ) return;
drh1bdd9b52004-04-23 17:04:44 +0000517 assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
518 assert( pList->nExpr<=pList->nAlloc );
drhbe5c89a2004-07-26 00:31:09 +0000519 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
520 sqlite3ExprDelete(pItem->pExpr);
521 sqliteFree(pItem->zName);
drha76b5df2002-02-23 02:32:10 +0000522 }
523 sqliteFree(pList->a);
524 sqliteFree(pList);
525}
526
527/*
drhfef52082000-06-06 01:50:43 +0000528** Walk an expression tree. Return 1 if the expression is constant
529** and 0 if it involves variables.
drh23989372002-05-21 13:43:04 +0000530**
531** For the purposes of this function, a double-quoted string (ex: "abc")
532** is considered a variable but a single-quoted string (ex: 'abc') is
533** a constant.
drhfef52082000-06-06 01:50:43 +0000534*/
danielk19774adee202004-05-08 08:23:19 +0000535int sqlite3ExprIsConstant(Expr *p){
drhfef52082000-06-06 01:50:43 +0000536 switch( p->op ){
537 case TK_ID:
drh967e8b72000-06-21 13:59:10 +0000538 case TK_COLUMN:
drhfef52082000-06-06 01:50:43 +0000539 case TK_DOT:
drh7bdc0c12003-04-19 17:27:24 +0000540 case TK_FUNCTION:
drhfef52082000-06-06 01:50:43 +0000541 return 0;
drh7bdc0c12003-04-19 17:27:24 +0000542 case TK_NULL:
drh23989372002-05-21 13:43:04 +0000543 case TK_STRING:
danielk1977c572ef72004-05-27 09:28:41 +0000544 case TK_BLOB:
drh92086432002-01-22 14:11:29 +0000545 case TK_INTEGER:
546 case TK_FLOAT:
drh50457892003-09-06 01:10:47 +0000547 case TK_VARIABLE:
drh92086432002-01-22 14:11:29 +0000548 return 1;
drhfef52082000-06-06 01:50:43 +0000549 default: {
danielk19774adee202004-05-08 08:23:19 +0000550 if( p->pLeft && !sqlite3ExprIsConstant(p->pLeft) ) return 0;
551 if( p->pRight && !sqlite3ExprIsConstant(p->pRight) ) return 0;
drhfef52082000-06-06 01:50:43 +0000552 if( p->pList ){
553 int i;
554 for(i=0; i<p->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +0000555 if( !sqlite3ExprIsConstant(p->pList->a[i].pExpr) ) return 0;
drhfef52082000-06-06 01:50:43 +0000556 }
557 }
drh92086432002-01-22 14:11:29 +0000558 return p->pLeft!=0 || p->pRight!=0 || (p->pList && p->pList->nExpr>0);
drhfef52082000-06-06 01:50:43 +0000559 }
560 }
drh92086432002-01-22 14:11:29 +0000561 return 0;
drhfef52082000-06-06 01:50:43 +0000562}
563
564/*
drh202b2df2004-01-06 01:13:46 +0000565** If the given expression codes a constant integer that is small enough
566** to fit in a 32-bit integer, return 1 and put the value of the integer
567** in *pValue. If the expression is not an integer or if it is too big
568** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
drhe4de1fe2002-06-02 16:09:01 +0000569*/
danielk19774adee202004-05-08 08:23:19 +0000570int sqlite3ExprIsInteger(Expr *p, int *pValue){
drhe4de1fe2002-06-02 16:09:01 +0000571 switch( p->op ){
572 case TK_INTEGER: {
drhfec19aa2004-05-19 20:41:03 +0000573 if( sqlite3GetInt32(p->token.z, pValue) ){
drh202b2df2004-01-06 01:13:46 +0000574 return 1;
575 }
576 break;
drhe4de1fe2002-06-02 16:09:01 +0000577 }
578 case TK_STRING: {
drh4c755c02004-08-08 20:22:17 +0000579 const u8 *z = (u8*)p->token.z;
drhe4de1fe2002-06-02 16:09:01 +0000580 int n = p->token.n;
drhbd790ee2002-06-02 18:22:06 +0000581 if( n>0 && z[0]=='-' ){ z++; n--; }
drhe4de1fe2002-06-02 16:09:01 +0000582 while( n>0 && *z && isdigit(*z) ){ z++; n--; }
drhfec19aa2004-05-19 20:41:03 +0000583 if( n==0 && sqlite3GetInt32(p->token.z, pValue) ){
drhe4de1fe2002-06-02 16:09:01 +0000584 return 1;
585 }
586 break;
587 }
drh4b59ab52002-08-24 18:24:51 +0000588 case TK_UPLUS: {
danielk19774adee202004-05-08 08:23:19 +0000589 return sqlite3ExprIsInteger(p->pLeft, pValue);
drh4b59ab52002-08-24 18:24:51 +0000590 }
drhe4de1fe2002-06-02 16:09:01 +0000591 case TK_UMINUS: {
592 int v;
danielk19774adee202004-05-08 08:23:19 +0000593 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
drhe4de1fe2002-06-02 16:09:01 +0000594 *pValue = -v;
595 return 1;
596 }
597 break;
598 }
599 default: break;
600 }
601 return 0;
602}
603
604/*
drhc4a3c772001-04-04 11:48:57 +0000605** Return TRUE if the given string is a row-id column name.
606*/
danielk19774adee202004-05-08 08:23:19 +0000607int sqlite3IsRowid(const char *z){
608 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
609 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
610 if( sqlite3StrICmp(z, "OID")==0 ) return 1;
drhc4a3c772001-04-04 11:48:57 +0000611 return 0;
612}
613
614/*
drh8141f612004-01-25 22:44:58 +0000615** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
616** that name in the set of source tables in pSrcList and make the pExpr
617** expression node refer back to that source column. The following changes
618** are made to pExpr:
619**
620** pExpr->iDb Set the index in db->aDb[] of the database holding
621** the table.
622** pExpr->iTable Set to the cursor number for the table obtained
623** from pSrcList.
624** pExpr->iColumn Set to the column number within the table.
drh8141f612004-01-25 22:44:58 +0000625** pExpr->op Set to TK_COLUMN.
626** pExpr->pLeft Any expression this points to is deleted
627** pExpr->pRight Any expression this points to is deleted.
628**
629** The pDbToken is the name of the database (the "X"). This value may be
630** NULL meaning that name is of the form Y.Z or Z. Any available database
631** can be used. The pTableToken is the name of the table (the "Y"). This
632** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it
633** means that the form of the name is Z and that columns from any table
634** can be used.
635**
636** If the name cannot be resolved unambiguously, leave an error message
637** in pParse and return non-zero. Return zero on success.
638*/
639static int lookupName(
640 Parse *pParse, /* The parsing context */
641 Token *pDbToken, /* Name of the database containing table, or NULL */
642 Token *pTableToken, /* Name of table containing column, or NULL */
643 Token *pColumnToken, /* Name of the column. */
644 SrcList *pSrcList, /* List of tables used to resolve column names */
645 ExprList *pEList, /* List of expressions used to resolve "AS" */
646 Expr *pExpr /* Make this EXPR node point to the selected column */
647){
648 char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */
649 char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */
650 char *zCol = 0; /* Name of the column. The "Z" */
651 int i, j; /* Loop counters */
652 int cnt = 0; /* Number of matching column names */
653 int cntTab = 0; /* Number of matching table names */
drh9bb575f2004-09-06 17:24:11 +0000654 sqlite3 *db = pParse->db; /* The database */
drh8141f612004-01-25 22:44:58 +0000655
656 assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
drha99db3b2004-06-19 14:49:12 +0000657 zDb = sqlite3NameFromToken(pDbToken);
658 zTab = sqlite3NameFromToken(pTableToken);
659 zCol = sqlite3NameFromToken(pColumnToken);
danielk197724b03fd2004-05-10 10:34:34 +0000660 if( sqlite3_malloc_failed ){
drh8141f612004-01-25 22:44:58 +0000661 return 1; /* Leak memory (zDb and zTab) if malloc fails */
662 }
663 assert( zTab==0 || pEList==0 );
664
665 pExpr->iTable = -1;
666 for(i=0; i<pSrcList->nSrc; i++){
667 struct SrcList_item *pItem = &pSrcList->a[i];
668 Table *pTab = pItem->pTab;
669 Column *pCol;
670
671 if( pTab==0 ) continue;
672 assert( pTab->nCol>0 );
673 if( zTab ){
674 if( pItem->zAlias ){
675 char *zTabName = pItem->zAlias;
danielk19774adee202004-05-08 08:23:19 +0000676 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
drh8141f612004-01-25 22:44:58 +0000677 }else{
678 char *zTabName = pTab->zName;
danielk19774adee202004-05-08 08:23:19 +0000679 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
680 if( zDb!=0 && sqlite3StrICmp(db->aDb[pTab->iDb].zName, zDb)!=0 ){
drh8141f612004-01-25 22:44:58 +0000681 continue;
682 }
683 }
684 }
685 if( 0==(cntTab++) ){
686 pExpr->iTable = pItem->iCursor;
687 pExpr->iDb = pTab->iDb;
688 }
689 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
danielk19774adee202004-05-08 08:23:19 +0000690 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
drh8141f612004-01-25 22:44:58 +0000691 cnt++;
692 pExpr->iTable = pItem->iCursor;
693 pExpr->iDb = pTab->iDb;
694 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
695 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
danielk1977a37cdde2004-05-16 11:15:36 +0000696 pExpr->affinity = pTab->aCol[j].affinity;
danielk19770202b292004-06-09 09:55:16 +0000697 pExpr->pColl = pTab->aCol[j].pColl;
drh8141f612004-01-25 22:44:58 +0000698 break;
699 }
700 }
701 }
702
drhb7f91642004-10-31 02:22:47 +0000703#ifndef SQLITE_OMIT_TRIGGER
drh8141f612004-01-25 22:44:58 +0000704 /* If we have not already resolved the name, then maybe
705 ** it is a new.* or old.* trigger argument reference
706 */
707 if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
708 TriggerStack *pTriggerStack = pParse->trigStack;
709 Table *pTab = 0;
danielk19774adee202004-05-08 08:23:19 +0000710 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){
drh8141f612004-01-25 22:44:58 +0000711 pExpr->iTable = pTriggerStack->newIdx;
712 assert( pTriggerStack->pTab );
713 pTab = pTriggerStack->pTab;
danielk19774adee202004-05-08 08:23:19 +0000714 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab) == 0 ){
drh8141f612004-01-25 22:44:58 +0000715 pExpr->iTable = pTriggerStack->oldIdx;
716 assert( pTriggerStack->pTab );
717 pTab = pTriggerStack->pTab;
718 }
719
720 if( pTab ){
721 int j;
722 Column *pCol = pTab->aCol;
723
724 pExpr->iDb = pTab->iDb;
725 cntTab++;
726 for(j=0; j < pTab->nCol; j++, pCol++) {
danielk19774adee202004-05-08 08:23:19 +0000727 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
drh8141f612004-01-25 22:44:58 +0000728 cnt++;
729 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
danielk1977a37cdde2004-05-16 11:15:36 +0000730 pExpr->affinity = pTab->aCol[j].affinity;
danielk19770202b292004-06-09 09:55:16 +0000731 pExpr->pColl = pTab->aCol[j].pColl;
drh8141f612004-01-25 22:44:58 +0000732 break;
733 }
734 }
735 }
736 }
drhb7f91642004-10-31 02:22:47 +0000737#endif /* !defined(SQLITE_OMIT_TRIGGER) */
drh8141f612004-01-25 22:44:58 +0000738
739 /*
740 ** Perhaps the name is a reference to the ROWID
741 */
danielk19774adee202004-05-08 08:23:19 +0000742 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
drh8141f612004-01-25 22:44:58 +0000743 cnt = 1;
744 pExpr->iColumn = -1;
danielk1977a37cdde2004-05-16 11:15:36 +0000745 pExpr->affinity = SQLITE_AFF_INTEGER;
drh8141f612004-01-25 22:44:58 +0000746 }
747
748 /*
749 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
750 ** might refer to an result-set alias. This happens, for example, when
751 ** we are resolving names in the WHERE clause of the following command:
752 **
753 ** SELECT a+b AS x FROM table WHERE x<10;
754 **
755 ** In cases like this, replace pExpr with a copy of the expression that
756 ** forms the result set entry ("a+b" in the example) and return immediately.
757 ** Note that the expression in the result set should have already been
758 ** resolved by the time the WHERE clause is resolved.
759 */
760 if( cnt==0 && pEList!=0 ){
761 for(j=0; j<pEList->nExpr; j++){
762 char *zAs = pEList->a[j].zName;
danielk19774adee202004-05-08 08:23:19 +0000763 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
drh8141f612004-01-25 22:44:58 +0000764 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
765 pExpr->op = TK_AS;
766 pExpr->iColumn = j;
danielk19774adee202004-05-08 08:23:19 +0000767 pExpr->pLeft = sqlite3ExprDup(pEList->a[j].pExpr);
drh8141f612004-01-25 22:44:58 +0000768 sqliteFree(zCol);
769 assert( zTab==0 && zDb==0 );
770 return 0;
771 }
772 }
773 }
774
775 /*
776 ** If X and Y are NULL (in other words if only the column name Z is
777 ** supplied) and the value of Z is enclosed in double-quotes, then
778 ** Z is a string literal if it doesn't match any column names. In that
779 ** case, we need to return right away and not make any changes to
780 ** pExpr.
781 */
782 if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
783 sqliteFree(zCol);
784 return 0;
785 }
786
787 /*
788 ** cnt==0 means there was not match. cnt>1 means there were two or
789 ** more matches. Either way, we have an error.
790 */
791 if( cnt!=1 ){
792 char *z = 0;
793 char *zErr;
794 zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s";
795 if( zDb ){
danielk19774adee202004-05-08 08:23:19 +0000796 sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, 0);
drh8141f612004-01-25 22:44:58 +0000797 }else if( zTab ){
danielk19774adee202004-05-08 08:23:19 +0000798 sqlite3SetString(&z, zTab, ".", zCol, 0);
drh8141f612004-01-25 22:44:58 +0000799 }else{
800 z = sqliteStrDup(zCol);
801 }
danielk19774adee202004-05-08 08:23:19 +0000802 sqlite3ErrorMsg(pParse, zErr, z);
drh8141f612004-01-25 22:44:58 +0000803 sqliteFree(z);
804 }
805
806 /* Clean up and return
807 */
808 sqliteFree(zDb);
809 sqliteFree(zTab);
810 sqliteFree(zCol);
danielk19774adee202004-05-08 08:23:19 +0000811 sqlite3ExprDelete(pExpr->pLeft);
drh8141f612004-01-25 22:44:58 +0000812 pExpr->pLeft = 0;
danielk19774adee202004-05-08 08:23:19 +0000813 sqlite3ExprDelete(pExpr->pRight);
drh8141f612004-01-25 22:44:58 +0000814 pExpr->pRight = 0;
815 pExpr->op = TK_COLUMN;
danielk19774adee202004-05-08 08:23:19 +0000816 sqlite3AuthRead(pParse, pExpr, pSrcList);
drh8141f612004-01-25 22:44:58 +0000817 return cnt!=1;
818}
819
820/*
drhcce7d172000-05-31 15:34:51 +0000821** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +0000822** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +0000823** index to the table in the table list and a column offset. The
824** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
825** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +0000826** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +0000827** VDBE cursor number for a cursor that is pointing into the referenced
828** table. The Expr.iColumn value is changed to the index of the column
829** of the referenced table. The Expr.iColumn value for the special
830** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
831** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +0000832**
drhfef52082000-06-06 01:50:43 +0000833** We also check for instances of the IN operator. IN comes in two
834** forms:
835**
836** expr IN (exprlist)
837** and
838** expr IN (SELECT ...)
839**
840** The first form is handled by creating a set holding the list
841** of allowed values. The second form causes the SELECT to generate
842** a temporary table.
843**
844** This routine also looks for scalar SELECTs that are part of an expression.
drh19a775c2000-06-05 18:54:46 +0000845** If it finds any, it generates code to write the value of that select
846** into a memory cell.
drhcce7d172000-05-31 15:34:51 +0000847**
drh967e8b72000-06-21 13:59:10 +0000848** Unknown columns or tables provoke an error. The function returns
drhcce7d172000-05-31 15:34:51 +0000849** the number of errors seen and leaves an error message on pParse->zErrMsg.
850*/
danielk19774adee202004-05-08 08:23:19 +0000851int sqlite3ExprResolveIds(
drha2e00042002-01-22 03:13:42 +0000852 Parse *pParse, /* The parser context */
drh8141f612004-01-25 22:44:58 +0000853 SrcList *pSrcList, /* List of tables used to resolve column names */
drha2e00042002-01-22 03:13:42 +0000854 ExprList *pEList, /* List of expressions used to resolve "AS" */
855 Expr *pExpr /* The expression to be analyzed. */
856){
drh6a3ea0e2003-05-02 14:32:12 +0000857 int i;
858
drh8141f612004-01-25 22:44:58 +0000859 if( pExpr==0 || pSrcList==0 ) return 0;
860 for(i=0; i<pSrcList->nSrc; i++){
861 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab );
drh6a3ea0e2003-05-02 14:32:12 +0000862 }
drhcce7d172000-05-31 15:34:51 +0000863 switch( pExpr->op ){
drh23989372002-05-21 13:43:04 +0000864 /* Double-quoted strings (ex: "abc") are used as identifiers if
865 ** possible. Otherwise they remain as strings. Single-quoted
866 ** strings (ex: 'abc') are always string literals.
867 */
868 case TK_STRING: {
869 if( pExpr->token.z[0]=='\'' ) break;
870 /* Fall thru into the TK_ID case if this is a double-quoted string */
871 }
drh8141f612004-01-25 22:44:58 +0000872 /* A lone identifier is the name of a columnd.
drha2e00042002-01-22 03:13:42 +0000873 */
drhcce7d172000-05-31 15:34:51 +0000874 case TK_ID: {
drh8141f612004-01-25 22:44:58 +0000875 if( lookupName(pParse, 0, 0, &pExpr->token, pSrcList, pEList, pExpr) ){
drhcce7d172000-05-31 15:34:51 +0000876 return 1;
drhed6c8672003-01-12 18:02:16 +0000877 }
drhcce7d172000-05-31 15:34:51 +0000878 break;
879 }
880
drhd24cc422003-03-27 12:51:24 +0000881 /* A table name and column name: ID.ID
882 ** Or a database, table and column: ID.ID.ID
883 */
drhcce7d172000-05-31 15:34:51 +0000884 case TK_DOT: {
drh8141f612004-01-25 22:44:58 +0000885 Token *pColumn;
886 Token *pTable;
887 Token *pDb;
888 Expr *pRight;
drhcce7d172000-05-31 15:34:51 +0000889
drhcce7d172000-05-31 15:34:51 +0000890 pRight = pExpr->pRight;
drhd24cc422003-03-27 12:51:24 +0000891 if( pRight->op==TK_ID ){
drh8141f612004-01-25 22:44:58 +0000892 pDb = 0;
893 pTable = &pExpr->pLeft->token;
894 pColumn = &pRight->token;
drhd24cc422003-03-27 12:51:24 +0000895 }else{
drh8141f612004-01-25 22:44:58 +0000896 assert( pRight->op==TK_DOT );
897 pDb = &pExpr->pLeft->token;
898 pTable = &pRight->pLeft->token;
899 pColumn = &pRight->pRight->token;
drhd24cc422003-03-27 12:51:24 +0000900 }
drh8141f612004-01-25 22:44:58 +0000901 if( lookupName(pParse, pDb, pTable, pColumn, pSrcList, 0, pExpr) ){
drhdaffd0e2001-04-11 14:28:42 +0000902 return 1;
903 }
drhcce7d172000-05-31 15:34:51 +0000904 break;
905 }
906
drhfef52082000-06-06 01:50:43 +0000907 case TK_IN: {
danielk1977e014a832004-05-17 10:48:57 +0000908 char affinity;
danielk19774adee202004-05-08 08:23:19 +0000909 Vdbe *v = sqlite3GetVdbe(pParse);
drhd3d39e92004-05-20 22:16:29 +0000910 KeyInfo keyInfo;
danielk19770202b292004-06-09 09:55:16 +0000911 int addr; /* Address of OP_OpenTemp instruction */
drhd3d39e92004-05-20 22:16:29 +0000912
drhfef52082000-06-06 01:50:43 +0000913 if( v==0 ) return 1;
danielk19774adee202004-05-08 08:23:19 +0000914 if( sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pLeft) ){
drhcfab11b2000-06-06 03:31:22 +0000915 return 1;
916 }
danielk1977bf3b7212004-05-18 10:06:24 +0000917 affinity = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +0000918
919 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
920 ** expression it is handled the same way. A temporary table is
921 ** filled with single-field index keys representing the results
922 ** from the SELECT or the <exprlist>.
923 **
924 ** If the 'x' expression is a column value, or the SELECT...
925 ** statement returns a column value, then the affinity of that
926 ** column is used to build the index keys. If both 'x' and the
927 ** SELECT... statement are columns, then numeric affinity is used
928 ** if either column has NUMERIC or INTEGER affinity. If neither
929 ** 'x' nor the SELECT... statement are columns, then numeric affinity
930 ** is used.
931 */
932 pExpr->iTable = pParse->nTab++;
danielk19770202b292004-06-09 09:55:16 +0000933 addr = sqlite3VdbeAddOp(v, OP_OpenTemp, pExpr->iTable, 0);
drhd3d39e92004-05-20 22:16:29 +0000934 memset(&keyInfo, 0, sizeof(keyInfo));
935 keyInfo.nField = 1;
drhf3218fe2004-05-28 08:21:02 +0000936 sqlite3VdbeAddOp(v, OP_SetNumColumns, pExpr->iTable, 1);
danielk1977e014a832004-05-17 10:48:57 +0000937
drhfef52082000-06-06 01:50:43 +0000938 if( pExpr->pSelect ){
939 /* Case 1: expr IN (SELECT ...)
940 **
danielk1977e014a832004-05-17 10:48:57 +0000941 ** Generate code to write the results of the select into the temporary
942 ** table allocated and opened above.
drhfef52082000-06-06 01:50:43 +0000943 */
danielk1977e014a832004-05-17 10:48:57 +0000944 int iParm = pExpr->iTable + (((int)affinity)<<16);
drhbe5c89a2004-07-26 00:31:09 +0000945 ExprList *pEList;
danielk1977e014a832004-05-17 10:48:57 +0000946 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
danielk1977bf3b7212004-05-18 10:06:24 +0000947 sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +0000948 pEList = pExpr->pSelect->pEList;
949 if( pEList && pEList->nExpr>0 ){
danielk19777cedc8d2004-06-10 10:50:08 +0000950 keyInfo.aColl[0] = binaryCompareCollSeq(pParse, pExpr->pLeft,
drhbe5c89a2004-07-26 00:31:09 +0000951 pEList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +0000952 }
drhfef52082000-06-06 01:50:43 +0000953 }else if( pExpr->pList ){
954 /* Case 2: expr IN (exprlist)
955 **
danielk1977e014a832004-05-17 10:48:57 +0000956 ** For each expression, build an index key from the evaluation and
957 ** store it in the temporary table. If <expr> is a column, then use
958 ** that columns affinity when building index keys. If <expr> is not
959 ** a column, use numeric affinity.
drhfef52082000-06-06 01:50:43 +0000960 */
danielk1977e014a832004-05-17 10:48:57 +0000961 int i;
danielk1977e014a832004-05-17 10:48:57 +0000962 if( !affinity ){
963 affinity = SQLITE_AFF_NUMERIC;
964 }
danielk19770202b292004-06-09 09:55:16 +0000965 keyInfo.aColl[0] = pExpr->pLeft->pColl;
danielk1977e014a832004-05-17 10:48:57 +0000966
967 /* Loop through each expression in <exprlist>. */
drhfef52082000-06-06 01:50:43 +0000968 for(i=0; i<pExpr->pList->nExpr; i++){
969 Expr *pE2 = pExpr->pList->a[i].pExpr;
danielk1977e014a832004-05-17 10:48:57 +0000970
971 /* Check that the expression is constant and valid. */
danielk19774adee202004-05-08 08:23:19 +0000972 if( !sqlite3ExprIsConstant(pE2) ){
973 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +0000974 "right-hand side of IN operator must be constant");
drhfef52082000-06-06 01:50:43 +0000975 return 1;
976 }
danielk19774adee202004-05-08 08:23:19 +0000977 if( sqlite3ExprCheck(pParse, pE2, 0, 0) ){
drh4794b982000-06-06 13:54:14 +0000978 return 1;
979 }
danielk1977e014a832004-05-17 10:48:57 +0000980
981 /* Evaluate the expression and insert it into the temp table */
982 sqlite3ExprCode(pParse, pE2);
drh94a11212004-09-25 13:12:14 +0000983 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1);
danielk19770f69c1e2004-05-29 11:24:50 +0000984 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +0000985 sqlite3VdbeAddOp(v, OP_PutStrKey, pExpr->iTable, 0);
drhfef52082000-06-06 01:50:43 +0000986 }
987 }
danielk19770202b292004-06-09 09:55:16 +0000988 sqlite3VdbeChangeP3(v, addr, (void *)&keyInfo, P3_KEYINFO);
989
drhcfab11b2000-06-06 03:31:22 +0000990 break;
drhfef52082000-06-06 01:50:43 +0000991 }
992
drh19a775c2000-06-05 18:54:46 +0000993 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +0000994 /* This has to be a scalar SELECT. Generate code to put the
995 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +0000996 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +0000997 */
drh967e8b72000-06-21 13:59:10 +0000998 pExpr->iColumn = pParse->nMem++;
danielk1977bf3b7212004-05-18 10:06:24 +0000999 if(sqlite3Select(pParse, pExpr->pSelect, SRT_Mem,pExpr->iColumn,0,0,0,0)){
drh19a775c2000-06-05 18:54:46 +00001000 return 1;
1001 }
1002 break;
1003 }
1004
drhcce7d172000-05-31 15:34:51 +00001005 /* For all else, just recursively walk the tree */
1006 default: {
drh4794b982000-06-06 13:54:14 +00001007 if( pExpr->pLeft
danielk19774adee202004-05-08 08:23:19 +00001008 && sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pLeft) ){
drhcce7d172000-05-31 15:34:51 +00001009 return 1;
1010 }
1011 if( pExpr->pRight
danielk19774adee202004-05-08 08:23:19 +00001012 && sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pRight) ){
drhcce7d172000-05-31 15:34:51 +00001013 return 1;
1014 }
1015 if( pExpr->pList ){
1016 int i;
1017 ExprList *pList = pExpr->pList;
1018 for(i=0; i<pList->nExpr; i++){
drh832508b2002-03-02 17:04:07 +00001019 Expr *pArg = pList->a[i].pExpr;
danielk19774adee202004-05-08 08:23:19 +00001020 if( sqlite3ExprResolveIds(pParse, pSrcList, pEList, pArg) ){
drhcce7d172000-05-31 15:34:51 +00001021 return 1;
1022 }
1023 }
1024 }
1025 }
1026 }
1027 return 0;
1028}
1029
drhcce7d172000-05-31 15:34:51 +00001030/*
drh4b59ab52002-08-24 18:24:51 +00001031** pExpr is a node that defines a function of some kind. It might
1032** be a syntactic function like "count(x)" or it might be a function
1033** that implements an operator, like "a LIKE b".
1034**
1035** This routine makes *pzName point to the name of the function and
1036** *pnName hold the number of characters in the function name.
1037*/
1038static void getFunctionName(Expr *pExpr, const char **pzName, int *pnName){
1039 switch( pExpr->op ){
1040 case TK_FUNCTION: {
1041 *pzName = pExpr->token.z;
drh6977fea2002-10-22 23:38:04 +00001042 *pnName = pExpr->token.n;
drh4b59ab52002-08-24 18:24:51 +00001043 break;
1044 }
1045 case TK_LIKE: {
1046 *pzName = "like";
1047 *pnName = 4;
1048 break;
1049 }
1050 case TK_GLOB: {
1051 *pzName = "glob";
1052 *pnName = 4;
1053 break;
1054 }
1055 default: {
1056 *pzName = "can't happen";
1057 *pnName = 12;
1058 break;
1059 }
1060 }
1061}
1062
1063/*
drhcce7d172000-05-31 15:34:51 +00001064** Error check the functions in an expression. Make sure all
1065** function names are recognized and all functions have the correct
1066** number of arguments. Leave an error message in pParse->zErrMsg
1067** if anything is amiss. Return the number of errors.
1068**
1069** if pIsAgg is not null and this expression is an aggregate function
1070** (like count(*) or max(value)) then write a 1 into *pIsAgg.
1071*/
danielk19774adee202004-05-08 08:23:19 +00001072int sqlite3ExprCheck(Parse *pParse, Expr *pExpr, int allowAgg, int *pIsAgg){
drhcce7d172000-05-31 15:34:51 +00001073 int nErr = 0;
1074 if( pExpr==0 ) return 0;
drhcce7d172000-05-31 15:34:51 +00001075 switch( pExpr->op ){
drh4b59ab52002-08-24 18:24:51 +00001076 case TK_GLOB:
1077 case TK_LIKE:
drhcce7d172000-05-31 15:34:51 +00001078 case TK_FUNCTION: {
drhc9b84a12002-06-20 11:36:48 +00001079 int n = pExpr->pList ? pExpr->pList->nExpr : 0; /* Number of arguments */
1080 int no_such_func = 0; /* True if no such function exists */
drhc9b84a12002-06-20 11:36:48 +00001081 int wrong_num_args = 0; /* True if wrong number of arguments */
1082 int is_agg = 0; /* True if is an aggregate function */
drhcce7d172000-05-31 15:34:51 +00001083 int i;
drh4b59ab52002-08-24 18:24:51 +00001084 int nId; /* Number of characters in function name */
1085 const char *zId; /* The function name. */
drh0bce8352002-02-28 00:41:10 +00001086 FuncDef *pDef;
danielk1977d8123362004-06-12 09:25:12 +00001087 int enc = pParse->db->enc;
drh0bce8352002-02-28 00:41:10 +00001088
drh4b59ab52002-08-24 18:24:51 +00001089 getFunctionName(pExpr, &zId, &nId);
danielk1977d8123362004-06-12 09:25:12 +00001090 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001091 if( pDef==0 ){
danielk1977d8123362004-06-12 09:25:12 +00001092 pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001093 if( pDef==0 ){
drh268380c2004-02-25 13:47:31 +00001094 no_such_func = 1;
drh0bce8352002-02-28 00:41:10 +00001095 }else{
1096 wrong_num_args = 1;
drhcce7d172000-05-31 15:34:51 +00001097 }
drh0bce8352002-02-28 00:41:10 +00001098 }else{
1099 is_agg = pDef->xFunc==0;
drhcce7d172000-05-31 15:34:51 +00001100 }
drh8e0a2f92002-02-23 23:45:45 +00001101 if( is_agg && !allowAgg ){
danielk19774adee202004-05-08 08:23:19 +00001102 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId, zId);
drh8e0a2f92002-02-23 23:45:45 +00001103 nErr++;
1104 is_agg = 0;
1105 }else if( no_such_func ){
danielk19774adee202004-05-08 08:23:19 +00001106 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
drhcce7d172000-05-31 15:34:51 +00001107 nErr++;
drh8e0a2f92002-02-23 23:45:45 +00001108 }else if( wrong_num_args ){
danielk19774adee202004-05-08 08:23:19 +00001109 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
drhf7a9e1a2004-02-22 18:40:56 +00001110 nId, zId);
drh8e0a2f92002-02-23 23:45:45 +00001111 nErr++;
drhcce7d172000-05-31 15:34:51 +00001112 }
drhf7a9e1a2004-02-22 18:40:56 +00001113 if( is_agg ){
1114 pExpr->op = TK_AGG_FUNCTION;
1115 if( pIsAgg ) *pIsAgg = 1;
1116 }
drhcce7d172000-05-31 15:34:51 +00001117 for(i=0; nErr==0 && i<n; i++){
danielk19774adee202004-05-08 08:23:19 +00001118 nErr = sqlite3ExprCheck(pParse, pExpr->pList->a[i].pExpr,
drh4cfa7932000-06-08 15:10:46 +00001119 allowAgg && !is_agg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +00001120 }
danielk19770202b292004-06-09 09:55:16 +00001121 /* FIX ME: Compute pExpr->affinity based on the expected return
1122 ** type of the function
1123 */
drhcce7d172000-05-31 15:34:51 +00001124 }
1125 default: {
1126 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001127 nErr = sqlite3ExprCheck(pParse, pExpr->pLeft, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +00001128 }
1129 if( nErr==0 && pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001130 nErr = sqlite3ExprCheck(pParse, pExpr->pRight, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +00001131 }
drhfef52082000-06-06 01:50:43 +00001132 if( nErr==0 && pExpr->pList ){
1133 int n = pExpr->pList->nExpr;
1134 int i;
1135 for(i=0; nErr==0 && i<n; i++){
drh22827922000-06-06 17:27:05 +00001136 Expr *pE2 = pExpr->pList->a[i].pExpr;
danielk19774adee202004-05-08 08:23:19 +00001137 nErr = sqlite3ExprCheck(pParse, pE2, allowAgg, pIsAgg);
drhfef52082000-06-06 01:50:43 +00001138 }
1139 }
drhcce7d172000-05-31 15:34:51 +00001140 break;
1141 }
1142 }
1143 return nErr;
1144}
1145
1146/*
drh290c1942004-08-21 17:54:45 +00001147** Call sqlite3ExprResolveIds() followed by sqlite3ExprCheck().
1148**
1149** This routine is provided as a convenience since it is very common
1150** to call ResolveIds() and Check() back to back.
1151*/
1152int sqlite3ExprResolveAndCheck(
1153 Parse *pParse, /* The parser context */
1154 SrcList *pSrcList, /* List of tables used to resolve column names */
1155 ExprList *pEList, /* List of expressions used to resolve "AS" */
1156 Expr *pExpr, /* The expression to be analyzed. */
1157 int allowAgg, /* True to allow aggregate expressions */
1158 int *pIsAgg /* Set to TRUE if aggregates are found */
1159){
1160 if( pExpr==0 ) return 0;
1161 if( sqlite3ExprResolveIds(pParse,pSrcList,pEList,pExpr) ){
1162 return 1;
1163 }
1164 return sqlite3ExprCheck(pParse, pExpr, allowAgg, pIsAgg);
1165}
1166
1167/*
drhfec19aa2004-05-19 20:41:03 +00001168** Generate an instruction that will put the integer describe by
1169** text z[0..n-1] on the stack.
1170*/
1171static void codeInteger(Vdbe *v, const char *z, int n){
1172 int i;
drh6fec0762004-05-30 01:38:43 +00001173 if( sqlite3GetInt32(z, &i) ){
1174 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
1175 }else if( sqlite3FitsIn64Bits(z) ){
1176 sqlite3VdbeOp3(v, OP_Integer, 0, 0, z, n);
drhfec19aa2004-05-19 20:41:03 +00001177 }else{
1178 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n);
1179 }
1180}
1181
1182/*
drhcce7d172000-05-31 15:34:51 +00001183** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +00001184** expression and leave the result on the top of stack.
drhf2bc0132004-10-04 13:19:23 +00001185**
1186** This code depends on the fact that certain token values (ex: TK_EQ)
1187** are the same as opcode values (ex: OP_Eq) that implement the corresponding
1188** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
1189** the make process cause these values to align. Assert()s in the code
1190** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00001191*/
danielk19774adee202004-05-08 08:23:19 +00001192void sqlite3ExprCode(Parse *pParse, Expr *pExpr){
drhcce7d172000-05-31 15:34:51 +00001193 Vdbe *v = pParse->pVdbe;
1194 int op;
drhdaffd0e2001-04-11 14:28:42 +00001195 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00001196 op = pExpr->op;
1197 switch( op ){
drh967e8b72000-06-21 13:59:10 +00001198 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +00001199 if( pParse->useAgg ){
danielk19774adee202004-05-08 08:23:19 +00001200 sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drhc4a3c772001-04-04 11:48:57 +00001201 }else if( pExpr->iColumn>=0 ){
danielk19774adee202004-05-08 08:23:19 +00001202 sqlite3VdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn);
drh145716b2004-09-24 12:24:06 +00001203#ifndef NDEBUG
1204 if( pExpr->span.z && pExpr->span.n>0 && pExpr->span.n<100 ){
1205 VdbeComment((v, "# %T", &pExpr->span));
1206 }
1207#endif
drhc4a3c772001-04-04 11:48:57 +00001208 }else{
danielk19774adee202004-05-08 08:23:19 +00001209 sqlite3VdbeAddOp(v, OP_Recno, pExpr->iTable, 0);
drh22827922000-06-06 17:27:05 +00001210 }
drhcce7d172000-05-31 15:34:51 +00001211 break;
1212 }
1213 case TK_INTEGER: {
drhfec19aa2004-05-19 20:41:03 +00001214 codeInteger(v, pExpr->token.z, pExpr->token.n);
1215 break;
1216 }
1217 case TK_FLOAT:
1218 case TK_STRING: {
drhf2bc0132004-10-04 13:19:23 +00001219 assert( TK_FLOAT==OP_Real );
1220 assert( TK_STRING==OP_String8 );
drhfec19aa2004-05-19 20:41:03 +00001221 sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z, pExpr->token.n);
danielk19774adee202004-05-08 08:23:19 +00001222 sqlite3VdbeDequoteP3(v, -1);
drhcce7d172000-05-31 15:34:51 +00001223 break;
1224 }
danielk1977c572ef72004-05-27 09:28:41 +00001225 case TK_BLOB: {
drhf2bc0132004-10-04 13:19:23 +00001226 assert( TK_BLOB==OP_HexBlob );
danielk1977c572ef72004-05-27 09:28:41 +00001227 sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z+1, pExpr->token.n-1);
1228 sqlite3VdbeDequoteP3(v, -1);
1229 break;
1230 }
drhcce7d172000-05-31 15:34:51 +00001231 case TK_NULL: {
danielk19770f69c1e2004-05-29 11:24:50 +00001232 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001233 break;
1234 }
drh50457892003-09-06 01:10:47 +00001235 case TK_VARIABLE: {
danielk19774adee202004-05-08 08:23:19 +00001236 sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0);
drh895d7472004-08-20 16:02:39 +00001237 if( pExpr->token.n>1 ){
1238 sqlite3VdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
1239 }
drh50457892003-09-06 01:10:47 +00001240 break;
1241 }
drhc9b84a12002-06-20 11:36:48 +00001242 case TK_LT:
1243 case TK_LE:
1244 case TK_GT:
1245 case TK_GE:
1246 case TK_NE:
1247 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00001248 assert( TK_LT==OP_Lt );
1249 assert( TK_LE==OP_Le );
1250 assert( TK_GT==OP_Gt );
1251 assert( TK_GE==OP_Ge );
1252 assert( TK_EQ==OP_Eq );
1253 assert( TK_NE==OP_Ne );
danielk1977a37cdde2004-05-16 11:15:36 +00001254 sqlite3ExprCode(pParse, pExpr->pLeft);
1255 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001256 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 0, 0);
danielk1977a37cdde2004-05-16 11:15:36 +00001257 break;
drhc9b84a12002-06-20 11:36:48 +00001258 }
drhcce7d172000-05-31 15:34:51 +00001259 case TK_AND:
1260 case TK_OR:
1261 case TK_PLUS:
1262 case TK_STAR:
1263 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00001264 case TK_REM:
1265 case TK_BITAND:
1266 case TK_BITOR:
drh17c40292004-07-21 02:53:29 +00001267 case TK_SLASH:
drhbf4133c2001-10-13 02:59:08 +00001268 case TK_LSHIFT:
drh855eb1c2004-08-31 13:45:11 +00001269 case TK_RSHIFT:
drh00400772000-06-16 20:51:26 +00001270 case TK_CONCAT: {
drhf2bc0132004-10-04 13:19:23 +00001271 assert( TK_AND==OP_And );
1272 assert( TK_OR==OP_Or );
1273 assert( TK_PLUS==OP_Add );
1274 assert( TK_MINUS==OP_Subtract );
1275 assert( TK_REM==OP_Remainder );
1276 assert( TK_BITAND==OP_BitAnd );
1277 assert( TK_BITOR==OP_BitOr );
1278 assert( TK_SLASH==OP_Divide );
1279 assert( TK_LSHIFT==OP_ShiftLeft );
1280 assert( TK_RSHIFT==OP_ShiftRight );
1281 assert( TK_CONCAT==OP_Concat );
danielk19774adee202004-05-08 08:23:19 +00001282 sqlite3ExprCode(pParse, pExpr->pLeft);
1283 sqlite3ExprCode(pParse, pExpr->pRight);
drh855eb1c2004-08-31 13:45:11 +00001284 sqlite3VdbeAddOp(v, op, 0, 0);
drh00400772000-06-16 20:51:26 +00001285 break;
1286 }
drhcce7d172000-05-31 15:34:51 +00001287 case TK_UMINUS: {
drhfec19aa2004-05-19 20:41:03 +00001288 Expr *pLeft = pExpr->pLeft;
1289 assert( pLeft );
1290 if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
1291 Token *p = &pLeft->token;
drh6e142f52000-06-08 13:36:40 +00001292 char *z = sqliteMalloc( p->n + 2 );
1293 sprintf(z, "-%.*s", p->n, p->z);
drhfec19aa2004-05-19 20:41:03 +00001294 if( pLeft->op==TK_FLOAT ){
1295 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001296 }else{
drhfec19aa2004-05-19 20:41:03 +00001297 codeInteger(v, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001298 }
drh6e142f52000-06-08 13:36:40 +00001299 sqliteFree(z);
1300 break;
1301 }
drh1ccde152000-06-17 13:12:39 +00001302 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +00001303 }
drhbf4133c2001-10-13 02:59:08 +00001304 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00001305 case TK_NOT: {
drhf2bc0132004-10-04 13:19:23 +00001306 assert( TK_BITNOT==OP_BitNot );
1307 assert( TK_NOT==OP_Not );
danielk19774adee202004-05-08 08:23:19 +00001308 sqlite3ExprCode(pParse, pExpr->pLeft);
1309 sqlite3VdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001310 break;
1311 }
1312 case TK_ISNULL:
1313 case TK_NOTNULL: {
1314 int dest;
drhf2bc0132004-10-04 13:19:23 +00001315 assert( TK_ISNULL==OP_IsNull );
1316 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00001317 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1318 sqlite3ExprCode(pParse, pExpr->pLeft);
1319 dest = sqlite3VdbeCurrentAddr(v) + 2;
1320 sqlite3VdbeAddOp(v, op, 1, dest);
1321 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);
drhf2bc0132004-10-04 13:19:23 +00001322 break;
drhcce7d172000-05-31 15:34:51 +00001323 }
drh22827922000-06-06 17:27:05 +00001324 case TK_AGG_FUNCTION: {
danielk19774adee202004-05-08 08:23:19 +00001325 sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drh22827922000-06-06 17:27:05 +00001326 break;
1327 }
drh4b59ab52002-08-24 18:24:51 +00001328 case TK_GLOB:
1329 case TK_LIKE:
drhcce7d172000-05-31 15:34:51 +00001330 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00001331 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00001332 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00001333 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00001334 int nId;
1335 const char *zId;
danielk1977682f68b2004-06-05 10:22:17 +00001336 int p2 = 0;
1337 int i;
danielk1977d8123362004-06-12 09:25:12 +00001338 u8 enc = pParse->db->enc;
danielk1977dc1bdc42004-06-11 10:51:27 +00001339 CollSeq *pColl = 0;
drh4b59ab52002-08-24 18:24:51 +00001340 getFunctionName(pExpr, &zId, &nId);
danielk1977d8123362004-06-12 09:25:12 +00001341 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001342 assert( pDef!=0 );
drhf9b596e2004-05-26 16:54:42 +00001343 nExpr = sqlite3ExprCodeExprList(pParse, pList);
danielk1977682f68b2004-06-05 10:22:17 +00001344 for(i=0; i<nExpr && i<32; i++){
danielk1977d02eb1f2004-06-06 09:44:03 +00001345 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
1346 p2 |= (1<<i);
1347 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001348 if( pDef->needCollSeq && !pColl ){
1349 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
1350 }
1351 }
1352 if( pDef->needCollSeq ){
1353 if( !pColl ) pColl = pParse->db->pDfltColl;
danielk1977d8123362004-06-12 09:25:12 +00001354 sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ);
danielk1977682f68b2004-06-05 10:22:17 +00001355 }
1356 sqlite3VdbeOp3(v, OP_Function, nExpr, p2, (char*)pDef, P3_FUNCDEF);
drhcce7d172000-05-31 15:34:51 +00001357 break;
1358 }
drh19a775c2000-06-05 18:54:46 +00001359 case TK_SELECT: {
danielk19774adee202004-05-08 08:23:19 +00001360 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drhad6d9462004-09-19 02:15:24 +00001361 VdbeComment((v, "# load subquery result"));
drh19a775c2000-06-05 18:54:46 +00001362 break;
1363 }
drhfef52082000-06-06 01:50:43 +00001364 case TK_IN: {
1365 int addr;
drh94a11212004-09-25 13:12:14 +00001366 char affinity;
danielk1977e014a832004-05-17 10:48:57 +00001367
1368 /* Figure out the affinity to use to create a key from the results
1369 ** of the expression. affinityStr stores a static string suitable for
danielk1977ededfd52004-06-17 07:53:01 +00001370 ** P3 of OP_MakeRecord.
danielk1977e014a832004-05-17 10:48:57 +00001371 */
drh94a11212004-09-25 13:12:14 +00001372 affinity = comparisonAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +00001373
danielk19774adee202004-05-08 08:23:19 +00001374 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
danielk1977e014a832004-05-17 10:48:57 +00001375
1376 /* Code the <expr> from "<expr> IN (...)". The temporary table
1377 ** pExpr->iTable contains the values that make up the (...) set.
1378 */
danielk19774adee202004-05-08 08:23:19 +00001379 sqlite3ExprCode(pParse, pExpr->pLeft);
1380 addr = sqlite3VdbeCurrentAddr(v);
danielk1977e014a832004-05-17 10:48:57 +00001381 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); /* addr + 0 */
danielk19774adee202004-05-08 08:23:19 +00001382 sqlite3VdbeAddOp(v, OP_Pop, 2, 0);
danielk19770f69c1e2004-05-29 11:24:50 +00001383 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +00001384 sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7);
drh94a11212004-09-25 13:12:14 +00001385 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1); /* addr + 4 */
danielk1977e014a832004-05-17 10:48:57 +00001386 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7);
1387 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); /* addr + 6 */
1388
drhfef52082000-06-06 01:50:43 +00001389 break;
1390 }
1391 case TK_BETWEEN: {
drhbe5c89a2004-07-26 00:31:09 +00001392 Expr *pLeft = pExpr->pLeft;
1393 struct ExprList_item *pLItem = pExpr->pList->a;
1394 Expr *pRight = pLItem->pExpr;
1395 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001396 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001397 sqlite3ExprCode(pParse, pRight);
1398 codeCompare(pParse, pLeft, pRight, OP_Ge, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001399 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
drhbe5c89a2004-07-26 00:31:09 +00001400 pLItem++;
1401 pRight = pLItem->pExpr;
1402 sqlite3ExprCode(pParse, pRight);
1403 codeCompare(pParse, pLeft, pRight, OP_Le, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001404 sqlite3VdbeAddOp(v, OP_And, 0, 0);
drhfef52082000-06-06 01:50:43 +00001405 break;
1406 }
drh51e9a442004-01-16 16:42:53 +00001407 case TK_UPLUS:
drha2e00042002-01-22 03:13:42 +00001408 case TK_AS: {
danielk19774adee202004-05-08 08:23:19 +00001409 sqlite3ExprCode(pParse, pExpr->pLeft);
drha2e00042002-01-22 03:13:42 +00001410 break;
1411 }
drh17a7f8d2002-03-24 13:13:27 +00001412 case TK_CASE: {
1413 int expr_end_label;
drhf5905aa2002-05-26 20:54:33 +00001414 int jumpInst;
1415 int addr;
1416 int nExpr;
drh17a7f8d2002-03-24 13:13:27 +00001417 int i;
drhbe5c89a2004-07-26 00:31:09 +00001418 ExprList *pEList;
1419 struct ExprList_item *aListelem;
drh17a7f8d2002-03-24 13:13:27 +00001420
1421 assert(pExpr->pList);
1422 assert((pExpr->pList->nExpr % 2) == 0);
1423 assert(pExpr->pList->nExpr > 0);
drhbe5c89a2004-07-26 00:31:09 +00001424 pEList = pExpr->pList;
1425 aListelem = pEList->a;
1426 nExpr = pEList->nExpr;
danielk19774adee202004-05-08 08:23:19 +00001427 expr_end_label = sqlite3VdbeMakeLabel(v);
drh17a7f8d2002-03-24 13:13:27 +00001428 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001429 sqlite3ExprCode(pParse, pExpr->pLeft);
drh17a7f8d2002-03-24 13:13:27 +00001430 }
drhf5905aa2002-05-26 20:54:33 +00001431 for(i=0; i<nExpr; i=i+2){
drhbe5c89a2004-07-26 00:31:09 +00001432 sqlite3ExprCode(pParse, aListelem[i].pExpr);
drh17a7f8d2002-03-24 13:13:27 +00001433 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001434 sqlite3VdbeAddOp(v, OP_Dup, 1, 1);
drhbe5c89a2004-07-26 00:31:09 +00001435 jumpInst = codeCompare(pParse, pExpr->pLeft, aListelem[i].pExpr,
1436 OP_Ne, 0, 1);
danielk19774adee202004-05-08 08:23:19 +00001437 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001438 }else{
danielk19774adee202004-05-08 08:23:19 +00001439 jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001440 }
drhbe5c89a2004-07-26 00:31:09 +00001441 sqlite3ExprCode(pParse, aListelem[i+1].pExpr);
danielk19774adee202004-05-08 08:23:19 +00001442 sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label);
1443 addr = sqlite3VdbeCurrentAddr(v);
1444 sqlite3VdbeChangeP2(v, jumpInst, addr);
drh17a7f8d2002-03-24 13:13:27 +00001445 }
drhf570f012002-05-31 15:51:25 +00001446 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001447 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhf570f012002-05-31 15:51:25 +00001448 }
drh17a7f8d2002-03-24 13:13:27 +00001449 if( pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001450 sqlite3ExprCode(pParse, pExpr->pRight);
drh17a7f8d2002-03-24 13:13:27 +00001451 }else{
danielk19770f69c1e2004-05-29 11:24:50 +00001452 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
drh17a7f8d2002-03-24 13:13:27 +00001453 }
danielk19774adee202004-05-08 08:23:19 +00001454 sqlite3VdbeResolveLabel(v, expr_end_label);
danielk19776f349032002-06-11 02:25:40 +00001455 break;
1456 }
1457 case TK_RAISE: {
1458 if( !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +00001459 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00001460 "RAISE() may only be used within a trigger-program");
danielk19776f349032002-06-11 02:25:40 +00001461 return;
1462 }
drhad6d9462004-09-19 02:15:24 +00001463 if( pExpr->iColumn!=OE_Ignore ){
1464 assert( pExpr->iColumn==OE_Rollback ||
1465 pExpr->iColumn == OE_Abort ||
1466 pExpr->iColumn == OE_Fail );
1467 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn,
1468 pExpr->token.z, pExpr->token.n);
1469 sqlite3VdbeDequoteP3(v, -1);
danielk19776f349032002-06-11 02:25:40 +00001470 } else {
drhad6d9462004-09-19 02:15:24 +00001471 assert( pExpr->iColumn == OE_Ignore );
1472 sqlite3VdbeAddOp(v, OP_ContextPop, 0, 0);
1473 sqlite3VdbeAddOp(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
1474 VdbeComment((v, "# raise(IGNORE)"));
danielk19776f349032002-06-11 02:25:40 +00001475 }
drh17a7f8d2002-03-24 13:13:27 +00001476 }
1477 break;
drhcce7d172000-05-31 15:34:51 +00001478 }
drhcce7d172000-05-31 15:34:51 +00001479}
1480
1481/*
drh268380c2004-02-25 13:47:31 +00001482** Generate code that pushes the value of every element of the given
drhf9b596e2004-05-26 16:54:42 +00001483** expression list onto the stack.
drh268380c2004-02-25 13:47:31 +00001484**
1485** Return the number of elements pushed onto the stack.
1486*/
danielk19774adee202004-05-08 08:23:19 +00001487int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00001488 Parse *pParse, /* Parsing context */
drhf9b596e2004-05-26 16:54:42 +00001489 ExprList *pList /* The expression list to be coded */
drh268380c2004-02-25 13:47:31 +00001490){
1491 struct ExprList_item *pItem;
1492 int i, n;
1493 Vdbe *v;
1494 if( pList==0 ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001495 v = sqlite3GetVdbe(pParse);
drh268380c2004-02-25 13:47:31 +00001496 n = pList->nExpr;
1497 for(pItem=pList->a, i=0; i<n; i++, pItem++){
danielk19774adee202004-05-08 08:23:19 +00001498 sqlite3ExprCode(pParse, pItem->pExpr);
drh268380c2004-02-25 13:47:31 +00001499 }
drhf9b596e2004-05-26 16:54:42 +00001500 return n;
drh268380c2004-02-25 13:47:31 +00001501}
1502
1503/*
drhcce7d172000-05-31 15:34:51 +00001504** Generate code for a boolean expression such that a jump is made
1505** to the label "dest" if the expression is true but execution
1506** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00001507**
1508** If the expression evaluates to NULL (neither true nor false), then
1509** take the jump if the jumpIfNull flag is true.
drhf2bc0132004-10-04 13:19:23 +00001510**
1511** This code depends on the fact that certain token values (ex: TK_EQ)
1512** are the same as opcode values (ex: OP_Eq) that implement the corresponding
1513** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
1514** the make process cause these values to align. Assert()s in the code
1515** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00001516*/
danielk19774adee202004-05-08 08:23:19 +00001517void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001518 Vdbe *v = pParse->pVdbe;
1519 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001520 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00001521 op = pExpr->op;
1522 switch( op ){
drhcce7d172000-05-31 15:34:51 +00001523 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001524 int d2 = sqlite3VdbeMakeLabel(v);
1525 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
1526 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
1527 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001528 break;
1529 }
1530 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001531 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
1532 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001533 break;
1534 }
1535 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001536 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001537 break;
1538 }
1539 case TK_LT:
1540 case TK_LE:
1541 case TK_GT:
1542 case TK_GE:
1543 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00001544 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00001545 assert( TK_LT==OP_Lt );
1546 assert( TK_LE==OP_Le );
1547 assert( TK_GT==OP_Gt );
1548 assert( TK_GE==OP_Ge );
1549 assert( TK_EQ==OP_Eq );
1550 assert( TK_NE==OP_Ne );
danielk19774adee202004-05-08 08:23:19 +00001551 sqlite3ExprCode(pParse, pExpr->pLeft);
1552 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001553 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001554 break;
1555 }
1556 case TK_ISNULL:
1557 case TK_NOTNULL: {
drhf2bc0132004-10-04 13:19:23 +00001558 assert( TK_ISNULL==OP_IsNull );
1559 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00001560 sqlite3ExprCode(pParse, pExpr->pLeft);
1561 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001562 break;
1563 }
drhfef52082000-06-06 01:50:43 +00001564 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00001565 /* The expression "x BETWEEN y AND z" is implemented as:
1566 **
1567 ** 1 IF (x < y) GOTO 3
1568 ** 2 IF (x <= z) GOTO <dest>
1569 ** 3 ...
1570 */
drhf5905aa2002-05-26 20:54:33 +00001571 int addr;
drhbe5c89a2004-07-26 00:31:09 +00001572 Expr *pLeft = pExpr->pLeft;
1573 Expr *pRight = pExpr->pList->a[0].pExpr;
1574 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001575 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001576 sqlite3ExprCode(pParse, pRight);
1577 addr = codeCompare(pParse, pLeft, pRight, OP_Lt, 0, !jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00001578
drhbe5c89a2004-07-26 00:31:09 +00001579 pRight = pExpr->pList->a[1].pExpr;
1580 sqlite3ExprCode(pParse, pRight);
1581 codeCompare(pParse, pLeft, pRight, OP_Le, dest, jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00001582
danielk19774adee202004-05-08 08:23:19 +00001583 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
1584 sqlite3VdbeChangeP2(v, addr, sqlite3VdbeCurrentAddr(v));
1585 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00001586 break;
1587 }
drhcce7d172000-05-31 15:34:51 +00001588 default: {
danielk19774adee202004-05-08 08:23:19 +00001589 sqlite3ExprCode(pParse, pExpr);
1590 sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001591 break;
1592 }
1593 }
1594}
1595
1596/*
drh66b89c82000-11-28 20:47:17 +00001597** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00001598** to the label "dest" if the expression is false but execution
1599** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00001600**
1601** If the expression evaluates to NULL (neither true nor false) then
1602** jump if jumpIfNull is true or fall through if jumpIfNull is false.
drhcce7d172000-05-31 15:34:51 +00001603*/
danielk19774adee202004-05-08 08:23:19 +00001604void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001605 Vdbe *v = pParse->pVdbe;
1606 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001607 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00001608
1609 /* The value of pExpr->op and op are related as follows:
1610 **
1611 ** pExpr->op op
1612 ** --------- ----------
1613 ** TK_ISNULL OP_NotNull
1614 ** TK_NOTNULL OP_IsNull
1615 ** TK_NE OP_Eq
1616 ** TK_EQ OP_Ne
1617 ** TK_GT OP_Le
1618 ** TK_LE OP_Gt
1619 ** TK_GE OP_Lt
1620 ** TK_LT OP_Ge
1621 **
1622 ** For other values of pExpr->op, op is undefined and unused.
1623 ** The value of TK_ and OP_ constants are arranged such that we
1624 ** can compute the mapping above using the following expression.
1625 ** Assert()s verify that the computation is correct.
1626 */
1627 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
1628
1629 /* Verify correct alignment of TK_ and OP_ constants
1630 */
1631 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
1632 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
1633 assert( pExpr->op!=TK_NE || op==OP_Eq );
1634 assert( pExpr->op!=TK_EQ || op==OP_Ne );
1635 assert( pExpr->op!=TK_LT || op==OP_Ge );
1636 assert( pExpr->op!=TK_LE || op==OP_Gt );
1637 assert( pExpr->op!=TK_GT || op==OP_Le );
1638 assert( pExpr->op!=TK_GE || op==OP_Lt );
1639
drhcce7d172000-05-31 15:34:51 +00001640 switch( pExpr->op ){
1641 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001642 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
1643 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001644 break;
1645 }
1646 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001647 int d2 = sqlite3VdbeMakeLabel(v);
1648 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
1649 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
1650 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001651 break;
1652 }
1653 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001654 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001655 break;
1656 }
1657 case TK_LT:
1658 case TK_LE:
1659 case TK_GT:
1660 case TK_GE:
1661 case TK_NE:
1662 case TK_EQ: {
danielk19774adee202004-05-08 08:23:19 +00001663 sqlite3ExprCode(pParse, pExpr->pLeft);
1664 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001665 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001666 break;
1667 }
drhcce7d172000-05-31 15:34:51 +00001668 case TK_ISNULL:
1669 case TK_NOTNULL: {
danielk19774adee202004-05-08 08:23:19 +00001670 sqlite3ExprCode(pParse, pExpr->pLeft);
1671 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001672 break;
1673 }
drhfef52082000-06-06 01:50:43 +00001674 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00001675 /* The expression is "x BETWEEN y AND z". It is implemented as:
1676 **
1677 ** 1 IF (x >= y) GOTO 3
1678 ** 2 GOTO <dest>
1679 ** 3 IF (x > z) GOTO <dest>
1680 */
drhfef52082000-06-06 01:50:43 +00001681 int addr;
drhbe5c89a2004-07-26 00:31:09 +00001682 Expr *pLeft = pExpr->pLeft;
1683 Expr *pRight = pExpr->pList->a[0].pExpr;
1684 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001685 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001686 sqlite3ExprCode(pParse, pRight);
danielk19774adee202004-05-08 08:23:19 +00001687 addr = sqlite3VdbeCurrentAddr(v);
drhbe5c89a2004-07-26 00:31:09 +00001688 codeCompare(pParse, pLeft, pRight, OP_Ge, addr+3, !jumpIfNull);
1689
danielk19774adee202004-05-08 08:23:19 +00001690 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
1691 sqlite3VdbeAddOp(v, OP_Goto, 0, dest);
drhbe5c89a2004-07-26 00:31:09 +00001692 pRight = pExpr->pList->a[1].pExpr;
1693 sqlite3ExprCode(pParse, pRight);
1694 codeCompare(pParse, pLeft, pRight, OP_Gt, dest, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00001695 break;
1696 }
drhcce7d172000-05-31 15:34:51 +00001697 default: {
danielk19774adee202004-05-08 08:23:19 +00001698 sqlite3ExprCode(pParse, pExpr);
1699 sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001700 break;
1701 }
1702 }
1703}
drh22827922000-06-06 17:27:05 +00001704
1705/*
1706** Do a deep comparison of two expression trees. Return TRUE (non-zero)
1707** if they are identical and return FALSE if they differ in any way.
1708*/
danielk19774adee202004-05-08 08:23:19 +00001709int sqlite3ExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00001710 int i;
1711 if( pA==0 ){
1712 return pB==0;
1713 }else if( pB==0 ){
1714 return 0;
1715 }
1716 if( pA->op!=pB->op ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001717 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
1718 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00001719 if( pA->pList ){
1720 if( pB->pList==0 ) return 0;
1721 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
1722 for(i=0; i<pA->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00001723 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00001724 return 0;
1725 }
1726 }
1727 }else if( pB->pList ){
1728 return 0;
1729 }
1730 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00001731 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drh22827922000-06-06 17:27:05 +00001732 if( pA->token.z ){
1733 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00001734 if( pB->token.n!=pA->token.n ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001735 if( sqlite3StrNICmp(pA->token.z, pB->token.z, pB->token.n)!=0 ) return 0;
drh22827922000-06-06 17:27:05 +00001736 }
1737 return 1;
1738}
1739
1740/*
1741** Add a new element to the pParse->aAgg[] array and return its index.
1742*/
1743static int appendAggInfo(Parse *pParse){
1744 if( (pParse->nAgg & 0x7)==0 ){
1745 int amt = pParse->nAgg + 8;
drh6d4abfb2001-10-22 02:58:08 +00001746 AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0]));
1747 if( aAgg==0 ){
drh22827922000-06-06 17:27:05 +00001748 return -1;
1749 }
drh6d4abfb2001-10-22 02:58:08 +00001750 pParse->aAgg = aAgg;
drh22827922000-06-06 17:27:05 +00001751 }
1752 memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0]));
1753 return pParse->nAgg++;
1754}
1755
1756/*
1757** Analyze the given expression looking for aggregate functions and
1758** for variables that need to be added to the pParse->aAgg[] array.
1759** Make additional entries to the pParse->aAgg[] array as necessary.
1760**
1761** This routine should only be called after the expression has been
danielk19774adee202004-05-08 08:23:19 +00001762** analyzed by sqlite3ExprResolveIds() and sqlite3ExprCheck().
drh22827922000-06-06 17:27:05 +00001763**
1764** If errors are seen, leave an error message in zErrMsg and return
1765** the number of errors.
1766*/
danielk19774adee202004-05-08 08:23:19 +00001767int sqlite3ExprAnalyzeAggregates(Parse *pParse, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00001768 int i;
1769 AggExpr *aAgg;
1770 int nErr = 0;
1771
1772 if( pExpr==0 ) return 0;
1773 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00001774 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +00001775 aAgg = pParse->aAgg;
1776 for(i=0; i<pParse->nAgg; i++){
1777 if( aAgg[i].isAgg ) continue;
1778 if( aAgg[i].pExpr->iTable==pExpr->iTable
drh967e8b72000-06-21 13:59:10 +00001779 && aAgg[i].pExpr->iColumn==pExpr->iColumn ){
drh22827922000-06-06 17:27:05 +00001780 break;
1781 }
1782 }
1783 if( i>=pParse->nAgg ){
1784 i = appendAggInfo(pParse);
1785 if( i<0 ) return 1;
1786 pParse->aAgg[i].isAgg = 0;
1787 pParse->aAgg[i].pExpr = pExpr;
1788 }
drhaaf88722000-06-08 11:25:00 +00001789 pExpr->iAgg = i;
drh22827922000-06-06 17:27:05 +00001790 break;
1791 }
1792 case TK_AGG_FUNCTION: {
drh22827922000-06-06 17:27:05 +00001793 aAgg = pParse->aAgg;
1794 for(i=0; i<pParse->nAgg; i++){
1795 if( !aAgg[i].isAgg ) continue;
danielk19774adee202004-05-08 08:23:19 +00001796 if( sqlite3ExprCompare(aAgg[i].pExpr, pExpr) ){
drh22827922000-06-06 17:27:05 +00001797 break;
1798 }
1799 }
1800 if( i>=pParse->nAgg ){
danielk1977d8123362004-06-12 09:25:12 +00001801 u8 enc = pParse->db->enc;
drh22827922000-06-06 17:27:05 +00001802 i = appendAggInfo(pParse);
1803 if( i<0 ) return 1;
1804 pParse->aAgg[i].isAgg = 1;
1805 pParse->aAgg[i].pExpr = pExpr;
danielk19774adee202004-05-08 08:23:19 +00001806 pParse->aAgg[i].pFunc = sqlite3FindFunction(pParse->db,
drh6977fea2002-10-22 23:38:04 +00001807 pExpr->token.z, pExpr->token.n,
danielk1977d8123362004-06-12 09:25:12 +00001808 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
drh22827922000-06-06 17:27:05 +00001809 }
1810 pExpr->iAgg = i;
1811 break;
1812 }
1813 default: {
1814 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001815 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pLeft);
drh22827922000-06-06 17:27:05 +00001816 }
1817 if( nErr==0 && pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001818 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pRight);
drh22827922000-06-06 17:27:05 +00001819 }
1820 if( nErr==0 && pExpr->pList ){
1821 int n = pExpr->pList->nExpr;
1822 int i;
1823 for(i=0; nErr==0 && i<n; i++){
danielk19774adee202004-05-08 08:23:19 +00001824 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pList->a[i].pExpr);
drh22827922000-06-06 17:27:05 +00001825 }
1826 }
1827 break;
1828 }
1829 }
1830 return nErr;
1831}
drh8e0a2f92002-02-23 23:45:45 +00001832
1833/*
danielk1977d02eb1f2004-06-06 09:44:03 +00001834** Locate a user function given a name, a number of arguments and a flag
1835** indicating whether the function prefers UTF-16 over UTF-8. Return a
1836** pointer to the FuncDef structure that defines that function, or return
1837** NULL if the function does not exist.
drh8e0a2f92002-02-23 23:45:45 +00001838**
drh0bce8352002-02-28 00:41:10 +00001839** If the createFlag argument is true, then a new (blank) FuncDef
drh8e0a2f92002-02-23 23:45:45 +00001840** structure is created and liked into the "db" structure if a
1841** no matching function previously existed. When createFlag is true
1842** and the nArg parameter is -1, then only a function that accepts
1843** any number of arguments will be returned.
1844**
1845** If createFlag is false and nArg is -1, then the first valid
1846** function found is returned. A function is valid if either xFunc
1847** or xStep is non-zero.
danielk1977d02eb1f2004-06-06 09:44:03 +00001848**
1849** If createFlag is false, then a function with the required name and
1850** number of arguments may be returned even if the eTextRep flag does not
1851** match that requested.
drh8e0a2f92002-02-23 23:45:45 +00001852*/
danielk19774adee202004-05-08 08:23:19 +00001853FuncDef *sqlite3FindFunction(
drh9bb575f2004-09-06 17:24:11 +00001854 sqlite3 *db, /* An open database */
drh8e0a2f92002-02-23 23:45:45 +00001855 const char *zName, /* Name of the function. Not null-terminated */
1856 int nName, /* Number of characters in the name */
1857 int nArg, /* Number of arguments. -1 means any number */
danielk1977d8123362004-06-12 09:25:12 +00001858 u8 enc, /* Preferred text encoding */
drh8e0a2f92002-02-23 23:45:45 +00001859 int createFlag /* Create new entry if true and does not otherwise exist */
1860){
danielk1977d02eb1f2004-06-06 09:44:03 +00001861 FuncDef *p; /* Iterator variable */
1862 FuncDef *pFirst; /* First function with this name */
1863 FuncDef *pBest = 0; /* Best match found so far */
danielk1977d8123362004-06-12 09:25:12 +00001864 int bestmatch = 0;
danielk1977d02eb1f2004-06-06 09:44:03 +00001865
danielk1977d8123362004-06-12 09:25:12 +00001866
1867 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
danielk1977d02eb1f2004-06-06 09:44:03 +00001868 if( nArg<-1 ) nArg = -1;
1869
1870 pFirst = (FuncDef*)sqlite3HashFind(&db->aFunc, zName, nName);
1871 for(p=pFirst; p; p=p->pNext){
danielk1977d8123362004-06-12 09:25:12 +00001872 /* During the search for the best function definition, bestmatch is set
1873 ** as follows to indicate the quality of the match with the definition
1874 ** pointed to by pBest:
1875 **
1876 ** 0: pBest is NULL. No match has been found.
1877 ** 1: A variable arguments function that prefers UTF-8 when a UTF-16
1878 ** encoding is requested, or vice versa.
1879 ** 2: A variable arguments function that uses UTF-16BE when UTF-16LE is
1880 ** requested, or vice versa.
1881 ** 3: A variable arguments function using the same text encoding.
1882 ** 4: A function with the exact number of arguments requested that
1883 ** prefers UTF-8 when a UTF-16 encoding is requested, or vice versa.
1884 ** 5: A function with the exact number of arguments requested that
1885 ** prefers UTF-16LE when UTF-16BE is requested, or vice versa.
1886 ** 6: An exact match.
1887 **
1888 ** A larger value of 'matchqual' indicates a more desirable match.
1889 */
danielk1977e12c17b2004-06-23 12:35:14 +00001890 if( p->nArg==-1 || p->nArg==nArg || nArg==-1 ){
danielk1977d8123362004-06-12 09:25:12 +00001891 int match = 1; /* Quality of this match */
1892 if( p->nArg==nArg || nArg==-1 ){
1893 match = 4;
danielk1977d02eb1f2004-06-06 09:44:03 +00001894 }
danielk1977d8123362004-06-12 09:25:12 +00001895 if( enc==p->iPrefEnc ){
1896 match += 2;
danielk1977d02eb1f2004-06-06 09:44:03 +00001897 }
danielk1977d8123362004-06-12 09:25:12 +00001898 else if( (enc==SQLITE_UTF16LE && p->iPrefEnc==SQLITE_UTF16BE) ||
1899 (enc==SQLITE_UTF16BE && p->iPrefEnc==SQLITE_UTF16LE) ){
1900 match += 1;
1901 }
1902
1903 if( match>bestmatch ){
1904 pBest = p;
1905 bestmatch = match;
danielk1977d02eb1f2004-06-06 09:44:03 +00001906 }
1907 }
drh8e0a2f92002-02-23 23:45:45 +00001908 }
danielk1977d02eb1f2004-06-06 09:44:03 +00001909
danielk1977d8123362004-06-12 09:25:12 +00001910 /* If the createFlag parameter is true, and the seach did not reveal an
1911 ** exact match for the name, number of arguments and encoding, then add a
1912 ** new entry to the hash table and return it.
1913 */
1914 if( createFlag && bestmatch<6 &&
danielk1977d02eb1f2004-06-06 09:44:03 +00001915 (pBest = sqliteMalloc(sizeof(*pBest)+nName+1)) ){
1916 pBest->nArg = nArg;
1917 pBest->pNext = pFirst;
1918 pBest->zName = (char*)&pBest[1];
danielk1977d8123362004-06-12 09:25:12 +00001919 pBest->iPrefEnc = enc;
danielk1977d02eb1f2004-06-06 09:44:03 +00001920 memcpy(pBest->zName, zName, nName);
1921 pBest->zName[nName] = 0;
1922 sqlite3HashInsert(&db->aFunc, pBest->zName, nName, (void*)pBest);
drh8e0a2f92002-02-23 23:45:45 +00001923 }
danielk1977d02eb1f2004-06-06 09:44:03 +00001924
1925 if( pBest && (pBest->xStep || pBest->xFunc || createFlag) ){
1926 return pBest;
drh8e0a2f92002-02-23 23:45:45 +00001927 }
danielk1977d02eb1f2004-06-06 09:44:03 +00001928 return 0;
drh8e0a2f92002-02-23 23:45:45 +00001929}