blob: ed464ebf813d91b53ceda80ee625c667fc99ad61 [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**
danielk19774adee202004-05-08 08:23:19 +000015** $Id: expr.c,v 1.115 2004/05/08 08:23:24 danielk1977 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*/
danielk19774adee202004-05-08 08:23:19 +000025Expr *sqlite3Expr(int op, Expr *pLeft, Expr *pRight, Token *pToken){
drha76b5df2002-02-23 02:32:10 +000026 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 ){
danielk19774adee202004-05-08 08:23:19 +000044 sqlite3ExprSpan(pNew, &pLeft->span, &pRight->span);
drh6977fea2002-10-22 23:38:04 +000045 }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*/
danielk19774adee202004-05-08 08:23:19 +000056void sqlite3ExprSpan(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*/
danielk19774adee202004-05-08 08:23:19 +000074Expr *sqlite3ExprFunction(ExprList *pList, Token *pToken){
drha76b5df2002-02-23 02:32:10 +000075 Expr *pNew;
76 pNew = sqliteMalloc( sizeof(Expr) );
77 if( pNew==0 ){
danielk19774adee202004-05-08 08:23:19 +000078 /* sqlite3ExprListDelete(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*/
danielk19774adee202004-05-08 08:23:19 +000096void sqlite3ExprDelete(Expr *p){
drha2e00042002-01-22 03:13:42 +000097 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);
danielk19774adee202004-05-08 08:23:19 +0000100 sqlite3ExprDelete(p->pLeft);
101 sqlite3ExprDelete(p->pRight);
102 sqlite3ExprListDelete(p->pList);
103 sqlite3SelectDelete(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**
danielk19774adee202004-05-08 08:23:19 +0000114** The expression list, ID, and source lists return by sqlite3ExprListDup(),
115** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
drhad3cab52002-05-24 02:04:32 +0000116** 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*/
danielk19774adee202004-05-08 08:23:19 +0000120Expr *sqlite3ExprDup(Expr *p){
drhff78bd22002-02-27 01:47:11 +0000121 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;
danielk19774adee202004-05-08 08:23:19 +0000133 pNew->pLeft = sqlite3ExprDup(p->pLeft);
134 pNew->pRight = sqlite3ExprDup(p->pRight);
135 pNew->pList = sqlite3ExprListDup(p->pList);
136 pNew->pSelect = sqlite3SelectDup(p->pSelect);
drhff78bd22002-02-27 01:47:11 +0000137 return pNew;
138}
danielk19774adee202004-05-08 08:23:19 +0000139void sqlite3TokenCopy(Token *pTo, Token *pFrom){
drh4b59ab52002-08-24 18:24:51 +0000140 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}
danielk19774adee202004-05-08 08:23:19 +0000149ExprList *sqlite3ExprListDup(ExprList *p){
drhff78bd22002-02-27 01:47:11 +0000150 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]) );
drh1bdd9b52004-04-23 17:04:44 +0000158 if( pItem==0 ) return 0; /* Leaks memory after a malloc failure */
159 for(i=0; i<p->nExpr; i++, pItem++){
drh4b59ab52002-08-24 18:24:51 +0000160 Expr *pNewExpr, *pOldExpr;
danielk19774adee202004-05-08 08:23:19 +0000161 pItem->pExpr = pNewExpr = sqlite3ExprDup(pOldExpr = p->a[i].pExpr);
drh6977fea2002-10-22 23:38:04 +0000162 if( pOldExpr->span.z!=0 && pNewExpr ){
163 /* Always make a copy of the span for top-level expressions in the
drh4b59ab52002-08-24 18:24:51 +0000164 ** expression list. The logic in SELECT processing that determines
165 ** the names of columns in the result set needs this information */
danielk19774adee202004-05-08 08:23:19 +0000166 sqlite3TokenCopy(&pNewExpr->span, &pOldExpr->span);
drh4b59ab52002-08-24 18:24:51 +0000167 }
drh1f3e9052002-10-31 00:09:39 +0000168 assert( pNewExpr==0 || pNewExpr->span.z!=0
169 || pOldExpr->span.z==0 || sqlite_malloc_failed );
drh3e7bc9c2004-02-21 19:17:17 +0000170 pItem->zName = sqliteStrDup(p->a[i].zName);
171 pItem->sortOrder = p->a[i].sortOrder;
172 pItem->isAgg = p->a[i].isAgg;
173 pItem->done = 0;
drhff78bd22002-02-27 01:47:11 +0000174 }
175 return pNew;
176}
danielk19774adee202004-05-08 08:23:19 +0000177SrcList *sqlite3SrcListDup(SrcList *p){
drhad3cab52002-05-24 02:04:32 +0000178 SrcList *pNew;
179 int i;
drh113088e2003-03-20 01:16:58 +0000180 int nByte;
drhad3cab52002-05-24 02:04:32 +0000181 if( p==0 ) return 0;
drh113088e2003-03-20 01:16:58 +0000182 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
drh4efc4752004-01-16 15:55:37 +0000183 pNew = sqliteMallocRaw( nByte );
drhad3cab52002-05-24 02:04:32 +0000184 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000185 pNew->nSrc = pNew->nAlloc = p->nSrc;
drhad3cab52002-05-24 02:04:32 +0000186 for(i=0; i<p->nSrc; i++){
drh4efc4752004-01-16 15:55:37 +0000187 struct SrcList_item *pNewItem = &pNew->a[i];
188 struct SrcList_item *pOldItem = &p->a[i];
189 pNewItem->zDatabase = sqliteStrDup(pOldItem->zDatabase);
190 pNewItem->zName = sqliteStrDup(pOldItem->zName);
191 pNewItem->zAlias = sqliteStrDup(pOldItem->zAlias);
192 pNewItem->jointype = pOldItem->jointype;
193 pNewItem->iCursor = pOldItem->iCursor;
194 pNewItem->pTab = 0;
danielk19774adee202004-05-08 08:23:19 +0000195 pNewItem->pSelect = sqlite3SelectDup(pOldItem->pSelect);
196 pNewItem->pOn = sqlite3ExprDup(pOldItem->pOn);
197 pNewItem->pUsing = sqlite3IdListDup(pOldItem->pUsing);
drhad3cab52002-05-24 02:04:32 +0000198 }
199 return pNew;
200}
danielk19774adee202004-05-08 08:23:19 +0000201IdList *sqlite3IdListDup(IdList *p){
drhff78bd22002-02-27 01:47:11 +0000202 IdList *pNew;
203 int i;
204 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000205 pNew = sqliteMallocRaw( sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +0000206 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000207 pNew->nId = pNew->nAlloc = p->nId;
drh4efc4752004-01-16 15:55:37 +0000208 pNew->a = sqliteMallocRaw( p->nId*sizeof(p->a[0]) );
drhe4697f52002-05-23 02:09:03 +0000209 if( pNew->a==0 ) return 0;
drhff78bd22002-02-27 01:47:11 +0000210 for(i=0; i<p->nId; i++){
drh4efc4752004-01-16 15:55:37 +0000211 struct IdList_item *pNewItem = &pNew->a[i];
212 struct IdList_item *pOldItem = &p->a[i];
213 pNewItem->zName = sqliteStrDup(pOldItem->zName);
214 pNewItem->idx = pOldItem->idx;
drhff78bd22002-02-27 01:47:11 +0000215 }
216 return pNew;
217}
danielk19774adee202004-05-08 08:23:19 +0000218Select *sqlite3SelectDup(Select *p){
drhff78bd22002-02-27 01:47:11 +0000219 Select *pNew;
220 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000221 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000222 if( pNew==0 ) return 0;
223 pNew->isDistinct = p->isDistinct;
danielk19774adee202004-05-08 08:23:19 +0000224 pNew->pEList = sqlite3ExprListDup(p->pEList);
225 pNew->pSrc = sqlite3SrcListDup(p->pSrc);
226 pNew->pWhere = sqlite3ExprDup(p->pWhere);
227 pNew->pGroupBy = sqlite3ExprListDup(p->pGroupBy);
228 pNew->pHaving = sqlite3ExprDup(p->pHaving);
229 pNew->pOrderBy = sqlite3ExprListDup(p->pOrderBy);
drhff78bd22002-02-27 01:47:11 +0000230 pNew->op = p->op;
danielk19774adee202004-05-08 08:23:19 +0000231 pNew->pPrior = sqlite3SelectDup(p->pPrior);
drhff78bd22002-02-27 01:47:11 +0000232 pNew->nLimit = p->nLimit;
233 pNew->nOffset = p->nOffset;
234 pNew->zSelect = 0;
drh7b58dae2003-07-20 01:16:46 +0000235 pNew->iLimit = -1;
236 pNew->iOffset = -1;
drhff78bd22002-02-27 01:47:11 +0000237 return pNew;
238}
239
240
241/*
drha76b5df2002-02-23 02:32:10 +0000242** Add a new element to the end of an expression list. If pList is
243** initially NULL, then create a new expression list.
244*/
danielk19774adee202004-05-08 08:23:19 +0000245ExprList *sqlite3ExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){
drha76b5df2002-02-23 02:32:10 +0000246 if( pList==0 ){
247 pList = sqliteMalloc( sizeof(ExprList) );
248 if( pList==0 ){
danielk19774adee202004-05-08 08:23:19 +0000249 /* sqlite3ExprDelete(pExpr); // Leak memory if malloc fails */
drha76b5df2002-02-23 02:32:10 +0000250 return 0;
251 }
drh4efc4752004-01-16 15:55:37 +0000252 assert( pList->nAlloc==0 );
drha76b5df2002-02-23 02:32:10 +0000253 }
drh4305d102003-07-30 12:34:12 +0000254 if( pList->nAlloc<=pList->nExpr ){
drh4305d102003-07-30 12:34:12 +0000255 pList->nAlloc = pList->nAlloc*2 + 4;
drh4efc4752004-01-16 15:55:37 +0000256 pList->a = sqliteRealloc(pList->a, pList->nAlloc*sizeof(pList->a[0]));
257 if( pList->a==0 ){
danielk19774adee202004-05-08 08:23:19 +0000258 /* sqlite3ExprDelete(pExpr); // Leak memory if malloc fails */
drh4efc4752004-01-16 15:55:37 +0000259 pList->nExpr = pList->nAlloc = 0;
drha76b5df2002-02-23 02:32:10 +0000260 return pList;
261 }
drha76b5df2002-02-23 02:32:10 +0000262 }
drh4efc4752004-01-16 15:55:37 +0000263 assert( pList->a!=0 );
264 if( pExpr || pName ){
265 struct ExprList_item *pItem = &pList->a[pList->nExpr++];
266 memset(pItem, 0, sizeof(*pItem));
267 pItem->pExpr = pExpr;
drha76b5df2002-02-23 02:32:10 +0000268 if( pName ){
danielk19774adee202004-05-08 08:23:19 +0000269 sqlite3SetNString(&pItem->zName, pName->z, pName->n, 0);
270 sqlite3Dequote(pItem->zName);
drha76b5df2002-02-23 02:32:10 +0000271 }
272 }
273 return pList;
274}
275
276/*
277** Delete an entire expression list.
278*/
danielk19774adee202004-05-08 08:23:19 +0000279void sqlite3ExprListDelete(ExprList *pList){
drha76b5df2002-02-23 02:32:10 +0000280 int i;
281 if( pList==0 ) return;
drh1bdd9b52004-04-23 17:04:44 +0000282 assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
283 assert( pList->nExpr<=pList->nAlloc );
drha76b5df2002-02-23 02:32:10 +0000284 for(i=0; i<pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +0000285 sqlite3ExprDelete(pList->a[i].pExpr);
drha76b5df2002-02-23 02:32:10 +0000286 sqliteFree(pList->a[i].zName);
287 }
288 sqliteFree(pList->a);
289 sqliteFree(pList);
290}
291
292/*
drhfef52082000-06-06 01:50:43 +0000293** Walk an expression tree. Return 1 if the expression is constant
294** and 0 if it involves variables.
drh23989372002-05-21 13:43:04 +0000295**
296** For the purposes of this function, a double-quoted string (ex: "abc")
297** is considered a variable but a single-quoted string (ex: 'abc') is
298** a constant.
drhfef52082000-06-06 01:50:43 +0000299*/
danielk19774adee202004-05-08 08:23:19 +0000300int sqlite3ExprIsConstant(Expr *p){
drhfef52082000-06-06 01:50:43 +0000301 switch( p->op ){
302 case TK_ID:
drh967e8b72000-06-21 13:59:10 +0000303 case TK_COLUMN:
drhfef52082000-06-06 01:50:43 +0000304 case TK_DOT:
drh7bdc0c12003-04-19 17:27:24 +0000305 case TK_FUNCTION:
drhfef52082000-06-06 01:50:43 +0000306 return 0;
drh7bdc0c12003-04-19 17:27:24 +0000307 case TK_NULL:
drh23989372002-05-21 13:43:04 +0000308 case TK_STRING:
drh92086432002-01-22 14:11:29 +0000309 case TK_INTEGER:
310 case TK_FLOAT:
drh50457892003-09-06 01:10:47 +0000311 case TK_VARIABLE:
drh92086432002-01-22 14:11:29 +0000312 return 1;
drhfef52082000-06-06 01:50:43 +0000313 default: {
danielk19774adee202004-05-08 08:23:19 +0000314 if( p->pLeft && !sqlite3ExprIsConstant(p->pLeft) ) return 0;
315 if( p->pRight && !sqlite3ExprIsConstant(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++){
danielk19774adee202004-05-08 08:23:19 +0000319 if( !sqlite3ExprIsConstant(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/*
drh202b2df2004-01-06 01:13:46 +0000329** If the given expression codes a constant integer that is small enough
330** to fit in a 32-bit integer, return 1 and put the value of the integer
331** in *pValue. If the expression is not an integer or if it is too big
332** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
drhe4de1fe2002-06-02 16:09:01 +0000333*/
danielk19774adee202004-05-08 08:23:19 +0000334int sqlite3ExprIsInteger(Expr *p, int *pValue){
drhe4de1fe2002-06-02 16:09:01 +0000335 switch( p->op ){
336 case TK_INTEGER: {
danielk19774adee202004-05-08 08:23:19 +0000337 if( sqlite3FitsIn32Bits(p->token.z) ){
drh202b2df2004-01-06 01:13:46 +0000338 *pValue = atoi(p->token.z);
339 return 1;
340 }
341 break;
drhe4de1fe2002-06-02 16:09:01 +0000342 }
343 case TK_STRING: {
drhbd790ee2002-06-02 18:22:06 +0000344 const char *z = p->token.z;
drhe4de1fe2002-06-02 16:09:01 +0000345 int n = p->token.n;
drhbd790ee2002-06-02 18:22:06 +0000346 if( n>0 && z[0]=='-' ){ z++; n--; }
drhe4de1fe2002-06-02 16:09:01 +0000347 while( n>0 && *z && isdigit(*z) ){ z++; n--; }
danielk19774adee202004-05-08 08:23:19 +0000348 if( n==0 && sqlite3FitsIn32Bits(p->token.z) ){
drhe4de1fe2002-06-02 16:09:01 +0000349 *pValue = atoi(p->token.z);
350 return 1;
351 }
352 break;
353 }
drh4b59ab52002-08-24 18:24:51 +0000354 case TK_UPLUS: {
danielk19774adee202004-05-08 08:23:19 +0000355 return sqlite3ExprIsInteger(p->pLeft, pValue);
drh4b59ab52002-08-24 18:24:51 +0000356 }
drhe4de1fe2002-06-02 16:09:01 +0000357 case TK_UMINUS: {
358 int v;
danielk19774adee202004-05-08 08:23:19 +0000359 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
drhe4de1fe2002-06-02 16:09:01 +0000360 *pValue = -v;
361 return 1;
362 }
363 break;
364 }
365 default: break;
366 }
367 return 0;
368}
369
370/*
drhc4a3c772001-04-04 11:48:57 +0000371** Return TRUE if the given string is a row-id column name.
372*/
danielk19774adee202004-05-08 08:23:19 +0000373int sqlite3IsRowid(const char *z){
374 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
375 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
376 if( sqlite3StrICmp(z, "OID")==0 ) return 1;
drhc4a3c772001-04-04 11:48:57 +0000377 return 0;
378}
379
380/*
drh8141f612004-01-25 22:44:58 +0000381** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
382** that name in the set of source tables in pSrcList and make the pExpr
383** expression node refer back to that source column. The following changes
384** are made to pExpr:
385**
386** pExpr->iDb Set the index in db->aDb[] of the database holding
387** the table.
388** pExpr->iTable Set to the cursor number for the table obtained
389** from pSrcList.
390** pExpr->iColumn Set to the column number within the table.
391** pExpr->dataType Set to the appropriate data type for the column.
392** pExpr->op Set to TK_COLUMN.
393** pExpr->pLeft Any expression this points to is deleted
394** pExpr->pRight Any expression this points to is deleted.
395**
396** The pDbToken is the name of the database (the "X"). This value may be
397** NULL meaning that name is of the form Y.Z or Z. Any available database
398** can be used. The pTableToken is the name of the table (the "Y"). This
399** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it
400** means that the form of the name is Z and that columns from any table
401** can be used.
402**
403** If the name cannot be resolved unambiguously, leave an error message
404** in pParse and return non-zero. Return zero on success.
405*/
406static int lookupName(
407 Parse *pParse, /* The parsing context */
408 Token *pDbToken, /* Name of the database containing table, or NULL */
409 Token *pTableToken, /* Name of table containing column, or NULL */
410 Token *pColumnToken, /* Name of the column. */
411 SrcList *pSrcList, /* List of tables used to resolve column names */
412 ExprList *pEList, /* List of expressions used to resolve "AS" */
413 Expr *pExpr /* Make this EXPR node point to the selected column */
414){
415 char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */
416 char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */
417 char *zCol = 0; /* Name of the column. The "Z" */
418 int i, j; /* Loop counters */
419 int cnt = 0; /* Number of matching column names */
420 int cntTab = 0; /* Number of matching table names */
drh7e26d752004-02-11 10:35:29 +0000421 sqlite *db = pParse->db; /* The database */
drh8141f612004-01-25 22:44:58 +0000422
423 assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
424 if( pDbToken && pDbToken->z ){
425 zDb = sqliteStrNDup(pDbToken->z, pDbToken->n);
danielk19774adee202004-05-08 08:23:19 +0000426 sqlite3Dequote(zDb);
drh8141f612004-01-25 22:44:58 +0000427 }else{
428 zDb = 0;
429 }
430 if( pTableToken && pTableToken->z ){
431 zTab = sqliteStrNDup(pTableToken->z, pTableToken->n);
danielk19774adee202004-05-08 08:23:19 +0000432 sqlite3Dequote(zTab);
drh8141f612004-01-25 22:44:58 +0000433 }else{
434 assert( zDb==0 );
435 zTab = 0;
436 }
437 zCol = sqliteStrNDup(pColumnToken->z, pColumnToken->n);
danielk19774adee202004-05-08 08:23:19 +0000438 sqlite3Dequote(zCol);
drh8141f612004-01-25 22:44:58 +0000439 if( sqlite_malloc_failed ){
440 return 1; /* Leak memory (zDb and zTab) if malloc fails */
441 }
442 assert( zTab==0 || pEList==0 );
443
444 pExpr->iTable = -1;
445 for(i=0; i<pSrcList->nSrc; i++){
446 struct SrcList_item *pItem = &pSrcList->a[i];
447 Table *pTab = pItem->pTab;
448 Column *pCol;
449
450 if( pTab==0 ) continue;
451 assert( pTab->nCol>0 );
452 if( zTab ){
453 if( pItem->zAlias ){
454 char *zTabName = pItem->zAlias;
danielk19774adee202004-05-08 08:23:19 +0000455 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
drh8141f612004-01-25 22:44:58 +0000456 }else{
457 char *zTabName = pTab->zName;
danielk19774adee202004-05-08 08:23:19 +0000458 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
459 if( zDb!=0 && sqlite3StrICmp(db->aDb[pTab->iDb].zName, zDb)!=0 ){
drh8141f612004-01-25 22:44:58 +0000460 continue;
461 }
462 }
463 }
464 if( 0==(cntTab++) ){
465 pExpr->iTable = pItem->iCursor;
466 pExpr->iDb = pTab->iDb;
467 }
468 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
danielk19774adee202004-05-08 08:23:19 +0000469 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
drh8141f612004-01-25 22:44:58 +0000470 cnt++;
471 pExpr->iTable = pItem->iCursor;
472 pExpr->iDb = pTab->iDb;
473 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
474 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
475 pExpr->dataType = pCol->sortOrder & SQLITE_SO_TYPEMASK;
476 break;
477 }
478 }
479 }
480
481 /* If we have not already resolved the name, then maybe
482 ** it is a new.* or old.* trigger argument reference
483 */
484 if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
485 TriggerStack *pTriggerStack = pParse->trigStack;
486 Table *pTab = 0;
danielk19774adee202004-05-08 08:23:19 +0000487 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){
drh8141f612004-01-25 22:44:58 +0000488 pExpr->iTable = pTriggerStack->newIdx;
489 assert( pTriggerStack->pTab );
490 pTab = pTriggerStack->pTab;
danielk19774adee202004-05-08 08:23:19 +0000491 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab) == 0 ){
drh8141f612004-01-25 22:44:58 +0000492 pExpr->iTable = pTriggerStack->oldIdx;
493 assert( pTriggerStack->pTab );
494 pTab = pTriggerStack->pTab;
495 }
496
497 if( pTab ){
498 int j;
499 Column *pCol = pTab->aCol;
500
501 pExpr->iDb = pTab->iDb;
502 cntTab++;
503 for(j=0; j < pTab->nCol; j++, pCol++) {
danielk19774adee202004-05-08 08:23:19 +0000504 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
drh8141f612004-01-25 22:44:58 +0000505 cnt++;
506 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
507 pExpr->dataType = pCol->sortOrder & SQLITE_SO_TYPEMASK;
508 break;
509 }
510 }
511 }
512 }
513
514 /*
515 ** Perhaps the name is a reference to the ROWID
516 */
danielk19774adee202004-05-08 08:23:19 +0000517 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
drh8141f612004-01-25 22:44:58 +0000518 cnt = 1;
519 pExpr->iColumn = -1;
520 pExpr->dataType = SQLITE_SO_NUM;
521 }
522
523 /*
524 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
525 ** might refer to an result-set alias. This happens, for example, when
526 ** we are resolving names in the WHERE clause of the following command:
527 **
528 ** SELECT a+b AS x FROM table WHERE x<10;
529 **
530 ** In cases like this, replace pExpr with a copy of the expression that
531 ** forms the result set entry ("a+b" in the example) and return immediately.
532 ** Note that the expression in the result set should have already been
533 ** resolved by the time the WHERE clause is resolved.
534 */
535 if( cnt==0 && pEList!=0 ){
536 for(j=0; j<pEList->nExpr; j++){
537 char *zAs = pEList->a[j].zName;
danielk19774adee202004-05-08 08:23:19 +0000538 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
drh8141f612004-01-25 22:44:58 +0000539 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
540 pExpr->op = TK_AS;
541 pExpr->iColumn = j;
danielk19774adee202004-05-08 08:23:19 +0000542 pExpr->pLeft = sqlite3ExprDup(pEList->a[j].pExpr);
drh8141f612004-01-25 22:44:58 +0000543 sqliteFree(zCol);
544 assert( zTab==0 && zDb==0 );
545 return 0;
546 }
547 }
548 }
549
550 /*
551 ** If X and Y are NULL (in other words if only the column name Z is
552 ** supplied) and the value of Z is enclosed in double-quotes, then
553 ** Z is a string literal if it doesn't match any column names. In that
554 ** case, we need to return right away and not make any changes to
555 ** pExpr.
556 */
557 if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
558 sqliteFree(zCol);
559 return 0;
560 }
561
562 /*
563 ** cnt==0 means there was not match. cnt>1 means there were two or
564 ** more matches. Either way, we have an error.
565 */
566 if( cnt!=1 ){
567 char *z = 0;
568 char *zErr;
569 zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s";
570 if( zDb ){
danielk19774adee202004-05-08 08:23:19 +0000571 sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, 0);
drh8141f612004-01-25 22:44:58 +0000572 }else if( zTab ){
danielk19774adee202004-05-08 08:23:19 +0000573 sqlite3SetString(&z, zTab, ".", zCol, 0);
drh8141f612004-01-25 22:44:58 +0000574 }else{
575 z = sqliteStrDup(zCol);
576 }
danielk19774adee202004-05-08 08:23:19 +0000577 sqlite3ErrorMsg(pParse, zErr, z);
drh8141f612004-01-25 22:44:58 +0000578 sqliteFree(z);
579 }
580
581 /* Clean up and return
582 */
583 sqliteFree(zDb);
584 sqliteFree(zTab);
585 sqliteFree(zCol);
danielk19774adee202004-05-08 08:23:19 +0000586 sqlite3ExprDelete(pExpr->pLeft);
drh8141f612004-01-25 22:44:58 +0000587 pExpr->pLeft = 0;
danielk19774adee202004-05-08 08:23:19 +0000588 sqlite3ExprDelete(pExpr->pRight);
drh8141f612004-01-25 22:44:58 +0000589 pExpr->pRight = 0;
590 pExpr->op = TK_COLUMN;
danielk19774adee202004-05-08 08:23:19 +0000591 sqlite3AuthRead(pParse, pExpr, pSrcList);
drh8141f612004-01-25 22:44:58 +0000592 return cnt!=1;
593}
594
595/*
drhcce7d172000-05-31 15:34:51 +0000596** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +0000597** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +0000598** index to the table in the table list and a column offset. The
599** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
600** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +0000601** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +0000602** VDBE cursor number for a cursor that is pointing into the referenced
603** table. The Expr.iColumn value is changed to the index of the column
604** of the referenced table. The Expr.iColumn value for the special
605** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
606** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +0000607**
drhfef52082000-06-06 01:50:43 +0000608** We also check for instances of the IN operator. IN comes in two
609** forms:
610**
611** expr IN (exprlist)
612** and
613** expr IN (SELECT ...)
614**
615** The first form is handled by creating a set holding the list
616** of allowed values. The second form causes the SELECT to generate
617** a temporary table.
618**
619** This routine also looks for scalar SELECTs that are part of an expression.
drh19a775c2000-06-05 18:54:46 +0000620** If it finds any, it generates code to write the value of that select
621** into a memory cell.
drhcce7d172000-05-31 15:34:51 +0000622**
drh967e8b72000-06-21 13:59:10 +0000623** Unknown columns or tables provoke an error. The function returns
drhcce7d172000-05-31 15:34:51 +0000624** the number of errors seen and leaves an error message on pParse->zErrMsg.
625*/
danielk19774adee202004-05-08 08:23:19 +0000626int sqlite3ExprResolveIds(
drha2e00042002-01-22 03:13:42 +0000627 Parse *pParse, /* The parser context */
drh8141f612004-01-25 22:44:58 +0000628 SrcList *pSrcList, /* List of tables used to resolve column names */
drha2e00042002-01-22 03:13:42 +0000629 ExprList *pEList, /* List of expressions used to resolve "AS" */
630 Expr *pExpr /* The expression to be analyzed. */
631){
drh6a3ea0e2003-05-02 14:32:12 +0000632 int i;
633
drh8141f612004-01-25 22:44:58 +0000634 if( pExpr==0 || pSrcList==0 ) return 0;
635 for(i=0; i<pSrcList->nSrc; i++){
636 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab );
drh6a3ea0e2003-05-02 14:32:12 +0000637 }
drhcce7d172000-05-31 15:34:51 +0000638 switch( pExpr->op ){
drh23989372002-05-21 13:43:04 +0000639 /* Double-quoted strings (ex: "abc") are used as identifiers if
640 ** possible. Otherwise they remain as strings. Single-quoted
641 ** strings (ex: 'abc') are always string literals.
642 */
643 case TK_STRING: {
644 if( pExpr->token.z[0]=='\'' ) break;
645 /* Fall thru into the TK_ID case if this is a double-quoted string */
646 }
drh8141f612004-01-25 22:44:58 +0000647 /* A lone identifier is the name of a columnd.
drha2e00042002-01-22 03:13:42 +0000648 */
drhcce7d172000-05-31 15:34:51 +0000649 case TK_ID: {
drh8141f612004-01-25 22:44:58 +0000650 if( lookupName(pParse, 0, 0, &pExpr->token, pSrcList, pEList, pExpr) ){
drhcce7d172000-05-31 15:34:51 +0000651 return 1;
drhed6c8672003-01-12 18:02:16 +0000652 }
drhcce7d172000-05-31 15:34:51 +0000653 break;
654 }
655
drhd24cc422003-03-27 12:51:24 +0000656 /* A table name and column name: ID.ID
657 ** Or a database, table and column: ID.ID.ID
658 */
drhcce7d172000-05-31 15:34:51 +0000659 case TK_DOT: {
drh8141f612004-01-25 22:44:58 +0000660 Token *pColumn;
661 Token *pTable;
662 Token *pDb;
663 Expr *pRight;
drhcce7d172000-05-31 15:34:51 +0000664
drhcce7d172000-05-31 15:34:51 +0000665 pRight = pExpr->pRight;
drhd24cc422003-03-27 12:51:24 +0000666 if( pRight->op==TK_ID ){
drh8141f612004-01-25 22:44:58 +0000667 pDb = 0;
668 pTable = &pExpr->pLeft->token;
669 pColumn = &pRight->token;
drhd24cc422003-03-27 12:51:24 +0000670 }else{
drh8141f612004-01-25 22:44:58 +0000671 assert( pRight->op==TK_DOT );
672 pDb = &pExpr->pLeft->token;
673 pTable = &pRight->pLeft->token;
674 pColumn = &pRight->pRight->token;
drhd24cc422003-03-27 12:51:24 +0000675 }
drh8141f612004-01-25 22:44:58 +0000676 if( lookupName(pParse, pDb, pTable, pColumn, pSrcList, 0, pExpr) ){
drhdaffd0e2001-04-11 14:28:42 +0000677 return 1;
678 }
drhcce7d172000-05-31 15:34:51 +0000679 break;
680 }
681
drhfef52082000-06-06 01:50:43 +0000682 case TK_IN: {
danielk19774adee202004-05-08 08:23:19 +0000683 Vdbe *v = sqlite3GetVdbe(pParse);
drhfef52082000-06-06 01:50:43 +0000684 if( v==0 ) return 1;
danielk19774adee202004-05-08 08:23:19 +0000685 if( sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pLeft) ){
drhcfab11b2000-06-06 03:31:22 +0000686 return 1;
687 }
drhfef52082000-06-06 01:50:43 +0000688 if( pExpr->pSelect ){
689 /* Case 1: expr IN (SELECT ...)
690 **
691 ** Generate code to write the results of the select into a temporary
drh4794b982000-06-06 13:54:14 +0000692 ** table. The cursor number of the temporary table has already
danielk19774adee202004-05-08 08:23:19 +0000693 ** been put in iTable by sqlite3ExprResolveInSelect().
drhfef52082000-06-06 01:50:43 +0000694 */
drh832508b2002-03-02 17:04:07 +0000695 pExpr->iTable = pParse->nTab++;
danielk19774adee202004-05-08 08:23:19 +0000696 sqlite3VdbeAddOp(v, OP_OpenTemp, pExpr->iTable, 1);
697 sqlite3Select(pParse, pExpr->pSelect, SRT_Set, pExpr->iTable, 0,0,0);
drhfef52082000-06-06 01:50:43 +0000698 }else if( pExpr->pList ){
699 /* Case 2: expr IN (exprlist)
700 **
701 ** Create a set to put the exprlist values in. The Set id is stored
702 ** in iTable.
703 */
704 int i, iSet;
705 for(i=0; i<pExpr->pList->nExpr; i++){
706 Expr *pE2 = pExpr->pList->a[i].pExpr;
danielk19774adee202004-05-08 08:23:19 +0000707 if( !sqlite3ExprIsConstant(pE2) ){
708 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +0000709 "right-hand side of IN operator must be constant");
drhfef52082000-06-06 01:50:43 +0000710 return 1;
711 }
danielk19774adee202004-05-08 08:23:19 +0000712 if( sqlite3ExprCheck(pParse, pE2, 0, 0) ){
drh4794b982000-06-06 13:54:14 +0000713 return 1;
714 }
drhfef52082000-06-06 01:50:43 +0000715 }
716 iSet = pExpr->iTable = pParse->nSet++;
717 for(i=0; i<pExpr->pList->nExpr; i++){
718 Expr *pE2 = pExpr->pList->a[i].pExpr;
719 switch( pE2->op ){
720 case TK_FLOAT:
721 case TK_INTEGER:
722 case TK_STRING: {
drh701a0ae2004-02-22 20:05:00 +0000723 int addr;
drha76b5df2002-02-23 02:32:10 +0000724 assert( pE2->token.z );
danielk19774adee202004-05-08 08:23:19 +0000725 addr = sqlite3VdbeOp3(v, OP_SetInsert, iSet, 0,
drh701a0ae2004-02-22 20:05:00 +0000726 pE2->token.z, pE2->token.n);
danielk19774adee202004-05-08 08:23:19 +0000727 sqlite3VdbeDequoteP3(v, addr);
drhfef52082000-06-06 01:50:43 +0000728 break;
729 }
730 default: {
danielk19774adee202004-05-08 08:23:19 +0000731 sqlite3ExprCode(pParse, pE2);
732 sqlite3VdbeAddOp(v, OP_SetInsert, iSet, 0);
drhfef52082000-06-06 01:50:43 +0000733 break;
734 }
735 }
736 }
737 }
drhcfab11b2000-06-06 03:31:22 +0000738 break;
drhfef52082000-06-06 01:50:43 +0000739 }
740
drh19a775c2000-06-05 18:54:46 +0000741 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +0000742 /* This has to be a scalar SELECT. Generate code to put the
743 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +0000744 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +0000745 */
drh967e8b72000-06-21 13:59:10 +0000746 pExpr->iColumn = pParse->nMem++;
danielk19774adee202004-05-08 08:23:19 +0000747 if( sqlite3Select(pParse, pExpr->pSelect, SRT_Mem, pExpr->iColumn,0,0,0) ){
drh19a775c2000-06-05 18:54:46 +0000748 return 1;
749 }
750 break;
751 }
752
drhcce7d172000-05-31 15:34:51 +0000753 /* For all else, just recursively walk the tree */
754 default: {
drh4794b982000-06-06 13:54:14 +0000755 if( pExpr->pLeft
danielk19774adee202004-05-08 08:23:19 +0000756 && sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pLeft) ){
drhcce7d172000-05-31 15:34:51 +0000757 return 1;
758 }
759 if( pExpr->pRight
danielk19774adee202004-05-08 08:23:19 +0000760 && sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pRight) ){
drhcce7d172000-05-31 15:34:51 +0000761 return 1;
762 }
763 if( pExpr->pList ){
764 int i;
765 ExprList *pList = pExpr->pList;
766 for(i=0; i<pList->nExpr; i++){
drh832508b2002-03-02 17:04:07 +0000767 Expr *pArg = pList->a[i].pExpr;
danielk19774adee202004-05-08 08:23:19 +0000768 if( sqlite3ExprResolveIds(pParse, pSrcList, pEList, pArg) ){
drhcce7d172000-05-31 15:34:51 +0000769 return 1;
770 }
771 }
772 }
773 }
774 }
775 return 0;
776}
777
drhcce7d172000-05-31 15:34:51 +0000778/*
drh4b59ab52002-08-24 18:24:51 +0000779** pExpr is a node that defines a function of some kind. It might
780** be a syntactic function like "count(x)" or it might be a function
781** that implements an operator, like "a LIKE b".
782**
783** This routine makes *pzName point to the name of the function and
784** *pnName hold the number of characters in the function name.
785*/
786static void getFunctionName(Expr *pExpr, const char **pzName, int *pnName){
787 switch( pExpr->op ){
788 case TK_FUNCTION: {
789 *pzName = pExpr->token.z;
drh6977fea2002-10-22 23:38:04 +0000790 *pnName = pExpr->token.n;
drh4b59ab52002-08-24 18:24:51 +0000791 break;
792 }
793 case TK_LIKE: {
794 *pzName = "like";
795 *pnName = 4;
796 break;
797 }
798 case TK_GLOB: {
799 *pzName = "glob";
800 *pnName = 4;
801 break;
802 }
803 default: {
804 *pzName = "can't happen";
805 *pnName = 12;
806 break;
807 }
808 }
809}
810
811/*
drhcce7d172000-05-31 15:34:51 +0000812** Error check the functions in an expression. Make sure all
813** function names are recognized and all functions have the correct
814** number of arguments. Leave an error message in pParse->zErrMsg
815** if anything is amiss. Return the number of errors.
816**
817** if pIsAgg is not null and this expression is an aggregate function
818** (like count(*) or max(value)) then write a 1 into *pIsAgg.
819*/
danielk19774adee202004-05-08 08:23:19 +0000820int sqlite3ExprCheck(Parse *pParse, Expr *pExpr, int allowAgg, int *pIsAgg){
drhcce7d172000-05-31 15:34:51 +0000821 int nErr = 0;
822 if( pExpr==0 ) return 0;
drhcce7d172000-05-31 15:34:51 +0000823 switch( pExpr->op ){
drh4b59ab52002-08-24 18:24:51 +0000824 case TK_GLOB:
825 case TK_LIKE:
drhcce7d172000-05-31 15:34:51 +0000826 case TK_FUNCTION: {
drhc9b84a12002-06-20 11:36:48 +0000827 int n = pExpr->pList ? pExpr->pList->nExpr : 0; /* Number of arguments */
828 int no_such_func = 0; /* True if no such function exists */
drhc9b84a12002-06-20 11:36:48 +0000829 int wrong_num_args = 0; /* True if wrong number of arguments */
830 int is_agg = 0; /* True if is an aggregate function */
drhcce7d172000-05-31 15:34:51 +0000831 int i;
drh4b59ab52002-08-24 18:24:51 +0000832 int nId; /* Number of characters in function name */
833 const char *zId; /* The function name. */
drh0bce8352002-02-28 00:41:10 +0000834 FuncDef *pDef;
835
drh4b59ab52002-08-24 18:24:51 +0000836 getFunctionName(pExpr, &zId, &nId);
danielk19774adee202004-05-08 08:23:19 +0000837 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, 0);
drh0bce8352002-02-28 00:41:10 +0000838 if( pDef==0 ){
danielk19774adee202004-05-08 08:23:19 +0000839 pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, 0);
drh0bce8352002-02-28 00:41:10 +0000840 if( pDef==0 ){
drh268380c2004-02-25 13:47:31 +0000841 no_such_func = 1;
drh0bce8352002-02-28 00:41:10 +0000842 }else{
843 wrong_num_args = 1;
drhcce7d172000-05-31 15:34:51 +0000844 }
drh0bce8352002-02-28 00:41:10 +0000845 }else{
846 is_agg = pDef->xFunc==0;
drhcce7d172000-05-31 15:34:51 +0000847 }
drh8e0a2f92002-02-23 23:45:45 +0000848 if( is_agg && !allowAgg ){
danielk19774adee202004-05-08 08:23:19 +0000849 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId, zId);
drh8e0a2f92002-02-23 23:45:45 +0000850 nErr++;
851 is_agg = 0;
852 }else if( no_such_func ){
danielk19774adee202004-05-08 08:23:19 +0000853 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
drhcce7d172000-05-31 15:34:51 +0000854 nErr++;
drh8e0a2f92002-02-23 23:45:45 +0000855 }else if( wrong_num_args ){
danielk19774adee202004-05-08 08:23:19 +0000856 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
drhf7a9e1a2004-02-22 18:40:56 +0000857 nId, zId);
drh8e0a2f92002-02-23 23:45:45 +0000858 nErr++;
drhcce7d172000-05-31 15:34:51 +0000859 }
drhf7a9e1a2004-02-22 18:40:56 +0000860 if( is_agg ){
861 pExpr->op = TK_AGG_FUNCTION;
862 if( pIsAgg ) *pIsAgg = 1;
863 }
drhcce7d172000-05-31 15:34:51 +0000864 for(i=0; nErr==0 && i<n; i++){
danielk19774adee202004-05-08 08:23:19 +0000865 nErr = sqlite3ExprCheck(pParse, pExpr->pList->a[i].pExpr,
drh4cfa7932000-06-08 15:10:46 +0000866 allowAgg && !is_agg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000867 }
drhc9b84a12002-06-20 11:36:48 +0000868 if( pDef==0 ){
drh268380c2004-02-25 13:47:31 +0000869 /* Already reported an error */
drhc9b84a12002-06-20 11:36:48 +0000870 }else if( pDef->dataType>=0 ){
871 if( pDef->dataType<n ){
872 pExpr->dataType =
danielk19774adee202004-05-08 08:23:19 +0000873 sqlite3ExprType(pExpr->pList->a[pDef->dataType].pExpr);
drhc9b84a12002-06-20 11:36:48 +0000874 }else{
875 pExpr->dataType = SQLITE_SO_NUM;
876 }
877 }else if( pDef->dataType==SQLITE_ARGS ){
878 pDef->dataType = SQLITE_SO_TEXT;
879 for(i=0; i<n; i++){
danielk19774adee202004-05-08 08:23:19 +0000880 if( sqlite3ExprType(pExpr->pList->a[i].pExpr)==SQLITE_SO_NUM ){
drhc9b84a12002-06-20 11:36:48 +0000881 pExpr->dataType = SQLITE_SO_NUM;
882 break;
883 }
884 }
885 }else if( pDef->dataType==SQLITE_NUMERIC ){
886 pExpr->dataType = SQLITE_SO_NUM;
887 }else{
888 pExpr->dataType = SQLITE_SO_TEXT;
889 }
drhcce7d172000-05-31 15:34:51 +0000890 }
891 default: {
892 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +0000893 nErr = sqlite3ExprCheck(pParse, pExpr->pLeft, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000894 }
895 if( nErr==0 && pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +0000896 nErr = sqlite3ExprCheck(pParse, pExpr->pRight, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000897 }
drhfef52082000-06-06 01:50:43 +0000898 if( nErr==0 && pExpr->pList ){
899 int n = pExpr->pList->nExpr;
900 int i;
901 for(i=0; nErr==0 && i<n; i++){
drh22827922000-06-06 17:27:05 +0000902 Expr *pE2 = pExpr->pList->a[i].pExpr;
danielk19774adee202004-05-08 08:23:19 +0000903 nErr = sqlite3ExprCheck(pParse, pE2, allowAgg, pIsAgg);
drhfef52082000-06-06 01:50:43 +0000904 }
905 }
drhcce7d172000-05-31 15:34:51 +0000906 break;
907 }
908 }
909 return nErr;
910}
911
912/*
drhc9b84a12002-06-20 11:36:48 +0000913** Return either SQLITE_SO_NUM or SQLITE_SO_TEXT to indicate whether the
914** given expression should sort as numeric values or as text.
915**
danielk19774adee202004-05-08 08:23:19 +0000916** The sqlite3ExprResolveIds() and sqlite3ExprCheck() routines must have
drhc9b84a12002-06-20 11:36:48 +0000917** both been called on the expression before it is passed to this routine.
918*/
danielk19774adee202004-05-08 08:23:19 +0000919int sqlite3ExprType(Expr *p){
drhc9b84a12002-06-20 11:36:48 +0000920 if( p==0 ) return SQLITE_SO_NUM;
921 while( p ) switch( p->op ){
922 case TK_PLUS:
923 case TK_MINUS:
924 case TK_STAR:
925 case TK_SLASH:
926 case TK_AND:
927 case TK_OR:
928 case TK_ISNULL:
929 case TK_NOTNULL:
930 case TK_NOT:
931 case TK_UMINUS:
drh4b59ab52002-08-24 18:24:51 +0000932 case TK_UPLUS:
drhc9b84a12002-06-20 11:36:48 +0000933 case TK_BITAND:
934 case TK_BITOR:
935 case TK_BITNOT:
936 case TK_LSHIFT:
937 case TK_RSHIFT:
938 case TK_REM:
939 case TK_INTEGER:
940 case TK_FLOAT:
941 case TK_IN:
942 case TK_BETWEEN:
drh4b59ab52002-08-24 18:24:51 +0000943 case TK_GLOB:
944 case TK_LIKE:
drhc9b84a12002-06-20 11:36:48 +0000945 return SQLITE_SO_NUM;
946
947 case TK_STRING:
948 case TK_NULL:
949 case TK_CONCAT:
drh50457892003-09-06 01:10:47 +0000950 case TK_VARIABLE:
drhc9b84a12002-06-20 11:36:48 +0000951 return SQLITE_SO_TEXT;
952
953 case TK_LT:
954 case TK_LE:
955 case TK_GT:
956 case TK_GE:
957 case TK_NE:
958 case TK_EQ:
danielk19774adee202004-05-08 08:23:19 +0000959 if( sqlite3ExprType(p->pLeft)==SQLITE_SO_NUM ){
drhc9b84a12002-06-20 11:36:48 +0000960 return SQLITE_SO_NUM;
961 }
962 p = p->pRight;
963 break;
964
965 case TK_AS:
966 p = p->pLeft;
967 break;
968
969 case TK_COLUMN:
970 case TK_FUNCTION:
971 case TK_AGG_FUNCTION:
972 return p->dataType;
973
974 case TK_SELECT:
975 assert( p->pSelect );
976 assert( p->pSelect->pEList );
977 assert( p->pSelect->pEList->nExpr>0 );
978 p = p->pSelect->pEList->a[0].pExpr;
979 break;
980
drhb1363202002-06-26 02:45:03 +0000981 case TK_CASE: {
danielk19774adee202004-05-08 08:23:19 +0000982 if( p->pRight && sqlite3ExprType(p->pRight)==SQLITE_SO_NUM ){
drhb1363202002-06-26 02:45:03 +0000983 return SQLITE_SO_NUM;
984 }
985 if( p->pList ){
986 int i;
987 ExprList *pList = p->pList;
988 for(i=1; i<pList->nExpr; i+=2){
danielk19774adee202004-05-08 08:23:19 +0000989 if( sqlite3ExprType(pList->a[i].pExpr)==SQLITE_SO_NUM ){
drhb1363202002-06-26 02:45:03 +0000990 return SQLITE_SO_NUM;
991 }
992 }
993 }
994 return SQLITE_SO_TEXT;
995 }
996
drhc9b84a12002-06-20 11:36:48 +0000997 default:
998 assert( p->op==TK_ABORT ); /* Can't Happen */
999 break;
1000 }
1001 return SQLITE_SO_NUM;
1002}
1003
1004/*
drhcce7d172000-05-31 15:34:51 +00001005** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +00001006** expression and leave the result on the top of stack.
drhcce7d172000-05-31 15:34:51 +00001007*/
danielk19774adee202004-05-08 08:23:19 +00001008void sqlite3ExprCode(Parse *pParse, Expr *pExpr){
drhcce7d172000-05-31 15:34:51 +00001009 Vdbe *v = pParse->pVdbe;
1010 int op;
drhdaffd0e2001-04-11 14:28:42 +00001011 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001012 switch( pExpr->op ){
1013 case TK_PLUS: op = OP_Add; break;
1014 case TK_MINUS: op = OP_Subtract; break;
1015 case TK_STAR: op = OP_Multiply; break;
1016 case TK_SLASH: op = OP_Divide; break;
1017 case TK_AND: op = OP_And; break;
1018 case TK_OR: op = OP_Or; break;
1019 case TK_LT: op = OP_Lt; break;
1020 case TK_LE: op = OP_Le; break;
1021 case TK_GT: op = OP_Gt; break;
1022 case TK_GE: op = OP_Ge; break;
1023 case TK_NE: op = OP_Ne; break;
1024 case TK_EQ: op = OP_Eq; break;
drhcce7d172000-05-31 15:34:51 +00001025 case TK_ISNULL: op = OP_IsNull; break;
1026 case TK_NOTNULL: op = OP_NotNull; break;
1027 case TK_NOT: op = OP_Not; break;
1028 case TK_UMINUS: op = OP_Negative; break;
drhbf4133c2001-10-13 02:59:08 +00001029 case TK_BITAND: op = OP_BitAnd; break;
1030 case TK_BITOR: op = OP_BitOr; break;
1031 case TK_BITNOT: op = OP_BitNot; break;
1032 case TK_LSHIFT: op = OP_ShiftLeft; break;
1033 case TK_RSHIFT: op = OP_ShiftRight; break;
1034 case TK_REM: op = OP_Remainder; break;
drhcce7d172000-05-31 15:34:51 +00001035 default: break;
1036 }
1037 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00001038 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +00001039 if( pParse->useAgg ){
danielk19774adee202004-05-08 08:23:19 +00001040 sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drhc4a3c772001-04-04 11:48:57 +00001041 }else if( pExpr->iColumn>=0 ){
danielk19774adee202004-05-08 08:23:19 +00001042 sqlite3VdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn);
drhc4a3c772001-04-04 11:48:57 +00001043 }else{
danielk19774adee202004-05-08 08:23:19 +00001044 sqlite3VdbeAddOp(v, OP_Recno, pExpr->iTable, 0);
drh22827922000-06-06 17:27:05 +00001045 }
drhcce7d172000-05-31 15:34:51 +00001046 break;
1047 }
drh51e9a442004-01-16 16:42:53 +00001048 case TK_STRING:
1049 case TK_FLOAT:
drhcce7d172000-05-31 15:34:51 +00001050 case TK_INTEGER: {
danielk19774adee202004-05-08 08:23:19 +00001051 if( pExpr->op==TK_INTEGER && sqlite3FitsIn32Bits(pExpr->token.z) ){
1052 sqlite3VdbeAddOp(v, OP_Integer, atoi(pExpr->token.z), 0);
drh51e9a442004-01-16 16:42:53 +00001053 }else{
danielk19774adee202004-05-08 08:23:19 +00001054 sqlite3VdbeAddOp(v, OP_String, 0, 0);
drhd9e30932002-06-09 01:16:01 +00001055 }
drha76b5df2002-02-23 02:32:10 +00001056 assert( pExpr->token.z );
danielk19774adee202004-05-08 08:23:19 +00001057 sqlite3VdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
1058 sqlite3VdbeDequoteP3(v, -1);
drhcce7d172000-05-31 15:34:51 +00001059 break;
1060 }
1061 case TK_NULL: {
danielk19774adee202004-05-08 08:23:19 +00001062 sqlite3VdbeAddOp(v, OP_String, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001063 break;
1064 }
drh50457892003-09-06 01:10:47 +00001065 case TK_VARIABLE: {
danielk19774adee202004-05-08 08:23:19 +00001066 sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0);
drh50457892003-09-06 01:10:47 +00001067 break;
1068 }
drhc9b84a12002-06-20 11:36:48 +00001069 case TK_LT:
1070 case TK_LE:
1071 case TK_GT:
1072 case TK_GE:
1073 case TK_NE:
1074 case TK_EQ: {
danielk19774adee202004-05-08 08:23:19 +00001075 if( pParse->db->file_format>=4 && sqlite3ExprType(pExpr)==SQLITE_SO_TEXT ){
drhc9b84a12002-06-20 11:36:48 +00001076 op += 6; /* Convert numeric opcodes to text opcodes */
1077 }
1078 /* Fall through into the next case */
1079 }
drhcce7d172000-05-31 15:34:51 +00001080 case TK_AND:
1081 case TK_OR:
1082 case TK_PLUS:
1083 case TK_STAR:
1084 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00001085 case TK_REM:
1086 case TK_BITAND:
1087 case TK_BITOR:
drhc9b84a12002-06-20 11:36:48 +00001088 case TK_SLASH: {
danielk19774adee202004-05-08 08:23:19 +00001089 sqlite3ExprCode(pParse, pExpr->pLeft);
1090 sqlite3ExprCode(pParse, pExpr->pRight);
1091 sqlite3VdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001092 break;
1093 }
drhbf4133c2001-10-13 02:59:08 +00001094 case TK_LSHIFT:
1095 case TK_RSHIFT: {
danielk19774adee202004-05-08 08:23:19 +00001096 sqlite3ExprCode(pParse, pExpr->pRight);
1097 sqlite3ExprCode(pParse, pExpr->pLeft);
1098 sqlite3VdbeAddOp(v, op, 0, 0);
drhbf4133c2001-10-13 02:59:08 +00001099 break;
1100 }
drh00400772000-06-16 20:51:26 +00001101 case TK_CONCAT: {
danielk19774adee202004-05-08 08:23:19 +00001102 sqlite3ExprCode(pParse, pExpr->pLeft);
1103 sqlite3ExprCode(pParse, pExpr->pRight);
1104 sqlite3VdbeAddOp(v, OP_Concat, 2, 0);
drh00400772000-06-16 20:51:26 +00001105 break;
1106 }
drhcce7d172000-05-31 15:34:51 +00001107 case TK_UMINUS: {
drh6e142f52000-06-08 13:36:40 +00001108 assert( pExpr->pLeft );
drh7a7c7392001-11-24 00:31:46 +00001109 if( pExpr->pLeft->op==TK_FLOAT || pExpr->pLeft->op==TK_INTEGER ){
drh6e142f52000-06-08 13:36:40 +00001110 Token *p = &pExpr->pLeft->token;
1111 char *z = sqliteMalloc( p->n + 2 );
1112 sprintf(z, "-%.*s", p->n, p->z);
danielk19774adee202004-05-08 08:23:19 +00001113 if( pExpr->pLeft->op==TK_INTEGER && sqlite3FitsIn32Bits(z) ){
1114 sqlite3VdbeAddOp(v, OP_Integer, atoi(z), 0);
drhe6840902002-03-06 03:08:25 +00001115 }else{
danielk19774adee202004-05-08 08:23:19 +00001116 sqlite3VdbeAddOp(v, OP_String, 0, 0);
drhe6840902002-03-06 03:08:25 +00001117 }
danielk19774adee202004-05-08 08:23:19 +00001118 sqlite3VdbeChangeP3(v, -1, z, p->n+1);
drh6e142f52000-06-08 13:36:40 +00001119 sqliteFree(z);
1120 break;
1121 }
drh1ccde152000-06-17 13:12:39 +00001122 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +00001123 }
drhbf4133c2001-10-13 02:59:08 +00001124 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00001125 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001126 sqlite3ExprCode(pParse, pExpr->pLeft);
1127 sqlite3VdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001128 break;
1129 }
1130 case TK_ISNULL:
1131 case TK_NOTNULL: {
1132 int dest;
danielk19774adee202004-05-08 08:23:19 +00001133 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1134 sqlite3ExprCode(pParse, pExpr->pLeft);
1135 dest = sqlite3VdbeCurrentAddr(v) + 2;
1136 sqlite3VdbeAddOp(v, op, 1, dest);
1137 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);
drhcce7d172000-05-31 15:34:51 +00001138 break;
1139 }
drh22827922000-06-06 17:27:05 +00001140 case TK_AGG_FUNCTION: {
danielk19774adee202004-05-08 08:23:19 +00001141 sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drh22827922000-06-06 17:27:05 +00001142 break;
1143 }
drh4b59ab52002-08-24 18:24:51 +00001144 case TK_GLOB:
1145 case TK_LIKE:
drhcce7d172000-05-31 15:34:51 +00001146 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00001147 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00001148 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00001149 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00001150 int nId;
1151 const char *zId;
1152 getFunctionName(pExpr, &zId, &nId);
danielk19774adee202004-05-08 08:23:19 +00001153 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, 0);
drh0bce8352002-02-28 00:41:10 +00001154 assert( pDef!=0 );
danielk19774adee202004-05-08 08:23:19 +00001155 nExpr = sqlite3ExprCodeExprList(pParse, pList, pDef->includeTypes);
1156 sqlite3VdbeOp3(v, OP_Function, nExpr, 0, (char*)pDef, P3_POINTER);
drhcce7d172000-05-31 15:34:51 +00001157 break;
1158 }
drh19a775c2000-06-05 18:54:46 +00001159 case TK_SELECT: {
danielk19774adee202004-05-08 08:23:19 +00001160 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drh19a775c2000-06-05 18:54:46 +00001161 break;
1162 }
drhfef52082000-06-06 01:50:43 +00001163 case TK_IN: {
1164 int addr;
danielk19774adee202004-05-08 08:23:19 +00001165 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1166 sqlite3ExprCode(pParse, pExpr->pLeft);
1167 addr = sqlite3VdbeCurrentAddr(v);
1168 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4);
1169 sqlite3VdbeAddOp(v, OP_Pop, 2, 0);
1170 sqlite3VdbeAddOp(v, OP_String, 0, 0);
1171 sqlite3VdbeAddOp(v, OP_Goto, 0, addr+6);
drhfef52082000-06-06 01:50:43 +00001172 if( pExpr->pSelect ){
danielk19774adee202004-05-08 08:23:19 +00001173 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+6);
drhfef52082000-06-06 01:50:43 +00001174 }else{
danielk19774adee202004-05-08 08:23:19 +00001175 sqlite3VdbeAddOp(v, OP_SetFound, pExpr->iTable, addr+6);
drhfef52082000-06-06 01:50:43 +00001176 }
danielk19774adee202004-05-08 08:23:19 +00001177 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);
drhfef52082000-06-06 01:50:43 +00001178 break;
1179 }
1180 case TK_BETWEEN: {
danielk19774adee202004-05-08 08:23:19 +00001181 sqlite3ExprCode(pParse, pExpr->pLeft);
1182 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
1183 sqlite3ExprCode(pParse, pExpr->pList->a[0].pExpr);
1184 sqlite3VdbeAddOp(v, OP_Ge, 0, 0);
1185 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
1186 sqlite3ExprCode(pParse, pExpr->pList->a[1].pExpr);
1187 sqlite3VdbeAddOp(v, OP_Le, 0, 0);
1188 sqlite3VdbeAddOp(v, OP_And, 0, 0);
drhfef52082000-06-06 01:50:43 +00001189 break;
1190 }
drh51e9a442004-01-16 16:42:53 +00001191 case TK_UPLUS:
drha2e00042002-01-22 03:13:42 +00001192 case TK_AS: {
danielk19774adee202004-05-08 08:23:19 +00001193 sqlite3ExprCode(pParse, pExpr->pLeft);
drha2e00042002-01-22 03:13:42 +00001194 break;
1195 }
drh17a7f8d2002-03-24 13:13:27 +00001196 case TK_CASE: {
1197 int expr_end_label;
drhf5905aa2002-05-26 20:54:33 +00001198 int jumpInst;
1199 int addr;
1200 int nExpr;
drh17a7f8d2002-03-24 13:13:27 +00001201 int i;
1202
1203 assert(pExpr->pList);
1204 assert((pExpr->pList->nExpr % 2) == 0);
1205 assert(pExpr->pList->nExpr > 0);
drhf5905aa2002-05-26 20:54:33 +00001206 nExpr = pExpr->pList->nExpr;
danielk19774adee202004-05-08 08:23:19 +00001207 expr_end_label = sqlite3VdbeMakeLabel(v);
drh17a7f8d2002-03-24 13:13:27 +00001208 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001209 sqlite3ExprCode(pParse, pExpr->pLeft);
drh17a7f8d2002-03-24 13:13:27 +00001210 }
drhf5905aa2002-05-26 20:54:33 +00001211 for(i=0; i<nExpr; i=i+2){
danielk19774adee202004-05-08 08:23:19 +00001212 sqlite3ExprCode(pParse, pExpr->pList->a[i].pExpr);
drh17a7f8d2002-03-24 13:13:27 +00001213 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001214 sqlite3VdbeAddOp(v, OP_Dup, 1, 1);
1215 jumpInst = sqlite3VdbeAddOp(v, OP_Ne, 1, 0);
1216 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001217 }else{
danielk19774adee202004-05-08 08:23:19 +00001218 jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001219 }
danielk19774adee202004-05-08 08:23:19 +00001220 sqlite3ExprCode(pParse, pExpr->pList->a[i+1].pExpr);
1221 sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label);
1222 addr = sqlite3VdbeCurrentAddr(v);
1223 sqlite3VdbeChangeP2(v, jumpInst, addr);
drh17a7f8d2002-03-24 13:13:27 +00001224 }
drhf570f012002-05-31 15:51:25 +00001225 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001226 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhf570f012002-05-31 15:51:25 +00001227 }
drh17a7f8d2002-03-24 13:13:27 +00001228 if( pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001229 sqlite3ExprCode(pParse, pExpr->pRight);
drh17a7f8d2002-03-24 13:13:27 +00001230 }else{
danielk19774adee202004-05-08 08:23:19 +00001231 sqlite3VdbeAddOp(v, OP_String, 0, 0);
drh17a7f8d2002-03-24 13:13:27 +00001232 }
danielk19774adee202004-05-08 08:23:19 +00001233 sqlite3VdbeResolveLabel(v, expr_end_label);
danielk19776f349032002-06-11 02:25:40 +00001234 break;
1235 }
1236 case TK_RAISE: {
1237 if( !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +00001238 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00001239 "RAISE() may only be used within a trigger-program");
danielk19776f349032002-06-11 02:25:40 +00001240 pParse->nErr++;
1241 return;
1242 }
1243 if( pExpr->iColumn == OE_Rollback ||
1244 pExpr->iColumn == OE_Abort ||
1245 pExpr->iColumn == OE_Fail ){
danielk19774adee202004-05-08 08:23:19 +00001246 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn,
drh701a0ae2004-02-22 20:05:00 +00001247 pExpr->token.z, pExpr->token.n);
danielk19774adee202004-05-08 08:23:19 +00001248 sqlite3VdbeDequoteP3(v, -1);
danielk19776f349032002-06-11 02:25:40 +00001249 } else {
1250 assert( pExpr->iColumn == OE_Ignore );
danielk19774adee202004-05-08 08:23:19 +00001251 sqlite3VdbeOp3(v, OP_Goto, 0, pParse->trigStack->ignoreJump,
drh701a0ae2004-02-22 20:05:00 +00001252 "(IGNORE jump)", 0);
danielk19776f349032002-06-11 02:25:40 +00001253 }
drh17a7f8d2002-03-24 13:13:27 +00001254 }
1255 break;
drhcce7d172000-05-31 15:34:51 +00001256 }
drhcce7d172000-05-31 15:34:51 +00001257}
1258
1259/*
drh268380c2004-02-25 13:47:31 +00001260** Generate code that pushes the value of every element of the given
1261** expression list onto the stack. If the includeTypes flag is true,
1262** then also push a string that is the datatype of each element onto
1263** the stack after the value.
1264**
1265** Return the number of elements pushed onto the stack.
1266*/
danielk19774adee202004-05-08 08:23:19 +00001267int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00001268 Parse *pParse, /* Parsing context */
1269 ExprList *pList, /* The expression list to be coded */
1270 int includeTypes /* TRUE to put datatypes on the stack too */
1271){
1272 struct ExprList_item *pItem;
1273 int i, n;
1274 Vdbe *v;
1275 if( pList==0 ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001276 v = sqlite3GetVdbe(pParse);
drh268380c2004-02-25 13:47:31 +00001277 n = pList->nExpr;
1278 for(pItem=pList->a, i=0; i<n; i++, pItem++){
danielk19774adee202004-05-08 08:23:19 +00001279 sqlite3ExprCode(pParse, pItem->pExpr);
drh268380c2004-02-25 13:47:31 +00001280 if( includeTypes ){
danielk19774adee202004-05-08 08:23:19 +00001281 sqlite3VdbeOp3(v, OP_String, 0, 0,
1282 sqlite3ExprType(pItem->pExpr)==SQLITE_SO_NUM ? "numeric" : "text",
drh268380c2004-02-25 13:47:31 +00001283 P3_STATIC);
1284 }
1285 }
1286 return includeTypes ? n*2 : n;
1287}
1288
1289/*
drhcce7d172000-05-31 15:34:51 +00001290** Generate code for a boolean expression such that a jump is made
1291** to the label "dest" if the expression is true but execution
1292** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00001293**
1294** If the expression evaluates to NULL (neither true nor false), then
1295** take the jump if the jumpIfNull flag is true.
drhcce7d172000-05-31 15:34:51 +00001296*/
danielk19774adee202004-05-08 08:23:19 +00001297void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001298 Vdbe *v = pParse->pVdbe;
1299 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001300 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001301 switch( pExpr->op ){
1302 case TK_LT: op = OP_Lt; break;
1303 case TK_LE: op = OP_Le; break;
1304 case TK_GT: op = OP_Gt; break;
1305 case TK_GE: op = OP_Ge; break;
1306 case TK_NE: op = OP_Ne; break;
1307 case TK_EQ: op = OP_Eq; break;
drhcce7d172000-05-31 15:34:51 +00001308 case TK_ISNULL: op = OP_IsNull; break;
1309 case TK_NOTNULL: op = OP_NotNull; break;
1310 default: break;
1311 }
1312 switch( pExpr->op ){
1313 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001314 int d2 = sqlite3VdbeMakeLabel(v);
1315 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
1316 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
1317 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001318 break;
1319 }
1320 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001321 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
1322 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001323 break;
1324 }
1325 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001326 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001327 break;
1328 }
1329 case TK_LT:
1330 case TK_LE:
1331 case TK_GT:
1332 case TK_GE:
1333 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00001334 case TK_EQ: {
danielk19774adee202004-05-08 08:23:19 +00001335 sqlite3ExprCode(pParse, pExpr->pLeft);
1336 sqlite3ExprCode(pParse, pExpr->pRight);
1337 if( pParse->db->file_format>=4 && sqlite3ExprType(pExpr)==SQLITE_SO_TEXT ){
drhc9b84a12002-06-20 11:36:48 +00001338 op += 6; /* Convert numeric opcodes to text opcodes */
1339 }
danielk19774adee202004-05-08 08:23:19 +00001340 sqlite3VdbeAddOp(v, op, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001341 break;
1342 }
1343 case TK_ISNULL:
1344 case TK_NOTNULL: {
danielk19774adee202004-05-08 08:23:19 +00001345 sqlite3ExprCode(pParse, pExpr->pLeft);
1346 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001347 break;
1348 }
drhfef52082000-06-06 01:50:43 +00001349 case TK_IN: {
drhf5905aa2002-05-26 20:54:33 +00001350 int addr;
danielk19774adee202004-05-08 08:23:19 +00001351 sqlite3ExprCode(pParse, pExpr->pLeft);
1352 addr = sqlite3VdbeCurrentAddr(v);
1353 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+3);
1354 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
1355 sqlite3VdbeAddOp(v, OP_Goto, 0, jumpIfNull ? dest : addr+4);
drhfef52082000-06-06 01:50:43 +00001356 if( pExpr->pSelect ){
danielk19774adee202004-05-08 08:23:19 +00001357 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001358 }else{
danielk19774adee202004-05-08 08:23:19 +00001359 sqlite3VdbeAddOp(v, OP_SetFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001360 }
1361 break;
1362 }
1363 case TK_BETWEEN: {
drhf5905aa2002-05-26 20:54:33 +00001364 int addr;
danielk19774adee202004-05-08 08:23:19 +00001365 sqlite3ExprCode(pParse, pExpr->pLeft);
1366 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
1367 sqlite3ExprCode(pParse, pExpr->pList->a[0].pExpr);
1368 addr = sqlite3VdbeAddOp(v, OP_Lt, !jumpIfNull, 0);
1369 sqlite3ExprCode(pParse, pExpr->pList->a[1].pExpr);
1370 sqlite3VdbeAddOp(v, OP_Le, jumpIfNull, dest);
1371 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
1372 sqlite3VdbeChangeP2(v, addr, sqlite3VdbeCurrentAddr(v));
1373 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00001374 break;
1375 }
drhcce7d172000-05-31 15:34:51 +00001376 default: {
danielk19774adee202004-05-08 08:23:19 +00001377 sqlite3ExprCode(pParse, pExpr);
1378 sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001379 break;
1380 }
1381 }
1382}
1383
1384/*
drh66b89c82000-11-28 20:47:17 +00001385** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00001386** to the label "dest" if the expression is false but execution
1387** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00001388**
1389** If the expression evaluates to NULL (neither true nor false) then
1390** jump if jumpIfNull is true or fall through if jumpIfNull is false.
drhcce7d172000-05-31 15:34:51 +00001391*/
danielk19774adee202004-05-08 08:23:19 +00001392void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001393 Vdbe *v = pParse->pVdbe;
1394 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001395 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001396 switch( pExpr->op ){
1397 case TK_LT: op = OP_Ge; break;
1398 case TK_LE: op = OP_Gt; break;
1399 case TK_GT: op = OP_Le; break;
1400 case TK_GE: op = OP_Lt; break;
1401 case TK_NE: op = OP_Eq; break;
1402 case TK_EQ: op = OP_Ne; break;
drhcce7d172000-05-31 15:34:51 +00001403 case TK_ISNULL: op = OP_NotNull; break;
1404 case TK_NOTNULL: op = OP_IsNull; break;
1405 default: break;
1406 }
1407 switch( pExpr->op ){
1408 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001409 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
1410 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001411 break;
1412 }
1413 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001414 int d2 = sqlite3VdbeMakeLabel(v);
1415 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
1416 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
1417 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001418 break;
1419 }
1420 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001421 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001422 break;
1423 }
1424 case TK_LT:
1425 case TK_LE:
1426 case TK_GT:
1427 case TK_GE:
1428 case TK_NE:
1429 case TK_EQ: {
danielk19774adee202004-05-08 08:23:19 +00001430 if( pParse->db->file_format>=4 && sqlite3ExprType(pExpr)==SQLITE_SO_TEXT ){
drh8f619cc2002-09-08 00:04:50 +00001431 /* Convert numeric comparison opcodes into text comparison opcodes.
1432 ** This step depends on the fact that the text comparision opcodes are
1433 ** always 6 greater than their corresponding numeric comparison
1434 ** opcodes.
1435 */
1436 assert( OP_Eq+6 == OP_StrEq );
1437 op += 6;
drhc9b84a12002-06-20 11:36:48 +00001438 }
danielk19774adee202004-05-08 08:23:19 +00001439 sqlite3ExprCode(pParse, pExpr->pLeft);
1440 sqlite3ExprCode(pParse, pExpr->pRight);
1441 sqlite3VdbeAddOp(v, op, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001442 break;
1443 }
drhcce7d172000-05-31 15:34:51 +00001444 case TK_ISNULL:
1445 case TK_NOTNULL: {
danielk19774adee202004-05-08 08:23:19 +00001446 sqlite3ExprCode(pParse, pExpr->pLeft);
1447 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001448 break;
1449 }
drhfef52082000-06-06 01:50:43 +00001450 case TK_IN: {
drhf5905aa2002-05-26 20:54:33 +00001451 int addr;
danielk19774adee202004-05-08 08:23:19 +00001452 sqlite3ExprCode(pParse, pExpr->pLeft);
1453 addr = sqlite3VdbeCurrentAddr(v);
1454 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+3);
1455 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
1456 sqlite3VdbeAddOp(v, OP_Goto, 0, jumpIfNull ? dest : addr+4);
drhfef52082000-06-06 01:50:43 +00001457 if( pExpr->pSelect ){
danielk19774adee202004-05-08 08:23:19 +00001458 sqlite3VdbeAddOp(v, OP_NotFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001459 }else{
danielk19774adee202004-05-08 08:23:19 +00001460 sqlite3VdbeAddOp(v, OP_SetNotFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001461 }
1462 break;
1463 }
1464 case TK_BETWEEN: {
1465 int addr;
danielk19774adee202004-05-08 08:23:19 +00001466 sqlite3ExprCode(pParse, pExpr->pLeft);
1467 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
1468 sqlite3ExprCode(pParse, pExpr->pList->a[0].pExpr);
1469 addr = sqlite3VdbeCurrentAddr(v);
1470 sqlite3VdbeAddOp(v, OP_Ge, !jumpIfNull, addr+3);
1471 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
1472 sqlite3VdbeAddOp(v, OP_Goto, 0, dest);
1473 sqlite3ExprCode(pParse, pExpr->pList->a[1].pExpr);
1474 sqlite3VdbeAddOp(v, OP_Gt, jumpIfNull, dest);
drhfef52082000-06-06 01:50:43 +00001475 break;
1476 }
drhcce7d172000-05-31 15:34:51 +00001477 default: {
danielk19774adee202004-05-08 08:23:19 +00001478 sqlite3ExprCode(pParse, pExpr);
1479 sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001480 break;
1481 }
1482 }
1483}
drh22827922000-06-06 17:27:05 +00001484
1485/*
1486** Do a deep comparison of two expression trees. Return TRUE (non-zero)
1487** if they are identical and return FALSE if they differ in any way.
1488*/
danielk19774adee202004-05-08 08:23:19 +00001489int sqlite3ExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00001490 int i;
1491 if( pA==0 ){
1492 return pB==0;
1493 }else if( pB==0 ){
1494 return 0;
1495 }
1496 if( pA->op!=pB->op ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001497 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
1498 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00001499 if( pA->pList ){
1500 if( pB->pList==0 ) return 0;
1501 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
1502 for(i=0; i<pA->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00001503 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00001504 return 0;
1505 }
1506 }
1507 }else if( pB->pList ){
1508 return 0;
1509 }
1510 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00001511 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drh22827922000-06-06 17:27:05 +00001512 if( pA->token.z ){
1513 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00001514 if( pB->token.n!=pA->token.n ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001515 if( sqlite3StrNICmp(pA->token.z, pB->token.z, pB->token.n)!=0 ) return 0;
drh22827922000-06-06 17:27:05 +00001516 }
1517 return 1;
1518}
1519
1520/*
1521** Add a new element to the pParse->aAgg[] array and return its index.
1522*/
1523static int appendAggInfo(Parse *pParse){
1524 if( (pParse->nAgg & 0x7)==0 ){
1525 int amt = pParse->nAgg + 8;
drh6d4abfb2001-10-22 02:58:08 +00001526 AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0]));
1527 if( aAgg==0 ){
drh22827922000-06-06 17:27:05 +00001528 return -1;
1529 }
drh6d4abfb2001-10-22 02:58:08 +00001530 pParse->aAgg = aAgg;
drh22827922000-06-06 17:27:05 +00001531 }
1532 memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0]));
1533 return pParse->nAgg++;
1534}
1535
1536/*
1537** Analyze the given expression looking for aggregate functions and
1538** for variables that need to be added to the pParse->aAgg[] array.
1539** Make additional entries to the pParse->aAgg[] array as necessary.
1540**
1541** This routine should only be called after the expression has been
danielk19774adee202004-05-08 08:23:19 +00001542** analyzed by sqlite3ExprResolveIds() and sqlite3ExprCheck().
drh22827922000-06-06 17:27:05 +00001543**
1544** If errors are seen, leave an error message in zErrMsg and return
1545** the number of errors.
1546*/
danielk19774adee202004-05-08 08:23:19 +00001547int sqlite3ExprAnalyzeAggregates(Parse *pParse, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00001548 int i;
1549 AggExpr *aAgg;
1550 int nErr = 0;
1551
1552 if( pExpr==0 ) return 0;
1553 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00001554 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +00001555 aAgg = pParse->aAgg;
1556 for(i=0; i<pParse->nAgg; i++){
1557 if( aAgg[i].isAgg ) continue;
1558 if( aAgg[i].pExpr->iTable==pExpr->iTable
drh967e8b72000-06-21 13:59:10 +00001559 && aAgg[i].pExpr->iColumn==pExpr->iColumn ){
drh22827922000-06-06 17:27:05 +00001560 break;
1561 }
1562 }
1563 if( i>=pParse->nAgg ){
1564 i = appendAggInfo(pParse);
1565 if( i<0 ) return 1;
1566 pParse->aAgg[i].isAgg = 0;
1567 pParse->aAgg[i].pExpr = pExpr;
1568 }
drhaaf88722000-06-08 11:25:00 +00001569 pExpr->iAgg = i;
drh22827922000-06-06 17:27:05 +00001570 break;
1571 }
1572 case TK_AGG_FUNCTION: {
drh22827922000-06-06 17:27:05 +00001573 aAgg = pParse->aAgg;
1574 for(i=0; i<pParse->nAgg; i++){
1575 if( !aAgg[i].isAgg ) continue;
danielk19774adee202004-05-08 08:23:19 +00001576 if( sqlite3ExprCompare(aAgg[i].pExpr, pExpr) ){
drh22827922000-06-06 17:27:05 +00001577 break;
1578 }
1579 }
1580 if( i>=pParse->nAgg ){
1581 i = appendAggInfo(pParse);
1582 if( i<0 ) return 1;
1583 pParse->aAgg[i].isAgg = 1;
1584 pParse->aAgg[i].pExpr = pExpr;
danielk19774adee202004-05-08 08:23:19 +00001585 pParse->aAgg[i].pFunc = sqlite3FindFunction(pParse->db,
drh6977fea2002-10-22 23:38:04 +00001586 pExpr->token.z, pExpr->token.n,
drhf55f25f2002-02-28 01:46:11 +00001587 pExpr->pList ? pExpr->pList->nExpr : 0, 0);
drh22827922000-06-06 17:27:05 +00001588 }
1589 pExpr->iAgg = i;
1590 break;
1591 }
1592 default: {
1593 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001594 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pLeft);
drh22827922000-06-06 17:27:05 +00001595 }
1596 if( nErr==0 && pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001597 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pRight);
drh22827922000-06-06 17:27:05 +00001598 }
1599 if( nErr==0 && pExpr->pList ){
1600 int n = pExpr->pList->nExpr;
1601 int i;
1602 for(i=0; nErr==0 && i<n; i++){
danielk19774adee202004-05-08 08:23:19 +00001603 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pList->a[i].pExpr);
drh22827922000-06-06 17:27:05 +00001604 }
1605 }
1606 break;
1607 }
1608 }
1609 return nErr;
1610}
drh8e0a2f92002-02-23 23:45:45 +00001611
1612/*
1613** Locate a user function given a name and a number of arguments.
drh0bce8352002-02-28 00:41:10 +00001614** Return a pointer to the FuncDef structure that defines that
drh8e0a2f92002-02-23 23:45:45 +00001615** function, or return NULL if the function does not exist.
1616**
drh0bce8352002-02-28 00:41:10 +00001617** If the createFlag argument is true, then a new (blank) FuncDef
drh8e0a2f92002-02-23 23:45:45 +00001618** structure is created and liked into the "db" structure if a
1619** no matching function previously existed. When createFlag is true
1620** and the nArg parameter is -1, then only a function that accepts
1621** any number of arguments will be returned.
1622**
1623** If createFlag is false and nArg is -1, then the first valid
1624** function found is returned. A function is valid if either xFunc
1625** or xStep is non-zero.
1626*/
danielk19774adee202004-05-08 08:23:19 +00001627FuncDef *sqlite3FindFunction(
drh8e0a2f92002-02-23 23:45:45 +00001628 sqlite *db, /* An open database */
1629 const char *zName, /* Name of the function. Not null-terminated */
1630 int nName, /* Number of characters in the name */
1631 int nArg, /* Number of arguments. -1 means any number */
1632 int createFlag /* Create new entry if true and does not otherwise exist */
1633){
drh0bce8352002-02-28 00:41:10 +00001634 FuncDef *pFirst, *p, *pMaybe;
danielk19774adee202004-05-08 08:23:19 +00001635 pFirst = p = (FuncDef*)sqlite3HashFind(&db->aFunc, zName, nName);
drh1350b032002-02-27 19:00:20 +00001636 if( p && !createFlag && nArg<0 ){
drh8e0a2f92002-02-23 23:45:45 +00001637 while( p && p->xFunc==0 && p->xStep==0 ){ p = p->pNext; }
1638 return p;
1639 }
1640 pMaybe = 0;
1641 while( p && p->nArg!=nArg ){
1642 if( p->nArg<0 && !createFlag && (p->xFunc || p->xStep) ) pMaybe = p;
1643 p = p->pNext;
1644 }
1645 if( p && !createFlag && p->xFunc==0 && p->xStep==0 ){
1646 return 0;
1647 }
1648 if( p==0 && pMaybe ){
1649 assert( createFlag==0 );
1650 return pMaybe;
1651 }
drh89425d52002-02-28 03:04:48 +00001652 if( p==0 && createFlag && (p = sqliteMalloc(sizeof(*p)))!=0 ){
drh8e0a2f92002-02-23 23:45:45 +00001653 p->nArg = nArg;
1654 p->pNext = pFirst;
drhc9b84a12002-06-20 11:36:48 +00001655 p->dataType = pFirst ? pFirst->dataType : SQLITE_NUMERIC;
danielk19774adee202004-05-08 08:23:19 +00001656 sqlite3HashInsert(&db->aFunc, zName, nName, (void*)p);
drh8e0a2f92002-02-23 23:45:45 +00001657 }
1658 return p;
1659}
danielk19774adee202004-05-08 08:23:19 +00001660
1661
1662