blob: e8dfc2baf40f1e52b99599212e50bf7532ae0f8c [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**
drh855eb1c2004-08-31 13:45:11 +000015** $Id: expr.c,v 1.158 2004/08/31 13:45:12 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/*
drh290c1942004-08-21 17:54:45 +00001083** Call sqlite3ExprResolveIds() followed by sqlite3ExprCheck().
1084**
1085** This routine is provided as a convenience since it is very common
1086** to call ResolveIds() and Check() back to back.
1087*/
1088int sqlite3ExprResolveAndCheck(
1089 Parse *pParse, /* The parser context */
1090 SrcList *pSrcList, /* List of tables used to resolve column names */
1091 ExprList *pEList, /* List of expressions used to resolve "AS" */
1092 Expr *pExpr, /* The expression to be analyzed. */
1093 int allowAgg, /* True to allow aggregate expressions */
1094 int *pIsAgg /* Set to TRUE if aggregates are found */
1095){
1096 if( pExpr==0 ) return 0;
1097 if( sqlite3ExprResolveIds(pParse,pSrcList,pEList,pExpr) ){
1098 return 1;
1099 }
1100 return sqlite3ExprCheck(pParse, pExpr, allowAgg, pIsAgg);
1101}
1102
1103/*
drhfec19aa2004-05-19 20:41:03 +00001104** Generate an instruction that will put the integer describe by
1105** text z[0..n-1] on the stack.
1106*/
1107static void codeInteger(Vdbe *v, const char *z, int n){
1108 int i;
drh6fec0762004-05-30 01:38:43 +00001109 if( sqlite3GetInt32(z, &i) ){
1110 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
1111 }else if( sqlite3FitsIn64Bits(z) ){
1112 sqlite3VdbeOp3(v, OP_Integer, 0, 0, z, n);
drhfec19aa2004-05-19 20:41:03 +00001113 }else{
1114 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n);
1115 }
1116}
1117
1118/*
drhcce7d172000-05-31 15:34:51 +00001119** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +00001120** expression and leave the result on the top of stack.
drhcce7d172000-05-31 15:34:51 +00001121*/
danielk19774adee202004-05-08 08:23:19 +00001122void sqlite3ExprCode(Parse *pParse, Expr *pExpr){
drhcce7d172000-05-31 15:34:51 +00001123 Vdbe *v = pParse->pVdbe;
1124 int op;
drhdaffd0e2001-04-11 14:28:42 +00001125 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001126 switch( pExpr->op ){
1127 case TK_PLUS: op = OP_Add; break;
1128 case TK_MINUS: op = OP_Subtract; break;
1129 case TK_STAR: op = OP_Multiply; break;
1130 case TK_SLASH: op = OP_Divide; break;
1131 case TK_AND: op = OP_And; break;
1132 case TK_OR: op = OP_Or; break;
1133 case TK_LT: op = OP_Lt; break;
1134 case TK_LE: op = OP_Le; break;
1135 case TK_GT: op = OP_Gt; break;
1136 case TK_GE: op = OP_Ge; break;
1137 case TK_NE: op = OP_Ne; break;
1138 case TK_EQ: op = OP_Eq; break;
drhcce7d172000-05-31 15:34:51 +00001139 case TK_ISNULL: op = OP_IsNull; break;
1140 case TK_NOTNULL: op = OP_NotNull; break;
1141 case TK_NOT: op = OP_Not; break;
1142 case TK_UMINUS: op = OP_Negative; break;
drhbf4133c2001-10-13 02:59:08 +00001143 case TK_BITAND: op = OP_BitAnd; break;
1144 case TK_BITOR: op = OP_BitOr; break;
1145 case TK_BITNOT: op = OP_BitNot; break;
1146 case TK_LSHIFT: op = OP_ShiftLeft; break;
1147 case TK_RSHIFT: op = OP_ShiftRight; break;
1148 case TK_REM: op = OP_Remainder; break;
drhfec19aa2004-05-19 20:41:03 +00001149 case TK_FLOAT: op = OP_Real; break;
drh855eb1c2004-08-31 13:45:11 +00001150 case TK_STRING: op = OP_String8; break;
danielk1977c572ef72004-05-27 09:28:41 +00001151 case TK_BLOB: op = OP_HexBlob; break;
drh855eb1c2004-08-31 13:45:11 +00001152 case TK_CONCAT: op = OP_Concat; break;
danielk1977cfe9a692004-06-16 12:00:29 +00001153 default: op = 0; break;
drhcce7d172000-05-31 15:34:51 +00001154 }
1155 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00001156 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +00001157 if( pParse->useAgg ){
danielk19774adee202004-05-08 08:23:19 +00001158 sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drhc4a3c772001-04-04 11:48:57 +00001159 }else if( pExpr->iColumn>=0 ){
danielk19774adee202004-05-08 08:23:19 +00001160 sqlite3VdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn);
drhc4a3c772001-04-04 11:48:57 +00001161 }else{
danielk19774adee202004-05-08 08:23:19 +00001162 sqlite3VdbeAddOp(v, OP_Recno, pExpr->iTable, 0);
drh22827922000-06-06 17:27:05 +00001163 }
drhcce7d172000-05-31 15:34:51 +00001164 break;
1165 }
1166 case TK_INTEGER: {
drhfec19aa2004-05-19 20:41:03 +00001167 codeInteger(v, pExpr->token.z, pExpr->token.n);
1168 break;
1169 }
1170 case TK_FLOAT:
1171 case TK_STRING: {
1172 sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z, pExpr->token.n);
danielk19774adee202004-05-08 08:23:19 +00001173 sqlite3VdbeDequoteP3(v, -1);
drhcce7d172000-05-31 15:34:51 +00001174 break;
1175 }
danielk1977c572ef72004-05-27 09:28:41 +00001176 case TK_BLOB: {
1177 sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z+1, pExpr->token.n-1);
1178 sqlite3VdbeDequoteP3(v, -1);
1179 break;
1180 }
drhcce7d172000-05-31 15:34:51 +00001181 case TK_NULL: {
danielk19770f69c1e2004-05-29 11:24:50 +00001182 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001183 break;
1184 }
drh50457892003-09-06 01:10:47 +00001185 case TK_VARIABLE: {
danielk19774adee202004-05-08 08:23:19 +00001186 sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0);
drh895d7472004-08-20 16:02:39 +00001187 if( pExpr->token.n>1 ){
1188 sqlite3VdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
1189 }
drh50457892003-09-06 01:10:47 +00001190 break;
1191 }
drhc9b84a12002-06-20 11:36:48 +00001192 case TK_LT:
1193 case TK_LE:
1194 case TK_GT:
1195 case TK_GE:
1196 case TK_NE:
1197 case TK_EQ: {
danielk1977a37cdde2004-05-16 11:15:36 +00001198 sqlite3ExprCode(pParse, pExpr->pLeft);
1199 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001200 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 0, 0);
danielk1977a37cdde2004-05-16 11:15:36 +00001201 break;
drhc9b84a12002-06-20 11:36:48 +00001202 }
drhcce7d172000-05-31 15:34:51 +00001203 case TK_AND:
1204 case TK_OR:
1205 case TK_PLUS:
1206 case TK_STAR:
1207 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00001208 case TK_REM:
1209 case TK_BITAND:
1210 case TK_BITOR:
drh17c40292004-07-21 02:53:29 +00001211 case TK_SLASH:
drhbf4133c2001-10-13 02:59:08 +00001212 case TK_LSHIFT:
drh855eb1c2004-08-31 13:45:11 +00001213 case TK_RSHIFT:
drh00400772000-06-16 20:51:26 +00001214 case TK_CONCAT: {
danielk19774adee202004-05-08 08:23:19 +00001215 sqlite3ExprCode(pParse, pExpr->pLeft);
1216 sqlite3ExprCode(pParse, pExpr->pRight);
drh855eb1c2004-08-31 13:45:11 +00001217 sqlite3VdbeAddOp(v, op, 0, 0);
drh00400772000-06-16 20:51:26 +00001218 break;
1219 }
drhcce7d172000-05-31 15:34:51 +00001220 case TK_UMINUS: {
drhfec19aa2004-05-19 20:41:03 +00001221 Expr *pLeft = pExpr->pLeft;
1222 assert( pLeft );
1223 if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
1224 Token *p = &pLeft->token;
drh6e142f52000-06-08 13:36:40 +00001225 char *z = sqliteMalloc( p->n + 2 );
1226 sprintf(z, "-%.*s", p->n, p->z);
drhfec19aa2004-05-19 20:41:03 +00001227 if( pLeft->op==TK_FLOAT ){
1228 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001229 }else{
drhfec19aa2004-05-19 20:41:03 +00001230 codeInteger(v, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001231 }
drh6e142f52000-06-08 13:36:40 +00001232 sqliteFree(z);
1233 break;
1234 }
drh1ccde152000-06-17 13:12:39 +00001235 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +00001236 }
drhbf4133c2001-10-13 02:59:08 +00001237 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00001238 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001239 sqlite3ExprCode(pParse, pExpr->pLeft);
1240 sqlite3VdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001241 break;
1242 }
1243 case TK_ISNULL:
1244 case TK_NOTNULL: {
1245 int dest;
danielk19774adee202004-05-08 08:23:19 +00001246 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1247 sqlite3ExprCode(pParse, pExpr->pLeft);
1248 dest = sqlite3VdbeCurrentAddr(v) + 2;
1249 sqlite3VdbeAddOp(v, op, 1, dest);
1250 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);
drhcce7d172000-05-31 15:34:51 +00001251 }
danielk1977a37cdde2004-05-16 11:15:36 +00001252 break;
drh22827922000-06-06 17:27:05 +00001253 case TK_AGG_FUNCTION: {
danielk19774adee202004-05-08 08:23:19 +00001254 sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drh22827922000-06-06 17:27:05 +00001255 break;
1256 }
drh4b59ab52002-08-24 18:24:51 +00001257 case TK_GLOB:
1258 case TK_LIKE:
drhcce7d172000-05-31 15:34:51 +00001259 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00001260 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00001261 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00001262 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00001263 int nId;
1264 const char *zId;
danielk1977682f68b2004-06-05 10:22:17 +00001265 int p2 = 0;
1266 int i;
danielk1977d8123362004-06-12 09:25:12 +00001267 u8 enc = pParse->db->enc;
danielk1977dc1bdc42004-06-11 10:51:27 +00001268 CollSeq *pColl = 0;
drh4b59ab52002-08-24 18:24:51 +00001269 getFunctionName(pExpr, &zId, &nId);
danielk1977d8123362004-06-12 09:25:12 +00001270 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001271 assert( pDef!=0 );
drhf9b596e2004-05-26 16:54:42 +00001272 nExpr = sqlite3ExprCodeExprList(pParse, pList);
danielk1977682f68b2004-06-05 10:22:17 +00001273 for(i=0; i<nExpr && i<32; i++){
danielk1977d02eb1f2004-06-06 09:44:03 +00001274 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
1275 p2 |= (1<<i);
1276 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001277 if( pDef->needCollSeq && !pColl ){
1278 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
1279 }
1280 }
1281 if( pDef->needCollSeq ){
1282 if( !pColl ) pColl = pParse->db->pDfltColl;
danielk1977d8123362004-06-12 09:25:12 +00001283 sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ);
danielk1977682f68b2004-06-05 10:22:17 +00001284 }
1285 sqlite3VdbeOp3(v, OP_Function, nExpr, p2, (char*)pDef, P3_FUNCDEF);
drhcce7d172000-05-31 15:34:51 +00001286 break;
1287 }
drh19a775c2000-06-05 18:54:46 +00001288 case TK_SELECT: {
danielk19774adee202004-05-08 08:23:19 +00001289 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drh19a775c2000-06-05 18:54:46 +00001290 break;
1291 }
drhfef52082000-06-06 01:50:43 +00001292 case TK_IN: {
1293 int addr;
danielk1977e014a832004-05-17 10:48:57 +00001294 char const *affStr;
1295
1296 /* Figure out the affinity to use to create a key from the results
1297 ** of the expression. affinityStr stores a static string suitable for
danielk1977ededfd52004-06-17 07:53:01 +00001298 ** P3 of OP_MakeRecord.
danielk1977e014a832004-05-17 10:48:57 +00001299 */
1300 affStr = sqlite3AffinityString(comparisonAffinity(pExpr));
1301
danielk19774adee202004-05-08 08:23:19 +00001302 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
danielk1977e014a832004-05-17 10:48:57 +00001303
1304 /* Code the <expr> from "<expr> IN (...)". The temporary table
1305 ** pExpr->iTable contains the values that make up the (...) set.
1306 */
danielk19774adee202004-05-08 08:23:19 +00001307 sqlite3ExprCode(pParse, pExpr->pLeft);
1308 addr = sqlite3VdbeCurrentAddr(v);
danielk1977e014a832004-05-17 10:48:57 +00001309 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); /* addr + 0 */
danielk19774adee202004-05-08 08:23:19 +00001310 sqlite3VdbeAddOp(v, OP_Pop, 2, 0);
danielk19770f69c1e2004-05-29 11:24:50 +00001311 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +00001312 sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7);
danielk1977ededfd52004-06-17 07:53:01 +00001313 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, affStr, P3_STATIC); /* addr + 4 */
danielk1977e014a832004-05-17 10:48:57 +00001314 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7);
1315 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); /* addr + 6 */
1316
drhfef52082000-06-06 01:50:43 +00001317 break;
1318 }
1319 case TK_BETWEEN: {
drhbe5c89a2004-07-26 00:31:09 +00001320 Expr *pLeft = pExpr->pLeft;
1321 struct ExprList_item *pLItem = pExpr->pList->a;
1322 Expr *pRight = pLItem->pExpr;
1323 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001324 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001325 sqlite3ExprCode(pParse, pRight);
1326 codeCompare(pParse, pLeft, pRight, OP_Ge, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001327 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
drhbe5c89a2004-07-26 00:31:09 +00001328 pLItem++;
1329 pRight = pLItem->pExpr;
1330 sqlite3ExprCode(pParse, pRight);
1331 codeCompare(pParse, pLeft, pRight, OP_Le, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001332 sqlite3VdbeAddOp(v, OP_And, 0, 0);
drhfef52082000-06-06 01:50:43 +00001333 break;
1334 }
drh51e9a442004-01-16 16:42:53 +00001335 case TK_UPLUS:
drha2e00042002-01-22 03:13:42 +00001336 case TK_AS: {
danielk19774adee202004-05-08 08:23:19 +00001337 sqlite3ExprCode(pParse, pExpr->pLeft);
drha2e00042002-01-22 03:13:42 +00001338 break;
1339 }
drh17a7f8d2002-03-24 13:13:27 +00001340 case TK_CASE: {
1341 int expr_end_label;
drhf5905aa2002-05-26 20:54:33 +00001342 int jumpInst;
1343 int addr;
1344 int nExpr;
drh17a7f8d2002-03-24 13:13:27 +00001345 int i;
drhbe5c89a2004-07-26 00:31:09 +00001346 ExprList *pEList;
1347 struct ExprList_item *aListelem;
drh17a7f8d2002-03-24 13:13:27 +00001348
1349 assert(pExpr->pList);
1350 assert((pExpr->pList->nExpr % 2) == 0);
1351 assert(pExpr->pList->nExpr > 0);
drhbe5c89a2004-07-26 00:31:09 +00001352 pEList = pExpr->pList;
1353 aListelem = pEList->a;
1354 nExpr = pEList->nExpr;
danielk19774adee202004-05-08 08:23:19 +00001355 expr_end_label = sqlite3VdbeMakeLabel(v);
drh17a7f8d2002-03-24 13:13:27 +00001356 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001357 sqlite3ExprCode(pParse, pExpr->pLeft);
drh17a7f8d2002-03-24 13:13:27 +00001358 }
drhf5905aa2002-05-26 20:54:33 +00001359 for(i=0; i<nExpr; i=i+2){
drhbe5c89a2004-07-26 00:31:09 +00001360 sqlite3ExprCode(pParse, aListelem[i].pExpr);
drh17a7f8d2002-03-24 13:13:27 +00001361 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001362 sqlite3VdbeAddOp(v, OP_Dup, 1, 1);
drhbe5c89a2004-07-26 00:31:09 +00001363 jumpInst = codeCompare(pParse, pExpr->pLeft, aListelem[i].pExpr,
1364 OP_Ne, 0, 1);
danielk19774adee202004-05-08 08:23:19 +00001365 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001366 }else{
danielk19774adee202004-05-08 08:23:19 +00001367 jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001368 }
drhbe5c89a2004-07-26 00:31:09 +00001369 sqlite3ExprCode(pParse, aListelem[i+1].pExpr);
danielk19774adee202004-05-08 08:23:19 +00001370 sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label);
1371 addr = sqlite3VdbeCurrentAddr(v);
1372 sqlite3VdbeChangeP2(v, jumpInst, addr);
drh17a7f8d2002-03-24 13:13:27 +00001373 }
drhf570f012002-05-31 15:51:25 +00001374 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001375 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhf570f012002-05-31 15:51:25 +00001376 }
drh17a7f8d2002-03-24 13:13:27 +00001377 if( pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001378 sqlite3ExprCode(pParse, pExpr->pRight);
drh17a7f8d2002-03-24 13:13:27 +00001379 }else{
danielk19770f69c1e2004-05-29 11:24:50 +00001380 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
drh17a7f8d2002-03-24 13:13:27 +00001381 }
danielk19774adee202004-05-08 08:23:19 +00001382 sqlite3VdbeResolveLabel(v, expr_end_label);
danielk19776f349032002-06-11 02:25:40 +00001383 break;
1384 }
1385 case TK_RAISE: {
1386 if( !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +00001387 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00001388 "RAISE() may only be used within a trigger-program");
danielk19776f349032002-06-11 02:25:40 +00001389 pParse->nErr++;
1390 return;
1391 }
1392 if( pExpr->iColumn == OE_Rollback ||
1393 pExpr->iColumn == OE_Abort ||
1394 pExpr->iColumn == OE_Fail ){
danielk19774adee202004-05-08 08:23:19 +00001395 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn,
drh701a0ae2004-02-22 20:05:00 +00001396 pExpr->token.z, pExpr->token.n);
danielk19774adee202004-05-08 08:23:19 +00001397 sqlite3VdbeDequoteP3(v, -1);
danielk19776f349032002-06-11 02:25:40 +00001398 } else {
1399 assert( pExpr->iColumn == OE_Ignore );
danielk19774adee202004-05-08 08:23:19 +00001400 sqlite3VdbeOp3(v, OP_Goto, 0, pParse->trigStack->ignoreJump,
drh701a0ae2004-02-22 20:05:00 +00001401 "(IGNORE jump)", 0);
danielk19776f349032002-06-11 02:25:40 +00001402 }
drh17a7f8d2002-03-24 13:13:27 +00001403 }
1404 break;
drhcce7d172000-05-31 15:34:51 +00001405 }
drhcce7d172000-05-31 15:34:51 +00001406}
1407
1408/*
drh268380c2004-02-25 13:47:31 +00001409** Generate code that pushes the value of every element of the given
drhf9b596e2004-05-26 16:54:42 +00001410** expression list onto the stack.
drh268380c2004-02-25 13:47:31 +00001411**
1412** Return the number of elements pushed onto the stack.
1413*/
danielk19774adee202004-05-08 08:23:19 +00001414int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00001415 Parse *pParse, /* Parsing context */
drhf9b596e2004-05-26 16:54:42 +00001416 ExprList *pList /* The expression list to be coded */
drh268380c2004-02-25 13:47:31 +00001417){
1418 struct ExprList_item *pItem;
1419 int i, n;
1420 Vdbe *v;
1421 if( pList==0 ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001422 v = sqlite3GetVdbe(pParse);
drh268380c2004-02-25 13:47:31 +00001423 n = pList->nExpr;
1424 for(pItem=pList->a, i=0; i<n; i++, pItem++){
danielk19774adee202004-05-08 08:23:19 +00001425 sqlite3ExprCode(pParse, pItem->pExpr);
drh268380c2004-02-25 13:47:31 +00001426 }
drhf9b596e2004-05-26 16:54:42 +00001427 return n;
drh268380c2004-02-25 13:47:31 +00001428}
1429
1430/*
drhcce7d172000-05-31 15:34:51 +00001431** Generate code for a boolean expression such that a jump is made
1432** to the label "dest" if the expression is true but execution
1433** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00001434**
1435** If the expression evaluates to NULL (neither true nor false), then
1436** take the jump if the jumpIfNull flag is true.
drhcce7d172000-05-31 15:34:51 +00001437*/
danielk19774adee202004-05-08 08:23:19 +00001438void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001439 Vdbe *v = pParse->pVdbe;
1440 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001441 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001442 switch( pExpr->op ){
1443 case TK_LT: op = OP_Lt; break;
1444 case TK_LE: op = OP_Le; break;
1445 case TK_GT: op = OP_Gt; break;
1446 case TK_GE: op = OP_Ge; break;
1447 case TK_NE: op = OP_Ne; break;
1448 case TK_EQ: op = OP_Eq; break;
drhcce7d172000-05-31 15:34:51 +00001449 case TK_ISNULL: op = OP_IsNull; break;
1450 case TK_NOTNULL: op = OP_NotNull; break;
1451 default: break;
1452 }
1453 switch( pExpr->op ){
1454 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001455 int d2 = sqlite3VdbeMakeLabel(v);
1456 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
1457 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
1458 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001459 break;
1460 }
1461 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001462 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
1463 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001464 break;
1465 }
1466 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001467 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001468 break;
1469 }
1470 case TK_LT:
1471 case TK_LE:
1472 case TK_GT:
1473 case TK_GE:
1474 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00001475 case TK_EQ: {
danielk19774adee202004-05-08 08:23:19 +00001476 sqlite3ExprCode(pParse, pExpr->pLeft);
1477 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001478 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001479 break;
1480 }
1481 case TK_ISNULL:
1482 case TK_NOTNULL: {
danielk19774adee202004-05-08 08:23:19 +00001483 sqlite3ExprCode(pParse, pExpr->pLeft);
1484 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001485 break;
1486 }
drhfef52082000-06-06 01:50:43 +00001487 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00001488 /* The expression "x BETWEEN y AND z" is implemented as:
1489 **
1490 ** 1 IF (x < y) GOTO 3
1491 ** 2 IF (x <= z) GOTO <dest>
1492 ** 3 ...
1493 */
drhf5905aa2002-05-26 20:54:33 +00001494 int addr;
drhbe5c89a2004-07-26 00:31:09 +00001495 Expr *pLeft = pExpr->pLeft;
1496 Expr *pRight = pExpr->pList->a[0].pExpr;
1497 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001498 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001499 sqlite3ExprCode(pParse, pRight);
1500 addr = codeCompare(pParse, pLeft, pRight, OP_Lt, 0, !jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00001501
drhbe5c89a2004-07-26 00:31:09 +00001502 pRight = pExpr->pList->a[1].pExpr;
1503 sqlite3ExprCode(pParse, pRight);
1504 codeCompare(pParse, pLeft, pRight, OP_Le, dest, jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00001505
danielk19774adee202004-05-08 08:23:19 +00001506 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
1507 sqlite3VdbeChangeP2(v, addr, sqlite3VdbeCurrentAddr(v));
1508 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00001509 break;
1510 }
drhcce7d172000-05-31 15:34:51 +00001511 default: {
danielk19774adee202004-05-08 08:23:19 +00001512 sqlite3ExprCode(pParse, pExpr);
1513 sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001514 break;
1515 }
1516 }
1517}
1518
1519/*
drh66b89c82000-11-28 20:47:17 +00001520** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00001521** to the label "dest" if the expression is false but execution
1522** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00001523**
1524** If the expression evaluates to NULL (neither true nor false) then
1525** jump if jumpIfNull is true or fall through if jumpIfNull is false.
drhcce7d172000-05-31 15:34:51 +00001526*/
danielk19774adee202004-05-08 08:23:19 +00001527void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001528 Vdbe *v = pParse->pVdbe;
1529 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001530 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001531 switch( pExpr->op ){
1532 case TK_LT: op = OP_Ge; break;
1533 case TK_LE: op = OP_Gt; break;
1534 case TK_GT: op = OP_Le; break;
1535 case TK_GE: op = OP_Lt; break;
1536 case TK_NE: op = OP_Eq; break;
1537 case TK_EQ: op = OP_Ne; break;
drhcce7d172000-05-31 15:34:51 +00001538 case TK_ISNULL: op = OP_NotNull; break;
1539 case TK_NOTNULL: op = OP_IsNull; break;
1540 default: break;
1541 }
1542 switch( pExpr->op ){
1543 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001544 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
1545 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001546 break;
1547 }
1548 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001549 int d2 = sqlite3VdbeMakeLabel(v);
1550 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
1551 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
1552 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001553 break;
1554 }
1555 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001556 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001557 break;
1558 }
1559 case TK_LT:
1560 case TK_LE:
1561 case TK_GT:
1562 case TK_GE:
1563 case TK_NE:
1564 case TK_EQ: {
danielk19774adee202004-05-08 08:23:19 +00001565 sqlite3ExprCode(pParse, pExpr->pLeft);
1566 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001567 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001568 break;
1569 }
drhcce7d172000-05-31 15:34:51 +00001570 case TK_ISNULL:
1571 case TK_NOTNULL: {
danielk19774adee202004-05-08 08:23:19 +00001572 sqlite3ExprCode(pParse, pExpr->pLeft);
1573 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001574 break;
1575 }
drhfef52082000-06-06 01:50:43 +00001576 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00001577 /* The expression is "x BETWEEN y AND z". It is implemented as:
1578 **
1579 ** 1 IF (x >= y) GOTO 3
1580 ** 2 GOTO <dest>
1581 ** 3 IF (x > z) GOTO <dest>
1582 */
drhfef52082000-06-06 01:50:43 +00001583 int addr;
drhbe5c89a2004-07-26 00:31:09 +00001584 Expr *pLeft = pExpr->pLeft;
1585 Expr *pRight = pExpr->pList->a[0].pExpr;
1586 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001587 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001588 sqlite3ExprCode(pParse, pRight);
danielk19774adee202004-05-08 08:23:19 +00001589 addr = sqlite3VdbeCurrentAddr(v);
drhbe5c89a2004-07-26 00:31:09 +00001590 codeCompare(pParse, pLeft, pRight, OP_Ge, addr+3, !jumpIfNull);
1591
danielk19774adee202004-05-08 08:23:19 +00001592 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
1593 sqlite3VdbeAddOp(v, OP_Goto, 0, dest);
drhbe5c89a2004-07-26 00:31:09 +00001594 pRight = pExpr->pList->a[1].pExpr;
1595 sqlite3ExprCode(pParse, pRight);
1596 codeCompare(pParse, pLeft, pRight, OP_Gt, dest, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00001597 break;
1598 }
drhcce7d172000-05-31 15:34:51 +00001599 default: {
danielk19774adee202004-05-08 08:23:19 +00001600 sqlite3ExprCode(pParse, pExpr);
1601 sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001602 break;
1603 }
1604 }
1605}
drh22827922000-06-06 17:27:05 +00001606
1607/*
1608** Do a deep comparison of two expression trees. Return TRUE (non-zero)
1609** if they are identical and return FALSE if they differ in any way.
1610*/
danielk19774adee202004-05-08 08:23:19 +00001611int sqlite3ExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00001612 int i;
1613 if( pA==0 ){
1614 return pB==0;
1615 }else if( pB==0 ){
1616 return 0;
1617 }
1618 if( pA->op!=pB->op ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001619 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
1620 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00001621 if( pA->pList ){
1622 if( pB->pList==0 ) return 0;
1623 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
1624 for(i=0; i<pA->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00001625 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00001626 return 0;
1627 }
1628 }
1629 }else if( pB->pList ){
1630 return 0;
1631 }
1632 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00001633 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drh22827922000-06-06 17:27:05 +00001634 if( pA->token.z ){
1635 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00001636 if( pB->token.n!=pA->token.n ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001637 if( sqlite3StrNICmp(pA->token.z, pB->token.z, pB->token.n)!=0 ) return 0;
drh22827922000-06-06 17:27:05 +00001638 }
1639 return 1;
1640}
1641
1642/*
1643** Add a new element to the pParse->aAgg[] array and return its index.
1644*/
1645static int appendAggInfo(Parse *pParse){
1646 if( (pParse->nAgg & 0x7)==0 ){
1647 int amt = pParse->nAgg + 8;
drh6d4abfb2001-10-22 02:58:08 +00001648 AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0]));
1649 if( aAgg==0 ){
drh22827922000-06-06 17:27:05 +00001650 return -1;
1651 }
drh6d4abfb2001-10-22 02:58:08 +00001652 pParse->aAgg = aAgg;
drh22827922000-06-06 17:27:05 +00001653 }
1654 memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0]));
1655 return pParse->nAgg++;
1656}
1657
1658/*
1659** Analyze the given expression looking for aggregate functions and
1660** for variables that need to be added to the pParse->aAgg[] array.
1661** Make additional entries to the pParse->aAgg[] array as necessary.
1662**
1663** This routine should only be called after the expression has been
danielk19774adee202004-05-08 08:23:19 +00001664** analyzed by sqlite3ExprResolveIds() and sqlite3ExprCheck().
drh22827922000-06-06 17:27:05 +00001665**
1666** If errors are seen, leave an error message in zErrMsg and return
1667** the number of errors.
1668*/
danielk19774adee202004-05-08 08:23:19 +00001669int sqlite3ExprAnalyzeAggregates(Parse *pParse, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00001670 int i;
1671 AggExpr *aAgg;
1672 int nErr = 0;
1673
1674 if( pExpr==0 ) return 0;
1675 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00001676 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +00001677 aAgg = pParse->aAgg;
1678 for(i=0; i<pParse->nAgg; i++){
1679 if( aAgg[i].isAgg ) continue;
1680 if( aAgg[i].pExpr->iTable==pExpr->iTable
drh967e8b72000-06-21 13:59:10 +00001681 && aAgg[i].pExpr->iColumn==pExpr->iColumn ){
drh22827922000-06-06 17:27:05 +00001682 break;
1683 }
1684 }
1685 if( i>=pParse->nAgg ){
1686 i = appendAggInfo(pParse);
1687 if( i<0 ) return 1;
1688 pParse->aAgg[i].isAgg = 0;
1689 pParse->aAgg[i].pExpr = pExpr;
1690 }
drhaaf88722000-06-08 11:25:00 +00001691 pExpr->iAgg = i;
drh22827922000-06-06 17:27:05 +00001692 break;
1693 }
1694 case TK_AGG_FUNCTION: {
drh22827922000-06-06 17:27:05 +00001695 aAgg = pParse->aAgg;
1696 for(i=0; i<pParse->nAgg; i++){
1697 if( !aAgg[i].isAgg ) continue;
danielk19774adee202004-05-08 08:23:19 +00001698 if( sqlite3ExprCompare(aAgg[i].pExpr, pExpr) ){
drh22827922000-06-06 17:27:05 +00001699 break;
1700 }
1701 }
1702 if( i>=pParse->nAgg ){
danielk1977d8123362004-06-12 09:25:12 +00001703 u8 enc = pParse->db->enc;
drh22827922000-06-06 17:27:05 +00001704 i = appendAggInfo(pParse);
1705 if( i<0 ) return 1;
1706 pParse->aAgg[i].isAgg = 1;
1707 pParse->aAgg[i].pExpr = pExpr;
danielk19774adee202004-05-08 08:23:19 +00001708 pParse->aAgg[i].pFunc = sqlite3FindFunction(pParse->db,
drh6977fea2002-10-22 23:38:04 +00001709 pExpr->token.z, pExpr->token.n,
danielk1977d8123362004-06-12 09:25:12 +00001710 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
drh22827922000-06-06 17:27:05 +00001711 }
1712 pExpr->iAgg = i;
1713 break;
1714 }
1715 default: {
1716 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001717 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pLeft);
drh22827922000-06-06 17:27:05 +00001718 }
1719 if( nErr==0 && pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001720 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pRight);
drh22827922000-06-06 17:27:05 +00001721 }
1722 if( nErr==0 && pExpr->pList ){
1723 int n = pExpr->pList->nExpr;
1724 int i;
1725 for(i=0; nErr==0 && i<n; i++){
danielk19774adee202004-05-08 08:23:19 +00001726 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pList->a[i].pExpr);
drh22827922000-06-06 17:27:05 +00001727 }
1728 }
1729 break;
1730 }
1731 }
1732 return nErr;
1733}
drh8e0a2f92002-02-23 23:45:45 +00001734
1735/*
danielk1977d02eb1f2004-06-06 09:44:03 +00001736** Locate a user function given a name, a number of arguments and a flag
1737** indicating whether the function prefers UTF-16 over UTF-8. Return a
1738** pointer to the FuncDef structure that defines that function, or return
1739** NULL if the function does not exist.
drh8e0a2f92002-02-23 23:45:45 +00001740**
drh0bce8352002-02-28 00:41:10 +00001741** If the createFlag argument is true, then a new (blank) FuncDef
drh8e0a2f92002-02-23 23:45:45 +00001742** structure is created and liked into the "db" structure if a
1743** no matching function previously existed. When createFlag is true
1744** and the nArg parameter is -1, then only a function that accepts
1745** any number of arguments will be returned.
1746**
1747** If createFlag is false and nArg is -1, then the first valid
1748** function found is returned. A function is valid if either xFunc
1749** or xStep is non-zero.
danielk1977d02eb1f2004-06-06 09:44:03 +00001750**
1751** If createFlag is false, then a function with the required name and
1752** number of arguments may be returned even if the eTextRep flag does not
1753** match that requested.
drh8e0a2f92002-02-23 23:45:45 +00001754*/
danielk19774adee202004-05-08 08:23:19 +00001755FuncDef *sqlite3FindFunction(
drh8e0a2f92002-02-23 23:45:45 +00001756 sqlite *db, /* An open database */
1757 const char *zName, /* Name of the function. Not null-terminated */
1758 int nName, /* Number of characters in the name */
1759 int nArg, /* Number of arguments. -1 means any number */
danielk1977d8123362004-06-12 09:25:12 +00001760 u8 enc, /* Preferred text encoding */
drh8e0a2f92002-02-23 23:45:45 +00001761 int createFlag /* Create new entry if true and does not otherwise exist */
1762){
danielk1977d02eb1f2004-06-06 09:44:03 +00001763 FuncDef *p; /* Iterator variable */
1764 FuncDef *pFirst; /* First function with this name */
1765 FuncDef *pBest = 0; /* Best match found so far */
danielk1977d8123362004-06-12 09:25:12 +00001766 int bestmatch = 0;
danielk1977d02eb1f2004-06-06 09:44:03 +00001767
danielk1977d8123362004-06-12 09:25:12 +00001768
1769 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
danielk1977d02eb1f2004-06-06 09:44:03 +00001770 if( nArg<-1 ) nArg = -1;
1771
1772 pFirst = (FuncDef*)sqlite3HashFind(&db->aFunc, zName, nName);
1773 for(p=pFirst; p; p=p->pNext){
danielk1977d8123362004-06-12 09:25:12 +00001774 /* During the search for the best function definition, bestmatch is set
1775 ** as follows to indicate the quality of the match with the definition
1776 ** pointed to by pBest:
1777 **
1778 ** 0: pBest is NULL. No match has been found.
1779 ** 1: A variable arguments function that prefers UTF-8 when a UTF-16
1780 ** encoding is requested, or vice versa.
1781 ** 2: A variable arguments function that uses UTF-16BE when UTF-16LE is
1782 ** requested, or vice versa.
1783 ** 3: A variable arguments function using the same text encoding.
1784 ** 4: A function with the exact number of arguments requested that
1785 ** prefers UTF-8 when a UTF-16 encoding is requested, or vice versa.
1786 ** 5: A function with the exact number of arguments requested that
1787 ** prefers UTF-16LE when UTF-16BE is requested, or vice versa.
1788 ** 6: An exact match.
1789 **
1790 ** A larger value of 'matchqual' indicates a more desirable match.
1791 */
danielk1977e12c17b2004-06-23 12:35:14 +00001792 if( p->nArg==-1 || p->nArg==nArg || nArg==-1 ){
danielk1977d8123362004-06-12 09:25:12 +00001793 int match = 1; /* Quality of this match */
1794 if( p->nArg==nArg || nArg==-1 ){
1795 match = 4;
danielk1977d02eb1f2004-06-06 09:44:03 +00001796 }
danielk1977d8123362004-06-12 09:25:12 +00001797 if( enc==p->iPrefEnc ){
1798 match += 2;
danielk1977d02eb1f2004-06-06 09:44:03 +00001799 }
danielk1977d8123362004-06-12 09:25:12 +00001800 else if( (enc==SQLITE_UTF16LE && p->iPrefEnc==SQLITE_UTF16BE) ||
1801 (enc==SQLITE_UTF16BE && p->iPrefEnc==SQLITE_UTF16LE) ){
1802 match += 1;
1803 }
1804
1805 if( match>bestmatch ){
1806 pBest = p;
1807 bestmatch = match;
danielk1977d02eb1f2004-06-06 09:44:03 +00001808 }
1809 }
drh8e0a2f92002-02-23 23:45:45 +00001810 }
danielk1977d02eb1f2004-06-06 09:44:03 +00001811
danielk1977d8123362004-06-12 09:25:12 +00001812 /* If the createFlag parameter is true, and the seach did not reveal an
1813 ** exact match for the name, number of arguments and encoding, then add a
1814 ** new entry to the hash table and return it.
1815 */
1816 if( createFlag && bestmatch<6 &&
danielk1977d02eb1f2004-06-06 09:44:03 +00001817 (pBest = sqliteMalloc(sizeof(*pBest)+nName+1)) ){
1818 pBest->nArg = nArg;
1819 pBest->pNext = pFirst;
1820 pBest->zName = (char*)&pBest[1];
danielk1977d8123362004-06-12 09:25:12 +00001821 pBest->iPrefEnc = enc;
danielk1977d02eb1f2004-06-06 09:44:03 +00001822 memcpy(pBest->zName, zName, nName);
1823 pBest->zName[nName] = 0;
1824 sqlite3HashInsert(&db->aFunc, pBest->zName, nName, (void*)pBest);
drh8e0a2f92002-02-23 23:45:45 +00001825 }
danielk1977d02eb1f2004-06-06 09:44:03 +00001826
1827 if( pBest && (pBest->xStep || pBest->xFunc || createFlag) ){
1828 return pBest;
drh8e0a2f92002-02-23 23:45:45 +00001829 }
danielk1977d02eb1f2004-06-06 09:44:03 +00001830 return 0;
drh8e0a2f92002-02-23 23:45:45 +00001831}