blob: 415cc3a1f7b2e8a5a1f15e41d23799442dd7298a [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**
drhfcb78a42003-01-18 20:11:05 +000015** $Id: expr.c,v 1.86 2003/01/18 20:11:07 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
20/*
drha76b5df2002-02-23 02:32:10 +000021** Construct a new expression node and return a pointer to it. Memory
22** for this node is obtained from sqliteMalloc(). The calling function
23** is responsible for making sure the node eventually gets freed.
24*/
25Expr *sqliteExpr(int op, Expr *pLeft, Expr *pRight, Token *pToken){
26 Expr *pNew;
27 pNew = sqliteMalloc( sizeof(Expr) );
28 if( pNew==0 ){
29 sqliteExprDelete(pLeft);
30 sqliteExprDelete(pRight);
31 return 0;
32 }
33 pNew->op = op;
34 pNew->pLeft = pLeft;
35 pNew->pRight = pRight;
36 if( pToken ){
drh4b59ab52002-08-24 18:24:51 +000037 assert( pToken->dyn==0 );
drha76b5df2002-02-23 02:32:10 +000038 pNew->token = *pToken;
drh6977fea2002-10-22 23:38:04 +000039 pNew->span = *pToken;
drha76b5df2002-02-23 02:32:10 +000040 }else{
drh4b59ab52002-08-24 18:24:51 +000041 pNew->token.dyn = 0;
drha76b5df2002-02-23 02:32:10 +000042 pNew->token.z = 0;
43 pNew->token.n = 0;
drh6977fea2002-10-22 23:38:04 +000044 if( pLeft && pRight ){
45 sqliteExprSpan(pNew, &pLeft->span, &pRight->span);
46 }else{
47 pNew->span = pNew->token;
48 }
drha76b5df2002-02-23 02:32:10 +000049 }
drha76b5df2002-02-23 02:32:10 +000050 return pNew;
51}
52
53/*
drh6977fea2002-10-22 23:38:04 +000054** Set the Expr.span field of the given expression to span all
drha76b5df2002-02-23 02:32:10 +000055** text between the two given tokens.
56*/
57void sqliteExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
drh6977fea2002-10-22 23:38:04 +000058 if( pExpr && pRight && pRight->z && pLeft && pLeft->z ){
drh4b59ab52002-08-24 18:24:51 +000059 if( pLeft->dyn==0 && pRight->dyn==0 ){
drh6977fea2002-10-22 23:38:04 +000060 pExpr->span.z = pLeft->z;
61 pExpr->span.n = pRight->n + Addr(pRight->z) - Addr(pLeft->z);
drh4b59ab52002-08-24 18:24:51 +000062 }else{
drh6977fea2002-10-22 23:38:04 +000063 pExpr->span.z = 0;
64 pExpr->span.n = 0;
65 pExpr->span.dyn = 0;
drh4b59ab52002-08-24 18:24:51 +000066 }
drha76b5df2002-02-23 02:32:10 +000067 }
68}
69
70/*
71** Construct a new expression node for a function with multiple
72** arguments.
73*/
74Expr *sqliteExprFunction(ExprList *pList, Token *pToken){
75 Expr *pNew;
76 pNew = sqliteMalloc( sizeof(Expr) );
77 if( pNew==0 ){
78 sqliteExprListDelete(pList);
79 return 0;
80 }
81 pNew->op = TK_FUNCTION;
82 pNew->pList = pList;
drh4b59ab52002-08-24 18:24:51 +000083 pNew->token.dyn = 0;
drha76b5df2002-02-23 02:32:10 +000084 if( pToken ){
drh4b59ab52002-08-24 18:24:51 +000085 assert( pToken->dyn==0 );
drha76b5df2002-02-23 02:32:10 +000086 pNew->token = *pToken;
87 }else{
88 pNew->token.z = 0;
89 pNew->token.n = 0;
90 }
drh6977fea2002-10-22 23:38:04 +000091 pNew->span = pNew->token;
drha76b5df2002-02-23 02:32:10 +000092 return pNew;
93}
94
95/*
drha2e00042002-01-22 03:13:42 +000096** Recursively delete an expression tree.
97*/
98void sqliteExprDelete(Expr *p){
99 if( p==0 ) return;
drh6977fea2002-10-22 23:38:04 +0000100 if( p->span.dyn && p->span.z ) sqliteFree((char*)p->span.z);
drh4b59ab52002-08-24 18:24:51 +0000101 if( p->token.dyn && p->token.z ) sqliteFree((char*)p->token.z);
drh75148a22002-03-03 03:42:31 +0000102 if( p->pLeft ) sqliteExprDelete(p->pLeft);
103 if( p->pRight ) sqliteExprDelete(p->pRight);
drha2e00042002-01-22 03:13:42 +0000104 if( p->pList ) sqliteExprListDelete(p->pList);
105 if( p->pSelect ) sqliteSelectDelete(p->pSelect);
106 sqliteFree(p);
107}
108
drha76b5df2002-02-23 02:32:10 +0000109
110/*
drhff78bd22002-02-27 01:47:11 +0000111** The following group of routines make deep copies of expressions,
112** expression lists, ID lists, and select statements. The copies can
113** be deleted (by being passed to their respective ...Delete() routines)
114** without effecting the originals.
115**
drhad3cab52002-05-24 02:04:32 +0000116** The expression list, ID, and source lists return by sqliteExprListDup(),
117** sqliteIdListDup(), and sqliteSrcListDup() can not be further expanded
118** by subsequent calls to sqlite*ListAppend() routines.
drhff78bd22002-02-27 01:47:11 +0000119**
drhad3cab52002-05-24 02:04:32 +0000120** Any tables that the SrcList might point to are not duplicated.
drhff78bd22002-02-27 01:47:11 +0000121*/
122Expr *sqliteExprDup(Expr *p){
123 Expr *pNew;
124 if( p==0 ) return 0;
drhfcb78a42003-01-18 20:11:05 +0000125 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000126 if( pNew==0 ) return 0;
drh3b167c72002-06-28 12:18:47 +0000127 memcpy(pNew, p, sizeof(*pNew));
drh6977fea2002-10-22 23:38:04 +0000128 if( p->token.z!=0 ){
drh4b59ab52002-08-24 18:24:51 +0000129 pNew->token.z = sqliteStrDup(p->token.z);
130 pNew->token.dyn = 1;
131 }else{
132 pNew->token.z = 0;
133 pNew->token.n = 0;
134 pNew->token.dyn = 0;
135 }
drh6977fea2002-10-22 23:38:04 +0000136 pNew->span.z = 0;
137 pNew->span.n = 0;
138 pNew->span.dyn = 0;
drhff78bd22002-02-27 01:47:11 +0000139 pNew->pLeft = sqliteExprDup(p->pLeft);
140 pNew->pRight = sqliteExprDup(p->pRight);
141 pNew->pList = sqliteExprListDup(p->pList);
drhff78bd22002-02-27 01:47:11 +0000142 pNew->pSelect = sqliteSelectDup(p->pSelect);
143 return pNew;
144}
drh4b59ab52002-08-24 18:24:51 +0000145void sqliteTokenCopy(Token *pTo, Token *pFrom){
146 if( pTo->dyn ) sqliteFree((char*)pTo->z);
drh4b59ab52002-08-24 18:24:51 +0000147 if( pFrom->z ){
148 pTo->n = pFrom->n;
149 pTo->z = sqliteStrNDup(pFrom->z, pFrom->n);
150 pTo->dyn = 1;
151 }else{
152 pTo->n = 0;
153 pTo->z = 0;
154 pTo->dyn = 0;
155 }
156}
drhff78bd22002-02-27 01:47:11 +0000157ExprList *sqliteExprListDup(ExprList *p){
158 ExprList *pNew;
159 int i;
160 if( p==0 ) return 0;
161 pNew = sqliteMalloc( sizeof(*pNew) );
162 if( pNew==0 ) return 0;
163 pNew->nExpr = p->nExpr;
164 pNew->a = sqliteMalloc( p->nExpr*sizeof(p->a[0]) );
drhe4697f52002-05-23 02:09:03 +0000165 if( pNew->a==0 ) return 0;
drhff78bd22002-02-27 01:47:11 +0000166 for(i=0; i<p->nExpr; i++){
drh4b59ab52002-08-24 18:24:51 +0000167 Expr *pNewExpr, *pOldExpr;
168 pNew->a[i].pExpr = pNewExpr = sqliteExprDup(pOldExpr = p->a[i].pExpr);
drh6977fea2002-10-22 23:38:04 +0000169 if( pOldExpr->span.z!=0 && pNewExpr ){
170 /* Always make a copy of the span for top-level expressions in the
drh4b59ab52002-08-24 18:24:51 +0000171 ** expression list. The logic in SELECT processing that determines
172 ** the names of columns in the result set needs this information */
drh6977fea2002-10-22 23:38:04 +0000173 sqliteTokenCopy(&pNewExpr->span, &pOldExpr->span);
drh4b59ab52002-08-24 18:24:51 +0000174 }
drh1f3e9052002-10-31 00:09:39 +0000175 assert( pNewExpr==0 || pNewExpr->span.z!=0
176 || pOldExpr->span.z==0 || sqlite_malloc_failed );
drhff78bd22002-02-27 01:47:11 +0000177 pNew->a[i].zName = sqliteStrDup(p->a[i].zName);
178 pNew->a[i].sortOrder = p->a[i].sortOrder;
179 pNew->a[i].isAgg = p->a[i].isAgg;
180 pNew->a[i].done = 0;
181 }
182 return pNew;
183}
drhad3cab52002-05-24 02:04:32 +0000184SrcList *sqliteSrcListDup(SrcList *p){
185 SrcList *pNew;
186 int i;
187 if( p==0 ) return 0;
188 pNew = sqliteMalloc( sizeof(*pNew) );
189 if( pNew==0 ) return 0;
190 pNew->nSrc = p->nSrc;
191 pNew->a = sqliteMalloc( p->nSrc*sizeof(p->a[0]) );
danielk19776f349032002-06-11 02:25:40 +0000192 if( pNew->a==0 && p->nSrc != 0 ) return 0;
drhad3cab52002-05-24 02:04:32 +0000193 for(i=0; i<p->nSrc; i++){
194 pNew->a[i].zName = sqliteStrDup(p->a[i].zName);
195 pNew->a[i].zAlias = sqliteStrDup(p->a[i].zAlias);
196 pNew->a[i].jointype = p->a[i].jointype;
197 pNew->a[i].pTab = 0;
198 pNew->a[i].pSelect = sqliteSelectDup(p->a[i].pSelect);
199 pNew->a[i].pOn = sqliteExprDup(p->a[i].pOn);
200 pNew->a[i].pUsing = sqliteIdListDup(p->a[i].pUsing);
201 }
202 return pNew;
203}
drhff78bd22002-02-27 01:47:11 +0000204IdList *sqliteIdListDup(IdList *p){
205 IdList *pNew;
206 int i;
207 if( p==0 ) return 0;
208 pNew = sqliteMalloc( sizeof(*pNew) );
209 if( pNew==0 ) return 0;
210 pNew->nId = p->nId;
211 pNew->a = sqliteMalloc( p->nId*sizeof(p->a[0]) );
drhe4697f52002-05-23 02:09:03 +0000212 if( pNew->a==0 ) return 0;
drhff78bd22002-02-27 01:47:11 +0000213 for(i=0; i<p->nId; i++){
214 pNew->a[i].zName = sqliteStrDup(p->a[i].zName);
drhff78bd22002-02-27 01:47:11 +0000215 pNew->a[i].idx = p->a[i].idx;
drhff78bd22002-02-27 01:47:11 +0000216 }
217 return pNew;
218}
219Select *sqliteSelectDup(Select *p){
220 Select *pNew;
221 if( p==0 ) return 0;
222 pNew = sqliteMalloc( sizeof(*p) );
223 if( pNew==0 ) return 0;
224 pNew->isDistinct = p->isDistinct;
225 pNew->pEList = sqliteExprListDup(p->pEList);
drhad3cab52002-05-24 02:04:32 +0000226 pNew->pSrc = sqliteSrcListDup(p->pSrc);
drhff78bd22002-02-27 01:47:11 +0000227 pNew->pWhere = sqliteExprDup(p->pWhere);
228 pNew->pGroupBy = sqliteExprListDup(p->pGroupBy);
229 pNew->pHaving = sqliteExprDup(p->pHaving);
230 pNew->pOrderBy = sqliteExprListDup(p->pOrderBy);
231 pNew->op = p->op;
232 pNew->pPrior = sqliteSelectDup(p->pPrior);
233 pNew->nLimit = p->nLimit;
234 pNew->nOffset = p->nOffset;
235 pNew->zSelect = 0;
236 return pNew;
237}
238
239
240/*
drha76b5df2002-02-23 02:32:10 +0000241** Add a new element to the end of an expression list. If pList is
242** initially NULL, then create a new expression list.
243*/
244ExprList *sqliteExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){
245 int i;
246 if( pList==0 ){
247 pList = sqliteMalloc( sizeof(ExprList) );
248 if( pList==0 ){
249 sqliteExprDelete(pExpr);
250 return 0;
251 }
252 }
253 if( (pList->nExpr & 7)==0 ){
254 int n = pList->nExpr + 8;
255 struct ExprList_item *a;
256 a = sqliteRealloc(pList->a, n*sizeof(pList->a[0]));
257 if( a==0 ){
258 sqliteExprDelete(pExpr);
259 return pList;
260 }
261 pList->a = a;
262 }
263 if( pExpr || pName ){
264 i = pList->nExpr++;
265 pList->a[i].pExpr = pExpr;
266 pList->a[i].zName = 0;
267 if( pName ){
268 sqliteSetNString(&pList->a[i].zName, pName->z, pName->n, 0);
269 sqliteDequote(pList->a[i].zName);
270 }
271 }
272 return pList;
273}
274
275/*
276** Delete an entire expression list.
277*/
278void sqliteExprListDelete(ExprList *pList){
279 int i;
280 if( pList==0 ) return;
281 for(i=0; i<pList->nExpr; i++){
282 sqliteExprDelete(pList->a[i].pExpr);
283 sqliteFree(pList->a[i].zName);
284 }
285 sqliteFree(pList->a);
286 sqliteFree(pList);
287}
288
289/*
drhfef52082000-06-06 01:50:43 +0000290** Walk an expression tree. Return 1 if the expression is constant
291** and 0 if it involves variables.
drh23989372002-05-21 13:43:04 +0000292**
293** For the purposes of this function, a double-quoted string (ex: "abc")
294** is considered a variable but a single-quoted string (ex: 'abc') is
295** a constant.
drhfef52082000-06-06 01:50:43 +0000296*/
drh92086432002-01-22 14:11:29 +0000297int sqliteExprIsConstant(Expr *p){
drhfef52082000-06-06 01:50:43 +0000298 switch( p->op ){
299 case TK_ID:
drh967e8b72000-06-21 13:59:10 +0000300 case TK_COLUMN:
drhfef52082000-06-06 01:50:43 +0000301 case TK_DOT:
302 return 0;
drh23989372002-05-21 13:43:04 +0000303 case TK_STRING:
304 return p->token.z[0]=='\'';
drh92086432002-01-22 14:11:29 +0000305 case TK_INTEGER:
306 case TK_FLOAT:
drh92086432002-01-22 14:11:29 +0000307 return 1;
drhfef52082000-06-06 01:50:43 +0000308 default: {
drh92086432002-01-22 14:11:29 +0000309 if( p->pLeft && !sqliteExprIsConstant(p->pLeft) ) return 0;
310 if( p->pRight && !sqliteExprIsConstant(p->pRight) ) return 0;
drhfef52082000-06-06 01:50:43 +0000311 if( p->pList ){
312 int i;
313 for(i=0; i<p->pList->nExpr; i++){
drh92086432002-01-22 14:11:29 +0000314 if( !sqliteExprIsConstant(p->pList->a[i].pExpr) ) return 0;
drhfef52082000-06-06 01:50:43 +0000315 }
316 }
drh92086432002-01-22 14:11:29 +0000317 return p->pLeft!=0 || p->pRight!=0 || (p->pList && p->pList->nExpr>0);
drhfef52082000-06-06 01:50:43 +0000318 }
319 }
drh92086432002-01-22 14:11:29 +0000320 return 0;
drhfef52082000-06-06 01:50:43 +0000321}
322
323/*
drhe4de1fe2002-06-02 16:09:01 +0000324** If the given expression codes a constant integer, return 1 and put
325** the value of the integer in *pValue. If the expression is not an
326** integer, return 0 and leave *pValue unchanged.
327*/
328int sqliteExprIsInteger(Expr *p, int *pValue){
329 switch( p->op ){
330 case TK_INTEGER: {
331 *pValue = atoi(p->token.z);
332 return 1;
333 }
334 case TK_STRING: {
drhbd790ee2002-06-02 18:22:06 +0000335 const char *z = p->token.z;
drhe4de1fe2002-06-02 16:09:01 +0000336 int n = p->token.n;
drhbd790ee2002-06-02 18:22:06 +0000337 if( n>0 && z[0]=='-' ){ z++; n--; }
drhe4de1fe2002-06-02 16:09:01 +0000338 while( n>0 && *z && isdigit(*z) ){ z++; n--; }
339 if( n==0 ){
340 *pValue = atoi(p->token.z);
341 return 1;
342 }
343 break;
344 }
drh4b59ab52002-08-24 18:24:51 +0000345 case TK_UPLUS: {
346 return sqliteExprIsInteger(p->pLeft, pValue);
347 }
drhe4de1fe2002-06-02 16:09:01 +0000348 case TK_UMINUS: {
349 int v;
350 if( sqliteExprIsInteger(p->pLeft, &v) ){
351 *pValue = -v;
352 return 1;
353 }
354 break;
355 }
356 default: break;
357 }
358 return 0;
359}
360
361/*
drhc4a3c772001-04-04 11:48:57 +0000362** Return TRUE if the given string is a row-id column name.
363*/
drha9f9d1c2002-06-29 02:20:08 +0000364int sqliteIsRowid(const char *z){
drhc4a3c772001-04-04 11:48:57 +0000365 if( sqliteStrICmp(z, "_ROWID_")==0 ) return 1;
366 if( sqliteStrICmp(z, "ROWID")==0 ) return 1;
367 if( sqliteStrICmp(z, "OID")==0 ) return 1;
368 return 0;
369}
370
371/*
drhcce7d172000-05-31 15:34:51 +0000372** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +0000373** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +0000374** index to the table in the table list and a column offset. The
375** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
376** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +0000377** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +0000378** VDBE cursor number for a cursor that is pointing into the referenced
379** table. The Expr.iColumn value is changed to the index of the column
380** of the referenced table. The Expr.iColumn value for the special
381** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
382** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +0000383**
drhfef52082000-06-06 01:50:43 +0000384** We also check for instances of the IN operator. IN comes in two
385** forms:
386**
387** expr IN (exprlist)
388** and
389** expr IN (SELECT ...)
390**
391** The first form is handled by creating a set holding the list
392** of allowed values. The second form causes the SELECT to generate
393** a temporary table.
394**
395** This routine also looks for scalar SELECTs that are part of an expression.
drh19a775c2000-06-05 18:54:46 +0000396** If it finds any, it generates code to write the value of that select
397** into a memory cell.
drhcce7d172000-05-31 15:34:51 +0000398**
drh967e8b72000-06-21 13:59:10 +0000399** Unknown columns or tables provoke an error. The function returns
drhcce7d172000-05-31 15:34:51 +0000400** the number of errors seen and leaves an error message on pParse->zErrMsg.
401*/
drha2e00042002-01-22 03:13:42 +0000402int sqliteExprResolveIds(
403 Parse *pParse, /* The parser context */
drh832508b2002-03-02 17:04:07 +0000404 int base, /* VDBE cursor number for first entry in pTabList */
drhad3cab52002-05-24 02:04:32 +0000405 SrcList *pTabList, /* List of tables used to resolve column names */
drha2e00042002-01-22 03:13:42 +0000406 ExprList *pEList, /* List of expressions used to resolve "AS" */
407 Expr *pExpr /* The expression to be analyzed. */
408){
drhdaffd0e2001-04-11 14:28:42 +0000409 if( pExpr==0 || pTabList==0 ) return 0;
drhad3cab52002-05-24 02:04:32 +0000410 assert( base+pTabList->nSrc<=pParse->nTab );
drhcce7d172000-05-31 15:34:51 +0000411 switch( pExpr->op ){
drh23989372002-05-21 13:43:04 +0000412 /* Double-quoted strings (ex: "abc") are used as identifiers if
413 ** possible. Otherwise they remain as strings. Single-quoted
414 ** strings (ex: 'abc') are always string literals.
415 */
416 case TK_STRING: {
417 if( pExpr->token.z[0]=='\'' ) break;
418 /* Fall thru into the TK_ID case if this is a double-quoted string */
419 }
drha2e00042002-01-22 03:13:42 +0000420 /* A lone identifier. Try and match it as follows:
421 **
422 ** 1. To the name of a column of one of the tables in pTabList
423 **
424 ** 2. To the right side of an AS keyword in the column list of
425 ** a SELECT statement. (For example, match against 'x' in
426 ** "SELECT a+b AS 'x' FROM t1".)
427 **
428 ** 3. One of the special names "ROWID", "OID", or "_ROWID_".
429 */
drhcce7d172000-05-31 15:34:51 +0000430 case TK_ID: {
drhc4a3c772001-04-04 11:48:57 +0000431 int cnt = 0; /* Number of matches */
432 int i; /* Loop counter */
drha76b5df2002-02-23 02:32:10 +0000433 char *z;
434 assert( pExpr->token.z );
435 z = sqliteStrNDup(pExpr->token.z, pExpr->token.n);
drh2f4392f2002-02-14 21:42:51 +0000436 sqliteDequote(z);
drhdaffd0e2001-04-11 14:28:42 +0000437 if( z==0 ) return 1;
drhad3cab52002-05-24 02:04:32 +0000438 for(i=0; i<pTabList->nSrc; i++){
drhcce7d172000-05-31 15:34:51 +0000439 int j;
440 Table *pTab = pTabList->a[i].pTab;
441 if( pTab==0 ) continue;
drh417be792002-03-03 18:59:40 +0000442 assert( pTab->nCol>0 );
drhcce7d172000-05-31 15:34:51 +0000443 for(j=0; j<pTab->nCol; j++){
drh7020f652000-06-03 18:06:52 +0000444 if( sqliteStrICmp(pTab->aCol[j].zName, z)==0 ){
drhcce7d172000-05-31 15:34:51 +0000445 cnt++;
drh832508b2002-03-02 17:04:07 +0000446 pExpr->iTable = i + base;
drh4a324312001-12-21 14:30:42 +0000447 if( j==pTab->iPKey ){
448 /* Substitute the record number for the INTEGER PRIMARY KEY */
449 pExpr->iColumn = -1;
drhc9b84a12002-06-20 11:36:48 +0000450 pExpr->dataType = SQLITE_SO_NUM;
drh4a324312001-12-21 14:30:42 +0000451 }else{
452 pExpr->iColumn = j;
drhc9b84a12002-06-20 11:36:48 +0000453 pExpr->dataType = pTab->aCol[j].sortOrder & SQLITE_SO_TYPEMASK;
drh4a324312001-12-21 14:30:42 +0000454 }
drha2e00042002-01-22 03:13:42 +0000455 pExpr->op = TK_COLUMN;
drhcce7d172000-05-31 15:34:51 +0000456 }
457 }
458 }
drha2e00042002-01-22 03:13:42 +0000459 if( cnt==0 && pEList!=0 ){
460 int j;
461 for(j=0; j<pEList->nExpr; j++){
462 char *zAs = pEList->a[j].zName;
463 if( zAs!=0 && sqliteStrICmp(zAs, z)==0 ){
464 cnt++;
465 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
466 pExpr->op = TK_AS;
467 pExpr->iColumn = j;
drh75148a22002-03-03 03:42:31 +0000468 pExpr->pLeft = sqliteExprDup(pEList->a[j].pExpr);
drha2e00042002-01-22 03:13:42 +0000469 }
470 }
471 }
drhc4a3c772001-04-04 11:48:57 +0000472 if( cnt==0 && sqliteIsRowid(z) ){
473 pExpr->iColumn = -1;
drh832508b2002-03-02 17:04:07 +0000474 pExpr->iTable = base;
drhad3cab52002-05-24 02:04:32 +0000475 cnt = 1 + (pTabList->nSrc>1);
drha2e00042002-01-22 03:13:42 +0000476 pExpr->op = TK_COLUMN;
drhc9b84a12002-06-20 11:36:48 +0000477 pExpr->dataType = SQLITE_SO_NUM;
drhc4a3c772001-04-04 11:48:57 +0000478 }
drhcce7d172000-05-31 15:34:51 +0000479 sqliteFree(z);
drh23989372002-05-21 13:43:04 +0000480 if( cnt==0 && pExpr->token.z[0]!='"' ){
drh967e8b72000-06-21 13:59:10 +0000481 sqliteSetNString(&pParse->zErrMsg, "no such column: ", -1,
drhcce7d172000-05-31 15:34:51 +0000482 pExpr->token.z, pExpr->token.n, 0);
483 pParse->nErr++;
484 return 1;
485 }else if( cnt>1 ){
drh967e8b72000-06-21 13:59:10 +0000486 sqliteSetNString(&pParse->zErrMsg, "ambiguous column name: ", -1,
drhcce7d172000-05-31 15:34:51 +0000487 pExpr->token.z, pExpr->token.n, 0);
488 pParse->nErr++;
489 return 1;
490 }
drhed6c8672003-01-12 18:02:16 +0000491 if( pExpr->op==TK_COLUMN ){
492 sqliteAuthRead(pParse, pExpr, pTabList, base);
493 }
drhcce7d172000-05-31 15:34:51 +0000494 break;
495 }
496
drh967e8b72000-06-21 13:59:10 +0000497 /* A table name and column name: ID.ID */
drhcce7d172000-05-31 15:34:51 +0000498 case TK_DOT: {
drhbed86902000-06-02 13:27:59 +0000499 int cnt = 0; /* Number of matches */
drhc4a3c772001-04-04 11:48:57 +0000500 int cntTab = 0; /* Number of matching tables */
drhbed86902000-06-02 13:27:59 +0000501 int i; /* Loop counter */
drhcce7d172000-05-31 15:34:51 +0000502 Expr *pLeft, *pRight; /* Left and right subbranches of the expr */
drhcce7d172000-05-31 15:34:51 +0000503 char *zLeft, *zRight; /* Text of an identifier */
504
505 pLeft = pExpr->pLeft;
506 pRight = pExpr->pRight;
drha76b5df2002-02-23 02:32:10 +0000507 assert( pLeft && pLeft->op==TK_ID && pLeft->token.z );
508 assert( pRight && pRight->op==TK_ID && pRight->token.z );
drh6e142f52000-06-08 13:36:40 +0000509 zLeft = sqliteStrNDup(pLeft->token.z, pLeft->token.n);
510 zRight = sqliteStrNDup(pRight->token.z, pRight->token.n);
drhdaffd0e2001-04-11 14:28:42 +0000511 if( zLeft==0 || zRight==0 ){
512 sqliteFree(zLeft);
513 sqliteFree(zRight);
514 return 1;
515 }
drh87c40e82001-07-23 14:33:02 +0000516 sqliteDequote(zLeft);
517 sqliteDequote(zRight);
drhc4a3c772001-04-04 11:48:57 +0000518 pExpr->iTable = -1;
drhad3cab52002-05-24 02:04:32 +0000519 for(i=0; i<pTabList->nSrc; i++){
drhcce7d172000-05-31 15:34:51 +0000520 int j;
521 char *zTab;
522 Table *pTab = pTabList->a[i].pTab;
523 if( pTab==0 ) continue;
drh417be792002-03-03 18:59:40 +0000524 assert( pTab->nCol>0 );
drhcce7d172000-05-31 15:34:51 +0000525 if( pTabList->a[i].zAlias ){
526 zTab = pTabList->a[i].zAlias;
527 }else{
528 zTab = pTab->zName;
529 }
drh094b2bb2002-03-13 18:54:07 +0000530 if( zTab==0 || sqliteStrICmp(zTab, zLeft)!=0 ) continue;
drh832508b2002-03-02 17:04:07 +0000531 if( 0==(cntTab++) ) pExpr->iTable = i + base;
drhcce7d172000-05-31 15:34:51 +0000532 for(j=0; j<pTab->nCol; j++){
drh7020f652000-06-03 18:06:52 +0000533 if( sqliteStrICmp(pTab->aCol[j].zName, zRight)==0 ){
drhcce7d172000-05-31 15:34:51 +0000534 cnt++;
drh832508b2002-03-02 17:04:07 +0000535 pExpr->iTable = i + base;
drh4a324312001-12-21 14:30:42 +0000536 if( j==pTab->iPKey ){
537 /* Substitute the record number for the INTEGER PRIMARY KEY */
538 pExpr->iColumn = -1;
539 }else{
540 pExpr->iColumn = j;
541 }
drhc9b84a12002-06-20 11:36:48 +0000542 pExpr->dataType = pTab->aCol[j].sortOrder & SQLITE_SO_TYPEMASK;
drhcce7d172000-05-31 15:34:51 +0000543 }
544 }
545 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000546
547 /* If we have not already resolved this *.* expression, then maybe
548 * it is a new.* or old.* trigger argument reference */
danielk1977f29ce552002-05-19 23:43:12 +0000549 if( cnt == 0 && pParse->trigStack != 0 ){
550 TriggerStack *pTriggerStack = pParse->trigStack;
danielk1977c3f9bad2002-05-15 08:30:12 +0000551 int t = 0;
danielk1977f29ce552002-05-19 23:43:12 +0000552 if( pTriggerStack->newIdx != -1 && sqliteStrICmp("new", zLeft) == 0 ){
553 pExpr->iTable = pTriggerStack->newIdx;
danielk1977c3f9bad2002-05-15 08:30:12 +0000554 cntTab++;
555 t = 1;
556 }
danielk1977f29ce552002-05-19 23:43:12 +0000557 if( pTriggerStack->oldIdx != -1 && sqliteStrICmp("old", zLeft) == 0 ){
558 pExpr->iTable = pTriggerStack->oldIdx;
danielk1977c3f9bad2002-05-15 08:30:12 +0000559 cntTab++;
560 t = 1;
561 }
562
danielk1977f29ce552002-05-19 23:43:12 +0000563 if( t ){
564 int j;
drhc9b84a12002-06-20 11:36:48 +0000565 Table *pTab = pTriggerStack->pTab;
566 for(j=0; j < pTab->nCol; j++) {
567 if( sqliteStrICmp(pTab->aCol[j].zName, zRight)==0 ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000568 cnt++;
569 pExpr->iColumn = j;
drhc9b84a12002-06-20 11:36:48 +0000570 pExpr->dataType = pTab->aCol[j].sortOrder & SQLITE_SO_TYPEMASK;
danielk1977c3f9bad2002-05-15 08:30:12 +0000571 }
572 }
danielk1977f29ce552002-05-19 23:43:12 +0000573 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000574 }
575
drhc4a3c772001-04-04 11:48:57 +0000576 if( cnt==0 && cntTab==1 && sqliteIsRowid(zRight) ){
577 cnt = 1;
578 pExpr->iColumn = -1;
drhc9b84a12002-06-20 11:36:48 +0000579 pExpr->dataType = SQLITE_SO_NUM;
drhc4a3c772001-04-04 11:48:57 +0000580 }
drhcce7d172000-05-31 15:34:51 +0000581 sqliteFree(zLeft);
582 sqliteFree(zRight);
583 if( cnt==0 ){
drh967e8b72000-06-21 13:59:10 +0000584 sqliteSetNString(&pParse->zErrMsg, "no such column: ", -1,
drhcce7d172000-05-31 15:34:51 +0000585 pLeft->token.z, pLeft->token.n, ".", 1,
586 pRight->token.z, pRight->token.n, 0);
587 pParse->nErr++;
588 return 1;
589 }else if( cnt>1 ){
drh967e8b72000-06-21 13:59:10 +0000590 sqliteSetNString(&pParse->zErrMsg, "ambiguous column name: ", -1,
drhcce7d172000-05-31 15:34:51 +0000591 pLeft->token.z, pLeft->token.n, ".", 1,
592 pRight->token.z, pRight->token.n, 0);
593 pParse->nErr++;
594 return 1;
595 }
596 sqliteExprDelete(pLeft);
597 pExpr->pLeft = 0;
598 sqliteExprDelete(pRight);
599 pExpr->pRight = 0;
drh967e8b72000-06-21 13:59:10 +0000600 pExpr->op = TK_COLUMN;
drhed6c8672003-01-12 18:02:16 +0000601 sqliteAuthRead(pParse, pExpr, pTabList, base);
drhcce7d172000-05-31 15:34:51 +0000602 break;
603 }
604
drhfef52082000-06-06 01:50:43 +0000605 case TK_IN: {
drhd8bc7082000-06-07 23:51:50 +0000606 Vdbe *v = sqliteGetVdbe(pParse);
drhfef52082000-06-06 01:50:43 +0000607 if( v==0 ) return 1;
drh832508b2002-03-02 17:04:07 +0000608 if( sqliteExprResolveIds(pParse, base, pTabList, pEList, pExpr->pLeft) ){
drhcfab11b2000-06-06 03:31:22 +0000609 return 1;
610 }
drhfef52082000-06-06 01:50:43 +0000611 if( pExpr->pSelect ){
612 /* Case 1: expr IN (SELECT ...)
613 **
614 ** Generate code to write the results of the select into a temporary
drh4794b982000-06-06 13:54:14 +0000615 ** table. The cursor number of the temporary table has already
616 ** been put in iTable by sqliteExprResolveInSelect().
drhfef52082000-06-06 01:50:43 +0000617 */
drh832508b2002-03-02 17:04:07 +0000618 pExpr->iTable = pParse->nTab++;
drhc6b52df2002-01-04 03:09:29 +0000619 sqliteVdbeAddOp(v, OP_OpenTemp, pExpr->iTable, 1);
drh832508b2002-03-02 17:04:07 +0000620 sqliteSelect(pParse, pExpr->pSelect, SRT_Set, pExpr->iTable, 0,0,0);
drhfef52082000-06-06 01:50:43 +0000621 }else if( pExpr->pList ){
622 /* Case 2: expr IN (exprlist)
623 **
624 ** Create a set to put the exprlist values in. The Set id is stored
625 ** in iTable.
626 */
627 int i, iSet;
628 for(i=0; i<pExpr->pList->nExpr; i++){
629 Expr *pE2 = pExpr->pList->a[i].pExpr;
drh92086432002-01-22 14:11:29 +0000630 if( !sqliteExprIsConstant(pE2) ){
drhfef52082000-06-06 01:50:43 +0000631 sqliteSetString(&pParse->zErrMsg,
632 "right-hand side of IN operator must be constant", 0);
633 pParse->nErr++;
634 return 1;
635 }
drh4794b982000-06-06 13:54:14 +0000636 if( sqliteExprCheck(pParse, pE2, 0, 0) ){
637 return 1;
638 }
drhfef52082000-06-06 01:50:43 +0000639 }
640 iSet = pExpr->iTable = pParse->nSet++;
641 for(i=0; i<pExpr->pList->nExpr; i++){
642 Expr *pE2 = pExpr->pList->a[i].pExpr;
643 switch( pE2->op ){
644 case TK_FLOAT:
645 case TK_INTEGER:
646 case TK_STRING: {
drh99fcd712001-10-13 01:06:47 +0000647 int addr = sqliteVdbeAddOp(v, OP_SetInsert, iSet, 0);
drha76b5df2002-02-23 02:32:10 +0000648 assert( pE2->token.z );
drhfef52082000-06-06 01:50:43 +0000649 sqliteVdbeChangeP3(v, addr, pE2->token.z, pE2->token.n);
650 sqliteVdbeDequoteP3(v, addr);
651 break;
652 }
653 default: {
654 sqliteExprCode(pParse, pE2);
drh99fcd712001-10-13 01:06:47 +0000655 sqliteVdbeAddOp(v, OP_SetInsert, iSet, 0);
drhfef52082000-06-06 01:50:43 +0000656 break;
657 }
658 }
659 }
660 }
drhcfab11b2000-06-06 03:31:22 +0000661 break;
drhfef52082000-06-06 01:50:43 +0000662 }
663
drh19a775c2000-06-05 18:54:46 +0000664 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +0000665 /* This has to be a scalar SELECT. Generate code to put the
666 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +0000667 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +0000668 */
drh967e8b72000-06-21 13:59:10 +0000669 pExpr->iColumn = pParse->nMem++;
drh832508b2002-03-02 17:04:07 +0000670 if( sqliteSelect(pParse, pExpr->pSelect, SRT_Mem, pExpr->iColumn,0,0,0) ){
drh19a775c2000-06-05 18:54:46 +0000671 return 1;
672 }
673 break;
674 }
675
drhcce7d172000-05-31 15:34:51 +0000676 /* For all else, just recursively walk the tree */
677 default: {
drh4794b982000-06-06 13:54:14 +0000678 if( pExpr->pLeft
drh832508b2002-03-02 17:04:07 +0000679 && sqliteExprResolveIds(pParse, base, pTabList, pEList, pExpr->pLeft) ){
drhcce7d172000-05-31 15:34:51 +0000680 return 1;
681 }
682 if( pExpr->pRight
drh832508b2002-03-02 17:04:07 +0000683 && sqliteExprResolveIds(pParse, base, pTabList, pEList, pExpr->pRight) ){
drhcce7d172000-05-31 15:34:51 +0000684 return 1;
685 }
686 if( pExpr->pList ){
687 int i;
688 ExprList *pList = pExpr->pList;
689 for(i=0; i<pList->nExpr; i++){
drh832508b2002-03-02 17:04:07 +0000690 Expr *pArg = pList->a[i].pExpr;
691 if( sqliteExprResolveIds(pParse, base, pTabList, pEList, pArg) ){
drhcce7d172000-05-31 15:34:51 +0000692 return 1;
693 }
694 }
695 }
696 }
697 }
698 return 0;
699}
700
drhcce7d172000-05-31 15:34:51 +0000701/*
drh4b59ab52002-08-24 18:24:51 +0000702** pExpr is a node that defines a function of some kind. It might
703** be a syntactic function like "count(x)" or it might be a function
704** that implements an operator, like "a LIKE b".
705**
706** This routine makes *pzName point to the name of the function and
707** *pnName hold the number of characters in the function name.
708*/
709static void getFunctionName(Expr *pExpr, const char **pzName, int *pnName){
710 switch( pExpr->op ){
711 case TK_FUNCTION: {
712 *pzName = pExpr->token.z;
drh6977fea2002-10-22 23:38:04 +0000713 *pnName = pExpr->token.n;
drh4b59ab52002-08-24 18:24:51 +0000714 break;
715 }
716 case TK_LIKE: {
717 *pzName = "like";
718 *pnName = 4;
719 break;
720 }
721 case TK_GLOB: {
722 *pzName = "glob";
723 *pnName = 4;
724 break;
725 }
726 default: {
727 *pzName = "can't happen";
728 *pnName = 12;
729 break;
730 }
731 }
732}
733
734/*
drhcce7d172000-05-31 15:34:51 +0000735** Error check the functions in an expression. Make sure all
736** function names are recognized and all functions have the correct
737** number of arguments. Leave an error message in pParse->zErrMsg
738** if anything is amiss. Return the number of errors.
739**
740** if pIsAgg is not null and this expression is an aggregate function
741** (like count(*) or max(value)) then write a 1 into *pIsAgg.
742*/
743int sqliteExprCheck(Parse *pParse, Expr *pExpr, int allowAgg, int *pIsAgg){
744 int nErr = 0;
745 if( pExpr==0 ) return 0;
drhcce7d172000-05-31 15:34:51 +0000746 switch( pExpr->op ){
drh4b59ab52002-08-24 18:24:51 +0000747 case TK_GLOB:
748 case TK_LIKE:
drhcce7d172000-05-31 15:34:51 +0000749 case TK_FUNCTION: {
drhc9b84a12002-06-20 11:36:48 +0000750 int n = pExpr->pList ? pExpr->pList->nExpr : 0; /* Number of arguments */
751 int no_such_func = 0; /* True if no such function exists */
752 int is_type_of = 0; /* True if is the special TypeOf() function */
753 int wrong_num_args = 0; /* True if wrong number of arguments */
754 int is_agg = 0; /* True if is an aggregate function */
drhcce7d172000-05-31 15:34:51 +0000755 int i;
drh4b59ab52002-08-24 18:24:51 +0000756 int nId; /* Number of characters in function name */
757 const char *zId; /* The function name. */
drh0bce8352002-02-28 00:41:10 +0000758 FuncDef *pDef;
759
drh4b59ab52002-08-24 18:24:51 +0000760 getFunctionName(pExpr, &zId, &nId);
761 pDef = sqliteFindFunction(pParse->db, zId, nId, n, 0);
drh0bce8352002-02-28 00:41:10 +0000762 if( pDef==0 ){
drh4b59ab52002-08-24 18:24:51 +0000763 pDef = sqliteFindFunction(pParse->db, zId, nId, -1, 0);
drh0bce8352002-02-28 00:41:10 +0000764 if( pDef==0 ){
drh4b59ab52002-08-24 18:24:51 +0000765 if( n==1 && nId==6 && sqliteStrNICmp(zId, "typeof", 6)==0 ){
drhc9b84a12002-06-20 11:36:48 +0000766 is_type_of = 1;
767 }else {
768 no_such_func = 1;
769 }
drh0bce8352002-02-28 00:41:10 +0000770 }else{
771 wrong_num_args = 1;
drhcce7d172000-05-31 15:34:51 +0000772 }
drh0bce8352002-02-28 00:41:10 +0000773 }else{
774 is_agg = pDef->xFunc==0;
drhcce7d172000-05-31 15:34:51 +0000775 }
drh8e0a2f92002-02-23 23:45:45 +0000776 if( is_agg && !allowAgg ){
777 sqliteSetNString(&pParse->zErrMsg, "misuse of aggregate function ", -1,
drh4b59ab52002-08-24 18:24:51 +0000778 zId, nId, "()", 2, 0);
drh8e0a2f92002-02-23 23:45:45 +0000779 pParse->nErr++;
780 nErr++;
781 is_agg = 0;
782 }else if( no_such_func ){
drh4b59ab52002-08-24 18:24:51 +0000783 sqliteSetNString(&pParse->zErrMsg, "no such function: ", -1, zId,nId,0);
drhcce7d172000-05-31 15:34:51 +0000784 pParse->nErr++;
785 nErr++;
drh8e0a2f92002-02-23 23:45:45 +0000786 }else if( wrong_num_args ){
787 sqliteSetNString(&pParse->zErrMsg,
drh4b59ab52002-08-24 18:24:51 +0000788 "wrong number of arguments to function ", -1, zId, nId, "()", 2, 0);
drh8e0a2f92002-02-23 23:45:45 +0000789 pParse->nErr++;
790 nErr++;
drhcce7d172000-05-31 15:34:51 +0000791 }
drh22827922000-06-06 17:27:05 +0000792 if( is_agg ) pExpr->op = TK_AGG_FUNCTION;
drhcce7d172000-05-31 15:34:51 +0000793 if( is_agg && pIsAgg ) *pIsAgg = 1;
794 for(i=0; nErr==0 && i<n; i++){
drh4cfa7932000-06-08 15:10:46 +0000795 nErr = sqliteExprCheck(pParse, pExpr->pList->a[i].pExpr,
796 allowAgg && !is_agg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000797 }
drhc9b84a12002-06-20 11:36:48 +0000798 if( pDef==0 ){
799 if( is_type_of ){
800 pExpr->op = TK_STRING;
801 if( sqliteExprType(pExpr->pList->a[0].pExpr)==SQLITE_SO_NUM ){
802 pExpr->token.z = "numeric";
803 pExpr->token.n = 7;
804 }else{
805 pExpr->token.z = "text";
806 pExpr->token.n = 4;
807 }
808 }
809 }else if( pDef->dataType>=0 ){
810 if( pDef->dataType<n ){
811 pExpr->dataType =
812 sqliteExprType(pExpr->pList->a[pDef->dataType].pExpr);
813 }else{
814 pExpr->dataType = SQLITE_SO_NUM;
815 }
816 }else if( pDef->dataType==SQLITE_ARGS ){
817 pDef->dataType = SQLITE_SO_TEXT;
818 for(i=0; i<n; i++){
819 if( sqliteExprType(pExpr->pList->a[i].pExpr)==SQLITE_SO_NUM ){
820 pExpr->dataType = SQLITE_SO_NUM;
821 break;
822 }
823 }
824 }else if( pDef->dataType==SQLITE_NUMERIC ){
825 pExpr->dataType = SQLITE_SO_NUM;
826 }else{
827 pExpr->dataType = SQLITE_SO_TEXT;
828 }
drhcce7d172000-05-31 15:34:51 +0000829 }
830 default: {
831 if( pExpr->pLeft ){
drh22827922000-06-06 17:27:05 +0000832 nErr = sqliteExprCheck(pParse, pExpr->pLeft, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000833 }
834 if( nErr==0 && pExpr->pRight ){
drh22827922000-06-06 17:27:05 +0000835 nErr = sqliteExprCheck(pParse, pExpr->pRight, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000836 }
drhfef52082000-06-06 01:50:43 +0000837 if( nErr==0 && pExpr->pList ){
838 int n = pExpr->pList->nExpr;
839 int i;
840 for(i=0; nErr==0 && i<n; i++){
drh22827922000-06-06 17:27:05 +0000841 Expr *pE2 = pExpr->pList->a[i].pExpr;
842 nErr = sqliteExprCheck(pParse, pE2, allowAgg, pIsAgg);
drhfef52082000-06-06 01:50:43 +0000843 }
844 }
drhcce7d172000-05-31 15:34:51 +0000845 break;
846 }
847 }
848 return nErr;
849}
850
851/*
drhc9b84a12002-06-20 11:36:48 +0000852** Return either SQLITE_SO_NUM or SQLITE_SO_TEXT to indicate whether the
853** given expression should sort as numeric values or as text.
854**
855** The sqliteExprResolveIds() and sqliteExprCheck() routines must have
856** both been called on the expression before it is passed to this routine.
857*/
858int sqliteExprType(Expr *p){
859 if( p==0 ) return SQLITE_SO_NUM;
860 while( p ) switch( p->op ){
861 case TK_PLUS:
862 case TK_MINUS:
863 case TK_STAR:
864 case TK_SLASH:
865 case TK_AND:
866 case TK_OR:
867 case TK_ISNULL:
868 case TK_NOTNULL:
869 case TK_NOT:
870 case TK_UMINUS:
drh4b59ab52002-08-24 18:24:51 +0000871 case TK_UPLUS:
drhc9b84a12002-06-20 11:36:48 +0000872 case TK_BITAND:
873 case TK_BITOR:
874 case TK_BITNOT:
875 case TK_LSHIFT:
876 case TK_RSHIFT:
877 case TK_REM:
878 case TK_INTEGER:
879 case TK_FLOAT:
880 case TK_IN:
881 case TK_BETWEEN:
drh4b59ab52002-08-24 18:24:51 +0000882 case TK_GLOB:
883 case TK_LIKE:
drhc9b84a12002-06-20 11:36:48 +0000884 return SQLITE_SO_NUM;
885
886 case TK_STRING:
887 case TK_NULL:
888 case TK_CONCAT:
889 return SQLITE_SO_TEXT;
890
891 case TK_LT:
892 case TK_LE:
893 case TK_GT:
894 case TK_GE:
895 case TK_NE:
896 case TK_EQ:
897 if( sqliteExprType(p->pLeft)==SQLITE_SO_NUM ){
898 return SQLITE_SO_NUM;
899 }
900 p = p->pRight;
901 break;
902
903 case TK_AS:
904 p = p->pLeft;
905 break;
906
907 case TK_COLUMN:
908 case TK_FUNCTION:
909 case TK_AGG_FUNCTION:
910 return p->dataType;
911
912 case TK_SELECT:
913 assert( p->pSelect );
914 assert( p->pSelect->pEList );
915 assert( p->pSelect->pEList->nExpr>0 );
916 p = p->pSelect->pEList->a[0].pExpr;
917 break;
918
drhb1363202002-06-26 02:45:03 +0000919 case TK_CASE: {
920 if( p->pRight && sqliteExprType(p->pRight)==SQLITE_SO_NUM ){
921 return SQLITE_SO_NUM;
922 }
923 if( p->pList ){
924 int i;
925 ExprList *pList = p->pList;
926 for(i=1; i<pList->nExpr; i+=2){
927 if( sqliteExprType(pList->a[i].pExpr)==SQLITE_SO_NUM ){
928 return SQLITE_SO_NUM;
929 }
930 }
931 }
932 return SQLITE_SO_TEXT;
933 }
934
drhc9b84a12002-06-20 11:36:48 +0000935 default:
936 assert( p->op==TK_ABORT ); /* Can't Happen */
937 break;
938 }
939 return SQLITE_SO_NUM;
940}
941
942/*
drhcce7d172000-05-31 15:34:51 +0000943** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +0000944** expression and leave the result on the top of stack.
drhcce7d172000-05-31 15:34:51 +0000945*/
946void sqliteExprCode(Parse *pParse, Expr *pExpr){
947 Vdbe *v = pParse->pVdbe;
948 int op;
drhdaffd0e2001-04-11 14:28:42 +0000949 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +0000950 switch( pExpr->op ){
951 case TK_PLUS: op = OP_Add; break;
952 case TK_MINUS: op = OP_Subtract; break;
953 case TK_STAR: op = OP_Multiply; break;
954 case TK_SLASH: op = OP_Divide; break;
955 case TK_AND: op = OP_And; break;
956 case TK_OR: op = OP_Or; break;
957 case TK_LT: op = OP_Lt; break;
958 case TK_LE: op = OP_Le; break;
959 case TK_GT: op = OP_Gt; break;
960 case TK_GE: op = OP_Ge; break;
961 case TK_NE: op = OP_Ne; break;
962 case TK_EQ: op = OP_Eq; break;
drhcce7d172000-05-31 15:34:51 +0000963 case TK_ISNULL: op = OP_IsNull; break;
964 case TK_NOTNULL: op = OP_NotNull; break;
965 case TK_NOT: op = OP_Not; break;
966 case TK_UMINUS: op = OP_Negative; break;
drhbf4133c2001-10-13 02:59:08 +0000967 case TK_BITAND: op = OP_BitAnd; break;
968 case TK_BITOR: op = OP_BitOr; break;
969 case TK_BITNOT: op = OP_BitNot; break;
970 case TK_LSHIFT: op = OP_ShiftLeft; break;
971 case TK_RSHIFT: op = OP_ShiftRight; break;
972 case TK_REM: op = OP_Remainder; break;
drhcce7d172000-05-31 15:34:51 +0000973 default: break;
974 }
975 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +0000976 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +0000977 if( pParse->useAgg ){
drh99fcd712001-10-13 01:06:47 +0000978 sqliteVdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drhc4a3c772001-04-04 11:48:57 +0000979 }else if( pExpr->iColumn>=0 ){
drh99fcd712001-10-13 01:06:47 +0000980 sqliteVdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn);
drhc4a3c772001-04-04 11:48:57 +0000981 }else{
drh99fcd712001-10-13 01:06:47 +0000982 sqliteVdbeAddOp(v, OP_Recno, pExpr->iTable, 0);
drh22827922000-06-06 17:27:05 +0000983 }
drhcce7d172000-05-31 15:34:51 +0000984 break;
985 }
986 case TK_INTEGER: {
drhd9e30932002-06-09 01:16:01 +0000987 int iVal = atoi(pExpr->token.z);
988 char zBuf[30];
989 sprintf(zBuf,"%d",iVal);
990 if( strlen(zBuf)!=pExpr->token.n
991 || strncmp(pExpr->token.z,zBuf,pExpr->token.n)!=0 ){
992 /* If the integer value cannot be represented exactly in 32 bits,
993 ** then code it as a string instead. */
994 sqliteVdbeAddOp(v, OP_String, 0, 0);
995 }else{
996 sqliteVdbeAddOp(v, OP_Integer, iVal, 0);
997 }
drhe6840902002-03-06 03:08:25 +0000998 sqliteVdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
999 break;
1000 }
1001 case TK_FLOAT: {
drh7a7c7392001-11-24 00:31:46 +00001002 sqliteVdbeAddOp(v, OP_String, 0, 0);
drha76b5df2002-02-23 02:32:10 +00001003 assert( pExpr->token.z );
drh7a7c7392001-11-24 00:31:46 +00001004 sqliteVdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
drhcce7d172000-05-31 15:34:51 +00001005 break;
1006 }
drhcce7d172000-05-31 15:34:51 +00001007 case TK_STRING: {
drh99fcd712001-10-13 01:06:47 +00001008 int addr = sqliteVdbeAddOp(v, OP_String, 0, 0);
drha76b5df2002-02-23 02:32:10 +00001009 assert( pExpr->token.z );
drhcce7d172000-05-31 15:34:51 +00001010 sqliteVdbeChangeP3(v, addr, pExpr->token.z, pExpr->token.n);
1011 sqliteVdbeDequoteP3(v, addr);
1012 break;
1013 }
1014 case TK_NULL: {
drh99fcd712001-10-13 01:06:47 +00001015 sqliteVdbeAddOp(v, OP_String, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001016 break;
1017 }
drhc9b84a12002-06-20 11:36:48 +00001018 case TK_LT:
1019 case TK_LE:
1020 case TK_GT:
1021 case TK_GE:
1022 case TK_NE:
1023 case TK_EQ: {
drh491791a2002-07-18 00:34:09 +00001024 if( pParse->db->file_format>=4 && sqliteExprType(pExpr)==SQLITE_SO_TEXT ){
drhc9b84a12002-06-20 11:36:48 +00001025 op += 6; /* Convert numeric opcodes to text opcodes */
1026 }
1027 /* Fall through into the next case */
1028 }
drhcce7d172000-05-31 15:34:51 +00001029 case TK_AND:
1030 case TK_OR:
1031 case TK_PLUS:
1032 case TK_STAR:
1033 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00001034 case TK_REM:
1035 case TK_BITAND:
1036 case TK_BITOR:
drhc9b84a12002-06-20 11:36:48 +00001037 case TK_SLASH: {
drhcce7d172000-05-31 15:34:51 +00001038 sqliteExprCode(pParse, pExpr->pLeft);
1039 sqliteExprCode(pParse, pExpr->pRight);
drh99fcd712001-10-13 01:06:47 +00001040 sqliteVdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001041 break;
1042 }
drhbf4133c2001-10-13 02:59:08 +00001043 case TK_LSHIFT:
1044 case TK_RSHIFT: {
1045 sqliteExprCode(pParse, pExpr->pRight);
1046 sqliteExprCode(pParse, pExpr->pLeft);
1047 sqliteVdbeAddOp(v, op, 0, 0);
1048 break;
1049 }
drh00400772000-06-16 20:51:26 +00001050 case TK_CONCAT: {
1051 sqliteExprCode(pParse, pExpr->pLeft);
1052 sqliteExprCode(pParse, pExpr->pRight);
drh99fcd712001-10-13 01:06:47 +00001053 sqliteVdbeAddOp(v, OP_Concat, 2, 0);
drh00400772000-06-16 20:51:26 +00001054 break;
1055 }
drh4b59ab52002-08-24 18:24:51 +00001056 case TK_UPLUS: {
1057 Expr *pLeft = pExpr->pLeft;
1058 if( pLeft && pLeft->op==TK_INTEGER ){
1059 sqliteVdbeAddOp(v, OP_Integer, atoi(pLeft->token.z), 0);
1060 sqliteVdbeChangeP3(v, -1, pLeft->token.z, pLeft->token.n);
1061 }else if( pLeft && pLeft->op==TK_FLOAT ){
1062 sqliteVdbeAddOp(v, OP_String, 0, 0);
1063 sqliteVdbeChangeP3(v, -1, pLeft->token.z, pLeft->token.n);
1064 }else{
1065 sqliteExprCode(pParse, pExpr->pLeft);
1066 }
1067 break;
1068 }
drhcce7d172000-05-31 15:34:51 +00001069 case TK_UMINUS: {
drh6e142f52000-06-08 13:36:40 +00001070 assert( pExpr->pLeft );
drh7a7c7392001-11-24 00:31:46 +00001071 if( pExpr->pLeft->op==TK_FLOAT || pExpr->pLeft->op==TK_INTEGER ){
drh6e142f52000-06-08 13:36:40 +00001072 Token *p = &pExpr->pLeft->token;
1073 char *z = sqliteMalloc( p->n + 2 );
1074 sprintf(z, "-%.*s", p->n, p->z);
drhe6840902002-03-06 03:08:25 +00001075 if( pExpr->pLeft->op==TK_INTEGER ){
1076 sqliteVdbeAddOp(v, OP_Integer, atoi(z), 0);
1077 }else{
1078 sqliteVdbeAddOp(v, OP_String, 0, 0);
1079 }
drh99fcd712001-10-13 01:06:47 +00001080 sqliteVdbeChangeP3(v, -1, z, p->n+1);
drh6e142f52000-06-08 13:36:40 +00001081 sqliteFree(z);
1082 break;
1083 }
drh1ccde152000-06-17 13:12:39 +00001084 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +00001085 }
drhbf4133c2001-10-13 02:59:08 +00001086 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00001087 case TK_NOT: {
drhcce7d172000-05-31 15:34:51 +00001088 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +00001089 sqliteVdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001090 break;
1091 }
1092 case TK_ISNULL:
1093 case TK_NOTNULL: {
1094 int dest;
drh99fcd712001-10-13 01:06:47 +00001095 sqliteVdbeAddOp(v, OP_Integer, 1, 0);
drhcce7d172000-05-31 15:34:51 +00001096 sqliteExprCode(pParse, pExpr->pLeft);
1097 dest = sqliteVdbeCurrentAddr(v) + 2;
drhf5905aa2002-05-26 20:54:33 +00001098 sqliteVdbeAddOp(v, op, 1, dest);
drh99fcd712001-10-13 01:06:47 +00001099 sqliteVdbeAddOp(v, OP_AddImm, -1, 0);
drhcce7d172000-05-31 15:34:51 +00001100 break;
1101 }
drh22827922000-06-06 17:27:05 +00001102 case TK_AGG_FUNCTION: {
drh99fcd712001-10-13 01:06:47 +00001103 sqliteVdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drh22827922000-06-06 17:27:05 +00001104 break;
1105 }
drh4b59ab52002-08-24 18:24:51 +00001106 case TK_GLOB:
1107 case TK_LIKE:
drhcce7d172000-05-31 15:34:51 +00001108 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00001109 int i;
1110 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00001111 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00001112 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00001113 int nId;
1114 const char *zId;
1115 getFunctionName(pExpr, &zId, &nId);
1116 pDef = sqliteFindFunction(pParse->db, zId, nId, nExpr, 0);
drh0bce8352002-02-28 00:41:10 +00001117 assert( pDef!=0 );
drh89425d52002-02-28 03:04:48 +00001118 for(i=0; i<nExpr; i++){
drh0bce8352002-02-28 00:41:10 +00001119 sqliteExprCode(pParse, pList->a[i].pExpr);
drhcce7d172000-05-31 15:34:51 +00001120 }
drh89425d52002-02-28 03:04:48 +00001121 sqliteVdbeAddOp(v, OP_Function, nExpr, 0);
drh0bce8352002-02-28 00:41:10 +00001122 sqliteVdbeChangeP3(v, -1, (char*)pDef, P3_POINTER);
drhcce7d172000-05-31 15:34:51 +00001123 break;
1124 }
drh19a775c2000-06-05 18:54:46 +00001125 case TK_SELECT: {
drh99fcd712001-10-13 01:06:47 +00001126 sqliteVdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drh19a775c2000-06-05 18:54:46 +00001127 break;
1128 }
drhfef52082000-06-06 01:50:43 +00001129 case TK_IN: {
1130 int addr;
drh99fcd712001-10-13 01:06:47 +00001131 sqliteVdbeAddOp(v, OP_Integer, 1, 0);
drhfef52082000-06-06 01:50:43 +00001132 sqliteExprCode(pParse, pExpr->pLeft);
1133 addr = sqliteVdbeCurrentAddr(v);
drhf5905aa2002-05-26 20:54:33 +00001134 sqliteVdbeAddOp(v, OP_NotNull, -1, addr+4);
1135 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1136 sqliteVdbeAddOp(v, OP_String, 0, 0);
1137 sqliteVdbeAddOp(v, OP_Goto, 0, addr+6);
drhfef52082000-06-06 01:50:43 +00001138 if( pExpr->pSelect ){
drhf5905aa2002-05-26 20:54:33 +00001139 sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, addr+6);
drhfef52082000-06-06 01:50:43 +00001140 }else{
drhf5905aa2002-05-26 20:54:33 +00001141 sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, addr+6);
drhfef52082000-06-06 01:50:43 +00001142 }
drh99fcd712001-10-13 01:06:47 +00001143 sqliteVdbeAddOp(v, OP_AddImm, -1, 0);
drhfef52082000-06-06 01:50:43 +00001144 break;
1145 }
1146 case TK_BETWEEN: {
drhf5905aa2002-05-26 20:54:33 +00001147 sqliteExprCode(pParse, pExpr->pLeft);
1148 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
1149 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
1150 sqliteVdbeAddOp(v, OP_Ge, 0, 0);
1151 sqliteVdbeAddOp(v, OP_Pull, 1, 0);
1152 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
1153 sqliteVdbeAddOp(v, OP_Le, 0, 0);
1154 sqliteVdbeAddOp(v, OP_And, 0, 0);
drhfef52082000-06-06 01:50:43 +00001155 break;
1156 }
drha2e00042002-01-22 03:13:42 +00001157 case TK_AS: {
1158 sqliteExprCode(pParse, pExpr->pLeft);
1159 break;
1160 }
drh17a7f8d2002-03-24 13:13:27 +00001161 case TK_CASE: {
1162 int expr_end_label;
drhf5905aa2002-05-26 20:54:33 +00001163 int jumpInst;
1164 int addr;
1165 int nExpr;
drh17a7f8d2002-03-24 13:13:27 +00001166 int i;
1167
1168 assert(pExpr->pList);
1169 assert((pExpr->pList->nExpr % 2) == 0);
1170 assert(pExpr->pList->nExpr > 0);
drhf5905aa2002-05-26 20:54:33 +00001171 nExpr = pExpr->pList->nExpr;
1172 expr_end_label = sqliteVdbeMakeLabel(v);
drh17a7f8d2002-03-24 13:13:27 +00001173 if( pExpr->pLeft ){
1174 sqliteExprCode(pParse, pExpr->pLeft);
1175 }
drhf5905aa2002-05-26 20:54:33 +00001176 for(i=0; i<nExpr; i=i+2){
1177 sqliteExprCode(pParse, pExpr->pList->a[i].pExpr);
drh17a7f8d2002-03-24 13:13:27 +00001178 if( pExpr->pLeft ){
drhf5905aa2002-05-26 20:54:33 +00001179 sqliteVdbeAddOp(v, OP_Dup, 1, 1);
drhf570f012002-05-31 15:51:25 +00001180 jumpInst = sqliteVdbeAddOp(v, OP_Ne, 1, 0);
1181 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001182 }else{
drhf570f012002-05-31 15:51:25 +00001183 jumpInst = sqliteVdbeAddOp(v, OP_IfNot, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001184 }
1185 sqliteExprCode(pParse, pExpr->pList->a[i+1].pExpr);
drhf5905aa2002-05-26 20:54:33 +00001186 sqliteVdbeAddOp(v, OP_Goto, 0, expr_end_label);
drhf5905aa2002-05-26 20:54:33 +00001187 addr = sqliteVdbeCurrentAddr(v);
1188 sqliteVdbeChangeP2(v, jumpInst, addr);
drh17a7f8d2002-03-24 13:13:27 +00001189 }
drhf570f012002-05-31 15:51:25 +00001190 if( pExpr->pLeft ){
1191 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1192 }
drh17a7f8d2002-03-24 13:13:27 +00001193 if( pExpr->pRight ){
1194 sqliteExprCode(pParse, pExpr->pRight);
1195 }else{
drhf5905aa2002-05-26 20:54:33 +00001196 sqliteVdbeAddOp(v, OP_String, 0, 0);
drh17a7f8d2002-03-24 13:13:27 +00001197 }
drhf5905aa2002-05-26 20:54:33 +00001198 sqliteVdbeResolveLabel(v, expr_end_label);
danielk19776f349032002-06-11 02:25:40 +00001199 break;
1200 }
1201 case TK_RAISE: {
1202 if( !pParse->trigStack ){
1203 sqliteSetNString(&pParse->zErrMsg,
1204 "RAISE() may only be used within a trigger-program", -1, 0);
1205 pParse->nErr++;
1206 return;
1207 }
1208 if( pExpr->iColumn == OE_Rollback ||
1209 pExpr->iColumn == OE_Abort ||
1210 pExpr->iColumn == OE_Fail ){
1211 char * msg = sqliteStrNDup(pExpr->token.z, pExpr->token.n);
1212 sqliteVdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn);
1213 sqliteDequote(msg);
1214 sqliteVdbeChangeP3(v, -1, msg, 0);
1215 sqliteFree(msg);
1216 } else {
1217 assert( pExpr->iColumn == OE_Ignore );
1218 sqliteVdbeAddOp(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
1219 sqliteVdbeChangeP3(v, -1, "(IGNORE jump)", -1);
1220 }
drh17a7f8d2002-03-24 13:13:27 +00001221 }
1222 break;
drhcce7d172000-05-31 15:34:51 +00001223 }
drhcce7d172000-05-31 15:34:51 +00001224}
1225
1226/*
1227** Generate code for a boolean expression such that a jump is made
1228** to the label "dest" if the expression is true but execution
1229** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00001230**
1231** If the expression evaluates to NULL (neither true nor false), then
1232** take the jump if the jumpIfNull flag is true.
drhcce7d172000-05-31 15:34:51 +00001233*/
drhf5905aa2002-05-26 20:54:33 +00001234void sqliteExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001235 Vdbe *v = pParse->pVdbe;
1236 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001237 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001238 switch( pExpr->op ){
1239 case TK_LT: op = OP_Lt; break;
1240 case TK_LE: op = OP_Le; break;
1241 case TK_GT: op = OP_Gt; break;
1242 case TK_GE: op = OP_Ge; break;
1243 case TK_NE: op = OP_Ne; break;
1244 case TK_EQ: op = OP_Eq; break;
drhcce7d172000-05-31 15:34:51 +00001245 case TK_ISNULL: op = OP_IsNull; break;
1246 case TK_NOTNULL: op = OP_NotNull; break;
1247 default: break;
1248 }
1249 switch( pExpr->op ){
1250 case TK_AND: {
1251 int d2 = sqliteVdbeMakeLabel(v);
drhf5905aa2002-05-26 20:54:33 +00001252 sqliteExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
1253 sqliteExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001254 sqliteVdbeResolveLabel(v, d2);
1255 break;
1256 }
1257 case TK_OR: {
drhf5905aa2002-05-26 20:54:33 +00001258 sqliteExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
1259 sqliteExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001260 break;
1261 }
1262 case TK_NOT: {
drhf5905aa2002-05-26 20:54:33 +00001263 sqliteExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001264 break;
1265 }
1266 case TK_LT:
1267 case TK_LE:
1268 case TK_GT:
1269 case TK_GE:
1270 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00001271 case TK_EQ: {
drhcce7d172000-05-31 15:34:51 +00001272 sqliteExprCode(pParse, pExpr->pLeft);
1273 sqliteExprCode(pParse, pExpr->pRight);
drh491791a2002-07-18 00:34:09 +00001274 if( pParse->db->file_format>=4 && sqliteExprType(pExpr)==SQLITE_SO_TEXT ){
drhc9b84a12002-06-20 11:36:48 +00001275 op += 6; /* Convert numeric opcodes to text opcodes */
1276 }
drhf5905aa2002-05-26 20:54:33 +00001277 sqliteVdbeAddOp(v, op, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001278 break;
1279 }
1280 case TK_ISNULL:
1281 case TK_NOTNULL: {
1282 sqliteExprCode(pParse, pExpr->pLeft);
drhf5905aa2002-05-26 20:54:33 +00001283 sqliteVdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001284 break;
1285 }
drhfef52082000-06-06 01:50:43 +00001286 case TK_IN: {
drhf5905aa2002-05-26 20:54:33 +00001287 int addr;
drhcfab11b2000-06-06 03:31:22 +00001288 sqliteExprCode(pParse, pExpr->pLeft);
drhf5905aa2002-05-26 20:54:33 +00001289 addr = sqliteVdbeCurrentAddr(v);
1290 sqliteVdbeAddOp(v, OP_NotNull, -1, addr+3);
1291 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1292 sqliteVdbeAddOp(v, OP_Goto, 0, jumpIfNull ? dest : addr+4);
drhfef52082000-06-06 01:50:43 +00001293 if( pExpr->pSelect ){
drh99fcd712001-10-13 01:06:47 +00001294 sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001295 }else{
drh99fcd712001-10-13 01:06:47 +00001296 sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001297 }
1298 break;
1299 }
1300 case TK_BETWEEN: {
drhf5905aa2002-05-26 20:54:33 +00001301 int addr;
drhfef52082000-06-06 01:50:43 +00001302 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +00001303 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
drhfef52082000-06-06 01:50:43 +00001304 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
drhf5905aa2002-05-26 20:54:33 +00001305 addr = sqliteVdbeAddOp(v, OP_Lt, !jumpIfNull, 0);
drhfef52082000-06-06 01:50:43 +00001306 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
drhf5905aa2002-05-26 20:54:33 +00001307 sqliteVdbeAddOp(v, OP_Le, jumpIfNull, dest);
drh99fcd712001-10-13 01:06:47 +00001308 sqliteVdbeAddOp(v, OP_Integer, 0, 0);
drhf5905aa2002-05-26 20:54:33 +00001309 sqliteVdbeChangeP2(v, addr, sqliteVdbeCurrentAddr(v));
drh99fcd712001-10-13 01:06:47 +00001310 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00001311 break;
1312 }
drhcce7d172000-05-31 15:34:51 +00001313 default: {
1314 sqliteExprCode(pParse, pExpr);
drhf5905aa2002-05-26 20:54:33 +00001315 sqliteVdbeAddOp(v, OP_If, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001316 break;
1317 }
1318 }
1319}
1320
1321/*
drh66b89c82000-11-28 20:47:17 +00001322** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00001323** to the label "dest" if the expression is false but execution
1324** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00001325**
1326** If the expression evaluates to NULL (neither true nor false) then
1327** jump if jumpIfNull is true or fall through if jumpIfNull is false.
drhcce7d172000-05-31 15:34:51 +00001328*/
drhf5905aa2002-05-26 20:54:33 +00001329void sqliteExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001330 Vdbe *v = pParse->pVdbe;
1331 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001332 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001333 switch( pExpr->op ){
1334 case TK_LT: op = OP_Ge; break;
1335 case TK_LE: op = OP_Gt; break;
1336 case TK_GT: op = OP_Le; break;
1337 case TK_GE: op = OP_Lt; break;
1338 case TK_NE: op = OP_Eq; break;
1339 case TK_EQ: op = OP_Ne; break;
drhcce7d172000-05-31 15:34:51 +00001340 case TK_ISNULL: op = OP_NotNull; break;
1341 case TK_NOTNULL: op = OP_IsNull; break;
1342 default: break;
1343 }
1344 switch( pExpr->op ){
1345 case TK_AND: {
drhf5905aa2002-05-26 20:54:33 +00001346 sqliteExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
1347 sqliteExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001348 break;
1349 }
1350 case TK_OR: {
1351 int d2 = sqliteVdbeMakeLabel(v);
drhf5905aa2002-05-26 20:54:33 +00001352 sqliteExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
1353 sqliteExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001354 sqliteVdbeResolveLabel(v, d2);
1355 break;
1356 }
1357 case TK_NOT: {
drhf5905aa2002-05-26 20:54:33 +00001358 sqliteExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001359 break;
1360 }
1361 case TK_LT:
1362 case TK_LE:
1363 case TK_GT:
1364 case TK_GE:
1365 case TK_NE:
1366 case TK_EQ: {
drh491791a2002-07-18 00:34:09 +00001367 if( pParse->db->file_format>=4 && sqliteExprType(pExpr)==SQLITE_SO_TEXT ){
drh8f619cc2002-09-08 00:04:50 +00001368 /* Convert numeric comparison opcodes into text comparison opcodes.
1369 ** This step depends on the fact that the text comparision opcodes are
1370 ** always 6 greater than their corresponding numeric comparison
1371 ** opcodes.
1372 */
1373 assert( OP_Eq+6 == OP_StrEq );
1374 op += 6;
drhc9b84a12002-06-20 11:36:48 +00001375 }
drhcce7d172000-05-31 15:34:51 +00001376 sqliteExprCode(pParse, pExpr->pLeft);
1377 sqliteExprCode(pParse, pExpr->pRight);
drhf5905aa2002-05-26 20:54:33 +00001378 sqliteVdbeAddOp(v, op, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001379 break;
1380 }
drhcce7d172000-05-31 15:34:51 +00001381 case TK_ISNULL:
1382 case TK_NOTNULL: {
1383 sqliteExprCode(pParse, pExpr->pLeft);
drhf5905aa2002-05-26 20:54:33 +00001384 sqliteVdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001385 break;
1386 }
drhfef52082000-06-06 01:50:43 +00001387 case TK_IN: {
drhf5905aa2002-05-26 20:54:33 +00001388 int addr;
drhcfab11b2000-06-06 03:31:22 +00001389 sqliteExprCode(pParse, pExpr->pLeft);
drhf5905aa2002-05-26 20:54:33 +00001390 addr = sqliteVdbeCurrentAddr(v);
1391 sqliteVdbeAddOp(v, OP_NotNull, -1, addr+3);
1392 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1393 sqliteVdbeAddOp(v, OP_Goto, 0, jumpIfNull ? dest : addr+4);
drhfef52082000-06-06 01:50:43 +00001394 if( pExpr->pSelect ){
drh99fcd712001-10-13 01:06:47 +00001395 sqliteVdbeAddOp(v, OP_NotFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001396 }else{
drh99fcd712001-10-13 01:06:47 +00001397 sqliteVdbeAddOp(v, OP_SetNotFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001398 }
1399 break;
1400 }
1401 case TK_BETWEEN: {
1402 int addr;
1403 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +00001404 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
drhfef52082000-06-06 01:50:43 +00001405 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
1406 addr = sqliteVdbeCurrentAddr(v);
drhf5905aa2002-05-26 20:54:33 +00001407 sqliteVdbeAddOp(v, OP_Ge, !jumpIfNull, addr+3);
drh99fcd712001-10-13 01:06:47 +00001408 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1409 sqliteVdbeAddOp(v, OP_Goto, 0, dest);
drhfef52082000-06-06 01:50:43 +00001410 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
drhf5905aa2002-05-26 20:54:33 +00001411 sqliteVdbeAddOp(v, OP_Gt, jumpIfNull, dest);
drhfef52082000-06-06 01:50:43 +00001412 break;
1413 }
drhcce7d172000-05-31 15:34:51 +00001414 default: {
1415 sqliteExprCode(pParse, pExpr);
drh461c2812002-05-30 02:35:11 +00001416 sqliteVdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001417 break;
1418 }
1419 }
1420}
drh22827922000-06-06 17:27:05 +00001421
1422/*
1423** Do a deep comparison of two expression trees. Return TRUE (non-zero)
1424** if they are identical and return FALSE if they differ in any way.
1425*/
drhd8bc7082000-06-07 23:51:50 +00001426int sqliteExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00001427 int i;
1428 if( pA==0 ){
1429 return pB==0;
1430 }else if( pB==0 ){
1431 return 0;
1432 }
1433 if( pA->op!=pB->op ) return 0;
drhd8bc7082000-06-07 23:51:50 +00001434 if( !sqliteExprCompare(pA->pLeft, pB->pLeft) ) return 0;
1435 if( !sqliteExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00001436 if( pA->pList ){
1437 if( pB->pList==0 ) return 0;
1438 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
1439 for(i=0; i<pA->pList->nExpr; i++){
drhd8bc7082000-06-07 23:51:50 +00001440 if( !sqliteExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00001441 return 0;
1442 }
1443 }
1444 }else if( pB->pList ){
1445 return 0;
1446 }
1447 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00001448 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drh22827922000-06-06 17:27:05 +00001449 if( pA->token.z ){
1450 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00001451 if( pB->token.n!=pA->token.n ) return 0;
1452 if( sqliteStrNICmp(pA->token.z, pB->token.z, pB->token.n)!=0 ) return 0;
drh22827922000-06-06 17:27:05 +00001453 }
1454 return 1;
1455}
1456
1457/*
1458** Add a new element to the pParse->aAgg[] array and return its index.
1459*/
1460static int appendAggInfo(Parse *pParse){
1461 if( (pParse->nAgg & 0x7)==0 ){
1462 int amt = pParse->nAgg + 8;
drh6d4abfb2001-10-22 02:58:08 +00001463 AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0]));
1464 if( aAgg==0 ){
drh22827922000-06-06 17:27:05 +00001465 return -1;
1466 }
drh6d4abfb2001-10-22 02:58:08 +00001467 pParse->aAgg = aAgg;
drh22827922000-06-06 17:27:05 +00001468 }
1469 memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0]));
1470 return pParse->nAgg++;
1471}
1472
1473/*
1474** Analyze the given expression looking for aggregate functions and
1475** for variables that need to be added to the pParse->aAgg[] array.
1476** Make additional entries to the pParse->aAgg[] array as necessary.
1477**
1478** This routine should only be called after the expression has been
1479** analyzed by sqliteExprResolveIds() and sqliteExprCheck().
1480**
1481** If errors are seen, leave an error message in zErrMsg and return
1482** the number of errors.
1483*/
1484int sqliteExprAnalyzeAggregates(Parse *pParse, Expr *pExpr){
1485 int i;
1486 AggExpr *aAgg;
1487 int nErr = 0;
1488
1489 if( pExpr==0 ) return 0;
1490 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00001491 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +00001492 aAgg = pParse->aAgg;
1493 for(i=0; i<pParse->nAgg; i++){
1494 if( aAgg[i].isAgg ) continue;
1495 if( aAgg[i].pExpr->iTable==pExpr->iTable
drh967e8b72000-06-21 13:59:10 +00001496 && aAgg[i].pExpr->iColumn==pExpr->iColumn ){
drh22827922000-06-06 17:27:05 +00001497 break;
1498 }
1499 }
1500 if( i>=pParse->nAgg ){
1501 i = appendAggInfo(pParse);
1502 if( i<0 ) return 1;
1503 pParse->aAgg[i].isAgg = 0;
1504 pParse->aAgg[i].pExpr = pExpr;
1505 }
drhaaf88722000-06-08 11:25:00 +00001506 pExpr->iAgg = i;
drh22827922000-06-06 17:27:05 +00001507 break;
1508 }
1509 case TK_AGG_FUNCTION: {
drh22827922000-06-06 17:27:05 +00001510 aAgg = pParse->aAgg;
1511 for(i=0; i<pParse->nAgg; i++){
1512 if( !aAgg[i].isAgg ) continue;
drhd8bc7082000-06-07 23:51:50 +00001513 if( sqliteExprCompare(aAgg[i].pExpr, pExpr) ){
drh22827922000-06-06 17:27:05 +00001514 break;
1515 }
1516 }
1517 if( i>=pParse->nAgg ){
1518 i = appendAggInfo(pParse);
1519 if( i<0 ) return 1;
1520 pParse->aAgg[i].isAgg = 1;
1521 pParse->aAgg[i].pExpr = pExpr;
drh0bce8352002-02-28 00:41:10 +00001522 pParse->aAgg[i].pFunc = sqliteFindFunction(pParse->db,
drh6977fea2002-10-22 23:38:04 +00001523 pExpr->token.z, pExpr->token.n,
drhf55f25f2002-02-28 01:46:11 +00001524 pExpr->pList ? pExpr->pList->nExpr : 0, 0);
drh22827922000-06-06 17:27:05 +00001525 }
1526 pExpr->iAgg = i;
1527 break;
1528 }
1529 default: {
1530 if( pExpr->pLeft ){
1531 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pLeft);
1532 }
1533 if( nErr==0 && pExpr->pRight ){
1534 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pRight);
1535 }
1536 if( nErr==0 && pExpr->pList ){
1537 int n = pExpr->pList->nExpr;
1538 int i;
1539 for(i=0; nErr==0 && i<n; i++){
1540 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pList->a[i].pExpr);
1541 }
1542 }
1543 break;
1544 }
1545 }
1546 return nErr;
1547}
drh8e0a2f92002-02-23 23:45:45 +00001548
1549/*
1550** Locate a user function given a name and a number of arguments.
drh0bce8352002-02-28 00:41:10 +00001551** Return a pointer to the FuncDef structure that defines that
drh8e0a2f92002-02-23 23:45:45 +00001552** function, or return NULL if the function does not exist.
1553**
drh0bce8352002-02-28 00:41:10 +00001554** If the createFlag argument is true, then a new (blank) FuncDef
drh8e0a2f92002-02-23 23:45:45 +00001555** structure is created and liked into the "db" structure if a
1556** no matching function previously existed. When createFlag is true
1557** and the nArg parameter is -1, then only a function that accepts
1558** any number of arguments will be returned.
1559**
1560** If createFlag is false and nArg is -1, then the first valid
1561** function found is returned. A function is valid if either xFunc
1562** or xStep is non-zero.
1563*/
drh0bce8352002-02-28 00:41:10 +00001564FuncDef *sqliteFindFunction(
drh8e0a2f92002-02-23 23:45:45 +00001565 sqlite *db, /* An open database */
1566 const char *zName, /* Name of the function. Not null-terminated */
1567 int nName, /* Number of characters in the name */
1568 int nArg, /* Number of arguments. -1 means any number */
1569 int createFlag /* Create new entry if true and does not otherwise exist */
1570){
drh0bce8352002-02-28 00:41:10 +00001571 FuncDef *pFirst, *p, *pMaybe;
1572 pFirst = p = (FuncDef*)sqliteHashFind(&db->aFunc, zName, nName);
drh1350b032002-02-27 19:00:20 +00001573 if( p && !createFlag && nArg<0 ){
drh8e0a2f92002-02-23 23:45:45 +00001574 while( p && p->xFunc==0 && p->xStep==0 ){ p = p->pNext; }
1575 return p;
1576 }
1577 pMaybe = 0;
1578 while( p && p->nArg!=nArg ){
1579 if( p->nArg<0 && !createFlag && (p->xFunc || p->xStep) ) pMaybe = p;
1580 p = p->pNext;
1581 }
1582 if( p && !createFlag && p->xFunc==0 && p->xStep==0 ){
1583 return 0;
1584 }
1585 if( p==0 && pMaybe ){
1586 assert( createFlag==0 );
1587 return pMaybe;
1588 }
drh89425d52002-02-28 03:04:48 +00001589 if( p==0 && createFlag && (p = sqliteMalloc(sizeof(*p)))!=0 ){
drh8e0a2f92002-02-23 23:45:45 +00001590 p->nArg = nArg;
1591 p->pNext = pFirst;
drhc9b84a12002-06-20 11:36:48 +00001592 p->dataType = pFirst ? pFirst->dataType : SQLITE_NUMERIC;
drh0bce8352002-02-28 00:41:10 +00001593 sqliteHashInsert(&db->aFunc, zName, nName, (void*)p);
drh8e0a2f92002-02-23 23:45:45 +00001594 }
1595 return p;
1596}