blob: a777874803e96fe851d78acf6a10804b015f7313 [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**
drh71c697e2004-08-08 23:39:19 +000015** $Id: expr.c,v 1.155 2004/08/08 23:39:19 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 );
drh71c697e2004-08-08 23:39:19 +0000229 if( !sqlite3_malloc_failed && pRight->z && pLeft->z ){
drh4b59ab52002-08-24 18:24:51 +0000230 if( pLeft->dyn==0 && pRight->dyn==0 ){
drh6977fea2002-10-22 23:38:04 +0000231 pExpr->span.z = pLeft->z;
232 pExpr->span.n = pRight->n + Addr(pRight->z) - Addr(pLeft->z);
drh4b59ab52002-08-24 18:24:51 +0000233 }else{
drh6977fea2002-10-22 23:38:04 +0000234 pExpr->span.z = 0;
drh4b59ab52002-08-24 18:24:51 +0000235 }
drha76b5df2002-02-23 02:32:10 +0000236 }
237}
238
239/*
240** Construct a new expression node for a function with multiple
241** arguments.
242*/
danielk19774adee202004-05-08 08:23:19 +0000243Expr *sqlite3ExprFunction(ExprList *pList, Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000244 Expr *pNew;
245 pNew = sqliteMalloc( sizeof(Expr) );
246 if( pNew==0 ){
danielk19774adee202004-05-08 08:23:19 +0000247 /* sqlite3ExprListDelete(pList); // Leak pList when malloc fails */
drha76b5df2002-02-23 02:32:10 +0000248 return 0;
249 }
250 pNew->op = TK_FUNCTION;
251 pNew->pList = pList;
252 if( pToken ){
drh4b59ab52002-08-24 18:24:51 +0000253 assert( pToken->dyn==0 );
drha76b5df2002-02-23 02:32:10 +0000254 pNew->token = *pToken;
255 }else{
256 pNew->token.z = 0;
drha76b5df2002-02-23 02:32:10 +0000257 }
drh6977fea2002-10-22 23:38:04 +0000258 pNew->span = pNew->token;
drha76b5df2002-02-23 02:32:10 +0000259 return pNew;
260}
261
262/*
drha2e00042002-01-22 03:13:42 +0000263** Recursively delete an expression tree.
264*/
danielk19774adee202004-05-08 08:23:19 +0000265void sqlite3ExprDelete(Expr *p){
drha2e00042002-01-22 03:13:42 +0000266 if( p==0 ) return;
drh4efc4752004-01-16 15:55:37 +0000267 if( p->span.dyn ) sqliteFree((char*)p->span.z);
268 if( p->token.dyn ) sqliteFree((char*)p->token.z);
danielk19774adee202004-05-08 08:23:19 +0000269 sqlite3ExprDelete(p->pLeft);
270 sqlite3ExprDelete(p->pRight);
271 sqlite3ExprListDelete(p->pList);
272 sqlite3SelectDelete(p->pSelect);
drha2e00042002-01-22 03:13:42 +0000273 sqliteFree(p);
274}
275
drha76b5df2002-02-23 02:32:10 +0000276
277/*
drhff78bd22002-02-27 01:47:11 +0000278** The following group of routines make deep copies of expressions,
279** expression lists, ID lists, and select statements. The copies can
280** be deleted (by being passed to their respective ...Delete() routines)
281** without effecting the originals.
282**
danielk19774adee202004-05-08 08:23:19 +0000283** The expression list, ID, and source lists return by sqlite3ExprListDup(),
284** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
drhad3cab52002-05-24 02:04:32 +0000285** by subsequent calls to sqlite*ListAppend() routines.
drhff78bd22002-02-27 01:47:11 +0000286**
drhad3cab52002-05-24 02:04:32 +0000287** Any tables that the SrcList might point to are not duplicated.
drhff78bd22002-02-27 01:47:11 +0000288*/
danielk19774adee202004-05-08 08:23:19 +0000289Expr *sqlite3ExprDup(Expr *p){
drhff78bd22002-02-27 01:47:11 +0000290 Expr *pNew;
291 if( p==0 ) return 0;
drhfcb78a42003-01-18 20:11:05 +0000292 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000293 if( pNew==0 ) return 0;
drh3b167c72002-06-28 12:18:47 +0000294 memcpy(pNew, p, sizeof(*pNew));
drh6977fea2002-10-22 23:38:04 +0000295 if( p->token.z!=0 ){
drh4b59ab52002-08-24 18:24:51 +0000296 pNew->token.z = sqliteStrDup(p->token.z);
297 pNew->token.dyn = 1;
298 }else{
drh4efc4752004-01-16 15:55:37 +0000299 assert( pNew->token.z==0 );
drh4b59ab52002-08-24 18:24:51 +0000300 }
drh6977fea2002-10-22 23:38:04 +0000301 pNew->span.z = 0;
danielk19774adee202004-05-08 08:23:19 +0000302 pNew->pLeft = sqlite3ExprDup(p->pLeft);
303 pNew->pRight = sqlite3ExprDup(p->pRight);
304 pNew->pList = sqlite3ExprListDup(p->pList);
305 pNew->pSelect = sqlite3SelectDup(p->pSelect);
drhff78bd22002-02-27 01:47:11 +0000306 return pNew;
307}
danielk19774adee202004-05-08 08:23:19 +0000308void sqlite3TokenCopy(Token *pTo, Token *pFrom){
drh4b59ab52002-08-24 18:24:51 +0000309 if( pTo->dyn ) sqliteFree((char*)pTo->z);
drh4b59ab52002-08-24 18:24:51 +0000310 if( pFrom->z ){
311 pTo->n = pFrom->n;
312 pTo->z = sqliteStrNDup(pFrom->z, pFrom->n);
313 pTo->dyn = 1;
314 }else{
drh4b59ab52002-08-24 18:24:51 +0000315 pTo->z = 0;
drh4b59ab52002-08-24 18:24:51 +0000316 }
317}
danielk19774adee202004-05-08 08:23:19 +0000318ExprList *sqlite3ExprListDup(ExprList *p){
drhff78bd22002-02-27 01:47:11 +0000319 ExprList *pNew;
drh3e7bc9c2004-02-21 19:17:17 +0000320 struct ExprList_item *pItem;
drhff78bd22002-02-27 01:47:11 +0000321 int i;
322 if( p==0 ) return 0;
323 pNew = sqliteMalloc( sizeof(*pNew) );
324 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000325 pNew->nExpr = pNew->nAlloc = p->nExpr;
drh3e7bc9c2004-02-21 19:17:17 +0000326 pNew->a = pItem = sqliteMalloc( p->nExpr*sizeof(p->a[0]) );
danielk1977e0048402004-06-15 16:51:01 +0000327 if( pItem==0 ){
328 sqliteFree(pNew);
329 return 0;
330 }
drh1bdd9b52004-04-23 17:04:44 +0000331 for(i=0; i<p->nExpr; i++, pItem++){
drh4b59ab52002-08-24 18:24:51 +0000332 Expr *pNewExpr, *pOldExpr;
danielk19774adee202004-05-08 08:23:19 +0000333 pItem->pExpr = pNewExpr = sqlite3ExprDup(pOldExpr = p->a[i].pExpr);
drh6977fea2002-10-22 23:38:04 +0000334 if( pOldExpr->span.z!=0 && pNewExpr ){
335 /* Always make a copy of the span for top-level expressions in the
drh4b59ab52002-08-24 18:24:51 +0000336 ** expression list. The logic in SELECT processing that determines
337 ** the names of columns in the result set needs this information */
danielk19774adee202004-05-08 08:23:19 +0000338 sqlite3TokenCopy(&pNewExpr->span, &pOldExpr->span);
drh4b59ab52002-08-24 18:24:51 +0000339 }
drh1f3e9052002-10-31 00:09:39 +0000340 assert( pNewExpr==0 || pNewExpr->span.z!=0
danielk197724b03fd2004-05-10 10:34:34 +0000341 || pOldExpr->span.z==0 || sqlite3_malloc_failed );
drh3e7bc9c2004-02-21 19:17:17 +0000342 pItem->zName = sqliteStrDup(p->a[i].zName);
343 pItem->sortOrder = p->a[i].sortOrder;
344 pItem->isAgg = p->a[i].isAgg;
345 pItem->done = 0;
drhff78bd22002-02-27 01:47:11 +0000346 }
347 return pNew;
348}
danielk19774adee202004-05-08 08:23:19 +0000349SrcList *sqlite3SrcListDup(SrcList *p){
drhad3cab52002-05-24 02:04:32 +0000350 SrcList *pNew;
351 int i;
drh113088e2003-03-20 01:16:58 +0000352 int nByte;
drhad3cab52002-05-24 02:04:32 +0000353 if( p==0 ) return 0;
drh113088e2003-03-20 01:16:58 +0000354 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
drh4efc4752004-01-16 15:55:37 +0000355 pNew = sqliteMallocRaw( nByte );
drhad3cab52002-05-24 02:04:32 +0000356 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000357 pNew->nSrc = pNew->nAlloc = p->nSrc;
drhad3cab52002-05-24 02:04:32 +0000358 for(i=0; i<p->nSrc; i++){
drh4efc4752004-01-16 15:55:37 +0000359 struct SrcList_item *pNewItem = &pNew->a[i];
360 struct SrcList_item *pOldItem = &p->a[i];
361 pNewItem->zDatabase = sqliteStrDup(pOldItem->zDatabase);
362 pNewItem->zName = sqliteStrDup(pOldItem->zName);
363 pNewItem->zAlias = sqliteStrDup(pOldItem->zAlias);
364 pNewItem->jointype = pOldItem->jointype;
365 pNewItem->iCursor = pOldItem->iCursor;
366 pNewItem->pTab = 0;
danielk19774adee202004-05-08 08:23:19 +0000367 pNewItem->pSelect = sqlite3SelectDup(pOldItem->pSelect);
368 pNewItem->pOn = sqlite3ExprDup(pOldItem->pOn);
369 pNewItem->pUsing = sqlite3IdListDup(pOldItem->pUsing);
drhad3cab52002-05-24 02:04:32 +0000370 }
371 return pNew;
372}
danielk19774adee202004-05-08 08:23:19 +0000373IdList *sqlite3IdListDup(IdList *p){
drhff78bd22002-02-27 01:47:11 +0000374 IdList *pNew;
375 int i;
376 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000377 pNew = sqliteMallocRaw( sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +0000378 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000379 pNew->nId = pNew->nAlloc = p->nId;
drh4efc4752004-01-16 15:55:37 +0000380 pNew->a = sqliteMallocRaw( p->nId*sizeof(p->a[0]) );
drhe4697f52002-05-23 02:09:03 +0000381 if( pNew->a==0 ) return 0;
drhff78bd22002-02-27 01:47:11 +0000382 for(i=0; i<p->nId; i++){
drh4efc4752004-01-16 15:55:37 +0000383 struct IdList_item *pNewItem = &pNew->a[i];
384 struct IdList_item *pOldItem = &p->a[i];
385 pNewItem->zName = sqliteStrDup(pOldItem->zName);
386 pNewItem->idx = pOldItem->idx;
drhff78bd22002-02-27 01:47:11 +0000387 }
388 return pNew;
389}
danielk19774adee202004-05-08 08:23:19 +0000390Select *sqlite3SelectDup(Select *p){
drhff78bd22002-02-27 01:47:11 +0000391 Select *pNew;
392 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000393 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000394 if( pNew==0 ) return 0;
395 pNew->isDistinct = p->isDistinct;
danielk19774adee202004-05-08 08:23:19 +0000396 pNew->pEList = sqlite3ExprListDup(p->pEList);
397 pNew->pSrc = sqlite3SrcListDup(p->pSrc);
398 pNew->pWhere = sqlite3ExprDup(p->pWhere);
399 pNew->pGroupBy = sqlite3ExprListDup(p->pGroupBy);
400 pNew->pHaving = sqlite3ExprDup(p->pHaving);
401 pNew->pOrderBy = sqlite3ExprListDup(p->pOrderBy);
drhff78bd22002-02-27 01:47:11 +0000402 pNew->op = p->op;
danielk19774adee202004-05-08 08:23:19 +0000403 pNew->pPrior = sqlite3SelectDup(p->pPrior);
drhff78bd22002-02-27 01:47:11 +0000404 pNew->nLimit = p->nLimit;
405 pNew->nOffset = p->nOffset;
406 pNew->zSelect = 0;
drh7b58dae2003-07-20 01:16:46 +0000407 pNew->iLimit = -1;
408 pNew->iOffset = -1;
danielk1977dc1bdc42004-06-11 10:51:27 +0000409 pNew->ppOpenTemp = 0;
drhff78bd22002-02-27 01:47:11 +0000410 return pNew;
411}
412
413
414/*
drha76b5df2002-02-23 02:32:10 +0000415** Add a new element to the end of an expression list. If pList is
416** initially NULL, then create a new expression list.
417*/
danielk19774adee202004-05-08 08:23:19 +0000418ExprList *sqlite3ExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){
drha76b5df2002-02-23 02:32:10 +0000419 if( pList==0 ){
420 pList = sqliteMalloc( sizeof(ExprList) );
421 if( pList==0 ){
danielk19774adee202004-05-08 08:23:19 +0000422 /* sqlite3ExprDelete(pExpr); // Leak memory if malloc fails */
drha76b5df2002-02-23 02:32:10 +0000423 return 0;
424 }
drh4efc4752004-01-16 15:55:37 +0000425 assert( pList->nAlloc==0 );
drha76b5df2002-02-23 02:32:10 +0000426 }
drh4305d102003-07-30 12:34:12 +0000427 if( pList->nAlloc<=pList->nExpr ){
drh4305d102003-07-30 12:34:12 +0000428 pList->nAlloc = pList->nAlloc*2 + 4;
drh4efc4752004-01-16 15:55:37 +0000429 pList->a = sqliteRealloc(pList->a, pList->nAlloc*sizeof(pList->a[0]));
430 if( pList->a==0 ){
danielk19774adee202004-05-08 08:23:19 +0000431 /* sqlite3ExprDelete(pExpr); // Leak memory if malloc fails */
drh4efc4752004-01-16 15:55:37 +0000432 pList->nExpr = pList->nAlloc = 0;
drha76b5df2002-02-23 02:32:10 +0000433 return pList;
434 }
drha76b5df2002-02-23 02:32:10 +0000435 }
drh4efc4752004-01-16 15:55:37 +0000436 assert( pList->a!=0 );
437 if( pExpr || pName ){
438 struct ExprList_item *pItem = &pList->a[pList->nExpr++];
439 memset(pItem, 0, sizeof(*pItem));
440 pItem->pExpr = pExpr;
drha99db3b2004-06-19 14:49:12 +0000441 pItem->zName = sqlite3NameFromToken(pName);
drha76b5df2002-02-23 02:32:10 +0000442 }
443 return pList;
444}
445
446/*
447** Delete an entire expression list.
448*/
danielk19774adee202004-05-08 08:23:19 +0000449void sqlite3ExprListDelete(ExprList *pList){
drha76b5df2002-02-23 02:32:10 +0000450 int i;
drhbe5c89a2004-07-26 00:31:09 +0000451 struct ExprList_item *pItem;
drha76b5df2002-02-23 02:32:10 +0000452 if( pList==0 ) return;
drh1bdd9b52004-04-23 17:04:44 +0000453 assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
454 assert( pList->nExpr<=pList->nAlloc );
drhbe5c89a2004-07-26 00:31:09 +0000455 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
456 sqlite3ExprDelete(pItem->pExpr);
457 sqliteFree(pItem->zName);
drha76b5df2002-02-23 02:32:10 +0000458 }
459 sqliteFree(pList->a);
460 sqliteFree(pList);
461}
462
463/*
drhfef52082000-06-06 01:50:43 +0000464** Walk an expression tree. Return 1 if the expression is constant
465** and 0 if it involves variables.
drh23989372002-05-21 13:43:04 +0000466**
467** For the purposes of this function, a double-quoted string (ex: "abc")
468** is considered a variable but a single-quoted string (ex: 'abc') is
469** a constant.
drhfef52082000-06-06 01:50:43 +0000470*/
danielk19774adee202004-05-08 08:23:19 +0000471int sqlite3ExprIsConstant(Expr *p){
drhfef52082000-06-06 01:50:43 +0000472 switch( p->op ){
473 case TK_ID:
drh967e8b72000-06-21 13:59:10 +0000474 case TK_COLUMN:
drhfef52082000-06-06 01:50:43 +0000475 case TK_DOT:
drh7bdc0c12003-04-19 17:27:24 +0000476 case TK_FUNCTION:
drhfef52082000-06-06 01:50:43 +0000477 return 0;
drh7bdc0c12003-04-19 17:27:24 +0000478 case TK_NULL:
drh23989372002-05-21 13:43:04 +0000479 case TK_STRING:
danielk1977c572ef72004-05-27 09:28:41 +0000480 case TK_BLOB:
drh92086432002-01-22 14:11:29 +0000481 case TK_INTEGER:
482 case TK_FLOAT:
drh50457892003-09-06 01:10:47 +0000483 case TK_VARIABLE:
drh92086432002-01-22 14:11:29 +0000484 return 1;
drhfef52082000-06-06 01:50:43 +0000485 default: {
danielk19774adee202004-05-08 08:23:19 +0000486 if( p->pLeft && !sqlite3ExprIsConstant(p->pLeft) ) return 0;
487 if( p->pRight && !sqlite3ExprIsConstant(p->pRight) ) return 0;
drhfef52082000-06-06 01:50:43 +0000488 if( p->pList ){
489 int i;
490 for(i=0; i<p->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +0000491 if( !sqlite3ExprIsConstant(p->pList->a[i].pExpr) ) return 0;
drhfef52082000-06-06 01:50:43 +0000492 }
493 }
drh92086432002-01-22 14:11:29 +0000494 return p->pLeft!=0 || p->pRight!=0 || (p->pList && p->pList->nExpr>0);
drhfef52082000-06-06 01:50:43 +0000495 }
496 }
drh92086432002-01-22 14:11:29 +0000497 return 0;
drhfef52082000-06-06 01:50:43 +0000498}
499
500/*
drh202b2df2004-01-06 01:13:46 +0000501** If the given expression codes a constant integer that is small enough
502** to fit in a 32-bit integer, return 1 and put the value of the integer
503** in *pValue. If the expression is not an integer or if it is too big
504** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
drhe4de1fe2002-06-02 16:09:01 +0000505*/
danielk19774adee202004-05-08 08:23:19 +0000506int sqlite3ExprIsInteger(Expr *p, int *pValue){
drhe4de1fe2002-06-02 16:09:01 +0000507 switch( p->op ){
508 case TK_INTEGER: {
drhfec19aa2004-05-19 20:41:03 +0000509 if( sqlite3GetInt32(p->token.z, pValue) ){
drh202b2df2004-01-06 01:13:46 +0000510 return 1;
511 }
512 break;
drhe4de1fe2002-06-02 16:09:01 +0000513 }
514 case TK_STRING: {
drh4c755c02004-08-08 20:22:17 +0000515 const u8 *z = (u8*)p->token.z;
drhe4de1fe2002-06-02 16:09:01 +0000516 int n = p->token.n;
drhbd790ee2002-06-02 18:22:06 +0000517 if( n>0 && z[0]=='-' ){ z++; n--; }
drhe4de1fe2002-06-02 16:09:01 +0000518 while( n>0 && *z && isdigit(*z) ){ z++; n--; }
drhfec19aa2004-05-19 20:41:03 +0000519 if( n==0 && sqlite3GetInt32(p->token.z, pValue) ){
drhe4de1fe2002-06-02 16:09:01 +0000520 return 1;
521 }
522 break;
523 }
drh4b59ab52002-08-24 18:24:51 +0000524 case TK_UPLUS: {
danielk19774adee202004-05-08 08:23:19 +0000525 return sqlite3ExprIsInteger(p->pLeft, pValue);
drh4b59ab52002-08-24 18:24:51 +0000526 }
drhe4de1fe2002-06-02 16:09:01 +0000527 case TK_UMINUS: {
528 int v;
danielk19774adee202004-05-08 08:23:19 +0000529 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
drhe4de1fe2002-06-02 16:09:01 +0000530 *pValue = -v;
531 return 1;
532 }
533 break;
534 }
535 default: break;
536 }
537 return 0;
538}
539
540/*
drhc4a3c772001-04-04 11:48:57 +0000541** Return TRUE if the given string is a row-id column name.
542*/
danielk19774adee202004-05-08 08:23:19 +0000543int sqlite3IsRowid(const char *z){
544 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
545 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
546 if( sqlite3StrICmp(z, "OID")==0 ) return 1;
drhc4a3c772001-04-04 11:48:57 +0000547 return 0;
548}
549
550/*
drh8141f612004-01-25 22:44:58 +0000551** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
552** that name in the set of source tables in pSrcList and make the pExpr
553** expression node refer back to that source column. The following changes
554** are made to pExpr:
555**
556** pExpr->iDb Set the index in db->aDb[] of the database holding
557** the table.
558** pExpr->iTable Set to the cursor number for the table obtained
559** from pSrcList.
560** pExpr->iColumn Set to the column number within the table.
drh8141f612004-01-25 22:44:58 +0000561** pExpr->op Set to TK_COLUMN.
562** pExpr->pLeft Any expression this points to is deleted
563** pExpr->pRight Any expression this points to is deleted.
564**
565** The pDbToken is the name of the database (the "X"). This value may be
566** NULL meaning that name is of the form Y.Z or Z. Any available database
567** can be used. The pTableToken is the name of the table (the "Y"). This
568** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it
569** means that the form of the name is Z and that columns from any table
570** can be used.
571**
572** If the name cannot be resolved unambiguously, leave an error message
573** in pParse and return non-zero. Return zero on success.
574*/
575static int lookupName(
576 Parse *pParse, /* The parsing context */
577 Token *pDbToken, /* Name of the database containing table, or NULL */
578 Token *pTableToken, /* Name of table containing column, or NULL */
579 Token *pColumnToken, /* Name of the column. */
580 SrcList *pSrcList, /* List of tables used to resolve column names */
581 ExprList *pEList, /* List of expressions used to resolve "AS" */
582 Expr *pExpr /* Make this EXPR node point to the selected column */
583){
584 char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */
585 char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */
586 char *zCol = 0; /* Name of the column. The "Z" */
587 int i, j; /* Loop counters */
588 int cnt = 0; /* Number of matching column names */
589 int cntTab = 0; /* Number of matching table names */
drh7e26d752004-02-11 10:35:29 +0000590 sqlite *db = pParse->db; /* The database */
drh8141f612004-01-25 22:44:58 +0000591
592 assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
drha99db3b2004-06-19 14:49:12 +0000593 zDb = sqlite3NameFromToken(pDbToken);
594 zTab = sqlite3NameFromToken(pTableToken);
595 zCol = sqlite3NameFromToken(pColumnToken);
danielk197724b03fd2004-05-10 10:34:34 +0000596 if( sqlite3_malloc_failed ){
drh8141f612004-01-25 22:44:58 +0000597 return 1; /* Leak memory (zDb and zTab) if malloc fails */
598 }
599 assert( zTab==0 || pEList==0 );
600
601 pExpr->iTable = -1;
602 for(i=0; i<pSrcList->nSrc; i++){
603 struct SrcList_item *pItem = &pSrcList->a[i];
604 Table *pTab = pItem->pTab;
605 Column *pCol;
606
607 if( pTab==0 ) continue;
608 assert( pTab->nCol>0 );
609 if( zTab ){
610 if( pItem->zAlias ){
611 char *zTabName = pItem->zAlias;
danielk19774adee202004-05-08 08:23:19 +0000612 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
drh8141f612004-01-25 22:44:58 +0000613 }else{
614 char *zTabName = pTab->zName;
danielk19774adee202004-05-08 08:23:19 +0000615 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
616 if( zDb!=0 && sqlite3StrICmp(db->aDb[pTab->iDb].zName, zDb)!=0 ){
drh8141f612004-01-25 22:44:58 +0000617 continue;
618 }
619 }
620 }
621 if( 0==(cntTab++) ){
622 pExpr->iTable = pItem->iCursor;
623 pExpr->iDb = pTab->iDb;
624 }
625 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
danielk19774adee202004-05-08 08:23:19 +0000626 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
drh8141f612004-01-25 22:44:58 +0000627 cnt++;
628 pExpr->iTable = pItem->iCursor;
629 pExpr->iDb = pTab->iDb;
630 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
631 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
danielk1977a37cdde2004-05-16 11:15:36 +0000632 pExpr->affinity = pTab->aCol[j].affinity;
danielk19770202b292004-06-09 09:55:16 +0000633 pExpr->pColl = pTab->aCol[j].pColl;
drh8141f612004-01-25 22:44:58 +0000634 break;
635 }
636 }
637 }
638
639 /* If we have not already resolved the name, then maybe
640 ** it is a new.* or old.* trigger argument reference
641 */
642 if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
643 TriggerStack *pTriggerStack = pParse->trigStack;
644 Table *pTab = 0;
danielk19774adee202004-05-08 08:23:19 +0000645 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){
drh8141f612004-01-25 22:44:58 +0000646 pExpr->iTable = pTriggerStack->newIdx;
647 assert( pTriggerStack->pTab );
648 pTab = pTriggerStack->pTab;
danielk19774adee202004-05-08 08:23:19 +0000649 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab) == 0 ){
drh8141f612004-01-25 22:44:58 +0000650 pExpr->iTable = pTriggerStack->oldIdx;
651 assert( pTriggerStack->pTab );
652 pTab = pTriggerStack->pTab;
653 }
654
655 if( pTab ){
656 int j;
657 Column *pCol = pTab->aCol;
658
659 pExpr->iDb = pTab->iDb;
660 cntTab++;
661 for(j=0; j < pTab->nCol; j++, pCol++) {
danielk19774adee202004-05-08 08:23:19 +0000662 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
drh8141f612004-01-25 22:44:58 +0000663 cnt++;
664 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
danielk1977a37cdde2004-05-16 11:15:36 +0000665 pExpr->affinity = pTab->aCol[j].affinity;
danielk19770202b292004-06-09 09:55:16 +0000666 pExpr->pColl = pTab->aCol[j].pColl;
drh8141f612004-01-25 22:44:58 +0000667 break;
668 }
669 }
670 }
671 }
672
673 /*
674 ** Perhaps the name is a reference to the ROWID
675 */
danielk19774adee202004-05-08 08:23:19 +0000676 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
drh8141f612004-01-25 22:44:58 +0000677 cnt = 1;
678 pExpr->iColumn = -1;
danielk1977a37cdde2004-05-16 11:15:36 +0000679 pExpr->affinity = SQLITE_AFF_INTEGER;
drh8141f612004-01-25 22:44:58 +0000680 }
681
682 /*
683 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
684 ** might refer to an result-set alias. This happens, for example, when
685 ** we are resolving names in the WHERE clause of the following command:
686 **
687 ** SELECT a+b AS x FROM table WHERE x<10;
688 **
689 ** In cases like this, replace pExpr with a copy of the expression that
690 ** forms the result set entry ("a+b" in the example) and return immediately.
691 ** Note that the expression in the result set should have already been
692 ** resolved by the time the WHERE clause is resolved.
693 */
694 if( cnt==0 && pEList!=0 ){
695 for(j=0; j<pEList->nExpr; j++){
696 char *zAs = pEList->a[j].zName;
danielk19774adee202004-05-08 08:23:19 +0000697 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
drh8141f612004-01-25 22:44:58 +0000698 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
699 pExpr->op = TK_AS;
700 pExpr->iColumn = j;
danielk19774adee202004-05-08 08:23:19 +0000701 pExpr->pLeft = sqlite3ExprDup(pEList->a[j].pExpr);
drh8141f612004-01-25 22:44:58 +0000702 sqliteFree(zCol);
703 assert( zTab==0 && zDb==0 );
704 return 0;
705 }
706 }
707 }
708
709 /*
710 ** If X and Y are NULL (in other words if only the column name Z is
711 ** supplied) and the value of Z is enclosed in double-quotes, then
712 ** Z is a string literal if it doesn't match any column names. In that
713 ** case, we need to return right away and not make any changes to
714 ** pExpr.
715 */
716 if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
717 sqliteFree(zCol);
718 return 0;
719 }
720
721 /*
722 ** cnt==0 means there was not match. cnt>1 means there were two or
723 ** more matches. Either way, we have an error.
724 */
725 if( cnt!=1 ){
726 char *z = 0;
727 char *zErr;
728 zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s";
729 if( zDb ){
danielk19774adee202004-05-08 08:23:19 +0000730 sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, 0);
drh8141f612004-01-25 22:44:58 +0000731 }else if( zTab ){
danielk19774adee202004-05-08 08:23:19 +0000732 sqlite3SetString(&z, zTab, ".", zCol, 0);
drh8141f612004-01-25 22:44:58 +0000733 }else{
734 z = sqliteStrDup(zCol);
735 }
danielk19774adee202004-05-08 08:23:19 +0000736 sqlite3ErrorMsg(pParse, zErr, z);
drh8141f612004-01-25 22:44:58 +0000737 sqliteFree(z);
738 }
739
740 /* Clean up and return
741 */
742 sqliteFree(zDb);
743 sqliteFree(zTab);
744 sqliteFree(zCol);
danielk19774adee202004-05-08 08:23:19 +0000745 sqlite3ExprDelete(pExpr->pLeft);
drh8141f612004-01-25 22:44:58 +0000746 pExpr->pLeft = 0;
danielk19774adee202004-05-08 08:23:19 +0000747 sqlite3ExprDelete(pExpr->pRight);
drh8141f612004-01-25 22:44:58 +0000748 pExpr->pRight = 0;
749 pExpr->op = TK_COLUMN;
danielk19774adee202004-05-08 08:23:19 +0000750 sqlite3AuthRead(pParse, pExpr, pSrcList);
drh8141f612004-01-25 22:44:58 +0000751 return cnt!=1;
752}
753
754/*
drhcce7d172000-05-31 15:34:51 +0000755** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +0000756** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +0000757** index to the table in the table list and a column offset. The
758** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
759** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +0000760** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +0000761** VDBE cursor number for a cursor that is pointing into the referenced
762** table. The Expr.iColumn value is changed to the index of the column
763** of the referenced table. The Expr.iColumn value for the special
764** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
765** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +0000766**
drhfef52082000-06-06 01:50:43 +0000767** We also check for instances of the IN operator. IN comes in two
768** forms:
769**
770** expr IN (exprlist)
771** and
772** expr IN (SELECT ...)
773**
774** The first form is handled by creating a set holding the list
775** of allowed values. The second form causes the SELECT to generate
776** a temporary table.
777**
778** This routine also looks for scalar SELECTs that are part of an expression.
drh19a775c2000-06-05 18:54:46 +0000779** If it finds any, it generates code to write the value of that select
780** into a memory cell.
drhcce7d172000-05-31 15:34:51 +0000781**
drh967e8b72000-06-21 13:59:10 +0000782** Unknown columns or tables provoke an error. The function returns
drhcce7d172000-05-31 15:34:51 +0000783** the number of errors seen and leaves an error message on pParse->zErrMsg.
784*/
danielk19774adee202004-05-08 08:23:19 +0000785int sqlite3ExprResolveIds(
drha2e00042002-01-22 03:13:42 +0000786 Parse *pParse, /* The parser context */
drh8141f612004-01-25 22:44:58 +0000787 SrcList *pSrcList, /* List of tables used to resolve column names */
drha2e00042002-01-22 03:13:42 +0000788 ExprList *pEList, /* List of expressions used to resolve "AS" */
789 Expr *pExpr /* The expression to be analyzed. */
790){
drh6a3ea0e2003-05-02 14:32:12 +0000791 int i;
792
drh8141f612004-01-25 22:44:58 +0000793 if( pExpr==0 || pSrcList==0 ) return 0;
794 for(i=0; i<pSrcList->nSrc; i++){
795 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab );
drh6a3ea0e2003-05-02 14:32:12 +0000796 }
drhcce7d172000-05-31 15:34:51 +0000797 switch( pExpr->op ){
drh23989372002-05-21 13:43:04 +0000798 /* Double-quoted strings (ex: "abc") are used as identifiers if
799 ** possible. Otherwise they remain as strings. Single-quoted
800 ** strings (ex: 'abc') are always string literals.
801 */
802 case TK_STRING: {
803 if( pExpr->token.z[0]=='\'' ) break;
804 /* Fall thru into the TK_ID case if this is a double-quoted string */
805 }
drh8141f612004-01-25 22:44:58 +0000806 /* A lone identifier is the name of a columnd.
drha2e00042002-01-22 03:13:42 +0000807 */
drhcce7d172000-05-31 15:34:51 +0000808 case TK_ID: {
drh8141f612004-01-25 22:44:58 +0000809 if( lookupName(pParse, 0, 0, &pExpr->token, pSrcList, pEList, pExpr) ){
drhcce7d172000-05-31 15:34:51 +0000810 return 1;
drhed6c8672003-01-12 18:02:16 +0000811 }
drhcce7d172000-05-31 15:34:51 +0000812 break;
813 }
814
drhd24cc422003-03-27 12:51:24 +0000815 /* A table name and column name: ID.ID
816 ** Or a database, table and column: ID.ID.ID
817 */
drhcce7d172000-05-31 15:34:51 +0000818 case TK_DOT: {
drh8141f612004-01-25 22:44:58 +0000819 Token *pColumn;
820 Token *pTable;
821 Token *pDb;
822 Expr *pRight;
drhcce7d172000-05-31 15:34:51 +0000823
drhcce7d172000-05-31 15:34:51 +0000824 pRight = pExpr->pRight;
drhd24cc422003-03-27 12:51:24 +0000825 if( pRight->op==TK_ID ){
drh8141f612004-01-25 22:44:58 +0000826 pDb = 0;
827 pTable = &pExpr->pLeft->token;
828 pColumn = &pRight->token;
drhd24cc422003-03-27 12:51:24 +0000829 }else{
drh8141f612004-01-25 22:44:58 +0000830 assert( pRight->op==TK_DOT );
831 pDb = &pExpr->pLeft->token;
832 pTable = &pRight->pLeft->token;
833 pColumn = &pRight->pRight->token;
drhd24cc422003-03-27 12:51:24 +0000834 }
drh8141f612004-01-25 22:44:58 +0000835 if( lookupName(pParse, pDb, pTable, pColumn, pSrcList, 0, pExpr) ){
drhdaffd0e2001-04-11 14:28:42 +0000836 return 1;
837 }
drhcce7d172000-05-31 15:34:51 +0000838 break;
839 }
840
drhfef52082000-06-06 01:50:43 +0000841 case TK_IN: {
danielk1977e014a832004-05-17 10:48:57 +0000842 char affinity;
danielk19774adee202004-05-08 08:23:19 +0000843 Vdbe *v = sqlite3GetVdbe(pParse);
drhd3d39e92004-05-20 22:16:29 +0000844 KeyInfo keyInfo;
danielk19770202b292004-06-09 09:55:16 +0000845 int addr; /* Address of OP_OpenTemp instruction */
drhd3d39e92004-05-20 22:16:29 +0000846
drhfef52082000-06-06 01:50:43 +0000847 if( v==0 ) return 1;
danielk19774adee202004-05-08 08:23:19 +0000848 if( sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pLeft) ){
drhcfab11b2000-06-06 03:31:22 +0000849 return 1;
850 }
danielk1977bf3b7212004-05-18 10:06:24 +0000851 affinity = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +0000852
853 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
854 ** expression it is handled the same way. A temporary table is
855 ** filled with single-field index keys representing the results
856 ** from the SELECT or the <exprlist>.
857 **
858 ** If the 'x' expression is a column value, or the SELECT...
859 ** statement returns a column value, then the affinity of that
860 ** column is used to build the index keys. If both 'x' and the
861 ** SELECT... statement are columns, then numeric affinity is used
862 ** if either column has NUMERIC or INTEGER affinity. If neither
863 ** 'x' nor the SELECT... statement are columns, then numeric affinity
864 ** is used.
865 */
866 pExpr->iTable = pParse->nTab++;
danielk19770202b292004-06-09 09:55:16 +0000867 addr = sqlite3VdbeAddOp(v, OP_OpenTemp, pExpr->iTable, 0);
drhd3d39e92004-05-20 22:16:29 +0000868 memset(&keyInfo, 0, sizeof(keyInfo));
869 keyInfo.nField = 1;
drhf3218fe2004-05-28 08:21:02 +0000870 sqlite3VdbeAddOp(v, OP_SetNumColumns, pExpr->iTable, 1);
danielk1977e014a832004-05-17 10:48:57 +0000871
drhfef52082000-06-06 01:50:43 +0000872 if( pExpr->pSelect ){
873 /* Case 1: expr IN (SELECT ...)
874 **
danielk1977e014a832004-05-17 10:48:57 +0000875 ** Generate code to write the results of the select into the temporary
876 ** table allocated and opened above.
drhfef52082000-06-06 01:50:43 +0000877 */
danielk1977e014a832004-05-17 10:48:57 +0000878 int iParm = pExpr->iTable + (((int)affinity)<<16);
drhbe5c89a2004-07-26 00:31:09 +0000879 ExprList *pEList;
danielk1977e014a832004-05-17 10:48:57 +0000880 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
danielk1977bf3b7212004-05-18 10:06:24 +0000881 sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +0000882 pEList = pExpr->pSelect->pEList;
883 if( pEList && pEList->nExpr>0 ){
danielk19777cedc8d2004-06-10 10:50:08 +0000884 keyInfo.aColl[0] = binaryCompareCollSeq(pParse, pExpr->pLeft,
drhbe5c89a2004-07-26 00:31:09 +0000885 pEList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +0000886 }
drhfef52082000-06-06 01:50:43 +0000887 }else if( pExpr->pList ){
888 /* Case 2: expr IN (exprlist)
889 **
danielk1977e014a832004-05-17 10:48:57 +0000890 ** For each expression, build an index key from the evaluation and
891 ** store it in the temporary table. If <expr> is a column, then use
892 ** that columns affinity when building index keys. If <expr> is not
893 ** a column, use numeric affinity.
drhfef52082000-06-06 01:50:43 +0000894 */
danielk1977e014a832004-05-17 10:48:57 +0000895 int i;
896 char const *affStr;
897 if( !affinity ){
898 affinity = SQLITE_AFF_NUMERIC;
899 }
900 affStr = sqlite3AffinityString(affinity);
danielk19770202b292004-06-09 09:55:16 +0000901 keyInfo.aColl[0] = pExpr->pLeft->pColl;
danielk1977e014a832004-05-17 10:48:57 +0000902
903 /* Loop through each expression in <exprlist>. */
drhfef52082000-06-06 01:50:43 +0000904 for(i=0; i<pExpr->pList->nExpr; i++){
905 Expr *pE2 = pExpr->pList->a[i].pExpr;
danielk1977e014a832004-05-17 10:48:57 +0000906
907 /* Check that the expression is constant and valid. */
danielk19774adee202004-05-08 08:23:19 +0000908 if( !sqlite3ExprIsConstant(pE2) ){
909 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +0000910 "right-hand side of IN operator must be constant");
drhfef52082000-06-06 01:50:43 +0000911 return 1;
912 }
danielk19774adee202004-05-08 08:23:19 +0000913 if( sqlite3ExprCheck(pParse, pE2, 0, 0) ){
drh4794b982000-06-06 13:54:14 +0000914 return 1;
915 }
danielk1977e014a832004-05-17 10:48:57 +0000916
917 /* Evaluate the expression and insert it into the temp table */
918 sqlite3ExprCode(pParse, pE2);
danielk1977ededfd52004-06-17 07:53:01 +0000919 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, affStr, P3_STATIC);
danielk19770f69c1e2004-05-29 11:24:50 +0000920 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +0000921 sqlite3VdbeAddOp(v, OP_PutStrKey, pExpr->iTable, 0);
drhfef52082000-06-06 01:50:43 +0000922 }
923 }
danielk19770202b292004-06-09 09:55:16 +0000924 sqlite3VdbeChangeP3(v, addr, (void *)&keyInfo, P3_KEYINFO);
925
drhcfab11b2000-06-06 03:31:22 +0000926 break;
drhfef52082000-06-06 01:50:43 +0000927 }
928
drh19a775c2000-06-05 18:54:46 +0000929 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +0000930 /* This has to be a scalar SELECT. Generate code to put the
931 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +0000932 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +0000933 */
drh967e8b72000-06-21 13:59:10 +0000934 pExpr->iColumn = pParse->nMem++;
danielk1977bf3b7212004-05-18 10:06:24 +0000935 if(sqlite3Select(pParse, pExpr->pSelect, SRT_Mem,pExpr->iColumn,0,0,0,0)){
drh19a775c2000-06-05 18:54:46 +0000936 return 1;
937 }
938 break;
939 }
940
drhcce7d172000-05-31 15:34:51 +0000941 /* For all else, just recursively walk the tree */
942 default: {
drh4794b982000-06-06 13:54:14 +0000943 if( pExpr->pLeft
danielk19774adee202004-05-08 08:23:19 +0000944 && sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pLeft) ){
drhcce7d172000-05-31 15:34:51 +0000945 return 1;
946 }
947 if( pExpr->pRight
danielk19774adee202004-05-08 08:23:19 +0000948 && sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pRight) ){
drhcce7d172000-05-31 15:34:51 +0000949 return 1;
950 }
951 if( pExpr->pList ){
952 int i;
953 ExprList *pList = pExpr->pList;
954 for(i=0; i<pList->nExpr; i++){
drh832508b2002-03-02 17:04:07 +0000955 Expr *pArg = pList->a[i].pExpr;
danielk19774adee202004-05-08 08:23:19 +0000956 if( sqlite3ExprResolveIds(pParse, pSrcList, pEList, pArg) ){
drhcce7d172000-05-31 15:34:51 +0000957 return 1;
958 }
959 }
960 }
961 }
962 }
963 return 0;
964}
965
drhcce7d172000-05-31 15:34:51 +0000966/*
drh4b59ab52002-08-24 18:24:51 +0000967** pExpr is a node that defines a function of some kind. It might
968** be a syntactic function like "count(x)" or it might be a function
969** that implements an operator, like "a LIKE b".
970**
971** This routine makes *pzName point to the name of the function and
972** *pnName hold the number of characters in the function name.
973*/
974static void getFunctionName(Expr *pExpr, const char **pzName, int *pnName){
975 switch( pExpr->op ){
976 case TK_FUNCTION: {
977 *pzName = pExpr->token.z;
drh6977fea2002-10-22 23:38:04 +0000978 *pnName = pExpr->token.n;
drh4b59ab52002-08-24 18:24:51 +0000979 break;
980 }
981 case TK_LIKE: {
982 *pzName = "like";
983 *pnName = 4;
984 break;
985 }
986 case TK_GLOB: {
987 *pzName = "glob";
988 *pnName = 4;
989 break;
990 }
991 default: {
992 *pzName = "can't happen";
993 *pnName = 12;
994 break;
995 }
996 }
997}
998
999/*
drhcce7d172000-05-31 15:34:51 +00001000** Error check the functions in an expression. Make sure all
1001** function names are recognized and all functions have the correct
1002** number of arguments. Leave an error message in pParse->zErrMsg
1003** if anything is amiss. Return the number of errors.
1004**
1005** if pIsAgg is not null and this expression is an aggregate function
1006** (like count(*) or max(value)) then write a 1 into *pIsAgg.
1007*/
danielk19774adee202004-05-08 08:23:19 +00001008int sqlite3ExprCheck(Parse *pParse, Expr *pExpr, int allowAgg, int *pIsAgg){
drhcce7d172000-05-31 15:34:51 +00001009 int nErr = 0;
1010 if( pExpr==0 ) return 0;
drhcce7d172000-05-31 15:34:51 +00001011 switch( pExpr->op ){
drh4b59ab52002-08-24 18:24:51 +00001012 case TK_GLOB:
1013 case TK_LIKE:
drhcce7d172000-05-31 15:34:51 +00001014 case TK_FUNCTION: {
drhc9b84a12002-06-20 11:36:48 +00001015 int n = pExpr->pList ? pExpr->pList->nExpr : 0; /* Number of arguments */
1016 int no_such_func = 0; /* True if no such function exists */
drhc9b84a12002-06-20 11:36:48 +00001017 int wrong_num_args = 0; /* True if wrong number of arguments */
1018 int is_agg = 0; /* True if is an aggregate function */
drhcce7d172000-05-31 15:34:51 +00001019 int i;
drh4b59ab52002-08-24 18:24:51 +00001020 int nId; /* Number of characters in function name */
1021 const char *zId; /* The function name. */
drh0bce8352002-02-28 00:41:10 +00001022 FuncDef *pDef;
danielk1977d8123362004-06-12 09:25:12 +00001023 int enc = pParse->db->enc;
drh0bce8352002-02-28 00:41:10 +00001024
drh4b59ab52002-08-24 18:24:51 +00001025 getFunctionName(pExpr, &zId, &nId);
danielk1977d8123362004-06-12 09:25:12 +00001026 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001027 if( pDef==0 ){
danielk1977d8123362004-06-12 09:25:12 +00001028 pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001029 if( pDef==0 ){
drh268380c2004-02-25 13:47:31 +00001030 no_such_func = 1;
drh0bce8352002-02-28 00:41:10 +00001031 }else{
1032 wrong_num_args = 1;
drhcce7d172000-05-31 15:34:51 +00001033 }
drh0bce8352002-02-28 00:41:10 +00001034 }else{
1035 is_agg = pDef->xFunc==0;
drhcce7d172000-05-31 15:34:51 +00001036 }
drh8e0a2f92002-02-23 23:45:45 +00001037 if( is_agg && !allowAgg ){
danielk19774adee202004-05-08 08:23:19 +00001038 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId, zId);
drh8e0a2f92002-02-23 23:45:45 +00001039 nErr++;
1040 is_agg = 0;
1041 }else if( no_such_func ){
danielk19774adee202004-05-08 08:23:19 +00001042 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
drhcce7d172000-05-31 15:34:51 +00001043 nErr++;
drh8e0a2f92002-02-23 23:45:45 +00001044 }else if( wrong_num_args ){
danielk19774adee202004-05-08 08:23:19 +00001045 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
drhf7a9e1a2004-02-22 18:40:56 +00001046 nId, zId);
drh8e0a2f92002-02-23 23:45:45 +00001047 nErr++;
drhcce7d172000-05-31 15:34:51 +00001048 }
drhf7a9e1a2004-02-22 18:40:56 +00001049 if( is_agg ){
1050 pExpr->op = TK_AGG_FUNCTION;
1051 if( pIsAgg ) *pIsAgg = 1;
1052 }
drhcce7d172000-05-31 15:34:51 +00001053 for(i=0; nErr==0 && i<n; i++){
danielk19774adee202004-05-08 08:23:19 +00001054 nErr = sqlite3ExprCheck(pParse, pExpr->pList->a[i].pExpr,
drh4cfa7932000-06-08 15:10:46 +00001055 allowAgg && !is_agg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +00001056 }
danielk19770202b292004-06-09 09:55:16 +00001057 /* FIX ME: Compute pExpr->affinity based on the expected return
1058 ** type of the function
1059 */
drhcce7d172000-05-31 15:34:51 +00001060 }
1061 default: {
1062 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001063 nErr = sqlite3ExprCheck(pParse, pExpr->pLeft, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +00001064 }
1065 if( nErr==0 && pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001066 nErr = sqlite3ExprCheck(pParse, pExpr->pRight, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +00001067 }
drhfef52082000-06-06 01:50:43 +00001068 if( nErr==0 && pExpr->pList ){
1069 int n = pExpr->pList->nExpr;
1070 int i;
1071 for(i=0; nErr==0 && i<n; i++){
drh22827922000-06-06 17:27:05 +00001072 Expr *pE2 = pExpr->pList->a[i].pExpr;
danielk19774adee202004-05-08 08:23:19 +00001073 nErr = sqlite3ExprCheck(pParse, pE2, allowAgg, pIsAgg);
drhfef52082000-06-06 01:50:43 +00001074 }
1075 }
drhcce7d172000-05-31 15:34:51 +00001076 break;
1077 }
1078 }
1079 return nErr;
1080}
1081
1082/*
drhfec19aa2004-05-19 20:41:03 +00001083** Generate an instruction that will put the integer describe by
1084** text z[0..n-1] on the stack.
1085*/
1086static void codeInteger(Vdbe *v, const char *z, int n){
1087 int i;
drh6fec0762004-05-30 01:38:43 +00001088 if( sqlite3GetInt32(z, &i) ){
1089 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
1090 }else if( sqlite3FitsIn64Bits(z) ){
1091 sqlite3VdbeOp3(v, OP_Integer, 0, 0, z, n);
drhfec19aa2004-05-19 20:41:03 +00001092 }else{
1093 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n);
1094 }
1095}
1096
1097/*
drhcce7d172000-05-31 15:34:51 +00001098** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +00001099** expression and leave the result on the top of stack.
drhcce7d172000-05-31 15:34:51 +00001100*/
danielk19774adee202004-05-08 08:23:19 +00001101void sqlite3ExprCode(Parse *pParse, Expr *pExpr){
drhcce7d172000-05-31 15:34:51 +00001102 Vdbe *v = pParse->pVdbe;
1103 int op;
drhdaffd0e2001-04-11 14:28:42 +00001104 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001105 switch( pExpr->op ){
1106 case TK_PLUS: op = OP_Add; break;
1107 case TK_MINUS: op = OP_Subtract; break;
1108 case TK_STAR: op = OP_Multiply; break;
1109 case TK_SLASH: op = OP_Divide; break;
1110 case TK_AND: op = OP_And; break;
1111 case TK_OR: op = OP_Or; break;
1112 case TK_LT: op = OP_Lt; break;
1113 case TK_LE: op = OP_Le; break;
1114 case TK_GT: op = OP_Gt; break;
1115 case TK_GE: op = OP_Ge; break;
1116 case TK_NE: op = OP_Ne; break;
1117 case TK_EQ: op = OP_Eq; break;
drhcce7d172000-05-31 15:34:51 +00001118 case TK_ISNULL: op = OP_IsNull; break;
1119 case TK_NOTNULL: op = OP_NotNull; break;
1120 case TK_NOT: op = OP_Not; break;
1121 case TK_UMINUS: op = OP_Negative; break;
drhbf4133c2001-10-13 02:59:08 +00001122 case TK_BITAND: op = OP_BitAnd; break;
1123 case TK_BITOR: op = OP_BitOr; break;
1124 case TK_BITNOT: op = OP_BitNot; break;
1125 case TK_LSHIFT: op = OP_ShiftLeft; break;
1126 case TK_RSHIFT: op = OP_ShiftRight; break;
1127 case TK_REM: op = OP_Remainder; break;
drhfec19aa2004-05-19 20:41:03 +00001128 case TK_FLOAT: op = OP_Real; break;
danielk19770f69c1e2004-05-29 11:24:50 +00001129 case TK_STRING: op = OP_String8; break;
danielk1977c572ef72004-05-27 09:28:41 +00001130 case TK_BLOB: op = OP_HexBlob; break;
danielk1977cfe9a692004-06-16 12:00:29 +00001131 default: op = 0; break;
drhcce7d172000-05-31 15:34:51 +00001132 }
1133 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00001134 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +00001135 if( pParse->useAgg ){
danielk19774adee202004-05-08 08:23:19 +00001136 sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drhc4a3c772001-04-04 11:48:57 +00001137 }else if( pExpr->iColumn>=0 ){
danielk19774adee202004-05-08 08:23:19 +00001138 sqlite3VdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn);
drhc4a3c772001-04-04 11:48:57 +00001139 }else{
danielk19774adee202004-05-08 08:23:19 +00001140 sqlite3VdbeAddOp(v, OP_Recno, pExpr->iTable, 0);
drh22827922000-06-06 17:27:05 +00001141 }
drhcce7d172000-05-31 15:34:51 +00001142 break;
1143 }
1144 case TK_INTEGER: {
drhfec19aa2004-05-19 20:41:03 +00001145 codeInteger(v, pExpr->token.z, pExpr->token.n);
1146 break;
1147 }
1148 case TK_FLOAT:
1149 case TK_STRING: {
1150 sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z, pExpr->token.n);
danielk19774adee202004-05-08 08:23:19 +00001151 sqlite3VdbeDequoteP3(v, -1);
drhcce7d172000-05-31 15:34:51 +00001152 break;
1153 }
danielk1977c572ef72004-05-27 09:28:41 +00001154 case TK_BLOB: {
1155 sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z+1, pExpr->token.n-1);
1156 sqlite3VdbeDequoteP3(v, -1);
1157 break;
1158 }
drhcce7d172000-05-31 15:34:51 +00001159 case TK_NULL: {
danielk19770f69c1e2004-05-29 11:24:50 +00001160 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001161 break;
1162 }
drh50457892003-09-06 01:10:47 +00001163 case TK_VARIABLE: {
danielk19774adee202004-05-08 08:23:19 +00001164 sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0);
drh50457892003-09-06 01:10:47 +00001165 break;
1166 }
drhc9b84a12002-06-20 11:36:48 +00001167 case TK_LT:
1168 case TK_LE:
1169 case TK_GT:
1170 case TK_GE:
1171 case TK_NE:
1172 case TK_EQ: {
danielk1977a37cdde2004-05-16 11:15:36 +00001173 sqlite3ExprCode(pParse, pExpr->pLeft);
1174 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001175 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 0, 0);
danielk1977a37cdde2004-05-16 11:15:36 +00001176 break;
drhc9b84a12002-06-20 11:36:48 +00001177 }
drhcce7d172000-05-31 15:34:51 +00001178 case TK_AND:
1179 case TK_OR:
1180 case TK_PLUS:
1181 case TK_STAR:
1182 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00001183 case TK_REM:
1184 case TK_BITAND:
1185 case TK_BITOR:
drh17c40292004-07-21 02:53:29 +00001186 case TK_SLASH:
drhbf4133c2001-10-13 02:59:08 +00001187 case TK_LSHIFT:
1188 case TK_RSHIFT: {
danielk19774adee202004-05-08 08:23:19 +00001189 sqlite3ExprCode(pParse, pExpr->pLeft);
drh17c40292004-07-21 02:53:29 +00001190 sqlite3ExprCode(pParse, pExpr->pRight);
danielk19774adee202004-05-08 08:23:19 +00001191 sqlite3VdbeAddOp(v, op, 0, 0);
drhbf4133c2001-10-13 02:59:08 +00001192 break;
1193 }
drh00400772000-06-16 20:51:26 +00001194 case TK_CONCAT: {
danielk19774adee202004-05-08 08:23:19 +00001195 sqlite3ExprCode(pParse, pExpr->pLeft);
1196 sqlite3ExprCode(pParse, pExpr->pRight);
danielk197772c952a2004-06-21 09:06:41 +00001197 sqlite3VdbeAddOp(v, OP_Concat8, 2, 0);
drh00400772000-06-16 20:51:26 +00001198 break;
1199 }
drhcce7d172000-05-31 15:34:51 +00001200 case TK_UMINUS: {
drhfec19aa2004-05-19 20:41:03 +00001201 Expr *pLeft = pExpr->pLeft;
1202 assert( pLeft );
1203 if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
1204 Token *p = &pLeft->token;
drh6e142f52000-06-08 13:36:40 +00001205 char *z = sqliteMalloc( p->n + 2 );
1206 sprintf(z, "-%.*s", p->n, p->z);
drhfec19aa2004-05-19 20:41:03 +00001207 if( pLeft->op==TK_FLOAT ){
1208 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001209 }else{
drhfec19aa2004-05-19 20:41:03 +00001210 codeInteger(v, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001211 }
drh6e142f52000-06-08 13:36:40 +00001212 sqliteFree(z);
1213 break;
1214 }
drh1ccde152000-06-17 13:12:39 +00001215 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +00001216 }
drhbf4133c2001-10-13 02:59:08 +00001217 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00001218 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001219 sqlite3ExprCode(pParse, pExpr->pLeft);
1220 sqlite3VdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001221 break;
1222 }
1223 case TK_ISNULL:
1224 case TK_NOTNULL: {
1225 int dest;
danielk19774adee202004-05-08 08:23:19 +00001226 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1227 sqlite3ExprCode(pParse, pExpr->pLeft);
1228 dest = sqlite3VdbeCurrentAddr(v) + 2;
1229 sqlite3VdbeAddOp(v, op, 1, dest);
1230 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);
drhcce7d172000-05-31 15:34:51 +00001231 }
danielk1977a37cdde2004-05-16 11:15:36 +00001232 break;
drh22827922000-06-06 17:27:05 +00001233 case TK_AGG_FUNCTION: {
danielk19774adee202004-05-08 08:23:19 +00001234 sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drh22827922000-06-06 17:27:05 +00001235 break;
1236 }
drh4b59ab52002-08-24 18:24:51 +00001237 case TK_GLOB:
1238 case TK_LIKE:
drhcce7d172000-05-31 15:34:51 +00001239 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00001240 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00001241 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00001242 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00001243 int nId;
1244 const char *zId;
danielk1977682f68b2004-06-05 10:22:17 +00001245 int p2 = 0;
1246 int i;
danielk1977d8123362004-06-12 09:25:12 +00001247 u8 enc = pParse->db->enc;
danielk1977dc1bdc42004-06-11 10:51:27 +00001248 CollSeq *pColl = 0;
drh4b59ab52002-08-24 18:24:51 +00001249 getFunctionName(pExpr, &zId, &nId);
danielk1977d8123362004-06-12 09:25:12 +00001250 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001251 assert( pDef!=0 );
drhf9b596e2004-05-26 16:54:42 +00001252 nExpr = sqlite3ExprCodeExprList(pParse, pList);
danielk1977682f68b2004-06-05 10:22:17 +00001253 for(i=0; i<nExpr && i<32; i++){
danielk1977d02eb1f2004-06-06 09:44:03 +00001254 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
1255 p2 |= (1<<i);
1256 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001257 if( pDef->needCollSeq && !pColl ){
1258 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
1259 }
1260 }
1261 if( pDef->needCollSeq ){
1262 if( !pColl ) pColl = pParse->db->pDfltColl;
danielk1977d8123362004-06-12 09:25:12 +00001263 sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ);
danielk1977682f68b2004-06-05 10:22:17 +00001264 }
1265 sqlite3VdbeOp3(v, OP_Function, nExpr, p2, (char*)pDef, P3_FUNCDEF);
drhcce7d172000-05-31 15:34:51 +00001266 break;
1267 }
drh19a775c2000-06-05 18:54:46 +00001268 case TK_SELECT: {
danielk19774adee202004-05-08 08:23:19 +00001269 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drh19a775c2000-06-05 18:54:46 +00001270 break;
1271 }
drhfef52082000-06-06 01:50:43 +00001272 case TK_IN: {
1273 int addr;
danielk1977e014a832004-05-17 10:48:57 +00001274 char const *affStr;
1275
1276 /* Figure out the affinity to use to create a key from the results
1277 ** of the expression. affinityStr stores a static string suitable for
danielk1977ededfd52004-06-17 07:53:01 +00001278 ** P3 of OP_MakeRecord.
danielk1977e014a832004-05-17 10:48:57 +00001279 */
1280 affStr = sqlite3AffinityString(comparisonAffinity(pExpr));
1281
danielk19774adee202004-05-08 08:23:19 +00001282 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
danielk1977e014a832004-05-17 10:48:57 +00001283
1284 /* Code the <expr> from "<expr> IN (...)". The temporary table
1285 ** pExpr->iTable contains the values that make up the (...) set.
1286 */
danielk19774adee202004-05-08 08:23:19 +00001287 sqlite3ExprCode(pParse, pExpr->pLeft);
1288 addr = sqlite3VdbeCurrentAddr(v);
danielk1977e014a832004-05-17 10:48:57 +00001289 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); /* addr + 0 */
danielk19774adee202004-05-08 08:23:19 +00001290 sqlite3VdbeAddOp(v, OP_Pop, 2, 0);
danielk19770f69c1e2004-05-29 11:24:50 +00001291 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +00001292 sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7);
danielk1977ededfd52004-06-17 07:53:01 +00001293 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, affStr, P3_STATIC); /* addr + 4 */
danielk1977e014a832004-05-17 10:48:57 +00001294 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7);
1295 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); /* addr + 6 */
1296
drhfef52082000-06-06 01:50:43 +00001297 break;
1298 }
1299 case TK_BETWEEN: {
drhbe5c89a2004-07-26 00:31:09 +00001300 Expr *pLeft = pExpr->pLeft;
1301 struct ExprList_item *pLItem = pExpr->pList->a;
1302 Expr *pRight = pLItem->pExpr;
1303 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001304 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001305 sqlite3ExprCode(pParse, pRight);
1306 codeCompare(pParse, pLeft, pRight, OP_Ge, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001307 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
drhbe5c89a2004-07-26 00:31:09 +00001308 pLItem++;
1309 pRight = pLItem->pExpr;
1310 sqlite3ExprCode(pParse, pRight);
1311 codeCompare(pParse, pLeft, pRight, OP_Le, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001312 sqlite3VdbeAddOp(v, OP_And, 0, 0);
drhfef52082000-06-06 01:50:43 +00001313 break;
1314 }
drh51e9a442004-01-16 16:42:53 +00001315 case TK_UPLUS:
drha2e00042002-01-22 03:13:42 +00001316 case TK_AS: {
danielk19774adee202004-05-08 08:23:19 +00001317 sqlite3ExprCode(pParse, pExpr->pLeft);
drha2e00042002-01-22 03:13:42 +00001318 break;
1319 }
drh17a7f8d2002-03-24 13:13:27 +00001320 case TK_CASE: {
1321 int expr_end_label;
drhf5905aa2002-05-26 20:54:33 +00001322 int jumpInst;
1323 int addr;
1324 int nExpr;
drh17a7f8d2002-03-24 13:13:27 +00001325 int i;
drhbe5c89a2004-07-26 00:31:09 +00001326 ExprList *pEList;
1327 struct ExprList_item *aListelem;
drh17a7f8d2002-03-24 13:13:27 +00001328
1329 assert(pExpr->pList);
1330 assert((pExpr->pList->nExpr % 2) == 0);
1331 assert(pExpr->pList->nExpr > 0);
drhbe5c89a2004-07-26 00:31:09 +00001332 pEList = pExpr->pList;
1333 aListelem = pEList->a;
1334 nExpr = pEList->nExpr;
danielk19774adee202004-05-08 08:23:19 +00001335 expr_end_label = sqlite3VdbeMakeLabel(v);
drh17a7f8d2002-03-24 13:13:27 +00001336 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001337 sqlite3ExprCode(pParse, pExpr->pLeft);
drh17a7f8d2002-03-24 13:13:27 +00001338 }
drhf5905aa2002-05-26 20:54:33 +00001339 for(i=0; i<nExpr; i=i+2){
drhbe5c89a2004-07-26 00:31:09 +00001340 sqlite3ExprCode(pParse, aListelem[i].pExpr);
drh17a7f8d2002-03-24 13:13:27 +00001341 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001342 sqlite3VdbeAddOp(v, OP_Dup, 1, 1);
drhbe5c89a2004-07-26 00:31:09 +00001343 jumpInst = codeCompare(pParse, pExpr->pLeft, aListelem[i].pExpr,
1344 OP_Ne, 0, 1);
danielk19774adee202004-05-08 08:23:19 +00001345 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001346 }else{
danielk19774adee202004-05-08 08:23:19 +00001347 jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001348 }
drhbe5c89a2004-07-26 00:31:09 +00001349 sqlite3ExprCode(pParse, aListelem[i+1].pExpr);
danielk19774adee202004-05-08 08:23:19 +00001350 sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label);
1351 addr = sqlite3VdbeCurrentAddr(v);
1352 sqlite3VdbeChangeP2(v, jumpInst, addr);
drh17a7f8d2002-03-24 13:13:27 +00001353 }
drhf570f012002-05-31 15:51:25 +00001354 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001355 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhf570f012002-05-31 15:51:25 +00001356 }
drh17a7f8d2002-03-24 13:13:27 +00001357 if( pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001358 sqlite3ExprCode(pParse, pExpr->pRight);
drh17a7f8d2002-03-24 13:13:27 +00001359 }else{
danielk19770f69c1e2004-05-29 11:24:50 +00001360 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
drh17a7f8d2002-03-24 13:13:27 +00001361 }
danielk19774adee202004-05-08 08:23:19 +00001362 sqlite3VdbeResolveLabel(v, expr_end_label);
danielk19776f349032002-06-11 02:25:40 +00001363 break;
1364 }
1365 case TK_RAISE: {
1366 if( !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +00001367 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00001368 "RAISE() may only be used within a trigger-program");
danielk19776f349032002-06-11 02:25:40 +00001369 pParse->nErr++;
1370 return;
1371 }
1372 if( pExpr->iColumn == OE_Rollback ||
1373 pExpr->iColumn == OE_Abort ||
1374 pExpr->iColumn == OE_Fail ){
danielk19774adee202004-05-08 08:23:19 +00001375 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn,
drh701a0ae2004-02-22 20:05:00 +00001376 pExpr->token.z, pExpr->token.n);
danielk19774adee202004-05-08 08:23:19 +00001377 sqlite3VdbeDequoteP3(v, -1);
danielk19776f349032002-06-11 02:25:40 +00001378 } else {
1379 assert( pExpr->iColumn == OE_Ignore );
danielk19774adee202004-05-08 08:23:19 +00001380 sqlite3VdbeOp3(v, OP_Goto, 0, pParse->trigStack->ignoreJump,
drh701a0ae2004-02-22 20:05:00 +00001381 "(IGNORE jump)", 0);
danielk19776f349032002-06-11 02:25:40 +00001382 }
drh17a7f8d2002-03-24 13:13:27 +00001383 }
1384 break;
drhcce7d172000-05-31 15:34:51 +00001385 }
drhcce7d172000-05-31 15:34:51 +00001386}
1387
1388/*
drh268380c2004-02-25 13:47:31 +00001389** Generate code that pushes the value of every element of the given
drhf9b596e2004-05-26 16:54:42 +00001390** expression list onto the stack.
drh268380c2004-02-25 13:47:31 +00001391**
1392** Return the number of elements pushed onto the stack.
1393*/
danielk19774adee202004-05-08 08:23:19 +00001394int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00001395 Parse *pParse, /* Parsing context */
drhf9b596e2004-05-26 16:54:42 +00001396 ExprList *pList /* The expression list to be coded */
drh268380c2004-02-25 13:47:31 +00001397){
1398 struct ExprList_item *pItem;
1399 int i, n;
1400 Vdbe *v;
1401 if( pList==0 ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001402 v = sqlite3GetVdbe(pParse);
drh268380c2004-02-25 13:47:31 +00001403 n = pList->nExpr;
1404 for(pItem=pList->a, i=0; i<n; i++, pItem++){
danielk19774adee202004-05-08 08:23:19 +00001405 sqlite3ExprCode(pParse, pItem->pExpr);
drh268380c2004-02-25 13:47:31 +00001406 }
drhf9b596e2004-05-26 16:54:42 +00001407 return n;
drh268380c2004-02-25 13:47:31 +00001408}
1409
1410/*
drhcce7d172000-05-31 15:34:51 +00001411** Generate code for a boolean expression such that a jump is made
1412** to the label "dest" if the expression is true but execution
1413** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00001414**
1415** If the expression evaluates to NULL (neither true nor false), then
1416** take the jump if the jumpIfNull flag is true.
drhcce7d172000-05-31 15:34:51 +00001417*/
danielk19774adee202004-05-08 08:23:19 +00001418void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001419 Vdbe *v = pParse->pVdbe;
1420 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001421 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001422 switch( pExpr->op ){
1423 case TK_LT: op = OP_Lt; break;
1424 case TK_LE: op = OP_Le; break;
1425 case TK_GT: op = OP_Gt; break;
1426 case TK_GE: op = OP_Ge; break;
1427 case TK_NE: op = OP_Ne; break;
1428 case TK_EQ: op = OP_Eq; break;
drhcce7d172000-05-31 15:34:51 +00001429 case TK_ISNULL: op = OP_IsNull; break;
1430 case TK_NOTNULL: op = OP_NotNull; break;
1431 default: break;
1432 }
1433 switch( pExpr->op ){
1434 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001435 int d2 = sqlite3VdbeMakeLabel(v);
1436 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
1437 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
1438 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001439 break;
1440 }
1441 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001442 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
1443 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001444 break;
1445 }
1446 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001447 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001448 break;
1449 }
1450 case TK_LT:
1451 case TK_LE:
1452 case TK_GT:
1453 case TK_GE:
1454 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00001455 case TK_EQ: {
danielk19774adee202004-05-08 08:23:19 +00001456 sqlite3ExprCode(pParse, pExpr->pLeft);
1457 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001458 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001459 break;
1460 }
1461 case TK_ISNULL:
1462 case TK_NOTNULL: {
danielk19774adee202004-05-08 08:23:19 +00001463 sqlite3ExprCode(pParse, pExpr->pLeft);
1464 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001465 break;
1466 }
drhfef52082000-06-06 01:50:43 +00001467 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00001468 /* The expression "x BETWEEN y AND z" is implemented as:
1469 **
1470 ** 1 IF (x < y) GOTO 3
1471 ** 2 IF (x <= z) GOTO <dest>
1472 ** 3 ...
1473 */
drhf5905aa2002-05-26 20:54:33 +00001474 int addr;
drhbe5c89a2004-07-26 00:31:09 +00001475 Expr *pLeft = pExpr->pLeft;
1476 Expr *pRight = pExpr->pList->a[0].pExpr;
1477 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001478 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001479 sqlite3ExprCode(pParse, pRight);
1480 addr = codeCompare(pParse, pLeft, pRight, OP_Lt, 0, !jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00001481
drhbe5c89a2004-07-26 00:31:09 +00001482 pRight = pExpr->pList->a[1].pExpr;
1483 sqlite3ExprCode(pParse, pRight);
1484 codeCompare(pParse, pLeft, pRight, OP_Le, dest, jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00001485
danielk19774adee202004-05-08 08:23:19 +00001486 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
1487 sqlite3VdbeChangeP2(v, addr, sqlite3VdbeCurrentAddr(v));
1488 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00001489 break;
1490 }
drhcce7d172000-05-31 15:34:51 +00001491 default: {
danielk19774adee202004-05-08 08:23:19 +00001492 sqlite3ExprCode(pParse, pExpr);
1493 sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001494 break;
1495 }
1496 }
1497}
1498
1499/*
drh66b89c82000-11-28 20:47:17 +00001500** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00001501** to the label "dest" if the expression is false but execution
1502** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00001503**
1504** If the expression evaluates to NULL (neither true nor false) then
1505** jump if jumpIfNull is true or fall through if jumpIfNull is false.
drhcce7d172000-05-31 15:34:51 +00001506*/
danielk19774adee202004-05-08 08:23:19 +00001507void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001508 Vdbe *v = pParse->pVdbe;
1509 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001510 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001511 switch( pExpr->op ){
1512 case TK_LT: op = OP_Ge; break;
1513 case TK_LE: op = OP_Gt; break;
1514 case TK_GT: op = OP_Le; break;
1515 case TK_GE: op = OP_Lt; break;
1516 case TK_NE: op = OP_Eq; break;
1517 case TK_EQ: op = OP_Ne; break;
drhcce7d172000-05-31 15:34:51 +00001518 case TK_ISNULL: op = OP_NotNull; break;
1519 case TK_NOTNULL: op = OP_IsNull; break;
1520 default: break;
1521 }
1522 switch( pExpr->op ){
1523 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001524 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
1525 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001526 break;
1527 }
1528 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001529 int d2 = sqlite3VdbeMakeLabel(v);
1530 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
1531 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
1532 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001533 break;
1534 }
1535 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001536 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001537 break;
1538 }
1539 case TK_LT:
1540 case TK_LE:
1541 case TK_GT:
1542 case TK_GE:
1543 case TK_NE:
1544 case TK_EQ: {
danielk19774adee202004-05-08 08:23:19 +00001545 sqlite3ExprCode(pParse, pExpr->pLeft);
1546 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001547 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001548 break;
1549 }
drhcce7d172000-05-31 15:34:51 +00001550 case TK_ISNULL:
1551 case TK_NOTNULL: {
danielk19774adee202004-05-08 08:23:19 +00001552 sqlite3ExprCode(pParse, pExpr->pLeft);
1553 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001554 break;
1555 }
drhfef52082000-06-06 01:50:43 +00001556 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00001557 /* The expression is "x BETWEEN y AND z". It is implemented as:
1558 **
1559 ** 1 IF (x >= y) GOTO 3
1560 ** 2 GOTO <dest>
1561 ** 3 IF (x > z) GOTO <dest>
1562 */
drhfef52082000-06-06 01:50:43 +00001563 int addr;
drhbe5c89a2004-07-26 00:31:09 +00001564 Expr *pLeft = pExpr->pLeft;
1565 Expr *pRight = pExpr->pList->a[0].pExpr;
1566 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001567 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001568 sqlite3ExprCode(pParse, pRight);
danielk19774adee202004-05-08 08:23:19 +00001569 addr = sqlite3VdbeCurrentAddr(v);
drhbe5c89a2004-07-26 00:31:09 +00001570 codeCompare(pParse, pLeft, pRight, OP_Ge, addr+3, !jumpIfNull);
1571
danielk19774adee202004-05-08 08:23:19 +00001572 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
1573 sqlite3VdbeAddOp(v, OP_Goto, 0, dest);
drhbe5c89a2004-07-26 00:31:09 +00001574 pRight = pExpr->pList->a[1].pExpr;
1575 sqlite3ExprCode(pParse, pRight);
1576 codeCompare(pParse, pLeft, pRight, OP_Gt, dest, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00001577 break;
1578 }
drhcce7d172000-05-31 15:34:51 +00001579 default: {
danielk19774adee202004-05-08 08:23:19 +00001580 sqlite3ExprCode(pParse, pExpr);
1581 sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001582 break;
1583 }
1584 }
1585}
drh22827922000-06-06 17:27:05 +00001586
1587/*
1588** Do a deep comparison of two expression trees. Return TRUE (non-zero)
1589** if they are identical and return FALSE if they differ in any way.
1590*/
danielk19774adee202004-05-08 08:23:19 +00001591int sqlite3ExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00001592 int i;
1593 if( pA==0 ){
1594 return pB==0;
1595 }else if( pB==0 ){
1596 return 0;
1597 }
1598 if( pA->op!=pB->op ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001599 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
1600 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00001601 if( pA->pList ){
1602 if( pB->pList==0 ) return 0;
1603 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
1604 for(i=0; i<pA->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00001605 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00001606 return 0;
1607 }
1608 }
1609 }else if( pB->pList ){
1610 return 0;
1611 }
1612 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00001613 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drh22827922000-06-06 17:27:05 +00001614 if( pA->token.z ){
1615 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00001616 if( pB->token.n!=pA->token.n ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001617 if( sqlite3StrNICmp(pA->token.z, pB->token.z, pB->token.n)!=0 ) return 0;
drh22827922000-06-06 17:27:05 +00001618 }
1619 return 1;
1620}
1621
1622/*
1623** Add a new element to the pParse->aAgg[] array and return its index.
1624*/
1625static int appendAggInfo(Parse *pParse){
1626 if( (pParse->nAgg & 0x7)==0 ){
1627 int amt = pParse->nAgg + 8;
drh6d4abfb2001-10-22 02:58:08 +00001628 AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0]));
1629 if( aAgg==0 ){
drh22827922000-06-06 17:27:05 +00001630 return -1;
1631 }
drh6d4abfb2001-10-22 02:58:08 +00001632 pParse->aAgg = aAgg;
drh22827922000-06-06 17:27:05 +00001633 }
1634 memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0]));
1635 return pParse->nAgg++;
1636}
1637
1638/*
1639** Analyze the given expression looking for aggregate functions and
1640** for variables that need to be added to the pParse->aAgg[] array.
1641** Make additional entries to the pParse->aAgg[] array as necessary.
1642**
1643** This routine should only be called after the expression has been
danielk19774adee202004-05-08 08:23:19 +00001644** analyzed by sqlite3ExprResolveIds() and sqlite3ExprCheck().
drh22827922000-06-06 17:27:05 +00001645**
1646** If errors are seen, leave an error message in zErrMsg and return
1647** the number of errors.
1648*/
danielk19774adee202004-05-08 08:23:19 +00001649int sqlite3ExprAnalyzeAggregates(Parse *pParse, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00001650 int i;
1651 AggExpr *aAgg;
1652 int nErr = 0;
1653
1654 if( pExpr==0 ) return 0;
1655 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00001656 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +00001657 aAgg = pParse->aAgg;
1658 for(i=0; i<pParse->nAgg; i++){
1659 if( aAgg[i].isAgg ) continue;
1660 if( aAgg[i].pExpr->iTable==pExpr->iTable
drh967e8b72000-06-21 13:59:10 +00001661 && aAgg[i].pExpr->iColumn==pExpr->iColumn ){
drh22827922000-06-06 17:27:05 +00001662 break;
1663 }
1664 }
1665 if( i>=pParse->nAgg ){
1666 i = appendAggInfo(pParse);
1667 if( i<0 ) return 1;
1668 pParse->aAgg[i].isAgg = 0;
1669 pParse->aAgg[i].pExpr = pExpr;
1670 }
drhaaf88722000-06-08 11:25:00 +00001671 pExpr->iAgg = i;
drh22827922000-06-06 17:27:05 +00001672 break;
1673 }
1674 case TK_AGG_FUNCTION: {
drh22827922000-06-06 17:27:05 +00001675 aAgg = pParse->aAgg;
1676 for(i=0; i<pParse->nAgg; i++){
1677 if( !aAgg[i].isAgg ) continue;
danielk19774adee202004-05-08 08:23:19 +00001678 if( sqlite3ExprCompare(aAgg[i].pExpr, pExpr) ){
drh22827922000-06-06 17:27:05 +00001679 break;
1680 }
1681 }
1682 if( i>=pParse->nAgg ){
danielk1977d8123362004-06-12 09:25:12 +00001683 u8 enc = pParse->db->enc;
drh22827922000-06-06 17:27:05 +00001684 i = appendAggInfo(pParse);
1685 if( i<0 ) return 1;
1686 pParse->aAgg[i].isAgg = 1;
1687 pParse->aAgg[i].pExpr = pExpr;
danielk19774adee202004-05-08 08:23:19 +00001688 pParse->aAgg[i].pFunc = sqlite3FindFunction(pParse->db,
drh6977fea2002-10-22 23:38:04 +00001689 pExpr->token.z, pExpr->token.n,
danielk1977d8123362004-06-12 09:25:12 +00001690 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
drh22827922000-06-06 17:27:05 +00001691 }
1692 pExpr->iAgg = i;
1693 break;
1694 }
1695 default: {
1696 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001697 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pLeft);
drh22827922000-06-06 17:27:05 +00001698 }
1699 if( nErr==0 && pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001700 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pRight);
drh22827922000-06-06 17:27:05 +00001701 }
1702 if( nErr==0 && pExpr->pList ){
1703 int n = pExpr->pList->nExpr;
1704 int i;
1705 for(i=0; nErr==0 && i<n; i++){
danielk19774adee202004-05-08 08:23:19 +00001706 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pList->a[i].pExpr);
drh22827922000-06-06 17:27:05 +00001707 }
1708 }
1709 break;
1710 }
1711 }
1712 return nErr;
1713}
drh8e0a2f92002-02-23 23:45:45 +00001714
1715/*
danielk1977d02eb1f2004-06-06 09:44:03 +00001716** Locate a user function given a name, a number of arguments and a flag
1717** indicating whether the function prefers UTF-16 over UTF-8. Return a
1718** pointer to the FuncDef structure that defines that function, or return
1719** NULL if the function does not exist.
drh8e0a2f92002-02-23 23:45:45 +00001720**
drh0bce8352002-02-28 00:41:10 +00001721** If the createFlag argument is true, then a new (blank) FuncDef
drh8e0a2f92002-02-23 23:45:45 +00001722** structure is created and liked into the "db" structure if a
1723** no matching function previously existed. When createFlag is true
1724** and the nArg parameter is -1, then only a function that accepts
1725** any number of arguments will be returned.
1726**
1727** If createFlag is false and nArg is -1, then the first valid
1728** function found is returned. A function is valid if either xFunc
1729** or xStep is non-zero.
danielk1977d02eb1f2004-06-06 09:44:03 +00001730**
1731** If createFlag is false, then a function with the required name and
1732** number of arguments may be returned even if the eTextRep flag does not
1733** match that requested.
drh8e0a2f92002-02-23 23:45:45 +00001734*/
danielk19774adee202004-05-08 08:23:19 +00001735FuncDef *sqlite3FindFunction(
drh8e0a2f92002-02-23 23:45:45 +00001736 sqlite *db, /* An open database */
1737 const char *zName, /* Name of the function. Not null-terminated */
1738 int nName, /* Number of characters in the name */
1739 int nArg, /* Number of arguments. -1 means any number */
danielk1977d8123362004-06-12 09:25:12 +00001740 u8 enc, /* Preferred text encoding */
drh8e0a2f92002-02-23 23:45:45 +00001741 int createFlag /* Create new entry if true and does not otherwise exist */
1742){
danielk1977d02eb1f2004-06-06 09:44:03 +00001743 FuncDef *p; /* Iterator variable */
1744 FuncDef *pFirst; /* First function with this name */
1745 FuncDef *pBest = 0; /* Best match found so far */
danielk1977d8123362004-06-12 09:25:12 +00001746 int bestmatch = 0;
danielk1977d02eb1f2004-06-06 09:44:03 +00001747
danielk1977d8123362004-06-12 09:25:12 +00001748
1749 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
danielk1977d02eb1f2004-06-06 09:44:03 +00001750 if( nArg<-1 ) nArg = -1;
1751
1752 pFirst = (FuncDef*)sqlite3HashFind(&db->aFunc, zName, nName);
1753 for(p=pFirst; p; p=p->pNext){
danielk1977d8123362004-06-12 09:25:12 +00001754 /* During the search for the best function definition, bestmatch is set
1755 ** as follows to indicate the quality of the match with the definition
1756 ** pointed to by pBest:
1757 **
1758 ** 0: pBest is NULL. No match has been found.
1759 ** 1: A variable arguments function that prefers UTF-8 when a UTF-16
1760 ** encoding is requested, or vice versa.
1761 ** 2: A variable arguments function that uses UTF-16BE when UTF-16LE is
1762 ** requested, or vice versa.
1763 ** 3: A variable arguments function using the same text encoding.
1764 ** 4: A function with the exact number of arguments requested that
1765 ** prefers UTF-8 when a UTF-16 encoding is requested, or vice versa.
1766 ** 5: A function with the exact number of arguments requested that
1767 ** prefers UTF-16LE when UTF-16BE is requested, or vice versa.
1768 ** 6: An exact match.
1769 **
1770 ** A larger value of 'matchqual' indicates a more desirable match.
1771 */
danielk1977e12c17b2004-06-23 12:35:14 +00001772 if( p->nArg==-1 || p->nArg==nArg || nArg==-1 ){
danielk1977d8123362004-06-12 09:25:12 +00001773 int match = 1; /* Quality of this match */
1774 if( p->nArg==nArg || nArg==-1 ){
1775 match = 4;
danielk1977d02eb1f2004-06-06 09:44:03 +00001776 }
danielk1977d8123362004-06-12 09:25:12 +00001777 if( enc==p->iPrefEnc ){
1778 match += 2;
danielk1977d02eb1f2004-06-06 09:44:03 +00001779 }
danielk1977d8123362004-06-12 09:25:12 +00001780 else if( (enc==SQLITE_UTF16LE && p->iPrefEnc==SQLITE_UTF16BE) ||
1781 (enc==SQLITE_UTF16BE && p->iPrefEnc==SQLITE_UTF16LE) ){
1782 match += 1;
1783 }
1784
1785 if( match>bestmatch ){
1786 pBest = p;
1787 bestmatch = match;
danielk1977d02eb1f2004-06-06 09:44:03 +00001788 }
1789 }
drh8e0a2f92002-02-23 23:45:45 +00001790 }
danielk1977d02eb1f2004-06-06 09:44:03 +00001791
danielk1977d8123362004-06-12 09:25:12 +00001792 /* If the createFlag parameter is true, and the seach did not reveal an
1793 ** exact match for the name, number of arguments and encoding, then add a
1794 ** new entry to the hash table and return it.
1795 */
1796 if( createFlag && bestmatch<6 &&
danielk1977d02eb1f2004-06-06 09:44:03 +00001797 (pBest = sqliteMalloc(sizeof(*pBest)+nName+1)) ){
1798 pBest->nArg = nArg;
1799 pBest->pNext = pFirst;
1800 pBest->zName = (char*)&pBest[1];
danielk1977d8123362004-06-12 09:25:12 +00001801 pBest->iPrefEnc = enc;
danielk1977d02eb1f2004-06-06 09:44:03 +00001802 memcpy(pBest->zName, zName, nName);
1803 pBest->zName[nName] = 0;
1804 sqlite3HashInsert(&db->aFunc, pBest->zName, nName, (void*)pBest);
drh8e0a2f92002-02-23 23:45:45 +00001805 }
danielk1977d02eb1f2004-06-06 09:44:03 +00001806
1807 if( pBest && (pBest->xStep || pBest->xFunc || createFlag) ){
1808 return pBest;
drh8e0a2f92002-02-23 23:45:45 +00001809 }
danielk1977d02eb1f2004-06-06 09:44:03 +00001810 return 0;
drh8e0a2f92002-02-23 23:45:45 +00001811}