blob: 5281c6a0e1ce6c5918441ed12a036950534a701f [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**
danielk1977880c15b2007-09-01 18:24:55 +000015** $Id: expr.c,v 1.312 2007/09/01 18:24:55 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 ){
drh7e09fe02007-06-20 16:13:23 +000074 int op;
danielk19777cedc8d2004-06-10 10:50:08 +000075 pColl = pExpr->pColl;
drh7e09fe02007-06-20 16:13:23 +000076 op = pExpr->op;
77 if( (op==TK_CAST || op==TK_UPLUS) && !pColl ){
danielk19777cedc8d2004-06-10 10:50:08 +000078 return sqlite3ExprCollSeq(pParse, pExpr->pLeft);
danielk19770202b292004-06-09 09:55:16 +000079 }
80 }
danielk19777cedc8d2004-06-10 10:50:08 +000081 if( sqlite3CheckCollSeq(pParse, pColl) ){
82 pColl = 0;
83 }
84 return pColl;
danielk19770202b292004-06-09 09:55:16 +000085}
86
87/*
drh626a8792005-01-17 22:08:19 +000088** pExpr is an operand of a comparison operator. aff2 is the
89** type affinity of the other operand. This routine returns the
drh53db1452004-05-20 13:54:53 +000090** type affinity that should be used for the comparison operator.
91*/
danielk1977e014a832004-05-17 10:48:57 +000092char sqlite3CompareAffinity(Expr *pExpr, char aff2){
danielk1977bf3b7212004-05-18 10:06:24 +000093 char aff1 = sqlite3ExprAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +000094 if( aff1 && aff2 ){
drh8df447f2005-11-01 15:48:24 +000095 /* Both sides of the comparison are columns. If one has numeric
96 ** affinity, use that. Otherwise use no affinity.
danielk1977e014a832004-05-17 10:48:57 +000097 */
drh8a512562005-11-14 22:29:05 +000098 if( sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2) ){
danielk1977e014a832004-05-17 10:48:57 +000099 return SQLITE_AFF_NUMERIC;
100 }else{
101 return SQLITE_AFF_NONE;
102 }
103 }else if( !aff1 && !aff2 ){
drh5f6a87b2004-07-19 00:39:45 +0000104 /* Neither side of the comparison is a column. Compare the
105 ** results directly.
danielk1977e014a832004-05-17 10:48:57 +0000106 */
drh5f6a87b2004-07-19 00:39:45 +0000107 return SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +0000108 }else{
109 /* One side is a column, the other is not. Use the columns affinity. */
drhfe05af82005-07-21 03:14:59 +0000110 assert( aff1==0 || aff2==0 );
danielk1977e014a832004-05-17 10:48:57 +0000111 return (aff1 + aff2);
112 }
113}
114
drh53db1452004-05-20 13:54:53 +0000115/*
116** pExpr is a comparison operator. Return the type affinity that should
117** be applied to both operands prior to doing the comparison.
118*/
danielk1977e014a832004-05-17 10:48:57 +0000119static char comparisonAffinity(Expr *pExpr){
120 char aff;
121 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
122 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
123 pExpr->op==TK_NE );
124 assert( pExpr->pLeft );
danielk1977bf3b7212004-05-18 10:06:24 +0000125 aff = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +0000126 if( pExpr->pRight ){
127 aff = sqlite3CompareAffinity(pExpr->pRight, aff);
128 }
129 else if( pExpr->pSelect ){
130 aff = sqlite3CompareAffinity(pExpr->pSelect->pEList->a[0].pExpr, aff);
131 }
132 else if( !aff ){
drhde087bd2007-02-23 03:00:44 +0000133 aff = SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +0000134 }
135 return aff;
136}
137
138/*
139** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
140** idx_affinity is the affinity of an indexed column. Return true
141** if the index with affinity idx_affinity may be used to implement
142** the comparison in pExpr.
143*/
144int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){
145 char aff = comparisonAffinity(pExpr);
drh8a512562005-11-14 22:29:05 +0000146 switch( aff ){
147 case SQLITE_AFF_NONE:
148 return 1;
149 case SQLITE_AFF_TEXT:
150 return idx_affinity==SQLITE_AFF_TEXT;
151 default:
152 return sqlite3IsNumericAffinity(idx_affinity);
153 }
danielk1977e014a832004-05-17 10:48:57 +0000154}
155
danielk1977a37cdde2004-05-16 11:15:36 +0000156/*
157** Return the P1 value that should be used for a binary comparison
158** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
159** If jumpIfNull is true, then set the low byte of the returned
160** P1 value to tell the opcode to jump if either expression
161** evaluates to NULL.
162*/
danielk1977e014a832004-05-17 10:48:57 +0000163static int binaryCompareP1(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){
danielk1977bf3b7212004-05-18 10:06:24 +0000164 char aff = sqlite3ExprAffinity(pExpr2);
drhf0863fe2005-06-12 21:35:51 +0000165 return ((int)sqlite3CompareAffinity(pExpr1, aff))+(jumpIfNull?0x100:0);
danielk1977a37cdde2004-05-16 11:15:36 +0000166}
167
drha2e00042002-01-22 03:13:42 +0000168/*
danielk19770202b292004-06-09 09:55:16 +0000169** Return a pointer to the collation sequence that should be used by
170** a binary comparison operator comparing pLeft and pRight.
171**
172** If the left hand expression has a collating sequence type, then it is
173** used. Otherwise the collation sequence for the right hand expression
174** is used, or the default (BINARY) if neither expression has a collating
175** type.
danielk1977bcbb04e2007-05-29 12:11:29 +0000176**
177** Argument pRight (but not pLeft) may be a null pointer. In this case,
178** it is not considered.
danielk19770202b292004-06-09 09:55:16 +0000179*/
drh0a0e1312007-08-07 17:04:59 +0000180CollSeq *sqlite3BinaryCompareCollSeq(
danielk1977bcbb04e2007-05-29 12:11:29 +0000181 Parse *pParse,
182 Expr *pLeft,
183 Expr *pRight
184){
drhec41dda2007-02-07 13:09:45 +0000185 CollSeq *pColl;
186 assert( pLeft );
drhec41dda2007-02-07 13:09:45 +0000187 if( pLeft->flags & EP_ExpCollate ){
188 assert( pLeft->pColl );
189 pColl = pLeft->pColl;
danielk1977bcbb04e2007-05-29 12:11:29 +0000190 }else if( pRight && pRight->flags & EP_ExpCollate ){
drhec41dda2007-02-07 13:09:45 +0000191 assert( pRight->pColl );
192 pColl = pRight->pColl;
193 }else{
194 pColl = sqlite3ExprCollSeq(pParse, pLeft);
195 if( !pColl ){
196 pColl = sqlite3ExprCollSeq(pParse, pRight);
197 }
danielk19770202b292004-06-09 09:55:16 +0000198 }
199 return pColl;
200}
201
202/*
drhbe5c89a2004-07-26 00:31:09 +0000203** Generate code for a comparison operator.
204*/
205static int codeCompare(
206 Parse *pParse, /* The parsing (and code generating) context */
207 Expr *pLeft, /* The left operand */
208 Expr *pRight, /* The right operand */
209 int opcode, /* The comparison opcode */
210 int dest, /* Jump here if true. */
211 int jumpIfNull /* If true, jump if either operand is NULL */
212){
213 int p1 = binaryCompareP1(pLeft, pRight, jumpIfNull);
danielk1977bcbb04e2007-05-29 12:11:29 +0000214 CollSeq *p3 = sqlite3BinaryCompareCollSeq(pParse, pLeft, pRight);
drh94a11212004-09-25 13:12:14 +0000215 return sqlite3VdbeOp3(pParse->pVdbe, opcode, p1, dest, (void*)p3, P3_COLLSEQ);
drhbe5c89a2004-07-26 00:31:09 +0000216}
217
218/*
drha76b5df2002-02-23 02:32:10 +0000219** Construct a new expression node and return a pointer to it. Memory
drh17435752007-08-16 04:30:38 +0000220** for this node is obtained from sqlite3_malloc(). The calling function
drha76b5df2002-02-23 02:32:10 +0000221** is responsible for making sure the node eventually gets freed.
222*/
drh17435752007-08-16 04:30:38 +0000223Expr *sqlite3Expr(
danielk1977a1644fd2007-08-29 12:31:25 +0000224 sqlite3 *db, /* Handle for sqlite3DbMallocZero() (may be null) */
drh17435752007-08-16 04:30:38 +0000225 int op, /* Expression opcode */
226 Expr *pLeft, /* Left operand */
227 Expr *pRight, /* Right operand */
228 const Token *pToken /* Argument token */
229){
drha76b5df2002-02-23 02:32:10 +0000230 Expr *pNew;
danielk1977a1644fd2007-08-29 12:31:25 +0000231 pNew = sqlite3DbMallocZero(db, sizeof(Expr));
drha76b5df2002-02-23 02:32:10 +0000232 if( pNew==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000233 /* When malloc fails, delete pLeft and pRight. Expressions passed to
234 ** this function must always be allocated with sqlite3Expr() for this
235 ** reason.
236 */
237 sqlite3ExprDelete(pLeft);
238 sqlite3ExprDelete(pRight);
drha76b5df2002-02-23 02:32:10 +0000239 return 0;
240 }
241 pNew->op = op;
242 pNew->pLeft = pLeft;
243 pNew->pRight = pRight;
danielk1977a58fdfb2005-02-08 07:50:40 +0000244 pNew->iAgg = -1;
drha76b5df2002-02-23 02:32:10 +0000245 if( pToken ){
drh4b59ab52002-08-24 18:24:51 +0000246 assert( pToken->dyn==0 );
drh145716b2004-09-24 12:24:06 +0000247 pNew->span = pNew->token = *pToken;
drha34001c2007-02-02 12:44:37 +0000248 }else if( pLeft ){
249 if( pRight ){
250 sqlite3ExprSpan(pNew, &pLeft->span, &pRight->span);
drh5ffb3ac2007-04-18 17:07:57 +0000251 if( pRight->flags & EP_ExpCollate ){
drha34001c2007-02-02 12:44:37 +0000252 pNew->flags |= EP_ExpCollate;
253 pNew->pColl = pRight->pColl;
254 }
255 }
drh5ffb3ac2007-04-18 17:07:57 +0000256 if( pLeft->flags & EP_ExpCollate ){
drha34001c2007-02-02 12:44:37 +0000257 pNew->flags |= EP_ExpCollate;
258 pNew->pColl = pLeft->pColl;
259 }
drha76b5df2002-02-23 02:32:10 +0000260 }
danielk1977fc976062007-05-10 10:46:56 +0000261
262 sqlite3ExprSetHeight(pNew);
drha76b5df2002-02-23 02:32:10 +0000263 return pNew;
264}
265
266/*
drh17435752007-08-16 04:30:38 +0000267** Works like sqlite3Expr() except that it takes an extra Parse*
268** argument and notifies the associated connection object if malloc fails.
drh206f3d92006-07-11 13:15:08 +0000269*/
drh17435752007-08-16 04:30:38 +0000270Expr *sqlite3PExpr(
271 Parse *pParse, /* Parsing context */
272 int op, /* Expression opcode */
273 Expr *pLeft, /* Left operand */
274 Expr *pRight, /* Right operand */
275 const Token *pToken /* Argument token */
276){
danielk1977a1644fd2007-08-29 12:31:25 +0000277 return sqlite3Expr(pParse->db, op, pLeft, pRight, pToken);
drh206f3d92006-07-11 13:15:08 +0000278}
279
280/*
drh4e0cff62004-11-05 05:10:28 +0000281** When doing a nested parse, you can include terms in an expression
282** that look like this: #0 #1 #2 ... These terms refer to elements
drh288d37f2005-06-22 08:48:06 +0000283** on the stack. "#0" means the top of the stack.
284** "#1" means the next down on the stack. And so forth.
drh4e0cff62004-11-05 05:10:28 +0000285**
286** This routine is called by the parser to deal with on of those terms.
287** It immediately generates code to store the value in a memory location.
288** The returns an expression that will code to extract the value from
289** that memory location as needed.
290*/
291Expr *sqlite3RegisterExpr(Parse *pParse, Token *pToken){
292 Vdbe *v = pParse->pVdbe;
293 Expr *p;
294 int depth;
drh4e0cff62004-11-05 05:10:28 +0000295 if( pParse->nested==0 ){
296 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", pToken);
danielk1977a1644fd2007-08-29 12:31:25 +0000297 return sqlite3PExpr(pParse, TK_NULL, 0, 0, 0);
drh4e0cff62004-11-05 05:10:28 +0000298 }
drhbb7ac002005-08-12 22:58:53 +0000299 if( v==0 ) return 0;
danielk1977a1644fd2007-08-29 12:31:25 +0000300 p = sqlite3PExpr(pParse, TK_REGISTER, 0, 0, pToken);
drh73c42a12004-11-20 18:13:10 +0000301 if( p==0 ){
302 return 0; /* Malloc failed */
303 }
drh2646da72005-12-09 20:02:05 +0000304 depth = atoi((char*)&pToken->z[1]);
drh288d37f2005-06-22 08:48:06 +0000305 p->iTable = pParse->nMem++;
306 sqlite3VdbeAddOp(v, OP_Dup, depth, 0);
307 sqlite3VdbeAddOp(v, OP_MemStore, p->iTable, 1);
drh4e0cff62004-11-05 05:10:28 +0000308 return p;
309}
310
311/*
drh91bb0ee2004-09-01 03:06:34 +0000312** Join two expressions using an AND operator. If either expression is
313** NULL, then just return the other expression.
314*/
danielk19771e536952007-08-16 10:09:01 +0000315Expr *sqlite3ExprAnd(sqlite3 *db, Expr *pLeft, Expr *pRight){
drh91bb0ee2004-09-01 03:06:34 +0000316 if( pLeft==0 ){
317 return pRight;
318 }else if( pRight==0 ){
319 return pLeft;
320 }else{
danielk1977880c15b2007-09-01 18:24:55 +0000321 return sqlite3Expr(db, TK_AND, pLeft, pRight, 0);
drh91bb0ee2004-09-01 03:06:34 +0000322 }
323}
324
325/*
drh6977fea2002-10-22 23:38:04 +0000326** Set the Expr.span field of the given expression to span all
drha76b5df2002-02-23 02:32:10 +0000327** text between the two given tokens.
328*/
danielk19774adee202004-05-08 08:23:19 +0000329void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
drh4efc4752004-01-16 15:55:37 +0000330 assert( pRight!=0 );
331 assert( pLeft!=0 );
drhf3a65f72007-08-22 20:18:21 +0000332 if( pExpr && pRight->z && pLeft->z ){
drhad6d9462004-09-19 02:15:24 +0000333 assert( pLeft->dyn==0 || pLeft->z[pLeft->n]==0 );
drh145716b2004-09-24 12:24:06 +0000334 if( pLeft->dyn==0 && pRight->dyn==0 ){
drh6977fea2002-10-22 23:38:04 +0000335 pExpr->span.z = pLeft->z;
drh97903fe2005-05-24 20:19:57 +0000336 pExpr->span.n = pRight->n + (pRight->z - pLeft->z);
drh4b59ab52002-08-24 18:24:51 +0000337 }else{
drh6977fea2002-10-22 23:38:04 +0000338 pExpr->span.z = 0;
drh4b59ab52002-08-24 18:24:51 +0000339 }
drha76b5df2002-02-23 02:32:10 +0000340 }
341}
342
343/*
344** Construct a new expression node for a function with multiple
345** arguments.
346*/
drh17435752007-08-16 04:30:38 +0000347Expr *sqlite3ExprFunction(Parse *pParse, ExprList *pList, Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000348 Expr *pNew;
danielk19774b202ae2006-01-23 05:50:58 +0000349 assert( pToken );
drh17435752007-08-16 04:30:38 +0000350 pNew = sqlite3DbMallocZero(pParse->db, sizeof(Expr) );
drha76b5df2002-02-23 02:32:10 +0000351 if( pNew==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000352 sqlite3ExprListDelete(pList); /* Avoid leaking memory when malloc fails */
drha76b5df2002-02-23 02:32:10 +0000353 return 0;
354 }
355 pNew->op = TK_FUNCTION;
356 pNew->pList = pList;
danielk19774b202ae2006-01-23 05:50:58 +0000357 assert( pToken->dyn==0 );
358 pNew->token = *pToken;
drh6977fea2002-10-22 23:38:04 +0000359 pNew->span = pNew->token;
danielk1977fc976062007-05-10 10:46:56 +0000360
361 sqlite3ExprSetHeight(pNew);
drha76b5df2002-02-23 02:32:10 +0000362 return pNew;
363}
364
365/*
drhfa6bc002004-09-07 16:19:52 +0000366** Assign a variable number to an expression that encodes a wildcard
367** in the original SQL statement.
368**
369** Wildcards consisting of a single "?" are assigned the next sequential
370** variable number.
371**
372** Wildcards of the form "?nnn" are assigned the number "nnn". We make
373** sure "nnn" is not too be to avoid a denial of service attack when
374** the SQL statement comes from an external source.
375**
376** Wildcards of the form ":aaa" or "$aaa" are assigned the same number
377** as the previous instance of the same wildcard. Or if this is the first
378** instance of the wildcard, the next sequenial variable number is
379** assigned.
380*/
381void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){
382 Token *pToken;
drh17435752007-08-16 04:30:38 +0000383 sqlite3 *db = pParse->db;
384
drhfa6bc002004-09-07 16:19:52 +0000385 if( pExpr==0 ) return;
386 pToken = &pExpr->token;
387 assert( pToken->n>=1 );
388 assert( pToken->z!=0 );
389 assert( pToken->z[0]!=0 );
390 if( pToken->n==1 ){
391 /* Wildcard of the form "?". Assign the next variable number */
392 pExpr->iTable = ++pParse->nVar;
393 }else if( pToken->z[0]=='?' ){
394 /* Wildcard of the form "?nnn". Convert "nnn" to an integer and
395 ** use it as the variable number */
396 int i;
drh2646da72005-12-09 20:02:05 +0000397 pExpr->iTable = i = atoi((char*)&pToken->z[1]);
drhfa6bc002004-09-07 16:19:52 +0000398 if( i<1 || i>SQLITE_MAX_VARIABLE_NUMBER ){
399 sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
400 SQLITE_MAX_VARIABLE_NUMBER);
401 }
402 if( i>pParse->nVar ){
403 pParse->nVar = i;
404 }
405 }else{
406 /* Wildcards of the form ":aaa" or "$aaa". Reuse the same variable
407 ** number as the prior appearance of the same name, or if the name
408 ** has never appeared before, reuse the same variable number
409 */
410 int i, n;
411 n = pToken->n;
412 for(i=0; i<pParse->nVarExpr; i++){
413 Expr *pE;
414 if( (pE = pParse->apVarExpr[i])!=0
415 && pE->token.n==n
416 && memcmp(pE->token.z, pToken->z, n)==0 ){
417 pExpr->iTable = pE->iTable;
418 break;
419 }
420 }
421 if( i>=pParse->nVarExpr ){
422 pExpr->iTable = ++pParse->nVar;
423 if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){
424 pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10;
drh17435752007-08-16 04:30:38 +0000425 pParse->apVarExpr =
426 sqlite3DbReallocOrFree(
427 db,
428 pParse->apVarExpr,
429 pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0])
430 );
drhfa6bc002004-09-07 16:19:52 +0000431 }
drh17435752007-08-16 04:30:38 +0000432 if( !db->mallocFailed ){
drhfa6bc002004-09-07 16:19:52 +0000433 assert( pParse->apVarExpr!=0 );
434 pParse->apVarExpr[pParse->nVarExpr++] = pExpr;
435 }
436 }
437 }
danielk1977832b2662007-05-09 11:37:22 +0000438 if( !pParse->nErr && pParse->nVar>SQLITE_MAX_VARIABLE_NUMBER ){
439 sqlite3ErrorMsg(pParse, "too many SQL variables");
440 }
drhfa6bc002004-09-07 16:19:52 +0000441}
442
443/*
drha2e00042002-01-22 03:13:42 +0000444** Recursively delete an expression tree.
445*/
danielk19774adee202004-05-08 08:23:19 +0000446void sqlite3ExprDelete(Expr *p){
drha2e00042002-01-22 03:13:42 +0000447 if( p==0 ) return;
drh17435752007-08-16 04:30:38 +0000448 if( p->span.dyn ) sqlite3_free((char*)p->span.z);
449 if( p->token.dyn ) sqlite3_free((char*)p->token.z);
danielk19774adee202004-05-08 08:23:19 +0000450 sqlite3ExprDelete(p->pLeft);
451 sqlite3ExprDelete(p->pRight);
452 sqlite3ExprListDelete(p->pList);
453 sqlite3SelectDelete(p->pSelect);
drh17435752007-08-16 04:30:38 +0000454 sqlite3_free(p);
drha2e00042002-01-22 03:13:42 +0000455}
456
drhd2687b72005-08-12 22:56:09 +0000457/*
458** The Expr.token field might be a string literal that is quoted.
459** If so, remove the quotation marks.
460*/
drh17435752007-08-16 04:30:38 +0000461void sqlite3DequoteExpr(sqlite3 *db, Expr *p){
drhd2687b72005-08-12 22:56:09 +0000462 if( ExprHasAnyProperty(p, EP_Dequoted) ){
463 return;
464 }
465 ExprSetProperty(p, EP_Dequoted);
466 if( p->token.dyn==0 ){
drh17435752007-08-16 04:30:38 +0000467 sqlite3TokenCopy(db, &p->token, &p->token);
drhd2687b72005-08-12 22:56:09 +0000468 }
469 sqlite3Dequote((char*)p->token.z);
470}
471
drha76b5df2002-02-23 02:32:10 +0000472
473/*
drhff78bd22002-02-27 01:47:11 +0000474** The following group of routines make deep copies of expressions,
475** expression lists, ID lists, and select statements. The copies can
476** be deleted (by being passed to their respective ...Delete() routines)
477** without effecting the originals.
478**
danielk19774adee202004-05-08 08:23:19 +0000479** The expression list, ID, and source lists return by sqlite3ExprListDup(),
480** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
drhad3cab52002-05-24 02:04:32 +0000481** by subsequent calls to sqlite*ListAppend() routines.
drhff78bd22002-02-27 01:47:11 +0000482**
drhad3cab52002-05-24 02:04:32 +0000483** Any tables that the SrcList might point to are not duplicated.
drhff78bd22002-02-27 01:47:11 +0000484*/
danielk19771e536952007-08-16 10:09:01 +0000485Expr *sqlite3ExprDup(sqlite3 *db, Expr *p){
drhff78bd22002-02-27 01:47:11 +0000486 Expr *pNew;
487 if( p==0 ) return 0;
drh17435752007-08-16 04:30:38 +0000488 pNew = sqlite3DbMallocRaw(db, sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000489 if( pNew==0 ) return 0;
drh3b167c72002-06-28 12:18:47 +0000490 memcpy(pNew, p, sizeof(*pNew));
drh6977fea2002-10-22 23:38:04 +0000491 if( p->token.z!=0 ){
drh17435752007-08-16 04:30:38 +0000492 pNew->token.z = (u8*)sqlite3DbStrNDup(db, (char*)p->token.z, p->token.n);
drh4b59ab52002-08-24 18:24:51 +0000493 pNew->token.dyn = 1;
494 }else{
drh4efc4752004-01-16 15:55:37 +0000495 assert( pNew->token.z==0 );
drh4b59ab52002-08-24 18:24:51 +0000496 }
drh6977fea2002-10-22 23:38:04 +0000497 pNew->span.z = 0;
drh17435752007-08-16 04:30:38 +0000498 pNew->pLeft = sqlite3ExprDup(db, p->pLeft);
499 pNew->pRight = sqlite3ExprDup(db, p->pRight);
500 pNew->pList = sqlite3ExprListDup(db, p->pList);
501 pNew->pSelect = sqlite3SelectDup(db, p->pSelect);
drhff78bd22002-02-27 01:47:11 +0000502 return pNew;
503}
drh17435752007-08-16 04:30:38 +0000504void sqlite3TokenCopy(sqlite3 *db, Token *pTo, Token *pFrom){
505 if( pTo->dyn ) sqlite3_free((char*)pTo->z);
drh4b59ab52002-08-24 18:24:51 +0000506 if( pFrom->z ){
507 pTo->n = pFrom->n;
drh17435752007-08-16 04:30:38 +0000508 pTo->z = (u8*)sqlite3DbStrNDup(db, (char*)pFrom->z, pFrom->n);
drh4b59ab52002-08-24 18:24:51 +0000509 pTo->dyn = 1;
510 }else{
drh4b59ab52002-08-24 18:24:51 +0000511 pTo->z = 0;
drh4b59ab52002-08-24 18:24:51 +0000512 }
513}
drh17435752007-08-16 04:30:38 +0000514ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p){
drhff78bd22002-02-27 01:47:11 +0000515 ExprList *pNew;
drh145716b2004-09-24 12:24:06 +0000516 struct ExprList_item *pItem, *pOldItem;
drhff78bd22002-02-27 01:47:11 +0000517 int i;
518 if( p==0 ) return 0;
drh17435752007-08-16 04:30:38 +0000519 pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +0000520 if( pNew==0 ) return 0;
danielk197731dad9d2007-08-16 11:36:15 +0000521 pNew->iECursor = 0;
drh4305d102003-07-30 12:34:12 +0000522 pNew->nExpr = pNew->nAlloc = p->nExpr;
drh17435752007-08-16 04:30:38 +0000523 pNew->a = pItem = sqlite3DbMallocRaw(db, p->nExpr*sizeof(p->a[0]) );
danielk1977e0048402004-06-15 16:51:01 +0000524 if( pItem==0 ){
drh17435752007-08-16 04:30:38 +0000525 sqlite3_free(pNew);
danielk1977e0048402004-06-15 16:51:01 +0000526 return 0;
527 }
drh145716b2004-09-24 12:24:06 +0000528 pOldItem = p->a;
529 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
drh4b59ab52002-08-24 18:24:51 +0000530 Expr *pNewExpr, *pOldExpr;
drh17435752007-08-16 04:30:38 +0000531 pItem->pExpr = pNewExpr = sqlite3ExprDup(db, pOldExpr = pOldItem->pExpr);
drh6977fea2002-10-22 23:38:04 +0000532 if( pOldExpr->span.z!=0 && pNewExpr ){
533 /* Always make a copy of the span for top-level expressions in the
drh4b59ab52002-08-24 18:24:51 +0000534 ** expression list. The logic in SELECT processing that determines
535 ** the names of columns in the result set needs this information */
drh17435752007-08-16 04:30:38 +0000536 sqlite3TokenCopy(db, &pNewExpr->span, &pOldExpr->span);
drh4b59ab52002-08-24 18:24:51 +0000537 }
drh1f3e9052002-10-31 00:09:39 +0000538 assert( pNewExpr==0 || pNewExpr->span.z!=0
drh6f7adc82006-01-11 21:41:20 +0000539 || pOldExpr->span.z==0
drh17435752007-08-16 04:30:38 +0000540 || db->mallocFailed );
541 pItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
drh145716b2004-09-24 12:24:06 +0000542 pItem->sortOrder = pOldItem->sortOrder;
543 pItem->isAgg = pOldItem->isAgg;
drh3e7bc9c2004-02-21 19:17:17 +0000544 pItem->done = 0;
drhff78bd22002-02-27 01:47:11 +0000545 }
546 return pNew;
547}
danielk197793758c82005-01-21 08:13:14 +0000548
549/*
550** If cursors, triggers, views and subqueries are all omitted from
551** the build, then none of the following routines, except for
552** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes
553** called with a NULL argument.
554*/
danielk19776a67fe82005-02-04 04:07:16 +0000555#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \
556 || !defined(SQLITE_OMIT_SUBQUERY)
drh17435752007-08-16 04:30:38 +0000557SrcList *sqlite3SrcListDup(sqlite3 *db, SrcList *p){
drhad3cab52002-05-24 02:04:32 +0000558 SrcList *pNew;
559 int i;
drh113088e2003-03-20 01:16:58 +0000560 int nByte;
drhad3cab52002-05-24 02:04:32 +0000561 if( p==0 ) return 0;
drh113088e2003-03-20 01:16:58 +0000562 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
drh17435752007-08-16 04:30:38 +0000563 pNew = sqlite3DbMallocRaw(db, nByte );
drhad3cab52002-05-24 02:04:32 +0000564 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000565 pNew->nSrc = pNew->nAlloc = p->nSrc;
drhad3cab52002-05-24 02:04:32 +0000566 for(i=0; i<p->nSrc; i++){
drh4efc4752004-01-16 15:55:37 +0000567 struct SrcList_item *pNewItem = &pNew->a[i];
568 struct SrcList_item *pOldItem = &p->a[i];
drhed8a3bb2005-06-06 21:19:56 +0000569 Table *pTab;
drh17435752007-08-16 04:30:38 +0000570 pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase);
571 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
572 pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias);
drh4efc4752004-01-16 15:55:37 +0000573 pNewItem->jointype = pOldItem->jointype;
574 pNewItem->iCursor = pOldItem->iCursor;
danielk19771787cca2006-02-10 07:07:14 +0000575 pNewItem->isPopulated = pOldItem->isPopulated;
drhed8a3bb2005-06-06 21:19:56 +0000576 pTab = pNewItem->pTab = pOldItem->pTab;
577 if( pTab ){
578 pTab->nRef++;
danielk1977a1cb1832005-02-12 08:59:55 +0000579 }
drh17435752007-08-16 04:30:38 +0000580 pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect);
581 pNewItem->pOn = sqlite3ExprDup(db, pOldItem->pOn);
582 pNewItem->pUsing = sqlite3IdListDup(db, pOldItem->pUsing);
danielk19776c18b6e2005-01-30 09:17:58 +0000583 pNewItem->colUsed = pOldItem->colUsed;
drhad3cab52002-05-24 02:04:32 +0000584 }
585 return pNew;
586}
drh17435752007-08-16 04:30:38 +0000587IdList *sqlite3IdListDup(sqlite3 *db, IdList *p){
drhff78bd22002-02-27 01:47:11 +0000588 IdList *pNew;
589 int i;
590 if( p==0 ) return 0;
drh17435752007-08-16 04:30:38 +0000591 pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +0000592 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000593 pNew->nId = pNew->nAlloc = p->nId;
drh17435752007-08-16 04:30:38 +0000594 pNew->a = sqlite3DbMallocRaw(db, p->nId*sizeof(p->a[0]) );
danielk1977d5d56522005-03-16 12:15:20 +0000595 if( pNew->a==0 ){
drh17435752007-08-16 04:30:38 +0000596 sqlite3_free(pNew);
danielk1977d5d56522005-03-16 12:15:20 +0000597 return 0;
598 }
drhff78bd22002-02-27 01:47:11 +0000599 for(i=0; i<p->nId; i++){
drh4efc4752004-01-16 15:55:37 +0000600 struct IdList_item *pNewItem = &pNew->a[i];
601 struct IdList_item *pOldItem = &p->a[i];
drh17435752007-08-16 04:30:38 +0000602 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
drh4efc4752004-01-16 15:55:37 +0000603 pNewItem->idx = pOldItem->idx;
drhff78bd22002-02-27 01:47:11 +0000604 }
605 return pNew;
606}
drh17435752007-08-16 04:30:38 +0000607Select *sqlite3SelectDup(sqlite3 *db, Select *p){
drhff78bd22002-02-27 01:47:11 +0000608 Select *pNew;
609 if( p==0 ) return 0;
drh17435752007-08-16 04:30:38 +0000610 pNew = sqlite3DbMallocRaw(db, sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000611 if( pNew==0 ) return 0;
612 pNew->isDistinct = p->isDistinct;
drh17435752007-08-16 04:30:38 +0000613 pNew->pEList = sqlite3ExprListDup(db, p->pEList);
614 pNew->pSrc = sqlite3SrcListDup(db, p->pSrc);
615 pNew->pWhere = sqlite3ExprDup(db, p->pWhere);
616 pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy);
617 pNew->pHaving = sqlite3ExprDup(db, p->pHaving);
618 pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy);
drhff78bd22002-02-27 01:47:11 +0000619 pNew->op = p->op;
drh17435752007-08-16 04:30:38 +0000620 pNew->pPrior = sqlite3SelectDup(db, p->pPrior);
621 pNew->pLimit = sqlite3ExprDup(db, p->pLimit);
622 pNew->pOffset = sqlite3ExprDup(db, p->pOffset);
drh7b58dae2003-07-20 01:16:46 +0000623 pNew->iLimit = -1;
624 pNew->iOffset = -1;
danielk1977a1cb1832005-02-12 08:59:55 +0000625 pNew->isResolved = p->isResolved;
626 pNew->isAgg = p->isAgg;
drhb9bb7c12006-06-11 23:41:55 +0000627 pNew->usesEphm = 0;
drh8e647b82005-09-23 21:11:53 +0000628 pNew->disallowOrderBy = 0;
drh0342b1f2005-09-01 03:07:44 +0000629 pNew->pRightmost = 0;
drhb9bb7c12006-06-11 23:41:55 +0000630 pNew->addrOpenEphm[0] = -1;
631 pNew->addrOpenEphm[1] = -1;
632 pNew->addrOpenEphm[2] = -1;
drhff78bd22002-02-27 01:47:11 +0000633 return pNew;
634}
danielk197793758c82005-01-21 08:13:14 +0000635#else
drh17435752007-08-16 04:30:38 +0000636Select *sqlite3SelectDup(sqlite3 *db, Select *p){
danielk197793758c82005-01-21 08:13:14 +0000637 assert( p==0 );
638 return 0;
639}
640#endif
drhff78bd22002-02-27 01:47:11 +0000641
642
643/*
drha76b5df2002-02-23 02:32:10 +0000644** Add a new element to the end of an expression list. If pList is
645** initially NULL, then create a new expression list.
646*/
drh17435752007-08-16 04:30:38 +0000647ExprList *sqlite3ExprListAppend(
648 Parse *pParse, /* Parsing context */
649 ExprList *pList, /* List to which to append. Might be NULL */
650 Expr *pExpr, /* Expression to be appended */
651 Token *pName /* AS keyword for the expression */
652){
653 sqlite3 *db = pParse->db;
drha76b5df2002-02-23 02:32:10 +0000654 if( pList==0 ){
drh17435752007-08-16 04:30:38 +0000655 pList = sqlite3DbMallocZero(db, sizeof(ExprList) );
drha76b5df2002-02-23 02:32:10 +0000656 if( pList==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000657 goto no_mem;
drha76b5df2002-02-23 02:32:10 +0000658 }
drh4efc4752004-01-16 15:55:37 +0000659 assert( pList->nAlloc==0 );
drha76b5df2002-02-23 02:32:10 +0000660 }
drh4305d102003-07-30 12:34:12 +0000661 if( pList->nAlloc<=pList->nExpr ){
danielk1977d5d56522005-03-16 12:15:20 +0000662 struct ExprList_item *a;
663 int n = pList->nAlloc*2 + 4;
danielk197726783a52007-08-29 14:06:22 +0000664 a = sqlite3DbRealloc(db, pList->a, n*sizeof(pList->a[0]));
danielk1977d5d56522005-03-16 12:15:20 +0000665 if( a==0 ){
666 goto no_mem;
drha76b5df2002-02-23 02:32:10 +0000667 }
danielk1977d5d56522005-03-16 12:15:20 +0000668 pList->a = a;
669 pList->nAlloc = n;
drha76b5df2002-02-23 02:32:10 +0000670 }
drh4efc4752004-01-16 15:55:37 +0000671 assert( pList->a!=0 );
672 if( pExpr || pName ){
673 struct ExprList_item *pItem = &pList->a[pList->nExpr++];
674 memset(pItem, 0, sizeof(*pItem));
drh17435752007-08-16 04:30:38 +0000675 pItem->zName = sqlite3NameFromToken(db, pName);
danielk1977e94ddc92005-03-21 03:53:38 +0000676 pItem->pExpr = pExpr;
drha76b5df2002-02-23 02:32:10 +0000677 }
678 return pList;
danielk1977d5d56522005-03-16 12:15:20 +0000679
680no_mem:
681 /* Avoid leaking memory if malloc has failed. */
682 sqlite3ExprDelete(pExpr);
683 sqlite3ExprListDelete(pList);
684 return 0;
drha76b5df2002-02-23 02:32:10 +0000685}
686
687/*
danielk19777a15a4b2007-05-08 17:54:43 +0000688** If the expression list pEList contains more than iLimit elements,
689** leave an error message in pParse.
690*/
691void sqlite3ExprListCheckLength(
692 Parse *pParse,
693 ExprList *pEList,
694 int iLimit,
695 const char *zObject
696){
danielk1977b4fc6792007-05-08 18:04:46 +0000697 if( pEList && pEList->nExpr>iLimit ){
danielk19777a15a4b2007-05-08 17:54:43 +0000698 sqlite3ErrorMsg(pParse, "too many columns in %s", zObject);
699 }
700}
701
danielk1977fc976062007-05-10 10:46:56 +0000702
danielk1977e6a58a42007-08-31 17:42:48 +0000703#if defined(SQLITE_TEST) || SQLITE_MAX_EXPR_DEPTH>0
danielk1977fc976062007-05-10 10:46:56 +0000704/* The following three functions, heightOfExpr(), heightOfExprList()
705** and heightOfSelect(), are used to determine the maximum height
706** of any expression tree referenced by the structure passed as the
707** first argument.
708**
709** If this maximum height is greater than the current value pointed
710** to by pnHeight, the second parameter, then set *pnHeight to that
711** value.
712*/
713static void heightOfExpr(Expr *p, int *pnHeight){
714 if( p ){
715 if( p->nHeight>*pnHeight ){
716 *pnHeight = p->nHeight;
717 }
718 }
719}
720static void heightOfExprList(ExprList *p, int *pnHeight){
721 if( p ){
722 int i;
723 for(i=0; i<p->nExpr; i++){
724 heightOfExpr(p->a[i].pExpr, pnHeight);
725 }
726 }
727}
728static void heightOfSelect(Select *p, int *pnHeight){
729 if( p ){
730 heightOfExpr(p->pWhere, pnHeight);
731 heightOfExpr(p->pHaving, pnHeight);
732 heightOfExpr(p->pLimit, pnHeight);
733 heightOfExpr(p->pOffset, pnHeight);
734 heightOfExprList(p->pEList, pnHeight);
735 heightOfExprList(p->pGroupBy, pnHeight);
736 heightOfExprList(p->pOrderBy, pnHeight);
737 heightOfSelect(p->pPrior, pnHeight);
738 }
739}
740
741/*
742** Set the Expr.nHeight variable in the structure passed as an
743** argument. An expression with no children, Expr.pList or
744** Expr.pSelect member has a height of 1. Any other expression
745** has a height equal to the maximum height of any other
746** referenced Expr plus one.
747*/
748void sqlite3ExprSetHeight(Expr *p){
749 int nHeight = 0;
750 heightOfExpr(p->pLeft, &nHeight);
751 heightOfExpr(p->pRight, &nHeight);
752 heightOfExprList(p->pList, &nHeight);
753 heightOfSelect(p->pSelect, &nHeight);
754 p->nHeight = nHeight + 1;
755}
756
757/*
758** Return the maximum height of any expression tree referenced
759** by the select statement passed as an argument.
760*/
761int sqlite3SelectExprHeight(Select *p){
762 int nHeight = 0;
763 heightOfSelect(p, &nHeight);
764 return nHeight;
765}
766#endif
767
danielk19777a15a4b2007-05-08 17:54:43 +0000768/*
drha76b5df2002-02-23 02:32:10 +0000769** Delete an entire expression list.
770*/
danielk19774adee202004-05-08 08:23:19 +0000771void sqlite3ExprListDelete(ExprList *pList){
drha76b5df2002-02-23 02:32:10 +0000772 int i;
drhbe5c89a2004-07-26 00:31:09 +0000773 struct ExprList_item *pItem;
drha76b5df2002-02-23 02:32:10 +0000774 if( pList==0 ) return;
drh1bdd9b52004-04-23 17:04:44 +0000775 assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
776 assert( pList->nExpr<=pList->nAlloc );
drhbe5c89a2004-07-26 00:31:09 +0000777 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
778 sqlite3ExprDelete(pItem->pExpr);
drh17435752007-08-16 04:30:38 +0000779 sqlite3_free(pItem->zName);
drha76b5df2002-02-23 02:32:10 +0000780 }
drh17435752007-08-16 04:30:38 +0000781 sqlite3_free(pList->a);
782 sqlite3_free(pList);
drha76b5df2002-02-23 02:32:10 +0000783}
784
785/*
drh626a8792005-01-17 22:08:19 +0000786** Walk an expression tree. Call xFunc for each node visited.
drh73b211a2005-01-18 04:00:42 +0000787**
drh626a8792005-01-17 22:08:19 +0000788** The return value from xFunc determines whether the tree walk continues.
789** 0 means continue walking the tree. 1 means do not walk children
790** of the current node but continue with siblings. 2 means abandon
791** the tree walk completely.
792**
793** The return value from this routine is 1 to abandon the tree walk
794** and 0 to continue.
drh87abf5c2005-08-25 12:45:04 +0000795**
796** NOTICE: This routine does *not* descend into subqueries.
drh626a8792005-01-17 22:08:19 +0000797*/
danielk1977a58fdfb2005-02-08 07:50:40 +0000798static int walkExprList(ExprList *, int (*)(void *, Expr*), void *);
drh626a8792005-01-17 22:08:19 +0000799static int walkExprTree(Expr *pExpr, int (*xFunc)(void*,Expr*), void *pArg){
drh626a8792005-01-17 22:08:19 +0000800 int rc;
801 if( pExpr==0 ) return 0;
802 rc = (*xFunc)(pArg, pExpr);
803 if( rc==0 ){
804 if( walkExprTree(pExpr->pLeft, xFunc, pArg) ) return 1;
805 if( walkExprTree(pExpr->pRight, xFunc, pArg) ) return 1;
danielk1977a58fdfb2005-02-08 07:50:40 +0000806 if( walkExprList(pExpr->pList, xFunc, pArg) ) return 1;
drh626a8792005-01-17 22:08:19 +0000807 }
808 return rc>1;
809}
810
811/*
danielk1977a58fdfb2005-02-08 07:50:40 +0000812** Call walkExprTree() for every expression in list p.
813*/
814static int walkExprList(ExprList *p, int (*xFunc)(void *, Expr*), void *pArg){
815 int i;
816 struct ExprList_item *pItem;
817 if( !p ) return 0;
818 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
819 if( walkExprTree(pItem->pExpr, xFunc, pArg) ) return 1;
820 }
821 return 0;
822}
823
824/*
825** Call walkExprTree() for every expression in Select p, not including
826** expressions that are part of sub-selects in any FROM clause or the LIMIT
827** or OFFSET expressions..
828*/
829static int walkSelectExpr(Select *p, int (*xFunc)(void *, Expr*), void *pArg){
830 walkExprList(p->pEList, xFunc, pArg);
831 walkExprTree(p->pWhere, xFunc, pArg);
832 walkExprList(p->pGroupBy, xFunc, pArg);
833 walkExprTree(p->pHaving, xFunc, pArg);
834 walkExprList(p->pOrderBy, xFunc, pArg);
danielk197715d79822007-05-15 07:00:34 +0000835 if( p->pPrior ){
836 walkSelectExpr(p->pPrior, xFunc, pArg);
837 }
danielk1977a58fdfb2005-02-08 07:50:40 +0000838 return 0;
839}
840
841
842/*
drh626a8792005-01-17 22:08:19 +0000843** This routine is designed as an xFunc for walkExprTree().
844**
845** pArg is really a pointer to an integer. If we can tell by looking
drh73b211a2005-01-18 04:00:42 +0000846** at pExpr that the expression that contains pExpr is not a constant
847** expression, then set *pArg to 0 and return 2 to abandon the tree walk.
848** If pExpr does does not disqualify the expression from being a constant
849** then do nothing.
850**
851** After walking the whole tree, if no nodes are found that disqualify
852** the expression as constant, then we assume the whole expression
853** is constant. See sqlite3ExprIsConstant() for additional information.
drh626a8792005-01-17 22:08:19 +0000854*/
855static int exprNodeIsConstant(void *pArg, Expr *pExpr){
drh0a168372007-06-08 00:20:47 +0000856 int *pN = (int*)pArg;
857
858 /* If *pArg is 3 then any term of the expression that comes from
859 ** the ON or USING clauses of a join disqualifies the expression
860 ** from being considered constant. */
861 if( (*pN)==3 && ExprHasAnyProperty(pExpr, EP_FromJoin) ){
862 *pN = 0;
863 return 2;
864 }
865
drh626a8792005-01-17 22:08:19 +0000866 switch( pExpr->op ){
drheb55bd22005-06-30 17:04:21 +0000867 /* Consider functions to be constant if all their arguments are constant
868 ** and *pArg==2 */
869 case TK_FUNCTION:
drh0a168372007-06-08 00:20:47 +0000870 if( (*pN)==2 ) return 0;
drheb55bd22005-06-30 17:04:21 +0000871 /* Fall through */
drh626a8792005-01-17 22:08:19 +0000872 case TK_ID:
873 case TK_COLUMN:
874 case TK_DOT:
875 case TK_AGG_FUNCTION:
drh13449892005-09-07 21:22:45 +0000876 case TK_AGG_COLUMN:
drhfe2093d2005-01-20 22:48:47 +0000877#ifndef SQLITE_OMIT_SUBQUERY
878 case TK_SELECT:
879 case TK_EXISTS:
880#endif
drh0a168372007-06-08 00:20:47 +0000881 *pN = 0;
drh626a8792005-01-17 22:08:19 +0000882 return 2;
drh87abf5c2005-08-25 12:45:04 +0000883 case TK_IN:
884 if( pExpr->pSelect ){
drh0a168372007-06-08 00:20:47 +0000885 *pN = 0;
drh87abf5c2005-08-25 12:45:04 +0000886 return 2;
887 }
drh626a8792005-01-17 22:08:19 +0000888 default:
889 return 0;
890 }
891}
892
893/*
drhfef52082000-06-06 01:50:43 +0000894** Walk an expression tree. Return 1 if the expression is constant
drheb55bd22005-06-30 17:04:21 +0000895** and 0 if it involves variables or function calls.
drh23989372002-05-21 13:43:04 +0000896**
897** For the purposes of this function, a double-quoted string (ex: "abc")
898** is considered a variable but a single-quoted string (ex: 'abc') is
899** a constant.
drhfef52082000-06-06 01:50:43 +0000900*/
danielk19774adee202004-05-08 08:23:19 +0000901int sqlite3ExprIsConstant(Expr *p){
drh626a8792005-01-17 22:08:19 +0000902 int isConst = 1;
903 walkExprTree(p, exprNodeIsConstant, &isConst);
904 return isConst;
drhfef52082000-06-06 01:50:43 +0000905}
906
907/*
drheb55bd22005-06-30 17:04:21 +0000908** Walk an expression tree. Return 1 if the expression is constant
drh0a168372007-06-08 00:20:47 +0000909** that does no originate from the ON or USING clauses of a join.
910** Return 0 if it involves variables or function calls or terms from
911** an ON or USING clause.
912*/
913int sqlite3ExprIsConstantNotJoin(Expr *p){
914 int isConst = 3;
915 walkExprTree(p, exprNodeIsConstant, &isConst);
916 return isConst!=0;
917}
918
919/*
920** Walk an expression tree. Return 1 if the expression is constant
drheb55bd22005-06-30 17:04:21 +0000921** or a function call with constant arguments. Return and 0 if there
922** are any variables.
923**
924** For the purposes of this function, a double-quoted string (ex: "abc")
925** is considered a variable but a single-quoted string (ex: 'abc') is
926** a constant.
927*/
928int sqlite3ExprIsConstantOrFunction(Expr *p){
929 int isConst = 2;
930 walkExprTree(p, exprNodeIsConstant, &isConst);
931 return isConst!=0;
932}
933
934/*
drh73b211a2005-01-18 04:00:42 +0000935** If the expression p codes a constant integer that is small enough
drh202b2df2004-01-06 01:13:46 +0000936** to fit in a 32-bit integer, return 1 and put the value of the integer
937** in *pValue. If the expression is not an integer or if it is too big
938** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
drhe4de1fe2002-06-02 16:09:01 +0000939*/
danielk19774adee202004-05-08 08:23:19 +0000940int sqlite3ExprIsInteger(Expr *p, int *pValue){
drhe4de1fe2002-06-02 16:09:01 +0000941 switch( p->op ){
942 case TK_INTEGER: {
drh2646da72005-12-09 20:02:05 +0000943 if( sqlite3GetInt32((char*)p->token.z, pValue) ){
drh202b2df2004-01-06 01:13:46 +0000944 return 1;
945 }
946 break;
drhe4de1fe2002-06-02 16:09:01 +0000947 }
drh4b59ab52002-08-24 18:24:51 +0000948 case TK_UPLUS: {
danielk19774adee202004-05-08 08:23:19 +0000949 return sqlite3ExprIsInteger(p->pLeft, pValue);
drh4b59ab52002-08-24 18:24:51 +0000950 }
drhe4de1fe2002-06-02 16:09:01 +0000951 case TK_UMINUS: {
952 int v;
danielk19774adee202004-05-08 08:23:19 +0000953 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
drhe4de1fe2002-06-02 16:09:01 +0000954 *pValue = -v;
955 return 1;
956 }
957 break;
958 }
959 default: break;
960 }
961 return 0;
962}
963
964/*
drhc4a3c772001-04-04 11:48:57 +0000965** Return TRUE if the given string is a row-id column name.
966*/
danielk19774adee202004-05-08 08:23:19 +0000967int sqlite3IsRowid(const char *z){
968 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
969 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
970 if( sqlite3StrICmp(z, "OID")==0 ) return 1;
drhc4a3c772001-04-04 11:48:57 +0000971 return 0;
972}
973
974/*
drh8141f612004-01-25 22:44:58 +0000975** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
976** that name in the set of source tables in pSrcList and make the pExpr
977** expression node refer back to that source column. The following changes
978** are made to pExpr:
979**
980** pExpr->iDb Set the index in db->aDb[] of the database holding
981** the table.
982** pExpr->iTable Set to the cursor number for the table obtained
983** from pSrcList.
984** pExpr->iColumn Set to the column number within the table.
drh8141f612004-01-25 22:44:58 +0000985** pExpr->op Set to TK_COLUMN.
986** pExpr->pLeft Any expression this points to is deleted
987** pExpr->pRight Any expression this points to is deleted.
988**
989** The pDbToken is the name of the database (the "X"). This value may be
990** NULL meaning that name is of the form Y.Z or Z. Any available database
991** can be used. The pTableToken is the name of the table (the "Y"). This
992** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it
993** means that the form of the name is Z and that columns from any table
994** can be used.
995**
996** If the name cannot be resolved unambiguously, leave an error message
997** in pParse and return non-zero. Return zero on success.
998*/
999static int lookupName(
drhffe07b22005-11-03 00:41:17 +00001000 Parse *pParse, /* The parsing context */
drh8141f612004-01-25 22:44:58 +00001001 Token *pDbToken, /* Name of the database containing table, or NULL */
1002 Token *pTableToken, /* Name of table containing column, or NULL */
1003 Token *pColumnToken, /* Name of the column. */
drh626a8792005-01-17 22:08:19 +00001004 NameContext *pNC, /* The name context used to resolve the name */
drh8141f612004-01-25 22:44:58 +00001005 Expr *pExpr /* Make this EXPR node point to the selected column */
1006){
1007 char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */
1008 char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */
1009 char *zCol = 0; /* Name of the column. The "Z" */
1010 int i, j; /* Loop counters */
1011 int cnt = 0; /* Number of matching column names */
1012 int cntTab = 0; /* Number of matching table names */
drh626a8792005-01-17 22:08:19 +00001013 sqlite3 *db = pParse->db; /* The database */
drh51669862004-12-18 18:40:26 +00001014 struct SrcList_item *pItem; /* Use for looping over pSrcList items */
1015 struct SrcList_item *pMatch = 0; /* The matching pSrcList item */
drh73b211a2005-01-18 04:00:42 +00001016 NameContext *pTopNC = pNC; /* First namecontext in the list */
drh8141f612004-01-25 22:44:58 +00001017
1018 assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
drh17435752007-08-16 04:30:38 +00001019 zDb = sqlite3NameFromToken(db, pDbToken);
1020 zTab = sqlite3NameFromToken(db, pTableToken);
1021 zCol = sqlite3NameFromToken(db, pColumnToken);
1022 if( db->mallocFailed ){
danielk1977d5d56522005-03-16 12:15:20 +00001023 goto lookupname_end;
drh8141f612004-01-25 22:44:58 +00001024 }
drh8141f612004-01-25 22:44:58 +00001025
1026 pExpr->iTable = -1;
drh626a8792005-01-17 22:08:19 +00001027 while( pNC && cnt==0 ){
drhffe07b22005-11-03 00:41:17 +00001028 ExprList *pEList;
drh626a8792005-01-17 22:08:19 +00001029 SrcList *pSrcList = pNC->pSrcList;
drh8141f612004-01-25 22:44:58 +00001030
danielk1977b3bce662005-01-29 08:32:43 +00001031 if( pSrcList ){
1032 for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
drh43617e92006-03-06 20:55:46 +00001033 Table *pTab;
1034 int iDb;
danielk1977b3bce662005-01-29 08:32:43 +00001035 Column *pCol;
1036
drh43617e92006-03-06 20:55:46 +00001037 pTab = pItem->pTab;
1038 assert( pTab!=0 );
1039 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
danielk1977b3bce662005-01-29 08:32:43 +00001040 assert( pTab->nCol>0 );
1041 if( zTab ){
1042 if( pItem->zAlias ){
1043 char *zTabName = pItem->zAlias;
1044 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
1045 }else{
1046 char *zTabName = pTab->zName;
1047 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
danielk1977da184232006-01-05 11:34:32 +00001048 if( zDb!=0 && sqlite3StrICmp(db->aDb[iDb].zName, zDb)!=0 ){
danielk1977b3bce662005-01-29 08:32:43 +00001049 continue;
1050 }
drh626a8792005-01-17 22:08:19 +00001051 }
drh8141f612004-01-25 22:44:58 +00001052 }
danielk1977b3bce662005-01-29 08:32:43 +00001053 if( 0==(cntTab++) ){
drh626a8792005-01-17 22:08:19 +00001054 pExpr->iTable = pItem->iCursor;
danielk1977da184232006-01-05 11:34:32 +00001055 pExpr->pSchema = pTab->pSchema;
danielk1977b3bce662005-01-29 08:32:43 +00001056 pMatch = pItem;
1057 }
1058 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
1059 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
danielk1977b3bf5562006-01-10 17:58:23 +00001060 const char *zColl = pTab->aCol[j].zColl;
drh873fac02005-06-06 17:11:46 +00001061 IdList *pUsing;
danielk1977b3bce662005-01-29 08:32:43 +00001062 cnt++;
1063 pExpr->iTable = pItem->iCursor;
1064 pMatch = pItem;
danielk1977da184232006-01-05 11:34:32 +00001065 pExpr->pSchema = pTab->pSchema;
danielk1977b3bce662005-01-29 08:32:43 +00001066 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
1067 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
1068 pExpr->affinity = pTab->aCol[j].affinity;
drh8b4c40d2007-02-01 23:02:45 +00001069 if( (pExpr->flags & EP_ExpCollate)==0 ){
1070 pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0);
1071 }
drh61dfc312006-12-16 16:25:15 +00001072 if( i<pSrcList->nSrc-1 ){
1073 if( pItem[1].jointype & JT_NATURAL ){
1074 /* If this match occurred in the left table of a natural join,
1075 ** then skip the right table to avoid a duplicate match */
1076 pItem++;
1077 i++;
1078 }else if( (pUsing = pItem[1].pUsing)!=0 ){
1079 /* If this match occurs on a column that is in the USING clause
1080 ** of a join, skip the search of the right table of the join
1081 ** to avoid a duplicate match there. */
1082 int k;
1083 for(k=0; k<pUsing->nId; k++){
1084 if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ){
1085 pItem++;
1086 i++;
1087 break;
1088 }
drh873fac02005-06-06 17:11:46 +00001089 }
1090 }
1091 }
danielk1977b3bce662005-01-29 08:32:43 +00001092 break;
1093 }
drh8141f612004-01-25 22:44:58 +00001094 }
1095 }
1096 }
drh626a8792005-01-17 22:08:19 +00001097
1098#ifndef SQLITE_OMIT_TRIGGER
1099 /* If we have not already resolved the name, then maybe
1100 ** it is a new.* or old.* trigger argument reference
1101 */
1102 if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
1103 TriggerStack *pTriggerStack = pParse->trigStack;
1104 Table *pTab = 0;
1105 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){
1106 pExpr->iTable = pTriggerStack->newIdx;
1107 assert( pTriggerStack->pTab );
1108 pTab = pTriggerStack->pTab;
1109 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab)==0 ){
1110 pExpr->iTable = pTriggerStack->oldIdx;
1111 assert( pTriggerStack->pTab );
1112 pTab = pTriggerStack->pTab;
1113 }
1114
1115 if( pTab ){
danielk1977f0113002006-01-24 12:09:17 +00001116 int iCol;
drh626a8792005-01-17 22:08:19 +00001117 Column *pCol = pTab->aCol;
1118
danielk1977da184232006-01-05 11:34:32 +00001119 pExpr->pSchema = pTab->pSchema;
drh626a8792005-01-17 22:08:19 +00001120 cntTab++;
danielk1977f0113002006-01-24 12:09:17 +00001121 for(iCol=0; iCol < pTab->nCol; iCol++, pCol++) {
drh626a8792005-01-17 22:08:19 +00001122 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
danielk1977f0113002006-01-24 12:09:17 +00001123 const char *zColl = pTab->aCol[iCol].zColl;
drh626a8792005-01-17 22:08:19 +00001124 cnt++;
danielk1977f0113002006-01-24 12:09:17 +00001125 pExpr->iColumn = iCol==pTab->iPKey ? -1 : iCol;
1126 pExpr->affinity = pTab->aCol[iCol].affinity;
drh8b4c40d2007-02-01 23:02:45 +00001127 if( (pExpr->flags & EP_ExpCollate)==0 ){
1128 pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0);
1129 }
danielk1977aee18ef2005-03-09 12:26:50 +00001130 pExpr->pTab = pTab;
drh626a8792005-01-17 22:08:19 +00001131 break;
1132 }
1133 }
1134 }
1135 }
drhb7f91642004-10-31 02:22:47 +00001136#endif /* !defined(SQLITE_OMIT_TRIGGER) */
drh8141f612004-01-25 22:44:58 +00001137
drh626a8792005-01-17 22:08:19 +00001138 /*
1139 ** Perhaps the name is a reference to the ROWID
1140 */
1141 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
1142 cnt = 1;
1143 pExpr->iColumn = -1;
drh8a512562005-11-14 22:29:05 +00001144 pExpr->affinity = SQLITE_AFF_INTEGER;
drh626a8792005-01-17 22:08:19 +00001145 }
drh8141f612004-01-25 22:44:58 +00001146
drh626a8792005-01-17 22:08:19 +00001147 /*
1148 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
1149 ** might refer to an result-set alias. This happens, for example, when
1150 ** we are resolving names in the WHERE clause of the following command:
1151 **
1152 ** SELECT a+b AS x FROM table WHERE x<10;
1153 **
1154 ** In cases like this, replace pExpr with a copy of the expression that
1155 ** forms the result set entry ("a+b" in the example) and return immediately.
1156 ** Note that the expression in the result set should have already been
1157 ** resolved by the time the WHERE clause is resolved.
1158 */
drhffe07b22005-11-03 00:41:17 +00001159 if( cnt==0 && (pEList = pNC->pEList)!=0 && zTab==0 ){
drh626a8792005-01-17 22:08:19 +00001160 for(j=0; j<pEList->nExpr; j++){
1161 char *zAs = pEList->a[j].zName;
1162 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
drh36379e92007-07-23 22:51:15 +00001163 Expr *pDup, *pOrig;
drh626a8792005-01-17 22:08:19 +00001164 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
drh4f07e5f2007-05-14 11:34:46 +00001165 assert( pExpr->pList==0 );
1166 assert( pExpr->pSelect==0 );
drh36379e92007-07-23 22:51:15 +00001167 pOrig = pEList->a[j].pExpr;
1168 if( !pNC->allowAgg && ExprHasProperty(pOrig, EP_Agg) ){
1169 sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs);
drh17435752007-08-16 04:30:38 +00001170 sqlite3_free(zCol);
drh36379e92007-07-23 22:51:15 +00001171 return 2;
1172 }
danielk19771e536952007-08-16 10:09:01 +00001173 pDup = sqlite3ExprDup(db, pOrig);
drh4f07e5f2007-05-14 11:34:46 +00001174 if( pExpr->flags & EP_ExpCollate ){
1175 pDup->pColl = pExpr->pColl;
1176 pDup->flags |= EP_ExpCollate;
1177 }
drh17435752007-08-16 04:30:38 +00001178 if( pExpr->span.dyn ) sqlite3_free((char*)pExpr->span.z);
1179 if( pExpr->token.dyn ) sqlite3_free((char*)pExpr->token.z);
drh4f07e5f2007-05-14 11:34:46 +00001180 memcpy(pExpr, pDup, sizeof(*pExpr));
drh17435752007-08-16 04:30:38 +00001181 sqlite3_free(pDup);
drh15ccce12005-05-23 15:06:39 +00001182 cnt = 1;
danielk1977c9cf6e32007-06-25 16:29:33 +00001183 pMatch = 0;
drh626a8792005-01-17 22:08:19 +00001184 assert( zTab==0 && zDb==0 );
drh15ccce12005-05-23 15:06:39 +00001185 goto lookupname_end_2;
drh626a8792005-01-17 22:08:19 +00001186 }
1187 }
1188 }
1189
1190 /* Advance to the next name context. The loop will exit when either
1191 ** we have a match (cnt>0) or when we run out of name contexts.
1192 */
1193 if( cnt==0 ){
1194 pNC = pNC->pNext;
1195 }
drh8141f612004-01-25 22:44:58 +00001196 }
1197
1198 /*
1199 ** If X and Y are NULL (in other words if only the column name Z is
1200 ** supplied) and the value of Z is enclosed in double-quotes, then
1201 ** Z is a string literal if it doesn't match any column names. In that
1202 ** case, we need to return right away and not make any changes to
1203 ** pExpr.
drh15ccce12005-05-23 15:06:39 +00001204 **
1205 ** Because no reference was made to outer contexts, the pNC->nRef
1206 ** fields are not changed in any context.
drh8141f612004-01-25 22:44:58 +00001207 */
1208 if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
drh17435752007-08-16 04:30:38 +00001209 sqlite3_free(zCol);
drh8141f612004-01-25 22:44:58 +00001210 return 0;
1211 }
1212
1213 /*
1214 ** cnt==0 means there was not match. cnt>1 means there were two or
1215 ** more matches. Either way, we have an error.
1216 */
1217 if( cnt!=1 ){
1218 char *z = 0;
1219 char *zErr;
1220 zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s";
1221 if( zDb ){
drhf93339d2006-01-03 15:16:26 +00001222 sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, (char*)0);
drh8141f612004-01-25 22:44:58 +00001223 }else if( zTab ){
drhf93339d2006-01-03 15:16:26 +00001224 sqlite3SetString(&z, zTab, ".", zCol, (char*)0);
drh8141f612004-01-25 22:44:58 +00001225 }else{
drh17435752007-08-16 04:30:38 +00001226 z = sqlite3StrDup(zCol);
drh8141f612004-01-25 22:44:58 +00001227 }
danielk1977a1644fd2007-08-29 12:31:25 +00001228 if( z ){
1229 sqlite3ErrorMsg(pParse, zErr, z);
1230 sqlite3_free(z);
1231 pTopNC->nErr++;
1232 }else{
1233 db->mallocFailed = 1;
1234 }
drh8141f612004-01-25 22:44:58 +00001235 }
1236
drh51669862004-12-18 18:40:26 +00001237 /* If a column from a table in pSrcList is referenced, then record
1238 ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes
1239 ** bit 0 to be set. Column 1 sets bit 1. And so forth. If the
1240 ** column number is greater than the number of bits in the bitmask
1241 ** then set the high-order bit of the bitmask.
1242 */
1243 if( pExpr->iColumn>=0 && pMatch!=0 ){
1244 int n = pExpr->iColumn;
1245 if( n>=sizeof(Bitmask)*8 ){
1246 n = sizeof(Bitmask)*8-1;
1247 }
1248 assert( pMatch->iCursor==pExpr->iTable );
drhca83ac52007-02-01 01:40:44 +00001249 pMatch->colUsed |= ((Bitmask)1)<<n;
drh51669862004-12-18 18:40:26 +00001250 }
1251
danielk1977d5d56522005-03-16 12:15:20 +00001252lookupname_end:
drh8141f612004-01-25 22:44:58 +00001253 /* Clean up and return
1254 */
drh17435752007-08-16 04:30:38 +00001255 sqlite3_free(zDb);
1256 sqlite3_free(zTab);
danielk19774adee202004-05-08 08:23:19 +00001257 sqlite3ExprDelete(pExpr->pLeft);
drh8141f612004-01-25 22:44:58 +00001258 pExpr->pLeft = 0;
danielk19774adee202004-05-08 08:23:19 +00001259 sqlite3ExprDelete(pExpr->pRight);
drh8141f612004-01-25 22:44:58 +00001260 pExpr->pRight = 0;
1261 pExpr->op = TK_COLUMN;
drh15ccce12005-05-23 15:06:39 +00001262lookupname_end_2:
drh17435752007-08-16 04:30:38 +00001263 sqlite3_free(zCol);
drh626a8792005-01-17 22:08:19 +00001264 if( cnt==1 ){
danielk1977b3bce662005-01-29 08:32:43 +00001265 assert( pNC!=0 );
drh626a8792005-01-17 22:08:19 +00001266 sqlite3AuthRead(pParse, pExpr, pNC->pSrcList);
danielk1977aee18ef2005-03-09 12:26:50 +00001267 if( pMatch && !pMatch->pSelect ){
1268 pExpr->pTab = pMatch->pTab;
1269 }
drh15ccce12005-05-23 15:06:39 +00001270 /* Increment the nRef value on all name contexts from TopNC up to
1271 ** the point where the name matched. */
1272 for(;;){
1273 assert( pTopNC!=0 );
1274 pTopNC->nRef++;
1275 if( pTopNC==pNC ) break;
1276 pTopNC = pTopNC->pNext;
1277 }
1278 return 0;
1279 } else {
1280 return 1;
drh626a8792005-01-17 22:08:19 +00001281 }
drh8141f612004-01-25 22:44:58 +00001282}
1283
1284/*
drh626a8792005-01-17 22:08:19 +00001285** This routine is designed as an xFunc for walkExprTree().
1286**
drh73b211a2005-01-18 04:00:42 +00001287** Resolve symbolic names into TK_COLUMN operators for the current
drh626a8792005-01-17 22:08:19 +00001288** node in the expression tree. Return 0 to continue the search down
drh73b211a2005-01-18 04:00:42 +00001289** the tree or 2 to abort the tree walk.
1290**
1291** This routine also does error checking and name resolution for
1292** function names. The operator for aggregate functions is changed
1293** to TK_AGG_FUNCTION.
drh626a8792005-01-17 22:08:19 +00001294*/
1295static int nameResolverStep(void *pArg, Expr *pExpr){
1296 NameContext *pNC = (NameContext*)pArg;
drh626a8792005-01-17 22:08:19 +00001297 Parse *pParse;
drh626a8792005-01-17 22:08:19 +00001298
danielk1977b3bce662005-01-29 08:32:43 +00001299 if( pExpr==0 ) return 1;
drh626a8792005-01-17 22:08:19 +00001300 assert( pNC!=0 );
drh626a8792005-01-17 22:08:19 +00001301 pParse = pNC->pParse;
danielk1977b3bce662005-01-29 08:32:43 +00001302
drh626a8792005-01-17 22:08:19 +00001303 if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return 1;
1304 ExprSetProperty(pExpr, EP_Resolved);
1305#ifndef NDEBUG
danielk1977f0113002006-01-24 12:09:17 +00001306 if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
1307 SrcList *pSrcList = pNC->pSrcList;
danielk1977940fac92005-01-23 22:41:37 +00001308 int i;
danielk1977f0113002006-01-24 12:09:17 +00001309 for(i=0; i<pNC->pSrcList->nSrc; i++){
drh626a8792005-01-17 22:08:19 +00001310 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
1311 }
1312 }
1313#endif
1314 switch( pExpr->op ){
1315 /* Double-quoted strings (ex: "abc") are used as identifiers if
1316 ** possible. Otherwise they remain as strings. Single-quoted
1317 ** strings (ex: 'abc') are always string literals.
1318 */
1319 case TK_STRING: {
1320 if( pExpr->token.z[0]=='\'' ) break;
1321 /* Fall thru into the TK_ID case if this is a double-quoted string */
1322 }
1323 /* A lone identifier is the name of a column.
1324 */
1325 case TK_ID: {
drh626a8792005-01-17 22:08:19 +00001326 lookupName(pParse, 0, 0, &pExpr->token, pNC, pExpr);
1327 return 1;
1328 }
1329
1330 /* A table name and column name: ID.ID
1331 ** Or a database, table and column: ID.ID.ID
1332 */
1333 case TK_DOT: {
1334 Token *pColumn;
1335 Token *pTable;
1336 Token *pDb;
1337 Expr *pRight;
1338
danielk1977b3bce662005-01-29 08:32:43 +00001339 /* if( pSrcList==0 ) break; */
drh626a8792005-01-17 22:08:19 +00001340 pRight = pExpr->pRight;
1341 if( pRight->op==TK_ID ){
1342 pDb = 0;
1343 pTable = &pExpr->pLeft->token;
1344 pColumn = &pRight->token;
1345 }else{
1346 assert( pRight->op==TK_DOT );
1347 pDb = &pExpr->pLeft->token;
1348 pTable = &pRight->pLeft->token;
1349 pColumn = &pRight->pRight->token;
1350 }
1351 lookupName(pParse, pDb, pTable, pColumn, pNC, pExpr);
1352 return 1;
1353 }
1354
1355 /* Resolve function names
1356 */
drhb71090f2005-05-23 17:26:51 +00001357 case TK_CONST_FUNC:
drh626a8792005-01-17 22:08:19 +00001358 case TK_FUNCTION: {
1359 ExprList *pList = pExpr->pList; /* The argument list */
1360 int n = pList ? pList->nExpr : 0; /* Number of arguments */
1361 int no_such_func = 0; /* True if no such function exists */
1362 int wrong_num_args = 0; /* True if wrong number of arguments */
1363 int is_agg = 0; /* True if is an aggregate function */
1364 int i;
drh5169bbc2006-08-24 14:59:45 +00001365 int auth; /* Authorization to use the function */
drh626a8792005-01-17 22:08:19 +00001366 int nId; /* Number of characters in function name */
1367 const char *zId; /* The function name. */
drh73b211a2005-01-18 04:00:42 +00001368 FuncDef *pDef; /* Information about the function */
danielk197714db2662006-01-09 16:12:04 +00001369 int enc = ENC(pParse->db); /* The database encoding */
drh626a8792005-01-17 22:08:19 +00001370
drh2646da72005-12-09 20:02:05 +00001371 zId = (char*)pExpr->token.z;
drhb71090f2005-05-23 17:26:51 +00001372 nId = pExpr->token.n;
drh626a8792005-01-17 22:08:19 +00001373 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
1374 if( pDef==0 ){
1375 pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
1376 if( pDef==0 ){
1377 no_such_func = 1;
1378 }else{
1379 wrong_num_args = 1;
1380 }
1381 }else{
1382 is_agg = pDef->xFunc==0;
1383 }
drh2fca7fe2006-11-23 11:59:13 +00001384#ifndef SQLITE_OMIT_AUTHORIZATION
drh5169bbc2006-08-24 14:59:45 +00001385 if( pDef ){
1386 auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0);
1387 if( auth!=SQLITE_OK ){
1388 if( auth==SQLITE_DENY ){
1389 sqlite3ErrorMsg(pParse, "not authorized to use function: %s",
1390 pDef->zName);
1391 pNC->nErr++;
1392 }
1393 pExpr->op = TK_NULL;
1394 return 1;
1395 }
1396 }
drhb8b14212006-08-24 15:18:25 +00001397#endif
drh626a8792005-01-17 22:08:19 +00001398 if( is_agg && !pNC->allowAgg ){
1399 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
1400 pNC->nErr++;
1401 is_agg = 0;
1402 }else if( no_such_func ){
1403 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
1404 pNC->nErr++;
1405 }else if( wrong_num_args ){
1406 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
1407 nId, zId);
1408 pNC->nErr++;
1409 }
1410 if( is_agg ){
1411 pExpr->op = TK_AGG_FUNCTION;
1412 pNC->hasAgg = 1;
1413 }
drh73b211a2005-01-18 04:00:42 +00001414 if( is_agg ) pNC->allowAgg = 0;
drh626a8792005-01-17 22:08:19 +00001415 for(i=0; pNC->nErr==0 && i<n; i++){
drh73b211a2005-01-18 04:00:42 +00001416 walkExprTree(pList->a[i].pExpr, nameResolverStep, pNC);
drh626a8792005-01-17 22:08:19 +00001417 }
drh73b211a2005-01-18 04:00:42 +00001418 if( is_agg ) pNC->allowAgg = 1;
drh626a8792005-01-17 22:08:19 +00001419 /* FIX ME: Compute pExpr->affinity based on the expected return
1420 ** type of the function
1421 */
1422 return is_agg;
1423 }
danielk1977b3bce662005-01-29 08:32:43 +00001424#ifndef SQLITE_OMIT_SUBQUERY
1425 case TK_SELECT:
1426 case TK_EXISTS:
1427#endif
1428 case TK_IN: {
1429 if( pExpr->pSelect ){
drh8a9f38f2005-11-05 15:07:55 +00001430 int nRef = pNC->nRef;
drh06f65412005-11-03 02:03:13 +00001431#ifndef SQLITE_OMIT_CHECK
1432 if( pNC->isCheck ){
1433 sqlite3ErrorMsg(pParse,"subqueries prohibited in CHECK constraints");
1434 }
1435#endif
danielk1977b3bce662005-01-29 08:32:43 +00001436 sqlite3SelectResolve(pParse, pExpr->pSelect, pNC);
1437 assert( pNC->nRef>=nRef );
1438 if( nRef!=pNC->nRef ){
1439 ExprSetProperty(pExpr, EP_VarSelect);
1440 }
1441 }
drh4284fb02005-11-03 12:33:28 +00001442 break;
danielk1977b3bce662005-01-29 08:32:43 +00001443 }
drh4284fb02005-11-03 12:33:28 +00001444#ifndef SQLITE_OMIT_CHECK
1445 case TK_VARIABLE: {
1446 if( pNC->isCheck ){
1447 sqlite3ErrorMsg(pParse,"parameters prohibited in CHECK constraints");
1448 }
1449 break;
1450 }
1451#endif
drh626a8792005-01-17 22:08:19 +00001452 }
1453 return 0;
1454}
1455
1456/*
drhcce7d172000-05-31 15:34:51 +00001457** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +00001458** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +00001459** index to the table in the table list and a column offset. The
1460** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
1461** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +00001462** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +00001463** VDBE cursor number for a cursor that is pointing into the referenced
1464** table. The Expr.iColumn value is changed to the index of the column
1465** of the referenced table. The Expr.iColumn value for the special
1466** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
1467** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +00001468**
drh626a8792005-01-17 22:08:19 +00001469** Also resolve function names and check the functions for proper
1470** usage. Make sure all function names are recognized and all functions
1471** have the correct number of arguments. Leave an error message
1472** in pParse->zErrMsg if anything is amiss. Return the number of errors.
1473**
drh73b211a2005-01-18 04:00:42 +00001474** If the expression contains aggregate functions then set the EP_Agg
1475** property on the expression.
drh626a8792005-01-17 22:08:19 +00001476*/
danielk1977fc976062007-05-10 10:46:56 +00001477int sqlite3ExprResolveNames(
danielk1977b3bce662005-01-29 08:32:43 +00001478 NameContext *pNC, /* Namespace to resolve expressions in. */
1479 Expr *pExpr /* The expression to be analyzed. */
drh626a8792005-01-17 22:08:19 +00001480){
drh13449892005-09-07 21:22:45 +00001481 int savedHasAgg;
drh73b211a2005-01-18 04:00:42 +00001482 if( pExpr==0 ) return 0;
danielk1977e6a58a42007-08-31 17:42:48 +00001483#if defined(SQLITE_TEST) || SQLITE_MAX_EXPR_DEPTH>0
danielk1977fc976062007-05-10 10:46:56 +00001484 if( (pExpr->nHeight+pNC->pParse->nHeight)>SQLITE_MAX_EXPR_DEPTH ){
1485 sqlite3ErrorMsg(pNC->pParse,
1486 "Expression tree is too large (maximum depth %d)",
1487 SQLITE_MAX_EXPR_DEPTH
1488 );
1489 return 1;
1490 }
1491 pNC->pParse->nHeight += pExpr->nHeight;
1492#endif
drh13449892005-09-07 21:22:45 +00001493 savedHasAgg = pNC->hasAgg;
1494 pNC->hasAgg = 0;
danielk1977b3bce662005-01-29 08:32:43 +00001495 walkExprTree(pExpr, nameResolverStep, pNC);
danielk1977e6a58a42007-08-31 17:42:48 +00001496#if defined(SQLITE_TEST) || SQLITE_MAX_EXPR_DEPTH>0
danielk1977fc976062007-05-10 10:46:56 +00001497 pNC->pParse->nHeight -= pExpr->nHeight;
1498#endif
danielk1977b3bce662005-01-29 08:32:43 +00001499 if( pNC->nErr>0 ){
drh73b211a2005-01-18 04:00:42 +00001500 ExprSetProperty(pExpr, EP_Error);
drh73b211a2005-01-18 04:00:42 +00001501 }
drh13449892005-09-07 21:22:45 +00001502 if( pNC->hasAgg ){
1503 ExprSetProperty(pExpr, EP_Agg);
1504 }else if( savedHasAgg ){
1505 pNC->hasAgg = 1;
1506 }
drh73b211a2005-01-18 04:00:42 +00001507 return ExprHasProperty(pExpr, EP_Error);
drh626a8792005-01-17 22:08:19 +00001508}
1509
drh1398ad32005-01-19 23:24:50 +00001510/*
1511** A pointer instance of this structure is used to pass information
1512** through walkExprTree into codeSubqueryStep().
1513*/
1514typedef struct QueryCoder QueryCoder;
1515struct QueryCoder {
1516 Parse *pParse; /* The parsing context */
1517 NameContext *pNC; /* Namespace of first enclosing query */
1518};
1519
drh626a8792005-01-17 22:08:19 +00001520
1521/*
drh9cbe6352005-11-29 03:13:21 +00001522** Generate code for scalar subqueries used as an expression
1523** and IN operators. Examples:
drh626a8792005-01-17 22:08:19 +00001524**
drh9cbe6352005-11-29 03:13:21 +00001525** (SELECT a FROM b) -- subquery
1526** EXISTS (SELECT a FROM b) -- EXISTS subquery
1527** x IN (4,5,11) -- IN operator with list on right-hand side
1528** x IN (SELECT a FROM b) -- IN operator with subquery on the right
drhfef52082000-06-06 01:50:43 +00001529**
drh9cbe6352005-11-29 03:13:21 +00001530** The pExpr parameter describes the expression that contains the IN
1531** operator or subquery.
drhcce7d172000-05-31 15:34:51 +00001532*/
drh51522cd2005-01-20 13:36:19 +00001533#ifndef SQLITE_OMIT_SUBQUERY
danielk1977b3bce662005-01-29 08:32:43 +00001534void sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){
drh57dbd7b2005-07-08 18:25:26 +00001535 int testAddr = 0; /* One-time test address */
danielk1977b3bce662005-01-29 08:32:43 +00001536 Vdbe *v = sqlite3GetVdbe(pParse);
1537 if( v==0 ) return;
1538
danielk1977fc976062007-05-10 10:46:56 +00001539
drh57dbd7b2005-07-08 18:25:26 +00001540 /* This code must be run in its entirety every time it is encountered
1541 ** if any of the following is true:
1542 **
1543 ** * The right-hand side is a correlated subquery
1544 ** * The right-hand side is an expression list containing variables
1545 ** * We are inside a trigger
1546 **
1547 ** If all of the above are false, then we can run this code just once
1548 ** save the results, and reuse the same result on subsequent invocations.
danielk1977b3bce662005-01-29 08:32:43 +00001549 */
1550 if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->trigStack ){
1551 int mem = pParse->nMem++;
1552 sqlite3VdbeAddOp(v, OP_MemLoad, mem, 0);
drh57dbd7b2005-07-08 18:25:26 +00001553 testAddr = sqlite3VdbeAddOp(v, OP_If, 0, 0);
drh17435752007-08-16 04:30:38 +00001554 assert( testAddr>0 || pParse->db->mallocFailed );
drhd654be82005-09-20 17:42:23 +00001555 sqlite3VdbeAddOp(v, OP_MemInt, 1, mem);
danielk1977b3bce662005-01-29 08:32:43 +00001556 }
1557
drhcce7d172000-05-31 15:34:51 +00001558 switch( pExpr->op ){
drhfef52082000-06-06 01:50:43 +00001559 case TK_IN: {
danielk1977e014a832004-05-17 10:48:57 +00001560 char affinity;
drhd3d39e92004-05-20 22:16:29 +00001561 KeyInfo keyInfo;
drhb9bb7c12006-06-11 23:41:55 +00001562 int addr; /* Address of OP_OpenEphemeral instruction */
drhd3d39e92004-05-20 22:16:29 +00001563
danielk1977bf3b7212004-05-18 10:06:24 +00001564 affinity = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +00001565
1566 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
drh57dbd7b2005-07-08 18:25:26 +00001567 ** expression it is handled the same way. A virtual table is
danielk1977e014a832004-05-17 10:48:57 +00001568 ** filled with single-field index keys representing the results
1569 ** from the SELECT or the <exprlist>.
1570 **
1571 ** If the 'x' expression is a column value, or the SELECT...
1572 ** statement returns a column value, then the affinity of that
1573 ** column is used to build the index keys. If both 'x' and the
1574 ** SELECT... statement are columns, then numeric affinity is used
1575 ** if either column has NUMERIC or INTEGER affinity. If neither
1576 ** 'x' nor the SELECT... statement are columns, then numeric affinity
1577 ** is used.
1578 */
1579 pExpr->iTable = pParse->nTab++;
drhb9bb7c12006-06-11 23:41:55 +00001580 addr = sqlite3VdbeAddOp(v, OP_OpenEphemeral, pExpr->iTable, 0);
drhd3d39e92004-05-20 22:16:29 +00001581 memset(&keyInfo, 0, sizeof(keyInfo));
1582 keyInfo.nField = 1;
drhf3218fe2004-05-28 08:21:02 +00001583 sqlite3VdbeAddOp(v, OP_SetNumColumns, pExpr->iTable, 1);
danielk1977e014a832004-05-17 10:48:57 +00001584
drhfef52082000-06-06 01:50:43 +00001585 if( pExpr->pSelect ){
1586 /* Case 1: expr IN (SELECT ...)
1587 **
danielk1977e014a832004-05-17 10:48:57 +00001588 ** Generate code to write the results of the select into the temporary
1589 ** table allocated and opened above.
drhfef52082000-06-06 01:50:43 +00001590 */
danielk1977e014a832004-05-17 10:48:57 +00001591 int iParm = pExpr->iTable + (((int)affinity)<<16);
drhbe5c89a2004-07-26 00:31:09 +00001592 ExprList *pEList;
danielk1977e014a832004-05-17 10:48:57 +00001593 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
drh94ccde52007-04-13 16:06:32 +00001594 if( sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0) ){
1595 return;
1596 }
drhbe5c89a2004-07-26 00:31:09 +00001597 pEList = pExpr->pSelect->pEList;
1598 if( pEList && pEList->nExpr>0 ){
danielk1977bcbb04e2007-05-29 12:11:29 +00001599 keyInfo.aColl[0] = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft,
drhbe5c89a2004-07-26 00:31:09 +00001600 pEList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001601 }
drhfef52082000-06-06 01:50:43 +00001602 }else if( pExpr->pList ){
1603 /* Case 2: expr IN (exprlist)
1604 **
drhfd131da2007-08-07 17:13:03 +00001605 ** For each expression, build an index key from the evaluation and
danielk1977e014a832004-05-17 10:48:57 +00001606 ** store it in the temporary table. If <expr> is a column, then use
1607 ** that columns affinity when building index keys. If <expr> is not
1608 ** a column, use numeric affinity.
drhfef52082000-06-06 01:50:43 +00001609 */
danielk1977e014a832004-05-17 10:48:57 +00001610 int i;
drh57dbd7b2005-07-08 18:25:26 +00001611 ExprList *pList = pExpr->pList;
1612 struct ExprList_item *pItem;
1613
danielk1977e014a832004-05-17 10:48:57 +00001614 if( !affinity ){
drh8159a352006-05-23 23:22:29 +00001615 affinity = SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +00001616 }
danielk19770202b292004-06-09 09:55:16 +00001617 keyInfo.aColl[0] = pExpr->pLeft->pColl;
danielk1977e014a832004-05-17 10:48:57 +00001618
1619 /* Loop through each expression in <exprlist>. */
drh57dbd7b2005-07-08 18:25:26 +00001620 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
1621 Expr *pE2 = pItem->pExpr;
danielk1977e014a832004-05-17 10:48:57 +00001622
drh57dbd7b2005-07-08 18:25:26 +00001623 /* If the expression is not constant then we will need to
1624 ** disable the test that was generated above that makes sure
1625 ** this code only executes once. Because for a non-constant
1626 ** expression we need to rerun this code each time.
1627 */
drh6c30be82005-07-29 15:10:17 +00001628 if( testAddr>0 && !sqlite3ExprIsConstant(pE2) ){
drhf8875402006-03-17 13:56:34 +00001629 sqlite3VdbeChangeToNoop(v, testAddr-1, 3);
drh57dbd7b2005-07-08 18:25:26 +00001630 testAddr = 0;
drh4794b982000-06-06 13:54:14 +00001631 }
danielk1977e014a832004-05-17 10:48:57 +00001632
1633 /* Evaluate the expression and insert it into the temp table */
1634 sqlite3ExprCode(pParse, pE2);
drh94a11212004-09-25 13:12:14 +00001635 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1);
drhf0863fe2005-06-12 21:35:51 +00001636 sqlite3VdbeAddOp(v, OP_IdxInsert, pExpr->iTable, 0);
drhfef52082000-06-06 01:50:43 +00001637 }
1638 }
danielk19770202b292004-06-09 09:55:16 +00001639 sqlite3VdbeChangeP3(v, addr, (void *)&keyInfo, P3_KEYINFO);
danielk1977b3bce662005-01-29 08:32:43 +00001640 break;
drhfef52082000-06-06 01:50:43 +00001641 }
1642
drh51522cd2005-01-20 13:36:19 +00001643 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001644 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +00001645 /* This has to be a scalar SELECT. Generate code to put the
1646 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +00001647 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +00001648 */
drh2646da72005-12-09 20:02:05 +00001649 static const Token one = { (u8*)"1", 0, 1 };
drh51522cd2005-01-20 13:36:19 +00001650 Select *pSel;
drhec7429a2005-10-06 16:53:14 +00001651 int iMem;
1652 int sop;
drh1398ad32005-01-19 23:24:50 +00001653
drhec7429a2005-10-06 16:53:14 +00001654 pExpr->iColumn = iMem = pParse->nMem++;
drh51522cd2005-01-20 13:36:19 +00001655 pSel = pExpr->pSelect;
1656 if( pExpr->op==TK_SELECT ){
1657 sop = SRT_Mem;
drhec7429a2005-10-06 16:53:14 +00001658 sqlite3VdbeAddOp(v, OP_MemNull, iMem, 0);
1659 VdbeComment((v, "# Init subquery result"));
drh51522cd2005-01-20 13:36:19 +00001660 }else{
drh51522cd2005-01-20 13:36:19 +00001661 sop = SRT_Exists;
drhec7429a2005-10-06 16:53:14 +00001662 sqlite3VdbeAddOp(v, OP_MemInt, 0, iMem);
1663 VdbeComment((v, "# Init EXISTS result"));
drh51522cd2005-01-20 13:36:19 +00001664 }
drhec7429a2005-10-06 16:53:14 +00001665 sqlite3ExprDelete(pSel->pLimit);
danielk1977a1644fd2007-08-29 12:31:25 +00001666 pSel->pLimit = sqlite3PExpr(pParse, TK_INTEGER, 0, 0, &one);
drh94ccde52007-04-13 16:06:32 +00001667 if( sqlite3Select(pParse, pSel, sop, iMem, 0, 0, 0, 0) ){
1668 return;
1669 }
danielk1977b3bce662005-01-29 08:32:43 +00001670 break;
drhcce7d172000-05-31 15:34:51 +00001671 }
1672 }
danielk1977b3bce662005-01-29 08:32:43 +00001673
drh57dbd7b2005-07-08 18:25:26 +00001674 if( testAddr ){
drhd654be82005-09-20 17:42:23 +00001675 sqlite3VdbeJumpHere(v, testAddr);
danielk1977b3bce662005-01-29 08:32:43 +00001676 }
danielk1977fc976062007-05-10 10:46:56 +00001677
danielk1977b3bce662005-01-29 08:32:43 +00001678 return;
drhcce7d172000-05-31 15:34:51 +00001679}
drh51522cd2005-01-20 13:36:19 +00001680#endif /* SQLITE_OMIT_SUBQUERY */
drhcce7d172000-05-31 15:34:51 +00001681
drhcce7d172000-05-31 15:34:51 +00001682/*
drhfec19aa2004-05-19 20:41:03 +00001683** Generate an instruction that will put the integer describe by
1684** text z[0..n-1] on the stack.
1685*/
1686static void codeInteger(Vdbe *v, const char *z, int n){
drhabb6fca2007-08-16 12:24:01 +00001687 assert( z || v==0 || sqlite3VdbeDb(v)->mallocFailed );
danielk1977c9cf9012007-05-30 10:36:47 +00001688 if( z ){
1689 int i;
1690 if( sqlite3GetInt32(z, &i) ){
1691 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
1692 }else if( sqlite3FitsIn64Bits(z) ){
1693 sqlite3VdbeOp3(v, OP_Int64, 0, 0, z, n);
1694 }else{
1695 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n);
1696 }
drhfec19aa2004-05-19 20:41:03 +00001697 }
1698}
1699
drh945498f2007-02-24 11:52:52 +00001700
1701/*
1702** Generate code that will extract the iColumn-th column from
1703** table pTab and push that column value on the stack. There
1704** is an open cursor to pTab in iTable. If iColumn<0 then
1705** code is generated that extracts the rowid.
1706*/
1707void sqlite3ExprCodeGetColumn(Vdbe *v, Table *pTab, int iColumn, int iTable){
1708 if( iColumn<0 ){
1709 int op = (pTab && IsVirtual(pTab)) ? OP_VRowid : OP_Rowid;
1710 sqlite3VdbeAddOp(v, op, iTable, 0);
1711 }else if( pTab==0 ){
1712 sqlite3VdbeAddOp(v, OP_Column, iTable, iColumn);
1713 }else{
1714 int op = IsVirtual(pTab) ? OP_VColumn : OP_Column;
1715 sqlite3VdbeAddOp(v, op, iTable, iColumn);
1716 sqlite3ColumnDefault(v, pTab, iColumn);
1717#ifndef SQLITE_OMIT_FLOATING_POINT
1718 if( pTab->aCol[iColumn].affinity==SQLITE_AFF_REAL ){
1719 sqlite3VdbeAddOp(v, OP_RealAffinity, 0, 0);
1720 }
1721#endif
1722 }
1723}
1724
drhfec19aa2004-05-19 20:41:03 +00001725/*
drhcce7d172000-05-31 15:34:51 +00001726** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +00001727** expression and leave the result on the top of stack.
drhf2bc0132004-10-04 13:19:23 +00001728**
1729** This code depends on the fact that certain token values (ex: TK_EQ)
1730** are the same as opcode values (ex: OP_Eq) that implement the corresponding
1731** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
1732** the make process cause these values to align. Assert()s in the code
1733** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00001734*/
danielk19774adee202004-05-08 08:23:19 +00001735void sqlite3ExprCode(Parse *pParse, Expr *pExpr){
drhcce7d172000-05-31 15:34:51 +00001736 Vdbe *v = pParse->pVdbe;
1737 int op;
drhffe07b22005-11-03 00:41:17 +00001738 int stackChng = 1; /* Amount of change to stack depth */
1739
danielk19777977a172004-11-09 12:44:37 +00001740 if( v==0 ) return;
1741 if( pExpr==0 ){
drhf0863fe2005-06-12 21:35:51 +00001742 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
danielk19777977a172004-11-09 12:44:37 +00001743 return;
1744 }
drhf2bc0132004-10-04 13:19:23 +00001745 op = pExpr->op;
1746 switch( op ){
drh13449892005-09-07 21:22:45 +00001747 case TK_AGG_COLUMN: {
1748 AggInfo *pAggInfo = pExpr->pAggInfo;
1749 struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg];
1750 if( !pAggInfo->directMode ){
1751 sqlite3VdbeAddOp(v, OP_MemLoad, pCol->iMem, 0);
1752 break;
1753 }else if( pAggInfo->useSortingIdx ){
1754 sqlite3VdbeAddOp(v, OP_Column, pAggInfo->sortingIdx,
1755 pCol->iSorterColumn);
1756 break;
1757 }
1758 /* Otherwise, fall thru into the TK_COLUMN case */
1759 }
drh967e8b72000-06-21 13:59:10 +00001760 case TK_COLUMN: {
drhffe07b22005-11-03 00:41:17 +00001761 if( pExpr->iTable<0 ){
1762 /* This only happens when coding check constraints */
1763 assert( pParse->ckOffset>0 );
1764 sqlite3VdbeAddOp(v, OP_Dup, pParse->ckOffset-pExpr->iColumn-1, 1);
drhc4a3c772001-04-04 11:48:57 +00001765 }else{
drh945498f2007-02-24 11:52:52 +00001766 sqlite3ExprCodeGetColumn(v, pExpr->pTab, pExpr->iColumn, pExpr->iTable);
drh22827922000-06-06 17:27:05 +00001767 }
drhcce7d172000-05-31 15:34:51 +00001768 break;
1769 }
1770 case TK_INTEGER: {
drh2646da72005-12-09 20:02:05 +00001771 codeInteger(v, (char*)pExpr->token.z, pExpr->token.n);
drhfec19aa2004-05-19 20:41:03 +00001772 break;
1773 }
1774 case TK_FLOAT:
1775 case TK_STRING: {
drhf2bc0132004-10-04 13:19:23 +00001776 assert( TK_FLOAT==OP_Real );
1777 assert( TK_STRING==OP_String8 );
danielk19771e536952007-08-16 10:09:01 +00001778 sqlite3DequoteExpr(pParse->db, pExpr);
drh2646da72005-12-09 20:02:05 +00001779 sqlite3VdbeOp3(v, op, 0, 0, (char*)pExpr->token.z, pExpr->token.n);
drhcce7d172000-05-31 15:34:51 +00001780 break;
1781 }
drhf0863fe2005-06-12 21:35:51 +00001782 case TK_NULL: {
1783 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
1784 break;
1785 }
danielk19775338a5f2005-01-20 13:03:10 +00001786#ifndef SQLITE_OMIT_BLOB_LITERAL
danielk1977c572ef72004-05-27 09:28:41 +00001787 case TK_BLOB: {
drh6c8c6ce2005-08-23 11:17:58 +00001788 int n;
1789 const char *z;
drhf2bc0132004-10-04 13:19:23 +00001790 assert( TK_BLOB==OP_HexBlob );
drh6c8c6ce2005-08-23 11:17:58 +00001791 n = pExpr->token.n - 3;
drh2646da72005-12-09 20:02:05 +00001792 z = (char*)pExpr->token.z + 2;
drh6c8c6ce2005-08-23 11:17:58 +00001793 assert( n>=0 );
1794 if( n==0 ){
1795 z = "";
1796 }
1797 sqlite3VdbeOp3(v, op, 0, 0, z, n);
danielk1977c572ef72004-05-27 09:28:41 +00001798 break;
1799 }
danielk19775338a5f2005-01-20 13:03:10 +00001800#endif
drh50457892003-09-06 01:10:47 +00001801 case TK_VARIABLE: {
danielk19774adee202004-05-08 08:23:19 +00001802 sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0);
drh895d7472004-08-20 16:02:39 +00001803 if( pExpr->token.n>1 ){
drh2646da72005-12-09 20:02:05 +00001804 sqlite3VdbeChangeP3(v, -1, (char*)pExpr->token.z, pExpr->token.n);
drh895d7472004-08-20 16:02:39 +00001805 }
drh50457892003-09-06 01:10:47 +00001806 break;
1807 }
drh4e0cff62004-11-05 05:10:28 +00001808 case TK_REGISTER: {
1809 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iTable, 0);
1810 break;
1811 }
drh487e2622005-06-25 18:42:14 +00001812#ifndef SQLITE_OMIT_CAST
1813 case TK_CAST: {
1814 /* Expressions of the form: CAST(pLeft AS token) */
danielk1977f0113002006-01-24 12:09:17 +00001815 int aff, to_op;
drh487e2622005-06-25 18:42:14 +00001816 sqlite3ExprCode(pParse, pExpr->pLeft);
drh8a512562005-11-14 22:29:05 +00001817 aff = sqlite3AffinityType(&pExpr->token);
danielk1977f0113002006-01-24 12:09:17 +00001818 to_op = aff - SQLITE_AFF_TEXT + OP_ToText;
1819 assert( to_op==OP_ToText || aff!=SQLITE_AFF_TEXT );
1820 assert( to_op==OP_ToBlob || aff!=SQLITE_AFF_NONE );
1821 assert( to_op==OP_ToNumeric || aff!=SQLITE_AFF_NUMERIC );
1822 assert( to_op==OP_ToInt || aff!=SQLITE_AFF_INTEGER );
1823 assert( to_op==OP_ToReal || aff!=SQLITE_AFF_REAL );
1824 sqlite3VdbeAddOp(v, to_op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001825 stackChng = 0;
drh487e2622005-06-25 18:42:14 +00001826 break;
1827 }
1828#endif /* SQLITE_OMIT_CAST */
drhc9b84a12002-06-20 11:36:48 +00001829 case TK_LT:
1830 case TK_LE:
1831 case TK_GT:
1832 case TK_GE:
1833 case TK_NE:
1834 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00001835 assert( TK_LT==OP_Lt );
1836 assert( TK_LE==OP_Le );
1837 assert( TK_GT==OP_Gt );
1838 assert( TK_GE==OP_Ge );
1839 assert( TK_EQ==OP_Eq );
1840 assert( TK_NE==OP_Ne );
danielk1977a37cdde2004-05-16 11:15:36 +00001841 sqlite3ExprCode(pParse, pExpr->pLeft);
1842 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001843 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001844 stackChng = -1;
danielk1977a37cdde2004-05-16 11:15:36 +00001845 break;
drhc9b84a12002-06-20 11:36:48 +00001846 }
drhcce7d172000-05-31 15:34:51 +00001847 case TK_AND:
1848 case TK_OR:
1849 case TK_PLUS:
1850 case TK_STAR:
1851 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00001852 case TK_REM:
1853 case TK_BITAND:
1854 case TK_BITOR:
drh17c40292004-07-21 02:53:29 +00001855 case TK_SLASH:
drhbf4133c2001-10-13 02:59:08 +00001856 case TK_LSHIFT:
drh855eb1c2004-08-31 13:45:11 +00001857 case TK_RSHIFT:
drh00400772000-06-16 20:51:26 +00001858 case TK_CONCAT: {
drhf2bc0132004-10-04 13:19:23 +00001859 assert( TK_AND==OP_And );
1860 assert( TK_OR==OP_Or );
1861 assert( TK_PLUS==OP_Add );
1862 assert( TK_MINUS==OP_Subtract );
1863 assert( TK_REM==OP_Remainder );
1864 assert( TK_BITAND==OP_BitAnd );
1865 assert( TK_BITOR==OP_BitOr );
1866 assert( TK_SLASH==OP_Divide );
1867 assert( TK_LSHIFT==OP_ShiftLeft );
1868 assert( TK_RSHIFT==OP_ShiftRight );
1869 assert( TK_CONCAT==OP_Concat );
danielk19774adee202004-05-08 08:23:19 +00001870 sqlite3ExprCode(pParse, pExpr->pLeft);
1871 sqlite3ExprCode(pParse, pExpr->pRight);
drh855eb1c2004-08-31 13:45:11 +00001872 sqlite3VdbeAddOp(v, op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001873 stackChng = -1;
drh00400772000-06-16 20:51:26 +00001874 break;
1875 }
drhcce7d172000-05-31 15:34:51 +00001876 case TK_UMINUS: {
drhfec19aa2004-05-19 20:41:03 +00001877 Expr *pLeft = pExpr->pLeft;
1878 assert( pLeft );
1879 if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
1880 Token *p = &pLeft->token;
danielk19771e536952007-08-16 10:09:01 +00001881 char *z = sqlite3MPrintf(pParse->db, "-%.*s", p->n, p->z);
drhfec19aa2004-05-19 20:41:03 +00001882 if( pLeft->op==TK_FLOAT ){
1883 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001884 }else{
drhfec19aa2004-05-19 20:41:03 +00001885 codeInteger(v, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001886 }
drh17435752007-08-16 04:30:38 +00001887 sqlite3_free(z);
drh6e142f52000-06-08 13:36:40 +00001888 break;
1889 }
drh1ccde152000-06-17 13:12:39 +00001890 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +00001891 }
drhbf4133c2001-10-13 02:59:08 +00001892 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00001893 case TK_NOT: {
drhf2bc0132004-10-04 13:19:23 +00001894 assert( TK_BITNOT==OP_BitNot );
1895 assert( TK_NOT==OP_Not );
danielk19774adee202004-05-08 08:23:19 +00001896 sqlite3ExprCode(pParse, pExpr->pLeft);
1897 sqlite3VdbeAddOp(v, op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001898 stackChng = 0;
drhcce7d172000-05-31 15:34:51 +00001899 break;
1900 }
1901 case TK_ISNULL:
1902 case TK_NOTNULL: {
1903 int dest;
drhf2bc0132004-10-04 13:19:23 +00001904 assert( TK_ISNULL==OP_IsNull );
1905 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00001906 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1907 sqlite3ExprCode(pParse, pExpr->pLeft);
1908 dest = sqlite3VdbeCurrentAddr(v) + 2;
1909 sqlite3VdbeAddOp(v, op, 1, dest);
1910 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);
drhffe07b22005-11-03 00:41:17 +00001911 stackChng = 0;
drhf2bc0132004-10-04 13:19:23 +00001912 break;
drhcce7d172000-05-31 15:34:51 +00001913 }
drh22827922000-06-06 17:27:05 +00001914 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00001915 AggInfo *pInfo = pExpr->pAggInfo;
drh7e56e712005-11-16 12:53:15 +00001916 if( pInfo==0 ){
1917 sqlite3ErrorMsg(pParse, "misuse of aggregate: %T",
1918 &pExpr->span);
1919 }else{
1920 sqlite3VdbeAddOp(v, OP_MemLoad, pInfo->aFunc[pExpr->iAgg].iMem, 0);
1921 }
drh22827922000-06-06 17:27:05 +00001922 break;
1923 }
drhb71090f2005-05-23 17:26:51 +00001924 case TK_CONST_FUNC:
drhcce7d172000-05-31 15:34:51 +00001925 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00001926 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00001927 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00001928 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00001929 int nId;
1930 const char *zId;
drh13449892005-09-07 21:22:45 +00001931 int constMask = 0;
danielk1977682f68b2004-06-05 10:22:17 +00001932 int i;
drh17435752007-08-16 04:30:38 +00001933 sqlite3 *db = pParse->db;
1934 u8 enc = ENC(db);
danielk1977dc1bdc42004-06-11 10:51:27 +00001935 CollSeq *pColl = 0;
drh17435752007-08-16 04:30:38 +00001936
drh2646da72005-12-09 20:02:05 +00001937 zId = (char*)pExpr->token.z;
drhb71090f2005-05-23 17:26:51 +00001938 nId = pExpr->token.n;
danielk1977d8123362004-06-12 09:25:12 +00001939 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001940 assert( pDef!=0 );
drhf9b596e2004-05-26 16:54:42 +00001941 nExpr = sqlite3ExprCodeExprList(pParse, pList);
drhb7f6f682006-07-08 17:06:43 +00001942#ifndef SQLITE_OMIT_VIRTUALTABLE
drha43fa222006-07-08 18:41:37 +00001943 /* Possibly overload the function if the first argument is
1944 ** a virtual table column.
1945 **
1946 ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the
1947 ** second argument, not the first, as the argument to test to
1948 ** see if it is a column in a virtual table. This is done because
1949 ** the left operand of infix functions (the operand we want to
1950 ** control overloading) ends up as the second argument to the
1951 ** function. The expression "A glob B" is equivalent to
1952 ** "glob(B,A). We want to use the A in "A glob B" to test
1953 ** for function overloading. But we use the B term in "glob(B,A)".
1954 */
drh6a03a1c2006-07-08 18:34:59 +00001955 if( nExpr>=2 && (pExpr->flags & EP_InfixFunc) ){
drh17435752007-08-16 04:30:38 +00001956 pDef = sqlite3VtabOverloadFunction(db, pDef, nExpr, pList->a[1].pExpr);
drh6a03a1c2006-07-08 18:34:59 +00001957 }else if( nExpr>0 ){
drh17435752007-08-16 04:30:38 +00001958 pDef = sqlite3VtabOverloadFunction(db, pDef, nExpr, pList->a[0].pExpr);
drhb7f6f682006-07-08 17:06:43 +00001959 }
1960#endif
danielk1977682f68b2004-06-05 10:22:17 +00001961 for(i=0; i<nExpr && i<32; i++){
danielk1977d02eb1f2004-06-06 09:44:03 +00001962 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
drh13449892005-09-07 21:22:45 +00001963 constMask |= (1<<i);
danielk1977d02eb1f2004-06-06 09:44:03 +00001964 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001965 if( pDef->needCollSeq && !pColl ){
1966 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
1967 }
1968 }
1969 if( pDef->needCollSeq ){
1970 if( !pColl ) pColl = pParse->db->pDfltColl;
danielk1977d8123362004-06-12 09:25:12 +00001971 sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ);
danielk1977682f68b2004-06-05 10:22:17 +00001972 }
drh13449892005-09-07 21:22:45 +00001973 sqlite3VdbeOp3(v, OP_Function, constMask, nExpr, (char*)pDef, P3_FUNCDEF);
drhffe07b22005-11-03 00:41:17 +00001974 stackChng = 1-nExpr;
drhcce7d172000-05-31 15:34:51 +00001975 break;
1976 }
drhfe2093d2005-01-20 22:48:47 +00001977#ifndef SQLITE_OMIT_SUBQUERY
1978 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001979 case TK_SELECT: {
drh41714d62006-03-02 04:44:23 +00001980 if( pExpr->iColumn==0 ){
1981 sqlite3CodeSubselect(pParse, pExpr);
1982 }
danielk19774adee202004-05-08 08:23:19 +00001983 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drhad6d9462004-09-19 02:15:24 +00001984 VdbeComment((v, "# load subquery result"));
drh19a775c2000-06-05 18:54:46 +00001985 break;
1986 }
drhfef52082000-06-06 01:50:43 +00001987 case TK_IN: {
1988 int addr;
drh94a11212004-09-25 13:12:14 +00001989 char affinity;
drhafa5f682006-01-30 14:36:59 +00001990 int ckOffset = pParse->ckOffset;
danielk1977b3bce662005-01-29 08:32:43 +00001991 sqlite3CodeSubselect(pParse, pExpr);
danielk1977e014a832004-05-17 10:48:57 +00001992
1993 /* Figure out the affinity to use to create a key from the results
1994 ** of the expression. affinityStr stores a static string suitable for
danielk1977ededfd52004-06-17 07:53:01 +00001995 ** P3 of OP_MakeRecord.
danielk1977e014a832004-05-17 10:48:57 +00001996 */
drh94a11212004-09-25 13:12:14 +00001997 affinity = comparisonAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +00001998
danielk19774adee202004-05-08 08:23:19 +00001999 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
danielk1977cdbd8ef2007-05-12 06:11:12 +00002000 pParse->ckOffset = (ckOffset ? (ckOffset+1) : 0);
danielk1977e014a832004-05-17 10:48:57 +00002001
2002 /* Code the <expr> from "<expr> IN (...)". The temporary table
2003 ** pExpr->iTable contains the values that make up the (...) set.
2004 */
danielk19774adee202004-05-08 08:23:19 +00002005 sqlite3ExprCode(pParse, pExpr->pLeft);
2006 addr = sqlite3VdbeCurrentAddr(v);
danielk1977e014a832004-05-17 10:48:57 +00002007 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); /* addr + 0 */
danielk19774adee202004-05-08 08:23:19 +00002008 sqlite3VdbeAddOp(v, OP_Pop, 2, 0);
drhf0863fe2005-06-12 21:35:51 +00002009 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +00002010 sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7);
drh94a11212004-09-25 13:12:14 +00002011 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1); /* addr + 4 */
danielk1977e014a832004-05-17 10:48:57 +00002012 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7);
2013 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); /* addr + 6 */
2014
drhfef52082000-06-06 01:50:43 +00002015 break;
2016 }
danielk197793758c82005-01-21 08:13:14 +00002017#endif
drhfef52082000-06-06 01:50:43 +00002018 case TK_BETWEEN: {
drhbe5c89a2004-07-26 00:31:09 +00002019 Expr *pLeft = pExpr->pLeft;
2020 struct ExprList_item *pLItem = pExpr->pList->a;
2021 Expr *pRight = pLItem->pExpr;
2022 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00002023 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00002024 sqlite3ExprCode(pParse, pRight);
2025 codeCompare(pParse, pLeft, pRight, OP_Ge, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00002026 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
drhbe5c89a2004-07-26 00:31:09 +00002027 pLItem++;
2028 pRight = pLItem->pExpr;
2029 sqlite3ExprCode(pParse, pRight);
2030 codeCompare(pParse, pLeft, pRight, OP_Le, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00002031 sqlite3VdbeAddOp(v, OP_And, 0, 0);
drhfef52082000-06-06 01:50:43 +00002032 break;
2033 }
drh4f07e5f2007-05-14 11:34:46 +00002034 case TK_UPLUS: {
danielk19774adee202004-05-08 08:23:19 +00002035 sqlite3ExprCode(pParse, pExpr->pLeft);
drhffe07b22005-11-03 00:41:17 +00002036 stackChng = 0;
drha2e00042002-01-22 03:13:42 +00002037 break;
2038 }
drh17a7f8d2002-03-24 13:13:27 +00002039 case TK_CASE: {
2040 int expr_end_label;
drhf5905aa2002-05-26 20:54:33 +00002041 int jumpInst;
drhf5905aa2002-05-26 20:54:33 +00002042 int nExpr;
drh17a7f8d2002-03-24 13:13:27 +00002043 int i;
drhbe5c89a2004-07-26 00:31:09 +00002044 ExprList *pEList;
2045 struct ExprList_item *aListelem;
drh17a7f8d2002-03-24 13:13:27 +00002046
2047 assert(pExpr->pList);
2048 assert((pExpr->pList->nExpr % 2) == 0);
2049 assert(pExpr->pList->nExpr > 0);
drhbe5c89a2004-07-26 00:31:09 +00002050 pEList = pExpr->pList;
2051 aListelem = pEList->a;
2052 nExpr = pEList->nExpr;
danielk19774adee202004-05-08 08:23:19 +00002053 expr_end_label = sqlite3VdbeMakeLabel(v);
drh17a7f8d2002-03-24 13:13:27 +00002054 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00002055 sqlite3ExprCode(pParse, pExpr->pLeft);
drh17a7f8d2002-03-24 13:13:27 +00002056 }
drhf5905aa2002-05-26 20:54:33 +00002057 for(i=0; i<nExpr; i=i+2){
drhbe5c89a2004-07-26 00:31:09 +00002058 sqlite3ExprCode(pParse, aListelem[i].pExpr);
drh17a7f8d2002-03-24 13:13:27 +00002059 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00002060 sqlite3VdbeAddOp(v, OP_Dup, 1, 1);
drhbe5c89a2004-07-26 00:31:09 +00002061 jumpInst = codeCompare(pParse, pExpr->pLeft, aListelem[i].pExpr,
2062 OP_Ne, 0, 1);
danielk19774adee202004-05-08 08:23:19 +00002063 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00002064 }else{
danielk19774adee202004-05-08 08:23:19 +00002065 jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00002066 }
drhbe5c89a2004-07-26 00:31:09 +00002067 sqlite3ExprCode(pParse, aListelem[i+1].pExpr);
danielk19774adee202004-05-08 08:23:19 +00002068 sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label);
drhd654be82005-09-20 17:42:23 +00002069 sqlite3VdbeJumpHere(v, jumpInst);
drh17a7f8d2002-03-24 13:13:27 +00002070 }
drhf570f012002-05-31 15:51:25 +00002071 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00002072 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhf570f012002-05-31 15:51:25 +00002073 }
drh17a7f8d2002-03-24 13:13:27 +00002074 if( pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00002075 sqlite3ExprCode(pParse, pExpr->pRight);
drh17a7f8d2002-03-24 13:13:27 +00002076 }else{
drhf0863fe2005-06-12 21:35:51 +00002077 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
drh17a7f8d2002-03-24 13:13:27 +00002078 }
danielk19774adee202004-05-08 08:23:19 +00002079 sqlite3VdbeResolveLabel(v, expr_end_label);
danielk19776f349032002-06-11 02:25:40 +00002080 break;
2081 }
danielk19775338a5f2005-01-20 13:03:10 +00002082#ifndef SQLITE_OMIT_TRIGGER
danielk19776f349032002-06-11 02:25:40 +00002083 case TK_RAISE: {
2084 if( !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +00002085 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00002086 "RAISE() may only be used within a trigger-program");
drhfd131da2007-08-07 17:13:03 +00002087 return;
danielk19776f349032002-06-11 02:25:40 +00002088 }
drhad6d9462004-09-19 02:15:24 +00002089 if( pExpr->iColumn!=OE_Ignore ){
2090 assert( pExpr->iColumn==OE_Rollback ||
2091 pExpr->iColumn == OE_Abort ||
2092 pExpr->iColumn == OE_Fail );
danielk19771e536952007-08-16 10:09:01 +00002093 sqlite3DequoteExpr(pParse->db, pExpr);
drhad6d9462004-09-19 02:15:24 +00002094 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn,
drh2646da72005-12-09 20:02:05 +00002095 (char*)pExpr->token.z, pExpr->token.n);
danielk19776f349032002-06-11 02:25:40 +00002096 } else {
drhad6d9462004-09-19 02:15:24 +00002097 assert( pExpr->iColumn == OE_Ignore );
2098 sqlite3VdbeAddOp(v, OP_ContextPop, 0, 0);
2099 sqlite3VdbeAddOp(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
2100 VdbeComment((v, "# raise(IGNORE)"));
danielk19776f349032002-06-11 02:25:40 +00002101 }
drhffe07b22005-11-03 00:41:17 +00002102 stackChng = 0;
2103 break;
drh17a7f8d2002-03-24 13:13:27 +00002104 }
danielk19775338a5f2005-01-20 13:03:10 +00002105#endif
drhffe07b22005-11-03 00:41:17 +00002106 }
2107
2108 if( pParse->ckOffset ){
2109 pParse->ckOffset += stackChng;
2110 assert( pParse->ckOffset );
drhcce7d172000-05-31 15:34:51 +00002111 }
drhcce7d172000-05-31 15:34:51 +00002112}
2113
danielk197793758c82005-01-21 08:13:14 +00002114#ifndef SQLITE_OMIT_TRIGGER
drhcce7d172000-05-31 15:34:51 +00002115/*
drh25303782004-12-07 15:41:48 +00002116** Generate code that evalutes the given expression and leaves the result
2117** on the stack. See also sqlite3ExprCode().
2118**
2119** This routine might also cache the result and modify the pExpr tree
2120** so that it will make use of the cached result on subsequent evaluations
2121** rather than evaluate the whole expression again. Trivial expressions are
2122** not cached. If the expression is cached, its result is stored in a
2123** memory location.
2124*/
2125void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr){
2126 Vdbe *v = pParse->pVdbe;
2127 int iMem;
2128 int addr1, addr2;
2129 if( v==0 ) return;
2130 addr1 = sqlite3VdbeCurrentAddr(v);
2131 sqlite3ExprCode(pParse, pExpr);
2132 addr2 = sqlite3VdbeCurrentAddr(v);
2133 if( addr2>addr1+1 || sqlite3VdbeGetOp(v, addr1)->opcode==OP_Function ){
2134 iMem = pExpr->iTable = pParse->nMem++;
2135 sqlite3VdbeAddOp(v, OP_MemStore, iMem, 0);
2136 pExpr->op = TK_REGISTER;
2137 }
2138}
danielk197793758c82005-01-21 08:13:14 +00002139#endif
drh25303782004-12-07 15:41:48 +00002140
2141/*
drh268380c2004-02-25 13:47:31 +00002142** Generate code that pushes the value of every element of the given
drhf9b596e2004-05-26 16:54:42 +00002143** expression list onto the stack.
drh268380c2004-02-25 13:47:31 +00002144**
2145** Return the number of elements pushed onto the stack.
2146*/
danielk19774adee202004-05-08 08:23:19 +00002147int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00002148 Parse *pParse, /* Parsing context */
drhf9b596e2004-05-26 16:54:42 +00002149 ExprList *pList /* The expression list to be coded */
drh268380c2004-02-25 13:47:31 +00002150){
2151 struct ExprList_item *pItem;
2152 int i, n;
drh268380c2004-02-25 13:47:31 +00002153 if( pList==0 ) return 0;
drh268380c2004-02-25 13:47:31 +00002154 n = pList->nExpr;
drhc182d162005-08-14 20:47:16 +00002155 for(pItem=pList->a, i=n; i>0; i--, pItem++){
danielk19774adee202004-05-08 08:23:19 +00002156 sqlite3ExprCode(pParse, pItem->pExpr);
drh268380c2004-02-25 13:47:31 +00002157 }
drhf9b596e2004-05-26 16:54:42 +00002158 return n;
drh268380c2004-02-25 13:47:31 +00002159}
2160
2161/*
drhcce7d172000-05-31 15:34:51 +00002162** Generate code for a boolean expression such that a jump is made
2163** to the label "dest" if the expression is true but execution
2164** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00002165**
2166** If the expression evaluates to NULL (neither true nor false), then
2167** take the jump if the jumpIfNull flag is true.
drhf2bc0132004-10-04 13:19:23 +00002168**
2169** This code depends on the fact that certain token values (ex: TK_EQ)
2170** are the same as opcode values (ex: OP_Eq) that implement the corresponding
2171** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
2172** the make process cause these values to align. Assert()s in the code
2173** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00002174*/
danielk19774adee202004-05-08 08:23:19 +00002175void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00002176 Vdbe *v = pParse->pVdbe;
2177 int op = 0;
drhffe07b22005-11-03 00:41:17 +00002178 int ckOffset = pParse->ckOffset;
drhdaffd0e2001-04-11 14:28:42 +00002179 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00002180 op = pExpr->op;
2181 switch( op ){
drhcce7d172000-05-31 15:34:51 +00002182 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00002183 int d2 = sqlite3VdbeMakeLabel(v);
2184 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
2185 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
2186 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00002187 break;
2188 }
2189 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00002190 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
2191 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002192 break;
2193 }
2194 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00002195 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002196 break;
2197 }
2198 case TK_LT:
2199 case TK_LE:
2200 case TK_GT:
2201 case TK_GE:
2202 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00002203 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00002204 assert( TK_LT==OP_Lt );
2205 assert( TK_LE==OP_Le );
2206 assert( TK_GT==OP_Gt );
2207 assert( TK_GE==OP_Ge );
2208 assert( TK_EQ==OP_Eq );
2209 assert( TK_NE==OP_Ne );
danielk19774adee202004-05-08 08:23:19 +00002210 sqlite3ExprCode(pParse, pExpr->pLeft);
2211 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00002212 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002213 break;
2214 }
2215 case TK_ISNULL:
2216 case TK_NOTNULL: {
drhf2bc0132004-10-04 13:19:23 +00002217 assert( TK_ISNULL==OP_IsNull );
2218 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00002219 sqlite3ExprCode(pParse, pExpr->pLeft);
2220 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00002221 break;
2222 }
drhfef52082000-06-06 01:50:43 +00002223 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00002224 /* The expression "x BETWEEN y AND z" is implemented as:
2225 **
2226 ** 1 IF (x < y) GOTO 3
2227 ** 2 IF (x <= z) GOTO <dest>
2228 ** 3 ...
2229 */
drhf5905aa2002-05-26 20:54:33 +00002230 int addr;
drhbe5c89a2004-07-26 00:31:09 +00002231 Expr *pLeft = pExpr->pLeft;
2232 Expr *pRight = pExpr->pList->a[0].pExpr;
2233 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00002234 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00002235 sqlite3ExprCode(pParse, pRight);
2236 addr = codeCompare(pParse, pLeft, pRight, OP_Lt, 0, !jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00002237
drhbe5c89a2004-07-26 00:31:09 +00002238 pRight = pExpr->pList->a[1].pExpr;
2239 sqlite3ExprCode(pParse, pRight);
2240 codeCompare(pParse, pLeft, pRight, OP_Le, dest, jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00002241
danielk19774adee202004-05-08 08:23:19 +00002242 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
drhd654be82005-09-20 17:42:23 +00002243 sqlite3VdbeJumpHere(v, addr);
danielk19774adee202004-05-08 08:23:19 +00002244 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00002245 break;
2246 }
drhcce7d172000-05-31 15:34:51 +00002247 default: {
danielk19774adee202004-05-08 08:23:19 +00002248 sqlite3ExprCode(pParse, pExpr);
2249 sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00002250 break;
2251 }
2252 }
drhffe07b22005-11-03 00:41:17 +00002253 pParse->ckOffset = ckOffset;
drhcce7d172000-05-31 15:34:51 +00002254}
2255
2256/*
drh66b89c82000-11-28 20:47:17 +00002257** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00002258** to the label "dest" if the expression is false but execution
2259** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00002260**
2261** If the expression evaluates to NULL (neither true nor false) then
2262** jump if jumpIfNull is true or fall through if jumpIfNull is false.
drhcce7d172000-05-31 15:34:51 +00002263*/
danielk19774adee202004-05-08 08:23:19 +00002264void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00002265 Vdbe *v = pParse->pVdbe;
2266 int op = 0;
drhffe07b22005-11-03 00:41:17 +00002267 int ckOffset = pParse->ckOffset;
drhdaffd0e2001-04-11 14:28:42 +00002268 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00002269
2270 /* The value of pExpr->op and op are related as follows:
2271 **
2272 ** pExpr->op op
2273 ** --------- ----------
2274 ** TK_ISNULL OP_NotNull
2275 ** TK_NOTNULL OP_IsNull
2276 ** TK_NE OP_Eq
2277 ** TK_EQ OP_Ne
2278 ** TK_GT OP_Le
2279 ** TK_LE OP_Gt
2280 ** TK_GE OP_Lt
2281 ** TK_LT OP_Ge
2282 **
2283 ** For other values of pExpr->op, op is undefined and unused.
2284 ** The value of TK_ and OP_ constants are arranged such that we
2285 ** can compute the mapping above using the following expression.
2286 ** Assert()s verify that the computation is correct.
2287 */
2288 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
2289
2290 /* Verify correct alignment of TK_ and OP_ constants
2291 */
2292 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
2293 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
2294 assert( pExpr->op!=TK_NE || op==OP_Eq );
2295 assert( pExpr->op!=TK_EQ || op==OP_Ne );
2296 assert( pExpr->op!=TK_LT || op==OP_Ge );
2297 assert( pExpr->op!=TK_LE || op==OP_Gt );
2298 assert( pExpr->op!=TK_GT || op==OP_Le );
2299 assert( pExpr->op!=TK_GE || op==OP_Lt );
2300
drhcce7d172000-05-31 15:34:51 +00002301 switch( pExpr->op ){
2302 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00002303 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
2304 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002305 break;
2306 }
2307 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00002308 int d2 = sqlite3VdbeMakeLabel(v);
2309 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
2310 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
2311 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00002312 break;
2313 }
2314 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00002315 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002316 break;
2317 }
2318 case TK_LT:
2319 case TK_LE:
2320 case TK_GT:
2321 case TK_GE:
2322 case TK_NE:
2323 case TK_EQ: {
danielk19774adee202004-05-08 08:23:19 +00002324 sqlite3ExprCode(pParse, pExpr->pLeft);
2325 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00002326 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002327 break;
2328 }
drhcce7d172000-05-31 15:34:51 +00002329 case TK_ISNULL:
2330 case TK_NOTNULL: {
danielk19774adee202004-05-08 08:23:19 +00002331 sqlite3ExprCode(pParse, pExpr->pLeft);
2332 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00002333 break;
2334 }
drhfef52082000-06-06 01:50:43 +00002335 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00002336 /* The expression is "x BETWEEN y AND z". It is implemented as:
2337 **
2338 ** 1 IF (x >= y) GOTO 3
2339 ** 2 GOTO <dest>
2340 ** 3 IF (x > z) GOTO <dest>
2341 */
drhfef52082000-06-06 01:50:43 +00002342 int addr;
drhbe5c89a2004-07-26 00:31:09 +00002343 Expr *pLeft = pExpr->pLeft;
2344 Expr *pRight = pExpr->pList->a[0].pExpr;
2345 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00002346 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00002347 sqlite3ExprCode(pParse, pRight);
danielk19774adee202004-05-08 08:23:19 +00002348 addr = sqlite3VdbeCurrentAddr(v);
drhbe5c89a2004-07-26 00:31:09 +00002349 codeCompare(pParse, pLeft, pRight, OP_Ge, addr+3, !jumpIfNull);
2350
danielk19774adee202004-05-08 08:23:19 +00002351 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
2352 sqlite3VdbeAddOp(v, OP_Goto, 0, dest);
drhbe5c89a2004-07-26 00:31:09 +00002353 pRight = pExpr->pList->a[1].pExpr;
2354 sqlite3ExprCode(pParse, pRight);
2355 codeCompare(pParse, pLeft, pRight, OP_Gt, dest, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00002356 break;
2357 }
drhcce7d172000-05-31 15:34:51 +00002358 default: {
danielk19774adee202004-05-08 08:23:19 +00002359 sqlite3ExprCode(pParse, pExpr);
2360 sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00002361 break;
2362 }
2363 }
drhffe07b22005-11-03 00:41:17 +00002364 pParse->ckOffset = ckOffset;
drhcce7d172000-05-31 15:34:51 +00002365}
drh22827922000-06-06 17:27:05 +00002366
2367/*
2368** Do a deep comparison of two expression trees. Return TRUE (non-zero)
2369** if they are identical and return FALSE if they differ in any way.
drhd40aab02007-02-24 15:29:03 +00002370**
2371** Sometimes this routine will return FALSE even if the two expressions
2372** really are equivalent. If we cannot prove that the expressions are
2373** identical, we return FALSE just to be safe. So if this routine
2374** returns false, then you do not really know for certain if the two
2375** expressions are the same. But if you get a TRUE return, then you
2376** can be sure the expressions are the same. In the places where
2377** this routine is used, it does not hurt to get an extra FALSE - that
2378** just might result in some slightly slower code. But returning
2379** an incorrect TRUE could lead to a malfunction.
drh22827922000-06-06 17:27:05 +00002380*/
danielk19774adee202004-05-08 08:23:19 +00002381int sqlite3ExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00002382 int i;
danielk19774b202ae2006-01-23 05:50:58 +00002383 if( pA==0||pB==0 ){
2384 return pB==pA;
drh22827922000-06-06 17:27:05 +00002385 }
2386 if( pA->op!=pB->op ) return 0;
drhfd357972005-09-09 01:33:19 +00002387 if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 0;
danielk19774adee202004-05-08 08:23:19 +00002388 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
2389 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00002390 if( pA->pList ){
2391 if( pB->pList==0 ) return 0;
2392 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
2393 for(i=0; i<pA->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00002394 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00002395 return 0;
2396 }
2397 }
2398 }else if( pB->pList ){
2399 return 0;
2400 }
2401 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00002402 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drhdd735212007-02-24 13:53:05 +00002403 if( pA->op!=TK_COLUMN && pA->token.z ){
drh22827922000-06-06 17:27:05 +00002404 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00002405 if( pB->token.n!=pA->token.n ) return 0;
drh2646da72005-12-09 20:02:05 +00002406 if( sqlite3StrNICmp((char*)pA->token.z,(char*)pB->token.z,pB->token.n)!=0 ){
2407 return 0;
2408 }
drh22827922000-06-06 17:27:05 +00002409 }
2410 return 1;
2411}
2412
drh13449892005-09-07 21:22:45 +00002413
drh22827922000-06-06 17:27:05 +00002414/*
drh13449892005-09-07 21:22:45 +00002415** Add a new element to the pAggInfo->aCol[] array. Return the index of
2416** the new element. Return a negative number if malloc fails.
drh22827922000-06-06 17:27:05 +00002417*/
drh17435752007-08-16 04:30:38 +00002418static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){
drh13449892005-09-07 21:22:45 +00002419 int i;
drhcf643722007-03-27 13:36:37 +00002420 pInfo->aCol = sqlite3ArrayAllocate(
drh17435752007-08-16 04:30:38 +00002421 db,
drhcf643722007-03-27 13:36:37 +00002422 pInfo->aCol,
2423 sizeof(pInfo->aCol[0]),
2424 3,
2425 &pInfo->nColumn,
2426 &pInfo->nColumnAlloc,
2427 &i
2428 );
drh13449892005-09-07 21:22:45 +00002429 return i;
2430}
2431
2432/*
2433** Add a new element to the pAggInfo->aFunc[] array. Return the index of
2434** the new element. Return a negative number if malloc fails.
2435*/
drh17435752007-08-16 04:30:38 +00002436static int addAggInfoFunc(sqlite3 *db, AggInfo *pInfo){
drh13449892005-09-07 21:22:45 +00002437 int i;
drhcf643722007-03-27 13:36:37 +00002438 pInfo->aFunc = sqlite3ArrayAllocate(
drh17435752007-08-16 04:30:38 +00002439 db,
drhcf643722007-03-27 13:36:37 +00002440 pInfo->aFunc,
2441 sizeof(pInfo->aFunc[0]),
2442 3,
2443 &pInfo->nFunc,
2444 &pInfo->nFuncAlloc,
2445 &i
2446 );
drh13449892005-09-07 21:22:45 +00002447 return i;
2448}
drh22827922000-06-06 17:27:05 +00002449
2450/*
drh626a8792005-01-17 22:08:19 +00002451** This is an xFunc for walkExprTree() used to implement
2452** sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates
2453** for additional information.
drh22827922000-06-06 17:27:05 +00002454**
drh626a8792005-01-17 22:08:19 +00002455** This routine analyzes the aggregate function at pExpr.
drh22827922000-06-06 17:27:05 +00002456*/
drh626a8792005-01-17 22:08:19 +00002457static int analyzeAggregate(void *pArg, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00002458 int i;
danielk1977a58fdfb2005-02-08 07:50:40 +00002459 NameContext *pNC = (NameContext *)pArg;
2460 Parse *pParse = pNC->pParse;
2461 SrcList *pSrcList = pNC->pSrcList;
drh13449892005-09-07 21:22:45 +00002462 AggInfo *pAggInfo = pNC->pAggInfo;
drh22827922000-06-06 17:27:05 +00002463
drh22827922000-06-06 17:27:05 +00002464 switch( pExpr->op ){
drh89c69d02007-01-04 01:20:28 +00002465 case TK_AGG_COLUMN:
drh967e8b72000-06-21 13:59:10 +00002466 case TK_COLUMN: {
drh13449892005-09-07 21:22:45 +00002467 /* Check to see if the column is in one of the tables in the FROM
2468 ** clause of the aggregate query */
2469 if( pSrcList ){
2470 struct SrcList_item *pItem = pSrcList->a;
2471 for(i=0; i<pSrcList->nSrc; i++, pItem++){
2472 struct AggInfo_col *pCol;
2473 if( pExpr->iTable==pItem->iCursor ){
2474 /* If we reach this point, it means that pExpr refers to a table
2475 ** that is in the FROM clause of the aggregate query.
2476 **
2477 ** Make an entry for the column in pAggInfo->aCol[] if there
2478 ** is not an entry there already.
2479 */
drh7f906d62007-03-12 23:48:52 +00002480 int k;
drh13449892005-09-07 21:22:45 +00002481 pCol = pAggInfo->aCol;
drh7f906d62007-03-12 23:48:52 +00002482 for(k=0; k<pAggInfo->nColumn; k++, pCol++){
drh13449892005-09-07 21:22:45 +00002483 if( pCol->iTable==pExpr->iTable &&
2484 pCol->iColumn==pExpr->iColumn ){
2485 break;
2486 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002487 }
danielk19771e536952007-08-16 10:09:01 +00002488 if( (k>=pAggInfo->nColumn)
2489 && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0
2490 ){
drh7f906d62007-03-12 23:48:52 +00002491 pCol = &pAggInfo->aCol[k];
danielk19770817d0d2007-02-14 09:19:36 +00002492 pCol->pTab = pExpr->pTab;
drh13449892005-09-07 21:22:45 +00002493 pCol->iTable = pExpr->iTable;
2494 pCol->iColumn = pExpr->iColumn;
2495 pCol->iMem = pParse->nMem++;
2496 pCol->iSorterColumn = -1;
drh5774b802005-09-07 22:48:16 +00002497 pCol->pExpr = pExpr;
drh13449892005-09-07 21:22:45 +00002498 if( pAggInfo->pGroupBy ){
2499 int j, n;
2500 ExprList *pGB = pAggInfo->pGroupBy;
2501 struct ExprList_item *pTerm = pGB->a;
2502 n = pGB->nExpr;
2503 for(j=0; j<n; j++, pTerm++){
2504 Expr *pE = pTerm->pExpr;
2505 if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable &&
2506 pE->iColumn==pExpr->iColumn ){
2507 pCol->iSorterColumn = j;
2508 break;
2509 }
2510 }
2511 }
2512 if( pCol->iSorterColumn<0 ){
2513 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
2514 }
2515 }
2516 /* There is now an entry for pExpr in pAggInfo->aCol[] (either
2517 ** because it was there before or because we just created it).
2518 ** Convert the pExpr to be a TK_AGG_COLUMN referring to that
2519 ** pAggInfo->aCol[] entry.
2520 */
2521 pExpr->pAggInfo = pAggInfo;
2522 pExpr->op = TK_AGG_COLUMN;
drh7f906d62007-03-12 23:48:52 +00002523 pExpr->iAgg = k;
drh13449892005-09-07 21:22:45 +00002524 break;
2525 } /* endif pExpr->iTable==pItem->iCursor */
2526 } /* end loop over pSrcList */
drh22827922000-06-06 17:27:05 +00002527 }
drh626a8792005-01-17 22:08:19 +00002528 return 1;
drh22827922000-06-06 17:27:05 +00002529 }
2530 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00002531 /* The pNC->nDepth==0 test causes aggregate functions in subqueries
2532 ** to be ignored */
danielk1977a58fdfb2005-02-08 07:50:40 +00002533 if( pNC->nDepth==0 ){
drh13449892005-09-07 21:22:45 +00002534 /* Check to see if pExpr is a duplicate of another aggregate
2535 ** function that is already in the pAggInfo structure
2536 */
2537 struct AggInfo_func *pItem = pAggInfo->aFunc;
2538 for(i=0; i<pAggInfo->nFunc; i++, pItem++){
2539 if( sqlite3ExprCompare(pItem->pExpr, pExpr) ){
danielk1977a58fdfb2005-02-08 07:50:40 +00002540 break;
2541 }
drh22827922000-06-06 17:27:05 +00002542 }
drh13449892005-09-07 21:22:45 +00002543 if( i>=pAggInfo->nFunc ){
2544 /* pExpr is original. Make a new entry in pAggInfo->aFunc[]
2545 */
danielk197714db2662006-01-09 16:12:04 +00002546 u8 enc = ENC(pParse->db);
danielk19771e536952007-08-16 10:09:01 +00002547 i = addAggInfoFunc(pParse->db, pAggInfo);
drh13449892005-09-07 21:22:45 +00002548 if( i>=0 ){
2549 pItem = &pAggInfo->aFunc[i];
2550 pItem->pExpr = pExpr;
2551 pItem->iMem = pParse->nMem++;
2552 pItem->pFunc = sqlite3FindFunction(pParse->db,
drh2646da72005-12-09 20:02:05 +00002553 (char*)pExpr->token.z, pExpr->token.n,
drh13449892005-09-07 21:22:45 +00002554 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
drhfd357972005-09-09 01:33:19 +00002555 if( pExpr->flags & EP_Distinct ){
2556 pItem->iDistinct = pParse->nTab++;
2557 }else{
2558 pItem->iDistinct = -1;
2559 }
drh13449892005-09-07 21:22:45 +00002560 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002561 }
drh13449892005-09-07 21:22:45 +00002562 /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
2563 */
danielk1977a58fdfb2005-02-08 07:50:40 +00002564 pExpr->iAgg = i;
drh13449892005-09-07 21:22:45 +00002565 pExpr->pAggInfo = pAggInfo;
danielk1977a58fdfb2005-02-08 07:50:40 +00002566 return 1;
drh22827922000-06-06 17:27:05 +00002567 }
drh22827922000-06-06 17:27:05 +00002568 }
2569 }
drh13449892005-09-07 21:22:45 +00002570
2571 /* Recursively walk subqueries looking for TK_COLUMN nodes that need
2572 ** to be changed to TK_AGG_COLUMN. But increment nDepth so that
2573 ** TK_AGG_FUNCTION nodes in subqueries will be unchanged.
2574 */
danielk1977a58fdfb2005-02-08 07:50:40 +00002575 if( pExpr->pSelect ){
2576 pNC->nDepth++;
2577 walkSelectExpr(pExpr->pSelect, analyzeAggregate, pNC);
2578 pNC->nDepth--;
2579 }
drh626a8792005-01-17 22:08:19 +00002580 return 0;
2581}
2582
2583/*
2584** Analyze the given expression looking for aggregate functions and
2585** for variables that need to be added to the pParse->aAgg[] array.
2586** Make additional entries to the pParse->aAgg[] array as necessary.
2587**
2588** This routine should only be called after the expression has been
2589** analyzed by sqlite3ExprResolveNames().
2590**
2591** If errors are seen, leave an error message in zErrMsg and return
2592** the number of errors.
2593*/
danielk1977a58fdfb2005-02-08 07:50:40 +00002594int sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
2595 int nErr = pNC->pParse->nErr;
2596 walkExprTree(pExpr, analyzeAggregate, pNC);
2597 return pNC->pParse->nErr - nErr;
drh22827922000-06-06 17:27:05 +00002598}
drh5d9a4af2005-08-30 00:54:01 +00002599
2600/*
2601** Call sqlite3ExprAnalyzeAggregates() for every expression in an
2602** expression list. Return the number of errors.
2603**
2604** If an error is found, the analysis is cut short.
2605*/
2606int sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){
2607 struct ExprList_item *pItem;
2608 int i;
2609 int nErr = 0;
2610 if( pList ){
2611 for(pItem=pList->a, i=0; nErr==0 && i<pList->nExpr; i++, pItem++){
2612 nErr += sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
2613 }
2614 }
2615 return nErr;
2616}