blob: 059b7bbe75725bccc9e16ffc74c1838a9e81692c [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**
drh7bdc0c12003-04-19 17:27:24 +000015** $Id: expr.c,v 1.93 2003/04/19 17:27:25 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;
drh113088e2003-03-20 01:16:58 +0000187 int nByte;
drhad3cab52002-05-24 02:04:32 +0000188 if( p==0 ) return 0;
drh113088e2003-03-20 01:16:58 +0000189 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
190 pNew = sqliteMalloc( nByte );
drhad3cab52002-05-24 02:04:32 +0000191 if( pNew==0 ) return 0;
192 pNew->nSrc = p->nSrc;
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:
drh7bdc0c12003-04-19 17:27:24 +0000302 case TK_FUNCTION:
drhfef52082000-06-06 01:50:43 +0000303 return 0;
drh7bdc0c12003-04-19 17:27:24 +0000304 case TK_NULL:
drh23989372002-05-21 13:43:04 +0000305 case TK_STRING:
drh92086432002-01-22 14:11:29 +0000306 case TK_INTEGER:
307 case TK_FLOAT:
drh92086432002-01-22 14:11:29 +0000308 return 1;
drhfef52082000-06-06 01:50:43 +0000309 default: {
drh92086432002-01-22 14:11:29 +0000310 if( p->pLeft && !sqliteExprIsConstant(p->pLeft) ) return 0;
311 if( p->pRight && !sqliteExprIsConstant(p->pRight) ) return 0;
drhfef52082000-06-06 01:50:43 +0000312 if( p->pList ){
313 int i;
314 for(i=0; i<p->pList->nExpr; i++){
drh92086432002-01-22 14:11:29 +0000315 if( !sqliteExprIsConstant(p->pList->a[i].pExpr) ) return 0;
drhfef52082000-06-06 01:50:43 +0000316 }
317 }
drh92086432002-01-22 14:11:29 +0000318 return p->pLeft!=0 || p->pRight!=0 || (p->pList && p->pList->nExpr>0);
drhfef52082000-06-06 01:50:43 +0000319 }
320 }
drh92086432002-01-22 14:11:29 +0000321 return 0;
drhfef52082000-06-06 01:50:43 +0000322}
323
324/*
drhe4de1fe2002-06-02 16:09:01 +0000325** If the given expression codes a constant integer, return 1 and put
326** the value of the integer in *pValue. If the expression is not an
327** integer, return 0 and leave *pValue unchanged.
328*/
329int sqliteExprIsInteger(Expr *p, int *pValue){
330 switch( p->op ){
331 case TK_INTEGER: {
332 *pValue = atoi(p->token.z);
333 return 1;
334 }
335 case TK_STRING: {
drhbd790ee2002-06-02 18:22:06 +0000336 const char *z = p->token.z;
drhe4de1fe2002-06-02 16:09:01 +0000337 int n = p->token.n;
drhbd790ee2002-06-02 18:22:06 +0000338 if( n>0 && z[0]=='-' ){ z++; n--; }
drhe4de1fe2002-06-02 16:09:01 +0000339 while( n>0 && *z && isdigit(*z) ){ z++; n--; }
340 if( n==0 ){
341 *pValue = atoi(p->token.z);
342 return 1;
343 }
344 break;
345 }
drh4b59ab52002-08-24 18:24:51 +0000346 case TK_UPLUS: {
347 return sqliteExprIsInteger(p->pLeft, pValue);
348 }
drhe4de1fe2002-06-02 16:09:01 +0000349 case TK_UMINUS: {
350 int v;
351 if( sqliteExprIsInteger(p->pLeft, &v) ){
352 *pValue = -v;
353 return 1;
354 }
355 break;
356 }
357 default: break;
358 }
359 return 0;
360}
361
362/*
drhc4a3c772001-04-04 11:48:57 +0000363** Return TRUE if the given string is a row-id column name.
364*/
drha9f9d1c2002-06-29 02:20:08 +0000365int sqliteIsRowid(const char *z){
drhc4a3c772001-04-04 11:48:57 +0000366 if( sqliteStrICmp(z, "_ROWID_")==0 ) return 1;
367 if( sqliteStrICmp(z, "ROWID")==0 ) return 1;
368 if( sqliteStrICmp(z, "OID")==0 ) return 1;
369 return 0;
370}
371
372/*
drhcce7d172000-05-31 15:34:51 +0000373** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +0000374** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +0000375** index to the table in the table list and a column offset. The
376** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
377** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +0000378** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +0000379** VDBE cursor number for a cursor that is pointing into the referenced
380** table. The Expr.iColumn value is changed to the index of the column
381** of the referenced table. The Expr.iColumn value for the special
382** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
383** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +0000384**
drhfef52082000-06-06 01:50:43 +0000385** We also check for instances of the IN operator. IN comes in two
386** forms:
387**
388** expr IN (exprlist)
389** and
390** expr IN (SELECT ...)
391**
392** The first form is handled by creating a set holding the list
393** of allowed values. The second form causes the SELECT to generate
394** a temporary table.
395**
396** This routine also looks for scalar SELECTs that are part of an expression.
drh19a775c2000-06-05 18:54:46 +0000397** If it finds any, it generates code to write the value of that select
398** into a memory cell.
drhcce7d172000-05-31 15:34:51 +0000399**
drh967e8b72000-06-21 13:59:10 +0000400** Unknown columns or tables provoke an error. The function returns
drhcce7d172000-05-31 15:34:51 +0000401** the number of errors seen and leaves an error message on pParse->zErrMsg.
402*/
drha2e00042002-01-22 03:13:42 +0000403int sqliteExprResolveIds(
404 Parse *pParse, /* The parser context */
drh832508b2002-03-02 17:04:07 +0000405 int base, /* VDBE cursor number for first entry in pTabList */
drhad3cab52002-05-24 02:04:32 +0000406 SrcList *pTabList, /* List of tables used to resolve column names */
drha2e00042002-01-22 03:13:42 +0000407 ExprList *pEList, /* List of expressions used to resolve "AS" */
408 Expr *pExpr /* The expression to be analyzed. */
409){
drhdaffd0e2001-04-11 14:28:42 +0000410 if( pExpr==0 || pTabList==0 ) return 0;
drhad3cab52002-05-24 02:04:32 +0000411 assert( base+pTabList->nSrc<=pParse->nTab );
drhcce7d172000-05-31 15:34:51 +0000412 switch( pExpr->op ){
drh23989372002-05-21 13:43:04 +0000413 /* Double-quoted strings (ex: "abc") are used as identifiers if
414 ** possible. Otherwise they remain as strings. Single-quoted
415 ** strings (ex: 'abc') are always string literals.
416 */
417 case TK_STRING: {
418 if( pExpr->token.z[0]=='\'' ) break;
419 /* Fall thru into the TK_ID case if this is a double-quoted string */
420 }
drha2e00042002-01-22 03:13:42 +0000421 /* A lone identifier. Try and match it as follows:
422 **
423 ** 1. To the name of a column of one of the tables in pTabList
424 **
425 ** 2. To the right side of an AS keyword in the column list of
426 ** a SELECT statement. (For example, match against 'x' in
427 ** "SELECT a+b AS 'x' FROM t1".)
428 **
429 ** 3. One of the special names "ROWID", "OID", or "_ROWID_".
430 */
drhcce7d172000-05-31 15:34:51 +0000431 case TK_ID: {
drhc4a3c772001-04-04 11:48:57 +0000432 int cnt = 0; /* Number of matches */
433 int i; /* Loop counter */
drha76b5df2002-02-23 02:32:10 +0000434 char *z;
435 assert( pExpr->token.z );
436 z = sqliteStrNDup(pExpr->token.z, pExpr->token.n);
drh2f4392f2002-02-14 21:42:51 +0000437 sqliteDequote(z);
drhdaffd0e2001-04-11 14:28:42 +0000438 if( z==0 ) return 1;
drhad3cab52002-05-24 02:04:32 +0000439 for(i=0; i<pTabList->nSrc; i++){
drhcce7d172000-05-31 15:34:51 +0000440 int j;
441 Table *pTab = pTabList->a[i].pTab;
442 if( pTab==0 ) continue;
drh417be792002-03-03 18:59:40 +0000443 assert( pTab->nCol>0 );
drhcce7d172000-05-31 15:34:51 +0000444 for(j=0; j<pTab->nCol; j++){
drh7020f652000-06-03 18:06:52 +0000445 if( sqliteStrICmp(pTab->aCol[j].zName, z)==0 ){
drhcce7d172000-05-31 15:34:51 +0000446 cnt++;
drh832508b2002-03-02 17:04:07 +0000447 pExpr->iTable = i + base;
drh4a324312001-12-21 14:30:42 +0000448 if( j==pTab->iPKey ){
449 /* Substitute the record number for the INTEGER PRIMARY KEY */
450 pExpr->iColumn = -1;
drhc9b84a12002-06-20 11:36:48 +0000451 pExpr->dataType = SQLITE_SO_NUM;
drh4a324312001-12-21 14:30:42 +0000452 }else{
453 pExpr->iColumn = j;
drhc9b84a12002-06-20 11:36:48 +0000454 pExpr->dataType = pTab->aCol[j].sortOrder & SQLITE_SO_TYPEMASK;
drh4a324312001-12-21 14:30:42 +0000455 }
drha2e00042002-01-22 03:13:42 +0000456 pExpr->op = TK_COLUMN;
drhcce7d172000-05-31 15:34:51 +0000457 }
458 }
459 }
drha2e00042002-01-22 03:13:42 +0000460 if( cnt==0 && pEList!=0 ){
461 int j;
462 for(j=0; j<pEList->nExpr; j++){
463 char *zAs = pEList->a[j].zName;
464 if( zAs!=0 && sqliteStrICmp(zAs, z)==0 ){
465 cnt++;
466 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
467 pExpr->op = TK_AS;
468 pExpr->iColumn = j;
drh75148a22002-03-03 03:42:31 +0000469 pExpr->pLeft = sqliteExprDup(pEList->a[j].pExpr);
drha2e00042002-01-22 03:13:42 +0000470 }
471 }
472 }
drhc4a3c772001-04-04 11:48:57 +0000473 if( cnt==0 && sqliteIsRowid(z) ){
474 pExpr->iColumn = -1;
drh832508b2002-03-02 17:04:07 +0000475 pExpr->iTable = base;
drhad3cab52002-05-24 02:04:32 +0000476 cnt = 1 + (pTabList->nSrc>1);
drha2e00042002-01-22 03:13:42 +0000477 pExpr->op = TK_COLUMN;
drhc9b84a12002-06-20 11:36:48 +0000478 pExpr->dataType = SQLITE_SO_NUM;
drhc4a3c772001-04-04 11:48:57 +0000479 }
drhcce7d172000-05-31 15:34:51 +0000480 sqliteFree(z);
drh23989372002-05-21 13:43:04 +0000481 if( cnt==0 && pExpr->token.z[0]!='"' ){
drhda93d232003-03-31 02:12:46 +0000482 sqliteErrorMsg(pParse, "no such column: %T", &pExpr->token);
drhcce7d172000-05-31 15:34:51 +0000483 return 1;
484 }else if( cnt>1 ){
drhda93d232003-03-31 02:12:46 +0000485 sqliteErrorMsg(pParse, "ambiguous column name: %T", &pExpr->token);
drhcce7d172000-05-31 15:34:51 +0000486 return 1;
487 }
drhed6c8672003-01-12 18:02:16 +0000488 if( pExpr->op==TK_COLUMN ){
489 sqliteAuthRead(pParse, pExpr, pTabList, base);
490 }
drhcce7d172000-05-31 15:34:51 +0000491 break;
492 }
493
drhd24cc422003-03-27 12:51:24 +0000494 /* A table name and column name: ID.ID
495 ** Or a database, table and column: ID.ID.ID
496 */
drhcce7d172000-05-31 15:34:51 +0000497 case TK_DOT: {
drhbed86902000-06-02 13:27:59 +0000498 int cnt = 0; /* Number of matches */
drhc4a3c772001-04-04 11:48:57 +0000499 int cntTab = 0; /* Number of matching tables */
drhbed86902000-06-02 13:27:59 +0000500 int i; /* Loop counter */
drhcce7d172000-05-31 15:34:51 +0000501 Expr *pLeft, *pRight; /* Left and right subbranches of the expr */
drhcce7d172000-05-31 15:34:51 +0000502 char *zLeft, *zRight; /* Text of an identifier */
drhd24cc422003-03-27 12:51:24 +0000503 char *zDb; /* Name of database holding table */
504 sqlite *db = pParse->db;
drhcce7d172000-05-31 15:34:51 +0000505
drhcce7d172000-05-31 15:34:51 +0000506 pRight = pExpr->pRight;
drhd24cc422003-03-27 12:51:24 +0000507 if( pRight->op==TK_ID ){
508 pLeft = pExpr->pLeft;
509 zDb = 0;
510 }else{
511 Expr *pDb = pExpr->pLeft;
512 assert( pDb && pDb->op==TK_ID && pDb->token.z );
513 zDb = sqliteStrNDup(pDb->token.z, pDb->token.n);
514 pLeft = pRight->pLeft;
515 pRight = pRight->pRight;
516 }
drha76b5df2002-02-23 02:32:10 +0000517 assert( pLeft && pLeft->op==TK_ID && pLeft->token.z );
518 assert( pRight && pRight->op==TK_ID && pRight->token.z );
drh6e142f52000-06-08 13:36:40 +0000519 zLeft = sqliteStrNDup(pLeft->token.z, pLeft->token.n);
520 zRight = sqliteStrNDup(pRight->token.z, pRight->token.n);
drhdaffd0e2001-04-11 14:28:42 +0000521 if( zLeft==0 || zRight==0 ){
522 sqliteFree(zLeft);
523 sqliteFree(zRight);
drhd24cc422003-03-27 12:51:24 +0000524 sqliteFree(zDb);
drhdaffd0e2001-04-11 14:28:42 +0000525 return 1;
526 }
drhd24cc422003-03-27 12:51:24 +0000527 sqliteDequote(zDb);
drh87c40e82001-07-23 14:33:02 +0000528 sqliteDequote(zLeft);
529 sqliteDequote(zRight);
drhc4a3c772001-04-04 11:48:57 +0000530 pExpr->iTable = -1;
drhad3cab52002-05-24 02:04:32 +0000531 for(i=0; i<pTabList->nSrc; i++){
drhcce7d172000-05-31 15:34:51 +0000532 int j;
533 char *zTab;
534 Table *pTab = pTabList->a[i].pTab;
535 if( pTab==0 ) continue;
drh417be792002-03-03 18:59:40 +0000536 assert( pTab->nCol>0 );
drhcce7d172000-05-31 15:34:51 +0000537 if( pTabList->a[i].zAlias ){
538 zTab = pTabList->a[i].zAlias;
drhd24cc422003-03-27 12:51:24 +0000539 if( sqliteStrICmp(zTab, zLeft)!=0 ) continue;
drhcce7d172000-05-31 15:34:51 +0000540 }else{
541 zTab = pTab->zName;
drhd24cc422003-03-27 12:51:24 +0000542 if( zTab==0 || sqliteStrICmp(zTab, zLeft)!=0 ) continue;
543 if( zDb!=0 && sqliteStrICmp(db->aDb[pTab->iDb].zName, zDb)!=0 ){
544 continue;
545 }
drhcce7d172000-05-31 15:34:51 +0000546 }
drh832508b2002-03-02 17:04:07 +0000547 if( 0==(cntTab++) ) pExpr->iTable = i + base;
drhcce7d172000-05-31 15:34:51 +0000548 for(j=0; j<pTab->nCol; j++){
drh7020f652000-06-03 18:06:52 +0000549 if( sqliteStrICmp(pTab->aCol[j].zName, zRight)==0 ){
drhcce7d172000-05-31 15:34:51 +0000550 cnt++;
drh832508b2002-03-02 17:04:07 +0000551 pExpr->iTable = i + base;
drh70ce3f02003-04-15 19:22:22 +0000552 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
553 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
drhc9b84a12002-06-20 11:36:48 +0000554 pExpr->dataType = pTab->aCol[j].sortOrder & SQLITE_SO_TYPEMASK;
drhcce7d172000-05-31 15:34:51 +0000555 }
556 }
557 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000558
559 /* If we have not already resolved this *.* expression, then maybe
560 * it is a new.* or old.* trigger argument reference */
danielk1977f29ce552002-05-19 23:43:12 +0000561 if( cnt == 0 && pParse->trigStack != 0 ){
562 TriggerStack *pTriggerStack = pParse->trigStack;
danielk1977c3f9bad2002-05-15 08:30:12 +0000563 int t = 0;
danielk1977f29ce552002-05-19 23:43:12 +0000564 if( pTriggerStack->newIdx != -1 && sqliteStrICmp("new", zLeft) == 0 ){
565 pExpr->iTable = pTriggerStack->newIdx;
danielk1977c3f9bad2002-05-15 08:30:12 +0000566 cntTab++;
567 t = 1;
568 }
danielk1977f29ce552002-05-19 23:43:12 +0000569 if( pTriggerStack->oldIdx != -1 && sqliteStrICmp("old", zLeft) == 0 ){
570 pExpr->iTable = pTriggerStack->oldIdx;
danielk1977c3f9bad2002-05-15 08:30:12 +0000571 cntTab++;
572 t = 1;
573 }
574
danielk1977f29ce552002-05-19 23:43:12 +0000575 if( t ){
576 int j;
drhc9b84a12002-06-20 11:36:48 +0000577 Table *pTab = pTriggerStack->pTab;
578 for(j=0; j < pTab->nCol; j++) {
579 if( sqliteStrICmp(pTab->aCol[j].zName, zRight)==0 ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000580 cnt++;
drh70ce3f02003-04-15 19:22:22 +0000581 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
drhc9b84a12002-06-20 11:36:48 +0000582 pExpr->dataType = pTab->aCol[j].sortOrder & SQLITE_SO_TYPEMASK;
danielk1977c3f9bad2002-05-15 08:30:12 +0000583 }
584 }
danielk1977f29ce552002-05-19 23:43:12 +0000585 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000586 }
587
drhc4a3c772001-04-04 11:48:57 +0000588 if( cnt==0 && cntTab==1 && sqliteIsRowid(zRight) ){
589 cnt = 1;
590 pExpr->iColumn = -1;
drhc9b84a12002-06-20 11:36:48 +0000591 pExpr->dataType = SQLITE_SO_NUM;
drhc4a3c772001-04-04 11:48:57 +0000592 }
drhd24cc422003-03-27 12:51:24 +0000593 sqliteFree(zDb);
drhcce7d172000-05-31 15:34:51 +0000594 sqliteFree(zLeft);
595 sqliteFree(zRight);
596 if( cnt==0 ){
drhda93d232003-03-31 02:12:46 +0000597 sqliteErrorMsg(pParse, "no such column: %T.%T",
598 &pLeft->token, &pRight->token);
drhcce7d172000-05-31 15:34:51 +0000599 return 1;
600 }else if( cnt>1 ){
drhda93d232003-03-31 02:12:46 +0000601 sqliteErrorMsg(pParse, "ambiguous column name: %T.%T",
602 &pLeft->token, &pRight->token);
drhcce7d172000-05-31 15:34:51 +0000603 return 1;
604 }
drhd24cc422003-03-27 12:51:24 +0000605 sqliteExprDelete(pExpr->pLeft);
drhcce7d172000-05-31 15:34:51 +0000606 pExpr->pLeft = 0;
drhd24cc422003-03-27 12:51:24 +0000607 sqliteExprDelete(pExpr->pRight);
drhcce7d172000-05-31 15:34:51 +0000608 pExpr->pRight = 0;
drh967e8b72000-06-21 13:59:10 +0000609 pExpr->op = TK_COLUMN;
drhed6c8672003-01-12 18:02:16 +0000610 sqliteAuthRead(pParse, pExpr, pTabList, base);
drhcce7d172000-05-31 15:34:51 +0000611 break;
612 }
613
drhfef52082000-06-06 01:50:43 +0000614 case TK_IN: {
drhd8bc7082000-06-07 23:51:50 +0000615 Vdbe *v = sqliteGetVdbe(pParse);
drhfef52082000-06-06 01:50:43 +0000616 if( v==0 ) return 1;
drh832508b2002-03-02 17:04:07 +0000617 if( sqliteExprResolveIds(pParse, base, pTabList, pEList, pExpr->pLeft) ){
drhcfab11b2000-06-06 03:31:22 +0000618 return 1;
619 }
drhfef52082000-06-06 01:50:43 +0000620 if( pExpr->pSelect ){
621 /* Case 1: expr IN (SELECT ...)
622 **
623 ** Generate code to write the results of the select into a temporary
drh4794b982000-06-06 13:54:14 +0000624 ** table. The cursor number of the temporary table has already
625 ** been put in iTable by sqliteExprResolveInSelect().
drhfef52082000-06-06 01:50:43 +0000626 */
drh832508b2002-03-02 17:04:07 +0000627 pExpr->iTable = pParse->nTab++;
drhc6b52df2002-01-04 03:09:29 +0000628 sqliteVdbeAddOp(v, OP_OpenTemp, pExpr->iTable, 1);
drh832508b2002-03-02 17:04:07 +0000629 sqliteSelect(pParse, pExpr->pSelect, SRT_Set, pExpr->iTable, 0,0,0);
drhfef52082000-06-06 01:50:43 +0000630 }else if( pExpr->pList ){
631 /* Case 2: expr IN (exprlist)
632 **
633 ** Create a set to put the exprlist values in. The Set id is stored
634 ** in iTable.
635 */
636 int i, iSet;
637 for(i=0; i<pExpr->pList->nExpr; i++){
638 Expr *pE2 = pExpr->pList->a[i].pExpr;
drh92086432002-01-22 14:11:29 +0000639 if( !sqliteExprIsConstant(pE2) ){
drhda93d232003-03-31 02:12:46 +0000640 sqliteErrorMsg(pParse,
641 "right-hand side of IN operator must be constant");
drhfef52082000-06-06 01:50:43 +0000642 return 1;
643 }
drh4794b982000-06-06 13:54:14 +0000644 if( sqliteExprCheck(pParse, pE2, 0, 0) ){
645 return 1;
646 }
drhfef52082000-06-06 01:50:43 +0000647 }
648 iSet = pExpr->iTable = pParse->nSet++;
649 for(i=0; i<pExpr->pList->nExpr; i++){
650 Expr *pE2 = pExpr->pList->a[i].pExpr;
651 switch( pE2->op ){
652 case TK_FLOAT:
653 case TK_INTEGER:
654 case TK_STRING: {
drh99fcd712001-10-13 01:06:47 +0000655 int addr = sqliteVdbeAddOp(v, OP_SetInsert, iSet, 0);
drha76b5df2002-02-23 02:32:10 +0000656 assert( pE2->token.z );
drhfef52082000-06-06 01:50:43 +0000657 sqliteVdbeChangeP3(v, addr, pE2->token.z, pE2->token.n);
658 sqliteVdbeDequoteP3(v, addr);
659 break;
660 }
661 default: {
662 sqliteExprCode(pParse, pE2);
drh99fcd712001-10-13 01:06:47 +0000663 sqliteVdbeAddOp(v, OP_SetInsert, iSet, 0);
drhfef52082000-06-06 01:50:43 +0000664 break;
665 }
666 }
667 }
668 }
drhcfab11b2000-06-06 03:31:22 +0000669 break;
drhfef52082000-06-06 01:50:43 +0000670 }
671
drh19a775c2000-06-05 18:54:46 +0000672 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +0000673 /* This has to be a scalar SELECT. Generate code to put the
674 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +0000675 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +0000676 */
drh967e8b72000-06-21 13:59:10 +0000677 pExpr->iColumn = pParse->nMem++;
drh832508b2002-03-02 17:04:07 +0000678 if( sqliteSelect(pParse, pExpr->pSelect, SRT_Mem, pExpr->iColumn,0,0,0) ){
drh19a775c2000-06-05 18:54:46 +0000679 return 1;
680 }
681 break;
682 }
683
drhcce7d172000-05-31 15:34:51 +0000684 /* For all else, just recursively walk the tree */
685 default: {
drh4794b982000-06-06 13:54:14 +0000686 if( pExpr->pLeft
drh832508b2002-03-02 17:04:07 +0000687 && sqliteExprResolveIds(pParse, base, pTabList, pEList, pExpr->pLeft) ){
drhcce7d172000-05-31 15:34:51 +0000688 return 1;
689 }
690 if( pExpr->pRight
drh832508b2002-03-02 17:04:07 +0000691 && sqliteExprResolveIds(pParse, base, pTabList, pEList, pExpr->pRight) ){
drhcce7d172000-05-31 15:34:51 +0000692 return 1;
693 }
694 if( pExpr->pList ){
695 int i;
696 ExprList *pList = pExpr->pList;
697 for(i=0; i<pList->nExpr; i++){
drh832508b2002-03-02 17:04:07 +0000698 Expr *pArg = pList->a[i].pExpr;
699 if( sqliteExprResolveIds(pParse, base, pTabList, pEList, pArg) ){
drhcce7d172000-05-31 15:34:51 +0000700 return 1;
701 }
702 }
703 }
704 }
705 }
706 return 0;
707}
708
drhcce7d172000-05-31 15:34:51 +0000709/*
drh4b59ab52002-08-24 18:24:51 +0000710** pExpr is a node that defines a function of some kind. It might
711** be a syntactic function like "count(x)" or it might be a function
712** that implements an operator, like "a LIKE b".
713**
714** This routine makes *pzName point to the name of the function and
715** *pnName hold the number of characters in the function name.
716*/
717static void getFunctionName(Expr *pExpr, const char **pzName, int *pnName){
718 switch( pExpr->op ){
719 case TK_FUNCTION: {
720 *pzName = pExpr->token.z;
drh6977fea2002-10-22 23:38:04 +0000721 *pnName = pExpr->token.n;
drh4b59ab52002-08-24 18:24:51 +0000722 break;
723 }
724 case TK_LIKE: {
725 *pzName = "like";
726 *pnName = 4;
727 break;
728 }
729 case TK_GLOB: {
730 *pzName = "glob";
731 *pnName = 4;
732 break;
733 }
734 default: {
735 *pzName = "can't happen";
736 *pnName = 12;
737 break;
738 }
739 }
740}
741
742/*
drhcce7d172000-05-31 15:34:51 +0000743** Error check the functions in an expression. Make sure all
744** function names are recognized and all functions have the correct
745** number of arguments. Leave an error message in pParse->zErrMsg
746** if anything is amiss. Return the number of errors.
747**
748** if pIsAgg is not null and this expression is an aggregate function
749** (like count(*) or max(value)) then write a 1 into *pIsAgg.
750*/
751int sqliteExprCheck(Parse *pParse, Expr *pExpr, int allowAgg, int *pIsAgg){
752 int nErr = 0;
753 if( pExpr==0 ) return 0;
drhcce7d172000-05-31 15:34:51 +0000754 switch( pExpr->op ){
drh4b59ab52002-08-24 18:24:51 +0000755 case TK_GLOB:
756 case TK_LIKE:
drhcce7d172000-05-31 15:34:51 +0000757 case TK_FUNCTION: {
drhc9b84a12002-06-20 11:36:48 +0000758 int n = pExpr->pList ? pExpr->pList->nExpr : 0; /* Number of arguments */
759 int no_such_func = 0; /* True if no such function exists */
760 int is_type_of = 0; /* True if is the special TypeOf() function */
761 int wrong_num_args = 0; /* True if wrong number of arguments */
762 int is_agg = 0; /* True if is an aggregate function */
drhcce7d172000-05-31 15:34:51 +0000763 int i;
drh4b59ab52002-08-24 18:24:51 +0000764 int nId; /* Number of characters in function name */
765 const char *zId; /* The function name. */
drh0bce8352002-02-28 00:41:10 +0000766 FuncDef *pDef;
767
drh4b59ab52002-08-24 18:24:51 +0000768 getFunctionName(pExpr, &zId, &nId);
769 pDef = sqliteFindFunction(pParse->db, zId, nId, n, 0);
drh0bce8352002-02-28 00:41:10 +0000770 if( pDef==0 ){
drh4b59ab52002-08-24 18:24:51 +0000771 pDef = sqliteFindFunction(pParse->db, zId, nId, -1, 0);
drh0bce8352002-02-28 00:41:10 +0000772 if( pDef==0 ){
drh4b59ab52002-08-24 18:24:51 +0000773 if( n==1 && nId==6 && sqliteStrNICmp(zId, "typeof", 6)==0 ){
drhc9b84a12002-06-20 11:36:48 +0000774 is_type_of = 1;
775 }else {
776 no_such_func = 1;
777 }
drh0bce8352002-02-28 00:41:10 +0000778 }else{
779 wrong_num_args = 1;
drhcce7d172000-05-31 15:34:51 +0000780 }
drh0bce8352002-02-28 00:41:10 +0000781 }else{
782 is_agg = pDef->xFunc==0;
drhcce7d172000-05-31 15:34:51 +0000783 }
drh8e0a2f92002-02-23 23:45:45 +0000784 if( is_agg && !allowAgg ){
785 sqliteSetNString(&pParse->zErrMsg, "misuse of aggregate function ", -1,
drh4b59ab52002-08-24 18:24:51 +0000786 zId, nId, "()", 2, 0);
drh8e0a2f92002-02-23 23:45:45 +0000787 pParse->nErr++;
788 nErr++;
789 is_agg = 0;
790 }else if( no_such_func ){
drh4b59ab52002-08-24 18:24:51 +0000791 sqliteSetNString(&pParse->zErrMsg, "no such function: ", -1, zId,nId,0);
drhcce7d172000-05-31 15:34:51 +0000792 pParse->nErr++;
793 nErr++;
drh8e0a2f92002-02-23 23:45:45 +0000794 }else if( wrong_num_args ){
795 sqliteSetNString(&pParse->zErrMsg,
drh4b59ab52002-08-24 18:24:51 +0000796 "wrong number of arguments to function ", -1, zId, nId, "()", 2, 0);
drh8e0a2f92002-02-23 23:45:45 +0000797 pParse->nErr++;
798 nErr++;
drhcce7d172000-05-31 15:34:51 +0000799 }
drh22827922000-06-06 17:27:05 +0000800 if( is_agg ) pExpr->op = TK_AGG_FUNCTION;
drhcce7d172000-05-31 15:34:51 +0000801 if( is_agg && pIsAgg ) *pIsAgg = 1;
802 for(i=0; nErr==0 && i<n; i++){
drh4cfa7932000-06-08 15:10:46 +0000803 nErr = sqliteExprCheck(pParse, pExpr->pList->a[i].pExpr,
804 allowAgg && !is_agg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000805 }
drhc9b84a12002-06-20 11:36:48 +0000806 if( pDef==0 ){
807 if( is_type_of ){
808 pExpr->op = TK_STRING;
809 if( sqliteExprType(pExpr->pList->a[0].pExpr)==SQLITE_SO_NUM ){
810 pExpr->token.z = "numeric";
811 pExpr->token.n = 7;
812 }else{
813 pExpr->token.z = "text";
814 pExpr->token.n = 4;
815 }
816 }
817 }else if( pDef->dataType>=0 ){
818 if( pDef->dataType<n ){
819 pExpr->dataType =
820 sqliteExprType(pExpr->pList->a[pDef->dataType].pExpr);
821 }else{
822 pExpr->dataType = SQLITE_SO_NUM;
823 }
824 }else if( pDef->dataType==SQLITE_ARGS ){
825 pDef->dataType = SQLITE_SO_TEXT;
826 for(i=0; i<n; i++){
827 if( sqliteExprType(pExpr->pList->a[i].pExpr)==SQLITE_SO_NUM ){
828 pExpr->dataType = SQLITE_SO_NUM;
829 break;
830 }
831 }
832 }else if( pDef->dataType==SQLITE_NUMERIC ){
833 pExpr->dataType = SQLITE_SO_NUM;
834 }else{
835 pExpr->dataType = SQLITE_SO_TEXT;
836 }
drhcce7d172000-05-31 15:34:51 +0000837 }
838 default: {
839 if( pExpr->pLeft ){
drh22827922000-06-06 17:27:05 +0000840 nErr = sqliteExprCheck(pParse, pExpr->pLeft, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000841 }
842 if( nErr==0 && pExpr->pRight ){
drh22827922000-06-06 17:27:05 +0000843 nErr = sqliteExprCheck(pParse, pExpr->pRight, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000844 }
drhfef52082000-06-06 01:50:43 +0000845 if( nErr==0 && pExpr->pList ){
846 int n = pExpr->pList->nExpr;
847 int i;
848 for(i=0; nErr==0 && i<n; i++){
drh22827922000-06-06 17:27:05 +0000849 Expr *pE2 = pExpr->pList->a[i].pExpr;
850 nErr = sqliteExprCheck(pParse, pE2, allowAgg, pIsAgg);
drhfef52082000-06-06 01:50:43 +0000851 }
852 }
drhcce7d172000-05-31 15:34:51 +0000853 break;
854 }
855 }
856 return nErr;
857}
858
859/*
drhc9b84a12002-06-20 11:36:48 +0000860** Return either SQLITE_SO_NUM or SQLITE_SO_TEXT to indicate whether the
861** given expression should sort as numeric values or as text.
862**
863** The sqliteExprResolveIds() and sqliteExprCheck() routines must have
864** both been called on the expression before it is passed to this routine.
865*/
866int sqliteExprType(Expr *p){
867 if( p==0 ) return SQLITE_SO_NUM;
868 while( p ) switch( p->op ){
869 case TK_PLUS:
870 case TK_MINUS:
871 case TK_STAR:
872 case TK_SLASH:
873 case TK_AND:
874 case TK_OR:
875 case TK_ISNULL:
876 case TK_NOTNULL:
877 case TK_NOT:
878 case TK_UMINUS:
drh4b59ab52002-08-24 18:24:51 +0000879 case TK_UPLUS:
drhc9b84a12002-06-20 11:36:48 +0000880 case TK_BITAND:
881 case TK_BITOR:
882 case TK_BITNOT:
883 case TK_LSHIFT:
884 case TK_RSHIFT:
885 case TK_REM:
886 case TK_INTEGER:
887 case TK_FLOAT:
888 case TK_IN:
889 case TK_BETWEEN:
drh4b59ab52002-08-24 18:24:51 +0000890 case TK_GLOB:
891 case TK_LIKE:
drhc9b84a12002-06-20 11:36:48 +0000892 return SQLITE_SO_NUM;
893
894 case TK_STRING:
895 case TK_NULL:
896 case TK_CONCAT:
897 return SQLITE_SO_TEXT;
898
899 case TK_LT:
900 case TK_LE:
901 case TK_GT:
902 case TK_GE:
903 case TK_NE:
904 case TK_EQ:
905 if( sqliteExprType(p->pLeft)==SQLITE_SO_NUM ){
906 return SQLITE_SO_NUM;
907 }
908 p = p->pRight;
909 break;
910
911 case TK_AS:
912 p = p->pLeft;
913 break;
914
915 case TK_COLUMN:
916 case TK_FUNCTION:
917 case TK_AGG_FUNCTION:
918 return p->dataType;
919
920 case TK_SELECT:
921 assert( p->pSelect );
922 assert( p->pSelect->pEList );
923 assert( p->pSelect->pEList->nExpr>0 );
924 p = p->pSelect->pEList->a[0].pExpr;
925 break;
926
drhb1363202002-06-26 02:45:03 +0000927 case TK_CASE: {
928 if( p->pRight && sqliteExprType(p->pRight)==SQLITE_SO_NUM ){
929 return SQLITE_SO_NUM;
930 }
931 if( p->pList ){
932 int i;
933 ExprList *pList = p->pList;
934 for(i=1; i<pList->nExpr; i+=2){
935 if( sqliteExprType(pList->a[i].pExpr)==SQLITE_SO_NUM ){
936 return SQLITE_SO_NUM;
937 }
938 }
939 }
940 return SQLITE_SO_TEXT;
941 }
942
drhc9b84a12002-06-20 11:36:48 +0000943 default:
944 assert( p->op==TK_ABORT ); /* Can't Happen */
945 break;
946 }
947 return SQLITE_SO_NUM;
948}
949
950/*
drhcce7d172000-05-31 15:34:51 +0000951** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +0000952** expression and leave the result on the top of stack.
drhcce7d172000-05-31 15:34:51 +0000953*/
954void sqliteExprCode(Parse *pParse, Expr *pExpr){
955 Vdbe *v = pParse->pVdbe;
956 int op;
drhdaffd0e2001-04-11 14:28:42 +0000957 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +0000958 switch( pExpr->op ){
959 case TK_PLUS: op = OP_Add; break;
960 case TK_MINUS: op = OP_Subtract; break;
961 case TK_STAR: op = OP_Multiply; break;
962 case TK_SLASH: op = OP_Divide; break;
963 case TK_AND: op = OP_And; break;
964 case TK_OR: op = OP_Or; break;
965 case TK_LT: op = OP_Lt; break;
966 case TK_LE: op = OP_Le; break;
967 case TK_GT: op = OP_Gt; break;
968 case TK_GE: op = OP_Ge; break;
969 case TK_NE: op = OP_Ne; break;
970 case TK_EQ: op = OP_Eq; break;
drhcce7d172000-05-31 15:34:51 +0000971 case TK_ISNULL: op = OP_IsNull; break;
972 case TK_NOTNULL: op = OP_NotNull; break;
973 case TK_NOT: op = OP_Not; break;
974 case TK_UMINUS: op = OP_Negative; break;
drhbf4133c2001-10-13 02:59:08 +0000975 case TK_BITAND: op = OP_BitAnd; break;
976 case TK_BITOR: op = OP_BitOr; break;
977 case TK_BITNOT: op = OP_BitNot; break;
978 case TK_LSHIFT: op = OP_ShiftLeft; break;
979 case TK_RSHIFT: op = OP_ShiftRight; break;
980 case TK_REM: op = OP_Remainder; break;
drhcce7d172000-05-31 15:34:51 +0000981 default: break;
982 }
983 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +0000984 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +0000985 if( pParse->useAgg ){
drh99fcd712001-10-13 01:06:47 +0000986 sqliteVdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drhc4a3c772001-04-04 11:48:57 +0000987 }else if( pExpr->iColumn>=0 ){
drh99fcd712001-10-13 01:06:47 +0000988 sqliteVdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn);
drhc4a3c772001-04-04 11:48:57 +0000989 }else{
drh99fcd712001-10-13 01:06:47 +0000990 sqliteVdbeAddOp(v, OP_Recno, pExpr->iTable, 0);
drh22827922000-06-06 17:27:05 +0000991 }
drhcce7d172000-05-31 15:34:51 +0000992 break;
993 }
994 case TK_INTEGER: {
drhd9e30932002-06-09 01:16:01 +0000995 int iVal = atoi(pExpr->token.z);
996 char zBuf[30];
997 sprintf(zBuf,"%d",iVal);
998 if( strlen(zBuf)!=pExpr->token.n
999 || strncmp(pExpr->token.z,zBuf,pExpr->token.n)!=0 ){
1000 /* If the integer value cannot be represented exactly in 32 bits,
1001 ** then code it as a string instead. */
1002 sqliteVdbeAddOp(v, OP_String, 0, 0);
1003 }else{
1004 sqliteVdbeAddOp(v, OP_Integer, iVal, 0);
1005 }
drhe6840902002-03-06 03:08:25 +00001006 sqliteVdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
1007 break;
1008 }
1009 case TK_FLOAT: {
drh7a7c7392001-11-24 00:31:46 +00001010 sqliteVdbeAddOp(v, OP_String, 0, 0);
drha76b5df2002-02-23 02:32:10 +00001011 assert( pExpr->token.z );
drh7a7c7392001-11-24 00:31:46 +00001012 sqliteVdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
drhcce7d172000-05-31 15:34:51 +00001013 break;
1014 }
drhcce7d172000-05-31 15:34:51 +00001015 case TK_STRING: {
drh99fcd712001-10-13 01:06:47 +00001016 int addr = sqliteVdbeAddOp(v, OP_String, 0, 0);
drha76b5df2002-02-23 02:32:10 +00001017 assert( pExpr->token.z );
drhcce7d172000-05-31 15:34:51 +00001018 sqliteVdbeChangeP3(v, addr, pExpr->token.z, pExpr->token.n);
1019 sqliteVdbeDequoteP3(v, addr);
1020 break;
1021 }
1022 case TK_NULL: {
drh99fcd712001-10-13 01:06:47 +00001023 sqliteVdbeAddOp(v, OP_String, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001024 break;
1025 }
drhc9b84a12002-06-20 11:36:48 +00001026 case TK_LT:
1027 case TK_LE:
1028 case TK_GT:
1029 case TK_GE:
1030 case TK_NE:
1031 case TK_EQ: {
drh491791a2002-07-18 00:34:09 +00001032 if( pParse->db->file_format>=4 && sqliteExprType(pExpr)==SQLITE_SO_TEXT ){
drhc9b84a12002-06-20 11:36:48 +00001033 op += 6; /* Convert numeric opcodes to text opcodes */
1034 }
1035 /* Fall through into the next case */
1036 }
drhcce7d172000-05-31 15:34:51 +00001037 case TK_AND:
1038 case TK_OR:
1039 case TK_PLUS:
1040 case TK_STAR:
1041 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00001042 case TK_REM:
1043 case TK_BITAND:
1044 case TK_BITOR:
drhc9b84a12002-06-20 11:36:48 +00001045 case TK_SLASH: {
drhcce7d172000-05-31 15:34:51 +00001046 sqliteExprCode(pParse, pExpr->pLeft);
1047 sqliteExprCode(pParse, pExpr->pRight);
drh99fcd712001-10-13 01:06:47 +00001048 sqliteVdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001049 break;
1050 }
drhbf4133c2001-10-13 02:59:08 +00001051 case TK_LSHIFT:
1052 case TK_RSHIFT: {
1053 sqliteExprCode(pParse, pExpr->pRight);
1054 sqliteExprCode(pParse, pExpr->pLeft);
1055 sqliteVdbeAddOp(v, op, 0, 0);
1056 break;
1057 }
drh00400772000-06-16 20:51:26 +00001058 case TK_CONCAT: {
1059 sqliteExprCode(pParse, pExpr->pLeft);
1060 sqliteExprCode(pParse, pExpr->pRight);
drh99fcd712001-10-13 01:06:47 +00001061 sqliteVdbeAddOp(v, OP_Concat, 2, 0);
drh00400772000-06-16 20:51:26 +00001062 break;
1063 }
drh4b59ab52002-08-24 18:24:51 +00001064 case TK_UPLUS: {
1065 Expr *pLeft = pExpr->pLeft;
1066 if( pLeft && pLeft->op==TK_INTEGER ){
1067 sqliteVdbeAddOp(v, OP_Integer, atoi(pLeft->token.z), 0);
1068 sqliteVdbeChangeP3(v, -1, pLeft->token.z, pLeft->token.n);
1069 }else if( pLeft && pLeft->op==TK_FLOAT ){
1070 sqliteVdbeAddOp(v, OP_String, 0, 0);
1071 sqliteVdbeChangeP3(v, -1, pLeft->token.z, pLeft->token.n);
1072 }else{
1073 sqliteExprCode(pParse, pExpr->pLeft);
1074 }
1075 break;
1076 }
drhcce7d172000-05-31 15:34:51 +00001077 case TK_UMINUS: {
drh6e142f52000-06-08 13:36:40 +00001078 assert( pExpr->pLeft );
drh7a7c7392001-11-24 00:31:46 +00001079 if( pExpr->pLeft->op==TK_FLOAT || pExpr->pLeft->op==TK_INTEGER ){
drh6e142f52000-06-08 13:36:40 +00001080 Token *p = &pExpr->pLeft->token;
1081 char *z = sqliteMalloc( p->n + 2 );
1082 sprintf(z, "-%.*s", p->n, p->z);
drhe6840902002-03-06 03:08:25 +00001083 if( pExpr->pLeft->op==TK_INTEGER ){
1084 sqliteVdbeAddOp(v, OP_Integer, atoi(z), 0);
1085 }else{
1086 sqliteVdbeAddOp(v, OP_String, 0, 0);
1087 }
drh99fcd712001-10-13 01:06:47 +00001088 sqliteVdbeChangeP3(v, -1, z, p->n+1);
drh6e142f52000-06-08 13:36:40 +00001089 sqliteFree(z);
1090 break;
1091 }
drh1ccde152000-06-17 13:12:39 +00001092 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +00001093 }
drhbf4133c2001-10-13 02:59:08 +00001094 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00001095 case TK_NOT: {
drhcce7d172000-05-31 15:34:51 +00001096 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +00001097 sqliteVdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001098 break;
1099 }
1100 case TK_ISNULL:
1101 case TK_NOTNULL: {
1102 int dest;
drh99fcd712001-10-13 01:06:47 +00001103 sqliteVdbeAddOp(v, OP_Integer, 1, 0);
drhcce7d172000-05-31 15:34:51 +00001104 sqliteExprCode(pParse, pExpr->pLeft);
1105 dest = sqliteVdbeCurrentAddr(v) + 2;
drhf5905aa2002-05-26 20:54:33 +00001106 sqliteVdbeAddOp(v, op, 1, dest);
drh99fcd712001-10-13 01:06:47 +00001107 sqliteVdbeAddOp(v, OP_AddImm, -1, 0);
drhcce7d172000-05-31 15:34:51 +00001108 break;
1109 }
drh22827922000-06-06 17:27:05 +00001110 case TK_AGG_FUNCTION: {
drh99fcd712001-10-13 01:06:47 +00001111 sqliteVdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drh22827922000-06-06 17:27:05 +00001112 break;
1113 }
drh4b59ab52002-08-24 18:24:51 +00001114 case TK_GLOB:
1115 case TK_LIKE:
drhcce7d172000-05-31 15:34:51 +00001116 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00001117 int i;
1118 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00001119 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00001120 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00001121 int nId;
1122 const char *zId;
1123 getFunctionName(pExpr, &zId, &nId);
1124 pDef = sqliteFindFunction(pParse->db, zId, nId, nExpr, 0);
drh0bce8352002-02-28 00:41:10 +00001125 assert( pDef!=0 );
drh89425d52002-02-28 03:04:48 +00001126 for(i=0; i<nExpr; i++){
drh0bce8352002-02-28 00:41:10 +00001127 sqliteExprCode(pParse, pList->a[i].pExpr);
drhcce7d172000-05-31 15:34:51 +00001128 }
drh89425d52002-02-28 03:04:48 +00001129 sqliteVdbeAddOp(v, OP_Function, nExpr, 0);
drh0bce8352002-02-28 00:41:10 +00001130 sqliteVdbeChangeP3(v, -1, (char*)pDef, P3_POINTER);
drhcce7d172000-05-31 15:34:51 +00001131 break;
1132 }
drh19a775c2000-06-05 18:54:46 +00001133 case TK_SELECT: {
drh99fcd712001-10-13 01:06:47 +00001134 sqliteVdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drh19a775c2000-06-05 18:54:46 +00001135 break;
1136 }
drhfef52082000-06-06 01:50:43 +00001137 case TK_IN: {
1138 int addr;
drh99fcd712001-10-13 01:06:47 +00001139 sqliteVdbeAddOp(v, OP_Integer, 1, 0);
drhfef52082000-06-06 01:50:43 +00001140 sqliteExprCode(pParse, pExpr->pLeft);
1141 addr = sqliteVdbeCurrentAddr(v);
drhf5905aa2002-05-26 20:54:33 +00001142 sqliteVdbeAddOp(v, OP_NotNull, -1, addr+4);
1143 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1144 sqliteVdbeAddOp(v, OP_String, 0, 0);
1145 sqliteVdbeAddOp(v, OP_Goto, 0, addr+6);
drhfef52082000-06-06 01:50:43 +00001146 if( pExpr->pSelect ){
drhf5905aa2002-05-26 20:54:33 +00001147 sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, addr+6);
drhfef52082000-06-06 01:50:43 +00001148 }else{
drhf5905aa2002-05-26 20:54:33 +00001149 sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, addr+6);
drhfef52082000-06-06 01:50:43 +00001150 }
drh99fcd712001-10-13 01:06:47 +00001151 sqliteVdbeAddOp(v, OP_AddImm, -1, 0);
drhfef52082000-06-06 01:50:43 +00001152 break;
1153 }
1154 case TK_BETWEEN: {
drhf5905aa2002-05-26 20:54:33 +00001155 sqliteExprCode(pParse, pExpr->pLeft);
1156 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
1157 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
1158 sqliteVdbeAddOp(v, OP_Ge, 0, 0);
1159 sqliteVdbeAddOp(v, OP_Pull, 1, 0);
1160 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
1161 sqliteVdbeAddOp(v, OP_Le, 0, 0);
1162 sqliteVdbeAddOp(v, OP_And, 0, 0);
drhfef52082000-06-06 01:50:43 +00001163 break;
1164 }
drha2e00042002-01-22 03:13:42 +00001165 case TK_AS: {
1166 sqliteExprCode(pParse, pExpr->pLeft);
1167 break;
1168 }
drh17a7f8d2002-03-24 13:13:27 +00001169 case TK_CASE: {
1170 int expr_end_label;
drhf5905aa2002-05-26 20:54:33 +00001171 int jumpInst;
1172 int addr;
1173 int nExpr;
drh17a7f8d2002-03-24 13:13:27 +00001174 int i;
1175
1176 assert(pExpr->pList);
1177 assert((pExpr->pList->nExpr % 2) == 0);
1178 assert(pExpr->pList->nExpr > 0);
drhf5905aa2002-05-26 20:54:33 +00001179 nExpr = pExpr->pList->nExpr;
1180 expr_end_label = sqliteVdbeMakeLabel(v);
drh17a7f8d2002-03-24 13:13:27 +00001181 if( pExpr->pLeft ){
1182 sqliteExprCode(pParse, pExpr->pLeft);
1183 }
drhf5905aa2002-05-26 20:54:33 +00001184 for(i=0; i<nExpr; i=i+2){
1185 sqliteExprCode(pParse, pExpr->pList->a[i].pExpr);
drh17a7f8d2002-03-24 13:13:27 +00001186 if( pExpr->pLeft ){
drhf5905aa2002-05-26 20:54:33 +00001187 sqliteVdbeAddOp(v, OP_Dup, 1, 1);
drhf570f012002-05-31 15:51:25 +00001188 jumpInst = sqliteVdbeAddOp(v, OP_Ne, 1, 0);
1189 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001190 }else{
drhf570f012002-05-31 15:51:25 +00001191 jumpInst = sqliteVdbeAddOp(v, OP_IfNot, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001192 }
1193 sqliteExprCode(pParse, pExpr->pList->a[i+1].pExpr);
drhf5905aa2002-05-26 20:54:33 +00001194 sqliteVdbeAddOp(v, OP_Goto, 0, expr_end_label);
drhf5905aa2002-05-26 20:54:33 +00001195 addr = sqliteVdbeCurrentAddr(v);
1196 sqliteVdbeChangeP2(v, jumpInst, addr);
drh17a7f8d2002-03-24 13:13:27 +00001197 }
drhf570f012002-05-31 15:51:25 +00001198 if( pExpr->pLeft ){
1199 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1200 }
drh17a7f8d2002-03-24 13:13:27 +00001201 if( pExpr->pRight ){
1202 sqliteExprCode(pParse, pExpr->pRight);
1203 }else{
drhf5905aa2002-05-26 20:54:33 +00001204 sqliteVdbeAddOp(v, OP_String, 0, 0);
drh17a7f8d2002-03-24 13:13:27 +00001205 }
drhf5905aa2002-05-26 20:54:33 +00001206 sqliteVdbeResolveLabel(v, expr_end_label);
danielk19776f349032002-06-11 02:25:40 +00001207 break;
1208 }
1209 case TK_RAISE: {
1210 if( !pParse->trigStack ){
drhda93d232003-03-31 02:12:46 +00001211 sqliteErrorMsg(pParse,
1212 "RAISE() may only be used within a trigger-program");
danielk19776f349032002-06-11 02:25:40 +00001213 pParse->nErr++;
1214 return;
1215 }
1216 if( pExpr->iColumn == OE_Rollback ||
1217 pExpr->iColumn == OE_Abort ||
1218 pExpr->iColumn == OE_Fail ){
1219 char * msg = sqliteStrNDup(pExpr->token.z, pExpr->token.n);
1220 sqliteVdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn);
1221 sqliteDequote(msg);
1222 sqliteVdbeChangeP3(v, -1, msg, 0);
1223 sqliteFree(msg);
1224 } else {
1225 assert( pExpr->iColumn == OE_Ignore );
1226 sqliteVdbeAddOp(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
drh483750b2003-01-29 18:46:51 +00001227 sqliteVdbeChangeP3(v, -1, "(IGNORE jump)", 0);
danielk19776f349032002-06-11 02:25:40 +00001228 }
drh17a7f8d2002-03-24 13:13:27 +00001229 }
1230 break;
drhcce7d172000-05-31 15:34:51 +00001231 }
drhcce7d172000-05-31 15:34:51 +00001232}
1233
1234/*
1235** Generate code for a boolean expression such that a jump is made
1236** to the label "dest" if the expression is true but execution
1237** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00001238**
1239** If the expression evaluates to NULL (neither true nor false), then
1240** take the jump if the jumpIfNull flag is true.
drhcce7d172000-05-31 15:34:51 +00001241*/
drhf5905aa2002-05-26 20:54:33 +00001242void sqliteExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001243 Vdbe *v = pParse->pVdbe;
1244 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001245 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001246 switch( pExpr->op ){
1247 case TK_LT: op = OP_Lt; break;
1248 case TK_LE: op = OP_Le; break;
1249 case TK_GT: op = OP_Gt; break;
1250 case TK_GE: op = OP_Ge; break;
1251 case TK_NE: op = OP_Ne; break;
1252 case TK_EQ: op = OP_Eq; break;
drhcce7d172000-05-31 15:34:51 +00001253 case TK_ISNULL: op = OP_IsNull; break;
1254 case TK_NOTNULL: op = OP_NotNull; break;
1255 default: break;
1256 }
1257 switch( pExpr->op ){
1258 case TK_AND: {
1259 int d2 = sqliteVdbeMakeLabel(v);
drhf5905aa2002-05-26 20:54:33 +00001260 sqliteExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
1261 sqliteExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001262 sqliteVdbeResolveLabel(v, d2);
1263 break;
1264 }
1265 case TK_OR: {
drhf5905aa2002-05-26 20:54:33 +00001266 sqliteExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
1267 sqliteExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001268 break;
1269 }
1270 case TK_NOT: {
drhf5905aa2002-05-26 20:54:33 +00001271 sqliteExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001272 break;
1273 }
1274 case TK_LT:
1275 case TK_LE:
1276 case TK_GT:
1277 case TK_GE:
1278 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00001279 case TK_EQ: {
drhcce7d172000-05-31 15:34:51 +00001280 sqliteExprCode(pParse, pExpr->pLeft);
1281 sqliteExprCode(pParse, pExpr->pRight);
drh491791a2002-07-18 00:34:09 +00001282 if( pParse->db->file_format>=4 && sqliteExprType(pExpr)==SQLITE_SO_TEXT ){
drhc9b84a12002-06-20 11:36:48 +00001283 op += 6; /* Convert numeric opcodes to text opcodes */
1284 }
drhf5905aa2002-05-26 20:54:33 +00001285 sqliteVdbeAddOp(v, op, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001286 break;
1287 }
1288 case TK_ISNULL:
1289 case TK_NOTNULL: {
1290 sqliteExprCode(pParse, pExpr->pLeft);
drhf5905aa2002-05-26 20:54:33 +00001291 sqliteVdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001292 break;
1293 }
drhfef52082000-06-06 01:50:43 +00001294 case TK_IN: {
drhf5905aa2002-05-26 20:54:33 +00001295 int addr;
drhcfab11b2000-06-06 03:31:22 +00001296 sqliteExprCode(pParse, pExpr->pLeft);
drhf5905aa2002-05-26 20:54:33 +00001297 addr = sqliteVdbeCurrentAddr(v);
1298 sqliteVdbeAddOp(v, OP_NotNull, -1, addr+3);
1299 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1300 sqliteVdbeAddOp(v, OP_Goto, 0, jumpIfNull ? dest : addr+4);
drhfef52082000-06-06 01:50:43 +00001301 if( pExpr->pSelect ){
drh99fcd712001-10-13 01:06:47 +00001302 sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001303 }else{
drh99fcd712001-10-13 01:06:47 +00001304 sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001305 }
1306 break;
1307 }
1308 case TK_BETWEEN: {
drhf5905aa2002-05-26 20:54:33 +00001309 int addr;
drhfef52082000-06-06 01:50:43 +00001310 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +00001311 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
drhfef52082000-06-06 01:50:43 +00001312 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
drhf5905aa2002-05-26 20:54:33 +00001313 addr = sqliteVdbeAddOp(v, OP_Lt, !jumpIfNull, 0);
drhfef52082000-06-06 01:50:43 +00001314 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
drhf5905aa2002-05-26 20:54:33 +00001315 sqliteVdbeAddOp(v, OP_Le, jumpIfNull, dest);
drh99fcd712001-10-13 01:06:47 +00001316 sqliteVdbeAddOp(v, OP_Integer, 0, 0);
drhf5905aa2002-05-26 20:54:33 +00001317 sqliteVdbeChangeP2(v, addr, sqliteVdbeCurrentAddr(v));
drh99fcd712001-10-13 01:06:47 +00001318 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00001319 break;
1320 }
drhcce7d172000-05-31 15:34:51 +00001321 default: {
1322 sqliteExprCode(pParse, pExpr);
drhf5905aa2002-05-26 20:54:33 +00001323 sqliteVdbeAddOp(v, OP_If, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001324 break;
1325 }
1326 }
1327}
1328
1329/*
drh66b89c82000-11-28 20:47:17 +00001330** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00001331** to the label "dest" if the expression is false but execution
1332** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00001333**
1334** If the expression evaluates to NULL (neither true nor false) then
1335** jump if jumpIfNull is true or fall through if jumpIfNull is false.
drhcce7d172000-05-31 15:34:51 +00001336*/
drhf5905aa2002-05-26 20:54:33 +00001337void sqliteExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001338 Vdbe *v = pParse->pVdbe;
1339 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001340 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001341 switch( pExpr->op ){
1342 case TK_LT: op = OP_Ge; break;
1343 case TK_LE: op = OP_Gt; break;
1344 case TK_GT: op = OP_Le; break;
1345 case TK_GE: op = OP_Lt; break;
1346 case TK_NE: op = OP_Eq; break;
1347 case TK_EQ: op = OP_Ne; break;
drhcce7d172000-05-31 15:34:51 +00001348 case TK_ISNULL: op = OP_NotNull; break;
1349 case TK_NOTNULL: op = OP_IsNull; break;
1350 default: break;
1351 }
1352 switch( pExpr->op ){
1353 case TK_AND: {
drhf5905aa2002-05-26 20:54:33 +00001354 sqliteExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
1355 sqliteExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001356 break;
1357 }
1358 case TK_OR: {
1359 int d2 = sqliteVdbeMakeLabel(v);
drhf5905aa2002-05-26 20:54:33 +00001360 sqliteExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
1361 sqliteExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001362 sqliteVdbeResolveLabel(v, d2);
1363 break;
1364 }
1365 case TK_NOT: {
drhf5905aa2002-05-26 20:54:33 +00001366 sqliteExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001367 break;
1368 }
1369 case TK_LT:
1370 case TK_LE:
1371 case TK_GT:
1372 case TK_GE:
1373 case TK_NE:
1374 case TK_EQ: {
drh491791a2002-07-18 00:34:09 +00001375 if( pParse->db->file_format>=4 && sqliteExprType(pExpr)==SQLITE_SO_TEXT ){
drh8f619cc2002-09-08 00:04:50 +00001376 /* Convert numeric comparison opcodes into text comparison opcodes.
1377 ** This step depends on the fact that the text comparision opcodes are
1378 ** always 6 greater than their corresponding numeric comparison
1379 ** opcodes.
1380 */
1381 assert( OP_Eq+6 == OP_StrEq );
1382 op += 6;
drhc9b84a12002-06-20 11:36:48 +00001383 }
drhcce7d172000-05-31 15:34:51 +00001384 sqliteExprCode(pParse, pExpr->pLeft);
1385 sqliteExprCode(pParse, pExpr->pRight);
drhf5905aa2002-05-26 20:54:33 +00001386 sqliteVdbeAddOp(v, op, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001387 break;
1388 }
drhcce7d172000-05-31 15:34:51 +00001389 case TK_ISNULL:
1390 case TK_NOTNULL: {
1391 sqliteExprCode(pParse, pExpr->pLeft);
drhf5905aa2002-05-26 20:54:33 +00001392 sqliteVdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001393 break;
1394 }
drhfef52082000-06-06 01:50:43 +00001395 case TK_IN: {
drhf5905aa2002-05-26 20:54:33 +00001396 int addr;
drhcfab11b2000-06-06 03:31:22 +00001397 sqliteExprCode(pParse, pExpr->pLeft);
drhf5905aa2002-05-26 20:54:33 +00001398 addr = sqliteVdbeCurrentAddr(v);
1399 sqliteVdbeAddOp(v, OP_NotNull, -1, addr+3);
1400 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1401 sqliteVdbeAddOp(v, OP_Goto, 0, jumpIfNull ? dest : addr+4);
drhfef52082000-06-06 01:50:43 +00001402 if( pExpr->pSelect ){
drh99fcd712001-10-13 01:06:47 +00001403 sqliteVdbeAddOp(v, OP_NotFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001404 }else{
drh99fcd712001-10-13 01:06:47 +00001405 sqliteVdbeAddOp(v, OP_SetNotFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001406 }
1407 break;
1408 }
1409 case TK_BETWEEN: {
1410 int addr;
1411 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +00001412 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
drhfef52082000-06-06 01:50:43 +00001413 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
1414 addr = sqliteVdbeCurrentAddr(v);
drhf5905aa2002-05-26 20:54:33 +00001415 sqliteVdbeAddOp(v, OP_Ge, !jumpIfNull, addr+3);
drh99fcd712001-10-13 01:06:47 +00001416 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1417 sqliteVdbeAddOp(v, OP_Goto, 0, dest);
drhfef52082000-06-06 01:50:43 +00001418 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
drhf5905aa2002-05-26 20:54:33 +00001419 sqliteVdbeAddOp(v, OP_Gt, jumpIfNull, dest);
drhfef52082000-06-06 01:50:43 +00001420 break;
1421 }
drhcce7d172000-05-31 15:34:51 +00001422 default: {
1423 sqliteExprCode(pParse, pExpr);
drh461c2812002-05-30 02:35:11 +00001424 sqliteVdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001425 break;
1426 }
1427 }
1428}
drh22827922000-06-06 17:27:05 +00001429
1430/*
1431** Do a deep comparison of two expression trees. Return TRUE (non-zero)
1432** if they are identical and return FALSE if they differ in any way.
1433*/
drhd8bc7082000-06-07 23:51:50 +00001434int sqliteExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00001435 int i;
1436 if( pA==0 ){
1437 return pB==0;
1438 }else if( pB==0 ){
1439 return 0;
1440 }
1441 if( pA->op!=pB->op ) return 0;
drhd8bc7082000-06-07 23:51:50 +00001442 if( !sqliteExprCompare(pA->pLeft, pB->pLeft) ) return 0;
1443 if( !sqliteExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00001444 if( pA->pList ){
1445 if( pB->pList==0 ) return 0;
1446 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
1447 for(i=0; i<pA->pList->nExpr; i++){
drhd8bc7082000-06-07 23:51:50 +00001448 if( !sqliteExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00001449 return 0;
1450 }
1451 }
1452 }else if( pB->pList ){
1453 return 0;
1454 }
1455 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00001456 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drh22827922000-06-06 17:27:05 +00001457 if( pA->token.z ){
1458 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00001459 if( pB->token.n!=pA->token.n ) return 0;
1460 if( sqliteStrNICmp(pA->token.z, pB->token.z, pB->token.n)!=0 ) return 0;
drh22827922000-06-06 17:27:05 +00001461 }
1462 return 1;
1463}
1464
1465/*
1466** Add a new element to the pParse->aAgg[] array and return its index.
1467*/
1468static int appendAggInfo(Parse *pParse){
1469 if( (pParse->nAgg & 0x7)==0 ){
1470 int amt = pParse->nAgg + 8;
drh6d4abfb2001-10-22 02:58:08 +00001471 AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0]));
1472 if( aAgg==0 ){
drh22827922000-06-06 17:27:05 +00001473 return -1;
1474 }
drh6d4abfb2001-10-22 02:58:08 +00001475 pParse->aAgg = aAgg;
drh22827922000-06-06 17:27:05 +00001476 }
1477 memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0]));
1478 return pParse->nAgg++;
1479}
1480
1481/*
1482** Analyze the given expression looking for aggregate functions and
1483** for variables that need to be added to the pParse->aAgg[] array.
1484** Make additional entries to the pParse->aAgg[] array as necessary.
1485**
1486** This routine should only be called after the expression has been
1487** analyzed by sqliteExprResolveIds() and sqliteExprCheck().
1488**
1489** If errors are seen, leave an error message in zErrMsg and return
1490** the number of errors.
1491*/
1492int sqliteExprAnalyzeAggregates(Parse *pParse, Expr *pExpr){
1493 int i;
1494 AggExpr *aAgg;
1495 int nErr = 0;
1496
1497 if( pExpr==0 ) return 0;
1498 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00001499 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +00001500 aAgg = pParse->aAgg;
1501 for(i=0; i<pParse->nAgg; i++){
1502 if( aAgg[i].isAgg ) continue;
1503 if( aAgg[i].pExpr->iTable==pExpr->iTable
drh967e8b72000-06-21 13:59:10 +00001504 && aAgg[i].pExpr->iColumn==pExpr->iColumn ){
drh22827922000-06-06 17:27:05 +00001505 break;
1506 }
1507 }
1508 if( i>=pParse->nAgg ){
1509 i = appendAggInfo(pParse);
1510 if( i<0 ) return 1;
1511 pParse->aAgg[i].isAgg = 0;
1512 pParse->aAgg[i].pExpr = pExpr;
1513 }
drhaaf88722000-06-08 11:25:00 +00001514 pExpr->iAgg = i;
drh22827922000-06-06 17:27:05 +00001515 break;
1516 }
1517 case TK_AGG_FUNCTION: {
drh22827922000-06-06 17:27:05 +00001518 aAgg = pParse->aAgg;
1519 for(i=0; i<pParse->nAgg; i++){
1520 if( !aAgg[i].isAgg ) continue;
drhd8bc7082000-06-07 23:51:50 +00001521 if( sqliteExprCompare(aAgg[i].pExpr, pExpr) ){
drh22827922000-06-06 17:27:05 +00001522 break;
1523 }
1524 }
1525 if( i>=pParse->nAgg ){
1526 i = appendAggInfo(pParse);
1527 if( i<0 ) return 1;
1528 pParse->aAgg[i].isAgg = 1;
1529 pParse->aAgg[i].pExpr = pExpr;
drh0bce8352002-02-28 00:41:10 +00001530 pParse->aAgg[i].pFunc = sqliteFindFunction(pParse->db,
drh6977fea2002-10-22 23:38:04 +00001531 pExpr->token.z, pExpr->token.n,
drhf55f25f2002-02-28 01:46:11 +00001532 pExpr->pList ? pExpr->pList->nExpr : 0, 0);
drh22827922000-06-06 17:27:05 +00001533 }
1534 pExpr->iAgg = i;
1535 break;
1536 }
1537 default: {
1538 if( pExpr->pLeft ){
1539 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pLeft);
1540 }
1541 if( nErr==0 && pExpr->pRight ){
1542 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pRight);
1543 }
1544 if( nErr==0 && pExpr->pList ){
1545 int n = pExpr->pList->nExpr;
1546 int i;
1547 for(i=0; nErr==0 && i<n; i++){
1548 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pList->a[i].pExpr);
1549 }
1550 }
1551 break;
1552 }
1553 }
1554 return nErr;
1555}
drh8e0a2f92002-02-23 23:45:45 +00001556
1557/*
1558** Locate a user function given a name and a number of arguments.
drh0bce8352002-02-28 00:41:10 +00001559** Return a pointer to the FuncDef structure that defines that
drh8e0a2f92002-02-23 23:45:45 +00001560** function, or return NULL if the function does not exist.
1561**
drh0bce8352002-02-28 00:41:10 +00001562** If the createFlag argument is true, then a new (blank) FuncDef
drh8e0a2f92002-02-23 23:45:45 +00001563** structure is created and liked into the "db" structure if a
1564** no matching function previously existed. When createFlag is true
1565** and the nArg parameter is -1, then only a function that accepts
1566** any number of arguments will be returned.
1567**
1568** If createFlag is false and nArg is -1, then the first valid
1569** function found is returned. A function is valid if either xFunc
1570** or xStep is non-zero.
1571*/
drh0bce8352002-02-28 00:41:10 +00001572FuncDef *sqliteFindFunction(
drh8e0a2f92002-02-23 23:45:45 +00001573 sqlite *db, /* An open database */
1574 const char *zName, /* Name of the function. Not null-terminated */
1575 int nName, /* Number of characters in the name */
1576 int nArg, /* Number of arguments. -1 means any number */
1577 int createFlag /* Create new entry if true and does not otherwise exist */
1578){
drh0bce8352002-02-28 00:41:10 +00001579 FuncDef *pFirst, *p, *pMaybe;
1580 pFirst = p = (FuncDef*)sqliteHashFind(&db->aFunc, zName, nName);
drh1350b032002-02-27 19:00:20 +00001581 if( p && !createFlag && nArg<0 ){
drh8e0a2f92002-02-23 23:45:45 +00001582 while( p && p->xFunc==0 && p->xStep==0 ){ p = p->pNext; }
1583 return p;
1584 }
1585 pMaybe = 0;
1586 while( p && p->nArg!=nArg ){
1587 if( p->nArg<0 && !createFlag && (p->xFunc || p->xStep) ) pMaybe = p;
1588 p = p->pNext;
1589 }
1590 if( p && !createFlag && p->xFunc==0 && p->xStep==0 ){
1591 return 0;
1592 }
1593 if( p==0 && pMaybe ){
1594 assert( createFlag==0 );
1595 return pMaybe;
1596 }
drh89425d52002-02-28 03:04:48 +00001597 if( p==0 && createFlag && (p = sqliteMalloc(sizeof(*p)))!=0 ){
drh8e0a2f92002-02-23 23:45:45 +00001598 p->nArg = nArg;
1599 p->pNext = pFirst;
drhc9b84a12002-06-20 11:36:48 +00001600 p->dataType = pFirst ? pFirst->dataType : SQLITE_NUMERIC;
drh0bce8352002-02-28 00:41:10 +00001601 sqliteHashInsert(&db->aFunc, zName, nName, (void*)p);
drh8e0a2f92002-02-23 23:45:45 +00001602 }
1603 return p;
1604}