blob: 91756363d29da40567b99648fe5fea80a85b2a06 [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**
drh91bb0ee2004-09-01 03:06:34 +000015** $Id: expr.c,v 1.159 2004/09/01 03:06:35 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/*
drh91bb0ee2004-09-01 03:06:34 +0000223** Join two expressions using an AND operator. If either expression is
224** NULL, then just return the other expression.
225*/
226Expr *sqlite3ExprAnd(Expr *pLeft, Expr *pRight){
227 if( pLeft==0 ){
228 return pRight;
229 }else if( pRight==0 ){
230 return pLeft;
231 }else{
232 return sqlite3Expr(TK_AND, pLeft, pRight, 0);
233 }
234}
235
236/*
drh6977fea2002-10-22 23:38:04 +0000237** Set the Expr.span field of the given expression to span all
drha76b5df2002-02-23 02:32:10 +0000238** text between the two given tokens.
239*/
danielk19774adee202004-05-08 08:23:19 +0000240void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
drh4efc4752004-01-16 15:55:37 +0000241 assert( pRight!=0 );
242 assert( pLeft!=0 );
drh71c697e2004-08-08 23:39:19 +0000243 if( !sqlite3_malloc_failed && pRight->z && pLeft->z ){
drh4b59ab52002-08-24 18:24:51 +0000244 if( pLeft->dyn==0 && pRight->dyn==0 ){
drh6977fea2002-10-22 23:38:04 +0000245 pExpr->span.z = pLeft->z;
246 pExpr->span.n = pRight->n + Addr(pRight->z) - Addr(pLeft->z);
drh4b59ab52002-08-24 18:24:51 +0000247 }else{
drh6977fea2002-10-22 23:38:04 +0000248 pExpr->span.z = 0;
drh4b59ab52002-08-24 18:24:51 +0000249 }
drha76b5df2002-02-23 02:32:10 +0000250 }
251}
252
253/*
254** Construct a new expression node for a function with multiple
255** arguments.
256*/
danielk19774adee202004-05-08 08:23:19 +0000257Expr *sqlite3ExprFunction(ExprList *pList, Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000258 Expr *pNew;
259 pNew = sqliteMalloc( sizeof(Expr) );
260 if( pNew==0 ){
danielk19774adee202004-05-08 08:23:19 +0000261 /* sqlite3ExprListDelete(pList); // Leak pList when malloc fails */
drha76b5df2002-02-23 02:32:10 +0000262 return 0;
263 }
264 pNew->op = TK_FUNCTION;
265 pNew->pList = pList;
266 if( pToken ){
drh4b59ab52002-08-24 18:24:51 +0000267 assert( pToken->dyn==0 );
drha76b5df2002-02-23 02:32:10 +0000268 pNew->token = *pToken;
269 }else{
270 pNew->token.z = 0;
drha76b5df2002-02-23 02:32:10 +0000271 }
drh6977fea2002-10-22 23:38:04 +0000272 pNew->span = pNew->token;
drha76b5df2002-02-23 02:32:10 +0000273 return pNew;
274}
275
276/*
drha2e00042002-01-22 03:13:42 +0000277** Recursively delete an expression tree.
278*/
danielk19774adee202004-05-08 08:23:19 +0000279void sqlite3ExprDelete(Expr *p){
drha2e00042002-01-22 03:13:42 +0000280 if( p==0 ) return;
drh4efc4752004-01-16 15:55:37 +0000281 if( p->span.dyn ) sqliteFree((char*)p->span.z);
282 if( p->token.dyn ) sqliteFree((char*)p->token.z);
danielk19774adee202004-05-08 08:23:19 +0000283 sqlite3ExprDelete(p->pLeft);
284 sqlite3ExprDelete(p->pRight);
285 sqlite3ExprListDelete(p->pList);
286 sqlite3SelectDelete(p->pSelect);
drha2e00042002-01-22 03:13:42 +0000287 sqliteFree(p);
288}
289
drha76b5df2002-02-23 02:32:10 +0000290
291/*
drhff78bd22002-02-27 01:47:11 +0000292** The following group of routines make deep copies of expressions,
293** expression lists, ID lists, and select statements. The copies can
294** be deleted (by being passed to their respective ...Delete() routines)
295** without effecting the originals.
296**
danielk19774adee202004-05-08 08:23:19 +0000297** The expression list, ID, and source lists return by sqlite3ExprListDup(),
298** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
drhad3cab52002-05-24 02:04:32 +0000299** by subsequent calls to sqlite*ListAppend() routines.
drhff78bd22002-02-27 01:47:11 +0000300**
drhad3cab52002-05-24 02:04:32 +0000301** Any tables that the SrcList might point to are not duplicated.
drhff78bd22002-02-27 01:47:11 +0000302*/
danielk19774adee202004-05-08 08:23:19 +0000303Expr *sqlite3ExprDup(Expr *p){
drhff78bd22002-02-27 01:47:11 +0000304 Expr *pNew;
305 if( p==0 ) return 0;
drhfcb78a42003-01-18 20:11:05 +0000306 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000307 if( pNew==0 ) return 0;
drh3b167c72002-06-28 12:18:47 +0000308 memcpy(pNew, p, sizeof(*pNew));
drh6977fea2002-10-22 23:38:04 +0000309 if( p->token.z!=0 ){
drh4b59ab52002-08-24 18:24:51 +0000310 pNew->token.z = sqliteStrDup(p->token.z);
311 pNew->token.dyn = 1;
312 }else{
drh4efc4752004-01-16 15:55:37 +0000313 assert( pNew->token.z==0 );
drh4b59ab52002-08-24 18:24:51 +0000314 }
drh6977fea2002-10-22 23:38:04 +0000315 pNew->span.z = 0;
danielk19774adee202004-05-08 08:23:19 +0000316 pNew->pLeft = sqlite3ExprDup(p->pLeft);
317 pNew->pRight = sqlite3ExprDup(p->pRight);
318 pNew->pList = sqlite3ExprListDup(p->pList);
319 pNew->pSelect = sqlite3SelectDup(p->pSelect);
drhff78bd22002-02-27 01:47:11 +0000320 return pNew;
321}
danielk19774adee202004-05-08 08:23:19 +0000322void sqlite3TokenCopy(Token *pTo, Token *pFrom){
drh4b59ab52002-08-24 18:24:51 +0000323 if( pTo->dyn ) sqliteFree((char*)pTo->z);
drh4b59ab52002-08-24 18:24:51 +0000324 if( pFrom->z ){
325 pTo->n = pFrom->n;
326 pTo->z = sqliteStrNDup(pFrom->z, pFrom->n);
327 pTo->dyn = 1;
328 }else{
drh4b59ab52002-08-24 18:24:51 +0000329 pTo->z = 0;
drh4b59ab52002-08-24 18:24:51 +0000330 }
331}
danielk19774adee202004-05-08 08:23:19 +0000332ExprList *sqlite3ExprListDup(ExprList *p){
drhff78bd22002-02-27 01:47:11 +0000333 ExprList *pNew;
drh3e7bc9c2004-02-21 19:17:17 +0000334 struct ExprList_item *pItem;
drhff78bd22002-02-27 01:47:11 +0000335 int i;
336 if( p==0 ) return 0;
337 pNew = sqliteMalloc( sizeof(*pNew) );
338 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000339 pNew->nExpr = pNew->nAlloc = p->nExpr;
drh3e7bc9c2004-02-21 19:17:17 +0000340 pNew->a = pItem = sqliteMalloc( p->nExpr*sizeof(p->a[0]) );
danielk1977e0048402004-06-15 16:51:01 +0000341 if( pItem==0 ){
342 sqliteFree(pNew);
343 return 0;
344 }
drh1bdd9b52004-04-23 17:04:44 +0000345 for(i=0; i<p->nExpr; i++, pItem++){
drh4b59ab52002-08-24 18:24:51 +0000346 Expr *pNewExpr, *pOldExpr;
danielk19774adee202004-05-08 08:23:19 +0000347 pItem->pExpr = pNewExpr = sqlite3ExprDup(pOldExpr = p->a[i].pExpr);
drh6977fea2002-10-22 23:38:04 +0000348 if( pOldExpr->span.z!=0 && pNewExpr ){
349 /* Always make a copy of the span for top-level expressions in the
drh4b59ab52002-08-24 18:24:51 +0000350 ** expression list. The logic in SELECT processing that determines
351 ** the names of columns in the result set needs this information */
danielk19774adee202004-05-08 08:23:19 +0000352 sqlite3TokenCopy(&pNewExpr->span, &pOldExpr->span);
drh4b59ab52002-08-24 18:24:51 +0000353 }
drh1f3e9052002-10-31 00:09:39 +0000354 assert( pNewExpr==0 || pNewExpr->span.z!=0
danielk197724b03fd2004-05-10 10:34:34 +0000355 || pOldExpr->span.z==0 || sqlite3_malloc_failed );
drh3e7bc9c2004-02-21 19:17:17 +0000356 pItem->zName = sqliteStrDup(p->a[i].zName);
357 pItem->sortOrder = p->a[i].sortOrder;
358 pItem->isAgg = p->a[i].isAgg;
359 pItem->done = 0;
drhff78bd22002-02-27 01:47:11 +0000360 }
361 return pNew;
362}
danielk19774adee202004-05-08 08:23:19 +0000363SrcList *sqlite3SrcListDup(SrcList *p){
drhad3cab52002-05-24 02:04:32 +0000364 SrcList *pNew;
365 int i;
drh113088e2003-03-20 01:16:58 +0000366 int nByte;
drhad3cab52002-05-24 02:04:32 +0000367 if( p==0 ) return 0;
drh113088e2003-03-20 01:16:58 +0000368 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
drh4efc4752004-01-16 15:55:37 +0000369 pNew = sqliteMallocRaw( nByte );
drhad3cab52002-05-24 02:04:32 +0000370 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000371 pNew->nSrc = pNew->nAlloc = p->nSrc;
drhad3cab52002-05-24 02:04:32 +0000372 for(i=0; i<p->nSrc; i++){
drh4efc4752004-01-16 15:55:37 +0000373 struct SrcList_item *pNewItem = &pNew->a[i];
374 struct SrcList_item *pOldItem = &p->a[i];
375 pNewItem->zDatabase = sqliteStrDup(pOldItem->zDatabase);
376 pNewItem->zName = sqliteStrDup(pOldItem->zName);
377 pNewItem->zAlias = sqliteStrDup(pOldItem->zAlias);
378 pNewItem->jointype = pOldItem->jointype;
379 pNewItem->iCursor = pOldItem->iCursor;
380 pNewItem->pTab = 0;
danielk19774adee202004-05-08 08:23:19 +0000381 pNewItem->pSelect = sqlite3SelectDup(pOldItem->pSelect);
382 pNewItem->pOn = sqlite3ExprDup(pOldItem->pOn);
383 pNewItem->pUsing = sqlite3IdListDup(pOldItem->pUsing);
drhad3cab52002-05-24 02:04:32 +0000384 }
385 return pNew;
386}
danielk19774adee202004-05-08 08:23:19 +0000387IdList *sqlite3IdListDup(IdList *p){
drhff78bd22002-02-27 01:47:11 +0000388 IdList *pNew;
389 int i;
390 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000391 pNew = sqliteMallocRaw( sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +0000392 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000393 pNew->nId = pNew->nAlloc = p->nId;
drh4efc4752004-01-16 15:55:37 +0000394 pNew->a = sqliteMallocRaw( p->nId*sizeof(p->a[0]) );
drhe4697f52002-05-23 02:09:03 +0000395 if( pNew->a==0 ) return 0;
drhff78bd22002-02-27 01:47:11 +0000396 for(i=0; i<p->nId; i++){
drh4efc4752004-01-16 15:55:37 +0000397 struct IdList_item *pNewItem = &pNew->a[i];
398 struct IdList_item *pOldItem = &p->a[i];
399 pNewItem->zName = sqliteStrDup(pOldItem->zName);
400 pNewItem->idx = pOldItem->idx;
drhff78bd22002-02-27 01:47:11 +0000401 }
402 return pNew;
403}
danielk19774adee202004-05-08 08:23:19 +0000404Select *sqlite3SelectDup(Select *p){
drhff78bd22002-02-27 01:47:11 +0000405 Select *pNew;
406 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000407 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000408 if( pNew==0 ) return 0;
409 pNew->isDistinct = p->isDistinct;
danielk19774adee202004-05-08 08:23:19 +0000410 pNew->pEList = sqlite3ExprListDup(p->pEList);
411 pNew->pSrc = sqlite3SrcListDup(p->pSrc);
412 pNew->pWhere = sqlite3ExprDup(p->pWhere);
413 pNew->pGroupBy = sqlite3ExprListDup(p->pGroupBy);
414 pNew->pHaving = sqlite3ExprDup(p->pHaving);
415 pNew->pOrderBy = sqlite3ExprListDup(p->pOrderBy);
drhff78bd22002-02-27 01:47:11 +0000416 pNew->op = p->op;
danielk19774adee202004-05-08 08:23:19 +0000417 pNew->pPrior = sqlite3SelectDup(p->pPrior);
drhff78bd22002-02-27 01:47:11 +0000418 pNew->nLimit = p->nLimit;
419 pNew->nOffset = p->nOffset;
420 pNew->zSelect = 0;
drh7b58dae2003-07-20 01:16:46 +0000421 pNew->iLimit = -1;
422 pNew->iOffset = -1;
danielk1977dc1bdc42004-06-11 10:51:27 +0000423 pNew->ppOpenTemp = 0;
drhff78bd22002-02-27 01:47:11 +0000424 return pNew;
425}
426
427
428/*
drha76b5df2002-02-23 02:32:10 +0000429** Add a new element to the end of an expression list. If pList is
430** initially NULL, then create a new expression list.
431*/
danielk19774adee202004-05-08 08:23:19 +0000432ExprList *sqlite3ExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){
drha76b5df2002-02-23 02:32:10 +0000433 if( pList==0 ){
434 pList = sqliteMalloc( sizeof(ExprList) );
435 if( pList==0 ){
danielk19774adee202004-05-08 08:23:19 +0000436 /* sqlite3ExprDelete(pExpr); // Leak memory if malloc fails */
drha76b5df2002-02-23 02:32:10 +0000437 return 0;
438 }
drh4efc4752004-01-16 15:55:37 +0000439 assert( pList->nAlloc==0 );
drha76b5df2002-02-23 02:32:10 +0000440 }
drh4305d102003-07-30 12:34:12 +0000441 if( pList->nAlloc<=pList->nExpr ){
drh4305d102003-07-30 12:34:12 +0000442 pList->nAlloc = pList->nAlloc*2 + 4;
drh4efc4752004-01-16 15:55:37 +0000443 pList->a = sqliteRealloc(pList->a, pList->nAlloc*sizeof(pList->a[0]));
444 if( pList->a==0 ){
danielk19774adee202004-05-08 08:23:19 +0000445 /* sqlite3ExprDelete(pExpr); // Leak memory if malloc fails */
drh4efc4752004-01-16 15:55:37 +0000446 pList->nExpr = pList->nAlloc = 0;
drha76b5df2002-02-23 02:32:10 +0000447 return pList;
448 }
drha76b5df2002-02-23 02:32:10 +0000449 }
drh4efc4752004-01-16 15:55:37 +0000450 assert( pList->a!=0 );
451 if( pExpr || pName ){
452 struct ExprList_item *pItem = &pList->a[pList->nExpr++];
453 memset(pItem, 0, sizeof(*pItem));
454 pItem->pExpr = pExpr;
drha99db3b2004-06-19 14:49:12 +0000455 pItem->zName = sqlite3NameFromToken(pName);
drha76b5df2002-02-23 02:32:10 +0000456 }
457 return pList;
458}
459
460/*
461** Delete an entire expression list.
462*/
danielk19774adee202004-05-08 08:23:19 +0000463void sqlite3ExprListDelete(ExprList *pList){
drha76b5df2002-02-23 02:32:10 +0000464 int i;
drhbe5c89a2004-07-26 00:31:09 +0000465 struct ExprList_item *pItem;
drha76b5df2002-02-23 02:32:10 +0000466 if( pList==0 ) return;
drh1bdd9b52004-04-23 17:04:44 +0000467 assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
468 assert( pList->nExpr<=pList->nAlloc );
drhbe5c89a2004-07-26 00:31:09 +0000469 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
470 sqlite3ExprDelete(pItem->pExpr);
471 sqliteFree(pItem->zName);
drha76b5df2002-02-23 02:32:10 +0000472 }
473 sqliteFree(pList->a);
474 sqliteFree(pList);
475}
476
477/*
drhfef52082000-06-06 01:50:43 +0000478** Walk an expression tree. Return 1 if the expression is constant
479** and 0 if it involves variables.
drh23989372002-05-21 13:43:04 +0000480**
481** For the purposes of this function, a double-quoted string (ex: "abc")
482** is considered a variable but a single-quoted string (ex: 'abc') is
483** a constant.
drhfef52082000-06-06 01:50:43 +0000484*/
danielk19774adee202004-05-08 08:23:19 +0000485int sqlite3ExprIsConstant(Expr *p){
drhfef52082000-06-06 01:50:43 +0000486 switch( p->op ){
487 case TK_ID:
drh967e8b72000-06-21 13:59:10 +0000488 case TK_COLUMN:
drhfef52082000-06-06 01:50:43 +0000489 case TK_DOT:
drh7bdc0c12003-04-19 17:27:24 +0000490 case TK_FUNCTION:
drhfef52082000-06-06 01:50:43 +0000491 return 0;
drh7bdc0c12003-04-19 17:27:24 +0000492 case TK_NULL:
drh23989372002-05-21 13:43:04 +0000493 case TK_STRING:
danielk1977c572ef72004-05-27 09:28:41 +0000494 case TK_BLOB:
drh92086432002-01-22 14:11:29 +0000495 case TK_INTEGER:
496 case TK_FLOAT:
drh50457892003-09-06 01:10:47 +0000497 case TK_VARIABLE:
drh92086432002-01-22 14:11:29 +0000498 return 1;
drhfef52082000-06-06 01:50:43 +0000499 default: {
danielk19774adee202004-05-08 08:23:19 +0000500 if( p->pLeft && !sqlite3ExprIsConstant(p->pLeft) ) return 0;
501 if( p->pRight && !sqlite3ExprIsConstant(p->pRight) ) return 0;
drhfef52082000-06-06 01:50:43 +0000502 if( p->pList ){
503 int i;
504 for(i=0; i<p->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +0000505 if( !sqlite3ExprIsConstant(p->pList->a[i].pExpr) ) return 0;
drhfef52082000-06-06 01:50:43 +0000506 }
507 }
drh92086432002-01-22 14:11:29 +0000508 return p->pLeft!=0 || p->pRight!=0 || (p->pList && p->pList->nExpr>0);
drhfef52082000-06-06 01:50:43 +0000509 }
510 }
drh92086432002-01-22 14:11:29 +0000511 return 0;
drhfef52082000-06-06 01:50:43 +0000512}
513
514/*
drh202b2df2004-01-06 01:13:46 +0000515** If the given expression codes a constant integer that is small enough
516** to fit in a 32-bit integer, return 1 and put the value of the integer
517** in *pValue. If the expression is not an integer or if it is too big
518** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
drhe4de1fe2002-06-02 16:09:01 +0000519*/
danielk19774adee202004-05-08 08:23:19 +0000520int sqlite3ExprIsInteger(Expr *p, int *pValue){
drhe4de1fe2002-06-02 16:09:01 +0000521 switch( p->op ){
522 case TK_INTEGER: {
drhfec19aa2004-05-19 20:41:03 +0000523 if( sqlite3GetInt32(p->token.z, pValue) ){
drh202b2df2004-01-06 01:13:46 +0000524 return 1;
525 }
526 break;
drhe4de1fe2002-06-02 16:09:01 +0000527 }
528 case TK_STRING: {
drh4c755c02004-08-08 20:22:17 +0000529 const u8 *z = (u8*)p->token.z;
drhe4de1fe2002-06-02 16:09:01 +0000530 int n = p->token.n;
drhbd790ee2002-06-02 18:22:06 +0000531 if( n>0 && z[0]=='-' ){ z++; n--; }
drhe4de1fe2002-06-02 16:09:01 +0000532 while( n>0 && *z && isdigit(*z) ){ z++; n--; }
drhfec19aa2004-05-19 20:41:03 +0000533 if( n==0 && sqlite3GetInt32(p->token.z, pValue) ){
drhe4de1fe2002-06-02 16:09:01 +0000534 return 1;
535 }
536 break;
537 }
drh4b59ab52002-08-24 18:24:51 +0000538 case TK_UPLUS: {
danielk19774adee202004-05-08 08:23:19 +0000539 return sqlite3ExprIsInteger(p->pLeft, pValue);
drh4b59ab52002-08-24 18:24:51 +0000540 }
drhe4de1fe2002-06-02 16:09:01 +0000541 case TK_UMINUS: {
542 int v;
danielk19774adee202004-05-08 08:23:19 +0000543 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
drhe4de1fe2002-06-02 16:09:01 +0000544 *pValue = -v;
545 return 1;
546 }
547 break;
548 }
549 default: break;
550 }
551 return 0;
552}
553
554/*
drhc4a3c772001-04-04 11:48:57 +0000555** Return TRUE if the given string is a row-id column name.
556*/
danielk19774adee202004-05-08 08:23:19 +0000557int sqlite3IsRowid(const char *z){
558 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
559 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
560 if( sqlite3StrICmp(z, "OID")==0 ) return 1;
drhc4a3c772001-04-04 11:48:57 +0000561 return 0;
562}
563
564/*
drh8141f612004-01-25 22:44:58 +0000565** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
566** that name in the set of source tables in pSrcList and make the pExpr
567** expression node refer back to that source column. The following changes
568** are made to pExpr:
569**
570** pExpr->iDb Set the index in db->aDb[] of the database holding
571** the table.
572** pExpr->iTable Set to the cursor number for the table obtained
573** from pSrcList.
574** pExpr->iColumn Set to the column number within the table.
drh8141f612004-01-25 22:44:58 +0000575** pExpr->op Set to TK_COLUMN.
576** pExpr->pLeft Any expression this points to is deleted
577** pExpr->pRight Any expression this points to is deleted.
578**
579** The pDbToken is the name of the database (the "X"). This value may be
580** NULL meaning that name is of the form Y.Z or Z. Any available database
581** can be used. The pTableToken is the name of the table (the "Y"). This
582** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it
583** means that the form of the name is Z and that columns from any table
584** can be used.
585**
586** If the name cannot be resolved unambiguously, leave an error message
587** in pParse and return non-zero. Return zero on success.
588*/
589static int lookupName(
590 Parse *pParse, /* The parsing context */
591 Token *pDbToken, /* Name of the database containing table, or NULL */
592 Token *pTableToken, /* Name of table containing column, or NULL */
593 Token *pColumnToken, /* Name of the column. */
594 SrcList *pSrcList, /* List of tables used to resolve column names */
595 ExprList *pEList, /* List of expressions used to resolve "AS" */
596 Expr *pExpr /* Make this EXPR node point to the selected column */
597){
598 char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */
599 char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */
600 char *zCol = 0; /* Name of the column. The "Z" */
601 int i, j; /* Loop counters */
602 int cnt = 0; /* Number of matching column names */
603 int cntTab = 0; /* Number of matching table names */
drh7e26d752004-02-11 10:35:29 +0000604 sqlite *db = pParse->db; /* The database */
drh8141f612004-01-25 22:44:58 +0000605
606 assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
drha99db3b2004-06-19 14:49:12 +0000607 zDb = sqlite3NameFromToken(pDbToken);
608 zTab = sqlite3NameFromToken(pTableToken);
609 zCol = sqlite3NameFromToken(pColumnToken);
danielk197724b03fd2004-05-10 10:34:34 +0000610 if( sqlite3_malloc_failed ){
drh8141f612004-01-25 22:44:58 +0000611 return 1; /* Leak memory (zDb and zTab) if malloc fails */
612 }
613 assert( zTab==0 || pEList==0 );
614
615 pExpr->iTable = -1;
616 for(i=0; i<pSrcList->nSrc; i++){
617 struct SrcList_item *pItem = &pSrcList->a[i];
618 Table *pTab = pItem->pTab;
619 Column *pCol;
620
621 if( pTab==0 ) continue;
622 assert( pTab->nCol>0 );
623 if( zTab ){
624 if( pItem->zAlias ){
625 char *zTabName = pItem->zAlias;
danielk19774adee202004-05-08 08:23:19 +0000626 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
drh8141f612004-01-25 22:44:58 +0000627 }else{
628 char *zTabName = pTab->zName;
danielk19774adee202004-05-08 08:23:19 +0000629 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
630 if( zDb!=0 && sqlite3StrICmp(db->aDb[pTab->iDb].zName, zDb)!=0 ){
drh8141f612004-01-25 22:44:58 +0000631 continue;
632 }
633 }
634 }
635 if( 0==(cntTab++) ){
636 pExpr->iTable = pItem->iCursor;
637 pExpr->iDb = pTab->iDb;
638 }
639 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
danielk19774adee202004-05-08 08:23:19 +0000640 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
drh8141f612004-01-25 22:44:58 +0000641 cnt++;
642 pExpr->iTable = pItem->iCursor;
643 pExpr->iDb = pTab->iDb;
644 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
645 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
danielk1977a37cdde2004-05-16 11:15:36 +0000646 pExpr->affinity = pTab->aCol[j].affinity;
danielk19770202b292004-06-09 09:55:16 +0000647 pExpr->pColl = pTab->aCol[j].pColl;
drh8141f612004-01-25 22:44:58 +0000648 break;
649 }
650 }
651 }
652
653 /* If we have not already resolved the name, then maybe
654 ** it is a new.* or old.* trigger argument reference
655 */
656 if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
657 TriggerStack *pTriggerStack = pParse->trigStack;
658 Table *pTab = 0;
danielk19774adee202004-05-08 08:23:19 +0000659 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){
drh8141f612004-01-25 22:44:58 +0000660 pExpr->iTable = pTriggerStack->newIdx;
661 assert( pTriggerStack->pTab );
662 pTab = pTriggerStack->pTab;
danielk19774adee202004-05-08 08:23:19 +0000663 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab) == 0 ){
drh8141f612004-01-25 22:44:58 +0000664 pExpr->iTable = pTriggerStack->oldIdx;
665 assert( pTriggerStack->pTab );
666 pTab = pTriggerStack->pTab;
667 }
668
669 if( pTab ){
670 int j;
671 Column *pCol = pTab->aCol;
672
673 pExpr->iDb = pTab->iDb;
674 cntTab++;
675 for(j=0; j < pTab->nCol; j++, pCol++) {
danielk19774adee202004-05-08 08:23:19 +0000676 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
drh8141f612004-01-25 22:44:58 +0000677 cnt++;
678 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
danielk1977a37cdde2004-05-16 11:15:36 +0000679 pExpr->affinity = pTab->aCol[j].affinity;
danielk19770202b292004-06-09 09:55:16 +0000680 pExpr->pColl = pTab->aCol[j].pColl;
drh8141f612004-01-25 22:44:58 +0000681 break;
682 }
683 }
684 }
685 }
686
687 /*
688 ** Perhaps the name is a reference to the ROWID
689 */
danielk19774adee202004-05-08 08:23:19 +0000690 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
drh8141f612004-01-25 22:44:58 +0000691 cnt = 1;
692 pExpr->iColumn = -1;
danielk1977a37cdde2004-05-16 11:15:36 +0000693 pExpr->affinity = SQLITE_AFF_INTEGER;
drh8141f612004-01-25 22:44:58 +0000694 }
695
696 /*
697 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
698 ** might refer to an result-set alias. This happens, for example, when
699 ** we are resolving names in the WHERE clause of the following command:
700 **
701 ** SELECT a+b AS x FROM table WHERE x<10;
702 **
703 ** In cases like this, replace pExpr with a copy of the expression that
704 ** forms the result set entry ("a+b" in the example) and return immediately.
705 ** Note that the expression in the result set should have already been
706 ** resolved by the time the WHERE clause is resolved.
707 */
708 if( cnt==0 && pEList!=0 ){
709 for(j=0; j<pEList->nExpr; j++){
710 char *zAs = pEList->a[j].zName;
danielk19774adee202004-05-08 08:23:19 +0000711 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
drh8141f612004-01-25 22:44:58 +0000712 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
713 pExpr->op = TK_AS;
714 pExpr->iColumn = j;
danielk19774adee202004-05-08 08:23:19 +0000715 pExpr->pLeft = sqlite3ExprDup(pEList->a[j].pExpr);
drh8141f612004-01-25 22:44:58 +0000716 sqliteFree(zCol);
717 assert( zTab==0 && zDb==0 );
718 return 0;
719 }
720 }
721 }
722
723 /*
724 ** If X and Y are NULL (in other words if only the column name Z is
725 ** supplied) and the value of Z is enclosed in double-quotes, then
726 ** Z is a string literal if it doesn't match any column names. In that
727 ** case, we need to return right away and not make any changes to
728 ** pExpr.
729 */
730 if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
731 sqliteFree(zCol);
732 return 0;
733 }
734
735 /*
736 ** cnt==0 means there was not match. cnt>1 means there were two or
737 ** more matches. Either way, we have an error.
738 */
739 if( cnt!=1 ){
740 char *z = 0;
741 char *zErr;
742 zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s";
743 if( zDb ){
danielk19774adee202004-05-08 08:23:19 +0000744 sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, 0);
drh8141f612004-01-25 22:44:58 +0000745 }else if( zTab ){
danielk19774adee202004-05-08 08:23:19 +0000746 sqlite3SetString(&z, zTab, ".", zCol, 0);
drh8141f612004-01-25 22:44:58 +0000747 }else{
748 z = sqliteStrDup(zCol);
749 }
danielk19774adee202004-05-08 08:23:19 +0000750 sqlite3ErrorMsg(pParse, zErr, z);
drh8141f612004-01-25 22:44:58 +0000751 sqliteFree(z);
752 }
753
754 /* Clean up and return
755 */
756 sqliteFree(zDb);
757 sqliteFree(zTab);
758 sqliteFree(zCol);
danielk19774adee202004-05-08 08:23:19 +0000759 sqlite3ExprDelete(pExpr->pLeft);
drh8141f612004-01-25 22:44:58 +0000760 pExpr->pLeft = 0;
danielk19774adee202004-05-08 08:23:19 +0000761 sqlite3ExprDelete(pExpr->pRight);
drh8141f612004-01-25 22:44:58 +0000762 pExpr->pRight = 0;
763 pExpr->op = TK_COLUMN;
danielk19774adee202004-05-08 08:23:19 +0000764 sqlite3AuthRead(pParse, pExpr, pSrcList);
drh8141f612004-01-25 22:44:58 +0000765 return cnt!=1;
766}
767
768/*
drhcce7d172000-05-31 15:34:51 +0000769** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +0000770** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +0000771** index to the table in the table list and a column offset. The
772** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
773** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +0000774** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +0000775** VDBE cursor number for a cursor that is pointing into the referenced
776** table. The Expr.iColumn value is changed to the index of the column
777** of the referenced table. The Expr.iColumn value for the special
778** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
779** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +0000780**
drhfef52082000-06-06 01:50:43 +0000781** We also check for instances of the IN operator. IN comes in two
782** forms:
783**
784** expr IN (exprlist)
785** and
786** expr IN (SELECT ...)
787**
788** The first form is handled by creating a set holding the list
789** of allowed values. The second form causes the SELECT to generate
790** a temporary table.
791**
792** This routine also looks for scalar SELECTs that are part of an expression.
drh19a775c2000-06-05 18:54:46 +0000793** If it finds any, it generates code to write the value of that select
794** into a memory cell.
drhcce7d172000-05-31 15:34:51 +0000795**
drh967e8b72000-06-21 13:59:10 +0000796** Unknown columns or tables provoke an error. The function returns
drhcce7d172000-05-31 15:34:51 +0000797** the number of errors seen and leaves an error message on pParse->zErrMsg.
798*/
danielk19774adee202004-05-08 08:23:19 +0000799int sqlite3ExprResolveIds(
drha2e00042002-01-22 03:13:42 +0000800 Parse *pParse, /* The parser context */
drh8141f612004-01-25 22:44:58 +0000801 SrcList *pSrcList, /* List of tables used to resolve column names */
drha2e00042002-01-22 03:13:42 +0000802 ExprList *pEList, /* List of expressions used to resolve "AS" */
803 Expr *pExpr /* The expression to be analyzed. */
804){
drh6a3ea0e2003-05-02 14:32:12 +0000805 int i;
806
drh8141f612004-01-25 22:44:58 +0000807 if( pExpr==0 || pSrcList==0 ) return 0;
808 for(i=0; i<pSrcList->nSrc; i++){
809 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab );
drh6a3ea0e2003-05-02 14:32:12 +0000810 }
drhcce7d172000-05-31 15:34:51 +0000811 switch( pExpr->op ){
drh23989372002-05-21 13:43:04 +0000812 /* Double-quoted strings (ex: "abc") are used as identifiers if
813 ** possible. Otherwise they remain as strings. Single-quoted
814 ** strings (ex: 'abc') are always string literals.
815 */
816 case TK_STRING: {
817 if( pExpr->token.z[0]=='\'' ) break;
818 /* Fall thru into the TK_ID case if this is a double-quoted string */
819 }
drh8141f612004-01-25 22:44:58 +0000820 /* A lone identifier is the name of a columnd.
drha2e00042002-01-22 03:13:42 +0000821 */
drhcce7d172000-05-31 15:34:51 +0000822 case TK_ID: {
drh8141f612004-01-25 22:44:58 +0000823 if( lookupName(pParse, 0, 0, &pExpr->token, pSrcList, pEList, pExpr) ){
drhcce7d172000-05-31 15:34:51 +0000824 return 1;
drhed6c8672003-01-12 18:02:16 +0000825 }
drhcce7d172000-05-31 15:34:51 +0000826 break;
827 }
828
drhd24cc422003-03-27 12:51:24 +0000829 /* A table name and column name: ID.ID
830 ** Or a database, table and column: ID.ID.ID
831 */
drhcce7d172000-05-31 15:34:51 +0000832 case TK_DOT: {
drh8141f612004-01-25 22:44:58 +0000833 Token *pColumn;
834 Token *pTable;
835 Token *pDb;
836 Expr *pRight;
drhcce7d172000-05-31 15:34:51 +0000837
drhcce7d172000-05-31 15:34:51 +0000838 pRight = pExpr->pRight;
drhd24cc422003-03-27 12:51:24 +0000839 if( pRight->op==TK_ID ){
drh8141f612004-01-25 22:44:58 +0000840 pDb = 0;
841 pTable = &pExpr->pLeft->token;
842 pColumn = &pRight->token;
drhd24cc422003-03-27 12:51:24 +0000843 }else{
drh8141f612004-01-25 22:44:58 +0000844 assert( pRight->op==TK_DOT );
845 pDb = &pExpr->pLeft->token;
846 pTable = &pRight->pLeft->token;
847 pColumn = &pRight->pRight->token;
drhd24cc422003-03-27 12:51:24 +0000848 }
drh8141f612004-01-25 22:44:58 +0000849 if( lookupName(pParse, pDb, pTable, pColumn, pSrcList, 0, pExpr) ){
drhdaffd0e2001-04-11 14:28:42 +0000850 return 1;
851 }
drhcce7d172000-05-31 15:34:51 +0000852 break;
853 }
854
drhfef52082000-06-06 01:50:43 +0000855 case TK_IN: {
danielk1977e014a832004-05-17 10:48:57 +0000856 char affinity;
danielk19774adee202004-05-08 08:23:19 +0000857 Vdbe *v = sqlite3GetVdbe(pParse);
drhd3d39e92004-05-20 22:16:29 +0000858 KeyInfo keyInfo;
danielk19770202b292004-06-09 09:55:16 +0000859 int addr; /* Address of OP_OpenTemp instruction */
drhd3d39e92004-05-20 22:16:29 +0000860
drhfef52082000-06-06 01:50:43 +0000861 if( v==0 ) return 1;
danielk19774adee202004-05-08 08:23:19 +0000862 if( sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pLeft) ){
drhcfab11b2000-06-06 03:31:22 +0000863 return 1;
864 }
danielk1977bf3b7212004-05-18 10:06:24 +0000865 affinity = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +0000866
867 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
868 ** expression it is handled the same way. A temporary table is
869 ** filled with single-field index keys representing the results
870 ** from the SELECT or the <exprlist>.
871 **
872 ** If the 'x' expression is a column value, or the SELECT...
873 ** statement returns a column value, then the affinity of that
874 ** column is used to build the index keys. If both 'x' and the
875 ** SELECT... statement are columns, then numeric affinity is used
876 ** if either column has NUMERIC or INTEGER affinity. If neither
877 ** 'x' nor the SELECT... statement are columns, then numeric affinity
878 ** is used.
879 */
880 pExpr->iTable = pParse->nTab++;
danielk19770202b292004-06-09 09:55:16 +0000881 addr = sqlite3VdbeAddOp(v, OP_OpenTemp, pExpr->iTable, 0);
drhd3d39e92004-05-20 22:16:29 +0000882 memset(&keyInfo, 0, sizeof(keyInfo));
883 keyInfo.nField = 1;
drhf3218fe2004-05-28 08:21:02 +0000884 sqlite3VdbeAddOp(v, OP_SetNumColumns, pExpr->iTable, 1);
danielk1977e014a832004-05-17 10:48:57 +0000885
drhfef52082000-06-06 01:50:43 +0000886 if( pExpr->pSelect ){
887 /* Case 1: expr IN (SELECT ...)
888 **
danielk1977e014a832004-05-17 10:48:57 +0000889 ** Generate code to write the results of the select into the temporary
890 ** table allocated and opened above.
drhfef52082000-06-06 01:50:43 +0000891 */
danielk1977e014a832004-05-17 10:48:57 +0000892 int iParm = pExpr->iTable + (((int)affinity)<<16);
drhbe5c89a2004-07-26 00:31:09 +0000893 ExprList *pEList;
danielk1977e014a832004-05-17 10:48:57 +0000894 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
danielk1977bf3b7212004-05-18 10:06:24 +0000895 sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +0000896 pEList = pExpr->pSelect->pEList;
897 if( pEList && pEList->nExpr>0 ){
danielk19777cedc8d2004-06-10 10:50:08 +0000898 keyInfo.aColl[0] = binaryCompareCollSeq(pParse, pExpr->pLeft,
drhbe5c89a2004-07-26 00:31:09 +0000899 pEList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +0000900 }
drhfef52082000-06-06 01:50:43 +0000901 }else if( pExpr->pList ){
902 /* Case 2: expr IN (exprlist)
903 **
danielk1977e014a832004-05-17 10:48:57 +0000904 ** For each expression, build an index key from the evaluation and
905 ** store it in the temporary table. If <expr> is a column, then use
906 ** that columns affinity when building index keys. If <expr> is not
907 ** a column, use numeric affinity.
drhfef52082000-06-06 01:50:43 +0000908 */
danielk1977e014a832004-05-17 10:48:57 +0000909 int i;
910 char const *affStr;
911 if( !affinity ){
912 affinity = SQLITE_AFF_NUMERIC;
913 }
914 affStr = sqlite3AffinityString(affinity);
danielk19770202b292004-06-09 09:55:16 +0000915 keyInfo.aColl[0] = pExpr->pLeft->pColl;
danielk1977e014a832004-05-17 10:48:57 +0000916
917 /* Loop through each expression in <exprlist>. */
drhfef52082000-06-06 01:50:43 +0000918 for(i=0; i<pExpr->pList->nExpr; i++){
919 Expr *pE2 = pExpr->pList->a[i].pExpr;
danielk1977e014a832004-05-17 10:48:57 +0000920
921 /* Check that the expression is constant and valid. */
danielk19774adee202004-05-08 08:23:19 +0000922 if( !sqlite3ExprIsConstant(pE2) ){
923 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +0000924 "right-hand side of IN operator must be constant");
drhfef52082000-06-06 01:50:43 +0000925 return 1;
926 }
danielk19774adee202004-05-08 08:23:19 +0000927 if( sqlite3ExprCheck(pParse, pE2, 0, 0) ){
drh4794b982000-06-06 13:54:14 +0000928 return 1;
929 }
danielk1977e014a832004-05-17 10:48:57 +0000930
931 /* Evaluate the expression and insert it into the temp table */
932 sqlite3ExprCode(pParse, pE2);
danielk1977ededfd52004-06-17 07:53:01 +0000933 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, affStr, P3_STATIC);
danielk19770f69c1e2004-05-29 11:24:50 +0000934 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +0000935 sqlite3VdbeAddOp(v, OP_PutStrKey, pExpr->iTable, 0);
drhfef52082000-06-06 01:50:43 +0000936 }
937 }
danielk19770202b292004-06-09 09:55:16 +0000938 sqlite3VdbeChangeP3(v, addr, (void *)&keyInfo, P3_KEYINFO);
939
drhcfab11b2000-06-06 03:31:22 +0000940 break;
drhfef52082000-06-06 01:50:43 +0000941 }
942
drh19a775c2000-06-05 18:54:46 +0000943 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +0000944 /* This has to be a scalar SELECT. Generate code to put the
945 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +0000946 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +0000947 */
drh967e8b72000-06-21 13:59:10 +0000948 pExpr->iColumn = pParse->nMem++;
danielk1977bf3b7212004-05-18 10:06:24 +0000949 if(sqlite3Select(pParse, pExpr->pSelect, SRT_Mem,pExpr->iColumn,0,0,0,0)){
drh19a775c2000-06-05 18:54:46 +0000950 return 1;
951 }
952 break;
953 }
954
drhcce7d172000-05-31 15:34:51 +0000955 /* For all else, just recursively walk the tree */
956 default: {
drh4794b982000-06-06 13:54:14 +0000957 if( pExpr->pLeft
danielk19774adee202004-05-08 08:23:19 +0000958 && sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pLeft) ){
drhcce7d172000-05-31 15:34:51 +0000959 return 1;
960 }
961 if( pExpr->pRight
danielk19774adee202004-05-08 08:23:19 +0000962 && sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pRight) ){
drhcce7d172000-05-31 15:34:51 +0000963 return 1;
964 }
965 if( pExpr->pList ){
966 int i;
967 ExprList *pList = pExpr->pList;
968 for(i=0; i<pList->nExpr; i++){
drh832508b2002-03-02 17:04:07 +0000969 Expr *pArg = pList->a[i].pExpr;
danielk19774adee202004-05-08 08:23:19 +0000970 if( sqlite3ExprResolveIds(pParse, pSrcList, pEList, pArg) ){
drhcce7d172000-05-31 15:34:51 +0000971 return 1;
972 }
973 }
974 }
975 }
976 }
977 return 0;
978}
979
drhcce7d172000-05-31 15:34:51 +0000980/*
drh4b59ab52002-08-24 18:24:51 +0000981** pExpr is a node that defines a function of some kind. It might
982** be a syntactic function like "count(x)" or it might be a function
983** that implements an operator, like "a LIKE b".
984**
985** This routine makes *pzName point to the name of the function and
986** *pnName hold the number of characters in the function name.
987*/
988static void getFunctionName(Expr *pExpr, const char **pzName, int *pnName){
989 switch( pExpr->op ){
990 case TK_FUNCTION: {
991 *pzName = pExpr->token.z;
drh6977fea2002-10-22 23:38:04 +0000992 *pnName = pExpr->token.n;
drh4b59ab52002-08-24 18:24:51 +0000993 break;
994 }
995 case TK_LIKE: {
996 *pzName = "like";
997 *pnName = 4;
998 break;
999 }
1000 case TK_GLOB: {
1001 *pzName = "glob";
1002 *pnName = 4;
1003 break;
1004 }
1005 default: {
1006 *pzName = "can't happen";
1007 *pnName = 12;
1008 break;
1009 }
1010 }
1011}
1012
1013/*
drhcce7d172000-05-31 15:34:51 +00001014** Error check the functions in an expression. Make sure all
1015** function names are recognized and all functions have the correct
1016** number of arguments. Leave an error message in pParse->zErrMsg
1017** if anything is amiss. Return the number of errors.
1018**
1019** if pIsAgg is not null and this expression is an aggregate function
1020** (like count(*) or max(value)) then write a 1 into *pIsAgg.
1021*/
danielk19774adee202004-05-08 08:23:19 +00001022int sqlite3ExprCheck(Parse *pParse, Expr *pExpr, int allowAgg, int *pIsAgg){
drhcce7d172000-05-31 15:34:51 +00001023 int nErr = 0;
1024 if( pExpr==0 ) return 0;
drhcce7d172000-05-31 15:34:51 +00001025 switch( pExpr->op ){
drh4b59ab52002-08-24 18:24:51 +00001026 case TK_GLOB:
1027 case TK_LIKE:
drhcce7d172000-05-31 15:34:51 +00001028 case TK_FUNCTION: {
drhc9b84a12002-06-20 11:36:48 +00001029 int n = pExpr->pList ? pExpr->pList->nExpr : 0; /* Number of arguments */
1030 int no_such_func = 0; /* True if no such function exists */
drhc9b84a12002-06-20 11:36:48 +00001031 int wrong_num_args = 0; /* True if wrong number of arguments */
1032 int is_agg = 0; /* True if is an aggregate function */
drhcce7d172000-05-31 15:34:51 +00001033 int i;
drh4b59ab52002-08-24 18:24:51 +00001034 int nId; /* Number of characters in function name */
1035 const char *zId; /* The function name. */
drh0bce8352002-02-28 00:41:10 +00001036 FuncDef *pDef;
danielk1977d8123362004-06-12 09:25:12 +00001037 int enc = pParse->db->enc;
drh0bce8352002-02-28 00:41:10 +00001038
drh4b59ab52002-08-24 18:24:51 +00001039 getFunctionName(pExpr, &zId, &nId);
danielk1977d8123362004-06-12 09:25:12 +00001040 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001041 if( pDef==0 ){
danielk1977d8123362004-06-12 09:25:12 +00001042 pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001043 if( pDef==0 ){
drh268380c2004-02-25 13:47:31 +00001044 no_such_func = 1;
drh0bce8352002-02-28 00:41:10 +00001045 }else{
1046 wrong_num_args = 1;
drhcce7d172000-05-31 15:34:51 +00001047 }
drh0bce8352002-02-28 00:41:10 +00001048 }else{
1049 is_agg = pDef->xFunc==0;
drhcce7d172000-05-31 15:34:51 +00001050 }
drh8e0a2f92002-02-23 23:45:45 +00001051 if( is_agg && !allowAgg ){
danielk19774adee202004-05-08 08:23:19 +00001052 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId, zId);
drh8e0a2f92002-02-23 23:45:45 +00001053 nErr++;
1054 is_agg = 0;
1055 }else if( no_such_func ){
danielk19774adee202004-05-08 08:23:19 +00001056 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
drhcce7d172000-05-31 15:34:51 +00001057 nErr++;
drh8e0a2f92002-02-23 23:45:45 +00001058 }else if( wrong_num_args ){
danielk19774adee202004-05-08 08:23:19 +00001059 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
drhf7a9e1a2004-02-22 18:40:56 +00001060 nId, zId);
drh8e0a2f92002-02-23 23:45:45 +00001061 nErr++;
drhcce7d172000-05-31 15:34:51 +00001062 }
drhf7a9e1a2004-02-22 18:40:56 +00001063 if( is_agg ){
1064 pExpr->op = TK_AGG_FUNCTION;
1065 if( pIsAgg ) *pIsAgg = 1;
1066 }
drhcce7d172000-05-31 15:34:51 +00001067 for(i=0; nErr==0 && i<n; i++){
danielk19774adee202004-05-08 08:23:19 +00001068 nErr = sqlite3ExprCheck(pParse, pExpr->pList->a[i].pExpr,
drh4cfa7932000-06-08 15:10:46 +00001069 allowAgg && !is_agg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +00001070 }
danielk19770202b292004-06-09 09:55:16 +00001071 /* FIX ME: Compute pExpr->affinity based on the expected return
1072 ** type of the function
1073 */
drhcce7d172000-05-31 15:34:51 +00001074 }
1075 default: {
1076 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001077 nErr = sqlite3ExprCheck(pParse, pExpr->pLeft, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +00001078 }
1079 if( nErr==0 && pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001080 nErr = sqlite3ExprCheck(pParse, pExpr->pRight, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +00001081 }
drhfef52082000-06-06 01:50:43 +00001082 if( nErr==0 && pExpr->pList ){
1083 int n = pExpr->pList->nExpr;
1084 int i;
1085 for(i=0; nErr==0 && i<n; i++){
drh22827922000-06-06 17:27:05 +00001086 Expr *pE2 = pExpr->pList->a[i].pExpr;
danielk19774adee202004-05-08 08:23:19 +00001087 nErr = sqlite3ExprCheck(pParse, pE2, allowAgg, pIsAgg);
drhfef52082000-06-06 01:50:43 +00001088 }
1089 }
drhcce7d172000-05-31 15:34:51 +00001090 break;
1091 }
1092 }
1093 return nErr;
1094}
1095
1096/*
drh290c1942004-08-21 17:54:45 +00001097** Call sqlite3ExprResolveIds() followed by sqlite3ExprCheck().
1098**
1099** This routine is provided as a convenience since it is very common
1100** to call ResolveIds() and Check() back to back.
1101*/
1102int sqlite3ExprResolveAndCheck(
1103 Parse *pParse, /* The parser context */
1104 SrcList *pSrcList, /* List of tables used to resolve column names */
1105 ExprList *pEList, /* List of expressions used to resolve "AS" */
1106 Expr *pExpr, /* The expression to be analyzed. */
1107 int allowAgg, /* True to allow aggregate expressions */
1108 int *pIsAgg /* Set to TRUE if aggregates are found */
1109){
1110 if( pExpr==0 ) return 0;
1111 if( sqlite3ExprResolveIds(pParse,pSrcList,pEList,pExpr) ){
1112 return 1;
1113 }
1114 return sqlite3ExprCheck(pParse, pExpr, allowAgg, pIsAgg);
1115}
1116
1117/*
drhfec19aa2004-05-19 20:41:03 +00001118** Generate an instruction that will put the integer describe by
1119** text z[0..n-1] on the stack.
1120*/
1121static void codeInteger(Vdbe *v, const char *z, int n){
1122 int i;
drh6fec0762004-05-30 01:38:43 +00001123 if( sqlite3GetInt32(z, &i) ){
1124 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
1125 }else if( sqlite3FitsIn64Bits(z) ){
1126 sqlite3VdbeOp3(v, OP_Integer, 0, 0, z, n);
drhfec19aa2004-05-19 20:41:03 +00001127 }else{
1128 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n);
1129 }
1130}
1131
1132/*
drhcce7d172000-05-31 15:34:51 +00001133** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +00001134** expression and leave the result on the top of stack.
drhcce7d172000-05-31 15:34:51 +00001135*/
danielk19774adee202004-05-08 08:23:19 +00001136void sqlite3ExprCode(Parse *pParse, Expr *pExpr){
drhcce7d172000-05-31 15:34:51 +00001137 Vdbe *v = pParse->pVdbe;
1138 int op;
drhdaffd0e2001-04-11 14:28:42 +00001139 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001140 switch( pExpr->op ){
1141 case TK_PLUS: op = OP_Add; break;
1142 case TK_MINUS: op = OP_Subtract; break;
1143 case TK_STAR: op = OP_Multiply; break;
1144 case TK_SLASH: op = OP_Divide; break;
1145 case TK_AND: op = OP_And; break;
1146 case TK_OR: op = OP_Or; break;
1147 case TK_LT: op = OP_Lt; break;
1148 case TK_LE: op = OP_Le; break;
1149 case TK_GT: op = OP_Gt; break;
1150 case TK_GE: op = OP_Ge; break;
1151 case TK_NE: op = OP_Ne; break;
1152 case TK_EQ: op = OP_Eq; break;
drhcce7d172000-05-31 15:34:51 +00001153 case TK_ISNULL: op = OP_IsNull; break;
1154 case TK_NOTNULL: op = OP_NotNull; break;
1155 case TK_NOT: op = OP_Not; break;
1156 case TK_UMINUS: op = OP_Negative; break;
drhbf4133c2001-10-13 02:59:08 +00001157 case TK_BITAND: op = OP_BitAnd; break;
1158 case TK_BITOR: op = OP_BitOr; break;
1159 case TK_BITNOT: op = OP_BitNot; break;
1160 case TK_LSHIFT: op = OP_ShiftLeft; break;
1161 case TK_RSHIFT: op = OP_ShiftRight; break;
1162 case TK_REM: op = OP_Remainder; break;
drhfec19aa2004-05-19 20:41:03 +00001163 case TK_FLOAT: op = OP_Real; break;
drh855eb1c2004-08-31 13:45:11 +00001164 case TK_STRING: op = OP_String8; break;
danielk1977c572ef72004-05-27 09:28:41 +00001165 case TK_BLOB: op = OP_HexBlob; break;
drh855eb1c2004-08-31 13:45:11 +00001166 case TK_CONCAT: op = OP_Concat; break;
danielk1977cfe9a692004-06-16 12:00:29 +00001167 default: op = 0; break;
drhcce7d172000-05-31 15:34:51 +00001168 }
1169 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00001170 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +00001171 if( pParse->useAgg ){
danielk19774adee202004-05-08 08:23:19 +00001172 sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drhc4a3c772001-04-04 11:48:57 +00001173 }else if( pExpr->iColumn>=0 ){
danielk19774adee202004-05-08 08:23:19 +00001174 sqlite3VdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn);
drhc4a3c772001-04-04 11:48:57 +00001175 }else{
danielk19774adee202004-05-08 08:23:19 +00001176 sqlite3VdbeAddOp(v, OP_Recno, pExpr->iTable, 0);
drh22827922000-06-06 17:27:05 +00001177 }
drhcce7d172000-05-31 15:34:51 +00001178 break;
1179 }
1180 case TK_INTEGER: {
drhfec19aa2004-05-19 20:41:03 +00001181 codeInteger(v, pExpr->token.z, pExpr->token.n);
1182 break;
1183 }
1184 case TK_FLOAT:
1185 case TK_STRING: {
1186 sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z, pExpr->token.n);
danielk19774adee202004-05-08 08:23:19 +00001187 sqlite3VdbeDequoteP3(v, -1);
drhcce7d172000-05-31 15:34:51 +00001188 break;
1189 }
danielk1977c572ef72004-05-27 09:28:41 +00001190 case TK_BLOB: {
1191 sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z+1, pExpr->token.n-1);
1192 sqlite3VdbeDequoteP3(v, -1);
1193 break;
1194 }
drhcce7d172000-05-31 15:34:51 +00001195 case TK_NULL: {
danielk19770f69c1e2004-05-29 11:24:50 +00001196 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001197 break;
1198 }
drh50457892003-09-06 01:10:47 +00001199 case TK_VARIABLE: {
danielk19774adee202004-05-08 08:23:19 +00001200 sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0);
drh895d7472004-08-20 16:02:39 +00001201 if( pExpr->token.n>1 ){
1202 sqlite3VdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
1203 }
drh50457892003-09-06 01:10:47 +00001204 break;
1205 }
drhc9b84a12002-06-20 11:36:48 +00001206 case TK_LT:
1207 case TK_LE:
1208 case TK_GT:
1209 case TK_GE:
1210 case TK_NE:
1211 case TK_EQ: {
danielk1977a37cdde2004-05-16 11:15:36 +00001212 sqlite3ExprCode(pParse, pExpr->pLeft);
1213 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001214 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 0, 0);
danielk1977a37cdde2004-05-16 11:15:36 +00001215 break;
drhc9b84a12002-06-20 11:36:48 +00001216 }
drhcce7d172000-05-31 15:34:51 +00001217 case TK_AND:
1218 case TK_OR:
1219 case TK_PLUS:
1220 case TK_STAR:
1221 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00001222 case TK_REM:
1223 case TK_BITAND:
1224 case TK_BITOR:
drh17c40292004-07-21 02:53:29 +00001225 case TK_SLASH:
drhbf4133c2001-10-13 02:59:08 +00001226 case TK_LSHIFT:
drh855eb1c2004-08-31 13:45:11 +00001227 case TK_RSHIFT:
drh00400772000-06-16 20:51:26 +00001228 case TK_CONCAT: {
danielk19774adee202004-05-08 08:23:19 +00001229 sqlite3ExprCode(pParse, pExpr->pLeft);
1230 sqlite3ExprCode(pParse, pExpr->pRight);
drh855eb1c2004-08-31 13:45:11 +00001231 sqlite3VdbeAddOp(v, op, 0, 0);
drh00400772000-06-16 20:51:26 +00001232 break;
1233 }
drhcce7d172000-05-31 15:34:51 +00001234 case TK_UMINUS: {
drhfec19aa2004-05-19 20:41:03 +00001235 Expr *pLeft = pExpr->pLeft;
1236 assert( pLeft );
1237 if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
1238 Token *p = &pLeft->token;
drh6e142f52000-06-08 13:36:40 +00001239 char *z = sqliteMalloc( p->n + 2 );
1240 sprintf(z, "-%.*s", p->n, p->z);
drhfec19aa2004-05-19 20:41:03 +00001241 if( pLeft->op==TK_FLOAT ){
1242 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001243 }else{
drhfec19aa2004-05-19 20:41:03 +00001244 codeInteger(v, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001245 }
drh6e142f52000-06-08 13:36:40 +00001246 sqliteFree(z);
1247 break;
1248 }
drh1ccde152000-06-17 13:12:39 +00001249 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +00001250 }
drhbf4133c2001-10-13 02:59:08 +00001251 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00001252 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001253 sqlite3ExprCode(pParse, pExpr->pLeft);
1254 sqlite3VdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001255 break;
1256 }
1257 case TK_ISNULL:
1258 case TK_NOTNULL: {
1259 int dest;
danielk19774adee202004-05-08 08:23:19 +00001260 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1261 sqlite3ExprCode(pParse, pExpr->pLeft);
1262 dest = sqlite3VdbeCurrentAddr(v) + 2;
1263 sqlite3VdbeAddOp(v, op, 1, dest);
1264 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);
drhcce7d172000-05-31 15:34:51 +00001265 }
danielk1977a37cdde2004-05-16 11:15:36 +00001266 break;
drh22827922000-06-06 17:27:05 +00001267 case TK_AGG_FUNCTION: {
danielk19774adee202004-05-08 08:23:19 +00001268 sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drh22827922000-06-06 17:27:05 +00001269 break;
1270 }
drh4b59ab52002-08-24 18:24:51 +00001271 case TK_GLOB:
1272 case TK_LIKE:
drhcce7d172000-05-31 15:34:51 +00001273 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00001274 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00001275 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00001276 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00001277 int nId;
1278 const char *zId;
danielk1977682f68b2004-06-05 10:22:17 +00001279 int p2 = 0;
1280 int i;
danielk1977d8123362004-06-12 09:25:12 +00001281 u8 enc = pParse->db->enc;
danielk1977dc1bdc42004-06-11 10:51:27 +00001282 CollSeq *pColl = 0;
drh4b59ab52002-08-24 18:24:51 +00001283 getFunctionName(pExpr, &zId, &nId);
danielk1977d8123362004-06-12 09:25:12 +00001284 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001285 assert( pDef!=0 );
drhf9b596e2004-05-26 16:54:42 +00001286 nExpr = sqlite3ExprCodeExprList(pParse, pList);
danielk1977682f68b2004-06-05 10:22:17 +00001287 for(i=0; i<nExpr && i<32; i++){
danielk1977d02eb1f2004-06-06 09:44:03 +00001288 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
1289 p2 |= (1<<i);
1290 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001291 if( pDef->needCollSeq && !pColl ){
1292 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
1293 }
1294 }
1295 if( pDef->needCollSeq ){
1296 if( !pColl ) pColl = pParse->db->pDfltColl;
danielk1977d8123362004-06-12 09:25:12 +00001297 sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ);
danielk1977682f68b2004-06-05 10:22:17 +00001298 }
1299 sqlite3VdbeOp3(v, OP_Function, nExpr, p2, (char*)pDef, P3_FUNCDEF);
drhcce7d172000-05-31 15:34:51 +00001300 break;
1301 }
drh19a775c2000-06-05 18:54:46 +00001302 case TK_SELECT: {
danielk19774adee202004-05-08 08:23:19 +00001303 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drh19a775c2000-06-05 18:54:46 +00001304 break;
1305 }
drhfef52082000-06-06 01:50:43 +00001306 case TK_IN: {
1307 int addr;
danielk1977e014a832004-05-17 10:48:57 +00001308 char const *affStr;
1309
1310 /* Figure out the affinity to use to create a key from the results
1311 ** of the expression. affinityStr stores a static string suitable for
danielk1977ededfd52004-06-17 07:53:01 +00001312 ** P3 of OP_MakeRecord.
danielk1977e014a832004-05-17 10:48:57 +00001313 */
1314 affStr = sqlite3AffinityString(comparisonAffinity(pExpr));
1315
danielk19774adee202004-05-08 08:23:19 +00001316 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
danielk1977e014a832004-05-17 10:48:57 +00001317
1318 /* Code the <expr> from "<expr> IN (...)". The temporary table
1319 ** pExpr->iTable contains the values that make up the (...) set.
1320 */
danielk19774adee202004-05-08 08:23:19 +00001321 sqlite3ExprCode(pParse, pExpr->pLeft);
1322 addr = sqlite3VdbeCurrentAddr(v);
danielk1977e014a832004-05-17 10:48:57 +00001323 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); /* addr + 0 */
danielk19774adee202004-05-08 08:23:19 +00001324 sqlite3VdbeAddOp(v, OP_Pop, 2, 0);
danielk19770f69c1e2004-05-29 11:24:50 +00001325 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +00001326 sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7);
danielk1977ededfd52004-06-17 07:53:01 +00001327 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, affStr, P3_STATIC); /* addr + 4 */
danielk1977e014a832004-05-17 10:48:57 +00001328 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7);
1329 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); /* addr + 6 */
1330
drhfef52082000-06-06 01:50:43 +00001331 break;
1332 }
1333 case TK_BETWEEN: {
drhbe5c89a2004-07-26 00:31:09 +00001334 Expr *pLeft = pExpr->pLeft;
1335 struct ExprList_item *pLItem = pExpr->pList->a;
1336 Expr *pRight = pLItem->pExpr;
1337 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001338 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001339 sqlite3ExprCode(pParse, pRight);
1340 codeCompare(pParse, pLeft, pRight, OP_Ge, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001341 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
drhbe5c89a2004-07-26 00:31:09 +00001342 pLItem++;
1343 pRight = pLItem->pExpr;
1344 sqlite3ExprCode(pParse, pRight);
1345 codeCompare(pParse, pLeft, pRight, OP_Le, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001346 sqlite3VdbeAddOp(v, OP_And, 0, 0);
drhfef52082000-06-06 01:50:43 +00001347 break;
1348 }
drh51e9a442004-01-16 16:42:53 +00001349 case TK_UPLUS:
drha2e00042002-01-22 03:13:42 +00001350 case TK_AS: {
danielk19774adee202004-05-08 08:23:19 +00001351 sqlite3ExprCode(pParse, pExpr->pLeft);
drha2e00042002-01-22 03:13:42 +00001352 break;
1353 }
drh17a7f8d2002-03-24 13:13:27 +00001354 case TK_CASE: {
1355 int expr_end_label;
drhf5905aa2002-05-26 20:54:33 +00001356 int jumpInst;
1357 int addr;
1358 int nExpr;
drh17a7f8d2002-03-24 13:13:27 +00001359 int i;
drhbe5c89a2004-07-26 00:31:09 +00001360 ExprList *pEList;
1361 struct ExprList_item *aListelem;
drh17a7f8d2002-03-24 13:13:27 +00001362
1363 assert(pExpr->pList);
1364 assert((pExpr->pList->nExpr % 2) == 0);
1365 assert(pExpr->pList->nExpr > 0);
drhbe5c89a2004-07-26 00:31:09 +00001366 pEList = pExpr->pList;
1367 aListelem = pEList->a;
1368 nExpr = pEList->nExpr;
danielk19774adee202004-05-08 08:23:19 +00001369 expr_end_label = sqlite3VdbeMakeLabel(v);
drh17a7f8d2002-03-24 13:13:27 +00001370 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001371 sqlite3ExprCode(pParse, pExpr->pLeft);
drh17a7f8d2002-03-24 13:13:27 +00001372 }
drhf5905aa2002-05-26 20:54:33 +00001373 for(i=0; i<nExpr; i=i+2){
drhbe5c89a2004-07-26 00:31:09 +00001374 sqlite3ExprCode(pParse, aListelem[i].pExpr);
drh17a7f8d2002-03-24 13:13:27 +00001375 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001376 sqlite3VdbeAddOp(v, OP_Dup, 1, 1);
drhbe5c89a2004-07-26 00:31:09 +00001377 jumpInst = codeCompare(pParse, pExpr->pLeft, aListelem[i].pExpr,
1378 OP_Ne, 0, 1);
danielk19774adee202004-05-08 08:23:19 +00001379 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001380 }else{
danielk19774adee202004-05-08 08:23:19 +00001381 jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001382 }
drhbe5c89a2004-07-26 00:31:09 +00001383 sqlite3ExprCode(pParse, aListelem[i+1].pExpr);
danielk19774adee202004-05-08 08:23:19 +00001384 sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label);
1385 addr = sqlite3VdbeCurrentAddr(v);
1386 sqlite3VdbeChangeP2(v, jumpInst, addr);
drh17a7f8d2002-03-24 13:13:27 +00001387 }
drhf570f012002-05-31 15:51:25 +00001388 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001389 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhf570f012002-05-31 15:51:25 +00001390 }
drh17a7f8d2002-03-24 13:13:27 +00001391 if( pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001392 sqlite3ExprCode(pParse, pExpr->pRight);
drh17a7f8d2002-03-24 13:13:27 +00001393 }else{
danielk19770f69c1e2004-05-29 11:24:50 +00001394 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
drh17a7f8d2002-03-24 13:13:27 +00001395 }
danielk19774adee202004-05-08 08:23:19 +00001396 sqlite3VdbeResolveLabel(v, expr_end_label);
danielk19776f349032002-06-11 02:25:40 +00001397 break;
1398 }
1399 case TK_RAISE: {
1400 if( !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +00001401 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00001402 "RAISE() may only be used within a trigger-program");
danielk19776f349032002-06-11 02:25:40 +00001403 pParse->nErr++;
1404 return;
1405 }
1406 if( pExpr->iColumn == OE_Rollback ||
1407 pExpr->iColumn == OE_Abort ||
1408 pExpr->iColumn == OE_Fail ){
danielk19774adee202004-05-08 08:23:19 +00001409 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn,
drh701a0ae2004-02-22 20:05:00 +00001410 pExpr->token.z, pExpr->token.n);
danielk19774adee202004-05-08 08:23:19 +00001411 sqlite3VdbeDequoteP3(v, -1);
danielk19776f349032002-06-11 02:25:40 +00001412 } else {
1413 assert( pExpr->iColumn == OE_Ignore );
danielk19774adee202004-05-08 08:23:19 +00001414 sqlite3VdbeOp3(v, OP_Goto, 0, pParse->trigStack->ignoreJump,
drh701a0ae2004-02-22 20:05:00 +00001415 "(IGNORE jump)", 0);
danielk19776f349032002-06-11 02:25:40 +00001416 }
drh17a7f8d2002-03-24 13:13:27 +00001417 }
1418 break;
drhcce7d172000-05-31 15:34:51 +00001419 }
drhcce7d172000-05-31 15:34:51 +00001420}
1421
1422/*
drh268380c2004-02-25 13:47:31 +00001423** Generate code that pushes the value of every element of the given
drhf9b596e2004-05-26 16:54:42 +00001424** expression list onto the stack.
drh268380c2004-02-25 13:47:31 +00001425**
1426** Return the number of elements pushed onto the stack.
1427*/
danielk19774adee202004-05-08 08:23:19 +00001428int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00001429 Parse *pParse, /* Parsing context */
drhf9b596e2004-05-26 16:54:42 +00001430 ExprList *pList /* The expression list to be coded */
drh268380c2004-02-25 13:47:31 +00001431){
1432 struct ExprList_item *pItem;
1433 int i, n;
1434 Vdbe *v;
1435 if( pList==0 ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001436 v = sqlite3GetVdbe(pParse);
drh268380c2004-02-25 13:47:31 +00001437 n = pList->nExpr;
1438 for(pItem=pList->a, i=0; i<n; i++, pItem++){
danielk19774adee202004-05-08 08:23:19 +00001439 sqlite3ExprCode(pParse, pItem->pExpr);
drh268380c2004-02-25 13:47:31 +00001440 }
drhf9b596e2004-05-26 16:54:42 +00001441 return n;
drh268380c2004-02-25 13:47:31 +00001442}
1443
1444/*
drhcce7d172000-05-31 15:34:51 +00001445** Generate code for a boolean expression such that a jump is made
1446** to the label "dest" if the expression is true but execution
1447** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00001448**
1449** If the expression evaluates to NULL (neither true nor false), then
1450** take the jump if the jumpIfNull flag is true.
drhcce7d172000-05-31 15:34:51 +00001451*/
danielk19774adee202004-05-08 08:23:19 +00001452void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001453 Vdbe *v = pParse->pVdbe;
1454 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001455 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001456 switch( pExpr->op ){
1457 case TK_LT: op = OP_Lt; break;
1458 case TK_LE: op = OP_Le; break;
1459 case TK_GT: op = OP_Gt; break;
1460 case TK_GE: op = OP_Ge; break;
1461 case TK_NE: op = OP_Ne; break;
1462 case TK_EQ: op = OP_Eq; break;
drhcce7d172000-05-31 15:34:51 +00001463 case TK_ISNULL: op = OP_IsNull; break;
1464 case TK_NOTNULL: op = OP_NotNull; break;
1465 default: break;
1466 }
1467 switch( pExpr->op ){
1468 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001469 int d2 = sqlite3VdbeMakeLabel(v);
1470 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
1471 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
1472 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001473 break;
1474 }
1475 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001476 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
1477 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001478 break;
1479 }
1480 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001481 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001482 break;
1483 }
1484 case TK_LT:
1485 case TK_LE:
1486 case TK_GT:
1487 case TK_GE:
1488 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00001489 case TK_EQ: {
danielk19774adee202004-05-08 08:23:19 +00001490 sqlite3ExprCode(pParse, pExpr->pLeft);
1491 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001492 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001493 break;
1494 }
1495 case TK_ISNULL:
1496 case TK_NOTNULL: {
danielk19774adee202004-05-08 08:23:19 +00001497 sqlite3ExprCode(pParse, pExpr->pLeft);
1498 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001499 break;
1500 }
drhfef52082000-06-06 01:50:43 +00001501 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00001502 /* The expression "x BETWEEN y AND z" is implemented as:
1503 **
1504 ** 1 IF (x < y) GOTO 3
1505 ** 2 IF (x <= z) GOTO <dest>
1506 ** 3 ...
1507 */
drhf5905aa2002-05-26 20:54:33 +00001508 int addr;
drhbe5c89a2004-07-26 00:31:09 +00001509 Expr *pLeft = pExpr->pLeft;
1510 Expr *pRight = pExpr->pList->a[0].pExpr;
1511 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001512 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001513 sqlite3ExprCode(pParse, pRight);
1514 addr = codeCompare(pParse, pLeft, pRight, OP_Lt, 0, !jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00001515
drhbe5c89a2004-07-26 00:31:09 +00001516 pRight = pExpr->pList->a[1].pExpr;
1517 sqlite3ExprCode(pParse, pRight);
1518 codeCompare(pParse, pLeft, pRight, OP_Le, dest, jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00001519
danielk19774adee202004-05-08 08:23:19 +00001520 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
1521 sqlite3VdbeChangeP2(v, addr, sqlite3VdbeCurrentAddr(v));
1522 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00001523 break;
1524 }
drhcce7d172000-05-31 15:34:51 +00001525 default: {
danielk19774adee202004-05-08 08:23:19 +00001526 sqlite3ExprCode(pParse, pExpr);
1527 sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001528 break;
1529 }
1530 }
1531}
1532
1533/*
drh66b89c82000-11-28 20:47:17 +00001534** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00001535** to the label "dest" if the expression is false but execution
1536** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00001537**
1538** If the expression evaluates to NULL (neither true nor false) then
1539** jump if jumpIfNull is true or fall through if jumpIfNull is false.
drhcce7d172000-05-31 15:34:51 +00001540*/
danielk19774adee202004-05-08 08:23:19 +00001541void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001542 Vdbe *v = pParse->pVdbe;
1543 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001544 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001545 switch( pExpr->op ){
1546 case TK_LT: op = OP_Ge; break;
1547 case TK_LE: op = OP_Gt; break;
1548 case TK_GT: op = OP_Le; break;
1549 case TK_GE: op = OP_Lt; break;
1550 case TK_NE: op = OP_Eq; break;
1551 case TK_EQ: op = OP_Ne; break;
drhcce7d172000-05-31 15:34:51 +00001552 case TK_ISNULL: op = OP_NotNull; break;
1553 case TK_NOTNULL: op = OP_IsNull; break;
1554 default: break;
1555 }
1556 switch( pExpr->op ){
1557 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001558 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
1559 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001560 break;
1561 }
1562 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001563 int d2 = sqlite3VdbeMakeLabel(v);
1564 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
1565 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
1566 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001567 break;
1568 }
1569 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001570 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001571 break;
1572 }
1573 case TK_LT:
1574 case TK_LE:
1575 case TK_GT:
1576 case TK_GE:
1577 case TK_NE:
1578 case TK_EQ: {
danielk19774adee202004-05-08 08:23:19 +00001579 sqlite3ExprCode(pParse, pExpr->pLeft);
1580 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001581 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001582 break;
1583 }
drhcce7d172000-05-31 15:34:51 +00001584 case TK_ISNULL:
1585 case TK_NOTNULL: {
danielk19774adee202004-05-08 08:23:19 +00001586 sqlite3ExprCode(pParse, pExpr->pLeft);
1587 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001588 break;
1589 }
drhfef52082000-06-06 01:50:43 +00001590 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00001591 /* The expression is "x BETWEEN y AND z". It is implemented as:
1592 **
1593 ** 1 IF (x >= y) GOTO 3
1594 ** 2 GOTO <dest>
1595 ** 3 IF (x > z) GOTO <dest>
1596 */
drhfef52082000-06-06 01:50:43 +00001597 int addr;
drhbe5c89a2004-07-26 00:31:09 +00001598 Expr *pLeft = pExpr->pLeft;
1599 Expr *pRight = pExpr->pList->a[0].pExpr;
1600 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001601 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001602 sqlite3ExprCode(pParse, pRight);
danielk19774adee202004-05-08 08:23:19 +00001603 addr = sqlite3VdbeCurrentAddr(v);
drhbe5c89a2004-07-26 00:31:09 +00001604 codeCompare(pParse, pLeft, pRight, OP_Ge, addr+3, !jumpIfNull);
1605
danielk19774adee202004-05-08 08:23:19 +00001606 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
1607 sqlite3VdbeAddOp(v, OP_Goto, 0, dest);
drhbe5c89a2004-07-26 00:31:09 +00001608 pRight = pExpr->pList->a[1].pExpr;
1609 sqlite3ExprCode(pParse, pRight);
1610 codeCompare(pParse, pLeft, pRight, OP_Gt, dest, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00001611 break;
1612 }
drhcce7d172000-05-31 15:34:51 +00001613 default: {
danielk19774adee202004-05-08 08:23:19 +00001614 sqlite3ExprCode(pParse, pExpr);
1615 sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001616 break;
1617 }
1618 }
1619}
drh22827922000-06-06 17:27:05 +00001620
1621/*
1622** Do a deep comparison of two expression trees. Return TRUE (non-zero)
1623** if they are identical and return FALSE if they differ in any way.
1624*/
danielk19774adee202004-05-08 08:23:19 +00001625int sqlite3ExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00001626 int i;
1627 if( pA==0 ){
1628 return pB==0;
1629 }else if( pB==0 ){
1630 return 0;
1631 }
1632 if( pA->op!=pB->op ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001633 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
1634 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00001635 if( pA->pList ){
1636 if( pB->pList==0 ) return 0;
1637 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
1638 for(i=0; i<pA->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00001639 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00001640 return 0;
1641 }
1642 }
1643 }else if( pB->pList ){
1644 return 0;
1645 }
1646 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00001647 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drh22827922000-06-06 17:27:05 +00001648 if( pA->token.z ){
1649 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00001650 if( pB->token.n!=pA->token.n ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001651 if( sqlite3StrNICmp(pA->token.z, pB->token.z, pB->token.n)!=0 ) return 0;
drh22827922000-06-06 17:27:05 +00001652 }
1653 return 1;
1654}
1655
1656/*
1657** Add a new element to the pParse->aAgg[] array and return its index.
1658*/
1659static int appendAggInfo(Parse *pParse){
1660 if( (pParse->nAgg & 0x7)==0 ){
1661 int amt = pParse->nAgg + 8;
drh6d4abfb2001-10-22 02:58:08 +00001662 AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0]));
1663 if( aAgg==0 ){
drh22827922000-06-06 17:27:05 +00001664 return -1;
1665 }
drh6d4abfb2001-10-22 02:58:08 +00001666 pParse->aAgg = aAgg;
drh22827922000-06-06 17:27:05 +00001667 }
1668 memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0]));
1669 return pParse->nAgg++;
1670}
1671
1672/*
1673** Analyze the given expression looking for aggregate functions and
1674** for variables that need to be added to the pParse->aAgg[] array.
1675** Make additional entries to the pParse->aAgg[] array as necessary.
1676**
1677** This routine should only be called after the expression has been
danielk19774adee202004-05-08 08:23:19 +00001678** analyzed by sqlite3ExprResolveIds() and sqlite3ExprCheck().
drh22827922000-06-06 17:27:05 +00001679**
1680** If errors are seen, leave an error message in zErrMsg and return
1681** the number of errors.
1682*/
danielk19774adee202004-05-08 08:23:19 +00001683int sqlite3ExprAnalyzeAggregates(Parse *pParse, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00001684 int i;
1685 AggExpr *aAgg;
1686 int nErr = 0;
1687
1688 if( pExpr==0 ) return 0;
1689 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00001690 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +00001691 aAgg = pParse->aAgg;
1692 for(i=0; i<pParse->nAgg; i++){
1693 if( aAgg[i].isAgg ) continue;
1694 if( aAgg[i].pExpr->iTable==pExpr->iTable
drh967e8b72000-06-21 13:59:10 +00001695 && aAgg[i].pExpr->iColumn==pExpr->iColumn ){
drh22827922000-06-06 17:27:05 +00001696 break;
1697 }
1698 }
1699 if( i>=pParse->nAgg ){
1700 i = appendAggInfo(pParse);
1701 if( i<0 ) return 1;
1702 pParse->aAgg[i].isAgg = 0;
1703 pParse->aAgg[i].pExpr = pExpr;
1704 }
drhaaf88722000-06-08 11:25:00 +00001705 pExpr->iAgg = i;
drh22827922000-06-06 17:27:05 +00001706 break;
1707 }
1708 case TK_AGG_FUNCTION: {
drh22827922000-06-06 17:27:05 +00001709 aAgg = pParse->aAgg;
1710 for(i=0; i<pParse->nAgg; i++){
1711 if( !aAgg[i].isAgg ) continue;
danielk19774adee202004-05-08 08:23:19 +00001712 if( sqlite3ExprCompare(aAgg[i].pExpr, pExpr) ){
drh22827922000-06-06 17:27:05 +00001713 break;
1714 }
1715 }
1716 if( i>=pParse->nAgg ){
danielk1977d8123362004-06-12 09:25:12 +00001717 u8 enc = pParse->db->enc;
drh22827922000-06-06 17:27:05 +00001718 i = appendAggInfo(pParse);
1719 if( i<0 ) return 1;
1720 pParse->aAgg[i].isAgg = 1;
1721 pParse->aAgg[i].pExpr = pExpr;
danielk19774adee202004-05-08 08:23:19 +00001722 pParse->aAgg[i].pFunc = sqlite3FindFunction(pParse->db,
drh6977fea2002-10-22 23:38:04 +00001723 pExpr->token.z, pExpr->token.n,
danielk1977d8123362004-06-12 09:25:12 +00001724 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
drh22827922000-06-06 17:27:05 +00001725 }
1726 pExpr->iAgg = i;
1727 break;
1728 }
1729 default: {
1730 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001731 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pLeft);
drh22827922000-06-06 17:27:05 +00001732 }
1733 if( nErr==0 && pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001734 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pRight);
drh22827922000-06-06 17:27:05 +00001735 }
1736 if( nErr==0 && pExpr->pList ){
1737 int n = pExpr->pList->nExpr;
1738 int i;
1739 for(i=0; nErr==0 && i<n; i++){
danielk19774adee202004-05-08 08:23:19 +00001740 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pList->a[i].pExpr);
drh22827922000-06-06 17:27:05 +00001741 }
1742 }
1743 break;
1744 }
1745 }
1746 return nErr;
1747}
drh8e0a2f92002-02-23 23:45:45 +00001748
1749/*
danielk1977d02eb1f2004-06-06 09:44:03 +00001750** Locate a user function given a name, a number of arguments and a flag
1751** indicating whether the function prefers UTF-16 over UTF-8. Return a
1752** pointer to the FuncDef structure that defines that function, or return
1753** NULL if the function does not exist.
drh8e0a2f92002-02-23 23:45:45 +00001754**
drh0bce8352002-02-28 00:41:10 +00001755** If the createFlag argument is true, then a new (blank) FuncDef
drh8e0a2f92002-02-23 23:45:45 +00001756** structure is created and liked into the "db" structure if a
1757** no matching function previously existed. When createFlag is true
1758** and the nArg parameter is -1, then only a function that accepts
1759** any number of arguments will be returned.
1760**
1761** If createFlag is false and nArg is -1, then the first valid
1762** function found is returned. A function is valid if either xFunc
1763** or xStep is non-zero.
danielk1977d02eb1f2004-06-06 09:44:03 +00001764**
1765** If createFlag is false, then a function with the required name and
1766** number of arguments may be returned even if the eTextRep flag does not
1767** match that requested.
drh8e0a2f92002-02-23 23:45:45 +00001768*/
danielk19774adee202004-05-08 08:23:19 +00001769FuncDef *sqlite3FindFunction(
drh8e0a2f92002-02-23 23:45:45 +00001770 sqlite *db, /* An open database */
1771 const char *zName, /* Name of the function. Not null-terminated */
1772 int nName, /* Number of characters in the name */
1773 int nArg, /* Number of arguments. -1 means any number */
danielk1977d8123362004-06-12 09:25:12 +00001774 u8 enc, /* Preferred text encoding */
drh8e0a2f92002-02-23 23:45:45 +00001775 int createFlag /* Create new entry if true and does not otherwise exist */
1776){
danielk1977d02eb1f2004-06-06 09:44:03 +00001777 FuncDef *p; /* Iterator variable */
1778 FuncDef *pFirst; /* First function with this name */
1779 FuncDef *pBest = 0; /* Best match found so far */
danielk1977d8123362004-06-12 09:25:12 +00001780 int bestmatch = 0;
danielk1977d02eb1f2004-06-06 09:44:03 +00001781
danielk1977d8123362004-06-12 09:25:12 +00001782
1783 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
danielk1977d02eb1f2004-06-06 09:44:03 +00001784 if( nArg<-1 ) nArg = -1;
1785
1786 pFirst = (FuncDef*)sqlite3HashFind(&db->aFunc, zName, nName);
1787 for(p=pFirst; p; p=p->pNext){
danielk1977d8123362004-06-12 09:25:12 +00001788 /* During the search for the best function definition, bestmatch is set
1789 ** as follows to indicate the quality of the match with the definition
1790 ** pointed to by pBest:
1791 **
1792 ** 0: pBest is NULL. No match has been found.
1793 ** 1: A variable arguments function that prefers UTF-8 when a UTF-16
1794 ** encoding is requested, or vice versa.
1795 ** 2: A variable arguments function that uses UTF-16BE when UTF-16LE is
1796 ** requested, or vice versa.
1797 ** 3: A variable arguments function using the same text encoding.
1798 ** 4: A function with the exact number of arguments requested that
1799 ** prefers UTF-8 when a UTF-16 encoding is requested, or vice versa.
1800 ** 5: A function with the exact number of arguments requested that
1801 ** prefers UTF-16LE when UTF-16BE is requested, or vice versa.
1802 ** 6: An exact match.
1803 **
1804 ** A larger value of 'matchqual' indicates a more desirable match.
1805 */
danielk1977e12c17b2004-06-23 12:35:14 +00001806 if( p->nArg==-1 || p->nArg==nArg || nArg==-1 ){
danielk1977d8123362004-06-12 09:25:12 +00001807 int match = 1; /* Quality of this match */
1808 if( p->nArg==nArg || nArg==-1 ){
1809 match = 4;
danielk1977d02eb1f2004-06-06 09:44:03 +00001810 }
danielk1977d8123362004-06-12 09:25:12 +00001811 if( enc==p->iPrefEnc ){
1812 match += 2;
danielk1977d02eb1f2004-06-06 09:44:03 +00001813 }
danielk1977d8123362004-06-12 09:25:12 +00001814 else if( (enc==SQLITE_UTF16LE && p->iPrefEnc==SQLITE_UTF16BE) ||
1815 (enc==SQLITE_UTF16BE && p->iPrefEnc==SQLITE_UTF16LE) ){
1816 match += 1;
1817 }
1818
1819 if( match>bestmatch ){
1820 pBest = p;
1821 bestmatch = match;
danielk1977d02eb1f2004-06-06 09:44:03 +00001822 }
1823 }
drh8e0a2f92002-02-23 23:45:45 +00001824 }
danielk1977d02eb1f2004-06-06 09:44:03 +00001825
danielk1977d8123362004-06-12 09:25:12 +00001826 /* If the createFlag parameter is true, and the seach did not reveal an
1827 ** exact match for the name, number of arguments and encoding, then add a
1828 ** new entry to the hash table and return it.
1829 */
1830 if( createFlag && bestmatch<6 &&
danielk1977d02eb1f2004-06-06 09:44:03 +00001831 (pBest = sqliteMalloc(sizeof(*pBest)+nName+1)) ){
1832 pBest->nArg = nArg;
1833 pBest->pNext = pFirst;
1834 pBest->zName = (char*)&pBest[1];
danielk1977d8123362004-06-12 09:25:12 +00001835 pBest->iPrefEnc = enc;
danielk1977d02eb1f2004-06-06 09:44:03 +00001836 memcpy(pBest->zName, zName, nName);
1837 pBest->zName[nName] = 0;
1838 sqlite3HashInsert(&db->aFunc, pBest->zName, nName, (void*)pBest);
drh8e0a2f92002-02-23 23:45:45 +00001839 }
danielk1977d02eb1f2004-06-06 09:44:03 +00001840
1841 if( pBest && (pBest->xStep || pBest->xFunc || createFlag) ){
1842 return pBest;
drh8e0a2f92002-02-23 23:45:45 +00001843 }
danielk1977d02eb1f2004-06-06 09:44:03 +00001844 return 0;
drh8e0a2f92002-02-23 23:45:45 +00001845}