blob: c173be5245c84389d38f45f1538af8a26e92cd28 [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**
drhad6d9462004-09-19 02:15:24 +000015** $Id: expr.c,v 1.163 2004/09/19 02:15:25 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 ){
drhad6d9462004-09-19 02:15:24 +0000244 assert( pLeft->dyn==0 || pLeft->z[pLeft->n]==0 );
245 if( pLeft->z[pLeft->n]!=0 && pRight->dyn==0 ){
drh6977fea2002-10-22 23:38:04 +0000246 pExpr->span.z = pLeft->z;
247 pExpr->span.n = pRight->n + Addr(pRight->z) - Addr(pLeft->z);
drh4b59ab52002-08-24 18:24:51 +0000248 }else{
drh6977fea2002-10-22 23:38:04 +0000249 pExpr->span.z = 0;
drh4b59ab52002-08-24 18:24:51 +0000250 }
drha76b5df2002-02-23 02:32:10 +0000251 }
252}
253
254/*
255** Construct a new expression node for a function with multiple
256** arguments.
257*/
danielk19774adee202004-05-08 08:23:19 +0000258Expr *sqlite3ExprFunction(ExprList *pList, Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000259 Expr *pNew;
260 pNew = sqliteMalloc( sizeof(Expr) );
261 if( pNew==0 ){
danielk19774adee202004-05-08 08:23:19 +0000262 /* sqlite3ExprListDelete(pList); // Leak pList when malloc fails */
drha76b5df2002-02-23 02:32:10 +0000263 return 0;
264 }
265 pNew->op = TK_FUNCTION;
266 pNew->pList = pList;
267 if( pToken ){
drh4b59ab52002-08-24 18:24:51 +0000268 assert( pToken->dyn==0 );
drha76b5df2002-02-23 02:32:10 +0000269 pNew->token = *pToken;
270 }else{
271 pNew->token.z = 0;
drha76b5df2002-02-23 02:32:10 +0000272 }
drh6977fea2002-10-22 23:38:04 +0000273 pNew->span = pNew->token;
drha76b5df2002-02-23 02:32:10 +0000274 return pNew;
275}
276
277/*
drhfa6bc002004-09-07 16:19:52 +0000278** Assign a variable number to an expression that encodes a wildcard
279** in the original SQL statement.
280**
281** Wildcards consisting of a single "?" are assigned the next sequential
282** variable number.
283**
284** Wildcards of the form "?nnn" are assigned the number "nnn". We make
285** sure "nnn" is not too be to avoid a denial of service attack when
286** the SQL statement comes from an external source.
287**
288** Wildcards of the form ":aaa" or "$aaa" are assigned the same number
289** as the previous instance of the same wildcard. Or if this is the first
290** instance of the wildcard, the next sequenial variable number is
291** assigned.
292*/
293void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){
294 Token *pToken;
295 if( pExpr==0 ) return;
296 pToken = &pExpr->token;
297 assert( pToken->n>=1 );
298 assert( pToken->z!=0 );
299 assert( pToken->z[0]!=0 );
300 if( pToken->n==1 ){
301 /* Wildcard of the form "?". Assign the next variable number */
302 pExpr->iTable = ++pParse->nVar;
303 }else if( pToken->z[0]=='?' ){
304 /* Wildcard of the form "?nnn". Convert "nnn" to an integer and
305 ** use it as the variable number */
306 int i;
307 pExpr->iTable = i = atoi(&pToken->z[1]);
308 if( i<1 || i>SQLITE_MAX_VARIABLE_NUMBER ){
309 sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
310 SQLITE_MAX_VARIABLE_NUMBER);
311 }
312 if( i>pParse->nVar ){
313 pParse->nVar = i;
314 }
315 }else{
316 /* Wildcards of the form ":aaa" or "$aaa". Reuse the same variable
317 ** number as the prior appearance of the same name, or if the name
318 ** has never appeared before, reuse the same variable number
319 */
320 int i, n;
321 n = pToken->n;
322 for(i=0; i<pParse->nVarExpr; i++){
323 Expr *pE;
324 if( (pE = pParse->apVarExpr[i])!=0
325 && pE->token.n==n
326 && memcmp(pE->token.z, pToken->z, n)==0 ){
327 pExpr->iTable = pE->iTable;
328 break;
329 }
330 }
331 if( i>=pParse->nVarExpr ){
332 pExpr->iTable = ++pParse->nVar;
333 if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){
334 pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10;
335 pParse->apVarExpr = sqliteRealloc(pParse->apVarExpr,
336 pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0]) );
337 }
338 if( !sqlite3_malloc_failed ){
339 assert( pParse->apVarExpr!=0 );
340 pParse->apVarExpr[pParse->nVarExpr++] = pExpr;
341 }
342 }
343 }
344}
345
346/*
drha2e00042002-01-22 03:13:42 +0000347** Recursively delete an expression tree.
348*/
danielk19774adee202004-05-08 08:23:19 +0000349void sqlite3ExprDelete(Expr *p){
drha2e00042002-01-22 03:13:42 +0000350 if( p==0 ) return;
drh4efc4752004-01-16 15:55:37 +0000351 if( p->span.dyn ) sqliteFree((char*)p->span.z);
352 if( p->token.dyn ) sqliteFree((char*)p->token.z);
danielk19774adee202004-05-08 08:23:19 +0000353 sqlite3ExprDelete(p->pLeft);
354 sqlite3ExprDelete(p->pRight);
355 sqlite3ExprListDelete(p->pList);
356 sqlite3SelectDelete(p->pSelect);
drha2e00042002-01-22 03:13:42 +0000357 sqliteFree(p);
358}
359
drha76b5df2002-02-23 02:32:10 +0000360
361/*
drhff78bd22002-02-27 01:47:11 +0000362** The following group of routines make deep copies of expressions,
363** expression lists, ID lists, and select statements. The copies can
364** be deleted (by being passed to their respective ...Delete() routines)
365** without effecting the originals.
366**
danielk19774adee202004-05-08 08:23:19 +0000367** The expression list, ID, and source lists return by sqlite3ExprListDup(),
368** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
drhad3cab52002-05-24 02:04:32 +0000369** by subsequent calls to sqlite*ListAppend() routines.
drhff78bd22002-02-27 01:47:11 +0000370**
drhad3cab52002-05-24 02:04:32 +0000371** Any tables that the SrcList might point to are not duplicated.
drhff78bd22002-02-27 01:47:11 +0000372*/
danielk19774adee202004-05-08 08:23:19 +0000373Expr *sqlite3ExprDup(Expr *p){
drhff78bd22002-02-27 01:47:11 +0000374 Expr *pNew;
375 if( p==0 ) return 0;
drhfcb78a42003-01-18 20:11:05 +0000376 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000377 if( pNew==0 ) return 0;
drh3b167c72002-06-28 12:18:47 +0000378 memcpy(pNew, p, sizeof(*pNew));
drh6977fea2002-10-22 23:38:04 +0000379 if( p->token.z!=0 ){
drh4b59ab52002-08-24 18:24:51 +0000380 pNew->token.z = sqliteStrDup(p->token.z);
381 pNew->token.dyn = 1;
382 }else{
drh4efc4752004-01-16 15:55:37 +0000383 assert( pNew->token.z==0 );
drh4b59ab52002-08-24 18:24:51 +0000384 }
drh6977fea2002-10-22 23:38:04 +0000385 pNew->span.z = 0;
danielk19774adee202004-05-08 08:23:19 +0000386 pNew->pLeft = sqlite3ExprDup(p->pLeft);
387 pNew->pRight = sqlite3ExprDup(p->pRight);
388 pNew->pList = sqlite3ExprListDup(p->pList);
389 pNew->pSelect = sqlite3SelectDup(p->pSelect);
drhff78bd22002-02-27 01:47:11 +0000390 return pNew;
391}
danielk19774adee202004-05-08 08:23:19 +0000392void sqlite3TokenCopy(Token *pTo, Token *pFrom){
drh4b59ab52002-08-24 18:24:51 +0000393 if( pTo->dyn ) sqliteFree((char*)pTo->z);
drh4b59ab52002-08-24 18:24:51 +0000394 if( pFrom->z ){
395 pTo->n = pFrom->n;
396 pTo->z = sqliteStrNDup(pFrom->z, pFrom->n);
397 pTo->dyn = 1;
398 }else{
drh4b59ab52002-08-24 18:24:51 +0000399 pTo->z = 0;
drh4b59ab52002-08-24 18:24:51 +0000400 }
401}
danielk19774adee202004-05-08 08:23:19 +0000402ExprList *sqlite3ExprListDup(ExprList *p){
drhff78bd22002-02-27 01:47:11 +0000403 ExprList *pNew;
drh3e7bc9c2004-02-21 19:17:17 +0000404 struct ExprList_item *pItem;
drhff78bd22002-02-27 01:47:11 +0000405 int i;
406 if( p==0 ) return 0;
407 pNew = sqliteMalloc( sizeof(*pNew) );
408 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000409 pNew->nExpr = pNew->nAlloc = p->nExpr;
drh3e7bc9c2004-02-21 19:17:17 +0000410 pNew->a = pItem = sqliteMalloc( p->nExpr*sizeof(p->a[0]) );
danielk1977e0048402004-06-15 16:51:01 +0000411 if( pItem==0 ){
412 sqliteFree(pNew);
413 return 0;
414 }
drh1bdd9b52004-04-23 17:04:44 +0000415 for(i=0; i<p->nExpr; i++, pItem++){
drh4b59ab52002-08-24 18:24:51 +0000416 Expr *pNewExpr, *pOldExpr;
danielk19774adee202004-05-08 08:23:19 +0000417 pItem->pExpr = pNewExpr = sqlite3ExprDup(pOldExpr = p->a[i].pExpr);
drh6977fea2002-10-22 23:38:04 +0000418 if( pOldExpr->span.z!=0 && pNewExpr ){
419 /* Always make a copy of the span for top-level expressions in the
drh4b59ab52002-08-24 18:24:51 +0000420 ** expression list. The logic in SELECT processing that determines
421 ** the names of columns in the result set needs this information */
danielk19774adee202004-05-08 08:23:19 +0000422 sqlite3TokenCopy(&pNewExpr->span, &pOldExpr->span);
drh4b59ab52002-08-24 18:24:51 +0000423 }
drh1f3e9052002-10-31 00:09:39 +0000424 assert( pNewExpr==0 || pNewExpr->span.z!=0
danielk197724b03fd2004-05-10 10:34:34 +0000425 || pOldExpr->span.z==0 || sqlite3_malloc_failed );
drh3e7bc9c2004-02-21 19:17:17 +0000426 pItem->zName = sqliteStrDup(p->a[i].zName);
427 pItem->sortOrder = p->a[i].sortOrder;
428 pItem->isAgg = p->a[i].isAgg;
429 pItem->done = 0;
drhff78bd22002-02-27 01:47:11 +0000430 }
431 return pNew;
432}
danielk19774adee202004-05-08 08:23:19 +0000433SrcList *sqlite3SrcListDup(SrcList *p){
drhad3cab52002-05-24 02:04:32 +0000434 SrcList *pNew;
435 int i;
drh113088e2003-03-20 01:16:58 +0000436 int nByte;
drhad3cab52002-05-24 02:04:32 +0000437 if( p==0 ) return 0;
drh113088e2003-03-20 01:16:58 +0000438 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
drh4efc4752004-01-16 15:55:37 +0000439 pNew = sqliteMallocRaw( nByte );
drhad3cab52002-05-24 02:04:32 +0000440 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000441 pNew->nSrc = pNew->nAlloc = p->nSrc;
drhad3cab52002-05-24 02:04:32 +0000442 for(i=0; i<p->nSrc; i++){
drh4efc4752004-01-16 15:55:37 +0000443 struct SrcList_item *pNewItem = &pNew->a[i];
444 struct SrcList_item *pOldItem = &p->a[i];
445 pNewItem->zDatabase = sqliteStrDup(pOldItem->zDatabase);
446 pNewItem->zName = sqliteStrDup(pOldItem->zName);
447 pNewItem->zAlias = sqliteStrDup(pOldItem->zAlias);
448 pNewItem->jointype = pOldItem->jointype;
449 pNewItem->iCursor = pOldItem->iCursor;
450 pNewItem->pTab = 0;
danielk19774adee202004-05-08 08:23:19 +0000451 pNewItem->pSelect = sqlite3SelectDup(pOldItem->pSelect);
452 pNewItem->pOn = sqlite3ExprDup(pOldItem->pOn);
453 pNewItem->pUsing = sqlite3IdListDup(pOldItem->pUsing);
drhad3cab52002-05-24 02:04:32 +0000454 }
455 return pNew;
456}
danielk19774adee202004-05-08 08:23:19 +0000457IdList *sqlite3IdListDup(IdList *p){
drhff78bd22002-02-27 01:47:11 +0000458 IdList *pNew;
459 int i;
460 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000461 pNew = sqliteMallocRaw( sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +0000462 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000463 pNew->nId = pNew->nAlloc = p->nId;
drh4efc4752004-01-16 15:55:37 +0000464 pNew->a = sqliteMallocRaw( p->nId*sizeof(p->a[0]) );
drhe4697f52002-05-23 02:09:03 +0000465 if( pNew->a==0 ) return 0;
drhff78bd22002-02-27 01:47:11 +0000466 for(i=0; i<p->nId; i++){
drh4efc4752004-01-16 15:55:37 +0000467 struct IdList_item *pNewItem = &pNew->a[i];
468 struct IdList_item *pOldItem = &p->a[i];
469 pNewItem->zName = sqliteStrDup(pOldItem->zName);
470 pNewItem->idx = pOldItem->idx;
drhff78bd22002-02-27 01:47:11 +0000471 }
472 return pNew;
473}
danielk19774adee202004-05-08 08:23:19 +0000474Select *sqlite3SelectDup(Select *p){
drhff78bd22002-02-27 01:47:11 +0000475 Select *pNew;
476 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000477 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000478 if( pNew==0 ) return 0;
479 pNew->isDistinct = p->isDistinct;
danielk19774adee202004-05-08 08:23:19 +0000480 pNew->pEList = sqlite3ExprListDup(p->pEList);
481 pNew->pSrc = sqlite3SrcListDup(p->pSrc);
482 pNew->pWhere = sqlite3ExprDup(p->pWhere);
483 pNew->pGroupBy = sqlite3ExprListDup(p->pGroupBy);
484 pNew->pHaving = sqlite3ExprDup(p->pHaving);
485 pNew->pOrderBy = sqlite3ExprListDup(p->pOrderBy);
drhff78bd22002-02-27 01:47:11 +0000486 pNew->op = p->op;
danielk19774adee202004-05-08 08:23:19 +0000487 pNew->pPrior = sqlite3SelectDup(p->pPrior);
drhff78bd22002-02-27 01:47:11 +0000488 pNew->nLimit = p->nLimit;
489 pNew->nOffset = p->nOffset;
490 pNew->zSelect = 0;
drh7b58dae2003-07-20 01:16:46 +0000491 pNew->iLimit = -1;
492 pNew->iOffset = -1;
danielk1977dc1bdc42004-06-11 10:51:27 +0000493 pNew->ppOpenTemp = 0;
drhff78bd22002-02-27 01:47:11 +0000494 return pNew;
495}
496
497
498/*
drha76b5df2002-02-23 02:32:10 +0000499** Add a new element to the end of an expression list. If pList is
500** initially NULL, then create a new expression list.
501*/
danielk19774adee202004-05-08 08:23:19 +0000502ExprList *sqlite3ExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){
drha76b5df2002-02-23 02:32:10 +0000503 if( pList==0 ){
504 pList = sqliteMalloc( sizeof(ExprList) );
505 if( pList==0 ){
danielk19774adee202004-05-08 08:23:19 +0000506 /* sqlite3ExprDelete(pExpr); // Leak memory if malloc fails */
drha76b5df2002-02-23 02:32:10 +0000507 return 0;
508 }
drh4efc4752004-01-16 15:55:37 +0000509 assert( pList->nAlloc==0 );
drha76b5df2002-02-23 02:32:10 +0000510 }
drh4305d102003-07-30 12:34:12 +0000511 if( pList->nAlloc<=pList->nExpr ){
drh4305d102003-07-30 12:34:12 +0000512 pList->nAlloc = pList->nAlloc*2 + 4;
drh4efc4752004-01-16 15:55:37 +0000513 pList->a = sqliteRealloc(pList->a, pList->nAlloc*sizeof(pList->a[0]));
514 if( pList->a==0 ){
danielk19774adee202004-05-08 08:23:19 +0000515 /* sqlite3ExprDelete(pExpr); // Leak memory if malloc fails */
drh4efc4752004-01-16 15:55:37 +0000516 pList->nExpr = pList->nAlloc = 0;
drha76b5df2002-02-23 02:32:10 +0000517 return pList;
518 }
drha76b5df2002-02-23 02:32:10 +0000519 }
drh4efc4752004-01-16 15:55:37 +0000520 assert( pList->a!=0 );
521 if( pExpr || pName ){
522 struct ExprList_item *pItem = &pList->a[pList->nExpr++];
523 memset(pItem, 0, sizeof(*pItem));
524 pItem->pExpr = pExpr;
drha99db3b2004-06-19 14:49:12 +0000525 pItem->zName = sqlite3NameFromToken(pName);
drha76b5df2002-02-23 02:32:10 +0000526 }
527 return pList;
528}
529
530/*
531** Delete an entire expression list.
532*/
danielk19774adee202004-05-08 08:23:19 +0000533void sqlite3ExprListDelete(ExprList *pList){
drha76b5df2002-02-23 02:32:10 +0000534 int i;
drhbe5c89a2004-07-26 00:31:09 +0000535 struct ExprList_item *pItem;
drha76b5df2002-02-23 02:32:10 +0000536 if( pList==0 ) return;
drh1bdd9b52004-04-23 17:04:44 +0000537 assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
538 assert( pList->nExpr<=pList->nAlloc );
drhbe5c89a2004-07-26 00:31:09 +0000539 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
540 sqlite3ExprDelete(pItem->pExpr);
541 sqliteFree(pItem->zName);
drha76b5df2002-02-23 02:32:10 +0000542 }
543 sqliteFree(pList->a);
544 sqliteFree(pList);
545}
546
547/*
drhfef52082000-06-06 01:50:43 +0000548** Walk an expression tree. Return 1 if the expression is constant
549** and 0 if it involves variables.
drh23989372002-05-21 13:43:04 +0000550**
551** For the purposes of this function, a double-quoted string (ex: "abc")
552** is considered a variable but a single-quoted string (ex: 'abc') is
553** a constant.
drhfef52082000-06-06 01:50:43 +0000554*/
danielk19774adee202004-05-08 08:23:19 +0000555int sqlite3ExprIsConstant(Expr *p){
drhfef52082000-06-06 01:50:43 +0000556 switch( p->op ){
557 case TK_ID:
drh967e8b72000-06-21 13:59:10 +0000558 case TK_COLUMN:
drhfef52082000-06-06 01:50:43 +0000559 case TK_DOT:
drh7bdc0c12003-04-19 17:27:24 +0000560 case TK_FUNCTION:
drhfef52082000-06-06 01:50:43 +0000561 return 0;
drh7bdc0c12003-04-19 17:27:24 +0000562 case TK_NULL:
drh23989372002-05-21 13:43:04 +0000563 case TK_STRING:
danielk1977c572ef72004-05-27 09:28:41 +0000564 case TK_BLOB:
drh92086432002-01-22 14:11:29 +0000565 case TK_INTEGER:
566 case TK_FLOAT:
drh50457892003-09-06 01:10:47 +0000567 case TK_VARIABLE:
drh92086432002-01-22 14:11:29 +0000568 return 1;
drhfef52082000-06-06 01:50:43 +0000569 default: {
danielk19774adee202004-05-08 08:23:19 +0000570 if( p->pLeft && !sqlite3ExprIsConstant(p->pLeft) ) return 0;
571 if( p->pRight && !sqlite3ExprIsConstant(p->pRight) ) return 0;
drhfef52082000-06-06 01:50:43 +0000572 if( p->pList ){
573 int i;
574 for(i=0; i<p->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +0000575 if( !sqlite3ExprIsConstant(p->pList->a[i].pExpr) ) return 0;
drhfef52082000-06-06 01:50:43 +0000576 }
577 }
drh92086432002-01-22 14:11:29 +0000578 return p->pLeft!=0 || p->pRight!=0 || (p->pList && p->pList->nExpr>0);
drhfef52082000-06-06 01:50:43 +0000579 }
580 }
drh92086432002-01-22 14:11:29 +0000581 return 0;
drhfef52082000-06-06 01:50:43 +0000582}
583
584/*
drh202b2df2004-01-06 01:13:46 +0000585** If the given expression codes a constant integer that is small enough
586** to fit in a 32-bit integer, return 1 and put the value of the integer
587** in *pValue. If the expression is not an integer or if it is too big
588** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
drhe4de1fe2002-06-02 16:09:01 +0000589*/
danielk19774adee202004-05-08 08:23:19 +0000590int sqlite3ExprIsInteger(Expr *p, int *pValue){
drhe4de1fe2002-06-02 16:09:01 +0000591 switch( p->op ){
592 case TK_INTEGER: {
drhfec19aa2004-05-19 20:41:03 +0000593 if( sqlite3GetInt32(p->token.z, pValue) ){
drh202b2df2004-01-06 01:13:46 +0000594 return 1;
595 }
596 break;
drhe4de1fe2002-06-02 16:09:01 +0000597 }
598 case TK_STRING: {
drh4c755c02004-08-08 20:22:17 +0000599 const u8 *z = (u8*)p->token.z;
drhe4de1fe2002-06-02 16:09:01 +0000600 int n = p->token.n;
drhbd790ee2002-06-02 18:22:06 +0000601 if( n>0 && z[0]=='-' ){ z++; n--; }
drhe4de1fe2002-06-02 16:09:01 +0000602 while( n>0 && *z && isdigit(*z) ){ z++; n--; }
drhfec19aa2004-05-19 20:41:03 +0000603 if( n==0 && sqlite3GetInt32(p->token.z, pValue) ){
drhe4de1fe2002-06-02 16:09:01 +0000604 return 1;
605 }
606 break;
607 }
drh4b59ab52002-08-24 18:24:51 +0000608 case TK_UPLUS: {
danielk19774adee202004-05-08 08:23:19 +0000609 return sqlite3ExprIsInteger(p->pLeft, pValue);
drh4b59ab52002-08-24 18:24:51 +0000610 }
drhe4de1fe2002-06-02 16:09:01 +0000611 case TK_UMINUS: {
612 int v;
danielk19774adee202004-05-08 08:23:19 +0000613 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
drhe4de1fe2002-06-02 16:09:01 +0000614 *pValue = -v;
615 return 1;
616 }
617 break;
618 }
619 default: break;
620 }
621 return 0;
622}
623
624/*
drhc4a3c772001-04-04 11:48:57 +0000625** Return TRUE if the given string is a row-id column name.
626*/
danielk19774adee202004-05-08 08:23:19 +0000627int sqlite3IsRowid(const char *z){
628 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
629 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
630 if( sqlite3StrICmp(z, "OID")==0 ) return 1;
drhc4a3c772001-04-04 11:48:57 +0000631 return 0;
632}
633
634/*
drh8141f612004-01-25 22:44:58 +0000635** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
636** that name in the set of source tables in pSrcList and make the pExpr
637** expression node refer back to that source column. The following changes
638** are made to pExpr:
639**
640** pExpr->iDb Set the index in db->aDb[] of the database holding
641** the table.
642** pExpr->iTable Set to the cursor number for the table obtained
643** from pSrcList.
644** pExpr->iColumn Set to the column number within the table.
drh8141f612004-01-25 22:44:58 +0000645** pExpr->op Set to TK_COLUMN.
646** pExpr->pLeft Any expression this points to is deleted
647** pExpr->pRight Any expression this points to is deleted.
648**
649** The pDbToken is the name of the database (the "X"). This value may be
650** NULL meaning that name is of the form Y.Z or Z. Any available database
651** can be used. The pTableToken is the name of the table (the "Y"). This
652** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it
653** means that the form of the name is Z and that columns from any table
654** can be used.
655**
656** If the name cannot be resolved unambiguously, leave an error message
657** in pParse and return non-zero. Return zero on success.
658*/
659static int lookupName(
660 Parse *pParse, /* The parsing context */
661 Token *pDbToken, /* Name of the database containing table, or NULL */
662 Token *pTableToken, /* Name of table containing column, or NULL */
663 Token *pColumnToken, /* Name of the column. */
664 SrcList *pSrcList, /* List of tables used to resolve column names */
665 ExprList *pEList, /* List of expressions used to resolve "AS" */
666 Expr *pExpr /* Make this EXPR node point to the selected column */
667){
668 char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */
669 char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */
670 char *zCol = 0; /* Name of the column. The "Z" */
671 int i, j; /* Loop counters */
672 int cnt = 0; /* Number of matching column names */
673 int cntTab = 0; /* Number of matching table names */
drh9bb575f2004-09-06 17:24:11 +0000674 sqlite3 *db = pParse->db; /* The database */
drh8141f612004-01-25 22:44:58 +0000675
676 assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
drha99db3b2004-06-19 14:49:12 +0000677 zDb = sqlite3NameFromToken(pDbToken);
678 zTab = sqlite3NameFromToken(pTableToken);
679 zCol = sqlite3NameFromToken(pColumnToken);
danielk197724b03fd2004-05-10 10:34:34 +0000680 if( sqlite3_malloc_failed ){
drh8141f612004-01-25 22:44:58 +0000681 return 1; /* Leak memory (zDb and zTab) if malloc fails */
682 }
683 assert( zTab==0 || pEList==0 );
684
685 pExpr->iTable = -1;
686 for(i=0; i<pSrcList->nSrc; i++){
687 struct SrcList_item *pItem = &pSrcList->a[i];
688 Table *pTab = pItem->pTab;
689 Column *pCol;
690
691 if( pTab==0 ) continue;
692 assert( pTab->nCol>0 );
693 if( zTab ){
694 if( pItem->zAlias ){
695 char *zTabName = pItem->zAlias;
danielk19774adee202004-05-08 08:23:19 +0000696 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
drh8141f612004-01-25 22:44:58 +0000697 }else{
698 char *zTabName = pTab->zName;
danielk19774adee202004-05-08 08:23:19 +0000699 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
700 if( zDb!=0 && sqlite3StrICmp(db->aDb[pTab->iDb].zName, zDb)!=0 ){
drh8141f612004-01-25 22:44:58 +0000701 continue;
702 }
703 }
704 }
705 if( 0==(cntTab++) ){
706 pExpr->iTable = pItem->iCursor;
707 pExpr->iDb = pTab->iDb;
708 }
709 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
danielk19774adee202004-05-08 08:23:19 +0000710 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
drh8141f612004-01-25 22:44:58 +0000711 cnt++;
712 pExpr->iTable = pItem->iCursor;
713 pExpr->iDb = pTab->iDb;
714 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
715 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
danielk1977a37cdde2004-05-16 11:15:36 +0000716 pExpr->affinity = pTab->aCol[j].affinity;
danielk19770202b292004-06-09 09:55:16 +0000717 pExpr->pColl = pTab->aCol[j].pColl;
drh8141f612004-01-25 22:44:58 +0000718 break;
719 }
720 }
721 }
722
723 /* If we have not already resolved the name, then maybe
724 ** it is a new.* or old.* trigger argument reference
725 */
726 if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
727 TriggerStack *pTriggerStack = pParse->trigStack;
728 Table *pTab = 0;
danielk19774adee202004-05-08 08:23:19 +0000729 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){
drh8141f612004-01-25 22:44:58 +0000730 pExpr->iTable = pTriggerStack->newIdx;
731 assert( pTriggerStack->pTab );
732 pTab = pTriggerStack->pTab;
danielk19774adee202004-05-08 08:23:19 +0000733 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab) == 0 ){
drh8141f612004-01-25 22:44:58 +0000734 pExpr->iTable = pTriggerStack->oldIdx;
735 assert( pTriggerStack->pTab );
736 pTab = pTriggerStack->pTab;
737 }
738
739 if( pTab ){
740 int j;
741 Column *pCol = pTab->aCol;
742
743 pExpr->iDb = pTab->iDb;
744 cntTab++;
745 for(j=0; j < pTab->nCol; j++, pCol++) {
danielk19774adee202004-05-08 08:23:19 +0000746 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
drh8141f612004-01-25 22:44:58 +0000747 cnt++;
748 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
danielk1977a37cdde2004-05-16 11:15:36 +0000749 pExpr->affinity = pTab->aCol[j].affinity;
danielk19770202b292004-06-09 09:55:16 +0000750 pExpr->pColl = pTab->aCol[j].pColl;
drh8141f612004-01-25 22:44:58 +0000751 break;
752 }
753 }
754 }
755 }
756
757 /*
758 ** Perhaps the name is a reference to the ROWID
759 */
danielk19774adee202004-05-08 08:23:19 +0000760 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
drh8141f612004-01-25 22:44:58 +0000761 cnt = 1;
762 pExpr->iColumn = -1;
danielk1977a37cdde2004-05-16 11:15:36 +0000763 pExpr->affinity = SQLITE_AFF_INTEGER;
drh8141f612004-01-25 22:44:58 +0000764 }
765
766 /*
767 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
768 ** might refer to an result-set alias. This happens, for example, when
769 ** we are resolving names in the WHERE clause of the following command:
770 **
771 ** SELECT a+b AS x FROM table WHERE x<10;
772 **
773 ** In cases like this, replace pExpr with a copy of the expression that
774 ** forms the result set entry ("a+b" in the example) and return immediately.
775 ** Note that the expression in the result set should have already been
776 ** resolved by the time the WHERE clause is resolved.
777 */
778 if( cnt==0 && pEList!=0 ){
779 for(j=0; j<pEList->nExpr; j++){
780 char *zAs = pEList->a[j].zName;
danielk19774adee202004-05-08 08:23:19 +0000781 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
drh8141f612004-01-25 22:44:58 +0000782 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
783 pExpr->op = TK_AS;
784 pExpr->iColumn = j;
danielk19774adee202004-05-08 08:23:19 +0000785 pExpr->pLeft = sqlite3ExprDup(pEList->a[j].pExpr);
drh8141f612004-01-25 22:44:58 +0000786 sqliteFree(zCol);
787 assert( zTab==0 && zDb==0 );
788 return 0;
789 }
790 }
791 }
792
793 /*
794 ** If X and Y are NULL (in other words if only the column name Z is
795 ** supplied) and the value of Z is enclosed in double-quotes, then
796 ** Z is a string literal if it doesn't match any column names. In that
797 ** case, we need to return right away and not make any changes to
798 ** pExpr.
799 */
800 if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
801 sqliteFree(zCol);
802 return 0;
803 }
804
805 /*
806 ** cnt==0 means there was not match. cnt>1 means there were two or
807 ** more matches. Either way, we have an error.
808 */
809 if( cnt!=1 ){
810 char *z = 0;
811 char *zErr;
812 zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s";
813 if( zDb ){
danielk19774adee202004-05-08 08:23:19 +0000814 sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, 0);
drh8141f612004-01-25 22:44:58 +0000815 }else if( zTab ){
danielk19774adee202004-05-08 08:23:19 +0000816 sqlite3SetString(&z, zTab, ".", zCol, 0);
drh8141f612004-01-25 22:44:58 +0000817 }else{
818 z = sqliteStrDup(zCol);
819 }
danielk19774adee202004-05-08 08:23:19 +0000820 sqlite3ErrorMsg(pParse, zErr, z);
drh8141f612004-01-25 22:44:58 +0000821 sqliteFree(z);
822 }
823
824 /* Clean up and return
825 */
826 sqliteFree(zDb);
827 sqliteFree(zTab);
828 sqliteFree(zCol);
danielk19774adee202004-05-08 08:23:19 +0000829 sqlite3ExprDelete(pExpr->pLeft);
drh8141f612004-01-25 22:44:58 +0000830 pExpr->pLeft = 0;
danielk19774adee202004-05-08 08:23:19 +0000831 sqlite3ExprDelete(pExpr->pRight);
drh8141f612004-01-25 22:44:58 +0000832 pExpr->pRight = 0;
833 pExpr->op = TK_COLUMN;
danielk19774adee202004-05-08 08:23:19 +0000834 sqlite3AuthRead(pParse, pExpr, pSrcList);
drh8141f612004-01-25 22:44:58 +0000835 return cnt!=1;
836}
837
838/*
drhcce7d172000-05-31 15:34:51 +0000839** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +0000840** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +0000841** index to the table in the table list and a column offset. The
842** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
843** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +0000844** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +0000845** VDBE cursor number for a cursor that is pointing into the referenced
846** table. The Expr.iColumn value is changed to the index of the column
847** of the referenced table. The Expr.iColumn value for the special
848** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
849** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +0000850**
drhfef52082000-06-06 01:50:43 +0000851** We also check for instances of the IN operator. IN comes in two
852** forms:
853**
854** expr IN (exprlist)
855** and
856** expr IN (SELECT ...)
857**
858** The first form is handled by creating a set holding the list
859** of allowed values. The second form causes the SELECT to generate
860** a temporary table.
861**
862** This routine also looks for scalar SELECTs that are part of an expression.
drh19a775c2000-06-05 18:54:46 +0000863** If it finds any, it generates code to write the value of that select
864** into a memory cell.
drhcce7d172000-05-31 15:34:51 +0000865**
drh967e8b72000-06-21 13:59:10 +0000866** Unknown columns or tables provoke an error. The function returns
drhcce7d172000-05-31 15:34:51 +0000867** the number of errors seen and leaves an error message on pParse->zErrMsg.
868*/
danielk19774adee202004-05-08 08:23:19 +0000869int sqlite3ExprResolveIds(
drha2e00042002-01-22 03:13:42 +0000870 Parse *pParse, /* The parser context */
drh8141f612004-01-25 22:44:58 +0000871 SrcList *pSrcList, /* List of tables used to resolve column names */
drha2e00042002-01-22 03:13:42 +0000872 ExprList *pEList, /* List of expressions used to resolve "AS" */
873 Expr *pExpr /* The expression to be analyzed. */
874){
drh6a3ea0e2003-05-02 14:32:12 +0000875 int i;
876
drh8141f612004-01-25 22:44:58 +0000877 if( pExpr==0 || pSrcList==0 ) return 0;
878 for(i=0; i<pSrcList->nSrc; i++){
879 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab );
drh6a3ea0e2003-05-02 14:32:12 +0000880 }
drhcce7d172000-05-31 15:34:51 +0000881 switch( pExpr->op ){
drh23989372002-05-21 13:43:04 +0000882 /* Double-quoted strings (ex: "abc") are used as identifiers if
883 ** possible. Otherwise they remain as strings. Single-quoted
884 ** strings (ex: 'abc') are always string literals.
885 */
886 case TK_STRING: {
887 if( pExpr->token.z[0]=='\'' ) break;
888 /* Fall thru into the TK_ID case if this is a double-quoted string */
889 }
drh8141f612004-01-25 22:44:58 +0000890 /* A lone identifier is the name of a columnd.
drha2e00042002-01-22 03:13:42 +0000891 */
drhcce7d172000-05-31 15:34:51 +0000892 case TK_ID: {
drh8141f612004-01-25 22:44:58 +0000893 if( lookupName(pParse, 0, 0, &pExpr->token, pSrcList, pEList, pExpr) ){
drhcce7d172000-05-31 15:34:51 +0000894 return 1;
drhed6c8672003-01-12 18:02:16 +0000895 }
drhcce7d172000-05-31 15:34:51 +0000896 break;
897 }
898
drhd24cc422003-03-27 12:51:24 +0000899 /* A table name and column name: ID.ID
900 ** Or a database, table and column: ID.ID.ID
901 */
drhcce7d172000-05-31 15:34:51 +0000902 case TK_DOT: {
drh8141f612004-01-25 22:44:58 +0000903 Token *pColumn;
904 Token *pTable;
905 Token *pDb;
906 Expr *pRight;
drhcce7d172000-05-31 15:34:51 +0000907
drhcce7d172000-05-31 15:34:51 +0000908 pRight = pExpr->pRight;
drhd24cc422003-03-27 12:51:24 +0000909 if( pRight->op==TK_ID ){
drh8141f612004-01-25 22:44:58 +0000910 pDb = 0;
911 pTable = &pExpr->pLeft->token;
912 pColumn = &pRight->token;
drhd24cc422003-03-27 12:51:24 +0000913 }else{
drh8141f612004-01-25 22:44:58 +0000914 assert( pRight->op==TK_DOT );
915 pDb = &pExpr->pLeft->token;
916 pTable = &pRight->pLeft->token;
917 pColumn = &pRight->pRight->token;
drhd24cc422003-03-27 12:51:24 +0000918 }
drh8141f612004-01-25 22:44:58 +0000919 if( lookupName(pParse, pDb, pTable, pColumn, pSrcList, 0, pExpr) ){
drhdaffd0e2001-04-11 14:28:42 +0000920 return 1;
921 }
drhcce7d172000-05-31 15:34:51 +0000922 break;
923 }
924
drhfef52082000-06-06 01:50:43 +0000925 case TK_IN: {
danielk1977e014a832004-05-17 10:48:57 +0000926 char affinity;
danielk19774adee202004-05-08 08:23:19 +0000927 Vdbe *v = sqlite3GetVdbe(pParse);
drhd3d39e92004-05-20 22:16:29 +0000928 KeyInfo keyInfo;
danielk19770202b292004-06-09 09:55:16 +0000929 int addr; /* Address of OP_OpenTemp instruction */
drhd3d39e92004-05-20 22:16:29 +0000930
drhfef52082000-06-06 01:50:43 +0000931 if( v==0 ) return 1;
danielk19774adee202004-05-08 08:23:19 +0000932 if( sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pLeft) ){
drhcfab11b2000-06-06 03:31:22 +0000933 return 1;
934 }
danielk1977bf3b7212004-05-18 10:06:24 +0000935 affinity = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +0000936
937 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
938 ** expression it is handled the same way. A temporary table is
939 ** filled with single-field index keys representing the results
940 ** from the SELECT or the <exprlist>.
941 **
942 ** If the 'x' expression is a column value, or the SELECT...
943 ** statement returns a column value, then the affinity of that
944 ** column is used to build the index keys. If both 'x' and the
945 ** SELECT... statement are columns, then numeric affinity is used
946 ** if either column has NUMERIC or INTEGER affinity. If neither
947 ** 'x' nor the SELECT... statement are columns, then numeric affinity
948 ** is used.
949 */
950 pExpr->iTable = pParse->nTab++;
danielk19770202b292004-06-09 09:55:16 +0000951 addr = sqlite3VdbeAddOp(v, OP_OpenTemp, pExpr->iTable, 0);
drhd3d39e92004-05-20 22:16:29 +0000952 memset(&keyInfo, 0, sizeof(keyInfo));
953 keyInfo.nField = 1;
drhf3218fe2004-05-28 08:21:02 +0000954 sqlite3VdbeAddOp(v, OP_SetNumColumns, pExpr->iTable, 1);
danielk1977e014a832004-05-17 10:48:57 +0000955
drhfef52082000-06-06 01:50:43 +0000956 if( pExpr->pSelect ){
957 /* Case 1: expr IN (SELECT ...)
958 **
danielk1977e014a832004-05-17 10:48:57 +0000959 ** Generate code to write the results of the select into the temporary
960 ** table allocated and opened above.
drhfef52082000-06-06 01:50:43 +0000961 */
danielk1977e014a832004-05-17 10:48:57 +0000962 int iParm = pExpr->iTable + (((int)affinity)<<16);
drhbe5c89a2004-07-26 00:31:09 +0000963 ExprList *pEList;
danielk1977e014a832004-05-17 10:48:57 +0000964 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
danielk1977bf3b7212004-05-18 10:06:24 +0000965 sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +0000966 pEList = pExpr->pSelect->pEList;
967 if( pEList && pEList->nExpr>0 ){
danielk19777cedc8d2004-06-10 10:50:08 +0000968 keyInfo.aColl[0] = binaryCompareCollSeq(pParse, pExpr->pLeft,
drhbe5c89a2004-07-26 00:31:09 +0000969 pEList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +0000970 }
drhfef52082000-06-06 01:50:43 +0000971 }else if( pExpr->pList ){
972 /* Case 2: expr IN (exprlist)
973 **
danielk1977e014a832004-05-17 10:48:57 +0000974 ** For each expression, build an index key from the evaluation and
975 ** store it in the temporary table. If <expr> is a column, then use
976 ** that columns affinity when building index keys. If <expr> is not
977 ** a column, use numeric affinity.
drhfef52082000-06-06 01:50:43 +0000978 */
danielk1977e014a832004-05-17 10:48:57 +0000979 int i;
980 char const *affStr;
981 if( !affinity ){
982 affinity = SQLITE_AFF_NUMERIC;
983 }
984 affStr = sqlite3AffinityString(affinity);
danielk19770202b292004-06-09 09:55:16 +0000985 keyInfo.aColl[0] = pExpr->pLeft->pColl;
danielk1977e014a832004-05-17 10:48:57 +0000986
987 /* Loop through each expression in <exprlist>. */
drhfef52082000-06-06 01:50:43 +0000988 for(i=0; i<pExpr->pList->nExpr; i++){
989 Expr *pE2 = pExpr->pList->a[i].pExpr;
danielk1977e014a832004-05-17 10:48:57 +0000990
991 /* Check that the expression is constant and valid. */
danielk19774adee202004-05-08 08:23:19 +0000992 if( !sqlite3ExprIsConstant(pE2) ){
993 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +0000994 "right-hand side of IN operator must be constant");
drhfef52082000-06-06 01:50:43 +0000995 return 1;
996 }
danielk19774adee202004-05-08 08:23:19 +0000997 if( sqlite3ExprCheck(pParse, pE2, 0, 0) ){
drh4794b982000-06-06 13:54:14 +0000998 return 1;
999 }
danielk1977e014a832004-05-17 10:48:57 +00001000
1001 /* Evaluate the expression and insert it into the temp table */
1002 sqlite3ExprCode(pParse, pE2);
danielk1977ededfd52004-06-17 07:53:01 +00001003 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, affStr, P3_STATIC);
danielk19770f69c1e2004-05-29 11:24:50 +00001004 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +00001005 sqlite3VdbeAddOp(v, OP_PutStrKey, pExpr->iTable, 0);
drhfef52082000-06-06 01:50:43 +00001006 }
1007 }
danielk19770202b292004-06-09 09:55:16 +00001008 sqlite3VdbeChangeP3(v, addr, (void *)&keyInfo, P3_KEYINFO);
1009
drhcfab11b2000-06-06 03:31:22 +00001010 break;
drhfef52082000-06-06 01:50:43 +00001011 }
1012
drh19a775c2000-06-05 18:54:46 +00001013 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +00001014 /* This has to be a scalar SELECT. Generate code to put the
1015 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +00001016 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +00001017 */
drh967e8b72000-06-21 13:59:10 +00001018 pExpr->iColumn = pParse->nMem++;
danielk1977bf3b7212004-05-18 10:06:24 +00001019 if(sqlite3Select(pParse, pExpr->pSelect, SRT_Mem,pExpr->iColumn,0,0,0,0)){
drh19a775c2000-06-05 18:54:46 +00001020 return 1;
1021 }
1022 break;
1023 }
1024
drhcce7d172000-05-31 15:34:51 +00001025 /* For all else, just recursively walk the tree */
1026 default: {
drh4794b982000-06-06 13:54:14 +00001027 if( pExpr->pLeft
danielk19774adee202004-05-08 08:23:19 +00001028 && sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pLeft) ){
drhcce7d172000-05-31 15:34:51 +00001029 return 1;
1030 }
1031 if( pExpr->pRight
danielk19774adee202004-05-08 08:23:19 +00001032 && sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pRight) ){
drhcce7d172000-05-31 15:34:51 +00001033 return 1;
1034 }
1035 if( pExpr->pList ){
1036 int i;
1037 ExprList *pList = pExpr->pList;
1038 for(i=0; i<pList->nExpr; i++){
drh832508b2002-03-02 17:04:07 +00001039 Expr *pArg = pList->a[i].pExpr;
danielk19774adee202004-05-08 08:23:19 +00001040 if( sqlite3ExprResolveIds(pParse, pSrcList, pEList, pArg) ){
drhcce7d172000-05-31 15:34:51 +00001041 return 1;
1042 }
1043 }
1044 }
1045 }
1046 }
1047 return 0;
1048}
1049
drhcce7d172000-05-31 15:34:51 +00001050/*
drh4b59ab52002-08-24 18:24:51 +00001051** pExpr is a node that defines a function of some kind. It might
1052** be a syntactic function like "count(x)" or it might be a function
1053** that implements an operator, like "a LIKE b".
1054**
1055** This routine makes *pzName point to the name of the function and
1056** *pnName hold the number of characters in the function name.
1057*/
1058static void getFunctionName(Expr *pExpr, const char **pzName, int *pnName){
1059 switch( pExpr->op ){
1060 case TK_FUNCTION: {
1061 *pzName = pExpr->token.z;
drh6977fea2002-10-22 23:38:04 +00001062 *pnName = pExpr->token.n;
drh4b59ab52002-08-24 18:24:51 +00001063 break;
1064 }
1065 case TK_LIKE: {
1066 *pzName = "like";
1067 *pnName = 4;
1068 break;
1069 }
1070 case TK_GLOB: {
1071 *pzName = "glob";
1072 *pnName = 4;
1073 break;
1074 }
1075 default: {
1076 *pzName = "can't happen";
1077 *pnName = 12;
1078 break;
1079 }
1080 }
1081}
1082
1083/*
drhcce7d172000-05-31 15:34:51 +00001084** Error check the functions in an expression. Make sure all
1085** function names are recognized and all functions have the correct
1086** number of arguments. Leave an error message in pParse->zErrMsg
1087** if anything is amiss. Return the number of errors.
1088**
1089** if pIsAgg is not null and this expression is an aggregate function
1090** (like count(*) or max(value)) then write a 1 into *pIsAgg.
1091*/
danielk19774adee202004-05-08 08:23:19 +00001092int sqlite3ExprCheck(Parse *pParse, Expr *pExpr, int allowAgg, int *pIsAgg){
drhcce7d172000-05-31 15:34:51 +00001093 int nErr = 0;
1094 if( pExpr==0 ) return 0;
drhcce7d172000-05-31 15:34:51 +00001095 switch( pExpr->op ){
drh4b59ab52002-08-24 18:24:51 +00001096 case TK_GLOB:
1097 case TK_LIKE:
drhcce7d172000-05-31 15:34:51 +00001098 case TK_FUNCTION: {
drhc9b84a12002-06-20 11:36:48 +00001099 int n = pExpr->pList ? pExpr->pList->nExpr : 0; /* Number of arguments */
1100 int no_such_func = 0; /* True if no such function exists */
drhc9b84a12002-06-20 11:36:48 +00001101 int wrong_num_args = 0; /* True if wrong number of arguments */
1102 int is_agg = 0; /* True if is an aggregate function */
drhcce7d172000-05-31 15:34:51 +00001103 int i;
drh4b59ab52002-08-24 18:24:51 +00001104 int nId; /* Number of characters in function name */
1105 const char *zId; /* The function name. */
drh0bce8352002-02-28 00:41:10 +00001106 FuncDef *pDef;
danielk1977d8123362004-06-12 09:25:12 +00001107 int enc = pParse->db->enc;
drh0bce8352002-02-28 00:41:10 +00001108
drh4b59ab52002-08-24 18:24:51 +00001109 getFunctionName(pExpr, &zId, &nId);
danielk1977d8123362004-06-12 09:25:12 +00001110 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001111 if( pDef==0 ){
danielk1977d8123362004-06-12 09:25:12 +00001112 pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001113 if( pDef==0 ){
drh268380c2004-02-25 13:47:31 +00001114 no_such_func = 1;
drh0bce8352002-02-28 00:41:10 +00001115 }else{
1116 wrong_num_args = 1;
drhcce7d172000-05-31 15:34:51 +00001117 }
drh0bce8352002-02-28 00:41:10 +00001118 }else{
1119 is_agg = pDef->xFunc==0;
drhcce7d172000-05-31 15:34:51 +00001120 }
drh8e0a2f92002-02-23 23:45:45 +00001121 if( is_agg && !allowAgg ){
danielk19774adee202004-05-08 08:23:19 +00001122 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId, zId);
drh8e0a2f92002-02-23 23:45:45 +00001123 nErr++;
1124 is_agg = 0;
1125 }else if( no_such_func ){
danielk19774adee202004-05-08 08:23:19 +00001126 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
drhcce7d172000-05-31 15:34:51 +00001127 nErr++;
drh8e0a2f92002-02-23 23:45:45 +00001128 }else if( wrong_num_args ){
danielk19774adee202004-05-08 08:23:19 +00001129 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
drhf7a9e1a2004-02-22 18:40:56 +00001130 nId, zId);
drh8e0a2f92002-02-23 23:45:45 +00001131 nErr++;
drhcce7d172000-05-31 15:34:51 +00001132 }
drhf7a9e1a2004-02-22 18:40:56 +00001133 if( is_agg ){
1134 pExpr->op = TK_AGG_FUNCTION;
1135 if( pIsAgg ) *pIsAgg = 1;
1136 }
drhcce7d172000-05-31 15:34:51 +00001137 for(i=0; nErr==0 && i<n; i++){
danielk19774adee202004-05-08 08:23:19 +00001138 nErr = sqlite3ExprCheck(pParse, pExpr->pList->a[i].pExpr,
drh4cfa7932000-06-08 15:10:46 +00001139 allowAgg && !is_agg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +00001140 }
danielk19770202b292004-06-09 09:55:16 +00001141 /* FIX ME: Compute pExpr->affinity based on the expected return
1142 ** type of the function
1143 */
drhcce7d172000-05-31 15:34:51 +00001144 }
1145 default: {
1146 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001147 nErr = sqlite3ExprCheck(pParse, pExpr->pLeft, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +00001148 }
1149 if( nErr==0 && pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001150 nErr = sqlite3ExprCheck(pParse, pExpr->pRight, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +00001151 }
drhfef52082000-06-06 01:50:43 +00001152 if( nErr==0 && pExpr->pList ){
1153 int n = pExpr->pList->nExpr;
1154 int i;
1155 for(i=0; nErr==0 && i<n; i++){
drh22827922000-06-06 17:27:05 +00001156 Expr *pE2 = pExpr->pList->a[i].pExpr;
danielk19774adee202004-05-08 08:23:19 +00001157 nErr = sqlite3ExprCheck(pParse, pE2, allowAgg, pIsAgg);
drhfef52082000-06-06 01:50:43 +00001158 }
1159 }
drhcce7d172000-05-31 15:34:51 +00001160 break;
1161 }
1162 }
1163 return nErr;
1164}
1165
1166/*
drh290c1942004-08-21 17:54:45 +00001167** Call sqlite3ExprResolveIds() followed by sqlite3ExprCheck().
1168**
1169** This routine is provided as a convenience since it is very common
1170** to call ResolveIds() and Check() back to back.
1171*/
1172int sqlite3ExprResolveAndCheck(
1173 Parse *pParse, /* The parser context */
1174 SrcList *pSrcList, /* List of tables used to resolve column names */
1175 ExprList *pEList, /* List of expressions used to resolve "AS" */
1176 Expr *pExpr, /* The expression to be analyzed. */
1177 int allowAgg, /* True to allow aggregate expressions */
1178 int *pIsAgg /* Set to TRUE if aggregates are found */
1179){
1180 if( pExpr==0 ) return 0;
1181 if( sqlite3ExprResolveIds(pParse,pSrcList,pEList,pExpr) ){
1182 return 1;
1183 }
1184 return sqlite3ExprCheck(pParse, pExpr, allowAgg, pIsAgg);
1185}
1186
1187/*
drhfec19aa2004-05-19 20:41:03 +00001188** Generate an instruction that will put the integer describe by
1189** text z[0..n-1] on the stack.
1190*/
1191static void codeInteger(Vdbe *v, const char *z, int n){
1192 int i;
drh6fec0762004-05-30 01:38:43 +00001193 if( sqlite3GetInt32(z, &i) ){
1194 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
1195 }else if( sqlite3FitsIn64Bits(z) ){
1196 sqlite3VdbeOp3(v, OP_Integer, 0, 0, z, n);
drhfec19aa2004-05-19 20:41:03 +00001197 }else{
1198 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n);
1199 }
1200}
1201
1202/*
drhcce7d172000-05-31 15:34:51 +00001203** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +00001204** expression and leave the result on the top of stack.
drhcce7d172000-05-31 15:34:51 +00001205*/
danielk19774adee202004-05-08 08:23:19 +00001206void sqlite3ExprCode(Parse *pParse, Expr *pExpr){
drhcce7d172000-05-31 15:34:51 +00001207 Vdbe *v = pParse->pVdbe;
1208 int op;
drhdaffd0e2001-04-11 14:28:42 +00001209 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001210 switch( pExpr->op ){
1211 case TK_PLUS: op = OP_Add; break;
1212 case TK_MINUS: op = OP_Subtract; break;
1213 case TK_STAR: op = OP_Multiply; break;
1214 case TK_SLASH: op = OP_Divide; break;
1215 case TK_AND: op = OP_And; break;
1216 case TK_OR: op = OP_Or; break;
1217 case TK_LT: op = OP_Lt; break;
1218 case TK_LE: op = OP_Le; break;
1219 case TK_GT: op = OP_Gt; break;
1220 case TK_GE: op = OP_Ge; break;
1221 case TK_NE: op = OP_Ne; break;
1222 case TK_EQ: op = OP_Eq; break;
drhcce7d172000-05-31 15:34:51 +00001223 case TK_ISNULL: op = OP_IsNull; break;
1224 case TK_NOTNULL: op = OP_NotNull; break;
1225 case TK_NOT: op = OP_Not; break;
1226 case TK_UMINUS: op = OP_Negative; break;
drhbf4133c2001-10-13 02:59:08 +00001227 case TK_BITAND: op = OP_BitAnd; break;
1228 case TK_BITOR: op = OP_BitOr; break;
1229 case TK_BITNOT: op = OP_BitNot; break;
1230 case TK_LSHIFT: op = OP_ShiftLeft; break;
1231 case TK_RSHIFT: op = OP_ShiftRight; break;
1232 case TK_REM: op = OP_Remainder; break;
drhfec19aa2004-05-19 20:41:03 +00001233 case TK_FLOAT: op = OP_Real; break;
drh855eb1c2004-08-31 13:45:11 +00001234 case TK_STRING: op = OP_String8; break;
danielk1977c572ef72004-05-27 09:28:41 +00001235 case TK_BLOB: op = OP_HexBlob; break;
drh855eb1c2004-08-31 13:45:11 +00001236 case TK_CONCAT: op = OP_Concat; break;
danielk1977cfe9a692004-06-16 12:00:29 +00001237 default: op = 0; break;
drhcce7d172000-05-31 15:34:51 +00001238 }
1239 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00001240 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +00001241 if( pParse->useAgg ){
danielk19774adee202004-05-08 08:23:19 +00001242 sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drhc4a3c772001-04-04 11:48:57 +00001243 }else if( pExpr->iColumn>=0 ){
danielk19774adee202004-05-08 08:23:19 +00001244 sqlite3VdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn);
drhad6d9462004-09-19 02:15:24 +00001245 VdbeComment((v, "# %T", &pExpr->span));
drhc4a3c772001-04-04 11:48:57 +00001246 }else{
danielk19774adee202004-05-08 08:23:19 +00001247 sqlite3VdbeAddOp(v, OP_Recno, pExpr->iTable, 0);
drh22827922000-06-06 17:27:05 +00001248 }
drhcce7d172000-05-31 15:34:51 +00001249 break;
1250 }
1251 case TK_INTEGER: {
drhfec19aa2004-05-19 20:41:03 +00001252 codeInteger(v, pExpr->token.z, pExpr->token.n);
1253 break;
1254 }
1255 case TK_FLOAT:
1256 case TK_STRING: {
1257 sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z, pExpr->token.n);
danielk19774adee202004-05-08 08:23:19 +00001258 sqlite3VdbeDequoteP3(v, -1);
drhcce7d172000-05-31 15:34:51 +00001259 break;
1260 }
danielk1977c572ef72004-05-27 09:28:41 +00001261 case TK_BLOB: {
1262 sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z+1, pExpr->token.n-1);
1263 sqlite3VdbeDequoteP3(v, -1);
1264 break;
1265 }
drhcce7d172000-05-31 15:34:51 +00001266 case TK_NULL: {
danielk19770f69c1e2004-05-29 11:24:50 +00001267 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001268 break;
1269 }
drh50457892003-09-06 01:10:47 +00001270 case TK_VARIABLE: {
danielk19774adee202004-05-08 08:23:19 +00001271 sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0);
drh895d7472004-08-20 16:02:39 +00001272 if( pExpr->token.n>1 ){
1273 sqlite3VdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
1274 }
drh50457892003-09-06 01:10:47 +00001275 break;
1276 }
drhc9b84a12002-06-20 11:36:48 +00001277 case TK_LT:
1278 case TK_LE:
1279 case TK_GT:
1280 case TK_GE:
1281 case TK_NE:
1282 case TK_EQ: {
danielk1977a37cdde2004-05-16 11:15:36 +00001283 sqlite3ExprCode(pParse, pExpr->pLeft);
1284 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001285 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 0, 0);
danielk1977a37cdde2004-05-16 11:15:36 +00001286 break;
drhc9b84a12002-06-20 11:36:48 +00001287 }
drhcce7d172000-05-31 15:34:51 +00001288 case TK_AND:
1289 case TK_OR:
1290 case TK_PLUS:
1291 case TK_STAR:
1292 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00001293 case TK_REM:
1294 case TK_BITAND:
1295 case TK_BITOR:
drh17c40292004-07-21 02:53:29 +00001296 case TK_SLASH:
drhbf4133c2001-10-13 02:59:08 +00001297 case TK_LSHIFT:
drh855eb1c2004-08-31 13:45:11 +00001298 case TK_RSHIFT:
drh00400772000-06-16 20:51:26 +00001299 case TK_CONCAT: {
danielk19774adee202004-05-08 08:23:19 +00001300 sqlite3ExprCode(pParse, pExpr->pLeft);
1301 sqlite3ExprCode(pParse, pExpr->pRight);
drh855eb1c2004-08-31 13:45:11 +00001302 sqlite3VdbeAddOp(v, op, 0, 0);
drh00400772000-06-16 20:51:26 +00001303 break;
1304 }
drhcce7d172000-05-31 15:34:51 +00001305 case TK_UMINUS: {
drhfec19aa2004-05-19 20:41:03 +00001306 Expr *pLeft = pExpr->pLeft;
1307 assert( pLeft );
1308 if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
1309 Token *p = &pLeft->token;
drh6e142f52000-06-08 13:36:40 +00001310 char *z = sqliteMalloc( p->n + 2 );
1311 sprintf(z, "-%.*s", p->n, p->z);
drhfec19aa2004-05-19 20:41:03 +00001312 if( pLeft->op==TK_FLOAT ){
1313 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001314 }else{
drhfec19aa2004-05-19 20:41:03 +00001315 codeInteger(v, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001316 }
drh6e142f52000-06-08 13:36:40 +00001317 sqliteFree(z);
1318 break;
1319 }
drh1ccde152000-06-17 13:12:39 +00001320 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +00001321 }
drhbf4133c2001-10-13 02:59:08 +00001322 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00001323 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001324 sqlite3ExprCode(pParse, pExpr->pLeft);
1325 sqlite3VdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001326 break;
1327 }
1328 case TK_ISNULL:
1329 case TK_NOTNULL: {
1330 int dest;
danielk19774adee202004-05-08 08:23:19 +00001331 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1332 sqlite3ExprCode(pParse, pExpr->pLeft);
1333 dest = sqlite3VdbeCurrentAddr(v) + 2;
1334 sqlite3VdbeAddOp(v, op, 1, dest);
1335 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);
drhcce7d172000-05-31 15:34:51 +00001336 }
danielk1977a37cdde2004-05-16 11:15:36 +00001337 break;
drh22827922000-06-06 17:27:05 +00001338 case TK_AGG_FUNCTION: {
danielk19774adee202004-05-08 08:23:19 +00001339 sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drh22827922000-06-06 17:27:05 +00001340 break;
1341 }
drh4b59ab52002-08-24 18:24:51 +00001342 case TK_GLOB:
1343 case TK_LIKE:
drhcce7d172000-05-31 15:34:51 +00001344 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00001345 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00001346 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00001347 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00001348 int nId;
1349 const char *zId;
danielk1977682f68b2004-06-05 10:22:17 +00001350 int p2 = 0;
1351 int i;
danielk1977d8123362004-06-12 09:25:12 +00001352 u8 enc = pParse->db->enc;
danielk1977dc1bdc42004-06-11 10:51:27 +00001353 CollSeq *pColl = 0;
drh4b59ab52002-08-24 18:24:51 +00001354 getFunctionName(pExpr, &zId, &nId);
danielk1977d8123362004-06-12 09:25:12 +00001355 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001356 assert( pDef!=0 );
drhf9b596e2004-05-26 16:54:42 +00001357 nExpr = sqlite3ExprCodeExprList(pParse, pList);
danielk1977682f68b2004-06-05 10:22:17 +00001358 for(i=0; i<nExpr && i<32; i++){
danielk1977d02eb1f2004-06-06 09:44:03 +00001359 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
1360 p2 |= (1<<i);
1361 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001362 if( pDef->needCollSeq && !pColl ){
1363 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
1364 }
1365 }
1366 if( pDef->needCollSeq ){
1367 if( !pColl ) pColl = pParse->db->pDfltColl;
danielk1977d8123362004-06-12 09:25:12 +00001368 sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ);
danielk1977682f68b2004-06-05 10:22:17 +00001369 }
1370 sqlite3VdbeOp3(v, OP_Function, nExpr, p2, (char*)pDef, P3_FUNCDEF);
drhcce7d172000-05-31 15:34:51 +00001371 break;
1372 }
drh19a775c2000-06-05 18:54:46 +00001373 case TK_SELECT: {
danielk19774adee202004-05-08 08:23:19 +00001374 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drhad6d9462004-09-19 02:15:24 +00001375 VdbeComment((v, "# load subquery result"));
drh19a775c2000-06-05 18:54:46 +00001376 break;
1377 }
drhfef52082000-06-06 01:50:43 +00001378 case TK_IN: {
1379 int addr;
danielk1977e014a832004-05-17 10:48:57 +00001380 char const *affStr;
1381
1382 /* Figure out the affinity to use to create a key from the results
1383 ** of the expression. affinityStr stores a static string suitable for
danielk1977ededfd52004-06-17 07:53:01 +00001384 ** P3 of OP_MakeRecord.
danielk1977e014a832004-05-17 10:48:57 +00001385 */
1386 affStr = sqlite3AffinityString(comparisonAffinity(pExpr));
1387
danielk19774adee202004-05-08 08:23:19 +00001388 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
danielk1977e014a832004-05-17 10:48:57 +00001389
1390 /* Code the <expr> from "<expr> IN (...)". The temporary table
1391 ** pExpr->iTable contains the values that make up the (...) set.
1392 */
danielk19774adee202004-05-08 08:23:19 +00001393 sqlite3ExprCode(pParse, pExpr->pLeft);
1394 addr = sqlite3VdbeCurrentAddr(v);
danielk1977e014a832004-05-17 10:48:57 +00001395 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); /* addr + 0 */
danielk19774adee202004-05-08 08:23:19 +00001396 sqlite3VdbeAddOp(v, OP_Pop, 2, 0);
danielk19770f69c1e2004-05-29 11:24:50 +00001397 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +00001398 sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7);
danielk1977ededfd52004-06-17 07:53:01 +00001399 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, affStr, P3_STATIC); /* addr + 4 */
danielk1977e014a832004-05-17 10:48:57 +00001400 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7);
1401 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); /* addr + 6 */
1402
drhfef52082000-06-06 01:50:43 +00001403 break;
1404 }
1405 case TK_BETWEEN: {
drhbe5c89a2004-07-26 00:31:09 +00001406 Expr *pLeft = pExpr->pLeft;
1407 struct ExprList_item *pLItem = pExpr->pList->a;
1408 Expr *pRight = pLItem->pExpr;
1409 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001410 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001411 sqlite3ExprCode(pParse, pRight);
1412 codeCompare(pParse, pLeft, pRight, OP_Ge, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001413 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
drhbe5c89a2004-07-26 00:31:09 +00001414 pLItem++;
1415 pRight = pLItem->pExpr;
1416 sqlite3ExprCode(pParse, pRight);
1417 codeCompare(pParse, pLeft, pRight, OP_Le, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001418 sqlite3VdbeAddOp(v, OP_And, 0, 0);
drhfef52082000-06-06 01:50:43 +00001419 break;
1420 }
drh51e9a442004-01-16 16:42:53 +00001421 case TK_UPLUS:
drha2e00042002-01-22 03:13:42 +00001422 case TK_AS: {
danielk19774adee202004-05-08 08:23:19 +00001423 sqlite3ExprCode(pParse, pExpr->pLeft);
drha2e00042002-01-22 03:13:42 +00001424 break;
1425 }
drh17a7f8d2002-03-24 13:13:27 +00001426 case TK_CASE: {
1427 int expr_end_label;
drhf5905aa2002-05-26 20:54:33 +00001428 int jumpInst;
1429 int addr;
1430 int nExpr;
drh17a7f8d2002-03-24 13:13:27 +00001431 int i;
drhbe5c89a2004-07-26 00:31:09 +00001432 ExprList *pEList;
1433 struct ExprList_item *aListelem;
drh17a7f8d2002-03-24 13:13:27 +00001434
1435 assert(pExpr->pList);
1436 assert((pExpr->pList->nExpr % 2) == 0);
1437 assert(pExpr->pList->nExpr > 0);
drhbe5c89a2004-07-26 00:31:09 +00001438 pEList = pExpr->pList;
1439 aListelem = pEList->a;
1440 nExpr = pEList->nExpr;
danielk19774adee202004-05-08 08:23:19 +00001441 expr_end_label = sqlite3VdbeMakeLabel(v);
drh17a7f8d2002-03-24 13:13:27 +00001442 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001443 sqlite3ExprCode(pParse, pExpr->pLeft);
drh17a7f8d2002-03-24 13:13:27 +00001444 }
drhf5905aa2002-05-26 20:54:33 +00001445 for(i=0; i<nExpr; i=i+2){
drhbe5c89a2004-07-26 00:31:09 +00001446 sqlite3ExprCode(pParse, aListelem[i].pExpr);
drh17a7f8d2002-03-24 13:13:27 +00001447 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001448 sqlite3VdbeAddOp(v, OP_Dup, 1, 1);
drhbe5c89a2004-07-26 00:31:09 +00001449 jumpInst = codeCompare(pParse, pExpr->pLeft, aListelem[i].pExpr,
1450 OP_Ne, 0, 1);
danielk19774adee202004-05-08 08:23:19 +00001451 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001452 }else{
danielk19774adee202004-05-08 08:23:19 +00001453 jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001454 }
drhbe5c89a2004-07-26 00:31:09 +00001455 sqlite3ExprCode(pParse, aListelem[i+1].pExpr);
danielk19774adee202004-05-08 08:23:19 +00001456 sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label);
1457 addr = sqlite3VdbeCurrentAddr(v);
1458 sqlite3VdbeChangeP2(v, jumpInst, addr);
drh17a7f8d2002-03-24 13:13:27 +00001459 }
drhf570f012002-05-31 15:51:25 +00001460 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001461 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhf570f012002-05-31 15:51:25 +00001462 }
drh17a7f8d2002-03-24 13:13:27 +00001463 if( pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001464 sqlite3ExprCode(pParse, pExpr->pRight);
drh17a7f8d2002-03-24 13:13:27 +00001465 }else{
danielk19770f69c1e2004-05-29 11:24:50 +00001466 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
drh17a7f8d2002-03-24 13:13:27 +00001467 }
danielk19774adee202004-05-08 08:23:19 +00001468 sqlite3VdbeResolveLabel(v, expr_end_label);
danielk19776f349032002-06-11 02:25:40 +00001469 break;
1470 }
1471 case TK_RAISE: {
1472 if( !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +00001473 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00001474 "RAISE() may only be used within a trigger-program");
danielk19776f349032002-06-11 02:25:40 +00001475 return;
1476 }
drhad6d9462004-09-19 02:15:24 +00001477 if( pExpr->iColumn!=OE_Ignore ){
1478 assert( pExpr->iColumn==OE_Rollback ||
1479 pExpr->iColumn == OE_Abort ||
1480 pExpr->iColumn == OE_Fail );
1481 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn,
1482 pExpr->token.z, pExpr->token.n);
1483 sqlite3VdbeDequoteP3(v, -1);
danielk19776f349032002-06-11 02:25:40 +00001484 } else {
drhad6d9462004-09-19 02:15:24 +00001485 assert( pExpr->iColumn == OE_Ignore );
1486 sqlite3VdbeAddOp(v, OP_ContextPop, 0, 0);
1487 sqlite3VdbeAddOp(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
1488 VdbeComment((v, "# raise(IGNORE)"));
danielk19776f349032002-06-11 02:25:40 +00001489 }
drh17a7f8d2002-03-24 13:13:27 +00001490 }
1491 break;
drhcce7d172000-05-31 15:34:51 +00001492 }
drhcce7d172000-05-31 15:34:51 +00001493}
1494
1495/*
drh268380c2004-02-25 13:47:31 +00001496** Generate code that pushes the value of every element of the given
drhf9b596e2004-05-26 16:54:42 +00001497** expression list onto the stack.
drh268380c2004-02-25 13:47:31 +00001498**
1499** Return the number of elements pushed onto the stack.
1500*/
danielk19774adee202004-05-08 08:23:19 +00001501int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00001502 Parse *pParse, /* Parsing context */
drhf9b596e2004-05-26 16:54:42 +00001503 ExprList *pList /* The expression list to be coded */
drh268380c2004-02-25 13:47:31 +00001504){
1505 struct ExprList_item *pItem;
1506 int i, n;
1507 Vdbe *v;
1508 if( pList==0 ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001509 v = sqlite3GetVdbe(pParse);
drh268380c2004-02-25 13:47:31 +00001510 n = pList->nExpr;
1511 for(pItem=pList->a, i=0; i<n; i++, pItem++){
danielk19774adee202004-05-08 08:23:19 +00001512 sqlite3ExprCode(pParse, pItem->pExpr);
drh268380c2004-02-25 13:47:31 +00001513 }
drhf9b596e2004-05-26 16:54:42 +00001514 return n;
drh268380c2004-02-25 13:47:31 +00001515}
1516
1517/*
drhcce7d172000-05-31 15:34:51 +00001518** Generate code for a boolean expression such that a jump is made
1519** to the label "dest" if the expression is true but execution
1520** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00001521**
1522** If the expression evaluates to NULL (neither true nor false), then
1523** take the jump if the jumpIfNull flag is true.
drhcce7d172000-05-31 15:34:51 +00001524*/
danielk19774adee202004-05-08 08:23:19 +00001525void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001526 Vdbe *v = pParse->pVdbe;
1527 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001528 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001529 switch( pExpr->op ){
1530 case TK_LT: op = OP_Lt; break;
1531 case TK_LE: op = OP_Le; break;
1532 case TK_GT: op = OP_Gt; break;
1533 case TK_GE: op = OP_Ge; break;
1534 case TK_NE: op = OP_Ne; break;
1535 case TK_EQ: op = OP_Eq; break;
drhcce7d172000-05-31 15:34:51 +00001536 case TK_ISNULL: op = OP_IsNull; break;
1537 case TK_NOTNULL: op = OP_NotNull; break;
1538 default: break;
1539 }
1540 switch( pExpr->op ){
1541 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001542 int d2 = sqlite3VdbeMakeLabel(v);
1543 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
1544 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
1545 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001546 break;
1547 }
1548 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001549 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
1550 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001551 break;
1552 }
1553 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001554 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001555 break;
1556 }
1557 case TK_LT:
1558 case TK_LE:
1559 case TK_GT:
1560 case TK_GE:
1561 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00001562 case TK_EQ: {
danielk19774adee202004-05-08 08:23:19 +00001563 sqlite3ExprCode(pParse, pExpr->pLeft);
1564 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001565 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001566 break;
1567 }
1568 case TK_ISNULL:
1569 case TK_NOTNULL: {
danielk19774adee202004-05-08 08:23:19 +00001570 sqlite3ExprCode(pParse, pExpr->pLeft);
1571 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001572 break;
1573 }
drhfef52082000-06-06 01:50:43 +00001574 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00001575 /* The expression "x BETWEEN y AND z" is implemented as:
1576 **
1577 ** 1 IF (x < y) GOTO 3
1578 ** 2 IF (x <= z) GOTO <dest>
1579 ** 3 ...
1580 */
drhf5905aa2002-05-26 20:54:33 +00001581 int addr;
drhbe5c89a2004-07-26 00:31:09 +00001582 Expr *pLeft = pExpr->pLeft;
1583 Expr *pRight = pExpr->pList->a[0].pExpr;
1584 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001585 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001586 sqlite3ExprCode(pParse, pRight);
1587 addr = codeCompare(pParse, pLeft, pRight, OP_Lt, 0, !jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00001588
drhbe5c89a2004-07-26 00:31:09 +00001589 pRight = pExpr->pList->a[1].pExpr;
1590 sqlite3ExprCode(pParse, pRight);
1591 codeCompare(pParse, pLeft, pRight, OP_Le, dest, jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00001592
danielk19774adee202004-05-08 08:23:19 +00001593 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
1594 sqlite3VdbeChangeP2(v, addr, sqlite3VdbeCurrentAddr(v));
1595 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00001596 break;
1597 }
drhcce7d172000-05-31 15:34:51 +00001598 default: {
danielk19774adee202004-05-08 08:23:19 +00001599 sqlite3ExprCode(pParse, pExpr);
1600 sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001601 break;
1602 }
1603 }
1604}
1605
1606/*
drh66b89c82000-11-28 20:47:17 +00001607** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00001608** to the label "dest" if the expression is false but execution
1609** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00001610**
1611** If the expression evaluates to NULL (neither true nor false) then
1612** jump if jumpIfNull is true or fall through if jumpIfNull is false.
drhcce7d172000-05-31 15:34:51 +00001613*/
danielk19774adee202004-05-08 08:23:19 +00001614void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001615 Vdbe *v = pParse->pVdbe;
1616 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001617 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001618 switch( pExpr->op ){
1619 case TK_LT: op = OP_Ge; break;
1620 case TK_LE: op = OP_Gt; break;
1621 case TK_GT: op = OP_Le; break;
1622 case TK_GE: op = OP_Lt; break;
1623 case TK_NE: op = OP_Eq; break;
1624 case TK_EQ: op = OP_Ne; break;
drhcce7d172000-05-31 15:34:51 +00001625 case TK_ISNULL: op = OP_NotNull; break;
1626 case TK_NOTNULL: op = OP_IsNull; break;
1627 default: break;
1628 }
1629 switch( pExpr->op ){
1630 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001631 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
1632 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001633 break;
1634 }
1635 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001636 int d2 = sqlite3VdbeMakeLabel(v);
1637 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
1638 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
1639 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001640 break;
1641 }
1642 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001643 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001644 break;
1645 }
1646 case TK_LT:
1647 case TK_LE:
1648 case TK_GT:
1649 case TK_GE:
1650 case TK_NE:
1651 case TK_EQ: {
danielk19774adee202004-05-08 08:23:19 +00001652 sqlite3ExprCode(pParse, pExpr->pLeft);
1653 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001654 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001655 break;
1656 }
drhcce7d172000-05-31 15:34:51 +00001657 case TK_ISNULL:
1658 case TK_NOTNULL: {
danielk19774adee202004-05-08 08:23:19 +00001659 sqlite3ExprCode(pParse, pExpr->pLeft);
1660 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001661 break;
1662 }
drhfef52082000-06-06 01:50:43 +00001663 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00001664 /* The expression is "x BETWEEN y AND z". It is implemented as:
1665 **
1666 ** 1 IF (x >= y) GOTO 3
1667 ** 2 GOTO <dest>
1668 ** 3 IF (x > z) GOTO <dest>
1669 */
drhfef52082000-06-06 01:50:43 +00001670 int addr;
drhbe5c89a2004-07-26 00:31:09 +00001671 Expr *pLeft = pExpr->pLeft;
1672 Expr *pRight = pExpr->pList->a[0].pExpr;
1673 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001674 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001675 sqlite3ExprCode(pParse, pRight);
danielk19774adee202004-05-08 08:23:19 +00001676 addr = sqlite3VdbeCurrentAddr(v);
drhbe5c89a2004-07-26 00:31:09 +00001677 codeCompare(pParse, pLeft, pRight, OP_Ge, addr+3, !jumpIfNull);
1678
danielk19774adee202004-05-08 08:23:19 +00001679 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
1680 sqlite3VdbeAddOp(v, OP_Goto, 0, dest);
drhbe5c89a2004-07-26 00:31:09 +00001681 pRight = pExpr->pList->a[1].pExpr;
1682 sqlite3ExprCode(pParse, pRight);
1683 codeCompare(pParse, pLeft, pRight, OP_Gt, dest, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00001684 break;
1685 }
drhcce7d172000-05-31 15:34:51 +00001686 default: {
danielk19774adee202004-05-08 08:23:19 +00001687 sqlite3ExprCode(pParse, pExpr);
1688 sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001689 break;
1690 }
1691 }
1692}
drh22827922000-06-06 17:27:05 +00001693
1694/*
1695** Do a deep comparison of two expression trees. Return TRUE (non-zero)
1696** if they are identical and return FALSE if they differ in any way.
1697*/
danielk19774adee202004-05-08 08:23:19 +00001698int sqlite3ExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00001699 int i;
1700 if( pA==0 ){
1701 return pB==0;
1702 }else if( pB==0 ){
1703 return 0;
1704 }
1705 if( pA->op!=pB->op ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001706 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
1707 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00001708 if( pA->pList ){
1709 if( pB->pList==0 ) return 0;
1710 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
1711 for(i=0; i<pA->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00001712 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00001713 return 0;
1714 }
1715 }
1716 }else if( pB->pList ){
1717 return 0;
1718 }
1719 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00001720 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drh22827922000-06-06 17:27:05 +00001721 if( pA->token.z ){
1722 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00001723 if( pB->token.n!=pA->token.n ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001724 if( sqlite3StrNICmp(pA->token.z, pB->token.z, pB->token.n)!=0 ) return 0;
drh22827922000-06-06 17:27:05 +00001725 }
1726 return 1;
1727}
1728
1729/*
1730** Add a new element to the pParse->aAgg[] array and return its index.
1731*/
1732static int appendAggInfo(Parse *pParse){
1733 if( (pParse->nAgg & 0x7)==0 ){
1734 int amt = pParse->nAgg + 8;
drh6d4abfb2001-10-22 02:58:08 +00001735 AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0]));
1736 if( aAgg==0 ){
drh22827922000-06-06 17:27:05 +00001737 return -1;
1738 }
drh6d4abfb2001-10-22 02:58:08 +00001739 pParse->aAgg = aAgg;
drh22827922000-06-06 17:27:05 +00001740 }
1741 memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0]));
1742 return pParse->nAgg++;
1743}
1744
1745/*
1746** Analyze the given expression looking for aggregate functions and
1747** for variables that need to be added to the pParse->aAgg[] array.
1748** Make additional entries to the pParse->aAgg[] array as necessary.
1749**
1750** This routine should only be called after the expression has been
danielk19774adee202004-05-08 08:23:19 +00001751** analyzed by sqlite3ExprResolveIds() and sqlite3ExprCheck().
drh22827922000-06-06 17:27:05 +00001752**
1753** If errors are seen, leave an error message in zErrMsg and return
1754** the number of errors.
1755*/
danielk19774adee202004-05-08 08:23:19 +00001756int sqlite3ExprAnalyzeAggregates(Parse *pParse, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00001757 int i;
1758 AggExpr *aAgg;
1759 int nErr = 0;
1760
1761 if( pExpr==0 ) return 0;
1762 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00001763 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +00001764 aAgg = pParse->aAgg;
1765 for(i=0; i<pParse->nAgg; i++){
1766 if( aAgg[i].isAgg ) continue;
1767 if( aAgg[i].pExpr->iTable==pExpr->iTable
drh967e8b72000-06-21 13:59:10 +00001768 && aAgg[i].pExpr->iColumn==pExpr->iColumn ){
drh22827922000-06-06 17:27:05 +00001769 break;
1770 }
1771 }
1772 if( i>=pParse->nAgg ){
1773 i = appendAggInfo(pParse);
1774 if( i<0 ) return 1;
1775 pParse->aAgg[i].isAgg = 0;
1776 pParse->aAgg[i].pExpr = pExpr;
1777 }
drhaaf88722000-06-08 11:25:00 +00001778 pExpr->iAgg = i;
drh22827922000-06-06 17:27:05 +00001779 break;
1780 }
1781 case TK_AGG_FUNCTION: {
drh22827922000-06-06 17:27:05 +00001782 aAgg = pParse->aAgg;
1783 for(i=0; i<pParse->nAgg; i++){
1784 if( !aAgg[i].isAgg ) continue;
danielk19774adee202004-05-08 08:23:19 +00001785 if( sqlite3ExprCompare(aAgg[i].pExpr, pExpr) ){
drh22827922000-06-06 17:27:05 +00001786 break;
1787 }
1788 }
1789 if( i>=pParse->nAgg ){
danielk1977d8123362004-06-12 09:25:12 +00001790 u8 enc = pParse->db->enc;
drh22827922000-06-06 17:27:05 +00001791 i = appendAggInfo(pParse);
1792 if( i<0 ) return 1;
1793 pParse->aAgg[i].isAgg = 1;
1794 pParse->aAgg[i].pExpr = pExpr;
danielk19774adee202004-05-08 08:23:19 +00001795 pParse->aAgg[i].pFunc = sqlite3FindFunction(pParse->db,
drh6977fea2002-10-22 23:38:04 +00001796 pExpr->token.z, pExpr->token.n,
danielk1977d8123362004-06-12 09:25:12 +00001797 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
drh22827922000-06-06 17:27:05 +00001798 }
1799 pExpr->iAgg = i;
1800 break;
1801 }
1802 default: {
1803 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001804 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pLeft);
drh22827922000-06-06 17:27:05 +00001805 }
1806 if( nErr==0 && pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001807 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pRight);
drh22827922000-06-06 17:27:05 +00001808 }
1809 if( nErr==0 && pExpr->pList ){
1810 int n = pExpr->pList->nExpr;
1811 int i;
1812 for(i=0; nErr==0 && i<n; i++){
danielk19774adee202004-05-08 08:23:19 +00001813 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pList->a[i].pExpr);
drh22827922000-06-06 17:27:05 +00001814 }
1815 }
1816 break;
1817 }
1818 }
1819 return nErr;
1820}
drh8e0a2f92002-02-23 23:45:45 +00001821
1822/*
danielk1977d02eb1f2004-06-06 09:44:03 +00001823** Locate a user function given a name, a number of arguments and a flag
1824** indicating whether the function prefers UTF-16 over UTF-8. Return a
1825** pointer to the FuncDef structure that defines that function, or return
1826** NULL if the function does not exist.
drh8e0a2f92002-02-23 23:45:45 +00001827**
drh0bce8352002-02-28 00:41:10 +00001828** If the createFlag argument is true, then a new (blank) FuncDef
drh8e0a2f92002-02-23 23:45:45 +00001829** structure is created and liked into the "db" structure if a
1830** no matching function previously existed. When createFlag is true
1831** and the nArg parameter is -1, then only a function that accepts
1832** any number of arguments will be returned.
1833**
1834** If createFlag is false and nArg is -1, then the first valid
1835** function found is returned. A function is valid if either xFunc
1836** or xStep is non-zero.
danielk1977d02eb1f2004-06-06 09:44:03 +00001837**
1838** If createFlag is false, then a function with the required name and
1839** number of arguments may be returned even if the eTextRep flag does not
1840** match that requested.
drh8e0a2f92002-02-23 23:45:45 +00001841*/
danielk19774adee202004-05-08 08:23:19 +00001842FuncDef *sqlite3FindFunction(
drh9bb575f2004-09-06 17:24:11 +00001843 sqlite3 *db, /* An open database */
drh8e0a2f92002-02-23 23:45:45 +00001844 const char *zName, /* Name of the function. Not null-terminated */
1845 int nName, /* Number of characters in the name */
1846 int nArg, /* Number of arguments. -1 means any number */
danielk1977d8123362004-06-12 09:25:12 +00001847 u8 enc, /* Preferred text encoding */
drh8e0a2f92002-02-23 23:45:45 +00001848 int createFlag /* Create new entry if true and does not otherwise exist */
1849){
danielk1977d02eb1f2004-06-06 09:44:03 +00001850 FuncDef *p; /* Iterator variable */
1851 FuncDef *pFirst; /* First function with this name */
1852 FuncDef *pBest = 0; /* Best match found so far */
danielk1977d8123362004-06-12 09:25:12 +00001853 int bestmatch = 0;
danielk1977d02eb1f2004-06-06 09:44:03 +00001854
danielk1977d8123362004-06-12 09:25:12 +00001855
1856 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
danielk1977d02eb1f2004-06-06 09:44:03 +00001857 if( nArg<-1 ) nArg = -1;
1858
1859 pFirst = (FuncDef*)sqlite3HashFind(&db->aFunc, zName, nName);
1860 for(p=pFirst; p; p=p->pNext){
danielk1977d8123362004-06-12 09:25:12 +00001861 /* During the search for the best function definition, bestmatch is set
1862 ** as follows to indicate the quality of the match with the definition
1863 ** pointed to by pBest:
1864 **
1865 ** 0: pBest is NULL. No match has been found.
1866 ** 1: A variable arguments function that prefers UTF-8 when a UTF-16
1867 ** encoding is requested, or vice versa.
1868 ** 2: A variable arguments function that uses UTF-16BE when UTF-16LE is
1869 ** requested, or vice versa.
1870 ** 3: A variable arguments function using the same text encoding.
1871 ** 4: A function with the exact number of arguments requested that
1872 ** prefers UTF-8 when a UTF-16 encoding is requested, or vice versa.
1873 ** 5: A function with the exact number of arguments requested that
1874 ** prefers UTF-16LE when UTF-16BE is requested, or vice versa.
1875 ** 6: An exact match.
1876 **
1877 ** A larger value of 'matchqual' indicates a more desirable match.
1878 */
danielk1977e12c17b2004-06-23 12:35:14 +00001879 if( p->nArg==-1 || p->nArg==nArg || nArg==-1 ){
danielk1977d8123362004-06-12 09:25:12 +00001880 int match = 1; /* Quality of this match */
1881 if( p->nArg==nArg || nArg==-1 ){
1882 match = 4;
danielk1977d02eb1f2004-06-06 09:44:03 +00001883 }
danielk1977d8123362004-06-12 09:25:12 +00001884 if( enc==p->iPrefEnc ){
1885 match += 2;
danielk1977d02eb1f2004-06-06 09:44:03 +00001886 }
danielk1977d8123362004-06-12 09:25:12 +00001887 else if( (enc==SQLITE_UTF16LE && p->iPrefEnc==SQLITE_UTF16BE) ||
1888 (enc==SQLITE_UTF16BE && p->iPrefEnc==SQLITE_UTF16LE) ){
1889 match += 1;
1890 }
1891
1892 if( match>bestmatch ){
1893 pBest = p;
1894 bestmatch = match;
danielk1977d02eb1f2004-06-06 09:44:03 +00001895 }
1896 }
drh8e0a2f92002-02-23 23:45:45 +00001897 }
danielk1977d02eb1f2004-06-06 09:44:03 +00001898
danielk1977d8123362004-06-12 09:25:12 +00001899 /* If the createFlag parameter is true, and the seach did not reveal an
1900 ** exact match for the name, number of arguments and encoding, then add a
1901 ** new entry to the hash table and return it.
1902 */
1903 if( createFlag && bestmatch<6 &&
danielk1977d02eb1f2004-06-06 09:44:03 +00001904 (pBest = sqliteMalloc(sizeof(*pBest)+nName+1)) ){
1905 pBest->nArg = nArg;
1906 pBest->pNext = pFirst;
1907 pBest->zName = (char*)&pBest[1];
danielk1977d8123362004-06-12 09:25:12 +00001908 pBest->iPrefEnc = enc;
danielk1977d02eb1f2004-06-06 09:44:03 +00001909 memcpy(pBest->zName, zName, nName);
1910 pBest->zName[nName] = 0;
1911 sqlite3HashInsert(&db->aFunc, pBest->zName, nName, (void*)pBest);
drh8e0a2f92002-02-23 23:45:45 +00001912 }
danielk1977d02eb1f2004-06-06 09:44:03 +00001913
1914 if( pBest && (pBest->xStep || pBest->xFunc || createFlag) ){
1915 return pBest;
drh8e0a2f92002-02-23 23:45:45 +00001916 }
danielk1977d02eb1f2004-06-06 09:44:03 +00001917 return 0;
drh8e0a2f92002-02-23 23:45:45 +00001918}