blob: d43eb0dbfb8a4f6bf466718e7c1dfa8d6d42a4e8 [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**
danielk1977e0048402004-06-15 16:51:01 +000015** $Id: expr.c,v 1.142 2004/06/15 16:51:01 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]) );
danielk1977e0048402004-06-15 16:51:01 +0000310 if( pItem==0 ){
311 sqliteFree(pNew);
312 return 0;
313 }
drh1bdd9b52004-04-23 17:04:44 +0000314 for(i=0; i<p->nExpr; i++, pItem++){
drh4b59ab52002-08-24 18:24:51 +0000315 Expr *pNewExpr, *pOldExpr;
danielk19774adee202004-05-08 08:23:19 +0000316 pItem->pExpr = pNewExpr = sqlite3ExprDup(pOldExpr = p->a[i].pExpr);
drh6977fea2002-10-22 23:38:04 +0000317 if( pOldExpr->span.z!=0 && pNewExpr ){
318 /* Always make a copy of the span for top-level expressions in the
drh4b59ab52002-08-24 18:24:51 +0000319 ** expression list. The logic in SELECT processing that determines
320 ** the names of columns in the result set needs this information */
danielk19774adee202004-05-08 08:23:19 +0000321 sqlite3TokenCopy(&pNewExpr->span, &pOldExpr->span);
drh4b59ab52002-08-24 18:24:51 +0000322 }
drh1f3e9052002-10-31 00:09:39 +0000323 assert( pNewExpr==0 || pNewExpr->span.z!=0
danielk197724b03fd2004-05-10 10:34:34 +0000324 || pOldExpr->span.z==0 || sqlite3_malloc_failed );
drh3e7bc9c2004-02-21 19:17:17 +0000325 pItem->zName = sqliteStrDup(p->a[i].zName);
326 pItem->sortOrder = p->a[i].sortOrder;
327 pItem->isAgg = p->a[i].isAgg;
328 pItem->done = 0;
drhff78bd22002-02-27 01:47:11 +0000329 }
330 return pNew;
331}
danielk19774adee202004-05-08 08:23:19 +0000332SrcList *sqlite3SrcListDup(SrcList *p){
drhad3cab52002-05-24 02:04:32 +0000333 SrcList *pNew;
334 int i;
drh113088e2003-03-20 01:16:58 +0000335 int nByte;
drhad3cab52002-05-24 02:04:32 +0000336 if( p==0 ) return 0;
drh113088e2003-03-20 01:16:58 +0000337 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
drh4efc4752004-01-16 15:55:37 +0000338 pNew = sqliteMallocRaw( nByte );
drhad3cab52002-05-24 02:04:32 +0000339 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000340 pNew->nSrc = pNew->nAlloc = p->nSrc;
drhad3cab52002-05-24 02:04:32 +0000341 for(i=0; i<p->nSrc; i++){
drh4efc4752004-01-16 15:55:37 +0000342 struct SrcList_item *pNewItem = &pNew->a[i];
343 struct SrcList_item *pOldItem = &p->a[i];
344 pNewItem->zDatabase = sqliteStrDup(pOldItem->zDatabase);
345 pNewItem->zName = sqliteStrDup(pOldItem->zName);
346 pNewItem->zAlias = sqliteStrDup(pOldItem->zAlias);
347 pNewItem->jointype = pOldItem->jointype;
348 pNewItem->iCursor = pOldItem->iCursor;
349 pNewItem->pTab = 0;
danielk19774adee202004-05-08 08:23:19 +0000350 pNewItem->pSelect = sqlite3SelectDup(pOldItem->pSelect);
351 pNewItem->pOn = sqlite3ExprDup(pOldItem->pOn);
352 pNewItem->pUsing = sqlite3IdListDup(pOldItem->pUsing);
drhad3cab52002-05-24 02:04:32 +0000353 }
354 return pNew;
355}
danielk19774adee202004-05-08 08:23:19 +0000356IdList *sqlite3IdListDup(IdList *p){
drhff78bd22002-02-27 01:47:11 +0000357 IdList *pNew;
358 int i;
359 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000360 pNew = sqliteMallocRaw( sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +0000361 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000362 pNew->nId = pNew->nAlloc = p->nId;
drh4efc4752004-01-16 15:55:37 +0000363 pNew->a = sqliteMallocRaw( p->nId*sizeof(p->a[0]) );
drhe4697f52002-05-23 02:09:03 +0000364 if( pNew->a==0 ) return 0;
drhff78bd22002-02-27 01:47:11 +0000365 for(i=0; i<p->nId; i++){
drh4efc4752004-01-16 15:55:37 +0000366 struct IdList_item *pNewItem = &pNew->a[i];
367 struct IdList_item *pOldItem = &p->a[i];
368 pNewItem->zName = sqliteStrDup(pOldItem->zName);
369 pNewItem->idx = pOldItem->idx;
drhff78bd22002-02-27 01:47:11 +0000370 }
371 return pNew;
372}
danielk19774adee202004-05-08 08:23:19 +0000373Select *sqlite3SelectDup(Select *p){
drhff78bd22002-02-27 01:47:11 +0000374 Select *pNew;
375 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000376 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000377 if( pNew==0 ) return 0;
378 pNew->isDistinct = p->isDistinct;
danielk19774adee202004-05-08 08:23:19 +0000379 pNew->pEList = sqlite3ExprListDup(p->pEList);
380 pNew->pSrc = sqlite3SrcListDup(p->pSrc);
381 pNew->pWhere = sqlite3ExprDup(p->pWhere);
382 pNew->pGroupBy = sqlite3ExprListDup(p->pGroupBy);
383 pNew->pHaving = sqlite3ExprDup(p->pHaving);
384 pNew->pOrderBy = sqlite3ExprListDup(p->pOrderBy);
drhff78bd22002-02-27 01:47:11 +0000385 pNew->op = p->op;
danielk19774adee202004-05-08 08:23:19 +0000386 pNew->pPrior = sqlite3SelectDup(p->pPrior);
drhff78bd22002-02-27 01:47:11 +0000387 pNew->nLimit = p->nLimit;
388 pNew->nOffset = p->nOffset;
389 pNew->zSelect = 0;
drh7b58dae2003-07-20 01:16:46 +0000390 pNew->iLimit = -1;
391 pNew->iOffset = -1;
danielk1977dc1bdc42004-06-11 10:51:27 +0000392 pNew->ppOpenTemp = 0;
drhff78bd22002-02-27 01:47:11 +0000393 return pNew;
394}
395
396
397/*
drha76b5df2002-02-23 02:32:10 +0000398** Add a new element to the end of an expression list. If pList is
399** initially NULL, then create a new expression list.
400*/
danielk19774adee202004-05-08 08:23:19 +0000401ExprList *sqlite3ExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){
drha76b5df2002-02-23 02:32:10 +0000402 if( pList==0 ){
403 pList = sqliteMalloc( sizeof(ExprList) );
404 if( pList==0 ){
danielk19774adee202004-05-08 08:23:19 +0000405 /* sqlite3ExprDelete(pExpr); // Leak memory if malloc fails */
drha76b5df2002-02-23 02:32:10 +0000406 return 0;
407 }
drh4efc4752004-01-16 15:55:37 +0000408 assert( pList->nAlloc==0 );
drha76b5df2002-02-23 02:32:10 +0000409 }
drh4305d102003-07-30 12:34:12 +0000410 if( pList->nAlloc<=pList->nExpr ){
drh4305d102003-07-30 12:34:12 +0000411 pList->nAlloc = pList->nAlloc*2 + 4;
drh4efc4752004-01-16 15:55:37 +0000412 pList->a = sqliteRealloc(pList->a, pList->nAlloc*sizeof(pList->a[0]));
413 if( pList->a==0 ){
danielk19774adee202004-05-08 08:23:19 +0000414 /* sqlite3ExprDelete(pExpr); // Leak memory if malloc fails */
drh4efc4752004-01-16 15:55:37 +0000415 pList->nExpr = pList->nAlloc = 0;
drha76b5df2002-02-23 02:32:10 +0000416 return pList;
417 }
drha76b5df2002-02-23 02:32:10 +0000418 }
drh4efc4752004-01-16 15:55:37 +0000419 assert( pList->a!=0 );
420 if( pExpr || pName ){
421 struct ExprList_item *pItem = &pList->a[pList->nExpr++];
422 memset(pItem, 0, sizeof(*pItem));
423 pItem->pExpr = pExpr;
drha76b5df2002-02-23 02:32:10 +0000424 if( pName ){
danielk19774adee202004-05-08 08:23:19 +0000425 sqlite3SetNString(&pItem->zName, pName->z, pName->n, 0);
426 sqlite3Dequote(pItem->zName);
drha76b5df2002-02-23 02:32:10 +0000427 }
428 }
429 return pList;
430}
431
432/*
433** Delete an entire expression list.
434*/
danielk19774adee202004-05-08 08:23:19 +0000435void sqlite3ExprListDelete(ExprList *pList){
drha76b5df2002-02-23 02:32:10 +0000436 int i;
437 if( pList==0 ) return;
drh1bdd9b52004-04-23 17:04:44 +0000438 assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
439 assert( pList->nExpr<=pList->nAlloc );
drha76b5df2002-02-23 02:32:10 +0000440 for(i=0; i<pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +0000441 sqlite3ExprDelete(pList->a[i].pExpr);
drha76b5df2002-02-23 02:32:10 +0000442 sqliteFree(pList->a[i].zName);
443 }
444 sqliteFree(pList->a);
445 sqliteFree(pList);
446}
447
448/*
drhfef52082000-06-06 01:50:43 +0000449** Walk an expression tree. Return 1 if the expression is constant
450** and 0 if it involves variables.
drh23989372002-05-21 13:43:04 +0000451**
452** For the purposes of this function, a double-quoted string (ex: "abc")
453** is considered a variable but a single-quoted string (ex: 'abc') is
454** a constant.
drhfef52082000-06-06 01:50:43 +0000455*/
danielk19774adee202004-05-08 08:23:19 +0000456int sqlite3ExprIsConstant(Expr *p){
drhfef52082000-06-06 01:50:43 +0000457 switch( p->op ){
458 case TK_ID:
drh967e8b72000-06-21 13:59:10 +0000459 case TK_COLUMN:
drhfef52082000-06-06 01:50:43 +0000460 case TK_DOT:
drh7bdc0c12003-04-19 17:27:24 +0000461 case TK_FUNCTION:
drhfef52082000-06-06 01:50:43 +0000462 return 0;
drh7bdc0c12003-04-19 17:27:24 +0000463 case TK_NULL:
drh23989372002-05-21 13:43:04 +0000464 case TK_STRING:
danielk1977c572ef72004-05-27 09:28:41 +0000465 case TK_BLOB:
drh92086432002-01-22 14:11:29 +0000466 case TK_INTEGER:
467 case TK_FLOAT:
drh50457892003-09-06 01:10:47 +0000468 case TK_VARIABLE:
drh92086432002-01-22 14:11:29 +0000469 return 1;
drhfef52082000-06-06 01:50:43 +0000470 default: {
danielk19774adee202004-05-08 08:23:19 +0000471 if( p->pLeft && !sqlite3ExprIsConstant(p->pLeft) ) return 0;
472 if( p->pRight && !sqlite3ExprIsConstant(p->pRight) ) return 0;
drhfef52082000-06-06 01:50:43 +0000473 if( p->pList ){
474 int i;
475 for(i=0; i<p->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +0000476 if( !sqlite3ExprIsConstant(p->pList->a[i].pExpr) ) return 0;
drhfef52082000-06-06 01:50:43 +0000477 }
478 }
drh92086432002-01-22 14:11:29 +0000479 return p->pLeft!=0 || p->pRight!=0 || (p->pList && p->pList->nExpr>0);
drhfef52082000-06-06 01:50:43 +0000480 }
481 }
drh92086432002-01-22 14:11:29 +0000482 return 0;
drhfef52082000-06-06 01:50:43 +0000483}
484
485/*
drh202b2df2004-01-06 01:13:46 +0000486** If the given expression codes a constant integer that is small enough
487** to fit in a 32-bit integer, return 1 and put the value of the integer
488** in *pValue. If the expression is not an integer or if it is too big
489** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
drhe4de1fe2002-06-02 16:09:01 +0000490*/
danielk19774adee202004-05-08 08:23:19 +0000491int sqlite3ExprIsInteger(Expr *p, int *pValue){
drhe4de1fe2002-06-02 16:09:01 +0000492 switch( p->op ){
493 case TK_INTEGER: {
drhfec19aa2004-05-19 20:41:03 +0000494 if( sqlite3GetInt32(p->token.z, pValue) ){
drh202b2df2004-01-06 01:13:46 +0000495 return 1;
496 }
497 break;
drhe4de1fe2002-06-02 16:09:01 +0000498 }
499 case TK_STRING: {
drhbd790ee2002-06-02 18:22:06 +0000500 const char *z = p->token.z;
drhe4de1fe2002-06-02 16:09:01 +0000501 int n = p->token.n;
drhbd790ee2002-06-02 18:22:06 +0000502 if( n>0 && z[0]=='-' ){ z++; n--; }
drhe4de1fe2002-06-02 16:09:01 +0000503 while( n>0 && *z && isdigit(*z) ){ z++; n--; }
drhfec19aa2004-05-19 20:41:03 +0000504 if( n==0 && sqlite3GetInt32(p->token.z, pValue) ){
drhe4de1fe2002-06-02 16:09:01 +0000505 return 1;
506 }
507 break;
508 }
drh4b59ab52002-08-24 18:24:51 +0000509 case TK_UPLUS: {
danielk19774adee202004-05-08 08:23:19 +0000510 return sqlite3ExprIsInteger(p->pLeft, pValue);
drh4b59ab52002-08-24 18:24:51 +0000511 }
drhe4de1fe2002-06-02 16:09:01 +0000512 case TK_UMINUS: {
513 int v;
danielk19774adee202004-05-08 08:23:19 +0000514 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
drhe4de1fe2002-06-02 16:09:01 +0000515 *pValue = -v;
516 return 1;
517 }
518 break;
519 }
520 default: break;
521 }
522 return 0;
523}
524
525/*
drhc4a3c772001-04-04 11:48:57 +0000526** Return TRUE if the given string is a row-id column name.
527*/
danielk19774adee202004-05-08 08:23:19 +0000528int sqlite3IsRowid(const char *z){
529 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
530 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
531 if( sqlite3StrICmp(z, "OID")==0 ) return 1;
drhc4a3c772001-04-04 11:48:57 +0000532 return 0;
533}
534
535/*
drh8141f612004-01-25 22:44:58 +0000536** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
537** that name in the set of source tables in pSrcList and make the pExpr
538** expression node refer back to that source column. The following changes
539** are made to pExpr:
540**
541** pExpr->iDb Set the index in db->aDb[] of the database holding
542** the table.
543** pExpr->iTable Set to the cursor number for the table obtained
544** from pSrcList.
545** pExpr->iColumn Set to the column number within the table.
drh8141f612004-01-25 22:44:58 +0000546** pExpr->op Set to TK_COLUMN.
547** pExpr->pLeft Any expression this points to is deleted
548** pExpr->pRight Any expression this points to is deleted.
549**
550** The pDbToken is the name of the database (the "X"). This value may be
551** NULL meaning that name is of the form Y.Z or Z. Any available database
552** can be used. The pTableToken is the name of the table (the "Y"). This
553** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it
554** means that the form of the name is Z and that columns from any table
555** can be used.
556**
557** If the name cannot be resolved unambiguously, leave an error message
558** in pParse and return non-zero. Return zero on success.
559*/
560static int lookupName(
561 Parse *pParse, /* The parsing context */
562 Token *pDbToken, /* Name of the database containing table, or NULL */
563 Token *pTableToken, /* Name of table containing column, or NULL */
564 Token *pColumnToken, /* Name of the column. */
565 SrcList *pSrcList, /* List of tables used to resolve column names */
566 ExprList *pEList, /* List of expressions used to resolve "AS" */
567 Expr *pExpr /* Make this EXPR node point to the selected column */
568){
569 char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */
570 char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */
571 char *zCol = 0; /* Name of the column. The "Z" */
572 int i, j; /* Loop counters */
573 int cnt = 0; /* Number of matching column names */
574 int cntTab = 0; /* Number of matching table names */
drh7e26d752004-02-11 10:35:29 +0000575 sqlite *db = pParse->db; /* The database */
drh8141f612004-01-25 22:44:58 +0000576
577 assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
578 if( pDbToken && pDbToken->z ){
579 zDb = sqliteStrNDup(pDbToken->z, pDbToken->n);
danielk19774adee202004-05-08 08:23:19 +0000580 sqlite3Dequote(zDb);
drh8141f612004-01-25 22:44:58 +0000581 }else{
582 zDb = 0;
583 }
584 if( pTableToken && pTableToken->z ){
585 zTab = sqliteStrNDup(pTableToken->z, pTableToken->n);
danielk19774adee202004-05-08 08:23:19 +0000586 sqlite3Dequote(zTab);
drh8141f612004-01-25 22:44:58 +0000587 }else{
588 assert( zDb==0 );
589 zTab = 0;
590 }
591 zCol = sqliteStrNDup(pColumnToken->z, pColumnToken->n);
danielk19774adee202004-05-08 08:23:19 +0000592 sqlite3Dequote(zCol);
danielk197724b03fd2004-05-10 10:34:34 +0000593 if( sqlite3_malloc_failed ){
drh8141f612004-01-25 22:44:58 +0000594 return 1; /* Leak memory (zDb and zTab) if malloc fails */
595 }
596 assert( zTab==0 || pEList==0 );
597
598 pExpr->iTable = -1;
599 for(i=0; i<pSrcList->nSrc; i++){
600 struct SrcList_item *pItem = &pSrcList->a[i];
601 Table *pTab = pItem->pTab;
602 Column *pCol;
603
604 if( pTab==0 ) continue;
605 assert( pTab->nCol>0 );
606 if( zTab ){
607 if( pItem->zAlias ){
608 char *zTabName = pItem->zAlias;
danielk19774adee202004-05-08 08:23:19 +0000609 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
drh8141f612004-01-25 22:44:58 +0000610 }else{
611 char *zTabName = pTab->zName;
danielk19774adee202004-05-08 08:23:19 +0000612 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
613 if( zDb!=0 && sqlite3StrICmp(db->aDb[pTab->iDb].zName, zDb)!=0 ){
drh8141f612004-01-25 22:44:58 +0000614 continue;
615 }
616 }
617 }
618 if( 0==(cntTab++) ){
619 pExpr->iTable = pItem->iCursor;
620 pExpr->iDb = pTab->iDb;
621 }
622 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
danielk19774adee202004-05-08 08:23:19 +0000623 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
drh8141f612004-01-25 22:44:58 +0000624 cnt++;
625 pExpr->iTable = pItem->iCursor;
626 pExpr->iDb = pTab->iDb;
627 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
628 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
danielk1977a37cdde2004-05-16 11:15:36 +0000629 pExpr->affinity = pTab->aCol[j].affinity;
danielk19770202b292004-06-09 09:55:16 +0000630 pExpr->pColl = pTab->aCol[j].pColl;
drh8141f612004-01-25 22:44:58 +0000631 break;
632 }
633 }
634 }
635
636 /* If we have not already resolved the name, then maybe
637 ** it is a new.* or old.* trigger argument reference
638 */
639 if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
640 TriggerStack *pTriggerStack = pParse->trigStack;
641 Table *pTab = 0;
danielk19774adee202004-05-08 08:23:19 +0000642 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){
drh8141f612004-01-25 22:44:58 +0000643 pExpr->iTable = pTriggerStack->newIdx;
644 assert( pTriggerStack->pTab );
645 pTab = pTriggerStack->pTab;
danielk19774adee202004-05-08 08:23:19 +0000646 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab) == 0 ){
drh8141f612004-01-25 22:44:58 +0000647 pExpr->iTable = pTriggerStack->oldIdx;
648 assert( pTriggerStack->pTab );
649 pTab = pTriggerStack->pTab;
650 }
651
652 if( pTab ){
653 int j;
654 Column *pCol = pTab->aCol;
655
656 pExpr->iDb = pTab->iDb;
657 cntTab++;
658 for(j=0; j < pTab->nCol; j++, pCol++) {
danielk19774adee202004-05-08 08:23:19 +0000659 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
drh8141f612004-01-25 22:44:58 +0000660 cnt++;
661 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
danielk1977a37cdde2004-05-16 11:15:36 +0000662 pExpr->affinity = pTab->aCol[j].affinity;
danielk19770202b292004-06-09 09:55:16 +0000663 pExpr->pColl = pTab->aCol[j].pColl;
drh8141f612004-01-25 22:44:58 +0000664 break;
665 }
666 }
667 }
668 }
669
670 /*
671 ** Perhaps the name is a reference to the ROWID
672 */
danielk19774adee202004-05-08 08:23:19 +0000673 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
drh8141f612004-01-25 22:44:58 +0000674 cnt = 1;
675 pExpr->iColumn = -1;
danielk1977a37cdde2004-05-16 11:15:36 +0000676 pExpr->affinity = SQLITE_AFF_INTEGER;
drh8141f612004-01-25 22:44:58 +0000677 }
678
679 /*
680 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
681 ** might refer to an result-set alias. This happens, for example, when
682 ** we are resolving names in the WHERE clause of the following command:
683 **
684 ** SELECT a+b AS x FROM table WHERE x<10;
685 **
686 ** In cases like this, replace pExpr with a copy of the expression that
687 ** forms the result set entry ("a+b" in the example) and return immediately.
688 ** Note that the expression in the result set should have already been
689 ** resolved by the time the WHERE clause is resolved.
690 */
691 if( cnt==0 && pEList!=0 ){
692 for(j=0; j<pEList->nExpr; j++){
693 char *zAs = pEList->a[j].zName;
danielk19774adee202004-05-08 08:23:19 +0000694 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
drh8141f612004-01-25 22:44:58 +0000695 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
696 pExpr->op = TK_AS;
697 pExpr->iColumn = j;
danielk19774adee202004-05-08 08:23:19 +0000698 pExpr->pLeft = sqlite3ExprDup(pEList->a[j].pExpr);
drh8141f612004-01-25 22:44:58 +0000699 sqliteFree(zCol);
700 assert( zTab==0 && zDb==0 );
701 return 0;
702 }
703 }
704 }
705
706 /*
707 ** If X and Y are NULL (in other words if only the column name Z is
708 ** supplied) and the value of Z is enclosed in double-quotes, then
709 ** Z is a string literal if it doesn't match any column names. In that
710 ** case, we need to return right away and not make any changes to
711 ** pExpr.
712 */
713 if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
714 sqliteFree(zCol);
715 return 0;
716 }
717
718 /*
719 ** cnt==0 means there was not match. cnt>1 means there were two or
720 ** more matches. Either way, we have an error.
721 */
722 if( cnt!=1 ){
723 char *z = 0;
724 char *zErr;
725 zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s";
726 if( zDb ){
danielk19774adee202004-05-08 08:23:19 +0000727 sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, 0);
drh8141f612004-01-25 22:44:58 +0000728 }else if( zTab ){
danielk19774adee202004-05-08 08:23:19 +0000729 sqlite3SetString(&z, zTab, ".", zCol, 0);
drh8141f612004-01-25 22:44:58 +0000730 }else{
731 z = sqliteStrDup(zCol);
732 }
danielk19774adee202004-05-08 08:23:19 +0000733 sqlite3ErrorMsg(pParse, zErr, z);
drh8141f612004-01-25 22:44:58 +0000734 sqliteFree(z);
735 }
736
737 /* Clean up and return
738 */
739 sqliteFree(zDb);
740 sqliteFree(zTab);
741 sqliteFree(zCol);
danielk19774adee202004-05-08 08:23:19 +0000742 sqlite3ExprDelete(pExpr->pLeft);
drh8141f612004-01-25 22:44:58 +0000743 pExpr->pLeft = 0;
danielk19774adee202004-05-08 08:23:19 +0000744 sqlite3ExprDelete(pExpr->pRight);
drh8141f612004-01-25 22:44:58 +0000745 pExpr->pRight = 0;
746 pExpr->op = TK_COLUMN;
danielk19774adee202004-05-08 08:23:19 +0000747 sqlite3AuthRead(pParse, pExpr, pSrcList);
drh8141f612004-01-25 22:44:58 +0000748 return cnt!=1;
749}
750
751/*
drhcce7d172000-05-31 15:34:51 +0000752** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +0000753** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +0000754** index to the table in the table list and a column offset. The
755** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
756** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +0000757** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +0000758** VDBE cursor number for a cursor that is pointing into the referenced
759** table. The Expr.iColumn value is changed to the index of the column
760** of the referenced table. The Expr.iColumn value for the special
761** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
762** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +0000763**
drhfef52082000-06-06 01:50:43 +0000764** We also check for instances of the IN operator. IN comes in two
765** forms:
766**
767** expr IN (exprlist)
768** and
769** expr IN (SELECT ...)
770**
771** The first form is handled by creating a set holding the list
772** of allowed values. The second form causes the SELECT to generate
773** a temporary table.
774**
775** This routine also looks for scalar SELECTs that are part of an expression.
drh19a775c2000-06-05 18:54:46 +0000776** If it finds any, it generates code to write the value of that select
777** into a memory cell.
drhcce7d172000-05-31 15:34:51 +0000778**
drh967e8b72000-06-21 13:59:10 +0000779** Unknown columns or tables provoke an error. The function returns
drhcce7d172000-05-31 15:34:51 +0000780** the number of errors seen and leaves an error message on pParse->zErrMsg.
781*/
danielk19774adee202004-05-08 08:23:19 +0000782int sqlite3ExprResolveIds(
drha2e00042002-01-22 03:13:42 +0000783 Parse *pParse, /* The parser context */
drh8141f612004-01-25 22:44:58 +0000784 SrcList *pSrcList, /* List of tables used to resolve column names */
drha2e00042002-01-22 03:13:42 +0000785 ExprList *pEList, /* List of expressions used to resolve "AS" */
786 Expr *pExpr /* The expression to be analyzed. */
787){
drh6a3ea0e2003-05-02 14:32:12 +0000788 int i;
789
drh8141f612004-01-25 22:44:58 +0000790 if( pExpr==0 || pSrcList==0 ) return 0;
791 for(i=0; i<pSrcList->nSrc; i++){
792 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab );
drh6a3ea0e2003-05-02 14:32:12 +0000793 }
drhcce7d172000-05-31 15:34:51 +0000794 switch( pExpr->op ){
drh23989372002-05-21 13:43:04 +0000795 /* Double-quoted strings (ex: "abc") are used as identifiers if
796 ** possible. Otherwise they remain as strings. Single-quoted
797 ** strings (ex: 'abc') are always string literals.
798 */
799 case TK_STRING: {
800 if( pExpr->token.z[0]=='\'' ) break;
801 /* Fall thru into the TK_ID case if this is a double-quoted string */
802 }
drh8141f612004-01-25 22:44:58 +0000803 /* A lone identifier is the name of a columnd.
drha2e00042002-01-22 03:13:42 +0000804 */
drhcce7d172000-05-31 15:34:51 +0000805 case TK_ID: {
drh8141f612004-01-25 22:44:58 +0000806 if( lookupName(pParse, 0, 0, &pExpr->token, pSrcList, pEList, pExpr) ){
drhcce7d172000-05-31 15:34:51 +0000807 return 1;
drhed6c8672003-01-12 18:02:16 +0000808 }
drhcce7d172000-05-31 15:34:51 +0000809 break;
810 }
811
drhd24cc422003-03-27 12:51:24 +0000812 /* A table name and column name: ID.ID
813 ** Or a database, table and column: ID.ID.ID
814 */
drhcce7d172000-05-31 15:34:51 +0000815 case TK_DOT: {
drh8141f612004-01-25 22:44:58 +0000816 Token *pColumn;
817 Token *pTable;
818 Token *pDb;
819 Expr *pRight;
drhcce7d172000-05-31 15:34:51 +0000820
drhcce7d172000-05-31 15:34:51 +0000821 pRight = pExpr->pRight;
drhd24cc422003-03-27 12:51:24 +0000822 if( pRight->op==TK_ID ){
drh8141f612004-01-25 22:44:58 +0000823 pDb = 0;
824 pTable = &pExpr->pLeft->token;
825 pColumn = &pRight->token;
drhd24cc422003-03-27 12:51:24 +0000826 }else{
drh8141f612004-01-25 22:44:58 +0000827 assert( pRight->op==TK_DOT );
828 pDb = &pExpr->pLeft->token;
829 pTable = &pRight->pLeft->token;
830 pColumn = &pRight->pRight->token;
drhd24cc422003-03-27 12:51:24 +0000831 }
drh8141f612004-01-25 22:44:58 +0000832 if( lookupName(pParse, pDb, pTable, pColumn, pSrcList, 0, pExpr) ){
drhdaffd0e2001-04-11 14:28:42 +0000833 return 1;
834 }
drhcce7d172000-05-31 15:34:51 +0000835 break;
836 }
837
drhfef52082000-06-06 01:50:43 +0000838 case TK_IN: {
danielk1977e014a832004-05-17 10:48:57 +0000839 char affinity;
danielk19774adee202004-05-08 08:23:19 +0000840 Vdbe *v = sqlite3GetVdbe(pParse);
drhd3d39e92004-05-20 22:16:29 +0000841 KeyInfo keyInfo;
danielk19770202b292004-06-09 09:55:16 +0000842 int addr; /* Address of OP_OpenTemp instruction */
drhd3d39e92004-05-20 22:16:29 +0000843
drhfef52082000-06-06 01:50:43 +0000844 if( v==0 ) return 1;
danielk19774adee202004-05-08 08:23:19 +0000845 if( sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pLeft) ){
drhcfab11b2000-06-06 03:31:22 +0000846 return 1;
847 }
danielk1977bf3b7212004-05-18 10:06:24 +0000848 affinity = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +0000849
850 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
851 ** expression it is handled the same way. A temporary table is
852 ** filled with single-field index keys representing the results
853 ** from the SELECT or the <exprlist>.
854 **
855 ** If the 'x' expression is a column value, or the SELECT...
856 ** statement returns a column value, then the affinity of that
857 ** column is used to build the index keys. If both 'x' and the
858 ** SELECT... statement are columns, then numeric affinity is used
859 ** if either column has NUMERIC or INTEGER affinity. If neither
860 ** 'x' nor the SELECT... statement are columns, then numeric affinity
861 ** is used.
862 */
863 pExpr->iTable = pParse->nTab++;
danielk19770202b292004-06-09 09:55:16 +0000864 addr = sqlite3VdbeAddOp(v, OP_OpenTemp, pExpr->iTable, 0);
drhd3d39e92004-05-20 22:16:29 +0000865 memset(&keyInfo, 0, sizeof(keyInfo));
866 keyInfo.nField = 1;
drhf3218fe2004-05-28 08:21:02 +0000867 sqlite3VdbeAddOp(v, OP_SetNumColumns, pExpr->iTable, 1);
danielk1977e014a832004-05-17 10:48:57 +0000868
drhfef52082000-06-06 01:50:43 +0000869 if( pExpr->pSelect ){
870 /* Case 1: expr IN (SELECT ...)
871 **
danielk1977e014a832004-05-17 10:48:57 +0000872 ** Generate code to write the results of the select into the temporary
873 ** table allocated and opened above.
drhfef52082000-06-06 01:50:43 +0000874 */
danielk1977e014a832004-05-17 10:48:57 +0000875 int iParm = pExpr->iTable + (((int)affinity)<<16);
876 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
danielk1977bf3b7212004-05-18 10:06:24 +0000877 sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0);
danielk19770202b292004-06-09 09:55:16 +0000878 if( pExpr->pSelect->pEList && pExpr->pSelect->pEList->nExpr>0 ){
danielk19777cedc8d2004-06-10 10:50:08 +0000879 keyInfo.aColl[0] = binaryCompareCollSeq(pParse, pExpr->pLeft,
danielk19770202b292004-06-09 09:55:16 +0000880 pExpr->pSelect->pEList->a[0].pExpr);
881 }
drhfef52082000-06-06 01:50:43 +0000882 }else if( pExpr->pList ){
883 /* Case 2: expr IN (exprlist)
884 **
danielk1977e014a832004-05-17 10:48:57 +0000885 ** For each expression, build an index key from the evaluation and
886 ** store it in the temporary table. If <expr> is a column, then use
887 ** that columns affinity when building index keys. If <expr> is not
888 ** a column, use numeric affinity.
drhfef52082000-06-06 01:50:43 +0000889 */
danielk1977e014a832004-05-17 10:48:57 +0000890 int i;
891 char const *affStr;
892 if( !affinity ){
893 affinity = SQLITE_AFF_NUMERIC;
894 }
895 affStr = sqlite3AffinityString(affinity);
danielk19770202b292004-06-09 09:55:16 +0000896 keyInfo.aColl[0] = pExpr->pLeft->pColl;
danielk1977e014a832004-05-17 10:48:57 +0000897
898 /* Loop through each expression in <exprlist>. */
drhfef52082000-06-06 01:50:43 +0000899 for(i=0; i<pExpr->pList->nExpr; i++){
900 Expr *pE2 = pExpr->pList->a[i].pExpr;
danielk1977e014a832004-05-17 10:48:57 +0000901
902 /* Check that the expression is constant and valid. */
danielk19774adee202004-05-08 08:23:19 +0000903 if( !sqlite3ExprIsConstant(pE2) ){
904 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +0000905 "right-hand side of IN operator must be constant");
drhfef52082000-06-06 01:50:43 +0000906 return 1;
907 }
danielk19774adee202004-05-08 08:23:19 +0000908 if( sqlite3ExprCheck(pParse, pE2, 0, 0) ){
drh4794b982000-06-06 13:54:14 +0000909 return 1;
910 }
danielk1977e014a832004-05-17 10:48:57 +0000911
912 /* Evaluate the expression and insert it into the temp table */
913 sqlite3ExprCode(pParse, pE2);
914 sqlite3VdbeOp3(v, OP_MakeKey, 1, 0, affStr, P3_STATIC);
danielk19770f69c1e2004-05-29 11:24:50 +0000915 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +0000916 sqlite3VdbeAddOp(v, OP_PutStrKey, pExpr->iTable, 0);
drhfef52082000-06-06 01:50:43 +0000917 }
918 }
danielk19770202b292004-06-09 09:55:16 +0000919 sqlite3VdbeChangeP3(v, addr, (void *)&keyInfo, P3_KEYINFO);
920
drhcfab11b2000-06-06 03:31:22 +0000921 break;
drhfef52082000-06-06 01:50:43 +0000922 }
923
drh19a775c2000-06-05 18:54:46 +0000924 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +0000925 /* This has to be a scalar SELECT. Generate code to put the
926 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +0000927 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +0000928 */
drh967e8b72000-06-21 13:59:10 +0000929 pExpr->iColumn = pParse->nMem++;
danielk1977bf3b7212004-05-18 10:06:24 +0000930 if(sqlite3Select(pParse, pExpr->pSelect, SRT_Mem,pExpr->iColumn,0,0,0,0)){
drh19a775c2000-06-05 18:54:46 +0000931 return 1;
932 }
933 break;
934 }
935
drhcce7d172000-05-31 15:34:51 +0000936 /* For all else, just recursively walk the tree */
937 default: {
drh4794b982000-06-06 13:54:14 +0000938 if( pExpr->pLeft
danielk19774adee202004-05-08 08:23:19 +0000939 && sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pLeft) ){
drhcce7d172000-05-31 15:34:51 +0000940 return 1;
941 }
942 if( pExpr->pRight
danielk19774adee202004-05-08 08:23:19 +0000943 && sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pRight) ){
drhcce7d172000-05-31 15:34:51 +0000944 return 1;
945 }
946 if( pExpr->pList ){
947 int i;
948 ExprList *pList = pExpr->pList;
949 for(i=0; i<pList->nExpr; i++){
drh832508b2002-03-02 17:04:07 +0000950 Expr *pArg = pList->a[i].pExpr;
danielk19774adee202004-05-08 08:23:19 +0000951 if( sqlite3ExprResolveIds(pParse, pSrcList, pEList, pArg) ){
drhcce7d172000-05-31 15:34:51 +0000952 return 1;
953 }
954 }
955 }
956 }
957 }
958 return 0;
959}
960
drhcce7d172000-05-31 15:34:51 +0000961/*
drh4b59ab52002-08-24 18:24:51 +0000962** pExpr is a node that defines a function of some kind. It might
963** be a syntactic function like "count(x)" or it might be a function
964** that implements an operator, like "a LIKE b".
965**
966** This routine makes *pzName point to the name of the function and
967** *pnName hold the number of characters in the function name.
968*/
969static void getFunctionName(Expr *pExpr, const char **pzName, int *pnName){
970 switch( pExpr->op ){
971 case TK_FUNCTION: {
972 *pzName = pExpr->token.z;
drh6977fea2002-10-22 23:38:04 +0000973 *pnName = pExpr->token.n;
drh4b59ab52002-08-24 18:24:51 +0000974 break;
975 }
976 case TK_LIKE: {
977 *pzName = "like";
978 *pnName = 4;
979 break;
980 }
981 case TK_GLOB: {
982 *pzName = "glob";
983 *pnName = 4;
984 break;
985 }
986 default: {
987 *pzName = "can't happen";
988 *pnName = 12;
989 break;
990 }
991 }
992}
993
994/*
drhcce7d172000-05-31 15:34:51 +0000995** Error check the functions in an expression. Make sure all
996** function names are recognized and all functions have the correct
997** number of arguments. Leave an error message in pParse->zErrMsg
998** if anything is amiss. Return the number of errors.
999**
1000** if pIsAgg is not null and this expression is an aggregate function
1001** (like count(*) or max(value)) then write a 1 into *pIsAgg.
1002*/
danielk19774adee202004-05-08 08:23:19 +00001003int sqlite3ExprCheck(Parse *pParse, Expr *pExpr, int allowAgg, int *pIsAgg){
drhcce7d172000-05-31 15:34:51 +00001004 int nErr = 0;
1005 if( pExpr==0 ) return 0;
drhcce7d172000-05-31 15:34:51 +00001006 switch( pExpr->op ){
drh4b59ab52002-08-24 18:24:51 +00001007 case TK_GLOB:
1008 case TK_LIKE:
drhcce7d172000-05-31 15:34:51 +00001009 case TK_FUNCTION: {
drhc9b84a12002-06-20 11:36:48 +00001010 int n = pExpr->pList ? pExpr->pList->nExpr : 0; /* Number of arguments */
1011 int no_such_func = 0; /* True if no such function exists */
drhc9b84a12002-06-20 11:36:48 +00001012 int wrong_num_args = 0; /* True if wrong number of arguments */
1013 int is_agg = 0; /* True if is an aggregate function */
drhcce7d172000-05-31 15:34:51 +00001014 int i;
drh4b59ab52002-08-24 18:24:51 +00001015 int nId; /* Number of characters in function name */
1016 const char *zId; /* The function name. */
drh0bce8352002-02-28 00:41:10 +00001017 FuncDef *pDef;
danielk1977d8123362004-06-12 09:25:12 +00001018 int enc = pParse->db->enc;
drh0bce8352002-02-28 00:41:10 +00001019
drh4b59ab52002-08-24 18:24:51 +00001020 getFunctionName(pExpr, &zId, &nId);
danielk1977d8123362004-06-12 09:25:12 +00001021 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001022 if( pDef==0 ){
danielk1977d8123362004-06-12 09:25:12 +00001023 pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001024 if( pDef==0 ){
drh268380c2004-02-25 13:47:31 +00001025 no_such_func = 1;
drh0bce8352002-02-28 00:41:10 +00001026 }else{
1027 wrong_num_args = 1;
drhcce7d172000-05-31 15:34:51 +00001028 }
drh0bce8352002-02-28 00:41:10 +00001029 }else{
1030 is_agg = pDef->xFunc==0;
drhcce7d172000-05-31 15:34:51 +00001031 }
drh8e0a2f92002-02-23 23:45:45 +00001032 if( is_agg && !allowAgg ){
danielk19774adee202004-05-08 08:23:19 +00001033 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId, zId);
drh8e0a2f92002-02-23 23:45:45 +00001034 nErr++;
1035 is_agg = 0;
1036 }else if( no_such_func ){
danielk19774adee202004-05-08 08:23:19 +00001037 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
drhcce7d172000-05-31 15:34:51 +00001038 nErr++;
drh8e0a2f92002-02-23 23:45:45 +00001039 }else if( wrong_num_args ){
danielk19774adee202004-05-08 08:23:19 +00001040 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
drhf7a9e1a2004-02-22 18:40:56 +00001041 nId, zId);
drh8e0a2f92002-02-23 23:45:45 +00001042 nErr++;
drhcce7d172000-05-31 15:34:51 +00001043 }
drhf7a9e1a2004-02-22 18:40:56 +00001044 if( is_agg ){
1045 pExpr->op = TK_AGG_FUNCTION;
1046 if( pIsAgg ) *pIsAgg = 1;
1047 }
drhcce7d172000-05-31 15:34:51 +00001048 for(i=0; nErr==0 && i<n; i++){
danielk19774adee202004-05-08 08:23:19 +00001049 nErr = sqlite3ExprCheck(pParse, pExpr->pList->a[i].pExpr,
drh4cfa7932000-06-08 15:10:46 +00001050 allowAgg && !is_agg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +00001051 }
danielk19770202b292004-06-09 09:55:16 +00001052 /* FIX ME: Compute pExpr->affinity based on the expected return
1053 ** type of the function
1054 */
drhcce7d172000-05-31 15:34:51 +00001055 }
1056 default: {
1057 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001058 nErr = sqlite3ExprCheck(pParse, pExpr->pLeft, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +00001059 }
1060 if( nErr==0 && pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001061 nErr = sqlite3ExprCheck(pParse, pExpr->pRight, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +00001062 }
drhfef52082000-06-06 01:50:43 +00001063 if( nErr==0 && pExpr->pList ){
1064 int n = pExpr->pList->nExpr;
1065 int i;
1066 for(i=0; nErr==0 && i<n; i++){
drh22827922000-06-06 17:27:05 +00001067 Expr *pE2 = pExpr->pList->a[i].pExpr;
danielk19774adee202004-05-08 08:23:19 +00001068 nErr = sqlite3ExprCheck(pParse, pE2, allowAgg, pIsAgg);
drhfef52082000-06-06 01:50:43 +00001069 }
1070 }
drhcce7d172000-05-31 15:34:51 +00001071 break;
1072 }
1073 }
1074 return nErr;
1075}
1076
1077/*
drhd3d39e92004-05-20 22:16:29 +00001078** Return one of the SQLITE_AFF_* affinity types that indicates the likely
1079** data type of the result of the given expression.
1080**
1081** Not every expression has a fixed type. If the type cannot be determined
1082** at compile-time, then try to return the type affinity if the expression
1083** is a column. Otherwise just return SQLITE_AFF_NONE.
drhc9b84a12002-06-20 11:36:48 +00001084**
danielk19774adee202004-05-08 08:23:19 +00001085** The sqlite3ExprResolveIds() and sqlite3ExprCheck() routines must have
drhc9b84a12002-06-20 11:36:48 +00001086** both been called on the expression before it is passed to this routine.
1087*/
danielk19774adee202004-05-08 08:23:19 +00001088int sqlite3ExprType(Expr *p){
drhd3d39e92004-05-20 22:16:29 +00001089 if( p==0 ) return SQLITE_AFF_NONE;
drhc9b84a12002-06-20 11:36:48 +00001090 while( p ) switch( p->op ){
drhc9b84a12002-06-20 11:36:48 +00001091 case TK_CONCAT:
drh736c22b2004-05-21 02:14:24 +00001092 case TK_STRING:
danielk1977c572ef72004-05-27 09:28:41 +00001093 case TK_BLOB:
drhd3d39e92004-05-20 22:16:29 +00001094 return SQLITE_AFF_TEXT;
drhc9b84a12002-06-20 11:36:48 +00001095
1096 case TK_AS:
1097 p = p->pLeft;
1098 break;
1099
drh736c22b2004-05-21 02:14:24 +00001100 case TK_VARIABLE:
drhd3d39e92004-05-20 22:16:29 +00001101 case TK_NULL:
1102 return SQLITE_AFF_NONE;
drhc9b84a12002-06-20 11:36:48 +00001103
drhd3d39e92004-05-20 22:16:29 +00001104 case TK_SELECT: /*** FIX ME ****/
1105 case TK_COLUMN: /*** FIX ME ****/
1106 case TK_CASE: /*** FIX ME ****/
drhb1363202002-06-26 02:45:03 +00001107
drhc9b84a12002-06-20 11:36:48 +00001108 default:
drhd3d39e92004-05-20 22:16:29 +00001109 return SQLITE_AFF_NUMERIC;
drhc9b84a12002-06-20 11:36:48 +00001110 }
drhd3d39e92004-05-20 22:16:29 +00001111 return SQLITE_AFF_NONE;
drhc9b84a12002-06-20 11:36:48 +00001112}
1113
1114/*
drhfec19aa2004-05-19 20:41:03 +00001115** Generate an instruction that will put the integer describe by
1116** text z[0..n-1] on the stack.
1117*/
1118static void codeInteger(Vdbe *v, const char *z, int n){
1119 int i;
drh6fec0762004-05-30 01:38:43 +00001120 if( sqlite3GetInt32(z, &i) ){
1121 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
1122 }else if( sqlite3FitsIn64Bits(z) ){
1123 sqlite3VdbeOp3(v, OP_Integer, 0, 0, z, n);
drhfec19aa2004-05-19 20:41:03 +00001124 }else{
1125 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n);
1126 }
1127}
1128
1129/*
drhcce7d172000-05-31 15:34:51 +00001130** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +00001131** expression and leave the result on the top of stack.
drhcce7d172000-05-31 15:34:51 +00001132*/
danielk19774adee202004-05-08 08:23:19 +00001133void sqlite3ExprCode(Parse *pParse, Expr *pExpr){
drhcce7d172000-05-31 15:34:51 +00001134 Vdbe *v = pParse->pVdbe;
1135 int op;
drhdaffd0e2001-04-11 14:28:42 +00001136 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001137 switch( pExpr->op ){
1138 case TK_PLUS: op = OP_Add; break;
1139 case TK_MINUS: op = OP_Subtract; break;
1140 case TK_STAR: op = OP_Multiply; break;
1141 case TK_SLASH: op = OP_Divide; break;
1142 case TK_AND: op = OP_And; break;
1143 case TK_OR: op = OP_Or; break;
1144 case TK_LT: op = OP_Lt; break;
1145 case TK_LE: op = OP_Le; break;
1146 case TK_GT: op = OP_Gt; break;
1147 case TK_GE: op = OP_Ge; break;
1148 case TK_NE: op = OP_Ne; break;
1149 case TK_EQ: op = OP_Eq; break;
drhcce7d172000-05-31 15:34:51 +00001150 case TK_ISNULL: op = OP_IsNull; break;
1151 case TK_NOTNULL: op = OP_NotNull; break;
1152 case TK_NOT: op = OP_Not; break;
1153 case TK_UMINUS: op = OP_Negative; break;
drhbf4133c2001-10-13 02:59:08 +00001154 case TK_BITAND: op = OP_BitAnd; break;
1155 case TK_BITOR: op = OP_BitOr; break;
1156 case TK_BITNOT: op = OP_BitNot; break;
1157 case TK_LSHIFT: op = OP_ShiftLeft; break;
1158 case TK_RSHIFT: op = OP_ShiftRight; break;
1159 case TK_REM: op = OP_Remainder; break;
drhfec19aa2004-05-19 20:41:03 +00001160 case TK_FLOAT: op = OP_Real; break;
danielk19770f69c1e2004-05-29 11:24:50 +00001161 case TK_STRING: op = OP_String8; break;
danielk1977c572ef72004-05-27 09:28:41 +00001162 case TK_BLOB: op = OP_HexBlob; break;
drhcce7d172000-05-31 15:34:51 +00001163 default: break;
1164 }
1165 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00001166 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +00001167 if( pParse->useAgg ){
danielk19774adee202004-05-08 08:23:19 +00001168 sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drhc4a3c772001-04-04 11:48:57 +00001169 }else if( pExpr->iColumn>=0 ){
danielk19774adee202004-05-08 08:23:19 +00001170 sqlite3VdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn);
drhc4a3c772001-04-04 11:48:57 +00001171 }else{
danielk19774adee202004-05-08 08:23:19 +00001172 sqlite3VdbeAddOp(v, OP_Recno, pExpr->iTable, 0);
drh22827922000-06-06 17:27:05 +00001173 }
drhcce7d172000-05-31 15:34:51 +00001174 break;
1175 }
1176 case TK_INTEGER: {
drhfec19aa2004-05-19 20:41:03 +00001177 codeInteger(v, pExpr->token.z, pExpr->token.n);
1178 break;
1179 }
1180 case TK_FLOAT:
1181 case TK_STRING: {
1182 sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z, pExpr->token.n);
danielk19774adee202004-05-08 08:23:19 +00001183 sqlite3VdbeDequoteP3(v, -1);
drhcce7d172000-05-31 15:34:51 +00001184 break;
1185 }
danielk1977c572ef72004-05-27 09:28:41 +00001186 case TK_BLOB: {
1187 sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z+1, pExpr->token.n-1);
1188 sqlite3VdbeDequoteP3(v, -1);
1189 break;
1190 }
drhcce7d172000-05-31 15:34:51 +00001191 case TK_NULL: {
danielk19770f69c1e2004-05-29 11:24:50 +00001192 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001193 break;
1194 }
drh50457892003-09-06 01:10:47 +00001195 case TK_VARIABLE: {
danielk19774adee202004-05-08 08:23:19 +00001196 sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0);
drh50457892003-09-06 01:10:47 +00001197 break;
1198 }
drhc9b84a12002-06-20 11:36:48 +00001199 case TK_LT:
1200 case TK_LE:
1201 case TK_GT:
1202 case TK_GE:
1203 case TK_NE:
1204 case TK_EQ: {
danielk1977a37cdde2004-05-16 11:15:36 +00001205 int p1 = binaryCompareP1(pExpr->pLeft, pExpr->pRight, 0);
danielk19777cedc8d2004-06-10 10:50:08 +00001206 CollSeq *p3 = binaryCompareCollSeq(pParse, pExpr->pLeft, pExpr->pRight);
danielk1977a37cdde2004-05-16 11:15:36 +00001207 sqlite3ExprCode(pParse, pExpr->pLeft);
1208 sqlite3ExprCode(pParse, pExpr->pRight);
danielk19770202b292004-06-09 09:55:16 +00001209 sqlite3VdbeOp3(v, op, p1, 0, (void *)p3, P3_COLLSEQ);
danielk1977a37cdde2004-05-16 11:15:36 +00001210 break;
drhc9b84a12002-06-20 11:36:48 +00001211 }
drhcce7d172000-05-31 15:34:51 +00001212 case TK_AND:
1213 case TK_OR:
1214 case TK_PLUS:
1215 case TK_STAR:
1216 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00001217 case TK_REM:
1218 case TK_BITAND:
1219 case TK_BITOR:
drhc9b84a12002-06-20 11:36:48 +00001220 case TK_SLASH: {
danielk19774adee202004-05-08 08:23:19 +00001221 sqlite3ExprCode(pParse, pExpr->pLeft);
1222 sqlite3ExprCode(pParse, pExpr->pRight);
1223 sqlite3VdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001224 break;
1225 }
drhbf4133c2001-10-13 02:59:08 +00001226 case TK_LSHIFT:
1227 case TK_RSHIFT: {
danielk19774adee202004-05-08 08:23:19 +00001228 sqlite3ExprCode(pParse, pExpr->pRight);
1229 sqlite3ExprCode(pParse, pExpr->pLeft);
1230 sqlite3VdbeAddOp(v, op, 0, 0);
drhbf4133c2001-10-13 02:59:08 +00001231 break;
1232 }
drh00400772000-06-16 20:51:26 +00001233 case TK_CONCAT: {
danielk19774adee202004-05-08 08:23:19 +00001234 sqlite3ExprCode(pParse, pExpr->pLeft);
1235 sqlite3ExprCode(pParse, pExpr->pRight);
1236 sqlite3VdbeAddOp(v, OP_Concat, 2, 0);
drh00400772000-06-16 20:51:26 +00001237 break;
1238 }
drhcce7d172000-05-31 15:34:51 +00001239 case TK_UMINUS: {
drhfec19aa2004-05-19 20:41:03 +00001240 Expr *pLeft = pExpr->pLeft;
1241 assert( pLeft );
1242 if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
1243 Token *p = &pLeft->token;
drh6e142f52000-06-08 13:36:40 +00001244 char *z = sqliteMalloc( p->n + 2 );
1245 sprintf(z, "-%.*s", p->n, p->z);
drhfec19aa2004-05-19 20:41:03 +00001246 if( pLeft->op==TK_FLOAT ){
1247 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001248 }else{
drhfec19aa2004-05-19 20:41:03 +00001249 codeInteger(v, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001250 }
drh6e142f52000-06-08 13:36:40 +00001251 sqliteFree(z);
1252 break;
1253 }
drh1ccde152000-06-17 13:12:39 +00001254 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +00001255 }
drhbf4133c2001-10-13 02:59:08 +00001256 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00001257 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001258 sqlite3ExprCode(pParse, pExpr->pLeft);
1259 sqlite3VdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001260 break;
1261 }
1262 case TK_ISNULL:
1263 case TK_NOTNULL: {
1264 int dest;
danielk19774adee202004-05-08 08:23:19 +00001265 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1266 sqlite3ExprCode(pParse, pExpr->pLeft);
1267 dest = sqlite3VdbeCurrentAddr(v) + 2;
1268 sqlite3VdbeAddOp(v, op, 1, dest);
1269 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);
drhcce7d172000-05-31 15:34:51 +00001270 }
danielk1977a37cdde2004-05-16 11:15:36 +00001271 break;
drh22827922000-06-06 17:27:05 +00001272 case TK_AGG_FUNCTION: {
danielk19774adee202004-05-08 08:23:19 +00001273 sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drh22827922000-06-06 17:27:05 +00001274 break;
1275 }
drh4b59ab52002-08-24 18:24:51 +00001276 case TK_GLOB:
1277 case TK_LIKE:
drhcce7d172000-05-31 15:34:51 +00001278 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00001279 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00001280 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00001281 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00001282 int nId;
1283 const char *zId;
danielk1977682f68b2004-06-05 10:22:17 +00001284 int p2 = 0;
1285 int i;
danielk1977d8123362004-06-12 09:25:12 +00001286 u8 enc = pParse->db->enc;
danielk1977dc1bdc42004-06-11 10:51:27 +00001287 CollSeq *pColl = 0;
drh4b59ab52002-08-24 18:24:51 +00001288 getFunctionName(pExpr, &zId, &nId);
danielk1977d8123362004-06-12 09:25:12 +00001289 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001290 assert( pDef!=0 );
drhf9b596e2004-05-26 16:54:42 +00001291 nExpr = sqlite3ExprCodeExprList(pParse, pList);
danielk1977682f68b2004-06-05 10:22:17 +00001292 for(i=0; i<nExpr && i<32; i++){
danielk1977d02eb1f2004-06-06 09:44:03 +00001293 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
1294 p2 |= (1<<i);
1295 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001296 if( pDef->needCollSeq && !pColl ){
1297 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
1298 }
1299 }
1300 if( pDef->needCollSeq ){
1301 if( !pColl ) pColl = pParse->db->pDfltColl;
danielk1977d8123362004-06-12 09:25:12 +00001302 sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ);
danielk1977682f68b2004-06-05 10:22:17 +00001303 }
1304 sqlite3VdbeOp3(v, OP_Function, nExpr, p2, (char*)pDef, P3_FUNCDEF);
drhcce7d172000-05-31 15:34:51 +00001305 break;
1306 }
drh19a775c2000-06-05 18:54:46 +00001307 case TK_SELECT: {
danielk19774adee202004-05-08 08:23:19 +00001308 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drh19a775c2000-06-05 18:54:46 +00001309 break;
1310 }
drhfef52082000-06-06 01:50:43 +00001311 case TK_IN: {
1312 int addr;
danielk1977e014a832004-05-17 10:48:57 +00001313 char const *affStr;
1314
1315 /* Figure out the affinity to use to create a key from the results
1316 ** of the expression. affinityStr stores a static string suitable for
1317 ** P3 of OP_MakeKey.
1318 */
1319 affStr = sqlite3AffinityString(comparisonAffinity(pExpr));
1320
danielk19774adee202004-05-08 08:23:19 +00001321 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
danielk1977e014a832004-05-17 10:48:57 +00001322
1323 /* Code the <expr> from "<expr> IN (...)". The temporary table
1324 ** pExpr->iTable contains the values that make up the (...) set.
1325 */
danielk19774adee202004-05-08 08:23:19 +00001326 sqlite3ExprCode(pParse, pExpr->pLeft);
1327 addr = sqlite3VdbeCurrentAddr(v);
danielk1977e014a832004-05-17 10:48:57 +00001328 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); /* addr + 0 */
danielk19774adee202004-05-08 08:23:19 +00001329 sqlite3VdbeAddOp(v, OP_Pop, 2, 0);
danielk19770f69c1e2004-05-29 11:24:50 +00001330 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +00001331 sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7);
1332 sqlite3VdbeOp3(v, OP_MakeKey, 1, 0, affStr, P3_STATIC); /* addr + 4 */
1333 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7);
1334 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); /* addr + 6 */
1335
drhfef52082000-06-06 01:50:43 +00001336 break;
1337 }
1338 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00001339 int p1;
1340 CollSeq *p3;
danielk19774adee202004-05-08 08:23:19 +00001341 sqlite3ExprCode(pParse, pExpr->pLeft);
1342 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
1343 sqlite3ExprCode(pParse, pExpr->pList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001344 p1 = binaryCompareP1(pExpr->pLeft, pExpr->pList->a[0].pExpr, 0);
danielk19777cedc8d2004-06-10 10:50:08 +00001345 p3 = binaryCompareCollSeq(pParse, pExpr->pLeft, pExpr->pList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001346 sqlite3VdbeOp3(v, OP_Ge, p1, 0, (void *)p3, P3_COLLSEQ);
danielk19774adee202004-05-08 08:23:19 +00001347 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
1348 sqlite3ExprCode(pParse, pExpr->pList->a[1].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001349 p1 = binaryCompareP1(pExpr->pLeft, pExpr->pList->a[1].pExpr, 0);
danielk19777cedc8d2004-06-10 10:50:08 +00001350 p3 = binaryCompareCollSeq(pParse, pExpr->pLeft, pExpr->pList->a[1].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001351 sqlite3VdbeOp3(v, OP_Le, p1, 0, (void *)p3, P3_COLLSEQ);
danielk19774adee202004-05-08 08:23:19 +00001352 sqlite3VdbeAddOp(v, OP_And, 0, 0);
drhfef52082000-06-06 01:50:43 +00001353 break;
1354 }
drh51e9a442004-01-16 16:42:53 +00001355 case TK_UPLUS:
drha2e00042002-01-22 03:13:42 +00001356 case TK_AS: {
danielk19774adee202004-05-08 08:23:19 +00001357 sqlite3ExprCode(pParse, pExpr->pLeft);
drha2e00042002-01-22 03:13:42 +00001358 break;
1359 }
drh17a7f8d2002-03-24 13:13:27 +00001360 case TK_CASE: {
1361 int expr_end_label;
drhf5905aa2002-05-26 20:54:33 +00001362 int jumpInst;
1363 int addr;
1364 int nExpr;
drh17a7f8d2002-03-24 13:13:27 +00001365 int i;
1366
1367 assert(pExpr->pList);
1368 assert((pExpr->pList->nExpr % 2) == 0);
1369 assert(pExpr->pList->nExpr > 0);
drhf5905aa2002-05-26 20:54:33 +00001370 nExpr = pExpr->pList->nExpr;
danielk19774adee202004-05-08 08:23:19 +00001371 expr_end_label = sqlite3VdbeMakeLabel(v);
drh17a7f8d2002-03-24 13:13:27 +00001372 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001373 sqlite3ExprCode(pParse, pExpr->pLeft);
drh17a7f8d2002-03-24 13:13:27 +00001374 }
drhf5905aa2002-05-26 20:54:33 +00001375 for(i=0; i<nExpr; i=i+2){
danielk19774adee202004-05-08 08:23:19 +00001376 sqlite3ExprCode(pParse, pExpr->pList->a[i].pExpr);
drh17a7f8d2002-03-24 13:13:27 +00001377 if( pExpr->pLeft ){
danielk19770202b292004-06-09 09:55:16 +00001378 int p1 = binaryCompareP1(pExpr->pLeft, pExpr->pList->a[i].pExpr, 1);
danielk19777cedc8d2004-06-10 10:50:08 +00001379 CollSeq *p3 = binaryCompareCollSeq(pParse, pExpr->pLeft,
danielk19770202b292004-06-09 09:55:16 +00001380 pExpr->pList->a[i].pExpr);
danielk19774adee202004-05-08 08:23:19 +00001381 sqlite3VdbeAddOp(v, OP_Dup, 1, 1);
danielk19770202b292004-06-09 09:55:16 +00001382 jumpInst = sqlite3VdbeOp3(v, OP_Ne, p1, 0, (void *)p3, P3_COLLSEQ);
danielk19774adee202004-05-08 08:23:19 +00001383 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001384 }else{
danielk19774adee202004-05-08 08:23:19 +00001385 jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001386 }
danielk19774adee202004-05-08 08:23:19 +00001387 sqlite3ExprCode(pParse, pExpr->pList->a[i+1].pExpr);
1388 sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label);
1389 addr = sqlite3VdbeCurrentAddr(v);
1390 sqlite3VdbeChangeP2(v, jumpInst, addr);
drh17a7f8d2002-03-24 13:13:27 +00001391 }
drhf570f012002-05-31 15:51:25 +00001392 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001393 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhf570f012002-05-31 15:51:25 +00001394 }
drh17a7f8d2002-03-24 13:13:27 +00001395 if( pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001396 sqlite3ExprCode(pParse, pExpr->pRight);
drh17a7f8d2002-03-24 13:13:27 +00001397 }else{
danielk19770f69c1e2004-05-29 11:24:50 +00001398 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
drh17a7f8d2002-03-24 13:13:27 +00001399 }
danielk19774adee202004-05-08 08:23:19 +00001400 sqlite3VdbeResolveLabel(v, expr_end_label);
danielk19776f349032002-06-11 02:25:40 +00001401 break;
1402 }
1403 case TK_RAISE: {
1404 if( !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +00001405 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00001406 "RAISE() may only be used within a trigger-program");
danielk19776f349032002-06-11 02:25:40 +00001407 pParse->nErr++;
1408 return;
1409 }
1410 if( pExpr->iColumn == OE_Rollback ||
1411 pExpr->iColumn == OE_Abort ||
1412 pExpr->iColumn == OE_Fail ){
danielk19774adee202004-05-08 08:23:19 +00001413 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn,
drh701a0ae2004-02-22 20:05:00 +00001414 pExpr->token.z, pExpr->token.n);
danielk19774adee202004-05-08 08:23:19 +00001415 sqlite3VdbeDequoteP3(v, -1);
danielk19776f349032002-06-11 02:25:40 +00001416 } else {
1417 assert( pExpr->iColumn == OE_Ignore );
danielk19774adee202004-05-08 08:23:19 +00001418 sqlite3VdbeOp3(v, OP_Goto, 0, pParse->trigStack->ignoreJump,
drh701a0ae2004-02-22 20:05:00 +00001419 "(IGNORE jump)", 0);
danielk19776f349032002-06-11 02:25:40 +00001420 }
drh17a7f8d2002-03-24 13:13:27 +00001421 }
1422 break;
drhcce7d172000-05-31 15:34:51 +00001423 }
drhcce7d172000-05-31 15:34:51 +00001424}
1425
1426/*
drh268380c2004-02-25 13:47:31 +00001427** Generate code that pushes the value of every element of the given
drhf9b596e2004-05-26 16:54:42 +00001428** expression list onto the stack.
drh268380c2004-02-25 13:47:31 +00001429**
1430** Return the number of elements pushed onto the stack.
1431*/
danielk19774adee202004-05-08 08:23:19 +00001432int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00001433 Parse *pParse, /* Parsing context */
drhf9b596e2004-05-26 16:54:42 +00001434 ExprList *pList /* The expression list to be coded */
drh268380c2004-02-25 13:47:31 +00001435){
1436 struct ExprList_item *pItem;
1437 int i, n;
1438 Vdbe *v;
1439 if( pList==0 ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001440 v = sqlite3GetVdbe(pParse);
drh268380c2004-02-25 13:47:31 +00001441 n = pList->nExpr;
1442 for(pItem=pList->a, i=0; i<n; i++, pItem++){
danielk19774adee202004-05-08 08:23:19 +00001443 sqlite3ExprCode(pParse, pItem->pExpr);
drh268380c2004-02-25 13:47:31 +00001444 }
drhf9b596e2004-05-26 16:54:42 +00001445 return n;
drh268380c2004-02-25 13:47:31 +00001446}
1447
1448/*
drhcce7d172000-05-31 15:34:51 +00001449** Generate code for a boolean expression such that a jump is made
1450** to the label "dest" if the expression is true but execution
1451** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00001452**
1453** If the expression evaluates to NULL (neither true nor false), then
1454** take the jump if the jumpIfNull flag is true.
drhcce7d172000-05-31 15:34:51 +00001455*/
danielk19774adee202004-05-08 08:23:19 +00001456void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001457 Vdbe *v = pParse->pVdbe;
1458 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001459 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001460 switch( pExpr->op ){
1461 case TK_LT: op = OP_Lt; break;
1462 case TK_LE: op = OP_Le; break;
1463 case TK_GT: op = OP_Gt; break;
1464 case TK_GE: op = OP_Ge; break;
1465 case TK_NE: op = OP_Ne; break;
1466 case TK_EQ: op = OP_Eq; break;
drhcce7d172000-05-31 15:34:51 +00001467 case TK_ISNULL: op = OP_IsNull; break;
1468 case TK_NOTNULL: op = OP_NotNull; break;
1469 default: break;
1470 }
1471 switch( pExpr->op ){
1472 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001473 int d2 = sqlite3VdbeMakeLabel(v);
1474 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
1475 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
1476 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001477 break;
1478 }
1479 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001480 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
1481 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001482 break;
1483 }
1484 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001485 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001486 break;
1487 }
1488 case TK_LT:
1489 case TK_LE:
1490 case TK_GT:
1491 case TK_GE:
1492 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00001493 case TK_EQ: {
danielk1977a37cdde2004-05-16 11:15:36 +00001494 int p1 = binaryCompareP1(pExpr->pLeft, pExpr->pRight, jumpIfNull);
danielk19777cedc8d2004-06-10 10:50:08 +00001495 CollSeq *p3 = binaryCompareCollSeq(pParse, pExpr->pLeft, pExpr->pRight);
danielk19774adee202004-05-08 08:23:19 +00001496 sqlite3ExprCode(pParse, pExpr->pLeft);
1497 sqlite3ExprCode(pParse, pExpr->pRight);
danielk19770202b292004-06-09 09:55:16 +00001498 sqlite3VdbeOp3(v, op, p1, dest, (void *)p3, P3_COLLSEQ);
drhcce7d172000-05-31 15:34:51 +00001499 break;
1500 }
1501 case TK_ISNULL:
1502 case TK_NOTNULL: {
danielk19774adee202004-05-08 08:23:19 +00001503 sqlite3ExprCode(pParse, pExpr->pLeft);
1504 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001505 break;
1506 }
drhfef52082000-06-06 01:50:43 +00001507 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00001508 /* The expression "x BETWEEN y AND z" is implemented as:
1509 **
1510 ** 1 IF (x < y) GOTO 3
1511 ** 2 IF (x <= z) GOTO <dest>
1512 ** 3 ...
1513 */
drhf5905aa2002-05-26 20:54:33 +00001514 int addr;
danielk19770202b292004-06-09 09:55:16 +00001515 int p1;
1516 CollSeq *p3;
danielk19774adee202004-05-08 08:23:19 +00001517 sqlite3ExprCode(pParse, pExpr->pLeft);
1518 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
1519 sqlite3ExprCode(pParse, pExpr->pList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001520 p1 = binaryCompareP1(pExpr->pLeft, pExpr->pList->a[0].pExpr, !jumpIfNull);
danielk19777cedc8d2004-06-10 10:50:08 +00001521 p3 = binaryCompareCollSeq(pParse, pExpr->pLeft, pExpr->pList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001522 addr = sqlite3VdbeOp3(v, OP_Lt, p1, 0, (void *)p3, P3_COLLSEQ);
1523
danielk19774adee202004-05-08 08:23:19 +00001524 sqlite3ExprCode(pParse, pExpr->pList->a[1].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001525 p1 = binaryCompareP1(pExpr->pLeft, pExpr->pList->a[1].pExpr, jumpIfNull);
danielk19777cedc8d2004-06-10 10:50:08 +00001526 p3 = binaryCompareCollSeq(pParse, pExpr->pLeft, pExpr->pList->a[1].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001527 sqlite3VdbeOp3(v, OP_Le, p1, dest, (void *)p3, P3_COLLSEQ);
1528
danielk19774adee202004-05-08 08:23:19 +00001529 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
1530 sqlite3VdbeChangeP2(v, addr, sqlite3VdbeCurrentAddr(v));
1531 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00001532 break;
1533 }
drhcce7d172000-05-31 15:34:51 +00001534 default: {
danielk19774adee202004-05-08 08:23:19 +00001535 sqlite3ExprCode(pParse, pExpr);
1536 sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001537 break;
1538 }
1539 }
1540}
1541
1542/*
drh66b89c82000-11-28 20:47:17 +00001543** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00001544** to the label "dest" if the expression is false but execution
1545** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00001546**
1547** If the expression evaluates to NULL (neither true nor false) then
1548** jump if jumpIfNull is true or fall through if jumpIfNull is false.
drhcce7d172000-05-31 15:34:51 +00001549*/
danielk19774adee202004-05-08 08:23:19 +00001550void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001551 Vdbe *v = pParse->pVdbe;
1552 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001553 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001554 switch( pExpr->op ){
1555 case TK_LT: op = OP_Ge; break;
1556 case TK_LE: op = OP_Gt; break;
1557 case TK_GT: op = OP_Le; break;
1558 case TK_GE: op = OP_Lt; break;
1559 case TK_NE: op = OP_Eq; break;
1560 case TK_EQ: op = OP_Ne; break;
drhcce7d172000-05-31 15:34:51 +00001561 case TK_ISNULL: op = OP_NotNull; break;
1562 case TK_NOTNULL: op = OP_IsNull; break;
1563 default: break;
1564 }
1565 switch( pExpr->op ){
1566 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001567 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
1568 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001569 break;
1570 }
1571 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001572 int d2 = sqlite3VdbeMakeLabel(v);
1573 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
1574 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
1575 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001576 break;
1577 }
1578 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001579 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001580 break;
1581 }
1582 case TK_LT:
1583 case TK_LE:
1584 case TK_GT:
1585 case TK_GE:
1586 case TK_NE:
1587 case TK_EQ: {
danielk1977a37cdde2004-05-16 11:15:36 +00001588 int p1 = binaryCompareP1(pExpr->pLeft, pExpr->pRight, jumpIfNull);
danielk19777cedc8d2004-06-10 10:50:08 +00001589 CollSeq *p3 = binaryCompareCollSeq(pParse, pExpr->pLeft, pExpr->pRight);
danielk19774adee202004-05-08 08:23:19 +00001590 sqlite3ExprCode(pParse, pExpr->pLeft);
1591 sqlite3ExprCode(pParse, pExpr->pRight);
danielk19770202b292004-06-09 09:55:16 +00001592 sqlite3VdbeOp3(v, op, p1, dest, (void *)p3, P3_COLLSEQ);
drhcce7d172000-05-31 15:34:51 +00001593 break;
1594 }
drhcce7d172000-05-31 15:34:51 +00001595 case TK_ISNULL:
1596 case TK_NOTNULL: {
danielk19774adee202004-05-08 08:23:19 +00001597 sqlite3ExprCode(pParse, pExpr->pLeft);
1598 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001599 break;
1600 }
drhfef52082000-06-06 01:50:43 +00001601 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00001602 /* The expression is "x BETWEEN y AND z". It is implemented as:
1603 **
1604 ** 1 IF (x >= y) GOTO 3
1605 ** 2 GOTO <dest>
1606 ** 3 IF (x > z) GOTO <dest>
1607 */
drhfef52082000-06-06 01:50:43 +00001608 int addr;
danielk19770202b292004-06-09 09:55:16 +00001609 int p1;
1610 CollSeq *p3;
danielk19774adee202004-05-08 08:23:19 +00001611 sqlite3ExprCode(pParse, pExpr->pLeft);
1612 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
1613 sqlite3ExprCode(pParse, pExpr->pList->a[0].pExpr);
1614 addr = sqlite3VdbeCurrentAddr(v);
danielk19770202b292004-06-09 09:55:16 +00001615 p1 = binaryCompareP1(pExpr->pLeft, pExpr->pList->a[0].pExpr, !jumpIfNull);
danielk19777cedc8d2004-06-10 10:50:08 +00001616 p3 = binaryCompareCollSeq(pParse, pExpr->pLeft, pExpr->pList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001617 sqlite3VdbeOp3(v, OP_Ge, p1, addr+3, (void *)p3, P3_COLLSEQ);
danielk19774adee202004-05-08 08:23:19 +00001618 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
1619 sqlite3VdbeAddOp(v, OP_Goto, 0, dest);
1620 sqlite3ExprCode(pParse, pExpr->pList->a[1].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001621 p1 = binaryCompareP1(pExpr->pLeft, pExpr->pList->a[1].pExpr, jumpIfNull);
danielk19777cedc8d2004-06-10 10:50:08 +00001622 p3 = binaryCompareCollSeq(pParse, pExpr->pLeft, pExpr->pList->a[1].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001623 sqlite3VdbeOp3(v, OP_Gt, p1, dest, (void *)p3, P3_COLLSEQ);
drhfef52082000-06-06 01:50:43 +00001624 break;
1625 }
drhcce7d172000-05-31 15:34:51 +00001626 default: {
danielk19774adee202004-05-08 08:23:19 +00001627 sqlite3ExprCode(pParse, pExpr);
1628 sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001629 break;
1630 }
1631 }
1632}
drh22827922000-06-06 17:27:05 +00001633
1634/*
1635** Do a deep comparison of two expression trees. Return TRUE (non-zero)
1636** if they are identical and return FALSE if they differ in any way.
1637*/
danielk19774adee202004-05-08 08:23:19 +00001638int sqlite3ExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00001639 int i;
1640 if( pA==0 ){
1641 return pB==0;
1642 }else if( pB==0 ){
1643 return 0;
1644 }
1645 if( pA->op!=pB->op ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001646 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
1647 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00001648 if( pA->pList ){
1649 if( pB->pList==0 ) return 0;
1650 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
1651 for(i=0; i<pA->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00001652 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00001653 return 0;
1654 }
1655 }
1656 }else if( pB->pList ){
1657 return 0;
1658 }
1659 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00001660 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drh22827922000-06-06 17:27:05 +00001661 if( pA->token.z ){
1662 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00001663 if( pB->token.n!=pA->token.n ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001664 if( sqlite3StrNICmp(pA->token.z, pB->token.z, pB->token.n)!=0 ) return 0;
drh22827922000-06-06 17:27:05 +00001665 }
1666 return 1;
1667}
1668
1669/*
1670** Add a new element to the pParse->aAgg[] array and return its index.
1671*/
1672static int appendAggInfo(Parse *pParse){
1673 if( (pParse->nAgg & 0x7)==0 ){
1674 int amt = pParse->nAgg + 8;
drh6d4abfb2001-10-22 02:58:08 +00001675 AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0]));
1676 if( aAgg==0 ){
drh22827922000-06-06 17:27:05 +00001677 return -1;
1678 }
drh6d4abfb2001-10-22 02:58:08 +00001679 pParse->aAgg = aAgg;
drh22827922000-06-06 17:27:05 +00001680 }
1681 memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0]));
1682 return pParse->nAgg++;
1683}
1684
1685/*
1686** Analyze the given expression looking for aggregate functions and
1687** for variables that need to be added to the pParse->aAgg[] array.
1688** Make additional entries to the pParse->aAgg[] array as necessary.
1689**
1690** This routine should only be called after the expression has been
danielk19774adee202004-05-08 08:23:19 +00001691** analyzed by sqlite3ExprResolveIds() and sqlite3ExprCheck().
drh22827922000-06-06 17:27:05 +00001692**
1693** If errors are seen, leave an error message in zErrMsg and return
1694** the number of errors.
1695*/
danielk19774adee202004-05-08 08:23:19 +00001696int sqlite3ExprAnalyzeAggregates(Parse *pParse, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00001697 int i;
1698 AggExpr *aAgg;
1699 int nErr = 0;
1700
1701 if( pExpr==0 ) return 0;
1702 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00001703 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +00001704 aAgg = pParse->aAgg;
1705 for(i=0; i<pParse->nAgg; i++){
1706 if( aAgg[i].isAgg ) continue;
1707 if( aAgg[i].pExpr->iTable==pExpr->iTable
drh967e8b72000-06-21 13:59:10 +00001708 && aAgg[i].pExpr->iColumn==pExpr->iColumn ){
drh22827922000-06-06 17:27:05 +00001709 break;
1710 }
1711 }
1712 if( i>=pParse->nAgg ){
1713 i = appendAggInfo(pParse);
1714 if( i<0 ) return 1;
1715 pParse->aAgg[i].isAgg = 0;
1716 pParse->aAgg[i].pExpr = pExpr;
1717 }
drhaaf88722000-06-08 11:25:00 +00001718 pExpr->iAgg = i;
drh22827922000-06-06 17:27:05 +00001719 break;
1720 }
1721 case TK_AGG_FUNCTION: {
drh22827922000-06-06 17:27:05 +00001722 aAgg = pParse->aAgg;
1723 for(i=0; i<pParse->nAgg; i++){
1724 if( !aAgg[i].isAgg ) continue;
danielk19774adee202004-05-08 08:23:19 +00001725 if( sqlite3ExprCompare(aAgg[i].pExpr, pExpr) ){
drh22827922000-06-06 17:27:05 +00001726 break;
1727 }
1728 }
1729 if( i>=pParse->nAgg ){
danielk1977d8123362004-06-12 09:25:12 +00001730 u8 enc = pParse->db->enc;
drh22827922000-06-06 17:27:05 +00001731 i = appendAggInfo(pParse);
1732 if( i<0 ) return 1;
1733 pParse->aAgg[i].isAgg = 1;
1734 pParse->aAgg[i].pExpr = pExpr;
danielk19774adee202004-05-08 08:23:19 +00001735 pParse->aAgg[i].pFunc = sqlite3FindFunction(pParse->db,
drh6977fea2002-10-22 23:38:04 +00001736 pExpr->token.z, pExpr->token.n,
danielk1977d8123362004-06-12 09:25:12 +00001737 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
drh22827922000-06-06 17:27:05 +00001738 }
1739 pExpr->iAgg = i;
1740 break;
1741 }
1742 default: {
1743 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001744 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pLeft);
drh22827922000-06-06 17:27:05 +00001745 }
1746 if( nErr==0 && pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001747 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pRight);
drh22827922000-06-06 17:27:05 +00001748 }
1749 if( nErr==0 && pExpr->pList ){
1750 int n = pExpr->pList->nExpr;
1751 int i;
1752 for(i=0; nErr==0 && i<n; i++){
danielk19774adee202004-05-08 08:23:19 +00001753 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pList->a[i].pExpr);
drh22827922000-06-06 17:27:05 +00001754 }
1755 }
1756 break;
1757 }
1758 }
1759 return nErr;
1760}
drh8e0a2f92002-02-23 23:45:45 +00001761
1762/*
danielk1977d02eb1f2004-06-06 09:44:03 +00001763** Locate a user function given a name, a number of arguments and a flag
1764** indicating whether the function prefers UTF-16 over UTF-8. Return a
1765** pointer to the FuncDef structure that defines that function, or return
1766** NULL if the function does not exist.
drh8e0a2f92002-02-23 23:45:45 +00001767**
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.
danielk1977d02eb1f2004-06-06 09:44:03 +00001777**
1778** If createFlag is false, then a function with the required name and
1779** number of arguments may be returned even if the eTextRep flag does not
1780** match that requested.
drh8e0a2f92002-02-23 23:45:45 +00001781*/
danielk19774adee202004-05-08 08:23:19 +00001782FuncDef *sqlite3FindFunction(
drh8e0a2f92002-02-23 23:45:45 +00001783 sqlite *db, /* An open database */
1784 const char *zName, /* Name of the function. Not null-terminated */
1785 int nName, /* Number of characters in the name */
1786 int nArg, /* Number of arguments. -1 means any number */
danielk1977d8123362004-06-12 09:25:12 +00001787 u8 enc, /* Preferred text encoding */
drh8e0a2f92002-02-23 23:45:45 +00001788 int createFlag /* Create new entry if true and does not otherwise exist */
1789){
danielk1977d02eb1f2004-06-06 09:44:03 +00001790 FuncDef *p; /* Iterator variable */
1791 FuncDef *pFirst; /* First function with this name */
1792 FuncDef *pBest = 0; /* Best match found so far */
danielk1977d8123362004-06-12 09:25:12 +00001793 int bestmatch = 0;
danielk1977d02eb1f2004-06-06 09:44:03 +00001794
danielk1977d8123362004-06-12 09:25:12 +00001795
1796 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
danielk1977d02eb1f2004-06-06 09:44:03 +00001797 if( nArg<-1 ) nArg = -1;
1798
1799 pFirst = (FuncDef*)sqlite3HashFind(&db->aFunc, zName, nName);
1800 for(p=pFirst; p; p=p->pNext){
danielk1977d8123362004-06-12 09:25:12 +00001801 /* During the search for the best function definition, bestmatch is set
1802 ** as follows to indicate the quality of the match with the definition
1803 ** pointed to by pBest:
1804 **
1805 ** 0: pBest is NULL. No match has been found.
1806 ** 1: A variable arguments function that prefers UTF-8 when a UTF-16
1807 ** encoding is requested, or vice versa.
1808 ** 2: A variable arguments function that uses UTF-16BE when UTF-16LE is
1809 ** requested, or vice versa.
1810 ** 3: A variable arguments function using the same text encoding.
1811 ** 4: A function with the exact number of arguments requested that
1812 ** prefers UTF-8 when a UTF-16 encoding is requested, or vice versa.
1813 ** 5: A function with the exact number of arguments requested that
1814 ** prefers UTF-16LE when UTF-16BE is requested, or vice versa.
1815 ** 6: An exact match.
1816 **
1817 ** A larger value of 'matchqual' indicates a more desirable match.
1818 */
1819 if( p->nArg==-1 || p->nArg==nArg || nArg==-1 ){
1820 int match = 1; /* Quality of this match */
1821 if( p->nArg==nArg || nArg==-1 ){
1822 match = 4;
danielk1977d02eb1f2004-06-06 09:44:03 +00001823 }
danielk1977d8123362004-06-12 09:25:12 +00001824 if( enc==p->iPrefEnc ){
1825 match += 2;
danielk1977d02eb1f2004-06-06 09:44:03 +00001826 }
danielk1977d8123362004-06-12 09:25:12 +00001827 else if( (enc==SQLITE_UTF16LE && p->iPrefEnc==SQLITE_UTF16BE) ||
1828 (enc==SQLITE_UTF16BE && p->iPrefEnc==SQLITE_UTF16LE) ){
1829 match += 1;
1830 }
1831
1832 if( match>bestmatch ){
1833 pBest = p;
1834 bestmatch = match;
danielk1977d02eb1f2004-06-06 09:44:03 +00001835 }
1836 }
drh8e0a2f92002-02-23 23:45:45 +00001837 }
danielk1977d02eb1f2004-06-06 09:44:03 +00001838
danielk1977d8123362004-06-12 09:25:12 +00001839 /* If the createFlag parameter is true, and the seach did not reveal an
1840 ** exact match for the name, number of arguments and encoding, then add a
1841 ** new entry to the hash table and return it.
1842 */
1843 if( createFlag && bestmatch<6 &&
danielk1977d02eb1f2004-06-06 09:44:03 +00001844 (pBest = sqliteMalloc(sizeof(*pBest)+nName+1)) ){
1845 pBest->nArg = nArg;
1846 pBest->pNext = pFirst;
1847 pBest->zName = (char*)&pBest[1];
danielk1977d8123362004-06-12 09:25:12 +00001848 pBest->iPrefEnc = enc;
danielk1977d02eb1f2004-06-06 09:44:03 +00001849 memcpy(pBest->zName, zName, nName);
1850 pBest->zName[nName] = 0;
1851 sqlite3HashInsert(&db->aFunc, pBest->zName, nName, (void*)pBest);
drh8e0a2f92002-02-23 23:45:45 +00001852 }
danielk1977d02eb1f2004-06-06 09:44:03 +00001853
1854 if( pBest && (pBest->xStep || pBest->xFunc || createFlag) ){
1855 return pBest;
drh8e0a2f92002-02-23 23:45:45 +00001856 }
danielk1977d02eb1f2004-06-06 09:44:03 +00001857 return 0;
drh8e0a2f92002-02-23 23:45:45 +00001858}
danielk1977d02eb1f2004-06-06 09:44:03 +00001859