blob: 4f3d52779831dce35bb6ec4909b609cef51cd29e [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**
drh0cf19ed2007-10-23 18:55:48 +000015** $Id: expr.c,v 1.315 2007/10/23 18:55:49 drh Exp $
drhcce7d172000-05-31 15:34:51 +000016*/
17#include "sqliteInt.h"
drh04738cb2002-06-02 18:19:00 +000018#include <ctype.h>
drha2e00042002-01-22 03:13:42 +000019
danielk1977e014a832004-05-17 10:48:57 +000020/*
21** Return the 'affinity' of the expression pExpr if any.
22**
23** If pExpr is a column, a reference to a column via an 'AS' alias,
24** or a sub-select with a column as the return value, then the
25** affinity of that column is returned. Otherwise, 0x00 is returned,
26** indicating no affinity for the expression.
27**
28** i.e. the WHERE clause expresssions in the following statements all
29** have an affinity:
30**
31** CREATE TABLE t1(a);
32** SELECT * FROM t1 WHERE a;
33** SELECT a AS b FROM t1 WHERE b;
34** SELECT * FROM t1 WHERE (select a from t1);
35*/
danielk1977bf3b7212004-05-18 10:06:24 +000036char sqlite3ExprAffinity(Expr *pExpr){
drh487e2622005-06-25 18:42:14 +000037 int op = pExpr->op;
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 */
drh728b5772007-09-18 15:55:07 +00001017 Schema *pSchema = 0; /* Schema of the expression */
drh8141f612004-01-25 22:44:58 +00001018
1019 assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
drh17435752007-08-16 04:30:38 +00001020 zDb = sqlite3NameFromToken(db, pDbToken);
1021 zTab = sqlite3NameFromToken(db, pTableToken);
1022 zCol = sqlite3NameFromToken(db, pColumnToken);
1023 if( db->mallocFailed ){
danielk1977d5d56522005-03-16 12:15:20 +00001024 goto lookupname_end;
drh8141f612004-01-25 22:44:58 +00001025 }
drh8141f612004-01-25 22:44:58 +00001026
1027 pExpr->iTable = -1;
drh626a8792005-01-17 22:08:19 +00001028 while( pNC && cnt==0 ){
drhffe07b22005-11-03 00:41:17 +00001029 ExprList *pEList;
drh626a8792005-01-17 22:08:19 +00001030 SrcList *pSrcList = pNC->pSrcList;
drh8141f612004-01-25 22:44:58 +00001031
danielk1977b3bce662005-01-29 08:32:43 +00001032 if( pSrcList ){
1033 for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
drh43617e92006-03-06 20:55:46 +00001034 Table *pTab;
1035 int iDb;
danielk1977b3bce662005-01-29 08:32:43 +00001036 Column *pCol;
1037
drh43617e92006-03-06 20:55:46 +00001038 pTab = pItem->pTab;
1039 assert( pTab!=0 );
1040 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
danielk1977b3bce662005-01-29 08:32:43 +00001041 assert( pTab->nCol>0 );
1042 if( zTab ){
1043 if( pItem->zAlias ){
1044 char *zTabName = pItem->zAlias;
1045 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
1046 }else{
1047 char *zTabName = pTab->zName;
1048 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
danielk1977da184232006-01-05 11:34:32 +00001049 if( zDb!=0 && sqlite3StrICmp(db->aDb[iDb].zName, zDb)!=0 ){
danielk1977b3bce662005-01-29 08:32:43 +00001050 continue;
1051 }
drh626a8792005-01-17 22:08:19 +00001052 }
drh8141f612004-01-25 22:44:58 +00001053 }
danielk1977b3bce662005-01-29 08:32:43 +00001054 if( 0==(cntTab++) ){
drh626a8792005-01-17 22:08:19 +00001055 pExpr->iTable = pItem->iCursor;
drh728b5772007-09-18 15:55:07 +00001056 pSchema = pTab->pSchema;
danielk1977b3bce662005-01-29 08:32:43 +00001057 pMatch = pItem;
1058 }
1059 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
1060 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
danielk1977b3bf5562006-01-10 17:58:23 +00001061 const char *zColl = pTab->aCol[j].zColl;
drh873fac02005-06-06 17:11:46 +00001062 IdList *pUsing;
danielk1977b3bce662005-01-29 08:32:43 +00001063 cnt++;
1064 pExpr->iTable = pItem->iCursor;
1065 pMatch = pItem;
drh728b5772007-09-18 15:55:07 +00001066 pSchema = pTab->pSchema;
danielk1977b3bce662005-01-29 08:32:43 +00001067 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
1068 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
1069 pExpr->affinity = pTab->aCol[j].affinity;
drh8b4c40d2007-02-01 23:02:45 +00001070 if( (pExpr->flags & EP_ExpCollate)==0 ){
1071 pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0);
1072 }
drh61dfc312006-12-16 16:25:15 +00001073 if( i<pSrcList->nSrc-1 ){
1074 if( pItem[1].jointype & JT_NATURAL ){
1075 /* If this match occurred in the left table of a natural join,
1076 ** then skip the right table to avoid a duplicate match */
1077 pItem++;
1078 i++;
1079 }else if( (pUsing = pItem[1].pUsing)!=0 ){
1080 /* If this match occurs on a column that is in the USING clause
1081 ** of a join, skip the search of the right table of the join
1082 ** to avoid a duplicate match there. */
1083 int k;
1084 for(k=0; k<pUsing->nId; k++){
1085 if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ){
1086 pItem++;
1087 i++;
1088 break;
1089 }
drh873fac02005-06-06 17:11:46 +00001090 }
1091 }
1092 }
danielk1977b3bce662005-01-29 08:32:43 +00001093 break;
1094 }
drh8141f612004-01-25 22:44:58 +00001095 }
1096 }
1097 }
drh626a8792005-01-17 22:08:19 +00001098
1099#ifndef SQLITE_OMIT_TRIGGER
1100 /* If we have not already resolved the name, then maybe
1101 ** it is a new.* or old.* trigger argument reference
1102 */
1103 if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
1104 TriggerStack *pTriggerStack = pParse->trigStack;
1105 Table *pTab = 0;
1106 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){
1107 pExpr->iTable = pTriggerStack->newIdx;
1108 assert( pTriggerStack->pTab );
1109 pTab = pTriggerStack->pTab;
1110 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab)==0 ){
1111 pExpr->iTable = pTriggerStack->oldIdx;
1112 assert( pTriggerStack->pTab );
1113 pTab = pTriggerStack->pTab;
1114 }
1115
1116 if( pTab ){
danielk1977f0113002006-01-24 12:09:17 +00001117 int iCol;
drh626a8792005-01-17 22:08:19 +00001118 Column *pCol = pTab->aCol;
1119
drh728b5772007-09-18 15:55:07 +00001120 pSchema = pTab->pSchema;
drh626a8792005-01-17 22:08:19 +00001121 cntTab++;
danielk1977f0113002006-01-24 12:09:17 +00001122 for(iCol=0; iCol < pTab->nCol; iCol++, pCol++) {
drh626a8792005-01-17 22:08:19 +00001123 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
danielk1977f0113002006-01-24 12:09:17 +00001124 const char *zColl = pTab->aCol[iCol].zColl;
drh626a8792005-01-17 22:08:19 +00001125 cnt++;
danielk1977f0113002006-01-24 12:09:17 +00001126 pExpr->iColumn = iCol==pTab->iPKey ? -1 : iCol;
1127 pExpr->affinity = pTab->aCol[iCol].affinity;
drh8b4c40d2007-02-01 23:02:45 +00001128 if( (pExpr->flags & EP_ExpCollate)==0 ){
1129 pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0);
1130 }
danielk1977aee18ef2005-03-09 12:26:50 +00001131 pExpr->pTab = pTab;
drh626a8792005-01-17 22:08:19 +00001132 break;
1133 }
1134 }
1135 }
1136 }
drhb7f91642004-10-31 02:22:47 +00001137#endif /* !defined(SQLITE_OMIT_TRIGGER) */
drh8141f612004-01-25 22:44:58 +00001138
drh626a8792005-01-17 22:08:19 +00001139 /*
1140 ** Perhaps the name is a reference to the ROWID
1141 */
1142 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
1143 cnt = 1;
1144 pExpr->iColumn = -1;
drh8a512562005-11-14 22:29:05 +00001145 pExpr->affinity = SQLITE_AFF_INTEGER;
drh626a8792005-01-17 22:08:19 +00001146 }
drh8141f612004-01-25 22:44:58 +00001147
drh626a8792005-01-17 22:08:19 +00001148 /*
1149 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
1150 ** might refer to an result-set alias. This happens, for example, when
1151 ** we are resolving names in the WHERE clause of the following command:
1152 **
1153 ** SELECT a+b AS x FROM table WHERE x<10;
1154 **
1155 ** In cases like this, replace pExpr with a copy of the expression that
1156 ** forms the result set entry ("a+b" in the example) and return immediately.
1157 ** Note that the expression in the result set should have already been
1158 ** resolved by the time the WHERE clause is resolved.
1159 */
drhffe07b22005-11-03 00:41:17 +00001160 if( cnt==0 && (pEList = pNC->pEList)!=0 && zTab==0 ){
drh626a8792005-01-17 22:08:19 +00001161 for(j=0; j<pEList->nExpr; j++){
1162 char *zAs = pEList->a[j].zName;
1163 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
drh36379e92007-07-23 22:51:15 +00001164 Expr *pDup, *pOrig;
drh626a8792005-01-17 22:08:19 +00001165 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
drh4f07e5f2007-05-14 11:34:46 +00001166 assert( pExpr->pList==0 );
1167 assert( pExpr->pSelect==0 );
drh36379e92007-07-23 22:51:15 +00001168 pOrig = pEList->a[j].pExpr;
1169 if( !pNC->allowAgg && ExprHasProperty(pOrig, EP_Agg) ){
1170 sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs);
drh17435752007-08-16 04:30:38 +00001171 sqlite3_free(zCol);
drh36379e92007-07-23 22:51:15 +00001172 return 2;
1173 }
danielk19771e536952007-08-16 10:09:01 +00001174 pDup = sqlite3ExprDup(db, pOrig);
drh4f07e5f2007-05-14 11:34:46 +00001175 if( pExpr->flags & EP_ExpCollate ){
1176 pDup->pColl = pExpr->pColl;
1177 pDup->flags |= EP_ExpCollate;
1178 }
drh17435752007-08-16 04:30:38 +00001179 if( pExpr->span.dyn ) sqlite3_free((char*)pExpr->span.z);
1180 if( pExpr->token.dyn ) sqlite3_free((char*)pExpr->token.z);
drh4f07e5f2007-05-14 11:34:46 +00001181 memcpy(pExpr, pDup, sizeof(*pExpr));
drh17435752007-08-16 04:30:38 +00001182 sqlite3_free(pDup);
drh15ccce12005-05-23 15:06:39 +00001183 cnt = 1;
danielk1977c9cf6e32007-06-25 16:29:33 +00001184 pMatch = 0;
drh626a8792005-01-17 22:08:19 +00001185 assert( zTab==0 && zDb==0 );
drh15ccce12005-05-23 15:06:39 +00001186 goto lookupname_end_2;
drh626a8792005-01-17 22:08:19 +00001187 }
1188 }
1189 }
1190
1191 /* Advance to the next name context. The loop will exit when either
1192 ** we have a match (cnt>0) or when we run out of name contexts.
1193 */
1194 if( cnt==0 ){
1195 pNC = pNC->pNext;
1196 }
drh8141f612004-01-25 22:44:58 +00001197 }
1198
1199 /*
1200 ** If X and Y are NULL (in other words if only the column name Z is
1201 ** supplied) and the value of Z is enclosed in double-quotes, then
1202 ** Z is a string literal if it doesn't match any column names. In that
1203 ** case, we need to return right away and not make any changes to
1204 ** pExpr.
drh15ccce12005-05-23 15:06:39 +00001205 **
1206 ** Because no reference was made to outer contexts, the pNC->nRef
1207 ** fields are not changed in any context.
drh8141f612004-01-25 22:44:58 +00001208 */
1209 if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
drh17435752007-08-16 04:30:38 +00001210 sqlite3_free(zCol);
drh8141f612004-01-25 22:44:58 +00001211 return 0;
1212 }
1213
1214 /*
1215 ** cnt==0 means there was not match. cnt>1 means there were two or
1216 ** more matches. Either way, we have an error.
1217 */
1218 if( cnt!=1 ){
1219 char *z = 0;
1220 char *zErr;
1221 zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s";
1222 if( zDb ){
drhf93339d2006-01-03 15:16:26 +00001223 sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, (char*)0);
drh8141f612004-01-25 22:44:58 +00001224 }else if( zTab ){
drhf93339d2006-01-03 15:16:26 +00001225 sqlite3SetString(&z, zTab, ".", zCol, (char*)0);
drh8141f612004-01-25 22:44:58 +00001226 }else{
drh17435752007-08-16 04:30:38 +00001227 z = sqlite3StrDup(zCol);
drh8141f612004-01-25 22:44:58 +00001228 }
danielk1977a1644fd2007-08-29 12:31:25 +00001229 if( z ){
1230 sqlite3ErrorMsg(pParse, zErr, z);
1231 sqlite3_free(z);
1232 pTopNC->nErr++;
1233 }else{
1234 db->mallocFailed = 1;
1235 }
drh8141f612004-01-25 22:44:58 +00001236 }
1237
drh51669862004-12-18 18:40:26 +00001238 /* If a column from a table in pSrcList is referenced, then record
1239 ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes
1240 ** bit 0 to be set. Column 1 sets bit 1. And so forth. If the
1241 ** column number is greater than the number of bits in the bitmask
1242 ** then set the high-order bit of the bitmask.
1243 */
1244 if( pExpr->iColumn>=0 && pMatch!=0 ){
1245 int n = pExpr->iColumn;
1246 if( n>=sizeof(Bitmask)*8 ){
1247 n = sizeof(Bitmask)*8-1;
1248 }
1249 assert( pMatch->iCursor==pExpr->iTable );
drhca83ac52007-02-01 01:40:44 +00001250 pMatch->colUsed |= ((Bitmask)1)<<n;
drh51669862004-12-18 18:40:26 +00001251 }
1252
danielk1977d5d56522005-03-16 12:15:20 +00001253lookupname_end:
drh8141f612004-01-25 22:44:58 +00001254 /* Clean up and return
1255 */
drh17435752007-08-16 04:30:38 +00001256 sqlite3_free(zDb);
1257 sqlite3_free(zTab);
danielk19774adee202004-05-08 08:23:19 +00001258 sqlite3ExprDelete(pExpr->pLeft);
drh8141f612004-01-25 22:44:58 +00001259 pExpr->pLeft = 0;
danielk19774adee202004-05-08 08:23:19 +00001260 sqlite3ExprDelete(pExpr->pRight);
drh8141f612004-01-25 22:44:58 +00001261 pExpr->pRight = 0;
1262 pExpr->op = TK_COLUMN;
drh15ccce12005-05-23 15:06:39 +00001263lookupname_end_2:
drh17435752007-08-16 04:30:38 +00001264 sqlite3_free(zCol);
drh626a8792005-01-17 22:08:19 +00001265 if( cnt==1 ){
danielk1977b3bce662005-01-29 08:32:43 +00001266 assert( pNC!=0 );
drh728b5772007-09-18 15:55:07 +00001267 sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList);
danielk1977aee18ef2005-03-09 12:26:50 +00001268 if( pMatch && !pMatch->pSelect ){
1269 pExpr->pTab = pMatch->pTab;
1270 }
drh15ccce12005-05-23 15:06:39 +00001271 /* Increment the nRef value on all name contexts from TopNC up to
1272 ** the point where the name matched. */
1273 for(;;){
1274 assert( pTopNC!=0 );
1275 pTopNC->nRef++;
1276 if( pTopNC==pNC ) break;
1277 pTopNC = pTopNC->pNext;
1278 }
1279 return 0;
1280 } else {
1281 return 1;
drh626a8792005-01-17 22:08:19 +00001282 }
drh8141f612004-01-25 22:44:58 +00001283}
1284
1285/*
drh626a8792005-01-17 22:08:19 +00001286** This routine is designed as an xFunc for walkExprTree().
1287**
drh73b211a2005-01-18 04:00:42 +00001288** Resolve symbolic names into TK_COLUMN operators for the current
drh626a8792005-01-17 22:08:19 +00001289** node in the expression tree. Return 0 to continue the search down
drh73b211a2005-01-18 04:00:42 +00001290** the tree or 2 to abort the tree walk.
1291**
1292** This routine also does error checking and name resolution for
1293** function names. The operator for aggregate functions is changed
1294** to TK_AGG_FUNCTION.
drh626a8792005-01-17 22:08:19 +00001295*/
1296static int nameResolverStep(void *pArg, Expr *pExpr){
1297 NameContext *pNC = (NameContext*)pArg;
drh626a8792005-01-17 22:08:19 +00001298 Parse *pParse;
drh626a8792005-01-17 22:08:19 +00001299
danielk1977b3bce662005-01-29 08:32:43 +00001300 if( pExpr==0 ) return 1;
drh626a8792005-01-17 22:08:19 +00001301 assert( pNC!=0 );
drh626a8792005-01-17 22:08:19 +00001302 pParse = pNC->pParse;
danielk1977b3bce662005-01-29 08:32:43 +00001303
drh626a8792005-01-17 22:08:19 +00001304 if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return 1;
1305 ExprSetProperty(pExpr, EP_Resolved);
1306#ifndef NDEBUG
danielk1977f0113002006-01-24 12:09:17 +00001307 if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
1308 SrcList *pSrcList = pNC->pSrcList;
danielk1977940fac92005-01-23 22:41:37 +00001309 int i;
danielk1977f0113002006-01-24 12:09:17 +00001310 for(i=0; i<pNC->pSrcList->nSrc; i++){
drh626a8792005-01-17 22:08:19 +00001311 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
1312 }
1313 }
1314#endif
1315 switch( pExpr->op ){
1316 /* Double-quoted strings (ex: "abc") are used as identifiers if
1317 ** possible. Otherwise they remain as strings. Single-quoted
1318 ** strings (ex: 'abc') are always string literals.
1319 */
1320 case TK_STRING: {
1321 if( pExpr->token.z[0]=='\'' ) break;
1322 /* Fall thru into the TK_ID case if this is a double-quoted string */
1323 }
1324 /* A lone identifier is the name of a column.
1325 */
1326 case TK_ID: {
drh626a8792005-01-17 22:08:19 +00001327 lookupName(pParse, 0, 0, &pExpr->token, pNC, pExpr);
1328 return 1;
1329 }
1330
1331 /* A table name and column name: ID.ID
1332 ** Or a database, table and column: ID.ID.ID
1333 */
1334 case TK_DOT: {
1335 Token *pColumn;
1336 Token *pTable;
1337 Token *pDb;
1338 Expr *pRight;
1339
danielk1977b3bce662005-01-29 08:32:43 +00001340 /* if( pSrcList==0 ) break; */
drh626a8792005-01-17 22:08:19 +00001341 pRight = pExpr->pRight;
1342 if( pRight->op==TK_ID ){
1343 pDb = 0;
1344 pTable = &pExpr->pLeft->token;
1345 pColumn = &pRight->token;
1346 }else{
1347 assert( pRight->op==TK_DOT );
1348 pDb = &pExpr->pLeft->token;
1349 pTable = &pRight->pLeft->token;
1350 pColumn = &pRight->pRight->token;
1351 }
1352 lookupName(pParse, pDb, pTable, pColumn, pNC, pExpr);
1353 return 1;
1354 }
1355
1356 /* Resolve function names
1357 */
drhb71090f2005-05-23 17:26:51 +00001358 case TK_CONST_FUNC:
drh626a8792005-01-17 22:08:19 +00001359 case TK_FUNCTION: {
1360 ExprList *pList = pExpr->pList; /* The argument list */
1361 int n = pList ? pList->nExpr : 0; /* Number of arguments */
1362 int no_such_func = 0; /* True if no such function exists */
1363 int wrong_num_args = 0; /* True if wrong number of arguments */
1364 int is_agg = 0; /* True if is an aggregate function */
1365 int i;
drh5169bbc2006-08-24 14:59:45 +00001366 int auth; /* Authorization to use the function */
drh626a8792005-01-17 22:08:19 +00001367 int nId; /* Number of characters in function name */
1368 const char *zId; /* The function name. */
drh73b211a2005-01-18 04:00:42 +00001369 FuncDef *pDef; /* Information about the function */
danielk197714db2662006-01-09 16:12:04 +00001370 int enc = ENC(pParse->db); /* The database encoding */
drh626a8792005-01-17 22:08:19 +00001371
drh2646da72005-12-09 20:02:05 +00001372 zId = (char*)pExpr->token.z;
drhb71090f2005-05-23 17:26:51 +00001373 nId = pExpr->token.n;
drh626a8792005-01-17 22:08:19 +00001374 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
1375 if( pDef==0 ){
1376 pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
1377 if( pDef==0 ){
1378 no_such_func = 1;
1379 }else{
1380 wrong_num_args = 1;
1381 }
1382 }else{
1383 is_agg = pDef->xFunc==0;
1384 }
drh2fca7fe2006-11-23 11:59:13 +00001385#ifndef SQLITE_OMIT_AUTHORIZATION
drh5169bbc2006-08-24 14:59:45 +00001386 if( pDef ){
1387 auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0);
1388 if( auth!=SQLITE_OK ){
1389 if( auth==SQLITE_DENY ){
1390 sqlite3ErrorMsg(pParse, "not authorized to use function: %s",
1391 pDef->zName);
1392 pNC->nErr++;
1393 }
1394 pExpr->op = TK_NULL;
1395 return 1;
1396 }
1397 }
drhb8b14212006-08-24 15:18:25 +00001398#endif
drh626a8792005-01-17 22:08:19 +00001399 if( is_agg && !pNC->allowAgg ){
1400 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
1401 pNC->nErr++;
1402 is_agg = 0;
1403 }else if( no_such_func ){
1404 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
1405 pNC->nErr++;
1406 }else if( wrong_num_args ){
1407 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
1408 nId, zId);
1409 pNC->nErr++;
1410 }
1411 if( is_agg ){
1412 pExpr->op = TK_AGG_FUNCTION;
1413 pNC->hasAgg = 1;
1414 }
drh73b211a2005-01-18 04:00:42 +00001415 if( is_agg ) pNC->allowAgg = 0;
drh626a8792005-01-17 22:08:19 +00001416 for(i=0; pNC->nErr==0 && i<n; i++){
drh73b211a2005-01-18 04:00:42 +00001417 walkExprTree(pList->a[i].pExpr, nameResolverStep, pNC);
drh626a8792005-01-17 22:08:19 +00001418 }
drh73b211a2005-01-18 04:00:42 +00001419 if( is_agg ) pNC->allowAgg = 1;
drh626a8792005-01-17 22:08:19 +00001420 /* FIX ME: Compute pExpr->affinity based on the expected return
1421 ** type of the function
1422 */
1423 return is_agg;
1424 }
danielk1977b3bce662005-01-29 08:32:43 +00001425#ifndef SQLITE_OMIT_SUBQUERY
1426 case TK_SELECT:
1427 case TK_EXISTS:
1428#endif
1429 case TK_IN: {
1430 if( pExpr->pSelect ){
drh8a9f38f2005-11-05 15:07:55 +00001431 int nRef = pNC->nRef;
drh06f65412005-11-03 02:03:13 +00001432#ifndef SQLITE_OMIT_CHECK
1433 if( pNC->isCheck ){
1434 sqlite3ErrorMsg(pParse,"subqueries prohibited in CHECK constraints");
1435 }
1436#endif
danielk1977b3bce662005-01-29 08:32:43 +00001437 sqlite3SelectResolve(pParse, pExpr->pSelect, pNC);
1438 assert( pNC->nRef>=nRef );
1439 if( nRef!=pNC->nRef ){
1440 ExprSetProperty(pExpr, EP_VarSelect);
1441 }
1442 }
drh4284fb02005-11-03 12:33:28 +00001443 break;
danielk1977b3bce662005-01-29 08:32:43 +00001444 }
drh4284fb02005-11-03 12:33:28 +00001445#ifndef SQLITE_OMIT_CHECK
1446 case TK_VARIABLE: {
1447 if( pNC->isCheck ){
1448 sqlite3ErrorMsg(pParse,"parameters prohibited in CHECK constraints");
1449 }
1450 break;
1451 }
1452#endif
drh626a8792005-01-17 22:08:19 +00001453 }
1454 return 0;
1455}
1456
1457/*
drhcce7d172000-05-31 15:34:51 +00001458** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +00001459** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +00001460** index to the table in the table list and a column offset. The
1461** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
1462** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +00001463** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +00001464** VDBE cursor number for a cursor that is pointing into the referenced
1465** table. The Expr.iColumn value is changed to the index of the column
1466** of the referenced table. The Expr.iColumn value for the special
1467** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
1468** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +00001469**
drh626a8792005-01-17 22:08:19 +00001470** Also resolve function names and check the functions for proper
1471** usage. Make sure all function names are recognized and all functions
1472** have the correct number of arguments. Leave an error message
1473** in pParse->zErrMsg if anything is amiss. Return the number of errors.
1474**
drh73b211a2005-01-18 04:00:42 +00001475** If the expression contains aggregate functions then set the EP_Agg
1476** property on the expression.
drh626a8792005-01-17 22:08:19 +00001477*/
danielk1977fc976062007-05-10 10:46:56 +00001478int sqlite3ExprResolveNames(
danielk1977b3bce662005-01-29 08:32:43 +00001479 NameContext *pNC, /* Namespace to resolve expressions in. */
1480 Expr *pExpr /* The expression to be analyzed. */
drh626a8792005-01-17 22:08:19 +00001481){
drh13449892005-09-07 21:22:45 +00001482 int savedHasAgg;
drh73b211a2005-01-18 04:00:42 +00001483 if( pExpr==0 ) return 0;
danielk1977e6a58a42007-08-31 17:42:48 +00001484#if defined(SQLITE_TEST) || SQLITE_MAX_EXPR_DEPTH>0
danielk1977fc976062007-05-10 10:46:56 +00001485 if( (pExpr->nHeight+pNC->pParse->nHeight)>SQLITE_MAX_EXPR_DEPTH ){
1486 sqlite3ErrorMsg(pNC->pParse,
1487 "Expression tree is too large (maximum depth %d)",
1488 SQLITE_MAX_EXPR_DEPTH
1489 );
1490 return 1;
1491 }
1492 pNC->pParse->nHeight += pExpr->nHeight;
1493#endif
drh13449892005-09-07 21:22:45 +00001494 savedHasAgg = pNC->hasAgg;
1495 pNC->hasAgg = 0;
danielk1977b3bce662005-01-29 08:32:43 +00001496 walkExprTree(pExpr, nameResolverStep, pNC);
danielk1977e6a58a42007-08-31 17:42:48 +00001497#if defined(SQLITE_TEST) || SQLITE_MAX_EXPR_DEPTH>0
danielk1977fc976062007-05-10 10:46:56 +00001498 pNC->pParse->nHeight -= pExpr->nHeight;
1499#endif
danielk1977b3bce662005-01-29 08:32:43 +00001500 if( pNC->nErr>0 ){
drh73b211a2005-01-18 04:00:42 +00001501 ExprSetProperty(pExpr, EP_Error);
drh73b211a2005-01-18 04:00:42 +00001502 }
drh13449892005-09-07 21:22:45 +00001503 if( pNC->hasAgg ){
1504 ExprSetProperty(pExpr, EP_Agg);
1505 }else if( savedHasAgg ){
1506 pNC->hasAgg = 1;
1507 }
drh73b211a2005-01-18 04:00:42 +00001508 return ExprHasProperty(pExpr, EP_Error);
drh626a8792005-01-17 22:08:19 +00001509}
1510
drh1398ad32005-01-19 23:24:50 +00001511/*
1512** A pointer instance of this structure is used to pass information
1513** through walkExprTree into codeSubqueryStep().
1514*/
1515typedef struct QueryCoder QueryCoder;
1516struct QueryCoder {
1517 Parse *pParse; /* The parsing context */
1518 NameContext *pNC; /* Namespace of first enclosing query */
1519};
1520
drh626a8792005-01-17 22:08:19 +00001521
1522/*
drh9cbe6352005-11-29 03:13:21 +00001523** Generate code for scalar subqueries used as an expression
1524** and IN operators. Examples:
drh626a8792005-01-17 22:08:19 +00001525**
drh9cbe6352005-11-29 03:13:21 +00001526** (SELECT a FROM b) -- subquery
1527** EXISTS (SELECT a FROM b) -- EXISTS subquery
1528** x IN (4,5,11) -- IN operator with list on right-hand side
1529** x IN (SELECT a FROM b) -- IN operator with subquery on the right
drhfef52082000-06-06 01:50:43 +00001530**
drh9cbe6352005-11-29 03:13:21 +00001531** The pExpr parameter describes the expression that contains the IN
1532** operator or subquery.
drhcce7d172000-05-31 15:34:51 +00001533*/
drh51522cd2005-01-20 13:36:19 +00001534#ifndef SQLITE_OMIT_SUBQUERY
danielk1977b3bce662005-01-29 08:32:43 +00001535void sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){
drh57dbd7b2005-07-08 18:25:26 +00001536 int testAddr = 0; /* One-time test address */
danielk1977b3bce662005-01-29 08:32:43 +00001537 Vdbe *v = sqlite3GetVdbe(pParse);
1538 if( v==0 ) return;
1539
danielk1977fc976062007-05-10 10:46:56 +00001540
drh57dbd7b2005-07-08 18:25:26 +00001541 /* This code must be run in its entirety every time it is encountered
1542 ** if any of the following is true:
1543 **
1544 ** * The right-hand side is a correlated subquery
1545 ** * The right-hand side is an expression list containing variables
1546 ** * We are inside a trigger
1547 **
1548 ** If all of the above are false, then we can run this code just once
1549 ** save the results, and reuse the same result on subsequent invocations.
danielk1977b3bce662005-01-29 08:32:43 +00001550 */
1551 if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->trigStack ){
1552 int mem = pParse->nMem++;
1553 sqlite3VdbeAddOp(v, OP_MemLoad, mem, 0);
drh57dbd7b2005-07-08 18:25:26 +00001554 testAddr = sqlite3VdbeAddOp(v, OP_If, 0, 0);
drh17435752007-08-16 04:30:38 +00001555 assert( testAddr>0 || pParse->db->mallocFailed );
drhd654be82005-09-20 17:42:23 +00001556 sqlite3VdbeAddOp(v, OP_MemInt, 1, mem);
danielk1977b3bce662005-01-29 08:32:43 +00001557 }
1558
drhcce7d172000-05-31 15:34:51 +00001559 switch( pExpr->op ){
drhfef52082000-06-06 01:50:43 +00001560 case TK_IN: {
danielk1977e014a832004-05-17 10:48:57 +00001561 char affinity;
drhd3d39e92004-05-20 22:16:29 +00001562 KeyInfo keyInfo;
drhb9bb7c12006-06-11 23:41:55 +00001563 int addr; /* Address of OP_OpenEphemeral instruction */
drhd3d39e92004-05-20 22:16:29 +00001564
danielk1977bf3b7212004-05-18 10:06:24 +00001565 affinity = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +00001566
1567 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
drh57dbd7b2005-07-08 18:25:26 +00001568 ** expression it is handled the same way. A virtual table is
danielk1977e014a832004-05-17 10:48:57 +00001569 ** filled with single-field index keys representing the results
1570 ** from the SELECT or the <exprlist>.
1571 **
1572 ** If the 'x' expression is a column value, or the SELECT...
1573 ** statement returns a column value, then the affinity of that
1574 ** column is used to build the index keys. If both 'x' and the
1575 ** SELECT... statement are columns, then numeric affinity is used
1576 ** if either column has NUMERIC or INTEGER affinity. If neither
1577 ** 'x' nor the SELECT... statement are columns, then numeric affinity
1578 ** is used.
1579 */
1580 pExpr->iTable = pParse->nTab++;
drhb9bb7c12006-06-11 23:41:55 +00001581 addr = sqlite3VdbeAddOp(v, OP_OpenEphemeral, pExpr->iTable, 0);
drhd3d39e92004-05-20 22:16:29 +00001582 memset(&keyInfo, 0, sizeof(keyInfo));
1583 keyInfo.nField = 1;
drhf3218fe2004-05-28 08:21:02 +00001584 sqlite3VdbeAddOp(v, OP_SetNumColumns, pExpr->iTable, 1);
danielk1977e014a832004-05-17 10:48:57 +00001585
drhfef52082000-06-06 01:50:43 +00001586 if( pExpr->pSelect ){
1587 /* Case 1: expr IN (SELECT ...)
1588 **
danielk1977e014a832004-05-17 10:48:57 +00001589 ** Generate code to write the results of the select into the temporary
1590 ** table allocated and opened above.
drhfef52082000-06-06 01:50:43 +00001591 */
danielk1977e014a832004-05-17 10:48:57 +00001592 int iParm = pExpr->iTable + (((int)affinity)<<16);
drhbe5c89a2004-07-26 00:31:09 +00001593 ExprList *pEList;
danielk1977e014a832004-05-17 10:48:57 +00001594 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
drh94ccde52007-04-13 16:06:32 +00001595 if( sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0) ){
1596 return;
1597 }
drhbe5c89a2004-07-26 00:31:09 +00001598 pEList = pExpr->pSelect->pEList;
1599 if( pEList && pEList->nExpr>0 ){
danielk1977bcbb04e2007-05-29 12:11:29 +00001600 keyInfo.aColl[0] = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft,
drhbe5c89a2004-07-26 00:31:09 +00001601 pEList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001602 }
drhfef52082000-06-06 01:50:43 +00001603 }else if( pExpr->pList ){
1604 /* Case 2: expr IN (exprlist)
1605 **
drhfd131da2007-08-07 17:13:03 +00001606 ** For each expression, build an index key from the evaluation and
danielk1977e014a832004-05-17 10:48:57 +00001607 ** store it in the temporary table. If <expr> is a column, then use
1608 ** that columns affinity when building index keys. If <expr> is not
1609 ** a column, use numeric affinity.
drhfef52082000-06-06 01:50:43 +00001610 */
danielk1977e014a832004-05-17 10:48:57 +00001611 int i;
drh57dbd7b2005-07-08 18:25:26 +00001612 ExprList *pList = pExpr->pList;
1613 struct ExprList_item *pItem;
1614
danielk1977e014a832004-05-17 10:48:57 +00001615 if( !affinity ){
drh8159a352006-05-23 23:22:29 +00001616 affinity = SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +00001617 }
danielk19770202b292004-06-09 09:55:16 +00001618 keyInfo.aColl[0] = pExpr->pLeft->pColl;
danielk1977e014a832004-05-17 10:48:57 +00001619
1620 /* Loop through each expression in <exprlist>. */
drh57dbd7b2005-07-08 18:25:26 +00001621 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
1622 Expr *pE2 = pItem->pExpr;
danielk1977e014a832004-05-17 10:48:57 +00001623
drh57dbd7b2005-07-08 18:25:26 +00001624 /* If the expression is not constant then we will need to
1625 ** disable the test that was generated above that makes sure
1626 ** this code only executes once. Because for a non-constant
1627 ** expression we need to rerun this code each time.
1628 */
drh6c30be82005-07-29 15:10:17 +00001629 if( testAddr>0 && !sqlite3ExprIsConstant(pE2) ){
drhf8875402006-03-17 13:56:34 +00001630 sqlite3VdbeChangeToNoop(v, testAddr-1, 3);
drh57dbd7b2005-07-08 18:25:26 +00001631 testAddr = 0;
drh4794b982000-06-06 13:54:14 +00001632 }
danielk1977e014a832004-05-17 10:48:57 +00001633
1634 /* Evaluate the expression and insert it into the temp table */
1635 sqlite3ExprCode(pParse, pE2);
drh94a11212004-09-25 13:12:14 +00001636 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1);
drhf0863fe2005-06-12 21:35:51 +00001637 sqlite3VdbeAddOp(v, OP_IdxInsert, pExpr->iTable, 0);
drhfef52082000-06-06 01:50:43 +00001638 }
1639 }
danielk19770202b292004-06-09 09:55:16 +00001640 sqlite3VdbeChangeP3(v, addr, (void *)&keyInfo, P3_KEYINFO);
danielk1977b3bce662005-01-29 08:32:43 +00001641 break;
drhfef52082000-06-06 01:50:43 +00001642 }
1643
drh51522cd2005-01-20 13:36:19 +00001644 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001645 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +00001646 /* This has to be a scalar SELECT. Generate code to put the
1647 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +00001648 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +00001649 */
drh2646da72005-12-09 20:02:05 +00001650 static const Token one = { (u8*)"1", 0, 1 };
drh51522cd2005-01-20 13:36:19 +00001651 Select *pSel;
drhec7429a2005-10-06 16:53:14 +00001652 int iMem;
1653 int sop;
drh1398ad32005-01-19 23:24:50 +00001654
drhec7429a2005-10-06 16:53:14 +00001655 pExpr->iColumn = iMem = pParse->nMem++;
drh51522cd2005-01-20 13:36:19 +00001656 pSel = pExpr->pSelect;
1657 if( pExpr->op==TK_SELECT ){
1658 sop = SRT_Mem;
drhec7429a2005-10-06 16:53:14 +00001659 sqlite3VdbeAddOp(v, OP_MemNull, iMem, 0);
1660 VdbeComment((v, "# Init subquery result"));
drh51522cd2005-01-20 13:36:19 +00001661 }else{
drh51522cd2005-01-20 13:36:19 +00001662 sop = SRT_Exists;
drhec7429a2005-10-06 16:53:14 +00001663 sqlite3VdbeAddOp(v, OP_MemInt, 0, iMem);
1664 VdbeComment((v, "# Init EXISTS result"));
drh51522cd2005-01-20 13:36:19 +00001665 }
drhec7429a2005-10-06 16:53:14 +00001666 sqlite3ExprDelete(pSel->pLimit);
danielk1977a1644fd2007-08-29 12:31:25 +00001667 pSel->pLimit = sqlite3PExpr(pParse, TK_INTEGER, 0, 0, &one);
drh94ccde52007-04-13 16:06:32 +00001668 if( sqlite3Select(pParse, pSel, sop, iMem, 0, 0, 0, 0) ){
1669 return;
1670 }
danielk1977b3bce662005-01-29 08:32:43 +00001671 break;
drhcce7d172000-05-31 15:34:51 +00001672 }
1673 }
danielk1977b3bce662005-01-29 08:32:43 +00001674
drh57dbd7b2005-07-08 18:25:26 +00001675 if( testAddr ){
drhd654be82005-09-20 17:42:23 +00001676 sqlite3VdbeJumpHere(v, testAddr);
danielk1977b3bce662005-01-29 08:32:43 +00001677 }
danielk1977fc976062007-05-10 10:46:56 +00001678
danielk1977b3bce662005-01-29 08:32:43 +00001679 return;
drhcce7d172000-05-31 15:34:51 +00001680}
drh51522cd2005-01-20 13:36:19 +00001681#endif /* SQLITE_OMIT_SUBQUERY */
drhcce7d172000-05-31 15:34:51 +00001682
drhcce7d172000-05-31 15:34:51 +00001683/*
drh598f1342007-10-23 15:39:45 +00001684** Duplicate an 8-byte value
1685*/
1686static char *dup8bytes(Vdbe *v, const char *in){
1687 char *out = sqlite3DbMallocRaw(sqlite3VdbeDb(v), 8);
1688 if( out ){
1689 memcpy(out, in, 8);
1690 }
1691 return out;
1692}
1693
1694/*
1695** Generate an instruction that will put the floating point
1696** value described by z[0..n-1] on the stack.
drh0cf19ed2007-10-23 18:55:48 +00001697**
1698** The z[] string will probably not be zero-terminated. But the
1699** z[n] character is guaranteed to be something that does not look
1700** like the continuation of the number.
drh598f1342007-10-23 15:39:45 +00001701*/
1702static void codeReal(Vdbe *v, const char *z, int n, int negateFlag){
1703 assert( z || v==0 || sqlite3VdbeDb(v)->mallocFailed );
1704 if( z ){
1705 double value;
1706 char *zV;
drh0cf19ed2007-10-23 18:55:48 +00001707 assert( !isdigit(z[n]) );
drh598f1342007-10-23 15:39:45 +00001708 sqlite3AtoF(z, &value);
1709 if( negateFlag ) value = -value;
1710 zV = dup8bytes(v, (char*)&value);
1711 sqlite3VdbeOp3(v, OP_Real, 0, 0, zV, P3_REAL);
1712 }
1713}
1714
1715
1716/*
drhfec19aa2004-05-19 20:41:03 +00001717** Generate an instruction that will put the integer describe by
1718** text z[0..n-1] on the stack.
drh0cf19ed2007-10-23 18:55:48 +00001719**
1720** The z[] string will probably not be zero-terminated. But the
1721** z[n] character is guaranteed to be something that does not look
1722** like the continuation of the number.
drhfec19aa2004-05-19 20:41:03 +00001723*/
drh598f1342007-10-23 15:39:45 +00001724static void codeInteger(Vdbe *v, const char *z, int n, int negateFlag){
drhabb6fca2007-08-16 12:24:01 +00001725 assert( z || v==0 || sqlite3VdbeDb(v)->mallocFailed );
danielk1977c9cf9012007-05-30 10:36:47 +00001726 if( z ){
1727 int i;
drh0cf19ed2007-10-23 18:55:48 +00001728 assert( !isdigit(z[n]) );
danielk1977c9cf9012007-05-30 10:36:47 +00001729 if( sqlite3GetInt32(z, &i) ){
drh598f1342007-10-23 15:39:45 +00001730 if( negateFlag ) i = -i;
danielk1977c9cf9012007-05-30 10:36:47 +00001731 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
drh598f1342007-10-23 15:39:45 +00001732 }else if( sqlite3FitsIn64Bits(z, negateFlag) ){
1733 i64 value;
1734 char *zV;
1735 sqlite3Atoi64(z, &value);
1736 if( negateFlag ) value = -value;
1737 zV = dup8bytes(v, (char*)&value);
1738 sqlite3VdbeOp3(v, OP_Int64, 0, 0, zV, P3_INT64);
danielk1977c9cf9012007-05-30 10:36:47 +00001739 }else{
drh598f1342007-10-23 15:39:45 +00001740 codeReal(v, z, n, negateFlag);
danielk1977c9cf9012007-05-30 10:36:47 +00001741 }
drhfec19aa2004-05-19 20:41:03 +00001742 }
1743}
1744
drh945498f2007-02-24 11:52:52 +00001745
1746/*
1747** Generate code that will extract the iColumn-th column from
1748** table pTab and push that column value on the stack. There
1749** is an open cursor to pTab in iTable. If iColumn<0 then
1750** code is generated that extracts the rowid.
1751*/
1752void sqlite3ExprCodeGetColumn(Vdbe *v, Table *pTab, int iColumn, int iTable){
1753 if( iColumn<0 ){
1754 int op = (pTab && IsVirtual(pTab)) ? OP_VRowid : OP_Rowid;
1755 sqlite3VdbeAddOp(v, op, iTable, 0);
1756 }else if( pTab==0 ){
1757 sqlite3VdbeAddOp(v, OP_Column, iTable, iColumn);
1758 }else{
1759 int op = IsVirtual(pTab) ? OP_VColumn : OP_Column;
1760 sqlite3VdbeAddOp(v, op, iTable, iColumn);
1761 sqlite3ColumnDefault(v, pTab, iColumn);
1762#ifndef SQLITE_OMIT_FLOATING_POINT
1763 if( pTab->aCol[iColumn].affinity==SQLITE_AFF_REAL ){
1764 sqlite3VdbeAddOp(v, OP_RealAffinity, 0, 0);
1765 }
1766#endif
1767 }
1768}
1769
drhfec19aa2004-05-19 20:41:03 +00001770/*
drhcce7d172000-05-31 15:34:51 +00001771** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +00001772** expression and leave the result on the top of stack.
drhf2bc0132004-10-04 13:19:23 +00001773**
1774** This code depends on the fact that certain token values (ex: TK_EQ)
1775** are the same as opcode values (ex: OP_Eq) that implement the corresponding
1776** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
1777** the make process cause these values to align. Assert()s in the code
1778** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00001779*/
danielk19774adee202004-05-08 08:23:19 +00001780void sqlite3ExprCode(Parse *pParse, Expr *pExpr){
drhcce7d172000-05-31 15:34:51 +00001781 Vdbe *v = pParse->pVdbe;
1782 int op;
drhffe07b22005-11-03 00:41:17 +00001783 int stackChng = 1; /* Amount of change to stack depth */
1784
danielk19777977a172004-11-09 12:44:37 +00001785 if( v==0 ) return;
1786 if( pExpr==0 ){
drhf0863fe2005-06-12 21:35:51 +00001787 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
danielk19777977a172004-11-09 12:44:37 +00001788 return;
1789 }
drhf2bc0132004-10-04 13:19:23 +00001790 op = pExpr->op;
1791 switch( op ){
drh13449892005-09-07 21:22:45 +00001792 case TK_AGG_COLUMN: {
1793 AggInfo *pAggInfo = pExpr->pAggInfo;
1794 struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg];
1795 if( !pAggInfo->directMode ){
1796 sqlite3VdbeAddOp(v, OP_MemLoad, pCol->iMem, 0);
1797 break;
1798 }else if( pAggInfo->useSortingIdx ){
1799 sqlite3VdbeAddOp(v, OP_Column, pAggInfo->sortingIdx,
1800 pCol->iSorterColumn);
1801 break;
1802 }
1803 /* Otherwise, fall thru into the TK_COLUMN case */
1804 }
drh967e8b72000-06-21 13:59:10 +00001805 case TK_COLUMN: {
drhffe07b22005-11-03 00:41:17 +00001806 if( pExpr->iTable<0 ){
1807 /* This only happens when coding check constraints */
1808 assert( pParse->ckOffset>0 );
1809 sqlite3VdbeAddOp(v, OP_Dup, pParse->ckOffset-pExpr->iColumn-1, 1);
drhc4a3c772001-04-04 11:48:57 +00001810 }else{
drh945498f2007-02-24 11:52:52 +00001811 sqlite3ExprCodeGetColumn(v, pExpr->pTab, pExpr->iColumn, pExpr->iTable);
drh22827922000-06-06 17:27:05 +00001812 }
drhcce7d172000-05-31 15:34:51 +00001813 break;
1814 }
1815 case TK_INTEGER: {
drh598f1342007-10-23 15:39:45 +00001816 codeInteger(v, (char*)pExpr->token.z, pExpr->token.n, 0);
drhfec19aa2004-05-19 20:41:03 +00001817 break;
1818 }
drh598f1342007-10-23 15:39:45 +00001819 case TK_FLOAT: {
1820 codeReal(v, (char*)pExpr->token.z, pExpr->token.n, 0);
1821 break;
1822 }
drhfec19aa2004-05-19 20:41:03 +00001823 case TK_STRING: {
danielk19771e536952007-08-16 10:09:01 +00001824 sqlite3DequoteExpr(pParse->db, pExpr);
drh598f1342007-10-23 15:39:45 +00001825 sqlite3VdbeOp3(v,OP_String8, 0, 0, (char*)pExpr->token.z, pExpr->token.n);
drhcce7d172000-05-31 15:34:51 +00001826 break;
1827 }
drhf0863fe2005-06-12 21:35:51 +00001828 case TK_NULL: {
1829 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
1830 break;
1831 }
danielk19775338a5f2005-01-20 13:03:10 +00001832#ifndef SQLITE_OMIT_BLOB_LITERAL
danielk1977c572ef72004-05-27 09:28:41 +00001833 case TK_BLOB: {
drh6c8c6ce2005-08-23 11:17:58 +00001834 int n;
1835 const char *z;
drhf2bc0132004-10-04 13:19:23 +00001836 assert( TK_BLOB==OP_HexBlob );
drh6c8c6ce2005-08-23 11:17:58 +00001837 n = pExpr->token.n - 3;
drh2646da72005-12-09 20:02:05 +00001838 z = (char*)pExpr->token.z + 2;
drh6c8c6ce2005-08-23 11:17:58 +00001839 assert( n>=0 );
1840 if( n==0 ){
1841 z = "";
1842 }
1843 sqlite3VdbeOp3(v, op, 0, 0, z, n);
danielk1977c572ef72004-05-27 09:28:41 +00001844 break;
1845 }
danielk19775338a5f2005-01-20 13:03:10 +00001846#endif
drh50457892003-09-06 01:10:47 +00001847 case TK_VARIABLE: {
danielk19774adee202004-05-08 08:23:19 +00001848 sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0);
drh895d7472004-08-20 16:02:39 +00001849 if( pExpr->token.n>1 ){
drh2646da72005-12-09 20:02:05 +00001850 sqlite3VdbeChangeP3(v, -1, (char*)pExpr->token.z, pExpr->token.n);
drh895d7472004-08-20 16:02:39 +00001851 }
drh50457892003-09-06 01:10:47 +00001852 break;
1853 }
drh4e0cff62004-11-05 05:10:28 +00001854 case TK_REGISTER: {
1855 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iTable, 0);
1856 break;
1857 }
drh487e2622005-06-25 18:42:14 +00001858#ifndef SQLITE_OMIT_CAST
1859 case TK_CAST: {
1860 /* Expressions of the form: CAST(pLeft AS token) */
danielk1977f0113002006-01-24 12:09:17 +00001861 int aff, to_op;
drh487e2622005-06-25 18:42:14 +00001862 sqlite3ExprCode(pParse, pExpr->pLeft);
drh8a512562005-11-14 22:29:05 +00001863 aff = sqlite3AffinityType(&pExpr->token);
danielk1977f0113002006-01-24 12:09:17 +00001864 to_op = aff - SQLITE_AFF_TEXT + OP_ToText;
1865 assert( to_op==OP_ToText || aff!=SQLITE_AFF_TEXT );
1866 assert( to_op==OP_ToBlob || aff!=SQLITE_AFF_NONE );
1867 assert( to_op==OP_ToNumeric || aff!=SQLITE_AFF_NUMERIC );
1868 assert( to_op==OP_ToInt || aff!=SQLITE_AFF_INTEGER );
1869 assert( to_op==OP_ToReal || aff!=SQLITE_AFF_REAL );
1870 sqlite3VdbeAddOp(v, to_op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001871 stackChng = 0;
drh487e2622005-06-25 18:42:14 +00001872 break;
1873 }
1874#endif /* SQLITE_OMIT_CAST */
drhc9b84a12002-06-20 11:36:48 +00001875 case TK_LT:
1876 case TK_LE:
1877 case TK_GT:
1878 case TK_GE:
1879 case TK_NE:
1880 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00001881 assert( TK_LT==OP_Lt );
1882 assert( TK_LE==OP_Le );
1883 assert( TK_GT==OP_Gt );
1884 assert( TK_GE==OP_Ge );
1885 assert( TK_EQ==OP_Eq );
1886 assert( TK_NE==OP_Ne );
danielk1977a37cdde2004-05-16 11:15:36 +00001887 sqlite3ExprCode(pParse, pExpr->pLeft);
1888 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001889 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001890 stackChng = -1;
danielk1977a37cdde2004-05-16 11:15:36 +00001891 break;
drhc9b84a12002-06-20 11:36:48 +00001892 }
drhcce7d172000-05-31 15:34:51 +00001893 case TK_AND:
1894 case TK_OR:
1895 case TK_PLUS:
1896 case TK_STAR:
1897 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00001898 case TK_REM:
1899 case TK_BITAND:
1900 case TK_BITOR:
drh17c40292004-07-21 02:53:29 +00001901 case TK_SLASH:
drhbf4133c2001-10-13 02:59:08 +00001902 case TK_LSHIFT:
drh855eb1c2004-08-31 13:45:11 +00001903 case TK_RSHIFT:
drh00400772000-06-16 20:51:26 +00001904 case TK_CONCAT: {
drhf2bc0132004-10-04 13:19:23 +00001905 assert( TK_AND==OP_And );
1906 assert( TK_OR==OP_Or );
1907 assert( TK_PLUS==OP_Add );
1908 assert( TK_MINUS==OP_Subtract );
1909 assert( TK_REM==OP_Remainder );
1910 assert( TK_BITAND==OP_BitAnd );
1911 assert( TK_BITOR==OP_BitOr );
1912 assert( TK_SLASH==OP_Divide );
1913 assert( TK_LSHIFT==OP_ShiftLeft );
1914 assert( TK_RSHIFT==OP_ShiftRight );
1915 assert( TK_CONCAT==OP_Concat );
danielk19774adee202004-05-08 08:23:19 +00001916 sqlite3ExprCode(pParse, pExpr->pLeft);
1917 sqlite3ExprCode(pParse, pExpr->pRight);
drh855eb1c2004-08-31 13:45:11 +00001918 sqlite3VdbeAddOp(v, op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001919 stackChng = -1;
drh00400772000-06-16 20:51:26 +00001920 break;
1921 }
drhcce7d172000-05-31 15:34:51 +00001922 case TK_UMINUS: {
drhfec19aa2004-05-19 20:41:03 +00001923 Expr *pLeft = pExpr->pLeft;
1924 assert( pLeft );
1925 if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
1926 Token *p = &pLeft->token;
drhfec19aa2004-05-19 20:41:03 +00001927 if( pLeft->op==TK_FLOAT ){
drh598f1342007-10-23 15:39:45 +00001928 codeReal(v, (char*)p->z, p->n, 1);
drhe6840902002-03-06 03:08:25 +00001929 }else{
drh598f1342007-10-23 15:39:45 +00001930 codeInteger(v, (char*)p->z, p->n, 1);
drhe6840902002-03-06 03:08:25 +00001931 }
drh6e142f52000-06-08 13:36:40 +00001932 break;
1933 }
drh1ccde152000-06-17 13:12:39 +00001934 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +00001935 }
drhbf4133c2001-10-13 02:59:08 +00001936 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00001937 case TK_NOT: {
drhf2bc0132004-10-04 13:19:23 +00001938 assert( TK_BITNOT==OP_BitNot );
1939 assert( TK_NOT==OP_Not );
danielk19774adee202004-05-08 08:23:19 +00001940 sqlite3ExprCode(pParse, pExpr->pLeft);
1941 sqlite3VdbeAddOp(v, op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001942 stackChng = 0;
drhcce7d172000-05-31 15:34:51 +00001943 break;
1944 }
1945 case TK_ISNULL:
1946 case TK_NOTNULL: {
1947 int dest;
drhf2bc0132004-10-04 13:19:23 +00001948 assert( TK_ISNULL==OP_IsNull );
1949 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00001950 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1951 sqlite3ExprCode(pParse, pExpr->pLeft);
1952 dest = sqlite3VdbeCurrentAddr(v) + 2;
1953 sqlite3VdbeAddOp(v, op, 1, dest);
1954 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);
drhffe07b22005-11-03 00:41:17 +00001955 stackChng = 0;
drhf2bc0132004-10-04 13:19:23 +00001956 break;
drhcce7d172000-05-31 15:34:51 +00001957 }
drh22827922000-06-06 17:27:05 +00001958 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00001959 AggInfo *pInfo = pExpr->pAggInfo;
drh7e56e712005-11-16 12:53:15 +00001960 if( pInfo==0 ){
1961 sqlite3ErrorMsg(pParse, "misuse of aggregate: %T",
1962 &pExpr->span);
1963 }else{
1964 sqlite3VdbeAddOp(v, OP_MemLoad, pInfo->aFunc[pExpr->iAgg].iMem, 0);
1965 }
drh22827922000-06-06 17:27:05 +00001966 break;
1967 }
drhb71090f2005-05-23 17:26:51 +00001968 case TK_CONST_FUNC:
drhcce7d172000-05-31 15:34:51 +00001969 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00001970 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00001971 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00001972 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00001973 int nId;
1974 const char *zId;
drh13449892005-09-07 21:22:45 +00001975 int constMask = 0;
danielk1977682f68b2004-06-05 10:22:17 +00001976 int i;
drh17435752007-08-16 04:30:38 +00001977 sqlite3 *db = pParse->db;
1978 u8 enc = ENC(db);
danielk1977dc1bdc42004-06-11 10:51:27 +00001979 CollSeq *pColl = 0;
drh17435752007-08-16 04:30:38 +00001980
drh2646da72005-12-09 20:02:05 +00001981 zId = (char*)pExpr->token.z;
drhb71090f2005-05-23 17:26:51 +00001982 nId = pExpr->token.n;
danielk1977d8123362004-06-12 09:25:12 +00001983 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001984 assert( pDef!=0 );
drhf9b596e2004-05-26 16:54:42 +00001985 nExpr = sqlite3ExprCodeExprList(pParse, pList);
drhb7f6f682006-07-08 17:06:43 +00001986#ifndef SQLITE_OMIT_VIRTUALTABLE
drha43fa222006-07-08 18:41:37 +00001987 /* Possibly overload the function if the first argument is
1988 ** a virtual table column.
1989 **
1990 ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the
1991 ** second argument, not the first, as the argument to test to
1992 ** see if it is a column in a virtual table. This is done because
1993 ** the left operand of infix functions (the operand we want to
1994 ** control overloading) ends up as the second argument to the
1995 ** function. The expression "A glob B" is equivalent to
1996 ** "glob(B,A). We want to use the A in "A glob B" to test
1997 ** for function overloading. But we use the B term in "glob(B,A)".
1998 */
drh6a03a1c2006-07-08 18:34:59 +00001999 if( nExpr>=2 && (pExpr->flags & EP_InfixFunc) ){
drh17435752007-08-16 04:30:38 +00002000 pDef = sqlite3VtabOverloadFunction(db, pDef, nExpr, pList->a[1].pExpr);
drh6a03a1c2006-07-08 18:34:59 +00002001 }else if( nExpr>0 ){
drh17435752007-08-16 04:30:38 +00002002 pDef = sqlite3VtabOverloadFunction(db, pDef, nExpr, pList->a[0].pExpr);
drhb7f6f682006-07-08 17:06:43 +00002003 }
2004#endif
danielk1977682f68b2004-06-05 10:22:17 +00002005 for(i=0; i<nExpr && i<32; i++){
danielk1977d02eb1f2004-06-06 09:44:03 +00002006 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
drh13449892005-09-07 21:22:45 +00002007 constMask |= (1<<i);
danielk1977d02eb1f2004-06-06 09:44:03 +00002008 }
danielk1977dc1bdc42004-06-11 10:51:27 +00002009 if( pDef->needCollSeq && !pColl ){
2010 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
2011 }
2012 }
2013 if( pDef->needCollSeq ){
2014 if( !pColl ) pColl = pParse->db->pDfltColl;
danielk1977d8123362004-06-12 09:25:12 +00002015 sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ);
danielk1977682f68b2004-06-05 10:22:17 +00002016 }
drh13449892005-09-07 21:22:45 +00002017 sqlite3VdbeOp3(v, OP_Function, constMask, nExpr, (char*)pDef, P3_FUNCDEF);
drhffe07b22005-11-03 00:41:17 +00002018 stackChng = 1-nExpr;
drhcce7d172000-05-31 15:34:51 +00002019 break;
2020 }
drhfe2093d2005-01-20 22:48:47 +00002021#ifndef SQLITE_OMIT_SUBQUERY
2022 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00002023 case TK_SELECT: {
drh41714d62006-03-02 04:44:23 +00002024 if( pExpr->iColumn==0 ){
2025 sqlite3CodeSubselect(pParse, pExpr);
2026 }
danielk19774adee202004-05-08 08:23:19 +00002027 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drhad6d9462004-09-19 02:15:24 +00002028 VdbeComment((v, "# load subquery result"));
drh19a775c2000-06-05 18:54:46 +00002029 break;
2030 }
drhfef52082000-06-06 01:50:43 +00002031 case TK_IN: {
2032 int addr;
drh94a11212004-09-25 13:12:14 +00002033 char affinity;
drhafa5f682006-01-30 14:36:59 +00002034 int ckOffset = pParse->ckOffset;
danielk1977b3bce662005-01-29 08:32:43 +00002035 sqlite3CodeSubselect(pParse, pExpr);
danielk1977e014a832004-05-17 10:48:57 +00002036
2037 /* Figure out the affinity to use to create a key from the results
2038 ** of the expression. affinityStr stores a static string suitable for
danielk1977ededfd52004-06-17 07:53:01 +00002039 ** P3 of OP_MakeRecord.
danielk1977e014a832004-05-17 10:48:57 +00002040 */
drh94a11212004-09-25 13:12:14 +00002041 affinity = comparisonAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +00002042
danielk19774adee202004-05-08 08:23:19 +00002043 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
danielk1977cdbd8ef2007-05-12 06:11:12 +00002044 pParse->ckOffset = (ckOffset ? (ckOffset+1) : 0);
danielk1977e014a832004-05-17 10:48:57 +00002045
2046 /* Code the <expr> from "<expr> IN (...)". The temporary table
2047 ** pExpr->iTable contains the values that make up the (...) set.
2048 */
danielk19774adee202004-05-08 08:23:19 +00002049 sqlite3ExprCode(pParse, pExpr->pLeft);
2050 addr = sqlite3VdbeCurrentAddr(v);
danielk1977e014a832004-05-17 10:48:57 +00002051 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); /* addr + 0 */
danielk19774adee202004-05-08 08:23:19 +00002052 sqlite3VdbeAddOp(v, OP_Pop, 2, 0);
drhf0863fe2005-06-12 21:35:51 +00002053 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +00002054 sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7);
drh94a11212004-09-25 13:12:14 +00002055 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1); /* addr + 4 */
danielk1977e014a832004-05-17 10:48:57 +00002056 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7);
2057 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); /* addr + 6 */
2058
drhfef52082000-06-06 01:50:43 +00002059 break;
2060 }
danielk197793758c82005-01-21 08:13:14 +00002061#endif
drhfef52082000-06-06 01:50:43 +00002062 case TK_BETWEEN: {
drhbe5c89a2004-07-26 00:31:09 +00002063 Expr *pLeft = pExpr->pLeft;
2064 struct ExprList_item *pLItem = pExpr->pList->a;
2065 Expr *pRight = pLItem->pExpr;
2066 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00002067 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00002068 sqlite3ExprCode(pParse, pRight);
2069 codeCompare(pParse, pLeft, pRight, OP_Ge, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00002070 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
drhbe5c89a2004-07-26 00:31:09 +00002071 pLItem++;
2072 pRight = pLItem->pExpr;
2073 sqlite3ExprCode(pParse, pRight);
2074 codeCompare(pParse, pLeft, pRight, OP_Le, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00002075 sqlite3VdbeAddOp(v, OP_And, 0, 0);
drhfef52082000-06-06 01:50:43 +00002076 break;
2077 }
drh4f07e5f2007-05-14 11:34:46 +00002078 case TK_UPLUS: {
danielk19774adee202004-05-08 08:23:19 +00002079 sqlite3ExprCode(pParse, pExpr->pLeft);
drhffe07b22005-11-03 00:41:17 +00002080 stackChng = 0;
drha2e00042002-01-22 03:13:42 +00002081 break;
2082 }
drh17a7f8d2002-03-24 13:13:27 +00002083 case TK_CASE: {
2084 int expr_end_label;
drhf5905aa2002-05-26 20:54:33 +00002085 int jumpInst;
drhf5905aa2002-05-26 20:54:33 +00002086 int nExpr;
drh17a7f8d2002-03-24 13:13:27 +00002087 int i;
drhbe5c89a2004-07-26 00:31:09 +00002088 ExprList *pEList;
2089 struct ExprList_item *aListelem;
drh17a7f8d2002-03-24 13:13:27 +00002090
2091 assert(pExpr->pList);
2092 assert((pExpr->pList->nExpr % 2) == 0);
2093 assert(pExpr->pList->nExpr > 0);
drhbe5c89a2004-07-26 00:31:09 +00002094 pEList = pExpr->pList;
2095 aListelem = pEList->a;
2096 nExpr = pEList->nExpr;
danielk19774adee202004-05-08 08:23:19 +00002097 expr_end_label = sqlite3VdbeMakeLabel(v);
drh17a7f8d2002-03-24 13:13:27 +00002098 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00002099 sqlite3ExprCode(pParse, pExpr->pLeft);
drh17a7f8d2002-03-24 13:13:27 +00002100 }
drhf5905aa2002-05-26 20:54:33 +00002101 for(i=0; i<nExpr; i=i+2){
drhbe5c89a2004-07-26 00:31:09 +00002102 sqlite3ExprCode(pParse, aListelem[i].pExpr);
drh17a7f8d2002-03-24 13:13:27 +00002103 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00002104 sqlite3VdbeAddOp(v, OP_Dup, 1, 1);
drhbe5c89a2004-07-26 00:31:09 +00002105 jumpInst = codeCompare(pParse, pExpr->pLeft, aListelem[i].pExpr,
2106 OP_Ne, 0, 1);
danielk19774adee202004-05-08 08:23:19 +00002107 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00002108 }else{
danielk19774adee202004-05-08 08:23:19 +00002109 jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00002110 }
drhbe5c89a2004-07-26 00:31:09 +00002111 sqlite3ExprCode(pParse, aListelem[i+1].pExpr);
danielk19774adee202004-05-08 08:23:19 +00002112 sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label);
drhd654be82005-09-20 17:42:23 +00002113 sqlite3VdbeJumpHere(v, jumpInst);
drh17a7f8d2002-03-24 13:13:27 +00002114 }
drhf570f012002-05-31 15:51:25 +00002115 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00002116 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhf570f012002-05-31 15:51:25 +00002117 }
drh17a7f8d2002-03-24 13:13:27 +00002118 if( pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00002119 sqlite3ExprCode(pParse, pExpr->pRight);
drh17a7f8d2002-03-24 13:13:27 +00002120 }else{
drhf0863fe2005-06-12 21:35:51 +00002121 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
drh17a7f8d2002-03-24 13:13:27 +00002122 }
danielk19774adee202004-05-08 08:23:19 +00002123 sqlite3VdbeResolveLabel(v, expr_end_label);
danielk19776f349032002-06-11 02:25:40 +00002124 break;
2125 }
danielk19775338a5f2005-01-20 13:03:10 +00002126#ifndef SQLITE_OMIT_TRIGGER
danielk19776f349032002-06-11 02:25:40 +00002127 case TK_RAISE: {
2128 if( !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +00002129 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00002130 "RAISE() may only be used within a trigger-program");
drhfd131da2007-08-07 17:13:03 +00002131 return;
danielk19776f349032002-06-11 02:25:40 +00002132 }
drhad6d9462004-09-19 02:15:24 +00002133 if( pExpr->iColumn!=OE_Ignore ){
2134 assert( pExpr->iColumn==OE_Rollback ||
2135 pExpr->iColumn == OE_Abort ||
2136 pExpr->iColumn == OE_Fail );
danielk19771e536952007-08-16 10:09:01 +00002137 sqlite3DequoteExpr(pParse->db, pExpr);
drhad6d9462004-09-19 02:15:24 +00002138 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn,
drh2646da72005-12-09 20:02:05 +00002139 (char*)pExpr->token.z, pExpr->token.n);
danielk19776f349032002-06-11 02:25:40 +00002140 } else {
drhad6d9462004-09-19 02:15:24 +00002141 assert( pExpr->iColumn == OE_Ignore );
2142 sqlite3VdbeAddOp(v, OP_ContextPop, 0, 0);
2143 sqlite3VdbeAddOp(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
2144 VdbeComment((v, "# raise(IGNORE)"));
danielk19776f349032002-06-11 02:25:40 +00002145 }
drhffe07b22005-11-03 00:41:17 +00002146 stackChng = 0;
2147 break;
drh17a7f8d2002-03-24 13:13:27 +00002148 }
danielk19775338a5f2005-01-20 13:03:10 +00002149#endif
drhffe07b22005-11-03 00:41:17 +00002150 }
2151
2152 if( pParse->ckOffset ){
2153 pParse->ckOffset += stackChng;
2154 assert( pParse->ckOffset );
drhcce7d172000-05-31 15:34:51 +00002155 }
drhcce7d172000-05-31 15:34:51 +00002156}
2157
danielk197793758c82005-01-21 08:13:14 +00002158#ifndef SQLITE_OMIT_TRIGGER
drhcce7d172000-05-31 15:34:51 +00002159/*
drh25303782004-12-07 15:41:48 +00002160** Generate code that evalutes the given expression and leaves the result
2161** on the stack. See also sqlite3ExprCode().
2162**
2163** This routine might also cache the result and modify the pExpr tree
2164** so that it will make use of the cached result on subsequent evaluations
2165** rather than evaluate the whole expression again. Trivial expressions are
2166** not cached. If the expression is cached, its result is stored in a
2167** memory location.
2168*/
2169void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr){
2170 Vdbe *v = pParse->pVdbe;
2171 int iMem;
2172 int addr1, addr2;
2173 if( v==0 ) return;
2174 addr1 = sqlite3VdbeCurrentAddr(v);
2175 sqlite3ExprCode(pParse, pExpr);
2176 addr2 = sqlite3VdbeCurrentAddr(v);
2177 if( addr2>addr1+1 || sqlite3VdbeGetOp(v, addr1)->opcode==OP_Function ){
2178 iMem = pExpr->iTable = pParse->nMem++;
2179 sqlite3VdbeAddOp(v, OP_MemStore, iMem, 0);
2180 pExpr->op = TK_REGISTER;
2181 }
2182}
danielk197793758c82005-01-21 08:13:14 +00002183#endif
drh25303782004-12-07 15:41:48 +00002184
2185/*
drh268380c2004-02-25 13:47:31 +00002186** Generate code that pushes the value of every element of the given
drhf9b596e2004-05-26 16:54:42 +00002187** expression list onto the stack.
drh268380c2004-02-25 13:47:31 +00002188**
2189** Return the number of elements pushed onto the stack.
2190*/
danielk19774adee202004-05-08 08:23:19 +00002191int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00002192 Parse *pParse, /* Parsing context */
drhf9b596e2004-05-26 16:54:42 +00002193 ExprList *pList /* The expression list to be coded */
drh268380c2004-02-25 13:47:31 +00002194){
2195 struct ExprList_item *pItem;
2196 int i, n;
drh268380c2004-02-25 13:47:31 +00002197 if( pList==0 ) return 0;
drh268380c2004-02-25 13:47:31 +00002198 n = pList->nExpr;
drhc182d162005-08-14 20:47:16 +00002199 for(pItem=pList->a, i=n; i>0; i--, pItem++){
danielk19774adee202004-05-08 08:23:19 +00002200 sqlite3ExprCode(pParse, pItem->pExpr);
drh268380c2004-02-25 13:47:31 +00002201 }
drhf9b596e2004-05-26 16:54:42 +00002202 return n;
drh268380c2004-02-25 13:47:31 +00002203}
2204
2205/*
drhcce7d172000-05-31 15:34:51 +00002206** Generate code for a boolean expression such that a jump is made
2207** to the label "dest" if the expression is true but execution
2208** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00002209**
2210** If the expression evaluates to NULL (neither true nor false), then
2211** take the jump if the jumpIfNull flag is true.
drhf2bc0132004-10-04 13:19:23 +00002212**
2213** This code depends on the fact that certain token values (ex: TK_EQ)
2214** are the same as opcode values (ex: OP_Eq) that implement the corresponding
2215** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
2216** the make process cause these values to align. Assert()s in the code
2217** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00002218*/
danielk19774adee202004-05-08 08:23:19 +00002219void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00002220 Vdbe *v = pParse->pVdbe;
2221 int op = 0;
drhffe07b22005-11-03 00:41:17 +00002222 int ckOffset = pParse->ckOffset;
drhdaffd0e2001-04-11 14:28:42 +00002223 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00002224 op = pExpr->op;
2225 switch( op ){
drhcce7d172000-05-31 15:34:51 +00002226 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00002227 int d2 = sqlite3VdbeMakeLabel(v);
2228 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
2229 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
2230 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00002231 break;
2232 }
2233 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00002234 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
2235 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002236 break;
2237 }
2238 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00002239 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002240 break;
2241 }
2242 case TK_LT:
2243 case TK_LE:
2244 case TK_GT:
2245 case TK_GE:
2246 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00002247 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00002248 assert( TK_LT==OP_Lt );
2249 assert( TK_LE==OP_Le );
2250 assert( TK_GT==OP_Gt );
2251 assert( TK_GE==OP_Ge );
2252 assert( TK_EQ==OP_Eq );
2253 assert( TK_NE==OP_Ne );
danielk19774adee202004-05-08 08:23:19 +00002254 sqlite3ExprCode(pParse, pExpr->pLeft);
2255 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00002256 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002257 break;
2258 }
2259 case TK_ISNULL:
2260 case TK_NOTNULL: {
drhf2bc0132004-10-04 13:19:23 +00002261 assert( TK_ISNULL==OP_IsNull );
2262 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00002263 sqlite3ExprCode(pParse, pExpr->pLeft);
2264 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00002265 break;
2266 }
drhfef52082000-06-06 01:50:43 +00002267 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00002268 /* The expression "x BETWEEN y AND z" is implemented as:
2269 **
2270 ** 1 IF (x < y) GOTO 3
2271 ** 2 IF (x <= z) GOTO <dest>
2272 ** 3 ...
2273 */
drhf5905aa2002-05-26 20:54:33 +00002274 int addr;
drhbe5c89a2004-07-26 00:31:09 +00002275 Expr *pLeft = pExpr->pLeft;
2276 Expr *pRight = pExpr->pList->a[0].pExpr;
2277 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00002278 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00002279 sqlite3ExprCode(pParse, pRight);
2280 addr = codeCompare(pParse, pLeft, pRight, OP_Lt, 0, !jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00002281
drhbe5c89a2004-07-26 00:31:09 +00002282 pRight = pExpr->pList->a[1].pExpr;
2283 sqlite3ExprCode(pParse, pRight);
2284 codeCompare(pParse, pLeft, pRight, OP_Le, dest, jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00002285
danielk19774adee202004-05-08 08:23:19 +00002286 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
drhd654be82005-09-20 17:42:23 +00002287 sqlite3VdbeJumpHere(v, addr);
danielk19774adee202004-05-08 08:23:19 +00002288 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00002289 break;
2290 }
drhcce7d172000-05-31 15:34:51 +00002291 default: {
danielk19774adee202004-05-08 08:23:19 +00002292 sqlite3ExprCode(pParse, pExpr);
2293 sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00002294 break;
2295 }
2296 }
drhffe07b22005-11-03 00:41:17 +00002297 pParse->ckOffset = ckOffset;
drhcce7d172000-05-31 15:34:51 +00002298}
2299
2300/*
drh66b89c82000-11-28 20:47:17 +00002301** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00002302** to the label "dest" if the expression is false but execution
2303** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00002304**
2305** If the expression evaluates to NULL (neither true nor false) then
2306** jump if jumpIfNull is true or fall through if jumpIfNull is false.
drhcce7d172000-05-31 15:34:51 +00002307*/
danielk19774adee202004-05-08 08:23:19 +00002308void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00002309 Vdbe *v = pParse->pVdbe;
2310 int op = 0;
drhffe07b22005-11-03 00:41:17 +00002311 int ckOffset = pParse->ckOffset;
drhdaffd0e2001-04-11 14:28:42 +00002312 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00002313
2314 /* The value of pExpr->op and op are related as follows:
2315 **
2316 ** pExpr->op op
2317 ** --------- ----------
2318 ** TK_ISNULL OP_NotNull
2319 ** TK_NOTNULL OP_IsNull
2320 ** TK_NE OP_Eq
2321 ** TK_EQ OP_Ne
2322 ** TK_GT OP_Le
2323 ** TK_LE OP_Gt
2324 ** TK_GE OP_Lt
2325 ** TK_LT OP_Ge
2326 **
2327 ** For other values of pExpr->op, op is undefined and unused.
2328 ** The value of TK_ and OP_ constants are arranged such that we
2329 ** can compute the mapping above using the following expression.
2330 ** Assert()s verify that the computation is correct.
2331 */
2332 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
2333
2334 /* Verify correct alignment of TK_ and OP_ constants
2335 */
2336 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
2337 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
2338 assert( pExpr->op!=TK_NE || op==OP_Eq );
2339 assert( pExpr->op!=TK_EQ || op==OP_Ne );
2340 assert( pExpr->op!=TK_LT || op==OP_Ge );
2341 assert( pExpr->op!=TK_LE || op==OP_Gt );
2342 assert( pExpr->op!=TK_GT || op==OP_Le );
2343 assert( pExpr->op!=TK_GE || op==OP_Lt );
2344
drhcce7d172000-05-31 15:34:51 +00002345 switch( pExpr->op ){
2346 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00002347 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
2348 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002349 break;
2350 }
2351 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00002352 int d2 = sqlite3VdbeMakeLabel(v);
2353 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
2354 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
2355 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00002356 break;
2357 }
2358 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00002359 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002360 break;
2361 }
2362 case TK_LT:
2363 case TK_LE:
2364 case TK_GT:
2365 case TK_GE:
2366 case TK_NE:
2367 case TK_EQ: {
danielk19774adee202004-05-08 08:23:19 +00002368 sqlite3ExprCode(pParse, pExpr->pLeft);
2369 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00002370 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002371 break;
2372 }
drhcce7d172000-05-31 15:34:51 +00002373 case TK_ISNULL:
2374 case TK_NOTNULL: {
danielk19774adee202004-05-08 08:23:19 +00002375 sqlite3ExprCode(pParse, pExpr->pLeft);
2376 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00002377 break;
2378 }
drhfef52082000-06-06 01:50:43 +00002379 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00002380 /* The expression is "x BETWEEN y AND z". It is implemented as:
2381 **
2382 ** 1 IF (x >= y) GOTO 3
2383 ** 2 GOTO <dest>
2384 ** 3 IF (x > z) GOTO <dest>
2385 */
drhfef52082000-06-06 01:50:43 +00002386 int addr;
drhbe5c89a2004-07-26 00:31:09 +00002387 Expr *pLeft = pExpr->pLeft;
2388 Expr *pRight = pExpr->pList->a[0].pExpr;
2389 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00002390 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00002391 sqlite3ExprCode(pParse, pRight);
danielk19774adee202004-05-08 08:23:19 +00002392 addr = sqlite3VdbeCurrentAddr(v);
drhbe5c89a2004-07-26 00:31:09 +00002393 codeCompare(pParse, pLeft, pRight, OP_Ge, addr+3, !jumpIfNull);
2394
danielk19774adee202004-05-08 08:23:19 +00002395 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
2396 sqlite3VdbeAddOp(v, OP_Goto, 0, dest);
drhbe5c89a2004-07-26 00:31:09 +00002397 pRight = pExpr->pList->a[1].pExpr;
2398 sqlite3ExprCode(pParse, pRight);
2399 codeCompare(pParse, pLeft, pRight, OP_Gt, dest, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00002400 break;
2401 }
drhcce7d172000-05-31 15:34:51 +00002402 default: {
danielk19774adee202004-05-08 08:23:19 +00002403 sqlite3ExprCode(pParse, pExpr);
2404 sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00002405 break;
2406 }
2407 }
drhffe07b22005-11-03 00:41:17 +00002408 pParse->ckOffset = ckOffset;
drhcce7d172000-05-31 15:34:51 +00002409}
drh22827922000-06-06 17:27:05 +00002410
2411/*
2412** Do a deep comparison of two expression trees. Return TRUE (non-zero)
2413** if they are identical and return FALSE if they differ in any way.
drhd40aab02007-02-24 15:29:03 +00002414**
2415** Sometimes this routine will return FALSE even if the two expressions
2416** really are equivalent. If we cannot prove that the expressions are
2417** identical, we return FALSE just to be safe. So if this routine
2418** returns false, then you do not really know for certain if the two
2419** expressions are the same. But if you get a TRUE return, then you
2420** can be sure the expressions are the same. In the places where
2421** this routine is used, it does not hurt to get an extra FALSE - that
2422** just might result in some slightly slower code. But returning
2423** an incorrect TRUE could lead to a malfunction.
drh22827922000-06-06 17:27:05 +00002424*/
danielk19774adee202004-05-08 08:23:19 +00002425int sqlite3ExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00002426 int i;
danielk19774b202ae2006-01-23 05:50:58 +00002427 if( pA==0||pB==0 ){
2428 return pB==pA;
drh22827922000-06-06 17:27:05 +00002429 }
2430 if( pA->op!=pB->op ) return 0;
drhfd357972005-09-09 01:33:19 +00002431 if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 0;
danielk19774adee202004-05-08 08:23:19 +00002432 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
2433 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00002434 if( pA->pList ){
2435 if( pB->pList==0 ) return 0;
2436 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
2437 for(i=0; i<pA->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00002438 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00002439 return 0;
2440 }
2441 }
2442 }else if( pB->pList ){
2443 return 0;
2444 }
2445 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00002446 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drhdd735212007-02-24 13:53:05 +00002447 if( pA->op!=TK_COLUMN && pA->token.z ){
drh22827922000-06-06 17:27:05 +00002448 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00002449 if( pB->token.n!=pA->token.n ) return 0;
drh2646da72005-12-09 20:02:05 +00002450 if( sqlite3StrNICmp((char*)pA->token.z,(char*)pB->token.z,pB->token.n)!=0 ){
2451 return 0;
2452 }
drh22827922000-06-06 17:27:05 +00002453 }
2454 return 1;
2455}
2456
drh13449892005-09-07 21:22:45 +00002457
drh22827922000-06-06 17:27:05 +00002458/*
drh13449892005-09-07 21:22:45 +00002459** Add a new element to the pAggInfo->aCol[] array. Return the index of
2460** the new element. Return a negative number if malloc fails.
drh22827922000-06-06 17:27:05 +00002461*/
drh17435752007-08-16 04:30:38 +00002462static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){
drh13449892005-09-07 21:22:45 +00002463 int i;
drhcf643722007-03-27 13:36:37 +00002464 pInfo->aCol = sqlite3ArrayAllocate(
drh17435752007-08-16 04:30:38 +00002465 db,
drhcf643722007-03-27 13:36:37 +00002466 pInfo->aCol,
2467 sizeof(pInfo->aCol[0]),
2468 3,
2469 &pInfo->nColumn,
2470 &pInfo->nColumnAlloc,
2471 &i
2472 );
drh13449892005-09-07 21:22:45 +00002473 return i;
2474}
2475
2476/*
2477** Add a new element to the pAggInfo->aFunc[] array. Return the index of
2478** the new element. Return a negative number if malloc fails.
2479*/
drh17435752007-08-16 04:30:38 +00002480static int addAggInfoFunc(sqlite3 *db, AggInfo *pInfo){
drh13449892005-09-07 21:22:45 +00002481 int i;
drhcf643722007-03-27 13:36:37 +00002482 pInfo->aFunc = sqlite3ArrayAllocate(
drh17435752007-08-16 04:30:38 +00002483 db,
drhcf643722007-03-27 13:36:37 +00002484 pInfo->aFunc,
2485 sizeof(pInfo->aFunc[0]),
2486 3,
2487 &pInfo->nFunc,
2488 &pInfo->nFuncAlloc,
2489 &i
2490 );
drh13449892005-09-07 21:22:45 +00002491 return i;
2492}
drh22827922000-06-06 17:27:05 +00002493
2494/*
drh626a8792005-01-17 22:08:19 +00002495** This is an xFunc for walkExprTree() used to implement
2496** sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates
2497** for additional information.
drh22827922000-06-06 17:27:05 +00002498**
drh626a8792005-01-17 22:08:19 +00002499** This routine analyzes the aggregate function at pExpr.
drh22827922000-06-06 17:27:05 +00002500*/
drh626a8792005-01-17 22:08:19 +00002501static int analyzeAggregate(void *pArg, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00002502 int i;
danielk1977a58fdfb2005-02-08 07:50:40 +00002503 NameContext *pNC = (NameContext *)pArg;
2504 Parse *pParse = pNC->pParse;
2505 SrcList *pSrcList = pNC->pSrcList;
drh13449892005-09-07 21:22:45 +00002506 AggInfo *pAggInfo = pNC->pAggInfo;
drh22827922000-06-06 17:27:05 +00002507
drh22827922000-06-06 17:27:05 +00002508 switch( pExpr->op ){
drh89c69d02007-01-04 01:20:28 +00002509 case TK_AGG_COLUMN:
drh967e8b72000-06-21 13:59:10 +00002510 case TK_COLUMN: {
drh13449892005-09-07 21:22:45 +00002511 /* Check to see if the column is in one of the tables in the FROM
2512 ** clause of the aggregate query */
2513 if( pSrcList ){
2514 struct SrcList_item *pItem = pSrcList->a;
2515 for(i=0; i<pSrcList->nSrc; i++, pItem++){
2516 struct AggInfo_col *pCol;
2517 if( pExpr->iTable==pItem->iCursor ){
2518 /* If we reach this point, it means that pExpr refers to a table
2519 ** that is in the FROM clause of the aggregate query.
2520 **
2521 ** Make an entry for the column in pAggInfo->aCol[] if there
2522 ** is not an entry there already.
2523 */
drh7f906d62007-03-12 23:48:52 +00002524 int k;
drh13449892005-09-07 21:22:45 +00002525 pCol = pAggInfo->aCol;
drh7f906d62007-03-12 23:48:52 +00002526 for(k=0; k<pAggInfo->nColumn; k++, pCol++){
drh13449892005-09-07 21:22:45 +00002527 if( pCol->iTable==pExpr->iTable &&
2528 pCol->iColumn==pExpr->iColumn ){
2529 break;
2530 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002531 }
danielk19771e536952007-08-16 10:09:01 +00002532 if( (k>=pAggInfo->nColumn)
2533 && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0
2534 ){
drh7f906d62007-03-12 23:48:52 +00002535 pCol = &pAggInfo->aCol[k];
danielk19770817d0d2007-02-14 09:19:36 +00002536 pCol->pTab = pExpr->pTab;
drh13449892005-09-07 21:22:45 +00002537 pCol->iTable = pExpr->iTable;
2538 pCol->iColumn = pExpr->iColumn;
2539 pCol->iMem = pParse->nMem++;
2540 pCol->iSorterColumn = -1;
drh5774b802005-09-07 22:48:16 +00002541 pCol->pExpr = pExpr;
drh13449892005-09-07 21:22:45 +00002542 if( pAggInfo->pGroupBy ){
2543 int j, n;
2544 ExprList *pGB = pAggInfo->pGroupBy;
2545 struct ExprList_item *pTerm = pGB->a;
2546 n = pGB->nExpr;
2547 for(j=0; j<n; j++, pTerm++){
2548 Expr *pE = pTerm->pExpr;
2549 if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable &&
2550 pE->iColumn==pExpr->iColumn ){
2551 pCol->iSorterColumn = j;
2552 break;
2553 }
2554 }
2555 }
2556 if( pCol->iSorterColumn<0 ){
2557 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
2558 }
2559 }
2560 /* There is now an entry for pExpr in pAggInfo->aCol[] (either
2561 ** because it was there before or because we just created it).
2562 ** Convert the pExpr to be a TK_AGG_COLUMN referring to that
2563 ** pAggInfo->aCol[] entry.
2564 */
2565 pExpr->pAggInfo = pAggInfo;
2566 pExpr->op = TK_AGG_COLUMN;
drh7f906d62007-03-12 23:48:52 +00002567 pExpr->iAgg = k;
drh13449892005-09-07 21:22:45 +00002568 break;
2569 } /* endif pExpr->iTable==pItem->iCursor */
2570 } /* end loop over pSrcList */
drh22827922000-06-06 17:27:05 +00002571 }
drh626a8792005-01-17 22:08:19 +00002572 return 1;
drh22827922000-06-06 17:27:05 +00002573 }
2574 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00002575 /* The pNC->nDepth==0 test causes aggregate functions in subqueries
2576 ** to be ignored */
danielk1977a58fdfb2005-02-08 07:50:40 +00002577 if( pNC->nDepth==0 ){
drh13449892005-09-07 21:22:45 +00002578 /* Check to see if pExpr is a duplicate of another aggregate
2579 ** function that is already in the pAggInfo structure
2580 */
2581 struct AggInfo_func *pItem = pAggInfo->aFunc;
2582 for(i=0; i<pAggInfo->nFunc; i++, pItem++){
2583 if( sqlite3ExprCompare(pItem->pExpr, pExpr) ){
danielk1977a58fdfb2005-02-08 07:50:40 +00002584 break;
2585 }
drh22827922000-06-06 17:27:05 +00002586 }
drh13449892005-09-07 21:22:45 +00002587 if( i>=pAggInfo->nFunc ){
2588 /* pExpr is original. Make a new entry in pAggInfo->aFunc[]
2589 */
danielk197714db2662006-01-09 16:12:04 +00002590 u8 enc = ENC(pParse->db);
danielk19771e536952007-08-16 10:09:01 +00002591 i = addAggInfoFunc(pParse->db, pAggInfo);
drh13449892005-09-07 21:22:45 +00002592 if( i>=0 ){
2593 pItem = &pAggInfo->aFunc[i];
2594 pItem->pExpr = pExpr;
2595 pItem->iMem = pParse->nMem++;
2596 pItem->pFunc = sqlite3FindFunction(pParse->db,
drh2646da72005-12-09 20:02:05 +00002597 (char*)pExpr->token.z, pExpr->token.n,
drh13449892005-09-07 21:22:45 +00002598 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
drhfd357972005-09-09 01:33:19 +00002599 if( pExpr->flags & EP_Distinct ){
2600 pItem->iDistinct = pParse->nTab++;
2601 }else{
2602 pItem->iDistinct = -1;
2603 }
drh13449892005-09-07 21:22:45 +00002604 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002605 }
drh13449892005-09-07 21:22:45 +00002606 /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
2607 */
danielk1977a58fdfb2005-02-08 07:50:40 +00002608 pExpr->iAgg = i;
drh13449892005-09-07 21:22:45 +00002609 pExpr->pAggInfo = pAggInfo;
danielk1977a58fdfb2005-02-08 07:50:40 +00002610 return 1;
drh22827922000-06-06 17:27:05 +00002611 }
drh22827922000-06-06 17:27:05 +00002612 }
2613 }
drh13449892005-09-07 21:22:45 +00002614
2615 /* Recursively walk subqueries looking for TK_COLUMN nodes that need
2616 ** to be changed to TK_AGG_COLUMN. But increment nDepth so that
2617 ** TK_AGG_FUNCTION nodes in subqueries will be unchanged.
2618 */
danielk1977a58fdfb2005-02-08 07:50:40 +00002619 if( pExpr->pSelect ){
2620 pNC->nDepth++;
2621 walkSelectExpr(pExpr->pSelect, analyzeAggregate, pNC);
2622 pNC->nDepth--;
2623 }
drh626a8792005-01-17 22:08:19 +00002624 return 0;
2625}
2626
2627/*
2628** Analyze the given expression looking for aggregate functions and
2629** for variables that need to be added to the pParse->aAgg[] array.
2630** Make additional entries to the pParse->aAgg[] array as necessary.
2631**
2632** This routine should only be called after the expression has been
2633** analyzed by sqlite3ExprResolveNames().
2634**
2635** If errors are seen, leave an error message in zErrMsg and return
2636** the number of errors.
2637*/
danielk1977a58fdfb2005-02-08 07:50:40 +00002638int sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
2639 int nErr = pNC->pParse->nErr;
2640 walkExprTree(pExpr, analyzeAggregate, pNC);
2641 return pNC->pParse->nErr - nErr;
drh22827922000-06-06 17:27:05 +00002642}
drh5d9a4af2005-08-30 00:54:01 +00002643
2644/*
2645** Call sqlite3ExprAnalyzeAggregates() for every expression in an
2646** expression list. Return the number of errors.
2647**
2648** If an error is found, the analysis is cut short.
2649*/
2650int sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){
2651 struct ExprList_item *pItem;
2652 int i;
2653 int nErr = 0;
2654 if( pList ){
2655 for(pItem=pList->a, i=0; nErr==0 && i<pList->nExpr; i++, pItem++){
2656 nErr += sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
2657 }
2658 }
2659 return nErr;
2660}