blob: 921cca343577095cff90893812dc0e7c762e179a [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**
danielk197715d79822007-05-15 07:00:34 +000015** $Id: expr.c,v 1.294 2007/05/15 07:00:34 danielk1977 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;
drh487e2622005-06-25 18:42:14 +000038 if( op==TK_SELECT ){
danielk1977bf3b7212004-05-18 10:06:24 +000039 return sqlite3ExprAffinity(pExpr->pSelect->pEList->a[0].pExpr);
danielk1977a37cdde2004-05-16 11:15:36 +000040 }
drh487e2622005-06-25 18:42:14 +000041#ifndef SQLITE_OMIT_CAST
42 if( op==TK_CAST ){
drh8a512562005-11-14 22:29:05 +000043 return sqlite3AffinityType(&pExpr->token);
drh487e2622005-06-25 18:42:14 +000044 }
45#endif
danielk1977a37cdde2004-05-16 11:15:36 +000046 return pExpr->affinity;
47}
48
drh53db1452004-05-20 13:54:53 +000049/*
drh8b4c40d2007-02-01 23:02:45 +000050** Set the collating sequence for expression pExpr to be the collating
51** sequence named by pToken. Return a pointer to the revised expression.
drha34001c2007-02-02 12:44:37 +000052** The collating sequence is marked as "explicit" using the EP_ExpCollate
53** flag. An explicit collating sequence will override implicit
54** collating sequences.
drh8b4c40d2007-02-01 23:02:45 +000055*/
56Expr *sqlite3ExprSetColl(Parse *pParse, Expr *pExpr, Token *pName){
57 CollSeq *pColl;
58 if( pExpr==0 ) return 0;
59 pColl = sqlite3LocateCollSeq(pParse, (char*)pName->z, pName->n);
60 if( pColl ){
61 pExpr->pColl = pColl;
62 pExpr->flags |= EP_ExpCollate;
63 }
64 return pExpr;
65}
66
67/*
danielk19770202b292004-06-09 09:55:16 +000068** Return the default collation sequence for the expression pExpr. If
69** there is no default collation type, return 0.
70*/
danielk19777cedc8d2004-06-10 10:50:08 +000071CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
72 CollSeq *pColl = 0;
danielk19770202b292004-06-09 09:55:16 +000073 if( pExpr ){
danielk19777cedc8d2004-06-10 10:50:08 +000074 pColl = pExpr->pColl;
drh4f07e5f2007-05-14 11:34:46 +000075 if( pExpr->op==TK_CAST && !pColl ){
danielk19777cedc8d2004-06-10 10:50:08 +000076 return sqlite3ExprCollSeq(pParse, pExpr->pLeft);
danielk19770202b292004-06-09 09:55:16 +000077 }
78 }
danielk19777cedc8d2004-06-10 10:50:08 +000079 if( sqlite3CheckCollSeq(pParse, pColl) ){
80 pColl = 0;
81 }
82 return pColl;
danielk19770202b292004-06-09 09:55:16 +000083}
84
85/*
drh626a8792005-01-17 22:08:19 +000086** pExpr is an operand of a comparison operator. aff2 is the
87** type affinity of the other operand. This routine returns the
drh53db1452004-05-20 13:54:53 +000088** type affinity that should be used for the comparison operator.
89*/
danielk1977e014a832004-05-17 10:48:57 +000090char sqlite3CompareAffinity(Expr *pExpr, char aff2){
danielk1977bf3b7212004-05-18 10:06:24 +000091 char aff1 = sqlite3ExprAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +000092 if( aff1 && aff2 ){
drh8df447f2005-11-01 15:48:24 +000093 /* Both sides of the comparison are columns. If one has numeric
94 ** affinity, use that. Otherwise use no affinity.
danielk1977e014a832004-05-17 10:48:57 +000095 */
drh8a512562005-11-14 22:29:05 +000096 if( sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2) ){
danielk1977e014a832004-05-17 10:48:57 +000097 return SQLITE_AFF_NUMERIC;
98 }else{
99 return SQLITE_AFF_NONE;
100 }
101 }else if( !aff1 && !aff2 ){
drh5f6a87b2004-07-19 00:39:45 +0000102 /* Neither side of the comparison is a column. Compare the
103 ** results directly.
danielk1977e014a832004-05-17 10:48:57 +0000104 */
drh5f6a87b2004-07-19 00:39:45 +0000105 return SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +0000106 }else{
107 /* One side is a column, the other is not. Use the columns affinity. */
drhfe05af82005-07-21 03:14:59 +0000108 assert( aff1==0 || aff2==0 );
danielk1977e014a832004-05-17 10:48:57 +0000109 return (aff1 + aff2);
110 }
111}
112
drh53db1452004-05-20 13:54:53 +0000113/*
114** pExpr is a comparison operator. Return the type affinity that should
115** be applied to both operands prior to doing the comparison.
116*/
danielk1977e014a832004-05-17 10:48:57 +0000117static char comparisonAffinity(Expr *pExpr){
118 char aff;
119 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
120 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
121 pExpr->op==TK_NE );
122 assert( pExpr->pLeft );
danielk1977bf3b7212004-05-18 10:06:24 +0000123 aff = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +0000124 if( pExpr->pRight ){
125 aff = sqlite3CompareAffinity(pExpr->pRight, aff);
126 }
127 else if( pExpr->pSelect ){
128 aff = sqlite3CompareAffinity(pExpr->pSelect->pEList->a[0].pExpr, aff);
129 }
130 else if( !aff ){
drhde087bd2007-02-23 03:00:44 +0000131 aff = SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +0000132 }
133 return aff;
134}
135
136/*
137** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
138** idx_affinity is the affinity of an indexed column. Return true
139** if the index with affinity idx_affinity may be used to implement
140** the comparison in pExpr.
141*/
142int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){
143 char aff = comparisonAffinity(pExpr);
drh8a512562005-11-14 22:29:05 +0000144 switch( aff ){
145 case SQLITE_AFF_NONE:
146 return 1;
147 case SQLITE_AFF_TEXT:
148 return idx_affinity==SQLITE_AFF_TEXT;
149 default:
150 return sqlite3IsNumericAffinity(idx_affinity);
151 }
danielk1977e014a832004-05-17 10:48:57 +0000152}
153
danielk1977a37cdde2004-05-16 11:15:36 +0000154/*
155** Return the P1 value that should be used for a binary comparison
156** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
157** If jumpIfNull is true, then set the low byte of the returned
158** P1 value to tell the opcode to jump if either expression
159** evaluates to NULL.
160*/
danielk1977e014a832004-05-17 10:48:57 +0000161static int binaryCompareP1(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){
danielk1977bf3b7212004-05-18 10:06:24 +0000162 char aff = sqlite3ExprAffinity(pExpr2);
drhf0863fe2005-06-12 21:35:51 +0000163 return ((int)sqlite3CompareAffinity(pExpr1, aff))+(jumpIfNull?0x100:0);
danielk1977a37cdde2004-05-16 11:15:36 +0000164}
165
drha2e00042002-01-22 03:13:42 +0000166/*
danielk19770202b292004-06-09 09:55:16 +0000167** Return a pointer to the collation sequence that should be used by
168** a binary comparison operator comparing pLeft and pRight.
169**
170** If the left hand expression has a collating sequence type, then it is
171** used. Otherwise the collation sequence for the right hand expression
172** is used, or the default (BINARY) if neither expression has a collating
173** type.
174*/
danielk19777cedc8d2004-06-10 10:50:08 +0000175static CollSeq* binaryCompareCollSeq(Parse *pParse, Expr *pLeft, Expr *pRight){
drhec41dda2007-02-07 13:09:45 +0000176 CollSeq *pColl;
177 assert( pLeft );
178 assert( pRight );
179 if( pLeft->flags & EP_ExpCollate ){
180 assert( pLeft->pColl );
181 pColl = pLeft->pColl;
182 }else if( pRight->flags & EP_ExpCollate ){
183 assert( pRight->pColl );
184 pColl = pRight->pColl;
185 }else{
186 pColl = sqlite3ExprCollSeq(pParse, pLeft);
187 if( !pColl ){
188 pColl = sqlite3ExprCollSeq(pParse, pRight);
189 }
danielk19770202b292004-06-09 09:55:16 +0000190 }
191 return pColl;
192}
193
194/*
drhbe5c89a2004-07-26 00:31:09 +0000195** Generate code for a comparison operator.
196*/
197static int codeCompare(
198 Parse *pParse, /* The parsing (and code generating) context */
199 Expr *pLeft, /* The left operand */
200 Expr *pRight, /* The right operand */
201 int opcode, /* The comparison opcode */
202 int dest, /* Jump here if true. */
203 int jumpIfNull /* If true, jump if either operand is NULL */
204){
205 int p1 = binaryCompareP1(pLeft, pRight, jumpIfNull);
206 CollSeq *p3 = binaryCompareCollSeq(pParse, pLeft, pRight);
drh94a11212004-09-25 13:12:14 +0000207 return sqlite3VdbeOp3(pParse->pVdbe, opcode, p1, dest, (void*)p3, P3_COLLSEQ);
drhbe5c89a2004-07-26 00:31:09 +0000208}
209
210/*
drha76b5df2002-02-23 02:32:10 +0000211** Construct a new expression node and return a pointer to it. Memory
212** for this node is obtained from sqliteMalloc(). The calling function
213** is responsible for making sure the node eventually gets freed.
214*/
drhe4e72072004-11-23 01:47:30 +0000215Expr *sqlite3Expr(int op, Expr *pLeft, Expr *pRight, const Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000216 Expr *pNew;
217 pNew = sqliteMalloc( sizeof(Expr) );
218 if( pNew==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000219 /* When malloc fails, delete pLeft and pRight. Expressions passed to
220 ** this function must always be allocated with sqlite3Expr() for this
221 ** reason.
222 */
223 sqlite3ExprDelete(pLeft);
224 sqlite3ExprDelete(pRight);
drha76b5df2002-02-23 02:32:10 +0000225 return 0;
226 }
227 pNew->op = op;
228 pNew->pLeft = pLeft;
229 pNew->pRight = pRight;
danielk1977a58fdfb2005-02-08 07:50:40 +0000230 pNew->iAgg = -1;
drha76b5df2002-02-23 02:32:10 +0000231 if( pToken ){
drh4b59ab52002-08-24 18:24:51 +0000232 assert( pToken->dyn==0 );
drh145716b2004-09-24 12:24:06 +0000233 pNew->span = pNew->token = *pToken;
drha34001c2007-02-02 12:44:37 +0000234 }else if( pLeft ){
235 if( pRight ){
236 sqlite3ExprSpan(pNew, &pLeft->span, &pRight->span);
drh5ffb3ac2007-04-18 17:07:57 +0000237 if( pRight->flags & EP_ExpCollate ){
drha34001c2007-02-02 12:44:37 +0000238 pNew->flags |= EP_ExpCollate;
239 pNew->pColl = pRight->pColl;
240 }
241 }
drh5ffb3ac2007-04-18 17:07:57 +0000242 if( pLeft->flags & EP_ExpCollate ){
drha34001c2007-02-02 12:44:37 +0000243 pNew->flags |= EP_ExpCollate;
244 pNew->pColl = pLeft->pColl;
245 }
drha76b5df2002-02-23 02:32:10 +0000246 }
danielk1977fc976062007-05-10 10:46:56 +0000247
248 sqlite3ExprSetHeight(pNew);
drha76b5df2002-02-23 02:32:10 +0000249 return pNew;
250}
251
252/*
drh206f3d92006-07-11 13:15:08 +0000253** Works like sqlite3Expr() but frees its pLeft and pRight arguments
254** if it fails due to a malloc problem.
255*/
256Expr *sqlite3ExprOrFree(int op, Expr *pLeft, Expr *pRight, const Token *pToken){
257 Expr *pNew = sqlite3Expr(op, pLeft, pRight, pToken);
258 if( pNew==0 ){
259 sqlite3ExprDelete(pLeft);
260 sqlite3ExprDelete(pRight);
261 }
262 return pNew;
263}
264
265/*
drh4e0cff62004-11-05 05:10:28 +0000266** When doing a nested parse, you can include terms in an expression
267** that look like this: #0 #1 #2 ... These terms refer to elements
drh288d37f2005-06-22 08:48:06 +0000268** on the stack. "#0" means the top of the stack.
269** "#1" means the next down on the stack. And so forth.
drh4e0cff62004-11-05 05:10:28 +0000270**
271** This routine is called by the parser to deal with on of those terms.
272** It immediately generates code to store the value in a memory location.
273** The returns an expression that will code to extract the value from
274** that memory location as needed.
275*/
276Expr *sqlite3RegisterExpr(Parse *pParse, Token *pToken){
277 Vdbe *v = pParse->pVdbe;
278 Expr *p;
279 int depth;
drh4e0cff62004-11-05 05:10:28 +0000280 if( pParse->nested==0 ){
281 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", pToken);
drh4e05c832007-05-11 01:44:50 +0000282 return sqlite3Expr(TK_NULL, 0, 0, 0);
drh4e0cff62004-11-05 05:10:28 +0000283 }
drhbb7ac002005-08-12 22:58:53 +0000284 if( v==0 ) return 0;
drh4e0cff62004-11-05 05:10:28 +0000285 p = sqlite3Expr(TK_REGISTER, 0, 0, pToken);
drh73c42a12004-11-20 18:13:10 +0000286 if( p==0 ){
287 return 0; /* Malloc failed */
288 }
drh2646da72005-12-09 20:02:05 +0000289 depth = atoi((char*)&pToken->z[1]);
drh288d37f2005-06-22 08:48:06 +0000290 p->iTable = pParse->nMem++;
291 sqlite3VdbeAddOp(v, OP_Dup, depth, 0);
292 sqlite3VdbeAddOp(v, OP_MemStore, p->iTable, 1);
drh4e0cff62004-11-05 05:10:28 +0000293 return p;
294}
295
296/*
drh91bb0ee2004-09-01 03:06:34 +0000297** Join two expressions using an AND operator. If either expression is
298** NULL, then just return the other expression.
299*/
300Expr *sqlite3ExprAnd(Expr *pLeft, Expr *pRight){
301 if( pLeft==0 ){
302 return pRight;
303 }else if( pRight==0 ){
304 return pLeft;
305 }else{
306 return sqlite3Expr(TK_AND, pLeft, pRight, 0);
307 }
308}
309
310/*
drh6977fea2002-10-22 23:38:04 +0000311** Set the Expr.span field of the given expression to span all
drha76b5df2002-02-23 02:32:10 +0000312** text between the two given tokens.
313*/
danielk19774adee202004-05-08 08:23:19 +0000314void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
drh4efc4752004-01-16 15:55:37 +0000315 assert( pRight!=0 );
316 assert( pLeft!=0 );
danielk19779e128002006-01-18 16:51:35 +0000317 if( !sqlite3MallocFailed() && pRight->z && pLeft->z ){
drhad6d9462004-09-19 02:15:24 +0000318 assert( pLeft->dyn==0 || pLeft->z[pLeft->n]==0 );
drh145716b2004-09-24 12:24:06 +0000319 if( pLeft->dyn==0 && pRight->dyn==0 ){
drh6977fea2002-10-22 23:38:04 +0000320 pExpr->span.z = pLeft->z;
drh97903fe2005-05-24 20:19:57 +0000321 pExpr->span.n = pRight->n + (pRight->z - pLeft->z);
drh4b59ab52002-08-24 18:24:51 +0000322 }else{
drh6977fea2002-10-22 23:38:04 +0000323 pExpr->span.z = 0;
drh4b59ab52002-08-24 18:24:51 +0000324 }
drha76b5df2002-02-23 02:32:10 +0000325 }
326}
327
328/*
329** Construct a new expression node for a function with multiple
330** arguments.
331*/
danielk19774adee202004-05-08 08:23:19 +0000332Expr *sqlite3ExprFunction(ExprList *pList, Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000333 Expr *pNew;
danielk19774b202ae2006-01-23 05:50:58 +0000334 assert( pToken );
drha76b5df2002-02-23 02:32:10 +0000335 pNew = sqliteMalloc( sizeof(Expr) );
336 if( pNew==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000337 sqlite3ExprListDelete(pList); /* Avoid leaking memory when malloc fails */
drha76b5df2002-02-23 02:32:10 +0000338 return 0;
339 }
340 pNew->op = TK_FUNCTION;
341 pNew->pList = pList;
danielk19774b202ae2006-01-23 05:50:58 +0000342 assert( pToken->dyn==0 );
343 pNew->token = *pToken;
drh6977fea2002-10-22 23:38:04 +0000344 pNew->span = pNew->token;
danielk1977fc976062007-05-10 10:46:56 +0000345
346 sqlite3ExprSetHeight(pNew);
drha76b5df2002-02-23 02:32:10 +0000347 return pNew;
348}
349
350/*
drhfa6bc002004-09-07 16:19:52 +0000351** Assign a variable number to an expression that encodes a wildcard
352** in the original SQL statement.
353**
354** Wildcards consisting of a single "?" are assigned the next sequential
355** variable number.
356**
357** Wildcards of the form "?nnn" are assigned the number "nnn". We make
358** sure "nnn" is not too be to avoid a denial of service attack when
359** the SQL statement comes from an external source.
360**
361** Wildcards of the form ":aaa" or "$aaa" are assigned the same number
362** as the previous instance of the same wildcard. Or if this is the first
363** instance of the wildcard, the next sequenial variable number is
364** assigned.
365*/
366void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){
367 Token *pToken;
368 if( pExpr==0 ) return;
369 pToken = &pExpr->token;
370 assert( pToken->n>=1 );
371 assert( pToken->z!=0 );
372 assert( pToken->z[0]!=0 );
373 if( pToken->n==1 ){
374 /* Wildcard of the form "?". Assign the next variable number */
375 pExpr->iTable = ++pParse->nVar;
376 }else if( pToken->z[0]=='?' ){
377 /* Wildcard of the form "?nnn". Convert "nnn" to an integer and
378 ** use it as the variable number */
379 int i;
drh2646da72005-12-09 20:02:05 +0000380 pExpr->iTable = i = atoi((char*)&pToken->z[1]);
drhfa6bc002004-09-07 16:19:52 +0000381 if( i<1 || i>SQLITE_MAX_VARIABLE_NUMBER ){
382 sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
383 SQLITE_MAX_VARIABLE_NUMBER);
384 }
385 if( i>pParse->nVar ){
386 pParse->nVar = i;
387 }
388 }else{
389 /* Wildcards of the form ":aaa" or "$aaa". Reuse the same variable
390 ** number as the prior appearance of the same name, or if the name
391 ** has never appeared before, reuse the same variable number
392 */
393 int i, n;
394 n = pToken->n;
395 for(i=0; i<pParse->nVarExpr; i++){
396 Expr *pE;
397 if( (pE = pParse->apVarExpr[i])!=0
398 && pE->token.n==n
399 && memcmp(pE->token.z, pToken->z, n)==0 ){
400 pExpr->iTable = pE->iTable;
401 break;
402 }
403 }
404 if( i>=pParse->nVarExpr ){
405 pExpr->iTable = ++pParse->nVar;
406 if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){
407 pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10;
drhcf643722007-03-27 13:36:37 +0000408 pParse->apVarExpr = sqliteReallocOrFree(pParse->apVarExpr,
drhfa6bc002004-09-07 16:19:52 +0000409 pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0]) );
410 }
danielk19779e128002006-01-18 16:51:35 +0000411 if( !sqlite3MallocFailed() ){
drhfa6bc002004-09-07 16:19:52 +0000412 assert( pParse->apVarExpr!=0 );
413 pParse->apVarExpr[pParse->nVarExpr++] = pExpr;
414 }
415 }
416 }
danielk1977832b2662007-05-09 11:37:22 +0000417 if( !pParse->nErr && pParse->nVar>SQLITE_MAX_VARIABLE_NUMBER ){
418 sqlite3ErrorMsg(pParse, "too many SQL variables");
419 }
drhfa6bc002004-09-07 16:19:52 +0000420}
421
422/*
drha2e00042002-01-22 03:13:42 +0000423** Recursively delete an expression tree.
424*/
danielk19774adee202004-05-08 08:23:19 +0000425void sqlite3ExprDelete(Expr *p){
drha2e00042002-01-22 03:13:42 +0000426 if( p==0 ) return;
drh4efc4752004-01-16 15:55:37 +0000427 if( p->span.dyn ) sqliteFree((char*)p->span.z);
428 if( p->token.dyn ) sqliteFree((char*)p->token.z);
danielk19774adee202004-05-08 08:23:19 +0000429 sqlite3ExprDelete(p->pLeft);
430 sqlite3ExprDelete(p->pRight);
431 sqlite3ExprListDelete(p->pList);
432 sqlite3SelectDelete(p->pSelect);
drha2e00042002-01-22 03:13:42 +0000433 sqliteFree(p);
434}
435
drhd2687b72005-08-12 22:56:09 +0000436/*
437** The Expr.token field might be a string literal that is quoted.
438** If so, remove the quotation marks.
439*/
440void sqlite3DequoteExpr(Expr *p){
441 if( ExprHasAnyProperty(p, EP_Dequoted) ){
442 return;
443 }
444 ExprSetProperty(p, EP_Dequoted);
445 if( p->token.dyn==0 ){
drhd2687b72005-08-12 22:56:09 +0000446 sqlite3TokenCopy(&p->token, &p->token);
447 }
448 sqlite3Dequote((char*)p->token.z);
449}
450
drha76b5df2002-02-23 02:32:10 +0000451
452/*
drhff78bd22002-02-27 01:47:11 +0000453** The following group of routines make deep copies of expressions,
454** expression lists, ID lists, and select statements. The copies can
455** be deleted (by being passed to their respective ...Delete() routines)
456** without effecting the originals.
457**
danielk19774adee202004-05-08 08:23:19 +0000458** The expression list, ID, and source lists return by sqlite3ExprListDup(),
459** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
drhad3cab52002-05-24 02:04:32 +0000460** by subsequent calls to sqlite*ListAppend() routines.
drhff78bd22002-02-27 01:47:11 +0000461**
drhad3cab52002-05-24 02:04:32 +0000462** Any tables that the SrcList might point to are not duplicated.
drhff78bd22002-02-27 01:47:11 +0000463*/
danielk19774adee202004-05-08 08:23:19 +0000464Expr *sqlite3ExprDup(Expr *p){
drhff78bd22002-02-27 01:47:11 +0000465 Expr *pNew;
466 if( p==0 ) return 0;
drhfcb78a42003-01-18 20:11:05 +0000467 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000468 if( pNew==0 ) return 0;
drh3b167c72002-06-28 12:18:47 +0000469 memcpy(pNew, p, sizeof(*pNew));
drh6977fea2002-10-22 23:38:04 +0000470 if( p->token.z!=0 ){
drh2646da72005-12-09 20:02:05 +0000471 pNew->token.z = (u8*)sqliteStrNDup((char*)p->token.z, p->token.n);
drh4b59ab52002-08-24 18:24:51 +0000472 pNew->token.dyn = 1;
473 }else{
drh4efc4752004-01-16 15:55:37 +0000474 assert( pNew->token.z==0 );
drh4b59ab52002-08-24 18:24:51 +0000475 }
drh6977fea2002-10-22 23:38:04 +0000476 pNew->span.z = 0;
danielk19774adee202004-05-08 08:23:19 +0000477 pNew->pLeft = sqlite3ExprDup(p->pLeft);
478 pNew->pRight = sqlite3ExprDup(p->pRight);
479 pNew->pList = sqlite3ExprListDup(p->pList);
480 pNew->pSelect = sqlite3SelectDup(p->pSelect);
drhff78bd22002-02-27 01:47:11 +0000481 return pNew;
482}
danielk19774adee202004-05-08 08:23:19 +0000483void sqlite3TokenCopy(Token *pTo, Token *pFrom){
drh4b59ab52002-08-24 18:24:51 +0000484 if( pTo->dyn ) sqliteFree((char*)pTo->z);
drh4b59ab52002-08-24 18:24:51 +0000485 if( pFrom->z ){
486 pTo->n = pFrom->n;
drh2646da72005-12-09 20:02:05 +0000487 pTo->z = (u8*)sqliteStrNDup((char*)pFrom->z, pFrom->n);
drh4b59ab52002-08-24 18:24:51 +0000488 pTo->dyn = 1;
489 }else{
drh4b59ab52002-08-24 18:24:51 +0000490 pTo->z = 0;
drh4b59ab52002-08-24 18:24:51 +0000491 }
492}
danielk19774adee202004-05-08 08:23:19 +0000493ExprList *sqlite3ExprListDup(ExprList *p){
drhff78bd22002-02-27 01:47:11 +0000494 ExprList *pNew;
drh145716b2004-09-24 12:24:06 +0000495 struct ExprList_item *pItem, *pOldItem;
drhff78bd22002-02-27 01:47:11 +0000496 int i;
497 if( p==0 ) return 0;
498 pNew = sqliteMalloc( sizeof(*pNew) );
499 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000500 pNew->nExpr = pNew->nAlloc = p->nExpr;
drh3e7bc9c2004-02-21 19:17:17 +0000501 pNew->a = pItem = sqliteMalloc( p->nExpr*sizeof(p->a[0]) );
danielk1977e0048402004-06-15 16:51:01 +0000502 if( pItem==0 ){
503 sqliteFree(pNew);
504 return 0;
505 }
drh145716b2004-09-24 12:24:06 +0000506 pOldItem = p->a;
507 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
drh4b59ab52002-08-24 18:24:51 +0000508 Expr *pNewExpr, *pOldExpr;
drh145716b2004-09-24 12:24:06 +0000509 pItem->pExpr = pNewExpr = sqlite3ExprDup(pOldExpr = pOldItem->pExpr);
drh6977fea2002-10-22 23:38:04 +0000510 if( pOldExpr->span.z!=0 && pNewExpr ){
511 /* Always make a copy of the span for top-level expressions in the
drh4b59ab52002-08-24 18:24:51 +0000512 ** expression list. The logic in SELECT processing that determines
513 ** the names of columns in the result set needs this information */
danielk19774adee202004-05-08 08:23:19 +0000514 sqlite3TokenCopy(&pNewExpr->span, &pOldExpr->span);
drh4b59ab52002-08-24 18:24:51 +0000515 }
drh1f3e9052002-10-31 00:09:39 +0000516 assert( pNewExpr==0 || pNewExpr->span.z!=0
drh6f7adc82006-01-11 21:41:20 +0000517 || pOldExpr->span.z==0
danielk19779e128002006-01-18 16:51:35 +0000518 || sqlite3MallocFailed() );
drh145716b2004-09-24 12:24:06 +0000519 pItem->zName = sqliteStrDup(pOldItem->zName);
520 pItem->sortOrder = pOldItem->sortOrder;
521 pItem->isAgg = pOldItem->isAgg;
drh3e7bc9c2004-02-21 19:17:17 +0000522 pItem->done = 0;
drhff78bd22002-02-27 01:47:11 +0000523 }
524 return pNew;
525}
danielk197793758c82005-01-21 08:13:14 +0000526
527/*
528** If cursors, triggers, views and subqueries are all omitted from
529** the build, then none of the following routines, except for
530** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes
531** called with a NULL argument.
532*/
danielk19776a67fe82005-02-04 04:07:16 +0000533#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \
534 || !defined(SQLITE_OMIT_SUBQUERY)
danielk19774adee202004-05-08 08:23:19 +0000535SrcList *sqlite3SrcListDup(SrcList *p){
drhad3cab52002-05-24 02:04:32 +0000536 SrcList *pNew;
537 int i;
drh113088e2003-03-20 01:16:58 +0000538 int nByte;
drhad3cab52002-05-24 02:04:32 +0000539 if( p==0 ) return 0;
drh113088e2003-03-20 01:16:58 +0000540 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
drh4efc4752004-01-16 15:55:37 +0000541 pNew = sqliteMallocRaw( nByte );
drhad3cab52002-05-24 02:04:32 +0000542 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000543 pNew->nSrc = pNew->nAlloc = p->nSrc;
drhad3cab52002-05-24 02:04:32 +0000544 for(i=0; i<p->nSrc; i++){
drh4efc4752004-01-16 15:55:37 +0000545 struct SrcList_item *pNewItem = &pNew->a[i];
546 struct SrcList_item *pOldItem = &p->a[i];
drhed8a3bb2005-06-06 21:19:56 +0000547 Table *pTab;
drh4efc4752004-01-16 15:55:37 +0000548 pNewItem->zDatabase = sqliteStrDup(pOldItem->zDatabase);
549 pNewItem->zName = sqliteStrDup(pOldItem->zName);
550 pNewItem->zAlias = sqliteStrDup(pOldItem->zAlias);
551 pNewItem->jointype = pOldItem->jointype;
552 pNewItem->iCursor = pOldItem->iCursor;
danielk19771787cca2006-02-10 07:07:14 +0000553 pNewItem->isPopulated = pOldItem->isPopulated;
drhed8a3bb2005-06-06 21:19:56 +0000554 pTab = pNewItem->pTab = pOldItem->pTab;
555 if( pTab ){
556 pTab->nRef++;
danielk1977a1cb1832005-02-12 08:59:55 +0000557 }
danielk19774adee202004-05-08 08:23:19 +0000558 pNewItem->pSelect = sqlite3SelectDup(pOldItem->pSelect);
559 pNewItem->pOn = sqlite3ExprDup(pOldItem->pOn);
560 pNewItem->pUsing = sqlite3IdListDup(pOldItem->pUsing);
danielk19776c18b6e2005-01-30 09:17:58 +0000561 pNewItem->colUsed = pOldItem->colUsed;
drhad3cab52002-05-24 02:04:32 +0000562 }
563 return pNew;
564}
danielk19774adee202004-05-08 08:23:19 +0000565IdList *sqlite3IdListDup(IdList *p){
drhff78bd22002-02-27 01:47:11 +0000566 IdList *pNew;
567 int i;
568 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000569 pNew = sqliteMallocRaw( sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +0000570 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000571 pNew->nId = pNew->nAlloc = p->nId;
drh4efc4752004-01-16 15:55:37 +0000572 pNew->a = sqliteMallocRaw( p->nId*sizeof(p->a[0]) );
danielk1977d5d56522005-03-16 12:15:20 +0000573 if( pNew->a==0 ){
574 sqliteFree(pNew);
575 return 0;
576 }
drhff78bd22002-02-27 01:47:11 +0000577 for(i=0; i<p->nId; i++){
drh4efc4752004-01-16 15:55:37 +0000578 struct IdList_item *pNewItem = &pNew->a[i];
579 struct IdList_item *pOldItem = &p->a[i];
580 pNewItem->zName = sqliteStrDup(pOldItem->zName);
581 pNewItem->idx = pOldItem->idx;
drhff78bd22002-02-27 01:47:11 +0000582 }
583 return pNew;
584}
danielk19774adee202004-05-08 08:23:19 +0000585Select *sqlite3SelectDup(Select *p){
drhff78bd22002-02-27 01:47:11 +0000586 Select *pNew;
587 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000588 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000589 if( pNew==0 ) return 0;
590 pNew->isDistinct = p->isDistinct;
danielk19774adee202004-05-08 08:23:19 +0000591 pNew->pEList = sqlite3ExprListDup(p->pEList);
592 pNew->pSrc = sqlite3SrcListDup(p->pSrc);
593 pNew->pWhere = sqlite3ExprDup(p->pWhere);
594 pNew->pGroupBy = sqlite3ExprListDup(p->pGroupBy);
595 pNew->pHaving = sqlite3ExprDup(p->pHaving);
596 pNew->pOrderBy = sqlite3ExprListDup(p->pOrderBy);
drhff78bd22002-02-27 01:47:11 +0000597 pNew->op = p->op;
danielk19774adee202004-05-08 08:23:19 +0000598 pNew->pPrior = sqlite3SelectDup(p->pPrior);
danielk1977a2dc3b12005-02-05 12:48:48 +0000599 pNew->pLimit = sqlite3ExprDup(p->pLimit);
600 pNew->pOffset = sqlite3ExprDup(p->pOffset);
drh7b58dae2003-07-20 01:16:46 +0000601 pNew->iLimit = -1;
602 pNew->iOffset = -1;
danielk1977a1cb1832005-02-12 08:59:55 +0000603 pNew->isResolved = p->isResolved;
604 pNew->isAgg = p->isAgg;
drhb9bb7c12006-06-11 23:41:55 +0000605 pNew->usesEphm = 0;
drh8e647b82005-09-23 21:11:53 +0000606 pNew->disallowOrderBy = 0;
drh0342b1f2005-09-01 03:07:44 +0000607 pNew->pRightmost = 0;
drhb9bb7c12006-06-11 23:41:55 +0000608 pNew->addrOpenEphm[0] = -1;
609 pNew->addrOpenEphm[1] = -1;
610 pNew->addrOpenEphm[2] = -1;
drhff78bd22002-02-27 01:47:11 +0000611 return pNew;
612}
danielk197793758c82005-01-21 08:13:14 +0000613#else
614Select *sqlite3SelectDup(Select *p){
615 assert( p==0 );
616 return 0;
617}
618#endif
drhff78bd22002-02-27 01:47:11 +0000619
620
621/*
drha76b5df2002-02-23 02:32:10 +0000622** Add a new element to the end of an expression list. If pList is
623** initially NULL, then create a new expression list.
624*/
danielk19774adee202004-05-08 08:23:19 +0000625ExprList *sqlite3ExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){
drha76b5df2002-02-23 02:32:10 +0000626 if( pList==0 ){
627 pList = sqliteMalloc( sizeof(ExprList) );
628 if( pList==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000629 goto no_mem;
drha76b5df2002-02-23 02:32:10 +0000630 }
drh4efc4752004-01-16 15:55:37 +0000631 assert( pList->nAlloc==0 );
drha76b5df2002-02-23 02:32:10 +0000632 }
drh4305d102003-07-30 12:34:12 +0000633 if( pList->nAlloc<=pList->nExpr ){
danielk1977d5d56522005-03-16 12:15:20 +0000634 struct ExprList_item *a;
635 int n = pList->nAlloc*2 + 4;
636 a = sqliteRealloc(pList->a, n*sizeof(pList->a[0]));
637 if( a==0 ){
638 goto no_mem;
drha76b5df2002-02-23 02:32:10 +0000639 }
danielk1977d5d56522005-03-16 12:15:20 +0000640 pList->a = a;
641 pList->nAlloc = n;
drha76b5df2002-02-23 02:32:10 +0000642 }
drh4efc4752004-01-16 15:55:37 +0000643 assert( pList->a!=0 );
644 if( pExpr || pName ){
645 struct ExprList_item *pItem = &pList->a[pList->nExpr++];
646 memset(pItem, 0, sizeof(*pItem));
drha99db3b2004-06-19 14:49:12 +0000647 pItem->zName = sqlite3NameFromToken(pName);
danielk1977e94ddc92005-03-21 03:53:38 +0000648 pItem->pExpr = pExpr;
drha76b5df2002-02-23 02:32:10 +0000649 }
650 return pList;
danielk1977d5d56522005-03-16 12:15:20 +0000651
652no_mem:
653 /* Avoid leaking memory if malloc has failed. */
654 sqlite3ExprDelete(pExpr);
655 sqlite3ExprListDelete(pList);
656 return 0;
drha76b5df2002-02-23 02:32:10 +0000657}
658
659/*
danielk19777a15a4b2007-05-08 17:54:43 +0000660** If the expression list pEList contains more than iLimit elements,
661** leave an error message in pParse.
662*/
663void sqlite3ExprListCheckLength(
664 Parse *pParse,
665 ExprList *pEList,
666 int iLimit,
667 const char *zObject
668){
danielk1977b4fc6792007-05-08 18:04:46 +0000669 if( pEList && pEList->nExpr>iLimit ){
danielk19777a15a4b2007-05-08 17:54:43 +0000670 sqlite3ErrorMsg(pParse, "too many columns in %s", zObject);
671 }
672}
673
danielk1977fc976062007-05-10 10:46:56 +0000674
675#if SQLITE_MAX_EXPR_DEPTH>0
676/* The following three functions, heightOfExpr(), heightOfExprList()
677** and heightOfSelect(), are used to determine the maximum height
678** of any expression tree referenced by the structure passed as the
679** first argument.
680**
681** If this maximum height is greater than the current value pointed
682** to by pnHeight, the second parameter, then set *pnHeight to that
683** value.
684*/
685static void heightOfExpr(Expr *p, int *pnHeight){
686 if( p ){
687 if( p->nHeight>*pnHeight ){
688 *pnHeight = p->nHeight;
689 }
690 }
691}
692static void heightOfExprList(ExprList *p, int *pnHeight){
693 if( p ){
694 int i;
695 for(i=0; i<p->nExpr; i++){
696 heightOfExpr(p->a[i].pExpr, pnHeight);
697 }
698 }
699}
700static void heightOfSelect(Select *p, int *pnHeight){
701 if( p ){
702 heightOfExpr(p->pWhere, pnHeight);
703 heightOfExpr(p->pHaving, pnHeight);
704 heightOfExpr(p->pLimit, pnHeight);
705 heightOfExpr(p->pOffset, pnHeight);
706 heightOfExprList(p->pEList, pnHeight);
707 heightOfExprList(p->pGroupBy, pnHeight);
708 heightOfExprList(p->pOrderBy, pnHeight);
709 heightOfSelect(p->pPrior, pnHeight);
710 }
711}
712
713/*
714** Set the Expr.nHeight variable in the structure passed as an
715** argument. An expression with no children, Expr.pList or
716** Expr.pSelect member has a height of 1. Any other expression
717** has a height equal to the maximum height of any other
718** referenced Expr plus one.
719*/
720void sqlite3ExprSetHeight(Expr *p){
721 int nHeight = 0;
722 heightOfExpr(p->pLeft, &nHeight);
723 heightOfExpr(p->pRight, &nHeight);
724 heightOfExprList(p->pList, &nHeight);
725 heightOfSelect(p->pSelect, &nHeight);
726 p->nHeight = nHeight + 1;
727}
728
729/*
730** Return the maximum height of any expression tree referenced
731** by the select statement passed as an argument.
732*/
733int sqlite3SelectExprHeight(Select *p){
734 int nHeight = 0;
735 heightOfSelect(p, &nHeight);
736 return nHeight;
737}
738#endif
739
danielk19777a15a4b2007-05-08 17:54:43 +0000740/*
drha76b5df2002-02-23 02:32:10 +0000741** Delete an entire expression list.
742*/
danielk19774adee202004-05-08 08:23:19 +0000743void sqlite3ExprListDelete(ExprList *pList){
drha76b5df2002-02-23 02:32:10 +0000744 int i;
drhbe5c89a2004-07-26 00:31:09 +0000745 struct ExprList_item *pItem;
drha76b5df2002-02-23 02:32:10 +0000746 if( pList==0 ) return;
drh1bdd9b52004-04-23 17:04:44 +0000747 assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
748 assert( pList->nExpr<=pList->nAlloc );
drhbe5c89a2004-07-26 00:31:09 +0000749 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
750 sqlite3ExprDelete(pItem->pExpr);
751 sqliteFree(pItem->zName);
drha76b5df2002-02-23 02:32:10 +0000752 }
753 sqliteFree(pList->a);
754 sqliteFree(pList);
755}
756
757/*
drh626a8792005-01-17 22:08:19 +0000758** Walk an expression tree. Call xFunc for each node visited.
drh73b211a2005-01-18 04:00:42 +0000759**
drh626a8792005-01-17 22:08:19 +0000760** The return value from xFunc determines whether the tree walk continues.
761** 0 means continue walking the tree. 1 means do not walk children
762** of the current node but continue with siblings. 2 means abandon
763** the tree walk completely.
764**
765** The return value from this routine is 1 to abandon the tree walk
766** and 0 to continue.
drh87abf5c2005-08-25 12:45:04 +0000767**
768** NOTICE: This routine does *not* descend into subqueries.
drh626a8792005-01-17 22:08:19 +0000769*/
danielk1977a58fdfb2005-02-08 07:50:40 +0000770static int walkExprList(ExprList *, int (*)(void *, Expr*), void *);
drh626a8792005-01-17 22:08:19 +0000771static int walkExprTree(Expr *pExpr, int (*xFunc)(void*,Expr*), void *pArg){
drh626a8792005-01-17 22:08:19 +0000772 int rc;
773 if( pExpr==0 ) return 0;
774 rc = (*xFunc)(pArg, pExpr);
775 if( rc==0 ){
776 if( walkExprTree(pExpr->pLeft, xFunc, pArg) ) return 1;
777 if( walkExprTree(pExpr->pRight, xFunc, pArg) ) return 1;
danielk1977a58fdfb2005-02-08 07:50:40 +0000778 if( walkExprList(pExpr->pList, xFunc, pArg) ) return 1;
drh626a8792005-01-17 22:08:19 +0000779 }
780 return rc>1;
781}
782
783/*
danielk1977a58fdfb2005-02-08 07:50:40 +0000784** Call walkExprTree() for every expression in list p.
785*/
786static int walkExprList(ExprList *p, int (*xFunc)(void *, Expr*), void *pArg){
787 int i;
788 struct ExprList_item *pItem;
789 if( !p ) return 0;
790 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
791 if( walkExprTree(pItem->pExpr, xFunc, pArg) ) return 1;
792 }
793 return 0;
794}
795
796/*
797** Call walkExprTree() for every expression in Select p, not including
798** expressions that are part of sub-selects in any FROM clause or the LIMIT
799** or OFFSET expressions..
800*/
801static int walkSelectExpr(Select *p, int (*xFunc)(void *, Expr*), void *pArg){
802 walkExprList(p->pEList, xFunc, pArg);
803 walkExprTree(p->pWhere, xFunc, pArg);
804 walkExprList(p->pGroupBy, xFunc, pArg);
805 walkExprTree(p->pHaving, xFunc, pArg);
806 walkExprList(p->pOrderBy, xFunc, pArg);
danielk197715d79822007-05-15 07:00:34 +0000807 if( p->pPrior ){
808 walkSelectExpr(p->pPrior, xFunc, pArg);
809 }
danielk1977a58fdfb2005-02-08 07:50:40 +0000810 return 0;
811}
812
813
814/*
drh626a8792005-01-17 22:08:19 +0000815** This routine is designed as an xFunc for walkExprTree().
816**
817** pArg is really a pointer to an integer. If we can tell by looking
drh73b211a2005-01-18 04:00:42 +0000818** at pExpr that the expression that contains pExpr is not a constant
819** expression, then set *pArg to 0 and return 2 to abandon the tree walk.
820** If pExpr does does not disqualify the expression from being a constant
821** then do nothing.
822**
823** After walking the whole tree, if no nodes are found that disqualify
824** the expression as constant, then we assume the whole expression
825** is constant. See sqlite3ExprIsConstant() for additional information.
drh626a8792005-01-17 22:08:19 +0000826*/
827static int exprNodeIsConstant(void *pArg, Expr *pExpr){
828 switch( pExpr->op ){
drheb55bd22005-06-30 17:04:21 +0000829 /* Consider functions to be constant if all their arguments are constant
830 ** and *pArg==2 */
831 case TK_FUNCTION:
832 if( *((int*)pArg)==2 ) return 0;
833 /* Fall through */
drh626a8792005-01-17 22:08:19 +0000834 case TK_ID:
835 case TK_COLUMN:
836 case TK_DOT:
837 case TK_AGG_FUNCTION:
drh13449892005-09-07 21:22:45 +0000838 case TK_AGG_COLUMN:
drhfe2093d2005-01-20 22:48:47 +0000839#ifndef SQLITE_OMIT_SUBQUERY
840 case TK_SELECT:
841 case TK_EXISTS:
842#endif
drh626a8792005-01-17 22:08:19 +0000843 *((int*)pArg) = 0;
844 return 2;
drh87abf5c2005-08-25 12:45:04 +0000845 case TK_IN:
846 if( pExpr->pSelect ){
847 *((int*)pArg) = 0;
848 return 2;
849 }
drh626a8792005-01-17 22:08:19 +0000850 default:
851 return 0;
852 }
853}
854
855/*
drhfef52082000-06-06 01:50:43 +0000856** Walk an expression tree. Return 1 if the expression is constant
drheb55bd22005-06-30 17:04:21 +0000857** and 0 if it involves variables or function calls.
drh23989372002-05-21 13:43:04 +0000858**
859** For the purposes of this function, a double-quoted string (ex: "abc")
860** is considered a variable but a single-quoted string (ex: 'abc') is
861** a constant.
drhfef52082000-06-06 01:50:43 +0000862*/
danielk19774adee202004-05-08 08:23:19 +0000863int sqlite3ExprIsConstant(Expr *p){
drh626a8792005-01-17 22:08:19 +0000864 int isConst = 1;
865 walkExprTree(p, exprNodeIsConstant, &isConst);
866 return isConst;
drhfef52082000-06-06 01:50:43 +0000867}
868
869/*
drheb55bd22005-06-30 17:04:21 +0000870** Walk an expression tree. Return 1 if the expression is constant
871** or a function call with constant arguments. Return and 0 if there
872** are any variables.
873**
874** For the purposes of this function, a double-quoted string (ex: "abc")
875** is considered a variable but a single-quoted string (ex: 'abc') is
876** a constant.
877*/
878int sqlite3ExprIsConstantOrFunction(Expr *p){
879 int isConst = 2;
880 walkExprTree(p, exprNodeIsConstant, &isConst);
881 return isConst!=0;
882}
883
884/*
drh73b211a2005-01-18 04:00:42 +0000885** If the expression p codes a constant integer that is small enough
drh202b2df2004-01-06 01:13:46 +0000886** to fit in a 32-bit integer, return 1 and put the value of the integer
887** in *pValue. If the expression is not an integer or if it is too big
888** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
drhe4de1fe2002-06-02 16:09:01 +0000889*/
danielk19774adee202004-05-08 08:23:19 +0000890int sqlite3ExprIsInteger(Expr *p, int *pValue){
drhe4de1fe2002-06-02 16:09:01 +0000891 switch( p->op ){
892 case TK_INTEGER: {
drh2646da72005-12-09 20:02:05 +0000893 if( sqlite3GetInt32((char*)p->token.z, pValue) ){
drh202b2df2004-01-06 01:13:46 +0000894 return 1;
895 }
896 break;
drhe4de1fe2002-06-02 16:09:01 +0000897 }
drh4b59ab52002-08-24 18:24:51 +0000898 case TK_UPLUS: {
danielk19774adee202004-05-08 08:23:19 +0000899 return sqlite3ExprIsInteger(p->pLeft, pValue);
drh4b59ab52002-08-24 18:24:51 +0000900 }
drhe4de1fe2002-06-02 16:09:01 +0000901 case TK_UMINUS: {
902 int v;
danielk19774adee202004-05-08 08:23:19 +0000903 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
drhe4de1fe2002-06-02 16:09:01 +0000904 *pValue = -v;
905 return 1;
906 }
907 break;
908 }
909 default: break;
910 }
911 return 0;
912}
913
914/*
drhc4a3c772001-04-04 11:48:57 +0000915** Return TRUE if the given string is a row-id column name.
916*/
danielk19774adee202004-05-08 08:23:19 +0000917int sqlite3IsRowid(const char *z){
918 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
919 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
920 if( sqlite3StrICmp(z, "OID")==0 ) return 1;
drhc4a3c772001-04-04 11:48:57 +0000921 return 0;
922}
923
924/*
drh8141f612004-01-25 22:44:58 +0000925** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
926** that name in the set of source tables in pSrcList and make the pExpr
927** expression node refer back to that source column. The following changes
928** are made to pExpr:
929**
930** pExpr->iDb Set the index in db->aDb[] of the database holding
931** the table.
932** pExpr->iTable Set to the cursor number for the table obtained
933** from pSrcList.
934** pExpr->iColumn Set to the column number within the table.
drh8141f612004-01-25 22:44:58 +0000935** pExpr->op Set to TK_COLUMN.
936** pExpr->pLeft Any expression this points to is deleted
937** pExpr->pRight Any expression this points to is deleted.
938**
939** The pDbToken is the name of the database (the "X"). This value may be
940** NULL meaning that name is of the form Y.Z or Z. Any available database
941** can be used. The pTableToken is the name of the table (the "Y"). This
942** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it
943** means that the form of the name is Z and that columns from any table
944** can be used.
945**
946** If the name cannot be resolved unambiguously, leave an error message
947** in pParse and return non-zero. Return zero on success.
948*/
949static int lookupName(
drhffe07b22005-11-03 00:41:17 +0000950 Parse *pParse, /* The parsing context */
drh8141f612004-01-25 22:44:58 +0000951 Token *pDbToken, /* Name of the database containing table, or NULL */
952 Token *pTableToken, /* Name of table containing column, or NULL */
953 Token *pColumnToken, /* Name of the column. */
drh626a8792005-01-17 22:08:19 +0000954 NameContext *pNC, /* The name context used to resolve the name */
drh8141f612004-01-25 22:44:58 +0000955 Expr *pExpr /* Make this EXPR node point to the selected column */
956){
957 char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */
958 char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */
959 char *zCol = 0; /* Name of the column. The "Z" */
960 int i, j; /* Loop counters */
961 int cnt = 0; /* Number of matching column names */
962 int cntTab = 0; /* Number of matching table names */
drh626a8792005-01-17 22:08:19 +0000963 sqlite3 *db = pParse->db; /* The database */
drh51669862004-12-18 18:40:26 +0000964 struct SrcList_item *pItem; /* Use for looping over pSrcList items */
965 struct SrcList_item *pMatch = 0; /* The matching pSrcList item */
drh73b211a2005-01-18 04:00:42 +0000966 NameContext *pTopNC = pNC; /* First namecontext in the list */
drh8141f612004-01-25 22:44:58 +0000967
968 assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
drha99db3b2004-06-19 14:49:12 +0000969 zDb = sqlite3NameFromToken(pDbToken);
970 zTab = sqlite3NameFromToken(pTableToken);
971 zCol = sqlite3NameFromToken(pColumnToken);
danielk19779e128002006-01-18 16:51:35 +0000972 if( sqlite3MallocFailed() ){
danielk1977d5d56522005-03-16 12:15:20 +0000973 goto lookupname_end;
drh8141f612004-01-25 22:44:58 +0000974 }
drh8141f612004-01-25 22:44:58 +0000975
976 pExpr->iTable = -1;
drh626a8792005-01-17 22:08:19 +0000977 while( pNC && cnt==0 ){
drhffe07b22005-11-03 00:41:17 +0000978 ExprList *pEList;
drh626a8792005-01-17 22:08:19 +0000979 SrcList *pSrcList = pNC->pSrcList;
drh8141f612004-01-25 22:44:58 +0000980
danielk1977b3bce662005-01-29 08:32:43 +0000981 if( pSrcList ){
982 for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
drh43617e92006-03-06 20:55:46 +0000983 Table *pTab;
984 int iDb;
danielk1977b3bce662005-01-29 08:32:43 +0000985 Column *pCol;
986
drh43617e92006-03-06 20:55:46 +0000987 pTab = pItem->pTab;
988 assert( pTab!=0 );
989 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
danielk1977b3bce662005-01-29 08:32:43 +0000990 assert( pTab->nCol>0 );
991 if( zTab ){
992 if( pItem->zAlias ){
993 char *zTabName = pItem->zAlias;
994 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
995 }else{
996 char *zTabName = pTab->zName;
997 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
danielk1977da184232006-01-05 11:34:32 +0000998 if( zDb!=0 && sqlite3StrICmp(db->aDb[iDb].zName, zDb)!=0 ){
danielk1977b3bce662005-01-29 08:32:43 +0000999 continue;
1000 }
drh626a8792005-01-17 22:08:19 +00001001 }
drh8141f612004-01-25 22:44:58 +00001002 }
danielk1977b3bce662005-01-29 08:32:43 +00001003 if( 0==(cntTab++) ){
drh626a8792005-01-17 22:08:19 +00001004 pExpr->iTable = pItem->iCursor;
danielk1977da184232006-01-05 11:34:32 +00001005 pExpr->pSchema = pTab->pSchema;
danielk1977b3bce662005-01-29 08:32:43 +00001006 pMatch = pItem;
1007 }
1008 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
1009 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
danielk1977b3bf5562006-01-10 17:58:23 +00001010 const char *zColl = pTab->aCol[j].zColl;
drh873fac02005-06-06 17:11:46 +00001011 IdList *pUsing;
danielk1977b3bce662005-01-29 08:32:43 +00001012 cnt++;
1013 pExpr->iTable = pItem->iCursor;
1014 pMatch = pItem;
danielk1977da184232006-01-05 11:34:32 +00001015 pExpr->pSchema = pTab->pSchema;
danielk1977b3bce662005-01-29 08:32:43 +00001016 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
1017 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
1018 pExpr->affinity = pTab->aCol[j].affinity;
drh8b4c40d2007-02-01 23:02:45 +00001019 if( (pExpr->flags & EP_ExpCollate)==0 ){
1020 pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0);
1021 }
drh61dfc312006-12-16 16:25:15 +00001022 if( i<pSrcList->nSrc-1 ){
1023 if( pItem[1].jointype & JT_NATURAL ){
1024 /* If this match occurred in the left table of a natural join,
1025 ** then skip the right table to avoid a duplicate match */
1026 pItem++;
1027 i++;
1028 }else if( (pUsing = pItem[1].pUsing)!=0 ){
1029 /* If this match occurs on a column that is in the USING clause
1030 ** of a join, skip the search of the right table of the join
1031 ** to avoid a duplicate match there. */
1032 int k;
1033 for(k=0; k<pUsing->nId; k++){
1034 if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ){
1035 pItem++;
1036 i++;
1037 break;
1038 }
drh873fac02005-06-06 17:11:46 +00001039 }
1040 }
1041 }
danielk1977b3bce662005-01-29 08:32:43 +00001042 break;
1043 }
drh8141f612004-01-25 22:44:58 +00001044 }
1045 }
1046 }
drh626a8792005-01-17 22:08:19 +00001047
1048#ifndef SQLITE_OMIT_TRIGGER
1049 /* If we have not already resolved the name, then maybe
1050 ** it is a new.* or old.* trigger argument reference
1051 */
1052 if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
1053 TriggerStack *pTriggerStack = pParse->trigStack;
1054 Table *pTab = 0;
1055 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){
1056 pExpr->iTable = pTriggerStack->newIdx;
1057 assert( pTriggerStack->pTab );
1058 pTab = pTriggerStack->pTab;
1059 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab)==0 ){
1060 pExpr->iTable = pTriggerStack->oldIdx;
1061 assert( pTriggerStack->pTab );
1062 pTab = pTriggerStack->pTab;
1063 }
1064
1065 if( pTab ){
danielk1977f0113002006-01-24 12:09:17 +00001066 int iCol;
drh626a8792005-01-17 22:08:19 +00001067 Column *pCol = pTab->aCol;
1068
danielk1977da184232006-01-05 11:34:32 +00001069 pExpr->pSchema = pTab->pSchema;
drh626a8792005-01-17 22:08:19 +00001070 cntTab++;
danielk1977f0113002006-01-24 12:09:17 +00001071 for(iCol=0; iCol < pTab->nCol; iCol++, pCol++) {
drh626a8792005-01-17 22:08:19 +00001072 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
danielk1977f0113002006-01-24 12:09:17 +00001073 const char *zColl = pTab->aCol[iCol].zColl;
drh626a8792005-01-17 22:08:19 +00001074 cnt++;
danielk1977f0113002006-01-24 12:09:17 +00001075 pExpr->iColumn = iCol==pTab->iPKey ? -1 : iCol;
1076 pExpr->affinity = pTab->aCol[iCol].affinity;
drh8b4c40d2007-02-01 23:02:45 +00001077 if( (pExpr->flags & EP_ExpCollate)==0 ){
1078 pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0);
1079 }
danielk1977aee18ef2005-03-09 12:26:50 +00001080 pExpr->pTab = pTab;
drh626a8792005-01-17 22:08:19 +00001081 break;
1082 }
1083 }
1084 }
1085 }
drhb7f91642004-10-31 02:22:47 +00001086#endif /* !defined(SQLITE_OMIT_TRIGGER) */
drh8141f612004-01-25 22:44:58 +00001087
drh626a8792005-01-17 22:08:19 +00001088 /*
1089 ** Perhaps the name is a reference to the ROWID
1090 */
1091 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
1092 cnt = 1;
1093 pExpr->iColumn = -1;
drh8a512562005-11-14 22:29:05 +00001094 pExpr->affinity = SQLITE_AFF_INTEGER;
drh626a8792005-01-17 22:08:19 +00001095 }
drh8141f612004-01-25 22:44:58 +00001096
drh626a8792005-01-17 22:08:19 +00001097 /*
1098 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
1099 ** might refer to an result-set alias. This happens, for example, when
1100 ** we are resolving names in the WHERE clause of the following command:
1101 **
1102 ** SELECT a+b AS x FROM table WHERE x<10;
1103 **
1104 ** In cases like this, replace pExpr with a copy of the expression that
1105 ** forms the result set entry ("a+b" in the example) and return immediately.
1106 ** Note that the expression in the result set should have already been
1107 ** resolved by the time the WHERE clause is resolved.
1108 */
drhffe07b22005-11-03 00:41:17 +00001109 if( cnt==0 && (pEList = pNC->pEList)!=0 && zTab==0 ){
drh626a8792005-01-17 22:08:19 +00001110 for(j=0; j<pEList->nExpr; j++){
1111 char *zAs = pEList->a[j].zName;
1112 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
drh4f07e5f2007-05-14 11:34:46 +00001113 Expr *pDup;
drh626a8792005-01-17 22:08:19 +00001114 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
drh4f07e5f2007-05-14 11:34:46 +00001115 assert( pExpr->pList==0 );
1116 assert( pExpr->pSelect==0 );
1117 pDup = sqlite3ExprDup(pEList->a[j].pExpr);
1118 if( pExpr->flags & EP_ExpCollate ){
1119 pDup->pColl = pExpr->pColl;
1120 pDup->flags |= EP_ExpCollate;
1121 }
1122 memcpy(pExpr, pDup, sizeof(*pExpr));
1123 sqliteFree(pDup);
drh15ccce12005-05-23 15:06:39 +00001124 cnt = 1;
drh626a8792005-01-17 22:08:19 +00001125 assert( zTab==0 && zDb==0 );
drh15ccce12005-05-23 15:06:39 +00001126 goto lookupname_end_2;
drh626a8792005-01-17 22:08:19 +00001127 }
1128 }
1129 }
1130
1131 /* Advance to the next name context. The loop will exit when either
1132 ** we have a match (cnt>0) or when we run out of name contexts.
1133 */
1134 if( cnt==0 ){
1135 pNC = pNC->pNext;
1136 }
drh8141f612004-01-25 22:44:58 +00001137 }
1138
1139 /*
1140 ** If X and Y are NULL (in other words if only the column name Z is
1141 ** supplied) and the value of Z is enclosed in double-quotes, then
1142 ** Z is a string literal if it doesn't match any column names. In that
1143 ** case, we need to return right away and not make any changes to
1144 ** pExpr.
drh15ccce12005-05-23 15:06:39 +00001145 **
1146 ** Because no reference was made to outer contexts, the pNC->nRef
1147 ** fields are not changed in any context.
drh8141f612004-01-25 22:44:58 +00001148 */
1149 if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
1150 sqliteFree(zCol);
1151 return 0;
1152 }
1153
1154 /*
1155 ** cnt==0 means there was not match. cnt>1 means there were two or
1156 ** more matches. Either way, we have an error.
1157 */
1158 if( cnt!=1 ){
1159 char *z = 0;
1160 char *zErr;
1161 zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s";
1162 if( zDb ){
drhf93339d2006-01-03 15:16:26 +00001163 sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, (char*)0);
drh8141f612004-01-25 22:44:58 +00001164 }else if( zTab ){
drhf93339d2006-01-03 15:16:26 +00001165 sqlite3SetString(&z, zTab, ".", zCol, (char*)0);
drh8141f612004-01-25 22:44:58 +00001166 }else{
1167 z = sqliteStrDup(zCol);
1168 }
danielk19774adee202004-05-08 08:23:19 +00001169 sqlite3ErrorMsg(pParse, zErr, z);
drh8141f612004-01-25 22:44:58 +00001170 sqliteFree(z);
drh73b211a2005-01-18 04:00:42 +00001171 pTopNC->nErr++;
drh8141f612004-01-25 22:44:58 +00001172 }
1173
drh51669862004-12-18 18:40:26 +00001174 /* If a column from a table in pSrcList is referenced, then record
1175 ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes
1176 ** bit 0 to be set. Column 1 sets bit 1. And so forth. If the
1177 ** column number is greater than the number of bits in the bitmask
1178 ** then set the high-order bit of the bitmask.
1179 */
1180 if( pExpr->iColumn>=0 && pMatch!=0 ){
1181 int n = pExpr->iColumn;
1182 if( n>=sizeof(Bitmask)*8 ){
1183 n = sizeof(Bitmask)*8-1;
1184 }
1185 assert( pMatch->iCursor==pExpr->iTable );
drhca83ac52007-02-01 01:40:44 +00001186 pMatch->colUsed |= ((Bitmask)1)<<n;
drh51669862004-12-18 18:40:26 +00001187 }
1188
danielk1977d5d56522005-03-16 12:15:20 +00001189lookupname_end:
drh8141f612004-01-25 22:44:58 +00001190 /* Clean up and return
1191 */
1192 sqliteFree(zDb);
1193 sqliteFree(zTab);
danielk19774adee202004-05-08 08:23:19 +00001194 sqlite3ExprDelete(pExpr->pLeft);
drh8141f612004-01-25 22:44:58 +00001195 pExpr->pLeft = 0;
danielk19774adee202004-05-08 08:23:19 +00001196 sqlite3ExprDelete(pExpr->pRight);
drh8141f612004-01-25 22:44:58 +00001197 pExpr->pRight = 0;
1198 pExpr->op = TK_COLUMN;
drh15ccce12005-05-23 15:06:39 +00001199lookupname_end_2:
1200 sqliteFree(zCol);
drh626a8792005-01-17 22:08:19 +00001201 if( cnt==1 ){
danielk1977b3bce662005-01-29 08:32:43 +00001202 assert( pNC!=0 );
drh626a8792005-01-17 22:08:19 +00001203 sqlite3AuthRead(pParse, pExpr, pNC->pSrcList);
danielk1977aee18ef2005-03-09 12:26:50 +00001204 if( pMatch && !pMatch->pSelect ){
1205 pExpr->pTab = pMatch->pTab;
1206 }
drh15ccce12005-05-23 15:06:39 +00001207 /* Increment the nRef value on all name contexts from TopNC up to
1208 ** the point where the name matched. */
1209 for(;;){
1210 assert( pTopNC!=0 );
1211 pTopNC->nRef++;
1212 if( pTopNC==pNC ) break;
1213 pTopNC = pTopNC->pNext;
1214 }
1215 return 0;
1216 } else {
1217 return 1;
drh626a8792005-01-17 22:08:19 +00001218 }
drh8141f612004-01-25 22:44:58 +00001219}
1220
1221/*
drh626a8792005-01-17 22:08:19 +00001222** This routine is designed as an xFunc for walkExprTree().
1223**
drh73b211a2005-01-18 04:00:42 +00001224** Resolve symbolic names into TK_COLUMN operators for the current
drh626a8792005-01-17 22:08:19 +00001225** node in the expression tree. Return 0 to continue the search down
drh73b211a2005-01-18 04:00:42 +00001226** the tree or 2 to abort the tree walk.
1227**
1228** This routine also does error checking and name resolution for
1229** function names. The operator for aggregate functions is changed
1230** to TK_AGG_FUNCTION.
drh626a8792005-01-17 22:08:19 +00001231*/
1232static int nameResolverStep(void *pArg, Expr *pExpr){
1233 NameContext *pNC = (NameContext*)pArg;
drh626a8792005-01-17 22:08:19 +00001234 Parse *pParse;
drh626a8792005-01-17 22:08:19 +00001235
danielk1977b3bce662005-01-29 08:32:43 +00001236 if( pExpr==0 ) return 1;
drh626a8792005-01-17 22:08:19 +00001237 assert( pNC!=0 );
drh626a8792005-01-17 22:08:19 +00001238 pParse = pNC->pParse;
danielk1977b3bce662005-01-29 08:32:43 +00001239
drh626a8792005-01-17 22:08:19 +00001240 if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return 1;
1241 ExprSetProperty(pExpr, EP_Resolved);
1242#ifndef NDEBUG
danielk1977f0113002006-01-24 12:09:17 +00001243 if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
1244 SrcList *pSrcList = pNC->pSrcList;
danielk1977940fac92005-01-23 22:41:37 +00001245 int i;
danielk1977f0113002006-01-24 12:09:17 +00001246 for(i=0; i<pNC->pSrcList->nSrc; i++){
drh626a8792005-01-17 22:08:19 +00001247 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
1248 }
1249 }
1250#endif
1251 switch( pExpr->op ){
1252 /* Double-quoted strings (ex: "abc") are used as identifiers if
1253 ** possible. Otherwise they remain as strings. Single-quoted
1254 ** strings (ex: 'abc') are always string literals.
1255 */
1256 case TK_STRING: {
1257 if( pExpr->token.z[0]=='\'' ) break;
1258 /* Fall thru into the TK_ID case if this is a double-quoted string */
1259 }
1260 /* A lone identifier is the name of a column.
1261 */
1262 case TK_ID: {
drh626a8792005-01-17 22:08:19 +00001263 lookupName(pParse, 0, 0, &pExpr->token, pNC, pExpr);
1264 return 1;
1265 }
1266
1267 /* A table name and column name: ID.ID
1268 ** Or a database, table and column: ID.ID.ID
1269 */
1270 case TK_DOT: {
1271 Token *pColumn;
1272 Token *pTable;
1273 Token *pDb;
1274 Expr *pRight;
1275
danielk1977b3bce662005-01-29 08:32:43 +00001276 /* if( pSrcList==0 ) break; */
drh626a8792005-01-17 22:08:19 +00001277 pRight = pExpr->pRight;
1278 if( pRight->op==TK_ID ){
1279 pDb = 0;
1280 pTable = &pExpr->pLeft->token;
1281 pColumn = &pRight->token;
1282 }else{
1283 assert( pRight->op==TK_DOT );
1284 pDb = &pExpr->pLeft->token;
1285 pTable = &pRight->pLeft->token;
1286 pColumn = &pRight->pRight->token;
1287 }
1288 lookupName(pParse, pDb, pTable, pColumn, pNC, pExpr);
1289 return 1;
1290 }
1291
1292 /* Resolve function names
1293 */
drhb71090f2005-05-23 17:26:51 +00001294 case TK_CONST_FUNC:
drh626a8792005-01-17 22:08:19 +00001295 case TK_FUNCTION: {
1296 ExprList *pList = pExpr->pList; /* The argument list */
1297 int n = pList ? pList->nExpr : 0; /* Number of arguments */
1298 int no_such_func = 0; /* True if no such function exists */
1299 int wrong_num_args = 0; /* True if wrong number of arguments */
1300 int is_agg = 0; /* True if is an aggregate function */
1301 int i;
drh5169bbc2006-08-24 14:59:45 +00001302 int auth; /* Authorization to use the function */
drh626a8792005-01-17 22:08:19 +00001303 int nId; /* Number of characters in function name */
1304 const char *zId; /* The function name. */
drh73b211a2005-01-18 04:00:42 +00001305 FuncDef *pDef; /* Information about the function */
danielk197714db2662006-01-09 16:12:04 +00001306 int enc = ENC(pParse->db); /* The database encoding */
drh626a8792005-01-17 22:08:19 +00001307
drh2646da72005-12-09 20:02:05 +00001308 zId = (char*)pExpr->token.z;
drhb71090f2005-05-23 17:26:51 +00001309 nId = pExpr->token.n;
drh626a8792005-01-17 22:08:19 +00001310 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
1311 if( pDef==0 ){
1312 pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
1313 if( pDef==0 ){
1314 no_such_func = 1;
1315 }else{
1316 wrong_num_args = 1;
1317 }
1318 }else{
1319 is_agg = pDef->xFunc==0;
1320 }
drh2fca7fe2006-11-23 11:59:13 +00001321#ifndef SQLITE_OMIT_AUTHORIZATION
drh5169bbc2006-08-24 14:59:45 +00001322 if( pDef ){
1323 auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0);
1324 if( auth!=SQLITE_OK ){
1325 if( auth==SQLITE_DENY ){
1326 sqlite3ErrorMsg(pParse, "not authorized to use function: %s",
1327 pDef->zName);
1328 pNC->nErr++;
1329 }
1330 pExpr->op = TK_NULL;
1331 return 1;
1332 }
1333 }
drhb8b14212006-08-24 15:18:25 +00001334#endif
drh626a8792005-01-17 22:08:19 +00001335 if( is_agg && !pNC->allowAgg ){
1336 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
1337 pNC->nErr++;
1338 is_agg = 0;
1339 }else if( no_such_func ){
1340 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
1341 pNC->nErr++;
1342 }else if( wrong_num_args ){
1343 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
1344 nId, zId);
1345 pNC->nErr++;
1346 }
1347 if( is_agg ){
1348 pExpr->op = TK_AGG_FUNCTION;
1349 pNC->hasAgg = 1;
1350 }
drh73b211a2005-01-18 04:00:42 +00001351 if( is_agg ) pNC->allowAgg = 0;
drh626a8792005-01-17 22:08:19 +00001352 for(i=0; pNC->nErr==0 && i<n; i++){
drh73b211a2005-01-18 04:00:42 +00001353 walkExprTree(pList->a[i].pExpr, nameResolverStep, pNC);
drh626a8792005-01-17 22:08:19 +00001354 }
drh73b211a2005-01-18 04:00:42 +00001355 if( is_agg ) pNC->allowAgg = 1;
drh626a8792005-01-17 22:08:19 +00001356 /* FIX ME: Compute pExpr->affinity based on the expected return
1357 ** type of the function
1358 */
1359 return is_agg;
1360 }
danielk1977b3bce662005-01-29 08:32:43 +00001361#ifndef SQLITE_OMIT_SUBQUERY
1362 case TK_SELECT:
1363 case TK_EXISTS:
1364#endif
1365 case TK_IN: {
1366 if( pExpr->pSelect ){
drh8a9f38f2005-11-05 15:07:55 +00001367 int nRef = pNC->nRef;
drh06f65412005-11-03 02:03:13 +00001368#ifndef SQLITE_OMIT_CHECK
1369 if( pNC->isCheck ){
1370 sqlite3ErrorMsg(pParse,"subqueries prohibited in CHECK constraints");
1371 }
1372#endif
danielk1977b3bce662005-01-29 08:32:43 +00001373 sqlite3SelectResolve(pParse, pExpr->pSelect, pNC);
1374 assert( pNC->nRef>=nRef );
1375 if( nRef!=pNC->nRef ){
1376 ExprSetProperty(pExpr, EP_VarSelect);
1377 }
1378 }
drh4284fb02005-11-03 12:33:28 +00001379 break;
danielk1977b3bce662005-01-29 08:32:43 +00001380 }
drh4284fb02005-11-03 12:33:28 +00001381#ifndef SQLITE_OMIT_CHECK
1382 case TK_VARIABLE: {
1383 if( pNC->isCheck ){
1384 sqlite3ErrorMsg(pParse,"parameters prohibited in CHECK constraints");
1385 }
1386 break;
1387 }
1388#endif
drh626a8792005-01-17 22:08:19 +00001389 }
1390 return 0;
1391}
1392
1393/*
drhcce7d172000-05-31 15:34:51 +00001394** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +00001395** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +00001396** index to the table in the table list and a column offset. The
1397** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
1398** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +00001399** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +00001400** VDBE cursor number for a cursor that is pointing into the referenced
1401** table. The Expr.iColumn value is changed to the index of the column
1402** of the referenced table. The Expr.iColumn value for the special
1403** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
1404** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +00001405**
drh626a8792005-01-17 22:08:19 +00001406** Also resolve function names and check the functions for proper
1407** usage. Make sure all function names are recognized and all functions
1408** have the correct number of arguments. Leave an error message
1409** in pParse->zErrMsg if anything is amiss. Return the number of errors.
1410**
drh73b211a2005-01-18 04:00:42 +00001411** If the expression contains aggregate functions then set the EP_Agg
1412** property on the expression.
drh626a8792005-01-17 22:08:19 +00001413*/
danielk1977fc976062007-05-10 10:46:56 +00001414int sqlite3ExprResolveNames(
danielk1977b3bce662005-01-29 08:32:43 +00001415 NameContext *pNC, /* Namespace to resolve expressions in. */
1416 Expr *pExpr /* The expression to be analyzed. */
drh626a8792005-01-17 22:08:19 +00001417){
drh13449892005-09-07 21:22:45 +00001418 int savedHasAgg;
drh73b211a2005-01-18 04:00:42 +00001419 if( pExpr==0 ) return 0;
danielk1977fc976062007-05-10 10:46:56 +00001420#if SQLITE_MAX_EXPR_DEPTH>0
1421 if( (pExpr->nHeight+pNC->pParse->nHeight)>SQLITE_MAX_EXPR_DEPTH ){
1422 sqlite3ErrorMsg(pNC->pParse,
1423 "Expression tree is too large (maximum depth %d)",
1424 SQLITE_MAX_EXPR_DEPTH
1425 );
1426 return 1;
1427 }
1428 pNC->pParse->nHeight += pExpr->nHeight;
1429#endif
drh13449892005-09-07 21:22:45 +00001430 savedHasAgg = pNC->hasAgg;
1431 pNC->hasAgg = 0;
danielk1977b3bce662005-01-29 08:32:43 +00001432 walkExprTree(pExpr, nameResolverStep, pNC);
danielk1977fc976062007-05-10 10:46:56 +00001433#if SQLITE_MAX_EXPR_DEPTH>0
1434 pNC->pParse->nHeight -= pExpr->nHeight;
1435#endif
danielk1977b3bce662005-01-29 08:32:43 +00001436 if( pNC->nErr>0 ){
drh73b211a2005-01-18 04:00:42 +00001437 ExprSetProperty(pExpr, EP_Error);
drh73b211a2005-01-18 04:00:42 +00001438 }
drh13449892005-09-07 21:22:45 +00001439 if( pNC->hasAgg ){
1440 ExprSetProperty(pExpr, EP_Agg);
1441 }else if( savedHasAgg ){
1442 pNC->hasAgg = 1;
1443 }
drh73b211a2005-01-18 04:00:42 +00001444 return ExprHasProperty(pExpr, EP_Error);
drh626a8792005-01-17 22:08:19 +00001445}
1446
drh1398ad32005-01-19 23:24:50 +00001447/*
1448** A pointer instance of this structure is used to pass information
1449** through walkExprTree into codeSubqueryStep().
1450*/
1451typedef struct QueryCoder QueryCoder;
1452struct QueryCoder {
1453 Parse *pParse; /* The parsing context */
1454 NameContext *pNC; /* Namespace of first enclosing query */
1455};
1456
drh626a8792005-01-17 22:08:19 +00001457
1458/*
drh9cbe6352005-11-29 03:13:21 +00001459** Generate code for scalar subqueries used as an expression
1460** and IN operators. Examples:
drh626a8792005-01-17 22:08:19 +00001461**
drh9cbe6352005-11-29 03:13:21 +00001462** (SELECT a FROM b) -- subquery
1463** EXISTS (SELECT a FROM b) -- EXISTS subquery
1464** x IN (4,5,11) -- IN operator with list on right-hand side
1465** x IN (SELECT a FROM b) -- IN operator with subquery on the right
drhfef52082000-06-06 01:50:43 +00001466**
drh9cbe6352005-11-29 03:13:21 +00001467** The pExpr parameter describes the expression that contains the IN
1468** operator or subquery.
drhcce7d172000-05-31 15:34:51 +00001469*/
drh51522cd2005-01-20 13:36:19 +00001470#ifndef SQLITE_OMIT_SUBQUERY
danielk1977b3bce662005-01-29 08:32:43 +00001471void sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){
drh57dbd7b2005-07-08 18:25:26 +00001472 int testAddr = 0; /* One-time test address */
danielk1977b3bce662005-01-29 08:32:43 +00001473 Vdbe *v = sqlite3GetVdbe(pParse);
1474 if( v==0 ) return;
1475
danielk1977fc976062007-05-10 10:46:56 +00001476
drh57dbd7b2005-07-08 18:25:26 +00001477 /* This code must be run in its entirety every time it is encountered
1478 ** if any of the following is true:
1479 **
1480 ** * The right-hand side is a correlated subquery
1481 ** * The right-hand side is an expression list containing variables
1482 ** * We are inside a trigger
1483 **
1484 ** If all of the above are false, then we can run this code just once
1485 ** save the results, and reuse the same result on subsequent invocations.
danielk1977b3bce662005-01-29 08:32:43 +00001486 */
1487 if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->trigStack ){
1488 int mem = pParse->nMem++;
1489 sqlite3VdbeAddOp(v, OP_MemLoad, mem, 0);
drh57dbd7b2005-07-08 18:25:26 +00001490 testAddr = sqlite3VdbeAddOp(v, OP_If, 0, 0);
danielk19779e128002006-01-18 16:51:35 +00001491 assert( testAddr>0 || sqlite3MallocFailed() );
drhd654be82005-09-20 17:42:23 +00001492 sqlite3VdbeAddOp(v, OP_MemInt, 1, mem);
danielk1977b3bce662005-01-29 08:32:43 +00001493 }
1494
drhcce7d172000-05-31 15:34:51 +00001495 switch( pExpr->op ){
drhfef52082000-06-06 01:50:43 +00001496 case TK_IN: {
danielk1977e014a832004-05-17 10:48:57 +00001497 char affinity;
drhd3d39e92004-05-20 22:16:29 +00001498 KeyInfo keyInfo;
drhb9bb7c12006-06-11 23:41:55 +00001499 int addr; /* Address of OP_OpenEphemeral instruction */
drhd3d39e92004-05-20 22:16:29 +00001500
danielk1977bf3b7212004-05-18 10:06:24 +00001501 affinity = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +00001502
1503 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
drh57dbd7b2005-07-08 18:25:26 +00001504 ** expression it is handled the same way. A virtual table is
danielk1977e014a832004-05-17 10:48:57 +00001505 ** filled with single-field index keys representing the results
1506 ** from the SELECT or the <exprlist>.
1507 **
1508 ** If the 'x' expression is a column value, or the SELECT...
1509 ** statement returns a column value, then the affinity of that
1510 ** column is used to build the index keys. If both 'x' and the
1511 ** SELECT... statement are columns, then numeric affinity is used
1512 ** if either column has NUMERIC or INTEGER affinity. If neither
1513 ** 'x' nor the SELECT... statement are columns, then numeric affinity
1514 ** is used.
1515 */
1516 pExpr->iTable = pParse->nTab++;
drhb9bb7c12006-06-11 23:41:55 +00001517 addr = sqlite3VdbeAddOp(v, OP_OpenEphemeral, pExpr->iTable, 0);
drhd3d39e92004-05-20 22:16:29 +00001518 memset(&keyInfo, 0, sizeof(keyInfo));
1519 keyInfo.nField = 1;
drhf3218fe2004-05-28 08:21:02 +00001520 sqlite3VdbeAddOp(v, OP_SetNumColumns, pExpr->iTable, 1);
danielk1977e014a832004-05-17 10:48:57 +00001521
drhfef52082000-06-06 01:50:43 +00001522 if( pExpr->pSelect ){
1523 /* Case 1: expr IN (SELECT ...)
1524 **
danielk1977e014a832004-05-17 10:48:57 +00001525 ** Generate code to write the results of the select into the temporary
1526 ** table allocated and opened above.
drhfef52082000-06-06 01:50:43 +00001527 */
danielk1977e014a832004-05-17 10:48:57 +00001528 int iParm = pExpr->iTable + (((int)affinity)<<16);
drhbe5c89a2004-07-26 00:31:09 +00001529 ExprList *pEList;
danielk1977e014a832004-05-17 10:48:57 +00001530 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
drh94ccde52007-04-13 16:06:32 +00001531 if( sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0) ){
1532 return;
1533 }
drhbe5c89a2004-07-26 00:31:09 +00001534 pEList = pExpr->pSelect->pEList;
1535 if( pEList && pEList->nExpr>0 ){
danielk19777cedc8d2004-06-10 10:50:08 +00001536 keyInfo.aColl[0] = binaryCompareCollSeq(pParse, pExpr->pLeft,
drhbe5c89a2004-07-26 00:31:09 +00001537 pEList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001538 }
drhfef52082000-06-06 01:50:43 +00001539 }else if( pExpr->pList ){
1540 /* Case 2: expr IN (exprlist)
1541 **
danielk1977e014a832004-05-17 10:48:57 +00001542 ** For each expression, build an index key from the evaluation and
1543 ** store it in the temporary table. If <expr> is a column, then use
1544 ** that columns affinity when building index keys. If <expr> is not
1545 ** a column, use numeric affinity.
drhfef52082000-06-06 01:50:43 +00001546 */
danielk1977e014a832004-05-17 10:48:57 +00001547 int i;
drh57dbd7b2005-07-08 18:25:26 +00001548 ExprList *pList = pExpr->pList;
1549 struct ExprList_item *pItem;
1550
danielk1977e014a832004-05-17 10:48:57 +00001551 if( !affinity ){
drh8159a352006-05-23 23:22:29 +00001552 affinity = SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +00001553 }
danielk19770202b292004-06-09 09:55:16 +00001554 keyInfo.aColl[0] = pExpr->pLeft->pColl;
danielk1977e014a832004-05-17 10:48:57 +00001555
1556 /* Loop through each expression in <exprlist>. */
drh57dbd7b2005-07-08 18:25:26 +00001557 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
1558 Expr *pE2 = pItem->pExpr;
danielk1977e014a832004-05-17 10:48:57 +00001559
drh57dbd7b2005-07-08 18:25:26 +00001560 /* If the expression is not constant then we will need to
1561 ** disable the test that was generated above that makes sure
1562 ** this code only executes once. Because for a non-constant
1563 ** expression we need to rerun this code each time.
1564 */
drh6c30be82005-07-29 15:10:17 +00001565 if( testAddr>0 && !sqlite3ExprIsConstant(pE2) ){
drhf8875402006-03-17 13:56:34 +00001566 sqlite3VdbeChangeToNoop(v, testAddr-1, 3);
drh57dbd7b2005-07-08 18:25:26 +00001567 testAddr = 0;
drh4794b982000-06-06 13:54:14 +00001568 }
danielk1977e014a832004-05-17 10:48:57 +00001569
1570 /* Evaluate the expression and insert it into the temp table */
1571 sqlite3ExprCode(pParse, pE2);
drh94a11212004-09-25 13:12:14 +00001572 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1);
drhf0863fe2005-06-12 21:35:51 +00001573 sqlite3VdbeAddOp(v, OP_IdxInsert, pExpr->iTable, 0);
drhfef52082000-06-06 01:50:43 +00001574 }
1575 }
danielk19770202b292004-06-09 09:55:16 +00001576 sqlite3VdbeChangeP3(v, addr, (void *)&keyInfo, P3_KEYINFO);
danielk1977b3bce662005-01-29 08:32:43 +00001577 break;
drhfef52082000-06-06 01:50:43 +00001578 }
1579
drh51522cd2005-01-20 13:36:19 +00001580 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001581 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +00001582 /* This has to be a scalar SELECT. Generate code to put the
1583 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +00001584 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +00001585 */
drh2646da72005-12-09 20:02:05 +00001586 static const Token one = { (u8*)"1", 0, 1 };
drh51522cd2005-01-20 13:36:19 +00001587 Select *pSel;
drhec7429a2005-10-06 16:53:14 +00001588 int iMem;
1589 int sop;
drh1398ad32005-01-19 23:24:50 +00001590
drhec7429a2005-10-06 16:53:14 +00001591 pExpr->iColumn = iMem = pParse->nMem++;
drh51522cd2005-01-20 13:36:19 +00001592 pSel = pExpr->pSelect;
1593 if( pExpr->op==TK_SELECT ){
1594 sop = SRT_Mem;
drhec7429a2005-10-06 16:53:14 +00001595 sqlite3VdbeAddOp(v, OP_MemNull, iMem, 0);
1596 VdbeComment((v, "# Init subquery result"));
drh51522cd2005-01-20 13:36:19 +00001597 }else{
drh51522cd2005-01-20 13:36:19 +00001598 sop = SRT_Exists;
drhec7429a2005-10-06 16:53:14 +00001599 sqlite3VdbeAddOp(v, OP_MemInt, 0, iMem);
1600 VdbeComment((v, "# Init EXISTS result"));
drh51522cd2005-01-20 13:36:19 +00001601 }
drhec7429a2005-10-06 16:53:14 +00001602 sqlite3ExprDelete(pSel->pLimit);
1603 pSel->pLimit = sqlite3Expr(TK_INTEGER, 0, 0, &one);
drh94ccde52007-04-13 16:06:32 +00001604 if( sqlite3Select(pParse, pSel, sop, iMem, 0, 0, 0, 0) ){
1605 return;
1606 }
danielk1977b3bce662005-01-29 08:32:43 +00001607 break;
drhcce7d172000-05-31 15:34:51 +00001608 }
1609 }
danielk1977b3bce662005-01-29 08:32:43 +00001610
drh57dbd7b2005-07-08 18:25:26 +00001611 if( testAddr ){
drhd654be82005-09-20 17:42:23 +00001612 sqlite3VdbeJumpHere(v, testAddr);
danielk1977b3bce662005-01-29 08:32:43 +00001613 }
danielk1977fc976062007-05-10 10:46:56 +00001614
danielk1977b3bce662005-01-29 08:32:43 +00001615 return;
drhcce7d172000-05-31 15:34:51 +00001616}
drh51522cd2005-01-20 13:36:19 +00001617#endif /* SQLITE_OMIT_SUBQUERY */
drhcce7d172000-05-31 15:34:51 +00001618
drhcce7d172000-05-31 15:34:51 +00001619/*
drhfec19aa2004-05-19 20:41:03 +00001620** Generate an instruction that will put the integer describe by
1621** text z[0..n-1] on the stack.
1622*/
1623static void codeInteger(Vdbe *v, const char *z, int n){
1624 int i;
drh6fec0762004-05-30 01:38:43 +00001625 if( sqlite3GetInt32(z, &i) ){
1626 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
1627 }else if( sqlite3FitsIn64Bits(z) ){
drh29dda4a2005-07-21 18:23:20 +00001628 sqlite3VdbeOp3(v, OP_Int64, 0, 0, z, n);
drhfec19aa2004-05-19 20:41:03 +00001629 }else{
1630 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n);
1631 }
1632}
1633
drh945498f2007-02-24 11:52:52 +00001634
1635/*
1636** Generate code that will extract the iColumn-th column from
1637** table pTab and push that column value on the stack. There
1638** is an open cursor to pTab in iTable. If iColumn<0 then
1639** code is generated that extracts the rowid.
1640*/
1641void sqlite3ExprCodeGetColumn(Vdbe *v, Table *pTab, int iColumn, int iTable){
1642 if( iColumn<0 ){
1643 int op = (pTab && IsVirtual(pTab)) ? OP_VRowid : OP_Rowid;
1644 sqlite3VdbeAddOp(v, op, iTable, 0);
1645 }else if( pTab==0 ){
1646 sqlite3VdbeAddOp(v, OP_Column, iTable, iColumn);
1647 }else{
1648 int op = IsVirtual(pTab) ? OP_VColumn : OP_Column;
1649 sqlite3VdbeAddOp(v, op, iTable, iColumn);
1650 sqlite3ColumnDefault(v, pTab, iColumn);
1651#ifndef SQLITE_OMIT_FLOATING_POINT
1652 if( pTab->aCol[iColumn].affinity==SQLITE_AFF_REAL ){
1653 sqlite3VdbeAddOp(v, OP_RealAffinity, 0, 0);
1654 }
1655#endif
1656 }
1657}
1658
drhfec19aa2004-05-19 20:41:03 +00001659/*
drhcce7d172000-05-31 15:34:51 +00001660** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +00001661** expression and leave the result on the top of stack.
drhf2bc0132004-10-04 13:19:23 +00001662**
1663** This code depends on the fact that certain token values (ex: TK_EQ)
1664** are the same as opcode values (ex: OP_Eq) that implement the corresponding
1665** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
1666** the make process cause these values to align. Assert()s in the code
1667** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00001668*/
danielk19774adee202004-05-08 08:23:19 +00001669void sqlite3ExprCode(Parse *pParse, Expr *pExpr){
drhcce7d172000-05-31 15:34:51 +00001670 Vdbe *v = pParse->pVdbe;
1671 int op;
drhffe07b22005-11-03 00:41:17 +00001672 int stackChng = 1; /* Amount of change to stack depth */
1673
danielk19777977a172004-11-09 12:44:37 +00001674 if( v==0 ) return;
1675 if( pExpr==0 ){
drhf0863fe2005-06-12 21:35:51 +00001676 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
danielk19777977a172004-11-09 12:44:37 +00001677 return;
1678 }
drhf2bc0132004-10-04 13:19:23 +00001679 op = pExpr->op;
1680 switch( op ){
drh13449892005-09-07 21:22:45 +00001681 case TK_AGG_COLUMN: {
1682 AggInfo *pAggInfo = pExpr->pAggInfo;
1683 struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg];
1684 if( !pAggInfo->directMode ){
1685 sqlite3VdbeAddOp(v, OP_MemLoad, pCol->iMem, 0);
1686 break;
1687 }else if( pAggInfo->useSortingIdx ){
1688 sqlite3VdbeAddOp(v, OP_Column, pAggInfo->sortingIdx,
1689 pCol->iSorterColumn);
1690 break;
1691 }
1692 /* Otherwise, fall thru into the TK_COLUMN case */
1693 }
drh967e8b72000-06-21 13:59:10 +00001694 case TK_COLUMN: {
drhffe07b22005-11-03 00:41:17 +00001695 if( pExpr->iTable<0 ){
1696 /* This only happens when coding check constraints */
1697 assert( pParse->ckOffset>0 );
1698 sqlite3VdbeAddOp(v, OP_Dup, pParse->ckOffset-pExpr->iColumn-1, 1);
drhc4a3c772001-04-04 11:48:57 +00001699 }else{
drh945498f2007-02-24 11:52:52 +00001700 sqlite3ExprCodeGetColumn(v, pExpr->pTab, pExpr->iColumn, pExpr->iTable);
drh22827922000-06-06 17:27:05 +00001701 }
drhcce7d172000-05-31 15:34:51 +00001702 break;
1703 }
1704 case TK_INTEGER: {
drh2646da72005-12-09 20:02:05 +00001705 codeInteger(v, (char*)pExpr->token.z, pExpr->token.n);
drhfec19aa2004-05-19 20:41:03 +00001706 break;
1707 }
1708 case TK_FLOAT:
1709 case TK_STRING: {
drhf2bc0132004-10-04 13:19:23 +00001710 assert( TK_FLOAT==OP_Real );
1711 assert( TK_STRING==OP_String8 );
drhd2687b72005-08-12 22:56:09 +00001712 sqlite3DequoteExpr(pExpr);
drh2646da72005-12-09 20:02:05 +00001713 sqlite3VdbeOp3(v, op, 0, 0, (char*)pExpr->token.z, pExpr->token.n);
drhcce7d172000-05-31 15:34:51 +00001714 break;
1715 }
drhf0863fe2005-06-12 21:35:51 +00001716 case TK_NULL: {
1717 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
1718 break;
1719 }
danielk19775338a5f2005-01-20 13:03:10 +00001720#ifndef SQLITE_OMIT_BLOB_LITERAL
danielk1977c572ef72004-05-27 09:28:41 +00001721 case TK_BLOB: {
drh6c8c6ce2005-08-23 11:17:58 +00001722 int n;
1723 const char *z;
drhf2bc0132004-10-04 13:19:23 +00001724 assert( TK_BLOB==OP_HexBlob );
drh6c8c6ce2005-08-23 11:17:58 +00001725 n = pExpr->token.n - 3;
drh2646da72005-12-09 20:02:05 +00001726 z = (char*)pExpr->token.z + 2;
drh6c8c6ce2005-08-23 11:17:58 +00001727 assert( n>=0 );
1728 if( n==0 ){
1729 z = "";
1730 }
1731 sqlite3VdbeOp3(v, op, 0, 0, z, n);
danielk1977c572ef72004-05-27 09:28:41 +00001732 break;
1733 }
danielk19775338a5f2005-01-20 13:03:10 +00001734#endif
drh50457892003-09-06 01:10:47 +00001735 case TK_VARIABLE: {
danielk19774adee202004-05-08 08:23:19 +00001736 sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0);
drh895d7472004-08-20 16:02:39 +00001737 if( pExpr->token.n>1 ){
drh2646da72005-12-09 20:02:05 +00001738 sqlite3VdbeChangeP3(v, -1, (char*)pExpr->token.z, pExpr->token.n);
drh895d7472004-08-20 16:02:39 +00001739 }
drh50457892003-09-06 01:10:47 +00001740 break;
1741 }
drh4e0cff62004-11-05 05:10:28 +00001742 case TK_REGISTER: {
1743 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iTable, 0);
1744 break;
1745 }
drh487e2622005-06-25 18:42:14 +00001746#ifndef SQLITE_OMIT_CAST
1747 case TK_CAST: {
1748 /* Expressions of the form: CAST(pLeft AS token) */
danielk1977f0113002006-01-24 12:09:17 +00001749 int aff, to_op;
drh487e2622005-06-25 18:42:14 +00001750 sqlite3ExprCode(pParse, pExpr->pLeft);
drh8a512562005-11-14 22:29:05 +00001751 aff = sqlite3AffinityType(&pExpr->token);
danielk1977f0113002006-01-24 12:09:17 +00001752 to_op = aff - SQLITE_AFF_TEXT + OP_ToText;
1753 assert( to_op==OP_ToText || aff!=SQLITE_AFF_TEXT );
1754 assert( to_op==OP_ToBlob || aff!=SQLITE_AFF_NONE );
1755 assert( to_op==OP_ToNumeric || aff!=SQLITE_AFF_NUMERIC );
1756 assert( to_op==OP_ToInt || aff!=SQLITE_AFF_INTEGER );
1757 assert( to_op==OP_ToReal || aff!=SQLITE_AFF_REAL );
1758 sqlite3VdbeAddOp(v, to_op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001759 stackChng = 0;
drh487e2622005-06-25 18:42:14 +00001760 break;
1761 }
1762#endif /* SQLITE_OMIT_CAST */
drhc9b84a12002-06-20 11:36:48 +00001763 case TK_LT:
1764 case TK_LE:
1765 case TK_GT:
1766 case TK_GE:
1767 case TK_NE:
1768 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00001769 assert( TK_LT==OP_Lt );
1770 assert( TK_LE==OP_Le );
1771 assert( TK_GT==OP_Gt );
1772 assert( TK_GE==OP_Ge );
1773 assert( TK_EQ==OP_Eq );
1774 assert( TK_NE==OP_Ne );
danielk1977a37cdde2004-05-16 11:15:36 +00001775 sqlite3ExprCode(pParse, pExpr->pLeft);
1776 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001777 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001778 stackChng = -1;
danielk1977a37cdde2004-05-16 11:15:36 +00001779 break;
drhc9b84a12002-06-20 11:36:48 +00001780 }
drhcce7d172000-05-31 15:34:51 +00001781 case TK_AND:
1782 case TK_OR:
1783 case TK_PLUS:
1784 case TK_STAR:
1785 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00001786 case TK_REM:
1787 case TK_BITAND:
1788 case TK_BITOR:
drh17c40292004-07-21 02:53:29 +00001789 case TK_SLASH:
drhbf4133c2001-10-13 02:59:08 +00001790 case TK_LSHIFT:
drh855eb1c2004-08-31 13:45:11 +00001791 case TK_RSHIFT:
drh00400772000-06-16 20:51:26 +00001792 case TK_CONCAT: {
drhf2bc0132004-10-04 13:19:23 +00001793 assert( TK_AND==OP_And );
1794 assert( TK_OR==OP_Or );
1795 assert( TK_PLUS==OP_Add );
1796 assert( TK_MINUS==OP_Subtract );
1797 assert( TK_REM==OP_Remainder );
1798 assert( TK_BITAND==OP_BitAnd );
1799 assert( TK_BITOR==OP_BitOr );
1800 assert( TK_SLASH==OP_Divide );
1801 assert( TK_LSHIFT==OP_ShiftLeft );
1802 assert( TK_RSHIFT==OP_ShiftRight );
1803 assert( TK_CONCAT==OP_Concat );
danielk19774adee202004-05-08 08:23:19 +00001804 sqlite3ExprCode(pParse, pExpr->pLeft);
1805 sqlite3ExprCode(pParse, pExpr->pRight);
drh855eb1c2004-08-31 13:45:11 +00001806 sqlite3VdbeAddOp(v, op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001807 stackChng = -1;
drh00400772000-06-16 20:51:26 +00001808 break;
1809 }
drhcce7d172000-05-31 15:34:51 +00001810 case TK_UMINUS: {
drhfec19aa2004-05-19 20:41:03 +00001811 Expr *pLeft = pExpr->pLeft;
1812 assert( pLeft );
1813 if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
1814 Token *p = &pLeft->token;
drh9267bdc2005-11-28 12:36:35 +00001815 char *z = sqlite3MPrintf("-%.*s", p->n, p->z);
drhfec19aa2004-05-19 20:41:03 +00001816 if( pLeft->op==TK_FLOAT ){
1817 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001818 }else{
drhfec19aa2004-05-19 20:41:03 +00001819 codeInteger(v, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001820 }
drh6e142f52000-06-08 13:36:40 +00001821 sqliteFree(z);
1822 break;
1823 }
drh1ccde152000-06-17 13:12:39 +00001824 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +00001825 }
drhbf4133c2001-10-13 02:59:08 +00001826 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00001827 case TK_NOT: {
drhf2bc0132004-10-04 13:19:23 +00001828 assert( TK_BITNOT==OP_BitNot );
1829 assert( TK_NOT==OP_Not );
danielk19774adee202004-05-08 08:23:19 +00001830 sqlite3ExprCode(pParse, pExpr->pLeft);
1831 sqlite3VdbeAddOp(v, op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001832 stackChng = 0;
drhcce7d172000-05-31 15:34:51 +00001833 break;
1834 }
1835 case TK_ISNULL:
1836 case TK_NOTNULL: {
1837 int dest;
drhf2bc0132004-10-04 13:19:23 +00001838 assert( TK_ISNULL==OP_IsNull );
1839 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00001840 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1841 sqlite3ExprCode(pParse, pExpr->pLeft);
1842 dest = sqlite3VdbeCurrentAddr(v) + 2;
1843 sqlite3VdbeAddOp(v, op, 1, dest);
1844 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);
drhffe07b22005-11-03 00:41:17 +00001845 stackChng = 0;
drhf2bc0132004-10-04 13:19:23 +00001846 break;
drhcce7d172000-05-31 15:34:51 +00001847 }
drh22827922000-06-06 17:27:05 +00001848 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00001849 AggInfo *pInfo = pExpr->pAggInfo;
drh7e56e712005-11-16 12:53:15 +00001850 if( pInfo==0 ){
1851 sqlite3ErrorMsg(pParse, "misuse of aggregate: %T",
1852 &pExpr->span);
1853 }else{
1854 sqlite3VdbeAddOp(v, OP_MemLoad, pInfo->aFunc[pExpr->iAgg].iMem, 0);
1855 }
drh22827922000-06-06 17:27:05 +00001856 break;
1857 }
drhb71090f2005-05-23 17:26:51 +00001858 case TK_CONST_FUNC:
drhcce7d172000-05-31 15:34:51 +00001859 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00001860 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00001861 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00001862 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00001863 int nId;
1864 const char *zId;
drh13449892005-09-07 21:22:45 +00001865 int constMask = 0;
danielk1977682f68b2004-06-05 10:22:17 +00001866 int i;
danielk197714db2662006-01-09 16:12:04 +00001867 u8 enc = ENC(pParse->db);
danielk1977dc1bdc42004-06-11 10:51:27 +00001868 CollSeq *pColl = 0;
drh2646da72005-12-09 20:02:05 +00001869 zId = (char*)pExpr->token.z;
drhb71090f2005-05-23 17:26:51 +00001870 nId = pExpr->token.n;
danielk1977d8123362004-06-12 09:25:12 +00001871 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001872 assert( pDef!=0 );
drhf9b596e2004-05-26 16:54:42 +00001873 nExpr = sqlite3ExprCodeExprList(pParse, pList);
drhb7f6f682006-07-08 17:06:43 +00001874#ifndef SQLITE_OMIT_VIRTUALTABLE
drha43fa222006-07-08 18:41:37 +00001875 /* Possibly overload the function if the first argument is
1876 ** a virtual table column.
1877 **
1878 ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the
1879 ** second argument, not the first, as the argument to test to
1880 ** see if it is a column in a virtual table. This is done because
1881 ** the left operand of infix functions (the operand we want to
1882 ** control overloading) ends up as the second argument to the
1883 ** function. The expression "A glob B" is equivalent to
1884 ** "glob(B,A). We want to use the A in "A glob B" to test
1885 ** for function overloading. But we use the B term in "glob(B,A)".
1886 */
drh6a03a1c2006-07-08 18:34:59 +00001887 if( nExpr>=2 && (pExpr->flags & EP_InfixFunc) ){
drh6a03a1c2006-07-08 18:34:59 +00001888 pDef = sqlite3VtabOverloadFunction(pDef, nExpr, pList->a[1].pExpr);
1889 }else if( nExpr>0 ){
drhb7f6f682006-07-08 17:06:43 +00001890 pDef = sqlite3VtabOverloadFunction(pDef, nExpr, pList->a[0].pExpr);
1891 }
1892#endif
danielk1977682f68b2004-06-05 10:22:17 +00001893 for(i=0; i<nExpr && i<32; i++){
danielk1977d02eb1f2004-06-06 09:44:03 +00001894 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
drh13449892005-09-07 21:22:45 +00001895 constMask |= (1<<i);
danielk1977d02eb1f2004-06-06 09:44:03 +00001896 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001897 if( pDef->needCollSeq && !pColl ){
1898 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
1899 }
1900 }
1901 if( pDef->needCollSeq ){
1902 if( !pColl ) pColl = pParse->db->pDfltColl;
danielk1977d8123362004-06-12 09:25:12 +00001903 sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ);
danielk1977682f68b2004-06-05 10:22:17 +00001904 }
drh13449892005-09-07 21:22:45 +00001905 sqlite3VdbeOp3(v, OP_Function, constMask, nExpr, (char*)pDef, P3_FUNCDEF);
drhffe07b22005-11-03 00:41:17 +00001906 stackChng = 1-nExpr;
drhcce7d172000-05-31 15:34:51 +00001907 break;
1908 }
drhfe2093d2005-01-20 22:48:47 +00001909#ifndef SQLITE_OMIT_SUBQUERY
1910 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001911 case TK_SELECT: {
drh41714d62006-03-02 04:44:23 +00001912 if( pExpr->iColumn==0 ){
1913 sqlite3CodeSubselect(pParse, pExpr);
1914 }
danielk19774adee202004-05-08 08:23:19 +00001915 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drhad6d9462004-09-19 02:15:24 +00001916 VdbeComment((v, "# load subquery result"));
drh19a775c2000-06-05 18:54:46 +00001917 break;
1918 }
drhfef52082000-06-06 01:50:43 +00001919 case TK_IN: {
1920 int addr;
drh94a11212004-09-25 13:12:14 +00001921 char affinity;
drhafa5f682006-01-30 14:36:59 +00001922 int ckOffset = pParse->ckOffset;
danielk1977b3bce662005-01-29 08:32:43 +00001923 sqlite3CodeSubselect(pParse, pExpr);
danielk1977e014a832004-05-17 10:48:57 +00001924
1925 /* Figure out the affinity to use to create a key from the results
1926 ** of the expression. affinityStr stores a static string suitable for
danielk1977ededfd52004-06-17 07:53:01 +00001927 ** P3 of OP_MakeRecord.
danielk1977e014a832004-05-17 10:48:57 +00001928 */
drh94a11212004-09-25 13:12:14 +00001929 affinity = comparisonAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +00001930
danielk19774adee202004-05-08 08:23:19 +00001931 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
danielk1977cdbd8ef2007-05-12 06:11:12 +00001932 pParse->ckOffset = (ckOffset ? (ckOffset+1) : 0);
danielk1977e014a832004-05-17 10:48:57 +00001933
1934 /* Code the <expr> from "<expr> IN (...)". The temporary table
1935 ** pExpr->iTable contains the values that make up the (...) set.
1936 */
danielk19774adee202004-05-08 08:23:19 +00001937 sqlite3ExprCode(pParse, pExpr->pLeft);
1938 addr = sqlite3VdbeCurrentAddr(v);
danielk1977e014a832004-05-17 10:48:57 +00001939 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); /* addr + 0 */
danielk19774adee202004-05-08 08:23:19 +00001940 sqlite3VdbeAddOp(v, OP_Pop, 2, 0);
drhf0863fe2005-06-12 21:35:51 +00001941 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +00001942 sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7);
drh94a11212004-09-25 13:12:14 +00001943 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1); /* addr + 4 */
danielk1977e014a832004-05-17 10:48:57 +00001944 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7);
1945 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); /* addr + 6 */
1946
drhfef52082000-06-06 01:50:43 +00001947 break;
1948 }
danielk197793758c82005-01-21 08:13:14 +00001949#endif
drhfef52082000-06-06 01:50:43 +00001950 case TK_BETWEEN: {
drhbe5c89a2004-07-26 00:31:09 +00001951 Expr *pLeft = pExpr->pLeft;
1952 struct ExprList_item *pLItem = pExpr->pList->a;
1953 Expr *pRight = pLItem->pExpr;
1954 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001955 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001956 sqlite3ExprCode(pParse, pRight);
1957 codeCompare(pParse, pLeft, pRight, OP_Ge, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001958 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
drhbe5c89a2004-07-26 00:31:09 +00001959 pLItem++;
1960 pRight = pLItem->pExpr;
1961 sqlite3ExprCode(pParse, pRight);
1962 codeCompare(pParse, pLeft, pRight, OP_Le, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001963 sqlite3VdbeAddOp(v, OP_And, 0, 0);
drhfef52082000-06-06 01:50:43 +00001964 break;
1965 }
drh4f07e5f2007-05-14 11:34:46 +00001966 case TK_UPLUS: {
danielk19774adee202004-05-08 08:23:19 +00001967 sqlite3ExprCode(pParse, pExpr->pLeft);
drhffe07b22005-11-03 00:41:17 +00001968 stackChng = 0;
drha2e00042002-01-22 03:13:42 +00001969 break;
1970 }
drh17a7f8d2002-03-24 13:13:27 +00001971 case TK_CASE: {
1972 int expr_end_label;
drhf5905aa2002-05-26 20:54:33 +00001973 int jumpInst;
drhf5905aa2002-05-26 20:54:33 +00001974 int nExpr;
drh17a7f8d2002-03-24 13:13:27 +00001975 int i;
drhbe5c89a2004-07-26 00:31:09 +00001976 ExprList *pEList;
1977 struct ExprList_item *aListelem;
drh17a7f8d2002-03-24 13:13:27 +00001978
1979 assert(pExpr->pList);
1980 assert((pExpr->pList->nExpr % 2) == 0);
1981 assert(pExpr->pList->nExpr > 0);
drhbe5c89a2004-07-26 00:31:09 +00001982 pEList = pExpr->pList;
1983 aListelem = pEList->a;
1984 nExpr = pEList->nExpr;
danielk19774adee202004-05-08 08:23:19 +00001985 expr_end_label = sqlite3VdbeMakeLabel(v);
drh17a7f8d2002-03-24 13:13:27 +00001986 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001987 sqlite3ExprCode(pParse, pExpr->pLeft);
drh17a7f8d2002-03-24 13:13:27 +00001988 }
drhf5905aa2002-05-26 20:54:33 +00001989 for(i=0; i<nExpr; i=i+2){
drhbe5c89a2004-07-26 00:31:09 +00001990 sqlite3ExprCode(pParse, aListelem[i].pExpr);
drh17a7f8d2002-03-24 13:13:27 +00001991 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001992 sqlite3VdbeAddOp(v, OP_Dup, 1, 1);
drhbe5c89a2004-07-26 00:31:09 +00001993 jumpInst = codeCompare(pParse, pExpr->pLeft, aListelem[i].pExpr,
1994 OP_Ne, 0, 1);
danielk19774adee202004-05-08 08:23:19 +00001995 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001996 }else{
danielk19774adee202004-05-08 08:23:19 +00001997 jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001998 }
drhbe5c89a2004-07-26 00:31:09 +00001999 sqlite3ExprCode(pParse, aListelem[i+1].pExpr);
danielk19774adee202004-05-08 08:23:19 +00002000 sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label);
drhd654be82005-09-20 17:42:23 +00002001 sqlite3VdbeJumpHere(v, jumpInst);
drh17a7f8d2002-03-24 13:13:27 +00002002 }
drhf570f012002-05-31 15:51:25 +00002003 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00002004 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhf570f012002-05-31 15:51:25 +00002005 }
drh17a7f8d2002-03-24 13:13:27 +00002006 if( pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00002007 sqlite3ExprCode(pParse, pExpr->pRight);
drh17a7f8d2002-03-24 13:13:27 +00002008 }else{
drhf0863fe2005-06-12 21:35:51 +00002009 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
drh17a7f8d2002-03-24 13:13:27 +00002010 }
danielk19774adee202004-05-08 08:23:19 +00002011 sqlite3VdbeResolveLabel(v, expr_end_label);
danielk19776f349032002-06-11 02:25:40 +00002012 break;
2013 }
danielk19775338a5f2005-01-20 13:03:10 +00002014#ifndef SQLITE_OMIT_TRIGGER
danielk19776f349032002-06-11 02:25:40 +00002015 case TK_RAISE: {
2016 if( !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +00002017 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00002018 "RAISE() may only be used within a trigger-program");
danielk19776f349032002-06-11 02:25:40 +00002019 return;
2020 }
drhad6d9462004-09-19 02:15:24 +00002021 if( pExpr->iColumn!=OE_Ignore ){
2022 assert( pExpr->iColumn==OE_Rollback ||
2023 pExpr->iColumn == OE_Abort ||
2024 pExpr->iColumn == OE_Fail );
drhd2687b72005-08-12 22:56:09 +00002025 sqlite3DequoteExpr(pExpr);
drhad6d9462004-09-19 02:15:24 +00002026 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn,
drh2646da72005-12-09 20:02:05 +00002027 (char*)pExpr->token.z, pExpr->token.n);
danielk19776f349032002-06-11 02:25:40 +00002028 } else {
drhad6d9462004-09-19 02:15:24 +00002029 assert( pExpr->iColumn == OE_Ignore );
2030 sqlite3VdbeAddOp(v, OP_ContextPop, 0, 0);
2031 sqlite3VdbeAddOp(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
2032 VdbeComment((v, "# raise(IGNORE)"));
danielk19776f349032002-06-11 02:25:40 +00002033 }
drhffe07b22005-11-03 00:41:17 +00002034 stackChng = 0;
2035 break;
drh17a7f8d2002-03-24 13:13:27 +00002036 }
danielk19775338a5f2005-01-20 13:03:10 +00002037#endif
drhffe07b22005-11-03 00:41:17 +00002038 }
2039
2040 if( pParse->ckOffset ){
2041 pParse->ckOffset += stackChng;
2042 assert( pParse->ckOffset );
drhcce7d172000-05-31 15:34:51 +00002043 }
drhcce7d172000-05-31 15:34:51 +00002044}
2045
danielk197793758c82005-01-21 08:13:14 +00002046#ifndef SQLITE_OMIT_TRIGGER
drhcce7d172000-05-31 15:34:51 +00002047/*
drh25303782004-12-07 15:41:48 +00002048** Generate code that evalutes the given expression and leaves the result
2049** on the stack. See also sqlite3ExprCode().
2050**
2051** This routine might also cache the result and modify the pExpr tree
2052** so that it will make use of the cached result on subsequent evaluations
2053** rather than evaluate the whole expression again. Trivial expressions are
2054** not cached. If the expression is cached, its result is stored in a
2055** memory location.
2056*/
2057void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr){
2058 Vdbe *v = pParse->pVdbe;
2059 int iMem;
2060 int addr1, addr2;
2061 if( v==0 ) return;
2062 addr1 = sqlite3VdbeCurrentAddr(v);
2063 sqlite3ExprCode(pParse, pExpr);
2064 addr2 = sqlite3VdbeCurrentAddr(v);
2065 if( addr2>addr1+1 || sqlite3VdbeGetOp(v, addr1)->opcode==OP_Function ){
2066 iMem = pExpr->iTable = pParse->nMem++;
2067 sqlite3VdbeAddOp(v, OP_MemStore, iMem, 0);
2068 pExpr->op = TK_REGISTER;
2069 }
2070}
danielk197793758c82005-01-21 08:13:14 +00002071#endif
drh25303782004-12-07 15:41:48 +00002072
2073/*
drh268380c2004-02-25 13:47:31 +00002074** Generate code that pushes the value of every element of the given
drhf9b596e2004-05-26 16:54:42 +00002075** expression list onto the stack.
drh268380c2004-02-25 13:47:31 +00002076**
2077** Return the number of elements pushed onto the stack.
2078*/
danielk19774adee202004-05-08 08:23:19 +00002079int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00002080 Parse *pParse, /* Parsing context */
drhf9b596e2004-05-26 16:54:42 +00002081 ExprList *pList /* The expression list to be coded */
drh268380c2004-02-25 13:47:31 +00002082){
2083 struct ExprList_item *pItem;
2084 int i, n;
drh268380c2004-02-25 13:47:31 +00002085 if( pList==0 ) return 0;
drh268380c2004-02-25 13:47:31 +00002086 n = pList->nExpr;
drhc182d162005-08-14 20:47:16 +00002087 for(pItem=pList->a, i=n; i>0; i--, pItem++){
danielk19774adee202004-05-08 08:23:19 +00002088 sqlite3ExprCode(pParse, pItem->pExpr);
drh268380c2004-02-25 13:47:31 +00002089 }
drhf9b596e2004-05-26 16:54:42 +00002090 return n;
drh268380c2004-02-25 13:47:31 +00002091}
2092
2093/*
drhcce7d172000-05-31 15:34:51 +00002094** Generate code for a boolean expression such that a jump is made
2095** to the label "dest" if the expression is true but execution
2096** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00002097**
2098** If the expression evaluates to NULL (neither true nor false), then
2099** take the jump if the jumpIfNull flag is true.
drhf2bc0132004-10-04 13:19:23 +00002100**
2101** This code depends on the fact that certain token values (ex: TK_EQ)
2102** are the same as opcode values (ex: OP_Eq) that implement the corresponding
2103** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
2104** the make process cause these values to align. Assert()s in the code
2105** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00002106*/
danielk19774adee202004-05-08 08:23:19 +00002107void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00002108 Vdbe *v = pParse->pVdbe;
2109 int op = 0;
drhffe07b22005-11-03 00:41:17 +00002110 int ckOffset = pParse->ckOffset;
drhdaffd0e2001-04-11 14:28:42 +00002111 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00002112 op = pExpr->op;
2113 switch( op ){
drhcce7d172000-05-31 15:34:51 +00002114 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00002115 int d2 = sqlite3VdbeMakeLabel(v);
2116 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
2117 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
2118 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00002119 break;
2120 }
2121 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00002122 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
2123 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002124 break;
2125 }
2126 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00002127 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002128 break;
2129 }
2130 case TK_LT:
2131 case TK_LE:
2132 case TK_GT:
2133 case TK_GE:
2134 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00002135 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00002136 assert( TK_LT==OP_Lt );
2137 assert( TK_LE==OP_Le );
2138 assert( TK_GT==OP_Gt );
2139 assert( TK_GE==OP_Ge );
2140 assert( TK_EQ==OP_Eq );
2141 assert( TK_NE==OP_Ne );
danielk19774adee202004-05-08 08:23:19 +00002142 sqlite3ExprCode(pParse, pExpr->pLeft);
2143 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00002144 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002145 break;
2146 }
2147 case TK_ISNULL:
2148 case TK_NOTNULL: {
drhf2bc0132004-10-04 13:19:23 +00002149 assert( TK_ISNULL==OP_IsNull );
2150 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00002151 sqlite3ExprCode(pParse, pExpr->pLeft);
2152 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00002153 break;
2154 }
drhfef52082000-06-06 01:50:43 +00002155 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00002156 /* The expression "x BETWEEN y AND z" is implemented as:
2157 **
2158 ** 1 IF (x < y) GOTO 3
2159 ** 2 IF (x <= z) GOTO <dest>
2160 ** 3 ...
2161 */
drhf5905aa2002-05-26 20:54:33 +00002162 int addr;
drhbe5c89a2004-07-26 00:31:09 +00002163 Expr *pLeft = pExpr->pLeft;
2164 Expr *pRight = pExpr->pList->a[0].pExpr;
2165 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00002166 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00002167 sqlite3ExprCode(pParse, pRight);
2168 addr = codeCompare(pParse, pLeft, pRight, OP_Lt, 0, !jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00002169
drhbe5c89a2004-07-26 00:31:09 +00002170 pRight = pExpr->pList->a[1].pExpr;
2171 sqlite3ExprCode(pParse, pRight);
2172 codeCompare(pParse, pLeft, pRight, OP_Le, dest, jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00002173
danielk19774adee202004-05-08 08:23:19 +00002174 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
drhd654be82005-09-20 17:42:23 +00002175 sqlite3VdbeJumpHere(v, addr);
danielk19774adee202004-05-08 08:23:19 +00002176 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00002177 break;
2178 }
drhcce7d172000-05-31 15:34:51 +00002179 default: {
danielk19774adee202004-05-08 08:23:19 +00002180 sqlite3ExprCode(pParse, pExpr);
2181 sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00002182 break;
2183 }
2184 }
drhffe07b22005-11-03 00:41:17 +00002185 pParse->ckOffset = ckOffset;
drhcce7d172000-05-31 15:34:51 +00002186}
2187
2188/*
drh66b89c82000-11-28 20:47:17 +00002189** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00002190** to the label "dest" if the expression is false but execution
2191** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00002192**
2193** If the expression evaluates to NULL (neither true nor false) then
2194** jump if jumpIfNull is true or fall through if jumpIfNull is false.
drhcce7d172000-05-31 15:34:51 +00002195*/
danielk19774adee202004-05-08 08:23:19 +00002196void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00002197 Vdbe *v = pParse->pVdbe;
2198 int op = 0;
drhffe07b22005-11-03 00:41:17 +00002199 int ckOffset = pParse->ckOffset;
drhdaffd0e2001-04-11 14:28:42 +00002200 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00002201
2202 /* The value of pExpr->op and op are related as follows:
2203 **
2204 ** pExpr->op op
2205 ** --------- ----------
2206 ** TK_ISNULL OP_NotNull
2207 ** TK_NOTNULL OP_IsNull
2208 ** TK_NE OP_Eq
2209 ** TK_EQ OP_Ne
2210 ** TK_GT OP_Le
2211 ** TK_LE OP_Gt
2212 ** TK_GE OP_Lt
2213 ** TK_LT OP_Ge
2214 **
2215 ** For other values of pExpr->op, op is undefined and unused.
2216 ** The value of TK_ and OP_ constants are arranged such that we
2217 ** can compute the mapping above using the following expression.
2218 ** Assert()s verify that the computation is correct.
2219 */
2220 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
2221
2222 /* Verify correct alignment of TK_ and OP_ constants
2223 */
2224 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
2225 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
2226 assert( pExpr->op!=TK_NE || op==OP_Eq );
2227 assert( pExpr->op!=TK_EQ || op==OP_Ne );
2228 assert( pExpr->op!=TK_LT || op==OP_Ge );
2229 assert( pExpr->op!=TK_LE || op==OP_Gt );
2230 assert( pExpr->op!=TK_GT || op==OP_Le );
2231 assert( pExpr->op!=TK_GE || op==OP_Lt );
2232
drhcce7d172000-05-31 15:34:51 +00002233 switch( pExpr->op ){
2234 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00002235 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
2236 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002237 break;
2238 }
2239 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00002240 int d2 = sqlite3VdbeMakeLabel(v);
2241 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
2242 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
2243 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00002244 break;
2245 }
2246 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00002247 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002248 break;
2249 }
2250 case TK_LT:
2251 case TK_LE:
2252 case TK_GT:
2253 case TK_GE:
2254 case TK_NE:
2255 case TK_EQ: {
danielk19774adee202004-05-08 08:23:19 +00002256 sqlite3ExprCode(pParse, pExpr->pLeft);
2257 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00002258 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002259 break;
2260 }
drhcce7d172000-05-31 15:34:51 +00002261 case TK_ISNULL:
2262 case TK_NOTNULL: {
danielk19774adee202004-05-08 08:23:19 +00002263 sqlite3ExprCode(pParse, pExpr->pLeft);
2264 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00002265 break;
2266 }
drhfef52082000-06-06 01:50:43 +00002267 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00002268 /* The expression is "x BETWEEN y AND z". It is implemented as:
2269 **
2270 ** 1 IF (x >= y) GOTO 3
2271 ** 2 GOTO <dest>
2272 ** 3 IF (x > z) GOTO <dest>
2273 */
drhfef52082000-06-06 01:50:43 +00002274 int addr;
drhbe5c89a2004-07-26 00:31:09 +00002275 Expr *pLeft = pExpr->pLeft;
2276 Expr *pRight = pExpr->pList->a[0].pExpr;
2277 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00002278 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00002279 sqlite3ExprCode(pParse, pRight);
danielk19774adee202004-05-08 08:23:19 +00002280 addr = sqlite3VdbeCurrentAddr(v);
drhbe5c89a2004-07-26 00:31:09 +00002281 codeCompare(pParse, pLeft, pRight, OP_Ge, addr+3, !jumpIfNull);
2282
danielk19774adee202004-05-08 08:23:19 +00002283 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
2284 sqlite3VdbeAddOp(v, OP_Goto, 0, dest);
drhbe5c89a2004-07-26 00:31:09 +00002285 pRight = pExpr->pList->a[1].pExpr;
2286 sqlite3ExprCode(pParse, pRight);
2287 codeCompare(pParse, pLeft, pRight, OP_Gt, dest, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00002288 break;
2289 }
drhcce7d172000-05-31 15:34:51 +00002290 default: {
danielk19774adee202004-05-08 08:23:19 +00002291 sqlite3ExprCode(pParse, pExpr);
2292 sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00002293 break;
2294 }
2295 }
drhffe07b22005-11-03 00:41:17 +00002296 pParse->ckOffset = ckOffset;
drhcce7d172000-05-31 15:34:51 +00002297}
drh22827922000-06-06 17:27:05 +00002298
2299/*
2300** Do a deep comparison of two expression trees. Return TRUE (non-zero)
2301** if they are identical and return FALSE if they differ in any way.
drhd40aab02007-02-24 15:29:03 +00002302**
2303** Sometimes this routine will return FALSE even if the two expressions
2304** really are equivalent. If we cannot prove that the expressions are
2305** identical, we return FALSE just to be safe. So if this routine
2306** returns false, then you do not really know for certain if the two
2307** expressions are the same. But if you get a TRUE return, then you
2308** can be sure the expressions are the same. In the places where
2309** this routine is used, it does not hurt to get an extra FALSE - that
2310** just might result in some slightly slower code. But returning
2311** an incorrect TRUE could lead to a malfunction.
drh22827922000-06-06 17:27:05 +00002312*/
danielk19774adee202004-05-08 08:23:19 +00002313int sqlite3ExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00002314 int i;
danielk19774b202ae2006-01-23 05:50:58 +00002315 if( pA==0||pB==0 ){
2316 return pB==pA;
drh22827922000-06-06 17:27:05 +00002317 }
2318 if( pA->op!=pB->op ) return 0;
drhfd357972005-09-09 01:33:19 +00002319 if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 0;
danielk19774adee202004-05-08 08:23:19 +00002320 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
2321 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00002322 if( pA->pList ){
2323 if( pB->pList==0 ) return 0;
2324 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
2325 for(i=0; i<pA->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00002326 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00002327 return 0;
2328 }
2329 }
2330 }else if( pB->pList ){
2331 return 0;
2332 }
2333 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00002334 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drhdd735212007-02-24 13:53:05 +00002335 if( pA->op!=TK_COLUMN && pA->token.z ){
drh22827922000-06-06 17:27:05 +00002336 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00002337 if( pB->token.n!=pA->token.n ) return 0;
drh2646da72005-12-09 20:02:05 +00002338 if( sqlite3StrNICmp((char*)pA->token.z,(char*)pB->token.z,pB->token.n)!=0 ){
2339 return 0;
2340 }
drh22827922000-06-06 17:27:05 +00002341 }
2342 return 1;
2343}
2344
drh13449892005-09-07 21:22:45 +00002345
drh22827922000-06-06 17:27:05 +00002346/*
drh13449892005-09-07 21:22:45 +00002347** Add a new element to the pAggInfo->aCol[] array. Return the index of
2348** the new element. Return a negative number if malloc fails.
drh22827922000-06-06 17:27:05 +00002349*/
drh13449892005-09-07 21:22:45 +00002350static int addAggInfoColumn(AggInfo *pInfo){
2351 int i;
drhcf643722007-03-27 13:36:37 +00002352 pInfo->aCol = sqlite3ArrayAllocate(
2353 pInfo->aCol,
2354 sizeof(pInfo->aCol[0]),
2355 3,
2356 &pInfo->nColumn,
2357 &pInfo->nColumnAlloc,
2358 &i
2359 );
drh13449892005-09-07 21:22:45 +00002360 return i;
2361}
2362
2363/*
2364** Add a new element to the pAggInfo->aFunc[] array. Return the index of
2365** the new element. Return a negative number if malloc fails.
2366*/
2367static int addAggInfoFunc(AggInfo *pInfo){
2368 int i;
drhcf643722007-03-27 13:36:37 +00002369 pInfo->aFunc = sqlite3ArrayAllocate(
2370 pInfo->aFunc,
2371 sizeof(pInfo->aFunc[0]),
2372 3,
2373 &pInfo->nFunc,
2374 &pInfo->nFuncAlloc,
2375 &i
2376 );
drh13449892005-09-07 21:22:45 +00002377 return i;
2378}
drh22827922000-06-06 17:27:05 +00002379
2380/*
drh626a8792005-01-17 22:08:19 +00002381** This is an xFunc for walkExprTree() used to implement
2382** sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates
2383** for additional information.
drh22827922000-06-06 17:27:05 +00002384**
drh626a8792005-01-17 22:08:19 +00002385** This routine analyzes the aggregate function at pExpr.
drh22827922000-06-06 17:27:05 +00002386*/
drh626a8792005-01-17 22:08:19 +00002387static int analyzeAggregate(void *pArg, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00002388 int i;
danielk1977a58fdfb2005-02-08 07:50:40 +00002389 NameContext *pNC = (NameContext *)pArg;
2390 Parse *pParse = pNC->pParse;
2391 SrcList *pSrcList = pNC->pSrcList;
drh13449892005-09-07 21:22:45 +00002392 AggInfo *pAggInfo = pNC->pAggInfo;
2393
drh22827922000-06-06 17:27:05 +00002394
drh22827922000-06-06 17:27:05 +00002395 switch( pExpr->op ){
drh89c69d02007-01-04 01:20:28 +00002396 case TK_AGG_COLUMN:
drh967e8b72000-06-21 13:59:10 +00002397 case TK_COLUMN: {
drh13449892005-09-07 21:22:45 +00002398 /* Check to see if the column is in one of the tables in the FROM
2399 ** clause of the aggregate query */
2400 if( pSrcList ){
2401 struct SrcList_item *pItem = pSrcList->a;
2402 for(i=0; i<pSrcList->nSrc; i++, pItem++){
2403 struct AggInfo_col *pCol;
2404 if( pExpr->iTable==pItem->iCursor ){
2405 /* If we reach this point, it means that pExpr refers to a table
2406 ** that is in the FROM clause of the aggregate query.
2407 **
2408 ** Make an entry for the column in pAggInfo->aCol[] if there
2409 ** is not an entry there already.
2410 */
drh7f906d62007-03-12 23:48:52 +00002411 int k;
drh13449892005-09-07 21:22:45 +00002412 pCol = pAggInfo->aCol;
drh7f906d62007-03-12 23:48:52 +00002413 for(k=0; k<pAggInfo->nColumn; k++, pCol++){
drh13449892005-09-07 21:22:45 +00002414 if( pCol->iTable==pExpr->iTable &&
2415 pCol->iColumn==pExpr->iColumn ){
2416 break;
2417 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002418 }
drh7f906d62007-03-12 23:48:52 +00002419 if( k>=pAggInfo->nColumn && (k = addAggInfoColumn(pAggInfo))>=0 ){
2420 pCol = &pAggInfo->aCol[k];
danielk19770817d0d2007-02-14 09:19:36 +00002421 pCol->pTab = pExpr->pTab;
drh13449892005-09-07 21:22:45 +00002422 pCol->iTable = pExpr->iTable;
2423 pCol->iColumn = pExpr->iColumn;
2424 pCol->iMem = pParse->nMem++;
2425 pCol->iSorterColumn = -1;
drh5774b802005-09-07 22:48:16 +00002426 pCol->pExpr = pExpr;
drh13449892005-09-07 21:22:45 +00002427 if( pAggInfo->pGroupBy ){
2428 int j, n;
2429 ExprList *pGB = pAggInfo->pGroupBy;
2430 struct ExprList_item *pTerm = pGB->a;
2431 n = pGB->nExpr;
2432 for(j=0; j<n; j++, pTerm++){
2433 Expr *pE = pTerm->pExpr;
2434 if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable &&
2435 pE->iColumn==pExpr->iColumn ){
2436 pCol->iSorterColumn = j;
2437 break;
2438 }
2439 }
2440 }
2441 if( pCol->iSorterColumn<0 ){
2442 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
2443 }
2444 }
2445 /* There is now an entry for pExpr in pAggInfo->aCol[] (either
2446 ** because it was there before or because we just created it).
2447 ** Convert the pExpr to be a TK_AGG_COLUMN referring to that
2448 ** pAggInfo->aCol[] entry.
2449 */
2450 pExpr->pAggInfo = pAggInfo;
2451 pExpr->op = TK_AGG_COLUMN;
drh7f906d62007-03-12 23:48:52 +00002452 pExpr->iAgg = k;
drh13449892005-09-07 21:22:45 +00002453 break;
2454 } /* endif pExpr->iTable==pItem->iCursor */
2455 } /* end loop over pSrcList */
drh22827922000-06-06 17:27:05 +00002456 }
drh626a8792005-01-17 22:08:19 +00002457 return 1;
drh22827922000-06-06 17:27:05 +00002458 }
2459 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00002460 /* The pNC->nDepth==0 test causes aggregate functions in subqueries
2461 ** to be ignored */
danielk1977a58fdfb2005-02-08 07:50:40 +00002462 if( pNC->nDepth==0 ){
drh13449892005-09-07 21:22:45 +00002463 /* Check to see if pExpr is a duplicate of another aggregate
2464 ** function that is already in the pAggInfo structure
2465 */
2466 struct AggInfo_func *pItem = pAggInfo->aFunc;
2467 for(i=0; i<pAggInfo->nFunc; i++, pItem++){
2468 if( sqlite3ExprCompare(pItem->pExpr, pExpr) ){
danielk1977a58fdfb2005-02-08 07:50:40 +00002469 break;
2470 }
drh22827922000-06-06 17:27:05 +00002471 }
drh13449892005-09-07 21:22:45 +00002472 if( i>=pAggInfo->nFunc ){
2473 /* pExpr is original. Make a new entry in pAggInfo->aFunc[]
2474 */
danielk197714db2662006-01-09 16:12:04 +00002475 u8 enc = ENC(pParse->db);
drh13449892005-09-07 21:22:45 +00002476 i = addAggInfoFunc(pAggInfo);
2477 if( i>=0 ){
2478 pItem = &pAggInfo->aFunc[i];
2479 pItem->pExpr = pExpr;
2480 pItem->iMem = pParse->nMem++;
2481 pItem->pFunc = sqlite3FindFunction(pParse->db,
drh2646da72005-12-09 20:02:05 +00002482 (char*)pExpr->token.z, pExpr->token.n,
drh13449892005-09-07 21:22:45 +00002483 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
drhfd357972005-09-09 01:33:19 +00002484 if( pExpr->flags & EP_Distinct ){
2485 pItem->iDistinct = pParse->nTab++;
2486 }else{
2487 pItem->iDistinct = -1;
2488 }
drh13449892005-09-07 21:22:45 +00002489 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002490 }
drh13449892005-09-07 21:22:45 +00002491 /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
2492 */
danielk1977a58fdfb2005-02-08 07:50:40 +00002493 pExpr->iAgg = i;
drh13449892005-09-07 21:22:45 +00002494 pExpr->pAggInfo = pAggInfo;
danielk1977a58fdfb2005-02-08 07:50:40 +00002495 return 1;
drh22827922000-06-06 17:27:05 +00002496 }
drh22827922000-06-06 17:27:05 +00002497 }
2498 }
drh13449892005-09-07 21:22:45 +00002499
2500 /* Recursively walk subqueries looking for TK_COLUMN nodes that need
2501 ** to be changed to TK_AGG_COLUMN. But increment nDepth so that
2502 ** TK_AGG_FUNCTION nodes in subqueries will be unchanged.
2503 */
danielk1977a58fdfb2005-02-08 07:50:40 +00002504 if( pExpr->pSelect ){
2505 pNC->nDepth++;
2506 walkSelectExpr(pExpr->pSelect, analyzeAggregate, pNC);
2507 pNC->nDepth--;
2508 }
drh626a8792005-01-17 22:08:19 +00002509 return 0;
2510}
2511
2512/*
2513** Analyze the given expression looking for aggregate functions and
2514** for variables that need to be added to the pParse->aAgg[] array.
2515** Make additional entries to the pParse->aAgg[] array as necessary.
2516**
2517** This routine should only be called after the expression has been
2518** analyzed by sqlite3ExprResolveNames().
2519**
2520** If errors are seen, leave an error message in zErrMsg and return
2521** the number of errors.
2522*/
danielk1977a58fdfb2005-02-08 07:50:40 +00002523int sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
2524 int nErr = pNC->pParse->nErr;
2525 walkExprTree(pExpr, analyzeAggregate, pNC);
2526 return pNC->pParse->nErr - nErr;
drh22827922000-06-06 17:27:05 +00002527}
drh5d9a4af2005-08-30 00:54:01 +00002528
2529/*
2530** Call sqlite3ExprAnalyzeAggregates() for every expression in an
2531** expression list. Return the number of errors.
2532**
2533** If an error is found, the analysis is cut short.
2534*/
2535int sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){
2536 struct ExprList_item *pItem;
2537 int i;
2538 int nErr = 0;
2539 if( pList ){
2540 for(pItem=pList->a, i=0; nErr==0 && i<pList->nExpr; i++, pItem++){
2541 nErr += sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
2542 }
2543 }
2544 return nErr;
2545}