blob: c774f73a5bf20e58efb7860466a6ad64547fe3c4 [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**
drh7f0f12e2004-05-21 13:39:50 +000015** $Id: expr.c,v 1.127 2004/05/21 13:39:51 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
danielk1977e014a832004-05-17 10:48:57 +000020char const *sqlite3AffinityString(char affinity){
21 switch( affinity ){
22 case SQLITE_AFF_INTEGER: return "i";
23 case SQLITE_AFF_NUMERIC: return "n";
24 case SQLITE_AFF_TEXT: return "t";
25 case SQLITE_AFF_NONE: return "o";
26 default:
27 assert(0);
28 }
29}
30
31
32/*
33** Return the 'affinity' of the expression pExpr if any.
34**
35** If pExpr is a column, a reference to a column via an 'AS' alias,
36** or a sub-select with a column as the return value, then the
37** affinity of that column is returned. Otherwise, 0x00 is returned,
38** indicating no affinity for the expression.
39**
40** i.e. the WHERE clause expresssions in the following statements all
41** have an affinity:
42**
43** CREATE TABLE t1(a);
44** SELECT * FROM t1 WHERE a;
45** SELECT a AS b FROM t1 WHERE b;
46** SELECT * FROM t1 WHERE (select a from t1);
47*/
danielk1977bf3b7212004-05-18 10:06:24 +000048char sqlite3ExprAffinity(Expr *pExpr){
danielk1977a37cdde2004-05-16 11:15:36 +000049 if( pExpr->op==TK_AS ){
danielk1977bf3b7212004-05-18 10:06:24 +000050 return sqlite3ExprAffinity(pExpr->pLeft);
danielk1977a37cdde2004-05-16 11:15:36 +000051 }
52 if( pExpr->op==TK_SELECT ){
danielk1977bf3b7212004-05-18 10:06:24 +000053 return sqlite3ExprAffinity(pExpr->pSelect->pEList->a[0].pExpr);
danielk1977a37cdde2004-05-16 11:15:36 +000054 }
55 return pExpr->affinity;
56}
57
drh53db1452004-05-20 13:54:53 +000058/*
59** pExpr is the left operand of a comparison operator. aff2 is the
60** type affinity of the right operand. This routine returns the
61** type affinity that should be used for the comparison operator.
62*/
danielk1977e014a832004-05-17 10:48:57 +000063char sqlite3CompareAffinity(Expr *pExpr, char aff2){
danielk1977bf3b7212004-05-18 10:06:24 +000064 char aff1 = sqlite3ExprAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +000065 if( aff1 && aff2 ){
66 /* Both sides of the comparison are columns. If one has numeric or
67 ** integer affinity, use that. Otherwise use no affinity.
68 */
69 if( aff1==SQLITE_AFF_INTEGER || aff2==SQLITE_AFF_INTEGER ){
70 return SQLITE_AFF_INTEGER;
71 }else if( aff1==SQLITE_AFF_NUMERIC || aff2==SQLITE_AFF_NUMERIC ){
72 return SQLITE_AFF_NUMERIC;
73 }else{
74 return SQLITE_AFF_NONE;
75 }
76 }else if( !aff1 && !aff2 ){
77 /* Neither side of the comparison is a column. Use numeric affinity
78 ** for the comparison.
79 */
80 return SQLITE_AFF_NUMERIC;
81 }else{
82 /* One side is a column, the other is not. Use the columns affinity. */
83 return (aff1 + aff2);
84 }
85}
86
drh53db1452004-05-20 13:54:53 +000087/*
88** pExpr is a comparison operator. Return the type affinity that should
89** be applied to both operands prior to doing the comparison.
90*/
danielk1977e014a832004-05-17 10:48:57 +000091static char comparisonAffinity(Expr *pExpr){
92 char aff;
93 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
94 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
95 pExpr->op==TK_NE );
96 assert( pExpr->pLeft );
danielk1977bf3b7212004-05-18 10:06:24 +000097 aff = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +000098 if( pExpr->pRight ){
99 aff = sqlite3CompareAffinity(pExpr->pRight, aff);
100 }
101 else if( pExpr->pSelect ){
102 aff = sqlite3CompareAffinity(pExpr->pSelect->pEList->a[0].pExpr, aff);
103 }
104 else if( !aff ){
105 aff = SQLITE_AFF_NUMERIC;
106 }
107 return aff;
108}
109
110/*
111** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
112** idx_affinity is the affinity of an indexed column. Return true
113** if the index with affinity idx_affinity may be used to implement
114** the comparison in pExpr.
115*/
116int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){
117 char aff = comparisonAffinity(pExpr);
118 return
119 (aff==SQLITE_AFF_NONE) ||
120 (aff==SQLITE_AFF_NUMERIC && idx_affinity==SQLITE_AFF_INTEGER) ||
121 (aff==SQLITE_AFF_INTEGER && idx_affinity==SQLITE_AFF_NUMERIC) ||
122 (aff==idx_affinity);
123}
124
danielk1977a37cdde2004-05-16 11:15:36 +0000125/*
126** Return the P1 value that should be used for a binary comparison
127** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
128** If jumpIfNull is true, then set the low byte of the returned
129** P1 value to tell the opcode to jump if either expression
130** evaluates to NULL.
131*/
danielk1977e014a832004-05-17 10:48:57 +0000132static int binaryCompareP1(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){
danielk1977bf3b7212004-05-18 10:06:24 +0000133 char aff = sqlite3ExprAffinity(pExpr2);
danielk1977e014a832004-05-17 10:48:57 +0000134 return (((int)sqlite3CompareAffinity(pExpr1, aff))<<8)+(jumpIfNull?1:0);
danielk1977a37cdde2004-05-16 11:15:36 +0000135}
136
drha2e00042002-01-22 03:13:42 +0000137/*
drha76b5df2002-02-23 02:32:10 +0000138** Construct a new expression node and return a pointer to it. Memory
139** for this node is obtained from sqliteMalloc(). The calling function
140** is responsible for making sure the node eventually gets freed.
141*/
danielk19774adee202004-05-08 08:23:19 +0000142Expr *sqlite3Expr(int op, Expr *pLeft, Expr *pRight, Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000143 Expr *pNew;
144 pNew = sqliteMalloc( sizeof(Expr) );
145 if( pNew==0 ){
drh4efc4752004-01-16 15:55:37 +0000146 /* When malloc fails, we leak memory from pLeft and pRight */
drha76b5df2002-02-23 02:32:10 +0000147 return 0;
148 }
149 pNew->op = op;
150 pNew->pLeft = pLeft;
151 pNew->pRight = pRight;
152 if( pToken ){
drh4b59ab52002-08-24 18:24:51 +0000153 assert( pToken->dyn==0 );
drha76b5df2002-02-23 02:32:10 +0000154 pNew->token = *pToken;
drh6977fea2002-10-22 23:38:04 +0000155 pNew->span = *pToken;
drha76b5df2002-02-23 02:32:10 +0000156 }else{
drh4efc4752004-01-16 15:55:37 +0000157 assert( pNew->token.dyn==0 );
158 assert( pNew->token.z==0 );
159 assert( pNew->token.n==0 );
drh6977fea2002-10-22 23:38:04 +0000160 if( pLeft && pRight ){
danielk19774adee202004-05-08 08:23:19 +0000161 sqlite3ExprSpan(pNew, &pLeft->span, &pRight->span);
drh6977fea2002-10-22 23:38:04 +0000162 }else{
163 pNew->span = pNew->token;
164 }
drha76b5df2002-02-23 02:32:10 +0000165 }
drha76b5df2002-02-23 02:32:10 +0000166 return pNew;
167}
168
169/*
drh6977fea2002-10-22 23:38:04 +0000170** Set the Expr.span field of the given expression to span all
drha76b5df2002-02-23 02:32:10 +0000171** text between the two given tokens.
172*/
danielk19774adee202004-05-08 08:23:19 +0000173void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
drh4efc4752004-01-16 15:55:37 +0000174 assert( pRight!=0 );
175 assert( pLeft!=0 );
176 /* Note: pExpr might be NULL due to a prior malloc failure */
177 if( pExpr && pRight->z && pLeft->z ){
drh4b59ab52002-08-24 18:24:51 +0000178 if( pLeft->dyn==0 && pRight->dyn==0 ){
drh6977fea2002-10-22 23:38:04 +0000179 pExpr->span.z = pLeft->z;
180 pExpr->span.n = pRight->n + Addr(pRight->z) - Addr(pLeft->z);
drh4b59ab52002-08-24 18:24:51 +0000181 }else{
drh6977fea2002-10-22 23:38:04 +0000182 pExpr->span.z = 0;
drh4b59ab52002-08-24 18:24:51 +0000183 }
drha76b5df2002-02-23 02:32:10 +0000184 }
185}
186
187/*
188** Construct a new expression node for a function with multiple
189** arguments.
190*/
danielk19774adee202004-05-08 08:23:19 +0000191Expr *sqlite3ExprFunction(ExprList *pList, Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000192 Expr *pNew;
193 pNew = sqliteMalloc( sizeof(Expr) );
194 if( pNew==0 ){
danielk19774adee202004-05-08 08:23:19 +0000195 /* sqlite3ExprListDelete(pList); // Leak pList when malloc fails */
drha76b5df2002-02-23 02:32:10 +0000196 return 0;
197 }
198 pNew->op = TK_FUNCTION;
199 pNew->pList = pList;
200 if( pToken ){
drh4b59ab52002-08-24 18:24:51 +0000201 assert( pToken->dyn==0 );
drha76b5df2002-02-23 02:32:10 +0000202 pNew->token = *pToken;
203 }else{
204 pNew->token.z = 0;
drha76b5df2002-02-23 02:32:10 +0000205 }
drh6977fea2002-10-22 23:38:04 +0000206 pNew->span = pNew->token;
drha76b5df2002-02-23 02:32:10 +0000207 return pNew;
208}
209
210/*
drha2e00042002-01-22 03:13:42 +0000211** Recursively delete an expression tree.
212*/
danielk19774adee202004-05-08 08:23:19 +0000213void sqlite3ExprDelete(Expr *p){
drha2e00042002-01-22 03:13:42 +0000214 if( p==0 ) return;
drh4efc4752004-01-16 15:55:37 +0000215 if( p->span.dyn ) sqliteFree((char*)p->span.z);
216 if( p->token.dyn ) sqliteFree((char*)p->token.z);
danielk19774adee202004-05-08 08:23:19 +0000217 sqlite3ExprDelete(p->pLeft);
218 sqlite3ExprDelete(p->pRight);
219 sqlite3ExprListDelete(p->pList);
220 sqlite3SelectDelete(p->pSelect);
drha2e00042002-01-22 03:13:42 +0000221 sqliteFree(p);
222}
223
drha76b5df2002-02-23 02:32:10 +0000224
225/*
drhff78bd22002-02-27 01:47:11 +0000226** The following group of routines make deep copies of expressions,
227** expression lists, ID lists, and select statements. The copies can
228** be deleted (by being passed to their respective ...Delete() routines)
229** without effecting the originals.
230**
danielk19774adee202004-05-08 08:23:19 +0000231** The expression list, ID, and source lists return by sqlite3ExprListDup(),
232** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
drhad3cab52002-05-24 02:04:32 +0000233** by subsequent calls to sqlite*ListAppend() routines.
drhff78bd22002-02-27 01:47:11 +0000234**
drhad3cab52002-05-24 02:04:32 +0000235** Any tables that the SrcList might point to are not duplicated.
drhff78bd22002-02-27 01:47:11 +0000236*/
danielk19774adee202004-05-08 08:23:19 +0000237Expr *sqlite3ExprDup(Expr *p){
drhff78bd22002-02-27 01:47:11 +0000238 Expr *pNew;
239 if( p==0 ) return 0;
drhfcb78a42003-01-18 20:11:05 +0000240 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000241 if( pNew==0 ) return 0;
drh3b167c72002-06-28 12:18:47 +0000242 memcpy(pNew, p, sizeof(*pNew));
drh6977fea2002-10-22 23:38:04 +0000243 if( p->token.z!=0 ){
drh4b59ab52002-08-24 18:24:51 +0000244 pNew->token.z = sqliteStrDup(p->token.z);
245 pNew->token.dyn = 1;
246 }else{
drh4efc4752004-01-16 15:55:37 +0000247 assert( pNew->token.z==0 );
drh4b59ab52002-08-24 18:24:51 +0000248 }
drh6977fea2002-10-22 23:38:04 +0000249 pNew->span.z = 0;
danielk19774adee202004-05-08 08:23:19 +0000250 pNew->pLeft = sqlite3ExprDup(p->pLeft);
251 pNew->pRight = sqlite3ExprDup(p->pRight);
252 pNew->pList = sqlite3ExprListDup(p->pList);
253 pNew->pSelect = sqlite3SelectDup(p->pSelect);
drhff78bd22002-02-27 01:47:11 +0000254 return pNew;
255}
danielk19774adee202004-05-08 08:23:19 +0000256void sqlite3TokenCopy(Token *pTo, Token *pFrom){
drh4b59ab52002-08-24 18:24:51 +0000257 if( pTo->dyn ) sqliteFree((char*)pTo->z);
drh4b59ab52002-08-24 18:24:51 +0000258 if( pFrom->z ){
259 pTo->n = pFrom->n;
260 pTo->z = sqliteStrNDup(pFrom->z, pFrom->n);
261 pTo->dyn = 1;
262 }else{
drh4b59ab52002-08-24 18:24:51 +0000263 pTo->z = 0;
drh4b59ab52002-08-24 18:24:51 +0000264 }
265}
danielk19774adee202004-05-08 08:23:19 +0000266ExprList *sqlite3ExprListDup(ExprList *p){
drhff78bd22002-02-27 01:47:11 +0000267 ExprList *pNew;
drh3e7bc9c2004-02-21 19:17:17 +0000268 struct ExprList_item *pItem;
drhff78bd22002-02-27 01:47:11 +0000269 int i;
270 if( p==0 ) return 0;
271 pNew = sqliteMalloc( sizeof(*pNew) );
272 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000273 pNew->nExpr = pNew->nAlloc = p->nExpr;
drh3e7bc9c2004-02-21 19:17:17 +0000274 pNew->a = pItem = sqliteMalloc( p->nExpr*sizeof(p->a[0]) );
drh1bdd9b52004-04-23 17:04:44 +0000275 if( pItem==0 ) return 0; /* Leaks memory after a malloc failure */
276 for(i=0; i<p->nExpr; i++, pItem++){
drh4b59ab52002-08-24 18:24:51 +0000277 Expr *pNewExpr, *pOldExpr;
danielk19774adee202004-05-08 08:23:19 +0000278 pItem->pExpr = pNewExpr = sqlite3ExprDup(pOldExpr = p->a[i].pExpr);
drh6977fea2002-10-22 23:38:04 +0000279 if( pOldExpr->span.z!=0 && pNewExpr ){
280 /* Always make a copy of the span for top-level expressions in the
drh4b59ab52002-08-24 18:24:51 +0000281 ** expression list. The logic in SELECT processing that determines
282 ** the names of columns in the result set needs this information */
danielk19774adee202004-05-08 08:23:19 +0000283 sqlite3TokenCopy(&pNewExpr->span, &pOldExpr->span);
drh4b59ab52002-08-24 18:24:51 +0000284 }
drh1f3e9052002-10-31 00:09:39 +0000285 assert( pNewExpr==0 || pNewExpr->span.z!=0
danielk197724b03fd2004-05-10 10:34:34 +0000286 || pOldExpr->span.z==0 || sqlite3_malloc_failed );
drh3e7bc9c2004-02-21 19:17:17 +0000287 pItem->zName = sqliteStrDup(p->a[i].zName);
288 pItem->sortOrder = p->a[i].sortOrder;
289 pItem->isAgg = p->a[i].isAgg;
290 pItem->done = 0;
drhff78bd22002-02-27 01:47:11 +0000291 }
292 return pNew;
293}
danielk19774adee202004-05-08 08:23:19 +0000294SrcList *sqlite3SrcListDup(SrcList *p){
drhad3cab52002-05-24 02:04:32 +0000295 SrcList *pNew;
296 int i;
drh113088e2003-03-20 01:16:58 +0000297 int nByte;
drhad3cab52002-05-24 02:04:32 +0000298 if( p==0 ) return 0;
drh113088e2003-03-20 01:16:58 +0000299 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
drh4efc4752004-01-16 15:55:37 +0000300 pNew = sqliteMallocRaw( nByte );
drhad3cab52002-05-24 02:04:32 +0000301 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000302 pNew->nSrc = pNew->nAlloc = p->nSrc;
drhad3cab52002-05-24 02:04:32 +0000303 for(i=0; i<p->nSrc; i++){
drh4efc4752004-01-16 15:55:37 +0000304 struct SrcList_item *pNewItem = &pNew->a[i];
305 struct SrcList_item *pOldItem = &p->a[i];
306 pNewItem->zDatabase = sqliteStrDup(pOldItem->zDatabase);
307 pNewItem->zName = sqliteStrDup(pOldItem->zName);
308 pNewItem->zAlias = sqliteStrDup(pOldItem->zAlias);
309 pNewItem->jointype = pOldItem->jointype;
310 pNewItem->iCursor = pOldItem->iCursor;
311 pNewItem->pTab = 0;
danielk19774adee202004-05-08 08:23:19 +0000312 pNewItem->pSelect = sqlite3SelectDup(pOldItem->pSelect);
313 pNewItem->pOn = sqlite3ExprDup(pOldItem->pOn);
314 pNewItem->pUsing = sqlite3IdListDup(pOldItem->pUsing);
drhad3cab52002-05-24 02:04:32 +0000315 }
316 return pNew;
317}
danielk19774adee202004-05-08 08:23:19 +0000318IdList *sqlite3IdListDup(IdList *p){
drhff78bd22002-02-27 01:47:11 +0000319 IdList *pNew;
320 int i;
321 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000322 pNew = sqliteMallocRaw( sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +0000323 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000324 pNew->nId = pNew->nAlloc = p->nId;
drh4efc4752004-01-16 15:55:37 +0000325 pNew->a = sqliteMallocRaw( p->nId*sizeof(p->a[0]) );
drhe4697f52002-05-23 02:09:03 +0000326 if( pNew->a==0 ) return 0;
drhff78bd22002-02-27 01:47:11 +0000327 for(i=0; i<p->nId; i++){
drh4efc4752004-01-16 15:55:37 +0000328 struct IdList_item *pNewItem = &pNew->a[i];
329 struct IdList_item *pOldItem = &p->a[i];
330 pNewItem->zName = sqliteStrDup(pOldItem->zName);
331 pNewItem->idx = pOldItem->idx;
drhff78bd22002-02-27 01:47:11 +0000332 }
333 return pNew;
334}
danielk19774adee202004-05-08 08:23:19 +0000335Select *sqlite3SelectDup(Select *p){
drhff78bd22002-02-27 01:47:11 +0000336 Select *pNew;
337 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000338 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000339 if( pNew==0 ) return 0;
340 pNew->isDistinct = p->isDistinct;
danielk19774adee202004-05-08 08:23:19 +0000341 pNew->pEList = sqlite3ExprListDup(p->pEList);
342 pNew->pSrc = sqlite3SrcListDup(p->pSrc);
343 pNew->pWhere = sqlite3ExprDup(p->pWhere);
344 pNew->pGroupBy = sqlite3ExprListDup(p->pGroupBy);
345 pNew->pHaving = sqlite3ExprDup(p->pHaving);
346 pNew->pOrderBy = sqlite3ExprListDup(p->pOrderBy);
drhff78bd22002-02-27 01:47:11 +0000347 pNew->op = p->op;
danielk19774adee202004-05-08 08:23:19 +0000348 pNew->pPrior = sqlite3SelectDup(p->pPrior);
drhff78bd22002-02-27 01:47:11 +0000349 pNew->nLimit = p->nLimit;
350 pNew->nOffset = p->nOffset;
351 pNew->zSelect = 0;
drh7b58dae2003-07-20 01:16:46 +0000352 pNew->iLimit = -1;
353 pNew->iOffset = -1;
drhff78bd22002-02-27 01:47:11 +0000354 return pNew;
355}
356
357
358/*
drha76b5df2002-02-23 02:32:10 +0000359** Add a new element to the end of an expression list. If pList is
360** initially NULL, then create a new expression list.
361*/
danielk19774adee202004-05-08 08:23:19 +0000362ExprList *sqlite3ExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){
drha76b5df2002-02-23 02:32:10 +0000363 if( pList==0 ){
364 pList = sqliteMalloc( sizeof(ExprList) );
365 if( pList==0 ){
danielk19774adee202004-05-08 08:23:19 +0000366 /* sqlite3ExprDelete(pExpr); // Leak memory if malloc fails */
drha76b5df2002-02-23 02:32:10 +0000367 return 0;
368 }
drh4efc4752004-01-16 15:55:37 +0000369 assert( pList->nAlloc==0 );
drha76b5df2002-02-23 02:32:10 +0000370 }
drh4305d102003-07-30 12:34:12 +0000371 if( pList->nAlloc<=pList->nExpr ){
drh4305d102003-07-30 12:34:12 +0000372 pList->nAlloc = pList->nAlloc*2 + 4;
drh4efc4752004-01-16 15:55:37 +0000373 pList->a = sqliteRealloc(pList->a, pList->nAlloc*sizeof(pList->a[0]));
374 if( pList->a==0 ){
danielk19774adee202004-05-08 08:23:19 +0000375 /* sqlite3ExprDelete(pExpr); // Leak memory if malloc fails */
drh4efc4752004-01-16 15:55:37 +0000376 pList->nExpr = pList->nAlloc = 0;
drha76b5df2002-02-23 02:32:10 +0000377 return pList;
378 }
drha76b5df2002-02-23 02:32:10 +0000379 }
drh4efc4752004-01-16 15:55:37 +0000380 assert( pList->a!=0 );
381 if( pExpr || pName ){
382 struct ExprList_item *pItem = &pList->a[pList->nExpr++];
383 memset(pItem, 0, sizeof(*pItem));
384 pItem->pExpr = pExpr;
drha76b5df2002-02-23 02:32:10 +0000385 if( pName ){
danielk19774adee202004-05-08 08:23:19 +0000386 sqlite3SetNString(&pItem->zName, pName->z, pName->n, 0);
387 sqlite3Dequote(pItem->zName);
drha76b5df2002-02-23 02:32:10 +0000388 }
389 }
390 return pList;
391}
392
393/*
394** Delete an entire expression list.
395*/
danielk19774adee202004-05-08 08:23:19 +0000396void sqlite3ExprListDelete(ExprList *pList){
drha76b5df2002-02-23 02:32:10 +0000397 int i;
398 if( pList==0 ) return;
drh1bdd9b52004-04-23 17:04:44 +0000399 assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
400 assert( pList->nExpr<=pList->nAlloc );
drha76b5df2002-02-23 02:32:10 +0000401 for(i=0; i<pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +0000402 sqlite3ExprDelete(pList->a[i].pExpr);
drha76b5df2002-02-23 02:32:10 +0000403 sqliteFree(pList->a[i].zName);
404 }
405 sqliteFree(pList->a);
406 sqliteFree(pList);
407}
408
409/*
drhfef52082000-06-06 01:50:43 +0000410** Walk an expression tree. Return 1 if the expression is constant
411** and 0 if it involves variables.
drh23989372002-05-21 13:43:04 +0000412**
413** For the purposes of this function, a double-quoted string (ex: "abc")
414** is considered a variable but a single-quoted string (ex: 'abc') is
415** a constant.
drhfef52082000-06-06 01:50:43 +0000416*/
danielk19774adee202004-05-08 08:23:19 +0000417int sqlite3ExprIsConstant(Expr *p){
drhfef52082000-06-06 01:50:43 +0000418 switch( p->op ){
419 case TK_ID:
drh967e8b72000-06-21 13:59:10 +0000420 case TK_COLUMN:
drhfef52082000-06-06 01:50:43 +0000421 case TK_DOT:
drh7bdc0c12003-04-19 17:27:24 +0000422 case TK_FUNCTION:
drhfef52082000-06-06 01:50:43 +0000423 return 0;
drh7bdc0c12003-04-19 17:27:24 +0000424 case TK_NULL:
drh23989372002-05-21 13:43:04 +0000425 case TK_STRING:
drh92086432002-01-22 14:11:29 +0000426 case TK_INTEGER:
427 case TK_FLOAT:
drh50457892003-09-06 01:10:47 +0000428 case TK_VARIABLE:
drh92086432002-01-22 14:11:29 +0000429 return 1;
drhfef52082000-06-06 01:50:43 +0000430 default: {
danielk19774adee202004-05-08 08:23:19 +0000431 if( p->pLeft && !sqlite3ExprIsConstant(p->pLeft) ) return 0;
432 if( p->pRight && !sqlite3ExprIsConstant(p->pRight) ) return 0;
drhfef52082000-06-06 01:50:43 +0000433 if( p->pList ){
434 int i;
435 for(i=0; i<p->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +0000436 if( !sqlite3ExprIsConstant(p->pList->a[i].pExpr) ) return 0;
drhfef52082000-06-06 01:50:43 +0000437 }
438 }
drh92086432002-01-22 14:11:29 +0000439 return p->pLeft!=0 || p->pRight!=0 || (p->pList && p->pList->nExpr>0);
drhfef52082000-06-06 01:50:43 +0000440 }
441 }
drh92086432002-01-22 14:11:29 +0000442 return 0;
drhfef52082000-06-06 01:50:43 +0000443}
444
445/*
drh202b2df2004-01-06 01:13:46 +0000446** If the given expression codes a constant integer that is small enough
447** to fit in a 32-bit integer, return 1 and put the value of the integer
448** in *pValue. If the expression is not an integer or if it is too big
449** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
drhe4de1fe2002-06-02 16:09:01 +0000450*/
danielk19774adee202004-05-08 08:23:19 +0000451int sqlite3ExprIsInteger(Expr *p, int *pValue){
drhe4de1fe2002-06-02 16:09:01 +0000452 switch( p->op ){
453 case TK_INTEGER: {
drhfec19aa2004-05-19 20:41:03 +0000454 if( sqlite3GetInt32(p->token.z, pValue) ){
drh202b2df2004-01-06 01:13:46 +0000455 return 1;
456 }
457 break;
drhe4de1fe2002-06-02 16:09:01 +0000458 }
459 case TK_STRING: {
drhbd790ee2002-06-02 18:22:06 +0000460 const char *z = p->token.z;
drhe4de1fe2002-06-02 16:09:01 +0000461 int n = p->token.n;
drhbd790ee2002-06-02 18:22:06 +0000462 if( n>0 && z[0]=='-' ){ z++; n--; }
drhe4de1fe2002-06-02 16:09:01 +0000463 while( n>0 && *z && isdigit(*z) ){ z++; n--; }
drhfec19aa2004-05-19 20:41:03 +0000464 if( n==0 && sqlite3GetInt32(p->token.z, pValue) ){
drhe4de1fe2002-06-02 16:09:01 +0000465 return 1;
466 }
467 break;
468 }
drh4b59ab52002-08-24 18:24:51 +0000469 case TK_UPLUS: {
danielk19774adee202004-05-08 08:23:19 +0000470 return sqlite3ExprIsInteger(p->pLeft, pValue);
drh4b59ab52002-08-24 18:24:51 +0000471 }
drhe4de1fe2002-06-02 16:09:01 +0000472 case TK_UMINUS: {
473 int v;
danielk19774adee202004-05-08 08:23:19 +0000474 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
drhe4de1fe2002-06-02 16:09:01 +0000475 *pValue = -v;
476 return 1;
477 }
478 break;
479 }
480 default: break;
481 }
482 return 0;
483}
484
485/*
drhc4a3c772001-04-04 11:48:57 +0000486** Return TRUE if the given string is a row-id column name.
487*/
danielk19774adee202004-05-08 08:23:19 +0000488int sqlite3IsRowid(const char *z){
489 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
490 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
491 if( sqlite3StrICmp(z, "OID")==0 ) return 1;
drhc4a3c772001-04-04 11:48:57 +0000492 return 0;
493}
494
495/*
drh8141f612004-01-25 22:44:58 +0000496** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
497** that name in the set of source tables in pSrcList and make the pExpr
498** expression node refer back to that source column. The following changes
499** are made to pExpr:
500**
501** pExpr->iDb Set the index in db->aDb[] of the database holding
502** the table.
503** pExpr->iTable Set to the cursor number for the table obtained
504** from pSrcList.
505** pExpr->iColumn Set to the column number within the table.
506** pExpr->dataType Set to the appropriate data type for the column.
507** pExpr->op Set to TK_COLUMN.
508** pExpr->pLeft Any expression this points to is deleted
509** pExpr->pRight Any expression this points to is deleted.
510**
511** The pDbToken is the name of the database (the "X"). This value may be
512** NULL meaning that name is of the form Y.Z or Z. Any available database
513** can be used. The pTableToken is the name of the table (the "Y"). This
514** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it
515** means that the form of the name is Z and that columns from any table
516** can be used.
517**
518** If the name cannot be resolved unambiguously, leave an error message
519** in pParse and return non-zero. Return zero on success.
520*/
521static int lookupName(
522 Parse *pParse, /* The parsing context */
523 Token *pDbToken, /* Name of the database containing table, or NULL */
524 Token *pTableToken, /* Name of table containing column, or NULL */
525 Token *pColumnToken, /* Name of the column. */
526 SrcList *pSrcList, /* List of tables used to resolve column names */
527 ExprList *pEList, /* List of expressions used to resolve "AS" */
528 Expr *pExpr /* Make this EXPR node point to the selected column */
529){
530 char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */
531 char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */
532 char *zCol = 0; /* Name of the column. The "Z" */
533 int i, j; /* Loop counters */
534 int cnt = 0; /* Number of matching column names */
535 int cntTab = 0; /* Number of matching table names */
drh7e26d752004-02-11 10:35:29 +0000536 sqlite *db = pParse->db; /* The database */
drh8141f612004-01-25 22:44:58 +0000537
538 assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
539 if( pDbToken && pDbToken->z ){
540 zDb = sqliteStrNDup(pDbToken->z, pDbToken->n);
danielk19774adee202004-05-08 08:23:19 +0000541 sqlite3Dequote(zDb);
drh8141f612004-01-25 22:44:58 +0000542 }else{
543 zDb = 0;
544 }
545 if( pTableToken && pTableToken->z ){
546 zTab = sqliteStrNDup(pTableToken->z, pTableToken->n);
danielk19774adee202004-05-08 08:23:19 +0000547 sqlite3Dequote(zTab);
drh8141f612004-01-25 22:44:58 +0000548 }else{
549 assert( zDb==0 );
550 zTab = 0;
551 }
552 zCol = sqliteStrNDup(pColumnToken->z, pColumnToken->n);
danielk19774adee202004-05-08 08:23:19 +0000553 sqlite3Dequote(zCol);
danielk197724b03fd2004-05-10 10:34:34 +0000554 if( sqlite3_malloc_failed ){
drh8141f612004-01-25 22:44:58 +0000555 return 1; /* Leak memory (zDb and zTab) if malloc fails */
556 }
557 assert( zTab==0 || pEList==0 );
558
559 pExpr->iTable = -1;
560 for(i=0; i<pSrcList->nSrc; i++){
561 struct SrcList_item *pItem = &pSrcList->a[i];
562 Table *pTab = pItem->pTab;
563 Column *pCol;
564
565 if( pTab==0 ) continue;
566 assert( pTab->nCol>0 );
567 if( zTab ){
568 if( pItem->zAlias ){
569 char *zTabName = pItem->zAlias;
danielk19774adee202004-05-08 08:23:19 +0000570 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
drh8141f612004-01-25 22:44:58 +0000571 }else{
572 char *zTabName = pTab->zName;
danielk19774adee202004-05-08 08:23:19 +0000573 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
574 if( zDb!=0 && sqlite3StrICmp(db->aDb[pTab->iDb].zName, zDb)!=0 ){
drh8141f612004-01-25 22:44:58 +0000575 continue;
576 }
577 }
578 }
579 if( 0==(cntTab++) ){
580 pExpr->iTable = pItem->iCursor;
581 pExpr->iDb = pTab->iDb;
582 }
583 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
danielk19774adee202004-05-08 08:23:19 +0000584 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
drh8141f612004-01-25 22:44:58 +0000585 cnt++;
586 pExpr->iTable = pItem->iCursor;
587 pExpr->iDb = pTab->iDb;
588 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
589 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
danielk1977a37cdde2004-05-16 11:15:36 +0000590 pExpr->affinity = pTab->aCol[j].affinity;
drh8141f612004-01-25 22:44:58 +0000591 break;
592 }
593 }
594 }
595
596 /* If we have not already resolved the name, then maybe
597 ** it is a new.* or old.* trigger argument reference
598 */
599 if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
600 TriggerStack *pTriggerStack = pParse->trigStack;
601 Table *pTab = 0;
danielk19774adee202004-05-08 08:23:19 +0000602 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){
drh8141f612004-01-25 22:44:58 +0000603 pExpr->iTable = pTriggerStack->newIdx;
604 assert( pTriggerStack->pTab );
605 pTab = pTriggerStack->pTab;
danielk19774adee202004-05-08 08:23:19 +0000606 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab) == 0 ){
drh8141f612004-01-25 22:44:58 +0000607 pExpr->iTable = pTriggerStack->oldIdx;
608 assert( pTriggerStack->pTab );
609 pTab = pTriggerStack->pTab;
610 }
611
612 if( pTab ){
613 int j;
614 Column *pCol = pTab->aCol;
615
616 pExpr->iDb = pTab->iDb;
617 cntTab++;
618 for(j=0; j < pTab->nCol; j++, pCol++) {
danielk19774adee202004-05-08 08:23:19 +0000619 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
drh8141f612004-01-25 22:44:58 +0000620 cnt++;
621 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
danielk1977a37cdde2004-05-16 11:15:36 +0000622 pExpr->affinity = pTab->aCol[j].affinity;
drh8141f612004-01-25 22:44:58 +0000623 break;
624 }
625 }
626 }
627 }
628
629 /*
630 ** Perhaps the name is a reference to the ROWID
631 */
danielk19774adee202004-05-08 08:23:19 +0000632 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
drh8141f612004-01-25 22:44:58 +0000633 cnt = 1;
634 pExpr->iColumn = -1;
danielk1977a37cdde2004-05-16 11:15:36 +0000635 pExpr->affinity = SQLITE_AFF_INTEGER;
drh8141f612004-01-25 22:44:58 +0000636 }
637
638 /*
639 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
640 ** might refer to an result-set alias. This happens, for example, when
641 ** we are resolving names in the WHERE clause of the following command:
642 **
643 ** SELECT a+b AS x FROM table WHERE x<10;
644 **
645 ** In cases like this, replace pExpr with a copy of the expression that
646 ** forms the result set entry ("a+b" in the example) and return immediately.
647 ** Note that the expression in the result set should have already been
648 ** resolved by the time the WHERE clause is resolved.
649 */
650 if( cnt==0 && pEList!=0 ){
651 for(j=0; j<pEList->nExpr; j++){
652 char *zAs = pEList->a[j].zName;
danielk19774adee202004-05-08 08:23:19 +0000653 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
drh8141f612004-01-25 22:44:58 +0000654 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
655 pExpr->op = TK_AS;
656 pExpr->iColumn = j;
danielk19774adee202004-05-08 08:23:19 +0000657 pExpr->pLeft = sqlite3ExprDup(pEList->a[j].pExpr);
drh8141f612004-01-25 22:44:58 +0000658 sqliteFree(zCol);
659 assert( zTab==0 && zDb==0 );
660 return 0;
661 }
662 }
663 }
664
665 /*
666 ** If X and Y are NULL (in other words if only the column name Z is
667 ** supplied) and the value of Z is enclosed in double-quotes, then
668 ** Z is a string literal if it doesn't match any column names. In that
669 ** case, we need to return right away and not make any changes to
670 ** pExpr.
671 */
672 if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
673 sqliteFree(zCol);
674 return 0;
675 }
676
677 /*
678 ** cnt==0 means there was not match. cnt>1 means there were two or
679 ** more matches. Either way, we have an error.
680 */
681 if( cnt!=1 ){
682 char *z = 0;
683 char *zErr;
684 zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s";
685 if( zDb ){
danielk19774adee202004-05-08 08:23:19 +0000686 sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, 0);
drh8141f612004-01-25 22:44:58 +0000687 }else if( zTab ){
danielk19774adee202004-05-08 08:23:19 +0000688 sqlite3SetString(&z, zTab, ".", zCol, 0);
drh8141f612004-01-25 22:44:58 +0000689 }else{
690 z = sqliteStrDup(zCol);
691 }
danielk19774adee202004-05-08 08:23:19 +0000692 sqlite3ErrorMsg(pParse, zErr, z);
drh8141f612004-01-25 22:44:58 +0000693 sqliteFree(z);
694 }
695
696 /* Clean up and return
697 */
698 sqliteFree(zDb);
699 sqliteFree(zTab);
700 sqliteFree(zCol);
danielk19774adee202004-05-08 08:23:19 +0000701 sqlite3ExprDelete(pExpr->pLeft);
drh8141f612004-01-25 22:44:58 +0000702 pExpr->pLeft = 0;
danielk19774adee202004-05-08 08:23:19 +0000703 sqlite3ExprDelete(pExpr->pRight);
drh8141f612004-01-25 22:44:58 +0000704 pExpr->pRight = 0;
705 pExpr->op = TK_COLUMN;
danielk19774adee202004-05-08 08:23:19 +0000706 sqlite3AuthRead(pParse, pExpr, pSrcList);
drh8141f612004-01-25 22:44:58 +0000707 return cnt!=1;
708}
709
710/*
drhcce7d172000-05-31 15:34:51 +0000711** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +0000712** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +0000713** index to the table in the table list and a column offset. The
714** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
715** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +0000716** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +0000717** VDBE cursor number for a cursor that is pointing into the referenced
718** table. The Expr.iColumn value is changed to the index of the column
719** of the referenced table. The Expr.iColumn value for the special
720** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
721** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +0000722**
drhfef52082000-06-06 01:50:43 +0000723** We also check for instances of the IN operator. IN comes in two
724** forms:
725**
726** expr IN (exprlist)
727** and
728** expr IN (SELECT ...)
729**
730** The first form is handled by creating a set holding the list
731** of allowed values. The second form causes the SELECT to generate
732** a temporary table.
733**
734** This routine also looks for scalar SELECTs that are part of an expression.
drh19a775c2000-06-05 18:54:46 +0000735** If it finds any, it generates code to write the value of that select
736** into a memory cell.
drhcce7d172000-05-31 15:34:51 +0000737**
drh967e8b72000-06-21 13:59:10 +0000738** Unknown columns or tables provoke an error. The function returns
drhcce7d172000-05-31 15:34:51 +0000739** the number of errors seen and leaves an error message on pParse->zErrMsg.
740*/
danielk19774adee202004-05-08 08:23:19 +0000741int sqlite3ExprResolveIds(
drha2e00042002-01-22 03:13:42 +0000742 Parse *pParse, /* The parser context */
drh8141f612004-01-25 22:44:58 +0000743 SrcList *pSrcList, /* List of tables used to resolve column names */
drha2e00042002-01-22 03:13:42 +0000744 ExprList *pEList, /* List of expressions used to resolve "AS" */
745 Expr *pExpr /* The expression to be analyzed. */
746){
drh6a3ea0e2003-05-02 14:32:12 +0000747 int i;
748
drh8141f612004-01-25 22:44:58 +0000749 if( pExpr==0 || pSrcList==0 ) return 0;
750 for(i=0; i<pSrcList->nSrc; i++){
751 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab );
drh6a3ea0e2003-05-02 14:32:12 +0000752 }
drhcce7d172000-05-31 15:34:51 +0000753 switch( pExpr->op ){
drh23989372002-05-21 13:43:04 +0000754 /* Double-quoted strings (ex: "abc") are used as identifiers if
755 ** possible. Otherwise they remain as strings. Single-quoted
756 ** strings (ex: 'abc') are always string literals.
757 */
758 case TK_STRING: {
759 if( pExpr->token.z[0]=='\'' ) break;
760 /* Fall thru into the TK_ID case if this is a double-quoted string */
761 }
drh8141f612004-01-25 22:44:58 +0000762 /* A lone identifier is the name of a columnd.
drha2e00042002-01-22 03:13:42 +0000763 */
drhcce7d172000-05-31 15:34:51 +0000764 case TK_ID: {
drh8141f612004-01-25 22:44:58 +0000765 if( lookupName(pParse, 0, 0, &pExpr->token, pSrcList, pEList, pExpr) ){
drhcce7d172000-05-31 15:34:51 +0000766 return 1;
drhed6c8672003-01-12 18:02:16 +0000767 }
drhcce7d172000-05-31 15:34:51 +0000768 break;
769 }
770
drhd24cc422003-03-27 12:51:24 +0000771 /* A table name and column name: ID.ID
772 ** Or a database, table and column: ID.ID.ID
773 */
drhcce7d172000-05-31 15:34:51 +0000774 case TK_DOT: {
drh8141f612004-01-25 22:44:58 +0000775 Token *pColumn;
776 Token *pTable;
777 Token *pDb;
778 Expr *pRight;
drhcce7d172000-05-31 15:34:51 +0000779
drhcce7d172000-05-31 15:34:51 +0000780 pRight = pExpr->pRight;
drhd24cc422003-03-27 12:51:24 +0000781 if( pRight->op==TK_ID ){
drh8141f612004-01-25 22:44:58 +0000782 pDb = 0;
783 pTable = &pExpr->pLeft->token;
784 pColumn = &pRight->token;
drhd24cc422003-03-27 12:51:24 +0000785 }else{
drh8141f612004-01-25 22:44:58 +0000786 assert( pRight->op==TK_DOT );
787 pDb = &pExpr->pLeft->token;
788 pTable = &pRight->pLeft->token;
789 pColumn = &pRight->pRight->token;
drhd24cc422003-03-27 12:51:24 +0000790 }
drh8141f612004-01-25 22:44:58 +0000791 if( lookupName(pParse, pDb, pTable, pColumn, pSrcList, 0, pExpr) ){
drhdaffd0e2001-04-11 14:28:42 +0000792 return 1;
793 }
drhcce7d172000-05-31 15:34:51 +0000794 break;
795 }
796
drhfef52082000-06-06 01:50:43 +0000797 case TK_IN: {
danielk1977e014a832004-05-17 10:48:57 +0000798 char affinity;
danielk19774adee202004-05-08 08:23:19 +0000799 Vdbe *v = sqlite3GetVdbe(pParse);
drhd3d39e92004-05-20 22:16:29 +0000800 KeyInfo keyInfo;
801
drhfef52082000-06-06 01:50:43 +0000802 if( v==0 ) return 1;
danielk19774adee202004-05-08 08:23:19 +0000803 if( sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pLeft) ){
drhcfab11b2000-06-06 03:31:22 +0000804 return 1;
805 }
danielk1977bf3b7212004-05-18 10:06:24 +0000806 affinity = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +0000807
808 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
809 ** expression it is handled the same way. A temporary table is
810 ** filled with single-field index keys representing the results
811 ** from the SELECT or the <exprlist>.
812 **
813 ** If the 'x' expression is a column value, or the SELECT...
814 ** statement returns a column value, then the affinity of that
815 ** column is used to build the index keys. If both 'x' and the
816 ** SELECT... statement are columns, then numeric affinity is used
817 ** if either column has NUMERIC or INTEGER affinity. If neither
818 ** 'x' nor the SELECT... statement are columns, then numeric affinity
819 ** is used.
820 */
821 pExpr->iTable = pParse->nTab++;
drhd3d39e92004-05-20 22:16:29 +0000822 memset(&keyInfo, 0, sizeof(keyInfo));
823 keyInfo.nField = 1;
824 keyInfo.aColl[0] = pParse->db->pDfltColl;
825 sqlite3VdbeOp3(v, OP_OpenTemp, pExpr->iTable, 0, \
826 (char*)&keyInfo, P3_KEYINFO);
danielk1977e014a832004-05-17 10:48:57 +0000827
drhfef52082000-06-06 01:50:43 +0000828 if( pExpr->pSelect ){
829 /* Case 1: expr IN (SELECT ...)
830 **
danielk1977e014a832004-05-17 10:48:57 +0000831 ** Generate code to write the results of the select into the temporary
832 ** table allocated and opened above.
drhfef52082000-06-06 01:50:43 +0000833 */
danielk1977e014a832004-05-17 10:48:57 +0000834 int iParm = pExpr->iTable + (((int)affinity)<<16);
835 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
danielk1977bf3b7212004-05-18 10:06:24 +0000836 sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0);
drhfef52082000-06-06 01:50:43 +0000837 }else if( pExpr->pList ){
838 /* Case 2: expr IN (exprlist)
839 **
danielk1977e014a832004-05-17 10:48:57 +0000840 ** For each expression, build an index key from the evaluation and
841 ** store it in the temporary table. If <expr> is a column, then use
842 ** that columns affinity when building index keys. If <expr> is not
843 ** a column, use numeric affinity.
drhfef52082000-06-06 01:50:43 +0000844 */
danielk1977e014a832004-05-17 10:48:57 +0000845 int i;
846 char const *affStr;
847 if( !affinity ){
848 affinity = SQLITE_AFF_NUMERIC;
849 }
850 affStr = sqlite3AffinityString(affinity);
851
852 /* Loop through each expression in <exprlist>. */
drhfef52082000-06-06 01:50:43 +0000853 for(i=0; i<pExpr->pList->nExpr; i++){
854 Expr *pE2 = pExpr->pList->a[i].pExpr;
danielk1977e014a832004-05-17 10:48:57 +0000855
856 /* Check that the expression is constant and valid. */
danielk19774adee202004-05-08 08:23:19 +0000857 if( !sqlite3ExprIsConstant(pE2) ){
858 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +0000859 "right-hand side of IN operator must be constant");
drhfef52082000-06-06 01:50:43 +0000860 return 1;
861 }
danielk19774adee202004-05-08 08:23:19 +0000862 if( sqlite3ExprCheck(pParse, pE2, 0, 0) ){
drh4794b982000-06-06 13:54:14 +0000863 return 1;
864 }
danielk1977e014a832004-05-17 10:48:57 +0000865
866 /* Evaluate the expression and insert it into the temp table */
867 sqlite3ExprCode(pParse, pE2);
868 sqlite3VdbeOp3(v, OP_MakeKey, 1, 0, affStr, P3_STATIC);
869 sqlite3VdbeAddOp(v, OP_String, 0, 0);
870 sqlite3VdbeAddOp(v, OP_PutStrKey, pExpr->iTable, 0);
drhfef52082000-06-06 01:50:43 +0000871 }
872 }
drhcfab11b2000-06-06 03:31:22 +0000873 break;
drhfef52082000-06-06 01:50:43 +0000874 }
875
drh19a775c2000-06-05 18:54:46 +0000876 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +0000877 /* This has to be a scalar SELECT. Generate code to put the
878 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +0000879 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +0000880 */
drh967e8b72000-06-21 13:59:10 +0000881 pExpr->iColumn = pParse->nMem++;
danielk1977bf3b7212004-05-18 10:06:24 +0000882 if(sqlite3Select(pParse, pExpr->pSelect, SRT_Mem,pExpr->iColumn,0,0,0,0)){
drh19a775c2000-06-05 18:54:46 +0000883 return 1;
884 }
885 break;
886 }
887
drhcce7d172000-05-31 15:34:51 +0000888 /* For all else, just recursively walk the tree */
889 default: {
drh4794b982000-06-06 13:54:14 +0000890 if( pExpr->pLeft
danielk19774adee202004-05-08 08:23:19 +0000891 && sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pLeft) ){
drhcce7d172000-05-31 15:34:51 +0000892 return 1;
893 }
894 if( pExpr->pRight
danielk19774adee202004-05-08 08:23:19 +0000895 && sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pRight) ){
drhcce7d172000-05-31 15:34:51 +0000896 return 1;
897 }
898 if( pExpr->pList ){
899 int i;
900 ExprList *pList = pExpr->pList;
901 for(i=0; i<pList->nExpr; i++){
drh832508b2002-03-02 17:04:07 +0000902 Expr *pArg = pList->a[i].pExpr;
danielk19774adee202004-05-08 08:23:19 +0000903 if( sqlite3ExprResolveIds(pParse, pSrcList, pEList, pArg) ){
drhcce7d172000-05-31 15:34:51 +0000904 return 1;
905 }
906 }
907 }
908 }
909 }
910 return 0;
911}
912
drhcce7d172000-05-31 15:34:51 +0000913/*
drh4b59ab52002-08-24 18:24:51 +0000914** pExpr is a node that defines a function of some kind. It might
915** be a syntactic function like "count(x)" or it might be a function
916** that implements an operator, like "a LIKE b".
917**
918** This routine makes *pzName point to the name of the function and
919** *pnName hold the number of characters in the function name.
920*/
921static void getFunctionName(Expr *pExpr, const char **pzName, int *pnName){
922 switch( pExpr->op ){
923 case TK_FUNCTION: {
924 *pzName = pExpr->token.z;
drh6977fea2002-10-22 23:38:04 +0000925 *pnName = pExpr->token.n;
drh4b59ab52002-08-24 18:24:51 +0000926 break;
927 }
928 case TK_LIKE: {
929 *pzName = "like";
930 *pnName = 4;
931 break;
932 }
933 case TK_GLOB: {
934 *pzName = "glob";
935 *pnName = 4;
936 break;
937 }
938 default: {
939 *pzName = "can't happen";
940 *pnName = 12;
941 break;
942 }
943 }
944}
945
946/*
drhcce7d172000-05-31 15:34:51 +0000947** Error check the functions in an expression. Make sure all
948** function names are recognized and all functions have the correct
949** number of arguments. Leave an error message in pParse->zErrMsg
950** if anything is amiss. Return the number of errors.
951**
952** if pIsAgg is not null and this expression is an aggregate function
953** (like count(*) or max(value)) then write a 1 into *pIsAgg.
954*/
danielk19774adee202004-05-08 08:23:19 +0000955int sqlite3ExprCheck(Parse *pParse, Expr *pExpr, int allowAgg, int *pIsAgg){
drhcce7d172000-05-31 15:34:51 +0000956 int nErr = 0;
957 if( pExpr==0 ) return 0;
drhcce7d172000-05-31 15:34:51 +0000958 switch( pExpr->op ){
drh4b59ab52002-08-24 18:24:51 +0000959 case TK_GLOB:
960 case TK_LIKE:
drhcce7d172000-05-31 15:34:51 +0000961 case TK_FUNCTION: {
drhc9b84a12002-06-20 11:36:48 +0000962 int n = pExpr->pList ? pExpr->pList->nExpr : 0; /* Number of arguments */
963 int no_such_func = 0; /* True if no such function exists */
drhc9b84a12002-06-20 11:36:48 +0000964 int wrong_num_args = 0; /* True if wrong number of arguments */
965 int is_agg = 0; /* True if is an aggregate function */
drhcce7d172000-05-31 15:34:51 +0000966 int i;
drh4b59ab52002-08-24 18:24:51 +0000967 int nId; /* Number of characters in function name */
968 const char *zId; /* The function name. */
drh0bce8352002-02-28 00:41:10 +0000969 FuncDef *pDef;
970
drh4b59ab52002-08-24 18:24:51 +0000971 getFunctionName(pExpr, &zId, &nId);
danielk19774adee202004-05-08 08:23:19 +0000972 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, 0);
drh0bce8352002-02-28 00:41:10 +0000973 if( pDef==0 ){
danielk19774adee202004-05-08 08:23:19 +0000974 pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, 0);
drh0bce8352002-02-28 00:41:10 +0000975 if( pDef==0 ){
drh268380c2004-02-25 13:47:31 +0000976 no_such_func = 1;
drh0bce8352002-02-28 00:41:10 +0000977 }else{
978 wrong_num_args = 1;
drhcce7d172000-05-31 15:34:51 +0000979 }
drh0bce8352002-02-28 00:41:10 +0000980 }else{
981 is_agg = pDef->xFunc==0;
drhcce7d172000-05-31 15:34:51 +0000982 }
drh8e0a2f92002-02-23 23:45:45 +0000983 if( is_agg && !allowAgg ){
danielk19774adee202004-05-08 08:23:19 +0000984 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId, zId);
drh8e0a2f92002-02-23 23:45:45 +0000985 nErr++;
986 is_agg = 0;
987 }else if( no_such_func ){
danielk19774adee202004-05-08 08:23:19 +0000988 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
drhcce7d172000-05-31 15:34:51 +0000989 nErr++;
drh8e0a2f92002-02-23 23:45:45 +0000990 }else if( wrong_num_args ){
danielk19774adee202004-05-08 08:23:19 +0000991 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
drhf7a9e1a2004-02-22 18:40:56 +0000992 nId, zId);
drh8e0a2f92002-02-23 23:45:45 +0000993 nErr++;
drhcce7d172000-05-31 15:34:51 +0000994 }
drhf7a9e1a2004-02-22 18:40:56 +0000995 if( is_agg ){
996 pExpr->op = TK_AGG_FUNCTION;
997 if( pIsAgg ) *pIsAgg = 1;
998 }
drhcce7d172000-05-31 15:34:51 +0000999 for(i=0; nErr==0 && i<n; i++){
danielk19774adee202004-05-08 08:23:19 +00001000 nErr = sqlite3ExprCheck(pParse, pExpr->pList->a[i].pExpr,
drh4cfa7932000-06-08 15:10:46 +00001001 allowAgg && !is_agg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +00001002 }
drhd3d39e92004-05-20 22:16:29 +00001003 /** TODO: Compute pExpr->affinity based on the expected return
1004 ** type of the function */
drhcce7d172000-05-31 15:34:51 +00001005 }
1006 default: {
1007 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001008 nErr = sqlite3ExprCheck(pParse, pExpr->pLeft, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +00001009 }
1010 if( nErr==0 && pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001011 nErr = sqlite3ExprCheck(pParse, pExpr->pRight, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +00001012 }
drhfef52082000-06-06 01:50:43 +00001013 if( nErr==0 && pExpr->pList ){
1014 int n = pExpr->pList->nExpr;
1015 int i;
1016 for(i=0; nErr==0 && i<n; i++){
drh22827922000-06-06 17:27:05 +00001017 Expr *pE2 = pExpr->pList->a[i].pExpr;
danielk19774adee202004-05-08 08:23:19 +00001018 nErr = sqlite3ExprCheck(pParse, pE2, allowAgg, pIsAgg);
drhfef52082000-06-06 01:50:43 +00001019 }
1020 }
drhcce7d172000-05-31 15:34:51 +00001021 break;
1022 }
1023 }
1024 return nErr;
1025}
1026
1027/*
drhd3d39e92004-05-20 22:16:29 +00001028** Return one of the SQLITE_AFF_* affinity types that indicates the likely
1029** data type of the result of the given expression.
1030**
1031** Not every expression has a fixed type. If the type cannot be determined
1032** at compile-time, then try to return the type affinity if the expression
1033** is a column. Otherwise just return SQLITE_AFF_NONE.
drhc9b84a12002-06-20 11:36:48 +00001034**
danielk19774adee202004-05-08 08:23:19 +00001035** The sqlite3ExprResolveIds() and sqlite3ExprCheck() routines must have
drhc9b84a12002-06-20 11:36:48 +00001036** both been called on the expression before it is passed to this routine.
1037*/
danielk19774adee202004-05-08 08:23:19 +00001038int sqlite3ExprType(Expr *p){
drhd3d39e92004-05-20 22:16:29 +00001039 if( p==0 ) return SQLITE_AFF_NONE;
drhc9b84a12002-06-20 11:36:48 +00001040 while( p ) switch( p->op ){
drhc9b84a12002-06-20 11:36:48 +00001041 case TK_CONCAT:
drh736c22b2004-05-21 02:14:24 +00001042 case TK_STRING:
drhd3d39e92004-05-20 22:16:29 +00001043 return SQLITE_AFF_TEXT;
drhc9b84a12002-06-20 11:36:48 +00001044
1045 case TK_AS:
1046 p = p->pLeft;
1047 break;
1048
drh736c22b2004-05-21 02:14:24 +00001049 case TK_VARIABLE:
drhd3d39e92004-05-20 22:16:29 +00001050 case TK_NULL:
1051 return SQLITE_AFF_NONE;
drhc9b84a12002-06-20 11:36:48 +00001052
drhd3d39e92004-05-20 22:16:29 +00001053 case TK_SELECT: /*** FIX ME ****/
1054 case TK_COLUMN: /*** FIX ME ****/
1055 case TK_CASE: /*** FIX ME ****/
drhb1363202002-06-26 02:45:03 +00001056
drhc9b84a12002-06-20 11:36:48 +00001057 default:
drhd3d39e92004-05-20 22:16:29 +00001058 return SQLITE_AFF_NUMERIC;
drhc9b84a12002-06-20 11:36:48 +00001059 }
drhd3d39e92004-05-20 22:16:29 +00001060 return SQLITE_AFF_NONE;
drhc9b84a12002-06-20 11:36:48 +00001061}
1062
1063/*
drhfec19aa2004-05-19 20:41:03 +00001064** Generate an instruction that will put the integer describe by
1065** text z[0..n-1] on the stack.
1066*/
1067static void codeInteger(Vdbe *v, const char *z, int n){
1068 int i;
1069 if( sqlite3GetInt32(z, &i) || (i=0, sqlite3FitsIn64Bits(z))!=0 ){
1070 sqlite3VdbeOp3(v, OP_Integer, i, 0, z, n);
1071 }else{
1072 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n);
1073 }
1074}
1075
1076/*
drhcce7d172000-05-31 15:34:51 +00001077** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +00001078** expression and leave the result on the top of stack.
drhcce7d172000-05-31 15:34:51 +00001079*/
danielk19774adee202004-05-08 08:23:19 +00001080void sqlite3ExprCode(Parse *pParse, Expr *pExpr){
drhcce7d172000-05-31 15:34:51 +00001081 Vdbe *v = pParse->pVdbe;
1082 int op;
drhdaffd0e2001-04-11 14:28:42 +00001083 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001084 switch( pExpr->op ){
1085 case TK_PLUS: op = OP_Add; break;
1086 case TK_MINUS: op = OP_Subtract; break;
1087 case TK_STAR: op = OP_Multiply; break;
1088 case TK_SLASH: op = OP_Divide; break;
1089 case TK_AND: op = OP_And; break;
1090 case TK_OR: op = OP_Or; break;
1091 case TK_LT: op = OP_Lt; break;
1092 case TK_LE: op = OP_Le; break;
1093 case TK_GT: op = OP_Gt; break;
1094 case TK_GE: op = OP_Ge; break;
1095 case TK_NE: op = OP_Ne; break;
1096 case TK_EQ: op = OP_Eq; break;
drhcce7d172000-05-31 15:34:51 +00001097 case TK_ISNULL: op = OP_IsNull; break;
1098 case TK_NOTNULL: op = OP_NotNull; break;
1099 case TK_NOT: op = OP_Not; break;
1100 case TK_UMINUS: op = OP_Negative; break;
drhbf4133c2001-10-13 02:59:08 +00001101 case TK_BITAND: op = OP_BitAnd; break;
1102 case TK_BITOR: op = OP_BitOr; break;
1103 case TK_BITNOT: op = OP_BitNot; break;
1104 case TK_LSHIFT: op = OP_ShiftLeft; break;
1105 case TK_RSHIFT: op = OP_ShiftRight; break;
1106 case TK_REM: op = OP_Remainder; break;
drhfec19aa2004-05-19 20:41:03 +00001107 case TK_FLOAT: op = OP_Real; break;
1108 case TK_STRING: op = OP_String; break;
drhcce7d172000-05-31 15:34:51 +00001109 default: break;
1110 }
1111 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00001112 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +00001113 if( pParse->useAgg ){
danielk19774adee202004-05-08 08:23:19 +00001114 sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drhc4a3c772001-04-04 11:48:57 +00001115 }else if( pExpr->iColumn>=0 ){
danielk19774adee202004-05-08 08:23:19 +00001116 sqlite3VdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn);
drhc4a3c772001-04-04 11:48:57 +00001117 }else{
danielk19774adee202004-05-08 08:23:19 +00001118 sqlite3VdbeAddOp(v, OP_Recno, pExpr->iTable, 0);
drh22827922000-06-06 17:27:05 +00001119 }
drhcce7d172000-05-31 15:34:51 +00001120 break;
1121 }
1122 case TK_INTEGER: {
drhfec19aa2004-05-19 20:41:03 +00001123 codeInteger(v, pExpr->token.z, pExpr->token.n);
1124 break;
1125 }
1126 case TK_FLOAT:
1127 case TK_STRING: {
1128 sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z, pExpr->token.n);
danielk19774adee202004-05-08 08:23:19 +00001129 sqlite3VdbeDequoteP3(v, -1);
drhcce7d172000-05-31 15:34:51 +00001130 break;
1131 }
1132 case TK_NULL: {
danielk19774adee202004-05-08 08:23:19 +00001133 sqlite3VdbeAddOp(v, OP_String, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001134 break;
1135 }
drh50457892003-09-06 01:10:47 +00001136 case TK_VARIABLE: {
danielk19774adee202004-05-08 08:23:19 +00001137 sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0);
drh50457892003-09-06 01:10:47 +00001138 break;
1139 }
drhc9b84a12002-06-20 11:36:48 +00001140 case TK_LT:
1141 case TK_LE:
1142 case TK_GT:
1143 case TK_GE:
1144 case TK_NE:
1145 case TK_EQ: {
danielk1977a37cdde2004-05-16 11:15:36 +00001146 int p1 = binaryCompareP1(pExpr->pLeft, pExpr->pRight, 0);
1147 sqlite3ExprCode(pParse, pExpr->pLeft);
1148 sqlite3ExprCode(pParse, pExpr->pRight);
1149 sqlite3VdbeAddOp(v, op, p1, 0);
1150 break;
drhc9b84a12002-06-20 11:36:48 +00001151 }
drhcce7d172000-05-31 15:34:51 +00001152 case TK_AND:
1153 case TK_OR:
1154 case TK_PLUS:
1155 case TK_STAR:
1156 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00001157 case TK_REM:
1158 case TK_BITAND:
1159 case TK_BITOR:
drhc9b84a12002-06-20 11:36:48 +00001160 case TK_SLASH: {
danielk19774adee202004-05-08 08:23:19 +00001161 sqlite3ExprCode(pParse, pExpr->pLeft);
1162 sqlite3ExprCode(pParse, pExpr->pRight);
1163 sqlite3VdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001164 break;
1165 }
drhbf4133c2001-10-13 02:59:08 +00001166 case TK_LSHIFT:
1167 case TK_RSHIFT: {
danielk19774adee202004-05-08 08:23:19 +00001168 sqlite3ExprCode(pParse, pExpr->pRight);
1169 sqlite3ExprCode(pParse, pExpr->pLeft);
1170 sqlite3VdbeAddOp(v, op, 0, 0);
drhbf4133c2001-10-13 02:59:08 +00001171 break;
1172 }
drh00400772000-06-16 20:51:26 +00001173 case TK_CONCAT: {
danielk19774adee202004-05-08 08:23:19 +00001174 sqlite3ExprCode(pParse, pExpr->pLeft);
1175 sqlite3ExprCode(pParse, pExpr->pRight);
1176 sqlite3VdbeAddOp(v, OP_Concat, 2, 0);
drh00400772000-06-16 20:51:26 +00001177 break;
1178 }
drhcce7d172000-05-31 15:34:51 +00001179 case TK_UMINUS: {
drhfec19aa2004-05-19 20:41:03 +00001180 Expr *pLeft = pExpr->pLeft;
1181 assert( pLeft );
1182 if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
1183 Token *p = &pLeft->token;
drh6e142f52000-06-08 13:36:40 +00001184 char *z = sqliteMalloc( p->n + 2 );
1185 sprintf(z, "-%.*s", p->n, p->z);
drhfec19aa2004-05-19 20:41:03 +00001186 if( pLeft->op==TK_FLOAT ){
1187 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001188 }else{
drhfec19aa2004-05-19 20:41:03 +00001189 codeInteger(v, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001190 }
drh6e142f52000-06-08 13:36:40 +00001191 sqliteFree(z);
1192 break;
1193 }
drh1ccde152000-06-17 13:12:39 +00001194 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +00001195 }
drhbf4133c2001-10-13 02:59:08 +00001196 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00001197 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001198 sqlite3ExprCode(pParse, pExpr->pLeft);
1199 sqlite3VdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001200 break;
1201 }
1202 case TK_ISNULL:
1203 case TK_NOTNULL: {
1204 int dest;
danielk19774adee202004-05-08 08:23:19 +00001205 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1206 sqlite3ExprCode(pParse, pExpr->pLeft);
1207 dest = sqlite3VdbeCurrentAddr(v) + 2;
1208 sqlite3VdbeAddOp(v, op, 1, dest);
1209 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);
drhcce7d172000-05-31 15:34:51 +00001210 }
danielk1977a37cdde2004-05-16 11:15:36 +00001211 break;
drh22827922000-06-06 17:27:05 +00001212 case TK_AGG_FUNCTION: {
danielk19774adee202004-05-08 08:23:19 +00001213 sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drh22827922000-06-06 17:27:05 +00001214 break;
1215 }
drh4b59ab52002-08-24 18:24:51 +00001216 case TK_GLOB:
1217 case TK_LIKE:
drhcce7d172000-05-31 15:34:51 +00001218 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00001219 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00001220 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00001221 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00001222 int nId;
1223 const char *zId;
1224 getFunctionName(pExpr, &zId, &nId);
danielk19774adee202004-05-08 08:23:19 +00001225 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, 0);
drh0bce8352002-02-28 00:41:10 +00001226 assert( pDef!=0 );
danielk19774adee202004-05-08 08:23:19 +00001227 nExpr = sqlite3ExprCodeExprList(pParse, pList, pDef->includeTypes);
danielk1977a37cdde2004-05-16 11:15:36 +00001228 /* FIX ME: The following is a temporary hack. */
1229 if( 0==sqlite3StrNICmp(zId, "classof", nId) ){
1230 assert( nExpr==1 );
drhfec19aa2004-05-19 20:41:03 +00001231 sqlite3VdbeAddOp(v, OP_Class, nExpr, 0);
danielk1977a37cdde2004-05-16 11:15:36 +00001232 }else{
1233 sqlite3VdbeOp3(v, OP_Function, nExpr, 0, (char*)pDef, P3_POINTER);
1234 }
drhcce7d172000-05-31 15:34:51 +00001235 break;
1236 }
drh19a775c2000-06-05 18:54:46 +00001237 case TK_SELECT: {
danielk19774adee202004-05-08 08:23:19 +00001238 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drh19a775c2000-06-05 18:54:46 +00001239 break;
1240 }
drhfef52082000-06-06 01:50:43 +00001241 case TK_IN: {
1242 int addr;
danielk1977e014a832004-05-17 10:48:57 +00001243 char const *affStr;
1244
1245 /* Figure out the affinity to use to create a key from the results
1246 ** of the expression. affinityStr stores a static string suitable for
1247 ** P3 of OP_MakeKey.
1248 */
1249 affStr = sqlite3AffinityString(comparisonAffinity(pExpr));
1250
danielk19774adee202004-05-08 08:23:19 +00001251 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
danielk1977e014a832004-05-17 10:48:57 +00001252
1253 /* Code the <expr> from "<expr> IN (...)". The temporary table
1254 ** pExpr->iTable contains the values that make up the (...) set.
1255 */
danielk19774adee202004-05-08 08:23:19 +00001256 sqlite3ExprCode(pParse, pExpr->pLeft);
1257 addr = sqlite3VdbeCurrentAddr(v);
danielk1977e014a832004-05-17 10:48:57 +00001258 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); /* addr + 0 */
danielk19774adee202004-05-08 08:23:19 +00001259 sqlite3VdbeAddOp(v, OP_Pop, 2, 0);
1260 sqlite3VdbeAddOp(v, OP_String, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +00001261 sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7);
1262 sqlite3VdbeOp3(v, OP_MakeKey, 1, 0, affStr, P3_STATIC); /* addr + 4 */
1263 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7);
1264 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); /* addr + 6 */
1265
drhfef52082000-06-06 01:50:43 +00001266 break;
1267 }
1268 case TK_BETWEEN: {
danielk19774adee202004-05-08 08:23:19 +00001269 sqlite3ExprCode(pParse, pExpr->pLeft);
1270 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
1271 sqlite3ExprCode(pParse, pExpr->pList->a[0].pExpr);
1272 sqlite3VdbeAddOp(v, OP_Ge, 0, 0);
1273 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
1274 sqlite3ExprCode(pParse, pExpr->pList->a[1].pExpr);
1275 sqlite3VdbeAddOp(v, OP_Le, 0, 0);
1276 sqlite3VdbeAddOp(v, OP_And, 0, 0);
drhfef52082000-06-06 01:50:43 +00001277 break;
1278 }
drh51e9a442004-01-16 16:42:53 +00001279 case TK_UPLUS:
drha2e00042002-01-22 03:13:42 +00001280 case TK_AS: {
danielk19774adee202004-05-08 08:23:19 +00001281 sqlite3ExprCode(pParse, pExpr->pLeft);
drha2e00042002-01-22 03:13:42 +00001282 break;
1283 }
drh17a7f8d2002-03-24 13:13:27 +00001284 case TK_CASE: {
1285 int expr_end_label;
drhf5905aa2002-05-26 20:54:33 +00001286 int jumpInst;
1287 int addr;
1288 int nExpr;
drh17a7f8d2002-03-24 13:13:27 +00001289 int i;
1290
1291 assert(pExpr->pList);
1292 assert((pExpr->pList->nExpr % 2) == 0);
1293 assert(pExpr->pList->nExpr > 0);
drhf5905aa2002-05-26 20:54:33 +00001294 nExpr = pExpr->pList->nExpr;
danielk19774adee202004-05-08 08:23:19 +00001295 expr_end_label = sqlite3VdbeMakeLabel(v);
drh17a7f8d2002-03-24 13:13:27 +00001296 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001297 sqlite3ExprCode(pParse, pExpr->pLeft);
drh17a7f8d2002-03-24 13:13:27 +00001298 }
drhf5905aa2002-05-26 20:54:33 +00001299 for(i=0; i<nExpr; i=i+2){
danielk19774adee202004-05-08 08:23:19 +00001300 sqlite3ExprCode(pParse, pExpr->pList->a[i].pExpr);
drh17a7f8d2002-03-24 13:13:27 +00001301 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001302 sqlite3VdbeAddOp(v, OP_Dup, 1, 1);
1303 jumpInst = sqlite3VdbeAddOp(v, OP_Ne, 1, 0);
1304 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001305 }else{
danielk19774adee202004-05-08 08:23:19 +00001306 jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001307 }
danielk19774adee202004-05-08 08:23:19 +00001308 sqlite3ExprCode(pParse, pExpr->pList->a[i+1].pExpr);
1309 sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label);
1310 addr = sqlite3VdbeCurrentAddr(v);
1311 sqlite3VdbeChangeP2(v, jumpInst, addr);
drh17a7f8d2002-03-24 13:13:27 +00001312 }
drhf570f012002-05-31 15:51:25 +00001313 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001314 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhf570f012002-05-31 15:51:25 +00001315 }
drh17a7f8d2002-03-24 13:13:27 +00001316 if( pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001317 sqlite3ExprCode(pParse, pExpr->pRight);
drh17a7f8d2002-03-24 13:13:27 +00001318 }else{
danielk19774adee202004-05-08 08:23:19 +00001319 sqlite3VdbeAddOp(v, OP_String, 0, 0);
drh17a7f8d2002-03-24 13:13:27 +00001320 }
danielk19774adee202004-05-08 08:23:19 +00001321 sqlite3VdbeResolveLabel(v, expr_end_label);
danielk19776f349032002-06-11 02:25:40 +00001322 break;
1323 }
1324 case TK_RAISE: {
1325 if( !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +00001326 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00001327 "RAISE() may only be used within a trigger-program");
danielk19776f349032002-06-11 02:25:40 +00001328 pParse->nErr++;
1329 return;
1330 }
1331 if( pExpr->iColumn == OE_Rollback ||
1332 pExpr->iColumn == OE_Abort ||
1333 pExpr->iColumn == OE_Fail ){
danielk19774adee202004-05-08 08:23:19 +00001334 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn,
drh701a0ae2004-02-22 20:05:00 +00001335 pExpr->token.z, pExpr->token.n);
danielk19774adee202004-05-08 08:23:19 +00001336 sqlite3VdbeDequoteP3(v, -1);
danielk19776f349032002-06-11 02:25:40 +00001337 } else {
1338 assert( pExpr->iColumn == OE_Ignore );
danielk19774adee202004-05-08 08:23:19 +00001339 sqlite3VdbeOp3(v, OP_Goto, 0, pParse->trigStack->ignoreJump,
drh701a0ae2004-02-22 20:05:00 +00001340 "(IGNORE jump)", 0);
danielk19776f349032002-06-11 02:25:40 +00001341 }
drh17a7f8d2002-03-24 13:13:27 +00001342 }
1343 break;
drhcce7d172000-05-31 15:34:51 +00001344 }
drhcce7d172000-05-31 15:34:51 +00001345}
1346
1347/*
drh268380c2004-02-25 13:47:31 +00001348** Generate code that pushes the value of every element of the given
1349** expression list onto the stack. If the includeTypes flag is true,
1350** then also push a string that is the datatype of each element onto
1351** the stack after the value.
1352**
1353** Return the number of elements pushed onto the stack.
1354*/
danielk19774adee202004-05-08 08:23:19 +00001355int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00001356 Parse *pParse, /* Parsing context */
1357 ExprList *pList, /* The expression list to be coded */
1358 int includeTypes /* TRUE to put datatypes on the stack too */
1359){
1360 struct ExprList_item *pItem;
1361 int i, n;
1362 Vdbe *v;
1363 if( pList==0 ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001364 v = sqlite3GetVdbe(pParse);
drh268380c2004-02-25 13:47:31 +00001365 n = pList->nExpr;
1366 for(pItem=pList->a, i=0; i<n; i++, pItem++){
danielk19774adee202004-05-08 08:23:19 +00001367 sqlite3ExprCode(pParse, pItem->pExpr);
drh268380c2004-02-25 13:47:31 +00001368 if( includeTypes ){
drhd3d39e92004-05-20 22:16:29 +00001369 /** DEPRECATED. This will go away with the new function interface **/
1370 sqlite3VdbeOp3(v, OP_String, 0, 0, "numeric", P3_STATIC);
drh268380c2004-02-25 13:47:31 +00001371 }
1372 }
1373 return includeTypes ? n*2 : n;
1374}
1375
1376/*
drhcce7d172000-05-31 15:34:51 +00001377** Generate code for a boolean expression such that a jump is made
1378** to the label "dest" if the expression is true but execution
1379** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00001380**
1381** If the expression evaluates to NULL (neither true nor false), then
1382** take the jump if the jumpIfNull flag is true.
drhcce7d172000-05-31 15:34:51 +00001383*/
danielk19774adee202004-05-08 08:23:19 +00001384void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001385 Vdbe *v = pParse->pVdbe;
1386 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001387 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001388 switch( pExpr->op ){
1389 case TK_LT: op = OP_Lt; break;
1390 case TK_LE: op = OP_Le; break;
1391 case TK_GT: op = OP_Gt; break;
1392 case TK_GE: op = OP_Ge; break;
1393 case TK_NE: op = OP_Ne; break;
1394 case TK_EQ: op = OP_Eq; break;
drhcce7d172000-05-31 15:34:51 +00001395 case TK_ISNULL: op = OP_IsNull; break;
1396 case TK_NOTNULL: op = OP_NotNull; break;
1397 default: break;
1398 }
1399 switch( pExpr->op ){
1400 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001401 int d2 = sqlite3VdbeMakeLabel(v);
1402 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
1403 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
1404 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001405 break;
1406 }
1407 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001408 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
1409 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001410 break;
1411 }
1412 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001413 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001414 break;
1415 }
1416 case TK_LT:
1417 case TK_LE:
1418 case TK_GT:
1419 case TK_GE:
1420 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00001421 case TK_EQ: {
danielk1977a37cdde2004-05-16 11:15:36 +00001422 int p1 = binaryCompareP1(pExpr->pLeft, pExpr->pRight, jumpIfNull);
danielk19774adee202004-05-08 08:23:19 +00001423 sqlite3ExprCode(pParse, pExpr->pLeft);
1424 sqlite3ExprCode(pParse, pExpr->pRight);
danielk1977a37cdde2004-05-16 11:15:36 +00001425 sqlite3VdbeAddOp(v, op, p1, dest);
drhcce7d172000-05-31 15:34:51 +00001426 break;
1427 }
1428 case TK_ISNULL:
1429 case TK_NOTNULL: {
danielk19774adee202004-05-08 08:23:19 +00001430 sqlite3ExprCode(pParse, pExpr->pLeft);
1431 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001432 break;
1433 }
drhfef52082000-06-06 01:50:43 +00001434 case TK_BETWEEN: {
drhf5905aa2002-05-26 20:54:33 +00001435 int addr;
danielk19774adee202004-05-08 08:23:19 +00001436 sqlite3ExprCode(pParse, pExpr->pLeft);
1437 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
1438 sqlite3ExprCode(pParse, pExpr->pList->a[0].pExpr);
1439 addr = sqlite3VdbeAddOp(v, OP_Lt, !jumpIfNull, 0);
1440 sqlite3ExprCode(pParse, pExpr->pList->a[1].pExpr);
1441 sqlite3VdbeAddOp(v, OP_Le, jumpIfNull, dest);
1442 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
1443 sqlite3VdbeChangeP2(v, addr, sqlite3VdbeCurrentAddr(v));
1444 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00001445 break;
1446 }
drhcce7d172000-05-31 15:34:51 +00001447 default: {
danielk19774adee202004-05-08 08:23:19 +00001448 sqlite3ExprCode(pParse, pExpr);
1449 sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001450 break;
1451 }
1452 }
1453}
1454
1455/*
drh66b89c82000-11-28 20:47:17 +00001456** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00001457** to the label "dest" if the expression is false but execution
1458** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00001459**
1460** If the expression evaluates to NULL (neither true nor false) then
1461** jump if jumpIfNull is true or fall through if jumpIfNull is false.
drhcce7d172000-05-31 15:34:51 +00001462*/
danielk19774adee202004-05-08 08:23:19 +00001463void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001464 Vdbe *v = pParse->pVdbe;
1465 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001466 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001467 switch( pExpr->op ){
1468 case TK_LT: op = OP_Ge; break;
1469 case TK_LE: op = OP_Gt; break;
1470 case TK_GT: op = OP_Le; break;
1471 case TK_GE: op = OP_Lt; break;
1472 case TK_NE: op = OP_Eq; break;
1473 case TK_EQ: op = OP_Ne; break;
drhcce7d172000-05-31 15:34:51 +00001474 case TK_ISNULL: op = OP_NotNull; break;
1475 case TK_NOTNULL: op = OP_IsNull; break;
1476 default: break;
1477 }
1478 switch( pExpr->op ){
1479 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001480 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
1481 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001482 break;
1483 }
1484 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001485 int d2 = sqlite3VdbeMakeLabel(v);
1486 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
1487 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
1488 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001489 break;
1490 }
1491 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001492 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001493 break;
1494 }
1495 case TK_LT:
1496 case TK_LE:
1497 case TK_GT:
1498 case TK_GE:
1499 case TK_NE:
1500 case TK_EQ: {
danielk1977a37cdde2004-05-16 11:15:36 +00001501 int p1 = binaryCompareP1(pExpr->pLeft, pExpr->pRight, jumpIfNull);
danielk19774adee202004-05-08 08:23:19 +00001502 sqlite3ExprCode(pParse, pExpr->pLeft);
1503 sqlite3ExprCode(pParse, pExpr->pRight);
danielk1977a37cdde2004-05-16 11:15:36 +00001504 sqlite3VdbeAddOp(v, op, p1, dest);
drhcce7d172000-05-31 15:34:51 +00001505 break;
1506 }
drhcce7d172000-05-31 15:34:51 +00001507 case TK_ISNULL:
1508 case TK_NOTNULL: {
danielk19774adee202004-05-08 08:23:19 +00001509 sqlite3ExprCode(pParse, pExpr->pLeft);
1510 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001511 break;
1512 }
danielk1977e014a832004-05-17 10:48:57 +00001513#if 0
drhfef52082000-06-06 01:50:43 +00001514 case TK_IN: {
drhf5905aa2002-05-26 20:54:33 +00001515 int addr;
danielk19774adee202004-05-08 08:23:19 +00001516 sqlite3ExprCode(pParse, pExpr->pLeft);
1517 addr = sqlite3VdbeCurrentAddr(v);
1518 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+3);
1519 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
1520 sqlite3VdbeAddOp(v, OP_Goto, 0, jumpIfNull ? dest : addr+4);
drhfef52082000-06-06 01:50:43 +00001521 if( pExpr->pSelect ){
danielk19774adee202004-05-08 08:23:19 +00001522 sqlite3VdbeAddOp(v, OP_NotFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001523 }else{
danielk19774adee202004-05-08 08:23:19 +00001524 sqlite3VdbeAddOp(v, OP_SetNotFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001525 }
1526 break;
1527 }
danielk1977e014a832004-05-17 10:48:57 +00001528#endif
drhfef52082000-06-06 01:50:43 +00001529 case TK_BETWEEN: {
1530 int addr;
danielk19774adee202004-05-08 08:23:19 +00001531 sqlite3ExprCode(pParse, pExpr->pLeft);
1532 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
1533 sqlite3ExprCode(pParse, pExpr->pList->a[0].pExpr);
1534 addr = sqlite3VdbeCurrentAddr(v);
1535 sqlite3VdbeAddOp(v, OP_Ge, !jumpIfNull, addr+3);
1536 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
1537 sqlite3VdbeAddOp(v, OP_Goto, 0, dest);
1538 sqlite3ExprCode(pParse, pExpr->pList->a[1].pExpr);
1539 sqlite3VdbeAddOp(v, OP_Gt, jumpIfNull, dest);
drhfef52082000-06-06 01:50:43 +00001540 break;
1541 }
drhcce7d172000-05-31 15:34:51 +00001542 default: {
danielk19774adee202004-05-08 08:23:19 +00001543 sqlite3ExprCode(pParse, pExpr);
1544 sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001545 break;
1546 }
1547 }
1548}
drh22827922000-06-06 17:27:05 +00001549
1550/*
1551** Do a deep comparison of two expression trees. Return TRUE (non-zero)
1552** if they are identical and return FALSE if they differ in any way.
1553*/
danielk19774adee202004-05-08 08:23:19 +00001554int sqlite3ExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00001555 int i;
1556 if( pA==0 ){
1557 return pB==0;
1558 }else if( pB==0 ){
1559 return 0;
1560 }
1561 if( pA->op!=pB->op ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001562 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
1563 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00001564 if( pA->pList ){
1565 if( pB->pList==0 ) return 0;
1566 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
1567 for(i=0; i<pA->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00001568 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00001569 return 0;
1570 }
1571 }
1572 }else if( pB->pList ){
1573 return 0;
1574 }
1575 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00001576 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drh22827922000-06-06 17:27:05 +00001577 if( pA->token.z ){
1578 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00001579 if( pB->token.n!=pA->token.n ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001580 if( sqlite3StrNICmp(pA->token.z, pB->token.z, pB->token.n)!=0 ) return 0;
drh22827922000-06-06 17:27:05 +00001581 }
1582 return 1;
1583}
1584
1585/*
1586** Add a new element to the pParse->aAgg[] array and return its index.
1587*/
1588static int appendAggInfo(Parse *pParse){
1589 if( (pParse->nAgg & 0x7)==0 ){
1590 int amt = pParse->nAgg + 8;
drh6d4abfb2001-10-22 02:58:08 +00001591 AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0]));
1592 if( aAgg==0 ){
drh22827922000-06-06 17:27:05 +00001593 return -1;
1594 }
drh6d4abfb2001-10-22 02:58:08 +00001595 pParse->aAgg = aAgg;
drh22827922000-06-06 17:27:05 +00001596 }
1597 memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0]));
1598 return pParse->nAgg++;
1599}
1600
1601/*
1602** Analyze the given expression looking for aggregate functions and
1603** for variables that need to be added to the pParse->aAgg[] array.
1604** Make additional entries to the pParse->aAgg[] array as necessary.
1605**
1606** This routine should only be called after the expression has been
danielk19774adee202004-05-08 08:23:19 +00001607** analyzed by sqlite3ExprResolveIds() and sqlite3ExprCheck().
drh22827922000-06-06 17:27:05 +00001608**
1609** If errors are seen, leave an error message in zErrMsg and return
1610** the number of errors.
1611*/
danielk19774adee202004-05-08 08:23:19 +00001612int sqlite3ExprAnalyzeAggregates(Parse *pParse, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00001613 int i;
1614 AggExpr *aAgg;
1615 int nErr = 0;
1616
1617 if( pExpr==0 ) return 0;
1618 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00001619 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +00001620 aAgg = pParse->aAgg;
1621 for(i=0; i<pParse->nAgg; i++){
1622 if( aAgg[i].isAgg ) continue;
1623 if( aAgg[i].pExpr->iTable==pExpr->iTable
drh967e8b72000-06-21 13:59:10 +00001624 && aAgg[i].pExpr->iColumn==pExpr->iColumn ){
drh22827922000-06-06 17:27:05 +00001625 break;
1626 }
1627 }
1628 if( i>=pParse->nAgg ){
1629 i = appendAggInfo(pParse);
1630 if( i<0 ) return 1;
1631 pParse->aAgg[i].isAgg = 0;
1632 pParse->aAgg[i].pExpr = pExpr;
1633 }
drhaaf88722000-06-08 11:25:00 +00001634 pExpr->iAgg = i;
drh22827922000-06-06 17:27:05 +00001635 break;
1636 }
1637 case TK_AGG_FUNCTION: {
drh22827922000-06-06 17:27:05 +00001638 aAgg = pParse->aAgg;
1639 for(i=0; i<pParse->nAgg; i++){
1640 if( !aAgg[i].isAgg ) continue;
danielk19774adee202004-05-08 08:23:19 +00001641 if( sqlite3ExprCompare(aAgg[i].pExpr, pExpr) ){
drh22827922000-06-06 17:27:05 +00001642 break;
1643 }
1644 }
1645 if( i>=pParse->nAgg ){
1646 i = appendAggInfo(pParse);
1647 if( i<0 ) return 1;
1648 pParse->aAgg[i].isAgg = 1;
1649 pParse->aAgg[i].pExpr = pExpr;
danielk19774adee202004-05-08 08:23:19 +00001650 pParse->aAgg[i].pFunc = sqlite3FindFunction(pParse->db,
drh6977fea2002-10-22 23:38:04 +00001651 pExpr->token.z, pExpr->token.n,
drhf55f25f2002-02-28 01:46:11 +00001652 pExpr->pList ? pExpr->pList->nExpr : 0, 0);
drh22827922000-06-06 17:27:05 +00001653 }
1654 pExpr->iAgg = i;
1655 break;
1656 }
1657 default: {
1658 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001659 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pLeft);
drh22827922000-06-06 17:27:05 +00001660 }
1661 if( nErr==0 && pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001662 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pRight);
drh22827922000-06-06 17:27:05 +00001663 }
1664 if( nErr==0 && pExpr->pList ){
1665 int n = pExpr->pList->nExpr;
1666 int i;
1667 for(i=0; nErr==0 && i<n; i++){
danielk19774adee202004-05-08 08:23:19 +00001668 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pList->a[i].pExpr);
drh22827922000-06-06 17:27:05 +00001669 }
1670 }
1671 break;
1672 }
1673 }
1674 return nErr;
1675}
drh8e0a2f92002-02-23 23:45:45 +00001676
1677/*
1678** Locate a user function given a name and a number of arguments.
drh0bce8352002-02-28 00:41:10 +00001679** Return a pointer to the FuncDef structure that defines that
drh8e0a2f92002-02-23 23:45:45 +00001680** function, or return NULL if the function does not exist.
1681**
drh0bce8352002-02-28 00:41:10 +00001682** If the createFlag argument is true, then a new (blank) FuncDef
drh8e0a2f92002-02-23 23:45:45 +00001683** structure is created and liked into the "db" structure if a
1684** no matching function previously existed. When createFlag is true
1685** and the nArg parameter is -1, then only a function that accepts
1686** any number of arguments will be returned.
1687**
1688** If createFlag is false and nArg is -1, then the first valid
1689** function found is returned. A function is valid if either xFunc
1690** or xStep is non-zero.
1691*/
danielk19774adee202004-05-08 08:23:19 +00001692FuncDef *sqlite3FindFunction(
drh8e0a2f92002-02-23 23:45:45 +00001693 sqlite *db, /* An open database */
1694 const char *zName, /* Name of the function. Not null-terminated */
1695 int nName, /* Number of characters in the name */
1696 int nArg, /* Number of arguments. -1 means any number */
1697 int createFlag /* Create new entry if true and does not otherwise exist */
1698){
drh0bce8352002-02-28 00:41:10 +00001699 FuncDef *pFirst, *p, *pMaybe;
danielk19774adee202004-05-08 08:23:19 +00001700 pFirst = p = (FuncDef*)sqlite3HashFind(&db->aFunc, zName, nName);
drh1350b032002-02-27 19:00:20 +00001701 if( p && !createFlag && nArg<0 ){
drh8e0a2f92002-02-23 23:45:45 +00001702 while( p && p->xFunc==0 && p->xStep==0 ){ p = p->pNext; }
1703 return p;
1704 }
1705 pMaybe = 0;
1706 while( p && p->nArg!=nArg ){
1707 if( p->nArg<0 && !createFlag && (p->xFunc || p->xStep) ) pMaybe = p;
1708 p = p->pNext;
1709 }
1710 if( p && !createFlag && p->xFunc==0 && p->xStep==0 ){
1711 return 0;
1712 }
1713 if( p==0 && pMaybe ){
1714 assert( createFlag==0 );
1715 return pMaybe;
1716 }
drh89425d52002-02-28 03:04:48 +00001717 if( p==0 && createFlag && (p = sqliteMalloc(sizeof(*p)))!=0 ){
drh8e0a2f92002-02-23 23:45:45 +00001718 p->nArg = nArg;
1719 p->pNext = pFirst;
drhc9b84a12002-06-20 11:36:48 +00001720 p->dataType = pFirst ? pFirst->dataType : SQLITE_NUMERIC;
danielk19774adee202004-05-08 08:23:19 +00001721 sqlite3HashInsert(&db->aFunc, zName, nName, (void*)p);
drh8e0a2f92002-02-23 23:45:45 +00001722 }
1723 return p;
1724}