blob: 8eec92c64673a2f6eed35f54bbf8f72ce331a9fc [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**
drh4e0cff62004-11-05 05:10:28 +000015** $Id: expr.c,v 1.168 2004/11/05 05:10:29 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 +000020/*
21** Return the 'affinity' of the expression pExpr if any.
22**
23** If pExpr is a column, a reference to a column via an 'AS' alias,
24** or a sub-select with a column as the return value, then the
25** affinity of that column is returned. Otherwise, 0x00 is returned,
26** indicating no affinity for the expression.
27**
28** i.e. the WHERE clause expresssions in the following statements all
29** have an affinity:
30**
31** CREATE TABLE t1(a);
32** SELECT * FROM t1 WHERE a;
33** SELECT a AS b FROM t1 WHERE b;
34** SELECT * FROM t1 WHERE (select a from t1);
35*/
danielk1977bf3b7212004-05-18 10:06:24 +000036char sqlite3ExprAffinity(Expr *pExpr){
danielk1977a37cdde2004-05-16 11:15:36 +000037 if( pExpr->op==TK_AS ){
danielk1977bf3b7212004-05-18 10:06:24 +000038 return sqlite3ExprAffinity(pExpr->pLeft);
danielk1977a37cdde2004-05-16 11:15:36 +000039 }
40 if( pExpr->op==TK_SELECT ){
danielk1977bf3b7212004-05-18 10:06:24 +000041 return sqlite3ExprAffinity(pExpr->pSelect->pEList->a[0].pExpr);
danielk1977a37cdde2004-05-16 11:15:36 +000042 }
43 return pExpr->affinity;
44}
45
drh53db1452004-05-20 13:54:53 +000046/*
danielk19770202b292004-06-09 09:55:16 +000047** Return the default collation sequence for the expression pExpr. If
48** there is no default collation type, return 0.
49*/
danielk19777cedc8d2004-06-10 10:50:08 +000050CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
51 CollSeq *pColl = 0;
danielk19770202b292004-06-09 09:55:16 +000052 if( pExpr ){
danielk19777cedc8d2004-06-10 10:50:08 +000053 pColl = pExpr->pColl;
54 if( pExpr->op==TK_AS && !pColl ){
55 return sqlite3ExprCollSeq(pParse, pExpr->pLeft);
danielk19770202b292004-06-09 09:55:16 +000056 }
57 }
danielk19777cedc8d2004-06-10 10:50:08 +000058 if( sqlite3CheckCollSeq(pParse, pColl) ){
59 pColl = 0;
60 }
61 return pColl;
danielk19770202b292004-06-09 09:55:16 +000062}
63
64/*
drh53db1452004-05-20 13:54:53 +000065** pExpr is the left operand of a comparison operator. aff2 is the
66** type affinity of the right operand. This routine returns the
67** type affinity that should be used for the comparison operator.
68*/
danielk1977e014a832004-05-17 10:48:57 +000069char sqlite3CompareAffinity(Expr *pExpr, char aff2){
danielk1977bf3b7212004-05-18 10:06:24 +000070 char aff1 = sqlite3ExprAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +000071 if( aff1 && aff2 ){
72 /* Both sides of the comparison are columns. If one has numeric or
73 ** integer affinity, use that. Otherwise use no affinity.
74 */
75 if( aff1==SQLITE_AFF_INTEGER || aff2==SQLITE_AFF_INTEGER ){
76 return SQLITE_AFF_INTEGER;
77 }else if( aff1==SQLITE_AFF_NUMERIC || aff2==SQLITE_AFF_NUMERIC ){
78 return SQLITE_AFF_NUMERIC;
79 }else{
80 return SQLITE_AFF_NONE;
81 }
82 }else if( !aff1 && !aff2 ){
drh5f6a87b2004-07-19 00:39:45 +000083 /* Neither side of the comparison is a column. Compare the
84 ** results directly.
danielk1977e014a832004-05-17 10:48:57 +000085 */
drh5f6a87b2004-07-19 00:39:45 +000086 /* return SQLITE_AFF_NUMERIC; // Ticket #805 */
87 return SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +000088 }else{
89 /* One side is a column, the other is not. Use the columns affinity. */
90 return (aff1 + aff2);
91 }
92}
93
drh53db1452004-05-20 13:54:53 +000094/*
95** pExpr is a comparison operator. Return the type affinity that should
96** be applied to both operands prior to doing the comparison.
97*/
danielk1977e014a832004-05-17 10:48:57 +000098static char comparisonAffinity(Expr *pExpr){
99 char aff;
100 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
101 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
102 pExpr->op==TK_NE );
103 assert( pExpr->pLeft );
danielk1977bf3b7212004-05-18 10:06:24 +0000104 aff = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +0000105 if( pExpr->pRight ){
106 aff = sqlite3CompareAffinity(pExpr->pRight, aff);
107 }
108 else if( pExpr->pSelect ){
109 aff = sqlite3CompareAffinity(pExpr->pSelect->pEList->a[0].pExpr, aff);
110 }
111 else if( !aff ){
112 aff = SQLITE_AFF_NUMERIC;
113 }
114 return aff;
115}
116
117/*
118** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
119** idx_affinity is the affinity of an indexed column. Return true
120** if the index with affinity idx_affinity may be used to implement
121** the comparison in pExpr.
122*/
123int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){
124 char aff = comparisonAffinity(pExpr);
125 return
126 (aff==SQLITE_AFF_NONE) ||
127 (aff==SQLITE_AFF_NUMERIC && idx_affinity==SQLITE_AFF_INTEGER) ||
128 (aff==SQLITE_AFF_INTEGER && idx_affinity==SQLITE_AFF_NUMERIC) ||
129 (aff==idx_affinity);
130}
131
danielk1977a37cdde2004-05-16 11:15:36 +0000132/*
133** Return the P1 value that should be used for a binary comparison
134** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
135** If jumpIfNull is true, then set the low byte of the returned
136** P1 value to tell the opcode to jump if either expression
137** evaluates to NULL.
138*/
danielk1977e014a832004-05-17 10:48:57 +0000139static int binaryCompareP1(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){
danielk1977bf3b7212004-05-18 10:06:24 +0000140 char aff = sqlite3ExprAffinity(pExpr2);
danielk1977e014a832004-05-17 10:48:57 +0000141 return (((int)sqlite3CompareAffinity(pExpr1, aff))<<8)+(jumpIfNull?1:0);
danielk1977a37cdde2004-05-16 11:15:36 +0000142}
143
drha2e00042002-01-22 03:13:42 +0000144/*
danielk19770202b292004-06-09 09:55:16 +0000145** Return a pointer to the collation sequence that should be used by
146** a binary comparison operator comparing pLeft and pRight.
147**
148** If the left hand expression has a collating sequence type, then it is
149** used. Otherwise the collation sequence for the right hand expression
150** is used, or the default (BINARY) if neither expression has a collating
151** type.
152*/
danielk19777cedc8d2004-06-10 10:50:08 +0000153static CollSeq* binaryCompareCollSeq(Parse *pParse, Expr *pLeft, Expr *pRight){
154 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pLeft);
danielk19770202b292004-06-09 09:55:16 +0000155 if( !pColl ){
danielk19777cedc8d2004-06-10 10:50:08 +0000156 pColl = sqlite3ExprCollSeq(pParse, pRight);
danielk19770202b292004-06-09 09:55:16 +0000157 }
158 return pColl;
159}
160
161/*
drhbe5c89a2004-07-26 00:31:09 +0000162** Generate code for a comparison operator.
163*/
164static int codeCompare(
165 Parse *pParse, /* The parsing (and code generating) context */
166 Expr *pLeft, /* The left operand */
167 Expr *pRight, /* The right operand */
168 int opcode, /* The comparison opcode */
169 int dest, /* Jump here if true. */
170 int jumpIfNull /* If true, jump if either operand is NULL */
171){
172 int p1 = binaryCompareP1(pLeft, pRight, jumpIfNull);
173 CollSeq *p3 = binaryCompareCollSeq(pParse, pLeft, pRight);
drh94a11212004-09-25 13:12:14 +0000174 return sqlite3VdbeOp3(pParse->pVdbe, opcode, p1, dest, (void*)p3, P3_COLLSEQ);
drhbe5c89a2004-07-26 00:31:09 +0000175}
176
177/*
drha76b5df2002-02-23 02:32:10 +0000178** Construct a new expression node and return a pointer to it. Memory
179** for this node is obtained from sqliteMalloc(). The calling function
180** is responsible for making sure the node eventually gets freed.
181*/
danielk19774adee202004-05-08 08:23:19 +0000182Expr *sqlite3Expr(int op, Expr *pLeft, Expr *pRight, Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000183 Expr *pNew;
184 pNew = sqliteMalloc( sizeof(Expr) );
185 if( pNew==0 ){
drh4efc4752004-01-16 15:55:37 +0000186 /* When malloc fails, we leak memory from pLeft and pRight */
drha76b5df2002-02-23 02:32:10 +0000187 return 0;
188 }
189 pNew->op = op;
190 pNew->pLeft = pLeft;
191 pNew->pRight = pRight;
192 if( pToken ){
drh4b59ab52002-08-24 18:24:51 +0000193 assert( pToken->dyn==0 );
drh145716b2004-09-24 12:24:06 +0000194 pNew->span = pNew->token = *pToken;
195 }else if( pLeft && pRight ){
196 sqlite3ExprSpan(pNew, &pLeft->span, &pRight->span);
drha76b5df2002-02-23 02:32:10 +0000197 }
drha76b5df2002-02-23 02:32:10 +0000198 return pNew;
199}
200
201/*
drh4e0cff62004-11-05 05:10:28 +0000202** When doing a nested parse, you can include terms in an expression
203** that look like this: #0 #1 #2 ... These terms refer to elements
204** on the stack. "#0" (or just "#") means the top of the stack.
205** "#1" means the next down on the stack. And so forth.
206**
207** This routine is called by the parser to deal with on of those terms.
208** It immediately generates code to store the value in a memory location.
209** The returns an expression that will code to extract the value from
210** that memory location as needed.
211*/
212Expr *sqlite3RegisterExpr(Parse *pParse, Token *pToken){
213 Vdbe *v = pParse->pVdbe;
214 Expr *p;
215 int depth;
216 if( v==0 ) return 0;
217 if( pParse->nested==0 ){
218 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", pToken);
219 return 0;
220 }
221 p = sqlite3Expr(TK_REGISTER, 0, 0, pToken);
222 depth = atoi(&pToken->z[1]);
223 p->iTable = pParse->nMem++;
224 sqlite3VdbeAddOp(v, OP_Dup, depth, 0);
225 sqlite3VdbeAddOp(v, OP_MemStore, p->iTable, 1);
226 return p;
227}
228
229/*
drh91bb0ee2004-09-01 03:06:34 +0000230** Join two expressions using an AND operator. If either expression is
231** NULL, then just return the other expression.
232*/
233Expr *sqlite3ExprAnd(Expr *pLeft, Expr *pRight){
234 if( pLeft==0 ){
235 return pRight;
236 }else if( pRight==0 ){
237 return pLeft;
238 }else{
239 return sqlite3Expr(TK_AND, pLeft, pRight, 0);
240 }
241}
242
243/*
drh6977fea2002-10-22 23:38:04 +0000244** Set the Expr.span field of the given expression to span all
drha76b5df2002-02-23 02:32:10 +0000245** text between the two given tokens.
246*/
danielk19774adee202004-05-08 08:23:19 +0000247void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
drh4efc4752004-01-16 15:55:37 +0000248 assert( pRight!=0 );
249 assert( pLeft!=0 );
drh71c697e2004-08-08 23:39:19 +0000250 if( !sqlite3_malloc_failed && pRight->z && pLeft->z ){
drhad6d9462004-09-19 02:15:24 +0000251 assert( pLeft->dyn==0 || pLeft->z[pLeft->n]==0 );
drh145716b2004-09-24 12:24:06 +0000252 if( pLeft->dyn==0 && pRight->dyn==0 ){
drh6977fea2002-10-22 23:38:04 +0000253 pExpr->span.z = pLeft->z;
254 pExpr->span.n = pRight->n + Addr(pRight->z) - Addr(pLeft->z);
drh4b59ab52002-08-24 18:24:51 +0000255 }else{
drh6977fea2002-10-22 23:38:04 +0000256 pExpr->span.z = 0;
drh4b59ab52002-08-24 18:24:51 +0000257 }
drha76b5df2002-02-23 02:32:10 +0000258 }
259}
260
261/*
262** Construct a new expression node for a function with multiple
263** arguments.
264*/
danielk19774adee202004-05-08 08:23:19 +0000265Expr *sqlite3ExprFunction(ExprList *pList, Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000266 Expr *pNew;
267 pNew = sqliteMalloc( sizeof(Expr) );
268 if( pNew==0 ){
danielk19774adee202004-05-08 08:23:19 +0000269 /* sqlite3ExprListDelete(pList); // Leak pList when malloc fails */
drha76b5df2002-02-23 02:32:10 +0000270 return 0;
271 }
272 pNew->op = TK_FUNCTION;
273 pNew->pList = pList;
274 if( pToken ){
drh4b59ab52002-08-24 18:24:51 +0000275 assert( pToken->dyn==0 );
drha76b5df2002-02-23 02:32:10 +0000276 pNew->token = *pToken;
277 }else{
278 pNew->token.z = 0;
drha76b5df2002-02-23 02:32:10 +0000279 }
drh6977fea2002-10-22 23:38:04 +0000280 pNew->span = pNew->token;
drha76b5df2002-02-23 02:32:10 +0000281 return pNew;
282}
283
284/*
drhfa6bc002004-09-07 16:19:52 +0000285** Assign a variable number to an expression that encodes a wildcard
286** in the original SQL statement.
287**
288** Wildcards consisting of a single "?" are assigned the next sequential
289** variable number.
290**
291** Wildcards of the form "?nnn" are assigned the number "nnn". We make
292** sure "nnn" is not too be to avoid a denial of service attack when
293** the SQL statement comes from an external source.
294**
295** Wildcards of the form ":aaa" or "$aaa" are assigned the same number
296** as the previous instance of the same wildcard. Or if this is the first
297** instance of the wildcard, the next sequenial variable number is
298** assigned.
299*/
300void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){
301 Token *pToken;
302 if( pExpr==0 ) return;
303 pToken = &pExpr->token;
304 assert( pToken->n>=1 );
305 assert( pToken->z!=0 );
306 assert( pToken->z[0]!=0 );
307 if( pToken->n==1 ){
308 /* Wildcard of the form "?". Assign the next variable number */
309 pExpr->iTable = ++pParse->nVar;
310 }else if( pToken->z[0]=='?' ){
311 /* Wildcard of the form "?nnn". Convert "nnn" to an integer and
312 ** use it as the variable number */
313 int i;
314 pExpr->iTable = i = atoi(&pToken->z[1]);
315 if( i<1 || i>SQLITE_MAX_VARIABLE_NUMBER ){
316 sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
317 SQLITE_MAX_VARIABLE_NUMBER);
318 }
319 if( i>pParse->nVar ){
320 pParse->nVar = i;
321 }
322 }else{
323 /* Wildcards of the form ":aaa" or "$aaa". Reuse the same variable
324 ** number as the prior appearance of the same name, or if the name
325 ** has never appeared before, reuse the same variable number
326 */
327 int i, n;
328 n = pToken->n;
329 for(i=0; i<pParse->nVarExpr; i++){
330 Expr *pE;
331 if( (pE = pParse->apVarExpr[i])!=0
332 && pE->token.n==n
333 && memcmp(pE->token.z, pToken->z, n)==0 ){
334 pExpr->iTable = pE->iTable;
335 break;
336 }
337 }
338 if( i>=pParse->nVarExpr ){
339 pExpr->iTable = ++pParse->nVar;
340 if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){
341 pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10;
342 pParse->apVarExpr = sqliteRealloc(pParse->apVarExpr,
343 pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0]) );
344 }
345 if( !sqlite3_malloc_failed ){
346 assert( pParse->apVarExpr!=0 );
347 pParse->apVarExpr[pParse->nVarExpr++] = pExpr;
348 }
349 }
350 }
351}
352
353/*
drha2e00042002-01-22 03:13:42 +0000354** Recursively delete an expression tree.
355*/
danielk19774adee202004-05-08 08:23:19 +0000356void sqlite3ExprDelete(Expr *p){
drha2e00042002-01-22 03:13:42 +0000357 if( p==0 ) return;
drh4efc4752004-01-16 15:55:37 +0000358 if( p->span.dyn ) sqliteFree((char*)p->span.z);
359 if( p->token.dyn ) sqliteFree((char*)p->token.z);
danielk19774adee202004-05-08 08:23:19 +0000360 sqlite3ExprDelete(p->pLeft);
361 sqlite3ExprDelete(p->pRight);
362 sqlite3ExprListDelete(p->pList);
363 sqlite3SelectDelete(p->pSelect);
drha2e00042002-01-22 03:13:42 +0000364 sqliteFree(p);
365}
366
drha76b5df2002-02-23 02:32:10 +0000367
368/*
drhff78bd22002-02-27 01:47:11 +0000369** The following group of routines make deep copies of expressions,
370** expression lists, ID lists, and select statements. The copies can
371** be deleted (by being passed to their respective ...Delete() routines)
372** without effecting the originals.
373**
danielk19774adee202004-05-08 08:23:19 +0000374** The expression list, ID, and source lists return by sqlite3ExprListDup(),
375** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
drhad3cab52002-05-24 02:04:32 +0000376** by subsequent calls to sqlite*ListAppend() routines.
drhff78bd22002-02-27 01:47:11 +0000377**
drhad3cab52002-05-24 02:04:32 +0000378** Any tables that the SrcList might point to are not duplicated.
drhff78bd22002-02-27 01:47:11 +0000379*/
danielk19774adee202004-05-08 08:23:19 +0000380Expr *sqlite3ExprDup(Expr *p){
drhff78bd22002-02-27 01:47:11 +0000381 Expr *pNew;
382 if( p==0 ) return 0;
drhfcb78a42003-01-18 20:11:05 +0000383 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000384 if( pNew==0 ) return 0;
drh3b167c72002-06-28 12:18:47 +0000385 memcpy(pNew, p, sizeof(*pNew));
drh6977fea2002-10-22 23:38:04 +0000386 if( p->token.z!=0 ){
drh4b59ab52002-08-24 18:24:51 +0000387 pNew->token.z = sqliteStrDup(p->token.z);
388 pNew->token.dyn = 1;
389 }else{
drh4efc4752004-01-16 15:55:37 +0000390 assert( pNew->token.z==0 );
drh4b59ab52002-08-24 18:24:51 +0000391 }
drh6977fea2002-10-22 23:38:04 +0000392 pNew->span.z = 0;
danielk19774adee202004-05-08 08:23:19 +0000393 pNew->pLeft = sqlite3ExprDup(p->pLeft);
394 pNew->pRight = sqlite3ExprDup(p->pRight);
395 pNew->pList = sqlite3ExprListDup(p->pList);
396 pNew->pSelect = sqlite3SelectDup(p->pSelect);
drhff78bd22002-02-27 01:47:11 +0000397 return pNew;
398}
danielk19774adee202004-05-08 08:23:19 +0000399void sqlite3TokenCopy(Token *pTo, Token *pFrom){
drh4b59ab52002-08-24 18:24:51 +0000400 if( pTo->dyn ) sqliteFree((char*)pTo->z);
drh4b59ab52002-08-24 18:24:51 +0000401 if( pFrom->z ){
402 pTo->n = pFrom->n;
403 pTo->z = sqliteStrNDup(pFrom->z, pFrom->n);
404 pTo->dyn = 1;
405 }else{
drh4b59ab52002-08-24 18:24:51 +0000406 pTo->z = 0;
drh4b59ab52002-08-24 18:24:51 +0000407 }
408}
danielk19774adee202004-05-08 08:23:19 +0000409ExprList *sqlite3ExprListDup(ExprList *p){
drhff78bd22002-02-27 01:47:11 +0000410 ExprList *pNew;
drh145716b2004-09-24 12:24:06 +0000411 struct ExprList_item *pItem, *pOldItem;
drhff78bd22002-02-27 01:47:11 +0000412 int i;
413 if( p==0 ) return 0;
414 pNew = sqliteMalloc( sizeof(*pNew) );
415 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000416 pNew->nExpr = pNew->nAlloc = p->nExpr;
drh3e7bc9c2004-02-21 19:17:17 +0000417 pNew->a = pItem = sqliteMalloc( p->nExpr*sizeof(p->a[0]) );
danielk1977e0048402004-06-15 16:51:01 +0000418 if( pItem==0 ){
419 sqliteFree(pNew);
420 return 0;
421 }
drh145716b2004-09-24 12:24:06 +0000422 pOldItem = p->a;
423 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
drh4b59ab52002-08-24 18:24:51 +0000424 Expr *pNewExpr, *pOldExpr;
drh145716b2004-09-24 12:24:06 +0000425 pItem->pExpr = pNewExpr = sqlite3ExprDup(pOldExpr = pOldItem->pExpr);
drh6977fea2002-10-22 23:38:04 +0000426 if( pOldExpr->span.z!=0 && pNewExpr ){
427 /* Always make a copy of the span for top-level expressions in the
drh4b59ab52002-08-24 18:24:51 +0000428 ** expression list. The logic in SELECT processing that determines
429 ** the names of columns in the result set needs this information */
danielk19774adee202004-05-08 08:23:19 +0000430 sqlite3TokenCopy(&pNewExpr->span, &pOldExpr->span);
drh4b59ab52002-08-24 18:24:51 +0000431 }
drh1f3e9052002-10-31 00:09:39 +0000432 assert( pNewExpr==0 || pNewExpr->span.z!=0
danielk197724b03fd2004-05-10 10:34:34 +0000433 || pOldExpr->span.z==0 || sqlite3_malloc_failed );
drh145716b2004-09-24 12:24:06 +0000434 pItem->zName = sqliteStrDup(pOldItem->zName);
435 pItem->sortOrder = pOldItem->sortOrder;
436 pItem->isAgg = pOldItem->isAgg;
drh3e7bc9c2004-02-21 19:17:17 +0000437 pItem->done = 0;
drhff78bd22002-02-27 01:47:11 +0000438 }
439 return pNew;
440}
danielk19774adee202004-05-08 08:23:19 +0000441SrcList *sqlite3SrcListDup(SrcList *p){
drhad3cab52002-05-24 02:04:32 +0000442 SrcList *pNew;
443 int i;
drh113088e2003-03-20 01:16:58 +0000444 int nByte;
drhad3cab52002-05-24 02:04:32 +0000445 if( p==0 ) return 0;
drh113088e2003-03-20 01:16:58 +0000446 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
drh4efc4752004-01-16 15:55:37 +0000447 pNew = sqliteMallocRaw( nByte );
drhad3cab52002-05-24 02:04:32 +0000448 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000449 pNew->nSrc = pNew->nAlloc = p->nSrc;
drhad3cab52002-05-24 02:04:32 +0000450 for(i=0; i<p->nSrc; i++){
drh4efc4752004-01-16 15:55:37 +0000451 struct SrcList_item *pNewItem = &pNew->a[i];
452 struct SrcList_item *pOldItem = &p->a[i];
453 pNewItem->zDatabase = sqliteStrDup(pOldItem->zDatabase);
454 pNewItem->zName = sqliteStrDup(pOldItem->zName);
455 pNewItem->zAlias = sqliteStrDup(pOldItem->zAlias);
456 pNewItem->jointype = pOldItem->jointype;
457 pNewItem->iCursor = pOldItem->iCursor;
458 pNewItem->pTab = 0;
danielk19774adee202004-05-08 08:23:19 +0000459 pNewItem->pSelect = sqlite3SelectDup(pOldItem->pSelect);
460 pNewItem->pOn = sqlite3ExprDup(pOldItem->pOn);
461 pNewItem->pUsing = sqlite3IdListDup(pOldItem->pUsing);
drhad3cab52002-05-24 02:04:32 +0000462 }
463 return pNew;
464}
danielk19774adee202004-05-08 08:23:19 +0000465IdList *sqlite3IdListDup(IdList *p){
drhff78bd22002-02-27 01:47:11 +0000466 IdList *pNew;
467 int i;
468 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000469 pNew = sqliteMallocRaw( sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +0000470 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000471 pNew->nId = pNew->nAlloc = p->nId;
drh4efc4752004-01-16 15:55:37 +0000472 pNew->a = sqliteMallocRaw( p->nId*sizeof(p->a[0]) );
drhe4697f52002-05-23 02:09:03 +0000473 if( pNew->a==0 ) return 0;
drhff78bd22002-02-27 01:47:11 +0000474 for(i=0; i<p->nId; i++){
drh4efc4752004-01-16 15:55:37 +0000475 struct IdList_item *pNewItem = &pNew->a[i];
476 struct IdList_item *pOldItem = &p->a[i];
477 pNewItem->zName = sqliteStrDup(pOldItem->zName);
478 pNewItem->idx = pOldItem->idx;
drhff78bd22002-02-27 01:47:11 +0000479 }
480 return pNew;
481}
danielk19774adee202004-05-08 08:23:19 +0000482Select *sqlite3SelectDup(Select *p){
drhff78bd22002-02-27 01:47:11 +0000483 Select *pNew;
484 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000485 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000486 if( pNew==0 ) return 0;
487 pNew->isDistinct = p->isDistinct;
danielk19774adee202004-05-08 08:23:19 +0000488 pNew->pEList = sqlite3ExprListDup(p->pEList);
489 pNew->pSrc = sqlite3SrcListDup(p->pSrc);
490 pNew->pWhere = sqlite3ExprDup(p->pWhere);
491 pNew->pGroupBy = sqlite3ExprListDup(p->pGroupBy);
492 pNew->pHaving = sqlite3ExprDup(p->pHaving);
493 pNew->pOrderBy = sqlite3ExprListDup(p->pOrderBy);
drhff78bd22002-02-27 01:47:11 +0000494 pNew->op = p->op;
danielk19774adee202004-05-08 08:23:19 +0000495 pNew->pPrior = sqlite3SelectDup(p->pPrior);
drhff78bd22002-02-27 01:47:11 +0000496 pNew->nLimit = p->nLimit;
497 pNew->nOffset = p->nOffset;
498 pNew->zSelect = 0;
drh7b58dae2003-07-20 01:16:46 +0000499 pNew->iLimit = -1;
500 pNew->iOffset = -1;
danielk1977dc1bdc42004-06-11 10:51:27 +0000501 pNew->ppOpenTemp = 0;
drhff78bd22002-02-27 01:47:11 +0000502 return pNew;
503}
504
505
506/*
drha76b5df2002-02-23 02:32:10 +0000507** Add a new element to the end of an expression list. If pList is
508** initially NULL, then create a new expression list.
509*/
danielk19774adee202004-05-08 08:23:19 +0000510ExprList *sqlite3ExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){
drha76b5df2002-02-23 02:32:10 +0000511 if( pList==0 ){
512 pList = sqliteMalloc( sizeof(ExprList) );
513 if( pList==0 ){
danielk19774adee202004-05-08 08:23:19 +0000514 /* sqlite3ExprDelete(pExpr); // Leak memory if malloc fails */
drha76b5df2002-02-23 02:32:10 +0000515 return 0;
516 }
drh4efc4752004-01-16 15:55:37 +0000517 assert( pList->nAlloc==0 );
drha76b5df2002-02-23 02:32:10 +0000518 }
drh4305d102003-07-30 12:34:12 +0000519 if( pList->nAlloc<=pList->nExpr ){
drh4305d102003-07-30 12:34:12 +0000520 pList->nAlloc = pList->nAlloc*2 + 4;
drh4efc4752004-01-16 15:55:37 +0000521 pList->a = sqliteRealloc(pList->a, pList->nAlloc*sizeof(pList->a[0]));
522 if( pList->a==0 ){
danielk19774adee202004-05-08 08:23:19 +0000523 /* sqlite3ExprDelete(pExpr); // Leak memory if malloc fails */
drh4efc4752004-01-16 15:55:37 +0000524 pList->nExpr = pList->nAlloc = 0;
drha76b5df2002-02-23 02:32:10 +0000525 return pList;
526 }
drha76b5df2002-02-23 02:32:10 +0000527 }
drh4efc4752004-01-16 15:55:37 +0000528 assert( pList->a!=0 );
529 if( pExpr || pName ){
530 struct ExprList_item *pItem = &pList->a[pList->nExpr++];
531 memset(pItem, 0, sizeof(*pItem));
532 pItem->pExpr = pExpr;
drha99db3b2004-06-19 14:49:12 +0000533 pItem->zName = sqlite3NameFromToken(pName);
drha76b5df2002-02-23 02:32:10 +0000534 }
535 return pList;
536}
537
538/*
539** Delete an entire expression list.
540*/
danielk19774adee202004-05-08 08:23:19 +0000541void sqlite3ExprListDelete(ExprList *pList){
drha76b5df2002-02-23 02:32:10 +0000542 int i;
drhbe5c89a2004-07-26 00:31:09 +0000543 struct ExprList_item *pItem;
drha76b5df2002-02-23 02:32:10 +0000544 if( pList==0 ) return;
drh1bdd9b52004-04-23 17:04:44 +0000545 assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
546 assert( pList->nExpr<=pList->nAlloc );
drhbe5c89a2004-07-26 00:31:09 +0000547 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
548 sqlite3ExprDelete(pItem->pExpr);
549 sqliteFree(pItem->zName);
drha76b5df2002-02-23 02:32:10 +0000550 }
551 sqliteFree(pList->a);
552 sqliteFree(pList);
553}
554
555/*
drhfef52082000-06-06 01:50:43 +0000556** Walk an expression tree. Return 1 if the expression is constant
557** and 0 if it involves variables.
drh23989372002-05-21 13:43:04 +0000558**
559** For the purposes of this function, a double-quoted string (ex: "abc")
560** is considered a variable but a single-quoted string (ex: 'abc') is
561** a constant.
drhfef52082000-06-06 01:50:43 +0000562*/
danielk19774adee202004-05-08 08:23:19 +0000563int sqlite3ExprIsConstant(Expr *p){
drhfef52082000-06-06 01:50:43 +0000564 switch( p->op ){
565 case TK_ID:
drh967e8b72000-06-21 13:59:10 +0000566 case TK_COLUMN:
drhfef52082000-06-06 01:50:43 +0000567 case TK_DOT:
drh7bdc0c12003-04-19 17:27:24 +0000568 case TK_FUNCTION:
drhfef52082000-06-06 01:50:43 +0000569 return 0;
drh7bdc0c12003-04-19 17:27:24 +0000570 case TK_NULL:
drh23989372002-05-21 13:43:04 +0000571 case TK_STRING:
danielk1977c572ef72004-05-27 09:28:41 +0000572 case TK_BLOB:
drh92086432002-01-22 14:11:29 +0000573 case TK_INTEGER:
574 case TK_FLOAT:
drh50457892003-09-06 01:10:47 +0000575 case TK_VARIABLE:
drh92086432002-01-22 14:11:29 +0000576 return 1;
drhfef52082000-06-06 01:50:43 +0000577 default: {
danielk19774adee202004-05-08 08:23:19 +0000578 if( p->pLeft && !sqlite3ExprIsConstant(p->pLeft) ) return 0;
579 if( p->pRight && !sqlite3ExprIsConstant(p->pRight) ) return 0;
drhfef52082000-06-06 01:50:43 +0000580 if( p->pList ){
581 int i;
582 for(i=0; i<p->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +0000583 if( !sqlite3ExprIsConstant(p->pList->a[i].pExpr) ) return 0;
drhfef52082000-06-06 01:50:43 +0000584 }
585 }
drh92086432002-01-22 14:11:29 +0000586 return p->pLeft!=0 || p->pRight!=0 || (p->pList && p->pList->nExpr>0);
drhfef52082000-06-06 01:50:43 +0000587 }
588 }
drh92086432002-01-22 14:11:29 +0000589 return 0;
drhfef52082000-06-06 01:50:43 +0000590}
591
592/*
drh202b2df2004-01-06 01:13:46 +0000593** If the given expression codes a constant integer that is small enough
594** to fit in a 32-bit integer, return 1 and put the value of the integer
595** in *pValue. If the expression is not an integer or if it is too big
596** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
drhe4de1fe2002-06-02 16:09:01 +0000597*/
danielk19774adee202004-05-08 08:23:19 +0000598int sqlite3ExprIsInteger(Expr *p, int *pValue){
drhe4de1fe2002-06-02 16:09:01 +0000599 switch( p->op ){
600 case TK_INTEGER: {
drhfec19aa2004-05-19 20:41:03 +0000601 if( sqlite3GetInt32(p->token.z, pValue) ){
drh202b2df2004-01-06 01:13:46 +0000602 return 1;
603 }
604 break;
drhe4de1fe2002-06-02 16:09:01 +0000605 }
606 case TK_STRING: {
drh4c755c02004-08-08 20:22:17 +0000607 const u8 *z = (u8*)p->token.z;
drhe4de1fe2002-06-02 16:09:01 +0000608 int n = p->token.n;
drhbd790ee2002-06-02 18:22:06 +0000609 if( n>0 && z[0]=='-' ){ z++; n--; }
drhe4de1fe2002-06-02 16:09:01 +0000610 while( n>0 && *z && isdigit(*z) ){ z++; n--; }
drhfec19aa2004-05-19 20:41:03 +0000611 if( n==0 && sqlite3GetInt32(p->token.z, pValue) ){
drhe4de1fe2002-06-02 16:09:01 +0000612 return 1;
613 }
614 break;
615 }
drh4b59ab52002-08-24 18:24:51 +0000616 case TK_UPLUS: {
danielk19774adee202004-05-08 08:23:19 +0000617 return sqlite3ExprIsInteger(p->pLeft, pValue);
drh4b59ab52002-08-24 18:24:51 +0000618 }
drhe4de1fe2002-06-02 16:09:01 +0000619 case TK_UMINUS: {
620 int v;
danielk19774adee202004-05-08 08:23:19 +0000621 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
drhe4de1fe2002-06-02 16:09:01 +0000622 *pValue = -v;
623 return 1;
624 }
625 break;
626 }
627 default: break;
628 }
629 return 0;
630}
631
632/*
drhc4a3c772001-04-04 11:48:57 +0000633** Return TRUE if the given string is a row-id column name.
634*/
danielk19774adee202004-05-08 08:23:19 +0000635int sqlite3IsRowid(const char *z){
636 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
637 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
638 if( sqlite3StrICmp(z, "OID")==0 ) return 1;
drhc4a3c772001-04-04 11:48:57 +0000639 return 0;
640}
641
642/*
drh8141f612004-01-25 22:44:58 +0000643** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
644** that name in the set of source tables in pSrcList and make the pExpr
645** expression node refer back to that source column. The following changes
646** are made to pExpr:
647**
648** pExpr->iDb Set the index in db->aDb[] of the database holding
649** the table.
650** pExpr->iTable Set to the cursor number for the table obtained
651** from pSrcList.
652** pExpr->iColumn Set to the column number within the table.
drh8141f612004-01-25 22:44:58 +0000653** pExpr->op Set to TK_COLUMN.
654** pExpr->pLeft Any expression this points to is deleted
655** pExpr->pRight Any expression this points to is deleted.
656**
657** The pDbToken is the name of the database (the "X"). This value may be
658** NULL meaning that name is of the form Y.Z or Z. Any available database
659** can be used. The pTableToken is the name of the table (the "Y"). This
660** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it
661** means that the form of the name is Z and that columns from any table
662** can be used.
663**
664** If the name cannot be resolved unambiguously, leave an error message
665** in pParse and return non-zero. Return zero on success.
666*/
667static int lookupName(
668 Parse *pParse, /* The parsing context */
669 Token *pDbToken, /* Name of the database containing table, or NULL */
670 Token *pTableToken, /* Name of table containing column, or NULL */
671 Token *pColumnToken, /* Name of the column. */
672 SrcList *pSrcList, /* List of tables used to resolve column names */
673 ExprList *pEList, /* List of expressions used to resolve "AS" */
674 Expr *pExpr /* Make this EXPR node point to the selected column */
675){
676 char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */
677 char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */
678 char *zCol = 0; /* Name of the column. The "Z" */
679 int i, j; /* Loop counters */
680 int cnt = 0; /* Number of matching column names */
681 int cntTab = 0; /* Number of matching table names */
drh9bb575f2004-09-06 17:24:11 +0000682 sqlite3 *db = pParse->db; /* The database */
drh8141f612004-01-25 22:44:58 +0000683
684 assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
drha99db3b2004-06-19 14:49:12 +0000685 zDb = sqlite3NameFromToken(pDbToken);
686 zTab = sqlite3NameFromToken(pTableToken);
687 zCol = sqlite3NameFromToken(pColumnToken);
danielk197724b03fd2004-05-10 10:34:34 +0000688 if( sqlite3_malloc_failed ){
drh8141f612004-01-25 22:44:58 +0000689 return 1; /* Leak memory (zDb and zTab) if malloc fails */
690 }
691 assert( zTab==0 || pEList==0 );
692
693 pExpr->iTable = -1;
694 for(i=0; i<pSrcList->nSrc; i++){
695 struct SrcList_item *pItem = &pSrcList->a[i];
696 Table *pTab = pItem->pTab;
697 Column *pCol;
698
699 if( pTab==0 ) continue;
700 assert( pTab->nCol>0 );
701 if( zTab ){
702 if( pItem->zAlias ){
703 char *zTabName = pItem->zAlias;
danielk19774adee202004-05-08 08:23:19 +0000704 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
drh8141f612004-01-25 22:44:58 +0000705 }else{
706 char *zTabName = pTab->zName;
danielk19774adee202004-05-08 08:23:19 +0000707 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
708 if( zDb!=0 && sqlite3StrICmp(db->aDb[pTab->iDb].zName, zDb)!=0 ){
drh8141f612004-01-25 22:44:58 +0000709 continue;
710 }
711 }
712 }
713 if( 0==(cntTab++) ){
714 pExpr->iTable = pItem->iCursor;
715 pExpr->iDb = pTab->iDb;
716 }
717 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
danielk19774adee202004-05-08 08:23:19 +0000718 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
drh8141f612004-01-25 22:44:58 +0000719 cnt++;
720 pExpr->iTable = pItem->iCursor;
721 pExpr->iDb = pTab->iDb;
722 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
723 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
danielk1977a37cdde2004-05-16 11:15:36 +0000724 pExpr->affinity = pTab->aCol[j].affinity;
danielk19770202b292004-06-09 09:55:16 +0000725 pExpr->pColl = pTab->aCol[j].pColl;
drh8141f612004-01-25 22:44:58 +0000726 break;
727 }
728 }
729 }
730
drhb7f91642004-10-31 02:22:47 +0000731#ifndef SQLITE_OMIT_TRIGGER
drh8141f612004-01-25 22:44:58 +0000732 /* If we have not already resolved the name, then maybe
733 ** it is a new.* or old.* trigger argument reference
734 */
735 if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
736 TriggerStack *pTriggerStack = pParse->trigStack;
737 Table *pTab = 0;
danielk19774adee202004-05-08 08:23:19 +0000738 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){
drh8141f612004-01-25 22:44:58 +0000739 pExpr->iTable = pTriggerStack->newIdx;
740 assert( pTriggerStack->pTab );
741 pTab = pTriggerStack->pTab;
danielk19774adee202004-05-08 08:23:19 +0000742 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab) == 0 ){
drh8141f612004-01-25 22:44:58 +0000743 pExpr->iTable = pTriggerStack->oldIdx;
744 assert( pTriggerStack->pTab );
745 pTab = pTriggerStack->pTab;
746 }
747
748 if( pTab ){
749 int j;
750 Column *pCol = pTab->aCol;
751
752 pExpr->iDb = pTab->iDb;
753 cntTab++;
754 for(j=0; j < pTab->nCol; j++, pCol++) {
danielk19774adee202004-05-08 08:23:19 +0000755 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
drh8141f612004-01-25 22:44:58 +0000756 cnt++;
757 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
danielk1977a37cdde2004-05-16 11:15:36 +0000758 pExpr->affinity = pTab->aCol[j].affinity;
danielk19770202b292004-06-09 09:55:16 +0000759 pExpr->pColl = pTab->aCol[j].pColl;
drh8141f612004-01-25 22:44:58 +0000760 break;
761 }
762 }
763 }
764 }
drhb7f91642004-10-31 02:22:47 +0000765#endif /* !defined(SQLITE_OMIT_TRIGGER) */
drh8141f612004-01-25 22:44:58 +0000766
767 /*
768 ** Perhaps the name is a reference to the ROWID
769 */
danielk19774adee202004-05-08 08:23:19 +0000770 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
drh8141f612004-01-25 22:44:58 +0000771 cnt = 1;
772 pExpr->iColumn = -1;
danielk1977a37cdde2004-05-16 11:15:36 +0000773 pExpr->affinity = SQLITE_AFF_INTEGER;
drh8141f612004-01-25 22:44:58 +0000774 }
775
776 /*
777 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
778 ** might refer to an result-set alias. This happens, for example, when
779 ** we are resolving names in the WHERE clause of the following command:
780 **
781 ** SELECT a+b AS x FROM table WHERE x<10;
782 **
783 ** In cases like this, replace pExpr with a copy of the expression that
784 ** forms the result set entry ("a+b" in the example) and return immediately.
785 ** Note that the expression in the result set should have already been
786 ** resolved by the time the WHERE clause is resolved.
787 */
788 if( cnt==0 && pEList!=0 ){
789 for(j=0; j<pEList->nExpr; j++){
790 char *zAs = pEList->a[j].zName;
danielk19774adee202004-05-08 08:23:19 +0000791 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
drh8141f612004-01-25 22:44:58 +0000792 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
793 pExpr->op = TK_AS;
794 pExpr->iColumn = j;
danielk19774adee202004-05-08 08:23:19 +0000795 pExpr->pLeft = sqlite3ExprDup(pEList->a[j].pExpr);
drh8141f612004-01-25 22:44:58 +0000796 sqliteFree(zCol);
797 assert( zTab==0 && zDb==0 );
798 return 0;
799 }
800 }
801 }
802
803 /*
804 ** If X and Y are NULL (in other words if only the column name Z is
805 ** supplied) and the value of Z is enclosed in double-quotes, then
806 ** Z is a string literal if it doesn't match any column names. In that
807 ** case, we need to return right away and not make any changes to
808 ** pExpr.
809 */
810 if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
811 sqliteFree(zCol);
812 return 0;
813 }
814
815 /*
816 ** cnt==0 means there was not match. cnt>1 means there were two or
817 ** more matches. Either way, we have an error.
818 */
819 if( cnt!=1 ){
820 char *z = 0;
821 char *zErr;
822 zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s";
823 if( zDb ){
danielk19774adee202004-05-08 08:23:19 +0000824 sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, 0);
drh8141f612004-01-25 22:44:58 +0000825 }else if( zTab ){
danielk19774adee202004-05-08 08:23:19 +0000826 sqlite3SetString(&z, zTab, ".", zCol, 0);
drh8141f612004-01-25 22:44:58 +0000827 }else{
828 z = sqliteStrDup(zCol);
829 }
danielk19774adee202004-05-08 08:23:19 +0000830 sqlite3ErrorMsg(pParse, zErr, z);
drh8141f612004-01-25 22:44:58 +0000831 sqliteFree(z);
832 }
833
834 /* Clean up and return
835 */
836 sqliteFree(zDb);
837 sqliteFree(zTab);
838 sqliteFree(zCol);
danielk19774adee202004-05-08 08:23:19 +0000839 sqlite3ExprDelete(pExpr->pLeft);
drh8141f612004-01-25 22:44:58 +0000840 pExpr->pLeft = 0;
danielk19774adee202004-05-08 08:23:19 +0000841 sqlite3ExprDelete(pExpr->pRight);
drh8141f612004-01-25 22:44:58 +0000842 pExpr->pRight = 0;
843 pExpr->op = TK_COLUMN;
danielk19774adee202004-05-08 08:23:19 +0000844 sqlite3AuthRead(pParse, pExpr, pSrcList);
drh8141f612004-01-25 22:44:58 +0000845 return cnt!=1;
846}
847
848/*
drhcce7d172000-05-31 15:34:51 +0000849** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +0000850** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +0000851** index to the table in the table list and a column offset. The
852** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
853** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +0000854** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +0000855** VDBE cursor number for a cursor that is pointing into the referenced
856** table. The Expr.iColumn value is changed to the index of the column
857** of the referenced table. The Expr.iColumn value for the special
858** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
859** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +0000860**
drhfef52082000-06-06 01:50:43 +0000861** We also check for instances of the IN operator. IN comes in two
862** forms:
863**
864** expr IN (exprlist)
865** and
866** expr IN (SELECT ...)
867**
868** The first form is handled by creating a set holding the list
869** of allowed values. The second form causes the SELECT to generate
870** a temporary table.
871**
872** This routine also looks for scalar SELECTs that are part of an expression.
drh19a775c2000-06-05 18:54:46 +0000873** If it finds any, it generates code to write the value of that select
874** into a memory cell.
drhcce7d172000-05-31 15:34:51 +0000875**
drh967e8b72000-06-21 13:59:10 +0000876** Unknown columns or tables provoke an error. The function returns
drhcce7d172000-05-31 15:34:51 +0000877** the number of errors seen and leaves an error message on pParse->zErrMsg.
878*/
danielk19774adee202004-05-08 08:23:19 +0000879int sqlite3ExprResolveIds(
drha2e00042002-01-22 03:13:42 +0000880 Parse *pParse, /* The parser context */
drh8141f612004-01-25 22:44:58 +0000881 SrcList *pSrcList, /* List of tables used to resolve column names */
drha2e00042002-01-22 03:13:42 +0000882 ExprList *pEList, /* List of expressions used to resolve "AS" */
883 Expr *pExpr /* The expression to be analyzed. */
884){
drh6a3ea0e2003-05-02 14:32:12 +0000885 int i;
886
drh8141f612004-01-25 22:44:58 +0000887 if( pExpr==0 || pSrcList==0 ) return 0;
888 for(i=0; i<pSrcList->nSrc; i++){
889 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab );
drh6a3ea0e2003-05-02 14:32:12 +0000890 }
drhcce7d172000-05-31 15:34:51 +0000891 switch( pExpr->op ){
drh23989372002-05-21 13:43:04 +0000892 /* Double-quoted strings (ex: "abc") are used as identifiers if
893 ** possible. Otherwise they remain as strings. Single-quoted
894 ** strings (ex: 'abc') are always string literals.
895 */
896 case TK_STRING: {
897 if( pExpr->token.z[0]=='\'' ) break;
898 /* Fall thru into the TK_ID case if this is a double-quoted string */
899 }
drh8141f612004-01-25 22:44:58 +0000900 /* A lone identifier is the name of a columnd.
drha2e00042002-01-22 03:13:42 +0000901 */
drhcce7d172000-05-31 15:34:51 +0000902 case TK_ID: {
drh8141f612004-01-25 22:44:58 +0000903 if( lookupName(pParse, 0, 0, &pExpr->token, pSrcList, pEList, pExpr) ){
drhcce7d172000-05-31 15:34:51 +0000904 return 1;
drhed6c8672003-01-12 18:02:16 +0000905 }
drhcce7d172000-05-31 15:34:51 +0000906 break;
907 }
908
drhd24cc422003-03-27 12:51:24 +0000909 /* A table name and column name: ID.ID
910 ** Or a database, table and column: ID.ID.ID
911 */
drhcce7d172000-05-31 15:34:51 +0000912 case TK_DOT: {
drh8141f612004-01-25 22:44:58 +0000913 Token *pColumn;
914 Token *pTable;
915 Token *pDb;
916 Expr *pRight;
drhcce7d172000-05-31 15:34:51 +0000917
drhcce7d172000-05-31 15:34:51 +0000918 pRight = pExpr->pRight;
drhd24cc422003-03-27 12:51:24 +0000919 if( pRight->op==TK_ID ){
drh8141f612004-01-25 22:44:58 +0000920 pDb = 0;
921 pTable = &pExpr->pLeft->token;
922 pColumn = &pRight->token;
drhd24cc422003-03-27 12:51:24 +0000923 }else{
drh8141f612004-01-25 22:44:58 +0000924 assert( pRight->op==TK_DOT );
925 pDb = &pExpr->pLeft->token;
926 pTable = &pRight->pLeft->token;
927 pColumn = &pRight->pRight->token;
drhd24cc422003-03-27 12:51:24 +0000928 }
drh8141f612004-01-25 22:44:58 +0000929 if( lookupName(pParse, pDb, pTable, pColumn, pSrcList, 0, pExpr) ){
drhdaffd0e2001-04-11 14:28:42 +0000930 return 1;
931 }
drhcce7d172000-05-31 15:34:51 +0000932 break;
933 }
934
drhfef52082000-06-06 01:50:43 +0000935 case TK_IN: {
danielk1977e014a832004-05-17 10:48:57 +0000936 char affinity;
danielk19774adee202004-05-08 08:23:19 +0000937 Vdbe *v = sqlite3GetVdbe(pParse);
drhd3d39e92004-05-20 22:16:29 +0000938 KeyInfo keyInfo;
danielk19770202b292004-06-09 09:55:16 +0000939 int addr; /* Address of OP_OpenTemp instruction */
drhd3d39e92004-05-20 22:16:29 +0000940
drhfef52082000-06-06 01:50:43 +0000941 if( v==0 ) return 1;
danielk19774adee202004-05-08 08:23:19 +0000942 if( sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pLeft) ){
drhcfab11b2000-06-06 03:31:22 +0000943 return 1;
944 }
danielk1977bf3b7212004-05-18 10:06:24 +0000945 affinity = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +0000946
947 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
948 ** expression it is handled the same way. A temporary table is
949 ** filled with single-field index keys representing the results
950 ** from the SELECT or the <exprlist>.
951 **
952 ** If the 'x' expression is a column value, or the SELECT...
953 ** statement returns a column value, then the affinity of that
954 ** column is used to build the index keys. If both 'x' and the
955 ** SELECT... statement are columns, then numeric affinity is used
956 ** if either column has NUMERIC or INTEGER affinity. If neither
957 ** 'x' nor the SELECT... statement are columns, then numeric affinity
958 ** is used.
959 */
960 pExpr->iTable = pParse->nTab++;
danielk19770202b292004-06-09 09:55:16 +0000961 addr = sqlite3VdbeAddOp(v, OP_OpenTemp, pExpr->iTable, 0);
drhd3d39e92004-05-20 22:16:29 +0000962 memset(&keyInfo, 0, sizeof(keyInfo));
963 keyInfo.nField = 1;
drhf3218fe2004-05-28 08:21:02 +0000964 sqlite3VdbeAddOp(v, OP_SetNumColumns, pExpr->iTable, 1);
danielk1977e014a832004-05-17 10:48:57 +0000965
drhfef52082000-06-06 01:50:43 +0000966 if( pExpr->pSelect ){
967 /* Case 1: expr IN (SELECT ...)
968 **
danielk1977e014a832004-05-17 10:48:57 +0000969 ** Generate code to write the results of the select into the temporary
970 ** table allocated and opened above.
drhfef52082000-06-06 01:50:43 +0000971 */
danielk1977e014a832004-05-17 10:48:57 +0000972 int iParm = pExpr->iTable + (((int)affinity)<<16);
drhbe5c89a2004-07-26 00:31:09 +0000973 ExprList *pEList;
danielk1977e014a832004-05-17 10:48:57 +0000974 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
danielk1977bf3b7212004-05-18 10:06:24 +0000975 sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +0000976 pEList = pExpr->pSelect->pEList;
977 if( pEList && pEList->nExpr>0 ){
danielk19777cedc8d2004-06-10 10:50:08 +0000978 keyInfo.aColl[0] = binaryCompareCollSeq(pParse, pExpr->pLeft,
drhbe5c89a2004-07-26 00:31:09 +0000979 pEList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +0000980 }
drhfef52082000-06-06 01:50:43 +0000981 }else if( pExpr->pList ){
982 /* Case 2: expr IN (exprlist)
983 **
danielk1977e014a832004-05-17 10:48:57 +0000984 ** For each expression, build an index key from the evaluation and
985 ** store it in the temporary table. If <expr> is a column, then use
986 ** that columns affinity when building index keys. If <expr> is not
987 ** a column, use numeric affinity.
drhfef52082000-06-06 01:50:43 +0000988 */
danielk1977e014a832004-05-17 10:48:57 +0000989 int i;
danielk1977e014a832004-05-17 10:48:57 +0000990 if( !affinity ){
991 affinity = SQLITE_AFF_NUMERIC;
992 }
danielk19770202b292004-06-09 09:55:16 +0000993 keyInfo.aColl[0] = pExpr->pLeft->pColl;
danielk1977e014a832004-05-17 10:48:57 +0000994
995 /* Loop through each expression in <exprlist>. */
drhfef52082000-06-06 01:50:43 +0000996 for(i=0; i<pExpr->pList->nExpr; i++){
997 Expr *pE2 = pExpr->pList->a[i].pExpr;
danielk1977e014a832004-05-17 10:48:57 +0000998
999 /* Check that the expression is constant and valid. */
danielk19774adee202004-05-08 08:23:19 +00001000 if( !sqlite3ExprIsConstant(pE2) ){
1001 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00001002 "right-hand side of IN operator must be constant");
drhfef52082000-06-06 01:50:43 +00001003 return 1;
1004 }
danielk19774adee202004-05-08 08:23:19 +00001005 if( sqlite3ExprCheck(pParse, pE2, 0, 0) ){
drh4794b982000-06-06 13:54:14 +00001006 return 1;
1007 }
danielk1977e014a832004-05-17 10:48:57 +00001008
1009 /* Evaluate the expression and insert it into the temp table */
1010 sqlite3ExprCode(pParse, pE2);
drh94a11212004-09-25 13:12:14 +00001011 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1);
danielk19770f69c1e2004-05-29 11:24:50 +00001012 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +00001013 sqlite3VdbeAddOp(v, OP_PutStrKey, pExpr->iTable, 0);
drhfef52082000-06-06 01:50:43 +00001014 }
1015 }
danielk19770202b292004-06-09 09:55:16 +00001016 sqlite3VdbeChangeP3(v, addr, (void *)&keyInfo, P3_KEYINFO);
1017
drhcfab11b2000-06-06 03:31:22 +00001018 break;
drhfef52082000-06-06 01:50:43 +00001019 }
1020
drh19a775c2000-06-05 18:54:46 +00001021 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +00001022 /* This has to be a scalar SELECT. Generate code to put the
1023 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +00001024 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +00001025 */
drh967e8b72000-06-21 13:59:10 +00001026 pExpr->iColumn = pParse->nMem++;
danielk1977bf3b7212004-05-18 10:06:24 +00001027 if(sqlite3Select(pParse, pExpr->pSelect, SRT_Mem,pExpr->iColumn,0,0,0,0)){
drh19a775c2000-06-05 18:54:46 +00001028 return 1;
1029 }
1030 break;
1031 }
1032
drhcce7d172000-05-31 15:34:51 +00001033 /* For all else, just recursively walk the tree */
1034 default: {
drh4794b982000-06-06 13:54:14 +00001035 if( pExpr->pLeft
danielk19774adee202004-05-08 08:23:19 +00001036 && sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pLeft) ){
drhcce7d172000-05-31 15:34:51 +00001037 return 1;
1038 }
1039 if( pExpr->pRight
danielk19774adee202004-05-08 08:23:19 +00001040 && sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pRight) ){
drhcce7d172000-05-31 15:34:51 +00001041 return 1;
1042 }
1043 if( pExpr->pList ){
1044 int i;
1045 ExprList *pList = pExpr->pList;
1046 for(i=0; i<pList->nExpr; i++){
drh832508b2002-03-02 17:04:07 +00001047 Expr *pArg = pList->a[i].pExpr;
danielk19774adee202004-05-08 08:23:19 +00001048 if( sqlite3ExprResolveIds(pParse, pSrcList, pEList, pArg) ){
drhcce7d172000-05-31 15:34:51 +00001049 return 1;
1050 }
1051 }
1052 }
1053 }
1054 }
1055 return 0;
1056}
1057
drhcce7d172000-05-31 15:34:51 +00001058/*
drh4b59ab52002-08-24 18:24:51 +00001059** pExpr is a node that defines a function of some kind. It might
1060** be a syntactic function like "count(x)" or it might be a function
1061** that implements an operator, like "a LIKE b".
1062**
1063** This routine makes *pzName point to the name of the function and
1064** *pnName hold the number of characters in the function name.
1065*/
1066static void getFunctionName(Expr *pExpr, const char **pzName, int *pnName){
1067 switch( pExpr->op ){
1068 case TK_FUNCTION: {
1069 *pzName = pExpr->token.z;
drh6977fea2002-10-22 23:38:04 +00001070 *pnName = pExpr->token.n;
drh4b59ab52002-08-24 18:24:51 +00001071 break;
1072 }
1073 case TK_LIKE: {
1074 *pzName = "like";
1075 *pnName = 4;
1076 break;
1077 }
1078 case TK_GLOB: {
1079 *pzName = "glob";
1080 *pnName = 4;
1081 break;
1082 }
1083 default: {
1084 *pzName = "can't happen";
1085 *pnName = 12;
1086 break;
1087 }
1088 }
1089}
1090
1091/*
drhcce7d172000-05-31 15:34:51 +00001092** Error check the functions in an expression. Make sure all
1093** function names are recognized and all functions have the correct
1094** number of arguments. Leave an error message in pParse->zErrMsg
1095** if anything is amiss. Return the number of errors.
1096**
1097** if pIsAgg is not null and this expression is an aggregate function
1098** (like count(*) or max(value)) then write a 1 into *pIsAgg.
1099*/
danielk19774adee202004-05-08 08:23:19 +00001100int sqlite3ExprCheck(Parse *pParse, Expr *pExpr, int allowAgg, int *pIsAgg){
drhcce7d172000-05-31 15:34:51 +00001101 int nErr = 0;
1102 if( pExpr==0 ) return 0;
drhcce7d172000-05-31 15:34:51 +00001103 switch( pExpr->op ){
drh4b59ab52002-08-24 18:24:51 +00001104 case TK_GLOB:
1105 case TK_LIKE:
drhcce7d172000-05-31 15:34:51 +00001106 case TK_FUNCTION: {
drhc9b84a12002-06-20 11:36:48 +00001107 int n = pExpr->pList ? pExpr->pList->nExpr : 0; /* Number of arguments */
1108 int no_such_func = 0; /* True if no such function exists */
drhc9b84a12002-06-20 11:36:48 +00001109 int wrong_num_args = 0; /* True if wrong number of arguments */
1110 int is_agg = 0; /* True if is an aggregate function */
drhcce7d172000-05-31 15:34:51 +00001111 int i;
drh4b59ab52002-08-24 18:24:51 +00001112 int nId; /* Number of characters in function name */
1113 const char *zId; /* The function name. */
drh0bce8352002-02-28 00:41:10 +00001114 FuncDef *pDef;
danielk1977d8123362004-06-12 09:25:12 +00001115 int enc = pParse->db->enc;
drh0bce8352002-02-28 00:41:10 +00001116
drh4b59ab52002-08-24 18:24:51 +00001117 getFunctionName(pExpr, &zId, &nId);
danielk1977d8123362004-06-12 09:25:12 +00001118 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001119 if( pDef==0 ){
danielk1977d8123362004-06-12 09:25:12 +00001120 pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001121 if( pDef==0 ){
drh268380c2004-02-25 13:47:31 +00001122 no_such_func = 1;
drh0bce8352002-02-28 00:41:10 +00001123 }else{
1124 wrong_num_args = 1;
drhcce7d172000-05-31 15:34:51 +00001125 }
drh0bce8352002-02-28 00:41:10 +00001126 }else{
1127 is_agg = pDef->xFunc==0;
drhcce7d172000-05-31 15:34:51 +00001128 }
drh8e0a2f92002-02-23 23:45:45 +00001129 if( is_agg && !allowAgg ){
danielk19774adee202004-05-08 08:23:19 +00001130 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId, zId);
drh8e0a2f92002-02-23 23:45:45 +00001131 nErr++;
1132 is_agg = 0;
1133 }else if( no_such_func ){
danielk19774adee202004-05-08 08:23:19 +00001134 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
drhcce7d172000-05-31 15:34:51 +00001135 nErr++;
drh8e0a2f92002-02-23 23:45:45 +00001136 }else if( wrong_num_args ){
danielk19774adee202004-05-08 08:23:19 +00001137 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
drhf7a9e1a2004-02-22 18:40:56 +00001138 nId, zId);
drh8e0a2f92002-02-23 23:45:45 +00001139 nErr++;
drhcce7d172000-05-31 15:34:51 +00001140 }
drhf7a9e1a2004-02-22 18:40:56 +00001141 if( is_agg ){
1142 pExpr->op = TK_AGG_FUNCTION;
1143 if( pIsAgg ) *pIsAgg = 1;
1144 }
drhcce7d172000-05-31 15:34:51 +00001145 for(i=0; nErr==0 && i<n; i++){
danielk19774adee202004-05-08 08:23:19 +00001146 nErr = sqlite3ExprCheck(pParse, pExpr->pList->a[i].pExpr,
drh4cfa7932000-06-08 15:10:46 +00001147 allowAgg && !is_agg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +00001148 }
danielk19770202b292004-06-09 09:55:16 +00001149 /* FIX ME: Compute pExpr->affinity based on the expected return
1150 ** type of the function
1151 */
drhcce7d172000-05-31 15:34:51 +00001152 }
1153 default: {
1154 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001155 nErr = sqlite3ExprCheck(pParse, pExpr->pLeft, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +00001156 }
1157 if( nErr==0 && pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001158 nErr = sqlite3ExprCheck(pParse, pExpr->pRight, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +00001159 }
drhfef52082000-06-06 01:50:43 +00001160 if( nErr==0 && pExpr->pList ){
1161 int n = pExpr->pList->nExpr;
1162 int i;
1163 for(i=0; nErr==0 && i<n; i++){
drh22827922000-06-06 17:27:05 +00001164 Expr *pE2 = pExpr->pList->a[i].pExpr;
danielk19774adee202004-05-08 08:23:19 +00001165 nErr = sqlite3ExprCheck(pParse, pE2, allowAgg, pIsAgg);
drhfef52082000-06-06 01:50:43 +00001166 }
1167 }
drhcce7d172000-05-31 15:34:51 +00001168 break;
1169 }
1170 }
1171 return nErr;
1172}
1173
1174/*
drh290c1942004-08-21 17:54:45 +00001175** Call sqlite3ExprResolveIds() followed by sqlite3ExprCheck().
1176**
1177** This routine is provided as a convenience since it is very common
1178** to call ResolveIds() and Check() back to back.
1179*/
1180int sqlite3ExprResolveAndCheck(
1181 Parse *pParse, /* The parser context */
1182 SrcList *pSrcList, /* List of tables used to resolve column names */
1183 ExprList *pEList, /* List of expressions used to resolve "AS" */
1184 Expr *pExpr, /* The expression to be analyzed. */
1185 int allowAgg, /* True to allow aggregate expressions */
1186 int *pIsAgg /* Set to TRUE if aggregates are found */
1187){
1188 if( pExpr==0 ) return 0;
1189 if( sqlite3ExprResolveIds(pParse,pSrcList,pEList,pExpr) ){
1190 return 1;
1191 }
1192 return sqlite3ExprCheck(pParse, pExpr, allowAgg, pIsAgg);
1193}
1194
1195/*
drhfec19aa2004-05-19 20:41:03 +00001196** Generate an instruction that will put the integer describe by
1197** text z[0..n-1] on the stack.
1198*/
1199static void codeInteger(Vdbe *v, const char *z, int n){
1200 int i;
drh6fec0762004-05-30 01:38:43 +00001201 if( sqlite3GetInt32(z, &i) ){
1202 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
1203 }else if( sqlite3FitsIn64Bits(z) ){
1204 sqlite3VdbeOp3(v, OP_Integer, 0, 0, z, n);
drhfec19aa2004-05-19 20:41:03 +00001205 }else{
1206 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n);
1207 }
1208}
1209
1210/*
drhcce7d172000-05-31 15:34:51 +00001211** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +00001212** expression and leave the result on the top of stack.
drhf2bc0132004-10-04 13:19:23 +00001213**
1214** This code depends on the fact that certain token values (ex: TK_EQ)
1215** are the same as opcode values (ex: OP_Eq) that implement the corresponding
1216** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
1217** the make process cause these values to align. Assert()s in the code
1218** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00001219*/
danielk19774adee202004-05-08 08:23:19 +00001220void sqlite3ExprCode(Parse *pParse, Expr *pExpr){
drhcce7d172000-05-31 15:34:51 +00001221 Vdbe *v = pParse->pVdbe;
1222 int op;
drhdaffd0e2001-04-11 14:28:42 +00001223 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00001224 op = pExpr->op;
1225 switch( op ){
drh967e8b72000-06-21 13:59:10 +00001226 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +00001227 if( pParse->useAgg ){
danielk19774adee202004-05-08 08:23:19 +00001228 sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drhc4a3c772001-04-04 11:48:57 +00001229 }else if( pExpr->iColumn>=0 ){
danielk19774adee202004-05-08 08:23:19 +00001230 sqlite3VdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn);
drh145716b2004-09-24 12:24:06 +00001231#ifndef NDEBUG
1232 if( pExpr->span.z && pExpr->span.n>0 && pExpr->span.n<100 ){
1233 VdbeComment((v, "# %T", &pExpr->span));
1234 }
1235#endif
drhc4a3c772001-04-04 11:48:57 +00001236 }else{
danielk19774adee202004-05-08 08:23:19 +00001237 sqlite3VdbeAddOp(v, OP_Recno, pExpr->iTable, 0);
drh22827922000-06-06 17:27:05 +00001238 }
drhcce7d172000-05-31 15:34:51 +00001239 break;
1240 }
1241 case TK_INTEGER: {
drhfec19aa2004-05-19 20:41:03 +00001242 codeInteger(v, pExpr->token.z, pExpr->token.n);
1243 break;
1244 }
1245 case TK_FLOAT:
1246 case TK_STRING: {
drhf2bc0132004-10-04 13:19:23 +00001247 assert( TK_FLOAT==OP_Real );
1248 assert( TK_STRING==OP_String8 );
drhfec19aa2004-05-19 20:41:03 +00001249 sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z, pExpr->token.n);
danielk19774adee202004-05-08 08:23:19 +00001250 sqlite3VdbeDequoteP3(v, -1);
drhcce7d172000-05-31 15:34:51 +00001251 break;
1252 }
danielk1977c572ef72004-05-27 09:28:41 +00001253 case TK_BLOB: {
drhf2bc0132004-10-04 13:19:23 +00001254 assert( TK_BLOB==OP_HexBlob );
danielk1977c572ef72004-05-27 09:28:41 +00001255 sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z+1, pExpr->token.n-1);
1256 sqlite3VdbeDequoteP3(v, -1);
1257 break;
1258 }
drhcce7d172000-05-31 15:34:51 +00001259 case TK_NULL: {
danielk19770f69c1e2004-05-29 11:24:50 +00001260 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001261 break;
1262 }
drh50457892003-09-06 01:10:47 +00001263 case TK_VARIABLE: {
danielk19774adee202004-05-08 08:23:19 +00001264 sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0);
drh895d7472004-08-20 16:02:39 +00001265 if( pExpr->token.n>1 ){
1266 sqlite3VdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
1267 }
drh50457892003-09-06 01:10:47 +00001268 break;
1269 }
drh4e0cff62004-11-05 05:10:28 +00001270 case TK_REGISTER: {
1271 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iTable, 0);
1272 break;
1273 }
drhc9b84a12002-06-20 11:36:48 +00001274 case TK_LT:
1275 case TK_LE:
1276 case TK_GT:
1277 case TK_GE:
1278 case TK_NE:
1279 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00001280 assert( TK_LT==OP_Lt );
1281 assert( TK_LE==OP_Le );
1282 assert( TK_GT==OP_Gt );
1283 assert( TK_GE==OP_Ge );
1284 assert( TK_EQ==OP_Eq );
1285 assert( TK_NE==OP_Ne );
danielk1977a37cdde2004-05-16 11:15:36 +00001286 sqlite3ExprCode(pParse, pExpr->pLeft);
1287 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001288 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 0, 0);
danielk1977a37cdde2004-05-16 11:15:36 +00001289 break;
drhc9b84a12002-06-20 11:36:48 +00001290 }
drhcce7d172000-05-31 15:34:51 +00001291 case TK_AND:
1292 case TK_OR:
1293 case TK_PLUS:
1294 case TK_STAR:
1295 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00001296 case TK_REM:
1297 case TK_BITAND:
1298 case TK_BITOR:
drh17c40292004-07-21 02:53:29 +00001299 case TK_SLASH:
drhbf4133c2001-10-13 02:59:08 +00001300 case TK_LSHIFT:
drh855eb1c2004-08-31 13:45:11 +00001301 case TK_RSHIFT:
drh00400772000-06-16 20:51:26 +00001302 case TK_CONCAT: {
drhf2bc0132004-10-04 13:19:23 +00001303 assert( TK_AND==OP_And );
1304 assert( TK_OR==OP_Or );
1305 assert( TK_PLUS==OP_Add );
1306 assert( TK_MINUS==OP_Subtract );
1307 assert( TK_REM==OP_Remainder );
1308 assert( TK_BITAND==OP_BitAnd );
1309 assert( TK_BITOR==OP_BitOr );
1310 assert( TK_SLASH==OP_Divide );
1311 assert( TK_LSHIFT==OP_ShiftLeft );
1312 assert( TK_RSHIFT==OP_ShiftRight );
1313 assert( TK_CONCAT==OP_Concat );
danielk19774adee202004-05-08 08:23:19 +00001314 sqlite3ExprCode(pParse, pExpr->pLeft);
1315 sqlite3ExprCode(pParse, pExpr->pRight);
drh855eb1c2004-08-31 13:45:11 +00001316 sqlite3VdbeAddOp(v, op, 0, 0);
drh00400772000-06-16 20:51:26 +00001317 break;
1318 }
drhcce7d172000-05-31 15:34:51 +00001319 case TK_UMINUS: {
drhfec19aa2004-05-19 20:41:03 +00001320 Expr *pLeft = pExpr->pLeft;
1321 assert( pLeft );
1322 if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
1323 Token *p = &pLeft->token;
drh6e142f52000-06-08 13:36:40 +00001324 char *z = sqliteMalloc( p->n + 2 );
1325 sprintf(z, "-%.*s", p->n, p->z);
drhfec19aa2004-05-19 20:41:03 +00001326 if( pLeft->op==TK_FLOAT ){
1327 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001328 }else{
drhfec19aa2004-05-19 20:41:03 +00001329 codeInteger(v, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001330 }
drh6e142f52000-06-08 13:36:40 +00001331 sqliteFree(z);
1332 break;
1333 }
drh1ccde152000-06-17 13:12:39 +00001334 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +00001335 }
drhbf4133c2001-10-13 02:59:08 +00001336 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00001337 case TK_NOT: {
drhf2bc0132004-10-04 13:19:23 +00001338 assert( TK_BITNOT==OP_BitNot );
1339 assert( TK_NOT==OP_Not );
danielk19774adee202004-05-08 08:23:19 +00001340 sqlite3ExprCode(pParse, pExpr->pLeft);
1341 sqlite3VdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001342 break;
1343 }
1344 case TK_ISNULL:
1345 case TK_NOTNULL: {
1346 int dest;
drhf2bc0132004-10-04 13:19:23 +00001347 assert( TK_ISNULL==OP_IsNull );
1348 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00001349 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1350 sqlite3ExprCode(pParse, pExpr->pLeft);
1351 dest = sqlite3VdbeCurrentAddr(v) + 2;
1352 sqlite3VdbeAddOp(v, op, 1, dest);
1353 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);
drhf2bc0132004-10-04 13:19:23 +00001354 break;
drhcce7d172000-05-31 15:34:51 +00001355 }
drh22827922000-06-06 17:27:05 +00001356 case TK_AGG_FUNCTION: {
danielk19774adee202004-05-08 08:23:19 +00001357 sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drh22827922000-06-06 17:27:05 +00001358 break;
1359 }
drh4b59ab52002-08-24 18:24:51 +00001360 case TK_GLOB:
1361 case TK_LIKE:
drhcce7d172000-05-31 15:34:51 +00001362 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00001363 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00001364 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00001365 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00001366 int nId;
1367 const char *zId;
danielk1977682f68b2004-06-05 10:22:17 +00001368 int p2 = 0;
1369 int i;
danielk1977d8123362004-06-12 09:25:12 +00001370 u8 enc = pParse->db->enc;
danielk1977dc1bdc42004-06-11 10:51:27 +00001371 CollSeq *pColl = 0;
drh4b59ab52002-08-24 18:24:51 +00001372 getFunctionName(pExpr, &zId, &nId);
danielk1977d8123362004-06-12 09:25:12 +00001373 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001374 assert( pDef!=0 );
drhf9b596e2004-05-26 16:54:42 +00001375 nExpr = sqlite3ExprCodeExprList(pParse, pList);
danielk1977682f68b2004-06-05 10:22:17 +00001376 for(i=0; i<nExpr && i<32; i++){
danielk1977d02eb1f2004-06-06 09:44:03 +00001377 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
1378 p2 |= (1<<i);
1379 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001380 if( pDef->needCollSeq && !pColl ){
1381 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
1382 }
1383 }
1384 if( pDef->needCollSeq ){
1385 if( !pColl ) pColl = pParse->db->pDfltColl;
danielk1977d8123362004-06-12 09:25:12 +00001386 sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ);
danielk1977682f68b2004-06-05 10:22:17 +00001387 }
1388 sqlite3VdbeOp3(v, OP_Function, nExpr, p2, (char*)pDef, P3_FUNCDEF);
drhcce7d172000-05-31 15:34:51 +00001389 break;
1390 }
drh19a775c2000-06-05 18:54:46 +00001391 case TK_SELECT: {
danielk19774adee202004-05-08 08:23:19 +00001392 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drhad6d9462004-09-19 02:15:24 +00001393 VdbeComment((v, "# load subquery result"));
drh19a775c2000-06-05 18:54:46 +00001394 break;
1395 }
drhfef52082000-06-06 01:50:43 +00001396 case TK_IN: {
1397 int addr;
drh94a11212004-09-25 13:12:14 +00001398 char affinity;
danielk1977e014a832004-05-17 10:48:57 +00001399
1400 /* Figure out the affinity to use to create a key from the results
1401 ** of the expression. affinityStr stores a static string suitable for
danielk1977ededfd52004-06-17 07:53:01 +00001402 ** P3 of OP_MakeRecord.
danielk1977e014a832004-05-17 10:48:57 +00001403 */
drh94a11212004-09-25 13:12:14 +00001404 affinity = comparisonAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +00001405
danielk19774adee202004-05-08 08:23:19 +00001406 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
danielk1977e014a832004-05-17 10:48:57 +00001407
1408 /* Code the <expr> from "<expr> IN (...)". The temporary table
1409 ** pExpr->iTable contains the values that make up the (...) set.
1410 */
danielk19774adee202004-05-08 08:23:19 +00001411 sqlite3ExprCode(pParse, pExpr->pLeft);
1412 addr = sqlite3VdbeCurrentAddr(v);
danielk1977e014a832004-05-17 10:48:57 +00001413 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); /* addr + 0 */
danielk19774adee202004-05-08 08:23:19 +00001414 sqlite3VdbeAddOp(v, OP_Pop, 2, 0);
danielk19770f69c1e2004-05-29 11:24:50 +00001415 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +00001416 sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7);
drh94a11212004-09-25 13:12:14 +00001417 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1); /* addr + 4 */
danielk1977e014a832004-05-17 10:48:57 +00001418 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7);
1419 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); /* addr + 6 */
1420
drhfef52082000-06-06 01:50:43 +00001421 break;
1422 }
1423 case TK_BETWEEN: {
drhbe5c89a2004-07-26 00:31:09 +00001424 Expr *pLeft = pExpr->pLeft;
1425 struct ExprList_item *pLItem = pExpr->pList->a;
1426 Expr *pRight = pLItem->pExpr;
1427 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001428 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001429 sqlite3ExprCode(pParse, pRight);
1430 codeCompare(pParse, pLeft, pRight, OP_Ge, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001431 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
drhbe5c89a2004-07-26 00:31:09 +00001432 pLItem++;
1433 pRight = pLItem->pExpr;
1434 sqlite3ExprCode(pParse, pRight);
1435 codeCompare(pParse, pLeft, pRight, OP_Le, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001436 sqlite3VdbeAddOp(v, OP_And, 0, 0);
drhfef52082000-06-06 01:50:43 +00001437 break;
1438 }
drh51e9a442004-01-16 16:42:53 +00001439 case TK_UPLUS:
drha2e00042002-01-22 03:13:42 +00001440 case TK_AS: {
danielk19774adee202004-05-08 08:23:19 +00001441 sqlite3ExprCode(pParse, pExpr->pLeft);
drha2e00042002-01-22 03:13:42 +00001442 break;
1443 }
drh17a7f8d2002-03-24 13:13:27 +00001444 case TK_CASE: {
1445 int expr_end_label;
drhf5905aa2002-05-26 20:54:33 +00001446 int jumpInst;
1447 int addr;
1448 int nExpr;
drh17a7f8d2002-03-24 13:13:27 +00001449 int i;
drhbe5c89a2004-07-26 00:31:09 +00001450 ExprList *pEList;
1451 struct ExprList_item *aListelem;
drh17a7f8d2002-03-24 13:13:27 +00001452
1453 assert(pExpr->pList);
1454 assert((pExpr->pList->nExpr % 2) == 0);
1455 assert(pExpr->pList->nExpr > 0);
drhbe5c89a2004-07-26 00:31:09 +00001456 pEList = pExpr->pList;
1457 aListelem = pEList->a;
1458 nExpr = pEList->nExpr;
danielk19774adee202004-05-08 08:23:19 +00001459 expr_end_label = sqlite3VdbeMakeLabel(v);
drh17a7f8d2002-03-24 13:13:27 +00001460 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001461 sqlite3ExprCode(pParse, pExpr->pLeft);
drh17a7f8d2002-03-24 13:13:27 +00001462 }
drhf5905aa2002-05-26 20:54:33 +00001463 for(i=0; i<nExpr; i=i+2){
drhbe5c89a2004-07-26 00:31:09 +00001464 sqlite3ExprCode(pParse, aListelem[i].pExpr);
drh17a7f8d2002-03-24 13:13:27 +00001465 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001466 sqlite3VdbeAddOp(v, OP_Dup, 1, 1);
drhbe5c89a2004-07-26 00:31:09 +00001467 jumpInst = codeCompare(pParse, pExpr->pLeft, aListelem[i].pExpr,
1468 OP_Ne, 0, 1);
danielk19774adee202004-05-08 08:23:19 +00001469 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001470 }else{
danielk19774adee202004-05-08 08:23:19 +00001471 jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001472 }
drhbe5c89a2004-07-26 00:31:09 +00001473 sqlite3ExprCode(pParse, aListelem[i+1].pExpr);
danielk19774adee202004-05-08 08:23:19 +00001474 sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label);
1475 addr = sqlite3VdbeCurrentAddr(v);
1476 sqlite3VdbeChangeP2(v, jumpInst, addr);
drh17a7f8d2002-03-24 13:13:27 +00001477 }
drhf570f012002-05-31 15:51:25 +00001478 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001479 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhf570f012002-05-31 15:51:25 +00001480 }
drh17a7f8d2002-03-24 13:13:27 +00001481 if( pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001482 sqlite3ExprCode(pParse, pExpr->pRight);
drh17a7f8d2002-03-24 13:13:27 +00001483 }else{
danielk19770f69c1e2004-05-29 11:24:50 +00001484 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
drh17a7f8d2002-03-24 13:13:27 +00001485 }
danielk19774adee202004-05-08 08:23:19 +00001486 sqlite3VdbeResolveLabel(v, expr_end_label);
danielk19776f349032002-06-11 02:25:40 +00001487 break;
1488 }
1489 case TK_RAISE: {
1490 if( !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +00001491 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00001492 "RAISE() may only be used within a trigger-program");
danielk19776f349032002-06-11 02:25:40 +00001493 return;
1494 }
drhad6d9462004-09-19 02:15:24 +00001495 if( pExpr->iColumn!=OE_Ignore ){
1496 assert( pExpr->iColumn==OE_Rollback ||
1497 pExpr->iColumn == OE_Abort ||
1498 pExpr->iColumn == OE_Fail );
1499 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn,
1500 pExpr->token.z, pExpr->token.n);
1501 sqlite3VdbeDequoteP3(v, -1);
danielk19776f349032002-06-11 02:25:40 +00001502 } else {
drhad6d9462004-09-19 02:15:24 +00001503 assert( pExpr->iColumn == OE_Ignore );
1504 sqlite3VdbeAddOp(v, OP_ContextPop, 0, 0);
1505 sqlite3VdbeAddOp(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
1506 VdbeComment((v, "# raise(IGNORE)"));
danielk19776f349032002-06-11 02:25:40 +00001507 }
drh17a7f8d2002-03-24 13:13:27 +00001508 }
1509 break;
drhcce7d172000-05-31 15:34:51 +00001510 }
drhcce7d172000-05-31 15:34:51 +00001511}
1512
1513/*
drh268380c2004-02-25 13:47:31 +00001514** Generate code that pushes the value of every element of the given
drhf9b596e2004-05-26 16:54:42 +00001515** expression list onto the stack.
drh268380c2004-02-25 13:47:31 +00001516**
1517** Return the number of elements pushed onto the stack.
1518*/
danielk19774adee202004-05-08 08:23:19 +00001519int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00001520 Parse *pParse, /* Parsing context */
drhf9b596e2004-05-26 16:54:42 +00001521 ExprList *pList /* The expression list to be coded */
drh268380c2004-02-25 13:47:31 +00001522){
1523 struct ExprList_item *pItem;
1524 int i, n;
1525 Vdbe *v;
1526 if( pList==0 ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001527 v = sqlite3GetVdbe(pParse);
drh268380c2004-02-25 13:47:31 +00001528 n = pList->nExpr;
1529 for(pItem=pList->a, i=0; i<n; i++, pItem++){
danielk19774adee202004-05-08 08:23:19 +00001530 sqlite3ExprCode(pParse, pItem->pExpr);
drh268380c2004-02-25 13:47:31 +00001531 }
drhf9b596e2004-05-26 16:54:42 +00001532 return n;
drh268380c2004-02-25 13:47:31 +00001533}
1534
1535/*
drhcce7d172000-05-31 15:34:51 +00001536** Generate code for a boolean expression such that a jump is made
1537** to the label "dest" if the expression is true but execution
1538** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00001539**
1540** If the expression evaluates to NULL (neither true nor false), then
1541** take the jump if the jumpIfNull flag is true.
drhf2bc0132004-10-04 13:19:23 +00001542**
1543** This code depends on the fact that certain token values (ex: TK_EQ)
1544** are the same as opcode values (ex: OP_Eq) that implement the corresponding
1545** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
1546** the make process cause these values to align. Assert()s in the code
1547** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00001548*/
danielk19774adee202004-05-08 08:23:19 +00001549void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001550 Vdbe *v = pParse->pVdbe;
1551 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001552 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00001553 op = pExpr->op;
1554 switch( op ){
drhcce7d172000-05-31 15:34:51 +00001555 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001556 int d2 = sqlite3VdbeMakeLabel(v);
1557 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
1558 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
1559 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001560 break;
1561 }
1562 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001563 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
1564 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001565 break;
1566 }
1567 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001568 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001569 break;
1570 }
1571 case TK_LT:
1572 case TK_LE:
1573 case TK_GT:
1574 case TK_GE:
1575 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00001576 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00001577 assert( TK_LT==OP_Lt );
1578 assert( TK_LE==OP_Le );
1579 assert( TK_GT==OP_Gt );
1580 assert( TK_GE==OP_Ge );
1581 assert( TK_EQ==OP_Eq );
1582 assert( TK_NE==OP_Ne );
danielk19774adee202004-05-08 08:23:19 +00001583 sqlite3ExprCode(pParse, pExpr->pLeft);
1584 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001585 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001586 break;
1587 }
1588 case TK_ISNULL:
1589 case TK_NOTNULL: {
drhf2bc0132004-10-04 13:19:23 +00001590 assert( TK_ISNULL==OP_IsNull );
1591 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00001592 sqlite3ExprCode(pParse, pExpr->pLeft);
1593 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001594 break;
1595 }
drhfef52082000-06-06 01:50:43 +00001596 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00001597 /* The expression "x BETWEEN y AND z" is implemented as:
1598 **
1599 ** 1 IF (x < y) GOTO 3
1600 ** 2 IF (x <= z) GOTO <dest>
1601 ** 3 ...
1602 */
drhf5905aa2002-05-26 20:54:33 +00001603 int addr;
drhbe5c89a2004-07-26 00:31:09 +00001604 Expr *pLeft = pExpr->pLeft;
1605 Expr *pRight = pExpr->pList->a[0].pExpr;
1606 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001607 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001608 sqlite3ExprCode(pParse, pRight);
1609 addr = codeCompare(pParse, pLeft, pRight, OP_Lt, 0, !jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00001610
drhbe5c89a2004-07-26 00:31:09 +00001611 pRight = pExpr->pList->a[1].pExpr;
1612 sqlite3ExprCode(pParse, pRight);
1613 codeCompare(pParse, pLeft, pRight, OP_Le, dest, jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00001614
danielk19774adee202004-05-08 08:23:19 +00001615 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
1616 sqlite3VdbeChangeP2(v, addr, sqlite3VdbeCurrentAddr(v));
1617 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00001618 break;
1619 }
drhcce7d172000-05-31 15:34:51 +00001620 default: {
danielk19774adee202004-05-08 08:23:19 +00001621 sqlite3ExprCode(pParse, pExpr);
1622 sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001623 break;
1624 }
1625 }
1626}
1627
1628/*
drh66b89c82000-11-28 20:47:17 +00001629** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00001630** to the label "dest" if the expression is false but execution
1631** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00001632**
1633** If the expression evaluates to NULL (neither true nor false) then
1634** jump if jumpIfNull is true or fall through if jumpIfNull is false.
drhcce7d172000-05-31 15:34:51 +00001635*/
danielk19774adee202004-05-08 08:23:19 +00001636void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001637 Vdbe *v = pParse->pVdbe;
1638 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001639 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00001640
1641 /* The value of pExpr->op and op are related as follows:
1642 **
1643 ** pExpr->op op
1644 ** --------- ----------
1645 ** TK_ISNULL OP_NotNull
1646 ** TK_NOTNULL OP_IsNull
1647 ** TK_NE OP_Eq
1648 ** TK_EQ OP_Ne
1649 ** TK_GT OP_Le
1650 ** TK_LE OP_Gt
1651 ** TK_GE OP_Lt
1652 ** TK_LT OP_Ge
1653 **
1654 ** For other values of pExpr->op, op is undefined and unused.
1655 ** The value of TK_ and OP_ constants are arranged such that we
1656 ** can compute the mapping above using the following expression.
1657 ** Assert()s verify that the computation is correct.
1658 */
1659 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
1660
1661 /* Verify correct alignment of TK_ and OP_ constants
1662 */
1663 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
1664 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
1665 assert( pExpr->op!=TK_NE || op==OP_Eq );
1666 assert( pExpr->op!=TK_EQ || op==OP_Ne );
1667 assert( pExpr->op!=TK_LT || op==OP_Ge );
1668 assert( pExpr->op!=TK_LE || op==OP_Gt );
1669 assert( pExpr->op!=TK_GT || op==OP_Le );
1670 assert( pExpr->op!=TK_GE || op==OP_Lt );
1671
drhcce7d172000-05-31 15:34:51 +00001672 switch( pExpr->op ){
1673 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001674 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
1675 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001676 break;
1677 }
1678 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001679 int d2 = sqlite3VdbeMakeLabel(v);
1680 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
1681 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
1682 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001683 break;
1684 }
1685 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001686 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001687 break;
1688 }
1689 case TK_LT:
1690 case TK_LE:
1691 case TK_GT:
1692 case TK_GE:
1693 case TK_NE:
1694 case TK_EQ: {
danielk19774adee202004-05-08 08:23:19 +00001695 sqlite3ExprCode(pParse, pExpr->pLeft);
1696 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001697 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001698 break;
1699 }
drhcce7d172000-05-31 15:34:51 +00001700 case TK_ISNULL:
1701 case TK_NOTNULL: {
danielk19774adee202004-05-08 08:23:19 +00001702 sqlite3ExprCode(pParse, pExpr->pLeft);
1703 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001704 break;
1705 }
drhfef52082000-06-06 01:50:43 +00001706 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00001707 /* The expression is "x BETWEEN y AND z". It is implemented as:
1708 **
1709 ** 1 IF (x >= y) GOTO 3
1710 ** 2 GOTO <dest>
1711 ** 3 IF (x > z) GOTO <dest>
1712 */
drhfef52082000-06-06 01:50:43 +00001713 int addr;
drhbe5c89a2004-07-26 00:31:09 +00001714 Expr *pLeft = pExpr->pLeft;
1715 Expr *pRight = pExpr->pList->a[0].pExpr;
1716 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001717 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001718 sqlite3ExprCode(pParse, pRight);
danielk19774adee202004-05-08 08:23:19 +00001719 addr = sqlite3VdbeCurrentAddr(v);
drhbe5c89a2004-07-26 00:31:09 +00001720 codeCompare(pParse, pLeft, pRight, OP_Ge, addr+3, !jumpIfNull);
1721
danielk19774adee202004-05-08 08:23:19 +00001722 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
1723 sqlite3VdbeAddOp(v, OP_Goto, 0, dest);
drhbe5c89a2004-07-26 00:31:09 +00001724 pRight = pExpr->pList->a[1].pExpr;
1725 sqlite3ExprCode(pParse, pRight);
1726 codeCompare(pParse, pLeft, pRight, OP_Gt, dest, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00001727 break;
1728 }
drhcce7d172000-05-31 15:34:51 +00001729 default: {
danielk19774adee202004-05-08 08:23:19 +00001730 sqlite3ExprCode(pParse, pExpr);
1731 sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001732 break;
1733 }
1734 }
1735}
drh22827922000-06-06 17:27:05 +00001736
1737/*
1738** Do a deep comparison of two expression trees. Return TRUE (non-zero)
1739** if they are identical and return FALSE if they differ in any way.
1740*/
danielk19774adee202004-05-08 08:23:19 +00001741int sqlite3ExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00001742 int i;
1743 if( pA==0 ){
1744 return pB==0;
1745 }else if( pB==0 ){
1746 return 0;
1747 }
1748 if( pA->op!=pB->op ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001749 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
1750 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00001751 if( pA->pList ){
1752 if( pB->pList==0 ) return 0;
1753 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
1754 for(i=0; i<pA->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00001755 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00001756 return 0;
1757 }
1758 }
1759 }else if( pB->pList ){
1760 return 0;
1761 }
1762 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00001763 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drh22827922000-06-06 17:27:05 +00001764 if( pA->token.z ){
1765 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00001766 if( pB->token.n!=pA->token.n ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001767 if( sqlite3StrNICmp(pA->token.z, pB->token.z, pB->token.n)!=0 ) return 0;
drh22827922000-06-06 17:27:05 +00001768 }
1769 return 1;
1770}
1771
1772/*
1773** Add a new element to the pParse->aAgg[] array and return its index.
1774*/
1775static int appendAggInfo(Parse *pParse){
1776 if( (pParse->nAgg & 0x7)==0 ){
1777 int amt = pParse->nAgg + 8;
drh6d4abfb2001-10-22 02:58:08 +00001778 AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0]));
1779 if( aAgg==0 ){
drh22827922000-06-06 17:27:05 +00001780 return -1;
1781 }
drh6d4abfb2001-10-22 02:58:08 +00001782 pParse->aAgg = aAgg;
drh22827922000-06-06 17:27:05 +00001783 }
1784 memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0]));
1785 return pParse->nAgg++;
1786}
1787
1788/*
1789** Analyze the given expression looking for aggregate functions and
1790** for variables that need to be added to the pParse->aAgg[] array.
1791** Make additional entries to the pParse->aAgg[] array as necessary.
1792**
1793** This routine should only be called after the expression has been
danielk19774adee202004-05-08 08:23:19 +00001794** analyzed by sqlite3ExprResolveIds() and sqlite3ExprCheck().
drh22827922000-06-06 17:27:05 +00001795**
1796** If errors are seen, leave an error message in zErrMsg and return
1797** the number of errors.
1798*/
danielk19774adee202004-05-08 08:23:19 +00001799int sqlite3ExprAnalyzeAggregates(Parse *pParse, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00001800 int i;
1801 AggExpr *aAgg;
1802 int nErr = 0;
1803
1804 if( pExpr==0 ) return 0;
1805 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00001806 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +00001807 aAgg = pParse->aAgg;
1808 for(i=0; i<pParse->nAgg; i++){
1809 if( aAgg[i].isAgg ) continue;
1810 if( aAgg[i].pExpr->iTable==pExpr->iTable
drh967e8b72000-06-21 13:59:10 +00001811 && aAgg[i].pExpr->iColumn==pExpr->iColumn ){
drh22827922000-06-06 17:27:05 +00001812 break;
1813 }
1814 }
1815 if( i>=pParse->nAgg ){
1816 i = appendAggInfo(pParse);
1817 if( i<0 ) return 1;
1818 pParse->aAgg[i].isAgg = 0;
1819 pParse->aAgg[i].pExpr = pExpr;
1820 }
drhaaf88722000-06-08 11:25:00 +00001821 pExpr->iAgg = i;
drh22827922000-06-06 17:27:05 +00001822 break;
1823 }
1824 case TK_AGG_FUNCTION: {
drh22827922000-06-06 17:27:05 +00001825 aAgg = pParse->aAgg;
1826 for(i=0; i<pParse->nAgg; i++){
1827 if( !aAgg[i].isAgg ) continue;
danielk19774adee202004-05-08 08:23:19 +00001828 if( sqlite3ExprCompare(aAgg[i].pExpr, pExpr) ){
drh22827922000-06-06 17:27:05 +00001829 break;
1830 }
1831 }
1832 if( i>=pParse->nAgg ){
danielk1977d8123362004-06-12 09:25:12 +00001833 u8 enc = pParse->db->enc;
drh22827922000-06-06 17:27:05 +00001834 i = appendAggInfo(pParse);
1835 if( i<0 ) return 1;
1836 pParse->aAgg[i].isAgg = 1;
1837 pParse->aAgg[i].pExpr = pExpr;
danielk19774adee202004-05-08 08:23:19 +00001838 pParse->aAgg[i].pFunc = sqlite3FindFunction(pParse->db,
drh6977fea2002-10-22 23:38:04 +00001839 pExpr->token.z, pExpr->token.n,
danielk1977d8123362004-06-12 09:25:12 +00001840 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
drh22827922000-06-06 17:27:05 +00001841 }
1842 pExpr->iAgg = i;
1843 break;
1844 }
1845 default: {
1846 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001847 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pLeft);
drh22827922000-06-06 17:27:05 +00001848 }
1849 if( nErr==0 && pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001850 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pRight);
drh22827922000-06-06 17:27:05 +00001851 }
1852 if( nErr==0 && pExpr->pList ){
1853 int n = pExpr->pList->nExpr;
1854 int i;
1855 for(i=0; nErr==0 && i<n; i++){
danielk19774adee202004-05-08 08:23:19 +00001856 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pList->a[i].pExpr);
drh22827922000-06-06 17:27:05 +00001857 }
1858 }
1859 break;
1860 }
1861 }
1862 return nErr;
1863}
drh8e0a2f92002-02-23 23:45:45 +00001864
1865/*
danielk1977d02eb1f2004-06-06 09:44:03 +00001866** Locate a user function given a name, a number of arguments and a flag
1867** indicating whether the function prefers UTF-16 over UTF-8. Return a
1868** pointer to the FuncDef structure that defines that function, or return
1869** NULL if the function does not exist.
drh8e0a2f92002-02-23 23:45:45 +00001870**
drh0bce8352002-02-28 00:41:10 +00001871** If the createFlag argument is true, then a new (blank) FuncDef
drh8e0a2f92002-02-23 23:45:45 +00001872** structure is created and liked into the "db" structure if a
1873** no matching function previously existed. When createFlag is true
1874** and the nArg parameter is -1, then only a function that accepts
1875** any number of arguments will be returned.
1876**
1877** If createFlag is false and nArg is -1, then the first valid
1878** function found is returned. A function is valid if either xFunc
1879** or xStep is non-zero.
danielk1977d02eb1f2004-06-06 09:44:03 +00001880**
1881** If createFlag is false, then a function with the required name and
1882** number of arguments may be returned even if the eTextRep flag does not
1883** match that requested.
drh8e0a2f92002-02-23 23:45:45 +00001884*/
danielk19774adee202004-05-08 08:23:19 +00001885FuncDef *sqlite3FindFunction(
drh9bb575f2004-09-06 17:24:11 +00001886 sqlite3 *db, /* An open database */
drh8e0a2f92002-02-23 23:45:45 +00001887 const char *zName, /* Name of the function. Not null-terminated */
1888 int nName, /* Number of characters in the name */
1889 int nArg, /* Number of arguments. -1 means any number */
danielk1977d8123362004-06-12 09:25:12 +00001890 u8 enc, /* Preferred text encoding */
drh8e0a2f92002-02-23 23:45:45 +00001891 int createFlag /* Create new entry if true and does not otherwise exist */
1892){
danielk1977d02eb1f2004-06-06 09:44:03 +00001893 FuncDef *p; /* Iterator variable */
1894 FuncDef *pFirst; /* First function with this name */
1895 FuncDef *pBest = 0; /* Best match found so far */
danielk1977d8123362004-06-12 09:25:12 +00001896 int bestmatch = 0;
danielk1977d02eb1f2004-06-06 09:44:03 +00001897
danielk1977d8123362004-06-12 09:25:12 +00001898
1899 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
danielk1977d02eb1f2004-06-06 09:44:03 +00001900 if( nArg<-1 ) nArg = -1;
1901
1902 pFirst = (FuncDef*)sqlite3HashFind(&db->aFunc, zName, nName);
1903 for(p=pFirst; p; p=p->pNext){
danielk1977d8123362004-06-12 09:25:12 +00001904 /* During the search for the best function definition, bestmatch is set
1905 ** as follows to indicate the quality of the match with the definition
1906 ** pointed to by pBest:
1907 **
1908 ** 0: pBest is NULL. No match has been found.
1909 ** 1: A variable arguments function that prefers UTF-8 when a UTF-16
1910 ** encoding is requested, or vice versa.
1911 ** 2: A variable arguments function that uses UTF-16BE when UTF-16LE is
1912 ** requested, or vice versa.
1913 ** 3: A variable arguments function using the same text encoding.
1914 ** 4: A function with the exact number of arguments requested that
1915 ** prefers UTF-8 when a UTF-16 encoding is requested, or vice versa.
1916 ** 5: A function with the exact number of arguments requested that
1917 ** prefers UTF-16LE when UTF-16BE is requested, or vice versa.
1918 ** 6: An exact match.
1919 **
1920 ** A larger value of 'matchqual' indicates a more desirable match.
1921 */
danielk1977e12c17b2004-06-23 12:35:14 +00001922 if( p->nArg==-1 || p->nArg==nArg || nArg==-1 ){
danielk1977d8123362004-06-12 09:25:12 +00001923 int match = 1; /* Quality of this match */
1924 if( p->nArg==nArg || nArg==-1 ){
1925 match = 4;
danielk1977d02eb1f2004-06-06 09:44:03 +00001926 }
danielk1977d8123362004-06-12 09:25:12 +00001927 if( enc==p->iPrefEnc ){
1928 match += 2;
danielk1977d02eb1f2004-06-06 09:44:03 +00001929 }
danielk1977d8123362004-06-12 09:25:12 +00001930 else if( (enc==SQLITE_UTF16LE && p->iPrefEnc==SQLITE_UTF16BE) ||
1931 (enc==SQLITE_UTF16BE && p->iPrefEnc==SQLITE_UTF16LE) ){
1932 match += 1;
1933 }
1934
1935 if( match>bestmatch ){
1936 pBest = p;
1937 bestmatch = match;
danielk1977d02eb1f2004-06-06 09:44:03 +00001938 }
1939 }
drh8e0a2f92002-02-23 23:45:45 +00001940 }
danielk1977d02eb1f2004-06-06 09:44:03 +00001941
danielk1977d8123362004-06-12 09:25:12 +00001942 /* If the createFlag parameter is true, and the seach did not reveal an
1943 ** exact match for the name, number of arguments and encoding, then add a
1944 ** new entry to the hash table and return it.
1945 */
1946 if( createFlag && bestmatch<6 &&
danielk1977d02eb1f2004-06-06 09:44:03 +00001947 (pBest = sqliteMalloc(sizeof(*pBest)+nName+1)) ){
1948 pBest->nArg = nArg;
1949 pBest->pNext = pFirst;
1950 pBest->zName = (char*)&pBest[1];
danielk1977d8123362004-06-12 09:25:12 +00001951 pBest->iPrefEnc = enc;
danielk1977d02eb1f2004-06-06 09:44:03 +00001952 memcpy(pBest->zName, zName, nName);
1953 pBest->zName[nName] = 0;
1954 sqlite3HashInsert(&db->aFunc, pBest->zName, nName, (void*)pBest);
drh8e0a2f92002-02-23 23:45:45 +00001955 }
danielk1977d02eb1f2004-06-06 09:44:03 +00001956
1957 if( pBest && (pBest->xStep || pBest->xFunc || createFlag) ){
1958 return pBest;
drh8e0a2f92002-02-23 23:45:45 +00001959 }
danielk1977d02eb1f2004-06-06 09:44:03 +00001960 return 0;
drh8e0a2f92002-02-23 23:45:45 +00001961}