blob: 762b696c486290bda5ec0e0990f39899422ff1b6 [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**
danielk19777cedc8d2004-06-10 10:50:08 +000015** $Id: expr.c,v 1.138 2004/06/10 10:50:17 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
drh53db1452004-05-20 13:54:53 +000058/*
danielk19770202b292004-06-09 09:55:16 +000059** Return the default collation sequence for the expression pExpr. If
60** there is no default collation type, return 0.
61*/
danielk19777cedc8d2004-06-10 10:50:08 +000062CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
63 CollSeq *pColl = 0;
danielk19770202b292004-06-09 09:55:16 +000064 if( pExpr ){
danielk19777cedc8d2004-06-10 10:50:08 +000065 pColl = pExpr->pColl;
66 if( pExpr->op==TK_AS && !pColl ){
67 return sqlite3ExprCollSeq(pParse, pExpr->pLeft);
danielk19770202b292004-06-09 09:55:16 +000068 }
69 }
danielk19777cedc8d2004-06-10 10:50:08 +000070 if( sqlite3CheckCollSeq(pParse, pColl) ){
71 pColl = 0;
72 }
73 return pColl;
danielk19770202b292004-06-09 09:55:16 +000074}
75
76/*
drh53db1452004-05-20 13:54:53 +000077** pExpr is the left operand of a comparison operator. aff2 is the
78** type affinity of the right operand. This routine returns the
79** type affinity that should be used for the comparison operator.
80*/
danielk1977e014a832004-05-17 10:48:57 +000081char sqlite3CompareAffinity(Expr *pExpr, char aff2){
danielk1977bf3b7212004-05-18 10:06:24 +000082 char aff1 = sqlite3ExprAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +000083 if( aff1 && aff2 ){
84 /* Both sides of the comparison are columns. If one has numeric or
85 ** integer affinity, use that. Otherwise use no affinity.
86 */
87 if( aff1==SQLITE_AFF_INTEGER || aff2==SQLITE_AFF_INTEGER ){
88 return SQLITE_AFF_INTEGER;
89 }else if( aff1==SQLITE_AFF_NUMERIC || aff2==SQLITE_AFF_NUMERIC ){
90 return SQLITE_AFF_NUMERIC;
91 }else{
92 return SQLITE_AFF_NONE;
93 }
94 }else if( !aff1 && !aff2 ){
95 /* Neither side of the comparison is a column. Use numeric affinity
96 ** for the comparison.
97 */
98 return SQLITE_AFF_NUMERIC;
99 }else{
100 /* One side is a column, the other is not. Use the columns affinity. */
101 return (aff1 + aff2);
102 }
103}
104
drh53db1452004-05-20 13:54:53 +0000105/*
106** pExpr is a comparison operator. Return the type affinity that should
107** be applied to both operands prior to doing the comparison.
108*/
danielk1977e014a832004-05-17 10:48:57 +0000109static char comparisonAffinity(Expr *pExpr){
110 char aff;
111 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
112 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
113 pExpr->op==TK_NE );
114 assert( pExpr->pLeft );
danielk1977bf3b7212004-05-18 10:06:24 +0000115 aff = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +0000116 if( pExpr->pRight ){
117 aff = sqlite3CompareAffinity(pExpr->pRight, aff);
118 }
119 else if( pExpr->pSelect ){
120 aff = sqlite3CompareAffinity(pExpr->pSelect->pEList->a[0].pExpr, aff);
121 }
122 else if( !aff ){
123 aff = SQLITE_AFF_NUMERIC;
124 }
125 return aff;
126}
127
128/*
129** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
130** idx_affinity is the affinity of an indexed column. Return true
131** if the index with affinity idx_affinity may be used to implement
132** the comparison in pExpr.
133*/
134int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){
135 char aff = comparisonAffinity(pExpr);
136 return
137 (aff==SQLITE_AFF_NONE) ||
138 (aff==SQLITE_AFF_NUMERIC && idx_affinity==SQLITE_AFF_INTEGER) ||
139 (aff==SQLITE_AFF_INTEGER && idx_affinity==SQLITE_AFF_NUMERIC) ||
140 (aff==idx_affinity);
141}
142
danielk1977a37cdde2004-05-16 11:15:36 +0000143/*
144** Return the P1 value that should be used for a binary comparison
145** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
146** If jumpIfNull is true, then set the low byte of the returned
147** P1 value to tell the opcode to jump if either expression
148** evaluates to NULL.
149*/
danielk1977e014a832004-05-17 10:48:57 +0000150static int binaryCompareP1(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){
danielk1977bf3b7212004-05-18 10:06:24 +0000151 char aff = sqlite3ExprAffinity(pExpr2);
danielk1977e014a832004-05-17 10:48:57 +0000152 return (((int)sqlite3CompareAffinity(pExpr1, aff))<<8)+(jumpIfNull?1:0);
danielk1977a37cdde2004-05-16 11:15:36 +0000153}
154
drha2e00042002-01-22 03:13:42 +0000155/*
danielk19770202b292004-06-09 09:55:16 +0000156** Return a pointer to the collation sequence that should be used by
157** a binary comparison operator comparing pLeft and pRight.
158**
159** If the left hand expression has a collating sequence type, then it is
160** used. Otherwise the collation sequence for the right hand expression
161** is used, or the default (BINARY) if neither expression has a collating
162** type.
163*/
danielk19777cedc8d2004-06-10 10:50:08 +0000164static CollSeq* binaryCompareCollSeq(Parse *pParse, Expr *pLeft, Expr *pRight){
165 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pLeft);
danielk19770202b292004-06-09 09:55:16 +0000166 if( !pColl ){
danielk19777cedc8d2004-06-10 10:50:08 +0000167 pColl = sqlite3ExprCollSeq(pParse, pRight);
danielk19770202b292004-06-09 09:55:16 +0000168 }
169 return pColl;
170}
171
172/*
drha76b5df2002-02-23 02:32:10 +0000173** Construct a new expression node and return a pointer to it. Memory
174** for this node is obtained from sqliteMalloc(). The calling function
175** is responsible for making sure the node eventually gets freed.
176*/
danielk19774adee202004-05-08 08:23:19 +0000177Expr *sqlite3Expr(int op, Expr *pLeft, Expr *pRight, Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000178 Expr *pNew;
179 pNew = sqliteMalloc( sizeof(Expr) );
180 if( pNew==0 ){
drh4efc4752004-01-16 15:55:37 +0000181 /* When malloc fails, we leak memory from pLeft and pRight */
drha76b5df2002-02-23 02:32:10 +0000182 return 0;
183 }
184 pNew->op = op;
185 pNew->pLeft = pLeft;
186 pNew->pRight = pRight;
187 if( pToken ){
drh4b59ab52002-08-24 18:24:51 +0000188 assert( pToken->dyn==0 );
drha76b5df2002-02-23 02:32:10 +0000189 pNew->token = *pToken;
drh6977fea2002-10-22 23:38:04 +0000190 pNew->span = *pToken;
drha76b5df2002-02-23 02:32:10 +0000191 }else{
drh4efc4752004-01-16 15:55:37 +0000192 assert( pNew->token.dyn==0 );
193 assert( pNew->token.z==0 );
194 assert( pNew->token.n==0 );
drh6977fea2002-10-22 23:38:04 +0000195 if( pLeft && pRight ){
danielk19774adee202004-05-08 08:23:19 +0000196 sqlite3ExprSpan(pNew, &pLeft->span, &pRight->span);
drh6977fea2002-10-22 23:38:04 +0000197 }else{
198 pNew->span = pNew->token;
199 }
drha76b5df2002-02-23 02:32:10 +0000200 }
drha76b5df2002-02-23 02:32:10 +0000201 return pNew;
202}
203
204/*
drh6977fea2002-10-22 23:38:04 +0000205** Set the Expr.span field of the given expression to span all
drha76b5df2002-02-23 02:32:10 +0000206** text between the two given tokens.
207*/
danielk19774adee202004-05-08 08:23:19 +0000208void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
drh4efc4752004-01-16 15:55:37 +0000209 assert( pRight!=0 );
210 assert( pLeft!=0 );
211 /* Note: pExpr might be NULL due to a prior malloc failure */
212 if( pExpr && pRight->z && pLeft->z ){
drh4b59ab52002-08-24 18:24:51 +0000213 if( pLeft->dyn==0 && pRight->dyn==0 ){
drh6977fea2002-10-22 23:38:04 +0000214 pExpr->span.z = pLeft->z;
215 pExpr->span.n = pRight->n + Addr(pRight->z) - Addr(pLeft->z);
drh4b59ab52002-08-24 18:24:51 +0000216 }else{
drh6977fea2002-10-22 23:38:04 +0000217 pExpr->span.z = 0;
drh4b59ab52002-08-24 18:24:51 +0000218 }
drha76b5df2002-02-23 02:32:10 +0000219 }
220}
221
222/*
223** Construct a new expression node for a function with multiple
224** arguments.
225*/
danielk19774adee202004-05-08 08:23:19 +0000226Expr *sqlite3ExprFunction(ExprList *pList, Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000227 Expr *pNew;
228 pNew = sqliteMalloc( sizeof(Expr) );
229 if( pNew==0 ){
danielk19774adee202004-05-08 08:23:19 +0000230 /* sqlite3ExprListDelete(pList); // Leak pList when malloc fails */
drha76b5df2002-02-23 02:32:10 +0000231 return 0;
232 }
233 pNew->op = TK_FUNCTION;
234 pNew->pList = pList;
235 if( pToken ){
drh4b59ab52002-08-24 18:24:51 +0000236 assert( pToken->dyn==0 );
drha76b5df2002-02-23 02:32:10 +0000237 pNew->token = *pToken;
238 }else{
239 pNew->token.z = 0;
drha76b5df2002-02-23 02:32:10 +0000240 }
drh6977fea2002-10-22 23:38:04 +0000241 pNew->span = pNew->token;
drha76b5df2002-02-23 02:32:10 +0000242 return pNew;
243}
244
245/*
drha2e00042002-01-22 03:13:42 +0000246** Recursively delete an expression tree.
247*/
danielk19774adee202004-05-08 08:23:19 +0000248void sqlite3ExprDelete(Expr *p){
drha2e00042002-01-22 03:13:42 +0000249 if( p==0 ) return;
drh4efc4752004-01-16 15:55:37 +0000250 if( p->span.dyn ) sqliteFree((char*)p->span.z);
251 if( p->token.dyn ) sqliteFree((char*)p->token.z);
danielk19774adee202004-05-08 08:23:19 +0000252 sqlite3ExprDelete(p->pLeft);
253 sqlite3ExprDelete(p->pRight);
254 sqlite3ExprListDelete(p->pList);
255 sqlite3SelectDelete(p->pSelect);
drha2e00042002-01-22 03:13:42 +0000256 sqliteFree(p);
257}
258
drha76b5df2002-02-23 02:32:10 +0000259
260/*
drhff78bd22002-02-27 01:47:11 +0000261** The following group of routines make deep copies of expressions,
262** expression lists, ID lists, and select statements. The copies can
263** be deleted (by being passed to their respective ...Delete() routines)
264** without effecting the originals.
265**
danielk19774adee202004-05-08 08:23:19 +0000266** The expression list, ID, and source lists return by sqlite3ExprListDup(),
267** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
drhad3cab52002-05-24 02:04:32 +0000268** by subsequent calls to sqlite*ListAppend() routines.
drhff78bd22002-02-27 01:47:11 +0000269**
drhad3cab52002-05-24 02:04:32 +0000270** Any tables that the SrcList might point to are not duplicated.
drhff78bd22002-02-27 01:47:11 +0000271*/
danielk19774adee202004-05-08 08:23:19 +0000272Expr *sqlite3ExprDup(Expr *p){
drhff78bd22002-02-27 01:47:11 +0000273 Expr *pNew;
274 if( p==0 ) return 0;
drhfcb78a42003-01-18 20:11:05 +0000275 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000276 if( pNew==0 ) return 0;
drh3b167c72002-06-28 12:18:47 +0000277 memcpy(pNew, p, sizeof(*pNew));
drh6977fea2002-10-22 23:38:04 +0000278 if( p->token.z!=0 ){
drh4b59ab52002-08-24 18:24:51 +0000279 pNew->token.z = sqliteStrDup(p->token.z);
280 pNew->token.dyn = 1;
281 }else{
drh4efc4752004-01-16 15:55:37 +0000282 assert( pNew->token.z==0 );
drh4b59ab52002-08-24 18:24:51 +0000283 }
drh6977fea2002-10-22 23:38:04 +0000284 pNew->span.z = 0;
danielk19774adee202004-05-08 08:23:19 +0000285 pNew->pLeft = sqlite3ExprDup(p->pLeft);
286 pNew->pRight = sqlite3ExprDup(p->pRight);
287 pNew->pList = sqlite3ExprListDup(p->pList);
288 pNew->pSelect = sqlite3SelectDup(p->pSelect);
drhff78bd22002-02-27 01:47:11 +0000289 return pNew;
290}
danielk19774adee202004-05-08 08:23:19 +0000291void sqlite3TokenCopy(Token *pTo, Token *pFrom){
drh4b59ab52002-08-24 18:24:51 +0000292 if( pTo->dyn ) sqliteFree((char*)pTo->z);
drh4b59ab52002-08-24 18:24:51 +0000293 if( pFrom->z ){
294 pTo->n = pFrom->n;
295 pTo->z = sqliteStrNDup(pFrom->z, pFrom->n);
296 pTo->dyn = 1;
297 }else{
drh4b59ab52002-08-24 18:24:51 +0000298 pTo->z = 0;
drh4b59ab52002-08-24 18:24:51 +0000299 }
300}
danielk19774adee202004-05-08 08:23:19 +0000301ExprList *sqlite3ExprListDup(ExprList *p){
drhff78bd22002-02-27 01:47:11 +0000302 ExprList *pNew;
drh3e7bc9c2004-02-21 19:17:17 +0000303 struct ExprList_item *pItem;
drhff78bd22002-02-27 01:47:11 +0000304 int i;
305 if( p==0 ) return 0;
306 pNew = sqliteMalloc( sizeof(*pNew) );
307 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000308 pNew->nExpr = pNew->nAlloc = p->nExpr;
drh3e7bc9c2004-02-21 19:17:17 +0000309 pNew->a = pItem = sqliteMalloc( p->nExpr*sizeof(p->a[0]) );
drh1bdd9b52004-04-23 17:04:44 +0000310 if( pItem==0 ) return 0; /* Leaks memory after a malloc failure */
311 for(i=0; i<p->nExpr; i++, pItem++){
drh4b59ab52002-08-24 18:24:51 +0000312 Expr *pNewExpr, *pOldExpr;
danielk19774adee202004-05-08 08:23:19 +0000313 pItem->pExpr = pNewExpr = sqlite3ExprDup(pOldExpr = p->a[i].pExpr);
drh6977fea2002-10-22 23:38:04 +0000314 if( pOldExpr->span.z!=0 && pNewExpr ){
315 /* Always make a copy of the span for top-level expressions in the
drh4b59ab52002-08-24 18:24:51 +0000316 ** expression list. The logic in SELECT processing that determines
317 ** the names of columns in the result set needs this information */
danielk19774adee202004-05-08 08:23:19 +0000318 sqlite3TokenCopy(&pNewExpr->span, &pOldExpr->span);
drh4b59ab52002-08-24 18:24:51 +0000319 }
drh1f3e9052002-10-31 00:09:39 +0000320 assert( pNewExpr==0 || pNewExpr->span.z!=0
danielk197724b03fd2004-05-10 10:34:34 +0000321 || pOldExpr->span.z==0 || sqlite3_malloc_failed );
drh3e7bc9c2004-02-21 19:17:17 +0000322 pItem->zName = sqliteStrDup(p->a[i].zName);
323 pItem->sortOrder = p->a[i].sortOrder;
324 pItem->isAgg = p->a[i].isAgg;
325 pItem->done = 0;
drhff78bd22002-02-27 01:47:11 +0000326 }
327 return pNew;
328}
danielk19774adee202004-05-08 08:23:19 +0000329SrcList *sqlite3SrcListDup(SrcList *p){
drhad3cab52002-05-24 02:04:32 +0000330 SrcList *pNew;
331 int i;
drh113088e2003-03-20 01:16:58 +0000332 int nByte;
drhad3cab52002-05-24 02:04:32 +0000333 if( p==0 ) return 0;
drh113088e2003-03-20 01:16:58 +0000334 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
drh4efc4752004-01-16 15:55:37 +0000335 pNew = sqliteMallocRaw( nByte );
drhad3cab52002-05-24 02:04:32 +0000336 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000337 pNew->nSrc = pNew->nAlloc = p->nSrc;
drhad3cab52002-05-24 02:04:32 +0000338 for(i=0; i<p->nSrc; i++){
drh4efc4752004-01-16 15:55:37 +0000339 struct SrcList_item *pNewItem = &pNew->a[i];
340 struct SrcList_item *pOldItem = &p->a[i];
341 pNewItem->zDatabase = sqliteStrDup(pOldItem->zDatabase);
342 pNewItem->zName = sqliteStrDup(pOldItem->zName);
343 pNewItem->zAlias = sqliteStrDup(pOldItem->zAlias);
344 pNewItem->jointype = pOldItem->jointype;
345 pNewItem->iCursor = pOldItem->iCursor;
346 pNewItem->pTab = 0;
danielk19774adee202004-05-08 08:23:19 +0000347 pNewItem->pSelect = sqlite3SelectDup(pOldItem->pSelect);
348 pNewItem->pOn = sqlite3ExprDup(pOldItem->pOn);
349 pNewItem->pUsing = sqlite3IdListDup(pOldItem->pUsing);
drhad3cab52002-05-24 02:04:32 +0000350 }
351 return pNew;
352}
danielk19774adee202004-05-08 08:23:19 +0000353IdList *sqlite3IdListDup(IdList *p){
drhff78bd22002-02-27 01:47:11 +0000354 IdList *pNew;
355 int i;
356 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000357 pNew = sqliteMallocRaw( sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +0000358 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000359 pNew->nId = pNew->nAlloc = p->nId;
drh4efc4752004-01-16 15:55:37 +0000360 pNew->a = sqliteMallocRaw( p->nId*sizeof(p->a[0]) );
drhe4697f52002-05-23 02:09:03 +0000361 if( pNew->a==0 ) return 0;
drhff78bd22002-02-27 01:47:11 +0000362 for(i=0; i<p->nId; i++){
drh4efc4752004-01-16 15:55:37 +0000363 struct IdList_item *pNewItem = &pNew->a[i];
364 struct IdList_item *pOldItem = &p->a[i];
365 pNewItem->zName = sqliteStrDup(pOldItem->zName);
366 pNewItem->idx = pOldItem->idx;
drhff78bd22002-02-27 01:47:11 +0000367 }
368 return pNew;
369}
danielk19774adee202004-05-08 08:23:19 +0000370Select *sqlite3SelectDup(Select *p){
drhff78bd22002-02-27 01:47:11 +0000371 Select *pNew;
372 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000373 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000374 if( pNew==0 ) return 0;
375 pNew->isDistinct = p->isDistinct;
danielk19774adee202004-05-08 08:23:19 +0000376 pNew->pEList = sqlite3ExprListDup(p->pEList);
377 pNew->pSrc = sqlite3SrcListDup(p->pSrc);
378 pNew->pWhere = sqlite3ExprDup(p->pWhere);
379 pNew->pGroupBy = sqlite3ExprListDup(p->pGroupBy);
380 pNew->pHaving = sqlite3ExprDup(p->pHaving);
381 pNew->pOrderBy = sqlite3ExprListDup(p->pOrderBy);
drhff78bd22002-02-27 01:47:11 +0000382 pNew->op = p->op;
danielk19774adee202004-05-08 08:23:19 +0000383 pNew->pPrior = sqlite3SelectDup(p->pPrior);
drhff78bd22002-02-27 01:47:11 +0000384 pNew->nLimit = p->nLimit;
385 pNew->nOffset = p->nOffset;
386 pNew->zSelect = 0;
drh7b58dae2003-07-20 01:16:46 +0000387 pNew->iLimit = -1;
388 pNew->iOffset = -1;
drhff78bd22002-02-27 01:47:11 +0000389 return pNew;
390}
391
392
393/*
drha76b5df2002-02-23 02:32:10 +0000394** Add a new element to the end of an expression list. If pList is
395** initially NULL, then create a new expression list.
396*/
danielk19774adee202004-05-08 08:23:19 +0000397ExprList *sqlite3ExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){
drha76b5df2002-02-23 02:32:10 +0000398 if( pList==0 ){
399 pList = sqliteMalloc( sizeof(ExprList) );
400 if( pList==0 ){
danielk19774adee202004-05-08 08:23:19 +0000401 /* sqlite3ExprDelete(pExpr); // Leak memory if malloc fails */
drha76b5df2002-02-23 02:32:10 +0000402 return 0;
403 }
drh4efc4752004-01-16 15:55:37 +0000404 assert( pList->nAlloc==0 );
drha76b5df2002-02-23 02:32:10 +0000405 }
drh4305d102003-07-30 12:34:12 +0000406 if( pList->nAlloc<=pList->nExpr ){
drh4305d102003-07-30 12:34:12 +0000407 pList->nAlloc = pList->nAlloc*2 + 4;
drh4efc4752004-01-16 15:55:37 +0000408 pList->a = sqliteRealloc(pList->a, pList->nAlloc*sizeof(pList->a[0]));
409 if( pList->a==0 ){
danielk19774adee202004-05-08 08:23:19 +0000410 /* sqlite3ExprDelete(pExpr); // Leak memory if malloc fails */
drh4efc4752004-01-16 15:55:37 +0000411 pList->nExpr = pList->nAlloc = 0;
drha76b5df2002-02-23 02:32:10 +0000412 return pList;
413 }
drha76b5df2002-02-23 02:32:10 +0000414 }
drh4efc4752004-01-16 15:55:37 +0000415 assert( pList->a!=0 );
416 if( pExpr || pName ){
417 struct ExprList_item *pItem = &pList->a[pList->nExpr++];
418 memset(pItem, 0, sizeof(*pItem));
419 pItem->pExpr = pExpr;
drha76b5df2002-02-23 02:32:10 +0000420 if( pName ){
danielk19774adee202004-05-08 08:23:19 +0000421 sqlite3SetNString(&pItem->zName, pName->z, pName->n, 0);
422 sqlite3Dequote(pItem->zName);
drha76b5df2002-02-23 02:32:10 +0000423 }
424 }
425 return pList;
426}
427
428/*
429** Delete an entire expression list.
430*/
danielk19774adee202004-05-08 08:23:19 +0000431void sqlite3ExprListDelete(ExprList *pList){
drha76b5df2002-02-23 02:32:10 +0000432 int i;
433 if( pList==0 ) return;
drh1bdd9b52004-04-23 17:04:44 +0000434 assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
435 assert( pList->nExpr<=pList->nAlloc );
drha76b5df2002-02-23 02:32:10 +0000436 for(i=0; i<pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +0000437 sqlite3ExprDelete(pList->a[i].pExpr);
drha76b5df2002-02-23 02:32:10 +0000438 sqliteFree(pList->a[i].zName);
439 }
440 sqliteFree(pList->a);
441 sqliteFree(pList);
442}
443
444/*
drhfef52082000-06-06 01:50:43 +0000445** Walk an expression tree. Return 1 if the expression is constant
446** and 0 if it involves variables.
drh23989372002-05-21 13:43:04 +0000447**
448** For the purposes of this function, a double-quoted string (ex: "abc")
449** is considered a variable but a single-quoted string (ex: 'abc') is
450** a constant.
drhfef52082000-06-06 01:50:43 +0000451*/
danielk19774adee202004-05-08 08:23:19 +0000452int sqlite3ExprIsConstant(Expr *p){
drhfef52082000-06-06 01:50:43 +0000453 switch( p->op ){
454 case TK_ID:
drh967e8b72000-06-21 13:59:10 +0000455 case TK_COLUMN:
drhfef52082000-06-06 01:50:43 +0000456 case TK_DOT:
drh7bdc0c12003-04-19 17:27:24 +0000457 case TK_FUNCTION:
drhfef52082000-06-06 01:50:43 +0000458 return 0;
drh7bdc0c12003-04-19 17:27:24 +0000459 case TK_NULL:
drh23989372002-05-21 13:43:04 +0000460 case TK_STRING:
danielk1977c572ef72004-05-27 09:28:41 +0000461 case TK_BLOB:
drh92086432002-01-22 14:11:29 +0000462 case TK_INTEGER:
463 case TK_FLOAT:
drh50457892003-09-06 01:10:47 +0000464 case TK_VARIABLE:
drh92086432002-01-22 14:11:29 +0000465 return 1;
drhfef52082000-06-06 01:50:43 +0000466 default: {
danielk19774adee202004-05-08 08:23:19 +0000467 if( p->pLeft && !sqlite3ExprIsConstant(p->pLeft) ) return 0;
468 if( p->pRight && !sqlite3ExprIsConstant(p->pRight) ) return 0;
drhfef52082000-06-06 01:50:43 +0000469 if( p->pList ){
470 int i;
471 for(i=0; i<p->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +0000472 if( !sqlite3ExprIsConstant(p->pList->a[i].pExpr) ) return 0;
drhfef52082000-06-06 01:50:43 +0000473 }
474 }
drh92086432002-01-22 14:11:29 +0000475 return p->pLeft!=0 || p->pRight!=0 || (p->pList && p->pList->nExpr>0);
drhfef52082000-06-06 01:50:43 +0000476 }
477 }
drh92086432002-01-22 14:11:29 +0000478 return 0;
drhfef52082000-06-06 01:50:43 +0000479}
480
481/*
drh202b2df2004-01-06 01:13:46 +0000482** If the given expression codes a constant integer that is small enough
483** to fit in a 32-bit integer, return 1 and put the value of the integer
484** in *pValue. If the expression is not an integer or if it is too big
485** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
drhe4de1fe2002-06-02 16:09:01 +0000486*/
danielk19774adee202004-05-08 08:23:19 +0000487int sqlite3ExprIsInteger(Expr *p, int *pValue){
drhe4de1fe2002-06-02 16:09:01 +0000488 switch( p->op ){
489 case TK_INTEGER: {
drhfec19aa2004-05-19 20:41:03 +0000490 if( sqlite3GetInt32(p->token.z, pValue) ){
drh202b2df2004-01-06 01:13:46 +0000491 return 1;
492 }
493 break;
drhe4de1fe2002-06-02 16:09:01 +0000494 }
495 case TK_STRING: {
drhbd790ee2002-06-02 18:22:06 +0000496 const char *z = p->token.z;
drhe4de1fe2002-06-02 16:09:01 +0000497 int n = p->token.n;
drhbd790ee2002-06-02 18:22:06 +0000498 if( n>0 && z[0]=='-' ){ z++; n--; }
drhe4de1fe2002-06-02 16:09:01 +0000499 while( n>0 && *z && isdigit(*z) ){ z++; n--; }
drhfec19aa2004-05-19 20:41:03 +0000500 if( n==0 && sqlite3GetInt32(p->token.z, pValue) ){
drhe4de1fe2002-06-02 16:09:01 +0000501 return 1;
502 }
503 break;
504 }
drh4b59ab52002-08-24 18:24:51 +0000505 case TK_UPLUS: {
danielk19774adee202004-05-08 08:23:19 +0000506 return sqlite3ExprIsInteger(p->pLeft, pValue);
drh4b59ab52002-08-24 18:24:51 +0000507 }
drhe4de1fe2002-06-02 16:09:01 +0000508 case TK_UMINUS: {
509 int v;
danielk19774adee202004-05-08 08:23:19 +0000510 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
drhe4de1fe2002-06-02 16:09:01 +0000511 *pValue = -v;
512 return 1;
513 }
514 break;
515 }
516 default: break;
517 }
518 return 0;
519}
520
521/*
drhc4a3c772001-04-04 11:48:57 +0000522** Return TRUE if the given string is a row-id column name.
523*/
danielk19774adee202004-05-08 08:23:19 +0000524int sqlite3IsRowid(const char *z){
525 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
526 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
527 if( sqlite3StrICmp(z, "OID")==0 ) return 1;
drhc4a3c772001-04-04 11:48:57 +0000528 return 0;
529}
530
531/*
drh8141f612004-01-25 22:44:58 +0000532** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
533** that name in the set of source tables in pSrcList and make the pExpr
534** expression node refer back to that source column. The following changes
535** are made to pExpr:
536**
537** pExpr->iDb Set the index in db->aDb[] of the database holding
538** the table.
539** pExpr->iTable Set to the cursor number for the table obtained
540** from pSrcList.
541** pExpr->iColumn Set to the column number within the table.
drh8141f612004-01-25 22:44:58 +0000542** pExpr->op Set to TK_COLUMN.
543** pExpr->pLeft Any expression this points to is deleted
544** pExpr->pRight Any expression this points to is deleted.
545**
546** The pDbToken is the name of the database (the "X"). This value may be
547** NULL meaning that name is of the form Y.Z or Z. Any available database
548** can be used. The pTableToken is the name of the table (the "Y"). This
549** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it
550** means that the form of the name is Z and that columns from any table
551** can be used.
552**
553** If the name cannot be resolved unambiguously, leave an error message
554** in pParse and return non-zero. Return zero on success.
555*/
556static int lookupName(
557 Parse *pParse, /* The parsing context */
558 Token *pDbToken, /* Name of the database containing table, or NULL */
559 Token *pTableToken, /* Name of table containing column, or NULL */
560 Token *pColumnToken, /* Name of the column. */
561 SrcList *pSrcList, /* List of tables used to resolve column names */
562 ExprList *pEList, /* List of expressions used to resolve "AS" */
563 Expr *pExpr /* Make this EXPR node point to the selected column */
564){
565 char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */
566 char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */
567 char *zCol = 0; /* Name of the column. The "Z" */
568 int i, j; /* Loop counters */
569 int cnt = 0; /* Number of matching column names */
570 int cntTab = 0; /* Number of matching table names */
drh7e26d752004-02-11 10:35:29 +0000571 sqlite *db = pParse->db; /* The database */
drh8141f612004-01-25 22:44:58 +0000572
573 assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
574 if( pDbToken && pDbToken->z ){
575 zDb = sqliteStrNDup(pDbToken->z, pDbToken->n);
danielk19774adee202004-05-08 08:23:19 +0000576 sqlite3Dequote(zDb);
drh8141f612004-01-25 22:44:58 +0000577 }else{
578 zDb = 0;
579 }
580 if( pTableToken && pTableToken->z ){
581 zTab = sqliteStrNDup(pTableToken->z, pTableToken->n);
danielk19774adee202004-05-08 08:23:19 +0000582 sqlite3Dequote(zTab);
drh8141f612004-01-25 22:44:58 +0000583 }else{
584 assert( zDb==0 );
585 zTab = 0;
586 }
587 zCol = sqliteStrNDup(pColumnToken->z, pColumnToken->n);
danielk19774adee202004-05-08 08:23:19 +0000588 sqlite3Dequote(zCol);
danielk197724b03fd2004-05-10 10:34:34 +0000589 if( sqlite3_malloc_failed ){
drh8141f612004-01-25 22:44:58 +0000590 return 1; /* Leak memory (zDb and zTab) if malloc fails */
591 }
592 assert( zTab==0 || pEList==0 );
593
594 pExpr->iTable = -1;
595 for(i=0; i<pSrcList->nSrc; i++){
596 struct SrcList_item *pItem = &pSrcList->a[i];
597 Table *pTab = pItem->pTab;
598 Column *pCol;
599
600 if( pTab==0 ) continue;
601 assert( pTab->nCol>0 );
602 if( zTab ){
603 if( pItem->zAlias ){
604 char *zTabName = pItem->zAlias;
danielk19774adee202004-05-08 08:23:19 +0000605 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
drh8141f612004-01-25 22:44:58 +0000606 }else{
607 char *zTabName = pTab->zName;
danielk19774adee202004-05-08 08:23:19 +0000608 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
609 if( zDb!=0 && sqlite3StrICmp(db->aDb[pTab->iDb].zName, zDb)!=0 ){
drh8141f612004-01-25 22:44:58 +0000610 continue;
611 }
612 }
613 }
614 if( 0==(cntTab++) ){
615 pExpr->iTable = pItem->iCursor;
616 pExpr->iDb = pTab->iDb;
617 }
618 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
danielk19774adee202004-05-08 08:23:19 +0000619 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
drh8141f612004-01-25 22:44:58 +0000620 cnt++;
621 pExpr->iTable = pItem->iCursor;
622 pExpr->iDb = pTab->iDb;
623 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
624 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
danielk1977a37cdde2004-05-16 11:15:36 +0000625 pExpr->affinity = pTab->aCol[j].affinity;
danielk19770202b292004-06-09 09:55:16 +0000626 pExpr->pColl = pTab->aCol[j].pColl;
drh8141f612004-01-25 22:44:58 +0000627 break;
628 }
629 }
630 }
631
632 /* If we have not already resolved the name, then maybe
633 ** it is a new.* or old.* trigger argument reference
634 */
635 if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
636 TriggerStack *pTriggerStack = pParse->trigStack;
637 Table *pTab = 0;
danielk19774adee202004-05-08 08:23:19 +0000638 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){
drh8141f612004-01-25 22:44:58 +0000639 pExpr->iTable = pTriggerStack->newIdx;
640 assert( pTriggerStack->pTab );
641 pTab = pTriggerStack->pTab;
danielk19774adee202004-05-08 08:23:19 +0000642 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab) == 0 ){
drh8141f612004-01-25 22:44:58 +0000643 pExpr->iTable = pTriggerStack->oldIdx;
644 assert( pTriggerStack->pTab );
645 pTab = pTriggerStack->pTab;
646 }
647
648 if( pTab ){
649 int j;
650 Column *pCol = pTab->aCol;
651
652 pExpr->iDb = pTab->iDb;
653 cntTab++;
654 for(j=0; j < pTab->nCol; j++, pCol++) {
danielk19774adee202004-05-08 08:23:19 +0000655 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
drh8141f612004-01-25 22:44:58 +0000656 cnt++;
657 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
danielk1977a37cdde2004-05-16 11:15:36 +0000658 pExpr->affinity = pTab->aCol[j].affinity;
danielk19770202b292004-06-09 09:55:16 +0000659 pExpr->pColl = pTab->aCol[j].pColl;
drh8141f612004-01-25 22:44:58 +0000660 break;
661 }
662 }
663 }
664 }
665
666 /*
667 ** Perhaps the name is a reference to the ROWID
668 */
danielk19774adee202004-05-08 08:23:19 +0000669 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
drh8141f612004-01-25 22:44:58 +0000670 cnt = 1;
671 pExpr->iColumn = -1;
danielk1977a37cdde2004-05-16 11:15:36 +0000672 pExpr->affinity = SQLITE_AFF_INTEGER;
drh8141f612004-01-25 22:44:58 +0000673 }
674
675 /*
676 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
677 ** might refer to an result-set alias. This happens, for example, when
678 ** we are resolving names in the WHERE clause of the following command:
679 **
680 ** SELECT a+b AS x FROM table WHERE x<10;
681 **
682 ** In cases like this, replace pExpr with a copy of the expression that
683 ** forms the result set entry ("a+b" in the example) and return immediately.
684 ** Note that the expression in the result set should have already been
685 ** resolved by the time the WHERE clause is resolved.
686 */
687 if( cnt==0 && pEList!=0 ){
688 for(j=0; j<pEList->nExpr; j++){
689 char *zAs = pEList->a[j].zName;
danielk19774adee202004-05-08 08:23:19 +0000690 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
drh8141f612004-01-25 22:44:58 +0000691 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
692 pExpr->op = TK_AS;
693 pExpr->iColumn = j;
danielk19774adee202004-05-08 08:23:19 +0000694 pExpr->pLeft = sqlite3ExprDup(pEList->a[j].pExpr);
drh8141f612004-01-25 22:44:58 +0000695 sqliteFree(zCol);
696 assert( zTab==0 && zDb==0 );
697 return 0;
698 }
699 }
700 }
701
702 /*
703 ** If X and Y are NULL (in other words if only the column name Z is
704 ** supplied) and the value of Z is enclosed in double-quotes, then
705 ** Z is a string literal if it doesn't match any column names. In that
706 ** case, we need to return right away and not make any changes to
707 ** pExpr.
708 */
709 if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
710 sqliteFree(zCol);
711 return 0;
712 }
713
714 /*
715 ** cnt==0 means there was not match. cnt>1 means there were two or
716 ** more matches. Either way, we have an error.
717 */
718 if( cnt!=1 ){
719 char *z = 0;
720 char *zErr;
721 zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s";
722 if( zDb ){
danielk19774adee202004-05-08 08:23:19 +0000723 sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, 0);
drh8141f612004-01-25 22:44:58 +0000724 }else if( zTab ){
danielk19774adee202004-05-08 08:23:19 +0000725 sqlite3SetString(&z, zTab, ".", zCol, 0);
drh8141f612004-01-25 22:44:58 +0000726 }else{
727 z = sqliteStrDup(zCol);
728 }
danielk19774adee202004-05-08 08:23:19 +0000729 sqlite3ErrorMsg(pParse, zErr, z);
drh8141f612004-01-25 22:44:58 +0000730 sqliteFree(z);
731 }
732
733 /* Clean up and return
734 */
735 sqliteFree(zDb);
736 sqliteFree(zTab);
737 sqliteFree(zCol);
danielk19774adee202004-05-08 08:23:19 +0000738 sqlite3ExprDelete(pExpr->pLeft);
drh8141f612004-01-25 22:44:58 +0000739 pExpr->pLeft = 0;
danielk19774adee202004-05-08 08:23:19 +0000740 sqlite3ExprDelete(pExpr->pRight);
drh8141f612004-01-25 22:44:58 +0000741 pExpr->pRight = 0;
742 pExpr->op = TK_COLUMN;
danielk19774adee202004-05-08 08:23:19 +0000743 sqlite3AuthRead(pParse, pExpr, pSrcList);
drh8141f612004-01-25 22:44:58 +0000744 return cnt!=1;
745}
746
747/*
drhcce7d172000-05-31 15:34:51 +0000748** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +0000749** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +0000750** index to the table in the table list and a column offset. The
751** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
752** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +0000753** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +0000754** VDBE cursor number for a cursor that is pointing into the referenced
755** table. The Expr.iColumn value is changed to the index of the column
756** of the referenced table. The Expr.iColumn value for the special
757** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
758** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +0000759**
drhfef52082000-06-06 01:50:43 +0000760** We also check for instances of the IN operator. IN comes in two
761** forms:
762**
763** expr IN (exprlist)
764** and
765** expr IN (SELECT ...)
766**
767** The first form is handled by creating a set holding the list
768** of allowed values. The second form causes the SELECT to generate
769** a temporary table.
770**
771** This routine also looks for scalar SELECTs that are part of an expression.
drh19a775c2000-06-05 18:54:46 +0000772** If it finds any, it generates code to write the value of that select
773** into a memory cell.
drhcce7d172000-05-31 15:34:51 +0000774**
drh967e8b72000-06-21 13:59:10 +0000775** Unknown columns or tables provoke an error. The function returns
drhcce7d172000-05-31 15:34:51 +0000776** the number of errors seen and leaves an error message on pParse->zErrMsg.
777*/
danielk19774adee202004-05-08 08:23:19 +0000778int sqlite3ExprResolveIds(
drha2e00042002-01-22 03:13:42 +0000779 Parse *pParse, /* The parser context */
drh8141f612004-01-25 22:44:58 +0000780 SrcList *pSrcList, /* List of tables used to resolve column names */
drha2e00042002-01-22 03:13:42 +0000781 ExprList *pEList, /* List of expressions used to resolve "AS" */
782 Expr *pExpr /* The expression to be analyzed. */
783){
drh6a3ea0e2003-05-02 14:32:12 +0000784 int i;
785
drh8141f612004-01-25 22:44:58 +0000786 if( pExpr==0 || pSrcList==0 ) return 0;
787 for(i=0; i<pSrcList->nSrc; i++){
788 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab );
drh6a3ea0e2003-05-02 14:32:12 +0000789 }
drhcce7d172000-05-31 15:34:51 +0000790 switch( pExpr->op ){
drh23989372002-05-21 13:43:04 +0000791 /* Double-quoted strings (ex: "abc") are used as identifiers if
792 ** possible. Otherwise they remain as strings. Single-quoted
793 ** strings (ex: 'abc') are always string literals.
794 */
795 case TK_STRING: {
796 if( pExpr->token.z[0]=='\'' ) break;
797 /* Fall thru into the TK_ID case if this is a double-quoted string */
798 }
drh8141f612004-01-25 22:44:58 +0000799 /* A lone identifier is the name of a columnd.
drha2e00042002-01-22 03:13:42 +0000800 */
drhcce7d172000-05-31 15:34:51 +0000801 case TK_ID: {
drh8141f612004-01-25 22:44:58 +0000802 if( lookupName(pParse, 0, 0, &pExpr->token, pSrcList, pEList, pExpr) ){
drhcce7d172000-05-31 15:34:51 +0000803 return 1;
drhed6c8672003-01-12 18:02:16 +0000804 }
drhcce7d172000-05-31 15:34:51 +0000805 break;
806 }
807
drhd24cc422003-03-27 12:51:24 +0000808 /* A table name and column name: ID.ID
809 ** Or a database, table and column: ID.ID.ID
810 */
drhcce7d172000-05-31 15:34:51 +0000811 case TK_DOT: {
drh8141f612004-01-25 22:44:58 +0000812 Token *pColumn;
813 Token *pTable;
814 Token *pDb;
815 Expr *pRight;
drhcce7d172000-05-31 15:34:51 +0000816
drhcce7d172000-05-31 15:34:51 +0000817 pRight = pExpr->pRight;
drhd24cc422003-03-27 12:51:24 +0000818 if( pRight->op==TK_ID ){
drh8141f612004-01-25 22:44:58 +0000819 pDb = 0;
820 pTable = &pExpr->pLeft->token;
821 pColumn = &pRight->token;
drhd24cc422003-03-27 12:51:24 +0000822 }else{
drh8141f612004-01-25 22:44:58 +0000823 assert( pRight->op==TK_DOT );
824 pDb = &pExpr->pLeft->token;
825 pTable = &pRight->pLeft->token;
826 pColumn = &pRight->pRight->token;
drhd24cc422003-03-27 12:51:24 +0000827 }
drh8141f612004-01-25 22:44:58 +0000828 if( lookupName(pParse, pDb, pTable, pColumn, pSrcList, 0, pExpr) ){
drhdaffd0e2001-04-11 14:28:42 +0000829 return 1;
830 }
drhcce7d172000-05-31 15:34:51 +0000831 break;
832 }
833
drhfef52082000-06-06 01:50:43 +0000834 case TK_IN: {
danielk1977e014a832004-05-17 10:48:57 +0000835 char affinity;
danielk19774adee202004-05-08 08:23:19 +0000836 Vdbe *v = sqlite3GetVdbe(pParse);
drhd3d39e92004-05-20 22:16:29 +0000837 KeyInfo keyInfo;
danielk19770202b292004-06-09 09:55:16 +0000838 int addr; /* Address of OP_OpenTemp instruction */
drhd3d39e92004-05-20 22:16:29 +0000839
drhfef52082000-06-06 01:50:43 +0000840 if( v==0 ) return 1;
danielk19774adee202004-05-08 08:23:19 +0000841 if( sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pLeft) ){
drhcfab11b2000-06-06 03:31:22 +0000842 return 1;
843 }
danielk1977bf3b7212004-05-18 10:06:24 +0000844 affinity = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +0000845
846 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
847 ** expression it is handled the same way. A temporary table is
848 ** filled with single-field index keys representing the results
849 ** from the SELECT or the <exprlist>.
850 **
851 ** If the 'x' expression is a column value, or the SELECT...
852 ** statement returns a column value, then the affinity of that
853 ** column is used to build the index keys. If both 'x' and the
854 ** SELECT... statement are columns, then numeric affinity is used
855 ** if either column has NUMERIC or INTEGER affinity. If neither
856 ** 'x' nor the SELECT... statement are columns, then numeric affinity
857 ** is used.
858 */
859 pExpr->iTable = pParse->nTab++;
danielk19770202b292004-06-09 09:55:16 +0000860 addr = sqlite3VdbeAddOp(v, OP_OpenTemp, pExpr->iTable, 0);
drhd3d39e92004-05-20 22:16:29 +0000861 memset(&keyInfo, 0, sizeof(keyInfo));
862 keyInfo.nField = 1;
drhf3218fe2004-05-28 08:21:02 +0000863 sqlite3VdbeAddOp(v, OP_SetNumColumns, pExpr->iTable, 1);
danielk1977e014a832004-05-17 10:48:57 +0000864
drhfef52082000-06-06 01:50:43 +0000865 if( pExpr->pSelect ){
866 /* Case 1: expr IN (SELECT ...)
867 **
danielk1977e014a832004-05-17 10:48:57 +0000868 ** Generate code to write the results of the select into the temporary
869 ** table allocated and opened above.
drhfef52082000-06-06 01:50:43 +0000870 */
danielk1977e014a832004-05-17 10:48:57 +0000871 int iParm = pExpr->iTable + (((int)affinity)<<16);
872 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
danielk1977bf3b7212004-05-18 10:06:24 +0000873 sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0);
danielk19770202b292004-06-09 09:55:16 +0000874 if( pExpr->pSelect->pEList && pExpr->pSelect->pEList->nExpr>0 ){
danielk19777cedc8d2004-06-10 10:50:08 +0000875 keyInfo.aColl[0] = binaryCompareCollSeq(pParse, pExpr->pLeft,
danielk19770202b292004-06-09 09:55:16 +0000876 pExpr->pSelect->pEList->a[0].pExpr);
877 }
drhfef52082000-06-06 01:50:43 +0000878 }else if( pExpr->pList ){
879 /* Case 2: expr IN (exprlist)
880 **
danielk1977e014a832004-05-17 10:48:57 +0000881 ** For each expression, build an index key from the evaluation and
882 ** store it in the temporary table. If <expr> is a column, then use
883 ** that columns affinity when building index keys. If <expr> is not
884 ** a column, use numeric affinity.
drhfef52082000-06-06 01:50:43 +0000885 */
danielk1977e014a832004-05-17 10:48:57 +0000886 int i;
887 char const *affStr;
888 if( !affinity ){
889 affinity = SQLITE_AFF_NUMERIC;
890 }
891 affStr = sqlite3AffinityString(affinity);
danielk19770202b292004-06-09 09:55:16 +0000892 keyInfo.aColl[0] = pExpr->pLeft->pColl;
danielk1977e014a832004-05-17 10:48:57 +0000893
894 /* Loop through each expression in <exprlist>. */
drhfef52082000-06-06 01:50:43 +0000895 for(i=0; i<pExpr->pList->nExpr; i++){
896 Expr *pE2 = pExpr->pList->a[i].pExpr;
danielk1977e014a832004-05-17 10:48:57 +0000897
898 /* Check that the expression is constant and valid. */
danielk19774adee202004-05-08 08:23:19 +0000899 if( !sqlite3ExprIsConstant(pE2) ){
900 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +0000901 "right-hand side of IN operator must be constant");
drhfef52082000-06-06 01:50:43 +0000902 return 1;
903 }
danielk19774adee202004-05-08 08:23:19 +0000904 if( sqlite3ExprCheck(pParse, pE2, 0, 0) ){
drh4794b982000-06-06 13:54:14 +0000905 return 1;
906 }
danielk1977e014a832004-05-17 10:48:57 +0000907
908 /* Evaluate the expression and insert it into the temp table */
909 sqlite3ExprCode(pParse, pE2);
910 sqlite3VdbeOp3(v, OP_MakeKey, 1, 0, affStr, P3_STATIC);
danielk19770f69c1e2004-05-29 11:24:50 +0000911 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +0000912 sqlite3VdbeAddOp(v, OP_PutStrKey, pExpr->iTable, 0);
drhfef52082000-06-06 01:50:43 +0000913 }
914 }
danielk19770202b292004-06-09 09:55:16 +0000915 sqlite3VdbeChangeP3(v, addr, (void *)&keyInfo, P3_KEYINFO);
916
drhcfab11b2000-06-06 03:31:22 +0000917 break;
drhfef52082000-06-06 01:50:43 +0000918 }
919
drh19a775c2000-06-05 18:54:46 +0000920 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +0000921 /* This has to be a scalar SELECT. Generate code to put the
922 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +0000923 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +0000924 */
drh967e8b72000-06-21 13:59:10 +0000925 pExpr->iColumn = pParse->nMem++;
danielk1977bf3b7212004-05-18 10:06:24 +0000926 if(sqlite3Select(pParse, pExpr->pSelect, SRT_Mem,pExpr->iColumn,0,0,0,0)){
drh19a775c2000-06-05 18:54:46 +0000927 return 1;
928 }
929 break;
930 }
931
drhcce7d172000-05-31 15:34:51 +0000932 /* For all else, just recursively walk the tree */
933 default: {
drh4794b982000-06-06 13:54:14 +0000934 if( pExpr->pLeft
danielk19774adee202004-05-08 08:23:19 +0000935 && sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pLeft) ){
drhcce7d172000-05-31 15:34:51 +0000936 return 1;
937 }
938 if( pExpr->pRight
danielk19774adee202004-05-08 08:23:19 +0000939 && sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pRight) ){
drhcce7d172000-05-31 15:34:51 +0000940 return 1;
941 }
942 if( pExpr->pList ){
943 int i;
944 ExprList *pList = pExpr->pList;
945 for(i=0; i<pList->nExpr; i++){
drh832508b2002-03-02 17:04:07 +0000946 Expr *pArg = pList->a[i].pExpr;
danielk19774adee202004-05-08 08:23:19 +0000947 if( sqlite3ExprResolveIds(pParse, pSrcList, pEList, pArg) ){
drhcce7d172000-05-31 15:34:51 +0000948 return 1;
949 }
950 }
951 }
952 }
953 }
954 return 0;
955}
956
drhcce7d172000-05-31 15:34:51 +0000957/*
drh4b59ab52002-08-24 18:24:51 +0000958** pExpr is a node that defines a function of some kind. It might
959** be a syntactic function like "count(x)" or it might be a function
960** that implements an operator, like "a LIKE b".
961**
962** This routine makes *pzName point to the name of the function and
963** *pnName hold the number of characters in the function name.
964*/
965static void getFunctionName(Expr *pExpr, const char **pzName, int *pnName){
966 switch( pExpr->op ){
967 case TK_FUNCTION: {
968 *pzName = pExpr->token.z;
drh6977fea2002-10-22 23:38:04 +0000969 *pnName = pExpr->token.n;
drh4b59ab52002-08-24 18:24:51 +0000970 break;
971 }
972 case TK_LIKE: {
973 *pzName = "like";
974 *pnName = 4;
975 break;
976 }
977 case TK_GLOB: {
978 *pzName = "glob";
979 *pnName = 4;
980 break;
981 }
982 default: {
983 *pzName = "can't happen";
984 *pnName = 12;
985 break;
986 }
987 }
988}
989
990/*
drhcce7d172000-05-31 15:34:51 +0000991** Error check the functions in an expression. Make sure all
992** function names are recognized and all functions have the correct
993** number of arguments. Leave an error message in pParse->zErrMsg
994** if anything is amiss. Return the number of errors.
995**
996** if pIsAgg is not null and this expression is an aggregate function
997** (like count(*) or max(value)) then write a 1 into *pIsAgg.
998*/
danielk19774adee202004-05-08 08:23:19 +0000999int sqlite3ExprCheck(Parse *pParse, Expr *pExpr, int allowAgg, int *pIsAgg){
drhcce7d172000-05-31 15:34:51 +00001000 int nErr = 0;
1001 if( pExpr==0 ) return 0;
drhcce7d172000-05-31 15:34:51 +00001002 switch( pExpr->op ){
drh4b59ab52002-08-24 18:24:51 +00001003 case TK_GLOB:
1004 case TK_LIKE:
drhcce7d172000-05-31 15:34:51 +00001005 case TK_FUNCTION: {
drhc9b84a12002-06-20 11:36:48 +00001006 int n = pExpr->pList ? pExpr->pList->nExpr : 0; /* Number of arguments */
1007 int no_such_func = 0; /* True if no such function exists */
drhc9b84a12002-06-20 11:36:48 +00001008 int wrong_num_args = 0; /* True if wrong number of arguments */
1009 int is_agg = 0; /* True if is an aggregate function */
drhcce7d172000-05-31 15:34:51 +00001010 int i;
drh4b59ab52002-08-24 18:24:51 +00001011 int nId; /* Number of characters in function name */
1012 const char *zId; /* The function name. */
drh0bce8352002-02-28 00:41:10 +00001013 FuncDef *pDef;
danielk1977d02eb1f2004-06-06 09:44:03 +00001014 int iPrefEnc = (pParse->db->enc==TEXT_Utf8)?0:1;
drh0bce8352002-02-28 00:41:10 +00001015
drh4b59ab52002-08-24 18:24:51 +00001016 getFunctionName(pExpr, &zId, &nId);
danielk1977d02eb1f2004-06-06 09:44:03 +00001017 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, iPrefEnc, 0);
drh0bce8352002-02-28 00:41:10 +00001018 if( pDef==0 ){
danielk1977d02eb1f2004-06-06 09:44:03 +00001019 pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, iPrefEnc, 0);
drh0bce8352002-02-28 00:41:10 +00001020 if( pDef==0 ){
drh268380c2004-02-25 13:47:31 +00001021 no_such_func = 1;
drh0bce8352002-02-28 00:41:10 +00001022 }else{
1023 wrong_num_args = 1;
drhcce7d172000-05-31 15:34:51 +00001024 }
drh0bce8352002-02-28 00:41:10 +00001025 }else{
1026 is_agg = pDef->xFunc==0;
drhcce7d172000-05-31 15:34:51 +00001027 }
drh8e0a2f92002-02-23 23:45:45 +00001028 if( is_agg && !allowAgg ){
danielk19774adee202004-05-08 08:23:19 +00001029 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId, zId);
drh8e0a2f92002-02-23 23:45:45 +00001030 nErr++;
1031 is_agg = 0;
1032 }else if( no_such_func ){
danielk19774adee202004-05-08 08:23:19 +00001033 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
drhcce7d172000-05-31 15:34:51 +00001034 nErr++;
drh8e0a2f92002-02-23 23:45:45 +00001035 }else if( wrong_num_args ){
danielk19774adee202004-05-08 08:23:19 +00001036 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
drhf7a9e1a2004-02-22 18:40:56 +00001037 nId, zId);
drh8e0a2f92002-02-23 23:45:45 +00001038 nErr++;
drhcce7d172000-05-31 15:34:51 +00001039 }
drhf7a9e1a2004-02-22 18:40:56 +00001040 if( is_agg ){
1041 pExpr->op = TK_AGG_FUNCTION;
1042 if( pIsAgg ) *pIsAgg = 1;
1043 }
drhcce7d172000-05-31 15:34:51 +00001044 for(i=0; nErr==0 && i<n; i++){
danielk19774adee202004-05-08 08:23:19 +00001045 nErr = sqlite3ExprCheck(pParse, pExpr->pList->a[i].pExpr,
drh4cfa7932000-06-08 15:10:46 +00001046 allowAgg && !is_agg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +00001047 }
danielk19770202b292004-06-09 09:55:16 +00001048 /* FIX ME: Compute pExpr->affinity based on the expected return
1049 ** type of the function
1050 */
drhcce7d172000-05-31 15:34:51 +00001051 }
1052 default: {
1053 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001054 nErr = sqlite3ExprCheck(pParse, pExpr->pLeft, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +00001055 }
1056 if( nErr==0 && pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001057 nErr = sqlite3ExprCheck(pParse, pExpr->pRight, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +00001058 }
drhfef52082000-06-06 01:50:43 +00001059 if( nErr==0 && pExpr->pList ){
1060 int n = pExpr->pList->nExpr;
1061 int i;
1062 for(i=0; nErr==0 && i<n; i++){
drh22827922000-06-06 17:27:05 +00001063 Expr *pE2 = pExpr->pList->a[i].pExpr;
danielk19774adee202004-05-08 08:23:19 +00001064 nErr = sqlite3ExprCheck(pParse, pE2, allowAgg, pIsAgg);
drhfef52082000-06-06 01:50:43 +00001065 }
1066 }
drhcce7d172000-05-31 15:34:51 +00001067 break;
1068 }
1069 }
1070 return nErr;
1071}
1072
1073/*
drhd3d39e92004-05-20 22:16:29 +00001074** Return one of the SQLITE_AFF_* affinity types that indicates the likely
1075** data type of the result of the given expression.
1076**
1077** Not every expression has a fixed type. If the type cannot be determined
1078** at compile-time, then try to return the type affinity if the expression
1079** is a column. Otherwise just return SQLITE_AFF_NONE.
drhc9b84a12002-06-20 11:36:48 +00001080**
danielk19774adee202004-05-08 08:23:19 +00001081** The sqlite3ExprResolveIds() and sqlite3ExprCheck() routines must have
drhc9b84a12002-06-20 11:36:48 +00001082** both been called on the expression before it is passed to this routine.
1083*/
danielk19774adee202004-05-08 08:23:19 +00001084int sqlite3ExprType(Expr *p){
drhd3d39e92004-05-20 22:16:29 +00001085 if( p==0 ) return SQLITE_AFF_NONE;
drhc9b84a12002-06-20 11:36:48 +00001086 while( p ) switch( p->op ){
drhc9b84a12002-06-20 11:36:48 +00001087 case TK_CONCAT:
drh736c22b2004-05-21 02:14:24 +00001088 case TK_STRING:
danielk1977c572ef72004-05-27 09:28:41 +00001089 case TK_BLOB:
drhd3d39e92004-05-20 22:16:29 +00001090 return SQLITE_AFF_TEXT;
drhc9b84a12002-06-20 11:36:48 +00001091
1092 case TK_AS:
1093 p = p->pLeft;
1094 break;
1095
drh736c22b2004-05-21 02:14:24 +00001096 case TK_VARIABLE:
drhd3d39e92004-05-20 22:16:29 +00001097 case TK_NULL:
1098 return SQLITE_AFF_NONE;
drhc9b84a12002-06-20 11:36:48 +00001099
drhd3d39e92004-05-20 22:16:29 +00001100 case TK_SELECT: /*** FIX ME ****/
1101 case TK_COLUMN: /*** FIX ME ****/
1102 case TK_CASE: /*** FIX ME ****/
drhb1363202002-06-26 02:45:03 +00001103
drhc9b84a12002-06-20 11:36:48 +00001104 default:
drhd3d39e92004-05-20 22:16:29 +00001105 return SQLITE_AFF_NUMERIC;
drhc9b84a12002-06-20 11:36:48 +00001106 }
drhd3d39e92004-05-20 22:16:29 +00001107 return SQLITE_AFF_NONE;
drhc9b84a12002-06-20 11:36:48 +00001108}
1109
1110/*
drhfec19aa2004-05-19 20:41:03 +00001111** Generate an instruction that will put the integer describe by
1112** text z[0..n-1] on the stack.
1113*/
1114static void codeInteger(Vdbe *v, const char *z, int n){
1115 int i;
drh6fec0762004-05-30 01:38:43 +00001116 if( sqlite3GetInt32(z, &i) ){
1117 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
1118 }else if( sqlite3FitsIn64Bits(z) ){
1119 sqlite3VdbeOp3(v, OP_Integer, 0, 0, z, n);
drhfec19aa2004-05-19 20:41:03 +00001120 }else{
1121 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n);
1122 }
1123}
1124
1125/*
drhcce7d172000-05-31 15:34:51 +00001126** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +00001127** expression and leave the result on the top of stack.
drhcce7d172000-05-31 15:34:51 +00001128*/
danielk19774adee202004-05-08 08:23:19 +00001129void sqlite3ExprCode(Parse *pParse, Expr *pExpr){
drhcce7d172000-05-31 15:34:51 +00001130 Vdbe *v = pParse->pVdbe;
1131 int op;
drhdaffd0e2001-04-11 14:28:42 +00001132 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001133 switch( pExpr->op ){
1134 case TK_PLUS: op = OP_Add; break;
1135 case TK_MINUS: op = OP_Subtract; break;
1136 case TK_STAR: op = OP_Multiply; break;
1137 case TK_SLASH: op = OP_Divide; break;
1138 case TK_AND: op = OP_And; break;
1139 case TK_OR: op = OP_Or; break;
1140 case TK_LT: op = OP_Lt; break;
1141 case TK_LE: op = OP_Le; break;
1142 case TK_GT: op = OP_Gt; break;
1143 case TK_GE: op = OP_Ge; break;
1144 case TK_NE: op = OP_Ne; break;
1145 case TK_EQ: op = OP_Eq; break;
drhcce7d172000-05-31 15:34:51 +00001146 case TK_ISNULL: op = OP_IsNull; break;
1147 case TK_NOTNULL: op = OP_NotNull; break;
1148 case TK_NOT: op = OP_Not; break;
1149 case TK_UMINUS: op = OP_Negative; break;
drhbf4133c2001-10-13 02:59:08 +00001150 case TK_BITAND: op = OP_BitAnd; break;
1151 case TK_BITOR: op = OP_BitOr; break;
1152 case TK_BITNOT: op = OP_BitNot; break;
1153 case TK_LSHIFT: op = OP_ShiftLeft; break;
1154 case TK_RSHIFT: op = OP_ShiftRight; break;
1155 case TK_REM: op = OP_Remainder; break;
drhfec19aa2004-05-19 20:41:03 +00001156 case TK_FLOAT: op = OP_Real; break;
danielk19770f69c1e2004-05-29 11:24:50 +00001157 case TK_STRING: op = OP_String8; break;
danielk1977c572ef72004-05-27 09:28:41 +00001158 case TK_BLOB: op = OP_HexBlob; break;
drhcce7d172000-05-31 15:34:51 +00001159 default: break;
1160 }
1161 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00001162 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +00001163 if( pParse->useAgg ){
danielk19774adee202004-05-08 08:23:19 +00001164 sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drhc4a3c772001-04-04 11:48:57 +00001165 }else if( pExpr->iColumn>=0 ){
danielk19774adee202004-05-08 08:23:19 +00001166 sqlite3VdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn);
drhc4a3c772001-04-04 11:48:57 +00001167 }else{
danielk19774adee202004-05-08 08:23:19 +00001168 sqlite3VdbeAddOp(v, OP_Recno, pExpr->iTable, 0);
drh22827922000-06-06 17:27:05 +00001169 }
drhcce7d172000-05-31 15:34:51 +00001170 break;
1171 }
1172 case TK_INTEGER: {
drhfec19aa2004-05-19 20:41:03 +00001173 codeInteger(v, pExpr->token.z, pExpr->token.n);
1174 break;
1175 }
1176 case TK_FLOAT:
1177 case TK_STRING: {
1178 sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z, pExpr->token.n);
danielk19774adee202004-05-08 08:23:19 +00001179 sqlite3VdbeDequoteP3(v, -1);
drhcce7d172000-05-31 15:34:51 +00001180 break;
1181 }
danielk1977c572ef72004-05-27 09:28:41 +00001182 case TK_BLOB: {
1183 sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z+1, pExpr->token.n-1);
1184 sqlite3VdbeDequoteP3(v, -1);
1185 break;
1186 }
drhcce7d172000-05-31 15:34:51 +00001187 case TK_NULL: {
danielk19770f69c1e2004-05-29 11:24:50 +00001188 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001189 break;
1190 }
drh50457892003-09-06 01:10:47 +00001191 case TK_VARIABLE: {
danielk19774adee202004-05-08 08:23:19 +00001192 sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0);
drh50457892003-09-06 01:10:47 +00001193 break;
1194 }
drhc9b84a12002-06-20 11:36:48 +00001195 case TK_LT:
1196 case TK_LE:
1197 case TK_GT:
1198 case TK_GE:
1199 case TK_NE:
1200 case TK_EQ: {
danielk1977a37cdde2004-05-16 11:15:36 +00001201 int p1 = binaryCompareP1(pExpr->pLeft, pExpr->pRight, 0);
danielk19777cedc8d2004-06-10 10:50:08 +00001202 CollSeq *p3 = binaryCompareCollSeq(pParse, pExpr->pLeft, pExpr->pRight);
danielk1977a37cdde2004-05-16 11:15:36 +00001203 sqlite3ExprCode(pParse, pExpr->pLeft);
1204 sqlite3ExprCode(pParse, pExpr->pRight);
danielk19770202b292004-06-09 09:55:16 +00001205 sqlite3VdbeOp3(v, op, p1, 0, (void *)p3, P3_COLLSEQ);
danielk1977a37cdde2004-05-16 11:15:36 +00001206 break;
drhc9b84a12002-06-20 11:36:48 +00001207 }
drhcce7d172000-05-31 15:34:51 +00001208 case TK_AND:
1209 case TK_OR:
1210 case TK_PLUS:
1211 case TK_STAR:
1212 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00001213 case TK_REM:
1214 case TK_BITAND:
1215 case TK_BITOR:
drhc9b84a12002-06-20 11:36:48 +00001216 case TK_SLASH: {
danielk19774adee202004-05-08 08:23:19 +00001217 sqlite3ExprCode(pParse, pExpr->pLeft);
1218 sqlite3ExprCode(pParse, pExpr->pRight);
1219 sqlite3VdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001220 break;
1221 }
drhbf4133c2001-10-13 02:59:08 +00001222 case TK_LSHIFT:
1223 case TK_RSHIFT: {
danielk19774adee202004-05-08 08:23:19 +00001224 sqlite3ExprCode(pParse, pExpr->pRight);
1225 sqlite3ExprCode(pParse, pExpr->pLeft);
1226 sqlite3VdbeAddOp(v, op, 0, 0);
drhbf4133c2001-10-13 02:59:08 +00001227 break;
1228 }
drh00400772000-06-16 20:51:26 +00001229 case TK_CONCAT: {
danielk19774adee202004-05-08 08:23:19 +00001230 sqlite3ExprCode(pParse, pExpr->pLeft);
1231 sqlite3ExprCode(pParse, pExpr->pRight);
1232 sqlite3VdbeAddOp(v, OP_Concat, 2, 0);
drh00400772000-06-16 20:51:26 +00001233 break;
1234 }
drhcce7d172000-05-31 15:34:51 +00001235 case TK_UMINUS: {
drhfec19aa2004-05-19 20:41:03 +00001236 Expr *pLeft = pExpr->pLeft;
1237 assert( pLeft );
1238 if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
1239 Token *p = &pLeft->token;
drh6e142f52000-06-08 13:36:40 +00001240 char *z = sqliteMalloc( p->n + 2 );
1241 sprintf(z, "-%.*s", p->n, p->z);
drhfec19aa2004-05-19 20:41:03 +00001242 if( pLeft->op==TK_FLOAT ){
1243 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001244 }else{
drhfec19aa2004-05-19 20:41:03 +00001245 codeInteger(v, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001246 }
drh6e142f52000-06-08 13:36:40 +00001247 sqliteFree(z);
1248 break;
1249 }
drh1ccde152000-06-17 13:12:39 +00001250 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +00001251 }
drhbf4133c2001-10-13 02:59:08 +00001252 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00001253 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001254 sqlite3ExprCode(pParse, pExpr->pLeft);
1255 sqlite3VdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001256 break;
1257 }
1258 case TK_ISNULL:
1259 case TK_NOTNULL: {
1260 int dest;
danielk19774adee202004-05-08 08:23:19 +00001261 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1262 sqlite3ExprCode(pParse, pExpr->pLeft);
1263 dest = sqlite3VdbeCurrentAddr(v) + 2;
1264 sqlite3VdbeAddOp(v, op, 1, dest);
1265 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);
drhcce7d172000-05-31 15:34:51 +00001266 }
danielk1977a37cdde2004-05-16 11:15:36 +00001267 break;
drh22827922000-06-06 17:27:05 +00001268 case TK_AGG_FUNCTION: {
danielk19774adee202004-05-08 08:23:19 +00001269 sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drh22827922000-06-06 17:27:05 +00001270 break;
1271 }
drh4b59ab52002-08-24 18:24:51 +00001272 case TK_GLOB:
1273 case TK_LIKE:
drhcce7d172000-05-31 15:34:51 +00001274 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00001275 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00001276 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00001277 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00001278 int nId;
1279 const char *zId;
danielk1977682f68b2004-06-05 10:22:17 +00001280 int p2 = 0;
1281 int i;
danielk1977d02eb1f2004-06-06 09:44:03 +00001282 int iPrefEnc = (pParse->db->enc==TEXT_Utf8)?0:1;
drh4b59ab52002-08-24 18:24:51 +00001283 getFunctionName(pExpr, &zId, &nId);
danielk1977d02eb1f2004-06-06 09:44:03 +00001284 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, iPrefEnc, 0);
drh0bce8352002-02-28 00:41:10 +00001285 assert( pDef!=0 );
drhf9b596e2004-05-26 16:54:42 +00001286 nExpr = sqlite3ExprCodeExprList(pParse, pList);
danielk1977682f68b2004-06-05 10:22:17 +00001287 for(i=0; i<nExpr && i<32; i++){
danielk1977d02eb1f2004-06-06 09:44:03 +00001288 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
1289 p2 |= (1<<i);
1290 }
danielk1977682f68b2004-06-05 10:22:17 +00001291 }
1292 sqlite3VdbeOp3(v, OP_Function, nExpr, p2, (char*)pDef, P3_FUNCDEF);
drhcce7d172000-05-31 15:34:51 +00001293 break;
1294 }
drh19a775c2000-06-05 18:54:46 +00001295 case TK_SELECT: {
danielk19774adee202004-05-08 08:23:19 +00001296 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drh19a775c2000-06-05 18:54:46 +00001297 break;
1298 }
drhfef52082000-06-06 01:50:43 +00001299 case TK_IN: {
1300 int addr;
danielk1977e014a832004-05-17 10:48:57 +00001301 char const *affStr;
1302
1303 /* Figure out the affinity to use to create a key from the results
1304 ** of the expression. affinityStr stores a static string suitable for
1305 ** P3 of OP_MakeKey.
1306 */
1307 affStr = sqlite3AffinityString(comparisonAffinity(pExpr));
1308
danielk19774adee202004-05-08 08:23:19 +00001309 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
danielk1977e014a832004-05-17 10:48:57 +00001310
1311 /* Code the <expr> from "<expr> IN (...)". The temporary table
1312 ** pExpr->iTable contains the values that make up the (...) set.
1313 */
danielk19774adee202004-05-08 08:23:19 +00001314 sqlite3ExprCode(pParse, pExpr->pLeft);
1315 addr = sqlite3VdbeCurrentAddr(v);
danielk1977e014a832004-05-17 10:48:57 +00001316 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); /* addr + 0 */
danielk19774adee202004-05-08 08:23:19 +00001317 sqlite3VdbeAddOp(v, OP_Pop, 2, 0);
danielk19770f69c1e2004-05-29 11:24:50 +00001318 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +00001319 sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7);
1320 sqlite3VdbeOp3(v, OP_MakeKey, 1, 0, affStr, P3_STATIC); /* addr + 4 */
1321 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7);
1322 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); /* addr + 6 */
1323
drhfef52082000-06-06 01:50:43 +00001324 break;
1325 }
1326 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00001327 int p1;
1328 CollSeq *p3;
danielk19774adee202004-05-08 08:23:19 +00001329 sqlite3ExprCode(pParse, pExpr->pLeft);
1330 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
1331 sqlite3ExprCode(pParse, pExpr->pList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001332 p1 = binaryCompareP1(pExpr->pLeft, pExpr->pList->a[0].pExpr, 0);
danielk19777cedc8d2004-06-10 10:50:08 +00001333 p3 = binaryCompareCollSeq(pParse, pExpr->pLeft, pExpr->pList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001334 sqlite3VdbeOp3(v, OP_Ge, p1, 0, (void *)p3, P3_COLLSEQ);
danielk19774adee202004-05-08 08:23:19 +00001335 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
1336 sqlite3ExprCode(pParse, pExpr->pList->a[1].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001337 p1 = binaryCompareP1(pExpr->pLeft, pExpr->pList->a[1].pExpr, 0);
danielk19777cedc8d2004-06-10 10:50:08 +00001338 p3 = binaryCompareCollSeq(pParse, pExpr->pLeft, pExpr->pList->a[1].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001339 sqlite3VdbeOp3(v, OP_Le, p1, 0, (void *)p3, P3_COLLSEQ);
danielk19774adee202004-05-08 08:23:19 +00001340 sqlite3VdbeAddOp(v, OP_And, 0, 0);
drhfef52082000-06-06 01:50:43 +00001341 break;
1342 }
drh51e9a442004-01-16 16:42:53 +00001343 case TK_UPLUS:
drha2e00042002-01-22 03:13:42 +00001344 case TK_AS: {
danielk19774adee202004-05-08 08:23:19 +00001345 sqlite3ExprCode(pParse, pExpr->pLeft);
drha2e00042002-01-22 03:13:42 +00001346 break;
1347 }
drh17a7f8d2002-03-24 13:13:27 +00001348 case TK_CASE: {
1349 int expr_end_label;
drhf5905aa2002-05-26 20:54:33 +00001350 int jumpInst;
1351 int addr;
1352 int nExpr;
drh17a7f8d2002-03-24 13:13:27 +00001353 int i;
1354
1355 assert(pExpr->pList);
1356 assert((pExpr->pList->nExpr % 2) == 0);
1357 assert(pExpr->pList->nExpr > 0);
drhf5905aa2002-05-26 20:54:33 +00001358 nExpr = pExpr->pList->nExpr;
danielk19774adee202004-05-08 08:23:19 +00001359 expr_end_label = sqlite3VdbeMakeLabel(v);
drh17a7f8d2002-03-24 13:13:27 +00001360 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001361 sqlite3ExprCode(pParse, pExpr->pLeft);
drh17a7f8d2002-03-24 13:13:27 +00001362 }
drhf5905aa2002-05-26 20:54:33 +00001363 for(i=0; i<nExpr; i=i+2){
danielk19774adee202004-05-08 08:23:19 +00001364 sqlite3ExprCode(pParse, pExpr->pList->a[i].pExpr);
drh17a7f8d2002-03-24 13:13:27 +00001365 if( pExpr->pLeft ){
danielk19770202b292004-06-09 09:55:16 +00001366 int p1 = binaryCompareP1(pExpr->pLeft, pExpr->pList->a[i].pExpr, 1);
danielk19777cedc8d2004-06-10 10:50:08 +00001367 CollSeq *p3 = binaryCompareCollSeq(pParse, pExpr->pLeft,
danielk19770202b292004-06-09 09:55:16 +00001368 pExpr->pList->a[i].pExpr);
danielk19774adee202004-05-08 08:23:19 +00001369 sqlite3VdbeAddOp(v, OP_Dup, 1, 1);
danielk19770202b292004-06-09 09:55:16 +00001370 jumpInst = sqlite3VdbeOp3(v, OP_Ne, p1, 0, (void *)p3, P3_COLLSEQ);
danielk19774adee202004-05-08 08:23:19 +00001371 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001372 }else{
danielk19774adee202004-05-08 08:23:19 +00001373 jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001374 }
danielk19774adee202004-05-08 08:23:19 +00001375 sqlite3ExprCode(pParse, pExpr->pList->a[i+1].pExpr);
1376 sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label);
1377 addr = sqlite3VdbeCurrentAddr(v);
1378 sqlite3VdbeChangeP2(v, jumpInst, addr);
drh17a7f8d2002-03-24 13:13:27 +00001379 }
drhf570f012002-05-31 15:51:25 +00001380 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001381 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhf570f012002-05-31 15:51:25 +00001382 }
drh17a7f8d2002-03-24 13:13:27 +00001383 if( pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001384 sqlite3ExprCode(pParse, pExpr->pRight);
drh17a7f8d2002-03-24 13:13:27 +00001385 }else{
danielk19770f69c1e2004-05-29 11:24:50 +00001386 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
drh17a7f8d2002-03-24 13:13:27 +00001387 }
danielk19774adee202004-05-08 08:23:19 +00001388 sqlite3VdbeResolveLabel(v, expr_end_label);
danielk19776f349032002-06-11 02:25:40 +00001389 break;
1390 }
1391 case TK_RAISE: {
1392 if( !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +00001393 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00001394 "RAISE() may only be used within a trigger-program");
danielk19776f349032002-06-11 02:25:40 +00001395 pParse->nErr++;
1396 return;
1397 }
1398 if( pExpr->iColumn == OE_Rollback ||
1399 pExpr->iColumn == OE_Abort ||
1400 pExpr->iColumn == OE_Fail ){
danielk19774adee202004-05-08 08:23:19 +00001401 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn,
drh701a0ae2004-02-22 20:05:00 +00001402 pExpr->token.z, pExpr->token.n);
danielk19774adee202004-05-08 08:23:19 +00001403 sqlite3VdbeDequoteP3(v, -1);
danielk19776f349032002-06-11 02:25:40 +00001404 } else {
1405 assert( pExpr->iColumn == OE_Ignore );
danielk19774adee202004-05-08 08:23:19 +00001406 sqlite3VdbeOp3(v, OP_Goto, 0, pParse->trigStack->ignoreJump,
drh701a0ae2004-02-22 20:05:00 +00001407 "(IGNORE jump)", 0);
danielk19776f349032002-06-11 02:25:40 +00001408 }
drh17a7f8d2002-03-24 13:13:27 +00001409 }
1410 break;
drhcce7d172000-05-31 15:34:51 +00001411 }
drhcce7d172000-05-31 15:34:51 +00001412}
1413
1414/*
drh268380c2004-02-25 13:47:31 +00001415** Generate code that pushes the value of every element of the given
drhf9b596e2004-05-26 16:54:42 +00001416** expression list onto the stack.
drh268380c2004-02-25 13:47:31 +00001417**
1418** Return the number of elements pushed onto the stack.
1419*/
danielk19774adee202004-05-08 08:23:19 +00001420int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00001421 Parse *pParse, /* Parsing context */
drhf9b596e2004-05-26 16:54:42 +00001422 ExprList *pList /* The expression list to be coded */
drh268380c2004-02-25 13:47:31 +00001423){
1424 struct ExprList_item *pItem;
1425 int i, n;
1426 Vdbe *v;
1427 if( pList==0 ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001428 v = sqlite3GetVdbe(pParse);
drh268380c2004-02-25 13:47:31 +00001429 n = pList->nExpr;
1430 for(pItem=pList->a, i=0; i<n; i++, pItem++){
danielk19774adee202004-05-08 08:23:19 +00001431 sqlite3ExprCode(pParse, pItem->pExpr);
drh268380c2004-02-25 13:47:31 +00001432 }
drhf9b596e2004-05-26 16:54:42 +00001433 return n;
drh268380c2004-02-25 13:47:31 +00001434}
1435
1436/*
drhcce7d172000-05-31 15:34:51 +00001437** Generate code for a boolean expression such that a jump is made
1438** to the label "dest" if the expression is true but execution
1439** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00001440**
1441** If the expression evaluates to NULL (neither true nor false), then
1442** take the jump if the jumpIfNull flag is true.
drhcce7d172000-05-31 15:34:51 +00001443*/
danielk19774adee202004-05-08 08:23:19 +00001444void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001445 Vdbe *v = pParse->pVdbe;
1446 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001447 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001448 switch( pExpr->op ){
1449 case TK_LT: op = OP_Lt; break;
1450 case TK_LE: op = OP_Le; break;
1451 case TK_GT: op = OP_Gt; break;
1452 case TK_GE: op = OP_Ge; break;
1453 case TK_NE: op = OP_Ne; break;
1454 case TK_EQ: op = OP_Eq; break;
drhcce7d172000-05-31 15:34:51 +00001455 case TK_ISNULL: op = OP_IsNull; break;
1456 case TK_NOTNULL: op = OP_NotNull; break;
1457 default: break;
1458 }
1459 switch( pExpr->op ){
1460 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001461 int d2 = sqlite3VdbeMakeLabel(v);
1462 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
1463 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
1464 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001465 break;
1466 }
1467 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001468 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
1469 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001470 break;
1471 }
1472 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001473 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001474 break;
1475 }
1476 case TK_LT:
1477 case TK_LE:
1478 case TK_GT:
1479 case TK_GE:
1480 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00001481 case TK_EQ: {
danielk1977a37cdde2004-05-16 11:15:36 +00001482 int p1 = binaryCompareP1(pExpr->pLeft, pExpr->pRight, jumpIfNull);
danielk19777cedc8d2004-06-10 10:50:08 +00001483 CollSeq *p3 = binaryCompareCollSeq(pParse, pExpr->pLeft, pExpr->pRight);
danielk19774adee202004-05-08 08:23:19 +00001484 sqlite3ExprCode(pParse, pExpr->pLeft);
1485 sqlite3ExprCode(pParse, pExpr->pRight);
danielk19770202b292004-06-09 09:55:16 +00001486 sqlite3VdbeOp3(v, op, p1, dest, (void *)p3, P3_COLLSEQ);
drhcce7d172000-05-31 15:34:51 +00001487 break;
1488 }
1489 case TK_ISNULL:
1490 case TK_NOTNULL: {
danielk19774adee202004-05-08 08:23:19 +00001491 sqlite3ExprCode(pParse, pExpr->pLeft);
1492 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001493 break;
1494 }
drhfef52082000-06-06 01:50:43 +00001495 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00001496 /* The expression "x BETWEEN y AND z" is implemented as:
1497 **
1498 ** 1 IF (x < y) GOTO 3
1499 ** 2 IF (x <= z) GOTO <dest>
1500 ** 3 ...
1501 */
drhf5905aa2002-05-26 20:54:33 +00001502 int addr;
danielk19770202b292004-06-09 09:55:16 +00001503 int p1;
1504 CollSeq *p3;
danielk19774adee202004-05-08 08:23:19 +00001505 sqlite3ExprCode(pParse, pExpr->pLeft);
1506 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
1507 sqlite3ExprCode(pParse, pExpr->pList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001508 p1 = binaryCompareP1(pExpr->pLeft, pExpr->pList->a[0].pExpr, !jumpIfNull);
danielk19777cedc8d2004-06-10 10:50:08 +00001509 p3 = binaryCompareCollSeq(pParse, pExpr->pLeft, pExpr->pList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001510 addr = sqlite3VdbeOp3(v, OP_Lt, p1, 0, (void *)p3, P3_COLLSEQ);
1511
danielk19774adee202004-05-08 08:23:19 +00001512 sqlite3ExprCode(pParse, pExpr->pList->a[1].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001513 p1 = binaryCompareP1(pExpr->pLeft, pExpr->pList->a[1].pExpr, jumpIfNull);
danielk19777cedc8d2004-06-10 10:50:08 +00001514 p3 = binaryCompareCollSeq(pParse, pExpr->pLeft, pExpr->pList->a[1].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001515 sqlite3VdbeOp3(v, OP_Le, p1, dest, (void *)p3, P3_COLLSEQ);
1516
danielk19774adee202004-05-08 08:23:19 +00001517 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
1518 sqlite3VdbeChangeP2(v, addr, sqlite3VdbeCurrentAddr(v));
1519 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00001520 break;
1521 }
drhcce7d172000-05-31 15:34:51 +00001522 default: {
danielk19774adee202004-05-08 08:23:19 +00001523 sqlite3ExprCode(pParse, pExpr);
1524 sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001525 break;
1526 }
1527 }
1528}
1529
1530/*
drh66b89c82000-11-28 20:47:17 +00001531** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00001532** to the label "dest" if the expression is false but execution
1533** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00001534**
1535** If the expression evaluates to NULL (neither true nor false) then
1536** jump if jumpIfNull is true or fall through if jumpIfNull is false.
drhcce7d172000-05-31 15:34:51 +00001537*/
danielk19774adee202004-05-08 08:23:19 +00001538void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001539 Vdbe *v = pParse->pVdbe;
1540 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001541 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001542 switch( pExpr->op ){
1543 case TK_LT: op = OP_Ge; break;
1544 case TK_LE: op = OP_Gt; break;
1545 case TK_GT: op = OP_Le; break;
1546 case TK_GE: op = OP_Lt; break;
1547 case TK_NE: op = OP_Eq; break;
1548 case TK_EQ: op = OP_Ne; break;
drhcce7d172000-05-31 15:34:51 +00001549 case TK_ISNULL: op = OP_NotNull; break;
1550 case TK_NOTNULL: op = OP_IsNull; break;
1551 default: break;
1552 }
1553 switch( pExpr->op ){
1554 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001555 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
1556 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001557 break;
1558 }
1559 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001560 int d2 = sqlite3VdbeMakeLabel(v);
1561 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
1562 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
1563 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001564 break;
1565 }
1566 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001567 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001568 break;
1569 }
1570 case TK_LT:
1571 case TK_LE:
1572 case TK_GT:
1573 case TK_GE:
1574 case TK_NE:
1575 case TK_EQ: {
danielk1977a37cdde2004-05-16 11:15:36 +00001576 int p1 = binaryCompareP1(pExpr->pLeft, pExpr->pRight, jumpIfNull);
danielk19777cedc8d2004-06-10 10:50:08 +00001577 CollSeq *p3 = binaryCompareCollSeq(pParse, pExpr->pLeft, pExpr->pRight);
danielk19774adee202004-05-08 08:23:19 +00001578 sqlite3ExprCode(pParse, pExpr->pLeft);
1579 sqlite3ExprCode(pParse, pExpr->pRight);
danielk19770202b292004-06-09 09:55:16 +00001580 sqlite3VdbeOp3(v, op, p1, dest, (void *)p3, P3_COLLSEQ);
drhcce7d172000-05-31 15:34:51 +00001581 break;
1582 }
drhcce7d172000-05-31 15:34:51 +00001583 case TK_ISNULL:
1584 case TK_NOTNULL: {
danielk19774adee202004-05-08 08:23:19 +00001585 sqlite3ExprCode(pParse, pExpr->pLeft);
1586 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001587 break;
1588 }
drhfef52082000-06-06 01:50:43 +00001589 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00001590 /* The expression is "x BETWEEN y AND z". It is implemented as:
1591 **
1592 ** 1 IF (x >= y) GOTO 3
1593 ** 2 GOTO <dest>
1594 ** 3 IF (x > z) GOTO <dest>
1595 */
drhfef52082000-06-06 01:50:43 +00001596 int addr;
danielk19770202b292004-06-09 09:55:16 +00001597 int p1;
1598 CollSeq *p3;
danielk19774adee202004-05-08 08:23:19 +00001599 sqlite3ExprCode(pParse, pExpr->pLeft);
1600 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
1601 sqlite3ExprCode(pParse, pExpr->pList->a[0].pExpr);
1602 addr = sqlite3VdbeCurrentAddr(v);
danielk19770202b292004-06-09 09:55:16 +00001603 p1 = binaryCompareP1(pExpr->pLeft, pExpr->pList->a[0].pExpr, !jumpIfNull);
danielk19777cedc8d2004-06-10 10:50:08 +00001604 p3 = binaryCompareCollSeq(pParse, pExpr->pLeft, pExpr->pList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001605 sqlite3VdbeOp3(v, OP_Ge, p1, addr+3, (void *)p3, P3_COLLSEQ);
danielk19774adee202004-05-08 08:23:19 +00001606 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
1607 sqlite3VdbeAddOp(v, OP_Goto, 0, dest);
1608 sqlite3ExprCode(pParse, pExpr->pList->a[1].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001609 p1 = binaryCompareP1(pExpr->pLeft, pExpr->pList->a[1].pExpr, jumpIfNull);
danielk19777cedc8d2004-06-10 10:50:08 +00001610 p3 = binaryCompareCollSeq(pParse, pExpr->pLeft, pExpr->pList->a[1].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001611 sqlite3VdbeOp3(v, OP_Gt, p1, dest, (void *)p3, P3_COLLSEQ);
drhfef52082000-06-06 01:50:43 +00001612 break;
1613 }
drhcce7d172000-05-31 15:34:51 +00001614 default: {
danielk19774adee202004-05-08 08:23:19 +00001615 sqlite3ExprCode(pParse, pExpr);
1616 sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001617 break;
1618 }
1619 }
1620}
drh22827922000-06-06 17:27:05 +00001621
1622/*
1623** Do a deep comparison of two expression trees. Return TRUE (non-zero)
1624** if they are identical and return FALSE if they differ in any way.
1625*/
danielk19774adee202004-05-08 08:23:19 +00001626int sqlite3ExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00001627 int i;
1628 if( pA==0 ){
1629 return pB==0;
1630 }else if( pB==0 ){
1631 return 0;
1632 }
1633 if( pA->op!=pB->op ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001634 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
1635 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00001636 if( pA->pList ){
1637 if( pB->pList==0 ) return 0;
1638 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
1639 for(i=0; i<pA->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00001640 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00001641 return 0;
1642 }
1643 }
1644 }else if( pB->pList ){
1645 return 0;
1646 }
1647 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00001648 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drh22827922000-06-06 17:27:05 +00001649 if( pA->token.z ){
1650 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00001651 if( pB->token.n!=pA->token.n ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001652 if( sqlite3StrNICmp(pA->token.z, pB->token.z, pB->token.n)!=0 ) return 0;
drh22827922000-06-06 17:27:05 +00001653 }
1654 return 1;
1655}
1656
1657/*
1658** Add a new element to the pParse->aAgg[] array and return its index.
1659*/
1660static int appendAggInfo(Parse *pParse){
1661 if( (pParse->nAgg & 0x7)==0 ){
1662 int amt = pParse->nAgg + 8;
drh6d4abfb2001-10-22 02:58:08 +00001663 AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0]));
1664 if( aAgg==0 ){
drh22827922000-06-06 17:27:05 +00001665 return -1;
1666 }
drh6d4abfb2001-10-22 02:58:08 +00001667 pParse->aAgg = aAgg;
drh22827922000-06-06 17:27:05 +00001668 }
1669 memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0]));
1670 return pParse->nAgg++;
1671}
1672
1673/*
1674** Analyze the given expression looking for aggregate functions and
1675** for variables that need to be added to the pParse->aAgg[] array.
1676** Make additional entries to the pParse->aAgg[] array as necessary.
1677**
1678** This routine should only be called after the expression has been
danielk19774adee202004-05-08 08:23:19 +00001679** analyzed by sqlite3ExprResolveIds() and sqlite3ExprCheck().
drh22827922000-06-06 17:27:05 +00001680**
1681** If errors are seen, leave an error message in zErrMsg and return
1682** the number of errors.
1683*/
danielk19774adee202004-05-08 08:23:19 +00001684int sqlite3ExprAnalyzeAggregates(Parse *pParse, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00001685 int i;
1686 AggExpr *aAgg;
1687 int nErr = 0;
1688
1689 if( pExpr==0 ) return 0;
1690 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00001691 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +00001692 aAgg = pParse->aAgg;
1693 for(i=0; i<pParse->nAgg; i++){
1694 if( aAgg[i].isAgg ) continue;
1695 if( aAgg[i].pExpr->iTable==pExpr->iTable
drh967e8b72000-06-21 13:59:10 +00001696 && aAgg[i].pExpr->iColumn==pExpr->iColumn ){
drh22827922000-06-06 17:27:05 +00001697 break;
1698 }
1699 }
1700 if( i>=pParse->nAgg ){
1701 i = appendAggInfo(pParse);
1702 if( i<0 ) return 1;
1703 pParse->aAgg[i].isAgg = 0;
1704 pParse->aAgg[i].pExpr = pExpr;
1705 }
drhaaf88722000-06-08 11:25:00 +00001706 pExpr->iAgg = i;
drh22827922000-06-06 17:27:05 +00001707 break;
1708 }
1709 case TK_AGG_FUNCTION: {
drh22827922000-06-06 17:27:05 +00001710 aAgg = pParse->aAgg;
1711 for(i=0; i<pParse->nAgg; i++){
1712 if( !aAgg[i].isAgg ) continue;
danielk19774adee202004-05-08 08:23:19 +00001713 if( sqlite3ExprCompare(aAgg[i].pExpr, pExpr) ){
drh22827922000-06-06 17:27:05 +00001714 break;
1715 }
1716 }
1717 if( i>=pParse->nAgg ){
danielk1977d02eb1f2004-06-06 09:44:03 +00001718 int iPrefEnc = (pParse->db->enc==TEXT_Utf8)?0:1;
drh22827922000-06-06 17:27:05 +00001719 i = appendAggInfo(pParse);
1720 if( i<0 ) return 1;
1721 pParse->aAgg[i].isAgg = 1;
1722 pParse->aAgg[i].pExpr = pExpr;
danielk19774adee202004-05-08 08:23:19 +00001723 pParse->aAgg[i].pFunc = sqlite3FindFunction(pParse->db,
drh6977fea2002-10-22 23:38:04 +00001724 pExpr->token.z, pExpr->token.n,
danielk1977d02eb1f2004-06-06 09:44:03 +00001725 pExpr->pList ? pExpr->pList->nExpr : 0, iPrefEnc, 0);
drh22827922000-06-06 17:27:05 +00001726 }
1727 pExpr->iAgg = i;
1728 break;
1729 }
1730 default: {
1731 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001732 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pLeft);
drh22827922000-06-06 17:27:05 +00001733 }
1734 if( nErr==0 && pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001735 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pRight);
drh22827922000-06-06 17:27:05 +00001736 }
1737 if( nErr==0 && pExpr->pList ){
1738 int n = pExpr->pList->nExpr;
1739 int i;
1740 for(i=0; nErr==0 && i<n; i++){
danielk19774adee202004-05-08 08:23:19 +00001741 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pList->a[i].pExpr);
drh22827922000-06-06 17:27:05 +00001742 }
1743 }
1744 break;
1745 }
1746 }
1747 return nErr;
1748}
drh8e0a2f92002-02-23 23:45:45 +00001749
1750/*
danielk1977d02eb1f2004-06-06 09:44:03 +00001751** Locate a user function given a name, a number of arguments and a flag
1752** indicating whether the function prefers UTF-16 over UTF-8. Return a
1753** pointer to the FuncDef structure that defines that function, or return
1754** NULL if the function does not exist.
drh8e0a2f92002-02-23 23:45:45 +00001755**
drh0bce8352002-02-28 00:41:10 +00001756** If the createFlag argument is true, then a new (blank) FuncDef
drh8e0a2f92002-02-23 23:45:45 +00001757** structure is created and liked into the "db" structure if a
1758** no matching function previously existed. When createFlag is true
1759** and the nArg parameter is -1, then only a function that accepts
1760** any number of arguments will be returned.
1761**
1762** If createFlag is false and nArg is -1, then the first valid
1763** function found is returned. A function is valid if either xFunc
1764** or xStep is non-zero.
danielk1977d02eb1f2004-06-06 09:44:03 +00001765**
1766** If createFlag is false, then a function with the required name and
1767** number of arguments may be returned even if the eTextRep flag does not
1768** match that requested.
drh8e0a2f92002-02-23 23:45:45 +00001769*/
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 */
danielk1977d02eb1f2004-06-06 09:44:03 +00001775 int eTextRep, /* True to retrieve UTF-16 versions. */
drh8e0a2f92002-02-23 23:45:45 +00001776 int createFlag /* Create new entry if true and does not otherwise exist */
1777){
danielk1977d02eb1f2004-06-06 09:44:03 +00001778 FuncDef *p; /* Iterator variable */
1779 FuncDef *pFirst; /* First function with this name */
1780 FuncDef *pBest = 0; /* Best match found so far */
1781 int matchqual = 0;
1782
1783 /* Normalize argument values to simplify comparisons below. */
1784 if( eTextRep ) eTextRep = 1;
1785 if( nArg<-1 ) nArg = -1;
1786
1787 pFirst = (FuncDef*)sqlite3HashFind(&db->aFunc, zName, nName);
1788 for(p=pFirst; p; p=p->pNext){
1789 if( 1 || p->xFunc || p->xStep ){
1790 if( p->nArg==nArg && p->iPrefEnc==eTextRep ){
1791 /* A perfect match. */
1792 pBest = p;
1793 matchqual = 4;
1794 break;
1795 }
1796 if( p->nArg==nArg ){
1797 /* Number of arguments matches, but not the text encoding */
1798 pBest = p;
1799 matchqual = 3;
1800 }
1801 else if( (p->nArg<0) || (nArg<0) ){
1802 if( matchqual<2 && p->iPrefEnc==eTextRep ){
1803 /* Matched a varargs function with correct text encoding */
1804 pBest = p;
1805 matchqual = 2;
1806 }
1807 if( matchqual<1 ){
1808 /* Matched a varargs function with incorrect text encoding */
1809 pBest = p;
1810 matchqual = 1;
1811 }
1812 }
1813 }
drh8e0a2f92002-02-23 23:45:45 +00001814 }
danielk1977d02eb1f2004-06-06 09:44:03 +00001815
1816 if( createFlag && matchqual<4 &&
1817 (pBest = sqliteMalloc(sizeof(*pBest)+nName+1)) ){
1818 pBest->nArg = nArg;
1819 pBest->pNext = pFirst;
1820 pBest->zName = (char*)&pBest[1];
danielk1977ad7dd422004-06-06 12:41:49 +00001821 pBest->iPrefEnc = eTextRep;
danielk1977d02eb1f2004-06-06 09:44:03 +00001822 memcpy(pBest->zName, zName, nName);
1823 pBest->zName[nName] = 0;
1824 sqlite3HashInsert(&db->aFunc, pBest->zName, nName, (void*)pBest);
drh8e0a2f92002-02-23 23:45:45 +00001825 }
danielk1977d02eb1f2004-06-06 09:44:03 +00001826
1827 if( pBest && (pBest->xStep || pBest->xFunc || createFlag) ){
1828 return pBest;
drh8e0a2f92002-02-23 23:45:45 +00001829 }
danielk1977d02eb1f2004-06-06 09:44:03 +00001830 return 0;
drh8e0a2f92002-02-23 23:45:45 +00001831}
danielk1977d02eb1f2004-06-06 09:44:03 +00001832