blob: a0fff71d8e86b84fd42e4e09c0c929447865b6ae [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**
danielk1977bf3b7212004-05-18 10:06:24 +000015** $Id: expr.c,v 1.122 2004/05/18 10:06:25 danielk1977 Exp $
drhcce7d172000-05-31 15:34:51 +000016*/
17#include "sqliteInt.h"
drh04738cb2002-06-02 18:19:00 +000018#include <ctype.h>
drha2e00042002-01-22 03:13:42 +000019
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
danielk1977e014a832004-05-17 10:48:57 +000058char sqlite3CompareAffinity(Expr *pExpr, char aff2){
danielk1977bf3b7212004-05-18 10:06:24 +000059 char aff1 = sqlite3ExprAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +000060 if( aff1 && aff2 ){
61 /* Both sides of the comparison are columns. If one has numeric or
62 ** integer affinity, use that. Otherwise use no affinity.
63 */
64 if( aff1==SQLITE_AFF_INTEGER || aff2==SQLITE_AFF_INTEGER ){
65 return SQLITE_AFF_INTEGER;
66 }else if( aff1==SQLITE_AFF_NUMERIC || aff2==SQLITE_AFF_NUMERIC ){
67 return SQLITE_AFF_NUMERIC;
68 }else{
69 return SQLITE_AFF_NONE;
70 }
71 }else if( !aff1 && !aff2 ){
72 /* Neither side of the comparison is a column. Use numeric affinity
73 ** for the comparison.
74 */
75 return SQLITE_AFF_NUMERIC;
76 }else{
77 /* One side is a column, the other is not. Use the columns affinity. */
78 return (aff1 + aff2);
79 }
80}
81
82static char comparisonAffinity(Expr *pExpr){
83 char aff;
84 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
85 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
86 pExpr->op==TK_NE );
87 assert( pExpr->pLeft );
danielk1977bf3b7212004-05-18 10:06:24 +000088 aff = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +000089 if( pExpr->pRight ){
90 aff = sqlite3CompareAffinity(pExpr->pRight, aff);
91 }
92 else if( pExpr->pSelect ){
93 aff = sqlite3CompareAffinity(pExpr->pSelect->pEList->a[0].pExpr, aff);
94 }
95 else if( !aff ){
96 aff = SQLITE_AFF_NUMERIC;
97 }
98 return aff;
99}
100
101/*
102** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
103** idx_affinity is the affinity of an indexed column. Return true
104** if the index with affinity idx_affinity may be used to implement
105** the comparison in pExpr.
106*/
107int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){
108 char aff = comparisonAffinity(pExpr);
109 return
110 (aff==SQLITE_AFF_NONE) ||
111 (aff==SQLITE_AFF_NUMERIC && idx_affinity==SQLITE_AFF_INTEGER) ||
112 (aff==SQLITE_AFF_INTEGER && idx_affinity==SQLITE_AFF_NUMERIC) ||
113 (aff==idx_affinity);
114}
115
danielk1977a37cdde2004-05-16 11:15:36 +0000116/*
117** Return the P1 value that should be used for a binary comparison
118** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
119** If jumpIfNull is true, then set the low byte of the returned
120** P1 value to tell the opcode to jump if either expression
121** evaluates to NULL.
122*/
danielk1977e014a832004-05-17 10:48:57 +0000123static int binaryCompareP1(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){
danielk1977bf3b7212004-05-18 10:06:24 +0000124 char aff = sqlite3ExprAffinity(pExpr2);
danielk1977e014a832004-05-17 10:48:57 +0000125 return (((int)sqlite3CompareAffinity(pExpr1, aff))<<8)+(jumpIfNull?1:0);
danielk1977a37cdde2004-05-16 11:15:36 +0000126}
127
drha2e00042002-01-22 03:13:42 +0000128/*
drha76b5df2002-02-23 02:32:10 +0000129** Construct a new expression node and return a pointer to it. Memory
130** for this node is obtained from sqliteMalloc(). The calling function
131** is responsible for making sure the node eventually gets freed.
132*/
danielk19774adee202004-05-08 08:23:19 +0000133Expr *sqlite3Expr(int op, Expr *pLeft, Expr *pRight, Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000134 Expr *pNew;
135 pNew = sqliteMalloc( sizeof(Expr) );
136 if( pNew==0 ){
drh4efc4752004-01-16 15:55:37 +0000137 /* When malloc fails, we leak memory from pLeft and pRight */
drha76b5df2002-02-23 02:32:10 +0000138 return 0;
139 }
140 pNew->op = op;
141 pNew->pLeft = pLeft;
142 pNew->pRight = pRight;
143 if( pToken ){
drh4b59ab52002-08-24 18:24:51 +0000144 assert( pToken->dyn==0 );
drha76b5df2002-02-23 02:32:10 +0000145 pNew->token = *pToken;
drh6977fea2002-10-22 23:38:04 +0000146 pNew->span = *pToken;
drha76b5df2002-02-23 02:32:10 +0000147 }else{
drh4efc4752004-01-16 15:55:37 +0000148 assert( pNew->token.dyn==0 );
149 assert( pNew->token.z==0 );
150 assert( pNew->token.n==0 );
drh6977fea2002-10-22 23:38:04 +0000151 if( pLeft && pRight ){
danielk19774adee202004-05-08 08:23:19 +0000152 sqlite3ExprSpan(pNew, &pLeft->span, &pRight->span);
drh6977fea2002-10-22 23:38:04 +0000153 }else{
154 pNew->span = pNew->token;
155 }
drha76b5df2002-02-23 02:32:10 +0000156 }
drha76b5df2002-02-23 02:32:10 +0000157 return pNew;
158}
159
160/*
drh6977fea2002-10-22 23:38:04 +0000161** Set the Expr.span field of the given expression to span all
drha76b5df2002-02-23 02:32:10 +0000162** text between the two given tokens.
163*/
danielk19774adee202004-05-08 08:23:19 +0000164void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
drh4efc4752004-01-16 15:55:37 +0000165 assert( pRight!=0 );
166 assert( pLeft!=0 );
167 /* Note: pExpr might be NULL due to a prior malloc failure */
168 if( pExpr && pRight->z && pLeft->z ){
drh4b59ab52002-08-24 18:24:51 +0000169 if( pLeft->dyn==0 && pRight->dyn==0 ){
drh6977fea2002-10-22 23:38:04 +0000170 pExpr->span.z = pLeft->z;
171 pExpr->span.n = pRight->n + Addr(pRight->z) - Addr(pLeft->z);
drh4b59ab52002-08-24 18:24:51 +0000172 }else{
drh6977fea2002-10-22 23:38:04 +0000173 pExpr->span.z = 0;
drh4b59ab52002-08-24 18:24:51 +0000174 }
drha76b5df2002-02-23 02:32:10 +0000175 }
176}
177
178/*
179** Construct a new expression node for a function with multiple
180** arguments.
181*/
danielk19774adee202004-05-08 08:23:19 +0000182Expr *sqlite3ExprFunction(ExprList *pList, Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000183 Expr *pNew;
184 pNew = sqliteMalloc( sizeof(Expr) );
185 if( pNew==0 ){
danielk19774adee202004-05-08 08:23:19 +0000186 /* sqlite3ExprListDelete(pList); // Leak pList when malloc fails */
drha76b5df2002-02-23 02:32:10 +0000187 return 0;
188 }
189 pNew->op = TK_FUNCTION;
190 pNew->pList = pList;
191 if( pToken ){
drh4b59ab52002-08-24 18:24:51 +0000192 assert( pToken->dyn==0 );
drha76b5df2002-02-23 02:32:10 +0000193 pNew->token = *pToken;
194 }else{
195 pNew->token.z = 0;
drha76b5df2002-02-23 02:32:10 +0000196 }
drh6977fea2002-10-22 23:38:04 +0000197 pNew->span = pNew->token;
drha76b5df2002-02-23 02:32:10 +0000198 return pNew;
199}
200
201/*
drha2e00042002-01-22 03:13:42 +0000202** Recursively delete an expression tree.
203*/
danielk19774adee202004-05-08 08:23:19 +0000204void sqlite3ExprDelete(Expr *p){
drha2e00042002-01-22 03:13:42 +0000205 if( p==0 ) return;
drh4efc4752004-01-16 15:55:37 +0000206 if( p->span.dyn ) sqliteFree((char*)p->span.z);
207 if( p->token.dyn ) sqliteFree((char*)p->token.z);
danielk19774adee202004-05-08 08:23:19 +0000208 sqlite3ExprDelete(p->pLeft);
209 sqlite3ExprDelete(p->pRight);
210 sqlite3ExprListDelete(p->pList);
211 sqlite3SelectDelete(p->pSelect);
drha2e00042002-01-22 03:13:42 +0000212 sqliteFree(p);
213}
214
drha76b5df2002-02-23 02:32:10 +0000215
216/*
drhff78bd22002-02-27 01:47:11 +0000217** The following group of routines make deep copies of expressions,
218** expression lists, ID lists, and select statements. The copies can
219** be deleted (by being passed to their respective ...Delete() routines)
220** without effecting the originals.
221**
danielk19774adee202004-05-08 08:23:19 +0000222** The expression list, ID, and source lists return by sqlite3ExprListDup(),
223** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
drhad3cab52002-05-24 02:04:32 +0000224** by subsequent calls to sqlite*ListAppend() routines.
drhff78bd22002-02-27 01:47:11 +0000225**
drhad3cab52002-05-24 02:04:32 +0000226** Any tables that the SrcList might point to are not duplicated.
drhff78bd22002-02-27 01:47:11 +0000227*/
danielk19774adee202004-05-08 08:23:19 +0000228Expr *sqlite3ExprDup(Expr *p){
drhff78bd22002-02-27 01:47:11 +0000229 Expr *pNew;
230 if( p==0 ) return 0;
drhfcb78a42003-01-18 20:11:05 +0000231 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000232 if( pNew==0 ) return 0;
drh3b167c72002-06-28 12:18:47 +0000233 memcpy(pNew, p, sizeof(*pNew));
drh6977fea2002-10-22 23:38:04 +0000234 if( p->token.z!=0 ){
drh4b59ab52002-08-24 18:24:51 +0000235 pNew->token.z = sqliteStrDup(p->token.z);
236 pNew->token.dyn = 1;
237 }else{
drh4efc4752004-01-16 15:55:37 +0000238 assert( pNew->token.z==0 );
drh4b59ab52002-08-24 18:24:51 +0000239 }
drh6977fea2002-10-22 23:38:04 +0000240 pNew->span.z = 0;
danielk19774adee202004-05-08 08:23:19 +0000241 pNew->pLeft = sqlite3ExprDup(p->pLeft);
242 pNew->pRight = sqlite3ExprDup(p->pRight);
243 pNew->pList = sqlite3ExprListDup(p->pList);
244 pNew->pSelect = sqlite3SelectDup(p->pSelect);
drhff78bd22002-02-27 01:47:11 +0000245 return pNew;
246}
danielk19774adee202004-05-08 08:23:19 +0000247void sqlite3TokenCopy(Token *pTo, Token *pFrom){
drh4b59ab52002-08-24 18:24:51 +0000248 if( pTo->dyn ) sqliteFree((char*)pTo->z);
drh4b59ab52002-08-24 18:24:51 +0000249 if( pFrom->z ){
250 pTo->n = pFrom->n;
251 pTo->z = sqliteStrNDup(pFrom->z, pFrom->n);
252 pTo->dyn = 1;
253 }else{
drh4b59ab52002-08-24 18:24:51 +0000254 pTo->z = 0;
drh4b59ab52002-08-24 18:24:51 +0000255 }
256}
danielk19774adee202004-05-08 08:23:19 +0000257ExprList *sqlite3ExprListDup(ExprList *p){
drhff78bd22002-02-27 01:47:11 +0000258 ExprList *pNew;
drh3e7bc9c2004-02-21 19:17:17 +0000259 struct ExprList_item *pItem;
drhff78bd22002-02-27 01:47:11 +0000260 int i;
261 if( p==0 ) return 0;
262 pNew = sqliteMalloc( sizeof(*pNew) );
263 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000264 pNew->nExpr = pNew->nAlloc = p->nExpr;
drh3e7bc9c2004-02-21 19:17:17 +0000265 pNew->a = pItem = sqliteMalloc( p->nExpr*sizeof(p->a[0]) );
drh1bdd9b52004-04-23 17:04:44 +0000266 if( pItem==0 ) return 0; /* Leaks memory after a malloc failure */
267 for(i=0; i<p->nExpr; i++, pItem++){
drh4b59ab52002-08-24 18:24:51 +0000268 Expr *pNewExpr, *pOldExpr;
danielk19774adee202004-05-08 08:23:19 +0000269 pItem->pExpr = pNewExpr = sqlite3ExprDup(pOldExpr = p->a[i].pExpr);
drh6977fea2002-10-22 23:38:04 +0000270 if( pOldExpr->span.z!=0 && pNewExpr ){
271 /* Always make a copy of the span for top-level expressions in the
drh4b59ab52002-08-24 18:24:51 +0000272 ** expression list. The logic in SELECT processing that determines
273 ** the names of columns in the result set needs this information */
danielk19774adee202004-05-08 08:23:19 +0000274 sqlite3TokenCopy(&pNewExpr->span, &pOldExpr->span);
drh4b59ab52002-08-24 18:24:51 +0000275 }
drh1f3e9052002-10-31 00:09:39 +0000276 assert( pNewExpr==0 || pNewExpr->span.z!=0
danielk197724b03fd2004-05-10 10:34:34 +0000277 || pOldExpr->span.z==0 || sqlite3_malloc_failed );
drh3e7bc9c2004-02-21 19:17:17 +0000278 pItem->zName = sqliteStrDup(p->a[i].zName);
279 pItem->sortOrder = p->a[i].sortOrder;
280 pItem->isAgg = p->a[i].isAgg;
281 pItem->done = 0;
drhff78bd22002-02-27 01:47:11 +0000282 }
283 return pNew;
284}
danielk19774adee202004-05-08 08:23:19 +0000285SrcList *sqlite3SrcListDup(SrcList *p){
drhad3cab52002-05-24 02:04:32 +0000286 SrcList *pNew;
287 int i;
drh113088e2003-03-20 01:16:58 +0000288 int nByte;
drhad3cab52002-05-24 02:04:32 +0000289 if( p==0 ) return 0;
drh113088e2003-03-20 01:16:58 +0000290 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
drh4efc4752004-01-16 15:55:37 +0000291 pNew = sqliteMallocRaw( nByte );
drhad3cab52002-05-24 02:04:32 +0000292 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000293 pNew->nSrc = pNew->nAlloc = p->nSrc;
drhad3cab52002-05-24 02:04:32 +0000294 for(i=0; i<p->nSrc; i++){
drh4efc4752004-01-16 15:55:37 +0000295 struct SrcList_item *pNewItem = &pNew->a[i];
296 struct SrcList_item *pOldItem = &p->a[i];
297 pNewItem->zDatabase = sqliteStrDup(pOldItem->zDatabase);
298 pNewItem->zName = sqliteStrDup(pOldItem->zName);
299 pNewItem->zAlias = sqliteStrDup(pOldItem->zAlias);
300 pNewItem->jointype = pOldItem->jointype;
301 pNewItem->iCursor = pOldItem->iCursor;
302 pNewItem->pTab = 0;
danielk19774adee202004-05-08 08:23:19 +0000303 pNewItem->pSelect = sqlite3SelectDup(pOldItem->pSelect);
304 pNewItem->pOn = sqlite3ExprDup(pOldItem->pOn);
305 pNewItem->pUsing = sqlite3IdListDup(pOldItem->pUsing);
drhad3cab52002-05-24 02:04:32 +0000306 }
307 return pNew;
308}
danielk19774adee202004-05-08 08:23:19 +0000309IdList *sqlite3IdListDup(IdList *p){
drhff78bd22002-02-27 01:47:11 +0000310 IdList *pNew;
311 int i;
312 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000313 pNew = sqliteMallocRaw( sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +0000314 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000315 pNew->nId = pNew->nAlloc = p->nId;
drh4efc4752004-01-16 15:55:37 +0000316 pNew->a = sqliteMallocRaw( p->nId*sizeof(p->a[0]) );
drhe4697f52002-05-23 02:09:03 +0000317 if( pNew->a==0 ) return 0;
drhff78bd22002-02-27 01:47:11 +0000318 for(i=0; i<p->nId; i++){
drh4efc4752004-01-16 15:55:37 +0000319 struct IdList_item *pNewItem = &pNew->a[i];
320 struct IdList_item *pOldItem = &p->a[i];
321 pNewItem->zName = sqliteStrDup(pOldItem->zName);
322 pNewItem->idx = pOldItem->idx;
drhff78bd22002-02-27 01:47:11 +0000323 }
324 return pNew;
325}
danielk19774adee202004-05-08 08:23:19 +0000326Select *sqlite3SelectDup(Select *p){
drhff78bd22002-02-27 01:47:11 +0000327 Select *pNew;
328 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000329 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000330 if( pNew==0 ) return 0;
331 pNew->isDistinct = p->isDistinct;
danielk19774adee202004-05-08 08:23:19 +0000332 pNew->pEList = sqlite3ExprListDup(p->pEList);
333 pNew->pSrc = sqlite3SrcListDup(p->pSrc);
334 pNew->pWhere = sqlite3ExprDup(p->pWhere);
335 pNew->pGroupBy = sqlite3ExprListDup(p->pGroupBy);
336 pNew->pHaving = sqlite3ExprDup(p->pHaving);
337 pNew->pOrderBy = sqlite3ExprListDup(p->pOrderBy);
drhff78bd22002-02-27 01:47:11 +0000338 pNew->op = p->op;
danielk19774adee202004-05-08 08:23:19 +0000339 pNew->pPrior = sqlite3SelectDup(p->pPrior);
drhff78bd22002-02-27 01:47:11 +0000340 pNew->nLimit = p->nLimit;
341 pNew->nOffset = p->nOffset;
342 pNew->zSelect = 0;
drh7b58dae2003-07-20 01:16:46 +0000343 pNew->iLimit = -1;
344 pNew->iOffset = -1;
drhff78bd22002-02-27 01:47:11 +0000345 return pNew;
346}
347
348
349/*
drha76b5df2002-02-23 02:32:10 +0000350** Add a new element to the end of an expression list. If pList is
351** initially NULL, then create a new expression list.
352*/
danielk19774adee202004-05-08 08:23:19 +0000353ExprList *sqlite3ExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){
drha76b5df2002-02-23 02:32:10 +0000354 if( pList==0 ){
355 pList = sqliteMalloc( sizeof(ExprList) );
356 if( pList==0 ){
danielk19774adee202004-05-08 08:23:19 +0000357 /* sqlite3ExprDelete(pExpr); // Leak memory if malloc fails */
drha76b5df2002-02-23 02:32:10 +0000358 return 0;
359 }
drh4efc4752004-01-16 15:55:37 +0000360 assert( pList->nAlloc==0 );
drha76b5df2002-02-23 02:32:10 +0000361 }
drh4305d102003-07-30 12:34:12 +0000362 if( pList->nAlloc<=pList->nExpr ){
drh4305d102003-07-30 12:34:12 +0000363 pList->nAlloc = pList->nAlloc*2 + 4;
drh4efc4752004-01-16 15:55:37 +0000364 pList->a = sqliteRealloc(pList->a, pList->nAlloc*sizeof(pList->a[0]));
365 if( pList->a==0 ){
danielk19774adee202004-05-08 08:23:19 +0000366 /* sqlite3ExprDelete(pExpr); // Leak memory if malloc fails */
drh4efc4752004-01-16 15:55:37 +0000367 pList->nExpr = pList->nAlloc = 0;
drha76b5df2002-02-23 02:32:10 +0000368 return pList;
369 }
drha76b5df2002-02-23 02:32:10 +0000370 }
drh4efc4752004-01-16 15:55:37 +0000371 assert( pList->a!=0 );
372 if( pExpr || pName ){
373 struct ExprList_item *pItem = &pList->a[pList->nExpr++];
374 memset(pItem, 0, sizeof(*pItem));
375 pItem->pExpr = pExpr;
drha76b5df2002-02-23 02:32:10 +0000376 if( pName ){
danielk19774adee202004-05-08 08:23:19 +0000377 sqlite3SetNString(&pItem->zName, pName->z, pName->n, 0);
378 sqlite3Dequote(pItem->zName);
drha76b5df2002-02-23 02:32:10 +0000379 }
380 }
381 return pList;
382}
383
384/*
385** Delete an entire expression list.
386*/
danielk19774adee202004-05-08 08:23:19 +0000387void sqlite3ExprListDelete(ExprList *pList){
drha76b5df2002-02-23 02:32:10 +0000388 int i;
389 if( pList==0 ) return;
drh1bdd9b52004-04-23 17:04:44 +0000390 assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
391 assert( pList->nExpr<=pList->nAlloc );
drha76b5df2002-02-23 02:32:10 +0000392 for(i=0; i<pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +0000393 sqlite3ExprDelete(pList->a[i].pExpr);
drha76b5df2002-02-23 02:32:10 +0000394 sqliteFree(pList->a[i].zName);
395 }
396 sqliteFree(pList->a);
397 sqliteFree(pList);
398}
399
400/*
drhfef52082000-06-06 01:50:43 +0000401** Walk an expression tree. Return 1 if the expression is constant
402** and 0 if it involves variables.
drh23989372002-05-21 13:43:04 +0000403**
404** For the purposes of this function, a double-quoted string (ex: "abc")
405** is considered a variable but a single-quoted string (ex: 'abc') is
406** a constant.
drhfef52082000-06-06 01:50:43 +0000407*/
danielk19774adee202004-05-08 08:23:19 +0000408int sqlite3ExprIsConstant(Expr *p){
drhfef52082000-06-06 01:50:43 +0000409 switch( p->op ){
410 case TK_ID:
drh967e8b72000-06-21 13:59:10 +0000411 case TK_COLUMN:
drhfef52082000-06-06 01:50:43 +0000412 case TK_DOT:
drh7bdc0c12003-04-19 17:27:24 +0000413 case TK_FUNCTION:
drhfef52082000-06-06 01:50:43 +0000414 return 0;
drh7bdc0c12003-04-19 17:27:24 +0000415 case TK_NULL:
drh23989372002-05-21 13:43:04 +0000416 case TK_STRING:
drh92086432002-01-22 14:11:29 +0000417 case TK_INTEGER:
418 case TK_FLOAT:
drh50457892003-09-06 01:10:47 +0000419 case TK_VARIABLE:
drh92086432002-01-22 14:11:29 +0000420 return 1;
drhfef52082000-06-06 01:50:43 +0000421 default: {
danielk19774adee202004-05-08 08:23:19 +0000422 if( p->pLeft && !sqlite3ExprIsConstant(p->pLeft) ) return 0;
423 if( p->pRight && !sqlite3ExprIsConstant(p->pRight) ) return 0;
drhfef52082000-06-06 01:50:43 +0000424 if( p->pList ){
425 int i;
426 for(i=0; i<p->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +0000427 if( !sqlite3ExprIsConstant(p->pList->a[i].pExpr) ) return 0;
drhfef52082000-06-06 01:50:43 +0000428 }
429 }
drh92086432002-01-22 14:11:29 +0000430 return p->pLeft!=0 || p->pRight!=0 || (p->pList && p->pList->nExpr>0);
drhfef52082000-06-06 01:50:43 +0000431 }
432 }
drh92086432002-01-22 14:11:29 +0000433 return 0;
drhfef52082000-06-06 01:50:43 +0000434}
435
436/*
drh202b2df2004-01-06 01:13:46 +0000437** If the given expression codes a constant integer that is small enough
438** to fit in a 32-bit integer, return 1 and put the value of the integer
439** in *pValue. If the expression is not an integer or if it is too big
440** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
drhe4de1fe2002-06-02 16:09:01 +0000441*/
danielk19774adee202004-05-08 08:23:19 +0000442int sqlite3ExprIsInteger(Expr *p, int *pValue){
drhe4de1fe2002-06-02 16:09:01 +0000443 switch( p->op ){
444 case TK_INTEGER: {
danielk19774adee202004-05-08 08:23:19 +0000445 if( sqlite3FitsIn32Bits(p->token.z) ){
drh202b2df2004-01-06 01:13:46 +0000446 *pValue = atoi(p->token.z);
447 return 1;
448 }
449 break;
drhe4de1fe2002-06-02 16:09:01 +0000450 }
451 case TK_STRING: {
drhbd790ee2002-06-02 18:22:06 +0000452 const char *z = p->token.z;
drhe4de1fe2002-06-02 16:09:01 +0000453 int n = p->token.n;
drhbd790ee2002-06-02 18:22:06 +0000454 if( n>0 && z[0]=='-' ){ z++; n--; }
drhe4de1fe2002-06-02 16:09:01 +0000455 while( n>0 && *z && isdigit(*z) ){ z++; n--; }
danielk19774adee202004-05-08 08:23:19 +0000456 if( n==0 && sqlite3FitsIn32Bits(p->token.z) ){
drhe4de1fe2002-06-02 16:09:01 +0000457 *pValue = atoi(p->token.z);
458 return 1;
459 }
460 break;
461 }
drh4b59ab52002-08-24 18:24:51 +0000462 case TK_UPLUS: {
danielk19774adee202004-05-08 08:23:19 +0000463 return sqlite3ExprIsInteger(p->pLeft, pValue);
drh4b59ab52002-08-24 18:24:51 +0000464 }
drhe4de1fe2002-06-02 16:09:01 +0000465 case TK_UMINUS: {
466 int v;
danielk19774adee202004-05-08 08:23:19 +0000467 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
drhe4de1fe2002-06-02 16:09:01 +0000468 *pValue = -v;
469 return 1;
470 }
471 break;
472 }
473 default: break;
474 }
475 return 0;
476}
477
478/*
drhc4a3c772001-04-04 11:48:57 +0000479** Return TRUE if the given string is a row-id column name.
480*/
danielk19774adee202004-05-08 08:23:19 +0000481int sqlite3IsRowid(const char *z){
482 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
483 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
484 if( sqlite3StrICmp(z, "OID")==0 ) return 1;
drhc4a3c772001-04-04 11:48:57 +0000485 return 0;
486}
487
488/*
drh8141f612004-01-25 22:44:58 +0000489** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
490** that name in the set of source tables in pSrcList and make the pExpr
491** expression node refer back to that source column. The following changes
492** are made to pExpr:
493**
494** pExpr->iDb Set the index in db->aDb[] of the database holding
495** the table.
496** pExpr->iTable Set to the cursor number for the table obtained
497** from pSrcList.
498** pExpr->iColumn Set to the column number within the table.
499** pExpr->dataType Set to the appropriate data type for the column.
500** pExpr->op Set to TK_COLUMN.
501** pExpr->pLeft Any expression this points to is deleted
502** pExpr->pRight Any expression this points to is deleted.
503**
504** The pDbToken is the name of the database (the "X"). This value may be
505** NULL meaning that name is of the form Y.Z or Z. Any available database
506** can be used. The pTableToken is the name of the table (the "Y"). This
507** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it
508** means that the form of the name is Z and that columns from any table
509** can be used.
510**
511** If the name cannot be resolved unambiguously, leave an error message
512** in pParse and return non-zero. Return zero on success.
513*/
514static int lookupName(
515 Parse *pParse, /* The parsing context */
516 Token *pDbToken, /* Name of the database containing table, or NULL */
517 Token *pTableToken, /* Name of table containing column, or NULL */
518 Token *pColumnToken, /* Name of the column. */
519 SrcList *pSrcList, /* List of tables used to resolve column names */
520 ExprList *pEList, /* List of expressions used to resolve "AS" */
521 Expr *pExpr /* Make this EXPR node point to the selected column */
522){
523 char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */
524 char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */
525 char *zCol = 0; /* Name of the column. The "Z" */
526 int i, j; /* Loop counters */
527 int cnt = 0; /* Number of matching column names */
528 int cntTab = 0; /* Number of matching table names */
drh7e26d752004-02-11 10:35:29 +0000529 sqlite *db = pParse->db; /* The database */
drh8141f612004-01-25 22:44:58 +0000530
531 assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
532 if( pDbToken && pDbToken->z ){
533 zDb = sqliteStrNDup(pDbToken->z, pDbToken->n);
danielk19774adee202004-05-08 08:23:19 +0000534 sqlite3Dequote(zDb);
drh8141f612004-01-25 22:44:58 +0000535 }else{
536 zDb = 0;
537 }
538 if( pTableToken && pTableToken->z ){
539 zTab = sqliteStrNDup(pTableToken->z, pTableToken->n);
danielk19774adee202004-05-08 08:23:19 +0000540 sqlite3Dequote(zTab);
drh8141f612004-01-25 22:44:58 +0000541 }else{
542 assert( zDb==0 );
543 zTab = 0;
544 }
545 zCol = sqliteStrNDup(pColumnToken->z, pColumnToken->n);
danielk19774adee202004-05-08 08:23:19 +0000546 sqlite3Dequote(zCol);
danielk197724b03fd2004-05-10 10:34:34 +0000547 if( sqlite3_malloc_failed ){
drh8141f612004-01-25 22:44:58 +0000548 return 1; /* Leak memory (zDb and zTab) if malloc fails */
549 }
550 assert( zTab==0 || pEList==0 );
551
552 pExpr->iTable = -1;
553 for(i=0; i<pSrcList->nSrc; i++){
554 struct SrcList_item *pItem = &pSrcList->a[i];
555 Table *pTab = pItem->pTab;
556 Column *pCol;
557
558 if( pTab==0 ) continue;
559 assert( pTab->nCol>0 );
560 if( zTab ){
561 if( pItem->zAlias ){
562 char *zTabName = pItem->zAlias;
danielk19774adee202004-05-08 08:23:19 +0000563 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
drh8141f612004-01-25 22:44:58 +0000564 }else{
565 char *zTabName = pTab->zName;
danielk19774adee202004-05-08 08:23:19 +0000566 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
567 if( zDb!=0 && sqlite3StrICmp(db->aDb[pTab->iDb].zName, zDb)!=0 ){
drh8141f612004-01-25 22:44:58 +0000568 continue;
569 }
570 }
571 }
572 if( 0==(cntTab++) ){
573 pExpr->iTable = pItem->iCursor;
574 pExpr->iDb = pTab->iDb;
575 }
576 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
danielk19774adee202004-05-08 08:23:19 +0000577 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
drh8141f612004-01-25 22:44:58 +0000578 cnt++;
579 pExpr->iTable = pItem->iCursor;
580 pExpr->iDb = pTab->iDb;
581 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
582 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
danielk1977a37cdde2004-05-16 11:15:36 +0000583 pExpr->affinity = pTab->aCol[j].affinity;
584
585 /* FIX ME: Expr::dataType will be removed... */
586 pExpr->dataType =
587 (pCol->affinity==SQLITE_AFF_TEXT?SQLITE_SO_TEXT:SQLITE_SO_NUM);
drh8141f612004-01-25 22:44:58 +0000588 break;
589 }
590 }
591 }
592
593 /* If we have not already resolved the name, then maybe
594 ** it is a new.* or old.* trigger argument reference
595 */
596 if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
597 TriggerStack *pTriggerStack = pParse->trigStack;
598 Table *pTab = 0;
danielk19774adee202004-05-08 08:23:19 +0000599 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){
drh8141f612004-01-25 22:44:58 +0000600 pExpr->iTable = pTriggerStack->newIdx;
601 assert( pTriggerStack->pTab );
602 pTab = pTriggerStack->pTab;
danielk19774adee202004-05-08 08:23:19 +0000603 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab) == 0 ){
drh8141f612004-01-25 22:44:58 +0000604 pExpr->iTable = pTriggerStack->oldIdx;
605 assert( pTriggerStack->pTab );
606 pTab = pTriggerStack->pTab;
607 }
608
609 if( pTab ){
610 int j;
611 Column *pCol = pTab->aCol;
612
613 pExpr->iDb = pTab->iDb;
614 cntTab++;
615 for(j=0; j < pTab->nCol; j++, pCol++) {
danielk19774adee202004-05-08 08:23:19 +0000616 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
drh8141f612004-01-25 22:44:58 +0000617 cnt++;
618 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
danielk1977a37cdde2004-05-16 11:15:36 +0000619 pExpr->affinity = pTab->aCol[j].affinity;
620 /* FIX ME: Expr::dataType will be removed... */
621 pExpr->dataType =
622 (pCol->affinity==SQLITE_AFF_TEXT?SQLITE_SO_TEXT:SQLITE_SO_NUM);
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;
635 pExpr->dataType = SQLITE_SO_NUM;
danielk1977a37cdde2004-05-16 11:15:36 +0000636 pExpr->affinity = SQLITE_AFF_INTEGER;
drh8141f612004-01-25 22:44:58 +0000637 }
638
639 /*
640 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
641 ** might refer to an result-set alias. This happens, for example, when
642 ** we are resolving names in the WHERE clause of the following command:
643 **
644 ** SELECT a+b AS x FROM table WHERE x<10;
645 **
646 ** In cases like this, replace pExpr with a copy of the expression that
647 ** forms the result set entry ("a+b" in the example) and return immediately.
648 ** Note that the expression in the result set should have already been
649 ** resolved by the time the WHERE clause is resolved.
650 */
651 if( cnt==0 && pEList!=0 ){
652 for(j=0; j<pEList->nExpr; j++){
653 char *zAs = pEList->a[j].zName;
danielk19774adee202004-05-08 08:23:19 +0000654 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
drh8141f612004-01-25 22:44:58 +0000655 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
656 pExpr->op = TK_AS;
657 pExpr->iColumn = j;
danielk19774adee202004-05-08 08:23:19 +0000658 pExpr->pLeft = sqlite3ExprDup(pEList->a[j].pExpr);
drh8141f612004-01-25 22:44:58 +0000659 sqliteFree(zCol);
660 assert( zTab==0 && zDb==0 );
661 return 0;
662 }
663 }
664 }
665
666 /*
667 ** If X and Y are NULL (in other words if only the column name Z is
668 ** supplied) and the value of Z is enclosed in double-quotes, then
669 ** Z is a string literal if it doesn't match any column names. In that
670 ** case, we need to return right away and not make any changes to
671 ** pExpr.
672 */
673 if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
674 sqliteFree(zCol);
675 return 0;
676 }
677
678 /*
679 ** cnt==0 means there was not match. cnt>1 means there were two or
680 ** more matches. Either way, we have an error.
681 */
682 if( cnt!=1 ){
683 char *z = 0;
684 char *zErr;
685 zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s";
686 if( zDb ){
danielk19774adee202004-05-08 08:23:19 +0000687 sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, 0);
drh8141f612004-01-25 22:44:58 +0000688 }else if( zTab ){
danielk19774adee202004-05-08 08:23:19 +0000689 sqlite3SetString(&z, zTab, ".", zCol, 0);
drh8141f612004-01-25 22:44:58 +0000690 }else{
691 z = sqliteStrDup(zCol);
692 }
danielk19774adee202004-05-08 08:23:19 +0000693 sqlite3ErrorMsg(pParse, zErr, z);
drh8141f612004-01-25 22:44:58 +0000694 sqliteFree(z);
695 }
696
697 /* Clean up and return
698 */
699 sqliteFree(zDb);
700 sqliteFree(zTab);
701 sqliteFree(zCol);
danielk19774adee202004-05-08 08:23:19 +0000702 sqlite3ExprDelete(pExpr->pLeft);
drh8141f612004-01-25 22:44:58 +0000703 pExpr->pLeft = 0;
danielk19774adee202004-05-08 08:23:19 +0000704 sqlite3ExprDelete(pExpr->pRight);
drh8141f612004-01-25 22:44:58 +0000705 pExpr->pRight = 0;
706 pExpr->op = TK_COLUMN;
danielk19774adee202004-05-08 08:23:19 +0000707 sqlite3AuthRead(pParse, pExpr, pSrcList);
drh8141f612004-01-25 22:44:58 +0000708 return cnt!=1;
709}
710
711/*
drhcce7d172000-05-31 15:34:51 +0000712** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +0000713** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +0000714** index to the table in the table list and a column offset. The
715** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
716** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +0000717** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +0000718** VDBE cursor number for a cursor that is pointing into the referenced
719** table. The Expr.iColumn value is changed to the index of the column
720** of the referenced table. The Expr.iColumn value for the special
721** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
722** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +0000723**
drhfef52082000-06-06 01:50:43 +0000724** We also check for instances of the IN operator. IN comes in two
725** forms:
726**
727** expr IN (exprlist)
728** and
729** expr IN (SELECT ...)
730**
731** The first form is handled by creating a set holding the list
732** of allowed values. The second form causes the SELECT to generate
733** a temporary table.
734**
735** This routine also looks for scalar SELECTs that are part of an expression.
drh19a775c2000-06-05 18:54:46 +0000736** If it finds any, it generates code to write the value of that select
737** into a memory cell.
drhcce7d172000-05-31 15:34:51 +0000738**
drh967e8b72000-06-21 13:59:10 +0000739** Unknown columns or tables provoke an error. The function returns
drhcce7d172000-05-31 15:34:51 +0000740** the number of errors seen and leaves an error message on pParse->zErrMsg.
741*/
danielk19774adee202004-05-08 08:23:19 +0000742int sqlite3ExprResolveIds(
drha2e00042002-01-22 03:13:42 +0000743 Parse *pParse, /* The parser context */
drh8141f612004-01-25 22:44:58 +0000744 SrcList *pSrcList, /* List of tables used to resolve column names */
drha2e00042002-01-22 03:13:42 +0000745 ExprList *pEList, /* List of expressions used to resolve "AS" */
746 Expr *pExpr /* The expression to be analyzed. */
747){
drh6a3ea0e2003-05-02 14:32:12 +0000748 int i;
749
drh8141f612004-01-25 22:44:58 +0000750 if( pExpr==0 || pSrcList==0 ) return 0;
751 for(i=0; i<pSrcList->nSrc; i++){
752 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab );
drh6a3ea0e2003-05-02 14:32:12 +0000753 }
drhcce7d172000-05-31 15:34:51 +0000754 switch( pExpr->op ){
drh23989372002-05-21 13:43:04 +0000755 /* Double-quoted strings (ex: "abc") are used as identifiers if
756 ** possible. Otherwise they remain as strings. Single-quoted
757 ** strings (ex: 'abc') are always string literals.
758 */
759 case TK_STRING: {
760 if( pExpr->token.z[0]=='\'' ) break;
761 /* Fall thru into the TK_ID case if this is a double-quoted string */
762 }
drh8141f612004-01-25 22:44:58 +0000763 /* A lone identifier is the name of a columnd.
drha2e00042002-01-22 03:13:42 +0000764 */
drhcce7d172000-05-31 15:34:51 +0000765 case TK_ID: {
drh8141f612004-01-25 22:44:58 +0000766 if( lookupName(pParse, 0, 0, &pExpr->token, pSrcList, pEList, pExpr) ){
drhcce7d172000-05-31 15:34:51 +0000767 return 1;
drhed6c8672003-01-12 18:02:16 +0000768 }
drhcce7d172000-05-31 15:34:51 +0000769 break;
770 }
771
drhd24cc422003-03-27 12:51:24 +0000772 /* A table name and column name: ID.ID
773 ** Or a database, table and column: ID.ID.ID
774 */
drhcce7d172000-05-31 15:34:51 +0000775 case TK_DOT: {
drh8141f612004-01-25 22:44:58 +0000776 Token *pColumn;
777 Token *pTable;
778 Token *pDb;
779 Expr *pRight;
drhcce7d172000-05-31 15:34:51 +0000780
drhcce7d172000-05-31 15:34:51 +0000781 pRight = pExpr->pRight;
drhd24cc422003-03-27 12:51:24 +0000782 if( pRight->op==TK_ID ){
drh8141f612004-01-25 22:44:58 +0000783 pDb = 0;
784 pTable = &pExpr->pLeft->token;
785 pColumn = &pRight->token;
drhd24cc422003-03-27 12:51:24 +0000786 }else{
drh8141f612004-01-25 22:44:58 +0000787 assert( pRight->op==TK_DOT );
788 pDb = &pExpr->pLeft->token;
789 pTable = &pRight->pLeft->token;
790 pColumn = &pRight->pRight->token;
drhd24cc422003-03-27 12:51:24 +0000791 }
drh8141f612004-01-25 22:44:58 +0000792 if( lookupName(pParse, pDb, pTable, pColumn, pSrcList, 0, pExpr) ){
drhdaffd0e2001-04-11 14:28:42 +0000793 return 1;
794 }
drhcce7d172000-05-31 15:34:51 +0000795 break;
796 }
797
drhfef52082000-06-06 01:50:43 +0000798 case TK_IN: {
danielk1977e014a832004-05-17 10:48:57 +0000799 char affinity;
danielk19774adee202004-05-08 08:23:19 +0000800 Vdbe *v = sqlite3GetVdbe(pParse);
drhfef52082000-06-06 01:50:43 +0000801 if( v==0 ) return 1;
danielk19774adee202004-05-08 08:23:19 +0000802 if( sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pLeft) ){
drhcfab11b2000-06-06 03:31:22 +0000803 return 1;
804 }
danielk1977bf3b7212004-05-18 10:06:24 +0000805 affinity = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +0000806
807 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
808 ** expression it is handled the same way. A temporary table is
809 ** filled with single-field index keys representing the results
810 ** from the SELECT or the <exprlist>.
811 **
812 ** If the 'x' expression is a column value, or the SELECT...
813 ** statement returns a column value, then the affinity of that
814 ** column is used to build the index keys. If both 'x' and the
815 ** SELECT... statement are columns, then numeric affinity is used
816 ** if either column has NUMERIC or INTEGER affinity. If neither
817 ** 'x' nor the SELECT... statement are columns, then numeric affinity
818 ** is used.
819 */
820 pExpr->iTable = pParse->nTab++;
821 sqlite3VdbeAddOp(v, OP_OpenTemp, pExpr->iTable, 1);
822
drhfef52082000-06-06 01:50:43 +0000823 if( pExpr->pSelect ){
824 /* Case 1: expr IN (SELECT ...)
825 **
danielk1977e014a832004-05-17 10:48:57 +0000826 ** Generate code to write the results of the select into the temporary
827 ** table allocated and opened above.
drhfef52082000-06-06 01:50:43 +0000828 */
danielk1977e014a832004-05-17 10:48:57 +0000829 int iParm = pExpr->iTable + (((int)affinity)<<16);
830 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
danielk1977bf3b7212004-05-18 10:06:24 +0000831 sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0);
drhfef52082000-06-06 01:50:43 +0000832 }else if( pExpr->pList ){
833 /* Case 2: expr IN (exprlist)
834 **
danielk1977e014a832004-05-17 10:48:57 +0000835 ** For each expression, build an index key from the evaluation and
836 ** store it in the temporary table. If <expr> is a column, then use
837 ** that columns affinity when building index keys. If <expr> is not
838 ** a column, use numeric affinity.
drhfef52082000-06-06 01:50:43 +0000839 */
danielk1977e014a832004-05-17 10:48:57 +0000840 int i;
841 char const *affStr;
842 if( !affinity ){
843 affinity = SQLITE_AFF_NUMERIC;
844 }
845 affStr = sqlite3AffinityString(affinity);
846
847 /* Loop through each expression in <exprlist>. */
drhfef52082000-06-06 01:50:43 +0000848 for(i=0; i<pExpr->pList->nExpr; i++){
849 Expr *pE2 = pExpr->pList->a[i].pExpr;
danielk1977e014a832004-05-17 10:48:57 +0000850
851 /* Check that the expression is constant and valid. */
danielk19774adee202004-05-08 08:23:19 +0000852 if( !sqlite3ExprIsConstant(pE2) ){
853 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +0000854 "right-hand side of IN operator must be constant");
drhfef52082000-06-06 01:50:43 +0000855 return 1;
856 }
danielk19774adee202004-05-08 08:23:19 +0000857 if( sqlite3ExprCheck(pParse, pE2, 0, 0) ){
drh4794b982000-06-06 13:54:14 +0000858 return 1;
859 }
danielk1977e014a832004-05-17 10:48:57 +0000860
861 /* Evaluate the expression and insert it into the temp table */
862 sqlite3ExprCode(pParse, pE2);
863 sqlite3VdbeOp3(v, OP_MakeKey, 1, 0, affStr, P3_STATIC);
864 sqlite3VdbeAddOp(v, OP_String, 0, 0);
865 sqlite3VdbeAddOp(v, OP_PutStrKey, pExpr->iTable, 0);
drhfef52082000-06-06 01:50:43 +0000866 }
867 }
drhcfab11b2000-06-06 03:31:22 +0000868 break;
drhfef52082000-06-06 01:50:43 +0000869 }
870
drh19a775c2000-06-05 18:54:46 +0000871 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +0000872 /* This has to be a scalar SELECT. Generate code to put the
873 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +0000874 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +0000875 */
drh967e8b72000-06-21 13:59:10 +0000876 pExpr->iColumn = pParse->nMem++;
danielk1977bf3b7212004-05-18 10:06:24 +0000877 if(sqlite3Select(pParse, pExpr->pSelect, SRT_Mem,pExpr->iColumn,0,0,0,0)){
drh19a775c2000-06-05 18:54:46 +0000878 return 1;
879 }
880 break;
881 }
882
drhcce7d172000-05-31 15:34:51 +0000883 /* For all else, just recursively walk the tree */
884 default: {
drh4794b982000-06-06 13:54:14 +0000885 if( pExpr->pLeft
danielk19774adee202004-05-08 08:23:19 +0000886 && sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pLeft) ){
drhcce7d172000-05-31 15:34:51 +0000887 return 1;
888 }
889 if( pExpr->pRight
danielk19774adee202004-05-08 08:23:19 +0000890 && sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pRight) ){
drhcce7d172000-05-31 15:34:51 +0000891 return 1;
892 }
893 if( pExpr->pList ){
894 int i;
895 ExprList *pList = pExpr->pList;
896 for(i=0; i<pList->nExpr; i++){
drh832508b2002-03-02 17:04:07 +0000897 Expr *pArg = pList->a[i].pExpr;
danielk19774adee202004-05-08 08:23:19 +0000898 if( sqlite3ExprResolveIds(pParse, pSrcList, pEList, pArg) ){
drhcce7d172000-05-31 15:34:51 +0000899 return 1;
900 }
901 }
902 }
903 }
904 }
905 return 0;
906}
907
drhcce7d172000-05-31 15:34:51 +0000908/*
drh4b59ab52002-08-24 18:24:51 +0000909** pExpr is a node that defines a function of some kind. It might
910** be a syntactic function like "count(x)" or it might be a function
911** that implements an operator, like "a LIKE b".
912**
913** This routine makes *pzName point to the name of the function and
914** *pnName hold the number of characters in the function name.
915*/
916static void getFunctionName(Expr *pExpr, const char **pzName, int *pnName){
917 switch( pExpr->op ){
918 case TK_FUNCTION: {
919 *pzName = pExpr->token.z;
drh6977fea2002-10-22 23:38:04 +0000920 *pnName = pExpr->token.n;
drh4b59ab52002-08-24 18:24:51 +0000921 break;
922 }
923 case TK_LIKE: {
924 *pzName = "like";
925 *pnName = 4;
926 break;
927 }
928 case TK_GLOB: {
929 *pzName = "glob";
930 *pnName = 4;
931 break;
932 }
933 default: {
934 *pzName = "can't happen";
935 *pnName = 12;
936 break;
937 }
938 }
939}
940
941/*
drhcce7d172000-05-31 15:34:51 +0000942** Error check the functions in an expression. Make sure all
943** function names are recognized and all functions have the correct
944** number of arguments. Leave an error message in pParse->zErrMsg
945** if anything is amiss. Return the number of errors.
946**
947** if pIsAgg is not null and this expression is an aggregate function
948** (like count(*) or max(value)) then write a 1 into *pIsAgg.
949*/
danielk19774adee202004-05-08 08:23:19 +0000950int sqlite3ExprCheck(Parse *pParse, Expr *pExpr, int allowAgg, int *pIsAgg){
drhcce7d172000-05-31 15:34:51 +0000951 int nErr = 0;
952 if( pExpr==0 ) return 0;
drhcce7d172000-05-31 15:34:51 +0000953 switch( pExpr->op ){
drh4b59ab52002-08-24 18:24:51 +0000954 case TK_GLOB:
955 case TK_LIKE:
drhcce7d172000-05-31 15:34:51 +0000956 case TK_FUNCTION: {
drhc9b84a12002-06-20 11:36:48 +0000957 int n = pExpr->pList ? pExpr->pList->nExpr : 0; /* Number of arguments */
958 int no_such_func = 0; /* True if no such function exists */
drhc9b84a12002-06-20 11:36:48 +0000959 int wrong_num_args = 0; /* True if wrong number of arguments */
960 int is_agg = 0; /* True if is an aggregate function */
drhcce7d172000-05-31 15:34:51 +0000961 int i;
drh4b59ab52002-08-24 18:24:51 +0000962 int nId; /* Number of characters in function name */
963 const char *zId; /* The function name. */
drh0bce8352002-02-28 00:41:10 +0000964 FuncDef *pDef;
965
drh4b59ab52002-08-24 18:24:51 +0000966 getFunctionName(pExpr, &zId, &nId);
danielk19774adee202004-05-08 08:23:19 +0000967 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, 0);
drh0bce8352002-02-28 00:41:10 +0000968 if( pDef==0 ){
danielk19774adee202004-05-08 08:23:19 +0000969 pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, 0);
drh0bce8352002-02-28 00:41:10 +0000970 if( pDef==0 ){
drh268380c2004-02-25 13:47:31 +0000971 no_such_func = 1;
drh0bce8352002-02-28 00:41:10 +0000972 }else{
973 wrong_num_args = 1;
drhcce7d172000-05-31 15:34:51 +0000974 }
drh0bce8352002-02-28 00:41:10 +0000975 }else{
976 is_agg = pDef->xFunc==0;
drhcce7d172000-05-31 15:34:51 +0000977 }
drh8e0a2f92002-02-23 23:45:45 +0000978 if( is_agg && !allowAgg ){
danielk19774adee202004-05-08 08:23:19 +0000979 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId, zId);
drh8e0a2f92002-02-23 23:45:45 +0000980 nErr++;
981 is_agg = 0;
982 }else if( no_such_func ){
danielk19774adee202004-05-08 08:23:19 +0000983 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
drhcce7d172000-05-31 15:34:51 +0000984 nErr++;
drh8e0a2f92002-02-23 23:45:45 +0000985 }else if( wrong_num_args ){
danielk19774adee202004-05-08 08:23:19 +0000986 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
drhf7a9e1a2004-02-22 18:40:56 +0000987 nId, zId);
drh8e0a2f92002-02-23 23:45:45 +0000988 nErr++;
drhcce7d172000-05-31 15:34:51 +0000989 }
drhf7a9e1a2004-02-22 18:40:56 +0000990 if( is_agg ){
991 pExpr->op = TK_AGG_FUNCTION;
992 if( pIsAgg ) *pIsAgg = 1;
993 }
drhcce7d172000-05-31 15:34:51 +0000994 for(i=0; nErr==0 && i<n; i++){
danielk19774adee202004-05-08 08:23:19 +0000995 nErr = sqlite3ExprCheck(pParse, pExpr->pList->a[i].pExpr,
drh4cfa7932000-06-08 15:10:46 +0000996 allowAgg && !is_agg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000997 }
drhc9b84a12002-06-20 11:36:48 +0000998 if( pDef==0 ){
drh268380c2004-02-25 13:47:31 +0000999 /* Already reported an error */
drhc9b84a12002-06-20 11:36:48 +00001000 }else if( pDef->dataType>=0 ){
1001 if( pDef->dataType<n ){
1002 pExpr->dataType =
danielk19774adee202004-05-08 08:23:19 +00001003 sqlite3ExprType(pExpr->pList->a[pDef->dataType].pExpr);
drhc9b84a12002-06-20 11:36:48 +00001004 }else{
1005 pExpr->dataType = SQLITE_SO_NUM;
1006 }
1007 }else if( pDef->dataType==SQLITE_ARGS ){
1008 pDef->dataType = SQLITE_SO_TEXT;
1009 for(i=0; i<n; i++){
danielk19774adee202004-05-08 08:23:19 +00001010 if( sqlite3ExprType(pExpr->pList->a[i].pExpr)==SQLITE_SO_NUM ){
drhc9b84a12002-06-20 11:36:48 +00001011 pExpr->dataType = SQLITE_SO_NUM;
1012 break;
1013 }
1014 }
1015 }else if( pDef->dataType==SQLITE_NUMERIC ){
1016 pExpr->dataType = SQLITE_SO_NUM;
1017 }else{
1018 pExpr->dataType = SQLITE_SO_TEXT;
1019 }
drhcce7d172000-05-31 15:34:51 +00001020 }
1021 default: {
1022 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001023 nErr = sqlite3ExprCheck(pParse, pExpr->pLeft, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +00001024 }
1025 if( nErr==0 && pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001026 nErr = sqlite3ExprCheck(pParse, pExpr->pRight, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +00001027 }
drhfef52082000-06-06 01:50:43 +00001028 if( nErr==0 && pExpr->pList ){
1029 int n = pExpr->pList->nExpr;
1030 int i;
1031 for(i=0; nErr==0 && i<n; i++){
drh22827922000-06-06 17:27:05 +00001032 Expr *pE2 = pExpr->pList->a[i].pExpr;
danielk19774adee202004-05-08 08:23:19 +00001033 nErr = sqlite3ExprCheck(pParse, pE2, allowAgg, pIsAgg);
drhfef52082000-06-06 01:50:43 +00001034 }
1035 }
drhcce7d172000-05-31 15:34:51 +00001036 break;
1037 }
1038 }
1039 return nErr;
1040}
1041
1042/*
drhc9b84a12002-06-20 11:36:48 +00001043** Return either SQLITE_SO_NUM or SQLITE_SO_TEXT to indicate whether the
1044** given expression should sort as numeric values or as text.
1045**
danielk19774adee202004-05-08 08:23:19 +00001046** The sqlite3ExprResolveIds() and sqlite3ExprCheck() routines must have
drhc9b84a12002-06-20 11:36:48 +00001047** both been called on the expression before it is passed to this routine.
1048*/
danielk19774adee202004-05-08 08:23:19 +00001049int sqlite3ExprType(Expr *p){
drhc9b84a12002-06-20 11:36:48 +00001050 if( p==0 ) return SQLITE_SO_NUM;
1051 while( p ) switch( p->op ){
1052 case TK_PLUS:
1053 case TK_MINUS:
1054 case TK_STAR:
1055 case TK_SLASH:
1056 case TK_AND:
1057 case TK_OR:
1058 case TK_ISNULL:
1059 case TK_NOTNULL:
1060 case TK_NOT:
1061 case TK_UMINUS:
drh4b59ab52002-08-24 18:24:51 +00001062 case TK_UPLUS:
drhc9b84a12002-06-20 11:36:48 +00001063 case TK_BITAND:
1064 case TK_BITOR:
1065 case TK_BITNOT:
1066 case TK_LSHIFT:
1067 case TK_RSHIFT:
1068 case TK_REM:
1069 case TK_INTEGER:
1070 case TK_FLOAT:
1071 case TK_IN:
1072 case TK_BETWEEN:
drh4b59ab52002-08-24 18:24:51 +00001073 case TK_GLOB:
1074 case TK_LIKE:
drhc9b84a12002-06-20 11:36:48 +00001075 return SQLITE_SO_NUM;
1076
1077 case TK_STRING:
1078 case TK_NULL:
1079 case TK_CONCAT:
drh50457892003-09-06 01:10:47 +00001080 case TK_VARIABLE:
drhc9b84a12002-06-20 11:36:48 +00001081 return SQLITE_SO_TEXT;
1082
1083 case TK_LT:
1084 case TK_LE:
1085 case TK_GT:
1086 case TK_GE:
1087 case TK_NE:
1088 case TK_EQ:
danielk19774adee202004-05-08 08:23:19 +00001089 if( sqlite3ExprType(p->pLeft)==SQLITE_SO_NUM ){
drhc9b84a12002-06-20 11:36:48 +00001090 return SQLITE_SO_NUM;
1091 }
1092 p = p->pRight;
1093 break;
1094
1095 case TK_AS:
1096 p = p->pLeft;
1097 break;
1098
1099 case TK_COLUMN:
1100 case TK_FUNCTION:
1101 case TK_AGG_FUNCTION:
1102 return p->dataType;
1103
1104 case TK_SELECT:
1105 assert( p->pSelect );
1106 assert( p->pSelect->pEList );
1107 assert( p->pSelect->pEList->nExpr>0 );
1108 p = p->pSelect->pEList->a[0].pExpr;
1109 break;
1110
drhb1363202002-06-26 02:45:03 +00001111 case TK_CASE: {
danielk19774adee202004-05-08 08:23:19 +00001112 if( p->pRight && sqlite3ExprType(p->pRight)==SQLITE_SO_NUM ){
drhb1363202002-06-26 02:45:03 +00001113 return SQLITE_SO_NUM;
1114 }
1115 if( p->pList ){
1116 int i;
1117 ExprList *pList = p->pList;
1118 for(i=1; i<pList->nExpr; i+=2){
danielk19774adee202004-05-08 08:23:19 +00001119 if( sqlite3ExprType(pList->a[i].pExpr)==SQLITE_SO_NUM ){
drhb1363202002-06-26 02:45:03 +00001120 return SQLITE_SO_NUM;
1121 }
1122 }
1123 }
1124 return SQLITE_SO_TEXT;
1125 }
1126
drhc9b84a12002-06-20 11:36:48 +00001127 default:
1128 assert( p->op==TK_ABORT ); /* Can't Happen */
1129 break;
1130 }
1131 return SQLITE_SO_NUM;
1132}
1133
1134/*
drhcce7d172000-05-31 15:34:51 +00001135** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +00001136** expression and leave the result on the top of stack.
drhcce7d172000-05-31 15:34:51 +00001137*/
danielk19774adee202004-05-08 08:23:19 +00001138void sqlite3ExprCode(Parse *pParse, Expr *pExpr){
drhcce7d172000-05-31 15:34:51 +00001139 Vdbe *v = pParse->pVdbe;
1140 int op;
drhdaffd0e2001-04-11 14:28:42 +00001141 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001142 switch( pExpr->op ){
1143 case TK_PLUS: op = OP_Add; break;
1144 case TK_MINUS: op = OP_Subtract; break;
1145 case TK_STAR: op = OP_Multiply; break;
1146 case TK_SLASH: op = OP_Divide; break;
1147 case TK_AND: op = OP_And; break;
1148 case TK_OR: op = OP_Or; break;
1149 case TK_LT: op = OP_Lt; break;
1150 case TK_LE: op = OP_Le; break;
1151 case TK_GT: op = OP_Gt; break;
1152 case TK_GE: op = OP_Ge; break;
1153 case TK_NE: op = OP_Ne; break;
1154 case TK_EQ: op = OP_Eq; break;
drhcce7d172000-05-31 15:34:51 +00001155 case TK_ISNULL: op = OP_IsNull; break;
1156 case TK_NOTNULL: op = OP_NotNull; break;
1157 case TK_NOT: op = OP_Not; break;
1158 case TK_UMINUS: op = OP_Negative; break;
drhbf4133c2001-10-13 02:59:08 +00001159 case TK_BITAND: op = OP_BitAnd; break;
1160 case TK_BITOR: op = OP_BitOr; break;
1161 case TK_BITNOT: op = OP_BitNot; break;
1162 case TK_LSHIFT: op = OP_ShiftLeft; break;
1163 case TK_RSHIFT: op = OP_ShiftRight; break;
1164 case TK_REM: op = OP_Remainder; break;
drhcce7d172000-05-31 15:34:51 +00001165 default: break;
1166 }
1167 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00001168 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +00001169 if( pParse->useAgg ){
danielk19774adee202004-05-08 08:23:19 +00001170 sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drhc4a3c772001-04-04 11:48:57 +00001171 }else if( pExpr->iColumn>=0 ){
danielk19774adee202004-05-08 08:23:19 +00001172 sqlite3VdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn);
drhc4a3c772001-04-04 11:48:57 +00001173 }else{
danielk19774adee202004-05-08 08:23:19 +00001174 sqlite3VdbeAddOp(v, OP_Recno, pExpr->iTable, 0);
drh22827922000-06-06 17:27:05 +00001175 }
drhcce7d172000-05-31 15:34:51 +00001176 break;
1177 }
drh51e9a442004-01-16 16:42:53 +00001178 case TK_STRING:
1179 case TK_FLOAT:
drhcce7d172000-05-31 15:34:51 +00001180 case TK_INTEGER: {
danielk19774adee202004-05-08 08:23:19 +00001181 if( pExpr->op==TK_INTEGER && sqlite3FitsIn32Bits(pExpr->token.z) ){
1182 sqlite3VdbeAddOp(v, OP_Integer, atoi(pExpr->token.z), 0);
danielk1977f9dd2c22004-05-16 11:57:28 +00001183 }else if( pExpr->op==TK_FLOAT || pExpr->op==TK_INTEGER ){
danielk1977a37cdde2004-05-16 11:15:36 +00001184 sqlite3VdbeAddOp(v, OP_Real, 0, 0);
drh51e9a442004-01-16 16:42:53 +00001185 }else{
danielk19774adee202004-05-08 08:23:19 +00001186 sqlite3VdbeAddOp(v, OP_String, 0, 0);
drhd9e30932002-06-09 01:16:01 +00001187 }
drha76b5df2002-02-23 02:32:10 +00001188 assert( pExpr->token.z );
danielk19774adee202004-05-08 08:23:19 +00001189 sqlite3VdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
1190 sqlite3VdbeDequoteP3(v, -1);
drhcce7d172000-05-31 15:34:51 +00001191 break;
1192 }
1193 case TK_NULL: {
danielk19774adee202004-05-08 08:23:19 +00001194 sqlite3VdbeAddOp(v, OP_String, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001195 break;
1196 }
drh50457892003-09-06 01:10:47 +00001197 case TK_VARIABLE: {
danielk19774adee202004-05-08 08:23:19 +00001198 sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0);
drh50457892003-09-06 01:10:47 +00001199 break;
1200 }
drhc9b84a12002-06-20 11:36:48 +00001201 case TK_LT:
1202 case TK_LE:
1203 case TK_GT:
1204 case TK_GE:
1205 case TK_NE:
1206 case TK_EQ: {
danielk1977a37cdde2004-05-16 11:15:36 +00001207 int p1 = binaryCompareP1(pExpr->pLeft, pExpr->pRight, 0);
1208 sqlite3ExprCode(pParse, pExpr->pLeft);
1209 sqlite3ExprCode(pParse, pExpr->pRight);
1210 sqlite3VdbeAddOp(v, op, p1, 0);
1211 break;
drhc9b84a12002-06-20 11:36:48 +00001212 }
drhcce7d172000-05-31 15:34:51 +00001213 case TK_AND:
1214 case TK_OR:
1215 case TK_PLUS:
1216 case TK_STAR:
1217 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00001218 case TK_REM:
1219 case TK_BITAND:
1220 case TK_BITOR:
drhc9b84a12002-06-20 11:36:48 +00001221 case TK_SLASH: {
danielk19774adee202004-05-08 08:23:19 +00001222 sqlite3ExprCode(pParse, pExpr->pLeft);
1223 sqlite3ExprCode(pParse, pExpr->pRight);
1224 sqlite3VdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001225 break;
1226 }
drhbf4133c2001-10-13 02:59:08 +00001227 case TK_LSHIFT:
1228 case TK_RSHIFT: {
danielk19774adee202004-05-08 08:23:19 +00001229 sqlite3ExprCode(pParse, pExpr->pRight);
1230 sqlite3ExprCode(pParse, pExpr->pLeft);
1231 sqlite3VdbeAddOp(v, op, 0, 0);
drhbf4133c2001-10-13 02:59:08 +00001232 break;
1233 }
drh00400772000-06-16 20:51:26 +00001234 case TK_CONCAT: {
danielk19774adee202004-05-08 08:23:19 +00001235 sqlite3ExprCode(pParse, pExpr->pLeft);
1236 sqlite3ExprCode(pParse, pExpr->pRight);
1237 sqlite3VdbeAddOp(v, OP_Concat, 2, 0);
drh00400772000-06-16 20:51:26 +00001238 break;
1239 }
drhcce7d172000-05-31 15:34:51 +00001240 case TK_UMINUS: {
drh6e142f52000-06-08 13:36:40 +00001241 assert( pExpr->pLeft );
drh7a7c7392001-11-24 00:31:46 +00001242 if( pExpr->pLeft->op==TK_FLOAT || pExpr->pLeft->op==TK_INTEGER ){
drh6e142f52000-06-08 13:36:40 +00001243 Token *p = &pExpr->pLeft->token;
1244 char *z = sqliteMalloc( p->n + 2 );
1245 sprintf(z, "-%.*s", p->n, p->z);
danielk19774adee202004-05-08 08:23:19 +00001246 if( pExpr->pLeft->op==TK_INTEGER && sqlite3FitsIn32Bits(z) ){
1247 sqlite3VdbeAddOp(v, OP_Integer, atoi(z), 0);
drhe6840902002-03-06 03:08:25 +00001248 }else{
danielk197793edea92004-05-16 22:55:28 +00001249 sqlite3VdbeAddOp(v, OP_Real, 0, 0);
drhe6840902002-03-06 03:08:25 +00001250 }
danielk19774adee202004-05-08 08:23:19 +00001251 sqlite3VdbeChangeP3(v, -1, z, p->n+1);
drh6e142f52000-06-08 13:36:40 +00001252 sqliteFree(z);
1253 break;
1254 }
drh1ccde152000-06-17 13:12:39 +00001255 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +00001256 }
drhbf4133c2001-10-13 02:59:08 +00001257 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00001258 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001259 sqlite3ExprCode(pParse, pExpr->pLeft);
1260 sqlite3VdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001261 break;
1262 }
1263 case TK_ISNULL:
1264 case TK_NOTNULL: {
1265 int dest;
danielk19774adee202004-05-08 08:23:19 +00001266 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1267 sqlite3ExprCode(pParse, pExpr->pLeft);
1268 dest = sqlite3VdbeCurrentAddr(v) + 2;
1269 sqlite3VdbeAddOp(v, op, 1, dest);
1270 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);
drhcce7d172000-05-31 15:34:51 +00001271 }
danielk1977a37cdde2004-05-16 11:15:36 +00001272 break;
drh22827922000-06-06 17:27:05 +00001273 case TK_AGG_FUNCTION: {
danielk19774adee202004-05-08 08:23:19 +00001274 sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drh22827922000-06-06 17:27:05 +00001275 break;
1276 }
drh4b59ab52002-08-24 18:24:51 +00001277 case TK_GLOB:
1278 case TK_LIKE:
drhcce7d172000-05-31 15:34:51 +00001279 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00001280 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00001281 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00001282 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00001283 int nId;
1284 const char *zId;
1285 getFunctionName(pExpr, &zId, &nId);
danielk19774adee202004-05-08 08:23:19 +00001286 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, 0);
drh0bce8352002-02-28 00:41:10 +00001287 assert( pDef!=0 );
danielk19774adee202004-05-08 08:23:19 +00001288 nExpr = sqlite3ExprCodeExprList(pParse, pList, pDef->includeTypes);
danielk1977a37cdde2004-05-16 11:15:36 +00001289 /* FIX ME: The following is a temporary hack. */
1290 if( 0==sqlite3StrNICmp(zId, "classof", nId) ){
1291 assert( nExpr==1 );
1292 sqlite3VdbeOp3(v, OP_Class, nExpr, 0, 0, 0);
1293 }else{
1294 sqlite3VdbeOp3(v, OP_Function, nExpr, 0, (char*)pDef, P3_POINTER);
1295 }
drhcce7d172000-05-31 15:34:51 +00001296 break;
1297 }
drh19a775c2000-06-05 18:54:46 +00001298 case TK_SELECT: {
danielk19774adee202004-05-08 08:23:19 +00001299 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drh19a775c2000-06-05 18:54:46 +00001300 break;
1301 }
drhfef52082000-06-06 01:50:43 +00001302 case TK_IN: {
1303 int addr;
danielk1977e014a832004-05-17 10:48:57 +00001304 char const *affStr;
1305
1306 /* Figure out the affinity to use to create a key from the results
1307 ** of the expression. affinityStr stores a static string suitable for
1308 ** P3 of OP_MakeKey.
1309 */
1310 affStr = sqlite3AffinityString(comparisonAffinity(pExpr));
1311
danielk19774adee202004-05-08 08:23:19 +00001312 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
danielk1977e014a832004-05-17 10:48:57 +00001313
1314 /* Code the <expr> from "<expr> IN (...)". The temporary table
1315 ** pExpr->iTable contains the values that make up the (...) set.
1316 */
danielk19774adee202004-05-08 08:23:19 +00001317 sqlite3ExprCode(pParse, pExpr->pLeft);
1318 addr = sqlite3VdbeCurrentAddr(v);
danielk1977e014a832004-05-17 10:48:57 +00001319 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); /* addr + 0 */
danielk19774adee202004-05-08 08:23:19 +00001320 sqlite3VdbeAddOp(v, OP_Pop, 2, 0);
1321 sqlite3VdbeAddOp(v, OP_String, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +00001322 sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7);
1323 sqlite3VdbeOp3(v, OP_MakeKey, 1, 0, affStr, P3_STATIC); /* addr + 4 */
1324 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7);
1325 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); /* addr + 6 */
1326
drhfef52082000-06-06 01:50:43 +00001327 break;
1328 }
1329 case TK_BETWEEN: {
danielk19774adee202004-05-08 08:23:19 +00001330 sqlite3ExprCode(pParse, pExpr->pLeft);
1331 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
1332 sqlite3ExprCode(pParse, pExpr->pList->a[0].pExpr);
1333 sqlite3VdbeAddOp(v, OP_Ge, 0, 0);
1334 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
1335 sqlite3ExprCode(pParse, pExpr->pList->a[1].pExpr);
1336 sqlite3VdbeAddOp(v, OP_Le, 0, 0);
1337 sqlite3VdbeAddOp(v, OP_And, 0, 0);
drhfef52082000-06-06 01:50:43 +00001338 break;
1339 }
drh51e9a442004-01-16 16:42:53 +00001340 case TK_UPLUS:
drha2e00042002-01-22 03:13:42 +00001341 case TK_AS: {
danielk19774adee202004-05-08 08:23:19 +00001342 sqlite3ExprCode(pParse, pExpr->pLeft);
drha2e00042002-01-22 03:13:42 +00001343 break;
1344 }
drh17a7f8d2002-03-24 13:13:27 +00001345 case TK_CASE: {
1346 int expr_end_label;
drhf5905aa2002-05-26 20:54:33 +00001347 int jumpInst;
1348 int addr;
1349 int nExpr;
drh17a7f8d2002-03-24 13:13:27 +00001350 int i;
1351
1352 assert(pExpr->pList);
1353 assert((pExpr->pList->nExpr % 2) == 0);
1354 assert(pExpr->pList->nExpr > 0);
drhf5905aa2002-05-26 20:54:33 +00001355 nExpr = pExpr->pList->nExpr;
danielk19774adee202004-05-08 08:23:19 +00001356 expr_end_label = sqlite3VdbeMakeLabel(v);
drh17a7f8d2002-03-24 13:13:27 +00001357 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001358 sqlite3ExprCode(pParse, pExpr->pLeft);
drh17a7f8d2002-03-24 13:13:27 +00001359 }
drhf5905aa2002-05-26 20:54:33 +00001360 for(i=0; i<nExpr; i=i+2){
danielk19774adee202004-05-08 08:23:19 +00001361 sqlite3ExprCode(pParse, pExpr->pList->a[i].pExpr);
drh17a7f8d2002-03-24 13:13:27 +00001362 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001363 sqlite3VdbeAddOp(v, OP_Dup, 1, 1);
1364 jumpInst = sqlite3VdbeAddOp(v, OP_Ne, 1, 0);
1365 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001366 }else{
danielk19774adee202004-05-08 08:23:19 +00001367 jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001368 }
danielk19774adee202004-05-08 08:23:19 +00001369 sqlite3ExprCode(pParse, pExpr->pList->a[i+1].pExpr);
1370 sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label);
1371 addr = sqlite3VdbeCurrentAddr(v);
1372 sqlite3VdbeChangeP2(v, jumpInst, addr);
drh17a7f8d2002-03-24 13:13:27 +00001373 }
drhf570f012002-05-31 15:51:25 +00001374 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001375 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhf570f012002-05-31 15:51:25 +00001376 }
drh17a7f8d2002-03-24 13:13:27 +00001377 if( pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001378 sqlite3ExprCode(pParse, pExpr->pRight);
drh17a7f8d2002-03-24 13:13:27 +00001379 }else{
danielk19774adee202004-05-08 08:23:19 +00001380 sqlite3VdbeAddOp(v, OP_String, 0, 0);
drh17a7f8d2002-03-24 13:13:27 +00001381 }
danielk19774adee202004-05-08 08:23:19 +00001382 sqlite3VdbeResolveLabel(v, expr_end_label);
danielk19776f349032002-06-11 02:25:40 +00001383 break;
1384 }
1385 case TK_RAISE: {
1386 if( !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +00001387 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00001388 "RAISE() may only be used within a trigger-program");
danielk19776f349032002-06-11 02:25:40 +00001389 pParse->nErr++;
1390 return;
1391 }
1392 if( pExpr->iColumn == OE_Rollback ||
1393 pExpr->iColumn == OE_Abort ||
1394 pExpr->iColumn == OE_Fail ){
danielk19774adee202004-05-08 08:23:19 +00001395 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn,
drh701a0ae2004-02-22 20:05:00 +00001396 pExpr->token.z, pExpr->token.n);
danielk19774adee202004-05-08 08:23:19 +00001397 sqlite3VdbeDequoteP3(v, -1);
danielk19776f349032002-06-11 02:25:40 +00001398 } else {
1399 assert( pExpr->iColumn == OE_Ignore );
danielk19774adee202004-05-08 08:23:19 +00001400 sqlite3VdbeOp3(v, OP_Goto, 0, pParse->trigStack->ignoreJump,
drh701a0ae2004-02-22 20:05:00 +00001401 "(IGNORE jump)", 0);
danielk19776f349032002-06-11 02:25:40 +00001402 }
drh17a7f8d2002-03-24 13:13:27 +00001403 }
1404 break;
drhcce7d172000-05-31 15:34:51 +00001405 }
drhcce7d172000-05-31 15:34:51 +00001406}
1407
1408/*
drh268380c2004-02-25 13:47:31 +00001409** Generate code that pushes the value of every element of the given
1410** expression list onto the stack. If the includeTypes flag is true,
1411** then also push a string that is the datatype of each element onto
1412** the stack after the value.
1413**
1414** Return the number of elements pushed onto the stack.
1415*/
danielk19774adee202004-05-08 08:23:19 +00001416int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00001417 Parse *pParse, /* Parsing context */
1418 ExprList *pList, /* The expression list to be coded */
1419 int includeTypes /* TRUE to put datatypes on the stack too */
1420){
1421 struct ExprList_item *pItem;
1422 int i, n;
1423 Vdbe *v;
1424 if( pList==0 ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001425 v = sqlite3GetVdbe(pParse);
drh268380c2004-02-25 13:47:31 +00001426 n = pList->nExpr;
1427 for(pItem=pList->a, i=0; i<n; i++, pItem++){
danielk19774adee202004-05-08 08:23:19 +00001428 sqlite3ExprCode(pParse, pItem->pExpr);
drh268380c2004-02-25 13:47:31 +00001429 if( includeTypes ){
danielk19774adee202004-05-08 08:23:19 +00001430 sqlite3VdbeOp3(v, OP_String, 0, 0,
1431 sqlite3ExprType(pItem->pExpr)==SQLITE_SO_NUM ? "numeric" : "text",
drh268380c2004-02-25 13:47:31 +00001432 P3_STATIC);
1433 }
1434 }
1435 return includeTypes ? n*2 : n;
1436}
1437
1438/*
drhcce7d172000-05-31 15:34:51 +00001439** Generate code for a boolean expression such that a jump is made
1440** to the label "dest" if the expression is true but execution
1441** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00001442**
1443** If the expression evaluates to NULL (neither true nor false), then
1444** take the jump if the jumpIfNull flag is true.
drhcce7d172000-05-31 15:34:51 +00001445*/
danielk19774adee202004-05-08 08:23:19 +00001446void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001447 Vdbe *v = pParse->pVdbe;
1448 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001449 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001450 switch( pExpr->op ){
1451 case TK_LT: op = OP_Lt; break;
1452 case TK_LE: op = OP_Le; break;
1453 case TK_GT: op = OP_Gt; break;
1454 case TK_GE: op = OP_Ge; break;
1455 case TK_NE: op = OP_Ne; break;
1456 case TK_EQ: op = OP_Eq; break;
drhcce7d172000-05-31 15:34:51 +00001457 case TK_ISNULL: op = OP_IsNull; break;
1458 case TK_NOTNULL: op = OP_NotNull; break;
1459 default: break;
1460 }
1461 switch( pExpr->op ){
1462 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001463 int d2 = sqlite3VdbeMakeLabel(v);
1464 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
1465 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
1466 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001467 break;
1468 }
1469 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001470 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
1471 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001472 break;
1473 }
1474 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001475 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001476 break;
1477 }
1478 case TK_LT:
1479 case TK_LE:
1480 case TK_GT:
1481 case TK_GE:
1482 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00001483 case TK_EQ: {
danielk1977a37cdde2004-05-16 11:15:36 +00001484 int p1 = binaryCompareP1(pExpr->pLeft, pExpr->pRight, jumpIfNull);
danielk19774adee202004-05-08 08:23:19 +00001485 sqlite3ExprCode(pParse, pExpr->pLeft);
1486 sqlite3ExprCode(pParse, pExpr->pRight);
danielk1977a37cdde2004-05-16 11:15:36 +00001487 sqlite3VdbeAddOp(v, op, p1, dest);
drhcce7d172000-05-31 15:34:51 +00001488 break;
1489 }
1490 case TK_ISNULL:
1491 case TK_NOTNULL: {
danielk19774adee202004-05-08 08:23:19 +00001492 sqlite3ExprCode(pParse, pExpr->pLeft);
1493 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001494 break;
1495 }
danielk1977e014a832004-05-17 10:48:57 +00001496#if 0
drhfef52082000-06-06 01:50:43 +00001497 case TK_IN: {
drhf5905aa2002-05-26 20:54:33 +00001498 int addr;
danielk19774adee202004-05-08 08:23:19 +00001499 sqlite3ExprCode(pParse, pExpr->pLeft);
1500 addr = sqlite3VdbeCurrentAddr(v);
1501 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+3);
1502 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
1503 sqlite3VdbeAddOp(v, OP_Goto, 0, jumpIfNull ? dest : addr+4);
drhfef52082000-06-06 01:50:43 +00001504 if( pExpr->pSelect ){
danielk19774adee202004-05-08 08:23:19 +00001505 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001506 }else{
danielk19774adee202004-05-08 08:23:19 +00001507 sqlite3VdbeAddOp(v, OP_SetFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001508 }
1509 break;
1510 }
danielk1977e014a832004-05-17 10:48:57 +00001511#endif
drhfef52082000-06-06 01:50:43 +00001512 case TK_BETWEEN: {
drhf5905aa2002-05-26 20:54:33 +00001513 int addr;
danielk19774adee202004-05-08 08:23:19 +00001514 sqlite3ExprCode(pParse, pExpr->pLeft);
1515 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
1516 sqlite3ExprCode(pParse, pExpr->pList->a[0].pExpr);
1517 addr = sqlite3VdbeAddOp(v, OP_Lt, !jumpIfNull, 0);
1518 sqlite3ExprCode(pParse, pExpr->pList->a[1].pExpr);
1519 sqlite3VdbeAddOp(v, OP_Le, jumpIfNull, dest);
1520 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
1521 sqlite3VdbeChangeP2(v, addr, sqlite3VdbeCurrentAddr(v));
1522 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00001523 break;
1524 }
drhcce7d172000-05-31 15:34:51 +00001525 default: {
danielk19774adee202004-05-08 08:23:19 +00001526 sqlite3ExprCode(pParse, pExpr);
1527 sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001528 break;
1529 }
1530 }
1531}
1532
1533/*
drh66b89c82000-11-28 20:47:17 +00001534** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00001535** to the label "dest" if the expression is false but execution
1536** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00001537**
1538** If the expression evaluates to NULL (neither true nor false) then
1539** jump if jumpIfNull is true or fall through if jumpIfNull is false.
drhcce7d172000-05-31 15:34:51 +00001540*/
danielk19774adee202004-05-08 08:23:19 +00001541void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001542 Vdbe *v = pParse->pVdbe;
1543 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001544 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001545 switch( pExpr->op ){
1546 case TK_LT: op = OP_Ge; break;
1547 case TK_LE: op = OP_Gt; break;
1548 case TK_GT: op = OP_Le; break;
1549 case TK_GE: op = OP_Lt; break;
1550 case TK_NE: op = OP_Eq; break;
1551 case TK_EQ: op = OP_Ne; break;
drhcce7d172000-05-31 15:34:51 +00001552 case TK_ISNULL: op = OP_NotNull; break;
1553 case TK_NOTNULL: op = OP_IsNull; break;
1554 default: break;
1555 }
1556 switch( pExpr->op ){
1557 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001558 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
1559 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001560 break;
1561 }
1562 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001563 int d2 = sqlite3VdbeMakeLabel(v);
1564 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
1565 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
1566 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001567 break;
1568 }
1569 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001570 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001571 break;
1572 }
1573 case TK_LT:
1574 case TK_LE:
1575 case TK_GT:
1576 case TK_GE:
1577 case TK_NE:
1578 case TK_EQ: {
danielk1977a37cdde2004-05-16 11:15:36 +00001579 int p1 = binaryCompareP1(pExpr->pLeft, pExpr->pRight, jumpIfNull);
danielk19774adee202004-05-08 08:23:19 +00001580 sqlite3ExprCode(pParse, pExpr->pLeft);
1581 sqlite3ExprCode(pParse, pExpr->pRight);
danielk1977a37cdde2004-05-16 11:15:36 +00001582 sqlite3VdbeAddOp(v, op, p1, dest);
drhcce7d172000-05-31 15:34:51 +00001583 break;
1584 }
drhcce7d172000-05-31 15:34:51 +00001585 case TK_ISNULL:
1586 case TK_NOTNULL: {
danielk19774adee202004-05-08 08:23:19 +00001587 sqlite3ExprCode(pParse, pExpr->pLeft);
1588 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001589 break;
1590 }
danielk1977e014a832004-05-17 10:48:57 +00001591#if 0
drhfef52082000-06-06 01:50:43 +00001592 case TK_IN: {
drhf5905aa2002-05-26 20:54:33 +00001593 int addr;
danielk19774adee202004-05-08 08:23:19 +00001594 sqlite3ExprCode(pParse, pExpr->pLeft);
1595 addr = sqlite3VdbeCurrentAddr(v);
1596 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+3);
1597 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
1598 sqlite3VdbeAddOp(v, OP_Goto, 0, jumpIfNull ? dest : addr+4);
drhfef52082000-06-06 01:50:43 +00001599 if( pExpr->pSelect ){
danielk19774adee202004-05-08 08:23:19 +00001600 sqlite3VdbeAddOp(v, OP_NotFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001601 }else{
danielk19774adee202004-05-08 08:23:19 +00001602 sqlite3VdbeAddOp(v, OP_SetNotFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001603 }
1604 break;
1605 }
danielk1977e014a832004-05-17 10:48:57 +00001606#endif
drhfef52082000-06-06 01:50:43 +00001607 case TK_BETWEEN: {
1608 int addr;
danielk19774adee202004-05-08 08:23:19 +00001609 sqlite3ExprCode(pParse, pExpr->pLeft);
1610 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
1611 sqlite3ExprCode(pParse, pExpr->pList->a[0].pExpr);
1612 addr = sqlite3VdbeCurrentAddr(v);
1613 sqlite3VdbeAddOp(v, OP_Ge, !jumpIfNull, addr+3);
1614 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
1615 sqlite3VdbeAddOp(v, OP_Goto, 0, dest);
1616 sqlite3ExprCode(pParse, pExpr->pList->a[1].pExpr);
1617 sqlite3VdbeAddOp(v, OP_Gt, jumpIfNull, dest);
drhfef52082000-06-06 01:50:43 +00001618 break;
1619 }
drhcce7d172000-05-31 15:34:51 +00001620 default: {
danielk19774adee202004-05-08 08:23:19 +00001621 sqlite3ExprCode(pParse, pExpr);
1622 sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001623 break;
1624 }
1625 }
1626}
drh22827922000-06-06 17:27:05 +00001627
1628/*
1629** Do a deep comparison of two expression trees. Return TRUE (non-zero)
1630** if they are identical and return FALSE if they differ in any way.
1631*/
danielk19774adee202004-05-08 08:23:19 +00001632int sqlite3ExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00001633 int i;
1634 if( pA==0 ){
1635 return pB==0;
1636 }else if( pB==0 ){
1637 return 0;
1638 }
1639 if( pA->op!=pB->op ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001640 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
1641 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00001642 if( pA->pList ){
1643 if( pB->pList==0 ) return 0;
1644 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
1645 for(i=0; i<pA->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00001646 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00001647 return 0;
1648 }
1649 }
1650 }else if( pB->pList ){
1651 return 0;
1652 }
1653 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00001654 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drh22827922000-06-06 17:27:05 +00001655 if( pA->token.z ){
1656 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00001657 if( pB->token.n!=pA->token.n ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001658 if( sqlite3StrNICmp(pA->token.z, pB->token.z, pB->token.n)!=0 ) return 0;
drh22827922000-06-06 17:27:05 +00001659 }
1660 return 1;
1661}
1662
1663/*
1664** Add a new element to the pParse->aAgg[] array and return its index.
1665*/
1666static int appendAggInfo(Parse *pParse){
1667 if( (pParse->nAgg & 0x7)==0 ){
1668 int amt = pParse->nAgg + 8;
drh6d4abfb2001-10-22 02:58:08 +00001669 AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0]));
1670 if( aAgg==0 ){
drh22827922000-06-06 17:27:05 +00001671 return -1;
1672 }
drh6d4abfb2001-10-22 02:58:08 +00001673 pParse->aAgg = aAgg;
drh22827922000-06-06 17:27:05 +00001674 }
1675 memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0]));
1676 return pParse->nAgg++;
1677}
1678
1679/*
1680** Analyze the given expression looking for aggregate functions and
1681** for variables that need to be added to the pParse->aAgg[] array.
1682** Make additional entries to the pParse->aAgg[] array as necessary.
1683**
1684** This routine should only be called after the expression has been
danielk19774adee202004-05-08 08:23:19 +00001685** analyzed by sqlite3ExprResolveIds() and sqlite3ExprCheck().
drh22827922000-06-06 17:27:05 +00001686**
1687** If errors are seen, leave an error message in zErrMsg and return
1688** the number of errors.
1689*/
danielk19774adee202004-05-08 08:23:19 +00001690int sqlite3ExprAnalyzeAggregates(Parse *pParse, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00001691 int i;
1692 AggExpr *aAgg;
1693 int nErr = 0;
1694
1695 if( pExpr==0 ) return 0;
1696 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00001697 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +00001698 aAgg = pParse->aAgg;
1699 for(i=0; i<pParse->nAgg; i++){
1700 if( aAgg[i].isAgg ) continue;
1701 if( aAgg[i].pExpr->iTable==pExpr->iTable
drh967e8b72000-06-21 13:59:10 +00001702 && aAgg[i].pExpr->iColumn==pExpr->iColumn ){
drh22827922000-06-06 17:27:05 +00001703 break;
1704 }
1705 }
1706 if( i>=pParse->nAgg ){
1707 i = appendAggInfo(pParse);
1708 if( i<0 ) return 1;
1709 pParse->aAgg[i].isAgg = 0;
1710 pParse->aAgg[i].pExpr = pExpr;
1711 }
drhaaf88722000-06-08 11:25:00 +00001712 pExpr->iAgg = i;
drh22827922000-06-06 17:27:05 +00001713 break;
1714 }
1715 case TK_AGG_FUNCTION: {
drh22827922000-06-06 17:27:05 +00001716 aAgg = pParse->aAgg;
1717 for(i=0; i<pParse->nAgg; i++){
1718 if( !aAgg[i].isAgg ) continue;
danielk19774adee202004-05-08 08:23:19 +00001719 if( sqlite3ExprCompare(aAgg[i].pExpr, pExpr) ){
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 = 1;
1727 pParse->aAgg[i].pExpr = pExpr;
danielk19774adee202004-05-08 08:23:19 +00001728 pParse->aAgg[i].pFunc = sqlite3FindFunction(pParse->db,
drh6977fea2002-10-22 23:38:04 +00001729 pExpr->token.z, pExpr->token.n,
drhf55f25f2002-02-28 01:46:11 +00001730 pExpr->pList ? pExpr->pList->nExpr : 0, 0);
drh22827922000-06-06 17:27:05 +00001731 }
1732 pExpr->iAgg = i;
1733 break;
1734 }
1735 default: {
1736 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001737 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pLeft);
drh22827922000-06-06 17:27:05 +00001738 }
1739 if( nErr==0 && pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001740 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pRight);
drh22827922000-06-06 17:27:05 +00001741 }
1742 if( nErr==0 && pExpr->pList ){
1743 int n = pExpr->pList->nExpr;
1744 int i;
1745 for(i=0; nErr==0 && i<n; i++){
danielk19774adee202004-05-08 08:23:19 +00001746 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pList->a[i].pExpr);
drh22827922000-06-06 17:27:05 +00001747 }
1748 }
1749 break;
1750 }
1751 }
1752 return nErr;
1753}
drh8e0a2f92002-02-23 23:45:45 +00001754
1755/*
1756** Locate a user function given a name and a number of arguments.
drh0bce8352002-02-28 00:41:10 +00001757** Return a pointer to the FuncDef structure that defines that
drh8e0a2f92002-02-23 23:45:45 +00001758** function, or return NULL if the function does not exist.
1759**
drh0bce8352002-02-28 00:41:10 +00001760** If the createFlag argument is true, then a new (blank) FuncDef
drh8e0a2f92002-02-23 23:45:45 +00001761** structure is created and liked into the "db" structure if a
1762** no matching function previously existed. When createFlag is true
1763** and the nArg parameter is -1, then only a function that accepts
1764** any number of arguments will be returned.
1765**
1766** If createFlag is false and nArg is -1, then the first valid
1767** function found is returned. A function is valid if either xFunc
1768** or xStep is non-zero.
1769*/
danielk19774adee202004-05-08 08:23:19 +00001770FuncDef *sqlite3FindFunction(
drh8e0a2f92002-02-23 23:45:45 +00001771 sqlite *db, /* An open database */
1772 const char *zName, /* Name of the function. Not null-terminated */
1773 int nName, /* Number of characters in the name */
1774 int nArg, /* Number of arguments. -1 means any number */
1775 int createFlag /* Create new entry if true and does not otherwise exist */
1776){
drh0bce8352002-02-28 00:41:10 +00001777 FuncDef *pFirst, *p, *pMaybe;
danielk19774adee202004-05-08 08:23:19 +00001778 pFirst = p = (FuncDef*)sqlite3HashFind(&db->aFunc, zName, nName);
drh1350b032002-02-27 19:00:20 +00001779 if( p && !createFlag && nArg<0 ){
drh8e0a2f92002-02-23 23:45:45 +00001780 while( p && p->xFunc==0 && p->xStep==0 ){ p = p->pNext; }
1781 return p;
1782 }
1783 pMaybe = 0;
1784 while( p && p->nArg!=nArg ){
1785 if( p->nArg<0 && !createFlag && (p->xFunc || p->xStep) ) pMaybe = p;
1786 p = p->pNext;
1787 }
1788 if( p && !createFlag && p->xFunc==0 && p->xStep==0 ){
1789 return 0;
1790 }
1791 if( p==0 && pMaybe ){
1792 assert( createFlag==0 );
1793 return pMaybe;
1794 }
drh89425d52002-02-28 03:04:48 +00001795 if( p==0 && createFlag && (p = sqliteMalloc(sizeof(*p)))!=0 ){
drh8e0a2f92002-02-23 23:45:45 +00001796 p->nArg = nArg;
1797 p->pNext = pFirst;
drhc9b84a12002-06-20 11:36:48 +00001798 p->dataType = pFirst ? pFirst->dataType : SQLITE_NUMERIC;
danielk19774adee202004-05-08 08:23:19 +00001799 sqlite3HashInsert(&db->aFunc, zName, nName, (void*)p);
drh8e0a2f92002-02-23 23:45:45 +00001800 }
1801 return p;
1802}
danielk19774adee202004-05-08 08:23:19 +00001803
1804
1805