blob: 75167579b3e1b6cea9f9fa9274de077d1a4321f4 [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**
drhf2bc0132004-10-04 13:19:23 +000015** $Id: expr.c,v 1.166 2004/10/04 13:19:24 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
703 /* If we have not already resolved the name, then maybe
704 ** it is a new.* or old.* trigger argument reference
705 */
706 if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
707 TriggerStack *pTriggerStack = pParse->trigStack;
708 Table *pTab = 0;
danielk19774adee202004-05-08 08:23:19 +0000709 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){
drh8141f612004-01-25 22:44:58 +0000710 pExpr->iTable = pTriggerStack->newIdx;
711 assert( pTriggerStack->pTab );
712 pTab = pTriggerStack->pTab;
danielk19774adee202004-05-08 08:23:19 +0000713 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab) == 0 ){
drh8141f612004-01-25 22:44:58 +0000714 pExpr->iTable = pTriggerStack->oldIdx;
715 assert( pTriggerStack->pTab );
716 pTab = pTriggerStack->pTab;
717 }
718
719 if( pTab ){
720 int j;
721 Column *pCol = pTab->aCol;
722
723 pExpr->iDb = pTab->iDb;
724 cntTab++;
725 for(j=0; j < pTab->nCol; j++, pCol++) {
danielk19774adee202004-05-08 08:23:19 +0000726 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
drh8141f612004-01-25 22:44:58 +0000727 cnt++;
728 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
danielk1977a37cdde2004-05-16 11:15:36 +0000729 pExpr->affinity = pTab->aCol[j].affinity;
danielk19770202b292004-06-09 09:55:16 +0000730 pExpr->pColl = pTab->aCol[j].pColl;
drh8141f612004-01-25 22:44:58 +0000731 break;
732 }
733 }
734 }
735 }
736
737 /*
738 ** Perhaps the name is a reference to the ROWID
739 */
danielk19774adee202004-05-08 08:23:19 +0000740 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
drh8141f612004-01-25 22:44:58 +0000741 cnt = 1;
742 pExpr->iColumn = -1;
danielk1977a37cdde2004-05-16 11:15:36 +0000743 pExpr->affinity = SQLITE_AFF_INTEGER;
drh8141f612004-01-25 22:44:58 +0000744 }
745
746 /*
747 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
748 ** might refer to an result-set alias. This happens, for example, when
749 ** we are resolving names in the WHERE clause of the following command:
750 **
751 ** SELECT a+b AS x FROM table WHERE x<10;
752 **
753 ** In cases like this, replace pExpr with a copy of the expression that
754 ** forms the result set entry ("a+b" in the example) and return immediately.
755 ** Note that the expression in the result set should have already been
756 ** resolved by the time the WHERE clause is resolved.
757 */
758 if( cnt==0 && pEList!=0 ){
759 for(j=0; j<pEList->nExpr; j++){
760 char *zAs = pEList->a[j].zName;
danielk19774adee202004-05-08 08:23:19 +0000761 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
drh8141f612004-01-25 22:44:58 +0000762 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
763 pExpr->op = TK_AS;
764 pExpr->iColumn = j;
danielk19774adee202004-05-08 08:23:19 +0000765 pExpr->pLeft = sqlite3ExprDup(pEList->a[j].pExpr);
drh8141f612004-01-25 22:44:58 +0000766 sqliteFree(zCol);
767 assert( zTab==0 && zDb==0 );
768 return 0;
769 }
770 }
771 }
772
773 /*
774 ** If X and Y are NULL (in other words if only the column name Z is
775 ** supplied) and the value of Z is enclosed in double-quotes, then
776 ** Z is a string literal if it doesn't match any column names. In that
777 ** case, we need to return right away and not make any changes to
778 ** pExpr.
779 */
780 if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
781 sqliteFree(zCol);
782 return 0;
783 }
784
785 /*
786 ** cnt==0 means there was not match. cnt>1 means there were two or
787 ** more matches. Either way, we have an error.
788 */
789 if( cnt!=1 ){
790 char *z = 0;
791 char *zErr;
792 zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s";
793 if( zDb ){
danielk19774adee202004-05-08 08:23:19 +0000794 sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, 0);
drh8141f612004-01-25 22:44:58 +0000795 }else if( zTab ){
danielk19774adee202004-05-08 08:23:19 +0000796 sqlite3SetString(&z, zTab, ".", zCol, 0);
drh8141f612004-01-25 22:44:58 +0000797 }else{
798 z = sqliteStrDup(zCol);
799 }
danielk19774adee202004-05-08 08:23:19 +0000800 sqlite3ErrorMsg(pParse, zErr, z);
drh8141f612004-01-25 22:44:58 +0000801 sqliteFree(z);
802 }
803
804 /* Clean up and return
805 */
806 sqliteFree(zDb);
807 sqliteFree(zTab);
808 sqliteFree(zCol);
danielk19774adee202004-05-08 08:23:19 +0000809 sqlite3ExprDelete(pExpr->pLeft);
drh8141f612004-01-25 22:44:58 +0000810 pExpr->pLeft = 0;
danielk19774adee202004-05-08 08:23:19 +0000811 sqlite3ExprDelete(pExpr->pRight);
drh8141f612004-01-25 22:44:58 +0000812 pExpr->pRight = 0;
813 pExpr->op = TK_COLUMN;
danielk19774adee202004-05-08 08:23:19 +0000814 sqlite3AuthRead(pParse, pExpr, pSrcList);
drh8141f612004-01-25 22:44:58 +0000815 return cnt!=1;
816}
817
818/*
drhcce7d172000-05-31 15:34:51 +0000819** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +0000820** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +0000821** index to the table in the table list and a column offset. The
822** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
823** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +0000824** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +0000825** VDBE cursor number for a cursor that is pointing into the referenced
826** table. The Expr.iColumn value is changed to the index of the column
827** of the referenced table. The Expr.iColumn value for the special
828** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
829** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +0000830**
drhfef52082000-06-06 01:50:43 +0000831** We also check for instances of the IN operator. IN comes in two
832** forms:
833**
834** expr IN (exprlist)
835** and
836** expr IN (SELECT ...)
837**
838** The first form is handled by creating a set holding the list
839** of allowed values. The second form causes the SELECT to generate
840** a temporary table.
841**
842** This routine also looks for scalar SELECTs that are part of an expression.
drh19a775c2000-06-05 18:54:46 +0000843** If it finds any, it generates code to write the value of that select
844** into a memory cell.
drhcce7d172000-05-31 15:34:51 +0000845**
drh967e8b72000-06-21 13:59:10 +0000846** Unknown columns or tables provoke an error. The function returns
drhcce7d172000-05-31 15:34:51 +0000847** the number of errors seen and leaves an error message on pParse->zErrMsg.
848*/
danielk19774adee202004-05-08 08:23:19 +0000849int sqlite3ExprResolveIds(
drha2e00042002-01-22 03:13:42 +0000850 Parse *pParse, /* The parser context */
drh8141f612004-01-25 22:44:58 +0000851 SrcList *pSrcList, /* List of tables used to resolve column names */
drha2e00042002-01-22 03:13:42 +0000852 ExprList *pEList, /* List of expressions used to resolve "AS" */
853 Expr *pExpr /* The expression to be analyzed. */
854){
drh6a3ea0e2003-05-02 14:32:12 +0000855 int i;
856
drh8141f612004-01-25 22:44:58 +0000857 if( pExpr==0 || pSrcList==0 ) return 0;
858 for(i=0; i<pSrcList->nSrc; i++){
859 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab );
drh6a3ea0e2003-05-02 14:32:12 +0000860 }
drhcce7d172000-05-31 15:34:51 +0000861 switch( pExpr->op ){
drh23989372002-05-21 13:43:04 +0000862 /* Double-quoted strings (ex: "abc") are used as identifiers if
863 ** possible. Otherwise they remain as strings. Single-quoted
864 ** strings (ex: 'abc') are always string literals.
865 */
866 case TK_STRING: {
867 if( pExpr->token.z[0]=='\'' ) break;
868 /* Fall thru into the TK_ID case if this is a double-quoted string */
869 }
drh8141f612004-01-25 22:44:58 +0000870 /* A lone identifier is the name of a columnd.
drha2e00042002-01-22 03:13:42 +0000871 */
drhcce7d172000-05-31 15:34:51 +0000872 case TK_ID: {
drh8141f612004-01-25 22:44:58 +0000873 if( lookupName(pParse, 0, 0, &pExpr->token, pSrcList, pEList, pExpr) ){
drhcce7d172000-05-31 15:34:51 +0000874 return 1;
drhed6c8672003-01-12 18:02:16 +0000875 }
drhcce7d172000-05-31 15:34:51 +0000876 break;
877 }
878
drhd24cc422003-03-27 12:51:24 +0000879 /* A table name and column name: ID.ID
880 ** Or a database, table and column: ID.ID.ID
881 */
drhcce7d172000-05-31 15:34:51 +0000882 case TK_DOT: {
drh8141f612004-01-25 22:44:58 +0000883 Token *pColumn;
884 Token *pTable;
885 Token *pDb;
886 Expr *pRight;
drhcce7d172000-05-31 15:34:51 +0000887
drhcce7d172000-05-31 15:34:51 +0000888 pRight = pExpr->pRight;
drhd24cc422003-03-27 12:51:24 +0000889 if( pRight->op==TK_ID ){
drh8141f612004-01-25 22:44:58 +0000890 pDb = 0;
891 pTable = &pExpr->pLeft->token;
892 pColumn = &pRight->token;
drhd24cc422003-03-27 12:51:24 +0000893 }else{
drh8141f612004-01-25 22:44:58 +0000894 assert( pRight->op==TK_DOT );
895 pDb = &pExpr->pLeft->token;
896 pTable = &pRight->pLeft->token;
897 pColumn = &pRight->pRight->token;
drhd24cc422003-03-27 12:51:24 +0000898 }
drh8141f612004-01-25 22:44:58 +0000899 if( lookupName(pParse, pDb, pTable, pColumn, pSrcList, 0, pExpr) ){
drhdaffd0e2001-04-11 14:28:42 +0000900 return 1;
901 }
drhcce7d172000-05-31 15:34:51 +0000902 break;
903 }
904
drhfef52082000-06-06 01:50:43 +0000905 case TK_IN: {
danielk1977e014a832004-05-17 10:48:57 +0000906 char affinity;
danielk19774adee202004-05-08 08:23:19 +0000907 Vdbe *v = sqlite3GetVdbe(pParse);
drhd3d39e92004-05-20 22:16:29 +0000908 KeyInfo keyInfo;
danielk19770202b292004-06-09 09:55:16 +0000909 int addr; /* Address of OP_OpenTemp instruction */
drhd3d39e92004-05-20 22:16:29 +0000910
drhfef52082000-06-06 01:50:43 +0000911 if( v==0 ) return 1;
danielk19774adee202004-05-08 08:23:19 +0000912 if( sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pLeft) ){
drhcfab11b2000-06-06 03:31:22 +0000913 return 1;
914 }
danielk1977bf3b7212004-05-18 10:06:24 +0000915 affinity = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +0000916
917 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
918 ** expression it is handled the same way. A temporary table is
919 ** filled with single-field index keys representing the results
920 ** from the SELECT or the <exprlist>.
921 **
922 ** If the 'x' expression is a column value, or the SELECT...
923 ** statement returns a column value, then the affinity of that
924 ** column is used to build the index keys. If both 'x' and the
925 ** SELECT... statement are columns, then numeric affinity is used
926 ** if either column has NUMERIC or INTEGER affinity. If neither
927 ** 'x' nor the SELECT... statement are columns, then numeric affinity
928 ** is used.
929 */
930 pExpr->iTable = pParse->nTab++;
danielk19770202b292004-06-09 09:55:16 +0000931 addr = sqlite3VdbeAddOp(v, OP_OpenTemp, pExpr->iTable, 0);
drhd3d39e92004-05-20 22:16:29 +0000932 memset(&keyInfo, 0, sizeof(keyInfo));
933 keyInfo.nField = 1;
drhf3218fe2004-05-28 08:21:02 +0000934 sqlite3VdbeAddOp(v, OP_SetNumColumns, pExpr->iTable, 1);
danielk1977e014a832004-05-17 10:48:57 +0000935
drhfef52082000-06-06 01:50:43 +0000936 if( pExpr->pSelect ){
937 /* Case 1: expr IN (SELECT ...)
938 **
danielk1977e014a832004-05-17 10:48:57 +0000939 ** Generate code to write the results of the select into the temporary
940 ** table allocated and opened above.
drhfef52082000-06-06 01:50:43 +0000941 */
danielk1977e014a832004-05-17 10:48:57 +0000942 int iParm = pExpr->iTable + (((int)affinity)<<16);
drhbe5c89a2004-07-26 00:31:09 +0000943 ExprList *pEList;
danielk1977e014a832004-05-17 10:48:57 +0000944 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
danielk1977bf3b7212004-05-18 10:06:24 +0000945 sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +0000946 pEList = pExpr->pSelect->pEList;
947 if( pEList && pEList->nExpr>0 ){
danielk19777cedc8d2004-06-10 10:50:08 +0000948 keyInfo.aColl[0] = binaryCompareCollSeq(pParse, pExpr->pLeft,
drhbe5c89a2004-07-26 00:31:09 +0000949 pEList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +0000950 }
drhfef52082000-06-06 01:50:43 +0000951 }else if( pExpr->pList ){
952 /* Case 2: expr IN (exprlist)
953 **
danielk1977e014a832004-05-17 10:48:57 +0000954 ** For each expression, build an index key from the evaluation and
955 ** store it in the temporary table. If <expr> is a column, then use
956 ** that columns affinity when building index keys. If <expr> is not
957 ** a column, use numeric affinity.
drhfef52082000-06-06 01:50:43 +0000958 */
danielk1977e014a832004-05-17 10:48:57 +0000959 int i;
danielk1977e014a832004-05-17 10:48:57 +0000960 if( !affinity ){
961 affinity = SQLITE_AFF_NUMERIC;
962 }
danielk19770202b292004-06-09 09:55:16 +0000963 keyInfo.aColl[0] = pExpr->pLeft->pColl;
danielk1977e014a832004-05-17 10:48:57 +0000964
965 /* Loop through each expression in <exprlist>. */
drhfef52082000-06-06 01:50:43 +0000966 for(i=0; i<pExpr->pList->nExpr; i++){
967 Expr *pE2 = pExpr->pList->a[i].pExpr;
danielk1977e014a832004-05-17 10:48:57 +0000968
969 /* Check that the expression is constant and valid. */
danielk19774adee202004-05-08 08:23:19 +0000970 if( !sqlite3ExprIsConstant(pE2) ){
971 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +0000972 "right-hand side of IN operator must be constant");
drhfef52082000-06-06 01:50:43 +0000973 return 1;
974 }
danielk19774adee202004-05-08 08:23:19 +0000975 if( sqlite3ExprCheck(pParse, pE2, 0, 0) ){
drh4794b982000-06-06 13:54:14 +0000976 return 1;
977 }
danielk1977e014a832004-05-17 10:48:57 +0000978
979 /* Evaluate the expression and insert it into the temp table */
980 sqlite3ExprCode(pParse, pE2);
drh94a11212004-09-25 13:12:14 +0000981 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1);
danielk19770f69c1e2004-05-29 11:24:50 +0000982 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +0000983 sqlite3VdbeAddOp(v, OP_PutStrKey, pExpr->iTable, 0);
drhfef52082000-06-06 01:50:43 +0000984 }
985 }
danielk19770202b292004-06-09 09:55:16 +0000986 sqlite3VdbeChangeP3(v, addr, (void *)&keyInfo, P3_KEYINFO);
987
drhcfab11b2000-06-06 03:31:22 +0000988 break;
drhfef52082000-06-06 01:50:43 +0000989 }
990
drh19a775c2000-06-05 18:54:46 +0000991 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +0000992 /* This has to be a scalar SELECT. Generate code to put the
993 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +0000994 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +0000995 */
drh967e8b72000-06-21 13:59:10 +0000996 pExpr->iColumn = pParse->nMem++;
danielk1977bf3b7212004-05-18 10:06:24 +0000997 if(sqlite3Select(pParse, pExpr->pSelect, SRT_Mem,pExpr->iColumn,0,0,0,0)){
drh19a775c2000-06-05 18:54:46 +0000998 return 1;
999 }
1000 break;
1001 }
1002
drhcce7d172000-05-31 15:34:51 +00001003 /* For all else, just recursively walk the tree */
1004 default: {
drh4794b982000-06-06 13:54:14 +00001005 if( pExpr->pLeft
danielk19774adee202004-05-08 08:23:19 +00001006 && sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pLeft) ){
drhcce7d172000-05-31 15:34:51 +00001007 return 1;
1008 }
1009 if( pExpr->pRight
danielk19774adee202004-05-08 08:23:19 +00001010 && sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pRight) ){
drhcce7d172000-05-31 15:34:51 +00001011 return 1;
1012 }
1013 if( pExpr->pList ){
1014 int i;
1015 ExprList *pList = pExpr->pList;
1016 for(i=0; i<pList->nExpr; i++){
drh832508b2002-03-02 17:04:07 +00001017 Expr *pArg = pList->a[i].pExpr;
danielk19774adee202004-05-08 08:23:19 +00001018 if( sqlite3ExprResolveIds(pParse, pSrcList, pEList, pArg) ){
drhcce7d172000-05-31 15:34:51 +00001019 return 1;
1020 }
1021 }
1022 }
1023 }
1024 }
1025 return 0;
1026}
1027
drhcce7d172000-05-31 15:34:51 +00001028/*
drh4b59ab52002-08-24 18:24:51 +00001029** pExpr is a node that defines a function of some kind. It might
1030** be a syntactic function like "count(x)" or it might be a function
1031** that implements an operator, like "a LIKE b".
1032**
1033** This routine makes *pzName point to the name of the function and
1034** *pnName hold the number of characters in the function name.
1035*/
1036static void getFunctionName(Expr *pExpr, const char **pzName, int *pnName){
1037 switch( pExpr->op ){
1038 case TK_FUNCTION: {
1039 *pzName = pExpr->token.z;
drh6977fea2002-10-22 23:38:04 +00001040 *pnName = pExpr->token.n;
drh4b59ab52002-08-24 18:24:51 +00001041 break;
1042 }
1043 case TK_LIKE: {
1044 *pzName = "like";
1045 *pnName = 4;
1046 break;
1047 }
1048 case TK_GLOB: {
1049 *pzName = "glob";
1050 *pnName = 4;
1051 break;
1052 }
1053 default: {
1054 *pzName = "can't happen";
1055 *pnName = 12;
1056 break;
1057 }
1058 }
1059}
1060
1061/*
drhcce7d172000-05-31 15:34:51 +00001062** Error check the functions in an expression. Make sure all
1063** function names are recognized and all functions have the correct
1064** number of arguments. Leave an error message in pParse->zErrMsg
1065** if anything is amiss. Return the number of errors.
1066**
1067** if pIsAgg is not null and this expression is an aggregate function
1068** (like count(*) or max(value)) then write a 1 into *pIsAgg.
1069*/
danielk19774adee202004-05-08 08:23:19 +00001070int sqlite3ExprCheck(Parse *pParse, Expr *pExpr, int allowAgg, int *pIsAgg){
drhcce7d172000-05-31 15:34:51 +00001071 int nErr = 0;
1072 if( pExpr==0 ) return 0;
drhcce7d172000-05-31 15:34:51 +00001073 switch( pExpr->op ){
drh4b59ab52002-08-24 18:24:51 +00001074 case TK_GLOB:
1075 case TK_LIKE:
drhcce7d172000-05-31 15:34:51 +00001076 case TK_FUNCTION: {
drhc9b84a12002-06-20 11:36:48 +00001077 int n = pExpr->pList ? pExpr->pList->nExpr : 0; /* Number of arguments */
1078 int no_such_func = 0; /* True if no such function exists */
drhc9b84a12002-06-20 11:36:48 +00001079 int wrong_num_args = 0; /* True if wrong number of arguments */
1080 int is_agg = 0; /* True if is an aggregate function */
drhcce7d172000-05-31 15:34:51 +00001081 int i;
drh4b59ab52002-08-24 18:24:51 +00001082 int nId; /* Number of characters in function name */
1083 const char *zId; /* The function name. */
drh0bce8352002-02-28 00:41:10 +00001084 FuncDef *pDef;
danielk1977d8123362004-06-12 09:25:12 +00001085 int enc = pParse->db->enc;
drh0bce8352002-02-28 00:41:10 +00001086
drh4b59ab52002-08-24 18:24:51 +00001087 getFunctionName(pExpr, &zId, &nId);
danielk1977d8123362004-06-12 09:25:12 +00001088 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001089 if( pDef==0 ){
danielk1977d8123362004-06-12 09:25:12 +00001090 pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001091 if( pDef==0 ){
drh268380c2004-02-25 13:47:31 +00001092 no_such_func = 1;
drh0bce8352002-02-28 00:41:10 +00001093 }else{
1094 wrong_num_args = 1;
drhcce7d172000-05-31 15:34:51 +00001095 }
drh0bce8352002-02-28 00:41:10 +00001096 }else{
1097 is_agg = pDef->xFunc==0;
drhcce7d172000-05-31 15:34:51 +00001098 }
drh8e0a2f92002-02-23 23:45:45 +00001099 if( is_agg && !allowAgg ){
danielk19774adee202004-05-08 08:23:19 +00001100 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId, zId);
drh8e0a2f92002-02-23 23:45:45 +00001101 nErr++;
1102 is_agg = 0;
1103 }else if( no_such_func ){
danielk19774adee202004-05-08 08:23:19 +00001104 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
drhcce7d172000-05-31 15:34:51 +00001105 nErr++;
drh8e0a2f92002-02-23 23:45:45 +00001106 }else if( wrong_num_args ){
danielk19774adee202004-05-08 08:23:19 +00001107 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
drhf7a9e1a2004-02-22 18:40:56 +00001108 nId, zId);
drh8e0a2f92002-02-23 23:45:45 +00001109 nErr++;
drhcce7d172000-05-31 15:34:51 +00001110 }
drhf7a9e1a2004-02-22 18:40:56 +00001111 if( is_agg ){
1112 pExpr->op = TK_AGG_FUNCTION;
1113 if( pIsAgg ) *pIsAgg = 1;
1114 }
drhcce7d172000-05-31 15:34:51 +00001115 for(i=0; nErr==0 && i<n; i++){
danielk19774adee202004-05-08 08:23:19 +00001116 nErr = sqlite3ExprCheck(pParse, pExpr->pList->a[i].pExpr,
drh4cfa7932000-06-08 15:10:46 +00001117 allowAgg && !is_agg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +00001118 }
danielk19770202b292004-06-09 09:55:16 +00001119 /* FIX ME: Compute pExpr->affinity based on the expected return
1120 ** type of the function
1121 */
drhcce7d172000-05-31 15:34:51 +00001122 }
1123 default: {
1124 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001125 nErr = sqlite3ExprCheck(pParse, pExpr->pLeft, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +00001126 }
1127 if( nErr==0 && pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001128 nErr = sqlite3ExprCheck(pParse, pExpr->pRight, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +00001129 }
drhfef52082000-06-06 01:50:43 +00001130 if( nErr==0 && pExpr->pList ){
1131 int n = pExpr->pList->nExpr;
1132 int i;
1133 for(i=0; nErr==0 && i<n; i++){
drh22827922000-06-06 17:27:05 +00001134 Expr *pE2 = pExpr->pList->a[i].pExpr;
danielk19774adee202004-05-08 08:23:19 +00001135 nErr = sqlite3ExprCheck(pParse, pE2, allowAgg, pIsAgg);
drhfef52082000-06-06 01:50:43 +00001136 }
1137 }
drhcce7d172000-05-31 15:34:51 +00001138 break;
1139 }
1140 }
1141 return nErr;
1142}
1143
1144/*
drh290c1942004-08-21 17:54:45 +00001145** Call sqlite3ExprResolveIds() followed by sqlite3ExprCheck().
1146**
1147** This routine is provided as a convenience since it is very common
1148** to call ResolveIds() and Check() back to back.
1149*/
1150int sqlite3ExprResolveAndCheck(
1151 Parse *pParse, /* The parser context */
1152 SrcList *pSrcList, /* List of tables used to resolve column names */
1153 ExprList *pEList, /* List of expressions used to resolve "AS" */
1154 Expr *pExpr, /* The expression to be analyzed. */
1155 int allowAgg, /* True to allow aggregate expressions */
1156 int *pIsAgg /* Set to TRUE if aggregates are found */
1157){
1158 if( pExpr==0 ) return 0;
1159 if( sqlite3ExprResolveIds(pParse,pSrcList,pEList,pExpr) ){
1160 return 1;
1161 }
1162 return sqlite3ExprCheck(pParse, pExpr, allowAgg, pIsAgg);
1163}
1164
1165/*
drhfec19aa2004-05-19 20:41:03 +00001166** Generate an instruction that will put the integer describe by
1167** text z[0..n-1] on the stack.
1168*/
1169static void codeInteger(Vdbe *v, const char *z, int n){
1170 int i;
drh6fec0762004-05-30 01:38:43 +00001171 if( sqlite3GetInt32(z, &i) ){
1172 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
1173 }else if( sqlite3FitsIn64Bits(z) ){
1174 sqlite3VdbeOp3(v, OP_Integer, 0, 0, z, n);
drhfec19aa2004-05-19 20:41:03 +00001175 }else{
1176 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n);
1177 }
1178}
1179
1180/*
drhcce7d172000-05-31 15:34:51 +00001181** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +00001182** expression and leave the result on the top of stack.
drhf2bc0132004-10-04 13:19:23 +00001183**
1184** This code depends on the fact that certain token values (ex: TK_EQ)
1185** are the same as opcode values (ex: OP_Eq) that implement the corresponding
1186** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
1187** the make process cause these values to align. Assert()s in the code
1188** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00001189*/
danielk19774adee202004-05-08 08:23:19 +00001190void sqlite3ExprCode(Parse *pParse, Expr *pExpr){
drhcce7d172000-05-31 15:34:51 +00001191 Vdbe *v = pParse->pVdbe;
1192 int op;
drhdaffd0e2001-04-11 14:28:42 +00001193 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00001194 op = pExpr->op;
1195 switch( op ){
drh967e8b72000-06-21 13:59:10 +00001196 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +00001197 if( pParse->useAgg ){
danielk19774adee202004-05-08 08:23:19 +00001198 sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drhc4a3c772001-04-04 11:48:57 +00001199 }else if( pExpr->iColumn>=0 ){
danielk19774adee202004-05-08 08:23:19 +00001200 sqlite3VdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn);
drh145716b2004-09-24 12:24:06 +00001201#ifndef NDEBUG
1202 if( pExpr->span.z && pExpr->span.n>0 && pExpr->span.n<100 ){
1203 VdbeComment((v, "# %T", &pExpr->span));
1204 }
1205#endif
drhc4a3c772001-04-04 11:48:57 +00001206 }else{
danielk19774adee202004-05-08 08:23:19 +00001207 sqlite3VdbeAddOp(v, OP_Recno, pExpr->iTable, 0);
drh22827922000-06-06 17:27:05 +00001208 }
drhcce7d172000-05-31 15:34:51 +00001209 break;
1210 }
1211 case TK_INTEGER: {
drhfec19aa2004-05-19 20:41:03 +00001212 codeInteger(v, pExpr->token.z, pExpr->token.n);
1213 break;
1214 }
1215 case TK_FLOAT:
1216 case TK_STRING: {
drhf2bc0132004-10-04 13:19:23 +00001217 assert( TK_FLOAT==OP_Real );
1218 assert( TK_STRING==OP_String8 );
drhfec19aa2004-05-19 20:41:03 +00001219 sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z, pExpr->token.n);
danielk19774adee202004-05-08 08:23:19 +00001220 sqlite3VdbeDequoteP3(v, -1);
drhcce7d172000-05-31 15:34:51 +00001221 break;
1222 }
danielk1977c572ef72004-05-27 09:28:41 +00001223 case TK_BLOB: {
drhf2bc0132004-10-04 13:19:23 +00001224 assert( TK_BLOB==OP_HexBlob );
danielk1977c572ef72004-05-27 09:28:41 +00001225 sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z+1, pExpr->token.n-1);
1226 sqlite3VdbeDequoteP3(v, -1);
1227 break;
1228 }
drhcce7d172000-05-31 15:34:51 +00001229 case TK_NULL: {
danielk19770f69c1e2004-05-29 11:24:50 +00001230 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001231 break;
1232 }
drh50457892003-09-06 01:10:47 +00001233 case TK_VARIABLE: {
danielk19774adee202004-05-08 08:23:19 +00001234 sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0);
drh895d7472004-08-20 16:02:39 +00001235 if( pExpr->token.n>1 ){
1236 sqlite3VdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
1237 }
drh50457892003-09-06 01:10:47 +00001238 break;
1239 }
drhc9b84a12002-06-20 11:36:48 +00001240 case TK_LT:
1241 case TK_LE:
1242 case TK_GT:
1243 case TK_GE:
1244 case TK_NE:
1245 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00001246 assert( TK_LT==OP_Lt );
1247 assert( TK_LE==OP_Le );
1248 assert( TK_GT==OP_Gt );
1249 assert( TK_GE==OP_Ge );
1250 assert( TK_EQ==OP_Eq );
1251 assert( TK_NE==OP_Ne );
danielk1977a37cdde2004-05-16 11:15:36 +00001252 sqlite3ExprCode(pParse, pExpr->pLeft);
1253 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001254 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 0, 0);
danielk1977a37cdde2004-05-16 11:15:36 +00001255 break;
drhc9b84a12002-06-20 11:36:48 +00001256 }
drhcce7d172000-05-31 15:34:51 +00001257 case TK_AND:
1258 case TK_OR:
1259 case TK_PLUS:
1260 case TK_STAR:
1261 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00001262 case TK_REM:
1263 case TK_BITAND:
1264 case TK_BITOR:
drh17c40292004-07-21 02:53:29 +00001265 case TK_SLASH:
drhbf4133c2001-10-13 02:59:08 +00001266 case TK_LSHIFT:
drh855eb1c2004-08-31 13:45:11 +00001267 case TK_RSHIFT:
drh00400772000-06-16 20:51:26 +00001268 case TK_CONCAT: {
drhf2bc0132004-10-04 13:19:23 +00001269 assert( TK_AND==OP_And );
1270 assert( TK_OR==OP_Or );
1271 assert( TK_PLUS==OP_Add );
1272 assert( TK_MINUS==OP_Subtract );
1273 assert( TK_REM==OP_Remainder );
1274 assert( TK_BITAND==OP_BitAnd );
1275 assert( TK_BITOR==OP_BitOr );
1276 assert( TK_SLASH==OP_Divide );
1277 assert( TK_LSHIFT==OP_ShiftLeft );
1278 assert( TK_RSHIFT==OP_ShiftRight );
1279 assert( TK_CONCAT==OP_Concat );
danielk19774adee202004-05-08 08:23:19 +00001280 sqlite3ExprCode(pParse, pExpr->pLeft);
1281 sqlite3ExprCode(pParse, pExpr->pRight);
drh855eb1c2004-08-31 13:45:11 +00001282 sqlite3VdbeAddOp(v, op, 0, 0);
drh00400772000-06-16 20:51:26 +00001283 break;
1284 }
drhcce7d172000-05-31 15:34:51 +00001285 case TK_UMINUS: {
drhfec19aa2004-05-19 20:41:03 +00001286 Expr *pLeft = pExpr->pLeft;
1287 assert( pLeft );
1288 if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
1289 Token *p = &pLeft->token;
drh6e142f52000-06-08 13:36:40 +00001290 char *z = sqliteMalloc( p->n + 2 );
1291 sprintf(z, "-%.*s", p->n, p->z);
drhfec19aa2004-05-19 20:41:03 +00001292 if( pLeft->op==TK_FLOAT ){
1293 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001294 }else{
drhfec19aa2004-05-19 20:41:03 +00001295 codeInteger(v, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001296 }
drh6e142f52000-06-08 13:36:40 +00001297 sqliteFree(z);
1298 break;
1299 }
drh1ccde152000-06-17 13:12:39 +00001300 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +00001301 }
drhbf4133c2001-10-13 02:59:08 +00001302 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00001303 case TK_NOT: {
drhf2bc0132004-10-04 13:19:23 +00001304 assert( TK_BITNOT==OP_BitNot );
1305 assert( TK_NOT==OP_Not );
danielk19774adee202004-05-08 08:23:19 +00001306 sqlite3ExprCode(pParse, pExpr->pLeft);
1307 sqlite3VdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001308 break;
1309 }
1310 case TK_ISNULL:
1311 case TK_NOTNULL: {
1312 int dest;
drhf2bc0132004-10-04 13:19:23 +00001313 assert( TK_ISNULL==OP_IsNull );
1314 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00001315 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1316 sqlite3ExprCode(pParse, pExpr->pLeft);
1317 dest = sqlite3VdbeCurrentAddr(v) + 2;
1318 sqlite3VdbeAddOp(v, op, 1, dest);
1319 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);
drhf2bc0132004-10-04 13:19:23 +00001320 break;
drhcce7d172000-05-31 15:34:51 +00001321 }
drh22827922000-06-06 17:27:05 +00001322 case TK_AGG_FUNCTION: {
danielk19774adee202004-05-08 08:23:19 +00001323 sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drh22827922000-06-06 17:27:05 +00001324 break;
1325 }
drh4b59ab52002-08-24 18:24:51 +00001326 case TK_GLOB:
1327 case TK_LIKE:
drhcce7d172000-05-31 15:34:51 +00001328 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00001329 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00001330 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00001331 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00001332 int nId;
1333 const char *zId;
danielk1977682f68b2004-06-05 10:22:17 +00001334 int p2 = 0;
1335 int i;
danielk1977d8123362004-06-12 09:25:12 +00001336 u8 enc = pParse->db->enc;
danielk1977dc1bdc42004-06-11 10:51:27 +00001337 CollSeq *pColl = 0;
drh4b59ab52002-08-24 18:24:51 +00001338 getFunctionName(pExpr, &zId, &nId);
danielk1977d8123362004-06-12 09:25:12 +00001339 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001340 assert( pDef!=0 );
drhf9b596e2004-05-26 16:54:42 +00001341 nExpr = sqlite3ExprCodeExprList(pParse, pList);
danielk1977682f68b2004-06-05 10:22:17 +00001342 for(i=0; i<nExpr && i<32; i++){
danielk1977d02eb1f2004-06-06 09:44:03 +00001343 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
1344 p2 |= (1<<i);
1345 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001346 if( pDef->needCollSeq && !pColl ){
1347 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
1348 }
1349 }
1350 if( pDef->needCollSeq ){
1351 if( !pColl ) pColl = pParse->db->pDfltColl;
danielk1977d8123362004-06-12 09:25:12 +00001352 sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ);
danielk1977682f68b2004-06-05 10:22:17 +00001353 }
1354 sqlite3VdbeOp3(v, OP_Function, nExpr, p2, (char*)pDef, P3_FUNCDEF);
drhcce7d172000-05-31 15:34:51 +00001355 break;
1356 }
drh19a775c2000-06-05 18:54:46 +00001357 case TK_SELECT: {
danielk19774adee202004-05-08 08:23:19 +00001358 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drhad6d9462004-09-19 02:15:24 +00001359 VdbeComment((v, "# load subquery result"));
drh19a775c2000-06-05 18:54:46 +00001360 break;
1361 }
drhfef52082000-06-06 01:50:43 +00001362 case TK_IN: {
1363 int addr;
drh94a11212004-09-25 13:12:14 +00001364 char affinity;
danielk1977e014a832004-05-17 10:48:57 +00001365
1366 /* Figure out the affinity to use to create a key from the results
1367 ** of the expression. affinityStr stores a static string suitable for
danielk1977ededfd52004-06-17 07:53:01 +00001368 ** P3 of OP_MakeRecord.
danielk1977e014a832004-05-17 10:48:57 +00001369 */
drh94a11212004-09-25 13:12:14 +00001370 affinity = comparisonAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +00001371
danielk19774adee202004-05-08 08:23:19 +00001372 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
danielk1977e014a832004-05-17 10:48:57 +00001373
1374 /* Code the <expr> from "<expr> IN (...)". The temporary table
1375 ** pExpr->iTable contains the values that make up the (...) set.
1376 */
danielk19774adee202004-05-08 08:23:19 +00001377 sqlite3ExprCode(pParse, pExpr->pLeft);
1378 addr = sqlite3VdbeCurrentAddr(v);
danielk1977e014a832004-05-17 10:48:57 +00001379 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); /* addr + 0 */
danielk19774adee202004-05-08 08:23:19 +00001380 sqlite3VdbeAddOp(v, OP_Pop, 2, 0);
danielk19770f69c1e2004-05-29 11:24:50 +00001381 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +00001382 sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7);
drh94a11212004-09-25 13:12:14 +00001383 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1); /* addr + 4 */
danielk1977e014a832004-05-17 10:48:57 +00001384 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7);
1385 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); /* addr + 6 */
1386
drhfef52082000-06-06 01:50:43 +00001387 break;
1388 }
1389 case TK_BETWEEN: {
drhbe5c89a2004-07-26 00:31:09 +00001390 Expr *pLeft = pExpr->pLeft;
1391 struct ExprList_item *pLItem = pExpr->pList->a;
1392 Expr *pRight = pLItem->pExpr;
1393 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001394 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001395 sqlite3ExprCode(pParse, pRight);
1396 codeCompare(pParse, pLeft, pRight, OP_Ge, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001397 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
drhbe5c89a2004-07-26 00:31:09 +00001398 pLItem++;
1399 pRight = pLItem->pExpr;
1400 sqlite3ExprCode(pParse, pRight);
1401 codeCompare(pParse, pLeft, pRight, OP_Le, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001402 sqlite3VdbeAddOp(v, OP_And, 0, 0);
drhfef52082000-06-06 01:50:43 +00001403 break;
1404 }
drh51e9a442004-01-16 16:42:53 +00001405 case TK_UPLUS:
drha2e00042002-01-22 03:13:42 +00001406 case TK_AS: {
danielk19774adee202004-05-08 08:23:19 +00001407 sqlite3ExprCode(pParse, pExpr->pLeft);
drha2e00042002-01-22 03:13:42 +00001408 break;
1409 }
drh17a7f8d2002-03-24 13:13:27 +00001410 case TK_CASE: {
1411 int expr_end_label;
drhf5905aa2002-05-26 20:54:33 +00001412 int jumpInst;
1413 int addr;
1414 int nExpr;
drh17a7f8d2002-03-24 13:13:27 +00001415 int i;
drhbe5c89a2004-07-26 00:31:09 +00001416 ExprList *pEList;
1417 struct ExprList_item *aListelem;
drh17a7f8d2002-03-24 13:13:27 +00001418
1419 assert(pExpr->pList);
1420 assert((pExpr->pList->nExpr % 2) == 0);
1421 assert(pExpr->pList->nExpr > 0);
drhbe5c89a2004-07-26 00:31:09 +00001422 pEList = pExpr->pList;
1423 aListelem = pEList->a;
1424 nExpr = pEList->nExpr;
danielk19774adee202004-05-08 08:23:19 +00001425 expr_end_label = sqlite3VdbeMakeLabel(v);
drh17a7f8d2002-03-24 13:13:27 +00001426 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001427 sqlite3ExprCode(pParse, pExpr->pLeft);
drh17a7f8d2002-03-24 13:13:27 +00001428 }
drhf5905aa2002-05-26 20:54:33 +00001429 for(i=0; i<nExpr; i=i+2){
drhbe5c89a2004-07-26 00:31:09 +00001430 sqlite3ExprCode(pParse, aListelem[i].pExpr);
drh17a7f8d2002-03-24 13:13:27 +00001431 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001432 sqlite3VdbeAddOp(v, OP_Dup, 1, 1);
drhbe5c89a2004-07-26 00:31:09 +00001433 jumpInst = codeCompare(pParse, pExpr->pLeft, aListelem[i].pExpr,
1434 OP_Ne, 0, 1);
danielk19774adee202004-05-08 08:23:19 +00001435 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001436 }else{
danielk19774adee202004-05-08 08:23:19 +00001437 jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001438 }
drhbe5c89a2004-07-26 00:31:09 +00001439 sqlite3ExprCode(pParse, aListelem[i+1].pExpr);
danielk19774adee202004-05-08 08:23:19 +00001440 sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label);
1441 addr = sqlite3VdbeCurrentAddr(v);
1442 sqlite3VdbeChangeP2(v, jumpInst, addr);
drh17a7f8d2002-03-24 13:13:27 +00001443 }
drhf570f012002-05-31 15:51:25 +00001444 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001445 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhf570f012002-05-31 15:51:25 +00001446 }
drh17a7f8d2002-03-24 13:13:27 +00001447 if( pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001448 sqlite3ExprCode(pParse, pExpr->pRight);
drh17a7f8d2002-03-24 13:13:27 +00001449 }else{
danielk19770f69c1e2004-05-29 11:24:50 +00001450 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
drh17a7f8d2002-03-24 13:13:27 +00001451 }
danielk19774adee202004-05-08 08:23:19 +00001452 sqlite3VdbeResolveLabel(v, expr_end_label);
danielk19776f349032002-06-11 02:25:40 +00001453 break;
1454 }
1455 case TK_RAISE: {
1456 if( !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +00001457 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00001458 "RAISE() may only be used within a trigger-program");
danielk19776f349032002-06-11 02:25:40 +00001459 return;
1460 }
drhad6d9462004-09-19 02:15:24 +00001461 if( pExpr->iColumn!=OE_Ignore ){
1462 assert( pExpr->iColumn==OE_Rollback ||
1463 pExpr->iColumn == OE_Abort ||
1464 pExpr->iColumn == OE_Fail );
1465 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn,
1466 pExpr->token.z, pExpr->token.n);
1467 sqlite3VdbeDequoteP3(v, -1);
danielk19776f349032002-06-11 02:25:40 +00001468 } else {
drhad6d9462004-09-19 02:15:24 +00001469 assert( pExpr->iColumn == OE_Ignore );
1470 sqlite3VdbeAddOp(v, OP_ContextPop, 0, 0);
1471 sqlite3VdbeAddOp(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
1472 VdbeComment((v, "# raise(IGNORE)"));
danielk19776f349032002-06-11 02:25:40 +00001473 }
drh17a7f8d2002-03-24 13:13:27 +00001474 }
1475 break;
drhcce7d172000-05-31 15:34:51 +00001476 }
drhcce7d172000-05-31 15:34:51 +00001477}
1478
1479/*
drh268380c2004-02-25 13:47:31 +00001480** Generate code that pushes the value of every element of the given
drhf9b596e2004-05-26 16:54:42 +00001481** expression list onto the stack.
drh268380c2004-02-25 13:47:31 +00001482**
1483** Return the number of elements pushed onto the stack.
1484*/
danielk19774adee202004-05-08 08:23:19 +00001485int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00001486 Parse *pParse, /* Parsing context */
drhf9b596e2004-05-26 16:54:42 +00001487 ExprList *pList /* The expression list to be coded */
drh268380c2004-02-25 13:47:31 +00001488){
1489 struct ExprList_item *pItem;
1490 int i, n;
1491 Vdbe *v;
1492 if( pList==0 ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001493 v = sqlite3GetVdbe(pParse);
drh268380c2004-02-25 13:47:31 +00001494 n = pList->nExpr;
1495 for(pItem=pList->a, i=0; i<n; i++, pItem++){
danielk19774adee202004-05-08 08:23:19 +00001496 sqlite3ExprCode(pParse, pItem->pExpr);
drh268380c2004-02-25 13:47:31 +00001497 }
drhf9b596e2004-05-26 16:54:42 +00001498 return n;
drh268380c2004-02-25 13:47:31 +00001499}
1500
1501/*
drhcce7d172000-05-31 15:34:51 +00001502** Generate code for a boolean expression such that a jump is made
1503** to the label "dest" if the expression is true but execution
1504** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00001505**
1506** If the expression evaluates to NULL (neither true nor false), then
1507** take the jump if the jumpIfNull flag is true.
drhf2bc0132004-10-04 13:19:23 +00001508**
1509** This code depends on the fact that certain token values (ex: TK_EQ)
1510** are the same as opcode values (ex: OP_Eq) that implement the corresponding
1511** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
1512** the make process cause these values to align. Assert()s in the code
1513** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00001514*/
danielk19774adee202004-05-08 08:23:19 +00001515void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001516 Vdbe *v = pParse->pVdbe;
1517 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001518 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00001519 op = pExpr->op;
1520 switch( op ){
drhcce7d172000-05-31 15:34:51 +00001521 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001522 int d2 = sqlite3VdbeMakeLabel(v);
1523 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
1524 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
1525 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001526 break;
1527 }
1528 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001529 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
1530 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001531 break;
1532 }
1533 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001534 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001535 break;
1536 }
1537 case TK_LT:
1538 case TK_LE:
1539 case TK_GT:
1540 case TK_GE:
1541 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00001542 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00001543 assert( TK_LT==OP_Lt );
1544 assert( TK_LE==OP_Le );
1545 assert( TK_GT==OP_Gt );
1546 assert( TK_GE==OP_Ge );
1547 assert( TK_EQ==OP_Eq );
1548 assert( TK_NE==OP_Ne );
danielk19774adee202004-05-08 08:23:19 +00001549 sqlite3ExprCode(pParse, pExpr->pLeft);
1550 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001551 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001552 break;
1553 }
1554 case TK_ISNULL:
1555 case TK_NOTNULL: {
drhf2bc0132004-10-04 13:19:23 +00001556 assert( TK_ISNULL==OP_IsNull );
1557 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00001558 sqlite3ExprCode(pParse, pExpr->pLeft);
1559 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001560 break;
1561 }
drhfef52082000-06-06 01:50:43 +00001562 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00001563 /* The expression "x BETWEEN y AND z" is implemented as:
1564 **
1565 ** 1 IF (x < y) GOTO 3
1566 ** 2 IF (x <= z) GOTO <dest>
1567 ** 3 ...
1568 */
drhf5905aa2002-05-26 20:54:33 +00001569 int addr;
drhbe5c89a2004-07-26 00:31:09 +00001570 Expr *pLeft = pExpr->pLeft;
1571 Expr *pRight = pExpr->pList->a[0].pExpr;
1572 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001573 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001574 sqlite3ExprCode(pParse, pRight);
1575 addr = codeCompare(pParse, pLeft, pRight, OP_Lt, 0, !jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00001576
drhbe5c89a2004-07-26 00:31:09 +00001577 pRight = pExpr->pList->a[1].pExpr;
1578 sqlite3ExprCode(pParse, pRight);
1579 codeCompare(pParse, pLeft, pRight, OP_Le, dest, jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00001580
danielk19774adee202004-05-08 08:23:19 +00001581 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
1582 sqlite3VdbeChangeP2(v, addr, sqlite3VdbeCurrentAddr(v));
1583 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00001584 break;
1585 }
drhcce7d172000-05-31 15:34:51 +00001586 default: {
danielk19774adee202004-05-08 08:23:19 +00001587 sqlite3ExprCode(pParse, pExpr);
1588 sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001589 break;
1590 }
1591 }
1592}
1593
1594/*
drh66b89c82000-11-28 20:47:17 +00001595** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00001596** to the label "dest" if the expression is false but execution
1597** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00001598**
1599** If the expression evaluates to NULL (neither true nor false) then
1600** jump if jumpIfNull is true or fall through if jumpIfNull is false.
drhcce7d172000-05-31 15:34:51 +00001601*/
danielk19774adee202004-05-08 08:23:19 +00001602void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001603 Vdbe *v = pParse->pVdbe;
1604 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001605 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00001606
1607 /* The value of pExpr->op and op are related as follows:
1608 **
1609 ** pExpr->op op
1610 ** --------- ----------
1611 ** TK_ISNULL OP_NotNull
1612 ** TK_NOTNULL OP_IsNull
1613 ** TK_NE OP_Eq
1614 ** TK_EQ OP_Ne
1615 ** TK_GT OP_Le
1616 ** TK_LE OP_Gt
1617 ** TK_GE OP_Lt
1618 ** TK_LT OP_Ge
1619 **
1620 ** For other values of pExpr->op, op is undefined and unused.
1621 ** The value of TK_ and OP_ constants are arranged such that we
1622 ** can compute the mapping above using the following expression.
1623 ** Assert()s verify that the computation is correct.
1624 */
1625 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
1626
1627 /* Verify correct alignment of TK_ and OP_ constants
1628 */
1629 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
1630 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
1631 assert( pExpr->op!=TK_NE || op==OP_Eq );
1632 assert( pExpr->op!=TK_EQ || op==OP_Ne );
1633 assert( pExpr->op!=TK_LT || op==OP_Ge );
1634 assert( pExpr->op!=TK_LE || op==OP_Gt );
1635 assert( pExpr->op!=TK_GT || op==OP_Le );
1636 assert( pExpr->op!=TK_GE || op==OP_Lt );
1637
drhcce7d172000-05-31 15:34:51 +00001638 switch( pExpr->op ){
1639 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001640 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
1641 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001642 break;
1643 }
1644 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001645 int d2 = sqlite3VdbeMakeLabel(v);
1646 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
1647 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
1648 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001649 break;
1650 }
1651 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001652 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001653 break;
1654 }
1655 case TK_LT:
1656 case TK_LE:
1657 case TK_GT:
1658 case TK_GE:
1659 case TK_NE:
1660 case TK_EQ: {
danielk19774adee202004-05-08 08:23:19 +00001661 sqlite3ExprCode(pParse, pExpr->pLeft);
1662 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001663 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001664 break;
1665 }
drhcce7d172000-05-31 15:34:51 +00001666 case TK_ISNULL:
1667 case TK_NOTNULL: {
danielk19774adee202004-05-08 08:23:19 +00001668 sqlite3ExprCode(pParse, pExpr->pLeft);
1669 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001670 break;
1671 }
drhfef52082000-06-06 01:50:43 +00001672 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00001673 /* The expression is "x BETWEEN y AND z". It is implemented as:
1674 **
1675 ** 1 IF (x >= y) GOTO 3
1676 ** 2 GOTO <dest>
1677 ** 3 IF (x > z) GOTO <dest>
1678 */
drhfef52082000-06-06 01:50:43 +00001679 int addr;
drhbe5c89a2004-07-26 00:31:09 +00001680 Expr *pLeft = pExpr->pLeft;
1681 Expr *pRight = pExpr->pList->a[0].pExpr;
1682 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001683 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001684 sqlite3ExprCode(pParse, pRight);
danielk19774adee202004-05-08 08:23:19 +00001685 addr = sqlite3VdbeCurrentAddr(v);
drhbe5c89a2004-07-26 00:31:09 +00001686 codeCompare(pParse, pLeft, pRight, OP_Ge, addr+3, !jumpIfNull);
1687
danielk19774adee202004-05-08 08:23:19 +00001688 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
1689 sqlite3VdbeAddOp(v, OP_Goto, 0, dest);
drhbe5c89a2004-07-26 00:31:09 +00001690 pRight = pExpr->pList->a[1].pExpr;
1691 sqlite3ExprCode(pParse, pRight);
1692 codeCompare(pParse, pLeft, pRight, OP_Gt, dest, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00001693 break;
1694 }
drhcce7d172000-05-31 15:34:51 +00001695 default: {
danielk19774adee202004-05-08 08:23:19 +00001696 sqlite3ExprCode(pParse, pExpr);
1697 sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001698 break;
1699 }
1700 }
1701}
drh22827922000-06-06 17:27:05 +00001702
1703/*
1704** Do a deep comparison of two expression trees. Return TRUE (non-zero)
1705** if they are identical and return FALSE if they differ in any way.
1706*/
danielk19774adee202004-05-08 08:23:19 +00001707int sqlite3ExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00001708 int i;
1709 if( pA==0 ){
1710 return pB==0;
1711 }else if( pB==0 ){
1712 return 0;
1713 }
1714 if( pA->op!=pB->op ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001715 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
1716 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00001717 if( pA->pList ){
1718 if( pB->pList==0 ) return 0;
1719 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
1720 for(i=0; i<pA->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00001721 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00001722 return 0;
1723 }
1724 }
1725 }else if( pB->pList ){
1726 return 0;
1727 }
1728 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00001729 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drh22827922000-06-06 17:27:05 +00001730 if( pA->token.z ){
1731 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00001732 if( pB->token.n!=pA->token.n ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001733 if( sqlite3StrNICmp(pA->token.z, pB->token.z, pB->token.n)!=0 ) return 0;
drh22827922000-06-06 17:27:05 +00001734 }
1735 return 1;
1736}
1737
1738/*
1739** Add a new element to the pParse->aAgg[] array and return its index.
1740*/
1741static int appendAggInfo(Parse *pParse){
1742 if( (pParse->nAgg & 0x7)==0 ){
1743 int amt = pParse->nAgg + 8;
drh6d4abfb2001-10-22 02:58:08 +00001744 AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0]));
1745 if( aAgg==0 ){
drh22827922000-06-06 17:27:05 +00001746 return -1;
1747 }
drh6d4abfb2001-10-22 02:58:08 +00001748 pParse->aAgg = aAgg;
drh22827922000-06-06 17:27:05 +00001749 }
1750 memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0]));
1751 return pParse->nAgg++;
1752}
1753
1754/*
1755** Analyze the given expression looking for aggregate functions and
1756** for variables that need to be added to the pParse->aAgg[] array.
1757** Make additional entries to the pParse->aAgg[] array as necessary.
1758**
1759** This routine should only be called after the expression has been
danielk19774adee202004-05-08 08:23:19 +00001760** analyzed by sqlite3ExprResolveIds() and sqlite3ExprCheck().
drh22827922000-06-06 17:27:05 +00001761**
1762** If errors are seen, leave an error message in zErrMsg and return
1763** the number of errors.
1764*/
danielk19774adee202004-05-08 08:23:19 +00001765int sqlite3ExprAnalyzeAggregates(Parse *pParse, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00001766 int i;
1767 AggExpr *aAgg;
1768 int nErr = 0;
1769
1770 if( pExpr==0 ) return 0;
1771 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00001772 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +00001773 aAgg = pParse->aAgg;
1774 for(i=0; i<pParse->nAgg; i++){
1775 if( aAgg[i].isAgg ) continue;
1776 if( aAgg[i].pExpr->iTable==pExpr->iTable
drh967e8b72000-06-21 13:59:10 +00001777 && aAgg[i].pExpr->iColumn==pExpr->iColumn ){
drh22827922000-06-06 17:27:05 +00001778 break;
1779 }
1780 }
1781 if( i>=pParse->nAgg ){
1782 i = appendAggInfo(pParse);
1783 if( i<0 ) return 1;
1784 pParse->aAgg[i].isAgg = 0;
1785 pParse->aAgg[i].pExpr = pExpr;
1786 }
drhaaf88722000-06-08 11:25:00 +00001787 pExpr->iAgg = i;
drh22827922000-06-06 17:27:05 +00001788 break;
1789 }
1790 case TK_AGG_FUNCTION: {
drh22827922000-06-06 17:27:05 +00001791 aAgg = pParse->aAgg;
1792 for(i=0; i<pParse->nAgg; i++){
1793 if( !aAgg[i].isAgg ) continue;
danielk19774adee202004-05-08 08:23:19 +00001794 if( sqlite3ExprCompare(aAgg[i].pExpr, pExpr) ){
drh22827922000-06-06 17:27:05 +00001795 break;
1796 }
1797 }
1798 if( i>=pParse->nAgg ){
danielk1977d8123362004-06-12 09:25:12 +00001799 u8 enc = pParse->db->enc;
drh22827922000-06-06 17:27:05 +00001800 i = appendAggInfo(pParse);
1801 if( i<0 ) return 1;
1802 pParse->aAgg[i].isAgg = 1;
1803 pParse->aAgg[i].pExpr = pExpr;
danielk19774adee202004-05-08 08:23:19 +00001804 pParse->aAgg[i].pFunc = sqlite3FindFunction(pParse->db,
drh6977fea2002-10-22 23:38:04 +00001805 pExpr->token.z, pExpr->token.n,
danielk1977d8123362004-06-12 09:25:12 +00001806 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
drh22827922000-06-06 17:27:05 +00001807 }
1808 pExpr->iAgg = i;
1809 break;
1810 }
1811 default: {
1812 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001813 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pLeft);
drh22827922000-06-06 17:27:05 +00001814 }
1815 if( nErr==0 && pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001816 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pRight);
drh22827922000-06-06 17:27:05 +00001817 }
1818 if( nErr==0 && pExpr->pList ){
1819 int n = pExpr->pList->nExpr;
1820 int i;
1821 for(i=0; nErr==0 && i<n; i++){
danielk19774adee202004-05-08 08:23:19 +00001822 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pList->a[i].pExpr);
drh22827922000-06-06 17:27:05 +00001823 }
1824 }
1825 break;
1826 }
1827 }
1828 return nErr;
1829}
drh8e0a2f92002-02-23 23:45:45 +00001830
1831/*
danielk1977d02eb1f2004-06-06 09:44:03 +00001832** Locate a user function given a name, a number of arguments and a flag
1833** indicating whether the function prefers UTF-16 over UTF-8. Return a
1834** pointer to the FuncDef structure that defines that function, or return
1835** NULL if the function does not exist.
drh8e0a2f92002-02-23 23:45:45 +00001836**
drh0bce8352002-02-28 00:41:10 +00001837** If the createFlag argument is true, then a new (blank) FuncDef
drh8e0a2f92002-02-23 23:45:45 +00001838** structure is created and liked into the "db" structure if a
1839** no matching function previously existed. When createFlag is true
1840** and the nArg parameter is -1, then only a function that accepts
1841** any number of arguments will be returned.
1842**
1843** If createFlag is false and nArg is -1, then the first valid
1844** function found is returned. A function is valid if either xFunc
1845** or xStep is non-zero.
danielk1977d02eb1f2004-06-06 09:44:03 +00001846**
1847** If createFlag is false, then a function with the required name and
1848** number of arguments may be returned even if the eTextRep flag does not
1849** match that requested.
drh8e0a2f92002-02-23 23:45:45 +00001850*/
danielk19774adee202004-05-08 08:23:19 +00001851FuncDef *sqlite3FindFunction(
drh9bb575f2004-09-06 17:24:11 +00001852 sqlite3 *db, /* An open database */
drh8e0a2f92002-02-23 23:45:45 +00001853 const char *zName, /* Name of the function. Not null-terminated */
1854 int nName, /* Number of characters in the name */
1855 int nArg, /* Number of arguments. -1 means any number */
danielk1977d8123362004-06-12 09:25:12 +00001856 u8 enc, /* Preferred text encoding */
drh8e0a2f92002-02-23 23:45:45 +00001857 int createFlag /* Create new entry if true and does not otherwise exist */
1858){
danielk1977d02eb1f2004-06-06 09:44:03 +00001859 FuncDef *p; /* Iterator variable */
1860 FuncDef *pFirst; /* First function with this name */
1861 FuncDef *pBest = 0; /* Best match found so far */
danielk1977d8123362004-06-12 09:25:12 +00001862 int bestmatch = 0;
danielk1977d02eb1f2004-06-06 09:44:03 +00001863
danielk1977d8123362004-06-12 09:25:12 +00001864
1865 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
danielk1977d02eb1f2004-06-06 09:44:03 +00001866 if( nArg<-1 ) nArg = -1;
1867
1868 pFirst = (FuncDef*)sqlite3HashFind(&db->aFunc, zName, nName);
1869 for(p=pFirst; p; p=p->pNext){
danielk1977d8123362004-06-12 09:25:12 +00001870 /* During the search for the best function definition, bestmatch is set
1871 ** as follows to indicate the quality of the match with the definition
1872 ** pointed to by pBest:
1873 **
1874 ** 0: pBest is NULL. No match has been found.
1875 ** 1: A variable arguments function that prefers UTF-8 when a UTF-16
1876 ** encoding is requested, or vice versa.
1877 ** 2: A variable arguments function that uses UTF-16BE when UTF-16LE is
1878 ** requested, or vice versa.
1879 ** 3: A variable arguments function using the same text encoding.
1880 ** 4: A function with the exact number of arguments requested that
1881 ** prefers UTF-8 when a UTF-16 encoding is requested, or vice versa.
1882 ** 5: A function with the exact number of arguments requested that
1883 ** prefers UTF-16LE when UTF-16BE is requested, or vice versa.
1884 ** 6: An exact match.
1885 **
1886 ** A larger value of 'matchqual' indicates a more desirable match.
1887 */
danielk1977e12c17b2004-06-23 12:35:14 +00001888 if( p->nArg==-1 || p->nArg==nArg || nArg==-1 ){
danielk1977d8123362004-06-12 09:25:12 +00001889 int match = 1; /* Quality of this match */
1890 if( p->nArg==nArg || nArg==-1 ){
1891 match = 4;
danielk1977d02eb1f2004-06-06 09:44:03 +00001892 }
danielk1977d8123362004-06-12 09:25:12 +00001893 if( enc==p->iPrefEnc ){
1894 match += 2;
danielk1977d02eb1f2004-06-06 09:44:03 +00001895 }
danielk1977d8123362004-06-12 09:25:12 +00001896 else if( (enc==SQLITE_UTF16LE && p->iPrefEnc==SQLITE_UTF16BE) ||
1897 (enc==SQLITE_UTF16BE && p->iPrefEnc==SQLITE_UTF16LE) ){
1898 match += 1;
1899 }
1900
1901 if( match>bestmatch ){
1902 pBest = p;
1903 bestmatch = match;
danielk1977d02eb1f2004-06-06 09:44:03 +00001904 }
1905 }
drh8e0a2f92002-02-23 23:45:45 +00001906 }
danielk1977d02eb1f2004-06-06 09:44:03 +00001907
danielk1977d8123362004-06-12 09:25:12 +00001908 /* If the createFlag parameter is true, and the seach did not reveal an
1909 ** exact match for the name, number of arguments and encoding, then add a
1910 ** new entry to the hash table and return it.
1911 */
1912 if( createFlag && bestmatch<6 &&
danielk1977d02eb1f2004-06-06 09:44:03 +00001913 (pBest = sqliteMalloc(sizeof(*pBest)+nName+1)) ){
1914 pBest->nArg = nArg;
1915 pBest->pNext = pFirst;
1916 pBest->zName = (char*)&pBest[1];
danielk1977d8123362004-06-12 09:25:12 +00001917 pBest->iPrefEnc = enc;
danielk1977d02eb1f2004-06-06 09:44:03 +00001918 memcpy(pBest->zName, zName, nName);
1919 pBest->zName[nName] = 0;
1920 sqlite3HashInsert(&db->aFunc, pBest->zName, nName, (void*)pBest);
drh8e0a2f92002-02-23 23:45:45 +00001921 }
danielk1977d02eb1f2004-06-06 09:44:03 +00001922
1923 if( pBest && (pBest->xStep || pBest->xFunc || createFlag) ){
1924 return pBest;
drh8e0a2f92002-02-23 23:45:45 +00001925 }
danielk1977d02eb1f2004-06-06 09:44:03 +00001926 return 0;
drh8e0a2f92002-02-23 23:45:45 +00001927}