blob: eafc50ce8223c6149750c00404c3f0fa4241a1f8 [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**
drh8b8891b2004-03-17 23:32:08 +000015** $Id: expr.c,v 1.113 2004/03/17 23:32:08 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 ){
drh4efc4752004-01-16 15:55:37 +000029 /* When malloc fails, we leak memory from pLeft and pRight */
drha76b5df2002-02-23 02:32:10 +000030 return 0;
31 }
32 pNew->op = op;
33 pNew->pLeft = pLeft;
34 pNew->pRight = pRight;
35 if( pToken ){
drh4b59ab52002-08-24 18:24:51 +000036 assert( pToken->dyn==0 );
drha76b5df2002-02-23 02:32:10 +000037 pNew->token = *pToken;
drh6977fea2002-10-22 23:38:04 +000038 pNew->span = *pToken;
drha76b5df2002-02-23 02:32:10 +000039 }else{
drh4efc4752004-01-16 15:55:37 +000040 assert( pNew->token.dyn==0 );
41 assert( pNew->token.z==0 );
42 assert( pNew->token.n==0 );
drh6977fea2002-10-22 23:38:04 +000043 if( pLeft && pRight ){
44 sqliteExprSpan(pNew, &pLeft->span, &pRight->span);
45 }else{
46 pNew->span = pNew->token;
47 }
drha76b5df2002-02-23 02:32:10 +000048 }
drha76b5df2002-02-23 02:32:10 +000049 return pNew;
50}
51
52/*
drh6977fea2002-10-22 23:38:04 +000053** Set the Expr.span field of the given expression to span all
drha76b5df2002-02-23 02:32:10 +000054** text between the two given tokens.
55*/
56void sqliteExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
drh4efc4752004-01-16 15:55:37 +000057 assert( pRight!=0 );
58 assert( pLeft!=0 );
59 /* Note: pExpr might be NULL due to a prior malloc failure */
60 if( pExpr && pRight->z && pLeft->z ){
drh4b59ab52002-08-24 18:24:51 +000061 if( pLeft->dyn==0 && pRight->dyn==0 ){
drh6977fea2002-10-22 23:38:04 +000062 pExpr->span.z = pLeft->z;
63 pExpr->span.n = pRight->n + Addr(pRight->z) - Addr(pLeft->z);
drh4b59ab52002-08-24 18:24:51 +000064 }else{
drh6977fea2002-10-22 23:38:04 +000065 pExpr->span.z = 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 ){
drh4efc4752004-01-16 15:55:37 +000078 /* sqliteExprListDelete(pList); // Leak pList when malloc fails */
drha76b5df2002-02-23 02:32:10 +000079 return 0;
80 }
81 pNew->op = TK_FUNCTION;
82 pNew->pList = pList;
83 if( pToken ){
drh4b59ab52002-08-24 18:24:51 +000084 assert( pToken->dyn==0 );
drha76b5df2002-02-23 02:32:10 +000085 pNew->token = *pToken;
86 }else{
87 pNew->token.z = 0;
drha76b5df2002-02-23 02:32:10 +000088 }
drh6977fea2002-10-22 23:38:04 +000089 pNew->span = pNew->token;
drha76b5df2002-02-23 02:32:10 +000090 return pNew;
91}
92
93/*
drha2e00042002-01-22 03:13:42 +000094** Recursively delete an expression tree.
95*/
96void sqliteExprDelete(Expr *p){
97 if( p==0 ) return;
drh4efc4752004-01-16 15:55:37 +000098 if( p->span.dyn ) sqliteFree((char*)p->span.z);
99 if( p->token.dyn ) sqliteFree((char*)p->token.z);
100 sqliteExprDelete(p->pLeft);
101 sqliteExprDelete(p->pRight);
102 sqliteExprListDelete(p->pList);
103 sqliteSelectDelete(p->pSelect);
drha2e00042002-01-22 03:13:42 +0000104 sqliteFree(p);
105}
106
drha76b5df2002-02-23 02:32:10 +0000107
108/*
drhff78bd22002-02-27 01:47:11 +0000109** The following group of routines make deep copies of expressions,
110** expression lists, ID lists, and select statements. The copies can
111** be deleted (by being passed to their respective ...Delete() routines)
112** without effecting the originals.
113**
drhad3cab52002-05-24 02:04:32 +0000114** The expression list, ID, and source lists return by sqliteExprListDup(),
115** sqliteIdListDup(), and sqliteSrcListDup() can not be further expanded
116** by subsequent calls to sqlite*ListAppend() routines.
drhff78bd22002-02-27 01:47:11 +0000117**
drhad3cab52002-05-24 02:04:32 +0000118** Any tables that the SrcList might point to are not duplicated.
drhff78bd22002-02-27 01:47:11 +0000119*/
120Expr *sqliteExprDup(Expr *p){
121 Expr *pNew;
122 if( p==0 ) return 0;
drhfcb78a42003-01-18 20:11:05 +0000123 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000124 if( pNew==0 ) return 0;
drh3b167c72002-06-28 12:18:47 +0000125 memcpy(pNew, p, sizeof(*pNew));
drh6977fea2002-10-22 23:38:04 +0000126 if( p->token.z!=0 ){
drh4b59ab52002-08-24 18:24:51 +0000127 pNew->token.z = sqliteStrDup(p->token.z);
128 pNew->token.dyn = 1;
129 }else{
drh4efc4752004-01-16 15:55:37 +0000130 assert( pNew->token.z==0 );
drh4b59ab52002-08-24 18:24:51 +0000131 }
drh6977fea2002-10-22 23:38:04 +0000132 pNew->span.z = 0;
drhff78bd22002-02-27 01:47:11 +0000133 pNew->pLeft = sqliteExprDup(p->pLeft);
134 pNew->pRight = sqliteExprDup(p->pRight);
135 pNew->pList = sqliteExprListDup(p->pList);
drhff78bd22002-02-27 01:47:11 +0000136 pNew->pSelect = sqliteSelectDup(p->pSelect);
137 return pNew;
138}
drh4b59ab52002-08-24 18:24:51 +0000139void sqliteTokenCopy(Token *pTo, Token *pFrom){
140 if( pTo->dyn ) sqliteFree((char*)pTo->z);
drh4b59ab52002-08-24 18:24:51 +0000141 if( pFrom->z ){
142 pTo->n = pFrom->n;
143 pTo->z = sqliteStrNDup(pFrom->z, pFrom->n);
144 pTo->dyn = 1;
145 }else{
drh4b59ab52002-08-24 18:24:51 +0000146 pTo->z = 0;
drh4b59ab52002-08-24 18:24:51 +0000147 }
148}
drhff78bd22002-02-27 01:47:11 +0000149ExprList *sqliteExprListDup(ExprList *p){
150 ExprList *pNew;
drh3e7bc9c2004-02-21 19:17:17 +0000151 struct ExprList_item *pItem;
drhff78bd22002-02-27 01:47:11 +0000152 int i;
153 if( p==0 ) return 0;
154 pNew = sqliteMalloc( sizeof(*pNew) );
155 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000156 pNew->nExpr = pNew->nAlloc = p->nExpr;
drh3e7bc9c2004-02-21 19:17:17 +0000157 pNew->a = pItem = sqliteMalloc( p->nExpr*sizeof(p->a[0]) );
158 for(i=0; pItem && i<p->nExpr; i++, pItem++){
drh4b59ab52002-08-24 18:24:51 +0000159 Expr *pNewExpr, *pOldExpr;
drh3e7bc9c2004-02-21 19:17:17 +0000160 pItem->pExpr = pNewExpr = sqliteExprDup(pOldExpr = p->a[i].pExpr);
drh6977fea2002-10-22 23:38:04 +0000161 if( pOldExpr->span.z!=0 && pNewExpr ){
162 /* Always make a copy of the span for top-level expressions in the
drh4b59ab52002-08-24 18:24:51 +0000163 ** expression list. The logic in SELECT processing that determines
164 ** the names of columns in the result set needs this information */
drh6977fea2002-10-22 23:38:04 +0000165 sqliteTokenCopy(&pNewExpr->span, &pOldExpr->span);
drh4b59ab52002-08-24 18:24:51 +0000166 }
drh1f3e9052002-10-31 00:09:39 +0000167 assert( pNewExpr==0 || pNewExpr->span.z!=0
168 || pOldExpr->span.z==0 || sqlite_malloc_failed );
drh3e7bc9c2004-02-21 19:17:17 +0000169 pItem->zName = sqliteStrDup(p->a[i].zName);
170 pItem->sortOrder = p->a[i].sortOrder;
171 pItem->isAgg = p->a[i].isAgg;
172 pItem->done = 0;
drhff78bd22002-02-27 01:47:11 +0000173 }
174 return pNew;
175}
drhad3cab52002-05-24 02:04:32 +0000176SrcList *sqliteSrcListDup(SrcList *p){
177 SrcList *pNew;
178 int i;
drh113088e2003-03-20 01:16:58 +0000179 int nByte;
drhad3cab52002-05-24 02:04:32 +0000180 if( p==0 ) return 0;
drh113088e2003-03-20 01:16:58 +0000181 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
drh4efc4752004-01-16 15:55:37 +0000182 pNew = sqliteMallocRaw( nByte );
drhad3cab52002-05-24 02:04:32 +0000183 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000184 pNew->nSrc = pNew->nAlloc = p->nSrc;
drhad3cab52002-05-24 02:04:32 +0000185 for(i=0; i<p->nSrc; i++){
drh4efc4752004-01-16 15:55:37 +0000186 struct SrcList_item *pNewItem = &pNew->a[i];
187 struct SrcList_item *pOldItem = &p->a[i];
188 pNewItem->zDatabase = sqliteStrDup(pOldItem->zDatabase);
189 pNewItem->zName = sqliteStrDup(pOldItem->zName);
190 pNewItem->zAlias = sqliteStrDup(pOldItem->zAlias);
191 pNewItem->jointype = pOldItem->jointype;
192 pNewItem->iCursor = pOldItem->iCursor;
193 pNewItem->pTab = 0;
194 pNewItem->pSelect = sqliteSelectDup(pOldItem->pSelect);
195 pNewItem->pOn = sqliteExprDup(pOldItem->pOn);
196 pNewItem->pUsing = sqliteIdListDup(pOldItem->pUsing);
drhad3cab52002-05-24 02:04:32 +0000197 }
198 return pNew;
199}
drhff78bd22002-02-27 01:47:11 +0000200IdList *sqliteIdListDup(IdList *p){
201 IdList *pNew;
202 int i;
203 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000204 pNew = sqliteMallocRaw( sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +0000205 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000206 pNew->nId = pNew->nAlloc = p->nId;
drh4efc4752004-01-16 15:55:37 +0000207 pNew->a = sqliteMallocRaw( p->nId*sizeof(p->a[0]) );
drhe4697f52002-05-23 02:09:03 +0000208 if( pNew->a==0 ) return 0;
drhff78bd22002-02-27 01:47:11 +0000209 for(i=0; i<p->nId; i++){
drh4efc4752004-01-16 15:55:37 +0000210 struct IdList_item *pNewItem = &pNew->a[i];
211 struct IdList_item *pOldItem = &p->a[i];
212 pNewItem->zName = sqliteStrDup(pOldItem->zName);
213 pNewItem->idx = pOldItem->idx;
drhff78bd22002-02-27 01:47:11 +0000214 }
215 return pNew;
216}
217Select *sqliteSelectDup(Select *p){
218 Select *pNew;
219 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000220 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000221 if( pNew==0 ) return 0;
222 pNew->isDistinct = p->isDistinct;
223 pNew->pEList = sqliteExprListDup(p->pEList);
drhad3cab52002-05-24 02:04:32 +0000224 pNew->pSrc = sqliteSrcListDup(p->pSrc);
drhff78bd22002-02-27 01:47:11 +0000225 pNew->pWhere = sqliteExprDup(p->pWhere);
226 pNew->pGroupBy = sqliteExprListDup(p->pGroupBy);
227 pNew->pHaving = sqliteExprDup(p->pHaving);
228 pNew->pOrderBy = sqliteExprListDup(p->pOrderBy);
229 pNew->op = p->op;
230 pNew->pPrior = sqliteSelectDup(p->pPrior);
231 pNew->nLimit = p->nLimit;
232 pNew->nOffset = p->nOffset;
233 pNew->zSelect = 0;
drh7b58dae2003-07-20 01:16:46 +0000234 pNew->iLimit = -1;
235 pNew->iOffset = -1;
drhff78bd22002-02-27 01:47:11 +0000236 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){
drha76b5df2002-02-23 02:32:10 +0000245 if( pList==0 ){
246 pList = sqliteMalloc( sizeof(ExprList) );
247 if( pList==0 ){
drh4efc4752004-01-16 15:55:37 +0000248 /* sqliteExprDelete(pExpr); // Leak memory if malloc fails */
drha76b5df2002-02-23 02:32:10 +0000249 return 0;
250 }
drh4efc4752004-01-16 15:55:37 +0000251 assert( pList->nAlloc==0 );
drha76b5df2002-02-23 02:32:10 +0000252 }
drh4305d102003-07-30 12:34:12 +0000253 if( pList->nAlloc<=pList->nExpr ){
drh4305d102003-07-30 12:34:12 +0000254 pList->nAlloc = pList->nAlloc*2 + 4;
drh4efc4752004-01-16 15:55:37 +0000255 pList->a = sqliteRealloc(pList->a, pList->nAlloc*sizeof(pList->a[0]));
256 if( pList->a==0 ){
257 /* sqliteExprDelete(pExpr); // Leak memory if malloc fails */
258 pList->nExpr = pList->nAlloc = 0;
drha76b5df2002-02-23 02:32:10 +0000259 return pList;
260 }
drha76b5df2002-02-23 02:32:10 +0000261 }
drh4efc4752004-01-16 15:55:37 +0000262 assert( pList->a!=0 );
263 if( pExpr || pName ){
264 struct ExprList_item *pItem = &pList->a[pList->nExpr++];
265 memset(pItem, 0, sizeof(*pItem));
266 pItem->pExpr = pExpr;
drha76b5df2002-02-23 02:32:10 +0000267 if( pName ){
drh4efc4752004-01-16 15:55:37 +0000268 sqliteSetNString(&pItem->zName, pName->z, pName->n, 0);
269 sqliteDequote(pItem->zName);
drha76b5df2002-02-23 02:32:10 +0000270 }
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:
drh50457892003-09-06 01:10:47 +0000308 case TK_VARIABLE:
drh92086432002-01-22 14:11:29 +0000309 return 1;
drhfef52082000-06-06 01:50:43 +0000310 default: {
drh92086432002-01-22 14:11:29 +0000311 if( p->pLeft && !sqliteExprIsConstant(p->pLeft) ) return 0;
312 if( p->pRight && !sqliteExprIsConstant(p->pRight) ) return 0;
drhfef52082000-06-06 01:50:43 +0000313 if( p->pList ){
314 int i;
315 for(i=0; i<p->pList->nExpr; i++){
drh92086432002-01-22 14:11:29 +0000316 if( !sqliteExprIsConstant(p->pList->a[i].pExpr) ) return 0;
drhfef52082000-06-06 01:50:43 +0000317 }
318 }
drh92086432002-01-22 14:11:29 +0000319 return p->pLeft!=0 || p->pRight!=0 || (p->pList && p->pList->nExpr>0);
drhfef52082000-06-06 01:50:43 +0000320 }
321 }
drh92086432002-01-22 14:11:29 +0000322 return 0;
drhfef52082000-06-06 01:50:43 +0000323}
324
325/*
drh202b2df2004-01-06 01:13:46 +0000326** If the given expression codes a constant integer that is small enough
327** to fit in a 32-bit integer, return 1 and put the value of the integer
328** in *pValue. If the expression is not an integer or if it is too big
329** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
drhe4de1fe2002-06-02 16:09:01 +0000330*/
331int sqliteExprIsInteger(Expr *p, int *pValue){
332 switch( p->op ){
333 case TK_INTEGER: {
drh202b2df2004-01-06 01:13:46 +0000334 if( sqliteFitsIn32Bits(p->token.z) ){
335 *pValue = atoi(p->token.z);
336 return 1;
337 }
338 break;
drhe4de1fe2002-06-02 16:09:01 +0000339 }
340 case TK_STRING: {
drhbd790ee2002-06-02 18:22:06 +0000341 const char *z = p->token.z;
drhe4de1fe2002-06-02 16:09:01 +0000342 int n = p->token.n;
drhbd790ee2002-06-02 18:22:06 +0000343 if( n>0 && z[0]=='-' ){ z++; n--; }
drhe4de1fe2002-06-02 16:09:01 +0000344 while( n>0 && *z && isdigit(*z) ){ z++; n--; }
drh202b2df2004-01-06 01:13:46 +0000345 if( n==0 && sqliteFitsIn32Bits(p->token.z) ){
drhe4de1fe2002-06-02 16:09:01 +0000346 *pValue = atoi(p->token.z);
347 return 1;
348 }
349 break;
350 }
drh4b59ab52002-08-24 18:24:51 +0000351 case TK_UPLUS: {
352 return sqliteExprIsInteger(p->pLeft, pValue);
353 }
drhe4de1fe2002-06-02 16:09:01 +0000354 case TK_UMINUS: {
355 int v;
356 if( sqliteExprIsInteger(p->pLeft, &v) ){
357 *pValue = -v;
358 return 1;
359 }
360 break;
361 }
362 default: break;
363 }
364 return 0;
365}
366
367/*
drhc4a3c772001-04-04 11:48:57 +0000368** Return TRUE if the given string is a row-id column name.
369*/
drha9f9d1c2002-06-29 02:20:08 +0000370int sqliteIsRowid(const char *z){
drhc4a3c772001-04-04 11:48:57 +0000371 if( sqliteStrICmp(z, "_ROWID_")==0 ) return 1;
372 if( sqliteStrICmp(z, "ROWID")==0 ) return 1;
373 if( sqliteStrICmp(z, "OID")==0 ) return 1;
374 return 0;
375}
376
377/*
drh8141f612004-01-25 22:44:58 +0000378** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
379** that name in the set of source tables in pSrcList and make the pExpr
380** expression node refer back to that source column. The following changes
381** are made to pExpr:
382**
383** pExpr->iDb Set the index in db->aDb[] of the database holding
384** the table.
385** pExpr->iTable Set to the cursor number for the table obtained
386** from pSrcList.
387** pExpr->iColumn Set to the column number within the table.
388** pExpr->dataType Set to the appropriate data type for the column.
389** pExpr->op Set to TK_COLUMN.
390** pExpr->pLeft Any expression this points to is deleted
391** pExpr->pRight Any expression this points to is deleted.
392**
393** The pDbToken is the name of the database (the "X"). This value may be
394** NULL meaning that name is of the form Y.Z or Z. Any available database
395** can be used. The pTableToken is the name of the table (the "Y"). This
396** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it
397** means that the form of the name is Z and that columns from any table
398** can be used.
399**
400** If the name cannot be resolved unambiguously, leave an error message
401** in pParse and return non-zero. Return zero on success.
402*/
403static int lookupName(
404 Parse *pParse, /* The parsing context */
405 Token *pDbToken, /* Name of the database containing table, or NULL */
406 Token *pTableToken, /* Name of table containing column, or NULL */
407 Token *pColumnToken, /* Name of the column. */
408 SrcList *pSrcList, /* List of tables used to resolve column names */
409 ExprList *pEList, /* List of expressions used to resolve "AS" */
410 Expr *pExpr /* Make this EXPR node point to the selected column */
411){
412 char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */
413 char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */
414 char *zCol = 0; /* Name of the column. The "Z" */
415 int i, j; /* Loop counters */
416 int cnt = 0; /* Number of matching column names */
417 int cntTab = 0; /* Number of matching table names */
drh7e26d752004-02-11 10:35:29 +0000418 sqlite *db = pParse->db; /* The database */
drh8141f612004-01-25 22:44:58 +0000419
420 assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
421 if( pDbToken && pDbToken->z ){
422 zDb = sqliteStrNDup(pDbToken->z, pDbToken->n);
423 sqliteDequote(zDb);
424 }else{
425 zDb = 0;
426 }
427 if( pTableToken && pTableToken->z ){
428 zTab = sqliteStrNDup(pTableToken->z, pTableToken->n);
429 sqliteDequote(zTab);
430 }else{
431 assert( zDb==0 );
432 zTab = 0;
433 }
434 zCol = sqliteStrNDup(pColumnToken->z, pColumnToken->n);
435 sqliteDequote(zCol);
436 if( sqlite_malloc_failed ){
437 return 1; /* Leak memory (zDb and zTab) if malloc fails */
438 }
439 assert( zTab==0 || pEList==0 );
440
441 pExpr->iTable = -1;
442 for(i=0; i<pSrcList->nSrc; i++){
443 struct SrcList_item *pItem = &pSrcList->a[i];
444 Table *pTab = pItem->pTab;
445 Column *pCol;
446
447 if( pTab==0 ) continue;
448 assert( pTab->nCol>0 );
449 if( zTab ){
450 if( pItem->zAlias ){
451 char *zTabName = pItem->zAlias;
452 if( sqliteStrICmp(zTabName, zTab)!=0 ) continue;
453 }else{
454 char *zTabName = pTab->zName;
455 if( zTabName==0 || sqliteStrICmp(zTabName, zTab)!=0 ) continue;
456 if( zDb!=0 && sqliteStrICmp(db->aDb[pTab->iDb].zName, zDb)!=0 ){
457 continue;
458 }
459 }
460 }
461 if( 0==(cntTab++) ){
462 pExpr->iTable = pItem->iCursor;
463 pExpr->iDb = pTab->iDb;
464 }
465 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
466 if( sqliteStrICmp(pCol->zName, zCol)==0 ){
467 cnt++;
468 pExpr->iTable = pItem->iCursor;
469 pExpr->iDb = pTab->iDb;
470 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
471 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
472 pExpr->dataType = pCol->sortOrder & SQLITE_SO_TYPEMASK;
473 break;
474 }
475 }
476 }
477
478 /* If we have not already resolved the name, then maybe
479 ** it is a new.* or old.* trigger argument reference
480 */
481 if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
482 TriggerStack *pTriggerStack = pParse->trigStack;
483 Table *pTab = 0;
484 if( pTriggerStack->newIdx != -1 && sqliteStrICmp("new", zTab) == 0 ){
485 pExpr->iTable = pTriggerStack->newIdx;
486 assert( pTriggerStack->pTab );
487 pTab = pTriggerStack->pTab;
488 }else if( pTriggerStack->oldIdx != -1 && sqliteStrICmp("old", zTab) == 0 ){
489 pExpr->iTable = pTriggerStack->oldIdx;
490 assert( pTriggerStack->pTab );
491 pTab = pTriggerStack->pTab;
492 }
493
494 if( pTab ){
495 int j;
496 Column *pCol = pTab->aCol;
497
498 pExpr->iDb = pTab->iDb;
499 cntTab++;
500 for(j=0; j < pTab->nCol; j++, pCol++) {
501 if( sqliteStrICmp(pCol->zName, zCol)==0 ){
502 cnt++;
503 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
504 pExpr->dataType = pCol->sortOrder & SQLITE_SO_TYPEMASK;
505 break;
506 }
507 }
508 }
509 }
510
511 /*
512 ** Perhaps the name is a reference to the ROWID
513 */
514 if( cnt==0 && cntTab==1 && sqliteIsRowid(zCol) ){
515 cnt = 1;
516 pExpr->iColumn = -1;
517 pExpr->dataType = SQLITE_SO_NUM;
518 }
519
520 /*
521 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
522 ** might refer to an result-set alias. This happens, for example, when
523 ** we are resolving names in the WHERE clause of the following command:
524 **
525 ** SELECT a+b AS x FROM table WHERE x<10;
526 **
527 ** In cases like this, replace pExpr with a copy of the expression that
528 ** forms the result set entry ("a+b" in the example) and return immediately.
529 ** Note that the expression in the result set should have already been
530 ** resolved by the time the WHERE clause is resolved.
531 */
532 if( cnt==0 && pEList!=0 ){
533 for(j=0; j<pEList->nExpr; j++){
534 char *zAs = pEList->a[j].zName;
535 if( zAs!=0 && sqliteStrICmp(zAs, zCol)==0 ){
536 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
537 pExpr->op = TK_AS;
538 pExpr->iColumn = j;
539 pExpr->pLeft = sqliteExprDup(pEList->a[j].pExpr);
540 sqliteFree(zCol);
541 assert( zTab==0 && zDb==0 );
542 return 0;
543 }
544 }
545 }
546
547 /*
548 ** If X and Y are NULL (in other words if only the column name Z is
549 ** supplied) and the value of Z is enclosed in double-quotes, then
550 ** Z is a string literal if it doesn't match any column names. In that
551 ** case, we need to return right away and not make any changes to
552 ** pExpr.
553 */
554 if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
555 sqliteFree(zCol);
556 return 0;
557 }
558
559 /*
560 ** cnt==0 means there was not match. cnt>1 means there were two or
561 ** more matches. Either way, we have an error.
562 */
563 if( cnt!=1 ){
564 char *z = 0;
565 char *zErr;
566 zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s";
567 if( zDb ){
568 sqliteSetString(&z, zDb, ".", zTab, ".", zCol, 0);
569 }else if( zTab ){
570 sqliteSetString(&z, zTab, ".", zCol, 0);
571 }else{
572 z = sqliteStrDup(zCol);
573 }
574 sqliteErrorMsg(pParse, zErr, z);
575 sqliteFree(z);
576 }
577
578 /* Clean up and return
579 */
580 sqliteFree(zDb);
581 sqliteFree(zTab);
582 sqliteFree(zCol);
583 sqliteExprDelete(pExpr->pLeft);
584 pExpr->pLeft = 0;
585 sqliteExprDelete(pExpr->pRight);
586 pExpr->pRight = 0;
587 pExpr->op = TK_COLUMN;
588 sqliteAuthRead(pParse, pExpr, pSrcList);
589 return cnt!=1;
590}
591
592/*
drhcce7d172000-05-31 15:34:51 +0000593** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +0000594** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +0000595** index to the table in the table list and a column offset. The
596** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
597** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +0000598** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +0000599** VDBE cursor number for a cursor that is pointing into the referenced
600** table. The Expr.iColumn value is changed to the index of the column
601** of the referenced table. The Expr.iColumn value for the special
602** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
603** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +0000604**
drhfef52082000-06-06 01:50:43 +0000605** We also check for instances of the IN operator. IN comes in two
606** forms:
607**
608** expr IN (exprlist)
609** and
610** expr IN (SELECT ...)
611**
612** The first form is handled by creating a set holding the list
613** of allowed values. The second form causes the SELECT to generate
614** a temporary table.
615**
616** This routine also looks for scalar SELECTs that are part of an expression.
drh19a775c2000-06-05 18:54:46 +0000617** If it finds any, it generates code to write the value of that select
618** into a memory cell.
drhcce7d172000-05-31 15:34:51 +0000619**
drh967e8b72000-06-21 13:59:10 +0000620** Unknown columns or tables provoke an error. The function returns
drhcce7d172000-05-31 15:34:51 +0000621** the number of errors seen and leaves an error message on pParse->zErrMsg.
622*/
drha2e00042002-01-22 03:13:42 +0000623int sqliteExprResolveIds(
624 Parse *pParse, /* The parser context */
drh8141f612004-01-25 22:44:58 +0000625 SrcList *pSrcList, /* List of tables used to resolve column names */
drha2e00042002-01-22 03:13:42 +0000626 ExprList *pEList, /* List of expressions used to resolve "AS" */
627 Expr *pExpr /* The expression to be analyzed. */
628){
drh6a3ea0e2003-05-02 14:32:12 +0000629 int i;
630
drh8141f612004-01-25 22:44:58 +0000631 if( pExpr==0 || pSrcList==0 ) return 0;
632 for(i=0; i<pSrcList->nSrc; i++){
633 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab );
drh6a3ea0e2003-05-02 14:32:12 +0000634 }
drhcce7d172000-05-31 15:34:51 +0000635 switch( pExpr->op ){
drh23989372002-05-21 13:43:04 +0000636 /* Double-quoted strings (ex: "abc") are used as identifiers if
637 ** possible. Otherwise they remain as strings. Single-quoted
638 ** strings (ex: 'abc') are always string literals.
639 */
640 case TK_STRING: {
641 if( pExpr->token.z[0]=='\'' ) break;
642 /* Fall thru into the TK_ID case if this is a double-quoted string */
643 }
drh8141f612004-01-25 22:44:58 +0000644 /* A lone identifier is the name of a columnd.
drha2e00042002-01-22 03:13:42 +0000645 */
drhcce7d172000-05-31 15:34:51 +0000646 case TK_ID: {
drh8141f612004-01-25 22:44:58 +0000647 if( lookupName(pParse, 0, 0, &pExpr->token, pSrcList, pEList, pExpr) ){
drhcce7d172000-05-31 15:34:51 +0000648 return 1;
drhed6c8672003-01-12 18:02:16 +0000649 }
drhcce7d172000-05-31 15:34:51 +0000650 break;
651 }
652
drhd24cc422003-03-27 12:51:24 +0000653 /* A table name and column name: ID.ID
654 ** Or a database, table and column: ID.ID.ID
655 */
drhcce7d172000-05-31 15:34:51 +0000656 case TK_DOT: {
drh8141f612004-01-25 22:44:58 +0000657 Token *pColumn;
658 Token *pTable;
659 Token *pDb;
660 Expr *pRight;
drhcce7d172000-05-31 15:34:51 +0000661
drhcce7d172000-05-31 15:34:51 +0000662 pRight = pExpr->pRight;
drhd24cc422003-03-27 12:51:24 +0000663 if( pRight->op==TK_ID ){
drh8141f612004-01-25 22:44:58 +0000664 pDb = 0;
665 pTable = &pExpr->pLeft->token;
666 pColumn = &pRight->token;
drhd24cc422003-03-27 12:51:24 +0000667 }else{
drh8141f612004-01-25 22:44:58 +0000668 assert( pRight->op==TK_DOT );
669 pDb = &pExpr->pLeft->token;
670 pTable = &pRight->pLeft->token;
671 pColumn = &pRight->pRight->token;
drhd24cc422003-03-27 12:51:24 +0000672 }
drh8141f612004-01-25 22:44:58 +0000673 if( lookupName(pParse, pDb, pTable, pColumn, pSrcList, 0, pExpr) ){
drhdaffd0e2001-04-11 14:28:42 +0000674 return 1;
675 }
drhcce7d172000-05-31 15:34:51 +0000676 break;
677 }
678
drhfef52082000-06-06 01:50:43 +0000679 case TK_IN: {
drhd8bc7082000-06-07 23:51:50 +0000680 Vdbe *v = sqliteGetVdbe(pParse);
drhfef52082000-06-06 01:50:43 +0000681 if( v==0 ) return 1;
drh8141f612004-01-25 22:44:58 +0000682 if( sqliteExprResolveIds(pParse, pSrcList, pEList, pExpr->pLeft) ){
drhcfab11b2000-06-06 03:31:22 +0000683 return 1;
684 }
drhfef52082000-06-06 01:50:43 +0000685 if( pExpr->pSelect ){
686 /* Case 1: expr IN (SELECT ...)
687 **
688 ** Generate code to write the results of the select into a temporary
drh4794b982000-06-06 13:54:14 +0000689 ** table. The cursor number of the temporary table has already
690 ** been put in iTable by sqliteExprResolveInSelect().
drhfef52082000-06-06 01:50:43 +0000691 */
drh832508b2002-03-02 17:04:07 +0000692 pExpr->iTable = pParse->nTab++;
drhc6b52df2002-01-04 03:09:29 +0000693 sqliteVdbeAddOp(v, OP_OpenTemp, pExpr->iTable, 1);
drh832508b2002-03-02 17:04:07 +0000694 sqliteSelect(pParse, pExpr->pSelect, SRT_Set, pExpr->iTable, 0,0,0);
drhfef52082000-06-06 01:50:43 +0000695 }else if( pExpr->pList ){
696 /* Case 2: expr IN (exprlist)
697 **
698 ** Create a set to put the exprlist values in. The Set id is stored
699 ** in iTable.
700 */
701 int i, iSet;
702 for(i=0; i<pExpr->pList->nExpr; i++){
703 Expr *pE2 = pExpr->pList->a[i].pExpr;
drh92086432002-01-22 14:11:29 +0000704 if( !sqliteExprIsConstant(pE2) ){
drhda93d232003-03-31 02:12:46 +0000705 sqliteErrorMsg(pParse,
706 "right-hand side of IN operator must be constant");
drhfef52082000-06-06 01:50:43 +0000707 return 1;
708 }
drh4794b982000-06-06 13:54:14 +0000709 if( sqliteExprCheck(pParse, pE2, 0, 0) ){
710 return 1;
711 }
drhfef52082000-06-06 01:50:43 +0000712 }
713 iSet = pExpr->iTable = pParse->nSet++;
714 for(i=0; i<pExpr->pList->nExpr; i++){
715 Expr *pE2 = pExpr->pList->a[i].pExpr;
716 switch( pE2->op ){
717 case TK_FLOAT:
718 case TK_INTEGER:
719 case TK_STRING: {
drh701a0ae2004-02-22 20:05:00 +0000720 int addr;
drha76b5df2002-02-23 02:32:10 +0000721 assert( pE2->token.z );
drh701a0ae2004-02-22 20:05:00 +0000722 addr = sqliteVdbeOp3(v, OP_SetInsert, iSet, 0,
723 pE2->token.z, pE2->token.n);
drhfef52082000-06-06 01:50:43 +0000724 sqliteVdbeDequoteP3(v, addr);
725 break;
726 }
727 default: {
728 sqliteExprCode(pParse, pE2);
drh99fcd712001-10-13 01:06:47 +0000729 sqliteVdbeAddOp(v, OP_SetInsert, iSet, 0);
drhfef52082000-06-06 01:50:43 +0000730 break;
731 }
732 }
733 }
734 }
drhcfab11b2000-06-06 03:31:22 +0000735 break;
drhfef52082000-06-06 01:50:43 +0000736 }
737
drh19a775c2000-06-05 18:54:46 +0000738 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +0000739 /* This has to be a scalar SELECT. Generate code to put the
740 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +0000741 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +0000742 */
drh967e8b72000-06-21 13:59:10 +0000743 pExpr->iColumn = pParse->nMem++;
drh832508b2002-03-02 17:04:07 +0000744 if( sqliteSelect(pParse, pExpr->pSelect, SRT_Mem, pExpr->iColumn,0,0,0) ){
drh19a775c2000-06-05 18:54:46 +0000745 return 1;
746 }
747 break;
748 }
749
drhcce7d172000-05-31 15:34:51 +0000750 /* For all else, just recursively walk the tree */
751 default: {
drh4794b982000-06-06 13:54:14 +0000752 if( pExpr->pLeft
drh8141f612004-01-25 22:44:58 +0000753 && sqliteExprResolveIds(pParse, pSrcList, pEList, pExpr->pLeft) ){
drhcce7d172000-05-31 15:34:51 +0000754 return 1;
755 }
756 if( pExpr->pRight
drh8141f612004-01-25 22:44:58 +0000757 && sqliteExprResolveIds(pParse, pSrcList, pEList, pExpr->pRight) ){
drhcce7d172000-05-31 15:34:51 +0000758 return 1;
759 }
760 if( pExpr->pList ){
761 int i;
762 ExprList *pList = pExpr->pList;
763 for(i=0; i<pList->nExpr; i++){
drh832508b2002-03-02 17:04:07 +0000764 Expr *pArg = pList->a[i].pExpr;
drh8141f612004-01-25 22:44:58 +0000765 if( sqliteExprResolveIds(pParse, pSrcList, pEList, pArg) ){
drhcce7d172000-05-31 15:34:51 +0000766 return 1;
767 }
768 }
769 }
770 }
771 }
772 return 0;
773}
774
drhcce7d172000-05-31 15:34:51 +0000775/*
drh4b59ab52002-08-24 18:24:51 +0000776** pExpr is a node that defines a function of some kind. It might
777** be a syntactic function like "count(x)" or it might be a function
778** that implements an operator, like "a LIKE b".
779**
780** This routine makes *pzName point to the name of the function and
781** *pnName hold the number of characters in the function name.
782*/
783static void getFunctionName(Expr *pExpr, const char **pzName, int *pnName){
784 switch( pExpr->op ){
785 case TK_FUNCTION: {
786 *pzName = pExpr->token.z;
drh6977fea2002-10-22 23:38:04 +0000787 *pnName = pExpr->token.n;
drh4b59ab52002-08-24 18:24:51 +0000788 break;
789 }
790 case TK_LIKE: {
791 *pzName = "like";
792 *pnName = 4;
793 break;
794 }
795 case TK_GLOB: {
796 *pzName = "glob";
797 *pnName = 4;
798 break;
799 }
800 default: {
801 *pzName = "can't happen";
802 *pnName = 12;
803 break;
804 }
805 }
806}
807
808/*
drhcce7d172000-05-31 15:34:51 +0000809** Error check the functions in an expression. Make sure all
810** function names are recognized and all functions have the correct
811** number of arguments. Leave an error message in pParse->zErrMsg
812** if anything is amiss. Return the number of errors.
813**
814** if pIsAgg is not null and this expression is an aggregate function
815** (like count(*) or max(value)) then write a 1 into *pIsAgg.
816*/
817int sqliteExprCheck(Parse *pParse, Expr *pExpr, int allowAgg, int *pIsAgg){
818 int nErr = 0;
819 if( pExpr==0 ) return 0;
drhcce7d172000-05-31 15:34:51 +0000820 switch( pExpr->op ){
drh4b59ab52002-08-24 18:24:51 +0000821 case TK_GLOB:
822 case TK_LIKE:
drhcce7d172000-05-31 15:34:51 +0000823 case TK_FUNCTION: {
drhc9b84a12002-06-20 11:36:48 +0000824 int n = pExpr->pList ? pExpr->pList->nExpr : 0; /* Number of arguments */
825 int no_such_func = 0; /* True if no such function exists */
drhc9b84a12002-06-20 11:36:48 +0000826 int wrong_num_args = 0; /* True if wrong number of arguments */
827 int is_agg = 0; /* True if is an aggregate function */
drhcce7d172000-05-31 15:34:51 +0000828 int i;
drh4b59ab52002-08-24 18:24:51 +0000829 int nId; /* Number of characters in function name */
830 const char *zId; /* The function name. */
drh0bce8352002-02-28 00:41:10 +0000831 FuncDef *pDef;
832
drh4b59ab52002-08-24 18:24:51 +0000833 getFunctionName(pExpr, &zId, &nId);
834 pDef = sqliteFindFunction(pParse->db, zId, nId, n, 0);
drh0bce8352002-02-28 00:41:10 +0000835 if( pDef==0 ){
drh4b59ab52002-08-24 18:24:51 +0000836 pDef = sqliteFindFunction(pParse->db, zId, nId, -1, 0);
drh0bce8352002-02-28 00:41:10 +0000837 if( pDef==0 ){
drh268380c2004-02-25 13:47:31 +0000838 no_such_func = 1;
drh0bce8352002-02-28 00:41:10 +0000839 }else{
840 wrong_num_args = 1;
drhcce7d172000-05-31 15:34:51 +0000841 }
drh0bce8352002-02-28 00:41:10 +0000842 }else{
843 is_agg = pDef->xFunc==0;
drhcce7d172000-05-31 15:34:51 +0000844 }
drh8e0a2f92002-02-23 23:45:45 +0000845 if( is_agg && !allowAgg ){
drhf7a9e1a2004-02-22 18:40:56 +0000846 sqliteErrorMsg(pParse, "misuse of aggregate function %.*s()", nId, zId);
drh8e0a2f92002-02-23 23:45:45 +0000847 nErr++;
848 is_agg = 0;
849 }else if( no_such_func ){
drhf7a9e1a2004-02-22 18:40:56 +0000850 sqliteErrorMsg(pParse, "no such function: %.*s", nId, zId);
drhcce7d172000-05-31 15:34:51 +0000851 nErr++;
drh8e0a2f92002-02-23 23:45:45 +0000852 }else if( wrong_num_args ){
drhf7a9e1a2004-02-22 18:40:56 +0000853 sqliteErrorMsg(pParse,"wrong number of arguments to function %.*s()",
854 nId, zId);
drh8e0a2f92002-02-23 23:45:45 +0000855 nErr++;
drhcce7d172000-05-31 15:34:51 +0000856 }
drhf7a9e1a2004-02-22 18:40:56 +0000857 if( is_agg ){
858 pExpr->op = TK_AGG_FUNCTION;
859 if( pIsAgg ) *pIsAgg = 1;
860 }
drhcce7d172000-05-31 15:34:51 +0000861 for(i=0; nErr==0 && i<n; i++){
drh4cfa7932000-06-08 15:10:46 +0000862 nErr = sqliteExprCheck(pParse, pExpr->pList->a[i].pExpr,
863 allowAgg && !is_agg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000864 }
drhc9b84a12002-06-20 11:36:48 +0000865 if( pDef==0 ){
drh268380c2004-02-25 13:47:31 +0000866 /* Already reported an error */
drhc9b84a12002-06-20 11:36:48 +0000867 }else if( pDef->dataType>=0 ){
868 if( pDef->dataType<n ){
869 pExpr->dataType =
870 sqliteExprType(pExpr->pList->a[pDef->dataType].pExpr);
871 }else{
872 pExpr->dataType = SQLITE_SO_NUM;
873 }
874 }else if( pDef->dataType==SQLITE_ARGS ){
875 pDef->dataType = SQLITE_SO_TEXT;
876 for(i=0; i<n; i++){
877 if( sqliteExprType(pExpr->pList->a[i].pExpr)==SQLITE_SO_NUM ){
878 pExpr->dataType = SQLITE_SO_NUM;
879 break;
880 }
881 }
882 }else if( pDef->dataType==SQLITE_NUMERIC ){
883 pExpr->dataType = SQLITE_SO_NUM;
884 }else{
885 pExpr->dataType = SQLITE_SO_TEXT;
886 }
drhcce7d172000-05-31 15:34:51 +0000887 }
888 default: {
889 if( pExpr->pLeft ){
drh22827922000-06-06 17:27:05 +0000890 nErr = sqliteExprCheck(pParse, pExpr->pLeft, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000891 }
892 if( nErr==0 && pExpr->pRight ){
drh22827922000-06-06 17:27:05 +0000893 nErr = sqliteExprCheck(pParse, pExpr->pRight, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000894 }
drhfef52082000-06-06 01:50:43 +0000895 if( nErr==0 && pExpr->pList ){
896 int n = pExpr->pList->nExpr;
897 int i;
898 for(i=0; nErr==0 && i<n; i++){
drh22827922000-06-06 17:27:05 +0000899 Expr *pE2 = pExpr->pList->a[i].pExpr;
900 nErr = sqliteExprCheck(pParse, pE2, allowAgg, pIsAgg);
drhfef52082000-06-06 01:50:43 +0000901 }
902 }
drhcce7d172000-05-31 15:34:51 +0000903 break;
904 }
905 }
906 return nErr;
907}
908
909/*
drhc9b84a12002-06-20 11:36:48 +0000910** Return either SQLITE_SO_NUM or SQLITE_SO_TEXT to indicate whether the
911** given expression should sort as numeric values or as text.
912**
913** The sqliteExprResolveIds() and sqliteExprCheck() routines must have
914** both been called on the expression before it is passed to this routine.
915*/
916int sqliteExprType(Expr *p){
917 if( p==0 ) return SQLITE_SO_NUM;
918 while( p ) switch( p->op ){
919 case TK_PLUS:
920 case TK_MINUS:
921 case TK_STAR:
922 case TK_SLASH:
923 case TK_AND:
924 case TK_OR:
925 case TK_ISNULL:
926 case TK_NOTNULL:
927 case TK_NOT:
928 case TK_UMINUS:
drh4b59ab52002-08-24 18:24:51 +0000929 case TK_UPLUS:
drhc9b84a12002-06-20 11:36:48 +0000930 case TK_BITAND:
931 case TK_BITOR:
932 case TK_BITNOT:
933 case TK_LSHIFT:
934 case TK_RSHIFT:
935 case TK_REM:
936 case TK_INTEGER:
937 case TK_FLOAT:
938 case TK_IN:
939 case TK_BETWEEN:
drh4b59ab52002-08-24 18:24:51 +0000940 case TK_GLOB:
941 case TK_LIKE:
drhc9b84a12002-06-20 11:36:48 +0000942 return SQLITE_SO_NUM;
943
944 case TK_STRING:
945 case TK_NULL:
946 case TK_CONCAT:
drh50457892003-09-06 01:10:47 +0000947 case TK_VARIABLE:
drhc9b84a12002-06-20 11:36:48 +0000948 return SQLITE_SO_TEXT;
949
950 case TK_LT:
951 case TK_LE:
952 case TK_GT:
953 case TK_GE:
954 case TK_NE:
955 case TK_EQ:
956 if( sqliteExprType(p->pLeft)==SQLITE_SO_NUM ){
957 return SQLITE_SO_NUM;
958 }
959 p = p->pRight;
960 break;
961
962 case TK_AS:
963 p = p->pLeft;
964 break;
965
966 case TK_COLUMN:
967 case TK_FUNCTION:
968 case TK_AGG_FUNCTION:
969 return p->dataType;
970
971 case TK_SELECT:
972 assert( p->pSelect );
973 assert( p->pSelect->pEList );
974 assert( p->pSelect->pEList->nExpr>0 );
975 p = p->pSelect->pEList->a[0].pExpr;
976 break;
977
drhb1363202002-06-26 02:45:03 +0000978 case TK_CASE: {
979 if( p->pRight && sqliteExprType(p->pRight)==SQLITE_SO_NUM ){
980 return SQLITE_SO_NUM;
981 }
982 if( p->pList ){
983 int i;
984 ExprList *pList = p->pList;
985 for(i=1; i<pList->nExpr; i+=2){
986 if( sqliteExprType(pList->a[i].pExpr)==SQLITE_SO_NUM ){
987 return SQLITE_SO_NUM;
988 }
989 }
990 }
991 return SQLITE_SO_TEXT;
992 }
993
drhc9b84a12002-06-20 11:36:48 +0000994 default:
995 assert( p->op==TK_ABORT ); /* Can't Happen */
996 break;
997 }
998 return SQLITE_SO_NUM;
999}
1000
1001/*
drhcce7d172000-05-31 15:34:51 +00001002** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +00001003** expression and leave the result on the top of stack.
drhcce7d172000-05-31 15:34:51 +00001004*/
1005void sqliteExprCode(Parse *pParse, Expr *pExpr){
1006 Vdbe *v = pParse->pVdbe;
1007 int op;
drhdaffd0e2001-04-11 14:28:42 +00001008 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001009 switch( pExpr->op ){
1010 case TK_PLUS: op = OP_Add; break;
1011 case TK_MINUS: op = OP_Subtract; break;
1012 case TK_STAR: op = OP_Multiply; break;
1013 case TK_SLASH: op = OP_Divide; break;
1014 case TK_AND: op = OP_And; break;
1015 case TK_OR: op = OP_Or; break;
1016 case TK_LT: op = OP_Lt; break;
1017 case TK_LE: op = OP_Le; break;
1018 case TK_GT: op = OP_Gt; break;
1019 case TK_GE: op = OP_Ge; break;
1020 case TK_NE: op = OP_Ne; break;
1021 case TK_EQ: op = OP_Eq; break;
drhcce7d172000-05-31 15:34:51 +00001022 case TK_ISNULL: op = OP_IsNull; break;
1023 case TK_NOTNULL: op = OP_NotNull; break;
1024 case TK_NOT: op = OP_Not; break;
1025 case TK_UMINUS: op = OP_Negative; break;
drhbf4133c2001-10-13 02:59:08 +00001026 case TK_BITAND: op = OP_BitAnd; break;
1027 case TK_BITOR: op = OP_BitOr; break;
1028 case TK_BITNOT: op = OP_BitNot; break;
1029 case TK_LSHIFT: op = OP_ShiftLeft; break;
1030 case TK_RSHIFT: op = OP_ShiftRight; break;
1031 case TK_REM: op = OP_Remainder; break;
drhcce7d172000-05-31 15:34:51 +00001032 default: break;
1033 }
1034 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00001035 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +00001036 if( pParse->useAgg ){
drh99fcd712001-10-13 01:06:47 +00001037 sqliteVdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drhc4a3c772001-04-04 11:48:57 +00001038 }else if( pExpr->iColumn>=0 ){
drh99fcd712001-10-13 01:06:47 +00001039 sqliteVdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn);
drhc4a3c772001-04-04 11:48:57 +00001040 }else{
drh99fcd712001-10-13 01:06:47 +00001041 sqliteVdbeAddOp(v, OP_Recno, pExpr->iTable, 0);
drh22827922000-06-06 17:27:05 +00001042 }
drhcce7d172000-05-31 15:34:51 +00001043 break;
1044 }
drh51e9a442004-01-16 16:42:53 +00001045 case TK_STRING:
1046 case TK_FLOAT:
drhcce7d172000-05-31 15:34:51 +00001047 case TK_INTEGER: {
drh51e9a442004-01-16 16:42:53 +00001048 if( pExpr->op==TK_INTEGER && sqliteFitsIn32Bits(pExpr->token.z) ){
drh202b2df2004-01-06 01:13:46 +00001049 sqliteVdbeAddOp(v, OP_Integer, atoi(pExpr->token.z), 0);
drh51e9a442004-01-16 16:42:53 +00001050 }else{
1051 sqliteVdbeAddOp(v, OP_String, 0, 0);
drhd9e30932002-06-09 01:16:01 +00001052 }
drha76b5df2002-02-23 02:32:10 +00001053 assert( pExpr->token.z );
drh7a7c7392001-11-24 00:31:46 +00001054 sqliteVdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
drh51e9a442004-01-16 16:42:53 +00001055 sqliteVdbeDequoteP3(v, -1);
drhcce7d172000-05-31 15:34:51 +00001056 break;
1057 }
1058 case TK_NULL: {
drh99fcd712001-10-13 01:06:47 +00001059 sqliteVdbeAddOp(v, OP_String, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001060 break;
1061 }
drh50457892003-09-06 01:10:47 +00001062 case TK_VARIABLE: {
drh7c972de2003-09-06 22:18:07 +00001063 sqliteVdbeAddOp(v, OP_Variable, pExpr->iTable, 0);
drh50457892003-09-06 01:10:47 +00001064 break;
1065 }
drhc9b84a12002-06-20 11:36:48 +00001066 case TK_LT:
1067 case TK_LE:
1068 case TK_GT:
1069 case TK_GE:
1070 case TK_NE:
1071 case TK_EQ: {
drh491791a2002-07-18 00:34:09 +00001072 if( pParse->db->file_format>=4 && sqliteExprType(pExpr)==SQLITE_SO_TEXT ){
drhc9b84a12002-06-20 11:36:48 +00001073 op += 6; /* Convert numeric opcodes to text opcodes */
1074 }
1075 /* Fall through into the next case */
1076 }
drhcce7d172000-05-31 15:34:51 +00001077 case TK_AND:
1078 case TK_OR:
1079 case TK_PLUS:
1080 case TK_STAR:
1081 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00001082 case TK_REM:
1083 case TK_BITAND:
1084 case TK_BITOR:
drhc9b84a12002-06-20 11:36:48 +00001085 case TK_SLASH: {
drhcce7d172000-05-31 15:34:51 +00001086 sqliteExprCode(pParse, pExpr->pLeft);
1087 sqliteExprCode(pParse, pExpr->pRight);
drh99fcd712001-10-13 01:06:47 +00001088 sqliteVdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001089 break;
1090 }
drhbf4133c2001-10-13 02:59:08 +00001091 case TK_LSHIFT:
1092 case TK_RSHIFT: {
1093 sqliteExprCode(pParse, pExpr->pRight);
1094 sqliteExprCode(pParse, pExpr->pLeft);
1095 sqliteVdbeAddOp(v, op, 0, 0);
1096 break;
1097 }
drh00400772000-06-16 20:51:26 +00001098 case TK_CONCAT: {
1099 sqliteExprCode(pParse, pExpr->pLeft);
1100 sqliteExprCode(pParse, pExpr->pRight);
drh99fcd712001-10-13 01:06:47 +00001101 sqliteVdbeAddOp(v, OP_Concat, 2, 0);
drh00400772000-06-16 20:51:26 +00001102 break;
1103 }
drhcce7d172000-05-31 15:34:51 +00001104 case TK_UMINUS: {
drh6e142f52000-06-08 13:36:40 +00001105 assert( pExpr->pLeft );
drh7a7c7392001-11-24 00:31:46 +00001106 if( pExpr->pLeft->op==TK_FLOAT || pExpr->pLeft->op==TK_INTEGER ){
drh6e142f52000-06-08 13:36:40 +00001107 Token *p = &pExpr->pLeft->token;
1108 char *z = sqliteMalloc( p->n + 2 );
1109 sprintf(z, "-%.*s", p->n, p->z);
drh202b2df2004-01-06 01:13:46 +00001110 if( pExpr->pLeft->op==TK_INTEGER && sqliteFitsIn32Bits(z) ){
drhe6840902002-03-06 03:08:25 +00001111 sqliteVdbeAddOp(v, OP_Integer, atoi(z), 0);
1112 }else{
1113 sqliteVdbeAddOp(v, OP_String, 0, 0);
1114 }
drh99fcd712001-10-13 01:06:47 +00001115 sqliteVdbeChangeP3(v, -1, z, p->n+1);
drh6e142f52000-06-08 13:36:40 +00001116 sqliteFree(z);
1117 break;
1118 }
drh1ccde152000-06-17 13:12:39 +00001119 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +00001120 }
drhbf4133c2001-10-13 02:59:08 +00001121 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00001122 case TK_NOT: {
drhcce7d172000-05-31 15:34:51 +00001123 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +00001124 sqliteVdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001125 break;
1126 }
1127 case TK_ISNULL:
1128 case TK_NOTNULL: {
1129 int dest;
drh99fcd712001-10-13 01:06:47 +00001130 sqliteVdbeAddOp(v, OP_Integer, 1, 0);
drhcce7d172000-05-31 15:34:51 +00001131 sqliteExprCode(pParse, pExpr->pLeft);
1132 dest = sqliteVdbeCurrentAddr(v) + 2;
drhf5905aa2002-05-26 20:54:33 +00001133 sqliteVdbeAddOp(v, op, 1, dest);
drh99fcd712001-10-13 01:06:47 +00001134 sqliteVdbeAddOp(v, OP_AddImm, -1, 0);
drhcce7d172000-05-31 15:34:51 +00001135 break;
1136 }
drh22827922000-06-06 17:27:05 +00001137 case TK_AGG_FUNCTION: {
drh99fcd712001-10-13 01:06:47 +00001138 sqliteVdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drh22827922000-06-06 17:27:05 +00001139 break;
1140 }
drh4b59ab52002-08-24 18:24:51 +00001141 case TK_GLOB:
1142 case TK_LIKE:
drhcce7d172000-05-31 15:34:51 +00001143 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00001144 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00001145 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00001146 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00001147 int nId;
1148 const char *zId;
1149 getFunctionName(pExpr, &zId, &nId);
1150 pDef = sqliteFindFunction(pParse->db, zId, nId, nExpr, 0);
drh0bce8352002-02-28 00:41:10 +00001151 assert( pDef!=0 );
drh268380c2004-02-25 13:47:31 +00001152 nExpr = sqliteExprCodeExprList(pParse, pList, pDef->includeTypes);
drh701a0ae2004-02-22 20:05:00 +00001153 sqliteVdbeOp3(v, OP_Function, nExpr, 0, (char*)pDef, P3_POINTER);
drhcce7d172000-05-31 15:34:51 +00001154 break;
1155 }
drh19a775c2000-06-05 18:54:46 +00001156 case TK_SELECT: {
drh99fcd712001-10-13 01:06:47 +00001157 sqliteVdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drh19a775c2000-06-05 18:54:46 +00001158 break;
1159 }
drhfef52082000-06-06 01:50:43 +00001160 case TK_IN: {
1161 int addr;
drh99fcd712001-10-13 01:06:47 +00001162 sqliteVdbeAddOp(v, OP_Integer, 1, 0);
drhfef52082000-06-06 01:50:43 +00001163 sqliteExprCode(pParse, pExpr->pLeft);
1164 addr = sqliteVdbeCurrentAddr(v);
drhf5905aa2002-05-26 20:54:33 +00001165 sqliteVdbeAddOp(v, OP_NotNull, -1, addr+4);
drh8b8891b2004-03-17 23:32:08 +00001166 sqliteVdbeAddOp(v, OP_Pop, 2, 0);
drhf5905aa2002-05-26 20:54:33 +00001167 sqliteVdbeAddOp(v, OP_String, 0, 0);
1168 sqliteVdbeAddOp(v, OP_Goto, 0, addr+6);
drhfef52082000-06-06 01:50:43 +00001169 if( pExpr->pSelect ){
drhf5905aa2002-05-26 20:54:33 +00001170 sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, addr+6);
drhfef52082000-06-06 01:50:43 +00001171 }else{
drhf5905aa2002-05-26 20:54:33 +00001172 sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, addr+6);
drhfef52082000-06-06 01:50:43 +00001173 }
drh99fcd712001-10-13 01:06:47 +00001174 sqliteVdbeAddOp(v, OP_AddImm, -1, 0);
drhfef52082000-06-06 01:50:43 +00001175 break;
1176 }
1177 case TK_BETWEEN: {
drhf5905aa2002-05-26 20:54:33 +00001178 sqliteExprCode(pParse, pExpr->pLeft);
1179 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
1180 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
1181 sqliteVdbeAddOp(v, OP_Ge, 0, 0);
1182 sqliteVdbeAddOp(v, OP_Pull, 1, 0);
1183 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
1184 sqliteVdbeAddOp(v, OP_Le, 0, 0);
1185 sqliteVdbeAddOp(v, OP_And, 0, 0);
drhfef52082000-06-06 01:50:43 +00001186 break;
1187 }
drh51e9a442004-01-16 16:42:53 +00001188 case TK_UPLUS:
drha2e00042002-01-22 03:13:42 +00001189 case TK_AS: {
1190 sqliteExprCode(pParse, pExpr->pLeft);
1191 break;
1192 }
drh17a7f8d2002-03-24 13:13:27 +00001193 case TK_CASE: {
1194 int expr_end_label;
drhf5905aa2002-05-26 20:54:33 +00001195 int jumpInst;
1196 int addr;
1197 int nExpr;
drh17a7f8d2002-03-24 13:13:27 +00001198 int i;
1199
1200 assert(pExpr->pList);
1201 assert((pExpr->pList->nExpr % 2) == 0);
1202 assert(pExpr->pList->nExpr > 0);
drhf5905aa2002-05-26 20:54:33 +00001203 nExpr = pExpr->pList->nExpr;
1204 expr_end_label = sqliteVdbeMakeLabel(v);
drh17a7f8d2002-03-24 13:13:27 +00001205 if( pExpr->pLeft ){
1206 sqliteExprCode(pParse, pExpr->pLeft);
1207 }
drhf5905aa2002-05-26 20:54:33 +00001208 for(i=0; i<nExpr; i=i+2){
1209 sqliteExprCode(pParse, pExpr->pList->a[i].pExpr);
drh17a7f8d2002-03-24 13:13:27 +00001210 if( pExpr->pLeft ){
drhf5905aa2002-05-26 20:54:33 +00001211 sqliteVdbeAddOp(v, OP_Dup, 1, 1);
drhf570f012002-05-31 15:51:25 +00001212 jumpInst = sqliteVdbeAddOp(v, OP_Ne, 1, 0);
1213 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001214 }else{
drhf570f012002-05-31 15:51:25 +00001215 jumpInst = sqliteVdbeAddOp(v, OP_IfNot, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001216 }
1217 sqliteExprCode(pParse, pExpr->pList->a[i+1].pExpr);
drhf5905aa2002-05-26 20:54:33 +00001218 sqliteVdbeAddOp(v, OP_Goto, 0, expr_end_label);
drhf5905aa2002-05-26 20:54:33 +00001219 addr = sqliteVdbeCurrentAddr(v);
1220 sqliteVdbeChangeP2(v, jumpInst, addr);
drh17a7f8d2002-03-24 13:13:27 +00001221 }
drhf570f012002-05-31 15:51:25 +00001222 if( pExpr->pLeft ){
1223 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1224 }
drh17a7f8d2002-03-24 13:13:27 +00001225 if( pExpr->pRight ){
1226 sqliteExprCode(pParse, pExpr->pRight);
1227 }else{
drhf5905aa2002-05-26 20:54:33 +00001228 sqliteVdbeAddOp(v, OP_String, 0, 0);
drh17a7f8d2002-03-24 13:13:27 +00001229 }
drhf5905aa2002-05-26 20:54:33 +00001230 sqliteVdbeResolveLabel(v, expr_end_label);
danielk19776f349032002-06-11 02:25:40 +00001231 break;
1232 }
1233 case TK_RAISE: {
1234 if( !pParse->trigStack ){
drhda93d232003-03-31 02:12:46 +00001235 sqliteErrorMsg(pParse,
1236 "RAISE() may only be used within a trigger-program");
danielk19776f349032002-06-11 02:25:40 +00001237 pParse->nErr++;
1238 return;
1239 }
1240 if( pExpr->iColumn == OE_Rollback ||
1241 pExpr->iColumn == OE_Abort ||
1242 pExpr->iColumn == OE_Fail ){
drh701a0ae2004-02-22 20:05:00 +00001243 sqliteVdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn,
1244 pExpr->token.z, pExpr->token.n);
1245 sqliteVdbeDequoteP3(v, -1);
danielk19776f349032002-06-11 02:25:40 +00001246 } else {
1247 assert( pExpr->iColumn == OE_Ignore );
drh701a0ae2004-02-22 20:05:00 +00001248 sqliteVdbeOp3(v, OP_Goto, 0, pParse->trigStack->ignoreJump,
1249 "(IGNORE jump)", 0);
danielk19776f349032002-06-11 02:25:40 +00001250 }
drh17a7f8d2002-03-24 13:13:27 +00001251 }
1252 break;
drhcce7d172000-05-31 15:34:51 +00001253 }
drhcce7d172000-05-31 15:34:51 +00001254}
1255
1256/*
drh268380c2004-02-25 13:47:31 +00001257** Generate code that pushes the value of every element of the given
1258** expression list onto the stack. If the includeTypes flag is true,
1259** then also push a string that is the datatype of each element onto
1260** the stack after the value.
1261**
1262** Return the number of elements pushed onto the stack.
1263*/
1264int sqliteExprCodeExprList(
1265 Parse *pParse, /* Parsing context */
1266 ExprList *pList, /* The expression list to be coded */
1267 int includeTypes /* TRUE to put datatypes on the stack too */
1268){
1269 struct ExprList_item *pItem;
1270 int i, n;
1271 Vdbe *v;
1272 if( pList==0 ) return 0;
1273 v = sqliteGetVdbe(pParse);
1274 n = pList->nExpr;
1275 for(pItem=pList->a, i=0; i<n; i++, pItem++){
1276 sqliteExprCode(pParse, pItem->pExpr);
1277 if( includeTypes ){
1278 sqliteVdbeOp3(v, OP_String, 0, 0,
1279 sqliteExprType(pItem->pExpr)==SQLITE_SO_NUM ? "numeric" : "text",
1280 P3_STATIC);
1281 }
1282 }
1283 return includeTypes ? n*2 : n;
1284}
1285
1286/*
drhcce7d172000-05-31 15:34:51 +00001287** Generate code for a boolean expression such that a jump is made
1288** to the label "dest" if the expression is true but execution
1289** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00001290**
1291** If the expression evaluates to NULL (neither true nor false), then
1292** take the jump if the jumpIfNull flag is true.
drhcce7d172000-05-31 15:34:51 +00001293*/
drhf5905aa2002-05-26 20:54:33 +00001294void sqliteExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001295 Vdbe *v = pParse->pVdbe;
1296 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001297 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001298 switch( pExpr->op ){
1299 case TK_LT: op = OP_Lt; break;
1300 case TK_LE: op = OP_Le; break;
1301 case TK_GT: op = OP_Gt; break;
1302 case TK_GE: op = OP_Ge; break;
1303 case TK_NE: op = OP_Ne; break;
1304 case TK_EQ: op = OP_Eq; break;
drhcce7d172000-05-31 15:34:51 +00001305 case TK_ISNULL: op = OP_IsNull; break;
1306 case TK_NOTNULL: op = OP_NotNull; break;
1307 default: break;
1308 }
1309 switch( pExpr->op ){
1310 case TK_AND: {
1311 int d2 = sqliteVdbeMakeLabel(v);
drhf5905aa2002-05-26 20:54:33 +00001312 sqliteExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
1313 sqliteExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001314 sqliteVdbeResolveLabel(v, d2);
1315 break;
1316 }
1317 case TK_OR: {
drhf5905aa2002-05-26 20:54:33 +00001318 sqliteExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
1319 sqliteExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001320 break;
1321 }
1322 case TK_NOT: {
drhf5905aa2002-05-26 20:54:33 +00001323 sqliteExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001324 break;
1325 }
1326 case TK_LT:
1327 case TK_LE:
1328 case TK_GT:
1329 case TK_GE:
1330 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00001331 case TK_EQ: {
drhcce7d172000-05-31 15:34:51 +00001332 sqliteExprCode(pParse, pExpr->pLeft);
1333 sqliteExprCode(pParse, pExpr->pRight);
drh491791a2002-07-18 00:34:09 +00001334 if( pParse->db->file_format>=4 && sqliteExprType(pExpr)==SQLITE_SO_TEXT ){
drhc9b84a12002-06-20 11:36:48 +00001335 op += 6; /* Convert numeric opcodes to text opcodes */
1336 }
drhf5905aa2002-05-26 20:54:33 +00001337 sqliteVdbeAddOp(v, op, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001338 break;
1339 }
1340 case TK_ISNULL:
1341 case TK_NOTNULL: {
1342 sqliteExprCode(pParse, pExpr->pLeft);
drhf5905aa2002-05-26 20:54:33 +00001343 sqliteVdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001344 break;
1345 }
drhfef52082000-06-06 01:50:43 +00001346 case TK_IN: {
drhf5905aa2002-05-26 20:54:33 +00001347 int addr;
drhcfab11b2000-06-06 03:31:22 +00001348 sqliteExprCode(pParse, pExpr->pLeft);
drhf5905aa2002-05-26 20:54:33 +00001349 addr = sqliteVdbeCurrentAddr(v);
1350 sqliteVdbeAddOp(v, OP_NotNull, -1, addr+3);
1351 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1352 sqliteVdbeAddOp(v, OP_Goto, 0, jumpIfNull ? dest : addr+4);
drhfef52082000-06-06 01:50:43 +00001353 if( pExpr->pSelect ){
drh99fcd712001-10-13 01:06:47 +00001354 sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001355 }else{
drh99fcd712001-10-13 01:06:47 +00001356 sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001357 }
1358 break;
1359 }
1360 case TK_BETWEEN: {
drhf5905aa2002-05-26 20:54:33 +00001361 int addr;
drhfef52082000-06-06 01:50:43 +00001362 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +00001363 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
drhfef52082000-06-06 01:50:43 +00001364 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
drhf5905aa2002-05-26 20:54:33 +00001365 addr = sqliteVdbeAddOp(v, OP_Lt, !jumpIfNull, 0);
drhfef52082000-06-06 01:50:43 +00001366 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
drhf5905aa2002-05-26 20:54:33 +00001367 sqliteVdbeAddOp(v, OP_Le, jumpIfNull, dest);
drh99fcd712001-10-13 01:06:47 +00001368 sqliteVdbeAddOp(v, OP_Integer, 0, 0);
drhf5905aa2002-05-26 20:54:33 +00001369 sqliteVdbeChangeP2(v, addr, sqliteVdbeCurrentAddr(v));
drh99fcd712001-10-13 01:06:47 +00001370 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00001371 break;
1372 }
drhcce7d172000-05-31 15:34:51 +00001373 default: {
1374 sqliteExprCode(pParse, pExpr);
drhf5905aa2002-05-26 20:54:33 +00001375 sqliteVdbeAddOp(v, OP_If, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001376 break;
1377 }
1378 }
1379}
1380
1381/*
drh66b89c82000-11-28 20:47:17 +00001382** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00001383** to the label "dest" if the expression is false but execution
1384** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00001385**
1386** If the expression evaluates to NULL (neither true nor false) then
1387** jump if jumpIfNull is true or fall through if jumpIfNull is false.
drhcce7d172000-05-31 15:34:51 +00001388*/
drhf5905aa2002-05-26 20:54:33 +00001389void sqliteExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001390 Vdbe *v = pParse->pVdbe;
1391 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001392 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001393 switch( pExpr->op ){
1394 case TK_LT: op = OP_Ge; break;
1395 case TK_LE: op = OP_Gt; break;
1396 case TK_GT: op = OP_Le; break;
1397 case TK_GE: op = OP_Lt; break;
1398 case TK_NE: op = OP_Eq; break;
1399 case TK_EQ: op = OP_Ne; break;
drhcce7d172000-05-31 15:34:51 +00001400 case TK_ISNULL: op = OP_NotNull; break;
1401 case TK_NOTNULL: op = OP_IsNull; break;
1402 default: break;
1403 }
1404 switch( pExpr->op ){
1405 case TK_AND: {
drhf5905aa2002-05-26 20:54:33 +00001406 sqliteExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
1407 sqliteExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001408 break;
1409 }
1410 case TK_OR: {
1411 int d2 = sqliteVdbeMakeLabel(v);
drhf5905aa2002-05-26 20:54:33 +00001412 sqliteExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
1413 sqliteExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001414 sqliteVdbeResolveLabel(v, d2);
1415 break;
1416 }
1417 case TK_NOT: {
drhf5905aa2002-05-26 20:54:33 +00001418 sqliteExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001419 break;
1420 }
1421 case TK_LT:
1422 case TK_LE:
1423 case TK_GT:
1424 case TK_GE:
1425 case TK_NE:
1426 case TK_EQ: {
drh491791a2002-07-18 00:34:09 +00001427 if( pParse->db->file_format>=4 && sqliteExprType(pExpr)==SQLITE_SO_TEXT ){
drh8f619cc2002-09-08 00:04:50 +00001428 /* Convert numeric comparison opcodes into text comparison opcodes.
1429 ** This step depends on the fact that the text comparision opcodes are
1430 ** always 6 greater than their corresponding numeric comparison
1431 ** opcodes.
1432 */
1433 assert( OP_Eq+6 == OP_StrEq );
1434 op += 6;
drhc9b84a12002-06-20 11:36:48 +00001435 }
drhcce7d172000-05-31 15:34:51 +00001436 sqliteExprCode(pParse, pExpr->pLeft);
1437 sqliteExprCode(pParse, pExpr->pRight);
drhf5905aa2002-05-26 20:54:33 +00001438 sqliteVdbeAddOp(v, op, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001439 break;
1440 }
drhcce7d172000-05-31 15:34:51 +00001441 case TK_ISNULL:
1442 case TK_NOTNULL: {
1443 sqliteExprCode(pParse, pExpr->pLeft);
drhf5905aa2002-05-26 20:54:33 +00001444 sqliteVdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001445 break;
1446 }
drhfef52082000-06-06 01:50:43 +00001447 case TK_IN: {
drhf5905aa2002-05-26 20:54:33 +00001448 int addr;
drhcfab11b2000-06-06 03:31:22 +00001449 sqliteExprCode(pParse, pExpr->pLeft);
drhf5905aa2002-05-26 20:54:33 +00001450 addr = sqliteVdbeCurrentAddr(v);
1451 sqliteVdbeAddOp(v, OP_NotNull, -1, addr+3);
1452 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1453 sqliteVdbeAddOp(v, OP_Goto, 0, jumpIfNull ? dest : addr+4);
drhfef52082000-06-06 01:50:43 +00001454 if( pExpr->pSelect ){
drh99fcd712001-10-13 01:06:47 +00001455 sqliteVdbeAddOp(v, OP_NotFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001456 }else{
drh99fcd712001-10-13 01:06:47 +00001457 sqliteVdbeAddOp(v, OP_SetNotFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001458 }
1459 break;
1460 }
1461 case TK_BETWEEN: {
1462 int addr;
1463 sqliteExprCode(pParse, pExpr->pLeft);
drh99fcd712001-10-13 01:06:47 +00001464 sqliteVdbeAddOp(v, OP_Dup, 0, 0);
drhfef52082000-06-06 01:50:43 +00001465 sqliteExprCode(pParse, pExpr->pList->a[0].pExpr);
1466 addr = sqliteVdbeCurrentAddr(v);
drhf5905aa2002-05-26 20:54:33 +00001467 sqliteVdbeAddOp(v, OP_Ge, !jumpIfNull, addr+3);
drh99fcd712001-10-13 01:06:47 +00001468 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
1469 sqliteVdbeAddOp(v, OP_Goto, 0, dest);
drhfef52082000-06-06 01:50:43 +00001470 sqliteExprCode(pParse, pExpr->pList->a[1].pExpr);
drhf5905aa2002-05-26 20:54:33 +00001471 sqliteVdbeAddOp(v, OP_Gt, jumpIfNull, dest);
drhfef52082000-06-06 01:50:43 +00001472 break;
1473 }
drhcce7d172000-05-31 15:34:51 +00001474 default: {
1475 sqliteExprCode(pParse, pExpr);
drh461c2812002-05-30 02:35:11 +00001476 sqliteVdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001477 break;
1478 }
1479 }
1480}
drh22827922000-06-06 17:27:05 +00001481
1482/*
1483** Do a deep comparison of two expression trees. Return TRUE (non-zero)
1484** if they are identical and return FALSE if they differ in any way.
1485*/
drhd8bc7082000-06-07 23:51:50 +00001486int sqliteExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00001487 int i;
1488 if( pA==0 ){
1489 return pB==0;
1490 }else if( pB==0 ){
1491 return 0;
1492 }
1493 if( pA->op!=pB->op ) return 0;
drhd8bc7082000-06-07 23:51:50 +00001494 if( !sqliteExprCompare(pA->pLeft, pB->pLeft) ) return 0;
1495 if( !sqliteExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00001496 if( pA->pList ){
1497 if( pB->pList==0 ) return 0;
1498 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
1499 for(i=0; i<pA->pList->nExpr; i++){
drhd8bc7082000-06-07 23:51:50 +00001500 if( !sqliteExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00001501 return 0;
1502 }
1503 }
1504 }else if( pB->pList ){
1505 return 0;
1506 }
1507 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00001508 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drh22827922000-06-06 17:27:05 +00001509 if( pA->token.z ){
1510 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00001511 if( pB->token.n!=pA->token.n ) return 0;
1512 if( sqliteStrNICmp(pA->token.z, pB->token.z, pB->token.n)!=0 ) return 0;
drh22827922000-06-06 17:27:05 +00001513 }
1514 return 1;
1515}
1516
1517/*
1518** Add a new element to the pParse->aAgg[] array and return its index.
1519*/
1520static int appendAggInfo(Parse *pParse){
1521 if( (pParse->nAgg & 0x7)==0 ){
1522 int amt = pParse->nAgg + 8;
drh6d4abfb2001-10-22 02:58:08 +00001523 AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0]));
1524 if( aAgg==0 ){
drh22827922000-06-06 17:27:05 +00001525 return -1;
1526 }
drh6d4abfb2001-10-22 02:58:08 +00001527 pParse->aAgg = aAgg;
drh22827922000-06-06 17:27:05 +00001528 }
1529 memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0]));
1530 return pParse->nAgg++;
1531}
1532
1533/*
1534** Analyze the given expression looking for aggregate functions and
1535** for variables that need to be added to the pParse->aAgg[] array.
1536** Make additional entries to the pParse->aAgg[] array as necessary.
1537**
1538** This routine should only be called after the expression has been
1539** analyzed by sqliteExprResolveIds() and sqliteExprCheck().
1540**
1541** If errors are seen, leave an error message in zErrMsg and return
1542** the number of errors.
1543*/
1544int sqliteExprAnalyzeAggregates(Parse *pParse, Expr *pExpr){
1545 int i;
1546 AggExpr *aAgg;
1547 int nErr = 0;
1548
1549 if( pExpr==0 ) return 0;
1550 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00001551 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +00001552 aAgg = pParse->aAgg;
1553 for(i=0; i<pParse->nAgg; i++){
1554 if( aAgg[i].isAgg ) continue;
1555 if( aAgg[i].pExpr->iTable==pExpr->iTable
drh967e8b72000-06-21 13:59:10 +00001556 && aAgg[i].pExpr->iColumn==pExpr->iColumn ){
drh22827922000-06-06 17:27:05 +00001557 break;
1558 }
1559 }
1560 if( i>=pParse->nAgg ){
1561 i = appendAggInfo(pParse);
1562 if( i<0 ) return 1;
1563 pParse->aAgg[i].isAgg = 0;
1564 pParse->aAgg[i].pExpr = pExpr;
1565 }
drhaaf88722000-06-08 11:25:00 +00001566 pExpr->iAgg = i;
drh22827922000-06-06 17:27:05 +00001567 break;
1568 }
1569 case TK_AGG_FUNCTION: {
drh22827922000-06-06 17:27:05 +00001570 aAgg = pParse->aAgg;
1571 for(i=0; i<pParse->nAgg; i++){
1572 if( !aAgg[i].isAgg ) continue;
drhd8bc7082000-06-07 23:51:50 +00001573 if( sqliteExprCompare(aAgg[i].pExpr, pExpr) ){
drh22827922000-06-06 17:27:05 +00001574 break;
1575 }
1576 }
1577 if( i>=pParse->nAgg ){
1578 i = appendAggInfo(pParse);
1579 if( i<0 ) return 1;
1580 pParse->aAgg[i].isAgg = 1;
1581 pParse->aAgg[i].pExpr = pExpr;
drh0bce8352002-02-28 00:41:10 +00001582 pParse->aAgg[i].pFunc = sqliteFindFunction(pParse->db,
drh6977fea2002-10-22 23:38:04 +00001583 pExpr->token.z, pExpr->token.n,
drhf55f25f2002-02-28 01:46:11 +00001584 pExpr->pList ? pExpr->pList->nExpr : 0, 0);
drh22827922000-06-06 17:27:05 +00001585 }
1586 pExpr->iAgg = i;
1587 break;
1588 }
1589 default: {
1590 if( pExpr->pLeft ){
1591 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pLeft);
1592 }
1593 if( nErr==0 && pExpr->pRight ){
1594 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pRight);
1595 }
1596 if( nErr==0 && pExpr->pList ){
1597 int n = pExpr->pList->nExpr;
1598 int i;
1599 for(i=0; nErr==0 && i<n; i++){
1600 nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pList->a[i].pExpr);
1601 }
1602 }
1603 break;
1604 }
1605 }
1606 return nErr;
1607}
drh8e0a2f92002-02-23 23:45:45 +00001608
1609/*
1610** Locate a user function given a name and a number of arguments.
drh0bce8352002-02-28 00:41:10 +00001611** Return a pointer to the FuncDef structure that defines that
drh8e0a2f92002-02-23 23:45:45 +00001612** function, or return NULL if the function does not exist.
1613**
drh0bce8352002-02-28 00:41:10 +00001614** If the createFlag argument is true, then a new (blank) FuncDef
drh8e0a2f92002-02-23 23:45:45 +00001615** structure is created and liked into the "db" structure if a
1616** no matching function previously existed. When createFlag is true
1617** and the nArg parameter is -1, then only a function that accepts
1618** any number of arguments will be returned.
1619**
1620** If createFlag is false and nArg is -1, then the first valid
1621** function found is returned. A function is valid if either xFunc
1622** or xStep is non-zero.
1623*/
drh0bce8352002-02-28 00:41:10 +00001624FuncDef *sqliteFindFunction(
drh8e0a2f92002-02-23 23:45:45 +00001625 sqlite *db, /* An open database */
1626 const char *zName, /* Name of the function. Not null-terminated */
1627 int nName, /* Number of characters in the name */
1628 int nArg, /* Number of arguments. -1 means any number */
1629 int createFlag /* Create new entry if true and does not otherwise exist */
1630){
drh0bce8352002-02-28 00:41:10 +00001631 FuncDef *pFirst, *p, *pMaybe;
1632 pFirst = p = (FuncDef*)sqliteHashFind(&db->aFunc, zName, nName);
drh1350b032002-02-27 19:00:20 +00001633 if( p && !createFlag && nArg<0 ){
drh8e0a2f92002-02-23 23:45:45 +00001634 while( p && p->xFunc==0 && p->xStep==0 ){ p = p->pNext; }
1635 return p;
1636 }
1637 pMaybe = 0;
1638 while( p && p->nArg!=nArg ){
1639 if( p->nArg<0 && !createFlag && (p->xFunc || p->xStep) ) pMaybe = p;
1640 p = p->pNext;
1641 }
1642 if( p && !createFlag && p->xFunc==0 && p->xStep==0 ){
1643 return 0;
1644 }
1645 if( p==0 && pMaybe ){
1646 assert( createFlag==0 );
1647 return pMaybe;
1648 }
drh89425d52002-02-28 03:04:48 +00001649 if( p==0 && createFlag && (p = sqliteMalloc(sizeof(*p)))!=0 ){
drh8e0a2f92002-02-23 23:45:45 +00001650 p->nArg = nArg;
1651 p->pNext = pFirst;
drhc9b84a12002-06-20 11:36:48 +00001652 p->dataType = pFirst ? pFirst->dataType : SQLITE_NUMERIC;
drh0bce8352002-02-28 00:41:10 +00001653 sqliteHashInsert(&db->aFunc, zName, nName, (void*)p);
drh8e0a2f92002-02-23 23:45:45 +00001654 }
1655 return p;
1656}