blob: 7f3f96f603dd8684536c0aa933285ccae2bdebc5 [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**
drh7b58dae2003-07-20 01:16:46 +000015** $Id: expr.c,v 1.97 2003/07/20 01:16:47 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++){
drhf26e09c2003-05-31 16:21:12 +0000194 pNew->a[i].zDatabase = sqliteStrDup(p->a[i].zDatabase);
drhad3cab52002-05-24 02:04:32 +0000195 pNew->a[i].zName = sqliteStrDup(p->a[i].zName);
196 pNew->a[i].zAlias = sqliteStrDup(p->a[i].zAlias);
197 pNew->a[i].jointype = p->a[i].jointype;
drh6a3ea0e2003-05-02 14:32:12 +0000198 pNew->a[i].iCursor = p->a[i].iCursor;
drhad3cab52002-05-24 02:04:32 +0000199 pNew->a[i].pTab = 0;
200 pNew->a[i].pSelect = sqliteSelectDup(p->a[i].pSelect);
201 pNew->a[i].pOn = sqliteExprDup(p->a[i].pOn);
202 pNew->a[i].pUsing = sqliteIdListDup(p->a[i].pUsing);
203 }
204 return pNew;
205}
drhff78bd22002-02-27 01:47:11 +0000206IdList *sqliteIdListDup(IdList *p){
207 IdList *pNew;
208 int i;
209 if( p==0 ) return 0;
210 pNew = sqliteMalloc( sizeof(*pNew) );
211 if( pNew==0 ) return 0;
212 pNew->nId = p->nId;
213 pNew->a = sqliteMalloc( p->nId*sizeof(p->a[0]) );
drhe4697f52002-05-23 02:09:03 +0000214 if( pNew->a==0 ) return 0;
drhff78bd22002-02-27 01:47:11 +0000215 for(i=0; i<p->nId; i++){
216 pNew->a[i].zName = sqliteStrDup(p->a[i].zName);
drhff78bd22002-02-27 01:47:11 +0000217 pNew->a[i].idx = p->a[i].idx;
drhff78bd22002-02-27 01:47:11 +0000218 }
219 return pNew;
220}
221Select *sqliteSelectDup(Select *p){
222 Select *pNew;
223 if( p==0 ) return 0;
224 pNew = sqliteMalloc( sizeof(*p) );
225 if( pNew==0 ) return 0;
226 pNew->isDistinct = p->isDistinct;
227 pNew->pEList = sqliteExprListDup(p->pEList);
drhad3cab52002-05-24 02:04:32 +0000228 pNew->pSrc = sqliteSrcListDup(p->pSrc);
drhff78bd22002-02-27 01:47:11 +0000229 pNew->pWhere = sqliteExprDup(p->pWhere);
230 pNew->pGroupBy = sqliteExprListDup(p->pGroupBy);
231 pNew->pHaving = sqliteExprDup(p->pHaving);
232 pNew->pOrderBy = sqliteExprListDup(p->pOrderBy);
233 pNew->op = p->op;
234 pNew->pPrior = sqliteSelectDup(p->pPrior);
235 pNew->nLimit = p->nLimit;
236 pNew->nOffset = p->nOffset;
237 pNew->zSelect = 0;
drh7b58dae2003-07-20 01:16:46 +0000238 pNew->iLimit = -1;
239 pNew->iOffset = -1;
drhff78bd22002-02-27 01:47:11 +0000240 return pNew;
241}
242
243
244/*
drha76b5df2002-02-23 02:32:10 +0000245** Add a new element to the end of an expression list. If pList is
246** initially NULL, then create a new expression list.
247*/
248ExprList *sqliteExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){
249 int i;
250 if( pList==0 ){
251 pList = sqliteMalloc( sizeof(ExprList) );
252 if( pList==0 ){
253 sqliteExprDelete(pExpr);
254 return 0;
255 }
256 }
257 if( (pList->nExpr & 7)==0 ){
258 int n = pList->nExpr + 8;
259 struct ExprList_item *a;
260 a = sqliteRealloc(pList->a, n*sizeof(pList->a[0]));
261 if( a==0 ){
262 sqliteExprDelete(pExpr);
263 return pList;
264 }
265 pList->a = a;
266 }
267 if( pExpr || pName ){
268 i = pList->nExpr++;
269 pList->a[i].pExpr = pExpr;
270 pList->a[i].zName = 0;
271 if( pName ){
272 sqliteSetNString(&pList->a[i].zName, pName->z, pName->n, 0);
273 sqliteDequote(pList->a[i].zName);
274 }
275 }
276 return pList;
277}
278
279/*
280** Delete an entire expression list.
281*/
282void sqliteExprListDelete(ExprList *pList){
283 int i;
284 if( pList==0 ) return;
285 for(i=0; i<pList->nExpr; i++){
286 sqliteExprDelete(pList->a[i].pExpr);
287 sqliteFree(pList->a[i].zName);
288 }
289 sqliteFree(pList->a);
290 sqliteFree(pList);
291}
292
293/*
drhfef52082000-06-06 01:50:43 +0000294** Walk an expression tree. Return 1 if the expression is constant
295** and 0 if it involves variables.
drh23989372002-05-21 13:43:04 +0000296**
297** For the purposes of this function, a double-quoted string (ex: "abc")
298** is considered a variable but a single-quoted string (ex: 'abc') is
299** a constant.
drhfef52082000-06-06 01:50:43 +0000300*/
drh92086432002-01-22 14:11:29 +0000301int sqliteExprIsConstant(Expr *p){
drhfef52082000-06-06 01:50:43 +0000302 switch( p->op ){
303 case TK_ID:
drh967e8b72000-06-21 13:59:10 +0000304 case TK_COLUMN:
drhfef52082000-06-06 01:50:43 +0000305 case TK_DOT:
drh7bdc0c12003-04-19 17:27:24 +0000306 case TK_FUNCTION:
drhfef52082000-06-06 01:50:43 +0000307 return 0;
drh7bdc0c12003-04-19 17:27:24 +0000308 case TK_NULL:
drh23989372002-05-21 13:43:04 +0000309 case TK_STRING:
drh92086432002-01-22 14:11:29 +0000310 case TK_INTEGER:
311 case TK_FLOAT:
drh92086432002-01-22 14:11:29 +0000312 return 1;
drhfef52082000-06-06 01:50:43 +0000313 default: {
drh92086432002-01-22 14:11:29 +0000314 if( p->pLeft && !sqliteExprIsConstant(p->pLeft) ) return 0;
315 if( p->pRight && !sqliteExprIsConstant(p->pRight) ) return 0;
drhfef52082000-06-06 01:50:43 +0000316 if( p->pList ){
317 int i;
318 for(i=0; i<p->pList->nExpr; i++){
drh92086432002-01-22 14:11:29 +0000319 if( !sqliteExprIsConstant(p->pList->a[i].pExpr) ) return 0;
drhfef52082000-06-06 01:50:43 +0000320 }
321 }
drh92086432002-01-22 14:11:29 +0000322 return p->pLeft!=0 || p->pRight!=0 || (p->pList && p->pList->nExpr>0);
drhfef52082000-06-06 01:50:43 +0000323 }
324 }
drh92086432002-01-22 14:11:29 +0000325 return 0;
drhfef52082000-06-06 01:50:43 +0000326}
327
328/*
drhe4de1fe2002-06-02 16:09:01 +0000329** If the given expression codes a constant integer, return 1 and put
330** the value of the integer in *pValue. If the expression is not an
331** integer, return 0 and leave *pValue unchanged.
332*/
333int sqliteExprIsInteger(Expr *p, int *pValue){
334 switch( p->op ){
335 case TK_INTEGER: {
336 *pValue = atoi(p->token.z);
337 return 1;
338 }
339 case TK_STRING: {
drhbd790ee2002-06-02 18:22:06 +0000340 const char *z = p->token.z;
drhe4de1fe2002-06-02 16:09:01 +0000341 int n = p->token.n;
drhbd790ee2002-06-02 18:22:06 +0000342 if( n>0 && z[0]=='-' ){ z++; n--; }
drhe4de1fe2002-06-02 16:09:01 +0000343 while( n>0 && *z && isdigit(*z) ){ z++; n--; }
344 if( n==0 ){
345 *pValue = atoi(p->token.z);
346 return 1;
347 }
348 break;
349 }
drh4b59ab52002-08-24 18:24:51 +0000350 case TK_UPLUS: {
351 return sqliteExprIsInteger(p->pLeft, pValue);
352 }
drhe4de1fe2002-06-02 16:09:01 +0000353 case TK_UMINUS: {
354 int v;
355 if( sqliteExprIsInteger(p->pLeft, &v) ){
356 *pValue = -v;
357 return 1;
358 }
359 break;
360 }
361 default: break;
362 }
363 return 0;
364}
365
366/*
drhc4a3c772001-04-04 11:48:57 +0000367** Return TRUE if the given string is a row-id column name.
368*/
drha9f9d1c2002-06-29 02:20:08 +0000369int sqliteIsRowid(const char *z){
drhc4a3c772001-04-04 11:48:57 +0000370 if( sqliteStrICmp(z, "_ROWID_")==0 ) return 1;
371 if( sqliteStrICmp(z, "ROWID")==0 ) return 1;
372 if( sqliteStrICmp(z, "OID")==0 ) return 1;
373 return 0;
374}
375
376/*
drhcce7d172000-05-31 15:34:51 +0000377** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +0000378** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +0000379** index to the table in the table list and a column offset. The
380** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
381** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +0000382** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +0000383** VDBE cursor number for a cursor that is pointing into the referenced
384** table. The Expr.iColumn value is changed to the index of the column
385** of the referenced table. The Expr.iColumn value for the special
386** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
387** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +0000388**
drhfef52082000-06-06 01:50:43 +0000389** We also check for instances of the IN operator. IN comes in two
390** forms:
391**
392** expr IN (exprlist)
393** and
394** expr IN (SELECT ...)
395**
396** The first form is handled by creating a set holding the list
397** of allowed values. The second form causes the SELECT to generate
398** a temporary table.
399**
400** This routine also looks for scalar SELECTs that are part of an expression.
drh19a775c2000-06-05 18:54:46 +0000401** If it finds any, it generates code to write the value of that select
402** into a memory cell.
drhcce7d172000-05-31 15:34:51 +0000403**
drh967e8b72000-06-21 13:59:10 +0000404** Unknown columns or tables provoke an error. The function returns
drhcce7d172000-05-31 15:34:51 +0000405** the number of errors seen and leaves an error message on pParse->zErrMsg.
406*/
drha2e00042002-01-22 03:13:42 +0000407int sqliteExprResolveIds(
408 Parse *pParse, /* The parser context */
drhad3cab52002-05-24 02:04:32 +0000409 SrcList *pTabList, /* List of tables used to resolve column names */
drha2e00042002-01-22 03:13:42 +0000410 ExprList *pEList, /* List of expressions used to resolve "AS" */
411 Expr *pExpr /* The expression to be analyzed. */
412){
drh6a3ea0e2003-05-02 14:32:12 +0000413 int i;
414
drhdaffd0e2001-04-11 14:28:42 +0000415 if( pExpr==0 || pTabList==0 ) return 0;
drh6a3ea0e2003-05-02 14:32:12 +0000416 for(i=0; i<pTabList->nSrc; i++){
417 assert( pTabList->a[i].iCursor>=0 && pTabList->a[i].iCursor<pParse->nTab );
418 }
drhcce7d172000-05-31 15:34:51 +0000419 switch( pExpr->op ){
drh23989372002-05-21 13:43:04 +0000420 /* Double-quoted strings (ex: "abc") are used as identifiers if
421 ** possible. Otherwise they remain as strings. Single-quoted
422 ** strings (ex: 'abc') are always string literals.
423 */
424 case TK_STRING: {
425 if( pExpr->token.z[0]=='\'' ) break;
426 /* Fall thru into the TK_ID case if this is a double-quoted string */
427 }
drha2e00042002-01-22 03:13:42 +0000428 /* A lone identifier. Try and match it as follows:
429 **
430 ** 1. To the name of a column of one of the tables in pTabList
431 **
432 ** 2. To the right side of an AS keyword in the column list of
433 ** a SELECT statement. (For example, match against 'x' in
434 ** "SELECT a+b AS 'x' FROM t1".)
435 **
436 ** 3. One of the special names "ROWID", "OID", or "_ROWID_".
437 */
drhcce7d172000-05-31 15:34:51 +0000438 case TK_ID: {
drhc4a3c772001-04-04 11:48:57 +0000439 int cnt = 0; /* Number of matches */
drha76b5df2002-02-23 02:32:10 +0000440 char *z;
drhe22a3342003-04-22 20:30:37 +0000441 int iDb = -1;
442
drha76b5df2002-02-23 02:32:10 +0000443 assert( pExpr->token.z );
444 z = sqliteStrNDup(pExpr->token.z, pExpr->token.n);
drh2f4392f2002-02-14 21:42:51 +0000445 sqliteDequote(z);
drhdaffd0e2001-04-11 14:28:42 +0000446 if( z==0 ) return 1;
drhad3cab52002-05-24 02:04:32 +0000447 for(i=0; i<pTabList->nSrc; i++){
drhcce7d172000-05-31 15:34:51 +0000448 int j;
449 Table *pTab = pTabList->a[i].pTab;
450 if( pTab==0 ) continue;
drhe22a3342003-04-22 20:30:37 +0000451 iDb = pTab->iDb;
drh417be792002-03-03 18:59:40 +0000452 assert( pTab->nCol>0 );
drhcce7d172000-05-31 15:34:51 +0000453 for(j=0; j<pTab->nCol; j++){
drh7020f652000-06-03 18:06:52 +0000454 if( sqliteStrICmp(pTab->aCol[j].zName, z)==0 ){
drhcce7d172000-05-31 15:34:51 +0000455 cnt++;
drh6a3ea0e2003-05-02 14:32:12 +0000456 pExpr->iTable = pTabList->a[i].iCursor;
drhe22a3342003-04-22 20:30:37 +0000457 pExpr->iDb = pTab->iDb;
drh4a324312001-12-21 14:30:42 +0000458 if( j==pTab->iPKey ){
459 /* Substitute the record number for the INTEGER PRIMARY KEY */
460 pExpr->iColumn = -1;
drhc9b84a12002-06-20 11:36:48 +0000461 pExpr->dataType = SQLITE_SO_NUM;
drh4a324312001-12-21 14:30:42 +0000462 }else{
463 pExpr->iColumn = j;
drhc9b84a12002-06-20 11:36:48 +0000464 pExpr->dataType = pTab->aCol[j].sortOrder & SQLITE_SO_TYPEMASK;
drh4a324312001-12-21 14:30:42 +0000465 }
drha2e00042002-01-22 03:13:42 +0000466 pExpr->op = TK_COLUMN;
drhcce7d172000-05-31 15:34:51 +0000467 }
468 }
469 }
drha2e00042002-01-22 03:13:42 +0000470 if( cnt==0 && pEList!=0 ){
471 int j;
472 for(j=0; j<pEList->nExpr; j++){
473 char *zAs = pEList->a[j].zName;
474 if( zAs!=0 && sqliteStrICmp(zAs, z)==0 ){
475 cnt++;
476 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
477 pExpr->op = TK_AS;
478 pExpr->iColumn = j;
drh75148a22002-03-03 03:42:31 +0000479 pExpr->pLeft = sqliteExprDup(pEList->a[j].pExpr);
drha2e00042002-01-22 03:13:42 +0000480 }
481 }
482 }
drhe22a3342003-04-22 20:30:37 +0000483 if( cnt==0 && iDb>=0 && sqliteIsRowid(z) ){
drhc4a3c772001-04-04 11:48:57 +0000484 pExpr->iColumn = -1;
drh6a3ea0e2003-05-02 14:32:12 +0000485 pExpr->iTable = pTabList->a[0].iCursor;
drhe22a3342003-04-22 20:30:37 +0000486 pExpr->iDb = iDb;
drhad3cab52002-05-24 02:04:32 +0000487 cnt = 1 + (pTabList->nSrc>1);
drha2e00042002-01-22 03:13:42 +0000488 pExpr->op = TK_COLUMN;
drhc9b84a12002-06-20 11:36:48 +0000489 pExpr->dataType = SQLITE_SO_NUM;
drhc4a3c772001-04-04 11:48:57 +0000490 }
drhcce7d172000-05-31 15:34:51 +0000491 sqliteFree(z);
drh23989372002-05-21 13:43:04 +0000492 if( cnt==0 && pExpr->token.z[0]!='"' ){
drhda93d232003-03-31 02:12:46 +0000493 sqliteErrorMsg(pParse, "no such column: %T", &pExpr->token);
drhcce7d172000-05-31 15:34:51 +0000494 return 1;
495 }else if( cnt>1 ){
drhda93d232003-03-31 02:12:46 +0000496 sqliteErrorMsg(pParse, "ambiguous column name: %T", &pExpr->token);
drhcce7d172000-05-31 15:34:51 +0000497 return 1;
498 }
drhed6c8672003-01-12 18:02:16 +0000499 if( pExpr->op==TK_COLUMN ){
drh6a3ea0e2003-05-02 14:32:12 +0000500 sqliteAuthRead(pParse, pExpr, pTabList);
drhed6c8672003-01-12 18:02:16 +0000501 }
drhcce7d172000-05-31 15:34:51 +0000502 break;
503 }
504
drhd24cc422003-03-27 12:51:24 +0000505 /* A table name and column name: ID.ID
506 ** Or a database, table and column: ID.ID.ID
507 */
drhcce7d172000-05-31 15:34:51 +0000508 case TK_DOT: {
drhbed86902000-06-02 13:27:59 +0000509 int cnt = 0; /* Number of matches */
drhc4a3c772001-04-04 11:48:57 +0000510 int cntTab = 0; /* Number of matching tables */
drhbed86902000-06-02 13:27:59 +0000511 int i; /* Loop counter */
drhcce7d172000-05-31 15:34:51 +0000512 Expr *pLeft, *pRight; /* Left and right subbranches of the expr */
drhcce7d172000-05-31 15:34:51 +0000513 char *zLeft, *zRight; /* Text of an identifier */
drhd24cc422003-03-27 12:51:24 +0000514 char *zDb; /* Name of database holding table */
515 sqlite *db = pParse->db;
drhcce7d172000-05-31 15:34:51 +0000516
drhcce7d172000-05-31 15:34:51 +0000517 pRight = pExpr->pRight;
drhd24cc422003-03-27 12:51:24 +0000518 if( pRight->op==TK_ID ){
519 pLeft = pExpr->pLeft;
520 zDb = 0;
521 }else{
522 Expr *pDb = pExpr->pLeft;
523 assert( pDb && pDb->op==TK_ID && pDb->token.z );
524 zDb = sqliteStrNDup(pDb->token.z, pDb->token.n);
525 pLeft = pRight->pLeft;
526 pRight = pRight->pRight;
527 }
drha76b5df2002-02-23 02:32:10 +0000528 assert( pLeft && pLeft->op==TK_ID && pLeft->token.z );
529 assert( pRight && pRight->op==TK_ID && pRight->token.z );
drh6e142f52000-06-08 13:36:40 +0000530 zLeft = sqliteStrNDup(pLeft->token.z, pLeft->token.n);
531 zRight = sqliteStrNDup(pRight->token.z, pRight->token.n);
drhdaffd0e2001-04-11 14:28:42 +0000532 if( zLeft==0 || zRight==0 ){
533 sqliteFree(zLeft);
534 sqliteFree(zRight);
drhd24cc422003-03-27 12:51:24 +0000535 sqliteFree(zDb);
drhdaffd0e2001-04-11 14:28:42 +0000536 return 1;
537 }
drhd24cc422003-03-27 12:51:24 +0000538 sqliteDequote(zDb);
drh87c40e82001-07-23 14:33:02 +0000539 sqliteDequote(zLeft);
540 sqliteDequote(zRight);
drhc4a3c772001-04-04 11:48:57 +0000541 pExpr->iTable = -1;
drhad3cab52002-05-24 02:04:32 +0000542 for(i=0; i<pTabList->nSrc; i++){
drhcce7d172000-05-31 15:34:51 +0000543 int j;
544 char *zTab;
545 Table *pTab = pTabList->a[i].pTab;
546 if( pTab==0 ) continue;
drh417be792002-03-03 18:59:40 +0000547 assert( pTab->nCol>0 );
drhcce7d172000-05-31 15:34:51 +0000548 if( pTabList->a[i].zAlias ){
549 zTab = pTabList->a[i].zAlias;
drhd24cc422003-03-27 12:51:24 +0000550 if( sqliteStrICmp(zTab, zLeft)!=0 ) continue;
drhcce7d172000-05-31 15:34:51 +0000551 }else{
552 zTab = pTab->zName;
drhd24cc422003-03-27 12:51:24 +0000553 if( zTab==0 || sqliteStrICmp(zTab, zLeft)!=0 ) continue;
554 if( zDb!=0 && sqliteStrICmp(db->aDb[pTab->iDb].zName, zDb)!=0 ){
555 continue;
556 }
drhcce7d172000-05-31 15:34:51 +0000557 }
drhe22a3342003-04-22 20:30:37 +0000558 if( 0==(cntTab++) ){
drh6a3ea0e2003-05-02 14:32:12 +0000559 pExpr->iTable = pTabList->a[i].iCursor;
drhe22a3342003-04-22 20:30:37 +0000560 pExpr->iDb = pTab->iDb;
561 }
drhcce7d172000-05-31 15:34:51 +0000562 for(j=0; j<pTab->nCol; j++){
drh7020f652000-06-03 18:06:52 +0000563 if( sqliteStrICmp(pTab->aCol[j].zName, zRight)==0 ){
drhcce7d172000-05-31 15:34:51 +0000564 cnt++;
drh6a3ea0e2003-05-02 14:32:12 +0000565 pExpr->iTable = pTabList->a[i].iCursor;
drhe22a3342003-04-22 20:30:37 +0000566 pExpr->iDb = pTab->iDb;
drh70ce3f02003-04-15 19:22:22 +0000567 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
568 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
drhc9b84a12002-06-20 11:36:48 +0000569 pExpr->dataType = pTab->aCol[j].sortOrder & SQLITE_SO_TYPEMASK;
drhcce7d172000-05-31 15:34:51 +0000570 }
571 }
572 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000573
574 /* If we have not already resolved this *.* expression, then maybe
575 * it is a new.* or old.* trigger argument reference */
danielk1977f29ce552002-05-19 23:43:12 +0000576 if( cnt == 0 && pParse->trigStack != 0 ){
577 TriggerStack *pTriggerStack = pParse->trigStack;
danielk1977c3f9bad2002-05-15 08:30:12 +0000578 int t = 0;
danielk1977f29ce552002-05-19 23:43:12 +0000579 if( pTriggerStack->newIdx != -1 && sqliteStrICmp("new", zLeft) == 0 ){
580 pExpr->iTable = pTriggerStack->newIdx;
drhe22a3342003-04-22 20:30:37 +0000581 assert( pTriggerStack->pTab );
582 pExpr->iDb = pTriggerStack->pTab->iDb;
danielk1977c3f9bad2002-05-15 08:30:12 +0000583 cntTab++;
584 t = 1;
585 }
danielk1977f29ce552002-05-19 23:43:12 +0000586 if( pTriggerStack->oldIdx != -1 && sqliteStrICmp("old", zLeft) == 0 ){
587 pExpr->iTable = pTriggerStack->oldIdx;
drhe22a3342003-04-22 20:30:37 +0000588 assert( pTriggerStack->pTab );
589 pExpr->iDb = pTriggerStack->pTab->iDb;
danielk1977c3f9bad2002-05-15 08:30:12 +0000590 cntTab++;
591 t = 1;
592 }
593
danielk1977f29ce552002-05-19 23:43:12 +0000594 if( t ){
595 int j;
drhc9b84a12002-06-20 11:36:48 +0000596 Table *pTab = pTriggerStack->pTab;
597 for(j=0; j < pTab->nCol; j++) {
598 if( sqliteStrICmp(pTab->aCol[j].zName, zRight)==0 ){
danielk1977c3f9bad2002-05-15 08:30:12 +0000599 cnt++;
drh70ce3f02003-04-15 19:22:22 +0000600 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
drhc9b84a12002-06-20 11:36:48 +0000601 pExpr->dataType = pTab->aCol[j].sortOrder & SQLITE_SO_TYPEMASK;
danielk1977c3f9bad2002-05-15 08:30:12 +0000602 }
603 }
danielk1977f29ce552002-05-19 23:43:12 +0000604 }
danielk1977c3f9bad2002-05-15 08:30:12 +0000605 }
606
drhc4a3c772001-04-04 11:48:57 +0000607 if( cnt==0 && cntTab==1 && sqliteIsRowid(zRight) ){
608 cnt = 1;
609 pExpr->iColumn = -1;
drhc9b84a12002-06-20 11:36:48 +0000610 pExpr->dataType = SQLITE_SO_NUM;
drhc4a3c772001-04-04 11:48:57 +0000611 }
drhd24cc422003-03-27 12:51:24 +0000612 sqliteFree(zDb);
drhcce7d172000-05-31 15:34:51 +0000613 sqliteFree(zLeft);
614 sqliteFree(zRight);
615 if( cnt==0 ){
drhda93d232003-03-31 02:12:46 +0000616 sqliteErrorMsg(pParse, "no such column: %T.%T",
617 &pLeft->token, &pRight->token);
drhcce7d172000-05-31 15:34:51 +0000618 return 1;
619 }else if( cnt>1 ){
drhda93d232003-03-31 02:12:46 +0000620 sqliteErrorMsg(pParse, "ambiguous column name: %T.%T",
621 &pLeft->token, &pRight->token);
drhcce7d172000-05-31 15:34:51 +0000622 return 1;
623 }
drhd24cc422003-03-27 12:51:24 +0000624 sqliteExprDelete(pExpr->pLeft);
drhcce7d172000-05-31 15:34:51 +0000625 pExpr->pLeft = 0;
drhd24cc422003-03-27 12:51:24 +0000626 sqliteExprDelete(pExpr->pRight);
drhcce7d172000-05-31 15:34:51 +0000627 pExpr->pRight = 0;
drh967e8b72000-06-21 13:59:10 +0000628 pExpr->op = TK_COLUMN;
drh6a3ea0e2003-05-02 14:32:12 +0000629 sqliteAuthRead(pParse, pExpr, pTabList);
drhcce7d172000-05-31 15:34:51 +0000630 break;
631 }
632
drhfef52082000-06-06 01:50:43 +0000633 case TK_IN: {
drhd8bc7082000-06-07 23:51:50 +0000634 Vdbe *v = sqliteGetVdbe(pParse);
drhfef52082000-06-06 01:50:43 +0000635 if( v==0 ) return 1;
drh6a3ea0e2003-05-02 14:32:12 +0000636 if( sqliteExprResolveIds(pParse, pTabList, pEList, pExpr->pLeft) ){
drhcfab11b2000-06-06 03:31:22 +0000637 return 1;
638 }
drhfef52082000-06-06 01:50:43 +0000639 if( pExpr->pSelect ){
640 /* Case 1: expr IN (SELECT ...)
641 **
642 ** Generate code to write the results of the select into a temporary
drh4794b982000-06-06 13:54:14 +0000643 ** table. The cursor number of the temporary table has already
644 ** been put in iTable by sqliteExprResolveInSelect().
drhfef52082000-06-06 01:50:43 +0000645 */
drh832508b2002-03-02 17:04:07 +0000646 pExpr->iTable = pParse->nTab++;
drhc6b52df2002-01-04 03:09:29 +0000647 sqliteVdbeAddOp(v, OP_OpenTemp, pExpr->iTable, 1);
drh832508b2002-03-02 17:04:07 +0000648 sqliteSelect(pParse, pExpr->pSelect, SRT_Set, pExpr->iTable, 0,0,0);
drhfef52082000-06-06 01:50:43 +0000649 }else if( pExpr->pList ){
650 /* Case 2: expr IN (exprlist)
651 **
652 ** Create a set to put the exprlist values in. The Set id is stored
653 ** in iTable.
654 */
655 int i, iSet;
656 for(i=0; i<pExpr->pList->nExpr; i++){
657 Expr *pE2 = pExpr->pList->a[i].pExpr;
drh92086432002-01-22 14:11:29 +0000658 if( !sqliteExprIsConstant(pE2) ){
drhda93d232003-03-31 02:12:46 +0000659 sqliteErrorMsg(pParse,
660 "right-hand side of IN operator must be constant");
drhfef52082000-06-06 01:50:43 +0000661 return 1;
662 }
drh4794b982000-06-06 13:54:14 +0000663 if( sqliteExprCheck(pParse, pE2, 0, 0) ){
664 return 1;
665 }
drhfef52082000-06-06 01:50:43 +0000666 }
667 iSet = pExpr->iTable = pParse->nSet++;
668 for(i=0; i<pExpr->pList->nExpr; i++){
669 Expr *pE2 = pExpr->pList->a[i].pExpr;
670 switch( pE2->op ){
671 case TK_FLOAT:
672 case TK_INTEGER:
673 case TK_STRING: {
drh99fcd712001-10-13 01:06:47 +0000674 int addr = sqliteVdbeAddOp(v, OP_SetInsert, iSet, 0);
drha76b5df2002-02-23 02:32:10 +0000675 assert( pE2->token.z );
drhfef52082000-06-06 01:50:43 +0000676 sqliteVdbeChangeP3(v, addr, pE2->token.z, pE2->token.n);
677 sqliteVdbeDequoteP3(v, addr);
678 break;
679 }
680 default: {
681 sqliteExprCode(pParse, pE2);
drh99fcd712001-10-13 01:06:47 +0000682 sqliteVdbeAddOp(v, OP_SetInsert, iSet, 0);
drhfef52082000-06-06 01:50:43 +0000683 break;
684 }
685 }
686 }
687 }
drhcfab11b2000-06-06 03:31:22 +0000688 break;
drhfef52082000-06-06 01:50:43 +0000689 }
690
drh19a775c2000-06-05 18:54:46 +0000691 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +0000692 /* This has to be a scalar SELECT. Generate code to put the
693 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +0000694 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +0000695 */
drh967e8b72000-06-21 13:59:10 +0000696 pExpr->iColumn = pParse->nMem++;
drh832508b2002-03-02 17:04:07 +0000697 if( sqliteSelect(pParse, pExpr->pSelect, SRT_Mem, pExpr->iColumn,0,0,0) ){
drh19a775c2000-06-05 18:54:46 +0000698 return 1;
699 }
700 break;
701 }
702
drhcce7d172000-05-31 15:34:51 +0000703 /* For all else, just recursively walk the tree */
704 default: {
drh4794b982000-06-06 13:54:14 +0000705 if( pExpr->pLeft
drh6a3ea0e2003-05-02 14:32:12 +0000706 && sqliteExprResolveIds(pParse, pTabList, pEList, pExpr->pLeft) ){
drhcce7d172000-05-31 15:34:51 +0000707 return 1;
708 }
709 if( pExpr->pRight
drh6a3ea0e2003-05-02 14:32:12 +0000710 && sqliteExprResolveIds(pParse, pTabList, pEList, pExpr->pRight) ){
drhcce7d172000-05-31 15:34:51 +0000711 return 1;
712 }
713 if( pExpr->pList ){
714 int i;
715 ExprList *pList = pExpr->pList;
716 for(i=0; i<pList->nExpr; i++){
drh832508b2002-03-02 17:04:07 +0000717 Expr *pArg = pList->a[i].pExpr;
drh6a3ea0e2003-05-02 14:32:12 +0000718 if( sqliteExprResolveIds(pParse, pTabList, pEList, pArg) ){
drhcce7d172000-05-31 15:34:51 +0000719 return 1;
720 }
721 }
722 }
723 }
724 }
725 return 0;
726}
727
drhcce7d172000-05-31 15:34:51 +0000728/*
drh4b59ab52002-08-24 18:24:51 +0000729** pExpr is a node that defines a function of some kind. It might
730** be a syntactic function like "count(x)" or it might be a function
731** that implements an operator, like "a LIKE b".
732**
733** This routine makes *pzName point to the name of the function and
734** *pnName hold the number of characters in the function name.
735*/
736static void getFunctionName(Expr *pExpr, const char **pzName, int *pnName){
737 switch( pExpr->op ){
738 case TK_FUNCTION: {
739 *pzName = pExpr->token.z;
drh6977fea2002-10-22 23:38:04 +0000740 *pnName = pExpr->token.n;
drh4b59ab52002-08-24 18:24:51 +0000741 break;
742 }
743 case TK_LIKE: {
744 *pzName = "like";
745 *pnName = 4;
746 break;
747 }
748 case TK_GLOB: {
749 *pzName = "glob";
750 *pnName = 4;
751 break;
752 }
753 default: {
754 *pzName = "can't happen";
755 *pnName = 12;
756 break;
757 }
758 }
759}
760
761/*
drhcce7d172000-05-31 15:34:51 +0000762** Error check the functions in an expression. Make sure all
763** function names are recognized and all functions have the correct
764** number of arguments. Leave an error message in pParse->zErrMsg
765** if anything is amiss. Return the number of errors.
766**
767** if pIsAgg is not null and this expression is an aggregate function
768** (like count(*) or max(value)) then write a 1 into *pIsAgg.
769*/
770int sqliteExprCheck(Parse *pParse, Expr *pExpr, int allowAgg, int *pIsAgg){
771 int nErr = 0;
772 if( pExpr==0 ) return 0;
drhcce7d172000-05-31 15:34:51 +0000773 switch( pExpr->op ){
drh4b59ab52002-08-24 18:24:51 +0000774 case TK_GLOB:
775 case TK_LIKE:
drhcce7d172000-05-31 15:34:51 +0000776 case TK_FUNCTION: {
drhc9b84a12002-06-20 11:36:48 +0000777 int n = pExpr->pList ? pExpr->pList->nExpr : 0; /* Number of arguments */
778 int no_such_func = 0; /* True if no such function exists */
779 int is_type_of = 0; /* True if is the special TypeOf() function */
780 int wrong_num_args = 0; /* True if wrong number of arguments */
781 int is_agg = 0; /* True if is an aggregate function */
drhcce7d172000-05-31 15:34:51 +0000782 int i;
drh4b59ab52002-08-24 18:24:51 +0000783 int nId; /* Number of characters in function name */
784 const char *zId; /* The function name. */
drh0bce8352002-02-28 00:41:10 +0000785 FuncDef *pDef;
786
drh4b59ab52002-08-24 18:24:51 +0000787 getFunctionName(pExpr, &zId, &nId);
788 pDef = sqliteFindFunction(pParse->db, zId, nId, n, 0);
drh0bce8352002-02-28 00:41:10 +0000789 if( pDef==0 ){
drh4b59ab52002-08-24 18:24:51 +0000790 pDef = sqliteFindFunction(pParse->db, zId, nId, -1, 0);
drh0bce8352002-02-28 00:41:10 +0000791 if( pDef==0 ){
drh4b59ab52002-08-24 18:24:51 +0000792 if( n==1 && nId==6 && sqliteStrNICmp(zId, "typeof", 6)==0 ){
drhc9b84a12002-06-20 11:36:48 +0000793 is_type_of = 1;
794 }else {
795 no_such_func = 1;
796 }
drh0bce8352002-02-28 00:41:10 +0000797 }else{
798 wrong_num_args = 1;
drhcce7d172000-05-31 15:34:51 +0000799 }
drh0bce8352002-02-28 00:41:10 +0000800 }else{
801 is_agg = pDef->xFunc==0;
drhcce7d172000-05-31 15:34:51 +0000802 }
drh8e0a2f92002-02-23 23:45:45 +0000803 if( is_agg && !allowAgg ){
804 sqliteSetNString(&pParse->zErrMsg, "misuse of aggregate function ", -1,
drh4b59ab52002-08-24 18:24:51 +0000805 zId, nId, "()", 2, 0);
drh8e0a2f92002-02-23 23:45:45 +0000806 pParse->nErr++;
807 nErr++;
808 is_agg = 0;
809 }else if( no_such_func ){
drh4b59ab52002-08-24 18:24:51 +0000810 sqliteSetNString(&pParse->zErrMsg, "no such function: ", -1, zId,nId,0);
drhcce7d172000-05-31 15:34:51 +0000811 pParse->nErr++;
812 nErr++;
drh8e0a2f92002-02-23 23:45:45 +0000813 }else if( wrong_num_args ){
814 sqliteSetNString(&pParse->zErrMsg,
drh4b59ab52002-08-24 18:24:51 +0000815 "wrong number of arguments to function ", -1, zId, nId, "()", 2, 0);
drh8e0a2f92002-02-23 23:45:45 +0000816 pParse->nErr++;
817 nErr++;
drhcce7d172000-05-31 15:34:51 +0000818 }
drh22827922000-06-06 17:27:05 +0000819 if( is_agg ) pExpr->op = TK_AGG_FUNCTION;
drhcce7d172000-05-31 15:34:51 +0000820 if( is_agg && pIsAgg ) *pIsAgg = 1;
821 for(i=0; nErr==0 && i<n; i++){
drh4cfa7932000-06-08 15:10:46 +0000822 nErr = sqliteExprCheck(pParse, pExpr->pList->a[i].pExpr,
823 allowAgg && !is_agg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000824 }
drhc9b84a12002-06-20 11:36:48 +0000825 if( pDef==0 ){
826 if( is_type_of ){
827 pExpr->op = TK_STRING;
828 if( sqliteExprType(pExpr->pList->a[0].pExpr)==SQLITE_SO_NUM ){
829 pExpr->token.z = "numeric";
830 pExpr->token.n = 7;
831 }else{
832 pExpr->token.z = "text";
833 pExpr->token.n = 4;
834 }
835 }
836 }else if( pDef->dataType>=0 ){
837 if( pDef->dataType<n ){
838 pExpr->dataType =
839 sqliteExprType(pExpr->pList->a[pDef->dataType].pExpr);
840 }else{
841 pExpr->dataType = SQLITE_SO_NUM;
842 }
843 }else if( pDef->dataType==SQLITE_ARGS ){
844 pDef->dataType = SQLITE_SO_TEXT;
845 for(i=0; i<n; i++){
846 if( sqliteExprType(pExpr->pList->a[i].pExpr)==SQLITE_SO_NUM ){
847 pExpr->dataType = SQLITE_SO_NUM;
848 break;
849 }
850 }
851 }else if( pDef->dataType==SQLITE_NUMERIC ){
852 pExpr->dataType = SQLITE_SO_NUM;
853 }else{
854 pExpr->dataType = SQLITE_SO_TEXT;
855 }
drhcce7d172000-05-31 15:34:51 +0000856 }
857 default: {
858 if( pExpr->pLeft ){
drh22827922000-06-06 17:27:05 +0000859 nErr = sqliteExprCheck(pParse, pExpr->pLeft, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000860 }
861 if( nErr==0 && pExpr->pRight ){
drh22827922000-06-06 17:27:05 +0000862 nErr = sqliteExprCheck(pParse, pExpr->pRight, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000863 }
drhfef52082000-06-06 01:50:43 +0000864 if( nErr==0 && pExpr->pList ){
865 int n = pExpr->pList->nExpr;
866 int i;
867 for(i=0; nErr==0 && i<n; i++){
drh22827922000-06-06 17:27:05 +0000868 Expr *pE2 = pExpr->pList->a[i].pExpr;
869 nErr = sqliteExprCheck(pParse, pE2, allowAgg, pIsAgg);
drhfef52082000-06-06 01:50:43 +0000870 }
871 }
drhcce7d172000-05-31 15:34:51 +0000872 break;
873 }
874 }
875 return nErr;
876}
877
878/*
drhc9b84a12002-06-20 11:36:48 +0000879** Return either SQLITE_SO_NUM or SQLITE_SO_TEXT to indicate whether the
880** given expression should sort as numeric values or as text.
881**
882** The sqliteExprResolveIds() and sqliteExprCheck() routines must have
883** both been called on the expression before it is passed to this routine.
884*/
885int sqliteExprType(Expr *p){
886 if( p==0 ) return SQLITE_SO_NUM;
887 while( p ) switch( p->op ){
888 case TK_PLUS:
889 case TK_MINUS:
890 case TK_STAR:
891 case TK_SLASH:
892 case TK_AND:
893 case TK_OR:
894 case TK_ISNULL:
895 case TK_NOTNULL:
896 case TK_NOT:
897 case TK_UMINUS:
drh4b59ab52002-08-24 18:24:51 +0000898 case TK_UPLUS:
drhc9b84a12002-06-20 11:36:48 +0000899 case TK_BITAND:
900 case TK_BITOR:
901 case TK_BITNOT:
902 case TK_LSHIFT:
903 case TK_RSHIFT:
904 case TK_REM:
905 case TK_INTEGER:
906 case TK_FLOAT:
907 case TK_IN:
908 case TK_BETWEEN:
drh4b59ab52002-08-24 18:24:51 +0000909 case TK_GLOB:
910 case TK_LIKE:
drhc9b84a12002-06-20 11:36:48 +0000911 return SQLITE_SO_NUM;
912
913 case TK_STRING:
914 case TK_NULL:
915 case TK_CONCAT:
916 return SQLITE_SO_TEXT;
917
918 case TK_LT:
919 case TK_LE:
920 case TK_GT:
921 case TK_GE:
922 case TK_NE:
923 case TK_EQ:
924 if( sqliteExprType(p->pLeft)==SQLITE_SO_NUM ){
925 return SQLITE_SO_NUM;
926 }
927 p = p->pRight;
928 break;
929
930 case TK_AS:
931 p = p->pLeft;
932 break;
933
934 case TK_COLUMN:
935 case TK_FUNCTION:
936 case TK_AGG_FUNCTION:
937 return p->dataType;
938
939 case TK_SELECT:
940 assert( p->pSelect );
941 assert( p->pSelect->pEList );
942 assert( p->pSelect->pEList->nExpr>0 );
943 p = p->pSelect->pEList->a[0].pExpr;
944 break;
945
drhb1363202002-06-26 02:45:03 +0000946 case TK_CASE: {
947 if( p->pRight && sqliteExprType(p->pRight)==SQLITE_SO_NUM ){
948 return SQLITE_SO_NUM;
949 }
950 if( p->pList ){
951 int i;
952 ExprList *pList = p->pList;
953 for(i=1; i<pList->nExpr; i+=2){
954 if( sqliteExprType(pList->a[i].pExpr)==SQLITE_SO_NUM ){
955 return SQLITE_SO_NUM;
956 }
957 }
958 }
959 return SQLITE_SO_TEXT;
960 }
961
drhc9b84a12002-06-20 11:36:48 +0000962 default:
963 assert( p->op==TK_ABORT ); /* Can't Happen */
964 break;
965 }
966 return SQLITE_SO_NUM;
967}
968
969/*
drhcce7d172000-05-31 15:34:51 +0000970** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +0000971** expression and leave the result on the top of stack.
drhcce7d172000-05-31 15:34:51 +0000972*/
973void sqliteExprCode(Parse *pParse, Expr *pExpr){
974 Vdbe *v = pParse->pVdbe;
975 int op;
drhdaffd0e2001-04-11 14:28:42 +0000976 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +0000977 switch( pExpr->op ){
978 case TK_PLUS: op = OP_Add; break;
979 case TK_MINUS: op = OP_Subtract; break;
980 case TK_STAR: op = OP_Multiply; break;
981 case TK_SLASH: op = OP_Divide; break;
982 case TK_AND: op = OP_And; break;
983 case TK_OR: op = OP_Or; break;
984 case TK_LT: op = OP_Lt; break;
985 case TK_LE: op = OP_Le; break;
986 case TK_GT: op = OP_Gt; break;
987 case TK_GE: op = OP_Ge; break;
988 case TK_NE: op = OP_Ne; break;
989 case TK_EQ: op = OP_Eq; break;
drhcce7d172000-05-31 15:34:51 +0000990 case TK_ISNULL: op = OP_IsNull; break;
991 case TK_NOTNULL: op = OP_NotNull; break;
992 case TK_NOT: op = OP_Not; break;
993 case TK_UMINUS: op = OP_Negative; break;
drhbf4133c2001-10-13 02:59:08 +0000994 case TK_BITAND: op = OP_BitAnd; break;
995 case TK_BITOR: op = OP_BitOr; break;
996 case TK_BITNOT: op = OP_BitNot; break;
997 case TK_LSHIFT: op = OP_ShiftLeft; break;
998 case TK_RSHIFT: op = OP_ShiftRight; break;
999 case TK_REM: op = OP_Remainder; break;
drhcce7d172000-05-31 15:34:51 +00001000 default: break;
1001 }
1002 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00001003 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +00001004 if( pParse->useAgg ){
drh99fcd712001-10-13 01:06:47 +00001005 sqliteVdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drhc4a3c772001-04-04 11:48:57 +00001006 }else if( pExpr->iColumn>=0 ){
drh99fcd712001-10-13 01:06:47 +00001007 sqliteVdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn);
drhc4a3c772001-04-04 11:48:57 +00001008 }else{
drh99fcd712001-10-13 01:06:47 +00001009 sqliteVdbeAddOp(v, OP_Recno, pExpr->iTable, 0);
drh22827922000-06-06 17:27:05 +00001010 }
drhcce7d172000-05-31 15:34:51 +00001011 break;
1012 }
1013 case TK_INTEGER: {
drhd9e30932002-06-09 01:16:01 +00001014 int iVal = atoi(pExpr->token.z);
1015 char zBuf[30];
1016 sprintf(zBuf,"%d",iVal);
1017 if( strlen(zBuf)!=pExpr->token.n
1018 || strncmp(pExpr->token.z,zBuf,pExpr->token.n)!=0 ){
1019 /* If the integer value cannot be represented exactly in 32 bits,
1020 ** then code it as a string instead. */
1021 sqliteVdbeAddOp(v, OP_String, 0, 0);
1022 }else{
1023 sqliteVdbeAddOp(v, OP_Integer, iVal, 0);
1024 }
drhe6840902002-03-06 03:08:25 +00001025 sqliteVdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
1026 break;
1027 }
1028 case TK_FLOAT: {
drh7a7c7392001-11-24 00:31:46 +00001029 sqliteVdbeAddOp(v, OP_String, 0, 0);
drha76b5df2002-02-23 02:32:10 +00001030 assert( pExpr->token.z );
drh7a7c7392001-11-24 00:31:46 +00001031 sqliteVdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
drhcce7d172000-05-31 15:34:51 +00001032 break;
1033 }
drhcce7d172000-05-31 15:34:51 +00001034 case TK_STRING: {
drh99fcd712001-10-13 01:06:47 +00001035 int addr = sqliteVdbeAddOp(v, OP_String, 0, 0);
drha76b5df2002-02-23 02:32:10 +00001036 assert( pExpr->token.z );
drhcce7d172000-05-31 15:34:51 +00001037 sqliteVdbeChangeP3(v, addr, pExpr->token.z, pExpr->token.n);
1038 sqliteVdbeDequoteP3(v, addr);
1039 break;
1040 }
1041 case TK_NULL: {
drh99fcd712001-10-13 01:06:47 +00001042 sqliteVdbeAddOp(v, OP_String, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001043 break;
1044 }
drhc9b84a12002-06-20 11:36:48 +00001045 case TK_LT:
1046 case TK_LE:
1047 case TK_GT:
1048 case TK_GE:
1049 case TK_NE:
1050 case TK_EQ: {
drh491791a2002-07-18 00:34:09 +00001051 if( pParse->db->file_format>=4 && sqliteExprType(pExpr)==SQLITE_SO_TEXT ){
drhc9b84a12002-06-20 11:36:48 +00001052 op += 6; /* Convert numeric opcodes to text opcodes */
1053 }
1054 /* Fall through into the next case */
1055 }
drhcce7d172000-05-31 15:34:51 +00001056 case TK_AND:
1057 case TK_OR:
1058 case TK_PLUS:
1059 case TK_STAR:
1060 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00001061 case TK_REM:
1062 case TK_BITAND:
1063 case TK_BITOR:
drhc9b84a12002-06-20 11:36:48 +00001064 case TK_SLASH: {
drhcce7d172000-05-31 15:34:51 +00001065 sqliteExprCode(pParse, pExpr->pLeft);
1066 sqliteExprCode(pParse, pExpr->pRight);
drh99fcd712001-10-13 01:06:47 +00001067 sqliteVdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001068 break;
1069 }
drhbf4133c2001-10-13 02:59:08 +00001070 case TK_LSHIFT:
1071 case TK_RSHIFT: {
1072 sqliteExprCode(pParse, pExpr->pRight);
1073 sqliteExprCode(pParse, pExpr->pLeft);
1074 sqliteVdbeAddOp(v, op, 0, 0);
1075 break;
1076 }
drh00400772000-06-16 20:51:26 +00001077 case TK_CONCAT: {
1078 sqliteExprCode(pParse, pExpr->pLeft);
1079 sqliteExprCode(pParse, pExpr->pRight);
drh99fcd712001-10-13 01:06:47 +00001080 sqliteVdbeAddOp(v, OP_Concat, 2, 0);
drh00400772000-06-16 20:51:26 +00001081 break;
1082 }
drh4b59ab52002-08-24 18:24:51 +00001083 case TK_UPLUS: {
1084 Expr *pLeft = pExpr->pLeft;
1085 if( pLeft && pLeft->op==TK_INTEGER ){
1086 sqliteVdbeAddOp(v, OP_Integer, atoi(pLeft->token.z), 0);
1087 sqliteVdbeChangeP3(v, -1, pLeft->token.z, pLeft->token.n);
1088 }else if( pLeft && pLeft->op==TK_FLOAT ){
1089 sqliteVdbeAddOp(v, OP_String, 0, 0);
1090 sqliteVdbeChangeP3(v, -1, pLeft->token.z, pLeft->token.n);
1091 }else{
1092 sqliteExprCode(pParse, pExpr->pLeft);
1093 }
1094 break;
1095 }
drhcce7d172000-05-31 15:34:51 +00001096 case TK_UMINUS: {
drh6e142f52000-06-08 13:36:40 +00001097 assert( pExpr->pLeft );
drh7a7c7392001-11-24 00:31:46 +00001098 if( pExpr->pLeft->op==TK_FLOAT || pExpr->pLeft->op==TK_INTEGER ){
drh6e142f52000-06-08 13:36:40 +00001099 Token *p = &pExpr->pLeft->token;
1100 char *z = sqliteMalloc( p->n + 2 );
1101 sprintf(z, "-%.*s", p->n, p->z);
drhe6840902002-03-06 03:08:25 +00001102 if( pExpr->pLeft->op==TK_INTEGER ){
1103 sqliteVdbeAddOp(v, OP_Integer, atoi(z), 0);
1104 }else{
1105 sqliteVdbeAddOp(v, OP_String, 0, 0);
1106 }
drh99fcd712001-10-13 01:06:47 +00001107 sqliteVdbeChangeP3(v, -1, z, p->n+1);
drh6e142f52000-06-08 13:36:40 +00001108 sqliteFree(z);
1109 break;
1110 }
drh1ccde152000-06-17 13:12:39 +00001111 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +00001112 }
drhbf4133c2001-10-13 02:59:08 +00001113 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00001114 case TK_NOT: {
drhcce7d172000-05-31 15:34:51 +00001115 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +00001116 sqliteVdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001117 break;
1118 }
1119 case TK_ISNULL:
1120 case TK_NOTNULL: {
1121 int dest;
drh99fcd712001-10-13 01:06:47 +00001122 sqliteVdbeAddOp(v, OP_Integer, 1, 0);
drhcce7d172000-05-31 15:34:51 +00001123 sqliteExprCode(pParse, pExpr->pLeft);
1124 dest = sqliteVdbeCurrentAddr(v) + 2;
drhf5905aa2002-05-26 20:54:33 +00001125 sqliteVdbeAddOp(v, op, 1, dest);
drh99fcd712001-10-13 01:06:47 +00001126 sqliteVdbeAddOp(v, OP_AddImm, -1, 0);
drhcce7d172000-05-31 15:34:51 +00001127 break;
1128 }
drh22827922000-06-06 17:27:05 +00001129 case TK_AGG_FUNCTION: {
drh99fcd712001-10-13 01:06:47 +00001130 sqliteVdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drh22827922000-06-06 17:27:05 +00001131 break;
1132 }
drh4b59ab52002-08-24 18:24:51 +00001133 case TK_GLOB:
1134 case TK_LIKE:
drhcce7d172000-05-31 15:34:51 +00001135 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00001136 int i;
1137 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00001138 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00001139 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00001140 int nId;
1141 const char *zId;
1142 getFunctionName(pExpr, &zId, &nId);
1143 pDef = sqliteFindFunction(pParse->db, zId, nId, nExpr, 0);
drh0bce8352002-02-28 00:41:10 +00001144 assert( pDef!=0 );
drh89425d52002-02-28 03:04:48 +00001145 for(i=0; i<nExpr; i++){
drh0bce8352002-02-28 00:41:10 +00001146 sqliteExprCode(pParse, pList->a[i].pExpr);
drhcce7d172000-05-31 15:34:51 +00001147 }
drh89425d52002-02-28 03:04:48 +00001148 sqliteVdbeAddOp(v, OP_Function, nExpr, 0);
drh0bce8352002-02-28 00:41:10 +00001149 sqliteVdbeChangeP3(v, -1, (char*)pDef, P3_POINTER);
drhcce7d172000-05-31 15:34:51 +00001150 break;
1151 }
drh19a775c2000-06-05 18:54:46 +00001152 case TK_SELECT: {
drh99fcd712001-10-13 01:06:47 +00001153 sqliteVdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drh19a775c2000-06-05 18:54:46 +00001154 break;
1155 }
drhfef52082000-06-06 01:50:43 +00001156 case TK_IN: {
1157 int addr;
drh99fcd712001-10-13 01:06:47 +00001158 sqliteVdbeAddOp(v, OP_Integer, 1, 0);
drhfef52082000-06-06 01:50:43 +00001159 sqliteExprCode(pParse, pExpr->pLeft);
1160 addr = sqliteVdbeCurrentAddr(v);
drhf5905aa2002-05-26 20:54:33 +00001161 sqliteVdbeAddOp(v, OP_NotNull, -1, addr+4);
1162 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1163 sqliteVdbeAddOp(v, OP_String, 0, 0);
1164 sqliteVdbeAddOp(v, OP_Goto, 0, addr+6);
drhfef52082000-06-06 01:50:43 +00001165 if( pExpr->pSelect ){
drhf5905aa2002-05-26 20:54:33 +00001166 sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, addr+6);
drhfef52082000-06-06 01:50:43 +00001167 }else{
drhf5905aa2002-05-26 20:54:33 +00001168 sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, addr+6);
drhfef52082000-06-06 01:50:43 +00001169 }
drh99fcd712001-10-13 01:06:47 +00001170 sqliteVdbeAddOp(v, OP_AddImm, -1, 0);
drhfef52082000-06-06 01:50:43 +00001171 break;
1172 }
1173 case TK_BETWEEN: {
drhf5905aa2002-05-26 20:54:33 +00001174 sqliteExprCode(pParse, pExpr->pLeft);
1175 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
1176 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
1177 sqliteVdbeAddOp(v, OP_Ge, 0, 0);
1178 sqliteVdbeAddOp(v, OP_Pull, 1, 0);
1179 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
1180 sqliteVdbeAddOp(v, OP_Le, 0, 0);
1181 sqliteVdbeAddOp(v, OP_And, 0, 0);
drhfef52082000-06-06 01:50:43 +00001182 break;
1183 }
drha2e00042002-01-22 03:13:42 +00001184 case TK_AS: {
1185 sqliteExprCode(pParse, pExpr->pLeft);
1186 break;
1187 }
drh17a7f8d2002-03-24 13:13:27 +00001188 case TK_CASE: {
1189 int expr_end_label;
drhf5905aa2002-05-26 20:54:33 +00001190 int jumpInst;
1191 int addr;
1192 int nExpr;
drh17a7f8d2002-03-24 13:13:27 +00001193 int i;
1194
1195 assert(pExpr->pList);
1196 assert((pExpr->pList->nExpr % 2) == 0);
1197 assert(pExpr->pList->nExpr > 0);
drhf5905aa2002-05-26 20:54:33 +00001198 nExpr = pExpr->pList->nExpr;
1199 expr_end_label = sqliteVdbeMakeLabel(v);
drh17a7f8d2002-03-24 13:13:27 +00001200 if( pExpr->pLeft ){
1201 sqliteExprCode(pParse, pExpr->pLeft);
1202 }
drhf5905aa2002-05-26 20:54:33 +00001203 for(i=0; i<nExpr; i=i+2){
1204 sqliteExprCode(pParse, pExpr->pList->a[i].pExpr);
drh17a7f8d2002-03-24 13:13:27 +00001205 if( pExpr->pLeft ){
drhf5905aa2002-05-26 20:54:33 +00001206 sqliteVdbeAddOp(v, OP_Dup, 1, 1);
drhf570f012002-05-31 15:51:25 +00001207 jumpInst = sqliteVdbeAddOp(v, OP_Ne, 1, 0);
1208 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001209 }else{
drhf570f012002-05-31 15:51:25 +00001210 jumpInst = sqliteVdbeAddOp(v, OP_IfNot, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001211 }
1212 sqliteExprCode(pParse, pExpr->pList->a[i+1].pExpr);
drhf5905aa2002-05-26 20:54:33 +00001213 sqliteVdbeAddOp(v, OP_Goto, 0, expr_end_label);
drhf5905aa2002-05-26 20:54:33 +00001214 addr = sqliteVdbeCurrentAddr(v);
1215 sqliteVdbeChangeP2(v, jumpInst, addr);
drh17a7f8d2002-03-24 13:13:27 +00001216 }
drhf570f012002-05-31 15:51:25 +00001217 if( pExpr->pLeft ){
1218 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1219 }
drh17a7f8d2002-03-24 13:13:27 +00001220 if( pExpr->pRight ){
1221 sqliteExprCode(pParse, pExpr->pRight);
1222 }else{
drhf5905aa2002-05-26 20:54:33 +00001223 sqliteVdbeAddOp(v, OP_String, 0, 0);
drh17a7f8d2002-03-24 13:13:27 +00001224 }
drhf5905aa2002-05-26 20:54:33 +00001225 sqliteVdbeResolveLabel(v, expr_end_label);
danielk19776f349032002-06-11 02:25:40 +00001226 break;
1227 }
1228 case TK_RAISE: {
1229 if( !pParse->trigStack ){
drhda93d232003-03-31 02:12:46 +00001230 sqliteErrorMsg(pParse,
1231 "RAISE() may only be used within a trigger-program");
danielk19776f349032002-06-11 02:25:40 +00001232 pParse->nErr++;
1233 return;
1234 }
1235 if( pExpr->iColumn == OE_Rollback ||
1236 pExpr->iColumn == OE_Abort ||
1237 pExpr->iColumn == OE_Fail ){
1238 char * msg = sqliteStrNDup(pExpr->token.z, pExpr->token.n);
1239 sqliteVdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn);
1240 sqliteDequote(msg);
1241 sqliteVdbeChangeP3(v, -1, msg, 0);
1242 sqliteFree(msg);
1243 } else {
1244 assert( pExpr->iColumn == OE_Ignore );
1245 sqliteVdbeAddOp(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
drh483750b2003-01-29 18:46:51 +00001246 sqliteVdbeChangeP3(v, -1, "(IGNORE jump)", 0);
danielk19776f349032002-06-11 02:25:40 +00001247 }
drh17a7f8d2002-03-24 13:13:27 +00001248 }
1249 break;
drhcce7d172000-05-31 15:34:51 +00001250 }
drhcce7d172000-05-31 15:34:51 +00001251}
1252
1253/*
1254** Generate code for a boolean expression such that a jump is made
1255** to the label "dest" if the expression is true but execution
1256** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00001257**
1258** If the expression evaluates to NULL (neither true nor false), then
1259** take the jump if the jumpIfNull flag is true.
drhcce7d172000-05-31 15:34:51 +00001260*/
drhf5905aa2002-05-26 20:54:33 +00001261void sqliteExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001262 Vdbe *v = pParse->pVdbe;
1263 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001264 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001265 switch( pExpr->op ){
1266 case TK_LT: op = OP_Lt; break;
1267 case TK_LE: op = OP_Le; break;
1268 case TK_GT: op = OP_Gt; break;
1269 case TK_GE: op = OP_Ge; break;
1270 case TK_NE: op = OP_Ne; break;
1271 case TK_EQ: op = OP_Eq; break;
drhcce7d172000-05-31 15:34:51 +00001272 case TK_ISNULL: op = OP_IsNull; break;
1273 case TK_NOTNULL: op = OP_NotNull; break;
1274 default: break;
1275 }
1276 switch( pExpr->op ){
1277 case TK_AND: {
1278 int d2 = sqliteVdbeMakeLabel(v);
drhf5905aa2002-05-26 20:54:33 +00001279 sqliteExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
1280 sqliteExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001281 sqliteVdbeResolveLabel(v, d2);
1282 break;
1283 }
1284 case TK_OR: {
drhf5905aa2002-05-26 20:54:33 +00001285 sqliteExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
1286 sqliteExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001287 break;
1288 }
1289 case TK_NOT: {
drhf5905aa2002-05-26 20:54:33 +00001290 sqliteExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001291 break;
1292 }
1293 case TK_LT:
1294 case TK_LE:
1295 case TK_GT:
1296 case TK_GE:
1297 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00001298 case TK_EQ: {
drhcce7d172000-05-31 15:34:51 +00001299 sqliteExprCode(pParse, pExpr->pLeft);
1300 sqliteExprCode(pParse, pExpr->pRight);
drh491791a2002-07-18 00:34:09 +00001301 if( pParse->db->file_format>=4 && sqliteExprType(pExpr)==SQLITE_SO_TEXT ){
drhc9b84a12002-06-20 11:36:48 +00001302 op += 6; /* Convert numeric opcodes to text opcodes */
1303 }
drhf5905aa2002-05-26 20:54:33 +00001304 sqliteVdbeAddOp(v, op, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001305 break;
1306 }
1307 case TK_ISNULL:
1308 case TK_NOTNULL: {
1309 sqliteExprCode(pParse, pExpr->pLeft);
drhf5905aa2002-05-26 20:54:33 +00001310 sqliteVdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001311 break;
1312 }
drhfef52082000-06-06 01:50:43 +00001313 case TK_IN: {
drhf5905aa2002-05-26 20:54:33 +00001314 int addr;
drhcfab11b2000-06-06 03:31:22 +00001315 sqliteExprCode(pParse, pExpr->pLeft);
drhf5905aa2002-05-26 20:54:33 +00001316 addr = sqliteVdbeCurrentAddr(v);
1317 sqliteVdbeAddOp(v, OP_NotNull, -1, addr+3);
1318 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1319 sqliteVdbeAddOp(v, OP_Goto, 0, jumpIfNull ? dest : addr+4);
drhfef52082000-06-06 01:50:43 +00001320 if( pExpr->pSelect ){
drh99fcd712001-10-13 01:06:47 +00001321 sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001322 }else{
drh99fcd712001-10-13 01:06:47 +00001323 sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001324 }
1325 break;
1326 }
1327 case TK_BETWEEN: {
drhf5905aa2002-05-26 20:54:33 +00001328 int addr;
drhfef52082000-06-06 01:50:43 +00001329 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +00001330 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
drhfef52082000-06-06 01:50:43 +00001331 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
drhf5905aa2002-05-26 20:54:33 +00001332 addr = sqliteVdbeAddOp(v, OP_Lt, !jumpIfNull, 0);
drhfef52082000-06-06 01:50:43 +00001333 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
drhf5905aa2002-05-26 20:54:33 +00001334 sqliteVdbeAddOp(v, OP_Le, jumpIfNull, dest);
drh99fcd712001-10-13 01:06:47 +00001335 sqliteVdbeAddOp(v, OP_Integer, 0, 0);
drhf5905aa2002-05-26 20:54:33 +00001336 sqliteVdbeChangeP2(v, addr, sqliteVdbeCurrentAddr(v));
drh99fcd712001-10-13 01:06:47 +00001337 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00001338 break;
1339 }
drhcce7d172000-05-31 15:34:51 +00001340 default: {
1341 sqliteExprCode(pParse, pExpr);
drhf5905aa2002-05-26 20:54:33 +00001342 sqliteVdbeAddOp(v, OP_If, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001343 break;
1344 }
1345 }
1346}
1347
1348/*
drh66b89c82000-11-28 20:47:17 +00001349** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00001350** to the label "dest" if the expression is false but execution
1351** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00001352**
1353** If the expression evaluates to NULL (neither true nor false) then
1354** jump if jumpIfNull is true or fall through if jumpIfNull is false.
drhcce7d172000-05-31 15:34:51 +00001355*/
drhf5905aa2002-05-26 20:54:33 +00001356void sqliteExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001357 Vdbe *v = pParse->pVdbe;
1358 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001359 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001360 switch( pExpr->op ){
1361 case TK_LT: op = OP_Ge; break;
1362 case TK_LE: op = OP_Gt; break;
1363 case TK_GT: op = OP_Le; break;
1364 case TK_GE: op = OP_Lt; break;
1365 case TK_NE: op = OP_Eq; break;
1366 case TK_EQ: op = OP_Ne; break;
drhcce7d172000-05-31 15:34:51 +00001367 case TK_ISNULL: op = OP_NotNull; break;
1368 case TK_NOTNULL: op = OP_IsNull; break;
1369 default: break;
1370 }
1371 switch( pExpr->op ){
1372 case TK_AND: {
drhf5905aa2002-05-26 20:54:33 +00001373 sqliteExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
1374 sqliteExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001375 break;
1376 }
1377 case TK_OR: {
1378 int d2 = sqliteVdbeMakeLabel(v);
drhf5905aa2002-05-26 20:54:33 +00001379 sqliteExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
1380 sqliteExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001381 sqliteVdbeResolveLabel(v, d2);
1382 break;
1383 }
1384 case TK_NOT: {
drhf5905aa2002-05-26 20:54:33 +00001385 sqliteExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001386 break;
1387 }
1388 case TK_LT:
1389 case TK_LE:
1390 case TK_GT:
1391 case TK_GE:
1392 case TK_NE:
1393 case TK_EQ: {
drh491791a2002-07-18 00:34:09 +00001394 if( pParse->db->file_format>=4 && sqliteExprType(pExpr)==SQLITE_SO_TEXT ){
drh8f619cc2002-09-08 00:04:50 +00001395 /* Convert numeric comparison opcodes into text comparison opcodes.
1396 ** This step depends on the fact that the text comparision opcodes are
1397 ** always 6 greater than their corresponding numeric comparison
1398 ** opcodes.
1399 */
1400 assert( OP_Eq+6 == OP_StrEq );
1401 op += 6;
drhc9b84a12002-06-20 11:36:48 +00001402 }
drhcce7d172000-05-31 15:34:51 +00001403 sqliteExprCode(pParse, pExpr->pLeft);
1404 sqliteExprCode(pParse, pExpr->pRight);
drhf5905aa2002-05-26 20:54:33 +00001405 sqliteVdbeAddOp(v, op, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001406 break;
1407 }
drhcce7d172000-05-31 15:34:51 +00001408 case TK_ISNULL:
1409 case TK_NOTNULL: {
1410 sqliteExprCode(pParse, pExpr->pLeft);
drhf5905aa2002-05-26 20:54:33 +00001411 sqliteVdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001412 break;
1413 }
drhfef52082000-06-06 01:50:43 +00001414 case TK_IN: {
drhf5905aa2002-05-26 20:54:33 +00001415 int addr;
drhcfab11b2000-06-06 03:31:22 +00001416 sqliteExprCode(pParse, pExpr->pLeft);
drhf5905aa2002-05-26 20:54:33 +00001417 addr = sqliteVdbeCurrentAddr(v);
1418 sqliteVdbeAddOp(v, OP_NotNull, -1, addr+3);
1419 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1420 sqliteVdbeAddOp(v, OP_Goto, 0, jumpIfNull ? dest : addr+4);
drhfef52082000-06-06 01:50:43 +00001421 if( pExpr->pSelect ){
drh99fcd712001-10-13 01:06:47 +00001422 sqliteVdbeAddOp(v, OP_NotFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001423 }else{
drh99fcd712001-10-13 01:06:47 +00001424 sqliteVdbeAddOp(v, OP_SetNotFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001425 }
1426 break;
1427 }
1428 case TK_BETWEEN: {
1429 int addr;
1430 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +00001431 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
drhfef52082000-06-06 01:50:43 +00001432 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
1433 addr = sqliteVdbeCurrentAddr(v);
drhf5905aa2002-05-26 20:54:33 +00001434 sqliteVdbeAddOp(v, OP_Ge, !jumpIfNull, addr+3);
drh99fcd712001-10-13 01:06:47 +00001435 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1436 sqliteVdbeAddOp(v, OP_Goto, 0, dest);
drhfef52082000-06-06 01:50:43 +00001437 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
drhf5905aa2002-05-26 20:54:33 +00001438 sqliteVdbeAddOp(v, OP_Gt, jumpIfNull, dest);
drhfef52082000-06-06 01:50:43 +00001439 break;
1440 }
drhcce7d172000-05-31 15:34:51 +00001441 default: {
1442 sqliteExprCode(pParse, pExpr);
drh461c2812002-05-30 02:35:11 +00001443 sqliteVdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001444 break;
1445 }
1446 }
1447}
drh22827922000-06-06 17:27:05 +00001448
1449/*
1450** Do a deep comparison of two expression trees. Return TRUE (non-zero)
1451** if they are identical and return FALSE if they differ in any way.
1452*/
drhd8bc7082000-06-07 23:51:50 +00001453int sqliteExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00001454 int i;
1455 if( pA==0 ){
1456 return pB==0;
1457 }else if( pB==0 ){
1458 return 0;
1459 }
1460 if( pA->op!=pB->op ) return 0;
drhd8bc7082000-06-07 23:51:50 +00001461 if( !sqliteExprCompare(pA->pLeft, pB->pLeft) ) return 0;
1462 if( !sqliteExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00001463 if( pA->pList ){
1464 if( pB->pList==0 ) return 0;
1465 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
1466 for(i=0; i<pA->pList->nExpr; i++){
drhd8bc7082000-06-07 23:51:50 +00001467 if( !sqliteExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00001468 return 0;
1469 }
1470 }
1471 }else if( pB->pList ){
1472 return 0;
1473 }
1474 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00001475 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drh22827922000-06-06 17:27:05 +00001476 if( pA->token.z ){
1477 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00001478 if( pB->token.n!=pA->token.n ) return 0;
1479 if( sqliteStrNICmp(pA->token.z, pB->token.z, pB->token.n)!=0 ) return 0;
drh22827922000-06-06 17:27:05 +00001480 }
1481 return 1;
1482}
1483
1484/*
1485** Add a new element to the pParse->aAgg[] array and return its index.
1486*/
1487static int appendAggInfo(Parse *pParse){
1488 if( (pParse->nAgg & 0x7)==0 ){
1489 int amt = pParse->nAgg + 8;
drh6d4abfb2001-10-22 02:58:08 +00001490 AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0]));
1491 if( aAgg==0 ){
drh22827922000-06-06 17:27:05 +00001492 return -1;
1493 }
drh6d4abfb2001-10-22 02:58:08 +00001494 pParse->aAgg = aAgg;
drh22827922000-06-06 17:27:05 +00001495 }
1496 memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0]));
1497 return pParse->nAgg++;
1498}
1499
1500/*
1501** Analyze the given expression looking for aggregate functions and
1502** for variables that need to be added to the pParse->aAgg[] array.
1503** Make additional entries to the pParse->aAgg[] array as necessary.
1504**
1505** This routine should only be called after the expression has been
1506** analyzed by sqliteExprResolveIds() and sqliteExprCheck().
1507**
1508** If errors are seen, leave an error message in zErrMsg and return
1509** the number of errors.
1510*/
1511int sqliteExprAnalyzeAggregates(Parse *pParse, Expr *pExpr){
1512 int i;
1513 AggExpr *aAgg;
1514 int nErr = 0;
1515
1516 if( pExpr==0 ) return 0;
1517 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00001518 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +00001519 aAgg = pParse->aAgg;
1520 for(i=0; i<pParse->nAgg; i++){
1521 if( aAgg[i].isAgg ) continue;
1522 if( aAgg[i].pExpr->iTable==pExpr->iTable
drh967e8b72000-06-21 13:59:10 +00001523 && aAgg[i].pExpr->iColumn==pExpr->iColumn ){
drh22827922000-06-06 17:27:05 +00001524 break;
1525 }
1526 }
1527 if( i>=pParse->nAgg ){
1528 i = appendAggInfo(pParse);
1529 if( i<0 ) return 1;
1530 pParse->aAgg[i].isAgg = 0;
1531 pParse->aAgg[i].pExpr = pExpr;
1532 }
drhaaf88722000-06-08 11:25:00 +00001533 pExpr->iAgg = i;
drh22827922000-06-06 17:27:05 +00001534 break;
1535 }
1536 case TK_AGG_FUNCTION: {
drh22827922000-06-06 17:27:05 +00001537 aAgg = pParse->aAgg;
1538 for(i=0; i<pParse->nAgg; i++){
1539 if( !aAgg[i].isAgg ) continue;
drhd8bc7082000-06-07 23:51:50 +00001540 if( sqliteExprCompare(aAgg[i].pExpr, pExpr) ){
drh22827922000-06-06 17:27:05 +00001541 break;
1542 }
1543 }
1544 if( i>=pParse->nAgg ){
1545 i = appendAggInfo(pParse);
1546 if( i<0 ) return 1;
1547 pParse->aAgg[i].isAgg = 1;
1548 pParse->aAgg[i].pExpr = pExpr;
drh0bce8352002-02-28 00:41:10 +00001549 pParse->aAgg[i].pFunc = sqliteFindFunction(pParse->db,
drh6977fea2002-10-22 23:38:04 +00001550 pExpr->token.z, pExpr->token.n,
drhf55f25f2002-02-28 01:46:11 +00001551 pExpr->pList ? pExpr->pList->nExpr : 0, 0);
drh22827922000-06-06 17:27:05 +00001552 }
1553 pExpr->iAgg = i;
1554 break;
1555 }
1556 default: {
1557 if( pExpr->pLeft ){
1558 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pLeft);
1559 }
1560 if( nErr==0 && pExpr->pRight ){
1561 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pRight);
1562 }
1563 if( nErr==0 && pExpr->pList ){
1564 int n = pExpr->pList->nExpr;
1565 int i;
1566 for(i=0; nErr==0 && i<n; i++){
1567 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pList->a[i].pExpr);
1568 }
1569 }
1570 break;
1571 }
1572 }
1573 return nErr;
1574}
drh8e0a2f92002-02-23 23:45:45 +00001575
1576/*
1577** Locate a user function given a name and a number of arguments.
drh0bce8352002-02-28 00:41:10 +00001578** Return a pointer to the FuncDef structure that defines that
drh8e0a2f92002-02-23 23:45:45 +00001579** function, or return NULL if the function does not exist.
1580**
drh0bce8352002-02-28 00:41:10 +00001581** If the createFlag argument is true, then a new (blank) FuncDef
drh8e0a2f92002-02-23 23:45:45 +00001582** structure is created and liked into the "db" structure if a
1583** no matching function previously existed. When createFlag is true
1584** and the nArg parameter is -1, then only a function that accepts
1585** any number of arguments will be returned.
1586**
1587** If createFlag is false and nArg is -1, then the first valid
1588** function found is returned. A function is valid if either xFunc
1589** or xStep is non-zero.
1590*/
drh0bce8352002-02-28 00:41:10 +00001591FuncDef *sqliteFindFunction(
drh8e0a2f92002-02-23 23:45:45 +00001592 sqlite *db, /* An open database */
1593 const char *zName, /* Name of the function. Not null-terminated */
1594 int nName, /* Number of characters in the name */
1595 int nArg, /* Number of arguments. -1 means any number */
1596 int createFlag /* Create new entry if true and does not otherwise exist */
1597){
drh0bce8352002-02-28 00:41:10 +00001598 FuncDef *pFirst, *p, *pMaybe;
1599 pFirst = p = (FuncDef*)sqliteHashFind(&db->aFunc, zName, nName);
drh1350b032002-02-27 19:00:20 +00001600 if( p && !createFlag && nArg<0 ){
drh8e0a2f92002-02-23 23:45:45 +00001601 while( p && p->xFunc==0 && p->xStep==0 ){ p = p->pNext; }
1602 return p;
1603 }
1604 pMaybe = 0;
1605 while( p && p->nArg!=nArg ){
1606 if( p->nArg<0 && !createFlag && (p->xFunc || p->xStep) ) pMaybe = p;
1607 p = p->pNext;
1608 }
1609 if( p && !createFlag && p->xFunc==0 && p->xStep==0 ){
1610 return 0;
1611 }
1612 if( p==0 && pMaybe ){
1613 assert( createFlag==0 );
1614 return pMaybe;
1615 }
drh89425d52002-02-28 03:04:48 +00001616 if( p==0 && createFlag && (p = sqliteMalloc(sizeof(*p)))!=0 ){
drh8e0a2f92002-02-23 23:45:45 +00001617 p->nArg = nArg;
1618 p->pNext = pFirst;
drhc9b84a12002-06-20 11:36:48 +00001619 p->dataType = pFirst ? pFirst->dataType : SQLITE_NUMERIC;
drh0bce8352002-02-28 00:41:10 +00001620 sqliteHashInsert(&db->aFunc, zName, nName, (void*)p);
drh8e0a2f92002-02-23 23:45:45 +00001621 }
1622 return p;
1623}