blob: a01ab8aa35f5f8980a95813f1764908793eca849 [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**
drh4c755c02004-08-08 20:22:17 +000015** $Id: expr.c,v 1.154 2004/08/08 20:22:17 drh Exp $
drhcce7d172000-05-31 15:34:51 +000016*/
17#include "sqliteInt.h"
drh04738cb2002-06-02 18:19:00 +000018#include <ctype.h>
drha2e00042002-01-22 03:13:42 +000019
danielk1977e014a832004-05-17 10:48:57 +000020char const *sqlite3AffinityString(char affinity){
21 switch( affinity ){
22 case SQLITE_AFF_INTEGER: return "i";
23 case SQLITE_AFF_NUMERIC: return "n";
24 case SQLITE_AFF_TEXT: return "t";
25 case SQLITE_AFF_NONE: return "o";
26 default:
27 assert(0);
28 }
danielk1977e0d4b062004-06-28 01:11:46 +000029 return 0;
danielk1977e014a832004-05-17 10:48:57 +000030}
31
32
33/*
34** Return the 'affinity' of the expression pExpr if any.
35**
36** If pExpr is a column, a reference to a column via an 'AS' alias,
37** or a sub-select with a column as the return value, then the
38** affinity of that column is returned. Otherwise, 0x00 is returned,
39** indicating no affinity for the expression.
40**
41** i.e. the WHERE clause expresssions in the following statements all
42** have an affinity:
43**
44** CREATE TABLE t1(a);
45** SELECT * FROM t1 WHERE a;
46** SELECT a AS b FROM t1 WHERE b;
47** SELECT * FROM t1 WHERE (select a from t1);
48*/
danielk1977bf3b7212004-05-18 10:06:24 +000049char sqlite3ExprAffinity(Expr *pExpr){
danielk1977a37cdde2004-05-16 11:15:36 +000050 if( pExpr->op==TK_AS ){
danielk1977bf3b7212004-05-18 10:06:24 +000051 return sqlite3ExprAffinity(pExpr->pLeft);
danielk1977a37cdde2004-05-16 11:15:36 +000052 }
53 if( pExpr->op==TK_SELECT ){
danielk1977bf3b7212004-05-18 10:06:24 +000054 return sqlite3ExprAffinity(pExpr->pSelect->pEList->a[0].pExpr);
danielk1977a37cdde2004-05-16 11:15:36 +000055 }
56 return pExpr->affinity;
57}
58
drh53db1452004-05-20 13:54:53 +000059/*
danielk19770202b292004-06-09 09:55:16 +000060** Return the default collation sequence for the expression pExpr. If
61** there is no default collation type, return 0.
62*/
danielk19777cedc8d2004-06-10 10:50:08 +000063CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
64 CollSeq *pColl = 0;
danielk19770202b292004-06-09 09:55:16 +000065 if( pExpr ){
danielk19777cedc8d2004-06-10 10:50:08 +000066 pColl = pExpr->pColl;
67 if( pExpr->op==TK_AS && !pColl ){
68 return sqlite3ExprCollSeq(pParse, pExpr->pLeft);
danielk19770202b292004-06-09 09:55:16 +000069 }
70 }
danielk19777cedc8d2004-06-10 10:50:08 +000071 if( sqlite3CheckCollSeq(pParse, pColl) ){
72 pColl = 0;
73 }
74 return pColl;
danielk19770202b292004-06-09 09:55:16 +000075}
76
77/*
drh53db1452004-05-20 13:54:53 +000078** pExpr is the left operand of a comparison operator. aff2 is the
79** type affinity of the right operand. This routine returns the
80** type affinity that should be used for the comparison operator.
81*/
danielk1977e014a832004-05-17 10:48:57 +000082char sqlite3CompareAffinity(Expr *pExpr, char aff2){
danielk1977bf3b7212004-05-18 10:06:24 +000083 char aff1 = sqlite3ExprAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +000084 if( aff1 && aff2 ){
85 /* Both sides of the comparison are columns. If one has numeric or
86 ** integer affinity, use that. Otherwise use no affinity.
87 */
88 if( aff1==SQLITE_AFF_INTEGER || aff2==SQLITE_AFF_INTEGER ){
89 return SQLITE_AFF_INTEGER;
90 }else if( aff1==SQLITE_AFF_NUMERIC || aff2==SQLITE_AFF_NUMERIC ){
91 return SQLITE_AFF_NUMERIC;
92 }else{
93 return SQLITE_AFF_NONE;
94 }
95 }else if( !aff1 && !aff2 ){
drh5f6a87b2004-07-19 00:39:45 +000096 /* Neither side of the comparison is a column. Compare the
97 ** results directly.
danielk1977e014a832004-05-17 10:48:57 +000098 */
drh5f6a87b2004-07-19 00:39:45 +000099 /* return SQLITE_AFF_NUMERIC; // Ticket #805 */
100 return SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +0000101 }else{
102 /* One side is a column, the other is not. Use the columns affinity. */
103 return (aff1 + aff2);
104 }
105}
106
drh53db1452004-05-20 13:54:53 +0000107/*
108** pExpr is a comparison operator. Return the type affinity that should
109** be applied to both operands prior to doing the comparison.
110*/
danielk1977e014a832004-05-17 10:48:57 +0000111static char comparisonAffinity(Expr *pExpr){
112 char aff;
113 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
114 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
115 pExpr->op==TK_NE );
116 assert( pExpr->pLeft );
danielk1977bf3b7212004-05-18 10:06:24 +0000117 aff = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +0000118 if( pExpr->pRight ){
119 aff = sqlite3CompareAffinity(pExpr->pRight, aff);
120 }
121 else if( pExpr->pSelect ){
122 aff = sqlite3CompareAffinity(pExpr->pSelect->pEList->a[0].pExpr, aff);
123 }
124 else if( !aff ){
125 aff = SQLITE_AFF_NUMERIC;
126 }
127 return aff;
128}
129
130/*
131** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
132** idx_affinity is the affinity of an indexed column. Return true
133** if the index with affinity idx_affinity may be used to implement
134** the comparison in pExpr.
135*/
136int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){
137 char aff = comparisonAffinity(pExpr);
138 return
139 (aff==SQLITE_AFF_NONE) ||
140 (aff==SQLITE_AFF_NUMERIC && idx_affinity==SQLITE_AFF_INTEGER) ||
141 (aff==SQLITE_AFF_INTEGER && idx_affinity==SQLITE_AFF_NUMERIC) ||
142 (aff==idx_affinity);
143}
144
danielk1977a37cdde2004-05-16 11:15:36 +0000145/*
146** Return the P1 value that should be used for a binary comparison
147** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
148** If jumpIfNull is true, then set the low byte of the returned
149** P1 value to tell the opcode to jump if either expression
150** evaluates to NULL.
151*/
danielk1977e014a832004-05-17 10:48:57 +0000152static int binaryCompareP1(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){
danielk1977bf3b7212004-05-18 10:06:24 +0000153 char aff = sqlite3ExprAffinity(pExpr2);
danielk1977e014a832004-05-17 10:48:57 +0000154 return (((int)sqlite3CompareAffinity(pExpr1, aff))<<8)+(jumpIfNull?1:0);
danielk1977a37cdde2004-05-16 11:15:36 +0000155}
156
drha2e00042002-01-22 03:13:42 +0000157/*
danielk19770202b292004-06-09 09:55:16 +0000158** Return a pointer to the collation sequence that should be used by
159** a binary comparison operator comparing pLeft and pRight.
160**
161** If the left hand expression has a collating sequence type, then it is
162** used. Otherwise the collation sequence for the right hand expression
163** is used, or the default (BINARY) if neither expression has a collating
164** type.
165*/
danielk19777cedc8d2004-06-10 10:50:08 +0000166static CollSeq* binaryCompareCollSeq(Parse *pParse, Expr *pLeft, Expr *pRight){
167 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pLeft);
danielk19770202b292004-06-09 09:55:16 +0000168 if( !pColl ){
danielk19777cedc8d2004-06-10 10:50:08 +0000169 pColl = sqlite3ExprCollSeq(pParse, pRight);
danielk19770202b292004-06-09 09:55:16 +0000170 }
171 return pColl;
172}
173
174/*
drhbe5c89a2004-07-26 00:31:09 +0000175** Generate code for a comparison operator.
176*/
177static int codeCompare(
178 Parse *pParse, /* The parsing (and code generating) context */
179 Expr *pLeft, /* The left operand */
180 Expr *pRight, /* The right operand */
181 int opcode, /* The comparison opcode */
182 int dest, /* Jump here if true. */
183 int jumpIfNull /* If true, jump if either operand is NULL */
184){
185 int p1 = binaryCompareP1(pLeft, pRight, jumpIfNull);
186 CollSeq *p3 = binaryCompareCollSeq(pParse, pLeft, pRight);
187 return sqlite3VdbeOp3(pParse->pVdbe, opcode, p1, dest, (void *)p3, P3_COLLSEQ);
188}
189
190/*
drha76b5df2002-02-23 02:32:10 +0000191** Construct a new expression node and return a pointer to it. Memory
192** for this node is obtained from sqliteMalloc(). The calling function
193** is responsible for making sure the node eventually gets freed.
194*/
danielk19774adee202004-05-08 08:23:19 +0000195Expr *sqlite3Expr(int op, Expr *pLeft, Expr *pRight, Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000196 Expr *pNew;
197 pNew = sqliteMalloc( sizeof(Expr) );
198 if( pNew==0 ){
drh4efc4752004-01-16 15:55:37 +0000199 /* When malloc fails, we leak memory from pLeft and pRight */
drha76b5df2002-02-23 02:32:10 +0000200 return 0;
201 }
202 pNew->op = op;
203 pNew->pLeft = pLeft;
204 pNew->pRight = pRight;
205 if( pToken ){
drh4b59ab52002-08-24 18:24:51 +0000206 assert( pToken->dyn==0 );
drha76b5df2002-02-23 02:32:10 +0000207 pNew->token = *pToken;
drh6977fea2002-10-22 23:38:04 +0000208 pNew->span = *pToken;
drha76b5df2002-02-23 02:32:10 +0000209 }else{
drh4efc4752004-01-16 15:55:37 +0000210 assert( pNew->token.dyn==0 );
211 assert( pNew->token.z==0 );
212 assert( pNew->token.n==0 );
drh6977fea2002-10-22 23:38:04 +0000213 if( pLeft && pRight ){
danielk19774adee202004-05-08 08:23:19 +0000214 sqlite3ExprSpan(pNew, &pLeft->span, &pRight->span);
drh6977fea2002-10-22 23:38:04 +0000215 }else{
216 pNew->span = pNew->token;
217 }
drha76b5df2002-02-23 02:32:10 +0000218 }
drha76b5df2002-02-23 02:32:10 +0000219 return pNew;
220}
221
222/*
drh6977fea2002-10-22 23:38:04 +0000223** Set the Expr.span field of the given expression to span all
drha76b5df2002-02-23 02:32:10 +0000224** text between the two given tokens.
225*/
danielk19774adee202004-05-08 08:23:19 +0000226void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
drh4efc4752004-01-16 15:55:37 +0000227 assert( pRight!=0 );
228 assert( pLeft!=0 );
229 /* Note: pExpr might be NULL due to a prior malloc failure */
230 if( pExpr && pRight->z && pLeft->z ){
drh4b59ab52002-08-24 18:24:51 +0000231 if( pLeft->dyn==0 && pRight->dyn==0 ){
drh6977fea2002-10-22 23:38:04 +0000232 pExpr->span.z = pLeft->z;
233 pExpr->span.n = pRight->n + Addr(pRight->z) - Addr(pLeft->z);
drh4b59ab52002-08-24 18:24:51 +0000234 }else{
drh6977fea2002-10-22 23:38:04 +0000235 pExpr->span.z = 0;
drh4b59ab52002-08-24 18:24:51 +0000236 }
drha76b5df2002-02-23 02:32:10 +0000237 }
238}
239
240/*
241** Construct a new expression node for a function with multiple
242** arguments.
243*/
danielk19774adee202004-05-08 08:23:19 +0000244Expr *sqlite3ExprFunction(ExprList *pList, Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000245 Expr *pNew;
246 pNew = sqliteMalloc( sizeof(Expr) );
247 if( pNew==0 ){
danielk19774adee202004-05-08 08:23:19 +0000248 /* sqlite3ExprListDelete(pList); // Leak pList when malloc fails */
drha76b5df2002-02-23 02:32:10 +0000249 return 0;
250 }
251 pNew->op = TK_FUNCTION;
252 pNew->pList = pList;
253 if( pToken ){
drh4b59ab52002-08-24 18:24:51 +0000254 assert( pToken->dyn==0 );
drha76b5df2002-02-23 02:32:10 +0000255 pNew->token = *pToken;
256 }else{
257 pNew->token.z = 0;
drha76b5df2002-02-23 02:32:10 +0000258 }
drh6977fea2002-10-22 23:38:04 +0000259 pNew->span = pNew->token;
drha76b5df2002-02-23 02:32:10 +0000260 return pNew;
261}
262
263/*
drha2e00042002-01-22 03:13:42 +0000264** Recursively delete an expression tree.
265*/
danielk19774adee202004-05-08 08:23:19 +0000266void sqlite3ExprDelete(Expr *p){
drha2e00042002-01-22 03:13:42 +0000267 if( p==0 ) return;
drh4efc4752004-01-16 15:55:37 +0000268 if( p->span.dyn ) sqliteFree((char*)p->span.z);
269 if( p->token.dyn ) sqliteFree((char*)p->token.z);
danielk19774adee202004-05-08 08:23:19 +0000270 sqlite3ExprDelete(p->pLeft);
271 sqlite3ExprDelete(p->pRight);
272 sqlite3ExprListDelete(p->pList);
273 sqlite3SelectDelete(p->pSelect);
drha2e00042002-01-22 03:13:42 +0000274 sqliteFree(p);
275}
276
drha76b5df2002-02-23 02:32:10 +0000277
278/*
drhff78bd22002-02-27 01:47:11 +0000279** The following group of routines make deep copies of expressions,
280** expression lists, ID lists, and select statements. The copies can
281** be deleted (by being passed to their respective ...Delete() routines)
282** without effecting the originals.
283**
danielk19774adee202004-05-08 08:23:19 +0000284** The expression list, ID, and source lists return by sqlite3ExprListDup(),
285** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
drhad3cab52002-05-24 02:04:32 +0000286** by subsequent calls to sqlite*ListAppend() routines.
drhff78bd22002-02-27 01:47:11 +0000287**
drhad3cab52002-05-24 02:04:32 +0000288** Any tables that the SrcList might point to are not duplicated.
drhff78bd22002-02-27 01:47:11 +0000289*/
danielk19774adee202004-05-08 08:23:19 +0000290Expr *sqlite3ExprDup(Expr *p){
drhff78bd22002-02-27 01:47:11 +0000291 Expr *pNew;
292 if( p==0 ) return 0;
drhfcb78a42003-01-18 20:11:05 +0000293 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000294 if( pNew==0 ) return 0;
drh3b167c72002-06-28 12:18:47 +0000295 memcpy(pNew, p, sizeof(*pNew));
drh6977fea2002-10-22 23:38:04 +0000296 if( p->token.z!=0 ){
drh4b59ab52002-08-24 18:24:51 +0000297 pNew->token.z = sqliteStrDup(p->token.z);
298 pNew->token.dyn = 1;
299 }else{
drh4efc4752004-01-16 15:55:37 +0000300 assert( pNew->token.z==0 );
drh4b59ab52002-08-24 18:24:51 +0000301 }
drh6977fea2002-10-22 23:38:04 +0000302 pNew->span.z = 0;
danielk19774adee202004-05-08 08:23:19 +0000303 pNew->pLeft = sqlite3ExprDup(p->pLeft);
304 pNew->pRight = sqlite3ExprDup(p->pRight);
305 pNew->pList = sqlite3ExprListDup(p->pList);
306 pNew->pSelect = sqlite3SelectDup(p->pSelect);
drhff78bd22002-02-27 01:47:11 +0000307 return pNew;
308}
danielk19774adee202004-05-08 08:23:19 +0000309void sqlite3TokenCopy(Token *pTo, Token *pFrom){
drh4b59ab52002-08-24 18:24:51 +0000310 if( pTo->dyn ) sqliteFree((char*)pTo->z);
drh4b59ab52002-08-24 18:24:51 +0000311 if( pFrom->z ){
312 pTo->n = pFrom->n;
313 pTo->z = sqliteStrNDup(pFrom->z, pFrom->n);
314 pTo->dyn = 1;
315 }else{
drh4b59ab52002-08-24 18:24:51 +0000316 pTo->z = 0;
drh4b59ab52002-08-24 18:24:51 +0000317 }
318}
danielk19774adee202004-05-08 08:23:19 +0000319ExprList *sqlite3ExprListDup(ExprList *p){
drhff78bd22002-02-27 01:47:11 +0000320 ExprList *pNew;
drh3e7bc9c2004-02-21 19:17:17 +0000321 struct ExprList_item *pItem;
drhff78bd22002-02-27 01:47:11 +0000322 int i;
323 if( p==0 ) return 0;
324 pNew = sqliteMalloc( sizeof(*pNew) );
325 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000326 pNew->nExpr = pNew->nAlloc = p->nExpr;
drh3e7bc9c2004-02-21 19:17:17 +0000327 pNew->a = pItem = sqliteMalloc( p->nExpr*sizeof(p->a[0]) );
danielk1977e0048402004-06-15 16:51:01 +0000328 if( pItem==0 ){
329 sqliteFree(pNew);
330 return 0;
331 }
drh1bdd9b52004-04-23 17:04:44 +0000332 for(i=0; i<p->nExpr; i++, pItem++){
drh4b59ab52002-08-24 18:24:51 +0000333 Expr *pNewExpr, *pOldExpr;
danielk19774adee202004-05-08 08:23:19 +0000334 pItem->pExpr = pNewExpr = sqlite3ExprDup(pOldExpr = p->a[i].pExpr);
drh6977fea2002-10-22 23:38:04 +0000335 if( pOldExpr->span.z!=0 && pNewExpr ){
336 /* Always make a copy of the span for top-level expressions in the
drh4b59ab52002-08-24 18:24:51 +0000337 ** expression list. The logic in SELECT processing that determines
338 ** the names of columns in the result set needs this information */
danielk19774adee202004-05-08 08:23:19 +0000339 sqlite3TokenCopy(&pNewExpr->span, &pOldExpr->span);
drh4b59ab52002-08-24 18:24:51 +0000340 }
drh1f3e9052002-10-31 00:09:39 +0000341 assert( pNewExpr==0 || pNewExpr->span.z!=0
danielk197724b03fd2004-05-10 10:34:34 +0000342 || pOldExpr->span.z==0 || sqlite3_malloc_failed );
drh3e7bc9c2004-02-21 19:17:17 +0000343 pItem->zName = sqliteStrDup(p->a[i].zName);
344 pItem->sortOrder = p->a[i].sortOrder;
345 pItem->isAgg = p->a[i].isAgg;
346 pItem->done = 0;
drhff78bd22002-02-27 01:47:11 +0000347 }
348 return pNew;
349}
danielk19774adee202004-05-08 08:23:19 +0000350SrcList *sqlite3SrcListDup(SrcList *p){
drhad3cab52002-05-24 02:04:32 +0000351 SrcList *pNew;
352 int i;
drh113088e2003-03-20 01:16:58 +0000353 int nByte;
drhad3cab52002-05-24 02:04:32 +0000354 if( p==0 ) return 0;
drh113088e2003-03-20 01:16:58 +0000355 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
drh4efc4752004-01-16 15:55:37 +0000356 pNew = sqliteMallocRaw( nByte );
drhad3cab52002-05-24 02:04:32 +0000357 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000358 pNew->nSrc = pNew->nAlloc = p->nSrc;
drhad3cab52002-05-24 02:04:32 +0000359 for(i=0; i<p->nSrc; i++){
drh4efc4752004-01-16 15:55:37 +0000360 struct SrcList_item *pNewItem = &pNew->a[i];
361 struct SrcList_item *pOldItem = &p->a[i];
362 pNewItem->zDatabase = sqliteStrDup(pOldItem->zDatabase);
363 pNewItem->zName = sqliteStrDup(pOldItem->zName);
364 pNewItem->zAlias = sqliteStrDup(pOldItem->zAlias);
365 pNewItem->jointype = pOldItem->jointype;
366 pNewItem->iCursor = pOldItem->iCursor;
367 pNewItem->pTab = 0;
danielk19774adee202004-05-08 08:23:19 +0000368 pNewItem->pSelect = sqlite3SelectDup(pOldItem->pSelect);
369 pNewItem->pOn = sqlite3ExprDup(pOldItem->pOn);
370 pNewItem->pUsing = sqlite3IdListDup(pOldItem->pUsing);
drhad3cab52002-05-24 02:04:32 +0000371 }
372 return pNew;
373}
danielk19774adee202004-05-08 08:23:19 +0000374IdList *sqlite3IdListDup(IdList *p){
drhff78bd22002-02-27 01:47:11 +0000375 IdList *pNew;
376 int i;
377 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000378 pNew = sqliteMallocRaw( sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +0000379 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000380 pNew->nId = pNew->nAlloc = p->nId;
drh4efc4752004-01-16 15:55:37 +0000381 pNew->a = sqliteMallocRaw( p->nId*sizeof(p->a[0]) );
drhe4697f52002-05-23 02:09:03 +0000382 if( pNew->a==0 ) return 0;
drhff78bd22002-02-27 01:47:11 +0000383 for(i=0; i<p->nId; i++){
drh4efc4752004-01-16 15:55:37 +0000384 struct IdList_item *pNewItem = &pNew->a[i];
385 struct IdList_item *pOldItem = &p->a[i];
386 pNewItem->zName = sqliteStrDup(pOldItem->zName);
387 pNewItem->idx = pOldItem->idx;
drhff78bd22002-02-27 01:47:11 +0000388 }
389 return pNew;
390}
danielk19774adee202004-05-08 08:23:19 +0000391Select *sqlite3SelectDup(Select *p){
drhff78bd22002-02-27 01:47:11 +0000392 Select *pNew;
393 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000394 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000395 if( pNew==0 ) return 0;
396 pNew->isDistinct = p->isDistinct;
danielk19774adee202004-05-08 08:23:19 +0000397 pNew->pEList = sqlite3ExprListDup(p->pEList);
398 pNew->pSrc = sqlite3SrcListDup(p->pSrc);
399 pNew->pWhere = sqlite3ExprDup(p->pWhere);
400 pNew->pGroupBy = sqlite3ExprListDup(p->pGroupBy);
401 pNew->pHaving = sqlite3ExprDup(p->pHaving);
402 pNew->pOrderBy = sqlite3ExprListDup(p->pOrderBy);
drhff78bd22002-02-27 01:47:11 +0000403 pNew->op = p->op;
danielk19774adee202004-05-08 08:23:19 +0000404 pNew->pPrior = sqlite3SelectDup(p->pPrior);
drhff78bd22002-02-27 01:47:11 +0000405 pNew->nLimit = p->nLimit;
406 pNew->nOffset = p->nOffset;
407 pNew->zSelect = 0;
drh7b58dae2003-07-20 01:16:46 +0000408 pNew->iLimit = -1;
409 pNew->iOffset = -1;
danielk1977dc1bdc42004-06-11 10:51:27 +0000410 pNew->ppOpenTemp = 0;
drhff78bd22002-02-27 01:47:11 +0000411 return pNew;
412}
413
414
415/*
drha76b5df2002-02-23 02:32:10 +0000416** Add a new element to the end of an expression list. If pList is
417** initially NULL, then create a new expression list.
418*/
danielk19774adee202004-05-08 08:23:19 +0000419ExprList *sqlite3ExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){
drha76b5df2002-02-23 02:32:10 +0000420 if( pList==0 ){
421 pList = sqliteMalloc( sizeof(ExprList) );
422 if( pList==0 ){
danielk19774adee202004-05-08 08:23:19 +0000423 /* sqlite3ExprDelete(pExpr); // Leak memory if malloc fails */
drha76b5df2002-02-23 02:32:10 +0000424 return 0;
425 }
drh4efc4752004-01-16 15:55:37 +0000426 assert( pList->nAlloc==0 );
drha76b5df2002-02-23 02:32:10 +0000427 }
drh4305d102003-07-30 12:34:12 +0000428 if( pList->nAlloc<=pList->nExpr ){
drh4305d102003-07-30 12:34:12 +0000429 pList->nAlloc = pList->nAlloc*2 + 4;
drh4efc4752004-01-16 15:55:37 +0000430 pList->a = sqliteRealloc(pList->a, pList->nAlloc*sizeof(pList->a[0]));
431 if( pList->a==0 ){
danielk19774adee202004-05-08 08:23:19 +0000432 /* sqlite3ExprDelete(pExpr); // Leak memory if malloc fails */
drh4efc4752004-01-16 15:55:37 +0000433 pList->nExpr = pList->nAlloc = 0;
drha76b5df2002-02-23 02:32:10 +0000434 return pList;
435 }
drha76b5df2002-02-23 02:32:10 +0000436 }
drh4efc4752004-01-16 15:55:37 +0000437 assert( pList->a!=0 );
438 if( pExpr || pName ){
439 struct ExprList_item *pItem = &pList->a[pList->nExpr++];
440 memset(pItem, 0, sizeof(*pItem));
441 pItem->pExpr = pExpr;
drha99db3b2004-06-19 14:49:12 +0000442 pItem->zName = sqlite3NameFromToken(pName);
drha76b5df2002-02-23 02:32:10 +0000443 }
444 return pList;
445}
446
447/*
448** Delete an entire expression list.
449*/
danielk19774adee202004-05-08 08:23:19 +0000450void sqlite3ExprListDelete(ExprList *pList){
drha76b5df2002-02-23 02:32:10 +0000451 int i;
drhbe5c89a2004-07-26 00:31:09 +0000452 struct ExprList_item *pItem;
drha76b5df2002-02-23 02:32:10 +0000453 if( pList==0 ) return;
drh1bdd9b52004-04-23 17:04:44 +0000454 assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
455 assert( pList->nExpr<=pList->nAlloc );
drhbe5c89a2004-07-26 00:31:09 +0000456 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
457 sqlite3ExprDelete(pItem->pExpr);
458 sqliteFree(pItem->zName);
drha76b5df2002-02-23 02:32:10 +0000459 }
460 sqliteFree(pList->a);
461 sqliteFree(pList);
462}
463
464/*
drhfef52082000-06-06 01:50:43 +0000465** Walk an expression tree. Return 1 if the expression is constant
466** and 0 if it involves variables.
drh23989372002-05-21 13:43:04 +0000467**
468** For the purposes of this function, a double-quoted string (ex: "abc")
469** is considered a variable but a single-quoted string (ex: 'abc') is
470** a constant.
drhfef52082000-06-06 01:50:43 +0000471*/
danielk19774adee202004-05-08 08:23:19 +0000472int sqlite3ExprIsConstant(Expr *p){
drhfef52082000-06-06 01:50:43 +0000473 switch( p->op ){
474 case TK_ID:
drh967e8b72000-06-21 13:59:10 +0000475 case TK_COLUMN:
drhfef52082000-06-06 01:50:43 +0000476 case TK_DOT:
drh7bdc0c12003-04-19 17:27:24 +0000477 case TK_FUNCTION:
drhfef52082000-06-06 01:50:43 +0000478 return 0;
drh7bdc0c12003-04-19 17:27:24 +0000479 case TK_NULL:
drh23989372002-05-21 13:43:04 +0000480 case TK_STRING:
danielk1977c572ef72004-05-27 09:28:41 +0000481 case TK_BLOB:
drh92086432002-01-22 14:11:29 +0000482 case TK_INTEGER:
483 case TK_FLOAT:
drh50457892003-09-06 01:10:47 +0000484 case TK_VARIABLE:
drh92086432002-01-22 14:11:29 +0000485 return 1;
drhfef52082000-06-06 01:50:43 +0000486 default: {
danielk19774adee202004-05-08 08:23:19 +0000487 if( p->pLeft && !sqlite3ExprIsConstant(p->pLeft) ) return 0;
488 if( p->pRight && !sqlite3ExprIsConstant(p->pRight) ) return 0;
drhfef52082000-06-06 01:50:43 +0000489 if( p->pList ){
490 int i;
491 for(i=0; i<p->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +0000492 if( !sqlite3ExprIsConstant(p->pList->a[i].pExpr) ) return 0;
drhfef52082000-06-06 01:50:43 +0000493 }
494 }
drh92086432002-01-22 14:11:29 +0000495 return p->pLeft!=0 || p->pRight!=0 || (p->pList && p->pList->nExpr>0);
drhfef52082000-06-06 01:50:43 +0000496 }
497 }
drh92086432002-01-22 14:11:29 +0000498 return 0;
drhfef52082000-06-06 01:50:43 +0000499}
500
501/*
drh202b2df2004-01-06 01:13:46 +0000502** If the given expression codes a constant integer that is small enough
503** to fit in a 32-bit integer, return 1 and put the value of the integer
504** in *pValue. If the expression is not an integer or if it is too big
505** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
drhe4de1fe2002-06-02 16:09:01 +0000506*/
danielk19774adee202004-05-08 08:23:19 +0000507int sqlite3ExprIsInteger(Expr *p, int *pValue){
drhe4de1fe2002-06-02 16:09:01 +0000508 switch( p->op ){
509 case TK_INTEGER: {
drhfec19aa2004-05-19 20:41:03 +0000510 if( sqlite3GetInt32(p->token.z, pValue) ){
drh202b2df2004-01-06 01:13:46 +0000511 return 1;
512 }
513 break;
drhe4de1fe2002-06-02 16:09:01 +0000514 }
515 case TK_STRING: {
drh4c755c02004-08-08 20:22:17 +0000516 const u8 *z = (u8*)p->token.z;
drhe4de1fe2002-06-02 16:09:01 +0000517 int n = p->token.n;
drhbd790ee2002-06-02 18:22:06 +0000518 if( n>0 && z[0]=='-' ){ z++; n--; }
drhe4de1fe2002-06-02 16:09:01 +0000519 while( n>0 && *z && isdigit(*z) ){ z++; n--; }
drhfec19aa2004-05-19 20:41:03 +0000520 if( n==0 && sqlite3GetInt32(p->token.z, pValue) ){
drhe4de1fe2002-06-02 16:09:01 +0000521 return 1;
522 }
523 break;
524 }
drh4b59ab52002-08-24 18:24:51 +0000525 case TK_UPLUS: {
danielk19774adee202004-05-08 08:23:19 +0000526 return sqlite3ExprIsInteger(p->pLeft, pValue);
drh4b59ab52002-08-24 18:24:51 +0000527 }
drhe4de1fe2002-06-02 16:09:01 +0000528 case TK_UMINUS: {
529 int v;
danielk19774adee202004-05-08 08:23:19 +0000530 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
drhe4de1fe2002-06-02 16:09:01 +0000531 *pValue = -v;
532 return 1;
533 }
534 break;
535 }
536 default: break;
537 }
538 return 0;
539}
540
541/*
drhc4a3c772001-04-04 11:48:57 +0000542** Return TRUE if the given string is a row-id column name.
543*/
danielk19774adee202004-05-08 08:23:19 +0000544int sqlite3IsRowid(const char *z){
545 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
546 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
547 if( sqlite3StrICmp(z, "OID")==0 ) return 1;
drhc4a3c772001-04-04 11:48:57 +0000548 return 0;
549}
550
551/*
drh8141f612004-01-25 22:44:58 +0000552** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
553** that name in the set of source tables in pSrcList and make the pExpr
554** expression node refer back to that source column. The following changes
555** are made to pExpr:
556**
557** pExpr->iDb Set the index in db->aDb[] of the database holding
558** the table.
559** pExpr->iTable Set to the cursor number for the table obtained
560** from pSrcList.
561** pExpr->iColumn Set to the column number within the table.
drh8141f612004-01-25 22:44:58 +0000562** pExpr->op Set to TK_COLUMN.
563** pExpr->pLeft Any expression this points to is deleted
564** pExpr->pRight Any expression this points to is deleted.
565**
566** The pDbToken is the name of the database (the "X"). This value may be
567** NULL meaning that name is of the form Y.Z or Z. Any available database
568** can be used. The pTableToken is the name of the table (the "Y"). This
569** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it
570** means that the form of the name is Z and that columns from any table
571** can be used.
572**
573** If the name cannot be resolved unambiguously, leave an error message
574** in pParse and return non-zero. Return zero on success.
575*/
576static int lookupName(
577 Parse *pParse, /* The parsing context */
578 Token *pDbToken, /* Name of the database containing table, or NULL */
579 Token *pTableToken, /* Name of table containing column, or NULL */
580 Token *pColumnToken, /* Name of the column. */
581 SrcList *pSrcList, /* List of tables used to resolve column names */
582 ExprList *pEList, /* List of expressions used to resolve "AS" */
583 Expr *pExpr /* Make this EXPR node point to the selected column */
584){
585 char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */
586 char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */
587 char *zCol = 0; /* Name of the column. The "Z" */
588 int i, j; /* Loop counters */
589 int cnt = 0; /* Number of matching column names */
590 int cntTab = 0; /* Number of matching table names */
drh7e26d752004-02-11 10:35:29 +0000591 sqlite *db = pParse->db; /* The database */
drh8141f612004-01-25 22:44:58 +0000592
593 assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
drha99db3b2004-06-19 14:49:12 +0000594 zDb = sqlite3NameFromToken(pDbToken);
595 zTab = sqlite3NameFromToken(pTableToken);
596 zCol = sqlite3NameFromToken(pColumnToken);
danielk197724b03fd2004-05-10 10:34:34 +0000597 if( sqlite3_malloc_failed ){
drh8141f612004-01-25 22:44:58 +0000598 return 1; /* Leak memory (zDb and zTab) if malloc fails */
599 }
600 assert( zTab==0 || pEList==0 );
601
602 pExpr->iTable = -1;
603 for(i=0; i<pSrcList->nSrc; i++){
604 struct SrcList_item *pItem = &pSrcList->a[i];
605 Table *pTab = pItem->pTab;
606 Column *pCol;
607
608 if( pTab==0 ) continue;
609 assert( pTab->nCol>0 );
610 if( zTab ){
611 if( pItem->zAlias ){
612 char *zTabName = pItem->zAlias;
danielk19774adee202004-05-08 08:23:19 +0000613 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
drh8141f612004-01-25 22:44:58 +0000614 }else{
615 char *zTabName = pTab->zName;
danielk19774adee202004-05-08 08:23:19 +0000616 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
617 if( zDb!=0 && sqlite3StrICmp(db->aDb[pTab->iDb].zName, zDb)!=0 ){
drh8141f612004-01-25 22:44:58 +0000618 continue;
619 }
620 }
621 }
622 if( 0==(cntTab++) ){
623 pExpr->iTable = pItem->iCursor;
624 pExpr->iDb = pTab->iDb;
625 }
626 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
danielk19774adee202004-05-08 08:23:19 +0000627 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
drh8141f612004-01-25 22:44:58 +0000628 cnt++;
629 pExpr->iTable = pItem->iCursor;
630 pExpr->iDb = pTab->iDb;
631 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
632 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
danielk1977a37cdde2004-05-16 11:15:36 +0000633 pExpr->affinity = pTab->aCol[j].affinity;
danielk19770202b292004-06-09 09:55:16 +0000634 pExpr->pColl = pTab->aCol[j].pColl;
drh8141f612004-01-25 22:44:58 +0000635 break;
636 }
637 }
638 }
639
640 /* If we have not already resolved the name, then maybe
641 ** it is a new.* or old.* trigger argument reference
642 */
643 if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
644 TriggerStack *pTriggerStack = pParse->trigStack;
645 Table *pTab = 0;
danielk19774adee202004-05-08 08:23:19 +0000646 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){
drh8141f612004-01-25 22:44:58 +0000647 pExpr->iTable = pTriggerStack->newIdx;
648 assert( pTriggerStack->pTab );
649 pTab = pTriggerStack->pTab;
danielk19774adee202004-05-08 08:23:19 +0000650 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab) == 0 ){
drh8141f612004-01-25 22:44:58 +0000651 pExpr->iTable = pTriggerStack->oldIdx;
652 assert( pTriggerStack->pTab );
653 pTab = pTriggerStack->pTab;
654 }
655
656 if( pTab ){
657 int j;
658 Column *pCol = pTab->aCol;
659
660 pExpr->iDb = pTab->iDb;
661 cntTab++;
662 for(j=0; j < pTab->nCol; j++, pCol++) {
danielk19774adee202004-05-08 08:23:19 +0000663 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
drh8141f612004-01-25 22:44:58 +0000664 cnt++;
665 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
danielk1977a37cdde2004-05-16 11:15:36 +0000666 pExpr->affinity = pTab->aCol[j].affinity;
danielk19770202b292004-06-09 09:55:16 +0000667 pExpr->pColl = pTab->aCol[j].pColl;
drh8141f612004-01-25 22:44:58 +0000668 break;
669 }
670 }
671 }
672 }
673
674 /*
675 ** Perhaps the name is a reference to the ROWID
676 */
danielk19774adee202004-05-08 08:23:19 +0000677 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
drh8141f612004-01-25 22:44:58 +0000678 cnt = 1;
679 pExpr->iColumn = -1;
danielk1977a37cdde2004-05-16 11:15:36 +0000680 pExpr->affinity = SQLITE_AFF_INTEGER;
drh8141f612004-01-25 22:44:58 +0000681 }
682
683 /*
684 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
685 ** might refer to an result-set alias. This happens, for example, when
686 ** we are resolving names in the WHERE clause of the following command:
687 **
688 ** SELECT a+b AS x FROM table WHERE x<10;
689 **
690 ** In cases like this, replace pExpr with a copy of the expression that
691 ** forms the result set entry ("a+b" in the example) and return immediately.
692 ** Note that the expression in the result set should have already been
693 ** resolved by the time the WHERE clause is resolved.
694 */
695 if( cnt==0 && pEList!=0 ){
696 for(j=0; j<pEList->nExpr; j++){
697 char *zAs = pEList->a[j].zName;
danielk19774adee202004-05-08 08:23:19 +0000698 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
drh8141f612004-01-25 22:44:58 +0000699 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
700 pExpr->op = TK_AS;
701 pExpr->iColumn = j;
danielk19774adee202004-05-08 08:23:19 +0000702 pExpr->pLeft = sqlite3ExprDup(pEList->a[j].pExpr);
drh8141f612004-01-25 22:44:58 +0000703 sqliteFree(zCol);
704 assert( zTab==0 && zDb==0 );
705 return 0;
706 }
707 }
708 }
709
710 /*
711 ** If X and Y are NULL (in other words if only the column name Z is
712 ** supplied) and the value of Z is enclosed in double-quotes, then
713 ** Z is a string literal if it doesn't match any column names. In that
714 ** case, we need to return right away and not make any changes to
715 ** pExpr.
716 */
717 if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
718 sqliteFree(zCol);
719 return 0;
720 }
721
722 /*
723 ** cnt==0 means there was not match. cnt>1 means there were two or
724 ** more matches. Either way, we have an error.
725 */
726 if( cnt!=1 ){
727 char *z = 0;
728 char *zErr;
729 zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s";
730 if( zDb ){
danielk19774adee202004-05-08 08:23:19 +0000731 sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, 0);
drh8141f612004-01-25 22:44:58 +0000732 }else if( zTab ){
danielk19774adee202004-05-08 08:23:19 +0000733 sqlite3SetString(&z, zTab, ".", zCol, 0);
drh8141f612004-01-25 22:44:58 +0000734 }else{
735 z = sqliteStrDup(zCol);
736 }
danielk19774adee202004-05-08 08:23:19 +0000737 sqlite3ErrorMsg(pParse, zErr, z);
drh8141f612004-01-25 22:44:58 +0000738 sqliteFree(z);
739 }
740
741 /* Clean up and return
742 */
743 sqliteFree(zDb);
744 sqliteFree(zTab);
745 sqliteFree(zCol);
danielk19774adee202004-05-08 08:23:19 +0000746 sqlite3ExprDelete(pExpr->pLeft);
drh8141f612004-01-25 22:44:58 +0000747 pExpr->pLeft = 0;
danielk19774adee202004-05-08 08:23:19 +0000748 sqlite3ExprDelete(pExpr->pRight);
drh8141f612004-01-25 22:44:58 +0000749 pExpr->pRight = 0;
750 pExpr->op = TK_COLUMN;
danielk19774adee202004-05-08 08:23:19 +0000751 sqlite3AuthRead(pParse, pExpr, pSrcList);
drh8141f612004-01-25 22:44:58 +0000752 return cnt!=1;
753}
754
755/*
drhcce7d172000-05-31 15:34:51 +0000756** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +0000757** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +0000758** index to the table in the table list and a column offset. The
759** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
760** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +0000761** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +0000762** VDBE cursor number for a cursor that is pointing into the referenced
763** table. The Expr.iColumn value is changed to the index of the column
764** of the referenced table. The Expr.iColumn value for the special
765** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
766** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +0000767**
drhfef52082000-06-06 01:50:43 +0000768** We also check for instances of the IN operator. IN comes in two
769** forms:
770**
771** expr IN (exprlist)
772** and
773** expr IN (SELECT ...)
774**
775** The first form is handled by creating a set holding the list
776** of allowed values. The second form causes the SELECT to generate
777** a temporary table.
778**
779** This routine also looks for scalar SELECTs that are part of an expression.
drh19a775c2000-06-05 18:54:46 +0000780** If it finds any, it generates code to write the value of that select
781** into a memory cell.
drhcce7d172000-05-31 15:34:51 +0000782**
drh967e8b72000-06-21 13:59:10 +0000783** Unknown columns or tables provoke an error. The function returns
drhcce7d172000-05-31 15:34:51 +0000784** the number of errors seen and leaves an error message on pParse->zErrMsg.
785*/
danielk19774adee202004-05-08 08:23:19 +0000786int sqlite3ExprResolveIds(
drha2e00042002-01-22 03:13:42 +0000787 Parse *pParse, /* The parser context */
drh8141f612004-01-25 22:44:58 +0000788 SrcList *pSrcList, /* List of tables used to resolve column names */
drha2e00042002-01-22 03:13:42 +0000789 ExprList *pEList, /* List of expressions used to resolve "AS" */
790 Expr *pExpr /* The expression to be analyzed. */
791){
drh6a3ea0e2003-05-02 14:32:12 +0000792 int i;
793
drh8141f612004-01-25 22:44:58 +0000794 if( pExpr==0 || pSrcList==0 ) return 0;
795 for(i=0; i<pSrcList->nSrc; i++){
796 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab );
drh6a3ea0e2003-05-02 14:32:12 +0000797 }
drhcce7d172000-05-31 15:34:51 +0000798 switch( pExpr->op ){
drh23989372002-05-21 13:43:04 +0000799 /* Double-quoted strings (ex: "abc") are used as identifiers if
800 ** possible. Otherwise they remain as strings. Single-quoted
801 ** strings (ex: 'abc') are always string literals.
802 */
803 case TK_STRING: {
804 if( pExpr->token.z[0]=='\'' ) break;
805 /* Fall thru into the TK_ID case if this is a double-quoted string */
806 }
drh8141f612004-01-25 22:44:58 +0000807 /* A lone identifier is the name of a columnd.
drha2e00042002-01-22 03:13:42 +0000808 */
drhcce7d172000-05-31 15:34:51 +0000809 case TK_ID: {
drh8141f612004-01-25 22:44:58 +0000810 if( lookupName(pParse, 0, 0, &pExpr->token, pSrcList, pEList, pExpr) ){
drhcce7d172000-05-31 15:34:51 +0000811 return 1;
drhed6c8672003-01-12 18:02:16 +0000812 }
drhcce7d172000-05-31 15:34:51 +0000813 break;
814 }
815
drhd24cc422003-03-27 12:51:24 +0000816 /* A table name and column name: ID.ID
817 ** Or a database, table and column: ID.ID.ID
818 */
drhcce7d172000-05-31 15:34:51 +0000819 case TK_DOT: {
drh8141f612004-01-25 22:44:58 +0000820 Token *pColumn;
821 Token *pTable;
822 Token *pDb;
823 Expr *pRight;
drhcce7d172000-05-31 15:34:51 +0000824
drhcce7d172000-05-31 15:34:51 +0000825 pRight = pExpr->pRight;
drhd24cc422003-03-27 12:51:24 +0000826 if( pRight->op==TK_ID ){
drh8141f612004-01-25 22:44:58 +0000827 pDb = 0;
828 pTable = &pExpr->pLeft->token;
829 pColumn = &pRight->token;
drhd24cc422003-03-27 12:51:24 +0000830 }else{
drh8141f612004-01-25 22:44:58 +0000831 assert( pRight->op==TK_DOT );
832 pDb = &pExpr->pLeft->token;
833 pTable = &pRight->pLeft->token;
834 pColumn = &pRight->pRight->token;
drhd24cc422003-03-27 12:51:24 +0000835 }
drh8141f612004-01-25 22:44:58 +0000836 if( lookupName(pParse, pDb, pTable, pColumn, pSrcList, 0, pExpr) ){
drhdaffd0e2001-04-11 14:28:42 +0000837 return 1;
838 }
drhcce7d172000-05-31 15:34:51 +0000839 break;
840 }
841
drhfef52082000-06-06 01:50:43 +0000842 case TK_IN: {
danielk1977e014a832004-05-17 10:48:57 +0000843 char affinity;
danielk19774adee202004-05-08 08:23:19 +0000844 Vdbe *v = sqlite3GetVdbe(pParse);
drhd3d39e92004-05-20 22:16:29 +0000845 KeyInfo keyInfo;
danielk19770202b292004-06-09 09:55:16 +0000846 int addr; /* Address of OP_OpenTemp instruction */
drhd3d39e92004-05-20 22:16:29 +0000847
drhfef52082000-06-06 01:50:43 +0000848 if( v==0 ) return 1;
danielk19774adee202004-05-08 08:23:19 +0000849 if( sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pLeft) ){
drhcfab11b2000-06-06 03:31:22 +0000850 return 1;
851 }
danielk1977bf3b7212004-05-18 10:06:24 +0000852 affinity = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +0000853
854 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
855 ** expression it is handled the same way. A temporary table is
856 ** filled with single-field index keys representing the results
857 ** from the SELECT or the <exprlist>.
858 **
859 ** If the 'x' expression is a column value, or the SELECT...
860 ** statement returns a column value, then the affinity of that
861 ** column is used to build the index keys. If both 'x' and the
862 ** SELECT... statement are columns, then numeric affinity is used
863 ** if either column has NUMERIC or INTEGER affinity. If neither
864 ** 'x' nor the SELECT... statement are columns, then numeric affinity
865 ** is used.
866 */
867 pExpr->iTable = pParse->nTab++;
danielk19770202b292004-06-09 09:55:16 +0000868 addr = sqlite3VdbeAddOp(v, OP_OpenTemp, pExpr->iTable, 0);
drhd3d39e92004-05-20 22:16:29 +0000869 memset(&keyInfo, 0, sizeof(keyInfo));
870 keyInfo.nField = 1;
drhf3218fe2004-05-28 08:21:02 +0000871 sqlite3VdbeAddOp(v, OP_SetNumColumns, pExpr->iTable, 1);
danielk1977e014a832004-05-17 10:48:57 +0000872
drhfef52082000-06-06 01:50:43 +0000873 if( pExpr->pSelect ){
874 /* Case 1: expr IN (SELECT ...)
875 **
danielk1977e014a832004-05-17 10:48:57 +0000876 ** Generate code to write the results of the select into the temporary
877 ** table allocated and opened above.
drhfef52082000-06-06 01:50:43 +0000878 */
danielk1977e014a832004-05-17 10:48:57 +0000879 int iParm = pExpr->iTable + (((int)affinity)<<16);
drhbe5c89a2004-07-26 00:31:09 +0000880 ExprList *pEList;
danielk1977e014a832004-05-17 10:48:57 +0000881 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
danielk1977bf3b7212004-05-18 10:06:24 +0000882 sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +0000883 pEList = pExpr->pSelect->pEList;
884 if( pEList && pEList->nExpr>0 ){
danielk19777cedc8d2004-06-10 10:50:08 +0000885 keyInfo.aColl[0] = binaryCompareCollSeq(pParse, pExpr->pLeft,
drhbe5c89a2004-07-26 00:31:09 +0000886 pEList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +0000887 }
drhfef52082000-06-06 01:50:43 +0000888 }else if( pExpr->pList ){
889 /* Case 2: expr IN (exprlist)
890 **
danielk1977e014a832004-05-17 10:48:57 +0000891 ** For each expression, build an index key from the evaluation and
892 ** store it in the temporary table. If <expr> is a column, then use
893 ** that columns affinity when building index keys. If <expr> is not
894 ** a column, use numeric affinity.
drhfef52082000-06-06 01:50:43 +0000895 */
danielk1977e014a832004-05-17 10:48:57 +0000896 int i;
897 char const *affStr;
898 if( !affinity ){
899 affinity = SQLITE_AFF_NUMERIC;
900 }
901 affStr = sqlite3AffinityString(affinity);
danielk19770202b292004-06-09 09:55:16 +0000902 keyInfo.aColl[0] = pExpr->pLeft->pColl;
danielk1977e014a832004-05-17 10:48:57 +0000903
904 /* Loop through each expression in <exprlist>. */
drhfef52082000-06-06 01:50:43 +0000905 for(i=0; i<pExpr->pList->nExpr; i++){
906 Expr *pE2 = pExpr->pList->a[i].pExpr;
danielk1977e014a832004-05-17 10:48:57 +0000907
908 /* Check that the expression is constant and valid. */
danielk19774adee202004-05-08 08:23:19 +0000909 if( !sqlite3ExprIsConstant(pE2) ){
910 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +0000911 "right-hand side of IN operator must be constant");
drhfef52082000-06-06 01:50:43 +0000912 return 1;
913 }
danielk19774adee202004-05-08 08:23:19 +0000914 if( sqlite3ExprCheck(pParse, pE2, 0, 0) ){
drh4794b982000-06-06 13:54:14 +0000915 return 1;
916 }
danielk1977e014a832004-05-17 10:48:57 +0000917
918 /* Evaluate the expression and insert it into the temp table */
919 sqlite3ExprCode(pParse, pE2);
danielk1977ededfd52004-06-17 07:53:01 +0000920 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, affStr, P3_STATIC);
danielk19770f69c1e2004-05-29 11:24:50 +0000921 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +0000922 sqlite3VdbeAddOp(v, OP_PutStrKey, pExpr->iTable, 0);
drhfef52082000-06-06 01:50:43 +0000923 }
924 }
danielk19770202b292004-06-09 09:55:16 +0000925 sqlite3VdbeChangeP3(v, addr, (void *)&keyInfo, P3_KEYINFO);
926
drhcfab11b2000-06-06 03:31:22 +0000927 break;
drhfef52082000-06-06 01:50:43 +0000928 }
929
drh19a775c2000-06-05 18:54:46 +0000930 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +0000931 /* This has to be a scalar SELECT. Generate code to put the
932 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +0000933 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +0000934 */
drh967e8b72000-06-21 13:59:10 +0000935 pExpr->iColumn = pParse->nMem++;
danielk1977bf3b7212004-05-18 10:06:24 +0000936 if(sqlite3Select(pParse, pExpr->pSelect, SRT_Mem,pExpr->iColumn,0,0,0,0)){
drh19a775c2000-06-05 18:54:46 +0000937 return 1;
938 }
939 break;
940 }
941
drhcce7d172000-05-31 15:34:51 +0000942 /* For all else, just recursively walk the tree */
943 default: {
drh4794b982000-06-06 13:54:14 +0000944 if( pExpr->pLeft
danielk19774adee202004-05-08 08:23:19 +0000945 && sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pLeft) ){
drhcce7d172000-05-31 15:34:51 +0000946 return 1;
947 }
948 if( pExpr->pRight
danielk19774adee202004-05-08 08:23:19 +0000949 && sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pRight) ){
drhcce7d172000-05-31 15:34:51 +0000950 return 1;
951 }
952 if( pExpr->pList ){
953 int i;
954 ExprList *pList = pExpr->pList;
955 for(i=0; i<pList->nExpr; i++){
drh832508b2002-03-02 17:04:07 +0000956 Expr *pArg = pList->a[i].pExpr;
danielk19774adee202004-05-08 08:23:19 +0000957 if( sqlite3ExprResolveIds(pParse, pSrcList, pEList, pArg) ){
drhcce7d172000-05-31 15:34:51 +0000958 return 1;
959 }
960 }
961 }
962 }
963 }
964 return 0;
965}
966
drhcce7d172000-05-31 15:34:51 +0000967/*
drh4b59ab52002-08-24 18:24:51 +0000968** pExpr is a node that defines a function of some kind. It might
969** be a syntactic function like "count(x)" or it might be a function
970** that implements an operator, like "a LIKE b".
971**
972** This routine makes *pzName point to the name of the function and
973** *pnName hold the number of characters in the function name.
974*/
975static void getFunctionName(Expr *pExpr, const char **pzName, int *pnName){
976 switch( pExpr->op ){
977 case TK_FUNCTION: {
978 *pzName = pExpr->token.z;
drh6977fea2002-10-22 23:38:04 +0000979 *pnName = pExpr->token.n;
drh4b59ab52002-08-24 18:24:51 +0000980 break;
981 }
982 case TK_LIKE: {
983 *pzName = "like";
984 *pnName = 4;
985 break;
986 }
987 case TK_GLOB: {
988 *pzName = "glob";
989 *pnName = 4;
990 break;
991 }
992 default: {
993 *pzName = "can't happen";
994 *pnName = 12;
995 break;
996 }
997 }
998}
999
1000/*
drhcce7d172000-05-31 15:34:51 +00001001** Error check the functions in an expression. Make sure all
1002** function names are recognized and all functions have the correct
1003** number of arguments. Leave an error message in pParse->zErrMsg
1004** if anything is amiss. Return the number of errors.
1005**
1006** if pIsAgg is not null and this expression is an aggregate function
1007** (like count(*) or max(value)) then write a 1 into *pIsAgg.
1008*/
danielk19774adee202004-05-08 08:23:19 +00001009int sqlite3ExprCheck(Parse *pParse, Expr *pExpr, int allowAgg, int *pIsAgg){
drhcce7d172000-05-31 15:34:51 +00001010 int nErr = 0;
1011 if( pExpr==0 ) return 0;
drhcce7d172000-05-31 15:34:51 +00001012 switch( pExpr->op ){
drh4b59ab52002-08-24 18:24:51 +00001013 case TK_GLOB:
1014 case TK_LIKE:
drhcce7d172000-05-31 15:34:51 +00001015 case TK_FUNCTION: {
drhc9b84a12002-06-20 11:36:48 +00001016 int n = pExpr->pList ? pExpr->pList->nExpr : 0; /* Number of arguments */
1017 int no_such_func = 0; /* True if no such function exists */
drhc9b84a12002-06-20 11:36:48 +00001018 int wrong_num_args = 0; /* True if wrong number of arguments */
1019 int is_agg = 0; /* True if is an aggregate function */
drhcce7d172000-05-31 15:34:51 +00001020 int i;
drh4b59ab52002-08-24 18:24:51 +00001021 int nId; /* Number of characters in function name */
1022 const char *zId; /* The function name. */
drh0bce8352002-02-28 00:41:10 +00001023 FuncDef *pDef;
danielk1977d8123362004-06-12 09:25:12 +00001024 int enc = pParse->db->enc;
drh0bce8352002-02-28 00:41:10 +00001025
drh4b59ab52002-08-24 18:24:51 +00001026 getFunctionName(pExpr, &zId, &nId);
danielk1977d8123362004-06-12 09:25:12 +00001027 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001028 if( pDef==0 ){
danielk1977d8123362004-06-12 09:25:12 +00001029 pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001030 if( pDef==0 ){
drh268380c2004-02-25 13:47:31 +00001031 no_such_func = 1;
drh0bce8352002-02-28 00:41:10 +00001032 }else{
1033 wrong_num_args = 1;
drhcce7d172000-05-31 15:34:51 +00001034 }
drh0bce8352002-02-28 00:41:10 +00001035 }else{
1036 is_agg = pDef->xFunc==0;
drhcce7d172000-05-31 15:34:51 +00001037 }
drh8e0a2f92002-02-23 23:45:45 +00001038 if( is_agg && !allowAgg ){
danielk19774adee202004-05-08 08:23:19 +00001039 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId, zId);
drh8e0a2f92002-02-23 23:45:45 +00001040 nErr++;
1041 is_agg = 0;
1042 }else if( no_such_func ){
danielk19774adee202004-05-08 08:23:19 +00001043 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
drhcce7d172000-05-31 15:34:51 +00001044 nErr++;
drh8e0a2f92002-02-23 23:45:45 +00001045 }else if( wrong_num_args ){
danielk19774adee202004-05-08 08:23:19 +00001046 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
drhf7a9e1a2004-02-22 18:40:56 +00001047 nId, zId);
drh8e0a2f92002-02-23 23:45:45 +00001048 nErr++;
drhcce7d172000-05-31 15:34:51 +00001049 }
drhf7a9e1a2004-02-22 18:40:56 +00001050 if( is_agg ){
1051 pExpr->op = TK_AGG_FUNCTION;
1052 if( pIsAgg ) *pIsAgg = 1;
1053 }
drhcce7d172000-05-31 15:34:51 +00001054 for(i=0; nErr==0 && i<n; i++){
danielk19774adee202004-05-08 08:23:19 +00001055 nErr = sqlite3ExprCheck(pParse, pExpr->pList->a[i].pExpr,
drh4cfa7932000-06-08 15:10:46 +00001056 allowAgg && !is_agg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +00001057 }
danielk19770202b292004-06-09 09:55:16 +00001058 /* FIX ME: Compute pExpr->affinity based on the expected return
1059 ** type of the function
1060 */
drhcce7d172000-05-31 15:34:51 +00001061 }
1062 default: {
1063 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001064 nErr = sqlite3ExprCheck(pParse, pExpr->pLeft, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +00001065 }
1066 if( nErr==0 && pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001067 nErr = sqlite3ExprCheck(pParse, pExpr->pRight, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +00001068 }
drhfef52082000-06-06 01:50:43 +00001069 if( nErr==0 && pExpr->pList ){
1070 int n = pExpr->pList->nExpr;
1071 int i;
1072 for(i=0; nErr==0 && i<n; i++){
drh22827922000-06-06 17:27:05 +00001073 Expr *pE2 = pExpr->pList->a[i].pExpr;
danielk19774adee202004-05-08 08:23:19 +00001074 nErr = sqlite3ExprCheck(pParse, pE2, allowAgg, pIsAgg);
drhfef52082000-06-06 01:50:43 +00001075 }
1076 }
drhcce7d172000-05-31 15:34:51 +00001077 break;
1078 }
1079 }
1080 return nErr;
1081}
1082
1083/*
drhfec19aa2004-05-19 20:41:03 +00001084** Generate an instruction that will put the integer describe by
1085** text z[0..n-1] on the stack.
1086*/
1087static void codeInteger(Vdbe *v, const char *z, int n){
1088 int i;
drh6fec0762004-05-30 01:38:43 +00001089 if( sqlite3GetInt32(z, &i) ){
1090 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
1091 }else if( sqlite3FitsIn64Bits(z) ){
1092 sqlite3VdbeOp3(v, OP_Integer, 0, 0, z, n);
drhfec19aa2004-05-19 20:41:03 +00001093 }else{
1094 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n);
1095 }
1096}
1097
1098/*
drhcce7d172000-05-31 15:34:51 +00001099** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +00001100** expression and leave the result on the top of stack.
drhcce7d172000-05-31 15:34:51 +00001101*/
danielk19774adee202004-05-08 08:23:19 +00001102void sqlite3ExprCode(Parse *pParse, Expr *pExpr){
drhcce7d172000-05-31 15:34:51 +00001103 Vdbe *v = pParse->pVdbe;
1104 int op;
drhdaffd0e2001-04-11 14:28:42 +00001105 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001106 switch( pExpr->op ){
1107 case TK_PLUS: op = OP_Add; break;
1108 case TK_MINUS: op = OP_Subtract; break;
1109 case TK_STAR: op = OP_Multiply; break;
1110 case TK_SLASH: op = OP_Divide; break;
1111 case TK_AND: op = OP_And; break;
1112 case TK_OR: op = OP_Or; break;
1113 case TK_LT: op = OP_Lt; break;
1114 case TK_LE: op = OP_Le; break;
1115 case TK_GT: op = OP_Gt; break;
1116 case TK_GE: op = OP_Ge; break;
1117 case TK_NE: op = OP_Ne; break;
1118 case TK_EQ: op = OP_Eq; break;
drhcce7d172000-05-31 15:34:51 +00001119 case TK_ISNULL: op = OP_IsNull; break;
1120 case TK_NOTNULL: op = OP_NotNull; break;
1121 case TK_NOT: op = OP_Not; break;
1122 case TK_UMINUS: op = OP_Negative; break;
drhbf4133c2001-10-13 02:59:08 +00001123 case TK_BITAND: op = OP_BitAnd; break;
1124 case TK_BITOR: op = OP_BitOr; break;
1125 case TK_BITNOT: op = OP_BitNot; break;
1126 case TK_LSHIFT: op = OP_ShiftLeft; break;
1127 case TK_RSHIFT: op = OP_ShiftRight; break;
1128 case TK_REM: op = OP_Remainder; break;
drhfec19aa2004-05-19 20:41:03 +00001129 case TK_FLOAT: op = OP_Real; break;
danielk19770f69c1e2004-05-29 11:24:50 +00001130 case TK_STRING: op = OP_String8; break;
danielk1977c572ef72004-05-27 09:28:41 +00001131 case TK_BLOB: op = OP_HexBlob; break;
danielk1977cfe9a692004-06-16 12:00:29 +00001132 default: op = 0; break;
drhcce7d172000-05-31 15:34:51 +00001133 }
1134 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00001135 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +00001136 if( pParse->useAgg ){
danielk19774adee202004-05-08 08:23:19 +00001137 sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drhc4a3c772001-04-04 11:48:57 +00001138 }else if( pExpr->iColumn>=0 ){
danielk19774adee202004-05-08 08:23:19 +00001139 sqlite3VdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn);
drhc4a3c772001-04-04 11:48:57 +00001140 }else{
danielk19774adee202004-05-08 08:23:19 +00001141 sqlite3VdbeAddOp(v, OP_Recno, pExpr->iTable, 0);
drh22827922000-06-06 17:27:05 +00001142 }
drhcce7d172000-05-31 15:34:51 +00001143 break;
1144 }
1145 case TK_INTEGER: {
drhfec19aa2004-05-19 20:41:03 +00001146 codeInteger(v, pExpr->token.z, pExpr->token.n);
1147 break;
1148 }
1149 case TK_FLOAT:
1150 case TK_STRING: {
1151 sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z, pExpr->token.n);
danielk19774adee202004-05-08 08:23:19 +00001152 sqlite3VdbeDequoteP3(v, -1);
drhcce7d172000-05-31 15:34:51 +00001153 break;
1154 }
danielk1977c572ef72004-05-27 09:28:41 +00001155 case TK_BLOB: {
1156 sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z+1, pExpr->token.n-1);
1157 sqlite3VdbeDequoteP3(v, -1);
1158 break;
1159 }
drhcce7d172000-05-31 15:34:51 +00001160 case TK_NULL: {
danielk19770f69c1e2004-05-29 11:24:50 +00001161 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001162 break;
1163 }
drh50457892003-09-06 01:10:47 +00001164 case TK_VARIABLE: {
danielk19774adee202004-05-08 08:23:19 +00001165 sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0);
drh50457892003-09-06 01:10:47 +00001166 break;
1167 }
drhc9b84a12002-06-20 11:36:48 +00001168 case TK_LT:
1169 case TK_LE:
1170 case TK_GT:
1171 case TK_GE:
1172 case TK_NE:
1173 case TK_EQ: {
danielk1977a37cdde2004-05-16 11:15:36 +00001174 sqlite3ExprCode(pParse, pExpr->pLeft);
1175 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001176 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 0, 0);
danielk1977a37cdde2004-05-16 11:15:36 +00001177 break;
drhc9b84a12002-06-20 11:36:48 +00001178 }
drhcce7d172000-05-31 15:34:51 +00001179 case TK_AND:
1180 case TK_OR:
1181 case TK_PLUS:
1182 case TK_STAR:
1183 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00001184 case TK_REM:
1185 case TK_BITAND:
1186 case TK_BITOR:
drh17c40292004-07-21 02:53:29 +00001187 case TK_SLASH:
drhbf4133c2001-10-13 02:59:08 +00001188 case TK_LSHIFT:
1189 case TK_RSHIFT: {
danielk19774adee202004-05-08 08:23:19 +00001190 sqlite3ExprCode(pParse, pExpr->pLeft);
drh17c40292004-07-21 02:53:29 +00001191 sqlite3ExprCode(pParse, pExpr->pRight);
danielk19774adee202004-05-08 08:23:19 +00001192 sqlite3VdbeAddOp(v, op, 0, 0);
drhbf4133c2001-10-13 02:59:08 +00001193 break;
1194 }
drh00400772000-06-16 20:51:26 +00001195 case TK_CONCAT: {
danielk19774adee202004-05-08 08:23:19 +00001196 sqlite3ExprCode(pParse, pExpr->pLeft);
1197 sqlite3ExprCode(pParse, pExpr->pRight);
danielk197772c952a2004-06-21 09:06:41 +00001198 sqlite3VdbeAddOp(v, OP_Concat8, 2, 0);
drh00400772000-06-16 20:51:26 +00001199 break;
1200 }
drhcce7d172000-05-31 15:34:51 +00001201 case TK_UMINUS: {
drhfec19aa2004-05-19 20:41:03 +00001202 Expr *pLeft = pExpr->pLeft;
1203 assert( pLeft );
1204 if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
1205 Token *p = &pLeft->token;
drh6e142f52000-06-08 13:36:40 +00001206 char *z = sqliteMalloc( p->n + 2 );
1207 sprintf(z, "-%.*s", p->n, p->z);
drhfec19aa2004-05-19 20:41:03 +00001208 if( pLeft->op==TK_FLOAT ){
1209 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001210 }else{
drhfec19aa2004-05-19 20:41:03 +00001211 codeInteger(v, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001212 }
drh6e142f52000-06-08 13:36:40 +00001213 sqliteFree(z);
1214 break;
1215 }
drh1ccde152000-06-17 13:12:39 +00001216 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +00001217 }
drhbf4133c2001-10-13 02:59:08 +00001218 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00001219 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001220 sqlite3ExprCode(pParse, pExpr->pLeft);
1221 sqlite3VdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001222 break;
1223 }
1224 case TK_ISNULL:
1225 case TK_NOTNULL: {
1226 int dest;
danielk19774adee202004-05-08 08:23:19 +00001227 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1228 sqlite3ExprCode(pParse, pExpr->pLeft);
1229 dest = sqlite3VdbeCurrentAddr(v) + 2;
1230 sqlite3VdbeAddOp(v, op, 1, dest);
1231 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);
drhcce7d172000-05-31 15:34:51 +00001232 }
danielk1977a37cdde2004-05-16 11:15:36 +00001233 break;
drh22827922000-06-06 17:27:05 +00001234 case TK_AGG_FUNCTION: {
danielk19774adee202004-05-08 08:23:19 +00001235 sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drh22827922000-06-06 17:27:05 +00001236 break;
1237 }
drh4b59ab52002-08-24 18:24:51 +00001238 case TK_GLOB:
1239 case TK_LIKE:
drhcce7d172000-05-31 15:34:51 +00001240 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00001241 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00001242 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00001243 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00001244 int nId;
1245 const char *zId;
danielk1977682f68b2004-06-05 10:22:17 +00001246 int p2 = 0;
1247 int i;
danielk1977d8123362004-06-12 09:25:12 +00001248 u8 enc = pParse->db->enc;
danielk1977dc1bdc42004-06-11 10:51:27 +00001249 CollSeq *pColl = 0;
drh4b59ab52002-08-24 18:24:51 +00001250 getFunctionName(pExpr, &zId, &nId);
danielk1977d8123362004-06-12 09:25:12 +00001251 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001252 assert( pDef!=0 );
drhf9b596e2004-05-26 16:54:42 +00001253 nExpr = sqlite3ExprCodeExprList(pParse, pList);
danielk1977682f68b2004-06-05 10:22:17 +00001254 for(i=0; i<nExpr && i<32; i++){
danielk1977d02eb1f2004-06-06 09:44:03 +00001255 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
1256 p2 |= (1<<i);
1257 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001258 if( pDef->needCollSeq && !pColl ){
1259 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
1260 }
1261 }
1262 if( pDef->needCollSeq ){
1263 if( !pColl ) pColl = pParse->db->pDfltColl;
danielk1977d8123362004-06-12 09:25:12 +00001264 sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ);
danielk1977682f68b2004-06-05 10:22:17 +00001265 }
1266 sqlite3VdbeOp3(v, OP_Function, nExpr, p2, (char*)pDef, P3_FUNCDEF);
drhcce7d172000-05-31 15:34:51 +00001267 break;
1268 }
drh19a775c2000-06-05 18:54:46 +00001269 case TK_SELECT: {
danielk19774adee202004-05-08 08:23:19 +00001270 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drh19a775c2000-06-05 18:54:46 +00001271 break;
1272 }
drhfef52082000-06-06 01:50:43 +00001273 case TK_IN: {
1274 int addr;
danielk1977e014a832004-05-17 10:48:57 +00001275 char const *affStr;
1276
1277 /* Figure out the affinity to use to create a key from the results
1278 ** of the expression. affinityStr stores a static string suitable for
danielk1977ededfd52004-06-17 07:53:01 +00001279 ** P3 of OP_MakeRecord.
danielk1977e014a832004-05-17 10:48:57 +00001280 */
1281 affStr = sqlite3AffinityString(comparisonAffinity(pExpr));
1282
danielk19774adee202004-05-08 08:23:19 +00001283 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
danielk1977e014a832004-05-17 10:48:57 +00001284
1285 /* Code the <expr> from "<expr> IN (...)". The temporary table
1286 ** pExpr->iTable contains the values that make up the (...) set.
1287 */
danielk19774adee202004-05-08 08:23:19 +00001288 sqlite3ExprCode(pParse, pExpr->pLeft);
1289 addr = sqlite3VdbeCurrentAddr(v);
danielk1977e014a832004-05-17 10:48:57 +00001290 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); /* addr + 0 */
danielk19774adee202004-05-08 08:23:19 +00001291 sqlite3VdbeAddOp(v, OP_Pop, 2, 0);
danielk19770f69c1e2004-05-29 11:24:50 +00001292 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +00001293 sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7);
danielk1977ededfd52004-06-17 07:53:01 +00001294 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, affStr, P3_STATIC); /* addr + 4 */
danielk1977e014a832004-05-17 10:48:57 +00001295 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7);
1296 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); /* addr + 6 */
1297
drhfef52082000-06-06 01:50:43 +00001298 break;
1299 }
1300 case TK_BETWEEN: {
drhbe5c89a2004-07-26 00:31:09 +00001301 Expr *pLeft = pExpr->pLeft;
1302 struct ExprList_item *pLItem = pExpr->pList->a;
1303 Expr *pRight = pLItem->pExpr;
1304 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001305 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001306 sqlite3ExprCode(pParse, pRight);
1307 codeCompare(pParse, pLeft, pRight, OP_Ge, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001308 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
drhbe5c89a2004-07-26 00:31:09 +00001309 pLItem++;
1310 pRight = pLItem->pExpr;
1311 sqlite3ExprCode(pParse, pRight);
1312 codeCompare(pParse, pLeft, pRight, OP_Le, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001313 sqlite3VdbeAddOp(v, OP_And, 0, 0);
drhfef52082000-06-06 01:50:43 +00001314 break;
1315 }
drh51e9a442004-01-16 16:42:53 +00001316 case TK_UPLUS:
drha2e00042002-01-22 03:13:42 +00001317 case TK_AS: {
danielk19774adee202004-05-08 08:23:19 +00001318 sqlite3ExprCode(pParse, pExpr->pLeft);
drha2e00042002-01-22 03:13:42 +00001319 break;
1320 }
drh17a7f8d2002-03-24 13:13:27 +00001321 case TK_CASE: {
1322 int expr_end_label;
drhf5905aa2002-05-26 20:54:33 +00001323 int jumpInst;
1324 int addr;
1325 int nExpr;
drh17a7f8d2002-03-24 13:13:27 +00001326 int i;
drhbe5c89a2004-07-26 00:31:09 +00001327 ExprList *pEList;
1328 struct ExprList_item *aListelem;
drh17a7f8d2002-03-24 13:13:27 +00001329
1330 assert(pExpr->pList);
1331 assert((pExpr->pList->nExpr % 2) == 0);
1332 assert(pExpr->pList->nExpr > 0);
drhbe5c89a2004-07-26 00:31:09 +00001333 pEList = pExpr->pList;
1334 aListelem = pEList->a;
1335 nExpr = pEList->nExpr;
danielk19774adee202004-05-08 08:23:19 +00001336 expr_end_label = sqlite3VdbeMakeLabel(v);
drh17a7f8d2002-03-24 13:13:27 +00001337 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001338 sqlite3ExprCode(pParse, pExpr->pLeft);
drh17a7f8d2002-03-24 13:13:27 +00001339 }
drhf5905aa2002-05-26 20:54:33 +00001340 for(i=0; i<nExpr; i=i+2){
drhbe5c89a2004-07-26 00:31:09 +00001341 sqlite3ExprCode(pParse, aListelem[i].pExpr);
drh17a7f8d2002-03-24 13:13:27 +00001342 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001343 sqlite3VdbeAddOp(v, OP_Dup, 1, 1);
drhbe5c89a2004-07-26 00:31:09 +00001344 jumpInst = codeCompare(pParse, pExpr->pLeft, aListelem[i].pExpr,
1345 OP_Ne, 0, 1);
danielk19774adee202004-05-08 08:23:19 +00001346 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001347 }else{
danielk19774adee202004-05-08 08:23:19 +00001348 jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001349 }
drhbe5c89a2004-07-26 00:31:09 +00001350 sqlite3ExprCode(pParse, aListelem[i+1].pExpr);
danielk19774adee202004-05-08 08:23:19 +00001351 sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label);
1352 addr = sqlite3VdbeCurrentAddr(v);
1353 sqlite3VdbeChangeP2(v, jumpInst, addr);
drh17a7f8d2002-03-24 13:13:27 +00001354 }
drhf570f012002-05-31 15:51:25 +00001355 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001356 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhf570f012002-05-31 15:51:25 +00001357 }
drh17a7f8d2002-03-24 13:13:27 +00001358 if( pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001359 sqlite3ExprCode(pParse, pExpr->pRight);
drh17a7f8d2002-03-24 13:13:27 +00001360 }else{
danielk19770f69c1e2004-05-29 11:24:50 +00001361 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
drh17a7f8d2002-03-24 13:13:27 +00001362 }
danielk19774adee202004-05-08 08:23:19 +00001363 sqlite3VdbeResolveLabel(v, expr_end_label);
danielk19776f349032002-06-11 02:25:40 +00001364 break;
1365 }
1366 case TK_RAISE: {
1367 if( !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +00001368 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00001369 "RAISE() may only be used within a trigger-program");
danielk19776f349032002-06-11 02:25:40 +00001370 pParse->nErr++;
1371 return;
1372 }
1373 if( pExpr->iColumn == OE_Rollback ||
1374 pExpr->iColumn == OE_Abort ||
1375 pExpr->iColumn == OE_Fail ){
danielk19774adee202004-05-08 08:23:19 +00001376 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn,
drh701a0ae2004-02-22 20:05:00 +00001377 pExpr->token.z, pExpr->token.n);
danielk19774adee202004-05-08 08:23:19 +00001378 sqlite3VdbeDequoteP3(v, -1);
danielk19776f349032002-06-11 02:25:40 +00001379 } else {
1380 assert( pExpr->iColumn == OE_Ignore );
danielk19774adee202004-05-08 08:23:19 +00001381 sqlite3VdbeOp3(v, OP_Goto, 0, pParse->trigStack->ignoreJump,
drh701a0ae2004-02-22 20:05:00 +00001382 "(IGNORE jump)", 0);
danielk19776f349032002-06-11 02:25:40 +00001383 }
drh17a7f8d2002-03-24 13:13:27 +00001384 }
1385 break;
drhcce7d172000-05-31 15:34:51 +00001386 }
drhcce7d172000-05-31 15:34:51 +00001387}
1388
1389/*
drh268380c2004-02-25 13:47:31 +00001390** Generate code that pushes the value of every element of the given
drhf9b596e2004-05-26 16:54:42 +00001391** expression list onto the stack.
drh268380c2004-02-25 13:47:31 +00001392**
1393** Return the number of elements pushed onto the stack.
1394*/
danielk19774adee202004-05-08 08:23:19 +00001395int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00001396 Parse *pParse, /* Parsing context */
drhf9b596e2004-05-26 16:54:42 +00001397 ExprList *pList /* The expression list to be coded */
drh268380c2004-02-25 13:47:31 +00001398){
1399 struct ExprList_item *pItem;
1400 int i, n;
1401 Vdbe *v;
1402 if( pList==0 ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001403 v = sqlite3GetVdbe(pParse);
drh268380c2004-02-25 13:47:31 +00001404 n = pList->nExpr;
1405 for(pItem=pList->a, i=0; i<n; i++, pItem++){
danielk19774adee202004-05-08 08:23:19 +00001406 sqlite3ExprCode(pParse, pItem->pExpr);
drh268380c2004-02-25 13:47:31 +00001407 }
drhf9b596e2004-05-26 16:54:42 +00001408 return n;
drh268380c2004-02-25 13:47:31 +00001409}
1410
1411/*
drhcce7d172000-05-31 15:34:51 +00001412** Generate code for a boolean expression such that a jump is made
1413** to the label "dest" if the expression is true but execution
1414** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00001415**
1416** If the expression evaluates to NULL (neither true nor false), then
1417** take the jump if the jumpIfNull flag is true.
drhcce7d172000-05-31 15:34:51 +00001418*/
danielk19774adee202004-05-08 08:23:19 +00001419void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001420 Vdbe *v = pParse->pVdbe;
1421 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001422 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001423 switch( pExpr->op ){
1424 case TK_LT: op = OP_Lt; break;
1425 case TK_LE: op = OP_Le; break;
1426 case TK_GT: op = OP_Gt; break;
1427 case TK_GE: op = OP_Ge; break;
1428 case TK_NE: op = OP_Ne; break;
1429 case TK_EQ: op = OP_Eq; break;
drhcce7d172000-05-31 15:34:51 +00001430 case TK_ISNULL: op = OP_IsNull; break;
1431 case TK_NOTNULL: op = OP_NotNull; break;
1432 default: break;
1433 }
1434 switch( pExpr->op ){
1435 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001436 int d2 = sqlite3VdbeMakeLabel(v);
1437 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
1438 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
1439 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001440 break;
1441 }
1442 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001443 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
1444 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001445 break;
1446 }
1447 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001448 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001449 break;
1450 }
1451 case TK_LT:
1452 case TK_LE:
1453 case TK_GT:
1454 case TK_GE:
1455 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00001456 case TK_EQ: {
danielk19774adee202004-05-08 08:23:19 +00001457 sqlite3ExprCode(pParse, pExpr->pLeft);
1458 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001459 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001460 break;
1461 }
1462 case TK_ISNULL:
1463 case TK_NOTNULL: {
danielk19774adee202004-05-08 08:23:19 +00001464 sqlite3ExprCode(pParse, pExpr->pLeft);
1465 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001466 break;
1467 }
drhfef52082000-06-06 01:50:43 +00001468 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00001469 /* The expression "x BETWEEN y AND z" is implemented as:
1470 **
1471 ** 1 IF (x < y) GOTO 3
1472 ** 2 IF (x <= z) GOTO <dest>
1473 ** 3 ...
1474 */
drhf5905aa2002-05-26 20:54:33 +00001475 int addr;
drhbe5c89a2004-07-26 00:31:09 +00001476 Expr *pLeft = pExpr->pLeft;
1477 Expr *pRight = pExpr->pList->a[0].pExpr;
1478 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001479 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001480 sqlite3ExprCode(pParse, pRight);
1481 addr = codeCompare(pParse, pLeft, pRight, OP_Lt, 0, !jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00001482
drhbe5c89a2004-07-26 00:31:09 +00001483 pRight = pExpr->pList->a[1].pExpr;
1484 sqlite3ExprCode(pParse, pRight);
1485 codeCompare(pParse, pLeft, pRight, OP_Le, dest, jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00001486
danielk19774adee202004-05-08 08:23:19 +00001487 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
1488 sqlite3VdbeChangeP2(v, addr, sqlite3VdbeCurrentAddr(v));
1489 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00001490 break;
1491 }
drhcce7d172000-05-31 15:34:51 +00001492 default: {
danielk19774adee202004-05-08 08:23:19 +00001493 sqlite3ExprCode(pParse, pExpr);
1494 sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001495 break;
1496 }
1497 }
1498}
1499
1500/*
drh66b89c82000-11-28 20:47:17 +00001501** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00001502** to the label "dest" if the expression is false but execution
1503** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00001504**
1505** If the expression evaluates to NULL (neither true nor false) then
1506** jump if jumpIfNull is true or fall through if jumpIfNull is false.
drhcce7d172000-05-31 15:34:51 +00001507*/
danielk19774adee202004-05-08 08:23:19 +00001508void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001509 Vdbe *v = pParse->pVdbe;
1510 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001511 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001512 switch( pExpr->op ){
1513 case TK_LT: op = OP_Ge; break;
1514 case TK_LE: op = OP_Gt; break;
1515 case TK_GT: op = OP_Le; break;
1516 case TK_GE: op = OP_Lt; break;
1517 case TK_NE: op = OP_Eq; break;
1518 case TK_EQ: op = OP_Ne; break;
drhcce7d172000-05-31 15:34:51 +00001519 case TK_ISNULL: op = OP_NotNull; break;
1520 case TK_NOTNULL: op = OP_IsNull; break;
1521 default: break;
1522 }
1523 switch( pExpr->op ){
1524 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001525 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
1526 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001527 break;
1528 }
1529 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001530 int d2 = sqlite3VdbeMakeLabel(v);
1531 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
1532 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
1533 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001534 break;
1535 }
1536 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001537 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001538 break;
1539 }
1540 case TK_LT:
1541 case TK_LE:
1542 case TK_GT:
1543 case TK_GE:
1544 case TK_NE:
1545 case TK_EQ: {
danielk19774adee202004-05-08 08:23:19 +00001546 sqlite3ExprCode(pParse, pExpr->pLeft);
1547 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001548 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001549 break;
1550 }
drhcce7d172000-05-31 15:34:51 +00001551 case TK_ISNULL:
1552 case TK_NOTNULL: {
danielk19774adee202004-05-08 08:23:19 +00001553 sqlite3ExprCode(pParse, pExpr->pLeft);
1554 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001555 break;
1556 }
drhfef52082000-06-06 01:50:43 +00001557 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00001558 /* The expression is "x BETWEEN y AND z". It is implemented as:
1559 **
1560 ** 1 IF (x >= y) GOTO 3
1561 ** 2 GOTO <dest>
1562 ** 3 IF (x > z) GOTO <dest>
1563 */
drhfef52082000-06-06 01:50:43 +00001564 int addr;
drhbe5c89a2004-07-26 00:31:09 +00001565 Expr *pLeft = pExpr->pLeft;
1566 Expr *pRight = pExpr->pList->a[0].pExpr;
1567 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001568 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001569 sqlite3ExprCode(pParse, pRight);
danielk19774adee202004-05-08 08:23:19 +00001570 addr = sqlite3VdbeCurrentAddr(v);
drhbe5c89a2004-07-26 00:31:09 +00001571 codeCompare(pParse, pLeft, pRight, OP_Ge, addr+3, !jumpIfNull);
1572
danielk19774adee202004-05-08 08:23:19 +00001573 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
1574 sqlite3VdbeAddOp(v, OP_Goto, 0, dest);
drhbe5c89a2004-07-26 00:31:09 +00001575 pRight = pExpr->pList->a[1].pExpr;
1576 sqlite3ExprCode(pParse, pRight);
1577 codeCompare(pParse, pLeft, pRight, OP_Gt, dest, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00001578 break;
1579 }
drhcce7d172000-05-31 15:34:51 +00001580 default: {
danielk19774adee202004-05-08 08:23:19 +00001581 sqlite3ExprCode(pParse, pExpr);
1582 sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001583 break;
1584 }
1585 }
1586}
drh22827922000-06-06 17:27:05 +00001587
1588/*
1589** Do a deep comparison of two expression trees. Return TRUE (non-zero)
1590** if they are identical and return FALSE if they differ in any way.
1591*/
danielk19774adee202004-05-08 08:23:19 +00001592int sqlite3ExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00001593 int i;
1594 if( pA==0 ){
1595 return pB==0;
1596 }else if( pB==0 ){
1597 return 0;
1598 }
1599 if( pA->op!=pB->op ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001600 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
1601 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00001602 if( pA->pList ){
1603 if( pB->pList==0 ) return 0;
1604 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
1605 for(i=0; i<pA->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00001606 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00001607 return 0;
1608 }
1609 }
1610 }else if( pB->pList ){
1611 return 0;
1612 }
1613 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00001614 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drh22827922000-06-06 17:27:05 +00001615 if( pA->token.z ){
1616 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00001617 if( pB->token.n!=pA->token.n ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001618 if( sqlite3StrNICmp(pA->token.z, pB->token.z, pB->token.n)!=0 ) return 0;
drh22827922000-06-06 17:27:05 +00001619 }
1620 return 1;
1621}
1622
1623/*
1624** Add a new element to the pParse->aAgg[] array and return its index.
1625*/
1626static int appendAggInfo(Parse *pParse){
1627 if( (pParse->nAgg & 0x7)==0 ){
1628 int amt = pParse->nAgg + 8;
drh6d4abfb2001-10-22 02:58:08 +00001629 AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0]));
1630 if( aAgg==0 ){
drh22827922000-06-06 17:27:05 +00001631 return -1;
1632 }
drh6d4abfb2001-10-22 02:58:08 +00001633 pParse->aAgg = aAgg;
drh22827922000-06-06 17:27:05 +00001634 }
1635 memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0]));
1636 return pParse->nAgg++;
1637}
1638
1639/*
1640** Analyze the given expression looking for aggregate functions and
1641** for variables that need to be added to the pParse->aAgg[] array.
1642** Make additional entries to the pParse->aAgg[] array as necessary.
1643**
1644** This routine should only be called after the expression has been
danielk19774adee202004-05-08 08:23:19 +00001645** analyzed by sqlite3ExprResolveIds() and sqlite3ExprCheck().
drh22827922000-06-06 17:27:05 +00001646**
1647** If errors are seen, leave an error message in zErrMsg and return
1648** the number of errors.
1649*/
danielk19774adee202004-05-08 08:23:19 +00001650int sqlite3ExprAnalyzeAggregates(Parse *pParse, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00001651 int i;
1652 AggExpr *aAgg;
1653 int nErr = 0;
1654
1655 if( pExpr==0 ) return 0;
1656 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00001657 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +00001658 aAgg = pParse->aAgg;
1659 for(i=0; i<pParse->nAgg; i++){
1660 if( aAgg[i].isAgg ) continue;
1661 if( aAgg[i].pExpr->iTable==pExpr->iTable
drh967e8b72000-06-21 13:59:10 +00001662 && aAgg[i].pExpr->iColumn==pExpr->iColumn ){
drh22827922000-06-06 17:27:05 +00001663 break;
1664 }
1665 }
1666 if( i>=pParse->nAgg ){
1667 i = appendAggInfo(pParse);
1668 if( i<0 ) return 1;
1669 pParse->aAgg[i].isAgg = 0;
1670 pParse->aAgg[i].pExpr = pExpr;
1671 }
drhaaf88722000-06-08 11:25:00 +00001672 pExpr->iAgg = i;
drh22827922000-06-06 17:27:05 +00001673 break;
1674 }
1675 case TK_AGG_FUNCTION: {
drh22827922000-06-06 17:27:05 +00001676 aAgg = pParse->aAgg;
1677 for(i=0; i<pParse->nAgg; i++){
1678 if( !aAgg[i].isAgg ) continue;
danielk19774adee202004-05-08 08:23:19 +00001679 if( sqlite3ExprCompare(aAgg[i].pExpr, pExpr) ){
drh22827922000-06-06 17:27:05 +00001680 break;
1681 }
1682 }
1683 if( i>=pParse->nAgg ){
danielk1977d8123362004-06-12 09:25:12 +00001684 u8 enc = pParse->db->enc;
drh22827922000-06-06 17:27:05 +00001685 i = appendAggInfo(pParse);
1686 if( i<0 ) return 1;
1687 pParse->aAgg[i].isAgg = 1;
1688 pParse->aAgg[i].pExpr = pExpr;
danielk19774adee202004-05-08 08:23:19 +00001689 pParse->aAgg[i].pFunc = sqlite3FindFunction(pParse->db,
drh6977fea2002-10-22 23:38:04 +00001690 pExpr->token.z, pExpr->token.n,
danielk1977d8123362004-06-12 09:25:12 +00001691 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
drh22827922000-06-06 17:27:05 +00001692 }
1693 pExpr->iAgg = i;
1694 break;
1695 }
1696 default: {
1697 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001698 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pLeft);
drh22827922000-06-06 17:27:05 +00001699 }
1700 if( nErr==0 && pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001701 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pRight);
drh22827922000-06-06 17:27:05 +00001702 }
1703 if( nErr==0 && pExpr->pList ){
1704 int n = pExpr->pList->nExpr;
1705 int i;
1706 for(i=0; nErr==0 && i<n; i++){
danielk19774adee202004-05-08 08:23:19 +00001707 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pList->a[i].pExpr);
drh22827922000-06-06 17:27:05 +00001708 }
1709 }
1710 break;
1711 }
1712 }
1713 return nErr;
1714}
drh8e0a2f92002-02-23 23:45:45 +00001715
1716/*
danielk1977d02eb1f2004-06-06 09:44:03 +00001717** Locate a user function given a name, a number of arguments and a flag
1718** indicating whether the function prefers UTF-16 over UTF-8. Return a
1719** pointer to the FuncDef structure that defines that function, or return
1720** NULL if the function does not exist.
drh8e0a2f92002-02-23 23:45:45 +00001721**
drh0bce8352002-02-28 00:41:10 +00001722** If the createFlag argument is true, then a new (blank) FuncDef
drh8e0a2f92002-02-23 23:45:45 +00001723** structure is created and liked into the "db" structure if a
1724** no matching function previously existed. When createFlag is true
1725** and the nArg parameter is -1, then only a function that accepts
1726** any number of arguments will be returned.
1727**
1728** If createFlag is false and nArg is -1, then the first valid
1729** function found is returned. A function is valid if either xFunc
1730** or xStep is non-zero.
danielk1977d02eb1f2004-06-06 09:44:03 +00001731**
1732** If createFlag is false, then a function with the required name and
1733** number of arguments may be returned even if the eTextRep flag does not
1734** match that requested.
drh8e0a2f92002-02-23 23:45:45 +00001735*/
danielk19774adee202004-05-08 08:23:19 +00001736FuncDef *sqlite3FindFunction(
drh8e0a2f92002-02-23 23:45:45 +00001737 sqlite *db, /* An open database */
1738 const char *zName, /* Name of the function. Not null-terminated */
1739 int nName, /* Number of characters in the name */
1740 int nArg, /* Number of arguments. -1 means any number */
danielk1977d8123362004-06-12 09:25:12 +00001741 u8 enc, /* Preferred text encoding */
drh8e0a2f92002-02-23 23:45:45 +00001742 int createFlag /* Create new entry if true and does not otherwise exist */
1743){
danielk1977d02eb1f2004-06-06 09:44:03 +00001744 FuncDef *p; /* Iterator variable */
1745 FuncDef *pFirst; /* First function with this name */
1746 FuncDef *pBest = 0; /* Best match found so far */
danielk1977d8123362004-06-12 09:25:12 +00001747 int bestmatch = 0;
danielk1977d02eb1f2004-06-06 09:44:03 +00001748
danielk1977d8123362004-06-12 09:25:12 +00001749
1750 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
danielk1977d02eb1f2004-06-06 09:44:03 +00001751 if( nArg<-1 ) nArg = -1;
1752
1753 pFirst = (FuncDef*)sqlite3HashFind(&db->aFunc, zName, nName);
1754 for(p=pFirst; p; p=p->pNext){
danielk1977d8123362004-06-12 09:25:12 +00001755 /* During the search for the best function definition, bestmatch is set
1756 ** as follows to indicate the quality of the match with the definition
1757 ** pointed to by pBest:
1758 **
1759 ** 0: pBest is NULL. No match has been found.
1760 ** 1: A variable arguments function that prefers UTF-8 when a UTF-16
1761 ** encoding is requested, or vice versa.
1762 ** 2: A variable arguments function that uses UTF-16BE when UTF-16LE is
1763 ** requested, or vice versa.
1764 ** 3: A variable arguments function using the same text encoding.
1765 ** 4: A function with the exact number of arguments requested that
1766 ** prefers UTF-8 when a UTF-16 encoding is requested, or vice versa.
1767 ** 5: A function with the exact number of arguments requested that
1768 ** prefers UTF-16LE when UTF-16BE is requested, or vice versa.
1769 ** 6: An exact match.
1770 **
1771 ** A larger value of 'matchqual' indicates a more desirable match.
1772 */
danielk1977e12c17b2004-06-23 12:35:14 +00001773 if( p->nArg==-1 || p->nArg==nArg || nArg==-1 ){
danielk1977d8123362004-06-12 09:25:12 +00001774 int match = 1; /* Quality of this match */
1775 if( p->nArg==nArg || nArg==-1 ){
1776 match = 4;
danielk1977d02eb1f2004-06-06 09:44:03 +00001777 }
danielk1977d8123362004-06-12 09:25:12 +00001778 if( enc==p->iPrefEnc ){
1779 match += 2;
danielk1977d02eb1f2004-06-06 09:44:03 +00001780 }
danielk1977d8123362004-06-12 09:25:12 +00001781 else if( (enc==SQLITE_UTF16LE && p->iPrefEnc==SQLITE_UTF16BE) ||
1782 (enc==SQLITE_UTF16BE && p->iPrefEnc==SQLITE_UTF16LE) ){
1783 match += 1;
1784 }
1785
1786 if( match>bestmatch ){
1787 pBest = p;
1788 bestmatch = match;
danielk1977d02eb1f2004-06-06 09:44:03 +00001789 }
1790 }
drh8e0a2f92002-02-23 23:45:45 +00001791 }
danielk1977d02eb1f2004-06-06 09:44:03 +00001792
danielk1977d8123362004-06-12 09:25:12 +00001793 /* If the createFlag parameter is true, and the seach did not reveal an
1794 ** exact match for the name, number of arguments and encoding, then add a
1795 ** new entry to the hash table and return it.
1796 */
1797 if( createFlag && bestmatch<6 &&
danielk1977d02eb1f2004-06-06 09:44:03 +00001798 (pBest = sqliteMalloc(sizeof(*pBest)+nName+1)) ){
1799 pBest->nArg = nArg;
1800 pBest->pNext = pFirst;
1801 pBest->zName = (char*)&pBest[1];
danielk1977d8123362004-06-12 09:25:12 +00001802 pBest->iPrefEnc = enc;
danielk1977d02eb1f2004-06-06 09:44:03 +00001803 memcpy(pBest->zName, zName, nName);
1804 pBest->zName[nName] = 0;
1805 sqlite3HashInsert(&db->aFunc, pBest->zName, nName, (void*)pBest);
drh8e0a2f92002-02-23 23:45:45 +00001806 }
danielk1977d02eb1f2004-06-06 09:44:03 +00001807
1808 if( pBest && (pBest->xStep || pBest->xFunc || createFlag) ){
1809 return pBest;
drh8e0a2f92002-02-23 23:45:45 +00001810 }
danielk1977d02eb1f2004-06-06 09:44:03 +00001811 return 0;
drh8e0a2f92002-02-23 23:45:45 +00001812}