blob: 3d3655d7eebaaf5a17bf63674d746dbccdff040b [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**
drh5d9a4af2005-08-30 00:54:01 +000015** $Id: expr.c,v 1.222 2005/08/30 00:54:02 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){
drh487e2622005-06-25 18:42:14 +000037 int op = pExpr->op;
38 if( op==TK_AS ){
danielk1977bf3b7212004-05-18 10:06:24 +000039 return sqlite3ExprAffinity(pExpr->pLeft);
danielk1977a37cdde2004-05-16 11:15:36 +000040 }
drh487e2622005-06-25 18:42:14 +000041 if( op==TK_SELECT ){
danielk1977bf3b7212004-05-18 10:06:24 +000042 return sqlite3ExprAffinity(pExpr->pSelect->pEList->a[0].pExpr);
danielk1977a37cdde2004-05-16 11:15:36 +000043 }
drh487e2622005-06-25 18:42:14 +000044#ifndef SQLITE_OMIT_CAST
45 if( op==TK_CAST ){
46 return sqlite3AffinityType(&pExpr->token);
47 }
48#endif
danielk1977a37cdde2004-05-16 11:15:36 +000049 return pExpr->affinity;
50}
51
drh53db1452004-05-20 13:54:53 +000052/*
danielk19770202b292004-06-09 09:55:16 +000053** Return the default collation sequence for the expression pExpr. If
54** there is no default collation type, return 0.
55*/
danielk19777cedc8d2004-06-10 10:50:08 +000056CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
57 CollSeq *pColl = 0;
danielk19770202b292004-06-09 09:55:16 +000058 if( pExpr ){
danielk19777cedc8d2004-06-10 10:50:08 +000059 pColl = pExpr->pColl;
drh487e2622005-06-25 18:42:14 +000060 if( (pExpr->op==TK_AS || pExpr->op==TK_CAST) && !pColl ){
danielk19777cedc8d2004-06-10 10:50:08 +000061 return sqlite3ExprCollSeq(pParse, pExpr->pLeft);
danielk19770202b292004-06-09 09:55:16 +000062 }
63 }
danielk19777cedc8d2004-06-10 10:50:08 +000064 if( sqlite3CheckCollSeq(pParse, pColl) ){
65 pColl = 0;
66 }
67 return pColl;
danielk19770202b292004-06-09 09:55:16 +000068}
69
70/*
drh626a8792005-01-17 22:08:19 +000071** pExpr is an operand of a comparison operator. aff2 is the
72** type affinity of the other operand. This routine returns the
drh53db1452004-05-20 13:54:53 +000073** type affinity that should be used for the comparison operator.
74*/
danielk1977e014a832004-05-17 10:48:57 +000075char sqlite3CompareAffinity(Expr *pExpr, char aff2){
danielk1977bf3b7212004-05-18 10:06:24 +000076 char aff1 = sqlite3ExprAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +000077 if( aff1 && aff2 ){
78 /* Both sides of the comparison are columns. If one has numeric or
79 ** integer affinity, use that. Otherwise use no affinity.
80 */
81 if( aff1==SQLITE_AFF_INTEGER || aff2==SQLITE_AFF_INTEGER ){
82 return SQLITE_AFF_INTEGER;
83 }else if( aff1==SQLITE_AFF_NUMERIC || aff2==SQLITE_AFF_NUMERIC ){
84 return SQLITE_AFF_NUMERIC;
85 }else{
86 return SQLITE_AFF_NONE;
87 }
88 }else if( !aff1 && !aff2 ){
drh5f6a87b2004-07-19 00:39:45 +000089 /* Neither side of the comparison is a column. Compare the
90 ** results directly.
danielk1977e014a832004-05-17 10:48:57 +000091 */
drh5f6a87b2004-07-19 00:39:45 +000092 /* return SQLITE_AFF_NUMERIC; // Ticket #805 */
93 return SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +000094 }else{
95 /* One side is a column, the other is not. Use the columns affinity. */
drhfe05af82005-07-21 03:14:59 +000096 assert( aff1==0 || aff2==0 );
danielk1977e014a832004-05-17 10:48:57 +000097 return (aff1 + aff2);
98 }
99}
100
drh53db1452004-05-20 13:54:53 +0000101/*
102** pExpr is a comparison operator. Return the type affinity that should
103** be applied to both operands prior to doing the comparison.
104*/
danielk1977e014a832004-05-17 10:48:57 +0000105static char comparisonAffinity(Expr *pExpr){
106 char aff;
107 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
108 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
109 pExpr->op==TK_NE );
110 assert( pExpr->pLeft );
danielk1977bf3b7212004-05-18 10:06:24 +0000111 aff = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +0000112 if( pExpr->pRight ){
113 aff = sqlite3CompareAffinity(pExpr->pRight, aff);
114 }
115 else if( pExpr->pSelect ){
116 aff = sqlite3CompareAffinity(pExpr->pSelect->pEList->a[0].pExpr, aff);
117 }
118 else if( !aff ){
119 aff = SQLITE_AFF_NUMERIC;
120 }
121 return aff;
122}
123
124/*
125** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
126** idx_affinity is the affinity of an indexed column. Return true
127** if the index with affinity idx_affinity may be used to implement
128** the comparison in pExpr.
129*/
130int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){
131 char aff = comparisonAffinity(pExpr);
132 return
133 (aff==SQLITE_AFF_NONE) ||
134 (aff==SQLITE_AFF_NUMERIC && idx_affinity==SQLITE_AFF_INTEGER) ||
135 (aff==SQLITE_AFF_INTEGER && idx_affinity==SQLITE_AFF_NUMERIC) ||
136 (aff==idx_affinity);
137}
138
danielk1977a37cdde2004-05-16 11:15:36 +0000139/*
140** Return the P1 value that should be used for a binary comparison
141** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
142** If jumpIfNull is true, then set the low byte of the returned
143** P1 value to tell the opcode to jump if either expression
144** evaluates to NULL.
145*/
danielk1977e014a832004-05-17 10:48:57 +0000146static int binaryCompareP1(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){
danielk1977bf3b7212004-05-18 10:06:24 +0000147 char aff = sqlite3ExprAffinity(pExpr2);
drhf0863fe2005-06-12 21:35:51 +0000148 return ((int)sqlite3CompareAffinity(pExpr1, aff))+(jumpIfNull?0x100:0);
danielk1977a37cdde2004-05-16 11:15:36 +0000149}
150
drha2e00042002-01-22 03:13:42 +0000151/*
danielk19770202b292004-06-09 09:55:16 +0000152** Return a pointer to the collation sequence that should be used by
153** a binary comparison operator comparing pLeft and pRight.
154**
155** If the left hand expression has a collating sequence type, then it is
156** used. Otherwise the collation sequence for the right hand expression
157** is used, or the default (BINARY) if neither expression has a collating
158** type.
159*/
danielk19777cedc8d2004-06-10 10:50:08 +0000160static CollSeq* binaryCompareCollSeq(Parse *pParse, Expr *pLeft, Expr *pRight){
161 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pLeft);
danielk19770202b292004-06-09 09:55:16 +0000162 if( !pColl ){
danielk19777cedc8d2004-06-10 10:50:08 +0000163 pColl = sqlite3ExprCollSeq(pParse, pRight);
danielk19770202b292004-06-09 09:55:16 +0000164 }
165 return pColl;
166}
167
168/*
drhbe5c89a2004-07-26 00:31:09 +0000169** Generate code for a comparison operator.
170*/
171static int codeCompare(
172 Parse *pParse, /* The parsing (and code generating) context */
173 Expr *pLeft, /* The left operand */
174 Expr *pRight, /* The right operand */
175 int opcode, /* The comparison opcode */
176 int dest, /* Jump here if true. */
177 int jumpIfNull /* If true, jump if either operand is NULL */
178){
179 int p1 = binaryCompareP1(pLeft, pRight, jumpIfNull);
180 CollSeq *p3 = binaryCompareCollSeq(pParse, pLeft, pRight);
drh94a11212004-09-25 13:12:14 +0000181 return sqlite3VdbeOp3(pParse->pVdbe, opcode, p1, dest, (void*)p3, P3_COLLSEQ);
drhbe5c89a2004-07-26 00:31:09 +0000182}
183
184/*
drha76b5df2002-02-23 02:32:10 +0000185** Construct a new expression node and return a pointer to it. Memory
186** for this node is obtained from sqliteMalloc(). The calling function
187** is responsible for making sure the node eventually gets freed.
188*/
drhe4e72072004-11-23 01:47:30 +0000189Expr *sqlite3Expr(int op, Expr *pLeft, Expr *pRight, const Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000190 Expr *pNew;
191 pNew = sqliteMalloc( sizeof(Expr) );
192 if( pNew==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000193 /* When malloc fails, delete pLeft and pRight. Expressions passed to
194 ** this function must always be allocated with sqlite3Expr() for this
195 ** reason.
196 */
197 sqlite3ExprDelete(pLeft);
198 sqlite3ExprDelete(pRight);
drha76b5df2002-02-23 02:32:10 +0000199 return 0;
200 }
201 pNew->op = op;
202 pNew->pLeft = pLeft;
203 pNew->pRight = pRight;
danielk1977a58fdfb2005-02-08 07:50:40 +0000204 pNew->iAgg = -1;
drha76b5df2002-02-23 02:32:10 +0000205 if( pToken ){
drh4b59ab52002-08-24 18:24:51 +0000206 assert( pToken->dyn==0 );
drh145716b2004-09-24 12:24:06 +0000207 pNew->span = pNew->token = *pToken;
208 }else if( pLeft && pRight ){
209 sqlite3ExprSpan(pNew, &pLeft->span, &pRight->span);
drha76b5df2002-02-23 02:32:10 +0000210 }
drha76b5df2002-02-23 02:32:10 +0000211 return pNew;
212}
213
214/*
drh4e0cff62004-11-05 05:10:28 +0000215** When doing a nested parse, you can include terms in an expression
216** that look like this: #0 #1 #2 ... These terms refer to elements
drh288d37f2005-06-22 08:48:06 +0000217** on the stack. "#0" means the top of the stack.
218** "#1" means the next down on the stack. And so forth.
drh4e0cff62004-11-05 05:10:28 +0000219**
220** This routine is called by the parser to deal with on of those terms.
221** It immediately generates code to store the value in a memory location.
222** The returns an expression that will code to extract the value from
223** that memory location as needed.
224*/
225Expr *sqlite3RegisterExpr(Parse *pParse, Token *pToken){
226 Vdbe *v = pParse->pVdbe;
227 Expr *p;
228 int depth;
drh4e0cff62004-11-05 05:10:28 +0000229 if( pParse->nested==0 ){
230 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", pToken);
231 return 0;
232 }
drhbb7ac002005-08-12 22:58:53 +0000233 if( v==0 ) return 0;
drh4e0cff62004-11-05 05:10:28 +0000234 p = sqlite3Expr(TK_REGISTER, 0, 0, pToken);
drh73c42a12004-11-20 18:13:10 +0000235 if( p==0 ){
236 return 0; /* Malloc failed */
237 }
drh4e0cff62004-11-05 05:10:28 +0000238 depth = atoi(&pToken->z[1]);
drh288d37f2005-06-22 08:48:06 +0000239 p->iTable = pParse->nMem++;
240 sqlite3VdbeAddOp(v, OP_Dup, depth, 0);
241 sqlite3VdbeAddOp(v, OP_MemStore, p->iTable, 1);
drh4e0cff62004-11-05 05:10:28 +0000242 return p;
243}
244
245/*
drh91bb0ee2004-09-01 03:06:34 +0000246** Join two expressions using an AND operator. If either expression is
247** NULL, then just return the other expression.
248*/
249Expr *sqlite3ExprAnd(Expr *pLeft, Expr *pRight){
250 if( pLeft==0 ){
251 return pRight;
252 }else if( pRight==0 ){
253 return pLeft;
254 }else{
255 return sqlite3Expr(TK_AND, pLeft, pRight, 0);
256 }
257}
258
259/*
drh6977fea2002-10-22 23:38:04 +0000260** Set the Expr.span field of the given expression to span all
drha76b5df2002-02-23 02:32:10 +0000261** text between the two given tokens.
262*/
danielk19774adee202004-05-08 08:23:19 +0000263void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
drh4efc4752004-01-16 15:55:37 +0000264 assert( pRight!=0 );
265 assert( pLeft!=0 );
drh71c697e2004-08-08 23:39:19 +0000266 if( !sqlite3_malloc_failed && pRight->z && pLeft->z ){
drhad6d9462004-09-19 02:15:24 +0000267 assert( pLeft->dyn==0 || pLeft->z[pLeft->n]==0 );
drh145716b2004-09-24 12:24:06 +0000268 if( pLeft->dyn==0 && pRight->dyn==0 ){
drh6977fea2002-10-22 23:38:04 +0000269 pExpr->span.z = pLeft->z;
drh97903fe2005-05-24 20:19:57 +0000270 pExpr->span.n = pRight->n + (pRight->z - pLeft->z);
drh4b59ab52002-08-24 18:24:51 +0000271 }else{
drh6977fea2002-10-22 23:38:04 +0000272 pExpr->span.z = 0;
drh4b59ab52002-08-24 18:24:51 +0000273 }
drha76b5df2002-02-23 02:32:10 +0000274 }
275}
276
277/*
278** Construct a new expression node for a function with multiple
279** arguments.
280*/
danielk19774adee202004-05-08 08:23:19 +0000281Expr *sqlite3ExprFunction(ExprList *pList, Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000282 Expr *pNew;
283 pNew = sqliteMalloc( sizeof(Expr) );
284 if( pNew==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000285 sqlite3ExprListDelete(pList); /* Avoid leaking memory when malloc fails */
drha76b5df2002-02-23 02:32:10 +0000286 return 0;
287 }
288 pNew->op = TK_FUNCTION;
289 pNew->pList = pList;
290 if( pToken ){
drh4b59ab52002-08-24 18:24:51 +0000291 assert( pToken->dyn==0 );
drha76b5df2002-02-23 02:32:10 +0000292 pNew->token = *pToken;
293 }else{
294 pNew->token.z = 0;
drha76b5df2002-02-23 02:32:10 +0000295 }
drh6977fea2002-10-22 23:38:04 +0000296 pNew->span = pNew->token;
drha76b5df2002-02-23 02:32:10 +0000297 return pNew;
298}
299
300/*
drhfa6bc002004-09-07 16:19:52 +0000301** Assign a variable number to an expression that encodes a wildcard
302** in the original SQL statement.
303**
304** Wildcards consisting of a single "?" are assigned the next sequential
305** variable number.
306**
307** Wildcards of the form "?nnn" are assigned the number "nnn". We make
308** sure "nnn" is not too be to avoid a denial of service attack when
309** the SQL statement comes from an external source.
310**
311** Wildcards of the form ":aaa" or "$aaa" are assigned the same number
312** as the previous instance of the same wildcard. Or if this is the first
313** instance of the wildcard, the next sequenial variable number is
314** assigned.
315*/
316void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){
317 Token *pToken;
318 if( pExpr==0 ) return;
319 pToken = &pExpr->token;
320 assert( pToken->n>=1 );
321 assert( pToken->z!=0 );
322 assert( pToken->z[0]!=0 );
323 if( pToken->n==1 ){
324 /* Wildcard of the form "?". Assign the next variable number */
325 pExpr->iTable = ++pParse->nVar;
326 }else if( pToken->z[0]=='?' ){
327 /* Wildcard of the form "?nnn". Convert "nnn" to an integer and
328 ** use it as the variable number */
329 int i;
330 pExpr->iTable = i = atoi(&pToken->z[1]);
331 if( i<1 || i>SQLITE_MAX_VARIABLE_NUMBER ){
332 sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
333 SQLITE_MAX_VARIABLE_NUMBER);
334 }
335 if( i>pParse->nVar ){
336 pParse->nVar = i;
337 }
338 }else{
339 /* Wildcards of the form ":aaa" or "$aaa". Reuse the same variable
340 ** number as the prior appearance of the same name, or if the name
341 ** has never appeared before, reuse the same variable number
342 */
343 int i, n;
344 n = pToken->n;
345 for(i=0; i<pParse->nVarExpr; i++){
346 Expr *pE;
347 if( (pE = pParse->apVarExpr[i])!=0
348 && pE->token.n==n
349 && memcmp(pE->token.z, pToken->z, n)==0 ){
350 pExpr->iTable = pE->iTable;
351 break;
352 }
353 }
354 if( i>=pParse->nVarExpr ){
355 pExpr->iTable = ++pParse->nVar;
356 if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){
357 pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10;
358 pParse->apVarExpr = sqliteRealloc(pParse->apVarExpr,
359 pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0]) );
360 }
361 if( !sqlite3_malloc_failed ){
362 assert( pParse->apVarExpr!=0 );
363 pParse->apVarExpr[pParse->nVarExpr++] = pExpr;
364 }
365 }
366 }
367}
368
369/*
drha2e00042002-01-22 03:13:42 +0000370** Recursively delete an expression tree.
371*/
danielk19774adee202004-05-08 08:23:19 +0000372void sqlite3ExprDelete(Expr *p){
drha2e00042002-01-22 03:13:42 +0000373 if( p==0 ) return;
drh4efc4752004-01-16 15:55:37 +0000374 if( p->span.dyn ) sqliteFree((char*)p->span.z);
375 if( p->token.dyn ) sqliteFree((char*)p->token.z);
danielk19774adee202004-05-08 08:23:19 +0000376 sqlite3ExprDelete(p->pLeft);
377 sqlite3ExprDelete(p->pRight);
378 sqlite3ExprListDelete(p->pList);
379 sqlite3SelectDelete(p->pSelect);
drha2e00042002-01-22 03:13:42 +0000380 sqliteFree(p);
381}
382
drhd2687b72005-08-12 22:56:09 +0000383/*
384** The Expr.token field might be a string literal that is quoted.
385** If so, remove the quotation marks.
386*/
387void sqlite3DequoteExpr(Expr *p){
388 if( ExprHasAnyProperty(p, EP_Dequoted) ){
389 return;
390 }
391 ExprSetProperty(p, EP_Dequoted);
392 if( p->token.dyn==0 ){
drhd2687b72005-08-12 22:56:09 +0000393 sqlite3TokenCopy(&p->token, &p->token);
394 }
395 sqlite3Dequote((char*)p->token.z);
396}
397
drha76b5df2002-02-23 02:32:10 +0000398
399/*
drhff78bd22002-02-27 01:47:11 +0000400** The following group of routines make deep copies of expressions,
401** expression lists, ID lists, and select statements. The copies can
402** be deleted (by being passed to their respective ...Delete() routines)
403** without effecting the originals.
404**
danielk19774adee202004-05-08 08:23:19 +0000405** The expression list, ID, and source lists return by sqlite3ExprListDup(),
406** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
drhad3cab52002-05-24 02:04:32 +0000407** by subsequent calls to sqlite*ListAppend() routines.
drhff78bd22002-02-27 01:47:11 +0000408**
drhad3cab52002-05-24 02:04:32 +0000409** Any tables that the SrcList might point to are not duplicated.
drhff78bd22002-02-27 01:47:11 +0000410*/
danielk19774adee202004-05-08 08:23:19 +0000411Expr *sqlite3ExprDup(Expr *p){
drhff78bd22002-02-27 01:47:11 +0000412 Expr *pNew;
413 if( p==0 ) return 0;
drhfcb78a42003-01-18 20:11:05 +0000414 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000415 if( pNew==0 ) return 0;
drh3b167c72002-06-28 12:18:47 +0000416 memcpy(pNew, p, sizeof(*pNew));
drh6977fea2002-10-22 23:38:04 +0000417 if( p->token.z!=0 ){
drhb9ecf6f2004-11-20 20:44:13 +0000418 pNew->token.z = sqliteStrNDup(p->token.z, p->token.n);
drh4b59ab52002-08-24 18:24:51 +0000419 pNew->token.dyn = 1;
420 }else{
drh4efc4752004-01-16 15:55:37 +0000421 assert( pNew->token.z==0 );
drh4b59ab52002-08-24 18:24:51 +0000422 }
drh6977fea2002-10-22 23:38:04 +0000423 pNew->span.z = 0;
danielk19774adee202004-05-08 08:23:19 +0000424 pNew->pLeft = sqlite3ExprDup(p->pLeft);
425 pNew->pRight = sqlite3ExprDup(p->pRight);
426 pNew->pList = sqlite3ExprListDup(p->pList);
427 pNew->pSelect = sqlite3SelectDup(p->pSelect);
danielk1977aee18ef2005-03-09 12:26:50 +0000428 pNew->pTab = p->pTab;
drhff78bd22002-02-27 01:47:11 +0000429 return pNew;
430}
danielk19774adee202004-05-08 08:23:19 +0000431void sqlite3TokenCopy(Token *pTo, Token *pFrom){
drh4b59ab52002-08-24 18:24:51 +0000432 if( pTo->dyn ) sqliteFree((char*)pTo->z);
drh4b59ab52002-08-24 18:24:51 +0000433 if( pFrom->z ){
434 pTo->n = pFrom->n;
435 pTo->z = sqliteStrNDup(pFrom->z, pFrom->n);
436 pTo->dyn = 1;
437 }else{
drh4b59ab52002-08-24 18:24:51 +0000438 pTo->z = 0;
drh4b59ab52002-08-24 18:24:51 +0000439 }
440}
danielk19774adee202004-05-08 08:23:19 +0000441ExprList *sqlite3ExprListDup(ExprList *p){
drhff78bd22002-02-27 01:47:11 +0000442 ExprList *pNew;
drh145716b2004-09-24 12:24:06 +0000443 struct ExprList_item *pItem, *pOldItem;
drhff78bd22002-02-27 01:47:11 +0000444 int i;
445 if( p==0 ) return 0;
446 pNew = sqliteMalloc( sizeof(*pNew) );
447 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000448 pNew->nExpr = pNew->nAlloc = p->nExpr;
drh3e7bc9c2004-02-21 19:17:17 +0000449 pNew->a = pItem = sqliteMalloc( p->nExpr*sizeof(p->a[0]) );
danielk1977e0048402004-06-15 16:51:01 +0000450 if( pItem==0 ){
451 sqliteFree(pNew);
452 return 0;
453 }
drh145716b2004-09-24 12:24:06 +0000454 pOldItem = p->a;
455 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
drh4b59ab52002-08-24 18:24:51 +0000456 Expr *pNewExpr, *pOldExpr;
drh145716b2004-09-24 12:24:06 +0000457 pItem->pExpr = pNewExpr = sqlite3ExprDup(pOldExpr = pOldItem->pExpr);
drh6977fea2002-10-22 23:38:04 +0000458 if( pOldExpr->span.z!=0 && pNewExpr ){
459 /* Always make a copy of the span for top-level expressions in the
drh4b59ab52002-08-24 18:24:51 +0000460 ** expression list. The logic in SELECT processing that determines
461 ** the names of columns in the result set needs this information */
danielk19774adee202004-05-08 08:23:19 +0000462 sqlite3TokenCopy(&pNewExpr->span, &pOldExpr->span);
drh4b59ab52002-08-24 18:24:51 +0000463 }
drh1f3e9052002-10-31 00:09:39 +0000464 assert( pNewExpr==0 || pNewExpr->span.z!=0
danielk197724b03fd2004-05-10 10:34:34 +0000465 || pOldExpr->span.z==0 || sqlite3_malloc_failed );
drh145716b2004-09-24 12:24:06 +0000466 pItem->zName = sqliteStrDup(pOldItem->zName);
467 pItem->sortOrder = pOldItem->sortOrder;
468 pItem->isAgg = pOldItem->isAgg;
drh3e7bc9c2004-02-21 19:17:17 +0000469 pItem->done = 0;
drhff78bd22002-02-27 01:47:11 +0000470 }
471 return pNew;
472}
danielk197793758c82005-01-21 08:13:14 +0000473
474/*
475** If cursors, triggers, views and subqueries are all omitted from
476** the build, then none of the following routines, except for
477** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes
478** called with a NULL argument.
479*/
danielk19776a67fe82005-02-04 04:07:16 +0000480#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \
481 || !defined(SQLITE_OMIT_SUBQUERY)
danielk19774adee202004-05-08 08:23:19 +0000482SrcList *sqlite3SrcListDup(SrcList *p){
drhad3cab52002-05-24 02:04:32 +0000483 SrcList *pNew;
484 int i;
drh113088e2003-03-20 01:16:58 +0000485 int nByte;
drhad3cab52002-05-24 02:04:32 +0000486 if( p==0 ) return 0;
drh113088e2003-03-20 01:16:58 +0000487 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
drh4efc4752004-01-16 15:55:37 +0000488 pNew = sqliteMallocRaw( nByte );
drhad3cab52002-05-24 02:04:32 +0000489 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000490 pNew->nSrc = pNew->nAlloc = p->nSrc;
drhad3cab52002-05-24 02:04:32 +0000491 for(i=0; i<p->nSrc; i++){
drh4efc4752004-01-16 15:55:37 +0000492 struct SrcList_item *pNewItem = &pNew->a[i];
493 struct SrcList_item *pOldItem = &p->a[i];
drhed8a3bb2005-06-06 21:19:56 +0000494 Table *pTab;
drh4efc4752004-01-16 15:55:37 +0000495 pNewItem->zDatabase = sqliteStrDup(pOldItem->zDatabase);
496 pNewItem->zName = sqliteStrDup(pOldItem->zName);
497 pNewItem->zAlias = sqliteStrDup(pOldItem->zAlias);
498 pNewItem->jointype = pOldItem->jointype;
499 pNewItem->iCursor = pOldItem->iCursor;
drhed8a3bb2005-06-06 21:19:56 +0000500 pTab = pNewItem->pTab = pOldItem->pTab;
501 if( pTab ){
502 pTab->nRef++;
danielk1977a1cb1832005-02-12 08:59:55 +0000503 }
danielk19774adee202004-05-08 08:23:19 +0000504 pNewItem->pSelect = sqlite3SelectDup(pOldItem->pSelect);
505 pNewItem->pOn = sqlite3ExprDup(pOldItem->pOn);
506 pNewItem->pUsing = sqlite3IdListDup(pOldItem->pUsing);
danielk19776c18b6e2005-01-30 09:17:58 +0000507 pNewItem->colUsed = pOldItem->colUsed;
drhad3cab52002-05-24 02:04:32 +0000508 }
509 return pNew;
510}
danielk19774adee202004-05-08 08:23:19 +0000511IdList *sqlite3IdListDup(IdList *p){
drhff78bd22002-02-27 01:47:11 +0000512 IdList *pNew;
513 int i;
514 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000515 pNew = sqliteMallocRaw( sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +0000516 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000517 pNew->nId = pNew->nAlloc = p->nId;
drh4efc4752004-01-16 15:55:37 +0000518 pNew->a = sqliteMallocRaw( p->nId*sizeof(p->a[0]) );
danielk1977d5d56522005-03-16 12:15:20 +0000519 if( pNew->a==0 ){
520 sqliteFree(pNew);
521 return 0;
522 }
drhff78bd22002-02-27 01:47:11 +0000523 for(i=0; i<p->nId; i++){
drh4efc4752004-01-16 15:55:37 +0000524 struct IdList_item *pNewItem = &pNew->a[i];
525 struct IdList_item *pOldItem = &p->a[i];
526 pNewItem->zName = sqliteStrDup(pOldItem->zName);
527 pNewItem->idx = pOldItem->idx;
drhff78bd22002-02-27 01:47:11 +0000528 }
529 return pNew;
530}
danielk19774adee202004-05-08 08:23:19 +0000531Select *sqlite3SelectDup(Select *p){
drhff78bd22002-02-27 01:47:11 +0000532 Select *pNew;
533 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000534 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000535 if( pNew==0 ) return 0;
536 pNew->isDistinct = p->isDistinct;
danielk19774adee202004-05-08 08:23:19 +0000537 pNew->pEList = sqlite3ExprListDup(p->pEList);
538 pNew->pSrc = sqlite3SrcListDup(p->pSrc);
539 pNew->pWhere = sqlite3ExprDup(p->pWhere);
540 pNew->pGroupBy = sqlite3ExprListDup(p->pGroupBy);
541 pNew->pHaving = sqlite3ExprDup(p->pHaving);
542 pNew->pOrderBy = sqlite3ExprListDup(p->pOrderBy);
drhff78bd22002-02-27 01:47:11 +0000543 pNew->op = p->op;
danielk19774adee202004-05-08 08:23:19 +0000544 pNew->pPrior = sqlite3SelectDup(p->pPrior);
danielk1977a2dc3b12005-02-05 12:48:48 +0000545 pNew->pLimit = sqlite3ExprDup(p->pLimit);
546 pNew->pOffset = sqlite3ExprDup(p->pOffset);
drh7b58dae2003-07-20 01:16:46 +0000547 pNew->iLimit = -1;
548 pNew->iOffset = -1;
drh9170dd72005-07-08 17:13:46 +0000549 pNew->ppOpenVirtual = 0;
danielk1977a1cb1832005-02-12 08:59:55 +0000550 pNew->isResolved = p->isResolved;
551 pNew->isAgg = p->isAgg;
drhff78bd22002-02-27 01:47:11 +0000552 return pNew;
553}
danielk197793758c82005-01-21 08:13:14 +0000554#else
555Select *sqlite3SelectDup(Select *p){
556 assert( p==0 );
557 return 0;
558}
559#endif
drhff78bd22002-02-27 01:47:11 +0000560
561
562/*
drha76b5df2002-02-23 02:32:10 +0000563** Add a new element to the end of an expression list. If pList is
564** initially NULL, then create a new expression list.
565*/
danielk19774adee202004-05-08 08:23:19 +0000566ExprList *sqlite3ExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){
drha76b5df2002-02-23 02:32:10 +0000567 if( pList==0 ){
568 pList = sqliteMalloc( sizeof(ExprList) );
569 if( pList==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000570 goto no_mem;
drha76b5df2002-02-23 02:32:10 +0000571 }
drh4efc4752004-01-16 15:55:37 +0000572 assert( pList->nAlloc==0 );
drha76b5df2002-02-23 02:32:10 +0000573 }
drh4305d102003-07-30 12:34:12 +0000574 if( pList->nAlloc<=pList->nExpr ){
danielk1977d5d56522005-03-16 12:15:20 +0000575 struct ExprList_item *a;
576 int n = pList->nAlloc*2 + 4;
577 a = sqliteRealloc(pList->a, n*sizeof(pList->a[0]));
578 if( a==0 ){
579 goto no_mem;
drha76b5df2002-02-23 02:32:10 +0000580 }
danielk1977d5d56522005-03-16 12:15:20 +0000581 pList->a = a;
582 pList->nAlloc = n;
drha76b5df2002-02-23 02:32:10 +0000583 }
drh4efc4752004-01-16 15:55:37 +0000584 assert( pList->a!=0 );
585 if( pExpr || pName ){
586 struct ExprList_item *pItem = &pList->a[pList->nExpr++];
587 memset(pItem, 0, sizeof(*pItem));
drha99db3b2004-06-19 14:49:12 +0000588 pItem->zName = sqlite3NameFromToken(pName);
danielk1977e94ddc92005-03-21 03:53:38 +0000589 pItem->pExpr = pExpr;
drha76b5df2002-02-23 02:32:10 +0000590 }
591 return pList;
danielk1977d5d56522005-03-16 12:15:20 +0000592
593no_mem:
594 /* Avoid leaking memory if malloc has failed. */
595 sqlite3ExprDelete(pExpr);
596 sqlite3ExprListDelete(pList);
597 return 0;
drha76b5df2002-02-23 02:32:10 +0000598}
599
600/*
601** Delete an entire expression list.
602*/
danielk19774adee202004-05-08 08:23:19 +0000603void sqlite3ExprListDelete(ExprList *pList){
drha76b5df2002-02-23 02:32:10 +0000604 int i;
drhbe5c89a2004-07-26 00:31:09 +0000605 struct ExprList_item *pItem;
drha76b5df2002-02-23 02:32:10 +0000606 if( pList==0 ) return;
drh1bdd9b52004-04-23 17:04:44 +0000607 assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
608 assert( pList->nExpr<=pList->nAlloc );
drhbe5c89a2004-07-26 00:31:09 +0000609 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
610 sqlite3ExprDelete(pItem->pExpr);
611 sqliteFree(pItem->zName);
drha76b5df2002-02-23 02:32:10 +0000612 }
613 sqliteFree(pList->a);
614 sqliteFree(pList);
615}
616
617/*
drh626a8792005-01-17 22:08:19 +0000618** Walk an expression tree. Call xFunc for each node visited.
drh73b211a2005-01-18 04:00:42 +0000619**
drh626a8792005-01-17 22:08:19 +0000620** The return value from xFunc determines whether the tree walk continues.
621** 0 means continue walking the tree. 1 means do not walk children
622** of the current node but continue with siblings. 2 means abandon
623** the tree walk completely.
624**
625** The return value from this routine is 1 to abandon the tree walk
626** and 0 to continue.
drh87abf5c2005-08-25 12:45:04 +0000627**
628** NOTICE: This routine does *not* descend into subqueries.
drh626a8792005-01-17 22:08:19 +0000629*/
danielk1977a58fdfb2005-02-08 07:50:40 +0000630static int walkExprList(ExprList *, int (*)(void *, Expr*), void *);
drh626a8792005-01-17 22:08:19 +0000631static int walkExprTree(Expr *pExpr, int (*xFunc)(void*,Expr*), void *pArg){
drh626a8792005-01-17 22:08:19 +0000632 int rc;
633 if( pExpr==0 ) return 0;
634 rc = (*xFunc)(pArg, pExpr);
635 if( rc==0 ){
636 if( walkExprTree(pExpr->pLeft, xFunc, pArg) ) return 1;
637 if( walkExprTree(pExpr->pRight, xFunc, pArg) ) return 1;
danielk1977a58fdfb2005-02-08 07:50:40 +0000638 if( walkExprList(pExpr->pList, xFunc, pArg) ) return 1;
drh626a8792005-01-17 22:08:19 +0000639 }
640 return rc>1;
641}
642
643/*
danielk1977a58fdfb2005-02-08 07:50:40 +0000644** Call walkExprTree() for every expression in list p.
645*/
646static int walkExprList(ExprList *p, int (*xFunc)(void *, Expr*), void *pArg){
647 int i;
648 struct ExprList_item *pItem;
649 if( !p ) return 0;
650 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
651 if( walkExprTree(pItem->pExpr, xFunc, pArg) ) return 1;
652 }
653 return 0;
654}
655
656/*
657** Call walkExprTree() for every expression in Select p, not including
658** expressions that are part of sub-selects in any FROM clause or the LIMIT
659** or OFFSET expressions..
660*/
661static int walkSelectExpr(Select *p, int (*xFunc)(void *, Expr*), void *pArg){
662 walkExprList(p->pEList, xFunc, pArg);
663 walkExprTree(p->pWhere, xFunc, pArg);
664 walkExprList(p->pGroupBy, xFunc, pArg);
665 walkExprTree(p->pHaving, xFunc, pArg);
666 walkExprList(p->pOrderBy, xFunc, pArg);
667 return 0;
668}
669
670
671/*
drh626a8792005-01-17 22:08:19 +0000672** This routine is designed as an xFunc for walkExprTree().
673**
674** pArg is really a pointer to an integer. If we can tell by looking
drh73b211a2005-01-18 04:00:42 +0000675** at pExpr that the expression that contains pExpr is not a constant
676** expression, then set *pArg to 0 and return 2 to abandon the tree walk.
677** If pExpr does does not disqualify the expression from being a constant
678** then do nothing.
679**
680** After walking the whole tree, if no nodes are found that disqualify
681** the expression as constant, then we assume the whole expression
682** is constant. See sqlite3ExprIsConstant() for additional information.
drh626a8792005-01-17 22:08:19 +0000683*/
684static int exprNodeIsConstant(void *pArg, Expr *pExpr){
685 switch( pExpr->op ){
drheb55bd22005-06-30 17:04:21 +0000686 /* Consider functions to be constant if all their arguments are constant
687 ** and *pArg==2 */
688 case TK_FUNCTION:
689 if( *((int*)pArg)==2 ) return 0;
690 /* Fall through */
drh626a8792005-01-17 22:08:19 +0000691 case TK_ID:
692 case TK_COLUMN:
693 case TK_DOT:
694 case TK_AGG_FUNCTION:
drhfe2093d2005-01-20 22:48:47 +0000695#ifndef SQLITE_OMIT_SUBQUERY
696 case TK_SELECT:
697 case TK_EXISTS:
698#endif
drh626a8792005-01-17 22:08:19 +0000699 *((int*)pArg) = 0;
700 return 2;
drh87abf5c2005-08-25 12:45:04 +0000701 case TK_IN:
702 if( pExpr->pSelect ){
703 *((int*)pArg) = 0;
704 return 2;
705 }
drh626a8792005-01-17 22:08:19 +0000706 default:
707 return 0;
708 }
709}
710
711/*
drhfef52082000-06-06 01:50:43 +0000712** Walk an expression tree. Return 1 if the expression is constant
drheb55bd22005-06-30 17:04:21 +0000713** and 0 if it involves variables or function calls.
drh23989372002-05-21 13:43:04 +0000714**
715** For the purposes of this function, a double-quoted string (ex: "abc")
716** is considered a variable but a single-quoted string (ex: 'abc') is
717** a constant.
drhfef52082000-06-06 01:50:43 +0000718*/
danielk19774adee202004-05-08 08:23:19 +0000719int sqlite3ExprIsConstant(Expr *p){
drh626a8792005-01-17 22:08:19 +0000720 int isConst = 1;
721 walkExprTree(p, exprNodeIsConstant, &isConst);
722 return isConst;
drhfef52082000-06-06 01:50:43 +0000723}
724
725/*
drheb55bd22005-06-30 17:04:21 +0000726** Walk an expression tree. Return 1 if the expression is constant
727** or a function call with constant arguments. Return and 0 if there
728** are any variables.
729**
730** For the purposes of this function, a double-quoted string (ex: "abc")
731** is considered a variable but a single-quoted string (ex: 'abc') is
732** a constant.
733*/
734int sqlite3ExprIsConstantOrFunction(Expr *p){
735 int isConst = 2;
736 walkExprTree(p, exprNodeIsConstant, &isConst);
737 return isConst!=0;
738}
739
740/*
drh73b211a2005-01-18 04:00:42 +0000741** If the expression p codes a constant integer that is small enough
drh202b2df2004-01-06 01:13:46 +0000742** to fit in a 32-bit integer, return 1 and put the value of the integer
743** in *pValue. If the expression is not an integer or if it is too big
744** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
drhe4de1fe2002-06-02 16:09:01 +0000745*/
danielk19774adee202004-05-08 08:23:19 +0000746int sqlite3ExprIsInteger(Expr *p, int *pValue){
drhe4de1fe2002-06-02 16:09:01 +0000747 switch( p->op ){
748 case TK_INTEGER: {
drhfec19aa2004-05-19 20:41:03 +0000749 if( sqlite3GetInt32(p->token.z, pValue) ){
drh202b2df2004-01-06 01:13:46 +0000750 return 1;
751 }
752 break;
drhe4de1fe2002-06-02 16:09:01 +0000753 }
drh4b59ab52002-08-24 18:24:51 +0000754 case TK_UPLUS: {
danielk19774adee202004-05-08 08:23:19 +0000755 return sqlite3ExprIsInteger(p->pLeft, pValue);
drh4b59ab52002-08-24 18:24:51 +0000756 }
drhe4de1fe2002-06-02 16:09:01 +0000757 case TK_UMINUS: {
758 int v;
danielk19774adee202004-05-08 08:23:19 +0000759 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
drhe4de1fe2002-06-02 16:09:01 +0000760 *pValue = -v;
761 return 1;
762 }
763 break;
764 }
765 default: break;
766 }
767 return 0;
768}
769
770/*
drhc4a3c772001-04-04 11:48:57 +0000771** Return TRUE if the given string is a row-id column name.
772*/
danielk19774adee202004-05-08 08:23:19 +0000773int sqlite3IsRowid(const char *z){
774 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
775 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
776 if( sqlite3StrICmp(z, "OID")==0 ) return 1;
drhc4a3c772001-04-04 11:48:57 +0000777 return 0;
778}
779
780/*
drh8141f612004-01-25 22:44:58 +0000781** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
782** that name in the set of source tables in pSrcList and make the pExpr
783** expression node refer back to that source column. The following changes
784** are made to pExpr:
785**
786** pExpr->iDb Set the index in db->aDb[] of the database holding
787** the table.
788** pExpr->iTable Set to the cursor number for the table obtained
789** from pSrcList.
790** pExpr->iColumn Set to the column number within the table.
drh8141f612004-01-25 22:44:58 +0000791** pExpr->op Set to TK_COLUMN.
792** pExpr->pLeft Any expression this points to is deleted
793** pExpr->pRight Any expression this points to is deleted.
794**
795** The pDbToken is the name of the database (the "X"). This value may be
796** NULL meaning that name is of the form Y.Z or Z. Any available database
797** can be used. The pTableToken is the name of the table (the "Y"). This
798** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it
799** means that the form of the name is Z and that columns from any table
800** can be used.
801**
802** If the name cannot be resolved unambiguously, leave an error message
803** in pParse and return non-zero. Return zero on success.
804*/
805static int lookupName(
806 Parse *pParse, /* The parsing context */
807 Token *pDbToken, /* Name of the database containing table, or NULL */
808 Token *pTableToken, /* Name of table containing column, or NULL */
809 Token *pColumnToken, /* Name of the column. */
drh626a8792005-01-17 22:08:19 +0000810 NameContext *pNC, /* The name context used to resolve the name */
drh8141f612004-01-25 22:44:58 +0000811 Expr *pExpr /* Make this EXPR node point to the selected column */
812){
813 char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */
814 char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */
815 char *zCol = 0; /* Name of the column. The "Z" */
816 int i, j; /* Loop counters */
817 int cnt = 0; /* Number of matching column names */
818 int cntTab = 0; /* Number of matching table names */
drh626a8792005-01-17 22:08:19 +0000819 sqlite3 *db = pParse->db; /* The database */
drh51669862004-12-18 18:40:26 +0000820 struct SrcList_item *pItem; /* Use for looping over pSrcList items */
821 struct SrcList_item *pMatch = 0; /* The matching pSrcList item */
drh73b211a2005-01-18 04:00:42 +0000822 NameContext *pTopNC = pNC; /* First namecontext in the list */
drh8141f612004-01-25 22:44:58 +0000823
824 assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
drha99db3b2004-06-19 14:49:12 +0000825 zDb = sqlite3NameFromToken(pDbToken);
826 zTab = sqlite3NameFromToken(pTableToken);
827 zCol = sqlite3NameFromToken(pColumnToken);
danielk197724b03fd2004-05-10 10:34:34 +0000828 if( sqlite3_malloc_failed ){
danielk1977d5d56522005-03-16 12:15:20 +0000829 goto lookupname_end;
drh8141f612004-01-25 22:44:58 +0000830 }
drh8141f612004-01-25 22:44:58 +0000831
832 pExpr->iTable = -1;
drh626a8792005-01-17 22:08:19 +0000833 while( pNC && cnt==0 ){
834 SrcList *pSrcList = pNC->pSrcList;
835 ExprList *pEList = pNC->pEList;
drh8141f612004-01-25 22:44:58 +0000836
drh626a8792005-01-17 22:08:19 +0000837 /* assert( zTab==0 || pEList==0 ); */
danielk1977b3bce662005-01-29 08:32:43 +0000838 if( pSrcList ){
839 for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
840 Table *pTab = pItem->pTab;
841 Column *pCol;
842
843 if( pTab==0 ) continue;
844 assert( pTab->nCol>0 );
845 if( zTab ){
846 if( pItem->zAlias ){
847 char *zTabName = pItem->zAlias;
848 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
849 }else{
850 char *zTabName = pTab->zName;
851 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
852 if( zDb!=0 && sqlite3StrICmp(db->aDb[pTab->iDb].zName, zDb)!=0 ){
853 continue;
854 }
drh626a8792005-01-17 22:08:19 +0000855 }
drh8141f612004-01-25 22:44:58 +0000856 }
danielk1977b3bce662005-01-29 08:32:43 +0000857 if( 0==(cntTab++) ){
drh626a8792005-01-17 22:08:19 +0000858 pExpr->iTable = pItem->iCursor;
drh626a8792005-01-17 22:08:19 +0000859 pExpr->iDb = pTab->iDb;
danielk1977b3bce662005-01-29 08:32:43 +0000860 pMatch = pItem;
861 }
862 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
863 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
drh873fac02005-06-06 17:11:46 +0000864 IdList *pUsing;
danielk1977b3bce662005-01-29 08:32:43 +0000865 cnt++;
866 pExpr->iTable = pItem->iCursor;
867 pMatch = pItem;
868 pExpr->iDb = pTab->iDb;
869 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
870 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
871 pExpr->affinity = pTab->aCol[j].affinity;
872 pExpr->pColl = pTab->aCol[j].pColl;
drh355ef362005-06-06 16:59:24 +0000873 if( pItem->jointype & JT_NATURAL ){
874 /* If this match occurred in the left table of a natural join,
875 ** then skip the right table to avoid a duplicate match */
876 pItem++;
877 i++;
878 }
drh873fac02005-06-06 17:11:46 +0000879 if( (pUsing = pItem->pUsing)!=0 ){
880 /* If this match occurs on a column that is in the USING clause
881 ** of a join, skip the search of the right table of the join
882 ** to avoid a duplicate match there. */
883 int k;
884 for(k=0; k<pUsing->nId; k++){
885 if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ){
886 pItem++;
887 i++;
888 break;
889 }
890 }
891 }
danielk1977b3bce662005-01-29 08:32:43 +0000892 break;
893 }
drh8141f612004-01-25 22:44:58 +0000894 }
895 }
896 }
drh626a8792005-01-17 22:08:19 +0000897
898#ifndef SQLITE_OMIT_TRIGGER
899 /* If we have not already resolved the name, then maybe
900 ** it is a new.* or old.* trigger argument reference
901 */
902 if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
903 TriggerStack *pTriggerStack = pParse->trigStack;
904 Table *pTab = 0;
905 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){
906 pExpr->iTable = pTriggerStack->newIdx;
907 assert( pTriggerStack->pTab );
908 pTab = pTriggerStack->pTab;
909 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab)==0 ){
910 pExpr->iTable = pTriggerStack->oldIdx;
911 assert( pTriggerStack->pTab );
912 pTab = pTriggerStack->pTab;
913 }
914
915 if( pTab ){
916 int j;
917 Column *pCol = pTab->aCol;
918
919 pExpr->iDb = pTab->iDb;
920 cntTab++;
921 for(j=0; j < pTab->nCol; j++, pCol++) {
922 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
923 cnt++;
924 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
925 pExpr->affinity = pTab->aCol[j].affinity;
926 pExpr->pColl = pTab->aCol[j].pColl;
danielk1977aee18ef2005-03-09 12:26:50 +0000927 pExpr->pTab = pTab;
drh626a8792005-01-17 22:08:19 +0000928 break;
929 }
930 }
931 }
932 }
drhb7f91642004-10-31 02:22:47 +0000933#endif /* !defined(SQLITE_OMIT_TRIGGER) */
drh8141f612004-01-25 22:44:58 +0000934
drh626a8792005-01-17 22:08:19 +0000935 /*
936 ** Perhaps the name is a reference to the ROWID
937 */
938 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
939 cnt = 1;
940 pExpr->iColumn = -1;
941 pExpr->affinity = SQLITE_AFF_INTEGER;
942 }
drh8141f612004-01-25 22:44:58 +0000943
drh626a8792005-01-17 22:08:19 +0000944 /*
945 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
946 ** might refer to an result-set alias. This happens, for example, when
947 ** we are resolving names in the WHERE clause of the following command:
948 **
949 ** SELECT a+b AS x FROM table WHERE x<10;
950 **
951 ** In cases like this, replace pExpr with a copy of the expression that
952 ** forms the result set entry ("a+b" in the example) and return immediately.
953 ** Note that the expression in the result set should have already been
954 ** resolved by the time the WHERE clause is resolved.
955 */
drh79d5f632005-01-18 17:20:10 +0000956 if( cnt==0 && pEList!=0 && zTab==0 ){
drh626a8792005-01-17 22:08:19 +0000957 for(j=0; j<pEList->nExpr; j++){
958 char *zAs = pEList->a[j].zName;
959 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
960 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
961 pExpr->op = TK_AS;
962 pExpr->iColumn = j;
963 pExpr->pLeft = sqlite3ExprDup(pEList->a[j].pExpr);
drh15ccce12005-05-23 15:06:39 +0000964 cnt = 1;
drh626a8792005-01-17 22:08:19 +0000965 assert( zTab==0 && zDb==0 );
drh15ccce12005-05-23 15:06:39 +0000966 goto lookupname_end_2;
drh626a8792005-01-17 22:08:19 +0000967 }
968 }
969 }
970
971 /* Advance to the next name context. The loop will exit when either
972 ** we have a match (cnt>0) or when we run out of name contexts.
973 */
974 if( cnt==0 ){
975 pNC = pNC->pNext;
976 }
drh8141f612004-01-25 22:44:58 +0000977 }
978
979 /*
980 ** If X and Y are NULL (in other words if only the column name Z is
981 ** supplied) and the value of Z is enclosed in double-quotes, then
982 ** Z is a string literal if it doesn't match any column names. In that
983 ** case, we need to return right away and not make any changes to
984 ** pExpr.
drh15ccce12005-05-23 15:06:39 +0000985 **
986 ** Because no reference was made to outer contexts, the pNC->nRef
987 ** fields are not changed in any context.
drh8141f612004-01-25 22:44:58 +0000988 */
989 if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
990 sqliteFree(zCol);
991 return 0;
992 }
993
994 /*
995 ** cnt==0 means there was not match. cnt>1 means there were two or
996 ** more matches. Either way, we have an error.
997 */
998 if( cnt!=1 ){
999 char *z = 0;
1000 char *zErr;
1001 zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s";
1002 if( zDb ){
danielk19774adee202004-05-08 08:23:19 +00001003 sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, 0);
drh8141f612004-01-25 22:44:58 +00001004 }else if( zTab ){
danielk19774adee202004-05-08 08:23:19 +00001005 sqlite3SetString(&z, zTab, ".", zCol, 0);
drh8141f612004-01-25 22:44:58 +00001006 }else{
1007 z = sqliteStrDup(zCol);
1008 }
danielk19774adee202004-05-08 08:23:19 +00001009 sqlite3ErrorMsg(pParse, zErr, z);
drh8141f612004-01-25 22:44:58 +00001010 sqliteFree(z);
drh73b211a2005-01-18 04:00:42 +00001011 pTopNC->nErr++;
drh8141f612004-01-25 22:44:58 +00001012 }
1013
drh51669862004-12-18 18:40:26 +00001014 /* If a column from a table in pSrcList is referenced, then record
1015 ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes
1016 ** bit 0 to be set. Column 1 sets bit 1. And so forth. If the
1017 ** column number is greater than the number of bits in the bitmask
1018 ** then set the high-order bit of the bitmask.
1019 */
1020 if( pExpr->iColumn>=0 && pMatch!=0 ){
1021 int n = pExpr->iColumn;
1022 if( n>=sizeof(Bitmask)*8 ){
1023 n = sizeof(Bitmask)*8-1;
1024 }
1025 assert( pMatch->iCursor==pExpr->iTable );
1026 pMatch->colUsed |= 1<<n;
1027 }
1028
danielk1977d5d56522005-03-16 12:15:20 +00001029lookupname_end:
drh8141f612004-01-25 22:44:58 +00001030 /* Clean up and return
1031 */
1032 sqliteFree(zDb);
1033 sqliteFree(zTab);
danielk19774adee202004-05-08 08:23:19 +00001034 sqlite3ExprDelete(pExpr->pLeft);
drh8141f612004-01-25 22:44:58 +00001035 pExpr->pLeft = 0;
danielk19774adee202004-05-08 08:23:19 +00001036 sqlite3ExprDelete(pExpr->pRight);
drh8141f612004-01-25 22:44:58 +00001037 pExpr->pRight = 0;
1038 pExpr->op = TK_COLUMN;
drh15ccce12005-05-23 15:06:39 +00001039lookupname_end_2:
1040 sqliteFree(zCol);
drh626a8792005-01-17 22:08:19 +00001041 if( cnt==1 ){
danielk1977b3bce662005-01-29 08:32:43 +00001042 assert( pNC!=0 );
drh626a8792005-01-17 22:08:19 +00001043 sqlite3AuthRead(pParse, pExpr, pNC->pSrcList);
danielk1977aee18ef2005-03-09 12:26:50 +00001044 if( pMatch && !pMatch->pSelect ){
1045 pExpr->pTab = pMatch->pTab;
1046 }
drh15ccce12005-05-23 15:06:39 +00001047 /* Increment the nRef value on all name contexts from TopNC up to
1048 ** the point where the name matched. */
1049 for(;;){
1050 assert( pTopNC!=0 );
1051 pTopNC->nRef++;
1052 if( pTopNC==pNC ) break;
1053 pTopNC = pTopNC->pNext;
1054 }
1055 return 0;
1056 } else {
1057 return 1;
drh626a8792005-01-17 22:08:19 +00001058 }
drh8141f612004-01-25 22:44:58 +00001059}
1060
1061/*
drh626a8792005-01-17 22:08:19 +00001062** This routine is designed as an xFunc for walkExprTree().
1063**
drh73b211a2005-01-18 04:00:42 +00001064** Resolve symbolic names into TK_COLUMN operators for the current
drh626a8792005-01-17 22:08:19 +00001065** node in the expression tree. Return 0 to continue the search down
drh73b211a2005-01-18 04:00:42 +00001066** the tree or 2 to abort the tree walk.
1067**
1068** This routine also does error checking and name resolution for
1069** function names. The operator for aggregate functions is changed
1070** to TK_AGG_FUNCTION.
drh626a8792005-01-17 22:08:19 +00001071*/
1072static int nameResolverStep(void *pArg, Expr *pExpr){
1073 NameContext *pNC = (NameContext*)pArg;
1074 SrcList *pSrcList;
1075 Parse *pParse;
drh626a8792005-01-17 22:08:19 +00001076
danielk1977b3bce662005-01-29 08:32:43 +00001077 if( pExpr==0 ) return 1;
drh626a8792005-01-17 22:08:19 +00001078 assert( pNC!=0 );
1079 pSrcList = pNC->pSrcList;
1080 pParse = pNC->pParse;
danielk1977b3bce662005-01-29 08:32:43 +00001081
drh626a8792005-01-17 22:08:19 +00001082 if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return 1;
1083 ExprSetProperty(pExpr, EP_Resolved);
1084#ifndef NDEBUG
1085 if( pSrcList ){
danielk1977940fac92005-01-23 22:41:37 +00001086 int i;
drh626a8792005-01-17 22:08:19 +00001087 for(i=0; i<pSrcList->nSrc; i++){
1088 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
1089 }
1090 }
1091#endif
1092 switch( pExpr->op ){
1093 /* Double-quoted strings (ex: "abc") are used as identifiers if
1094 ** possible. Otherwise they remain as strings. Single-quoted
1095 ** strings (ex: 'abc') are always string literals.
1096 */
1097 case TK_STRING: {
1098 if( pExpr->token.z[0]=='\'' ) break;
1099 /* Fall thru into the TK_ID case if this is a double-quoted string */
1100 }
1101 /* A lone identifier is the name of a column.
1102 */
1103 case TK_ID: {
drh626a8792005-01-17 22:08:19 +00001104 lookupName(pParse, 0, 0, &pExpr->token, pNC, pExpr);
1105 return 1;
1106 }
1107
1108 /* A table name and column name: ID.ID
1109 ** Or a database, table and column: ID.ID.ID
1110 */
1111 case TK_DOT: {
1112 Token *pColumn;
1113 Token *pTable;
1114 Token *pDb;
1115 Expr *pRight;
1116
danielk1977b3bce662005-01-29 08:32:43 +00001117 /* if( pSrcList==0 ) break; */
drh626a8792005-01-17 22:08:19 +00001118 pRight = pExpr->pRight;
1119 if( pRight->op==TK_ID ){
1120 pDb = 0;
1121 pTable = &pExpr->pLeft->token;
1122 pColumn = &pRight->token;
1123 }else{
1124 assert( pRight->op==TK_DOT );
1125 pDb = &pExpr->pLeft->token;
1126 pTable = &pRight->pLeft->token;
1127 pColumn = &pRight->pRight->token;
1128 }
1129 lookupName(pParse, pDb, pTable, pColumn, pNC, pExpr);
1130 return 1;
1131 }
1132
1133 /* Resolve function names
1134 */
drhb71090f2005-05-23 17:26:51 +00001135 case TK_CONST_FUNC:
drh626a8792005-01-17 22:08:19 +00001136 case TK_FUNCTION: {
1137 ExprList *pList = pExpr->pList; /* The argument list */
1138 int n = pList ? pList->nExpr : 0; /* Number of arguments */
1139 int no_such_func = 0; /* True if no such function exists */
1140 int wrong_num_args = 0; /* True if wrong number of arguments */
1141 int is_agg = 0; /* True if is an aggregate function */
1142 int i;
1143 int nId; /* Number of characters in function name */
1144 const char *zId; /* The function name. */
drh73b211a2005-01-18 04:00:42 +00001145 FuncDef *pDef; /* Information about the function */
1146 int enc = pParse->db->enc; /* The database encoding */
drh626a8792005-01-17 22:08:19 +00001147
drhb71090f2005-05-23 17:26:51 +00001148 zId = pExpr->token.z;
1149 nId = pExpr->token.n;
drh626a8792005-01-17 22:08:19 +00001150 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
1151 if( pDef==0 ){
1152 pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
1153 if( pDef==0 ){
1154 no_such_func = 1;
1155 }else{
1156 wrong_num_args = 1;
1157 }
1158 }else{
1159 is_agg = pDef->xFunc==0;
1160 }
1161 if( is_agg && !pNC->allowAgg ){
1162 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
1163 pNC->nErr++;
1164 is_agg = 0;
1165 }else if( no_such_func ){
1166 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
1167 pNC->nErr++;
1168 }else if( wrong_num_args ){
1169 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
1170 nId, zId);
1171 pNC->nErr++;
1172 }
1173 if( is_agg ){
1174 pExpr->op = TK_AGG_FUNCTION;
1175 pNC->hasAgg = 1;
1176 }
drh73b211a2005-01-18 04:00:42 +00001177 if( is_agg ) pNC->allowAgg = 0;
drh626a8792005-01-17 22:08:19 +00001178 for(i=0; pNC->nErr==0 && i<n; i++){
drh73b211a2005-01-18 04:00:42 +00001179 walkExprTree(pList->a[i].pExpr, nameResolverStep, pNC);
drh626a8792005-01-17 22:08:19 +00001180 }
drh73b211a2005-01-18 04:00:42 +00001181 if( is_agg ) pNC->allowAgg = 1;
drh626a8792005-01-17 22:08:19 +00001182 /* FIX ME: Compute pExpr->affinity based on the expected return
1183 ** type of the function
1184 */
1185 return is_agg;
1186 }
danielk1977b3bce662005-01-29 08:32:43 +00001187#ifndef SQLITE_OMIT_SUBQUERY
1188 case TK_SELECT:
1189 case TK_EXISTS:
1190#endif
1191 case TK_IN: {
1192 if( pExpr->pSelect ){
1193 int nRef = pNC->nRef;
1194 sqlite3SelectResolve(pParse, pExpr->pSelect, pNC);
1195 assert( pNC->nRef>=nRef );
1196 if( nRef!=pNC->nRef ){
1197 ExprSetProperty(pExpr, EP_VarSelect);
1198 }
1199 }
1200 }
drh626a8792005-01-17 22:08:19 +00001201 }
1202 return 0;
1203}
1204
1205/*
drhcce7d172000-05-31 15:34:51 +00001206** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +00001207** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +00001208** index to the table in the table list and a column offset. The
1209** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
1210** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +00001211** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +00001212** VDBE cursor number for a cursor that is pointing into the referenced
1213** table. The Expr.iColumn value is changed to the index of the column
1214** of the referenced table. The Expr.iColumn value for the special
1215** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
1216** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +00001217**
drh626a8792005-01-17 22:08:19 +00001218** Also resolve function names and check the functions for proper
1219** usage. Make sure all function names are recognized and all functions
1220** have the correct number of arguments. Leave an error message
1221** in pParse->zErrMsg if anything is amiss. Return the number of errors.
1222**
drh73b211a2005-01-18 04:00:42 +00001223** If the expression contains aggregate functions then set the EP_Agg
1224** property on the expression.
drh626a8792005-01-17 22:08:19 +00001225*/
1226int sqlite3ExprResolveNames(
danielk1977b3bce662005-01-29 08:32:43 +00001227 NameContext *pNC, /* Namespace to resolve expressions in. */
1228 Expr *pExpr /* The expression to be analyzed. */
drh626a8792005-01-17 22:08:19 +00001229){
drh73b211a2005-01-18 04:00:42 +00001230 if( pExpr==0 ) return 0;
danielk1977b3bce662005-01-29 08:32:43 +00001231 walkExprTree(pExpr, nameResolverStep, pNC);
1232 if( pNC->nErr>0 ){
drh73b211a2005-01-18 04:00:42 +00001233 ExprSetProperty(pExpr, EP_Error);
drh73b211a2005-01-18 04:00:42 +00001234 }
1235 return ExprHasProperty(pExpr, EP_Error);
drh626a8792005-01-17 22:08:19 +00001236}
1237
drh1398ad32005-01-19 23:24:50 +00001238/*
1239** A pointer instance of this structure is used to pass information
1240** through walkExprTree into codeSubqueryStep().
1241*/
1242typedef struct QueryCoder QueryCoder;
1243struct QueryCoder {
1244 Parse *pParse; /* The parsing context */
1245 NameContext *pNC; /* Namespace of first enclosing query */
1246};
1247
drh626a8792005-01-17 22:08:19 +00001248
1249/*
1250** Generate code for subqueries and IN operators.
1251**
drh73b211a2005-01-18 04:00:42 +00001252** IN operators comes in two forms:
drhfef52082000-06-06 01:50:43 +00001253**
1254** expr IN (exprlist)
1255** and
1256** expr IN (SELECT ...)
1257**
1258** The first form is handled by creating a set holding the list
1259** of allowed values. The second form causes the SELECT to generate
1260** a temporary table.
drhcce7d172000-05-31 15:34:51 +00001261*/
drh51522cd2005-01-20 13:36:19 +00001262#ifndef SQLITE_OMIT_SUBQUERY
danielk1977b3bce662005-01-29 08:32:43 +00001263void sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){
drh57dbd7b2005-07-08 18:25:26 +00001264 int testAddr = 0; /* One-time test address */
danielk1977b3bce662005-01-29 08:32:43 +00001265 Vdbe *v = sqlite3GetVdbe(pParse);
1266 if( v==0 ) return;
1267
drh57dbd7b2005-07-08 18:25:26 +00001268 /* This code must be run in its entirety every time it is encountered
1269 ** if any of the following is true:
1270 **
1271 ** * The right-hand side is a correlated subquery
1272 ** * The right-hand side is an expression list containing variables
1273 ** * We are inside a trigger
1274 **
1275 ** If all of the above are false, then we can run this code just once
1276 ** save the results, and reuse the same result on subsequent invocations.
danielk1977b3bce662005-01-29 08:32:43 +00001277 */
1278 if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->trigStack ){
1279 int mem = pParse->nMem++;
1280 sqlite3VdbeAddOp(v, OP_MemLoad, mem, 0);
drh57dbd7b2005-07-08 18:25:26 +00001281 testAddr = sqlite3VdbeAddOp(v, OP_If, 0, 0);
1282 assert( testAddr>0 );
danielk1977b3bce662005-01-29 08:32:43 +00001283 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1284 sqlite3VdbeAddOp(v, OP_MemStore, mem, 1);
1285 }
1286
1287 if( pExpr->pSelect ){
1288 sqlite3VdbeAddOp(v, OP_AggContextPush, 0, 0);
1289 }
drh6a3ea0e2003-05-02 14:32:12 +00001290
drhcce7d172000-05-31 15:34:51 +00001291 switch( pExpr->op ){
drhfef52082000-06-06 01:50:43 +00001292 case TK_IN: {
danielk1977e014a832004-05-17 10:48:57 +00001293 char affinity;
drhd3d39e92004-05-20 22:16:29 +00001294 KeyInfo keyInfo;
drh9170dd72005-07-08 17:13:46 +00001295 int addr; /* Address of OP_OpenVirtual instruction */
drhd3d39e92004-05-20 22:16:29 +00001296
danielk1977bf3b7212004-05-18 10:06:24 +00001297 affinity = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +00001298
1299 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
drh57dbd7b2005-07-08 18:25:26 +00001300 ** expression it is handled the same way. A virtual table is
danielk1977e014a832004-05-17 10:48:57 +00001301 ** filled with single-field index keys representing the results
1302 ** from the SELECT or the <exprlist>.
1303 **
1304 ** If the 'x' expression is a column value, or the SELECT...
1305 ** statement returns a column value, then the affinity of that
1306 ** column is used to build the index keys. If both 'x' and the
1307 ** SELECT... statement are columns, then numeric affinity is used
1308 ** if either column has NUMERIC or INTEGER affinity. If neither
1309 ** 'x' nor the SELECT... statement are columns, then numeric affinity
1310 ** is used.
1311 */
1312 pExpr->iTable = pParse->nTab++;
drh9170dd72005-07-08 17:13:46 +00001313 addr = sqlite3VdbeAddOp(v, OP_OpenVirtual, pExpr->iTable, 0);
drhd3d39e92004-05-20 22:16:29 +00001314 memset(&keyInfo, 0, sizeof(keyInfo));
1315 keyInfo.nField = 1;
drhf3218fe2004-05-28 08:21:02 +00001316 sqlite3VdbeAddOp(v, OP_SetNumColumns, pExpr->iTable, 1);
danielk1977e014a832004-05-17 10:48:57 +00001317
drhfef52082000-06-06 01:50:43 +00001318 if( pExpr->pSelect ){
1319 /* Case 1: expr IN (SELECT ...)
1320 **
danielk1977e014a832004-05-17 10:48:57 +00001321 ** Generate code to write the results of the select into the temporary
1322 ** table allocated and opened above.
drhfef52082000-06-06 01:50:43 +00001323 */
danielk1977e014a832004-05-17 10:48:57 +00001324 int iParm = pExpr->iTable + (((int)affinity)<<16);
drhbe5c89a2004-07-26 00:31:09 +00001325 ExprList *pEList;
danielk1977e014a832004-05-17 10:48:57 +00001326 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
danielk1977b3bce662005-01-29 08:32:43 +00001327 sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001328 pEList = pExpr->pSelect->pEList;
1329 if( pEList && pEList->nExpr>0 ){
danielk19777cedc8d2004-06-10 10:50:08 +00001330 keyInfo.aColl[0] = binaryCompareCollSeq(pParse, pExpr->pLeft,
drhbe5c89a2004-07-26 00:31:09 +00001331 pEList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001332 }
drhfef52082000-06-06 01:50:43 +00001333 }else if( pExpr->pList ){
1334 /* Case 2: expr IN (exprlist)
1335 **
danielk1977e014a832004-05-17 10:48:57 +00001336 ** For each expression, build an index key from the evaluation and
1337 ** store it in the temporary table. If <expr> is a column, then use
1338 ** that columns affinity when building index keys. If <expr> is not
1339 ** a column, use numeric affinity.
drhfef52082000-06-06 01:50:43 +00001340 */
danielk1977e014a832004-05-17 10:48:57 +00001341 int i;
drh57dbd7b2005-07-08 18:25:26 +00001342 ExprList *pList = pExpr->pList;
1343 struct ExprList_item *pItem;
1344
danielk1977e014a832004-05-17 10:48:57 +00001345 if( !affinity ){
1346 affinity = SQLITE_AFF_NUMERIC;
1347 }
danielk19770202b292004-06-09 09:55:16 +00001348 keyInfo.aColl[0] = pExpr->pLeft->pColl;
danielk1977e014a832004-05-17 10:48:57 +00001349
1350 /* Loop through each expression in <exprlist>. */
drh57dbd7b2005-07-08 18:25:26 +00001351 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
1352 Expr *pE2 = pItem->pExpr;
danielk1977e014a832004-05-17 10:48:57 +00001353
drh57dbd7b2005-07-08 18:25:26 +00001354 /* If the expression is not constant then we will need to
1355 ** disable the test that was generated above that makes sure
1356 ** this code only executes once. Because for a non-constant
1357 ** expression we need to rerun this code each time.
1358 */
drh6c30be82005-07-29 15:10:17 +00001359 if( testAddr>0 && !sqlite3ExprIsConstant(pE2) ){
drh57dbd7b2005-07-08 18:25:26 +00001360 VdbeOp *aOp = sqlite3VdbeGetOp(v, testAddr-1);
1361 int i;
1362 for(i=0; i<4; i++){
1363 aOp[i].opcode = OP_Noop;
1364 }
1365 testAddr = 0;
drh4794b982000-06-06 13:54:14 +00001366 }
danielk1977e014a832004-05-17 10:48:57 +00001367
1368 /* Evaluate the expression and insert it into the temp table */
1369 sqlite3ExprCode(pParse, pE2);
drh94a11212004-09-25 13:12:14 +00001370 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1);
drhf0863fe2005-06-12 21:35:51 +00001371 sqlite3VdbeAddOp(v, OP_IdxInsert, pExpr->iTable, 0);
drhfef52082000-06-06 01:50:43 +00001372 }
1373 }
danielk19770202b292004-06-09 09:55:16 +00001374 sqlite3VdbeChangeP3(v, addr, (void *)&keyInfo, P3_KEYINFO);
danielk1977b3bce662005-01-29 08:32:43 +00001375 break;
drhfef52082000-06-06 01:50:43 +00001376 }
1377
drh51522cd2005-01-20 13:36:19 +00001378 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001379 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +00001380 /* This has to be a scalar SELECT. Generate code to put the
1381 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +00001382 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +00001383 */
drh51522cd2005-01-20 13:36:19 +00001384 int sop;
1385 Select *pSel;
drh1398ad32005-01-19 23:24:50 +00001386
drh967e8b72000-06-21 13:59:10 +00001387 pExpr->iColumn = pParse->nMem++;
drh51522cd2005-01-20 13:36:19 +00001388 pSel = pExpr->pSelect;
1389 if( pExpr->op==TK_SELECT ){
1390 sop = SRT_Mem;
1391 }else{
1392 static const Token one = { "1", 0, 1 };
1393 sop = SRT_Exists;
1394 sqlite3ExprListDelete(pSel->pEList);
1395 pSel->pEList = sqlite3ExprListAppend(0,
1396 sqlite3Expr(TK_INTEGER, 0, 0, &one), 0);
1397 }
danielk1977b3bce662005-01-29 08:32:43 +00001398 sqlite3Select(pParse, pSel, sop, pExpr->iColumn, 0, 0, 0, 0);
1399 break;
drhcce7d172000-05-31 15:34:51 +00001400 }
1401 }
danielk1977b3bce662005-01-29 08:32:43 +00001402
1403 if( pExpr->pSelect ){
1404 sqlite3VdbeAddOp(v, OP_AggContextPop, 0, 0);
1405 }
drh57dbd7b2005-07-08 18:25:26 +00001406 if( testAddr ){
1407 sqlite3VdbeChangeP2(v, testAddr, sqlite3VdbeCurrentAddr(v));
danielk1977b3bce662005-01-29 08:32:43 +00001408 }
1409 return;
drhcce7d172000-05-31 15:34:51 +00001410}
drh51522cd2005-01-20 13:36:19 +00001411#endif /* SQLITE_OMIT_SUBQUERY */
drhcce7d172000-05-31 15:34:51 +00001412
drhcce7d172000-05-31 15:34:51 +00001413/*
drhfec19aa2004-05-19 20:41:03 +00001414** Generate an instruction that will put the integer describe by
1415** text z[0..n-1] on the stack.
1416*/
1417static void codeInteger(Vdbe *v, const char *z, int n){
1418 int i;
drh6fec0762004-05-30 01:38:43 +00001419 if( sqlite3GetInt32(z, &i) ){
1420 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
1421 }else if( sqlite3FitsIn64Bits(z) ){
drh29dda4a2005-07-21 18:23:20 +00001422 sqlite3VdbeOp3(v, OP_Int64, 0, 0, z, n);
drhfec19aa2004-05-19 20:41:03 +00001423 }else{
1424 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n);
1425 }
1426}
1427
1428/*
drhcce7d172000-05-31 15:34:51 +00001429** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +00001430** expression and leave the result on the top of stack.
drhf2bc0132004-10-04 13:19:23 +00001431**
1432** This code depends on the fact that certain token values (ex: TK_EQ)
1433** are the same as opcode values (ex: OP_Eq) that implement the corresponding
1434** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
1435** the make process cause these values to align. Assert()s in the code
1436** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00001437*/
danielk19774adee202004-05-08 08:23:19 +00001438void sqlite3ExprCode(Parse *pParse, Expr *pExpr){
drhcce7d172000-05-31 15:34:51 +00001439 Vdbe *v = pParse->pVdbe;
1440 int op;
danielk19777977a172004-11-09 12:44:37 +00001441 if( v==0 ) return;
1442 if( pExpr==0 ){
drhf0863fe2005-06-12 21:35:51 +00001443 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
danielk19777977a172004-11-09 12:44:37 +00001444 return;
1445 }
drhf2bc0132004-10-04 13:19:23 +00001446 op = pExpr->op;
1447 switch( op ){
drh967e8b72000-06-21 13:59:10 +00001448 case TK_COLUMN: {
danielk1977a58fdfb2005-02-08 07:50:40 +00001449 if( !pParse->fillAgg && pExpr->iAgg>=0 ){
1450 sqlite3VdbeAddOp(v, OP_AggGet, pExpr->iAggCtx, pExpr->iAgg);
drhc4a3c772001-04-04 11:48:57 +00001451 }else if( pExpr->iColumn>=0 ){
danielk19774adee202004-05-08 08:23:19 +00001452 sqlite3VdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn);
danielk1977aee18ef2005-03-09 12:26:50 +00001453 sqlite3ColumnDefault(v, pExpr->pTab, pExpr->iColumn);
drhc4a3c772001-04-04 11:48:57 +00001454 }else{
drhf0863fe2005-06-12 21:35:51 +00001455 sqlite3VdbeAddOp(v, OP_Rowid, pExpr->iTable, 0);
drh22827922000-06-06 17:27:05 +00001456 }
drhcce7d172000-05-31 15:34:51 +00001457 break;
1458 }
1459 case TK_INTEGER: {
drhfec19aa2004-05-19 20:41:03 +00001460 codeInteger(v, pExpr->token.z, pExpr->token.n);
1461 break;
1462 }
1463 case TK_FLOAT:
1464 case TK_STRING: {
drhf2bc0132004-10-04 13:19:23 +00001465 assert( TK_FLOAT==OP_Real );
1466 assert( TK_STRING==OP_String8 );
drhd2687b72005-08-12 22:56:09 +00001467 sqlite3DequoteExpr(pExpr);
drhfec19aa2004-05-19 20:41:03 +00001468 sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z, pExpr->token.n);
drhcce7d172000-05-31 15:34:51 +00001469 break;
1470 }
drhf0863fe2005-06-12 21:35:51 +00001471 case TK_NULL: {
1472 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
1473 break;
1474 }
danielk19775338a5f2005-01-20 13:03:10 +00001475#ifndef SQLITE_OMIT_BLOB_LITERAL
danielk1977c572ef72004-05-27 09:28:41 +00001476 case TK_BLOB: {
drh6c8c6ce2005-08-23 11:17:58 +00001477 int n;
1478 const char *z;
drhf2bc0132004-10-04 13:19:23 +00001479 assert( TK_BLOB==OP_HexBlob );
drh6c8c6ce2005-08-23 11:17:58 +00001480 n = pExpr->token.n - 3;
1481 z = pExpr->token.z + 2;
1482 assert( n>=0 );
1483 if( n==0 ){
1484 z = "";
1485 }
1486 sqlite3VdbeOp3(v, op, 0, 0, z, n);
danielk1977c572ef72004-05-27 09:28:41 +00001487 break;
1488 }
danielk19775338a5f2005-01-20 13:03:10 +00001489#endif
drh50457892003-09-06 01:10:47 +00001490 case TK_VARIABLE: {
danielk19774adee202004-05-08 08:23:19 +00001491 sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0);
drh895d7472004-08-20 16:02:39 +00001492 if( pExpr->token.n>1 ){
1493 sqlite3VdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
1494 }
drh50457892003-09-06 01:10:47 +00001495 break;
1496 }
drh4e0cff62004-11-05 05:10:28 +00001497 case TK_REGISTER: {
1498 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iTable, 0);
1499 break;
1500 }
drh487e2622005-06-25 18:42:14 +00001501#ifndef SQLITE_OMIT_CAST
1502 case TK_CAST: {
1503 /* Expressions of the form: CAST(pLeft AS token) */
1504 int aff, op;
1505 sqlite3ExprCode(pParse, pExpr->pLeft);
1506 aff = sqlite3AffinityType(&pExpr->token);
1507 switch( aff ){
1508 case SQLITE_AFF_INTEGER: op = OP_ToInt; break;
1509 case SQLITE_AFF_NUMERIC: op = OP_ToNumeric; break;
1510 case SQLITE_AFF_TEXT: op = OP_ToText; break;
1511 case SQLITE_AFF_NONE: op = OP_ToBlob; break;
1512 }
1513 sqlite3VdbeAddOp(v, op, 0, 0);
1514 break;
1515 }
1516#endif /* SQLITE_OMIT_CAST */
drhc9b84a12002-06-20 11:36:48 +00001517 case TK_LT:
1518 case TK_LE:
1519 case TK_GT:
1520 case TK_GE:
1521 case TK_NE:
1522 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00001523 assert( TK_LT==OP_Lt );
1524 assert( TK_LE==OP_Le );
1525 assert( TK_GT==OP_Gt );
1526 assert( TK_GE==OP_Ge );
1527 assert( TK_EQ==OP_Eq );
1528 assert( TK_NE==OP_Ne );
danielk1977a37cdde2004-05-16 11:15:36 +00001529 sqlite3ExprCode(pParse, pExpr->pLeft);
1530 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001531 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 0, 0);
danielk1977a37cdde2004-05-16 11:15:36 +00001532 break;
drhc9b84a12002-06-20 11:36:48 +00001533 }
drhcce7d172000-05-31 15:34:51 +00001534 case TK_AND:
1535 case TK_OR:
1536 case TK_PLUS:
1537 case TK_STAR:
1538 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00001539 case TK_REM:
1540 case TK_BITAND:
1541 case TK_BITOR:
drh17c40292004-07-21 02:53:29 +00001542 case TK_SLASH:
drhbf4133c2001-10-13 02:59:08 +00001543 case TK_LSHIFT:
drh855eb1c2004-08-31 13:45:11 +00001544 case TK_RSHIFT:
drh00400772000-06-16 20:51:26 +00001545 case TK_CONCAT: {
drhf2bc0132004-10-04 13:19:23 +00001546 assert( TK_AND==OP_And );
1547 assert( TK_OR==OP_Or );
1548 assert( TK_PLUS==OP_Add );
1549 assert( TK_MINUS==OP_Subtract );
1550 assert( TK_REM==OP_Remainder );
1551 assert( TK_BITAND==OP_BitAnd );
1552 assert( TK_BITOR==OP_BitOr );
1553 assert( TK_SLASH==OP_Divide );
1554 assert( TK_LSHIFT==OP_ShiftLeft );
1555 assert( TK_RSHIFT==OP_ShiftRight );
1556 assert( TK_CONCAT==OP_Concat );
danielk19774adee202004-05-08 08:23:19 +00001557 sqlite3ExprCode(pParse, pExpr->pLeft);
1558 sqlite3ExprCode(pParse, pExpr->pRight);
drh855eb1c2004-08-31 13:45:11 +00001559 sqlite3VdbeAddOp(v, op, 0, 0);
drh00400772000-06-16 20:51:26 +00001560 break;
1561 }
drhcce7d172000-05-31 15:34:51 +00001562 case TK_UMINUS: {
drhfec19aa2004-05-19 20:41:03 +00001563 Expr *pLeft = pExpr->pLeft;
1564 assert( pLeft );
1565 if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
1566 Token *p = &pLeft->token;
drh6e142f52000-06-08 13:36:40 +00001567 char *z = sqliteMalloc( p->n + 2 );
1568 sprintf(z, "-%.*s", p->n, p->z);
drhfec19aa2004-05-19 20:41:03 +00001569 if( pLeft->op==TK_FLOAT ){
1570 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001571 }else{
drhfec19aa2004-05-19 20:41:03 +00001572 codeInteger(v, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001573 }
drh6e142f52000-06-08 13:36:40 +00001574 sqliteFree(z);
1575 break;
1576 }
drh1ccde152000-06-17 13:12:39 +00001577 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +00001578 }
drhbf4133c2001-10-13 02:59:08 +00001579 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00001580 case TK_NOT: {
drhf2bc0132004-10-04 13:19:23 +00001581 assert( TK_BITNOT==OP_BitNot );
1582 assert( TK_NOT==OP_Not );
danielk19774adee202004-05-08 08:23:19 +00001583 sqlite3ExprCode(pParse, pExpr->pLeft);
1584 sqlite3VdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001585 break;
1586 }
1587 case TK_ISNULL:
1588 case TK_NOTNULL: {
1589 int dest;
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 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1593 sqlite3ExprCode(pParse, pExpr->pLeft);
1594 dest = sqlite3VdbeCurrentAddr(v) + 2;
1595 sqlite3VdbeAddOp(v, op, 1, dest);
1596 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);
drhf2bc0132004-10-04 13:19:23 +00001597 break;
drhcce7d172000-05-31 15:34:51 +00001598 }
drh22827922000-06-06 17:27:05 +00001599 case TK_AGG_FUNCTION: {
danielk19774adee202004-05-08 08:23:19 +00001600 sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drh22827922000-06-06 17:27:05 +00001601 break;
1602 }
drhb71090f2005-05-23 17:26:51 +00001603 case TK_CONST_FUNC:
drhcce7d172000-05-31 15:34:51 +00001604 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00001605 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00001606 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00001607 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00001608 int nId;
1609 const char *zId;
danielk1977682f68b2004-06-05 10:22:17 +00001610 int p2 = 0;
1611 int i;
danielk1977d8123362004-06-12 09:25:12 +00001612 u8 enc = pParse->db->enc;
danielk1977dc1bdc42004-06-11 10:51:27 +00001613 CollSeq *pColl = 0;
drhb71090f2005-05-23 17:26:51 +00001614 zId = pExpr->token.z;
1615 nId = pExpr->token.n;
danielk1977d8123362004-06-12 09:25:12 +00001616 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001617 assert( pDef!=0 );
drhf9b596e2004-05-26 16:54:42 +00001618 nExpr = sqlite3ExprCodeExprList(pParse, pList);
danielk1977682f68b2004-06-05 10:22:17 +00001619 for(i=0; i<nExpr && i<32; i++){
danielk1977d02eb1f2004-06-06 09:44:03 +00001620 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
1621 p2 |= (1<<i);
1622 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001623 if( pDef->needCollSeq && !pColl ){
1624 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
1625 }
1626 }
1627 if( pDef->needCollSeq ){
1628 if( !pColl ) pColl = pParse->db->pDfltColl;
danielk1977d8123362004-06-12 09:25:12 +00001629 sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ);
danielk1977682f68b2004-06-05 10:22:17 +00001630 }
1631 sqlite3VdbeOp3(v, OP_Function, nExpr, p2, (char*)pDef, P3_FUNCDEF);
drhcce7d172000-05-31 15:34:51 +00001632 break;
1633 }
drhfe2093d2005-01-20 22:48:47 +00001634#ifndef SQLITE_OMIT_SUBQUERY
1635 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001636 case TK_SELECT: {
danielk1977b3bce662005-01-29 08:32:43 +00001637 sqlite3CodeSubselect(pParse, pExpr);
danielk19774adee202004-05-08 08:23:19 +00001638 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drhad6d9462004-09-19 02:15:24 +00001639 VdbeComment((v, "# load subquery result"));
drh19a775c2000-06-05 18:54:46 +00001640 break;
1641 }
drhfef52082000-06-06 01:50:43 +00001642 case TK_IN: {
1643 int addr;
drh94a11212004-09-25 13:12:14 +00001644 char affinity;
danielk1977b3bce662005-01-29 08:32:43 +00001645 sqlite3CodeSubselect(pParse, pExpr);
danielk1977e014a832004-05-17 10:48:57 +00001646
1647 /* Figure out the affinity to use to create a key from the results
1648 ** of the expression. affinityStr stores a static string suitable for
danielk1977ededfd52004-06-17 07:53:01 +00001649 ** P3 of OP_MakeRecord.
danielk1977e014a832004-05-17 10:48:57 +00001650 */
drh94a11212004-09-25 13:12:14 +00001651 affinity = comparisonAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +00001652
danielk19774adee202004-05-08 08:23:19 +00001653 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
danielk1977e014a832004-05-17 10:48:57 +00001654
1655 /* Code the <expr> from "<expr> IN (...)". The temporary table
1656 ** pExpr->iTable contains the values that make up the (...) set.
1657 */
danielk19774adee202004-05-08 08:23:19 +00001658 sqlite3ExprCode(pParse, pExpr->pLeft);
1659 addr = sqlite3VdbeCurrentAddr(v);
danielk1977e014a832004-05-17 10:48:57 +00001660 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); /* addr + 0 */
danielk19774adee202004-05-08 08:23:19 +00001661 sqlite3VdbeAddOp(v, OP_Pop, 2, 0);
drhf0863fe2005-06-12 21:35:51 +00001662 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +00001663 sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7);
drh94a11212004-09-25 13:12:14 +00001664 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1); /* addr + 4 */
danielk1977e014a832004-05-17 10:48:57 +00001665 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7);
1666 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); /* addr + 6 */
1667
drhfef52082000-06-06 01:50:43 +00001668 break;
1669 }
danielk197793758c82005-01-21 08:13:14 +00001670#endif
drhfef52082000-06-06 01:50:43 +00001671 case TK_BETWEEN: {
drhbe5c89a2004-07-26 00:31:09 +00001672 Expr *pLeft = pExpr->pLeft;
1673 struct ExprList_item *pLItem = pExpr->pList->a;
1674 Expr *pRight = pLItem->pExpr;
1675 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001676 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001677 sqlite3ExprCode(pParse, pRight);
1678 codeCompare(pParse, pLeft, pRight, OP_Ge, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001679 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
drhbe5c89a2004-07-26 00:31:09 +00001680 pLItem++;
1681 pRight = pLItem->pExpr;
1682 sqlite3ExprCode(pParse, pRight);
1683 codeCompare(pParse, pLeft, pRight, OP_Le, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001684 sqlite3VdbeAddOp(v, OP_And, 0, 0);
drhfef52082000-06-06 01:50:43 +00001685 break;
1686 }
drh51e9a442004-01-16 16:42:53 +00001687 case TK_UPLUS:
drha2e00042002-01-22 03:13:42 +00001688 case TK_AS: {
danielk19774adee202004-05-08 08:23:19 +00001689 sqlite3ExprCode(pParse, pExpr->pLeft);
drha2e00042002-01-22 03:13:42 +00001690 break;
1691 }
drh17a7f8d2002-03-24 13:13:27 +00001692 case TK_CASE: {
1693 int expr_end_label;
drhf5905aa2002-05-26 20:54:33 +00001694 int jumpInst;
1695 int addr;
1696 int nExpr;
drh17a7f8d2002-03-24 13:13:27 +00001697 int i;
drhbe5c89a2004-07-26 00:31:09 +00001698 ExprList *pEList;
1699 struct ExprList_item *aListelem;
drh17a7f8d2002-03-24 13:13:27 +00001700
1701 assert(pExpr->pList);
1702 assert((pExpr->pList->nExpr % 2) == 0);
1703 assert(pExpr->pList->nExpr > 0);
drhbe5c89a2004-07-26 00:31:09 +00001704 pEList = pExpr->pList;
1705 aListelem = pEList->a;
1706 nExpr = pEList->nExpr;
danielk19774adee202004-05-08 08:23:19 +00001707 expr_end_label = sqlite3VdbeMakeLabel(v);
drh17a7f8d2002-03-24 13:13:27 +00001708 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001709 sqlite3ExprCode(pParse, pExpr->pLeft);
drh17a7f8d2002-03-24 13:13:27 +00001710 }
drhf5905aa2002-05-26 20:54:33 +00001711 for(i=0; i<nExpr; i=i+2){
drhbe5c89a2004-07-26 00:31:09 +00001712 sqlite3ExprCode(pParse, aListelem[i].pExpr);
drh17a7f8d2002-03-24 13:13:27 +00001713 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001714 sqlite3VdbeAddOp(v, OP_Dup, 1, 1);
drhbe5c89a2004-07-26 00:31:09 +00001715 jumpInst = codeCompare(pParse, pExpr->pLeft, aListelem[i].pExpr,
1716 OP_Ne, 0, 1);
danielk19774adee202004-05-08 08:23:19 +00001717 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001718 }else{
danielk19774adee202004-05-08 08:23:19 +00001719 jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001720 }
drhbe5c89a2004-07-26 00:31:09 +00001721 sqlite3ExprCode(pParse, aListelem[i+1].pExpr);
danielk19774adee202004-05-08 08:23:19 +00001722 sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label);
1723 addr = sqlite3VdbeCurrentAddr(v);
1724 sqlite3VdbeChangeP2(v, jumpInst, addr);
drh17a7f8d2002-03-24 13:13:27 +00001725 }
drhf570f012002-05-31 15:51:25 +00001726 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001727 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhf570f012002-05-31 15:51:25 +00001728 }
drh17a7f8d2002-03-24 13:13:27 +00001729 if( pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001730 sqlite3ExprCode(pParse, pExpr->pRight);
drh17a7f8d2002-03-24 13:13:27 +00001731 }else{
drhf0863fe2005-06-12 21:35:51 +00001732 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
drh17a7f8d2002-03-24 13:13:27 +00001733 }
danielk19774adee202004-05-08 08:23:19 +00001734 sqlite3VdbeResolveLabel(v, expr_end_label);
danielk19776f349032002-06-11 02:25:40 +00001735 break;
1736 }
danielk19775338a5f2005-01-20 13:03:10 +00001737#ifndef SQLITE_OMIT_TRIGGER
danielk19776f349032002-06-11 02:25:40 +00001738 case TK_RAISE: {
1739 if( !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +00001740 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00001741 "RAISE() may only be used within a trigger-program");
danielk19776f349032002-06-11 02:25:40 +00001742 return;
1743 }
drhad6d9462004-09-19 02:15:24 +00001744 if( pExpr->iColumn!=OE_Ignore ){
1745 assert( pExpr->iColumn==OE_Rollback ||
1746 pExpr->iColumn == OE_Abort ||
1747 pExpr->iColumn == OE_Fail );
drhd2687b72005-08-12 22:56:09 +00001748 sqlite3DequoteExpr(pExpr);
drhad6d9462004-09-19 02:15:24 +00001749 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn,
1750 pExpr->token.z, pExpr->token.n);
danielk19776f349032002-06-11 02:25:40 +00001751 } else {
drhad6d9462004-09-19 02:15:24 +00001752 assert( pExpr->iColumn == OE_Ignore );
1753 sqlite3VdbeAddOp(v, OP_ContextPop, 0, 0);
1754 sqlite3VdbeAddOp(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
1755 VdbeComment((v, "# raise(IGNORE)"));
danielk19776f349032002-06-11 02:25:40 +00001756 }
drh17a7f8d2002-03-24 13:13:27 +00001757 }
danielk19775338a5f2005-01-20 13:03:10 +00001758#endif
drh17a7f8d2002-03-24 13:13:27 +00001759 break;
drhcce7d172000-05-31 15:34:51 +00001760 }
drhcce7d172000-05-31 15:34:51 +00001761}
1762
danielk197793758c82005-01-21 08:13:14 +00001763#ifndef SQLITE_OMIT_TRIGGER
drhcce7d172000-05-31 15:34:51 +00001764/*
drh25303782004-12-07 15:41:48 +00001765** Generate code that evalutes the given expression and leaves the result
1766** on the stack. See also sqlite3ExprCode().
1767**
1768** This routine might also cache the result and modify the pExpr tree
1769** so that it will make use of the cached result on subsequent evaluations
1770** rather than evaluate the whole expression again. Trivial expressions are
1771** not cached. If the expression is cached, its result is stored in a
1772** memory location.
1773*/
1774void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr){
1775 Vdbe *v = pParse->pVdbe;
1776 int iMem;
1777 int addr1, addr2;
1778 if( v==0 ) return;
1779 addr1 = sqlite3VdbeCurrentAddr(v);
1780 sqlite3ExprCode(pParse, pExpr);
1781 addr2 = sqlite3VdbeCurrentAddr(v);
1782 if( addr2>addr1+1 || sqlite3VdbeGetOp(v, addr1)->opcode==OP_Function ){
1783 iMem = pExpr->iTable = pParse->nMem++;
1784 sqlite3VdbeAddOp(v, OP_MemStore, iMem, 0);
1785 pExpr->op = TK_REGISTER;
1786 }
1787}
danielk197793758c82005-01-21 08:13:14 +00001788#endif
drh25303782004-12-07 15:41:48 +00001789
1790/*
drh268380c2004-02-25 13:47:31 +00001791** Generate code that pushes the value of every element of the given
drhf9b596e2004-05-26 16:54:42 +00001792** expression list onto the stack.
drh268380c2004-02-25 13:47:31 +00001793**
1794** Return the number of elements pushed onto the stack.
1795*/
danielk19774adee202004-05-08 08:23:19 +00001796int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00001797 Parse *pParse, /* Parsing context */
drhf9b596e2004-05-26 16:54:42 +00001798 ExprList *pList /* The expression list to be coded */
drh268380c2004-02-25 13:47:31 +00001799){
1800 struct ExprList_item *pItem;
1801 int i, n;
drh268380c2004-02-25 13:47:31 +00001802 if( pList==0 ) return 0;
drh268380c2004-02-25 13:47:31 +00001803 n = pList->nExpr;
drhc182d162005-08-14 20:47:16 +00001804 for(pItem=pList->a, i=n; i>0; i--, pItem++){
danielk19774adee202004-05-08 08:23:19 +00001805 sqlite3ExprCode(pParse, pItem->pExpr);
drh268380c2004-02-25 13:47:31 +00001806 }
drhf9b596e2004-05-26 16:54:42 +00001807 return n;
drh268380c2004-02-25 13:47:31 +00001808}
1809
1810/*
drhcce7d172000-05-31 15:34:51 +00001811** Generate code for a boolean expression such that a jump is made
1812** to the label "dest" if the expression is true but execution
1813** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00001814**
1815** If the expression evaluates to NULL (neither true nor false), then
1816** take the jump if the jumpIfNull flag is true.
drhf2bc0132004-10-04 13:19:23 +00001817**
1818** This code depends on the fact that certain token values (ex: TK_EQ)
1819** are the same as opcode values (ex: OP_Eq) that implement the corresponding
1820** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
1821** the make process cause these values to align. Assert()s in the code
1822** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00001823*/
danielk19774adee202004-05-08 08:23:19 +00001824void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001825 Vdbe *v = pParse->pVdbe;
1826 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001827 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00001828 op = pExpr->op;
1829 switch( op ){
drhcce7d172000-05-31 15:34:51 +00001830 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001831 int d2 = sqlite3VdbeMakeLabel(v);
1832 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
1833 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
1834 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001835 break;
1836 }
1837 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001838 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
1839 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001840 break;
1841 }
1842 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001843 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001844 break;
1845 }
1846 case TK_LT:
1847 case TK_LE:
1848 case TK_GT:
1849 case TK_GE:
1850 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00001851 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00001852 assert( TK_LT==OP_Lt );
1853 assert( TK_LE==OP_Le );
1854 assert( TK_GT==OP_Gt );
1855 assert( TK_GE==OP_Ge );
1856 assert( TK_EQ==OP_Eq );
1857 assert( TK_NE==OP_Ne );
danielk19774adee202004-05-08 08:23:19 +00001858 sqlite3ExprCode(pParse, pExpr->pLeft);
1859 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001860 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001861 break;
1862 }
1863 case TK_ISNULL:
1864 case TK_NOTNULL: {
drhf2bc0132004-10-04 13:19:23 +00001865 assert( TK_ISNULL==OP_IsNull );
1866 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00001867 sqlite3ExprCode(pParse, pExpr->pLeft);
1868 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001869 break;
1870 }
drhfef52082000-06-06 01:50:43 +00001871 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00001872 /* The expression "x BETWEEN y AND z" is implemented as:
1873 **
1874 ** 1 IF (x < y) GOTO 3
1875 ** 2 IF (x <= z) GOTO <dest>
1876 ** 3 ...
1877 */
drhf5905aa2002-05-26 20:54:33 +00001878 int addr;
drhbe5c89a2004-07-26 00:31:09 +00001879 Expr *pLeft = pExpr->pLeft;
1880 Expr *pRight = pExpr->pList->a[0].pExpr;
1881 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001882 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001883 sqlite3ExprCode(pParse, pRight);
1884 addr = codeCompare(pParse, pLeft, pRight, OP_Lt, 0, !jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00001885
drhbe5c89a2004-07-26 00:31:09 +00001886 pRight = pExpr->pList->a[1].pExpr;
1887 sqlite3ExprCode(pParse, pRight);
1888 codeCompare(pParse, pLeft, pRight, OP_Le, dest, jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00001889
danielk19774adee202004-05-08 08:23:19 +00001890 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
1891 sqlite3VdbeChangeP2(v, addr, sqlite3VdbeCurrentAddr(v));
1892 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00001893 break;
1894 }
drhcce7d172000-05-31 15:34:51 +00001895 default: {
danielk19774adee202004-05-08 08:23:19 +00001896 sqlite3ExprCode(pParse, pExpr);
1897 sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001898 break;
1899 }
1900 }
1901}
1902
1903/*
drh66b89c82000-11-28 20:47:17 +00001904** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00001905** to the label "dest" if the expression is false but execution
1906** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00001907**
1908** If the expression evaluates to NULL (neither true nor false) then
1909** jump if jumpIfNull is true or fall through if jumpIfNull is false.
drhcce7d172000-05-31 15:34:51 +00001910*/
danielk19774adee202004-05-08 08:23:19 +00001911void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001912 Vdbe *v = pParse->pVdbe;
1913 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001914 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00001915
1916 /* The value of pExpr->op and op are related as follows:
1917 **
1918 ** pExpr->op op
1919 ** --------- ----------
1920 ** TK_ISNULL OP_NotNull
1921 ** TK_NOTNULL OP_IsNull
1922 ** TK_NE OP_Eq
1923 ** TK_EQ OP_Ne
1924 ** TK_GT OP_Le
1925 ** TK_LE OP_Gt
1926 ** TK_GE OP_Lt
1927 ** TK_LT OP_Ge
1928 **
1929 ** For other values of pExpr->op, op is undefined and unused.
1930 ** The value of TK_ and OP_ constants are arranged such that we
1931 ** can compute the mapping above using the following expression.
1932 ** Assert()s verify that the computation is correct.
1933 */
1934 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
1935
1936 /* Verify correct alignment of TK_ and OP_ constants
1937 */
1938 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
1939 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
1940 assert( pExpr->op!=TK_NE || op==OP_Eq );
1941 assert( pExpr->op!=TK_EQ || op==OP_Ne );
1942 assert( pExpr->op!=TK_LT || op==OP_Ge );
1943 assert( pExpr->op!=TK_LE || op==OP_Gt );
1944 assert( pExpr->op!=TK_GT || op==OP_Le );
1945 assert( pExpr->op!=TK_GE || op==OP_Lt );
1946
drhcce7d172000-05-31 15:34:51 +00001947 switch( pExpr->op ){
1948 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001949 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
1950 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001951 break;
1952 }
1953 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001954 int d2 = sqlite3VdbeMakeLabel(v);
1955 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
1956 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
1957 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001958 break;
1959 }
1960 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001961 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001962 break;
1963 }
1964 case TK_LT:
1965 case TK_LE:
1966 case TK_GT:
1967 case TK_GE:
1968 case TK_NE:
1969 case TK_EQ: {
danielk19774adee202004-05-08 08:23:19 +00001970 sqlite3ExprCode(pParse, pExpr->pLeft);
1971 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001972 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001973 break;
1974 }
drhcce7d172000-05-31 15:34:51 +00001975 case TK_ISNULL:
1976 case TK_NOTNULL: {
danielk19774adee202004-05-08 08:23:19 +00001977 sqlite3ExprCode(pParse, pExpr->pLeft);
1978 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001979 break;
1980 }
drhfef52082000-06-06 01:50:43 +00001981 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00001982 /* The expression is "x BETWEEN y AND z". It is implemented as:
1983 **
1984 ** 1 IF (x >= y) GOTO 3
1985 ** 2 GOTO <dest>
1986 ** 3 IF (x > z) GOTO <dest>
1987 */
drhfef52082000-06-06 01:50:43 +00001988 int addr;
drhbe5c89a2004-07-26 00:31:09 +00001989 Expr *pLeft = pExpr->pLeft;
1990 Expr *pRight = pExpr->pList->a[0].pExpr;
1991 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001992 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001993 sqlite3ExprCode(pParse, pRight);
danielk19774adee202004-05-08 08:23:19 +00001994 addr = sqlite3VdbeCurrentAddr(v);
drhbe5c89a2004-07-26 00:31:09 +00001995 codeCompare(pParse, pLeft, pRight, OP_Ge, addr+3, !jumpIfNull);
1996
danielk19774adee202004-05-08 08:23:19 +00001997 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
1998 sqlite3VdbeAddOp(v, OP_Goto, 0, dest);
drhbe5c89a2004-07-26 00:31:09 +00001999 pRight = pExpr->pList->a[1].pExpr;
2000 sqlite3ExprCode(pParse, pRight);
2001 codeCompare(pParse, pLeft, pRight, OP_Gt, dest, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00002002 break;
2003 }
drhcce7d172000-05-31 15:34:51 +00002004 default: {
danielk19774adee202004-05-08 08:23:19 +00002005 sqlite3ExprCode(pParse, pExpr);
2006 sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00002007 break;
2008 }
2009 }
2010}
drh22827922000-06-06 17:27:05 +00002011
2012/*
2013** Do a deep comparison of two expression trees. Return TRUE (non-zero)
2014** if they are identical and return FALSE if they differ in any way.
2015*/
danielk19774adee202004-05-08 08:23:19 +00002016int sqlite3ExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00002017 int i;
2018 if( pA==0 ){
2019 return pB==0;
2020 }else if( pB==0 ){
2021 return 0;
2022 }
2023 if( pA->op!=pB->op ) return 0;
danielk19774adee202004-05-08 08:23:19 +00002024 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
2025 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00002026 if( pA->pList ){
2027 if( pB->pList==0 ) return 0;
2028 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
2029 for(i=0; i<pA->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00002030 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00002031 return 0;
2032 }
2033 }
2034 }else if( pB->pList ){
2035 return 0;
2036 }
2037 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00002038 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drh22827922000-06-06 17:27:05 +00002039 if( pA->token.z ){
2040 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00002041 if( pB->token.n!=pA->token.n ) return 0;
danielk19774adee202004-05-08 08:23:19 +00002042 if( sqlite3StrNICmp(pA->token.z, pB->token.z, pB->token.n)!=0 ) return 0;
drh22827922000-06-06 17:27:05 +00002043 }
2044 return 1;
2045}
2046
2047/*
2048** Add a new element to the pParse->aAgg[] array and return its index.
drh73b211a2005-01-18 04:00:42 +00002049** The new element is initialized to zero. The calling function is
2050** expected to fill it in.
drh22827922000-06-06 17:27:05 +00002051*/
2052static int appendAggInfo(Parse *pParse){
2053 if( (pParse->nAgg & 0x7)==0 ){
2054 int amt = pParse->nAgg + 8;
drh6d4abfb2001-10-22 02:58:08 +00002055 AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0]));
2056 if( aAgg==0 ){
drh22827922000-06-06 17:27:05 +00002057 return -1;
2058 }
drh6d4abfb2001-10-22 02:58:08 +00002059 pParse->aAgg = aAgg;
drh22827922000-06-06 17:27:05 +00002060 }
2061 memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0]));
2062 return pParse->nAgg++;
2063}
2064
2065/*
drh626a8792005-01-17 22:08:19 +00002066** This is an xFunc for walkExprTree() used to implement
2067** sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates
2068** for additional information.
drh22827922000-06-06 17:27:05 +00002069**
drh626a8792005-01-17 22:08:19 +00002070** This routine analyzes the aggregate function at pExpr.
drh22827922000-06-06 17:27:05 +00002071*/
drh626a8792005-01-17 22:08:19 +00002072static int analyzeAggregate(void *pArg, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00002073 int i;
drh5d9a4af2005-08-30 00:54:01 +00002074 AggExpr *pAgg;
danielk1977a58fdfb2005-02-08 07:50:40 +00002075 NameContext *pNC = (NameContext *)pArg;
2076 Parse *pParse = pNC->pParse;
2077 SrcList *pSrcList = pNC->pSrcList;
drh5d9a4af2005-08-30 00:54:01 +00002078 Expr *pAggExpr;
drh22827922000-06-06 17:27:05 +00002079
drh22827922000-06-06 17:27:05 +00002080 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00002081 case TK_COLUMN: {
danielk1977a58fdfb2005-02-08 07:50:40 +00002082 for(i=0; pSrcList && i<pSrcList->nSrc; i++){
2083 if( pExpr->iTable==pSrcList->a[i].iCursor ){
drh5d9a4af2005-08-30 00:54:01 +00002084 pAgg = pParse->aAgg;
2085 for(i=0; i<pParse->nAgg; i++, pAgg++){
2086 if( pAgg->isAgg ) continue;
2087 if( (pAggExpr = pAgg->pExpr)->iTable==pExpr->iTable
2088 && pAggExpr->iColumn==pExpr->iColumn ){
danielk1977a58fdfb2005-02-08 07:50:40 +00002089 break;
2090 }
2091 }
2092 if( i>=pParse->nAgg ){
2093 i = appendAggInfo(pParse);
2094 if( i<0 ) return 1;
drh5d9a4af2005-08-30 00:54:01 +00002095 pAgg = &pParse->aAgg[i];
2096 pAgg->isAgg = 0;
2097 pAgg->pExpr = pExpr;
danielk1977a58fdfb2005-02-08 07:50:40 +00002098 }
2099 pExpr->iAgg = i;
2100 pExpr->iAggCtx = pNC->nDepth;
2101 return 1;
drh22827922000-06-06 17:27:05 +00002102 }
2103 }
drh626a8792005-01-17 22:08:19 +00002104 return 1;
drh22827922000-06-06 17:27:05 +00002105 }
2106 case TK_AGG_FUNCTION: {
danielk1977a58fdfb2005-02-08 07:50:40 +00002107 if( pNC->nDepth==0 ){
drh5d9a4af2005-08-30 00:54:01 +00002108 pAgg = pParse->aAgg;
2109 for(i=0; i<pParse->nAgg; i++, pAgg++){
2110 if( !pAgg->isAgg ) continue;
2111 if( sqlite3ExprCompare(pAgg->pExpr, pExpr) ){
danielk1977a58fdfb2005-02-08 07:50:40 +00002112 break;
2113 }
drh22827922000-06-06 17:27:05 +00002114 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002115 if( i>=pParse->nAgg ){
2116 u8 enc = pParse->db->enc;
2117 i = appendAggInfo(pParse);
2118 if( i<0 ) return 1;
drh5d9a4af2005-08-30 00:54:01 +00002119 pAgg = &pParse->aAgg[i];
2120 pAgg->isAgg = 1;
2121 pAgg->pExpr = pExpr;
2122 pAgg->pFunc = sqlite3FindFunction(pParse->db,
danielk1977a58fdfb2005-02-08 07:50:40 +00002123 pExpr->token.z, pExpr->token.n,
2124 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
2125 }
2126 pExpr->iAgg = i;
2127 return 1;
drh22827922000-06-06 17:27:05 +00002128 }
drh22827922000-06-06 17:27:05 +00002129 }
2130 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002131 if( pExpr->pSelect ){
2132 pNC->nDepth++;
2133 walkSelectExpr(pExpr->pSelect, analyzeAggregate, pNC);
2134 pNC->nDepth--;
2135 }
drh626a8792005-01-17 22:08:19 +00002136 return 0;
2137}
2138
2139/*
2140** Analyze the given expression looking for aggregate functions and
2141** for variables that need to be added to the pParse->aAgg[] array.
2142** Make additional entries to the pParse->aAgg[] array as necessary.
2143**
2144** This routine should only be called after the expression has been
2145** analyzed by sqlite3ExprResolveNames().
2146**
2147** If errors are seen, leave an error message in zErrMsg and return
2148** the number of errors.
2149*/
danielk1977a58fdfb2005-02-08 07:50:40 +00002150int sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
2151 int nErr = pNC->pParse->nErr;
2152 walkExprTree(pExpr, analyzeAggregate, pNC);
2153 return pNC->pParse->nErr - nErr;
drh22827922000-06-06 17:27:05 +00002154}
drh5d9a4af2005-08-30 00:54:01 +00002155
2156/*
2157** Call sqlite3ExprAnalyzeAggregates() for every expression in an
2158** expression list. Return the number of errors.
2159**
2160** If an error is found, the analysis is cut short.
2161*/
2162int sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){
2163 struct ExprList_item *pItem;
2164 int i;
2165 int nErr = 0;
2166 if( pList ){
2167 for(pItem=pList->a, i=0; nErr==0 && i<pList->nExpr; i++, pItem++){
2168 nErr += sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
2169 }
2170 }
2171 return nErr;
2172}