blob: 65821bd53509841df288ab37468c4fdbdc7fb86c [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**
drhfec19aa2004-05-19 20:41:03 +000015** $Id: expr.c,v 1.123 2004/05/19 20:41:03 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
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: {
drhfec19aa2004-05-19 20:41:03 +0000445 if( sqlite3GetInt32(p->token.z, pValue) ){
drh202b2df2004-01-06 01:13:46 +0000446 return 1;
447 }
448 break;
drhe4de1fe2002-06-02 16:09:01 +0000449 }
450 case TK_STRING: {
drhbd790ee2002-06-02 18:22:06 +0000451 const char *z = p->token.z;
drhe4de1fe2002-06-02 16:09:01 +0000452 int n = p->token.n;
drhbd790ee2002-06-02 18:22:06 +0000453 if( n>0 && z[0]=='-' ){ z++; n--; }
drhe4de1fe2002-06-02 16:09:01 +0000454 while( n>0 && *z && isdigit(*z) ){ z++; n--; }
drhfec19aa2004-05-19 20:41:03 +0000455 if( n==0 && sqlite3GetInt32(p->token.z, pValue) ){
drhe4de1fe2002-06-02 16:09:01 +0000456 return 1;
457 }
458 break;
459 }
drh4b59ab52002-08-24 18:24:51 +0000460 case TK_UPLUS: {
danielk19774adee202004-05-08 08:23:19 +0000461 return sqlite3ExprIsInteger(p->pLeft, pValue);
drh4b59ab52002-08-24 18:24:51 +0000462 }
drhe4de1fe2002-06-02 16:09:01 +0000463 case TK_UMINUS: {
464 int v;
danielk19774adee202004-05-08 08:23:19 +0000465 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
drhe4de1fe2002-06-02 16:09:01 +0000466 *pValue = -v;
467 return 1;
468 }
469 break;
470 }
471 default: break;
472 }
473 return 0;
474}
475
476/*
drhc4a3c772001-04-04 11:48:57 +0000477** Return TRUE if the given string is a row-id column name.
478*/
danielk19774adee202004-05-08 08:23:19 +0000479int sqlite3IsRowid(const char *z){
480 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
481 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
482 if( sqlite3StrICmp(z, "OID")==0 ) return 1;
drhc4a3c772001-04-04 11:48:57 +0000483 return 0;
484}
485
486/*
drh8141f612004-01-25 22:44:58 +0000487** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
488** that name in the set of source tables in pSrcList and make the pExpr
489** expression node refer back to that source column. The following changes
490** are made to pExpr:
491**
492** pExpr->iDb Set the index in db->aDb[] of the database holding
493** the table.
494** pExpr->iTable Set to the cursor number for the table obtained
495** from pSrcList.
496** pExpr->iColumn Set to the column number within the table.
497** pExpr->dataType Set to the appropriate data type for the column.
498** pExpr->op Set to TK_COLUMN.
499** pExpr->pLeft Any expression this points to is deleted
500** pExpr->pRight Any expression this points to is deleted.
501**
502** The pDbToken is the name of the database (the "X"). This value may be
503** NULL meaning that name is of the form Y.Z or Z. Any available database
504** can be used. The pTableToken is the name of the table (the "Y"). This
505** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it
506** means that the form of the name is Z and that columns from any table
507** can be used.
508**
509** If the name cannot be resolved unambiguously, leave an error message
510** in pParse and return non-zero. Return zero on success.
511*/
512static int lookupName(
513 Parse *pParse, /* The parsing context */
514 Token *pDbToken, /* Name of the database containing table, or NULL */
515 Token *pTableToken, /* Name of table containing column, or NULL */
516 Token *pColumnToken, /* Name of the column. */
517 SrcList *pSrcList, /* List of tables used to resolve column names */
518 ExprList *pEList, /* List of expressions used to resolve "AS" */
519 Expr *pExpr /* Make this EXPR node point to the selected column */
520){
521 char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */
522 char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */
523 char *zCol = 0; /* Name of the column. The "Z" */
524 int i, j; /* Loop counters */
525 int cnt = 0; /* Number of matching column names */
526 int cntTab = 0; /* Number of matching table names */
drh7e26d752004-02-11 10:35:29 +0000527 sqlite *db = pParse->db; /* The database */
drh8141f612004-01-25 22:44:58 +0000528
529 assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
530 if( pDbToken && pDbToken->z ){
531 zDb = sqliteStrNDup(pDbToken->z, pDbToken->n);
danielk19774adee202004-05-08 08:23:19 +0000532 sqlite3Dequote(zDb);
drh8141f612004-01-25 22:44:58 +0000533 }else{
534 zDb = 0;
535 }
536 if( pTableToken && pTableToken->z ){
537 zTab = sqliteStrNDup(pTableToken->z, pTableToken->n);
danielk19774adee202004-05-08 08:23:19 +0000538 sqlite3Dequote(zTab);
drh8141f612004-01-25 22:44:58 +0000539 }else{
540 assert( zDb==0 );
541 zTab = 0;
542 }
543 zCol = sqliteStrNDup(pColumnToken->z, pColumnToken->n);
danielk19774adee202004-05-08 08:23:19 +0000544 sqlite3Dequote(zCol);
danielk197724b03fd2004-05-10 10:34:34 +0000545 if( sqlite3_malloc_failed ){
drh8141f612004-01-25 22:44:58 +0000546 return 1; /* Leak memory (zDb and zTab) if malloc fails */
547 }
548 assert( zTab==0 || pEList==0 );
549
550 pExpr->iTable = -1;
551 for(i=0; i<pSrcList->nSrc; i++){
552 struct SrcList_item *pItem = &pSrcList->a[i];
553 Table *pTab = pItem->pTab;
554 Column *pCol;
555
556 if( pTab==0 ) continue;
557 assert( pTab->nCol>0 );
558 if( zTab ){
559 if( pItem->zAlias ){
560 char *zTabName = pItem->zAlias;
danielk19774adee202004-05-08 08:23:19 +0000561 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
drh8141f612004-01-25 22:44:58 +0000562 }else{
563 char *zTabName = pTab->zName;
danielk19774adee202004-05-08 08:23:19 +0000564 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
565 if( zDb!=0 && sqlite3StrICmp(db->aDb[pTab->iDb].zName, zDb)!=0 ){
drh8141f612004-01-25 22:44:58 +0000566 continue;
567 }
568 }
569 }
570 if( 0==(cntTab++) ){
571 pExpr->iTable = pItem->iCursor;
572 pExpr->iDb = pTab->iDb;
573 }
574 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
danielk19774adee202004-05-08 08:23:19 +0000575 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
drh8141f612004-01-25 22:44:58 +0000576 cnt++;
577 pExpr->iTable = pItem->iCursor;
578 pExpr->iDb = pTab->iDb;
579 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
580 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
danielk1977a37cdde2004-05-16 11:15:36 +0000581 pExpr->affinity = pTab->aCol[j].affinity;
582
583 /* FIX ME: Expr::dataType will be removed... */
584 pExpr->dataType =
585 (pCol->affinity==SQLITE_AFF_TEXT?SQLITE_SO_TEXT:SQLITE_SO_NUM);
drh8141f612004-01-25 22:44:58 +0000586 break;
587 }
588 }
589 }
590
591 /* If we have not already resolved the name, then maybe
592 ** it is a new.* or old.* trigger argument reference
593 */
594 if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
595 TriggerStack *pTriggerStack = pParse->trigStack;
596 Table *pTab = 0;
danielk19774adee202004-05-08 08:23:19 +0000597 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){
drh8141f612004-01-25 22:44:58 +0000598 pExpr->iTable = pTriggerStack->newIdx;
599 assert( pTriggerStack->pTab );
600 pTab = pTriggerStack->pTab;
danielk19774adee202004-05-08 08:23:19 +0000601 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab) == 0 ){
drh8141f612004-01-25 22:44:58 +0000602 pExpr->iTable = pTriggerStack->oldIdx;
603 assert( pTriggerStack->pTab );
604 pTab = pTriggerStack->pTab;
605 }
606
607 if( pTab ){
608 int j;
609 Column *pCol = pTab->aCol;
610
611 pExpr->iDb = pTab->iDb;
612 cntTab++;
613 for(j=0; j < pTab->nCol; j++, pCol++) {
danielk19774adee202004-05-08 08:23:19 +0000614 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
drh8141f612004-01-25 22:44:58 +0000615 cnt++;
616 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
danielk1977a37cdde2004-05-16 11:15:36 +0000617 pExpr->affinity = pTab->aCol[j].affinity;
618 /* FIX ME: Expr::dataType will be removed... */
619 pExpr->dataType =
620 (pCol->affinity==SQLITE_AFF_TEXT?SQLITE_SO_TEXT:SQLITE_SO_NUM);
drh8141f612004-01-25 22:44:58 +0000621 break;
622 }
623 }
624 }
625 }
626
627 /*
628 ** Perhaps the name is a reference to the ROWID
629 */
danielk19774adee202004-05-08 08:23:19 +0000630 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
drh8141f612004-01-25 22:44:58 +0000631 cnt = 1;
632 pExpr->iColumn = -1;
633 pExpr->dataType = SQLITE_SO_NUM;
danielk1977a37cdde2004-05-16 11:15:36 +0000634 pExpr->affinity = SQLITE_AFF_INTEGER;
drh8141f612004-01-25 22:44:58 +0000635 }
636
637 /*
638 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
639 ** might refer to an result-set alias. This happens, for example, when
640 ** we are resolving names in the WHERE clause of the following command:
641 **
642 ** SELECT a+b AS x FROM table WHERE x<10;
643 **
644 ** In cases like this, replace pExpr with a copy of the expression that
645 ** forms the result set entry ("a+b" in the example) and return immediately.
646 ** Note that the expression in the result set should have already been
647 ** resolved by the time the WHERE clause is resolved.
648 */
649 if( cnt==0 && pEList!=0 ){
650 for(j=0; j<pEList->nExpr; j++){
651 char *zAs = pEList->a[j].zName;
danielk19774adee202004-05-08 08:23:19 +0000652 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
drh8141f612004-01-25 22:44:58 +0000653 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
654 pExpr->op = TK_AS;
655 pExpr->iColumn = j;
danielk19774adee202004-05-08 08:23:19 +0000656 pExpr->pLeft = sqlite3ExprDup(pEList->a[j].pExpr);
drh8141f612004-01-25 22:44:58 +0000657 sqliteFree(zCol);
658 assert( zTab==0 && zDb==0 );
659 return 0;
660 }
661 }
662 }
663
664 /*
665 ** If X and Y are NULL (in other words if only the column name Z is
666 ** supplied) and the value of Z is enclosed in double-quotes, then
667 ** Z is a string literal if it doesn't match any column names. In that
668 ** case, we need to return right away and not make any changes to
669 ** pExpr.
670 */
671 if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
672 sqliteFree(zCol);
673 return 0;
674 }
675
676 /*
677 ** cnt==0 means there was not match. cnt>1 means there were two or
678 ** more matches. Either way, we have an error.
679 */
680 if( cnt!=1 ){
681 char *z = 0;
682 char *zErr;
683 zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s";
684 if( zDb ){
danielk19774adee202004-05-08 08:23:19 +0000685 sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, 0);
drh8141f612004-01-25 22:44:58 +0000686 }else if( zTab ){
danielk19774adee202004-05-08 08:23:19 +0000687 sqlite3SetString(&z, zTab, ".", zCol, 0);
drh8141f612004-01-25 22:44:58 +0000688 }else{
689 z = sqliteStrDup(zCol);
690 }
danielk19774adee202004-05-08 08:23:19 +0000691 sqlite3ErrorMsg(pParse, zErr, z);
drh8141f612004-01-25 22:44:58 +0000692 sqliteFree(z);
693 }
694
695 /* Clean up and return
696 */
697 sqliteFree(zDb);
698 sqliteFree(zTab);
699 sqliteFree(zCol);
danielk19774adee202004-05-08 08:23:19 +0000700 sqlite3ExprDelete(pExpr->pLeft);
drh8141f612004-01-25 22:44:58 +0000701 pExpr->pLeft = 0;
danielk19774adee202004-05-08 08:23:19 +0000702 sqlite3ExprDelete(pExpr->pRight);
drh8141f612004-01-25 22:44:58 +0000703 pExpr->pRight = 0;
704 pExpr->op = TK_COLUMN;
danielk19774adee202004-05-08 08:23:19 +0000705 sqlite3AuthRead(pParse, pExpr, pSrcList);
drh8141f612004-01-25 22:44:58 +0000706 return cnt!=1;
707}
708
709/*
drhcce7d172000-05-31 15:34:51 +0000710** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +0000711** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +0000712** index to the table in the table list and a column offset. The
713** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
714** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +0000715** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +0000716** VDBE cursor number for a cursor that is pointing into the referenced
717** table. The Expr.iColumn value is changed to the index of the column
718** of the referenced table. The Expr.iColumn value for the special
719** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
720** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +0000721**
drhfef52082000-06-06 01:50:43 +0000722** We also check for instances of the IN operator. IN comes in two
723** forms:
724**
725** expr IN (exprlist)
726** and
727** expr IN (SELECT ...)
728**
729** The first form is handled by creating a set holding the list
730** of allowed values. The second form causes the SELECT to generate
731** a temporary table.
732**
733** This routine also looks for scalar SELECTs that are part of an expression.
drh19a775c2000-06-05 18:54:46 +0000734** If it finds any, it generates code to write the value of that select
735** into a memory cell.
drhcce7d172000-05-31 15:34:51 +0000736**
drh967e8b72000-06-21 13:59:10 +0000737** Unknown columns or tables provoke an error. The function returns
drhcce7d172000-05-31 15:34:51 +0000738** the number of errors seen and leaves an error message on pParse->zErrMsg.
739*/
danielk19774adee202004-05-08 08:23:19 +0000740int sqlite3ExprResolveIds(
drha2e00042002-01-22 03:13:42 +0000741 Parse *pParse, /* The parser context */
drh8141f612004-01-25 22:44:58 +0000742 SrcList *pSrcList, /* List of tables used to resolve column names */
drha2e00042002-01-22 03:13:42 +0000743 ExprList *pEList, /* List of expressions used to resolve "AS" */
744 Expr *pExpr /* The expression to be analyzed. */
745){
drh6a3ea0e2003-05-02 14:32:12 +0000746 int i;
747
drh8141f612004-01-25 22:44:58 +0000748 if( pExpr==0 || pSrcList==0 ) return 0;
749 for(i=0; i<pSrcList->nSrc; i++){
750 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab );
drh6a3ea0e2003-05-02 14:32:12 +0000751 }
drhcce7d172000-05-31 15:34:51 +0000752 switch( pExpr->op ){
drh23989372002-05-21 13:43:04 +0000753 /* Double-quoted strings (ex: "abc") are used as identifiers if
754 ** possible. Otherwise they remain as strings. Single-quoted
755 ** strings (ex: 'abc') are always string literals.
756 */
757 case TK_STRING: {
758 if( pExpr->token.z[0]=='\'' ) break;
759 /* Fall thru into the TK_ID case if this is a double-quoted string */
760 }
drh8141f612004-01-25 22:44:58 +0000761 /* A lone identifier is the name of a columnd.
drha2e00042002-01-22 03:13:42 +0000762 */
drhcce7d172000-05-31 15:34:51 +0000763 case TK_ID: {
drh8141f612004-01-25 22:44:58 +0000764 if( lookupName(pParse, 0, 0, &pExpr->token, pSrcList, pEList, pExpr) ){
drhcce7d172000-05-31 15:34:51 +0000765 return 1;
drhed6c8672003-01-12 18:02:16 +0000766 }
drhcce7d172000-05-31 15:34:51 +0000767 break;
768 }
769
drhd24cc422003-03-27 12:51:24 +0000770 /* A table name and column name: ID.ID
771 ** Or a database, table and column: ID.ID.ID
772 */
drhcce7d172000-05-31 15:34:51 +0000773 case TK_DOT: {
drh8141f612004-01-25 22:44:58 +0000774 Token *pColumn;
775 Token *pTable;
776 Token *pDb;
777 Expr *pRight;
drhcce7d172000-05-31 15:34:51 +0000778
drhcce7d172000-05-31 15:34:51 +0000779 pRight = pExpr->pRight;
drhd24cc422003-03-27 12:51:24 +0000780 if( pRight->op==TK_ID ){
drh8141f612004-01-25 22:44:58 +0000781 pDb = 0;
782 pTable = &pExpr->pLeft->token;
783 pColumn = &pRight->token;
drhd24cc422003-03-27 12:51:24 +0000784 }else{
drh8141f612004-01-25 22:44:58 +0000785 assert( pRight->op==TK_DOT );
786 pDb = &pExpr->pLeft->token;
787 pTable = &pRight->pLeft->token;
788 pColumn = &pRight->pRight->token;
drhd24cc422003-03-27 12:51:24 +0000789 }
drh8141f612004-01-25 22:44:58 +0000790 if( lookupName(pParse, pDb, pTable, pColumn, pSrcList, 0, pExpr) ){
drhdaffd0e2001-04-11 14:28:42 +0000791 return 1;
792 }
drhcce7d172000-05-31 15:34:51 +0000793 break;
794 }
795
drhfef52082000-06-06 01:50:43 +0000796 case TK_IN: {
danielk1977e014a832004-05-17 10:48:57 +0000797 char affinity;
danielk19774adee202004-05-08 08:23:19 +0000798 Vdbe *v = sqlite3GetVdbe(pParse);
drhfef52082000-06-06 01:50:43 +0000799 if( v==0 ) return 1;
danielk19774adee202004-05-08 08:23:19 +0000800 if( sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pLeft) ){
drhcfab11b2000-06-06 03:31:22 +0000801 return 1;
802 }
danielk1977bf3b7212004-05-18 10:06:24 +0000803 affinity = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +0000804
805 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
806 ** expression it is handled the same way. A temporary table is
807 ** filled with single-field index keys representing the results
808 ** from the SELECT or the <exprlist>.
809 **
810 ** If the 'x' expression is a column value, or the SELECT...
811 ** statement returns a column value, then the affinity of that
812 ** column is used to build the index keys. If both 'x' and the
813 ** SELECT... statement are columns, then numeric affinity is used
814 ** if either column has NUMERIC or INTEGER affinity. If neither
815 ** 'x' nor the SELECT... statement are columns, then numeric affinity
816 ** is used.
817 */
818 pExpr->iTable = pParse->nTab++;
819 sqlite3VdbeAddOp(v, OP_OpenTemp, pExpr->iTable, 1);
820
drhfef52082000-06-06 01:50:43 +0000821 if( pExpr->pSelect ){
822 /* Case 1: expr IN (SELECT ...)
823 **
danielk1977e014a832004-05-17 10:48:57 +0000824 ** Generate code to write the results of the select into the temporary
825 ** table allocated and opened above.
drhfef52082000-06-06 01:50:43 +0000826 */
danielk1977e014a832004-05-17 10:48:57 +0000827 int iParm = pExpr->iTable + (((int)affinity)<<16);
828 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
danielk1977bf3b7212004-05-18 10:06:24 +0000829 sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0);
drhfef52082000-06-06 01:50:43 +0000830 }else if( pExpr->pList ){
831 /* Case 2: expr IN (exprlist)
832 **
danielk1977e014a832004-05-17 10:48:57 +0000833 ** For each expression, build an index key from the evaluation and
834 ** store it in the temporary table. If <expr> is a column, then use
835 ** that columns affinity when building index keys. If <expr> is not
836 ** a column, use numeric affinity.
drhfef52082000-06-06 01:50:43 +0000837 */
danielk1977e014a832004-05-17 10:48:57 +0000838 int i;
839 char const *affStr;
840 if( !affinity ){
841 affinity = SQLITE_AFF_NUMERIC;
842 }
843 affStr = sqlite3AffinityString(affinity);
844
845 /* Loop through each expression in <exprlist>. */
drhfef52082000-06-06 01:50:43 +0000846 for(i=0; i<pExpr->pList->nExpr; i++){
847 Expr *pE2 = pExpr->pList->a[i].pExpr;
danielk1977e014a832004-05-17 10:48:57 +0000848
849 /* Check that the expression is constant and valid. */
danielk19774adee202004-05-08 08:23:19 +0000850 if( !sqlite3ExprIsConstant(pE2) ){
851 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +0000852 "right-hand side of IN operator must be constant");
drhfef52082000-06-06 01:50:43 +0000853 return 1;
854 }
danielk19774adee202004-05-08 08:23:19 +0000855 if( sqlite3ExprCheck(pParse, pE2, 0, 0) ){
drh4794b982000-06-06 13:54:14 +0000856 return 1;
857 }
danielk1977e014a832004-05-17 10:48:57 +0000858
859 /* Evaluate the expression and insert it into the temp table */
860 sqlite3ExprCode(pParse, pE2);
861 sqlite3VdbeOp3(v, OP_MakeKey, 1, 0, affStr, P3_STATIC);
862 sqlite3VdbeAddOp(v, OP_String, 0, 0);
863 sqlite3VdbeAddOp(v, OP_PutStrKey, pExpr->iTable, 0);
drhfef52082000-06-06 01:50:43 +0000864 }
865 }
drhcfab11b2000-06-06 03:31:22 +0000866 break;
drhfef52082000-06-06 01:50:43 +0000867 }
868
drh19a775c2000-06-05 18:54:46 +0000869 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +0000870 /* This has to be a scalar SELECT. Generate code to put the
871 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +0000872 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +0000873 */
drh967e8b72000-06-21 13:59:10 +0000874 pExpr->iColumn = pParse->nMem++;
danielk1977bf3b7212004-05-18 10:06:24 +0000875 if(sqlite3Select(pParse, pExpr->pSelect, SRT_Mem,pExpr->iColumn,0,0,0,0)){
drh19a775c2000-06-05 18:54:46 +0000876 return 1;
877 }
878 break;
879 }
880
drhcce7d172000-05-31 15:34:51 +0000881 /* For all else, just recursively walk the tree */
882 default: {
drh4794b982000-06-06 13:54:14 +0000883 if( pExpr->pLeft
danielk19774adee202004-05-08 08:23:19 +0000884 && sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pLeft) ){
drhcce7d172000-05-31 15:34:51 +0000885 return 1;
886 }
887 if( pExpr->pRight
danielk19774adee202004-05-08 08:23:19 +0000888 && sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pRight) ){
drhcce7d172000-05-31 15:34:51 +0000889 return 1;
890 }
891 if( pExpr->pList ){
892 int i;
893 ExprList *pList = pExpr->pList;
894 for(i=0; i<pList->nExpr; i++){
drh832508b2002-03-02 17:04:07 +0000895 Expr *pArg = pList->a[i].pExpr;
danielk19774adee202004-05-08 08:23:19 +0000896 if( sqlite3ExprResolveIds(pParse, pSrcList, pEList, pArg) ){
drhcce7d172000-05-31 15:34:51 +0000897 return 1;
898 }
899 }
900 }
901 }
902 }
903 return 0;
904}
905
drhcce7d172000-05-31 15:34:51 +0000906/*
drh4b59ab52002-08-24 18:24:51 +0000907** pExpr is a node that defines a function of some kind. It might
908** be a syntactic function like "count(x)" or it might be a function
909** that implements an operator, like "a LIKE b".
910**
911** This routine makes *pzName point to the name of the function and
912** *pnName hold the number of characters in the function name.
913*/
914static void getFunctionName(Expr *pExpr, const char **pzName, int *pnName){
915 switch( pExpr->op ){
916 case TK_FUNCTION: {
917 *pzName = pExpr->token.z;
drh6977fea2002-10-22 23:38:04 +0000918 *pnName = pExpr->token.n;
drh4b59ab52002-08-24 18:24:51 +0000919 break;
920 }
921 case TK_LIKE: {
922 *pzName = "like";
923 *pnName = 4;
924 break;
925 }
926 case TK_GLOB: {
927 *pzName = "glob";
928 *pnName = 4;
929 break;
930 }
931 default: {
932 *pzName = "can't happen";
933 *pnName = 12;
934 break;
935 }
936 }
937}
938
939/*
drhcce7d172000-05-31 15:34:51 +0000940** Error check the functions in an expression. Make sure all
941** function names are recognized and all functions have the correct
942** number of arguments. Leave an error message in pParse->zErrMsg
943** if anything is amiss. Return the number of errors.
944**
945** if pIsAgg is not null and this expression is an aggregate function
946** (like count(*) or max(value)) then write a 1 into *pIsAgg.
947*/
danielk19774adee202004-05-08 08:23:19 +0000948int sqlite3ExprCheck(Parse *pParse, Expr *pExpr, int allowAgg, int *pIsAgg){
drhcce7d172000-05-31 15:34:51 +0000949 int nErr = 0;
950 if( pExpr==0 ) return 0;
drhcce7d172000-05-31 15:34:51 +0000951 switch( pExpr->op ){
drh4b59ab52002-08-24 18:24:51 +0000952 case TK_GLOB:
953 case TK_LIKE:
drhcce7d172000-05-31 15:34:51 +0000954 case TK_FUNCTION: {
drhc9b84a12002-06-20 11:36:48 +0000955 int n = pExpr->pList ? pExpr->pList->nExpr : 0; /* Number of arguments */
956 int no_such_func = 0; /* True if no such function exists */
drhc9b84a12002-06-20 11:36:48 +0000957 int wrong_num_args = 0; /* True if wrong number of arguments */
958 int is_agg = 0; /* True if is an aggregate function */
drhcce7d172000-05-31 15:34:51 +0000959 int i;
drh4b59ab52002-08-24 18:24:51 +0000960 int nId; /* Number of characters in function name */
961 const char *zId; /* The function name. */
drh0bce8352002-02-28 00:41:10 +0000962 FuncDef *pDef;
963
drh4b59ab52002-08-24 18:24:51 +0000964 getFunctionName(pExpr, &zId, &nId);
danielk19774adee202004-05-08 08:23:19 +0000965 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, 0);
drh0bce8352002-02-28 00:41:10 +0000966 if( pDef==0 ){
danielk19774adee202004-05-08 08:23:19 +0000967 pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, 0);
drh0bce8352002-02-28 00:41:10 +0000968 if( pDef==0 ){
drh268380c2004-02-25 13:47:31 +0000969 no_such_func = 1;
drh0bce8352002-02-28 00:41:10 +0000970 }else{
971 wrong_num_args = 1;
drhcce7d172000-05-31 15:34:51 +0000972 }
drh0bce8352002-02-28 00:41:10 +0000973 }else{
974 is_agg = pDef->xFunc==0;
drhcce7d172000-05-31 15:34:51 +0000975 }
drh8e0a2f92002-02-23 23:45:45 +0000976 if( is_agg && !allowAgg ){
danielk19774adee202004-05-08 08:23:19 +0000977 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId, zId);
drh8e0a2f92002-02-23 23:45:45 +0000978 nErr++;
979 is_agg = 0;
980 }else if( no_such_func ){
danielk19774adee202004-05-08 08:23:19 +0000981 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
drhcce7d172000-05-31 15:34:51 +0000982 nErr++;
drh8e0a2f92002-02-23 23:45:45 +0000983 }else if( wrong_num_args ){
danielk19774adee202004-05-08 08:23:19 +0000984 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
drhf7a9e1a2004-02-22 18:40:56 +0000985 nId, zId);
drh8e0a2f92002-02-23 23:45:45 +0000986 nErr++;
drhcce7d172000-05-31 15:34:51 +0000987 }
drhf7a9e1a2004-02-22 18:40:56 +0000988 if( is_agg ){
989 pExpr->op = TK_AGG_FUNCTION;
990 if( pIsAgg ) *pIsAgg = 1;
991 }
drhcce7d172000-05-31 15:34:51 +0000992 for(i=0; nErr==0 && i<n; i++){
danielk19774adee202004-05-08 08:23:19 +0000993 nErr = sqlite3ExprCheck(pParse, pExpr->pList->a[i].pExpr,
drh4cfa7932000-06-08 15:10:46 +0000994 allowAgg && !is_agg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +0000995 }
drhc9b84a12002-06-20 11:36:48 +0000996 if( pDef==0 ){
drh268380c2004-02-25 13:47:31 +0000997 /* Already reported an error */
drhc9b84a12002-06-20 11:36:48 +0000998 }else if( pDef->dataType>=0 ){
999 if( pDef->dataType<n ){
1000 pExpr->dataType =
danielk19774adee202004-05-08 08:23:19 +00001001 sqlite3ExprType(pExpr->pList->a[pDef->dataType].pExpr);
drhc9b84a12002-06-20 11:36:48 +00001002 }else{
1003 pExpr->dataType = SQLITE_SO_NUM;
1004 }
1005 }else if( pDef->dataType==SQLITE_ARGS ){
1006 pDef->dataType = SQLITE_SO_TEXT;
1007 for(i=0; i<n; i++){
danielk19774adee202004-05-08 08:23:19 +00001008 if( sqlite3ExprType(pExpr->pList->a[i].pExpr)==SQLITE_SO_NUM ){
drhc9b84a12002-06-20 11:36:48 +00001009 pExpr->dataType = SQLITE_SO_NUM;
1010 break;
1011 }
1012 }
1013 }else if( pDef->dataType==SQLITE_NUMERIC ){
1014 pExpr->dataType = SQLITE_SO_NUM;
1015 }else{
1016 pExpr->dataType = SQLITE_SO_TEXT;
1017 }
drhcce7d172000-05-31 15:34:51 +00001018 }
1019 default: {
1020 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001021 nErr = sqlite3ExprCheck(pParse, pExpr->pLeft, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +00001022 }
1023 if( nErr==0 && pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001024 nErr = sqlite3ExprCheck(pParse, pExpr->pRight, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +00001025 }
drhfef52082000-06-06 01:50:43 +00001026 if( nErr==0 && pExpr->pList ){
1027 int n = pExpr->pList->nExpr;
1028 int i;
1029 for(i=0; nErr==0 && i<n; i++){
drh22827922000-06-06 17:27:05 +00001030 Expr *pE2 = pExpr->pList->a[i].pExpr;
danielk19774adee202004-05-08 08:23:19 +00001031 nErr = sqlite3ExprCheck(pParse, pE2, allowAgg, pIsAgg);
drhfef52082000-06-06 01:50:43 +00001032 }
1033 }
drhcce7d172000-05-31 15:34:51 +00001034 break;
1035 }
1036 }
1037 return nErr;
1038}
1039
1040/*
drhc9b84a12002-06-20 11:36:48 +00001041** Return either SQLITE_SO_NUM or SQLITE_SO_TEXT to indicate whether the
1042** given expression should sort as numeric values or as text.
1043**
danielk19774adee202004-05-08 08:23:19 +00001044** The sqlite3ExprResolveIds() and sqlite3ExprCheck() routines must have
drhc9b84a12002-06-20 11:36:48 +00001045** both been called on the expression before it is passed to this routine.
1046*/
danielk19774adee202004-05-08 08:23:19 +00001047int sqlite3ExprType(Expr *p){
drhc9b84a12002-06-20 11:36:48 +00001048 if( p==0 ) return SQLITE_SO_NUM;
1049 while( p ) switch( p->op ){
1050 case TK_PLUS:
1051 case TK_MINUS:
1052 case TK_STAR:
1053 case TK_SLASH:
1054 case TK_AND:
1055 case TK_OR:
1056 case TK_ISNULL:
1057 case TK_NOTNULL:
1058 case TK_NOT:
1059 case TK_UMINUS:
drh4b59ab52002-08-24 18:24:51 +00001060 case TK_UPLUS:
drhc9b84a12002-06-20 11:36:48 +00001061 case TK_BITAND:
1062 case TK_BITOR:
1063 case TK_BITNOT:
1064 case TK_LSHIFT:
1065 case TK_RSHIFT:
1066 case TK_REM:
1067 case TK_INTEGER:
1068 case TK_FLOAT:
1069 case TK_IN:
1070 case TK_BETWEEN:
drh4b59ab52002-08-24 18:24:51 +00001071 case TK_GLOB:
1072 case TK_LIKE:
drhc9b84a12002-06-20 11:36:48 +00001073 return SQLITE_SO_NUM;
1074
1075 case TK_STRING:
1076 case TK_NULL:
1077 case TK_CONCAT:
drh50457892003-09-06 01:10:47 +00001078 case TK_VARIABLE:
drhc9b84a12002-06-20 11:36:48 +00001079 return SQLITE_SO_TEXT;
1080
1081 case TK_LT:
1082 case TK_LE:
1083 case TK_GT:
1084 case TK_GE:
1085 case TK_NE:
1086 case TK_EQ:
danielk19774adee202004-05-08 08:23:19 +00001087 if( sqlite3ExprType(p->pLeft)==SQLITE_SO_NUM ){
drhc9b84a12002-06-20 11:36:48 +00001088 return SQLITE_SO_NUM;
1089 }
1090 p = p->pRight;
1091 break;
1092
1093 case TK_AS:
1094 p = p->pLeft;
1095 break;
1096
1097 case TK_COLUMN:
1098 case TK_FUNCTION:
1099 case TK_AGG_FUNCTION:
1100 return p->dataType;
1101
1102 case TK_SELECT:
1103 assert( p->pSelect );
1104 assert( p->pSelect->pEList );
1105 assert( p->pSelect->pEList->nExpr>0 );
1106 p = p->pSelect->pEList->a[0].pExpr;
1107 break;
1108
drhb1363202002-06-26 02:45:03 +00001109 case TK_CASE: {
danielk19774adee202004-05-08 08:23:19 +00001110 if( p->pRight && sqlite3ExprType(p->pRight)==SQLITE_SO_NUM ){
drhb1363202002-06-26 02:45:03 +00001111 return SQLITE_SO_NUM;
1112 }
1113 if( p->pList ){
1114 int i;
1115 ExprList *pList = p->pList;
1116 for(i=1; i<pList->nExpr; i+=2){
danielk19774adee202004-05-08 08:23:19 +00001117 if( sqlite3ExprType(pList->a[i].pExpr)==SQLITE_SO_NUM ){
drhb1363202002-06-26 02:45:03 +00001118 return SQLITE_SO_NUM;
1119 }
1120 }
1121 }
1122 return SQLITE_SO_TEXT;
1123 }
1124
drhc9b84a12002-06-20 11:36:48 +00001125 default:
1126 assert( p->op==TK_ABORT ); /* Can't Happen */
1127 break;
1128 }
1129 return SQLITE_SO_NUM;
1130}
1131
1132/*
drhfec19aa2004-05-19 20:41:03 +00001133** Generate an instruction that will put the integer describe by
1134** text z[0..n-1] on the stack.
1135*/
1136static void codeInteger(Vdbe *v, const char *z, int n){
1137 int i;
1138 if( sqlite3GetInt32(z, &i) || (i=0, sqlite3FitsIn64Bits(z))!=0 ){
1139 sqlite3VdbeOp3(v, OP_Integer, i, 0, z, n);
1140 }else{
1141 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n);
1142 }
1143}
1144
1145/*
drhcce7d172000-05-31 15:34:51 +00001146** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +00001147** expression and leave the result on the top of stack.
drhcce7d172000-05-31 15:34:51 +00001148*/
danielk19774adee202004-05-08 08:23:19 +00001149void sqlite3ExprCode(Parse *pParse, Expr *pExpr){
drhcce7d172000-05-31 15:34:51 +00001150 Vdbe *v = pParse->pVdbe;
1151 int op;
drhdaffd0e2001-04-11 14:28:42 +00001152 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001153 switch( pExpr->op ){
1154 case TK_PLUS: op = OP_Add; break;
1155 case TK_MINUS: op = OP_Subtract; break;
1156 case TK_STAR: op = OP_Multiply; break;
1157 case TK_SLASH: op = OP_Divide; break;
1158 case TK_AND: op = OP_And; break;
1159 case TK_OR: op = OP_Or; break;
1160 case TK_LT: op = OP_Lt; break;
1161 case TK_LE: op = OP_Le; break;
1162 case TK_GT: op = OP_Gt; break;
1163 case TK_GE: op = OP_Ge; break;
1164 case TK_NE: op = OP_Ne; break;
1165 case TK_EQ: op = OP_Eq; break;
drhcce7d172000-05-31 15:34:51 +00001166 case TK_ISNULL: op = OP_IsNull; break;
1167 case TK_NOTNULL: op = OP_NotNull; break;
1168 case TK_NOT: op = OP_Not; break;
1169 case TK_UMINUS: op = OP_Negative; break;
drhbf4133c2001-10-13 02:59:08 +00001170 case TK_BITAND: op = OP_BitAnd; break;
1171 case TK_BITOR: op = OP_BitOr; break;
1172 case TK_BITNOT: op = OP_BitNot; break;
1173 case TK_LSHIFT: op = OP_ShiftLeft; break;
1174 case TK_RSHIFT: op = OP_ShiftRight; break;
1175 case TK_REM: op = OP_Remainder; break;
drhfec19aa2004-05-19 20:41:03 +00001176 case TK_FLOAT: op = OP_Real; break;
1177 case TK_STRING: op = OP_String; break;
drhcce7d172000-05-31 15:34:51 +00001178 default: break;
1179 }
1180 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00001181 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +00001182 if( pParse->useAgg ){
danielk19774adee202004-05-08 08:23:19 +00001183 sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drhc4a3c772001-04-04 11:48:57 +00001184 }else if( pExpr->iColumn>=0 ){
danielk19774adee202004-05-08 08:23:19 +00001185 sqlite3VdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn);
drhc4a3c772001-04-04 11:48:57 +00001186 }else{
danielk19774adee202004-05-08 08:23:19 +00001187 sqlite3VdbeAddOp(v, OP_Recno, pExpr->iTable, 0);
drh22827922000-06-06 17:27:05 +00001188 }
drhcce7d172000-05-31 15:34:51 +00001189 break;
1190 }
1191 case TK_INTEGER: {
drhfec19aa2004-05-19 20:41:03 +00001192 codeInteger(v, pExpr->token.z, pExpr->token.n);
1193 break;
1194 }
1195 case TK_FLOAT:
1196 case TK_STRING: {
1197 sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z, pExpr->token.n);
danielk19774adee202004-05-08 08:23:19 +00001198 sqlite3VdbeDequoteP3(v, -1);
drhcce7d172000-05-31 15:34:51 +00001199 break;
1200 }
1201 case TK_NULL: {
danielk19774adee202004-05-08 08:23:19 +00001202 sqlite3VdbeAddOp(v, OP_String, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001203 break;
1204 }
drh50457892003-09-06 01:10:47 +00001205 case TK_VARIABLE: {
danielk19774adee202004-05-08 08:23:19 +00001206 sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0);
drh50457892003-09-06 01:10:47 +00001207 break;
1208 }
drhc9b84a12002-06-20 11:36:48 +00001209 case TK_LT:
1210 case TK_LE:
1211 case TK_GT:
1212 case TK_GE:
1213 case TK_NE:
1214 case TK_EQ: {
danielk1977a37cdde2004-05-16 11:15:36 +00001215 int p1 = binaryCompareP1(pExpr->pLeft, pExpr->pRight, 0);
1216 sqlite3ExprCode(pParse, pExpr->pLeft);
1217 sqlite3ExprCode(pParse, pExpr->pRight);
1218 sqlite3VdbeAddOp(v, op, p1, 0);
1219 break;
drhc9b84a12002-06-20 11:36:48 +00001220 }
drhcce7d172000-05-31 15:34:51 +00001221 case TK_AND:
1222 case TK_OR:
1223 case TK_PLUS:
1224 case TK_STAR:
1225 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00001226 case TK_REM:
1227 case TK_BITAND:
1228 case TK_BITOR:
drhc9b84a12002-06-20 11:36:48 +00001229 case TK_SLASH: {
danielk19774adee202004-05-08 08:23:19 +00001230 sqlite3ExprCode(pParse, pExpr->pLeft);
1231 sqlite3ExprCode(pParse, pExpr->pRight);
1232 sqlite3VdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001233 break;
1234 }
drhbf4133c2001-10-13 02:59:08 +00001235 case TK_LSHIFT:
1236 case TK_RSHIFT: {
danielk19774adee202004-05-08 08:23:19 +00001237 sqlite3ExprCode(pParse, pExpr->pRight);
1238 sqlite3ExprCode(pParse, pExpr->pLeft);
1239 sqlite3VdbeAddOp(v, op, 0, 0);
drhbf4133c2001-10-13 02:59:08 +00001240 break;
1241 }
drh00400772000-06-16 20:51:26 +00001242 case TK_CONCAT: {
danielk19774adee202004-05-08 08:23:19 +00001243 sqlite3ExprCode(pParse, pExpr->pLeft);
1244 sqlite3ExprCode(pParse, pExpr->pRight);
1245 sqlite3VdbeAddOp(v, OP_Concat, 2, 0);
drh00400772000-06-16 20:51:26 +00001246 break;
1247 }
drhcce7d172000-05-31 15:34:51 +00001248 case TK_UMINUS: {
drhfec19aa2004-05-19 20:41:03 +00001249 Expr *pLeft = pExpr->pLeft;
1250 assert( pLeft );
1251 if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
1252 Token *p = &pLeft->token;
drh6e142f52000-06-08 13:36:40 +00001253 char *z = sqliteMalloc( p->n + 2 );
1254 sprintf(z, "-%.*s", p->n, p->z);
drhfec19aa2004-05-19 20:41:03 +00001255 if( pLeft->op==TK_FLOAT ){
1256 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001257 }else{
drhfec19aa2004-05-19 20:41:03 +00001258 codeInteger(v, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001259 }
drh6e142f52000-06-08 13:36:40 +00001260 sqliteFree(z);
1261 break;
1262 }
drh1ccde152000-06-17 13:12:39 +00001263 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +00001264 }
drhbf4133c2001-10-13 02:59:08 +00001265 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00001266 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001267 sqlite3ExprCode(pParse, pExpr->pLeft);
1268 sqlite3VdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001269 break;
1270 }
1271 case TK_ISNULL:
1272 case TK_NOTNULL: {
1273 int dest;
danielk19774adee202004-05-08 08:23:19 +00001274 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1275 sqlite3ExprCode(pParse, pExpr->pLeft);
1276 dest = sqlite3VdbeCurrentAddr(v) + 2;
1277 sqlite3VdbeAddOp(v, op, 1, dest);
1278 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);
drhcce7d172000-05-31 15:34:51 +00001279 }
danielk1977a37cdde2004-05-16 11:15:36 +00001280 break;
drh22827922000-06-06 17:27:05 +00001281 case TK_AGG_FUNCTION: {
danielk19774adee202004-05-08 08:23:19 +00001282 sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drh22827922000-06-06 17:27:05 +00001283 break;
1284 }
drh4b59ab52002-08-24 18:24:51 +00001285 case TK_GLOB:
1286 case TK_LIKE:
drhcce7d172000-05-31 15:34:51 +00001287 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00001288 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00001289 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00001290 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00001291 int nId;
1292 const char *zId;
1293 getFunctionName(pExpr, &zId, &nId);
danielk19774adee202004-05-08 08:23:19 +00001294 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, 0);
drh0bce8352002-02-28 00:41:10 +00001295 assert( pDef!=0 );
danielk19774adee202004-05-08 08:23:19 +00001296 nExpr = sqlite3ExprCodeExprList(pParse, pList, pDef->includeTypes);
danielk1977a37cdde2004-05-16 11:15:36 +00001297 /* FIX ME: The following is a temporary hack. */
1298 if( 0==sqlite3StrNICmp(zId, "classof", nId) ){
1299 assert( nExpr==1 );
drhfec19aa2004-05-19 20:41:03 +00001300 sqlite3VdbeAddOp(v, OP_Class, nExpr, 0);
danielk1977a37cdde2004-05-16 11:15:36 +00001301 }else{
1302 sqlite3VdbeOp3(v, OP_Function, nExpr, 0, (char*)pDef, P3_POINTER);
1303 }
drhcce7d172000-05-31 15:34:51 +00001304 break;
1305 }
drh19a775c2000-06-05 18:54:46 +00001306 case TK_SELECT: {
danielk19774adee202004-05-08 08:23:19 +00001307 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drh19a775c2000-06-05 18:54:46 +00001308 break;
1309 }
drhfef52082000-06-06 01:50:43 +00001310 case TK_IN: {
1311 int addr;
danielk1977e014a832004-05-17 10:48:57 +00001312 char const *affStr;
1313
1314 /* Figure out the affinity to use to create a key from the results
1315 ** of the expression. affinityStr stores a static string suitable for
1316 ** P3 of OP_MakeKey.
1317 */
1318 affStr = sqlite3AffinityString(comparisonAffinity(pExpr));
1319
danielk19774adee202004-05-08 08:23:19 +00001320 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
danielk1977e014a832004-05-17 10:48:57 +00001321
1322 /* Code the <expr> from "<expr> IN (...)". The temporary table
1323 ** pExpr->iTable contains the values that make up the (...) set.
1324 */
danielk19774adee202004-05-08 08:23:19 +00001325 sqlite3ExprCode(pParse, pExpr->pLeft);
1326 addr = sqlite3VdbeCurrentAddr(v);
danielk1977e014a832004-05-17 10:48:57 +00001327 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); /* addr + 0 */
danielk19774adee202004-05-08 08:23:19 +00001328 sqlite3VdbeAddOp(v, OP_Pop, 2, 0);
1329 sqlite3VdbeAddOp(v, OP_String, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +00001330 sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7);
1331 sqlite3VdbeOp3(v, OP_MakeKey, 1, 0, affStr, P3_STATIC); /* addr + 4 */
1332 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7);
1333 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); /* addr + 6 */
1334
drhfef52082000-06-06 01:50:43 +00001335 break;
1336 }
1337 case TK_BETWEEN: {
danielk19774adee202004-05-08 08:23:19 +00001338 sqlite3ExprCode(pParse, pExpr->pLeft);
1339 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
1340 sqlite3ExprCode(pParse, pExpr->pList->a[0].pExpr);
1341 sqlite3VdbeAddOp(v, OP_Ge, 0, 0);
1342 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
1343 sqlite3ExprCode(pParse, pExpr->pList->a[1].pExpr);
1344 sqlite3VdbeAddOp(v, OP_Le, 0, 0);
1345 sqlite3VdbeAddOp(v, OP_And, 0, 0);
drhfef52082000-06-06 01:50:43 +00001346 break;
1347 }
drh51e9a442004-01-16 16:42:53 +00001348 case TK_UPLUS:
drha2e00042002-01-22 03:13:42 +00001349 case TK_AS: {
danielk19774adee202004-05-08 08:23:19 +00001350 sqlite3ExprCode(pParse, pExpr->pLeft);
drha2e00042002-01-22 03:13:42 +00001351 break;
1352 }
drh17a7f8d2002-03-24 13:13:27 +00001353 case TK_CASE: {
1354 int expr_end_label;
drhf5905aa2002-05-26 20:54:33 +00001355 int jumpInst;
1356 int addr;
1357 int nExpr;
drh17a7f8d2002-03-24 13:13:27 +00001358 int i;
1359
1360 assert(pExpr->pList);
1361 assert((pExpr->pList->nExpr % 2) == 0);
1362 assert(pExpr->pList->nExpr > 0);
drhf5905aa2002-05-26 20:54:33 +00001363 nExpr = pExpr->pList->nExpr;
danielk19774adee202004-05-08 08:23:19 +00001364 expr_end_label = sqlite3VdbeMakeLabel(v);
drh17a7f8d2002-03-24 13:13:27 +00001365 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001366 sqlite3ExprCode(pParse, pExpr->pLeft);
drh17a7f8d2002-03-24 13:13:27 +00001367 }
drhf5905aa2002-05-26 20:54:33 +00001368 for(i=0; i<nExpr; i=i+2){
danielk19774adee202004-05-08 08:23:19 +00001369 sqlite3ExprCode(pParse, pExpr->pList->a[i].pExpr);
drh17a7f8d2002-03-24 13:13:27 +00001370 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001371 sqlite3VdbeAddOp(v, OP_Dup, 1, 1);
1372 jumpInst = sqlite3VdbeAddOp(v, OP_Ne, 1, 0);
1373 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001374 }else{
danielk19774adee202004-05-08 08:23:19 +00001375 jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001376 }
danielk19774adee202004-05-08 08:23:19 +00001377 sqlite3ExprCode(pParse, pExpr->pList->a[i+1].pExpr);
1378 sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label);
1379 addr = sqlite3VdbeCurrentAddr(v);
1380 sqlite3VdbeChangeP2(v, jumpInst, addr);
drh17a7f8d2002-03-24 13:13:27 +00001381 }
drhf570f012002-05-31 15:51:25 +00001382 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001383 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhf570f012002-05-31 15:51:25 +00001384 }
drh17a7f8d2002-03-24 13:13:27 +00001385 if( pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001386 sqlite3ExprCode(pParse, pExpr->pRight);
drh17a7f8d2002-03-24 13:13:27 +00001387 }else{
danielk19774adee202004-05-08 08:23:19 +00001388 sqlite3VdbeAddOp(v, OP_String, 0, 0);
drh17a7f8d2002-03-24 13:13:27 +00001389 }
danielk19774adee202004-05-08 08:23:19 +00001390 sqlite3VdbeResolveLabel(v, expr_end_label);
danielk19776f349032002-06-11 02:25:40 +00001391 break;
1392 }
1393 case TK_RAISE: {
1394 if( !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +00001395 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00001396 "RAISE() may only be used within a trigger-program");
danielk19776f349032002-06-11 02:25:40 +00001397 pParse->nErr++;
1398 return;
1399 }
1400 if( pExpr->iColumn == OE_Rollback ||
1401 pExpr->iColumn == OE_Abort ||
1402 pExpr->iColumn == OE_Fail ){
danielk19774adee202004-05-08 08:23:19 +00001403 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn,
drh701a0ae2004-02-22 20:05:00 +00001404 pExpr->token.z, pExpr->token.n);
danielk19774adee202004-05-08 08:23:19 +00001405 sqlite3VdbeDequoteP3(v, -1);
danielk19776f349032002-06-11 02:25:40 +00001406 } else {
1407 assert( pExpr->iColumn == OE_Ignore );
danielk19774adee202004-05-08 08:23:19 +00001408 sqlite3VdbeOp3(v, OP_Goto, 0, pParse->trigStack->ignoreJump,
drh701a0ae2004-02-22 20:05:00 +00001409 "(IGNORE jump)", 0);
danielk19776f349032002-06-11 02:25:40 +00001410 }
drh17a7f8d2002-03-24 13:13:27 +00001411 }
1412 break;
drhcce7d172000-05-31 15:34:51 +00001413 }
drhcce7d172000-05-31 15:34:51 +00001414}
1415
1416/*
drh268380c2004-02-25 13:47:31 +00001417** Generate code that pushes the value of every element of the given
1418** expression list onto the stack. If the includeTypes flag is true,
1419** then also push a string that is the datatype of each element onto
1420** the stack after the value.
1421**
1422** Return the number of elements pushed onto the stack.
1423*/
danielk19774adee202004-05-08 08:23:19 +00001424int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00001425 Parse *pParse, /* Parsing context */
1426 ExprList *pList, /* The expression list to be coded */
1427 int includeTypes /* TRUE to put datatypes on the stack too */
1428){
1429 struct ExprList_item *pItem;
1430 int i, n;
1431 Vdbe *v;
1432 if( pList==0 ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001433 v = sqlite3GetVdbe(pParse);
drh268380c2004-02-25 13:47:31 +00001434 n = pList->nExpr;
1435 for(pItem=pList->a, i=0; i<n; i++, pItem++){
danielk19774adee202004-05-08 08:23:19 +00001436 sqlite3ExprCode(pParse, pItem->pExpr);
drh268380c2004-02-25 13:47:31 +00001437 if( includeTypes ){
danielk19774adee202004-05-08 08:23:19 +00001438 sqlite3VdbeOp3(v, OP_String, 0, 0,
1439 sqlite3ExprType(pItem->pExpr)==SQLITE_SO_NUM ? "numeric" : "text",
drh268380c2004-02-25 13:47:31 +00001440 P3_STATIC);
1441 }
1442 }
1443 return includeTypes ? n*2 : n;
1444}
1445
1446/*
drhcce7d172000-05-31 15:34:51 +00001447** Generate code for a boolean expression such that a jump is made
1448** to the label "dest" if the expression is true but execution
1449** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00001450**
1451** If the expression evaluates to NULL (neither true nor false), then
1452** take the jump if the jumpIfNull flag is true.
drhcce7d172000-05-31 15:34:51 +00001453*/
danielk19774adee202004-05-08 08:23:19 +00001454void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001455 Vdbe *v = pParse->pVdbe;
1456 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001457 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001458 switch( pExpr->op ){
1459 case TK_LT: op = OP_Lt; break;
1460 case TK_LE: op = OP_Le; break;
1461 case TK_GT: op = OP_Gt; break;
1462 case TK_GE: op = OP_Ge; break;
1463 case TK_NE: op = OP_Ne; break;
1464 case TK_EQ: op = OP_Eq; break;
drhcce7d172000-05-31 15:34:51 +00001465 case TK_ISNULL: op = OP_IsNull; break;
1466 case TK_NOTNULL: op = OP_NotNull; break;
1467 default: break;
1468 }
1469 switch( pExpr->op ){
1470 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001471 int d2 = sqlite3VdbeMakeLabel(v);
1472 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
1473 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
1474 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001475 break;
1476 }
1477 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001478 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
1479 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001480 break;
1481 }
1482 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001483 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001484 break;
1485 }
1486 case TK_LT:
1487 case TK_LE:
1488 case TK_GT:
1489 case TK_GE:
1490 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00001491 case TK_EQ: {
danielk1977a37cdde2004-05-16 11:15:36 +00001492 int p1 = binaryCompareP1(pExpr->pLeft, pExpr->pRight, jumpIfNull);
danielk19774adee202004-05-08 08:23:19 +00001493 sqlite3ExprCode(pParse, pExpr->pLeft);
1494 sqlite3ExprCode(pParse, pExpr->pRight);
danielk1977a37cdde2004-05-16 11:15:36 +00001495 sqlite3VdbeAddOp(v, op, p1, dest);
drhcce7d172000-05-31 15:34:51 +00001496 break;
1497 }
1498 case TK_ISNULL:
1499 case TK_NOTNULL: {
danielk19774adee202004-05-08 08:23:19 +00001500 sqlite3ExprCode(pParse, pExpr->pLeft);
1501 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001502 break;
1503 }
danielk1977e014a832004-05-17 10:48:57 +00001504#if 0
drhfef52082000-06-06 01:50:43 +00001505 case TK_IN: {
drhf5905aa2002-05-26 20:54:33 +00001506 int addr;
danielk19774adee202004-05-08 08:23:19 +00001507 sqlite3ExprCode(pParse, pExpr->pLeft);
1508 addr = sqlite3VdbeCurrentAddr(v);
1509 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+3);
1510 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
1511 sqlite3VdbeAddOp(v, OP_Goto, 0, jumpIfNull ? dest : addr+4);
drhfef52082000-06-06 01:50:43 +00001512 if( pExpr->pSelect ){
danielk19774adee202004-05-08 08:23:19 +00001513 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001514 }else{
danielk19774adee202004-05-08 08:23:19 +00001515 sqlite3VdbeAddOp(v, OP_SetFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001516 }
1517 break;
1518 }
danielk1977e014a832004-05-17 10:48:57 +00001519#endif
drhfef52082000-06-06 01:50:43 +00001520 case TK_BETWEEN: {
drhf5905aa2002-05-26 20:54:33 +00001521 int addr;
danielk19774adee202004-05-08 08:23:19 +00001522 sqlite3ExprCode(pParse, pExpr->pLeft);
1523 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
1524 sqlite3ExprCode(pParse, pExpr->pList->a[0].pExpr);
1525 addr = sqlite3VdbeAddOp(v, OP_Lt, !jumpIfNull, 0);
1526 sqlite3ExprCode(pParse, pExpr->pList->a[1].pExpr);
1527 sqlite3VdbeAddOp(v, OP_Le, jumpIfNull, dest);
1528 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
1529 sqlite3VdbeChangeP2(v, addr, sqlite3VdbeCurrentAddr(v));
1530 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00001531 break;
1532 }
drhcce7d172000-05-31 15:34:51 +00001533 default: {
danielk19774adee202004-05-08 08:23:19 +00001534 sqlite3ExprCode(pParse, pExpr);
1535 sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001536 break;
1537 }
1538 }
1539}
1540
1541/*
drh66b89c82000-11-28 20:47:17 +00001542** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00001543** to the label "dest" if the expression is false but execution
1544** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00001545**
1546** If the expression evaluates to NULL (neither true nor false) then
1547** jump if jumpIfNull is true or fall through if jumpIfNull is false.
drhcce7d172000-05-31 15:34:51 +00001548*/
danielk19774adee202004-05-08 08:23:19 +00001549void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001550 Vdbe *v = pParse->pVdbe;
1551 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001552 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001553 switch( pExpr->op ){
1554 case TK_LT: op = OP_Ge; break;
1555 case TK_LE: op = OP_Gt; break;
1556 case TK_GT: op = OP_Le; break;
1557 case TK_GE: op = OP_Lt; break;
1558 case TK_NE: op = OP_Eq; break;
1559 case TK_EQ: op = OP_Ne; break;
drhcce7d172000-05-31 15:34:51 +00001560 case TK_ISNULL: op = OP_NotNull; break;
1561 case TK_NOTNULL: op = OP_IsNull; break;
1562 default: break;
1563 }
1564 switch( pExpr->op ){
1565 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001566 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
1567 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001568 break;
1569 }
1570 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001571 int d2 = sqlite3VdbeMakeLabel(v);
1572 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
1573 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
1574 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001575 break;
1576 }
1577 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001578 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001579 break;
1580 }
1581 case TK_LT:
1582 case TK_LE:
1583 case TK_GT:
1584 case TK_GE:
1585 case TK_NE:
1586 case TK_EQ: {
danielk1977a37cdde2004-05-16 11:15:36 +00001587 int p1 = binaryCompareP1(pExpr->pLeft, pExpr->pRight, jumpIfNull);
danielk19774adee202004-05-08 08:23:19 +00001588 sqlite3ExprCode(pParse, pExpr->pLeft);
1589 sqlite3ExprCode(pParse, pExpr->pRight);
danielk1977a37cdde2004-05-16 11:15:36 +00001590 sqlite3VdbeAddOp(v, op, p1, dest);
drhcce7d172000-05-31 15:34:51 +00001591 break;
1592 }
drhcce7d172000-05-31 15:34:51 +00001593 case TK_ISNULL:
1594 case TK_NOTNULL: {
danielk19774adee202004-05-08 08:23:19 +00001595 sqlite3ExprCode(pParse, pExpr->pLeft);
1596 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001597 break;
1598 }
danielk1977e014a832004-05-17 10:48:57 +00001599#if 0
drhfef52082000-06-06 01:50:43 +00001600 case TK_IN: {
drhf5905aa2002-05-26 20:54:33 +00001601 int addr;
danielk19774adee202004-05-08 08:23:19 +00001602 sqlite3ExprCode(pParse, pExpr->pLeft);
1603 addr = sqlite3VdbeCurrentAddr(v);
1604 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+3);
1605 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
1606 sqlite3VdbeAddOp(v, OP_Goto, 0, jumpIfNull ? dest : addr+4);
drhfef52082000-06-06 01:50:43 +00001607 if( pExpr->pSelect ){
danielk19774adee202004-05-08 08:23:19 +00001608 sqlite3VdbeAddOp(v, OP_NotFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001609 }else{
danielk19774adee202004-05-08 08:23:19 +00001610 sqlite3VdbeAddOp(v, OP_SetNotFound, pExpr->iTable, dest);
drhfef52082000-06-06 01:50:43 +00001611 }
1612 break;
1613 }
danielk1977e014a832004-05-17 10:48:57 +00001614#endif
drhfef52082000-06-06 01:50:43 +00001615 case TK_BETWEEN: {
1616 int addr;
danielk19774adee202004-05-08 08:23:19 +00001617 sqlite3ExprCode(pParse, pExpr->pLeft);
1618 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
1619 sqlite3ExprCode(pParse, pExpr->pList->a[0].pExpr);
1620 addr = sqlite3VdbeCurrentAddr(v);
1621 sqlite3VdbeAddOp(v, OP_Ge, !jumpIfNull, addr+3);
1622 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
1623 sqlite3VdbeAddOp(v, OP_Goto, 0, dest);
1624 sqlite3ExprCode(pParse, pExpr->pList->a[1].pExpr);
1625 sqlite3VdbeAddOp(v, OP_Gt, jumpIfNull, dest);
drhfef52082000-06-06 01:50:43 +00001626 break;
1627 }
drhcce7d172000-05-31 15:34:51 +00001628 default: {
danielk19774adee202004-05-08 08:23:19 +00001629 sqlite3ExprCode(pParse, pExpr);
1630 sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001631 break;
1632 }
1633 }
1634}
drh22827922000-06-06 17:27:05 +00001635
1636/*
1637** Do a deep comparison of two expression trees. Return TRUE (non-zero)
1638** if they are identical and return FALSE if they differ in any way.
1639*/
danielk19774adee202004-05-08 08:23:19 +00001640int sqlite3ExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00001641 int i;
1642 if( pA==0 ){
1643 return pB==0;
1644 }else if( pB==0 ){
1645 return 0;
1646 }
1647 if( pA->op!=pB->op ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001648 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
1649 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00001650 if( pA->pList ){
1651 if( pB->pList==0 ) return 0;
1652 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
1653 for(i=0; i<pA->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00001654 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00001655 return 0;
1656 }
1657 }
1658 }else if( pB->pList ){
1659 return 0;
1660 }
1661 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00001662 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drh22827922000-06-06 17:27:05 +00001663 if( pA->token.z ){
1664 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00001665 if( pB->token.n!=pA->token.n ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001666 if( sqlite3StrNICmp(pA->token.z, pB->token.z, pB->token.n)!=0 ) return 0;
drh22827922000-06-06 17:27:05 +00001667 }
1668 return 1;
1669}
1670
1671/*
1672** Add a new element to the pParse->aAgg[] array and return its index.
1673*/
1674static int appendAggInfo(Parse *pParse){
1675 if( (pParse->nAgg & 0x7)==0 ){
1676 int amt = pParse->nAgg + 8;
drh6d4abfb2001-10-22 02:58:08 +00001677 AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0]));
1678 if( aAgg==0 ){
drh22827922000-06-06 17:27:05 +00001679 return -1;
1680 }
drh6d4abfb2001-10-22 02:58:08 +00001681 pParse->aAgg = aAgg;
drh22827922000-06-06 17:27:05 +00001682 }
1683 memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0]));
1684 return pParse->nAgg++;
1685}
1686
1687/*
1688** Analyze the given expression looking for aggregate functions and
1689** for variables that need to be added to the pParse->aAgg[] array.
1690** Make additional entries to the pParse->aAgg[] array as necessary.
1691**
1692** This routine should only be called after the expression has been
danielk19774adee202004-05-08 08:23:19 +00001693** analyzed by sqlite3ExprResolveIds() and sqlite3ExprCheck().
drh22827922000-06-06 17:27:05 +00001694**
1695** If errors are seen, leave an error message in zErrMsg and return
1696** the number of errors.
1697*/
danielk19774adee202004-05-08 08:23:19 +00001698int sqlite3ExprAnalyzeAggregates(Parse *pParse, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00001699 int i;
1700 AggExpr *aAgg;
1701 int nErr = 0;
1702
1703 if( pExpr==0 ) return 0;
1704 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00001705 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +00001706 aAgg = pParse->aAgg;
1707 for(i=0; i<pParse->nAgg; i++){
1708 if( aAgg[i].isAgg ) continue;
1709 if( aAgg[i].pExpr->iTable==pExpr->iTable
drh967e8b72000-06-21 13:59:10 +00001710 && aAgg[i].pExpr->iColumn==pExpr->iColumn ){
drh22827922000-06-06 17:27:05 +00001711 break;
1712 }
1713 }
1714 if( i>=pParse->nAgg ){
1715 i = appendAggInfo(pParse);
1716 if( i<0 ) return 1;
1717 pParse->aAgg[i].isAgg = 0;
1718 pParse->aAgg[i].pExpr = pExpr;
1719 }
drhaaf88722000-06-08 11:25:00 +00001720 pExpr->iAgg = i;
drh22827922000-06-06 17:27:05 +00001721 break;
1722 }
1723 case TK_AGG_FUNCTION: {
drh22827922000-06-06 17:27:05 +00001724 aAgg = pParse->aAgg;
1725 for(i=0; i<pParse->nAgg; i++){
1726 if( !aAgg[i].isAgg ) continue;
danielk19774adee202004-05-08 08:23:19 +00001727 if( sqlite3ExprCompare(aAgg[i].pExpr, pExpr) ){
drh22827922000-06-06 17:27:05 +00001728 break;
1729 }
1730 }
1731 if( i>=pParse->nAgg ){
1732 i = appendAggInfo(pParse);
1733 if( i<0 ) return 1;
1734 pParse->aAgg[i].isAgg = 1;
1735 pParse->aAgg[i].pExpr = pExpr;
danielk19774adee202004-05-08 08:23:19 +00001736 pParse->aAgg[i].pFunc = sqlite3FindFunction(pParse->db,
drh6977fea2002-10-22 23:38:04 +00001737 pExpr->token.z, pExpr->token.n,
drhf55f25f2002-02-28 01:46:11 +00001738 pExpr->pList ? pExpr->pList->nExpr : 0, 0);
drh22827922000-06-06 17:27:05 +00001739 }
1740 pExpr->iAgg = i;
1741 break;
1742 }
1743 default: {
1744 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001745 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pLeft);
drh22827922000-06-06 17:27:05 +00001746 }
1747 if( nErr==0 && pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001748 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pRight);
drh22827922000-06-06 17:27:05 +00001749 }
1750 if( nErr==0 && pExpr->pList ){
1751 int n = pExpr->pList->nExpr;
1752 int i;
1753 for(i=0; nErr==0 && i<n; i++){
danielk19774adee202004-05-08 08:23:19 +00001754 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pList->a[i].pExpr);
drh22827922000-06-06 17:27:05 +00001755 }
1756 }
1757 break;
1758 }
1759 }
1760 return nErr;
1761}
drh8e0a2f92002-02-23 23:45:45 +00001762
1763/*
1764** Locate a user function given a name and a number of arguments.
drh0bce8352002-02-28 00:41:10 +00001765** Return a pointer to the FuncDef structure that defines that
drh8e0a2f92002-02-23 23:45:45 +00001766** function, or return NULL if the function does not exist.
1767**
drh0bce8352002-02-28 00:41:10 +00001768** If the createFlag argument is true, then a new (blank) FuncDef
drh8e0a2f92002-02-23 23:45:45 +00001769** structure is created and liked into the "db" structure if a
1770** no matching function previously existed. When createFlag is true
1771** and the nArg parameter is -1, then only a function that accepts
1772** any number of arguments will be returned.
1773**
1774** If createFlag is false and nArg is -1, then the first valid
1775** function found is returned. A function is valid if either xFunc
1776** or xStep is non-zero.
1777*/
danielk19774adee202004-05-08 08:23:19 +00001778FuncDef *sqlite3FindFunction(
drh8e0a2f92002-02-23 23:45:45 +00001779 sqlite *db, /* An open database */
1780 const char *zName, /* Name of the function. Not null-terminated */
1781 int nName, /* Number of characters in the name */
1782 int nArg, /* Number of arguments. -1 means any number */
1783 int createFlag /* Create new entry if true and does not otherwise exist */
1784){
drh0bce8352002-02-28 00:41:10 +00001785 FuncDef *pFirst, *p, *pMaybe;
danielk19774adee202004-05-08 08:23:19 +00001786 pFirst = p = (FuncDef*)sqlite3HashFind(&db->aFunc, zName, nName);
drh1350b032002-02-27 19:00:20 +00001787 if( p && !createFlag && nArg<0 ){
drh8e0a2f92002-02-23 23:45:45 +00001788 while( p && p->xFunc==0 && p->xStep==0 ){ p = p->pNext; }
1789 return p;
1790 }
1791 pMaybe = 0;
1792 while( p && p->nArg!=nArg ){
1793 if( p->nArg<0 && !createFlag && (p->xFunc || p->xStep) ) pMaybe = p;
1794 p = p->pNext;
1795 }
1796 if( p && !createFlag && p->xFunc==0 && p->xStep==0 ){
1797 return 0;
1798 }
1799 if( p==0 && pMaybe ){
1800 assert( createFlag==0 );
1801 return pMaybe;
1802 }
drh89425d52002-02-28 03:04:48 +00001803 if( p==0 && createFlag && (p = sqliteMalloc(sizeof(*p)))!=0 ){
drh8e0a2f92002-02-23 23:45:45 +00001804 p->nArg = nArg;
1805 p->pNext = pFirst;
drhc9b84a12002-06-20 11:36:48 +00001806 p->dataType = pFirst ? pFirst->dataType : SQLITE_NUMERIC;
danielk19774adee202004-05-08 08:23:19 +00001807 sqlite3HashInsert(&db->aFunc, zName, nName, (void*)p);
drh8e0a2f92002-02-23 23:45:45 +00001808 }
1809 return p;
1810}