blob: 52a0f29def096a4ca369e80b1212190c49e2ed4f [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**
drh0e359b32008-01-13 19:02:11 +000015** $Id: expr.c,v 1.345 2008/01/13 19:02:11 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){
danielk197739002502007-11-12 09:50:26 +000057 char *zColl = 0; /* Dequoted name of collation sequence */
drh8b4c40d2007-02-01 23:02:45 +000058 CollSeq *pColl;
danielk197739002502007-11-12 09:50:26 +000059 zColl = sqlite3NameFromToken(pParse->db, pName);
60 if( pExpr && zColl ){
61 pColl = sqlite3LocateCollSeq(pParse, zColl, -1);
62 if( pColl ){
63 pExpr->pColl = pColl;
64 pExpr->flags |= EP_ExpCollate;
65 }
drh8b4c40d2007-02-01 23:02:45 +000066 }
danielk197739002502007-11-12 09:50:26 +000067 sqlite3_free(zColl);
drh8b4c40d2007-02-01 23:02:45 +000068 return pExpr;
69}
70
71/*
danielk19770202b292004-06-09 09:55:16 +000072** Return the default collation sequence for the expression pExpr. If
73** there is no default collation type, return 0.
74*/
danielk19777cedc8d2004-06-10 10:50:08 +000075CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
76 CollSeq *pColl = 0;
danielk19770202b292004-06-09 09:55:16 +000077 if( pExpr ){
drh7e09fe02007-06-20 16:13:23 +000078 int op;
danielk19777cedc8d2004-06-10 10:50:08 +000079 pColl = pExpr->pColl;
drh7e09fe02007-06-20 16:13:23 +000080 op = pExpr->op;
81 if( (op==TK_CAST || op==TK_UPLUS) && !pColl ){
danielk19777cedc8d2004-06-10 10:50:08 +000082 return sqlite3ExprCollSeq(pParse, pExpr->pLeft);
danielk19770202b292004-06-09 09:55:16 +000083 }
84 }
danielk19777cedc8d2004-06-10 10:50:08 +000085 if( sqlite3CheckCollSeq(pParse, pColl) ){
86 pColl = 0;
87 }
88 return pColl;
danielk19770202b292004-06-09 09:55:16 +000089}
90
91/*
drh626a8792005-01-17 22:08:19 +000092** pExpr is an operand of a comparison operator. aff2 is the
93** type affinity of the other operand. This routine returns the
drh53db1452004-05-20 13:54:53 +000094** type affinity that should be used for the comparison operator.
95*/
danielk1977e014a832004-05-17 10:48:57 +000096char sqlite3CompareAffinity(Expr *pExpr, char aff2){
danielk1977bf3b7212004-05-18 10:06:24 +000097 char aff1 = sqlite3ExprAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +000098 if( aff1 && aff2 ){
drh8df447f2005-11-01 15:48:24 +000099 /* Both sides of the comparison are columns. If one has numeric
100 ** affinity, use that. Otherwise use no affinity.
danielk1977e014a832004-05-17 10:48:57 +0000101 */
drh8a512562005-11-14 22:29:05 +0000102 if( sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2) ){
danielk1977e014a832004-05-17 10:48:57 +0000103 return SQLITE_AFF_NUMERIC;
104 }else{
105 return SQLITE_AFF_NONE;
106 }
107 }else if( !aff1 && !aff2 ){
drh5f6a87b2004-07-19 00:39:45 +0000108 /* Neither side of the comparison is a column. Compare the
109 ** results directly.
danielk1977e014a832004-05-17 10:48:57 +0000110 */
drh5f6a87b2004-07-19 00:39:45 +0000111 return SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +0000112 }else{
113 /* One side is a column, the other is not. Use the columns affinity. */
drhfe05af82005-07-21 03:14:59 +0000114 assert( aff1==0 || aff2==0 );
danielk1977e014a832004-05-17 10:48:57 +0000115 return (aff1 + aff2);
116 }
117}
118
drh53db1452004-05-20 13:54:53 +0000119/*
120** pExpr is a comparison operator. Return the type affinity that should
121** be applied to both operands prior to doing the comparison.
122*/
danielk1977e014a832004-05-17 10:48:57 +0000123static char comparisonAffinity(Expr *pExpr){
124 char aff;
125 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
126 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
127 pExpr->op==TK_NE );
128 assert( pExpr->pLeft );
danielk1977bf3b7212004-05-18 10:06:24 +0000129 aff = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +0000130 if( pExpr->pRight ){
131 aff = sqlite3CompareAffinity(pExpr->pRight, aff);
132 }
133 else if( pExpr->pSelect ){
134 aff = sqlite3CompareAffinity(pExpr->pSelect->pEList->a[0].pExpr, aff);
135 }
136 else if( !aff ){
drhde087bd2007-02-23 03:00:44 +0000137 aff = SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +0000138 }
139 return aff;
140}
141
142/*
143** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
144** idx_affinity is the affinity of an indexed column. Return true
145** if the index with affinity idx_affinity may be used to implement
146** the comparison in pExpr.
147*/
148int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){
149 char aff = comparisonAffinity(pExpr);
drh8a512562005-11-14 22:29:05 +0000150 switch( aff ){
151 case SQLITE_AFF_NONE:
152 return 1;
153 case SQLITE_AFF_TEXT:
154 return idx_affinity==SQLITE_AFF_TEXT;
155 default:
156 return sqlite3IsNumericAffinity(idx_affinity);
157 }
danielk1977e014a832004-05-17 10:48:57 +0000158}
159
danielk1977a37cdde2004-05-16 11:15:36 +0000160/*
drh35573352008-01-08 23:54:25 +0000161** Return the P5 value that should be used for a binary comparison
danielk1977a37cdde2004-05-16 11:15:36 +0000162** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
danielk1977a37cdde2004-05-16 11:15:36 +0000163*/
drh35573352008-01-08 23:54:25 +0000164static u8 binaryCompareP5(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){
165 u8 aff = (char)sqlite3ExprAffinity(pExpr2);
166 aff = sqlite3CompareAffinity(pExpr1, aff) | jumpIfNull;
167 return aff;
danielk1977a37cdde2004-05-16 11:15:36 +0000168}
169
drha2e00042002-01-22 03:13:42 +0000170/*
danielk19770202b292004-06-09 09:55:16 +0000171** Return a pointer to the collation sequence that should be used by
172** a binary comparison operator comparing pLeft and pRight.
173**
174** If the left hand expression has a collating sequence type, then it is
175** used. Otherwise the collation sequence for the right hand expression
176** is used, or the default (BINARY) if neither expression has a collating
177** type.
danielk1977bcbb04e2007-05-29 12:11:29 +0000178**
179** Argument pRight (but not pLeft) may be a null pointer. In this case,
180** it is not considered.
danielk19770202b292004-06-09 09:55:16 +0000181*/
drh0a0e1312007-08-07 17:04:59 +0000182CollSeq *sqlite3BinaryCompareCollSeq(
danielk1977bcbb04e2007-05-29 12:11:29 +0000183 Parse *pParse,
184 Expr *pLeft,
185 Expr *pRight
186){
drhec41dda2007-02-07 13:09:45 +0000187 CollSeq *pColl;
188 assert( pLeft );
drhec41dda2007-02-07 13:09:45 +0000189 if( pLeft->flags & EP_ExpCollate ){
190 assert( pLeft->pColl );
191 pColl = pLeft->pColl;
danielk1977bcbb04e2007-05-29 12:11:29 +0000192 }else if( pRight && pRight->flags & EP_ExpCollate ){
drhec41dda2007-02-07 13:09:45 +0000193 assert( pRight->pColl );
194 pColl = pRight->pColl;
195 }else{
196 pColl = sqlite3ExprCollSeq(pParse, pLeft);
197 if( !pColl ){
198 pColl = sqlite3ExprCollSeq(pParse, pRight);
199 }
danielk19770202b292004-06-09 09:55:16 +0000200 }
201 return pColl;
202}
203
204/*
drhbe5c89a2004-07-26 00:31:09 +0000205** Generate code for a comparison operator.
206*/
207static int codeCompare(
208 Parse *pParse, /* The parsing (and code generating) context */
209 Expr *pLeft, /* The left operand */
210 Expr *pRight, /* The right operand */
211 int opcode, /* The comparison opcode */
drh35573352008-01-08 23:54:25 +0000212 int in1, int in2, /* Register holding operands */
drhbe5c89a2004-07-26 00:31:09 +0000213 int dest, /* Jump here if true. */
214 int jumpIfNull /* If true, jump if either operand is NULL */
215){
drh35573352008-01-08 23:54:25 +0000216 int p5;
217 int addr;
218 CollSeq *p4;
219
220 p4 = sqlite3BinaryCompareCollSeq(pParse, pLeft, pRight);
221 p5 = binaryCompareP5(pLeft, pRight, jumpIfNull);
222 addr = sqlite3VdbeAddOp4(pParse->pVdbe, opcode, in2, dest, in1,
223 (void*)p4, P4_COLLSEQ);
224 sqlite3VdbeChangeP5(pParse->pVdbe, p5);
225 return addr;
drhbe5c89a2004-07-26 00:31:09 +0000226}
227
228/*
drha76b5df2002-02-23 02:32:10 +0000229** Construct a new expression node and return a pointer to it. Memory
drh17435752007-08-16 04:30:38 +0000230** for this node is obtained from sqlite3_malloc(). The calling function
drha76b5df2002-02-23 02:32:10 +0000231** is responsible for making sure the node eventually gets freed.
232*/
drh17435752007-08-16 04:30:38 +0000233Expr *sqlite3Expr(
danielk1977a1644fd2007-08-29 12:31:25 +0000234 sqlite3 *db, /* Handle for sqlite3DbMallocZero() (may be null) */
drh17435752007-08-16 04:30:38 +0000235 int op, /* Expression opcode */
236 Expr *pLeft, /* Left operand */
237 Expr *pRight, /* Right operand */
238 const Token *pToken /* Argument token */
239){
drha76b5df2002-02-23 02:32:10 +0000240 Expr *pNew;
danielk1977a1644fd2007-08-29 12:31:25 +0000241 pNew = sqlite3DbMallocZero(db, sizeof(Expr));
drha76b5df2002-02-23 02:32:10 +0000242 if( pNew==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000243 /* When malloc fails, delete pLeft and pRight. Expressions passed to
244 ** this function must always be allocated with sqlite3Expr() for this
245 ** reason.
246 */
247 sqlite3ExprDelete(pLeft);
248 sqlite3ExprDelete(pRight);
drha76b5df2002-02-23 02:32:10 +0000249 return 0;
250 }
251 pNew->op = op;
252 pNew->pLeft = pLeft;
253 pNew->pRight = pRight;
danielk1977a58fdfb2005-02-08 07:50:40 +0000254 pNew->iAgg = -1;
drha76b5df2002-02-23 02:32:10 +0000255 if( pToken ){
drh4b59ab52002-08-24 18:24:51 +0000256 assert( pToken->dyn==0 );
drh145716b2004-09-24 12:24:06 +0000257 pNew->span = pNew->token = *pToken;
drha34001c2007-02-02 12:44:37 +0000258 }else if( pLeft ){
259 if( pRight ){
260 sqlite3ExprSpan(pNew, &pLeft->span, &pRight->span);
drh5ffb3ac2007-04-18 17:07:57 +0000261 if( pRight->flags & EP_ExpCollate ){
drha34001c2007-02-02 12:44:37 +0000262 pNew->flags |= EP_ExpCollate;
263 pNew->pColl = pRight->pColl;
264 }
265 }
drh5ffb3ac2007-04-18 17:07:57 +0000266 if( pLeft->flags & EP_ExpCollate ){
drha34001c2007-02-02 12:44:37 +0000267 pNew->flags |= EP_ExpCollate;
268 pNew->pColl = pLeft->pColl;
269 }
drha76b5df2002-02-23 02:32:10 +0000270 }
danielk1977fc976062007-05-10 10:46:56 +0000271
272 sqlite3ExprSetHeight(pNew);
drha76b5df2002-02-23 02:32:10 +0000273 return pNew;
274}
275
276/*
drh17435752007-08-16 04:30:38 +0000277** Works like sqlite3Expr() except that it takes an extra Parse*
278** argument and notifies the associated connection object if malloc fails.
drh206f3d92006-07-11 13:15:08 +0000279*/
drh17435752007-08-16 04:30:38 +0000280Expr *sqlite3PExpr(
281 Parse *pParse, /* Parsing context */
282 int op, /* Expression opcode */
283 Expr *pLeft, /* Left operand */
284 Expr *pRight, /* Right operand */
285 const Token *pToken /* Argument token */
286){
danielk1977a1644fd2007-08-29 12:31:25 +0000287 return sqlite3Expr(pParse->db, op, pLeft, pRight, pToken);
drh206f3d92006-07-11 13:15:08 +0000288}
289
290/*
drh4e0cff62004-11-05 05:10:28 +0000291** When doing a nested parse, you can include terms in an expression
drhb7654112008-01-12 12:48:07 +0000292** that look like this: #1 #2 ... These terms refer to registers
293** in the virtual machine. #N is the N-th register.
drh4e0cff62004-11-05 05:10:28 +0000294**
295** This routine is called by the parser to deal with on of those terms.
296** It immediately generates code to store the value in a memory location.
297** The returns an expression that will code to extract the value from
298** that memory location as needed.
299*/
300Expr *sqlite3RegisterExpr(Parse *pParse, Token *pToken){
301 Vdbe *v = pParse->pVdbe;
302 Expr *p;
drh4e0cff62004-11-05 05:10:28 +0000303 if( pParse->nested==0 ){
304 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", pToken);
danielk1977a1644fd2007-08-29 12:31:25 +0000305 return sqlite3PExpr(pParse, TK_NULL, 0, 0, 0);
drh4e0cff62004-11-05 05:10:28 +0000306 }
drhbb7ac002005-08-12 22:58:53 +0000307 if( v==0 ) return 0;
danielk1977a1644fd2007-08-29 12:31:25 +0000308 p = sqlite3PExpr(pParse, TK_REGISTER, 0, 0, pToken);
drh73c42a12004-11-20 18:13:10 +0000309 if( p==0 ){
310 return 0; /* Malloc failed */
311 }
drhb7654112008-01-12 12:48:07 +0000312 p->iTable = atoi((char*)&pToken->z[1]);
drh4e0cff62004-11-05 05:10:28 +0000313 return p;
314}
315
316/*
drh91bb0ee2004-09-01 03:06:34 +0000317** Join two expressions using an AND operator. If either expression is
318** NULL, then just return the other expression.
319*/
danielk19771e536952007-08-16 10:09:01 +0000320Expr *sqlite3ExprAnd(sqlite3 *db, Expr *pLeft, Expr *pRight){
drh91bb0ee2004-09-01 03:06:34 +0000321 if( pLeft==0 ){
322 return pRight;
323 }else if( pRight==0 ){
324 return pLeft;
325 }else{
danielk1977880c15b2007-09-01 18:24:55 +0000326 return sqlite3Expr(db, TK_AND, pLeft, pRight, 0);
drh91bb0ee2004-09-01 03:06:34 +0000327 }
328}
329
330/*
drh6977fea2002-10-22 23:38:04 +0000331** Set the Expr.span field of the given expression to span all
drha76b5df2002-02-23 02:32:10 +0000332** text between the two given tokens.
333*/
danielk19774adee202004-05-08 08:23:19 +0000334void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
drh4efc4752004-01-16 15:55:37 +0000335 assert( pRight!=0 );
336 assert( pLeft!=0 );
drhf3a65f72007-08-22 20:18:21 +0000337 if( pExpr && pRight->z && pLeft->z ){
drhad6d9462004-09-19 02:15:24 +0000338 assert( pLeft->dyn==0 || pLeft->z[pLeft->n]==0 );
drh145716b2004-09-24 12:24:06 +0000339 if( pLeft->dyn==0 && pRight->dyn==0 ){
drh6977fea2002-10-22 23:38:04 +0000340 pExpr->span.z = pLeft->z;
drh97903fe2005-05-24 20:19:57 +0000341 pExpr->span.n = pRight->n + (pRight->z - pLeft->z);
drh4b59ab52002-08-24 18:24:51 +0000342 }else{
drh6977fea2002-10-22 23:38:04 +0000343 pExpr->span.z = 0;
drh4b59ab52002-08-24 18:24:51 +0000344 }
drha76b5df2002-02-23 02:32:10 +0000345 }
346}
347
348/*
349** Construct a new expression node for a function with multiple
350** arguments.
351*/
drh17435752007-08-16 04:30:38 +0000352Expr *sqlite3ExprFunction(Parse *pParse, ExprList *pList, Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000353 Expr *pNew;
danielk19774b202ae2006-01-23 05:50:58 +0000354 assert( pToken );
drh17435752007-08-16 04:30:38 +0000355 pNew = sqlite3DbMallocZero(pParse->db, sizeof(Expr) );
drha76b5df2002-02-23 02:32:10 +0000356 if( pNew==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000357 sqlite3ExprListDelete(pList); /* Avoid leaking memory when malloc fails */
drha76b5df2002-02-23 02:32:10 +0000358 return 0;
359 }
360 pNew->op = TK_FUNCTION;
361 pNew->pList = pList;
danielk19774b202ae2006-01-23 05:50:58 +0000362 assert( pToken->dyn==0 );
363 pNew->token = *pToken;
drh6977fea2002-10-22 23:38:04 +0000364 pNew->span = pNew->token;
danielk1977fc976062007-05-10 10:46:56 +0000365
366 sqlite3ExprSetHeight(pNew);
drha76b5df2002-02-23 02:32:10 +0000367 return pNew;
368}
369
370/*
drhfa6bc002004-09-07 16:19:52 +0000371** Assign a variable number to an expression that encodes a wildcard
372** in the original SQL statement.
373**
374** Wildcards consisting of a single "?" are assigned the next sequential
375** variable number.
376**
377** Wildcards of the form "?nnn" are assigned the number "nnn". We make
378** sure "nnn" is not too be to avoid a denial of service attack when
379** the SQL statement comes from an external source.
380**
381** Wildcards of the form ":aaa" or "$aaa" are assigned the same number
382** as the previous instance of the same wildcard. Or if this is the first
383** instance of the wildcard, the next sequenial variable number is
384** assigned.
385*/
386void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){
387 Token *pToken;
drh17435752007-08-16 04:30:38 +0000388 sqlite3 *db = pParse->db;
389
drhfa6bc002004-09-07 16:19:52 +0000390 if( pExpr==0 ) return;
391 pToken = &pExpr->token;
392 assert( pToken->n>=1 );
393 assert( pToken->z!=0 );
394 assert( pToken->z[0]!=0 );
395 if( pToken->n==1 ){
396 /* Wildcard of the form "?". Assign the next variable number */
397 pExpr->iTable = ++pParse->nVar;
398 }else if( pToken->z[0]=='?' ){
399 /* Wildcard of the form "?nnn". Convert "nnn" to an integer and
400 ** use it as the variable number */
401 int i;
drh2646da72005-12-09 20:02:05 +0000402 pExpr->iTable = i = atoi((char*)&pToken->z[1]);
drhfa6bc002004-09-07 16:19:52 +0000403 if( i<1 || i>SQLITE_MAX_VARIABLE_NUMBER ){
404 sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
405 SQLITE_MAX_VARIABLE_NUMBER);
406 }
407 if( i>pParse->nVar ){
408 pParse->nVar = i;
409 }
410 }else{
411 /* Wildcards of the form ":aaa" or "$aaa". Reuse the same variable
412 ** number as the prior appearance of the same name, or if the name
413 ** has never appeared before, reuse the same variable number
414 */
415 int i, n;
416 n = pToken->n;
417 for(i=0; i<pParse->nVarExpr; i++){
418 Expr *pE;
419 if( (pE = pParse->apVarExpr[i])!=0
420 && pE->token.n==n
421 && memcmp(pE->token.z, pToken->z, n)==0 ){
422 pExpr->iTable = pE->iTable;
423 break;
424 }
425 }
426 if( i>=pParse->nVarExpr ){
427 pExpr->iTable = ++pParse->nVar;
428 if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){
429 pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10;
drh17435752007-08-16 04:30:38 +0000430 pParse->apVarExpr =
431 sqlite3DbReallocOrFree(
432 db,
433 pParse->apVarExpr,
434 pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0])
435 );
drhfa6bc002004-09-07 16:19:52 +0000436 }
drh17435752007-08-16 04:30:38 +0000437 if( !db->mallocFailed ){
drhfa6bc002004-09-07 16:19:52 +0000438 assert( pParse->apVarExpr!=0 );
439 pParse->apVarExpr[pParse->nVarExpr++] = pExpr;
440 }
441 }
442 }
danielk1977832b2662007-05-09 11:37:22 +0000443 if( !pParse->nErr && pParse->nVar>SQLITE_MAX_VARIABLE_NUMBER ){
444 sqlite3ErrorMsg(pParse, "too many SQL variables");
445 }
drhfa6bc002004-09-07 16:19:52 +0000446}
447
448/*
drha2e00042002-01-22 03:13:42 +0000449** Recursively delete an expression tree.
450*/
danielk19774adee202004-05-08 08:23:19 +0000451void sqlite3ExprDelete(Expr *p){
drha2e00042002-01-22 03:13:42 +0000452 if( p==0 ) return;
drh17435752007-08-16 04:30:38 +0000453 if( p->span.dyn ) sqlite3_free((char*)p->span.z);
454 if( p->token.dyn ) sqlite3_free((char*)p->token.z);
danielk19774adee202004-05-08 08:23:19 +0000455 sqlite3ExprDelete(p->pLeft);
456 sqlite3ExprDelete(p->pRight);
457 sqlite3ExprListDelete(p->pList);
458 sqlite3SelectDelete(p->pSelect);
drh17435752007-08-16 04:30:38 +0000459 sqlite3_free(p);
drha2e00042002-01-22 03:13:42 +0000460}
461
drhd2687b72005-08-12 22:56:09 +0000462/*
463** The Expr.token field might be a string literal that is quoted.
464** If so, remove the quotation marks.
465*/
drh17435752007-08-16 04:30:38 +0000466void sqlite3DequoteExpr(sqlite3 *db, Expr *p){
drhd2687b72005-08-12 22:56:09 +0000467 if( ExprHasAnyProperty(p, EP_Dequoted) ){
468 return;
469 }
470 ExprSetProperty(p, EP_Dequoted);
471 if( p->token.dyn==0 ){
drh17435752007-08-16 04:30:38 +0000472 sqlite3TokenCopy(db, &p->token, &p->token);
drhd2687b72005-08-12 22:56:09 +0000473 }
474 sqlite3Dequote((char*)p->token.z);
475}
476
drha76b5df2002-02-23 02:32:10 +0000477
478/*
drhff78bd22002-02-27 01:47:11 +0000479** The following group of routines make deep copies of expressions,
480** expression lists, ID lists, and select statements. The copies can
481** be deleted (by being passed to their respective ...Delete() routines)
482** without effecting the originals.
483**
danielk19774adee202004-05-08 08:23:19 +0000484** The expression list, ID, and source lists return by sqlite3ExprListDup(),
485** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
drhad3cab52002-05-24 02:04:32 +0000486** by subsequent calls to sqlite*ListAppend() routines.
drhff78bd22002-02-27 01:47:11 +0000487**
drhad3cab52002-05-24 02:04:32 +0000488** Any tables that the SrcList might point to are not duplicated.
drhff78bd22002-02-27 01:47:11 +0000489*/
danielk19771e536952007-08-16 10:09:01 +0000490Expr *sqlite3ExprDup(sqlite3 *db, Expr *p){
drhff78bd22002-02-27 01:47:11 +0000491 Expr *pNew;
492 if( p==0 ) return 0;
drh17435752007-08-16 04:30:38 +0000493 pNew = sqlite3DbMallocRaw(db, sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000494 if( pNew==0 ) return 0;
drh3b167c72002-06-28 12:18:47 +0000495 memcpy(pNew, p, sizeof(*pNew));
drh6977fea2002-10-22 23:38:04 +0000496 if( p->token.z!=0 ){
drh17435752007-08-16 04:30:38 +0000497 pNew->token.z = (u8*)sqlite3DbStrNDup(db, (char*)p->token.z, p->token.n);
drh4b59ab52002-08-24 18:24:51 +0000498 pNew->token.dyn = 1;
499 }else{
drh4efc4752004-01-16 15:55:37 +0000500 assert( pNew->token.z==0 );
drh4b59ab52002-08-24 18:24:51 +0000501 }
drh6977fea2002-10-22 23:38:04 +0000502 pNew->span.z = 0;
drh17435752007-08-16 04:30:38 +0000503 pNew->pLeft = sqlite3ExprDup(db, p->pLeft);
504 pNew->pRight = sqlite3ExprDup(db, p->pRight);
505 pNew->pList = sqlite3ExprListDup(db, p->pList);
506 pNew->pSelect = sqlite3SelectDup(db, p->pSelect);
drhff78bd22002-02-27 01:47:11 +0000507 return pNew;
508}
drh17435752007-08-16 04:30:38 +0000509void sqlite3TokenCopy(sqlite3 *db, Token *pTo, Token *pFrom){
510 if( pTo->dyn ) sqlite3_free((char*)pTo->z);
drh4b59ab52002-08-24 18:24:51 +0000511 if( pFrom->z ){
512 pTo->n = pFrom->n;
drh17435752007-08-16 04:30:38 +0000513 pTo->z = (u8*)sqlite3DbStrNDup(db, (char*)pFrom->z, pFrom->n);
drh4b59ab52002-08-24 18:24:51 +0000514 pTo->dyn = 1;
515 }else{
drh4b59ab52002-08-24 18:24:51 +0000516 pTo->z = 0;
drh4b59ab52002-08-24 18:24:51 +0000517 }
518}
drh17435752007-08-16 04:30:38 +0000519ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p){
drhff78bd22002-02-27 01:47:11 +0000520 ExprList *pNew;
drh145716b2004-09-24 12:24:06 +0000521 struct ExprList_item *pItem, *pOldItem;
drhff78bd22002-02-27 01:47:11 +0000522 int i;
523 if( p==0 ) return 0;
drh17435752007-08-16 04:30:38 +0000524 pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +0000525 if( pNew==0 ) return 0;
danielk197731dad9d2007-08-16 11:36:15 +0000526 pNew->iECursor = 0;
drh4305d102003-07-30 12:34:12 +0000527 pNew->nExpr = pNew->nAlloc = p->nExpr;
drh17435752007-08-16 04:30:38 +0000528 pNew->a = pItem = sqlite3DbMallocRaw(db, p->nExpr*sizeof(p->a[0]) );
danielk1977e0048402004-06-15 16:51:01 +0000529 if( pItem==0 ){
drh17435752007-08-16 04:30:38 +0000530 sqlite3_free(pNew);
danielk1977e0048402004-06-15 16:51:01 +0000531 return 0;
532 }
drh145716b2004-09-24 12:24:06 +0000533 pOldItem = p->a;
534 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
drh4b59ab52002-08-24 18:24:51 +0000535 Expr *pNewExpr, *pOldExpr;
drh17435752007-08-16 04:30:38 +0000536 pItem->pExpr = pNewExpr = sqlite3ExprDup(db, pOldExpr = pOldItem->pExpr);
drh6977fea2002-10-22 23:38:04 +0000537 if( pOldExpr->span.z!=0 && pNewExpr ){
538 /* Always make a copy of the span for top-level expressions in the
drh4b59ab52002-08-24 18:24:51 +0000539 ** expression list. The logic in SELECT processing that determines
540 ** the names of columns in the result set needs this information */
drh17435752007-08-16 04:30:38 +0000541 sqlite3TokenCopy(db, &pNewExpr->span, &pOldExpr->span);
drh4b59ab52002-08-24 18:24:51 +0000542 }
drh1f3e9052002-10-31 00:09:39 +0000543 assert( pNewExpr==0 || pNewExpr->span.z!=0
drh6f7adc82006-01-11 21:41:20 +0000544 || pOldExpr->span.z==0
drh17435752007-08-16 04:30:38 +0000545 || db->mallocFailed );
546 pItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
drh145716b2004-09-24 12:24:06 +0000547 pItem->sortOrder = pOldItem->sortOrder;
548 pItem->isAgg = pOldItem->isAgg;
drh3e7bc9c2004-02-21 19:17:17 +0000549 pItem->done = 0;
drhff78bd22002-02-27 01:47:11 +0000550 }
551 return pNew;
552}
danielk197793758c82005-01-21 08:13:14 +0000553
554/*
555** If cursors, triggers, views and subqueries are all omitted from
556** the build, then none of the following routines, except for
557** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes
558** called with a NULL argument.
559*/
danielk19776a67fe82005-02-04 04:07:16 +0000560#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \
561 || !defined(SQLITE_OMIT_SUBQUERY)
drh17435752007-08-16 04:30:38 +0000562SrcList *sqlite3SrcListDup(sqlite3 *db, SrcList *p){
drhad3cab52002-05-24 02:04:32 +0000563 SrcList *pNew;
564 int i;
drh113088e2003-03-20 01:16:58 +0000565 int nByte;
drhad3cab52002-05-24 02:04:32 +0000566 if( p==0 ) return 0;
drh113088e2003-03-20 01:16:58 +0000567 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
drh17435752007-08-16 04:30:38 +0000568 pNew = sqlite3DbMallocRaw(db, nByte );
drhad3cab52002-05-24 02:04:32 +0000569 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000570 pNew->nSrc = pNew->nAlloc = p->nSrc;
drhad3cab52002-05-24 02:04:32 +0000571 for(i=0; i<p->nSrc; i++){
drh4efc4752004-01-16 15:55:37 +0000572 struct SrcList_item *pNewItem = &pNew->a[i];
573 struct SrcList_item *pOldItem = &p->a[i];
drhed8a3bb2005-06-06 21:19:56 +0000574 Table *pTab;
drh17435752007-08-16 04:30:38 +0000575 pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase);
576 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
577 pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias);
drh4efc4752004-01-16 15:55:37 +0000578 pNewItem->jointype = pOldItem->jointype;
579 pNewItem->iCursor = pOldItem->iCursor;
danielk19771787cca2006-02-10 07:07:14 +0000580 pNewItem->isPopulated = pOldItem->isPopulated;
drhed8a3bb2005-06-06 21:19:56 +0000581 pTab = pNewItem->pTab = pOldItem->pTab;
582 if( pTab ){
583 pTab->nRef++;
danielk1977a1cb1832005-02-12 08:59:55 +0000584 }
drh17435752007-08-16 04:30:38 +0000585 pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect);
586 pNewItem->pOn = sqlite3ExprDup(db, pOldItem->pOn);
587 pNewItem->pUsing = sqlite3IdListDup(db, pOldItem->pUsing);
danielk19776c18b6e2005-01-30 09:17:58 +0000588 pNewItem->colUsed = pOldItem->colUsed;
drhad3cab52002-05-24 02:04:32 +0000589 }
590 return pNew;
591}
drh17435752007-08-16 04:30:38 +0000592IdList *sqlite3IdListDup(sqlite3 *db, IdList *p){
drhff78bd22002-02-27 01:47:11 +0000593 IdList *pNew;
594 int i;
595 if( p==0 ) return 0;
drh17435752007-08-16 04:30:38 +0000596 pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +0000597 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000598 pNew->nId = pNew->nAlloc = p->nId;
drh17435752007-08-16 04:30:38 +0000599 pNew->a = sqlite3DbMallocRaw(db, p->nId*sizeof(p->a[0]) );
danielk1977d5d56522005-03-16 12:15:20 +0000600 if( pNew->a==0 ){
drh17435752007-08-16 04:30:38 +0000601 sqlite3_free(pNew);
danielk1977d5d56522005-03-16 12:15:20 +0000602 return 0;
603 }
drhff78bd22002-02-27 01:47:11 +0000604 for(i=0; i<p->nId; i++){
drh4efc4752004-01-16 15:55:37 +0000605 struct IdList_item *pNewItem = &pNew->a[i];
606 struct IdList_item *pOldItem = &p->a[i];
drh17435752007-08-16 04:30:38 +0000607 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
drh4efc4752004-01-16 15:55:37 +0000608 pNewItem->idx = pOldItem->idx;
drhff78bd22002-02-27 01:47:11 +0000609 }
610 return pNew;
611}
drh17435752007-08-16 04:30:38 +0000612Select *sqlite3SelectDup(sqlite3 *db, Select *p){
drhff78bd22002-02-27 01:47:11 +0000613 Select *pNew;
614 if( p==0 ) return 0;
drh17435752007-08-16 04:30:38 +0000615 pNew = sqlite3DbMallocRaw(db, sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000616 if( pNew==0 ) return 0;
617 pNew->isDistinct = p->isDistinct;
drh17435752007-08-16 04:30:38 +0000618 pNew->pEList = sqlite3ExprListDup(db, p->pEList);
619 pNew->pSrc = sqlite3SrcListDup(db, p->pSrc);
620 pNew->pWhere = sqlite3ExprDup(db, p->pWhere);
621 pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy);
622 pNew->pHaving = sqlite3ExprDup(db, p->pHaving);
623 pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy);
drhff78bd22002-02-27 01:47:11 +0000624 pNew->op = p->op;
drh17435752007-08-16 04:30:38 +0000625 pNew->pPrior = sqlite3SelectDup(db, p->pPrior);
626 pNew->pLimit = sqlite3ExprDup(db, p->pLimit);
627 pNew->pOffset = sqlite3ExprDup(db, p->pOffset);
drh7b58dae2003-07-20 01:16:46 +0000628 pNew->iLimit = -1;
629 pNew->iOffset = -1;
danielk1977a1cb1832005-02-12 08:59:55 +0000630 pNew->isResolved = p->isResolved;
631 pNew->isAgg = p->isAgg;
drhb9bb7c12006-06-11 23:41:55 +0000632 pNew->usesEphm = 0;
drh8e647b82005-09-23 21:11:53 +0000633 pNew->disallowOrderBy = 0;
drh0342b1f2005-09-01 03:07:44 +0000634 pNew->pRightmost = 0;
drhb9bb7c12006-06-11 23:41:55 +0000635 pNew->addrOpenEphm[0] = -1;
636 pNew->addrOpenEphm[1] = -1;
637 pNew->addrOpenEphm[2] = -1;
drhff78bd22002-02-27 01:47:11 +0000638 return pNew;
639}
danielk197793758c82005-01-21 08:13:14 +0000640#else
drh17435752007-08-16 04:30:38 +0000641Select *sqlite3SelectDup(sqlite3 *db, Select *p){
danielk197793758c82005-01-21 08:13:14 +0000642 assert( p==0 );
643 return 0;
644}
645#endif
drhff78bd22002-02-27 01:47:11 +0000646
647
648/*
drha76b5df2002-02-23 02:32:10 +0000649** Add a new element to the end of an expression list. If pList is
650** initially NULL, then create a new expression list.
651*/
drh17435752007-08-16 04:30:38 +0000652ExprList *sqlite3ExprListAppend(
653 Parse *pParse, /* Parsing context */
654 ExprList *pList, /* List to which to append. Might be NULL */
655 Expr *pExpr, /* Expression to be appended */
656 Token *pName /* AS keyword for the expression */
657){
658 sqlite3 *db = pParse->db;
drha76b5df2002-02-23 02:32:10 +0000659 if( pList==0 ){
drh17435752007-08-16 04:30:38 +0000660 pList = sqlite3DbMallocZero(db, sizeof(ExprList) );
drha76b5df2002-02-23 02:32:10 +0000661 if( pList==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000662 goto no_mem;
drha76b5df2002-02-23 02:32:10 +0000663 }
drh4efc4752004-01-16 15:55:37 +0000664 assert( pList->nAlloc==0 );
drha76b5df2002-02-23 02:32:10 +0000665 }
drh4305d102003-07-30 12:34:12 +0000666 if( pList->nAlloc<=pList->nExpr ){
danielk1977d5d56522005-03-16 12:15:20 +0000667 struct ExprList_item *a;
668 int n = pList->nAlloc*2 + 4;
danielk197726783a52007-08-29 14:06:22 +0000669 a = sqlite3DbRealloc(db, pList->a, n*sizeof(pList->a[0]));
danielk1977d5d56522005-03-16 12:15:20 +0000670 if( a==0 ){
671 goto no_mem;
drha76b5df2002-02-23 02:32:10 +0000672 }
danielk1977d5d56522005-03-16 12:15:20 +0000673 pList->a = a;
674 pList->nAlloc = n;
drha76b5df2002-02-23 02:32:10 +0000675 }
drh4efc4752004-01-16 15:55:37 +0000676 assert( pList->a!=0 );
677 if( pExpr || pName ){
678 struct ExprList_item *pItem = &pList->a[pList->nExpr++];
679 memset(pItem, 0, sizeof(*pItem));
drh17435752007-08-16 04:30:38 +0000680 pItem->zName = sqlite3NameFromToken(db, pName);
danielk1977e94ddc92005-03-21 03:53:38 +0000681 pItem->pExpr = pExpr;
drha76b5df2002-02-23 02:32:10 +0000682 }
683 return pList;
danielk1977d5d56522005-03-16 12:15:20 +0000684
685no_mem:
686 /* Avoid leaking memory if malloc has failed. */
687 sqlite3ExprDelete(pExpr);
688 sqlite3ExprListDelete(pList);
689 return 0;
drha76b5df2002-02-23 02:32:10 +0000690}
691
692/*
danielk19777a15a4b2007-05-08 17:54:43 +0000693** If the expression list pEList contains more than iLimit elements,
694** leave an error message in pParse.
695*/
696void sqlite3ExprListCheckLength(
697 Parse *pParse,
698 ExprList *pEList,
699 int iLimit,
700 const char *zObject
701){
danielk1977b4fc6792007-05-08 18:04:46 +0000702 if( pEList && pEList->nExpr>iLimit ){
danielk19777a15a4b2007-05-08 17:54:43 +0000703 sqlite3ErrorMsg(pParse, "too many columns in %s", zObject);
704 }
705}
706
danielk1977fc976062007-05-10 10:46:56 +0000707
danielk1977e6a58a42007-08-31 17:42:48 +0000708#if defined(SQLITE_TEST) || SQLITE_MAX_EXPR_DEPTH>0
danielk1977fc976062007-05-10 10:46:56 +0000709/* The following three functions, heightOfExpr(), heightOfExprList()
710** and heightOfSelect(), are used to determine the maximum height
711** of any expression tree referenced by the structure passed as the
712** first argument.
713**
714** If this maximum height is greater than the current value pointed
715** to by pnHeight, the second parameter, then set *pnHeight to that
716** value.
717*/
718static void heightOfExpr(Expr *p, int *pnHeight){
719 if( p ){
720 if( p->nHeight>*pnHeight ){
721 *pnHeight = p->nHeight;
722 }
723 }
724}
725static void heightOfExprList(ExprList *p, int *pnHeight){
726 if( p ){
727 int i;
728 for(i=0; i<p->nExpr; i++){
729 heightOfExpr(p->a[i].pExpr, pnHeight);
730 }
731 }
732}
733static void heightOfSelect(Select *p, int *pnHeight){
734 if( p ){
735 heightOfExpr(p->pWhere, pnHeight);
736 heightOfExpr(p->pHaving, pnHeight);
737 heightOfExpr(p->pLimit, pnHeight);
738 heightOfExpr(p->pOffset, pnHeight);
739 heightOfExprList(p->pEList, pnHeight);
740 heightOfExprList(p->pGroupBy, pnHeight);
741 heightOfExprList(p->pOrderBy, pnHeight);
742 heightOfSelect(p->pPrior, pnHeight);
743 }
744}
745
746/*
747** Set the Expr.nHeight variable in the structure passed as an
748** argument. An expression with no children, Expr.pList or
749** Expr.pSelect member has a height of 1. Any other expression
750** has a height equal to the maximum height of any other
751** referenced Expr plus one.
752*/
753void sqlite3ExprSetHeight(Expr *p){
754 int nHeight = 0;
755 heightOfExpr(p->pLeft, &nHeight);
756 heightOfExpr(p->pRight, &nHeight);
757 heightOfExprList(p->pList, &nHeight);
758 heightOfSelect(p->pSelect, &nHeight);
759 p->nHeight = nHeight + 1;
760}
761
762/*
763** Return the maximum height of any expression tree referenced
764** by the select statement passed as an argument.
765*/
766int sqlite3SelectExprHeight(Select *p){
767 int nHeight = 0;
768 heightOfSelect(p, &nHeight);
769 return nHeight;
770}
771#endif
772
danielk19777a15a4b2007-05-08 17:54:43 +0000773/*
drha76b5df2002-02-23 02:32:10 +0000774** Delete an entire expression list.
775*/
danielk19774adee202004-05-08 08:23:19 +0000776void sqlite3ExprListDelete(ExprList *pList){
drha76b5df2002-02-23 02:32:10 +0000777 int i;
drhbe5c89a2004-07-26 00:31:09 +0000778 struct ExprList_item *pItem;
drha76b5df2002-02-23 02:32:10 +0000779 if( pList==0 ) return;
drh1bdd9b52004-04-23 17:04:44 +0000780 assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
781 assert( pList->nExpr<=pList->nAlloc );
drhbe5c89a2004-07-26 00:31:09 +0000782 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
783 sqlite3ExprDelete(pItem->pExpr);
drh17435752007-08-16 04:30:38 +0000784 sqlite3_free(pItem->zName);
drha76b5df2002-02-23 02:32:10 +0000785 }
drh17435752007-08-16 04:30:38 +0000786 sqlite3_free(pList->a);
787 sqlite3_free(pList);
drha76b5df2002-02-23 02:32:10 +0000788}
789
790/*
drh626a8792005-01-17 22:08:19 +0000791** Walk an expression tree. Call xFunc for each node visited.
drh73b211a2005-01-18 04:00:42 +0000792**
drh626a8792005-01-17 22:08:19 +0000793** The return value from xFunc determines whether the tree walk continues.
794** 0 means continue walking the tree. 1 means do not walk children
795** of the current node but continue with siblings. 2 means abandon
796** the tree walk completely.
797**
798** The return value from this routine is 1 to abandon the tree walk
799** and 0 to continue.
drh87abf5c2005-08-25 12:45:04 +0000800**
801** NOTICE: This routine does *not* descend into subqueries.
drh626a8792005-01-17 22:08:19 +0000802*/
danielk1977a58fdfb2005-02-08 07:50:40 +0000803static int walkExprList(ExprList *, int (*)(void *, Expr*), void *);
drh626a8792005-01-17 22:08:19 +0000804static int walkExprTree(Expr *pExpr, int (*xFunc)(void*,Expr*), void *pArg){
drh626a8792005-01-17 22:08:19 +0000805 int rc;
806 if( pExpr==0 ) return 0;
807 rc = (*xFunc)(pArg, pExpr);
808 if( rc==0 ){
809 if( walkExprTree(pExpr->pLeft, xFunc, pArg) ) return 1;
810 if( walkExprTree(pExpr->pRight, xFunc, pArg) ) return 1;
danielk1977a58fdfb2005-02-08 07:50:40 +0000811 if( walkExprList(pExpr->pList, xFunc, pArg) ) return 1;
drh626a8792005-01-17 22:08:19 +0000812 }
813 return rc>1;
814}
815
816/*
danielk1977a58fdfb2005-02-08 07:50:40 +0000817** Call walkExprTree() for every expression in list p.
818*/
819static int walkExprList(ExprList *p, int (*xFunc)(void *, Expr*), void *pArg){
820 int i;
821 struct ExprList_item *pItem;
822 if( !p ) return 0;
823 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
824 if( walkExprTree(pItem->pExpr, xFunc, pArg) ) return 1;
825 }
826 return 0;
827}
828
829/*
830** Call walkExprTree() for every expression in Select p, not including
831** expressions that are part of sub-selects in any FROM clause or the LIMIT
832** or OFFSET expressions..
833*/
834static int walkSelectExpr(Select *p, int (*xFunc)(void *, Expr*), void *pArg){
835 walkExprList(p->pEList, xFunc, pArg);
836 walkExprTree(p->pWhere, xFunc, pArg);
837 walkExprList(p->pGroupBy, xFunc, pArg);
838 walkExprTree(p->pHaving, xFunc, pArg);
839 walkExprList(p->pOrderBy, xFunc, pArg);
danielk197715d79822007-05-15 07:00:34 +0000840 if( p->pPrior ){
841 walkSelectExpr(p->pPrior, xFunc, pArg);
842 }
danielk1977a58fdfb2005-02-08 07:50:40 +0000843 return 0;
844}
845
846
847/*
drh626a8792005-01-17 22:08:19 +0000848** This routine is designed as an xFunc for walkExprTree().
849**
850** pArg is really a pointer to an integer. If we can tell by looking
drh73b211a2005-01-18 04:00:42 +0000851** at pExpr that the expression that contains pExpr is not a constant
852** expression, then set *pArg to 0 and return 2 to abandon the tree walk.
853** If pExpr does does not disqualify the expression from being a constant
854** then do nothing.
855**
856** After walking the whole tree, if no nodes are found that disqualify
857** the expression as constant, then we assume the whole expression
858** is constant. See sqlite3ExprIsConstant() for additional information.
drh626a8792005-01-17 22:08:19 +0000859*/
860static int exprNodeIsConstant(void *pArg, Expr *pExpr){
drh0a168372007-06-08 00:20:47 +0000861 int *pN = (int*)pArg;
862
863 /* If *pArg is 3 then any term of the expression that comes from
864 ** the ON or USING clauses of a join disqualifies the expression
865 ** from being considered constant. */
866 if( (*pN)==3 && ExprHasAnyProperty(pExpr, EP_FromJoin) ){
867 *pN = 0;
868 return 2;
869 }
870
drh626a8792005-01-17 22:08:19 +0000871 switch( pExpr->op ){
drheb55bd22005-06-30 17:04:21 +0000872 /* Consider functions to be constant if all their arguments are constant
873 ** and *pArg==2 */
874 case TK_FUNCTION:
drh0a168372007-06-08 00:20:47 +0000875 if( (*pN)==2 ) return 0;
drheb55bd22005-06-30 17:04:21 +0000876 /* Fall through */
drh626a8792005-01-17 22:08:19 +0000877 case TK_ID:
878 case TK_COLUMN:
879 case TK_DOT:
880 case TK_AGG_FUNCTION:
drh13449892005-09-07 21:22:45 +0000881 case TK_AGG_COLUMN:
drhfe2093d2005-01-20 22:48:47 +0000882#ifndef SQLITE_OMIT_SUBQUERY
883 case TK_SELECT:
884 case TK_EXISTS:
885#endif
drh0a168372007-06-08 00:20:47 +0000886 *pN = 0;
drh626a8792005-01-17 22:08:19 +0000887 return 2;
drh87abf5c2005-08-25 12:45:04 +0000888 case TK_IN:
889 if( pExpr->pSelect ){
drh0a168372007-06-08 00:20:47 +0000890 *pN = 0;
drh87abf5c2005-08-25 12:45:04 +0000891 return 2;
892 }
drh626a8792005-01-17 22:08:19 +0000893 default:
894 return 0;
895 }
896}
897
898/*
drhfef52082000-06-06 01:50:43 +0000899** Walk an expression tree. Return 1 if the expression is constant
drheb55bd22005-06-30 17:04:21 +0000900** and 0 if it involves variables or function calls.
drh23989372002-05-21 13:43:04 +0000901**
902** For the purposes of this function, a double-quoted string (ex: "abc")
903** is considered a variable but a single-quoted string (ex: 'abc') is
904** a constant.
drhfef52082000-06-06 01:50:43 +0000905*/
danielk19774adee202004-05-08 08:23:19 +0000906int sqlite3ExprIsConstant(Expr *p){
drh626a8792005-01-17 22:08:19 +0000907 int isConst = 1;
908 walkExprTree(p, exprNodeIsConstant, &isConst);
909 return isConst;
drhfef52082000-06-06 01:50:43 +0000910}
911
912/*
drheb55bd22005-06-30 17:04:21 +0000913** Walk an expression tree. Return 1 if the expression is constant
drh0a168372007-06-08 00:20:47 +0000914** that does no originate from the ON or USING clauses of a join.
915** Return 0 if it involves variables or function calls or terms from
916** an ON or USING clause.
917*/
918int sqlite3ExprIsConstantNotJoin(Expr *p){
919 int isConst = 3;
920 walkExprTree(p, exprNodeIsConstant, &isConst);
921 return isConst!=0;
922}
923
924/*
925** Walk an expression tree. Return 1 if the expression is constant
drheb55bd22005-06-30 17:04:21 +0000926** or a function call with constant arguments. Return and 0 if there
927** are any variables.
928**
929** For the purposes of this function, a double-quoted string (ex: "abc")
930** is considered a variable but a single-quoted string (ex: 'abc') is
931** a constant.
932*/
933int sqlite3ExprIsConstantOrFunction(Expr *p){
934 int isConst = 2;
935 walkExprTree(p, exprNodeIsConstant, &isConst);
936 return isConst!=0;
937}
938
939/*
drh73b211a2005-01-18 04:00:42 +0000940** If the expression p codes a constant integer that is small enough
drh202b2df2004-01-06 01:13:46 +0000941** to fit in a 32-bit integer, return 1 and put the value of the integer
942** in *pValue. If the expression is not an integer or if it is too big
943** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
drhe4de1fe2002-06-02 16:09:01 +0000944*/
danielk19774adee202004-05-08 08:23:19 +0000945int sqlite3ExprIsInteger(Expr *p, int *pValue){
drhe4de1fe2002-06-02 16:09:01 +0000946 switch( p->op ){
947 case TK_INTEGER: {
drh2646da72005-12-09 20:02:05 +0000948 if( sqlite3GetInt32((char*)p->token.z, pValue) ){
drh202b2df2004-01-06 01:13:46 +0000949 return 1;
950 }
951 break;
drhe4de1fe2002-06-02 16:09:01 +0000952 }
drh4b59ab52002-08-24 18:24:51 +0000953 case TK_UPLUS: {
danielk19774adee202004-05-08 08:23:19 +0000954 return sqlite3ExprIsInteger(p->pLeft, pValue);
drh4b59ab52002-08-24 18:24:51 +0000955 }
drhe4de1fe2002-06-02 16:09:01 +0000956 case TK_UMINUS: {
957 int v;
danielk19774adee202004-05-08 08:23:19 +0000958 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
drhe4de1fe2002-06-02 16:09:01 +0000959 *pValue = -v;
960 return 1;
961 }
962 break;
963 }
964 default: break;
965 }
966 return 0;
967}
968
969/*
drhc4a3c772001-04-04 11:48:57 +0000970** Return TRUE if the given string is a row-id column name.
971*/
danielk19774adee202004-05-08 08:23:19 +0000972int sqlite3IsRowid(const char *z){
973 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
974 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
975 if( sqlite3StrICmp(z, "OID")==0 ) return 1;
drhc4a3c772001-04-04 11:48:57 +0000976 return 0;
977}
978
979/*
drh8141f612004-01-25 22:44:58 +0000980** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
981** that name in the set of source tables in pSrcList and make the pExpr
982** expression node refer back to that source column. The following changes
983** are made to pExpr:
984**
985** pExpr->iDb Set the index in db->aDb[] of the database holding
986** the table.
987** pExpr->iTable Set to the cursor number for the table obtained
988** from pSrcList.
989** pExpr->iColumn Set to the column number within the table.
drh8141f612004-01-25 22:44:58 +0000990** pExpr->op Set to TK_COLUMN.
991** pExpr->pLeft Any expression this points to is deleted
992** pExpr->pRight Any expression this points to is deleted.
993**
994** The pDbToken is the name of the database (the "X"). This value may be
995** NULL meaning that name is of the form Y.Z or Z. Any available database
996** can be used. The pTableToken is the name of the table (the "Y"). This
997** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it
998** means that the form of the name is Z and that columns from any table
999** can be used.
1000**
1001** If the name cannot be resolved unambiguously, leave an error message
1002** in pParse and return non-zero. Return zero on success.
1003*/
1004static int lookupName(
drhffe07b22005-11-03 00:41:17 +00001005 Parse *pParse, /* The parsing context */
drh8141f612004-01-25 22:44:58 +00001006 Token *pDbToken, /* Name of the database containing table, or NULL */
1007 Token *pTableToken, /* Name of table containing column, or NULL */
1008 Token *pColumnToken, /* Name of the column. */
drh626a8792005-01-17 22:08:19 +00001009 NameContext *pNC, /* The name context used to resolve the name */
drh8141f612004-01-25 22:44:58 +00001010 Expr *pExpr /* Make this EXPR node point to the selected column */
1011){
1012 char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */
1013 char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */
1014 char *zCol = 0; /* Name of the column. The "Z" */
1015 int i, j; /* Loop counters */
1016 int cnt = 0; /* Number of matching column names */
1017 int cntTab = 0; /* Number of matching table names */
drh626a8792005-01-17 22:08:19 +00001018 sqlite3 *db = pParse->db; /* The database */
drh51669862004-12-18 18:40:26 +00001019 struct SrcList_item *pItem; /* Use for looping over pSrcList items */
1020 struct SrcList_item *pMatch = 0; /* The matching pSrcList item */
drh73b211a2005-01-18 04:00:42 +00001021 NameContext *pTopNC = pNC; /* First namecontext in the list */
drh728b5772007-09-18 15:55:07 +00001022 Schema *pSchema = 0; /* Schema of the expression */
drh8141f612004-01-25 22:44:58 +00001023
1024 assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
drh17435752007-08-16 04:30:38 +00001025 zDb = sqlite3NameFromToken(db, pDbToken);
1026 zTab = sqlite3NameFromToken(db, pTableToken);
1027 zCol = sqlite3NameFromToken(db, pColumnToken);
1028 if( db->mallocFailed ){
danielk1977d5d56522005-03-16 12:15:20 +00001029 goto lookupname_end;
drh8141f612004-01-25 22:44:58 +00001030 }
drh8141f612004-01-25 22:44:58 +00001031
1032 pExpr->iTable = -1;
drh626a8792005-01-17 22:08:19 +00001033 while( pNC && cnt==0 ){
drhffe07b22005-11-03 00:41:17 +00001034 ExprList *pEList;
drh626a8792005-01-17 22:08:19 +00001035 SrcList *pSrcList = pNC->pSrcList;
drh8141f612004-01-25 22:44:58 +00001036
danielk1977b3bce662005-01-29 08:32:43 +00001037 if( pSrcList ){
1038 for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
drh43617e92006-03-06 20:55:46 +00001039 Table *pTab;
1040 int iDb;
danielk1977b3bce662005-01-29 08:32:43 +00001041 Column *pCol;
1042
drh43617e92006-03-06 20:55:46 +00001043 pTab = pItem->pTab;
1044 assert( pTab!=0 );
1045 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
danielk1977b3bce662005-01-29 08:32:43 +00001046 assert( pTab->nCol>0 );
1047 if( zTab ){
1048 if( pItem->zAlias ){
1049 char *zTabName = pItem->zAlias;
1050 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
1051 }else{
1052 char *zTabName = pTab->zName;
1053 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
danielk1977da184232006-01-05 11:34:32 +00001054 if( zDb!=0 && sqlite3StrICmp(db->aDb[iDb].zName, zDb)!=0 ){
danielk1977b3bce662005-01-29 08:32:43 +00001055 continue;
1056 }
drh626a8792005-01-17 22:08:19 +00001057 }
drh8141f612004-01-25 22:44:58 +00001058 }
danielk1977b3bce662005-01-29 08:32:43 +00001059 if( 0==(cntTab++) ){
drh626a8792005-01-17 22:08:19 +00001060 pExpr->iTable = pItem->iCursor;
drh728b5772007-09-18 15:55:07 +00001061 pSchema = pTab->pSchema;
danielk1977b3bce662005-01-29 08:32:43 +00001062 pMatch = pItem;
1063 }
1064 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
1065 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
danielk1977b3bf5562006-01-10 17:58:23 +00001066 const char *zColl = pTab->aCol[j].zColl;
drh873fac02005-06-06 17:11:46 +00001067 IdList *pUsing;
danielk1977b3bce662005-01-29 08:32:43 +00001068 cnt++;
1069 pExpr->iTable = pItem->iCursor;
1070 pMatch = pItem;
drh728b5772007-09-18 15:55:07 +00001071 pSchema = pTab->pSchema;
danielk1977b3bce662005-01-29 08:32:43 +00001072 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
1073 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
1074 pExpr->affinity = pTab->aCol[j].affinity;
drh8b4c40d2007-02-01 23:02:45 +00001075 if( (pExpr->flags & EP_ExpCollate)==0 ){
1076 pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0);
1077 }
drh61dfc312006-12-16 16:25:15 +00001078 if( i<pSrcList->nSrc-1 ){
1079 if( pItem[1].jointype & JT_NATURAL ){
1080 /* If this match occurred in the left table of a natural join,
1081 ** then skip the right table to avoid a duplicate match */
1082 pItem++;
1083 i++;
1084 }else if( (pUsing = pItem[1].pUsing)!=0 ){
1085 /* If this match occurs on a column that is in the USING clause
1086 ** of a join, skip the search of the right table of the join
1087 ** to avoid a duplicate match there. */
1088 int k;
1089 for(k=0; k<pUsing->nId; k++){
1090 if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ){
1091 pItem++;
1092 i++;
1093 break;
1094 }
drh873fac02005-06-06 17:11:46 +00001095 }
1096 }
1097 }
danielk1977b3bce662005-01-29 08:32:43 +00001098 break;
1099 }
drh8141f612004-01-25 22:44:58 +00001100 }
1101 }
1102 }
drh626a8792005-01-17 22:08:19 +00001103
1104#ifndef SQLITE_OMIT_TRIGGER
1105 /* If we have not already resolved the name, then maybe
1106 ** it is a new.* or old.* trigger argument reference
1107 */
1108 if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
1109 TriggerStack *pTriggerStack = pParse->trigStack;
1110 Table *pTab = 0;
danielk19778f2c54e2008-01-01 19:02:09 +00001111 u32 *piColMask;
drh626a8792005-01-17 22:08:19 +00001112 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){
1113 pExpr->iTable = pTriggerStack->newIdx;
1114 assert( pTriggerStack->pTab );
1115 pTab = pTriggerStack->pTab;
danielk19778f2c54e2008-01-01 19:02:09 +00001116 piColMask = &(pTriggerStack->newColMask);
drh626a8792005-01-17 22:08:19 +00001117 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab)==0 ){
1118 pExpr->iTable = pTriggerStack->oldIdx;
1119 assert( pTriggerStack->pTab );
1120 pTab = pTriggerStack->pTab;
danielk19778f2c54e2008-01-01 19:02:09 +00001121 piColMask = &(pTriggerStack->oldColMask);
drh626a8792005-01-17 22:08:19 +00001122 }
1123
1124 if( pTab ){
danielk1977f0113002006-01-24 12:09:17 +00001125 int iCol;
drh626a8792005-01-17 22:08:19 +00001126 Column *pCol = pTab->aCol;
1127
drh728b5772007-09-18 15:55:07 +00001128 pSchema = pTab->pSchema;
drh626a8792005-01-17 22:08:19 +00001129 cntTab++;
danielk1977f0113002006-01-24 12:09:17 +00001130 for(iCol=0; iCol < pTab->nCol; iCol++, pCol++) {
drh626a8792005-01-17 22:08:19 +00001131 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
danielk1977f0113002006-01-24 12:09:17 +00001132 const char *zColl = pTab->aCol[iCol].zColl;
drh626a8792005-01-17 22:08:19 +00001133 cnt++;
danielk1977f0113002006-01-24 12:09:17 +00001134 pExpr->iColumn = iCol==pTab->iPKey ? -1 : iCol;
1135 pExpr->affinity = pTab->aCol[iCol].affinity;
drh8b4c40d2007-02-01 23:02:45 +00001136 if( (pExpr->flags & EP_ExpCollate)==0 ){
1137 pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0);
1138 }
danielk1977aee18ef2005-03-09 12:26:50 +00001139 pExpr->pTab = pTab;
danielk19778f2c54e2008-01-01 19:02:09 +00001140 if( iCol>=0 ){
1141 *piColMask |= ((u32)1<<iCol) | (iCol>=32?0xffffffff:0);
1142 }
drh626a8792005-01-17 22:08:19 +00001143 break;
1144 }
1145 }
1146 }
1147 }
drhb7f91642004-10-31 02:22:47 +00001148#endif /* !defined(SQLITE_OMIT_TRIGGER) */
drh8141f612004-01-25 22:44:58 +00001149
drh626a8792005-01-17 22:08:19 +00001150 /*
1151 ** Perhaps the name is a reference to the ROWID
1152 */
1153 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
1154 cnt = 1;
1155 pExpr->iColumn = -1;
drh8a512562005-11-14 22:29:05 +00001156 pExpr->affinity = SQLITE_AFF_INTEGER;
drh626a8792005-01-17 22:08:19 +00001157 }
drh8141f612004-01-25 22:44:58 +00001158
drh626a8792005-01-17 22:08:19 +00001159 /*
1160 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
1161 ** might refer to an result-set alias. This happens, for example, when
1162 ** we are resolving names in the WHERE clause of the following command:
1163 **
1164 ** SELECT a+b AS x FROM table WHERE x<10;
1165 **
1166 ** In cases like this, replace pExpr with a copy of the expression that
1167 ** forms the result set entry ("a+b" in the example) and return immediately.
1168 ** Note that the expression in the result set should have already been
1169 ** resolved by the time the WHERE clause is resolved.
1170 */
drhffe07b22005-11-03 00:41:17 +00001171 if( cnt==0 && (pEList = pNC->pEList)!=0 && zTab==0 ){
drh626a8792005-01-17 22:08:19 +00001172 for(j=0; j<pEList->nExpr; j++){
1173 char *zAs = pEList->a[j].zName;
1174 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
drh36379e92007-07-23 22:51:15 +00001175 Expr *pDup, *pOrig;
drh626a8792005-01-17 22:08:19 +00001176 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
drh4f07e5f2007-05-14 11:34:46 +00001177 assert( pExpr->pList==0 );
1178 assert( pExpr->pSelect==0 );
drh36379e92007-07-23 22:51:15 +00001179 pOrig = pEList->a[j].pExpr;
1180 if( !pNC->allowAgg && ExprHasProperty(pOrig, EP_Agg) ){
1181 sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs);
drh17435752007-08-16 04:30:38 +00001182 sqlite3_free(zCol);
drh36379e92007-07-23 22:51:15 +00001183 return 2;
1184 }
danielk19771e536952007-08-16 10:09:01 +00001185 pDup = sqlite3ExprDup(db, pOrig);
drh4f07e5f2007-05-14 11:34:46 +00001186 if( pExpr->flags & EP_ExpCollate ){
1187 pDup->pColl = pExpr->pColl;
1188 pDup->flags |= EP_ExpCollate;
1189 }
drh17435752007-08-16 04:30:38 +00001190 if( pExpr->span.dyn ) sqlite3_free((char*)pExpr->span.z);
1191 if( pExpr->token.dyn ) sqlite3_free((char*)pExpr->token.z);
drh4f07e5f2007-05-14 11:34:46 +00001192 memcpy(pExpr, pDup, sizeof(*pExpr));
drh17435752007-08-16 04:30:38 +00001193 sqlite3_free(pDup);
drh15ccce12005-05-23 15:06:39 +00001194 cnt = 1;
danielk1977c9cf6e32007-06-25 16:29:33 +00001195 pMatch = 0;
drh626a8792005-01-17 22:08:19 +00001196 assert( zTab==0 && zDb==0 );
drh15ccce12005-05-23 15:06:39 +00001197 goto lookupname_end_2;
drh626a8792005-01-17 22:08:19 +00001198 }
1199 }
1200 }
1201
1202 /* Advance to the next name context. The loop will exit when either
1203 ** we have a match (cnt>0) or when we run out of name contexts.
1204 */
1205 if( cnt==0 ){
1206 pNC = pNC->pNext;
1207 }
drh8141f612004-01-25 22:44:58 +00001208 }
1209
1210 /*
1211 ** If X and Y are NULL (in other words if only the column name Z is
1212 ** supplied) and the value of Z is enclosed in double-quotes, then
1213 ** Z is a string literal if it doesn't match any column names. In that
1214 ** case, we need to return right away and not make any changes to
1215 ** pExpr.
drh15ccce12005-05-23 15:06:39 +00001216 **
1217 ** Because no reference was made to outer contexts, the pNC->nRef
1218 ** fields are not changed in any context.
drh8141f612004-01-25 22:44:58 +00001219 */
1220 if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
drh17435752007-08-16 04:30:38 +00001221 sqlite3_free(zCol);
drh8141f612004-01-25 22:44:58 +00001222 return 0;
1223 }
1224
1225 /*
1226 ** cnt==0 means there was not match. cnt>1 means there were two or
1227 ** more matches. Either way, we have an error.
1228 */
1229 if( cnt!=1 ){
1230 char *z = 0;
1231 char *zErr;
1232 zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s";
1233 if( zDb ){
drhf93339d2006-01-03 15:16:26 +00001234 sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, (char*)0);
drh8141f612004-01-25 22:44:58 +00001235 }else if( zTab ){
drhf93339d2006-01-03 15:16:26 +00001236 sqlite3SetString(&z, zTab, ".", zCol, (char*)0);
drh8141f612004-01-25 22:44:58 +00001237 }else{
drh17435752007-08-16 04:30:38 +00001238 z = sqlite3StrDup(zCol);
drh8141f612004-01-25 22:44:58 +00001239 }
danielk1977a1644fd2007-08-29 12:31:25 +00001240 if( z ){
1241 sqlite3ErrorMsg(pParse, zErr, z);
1242 sqlite3_free(z);
1243 pTopNC->nErr++;
1244 }else{
1245 db->mallocFailed = 1;
1246 }
drh8141f612004-01-25 22:44:58 +00001247 }
1248
drh51669862004-12-18 18:40:26 +00001249 /* If a column from a table in pSrcList is referenced, then record
1250 ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes
1251 ** bit 0 to be set. Column 1 sets bit 1. And so forth. If the
1252 ** column number is greater than the number of bits in the bitmask
1253 ** then set the high-order bit of the bitmask.
1254 */
1255 if( pExpr->iColumn>=0 && pMatch!=0 ){
1256 int n = pExpr->iColumn;
1257 if( n>=sizeof(Bitmask)*8 ){
1258 n = sizeof(Bitmask)*8-1;
1259 }
1260 assert( pMatch->iCursor==pExpr->iTable );
drhca83ac52007-02-01 01:40:44 +00001261 pMatch->colUsed |= ((Bitmask)1)<<n;
drh51669862004-12-18 18:40:26 +00001262 }
1263
danielk1977d5d56522005-03-16 12:15:20 +00001264lookupname_end:
drh8141f612004-01-25 22:44:58 +00001265 /* Clean up and return
1266 */
drh17435752007-08-16 04:30:38 +00001267 sqlite3_free(zDb);
1268 sqlite3_free(zTab);
danielk19774adee202004-05-08 08:23:19 +00001269 sqlite3ExprDelete(pExpr->pLeft);
drh8141f612004-01-25 22:44:58 +00001270 pExpr->pLeft = 0;
danielk19774adee202004-05-08 08:23:19 +00001271 sqlite3ExprDelete(pExpr->pRight);
drh8141f612004-01-25 22:44:58 +00001272 pExpr->pRight = 0;
1273 pExpr->op = TK_COLUMN;
drh15ccce12005-05-23 15:06:39 +00001274lookupname_end_2:
drh17435752007-08-16 04:30:38 +00001275 sqlite3_free(zCol);
drh626a8792005-01-17 22:08:19 +00001276 if( cnt==1 ){
danielk1977b3bce662005-01-29 08:32:43 +00001277 assert( pNC!=0 );
drh728b5772007-09-18 15:55:07 +00001278 sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList);
danielk1977aee18ef2005-03-09 12:26:50 +00001279 if( pMatch && !pMatch->pSelect ){
1280 pExpr->pTab = pMatch->pTab;
1281 }
drh15ccce12005-05-23 15:06:39 +00001282 /* Increment the nRef value on all name contexts from TopNC up to
1283 ** the point where the name matched. */
1284 for(;;){
1285 assert( pTopNC!=0 );
1286 pTopNC->nRef++;
1287 if( pTopNC==pNC ) break;
1288 pTopNC = pTopNC->pNext;
1289 }
1290 return 0;
1291 } else {
1292 return 1;
drh626a8792005-01-17 22:08:19 +00001293 }
drh8141f612004-01-25 22:44:58 +00001294}
1295
1296/*
drh626a8792005-01-17 22:08:19 +00001297** This routine is designed as an xFunc for walkExprTree().
1298**
drh73b211a2005-01-18 04:00:42 +00001299** Resolve symbolic names into TK_COLUMN operators for the current
drh626a8792005-01-17 22:08:19 +00001300** node in the expression tree. Return 0 to continue the search down
drh73b211a2005-01-18 04:00:42 +00001301** the tree or 2 to abort the tree walk.
1302**
1303** This routine also does error checking and name resolution for
1304** function names. The operator for aggregate functions is changed
1305** to TK_AGG_FUNCTION.
drh626a8792005-01-17 22:08:19 +00001306*/
1307static int nameResolverStep(void *pArg, Expr *pExpr){
1308 NameContext *pNC = (NameContext*)pArg;
drh626a8792005-01-17 22:08:19 +00001309 Parse *pParse;
drh626a8792005-01-17 22:08:19 +00001310
danielk1977b3bce662005-01-29 08:32:43 +00001311 if( pExpr==0 ) return 1;
drh626a8792005-01-17 22:08:19 +00001312 assert( pNC!=0 );
drh626a8792005-01-17 22:08:19 +00001313 pParse = pNC->pParse;
danielk1977b3bce662005-01-29 08:32:43 +00001314
drh626a8792005-01-17 22:08:19 +00001315 if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return 1;
1316 ExprSetProperty(pExpr, EP_Resolved);
1317#ifndef NDEBUG
danielk1977f0113002006-01-24 12:09:17 +00001318 if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
1319 SrcList *pSrcList = pNC->pSrcList;
danielk1977940fac92005-01-23 22:41:37 +00001320 int i;
danielk1977f0113002006-01-24 12:09:17 +00001321 for(i=0; i<pNC->pSrcList->nSrc; i++){
drh626a8792005-01-17 22:08:19 +00001322 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
1323 }
1324 }
1325#endif
1326 switch( pExpr->op ){
1327 /* Double-quoted strings (ex: "abc") are used as identifiers if
1328 ** possible. Otherwise they remain as strings. Single-quoted
1329 ** strings (ex: 'abc') are always string literals.
1330 */
1331 case TK_STRING: {
1332 if( pExpr->token.z[0]=='\'' ) break;
1333 /* Fall thru into the TK_ID case if this is a double-quoted string */
1334 }
1335 /* A lone identifier is the name of a column.
1336 */
1337 case TK_ID: {
drh626a8792005-01-17 22:08:19 +00001338 lookupName(pParse, 0, 0, &pExpr->token, pNC, pExpr);
1339 return 1;
1340 }
1341
1342 /* A table name and column name: ID.ID
1343 ** Or a database, table and column: ID.ID.ID
1344 */
1345 case TK_DOT: {
1346 Token *pColumn;
1347 Token *pTable;
1348 Token *pDb;
1349 Expr *pRight;
1350
danielk1977b3bce662005-01-29 08:32:43 +00001351 /* if( pSrcList==0 ) break; */
drh626a8792005-01-17 22:08:19 +00001352 pRight = pExpr->pRight;
1353 if( pRight->op==TK_ID ){
1354 pDb = 0;
1355 pTable = &pExpr->pLeft->token;
1356 pColumn = &pRight->token;
1357 }else{
1358 assert( pRight->op==TK_DOT );
1359 pDb = &pExpr->pLeft->token;
1360 pTable = &pRight->pLeft->token;
1361 pColumn = &pRight->pRight->token;
1362 }
1363 lookupName(pParse, pDb, pTable, pColumn, pNC, pExpr);
1364 return 1;
1365 }
1366
1367 /* Resolve function names
1368 */
drhb71090f2005-05-23 17:26:51 +00001369 case TK_CONST_FUNC:
drh626a8792005-01-17 22:08:19 +00001370 case TK_FUNCTION: {
1371 ExprList *pList = pExpr->pList; /* The argument list */
1372 int n = pList ? pList->nExpr : 0; /* Number of arguments */
1373 int no_such_func = 0; /* True if no such function exists */
1374 int wrong_num_args = 0; /* True if wrong number of arguments */
1375 int is_agg = 0; /* True if is an aggregate function */
1376 int i;
drh5169bbc2006-08-24 14:59:45 +00001377 int auth; /* Authorization to use the function */
drh626a8792005-01-17 22:08:19 +00001378 int nId; /* Number of characters in function name */
1379 const char *zId; /* The function name. */
drh73b211a2005-01-18 04:00:42 +00001380 FuncDef *pDef; /* Information about the function */
danielk197714db2662006-01-09 16:12:04 +00001381 int enc = ENC(pParse->db); /* The database encoding */
drh626a8792005-01-17 22:08:19 +00001382
drh2646da72005-12-09 20:02:05 +00001383 zId = (char*)pExpr->token.z;
drhb71090f2005-05-23 17:26:51 +00001384 nId = pExpr->token.n;
drh626a8792005-01-17 22:08:19 +00001385 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
1386 if( pDef==0 ){
1387 pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
1388 if( pDef==0 ){
1389 no_such_func = 1;
1390 }else{
1391 wrong_num_args = 1;
1392 }
1393 }else{
1394 is_agg = pDef->xFunc==0;
1395 }
drh2fca7fe2006-11-23 11:59:13 +00001396#ifndef SQLITE_OMIT_AUTHORIZATION
drh5169bbc2006-08-24 14:59:45 +00001397 if( pDef ){
1398 auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0);
1399 if( auth!=SQLITE_OK ){
1400 if( auth==SQLITE_DENY ){
1401 sqlite3ErrorMsg(pParse, "not authorized to use function: %s",
1402 pDef->zName);
1403 pNC->nErr++;
1404 }
1405 pExpr->op = TK_NULL;
1406 return 1;
1407 }
1408 }
drhb8b14212006-08-24 15:18:25 +00001409#endif
drh626a8792005-01-17 22:08:19 +00001410 if( is_agg && !pNC->allowAgg ){
1411 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
1412 pNC->nErr++;
1413 is_agg = 0;
1414 }else if( no_such_func ){
1415 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
1416 pNC->nErr++;
1417 }else if( wrong_num_args ){
1418 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
1419 nId, zId);
1420 pNC->nErr++;
1421 }
1422 if( is_agg ){
1423 pExpr->op = TK_AGG_FUNCTION;
1424 pNC->hasAgg = 1;
1425 }
drh73b211a2005-01-18 04:00:42 +00001426 if( is_agg ) pNC->allowAgg = 0;
drh626a8792005-01-17 22:08:19 +00001427 for(i=0; pNC->nErr==0 && i<n; i++){
drh73b211a2005-01-18 04:00:42 +00001428 walkExprTree(pList->a[i].pExpr, nameResolverStep, pNC);
drh626a8792005-01-17 22:08:19 +00001429 }
drh73b211a2005-01-18 04:00:42 +00001430 if( is_agg ) pNC->allowAgg = 1;
drh626a8792005-01-17 22:08:19 +00001431 /* FIX ME: Compute pExpr->affinity based on the expected return
1432 ** type of the function
1433 */
1434 return is_agg;
1435 }
danielk1977b3bce662005-01-29 08:32:43 +00001436#ifndef SQLITE_OMIT_SUBQUERY
1437 case TK_SELECT:
1438 case TK_EXISTS:
1439#endif
1440 case TK_IN: {
1441 if( pExpr->pSelect ){
drh8a9f38f2005-11-05 15:07:55 +00001442 int nRef = pNC->nRef;
drh06f65412005-11-03 02:03:13 +00001443#ifndef SQLITE_OMIT_CHECK
1444 if( pNC->isCheck ){
1445 sqlite3ErrorMsg(pParse,"subqueries prohibited in CHECK constraints");
1446 }
1447#endif
danielk1977b3bce662005-01-29 08:32:43 +00001448 sqlite3SelectResolve(pParse, pExpr->pSelect, pNC);
1449 assert( pNC->nRef>=nRef );
1450 if( nRef!=pNC->nRef ){
1451 ExprSetProperty(pExpr, EP_VarSelect);
1452 }
1453 }
drh4284fb02005-11-03 12:33:28 +00001454 break;
danielk1977b3bce662005-01-29 08:32:43 +00001455 }
drh4284fb02005-11-03 12:33:28 +00001456#ifndef SQLITE_OMIT_CHECK
1457 case TK_VARIABLE: {
1458 if( pNC->isCheck ){
1459 sqlite3ErrorMsg(pParse,"parameters prohibited in CHECK constraints");
1460 }
1461 break;
1462 }
1463#endif
drh626a8792005-01-17 22:08:19 +00001464 }
1465 return 0;
1466}
1467
1468/*
drhcce7d172000-05-31 15:34:51 +00001469** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +00001470** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +00001471** index to the table in the table list and a column offset. The
1472** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
1473** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +00001474** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +00001475** VDBE cursor number for a cursor that is pointing into the referenced
1476** table. The Expr.iColumn value is changed to the index of the column
1477** of the referenced table. The Expr.iColumn value for the special
1478** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
1479** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +00001480**
drh626a8792005-01-17 22:08:19 +00001481** Also resolve function names and check the functions for proper
1482** usage. Make sure all function names are recognized and all functions
1483** have the correct number of arguments. Leave an error message
1484** in pParse->zErrMsg if anything is amiss. Return the number of errors.
1485**
drh73b211a2005-01-18 04:00:42 +00001486** If the expression contains aggregate functions then set the EP_Agg
1487** property on the expression.
drh626a8792005-01-17 22:08:19 +00001488*/
danielk1977fc976062007-05-10 10:46:56 +00001489int sqlite3ExprResolveNames(
danielk1977b3bce662005-01-29 08:32:43 +00001490 NameContext *pNC, /* Namespace to resolve expressions in. */
1491 Expr *pExpr /* The expression to be analyzed. */
drh626a8792005-01-17 22:08:19 +00001492){
drh13449892005-09-07 21:22:45 +00001493 int savedHasAgg;
drh73b211a2005-01-18 04:00:42 +00001494 if( pExpr==0 ) return 0;
danielk1977e6a58a42007-08-31 17:42:48 +00001495#if defined(SQLITE_TEST) || SQLITE_MAX_EXPR_DEPTH>0
danielk1977fc976062007-05-10 10:46:56 +00001496 if( (pExpr->nHeight+pNC->pParse->nHeight)>SQLITE_MAX_EXPR_DEPTH ){
1497 sqlite3ErrorMsg(pNC->pParse,
1498 "Expression tree is too large (maximum depth %d)",
1499 SQLITE_MAX_EXPR_DEPTH
1500 );
1501 return 1;
1502 }
1503 pNC->pParse->nHeight += pExpr->nHeight;
1504#endif
drh13449892005-09-07 21:22:45 +00001505 savedHasAgg = pNC->hasAgg;
1506 pNC->hasAgg = 0;
danielk1977b3bce662005-01-29 08:32:43 +00001507 walkExprTree(pExpr, nameResolverStep, pNC);
danielk1977e6a58a42007-08-31 17:42:48 +00001508#if defined(SQLITE_TEST) || SQLITE_MAX_EXPR_DEPTH>0
danielk1977fc976062007-05-10 10:46:56 +00001509 pNC->pParse->nHeight -= pExpr->nHeight;
1510#endif
danielk1977b3bce662005-01-29 08:32:43 +00001511 if( pNC->nErr>0 ){
drh73b211a2005-01-18 04:00:42 +00001512 ExprSetProperty(pExpr, EP_Error);
drh73b211a2005-01-18 04:00:42 +00001513 }
drh13449892005-09-07 21:22:45 +00001514 if( pNC->hasAgg ){
1515 ExprSetProperty(pExpr, EP_Agg);
1516 }else if( savedHasAgg ){
1517 pNC->hasAgg = 1;
1518 }
drh73b211a2005-01-18 04:00:42 +00001519 return ExprHasProperty(pExpr, EP_Error);
drh626a8792005-01-17 22:08:19 +00001520}
1521
drh1398ad32005-01-19 23:24:50 +00001522/*
1523** A pointer instance of this structure is used to pass information
1524** through walkExprTree into codeSubqueryStep().
1525*/
1526typedef struct QueryCoder QueryCoder;
1527struct QueryCoder {
1528 Parse *pParse; /* The parsing context */
1529 NameContext *pNC; /* Namespace of first enclosing query */
1530};
1531
danielk19779a96b662007-11-29 17:05:18 +00001532#ifdef SQLITE_TEST
1533 int sqlite3_enable_in_opt = 1;
1534#else
1535 #define sqlite3_enable_in_opt 1
1536#endif
1537
1538/*
1539** This function is used by the implementation of the IN (...) operator.
1540** It's job is to find or create a b-tree structure that may be used
1541** either to test for membership of the (...) set or to iterate through
drh85b623f2007-12-13 21:54:09 +00001542** its members, skipping duplicates.
danielk19779a96b662007-11-29 17:05:18 +00001543**
1544** The cursor opened on the structure (database table, database index
1545** or ephermal table) is stored in pX->iTable before this function returns.
1546** The returned value indicates the structure type, as follows:
1547**
1548** IN_INDEX_ROWID - The cursor was opened on a database table.
drh2d401ab2008-01-10 23:50:11 +00001549** IN_INDEX_INDEX - The cursor was opened on a database index.
danielk19779a96b662007-11-29 17:05:18 +00001550** IN_INDEX_EPH - The cursor was opened on a specially created and
1551** populated epheremal table.
1552**
1553** An existing structure may only be used if the SELECT is of the simple
1554** form:
1555**
1556** SELECT <column> FROM <table>
1557**
1558** If the mustBeUnique parameter is false, the structure will be used
1559** for fast set membership tests. In this case an epheremal table must
1560** be used unless <column> is an INTEGER PRIMARY KEY or an index can
drh85b623f2007-12-13 21:54:09 +00001561** be found with <column> as its left-most column.
danielk19779a96b662007-11-29 17:05:18 +00001562**
1563** If mustBeUnique is true, then the structure will be used to iterate
1564** through the set members, skipping any duplicates. In this case an
1565** epheremal table must be used unless the selected <column> is guaranteed
1566** to be unique - either because it is an INTEGER PRIMARY KEY or it
1567** is unique by virtue of a constraint or implicit index.
1568*/
danielk1977284f4ac2007-12-10 05:03:46 +00001569#ifndef SQLITE_OMIT_SUBQUERY
danielk19779a96b662007-11-29 17:05:18 +00001570int sqlite3FindInIndex(Parse *pParse, Expr *pX, int mustBeUnique){
1571 Select *p;
1572 int eType = 0;
1573 int iTab = pParse->nTab++;
1574
1575 /* The follwing if(...) expression is true if the SELECT is of the
1576 ** simple form:
1577 **
1578 ** SELECT <column> FROM <table>
1579 **
1580 ** If this is the case, it may be possible to use an existing table
1581 ** or index instead of generating an epheremal table.
1582 */
1583 if( sqlite3_enable_in_opt
1584 && (p=pX->pSelect) && !p->pPrior
1585 && !p->isDistinct && !p->isAgg && !p->pGroupBy
1586 && p->pSrc && p->pSrc->nSrc==1 && !p->pSrc->a[0].pSelect
1587 && !p->pSrc->a[0].pTab->pSelect
1588 && p->pEList->nExpr==1 && p->pEList->a[0].pExpr->op==TK_COLUMN
1589 && !p->pLimit && !p->pOffset && !p->pWhere
1590 ){
1591 sqlite3 *db = pParse->db;
1592 Index *pIdx;
1593 Expr *pExpr = p->pEList->a[0].pExpr;
1594 int iCol = pExpr->iColumn;
1595 Vdbe *v = sqlite3GetVdbe(pParse);
1596
1597 /* This function is only called from two places. In both cases the vdbe
1598 ** has already been allocated. So assume sqlite3GetVdbe() is always
1599 ** successful here.
1600 */
1601 assert(v);
1602 if( iCol<0 ){
drh0a07c102008-01-03 18:03:08 +00001603 int iMem = ++pParse->nMem;
danielk19779a96b662007-11-29 17:05:18 +00001604 int iAddr;
1605 Table *pTab = p->pSrc->a[0].pTab;
1606 int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
1607 sqlite3VdbeUsesBtree(v, iDb);
1608
drh892d3172008-01-10 03:46:36 +00001609 iAddr = sqlite3VdbeAddOp1(v, OP_If, iMem);
drh4c583122008-01-04 22:01:03 +00001610 sqlite3VdbeAddOp2(v, OP_Integer, 1, iMem);
danielk19779a96b662007-11-29 17:05:18 +00001611
1612 sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
1613 eType = IN_INDEX_ROWID;
1614
1615 sqlite3VdbeJumpHere(v, iAddr);
1616 }else{
1617 /* The collation sequence used by the comparison. If an index is to
1618 ** be used in place of a temp-table, it must be ordered according
1619 ** to this collation sequence.
1620 */
1621 CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pExpr);
1622
1623 /* Check that the affinity that will be used to perform the
1624 ** comparison is the same as the affinity of the column. If
1625 ** it is not, it is not possible to use any index.
1626 */
1627 Table *pTab = p->pSrc->a[0].pTab;
1628 char aff = comparisonAffinity(pX);
1629 int affinity_ok = (pTab->aCol[iCol].affinity==aff||aff==SQLITE_AFF_NONE);
1630
1631 for(pIdx=pTab->pIndex; pIdx && eType==0 && affinity_ok; pIdx=pIdx->pNext){
1632 if( (pIdx->aiColumn[0]==iCol)
1633 && (pReq==sqlite3FindCollSeq(db, ENC(db), pIdx->azColl[0], -1, 0))
1634 && (!mustBeUnique || (pIdx->nColumn==1 && pIdx->onError!=OE_None))
1635 ){
1636 int iDb;
drh0a07c102008-01-03 18:03:08 +00001637 int iMem = ++pParse->nMem;
danielk19779a96b662007-11-29 17:05:18 +00001638 int iAddr;
1639 char *pKey;
1640
1641 pKey = (char *)sqlite3IndexKeyinfo(pParse, pIdx);
1642 iDb = sqlite3SchemaToIndex(db, pIdx->pSchema);
1643 sqlite3VdbeUsesBtree(v, iDb);
1644
drh892d3172008-01-10 03:46:36 +00001645 iAddr = sqlite3VdbeAddOp1(v, OP_If, iMem);
drh4c583122008-01-04 22:01:03 +00001646 sqlite3VdbeAddOp2(v, OP_Integer, 1, iMem);
danielk19779a96b662007-11-29 17:05:18 +00001647
danielk1977207872a2008-01-03 07:54:23 +00001648 sqlite3VdbeAddOp4(v, OP_OpenRead, iTab, pIdx->tnum, iDb,
drh66a51672008-01-03 00:01:23 +00001649 pKey,P4_KEYINFO_HANDOFF);
danielk1977207872a2008-01-03 07:54:23 +00001650 VdbeComment((v, "%s", pIdx->zName));
danielk19779a96b662007-11-29 17:05:18 +00001651 eType = IN_INDEX_INDEX;
drh66a51672008-01-03 00:01:23 +00001652 sqlite3VdbeAddOp2(v, OP_SetNumColumns, iTab, pIdx->nColumn);
danielk19779a96b662007-11-29 17:05:18 +00001653
1654 sqlite3VdbeJumpHere(v, iAddr);
1655 }
1656 }
1657 }
1658 }
1659
1660 if( eType==0 ){
1661 sqlite3CodeSubselect(pParse, pX);
1662 eType = IN_INDEX_EPH;
1663 }else{
1664 pX->iTable = iTab;
1665 }
1666 return eType;
1667}
danielk1977284f4ac2007-12-10 05:03:46 +00001668#endif
drh626a8792005-01-17 22:08:19 +00001669
1670/*
drh9cbe6352005-11-29 03:13:21 +00001671** Generate code for scalar subqueries used as an expression
1672** and IN operators. Examples:
drh626a8792005-01-17 22:08:19 +00001673**
drh9cbe6352005-11-29 03:13:21 +00001674** (SELECT a FROM b) -- subquery
1675** EXISTS (SELECT a FROM b) -- EXISTS subquery
1676** x IN (4,5,11) -- IN operator with list on right-hand side
1677** x IN (SELECT a FROM b) -- IN operator with subquery on the right
drhfef52082000-06-06 01:50:43 +00001678**
drh9cbe6352005-11-29 03:13:21 +00001679** The pExpr parameter describes the expression that contains the IN
1680** operator or subquery.
drhcce7d172000-05-31 15:34:51 +00001681*/
drh51522cd2005-01-20 13:36:19 +00001682#ifndef SQLITE_OMIT_SUBQUERY
danielk1977b3bce662005-01-29 08:32:43 +00001683void sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){
drh57dbd7b2005-07-08 18:25:26 +00001684 int testAddr = 0; /* One-time test address */
danielk1977b3bce662005-01-29 08:32:43 +00001685 Vdbe *v = sqlite3GetVdbe(pParse);
1686 if( v==0 ) return;
1687
danielk1977fc976062007-05-10 10:46:56 +00001688
drh57dbd7b2005-07-08 18:25:26 +00001689 /* This code must be run in its entirety every time it is encountered
1690 ** if any of the following is true:
1691 **
1692 ** * The right-hand side is a correlated subquery
1693 ** * The right-hand side is an expression list containing variables
1694 ** * We are inside a trigger
1695 **
1696 ** If all of the above are false, then we can run this code just once
1697 ** save the results, and reuse the same result on subsequent invocations.
danielk1977b3bce662005-01-29 08:32:43 +00001698 */
1699 if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->trigStack ){
drh0a07c102008-01-03 18:03:08 +00001700 int mem = ++pParse->nMem;
drh892d3172008-01-10 03:46:36 +00001701 sqlite3VdbeAddOp1(v, OP_If, mem);
1702 testAddr = sqlite3VdbeAddOp2(v, OP_Integer, 1, mem);
drh17435752007-08-16 04:30:38 +00001703 assert( testAddr>0 || pParse->db->mallocFailed );
danielk1977b3bce662005-01-29 08:32:43 +00001704 }
1705
drhcce7d172000-05-31 15:34:51 +00001706 switch( pExpr->op ){
drhfef52082000-06-06 01:50:43 +00001707 case TK_IN: {
danielk1977e014a832004-05-17 10:48:57 +00001708 char affinity;
drhd3d39e92004-05-20 22:16:29 +00001709 KeyInfo keyInfo;
drhb9bb7c12006-06-11 23:41:55 +00001710 int addr; /* Address of OP_OpenEphemeral instruction */
drhd3d39e92004-05-20 22:16:29 +00001711
danielk1977bf3b7212004-05-18 10:06:24 +00001712 affinity = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +00001713
1714 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
drh57dbd7b2005-07-08 18:25:26 +00001715 ** expression it is handled the same way. A virtual table is
danielk1977e014a832004-05-17 10:48:57 +00001716 ** filled with single-field index keys representing the results
1717 ** from the SELECT or the <exprlist>.
1718 **
1719 ** If the 'x' expression is a column value, or the SELECT...
1720 ** statement returns a column value, then the affinity of that
1721 ** column is used to build the index keys. If both 'x' and the
1722 ** SELECT... statement are columns, then numeric affinity is used
1723 ** if either column has NUMERIC or INTEGER affinity. If neither
1724 ** 'x' nor the SELECT... statement are columns, then numeric affinity
1725 ** is used.
1726 */
1727 pExpr->iTable = pParse->nTab++;
drh66a51672008-01-03 00:01:23 +00001728 addr = sqlite3VdbeAddOp1(v, OP_OpenEphemeral, pExpr->iTable);
drhd3d39e92004-05-20 22:16:29 +00001729 memset(&keyInfo, 0, sizeof(keyInfo));
1730 keyInfo.nField = 1;
drh66a51672008-01-03 00:01:23 +00001731 sqlite3VdbeAddOp2(v, OP_SetNumColumns, pExpr->iTable, 1);
danielk1977e014a832004-05-17 10:48:57 +00001732
drhfef52082000-06-06 01:50:43 +00001733 if( pExpr->pSelect ){
1734 /* Case 1: expr IN (SELECT ...)
1735 **
danielk1977e014a832004-05-17 10:48:57 +00001736 ** Generate code to write the results of the select into the temporary
1737 ** table allocated and opened above.
drhfef52082000-06-06 01:50:43 +00001738 */
drh1013c932008-01-06 00:25:21 +00001739 SelectDest dest;
drhbe5c89a2004-07-26 00:31:09 +00001740 ExprList *pEList;
drh1013c932008-01-06 00:25:21 +00001741
1742 sqlite3SelectDestInit(&dest, SRT_Set, pExpr->iTable);
1743 dest.affinity = (int)affinity;
danielk1977e014a832004-05-17 10:48:57 +00001744 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
danielk19776c8c8ce2008-01-02 16:27:09 +00001745 if( sqlite3Select(pParse, pExpr->pSelect, &dest, 0, 0, 0, 0) ){
drh94ccde52007-04-13 16:06:32 +00001746 return;
1747 }
drhbe5c89a2004-07-26 00:31:09 +00001748 pEList = pExpr->pSelect->pEList;
1749 if( pEList && pEList->nExpr>0 ){
danielk1977bcbb04e2007-05-29 12:11:29 +00001750 keyInfo.aColl[0] = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft,
drhbe5c89a2004-07-26 00:31:09 +00001751 pEList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001752 }
drhfef52082000-06-06 01:50:43 +00001753 }else if( pExpr->pList ){
1754 /* Case 2: expr IN (exprlist)
1755 **
drhfd131da2007-08-07 17:13:03 +00001756 ** For each expression, build an index key from the evaluation and
danielk1977e014a832004-05-17 10:48:57 +00001757 ** store it in the temporary table. If <expr> is a column, then use
1758 ** that columns affinity when building index keys. If <expr> is not
1759 ** a column, use numeric affinity.
drhfef52082000-06-06 01:50:43 +00001760 */
danielk1977e014a832004-05-17 10:48:57 +00001761 int i;
drh57dbd7b2005-07-08 18:25:26 +00001762 ExprList *pList = pExpr->pList;
1763 struct ExprList_item *pItem;
drh2d401ab2008-01-10 23:50:11 +00001764 int r1, r2;
drh57dbd7b2005-07-08 18:25:26 +00001765
danielk1977e014a832004-05-17 10:48:57 +00001766 if( !affinity ){
drh8159a352006-05-23 23:22:29 +00001767 affinity = SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +00001768 }
danielk19770202b292004-06-09 09:55:16 +00001769 keyInfo.aColl[0] = pExpr->pLeft->pColl;
danielk1977e014a832004-05-17 10:48:57 +00001770
1771 /* Loop through each expression in <exprlist>. */
drh2d401ab2008-01-10 23:50:11 +00001772 r1 = sqlite3GetTempReg(pParse);
1773 r2 = sqlite3GetTempReg(pParse);
drh57dbd7b2005-07-08 18:25:26 +00001774 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
1775 Expr *pE2 = pItem->pExpr;
danielk1977e014a832004-05-17 10:48:57 +00001776
drh57dbd7b2005-07-08 18:25:26 +00001777 /* If the expression is not constant then we will need to
1778 ** disable the test that was generated above that makes sure
1779 ** this code only executes once. Because for a non-constant
1780 ** expression we need to rerun this code each time.
1781 */
drh892d3172008-01-10 03:46:36 +00001782 if( testAddr && !sqlite3ExprIsConstant(pE2) ){
1783 sqlite3VdbeChangeToNoop(v, testAddr-1, 2);
drh57dbd7b2005-07-08 18:25:26 +00001784 testAddr = 0;
drh4794b982000-06-06 13:54:14 +00001785 }
danielk1977e014a832004-05-17 10:48:57 +00001786
1787 /* Evaluate the expression and insert it into the temp table */
drh2d401ab2008-01-10 23:50:11 +00001788 sqlite3ExprCode(pParse, pE2, r1);
1789 sqlite3VdbeAddOp4(v, OP_RegMakeRec, r1, 1, r2, &affinity, 1);
1790 sqlite3VdbeAddOp2(v, OP_IdxInsert, pExpr->iTable, r2);
drhfef52082000-06-06 01:50:43 +00001791 }
drh2d401ab2008-01-10 23:50:11 +00001792 sqlite3ReleaseTempReg(pParse, r1);
1793 sqlite3ReleaseTempReg(pParse, r2);
drhfef52082000-06-06 01:50:43 +00001794 }
drh66a51672008-01-03 00:01:23 +00001795 sqlite3VdbeChangeP4(v, addr, (void *)&keyInfo, P4_KEYINFO);
danielk1977b3bce662005-01-29 08:32:43 +00001796 break;
drhfef52082000-06-06 01:50:43 +00001797 }
1798
drh51522cd2005-01-20 13:36:19 +00001799 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001800 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +00001801 /* This has to be a scalar SELECT. Generate code to put the
1802 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +00001803 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +00001804 */
drh2646da72005-12-09 20:02:05 +00001805 static const Token one = { (u8*)"1", 0, 1 };
drh51522cd2005-01-20 13:36:19 +00001806 Select *pSel;
danielk19776c8c8ce2008-01-02 16:27:09 +00001807 SelectDest dest;
drh1398ad32005-01-19 23:24:50 +00001808
drh51522cd2005-01-20 13:36:19 +00001809 pSel = pExpr->pSelect;
drh1013c932008-01-06 00:25:21 +00001810 sqlite3SelectDestInit(&dest, 0, ++pParse->nMem);
drh51522cd2005-01-20 13:36:19 +00001811 if( pExpr->op==TK_SELECT ){
danielk19776c8c8ce2008-01-02 16:27:09 +00001812 dest.eDest = SRT_Mem;
drh4c583122008-01-04 22:01:03 +00001813 sqlite3VdbeAddOp2(v, OP_Null, 0, dest.iParm);
drhd4e70eb2008-01-02 00:34:36 +00001814 VdbeComment((v, "Init subquery result"));
drh51522cd2005-01-20 13:36:19 +00001815 }else{
danielk19776c8c8ce2008-01-02 16:27:09 +00001816 dest.eDest = SRT_Exists;
drh4c583122008-01-04 22:01:03 +00001817 sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iParm);
drhd4e70eb2008-01-02 00:34:36 +00001818 VdbeComment((v, "Init EXISTS result"));
drh51522cd2005-01-20 13:36:19 +00001819 }
drhec7429a2005-10-06 16:53:14 +00001820 sqlite3ExprDelete(pSel->pLimit);
danielk1977a1644fd2007-08-29 12:31:25 +00001821 pSel->pLimit = sqlite3PExpr(pParse, TK_INTEGER, 0, 0, &one);
danielk19776c8c8ce2008-01-02 16:27:09 +00001822 if( sqlite3Select(pParse, pSel, &dest, 0, 0, 0, 0) ){
drh94ccde52007-04-13 16:06:32 +00001823 return;
1824 }
danielk19776c8c8ce2008-01-02 16:27:09 +00001825 pExpr->iColumn = dest.iParm;
danielk1977b3bce662005-01-29 08:32:43 +00001826 break;
drhcce7d172000-05-31 15:34:51 +00001827 }
1828 }
danielk1977b3bce662005-01-29 08:32:43 +00001829
drh57dbd7b2005-07-08 18:25:26 +00001830 if( testAddr ){
drh892d3172008-01-10 03:46:36 +00001831 sqlite3VdbeJumpHere(v, testAddr-1);
danielk1977b3bce662005-01-29 08:32:43 +00001832 }
danielk1977fc976062007-05-10 10:46:56 +00001833
danielk1977b3bce662005-01-29 08:32:43 +00001834 return;
drhcce7d172000-05-31 15:34:51 +00001835}
drh51522cd2005-01-20 13:36:19 +00001836#endif /* SQLITE_OMIT_SUBQUERY */
drhcce7d172000-05-31 15:34:51 +00001837
drhcce7d172000-05-31 15:34:51 +00001838/*
drh598f1342007-10-23 15:39:45 +00001839** Duplicate an 8-byte value
1840*/
1841static char *dup8bytes(Vdbe *v, const char *in){
1842 char *out = sqlite3DbMallocRaw(sqlite3VdbeDb(v), 8);
1843 if( out ){
1844 memcpy(out, in, 8);
1845 }
1846 return out;
1847}
1848
1849/*
1850** Generate an instruction that will put the floating point
1851** value described by z[0..n-1] on the stack.
drh0cf19ed2007-10-23 18:55:48 +00001852**
1853** The z[] string will probably not be zero-terminated. But the
1854** z[n] character is guaranteed to be something that does not look
1855** like the continuation of the number.
drh598f1342007-10-23 15:39:45 +00001856*/
drh9de221d2008-01-05 06:51:30 +00001857static void codeReal(Vdbe *v, const char *z, int n, int negateFlag, int iMem){
drh598f1342007-10-23 15:39:45 +00001858 assert( z || v==0 || sqlite3VdbeDb(v)->mallocFailed );
1859 if( z ){
1860 double value;
1861 char *zV;
drh0cf19ed2007-10-23 18:55:48 +00001862 assert( !isdigit(z[n]) );
drh598f1342007-10-23 15:39:45 +00001863 sqlite3AtoF(z, &value);
1864 if( negateFlag ) value = -value;
1865 zV = dup8bytes(v, (char*)&value);
drh9de221d2008-01-05 06:51:30 +00001866 sqlite3VdbeAddOp4(v, OP_Real, 0, iMem, 0, zV, P4_REAL);
drh598f1342007-10-23 15:39:45 +00001867 }
1868}
1869
1870
1871/*
drhfec19aa2004-05-19 20:41:03 +00001872** Generate an instruction that will put the integer describe by
1873** text z[0..n-1] on the stack.
drh0cf19ed2007-10-23 18:55:48 +00001874**
1875** The z[] string will probably not be zero-terminated. But the
1876** z[n] character is guaranteed to be something that does not look
1877** like the continuation of the number.
drhfec19aa2004-05-19 20:41:03 +00001878*/
drh9de221d2008-01-05 06:51:30 +00001879static void codeInteger(Vdbe *v, const char *z, int n, int negFlag, int iMem){
drhabb6fca2007-08-16 12:24:01 +00001880 assert( z || v==0 || sqlite3VdbeDb(v)->mallocFailed );
danielk1977c9cf9012007-05-30 10:36:47 +00001881 if( z ){
1882 int i;
drh0cf19ed2007-10-23 18:55:48 +00001883 assert( !isdigit(z[n]) );
danielk1977c9cf9012007-05-30 10:36:47 +00001884 if( sqlite3GetInt32(z, &i) ){
drh9de221d2008-01-05 06:51:30 +00001885 if( negFlag ) i = -i;
1886 sqlite3VdbeAddOp2(v, OP_Integer, i, iMem);
1887 }else if( sqlite3FitsIn64Bits(z, negFlag) ){
drh598f1342007-10-23 15:39:45 +00001888 i64 value;
1889 char *zV;
1890 sqlite3Atoi64(z, &value);
drh9de221d2008-01-05 06:51:30 +00001891 if( negFlag ) value = -value;
drh598f1342007-10-23 15:39:45 +00001892 zV = dup8bytes(v, (char*)&value);
drh9de221d2008-01-05 06:51:30 +00001893 sqlite3VdbeAddOp4(v, OP_Int64, 0, iMem, 0, zV, P4_INT64);
danielk1977c9cf9012007-05-30 10:36:47 +00001894 }else{
drh9de221d2008-01-05 06:51:30 +00001895 codeReal(v, z, n, negFlag, iMem);
danielk1977c9cf9012007-05-30 10:36:47 +00001896 }
drhfec19aa2004-05-19 20:41:03 +00001897 }
1898}
1899
drh945498f2007-02-24 11:52:52 +00001900
1901/*
1902** Generate code that will extract the iColumn-th column from
drh2133d822008-01-03 18:44:59 +00001903** table pTab and store the column value in register iMem, or on
1904** the stack if iMem==0. There is an open cursor to pTab in
1905** iTable. If iColumn<0 then code is generated that extracts the rowid.
drh945498f2007-02-24 11:52:52 +00001906*/
drh2133d822008-01-03 18:44:59 +00001907void sqlite3ExprCodeGetColumn(
1908 Vdbe *v, /* The VM being created */
1909 Table *pTab, /* Description of the table we are reading from */
1910 int iColumn, /* Index of the table column */
1911 int iTable, /* The cursor pointing to the table */
1912 int iReg /* Store results here */
1913){
drh945498f2007-02-24 11:52:52 +00001914 if( iColumn<0 ){
1915 int op = (pTab && IsVirtual(pTab)) ? OP_VRowid : OP_Rowid;
drh2133d822008-01-03 18:44:59 +00001916 sqlite3VdbeAddOp2(v, op, iTable, iReg);
drh945498f2007-02-24 11:52:52 +00001917 }else if( pTab==0 ){
drh2133d822008-01-03 18:44:59 +00001918 sqlite3VdbeAddOp3(v, OP_Column, iTable, iColumn, iReg);
drh945498f2007-02-24 11:52:52 +00001919 }else{
1920 int op = IsVirtual(pTab) ? OP_VColumn : OP_Column;
drh2133d822008-01-03 18:44:59 +00001921 sqlite3VdbeAddOp3(v, op, iTable, iColumn, iReg);
drh945498f2007-02-24 11:52:52 +00001922 sqlite3ColumnDefault(v, pTab, iColumn);
1923#ifndef SQLITE_OMIT_FLOATING_POINT
1924 if( pTab->aCol[iColumn].affinity==SQLITE_AFF_REAL ){
drh2133d822008-01-03 18:44:59 +00001925 sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg);
drh945498f2007-02-24 11:52:52 +00001926 }
1927#endif
1928 }
1929}
1930
drhfec19aa2004-05-19 20:41:03 +00001931/*
drhcce7d172000-05-31 15:34:51 +00001932** Generate code into the current Vdbe to evaluate the given
drh2dcef112008-01-12 19:03:48 +00001933** expression. Attempt to store the results in register "target".
1934** Return the register where results are stored.
drh389a1ad2008-01-03 23:44:53 +00001935**
drh2dcef112008-01-12 19:03:48 +00001936** With this routine, there is no guaranteed that results will
1937** be stored in target. The result might be stored in some other
1938** register if it is convenient to do so. The calling function
1939** must check the return code and move the results to the desired
1940** register.
drhcce7d172000-05-31 15:34:51 +00001941*/
drh2dcef112008-01-12 19:03:48 +00001942static int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){
1943 Vdbe *v = pParse->pVdbe; /* The VM under construction */
1944 int op; /* The opcode being coded */
1945 int inReg = target; /* Results stored in register inReg */
1946 int regFree1 = 0; /* If non-zero free this temporary register */
1947 int regFree2 = 0; /* If non-zero free this temporary register */
1948 int r1, r2, r3; /* Various register numbers */
drhffe07b22005-11-03 00:41:17 +00001949
drh389a1ad2008-01-03 23:44:53 +00001950 assert( v!=0 || pParse->db->mallocFailed );
drh2dcef112008-01-12 19:03:48 +00001951 assert( target>=0 );
drh389a1ad2008-01-03 23:44:53 +00001952 if( v==0 ) return 0;
drh389a1ad2008-01-03 23:44:53 +00001953
1954 if( pExpr==0 ){
1955 op = TK_NULL;
1956 }else{
1957 op = pExpr->op;
1958 }
drhf2bc0132004-10-04 13:19:23 +00001959 switch( op ){
drh13449892005-09-07 21:22:45 +00001960 case TK_AGG_COLUMN: {
1961 AggInfo *pAggInfo = pExpr->pAggInfo;
1962 struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg];
1963 if( !pAggInfo->directMode ){
drh9de221d2008-01-05 06:51:30 +00001964 assert( pCol->iMem>0 );
1965 inReg = pCol->iMem;
drh13449892005-09-07 21:22:45 +00001966 break;
1967 }else if( pAggInfo->useSortingIdx ){
drh389a1ad2008-01-03 23:44:53 +00001968 sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdx,
1969 pCol->iSorterColumn, target);
drh13449892005-09-07 21:22:45 +00001970 break;
1971 }
1972 /* Otherwise, fall thru into the TK_COLUMN case */
1973 }
drh967e8b72000-06-21 13:59:10 +00001974 case TK_COLUMN: {
drhffe07b22005-11-03 00:41:17 +00001975 if( pExpr->iTable<0 ){
1976 /* This only happens when coding check constraints */
drhaa9b8962008-01-08 02:57:55 +00001977 assert( pParse->ckBase>0 );
1978 inReg = pExpr->iColumn + pParse->ckBase;
drhc4a3c772001-04-04 11:48:57 +00001979 }else{
drh2133d822008-01-03 18:44:59 +00001980 sqlite3ExprCodeGetColumn(v, pExpr->pTab,
drh389a1ad2008-01-03 23:44:53 +00001981 pExpr->iColumn, pExpr->iTable, target);
drh22827922000-06-06 17:27:05 +00001982 }
drhcce7d172000-05-31 15:34:51 +00001983 break;
1984 }
1985 case TK_INTEGER: {
drh9de221d2008-01-05 06:51:30 +00001986 codeInteger(v, (char*)pExpr->token.z, pExpr->token.n, 0, target);
drhfec19aa2004-05-19 20:41:03 +00001987 break;
1988 }
drh598f1342007-10-23 15:39:45 +00001989 case TK_FLOAT: {
drh9de221d2008-01-05 06:51:30 +00001990 codeReal(v, (char*)pExpr->token.z, pExpr->token.n, 0, target);
drh598f1342007-10-23 15:39:45 +00001991 break;
1992 }
drhfec19aa2004-05-19 20:41:03 +00001993 case TK_STRING: {
danielk19771e536952007-08-16 10:09:01 +00001994 sqlite3DequoteExpr(pParse->db, pExpr);
drh9de221d2008-01-05 06:51:30 +00001995 sqlite3VdbeAddOp4(v,OP_String8, 0, target, 0,
drh66a51672008-01-03 00:01:23 +00001996 (char*)pExpr->token.z, pExpr->token.n);
drhcce7d172000-05-31 15:34:51 +00001997 break;
1998 }
drhf0863fe2005-06-12 21:35:51 +00001999 case TK_NULL: {
drh9de221d2008-01-05 06:51:30 +00002000 sqlite3VdbeAddOp2(v, OP_Null, 0, target);
drhf0863fe2005-06-12 21:35:51 +00002001 break;
2002 }
danielk19775338a5f2005-01-20 13:03:10 +00002003#ifndef SQLITE_OMIT_BLOB_LITERAL
danielk1977c572ef72004-05-27 09:28:41 +00002004 case TK_BLOB: {
drh6c8c6ce2005-08-23 11:17:58 +00002005 int n;
2006 const char *z;
drhf2bc0132004-10-04 13:19:23 +00002007 assert( TK_BLOB==OP_HexBlob );
drh6c8c6ce2005-08-23 11:17:58 +00002008 n = pExpr->token.n - 3;
drh2646da72005-12-09 20:02:05 +00002009 z = (char*)pExpr->token.z + 2;
drh6c8c6ce2005-08-23 11:17:58 +00002010 assert( n>=0 );
2011 if( n==0 ){
2012 z = "";
2013 }
drh9de221d2008-01-05 06:51:30 +00002014 sqlite3VdbeAddOp4(v, op, 0, target, 0, z, n);
danielk1977c572ef72004-05-27 09:28:41 +00002015 break;
2016 }
danielk19775338a5f2005-01-20 13:03:10 +00002017#endif
drh50457892003-09-06 01:10:47 +00002018 case TK_VARIABLE: {
drh9de221d2008-01-05 06:51:30 +00002019 sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iTable, target);
drh895d7472004-08-20 16:02:39 +00002020 if( pExpr->token.n>1 ){
drh66a51672008-01-03 00:01:23 +00002021 sqlite3VdbeChangeP4(v, -1, (char*)pExpr->token.z, pExpr->token.n);
drh895d7472004-08-20 16:02:39 +00002022 }
drh50457892003-09-06 01:10:47 +00002023 break;
2024 }
drh4e0cff62004-11-05 05:10:28 +00002025 case TK_REGISTER: {
drh9de221d2008-01-05 06:51:30 +00002026 inReg = pExpr->iTable;
drh4e0cff62004-11-05 05:10:28 +00002027 break;
2028 }
drh487e2622005-06-25 18:42:14 +00002029#ifndef SQLITE_OMIT_CAST
2030 case TK_CAST: {
2031 /* Expressions of the form: CAST(pLeft AS token) */
danielk1977f0113002006-01-24 12:09:17 +00002032 int aff, to_op;
drh2dcef112008-01-12 19:03:48 +00002033 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
drh8a512562005-11-14 22:29:05 +00002034 aff = sqlite3AffinityType(&pExpr->token);
danielk1977f0113002006-01-24 12:09:17 +00002035 to_op = aff - SQLITE_AFF_TEXT + OP_ToText;
2036 assert( to_op==OP_ToText || aff!=SQLITE_AFF_TEXT );
2037 assert( to_op==OP_ToBlob || aff!=SQLITE_AFF_NONE );
2038 assert( to_op==OP_ToNumeric || aff!=SQLITE_AFF_NUMERIC );
2039 assert( to_op==OP_ToInt || aff!=SQLITE_AFF_INTEGER );
2040 assert( to_op==OP_ToReal || aff!=SQLITE_AFF_REAL );
drh2dcef112008-01-12 19:03:48 +00002041 sqlite3VdbeAddOp1(v, to_op, inReg);
drh487e2622005-06-25 18:42:14 +00002042 break;
2043 }
2044#endif /* SQLITE_OMIT_CAST */
drhc9b84a12002-06-20 11:36:48 +00002045 case TK_LT:
2046 case TK_LE:
2047 case TK_GT:
2048 case TK_GE:
2049 case TK_NE:
2050 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00002051 assert( TK_LT==OP_Lt );
2052 assert( TK_LE==OP_Le );
2053 assert( TK_GT==OP_Gt );
2054 assert( TK_GE==OP_Ge );
2055 assert( TK_EQ==OP_Eq );
2056 assert( TK_NE==OP_Ne );
drh2dcef112008-01-12 19:03:48 +00002057 if( target==0 ){
drh35573352008-01-08 23:54:25 +00002058 inReg = ++pParse->nMem;
2059 }
drh2dcef112008-01-12 19:03:48 +00002060 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
2061 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
drh35573352008-01-08 23:54:25 +00002062 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
2063 r1, r2, inReg, SQLITE_STOREP2);
danielk1977a37cdde2004-05-16 11:15:36 +00002064 break;
drhc9b84a12002-06-20 11:36:48 +00002065 }
drhcce7d172000-05-31 15:34:51 +00002066 case TK_AND:
2067 case TK_OR:
2068 case TK_PLUS:
2069 case TK_STAR:
2070 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00002071 case TK_REM:
2072 case TK_BITAND:
2073 case TK_BITOR:
drh17c40292004-07-21 02:53:29 +00002074 case TK_SLASH:
drhbf4133c2001-10-13 02:59:08 +00002075 case TK_LSHIFT:
drh855eb1c2004-08-31 13:45:11 +00002076 case TK_RSHIFT:
drh00400772000-06-16 20:51:26 +00002077 case TK_CONCAT: {
drhf2bc0132004-10-04 13:19:23 +00002078 assert( TK_AND==OP_And );
2079 assert( TK_OR==OP_Or );
2080 assert( TK_PLUS==OP_Add );
2081 assert( TK_MINUS==OP_Subtract );
2082 assert( TK_REM==OP_Remainder );
2083 assert( TK_BITAND==OP_BitAnd );
2084 assert( TK_BITOR==OP_BitOr );
2085 assert( TK_SLASH==OP_Divide );
2086 assert( TK_LSHIFT==OP_ShiftLeft );
2087 assert( TK_RSHIFT==OP_ShiftRight );
2088 assert( TK_CONCAT==OP_Concat );
drh2dcef112008-01-12 19:03:48 +00002089 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
2090 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
drh5b6afba2008-01-05 16:29:28 +00002091 sqlite3VdbeAddOp3(v, op, r2, r1, target);
drh00400772000-06-16 20:51:26 +00002092 break;
2093 }
drhcce7d172000-05-31 15:34:51 +00002094 case TK_UMINUS: {
drhfec19aa2004-05-19 20:41:03 +00002095 Expr *pLeft = pExpr->pLeft;
2096 assert( pLeft );
2097 if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
2098 Token *p = &pLeft->token;
drhfec19aa2004-05-19 20:41:03 +00002099 if( pLeft->op==TK_FLOAT ){
drh9de221d2008-01-05 06:51:30 +00002100 codeReal(v, (char*)p->z, p->n, 1, target);
drhe6840902002-03-06 03:08:25 +00002101 }else{
drh9de221d2008-01-05 06:51:30 +00002102 codeInteger(v, (char*)p->z, p->n, 1, target);
drhe6840902002-03-06 03:08:25 +00002103 }
drh3c84ddf2008-01-09 02:15:38 +00002104 }else{
drh2dcef112008-01-12 19:03:48 +00002105 regFree1 = r1 = sqlite3GetTempReg(pParse);
drh3c84ddf2008-01-09 02:15:38 +00002106 sqlite3VdbeAddOp2(v, OP_Integer, 0, r1);
drh2dcef112008-01-12 19:03:48 +00002107 r2 = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
2108 sqlite3VdbeAddOp3(v, OP_Subtract, r2, r1, target);
drh6e142f52000-06-08 13:36:40 +00002109 }
drh3c84ddf2008-01-09 02:15:38 +00002110 inReg = target;
2111 break;
drh6e142f52000-06-08 13:36:40 +00002112 }
drhbf4133c2001-10-13 02:59:08 +00002113 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00002114 case TK_NOT: {
drhf2bc0132004-10-04 13:19:23 +00002115 assert( TK_BITNOT==OP_BitNot );
2116 assert( TK_NOT==OP_Not );
drh2dcef112008-01-12 19:03:48 +00002117 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
2118 sqlite3VdbeAddOp1(v, op, inReg);
drhcce7d172000-05-31 15:34:51 +00002119 break;
2120 }
2121 case TK_ISNULL:
2122 case TK_NOTNULL: {
drh6a288a32008-01-07 19:20:24 +00002123 int addr;
drhf2bc0132004-10-04 13:19:23 +00002124 assert( TK_ISNULL==OP_IsNull );
2125 assert( TK_NOTNULL==OP_NotNull );
drh9de221d2008-01-05 06:51:30 +00002126 sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
drh2dcef112008-01-12 19:03:48 +00002127 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
2128 addr = sqlite3VdbeAddOp1(v, op, r1);
drh9de221d2008-01-05 06:51:30 +00002129 sqlite3VdbeAddOp2(v, OP_AddImm, target, -1);
drh6a288a32008-01-07 19:20:24 +00002130 sqlite3VdbeJumpHere(v, addr);
drhf2bc0132004-10-04 13:19:23 +00002131 break;
drhcce7d172000-05-31 15:34:51 +00002132 }
drh22827922000-06-06 17:27:05 +00002133 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00002134 AggInfo *pInfo = pExpr->pAggInfo;
drh7e56e712005-11-16 12:53:15 +00002135 if( pInfo==0 ){
2136 sqlite3ErrorMsg(pParse, "misuse of aggregate: %T",
2137 &pExpr->span);
2138 }else{
drh9de221d2008-01-05 06:51:30 +00002139 inReg = pInfo->aFunc[pExpr->iAgg].iMem;
drh7e56e712005-11-16 12:53:15 +00002140 }
drh22827922000-06-06 17:27:05 +00002141 break;
2142 }
drhb71090f2005-05-23 17:26:51 +00002143 case TK_CONST_FUNC:
drhcce7d172000-05-31 15:34:51 +00002144 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00002145 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00002146 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00002147 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00002148 int nId;
2149 const char *zId;
drh13449892005-09-07 21:22:45 +00002150 int constMask = 0;
danielk1977682f68b2004-06-05 10:22:17 +00002151 int i;
drh17435752007-08-16 04:30:38 +00002152 sqlite3 *db = pParse->db;
2153 u8 enc = ENC(db);
danielk1977dc1bdc42004-06-11 10:51:27 +00002154 CollSeq *pColl = 0;
drh17435752007-08-16 04:30:38 +00002155
drh2646da72005-12-09 20:02:05 +00002156 zId = (char*)pExpr->token.z;
drhb71090f2005-05-23 17:26:51 +00002157 nId = pExpr->token.n;
danielk1977d8123362004-06-12 09:25:12 +00002158 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0);
drh0bce8352002-02-28 00:41:10 +00002159 assert( pDef!=0 );
drh892d3172008-01-10 03:46:36 +00002160 if( pList ){
2161 nExpr = pList->nExpr;
drh2dcef112008-01-12 19:03:48 +00002162 r1 = sqlite3GetTempRange(pParse, nExpr);
2163 sqlite3ExprCodeExprList(pParse, pList, r1);
drh892d3172008-01-10 03:46:36 +00002164 }else{
2165 nExpr = 0;
2166 }
drhb7f6f682006-07-08 17:06:43 +00002167#ifndef SQLITE_OMIT_VIRTUALTABLE
drha43fa222006-07-08 18:41:37 +00002168 /* Possibly overload the function if the first argument is
2169 ** a virtual table column.
2170 **
2171 ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the
2172 ** second argument, not the first, as the argument to test to
2173 ** see if it is a column in a virtual table. This is done because
2174 ** the left operand of infix functions (the operand we want to
2175 ** control overloading) ends up as the second argument to the
2176 ** function. The expression "A glob B" is equivalent to
2177 ** "glob(B,A). We want to use the A in "A glob B" to test
2178 ** for function overloading. But we use the B term in "glob(B,A)".
2179 */
drh6a03a1c2006-07-08 18:34:59 +00002180 if( nExpr>=2 && (pExpr->flags & EP_InfixFunc) ){
drh17435752007-08-16 04:30:38 +00002181 pDef = sqlite3VtabOverloadFunction(db, pDef, nExpr, pList->a[1].pExpr);
drh6a03a1c2006-07-08 18:34:59 +00002182 }else if( nExpr>0 ){
drh17435752007-08-16 04:30:38 +00002183 pDef = sqlite3VtabOverloadFunction(db, pDef, nExpr, pList->a[0].pExpr);
drhb7f6f682006-07-08 17:06:43 +00002184 }
2185#endif
danielk1977682f68b2004-06-05 10:22:17 +00002186 for(i=0; i<nExpr && i<32; i++){
danielk1977d02eb1f2004-06-06 09:44:03 +00002187 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
drh13449892005-09-07 21:22:45 +00002188 constMask |= (1<<i);
danielk1977d02eb1f2004-06-06 09:44:03 +00002189 }
danielk1977dc1bdc42004-06-11 10:51:27 +00002190 if( pDef->needCollSeq && !pColl ){
2191 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
2192 }
2193 }
2194 if( pDef->needCollSeq ){
2195 if( !pColl ) pColl = pParse->db->pDfltColl;
drh66a51672008-01-03 00:01:23 +00002196 sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ);
danielk1977682f68b2004-06-05 10:22:17 +00002197 }
drh2dcef112008-01-12 19:03:48 +00002198 sqlite3VdbeAddOp4(v, OP_Function, constMask, r1, target,
drh66a51672008-01-03 00:01:23 +00002199 (char*)pDef, P4_FUNCDEF);
drh98757152008-01-09 23:04:12 +00002200 sqlite3VdbeChangeP5(v, nExpr);
drh2dcef112008-01-12 19:03:48 +00002201 if( nExpr ){
2202 sqlite3ReleaseTempRange(pParse, r1, nExpr);
2203 }
drhcce7d172000-05-31 15:34:51 +00002204 break;
2205 }
drhfe2093d2005-01-20 22:48:47 +00002206#ifndef SQLITE_OMIT_SUBQUERY
2207 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00002208 case TK_SELECT: {
drh41714d62006-03-02 04:44:23 +00002209 if( pExpr->iColumn==0 ){
2210 sqlite3CodeSubselect(pParse, pExpr);
2211 }
drh9de221d2008-01-05 06:51:30 +00002212 inReg = pExpr->iColumn;
drh19a775c2000-06-05 18:54:46 +00002213 break;
2214 }
drhfef52082000-06-06 01:50:43 +00002215 case TK_IN: {
drh6a288a32008-01-07 19:20:24 +00002216 int j1, j2, j3, j4, j5;
drh94a11212004-09-25 13:12:14 +00002217 char affinity;
danielk19779a96b662007-11-29 17:05:18 +00002218 int eType;
danielk19779a96b662007-11-29 17:05:18 +00002219
2220 eType = sqlite3FindInIndex(pParse, pExpr, 0);
danielk1977e014a832004-05-17 10:48:57 +00002221
2222 /* Figure out the affinity to use to create a key from the results
2223 ** of the expression. affinityStr stores a static string suitable for
drh66a51672008-01-03 00:01:23 +00002224 ** P4 of OP_MakeRecord.
danielk1977e014a832004-05-17 10:48:57 +00002225 */
drh94a11212004-09-25 13:12:14 +00002226 affinity = comparisonAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +00002227
drh2dcef112008-01-12 19:03:48 +00002228 if( target==0 ){
2229 target = inReg = ++pParse->nMem;
drh2d401ab2008-01-10 23:50:11 +00002230 }
drh2dcef112008-01-12 19:03:48 +00002231 sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
danielk1977e014a832004-05-17 10:48:57 +00002232
2233 /* Code the <expr> from "<expr> IN (...)". The temporary table
2234 ** pExpr->iTable contains the values that make up the (...) set.
2235 */
drh2dcef112008-01-12 19:03:48 +00002236 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
2237 j1 = sqlite3VdbeAddOp1(v, OP_NotNull, r1);
2238 sqlite3VdbeAddOp2(v, OP_Null, 0, target);
drh6a288a32008-01-07 19:20:24 +00002239 j2 = sqlite3VdbeAddOp0(v, OP_Goto);
2240 sqlite3VdbeJumpHere(v, j1);
danielk19779a96b662007-11-29 17:05:18 +00002241 if( eType==IN_INDEX_ROWID ){
drh2dcef112008-01-12 19:03:48 +00002242 j3 = sqlite3VdbeAddOp3(v, OP_MustBeInt, r1, 0, 1);
2243 j4 = sqlite3VdbeAddOp3(v, OP_NotExists, pExpr->iTable, 0, r1);
drh6a288a32008-01-07 19:20:24 +00002244 j5 = sqlite3VdbeAddOp0(v, OP_Goto);
2245 sqlite3VdbeJumpHere(v, j3);
2246 sqlite3VdbeJumpHere(v, j4);
danielk19779a96b662007-11-29 17:05:18 +00002247 }else{
drh2dcef112008-01-12 19:03:48 +00002248 r2 = regFree2 = sqlite3GetTempReg(pParse);
2249 sqlite3VdbeAddOp4(v, OP_RegMakeRec, r1, 1, r2, &affinity, 1);
2250 j5 = sqlite3VdbeAddOp3(v, OP_Found, pExpr->iTable, 0, r2);
danielk19779a96b662007-11-29 17:05:18 +00002251 }
drh2dcef112008-01-12 19:03:48 +00002252 sqlite3VdbeAddOp2(v, OP_AddImm, target, -1);
drh6a288a32008-01-07 19:20:24 +00002253 sqlite3VdbeJumpHere(v, j2);
2254 sqlite3VdbeJumpHere(v, j5);
drhfef52082000-06-06 01:50:43 +00002255 break;
2256 }
danielk197793758c82005-01-21 08:13:14 +00002257#endif
drh2dcef112008-01-12 19:03:48 +00002258 /*
2259 ** x BETWEEN y AND z
2260 **
2261 ** This is equivalent to
2262 **
2263 ** x>=y AND x<=z
2264 **
2265 ** X is stored in pExpr->pLeft.
2266 ** Y is stored in pExpr->pList->a[0].pExpr.
2267 ** Z is stored in pExpr->pList->a[1].pExpr.
2268 */
drhfef52082000-06-06 01:50:43 +00002269 case TK_BETWEEN: {
drhbe5c89a2004-07-26 00:31:09 +00002270 Expr *pLeft = pExpr->pLeft;
2271 struct ExprList_item *pLItem = pExpr->pList->a;
2272 Expr *pRight = pLItem->pExpr;
drh35573352008-01-08 23:54:25 +00002273
drh2dcef112008-01-12 19:03:48 +00002274 if( target==0 ){
2275 inReg = target = ++pParse->nMem;
drh35573352008-01-08 23:54:25 +00002276 }
drh2dcef112008-01-12 19:03:48 +00002277 r1 = sqlite3ExprCodeTemp(pParse, pLeft, &regFree1);
2278 r2 = sqlite3ExprCodeTemp(pParse, pRight, &regFree2);
2279 r3 = sqlite3GetTempReg(pParse);
drh35573352008-01-08 23:54:25 +00002280 codeCompare(pParse, pLeft, pRight, OP_Ge,
2281 r1, r2, r3, SQLITE_STOREP2);
drhbe5c89a2004-07-26 00:31:09 +00002282 pLItem++;
2283 pRight = pLItem->pExpr;
drh2dcef112008-01-12 19:03:48 +00002284 sqlite3ReleaseTempReg(pParse, regFree2);
2285 r2 = sqlite3ExprCodeTemp(pParse, pRight, &regFree2);
2286 codeCompare(pParse, pLeft, pRight, OP_Le, r1, r2, r2, SQLITE_STOREP2);
2287 sqlite3VdbeAddOp3(v, OP_And, r3, r2, target);
2288 sqlite3ReleaseTempReg(pParse, r3);
drhfef52082000-06-06 01:50:43 +00002289 break;
2290 }
drh4f07e5f2007-05-14 11:34:46 +00002291 case TK_UPLUS: {
drh2dcef112008-01-12 19:03:48 +00002292 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
drha2e00042002-01-22 03:13:42 +00002293 break;
2294 }
drh2dcef112008-01-12 19:03:48 +00002295
2296 /*
2297 ** Form A:
2298 ** CASE x WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
2299 **
2300 ** Form B:
2301 ** CASE WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
2302 **
2303 ** Form A is can be transformed into the equivalent form B as follows:
2304 ** CASE WHEN x=e1 THEN r1 WHEN x=e2 THEN r2 ...
2305 ** WHEN x=eN THEN rN ELSE y END
2306 **
2307 ** X (if it exists) is in pExpr->pLeft.
2308 ** Y is in pExpr->pRight. The Y is also optional. If there is no
2309 ** ELSE clause and no other term matches, then the result of the
2310 ** exprssion is NULL.
2311 ** Ei is in pExpr->pList->a[i*2] and Ri is pExpr->pList->a[i*2+1].
2312 **
2313 ** The result of the expression is the Ri for the first matching Ei,
2314 ** or if there is no matching Ei, the ELSE term Y, or if there is
2315 ** no ELSE term, NULL.
2316 */
drh17a7f8d2002-03-24 13:13:27 +00002317 case TK_CASE: {
drh2dcef112008-01-12 19:03:48 +00002318 int endLabel; /* GOTO label for end of CASE stmt */
2319 int nextCase; /* GOTO label for next WHEN clause */
2320 int nExpr; /* 2x number of WHEN terms */
2321 int i; /* Loop counter */
2322 ExprList *pEList; /* List of WHEN terms */
2323 struct ExprList_item *aListelem; /* Array of WHEN terms */
2324 Expr opCompare; /* The X==Ei expression */
2325 Expr cacheX; /* Cached expression X */
2326 Expr *pX; /* The X expression */
2327 Expr *pTest; /* X==Ei (form A) or just Ei (form B) */
drh17a7f8d2002-03-24 13:13:27 +00002328
2329 assert(pExpr->pList);
2330 assert((pExpr->pList->nExpr % 2) == 0);
2331 assert(pExpr->pList->nExpr > 0);
drhbe5c89a2004-07-26 00:31:09 +00002332 pEList = pExpr->pList;
2333 aListelem = pEList->a;
2334 nExpr = pEList->nExpr;
drh2dcef112008-01-12 19:03:48 +00002335 endLabel = sqlite3VdbeMakeLabel(v);
2336 if( (pX = pExpr->pLeft)!=0 ){
2337 cacheX = *pX;
2338 cacheX.iTable = sqlite3ExprCodeTemp(pParse, pX, &regFree1);
2339 cacheX.op = TK_REGISTER;
2340 opCompare.op = TK_EQ;
2341 opCompare.pLeft = &cacheX;
2342 pTest = &opCompare;
drh17a7f8d2002-03-24 13:13:27 +00002343 }
drhf5905aa2002-05-26 20:54:33 +00002344 for(i=0; i<nExpr; i=i+2){
drh2dcef112008-01-12 19:03:48 +00002345 if( pX ){
2346 opCompare.pRight = aListelem[i].pExpr;
drh17a7f8d2002-03-24 13:13:27 +00002347 }else{
drh2dcef112008-01-12 19:03:48 +00002348 pTest = aListelem[i].pExpr;
drh17a7f8d2002-03-24 13:13:27 +00002349 }
drh2dcef112008-01-12 19:03:48 +00002350 nextCase = sqlite3VdbeMakeLabel(v);
2351 sqlite3ExprIfFalse(pParse, pTest, nextCase, SQLITE_JUMPIFNULL);
drh9de221d2008-01-05 06:51:30 +00002352 sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target);
drh2dcef112008-01-12 19:03:48 +00002353 sqlite3VdbeAddOp2(v, OP_Goto, 0, endLabel);
2354 sqlite3VdbeResolveLabel(v, nextCase);
drhf570f012002-05-31 15:51:25 +00002355 }
drh17a7f8d2002-03-24 13:13:27 +00002356 if( pExpr->pRight ){
drh9de221d2008-01-05 06:51:30 +00002357 sqlite3ExprCode(pParse, pExpr->pRight, target);
drh17a7f8d2002-03-24 13:13:27 +00002358 }else{
drh9de221d2008-01-05 06:51:30 +00002359 sqlite3VdbeAddOp2(v, OP_Null, 0, target);
drh17a7f8d2002-03-24 13:13:27 +00002360 }
drh2dcef112008-01-12 19:03:48 +00002361 sqlite3VdbeResolveLabel(v, endLabel);
danielk19776f349032002-06-11 02:25:40 +00002362 break;
2363 }
danielk19775338a5f2005-01-20 13:03:10 +00002364#ifndef SQLITE_OMIT_TRIGGER
danielk19776f349032002-06-11 02:25:40 +00002365 case TK_RAISE: {
2366 if( !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +00002367 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00002368 "RAISE() may only be used within a trigger-program");
drh389a1ad2008-01-03 23:44:53 +00002369 return 0;
danielk19776f349032002-06-11 02:25:40 +00002370 }
drhad6d9462004-09-19 02:15:24 +00002371 if( pExpr->iColumn!=OE_Ignore ){
2372 assert( pExpr->iColumn==OE_Rollback ||
2373 pExpr->iColumn == OE_Abort ||
2374 pExpr->iColumn == OE_Fail );
danielk19771e536952007-08-16 10:09:01 +00002375 sqlite3DequoteExpr(pParse->db, pExpr);
drh66a51672008-01-03 00:01:23 +00002376 sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn, 0,
drh2646da72005-12-09 20:02:05 +00002377 (char*)pExpr->token.z, pExpr->token.n);
danielk19776f349032002-06-11 02:25:40 +00002378 } else {
drhad6d9462004-09-19 02:15:24 +00002379 assert( pExpr->iColumn == OE_Ignore );
drh66a51672008-01-03 00:01:23 +00002380 sqlite3VdbeAddOp2(v, OP_ContextPop, 0, 0);
2381 sqlite3VdbeAddOp2(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
drhd4e70eb2008-01-02 00:34:36 +00002382 VdbeComment((v, "raise(IGNORE)"));
danielk19776f349032002-06-11 02:25:40 +00002383 }
drhffe07b22005-11-03 00:41:17 +00002384 break;
drh17a7f8d2002-03-24 13:13:27 +00002385 }
danielk19775338a5f2005-01-20 13:03:10 +00002386#endif
drhffe07b22005-11-03 00:41:17 +00002387 }
drh2dcef112008-01-12 19:03:48 +00002388 sqlite3ReleaseTempReg(pParse, regFree1);
2389 sqlite3ReleaseTempReg(pParse, regFree2);
2390 return inReg;
2391}
2392
2393/*
2394** Generate code to evaluate an expression and store the results
2395** into a register. Return the register number where the results
2396** are stored.
2397**
2398** If the register is a temporary register that can be deallocated,
2399** then write its number into *pReg. If the result register is no
2400** a temporary, then set *pReg to zero.
2401*/
2402int sqlite3ExprCodeTemp(Parse *pParse, Expr *pExpr, int *pReg){
2403 int r1 = sqlite3GetTempReg(pParse);
2404 int r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
2405 if( r2==r1 ){
2406 *pReg = r1;
2407 }else{
2408 sqlite3ReleaseTempReg(pParse, r1);
2409 *pReg = 0;
2410 }
2411 return r2;
2412}
2413
2414/*
2415** Generate code that will evaluate expression pExpr and store the
2416** results in register target. The results are guaranteed to appear
2417** in register target.
2418*/
2419int sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){
2420 int inReg = sqlite3ExprCodeTarget(pParse, pExpr, target);
drh0e359b32008-01-13 19:02:11 +00002421 assert( pParse->pVdbe || pParse->db->mallocFailed );
2422 if( inReg!=target && pParse->pVdbe ){
drh2dcef112008-01-12 19:03:48 +00002423 sqlite3VdbeAddOp2(pParse->pVdbe, (inReg>0 ? OP_SCopy : OP_Move),
2424 inReg, target);
drhcce7d172000-05-31 15:34:51 +00002425 }
drh389a1ad2008-01-03 23:44:53 +00002426 return target;
drhcce7d172000-05-31 15:34:51 +00002427}
2428
2429/*
drh2dcef112008-01-12 19:03:48 +00002430** Generate code that evalutes the given expression and puts the result
2431** in register target. If target==-1, then allocate a temporary register
2432** in which to store the result. In either case, return the register
2433** number where the result is stored.
drh25303782004-12-07 15:41:48 +00002434**
drh2dcef112008-01-12 19:03:48 +00002435** Also make a copy of the expression results into another "cache" register
2436** and modify the expression so that the next time it is evaluated,
2437** the result is a copy of the cache register.
2438**
2439** This routine is used for expressions that are used multiple
2440** times. They are evaluated once and the results of the expression
2441** are reused.
drh25303782004-12-07 15:41:48 +00002442*/
drh2dcef112008-01-12 19:03:48 +00002443int sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr, int target){
drh25303782004-12-07 15:41:48 +00002444 Vdbe *v = pParse->pVdbe;
drh2dcef112008-01-12 19:03:48 +00002445 int inReg;
2446 inReg = sqlite3ExprCode(pParse, pExpr, target);
2447 if( pExpr->op!=TK_REGISTER ){
2448 int iMem;
2449 if( target<0 ){
2450 iMem = inReg;
2451 }else{
2452 iMem = ++pParse->nMem;
2453 sqlite3VdbeAddOp2(v, OP_Copy, inReg, iMem);
2454 }
2455 pExpr->iTable = iMem;
drh25303782004-12-07 15:41:48 +00002456 pExpr->op = TK_REGISTER;
2457 }
drh2dcef112008-01-12 19:03:48 +00002458 return inReg;
drh25303782004-12-07 15:41:48 +00002459}
drh2dcef112008-01-12 19:03:48 +00002460
drh25303782004-12-07 15:41:48 +00002461
2462/*
drh268380c2004-02-25 13:47:31 +00002463** Generate code that pushes the value of every element of the given
drh892d3172008-01-10 03:46:36 +00002464** expression list onto the stack if target==0 or into a sequence of
2465** registers beginning at target.
drh268380c2004-02-25 13:47:31 +00002466**
drh892d3172008-01-10 03:46:36 +00002467** Return the number of elements evaluated.
drh268380c2004-02-25 13:47:31 +00002468*/
danielk19774adee202004-05-08 08:23:19 +00002469int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00002470 Parse *pParse, /* Parsing context */
drh389a1ad2008-01-03 23:44:53 +00002471 ExprList *pList, /* The expression list to be coded */
2472 int target /* Where to write results */
drh268380c2004-02-25 13:47:31 +00002473){
2474 struct ExprList_item *pItem;
drh389a1ad2008-01-03 23:44:53 +00002475 int i, n, incr = 1;
drh892d3172008-01-10 03:46:36 +00002476 assert( pList!=0 || pParse->db->mallocFailed );
2477 if( pList==0 ){
2478 return 0;
2479 }
2480 assert( target>=0 );
drh268380c2004-02-25 13:47:31 +00002481 n = pList->nExpr;
drh892d3172008-01-10 03:46:36 +00002482 if( target==0 ){
drh389a1ad2008-01-03 23:44:53 +00002483 incr = 0;
2484 }
drhc182d162005-08-14 20:47:16 +00002485 for(pItem=pList->a, i=n; i>0; i--, pItem++){
drh389a1ad2008-01-03 23:44:53 +00002486 sqlite3ExprCode(pParse, pItem->pExpr, target);
2487 target += incr;
drh268380c2004-02-25 13:47:31 +00002488 }
drhf9b596e2004-05-26 16:54:42 +00002489 return n;
drh268380c2004-02-25 13:47:31 +00002490}
2491
2492/*
drhcce7d172000-05-31 15:34:51 +00002493** Generate code for a boolean expression such that a jump is made
2494** to the label "dest" if the expression is true but execution
2495** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00002496**
2497** If the expression evaluates to NULL (neither true nor false), then
drh35573352008-01-08 23:54:25 +00002498** take the jump if the jumpIfNull flag is SQLITE_JUMPIFNULL.
drhf2bc0132004-10-04 13:19:23 +00002499**
2500** This code depends on the fact that certain token values (ex: TK_EQ)
2501** are the same as opcode values (ex: OP_Eq) that implement the corresponding
2502** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
2503** the make process cause these values to align. Assert()s in the code
2504** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00002505*/
danielk19774adee202004-05-08 08:23:19 +00002506void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00002507 Vdbe *v = pParse->pVdbe;
2508 int op = 0;
drh2dcef112008-01-12 19:03:48 +00002509 int regFree1 = 0;
2510 int regFree2 = 0;
2511 int r1, r2;
2512
drh35573352008-01-08 23:54:25 +00002513 assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
drhdaffd0e2001-04-11 14:28:42 +00002514 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00002515 op = pExpr->op;
2516 switch( op ){
drhcce7d172000-05-31 15:34:51 +00002517 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00002518 int d2 = sqlite3VdbeMakeLabel(v);
drh35573352008-01-08 23:54:25 +00002519 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,jumpIfNull^SQLITE_JUMPIFNULL);
danielk19774adee202004-05-08 08:23:19 +00002520 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
2521 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00002522 break;
2523 }
2524 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00002525 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
2526 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002527 break;
2528 }
2529 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00002530 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002531 break;
2532 }
2533 case TK_LT:
2534 case TK_LE:
2535 case TK_GT:
2536 case TK_GE:
2537 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00002538 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00002539 assert( TK_LT==OP_Lt );
2540 assert( TK_LE==OP_Le );
2541 assert( TK_GT==OP_Gt );
2542 assert( TK_GE==OP_Ge );
2543 assert( TK_EQ==OP_Eq );
2544 assert( TK_NE==OP_Ne );
drh2dcef112008-01-12 19:03:48 +00002545 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
2546 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
drh35573352008-01-08 23:54:25 +00002547 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
drh2dcef112008-01-12 19:03:48 +00002548 r1, r2, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002549 break;
2550 }
2551 case TK_ISNULL:
2552 case TK_NOTNULL: {
drhf2bc0132004-10-04 13:19:23 +00002553 assert( TK_ISNULL==OP_IsNull );
2554 assert( TK_NOTNULL==OP_NotNull );
drh2dcef112008-01-12 19:03:48 +00002555 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
2556 sqlite3VdbeAddOp2(v, op, r1, dest);
drhcce7d172000-05-31 15:34:51 +00002557 break;
2558 }
drhfef52082000-06-06 01:50:43 +00002559 case TK_BETWEEN: {
drh2dcef112008-01-12 19:03:48 +00002560 /* x BETWEEN y AND z
danielk19770202b292004-06-09 09:55:16 +00002561 **
drh2dcef112008-01-12 19:03:48 +00002562 ** Is equivalent to
2563 **
2564 ** x>=y AND x<=z
2565 **
2566 ** Code it as such, taking care to do the common subexpression
2567 ** elementation of x.
danielk19770202b292004-06-09 09:55:16 +00002568 */
drh2dcef112008-01-12 19:03:48 +00002569 Expr exprAnd;
2570 Expr compLeft;
2571 Expr compRight;
2572 Expr exprX;
danielk19770202b292004-06-09 09:55:16 +00002573
drh2dcef112008-01-12 19:03:48 +00002574 exprX = *pExpr->pLeft;
2575 exprAnd.op = TK_AND;
2576 exprAnd.pLeft = &compLeft;
2577 exprAnd.pRight = &compRight;
2578 compLeft.op = TK_GE;
2579 compLeft.pLeft = &exprX;
2580 compLeft.pRight = pExpr->pList->a[0].pExpr;
2581 compRight.op = TK_LE;
2582 compRight.pLeft = &exprX;
2583 compRight.pRight = pExpr->pList->a[1].pExpr;
2584 exprX.iTable = sqlite3ExprCodeTemp(pParse, &exprX, &regFree1);
2585 exprX.op = TK_REGISTER;
2586 sqlite3ExprIfTrue(pParse, &exprAnd, dest, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00002587 break;
2588 }
drhcce7d172000-05-31 15:34:51 +00002589 default: {
drh2dcef112008-01-12 19:03:48 +00002590 r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
2591 sqlite3VdbeAddOp3(v, OP_If, r1, dest, jumpIfNull!=0);
drhcce7d172000-05-31 15:34:51 +00002592 break;
2593 }
2594 }
drh2dcef112008-01-12 19:03:48 +00002595 sqlite3ReleaseTempReg(pParse, regFree1);
2596 sqlite3ReleaseTempReg(pParse, regFree2);
drhcce7d172000-05-31 15:34:51 +00002597}
2598
2599/*
drh66b89c82000-11-28 20:47:17 +00002600** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00002601** to the label "dest" if the expression is false but execution
2602** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00002603**
2604** If the expression evaluates to NULL (neither true nor false) then
drh35573352008-01-08 23:54:25 +00002605** jump if jumpIfNull is SQLITE_JUMPIFNULL or fall through if jumpIfNull
2606** is 0.
drhcce7d172000-05-31 15:34:51 +00002607*/
danielk19774adee202004-05-08 08:23:19 +00002608void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00002609 Vdbe *v = pParse->pVdbe;
2610 int op = 0;
drh2dcef112008-01-12 19:03:48 +00002611 int regFree1 = 0;
2612 int regFree2 = 0;
2613 int r1, r2;
2614
drh35573352008-01-08 23:54:25 +00002615 assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
drhdaffd0e2001-04-11 14:28:42 +00002616 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00002617
2618 /* The value of pExpr->op and op are related as follows:
2619 **
2620 ** pExpr->op op
2621 ** --------- ----------
2622 ** TK_ISNULL OP_NotNull
2623 ** TK_NOTNULL OP_IsNull
2624 ** TK_NE OP_Eq
2625 ** TK_EQ OP_Ne
2626 ** TK_GT OP_Le
2627 ** TK_LE OP_Gt
2628 ** TK_GE OP_Lt
2629 ** TK_LT OP_Ge
2630 **
2631 ** For other values of pExpr->op, op is undefined and unused.
2632 ** The value of TK_ and OP_ constants are arranged such that we
2633 ** can compute the mapping above using the following expression.
2634 ** Assert()s verify that the computation is correct.
2635 */
2636 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
2637
2638 /* Verify correct alignment of TK_ and OP_ constants
2639 */
2640 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
2641 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
2642 assert( pExpr->op!=TK_NE || op==OP_Eq );
2643 assert( pExpr->op!=TK_EQ || op==OP_Ne );
2644 assert( pExpr->op!=TK_LT || op==OP_Ge );
2645 assert( pExpr->op!=TK_LE || op==OP_Gt );
2646 assert( pExpr->op!=TK_GT || op==OP_Le );
2647 assert( pExpr->op!=TK_GE || op==OP_Lt );
2648
drhcce7d172000-05-31 15:34:51 +00002649 switch( pExpr->op ){
2650 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00002651 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
2652 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002653 break;
2654 }
2655 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00002656 int d2 = sqlite3VdbeMakeLabel(v);
drh35573352008-01-08 23:54:25 +00002657 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, jumpIfNull^SQLITE_JUMPIFNULL);
danielk19774adee202004-05-08 08:23:19 +00002658 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
2659 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00002660 break;
2661 }
2662 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00002663 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002664 break;
2665 }
2666 case TK_LT:
2667 case TK_LE:
2668 case TK_GT:
2669 case TK_GE:
2670 case TK_NE:
2671 case TK_EQ: {
drh2dcef112008-01-12 19:03:48 +00002672 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
2673 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
drh35573352008-01-08 23:54:25 +00002674 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
drh2dcef112008-01-12 19:03:48 +00002675 r1, r2, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002676 break;
2677 }
drhcce7d172000-05-31 15:34:51 +00002678 case TK_ISNULL:
2679 case TK_NOTNULL: {
drh2dcef112008-01-12 19:03:48 +00002680 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
2681 sqlite3VdbeAddOp2(v, op, r1, dest);
drhcce7d172000-05-31 15:34:51 +00002682 break;
2683 }
drhfef52082000-06-06 01:50:43 +00002684 case TK_BETWEEN: {
drh2dcef112008-01-12 19:03:48 +00002685 /* x BETWEEN y AND z
danielk19770202b292004-06-09 09:55:16 +00002686 **
drh2dcef112008-01-12 19:03:48 +00002687 ** Is equivalent to
2688 **
2689 ** x>=y AND x<=z
2690 **
2691 ** Code it as such, taking care to do the common subexpression
2692 ** elementation of x.
danielk19770202b292004-06-09 09:55:16 +00002693 */
drh2dcef112008-01-12 19:03:48 +00002694 Expr exprAnd;
2695 Expr compLeft;
2696 Expr compRight;
2697 Expr exprX;
drhbe5c89a2004-07-26 00:31:09 +00002698
drh2dcef112008-01-12 19:03:48 +00002699 exprX = *pExpr->pLeft;
2700 exprAnd.op = TK_AND;
2701 exprAnd.pLeft = &compLeft;
2702 exprAnd.pRight = &compRight;
2703 compLeft.op = TK_GE;
2704 compLeft.pLeft = &exprX;
2705 compLeft.pRight = pExpr->pList->a[0].pExpr;
2706 compRight.op = TK_LE;
2707 compRight.pLeft = &exprX;
2708 compRight.pRight = pExpr->pList->a[1].pExpr;
2709 exprX.iTable = sqlite3ExprCodeTemp(pParse, &exprX, &regFree1);
2710 exprX.op = TK_REGISTER;
2711 sqlite3ExprIfFalse(pParse, &exprAnd, dest, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00002712 break;
2713 }
drhcce7d172000-05-31 15:34:51 +00002714 default: {
drh2dcef112008-01-12 19:03:48 +00002715 r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
2716 sqlite3VdbeAddOp3(v, OP_IfNot, r1, dest, jumpIfNull!=0);
drhcce7d172000-05-31 15:34:51 +00002717 break;
2718 }
2719 }
drh2dcef112008-01-12 19:03:48 +00002720 sqlite3ReleaseTempReg(pParse, regFree1);
2721 sqlite3ReleaseTempReg(pParse, regFree2);
drhcce7d172000-05-31 15:34:51 +00002722}
drh22827922000-06-06 17:27:05 +00002723
2724/*
2725** Do a deep comparison of two expression trees. Return TRUE (non-zero)
2726** if they are identical and return FALSE if they differ in any way.
drhd40aab02007-02-24 15:29:03 +00002727**
2728** Sometimes this routine will return FALSE even if the two expressions
2729** really are equivalent. If we cannot prove that the expressions are
2730** identical, we return FALSE just to be safe. So if this routine
2731** returns false, then you do not really know for certain if the two
2732** expressions are the same. But if you get a TRUE return, then you
2733** can be sure the expressions are the same. In the places where
2734** this routine is used, it does not hurt to get an extra FALSE - that
2735** just might result in some slightly slower code. But returning
2736** an incorrect TRUE could lead to a malfunction.
drh22827922000-06-06 17:27:05 +00002737*/
danielk19774adee202004-05-08 08:23:19 +00002738int sqlite3ExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00002739 int i;
danielk19774b202ae2006-01-23 05:50:58 +00002740 if( pA==0||pB==0 ){
2741 return pB==pA;
drh22827922000-06-06 17:27:05 +00002742 }
2743 if( pA->op!=pB->op ) return 0;
drhfd357972005-09-09 01:33:19 +00002744 if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 0;
danielk19774adee202004-05-08 08:23:19 +00002745 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
2746 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00002747 if( pA->pList ){
2748 if( pB->pList==0 ) return 0;
2749 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
2750 for(i=0; i<pA->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00002751 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00002752 return 0;
2753 }
2754 }
2755 }else if( pB->pList ){
2756 return 0;
2757 }
2758 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00002759 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drhdd735212007-02-24 13:53:05 +00002760 if( pA->op!=TK_COLUMN && pA->token.z ){
drh22827922000-06-06 17:27:05 +00002761 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00002762 if( pB->token.n!=pA->token.n ) return 0;
drh2646da72005-12-09 20:02:05 +00002763 if( sqlite3StrNICmp((char*)pA->token.z,(char*)pB->token.z,pB->token.n)!=0 ){
2764 return 0;
2765 }
drh22827922000-06-06 17:27:05 +00002766 }
2767 return 1;
2768}
2769
drh13449892005-09-07 21:22:45 +00002770
drh22827922000-06-06 17:27:05 +00002771/*
drh13449892005-09-07 21:22:45 +00002772** Add a new element to the pAggInfo->aCol[] array. Return the index of
2773** the new element. Return a negative number if malloc fails.
drh22827922000-06-06 17:27:05 +00002774*/
drh17435752007-08-16 04:30:38 +00002775static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){
drh13449892005-09-07 21:22:45 +00002776 int i;
drhcf643722007-03-27 13:36:37 +00002777 pInfo->aCol = sqlite3ArrayAllocate(
drh17435752007-08-16 04:30:38 +00002778 db,
drhcf643722007-03-27 13:36:37 +00002779 pInfo->aCol,
2780 sizeof(pInfo->aCol[0]),
2781 3,
2782 &pInfo->nColumn,
2783 &pInfo->nColumnAlloc,
2784 &i
2785 );
drh13449892005-09-07 21:22:45 +00002786 return i;
2787}
2788
2789/*
2790** Add a new element to the pAggInfo->aFunc[] array. Return the index of
2791** the new element. Return a negative number if malloc fails.
2792*/
drh17435752007-08-16 04:30:38 +00002793static int addAggInfoFunc(sqlite3 *db, AggInfo *pInfo){
drh13449892005-09-07 21:22:45 +00002794 int i;
drhcf643722007-03-27 13:36:37 +00002795 pInfo->aFunc = sqlite3ArrayAllocate(
drh17435752007-08-16 04:30:38 +00002796 db,
drhcf643722007-03-27 13:36:37 +00002797 pInfo->aFunc,
2798 sizeof(pInfo->aFunc[0]),
2799 3,
2800 &pInfo->nFunc,
2801 &pInfo->nFuncAlloc,
2802 &i
2803 );
drh13449892005-09-07 21:22:45 +00002804 return i;
2805}
drh22827922000-06-06 17:27:05 +00002806
2807/*
drh626a8792005-01-17 22:08:19 +00002808** This is an xFunc for walkExprTree() used to implement
2809** sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates
2810** for additional information.
drh22827922000-06-06 17:27:05 +00002811**
drh626a8792005-01-17 22:08:19 +00002812** This routine analyzes the aggregate function at pExpr.
drh22827922000-06-06 17:27:05 +00002813*/
drh626a8792005-01-17 22:08:19 +00002814static int analyzeAggregate(void *pArg, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00002815 int i;
danielk1977a58fdfb2005-02-08 07:50:40 +00002816 NameContext *pNC = (NameContext *)pArg;
2817 Parse *pParse = pNC->pParse;
2818 SrcList *pSrcList = pNC->pSrcList;
drh13449892005-09-07 21:22:45 +00002819 AggInfo *pAggInfo = pNC->pAggInfo;
drh22827922000-06-06 17:27:05 +00002820
drh22827922000-06-06 17:27:05 +00002821 switch( pExpr->op ){
drh89c69d02007-01-04 01:20:28 +00002822 case TK_AGG_COLUMN:
drh967e8b72000-06-21 13:59:10 +00002823 case TK_COLUMN: {
drh13449892005-09-07 21:22:45 +00002824 /* Check to see if the column is in one of the tables in the FROM
2825 ** clause of the aggregate query */
2826 if( pSrcList ){
2827 struct SrcList_item *pItem = pSrcList->a;
2828 for(i=0; i<pSrcList->nSrc; i++, pItem++){
2829 struct AggInfo_col *pCol;
2830 if( pExpr->iTable==pItem->iCursor ){
2831 /* If we reach this point, it means that pExpr refers to a table
2832 ** that is in the FROM clause of the aggregate query.
2833 **
2834 ** Make an entry for the column in pAggInfo->aCol[] if there
2835 ** is not an entry there already.
2836 */
drh7f906d62007-03-12 23:48:52 +00002837 int k;
drh13449892005-09-07 21:22:45 +00002838 pCol = pAggInfo->aCol;
drh7f906d62007-03-12 23:48:52 +00002839 for(k=0; k<pAggInfo->nColumn; k++, pCol++){
drh13449892005-09-07 21:22:45 +00002840 if( pCol->iTable==pExpr->iTable &&
2841 pCol->iColumn==pExpr->iColumn ){
2842 break;
2843 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002844 }
danielk19771e536952007-08-16 10:09:01 +00002845 if( (k>=pAggInfo->nColumn)
2846 && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0
2847 ){
drh7f906d62007-03-12 23:48:52 +00002848 pCol = &pAggInfo->aCol[k];
danielk19770817d0d2007-02-14 09:19:36 +00002849 pCol->pTab = pExpr->pTab;
drh13449892005-09-07 21:22:45 +00002850 pCol->iTable = pExpr->iTable;
2851 pCol->iColumn = pExpr->iColumn;
drh0a07c102008-01-03 18:03:08 +00002852 pCol->iMem = ++pParse->nMem;
drh13449892005-09-07 21:22:45 +00002853 pCol->iSorterColumn = -1;
drh5774b802005-09-07 22:48:16 +00002854 pCol->pExpr = pExpr;
drh13449892005-09-07 21:22:45 +00002855 if( pAggInfo->pGroupBy ){
2856 int j, n;
2857 ExprList *pGB = pAggInfo->pGroupBy;
2858 struct ExprList_item *pTerm = pGB->a;
2859 n = pGB->nExpr;
2860 for(j=0; j<n; j++, pTerm++){
2861 Expr *pE = pTerm->pExpr;
2862 if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable &&
2863 pE->iColumn==pExpr->iColumn ){
2864 pCol->iSorterColumn = j;
2865 break;
2866 }
2867 }
2868 }
2869 if( pCol->iSorterColumn<0 ){
2870 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
2871 }
2872 }
2873 /* There is now an entry for pExpr in pAggInfo->aCol[] (either
2874 ** because it was there before or because we just created it).
2875 ** Convert the pExpr to be a TK_AGG_COLUMN referring to that
2876 ** pAggInfo->aCol[] entry.
2877 */
2878 pExpr->pAggInfo = pAggInfo;
2879 pExpr->op = TK_AGG_COLUMN;
drh7f906d62007-03-12 23:48:52 +00002880 pExpr->iAgg = k;
drh13449892005-09-07 21:22:45 +00002881 break;
2882 } /* endif pExpr->iTable==pItem->iCursor */
2883 } /* end loop over pSrcList */
drh22827922000-06-06 17:27:05 +00002884 }
drh626a8792005-01-17 22:08:19 +00002885 return 1;
drh22827922000-06-06 17:27:05 +00002886 }
2887 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00002888 /* The pNC->nDepth==0 test causes aggregate functions in subqueries
2889 ** to be ignored */
danielk1977a58fdfb2005-02-08 07:50:40 +00002890 if( pNC->nDepth==0 ){
drh13449892005-09-07 21:22:45 +00002891 /* Check to see if pExpr is a duplicate of another aggregate
2892 ** function that is already in the pAggInfo structure
2893 */
2894 struct AggInfo_func *pItem = pAggInfo->aFunc;
2895 for(i=0; i<pAggInfo->nFunc; i++, pItem++){
2896 if( sqlite3ExprCompare(pItem->pExpr, pExpr) ){
danielk1977a58fdfb2005-02-08 07:50:40 +00002897 break;
2898 }
drh22827922000-06-06 17:27:05 +00002899 }
drh13449892005-09-07 21:22:45 +00002900 if( i>=pAggInfo->nFunc ){
2901 /* pExpr is original. Make a new entry in pAggInfo->aFunc[]
2902 */
danielk197714db2662006-01-09 16:12:04 +00002903 u8 enc = ENC(pParse->db);
danielk19771e536952007-08-16 10:09:01 +00002904 i = addAggInfoFunc(pParse->db, pAggInfo);
drh13449892005-09-07 21:22:45 +00002905 if( i>=0 ){
2906 pItem = &pAggInfo->aFunc[i];
2907 pItem->pExpr = pExpr;
drh0a07c102008-01-03 18:03:08 +00002908 pItem->iMem = ++pParse->nMem;
drh13449892005-09-07 21:22:45 +00002909 pItem->pFunc = sqlite3FindFunction(pParse->db,
drh2646da72005-12-09 20:02:05 +00002910 (char*)pExpr->token.z, pExpr->token.n,
drh13449892005-09-07 21:22:45 +00002911 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
drhfd357972005-09-09 01:33:19 +00002912 if( pExpr->flags & EP_Distinct ){
2913 pItem->iDistinct = pParse->nTab++;
2914 }else{
2915 pItem->iDistinct = -1;
2916 }
drh13449892005-09-07 21:22:45 +00002917 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002918 }
drh13449892005-09-07 21:22:45 +00002919 /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
2920 */
danielk1977a58fdfb2005-02-08 07:50:40 +00002921 pExpr->iAgg = i;
drh13449892005-09-07 21:22:45 +00002922 pExpr->pAggInfo = pAggInfo;
danielk1977a58fdfb2005-02-08 07:50:40 +00002923 return 1;
drh22827922000-06-06 17:27:05 +00002924 }
drh22827922000-06-06 17:27:05 +00002925 }
2926 }
drh13449892005-09-07 21:22:45 +00002927
2928 /* Recursively walk subqueries looking for TK_COLUMN nodes that need
2929 ** to be changed to TK_AGG_COLUMN. But increment nDepth so that
2930 ** TK_AGG_FUNCTION nodes in subqueries will be unchanged.
2931 */
danielk1977a58fdfb2005-02-08 07:50:40 +00002932 if( pExpr->pSelect ){
2933 pNC->nDepth++;
2934 walkSelectExpr(pExpr->pSelect, analyzeAggregate, pNC);
2935 pNC->nDepth--;
2936 }
drh626a8792005-01-17 22:08:19 +00002937 return 0;
2938}
2939
2940/*
2941** Analyze the given expression looking for aggregate functions and
2942** for variables that need to be added to the pParse->aAgg[] array.
2943** Make additional entries to the pParse->aAgg[] array as necessary.
2944**
2945** This routine should only be called after the expression has been
2946** analyzed by sqlite3ExprResolveNames().
2947**
2948** If errors are seen, leave an error message in zErrMsg and return
2949** the number of errors.
2950*/
danielk1977a58fdfb2005-02-08 07:50:40 +00002951int sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
2952 int nErr = pNC->pParse->nErr;
2953 walkExprTree(pExpr, analyzeAggregate, pNC);
2954 return pNC->pParse->nErr - nErr;
drh22827922000-06-06 17:27:05 +00002955}
drh5d9a4af2005-08-30 00:54:01 +00002956
2957/*
2958** Call sqlite3ExprAnalyzeAggregates() for every expression in an
2959** expression list. Return the number of errors.
2960**
2961** If an error is found, the analysis is cut short.
2962*/
2963int sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){
2964 struct ExprList_item *pItem;
2965 int i;
2966 int nErr = 0;
2967 if( pList ){
2968 for(pItem=pList->a, i=0; nErr==0 && i<pList->nExpr; i++, pItem++){
2969 nErr += sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
2970 }
2971 }
2972 return nErr;
2973}
drh892d3172008-01-10 03:46:36 +00002974
2975/*
2976** Allocate or deallocate temporary use registers during code generation.
2977*/
2978int sqlite3GetTempReg(Parse *pParse){
2979 if( pParse->nTempReg ){
2980 return pParse->aTempReg[--pParse->nTempReg];
2981 }else{
2982 return ++pParse->nMem;
2983 }
2984}
2985void sqlite3ReleaseTempReg(Parse *pParse, int iReg){
drh2dcef112008-01-12 19:03:48 +00002986 if( iReg && pParse->nTempReg<ArraySize(pParse->aTempReg) ){
2987 assert( iReg>0 );
drh892d3172008-01-10 03:46:36 +00002988 pParse->aTempReg[pParse->nTempReg++] = iReg;
2989 }
2990}
2991
2992/*
2993** Allocate or deallocate a block of nReg consecutive registers
2994*/
2995int sqlite3GetTempRange(Parse *pParse, int nReg){
2996 int i;
2997 if( nReg<=pParse->nRangeReg ){
2998 i = pParse->iRangeReg;
2999 pParse->iRangeReg += nReg;
3000 pParse->nRangeReg -= nReg;
3001 }else{
3002 i = pParse->nMem+1;
3003 pParse->nMem += nReg;
3004 }
3005 return i;
3006}
3007void sqlite3ReleaseTempRange(Parse *pParse, int iReg, int nReg){
3008 if( nReg>pParse->nRangeReg ){
3009 pParse->nRangeReg = nReg;
3010 pParse->iRangeReg = iReg;
3011 }
3012}