blob: a07fdcd00311c5c9990212c7b72857a932bf808e [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**
drh94a11212004-09-25 13:12:14 +000015** $Id: expr.c,v 1.165 2004/09/25 13:12:15 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.
drhcce7d172000-05-31 15:34:51 +00001183*/
danielk19774adee202004-05-08 08:23:19 +00001184void sqlite3ExprCode(Parse *pParse, Expr *pExpr){
drhcce7d172000-05-31 15:34:51 +00001185 Vdbe *v = pParse->pVdbe;
1186 int op;
drhdaffd0e2001-04-11 14:28:42 +00001187 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001188 switch( pExpr->op ){
1189 case TK_PLUS: op = OP_Add; break;
1190 case TK_MINUS: op = OP_Subtract; break;
1191 case TK_STAR: op = OP_Multiply; break;
1192 case TK_SLASH: op = OP_Divide; break;
1193 case TK_AND: op = OP_And; break;
1194 case TK_OR: op = OP_Or; break;
1195 case TK_LT: op = OP_Lt; break;
1196 case TK_LE: op = OP_Le; break;
1197 case TK_GT: op = OP_Gt; break;
1198 case TK_GE: op = OP_Ge; break;
1199 case TK_NE: op = OP_Ne; break;
1200 case TK_EQ: op = OP_Eq; break;
drhcce7d172000-05-31 15:34:51 +00001201 case TK_ISNULL: op = OP_IsNull; break;
1202 case TK_NOTNULL: op = OP_NotNull; break;
1203 case TK_NOT: op = OP_Not; break;
1204 case TK_UMINUS: op = OP_Negative; break;
drhbf4133c2001-10-13 02:59:08 +00001205 case TK_BITAND: op = OP_BitAnd; break;
1206 case TK_BITOR: op = OP_BitOr; break;
1207 case TK_BITNOT: op = OP_BitNot; break;
1208 case TK_LSHIFT: op = OP_ShiftLeft; break;
1209 case TK_RSHIFT: op = OP_ShiftRight; break;
1210 case TK_REM: op = OP_Remainder; break;
drhfec19aa2004-05-19 20:41:03 +00001211 case TK_FLOAT: op = OP_Real; break;
drh855eb1c2004-08-31 13:45:11 +00001212 case TK_STRING: op = OP_String8; break;
danielk1977c572ef72004-05-27 09:28:41 +00001213 case TK_BLOB: op = OP_HexBlob; break;
drh855eb1c2004-08-31 13:45:11 +00001214 case TK_CONCAT: op = OP_Concat; break;
danielk1977cfe9a692004-06-16 12:00:29 +00001215 default: op = 0; break;
drhcce7d172000-05-31 15:34:51 +00001216 }
1217 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00001218 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +00001219 if( pParse->useAgg ){
danielk19774adee202004-05-08 08:23:19 +00001220 sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drhc4a3c772001-04-04 11:48:57 +00001221 }else if( pExpr->iColumn>=0 ){
danielk19774adee202004-05-08 08:23:19 +00001222 sqlite3VdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn);
drh145716b2004-09-24 12:24:06 +00001223#ifndef NDEBUG
1224 if( pExpr->span.z && pExpr->span.n>0 && pExpr->span.n<100 ){
1225 VdbeComment((v, "# %T", &pExpr->span));
1226 }
1227#endif
drhc4a3c772001-04-04 11:48:57 +00001228 }else{
danielk19774adee202004-05-08 08:23:19 +00001229 sqlite3VdbeAddOp(v, OP_Recno, pExpr->iTable, 0);
drh22827922000-06-06 17:27:05 +00001230 }
drhcce7d172000-05-31 15:34:51 +00001231 break;
1232 }
1233 case TK_INTEGER: {
drhfec19aa2004-05-19 20:41:03 +00001234 codeInteger(v, pExpr->token.z, pExpr->token.n);
1235 break;
1236 }
1237 case TK_FLOAT:
1238 case TK_STRING: {
1239 sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z, pExpr->token.n);
danielk19774adee202004-05-08 08:23:19 +00001240 sqlite3VdbeDequoteP3(v, -1);
drhcce7d172000-05-31 15:34:51 +00001241 break;
1242 }
danielk1977c572ef72004-05-27 09:28:41 +00001243 case TK_BLOB: {
1244 sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z+1, pExpr->token.n-1);
1245 sqlite3VdbeDequoteP3(v, -1);
1246 break;
1247 }
drhcce7d172000-05-31 15:34:51 +00001248 case TK_NULL: {
danielk19770f69c1e2004-05-29 11:24:50 +00001249 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001250 break;
1251 }
drh50457892003-09-06 01:10:47 +00001252 case TK_VARIABLE: {
danielk19774adee202004-05-08 08:23:19 +00001253 sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0);
drh895d7472004-08-20 16:02:39 +00001254 if( pExpr->token.n>1 ){
1255 sqlite3VdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
1256 }
drh50457892003-09-06 01:10:47 +00001257 break;
1258 }
drhc9b84a12002-06-20 11:36:48 +00001259 case TK_LT:
1260 case TK_LE:
1261 case TK_GT:
1262 case TK_GE:
1263 case TK_NE:
1264 case TK_EQ: {
danielk1977a37cdde2004-05-16 11:15:36 +00001265 sqlite3ExprCode(pParse, pExpr->pLeft);
1266 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001267 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 0, 0);
danielk1977a37cdde2004-05-16 11:15:36 +00001268 break;
drhc9b84a12002-06-20 11:36:48 +00001269 }
drhcce7d172000-05-31 15:34:51 +00001270 case TK_AND:
1271 case TK_OR:
1272 case TK_PLUS:
1273 case TK_STAR:
1274 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00001275 case TK_REM:
1276 case TK_BITAND:
1277 case TK_BITOR:
drh17c40292004-07-21 02:53:29 +00001278 case TK_SLASH:
drhbf4133c2001-10-13 02:59:08 +00001279 case TK_LSHIFT:
drh855eb1c2004-08-31 13:45:11 +00001280 case TK_RSHIFT:
drh00400772000-06-16 20:51:26 +00001281 case TK_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: {
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;
danielk19774adee202004-05-08 08:23:19 +00001313 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1314 sqlite3ExprCode(pParse, pExpr->pLeft);
1315 dest = sqlite3VdbeCurrentAddr(v) + 2;
1316 sqlite3VdbeAddOp(v, op, 1, dest);
1317 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);
drhcce7d172000-05-31 15:34:51 +00001318 }
danielk1977a37cdde2004-05-16 11:15:36 +00001319 break;
drh22827922000-06-06 17:27:05 +00001320 case TK_AGG_FUNCTION: {
danielk19774adee202004-05-08 08:23:19 +00001321 sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drh22827922000-06-06 17:27:05 +00001322 break;
1323 }
drh4b59ab52002-08-24 18:24:51 +00001324 case TK_GLOB:
1325 case TK_LIKE:
drhcce7d172000-05-31 15:34:51 +00001326 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00001327 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00001328 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00001329 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00001330 int nId;
1331 const char *zId;
danielk1977682f68b2004-06-05 10:22:17 +00001332 int p2 = 0;
1333 int i;
danielk1977d8123362004-06-12 09:25:12 +00001334 u8 enc = pParse->db->enc;
danielk1977dc1bdc42004-06-11 10:51:27 +00001335 CollSeq *pColl = 0;
drh4b59ab52002-08-24 18:24:51 +00001336 getFunctionName(pExpr, &zId, &nId);
danielk1977d8123362004-06-12 09:25:12 +00001337 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001338 assert( pDef!=0 );
drhf9b596e2004-05-26 16:54:42 +00001339 nExpr = sqlite3ExprCodeExprList(pParse, pList);
danielk1977682f68b2004-06-05 10:22:17 +00001340 for(i=0; i<nExpr && i<32; i++){
danielk1977d02eb1f2004-06-06 09:44:03 +00001341 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
1342 p2 |= (1<<i);
1343 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001344 if( pDef->needCollSeq && !pColl ){
1345 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
1346 }
1347 }
1348 if( pDef->needCollSeq ){
1349 if( !pColl ) pColl = pParse->db->pDfltColl;
danielk1977d8123362004-06-12 09:25:12 +00001350 sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ);
danielk1977682f68b2004-06-05 10:22:17 +00001351 }
1352 sqlite3VdbeOp3(v, OP_Function, nExpr, p2, (char*)pDef, P3_FUNCDEF);
drhcce7d172000-05-31 15:34:51 +00001353 break;
1354 }
drh19a775c2000-06-05 18:54:46 +00001355 case TK_SELECT: {
danielk19774adee202004-05-08 08:23:19 +00001356 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drhad6d9462004-09-19 02:15:24 +00001357 VdbeComment((v, "# load subquery result"));
drh19a775c2000-06-05 18:54:46 +00001358 break;
1359 }
drhfef52082000-06-06 01:50:43 +00001360 case TK_IN: {
1361 int addr;
drh94a11212004-09-25 13:12:14 +00001362 char affinity;
danielk1977e014a832004-05-17 10:48:57 +00001363
1364 /* Figure out the affinity to use to create a key from the results
1365 ** of the expression. affinityStr stores a static string suitable for
danielk1977ededfd52004-06-17 07:53:01 +00001366 ** P3 of OP_MakeRecord.
danielk1977e014a832004-05-17 10:48:57 +00001367 */
drh94a11212004-09-25 13:12:14 +00001368 affinity = comparisonAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +00001369
danielk19774adee202004-05-08 08:23:19 +00001370 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
danielk1977e014a832004-05-17 10:48:57 +00001371
1372 /* Code the <expr> from "<expr> IN (...)". The temporary table
1373 ** pExpr->iTable contains the values that make up the (...) set.
1374 */
danielk19774adee202004-05-08 08:23:19 +00001375 sqlite3ExprCode(pParse, pExpr->pLeft);
1376 addr = sqlite3VdbeCurrentAddr(v);
danielk1977e014a832004-05-17 10:48:57 +00001377 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); /* addr + 0 */
danielk19774adee202004-05-08 08:23:19 +00001378 sqlite3VdbeAddOp(v, OP_Pop, 2, 0);
danielk19770f69c1e2004-05-29 11:24:50 +00001379 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +00001380 sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7);
drh94a11212004-09-25 13:12:14 +00001381 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1); /* addr + 4 */
danielk1977e014a832004-05-17 10:48:57 +00001382 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7);
1383 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); /* addr + 6 */
1384
drhfef52082000-06-06 01:50:43 +00001385 break;
1386 }
1387 case TK_BETWEEN: {
drhbe5c89a2004-07-26 00:31:09 +00001388 Expr *pLeft = pExpr->pLeft;
1389 struct ExprList_item *pLItem = pExpr->pList->a;
1390 Expr *pRight = pLItem->pExpr;
1391 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001392 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001393 sqlite3ExprCode(pParse, pRight);
1394 codeCompare(pParse, pLeft, pRight, OP_Ge, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001395 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
drhbe5c89a2004-07-26 00:31:09 +00001396 pLItem++;
1397 pRight = pLItem->pExpr;
1398 sqlite3ExprCode(pParse, pRight);
1399 codeCompare(pParse, pLeft, pRight, OP_Le, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001400 sqlite3VdbeAddOp(v, OP_And, 0, 0);
drhfef52082000-06-06 01:50:43 +00001401 break;
1402 }
drh51e9a442004-01-16 16:42:53 +00001403 case TK_UPLUS:
drha2e00042002-01-22 03:13:42 +00001404 case TK_AS: {
danielk19774adee202004-05-08 08:23:19 +00001405 sqlite3ExprCode(pParse, pExpr->pLeft);
drha2e00042002-01-22 03:13:42 +00001406 break;
1407 }
drh17a7f8d2002-03-24 13:13:27 +00001408 case TK_CASE: {
1409 int expr_end_label;
drhf5905aa2002-05-26 20:54:33 +00001410 int jumpInst;
1411 int addr;
1412 int nExpr;
drh17a7f8d2002-03-24 13:13:27 +00001413 int i;
drhbe5c89a2004-07-26 00:31:09 +00001414 ExprList *pEList;
1415 struct ExprList_item *aListelem;
drh17a7f8d2002-03-24 13:13:27 +00001416
1417 assert(pExpr->pList);
1418 assert((pExpr->pList->nExpr % 2) == 0);
1419 assert(pExpr->pList->nExpr > 0);
drhbe5c89a2004-07-26 00:31:09 +00001420 pEList = pExpr->pList;
1421 aListelem = pEList->a;
1422 nExpr = pEList->nExpr;
danielk19774adee202004-05-08 08:23:19 +00001423 expr_end_label = sqlite3VdbeMakeLabel(v);
drh17a7f8d2002-03-24 13:13:27 +00001424 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001425 sqlite3ExprCode(pParse, pExpr->pLeft);
drh17a7f8d2002-03-24 13:13:27 +00001426 }
drhf5905aa2002-05-26 20:54:33 +00001427 for(i=0; i<nExpr; i=i+2){
drhbe5c89a2004-07-26 00:31:09 +00001428 sqlite3ExprCode(pParse, aListelem[i].pExpr);
drh17a7f8d2002-03-24 13:13:27 +00001429 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001430 sqlite3VdbeAddOp(v, OP_Dup, 1, 1);
drhbe5c89a2004-07-26 00:31:09 +00001431 jumpInst = codeCompare(pParse, pExpr->pLeft, aListelem[i].pExpr,
1432 OP_Ne, 0, 1);
danielk19774adee202004-05-08 08:23:19 +00001433 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001434 }else{
danielk19774adee202004-05-08 08:23:19 +00001435 jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001436 }
drhbe5c89a2004-07-26 00:31:09 +00001437 sqlite3ExprCode(pParse, aListelem[i+1].pExpr);
danielk19774adee202004-05-08 08:23:19 +00001438 sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label);
1439 addr = sqlite3VdbeCurrentAddr(v);
1440 sqlite3VdbeChangeP2(v, jumpInst, addr);
drh17a7f8d2002-03-24 13:13:27 +00001441 }
drhf570f012002-05-31 15:51:25 +00001442 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001443 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhf570f012002-05-31 15:51:25 +00001444 }
drh17a7f8d2002-03-24 13:13:27 +00001445 if( pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001446 sqlite3ExprCode(pParse, pExpr->pRight);
drh17a7f8d2002-03-24 13:13:27 +00001447 }else{
danielk19770f69c1e2004-05-29 11:24:50 +00001448 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
drh17a7f8d2002-03-24 13:13:27 +00001449 }
danielk19774adee202004-05-08 08:23:19 +00001450 sqlite3VdbeResolveLabel(v, expr_end_label);
danielk19776f349032002-06-11 02:25:40 +00001451 break;
1452 }
1453 case TK_RAISE: {
1454 if( !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +00001455 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00001456 "RAISE() may only be used within a trigger-program");
danielk19776f349032002-06-11 02:25:40 +00001457 return;
1458 }
drhad6d9462004-09-19 02:15:24 +00001459 if( pExpr->iColumn!=OE_Ignore ){
1460 assert( pExpr->iColumn==OE_Rollback ||
1461 pExpr->iColumn == OE_Abort ||
1462 pExpr->iColumn == OE_Fail );
1463 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn,
1464 pExpr->token.z, pExpr->token.n);
1465 sqlite3VdbeDequoteP3(v, -1);
danielk19776f349032002-06-11 02:25:40 +00001466 } else {
drhad6d9462004-09-19 02:15:24 +00001467 assert( pExpr->iColumn == OE_Ignore );
1468 sqlite3VdbeAddOp(v, OP_ContextPop, 0, 0);
1469 sqlite3VdbeAddOp(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
1470 VdbeComment((v, "# raise(IGNORE)"));
danielk19776f349032002-06-11 02:25:40 +00001471 }
drh17a7f8d2002-03-24 13:13:27 +00001472 }
1473 break;
drhcce7d172000-05-31 15:34:51 +00001474 }
drhcce7d172000-05-31 15:34:51 +00001475}
1476
1477/*
drh268380c2004-02-25 13:47:31 +00001478** Generate code that pushes the value of every element of the given
drhf9b596e2004-05-26 16:54:42 +00001479** expression list onto the stack.
drh268380c2004-02-25 13:47:31 +00001480**
1481** Return the number of elements pushed onto the stack.
1482*/
danielk19774adee202004-05-08 08:23:19 +00001483int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00001484 Parse *pParse, /* Parsing context */
drhf9b596e2004-05-26 16:54:42 +00001485 ExprList *pList /* The expression list to be coded */
drh268380c2004-02-25 13:47:31 +00001486){
1487 struct ExprList_item *pItem;
1488 int i, n;
1489 Vdbe *v;
1490 if( pList==0 ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001491 v = sqlite3GetVdbe(pParse);
drh268380c2004-02-25 13:47:31 +00001492 n = pList->nExpr;
1493 for(pItem=pList->a, i=0; i<n; i++, pItem++){
danielk19774adee202004-05-08 08:23:19 +00001494 sqlite3ExprCode(pParse, pItem->pExpr);
drh268380c2004-02-25 13:47:31 +00001495 }
drhf9b596e2004-05-26 16:54:42 +00001496 return n;
drh268380c2004-02-25 13:47:31 +00001497}
1498
1499/*
drhcce7d172000-05-31 15:34:51 +00001500** Generate code for a boolean expression such that a jump is made
1501** to the label "dest" if the expression is true but execution
1502** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00001503**
1504** If the expression evaluates to NULL (neither true nor false), then
1505** take the jump if the jumpIfNull flag is true.
drhcce7d172000-05-31 15:34:51 +00001506*/
danielk19774adee202004-05-08 08:23:19 +00001507void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001508 Vdbe *v = pParse->pVdbe;
1509 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001510 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001511 switch( pExpr->op ){
1512 case TK_LT: op = OP_Lt; break;
1513 case TK_LE: op = OP_Le; break;
1514 case TK_GT: op = OP_Gt; break;
1515 case TK_GE: op = OP_Ge; break;
1516 case TK_NE: op = OP_Ne; break;
1517 case TK_EQ: op = OP_Eq; break;
drhcce7d172000-05-31 15:34:51 +00001518 case TK_ISNULL: op = OP_IsNull; break;
1519 case TK_NOTNULL: op = OP_NotNull; break;
1520 default: break;
1521 }
1522 switch( pExpr->op ){
1523 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: {
danielk19774adee202004-05-08 08:23:19 +00001545 sqlite3ExprCode(pParse, pExpr->pLeft);
1546 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001547 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001548 break;
1549 }
1550 case TK_ISNULL:
1551 case TK_NOTNULL: {
danielk19774adee202004-05-08 08:23:19 +00001552 sqlite3ExprCode(pParse, pExpr->pLeft);
1553 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001554 break;
1555 }
drhfef52082000-06-06 01:50:43 +00001556 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00001557 /* The expression "x BETWEEN y AND z" is implemented as:
1558 **
1559 ** 1 IF (x < y) GOTO 3
1560 ** 2 IF (x <= z) GOTO <dest>
1561 ** 3 ...
1562 */
drhf5905aa2002-05-26 20:54:33 +00001563 int addr;
drhbe5c89a2004-07-26 00:31:09 +00001564 Expr *pLeft = pExpr->pLeft;
1565 Expr *pRight = pExpr->pList->a[0].pExpr;
1566 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001567 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001568 sqlite3ExprCode(pParse, pRight);
1569 addr = codeCompare(pParse, pLeft, pRight, OP_Lt, 0, !jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00001570
drhbe5c89a2004-07-26 00:31:09 +00001571 pRight = pExpr->pList->a[1].pExpr;
1572 sqlite3ExprCode(pParse, pRight);
1573 codeCompare(pParse, pLeft, pRight, OP_Le, dest, jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00001574
danielk19774adee202004-05-08 08:23:19 +00001575 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
1576 sqlite3VdbeChangeP2(v, addr, sqlite3VdbeCurrentAddr(v));
1577 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00001578 break;
1579 }
drhcce7d172000-05-31 15:34:51 +00001580 default: {
danielk19774adee202004-05-08 08:23:19 +00001581 sqlite3ExprCode(pParse, pExpr);
1582 sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001583 break;
1584 }
1585 }
1586}
1587
1588/*
drh66b89c82000-11-28 20:47:17 +00001589** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00001590** to the label "dest" if the expression is false but execution
1591** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00001592**
1593** If the expression evaluates to NULL (neither true nor false) then
1594** jump if jumpIfNull is true or fall through if jumpIfNull is false.
drhcce7d172000-05-31 15:34:51 +00001595*/
danielk19774adee202004-05-08 08:23:19 +00001596void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001597 Vdbe *v = pParse->pVdbe;
1598 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001599 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001600 switch( pExpr->op ){
1601 case TK_LT: op = OP_Ge; break;
1602 case TK_LE: op = OP_Gt; break;
1603 case TK_GT: op = OP_Le; break;
1604 case TK_GE: op = OP_Lt; break;
1605 case TK_NE: op = OP_Eq; break;
1606 case TK_EQ: op = OP_Ne; break;
drhcce7d172000-05-31 15:34:51 +00001607 case TK_ISNULL: op = OP_NotNull; break;
1608 case TK_NOTNULL: op = OP_IsNull; break;
1609 default: break;
1610 }
1611 switch( pExpr->op ){
1612 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001613 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
1614 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001615 break;
1616 }
1617 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001618 int d2 = sqlite3VdbeMakeLabel(v);
1619 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
1620 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
1621 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001622 break;
1623 }
1624 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001625 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001626 break;
1627 }
1628 case TK_LT:
1629 case TK_LE:
1630 case TK_GT:
1631 case TK_GE:
1632 case TK_NE:
1633 case TK_EQ: {
danielk19774adee202004-05-08 08:23:19 +00001634 sqlite3ExprCode(pParse, pExpr->pLeft);
1635 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001636 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001637 break;
1638 }
drhcce7d172000-05-31 15:34:51 +00001639 case TK_ISNULL:
1640 case TK_NOTNULL: {
danielk19774adee202004-05-08 08:23:19 +00001641 sqlite3ExprCode(pParse, pExpr->pLeft);
1642 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001643 break;
1644 }
drhfef52082000-06-06 01:50:43 +00001645 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00001646 /* The expression is "x BETWEEN y AND z". It is implemented as:
1647 **
1648 ** 1 IF (x >= y) GOTO 3
1649 ** 2 GOTO <dest>
1650 ** 3 IF (x > z) GOTO <dest>
1651 */
drhfef52082000-06-06 01:50:43 +00001652 int addr;
drhbe5c89a2004-07-26 00:31:09 +00001653 Expr *pLeft = pExpr->pLeft;
1654 Expr *pRight = pExpr->pList->a[0].pExpr;
1655 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001656 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001657 sqlite3ExprCode(pParse, pRight);
danielk19774adee202004-05-08 08:23:19 +00001658 addr = sqlite3VdbeCurrentAddr(v);
drhbe5c89a2004-07-26 00:31:09 +00001659 codeCompare(pParse, pLeft, pRight, OP_Ge, addr+3, !jumpIfNull);
1660
danielk19774adee202004-05-08 08:23:19 +00001661 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
1662 sqlite3VdbeAddOp(v, OP_Goto, 0, dest);
drhbe5c89a2004-07-26 00:31:09 +00001663 pRight = pExpr->pList->a[1].pExpr;
1664 sqlite3ExprCode(pParse, pRight);
1665 codeCompare(pParse, pLeft, pRight, OP_Gt, dest, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00001666 break;
1667 }
drhcce7d172000-05-31 15:34:51 +00001668 default: {
danielk19774adee202004-05-08 08:23:19 +00001669 sqlite3ExprCode(pParse, pExpr);
1670 sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001671 break;
1672 }
1673 }
1674}
drh22827922000-06-06 17:27:05 +00001675
1676/*
1677** Do a deep comparison of two expression trees. Return TRUE (non-zero)
1678** if they are identical and return FALSE if they differ in any way.
1679*/
danielk19774adee202004-05-08 08:23:19 +00001680int sqlite3ExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00001681 int i;
1682 if( pA==0 ){
1683 return pB==0;
1684 }else if( pB==0 ){
1685 return 0;
1686 }
1687 if( pA->op!=pB->op ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001688 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
1689 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00001690 if( pA->pList ){
1691 if( pB->pList==0 ) return 0;
1692 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
1693 for(i=0; i<pA->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00001694 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00001695 return 0;
1696 }
1697 }
1698 }else if( pB->pList ){
1699 return 0;
1700 }
1701 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00001702 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drh22827922000-06-06 17:27:05 +00001703 if( pA->token.z ){
1704 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00001705 if( pB->token.n!=pA->token.n ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001706 if( sqlite3StrNICmp(pA->token.z, pB->token.z, pB->token.n)!=0 ) return 0;
drh22827922000-06-06 17:27:05 +00001707 }
1708 return 1;
1709}
1710
1711/*
1712** Add a new element to the pParse->aAgg[] array and return its index.
1713*/
1714static int appendAggInfo(Parse *pParse){
1715 if( (pParse->nAgg & 0x7)==0 ){
1716 int amt = pParse->nAgg + 8;
drh6d4abfb2001-10-22 02:58:08 +00001717 AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0]));
1718 if( aAgg==0 ){
drh22827922000-06-06 17:27:05 +00001719 return -1;
1720 }
drh6d4abfb2001-10-22 02:58:08 +00001721 pParse->aAgg = aAgg;
drh22827922000-06-06 17:27:05 +00001722 }
1723 memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0]));
1724 return pParse->nAgg++;
1725}
1726
1727/*
1728** Analyze the given expression looking for aggregate functions and
1729** for variables that need to be added to the pParse->aAgg[] array.
1730** Make additional entries to the pParse->aAgg[] array as necessary.
1731**
1732** This routine should only be called after the expression has been
danielk19774adee202004-05-08 08:23:19 +00001733** analyzed by sqlite3ExprResolveIds() and sqlite3ExprCheck().
drh22827922000-06-06 17:27:05 +00001734**
1735** If errors are seen, leave an error message in zErrMsg and return
1736** the number of errors.
1737*/
danielk19774adee202004-05-08 08:23:19 +00001738int sqlite3ExprAnalyzeAggregates(Parse *pParse, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00001739 int i;
1740 AggExpr *aAgg;
1741 int nErr = 0;
1742
1743 if( pExpr==0 ) return 0;
1744 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00001745 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +00001746 aAgg = pParse->aAgg;
1747 for(i=0; i<pParse->nAgg; i++){
1748 if( aAgg[i].isAgg ) continue;
1749 if( aAgg[i].pExpr->iTable==pExpr->iTable
drh967e8b72000-06-21 13:59:10 +00001750 && aAgg[i].pExpr->iColumn==pExpr->iColumn ){
drh22827922000-06-06 17:27:05 +00001751 break;
1752 }
1753 }
1754 if( i>=pParse->nAgg ){
1755 i = appendAggInfo(pParse);
1756 if( i<0 ) return 1;
1757 pParse->aAgg[i].isAgg = 0;
1758 pParse->aAgg[i].pExpr = pExpr;
1759 }
drhaaf88722000-06-08 11:25:00 +00001760 pExpr->iAgg = i;
drh22827922000-06-06 17:27:05 +00001761 break;
1762 }
1763 case TK_AGG_FUNCTION: {
drh22827922000-06-06 17:27:05 +00001764 aAgg = pParse->aAgg;
1765 for(i=0; i<pParse->nAgg; i++){
1766 if( !aAgg[i].isAgg ) continue;
danielk19774adee202004-05-08 08:23:19 +00001767 if( sqlite3ExprCompare(aAgg[i].pExpr, pExpr) ){
drh22827922000-06-06 17:27:05 +00001768 break;
1769 }
1770 }
1771 if( i>=pParse->nAgg ){
danielk1977d8123362004-06-12 09:25:12 +00001772 u8 enc = pParse->db->enc;
drh22827922000-06-06 17:27:05 +00001773 i = appendAggInfo(pParse);
1774 if( i<0 ) return 1;
1775 pParse->aAgg[i].isAgg = 1;
1776 pParse->aAgg[i].pExpr = pExpr;
danielk19774adee202004-05-08 08:23:19 +00001777 pParse->aAgg[i].pFunc = sqlite3FindFunction(pParse->db,
drh6977fea2002-10-22 23:38:04 +00001778 pExpr->token.z, pExpr->token.n,
danielk1977d8123362004-06-12 09:25:12 +00001779 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
drh22827922000-06-06 17:27:05 +00001780 }
1781 pExpr->iAgg = i;
1782 break;
1783 }
1784 default: {
1785 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001786 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pLeft);
drh22827922000-06-06 17:27:05 +00001787 }
1788 if( nErr==0 && pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001789 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pRight);
drh22827922000-06-06 17:27:05 +00001790 }
1791 if( nErr==0 && pExpr->pList ){
1792 int n = pExpr->pList->nExpr;
1793 int i;
1794 for(i=0; nErr==0 && i<n; i++){
danielk19774adee202004-05-08 08:23:19 +00001795 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pList->a[i].pExpr);
drh22827922000-06-06 17:27:05 +00001796 }
1797 }
1798 break;
1799 }
1800 }
1801 return nErr;
1802}
drh8e0a2f92002-02-23 23:45:45 +00001803
1804/*
danielk1977d02eb1f2004-06-06 09:44:03 +00001805** Locate a user function given a name, a number of arguments and a flag
1806** indicating whether the function prefers UTF-16 over UTF-8. Return a
1807** pointer to the FuncDef structure that defines that function, or return
1808** NULL if the function does not exist.
drh8e0a2f92002-02-23 23:45:45 +00001809**
drh0bce8352002-02-28 00:41:10 +00001810** If the createFlag argument is true, then a new (blank) FuncDef
drh8e0a2f92002-02-23 23:45:45 +00001811** structure is created and liked into the "db" structure if a
1812** no matching function previously existed. When createFlag is true
1813** and the nArg parameter is -1, then only a function that accepts
1814** any number of arguments will be returned.
1815**
1816** If createFlag is false and nArg is -1, then the first valid
1817** function found is returned. A function is valid if either xFunc
1818** or xStep is non-zero.
danielk1977d02eb1f2004-06-06 09:44:03 +00001819**
1820** If createFlag is false, then a function with the required name and
1821** number of arguments may be returned even if the eTextRep flag does not
1822** match that requested.
drh8e0a2f92002-02-23 23:45:45 +00001823*/
danielk19774adee202004-05-08 08:23:19 +00001824FuncDef *sqlite3FindFunction(
drh9bb575f2004-09-06 17:24:11 +00001825 sqlite3 *db, /* An open database */
drh8e0a2f92002-02-23 23:45:45 +00001826 const char *zName, /* Name of the function. Not null-terminated */
1827 int nName, /* Number of characters in the name */
1828 int nArg, /* Number of arguments. -1 means any number */
danielk1977d8123362004-06-12 09:25:12 +00001829 u8 enc, /* Preferred text encoding */
drh8e0a2f92002-02-23 23:45:45 +00001830 int createFlag /* Create new entry if true and does not otherwise exist */
1831){
danielk1977d02eb1f2004-06-06 09:44:03 +00001832 FuncDef *p; /* Iterator variable */
1833 FuncDef *pFirst; /* First function with this name */
1834 FuncDef *pBest = 0; /* Best match found so far */
danielk1977d8123362004-06-12 09:25:12 +00001835 int bestmatch = 0;
danielk1977d02eb1f2004-06-06 09:44:03 +00001836
danielk1977d8123362004-06-12 09:25:12 +00001837
1838 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
danielk1977d02eb1f2004-06-06 09:44:03 +00001839 if( nArg<-1 ) nArg = -1;
1840
1841 pFirst = (FuncDef*)sqlite3HashFind(&db->aFunc, zName, nName);
1842 for(p=pFirst; p; p=p->pNext){
danielk1977d8123362004-06-12 09:25:12 +00001843 /* During the search for the best function definition, bestmatch is set
1844 ** as follows to indicate the quality of the match with the definition
1845 ** pointed to by pBest:
1846 **
1847 ** 0: pBest is NULL. No match has been found.
1848 ** 1: A variable arguments function that prefers UTF-8 when a UTF-16
1849 ** encoding is requested, or vice versa.
1850 ** 2: A variable arguments function that uses UTF-16BE when UTF-16LE is
1851 ** requested, or vice versa.
1852 ** 3: A variable arguments function using the same text encoding.
1853 ** 4: A function with the exact number of arguments requested that
1854 ** prefers UTF-8 when a UTF-16 encoding is requested, or vice versa.
1855 ** 5: A function with the exact number of arguments requested that
1856 ** prefers UTF-16LE when UTF-16BE is requested, or vice versa.
1857 ** 6: An exact match.
1858 **
1859 ** A larger value of 'matchqual' indicates a more desirable match.
1860 */
danielk1977e12c17b2004-06-23 12:35:14 +00001861 if( p->nArg==-1 || p->nArg==nArg || nArg==-1 ){
danielk1977d8123362004-06-12 09:25:12 +00001862 int match = 1; /* Quality of this match */
1863 if( p->nArg==nArg || nArg==-1 ){
1864 match = 4;
danielk1977d02eb1f2004-06-06 09:44:03 +00001865 }
danielk1977d8123362004-06-12 09:25:12 +00001866 if( enc==p->iPrefEnc ){
1867 match += 2;
danielk1977d02eb1f2004-06-06 09:44:03 +00001868 }
danielk1977d8123362004-06-12 09:25:12 +00001869 else if( (enc==SQLITE_UTF16LE && p->iPrefEnc==SQLITE_UTF16BE) ||
1870 (enc==SQLITE_UTF16BE && p->iPrefEnc==SQLITE_UTF16LE) ){
1871 match += 1;
1872 }
1873
1874 if( match>bestmatch ){
1875 pBest = p;
1876 bestmatch = match;
danielk1977d02eb1f2004-06-06 09:44:03 +00001877 }
1878 }
drh8e0a2f92002-02-23 23:45:45 +00001879 }
danielk1977d02eb1f2004-06-06 09:44:03 +00001880
danielk1977d8123362004-06-12 09:25:12 +00001881 /* If the createFlag parameter is true, and the seach did not reveal an
1882 ** exact match for the name, number of arguments and encoding, then add a
1883 ** new entry to the hash table and return it.
1884 */
1885 if( createFlag && bestmatch<6 &&
danielk1977d02eb1f2004-06-06 09:44:03 +00001886 (pBest = sqliteMalloc(sizeof(*pBest)+nName+1)) ){
1887 pBest->nArg = nArg;
1888 pBest->pNext = pFirst;
1889 pBest->zName = (char*)&pBest[1];
danielk1977d8123362004-06-12 09:25:12 +00001890 pBest->iPrefEnc = enc;
danielk1977d02eb1f2004-06-06 09:44:03 +00001891 memcpy(pBest->zName, zName, nName);
1892 pBest->zName[nName] = 0;
1893 sqlite3HashInsert(&db->aFunc, pBest->zName, nName, (void*)pBest);
drh8e0a2f92002-02-23 23:45:45 +00001894 }
danielk1977d02eb1f2004-06-06 09:44:03 +00001895
1896 if( pBest && (pBest->xStep || pBest->xFunc || createFlag) ){
1897 return pBest;
drh8e0a2f92002-02-23 23:45:45 +00001898 }
danielk1977d02eb1f2004-06-06 09:44:03 +00001899 return 0;
drh8e0a2f92002-02-23 23:45:45 +00001900}