blob: b2ec92e2e0a855b714e406d4c2e468edd7e493b2 [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**
drh6977fea2002-10-22 23:38:04 +000015** $Id: expr.c,v 1.82 2002/10/22 23:38:04 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;
125 pNew = sqliteMalloc( sizeof(*p) );
126 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 }
drh6977fea2002-10-22 23:38:04 +0000175 assert( pNewExpr==0 || pNewExpr->span.z!=0 || pOldExpr->span.z==0 );
drhff78bd22002-02-27 01:47:11 +0000176 pNew->a[i].zName = sqliteStrDup(p->a[i].zName);
177 pNew->a[i].sortOrder = p->a[i].sortOrder;
178 pNew->a[i].isAgg = p->a[i].isAgg;
179 pNew->a[i].done = 0;
180 }
181 return pNew;
182}
drhad3cab52002-05-24 02:04:32 +0000183SrcList *sqliteSrcListDup(SrcList *p){
184 SrcList *pNew;
185 int i;
186 if( p==0 ) return 0;
187 pNew = sqliteMalloc( sizeof(*pNew) );
188 if( pNew==0 ) return 0;
189 pNew->nSrc = p->nSrc;
190 pNew->a = sqliteMalloc( p->nSrc*sizeof(p->a[0]) );
danielk19776f349032002-06-11 02:25:40 +0000191 if( pNew->a==0 && p->nSrc != 0 ) return 0;
drhad3cab52002-05-24 02:04:32 +0000192 for(i=0; i<p->nSrc; i++){
193 pNew->a[i].zName = sqliteStrDup(p->a[i].zName);
194 pNew->a[i].zAlias = sqliteStrDup(p->a[i].zAlias);
195 pNew->a[i].jointype = p->a[i].jointype;
196 pNew->a[i].pTab = 0;
197 pNew->a[i].pSelect = sqliteSelectDup(p->a[i].pSelect);
198 pNew->a[i].pOn = sqliteExprDup(p->a[i].pOn);
199 pNew->a[i].pUsing = sqliteIdListDup(p->a[i].pUsing);
200 }
201 return pNew;
202}
drhff78bd22002-02-27 01:47:11 +0000203IdList *sqliteIdListDup(IdList *p){
204 IdList *pNew;
205 int i;
206 if( p==0 ) return 0;
207 pNew = sqliteMalloc( sizeof(*pNew) );
208 if( pNew==0 ) return 0;
209 pNew->nId = p->nId;
210 pNew->a = sqliteMalloc( p->nId*sizeof(p->a[0]) );
drhe4697f52002-05-23 02:09:03 +0000211 if( pNew->a==0 ) return 0;
drhff78bd22002-02-27 01:47:11 +0000212 for(i=0; i<p->nId; i++){
213 pNew->a[i].zName = sqliteStrDup(p->a[i].zName);
drhff78bd22002-02-27 01:47:11 +0000214 pNew->a[i].idx = p->a[i].idx;
drhff78bd22002-02-27 01:47:11 +0000215 }
216 return pNew;
217}
218Select *sqliteSelectDup(Select *p){
219 Select *pNew;
220 if( p==0 ) return 0;
221 pNew = sqliteMalloc( sizeof(*p) );
222 if( pNew==0 ) return 0;
223 pNew->isDistinct = p->isDistinct;
224 pNew->pEList = sqliteExprListDup(p->pEList);
drhad3cab52002-05-24 02:04:32 +0000225 pNew->pSrc = sqliteSrcListDup(p->pSrc);
drhff78bd22002-02-27 01:47:11 +0000226 pNew->pWhere = sqliteExprDup(p->pWhere);
227 pNew->pGroupBy = sqliteExprListDup(p->pGroupBy);
228 pNew->pHaving = sqliteExprDup(p->pHaving);
229 pNew->pOrderBy = sqliteExprListDup(p->pOrderBy);
230 pNew->op = p->op;
231 pNew->pPrior = sqliteSelectDup(p->pPrior);
232 pNew->nLimit = p->nLimit;
233 pNew->nOffset = p->nOffset;
234 pNew->zSelect = 0;
235 return pNew;
236}
237
238
239/*
drha76b5df2002-02-23 02:32:10 +0000240** Add a new element to the end of an expression list. If pList is
241** initially NULL, then create a new expression list.
242*/
243ExprList *sqliteExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){
244 int i;
245 if( pList==0 ){
246 pList = sqliteMalloc( sizeof(ExprList) );
247 if( pList==0 ){
248 sqliteExprDelete(pExpr);
249 return 0;
250 }
251 }
252 if( (pList->nExpr & 7)==0 ){
253 int n = pList->nExpr + 8;
254 struct ExprList_item *a;
255 a = sqliteRealloc(pList->a, n*sizeof(pList->a[0]));
256 if( a==0 ){
257 sqliteExprDelete(pExpr);
258 return pList;
259 }
260 pList->a = a;
261 }
262 if( pExpr || pName ){
263 i = pList->nExpr++;
264 pList->a[i].pExpr = pExpr;
265 pList->a[i].zName = 0;
266 if( pName ){
267 sqliteSetNString(&pList->a[i].zName, pName->z, pName->n, 0);
268 sqliteDequote(pList->a[i].zName);
269 }
270 }
271 return pList;
272}
273
274/*
275** Delete an entire expression list.
276*/
277void sqliteExprListDelete(ExprList *pList){
278 int i;
279 if( pList==0 ) return;
280 for(i=0; i<pList->nExpr; i++){
281 sqliteExprDelete(pList->a[i].pExpr);
282 sqliteFree(pList->a[i].zName);
283 }
284 sqliteFree(pList->a);
285 sqliteFree(pList);
286}
287
288/*
drhfef52082000-06-06 01:50:43 +0000289** Walk an expression tree. Return 1 if the expression is constant
290** and 0 if it involves variables.
drh23989372002-05-21 13:43:04 +0000291**
292** For the purposes of this function, a double-quoted string (ex: "abc")
293** is considered a variable but a single-quoted string (ex: 'abc') is
294** a constant.
drhfef52082000-06-06 01:50:43 +0000295*/
drh92086432002-01-22 14:11:29 +0000296int sqliteExprIsConstant(Expr *p){
drhfef52082000-06-06 01:50:43 +0000297 switch( p->op ){
298 case TK_ID:
drh967e8b72000-06-21 13:59:10 +0000299 case TK_COLUMN:
drhfef52082000-06-06 01:50:43 +0000300 case TK_DOT:
301 return 0;
drh23989372002-05-21 13:43:04 +0000302 case TK_STRING:
303 return p->token.z[0]=='\'';
drh92086432002-01-22 14:11:29 +0000304 case TK_INTEGER:
305 case TK_FLOAT:
drh92086432002-01-22 14:11:29 +0000306 return 1;
drhfef52082000-06-06 01:50:43 +0000307 default: {
drh92086432002-01-22 14:11:29 +0000308 if( p->pLeft && !sqliteExprIsConstant(p->pLeft) ) return 0;
309 if( p->pRight && !sqliteExprIsConstant(p->pRight) ) return 0;
drhfef52082000-06-06 01:50:43 +0000310 if( p->pList ){
311 int i;
312 for(i=0; i<p->pList->nExpr; i++){
drh92086432002-01-22 14:11:29 +0000313 if( !sqliteExprIsConstant(p->pList->a[i].pExpr) ) return 0;
drhfef52082000-06-06 01:50:43 +0000314 }
315 }
drh92086432002-01-22 14:11:29 +0000316 return p->pLeft!=0 || p->pRight!=0 || (p->pList && p->pList->nExpr>0);
drhfef52082000-06-06 01:50:43 +0000317 }
318 }
drh92086432002-01-22 14:11:29 +0000319 return 0;
drhfef52082000-06-06 01:50:43 +0000320}
321
322/*
drhe4de1fe2002-06-02 16:09:01 +0000323** If the given expression codes a constant integer, return 1 and put
324** the value of the integer in *pValue. If the expression is not an
325** integer, return 0 and leave *pValue unchanged.
326*/
327int sqliteExprIsInteger(Expr *p, int *pValue){
328 switch( p->op ){
329 case TK_INTEGER: {
330 *pValue = atoi(p->token.z);
331 return 1;
332 }
333 case TK_STRING: {
drhbd790ee2002-06-02 18:22:06 +0000334 const char *z = p->token.z;
drhe4de1fe2002-06-02 16:09:01 +0000335 int n = p->token.n;
drhbd790ee2002-06-02 18:22:06 +0000336 if( n>0 && z[0]=='-' ){ z++; n--; }
drhe4de1fe2002-06-02 16:09:01 +0000337 while( n>0 && *z && isdigit(*z) ){ z++; n--; }
338 if( n==0 ){
339 *pValue = atoi(p->token.z);
340 return 1;
341 }
342 break;
343 }
drh4b59ab52002-08-24 18:24:51 +0000344 case TK_UPLUS: {
345 return sqliteExprIsInteger(p->pLeft, pValue);
346 }
drhe4de1fe2002-06-02 16:09:01 +0000347 case TK_UMINUS: {
348 int v;
349 if( sqliteExprIsInteger(p->pLeft, &v) ){
350 *pValue = -v;
351 return 1;
352 }
353 break;
354 }
355 default: break;
356 }
357 return 0;
358}
359
360/*
drhc4a3c772001-04-04 11:48:57 +0000361** Return TRUE if the given string is a row-id column name.
362*/
drha9f9d1c2002-06-29 02:20:08 +0000363int sqliteIsRowid(const char *z){
drhc4a3c772001-04-04 11:48:57 +0000364 if( sqliteStrICmp(z, "_ROWID_")==0 ) return 1;
365 if( sqliteStrICmp(z, "ROWID")==0 ) return 1;
366 if( sqliteStrICmp(z, "OID")==0 ) return 1;
367 return 0;
368}
369
370/*
drhcce7d172000-05-31 15:34:51 +0000371** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +0000372** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +0000373** index to the table in the table list and a column offset. The
374** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
375** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +0000376** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +0000377** VDBE cursor number for a cursor that is pointing into the referenced
378** table. The Expr.iColumn value is changed to the index of the column
379** of the referenced table. The Expr.iColumn value for the special
380** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
381** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +0000382**
drhfef52082000-06-06 01:50:43 +0000383** We also check for instances of the IN operator. IN comes in two
384** forms:
385**
386** expr IN (exprlist)
387** and
388** expr IN (SELECT ...)
389**
390** The first form is handled by creating a set holding the list
391** of allowed values. The second form causes the SELECT to generate
392** a temporary table.
393**
394** This routine also looks for scalar SELECTs that are part of an expression.
drh19a775c2000-06-05 18:54:46 +0000395** If it finds any, it generates code to write the value of that select
396** into a memory cell.
drhcce7d172000-05-31 15:34:51 +0000397**
drh967e8b72000-06-21 13:59:10 +0000398** Unknown columns or tables provoke an error. The function returns
drhcce7d172000-05-31 15:34:51 +0000399** the number of errors seen and leaves an error message on pParse->zErrMsg.
400*/
drha2e00042002-01-22 03:13:42 +0000401int sqliteExprResolveIds(
402 Parse *pParse, /* The parser context */
drh832508b2002-03-02 17:04:07 +0000403 int base, /* VDBE cursor number for first entry in pTabList */
drhad3cab52002-05-24 02:04:32 +0000404 SrcList *pTabList, /* List of tables used to resolve column names */
drha2e00042002-01-22 03:13:42 +0000405 ExprList *pEList, /* List of expressions used to resolve "AS" */
406 Expr *pExpr /* The expression to be analyzed. */
407){
drhdaffd0e2001-04-11 14:28:42 +0000408 if( pExpr==0 || pTabList==0 ) return 0;
drhad3cab52002-05-24 02:04:32 +0000409 assert( base+pTabList->nSrc<=pParse->nTab );
drhcce7d172000-05-31 15:34:51 +0000410 switch( pExpr->op ){
drh23989372002-05-21 13:43:04 +0000411 /* Double-quoted strings (ex: "abc") are used as identifiers if
412 ** possible. Otherwise they remain as strings. Single-quoted
413 ** strings (ex: 'abc') are always string literals.
414 */
415 case TK_STRING: {
416 if( pExpr->token.z[0]=='\'' ) break;
417 /* Fall thru into the TK_ID case if this is a double-quoted string */
418 }
drha2e00042002-01-22 03:13:42 +0000419 /* A lone identifier. Try and match it as follows:
420 **
421 ** 1. To the name of a column of one of the tables in pTabList
422 **
423 ** 2. To the right side of an AS keyword in the column list of
424 ** a SELECT statement. (For example, match against 'x' in
425 ** "SELECT a+b AS 'x' FROM t1".)
426 **
427 ** 3. One of the special names "ROWID", "OID", or "_ROWID_".
428 */
drhcce7d172000-05-31 15:34:51 +0000429 case TK_ID: {
drhc4a3c772001-04-04 11:48:57 +0000430 int cnt = 0; /* Number of matches */
431 int i; /* Loop counter */
drha76b5df2002-02-23 02:32:10 +0000432 char *z;
433 assert( pExpr->token.z );
434 z = sqliteStrNDup(pExpr->token.z, pExpr->token.n);
drh2f4392f2002-02-14 21:42:51 +0000435 sqliteDequote(z);
drhdaffd0e2001-04-11 14:28:42 +0000436 if( z==0 ) return 1;
drhad3cab52002-05-24 02:04:32 +0000437 for(i=0; i<pTabList->nSrc; i++){
drhcce7d172000-05-31 15:34:51 +0000438 int j;
439 Table *pTab = pTabList->a[i].pTab;
440 if( pTab==0 ) continue;
drh417be792002-03-03 18:59:40 +0000441 assert( pTab->nCol>0 );
drhcce7d172000-05-31 15:34:51 +0000442 for(j=0; j<pTab->nCol; j++){
drh7020f652000-06-03 18:06:52 +0000443 if( sqliteStrICmp(pTab->aCol[j].zName, z)==0 ){
drhcce7d172000-05-31 15:34:51 +0000444 cnt++;
drh832508b2002-03-02 17:04:07 +0000445 pExpr->iTable = i + base;
drh4a324312001-12-21 14:30:42 +0000446 if( j==pTab->iPKey ){
447 /* Substitute the record number for the INTEGER PRIMARY KEY */
448 pExpr->iColumn = -1;
drhc9b84a12002-06-20 11:36:48 +0000449 pExpr->dataType = SQLITE_SO_NUM;
drh4a324312001-12-21 14:30:42 +0000450 }else{
451 pExpr->iColumn = j;
drhc9b84a12002-06-20 11:36:48 +0000452 pExpr->dataType = pTab->aCol[j].sortOrder & SQLITE_SO_TYPEMASK;
drh4a324312001-12-21 14:30:42 +0000453 }
drha2e00042002-01-22 03:13:42 +0000454 pExpr->op = TK_COLUMN;
drhcce7d172000-05-31 15:34:51 +0000455 }
456 }
457 }
drha2e00042002-01-22 03:13:42 +0000458 if( cnt==0 && pEList!=0 ){
459 int j;
460 for(j=0; j<pEList->nExpr; j++){
461 char *zAs = pEList->a[j].zName;
462 if( zAs!=0 && sqliteStrICmp(zAs, z)==0 ){
463 cnt++;
464 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
465 pExpr->op = TK_AS;
466 pExpr->iColumn = j;
drh75148a22002-03-03 03:42:31 +0000467 pExpr->pLeft = sqliteExprDup(pEList->a[j].pExpr);
drha2e00042002-01-22 03:13:42 +0000468 }
469 }
470 }
drhc4a3c772001-04-04 11:48:57 +0000471 if( cnt==0 && sqliteIsRowid(z) ){
472 pExpr->iColumn = -1;
drh832508b2002-03-02 17:04:07 +0000473 pExpr->iTable = base;
drhad3cab52002-05-24 02:04:32 +0000474 cnt = 1 + (pTabList->nSrc>1);
drha2e00042002-01-22 03:13:42 +0000475 pExpr->op = TK_COLUMN;
drhc9b84a12002-06-20 11:36:48 +0000476 pExpr->dataType = SQLITE_SO_NUM;
drhc4a3c772001-04-04 11:48:57 +0000477 }
drhcce7d172000-05-31 15:34:51 +0000478 sqliteFree(z);
drh23989372002-05-21 13:43:04 +0000479 if( cnt==0 && pExpr->token.z[0]!='"' ){
drh967e8b72000-06-21 13:59:10 +0000480 sqliteSetNString(&pParse->zErrMsg, "no such column: ", -1,
drhcce7d172000-05-31 15:34:51 +0000481 pExpr->token.z, pExpr->token.n, 0);
482 pParse->nErr++;
483 return 1;
484 }else if( cnt>1 ){
drh967e8b72000-06-21 13:59:10 +0000485 sqliteSetNString(&pParse->zErrMsg, "ambiguous column name: ", -1,
drhcce7d172000-05-31 15:34:51 +0000486 pExpr->token.z, pExpr->token.n, 0);
487 pParse->nErr++;
488 return 1;
489 }
drhcce7d172000-05-31 15:34:51 +0000490 break;
491 }
492
drh967e8b72000-06-21 13:59:10 +0000493 /* A table name and column name: ID.ID */
drhcce7d172000-05-31 15:34:51 +0000494 case TK_DOT: {
drhbed86902000-06-02 13:27:59 +0000495 int cnt = 0; /* Number of matches */
drhc4a3c772001-04-04 11:48:57 +0000496 int cntTab = 0; /* Number of matching tables */
drhbed86902000-06-02 13:27:59 +0000497 int i; /* Loop counter */
drhcce7d172000-05-31 15:34:51 +0000498 Expr *pLeft, *pRight; /* Left and right subbranches of the expr */
drhcce7d172000-05-31 15:34:51 +0000499 char *zLeft, *zRight; /* Text of an identifier */
500
501 pLeft = pExpr->pLeft;
502 pRight = pExpr->pRight;
drha76b5df2002-02-23 02:32:10 +0000503 assert( pLeft && pLeft->op==TK_ID && pLeft->token.z );
504 assert( pRight && pRight->op==TK_ID && pRight->token.z );
drh6e142f52000-06-08 13:36:40 +0000505 zLeft = sqliteStrNDup(pLeft->token.z, pLeft->token.n);
506 zRight = sqliteStrNDup(pRight->token.z, pRight->token.n);
drhdaffd0e2001-04-11 14:28:42 +0000507 if( zLeft==0 || zRight==0 ){
508 sqliteFree(zLeft);
509 sqliteFree(zRight);
510 return 1;
511 }
drh87c40e82001-07-23 14:33:02 +0000512 sqliteDequote(zLeft);
513 sqliteDequote(zRight);
drhc4a3c772001-04-04 11:48:57 +0000514 pExpr->iTable = -1;
drhad3cab52002-05-24 02:04:32 +0000515 for(i=0; i<pTabList->nSrc; i++){
drhcce7d172000-05-31 15:34:51 +0000516 int j;
517 char *zTab;
518 Table *pTab = pTabList->a[i].pTab;
519 if( pTab==0 ) continue;
drh417be792002-03-03 18:59:40 +0000520 assert( pTab->nCol>0 );
drhcce7d172000-05-31 15:34:51 +0000521 if( pTabList->a[i].zAlias ){
522 zTab = pTabList->a[i].zAlias;
523 }else{
524 zTab = pTab->zName;
525 }
drh094b2bb2002-03-13 18:54:07 +0000526 if( zTab==0 || sqliteStrICmp(zTab, zLeft)!=0 ) continue;
drh832508b2002-03-02 17:04:07 +0000527 if( 0==(cntTab++) ) pExpr->iTable = i + base;
drhcce7d172000-05-31 15:34:51 +0000528 for(j=0; j<pTab->nCol; j++){
drh7020f652000-06-03 18:06:52 +0000529 if( sqliteStrICmp(pTab->aCol[j].zName, zRight)==0 ){
drhcce7d172000-05-31 15:34:51 +0000530 cnt++;
drh832508b2002-03-02 17:04:07 +0000531 pExpr->iTable = i + base;
drh4a324312001-12-21 14:30:42 +0000532 if( j==pTab->iPKey ){
533 /* Substitute the record number for the INTEGER PRIMARY KEY */
534 pExpr->iColumn = -1;
535 }else{
536 pExpr->iColumn = j;
537 }
drhc9b84a12002-06-20 11:36:48 +0000538 pExpr->dataType = pTab->aCol[j].sortOrder & SQLITE_SO_TYPEMASK;
drhcce7d172000-05-31 15:34:51 +0000539 }
540 }
541 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000542
543 /* If we have not already resolved this *.* expression, then maybe
544 * it is a new.* or old.* trigger argument reference */
danielk1977f29ce552002-05-19 23:43:12 +0000545 if( cnt == 0 && pParse->trigStack != 0 ){
546 TriggerStack *pTriggerStack = pParse->trigStack;
danielk1977c3f9bad2002-05-15 08:30:12 +0000547 int t = 0;
danielk1977f29ce552002-05-19 23:43:12 +0000548 if( pTriggerStack->newIdx != -1 && sqliteStrICmp("new", zLeft) == 0 ){
549 pExpr->iTable = pTriggerStack->newIdx;
danielk1977c3f9bad2002-05-15 08:30:12 +0000550 cntTab++;
551 t = 1;
552 }
danielk1977f29ce552002-05-19 23:43:12 +0000553 if( pTriggerStack->oldIdx != -1 && sqliteStrICmp("old", zLeft) == 0 ){
554 pExpr->iTable = pTriggerStack->oldIdx;
danielk1977c3f9bad2002-05-15 08:30:12 +0000555 cntTab++;
556 t = 1;
557 }
558
danielk1977f29ce552002-05-19 23:43:12 +0000559 if( t ){
560 int j;
drhc9b84a12002-06-20 11:36:48 +0000561 Table *pTab = pTriggerStack->pTab;
562 for(j=0; j < pTab->nCol; j++) {
563 if( sqliteStrICmp(pTab->aCol[j].zName, zRight)==0 ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000564 cnt++;
565 pExpr->iColumn = j;
drhc9b84a12002-06-20 11:36:48 +0000566 pExpr->dataType = pTab->aCol[j].sortOrder & SQLITE_SO_TYPEMASK;
danielk1977c3f9bad2002-05-15 08:30:12 +0000567 }
568 }
danielk1977f29ce552002-05-19 23:43:12 +0000569 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000570 }
571
drhc4a3c772001-04-04 11:48:57 +0000572 if( cnt==0 && cntTab==1 && sqliteIsRowid(zRight) ){
573 cnt = 1;
574 pExpr->iColumn = -1;
drhc9b84a12002-06-20 11:36:48 +0000575 pExpr->dataType = SQLITE_SO_NUM;
drhc4a3c772001-04-04 11:48:57 +0000576 }
drhcce7d172000-05-31 15:34:51 +0000577 sqliteFree(zLeft);
578 sqliteFree(zRight);
579 if( cnt==0 ){
drh967e8b72000-06-21 13:59:10 +0000580 sqliteSetNString(&pParse->zErrMsg, "no such column: ", -1,
drhcce7d172000-05-31 15:34:51 +0000581 pLeft->token.z, pLeft->token.n, ".", 1,
582 pRight->token.z, pRight->token.n, 0);
583 pParse->nErr++;
584 return 1;
585 }else if( cnt>1 ){
drh967e8b72000-06-21 13:59:10 +0000586 sqliteSetNString(&pParse->zErrMsg, "ambiguous column name: ", -1,
drhcce7d172000-05-31 15:34:51 +0000587 pLeft->token.z, pLeft->token.n, ".", 1,
588 pRight->token.z, pRight->token.n, 0);
589 pParse->nErr++;
590 return 1;
591 }
592 sqliteExprDelete(pLeft);
593 pExpr->pLeft = 0;
594 sqliteExprDelete(pRight);
595 pExpr->pRight = 0;
drh967e8b72000-06-21 13:59:10 +0000596 pExpr->op = TK_COLUMN;
drhcce7d172000-05-31 15:34:51 +0000597 break;
598 }
599
drhfef52082000-06-06 01:50:43 +0000600 case TK_IN: {
drhd8bc7082000-06-07 23:51:50 +0000601 Vdbe *v = sqliteGetVdbe(pParse);
drhfef52082000-06-06 01:50:43 +0000602 if( v==0 ) return 1;
drh832508b2002-03-02 17:04:07 +0000603 if( sqliteExprResolveIds(pParse, base, pTabList, pEList, pExpr->pLeft) ){
drhcfab11b2000-06-06 03:31:22 +0000604 return 1;
605 }
drhfef52082000-06-06 01:50:43 +0000606 if( pExpr->pSelect ){
607 /* Case 1: expr IN (SELECT ...)
608 **
609 ** Generate code to write the results of the select into a temporary
drh4794b982000-06-06 13:54:14 +0000610 ** table. The cursor number of the temporary table has already
611 ** been put in iTable by sqliteExprResolveInSelect().
drhfef52082000-06-06 01:50:43 +0000612 */
drh832508b2002-03-02 17:04:07 +0000613 pExpr->iTable = pParse->nTab++;
drhc6b52df2002-01-04 03:09:29 +0000614 sqliteVdbeAddOp(v, OP_OpenTemp, pExpr->iTable, 1);
drh832508b2002-03-02 17:04:07 +0000615 sqliteSelect(pParse, pExpr->pSelect, SRT_Set, pExpr->iTable, 0,0,0);
drhfef52082000-06-06 01:50:43 +0000616 }else if( pExpr->pList ){
617 /* Case 2: expr IN (exprlist)
618 **
619 ** Create a set to put the exprlist values in. The Set id is stored
620 ** in iTable.
621 */
622 int i, iSet;
623 for(i=0; i<pExpr->pList->nExpr; i++){
624 Expr *pE2 = pExpr->pList->a[i].pExpr;
drh92086432002-01-22 14:11:29 +0000625 if( !sqliteExprIsConstant(pE2) ){
drhfef52082000-06-06 01:50:43 +0000626 sqliteSetString(&pParse->zErrMsg,
627 "right-hand side of IN operator must be constant", 0);
628 pParse->nErr++;
629 return 1;
630 }
drh4794b982000-06-06 13:54:14 +0000631 if( sqliteExprCheck(pParse, pE2, 0, 0) ){
632 return 1;
633 }
drhfef52082000-06-06 01:50:43 +0000634 }
635 iSet = pExpr->iTable = pParse->nSet++;
636 for(i=0; i<pExpr->pList->nExpr; i++){
637 Expr *pE2 = pExpr->pList->a[i].pExpr;
638 switch( pE2->op ){
639 case TK_FLOAT:
640 case TK_INTEGER:
641 case TK_STRING: {
drh99fcd712001-10-13 01:06:47 +0000642 int addr = sqliteVdbeAddOp(v, OP_SetInsert, iSet, 0);
drha76b5df2002-02-23 02:32:10 +0000643 assert( pE2->token.z );
drhfef52082000-06-06 01:50:43 +0000644 sqliteVdbeChangeP3(v, addr, pE2->token.z, pE2->token.n);
645 sqliteVdbeDequoteP3(v, addr);
646 break;
647 }
648 default: {
649 sqliteExprCode(pParse, pE2);
drh99fcd712001-10-13 01:06:47 +0000650 sqliteVdbeAddOp(v, OP_SetInsert, iSet, 0);
drhfef52082000-06-06 01:50:43 +0000651 break;
652 }
653 }
654 }
655 }
drhcfab11b2000-06-06 03:31:22 +0000656 break;
drhfef52082000-06-06 01:50:43 +0000657 }
658
drh19a775c2000-06-05 18:54:46 +0000659 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +0000660 /* This has to be a scalar SELECT. Generate code to put the
661 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +0000662 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +0000663 */
drh967e8b72000-06-21 13:59:10 +0000664 pExpr->iColumn = pParse->nMem++;
drh832508b2002-03-02 17:04:07 +0000665 if( sqliteSelect(pParse, pExpr->pSelect, SRT_Mem, pExpr->iColumn,0,0,0) ){
drh19a775c2000-06-05 18:54:46 +0000666 return 1;
667 }
668 break;
669 }
670
drhcce7d172000-05-31 15:34:51 +0000671 /* For all else, just recursively walk the tree */
672 default: {
drh4794b982000-06-06 13:54:14 +0000673 if( pExpr->pLeft
drh832508b2002-03-02 17:04:07 +0000674 && sqliteExprResolveIds(pParse, base, pTabList, pEList, pExpr->pLeft) ){
drhcce7d172000-05-31 15:34:51 +0000675 return 1;
676 }
677 if( pExpr->pRight
drh832508b2002-03-02 17:04:07 +0000678 && sqliteExprResolveIds(pParse, base, pTabList, pEList, pExpr->pRight) ){
drhcce7d172000-05-31 15:34:51 +0000679 return 1;
680 }
681 if( pExpr->pList ){
682 int i;
683 ExprList *pList = pExpr->pList;
684 for(i=0; i<pList->nExpr; i++){
drh832508b2002-03-02 17:04:07 +0000685 Expr *pArg = pList->a[i].pExpr;
686 if( sqliteExprResolveIds(pParse, base, pTabList, pEList, pArg) ){
drhcce7d172000-05-31 15:34:51 +0000687 return 1;
688 }
689 }
690 }
691 }
692 }
693 return 0;
694}
695
drhcce7d172000-05-31 15:34:51 +0000696/*
drh4b59ab52002-08-24 18:24:51 +0000697** pExpr is a node that defines a function of some kind. It might
698** be a syntactic function like "count(x)" or it might be a function
699** that implements an operator, like "a LIKE b".
700**
701** This routine makes *pzName point to the name of the function and
702** *pnName hold the number of characters in the function name.
703*/
704static void getFunctionName(Expr *pExpr, const char **pzName, int *pnName){
705 switch( pExpr->op ){
706 case TK_FUNCTION: {
707 *pzName = pExpr->token.z;
drh6977fea2002-10-22 23:38:04 +0000708 *pnName = pExpr->token.n;
drh4b59ab52002-08-24 18:24:51 +0000709 break;
710 }
711 case TK_LIKE: {
712 *pzName = "like";
713 *pnName = 4;
714 break;
715 }
716 case TK_GLOB: {
717 *pzName = "glob";
718 *pnName = 4;
719 break;
720 }
721 default: {
722 *pzName = "can't happen";
723 *pnName = 12;
724 break;
725 }
726 }
727}
728
729/*
drhcce7d172000-05-31 15:34:51 +0000730** Error check the functions in an expression. Make sure all
731** function names are recognized and all functions have the correct
732** number of arguments. Leave an error message in pParse->zErrMsg
733** if anything is amiss. Return the number of errors.
734**
735** if pIsAgg is not null and this expression is an aggregate function
736** (like count(*) or max(value)) then write a 1 into *pIsAgg.
737*/
738int sqliteExprCheck(Parse *pParse, Expr *pExpr, int allowAgg, int *pIsAgg){
739 int nErr = 0;
740 if( pExpr==0 ) return 0;
drhcce7d172000-05-31 15:34:51 +0000741 switch( pExpr->op ){
drh4b59ab52002-08-24 18:24:51 +0000742 case TK_GLOB:
743 case TK_LIKE:
drhcce7d172000-05-31 15:34:51 +0000744 case TK_FUNCTION: {
drhc9b84a12002-06-20 11:36:48 +0000745 int n = pExpr->pList ? pExpr->pList->nExpr : 0; /* Number of arguments */
746 int no_such_func = 0; /* True if no such function exists */
747 int is_type_of = 0; /* True if is the special TypeOf() function */
748 int wrong_num_args = 0; /* True if wrong number of arguments */
749 int is_agg = 0; /* True if is an aggregate function */
drhcce7d172000-05-31 15:34:51 +0000750 int i;
drh4b59ab52002-08-24 18:24:51 +0000751 int nId; /* Number of characters in function name */
752 const char *zId; /* The function name. */
drh0bce8352002-02-28 00:41:10 +0000753 FuncDef *pDef;
754
drh4b59ab52002-08-24 18:24:51 +0000755 getFunctionName(pExpr, &zId, &nId);
756 pDef = sqliteFindFunction(pParse->db, zId, nId, n, 0);
drh0bce8352002-02-28 00:41:10 +0000757 if( pDef==0 ){
drh4b59ab52002-08-24 18:24:51 +0000758 pDef = sqliteFindFunction(pParse->db, zId, nId, -1, 0);
drh0bce8352002-02-28 00:41:10 +0000759 if( pDef==0 ){
drh4b59ab52002-08-24 18:24:51 +0000760 if( n==1 && nId==6 && sqliteStrNICmp(zId, "typeof", 6)==0 ){
drhc9b84a12002-06-20 11:36:48 +0000761 is_type_of = 1;
762 }else {
763 no_such_func = 1;
764 }
drh0bce8352002-02-28 00:41:10 +0000765 }else{
766 wrong_num_args = 1;
drhcce7d172000-05-31 15:34:51 +0000767 }
drh0bce8352002-02-28 00:41:10 +0000768 }else{
769 is_agg = pDef->xFunc==0;
drhcce7d172000-05-31 15:34:51 +0000770 }
drh8e0a2f92002-02-23 23:45:45 +0000771 if( is_agg && !allowAgg ){
772 sqliteSetNString(&pParse->zErrMsg, "misuse of aggregate function ", -1,
drh4b59ab52002-08-24 18:24:51 +0000773 zId, nId, "()", 2, 0);
drh8e0a2f92002-02-23 23:45:45 +0000774 pParse->nErr++;
775 nErr++;
776 is_agg = 0;
777 }else if( no_such_func ){
drh4b59ab52002-08-24 18:24:51 +0000778 sqliteSetNString(&pParse->zErrMsg, "no such function: ", -1, zId,nId,0);
drhcce7d172000-05-31 15:34:51 +0000779 pParse->nErr++;
780 nErr++;
drh8e0a2f92002-02-23 23:45:45 +0000781 }else if( wrong_num_args ){
782 sqliteSetNString(&pParse->zErrMsg,
drh4b59ab52002-08-24 18:24:51 +0000783 "wrong number of arguments to function ", -1, zId, nId, "()", 2, 0);
drh8e0a2f92002-02-23 23:45:45 +0000784 pParse->nErr++;
785 nErr++;
drhcce7d172000-05-31 15:34:51 +0000786 }
drh22827922000-06-06 17:27:05 +0000787 if( is_agg ) pExpr->op = TK_AGG_FUNCTION;
drhcce7d172000-05-31 15:34:51 +0000788 if( is_agg && pIsAgg ) *pIsAgg = 1;
789 for(i=0; nErr==0 && i<n; i++){
drh4cfa7932000-06-08 15:10:46 +0000790 nErr = sqliteExprCheck(pParse, pExpr->pList->a[i].pExpr,
791 allowAgg && !is_agg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000792 }
drhc9b84a12002-06-20 11:36:48 +0000793 if( pDef==0 ){
794 if( is_type_of ){
795 pExpr->op = TK_STRING;
796 if( sqliteExprType(pExpr->pList->a[0].pExpr)==SQLITE_SO_NUM ){
797 pExpr->token.z = "numeric";
798 pExpr->token.n = 7;
799 }else{
800 pExpr->token.z = "text";
801 pExpr->token.n = 4;
802 }
803 }
804 }else if( pDef->dataType>=0 ){
805 if( pDef->dataType<n ){
806 pExpr->dataType =
807 sqliteExprType(pExpr->pList->a[pDef->dataType].pExpr);
808 }else{
809 pExpr->dataType = SQLITE_SO_NUM;
810 }
811 }else if( pDef->dataType==SQLITE_ARGS ){
812 pDef->dataType = SQLITE_SO_TEXT;
813 for(i=0; i<n; i++){
814 if( sqliteExprType(pExpr->pList->a[i].pExpr)==SQLITE_SO_NUM ){
815 pExpr->dataType = SQLITE_SO_NUM;
816 break;
817 }
818 }
819 }else if( pDef->dataType==SQLITE_NUMERIC ){
820 pExpr->dataType = SQLITE_SO_NUM;
821 }else{
822 pExpr->dataType = SQLITE_SO_TEXT;
823 }
drhcce7d172000-05-31 15:34:51 +0000824 }
825 default: {
826 if( pExpr->pLeft ){
drh22827922000-06-06 17:27:05 +0000827 nErr = sqliteExprCheck(pParse, pExpr->pLeft, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000828 }
829 if( nErr==0 && pExpr->pRight ){
drh22827922000-06-06 17:27:05 +0000830 nErr = sqliteExprCheck(pParse, pExpr->pRight, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000831 }
drhfef52082000-06-06 01:50:43 +0000832 if( nErr==0 && pExpr->pList ){
833 int n = pExpr->pList->nExpr;
834 int i;
835 for(i=0; nErr==0 && i<n; i++){
drh22827922000-06-06 17:27:05 +0000836 Expr *pE2 = pExpr->pList->a[i].pExpr;
837 nErr = sqliteExprCheck(pParse, pE2, allowAgg, pIsAgg);
drhfef52082000-06-06 01:50:43 +0000838 }
839 }
drhcce7d172000-05-31 15:34:51 +0000840 break;
841 }
842 }
843 return nErr;
844}
845
846/*
drhc9b84a12002-06-20 11:36:48 +0000847** Return either SQLITE_SO_NUM or SQLITE_SO_TEXT to indicate whether the
848** given expression should sort as numeric values or as text.
849**
850** The sqliteExprResolveIds() and sqliteExprCheck() routines must have
851** both been called on the expression before it is passed to this routine.
852*/
853int sqliteExprType(Expr *p){
854 if( p==0 ) return SQLITE_SO_NUM;
855 while( p ) switch( p->op ){
856 case TK_PLUS:
857 case TK_MINUS:
858 case TK_STAR:
859 case TK_SLASH:
860 case TK_AND:
861 case TK_OR:
862 case TK_ISNULL:
863 case TK_NOTNULL:
864 case TK_NOT:
865 case TK_UMINUS:
drh4b59ab52002-08-24 18:24:51 +0000866 case TK_UPLUS:
drhc9b84a12002-06-20 11:36:48 +0000867 case TK_BITAND:
868 case TK_BITOR:
869 case TK_BITNOT:
870 case TK_LSHIFT:
871 case TK_RSHIFT:
872 case TK_REM:
873 case TK_INTEGER:
874 case TK_FLOAT:
875 case TK_IN:
876 case TK_BETWEEN:
drh4b59ab52002-08-24 18:24:51 +0000877 case TK_GLOB:
878 case TK_LIKE:
drhc9b84a12002-06-20 11:36:48 +0000879 return SQLITE_SO_NUM;
880
881 case TK_STRING:
882 case TK_NULL:
883 case TK_CONCAT:
884 return SQLITE_SO_TEXT;
885
886 case TK_LT:
887 case TK_LE:
888 case TK_GT:
889 case TK_GE:
890 case TK_NE:
891 case TK_EQ:
892 if( sqliteExprType(p->pLeft)==SQLITE_SO_NUM ){
893 return SQLITE_SO_NUM;
894 }
895 p = p->pRight;
896 break;
897
898 case TK_AS:
899 p = p->pLeft;
900 break;
901
902 case TK_COLUMN:
903 case TK_FUNCTION:
904 case TK_AGG_FUNCTION:
905 return p->dataType;
906
907 case TK_SELECT:
908 assert( p->pSelect );
909 assert( p->pSelect->pEList );
910 assert( p->pSelect->pEList->nExpr>0 );
911 p = p->pSelect->pEList->a[0].pExpr;
912 break;
913
drhb1363202002-06-26 02:45:03 +0000914 case TK_CASE: {
915 if( p->pRight && sqliteExprType(p->pRight)==SQLITE_SO_NUM ){
916 return SQLITE_SO_NUM;
917 }
918 if( p->pList ){
919 int i;
920 ExprList *pList = p->pList;
921 for(i=1; i<pList->nExpr; i+=2){
922 if( sqliteExprType(pList->a[i].pExpr)==SQLITE_SO_NUM ){
923 return SQLITE_SO_NUM;
924 }
925 }
926 }
927 return SQLITE_SO_TEXT;
928 }
929
drhc9b84a12002-06-20 11:36:48 +0000930 default:
931 assert( p->op==TK_ABORT ); /* Can't Happen */
932 break;
933 }
934 return SQLITE_SO_NUM;
935}
936
937/*
drhcce7d172000-05-31 15:34:51 +0000938** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +0000939** expression and leave the result on the top of stack.
drhcce7d172000-05-31 15:34:51 +0000940*/
941void sqliteExprCode(Parse *pParse, Expr *pExpr){
942 Vdbe *v = pParse->pVdbe;
943 int op;
drhdaffd0e2001-04-11 14:28:42 +0000944 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +0000945 switch( pExpr->op ){
946 case TK_PLUS: op = OP_Add; break;
947 case TK_MINUS: op = OP_Subtract; break;
948 case TK_STAR: op = OP_Multiply; break;
949 case TK_SLASH: op = OP_Divide; break;
950 case TK_AND: op = OP_And; break;
951 case TK_OR: op = OP_Or; break;
952 case TK_LT: op = OP_Lt; break;
953 case TK_LE: op = OP_Le; break;
954 case TK_GT: op = OP_Gt; break;
955 case TK_GE: op = OP_Ge; break;
956 case TK_NE: op = OP_Ne; break;
957 case TK_EQ: op = OP_Eq; break;
drhcce7d172000-05-31 15:34:51 +0000958 case TK_ISNULL: op = OP_IsNull; break;
959 case TK_NOTNULL: op = OP_NotNull; break;
960 case TK_NOT: op = OP_Not; break;
961 case TK_UMINUS: op = OP_Negative; break;
drhbf4133c2001-10-13 02:59:08 +0000962 case TK_BITAND: op = OP_BitAnd; break;
963 case TK_BITOR: op = OP_BitOr; break;
964 case TK_BITNOT: op = OP_BitNot; break;
965 case TK_LSHIFT: op = OP_ShiftLeft; break;
966 case TK_RSHIFT: op = OP_ShiftRight; break;
967 case TK_REM: op = OP_Remainder; break;
drhcce7d172000-05-31 15:34:51 +0000968 default: break;
969 }
970 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +0000971 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +0000972 if( pParse->useAgg ){
drh99fcd712001-10-13 01:06:47 +0000973 sqliteVdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drhc4a3c772001-04-04 11:48:57 +0000974 }else if( pExpr->iColumn>=0 ){
drh99fcd712001-10-13 01:06:47 +0000975 sqliteVdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn);
drhc4a3c772001-04-04 11:48:57 +0000976 }else{
drh99fcd712001-10-13 01:06:47 +0000977 sqliteVdbeAddOp(v, OP_Recno, pExpr->iTable, 0);
drh22827922000-06-06 17:27:05 +0000978 }
drhcce7d172000-05-31 15:34:51 +0000979 break;
980 }
981 case TK_INTEGER: {
drhd9e30932002-06-09 01:16:01 +0000982 int iVal = atoi(pExpr->token.z);
983 char zBuf[30];
984 sprintf(zBuf,"%d",iVal);
985 if( strlen(zBuf)!=pExpr->token.n
986 || strncmp(pExpr->token.z,zBuf,pExpr->token.n)!=0 ){
987 /* If the integer value cannot be represented exactly in 32 bits,
988 ** then code it as a string instead. */
989 sqliteVdbeAddOp(v, OP_String, 0, 0);
990 }else{
991 sqliteVdbeAddOp(v, OP_Integer, iVal, 0);
992 }
drhe6840902002-03-06 03:08:25 +0000993 sqliteVdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
994 break;
995 }
996 case TK_FLOAT: {
drh7a7c7392001-11-24 00:31:46 +0000997 sqliteVdbeAddOp(v, OP_String, 0, 0);
drha76b5df2002-02-23 02:32:10 +0000998 assert( pExpr->token.z );
drh7a7c7392001-11-24 00:31:46 +0000999 sqliteVdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
drhcce7d172000-05-31 15:34:51 +00001000 break;
1001 }
drhcce7d172000-05-31 15:34:51 +00001002 case TK_STRING: {
drh99fcd712001-10-13 01:06:47 +00001003 int addr = sqliteVdbeAddOp(v, OP_String, 0, 0);
drha76b5df2002-02-23 02:32:10 +00001004 assert( pExpr->token.z );
drhcce7d172000-05-31 15:34:51 +00001005 sqliteVdbeChangeP3(v, addr, pExpr->token.z, pExpr->token.n);
1006 sqliteVdbeDequoteP3(v, addr);
1007 break;
1008 }
1009 case TK_NULL: {
drh99fcd712001-10-13 01:06:47 +00001010 sqliteVdbeAddOp(v, OP_String, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001011 break;
1012 }
drhc9b84a12002-06-20 11:36:48 +00001013 case TK_LT:
1014 case TK_LE:
1015 case TK_GT:
1016 case TK_GE:
1017 case TK_NE:
1018 case TK_EQ: {
drh491791a2002-07-18 00:34:09 +00001019 if( pParse->db->file_format>=4 && sqliteExprType(pExpr)==SQLITE_SO_TEXT ){
drhc9b84a12002-06-20 11:36:48 +00001020 op += 6; /* Convert numeric opcodes to text opcodes */
1021 }
1022 /* Fall through into the next case */
1023 }
drhcce7d172000-05-31 15:34:51 +00001024 case TK_AND:
1025 case TK_OR:
1026 case TK_PLUS:
1027 case TK_STAR:
1028 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00001029 case TK_REM:
1030 case TK_BITAND:
1031 case TK_BITOR:
drhc9b84a12002-06-20 11:36:48 +00001032 case TK_SLASH: {
drhcce7d172000-05-31 15:34:51 +00001033 sqliteExprCode(pParse, pExpr->pLeft);
1034 sqliteExprCode(pParse, pExpr->pRight);
drh99fcd712001-10-13 01:06:47 +00001035 sqliteVdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001036 break;
1037 }
drhbf4133c2001-10-13 02:59:08 +00001038 case TK_LSHIFT:
1039 case TK_RSHIFT: {
1040 sqliteExprCode(pParse, pExpr->pRight);
1041 sqliteExprCode(pParse, pExpr->pLeft);
1042 sqliteVdbeAddOp(v, op, 0, 0);
1043 break;
1044 }
drh00400772000-06-16 20:51:26 +00001045 case TK_CONCAT: {
1046 sqliteExprCode(pParse, pExpr->pLeft);
1047 sqliteExprCode(pParse, pExpr->pRight);
drh99fcd712001-10-13 01:06:47 +00001048 sqliteVdbeAddOp(v, OP_Concat, 2, 0);
drh00400772000-06-16 20:51:26 +00001049 break;
1050 }
drh4b59ab52002-08-24 18:24:51 +00001051 case TK_UPLUS: {
1052 Expr *pLeft = pExpr->pLeft;
1053 if( pLeft && pLeft->op==TK_INTEGER ){
1054 sqliteVdbeAddOp(v, OP_Integer, atoi(pLeft->token.z), 0);
1055 sqliteVdbeChangeP3(v, -1, pLeft->token.z, pLeft->token.n);
1056 }else if( pLeft && pLeft->op==TK_FLOAT ){
1057 sqliteVdbeAddOp(v, OP_String, 0, 0);
1058 sqliteVdbeChangeP3(v, -1, pLeft->token.z, pLeft->token.n);
1059 }else{
1060 sqliteExprCode(pParse, pExpr->pLeft);
1061 }
1062 break;
1063 }
drhcce7d172000-05-31 15:34:51 +00001064 case TK_UMINUS: {
drh6e142f52000-06-08 13:36:40 +00001065 assert( pExpr->pLeft );
drh7a7c7392001-11-24 00:31:46 +00001066 if( pExpr->pLeft->op==TK_FLOAT || pExpr->pLeft->op==TK_INTEGER ){
drh6e142f52000-06-08 13:36:40 +00001067 Token *p = &pExpr->pLeft->token;
1068 char *z = sqliteMalloc( p->n + 2 );
1069 sprintf(z, "-%.*s", p->n, p->z);
drhe6840902002-03-06 03:08:25 +00001070 if( pExpr->pLeft->op==TK_INTEGER ){
1071 sqliteVdbeAddOp(v, OP_Integer, atoi(z), 0);
1072 }else{
1073 sqliteVdbeAddOp(v, OP_String, 0, 0);
1074 }
drh99fcd712001-10-13 01:06:47 +00001075 sqliteVdbeChangeP3(v, -1, z, p->n+1);
drh6e142f52000-06-08 13:36:40 +00001076 sqliteFree(z);
1077 break;
1078 }
drh1ccde152000-06-17 13:12:39 +00001079 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +00001080 }
drhbf4133c2001-10-13 02:59:08 +00001081 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00001082 case TK_NOT: {
drhcce7d172000-05-31 15:34:51 +00001083 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +00001084 sqliteVdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001085 break;
1086 }
1087 case TK_ISNULL:
1088 case TK_NOTNULL: {
1089 int dest;
drh99fcd712001-10-13 01:06:47 +00001090 sqliteVdbeAddOp(v, OP_Integer, 1, 0);
drhcce7d172000-05-31 15:34:51 +00001091 sqliteExprCode(pParse, pExpr->pLeft);
1092 dest = sqliteVdbeCurrentAddr(v) + 2;
drhf5905aa2002-05-26 20:54:33 +00001093 sqliteVdbeAddOp(v, op, 1, dest);
drh99fcd712001-10-13 01:06:47 +00001094 sqliteVdbeAddOp(v, OP_AddImm, -1, 0);
drhcce7d172000-05-31 15:34:51 +00001095 break;
1096 }
drh22827922000-06-06 17:27:05 +00001097 case TK_AGG_FUNCTION: {
drh99fcd712001-10-13 01:06:47 +00001098 sqliteVdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drh22827922000-06-06 17:27:05 +00001099 break;
1100 }
drh4b59ab52002-08-24 18:24:51 +00001101 case TK_GLOB:
1102 case TK_LIKE:
drhcce7d172000-05-31 15:34:51 +00001103 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00001104 int i;
1105 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00001106 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00001107 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00001108 int nId;
1109 const char *zId;
1110 getFunctionName(pExpr, &zId, &nId);
1111 pDef = sqliteFindFunction(pParse->db, zId, nId, nExpr, 0);
drh0bce8352002-02-28 00:41:10 +00001112 assert( pDef!=0 );
drh89425d52002-02-28 03:04:48 +00001113 for(i=0; i<nExpr; i++){
drh0bce8352002-02-28 00:41:10 +00001114 sqliteExprCode(pParse, pList->a[i].pExpr);
drhcce7d172000-05-31 15:34:51 +00001115 }
drh89425d52002-02-28 03:04:48 +00001116 sqliteVdbeAddOp(v, OP_Function, nExpr, 0);
drh0bce8352002-02-28 00:41:10 +00001117 sqliteVdbeChangeP3(v, -1, (char*)pDef, P3_POINTER);
drhcce7d172000-05-31 15:34:51 +00001118 break;
1119 }
drh19a775c2000-06-05 18:54:46 +00001120 case TK_SELECT: {
drh99fcd712001-10-13 01:06:47 +00001121 sqliteVdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drh19a775c2000-06-05 18:54:46 +00001122 break;
1123 }
drhfef52082000-06-06 01:50:43 +00001124 case TK_IN: {
1125 int addr;
drh99fcd712001-10-13 01:06:47 +00001126 sqliteVdbeAddOp(v, OP_Integer, 1, 0);
drhfef52082000-06-06 01:50:43 +00001127 sqliteExprCode(pParse, pExpr->pLeft);
1128 addr = sqliteVdbeCurrentAddr(v);
drhf5905aa2002-05-26 20:54:33 +00001129 sqliteVdbeAddOp(v, OP_NotNull, -1, addr+4);
1130 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1131 sqliteVdbeAddOp(v, OP_String, 0, 0);
1132 sqliteVdbeAddOp(v, OP_Goto, 0, addr+6);
drhfef52082000-06-06 01:50:43 +00001133 if( pExpr->pSelect ){
drhf5905aa2002-05-26 20:54:33 +00001134 sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, addr+6);
drhfef52082000-06-06 01:50:43 +00001135 }else{
drhf5905aa2002-05-26 20:54:33 +00001136 sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, addr+6);
drhfef52082000-06-06 01:50:43 +00001137 }
drh99fcd712001-10-13 01:06:47 +00001138 sqliteVdbeAddOp(v, OP_AddImm, -1, 0);
drhfef52082000-06-06 01:50:43 +00001139 break;
1140 }
1141 case TK_BETWEEN: {
drhf5905aa2002-05-26 20:54:33 +00001142 sqliteExprCode(pParse, pExpr->pLeft);
1143 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
1144 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
1145 sqliteVdbeAddOp(v, OP_Ge, 0, 0);
1146 sqliteVdbeAddOp(v, OP_Pull, 1, 0);
1147 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
1148 sqliteVdbeAddOp(v, OP_Le, 0, 0);
1149 sqliteVdbeAddOp(v, OP_And, 0, 0);
drhfef52082000-06-06 01:50:43 +00001150 break;
1151 }
drha2e00042002-01-22 03:13:42 +00001152 case TK_AS: {
1153 sqliteExprCode(pParse, pExpr->pLeft);
1154 break;
1155 }
drh17a7f8d2002-03-24 13:13:27 +00001156 case TK_CASE: {
1157 int expr_end_label;
drhf5905aa2002-05-26 20:54:33 +00001158 int jumpInst;
1159 int addr;
1160 int nExpr;
drh17a7f8d2002-03-24 13:13:27 +00001161 int i;
1162
1163 assert(pExpr->pList);
1164 assert((pExpr->pList->nExpr % 2) == 0);
1165 assert(pExpr->pList->nExpr > 0);
drhf5905aa2002-05-26 20:54:33 +00001166 nExpr = pExpr->pList->nExpr;
1167 expr_end_label = sqliteVdbeMakeLabel(v);
drh17a7f8d2002-03-24 13:13:27 +00001168 if( pExpr->pLeft ){
1169 sqliteExprCode(pParse, pExpr->pLeft);
1170 }
drhf5905aa2002-05-26 20:54:33 +00001171 for(i=0; i<nExpr; i=i+2){
1172 sqliteExprCode(pParse, pExpr->pList->a[i].pExpr);
drh17a7f8d2002-03-24 13:13:27 +00001173 if( pExpr->pLeft ){
drhf5905aa2002-05-26 20:54:33 +00001174 sqliteVdbeAddOp(v, OP_Dup, 1, 1);
drhf570f012002-05-31 15:51:25 +00001175 jumpInst = sqliteVdbeAddOp(v, OP_Ne, 1, 0);
1176 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001177 }else{
drhf570f012002-05-31 15:51:25 +00001178 jumpInst = sqliteVdbeAddOp(v, OP_IfNot, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001179 }
1180 sqliteExprCode(pParse, pExpr->pList->a[i+1].pExpr);
drhf5905aa2002-05-26 20:54:33 +00001181 sqliteVdbeAddOp(v, OP_Goto, 0, expr_end_label);
drhf5905aa2002-05-26 20:54:33 +00001182 addr = sqliteVdbeCurrentAddr(v);
1183 sqliteVdbeChangeP2(v, jumpInst, addr);
drh17a7f8d2002-03-24 13:13:27 +00001184 }
drhf570f012002-05-31 15:51:25 +00001185 if( pExpr->pLeft ){
1186 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1187 }
drh17a7f8d2002-03-24 13:13:27 +00001188 if( pExpr->pRight ){
1189 sqliteExprCode(pParse, pExpr->pRight);
1190 }else{
drhf5905aa2002-05-26 20:54:33 +00001191 sqliteVdbeAddOp(v, OP_String, 0, 0);
drh17a7f8d2002-03-24 13:13:27 +00001192 }
drhf5905aa2002-05-26 20:54:33 +00001193 sqliteVdbeResolveLabel(v, expr_end_label);
danielk19776f349032002-06-11 02:25:40 +00001194 break;
1195 }
1196 case TK_RAISE: {
1197 if( !pParse->trigStack ){
1198 sqliteSetNString(&pParse->zErrMsg,
1199 "RAISE() may only be used within a trigger-program", -1, 0);
1200 pParse->nErr++;
1201 return;
1202 }
1203 if( pExpr->iColumn == OE_Rollback ||
1204 pExpr->iColumn == OE_Abort ||
1205 pExpr->iColumn == OE_Fail ){
1206 char * msg = sqliteStrNDup(pExpr->token.z, pExpr->token.n);
1207 sqliteVdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn);
1208 sqliteDequote(msg);
1209 sqliteVdbeChangeP3(v, -1, msg, 0);
1210 sqliteFree(msg);
1211 } else {
1212 assert( pExpr->iColumn == OE_Ignore );
1213 sqliteVdbeAddOp(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
1214 sqliteVdbeChangeP3(v, -1, "(IGNORE jump)", -1);
1215 }
drh17a7f8d2002-03-24 13:13:27 +00001216 }
1217 break;
drhcce7d172000-05-31 15:34:51 +00001218 }
drhcce7d172000-05-31 15:34:51 +00001219}
1220
1221/*
1222** Generate code for a boolean expression such that a jump is made
1223** to the label "dest" if the expression is true but execution
1224** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00001225**
1226** If the expression evaluates to NULL (neither true nor false), then
1227** take the jump if the jumpIfNull flag is true.
drhcce7d172000-05-31 15:34:51 +00001228*/
drhf5905aa2002-05-26 20:54:33 +00001229void sqliteExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001230 Vdbe *v = pParse->pVdbe;
1231 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001232 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001233 switch( pExpr->op ){
1234 case TK_LT: op = OP_Lt; break;
1235 case TK_LE: op = OP_Le; break;
1236 case TK_GT: op = OP_Gt; break;
1237 case TK_GE: op = OP_Ge; break;
1238 case TK_NE: op = OP_Ne; break;
1239 case TK_EQ: op = OP_Eq; break;
drhcce7d172000-05-31 15:34:51 +00001240 case TK_ISNULL: op = OP_IsNull; break;
1241 case TK_NOTNULL: op = OP_NotNull; break;
1242 default: break;
1243 }
1244 switch( pExpr->op ){
1245 case TK_AND: {
1246 int d2 = sqliteVdbeMakeLabel(v);
drhf5905aa2002-05-26 20:54:33 +00001247 sqliteExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
1248 sqliteExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001249 sqliteVdbeResolveLabel(v, d2);
1250 break;
1251 }
1252 case TK_OR: {
drhf5905aa2002-05-26 20:54:33 +00001253 sqliteExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
1254 sqliteExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001255 break;
1256 }
1257 case TK_NOT: {
drhf5905aa2002-05-26 20:54:33 +00001258 sqliteExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001259 break;
1260 }
1261 case TK_LT:
1262 case TK_LE:
1263 case TK_GT:
1264 case TK_GE:
1265 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00001266 case TK_EQ: {
drhcce7d172000-05-31 15:34:51 +00001267 sqliteExprCode(pParse, pExpr->pLeft);
1268 sqliteExprCode(pParse, pExpr->pRight);
drh491791a2002-07-18 00:34:09 +00001269 if( pParse->db->file_format>=4 && sqliteExprType(pExpr)==SQLITE_SO_TEXT ){
drhc9b84a12002-06-20 11:36:48 +00001270 op += 6; /* Convert numeric opcodes to text opcodes */
1271 }
drhf5905aa2002-05-26 20:54:33 +00001272 sqliteVdbeAddOp(v, op, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001273 break;
1274 }
1275 case TK_ISNULL:
1276 case TK_NOTNULL: {
1277 sqliteExprCode(pParse, pExpr->pLeft);
drhf5905aa2002-05-26 20:54:33 +00001278 sqliteVdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001279 break;
1280 }
drhfef52082000-06-06 01:50:43 +00001281 case TK_IN: {
drhf5905aa2002-05-26 20:54:33 +00001282 int addr;
drhcfab11b2000-06-06 03:31:22 +00001283 sqliteExprCode(pParse, pExpr->pLeft);
drhf5905aa2002-05-26 20:54:33 +00001284 addr = sqliteVdbeCurrentAddr(v);
1285 sqliteVdbeAddOp(v, OP_NotNull, -1, addr+3);
1286 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1287 sqliteVdbeAddOp(v, OP_Goto, 0, jumpIfNull ? dest : addr+4);
drhfef52082000-06-06 01:50:43 +00001288 if( pExpr->pSelect ){
drh99fcd712001-10-13 01:06:47 +00001289 sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001290 }else{
drh99fcd712001-10-13 01:06:47 +00001291 sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001292 }
1293 break;
1294 }
1295 case TK_BETWEEN: {
drhf5905aa2002-05-26 20:54:33 +00001296 int addr;
drhfef52082000-06-06 01:50:43 +00001297 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +00001298 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
drhfef52082000-06-06 01:50:43 +00001299 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
drhf5905aa2002-05-26 20:54:33 +00001300 addr = sqliteVdbeAddOp(v, OP_Lt, !jumpIfNull, 0);
drhfef52082000-06-06 01:50:43 +00001301 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
drhf5905aa2002-05-26 20:54:33 +00001302 sqliteVdbeAddOp(v, OP_Le, jumpIfNull, dest);
drh99fcd712001-10-13 01:06:47 +00001303 sqliteVdbeAddOp(v, OP_Integer, 0, 0);
drhf5905aa2002-05-26 20:54:33 +00001304 sqliteVdbeChangeP2(v, addr, sqliteVdbeCurrentAddr(v));
drh99fcd712001-10-13 01:06:47 +00001305 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00001306 break;
1307 }
drhcce7d172000-05-31 15:34:51 +00001308 default: {
1309 sqliteExprCode(pParse, pExpr);
drhf5905aa2002-05-26 20:54:33 +00001310 sqliteVdbeAddOp(v, OP_If, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001311 break;
1312 }
1313 }
1314}
1315
1316/*
drh66b89c82000-11-28 20:47:17 +00001317** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00001318** to the label "dest" if the expression is false but execution
1319** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00001320**
1321** If the expression evaluates to NULL (neither true nor false) then
1322** jump if jumpIfNull is true or fall through if jumpIfNull is false.
drhcce7d172000-05-31 15:34:51 +00001323*/
drhf5905aa2002-05-26 20:54:33 +00001324void sqliteExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001325 Vdbe *v = pParse->pVdbe;
1326 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001327 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001328 switch( pExpr->op ){
1329 case TK_LT: op = OP_Ge; break;
1330 case TK_LE: op = OP_Gt; break;
1331 case TK_GT: op = OP_Le; break;
1332 case TK_GE: op = OP_Lt; break;
1333 case TK_NE: op = OP_Eq; break;
1334 case TK_EQ: op = OP_Ne; break;
drhcce7d172000-05-31 15:34:51 +00001335 case TK_ISNULL: op = OP_NotNull; break;
1336 case TK_NOTNULL: op = OP_IsNull; break;
1337 default: break;
1338 }
1339 switch( pExpr->op ){
1340 case TK_AND: {
drhf5905aa2002-05-26 20:54:33 +00001341 sqliteExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
1342 sqliteExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001343 break;
1344 }
1345 case TK_OR: {
1346 int d2 = sqliteVdbeMakeLabel(v);
drhf5905aa2002-05-26 20:54:33 +00001347 sqliteExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
1348 sqliteExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001349 sqliteVdbeResolveLabel(v, d2);
1350 break;
1351 }
1352 case TK_NOT: {
drhf5905aa2002-05-26 20:54:33 +00001353 sqliteExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001354 break;
1355 }
1356 case TK_LT:
1357 case TK_LE:
1358 case TK_GT:
1359 case TK_GE:
1360 case TK_NE:
1361 case TK_EQ: {
drh491791a2002-07-18 00:34:09 +00001362 if( pParse->db->file_format>=4 && sqliteExprType(pExpr)==SQLITE_SO_TEXT ){
drh8f619cc2002-09-08 00:04:50 +00001363 /* Convert numeric comparison opcodes into text comparison opcodes.
1364 ** This step depends on the fact that the text comparision opcodes are
1365 ** always 6 greater than their corresponding numeric comparison
1366 ** opcodes.
1367 */
1368 assert( OP_Eq+6 == OP_StrEq );
1369 op += 6;
drhc9b84a12002-06-20 11:36:48 +00001370 }
drhcce7d172000-05-31 15:34:51 +00001371 sqliteExprCode(pParse, pExpr->pLeft);
1372 sqliteExprCode(pParse, pExpr->pRight);
drhf5905aa2002-05-26 20:54:33 +00001373 sqliteVdbeAddOp(v, op, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001374 break;
1375 }
drhcce7d172000-05-31 15:34:51 +00001376 case TK_ISNULL:
1377 case TK_NOTNULL: {
1378 sqliteExprCode(pParse, pExpr->pLeft);
drhf5905aa2002-05-26 20:54:33 +00001379 sqliteVdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001380 break;
1381 }
drhfef52082000-06-06 01:50:43 +00001382 case TK_IN: {
drhf5905aa2002-05-26 20:54:33 +00001383 int addr;
drhcfab11b2000-06-06 03:31:22 +00001384 sqliteExprCode(pParse, pExpr->pLeft);
drhf5905aa2002-05-26 20:54:33 +00001385 addr = sqliteVdbeCurrentAddr(v);
1386 sqliteVdbeAddOp(v, OP_NotNull, -1, addr+3);
1387 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1388 sqliteVdbeAddOp(v, OP_Goto, 0, jumpIfNull ? dest : addr+4);
drhfef52082000-06-06 01:50:43 +00001389 if( pExpr->pSelect ){
drh99fcd712001-10-13 01:06:47 +00001390 sqliteVdbeAddOp(v, OP_NotFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001391 }else{
drh99fcd712001-10-13 01:06:47 +00001392 sqliteVdbeAddOp(v, OP_SetNotFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001393 }
1394 break;
1395 }
1396 case TK_BETWEEN: {
1397 int addr;
1398 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +00001399 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
drhfef52082000-06-06 01:50:43 +00001400 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
1401 addr = sqliteVdbeCurrentAddr(v);
drhf5905aa2002-05-26 20:54:33 +00001402 sqliteVdbeAddOp(v, OP_Ge, !jumpIfNull, addr+3);
drh99fcd712001-10-13 01:06:47 +00001403 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1404 sqliteVdbeAddOp(v, OP_Goto, 0, dest);
drhfef52082000-06-06 01:50:43 +00001405 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
drhf5905aa2002-05-26 20:54:33 +00001406 sqliteVdbeAddOp(v, OP_Gt, jumpIfNull, dest);
drhfef52082000-06-06 01:50:43 +00001407 break;
1408 }
drhcce7d172000-05-31 15:34:51 +00001409 default: {
1410 sqliteExprCode(pParse, pExpr);
drh461c2812002-05-30 02:35:11 +00001411 sqliteVdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001412 break;
1413 }
1414 }
1415}
drh22827922000-06-06 17:27:05 +00001416
1417/*
1418** Do a deep comparison of two expression trees. Return TRUE (non-zero)
1419** if they are identical and return FALSE if they differ in any way.
1420*/
drhd8bc7082000-06-07 23:51:50 +00001421int sqliteExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00001422 int i;
1423 if( pA==0 ){
1424 return pB==0;
1425 }else if( pB==0 ){
1426 return 0;
1427 }
1428 if( pA->op!=pB->op ) return 0;
drhd8bc7082000-06-07 23:51:50 +00001429 if( !sqliteExprCompare(pA->pLeft, pB->pLeft) ) return 0;
1430 if( !sqliteExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00001431 if( pA->pList ){
1432 if( pB->pList==0 ) return 0;
1433 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
1434 for(i=0; i<pA->pList->nExpr; i++){
drhd8bc7082000-06-07 23:51:50 +00001435 if( !sqliteExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00001436 return 0;
1437 }
1438 }
1439 }else if( pB->pList ){
1440 return 0;
1441 }
1442 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00001443 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drh22827922000-06-06 17:27:05 +00001444 if( pA->token.z ){
1445 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00001446 if( pB->token.n!=pA->token.n ) return 0;
1447 if( sqliteStrNICmp(pA->token.z, pB->token.z, pB->token.n)!=0 ) return 0;
drh22827922000-06-06 17:27:05 +00001448 }
1449 return 1;
1450}
1451
1452/*
1453** Add a new element to the pParse->aAgg[] array and return its index.
1454*/
1455static int appendAggInfo(Parse *pParse){
1456 if( (pParse->nAgg & 0x7)==0 ){
1457 int amt = pParse->nAgg + 8;
drh6d4abfb2001-10-22 02:58:08 +00001458 AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0]));
1459 if( aAgg==0 ){
drh22827922000-06-06 17:27:05 +00001460 return -1;
1461 }
drh6d4abfb2001-10-22 02:58:08 +00001462 pParse->aAgg = aAgg;
drh22827922000-06-06 17:27:05 +00001463 }
1464 memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0]));
1465 return pParse->nAgg++;
1466}
1467
1468/*
1469** Analyze the given expression looking for aggregate functions and
1470** for variables that need to be added to the pParse->aAgg[] array.
1471** Make additional entries to the pParse->aAgg[] array as necessary.
1472**
1473** This routine should only be called after the expression has been
1474** analyzed by sqliteExprResolveIds() and sqliteExprCheck().
1475**
1476** If errors are seen, leave an error message in zErrMsg and return
1477** the number of errors.
1478*/
1479int sqliteExprAnalyzeAggregates(Parse *pParse, Expr *pExpr){
1480 int i;
1481 AggExpr *aAgg;
1482 int nErr = 0;
1483
1484 if( pExpr==0 ) return 0;
1485 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00001486 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +00001487 aAgg = pParse->aAgg;
1488 for(i=0; i<pParse->nAgg; i++){
1489 if( aAgg[i].isAgg ) continue;
1490 if( aAgg[i].pExpr->iTable==pExpr->iTable
drh967e8b72000-06-21 13:59:10 +00001491 && aAgg[i].pExpr->iColumn==pExpr->iColumn ){
drh22827922000-06-06 17:27:05 +00001492 break;
1493 }
1494 }
1495 if( i>=pParse->nAgg ){
1496 i = appendAggInfo(pParse);
1497 if( i<0 ) return 1;
1498 pParse->aAgg[i].isAgg = 0;
1499 pParse->aAgg[i].pExpr = pExpr;
1500 }
drhaaf88722000-06-08 11:25:00 +00001501 pExpr->iAgg = i;
drh22827922000-06-06 17:27:05 +00001502 break;
1503 }
1504 case TK_AGG_FUNCTION: {
drh22827922000-06-06 17:27:05 +00001505 aAgg = pParse->aAgg;
1506 for(i=0; i<pParse->nAgg; i++){
1507 if( !aAgg[i].isAgg ) continue;
drhd8bc7082000-06-07 23:51:50 +00001508 if( sqliteExprCompare(aAgg[i].pExpr, pExpr) ){
drh22827922000-06-06 17:27:05 +00001509 break;
1510 }
1511 }
1512 if( i>=pParse->nAgg ){
1513 i = appendAggInfo(pParse);
1514 if( i<0 ) return 1;
1515 pParse->aAgg[i].isAgg = 1;
1516 pParse->aAgg[i].pExpr = pExpr;
drh0bce8352002-02-28 00:41:10 +00001517 pParse->aAgg[i].pFunc = sqliteFindFunction(pParse->db,
drh6977fea2002-10-22 23:38:04 +00001518 pExpr->token.z, pExpr->token.n,
drhf55f25f2002-02-28 01:46:11 +00001519 pExpr->pList ? pExpr->pList->nExpr : 0, 0);
drh22827922000-06-06 17:27:05 +00001520 }
1521 pExpr->iAgg = i;
1522 break;
1523 }
1524 default: {
1525 if( pExpr->pLeft ){
1526 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pLeft);
1527 }
1528 if( nErr==0 && pExpr->pRight ){
1529 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pRight);
1530 }
1531 if( nErr==0 && pExpr->pList ){
1532 int n = pExpr->pList->nExpr;
1533 int i;
1534 for(i=0; nErr==0 && i<n; i++){
1535 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pList->a[i].pExpr);
1536 }
1537 }
1538 break;
1539 }
1540 }
1541 return nErr;
1542}
drh8e0a2f92002-02-23 23:45:45 +00001543
1544/*
1545** Locate a user function given a name and a number of arguments.
drh0bce8352002-02-28 00:41:10 +00001546** Return a pointer to the FuncDef structure that defines that
drh8e0a2f92002-02-23 23:45:45 +00001547** function, or return NULL if the function does not exist.
1548**
drh0bce8352002-02-28 00:41:10 +00001549** If the createFlag argument is true, then a new (blank) FuncDef
drh8e0a2f92002-02-23 23:45:45 +00001550** structure is created and liked into the "db" structure if a
1551** no matching function previously existed. When createFlag is true
1552** and the nArg parameter is -1, then only a function that accepts
1553** any number of arguments will be returned.
1554**
1555** If createFlag is false and nArg is -1, then the first valid
1556** function found is returned. A function is valid if either xFunc
1557** or xStep is non-zero.
1558*/
drh0bce8352002-02-28 00:41:10 +00001559FuncDef *sqliteFindFunction(
drh8e0a2f92002-02-23 23:45:45 +00001560 sqlite *db, /* An open database */
1561 const char *zName, /* Name of the function. Not null-terminated */
1562 int nName, /* Number of characters in the name */
1563 int nArg, /* Number of arguments. -1 means any number */
1564 int createFlag /* Create new entry if true and does not otherwise exist */
1565){
drh0bce8352002-02-28 00:41:10 +00001566 FuncDef *pFirst, *p, *pMaybe;
1567 pFirst = p = (FuncDef*)sqliteHashFind(&db->aFunc, zName, nName);
drh1350b032002-02-27 19:00:20 +00001568 if( p && !createFlag && nArg<0 ){
drh8e0a2f92002-02-23 23:45:45 +00001569 while( p && p->xFunc==0 && p->xStep==0 ){ p = p->pNext; }
1570 return p;
1571 }
1572 pMaybe = 0;
1573 while( p && p->nArg!=nArg ){
1574 if( p->nArg<0 && !createFlag && (p->xFunc || p->xStep) ) pMaybe = p;
1575 p = p->pNext;
1576 }
1577 if( p && !createFlag && p->xFunc==0 && p->xStep==0 ){
1578 return 0;
1579 }
1580 if( p==0 && pMaybe ){
1581 assert( createFlag==0 );
1582 return pMaybe;
1583 }
drh89425d52002-02-28 03:04:48 +00001584 if( p==0 && createFlag && (p = sqliteMalloc(sizeof(*p)))!=0 ){
drh8e0a2f92002-02-23 23:45:45 +00001585 p->nArg = nArg;
1586 p->pNext = pFirst;
drhc9b84a12002-06-20 11:36:48 +00001587 p->dataType = pFirst ? pFirst->dataType : SQLITE_NUMERIC;
drh0bce8352002-02-28 00:41:10 +00001588 sqliteHashInsert(&db->aFunc, zName, nName, (void*)p);
drh8e0a2f92002-02-23 23:45:45 +00001589 }
1590 return p;
1591}