blob: 7b4768f9a4d84a488bbd07d1e1b3c6521ff287fb [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**
danielk1977dc1bdc42004-06-11 10:51:27 +000015** $Id: expr.c,v 1.139 2004/06/11 10:51:27 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;
danielk1977dc1bdc42004-06-11 10:51:27 +0000389 pNew->ppOpenTemp = 0;
drhff78bd22002-02-27 01:47:11 +0000390 return pNew;
391}
392
393
394/*
drha76b5df2002-02-23 02:32:10 +0000395** Add a new element to the end of an expression list. If pList is
396** initially NULL, then create a new expression list.
397*/
danielk19774adee202004-05-08 08:23:19 +0000398ExprList *sqlite3ExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){
drha76b5df2002-02-23 02:32:10 +0000399 if( pList==0 ){
400 pList = sqliteMalloc( sizeof(ExprList) );
401 if( pList==0 ){
danielk19774adee202004-05-08 08:23:19 +0000402 /* sqlite3ExprDelete(pExpr); // Leak memory if malloc fails */
drha76b5df2002-02-23 02:32:10 +0000403 return 0;
404 }
drh4efc4752004-01-16 15:55:37 +0000405 assert( pList->nAlloc==0 );
drha76b5df2002-02-23 02:32:10 +0000406 }
drh4305d102003-07-30 12:34:12 +0000407 if( pList->nAlloc<=pList->nExpr ){
drh4305d102003-07-30 12:34:12 +0000408 pList->nAlloc = pList->nAlloc*2 + 4;
drh4efc4752004-01-16 15:55:37 +0000409 pList->a = sqliteRealloc(pList->a, pList->nAlloc*sizeof(pList->a[0]));
410 if( pList->a==0 ){
danielk19774adee202004-05-08 08:23:19 +0000411 /* sqlite3ExprDelete(pExpr); // Leak memory if malloc fails */
drh4efc4752004-01-16 15:55:37 +0000412 pList->nExpr = pList->nAlloc = 0;
drha76b5df2002-02-23 02:32:10 +0000413 return pList;
414 }
drha76b5df2002-02-23 02:32:10 +0000415 }
drh4efc4752004-01-16 15:55:37 +0000416 assert( pList->a!=0 );
417 if( pExpr || pName ){
418 struct ExprList_item *pItem = &pList->a[pList->nExpr++];
419 memset(pItem, 0, sizeof(*pItem));
420 pItem->pExpr = pExpr;
drha76b5df2002-02-23 02:32:10 +0000421 if( pName ){
danielk19774adee202004-05-08 08:23:19 +0000422 sqlite3SetNString(&pItem->zName, pName->z, pName->n, 0);
423 sqlite3Dequote(pItem->zName);
drha76b5df2002-02-23 02:32:10 +0000424 }
425 }
426 return pList;
427}
428
429/*
430** Delete an entire expression list.
431*/
danielk19774adee202004-05-08 08:23:19 +0000432void sqlite3ExprListDelete(ExprList *pList){
drha76b5df2002-02-23 02:32:10 +0000433 int i;
434 if( pList==0 ) return;
drh1bdd9b52004-04-23 17:04:44 +0000435 assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
436 assert( pList->nExpr<=pList->nAlloc );
drha76b5df2002-02-23 02:32:10 +0000437 for(i=0; i<pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +0000438 sqlite3ExprDelete(pList->a[i].pExpr);
drha76b5df2002-02-23 02:32:10 +0000439 sqliteFree(pList->a[i].zName);
440 }
441 sqliteFree(pList->a);
442 sqliteFree(pList);
443}
444
445/*
drhfef52082000-06-06 01:50:43 +0000446** Walk an expression tree. Return 1 if the expression is constant
447** and 0 if it involves variables.
drh23989372002-05-21 13:43:04 +0000448**
449** For the purposes of this function, a double-quoted string (ex: "abc")
450** is considered a variable but a single-quoted string (ex: 'abc') is
451** a constant.
drhfef52082000-06-06 01:50:43 +0000452*/
danielk19774adee202004-05-08 08:23:19 +0000453int sqlite3ExprIsConstant(Expr *p){
drhfef52082000-06-06 01:50:43 +0000454 switch( p->op ){
455 case TK_ID:
drh967e8b72000-06-21 13:59:10 +0000456 case TK_COLUMN:
drhfef52082000-06-06 01:50:43 +0000457 case TK_DOT:
drh7bdc0c12003-04-19 17:27:24 +0000458 case TK_FUNCTION:
drhfef52082000-06-06 01:50:43 +0000459 return 0;
drh7bdc0c12003-04-19 17:27:24 +0000460 case TK_NULL:
drh23989372002-05-21 13:43:04 +0000461 case TK_STRING:
danielk1977c572ef72004-05-27 09:28:41 +0000462 case TK_BLOB:
drh92086432002-01-22 14:11:29 +0000463 case TK_INTEGER:
464 case TK_FLOAT:
drh50457892003-09-06 01:10:47 +0000465 case TK_VARIABLE:
drh92086432002-01-22 14:11:29 +0000466 return 1;
drhfef52082000-06-06 01:50:43 +0000467 default: {
danielk19774adee202004-05-08 08:23:19 +0000468 if( p->pLeft && !sqlite3ExprIsConstant(p->pLeft) ) return 0;
469 if( p->pRight && !sqlite3ExprIsConstant(p->pRight) ) return 0;
drhfef52082000-06-06 01:50:43 +0000470 if( p->pList ){
471 int i;
472 for(i=0; i<p->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +0000473 if( !sqlite3ExprIsConstant(p->pList->a[i].pExpr) ) return 0;
drhfef52082000-06-06 01:50:43 +0000474 }
475 }
drh92086432002-01-22 14:11:29 +0000476 return p->pLeft!=0 || p->pRight!=0 || (p->pList && p->pList->nExpr>0);
drhfef52082000-06-06 01:50:43 +0000477 }
478 }
drh92086432002-01-22 14:11:29 +0000479 return 0;
drhfef52082000-06-06 01:50:43 +0000480}
481
482/*
drh202b2df2004-01-06 01:13:46 +0000483** If the given expression codes a constant integer that is small enough
484** to fit in a 32-bit integer, return 1 and put the value of the integer
485** in *pValue. If the expression is not an integer or if it is too big
486** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
drhe4de1fe2002-06-02 16:09:01 +0000487*/
danielk19774adee202004-05-08 08:23:19 +0000488int sqlite3ExprIsInteger(Expr *p, int *pValue){
drhe4de1fe2002-06-02 16:09:01 +0000489 switch( p->op ){
490 case TK_INTEGER: {
drhfec19aa2004-05-19 20:41:03 +0000491 if( sqlite3GetInt32(p->token.z, pValue) ){
drh202b2df2004-01-06 01:13:46 +0000492 return 1;
493 }
494 break;
drhe4de1fe2002-06-02 16:09:01 +0000495 }
496 case TK_STRING: {
drhbd790ee2002-06-02 18:22:06 +0000497 const char *z = p->token.z;
drhe4de1fe2002-06-02 16:09:01 +0000498 int n = p->token.n;
drhbd790ee2002-06-02 18:22:06 +0000499 if( n>0 && z[0]=='-' ){ z++; n--; }
drhe4de1fe2002-06-02 16:09:01 +0000500 while( n>0 && *z && isdigit(*z) ){ z++; n--; }
drhfec19aa2004-05-19 20:41:03 +0000501 if( n==0 && sqlite3GetInt32(p->token.z, pValue) ){
drhe4de1fe2002-06-02 16:09:01 +0000502 return 1;
503 }
504 break;
505 }
drh4b59ab52002-08-24 18:24:51 +0000506 case TK_UPLUS: {
danielk19774adee202004-05-08 08:23:19 +0000507 return sqlite3ExprIsInteger(p->pLeft, pValue);
drh4b59ab52002-08-24 18:24:51 +0000508 }
drhe4de1fe2002-06-02 16:09:01 +0000509 case TK_UMINUS: {
510 int v;
danielk19774adee202004-05-08 08:23:19 +0000511 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
drhe4de1fe2002-06-02 16:09:01 +0000512 *pValue = -v;
513 return 1;
514 }
515 break;
516 }
517 default: break;
518 }
519 return 0;
520}
521
522/*
drhc4a3c772001-04-04 11:48:57 +0000523** Return TRUE if the given string is a row-id column name.
524*/
danielk19774adee202004-05-08 08:23:19 +0000525int sqlite3IsRowid(const char *z){
526 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
527 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
528 if( sqlite3StrICmp(z, "OID")==0 ) return 1;
drhc4a3c772001-04-04 11:48:57 +0000529 return 0;
530}
531
532/*
drh8141f612004-01-25 22:44:58 +0000533** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
534** that name in the set of source tables in pSrcList and make the pExpr
535** expression node refer back to that source column. The following changes
536** are made to pExpr:
537**
538** pExpr->iDb Set the index in db->aDb[] of the database holding
539** the table.
540** pExpr->iTable Set to the cursor number for the table obtained
541** from pSrcList.
542** pExpr->iColumn Set to the column number within the table.
drh8141f612004-01-25 22:44:58 +0000543** pExpr->op Set to TK_COLUMN.
544** pExpr->pLeft Any expression this points to is deleted
545** pExpr->pRight Any expression this points to is deleted.
546**
547** The pDbToken is the name of the database (the "X"). This value may be
548** NULL meaning that name is of the form Y.Z or Z. Any available database
549** can be used. The pTableToken is the name of the table (the "Y"). This
550** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it
551** means that the form of the name is Z and that columns from any table
552** can be used.
553**
554** If the name cannot be resolved unambiguously, leave an error message
555** in pParse and return non-zero. Return zero on success.
556*/
557static int lookupName(
558 Parse *pParse, /* The parsing context */
559 Token *pDbToken, /* Name of the database containing table, or NULL */
560 Token *pTableToken, /* Name of table containing column, or NULL */
561 Token *pColumnToken, /* Name of the column. */
562 SrcList *pSrcList, /* List of tables used to resolve column names */
563 ExprList *pEList, /* List of expressions used to resolve "AS" */
564 Expr *pExpr /* Make this EXPR node point to the selected column */
565){
566 char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */
567 char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */
568 char *zCol = 0; /* Name of the column. The "Z" */
569 int i, j; /* Loop counters */
570 int cnt = 0; /* Number of matching column names */
571 int cntTab = 0; /* Number of matching table names */
drh7e26d752004-02-11 10:35:29 +0000572 sqlite *db = pParse->db; /* The database */
drh8141f612004-01-25 22:44:58 +0000573
574 assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
575 if( pDbToken && pDbToken->z ){
576 zDb = sqliteStrNDup(pDbToken->z, pDbToken->n);
danielk19774adee202004-05-08 08:23:19 +0000577 sqlite3Dequote(zDb);
drh8141f612004-01-25 22:44:58 +0000578 }else{
579 zDb = 0;
580 }
581 if( pTableToken && pTableToken->z ){
582 zTab = sqliteStrNDup(pTableToken->z, pTableToken->n);
danielk19774adee202004-05-08 08:23:19 +0000583 sqlite3Dequote(zTab);
drh8141f612004-01-25 22:44:58 +0000584 }else{
585 assert( zDb==0 );
586 zTab = 0;
587 }
588 zCol = sqliteStrNDup(pColumnToken->z, pColumnToken->n);
danielk19774adee202004-05-08 08:23:19 +0000589 sqlite3Dequote(zCol);
danielk197724b03fd2004-05-10 10:34:34 +0000590 if( sqlite3_malloc_failed ){
drh8141f612004-01-25 22:44:58 +0000591 return 1; /* Leak memory (zDb and zTab) if malloc fails */
592 }
593 assert( zTab==0 || pEList==0 );
594
595 pExpr->iTable = -1;
596 for(i=0; i<pSrcList->nSrc; i++){
597 struct SrcList_item *pItem = &pSrcList->a[i];
598 Table *pTab = pItem->pTab;
599 Column *pCol;
600
601 if( pTab==0 ) continue;
602 assert( pTab->nCol>0 );
603 if( zTab ){
604 if( pItem->zAlias ){
605 char *zTabName = pItem->zAlias;
danielk19774adee202004-05-08 08:23:19 +0000606 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
drh8141f612004-01-25 22:44:58 +0000607 }else{
608 char *zTabName = pTab->zName;
danielk19774adee202004-05-08 08:23:19 +0000609 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
610 if( zDb!=0 && sqlite3StrICmp(db->aDb[pTab->iDb].zName, zDb)!=0 ){
drh8141f612004-01-25 22:44:58 +0000611 continue;
612 }
613 }
614 }
615 if( 0==(cntTab++) ){
616 pExpr->iTable = pItem->iCursor;
617 pExpr->iDb = pTab->iDb;
618 }
619 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
danielk19774adee202004-05-08 08:23:19 +0000620 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
drh8141f612004-01-25 22:44:58 +0000621 cnt++;
622 pExpr->iTable = pItem->iCursor;
623 pExpr->iDb = pTab->iDb;
624 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
625 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
danielk1977a37cdde2004-05-16 11:15:36 +0000626 pExpr->affinity = pTab->aCol[j].affinity;
danielk19770202b292004-06-09 09:55:16 +0000627 pExpr->pColl = pTab->aCol[j].pColl;
drh8141f612004-01-25 22:44:58 +0000628 break;
629 }
630 }
631 }
632
633 /* If we have not already resolved the name, then maybe
634 ** it is a new.* or old.* trigger argument reference
635 */
636 if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
637 TriggerStack *pTriggerStack = pParse->trigStack;
638 Table *pTab = 0;
danielk19774adee202004-05-08 08:23:19 +0000639 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){
drh8141f612004-01-25 22:44:58 +0000640 pExpr->iTable = pTriggerStack->newIdx;
641 assert( pTriggerStack->pTab );
642 pTab = pTriggerStack->pTab;
danielk19774adee202004-05-08 08:23:19 +0000643 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab) == 0 ){
drh8141f612004-01-25 22:44:58 +0000644 pExpr->iTable = pTriggerStack->oldIdx;
645 assert( pTriggerStack->pTab );
646 pTab = pTriggerStack->pTab;
647 }
648
649 if( pTab ){
650 int j;
651 Column *pCol = pTab->aCol;
652
653 pExpr->iDb = pTab->iDb;
654 cntTab++;
655 for(j=0; j < pTab->nCol; j++, pCol++) {
danielk19774adee202004-05-08 08:23:19 +0000656 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
drh8141f612004-01-25 22:44:58 +0000657 cnt++;
658 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
danielk1977a37cdde2004-05-16 11:15:36 +0000659 pExpr->affinity = pTab->aCol[j].affinity;
danielk19770202b292004-06-09 09:55:16 +0000660 pExpr->pColl = pTab->aCol[j].pColl;
drh8141f612004-01-25 22:44:58 +0000661 break;
662 }
663 }
664 }
665 }
666
667 /*
668 ** Perhaps the name is a reference to the ROWID
669 */
danielk19774adee202004-05-08 08:23:19 +0000670 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
drh8141f612004-01-25 22:44:58 +0000671 cnt = 1;
672 pExpr->iColumn = -1;
danielk1977a37cdde2004-05-16 11:15:36 +0000673 pExpr->affinity = SQLITE_AFF_INTEGER;
drh8141f612004-01-25 22:44:58 +0000674 }
675
676 /*
677 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
678 ** might refer to an result-set alias. This happens, for example, when
679 ** we are resolving names in the WHERE clause of the following command:
680 **
681 ** SELECT a+b AS x FROM table WHERE x<10;
682 **
683 ** In cases like this, replace pExpr with a copy of the expression that
684 ** forms the result set entry ("a+b" in the example) and return immediately.
685 ** Note that the expression in the result set should have already been
686 ** resolved by the time the WHERE clause is resolved.
687 */
688 if( cnt==0 && pEList!=0 ){
689 for(j=0; j<pEList->nExpr; j++){
690 char *zAs = pEList->a[j].zName;
danielk19774adee202004-05-08 08:23:19 +0000691 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
drh8141f612004-01-25 22:44:58 +0000692 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
693 pExpr->op = TK_AS;
694 pExpr->iColumn = j;
danielk19774adee202004-05-08 08:23:19 +0000695 pExpr->pLeft = sqlite3ExprDup(pEList->a[j].pExpr);
drh8141f612004-01-25 22:44:58 +0000696 sqliteFree(zCol);
697 assert( zTab==0 && zDb==0 );
698 return 0;
699 }
700 }
701 }
702
703 /*
704 ** If X and Y are NULL (in other words if only the column name Z is
705 ** supplied) and the value of Z is enclosed in double-quotes, then
706 ** Z is a string literal if it doesn't match any column names. In that
707 ** case, we need to return right away and not make any changes to
708 ** pExpr.
709 */
710 if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
711 sqliteFree(zCol);
712 return 0;
713 }
714
715 /*
716 ** cnt==0 means there was not match. cnt>1 means there were two or
717 ** more matches. Either way, we have an error.
718 */
719 if( cnt!=1 ){
720 char *z = 0;
721 char *zErr;
722 zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s";
723 if( zDb ){
danielk19774adee202004-05-08 08:23:19 +0000724 sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, 0);
drh8141f612004-01-25 22:44:58 +0000725 }else if( zTab ){
danielk19774adee202004-05-08 08:23:19 +0000726 sqlite3SetString(&z, zTab, ".", zCol, 0);
drh8141f612004-01-25 22:44:58 +0000727 }else{
728 z = sqliteStrDup(zCol);
729 }
danielk19774adee202004-05-08 08:23:19 +0000730 sqlite3ErrorMsg(pParse, zErr, z);
drh8141f612004-01-25 22:44:58 +0000731 sqliteFree(z);
732 }
733
734 /* Clean up and return
735 */
736 sqliteFree(zDb);
737 sqliteFree(zTab);
738 sqliteFree(zCol);
danielk19774adee202004-05-08 08:23:19 +0000739 sqlite3ExprDelete(pExpr->pLeft);
drh8141f612004-01-25 22:44:58 +0000740 pExpr->pLeft = 0;
danielk19774adee202004-05-08 08:23:19 +0000741 sqlite3ExprDelete(pExpr->pRight);
drh8141f612004-01-25 22:44:58 +0000742 pExpr->pRight = 0;
743 pExpr->op = TK_COLUMN;
danielk19774adee202004-05-08 08:23:19 +0000744 sqlite3AuthRead(pParse, pExpr, pSrcList);
drh8141f612004-01-25 22:44:58 +0000745 return cnt!=1;
746}
747
748/*
drhcce7d172000-05-31 15:34:51 +0000749** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +0000750** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +0000751** index to the table in the table list and a column offset. The
752** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
753** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +0000754** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +0000755** VDBE cursor number for a cursor that is pointing into the referenced
756** table. The Expr.iColumn value is changed to the index of the column
757** of the referenced table. The Expr.iColumn value for the special
758** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
759** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +0000760**
drhfef52082000-06-06 01:50:43 +0000761** We also check for instances of the IN operator. IN comes in two
762** forms:
763**
764** expr IN (exprlist)
765** and
766** expr IN (SELECT ...)
767**
768** The first form is handled by creating a set holding the list
769** of allowed values. The second form causes the SELECT to generate
770** a temporary table.
771**
772** This routine also looks for scalar SELECTs that are part of an expression.
drh19a775c2000-06-05 18:54:46 +0000773** If it finds any, it generates code to write the value of that select
774** into a memory cell.
drhcce7d172000-05-31 15:34:51 +0000775**
drh967e8b72000-06-21 13:59:10 +0000776** Unknown columns or tables provoke an error. The function returns
drhcce7d172000-05-31 15:34:51 +0000777** the number of errors seen and leaves an error message on pParse->zErrMsg.
778*/
danielk19774adee202004-05-08 08:23:19 +0000779int sqlite3ExprResolveIds(
drha2e00042002-01-22 03:13:42 +0000780 Parse *pParse, /* The parser context */
drh8141f612004-01-25 22:44:58 +0000781 SrcList *pSrcList, /* List of tables used to resolve column names */
drha2e00042002-01-22 03:13:42 +0000782 ExprList *pEList, /* List of expressions used to resolve "AS" */
783 Expr *pExpr /* The expression to be analyzed. */
784){
drh6a3ea0e2003-05-02 14:32:12 +0000785 int i;
786
drh8141f612004-01-25 22:44:58 +0000787 if( pExpr==0 || pSrcList==0 ) return 0;
788 for(i=0; i<pSrcList->nSrc; i++){
789 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab );
drh6a3ea0e2003-05-02 14:32:12 +0000790 }
drhcce7d172000-05-31 15:34:51 +0000791 switch( pExpr->op ){
drh23989372002-05-21 13:43:04 +0000792 /* Double-quoted strings (ex: "abc") are used as identifiers if
793 ** possible. Otherwise they remain as strings. Single-quoted
794 ** strings (ex: 'abc') are always string literals.
795 */
796 case TK_STRING: {
797 if( pExpr->token.z[0]=='\'' ) break;
798 /* Fall thru into the TK_ID case if this is a double-quoted string */
799 }
drh8141f612004-01-25 22:44:58 +0000800 /* A lone identifier is the name of a columnd.
drha2e00042002-01-22 03:13:42 +0000801 */
drhcce7d172000-05-31 15:34:51 +0000802 case TK_ID: {
drh8141f612004-01-25 22:44:58 +0000803 if( lookupName(pParse, 0, 0, &pExpr->token, pSrcList, pEList, pExpr) ){
drhcce7d172000-05-31 15:34:51 +0000804 return 1;
drhed6c8672003-01-12 18:02:16 +0000805 }
drhcce7d172000-05-31 15:34:51 +0000806 break;
807 }
808
drhd24cc422003-03-27 12:51:24 +0000809 /* A table name and column name: ID.ID
810 ** Or a database, table and column: ID.ID.ID
811 */
drhcce7d172000-05-31 15:34:51 +0000812 case TK_DOT: {
drh8141f612004-01-25 22:44:58 +0000813 Token *pColumn;
814 Token *pTable;
815 Token *pDb;
816 Expr *pRight;
drhcce7d172000-05-31 15:34:51 +0000817
drhcce7d172000-05-31 15:34:51 +0000818 pRight = pExpr->pRight;
drhd24cc422003-03-27 12:51:24 +0000819 if( pRight->op==TK_ID ){
drh8141f612004-01-25 22:44:58 +0000820 pDb = 0;
821 pTable = &pExpr->pLeft->token;
822 pColumn = &pRight->token;
drhd24cc422003-03-27 12:51:24 +0000823 }else{
drh8141f612004-01-25 22:44:58 +0000824 assert( pRight->op==TK_DOT );
825 pDb = &pExpr->pLeft->token;
826 pTable = &pRight->pLeft->token;
827 pColumn = &pRight->pRight->token;
drhd24cc422003-03-27 12:51:24 +0000828 }
drh8141f612004-01-25 22:44:58 +0000829 if( lookupName(pParse, pDb, pTable, pColumn, pSrcList, 0, pExpr) ){
drhdaffd0e2001-04-11 14:28:42 +0000830 return 1;
831 }
drhcce7d172000-05-31 15:34:51 +0000832 break;
833 }
834
drhfef52082000-06-06 01:50:43 +0000835 case TK_IN: {
danielk1977e014a832004-05-17 10:48:57 +0000836 char affinity;
danielk19774adee202004-05-08 08:23:19 +0000837 Vdbe *v = sqlite3GetVdbe(pParse);
drhd3d39e92004-05-20 22:16:29 +0000838 KeyInfo keyInfo;
danielk19770202b292004-06-09 09:55:16 +0000839 int addr; /* Address of OP_OpenTemp instruction */
drhd3d39e92004-05-20 22:16:29 +0000840
drhfef52082000-06-06 01:50:43 +0000841 if( v==0 ) return 1;
danielk19774adee202004-05-08 08:23:19 +0000842 if( sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pLeft) ){
drhcfab11b2000-06-06 03:31:22 +0000843 return 1;
844 }
danielk1977bf3b7212004-05-18 10:06:24 +0000845 affinity = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +0000846
847 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
848 ** expression it is handled the same way. A temporary table is
849 ** filled with single-field index keys representing the results
850 ** from the SELECT or the <exprlist>.
851 **
852 ** If the 'x' expression is a column value, or the SELECT...
853 ** statement returns a column value, then the affinity of that
854 ** column is used to build the index keys. If both 'x' and the
855 ** SELECT... statement are columns, then numeric affinity is used
856 ** if either column has NUMERIC or INTEGER affinity. If neither
857 ** 'x' nor the SELECT... statement are columns, then numeric affinity
858 ** is used.
859 */
860 pExpr->iTable = pParse->nTab++;
danielk19770202b292004-06-09 09:55:16 +0000861 addr = sqlite3VdbeAddOp(v, OP_OpenTemp, pExpr->iTable, 0);
drhd3d39e92004-05-20 22:16:29 +0000862 memset(&keyInfo, 0, sizeof(keyInfo));
863 keyInfo.nField = 1;
drhf3218fe2004-05-28 08:21:02 +0000864 sqlite3VdbeAddOp(v, OP_SetNumColumns, pExpr->iTable, 1);
danielk1977e014a832004-05-17 10:48:57 +0000865
drhfef52082000-06-06 01:50:43 +0000866 if( pExpr->pSelect ){
867 /* Case 1: expr IN (SELECT ...)
868 **
danielk1977e014a832004-05-17 10:48:57 +0000869 ** Generate code to write the results of the select into the temporary
870 ** table allocated and opened above.
drhfef52082000-06-06 01:50:43 +0000871 */
danielk1977e014a832004-05-17 10:48:57 +0000872 int iParm = pExpr->iTable + (((int)affinity)<<16);
873 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
danielk1977bf3b7212004-05-18 10:06:24 +0000874 sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0);
danielk19770202b292004-06-09 09:55:16 +0000875 if( pExpr->pSelect->pEList && pExpr->pSelect->pEList->nExpr>0 ){
danielk19777cedc8d2004-06-10 10:50:08 +0000876 keyInfo.aColl[0] = binaryCompareCollSeq(pParse, pExpr->pLeft,
danielk19770202b292004-06-09 09:55:16 +0000877 pExpr->pSelect->pEList->a[0].pExpr);
878 }
drhfef52082000-06-06 01:50:43 +0000879 }else if( pExpr->pList ){
880 /* Case 2: expr IN (exprlist)
881 **
danielk1977e014a832004-05-17 10:48:57 +0000882 ** For each expression, build an index key from the evaluation and
883 ** store it in the temporary table. If <expr> is a column, then use
884 ** that columns affinity when building index keys. If <expr> is not
885 ** a column, use numeric affinity.
drhfef52082000-06-06 01:50:43 +0000886 */
danielk1977e014a832004-05-17 10:48:57 +0000887 int i;
888 char const *affStr;
889 if( !affinity ){
890 affinity = SQLITE_AFF_NUMERIC;
891 }
892 affStr = sqlite3AffinityString(affinity);
danielk19770202b292004-06-09 09:55:16 +0000893 keyInfo.aColl[0] = pExpr->pLeft->pColl;
danielk1977e014a832004-05-17 10:48:57 +0000894
895 /* Loop through each expression in <exprlist>. */
drhfef52082000-06-06 01:50:43 +0000896 for(i=0; i<pExpr->pList->nExpr; i++){
897 Expr *pE2 = pExpr->pList->a[i].pExpr;
danielk1977e014a832004-05-17 10:48:57 +0000898
899 /* Check that the expression is constant and valid. */
danielk19774adee202004-05-08 08:23:19 +0000900 if( !sqlite3ExprIsConstant(pE2) ){
901 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +0000902 "right-hand side of IN operator must be constant");
drhfef52082000-06-06 01:50:43 +0000903 return 1;
904 }
danielk19774adee202004-05-08 08:23:19 +0000905 if( sqlite3ExprCheck(pParse, pE2, 0, 0) ){
drh4794b982000-06-06 13:54:14 +0000906 return 1;
907 }
danielk1977e014a832004-05-17 10:48:57 +0000908
909 /* Evaluate the expression and insert it into the temp table */
910 sqlite3ExprCode(pParse, pE2);
911 sqlite3VdbeOp3(v, OP_MakeKey, 1, 0, affStr, P3_STATIC);
danielk19770f69c1e2004-05-29 11:24:50 +0000912 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +0000913 sqlite3VdbeAddOp(v, OP_PutStrKey, pExpr->iTable, 0);
drhfef52082000-06-06 01:50:43 +0000914 }
915 }
danielk19770202b292004-06-09 09:55:16 +0000916 sqlite3VdbeChangeP3(v, addr, (void *)&keyInfo, P3_KEYINFO);
917
drhcfab11b2000-06-06 03:31:22 +0000918 break;
drhfef52082000-06-06 01:50:43 +0000919 }
920
drh19a775c2000-06-05 18:54:46 +0000921 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +0000922 /* This has to be a scalar SELECT. Generate code to put the
923 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +0000924 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +0000925 */
drh967e8b72000-06-21 13:59:10 +0000926 pExpr->iColumn = pParse->nMem++;
danielk1977bf3b7212004-05-18 10:06:24 +0000927 if(sqlite3Select(pParse, pExpr->pSelect, SRT_Mem,pExpr->iColumn,0,0,0,0)){
drh19a775c2000-06-05 18:54:46 +0000928 return 1;
929 }
930 break;
931 }
932
drhcce7d172000-05-31 15:34:51 +0000933 /* For all else, just recursively walk the tree */
934 default: {
drh4794b982000-06-06 13:54:14 +0000935 if( pExpr->pLeft
danielk19774adee202004-05-08 08:23:19 +0000936 && sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pLeft) ){
drhcce7d172000-05-31 15:34:51 +0000937 return 1;
938 }
939 if( pExpr->pRight
danielk19774adee202004-05-08 08:23:19 +0000940 && sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pRight) ){
drhcce7d172000-05-31 15:34:51 +0000941 return 1;
942 }
943 if( pExpr->pList ){
944 int i;
945 ExprList *pList = pExpr->pList;
946 for(i=0; i<pList->nExpr; i++){
drh832508b2002-03-02 17:04:07 +0000947 Expr *pArg = pList->a[i].pExpr;
danielk19774adee202004-05-08 08:23:19 +0000948 if( sqlite3ExprResolveIds(pParse, pSrcList, pEList, pArg) ){
drhcce7d172000-05-31 15:34:51 +0000949 return 1;
950 }
951 }
952 }
953 }
954 }
955 return 0;
956}
957
drhcce7d172000-05-31 15:34:51 +0000958/*
drh4b59ab52002-08-24 18:24:51 +0000959** pExpr is a node that defines a function of some kind. It might
960** be a syntactic function like "count(x)" or it might be a function
961** that implements an operator, like "a LIKE b".
962**
963** This routine makes *pzName point to the name of the function and
964** *pnName hold the number of characters in the function name.
965*/
966static void getFunctionName(Expr *pExpr, const char **pzName, int *pnName){
967 switch( pExpr->op ){
968 case TK_FUNCTION: {
969 *pzName = pExpr->token.z;
drh6977fea2002-10-22 23:38:04 +0000970 *pnName = pExpr->token.n;
drh4b59ab52002-08-24 18:24:51 +0000971 break;
972 }
973 case TK_LIKE: {
974 *pzName = "like";
975 *pnName = 4;
976 break;
977 }
978 case TK_GLOB: {
979 *pzName = "glob";
980 *pnName = 4;
981 break;
982 }
983 default: {
984 *pzName = "can't happen";
985 *pnName = 12;
986 break;
987 }
988 }
989}
990
991/*
drhcce7d172000-05-31 15:34:51 +0000992** Error check the functions in an expression. Make sure all
993** function names are recognized and all functions have the correct
994** number of arguments. Leave an error message in pParse->zErrMsg
995** if anything is amiss. Return the number of errors.
996**
997** if pIsAgg is not null and this expression is an aggregate function
998** (like count(*) or max(value)) then write a 1 into *pIsAgg.
999*/
danielk19774adee202004-05-08 08:23:19 +00001000int sqlite3ExprCheck(Parse *pParse, Expr *pExpr, int allowAgg, int *pIsAgg){
drhcce7d172000-05-31 15:34:51 +00001001 int nErr = 0;
1002 if( pExpr==0 ) return 0;
drhcce7d172000-05-31 15:34:51 +00001003 switch( pExpr->op ){
drh4b59ab52002-08-24 18:24:51 +00001004 case TK_GLOB:
1005 case TK_LIKE:
drhcce7d172000-05-31 15:34:51 +00001006 case TK_FUNCTION: {
drhc9b84a12002-06-20 11:36:48 +00001007 int n = pExpr->pList ? pExpr->pList->nExpr : 0; /* Number of arguments */
1008 int no_such_func = 0; /* True if no such function exists */
drhc9b84a12002-06-20 11:36:48 +00001009 int wrong_num_args = 0; /* True if wrong number of arguments */
1010 int is_agg = 0; /* True if is an aggregate function */
drhcce7d172000-05-31 15:34:51 +00001011 int i;
drh4b59ab52002-08-24 18:24:51 +00001012 int nId; /* Number of characters in function name */
1013 const char *zId; /* The function name. */
drh0bce8352002-02-28 00:41:10 +00001014 FuncDef *pDef;
danielk1977d02eb1f2004-06-06 09:44:03 +00001015 int iPrefEnc = (pParse->db->enc==TEXT_Utf8)?0:1;
drh0bce8352002-02-28 00:41:10 +00001016
drh4b59ab52002-08-24 18:24:51 +00001017 getFunctionName(pExpr, &zId, &nId);
danielk1977d02eb1f2004-06-06 09:44:03 +00001018 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, iPrefEnc, 0);
drh0bce8352002-02-28 00:41:10 +00001019 if( pDef==0 ){
danielk1977d02eb1f2004-06-06 09:44:03 +00001020 pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, iPrefEnc, 0);
drh0bce8352002-02-28 00:41:10 +00001021 if( pDef==0 ){
drh268380c2004-02-25 13:47:31 +00001022 no_such_func = 1;
drh0bce8352002-02-28 00:41:10 +00001023 }else{
1024 wrong_num_args = 1;
drhcce7d172000-05-31 15:34:51 +00001025 }
drh0bce8352002-02-28 00:41:10 +00001026 }else{
1027 is_agg = pDef->xFunc==0;
drhcce7d172000-05-31 15:34:51 +00001028 }
drh8e0a2f92002-02-23 23:45:45 +00001029 if( is_agg && !allowAgg ){
danielk19774adee202004-05-08 08:23:19 +00001030 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId, zId);
drh8e0a2f92002-02-23 23:45:45 +00001031 nErr++;
1032 is_agg = 0;
1033 }else if( no_such_func ){
danielk19774adee202004-05-08 08:23:19 +00001034 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
drhcce7d172000-05-31 15:34:51 +00001035 nErr++;
drh8e0a2f92002-02-23 23:45:45 +00001036 }else if( wrong_num_args ){
danielk19774adee202004-05-08 08:23:19 +00001037 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
drhf7a9e1a2004-02-22 18:40:56 +00001038 nId, zId);
drh8e0a2f92002-02-23 23:45:45 +00001039 nErr++;
drhcce7d172000-05-31 15:34:51 +00001040 }
drhf7a9e1a2004-02-22 18:40:56 +00001041 if( is_agg ){
1042 pExpr->op = TK_AGG_FUNCTION;
1043 if( pIsAgg ) *pIsAgg = 1;
1044 }
drhcce7d172000-05-31 15:34:51 +00001045 for(i=0; nErr==0 && i<n; i++){
danielk19774adee202004-05-08 08:23:19 +00001046 nErr = sqlite3ExprCheck(pParse, pExpr->pList->a[i].pExpr,
drh4cfa7932000-06-08 15:10:46 +00001047 allowAgg && !is_agg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +00001048 }
danielk19770202b292004-06-09 09:55:16 +00001049 /* FIX ME: Compute pExpr->affinity based on the expected return
1050 ** type of the function
1051 */
drhcce7d172000-05-31 15:34:51 +00001052 }
1053 default: {
1054 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001055 nErr = sqlite3ExprCheck(pParse, pExpr->pLeft, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +00001056 }
1057 if( nErr==0 && pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001058 nErr = sqlite3ExprCheck(pParse, pExpr->pRight, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +00001059 }
drhfef52082000-06-06 01:50:43 +00001060 if( nErr==0 && pExpr->pList ){
1061 int n = pExpr->pList->nExpr;
1062 int i;
1063 for(i=0; nErr==0 && i<n; i++){
drh22827922000-06-06 17:27:05 +00001064 Expr *pE2 = pExpr->pList->a[i].pExpr;
danielk19774adee202004-05-08 08:23:19 +00001065 nErr = sqlite3ExprCheck(pParse, pE2, allowAgg, pIsAgg);
drhfef52082000-06-06 01:50:43 +00001066 }
1067 }
drhcce7d172000-05-31 15:34:51 +00001068 break;
1069 }
1070 }
1071 return nErr;
1072}
1073
1074/*
drhd3d39e92004-05-20 22:16:29 +00001075** Return one of the SQLITE_AFF_* affinity types that indicates the likely
1076** data type of the result of the given expression.
1077**
1078** Not every expression has a fixed type. If the type cannot be determined
1079** at compile-time, then try to return the type affinity if the expression
1080** is a column. Otherwise just return SQLITE_AFF_NONE.
drhc9b84a12002-06-20 11:36:48 +00001081**
danielk19774adee202004-05-08 08:23:19 +00001082** The sqlite3ExprResolveIds() and sqlite3ExprCheck() routines must have
drhc9b84a12002-06-20 11:36:48 +00001083** both been called on the expression before it is passed to this routine.
1084*/
danielk19774adee202004-05-08 08:23:19 +00001085int sqlite3ExprType(Expr *p){
drhd3d39e92004-05-20 22:16:29 +00001086 if( p==0 ) return SQLITE_AFF_NONE;
drhc9b84a12002-06-20 11:36:48 +00001087 while( p ) switch( p->op ){
drhc9b84a12002-06-20 11:36:48 +00001088 case TK_CONCAT:
drh736c22b2004-05-21 02:14:24 +00001089 case TK_STRING:
danielk1977c572ef72004-05-27 09:28:41 +00001090 case TK_BLOB:
drhd3d39e92004-05-20 22:16:29 +00001091 return SQLITE_AFF_TEXT;
drhc9b84a12002-06-20 11:36:48 +00001092
1093 case TK_AS:
1094 p = p->pLeft;
1095 break;
1096
drh736c22b2004-05-21 02:14:24 +00001097 case TK_VARIABLE:
drhd3d39e92004-05-20 22:16:29 +00001098 case TK_NULL:
1099 return SQLITE_AFF_NONE;
drhc9b84a12002-06-20 11:36:48 +00001100
drhd3d39e92004-05-20 22:16:29 +00001101 case TK_SELECT: /*** FIX ME ****/
1102 case TK_COLUMN: /*** FIX ME ****/
1103 case TK_CASE: /*** FIX ME ****/
drhb1363202002-06-26 02:45:03 +00001104
drhc9b84a12002-06-20 11:36:48 +00001105 default:
drhd3d39e92004-05-20 22:16:29 +00001106 return SQLITE_AFF_NUMERIC;
drhc9b84a12002-06-20 11:36:48 +00001107 }
drhd3d39e92004-05-20 22:16:29 +00001108 return SQLITE_AFF_NONE;
drhc9b84a12002-06-20 11:36:48 +00001109}
1110
1111/*
drhfec19aa2004-05-19 20:41:03 +00001112** Generate an instruction that will put the integer describe by
1113** text z[0..n-1] on the stack.
1114*/
1115static void codeInteger(Vdbe *v, const char *z, int n){
1116 int i;
drh6fec0762004-05-30 01:38:43 +00001117 if( sqlite3GetInt32(z, &i) ){
1118 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
1119 }else if( sqlite3FitsIn64Bits(z) ){
1120 sqlite3VdbeOp3(v, OP_Integer, 0, 0, z, n);
drhfec19aa2004-05-19 20:41:03 +00001121 }else{
1122 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n);
1123 }
1124}
1125
1126/*
drhcce7d172000-05-31 15:34:51 +00001127** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +00001128** expression and leave the result on the top of stack.
drhcce7d172000-05-31 15:34:51 +00001129*/
danielk19774adee202004-05-08 08:23:19 +00001130void sqlite3ExprCode(Parse *pParse, Expr *pExpr){
drhcce7d172000-05-31 15:34:51 +00001131 Vdbe *v = pParse->pVdbe;
1132 int op;
drhdaffd0e2001-04-11 14:28:42 +00001133 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001134 switch( pExpr->op ){
1135 case TK_PLUS: op = OP_Add; break;
1136 case TK_MINUS: op = OP_Subtract; break;
1137 case TK_STAR: op = OP_Multiply; break;
1138 case TK_SLASH: op = OP_Divide; break;
1139 case TK_AND: op = OP_And; break;
1140 case TK_OR: op = OP_Or; break;
1141 case TK_LT: op = OP_Lt; break;
1142 case TK_LE: op = OP_Le; break;
1143 case TK_GT: op = OP_Gt; break;
1144 case TK_GE: op = OP_Ge; break;
1145 case TK_NE: op = OP_Ne; break;
1146 case TK_EQ: op = OP_Eq; break;
drhcce7d172000-05-31 15:34:51 +00001147 case TK_ISNULL: op = OP_IsNull; break;
1148 case TK_NOTNULL: op = OP_NotNull; break;
1149 case TK_NOT: op = OP_Not; break;
1150 case TK_UMINUS: op = OP_Negative; break;
drhbf4133c2001-10-13 02:59:08 +00001151 case TK_BITAND: op = OP_BitAnd; break;
1152 case TK_BITOR: op = OP_BitOr; break;
1153 case TK_BITNOT: op = OP_BitNot; break;
1154 case TK_LSHIFT: op = OP_ShiftLeft; break;
1155 case TK_RSHIFT: op = OP_ShiftRight; break;
1156 case TK_REM: op = OP_Remainder; break;
drhfec19aa2004-05-19 20:41:03 +00001157 case TK_FLOAT: op = OP_Real; break;
danielk19770f69c1e2004-05-29 11:24:50 +00001158 case TK_STRING: op = OP_String8; break;
danielk1977c572ef72004-05-27 09:28:41 +00001159 case TK_BLOB: op = OP_HexBlob; break;
drhcce7d172000-05-31 15:34:51 +00001160 default: break;
1161 }
1162 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00001163 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +00001164 if( pParse->useAgg ){
danielk19774adee202004-05-08 08:23:19 +00001165 sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drhc4a3c772001-04-04 11:48:57 +00001166 }else if( pExpr->iColumn>=0 ){
danielk19774adee202004-05-08 08:23:19 +00001167 sqlite3VdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn);
drhc4a3c772001-04-04 11:48:57 +00001168 }else{
danielk19774adee202004-05-08 08:23:19 +00001169 sqlite3VdbeAddOp(v, OP_Recno, pExpr->iTable, 0);
drh22827922000-06-06 17:27:05 +00001170 }
drhcce7d172000-05-31 15:34:51 +00001171 break;
1172 }
1173 case TK_INTEGER: {
drhfec19aa2004-05-19 20:41:03 +00001174 codeInteger(v, pExpr->token.z, pExpr->token.n);
1175 break;
1176 }
1177 case TK_FLOAT:
1178 case TK_STRING: {
1179 sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z, pExpr->token.n);
danielk19774adee202004-05-08 08:23:19 +00001180 sqlite3VdbeDequoteP3(v, -1);
drhcce7d172000-05-31 15:34:51 +00001181 break;
1182 }
danielk1977c572ef72004-05-27 09:28:41 +00001183 case TK_BLOB: {
1184 sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z+1, pExpr->token.n-1);
1185 sqlite3VdbeDequoteP3(v, -1);
1186 break;
1187 }
drhcce7d172000-05-31 15:34:51 +00001188 case TK_NULL: {
danielk19770f69c1e2004-05-29 11:24:50 +00001189 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001190 break;
1191 }
drh50457892003-09-06 01:10:47 +00001192 case TK_VARIABLE: {
danielk19774adee202004-05-08 08:23:19 +00001193 sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0);
drh50457892003-09-06 01:10:47 +00001194 break;
1195 }
drhc9b84a12002-06-20 11:36:48 +00001196 case TK_LT:
1197 case TK_LE:
1198 case TK_GT:
1199 case TK_GE:
1200 case TK_NE:
1201 case TK_EQ: {
danielk1977a37cdde2004-05-16 11:15:36 +00001202 int p1 = binaryCompareP1(pExpr->pLeft, pExpr->pRight, 0);
danielk19777cedc8d2004-06-10 10:50:08 +00001203 CollSeq *p3 = binaryCompareCollSeq(pParse, pExpr->pLeft, pExpr->pRight);
danielk1977a37cdde2004-05-16 11:15:36 +00001204 sqlite3ExprCode(pParse, pExpr->pLeft);
1205 sqlite3ExprCode(pParse, pExpr->pRight);
danielk19770202b292004-06-09 09:55:16 +00001206 sqlite3VdbeOp3(v, op, p1, 0, (void *)p3, P3_COLLSEQ);
danielk1977a37cdde2004-05-16 11:15:36 +00001207 break;
drhc9b84a12002-06-20 11:36:48 +00001208 }
drhcce7d172000-05-31 15:34:51 +00001209 case TK_AND:
1210 case TK_OR:
1211 case TK_PLUS:
1212 case TK_STAR:
1213 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00001214 case TK_REM:
1215 case TK_BITAND:
1216 case TK_BITOR:
drhc9b84a12002-06-20 11:36:48 +00001217 case TK_SLASH: {
danielk19774adee202004-05-08 08:23:19 +00001218 sqlite3ExprCode(pParse, pExpr->pLeft);
1219 sqlite3ExprCode(pParse, pExpr->pRight);
1220 sqlite3VdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001221 break;
1222 }
drhbf4133c2001-10-13 02:59:08 +00001223 case TK_LSHIFT:
1224 case TK_RSHIFT: {
danielk19774adee202004-05-08 08:23:19 +00001225 sqlite3ExprCode(pParse, pExpr->pRight);
1226 sqlite3ExprCode(pParse, pExpr->pLeft);
1227 sqlite3VdbeAddOp(v, op, 0, 0);
drhbf4133c2001-10-13 02:59:08 +00001228 break;
1229 }
drh00400772000-06-16 20:51:26 +00001230 case TK_CONCAT: {
danielk19774adee202004-05-08 08:23:19 +00001231 sqlite3ExprCode(pParse, pExpr->pLeft);
1232 sqlite3ExprCode(pParse, pExpr->pRight);
1233 sqlite3VdbeAddOp(v, OP_Concat, 2, 0);
drh00400772000-06-16 20:51:26 +00001234 break;
1235 }
drhcce7d172000-05-31 15:34:51 +00001236 case TK_UMINUS: {
drhfec19aa2004-05-19 20:41:03 +00001237 Expr *pLeft = pExpr->pLeft;
1238 assert( pLeft );
1239 if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
1240 Token *p = &pLeft->token;
drh6e142f52000-06-08 13:36:40 +00001241 char *z = sqliteMalloc( p->n + 2 );
1242 sprintf(z, "-%.*s", p->n, p->z);
drhfec19aa2004-05-19 20:41:03 +00001243 if( pLeft->op==TK_FLOAT ){
1244 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001245 }else{
drhfec19aa2004-05-19 20:41:03 +00001246 codeInteger(v, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001247 }
drh6e142f52000-06-08 13:36:40 +00001248 sqliteFree(z);
1249 break;
1250 }
drh1ccde152000-06-17 13:12:39 +00001251 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +00001252 }
drhbf4133c2001-10-13 02:59:08 +00001253 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00001254 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001255 sqlite3ExprCode(pParse, pExpr->pLeft);
1256 sqlite3VdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001257 break;
1258 }
1259 case TK_ISNULL:
1260 case TK_NOTNULL: {
1261 int dest;
danielk19774adee202004-05-08 08:23:19 +00001262 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1263 sqlite3ExprCode(pParse, pExpr->pLeft);
1264 dest = sqlite3VdbeCurrentAddr(v) + 2;
1265 sqlite3VdbeAddOp(v, op, 1, dest);
1266 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);
drhcce7d172000-05-31 15:34:51 +00001267 }
danielk1977a37cdde2004-05-16 11:15:36 +00001268 break;
drh22827922000-06-06 17:27:05 +00001269 case TK_AGG_FUNCTION: {
danielk19774adee202004-05-08 08:23:19 +00001270 sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drh22827922000-06-06 17:27:05 +00001271 break;
1272 }
drh4b59ab52002-08-24 18:24:51 +00001273 case TK_GLOB:
1274 case TK_LIKE:
drhcce7d172000-05-31 15:34:51 +00001275 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00001276 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00001277 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00001278 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00001279 int nId;
1280 const char *zId;
danielk1977682f68b2004-06-05 10:22:17 +00001281 int p2 = 0;
1282 int i;
danielk1977d02eb1f2004-06-06 09:44:03 +00001283 int iPrefEnc = (pParse->db->enc==TEXT_Utf8)?0:1;
danielk1977dc1bdc42004-06-11 10:51:27 +00001284 CollSeq *pColl = 0;
drh4b59ab52002-08-24 18:24:51 +00001285 getFunctionName(pExpr, &zId, &nId);
danielk1977d02eb1f2004-06-06 09:44:03 +00001286 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, iPrefEnc, 0);
drh0bce8352002-02-28 00:41:10 +00001287 assert( pDef!=0 );
drhf9b596e2004-05-26 16:54:42 +00001288 nExpr = sqlite3ExprCodeExprList(pParse, pList);
danielk1977682f68b2004-06-05 10:22:17 +00001289 for(i=0; i<nExpr && i<32; i++){
danielk1977d02eb1f2004-06-06 09:44:03 +00001290 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
1291 p2 |= (1<<i);
1292 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001293 if( pDef->needCollSeq && !pColl ){
1294 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
1295 }
1296 }
1297 if( pDef->needCollSeq ){
1298 if( !pColl ) pColl = pParse->db->pDfltColl;
1299 sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, pColl, P3_COLLSEQ);
danielk1977682f68b2004-06-05 10:22:17 +00001300 }
1301 sqlite3VdbeOp3(v, OP_Function, nExpr, p2, (char*)pDef, P3_FUNCDEF);
drhcce7d172000-05-31 15:34:51 +00001302 break;
1303 }
drh19a775c2000-06-05 18:54:46 +00001304 case TK_SELECT: {
danielk19774adee202004-05-08 08:23:19 +00001305 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drh19a775c2000-06-05 18:54:46 +00001306 break;
1307 }
drhfef52082000-06-06 01:50:43 +00001308 case TK_IN: {
1309 int addr;
danielk1977e014a832004-05-17 10:48:57 +00001310 char const *affStr;
1311
1312 /* Figure out the affinity to use to create a key from the results
1313 ** of the expression. affinityStr stores a static string suitable for
1314 ** P3 of OP_MakeKey.
1315 */
1316 affStr = sqlite3AffinityString(comparisonAffinity(pExpr));
1317
danielk19774adee202004-05-08 08:23:19 +00001318 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
danielk1977e014a832004-05-17 10:48:57 +00001319
1320 /* Code the <expr> from "<expr> IN (...)". The temporary table
1321 ** pExpr->iTable contains the values that make up the (...) set.
1322 */
danielk19774adee202004-05-08 08:23:19 +00001323 sqlite3ExprCode(pParse, pExpr->pLeft);
1324 addr = sqlite3VdbeCurrentAddr(v);
danielk1977e014a832004-05-17 10:48:57 +00001325 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); /* addr + 0 */
danielk19774adee202004-05-08 08:23:19 +00001326 sqlite3VdbeAddOp(v, OP_Pop, 2, 0);
danielk19770f69c1e2004-05-29 11:24:50 +00001327 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +00001328 sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7);
1329 sqlite3VdbeOp3(v, OP_MakeKey, 1, 0, affStr, P3_STATIC); /* addr + 4 */
1330 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7);
1331 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); /* addr + 6 */
1332
drhfef52082000-06-06 01:50:43 +00001333 break;
1334 }
1335 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00001336 int p1;
1337 CollSeq *p3;
danielk19774adee202004-05-08 08:23:19 +00001338 sqlite3ExprCode(pParse, pExpr->pLeft);
1339 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
1340 sqlite3ExprCode(pParse, pExpr->pList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001341 p1 = binaryCompareP1(pExpr->pLeft, pExpr->pList->a[0].pExpr, 0);
danielk19777cedc8d2004-06-10 10:50:08 +00001342 p3 = binaryCompareCollSeq(pParse, pExpr->pLeft, pExpr->pList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001343 sqlite3VdbeOp3(v, OP_Ge, p1, 0, (void *)p3, P3_COLLSEQ);
danielk19774adee202004-05-08 08:23:19 +00001344 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
1345 sqlite3ExprCode(pParse, pExpr->pList->a[1].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001346 p1 = binaryCompareP1(pExpr->pLeft, pExpr->pList->a[1].pExpr, 0);
danielk19777cedc8d2004-06-10 10:50:08 +00001347 p3 = binaryCompareCollSeq(pParse, pExpr->pLeft, pExpr->pList->a[1].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001348 sqlite3VdbeOp3(v, OP_Le, p1, 0, (void *)p3, P3_COLLSEQ);
danielk19774adee202004-05-08 08:23:19 +00001349 sqlite3VdbeAddOp(v, OP_And, 0, 0);
drhfef52082000-06-06 01:50:43 +00001350 break;
1351 }
drh51e9a442004-01-16 16:42:53 +00001352 case TK_UPLUS:
drha2e00042002-01-22 03:13:42 +00001353 case TK_AS: {
danielk19774adee202004-05-08 08:23:19 +00001354 sqlite3ExprCode(pParse, pExpr->pLeft);
drha2e00042002-01-22 03:13:42 +00001355 break;
1356 }
drh17a7f8d2002-03-24 13:13:27 +00001357 case TK_CASE: {
1358 int expr_end_label;
drhf5905aa2002-05-26 20:54:33 +00001359 int jumpInst;
1360 int addr;
1361 int nExpr;
drh17a7f8d2002-03-24 13:13:27 +00001362 int i;
1363
1364 assert(pExpr->pList);
1365 assert((pExpr->pList->nExpr % 2) == 0);
1366 assert(pExpr->pList->nExpr > 0);
drhf5905aa2002-05-26 20:54:33 +00001367 nExpr = pExpr->pList->nExpr;
danielk19774adee202004-05-08 08:23:19 +00001368 expr_end_label = sqlite3VdbeMakeLabel(v);
drh17a7f8d2002-03-24 13:13:27 +00001369 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001370 sqlite3ExprCode(pParse, pExpr->pLeft);
drh17a7f8d2002-03-24 13:13:27 +00001371 }
drhf5905aa2002-05-26 20:54:33 +00001372 for(i=0; i<nExpr; i=i+2){
danielk19774adee202004-05-08 08:23:19 +00001373 sqlite3ExprCode(pParse, pExpr->pList->a[i].pExpr);
drh17a7f8d2002-03-24 13:13:27 +00001374 if( pExpr->pLeft ){
danielk19770202b292004-06-09 09:55:16 +00001375 int p1 = binaryCompareP1(pExpr->pLeft, pExpr->pList->a[i].pExpr, 1);
danielk19777cedc8d2004-06-10 10:50:08 +00001376 CollSeq *p3 = binaryCompareCollSeq(pParse, pExpr->pLeft,
danielk19770202b292004-06-09 09:55:16 +00001377 pExpr->pList->a[i].pExpr);
danielk19774adee202004-05-08 08:23:19 +00001378 sqlite3VdbeAddOp(v, OP_Dup, 1, 1);
danielk19770202b292004-06-09 09:55:16 +00001379 jumpInst = sqlite3VdbeOp3(v, OP_Ne, p1, 0, (void *)p3, P3_COLLSEQ);
danielk19774adee202004-05-08 08:23:19 +00001380 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001381 }else{
danielk19774adee202004-05-08 08:23:19 +00001382 jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001383 }
danielk19774adee202004-05-08 08:23:19 +00001384 sqlite3ExprCode(pParse, pExpr->pList->a[i+1].pExpr);
1385 sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label);
1386 addr = sqlite3VdbeCurrentAddr(v);
1387 sqlite3VdbeChangeP2(v, jumpInst, addr);
drh17a7f8d2002-03-24 13:13:27 +00001388 }
drhf570f012002-05-31 15:51:25 +00001389 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001390 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhf570f012002-05-31 15:51:25 +00001391 }
drh17a7f8d2002-03-24 13:13:27 +00001392 if( pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001393 sqlite3ExprCode(pParse, pExpr->pRight);
drh17a7f8d2002-03-24 13:13:27 +00001394 }else{
danielk19770f69c1e2004-05-29 11:24:50 +00001395 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
drh17a7f8d2002-03-24 13:13:27 +00001396 }
danielk19774adee202004-05-08 08:23:19 +00001397 sqlite3VdbeResolveLabel(v, expr_end_label);
danielk19776f349032002-06-11 02:25:40 +00001398 break;
1399 }
1400 case TK_RAISE: {
1401 if( !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +00001402 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00001403 "RAISE() may only be used within a trigger-program");
danielk19776f349032002-06-11 02:25:40 +00001404 pParse->nErr++;
1405 return;
1406 }
1407 if( pExpr->iColumn == OE_Rollback ||
1408 pExpr->iColumn == OE_Abort ||
1409 pExpr->iColumn == OE_Fail ){
danielk19774adee202004-05-08 08:23:19 +00001410 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn,
drh701a0ae2004-02-22 20:05:00 +00001411 pExpr->token.z, pExpr->token.n);
danielk19774adee202004-05-08 08:23:19 +00001412 sqlite3VdbeDequoteP3(v, -1);
danielk19776f349032002-06-11 02:25:40 +00001413 } else {
1414 assert( pExpr->iColumn == OE_Ignore );
danielk19774adee202004-05-08 08:23:19 +00001415 sqlite3VdbeOp3(v, OP_Goto, 0, pParse->trigStack->ignoreJump,
drh701a0ae2004-02-22 20:05:00 +00001416 "(IGNORE jump)", 0);
danielk19776f349032002-06-11 02:25:40 +00001417 }
drh17a7f8d2002-03-24 13:13:27 +00001418 }
1419 break;
drhcce7d172000-05-31 15:34:51 +00001420 }
drhcce7d172000-05-31 15:34:51 +00001421}
1422
1423/*
drh268380c2004-02-25 13:47:31 +00001424** Generate code that pushes the value of every element of the given
drhf9b596e2004-05-26 16:54:42 +00001425** expression list onto the stack.
drh268380c2004-02-25 13:47:31 +00001426**
1427** Return the number of elements pushed onto the stack.
1428*/
danielk19774adee202004-05-08 08:23:19 +00001429int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00001430 Parse *pParse, /* Parsing context */
drhf9b596e2004-05-26 16:54:42 +00001431 ExprList *pList /* The expression list to be coded */
drh268380c2004-02-25 13:47:31 +00001432){
1433 struct ExprList_item *pItem;
1434 int i, n;
1435 Vdbe *v;
1436 if( pList==0 ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001437 v = sqlite3GetVdbe(pParse);
drh268380c2004-02-25 13:47:31 +00001438 n = pList->nExpr;
1439 for(pItem=pList->a, i=0; i<n; i++, pItem++){
danielk19774adee202004-05-08 08:23:19 +00001440 sqlite3ExprCode(pParse, pItem->pExpr);
drh268380c2004-02-25 13:47:31 +00001441 }
drhf9b596e2004-05-26 16:54:42 +00001442 return n;
drh268380c2004-02-25 13:47:31 +00001443}
1444
1445/*
drhcce7d172000-05-31 15:34:51 +00001446** Generate code for a boolean expression such that a jump is made
1447** to the label "dest" if the expression is true but execution
1448** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00001449**
1450** If the expression evaluates to NULL (neither true nor false), then
1451** take the jump if the jumpIfNull flag is true.
drhcce7d172000-05-31 15:34:51 +00001452*/
danielk19774adee202004-05-08 08:23:19 +00001453void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001454 Vdbe *v = pParse->pVdbe;
1455 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001456 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001457 switch( pExpr->op ){
1458 case TK_LT: op = OP_Lt; break;
1459 case TK_LE: op = OP_Le; break;
1460 case TK_GT: op = OP_Gt; break;
1461 case TK_GE: op = OP_Ge; break;
1462 case TK_NE: op = OP_Ne; break;
1463 case TK_EQ: op = OP_Eq; break;
drhcce7d172000-05-31 15:34:51 +00001464 case TK_ISNULL: op = OP_IsNull; break;
1465 case TK_NOTNULL: op = OP_NotNull; break;
1466 default: break;
1467 }
1468 switch( pExpr->op ){
1469 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001470 int d2 = sqlite3VdbeMakeLabel(v);
1471 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
1472 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
1473 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001474 break;
1475 }
1476 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001477 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
1478 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001479 break;
1480 }
1481 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001482 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001483 break;
1484 }
1485 case TK_LT:
1486 case TK_LE:
1487 case TK_GT:
1488 case TK_GE:
1489 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00001490 case TK_EQ: {
danielk1977a37cdde2004-05-16 11:15:36 +00001491 int p1 = binaryCompareP1(pExpr->pLeft, pExpr->pRight, jumpIfNull);
danielk19777cedc8d2004-06-10 10:50:08 +00001492 CollSeq *p3 = binaryCompareCollSeq(pParse, pExpr->pLeft, pExpr->pRight);
danielk19774adee202004-05-08 08:23:19 +00001493 sqlite3ExprCode(pParse, pExpr->pLeft);
1494 sqlite3ExprCode(pParse, pExpr->pRight);
danielk19770202b292004-06-09 09:55:16 +00001495 sqlite3VdbeOp3(v, op, p1, dest, (void *)p3, P3_COLLSEQ);
drhcce7d172000-05-31 15:34:51 +00001496 break;
1497 }
1498 case TK_ISNULL:
1499 case TK_NOTNULL: {
danielk19774adee202004-05-08 08:23:19 +00001500 sqlite3ExprCode(pParse, pExpr->pLeft);
1501 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001502 break;
1503 }
drhfef52082000-06-06 01:50:43 +00001504 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00001505 /* The expression "x BETWEEN y AND z" is implemented as:
1506 **
1507 ** 1 IF (x < y) GOTO 3
1508 ** 2 IF (x <= z) GOTO <dest>
1509 ** 3 ...
1510 */
drhf5905aa2002-05-26 20:54:33 +00001511 int addr;
danielk19770202b292004-06-09 09:55:16 +00001512 int p1;
1513 CollSeq *p3;
danielk19774adee202004-05-08 08:23:19 +00001514 sqlite3ExprCode(pParse, pExpr->pLeft);
1515 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
1516 sqlite3ExprCode(pParse, pExpr->pList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001517 p1 = binaryCompareP1(pExpr->pLeft, pExpr->pList->a[0].pExpr, !jumpIfNull);
danielk19777cedc8d2004-06-10 10:50:08 +00001518 p3 = binaryCompareCollSeq(pParse, pExpr->pLeft, pExpr->pList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001519 addr = sqlite3VdbeOp3(v, OP_Lt, p1, 0, (void *)p3, P3_COLLSEQ);
1520
danielk19774adee202004-05-08 08:23:19 +00001521 sqlite3ExprCode(pParse, pExpr->pList->a[1].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001522 p1 = binaryCompareP1(pExpr->pLeft, pExpr->pList->a[1].pExpr, jumpIfNull);
danielk19777cedc8d2004-06-10 10:50:08 +00001523 p3 = binaryCompareCollSeq(pParse, pExpr->pLeft, pExpr->pList->a[1].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001524 sqlite3VdbeOp3(v, OP_Le, p1, dest, (void *)p3, P3_COLLSEQ);
1525
danielk19774adee202004-05-08 08:23:19 +00001526 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
1527 sqlite3VdbeChangeP2(v, addr, sqlite3VdbeCurrentAddr(v));
1528 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00001529 break;
1530 }
drhcce7d172000-05-31 15:34:51 +00001531 default: {
danielk19774adee202004-05-08 08:23:19 +00001532 sqlite3ExprCode(pParse, pExpr);
1533 sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001534 break;
1535 }
1536 }
1537}
1538
1539/*
drh66b89c82000-11-28 20:47:17 +00001540** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00001541** to the label "dest" if the expression is false but execution
1542** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00001543**
1544** If the expression evaluates to NULL (neither true nor false) then
1545** jump if jumpIfNull is true or fall through if jumpIfNull is false.
drhcce7d172000-05-31 15:34:51 +00001546*/
danielk19774adee202004-05-08 08:23:19 +00001547void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001548 Vdbe *v = pParse->pVdbe;
1549 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001550 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001551 switch( pExpr->op ){
1552 case TK_LT: op = OP_Ge; break;
1553 case TK_LE: op = OP_Gt; break;
1554 case TK_GT: op = OP_Le; break;
1555 case TK_GE: op = OP_Lt; break;
1556 case TK_NE: op = OP_Eq; break;
1557 case TK_EQ: op = OP_Ne; break;
drhcce7d172000-05-31 15:34:51 +00001558 case TK_ISNULL: op = OP_NotNull; break;
1559 case TK_NOTNULL: op = OP_IsNull; break;
1560 default: break;
1561 }
1562 switch( pExpr->op ){
1563 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001564 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
1565 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001566 break;
1567 }
1568 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001569 int d2 = sqlite3VdbeMakeLabel(v);
1570 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
1571 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
1572 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001573 break;
1574 }
1575 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001576 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001577 break;
1578 }
1579 case TK_LT:
1580 case TK_LE:
1581 case TK_GT:
1582 case TK_GE:
1583 case TK_NE:
1584 case TK_EQ: {
danielk1977a37cdde2004-05-16 11:15:36 +00001585 int p1 = binaryCompareP1(pExpr->pLeft, pExpr->pRight, jumpIfNull);
danielk19777cedc8d2004-06-10 10:50:08 +00001586 CollSeq *p3 = binaryCompareCollSeq(pParse, pExpr->pLeft, pExpr->pRight);
danielk19774adee202004-05-08 08:23:19 +00001587 sqlite3ExprCode(pParse, pExpr->pLeft);
1588 sqlite3ExprCode(pParse, pExpr->pRight);
danielk19770202b292004-06-09 09:55:16 +00001589 sqlite3VdbeOp3(v, op, p1, dest, (void *)p3, P3_COLLSEQ);
drhcce7d172000-05-31 15:34:51 +00001590 break;
1591 }
drhcce7d172000-05-31 15:34:51 +00001592 case TK_ISNULL:
1593 case TK_NOTNULL: {
danielk19774adee202004-05-08 08:23:19 +00001594 sqlite3ExprCode(pParse, pExpr->pLeft);
1595 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001596 break;
1597 }
drhfef52082000-06-06 01:50:43 +00001598 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00001599 /* The expression is "x BETWEEN y AND z". It is implemented as:
1600 **
1601 ** 1 IF (x >= y) GOTO 3
1602 ** 2 GOTO <dest>
1603 ** 3 IF (x > z) GOTO <dest>
1604 */
drhfef52082000-06-06 01:50:43 +00001605 int addr;
danielk19770202b292004-06-09 09:55:16 +00001606 int p1;
1607 CollSeq *p3;
danielk19774adee202004-05-08 08:23:19 +00001608 sqlite3ExprCode(pParse, pExpr->pLeft);
1609 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
1610 sqlite3ExprCode(pParse, pExpr->pList->a[0].pExpr);
1611 addr = sqlite3VdbeCurrentAddr(v);
danielk19770202b292004-06-09 09:55:16 +00001612 p1 = binaryCompareP1(pExpr->pLeft, pExpr->pList->a[0].pExpr, !jumpIfNull);
danielk19777cedc8d2004-06-10 10:50:08 +00001613 p3 = binaryCompareCollSeq(pParse, pExpr->pLeft, pExpr->pList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001614 sqlite3VdbeOp3(v, OP_Ge, p1, addr+3, (void *)p3, P3_COLLSEQ);
danielk19774adee202004-05-08 08:23:19 +00001615 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
1616 sqlite3VdbeAddOp(v, OP_Goto, 0, dest);
1617 sqlite3ExprCode(pParse, pExpr->pList->a[1].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001618 p1 = binaryCompareP1(pExpr->pLeft, pExpr->pList->a[1].pExpr, jumpIfNull);
danielk19777cedc8d2004-06-10 10:50:08 +00001619 p3 = binaryCompareCollSeq(pParse, pExpr->pLeft, pExpr->pList->a[1].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001620 sqlite3VdbeOp3(v, OP_Gt, p1, dest, (void *)p3, P3_COLLSEQ);
drhfef52082000-06-06 01:50:43 +00001621 break;
1622 }
drhcce7d172000-05-31 15:34:51 +00001623 default: {
danielk19774adee202004-05-08 08:23:19 +00001624 sqlite3ExprCode(pParse, pExpr);
1625 sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001626 break;
1627 }
1628 }
1629}
drh22827922000-06-06 17:27:05 +00001630
1631/*
1632** Do a deep comparison of two expression trees. Return TRUE (non-zero)
1633** if they are identical and return FALSE if they differ in any way.
1634*/
danielk19774adee202004-05-08 08:23:19 +00001635int sqlite3ExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00001636 int i;
1637 if( pA==0 ){
1638 return pB==0;
1639 }else if( pB==0 ){
1640 return 0;
1641 }
1642 if( pA->op!=pB->op ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001643 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
1644 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00001645 if( pA->pList ){
1646 if( pB->pList==0 ) return 0;
1647 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
1648 for(i=0; i<pA->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00001649 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00001650 return 0;
1651 }
1652 }
1653 }else if( pB->pList ){
1654 return 0;
1655 }
1656 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00001657 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drh22827922000-06-06 17:27:05 +00001658 if( pA->token.z ){
1659 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00001660 if( pB->token.n!=pA->token.n ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001661 if( sqlite3StrNICmp(pA->token.z, pB->token.z, pB->token.n)!=0 ) return 0;
drh22827922000-06-06 17:27:05 +00001662 }
1663 return 1;
1664}
1665
1666/*
1667** Add a new element to the pParse->aAgg[] array and return its index.
1668*/
1669static int appendAggInfo(Parse *pParse){
1670 if( (pParse->nAgg & 0x7)==0 ){
1671 int amt = pParse->nAgg + 8;
drh6d4abfb2001-10-22 02:58:08 +00001672 AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0]));
1673 if( aAgg==0 ){
drh22827922000-06-06 17:27:05 +00001674 return -1;
1675 }
drh6d4abfb2001-10-22 02:58:08 +00001676 pParse->aAgg = aAgg;
drh22827922000-06-06 17:27:05 +00001677 }
1678 memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0]));
1679 return pParse->nAgg++;
1680}
1681
1682/*
1683** Analyze the given expression looking for aggregate functions and
1684** for variables that need to be added to the pParse->aAgg[] array.
1685** Make additional entries to the pParse->aAgg[] array as necessary.
1686**
1687** This routine should only be called after the expression has been
danielk19774adee202004-05-08 08:23:19 +00001688** analyzed by sqlite3ExprResolveIds() and sqlite3ExprCheck().
drh22827922000-06-06 17:27:05 +00001689**
1690** If errors are seen, leave an error message in zErrMsg and return
1691** the number of errors.
1692*/
danielk19774adee202004-05-08 08:23:19 +00001693int sqlite3ExprAnalyzeAggregates(Parse *pParse, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00001694 int i;
1695 AggExpr *aAgg;
1696 int nErr = 0;
1697
1698 if( pExpr==0 ) return 0;
1699 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00001700 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +00001701 aAgg = pParse->aAgg;
1702 for(i=0; i<pParse->nAgg; i++){
1703 if( aAgg[i].isAgg ) continue;
1704 if( aAgg[i].pExpr->iTable==pExpr->iTable
drh967e8b72000-06-21 13:59:10 +00001705 && aAgg[i].pExpr->iColumn==pExpr->iColumn ){
drh22827922000-06-06 17:27:05 +00001706 break;
1707 }
1708 }
1709 if( i>=pParse->nAgg ){
1710 i = appendAggInfo(pParse);
1711 if( i<0 ) return 1;
1712 pParse->aAgg[i].isAgg = 0;
1713 pParse->aAgg[i].pExpr = pExpr;
1714 }
drhaaf88722000-06-08 11:25:00 +00001715 pExpr->iAgg = i;
drh22827922000-06-06 17:27:05 +00001716 break;
1717 }
1718 case TK_AGG_FUNCTION: {
drh22827922000-06-06 17:27:05 +00001719 aAgg = pParse->aAgg;
1720 for(i=0; i<pParse->nAgg; i++){
1721 if( !aAgg[i].isAgg ) continue;
danielk19774adee202004-05-08 08:23:19 +00001722 if( sqlite3ExprCompare(aAgg[i].pExpr, pExpr) ){
drh22827922000-06-06 17:27:05 +00001723 break;
1724 }
1725 }
1726 if( i>=pParse->nAgg ){
danielk1977d02eb1f2004-06-06 09:44:03 +00001727 int iPrefEnc = (pParse->db->enc==TEXT_Utf8)?0:1;
drh22827922000-06-06 17:27:05 +00001728 i = appendAggInfo(pParse);
1729 if( i<0 ) return 1;
1730 pParse->aAgg[i].isAgg = 1;
1731 pParse->aAgg[i].pExpr = pExpr;
danielk19774adee202004-05-08 08:23:19 +00001732 pParse->aAgg[i].pFunc = sqlite3FindFunction(pParse->db,
drh6977fea2002-10-22 23:38:04 +00001733 pExpr->token.z, pExpr->token.n,
danielk1977d02eb1f2004-06-06 09:44:03 +00001734 pExpr->pList ? pExpr->pList->nExpr : 0, iPrefEnc, 0);
drh22827922000-06-06 17:27:05 +00001735 }
1736 pExpr->iAgg = i;
1737 break;
1738 }
1739 default: {
1740 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001741 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pLeft);
drh22827922000-06-06 17:27:05 +00001742 }
1743 if( nErr==0 && pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001744 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pRight);
drh22827922000-06-06 17:27:05 +00001745 }
1746 if( nErr==0 && pExpr->pList ){
1747 int n = pExpr->pList->nExpr;
1748 int i;
1749 for(i=0; nErr==0 && i<n; i++){
danielk19774adee202004-05-08 08:23:19 +00001750 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pList->a[i].pExpr);
drh22827922000-06-06 17:27:05 +00001751 }
1752 }
1753 break;
1754 }
1755 }
1756 return nErr;
1757}
drh8e0a2f92002-02-23 23:45:45 +00001758
1759/*
danielk1977d02eb1f2004-06-06 09:44:03 +00001760** Locate a user function given a name, a number of arguments and a flag
1761** indicating whether the function prefers UTF-16 over UTF-8. Return a
1762** pointer to the FuncDef structure that defines that function, or return
1763** NULL if the function does not exist.
drh8e0a2f92002-02-23 23:45:45 +00001764**
drh0bce8352002-02-28 00:41:10 +00001765** If the createFlag argument is true, then a new (blank) FuncDef
drh8e0a2f92002-02-23 23:45:45 +00001766** structure is created and liked into the "db" structure if a
1767** no matching function previously existed. When createFlag is true
1768** and the nArg parameter is -1, then only a function that accepts
1769** any number of arguments will be returned.
1770**
1771** If createFlag is false and nArg is -1, then the first valid
1772** function found is returned. A function is valid if either xFunc
1773** or xStep is non-zero.
danielk1977d02eb1f2004-06-06 09:44:03 +00001774**
1775** If createFlag is false, then a function with the required name and
1776** number of arguments may be returned even if the eTextRep flag does not
1777** match that requested.
drh8e0a2f92002-02-23 23:45:45 +00001778*/
danielk19774adee202004-05-08 08:23:19 +00001779FuncDef *sqlite3FindFunction(
drh8e0a2f92002-02-23 23:45:45 +00001780 sqlite *db, /* An open database */
1781 const char *zName, /* Name of the function. Not null-terminated */
1782 int nName, /* Number of characters in the name */
1783 int nArg, /* Number of arguments. -1 means any number */
danielk1977d02eb1f2004-06-06 09:44:03 +00001784 int eTextRep, /* True to retrieve UTF-16 versions. */
drh8e0a2f92002-02-23 23:45:45 +00001785 int createFlag /* Create new entry if true and does not otherwise exist */
1786){
danielk1977d02eb1f2004-06-06 09:44:03 +00001787 FuncDef *p; /* Iterator variable */
1788 FuncDef *pFirst; /* First function with this name */
1789 FuncDef *pBest = 0; /* Best match found so far */
1790 int matchqual = 0;
1791
1792 /* Normalize argument values to simplify comparisons below. */
1793 if( eTextRep ) eTextRep = 1;
1794 if( nArg<-1 ) nArg = -1;
1795
1796 pFirst = (FuncDef*)sqlite3HashFind(&db->aFunc, zName, nName);
1797 for(p=pFirst; p; p=p->pNext){
1798 if( 1 || p->xFunc || p->xStep ){
1799 if( p->nArg==nArg && p->iPrefEnc==eTextRep ){
1800 /* A perfect match. */
1801 pBest = p;
1802 matchqual = 4;
1803 break;
1804 }
1805 if( p->nArg==nArg ){
1806 /* Number of arguments matches, but not the text encoding */
1807 pBest = p;
1808 matchqual = 3;
1809 }
1810 else if( (p->nArg<0) || (nArg<0) ){
1811 if( matchqual<2 && p->iPrefEnc==eTextRep ){
1812 /* Matched a varargs function with correct text encoding */
1813 pBest = p;
1814 matchqual = 2;
1815 }
1816 if( matchqual<1 ){
1817 /* Matched a varargs function with incorrect text encoding */
1818 pBest = p;
1819 matchqual = 1;
1820 }
1821 }
1822 }
drh8e0a2f92002-02-23 23:45:45 +00001823 }
danielk1977d02eb1f2004-06-06 09:44:03 +00001824
1825 if( createFlag && matchqual<4 &&
1826 (pBest = sqliteMalloc(sizeof(*pBest)+nName+1)) ){
1827 pBest->nArg = nArg;
1828 pBest->pNext = pFirst;
1829 pBest->zName = (char*)&pBest[1];
danielk1977ad7dd422004-06-06 12:41:49 +00001830 pBest->iPrefEnc = eTextRep;
danielk1977d02eb1f2004-06-06 09:44:03 +00001831 memcpy(pBest->zName, zName, nName);
1832 pBest->zName[nName] = 0;
1833 sqlite3HashInsert(&db->aFunc, pBest->zName, nName, (void*)pBest);
drh8e0a2f92002-02-23 23:45:45 +00001834 }
danielk1977d02eb1f2004-06-06 09:44:03 +00001835
1836 if( pBest && (pBest->xStep || pBest->xFunc || createFlag) ){
1837 return pBest;
drh8e0a2f92002-02-23 23:45:45 +00001838 }
danielk1977d02eb1f2004-06-06 09:44:03 +00001839 return 0;
drh8e0a2f92002-02-23 23:45:45 +00001840}
danielk1977d02eb1f2004-06-06 09:44:03 +00001841