blob: 5377a2daa5adaaf7ee09a695d8fdc9690778e151 [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**
drh290c1942004-08-21 17:54:45 +000015** $Id: expr.c,v 1.157 2004/08/21 17:54:45 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;
danielk19770f69c1e2004-05-29 11:24:50 +00001150 case TK_STRING: op = OP_String8; break;
danielk1977c572ef72004-05-27 09:28:41 +00001151 case TK_BLOB: op = OP_HexBlob; break;
danielk1977cfe9a692004-06-16 12:00:29 +00001152 default: op = 0; break;
drhcce7d172000-05-31 15:34:51 +00001153 }
1154 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00001155 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +00001156 if( pParse->useAgg ){
danielk19774adee202004-05-08 08:23:19 +00001157 sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drhc4a3c772001-04-04 11:48:57 +00001158 }else if( pExpr->iColumn>=0 ){
danielk19774adee202004-05-08 08:23:19 +00001159 sqlite3VdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn);
drhc4a3c772001-04-04 11:48:57 +00001160 }else{
danielk19774adee202004-05-08 08:23:19 +00001161 sqlite3VdbeAddOp(v, OP_Recno, pExpr->iTable, 0);
drh22827922000-06-06 17:27:05 +00001162 }
drhcce7d172000-05-31 15:34:51 +00001163 break;
1164 }
1165 case TK_INTEGER: {
drhfec19aa2004-05-19 20:41:03 +00001166 codeInteger(v, pExpr->token.z, pExpr->token.n);
1167 break;
1168 }
1169 case TK_FLOAT:
1170 case TK_STRING: {
1171 sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z, pExpr->token.n);
danielk19774adee202004-05-08 08:23:19 +00001172 sqlite3VdbeDequoteP3(v, -1);
drhcce7d172000-05-31 15:34:51 +00001173 break;
1174 }
danielk1977c572ef72004-05-27 09:28:41 +00001175 case TK_BLOB: {
1176 sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z+1, pExpr->token.n-1);
1177 sqlite3VdbeDequoteP3(v, -1);
1178 break;
1179 }
drhcce7d172000-05-31 15:34:51 +00001180 case TK_NULL: {
danielk19770f69c1e2004-05-29 11:24:50 +00001181 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001182 break;
1183 }
drh50457892003-09-06 01:10:47 +00001184 case TK_VARIABLE: {
danielk19774adee202004-05-08 08:23:19 +00001185 sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0);
drh895d7472004-08-20 16:02:39 +00001186 if( pExpr->token.n>1 ){
1187 sqlite3VdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
1188 }
drh50457892003-09-06 01:10:47 +00001189 break;
1190 }
drhc9b84a12002-06-20 11:36:48 +00001191 case TK_LT:
1192 case TK_LE:
1193 case TK_GT:
1194 case TK_GE:
1195 case TK_NE:
1196 case TK_EQ: {
danielk1977a37cdde2004-05-16 11:15:36 +00001197 sqlite3ExprCode(pParse, pExpr->pLeft);
1198 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001199 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 0, 0);
danielk1977a37cdde2004-05-16 11:15:36 +00001200 break;
drhc9b84a12002-06-20 11:36:48 +00001201 }
drhcce7d172000-05-31 15:34:51 +00001202 case TK_AND:
1203 case TK_OR:
1204 case TK_PLUS:
1205 case TK_STAR:
1206 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00001207 case TK_REM:
1208 case TK_BITAND:
1209 case TK_BITOR:
drh17c40292004-07-21 02:53:29 +00001210 case TK_SLASH:
drhbf4133c2001-10-13 02:59:08 +00001211 case TK_LSHIFT:
1212 case TK_RSHIFT: {
danielk19774adee202004-05-08 08:23:19 +00001213 sqlite3ExprCode(pParse, pExpr->pLeft);
drh17c40292004-07-21 02:53:29 +00001214 sqlite3ExprCode(pParse, pExpr->pRight);
danielk19774adee202004-05-08 08:23:19 +00001215 sqlite3VdbeAddOp(v, op, 0, 0);
drhbf4133c2001-10-13 02:59:08 +00001216 break;
1217 }
drh00400772000-06-16 20:51:26 +00001218 case TK_CONCAT: {
danielk19774adee202004-05-08 08:23:19 +00001219 sqlite3ExprCode(pParse, pExpr->pLeft);
1220 sqlite3ExprCode(pParse, pExpr->pRight);
danielk197772c952a2004-06-21 09:06:41 +00001221 sqlite3VdbeAddOp(v, OP_Concat8, 2, 0);
drh00400772000-06-16 20:51:26 +00001222 break;
1223 }
drhcce7d172000-05-31 15:34:51 +00001224 case TK_UMINUS: {
drhfec19aa2004-05-19 20:41:03 +00001225 Expr *pLeft = pExpr->pLeft;
1226 assert( pLeft );
1227 if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
1228 Token *p = &pLeft->token;
drh6e142f52000-06-08 13:36:40 +00001229 char *z = sqliteMalloc( p->n + 2 );
1230 sprintf(z, "-%.*s", p->n, p->z);
drhfec19aa2004-05-19 20:41:03 +00001231 if( pLeft->op==TK_FLOAT ){
1232 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001233 }else{
drhfec19aa2004-05-19 20:41:03 +00001234 codeInteger(v, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001235 }
drh6e142f52000-06-08 13:36:40 +00001236 sqliteFree(z);
1237 break;
1238 }
drh1ccde152000-06-17 13:12:39 +00001239 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +00001240 }
drhbf4133c2001-10-13 02:59:08 +00001241 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00001242 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001243 sqlite3ExprCode(pParse, pExpr->pLeft);
1244 sqlite3VdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001245 break;
1246 }
1247 case TK_ISNULL:
1248 case TK_NOTNULL: {
1249 int dest;
danielk19774adee202004-05-08 08:23:19 +00001250 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1251 sqlite3ExprCode(pParse, pExpr->pLeft);
1252 dest = sqlite3VdbeCurrentAddr(v) + 2;
1253 sqlite3VdbeAddOp(v, op, 1, dest);
1254 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);
drhcce7d172000-05-31 15:34:51 +00001255 }
danielk1977a37cdde2004-05-16 11:15:36 +00001256 break;
drh22827922000-06-06 17:27:05 +00001257 case TK_AGG_FUNCTION: {
danielk19774adee202004-05-08 08:23:19 +00001258 sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drh22827922000-06-06 17:27:05 +00001259 break;
1260 }
drh4b59ab52002-08-24 18:24:51 +00001261 case TK_GLOB:
1262 case TK_LIKE:
drhcce7d172000-05-31 15:34:51 +00001263 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00001264 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00001265 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00001266 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00001267 int nId;
1268 const char *zId;
danielk1977682f68b2004-06-05 10:22:17 +00001269 int p2 = 0;
1270 int i;
danielk1977d8123362004-06-12 09:25:12 +00001271 u8 enc = pParse->db->enc;
danielk1977dc1bdc42004-06-11 10:51:27 +00001272 CollSeq *pColl = 0;
drh4b59ab52002-08-24 18:24:51 +00001273 getFunctionName(pExpr, &zId, &nId);
danielk1977d8123362004-06-12 09:25:12 +00001274 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001275 assert( pDef!=0 );
drhf9b596e2004-05-26 16:54:42 +00001276 nExpr = sqlite3ExprCodeExprList(pParse, pList);
danielk1977682f68b2004-06-05 10:22:17 +00001277 for(i=0; i<nExpr && i<32; i++){
danielk1977d02eb1f2004-06-06 09:44:03 +00001278 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
1279 p2 |= (1<<i);
1280 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001281 if( pDef->needCollSeq && !pColl ){
1282 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
1283 }
1284 }
1285 if( pDef->needCollSeq ){
1286 if( !pColl ) pColl = pParse->db->pDfltColl;
danielk1977d8123362004-06-12 09:25:12 +00001287 sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ);
danielk1977682f68b2004-06-05 10:22:17 +00001288 }
1289 sqlite3VdbeOp3(v, OP_Function, nExpr, p2, (char*)pDef, P3_FUNCDEF);
drhcce7d172000-05-31 15:34:51 +00001290 break;
1291 }
drh19a775c2000-06-05 18:54:46 +00001292 case TK_SELECT: {
danielk19774adee202004-05-08 08:23:19 +00001293 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drh19a775c2000-06-05 18:54:46 +00001294 break;
1295 }
drhfef52082000-06-06 01:50:43 +00001296 case TK_IN: {
1297 int addr;
danielk1977e014a832004-05-17 10:48:57 +00001298 char const *affStr;
1299
1300 /* Figure out the affinity to use to create a key from the results
1301 ** of the expression. affinityStr stores a static string suitable for
danielk1977ededfd52004-06-17 07:53:01 +00001302 ** P3 of OP_MakeRecord.
danielk1977e014a832004-05-17 10:48:57 +00001303 */
1304 affStr = sqlite3AffinityString(comparisonAffinity(pExpr));
1305
danielk19774adee202004-05-08 08:23:19 +00001306 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
danielk1977e014a832004-05-17 10:48:57 +00001307
1308 /* Code the <expr> from "<expr> IN (...)". The temporary table
1309 ** pExpr->iTable contains the values that make up the (...) set.
1310 */
danielk19774adee202004-05-08 08:23:19 +00001311 sqlite3ExprCode(pParse, pExpr->pLeft);
1312 addr = sqlite3VdbeCurrentAddr(v);
danielk1977e014a832004-05-17 10:48:57 +00001313 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); /* addr + 0 */
danielk19774adee202004-05-08 08:23:19 +00001314 sqlite3VdbeAddOp(v, OP_Pop, 2, 0);
danielk19770f69c1e2004-05-29 11:24:50 +00001315 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +00001316 sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7);
danielk1977ededfd52004-06-17 07:53:01 +00001317 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, affStr, P3_STATIC); /* addr + 4 */
danielk1977e014a832004-05-17 10:48:57 +00001318 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7);
1319 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); /* addr + 6 */
1320
drhfef52082000-06-06 01:50:43 +00001321 break;
1322 }
1323 case TK_BETWEEN: {
drhbe5c89a2004-07-26 00:31:09 +00001324 Expr *pLeft = pExpr->pLeft;
1325 struct ExprList_item *pLItem = pExpr->pList->a;
1326 Expr *pRight = pLItem->pExpr;
1327 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001328 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001329 sqlite3ExprCode(pParse, pRight);
1330 codeCompare(pParse, pLeft, pRight, OP_Ge, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001331 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
drhbe5c89a2004-07-26 00:31:09 +00001332 pLItem++;
1333 pRight = pLItem->pExpr;
1334 sqlite3ExprCode(pParse, pRight);
1335 codeCompare(pParse, pLeft, pRight, OP_Le, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001336 sqlite3VdbeAddOp(v, OP_And, 0, 0);
drhfef52082000-06-06 01:50:43 +00001337 break;
1338 }
drh51e9a442004-01-16 16:42:53 +00001339 case TK_UPLUS:
drha2e00042002-01-22 03:13:42 +00001340 case TK_AS: {
danielk19774adee202004-05-08 08:23:19 +00001341 sqlite3ExprCode(pParse, pExpr->pLeft);
drha2e00042002-01-22 03:13:42 +00001342 break;
1343 }
drh17a7f8d2002-03-24 13:13:27 +00001344 case TK_CASE: {
1345 int expr_end_label;
drhf5905aa2002-05-26 20:54:33 +00001346 int jumpInst;
1347 int addr;
1348 int nExpr;
drh17a7f8d2002-03-24 13:13:27 +00001349 int i;
drhbe5c89a2004-07-26 00:31:09 +00001350 ExprList *pEList;
1351 struct ExprList_item *aListelem;
drh17a7f8d2002-03-24 13:13:27 +00001352
1353 assert(pExpr->pList);
1354 assert((pExpr->pList->nExpr % 2) == 0);
1355 assert(pExpr->pList->nExpr > 0);
drhbe5c89a2004-07-26 00:31:09 +00001356 pEList = pExpr->pList;
1357 aListelem = pEList->a;
1358 nExpr = pEList->nExpr;
danielk19774adee202004-05-08 08:23:19 +00001359 expr_end_label = sqlite3VdbeMakeLabel(v);
drh17a7f8d2002-03-24 13:13:27 +00001360 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001361 sqlite3ExprCode(pParse, pExpr->pLeft);
drh17a7f8d2002-03-24 13:13:27 +00001362 }
drhf5905aa2002-05-26 20:54:33 +00001363 for(i=0; i<nExpr; i=i+2){
drhbe5c89a2004-07-26 00:31:09 +00001364 sqlite3ExprCode(pParse, aListelem[i].pExpr);
drh17a7f8d2002-03-24 13:13:27 +00001365 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001366 sqlite3VdbeAddOp(v, OP_Dup, 1, 1);
drhbe5c89a2004-07-26 00:31:09 +00001367 jumpInst = codeCompare(pParse, pExpr->pLeft, aListelem[i].pExpr,
1368 OP_Ne, 0, 1);
danielk19774adee202004-05-08 08:23:19 +00001369 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001370 }else{
danielk19774adee202004-05-08 08:23:19 +00001371 jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001372 }
drhbe5c89a2004-07-26 00:31:09 +00001373 sqlite3ExprCode(pParse, aListelem[i+1].pExpr);
danielk19774adee202004-05-08 08:23:19 +00001374 sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label);
1375 addr = sqlite3VdbeCurrentAddr(v);
1376 sqlite3VdbeChangeP2(v, jumpInst, addr);
drh17a7f8d2002-03-24 13:13:27 +00001377 }
drhf570f012002-05-31 15:51:25 +00001378 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001379 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhf570f012002-05-31 15:51:25 +00001380 }
drh17a7f8d2002-03-24 13:13:27 +00001381 if( pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001382 sqlite3ExprCode(pParse, pExpr->pRight);
drh17a7f8d2002-03-24 13:13:27 +00001383 }else{
danielk19770f69c1e2004-05-29 11:24:50 +00001384 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
drh17a7f8d2002-03-24 13:13:27 +00001385 }
danielk19774adee202004-05-08 08:23:19 +00001386 sqlite3VdbeResolveLabel(v, expr_end_label);
danielk19776f349032002-06-11 02:25:40 +00001387 break;
1388 }
1389 case TK_RAISE: {
1390 if( !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +00001391 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00001392 "RAISE() may only be used within a trigger-program");
danielk19776f349032002-06-11 02:25:40 +00001393 pParse->nErr++;
1394 return;
1395 }
1396 if( pExpr->iColumn == OE_Rollback ||
1397 pExpr->iColumn == OE_Abort ||
1398 pExpr->iColumn == OE_Fail ){
danielk19774adee202004-05-08 08:23:19 +00001399 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn,
drh701a0ae2004-02-22 20:05:00 +00001400 pExpr->token.z, pExpr->token.n);
danielk19774adee202004-05-08 08:23:19 +00001401 sqlite3VdbeDequoteP3(v, -1);
danielk19776f349032002-06-11 02:25:40 +00001402 } else {
1403 assert( pExpr->iColumn == OE_Ignore );
danielk19774adee202004-05-08 08:23:19 +00001404 sqlite3VdbeOp3(v, OP_Goto, 0, pParse->trigStack->ignoreJump,
drh701a0ae2004-02-22 20:05:00 +00001405 "(IGNORE jump)", 0);
danielk19776f349032002-06-11 02:25:40 +00001406 }
drh17a7f8d2002-03-24 13:13:27 +00001407 }
1408 break;
drhcce7d172000-05-31 15:34:51 +00001409 }
drhcce7d172000-05-31 15:34:51 +00001410}
1411
1412/*
drh268380c2004-02-25 13:47:31 +00001413** Generate code that pushes the value of every element of the given
drhf9b596e2004-05-26 16:54:42 +00001414** expression list onto the stack.
drh268380c2004-02-25 13:47:31 +00001415**
1416** Return the number of elements pushed onto the stack.
1417*/
danielk19774adee202004-05-08 08:23:19 +00001418int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00001419 Parse *pParse, /* Parsing context */
drhf9b596e2004-05-26 16:54:42 +00001420 ExprList *pList /* The expression list to be coded */
drh268380c2004-02-25 13:47:31 +00001421){
1422 struct ExprList_item *pItem;
1423 int i, n;
1424 Vdbe *v;
1425 if( pList==0 ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001426 v = sqlite3GetVdbe(pParse);
drh268380c2004-02-25 13:47:31 +00001427 n = pList->nExpr;
1428 for(pItem=pList->a, i=0; i<n; i++, pItem++){
danielk19774adee202004-05-08 08:23:19 +00001429 sqlite3ExprCode(pParse, pItem->pExpr);
drh268380c2004-02-25 13:47:31 +00001430 }
drhf9b596e2004-05-26 16:54:42 +00001431 return n;
drh268380c2004-02-25 13:47:31 +00001432}
1433
1434/*
drhcce7d172000-05-31 15:34:51 +00001435** Generate code for a boolean expression such that a jump is made
1436** to the label "dest" if the expression is true but execution
1437** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00001438**
1439** If the expression evaluates to NULL (neither true nor false), then
1440** take the jump if the jumpIfNull flag is true.
drhcce7d172000-05-31 15:34:51 +00001441*/
danielk19774adee202004-05-08 08:23:19 +00001442void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001443 Vdbe *v = pParse->pVdbe;
1444 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001445 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001446 switch( pExpr->op ){
1447 case TK_LT: op = OP_Lt; break;
1448 case TK_LE: op = OP_Le; break;
1449 case TK_GT: op = OP_Gt; break;
1450 case TK_GE: op = OP_Ge; break;
1451 case TK_NE: op = OP_Ne; break;
1452 case TK_EQ: op = OP_Eq; break;
drhcce7d172000-05-31 15:34:51 +00001453 case TK_ISNULL: op = OP_IsNull; break;
1454 case TK_NOTNULL: op = OP_NotNull; break;
1455 default: break;
1456 }
1457 switch( pExpr->op ){
1458 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001459 int d2 = sqlite3VdbeMakeLabel(v);
1460 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
1461 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
1462 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001463 break;
1464 }
1465 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001466 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
1467 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001468 break;
1469 }
1470 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001471 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001472 break;
1473 }
1474 case TK_LT:
1475 case TK_LE:
1476 case TK_GT:
1477 case TK_GE:
1478 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00001479 case TK_EQ: {
danielk19774adee202004-05-08 08:23:19 +00001480 sqlite3ExprCode(pParse, pExpr->pLeft);
1481 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001482 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001483 break;
1484 }
1485 case TK_ISNULL:
1486 case TK_NOTNULL: {
danielk19774adee202004-05-08 08:23:19 +00001487 sqlite3ExprCode(pParse, pExpr->pLeft);
1488 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001489 break;
1490 }
drhfef52082000-06-06 01:50:43 +00001491 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00001492 /* The expression "x BETWEEN y AND z" is implemented as:
1493 **
1494 ** 1 IF (x < y) GOTO 3
1495 ** 2 IF (x <= z) GOTO <dest>
1496 ** 3 ...
1497 */
drhf5905aa2002-05-26 20:54:33 +00001498 int addr;
drhbe5c89a2004-07-26 00:31:09 +00001499 Expr *pLeft = pExpr->pLeft;
1500 Expr *pRight = pExpr->pList->a[0].pExpr;
1501 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001502 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001503 sqlite3ExprCode(pParse, pRight);
1504 addr = codeCompare(pParse, pLeft, pRight, OP_Lt, 0, !jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00001505
drhbe5c89a2004-07-26 00:31:09 +00001506 pRight = pExpr->pList->a[1].pExpr;
1507 sqlite3ExprCode(pParse, pRight);
1508 codeCompare(pParse, pLeft, pRight, OP_Le, dest, jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00001509
danielk19774adee202004-05-08 08:23:19 +00001510 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
1511 sqlite3VdbeChangeP2(v, addr, sqlite3VdbeCurrentAddr(v));
1512 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00001513 break;
1514 }
drhcce7d172000-05-31 15:34:51 +00001515 default: {
danielk19774adee202004-05-08 08:23:19 +00001516 sqlite3ExprCode(pParse, pExpr);
1517 sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001518 break;
1519 }
1520 }
1521}
1522
1523/*
drh66b89c82000-11-28 20:47:17 +00001524** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00001525** to the label "dest" if the expression is false but execution
1526** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00001527**
1528** If the expression evaluates to NULL (neither true nor false) then
1529** jump if jumpIfNull is true or fall through if jumpIfNull is false.
drhcce7d172000-05-31 15:34:51 +00001530*/
danielk19774adee202004-05-08 08:23:19 +00001531void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001532 Vdbe *v = pParse->pVdbe;
1533 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001534 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001535 switch( pExpr->op ){
1536 case TK_LT: op = OP_Ge; break;
1537 case TK_LE: op = OP_Gt; break;
1538 case TK_GT: op = OP_Le; break;
1539 case TK_GE: op = OP_Lt; break;
1540 case TK_NE: op = OP_Eq; break;
1541 case TK_EQ: op = OP_Ne; break;
drhcce7d172000-05-31 15:34:51 +00001542 case TK_ISNULL: op = OP_NotNull; break;
1543 case TK_NOTNULL: op = OP_IsNull; break;
1544 default: break;
1545 }
1546 switch( pExpr->op ){
1547 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001548 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
1549 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001550 break;
1551 }
1552 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001553 int d2 = sqlite3VdbeMakeLabel(v);
1554 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
1555 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
1556 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001557 break;
1558 }
1559 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001560 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001561 break;
1562 }
1563 case TK_LT:
1564 case TK_LE:
1565 case TK_GT:
1566 case TK_GE:
1567 case TK_NE:
1568 case TK_EQ: {
danielk19774adee202004-05-08 08:23:19 +00001569 sqlite3ExprCode(pParse, pExpr->pLeft);
1570 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001571 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001572 break;
1573 }
drhcce7d172000-05-31 15:34:51 +00001574 case TK_ISNULL:
1575 case TK_NOTNULL: {
danielk19774adee202004-05-08 08:23:19 +00001576 sqlite3ExprCode(pParse, pExpr->pLeft);
1577 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001578 break;
1579 }
drhfef52082000-06-06 01:50:43 +00001580 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00001581 /* The expression is "x BETWEEN y AND z". It is implemented as:
1582 **
1583 ** 1 IF (x >= y) GOTO 3
1584 ** 2 GOTO <dest>
1585 ** 3 IF (x > z) GOTO <dest>
1586 */
drhfef52082000-06-06 01:50:43 +00001587 int addr;
drhbe5c89a2004-07-26 00:31:09 +00001588 Expr *pLeft = pExpr->pLeft;
1589 Expr *pRight = pExpr->pList->a[0].pExpr;
1590 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001591 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001592 sqlite3ExprCode(pParse, pRight);
danielk19774adee202004-05-08 08:23:19 +00001593 addr = sqlite3VdbeCurrentAddr(v);
drhbe5c89a2004-07-26 00:31:09 +00001594 codeCompare(pParse, pLeft, pRight, OP_Ge, addr+3, !jumpIfNull);
1595
danielk19774adee202004-05-08 08:23:19 +00001596 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
1597 sqlite3VdbeAddOp(v, OP_Goto, 0, dest);
drhbe5c89a2004-07-26 00:31:09 +00001598 pRight = pExpr->pList->a[1].pExpr;
1599 sqlite3ExprCode(pParse, pRight);
1600 codeCompare(pParse, pLeft, pRight, OP_Gt, dest, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00001601 break;
1602 }
drhcce7d172000-05-31 15:34:51 +00001603 default: {
danielk19774adee202004-05-08 08:23:19 +00001604 sqlite3ExprCode(pParse, pExpr);
1605 sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001606 break;
1607 }
1608 }
1609}
drh22827922000-06-06 17:27:05 +00001610
1611/*
1612** Do a deep comparison of two expression trees. Return TRUE (non-zero)
1613** if they are identical and return FALSE if they differ in any way.
1614*/
danielk19774adee202004-05-08 08:23:19 +00001615int sqlite3ExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00001616 int i;
1617 if( pA==0 ){
1618 return pB==0;
1619 }else if( pB==0 ){
1620 return 0;
1621 }
1622 if( pA->op!=pB->op ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001623 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
1624 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00001625 if( pA->pList ){
1626 if( pB->pList==0 ) return 0;
1627 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
1628 for(i=0; i<pA->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00001629 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00001630 return 0;
1631 }
1632 }
1633 }else if( pB->pList ){
1634 return 0;
1635 }
1636 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00001637 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drh22827922000-06-06 17:27:05 +00001638 if( pA->token.z ){
1639 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00001640 if( pB->token.n!=pA->token.n ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001641 if( sqlite3StrNICmp(pA->token.z, pB->token.z, pB->token.n)!=0 ) return 0;
drh22827922000-06-06 17:27:05 +00001642 }
1643 return 1;
1644}
1645
1646/*
1647** Add a new element to the pParse->aAgg[] array and return its index.
1648*/
1649static int appendAggInfo(Parse *pParse){
1650 if( (pParse->nAgg & 0x7)==0 ){
1651 int amt = pParse->nAgg + 8;
drh6d4abfb2001-10-22 02:58:08 +00001652 AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0]));
1653 if( aAgg==0 ){
drh22827922000-06-06 17:27:05 +00001654 return -1;
1655 }
drh6d4abfb2001-10-22 02:58:08 +00001656 pParse->aAgg = aAgg;
drh22827922000-06-06 17:27:05 +00001657 }
1658 memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0]));
1659 return pParse->nAgg++;
1660}
1661
1662/*
1663** Analyze the given expression looking for aggregate functions and
1664** for variables that need to be added to the pParse->aAgg[] array.
1665** Make additional entries to the pParse->aAgg[] array as necessary.
1666**
1667** This routine should only be called after the expression has been
danielk19774adee202004-05-08 08:23:19 +00001668** analyzed by sqlite3ExprResolveIds() and sqlite3ExprCheck().
drh22827922000-06-06 17:27:05 +00001669**
1670** If errors are seen, leave an error message in zErrMsg and return
1671** the number of errors.
1672*/
danielk19774adee202004-05-08 08:23:19 +00001673int sqlite3ExprAnalyzeAggregates(Parse *pParse, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00001674 int i;
1675 AggExpr *aAgg;
1676 int nErr = 0;
1677
1678 if( pExpr==0 ) return 0;
1679 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00001680 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +00001681 aAgg = pParse->aAgg;
1682 for(i=0; i<pParse->nAgg; i++){
1683 if( aAgg[i].isAgg ) continue;
1684 if( aAgg[i].pExpr->iTable==pExpr->iTable
drh967e8b72000-06-21 13:59:10 +00001685 && aAgg[i].pExpr->iColumn==pExpr->iColumn ){
drh22827922000-06-06 17:27:05 +00001686 break;
1687 }
1688 }
1689 if( i>=pParse->nAgg ){
1690 i = appendAggInfo(pParse);
1691 if( i<0 ) return 1;
1692 pParse->aAgg[i].isAgg = 0;
1693 pParse->aAgg[i].pExpr = pExpr;
1694 }
drhaaf88722000-06-08 11:25:00 +00001695 pExpr->iAgg = i;
drh22827922000-06-06 17:27:05 +00001696 break;
1697 }
1698 case TK_AGG_FUNCTION: {
drh22827922000-06-06 17:27:05 +00001699 aAgg = pParse->aAgg;
1700 for(i=0; i<pParse->nAgg; i++){
1701 if( !aAgg[i].isAgg ) continue;
danielk19774adee202004-05-08 08:23:19 +00001702 if( sqlite3ExprCompare(aAgg[i].pExpr, pExpr) ){
drh22827922000-06-06 17:27:05 +00001703 break;
1704 }
1705 }
1706 if( i>=pParse->nAgg ){
danielk1977d8123362004-06-12 09:25:12 +00001707 u8 enc = pParse->db->enc;
drh22827922000-06-06 17:27:05 +00001708 i = appendAggInfo(pParse);
1709 if( i<0 ) return 1;
1710 pParse->aAgg[i].isAgg = 1;
1711 pParse->aAgg[i].pExpr = pExpr;
danielk19774adee202004-05-08 08:23:19 +00001712 pParse->aAgg[i].pFunc = sqlite3FindFunction(pParse->db,
drh6977fea2002-10-22 23:38:04 +00001713 pExpr->token.z, pExpr->token.n,
danielk1977d8123362004-06-12 09:25:12 +00001714 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
drh22827922000-06-06 17:27:05 +00001715 }
1716 pExpr->iAgg = i;
1717 break;
1718 }
1719 default: {
1720 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001721 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pLeft);
drh22827922000-06-06 17:27:05 +00001722 }
1723 if( nErr==0 && pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001724 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pRight);
drh22827922000-06-06 17:27:05 +00001725 }
1726 if( nErr==0 && pExpr->pList ){
1727 int n = pExpr->pList->nExpr;
1728 int i;
1729 for(i=0; nErr==0 && i<n; i++){
danielk19774adee202004-05-08 08:23:19 +00001730 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pList->a[i].pExpr);
drh22827922000-06-06 17:27:05 +00001731 }
1732 }
1733 break;
1734 }
1735 }
1736 return nErr;
1737}
drh8e0a2f92002-02-23 23:45:45 +00001738
1739/*
danielk1977d02eb1f2004-06-06 09:44:03 +00001740** Locate a user function given a name, a number of arguments and a flag
1741** indicating whether the function prefers UTF-16 over UTF-8. Return a
1742** pointer to the FuncDef structure that defines that function, or return
1743** NULL if the function does not exist.
drh8e0a2f92002-02-23 23:45:45 +00001744**
drh0bce8352002-02-28 00:41:10 +00001745** If the createFlag argument is true, then a new (blank) FuncDef
drh8e0a2f92002-02-23 23:45:45 +00001746** structure is created and liked into the "db" structure if a
1747** no matching function previously existed. When createFlag is true
1748** and the nArg parameter is -1, then only a function that accepts
1749** any number of arguments will be returned.
1750**
1751** If createFlag is false and nArg is -1, then the first valid
1752** function found is returned. A function is valid if either xFunc
1753** or xStep is non-zero.
danielk1977d02eb1f2004-06-06 09:44:03 +00001754**
1755** If createFlag is false, then a function with the required name and
1756** number of arguments may be returned even if the eTextRep flag does not
1757** match that requested.
drh8e0a2f92002-02-23 23:45:45 +00001758*/
danielk19774adee202004-05-08 08:23:19 +00001759FuncDef *sqlite3FindFunction(
drh8e0a2f92002-02-23 23:45:45 +00001760 sqlite *db, /* An open database */
1761 const char *zName, /* Name of the function. Not null-terminated */
1762 int nName, /* Number of characters in the name */
1763 int nArg, /* Number of arguments. -1 means any number */
danielk1977d8123362004-06-12 09:25:12 +00001764 u8 enc, /* Preferred text encoding */
drh8e0a2f92002-02-23 23:45:45 +00001765 int createFlag /* Create new entry if true and does not otherwise exist */
1766){
danielk1977d02eb1f2004-06-06 09:44:03 +00001767 FuncDef *p; /* Iterator variable */
1768 FuncDef *pFirst; /* First function with this name */
1769 FuncDef *pBest = 0; /* Best match found so far */
danielk1977d8123362004-06-12 09:25:12 +00001770 int bestmatch = 0;
danielk1977d02eb1f2004-06-06 09:44:03 +00001771
danielk1977d8123362004-06-12 09:25:12 +00001772
1773 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
danielk1977d02eb1f2004-06-06 09:44:03 +00001774 if( nArg<-1 ) nArg = -1;
1775
1776 pFirst = (FuncDef*)sqlite3HashFind(&db->aFunc, zName, nName);
1777 for(p=pFirst; p; p=p->pNext){
danielk1977d8123362004-06-12 09:25:12 +00001778 /* During the search for the best function definition, bestmatch is set
1779 ** as follows to indicate the quality of the match with the definition
1780 ** pointed to by pBest:
1781 **
1782 ** 0: pBest is NULL. No match has been found.
1783 ** 1: A variable arguments function that prefers UTF-8 when a UTF-16
1784 ** encoding is requested, or vice versa.
1785 ** 2: A variable arguments function that uses UTF-16BE when UTF-16LE is
1786 ** requested, or vice versa.
1787 ** 3: A variable arguments function using the same text encoding.
1788 ** 4: A function with the exact number of arguments requested that
1789 ** prefers UTF-8 when a UTF-16 encoding is requested, or vice versa.
1790 ** 5: A function with the exact number of arguments requested that
1791 ** prefers UTF-16LE when UTF-16BE is requested, or vice versa.
1792 ** 6: An exact match.
1793 **
1794 ** A larger value of 'matchqual' indicates a more desirable match.
1795 */
danielk1977e12c17b2004-06-23 12:35:14 +00001796 if( p->nArg==-1 || p->nArg==nArg || nArg==-1 ){
danielk1977d8123362004-06-12 09:25:12 +00001797 int match = 1; /* Quality of this match */
1798 if( p->nArg==nArg || nArg==-1 ){
1799 match = 4;
danielk1977d02eb1f2004-06-06 09:44:03 +00001800 }
danielk1977d8123362004-06-12 09:25:12 +00001801 if( enc==p->iPrefEnc ){
1802 match += 2;
danielk1977d02eb1f2004-06-06 09:44:03 +00001803 }
danielk1977d8123362004-06-12 09:25:12 +00001804 else if( (enc==SQLITE_UTF16LE && p->iPrefEnc==SQLITE_UTF16BE) ||
1805 (enc==SQLITE_UTF16BE && p->iPrefEnc==SQLITE_UTF16LE) ){
1806 match += 1;
1807 }
1808
1809 if( match>bestmatch ){
1810 pBest = p;
1811 bestmatch = match;
danielk1977d02eb1f2004-06-06 09:44:03 +00001812 }
1813 }
drh8e0a2f92002-02-23 23:45:45 +00001814 }
danielk1977d02eb1f2004-06-06 09:44:03 +00001815
danielk1977d8123362004-06-12 09:25:12 +00001816 /* If the createFlag parameter is true, and the seach did not reveal an
1817 ** exact match for the name, number of arguments and encoding, then add a
1818 ** new entry to the hash table and return it.
1819 */
1820 if( createFlag && bestmatch<6 &&
danielk1977d02eb1f2004-06-06 09:44:03 +00001821 (pBest = sqliteMalloc(sizeof(*pBest)+nName+1)) ){
1822 pBest->nArg = nArg;
1823 pBest->pNext = pFirst;
1824 pBest->zName = (char*)&pBest[1];
danielk1977d8123362004-06-12 09:25:12 +00001825 pBest->iPrefEnc = enc;
danielk1977d02eb1f2004-06-06 09:44:03 +00001826 memcpy(pBest->zName, zName, nName);
1827 pBest->zName[nName] = 0;
1828 sqlite3HashInsert(&db->aFunc, pBest->zName, nName, (void*)pBest);
drh8e0a2f92002-02-23 23:45:45 +00001829 }
danielk1977d02eb1f2004-06-06 09:44:03 +00001830
1831 if( pBest && (pBest->xStep || pBest->xFunc || createFlag) ){
1832 return pBest;
drh8e0a2f92002-02-23 23:45:45 +00001833 }
danielk1977d02eb1f2004-06-06 09:44:03 +00001834 return 0;
drh8e0a2f92002-02-23 23:45:45 +00001835}