blob: 7ee47a4e7a3fe31fded7bb5ffbe0bed460d08f86 [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**
drhb1a6c3c2008-03-20 16:30:17 +000015** $Id: expr.c,v 1.356 2008/03/20 16:30:18 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]);
drhbb4957f2008-03-20 14:03:29 +0000403 if( i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
drhfa6bc002004-09-07 16:19:52 +0000404 sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
drhbb4957f2008-03-20 14:03:29 +0000405 db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]);
drhfa6bc002004-09-07 16:19:52 +0000406 }
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 }
drhbb4957f2008-03-20 14:03:29 +0000443 if( !pParse->nErr && pParse->nVar>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
danielk1977832b2662007-05-09 11:37:22 +0000444 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,
danielk19777a15a4b2007-05-08 17:54:43 +0000699 const char *zObject
700){
drhb1a6c3c2008-03-20 16:30:17 +0000701 int mx = pParse->db->aLimit[SQLITE_LIMIT_COLUMN];
702 if( pEList && pEList->nExpr>mx ){
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
danielk1977fc976062007-05-10 10:46:56 +0000708/* The following three functions, heightOfExpr(), heightOfExprList()
709** and heightOfSelect(), are used to determine the maximum height
710** of any expression tree referenced by the structure passed as the
711** first argument.
712**
713** If this maximum height is greater than the current value pointed
714** to by pnHeight, the second parameter, then set *pnHeight to that
715** value.
716*/
717static void heightOfExpr(Expr *p, int *pnHeight){
718 if( p ){
719 if( p->nHeight>*pnHeight ){
720 *pnHeight = p->nHeight;
721 }
722 }
723}
724static void heightOfExprList(ExprList *p, int *pnHeight){
725 if( p ){
726 int i;
727 for(i=0; i<p->nExpr; i++){
728 heightOfExpr(p->a[i].pExpr, pnHeight);
729 }
730 }
731}
732static void heightOfSelect(Select *p, int *pnHeight){
733 if( p ){
734 heightOfExpr(p->pWhere, pnHeight);
735 heightOfExpr(p->pHaving, pnHeight);
736 heightOfExpr(p->pLimit, pnHeight);
737 heightOfExpr(p->pOffset, pnHeight);
738 heightOfExprList(p->pEList, pnHeight);
739 heightOfExprList(p->pGroupBy, pnHeight);
740 heightOfExprList(p->pOrderBy, pnHeight);
741 heightOfSelect(p->pPrior, pnHeight);
742 }
743}
744
745/*
746** Set the Expr.nHeight variable in the structure passed as an
747** argument. An expression with no children, Expr.pList or
748** Expr.pSelect member has a height of 1. Any other expression
749** has a height equal to the maximum height of any other
750** referenced Expr plus one.
751*/
752void sqlite3ExprSetHeight(Expr *p){
753 int nHeight = 0;
754 heightOfExpr(p->pLeft, &nHeight);
755 heightOfExpr(p->pRight, &nHeight);
756 heightOfExprList(p->pList, &nHeight);
757 heightOfSelect(p->pSelect, &nHeight);
758 p->nHeight = nHeight + 1;
759}
760
761/*
762** Return the maximum height of any expression tree referenced
763** by the select statement passed as an argument.
764*/
765int sqlite3SelectExprHeight(Select *p){
766 int nHeight = 0;
767 heightOfSelect(p, &nHeight);
768 return nHeight;
769}
danielk1977fc976062007-05-10 10:46:56 +0000770
danielk19777a15a4b2007-05-08 17:54:43 +0000771/*
drha76b5df2002-02-23 02:32:10 +0000772** Delete an entire expression list.
773*/
danielk19774adee202004-05-08 08:23:19 +0000774void sqlite3ExprListDelete(ExprList *pList){
drha76b5df2002-02-23 02:32:10 +0000775 int i;
drhbe5c89a2004-07-26 00:31:09 +0000776 struct ExprList_item *pItem;
drha76b5df2002-02-23 02:32:10 +0000777 if( pList==0 ) return;
drh1bdd9b52004-04-23 17:04:44 +0000778 assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
779 assert( pList->nExpr<=pList->nAlloc );
drhbe5c89a2004-07-26 00:31:09 +0000780 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
781 sqlite3ExprDelete(pItem->pExpr);
drh17435752007-08-16 04:30:38 +0000782 sqlite3_free(pItem->zName);
drha76b5df2002-02-23 02:32:10 +0000783 }
drh17435752007-08-16 04:30:38 +0000784 sqlite3_free(pList->a);
785 sqlite3_free(pList);
drha76b5df2002-02-23 02:32:10 +0000786}
787
788/*
drh626a8792005-01-17 22:08:19 +0000789** Walk an expression tree. Call xFunc for each node visited.
drh73b211a2005-01-18 04:00:42 +0000790**
drh626a8792005-01-17 22:08:19 +0000791** The return value from xFunc determines whether the tree walk continues.
792** 0 means continue walking the tree. 1 means do not walk children
793** of the current node but continue with siblings. 2 means abandon
794** the tree walk completely.
795**
796** The return value from this routine is 1 to abandon the tree walk
797** and 0 to continue.
drh87abf5c2005-08-25 12:45:04 +0000798**
799** NOTICE: This routine does *not* descend into subqueries.
drh626a8792005-01-17 22:08:19 +0000800*/
danielk1977a58fdfb2005-02-08 07:50:40 +0000801static int walkExprList(ExprList *, int (*)(void *, Expr*), void *);
drh626a8792005-01-17 22:08:19 +0000802static int walkExprTree(Expr *pExpr, int (*xFunc)(void*,Expr*), void *pArg){
drh626a8792005-01-17 22:08:19 +0000803 int rc;
804 if( pExpr==0 ) return 0;
805 rc = (*xFunc)(pArg, pExpr);
806 if( rc==0 ){
807 if( walkExprTree(pExpr->pLeft, xFunc, pArg) ) return 1;
808 if( walkExprTree(pExpr->pRight, xFunc, pArg) ) return 1;
danielk1977a58fdfb2005-02-08 07:50:40 +0000809 if( walkExprList(pExpr->pList, xFunc, pArg) ) return 1;
drh626a8792005-01-17 22:08:19 +0000810 }
811 return rc>1;
812}
813
814/*
danielk1977a58fdfb2005-02-08 07:50:40 +0000815** Call walkExprTree() for every expression in list p.
816*/
817static int walkExprList(ExprList *p, int (*xFunc)(void *, Expr*), void *pArg){
818 int i;
819 struct ExprList_item *pItem;
820 if( !p ) return 0;
821 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
822 if( walkExprTree(pItem->pExpr, xFunc, pArg) ) return 1;
823 }
824 return 0;
825}
826
827/*
828** Call walkExprTree() for every expression in Select p, not including
829** expressions that are part of sub-selects in any FROM clause or the LIMIT
830** or OFFSET expressions..
831*/
832static int walkSelectExpr(Select *p, int (*xFunc)(void *, Expr*), void *pArg){
833 walkExprList(p->pEList, xFunc, pArg);
834 walkExprTree(p->pWhere, xFunc, pArg);
835 walkExprList(p->pGroupBy, xFunc, pArg);
836 walkExprTree(p->pHaving, xFunc, pArg);
837 walkExprList(p->pOrderBy, xFunc, pArg);
danielk197715d79822007-05-15 07:00:34 +0000838 if( p->pPrior ){
839 walkSelectExpr(p->pPrior, xFunc, pArg);
840 }
danielk1977a58fdfb2005-02-08 07:50:40 +0000841 return 0;
842}
843
844
845/*
drh626a8792005-01-17 22:08:19 +0000846** This routine is designed as an xFunc for walkExprTree().
847**
848** pArg is really a pointer to an integer. If we can tell by looking
drh73b211a2005-01-18 04:00:42 +0000849** at pExpr that the expression that contains pExpr is not a constant
850** expression, then set *pArg to 0 and return 2 to abandon the tree walk.
851** If pExpr does does not disqualify the expression from being a constant
852** then do nothing.
853**
854** After walking the whole tree, if no nodes are found that disqualify
855** the expression as constant, then we assume the whole expression
856** is constant. See sqlite3ExprIsConstant() for additional information.
drh626a8792005-01-17 22:08:19 +0000857*/
858static int exprNodeIsConstant(void *pArg, Expr *pExpr){
drh0a168372007-06-08 00:20:47 +0000859 int *pN = (int*)pArg;
860
861 /* If *pArg is 3 then any term of the expression that comes from
862 ** the ON or USING clauses of a join disqualifies the expression
863 ** from being considered constant. */
864 if( (*pN)==3 && ExprHasAnyProperty(pExpr, EP_FromJoin) ){
865 *pN = 0;
866 return 2;
867 }
868
drh626a8792005-01-17 22:08:19 +0000869 switch( pExpr->op ){
drheb55bd22005-06-30 17:04:21 +0000870 /* Consider functions to be constant if all their arguments are constant
871 ** and *pArg==2 */
872 case TK_FUNCTION:
drh0a168372007-06-08 00:20:47 +0000873 if( (*pN)==2 ) return 0;
drheb55bd22005-06-30 17:04:21 +0000874 /* Fall through */
drh626a8792005-01-17 22:08:19 +0000875 case TK_ID:
876 case TK_COLUMN:
877 case TK_DOT:
878 case TK_AGG_FUNCTION:
drh13449892005-09-07 21:22:45 +0000879 case TK_AGG_COLUMN:
drhfe2093d2005-01-20 22:48:47 +0000880#ifndef SQLITE_OMIT_SUBQUERY
881 case TK_SELECT:
882 case TK_EXISTS:
883#endif
drh0a168372007-06-08 00:20:47 +0000884 *pN = 0;
drh626a8792005-01-17 22:08:19 +0000885 return 2;
drh87abf5c2005-08-25 12:45:04 +0000886 case TK_IN:
887 if( pExpr->pSelect ){
drh0a168372007-06-08 00:20:47 +0000888 *pN = 0;
drh87abf5c2005-08-25 12:45:04 +0000889 return 2;
890 }
drh626a8792005-01-17 22:08:19 +0000891 default:
892 return 0;
893 }
894}
895
896/*
drhfef52082000-06-06 01:50:43 +0000897** Walk an expression tree. Return 1 if the expression is constant
drheb55bd22005-06-30 17:04:21 +0000898** and 0 if it involves variables or function calls.
drh23989372002-05-21 13:43:04 +0000899**
900** For the purposes of this function, a double-quoted string (ex: "abc")
901** is considered a variable but a single-quoted string (ex: 'abc') is
902** a constant.
drhfef52082000-06-06 01:50:43 +0000903*/
danielk19774adee202004-05-08 08:23:19 +0000904int sqlite3ExprIsConstant(Expr *p){
drh626a8792005-01-17 22:08:19 +0000905 int isConst = 1;
906 walkExprTree(p, exprNodeIsConstant, &isConst);
907 return isConst;
drhfef52082000-06-06 01:50:43 +0000908}
909
910/*
drheb55bd22005-06-30 17:04:21 +0000911** Walk an expression tree. Return 1 if the expression is constant
drh0a168372007-06-08 00:20:47 +0000912** that does no originate from the ON or USING clauses of a join.
913** Return 0 if it involves variables or function calls or terms from
914** an ON or USING clause.
915*/
916int sqlite3ExprIsConstantNotJoin(Expr *p){
917 int isConst = 3;
918 walkExprTree(p, exprNodeIsConstant, &isConst);
919 return isConst!=0;
920}
921
922/*
923** Walk an expression tree. Return 1 if the expression is constant
drheb55bd22005-06-30 17:04:21 +0000924** or a function call with constant arguments. Return and 0 if there
925** are any variables.
926**
927** For the purposes of this function, a double-quoted string (ex: "abc")
928** is considered a variable but a single-quoted string (ex: 'abc') is
929** a constant.
930*/
931int sqlite3ExprIsConstantOrFunction(Expr *p){
932 int isConst = 2;
933 walkExprTree(p, exprNodeIsConstant, &isConst);
934 return isConst!=0;
935}
936
937/*
drh73b211a2005-01-18 04:00:42 +0000938** If the expression p codes a constant integer that is small enough
drh202b2df2004-01-06 01:13:46 +0000939** to fit in a 32-bit integer, return 1 and put the value of the integer
940** in *pValue. If the expression is not an integer or if it is too big
941** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
drhe4de1fe2002-06-02 16:09:01 +0000942*/
danielk19774adee202004-05-08 08:23:19 +0000943int sqlite3ExprIsInteger(Expr *p, int *pValue){
drhe4de1fe2002-06-02 16:09:01 +0000944 switch( p->op ){
945 case TK_INTEGER: {
drh2646da72005-12-09 20:02:05 +0000946 if( sqlite3GetInt32((char*)p->token.z, pValue) ){
drh202b2df2004-01-06 01:13:46 +0000947 return 1;
948 }
949 break;
drhe4de1fe2002-06-02 16:09:01 +0000950 }
drh4b59ab52002-08-24 18:24:51 +0000951 case TK_UPLUS: {
danielk19774adee202004-05-08 08:23:19 +0000952 return sqlite3ExprIsInteger(p->pLeft, pValue);
drh4b59ab52002-08-24 18:24:51 +0000953 }
drhe4de1fe2002-06-02 16:09:01 +0000954 case TK_UMINUS: {
955 int v;
danielk19774adee202004-05-08 08:23:19 +0000956 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
drhe4de1fe2002-06-02 16:09:01 +0000957 *pValue = -v;
958 return 1;
959 }
960 break;
961 }
962 default: break;
963 }
964 return 0;
965}
966
967/*
drhc4a3c772001-04-04 11:48:57 +0000968** Return TRUE if the given string is a row-id column name.
969*/
danielk19774adee202004-05-08 08:23:19 +0000970int sqlite3IsRowid(const char *z){
971 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
972 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
973 if( sqlite3StrICmp(z, "OID")==0 ) return 1;
drhc4a3c772001-04-04 11:48:57 +0000974 return 0;
975}
976
977/*
drh8141f612004-01-25 22:44:58 +0000978** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
979** that name in the set of source tables in pSrcList and make the pExpr
980** expression node refer back to that source column. The following changes
981** are made to pExpr:
982**
983** pExpr->iDb Set the index in db->aDb[] of the database holding
984** the table.
985** pExpr->iTable Set to the cursor number for the table obtained
986** from pSrcList.
987** pExpr->iColumn Set to the column number within the table.
drh8141f612004-01-25 22:44:58 +0000988** pExpr->op Set to TK_COLUMN.
989** pExpr->pLeft Any expression this points to is deleted
990** pExpr->pRight Any expression this points to is deleted.
991**
992** The pDbToken is the name of the database (the "X"). This value may be
993** NULL meaning that name is of the form Y.Z or Z. Any available database
994** can be used. The pTableToken is the name of the table (the "Y"). This
995** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it
996** means that the form of the name is Z and that columns from any table
997** can be used.
998**
999** If the name cannot be resolved unambiguously, leave an error message
1000** in pParse and return non-zero. Return zero on success.
1001*/
1002static int lookupName(
drhffe07b22005-11-03 00:41:17 +00001003 Parse *pParse, /* The parsing context */
drh8141f612004-01-25 22:44:58 +00001004 Token *pDbToken, /* Name of the database containing table, or NULL */
1005 Token *pTableToken, /* Name of table containing column, or NULL */
1006 Token *pColumnToken, /* Name of the column. */
drh626a8792005-01-17 22:08:19 +00001007 NameContext *pNC, /* The name context used to resolve the name */
drh8141f612004-01-25 22:44:58 +00001008 Expr *pExpr /* Make this EXPR node point to the selected column */
1009){
1010 char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */
1011 char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */
1012 char *zCol = 0; /* Name of the column. The "Z" */
1013 int i, j; /* Loop counters */
1014 int cnt = 0; /* Number of matching column names */
1015 int cntTab = 0; /* Number of matching table names */
drh626a8792005-01-17 22:08:19 +00001016 sqlite3 *db = pParse->db; /* The database */
drh51669862004-12-18 18:40:26 +00001017 struct SrcList_item *pItem; /* Use for looping over pSrcList items */
1018 struct SrcList_item *pMatch = 0; /* The matching pSrcList item */
drh73b211a2005-01-18 04:00:42 +00001019 NameContext *pTopNC = pNC; /* First namecontext in the list */
drh728b5772007-09-18 15:55:07 +00001020 Schema *pSchema = 0; /* Schema of the expression */
drh8141f612004-01-25 22:44:58 +00001021
1022 assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
drh17435752007-08-16 04:30:38 +00001023 zDb = sqlite3NameFromToken(db, pDbToken);
1024 zTab = sqlite3NameFromToken(db, pTableToken);
1025 zCol = sqlite3NameFromToken(db, pColumnToken);
1026 if( db->mallocFailed ){
danielk1977d5d56522005-03-16 12:15:20 +00001027 goto lookupname_end;
drh8141f612004-01-25 22:44:58 +00001028 }
drh8141f612004-01-25 22:44:58 +00001029
1030 pExpr->iTable = -1;
drh626a8792005-01-17 22:08:19 +00001031 while( pNC && cnt==0 ){
drhffe07b22005-11-03 00:41:17 +00001032 ExprList *pEList;
drh626a8792005-01-17 22:08:19 +00001033 SrcList *pSrcList = pNC->pSrcList;
drh8141f612004-01-25 22:44:58 +00001034
danielk1977b3bce662005-01-29 08:32:43 +00001035 if( pSrcList ){
1036 for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
drh43617e92006-03-06 20:55:46 +00001037 Table *pTab;
1038 int iDb;
danielk1977b3bce662005-01-29 08:32:43 +00001039 Column *pCol;
1040
drh43617e92006-03-06 20:55:46 +00001041 pTab = pItem->pTab;
1042 assert( pTab!=0 );
1043 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
danielk1977b3bce662005-01-29 08:32:43 +00001044 assert( pTab->nCol>0 );
1045 if( zTab ){
1046 if( pItem->zAlias ){
1047 char *zTabName = pItem->zAlias;
1048 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
1049 }else{
1050 char *zTabName = pTab->zName;
1051 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
danielk1977da184232006-01-05 11:34:32 +00001052 if( zDb!=0 && sqlite3StrICmp(db->aDb[iDb].zName, zDb)!=0 ){
danielk1977b3bce662005-01-29 08:32:43 +00001053 continue;
1054 }
drh626a8792005-01-17 22:08:19 +00001055 }
drh8141f612004-01-25 22:44:58 +00001056 }
danielk1977b3bce662005-01-29 08:32:43 +00001057 if( 0==(cntTab++) ){
drh626a8792005-01-17 22:08:19 +00001058 pExpr->iTable = pItem->iCursor;
drh728b5772007-09-18 15:55:07 +00001059 pSchema = pTab->pSchema;
danielk1977b3bce662005-01-29 08:32:43 +00001060 pMatch = pItem;
1061 }
1062 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
1063 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
danielk1977b3bf5562006-01-10 17:58:23 +00001064 const char *zColl = pTab->aCol[j].zColl;
drh873fac02005-06-06 17:11:46 +00001065 IdList *pUsing;
danielk1977b3bce662005-01-29 08:32:43 +00001066 cnt++;
1067 pExpr->iTable = pItem->iCursor;
1068 pMatch = pItem;
drh728b5772007-09-18 15:55:07 +00001069 pSchema = pTab->pSchema;
danielk1977b3bce662005-01-29 08:32:43 +00001070 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
1071 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
1072 pExpr->affinity = pTab->aCol[j].affinity;
drh8b4c40d2007-02-01 23:02:45 +00001073 if( (pExpr->flags & EP_ExpCollate)==0 ){
1074 pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0);
1075 }
drh61dfc312006-12-16 16:25:15 +00001076 if( i<pSrcList->nSrc-1 ){
1077 if( pItem[1].jointype & JT_NATURAL ){
1078 /* If this match occurred in the left table of a natural join,
1079 ** then skip the right table to avoid a duplicate match */
1080 pItem++;
1081 i++;
1082 }else if( (pUsing = pItem[1].pUsing)!=0 ){
1083 /* If this match occurs on a column that is in the USING clause
1084 ** of a join, skip the search of the right table of the join
1085 ** to avoid a duplicate match there. */
1086 int k;
1087 for(k=0; k<pUsing->nId; k++){
1088 if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ){
1089 pItem++;
1090 i++;
1091 break;
1092 }
drh873fac02005-06-06 17:11:46 +00001093 }
1094 }
1095 }
danielk1977b3bce662005-01-29 08:32:43 +00001096 break;
1097 }
drh8141f612004-01-25 22:44:58 +00001098 }
1099 }
1100 }
drh626a8792005-01-17 22:08:19 +00001101
1102#ifndef SQLITE_OMIT_TRIGGER
1103 /* If we have not already resolved the name, then maybe
1104 ** it is a new.* or old.* trigger argument reference
1105 */
1106 if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
1107 TriggerStack *pTriggerStack = pParse->trigStack;
1108 Table *pTab = 0;
danielk19778f2c54e2008-01-01 19:02:09 +00001109 u32 *piColMask;
drh626a8792005-01-17 22:08:19 +00001110 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){
1111 pExpr->iTable = pTriggerStack->newIdx;
1112 assert( pTriggerStack->pTab );
1113 pTab = pTriggerStack->pTab;
danielk19778f2c54e2008-01-01 19:02:09 +00001114 piColMask = &(pTriggerStack->newColMask);
drh626a8792005-01-17 22:08:19 +00001115 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab)==0 ){
1116 pExpr->iTable = pTriggerStack->oldIdx;
1117 assert( pTriggerStack->pTab );
1118 pTab = pTriggerStack->pTab;
danielk19778f2c54e2008-01-01 19:02:09 +00001119 piColMask = &(pTriggerStack->oldColMask);
drh626a8792005-01-17 22:08:19 +00001120 }
1121
1122 if( pTab ){
danielk1977f0113002006-01-24 12:09:17 +00001123 int iCol;
drh626a8792005-01-17 22:08:19 +00001124 Column *pCol = pTab->aCol;
1125
drh728b5772007-09-18 15:55:07 +00001126 pSchema = pTab->pSchema;
drh626a8792005-01-17 22:08:19 +00001127 cntTab++;
danielk1977f0113002006-01-24 12:09:17 +00001128 for(iCol=0; iCol < pTab->nCol; iCol++, pCol++) {
drh626a8792005-01-17 22:08:19 +00001129 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
danielk1977f0113002006-01-24 12:09:17 +00001130 const char *zColl = pTab->aCol[iCol].zColl;
drh626a8792005-01-17 22:08:19 +00001131 cnt++;
danielk1977f0113002006-01-24 12:09:17 +00001132 pExpr->iColumn = iCol==pTab->iPKey ? -1 : iCol;
1133 pExpr->affinity = pTab->aCol[iCol].affinity;
drh8b4c40d2007-02-01 23:02:45 +00001134 if( (pExpr->flags & EP_ExpCollate)==0 ){
1135 pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0);
1136 }
danielk1977aee18ef2005-03-09 12:26:50 +00001137 pExpr->pTab = pTab;
danielk19778f2c54e2008-01-01 19:02:09 +00001138 if( iCol>=0 ){
1139 *piColMask |= ((u32)1<<iCol) | (iCol>=32?0xffffffff:0);
1140 }
drh626a8792005-01-17 22:08:19 +00001141 break;
1142 }
1143 }
1144 }
1145 }
drhb7f91642004-10-31 02:22:47 +00001146#endif /* !defined(SQLITE_OMIT_TRIGGER) */
drh8141f612004-01-25 22:44:58 +00001147
drh626a8792005-01-17 22:08:19 +00001148 /*
1149 ** Perhaps the name is a reference to the ROWID
1150 */
1151 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
1152 cnt = 1;
1153 pExpr->iColumn = -1;
drh8a512562005-11-14 22:29:05 +00001154 pExpr->affinity = SQLITE_AFF_INTEGER;
drh626a8792005-01-17 22:08:19 +00001155 }
drh8141f612004-01-25 22:44:58 +00001156
drh626a8792005-01-17 22:08:19 +00001157 /*
1158 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
1159 ** might refer to an result-set alias. This happens, for example, when
1160 ** we are resolving names in the WHERE clause of the following command:
1161 **
1162 ** SELECT a+b AS x FROM table WHERE x<10;
1163 **
1164 ** In cases like this, replace pExpr with a copy of the expression that
1165 ** forms the result set entry ("a+b" in the example) and return immediately.
1166 ** Note that the expression in the result set should have already been
1167 ** resolved by the time the WHERE clause is resolved.
1168 */
drhffe07b22005-11-03 00:41:17 +00001169 if( cnt==0 && (pEList = pNC->pEList)!=0 && zTab==0 ){
drh626a8792005-01-17 22:08:19 +00001170 for(j=0; j<pEList->nExpr; j++){
1171 char *zAs = pEList->a[j].zName;
1172 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
drh36379e92007-07-23 22:51:15 +00001173 Expr *pDup, *pOrig;
drh626a8792005-01-17 22:08:19 +00001174 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
drh4f07e5f2007-05-14 11:34:46 +00001175 assert( pExpr->pList==0 );
1176 assert( pExpr->pSelect==0 );
drh36379e92007-07-23 22:51:15 +00001177 pOrig = pEList->a[j].pExpr;
1178 if( !pNC->allowAgg && ExprHasProperty(pOrig, EP_Agg) ){
1179 sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs);
drh17435752007-08-16 04:30:38 +00001180 sqlite3_free(zCol);
drh36379e92007-07-23 22:51:15 +00001181 return 2;
1182 }
danielk19771e536952007-08-16 10:09:01 +00001183 pDup = sqlite3ExprDup(db, pOrig);
drh4f07e5f2007-05-14 11:34:46 +00001184 if( pExpr->flags & EP_ExpCollate ){
1185 pDup->pColl = pExpr->pColl;
1186 pDup->flags |= EP_ExpCollate;
1187 }
drh17435752007-08-16 04:30:38 +00001188 if( pExpr->span.dyn ) sqlite3_free((char*)pExpr->span.z);
1189 if( pExpr->token.dyn ) sqlite3_free((char*)pExpr->token.z);
drh4f07e5f2007-05-14 11:34:46 +00001190 memcpy(pExpr, pDup, sizeof(*pExpr));
drh17435752007-08-16 04:30:38 +00001191 sqlite3_free(pDup);
drh15ccce12005-05-23 15:06:39 +00001192 cnt = 1;
danielk1977c9cf6e32007-06-25 16:29:33 +00001193 pMatch = 0;
drh626a8792005-01-17 22:08:19 +00001194 assert( zTab==0 && zDb==0 );
drh15ccce12005-05-23 15:06:39 +00001195 goto lookupname_end_2;
drh626a8792005-01-17 22:08:19 +00001196 }
1197 }
1198 }
1199
1200 /* Advance to the next name context. The loop will exit when either
1201 ** we have a match (cnt>0) or when we run out of name contexts.
1202 */
1203 if( cnt==0 ){
1204 pNC = pNC->pNext;
1205 }
drh8141f612004-01-25 22:44:58 +00001206 }
1207
1208 /*
1209 ** If X and Y are NULL (in other words if only the column name Z is
1210 ** supplied) and the value of Z is enclosed in double-quotes, then
1211 ** Z is a string literal if it doesn't match any column names. In that
1212 ** case, we need to return right away and not make any changes to
1213 ** pExpr.
drh15ccce12005-05-23 15:06:39 +00001214 **
1215 ** Because no reference was made to outer contexts, the pNC->nRef
1216 ** fields are not changed in any context.
drh8141f612004-01-25 22:44:58 +00001217 */
1218 if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
drh17435752007-08-16 04:30:38 +00001219 sqlite3_free(zCol);
drh8141f612004-01-25 22:44:58 +00001220 return 0;
1221 }
1222
1223 /*
1224 ** cnt==0 means there was not match. cnt>1 means there were two or
1225 ** more matches. Either way, we have an error.
1226 */
1227 if( cnt!=1 ){
drhde4fcfd2008-01-19 23:50:26 +00001228 const char *zErr;
1229 zErr = cnt==0 ? "no such column" : "ambiguous column name";
drh8141f612004-01-25 22:44:58 +00001230 if( zDb ){
drhde4fcfd2008-01-19 23:50:26 +00001231 sqlite3ErrorMsg(pParse, "%s: %s.%s.%s", zErr, zDb, zTab, zCol);
drh8141f612004-01-25 22:44:58 +00001232 }else if( zTab ){
drhde4fcfd2008-01-19 23:50:26 +00001233 sqlite3ErrorMsg(pParse, "%s: %s.%s", zErr, zTab, zCol);
drh8141f612004-01-25 22:44:58 +00001234 }else{
drhde4fcfd2008-01-19 23:50:26 +00001235 sqlite3ErrorMsg(pParse, "%s: %s", zErr, zCol);
drh8141f612004-01-25 22:44:58 +00001236 }
drhde4fcfd2008-01-19 23:50:26 +00001237 pTopNC->nErr++;
drh8141f612004-01-25 22:44:58 +00001238 }
1239
drh51669862004-12-18 18:40:26 +00001240 /* If a column from a table in pSrcList is referenced, then record
1241 ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes
1242 ** bit 0 to be set. Column 1 sets bit 1. And so forth. If the
1243 ** column number is greater than the number of bits in the bitmask
1244 ** then set the high-order bit of the bitmask.
1245 */
1246 if( pExpr->iColumn>=0 && pMatch!=0 ){
1247 int n = pExpr->iColumn;
1248 if( n>=sizeof(Bitmask)*8 ){
1249 n = sizeof(Bitmask)*8-1;
1250 }
1251 assert( pMatch->iCursor==pExpr->iTable );
drhca83ac52007-02-01 01:40:44 +00001252 pMatch->colUsed |= ((Bitmask)1)<<n;
drh51669862004-12-18 18:40:26 +00001253 }
1254
danielk1977d5d56522005-03-16 12:15:20 +00001255lookupname_end:
drh8141f612004-01-25 22:44:58 +00001256 /* Clean up and return
1257 */
drh17435752007-08-16 04:30:38 +00001258 sqlite3_free(zDb);
1259 sqlite3_free(zTab);
danielk19774adee202004-05-08 08:23:19 +00001260 sqlite3ExprDelete(pExpr->pLeft);
drh8141f612004-01-25 22:44:58 +00001261 pExpr->pLeft = 0;
danielk19774adee202004-05-08 08:23:19 +00001262 sqlite3ExprDelete(pExpr->pRight);
drh8141f612004-01-25 22:44:58 +00001263 pExpr->pRight = 0;
1264 pExpr->op = TK_COLUMN;
drh15ccce12005-05-23 15:06:39 +00001265lookupname_end_2:
drh17435752007-08-16 04:30:38 +00001266 sqlite3_free(zCol);
drh626a8792005-01-17 22:08:19 +00001267 if( cnt==1 ){
danielk1977b3bce662005-01-29 08:32:43 +00001268 assert( pNC!=0 );
drh728b5772007-09-18 15:55:07 +00001269 sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList);
danielk1977aee18ef2005-03-09 12:26:50 +00001270 if( pMatch && !pMatch->pSelect ){
1271 pExpr->pTab = pMatch->pTab;
1272 }
drh15ccce12005-05-23 15:06:39 +00001273 /* Increment the nRef value on all name contexts from TopNC up to
1274 ** the point where the name matched. */
1275 for(;;){
1276 assert( pTopNC!=0 );
1277 pTopNC->nRef++;
1278 if( pTopNC==pNC ) break;
1279 pTopNC = pTopNC->pNext;
1280 }
1281 return 0;
1282 } else {
1283 return 1;
drh626a8792005-01-17 22:08:19 +00001284 }
drh8141f612004-01-25 22:44:58 +00001285}
1286
1287/*
drh626a8792005-01-17 22:08:19 +00001288** This routine is designed as an xFunc for walkExprTree().
1289**
drh73b211a2005-01-18 04:00:42 +00001290** Resolve symbolic names into TK_COLUMN operators for the current
drh626a8792005-01-17 22:08:19 +00001291** node in the expression tree. Return 0 to continue the search down
drh73b211a2005-01-18 04:00:42 +00001292** the tree or 2 to abort the tree walk.
1293**
1294** This routine also does error checking and name resolution for
1295** function names. The operator for aggregate functions is changed
1296** to TK_AGG_FUNCTION.
drh626a8792005-01-17 22:08:19 +00001297*/
1298static int nameResolverStep(void *pArg, Expr *pExpr){
1299 NameContext *pNC = (NameContext*)pArg;
drh626a8792005-01-17 22:08:19 +00001300 Parse *pParse;
drh626a8792005-01-17 22:08:19 +00001301
danielk1977b3bce662005-01-29 08:32:43 +00001302 if( pExpr==0 ) return 1;
drh626a8792005-01-17 22:08:19 +00001303 assert( pNC!=0 );
drh626a8792005-01-17 22:08:19 +00001304 pParse = pNC->pParse;
danielk1977b3bce662005-01-29 08:32:43 +00001305
drh626a8792005-01-17 22:08:19 +00001306 if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return 1;
1307 ExprSetProperty(pExpr, EP_Resolved);
1308#ifndef NDEBUG
danielk1977f0113002006-01-24 12:09:17 +00001309 if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
1310 SrcList *pSrcList = pNC->pSrcList;
danielk1977940fac92005-01-23 22:41:37 +00001311 int i;
danielk1977f0113002006-01-24 12:09:17 +00001312 for(i=0; i<pNC->pSrcList->nSrc; i++){
drh626a8792005-01-17 22:08:19 +00001313 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
1314 }
1315 }
1316#endif
1317 switch( pExpr->op ){
1318 /* Double-quoted strings (ex: "abc") are used as identifiers if
1319 ** possible. Otherwise they remain as strings. Single-quoted
1320 ** strings (ex: 'abc') are always string literals.
1321 */
1322 case TK_STRING: {
1323 if( pExpr->token.z[0]=='\'' ) break;
1324 /* Fall thru into the TK_ID case if this is a double-quoted string */
1325 }
1326 /* A lone identifier is the name of a column.
1327 */
1328 case TK_ID: {
drh626a8792005-01-17 22:08:19 +00001329 lookupName(pParse, 0, 0, &pExpr->token, pNC, pExpr);
1330 return 1;
1331 }
1332
1333 /* A table name and column name: ID.ID
1334 ** Or a database, table and column: ID.ID.ID
1335 */
1336 case TK_DOT: {
1337 Token *pColumn;
1338 Token *pTable;
1339 Token *pDb;
1340 Expr *pRight;
1341
danielk1977b3bce662005-01-29 08:32:43 +00001342 /* if( pSrcList==0 ) break; */
drh626a8792005-01-17 22:08:19 +00001343 pRight = pExpr->pRight;
1344 if( pRight->op==TK_ID ){
1345 pDb = 0;
1346 pTable = &pExpr->pLeft->token;
1347 pColumn = &pRight->token;
1348 }else{
1349 assert( pRight->op==TK_DOT );
1350 pDb = &pExpr->pLeft->token;
1351 pTable = &pRight->pLeft->token;
1352 pColumn = &pRight->pRight->token;
1353 }
1354 lookupName(pParse, pDb, pTable, pColumn, pNC, pExpr);
1355 return 1;
1356 }
1357
1358 /* Resolve function names
1359 */
drhb71090f2005-05-23 17:26:51 +00001360 case TK_CONST_FUNC:
drh626a8792005-01-17 22:08:19 +00001361 case TK_FUNCTION: {
1362 ExprList *pList = pExpr->pList; /* The argument list */
1363 int n = pList ? pList->nExpr : 0; /* Number of arguments */
1364 int no_such_func = 0; /* True if no such function exists */
1365 int wrong_num_args = 0; /* True if wrong number of arguments */
1366 int is_agg = 0; /* True if is an aggregate function */
1367 int i;
drh5169bbc2006-08-24 14:59:45 +00001368 int auth; /* Authorization to use the function */
drh626a8792005-01-17 22:08:19 +00001369 int nId; /* Number of characters in function name */
1370 const char *zId; /* The function name. */
drh73b211a2005-01-18 04:00:42 +00001371 FuncDef *pDef; /* Information about the function */
danielk197714db2662006-01-09 16:12:04 +00001372 int enc = ENC(pParse->db); /* The database encoding */
drh626a8792005-01-17 22:08:19 +00001373
drh2646da72005-12-09 20:02:05 +00001374 zId = (char*)pExpr->token.z;
drhb71090f2005-05-23 17:26:51 +00001375 nId = pExpr->token.n;
drh626a8792005-01-17 22:08:19 +00001376 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
1377 if( pDef==0 ){
1378 pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
1379 if( pDef==0 ){
1380 no_such_func = 1;
1381 }else{
1382 wrong_num_args = 1;
1383 }
1384 }else{
1385 is_agg = pDef->xFunc==0;
1386 }
drh2fca7fe2006-11-23 11:59:13 +00001387#ifndef SQLITE_OMIT_AUTHORIZATION
drh5169bbc2006-08-24 14:59:45 +00001388 if( pDef ){
1389 auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0);
1390 if( auth!=SQLITE_OK ){
1391 if( auth==SQLITE_DENY ){
1392 sqlite3ErrorMsg(pParse, "not authorized to use function: %s",
1393 pDef->zName);
1394 pNC->nErr++;
1395 }
1396 pExpr->op = TK_NULL;
1397 return 1;
1398 }
1399 }
drhb8b14212006-08-24 15:18:25 +00001400#endif
drh626a8792005-01-17 22:08:19 +00001401 if( is_agg && !pNC->allowAgg ){
1402 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
1403 pNC->nErr++;
1404 is_agg = 0;
1405 }else if( no_such_func ){
1406 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
1407 pNC->nErr++;
1408 }else if( wrong_num_args ){
1409 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
1410 nId, zId);
1411 pNC->nErr++;
1412 }
1413 if( is_agg ){
1414 pExpr->op = TK_AGG_FUNCTION;
1415 pNC->hasAgg = 1;
1416 }
drh73b211a2005-01-18 04:00:42 +00001417 if( is_agg ) pNC->allowAgg = 0;
drh626a8792005-01-17 22:08:19 +00001418 for(i=0; pNC->nErr==0 && i<n; i++){
drh73b211a2005-01-18 04:00:42 +00001419 walkExprTree(pList->a[i].pExpr, nameResolverStep, pNC);
drh626a8792005-01-17 22:08:19 +00001420 }
drh73b211a2005-01-18 04:00:42 +00001421 if( is_agg ) pNC->allowAgg = 1;
drh626a8792005-01-17 22:08:19 +00001422 /* FIX ME: Compute pExpr->affinity based on the expected return
1423 ** type of the function
1424 */
1425 return is_agg;
1426 }
danielk1977b3bce662005-01-29 08:32:43 +00001427#ifndef SQLITE_OMIT_SUBQUERY
1428 case TK_SELECT:
1429 case TK_EXISTS:
1430#endif
1431 case TK_IN: {
1432 if( pExpr->pSelect ){
drh8a9f38f2005-11-05 15:07:55 +00001433 int nRef = pNC->nRef;
drh06f65412005-11-03 02:03:13 +00001434#ifndef SQLITE_OMIT_CHECK
1435 if( pNC->isCheck ){
1436 sqlite3ErrorMsg(pParse,"subqueries prohibited in CHECK constraints");
1437 }
1438#endif
danielk1977b3bce662005-01-29 08:32:43 +00001439 sqlite3SelectResolve(pParse, pExpr->pSelect, pNC);
1440 assert( pNC->nRef>=nRef );
1441 if( nRef!=pNC->nRef ){
1442 ExprSetProperty(pExpr, EP_VarSelect);
1443 }
1444 }
drh4284fb02005-11-03 12:33:28 +00001445 break;
danielk1977b3bce662005-01-29 08:32:43 +00001446 }
drh4284fb02005-11-03 12:33:28 +00001447#ifndef SQLITE_OMIT_CHECK
1448 case TK_VARIABLE: {
1449 if( pNC->isCheck ){
1450 sqlite3ErrorMsg(pParse,"parameters prohibited in CHECK constraints");
1451 }
1452 break;
1453 }
1454#endif
drh626a8792005-01-17 22:08:19 +00001455 }
1456 return 0;
1457}
1458
1459/*
drhcce7d172000-05-31 15:34:51 +00001460** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +00001461** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +00001462** index to the table in the table list and a column offset. The
1463** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
1464** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +00001465** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +00001466** VDBE cursor number for a cursor that is pointing into the referenced
1467** table. The Expr.iColumn value is changed to the index of the column
1468** of the referenced table. The Expr.iColumn value for the special
1469** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
1470** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +00001471**
drh626a8792005-01-17 22:08:19 +00001472** Also resolve function names and check the functions for proper
1473** usage. Make sure all function names are recognized and all functions
1474** have the correct number of arguments. Leave an error message
1475** in pParse->zErrMsg if anything is amiss. Return the number of errors.
1476**
drh73b211a2005-01-18 04:00:42 +00001477** If the expression contains aggregate functions then set the EP_Agg
1478** property on the expression.
drh626a8792005-01-17 22:08:19 +00001479*/
danielk1977fc976062007-05-10 10:46:56 +00001480int sqlite3ExprResolveNames(
danielk1977b3bce662005-01-29 08:32:43 +00001481 NameContext *pNC, /* Namespace to resolve expressions in. */
1482 Expr *pExpr /* The expression to be analyzed. */
drh626a8792005-01-17 22:08:19 +00001483){
drh13449892005-09-07 21:22:45 +00001484 int savedHasAgg;
drhbb4957f2008-03-20 14:03:29 +00001485
drh73b211a2005-01-18 04:00:42 +00001486 if( pExpr==0 ) return 0;
drhbb4957f2008-03-20 14:03:29 +00001487#if SQLITE_MAX_EXPR_DEPTH>0
1488 {
1489 int mxDepth = pNC->pParse->db->aLimit[SQLITE_LIMIT_EXPR_DEPTH];
1490 if( (pExpr->nHeight+pNC->pParse->nHeight)>mxDepth ){
1491 sqlite3ErrorMsg(pNC->pParse,
1492 "Expression tree is too large (maximum depth %d)", mxDepth
1493 );
1494 return 1;
1495 }
1496 pNC->pParse->nHeight += pExpr->nHeight;
danielk1977fc976062007-05-10 10:46:56 +00001497 }
danielk1977fc976062007-05-10 10:46:56 +00001498#endif
drh13449892005-09-07 21:22:45 +00001499 savedHasAgg = pNC->hasAgg;
1500 pNC->hasAgg = 0;
danielk1977b3bce662005-01-29 08:32:43 +00001501 walkExprTree(pExpr, nameResolverStep, pNC);
drhbb4957f2008-03-20 14:03:29 +00001502#if SQLITE_MAX_EXPR_DEPTH>0
danielk1977fc976062007-05-10 10:46:56 +00001503 pNC->pParse->nHeight -= pExpr->nHeight;
1504#endif
danielk1977b3bce662005-01-29 08:32:43 +00001505 if( pNC->nErr>0 ){
drh73b211a2005-01-18 04:00:42 +00001506 ExprSetProperty(pExpr, EP_Error);
drh73b211a2005-01-18 04:00:42 +00001507 }
drh13449892005-09-07 21:22:45 +00001508 if( pNC->hasAgg ){
1509 ExprSetProperty(pExpr, EP_Agg);
1510 }else if( savedHasAgg ){
1511 pNC->hasAgg = 1;
1512 }
drh73b211a2005-01-18 04:00:42 +00001513 return ExprHasProperty(pExpr, EP_Error);
drh626a8792005-01-17 22:08:19 +00001514}
1515
drh1398ad32005-01-19 23:24:50 +00001516/*
1517** A pointer instance of this structure is used to pass information
1518** through walkExprTree into codeSubqueryStep().
1519*/
1520typedef struct QueryCoder QueryCoder;
1521struct QueryCoder {
1522 Parse *pParse; /* The parsing context */
1523 NameContext *pNC; /* Namespace of first enclosing query */
1524};
1525
danielk19779a96b662007-11-29 17:05:18 +00001526#ifdef SQLITE_TEST
1527 int sqlite3_enable_in_opt = 1;
1528#else
1529 #define sqlite3_enable_in_opt 1
1530#endif
1531
1532/*
1533** This function is used by the implementation of the IN (...) operator.
1534** It's job is to find or create a b-tree structure that may be used
1535** either to test for membership of the (...) set or to iterate through
drh85b623f2007-12-13 21:54:09 +00001536** its members, skipping duplicates.
danielk19779a96b662007-11-29 17:05:18 +00001537**
1538** The cursor opened on the structure (database table, database index
1539** or ephermal table) is stored in pX->iTable before this function returns.
1540** The returned value indicates the structure type, as follows:
1541**
1542** IN_INDEX_ROWID - The cursor was opened on a database table.
drh2d401ab2008-01-10 23:50:11 +00001543** IN_INDEX_INDEX - The cursor was opened on a database index.
danielk19779a96b662007-11-29 17:05:18 +00001544** IN_INDEX_EPH - The cursor was opened on a specially created and
1545** populated epheremal table.
1546**
1547** An existing structure may only be used if the SELECT is of the simple
1548** form:
1549**
1550** SELECT <column> FROM <table>
1551**
1552** If the mustBeUnique parameter is false, the structure will be used
1553** for fast set membership tests. In this case an epheremal table must
1554** be used unless <column> is an INTEGER PRIMARY KEY or an index can
drh85b623f2007-12-13 21:54:09 +00001555** be found with <column> as its left-most column.
danielk19779a96b662007-11-29 17:05:18 +00001556**
1557** If mustBeUnique is true, then the structure will be used to iterate
1558** through the set members, skipping any duplicates. In this case an
1559** epheremal table must be used unless the selected <column> is guaranteed
1560** to be unique - either because it is an INTEGER PRIMARY KEY or it
1561** is unique by virtue of a constraint or implicit index.
1562*/
danielk1977284f4ac2007-12-10 05:03:46 +00001563#ifndef SQLITE_OMIT_SUBQUERY
danielk19779a96b662007-11-29 17:05:18 +00001564int sqlite3FindInIndex(Parse *pParse, Expr *pX, int mustBeUnique){
1565 Select *p;
1566 int eType = 0;
1567 int iTab = pParse->nTab++;
1568
1569 /* The follwing if(...) expression is true if the SELECT is of the
1570 ** simple form:
1571 **
1572 ** SELECT <column> FROM <table>
1573 **
1574 ** If this is the case, it may be possible to use an existing table
1575 ** or index instead of generating an epheremal table.
1576 */
1577 if( sqlite3_enable_in_opt
drhc81945e2008-03-10 14:12:53 +00001578 && (p=pX->pSelect)!=0 && !p->pPrior
danielk19779a96b662007-11-29 17:05:18 +00001579 && !p->isDistinct && !p->isAgg && !p->pGroupBy
1580 && p->pSrc && p->pSrc->nSrc==1 && !p->pSrc->a[0].pSelect
danielk1977b2b95d42008-03-12 10:39:00 +00001581 && p->pSrc->a[0].pTab && !p->pSrc->a[0].pTab->pSelect
danielk19779a96b662007-11-29 17:05:18 +00001582 && p->pEList->nExpr==1 && p->pEList->a[0].pExpr->op==TK_COLUMN
1583 && !p->pLimit && !p->pOffset && !p->pWhere
1584 ){
1585 sqlite3 *db = pParse->db;
1586 Index *pIdx;
1587 Expr *pExpr = p->pEList->a[0].pExpr;
1588 int iCol = pExpr->iColumn;
1589 Vdbe *v = sqlite3GetVdbe(pParse);
1590
1591 /* This function is only called from two places. In both cases the vdbe
1592 ** has already been allocated. So assume sqlite3GetVdbe() is always
1593 ** successful here.
1594 */
1595 assert(v);
1596 if( iCol<0 ){
drh0a07c102008-01-03 18:03:08 +00001597 int iMem = ++pParse->nMem;
danielk19779a96b662007-11-29 17:05:18 +00001598 int iAddr;
1599 Table *pTab = p->pSrc->a[0].pTab;
1600 int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
1601 sqlite3VdbeUsesBtree(v, iDb);
1602
drh892d3172008-01-10 03:46:36 +00001603 iAddr = sqlite3VdbeAddOp1(v, OP_If, iMem);
drh4c583122008-01-04 22:01:03 +00001604 sqlite3VdbeAddOp2(v, OP_Integer, 1, iMem);
danielk19779a96b662007-11-29 17:05:18 +00001605
1606 sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
1607 eType = IN_INDEX_ROWID;
1608
1609 sqlite3VdbeJumpHere(v, iAddr);
1610 }else{
1611 /* The collation sequence used by the comparison. If an index is to
1612 ** be used in place of a temp-table, it must be ordered according
1613 ** to this collation sequence.
1614 */
1615 CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pExpr);
1616
1617 /* Check that the affinity that will be used to perform the
1618 ** comparison is the same as the affinity of the column. If
1619 ** it is not, it is not possible to use any index.
1620 */
1621 Table *pTab = p->pSrc->a[0].pTab;
1622 char aff = comparisonAffinity(pX);
1623 int affinity_ok = (pTab->aCol[iCol].affinity==aff||aff==SQLITE_AFF_NONE);
1624
1625 for(pIdx=pTab->pIndex; pIdx && eType==0 && affinity_ok; pIdx=pIdx->pNext){
1626 if( (pIdx->aiColumn[0]==iCol)
1627 && (pReq==sqlite3FindCollSeq(db, ENC(db), pIdx->azColl[0], -1, 0))
1628 && (!mustBeUnique || (pIdx->nColumn==1 && pIdx->onError!=OE_None))
1629 ){
1630 int iDb;
drh0a07c102008-01-03 18:03:08 +00001631 int iMem = ++pParse->nMem;
danielk19779a96b662007-11-29 17:05:18 +00001632 int iAddr;
1633 char *pKey;
1634
1635 pKey = (char *)sqlite3IndexKeyinfo(pParse, pIdx);
1636 iDb = sqlite3SchemaToIndex(db, pIdx->pSchema);
1637 sqlite3VdbeUsesBtree(v, iDb);
1638
drh892d3172008-01-10 03:46:36 +00001639 iAddr = sqlite3VdbeAddOp1(v, OP_If, iMem);
drh4c583122008-01-04 22:01:03 +00001640 sqlite3VdbeAddOp2(v, OP_Integer, 1, iMem);
danielk19779a96b662007-11-29 17:05:18 +00001641
danielk1977207872a2008-01-03 07:54:23 +00001642 sqlite3VdbeAddOp4(v, OP_OpenRead, iTab, pIdx->tnum, iDb,
drh66a51672008-01-03 00:01:23 +00001643 pKey,P4_KEYINFO_HANDOFF);
danielk1977207872a2008-01-03 07:54:23 +00001644 VdbeComment((v, "%s", pIdx->zName));
danielk19779a96b662007-11-29 17:05:18 +00001645 eType = IN_INDEX_INDEX;
drh66a51672008-01-03 00:01:23 +00001646 sqlite3VdbeAddOp2(v, OP_SetNumColumns, iTab, pIdx->nColumn);
danielk19779a96b662007-11-29 17:05:18 +00001647
1648 sqlite3VdbeJumpHere(v, iAddr);
1649 }
1650 }
1651 }
1652 }
1653
1654 if( eType==0 ){
1655 sqlite3CodeSubselect(pParse, pX);
1656 eType = IN_INDEX_EPH;
1657 }else{
1658 pX->iTable = iTab;
1659 }
1660 return eType;
1661}
danielk1977284f4ac2007-12-10 05:03:46 +00001662#endif
drh626a8792005-01-17 22:08:19 +00001663
1664/*
drh9cbe6352005-11-29 03:13:21 +00001665** Generate code for scalar subqueries used as an expression
1666** and IN operators. Examples:
drh626a8792005-01-17 22:08:19 +00001667**
drh9cbe6352005-11-29 03:13:21 +00001668** (SELECT a FROM b) -- subquery
1669** EXISTS (SELECT a FROM b) -- EXISTS subquery
1670** x IN (4,5,11) -- IN operator with list on right-hand side
1671** x IN (SELECT a FROM b) -- IN operator with subquery on the right
drhfef52082000-06-06 01:50:43 +00001672**
drh9cbe6352005-11-29 03:13:21 +00001673** The pExpr parameter describes the expression that contains the IN
1674** operator or subquery.
drhcce7d172000-05-31 15:34:51 +00001675*/
drh51522cd2005-01-20 13:36:19 +00001676#ifndef SQLITE_OMIT_SUBQUERY
danielk1977b3bce662005-01-29 08:32:43 +00001677void sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){
drh57dbd7b2005-07-08 18:25:26 +00001678 int testAddr = 0; /* One-time test address */
danielk1977b3bce662005-01-29 08:32:43 +00001679 Vdbe *v = sqlite3GetVdbe(pParse);
1680 if( v==0 ) return;
1681
danielk1977fc976062007-05-10 10:46:56 +00001682
drh57dbd7b2005-07-08 18:25:26 +00001683 /* This code must be run in its entirety every time it is encountered
1684 ** if any of the following is true:
1685 **
1686 ** * The right-hand side is a correlated subquery
1687 ** * The right-hand side is an expression list containing variables
1688 ** * We are inside a trigger
1689 **
1690 ** If all of the above are false, then we can run this code just once
1691 ** save the results, and reuse the same result on subsequent invocations.
danielk1977b3bce662005-01-29 08:32:43 +00001692 */
1693 if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->trigStack ){
drh0a07c102008-01-03 18:03:08 +00001694 int mem = ++pParse->nMem;
drh892d3172008-01-10 03:46:36 +00001695 sqlite3VdbeAddOp1(v, OP_If, mem);
1696 testAddr = sqlite3VdbeAddOp2(v, OP_Integer, 1, mem);
drh17435752007-08-16 04:30:38 +00001697 assert( testAddr>0 || pParse->db->mallocFailed );
danielk1977b3bce662005-01-29 08:32:43 +00001698 }
1699
drhcce7d172000-05-31 15:34:51 +00001700 switch( pExpr->op ){
drhfef52082000-06-06 01:50:43 +00001701 case TK_IN: {
danielk1977e014a832004-05-17 10:48:57 +00001702 char affinity;
drhd3d39e92004-05-20 22:16:29 +00001703 KeyInfo keyInfo;
drhb9bb7c12006-06-11 23:41:55 +00001704 int addr; /* Address of OP_OpenEphemeral instruction */
drhd3d39e92004-05-20 22:16:29 +00001705
danielk1977bf3b7212004-05-18 10:06:24 +00001706 affinity = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +00001707
1708 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
drh57dbd7b2005-07-08 18:25:26 +00001709 ** expression it is handled the same way. A virtual table is
danielk1977e014a832004-05-17 10:48:57 +00001710 ** filled with single-field index keys representing the results
1711 ** from the SELECT or the <exprlist>.
1712 **
1713 ** If the 'x' expression is a column value, or the SELECT...
1714 ** statement returns a column value, then the affinity of that
1715 ** column is used to build the index keys. If both 'x' and the
1716 ** SELECT... statement are columns, then numeric affinity is used
1717 ** if either column has NUMERIC or INTEGER affinity. If neither
1718 ** 'x' nor the SELECT... statement are columns, then numeric affinity
1719 ** is used.
1720 */
1721 pExpr->iTable = pParse->nTab++;
drh66a51672008-01-03 00:01:23 +00001722 addr = sqlite3VdbeAddOp1(v, OP_OpenEphemeral, pExpr->iTable);
drhd3d39e92004-05-20 22:16:29 +00001723 memset(&keyInfo, 0, sizeof(keyInfo));
1724 keyInfo.nField = 1;
drh66a51672008-01-03 00:01:23 +00001725 sqlite3VdbeAddOp2(v, OP_SetNumColumns, pExpr->iTable, 1);
danielk1977e014a832004-05-17 10:48:57 +00001726
drhfef52082000-06-06 01:50:43 +00001727 if( pExpr->pSelect ){
1728 /* Case 1: expr IN (SELECT ...)
1729 **
danielk1977e014a832004-05-17 10:48:57 +00001730 ** Generate code to write the results of the select into the temporary
1731 ** table allocated and opened above.
drhfef52082000-06-06 01:50:43 +00001732 */
drh1013c932008-01-06 00:25:21 +00001733 SelectDest dest;
drhbe5c89a2004-07-26 00:31:09 +00001734 ExprList *pEList;
drh1013c932008-01-06 00:25:21 +00001735
1736 sqlite3SelectDestInit(&dest, SRT_Set, pExpr->iTable);
1737 dest.affinity = (int)affinity;
danielk1977e014a832004-05-17 10:48:57 +00001738 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
danielk19776c8c8ce2008-01-02 16:27:09 +00001739 if( sqlite3Select(pParse, pExpr->pSelect, &dest, 0, 0, 0, 0) ){
drh94ccde52007-04-13 16:06:32 +00001740 return;
1741 }
drhbe5c89a2004-07-26 00:31:09 +00001742 pEList = pExpr->pSelect->pEList;
1743 if( pEList && pEList->nExpr>0 ){
danielk1977bcbb04e2007-05-29 12:11:29 +00001744 keyInfo.aColl[0] = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft,
drhbe5c89a2004-07-26 00:31:09 +00001745 pEList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001746 }
drhfef52082000-06-06 01:50:43 +00001747 }else if( pExpr->pList ){
1748 /* Case 2: expr IN (exprlist)
1749 **
drhfd131da2007-08-07 17:13:03 +00001750 ** For each expression, build an index key from the evaluation and
danielk1977e014a832004-05-17 10:48:57 +00001751 ** store it in the temporary table. If <expr> is a column, then use
1752 ** that columns affinity when building index keys. If <expr> is not
1753 ** a column, use numeric affinity.
drhfef52082000-06-06 01:50:43 +00001754 */
danielk1977e014a832004-05-17 10:48:57 +00001755 int i;
drh57dbd7b2005-07-08 18:25:26 +00001756 ExprList *pList = pExpr->pList;
1757 struct ExprList_item *pItem;
drh2d401ab2008-01-10 23:50:11 +00001758 int r1, r2;
drh57dbd7b2005-07-08 18:25:26 +00001759
danielk1977e014a832004-05-17 10:48:57 +00001760 if( !affinity ){
drh8159a352006-05-23 23:22:29 +00001761 affinity = SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +00001762 }
danielk19770202b292004-06-09 09:55:16 +00001763 keyInfo.aColl[0] = pExpr->pLeft->pColl;
danielk1977e014a832004-05-17 10:48:57 +00001764
1765 /* Loop through each expression in <exprlist>. */
drh2d401ab2008-01-10 23:50:11 +00001766 r1 = sqlite3GetTempReg(pParse);
1767 r2 = sqlite3GetTempReg(pParse);
drh57dbd7b2005-07-08 18:25:26 +00001768 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
1769 Expr *pE2 = pItem->pExpr;
danielk1977e014a832004-05-17 10:48:57 +00001770
drh57dbd7b2005-07-08 18:25:26 +00001771 /* If the expression is not constant then we will need to
1772 ** disable the test that was generated above that makes sure
1773 ** this code only executes once. Because for a non-constant
1774 ** expression we need to rerun this code each time.
1775 */
drh892d3172008-01-10 03:46:36 +00001776 if( testAddr && !sqlite3ExprIsConstant(pE2) ){
1777 sqlite3VdbeChangeToNoop(v, testAddr-1, 2);
drh57dbd7b2005-07-08 18:25:26 +00001778 testAddr = 0;
drh4794b982000-06-06 13:54:14 +00001779 }
danielk1977e014a832004-05-17 10:48:57 +00001780
1781 /* Evaluate the expression and insert it into the temp table */
drh2d401ab2008-01-10 23:50:11 +00001782 sqlite3ExprCode(pParse, pE2, r1);
drh1db639c2008-01-17 02:36:28 +00001783 sqlite3VdbeAddOp4(v, OP_MakeRecord, r1, 1, r2, &affinity, 1);
drh2d401ab2008-01-10 23:50:11 +00001784 sqlite3VdbeAddOp2(v, OP_IdxInsert, pExpr->iTable, r2);
drhfef52082000-06-06 01:50:43 +00001785 }
drh2d401ab2008-01-10 23:50:11 +00001786 sqlite3ReleaseTempReg(pParse, r1);
1787 sqlite3ReleaseTempReg(pParse, r2);
drhfef52082000-06-06 01:50:43 +00001788 }
drh66a51672008-01-03 00:01:23 +00001789 sqlite3VdbeChangeP4(v, addr, (void *)&keyInfo, P4_KEYINFO);
danielk1977b3bce662005-01-29 08:32:43 +00001790 break;
drhfef52082000-06-06 01:50:43 +00001791 }
1792
drh51522cd2005-01-20 13:36:19 +00001793 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001794 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +00001795 /* This has to be a scalar SELECT. Generate code to put the
1796 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +00001797 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +00001798 */
drh2646da72005-12-09 20:02:05 +00001799 static const Token one = { (u8*)"1", 0, 1 };
drh51522cd2005-01-20 13:36:19 +00001800 Select *pSel;
danielk19776c8c8ce2008-01-02 16:27:09 +00001801 SelectDest dest;
drh1398ad32005-01-19 23:24:50 +00001802
drh51522cd2005-01-20 13:36:19 +00001803 pSel = pExpr->pSelect;
drh1013c932008-01-06 00:25:21 +00001804 sqlite3SelectDestInit(&dest, 0, ++pParse->nMem);
drh51522cd2005-01-20 13:36:19 +00001805 if( pExpr->op==TK_SELECT ){
danielk19776c8c8ce2008-01-02 16:27:09 +00001806 dest.eDest = SRT_Mem;
drh4c583122008-01-04 22:01:03 +00001807 sqlite3VdbeAddOp2(v, OP_Null, 0, dest.iParm);
drhd4e70eb2008-01-02 00:34:36 +00001808 VdbeComment((v, "Init subquery result"));
drh51522cd2005-01-20 13:36:19 +00001809 }else{
danielk19776c8c8ce2008-01-02 16:27:09 +00001810 dest.eDest = SRT_Exists;
drh4c583122008-01-04 22:01:03 +00001811 sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iParm);
drhd4e70eb2008-01-02 00:34:36 +00001812 VdbeComment((v, "Init EXISTS result"));
drh51522cd2005-01-20 13:36:19 +00001813 }
drhec7429a2005-10-06 16:53:14 +00001814 sqlite3ExprDelete(pSel->pLimit);
danielk1977a1644fd2007-08-29 12:31:25 +00001815 pSel->pLimit = sqlite3PExpr(pParse, TK_INTEGER, 0, 0, &one);
danielk19776c8c8ce2008-01-02 16:27:09 +00001816 if( sqlite3Select(pParse, pSel, &dest, 0, 0, 0, 0) ){
drh94ccde52007-04-13 16:06:32 +00001817 return;
1818 }
danielk19776c8c8ce2008-01-02 16:27:09 +00001819 pExpr->iColumn = dest.iParm;
danielk1977b3bce662005-01-29 08:32:43 +00001820 break;
drhcce7d172000-05-31 15:34:51 +00001821 }
1822 }
danielk1977b3bce662005-01-29 08:32:43 +00001823
drh57dbd7b2005-07-08 18:25:26 +00001824 if( testAddr ){
drh892d3172008-01-10 03:46:36 +00001825 sqlite3VdbeJumpHere(v, testAddr-1);
danielk1977b3bce662005-01-29 08:32:43 +00001826 }
danielk1977fc976062007-05-10 10:46:56 +00001827
danielk1977b3bce662005-01-29 08:32:43 +00001828 return;
drhcce7d172000-05-31 15:34:51 +00001829}
drh51522cd2005-01-20 13:36:19 +00001830#endif /* SQLITE_OMIT_SUBQUERY */
drhcce7d172000-05-31 15:34:51 +00001831
drhcce7d172000-05-31 15:34:51 +00001832/*
drh598f1342007-10-23 15:39:45 +00001833** Duplicate an 8-byte value
1834*/
1835static char *dup8bytes(Vdbe *v, const char *in){
1836 char *out = sqlite3DbMallocRaw(sqlite3VdbeDb(v), 8);
1837 if( out ){
1838 memcpy(out, in, 8);
1839 }
1840 return out;
1841}
1842
1843/*
1844** Generate an instruction that will put the floating point
drh9cbf3422008-01-17 16:22:13 +00001845** value described by z[0..n-1] into register iMem.
drh0cf19ed2007-10-23 18:55:48 +00001846**
1847** The z[] string will probably not be zero-terminated. But the
1848** z[n] character is guaranteed to be something that does not look
1849** like the continuation of the number.
drh598f1342007-10-23 15:39:45 +00001850*/
drh9de221d2008-01-05 06:51:30 +00001851static void codeReal(Vdbe *v, const char *z, int n, int negateFlag, int iMem){
drh598f1342007-10-23 15:39:45 +00001852 assert( z || v==0 || sqlite3VdbeDb(v)->mallocFailed );
1853 if( z ){
1854 double value;
1855 char *zV;
drh0cf19ed2007-10-23 18:55:48 +00001856 assert( !isdigit(z[n]) );
drh598f1342007-10-23 15:39:45 +00001857 sqlite3AtoF(z, &value);
1858 if( negateFlag ) value = -value;
1859 zV = dup8bytes(v, (char*)&value);
drh9de221d2008-01-05 06:51:30 +00001860 sqlite3VdbeAddOp4(v, OP_Real, 0, iMem, 0, zV, P4_REAL);
drh598f1342007-10-23 15:39:45 +00001861 }
1862}
1863
1864
1865/*
drhfec19aa2004-05-19 20:41:03 +00001866** Generate an instruction that will put the integer describe by
drh9cbf3422008-01-17 16:22:13 +00001867** text z[0..n-1] into register iMem.
drh0cf19ed2007-10-23 18:55:48 +00001868**
1869** The z[] string will probably not be zero-terminated. But the
1870** z[n] character is guaranteed to be something that does not look
1871** like the continuation of the number.
drhfec19aa2004-05-19 20:41:03 +00001872*/
drh9de221d2008-01-05 06:51:30 +00001873static void codeInteger(Vdbe *v, const char *z, int n, int negFlag, int iMem){
drhabb6fca2007-08-16 12:24:01 +00001874 assert( z || v==0 || sqlite3VdbeDb(v)->mallocFailed );
danielk1977c9cf9012007-05-30 10:36:47 +00001875 if( z ){
1876 int i;
drh0cf19ed2007-10-23 18:55:48 +00001877 assert( !isdigit(z[n]) );
danielk1977c9cf9012007-05-30 10:36:47 +00001878 if( sqlite3GetInt32(z, &i) ){
drh9de221d2008-01-05 06:51:30 +00001879 if( negFlag ) i = -i;
1880 sqlite3VdbeAddOp2(v, OP_Integer, i, iMem);
1881 }else if( sqlite3FitsIn64Bits(z, negFlag) ){
drh598f1342007-10-23 15:39:45 +00001882 i64 value;
1883 char *zV;
1884 sqlite3Atoi64(z, &value);
drh9de221d2008-01-05 06:51:30 +00001885 if( negFlag ) value = -value;
drh598f1342007-10-23 15:39:45 +00001886 zV = dup8bytes(v, (char*)&value);
drh9de221d2008-01-05 06:51:30 +00001887 sqlite3VdbeAddOp4(v, OP_Int64, 0, iMem, 0, zV, P4_INT64);
danielk1977c9cf9012007-05-30 10:36:47 +00001888 }else{
drh9de221d2008-01-05 06:51:30 +00001889 codeReal(v, z, n, negFlag, iMem);
danielk1977c9cf9012007-05-30 10:36:47 +00001890 }
drhfec19aa2004-05-19 20:41:03 +00001891 }
1892}
1893
drh945498f2007-02-24 11:52:52 +00001894
1895/*
1896** Generate code that will extract the iColumn-th column from
drh9cbf3422008-01-17 16:22:13 +00001897** table pTab and store the column value in register iReg.
1898** There is an open cursor to pTab in
drh2133d822008-01-03 18:44:59 +00001899** iTable. If iColumn<0 then code is generated that extracts the rowid.
drh945498f2007-02-24 11:52:52 +00001900*/
drh2133d822008-01-03 18:44:59 +00001901void sqlite3ExprCodeGetColumn(
1902 Vdbe *v, /* The VM being created */
1903 Table *pTab, /* Description of the table we are reading from */
1904 int iColumn, /* Index of the table column */
1905 int iTable, /* The cursor pointing to the table */
1906 int iReg /* Store results here */
1907){
drh945498f2007-02-24 11:52:52 +00001908 if( iColumn<0 ){
1909 int op = (pTab && IsVirtual(pTab)) ? OP_VRowid : OP_Rowid;
drh2133d822008-01-03 18:44:59 +00001910 sqlite3VdbeAddOp2(v, op, iTable, iReg);
drh945498f2007-02-24 11:52:52 +00001911 }else if( pTab==0 ){
drh2133d822008-01-03 18:44:59 +00001912 sqlite3VdbeAddOp3(v, OP_Column, iTable, iColumn, iReg);
drh945498f2007-02-24 11:52:52 +00001913 }else{
1914 int op = IsVirtual(pTab) ? OP_VColumn : OP_Column;
drh2133d822008-01-03 18:44:59 +00001915 sqlite3VdbeAddOp3(v, op, iTable, iColumn, iReg);
drh945498f2007-02-24 11:52:52 +00001916 sqlite3ColumnDefault(v, pTab, iColumn);
1917#ifndef SQLITE_OMIT_FLOATING_POINT
1918 if( pTab->aCol[iColumn].affinity==SQLITE_AFF_REAL ){
drh2133d822008-01-03 18:44:59 +00001919 sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg);
drh945498f2007-02-24 11:52:52 +00001920 }
1921#endif
1922 }
1923}
1924
drhfec19aa2004-05-19 20:41:03 +00001925/*
drhcce7d172000-05-31 15:34:51 +00001926** Generate code into the current Vdbe to evaluate the given
drh2dcef112008-01-12 19:03:48 +00001927** expression. Attempt to store the results in register "target".
1928** Return the register where results are stored.
drh389a1ad2008-01-03 23:44:53 +00001929**
drh2dcef112008-01-12 19:03:48 +00001930** With this routine, there is no guaranteed that results will
1931** be stored in target. The result might be stored in some other
1932** register if it is convenient to do so. The calling function
1933** must check the return code and move the results to the desired
1934** register.
drhcce7d172000-05-31 15:34:51 +00001935*/
drh2dcef112008-01-12 19:03:48 +00001936static int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){
1937 Vdbe *v = pParse->pVdbe; /* The VM under construction */
1938 int op; /* The opcode being coded */
1939 int inReg = target; /* Results stored in register inReg */
1940 int regFree1 = 0; /* If non-zero free this temporary register */
1941 int regFree2 = 0; /* If non-zero free this temporary register */
1942 int r1, r2, r3; /* Various register numbers */
drhffe07b22005-11-03 00:41:17 +00001943
drh389a1ad2008-01-03 23:44:53 +00001944 assert( v!=0 || pParse->db->mallocFailed );
drh9cbf3422008-01-17 16:22:13 +00001945 assert( target>0 && target<=pParse->nMem );
drh389a1ad2008-01-03 23:44:53 +00001946 if( v==0 ) return 0;
drh389a1ad2008-01-03 23:44:53 +00001947
1948 if( pExpr==0 ){
1949 op = TK_NULL;
1950 }else{
1951 op = pExpr->op;
1952 }
drhf2bc0132004-10-04 13:19:23 +00001953 switch( op ){
drh13449892005-09-07 21:22:45 +00001954 case TK_AGG_COLUMN: {
1955 AggInfo *pAggInfo = pExpr->pAggInfo;
1956 struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg];
1957 if( !pAggInfo->directMode ){
drh9de221d2008-01-05 06:51:30 +00001958 assert( pCol->iMem>0 );
1959 inReg = pCol->iMem;
drh13449892005-09-07 21:22:45 +00001960 break;
1961 }else if( pAggInfo->useSortingIdx ){
drh389a1ad2008-01-03 23:44:53 +00001962 sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdx,
1963 pCol->iSorterColumn, target);
drh13449892005-09-07 21:22:45 +00001964 break;
1965 }
1966 /* Otherwise, fall thru into the TK_COLUMN case */
1967 }
drh967e8b72000-06-21 13:59:10 +00001968 case TK_COLUMN: {
drhffe07b22005-11-03 00:41:17 +00001969 if( pExpr->iTable<0 ){
1970 /* This only happens when coding check constraints */
drhaa9b8962008-01-08 02:57:55 +00001971 assert( pParse->ckBase>0 );
1972 inReg = pExpr->iColumn + pParse->ckBase;
drhc4a3c772001-04-04 11:48:57 +00001973 }else{
drh2133d822008-01-03 18:44:59 +00001974 sqlite3ExprCodeGetColumn(v, pExpr->pTab,
drh389a1ad2008-01-03 23:44:53 +00001975 pExpr->iColumn, pExpr->iTable, target);
drh22827922000-06-06 17:27:05 +00001976 }
drhcce7d172000-05-31 15:34:51 +00001977 break;
1978 }
1979 case TK_INTEGER: {
drh9de221d2008-01-05 06:51:30 +00001980 codeInteger(v, (char*)pExpr->token.z, pExpr->token.n, 0, target);
drhfec19aa2004-05-19 20:41:03 +00001981 break;
1982 }
drh598f1342007-10-23 15:39:45 +00001983 case TK_FLOAT: {
drh9de221d2008-01-05 06:51:30 +00001984 codeReal(v, (char*)pExpr->token.z, pExpr->token.n, 0, target);
drh598f1342007-10-23 15:39:45 +00001985 break;
1986 }
drhfec19aa2004-05-19 20:41:03 +00001987 case TK_STRING: {
danielk19771e536952007-08-16 10:09:01 +00001988 sqlite3DequoteExpr(pParse->db, pExpr);
drh9de221d2008-01-05 06:51:30 +00001989 sqlite3VdbeAddOp4(v,OP_String8, 0, target, 0,
drh66a51672008-01-03 00:01:23 +00001990 (char*)pExpr->token.z, pExpr->token.n);
drhcce7d172000-05-31 15:34:51 +00001991 break;
1992 }
drhf0863fe2005-06-12 21:35:51 +00001993 case TK_NULL: {
drh9de221d2008-01-05 06:51:30 +00001994 sqlite3VdbeAddOp2(v, OP_Null, 0, target);
drhf0863fe2005-06-12 21:35:51 +00001995 break;
1996 }
danielk19775338a5f2005-01-20 13:03:10 +00001997#ifndef SQLITE_OMIT_BLOB_LITERAL
danielk1977c572ef72004-05-27 09:28:41 +00001998 case TK_BLOB: {
drh6c8c6ce2005-08-23 11:17:58 +00001999 int n;
2000 const char *z;
drhca48c902008-01-18 14:08:24 +00002001 char *zBlob;
2002 assert( pExpr->token.n>=3 );
2003 assert( pExpr->token.z[0]=='x' || pExpr->token.z[0]=='X' );
2004 assert( pExpr->token.z[1]=='\'' );
2005 assert( pExpr->token.z[pExpr->token.n-1]=='\'' );
drh6c8c6ce2005-08-23 11:17:58 +00002006 n = pExpr->token.n - 3;
drh2646da72005-12-09 20:02:05 +00002007 z = (char*)pExpr->token.z + 2;
drhca48c902008-01-18 14:08:24 +00002008 zBlob = sqlite3HexToBlob(sqlite3VdbeDb(v), z, n);
2009 sqlite3VdbeAddOp4(v, OP_Blob, n/2, target, 0, zBlob, P4_DYNAMIC);
danielk1977c572ef72004-05-27 09:28:41 +00002010 break;
2011 }
danielk19775338a5f2005-01-20 13:03:10 +00002012#endif
drh50457892003-09-06 01:10:47 +00002013 case TK_VARIABLE: {
drh9de221d2008-01-05 06:51:30 +00002014 sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iTable, target);
drh895d7472004-08-20 16:02:39 +00002015 if( pExpr->token.n>1 ){
drh66a51672008-01-03 00:01:23 +00002016 sqlite3VdbeChangeP4(v, -1, (char*)pExpr->token.z, pExpr->token.n);
drh895d7472004-08-20 16:02:39 +00002017 }
drh50457892003-09-06 01:10:47 +00002018 break;
2019 }
drh4e0cff62004-11-05 05:10:28 +00002020 case TK_REGISTER: {
drh9de221d2008-01-05 06:51:30 +00002021 inReg = pExpr->iTable;
drh4e0cff62004-11-05 05:10:28 +00002022 break;
2023 }
drh487e2622005-06-25 18:42:14 +00002024#ifndef SQLITE_OMIT_CAST
2025 case TK_CAST: {
2026 /* Expressions of the form: CAST(pLeft AS token) */
danielk1977f0113002006-01-24 12:09:17 +00002027 int aff, to_op;
drh2dcef112008-01-12 19:03:48 +00002028 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
drh8a512562005-11-14 22:29:05 +00002029 aff = sqlite3AffinityType(&pExpr->token);
danielk1977f0113002006-01-24 12:09:17 +00002030 to_op = aff - SQLITE_AFF_TEXT + OP_ToText;
2031 assert( to_op==OP_ToText || aff!=SQLITE_AFF_TEXT );
2032 assert( to_op==OP_ToBlob || aff!=SQLITE_AFF_NONE );
2033 assert( to_op==OP_ToNumeric || aff!=SQLITE_AFF_NUMERIC );
2034 assert( to_op==OP_ToInt || aff!=SQLITE_AFF_INTEGER );
2035 assert( to_op==OP_ToReal || aff!=SQLITE_AFF_REAL );
drh2dcef112008-01-12 19:03:48 +00002036 sqlite3VdbeAddOp1(v, to_op, inReg);
drh487e2622005-06-25 18:42:14 +00002037 break;
2038 }
2039#endif /* SQLITE_OMIT_CAST */
drhc9b84a12002-06-20 11:36:48 +00002040 case TK_LT:
2041 case TK_LE:
2042 case TK_GT:
2043 case TK_GE:
2044 case TK_NE:
2045 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00002046 assert( TK_LT==OP_Lt );
2047 assert( TK_LE==OP_Le );
2048 assert( TK_GT==OP_Gt );
2049 assert( TK_GE==OP_Ge );
2050 assert( TK_EQ==OP_Eq );
2051 assert( TK_NE==OP_Ne );
drh2dcef112008-01-12 19:03:48 +00002052 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
2053 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
drh35573352008-01-08 23:54:25 +00002054 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
2055 r1, r2, inReg, SQLITE_STOREP2);
danielk1977a37cdde2004-05-16 11:15:36 +00002056 break;
drhc9b84a12002-06-20 11:36:48 +00002057 }
drhcce7d172000-05-31 15:34:51 +00002058 case TK_AND:
2059 case TK_OR:
2060 case TK_PLUS:
2061 case TK_STAR:
2062 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00002063 case TK_REM:
2064 case TK_BITAND:
2065 case TK_BITOR:
drh17c40292004-07-21 02:53:29 +00002066 case TK_SLASH:
drhbf4133c2001-10-13 02:59:08 +00002067 case TK_LSHIFT:
drh855eb1c2004-08-31 13:45:11 +00002068 case TK_RSHIFT:
drh00400772000-06-16 20:51:26 +00002069 case TK_CONCAT: {
drhf2bc0132004-10-04 13:19:23 +00002070 assert( TK_AND==OP_And );
2071 assert( TK_OR==OP_Or );
2072 assert( TK_PLUS==OP_Add );
2073 assert( TK_MINUS==OP_Subtract );
2074 assert( TK_REM==OP_Remainder );
2075 assert( TK_BITAND==OP_BitAnd );
2076 assert( TK_BITOR==OP_BitOr );
2077 assert( TK_SLASH==OP_Divide );
2078 assert( TK_LSHIFT==OP_ShiftLeft );
2079 assert( TK_RSHIFT==OP_ShiftRight );
2080 assert( TK_CONCAT==OP_Concat );
drh2dcef112008-01-12 19:03:48 +00002081 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
2082 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
drh5b6afba2008-01-05 16:29:28 +00002083 sqlite3VdbeAddOp3(v, op, r2, r1, target);
drh00400772000-06-16 20:51:26 +00002084 break;
2085 }
drhcce7d172000-05-31 15:34:51 +00002086 case TK_UMINUS: {
drhfec19aa2004-05-19 20:41:03 +00002087 Expr *pLeft = pExpr->pLeft;
2088 assert( pLeft );
2089 if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
2090 Token *p = &pLeft->token;
drhfec19aa2004-05-19 20:41:03 +00002091 if( pLeft->op==TK_FLOAT ){
drh9de221d2008-01-05 06:51:30 +00002092 codeReal(v, (char*)p->z, p->n, 1, target);
drhe6840902002-03-06 03:08:25 +00002093 }else{
drh9de221d2008-01-05 06:51:30 +00002094 codeInteger(v, (char*)p->z, p->n, 1, target);
drhe6840902002-03-06 03:08:25 +00002095 }
drh3c84ddf2008-01-09 02:15:38 +00002096 }else{
drh2dcef112008-01-12 19:03:48 +00002097 regFree1 = r1 = sqlite3GetTempReg(pParse);
drh3c84ddf2008-01-09 02:15:38 +00002098 sqlite3VdbeAddOp2(v, OP_Integer, 0, r1);
drh2dcef112008-01-12 19:03:48 +00002099 r2 = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
2100 sqlite3VdbeAddOp3(v, OP_Subtract, r2, r1, target);
drh6e142f52000-06-08 13:36:40 +00002101 }
drh3c84ddf2008-01-09 02:15:38 +00002102 inReg = target;
2103 break;
drh6e142f52000-06-08 13:36:40 +00002104 }
drhbf4133c2001-10-13 02:59:08 +00002105 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00002106 case TK_NOT: {
drhf2bc0132004-10-04 13:19:23 +00002107 assert( TK_BITNOT==OP_BitNot );
2108 assert( TK_NOT==OP_Not );
drh2dcef112008-01-12 19:03:48 +00002109 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
2110 sqlite3VdbeAddOp1(v, op, inReg);
drhcce7d172000-05-31 15:34:51 +00002111 break;
2112 }
2113 case TK_ISNULL:
2114 case TK_NOTNULL: {
drh6a288a32008-01-07 19:20:24 +00002115 int addr;
drhf2bc0132004-10-04 13:19:23 +00002116 assert( TK_ISNULL==OP_IsNull );
2117 assert( TK_NOTNULL==OP_NotNull );
drh9de221d2008-01-05 06:51:30 +00002118 sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
drh2dcef112008-01-12 19:03:48 +00002119 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
2120 addr = sqlite3VdbeAddOp1(v, op, r1);
drh9de221d2008-01-05 06:51:30 +00002121 sqlite3VdbeAddOp2(v, OP_AddImm, target, -1);
drh6a288a32008-01-07 19:20:24 +00002122 sqlite3VdbeJumpHere(v, addr);
drhf2bc0132004-10-04 13:19:23 +00002123 break;
drhcce7d172000-05-31 15:34:51 +00002124 }
drh22827922000-06-06 17:27:05 +00002125 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00002126 AggInfo *pInfo = pExpr->pAggInfo;
drh7e56e712005-11-16 12:53:15 +00002127 if( pInfo==0 ){
2128 sqlite3ErrorMsg(pParse, "misuse of aggregate: %T",
2129 &pExpr->span);
2130 }else{
drh9de221d2008-01-05 06:51:30 +00002131 inReg = pInfo->aFunc[pExpr->iAgg].iMem;
drh7e56e712005-11-16 12:53:15 +00002132 }
drh22827922000-06-06 17:27:05 +00002133 break;
2134 }
drhb71090f2005-05-23 17:26:51 +00002135 case TK_CONST_FUNC:
drhcce7d172000-05-31 15:34:51 +00002136 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00002137 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00002138 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00002139 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00002140 int nId;
2141 const char *zId;
drh13449892005-09-07 21:22:45 +00002142 int constMask = 0;
danielk1977682f68b2004-06-05 10:22:17 +00002143 int i;
drh17435752007-08-16 04:30:38 +00002144 sqlite3 *db = pParse->db;
2145 u8 enc = ENC(db);
danielk1977dc1bdc42004-06-11 10:51:27 +00002146 CollSeq *pColl = 0;
drh17435752007-08-16 04:30:38 +00002147
drh2646da72005-12-09 20:02:05 +00002148 zId = (char*)pExpr->token.z;
drhb71090f2005-05-23 17:26:51 +00002149 nId = pExpr->token.n;
danielk1977d8123362004-06-12 09:25:12 +00002150 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0);
drh0bce8352002-02-28 00:41:10 +00002151 assert( pDef!=0 );
drh892d3172008-01-10 03:46:36 +00002152 if( pList ){
2153 nExpr = pList->nExpr;
drh2dcef112008-01-12 19:03:48 +00002154 r1 = sqlite3GetTempRange(pParse, nExpr);
2155 sqlite3ExprCodeExprList(pParse, pList, r1);
drh892d3172008-01-10 03:46:36 +00002156 }else{
drhd847eaa2008-01-17 17:15:56 +00002157 nExpr = r1 = 0;
drh892d3172008-01-10 03:46:36 +00002158 }
drhb7f6f682006-07-08 17:06:43 +00002159#ifndef SQLITE_OMIT_VIRTUALTABLE
drha43fa222006-07-08 18:41:37 +00002160 /* Possibly overload the function if the first argument is
2161 ** a virtual table column.
2162 **
2163 ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the
2164 ** second argument, not the first, as the argument to test to
2165 ** see if it is a column in a virtual table. This is done because
2166 ** the left operand of infix functions (the operand we want to
2167 ** control overloading) ends up as the second argument to the
2168 ** function. The expression "A glob B" is equivalent to
2169 ** "glob(B,A). We want to use the A in "A glob B" to test
2170 ** for function overloading. But we use the B term in "glob(B,A)".
2171 */
drh6a03a1c2006-07-08 18:34:59 +00002172 if( nExpr>=2 && (pExpr->flags & EP_InfixFunc) ){
drh17435752007-08-16 04:30:38 +00002173 pDef = sqlite3VtabOverloadFunction(db, pDef, nExpr, pList->a[1].pExpr);
drh6a03a1c2006-07-08 18:34:59 +00002174 }else if( nExpr>0 ){
drh17435752007-08-16 04:30:38 +00002175 pDef = sqlite3VtabOverloadFunction(db, pDef, nExpr, pList->a[0].pExpr);
drhb7f6f682006-07-08 17:06:43 +00002176 }
2177#endif
danielk1977682f68b2004-06-05 10:22:17 +00002178 for(i=0; i<nExpr && i<32; i++){
danielk1977d02eb1f2004-06-06 09:44:03 +00002179 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
drh13449892005-09-07 21:22:45 +00002180 constMask |= (1<<i);
danielk1977d02eb1f2004-06-06 09:44:03 +00002181 }
danielk1977dc1bdc42004-06-11 10:51:27 +00002182 if( pDef->needCollSeq && !pColl ){
2183 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
2184 }
2185 }
2186 if( pDef->needCollSeq ){
2187 if( !pColl ) pColl = pParse->db->pDfltColl;
drh66a51672008-01-03 00:01:23 +00002188 sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ);
danielk1977682f68b2004-06-05 10:22:17 +00002189 }
drh2dcef112008-01-12 19:03:48 +00002190 sqlite3VdbeAddOp4(v, OP_Function, constMask, r1, target,
drh66a51672008-01-03 00:01:23 +00002191 (char*)pDef, P4_FUNCDEF);
drh98757152008-01-09 23:04:12 +00002192 sqlite3VdbeChangeP5(v, nExpr);
drh2dcef112008-01-12 19:03:48 +00002193 if( nExpr ){
2194 sqlite3ReleaseTempRange(pParse, r1, nExpr);
2195 }
drhcce7d172000-05-31 15:34:51 +00002196 break;
2197 }
drhfe2093d2005-01-20 22:48:47 +00002198#ifndef SQLITE_OMIT_SUBQUERY
2199 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00002200 case TK_SELECT: {
drh41714d62006-03-02 04:44:23 +00002201 if( pExpr->iColumn==0 ){
2202 sqlite3CodeSubselect(pParse, pExpr);
2203 }
drh9de221d2008-01-05 06:51:30 +00002204 inReg = pExpr->iColumn;
drh19a775c2000-06-05 18:54:46 +00002205 break;
2206 }
drhfef52082000-06-06 01:50:43 +00002207 case TK_IN: {
drh6a288a32008-01-07 19:20:24 +00002208 int j1, j2, j3, j4, j5;
drh94a11212004-09-25 13:12:14 +00002209 char affinity;
danielk19779a96b662007-11-29 17:05:18 +00002210 int eType;
danielk19779a96b662007-11-29 17:05:18 +00002211
2212 eType = sqlite3FindInIndex(pParse, pExpr, 0);
danielk1977e014a832004-05-17 10:48:57 +00002213
2214 /* Figure out the affinity to use to create a key from the results
2215 ** of the expression. affinityStr stores a static string suitable for
drh66a51672008-01-03 00:01:23 +00002216 ** P4 of OP_MakeRecord.
danielk1977e014a832004-05-17 10:48:57 +00002217 */
drh94a11212004-09-25 13:12:14 +00002218 affinity = comparisonAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +00002219
drh2dcef112008-01-12 19:03:48 +00002220 sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
danielk1977e014a832004-05-17 10:48:57 +00002221
2222 /* Code the <expr> from "<expr> IN (...)". The temporary table
2223 ** pExpr->iTable contains the values that make up the (...) set.
2224 */
drh2dcef112008-01-12 19:03:48 +00002225 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
2226 j1 = sqlite3VdbeAddOp1(v, OP_NotNull, r1);
2227 sqlite3VdbeAddOp2(v, OP_Null, 0, target);
drh6a288a32008-01-07 19:20:24 +00002228 j2 = sqlite3VdbeAddOp0(v, OP_Goto);
2229 sqlite3VdbeJumpHere(v, j1);
danielk19779a96b662007-11-29 17:05:18 +00002230 if( eType==IN_INDEX_ROWID ){
drh2dcef112008-01-12 19:03:48 +00002231 j3 = sqlite3VdbeAddOp3(v, OP_MustBeInt, r1, 0, 1);
2232 j4 = sqlite3VdbeAddOp3(v, OP_NotExists, pExpr->iTable, 0, r1);
drh6a288a32008-01-07 19:20:24 +00002233 j5 = sqlite3VdbeAddOp0(v, OP_Goto);
2234 sqlite3VdbeJumpHere(v, j3);
2235 sqlite3VdbeJumpHere(v, j4);
danielk19779a96b662007-11-29 17:05:18 +00002236 }else{
drh2dcef112008-01-12 19:03:48 +00002237 r2 = regFree2 = sqlite3GetTempReg(pParse);
drh1db639c2008-01-17 02:36:28 +00002238 sqlite3VdbeAddOp4(v, OP_MakeRecord, r1, 1, r2, &affinity, 1);
drh2dcef112008-01-12 19:03:48 +00002239 j5 = sqlite3VdbeAddOp3(v, OP_Found, pExpr->iTable, 0, r2);
danielk19779a96b662007-11-29 17:05:18 +00002240 }
drh2dcef112008-01-12 19:03:48 +00002241 sqlite3VdbeAddOp2(v, OP_AddImm, target, -1);
drh6a288a32008-01-07 19:20:24 +00002242 sqlite3VdbeJumpHere(v, j2);
2243 sqlite3VdbeJumpHere(v, j5);
drhfef52082000-06-06 01:50:43 +00002244 break;
2245 }
danielk197793758c82005-01-21 08:13:14 +00002246#endif
drh2dcef112008-01-12 19:03:48 +00002247 /*
2248 ** x BETWEEN y AND z
2249 **
2250 ** This is equivalent to
2251 **
2252 ** x>=y AND x<=z
2253 **
2254 ** X is stored in pExpr->pLeft.
2255 ** Y is stored in pExpr->pList->a[0].pExpr.
2256 ** Z is stored in pExpr->pList->a[1].pExpr.
2257 */
drhfef52082000-06-06 01:50:43 +00002258 case TK_BETWEEN: {
drhbe5c89a2004-07-26 00:31:09 +00002259 Expr *pLeft = pExpr->pLeft;
2260 struct ExprList_item *pLItem = pExpr->pList->a;
2261 Expr *pRight = pLItem->pExpr;
drh35573352008-01-08 23:54:25 +00002262
drh2dcef112008-01-12 19:03:48 +00002263 r1 = sqlite3ExprCodeTemp(pParse, pLeft, &regFree1);
2264 r2 = sqlite3ExprCodeTemp(pParse, pRight, &regFree2);
2265 r3 = sqlite3GetTempReg(pParse);
drh35573352008-01-08 23:54:25 +00002266 codeCompare(pParse, pLeft, pRight, OP_Ge,
2267 r1, r2, r3, SQLITE_STOREP2);
drhbe5c89a2004-07-26 00:31:09 +00002268 pLItem++;
2269 pRight = pLItem->pExpr;
drh2dcef112008-01-12 19:03:48 +00002270 sqlite3ReleaseTempReg(pParse, regFree2);
2271 r2 = sqlite3ExprCodeTemp(pParse, pRight, &regFree2);
2272 codeCompare(pParse, pLeft, pRight, OP_Le, r1, r2, r2, SQLITE_STOREP2);
2273 sqlite3VdbeAddOp3(v, OP_And, r3, r2, target);
2274 sqlite3ReleaseTempReg(pParse, r3);
drhfef52082000-06-06 01:50:43 +00002275 break;
2276 }
drh4f07e5f2007-05-14 11:34:46 +00002277 case TK_UPLUS: {
drh2dcef112008-01-12 19:03:48 +00002278 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
drha2e00042002-01-22 03:13:42 +00002279 break;
2280 }
drh2dcef112008-01-12 19:03:48 +00002281
2282 /*
2283 ** Form A:
2284 ** CASE x WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
2285 **
2286 ** Form B:
2287 ** CASE WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
2288 **
2289 ** Form A is can be transformed into the equivalent form B as follows:
2290 ** CASE WHEN x=e1 THEN r1 WHEN x=e2 THEN r2 ...
2291 ** WHEN x=eN THEN rN ELSE y END
2292 **
2293 ** X (if it exists) is in pExpr->pLeft.
2294 ** Y is in pExpr->pRight. The Y is also optional. If there is no
2295 ** ELSE clause and no other term matches, then the result of the
2296 ** exprssion is NULL.
2297 ** Ei is in pExpr->pList->a[i*2] and Ri is pExpr->pList->a[i*2+1].
2298 **
2299 ** The result of the expression is the Ri for the first matching Ei,
2300 ** or if there is no matching Ei, the ELSE term Y, or if there is
2301 ** no ELSE term, NULL.
2302 */
drh17a7f8d2002-03-24 13:13:27 +00002303 case TK_CASE: {
drh2dcef112008-01-12 19:03:48 +00002304 int endLabel; /* GOTO label for end of CASE stmt */
2305 int nextCase; /* GOTO label for next WHEN clause */
2306 int nExpr; /* 2x number of WHEN terms */
2307 int i; /* Loop counter */
2308 ExprList *pEList; /* List of WHEN terms */
2309 struct ExprList_item *aListelem; /* Array of WHEN terms */
2310 Expr opCompare; /* The X==Ei expression */
2311 Expr cacheX; /* Cached expression X */
2312 Expr *pX; /* The X expression */
2313 Expr *pTest; /* X==Ei (form A) or just Ei (form B) */
drh17a7f8d2002-03-24 13:13:27 +00002314
2315 assert(pExpr->pList);
2316 assert((pExpr->pList->nExpr % 2) == 0);
2317 assert(pExpr->pList->nExpr > 0);
drhbe5c89a2004-07-26 00:31:09 +00002318 pEList = pExpr->pList;
2319 aListelem = pEList->a;
2320 nExpr = pEList->nExpr;
drh2dcef112008-01-12 19:03:48 +00002321 endLabel = sqlite3VdbeMakeLabel(v);
2322 if( (pX = pExpr->pLeft)!=0 ){
2323 cacheX = *pX;
2324 cacheX.iTable = sqlite3ExprCodeTemp(pParse, pX, &regFree1);
2325 cacheX.op = TK_REGISTER;
2326 opCompare.op = TK_EQ;
2327 opCompare.pLeft = &cacheX;
2328 pTest = &opCompare;
drh17a7f8d2002-03-24 13:13:27 +00002329 }
drhf5905aa2002-05-26 20:54:33 +00002330 for(i=0; i<nExpr; i=i+2){
drh2dcef112008-01-12 19:03:48 +00002331 if( pX ){
2332 opCompare.pRight = aListelem[i].pExpr;
drh17a7f8d2002-03-24 13:13:27 +00002333 }else{
drh2dcef112008-01-12 19:03:48 +00002334 pTest = aListelem[i].pExpr;
drh17a7f8d2002-03-24 13:13:27 +00002335 }
drh2dcef112008-01-12 19:03:48 +00002336 nextCase = sqlite3VdbeMakeLabel(v);
2337 sqlite3ExprIfFalse(pParse, pTest, nextCase, SQLITE_JUMPIFNULL);
drh9de221d2008-01-05 06:51:30 +00002338 sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target);
drh2dcef112008-01-12 19:03:48 +00002339 sqlite3VdbeAddOp2(v, OP_Goto, 0, endLabel);
2340 sqlite3VdbeResolveLabel(v, nextCase);
drhf570f012002-05-31 15:51:25 +00002341 }
drh17a7f8d2002-03-24 13:13:27 +00002342 if( pExpr->pRight ){
drh9de221d2008-01-05 06:51:30 +00002343 sqlite3ExprCode(pParse, pExpr->pRight, target);
drh17a7f8d2002-03-24 13:13:27 +00002344 }else{
drh9de221d2008-01-05 06:51:30 +00002345 sqlite3VdbeAddOp2(v, OP_Null, 0, target);
drh17a7f8d2002-03-24 13:13:27 +00002346 }
drh2dcef112008-01-12 19:03:48 +00002347 sqlite3VdbeResolveLabel(v, endLabel);
danielk19776f349032002-06-11 02:25:40 +00002348 break;
2349 }
danielk19775338a5f2005-01-20 13:03:10 +00002350#ifndef SQLITE_OMIT_TRIGGER
danielk19776f349032002-06-11 02:25:40 +00002351 case TK_RAISE: {
2352 if( !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +00002353 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00002354 "RAISE() may only be used within a trigger-program");
drh389a1ad2008-01-03 23:44:53 +00002355 return 0;
danielk19776f349032002-06-11 02:25:40 +00002356 }
drhad6d9462004-09-19 02:15:24 +00002357 if( pExpr->iColumn!=OE_Ignore ){
2358 assert( pExpr->iColumn==OE_Rollback ||
2359 pExpr->iColumn == OE_Abort ||
2360 pExpr->iColumn == OE_Fail );
danielk19771e536952007-08-16 10:09:01 +00002361 sqlite3DequoteExpr(pParse->db, pExpr);
drh66a51672008-01-03 00:01:23 +00002362 sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn, 0,
drh2646da72005-12-09 20:02:05 +00002363 (char*)pExpr->token.z, pExpr->token.n);
danielk19776f349032002-06-11 02:25:40 +00002364 } else {
drhad6d9462004-09-19 02:15:24 +00002365 assert( pExpr->iColumn == OE_Ignore );
drh66a51672008-01-03 00:01:23 +00002366 sqlite3VdbeAddOp2(v, OP_ContextPop, 0, 0);
2367 sqlite3VdbeAddOp2(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
drhd4e70eb2008-01-02 00:34:36 +00002368 VdbeComment((v, "raise(IGNORE)"));
danielk19776f349032002-06-11 02:25:40 +00002369 }
drhffe07b22005-11-03 00:41:17 +00002370 break;
drh17a7f8d2002-03-24 13:13:27 +00002371 }
danielk19775338a5f2005-01-20 13:03:10 +00002372#endif
drhffe07b22005-11-03 00:41:17 +00002373 }
drh2dcef112008-01-12 19:03:48 +00002374 sqlite3ReleaseTempReg(pParse, regFree1);
2375 sqlite3ReleaseTempReg(pParse, regFree2);
2376 return inReg;
2377}
2378
2379/*
2380** Generate code to evaluate an expression and store the results
2381** into a register. Return the register number where the results
2382** are stored.
2383**
2384** If the register is a temporary register that can be deallocated,
2385** then write its number into *pReg. If the result register is no
2386** a temporary, then set *pReg to zero.
2387*/
2388int sqlite3ExprCodeTemp(Parse *pParse, Expr *pExpr, int *pReg){
2389 int r1 = sqlite3GetTempReg(pParse);
2390 int r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
2391 if( r2==r1 ){
2392 *pReg = r1;
2393 }else{
2394 sqlite3ReleaseTempReg(pParse, r1);
2395 *pReg = 0;
2396 }
2397 return r2;
2398}
2399
2400/*
2401** Generate code that will evaluate expression pExpr and store the
2402** results in register target. The results are guaranteed to appear
2403** in register target.
2404*/
2405int sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){
drh9cbf3422008-01-17 16:22:13 +00002406 int inReg;
2407
2408 assert( target>0 && target<=pParse->nMem );
2409 inReg = sqlite3ExprCodeTarget(pParse, pExpr, target);
drh0e359b32008-01-13 19:02:11 +00002410 assert( pParse->pVdbe || pParse->db->mallocFailed );
2411 if( inReg!=target && pParse->pVdbe ){
drh9cbf3422008-01-17 16:22:13 +00002412 sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, inReg, target);
drhcce7d172000-05-31 15:34:51 +00002413 }
drh389a1ad2008-01-03 23:44:53 +00002414 return target;
drhcce7d172000-05-31 15:34:51 +00002415}
2416
2417/*
drh2dcef112008-01-12 19:03:48 +00002418** Generate code that evalutes the given expression and puts the result
drhde4fcfd2008-01-19 23:50:26 +00002419** in register target.
drh25303782004-12-07 15:41:48 +00002420**
drh2dcef112008-01-12 19:03:48 +00002421** Also make a copy of the expression results into another "cache" register
2422** and modify the expression so that the next time it is evaluated,
2423** the result is a copy of the cache register.
2424**
2425** This routine is used for expressions that are used multiple
2426** times. They are evaluated once and the results of the expression
2427** are reused.
drh25303782004-12-07 15:41:48 +00002428*/
drh2dcef112008-01-12 19:03:48 +00002429int sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr, int target){
drh25303782004-12-07 15:41:48 +00002430 Vdbe *v = pParse->pVdbe;
drh2dcef112008-01-12 19:03:48 +00002431 int inReg;
2432 inReg = sqlite3ExprCode(pParse, pExpr, target);
drhde4fcfd2008-01-19 23:50:26 +00002433 assert( target>0 );
drh2dcef112008-01-12 19:03:48 +00002434 if( pExpr->op!=TK_REGISTER ){
2435 int iMem;
drhde4fcfd2008-01-19 23:50:26 +00002436 iMem = ++pParse->nMem;
2437 sqlite3VdbeAddOp2(v, OP_Copy, inReg, iMem);
drh2dcef112008-01-12 19:03:48 +00002438 pExpr->iTable = iMem;
drh25303782004-12-07 15:41:48 +00002439 pExpr->op = TK_REGISTER;
2440 }
drh2dcef112008-01-12 19:03:48 +00002441 return inReg;
drh25303782004-12-07 15:41:48 +00002442}
drh2dcef112008-01-12 19:03:48 +00002443
drh25303782004-12-07 15:41:48 +00002444
2445/*
drh268380c2004-02-25 13:47:31 +00002446** Generate code that pushes the value of every element of the given
drh9cbf3422008-01-17 16:22:13 +00002447** expression list into a sequence of registers beginning at target.
drh268380c2004-02-25 13:47:31 +00002448**
drh892d3172008-01-10 03:46:36 +00002449** Return the number of elements evaluated.
drh268380c2004-02-25 13:47:31 +00002450*/
danielk19774adee202004-05-08 08:23:19 +00002451int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00002452 Parse *pParse, /* Parsing context */
drh389a1ad2008-01-03 23:44:53 +00002453 ExprList *pList, /* The expression list to be coded */
2454 int target /* Where to write results */
drh268380c2004-02-25 13:47:31 +00002455){
2456 struct ExprList_item *pItem;
drh9cbf3422008-01-17 16:22:13 +00002457 int i, n;
drh892d3172008-01-10 03:46:36 +00002458 assert( pList!=0 || pParse->db->mallocFailed );
2459 if( pList==0 ){
2460 return 0;
2461 }
drh9cbf3422008-01-17 16:22:13 +00002462 assert( target>0 );
drh268380c2004-02-25 13:47:31 +00002463 n = pList->nExpr;
drhc182d162005-08-14 20:47:16 +00002464 for(pItem=pList->a, i=n; i>0; i--, pItem++){
drh389a1ad2008-01-03 23:44:53 +00002465 sqlite3ExprCode(pParse, pItem->pExpr, target);
drh9cbf3422008-01-17 16:22:13 +00002466 target++;
drh268380c2004-02-25 13:47:31 +00002467 }
drhf9b596e2004-05-26 16:54:42 +00002468 return n;
drh268380c2004-02-25 13:47:31 +00002469}
2470
2471/*
drhcce7d172000-05-31 15:34:51 +00002472** Generate code for a boolean expression such that a jump is made
2473** to the label "dest" if the expression is true but execution
2474** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00002475**
2476** If the expression evaluates to NULL (neither true nor false), then
drh35573352008-01-08 23:54:25 +00002477** take the jump if the jumpIfNull flag is SQLITE_JUMPIFNULL.
drhf2bc0132004-10-04 13:19:23 +00002478**
2479** This code depends on the fact that certain token values (ex: TK_EQ)
2480** are the same as opcode values (ex: OP_Eq) that implement the corresponding
2481** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
2482** the make process cause these values to align. Assert()s in the code
2483** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00002484*/
danielk19774adee202004-05-08 08:23:19 +00002485void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00002486 Vdbe *v = pParse->pVdbe;
2487 int op = 0;
drh2dcef112008-01-12 19:03:48 +00002488 int regFree1 = 0;
2489 int regFree2 = 0;
2490 int r1, r2;
2491
drh35573352008-01-08 23:54:25 +00002492 assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
drhdaffd0e2001-04-11 14:28:42 +00002493 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00002494 op = pExpr->op;
2495 switch( op ){
drhcce7d172000-05-31 15:34:51 +00002496 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00002497 int d2 = sqlite3VdbeMakeLabel(v);
drh35573352008-01-08 23:54:25 +00002498 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,jumpIfNull^SQLITE_JUMPIFNULL);
danielk19774adee202004-05-08 08:23:19 +00002499 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
2500 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00002501 break;
2502 }
2503 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00002504 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
2505 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002506 break;
2507 }
2508 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00002509 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002510 break;
2511 }
2512 case TK_LT:
2513 case TK_LE:
2514 case TK_GT:
2515 case TK_GE:
2516 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00002517 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00002518 assert( TK_LT==OP_Lt );
2519 assert( TK_LE==OP_Le );
2520 assert( TK_GT==OP_Gt );
2521 assert( TK_GE==OP_Ge );
2522 assert( TK_EQ==OP_Eq );
2523 assert( TK_NE==OP_Ne );
drh2dcef112008-01-12 19:03:48 +00002524 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
2525 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
drh35573352008-01-08 23:54:25 +00002526 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
drh2dcef112008-01-12 19:03:48 +00002527 r1, r2, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002528 break;
2529 }
2530 case TK_ISNULL:
2531 case TK_NOTNULL: {
drhf2bc0132004-10-04 13:19:23 +00002532 assert( TK_ISNULL==OP_IsNull );
2533 assert( TK_NOTNULL==OP_NotNull );
drh2dcef112008-01-12 19:03:48 +00002534 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
2535 sqlite3VdbeAddOp2(v, op, r1, dest);
drhcce7d172000-05-31 15:34:51 +00002536 break;
2537 }
drhfef52082000-06-06 01:50:43 +00002538 case TK_BETWEEN: {
drh2dcef112008-01-12 19:03:48 +00002539 /* x BETWEEN y AND z
danielk19770202b292004-06-09 09:55:16 +00002540 **
drh2dcef112008-01-12 19:03:48 +00002541 ** Is equivalent to
2542 **
2543 ** x>=y AND x<=z
2544 **
2545 ** Code it as such, taking care to do the common subexpression
2546 ** elementation of x.
danielk19770202b292004-06-09 09:55:16 +00002547 */
drh2dcef112008-01-12 19:03:48 +00002548 Expr exprAnd;
2549 Expr compLeft;
2550 Expr compRight;
2551 Expr exprX;
danielk19770202b292004-06-09 09:55:16 +00002552
drh2dcef112008-01-12 19:03:48 +00002553 exprX = *pExpr->pLeft;
2554 exprAnd.op = TK_AND;
2555 exprAnd.pLeft = &compLeft;
2556 exprAnd.pRight = &compRight;
2557 compLeft.op = TK_GE;
2558 compLeft.pLeft = &exprX;
2559 compLeft.pRight = pExpr->pList->a[0].pExpr;
2560 compRight.op = TK_LE;
2561 compRight.pLeft = &exprX;
2562 compRight.pRight = pExpr->pList->a[1].pExpr;
2563 exprX.iTable = sqlite3ExprCodeTemp(pParse, &exprX, &regFree1);
2564 exprX.op = TK_REGISTER;
2565 sqlite3ExprIfTrue(pParse, &exprAnd, dest, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00002566 break;
2567 }
drhcce7d172000-05-31 15:34:51 +00002568 default: {
drh2dcef112008-01-12 19:03:48 +00002569 r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
2570 sqlite3VdbeAddOp3(v, OP_If, r1, dest, jumpIfNull!=0);
drhcce7d172000-05-31 15:34:51 +00002571 break;
2572 }
2573 }
drh2dcef112008-01-12 19:03:48 +00002574 sqlite3ReleaseTempReg(pParse, regFree1);
2575 sqlite3ReleaseTempReg(pParse, regFree2);
drhcce7d172000-05-31 15:34:51 +00002576}
2577
2578/*
drh66b89c82000-11-28 20:47:17 +00002579** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00002580** to the label "dest" if the expression is false but execution
2581** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00002582**
2583** If the expression evaluates to NULL (neither true nor false) then
drh35573352008-01-08 23:54:25 +00002584** jump if jumpIfNull is SQLITE_JUMPIFNULL or fall through if jumpIfNull
2585** is 0.
drhcce7d172000-05-31 15:34:51 +00002586*/
danielk19774adee202004-05-08 08:23:19 +00002587void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00002588 Vdbe *v = pParse->pVdbe;
2589 int op = 0;
drh2dcef112008-01-12 19:03:48 +00002590 int regFree1 = 0;
2591 int regFree2 = 0;
2592 int r1, r2;
2593
drh35573352008-01-08 23:54:25 +00002594 assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
drhdaffd0e2001-04-11 14:28:42 +00002595 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00002596
2597 /* The value of pExpr->op and op are related as follows:
2598 **
2599 ** pExpr->op op
2600 ** --------- ----------
2601 ** TK_ISNULL OP_NotNull
2602 ** TK_NOTNULL OP_IsNull
2603 ** TK_NE OP_Eq
2604 ** TK_EQ OP_Ne
2605 ** TK_GT OP_Le
2606 ** TK_LE OP_Gt
2607 ** TK_GE OP_Lt
2608 ** TK_LT OP_Ge
2609 **
2610 ** For other values of pExpr->op, op is undefined and unused.
2611 ** The value of TK_ and OP_ constants are arranged such that we
2612 ** can compute the mapping above using the following expression.
2613 ** Assert()s verify that the computation is correct.
2614 */
2615 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
2616
2617 /* Verify correct alignment of TK_ and OP_ constants
2618 */
2619 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
2620 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
2621 assert( pExpr->op!=TK_NE || op==OP_Eq );
2622 assert( pExpr->op!=TK_EQ || op==OP_Ne );
2623 assert( pExpr->op!=TK_LT || op==OP_Ge );
2624 assert( pExpr->op!=TK_LE || op==OP_Gt );
2625 assert( pExpr->op!=TK_GT || op==OP_Le );
2626 assert( pExpr->op!=TK_GE || op==OP_Lt );
2627
drhcce7d172000-05-31 15:34:51 +00002628 switch( pExpr->op ){
2629 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00002630 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
2631 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002632 break;
2633 }
2634 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00002635 int d2 = sqlite3VdbeMakeLabel(v);
drh35573352008-01-08 23:54:25 +00002636 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, jumpIfNull^SQLITE_JUMPIFNULL);
danielk19774adee202004-05-08 08:23:19 +00002637 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
2638 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00002639 break;
2640 }
2641 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00002642 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002643 break;
2644 }
2645 case TK_LT:
2646 case TK_LE:
2647 case TK_GT:
2648 case TK_GE:
2649 case TK_NE:
2650 case TK_EQ: {
drh2dcef112008-01-12 19:03:48 +00002651 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
2652 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
drh35573352008-01-08 23:54:25 +00002653 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
drh2dcef112008-01-12 19:03:48 +00002654 r1, r2, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002655 break;
2656 }
drhcce7d172000-05-31 15:34:51 +00002657 case TK_ISNULL:
2658 case TK_NOTNULL: {
drh2dcef112008-01-12 19:03:48 +00002659 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
2660 sqlite3VdbeAddOp2(v, op, r1, dest);
drhcce7d172000-05-31 15:34:51 +00002661 break;
2662 }
drhfef52082000-06-06 01:50:43 +00002663 case TK_BETWEEN: {
drh2dcef112008-01-12 19:03:48 +00002664 /* x BETWEEN y AND z
danielk19770202b292004-06-09 09:55:16 +00002665 **
drh2dcef112008-01-12 19:03:48 +00002666 ** Is equivalent to
2667 **
2668 ** x>=y AND x<=z
2669 **
2670 ** Code it as such, taking care to do the common subexpression
2671 ** elementation of x.
danielk19770202b292004-06-09 09:55:16 +00002672 */
drh2dcef112008-01-12 19:03:48 +00002673 Expr exprAnd;
2674 Expr compLeft;
2675 Expr compRight;
2676 Expr exprX;
drhbe5c89a2004-07-26 00:31:09 +00002677
drh2dcef112008-01-12 19:03:48 +00002678 exprX = *pExpr->pLeft;
2679 exprAnd.op = TK_AND;
2680 exprAnd.pLeft = &compLeft;
2681 exprAnd.pRight = &compRight;
2682 compLeft.op = TK_GE;
2683 compLeft.pLeft = &exprX;
2684 compLeft.pRight = pExpr->pList->a[0].pExpr;
2685 compRight.op = TK_LE;
2686 compRight.pLeft = &exprX;
2687 compRight.pRight = pExpr->pList->a[1].pExpr;
2688 exprX.iTable = sqlite3ExprCodeTemp(pParse, &exprX, &regFree1);
2689 exprX.op = TK_REGISTER;
2690 sqlite3ExprIfFalse(pParse, &exprAnd, dest, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00002691 break;
2692 }
drhcce7d172000-05-31 15:34:51 +00002693 default: {
drh2dcef112008-01-12 19:03:48 +00002694 r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
2695 sqlite3VdbeAddOp3(v, OP_IfNot, r1, dest, jumpIfNull!=0);
drhcce7d172000-05-31 15:34:51 +00002696 break;
2697 }
2698 }
drh2dcef112008-01-12 19:03:48 +00002699 sqlite3ReleaseTempReg(pParse, regFree1);
2700 sqlite3ReleaseTempReg(pParse, regFree2);
drhcce7d172000-05-31 15:34:51 +00002701}
drh22827922000-06-06 17:27:05 +00002702
2703/*
2704** Do a deep comparison of two expression trees. Return TRUE (non-zero)
2705** if they are identical and return FALSE if they differ in any way.
drhd40aab02007-02-24 15:29:03 +00002706**
2707** Sometimes this routine will return FALSE even if the two expressions
2708** really are equivalent. If we cannot prove that the expressions are
2709** identical, we return FALSE just to be safe. So if this routine
2710** returns false, then you do not really know for certain if the two
2711** expressions are the same. But if you get a TRUE return, then you
2712** can be sure the expressions are the same. In the places where
2713** this routine is used, it does not hurt to get an extra FALSE - that
2714** just might result in some slightly slower code. But returning
2715** an incorrect TRUE could lead to a malfunction.
drh22827922000-06-06 17:27:05 +00002716*/
danielk19774adee202004-05-08 08:23:19 +00002717int sqlite3ExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00002718 int i;
danielk19774b202ae2006-01-23 05:50:58 +00002719 if( pA==0||pB==0 ){
2720 return pB==pA;
drh22827922000-06-06 17:27:05 +00002721 }
2722 if( pA->op!=pB->op ) return 0;
drhfd357972005-09-09 01:33:19 +00002723 if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 0;
danielk19774adee202004-05-08 08:23:19 +00002724 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
2725 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00002726 if( pA->pList ){
2727 if( pB->pList==0 ) return 0;
2728 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
2729 for(i=0; i<pA->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00002730 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00002731 return 0;
2732 }
2733 }
2734 }else if( pB->pList ){
2735 return 0;
2736 }
2737 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00002738 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drhdd735212007-02-24 13:53:05 +00002739 if( pA->op!=TK_COLUMN && pA->token.z ){
drh22827922000-06-06 17:27:05 +00002740 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00002741 if( pB->token.n!=pA->token.n ) return 0;
drh2646da72005-12-09 20:02:05 +00002742 if( sqlite3StrNICmp((char*)pA->token.z,(char*)pB->token.z,pB->token.n)!=0 ){
2743 return 0;
2744 }
drh22827922000-06-06 17:27:05 +00002745 }
2746 return 1;
2747}
2748
drh13449892005-09-07 21:22:45 +00002749
drh22827922000-06-06 17:27:05 +00002750/*
drh13449892005-09-07 21:22:45 +00002751** Add a new element to the pAggInfo->aCol[] array. Return the index of
2752** the new element. Return a negative number if malloc fails.
drh22827922000-06-06 17:27:05 +00002753*/
drh17435752007-08-16 04:30:38 +00002754static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){
drh13449892005-09-07 21:22:45 +00002755 int i;
drhcf643722007-03-27 13:36:37 +00002756 pInfo->aCol = sqlite3ArrayAllocate(
drh17435752007-08-16 04:30:38 +00002757 db,
drhcf643722007-03-27 13:36:37 +00002758 pInfo->aCol,
2759 sizeof(pInfo->aCol[0]),
2760 3,
2761 &pInfo->nColumn,
2762 &pInfo->nColumnAlloc,
2763 &i
2764 );
drh13449892005-09-07 21:22:45 +00002765 return i;
2766}
2767
2768/*
2769** Add a new element to the pAggInfo->aFunc[] array. Return the index of
2770** the new element. Return a negative number if malloc fails.
2771*/
drh17435752007-08-16 04:30:38 +00002772static int addAggInfoFunc(sqlite3 *db, AggInfo *pInfo){
drh13449892005-09-07 21:22:45 +00002773 int i;
drhcf643722007-03-27 13:36:37 +00002774 pInfo->aFunc = sqlite3ArrayAllocate(
drh17435752007-08-16 04:30:38 +00002775 db,
drhcf643722007-03-27 13:36:37 +00002776 pInfo->aFunc,
2777 sizeof(pInfo->aFunc[0]),
2778 3,
2779 &pInfo->nFunc,
2780 &pInfo->nFuncAlloc,
2781 &i
2782 );
drh13449892005-09-07 21:22:45 +00002783 return i;
2784}
drh22827922000-06-06 17:27:05 +00002785
2786/*
drh626a8792005-01-17 22:08:19 +00002787** This is an xFunc for walkExprTree() used to implement
2788** sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates
2789** for additional information.
drh22827922000-06-06 17:27:05 +00002790**
drh626a8792005-01-17 22:08:19 +00002791** This routine analyzes the aggregate function at pExpr.
drh22827922000-06-06 17:27:05 +00002792*/
drh626a8792005-01-17 22:08:19 +00002793static int analyzeAggregate(void *pArg, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00002794 int i;
danielk1977a58fdfb2005-02-08 07:50:40 +00002795 NameContext *pNC = (NameContext *)pArg;
2796 Parse *pParse = pNC->pParse;
2797 SrcList *pSrcList = pNC->pSrcList;
drh13449892005-09-07 21:22:45 +00002798 AggInfo *pAggInfo = pNC->pAggInfo;
drh22827922000-06-06 17:27:05 +00002799
drh22827922000-06-06 17:27:05 +00002800 switch( pExpr->op ){
drh89c69d02007-01-04 01:20:28 +00002801 case TK_AGG_COLUMN:
drh967e8b72000-06-21 13:59:10 +00002802 case TK_COLUMN: {
drh13449892005-09-07 21:22:45 +00002803 /* Check to see if the column is in one of the tables in the FROM
2804 ** clause of the aggregate query */
2805 if( pSrcList ){
2806 struct SrcList_item *pItem = pSrcList->a;
2807 for(i=0; i<pSrcList->nSrc; i++, pItem++){
2808 struct AggInfo_col *pCol;
2809 if( pExpr->iTable==pItem->iCursor ){
2810 /* If we reach this point, it means that pExpr refers to a table
2811 ** that is in the FROM clause of the aggregate query.
2812 **
2813 ** Make an entry for the column in pAggInfo->aCol[] if there
2814 ** is not an entry there already.
2815 */
drh7f906d62007-03-12 23:48:52 +00002816 int k;
drh13449892005-09-07 21:22:45 +00002817 pCol = pAggInfo->aCol;
drh7f906d62007-03-12 23:48:52 +00002818 for(k=0; k<pAggInfo->nColumn; k++, pCol++){
drh13449892005-09-07 21:22:45 +00002819 if( pCol->iTable==pExpr->iTable &&
2820 pCol->iColumn==pExpr->iColumn ){
2821 break;
2822 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002823 }
danielk19771e536952007-08-16 10:09:01 +00002824 if( (k>=pAggInfo->nColumn)
2825 && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0
2826 ){
drh7f906d62007-03-12 23:48:52 +00002827 pCol = &pAggInfo->aCol[k];
danielk19770817d0d2007-02-14 09:19:36 +00002828 pCol->pTab = pExpr->pTab;
drh13449892005-09-07 21:22:45 +00002829 pCol->iTable = pExpr->iTable;
2830 pCol->iColumn = pExpr->iColumn;
drh0a07c102008-01-03 18:03:08 +00002831 pCol->iMem = ++pParse->nMem;
drh13449892005-09-07 21:22:45 +00002832 pCol->iSorterColumn = -1;
drh5774b802005-09-07 22:48:16 +00002833 pCol->pExpr = pExpr;
drh13449892005-09-07 21:22:45 +00002834 if( pAggInfo->pGroupBy ){
2835 int j, n;
2836 ExprList *pGB = pAggInfo->pGroupBy;
2837 struct ExprList_item *pTerm = pGB->a;
2838 n = pGB->nExpr;
2839 for(j=0; j<n; j++, pTerm++){
2840 Expr *pE = pTerm->pExpr;
2841 if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable &&
2842 pE->iColumn==pExpr->iColumn ){
2843 pCol->iSorterColumn = j;
2844 break;
2845 }
2846 }
2847 }
2848 if( pCol->iSorterColumn<0 ){
2849 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
2850 }
2851 }
2852 /* There is now an entry for pExpr in pAggInfo->aCol[] (either
2853 ** because it was there before or because we just created it).
2854 ** Convert the pExpr to be a TK_AGG_COLUMN referring to that
2855 ** pAggInfo->aCol[] entry.
2856 */
2857 pExpr->pAggInfo = pAggInfo;
2858 pExpr->op = TK_AGG_COLUMN;
drh7f906d62007-03-12 23:48:52 +00002859 pExpr->iAgg = k;
drh13449892005-09-07 21:22:45 +00002860 break;
2861 } /* endif pExpr->iTable==pItem->iCursor */
2862 } /* end loop over pSrcList */
drh22827922000-06-06 17:27:05 +00002863 }
drh626a8792005-01-17 22:08:19 +00002864 return 1;
drh22827922000-06-06 17:27:05 +00002865 }
2866 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00002867 /* The pNC->nDepth==0 test causes aggregate functions in subqueries
2868 ** to be ignored */
danielk1977a58fdfb2005-02-08 07:50:40 +00002869 if( pNC->nDepth==0 ){
drh13449892005-09-07 21:22:45 +00002870 /* Check to see if pExpr is a duplicate of another aggregate
2871 ** function that is already in the pAggInfo structure
2872 */
2873 struct AggInfo_func *pItem = pAggInfo->aFunc;
2874 for(i=0; i<pAggInfo->nFunc; i++, pItem++){
2875 if( sqlite3ExprCompare(pItem->pExpr, pExpr) ){
danielk1977a58fdfb2005-02-08 07:50:40 +00002876 break;
2877 }
drh22827922000-06-06 17:27:05 +00002878 }
drh13449892005-09-07 21:22:45 +00002879 if( i>=pAggInfo->nFunc ){
2880 /* pExpr is original. Make a new entry in pAggInfo->aFunc[]
2881 */
danielk197714db2662006-01-09 16:12:04 +00002882 u8 enc = ENC(pParse->db);
danielk19771e536952007-08-16 10:09:01 +00002883 i = addAggInfoFunc(pParse->db, pAggInfo);
drh13449892005-09-07 21:22:45 +00002884 if( i>=0 ){
2885 pItem = &pAggInfo->aFunc[i];
2886 pItem->pExpr = pExpr;
drh0a07c102008-01-03 18:03:08 +00002887 pItem->iMem = ++pParse->nMem;
drh13449892005-09-07 21:22:45 +00002888 pItem->pFunc = sqlite3FindFunction(pParse->db,
drh2646da72005-12-09 20:02:05 +00002889 (char*)pExpr->token.z, pExpr->token.n,
drh13449892005-09-07 21:22:45 +00002890 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
drhfd357972005-09-09 01:33:19 +00002891 if( pExpr->flags & EP_Distinct ){
2892 pItem->iDistinct = pParse->nTab++;
2893 }else{
2894 pItem->iDistinct = -1;
2895 }
drh13449892005-09-07 21:22:45 +00002896 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002897 }
drh13449892005-09-07 21:22:45 +00002898 /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
2899 */
danielk1977a58fdfb2005-02-08 07:50:40 +00002900 pExpr->iAgg = i;
drh13449892005-09-07 21:22:45 +00002901 pExpr->pAggInfo = pAggInfo;
danielk1977a58fdfb2005-02-08 07:50:40 +00002902 return 1;
drh22827922000-06-06 17:27:05 +00002903 }
drh22827922000-06-06 17:27:05 +00002904 }
2905 }
drh13449892005-09-07 21:22:45 +00002906
2907 /* Recursively walk subqueries looking for TK_COLUMN nodes that need
2908 ** to be changed to TK_AGG_COLUMN. But increment nDepth so that
2909 ** TK_AGG_FUNCTION nodes in subqueries will be unchanged.
2910 */
danielk1977a58fdfb2005-02-08 07:50:40 +00002911 if( pExpr->pSelect ){
2912 pNC->nDepth++;
2913 walkSelectExpr(pExpr->pSelect, analyzeAggregate, pNC);
2914 pNC->nDepth--;
2915 }
drh626a8792005-01-17 22:08:19 +00002916 return 0;
2917}
2918
2919/*
2920** Analyze the given expression looking for aggregate functions and
2921** for variables that need to be added to the pParse->aAgg[] array.
2922** Make additional entries to the pParse->aAgg[] array as necessary.
2923**
2924** This routine should only be called after the expression has been
2925** analyzed by sqlite3ExprResolveNames().
drh626a8792005-01-17 22:08:19 +00002926*/
drhd2b3e232008-01-23 14:51:49 +00002927void sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
danielk1977a58fdfb2005-02-08 07:50:40 +00002928 walkExprTree(pExpr, analyzeAggregate, pNC);
drh22827922000-06-06 17:27:05 +00002929}
drh5d9a4af2005-08-30 00:54:01 +00002930
2931/*
2932** Call sqlite3ExprAnalyzeAggregates() for every expression in an
2933** expression list. Return the number of errors.
2934**
2935** If an error is found, the analysis is cut short.
2936*/
drhd2b3e232008-01-23 14:51:49 +00002937void sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){
drh5d9a4af2005-08-30 00:54:01 +00002938 struct ExprList_item *pItem;
2939 int i;
drh5d9a4af2005-08-30 00:54:01 +00002940 if( pList ){
drhd2b3e232008-01-23 14:51:49 +00002941 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
2942 sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
drh5d9a4af2005-08-30 00:54:01 +00002943 }
2944 }
drh5d9a4af2005-08-30 00:54:01 +00002945}
drh892d3172008-01-10 03:46:36 +00002946
2947/*
2948** Allocate or deallocate temporary use registers during code generation.
2949*/
2950int sqlite3GetTempReg(Parse *pParse){
2951 if( pParse->nTempReg ){
2952 return pParse->aTempReg[--pParse->nTempReg];
2953 }else{
2954 return ++pParse->nMem;
2955 }
2956}
2957void sqlite3ReleaseTempReg(Parse *pParse, int iReg){
drh2dcef112008-01-12 19:03:48 +00002958 if( iReg && pParse->nTempReg<ArraySize(pParse->aTempReg) ){
2959 assert( iReg>0 );
drh892d3172008-01-10 03:46:36 +00002960 pParse->aTempReg[pParse->nTempReg++] = iReg;
2961 }
2962}
2963
2964/*
2965** Allocate or deallocate a block of nReg consecutive registers
2966*/
2967int sqlite3GetTempRange(Parse *pParse, int nReg){
2968 int i;
2969 if( nReg<=pParse->nRangeReg ){
2970 i = pParse->iRangeReg;
2971 pParse->iRangeReg += nReg;
2972 pParse->nRangeReg -= nReg;
2973 }else{
2974 i = pParse->nMem+1;
2975 pParse->nMem += nReg;
2976 }
2977 return i;
2978}
2979void sqlite3ReleaseTempRange(Parse *pParse, int iReg, int nReg){
2980 if( nReg>pParse->nRangeReg ){
2981 pParse->nRangeReg = nReg;
2982 pParse->iRangeReg = iReg;
2983 }
2984}