blob: 1987169e0b9fc279cb6bdc964c07a2112546c62b [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**
drh53db1452004-05-20 13:54:53 +000015** $Id: expr.c,v 1.124 2004/05/20 13:54:54 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;
591
592 /* FIX ME: Expr::dataType will be removed... */
593 pExpr->dataType =
594 (pCol->affinity==SQLITE_AFF_TEXT?SQLITE_SO_TEXT:SQLITE_SO_NUM);
drh8141f612004-01-25 22:44:58 +0000595 break;
596 }
597 }
598 }
599
600 /* If we have not already resolved the name, then maybe
601 ** it is a new.* or old.* trigger argument reference
602 */
603 if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
604 TriggerStack *pTriggerStack = pParse->trigStack;
605 Table *pTab = 0;
danielk19774adee202004-05-08 08:23:19 +0000606 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){
drh8141f612004-01-25 22:44:58 +0000607 pExpr->iTable = pTriggerStack->newIdx;
608 assert( pTriggerStack->pTab );
609 pTab = pTriggerStack->pTab;
danielk19774adee202004-05-08 08:23:19 +0000610 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab) == 0 ){
drh8141f612004-01-25 22:44:58 +0000611 pExpr->iTable = pTriggerStack->oldIdx;
612 assert( pTriggerStack->pTab );
613 pTab = pTriggerStack->pTab;
614 }
615
616 if( pTab ){
617 int j;
618 Column *pCol = pTab->aCol;
619
620 pExpr->iDb = pTab->iDb;
621 cntTab++;
622 for(j=0; j < pTab->nCol; j++, pCol++) {
danielk19774adee202004-05-08 08:23:19 +0000623 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
drh8141f612004-01-25 22:44:58 +0000624 cnt++;
625 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
danielk1977a37cdde2004-05-16 11:15:36 +0000626 pExpr->affinity = pTab->aCol[j].affinity;
627 /* FIX ME: Expr::dataType will be removed... */
628 pExpr->dataType =
629 (pCol->affinity==SQLITE_AFF_TEXT?SQLITE_SO_TEXT:SQLITE_SO_NUM);
drh8141f612004-01-25 22:44:58 +0000630 break;
631 }
632 }
633 }
634 }
635
636 /*
637 ** Perhaps the name is a reference to the ROWID
638 */
danielk19774adee202004-05-08 08:23:19 +0000639 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
drh8141f612004-01-25 22:44:58 +0000640 cnt = 1;
641 pExpr->iColumn = -1;
642 pExpr->dataType = SQLITE_SO_NUM;
danielk1977a37cdde2004-05-16 11:15:36 +0000643 pExpr->affinity = SQLITE_AFF_INTEGER;
drh8141f612004-01-25 22:44:58 +0000644 }
645
646 /*
647 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
648 ** might refer to an result-set alias. This happens, for example, when
649 ** we are resolving names in the WHERE clause of the following command:
650 **
651 ** SELECT a+b AS x FROM table WHERE x<10;
652 **
653 ** In cases like this, replace pExpr with a copy of the expression that
654 ** forms the result set entry ("a+b" in the example) and return immediately.
655 ** Note that the expression in the result set should have already been
656 ** resolved by the time the WHERE clause is resolved.
657 */
658 if( cnt==0 && pEList!=0 ){
659 for(j=0; j<pEList->nExpr; j++){
660 char *zAs = pEList->a[j].zName;
danielk19774adee202004-05-08 08:23:19 +0000661 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
drh8141f612004-01-25 22:44:58 +0000662 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
663 pExpr->op = TK_AS;
664 pExpr->iColumn = j;
danielk19774adee202004-05-08 08:23:19 +0000665 pExpr->pLeft = sqlite3ExprDup(pEList->a[j].pExpr);
drh8141f612004-01-25 22:44:58 +0000666 sqliteFree(zCol);
667 assert( zTab==0 && zDb==0 );
668 return 0;
669 }
670 }
671 }
672
673 /*
674 ** If X and Y are NULL (in other words if only the column name Z is
675 ** supplied) and the value of Z is enclosed in double-quotes, then
676 ** Z is a string literal if it doesn't match any column names. In that
677 ** case, we need to return right away and not make any changes to
678 ** pExpr.
679 */
680 if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
681 sqliteFree(zCol);
682 return 0;
683 }
684
685 /*
686 ** cnt==0 means there was not match. cnt>1 means there were two or
687 ** more matches. Either way, we have an error.
688 */
689 if( cnt!=1 ){
690 char *z = 0;
691 char *zErr;
692 zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s";
693 if( zDb ){
danielk19774adee202004-05-08 08:23:19 +0000694 sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, 0);
drh8141f612004-01-25 22:44:58 +0000695 }else if( zTab ){
danielk19774adee202004-05-08 08:23:19 +0000696 sqlite3SetString(&z, zTab, ".", zCol, 0);
drh8141f612004-01-25 22:44:58 +0000697 }else{
698 z = sqliteStrDup(zCol);
699 }
danielk19774adee202004-05-08 08:23:19 +0000700 sqlite3ErrorMsg(pParse, zErr, z);
drh8141f612004-01-25 22:44:58 +0000701 sqliteFree(z);
702 }
703
704 /* Clean up and return
705 */
706 sqliteFree(zDb);
707 sqliteFree(zTab);
708 sqliteFree(zCol);
danielk19774adee202004-05-08 08:23:19 +0000709 sqlite3ExprDelete(pExpr->pLeft);
drh8141f612004-01-25 22:44:58 +0000710 pExpr->pLeft = 0;
danielk19774adee202004-05-08 08:23:19 +0000711 sqlite3ExprDelete(pExpr->pRight);
drh8141f612004-01-25 22:44:58 +0000712 pExpr->pRight = 0;
713 pExpr->op = TK_COLUMN;
danielk19774adee202004-05-08 08:23:19 +0000714 sqlite3AuthRead(pParse, pExpr, pSrcList);
drh8141f612004-01-25 22:44:58 +0000715 return cnt!=1;
716}
717
718/*
drhcce7d172000-05-31 15:34:51 +0000719** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +0000720** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +0000721** index to the table in the table list and a column offset. The
722** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
723** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +0000724** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +0000725** VDBE cursor number for a cursor that is pointing into the referenced
726** table. The Expr.iColumn value is changed to the index of the column
727** of the referenced table. The Expr.iColumn value for the special
728** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
729** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +0000730**
drhfef52082000-06-06 01:50:43 +0000731** We also check for instances of the IN operator. IN comes in two
732** forms:
733**
734** expr IN (exprlist)
735** and
736** expr IN (SELECT ...)
737**
738** The first form is handled by creating a set holding the list
739** of allowed values. The second form causes the SELECT to generate
740** a temporary table.
741**
742** This routine also looks for scalar SELECTs that are part of an expression.
drh19a775c2000-06-05 18:54:46 +0000743** If it finds any, it generates code to write the value of that select
744** into a memory cell.
drhcce7d172000-05-31 15:34:51 +0000745**
drh967e8b72000-06-21 13:59:10 +0000746** Unknown columns or tables provoke an error. The function returns
drhcce7d172000-05-31 15:34:51 +0000747** the number of errors seen and leaves an error message on pParse->zErrMsg.
748*/
danielk19774adee202004-05-08 08:23:19 +0000749int sqlite3ExprResolveIds(
drha2e00042002-01-22 03:13:42 +0000750 Parse *pParse, /* The parser context */
drh8141f612004-01-25 22:44:58 +0000751 SrcList *pSrcList, /* List of tables used to resolve column names */
drha2e00042002-01-22 03:13:42 +0000752 ExprList *pEList, /* List of expressions used to resolve "AS" */
753 Expr *pExpr /* The expression to be analyzed. */
754){
drh6a3ea0e2003-05-02 14:32:12 +0000755 int i;
756
drh8141f612004-01-25 22:44:58 +0000757 if( pExpr==0 || pSrcList==0 ) return 0;
758 for(i=0; i<pSrcList->nSrc; i++){
759 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab );
drh6a3ea0e2003-05-02 14:32:12 +0000760 }
drhcce7d172000-05-31 15:34:51 +0000761 switch( pExpr->op ){
drh23989372002-05-21 13:43:04 +0000762 /* Double-quoted strings (ex: "abc") are used as identifiers if
763 ** possible. Otherwise they remain as strings. Single-quoted
764 ** strings (ex: 'abc') are always string literals.
765 */
766 case TK_STRING: {
767 if( pExpr->token.z[0]=='\'' ) break;
768 /* Fall thru into the TK_ID case if this is a double-quoted string */
769 }
drh8141f612004-01-25 22:44:58 +0000770 /* A lone identifier is the name of a columnd.
drha2e00042002-01-22 03:13:42 +0000771 */
drhcce7d172000-05-31 15:34:51 +0000772 case TK_ID: {
drh8141f612004-01-25 22:44:58 +0000773 if( lookupName(pParse, 0, 0, &pExpr->token, pSrcList, pEList, pExpr) ){
drhcce7d172000-05-31 15:34:51 +0000774 return 1;
drhed6c8672003-01-12 18:02:16 +0000775 }
drhcce7d172000-05-31 15:34:51 +0000776 break;
777 }
778
drhd24cc422003-03-27 12:51:24 +0000779 /* A table name and column name: ID.ID
780 ** Or a database, table and column: ID.ID.ID
781 */
drhcce7d172000-05-31 15:34:51 +0000782 case TK_DOT: {
drh8141f612004-01-25 22:44:58 +0000783 Token *pColumn;
784 Token *pTable;
785 Token *pDb;
786 Expr *pRight;
drhcce7d172000-05-31 15:34:51 +0000787
drhcce7d172000-05-31 15:34:51 +0000788 pRight = pExpr->pRight;
drhd24cc422003-03-27 12:51:24 +0000789 if( pRight->op==TK_ID ){
drh8141f612004-01-25 22:44:58 +0000790 pDb = 0;
791 pTable = &pExpr->pLeft->token;
792 pColumn = &pRight->token;
drhd24cc422003-03-27 12:51:24 +0000793 }else{
drh8141f612004-01-25 22:44:58 +0000794 assert( pRight->op==TK_DOT );
795 pDb = &pExpr->pLeft->token;
796 pTable = &pRight->pLeft->token;
797 pColumn = &pRight->pRight->token;
drhd24cc422003-03-27 12:51:24 +0000798 }
drh8141f612004-01-25 22:44:58 +0000799 if( lookupName(pParse, pDb, pTable, pColumn, pSrcList, 0, pExpr) ){
drhdaffd0e2001-04-11 14:28:42 +0000800 return 1;
801 }
drhcce7d172000-05-31 15:34:51 +0000802 break;
803 }
804
drhfef52082000-06-06 01:50:43 +0000805 case TK_IN: {
danielk1977e014a832004-05-17 10:48:57 +0000806 char affinity;
danielk19774adee202004-05-08 08:23:19 +0000807 Vdbe *v = sqlite3GetVdbe(pParse);
drhfef52082000-06-06 01:50:43 +0000808 if( v==0 ) return 1;
danielk19774adee202004-05-08 08:23:19 +0000809 if( sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pLeft) ){
drhcfab11b2000-06-06 03:31:22 +0000810 return 1;
811 }
danielk1977bf3b7212004-05-18 10:06:24 +0000812 affinity = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +0000813
814 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
815 ** expression it is handled the same way. A temporary table is
816 ** filled with single-field index keys representing the results
817 ** from the SELECT or the <exprlist>.
818 **
819 ** If the 'x' expression is a column value, or the SELECT...
820 ** statement returns a column value, then the affinity of that
821 ** column is used to build the index keys. If both 'x' and the
822 ** SELECT... statement are columns, then numeric affinity is used
823 ** if either column has NUMERIC or INTEGER affinity. If neither
824 ** 'x' nor the SELECT... statement are columns, then numeric affinity
825 ** is used.
826 */
827 pExpr->iTable = pParse->nTab++;
828 sqlite3VdbeAddOp(v, OP_OpenTemp, pExpr->iTable, 1);
829
drhfef52082000-06-06 01:50:43 +0000830 if( pExpr->pSelect ){
831 /* Case 1: expr IN (SELECT ...)
832 **
danielk1977e014a832004-05-17 10:48:57 +0000833 ** Generate code to write the results of the select into the temporary
834 ** table allocated and opened above.
drhfef52082000-06-06 01:50:43 +0000835 */
danielk1977e014a832004-05-17 10:48:57 +0000836 int iParm = pExpr->iTable + (((int)affinity)<<16);
837 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
danielk1977bf3b7212004-05-18 10:06:24 +0000838 sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0);
drhfef52082000-06-06 01:50:43 +0000839 }else if( pExpr->pList ){
840 /* Case 2: expr IN (exprlist)
841 **
danielk1977e014a832004-05-17 10:48:57 +0000842 ** For each expression, build an index key from the evaluation and
843 ** store it in the temporary table. If <expr> is a column, then use
844 ** that columns affinity when building index keys. If <expr> is not
845 ** a column, use numeric affinity.
drhfef52082000-06-06 01:50:43 +0000846 */
danielk1977e014a832004-05-17 10:48:57 +0000847 int i;
848 char const *affStr;
849 if( !affinity ){
850 affinity = SQLITE_AFF_NUMERIC;
851 }
852 affStr = sqlite3AffinityString(affinity);
853
854 /* Loop through each expression in <exprlist>. */
drhfef52082000-06-06 01:50:43 +0000855 for(i=0; i<pExpr->pList->nExpr; i++){
856 Expr *pE2 = pExpr->pList->a[i].pExpr;
danielk1977e014a832004-05-17 10:48:57 +0000857
858 /* Check that the expression is constant and valid. */
danielk19774adee202004-05-08 08:23:19 +0000859 if( !sqlite3ExprIsConstant(pE2) ){
860 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +0000861 "right-hand side of IN operator must be constant");
drhfef52082000-06-06 01:50:43 +0000862 return 1;
863 }
danielk19774adee202004-05-08 08:23:19 +0000864 if( sqlite3ExprCheck(pParse, pE2, 0, 0) ){
drh4794b982000-06-06 13:54:14 +0000865 return 1;
866 }
danielk1977e014a832004-05-17 10:48:57 +0000867
868 /* Evaluate the expression and insert it into the temp table */
869 sqlite3ExprCode(pParse, pE2);
870 sqlite3VdbeOp3(v, OP_MakeKey, 1, 0, affStr, P3_STATIC);
871 sqlite3VdbeAddOp(v, OP_String, 0, 0);
872 sqlite3VdbeAddOp(v, OP_PutStrKey, pExpr->iTable, 0);
drhfef52082000-06-06 01:50:43 +0000873 }
874 }
drhcfab11b2000-06-06 03:31:22 +0000875 break;
drhfef52082000-06-06 01:50:43 +0000876 }
877
drh19a775c2000-06-05 18:54:46 +0000878 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +0000879 /* This has to be a scalar SELECT. Generate code to put the
880 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +0000881 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +0000882 */
drh967e8b72000-06-21 13:59:10 +0000883 pExpr->iColumn = pParse->nMem++;
danielk1977bf3b7212004-05-18 10:06:24 +0000884 if(sqlite3Select(pParse, pExpr->pSelect, SRT_Mem,pExpr->iColumn,0,0,0,0)){
drh19a775c2000-06-05 18:54:46 +0000885 return 1;
886 }
887 break;
888 }
889
drhcce7d172000-05-31 15:34:51 +0000890 /* For all else, just recursively walk the tree */
891 default: {
drh4794b982000-06-06 13:54:14 +0000892 if( pExpr->pLeft
danielk19774adee202004-05-08 08:23:19 +0000893 && sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pLeft) ){
drhcce7d172000-05-31 15:34:51 +0000894 return 1;
895 }
896 if( pExpr->pRight
danielk19774adee202004-05-08 08:23:19 +0000897 && sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pRight) ){
drhcce7d172000-05-31 15:34:51 +0000898 return 1;
899 }
900 if( pExpr->pList ){
901 int i;
902 ExprList *pList = pExpr->pList;
903 for(i=0; i<pList->nExpr; i++){
drh832508b2002-03-02 17:04:07 +0000904 Expr *pArg = pList->a[i].pExpr;
danielk19774adee202004-05-08 08:23:19 +0000905 if( sqlite3ExprResolveIds(pParse, pSrcList, pEList, pArg) ){
drhcce7d172000-05-31 15:34:51 +0000906 return 1;
907 }
908 }
909 }
910 }
911 }
912 return 0;
913}
914
drhcce7d172000-05-31 15:34:51 +0000915/*
drh4b59ab52002-08-24 18:24:51 +0000916** pExpr is a node that defines a function of some kind. It might
917** be a syntactic function like "count(x)" or it might be a function
918** that implements an operator, like "a LIKE b".
919**
920** This routine makes *pzName point to the name of the function and
921** *pnName hold the number of characters in the function name.
922*/
923static void getFunctionName(Expr *pExpr, const char **pzName, int *pnName){
924 switch( pExpr->op ){
925 case TK_FUNCTION: {
926 *pzName = pExpr->token.z;
drh6977fea2002-10-22 23:38:04 +0000927 *pnName = pExpr->token.n;
drh4b59ab52002-08-24 18:24:51 +0000928 break;
929 }
930 case TK_LIKE: {
931 *pzName = "like";
932 *pnName = 4;
933 break;
934 }
935 case TK_GLOB: {
936 *pzName = "glob";
937 *pnName = 4;
938 break;
939 }
940 default: {
941 *pzName = "can't happen";
942 *pnName = 12;
943 break;
944 }
945 }
946}
947
948/*
drhcce7d172000-05-31 15:34:51 +0000949** Error check the functions in an expression. Make sure all
950** function names are recognized and all functions have the correct
951** number of arguments. Leave an error message in pParse->zErrMsg
952** if anything is amiss. Return the number of errors.
953**
954** if pIsAgg is not null and this expression is an aggregate function
955** (like count(*) or max(value)) then write a 1 into *pIsAgg.
956*/
danielk19774adee202004-05-08 08:23:19 +0000957int sqlite3ExprCheck(Parse *pParse, Expr *pExpr, int allowAgg, int *pIsAgg){
drhcce7d172000-05-31 15:34:51 +0000958 int nErr = 0;
959 if( pExpr==0 ) return 0;
drhcce7d172000-05-31 15:34:51 +0000960 switch( pExpr->op ){
drh4b59ab52002-08-24 18:24:51 +0000961 case TK_GLOB:
962 case TK_LIKE:
drhcce7d172000-05-31 15:34:51 +0000963 case TK_FUNCTION: {
drhc9b84a12002-06-20 11:36:48 +0000964 int n = pExpr->pList ? pExpr->pList->nExpr : 0; /* Number of arguments */
965 int no_such_func = 0; /* True if no such function exists */
drhc9b84a12002-06-20 11:36:48 +0000966 int wrong_num_args = 0; /* True if wrong number of arguments */
967 int is_agg = 0; /* True if is an aggregate function */
drhcce7d172000-05-31 15:34:51 +0000968 int i;
drh4b59ab52002-08-24 18:24:51 +0000969 int nId; /* Number of characters in function name */
970 const char *zId; /* The function name. */
drh0bce8352002-02-28 00:41:10 +0000971 FuncDef *pDef;
972
drh4b59ab52002-08-24 18:24:51 +0000973 getFunctionName(pExpr, &zId, &nId);
danielk19774adee202004-05-08 08:23:19 +0000974 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, 0);
drh0bce8352002-02-28 00:41:10 +0000975 if( pDef==0 ){
danielk19774adee202004-05-08 08:23:19 +0000976 pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, 0);
drh0bce8352002-02-28 00:41:10 +0000977 if( pDef==0 ){
drh268380c2004-02-25 13:47:31 +0000978 no_such_func = 1;
drh0bce8352002-02-28 00:41:10 +0000979 }else{
980 wrong_num_args = 1;
drhcce7d172000-05-31 15:34:51 +0000981 }
drh0bce8352002-02-28 00:41:10 +0000982 }else{
983 is_agg = pDef->xFunc==0;
drhcce7d172000-05-31 15:34:51 +0000984 }
drh8e0a2f92002-02-23 23:45:45 +0000985 if( is_agg && !allowAgg ){
danielk19774adee202004-05-08 08:23:19 +0000986 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId, zId);
drh8e0a2f92002-02-23 23:45:45 +0000987 nErr++;
988 is_agg = 0;
989 }else if( no_such_func ){
danielk19774adee202004-05-08 08:23:19 +0000990 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
drhcce7d172000-05-31 15:34:51 +0000991 nErr++;
drh8e0a2f92002-02-23 23:45:45 +0000992 }else if( wrong_num_args ){
danielk19774adee202004-05-08 08:23:19 +0000993 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
drhf7a9e1a2004-02-22 18:40:56 +0000994 nId, zId);
drh8e0a2f92002-02-23 23:45:45 +0000995 nErr++;
drhcce7d172000-05-31 15:34:51 +0000996 }
drhf7a9e1a2004-02-22 18:40:56 +0000997 if( is_agg ){
998 pExpr->op = TK_AGG_FUNCTION;
999 if( pIsAgg ) *pIsAgg = 1;
1000 }
drhcce7d172000-05-31 15:34:51 +00001001 for(i=0; nErr==0 && i<n; i++){
danielk19774adee202004-05-08 08:23:19 +00001002 nErr = sqlite3ExprCheck(pParse, pExpr->pList->a[i].pExpr,
drh4cfa7932000-06-08 15:10:46 +00001003 allowAgg && !is_agg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +00001004 }
drhc9b84a12002-06-20 11:36:48 +00001005 if( pDef==0 ){
drh268380c2004-02-25 13:47:31 +00001006 /* Already reported an error */
drhc9b84a12002-06-20 11:36:48 +00001007 }else if( pDef->dataType>=0 ){
1008 if( pDef->dataType<n ){
1009 pExpr->dataType =
danielk19774adee202004-05-08 08:23:19 +00001010 sqlite3ExprType(pExpr->pList->a[pDef->dataType].pExpr);
drhc9b84a12002-06-20 11:36:48 +00001011 }else{
1012 pExpr->dataType = SQLITE_SO_NUM;
1013 }
1014 }else if( pDef->dataType==SQLITE_ARGS ){
1015 pDef->dataType = SQLITE_SO_TEXT;
1016 for(i=0; i<n; i++){
danielk19774adee202004-05-08 08:23:19 +00001017 if( sqlite3ExprType(pExpr->pList->a[i].pExpr)==SQLITE_SO_NUM ){
drhc9b84a12002-06-20 11:36:48 +00001018 pExpr->dataType = SQLITE_SO_NUM;
1019 break;
1020 }
1021 }
1022 }else if( pDef->dataType==SQLITE_NUMERIC ){
1023 pExpr->dataType = SQLITE_SO_NUM;
1024 }else{
1025 pExpr->dataType = SQLITE_SO_TEXT;
1026 }
drhcce7d172000-05-31 15:34:51 +00001027 }
1028 default: {
1029 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001030 nErr = sqlite3ExprCheck(pParse, pExpr->pLeft, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +00001031 }
1032 if( nErr==0 && pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001033 nErr = sqlite3ExprCheck(pParse, pExpr->pRight, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +00001034 }
drhfef52082000-06-06 01:50:43 +00001035 if( nErr==0 && pExpr->pList ){
1036 int n = pExpr->pList->nExpr;
1037 int i;
1038 for(i=0; nErr==0 && i<n; i++){
drh22827922000-06-06 17:27:05 +00001039 Expr *pE2 = pExpr->pList->a[i].pExpr;
danielk19774adee202004-05-08 08:23:19 +00001040 nErr = sqlite3ExprCheck(pParse, pE2, allowAgg, pIsAgg);
drhfef52082000-06-06 01:50:43 +00001041 }
1042 }
drhcce7d172000-05-31 15:34:51 +00001043 break;
1044 }
1045 }
1046 return nErr;
1047}
1048
1049/*
drhc9b84a12002-06-20 11:36:48 +00001050** Return either SQLITE_SO_NUM or SQLITE_SO_TEXT to indicate whether the
1051** given expression should sort as numeric values or as text.
1052**
danielk19774adee202004-05-08 08:23:19 +00001053** The sqlite3ExprResolveIds() and sqlite3ExprCheck() routines must have
drhc9b84a12002-06-20 11:36:48 +00001054** both been called on the expression before it is passed to this routine.
1055*/
danielk19774adee202004-05-08 08:23:19 +00001056int sqlite3ExprType(Expr *p){
drhc9b84a12002-06-20 11:36:48 +00001057 if( p==0 ) return SQLITE_SO_NUM;
1058 while( p ) switch( p->op ){
1059 case TK_PLUS:
1060 case TK_MINUS:
1061 case TK_STAR:
1062 case TK_SLASH:
1063 case TK_AND:
1064 case TK_OR:
1065 case TK_ISNULL:
1066 case TK_NOTNULL:
1067 case TK_NOT:
1068 case TK_UMINUS:
drh4b59ab52002-08-24 18:24:51 +00001069 case TK_UPLUS:
drhc9b84a12002-06-20 11:36:48 +00001070 case TK_BITAND:
1071 case TK_BITOR:
1072 case TK_BITNOT:
1073 case TK_LSHIFT:
1074 case TK_RSHIFT:
1075 case TK_REM:
1076 case TK_INTEGER:
1077 case TK_FLOAT:
1078 case TK_IN:
1079 case TK_BETWEEN:
drh4b59ab52002-08-24 18:24:51 +00001080 case TK_GLOB:
1081 case TK_LIKE:
drhc9b84a12002-06-20 11:36:48 +00001082 return SQLITE_SO_NUM;
1083
1084 case TK_STRING:
1085 case TK_NULL:
1086 case TK_CONCAT:
drh50457892003-09-06 01:10:47 +00001087 case TK_VARIABLE:
drhc9b84a12002-06-20 11:36:48 +00001088 return SQLITE_SO_TEXT;
1089
1090 case TK_LT:
1091 case TK_LE:
1092 case TK_GT:
1093 case TK_GE:
1094 case TK_NE:
1095 case TK_EQ:
danielk19774adee202004-05-08 08:23:19 +00001096 if( sqlite3ExprType(p->pLeft)==SQLITE_SO_NUM ){
drhc9b84a12002-06-20 11:36:48 +00001097 return SQLITE_SO_NUM;
1098 }
1099 p = p->pRight;
1100 break;
1101
1102 case TK_AS:
1103 p = p->pLeft;
1104 break;
1105
1106 case TK_COLUMN:
1107 case TK_FUNCTION:
1108 case TK_AGG_FUNCTION:
1109 return p->dataType;
1110
1111 case TK_SELECT:
1112 assert( p->pSelect );
1113 assert( p->pSelect->pEList );
1114 assert( p->pSelect->pEList->nExpr>0 );
1115 p = p->pSelect->pEList->a[0].pExpr;
1116 break;
1117
drhb1363202002-06-26 02:45:03 +00001118 case TK_CASE: {
danielk19774adee202004-05-08 08:23:19 +00001119 if( p->pRight && sqlite3ExprType(p->pRight)==SQLITE_SO_NUM ){
drhb1363202002-06-26 02:45:03 +00001120 return SQLITE_SO_NUM;
1121 }
1122 if( p->pList ){
1123 int i;
1124 ExprList *pList = p->pList;
1125 for(i=1; i<pList->nExpr; i+=2){
danielk19774adee202004-05-08 08:23:19 +00001126 if( sqlite3ExprType(pList->a[i].pExpr)==SQLITE_SO_NUM ){
drhb1363202002-06-26 02:45:03 +00001127 return SQLITE_SO_NUM;
1128 }
1129 }
1130 }
1131 return SQLITE_SO_TEXT;
1132 }
1133
drhc9b84a12002-06-20 11:36:48 +00001134 default:
1135 assert( p->op==TK_ABORT ); /* Can't Happen */
1136 break;
1137 }
1138 return SQLITE_SO_NUM;
1139}
1140
1141/*
drhfec19aa2004-05-19 20:41:03 +00001142** Generate an instruction that will put the integer describe by
1143** text z[0..n-1] on the stack.
1144*/
1145static void codeInteger(Vdbe *v, const char *z, int n){
1146 int i;
1147 if( sqlite3GetInt32(z, &i) || (i=0, sqlite3FitsIn64Bits(z))!=0 ){
1148 sqlite3VdbeOp3(v, OP_Integer, i, 0, z, n);
1149 }else{
1150 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n);
1151 }
1152}
1153
1154/*
drhcce7d172000-05-31 15:34:51 +00001155** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +00001156** expression and leave the result on the top of stack.
drhcce7d172000-05-31 15:34:51 +00001157*/
danielk19774adee202004-05-08 08:23:19 +00001158void sqlite3ExprCode(Parse *pParse, Expr *pExpr){
drhcce7d172000-05-31 15:34:51 +00001159 Vdbe *v = pParse->pVdbe;
1160 int op;
drhdaffd0e2001-04-11 14:28:42 +00001161 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001162 switch( pExpr->op ){
1163 case TK_PLUS: op = OP_Add; break;
1164 case TK_MINUS: op = OP_Subtract; break;
1165 case TK_STAR: op = OP_Multiply; break;
1166 case TK_SLASH: op = OP_Divide; break;
1167 case TK_AND: op = OP_And; break;
1168 case TK_OR: op = OP_Or; break;
1169 case TK_LT: op = OP_Lt; break;
1170 case TK_LE: op = OP_Le; break;
1171 case TK_GT: op = OP_Gt; break;
1172 case TK_GE: op = OP_Ge; break;
1173 case TK_NE: op = OP_Ne; break;
1174 case TK_EQ: op = OP_Eq; break;
drhcce7d172000-05-31 15:34:51 +00001175 case TK_ISNULL: op = OP_IsNull; break;
1176 case TK_NOTNULL: op = OP_NotNull; break;
1177 case TK_NOT: op = OP_Not; break;
1178 case TK_UMINUS: op = OP_Negative; break;
drhbf4133c2001-10-13 02:59:08 +00001179 case TK_BITAND: op = OP_BitAnd; break;
1180 case TK_BITOR: op = OP_BitOr; break;
1181 case TK_BITNOT: op = OP_BitNot; break;
1182 case TK_LSHIFT: op = OP_ShiftLeft; break;
1183 case TK_RSHIFT: op = OP_ShiftRight; break;
1184 case TK_REM: op = OP_Remainder; break;
drhfec19aa2004-05-19 20:41:03 +00001185 case TK_FLOAT: op = OP_Real; break;
1186 case TK_STRING: op = OP_String; break;
drhcce7d172000-05-31 15:34:51 +00001187 default: break;
1188 }
1189 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00001190 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +00001191 if( pParse->useAgg ){
danielk19774adee202004-05-08 08:23:19 +00001192 sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drhc4a3c772001-04-04 11:48:57 +00001193 }else if( pExpr->iColumn>=0 ){
danielk19774adee202004-05-08 08:23:19 +00001194 sqlite3VdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn);
drhc4a3c772001-04-04 11:48:57 +00001195 }else{
danielk19774adee202004-05-08 08:23:19 +00001196 sqlite3VdbeAddOp(v, OP_Recno, pExpr->iTable, 0);
drh22827922000-06-06 17:27:05 +00001197 }
drhcce7d172000-05-31 15:34:51 +00001198 break;
1199 }
1200 case TK_INTEGER: {
drhfec19aa2004-05-19 20:41:03 +00001201 codeInteger(v, pExpr->token.z, pExpr->token.n);
1202 break;
1203 }
1204 case TK_FLOAT:
1205 case TK_STRING: {
1206 sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z, pExpr->token.n);
danielk19774adee202004-05-08 08:23:19 +00001207 sqlite3VdbeDequoteP3(v, -1);
drhcce7d172000-05-31 15:34:51 +00001208 break;
1209 }
1210 case TK_NULL: {
danielk19774adee202004-05-08 08:23:19 +00001211 sqlite3VdbeAddOp(v, OP_String, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001212 break;
1213 }
drh50457892003-09-06 01:10:47 +00001214 case TK_VARIABLE: {
danielk19774adee202004-05-08 08:23:19 +00001215 sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0);
drh50457892003-09-06 01:10:47 +00001216 break;
1217 }
drhc9b84a12002-06-20 11:36:48 +00001218 case TK_LT:
1219 case TK_LE:
1220 case TK_GT:
1221 case TK_GE:
1222 case TK_NE:
1223 case TK_EQ: {
danielk1977a37cdde2004-05-16 11:15:36 +00001224 int p1 = binaryCompareP1(pExpr->pLeft, pExpr->pRight, 0);
1225 sqlite3ExprCode(pParse, pExpr->pLeft);
1226 sqlite3ExprCode(pParse, pExpr->pRight);
1227 sqlite3VdbeAddOp(v, op, p1, 0);
1228 break;
drhc9b84a12002-06-20 11:36:48 +00001229 }
drhcce7d172000-05-31 15:34:51 +00001230 case TK_AND:
1231 case TK_OR:
1232 case TK_PLUS:
1233 case TK_STAR:
1234 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00001235 case TK_REM:
1236 case TK_BITAND:
1237 case TK_BITOR:
drhc9b84a12002-06-20 11:36:48 +00001238 case TK_SLASH: {
danielk19774adee202004-05-08 08:23:19 +00001239 sqlite3ExprCode(pParse, pExpr->pLeft);
1240 sqlite3ExprCode(pParse, pExpr->pRight);
1241 sqlite3VdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001242 break;
1243 }
drhbf4133c2001-10-13 02:59:08 +00001244 case TK_LSHIFT:
1245 case TK_RSHIFT: {
danielk19774adee202004-05-08 08:23:19 +00001246 sqlite3ExprCode(pParse, pExpr->pRight);
1247 sqlite3ExprCode(pParse, pExpr->pLeft);
1248 sqlite3VdbeAddOp(v, op, 0, 0);
drhbf4133c2001-10-13 02:59:08 +00001249 break;
1250 }
drh00400772000-06-16 20:51:26 +00001251 case TK_CONCAT: {
danielk19774adee202004-05-08 08:23:19 +00001252 sqlite3ExprCode(pParse, pExpr->pLeft);
1253 sqlite3ExprCode(pParse, pExpr->pRight);
1254 sqlite3VdbeAddOp(v, OP_Concat, 2, 0);
drh00400772000-06-16 20:51:26 +00001255 break;
1256 }
drhcce7d172000-05-31 15:34:51 +00001257 case TK_UMINUS: {
drhfec19aa2004-05-19 20:41:03 +00001258 Expr *pLeft = pExpr->pLeft;
1259 assert( pLeft );
1260 if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
1261 Token *p = &pLeft->token;
drh6e142f52000-06-08 13:36:40 +00001262 char *z = sqliteMalloc( p->n + 2 );
1263 sprintf(z, "-%.*s", p->n, p->z);
drhfec19aa2004-05-19 20:41:03 +00001264 if( pLeft->op==TK_FLOAT ){
1265 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001266 }else{
drhfec19aa2004-05-19 20:41:03 +00001267 codeInteger(v, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001268 }
drh6e142f52000-06-08 13:36:40 +00001269 sqliteFree(z);
1270 break;
1271 }
drh1ccde152000-06-17 13:12:39 +00001272 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +00001273 }
drhbf4133c2001-10-13 02:59:08 +00001274 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00001275 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001276 sqlite3ExprCode(pParse, pExpr->pLeft);
1277 sqlite3VdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001278 break;
1279 }
1280 case TK_ISNULL:
1281 case TK_NOTNULL: {
1282 int dest;
danielk19774adee202004-05-08 08:23:19 +00001283 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1284 sqlite3ExprCode(pParse, pExpr->pLeft);
1285 dest = sqlite3VdbeCurrentAddr(v) + 2;
1286 sqlite3VdbeAddOp(v, op, 1, dest);
1287 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);
drhcce7d172000-05-31 15:34:51 +00001288 }
danielk1977a37cdde2004-05-16 11:15:36 +00001289 break;
drh22827922000-06-06 17:27:05 +00001290 case TK_AGG_FUNCTION: {
danielk19774adee202004-05-08 08:23:19 +00001291 sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drh22827922000-06-06 17:27:05 +00001292 break;
1293 }
drh4b59ab52002-08-24 18:24:51 +00001294 case TK_GLOB:
1295 case TK_LIKE:
drhcce7d172000-05-31 15:34:51 +00001296 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00001297 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00001298 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00001299 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00001300 int nId;
1301 const char *zId;
1302 getFunctionName(pExpr, &zId, &nId);
danielk19774adee202004-05-08 08:23:19 +00001303 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, 0);
drh0bce8352002-02-28 00:41:10 +00001304 assert( pDef!=0 );
danielk19774adee202004-05-08 08:23:19 +00001305 nExpr = sqlite3ExprCodeExprList(pParse, pList, pDef->includeTypes);
danielk1977a37cdde2004-05-16 11:15:36 +00001306 /* FIX ME: The following is a temporary hack. */
1307 if( 0==sqlite3StrNICmp(zId, "classof", nId) ){
1308 assert( nExpr==1 );
drhfec19aa2004-05-19 20:41:03 +00001309 sqlite3VdbeAddOp(v, OP_Class, nExpr, 0);
danielk1977a37cdde2004-05-16 11:15:36 +00001310 }else{
1311 sqlite3VdbeOp3(v, OP_Function, nExpr, 0, (char*)pDef, P3_POINTER);
1312 }
drhcce7d172000-05-31 15:34:51 +00001313 break;
1314 }
drh19a775c2000-06-05 18:54:46 +00001315 case TK_SELECT: {
danielk19774adee202004-05-08 08:23:19 +00001316 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drh19a775c2000-06-05 18:54:46 +00001317 break;
1318 }
drhfef52082000-06-06 01:50:43 +00001319 case TK_IN: {
1320 int addr;
danielk1977e014a832004-05-17 10:48:57 +00001321 char const *affStr;
1322
1323 /* Figure out the affinity to use to create a key from the results
1324 ** of the expression. affinityStr stores a static string suitable for
1325 ** P3 of OP_MakeKey.
1326 */
1327 affStr = sqlite3AffinityString(comparisonAffinity(pExpr));
1328
danielk19774adee202004-05-08 08:23:19 +00001329 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
danielk1977e014a832004-05-17 10:48:57 +00001330
1331 /* Code the <expr> from "<expr> IN (...)". The temporary table
1332 ** pExpr->iTable contains the values that make up the (...) set.
1333 */
danielk19774adee202004-05-08 08:23:19 +00001334 sqlite3ExprCode(pParse, pExpr->pLeft);
1335 addr = sqlite3VdbeCurrentAddr(v);
danielk1977e014a832004-05-17 10:48:57 +00001336 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); /* addr + 0 */
danielk19774adee202004-05-08 08:23:19 +00001337 sqlite3VdbeAddOp(v, OP_Pop, 2, 0);
1338 sqlite3VdbeAddOp(v, OP_String, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +00001339 sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7);
1340 sqlite3VdbeOp3(v, OP_MakeKey, 1, 0, affStr, P3_STATIC); /* addr + 4 */
1341 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7);
1342 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); /* addr + 6 */
1343
drhfef52082000-06-06 01:50:43 +00001344 break;
1345 }
1346 case TK_BETWEEN: {
danielk19774adee202004-05-08 08:23:19 +00001347 sqlite3ExprCode(pParse, pExpr->pLeft);
1348 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
1349 sqlite3ExprCode(pParse, pExpr->pList->a[0].pExpr);
1350 sqlite3VdbeAddOp(v, OP_Ge, 0, 0);
1351 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
1352 sqlite3ExprCode(pParse, pExpr->pList->a[1].pExpr);
1353 sqlite3VdbeAddOp(v, OP_Le, 0, 0);
1354 sqlite3VdbeAddOp(v, OP_And, 0, 0);
drhfef52082000-06-06 01:50:43 +00001355 break;
1356 }
drh51e9a442004-01-16 16:42:53 +00001357 case TK_UPLUS:
drha2e00042002-01-22 03:13:42 +00001358 case TK_AS: {
danielk19774adee202004-05-08 08:23:19 +00001359 sqlite3ExprCode(pParse, pExpr->pLeft);
drha2e00042002-01-22 03:13:42 +00001360 break;
1361 }
drh17a7f8d2002-03-24 13:13:27 +00001362 case TK_CASE: {
1363 int expr_end_label;
drhf5905aa2002-05-26 20:54:33 +00001364 int jumpInst;
1365 int addr;
1366 int nExpr;
drh17a7f8d2002-03-24 13:13:27 +00001367 int i;
1368
1369 assert(pExpr->pList);
1370 assert((pExpr->pList->nExpr % 2) == 0);
1371 assert(pExpr->pList->nExpr > 0);
drhf5905aa2002-05-26 20:54:33 +00001372 nExpr = pExpr->pList->nExpr;
danielk19774adee202004-05-08 08:23:19 +00001373 expr_end_label = sqlite3VdbeMakeLabel(v);
drh17a7f8d2002-03-24 13:13:27 +00001374 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001375 sqlite3ExprCode(pParse, pExpr->pLeft);
drh17a7f8d2002-03-24 13:13:27 +00001376 }
drhf5905aa2002-05-26 20:54:33 +00001377 for(i=0; i<nExpr; i=i+2){
danielk19774adee202004-05-08 08:23:19 +00001378 sqlite3ExprCode(pParse, pExpr->pList->a[i].pExpr);
drh17a7f8d2002-03-24 13:13:27 +00001379 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001380 sqlite3VdbeAddOp(v, OP_Dup, 1, 1);
1381 jumpInst = sqlite3VdbeAddOp(v, OP_Ne, 1, 0);
1382 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001383 }else{
danielk19774adee202004-05-08 08:23:19 +00001384 jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001385 }
danielk19774adee202004-05-08 08:23:19 +00001386 sqlite3ExprCode(pParse, pExpr->pList->a[i+1].pExpr);
1387 sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label);
1388 addr = sqlite3VdbeCurrentAddr(v);
1389 sqlite3VdbeChangeP2(v, jumpInst, addr);
drh17a7f8d2002-03-24 13:13:27 +00001390 }
drhf570f012002-05-31 15:51:25 +00001391 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001392 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhf570f012002-05-31 15:51:25 +00001393 }
drh17a7f8d2002-03-24 13:13:27 +00001394 if( pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001395 sqlite3ExprCode(pParse, pExpr->pRight);
drh17a7f8d2002-03-24 13:13:27 +00001396 }else{
danielk19774adee202004-05-08 08:23:19 +00001397 sqlite3VdbeAddOp(v, OP_String, 0, 0);
drh17a7f8d2002-03-24 13:13:27 +00001398 }
danielk19774adee202004-05-08 08:23:19 +00001399 sqlite3VdbeResolveLabel(v, expr_end_label);
danielk19776f349032002-06-11 02:25:40 +00001400 break;
1401 }
1402 case TK_RAISE: {
1403 if( !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +00001404 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00001405 "RAISE() may only be used within a trigger-program");
danielk19776f349032002-06-11 02:25:40 +00001406 pParse->nErr++;
1407 return;
1408 }
1409 if( pExpr->iColumn == OE_Rollback ||
1410 pExpr->iColumn == OE_Abort ||
1411 pExpr->iColumn == OE_Fail ){
danielk19774adee202004-05-08 08:23:19 +00001412 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn,
drh701a0ae2004-02-22 20:05:00 +00001413 pExpr->token.z, pExpr->token.n);
danielk19774adee202004-05-08 08:23:19 +00001414 sqlite3VdbeDequoteP3(v, -1);
danielk19776f349032002-06-11 02:25:40 +00001415 } else {
1416 assert( pExpr->iColumn == OE_Ignore );
danielk19774adee202004-05-08 08:23:19 +00001417 sqlite3VdbeOp3(v, OP_Goto, 0, pParse->trigStack->ignoreJump,
drh701a0ae2004-02-22 20:05:00 +00001418 "(IGNORE jump)", 0);
danielk19776f349032002-06-11 02:25:40 +00001419 }
drh17a7f8d2002-03-24 13:13:27 +00001420 }
1421 break;
drhcce7d172000-05-31 15:34:51 +00001422 }
drhcce7d172000-05-31 15:34:51 +00001423}
1424
1425/*
drh268380c2004-02-25 13:47:31 +00001426** Generate code that pushes the value of every element of the given
1427** expression list onto the stack. If the includeTypes flag is true,
1428** then also push a string that is the datatype of each element onto
1429** the stack after the value.
1430**
1431** Return the number of elements pushed onto the stack.
1432*/
danielk19774adee202004-05-08 08:23:19 +00001433int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00001434 Parse *pParse, /* Parsing context */
1435 ExprList *pList, /* The expression list to be coded */
1436 int includeTypes /* TRUE to put datatypes on the stack too */
1437){
1438 struct ExprList_item *pItem;
1439 int i, n;
1440 Vdbe *v;
1441 if( pList==0 ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001442 v = sqlite3GetVdbe(pParse);
drh268380c2004-02-25 13:47:31 +00001443 n = pList->nExpr;
1444 for(pItem=pList->a, i=0; i<n; i++, pItem++){
danielk19774adee202004-05-08 08:23:19 +00001445 sqlite3ExprCode(pParse, pItem->pExpr);
drh268380c2004-02-25 13:47:31 +00001446 if( includeTypes ){
danielk19774adee202004-05-08 08:23:19 +00001447 sqlite3VdbeOp3(v, OP_String, 0, 0,
1448 sqlite3ExprType(pItem->pExpr)==SQLITE_SO_NUM ? "numeric" : "text",
drh268380c2004-02-25 13:47:31 +00001449 P3_STATIC);
1450 }
1451 }
1452 return includeTypes ? n*2 : n;
1453}
1454
1455/*
drhcce7d172000-05-31 15:34:51 +00001456** Generate code for a boolean expression such that a jump is made
1457** to the label "dest" if the expression is true but execution
1458** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00001459**
1460** If the expression evaluates to NULL (neither true nor false), then
1461** take the jump if the jumpIfNull flag is true.
drhcce7d172000-05-31 15:34:51 +00001462*/
danielk19774adee202004-05-08 08:23:19 +00001463void sqlite3ExprIfTrue(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_Lt; break;
1469 case TK_LE: op = OP_Le; break;
1470 case TK_GT: op = OP_Gt; break;
1471 case TK_GE: op = OP_Ge; break;
1472 case TK_NE: op = OP_Ne; break;
1473 case TK_EQ: op = OP_Eq; break;
drhcce7d172000-05-31 15:34:51 +00001474 case TK_ISNULL: op = OP_IsNull; break;
1475 case TK_NOTNULL: op = OP_NotNull; break;
1476 default: break;
1477 }
1478 switch( pExpr->op ){
1479 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001480 int d2 = sqlite3VdbeMakeLabel(v);
1481 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
1482 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
1483 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001484 break;
1485 }
1486 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001487 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
1488 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001489 break;
1490 }
1491 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001492 sqlite3ExprIfFalse(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:
drh0ac65892002-04-20 14:24:41 +00001500 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 }
1507 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_Found, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001523 }else{
danielk19774adee202004-05-08 08:23:19 +00001524 sqlite3VdbeAddOp(v, OP_SetFound, 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: {
drhf5905aa2002-05-26 20:54:33 +00001530 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 = sqlite3VdbeAddOp(v, OP_Lt, !jumpIfNull, 0);
1535 sqlite3ExprCode(pParse, pExpr->pList->a[1].pExpr);
1536 sqlite3VdbeAddOp(v, OP_Le, jumpIfNull, dest);
1537 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
1538 sqlite3VdbeChangeP2(v, addr, sqlite3VdbeCurrentAddr(v));
1539 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
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_If, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001545 break;
1546 }
1547 }
1548}
1549
1550/*
drh66b89c82000-11-28 20:47:17 +00001551** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00001552** to the label "dest" if the expression is false but execution
1553** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00001554**
1555** If the expression evaluates to NULL (neither true nor false) then
1556** jump if jumpIfNull is true or fall through if jumpIfNull is false.
drhcce7d172000-05-31 15:34:51 +00001557*/
danielk19774adee202004-05-08 08:23:19 +00001558void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001559 Vdbe *v = pParse->pVdbe;
1560 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001561 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001562 switch( pExpr->op ){
1563 case TK_LT: op = OP_Ge; break;
1564 case TK_LE: op = OP_Gt; break;
1565 case TK_GT: op = OP_Le; break;
1566 case TK_GE: op = OP_Lt; break;
1567 case TK_NE: op = OP_Eq; break;
1568 case TK_EQ: op = OP_Ne; break;
drhcce7d172000-05-31 15:34:51 +00001569 case TK_ISNULL: op = OP_NotNull; break;
1570 case TK_NOTNULL: op = OP_IsNull; break;
1571 default: break;
1572 }
1573 switch( pExpr->op ){
1574 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001575 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
1576 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001577 break;
1578 }
1579 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001580 int d2 = sqlite3VdbeMakeLabel(v);
1581 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
1582 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
1583 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001584 break;
1585 }
1586 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001587 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001588 break;
1589 }
1590 case TK_LT:
1591 case TK_LE:
1592 case TK_GT:
1593 case TK_GE:
1594 case TK_NE:
1595 case TK_EQ: {
danielk1977a37cdde2004-05-16 11:15:36 +00001596 int p1 = binaryCompareP1(pExpr->pLeft, pExpr->pRight, jumpIfNull);
danielk19774adee202004-05-08 08:23:19 +00001597 sqlite3ExprCode(pParse, pExpr->pLeft);
1598 sqlite3ExprCode(pParse, pExpr->pRight);
danielk1977a37cdde2004-05-16 11:15:36 +00001599 sqlite3VdbeAddOp(v, op, p1, dest);
drhcce7d172000-05-31 15:34:51 +00001600 break;
1601 }
drhcce7d172000-05-31 15:34:51 +00001602 case TK_ISNULL:
1603 case TK_NOTNULL: {
danielk19774adee202004-05-08 08:23:19 +00001604 sqlite3ExprCode(pParse, pExpr->pLeft);
1605 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001606 break;
1607 }
danielk1977e014a832004-05-17 10:48:57 +00001608#if 0
drhfef52082000-06-06 01:50:43 +00001609 case TK_IN: {
drhf5905aa2002-05-26 20:54:33 +00001610 int addr;
danielk19774adee202004-05-08 08:23:19 +00001611 sqlite3ExprCode(pParse, pExpr->pLeft);
1612 addr = sqlite3VdbeCurrentAddr(v);
1613 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+3);
1614 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
1615 sqlite3VdbeAddOp(v, OP_Goto, 0, jumpIfNull ? dest : addr+4);
drhfef52082000-06-06 01:50:43 +00001616 if( pExpr->pSelect ){
danielk19774adee202004-05-08 08:23:19 +00001617 sqlite3VdbeAddOp(v, OP_NotFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001618 }else{
danielk19774adee202004-05-08 08:23:19 +00001619 sqlite3VdbeAddOp(v, OP_SetNotFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001620 }
1621 break;
1622 }
danielk1977e014a832004-05-17 10:48:57 +00001623#endif
drhfef52082000-06-06 01:50:43 +00001624 case TK_BETWEEN: {
1625 int addr;
danielk19774adee202004-05-08 08:23:19 +00001626 sqlite3ExprCode(pParse, pExpr->pLeft);
1627 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
1628 sqlite3ExprCode(pParse, pExpr->pList->a[0].pExpr);
1629 addr = sqlite3VdbeCurrentAddr(v);
1630 sqlite3VdbeAddOp(v, OP_Ge, !jumpIfNull, addr+3);
1631 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
1632 sqlite3VdbeAddOp(v, OP_Goto, 0, dest);
1633 sqlite3ExprCode(pParse, pExpr->pList->a[1].pExpr);
1634 sqlite3VdbeAddOp(v, OP_Gt, jumpIfNull, dest);
drhfef52082000-06-06 01:50:43 +00001635 break;
1636 }
drhcce7d172000-05-31 15:34:51 +00001637 default: {
danielk19774adee202004-05-08 08:23:19 +00001638 sqlite3ExprCode(pParse, pExpr);
1639 sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001640 break;
1641 }
1642 }
1643}
drh22827922000-06-06 17:27:05 +00001644
1645/*
1646** Do a deep comparison of two expression trees. Return TRUE (non-zero)
1647** if they are identical and return FALSE if they differ in any way.
1648*/
danielk19774adee202004-05-08 08:23:19 +00001649int sqlite3ExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00001650 int i;
1651 if( pA==0 ){
1652 return pB==0;
1653 }else if( pB==0 ){
1654 return 0;
1655 }
1656 if( pA->op!=pB->op ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001657 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
1658 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00001659 if( pA->pList ){
1660 if( pB->pList==0 ) return 0;
1661 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
1662 for(i=0; i<pA->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00001663 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00001664 return 0;
1665 }
1666 }
1667 }else if( pB->pList ){
1668 return 0;
1669 }
1670 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00001671 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drh22827922000-06-06 17:27:05 +00001672 if( pA->token.z ){
1673 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00001674 if( pB->token.n!=pA->token.n ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001675 if( sqlite3StrNICmp(pA->token.z, pB->token.z, pB->token.n)!=0 ) return 0;
drh22827922000-06-06 17:27:05 +00001676 }
1677 return 1;
1678}
1679
1680/*
1681** Add a new element to the pParse->aAgg[] array and return its index.
1682*/
1683static int appendAggInfo(Parse *pParse){
1684 if( (pParse->nAgg & 0x7)==0 ){
1685 int amt = pParse->nAgg + 8;
drh6d4abfb2001-10-22 02:58:08 +00001686 AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0]));
1687 if( aAgg==0 ){
drh22827922000-06-06 17:27:05 +00001688 return -1;
1689 }
drh6d4abfb2001-10-22 02:58:08 +00001690 pParse->aAgg = aAgg;
drh22827922000-06-06 17:27:05 +00001691 }
1692 memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0]));
1693 return pParse->nAgg++;
1694}
1695
1696/*
1697** Analyze the given expression looking for aggregate functions and
1698** for variables that need to be added to the pParse->aAgg[] array.
1699** Make additional entries to the pParse->aAgg[] array as necessary.
1700**
1701** This routine should only be called after the expression has been
danielk19774adee202004-05-08 08:23:19 +00001702** analyzed by sqlite3ExprResolveIds() and sqlite3ExprCheck().
drh22827922000-06-06 17:27:05 +00001703**
1704** If errors are seen, leave an error message in zErrMsg and return
1705** the number of errors.
1706*/
danielk19774adee202004-05-08 08:23:19 +00001707int sqlite3ExprAnalyzeAggregates(Parse *pParse, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00001708 int i;
1709 AggExpr *aAgg;
1710 int nErr = 0;
1711
1712 if( pExpr==0 ) return 0;
1713 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00001714 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +00001715 aAgg = pParse->aAgg;
1716 for(i=0; i<pParse->nAgg; i++){
1717 if( aAgg[i].isAgg ) continue;
1718 if( aAgg[i].pExpr->iTable==pExpr->iTable
drh967e8b72000-06-21 13:59:10 +00001719 && aAgg[i].pExpr->iColumn==pExpr->iColumn ){
drh22827922000-06-06 17:27:05 +00001720 break;
1721 }
1722 }
1723 if( i>=pParse->nAgg ){
1724 i = appendAggInfo(pParse);
1725 if( i<0 ) return 1;
1726 pParse->aAgg[i].isAgg = 0;
1727 pParse->aAgg[i].pExpr = pExpr;
1728 }
drhaaf88722000-06-08 11:25:00 +00001729 pExpr->iAgg = i;
drh22827922000-06-06 17:27:05 +00001730 break;
1731 }
1732 case TK_AGG_FUNCTION: {
drh22827922000-06-06 17:27:05 +00001733 aAgg = pParse->aAgg;
1734 for(i=0; i<pParse->nAgg; i++){
1735 if( !aAgg[i].isAgg ) continue;
danielk19774adee202004-05-08 08:23:19 +00001736 if( sqlite3ExprCompare(aAgg[i].pExpr, pExpr) ){
drh22827922000-06-06 17:27:05 +00001737 break;
1738 }
1739 }
1740 if( i>=pParse->nAgg ){
1741 i = appendAggInfo(pParse);
1742 if( i<0 ) return 1;
1743 pParse->aAgg[i].isAgg = 1;
1744 pParse->aAgg[i].pExpr = pExpr;
danielk19774adee202004-05-08 08:23:19 +00001745 pParse->aAgg[i].pFunc = sqlite3FindFunction(pParse->db,
drh6977fea2002-10-22 23:38:04 +00001746 pExpr->token.z, pExpr->token.n,
drhf55f25f2002-02-28 01:46:11 +00001747 pExpr->pList ? pExpr->pList->nExpr : 0, 0);
drh22827922000-06-06 17:27:05 +00001748 }
1749 pExpr->iAgg = i;
1750 break;
1751 }
1752 default: {
1753 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001754 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pLeft);
drh22827922000-06-06 17:27:05 +00001755 }
1756 if( nErr==0 && pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001757 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pRight);
drh22827922000-06-06 17:27:05 +00001758 }
1759 if( nErr==0 && pExpr->pList ){
1760 int n = pExpr->pList->nExpr;
1761 int i;
1762 for(i=0; nErr==0 && i<n; i++){
danielk19774adee202004-05-08 08:23:19 +00001763 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pList->a[i].pExpr);
drh22827922000-06-06 17:27:05 +00001764 }
1765 }
1766 break;
1767 }
1768 }
1769 return nErr;
1770}
drh8e0a2f92002-02-23 23:45:45 +00001771
1772/*
1773** Locate a user function given a name and a number of arguments.
drh0bce8352002-02-28 00:41:10 +00001774** Return a pointer to the FuncDef structure that defines that
drh8e0a2f92002-02-23 23:45:45 +00001775** function, or return NULL if the function does not exist.
1776**
drh0bce8352002-02-28 00:41:10 +00001777** If the createFlag argument is true, then a new (blank) FuncDef
drh8e0a2f92002-02-23 23:45:45 +00001778** structure is created and liked into the "db" structure if a
1779** no matching function previously existed. When createFlag is true
1780** and the nArg parameter is -1, then only a function that accepts
1781** any number of arguments will be returned.
1782**
1783** If createFlag is false and nArg is -1, then the first valid
1784** function found is returned. A function is valid if either xFunc
1785** or xStep is non-zero.
1786*/
danielk19774adee202004-05-08 08:23:19 +00001787FuncDef *sqlite3FindFunction(
drh8e0a2f92002-02-23 23:45:45 +00001788 sqlite *db, /* An open database */
1789 const char *zName, /* Name of the function. Not null-terminated */
1790 int nName, /* Number of characters in the name */
1791 int nArg, /* Number of arguments. -1 means any number */
1792 int createFlag /* Create new entry if true and does not otherwise exist */
1793){
drh0bce8352002-02-28 00:41:10 +00001794 FuncDef *pFirst, *p, *pMaybe;
danielk19774adee202004-05-08 08:23:19 +00001795 pFirst = p = (FuncDef*)sqlite3HashFind(&db->aFunc, zName, nName);
drh1350b032002-02-27 19:00:20 +00001796 if( p && !createFlag && nArg<0 ){
drh8e0a2f92002-02-23 23:45:45 +00001797 while( p && p->xFunc==0 && p->xStep==0 ){ p = p->pNext; }
1798 return p;
1799 }
1800 pMaybe = 0;
1801 while( p && p->nArg!=nArg ){
1802 if( p->nArg<0 && !createFlag && (p->xFunc || p->xStep) ) pMaybe = p;
1803 p = p->pNext;
1804 }
1805 if( p && !createFlag && p->xFunc==0 && p->xStep==0 ){
1806 return 0;
1807 }
1808 if( p==0 && pMaybe ){
1809 assert( createFlag==0 );
1810 return pMaybe;
1811 }
drh89425d52002-02-28 03:04:48 +00001812 if( p==0 && createFlag && (p = sqliteMalloc(sizeof(*p)))!=0 ){
drh8e0a2f92002-02-23 23:45:45 +00001813 p->nArg = nArg;
1814 p->pNext = pFirst;
drhc9b84a12002-06-20 11:36:48 +00001815 p->dataType = pFirst ? pFirst->dataType : SQLITE_NUMERIC;
danielk19774adee202004-05-08 08:23:19 +00001816 sqlite3HashInsert(&db->aFunc, zName, nName, (void*)p);
drh8e0a2f92002-02-23 23:45:45 +00001817 }
1818 return p;
1819}