blob: ec9f7d554a09600f1b9d16f9e7f7b3df8b4e6d60 [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**
drhb3843a82008-04-01 12:24:11 +000015** $Id: expr.c,v 1.363 2008/04/01 12:24:11 drh Exp $
drhcce7d172000-05-31 15:34:51 +000016*/
17#include "sqliteInt.h"
drh04738cb2002-06-02 18:19:00 +000018#include <ctype.h>
drha2e00042002-01-22 03:13:42 +000019
danielk1977e014a832004-05-17 10:48:57 +000020/*
21** Return the 'affinity' of the expression pExpr if any.
22**
23** If pExpr is a column, a reference to a column via an 'AS' alias,
24** or a sub-select with a column as the return value, then the
25** affinity of that column is returned. Otherwise, 0x00 is returned,
26** indicating no affinity for the expression.
27**
28** i.e. the WHERE clause expresssions in the following statements all
29** have an affinity:
30**
31** CREATE TABLE t1(a);
32** SELECT * FROM t1 WHERE a;
33** SELECT a AS b FROM t1 WHERE b;
34** SELECT * FROM t1 WHERE (select a from t1);
35*/
danielk1977bf3b7212004-05-18 10:06:24 +000036char sqlite3ExprAffinity(Expr *pExpr){
drh487e2622005-06-25 18:42:14 +000037 int op = pExpr->op;
drh487e2622005-06-25 18:42:14 +000038 if( op==TK_SELECT ){
danielk1977bf3b7212004-05-18 10:06:24 +000039 return sqlite3ExprAffinity(pExpr->pSelect->pEList->a[0].pExpr);
danielk1977a37cdde2004-05-16 11:15:36 +000040 }
drh487e2622005-06-25 18:42:14 +000041#ifndef SQLITE_OMIT_CAST
42 if( op==TK_CAST ){
drh8a512562005-11-14 22:29:05 +000043 return sqlite3AffinityType(&pExpr->token);
drh487e2622005-06-25 18:42:14 +000044 }
45#endif
danielk1977a37cdde2004-05-16 11:15:36 +000046 return pExpr->affinity;
47}
48
drh53db1452004-05-20 13:54:53 +000049/*
drh8b4c40d2007-02-01 23:02:45 +000050** Set the collating sequence for expression pExpr to be the collating
51** sequence named by pToken. Return a pointer to the revised expression.
drha34001c2007-02-02 12:44:37 +000052** The collating sequence is marked as "explicit" using the EP_ExpCollate
53** flag. An explicit collating sequence will override implicit
54** collating sequences.
drh8b4c40d2007-02-01 23:02:45 +000055*/
56Expr *sqlite3ExprSetColl(Parse *pParse, Expr *pExpr, Token *pName){
danielk197739002502007-11-12 09:50:26 +000057 char *zColl = 0; /* Dequoted name of collation sequence */
drh8b4c40d2007-02-01 23:02:45 +000058 CollSeq *pColl;
danielk197739002502007-11-12 09:50:26 +000059 zColl = sqlite3NameFromToken(pParse->db, pName);
60 if( pExpr && zColl ){
61 pColl = sqlite3LocateCollSeq(pParse, zColl, -1);
62 if( pColl ){
63 pExpr->pColl = pColl;
64 pExpr->flags |= EP_ExpCollate;
65 }
drh8b4c40d2007-02-01 23:02:45 +000066 }
danielk197739002502007-11-12 09:50:26 +000067 sqlite3_free(zColl);
drh8b4c40d2007-02-01 23:02:45 +000068 return pExpr;
69}
70
71/*
danielk19770202b292004-06-09 09:55:16 +000072** Return the default collation sequence for the expression pExpr. If
73** there is no default collation type, return 0.
74*/
danielk19777cedc8d2004-06-10 10:50:08 +000075CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
76 CollSeq *pColl = 0;
danielk19770202b292004-06-09 09:55:16 +000077 if( pExpr ){
drh7e09fe02007-06-20 16:13:23 +000078 int op;
danielk19777cedc8d2004-06-10 10:50:08 +000079 pColl = pExpr->pColl;
drh7e09fe02007-06-20 16:13:23 +000080 op = pExpr->op;
81 if( (op==TK_CAST || op==TK_UPLUS) && !pColl ){
danielk19777cedc8d2004-06-10 10:50:08 +000082 return sqlite3ExprCollSeq(pParse, pExpr->pLeft);
danielk19770202b292004-06-09 09:55:16 +000083 }
84 }
danielk19777cedc8d2004-06-10 10:50:08 +000085 if( sqlite3CheckCollSeq(pParse, pColl) ){
86 pColl = 0;
87 }
88 return pColl;
danielk19770202b292004-06-09 09:55:16 +000089}
90
91/*
drh626a8792005-01-17 22:08:19 +000092** pExpr is an operand of a comparison operator. aff2 is the
93** type affinity of the other operand. This routine returns the
drh53db1452004-05-20 13:54:53 +000094** type affinity that should be used for the comparison operator.
95*/
danielk1977e014a832004-05-17 10:48:57 +000096char sqlite3CompareAffinity(Expr *pExpr, char aff2){
danielk1977bf3b7212004-05-18 10:06:24 +000097 char aff1 = sqlite3ExprAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +000098 if( aff1 && aff2 ){
drh8df447f2005-11-01 15:48:24 +000099 /* Both sides of the comparison are columns. If one has numeric
100 ** affinity, use that. Otherwise use no affinity.
danielk1977e014a832004-05-17 10:48:57 +0000101 */
drh8a512562005-11-14 22:29:05 +0000102 if( sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2) ){
danielk1977e014a832004-05-17 10:48:57 +0000103 return SQLITE_AFF_NUMERIC;
104 }else{
105 return SQLITE_AFF_NONE;
106 }
107 }else if( !aff1 && !aff2 ){
drh5f6a87b2004-07-19 00:39:45 +0000108 /* Neither side of the comparison is a column. Compare the
109 ** results directly.
danielk1977e014a832004-05-17 10:48:57 +0000110 */
drh5f6a87b2004-07-19 00:39:45 +0000111 return SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +0000112 }else{
113 /* One side is a column, the other is not. Use the columns affinity. */
drhfe05af82005-07-21 03:14:59 +0000114 assert( aff1==0 || aff2==0 );
danielk1977e014a832004-05-17 10:48:57 +0000115 return (aff1 + aff2);
116 }
117}
118
drh53db1452004-05-20 13:54:53 +0000119/*
120** pExpr is a comparison operator. Return the type affinity that should
121** be applied to both operands prior to doing the comparison.
122*/
danielk1977e014a832004-05-17 10:48:57 +0000123static char comparisonAffinity(Expr *pExpr){
124 char aff;
125 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
126 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
127 pExpr->op==TK_NE );
128 assert( pExpr->pLeft );
danielk1977bf3b7212004-05-18 10:06:24 +0000129 aff = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +0000130 if( pExpr->pRight ){
131 aff = sqlite3CompareAffinity(pExpr->pRight, aff);
132 }
133 else if( pExpr->pSelect ){
134 aff = sqlite3CompareAffinity(pExpr->pSelect->pEList->a[0].pExpr, aff);
135 }
136 else if( !aff ){
drhde087bd2007-02-23 03:00:44 +0000137 aff = SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +0000138 }
139 return aff;
140}
141
142/*
143** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
144** idx_affinity is the affinity of an indexed column. Return true
145** if the index with affinity idx_affinity may be used to implement
146** the comparison in pExpr.
147*/
148int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){
149 char aff = comparisonAffinity(pExpr);
drh8a512562005-11-14 22:29:05 +0000150 switch( aff ){
151 case SQLITE_AFF_NONE:
152 return 1;
153 case SQLITE_AFF_TEXT:
154 return idx_affinity==SQLITE_AFF_TEXT;
155 default:
156 return sqlite3IsNumericAffinity(idx_affinity);
157 }
danielk1977e014a832004-05-17 10:48:57 +0000158}
159
danielk1977a37cdde2004-05-16 11:15:36 +0000160/*
drh35573352008-01-08 23:54:25 +0000161** Return the P5 value that should be used for a binary comparison
danielk1977a37cdde2004-05-16 11:15:36 +0000162** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
danielk1977a37cdde2004-05-16 11:15:36 +0000163*/
drh35573352008-01-08 23:54:25 +0000164static u8 binaryCompareP5(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){
165 u8 aff = (char)sqlite3ExprAffinity(pExpr2);
166 aff = sqlite3CompareAffinity(pExpr1, aff) | jumpIfNull;
167 return aff;
danielk1977a37cdde2004-05-16 11:15:36 +0000168}
169
drha2e00042002-01-22 03:13:42 +0000170/*
danielk19770202b292004-06-09 09:55:16 +0000171** Return a pointer to the collation sequence that should be used by
172** a binary comparison operator comparing pLeft and pRight.
173**
174** If the left hand expression has a collating sequence type, then it is
175** used. Otherwise the collation sequence for the right hand expression
176** is used, or the default (BINARY) if neither expression has a collating
177** type.
danielk1977bcbb04e2007-05-29 12:11:29 +0000178**
179** Argument pRight (but not pLeft) may be a null pointer. In this case,
180** it is not considered.
danielk19770202b292004-06-09 09:55:16 +0000181*/
drh0a0e1312007-08-07 17:04:59 +0000182CollSeq *sqlite3BinaryCompareCollSeq(
danielk1977bcbb04e2007-05-29 12:11:29 +0000183 Parse *pParse,
184 Expr *pLeft,
185 Expr *pRight
186){
drhec41dda2007-02-07 13:09:45 +0000187 CollSeq *pColl;
188 assert( pLeft );
drhec41dda2007-02-07 13:09:45 +0000189 if( pLeft->flags & EP_ExpCollate ){
190 assert( pLeft->pColl );
191 pColl = pLeft->pColl;
danielk1977bcbb04e2007-05-29 12:11:29 +0000192 }else if( pRight && pRight->flags & EP_ExpCollate ){
drhec41dda2007-02-07 13:09:45 +0000193 assert( pRight->pColl );
194 pColl = pRight->pColl;
195 }else{
196 pColl = sqlite3ExprCollSeq(pParse, pLeft);
197 if( !pColl ){
198 pColl = sqlite3ExprCollSeq(pParse, pRight);
199 }
danielk19770202b292004-06-09 09:55:16 +0000200 }
201 return pColl;
202}
203
204/*
drhda250ea2008-04-01 05:07:14 +0000205** Generate the operands for a comparison operation. Before
206** generating the code for each operand, set the EP_AnyAff
207** flag on the expression so that it will be able to used a
208** cached column value that has previously undergone an
209** affinity change.
210*/
211static void codeCompareOperands(
212 Parse *pParse, /* Parsing and code generating context */
213 Expr *pLeft, /* The left operand */
214 int *pRegLeft, /* Register where left operand is stored */
215 int *pFreeLeft, /* Free this register when done */
216 Expr *pRight, /* The right operand */
217 int *pRegRight, /* Register where right operand is stored */
218 int *pFreeRight /* Write temp register for right operand there */
219){
220 while( pLeft->op==TK_UPLUS ) pLeft = pLeft->pLeft;
221 pLeft->flags |= EP_AnyAff;
222 *pRegLeft = sqlite3ExprCodeTemp(pParse, pLeft, pFreeLeft);
223 while( pRight->op==TK_UPLUS ) pRight = pRight->pLeft;
224 pRight->flags |= EP_AnyAff;
225 *pRegRight = sqlite3ExprCodeTemp(pParse, pRight, pFreeRight);
226}
227
228/*
drhbe5c89a2004-07-26 00:31:09 +0000229** Generate code for a comparison operator.
230*/
231static int codeCompare(
232 Parse *pParse, /* The parsing (and code generating) context */
233 Expr *pLeft, /* The left operand */
234 Expr *pRight, /* The right operand */
235 int opcode, /* The comparison opcode */
drh35573352008-01-08 23:54:25 +0000236 int in1, int in2, /* Register holding operands */
drhbe5c89a2004-07-26 00:31:09 +0000237 int dest, /* Jump here if true. */
238 int jumpIfNull /* If true, jump if either operand is NULL */
239){
drh35573352008-01-08 23:54:25 +0000240 int p5;
241 int addr;
242 CollSeq *p4;
243
244 p4 = sqlite3BinaryCompareCollSeq(pParse, pLeft, pRight);
245 p5 = binaryCompareP5(pLeft, pRight, jumpIfNull);
246 addr = sqlite3VdbeAddOp4(pParse->pVdbe, opcode, in2, dest, in1,
247 (void*)p4, P4_COLLSEQ);
248 sqlite3VdbeChangeP5(pParse->pVdbe, p5);
drh2f7794c2008-04-01 03:27:39 +0000249 if( p5 & SQLITE_AFF_MASK ){
drhda250ea2008-04-01 05:07:14 +0000250 sqlite3ExprCacheAffinityChange(pParse, in1, 1);
251 sqlite3ExprCacheAffinityChange(pParse, in2, 1);
drh2f7794c2008-04-01 03:27:39 +0000252 }
drh35573352008-01-08 23:54:25 +0000253 return addr;
drhbe5c89a2004-07-26 00:31:09 +0000254}
255
256/*
drha76b5df2002-02-23 02:32:10 +0000257** Construct a new expression node and return a pointer to it. Memory
drh17435752007-08-16 04:30:38 +0000258** for this node is obtained from sqlite3_malloc(). The calling function
drha76b5df2002-02-23 02:32:10 +0000259** is responsible for making sure the node eventually gets freed.
260*/
drh17435752007-08-16 04:30:38 +0000261Expr *sqlite3Expr(
danielk1977a1644fd2007-08-29 12:31:25 +0000262 sqlite3 *db, /* Handle for sqlite3DbMallocZero() (may be null) */
drh17435752007-08-16 04:30:38 +0000263 int op, /* Expression opcode */
264 Expr *pLeft, /* Left operand */
265 Expr *pRight, /* Right operand */
266 const Token *pToken /* Argument token */
267){
drha76b5df2002-02-23 02:32:10 +0000268 Expr *pNew;
danielk1977a1644fd2007-08-29 12:31:25 +0000269 pNew = sqlite3DbMallocZero(db, sizeof(Expr));
drha76b5df2002-02-23 02:32:10 +0000270 if( pNew==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000271 /* When malloc fails, delete pLeft and pRight. Expressions passed to
272 ** this function must always be allocated with sqlite3Expr() for this
273 ** reason.
274 */
275 sqlite3ExprDelete(pLeft);
276 sqlite3ExprDelete(pRight);
drha76b5df2002-02-23 02:32:10 +0000277 return 0;
278 }
279 pNew->op = op;
280 pNew->pLeft = pLeft;
281 pNew->pRight = pRight;
danielk1977a58fdfb2005-02-08 07:50:40 +0000282 pNew->iAgg = -1;
drha76b5df2002-02-23 02:32:10 +0000283 if( pToken ){
drh4b59ab52002-08-24 18:24:51 +0000284 assert( pToken->dyn==0 );
drh145716b2004-09-24 12:24:06 +0000285 pNew->span = pNew->token = *pToken;
drha34001c2007-02-02 12:44:37 +0000286 }else if( pLeft ){
287 if( pRight ){
288 sqlite3ExprSpan(pNew, &pLeft->span, &pRight->span);
drh5ffb3ac2007-04-18 17:07:57 +0000289 if( pRight->flags & EP_ExpCollate ){
drha34001c2007-02-02 12:44:37 +0000290 pNew->flags |= EP_ExpCollate;
291 pNew->pColl = pRight->pColl;
292 }
293 }
drh5ffb3ac2007-04-18 17:07:57 +0000294 if( pLeft->flags & EP_ExpCollate ){
drha34001c2007-02-02 12:44:37 +0000295 pNew->flags |= EP_ExpCollate;
296 pNew->pColl = pLeft->pColl;
297 }
drha76b5df2002-02-23 02:32:10 +0000298 }
danielk1977fc976062007-05-10 10:46:56 +0000299
300 sqlite3ExprSetHeight(pNew);
drha76b5df2002-02-23 02:32:10 +0000301 return pNew;
302}
303
304/*
drh17435752007-08-16 04:30:38 +0000305** Works like sqlite3Expr() except that it takes an extra Parse*
306** argument and notifies the associated connection object if malloc fails.
drh206f3d92006-07-11 13:15:08 +0000307*/
drh17435752007-08-16 04:30:38 +0000308Expr *sqlite3PExpr(
309 Parse *pParse, /* Parsing context */
310 int op, /* Expression opcode */
311 Expr *pLeft, /* Left operand */
312 Expr *pRight, /* Right operand */
313 const Token *pToken /* Argument token */
314){
danielk1977a1644fd2007-08-29 12:31:25 +0000315 return sqlite3Expr(pParse->db, op, pLeft, pRight, pToken);
drh206f3d92006-07-11 13:15:08 +0000316}
317
318/*
drh4e0cff62004-11-05 05:10:28 +0000319** When doing a nested parse, you can include terms in an expression
drhb7654112008-01-12 12:48:07 +0000320** that look like this: #1 #2 ... These terms refer to registers
321** in the virtual machine. #N is the N-th register.
drh4e0cff62004-11-05 05:10:28 +0000322**
323** This routine is called by the parser to deal with on of those terms.
324** It immediately generates code to store the value in a memory location.
325** The returns an expression that will code to extract the value from
326** that memory location as needed.
327*/
328Expr *sqlite3RegisterExpr(Parse *pParse, Token *pToken){
329 Vdbe *v = pParse->pVdbe;
330 Expr *p;
drh4e0cff62004-11-05 05:10:28 +0000331 if( pParse->nested==0 ){
332 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", pToken);
danielk1977a1644fd2007-08-29 12:31:25 +0000333 return sqlite3PExpr(pParse, TK_NULL, 0, 0, 0);
drh4e0cff62004-11-05 05:10:28 +0000334 }
drhbb7ac002005-08-12 22:58:53 +0000335 if( v==0 ) return 0;
danielk1977a1644fd2007-08-29 12:31:25 +0000336 p = sqlite3PExpr(pParse, TK_REGISTER, 0, 0, pToken);
drh73c42a12004-11-20 18:13:10 +0000337 if( p==0 ){
338 return 0; /* Malloc failed */
339 }
drhb7654112008-01-12 12:48:07 +0000340 p->iTable = atoi((char*)&pToken->z[1]);
drh4e0cff62004-11-05 05:10:28 +0000341 return p;
342}
343
344/*
drh91bb0ee2004-09-01 03:06:34 +0000345** Join two expressions using an AND operator. If either expression is
346** NULL, then just return the other expression.
347*/
danielk19771e536952007-08-16 10:09:01 +0000348Expr *sqlite3ExprAnd(sqlite3 *db, Expr *pLeft, Expr *pRight){
drh91bb0ee2004-09-01 03:06:34 +0000349 if( pLeft==0 ){
350 return pRight;
351 }else if( pRight==0 ){
352 return pLeft;
353 }else{
danielk1977880c15b2007-09-01 18:24:55 +0000354 return sqlite3Expr(db, TK_AND, pLeft, pRight, 0);
drh91bb0ee2004-09-01 03:06:34 +0000355 }
356}
357
358/*
drh6977fea2002-10-22 23:38:04 +0000359** Set the Expr.span field of the given expression to span all
drha76b5df2002-02-23 02:32:10 +0000360** text between the two given tokens.
361*/
danielk19774adee202004-05-08 08:23:19 +0000362void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
drh4efc4752004-01-16 15:55:37 +0000363 assert( pRight!=0 );
364 assert( pLeft!=0 );
drhf3a65f72007-08-22 20:18:21 +0000365 if( pExpr && pRight->z && pLeft->z ){
drhad6d9462004-09-19 02:15:24 +0000366 assert( pLeft->dyn==0 || pLeft->z[pLeft->n]==0 );
drh145716b2004-09-24 12:24:06 +0000367 if( pLeft->dyn==0 && pRight->dyn==0 ){
drh6977fea2002-10-22 23:38:04 +0000368 pExpr->span.z = pLeft->z;
drh97903fe2005-05-24 20:19:57 +0000369 pExpr->span.n = pRight->n + (pRight->z - pLeft->z);
drh4b59ab52002-08-24 18:24:51 +0000370 }else{
drh6977fea2002-10-22 23:38:04 +0000371 pExpr->span.z = 0;
drh4b59ab52002-08-24 18:24:51 +0000372 }
drha76b5df2002-02-23 02:32:10 +0000373 }
374}
375
376/*
377** Construct a new expression node for a function with multiple
378** arguments.
379*/
drh17435752007-08-16 04:30:38 +0000380Expr *sqlite3ExprFunction(Parse *pParse, ExprList *pList, Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000381 Expr *pNew;
danielk19774b202ae2006-01-23 05:50:58 +0000382 assert( pToken );
drh17435752007-08-16 04:30:38 +0000383 pNew = sqlite3DbMallocZero(pParse->db, sizeof(Expr) );
drha76b5df2002-02-23 02:32:10 +0000384 if( pNew==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000385 sqlite3ExprListDelete(pList); /* Avoid leaking memory when malloc fails */
drha76b5df2002-02-23 02:32:10 +0000386 return 0;
387 }
388 pNew->op = TK_FUNCTION;
389 pNew->pList = pList;
danielk19774b202ae2006-01-23 05:50:58 +0000390 assert( pToken->dyn==0 );
391 pNew->token = *pToken;
drh6977fea2002-10-22 23:38:04 +0000392 pNew->span = pNew->token;
danielk1977fc976062007-05-10 10:46:56 +0000393
394 sqlite3ExprSetHeight(pNew);
drha76b5df2002-02-23 02:32:10 +0000395 return pNew;
396}
397
398/*
drhfa6bc002004-09-07 16:19:52 +0000399** Assign a variable number to an expression that encodes a wildcard
400** in the original SQL statement.
401**
402** Wildcards consisting of a single "?" are assigned the next sequential
403** variable number.
404**
405** Wildcards of the form "?nnn" are assigned the number "nnn". We make
406** sure "nnn" is not too be to avoid a denial of service attack when
407** the SQL statement comes from an external source.
408**
409** Wildcards of the form ":aaa" or "$aaa" are assigned the same number
410** as the previous instance of the same wildcard. Or if this is the first
411** instance of the wildcard, the next sequenial variable number is
412** assigned.
413*/
414void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){
415 Token *pToken;
drh17435752007-08-16 04:30:38 +0000416 sqlite3 *db = pParse->db;
417
drhfa6bc002004-09-07 16:19:52 +0000418 if( pExpr==0 ) return;
419 pToken = &pExpr->token;
420 assert( pToken->n>=1 );
421 assert( pToken->z!=0 );
422 assert( pToken->z[0]!=0 );
423 if( pToken->n==1 ){
424 /* Wildcard of the form "?". Assign the next variable number */
425 pExpr->iTable = ++pParse->nVar;
426 }else if( pToken->z[0]=='?' ){
427 /* Wildcard of the form "?nnn". Convert "nnn" to an integer and
428 ** use it as the variable number */
429 int i;
drh2646da72005-12-09 20:02:05 +0000430 pExpr->iTable = i = atoi((char*)&pToken->z[1]);
drhbb4957f2008-03-20 14:03:29 +0000431 if( i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
drhfa6bc002004-09-07 16:19:52 +0000432 sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
drhbb4957f2008-03-20 14:03:29 +0000433 db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]);
drhfa6bc002004-09-07 16:19:52 +0000434 }
435 if( i>pParse->nVar ){
436 pParse->nVar = i;
437 }
438 }else{
439 /* Wildcards of the form ":aaa" or "$aaa". Reuse the same variable
440 ** number as the prior appearance of the same name, or if the name
441 ** has never appeared before, reuse the same variable number
442 */
443 int i, n;
444 n = pToken->n;
445 for(i=0; i<pParse->nVarExpr; i++){
446 Expr *pE;
447 if( (pE = pParse->apVarExpr[i])!=0
448 && pE->token.n==n
449 && memcmp(pE->token.z, pToken->z, n)==0 ){
450 pExpr->iTable = pE->iTable;
451 break;
452 }
453 }
454 if( i>=pParse->nVarExpr ){
455 pExpr->iTable = ++pParse->nVar;
456 if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){
457 pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10;
drh17435752007-08-16 04:30:38 +0000458 pParse->apVarExpr =
459 sqlite3DbReallocOrFree(
460 db,
461 pParse->apVarExpr,
462 pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0])
463 );
drhfa6bc002004-09-07 16:19:52 +0000464 }
drh17435752007-08-16 04:30:38 +0000465 if( !db->mallocFailed ){
drhfa6bc002004-09-07 16:19:52 +0000466 assert( pParse->apVarExpr!=0 );
467 pParse->apVarExpr[pParse->nVarExpr++] = pExpr;
468 }
469 }
470 }
drhbb4957f2008-03-20 14:03:29 +0000471 if( !pParse->nErr && pParse->nVar>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
danielk1977832b2662007-05-09 11:37:22 +0000472 sqlite3ErrorMsg(pParse, "too many SQL variables");
473 }
drhfa6bc002004-09-07 16:19:52 +0000474}
475
476/*
drha2e00042002-01-22 03:13:42 +0000477** Recursively delete an expression tree.
478*/
danielk19774adee202004-05-08 08:23:19 +0000479void sqlite3ExprDelete(Expr *p){
drha2e00042002-01-22 03:13:42 +0000480 if( p==0 ) return;
drh17435752007-08-16 04:30:38 +0000481 if( p->span.dyn ) sqlite3_free((char*)p->span.z);
482 if( p->token.dyn ) sqlite3_free((char*)p->token.z);
danielk19774adee202004-05-08 08:23:19 +0000483 sqlite3ExprDelete(p->pLeft);
484 sqlite3ExprDelete(p->pRight);
485 sqlite3ExprListDelete(p->pList);
486 sqlite3SelectDelete(p->pSelect);
drh17435752007-08-16 04:30:38 +0000487 sqlite3_free(p);
drha2e00042002-01-22 03:13:42 +0000488}
489
drhd2687b72005-08-12 22:56:09 +0000490/*
491** The Expr.token field might be a string literal that is quoted.
492** If so, remove the quotation marks.
493*/
drh17435752007-08-16 04:30:38 +0000494void sqlite3DequoteExpr(sqlite3 *db, Expr *p){
drhd2687b72005-08-12 22:56:09 +0000495 if( ExprHasAnyProperty(p, EP_Dequoted) ){
496 return;
497 }
498 ExprSetProperty(p, EP_Dequoted);
499 if( p->token.dyn==0 ){
drh17435752007-08-16 04:30:38 +0000500 sqlite3TokenCopy(db, &p->token, &p->token);
drhd2687b72005-08-12 22:56:09 +0000501 }
502 sqlite3Dequote((char*)p->token.z);
503}
504
drha76b5df2002-02-23 02:32:10 +0000505
506/*
drhff78bd22002-02-27 01:47:11 +0000507** The following group of routines make deep copies of expressions,
508** expression lists, ID lists, and select statements. The copies can
509** be deleted (by being passed to their respective ...Delete() routines)
510** without effecting the originals.
511**
danielk19774adee202004-05-08 08:23:19 +0000512** The expression list, ID, and source lists return by sqlite3ExprListDup(),
513** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
drhad3cab52002-05-24 02:04:32 +0000514** by subsequent calls to sqlite*ListAppend() routines.
drhff78bd22002-02-27 01:47:11 +0000515**
drhad3cab52002-05-24 02:04:32 +0000516** Any tables that the SrcList might point to are not duplicated.
drhff78bd22002-02-27 01:47:11 +0000517*/
danielk19771e536952007-08-16 10:09:01 +0000518Expr *sqlite3ExprDup(sqlite3 *db, Expr *p){
drhff78bd22002-02-27 01:47:11 +0000519 Expr *pNew;
520 if( p==0 ) return 0;
drh17435752007-08-16 04:30:38 +0000521 pNew = sqlite3DbMallocRaw(db, sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000522 if( pNew==0 ) return 0;
drh3b167c72002-06-28 12:18:47 +0000523 memcpy(pNew, p, sizeof(*pNew));
drh6977fea2002-10-22 23:38:04 +0000524 if( p->token.z!=0 ){
drh17435752007-08-16 04:30:38 +0000525 pNew->token.z = (u8*)sqlite3DbStrNDup(db, (char*)p->token.z, p->token.n);
drh4b59ab52002-08-24 18:24:51 +0000526 pNew->token.dyn = 1;
527 }else{
drh4efc4752004-01-16 15:55:37 +0000528 assert( pNew->token.z==0 );
drh4b59ab52002-08-24 18:24:51 +0000529 }
drh6977fea2002-10-22 23:38:04 +0000530 pNew->span.z = 0;
drh17435752007-08-16 04:30:38 +0000531 pNew->pLeft = sqlite3ExprDup(db, p->pLeft);
532 pNew->pRight = sqlite3ExprDup(db, p->pRight);
533 pNew->pList = sqlite3ExprListDup(db, p->pList);
534 pNew->pSelect = sqlite3SelectDup(db, p->pSelect);
drhff78bd22002-02-27 01:47:11 +0000535 return pNew;
536}
drh17435752007-08-16 04:30:38 +0000537void sqlite3TokenCopy(sqlite3 *db, Token *pTo, Token *pFrom){
538 if( pTo->dyn ) sqlite3_free((char*)pTo->z);
drh4b59ab52002-08-24 18:24:51 +0000539 if( pFrom->z ){
540 pTo->n = pFrom->n;
drh17435752007-08-16 04:30:38 +0000541 pTo->z = (u8*)sqlite3DbStrNDup(db, (char*)pFrom->z, pFrom->n);
drh4b59ab52002-08-24 18:24:51 +0000542 pTo->dyn = 1;
543 }else{
drh4b59ab52002-08-24 18:24:51 +0000544 pTo->z = 0;
drh4b59ab52002-08-24 18:24:51 +0000545 }
546}
drh17435752007-08-16 04:30:38 +0000547ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p){
drhff78bd22002-02-27 01:47:11 +0000548 ExprList *pNew;
drh145716b2004-09-24 12:24:06 +0000549 struct ExprList_item *pItem, *pOldItem;
drhff78bd22002-02-27 01:47:11 +0000550 int i;
551 if( p==0 ) return 0;
drh17435752007-08-16 04:30:38 +0000552 pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +0000553 if( pNew==0 ) return 0;
danielk197731dad9d2007-08-16 11:36:15 +0000554 pNew->iECursor = 0;
drh4305d102003-07-30 12:34:12 +0000555 pNew->nExpr = pNew->nAlloc = p->nExpr;
drh17435752007-08-16 04:30:38 +0000556 pNew->a = pItem = sqlite3DbMallocRaw(db, p->nExpr*sizeof(p->a[0]) );
danielk1977e0048402004-06-15 16:51:01 +0000557 if( pItem==0 ){
drh17435752007-08-16 04:30:38 +0000558 sqlite3_free(pNew);
danielk1977e0048402004-06-15 16:51:01 +0000559 return 0;
560 }
drh145716b2004-09-24 12:24:06 +0000561 pOldItem = p->a;
562 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
drh4b59ab52002-08-24 18:24:51 +0000563 Expr *pNewExpr, *pOldExpr;
drh17435752007-08-16 04:30:38 +0000564 pItem->pExpr = pNewExpr = sqlite3ExprDup(db, pOldExpr = pOldItem->pExpr);
drh6977fea2002-10-22 23:38:04 +0000565 if( pOldExpr->span.z!=0 && pNewExpr ){
566 /* Always make a copy of the span for top-level expressions in the
drh4b59ab52002-08-24 18:24:51 +0000567 ** expression list. The logic in SELECT processing that determines
568 ** the names of columns in the result set needs this information */
drh17435752007-08-16 04:30:38 +0000569 sqlite3TokenCopy(db, &pNewExpr->span, &pOldExpr->span);
drh4b59ab52002-08-24 18:24:51 +0000570 }
drh1f3e9052002-10-31 00:09:39 +0000571 assert( pNewExpr==0 || pNewExpr->span.z!=0
drh6f7adc82006-01-11 21:41:20 +0000572 || pOldExpr->span.z==0
drh17435752007-08-16 04:30:38 +0000573 || db->mallocFailed );
574 pItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
drh145716b2004-09-24 12:24:06 +0000575 pItem->sortOrder = pOldItem->sortOrder;
576 pItem->isAgg = pOldItem->isAgg;
drh3e7bc9c2004-02-21 19:17:17 +0000577 pItem->done = 0;
drhff78bd22002-02-27 01:47:11 +0000578 }
579 return pNew;
580}
danielk197793758c82005-01-21 08:13:14 +0000581
582/*
583** If cursors, triggers, views and subqueries are all omitted from
584** the build, then none of the following routines, except for
585** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes
586** called with a NULL argument.
587*/
danielk19776a67fe82005-02-04 04:07:16 +0000588#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \
589 || !defined(SQLITE_OMIT_SUBQUERY)
drh17435752007-08-16 04:30:38 +0000590SrcList *sqlite3SrcListDup(sqlite3 *db, SrcList *p){
drhad3cab52002-05-24 02:04:32 +0000591 SrcList *pNew;
592 int i;
drh113088e2003-03-20 01:16:58 +0000593 int nByte;
drhad3cab52002-05-24 02:04:32 +0000594 if( p==0 ) return 0;
drh113088e2003-03-20 01:16:58 +0000595 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
drh17435752007-08-16 04:30:38 +0000596 pNew = sqlite3DbMallocRaw(db, nByte );
drhad3cab52002-05-24 02:04:32 +0000597 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000598 pNew->nSrc = pNew->nAlloc = p->nSrc;
drhad3cab52002-05-24 02:04:32 +0000599 for(i=0; i<p->nSrc; i++){
drh4efc4752004-01-16 15:55:37 +0000600 struct SrcList_item *pNewItem = &pNew->a[i];
601 struct SrcList_item *pOldItem = &p->a[i];
drhed8a3bb2005-06-06 21:19:56 +0000602 Table *pTab;
drh17435752007-08-16 04:30:38 +0000603 pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase);
604 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
605 pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias);
drh4efc4752004-01-16 15:55:37 +0000606 pNewItem->jointype = pOldItem->jointype;
607 pNewItem->iCursor = pOldItem->iCursor;
danielk19771787cca2006-02-10 07:07:14 +0000608 pNewItem->isPopulated = pOldItem->isPopulated;
drhed8a3bb2005-06-06 21:19:56 +0000609 pTab = pNewItem->pTab = pOldItem->pTab;
610 if( pTab ){
611 pTab->nRef++;
danielk1977a1cb1832005-02-12 08:59:55 +0000612 }
drh17435752007-08-16 04:30:38 +0000613 pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect);
614 pNewItem->pOn = sqlite3ExprDup(db, pOldItem->pOn);
615 pNewItem->pUsing = sqlite3IdListDup(db, pOldItem->pUsing);
danielk19776c18b6e2005-01-30 09:17:58 +0000616 pNewItem->colUsed = pOldItem->colUsed;
drhad3cab52002-05-24 02:04:32 +0000617 }
618 return pNew;
619}
drh17435752007-08-16 04:30:38 +0000620IdList *sqlite3IdListDup(sqlite3 *db, IdList *p){
drhff78bd22002-02-27 01:47:11 +0000621 IdList *pNew;
622 int i;
623 if( p==0 ) return 0;
drh17435752007-08-16 04:30:38 +0000624 pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +0000625 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000626 pNew->nId = pNew->nAlloc = p->nId;
drh17435752007-08-16 04:30:38 +0000627 pNew->a = sqlite3DbMallocRaw(db, p->nId*sizeof(p->a[0]) );
danielk1977d5d56522005-03-16 12:15:20 +0000628 if( pNew->a==0 ){
drh17435752007-08-16 04:30:38 +0000629 sqlite3_free(pNew);
danielk1977d5d56522005-03-16 12:15:20 +0000630 return 0;
631 }
drhff78bd22002-02-27 01:47:11 +0000632 for(i=0; i<p->nId; i++){
drh4efc4752004-01-16 15:55:37 +0000633 struct IdList_item *pNewItem = &pNew->a[i];
634 struct IdList_item *pOldItem = &p->a[i];
drh17435752007-08-16 04:30:38 +0000635 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
drh4efc4752004-01-16 15:55:37 +0000636 pNewItem->idx = pOldItem->idx;
drhff78bd22002-02-27 01:47:11 +0000637 }
638 return pNew;
639}
drh17435752007-08-16 04:30:38 +0000640Select *sqlite3SelectDup(sqlite3 *db, Select *p){
drhff78bd22002-02-27 01:47:11 +0000641 Select *pNew;
642 if( p==0 ) return 0;
drh17435752007-08-16 04:30:38 +0000643 pNew = sqlite3DbMallocRaw(db, sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000644 if( pNew==0 ) return 0;
645 pNew->isDistinct = p->isDistinct;
drh17435752007-08-16 04:30:38 +0000646 pNew->pEList = sqlite3ExprListDup(db, p->pEList);
647 pNew->pSrc = sqlite3SrcListDup(db, p->pSrc);
648 pNew->pWhere = sqlite3ExprDup(db, p->pWhere);
649 pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy);
650 pNew->pHaving = sqlite3ExprDup(db, p->pHaving);
651 pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy);
drhff78bd22002-02-27 01:47:11 +0000652 pNew->op = p->op;
drh17435752007-08-16 04:30:38 +0000653 pNew->pPrior = sqlite3SelectDup(db, p->pPrior);
654 pNew->pLimit = sqlite3ExprDup(db, p->pLimit);
655 pNew->pOffset = sqlite3ExprDup(db, p->pOffset);
drh7b58dae2003-07-20 01:16:46 +0000656 pNew->iLimit = -1;
657 pNew->iOffset = -1;
danielk1977a1cb1832005-02-12 08:59:55 +0000658 pNew->isResolved = p->isResolved;
659 pNew->isAgg = p->isAgg;
drhb9bb7c12006-06-11 23:41:55 +0000660 pNew->usesEphm = 0;
drh8e647b82005-09-23 21:11:53 +0000661 pNew->disallowOrderBy = 0;
drh0342b1f2005-09-01 03:07:44 +0000662 pNew->pRightmost = 0;
drhb9bb7c12006-06-11 23:41:55 +0000663 pNew->addrOpenEphm[0] = -1;
664 pNew->addrOpenEphm[1] = -1;
665 pNew->addrOpenEphm[2] = -1;
drhff78bd22002-02-27 01:47:11 +0000666 return pNew;
667}
danielk197793758c82005-01-21 08:13:14 +0000668#else
drh17435752007-08-16 04:30:38 +0000669Select *sqlite3SelectDup(sqlite3 *db, Select *p){
danielk197793758c82005-01-21 08:13:14 +0000670 assert( p==0 );
671 return 0;
672}
673#endif
drhff78bd22002-02-27 01:47:11 +0000674
675
676/*
drha76b5df2002-02-23 02:32:10 +0000677** Add a new element to the end of an expression list. If pList is
678** initially NULL, then create a new expression list.
679*/
drh17435752007-08-16 04:30:38 +0000680ExprList *sqlite3ExprListAppend(
681 Parse *pParse, /* Parsing context */
682 ExprList *pList, /* List to which to append. Might be NULL */
683 Expr *pExpr, /* Expression to be appended */
684 Token *pName /* AS keyword for the expression */
685){
686 sqlite3 *db = pParse->db;
drha76b5df2002-02-23 02:32:10 +0000687 if( pList==0 ){
drh17435752007-08-16 04:30:38 +0000688 pList = sqlite3DbMallocZero(db, sizeof(ExprList) );
drha76b5df2002-02-23 02:32:10 +0000689 if( pList==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000690 goto no_mem;
drha76b5df2002-02-23 02:32:10 +0000691 }
drh4efc4752004-01-16 15:55:37 +0000692 assert( pList->nAlloc==0 );
drha76b5df2002-02-23 02:32:10 +0000693 }
drh4305d102003-07-30 12:34:12 +0000694 if( pList->nAlloc<=pList->nExpr ){
danielk1977d5d56522005-03-16 12:15:20 +0000695 struct ExprList_item *a;
696 int n = pList->nAlloc*2 + 4;
danielk197726783a52007-08-29 14:06:22 +0000697 a = sqlite3DbRealloc(db, pList->a, n*sizeof(pList->a[0]));
danielk1977d5d56522005-03-16 12:15:20 +0000698 if( a==0 ){
699 goto no_mem;
drha76b5df2002-02-23 02:32:10 +0000700 }
danielk1977d5d56522005-03-16 12:15:20 +0000701 pList->a = a;
702 pList->nAlloc = n;
drha76b5df2002-02-23 02:32:10 +0000703 }
drh4efc4752004-01-16 15:55:37 +0000704 assert( pList->a!=0 );
705 if( pExpr || pName ){
706 struct ExprList_item *pItem = &pList->a[pList->nExpr++];
707 memset(pItem, 0, sizeof(*pItem));
drh17435752007-08-16 04:30:38 +0000708 pItem->zName = sqlite3NameFromToken(db, pName);
danielk1977e94ddc92005-03-21 03:53:38 +0000709 pItem->pExpr = pExpr;
drha76b5df2002-02-23 02:32:10 +0000710 }
711 return pList;
danielk1977d5d56522005-03-16 12:15:20 +0000712
713no_mem:
714 /* Avoid leaking memory if malloc has failed. */
715 sqlite3ExprDelete(pExpr);
716 sqlite3ExprListDelete(pList);
717 return 0;
drha76b5df2002-02-23 02:32:10 +0000718}
719
720/*
danielk19777a15a4b2007-05-08 17:54:43 +0000721** If the expression list pEList contains more than iLimit elements,
722** leave an error message in pParse.
723*/
724void sqlite3ExprListCheckLength(
725 Parse *pParse,
726 ExprList *pEList,
danielk19777a15a4b2007-05-08 17:54:43 +0000727 const char *zObject
728){
drhb1a6c3c2008-03-20 16:30:17 +0000729 int mx = pParse->db->aLimit[SQLITE_LIMIT_COLUMN];
730 if( pEList && pEList->nExpr>mx ){
danielk19777a15a4b2007-05-08 17:54:43 +0000731 sqlite3ErrorMsg(pParse, "too many columns in %s", zObject);
732 }
733}
734
danielk1977fc976062007-05-10 10:46:56 +0000735
danielk1977fc976062007-05-10 10:46:56 +0000736/* The following three functions, heightOfExpr(), heightOfExprList()
737** and heightOfSelect(), are used to determine the maximum height
738** of any expression tree referenced by the structure passed as the
739** first argument.
740**
741** If this maximum height is greater than the current value pointed
742** to by pnHeight, the second parameter, then set *pnHeight to that
743** value.
744*/
745static void heightOfExpr(Expr *p, int *pnHeight){
746 if( p ){
747 if( p->nHeight>*pnHeight ){
748 *pnHeight = p->nHeight;
749 }
750 }
751}
752static void heightOfExprList(ExprList *p, int *pnHeight){
753 if( p ){
754 int i;
755 for(i=0; i<p->nExpr; i++){
756 heightOfExpr(p->a[i].pExpr, pnHeight);
757 }
758 }
759}
760static void heightOfSelect(Select *p, int *pnHeight){
761 if( p ){
762 heightOfExpr(p->pWhere, pnHeight);
763 heightOfExpr(p->pHaving, pnHeight);
764 heightOfExpr(p->pLimit, pnHeight);
765 heightOfExpr(p->pOffset, pnHeight);
766 heightOfExprList(p->pEList, pnHeight);
767 heightOfExprList(p->pGroupBy, pnHeight);
768 heightOfExprList(p->pOrderBy, pnHeight);
769 heightOfSelect(p->pPrior, pnHeight);
770 }
771}
772
773/*
774** Set the Expr.nHeight variable in the structure passed as an
775** argument. An expression with no children, Expr.pList or
776** Expr.pSelect member has a height of 1. Any other expression
777** has a height equal to the maximum height of any other
778** referenced Expr plus one.
779*/
780void sqlite3ExprSetHeight(Expr *p){
781 int nHeight = 0;
782 heightOfExpr(p->pLeft, &nHeight);
783 heightOfExpr(p->pRight, &nHeight);
784 heightOfExprList(p->pList, &nHeight);
785 heightOfSelect(p->pSelect, &nHeight);
786 p->nHeight = nHeight + 1;
787}
788
789/*
790** Return the maximum height of any expression tree referenced
791** by the select statement passed as an argument.
792*/
793int sqlite3SelectExprHeight(Select *p){
794 int nHeight = 0;
795 heightOfSelect(p, &nHeight);
796 return nHeight;
797}
danielk1977fc976062007-05-10 10:46:56 +0000798
danielk19777a15a4b2007-05-08 17:54:43 +0000799/*
drha76b5df2002-02-23 02:32:10 +0000800** Delete an entire expression list.
801*/
danielk19774adee202004-05-08 08:23:19 +0000802void sqlite3ExprListDelete(ExprList *pList){
drha76b5df2002-02-23 02:32:10 +0000803 int i;
drhbe5c89a2004-07-26 00:31:09 +0000804 struct ExprList_item *pItem;
drha76b5df2002-02-23 02:32:10 +0000805 if( pList==0 ) return;
drh1bdd9b52004-04-23 17:04:44 +0000806 assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
807 assert( pList->nExpr<=pList->nAlloc );
drhbe5c89a2004-07-26 00:31:09 +0000808 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
809 sqlite3ExprDelete(pItem->pExpr);
drh17435752007-08-16 04:30:38 +0000810 sqlite3_free(pItem->zName);
drha76b5df2002-02-23 02:32:10 +0000811 }
drh17435752007-08-16 04:30:38 +0000812 sqlite3_free(pList->a);
813 sqlite3_free(pList);
drha76b5df2002-02-23 02:32:10 +0000814}
815
816/*
drh678ccce2008-03-31 18:19:54 +0000817** Walk an expression tree. Call xFunc for each node visited. xFunc
818** is called on the node before xFunc is called on the nodes children.
drh73b211a2005-01-18 04:00:42 +0000819**
drh626a8792005-01-17 22:08:19 +0000820** The return value from xFunc determines whether the tree walk continues.
821** 0 means continue walking the tree. 1 means do not walk children
822** of the current node but continue with siblings. 2 means abandon
823** the tree walk completely.
824**
825** The return value from this routine is 1 to abandon the tree walk
826** and 0 to continue.
drh87abf5c2005-08-25 12:45:04 +0000827**
828** NOTICE: This routine does *not* descend into subqueries.
drh626a8792005-01-17 22:08:19 +0000829*/
danielk1977a58fdfb2005-02-08 07:50:40 +0000830static int walkExprList(ExprList *, int (*)(void *, Expr*), void *);
drh626a8792005-01-17 22:08:19 +0000831static int walkExprTree(Expr *pExpr, int (*xFunc)(void*,Expr*), void *pArg){
drh626a8792005-01-17 22:08:19 +0000832 int rc;
833 if( pExpr==0 ) return 0;
834 rc = (*xFunc)(pArg, pExpr);
835 if( rc==0 ){
836 if( walkExprTree(pExpr->pLeft, xFunc, pArg) ) return 1;
837 if( walkExprTree(pExpr->pRight, xFunc, pArg) ) return 1;
danielk1977a58fdfb2005-02-08 07:50:40 +0000838 if( walkExprList(pExpr->pList, xFunc, pArg) ) return 1;
drh626a8792005-01-17 22:08:19 +0000839 }
840 return rc>1;
841}
842
843/*
danielk1977a58fdfb2005-02-08 07:50:40 +0000844** Call walkExprTree() for every expression in list p.
845*/
846static int walkExprList(ExprList *p, int (*xFunc)(void *, Expr*), void *pArg){
847 int i;
848 struct ExprList_item *pItem;
849 if( !p ) return 0;
850 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
851 if( walkExprTree(pItem->pExpr, xFunc, pArg) ) return 1;
852 }
853 return 0;
854}
855
856/*
857** Call walkExprTree() for every expression in Select p, not including
858** expressions that are part of sub-selects in any FROM clause or the LIMIT
859** or OFFSET expressions..
860*/
861static int walkSelectExpr(Select *p, int (*xFunc)(void *, Expr*), void *pArg){
862 walkExprList(p->pEList, xFunc, pArg);
863 walkExprTree(p->pWhere, xFunc, pArg);
864 walkExprList(p->pGroupBy, xFunc, pArg);
865 walkExprTree(p->pHaving, xFunc, pArg);
866 walkExprList(p->pOrderBy, xFunc, pArg);
danielk197715d79822007-05-15 07:00:34 +0000867 if( p->pPrior ){
868 walkSelectExpr(p->pPrior, xFunc, pArg);
869 }
danielk1977a58fdfb2005-02-08 07:50:40 +0000870 return 0;
871}
872
873
874/*
drh626a8792005-01-17 22:08:19 +0000875** This routine is designed as an xFunc for walkExprTree().
876**
877** pArg is really a pointer to an integer. If we can tell by looking
drh73b211a2005-01-18 04:00:42 +0000878** at pExpr that the expression that contains pExpr is not a constant
879** expression, then set *pArg to 0 and return 2 to abandon the tree walk.
880** If pExpr does does not disqualify the expression from being a constant
881** then do nothing.
882**
883** After walking the whole tree, if no nodes are found that disqualify
884** the expression as constant, then we assume the whole expression
885** is constant. See sqlite3ExprIsConstant() for additional information.
drh626a8792005-01-17 22:08:19 +0000886*/
887static int exprNodeIsConstant(void *pArg, Expr *pExpr){
drh0a168372007-06-08 00:20:47 +0000888 int *pN = (int*)pArg;
889
890 /* If *pArg is 3 then any term of the expression that comes from
891 ** the ON or USING clauses of a join disqualifies the expression
892 ** from being considered constant. */
893 if( (*pN)==3 && ExprHasAnyProperty(pExpr, EP_FromJoin) ){
894 *pN = 0;
895 return 2;
896 }
897
drh626a8792005-01-17 22:08:19 +0000898 switch( pExpr->op ){
drheb55bd22005-06-30 17:04:21 +0000899 /* Consider functions to be constant if all their arguments are constant
900 ** and *pArg==2 */
901 case TK_FUNCTION:
drh0a168372007-06-08 00:20:47 +0000902 if( (*pN)==2 ) return 0;
drheb55bd22005-06-30 17:04:21 +0000903 /* Fall through */
drh626a8792005-01-17 22:08:19 +0000904 case TK_ID:
905 case TK_COLUMN:
906 case TK_DOT:
907 case TK_AGG_FUNCTION:
drh13449892005-09-07 21:22:45 +0000908 case TK_AGG_COLUMN:
drhfe2093d2005-01-20 22:48:47 +0000909#ifndef SQLITE_OMIT_SUBQUERY
910 case TK_SELECT:
911 case TK_EXISTS:
912#endif
drh0a168372007-06-08 00:20:47 +0000913 *pN = 0;
drh626a8792005-01-17 22:08:19 +0000914 return 2;
drh87abf5c2005-08-25 12:45:04 +0000915 case TK_IN:
916 if( pExpr->pSelect ){
drh0a168372007-06-08 00:20:47 +0000917 *pN = 0;
drh87abf5c2005-08-25 12:45:04 +0000918 return 2;
919 }
drh626a8792005-01-17 22:08:19 +0000920 default:
921 return 0;
922 }
923}
924
925/*
drhfef52082000-06-06 01:50:43 +0000926** Walk an expression tree. Return 1 if the expression is constant
drheb55bd22005-06-30 17:04:21 +0000927** and 0 if it involves variables or function calls.
drh23989372002-05-21 13:43:04 +0000928**
929** For the purposes of this function, a double-quoted string (ex: "abc")
930** is considered a variable but a single-quoted string (ex: 'abc') is
931** a constant.
drhfef52082000-06-06 01:50:43 +0000932*/
danielk19774adee202004-05-08 08:23:19 +0000933int sqlite3ExprIsConstant(Expr *p){
drh626a8792005-01-17 22:08:19 +0000934 int isConst = 1;
935 walkExprTree(p, exprNodeIsConstant, &isConst);
936 return isConst;
drhfef52082000-06-06 01:50:43 +0000937}
938
939/*
drheb55bd22005-06-30 17:04:21 +0000940** Walk an expression tree. Return 1 if the expression is constant
drh0a168372007-06-08 00:20:47 +0000941** that does no originate from the ON or USING clauses of a join.
942** Return 0 if it involves variables or function calls or terms from
943** an ON or USING clause.
944*/
945int sqlite3ExprIsConstantNotJoin(Expr *p){
946 int isConst = 3;
947 walkExprTree(p, exprNodeIsConstant, &isConst);
948 return isConst!=0;
949}
950
951/*
952** Walk an expression tree. Return 1 if the expression is constant
drheb55bd22005-06-30 17:04:21 +0000953** or a function call with constant arguments. Return and 0 if there
954** are any variables.
955**
956** For the purposes of this function, a double-quoted string (ex: "abc")
957** is considered a variable but a single-quoted string (ex: 'abc') is
958** a constant.
959*/
960int sqlite3ExprIsConstantOrFunction(Expr *p){
961 int isConst = 2;
962 walkExprTree(p, exprNodeIsConstant, &isConst);
963 return isConst!=0;
964}
965
966/*
drh73b211a2005-01-18 04:00:42 +0000967** If the expression p codes a constant integer that is small enough
drh202b2df2004-01-06 01:13:46 +0000968** to fit in a 32-bit integer, return 1 and put the value of the integer
969** in *pValue. If the expression is not an integer or if it is too big
970** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
drhe4de1fe2002-06-02 16:09:01 +0000971*/
danielk19774adee202004-05-08 08:23:19 +0000972int sqlite3ExprIsInteger(Expr *p, int *pValue){
drhe4de1fe2002-06-02 16:09:01 +0000973 switch( p->op ){
974 case TK_INTEGER: {
drh2646da72005-12-09 20:02:05 +0000975 if( sqlite3GetInt32((char*)p->token.z, pValue) ){
drh202b2df2004-01-06 01:13:46 +0000976 return 1;
977 }
978 break;
drhe4de1fe2002-06-02 16:09:01 +0000979 }
drh4b59ab52002-08-24 18:24:51 +0000980 case TK_UPLUS: {
danielk19774adee202004-05-08 08:23:19 +0000981 return sqlite3ExprIsInteger(p->pLeft, pValue);
drh4b59ab52002-08-24 18:24:51 +0000982 }
drhe4de1fe2002-06-02 16:09:01 +0000983 case TK_UMINUS: {
984 int v;
danielk19774adee202004-05-08 08:23:19 +0000985 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
drhe4de1fe2002-06-02 16:09:01 +0000986 *pValue = -v;
987 return 1;
988 }
989 break;
990 }
991 default: break;
992 }
993 return 0;
994}
995
996/*
drhc4a3c772001-04-04 11:48:57 +0000997** Return TRUE if the given string is a row-id column name.
998*/
danielk19774adee202004-05-08 08:23:19 +0000999int sqlite3IsRowid(const char *z){
1000 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
1001 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
1002 if( sqlite3StrICmp(z, "OID")==0 ) return 1;
drhc4a3c772001-04-04 11:48:57 +00001003 return 0;
1004}
1005
1006/*
drh8141f612004-01-25 22:44:58 +00001007** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
1008** that name in the set of source tables in pSrcList and make the pExpr
1009** expression node refer back to that source column. The following changes
1010** are made to pExpr:
1011**
1012** pExpr->iDb Set the index in db->aDb[] of the database holding
1013** the table.
1014** pExpr->iTable Set to the cursor number for the table obtained
1015** from pSrcList.
1016** pExpr->iColumn Set to the column number within the table.
drh8141f612004-01-25 22:44:58 +00001017** pExpr->op Set to TK_COLUMN.
1018** pExpr->pLeft Any expression this points to is deleted
1019** pExpr->pRight Any expression this points to is deleted.
1020**
1021** The pDbToken is the name of the database (the "X"). This value may be
1022** NULL meaning that name is of the form Y.Z or Z. Any available database
1023** can be used. The pTableToken is the name of the table (the "Y"). This
1024** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it
1025** means that the form of the name is Z and that columns from any table
1026** can be used.
1027**
1028** If the name cannot be resolved unambiguously, leave an error message
1029** in pParse and return non-zero. Return zero on success.
1030*/
1031static int lookupName(
drhffe07b22005-11-03 00:41:17 +00001032 Parse *pParse, /* The parsing context */
drh8141f612004-01-25 22:44:58 +00001033 Token *pDbToken, /* Name of the database containing table, or NULL */
1034 Token *pTableToken, /* Name of table containing column, or NULL */
1035 Token *pColumnToken, /* Name of the column. */
drh626a8792005-01-17 22:08:19 +00001036 NameContext *pNC, /* The name context used to resolve the name */
drh8141f612004-01-25 22:44:58 +00001037 Expr *pExpr /* Make this EXPR node point to the selected column */
1038){
1039 char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */
1040 char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */
1041 char *zCol = 0; /* Name of the column. The "Z" */
1042 int i, j; /* Loop counters */
1043 int cnt = 0; /* Number of matching column names */
1044 int cntTab = 0; /* Number of matching table names */
drh626a8792005-01-17 22:08:19 +00001045 sqlite3 *db = pParse->db; /* The database */
drh51669862004-12-18 18:40:26 +00001046 struct SrcList_item *pItem; /* Use for looping over pSrcList items */
1047 struct SrcList_item *pMatch = 0; /* The matching pSrcList item */
drh73b211a2005-01-18 04:00:42 +00001048 NameContext *pTopNC = pNC; /* First namecontext in the list */
drh728b5772007-09-18 15:55:07 +00001049 Schema *pSchema = 0; /* Schema of the expression */
drh8141f612004-01-25 22:44:58 +00001050
1051 assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
drh17435752007-08-16 04:30:38 +00001052 zDb = sqlite3NameFromToken(db, pDbToken);
1053 zTab = sqlite3NameFromToken(db, pTableToken);
1054 zCol = sqlite3NameFromToken(db, pColumnToken);
1055 if( db->mallocFailed ){
danielk1977d5d56522005-03-16 12:15:20 +00001056 goto lookupname_end;
drh8141f612004-01-25 22:44:58 +00001057 }
drh8141f612004-01-25 22:44:58 +00001058
1059 pExpr->iTable = -1;
drh626a8792005-01-17 22:08:19 +00001060 while( pNC && cnt==0 ){
drhffe07b22005-11-03 00:41:17 +00001061 ExprList *pEList;
drh626a8792005-01-17 22:08:19 +00001062 SrcList *pSrcList = pNC->pSrcList;
drh8141f612004-01-25 22:44:58 +00001063
danielk1977b3bce662005-01-29 08:32:43 +00001064 if( pSrcList ){
1065 for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
drh43617e92006-03-06 20:55:46 +00001066 Table *pTab;
1067 int iDb;
danielk1977b3bce662005-01-29 08:32:43 +00001068 Column *pCol;
1069
drh43617e92006-03-06 20:55:46 +00001070 pTab = pItem->pTab;
1071 assert( pTab!=0 );
1072 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
danielk1977b3bce662005-01-29 08:32:43 +00001073 assert( pTab->nCol>0 );
1074 if( zTab ){
1075 if( pItem->zAlias ){
1076 char *zTabName = pItem->zAlias;
1077 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
1078 }else{
1079 char *zTabName = pTab->zName;
1080 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
danielk1977da184232006-01-05 11:34:32 +00001081 if( zDb!=0 && sqlite3StrICmp(db->aDb[iDb].zName, zDb)!=0 ){
danielk1977b3bce662005-01-29 08:32:43 +00001082 continue;
1083 }
drh626a8792005-01-17 22:08:19 +00001084 }
drh8141f612004-01-25 22:44:58 +00001085 }
danielk1977b3bce662005-01-29 08:32:43 +00001086 if( 0==(cntTab++) ){
drh626a8792005-01-17 22:08:19 +00001087 pExpr->iTable = pItem->iCursor;
drh728b5772007-09-18 15:55:07 +00001088 pSchema = pTab->pSchema;
danielk1977b3bce662005-01-29 08:32:43 +00001089 pMatch = pItem;
1090 }
1091 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
1092 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
danielk1977b3bf5562006-01-10 17:58:23 +00001093 const char *zColl = pTab->aCol[j].zColl;
drh873fac02005-06-06 17:11:46 +00001094 IdList *pUsing;
danielk1977b3bce662005-01-29 08:32:43 +00001095 cnt++;
1096 pExpr->iTable = pItem->iCursor;
1097 pMatch = pItem;
drh728b5772007-09-18 15:55:07 +00001098 pSchema = pTab->pSchema;
danielk1977b3bce662005-01-29 08:32:43 +00001099 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
1100 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
1101 pExpr->affinity = pTab->aCol[j].affinity;
drh8b4c40d2007-02-01 23:02:45 +00001102 if( (pExpr->flags & EP_ExpCollate)==0 ){
1103 pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0);
1104 }
drh61dfc312006-12-16 16:25:15 +00001105 if( i<pSrcList->nSrc-1 ){
1106 if( pItem[1].jointype & JT_NATURAL ){
1107 /* If this match occurred in the left table of a natural join,
1108 ** then skip the right table to avoid a duplicate match */
1109 pItem++;
1110 i++;
1111 }else if( (pUsing = pItem[1].pUsing)!=0 ){
1112 /* If this match occurs on a column that is in the USING clause
1113 ** of a join, skip the search of the right table of the join
1114 ** to avoid a duplicate match there. */
1115 int k;
1116 for(k=0; k<pUsing->nId; k++){
1117 if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ){
1118 pItem++;
1119 i++;
1120 break;
1121 }
drh873fac02005-06-06 17:11:46 +00001122 }
1123 }
1124 }
danielk1977b3bce662005-01-29 08:32:43 +00001125 break;
1126 }
drh8141f612004-01-25 22:44:58 +00001127 }
1128 }
1129 }
drh626a8792005-01-17 22:08:19 +00001130
1131#ifndef SQLITE_OMIT_TRIGGER
1132 /* If we have not already resolved the name, then maybe
1133 ** it is a new.* or old.* trigger argument reference
1134 */
1135 if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
1136 TriggerStack *pTriggerStack = pParse->trigStack;
1137 Table *pTab = 0;
danielk19778f2c54e2008-01-01 19:02:09 +00001138 u32 *piColMask;
drh626a8792005-01-17 22:08:19 +00001139 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){
1140 pExpr->iTable = pTriggerStack->newIdx;
1141 assert( pTriggerStack->pTab );
1142 pTab = pTriggerStack->pTab;
danielk19778f2c54e2008-01-01 19:02:09 +00001143 piColMask = &(pTriggerStack->newColMask);
drh626a8792005-01-17 22:08:19 +00001144 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab)==0 ){
1145 pExpr->iTable = pTriggerStack->oldIdx;
1146 assert( pTriggerStack->pTab );
1147 pTab = pTriggerStack->pTab;
danielk19778f2c54e2008-01-01 19:02:09 +00001148 piColMask = &(pTriggerStack->oldColMask);
drh626a8792005-01-17 22:08:19 +00001149 }
1150
1151 if( pTab ){
danielk1977f0113002006-01-24 12:09:17 +00001152 int iCol;
drh626a8792005-01-17 22:08:19 +00001153 Column *pCol = pTab->aCol;
1154
drh728b5772007-09-18 15:55:07 +00001155 pSchema = pTab->pSchema;
drh626a8792005-01-17 22:08:19 +00001156 cntTab++;
danielk1977f0113002006-01-24 12:09:17 +00001157 for(iCol=0; iCol < pTab->nCol; iCol++, pCol++) {
drh626a8792005-01-17 22:08:19 +00001158 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
danielk1977f0113002006-01-24 12:09:17 +00001159 const char *zColl = pTab->aCol[iCol].zColl;
drh626a8792005-01-17 22:08:19 +00001160 cnt++;
danielk1977f0113002006-01-24 12:09:17 +00001161 pExpr->iColumn = iCol==pTab->iPKey ? -1 : iCol;
1162 pExpr->affinity = pTab->aCol[iCol].affinity;
drh8b4c40d2007-02-01 23:02:45 +00001163 if( (pExpr->flags & EP_ExpCollate)==0 ){
1164 pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0);
1165 }
danielk1977aee18ef2005-03-09 12:26:50 +00001166 pExpr->pTab = pTab;
danielk19778f2c54e2008-01-01 19:02:09 +00001167 if( iCol>=0 ){
1168 *piColMask |= ((u32)1<<iCol) | (iCol>=32?0xffffffff:0);
1169 }
drh626a8792005-01-17 22:08:19 +00001170 break;
1171 }
1172 }
1173 }
1174 }
drhb7f91642004-10-31 02:22:47 +00001175#endif /* !defined(SQLITE_OMIT_TRIGGER) */
drh8141f612004-01-25 22:44:58 +00001176
drh626a8792005-01-17 22:08:19 +00001177 /*
1178 ** Perhaps the name is a reference to the ROWID
1179 */
1180 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
1181 cnt = 1;
1182 pExpr->iColumn = -1;
drh8a512562005-11-14 22:29:05 +00001183 pExpr->affinity = SQLITE_AFF_INTEGER;
drh626a8792005-01-17 22:08:19 +00001184 }
drh8141f612004-01-25 22:44:58 +00001185
drh626a8792005-01-17 22:08:19 +00001186 /*
1187 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
1188 ** might refer to an result-set alias. This happens, for example, when
1189 ** we are resolving names in the WHERE clause of the following command:
1190 **
1191 ** SELECT a+b AS x FROM table WHERE x<10;
1192 **
1193 ** In cases like this, replace pExpr with a copy of the expression that
1194 ** forms the result set entry ("a+b" in the example) and return immediately.
1195 ** Note that the expression in the result set should have already been
1196 ** resolved by the time the WHERE clause is resolved.
1197 */
drhffe07b22005-11-03 00:41:17 +00001198 if( cnt==0 && (pEList = pNC->pEList)!=0 && zTab==0 ){
drh626a8792005-01-17 22:08:19 +00001199 for(j=0; j<pEList->nExpr; j++){
1200 char *zAs = pEList->a[j].zName;
1201 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
drh36379e92007-07-23 22:51:15 +00001202 Expr *pDup, *pOrig;
drh626a8792005-01-17 22:08:19 +00001203 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
drh4f07e5f2007-05-14 11:34:46 +00001204 assert( pExpr->pList==0 );
1205 assert( pExpr->pSelect==0 );
drh36379e92007-07-23 22:51:15 +00001206 pOrig = pEList->a[j].pExpr;
1207 if( !pNC->allowAgg && ExprHasProperty(pOrig, EP_Agg) ){
1208 sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs);
drh17435752007-08-16 04:30:38 +00001209 sqlite3_free(zCol);
drh36379e92007-07-23 22:51:15 +00001210 return 2;
1211 }
danielk19771e536952007-08-16 10:09:01 +00001212 pDup = sqlite3ExprDup(db, pOrig);
drh4f07e5f2007-05-14 11:34:46 +00001213 if( pExpr->flags & EP_ExpCollate ){
1214 pDup->pColl = pExpr->pColl;
1215 pDup->flags |= EP_ExpCollate;
1216 }
drh17435752007-08-16 04:30:38 +00001217 if( pExpr->span.dyn ) sqlite3_free((char*)pExpr->span.z);
1218 if( pExpr->token.dyn ) sqlite3_free((char*)pExpr->token.z);
drh4f07e5f2007-05-14 11:34:46 +00001219 memcpy(pExpr, pDup, sizeof(*pExpr));
drh17435752007-08-16 04:30:38 +00001220 sqlite3_free(pDup);
drh15ccce12005-05-23 15:06:39 +00001221 cnt = 1;
danielk1977c9cf6e32007-06-25 16:29:33 +00001222 pMatch = 0;
drh626a8792005-01-17 22:08:19 +00001223 assert( zTab==0 && zDb==0 );
drh15ccce12005-05-23 15:06:39 +00001224 goto lookupname_end_2;
drh626a8792005-01-17 22:08:19 +00001225 }
1226 }
1227 }
1228
1229 /* Advance to the next name context. The loop will exit when either
1230 ** we have a match (cnt>0) or when we run out of name contexts.
1231 */
1232 if( cnt==0 ){
1233 pNC = pNC->pNext;
1234 }
drh8141f612004-01-25 22:44:58 +00001235 }
1236
1237 /*
1238 ** If X and Y are NULL (in other words if only the column name Z is
1239 ** supplied) and the value of Z is enclosed in double-quotes, then
1240 ** Z is a string literal if it doesn't match any column names. In that
1241 ** case, we need to return right away and not make any changes to
1242 ** pExpr.
drh15ccce12005-05-23 15:06:39 +00001243 **
1244 ** Because no reference was made to outer contexts, the pNC->nRef
1245 ** fields are not changed in any context.
drh8141f612004-01-25 22:44:58 +00001246 */
1247 if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
drh17435752007-08-16 04:30:38 +00001248 sqlite3_free(zCol);
drh8141f612004-01-25 22:44:58 +00001249 return 0;
1250 }
1251
1252 /*
1253 ** cnt==0 means there was not match. cnt>1 means there were two or
1254 ** more matches. Either way, we have an error.
1255 */
1256 if( cnt!=1 ){
drhde4fcfd2008-01-19 23:50:26 +00001257 const char *zErr;
1258 zErr = cnt==0 ? "no such column" : "ambiguous column name";
drh8141f612004-01-25 22:44:58 +00001259 if( zDb ){
drhde4fcfd2008-01-19 23:50:26 +00001260 sqlite3ErrorMsg(pParse, "%s: %s.%s.%s", zErr, zDb, zTab, zCol);
drh8141f612004-01-25 22:44:58 +00001261 }else if( zTab ){
drhde4fcfd2008-01-19 23:50:26 +00001262 sqlite3ErrorMsg(pParse, "%s: %s.%s", zErr, zTab, zCol);
drh8141f612004-01-25 22:44:58 +00001263 }else{
drhde4fcfd2008-01-19 23:50:26 +00001264 sqlite3ErrorMsg(pParse, "%s: %s", zErr, zCol);
drh8141f612004-01-25 22:44:58 +00001265 }
drhde4fcfd2008-01-19 23:50:26 +00001266 pTopNC->nErr++;
drh8141f612004-01-25 22:44:58 +00001267 }
1268
drh51669862004-12-18 18:40:26 +00001269 /* If a column from a table in pSrcList is referenced, then record
1270 ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes
1271 ** bit 0 to be set. Column 1 sets bit 1. And so forth. If the
1272 ** column number is greater than the number of bits in the bitmask
1273 ** then set the high-order bit of the bitmask.
1274 */
1275 if( pExpr->iColumn>=0 && pMatch!=0 ){
1276 int n = pExpr->iColumn;
1277 if( n>=sizeof(Bitmask)*8 ){
1278 n = sizeof(Bitmask)*8-1;
1279 }
1280 assert( pMatch->iCursor==pExpr->iTable );
drhca83ac52007-02-01 01:40:44 +00001281 pMatch->colUsed |= ((Bitmask)1)<<n;
drh51669862004-12-18 18:40:26 +00001282 }
1283
danielk1977d5d56522005-03-16 12:15:20 +00001284lookupname_end:
drh8141f612004-01-25 22:44:58 +00001285 /* Clean up and return
1286 */
drh17435752007-08-16 04:30:38 +00001287 sqlite3_free(zDb);
1288 sqlite3_free(zTab);
danielk19774adee202004-05-08 08:23:19 +00001289 sqlite3ExprDelete(pExpr->pLeft);
drh8141f612004-01-25 22:44:58 +00001290 pExpr->pLeft = 0;
danielk19774adee202004-05-08 08:23:19 +00001291 sqlite3ExprDelete(pExpr->pRight);
drh8141f612004-01-25 22:44:58 +00001292 pExpr->pRight = 0;
1293 pExpr->op = TK_COLUMN;
drh15ccce12005-05-23 15:06:39 +00001294lookupname_end_2:
drh17435752007-08-16 04:30:38 +00001295 sqlite3_free(zCol);
drh626a8792005-01-17 22:08:19 +00001296 if( cnt==1 ){
danielk1977b3bce662005-01-29 08:32:43 +00001297 assert( pNC!=0 );
drh728b5772007-09-18 15:55:07 +00001298 sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList);
danielk1977aee18ef2005-03-09 12:26:50 +00001299 if( pMatch && !pMatch->pSelect ){
1300 pExpr->pTab = pMatch->pTab;
1301 }
drh15ccce12005-05-23 15:06:39 +00001302 /* Increment the nRef value on all name contexts from TopNC up to
1303 ** the point where the name matched. */
1304 for(;;){
1305 assert( pTopNC!=0 );
1306 pTopNC->nRef++;
1307 if( pTopNC==pNC ) break;
1308 pTopNC = pTopNC->pNext;
1309 }
1310 return 0;
1311 } else {
1312 return 1;
drh626a8792005-01-17 22:08:19 +00001313 }
drh8141f612004-01-25 22:44:58 +00001314}
1315
1316/*
drh626a8792005-01-17 22:08:19 +00001317** This routine is designed as an xFunc for walkExprTree().
1318**
drh73b211a2005-01-18 04:00:42 +00001319** Resolve symbolic names into TK_COLUMN operators for the current
drh626a8792005-01-17 22:08:19 +00001320** node in the expression tree. Return 0 to continue the search down
drh73b211a2005-01-18 04:00:42 +00001321** the tree or 2 to abort the tree walk.
1322**
1323** This routine also does error checking and name resolution for
1324** function names. The operator for aggregate functions is changed
1325** to TK_AGG_FUNCTION.
drh626a8792005-01-17 22:08:19 +00001326*/
1327static int nameResolverStep(void *pArg, Expr *pExpr){
1328 NameContext *pNC = (NameContext*)pArg;
drh626a8792005-01-17 22:08:19 +00001329 Parse *pParse;
drh626a8792005-01-17 22:08:19 +00001330
danielk1977b3bce662005-01-29 08:32:43 +00001331 if( pExpr==0 ) return 1;
drh626a8792005-01-17 22:08:19 +00001332 assert( pNC!=0 );
drh626a8792005-01-17 22:08:19 +00001333 pParse = pNC->pParse;
danielk1977b3bce662005-01-29 08:32:43 +00001334
drh626a8792005-01-17 22:08:19 +00001335 if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return 1;
1336 ExprSetProperty(pExpr, EP_Resolved);
1337#ifndef NDEBUG
danielk1977f0113002006-01-24 12:09:17 +00001338 if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
1339 SrcList *pSrcList = pNC->pSrcList;
danielk1977940fac92005-01-23 22:41:37 +00001340 int i;
danielk1977f0113002006-01-24 12:09:17 +00001341 for(i=0; i<pNC->pSrcList->nSrc; i++){
drh626a8792005-01-17 22:08:19 +00001342 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
1343 }
1344 }
1345#endif
1346 switch( pExpr->op ){
1347 /* Double-quoted strings (ex: "abc") are used as identifiers if
1348 ** possible. Otherwise they remain as strings. Single-quoted
1349 ** strings (ex: 'abc') are always string literals.
1350 */
1351 case TK_STRING: {
1352 if( pExpr->token.z[0]=='\'' ) break;
1353 /* Fall thru into the TK_ID case if this is a double-quoted string */
1354 }
1355 /* A lone identifier is the name of a column.
1356 */
1357 case TK_ID: {
drh626a8792005-01-17 22:08:19 +00001358 lookupName(pParse, 0, 0, &pExpr->token, pNC, pExpr);
1359 return 1;
1360 }
1361
1362 /* A table name and column name: ID.ID
1363 ** Or a database, table and column: ID.ID.ID
1364 */
1365 case TK_DOT: {
1366 Token *pColumn;
1367 Token *pTable;
1368 Token *pDb;
1369 Expr *pRight;
1370
danielk1977b3bce662005-01-29 08:32:43 +00001371 /* if( pSrcList==0 ) break; */
drh626a8792005-01-17 22:08:19 +00001372 pRight = pExpr->pRight;
1373 if( pRight->op==TK_ID ){
1374 pDb = 0;
1375 pTable = &pExpr->pLeft->token;
1376 pColumn = &pRight->token;
1377 }else{
1378 assert( pRight->op==TK_DOT );
1379 pDb = &pExpr->pLeft->token;
1380 pTable = &pRight->pLeft->token;
1381 pColumn = &pRight->pRight->token;
1382 }
1383 lookupName(pParse, pDb, pTable, pColumn, pNC, pExpr);
1384 return 1;
1385 }
1386
1387 /* Resolve function names
1388 */
drhb71090f2005-05-23 17:26:51 +00001389 case TK_CONST_FUNC:
drh626a8792005-01-17 22:08:19 +00001390 case TK_FUNCTION: {
1391 ExprList *pList = pExpr->pList; /* The argument list */
1392 int n = pList ? pList->nExpr : 0; /* Number of arguments */
1393 int no_such_func = 0; /* True if no such function exists */
1394 int wrong_num_args = 0; /* True if wrong number of arguments */
1395 int is_agg = 0; /* True if is an aggregate function */
1396 int i;
drh5169bbc2006-08-24 14:59:45 +00001397 int auth; /* Authorization to use the function */
drh626a8792005-01-17 22:08:19 +00001398 int nId; /* Number of characters in function name */
1399 const char *zId; /* The function name. */
drh73b211a2005-01-18 04:00:42 +00001400 FuncDef *pDef; /* Information about the function */
danielk197714db2662006-01-09 16:12:04 +00001401 int enc = ENC(pParse->db); /* The database encoding */
drh626a8792005-01-17 22:08:19 +00001402
drh2646da72005-12-09 20:02:05 +00001403 zId = (char*)pExpr->token.z;
drhb71090f2005-05-23 17:26:51 +00001404 nId = pExpr->token.n;
drh626a8792005-01-17 22:08:19 +00001405 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
1406 if( pDef==0 ){
1407 pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
1408 if( pDef==0 ){
1409 no_such_func = 1;
1410 }else{
1411 wrong_num_args = 1;
1412 }
1413 }else{
1414 is_agg = pDef->xFunc==0;
1415 }
drh2fca7fe2006-11-23 11:59:13 +00001416#ifndef SQLITE_OMIT_AUTHORIZATION
drh5169bbc2006-08-24 14:59:45 +00001417 if( pDef ){
1418 auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0);
1419 if( auth!=SQLITE_OK ){
1420 if( auth==SQLITE_DENY ){
1421 sqlite3ErrorMsg(pParse, "not authorized to use function: %s",
1422 pDef->zName);
1423 pNC->nErr++;
1424 }
1425 pExpr->op = TK_NULL;
1426 return 1;
1427 }
1428 }
drhb8b14212006-08-24 15:18:25 +00001429#endif
drh626a8792005-01-17 22:08:19 +00001430 if( is_agg && !pNC->allowAgg ){
1431 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
1432 pNC->nErr++;
1433 is_agg = 0;
1434 }else if( no_such_func ){
1435 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
1436 pNC->nErr++;
1437 }else if( wrong_num_args ){
1438 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
1439 nId, zId);
1440 pNC->nErr++;
1441 }
1442 if( is_agg ){
1443 pExpr->op = TK_AGG_FUNCTION;
1444 pNC->hasAgg = 1;
1445 }
drh73b211a2005-01-18 04:00:42 +00001446 if( is_agg ) pNC->allowAgg = 0;
drh626a8792005-01-17 22:08:19 +00001447 for(i=0; pNC->nErr==0 && i<n; i++){
drh73b211a2005-01-18 04:00:42 +00001448 walkExprTree(pList->a[i].pExpr, nameResolverStep, pNC);
drh626a8792005-01-17 22:08:19 +00001449 }
drh73b211a2005-01-18 04:00:42 +00001450 if( is_agg ) pNC->allowAgg = 1;
drh626a8792005-01-17 22:08:19 +00001451 /* FIX ME: Compute pExpr->affinity based on the expected return
1452 ** type of the function
1453 */
1454 return is_agg;
1455 }
danielk1977b3bce662005-01-29 08:32:43 +00001456#ifndef SQLITE_OMIT_SUBQUERY
1457 case TK_SELECT:
1458 case TK_EXISTS:
1459#endif
1460 case TK_IN: {
1461 if( pExpr->pSelect ){
drh8a9f38f2005-11-05 15:07:55 +00001462 int nRef = pNC->nRef;
drh06f65412005-11-03 02:03:13 +00001463#ifndef SQLITE_OMIT_CHECK
1464 if( pNC->isCheck ){
1465 sqlite3ErrorMsg(pParse,"subqueries prohibited in CHECK constraints");
1466 }
1467#endif
danielk1977b3bce662005-01-29 08:32:43 +00001468 sqlite3SelectResolve(pParse, pExpr->pSelect, pNC);
1469 assert( pNC->nRef>=nRef );
1470 if( nRef!=pNC->nRef ){
1471 ExprSetProperty(pExpr, EP_VarSelect);
1472 }
1473 }
drh4284fb02005-11-03 12:33:28 +00001474 break;
danielk1977b3bce662005-01-29 08:32:43 +00001475 }
drh4284fb02005-11-03 12:33:28 +00001476#ifndef SQLITE_OMIT_CHECK
1477 case TK_VARIABLE: {
1478 if( pNC->isCheck ){
1479 sqlite3ErrorMsg(pParse,"parameters prohibited in CHECK constraints");
1480 }
1481 break;
1482 }
1483#endif
drh626a8792005-01-17 22:08:19 +00001484 }
1485 return 0;
1486}
1487
1488/*
drhcce7d172000-05-31 15:34:51 +00001489** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +00001490** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +00001491** index to the table in the table list and a column offset. The
1492** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
1493** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +00001494** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +00001495** VDBE cursor number for a cursor that is pointing into the referenced
1496** table. The Expr.iColumn value is changed to the index of the column
1497** of the referenced table. The Expr.iColumn value for the special
1498** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
1499** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +00001500**
drh626a8792005-01-17 22:08:19 +00001501** Also resolve function names and check the functions for proper
1502** usage. Make sure all function names are recognized and all functions
1503** have the correct number of arguments. Leave an error message
1504** in pParse->zErrMsg if anything is amiss. Return the number of errors.
1505**
drh73b211a2005-01-18 04:00:42 +00001506** If the expression contains aggregate functions then set the EP_Agg
1507** property on the expression.
drh626a8792005-01-17 22:08:19 +00001508*/
danielk1977fc976062007-05-10 10:46:56 +00001509int sqlite3ExprResolveNames(
danielk1977b3bce662005-01-29 08:32:43 +00001510 NameContext *pNC, /* Namespace to resolve expressions in. */
1511 Expr *pExpr /* The expression to be analyzed. */
drh626a8792005-01-17 22:08:19 +00001512){
drh13449892005-09-07 21:22:45 +00001513 int savedHasAgg;
drhbb4957f2008-03-20 14:03:29 +00001514
drh73b211a2005-01-18 04:00:42 +00001515 if( pExpr==0 ) return 0;
drhbb4957f2008-03-20 14:03:29 +00001516#if SQLITE_MAX_EXPR_DEPTH>0
1517 {
1518 int mxDepth = pNC->pParse->db->aLimit[SQLITE_LIMIT_EXPR_DEPTH];
1519 if( (pExpr->nHeight+pNC->pParse->nHeight)>mxDepth ){
1520 sqlite3ErrorMsg(pNC->pParse,
1521 "Expression tree is too large (maximum depth %d)", mxDepth
1522 );
1523 return 1;
1524 }
1525 pNC->pParse->nHeight += pExpr->nHeight;
danielk1977fc976062007-05-10 10:46:56 +00001526 }
danielk1977fc976062007-05-10 10:46:56 +00001527#endif
drh13449892005-09-07 21:22:45 +00001528 savedHasAgg = pNC->hasAgg;
1529 pNC->hasAgg = 0;
danielk1977b3bce662005-01-29 08:32:43 +00001530 walkExprTree(pExpr, nameResolverStep, pNC);
drhbb4957f2008-03-20 14:03:29 +00001531#if SQLITE_MAX_EXPR_DEPTH>0
danielk1977fc976062007-05-10 10:46:56 +00001532 pNC->pParse->nHeight -= pExpr->nHeight;
1533#endif
danielk1977b3bce662005-01-29 08:32:43 +00001534 if( pNC->nErr>0 ){
drh73b211a2005-01-18 04:00:42 +00001535 ExprSetProperty(pExpr, EP_Error);
drh73b211a2005-01-18 04:00:42 +00001536 }
drh13449892005-09-07 21:22:45 +00001537 if( pNC->hasAgg ){
1538 ExprSetProperty(pExpr, EP_Agg);
1539 }else if( savedHasAgg ){
1540 pNC->hasAgg = 1;
1541 }
drh73b211a2005-01-18 04:00:42 +00001542 return ExprHasProperty(pExpr, EP_Error);
drh626a8792005-01-17 22:08:19 +00001543}
1544
drh1398ad32005-01-19 23:24:50 +00001545/*
1546** A pointer instance of this structure is used to pass information
1547** through walkExprTree into codeSubqueryStep().
1548*/
1549typedef struct QueryCoder QueryCoder;
1550struct QueryCoder {
1551 Parse *pParse; /* The parsing context */
1552 NameContext *pNC; /* Namespace of first enclosing query */
1553};
1554
danielk19779a96b662007-11-29 17:05:18 +00001555#ifdef SQLITE_TEST
1556 int sqlite3_enable_in_opt = 1;
1557#else
1558 #define sqlite3_enable_in_opt 1
1559#endif
1560
1561/*
1562** This function is used by the implementation of the IN (...) operator.
1563** It's job is to find or create a b-tree structure that may be used
1564** either to test for membership of the (...) set or to iterate through
drh85b623f2007-12-13 21:54:09 +00001565** its members, skipping duplicates.
danielk19779a96b662007-11-29 17:05:18 +00001566**
1567** The cursor opened on the structure (database table, database index
1568** or ephermal table) is stored in pX->iTable before this function returns.
1569** The returned value indicates the structure type, as follows:
1570**
1571** IN_INDEX_ROWID - The cursor was opened on a database table.
drh2d401ab2008-01-10 23:50:11 +00001572** IN_INDEX_INDEX - The cursor was opened on a database index.
danielk19779a96b662007-11-29 17:05:18 +00001573** IN_INDEX_EPH - The cursor was opened on a specially created and
1574** populated epheremal table.
1575**
1576** An existing structure may only be used if the SELECT is of the simple
1577** form:
1578**
1579** SELECT <column> FROM <table>
1580**
1581** If the mustBeUnique parameter is false, the structure will be used
1582** for fast set membership tests. In this case an epheremal table must
1583** be used unless <column> is an INTEGER PRIMARY KEY or an index can
drh85b623f2007-12-13 21:54:09 +00001584** be found with <column> as its left-most column.
danielk19779a96b662007-11-29 17:05:18 +00001585**
1586** If mustBeUnique is true, then the structure will be used to iterate
1587** through the set members, skipping any duplicates. In this case an
1588** epheremal table must be used unless the selected <column> is guaranteed
1589** to be unique - either because it is an INTEGER PRIMARY KEY or it
1590** is unique by virtue of a constraint or implicit index.
1591*/
danielk1977284f4ac2007-12-10 05:03:46 +00001592#ifndef SQLITE_OMIT_SUBQUERY
danielk19779a96b662007-11-29 17:05:18 +00001593int sqlite3FindInIndex(Parse *pParse, Expr *pX, int mustBeUnique){
1594 Select *p;
1595 int eType = 0;
1596 int iTab = pParse->nTab++;
1597
1598 /* The follwing if(...) expression is true if the SELECT is of the
1599 ** simple form:
1600 **
1601 ** SELECT <column> FROM <table>
1602 **
1603 ** If this is the case, it may be possible to use an existing table
1604 ** or index instead of generating an epheremal table.
1605 */
1606 if( sqlite3_enable_in_opt
drhc81945e2008-03-10 14:12:53 +00001607 && (p=pX->pSelect)!=0 && !p->pPrior
danielk19779a96b662007-11-29 17:05:18 +00001608 && !p->isDistinct && !p->isAgg && !p->pGroupBy
1609 && p->pSrc && p->pSrc->nSrc==1 && !p->pSrc->a[0].pSelect
danielk1977b2b95d42008-03-12 10:39:00 +00001610 && p->pSrc->a[0].pTab && !p->pSrc->a[0].pTab->pSelect
danielk19779a96b662007-11-29 17:05:18 +00001611 && p->pEList->nExpr==1 && p->pEList->a[0].pExpr->op==TK_COLUMN
1612 && !p->pLimit && !p->pOffset && !p->pWhere
1613 ){
1614 sqlite3 *db = pParse->db;
1615 Index *pIdx;
1616 Expr *pExpr = p->pEList->a[0].pExpr;
1617 int iCol = pExpr->iColumn;
1618 Vdbe *v = sqlite3GetVdbe(pParse);
1619
1620 /* This function is only called from two places. In both cases the vdbe
1621 ** has already been allocated. So assume sqlite3GetVdbe() is always
1622 ** successful here.
1623 */
1624 assert(v);
1625 if( iCol<0 ){
drh0a07c102008-01-03 18:03:08 +00001626 int iMem = ++pParse->nMem;
danielk19779a96b662007-11-29 17:05:18 +00001627 int iAddr;
1628 Table *pTab = p->pSrc->a[0].pTab;
1629 int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
1630 sqlite3VdbeUsesBtree(v, iDb);
1631
drh892d3172008-01-10 03:46:36 +00001632 iAddr = sqlite3VdbeAddOp1(v, OP_If, iMem);
drh4c583122008-01-04 22:01:03 +00001633 sqlite3VdbeAddOp2(v, OP_Integer, 1, iMem);
danielk19779a96b662007-11-29 17:05:18 +00001634
1635 sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
1636 eType = IN_INDEX_ROWID;
1637
1638 sqlite3VdbeJumpHere(v, iAddr);
1639 }else{
1640 /* The collation sequence used by the comparison. If an index is to
1641 ** be used in place of a temp-table, it must be ordered according
1642 ** to this collation sequence.
1643 */
1644 CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pExpr);
1645
1646 /* Check that the affinity that will be used to perform the
1647 ** comparison is the same as the affinity of the column. If
1648 ** it is not, it is not possible to use any index.
1649 */
1650 Table *pTab = p->pSrc->a[0].pTab;
1651 char aff = comparisonAffinity(pX);
1652 int affinity_ok = (pTab->aCol[iCol].affinity==aff||aff==SQLITE_AFF_NONE);
1653
1654 for(pIdx=pTab->pIndex; pIdx && eType==0 && affinity_ok; pIdx=pIdx->pNext){
1655 if( (pIdx->aiColumn[0]==iCol)
1656 && (pReq==sqlite3FindCollSeq(db, ENC(db), pIdx->azColl[0], -1, 0))
1657 && (!mustBeUnique || (pIdx->nColumn==1 && pIdx->onError!=OE_None))
1658 ){
1659 int iDb;
drh0a07c102008-01-03 18:03:08 +00001660 int iMem = ++pParse->nMem;
danielk19779a96b662007-11-29 17:05:18 +00001661 int iAddr;
1662 char *pKey;
1663
1664 pKey = (char *)sqlite3IndexKeyinfo(pParse, pIdx);
1665 iDb = sqlite3SchemaToIndex(db, pIdx->pSchema);
1666 sqlite3VdbeUsesBtree(v, iDb);
1667
drh892d3172008-01-10 03:46:36 +00001668 iAddr = sqlite3VdbeAddOp1(v, OP_If, iMem);
drh4c583122008-01-04 22:01:03 +00001669 sqlite3VdbeAddOp2(v, OP_Integer, 1, iMem);
danielk19779a96b662007-11-29 17:05:18 +00001670
danielk1977cd3e8f72008-03-25 09:47:35 +00001671 sqlite3VdbeAddOp2(v, OP_SetNumColumns, 0, pIdx->nColumn);
danielk1977207872a2008-01-03 07:54:23 +00001672 sqlite3VdbeAddOp4(v, OP_OpenRead, iTab, pIdx->tnum, iDb,
drh66a51672008-01-03 00:01:23 +00001673 pKey,P4_KEYINFO_HANDOFF);
danielk1977207872a2008-01-03 07:54:23 +00001674 VdbeComment((v, "%s", pIdx->zName));
danielk19779a96b662007-11-29 17:05:18 +00001675 eType = IN_INDEX_INDEX;
danielk19779a96b662007-11-29 17:05:18 +00001676
1677 sqlite3VdbeJumpHere(v, iAddr);
1678 }
1679 }
1680 }
1681 }
1682
1683 if( eType==0 ){
1684 sqlite3CodeSubselect(pParse, pX);
1685 eType = IN_INDEX_EPH;
1686 }else{
1687 pX->iTable = iTab;
1688 }
1689 return eType;
1690}
danielk1977284f4ac2007-12-10 05:03:46 +00001691#endif
drh626a8792005-01-17 22:08:19 +00001692
1693/*
drh9cbe6352005-11-29 03:13:21 +00001694** Generate code for scalar subqueries used as an expression
1695** and IN operators. Examples:
drh626a8792005-01-17 22:08:19 +00001696**
drh9cbe6352005-11-29 03:13:21 +00001697** (SELECT a FROM b) -- subquery
1698** EXISTS (SELECT a FROM b) -- EXISTS subquery
1699** x IN (4,5,11) -- IN operator with list on right-hand side
1700** x IN (SELECT a FROM b) -- IN operator with subquery on the right
drhfef52082000-06-06 01:50:43 +00001701**
drh9cbe6352005-11-29 03:13:21 +00001702** The pExpr parameter describes the expression that contains the IN
1703** operator or subquery.
drhcce7d172000-05-31 15:34:51 +00001704*/
drh51522cd2005-01-20 13:36:19 +00001705#ifndef SQLITE_OMIT_SUBQUERY
danielk1977b3bce662005-01-29 08:32:43 +00001706void sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){
drh57dbd7b2005-07-08 18:25:26 +00001707 int testAddr = 0; /* One-time test address */
danielk1977b3bce662005-01-29 08:32:43 +00001708 Vdbe *v = sqlite3GetVdbe(pParse);
1709 if( v==0 ) return;
1710
danielk1977fc976062007-05-10 10:46:56 +00001711
drh57dbd7b2005-07-08 18:25:26 +00001712 /* This code must be run in its entirety every time it is encountered
1713 ** if any of the following is true:
1714 **
1715 ** * The right-hand side is a correlated subquery
1716 ** * The right-hand side is an expression list containing variables
1717 ** * We are inside a trigger
1718 **
1719 ** If all of the above are false, then we can run this code just once
1720 ** save the results, and reuse the same result on subsequent invocations.
danielk1977b3bce662005-01-29 08:32:43 +00001721 */
1722 if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->trigStack ){
drh0a07c102008-01-03 18:03:08 +00001723 int mem = ++pParse->nMem;
drh892d3172008-01-10 03:46:36 +00001724 sqlite3VdbeAddOp1(v, OP_If, mem);
1725 testAddr = sqlite3VdbeAddOp2(v, OP_Integer, 1, mem);
drh17435752007-08-16 04:30:38 +00001726 assert( testAddr>0 || pParse->db->mallocFailed );
danielk1977b3bce662005-01-29 08:32:43 +00001727 }
1728
drhcce7d172000-05-31 15:34:51 +00001729 switch( pExpr->op ){
drhfef52082000-06-06 01:50:43 +00001730 case TK_IN: {
danielk1977e014a832004-05-17 10:48:57 +00001731 char affinity;
drhd3d39e92004-05-20 22:16:29 +00001732 KeyInfo keyInfo;
drhb9bb7c12006-06-11 23:41:55 +00001733 int addr; /* Address of OP_OpenEphemeral instruction */
drhd3d39e92004-05-20 22:16:29 +00001734
danielk1977bf3b7212004-05-18 10:06:24 +00001735 affinity = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +00001736
1737 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
drh57dbd7b2005-07-08 18:25:26 +00001738 ** expression it is handled the same way. A virtual table is
danielk1977e014a832004-05-17 10:48:57 +00001739 ** filled with single-field index keys representing the results
1740 ** from the SELECT or the <exprlist>.
1741 **
1742 ** If the 'x' expression is a column value, or the SELECT...
1743 ** statement returns a column value, then the affinity of that
1744 ** column is used to build the index keys. If both 'x' and the
1745 ** SELECT... statement are columns, then numeric affinity is used
1746 ** if either column has NUMERIC or INTEGER affinity. If neither
1747 ** 'x' nor the SELECT... statement are columns, then numeric affinity
1748 ** is used.
1749 */
1750 pExpr->iTable = pParse->nTab++;
danielk1977cd3e8f72008-03-25 09:47:35 +00001751 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pExpr->iTable, 1);
drhd3d39e92004-05-20 22:16:29 +00001752 memset(&keyInfo, 0, sizeof(keyInfo));
1753 keyInfo.nField = 1;
danielk1977e014a832004-05-17 10:48:57 +00001754
drhfef52082000-06-06 01:50:43 +00001755 if( pExpr->pSelect ){
1756 /* Case 1: expr IN (SELECT ...)
1757 **
danielk1977e014a832004-05-17 10:48:57 +00001758 ** Generate code to write the results of the select into the temporary
1759 ** table allocated and opened above.
drhfef52082000-06-06 01:50:43 +00001760 */
drh1013c932008-01-06 00:25:21 +00001761 SelectDest dest;
drhbe5c89a2004-07-26 00:31:09 +00001762 ExprList *pEList;
drh1013c932008-01-06 00:25:21 +00001763
1764 sqlite3SelectDestInit(&dest, SRT_Set, pExpr->iTable);
1765 dest.affinity = (int)affinity;
danielk1977e014a832004-05-17 10:48:57 +00001766 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
danielk19776c8c8ce2008-01-02 16:27:09 +00001767 if( sqlite3Select(pParse, pExpr->pSelect, &dest, 0, 0, 0, 0) ){
drh94ccde52007-04-13 16:06:32 +00001768 return;
1769 }
drhbe5c89a2004-07-26 00:31:09 +00001770 pEList = pExpr->pSelect->pEList;
1771 if( pEList && pEList->nExpr>0 ){
danielk1977bcbb04e2007-05-29 12:11:29 +00001772 keyInfo.aColl[0] = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft,
drhbe5c89a2004-07-26 00:31:09 +00001773 pEList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001774 }
drhfef52082000-06-06 01:50:43 +00001775 }else if( pExpr->pList ){
1776 /* Case 2: expr IN (exprlist)
1777 **
drhfd131da2007-08-07 17:13:03 +00001778 ** For each expression, build an index key from the evaluation and
danielk1977e014a832004-05-17 10:48:57 +00001779 ** store it in the temporary table. If <expr> is a column, then use
1780 ** that columns affinity when building index keys. If <expr> is not
1781 ** a column, use numeric affinity.
drhfef52082000-06-06 01:50:43 +00001782 */
danielk1977e014a832004-05-17 10:48:57 +00001783 int i;
drh57dbd7b2005-07-08 18:25:26 +00001784 ExprList *pList = pExpr->pList;
1785 struct ExprList_item *pItem;
drh2d401ab2008-01-10 23:50:11 +00001786 int r1, r2;
drh57dbd7b2005-07-08 18:25:26 +00001787
danielk1977e014a832004-05-17 10:48:57 +00001788 if( !affinity ){
drh8159a352006-05-23 23:22:29 +00001789 affinity = SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +00001790 }
danielk19770202b292004-06-09 09:55:16 +00001791 keyInfo.aColl[0] = pExpr->pLeft->pColl;
danielk1977e014a832004-05-17 10:48:57 +00001792
1793 /* Loop through each expression in <exprlist>. */
drh2d401ab2008-01-10 23:50:11 +00001794 r1 = sqlite3GetTempReg(pParse);
1795 r2 = sqlite3GetTempReg(pParse);
drh57dbd7b2005-07-08 18:25:26 +00001796 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
1797 Expr *pE2 = pItem->pExpr;
danielk1977e014a832004-05-17 10:48:57 +00001798
drh57dbd7b2005-07-08 18:25:26 +00001799 /* If the expression is not constant then we will need to
1800 ** disable the test that was generated above that makes sure
1801 ** this code only executes once. Because for a non-constant
1802 ** expression we need to rerun this code each time.
1803 */
drh892d3172008-01-10 03:46:36 +00001804 if( testAddr && !sqlite3ExprIsConstant(pE2) ){
1805 sqlite3VdbeChangeToNoop(v, testAddr-1, 2);
drh57dbd7b2005-07-08 18:25:26 +00001806 testAddr = 0;
drh4794b982000-06-06 13:54:14 +00001807 }
danielk1977e014a832004-05-17 10:48:57 +00001808
1809 /* Evaluate the expression and insert it into the temp table */
drhe55cbd72008-03-31 23:48:03 +00001810 pParse->disableColCache++;
drh2d401ab2008-01-10 23:50:11 +00001811 sqlite3ExprCode(pParse, pE2, r1);
drhe55cbd72008-03-31 23:48:03 +00001812 pParse->disableColCache--;
drh1db639c2008-01-17 02:36:28 +00001813 sqlite3VdbeAddOp4(v, OP_MakeRecord, r1, 1, r2, &affinity, 1);
drhda250ea2008-04-01 05:07:14 +00001814 sqlite3ExprCacheAffinityChange(pParse, r1, 1);
drh2d401ab2008-01-10 23:50:11 +00001815 sqlite3VdbeAddOp2(v, OP_IdxInsert, pExpr->iTable, r2);
drhfef52082000-06-06 01:50:43 +00001816 }
drh2d401ab2008-01-10 23:50:11 +00001817 sqlite3ReleaseTempReg(pParse, r1);
1818 sqlite3ReleaseTempReg(pParse, r2);
drhfef52082000-06-06 01:50:43 +00001819 }
drh66a51672008-01-03 00:01:23 +00001820 sqlite3VdbeChangeP4(v, addr, (void *)&keyInfo, P4_KEYINFO);
danielk1977b3bce662005-01-29 08:32:43 +00001821 break;
drhfef52082000-06-06 01:50:43 +00001822 }
1823
drh51522cd2005-01-20 13:36:19 +00001824 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001825 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +00001826 /* This has to be a scalar SELECT. Generate code to put the
1827 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +00001828 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +00001829 */
drh2646da72005-12-09 20:02:05 +00001830 static const Token one = { (u8*)"1", 0, 1 };
drh51522cd2005-01-20 13:36:19 +00001831 Select *pSel;
danielk19776c8c8ce2008-01-02 16:27:09 +00001832 SelectDest dest;
drh1398ad32005-01-19 23:24:50 +00001833
drh51522cd2005-01-20 13:36:19 +00001834 pSel = pExpr->pSelect;
drh1013c932008-01-06 00:25:21 +00001835 sqlite3SelectDestInit(&dest, 0, ++pParse->nMem);
drh51522cd2005-01-20 13:36:19 +00001836 if( pExpr->op==TK_SELECT ){
danielk19776c8c8ce2008-01-02 16:27:09 +00001837 dest.eDest = SRT_Mem;
drh4c583122008-01-04 22:01:03 +00001838 sqlite3VdbeAddOp2(v, OP_Null, 0, dest.iParm);
drhd4e70eb2008-01-02 00:34:36 +00001839 VdbeComment((v, "Init subquery result"));
drh51522cd2005-01-20 13:36:19 +00001840 }else{
danielk19776c8c8ce2008-01-02 16:27:09 +00001841 dest.eDest = SRT_Exists;
drh4c583122008-01-04 22:01:03 +00001842 sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iParm);
drhd4e70eb2008-01-02 00:34:36 +00001843 VdbeComment((v, "Init EXISTS result"));
drh51522cd2005-01-20 13:36:19 +00001844 }
drhec7429a2005-10-06 16:53:14 +00001845 sqlite3ExprDelete(pSel->pLimit);
danielk1977a1644fd2007-08-29 12:31:25 +00001846 pSel->pLimit = sqlite3PExpr(pParse, TK_INTEGER, 0, 0, &one);
danielk19776c8c8ce2008-01-02 16:27:09 +00001847 if( sqlite3Select(pParse, pSel, &dest, 0, 0, 0, 0) ){
drh94ccde52007-04-13 16:06:32 +00001848 return;
1849 }
danielk19776c8c8ce2008-01-02 16:27:09 +00001850 pExpr->iColumn = dest.iParm;
danielk1977b3bce662005-01-29 08:32:43 +00001851 break;
drhcce7d172000-05-31 15:34:51 +00001852 }
1853 }
danielk1977b3bce662005-01-29 08:32:43 +00001854
drh57dbd7b2005-07-08 18:25:26 +00001855 if( testAddr ){
drh892d3172008-01-10 03:46:36 +00001856 sqlite3VdbeJumpHere(v, testAddr-1);
danielk1977b3bce662005-01-29 08:32:43 +00001857 }
danielk1977fc976062007-05-10 10:46:56 +00001858
danielk1977b3bce662005-01-29 08:32:43 +00001859 return;
drhcce7d172000-05-31 15:34:51 +00001860}
drh51522cd2005-01-20 13:36:19 +00001861#endif /* SQLITE_OMIT_SUBQUERY */
drhcce7d172000-05-31 15:34:51 +00001862
drhcce7d172000-05-31 15:34:51 +00001863/*
drh598f1342007-10-23 15:39:45 +00001864** Duplicate an 8-byte value
1865*/
1866static char *dup8bytes(Vdbe *v, const char *in){
1867 char *out = sqlite3DbMallocRaw(sqlite3VdbeDb(v), 8);
1868 if( out ){
1869 memcpy(out, in, 8);
1870 }
1871 return out;
1872}
1873
1874/*
1875** Generate an instruction that will put the floating point
drh9cbf3422008-01-17 16:22:13 +00001876** value described by z[0..n-1] into register iMem.
drh0cf19ed2007-10-23 18:55:48 +00001877**
1878** The z[] string will probably not be zero-terminated. But the
1879** z[n] character is guaranteed to be something that does not look
1880** like the continuation of the number.
drh598f1342007-10-23 15:39:45 +00001881*/
drh9de221d2008-01-05 06:51:30 +00001882static void codeReal(Vdbe *v, const char *z, int n, int negateFlag, int iMem){
drh598f1342007-10-23 15:39:45 +00001883 assert( z || v==0 || sqlite3VdbeDb(v)->mallocFailed );
1884 if( z ){
1885 double value;
1886 char *zV;
drh0cf19ed2007-10-23 18:55:48 +00001887 assert( !isdigit(z[n]) );
drh598f1342007-10-23 15:39:45 +00001888 sqlite3AtoF(z, &value);
1889 if( negateFlag ) value = -value;
1890 zV = dup8bytes(v, (char*)&value);
drh9de221d2008-01-05 06:51:30 +00001891 sqlite3VdbeAddOp4(v, OP_Real, 0, iMem, 0, zV, P4_REAL);
drh598f1342007-10-23 15:39:45 +00001892 }
1893}
1894
1895
1896/*
drhfec19aa2004-05-19 20:41:03 +00001897** Generate an instruction that will put the integer describe by
drh9cbf3422008-01-17 16:22:13 +00001898** text z[0..n-1] into register iMem.
drh0cf19ed2007-10-23 18:55:48 +00001899**
1900** The z[] string will probably not be zero-terminated. But the
1901** z[n] character is guaranteed to be something that does not look
1902** like the continuation of the number.
drhfec19aa2004-05-19 20:41:03 +00001903*/
drh9de221d2008-01-05 06:51:30 +00001904static void codeInteger(Vdbe *v, const char *z, int n, int negFlag, int iMem){
drhabb6fca2007-08-16 12:24:01 +00001905 assert( z || v==0 || sqlite3VdbeDb(v)->mallocFailed );
danielk1977c9cf9012007-05-30 10:36:47 +00001906 if( z ){
1907 int i;
drh0cf19ed2007-10-23 18:55:48 +00001908 assert( !isdigit(z[n]) );
danielk1977c9cf9012007-05-30 10:36:47 +00001909 if( sqlite3GetInt32(z, &i) ){
drh9de221d2008-01-05 06:51:30 +00001910 if( negFlag ) i = -i;
1911 sqlite3VdbeAddOp2(v, OP_Integer, i, iMem);
1912 }else if( sqlite3FitsIn64Bits(z, negFlag) ){
drh598f1342007-10-23 15:39:45 +00001913 i64 value;
1914 char *zV;
1915 sqlite3Atoi64(z, &value);
drh9de221d2008-01-05 06:51:30 +00001916 if( negFlag ) value = -value;
drh598f1342007-10-23 15:39:45 +00001917 zV = dup8bytes(v, (char*)&value);
drh9de221d2008-01-05 06:51:30 +00001918 sqlite3VdbeAddOp4(v, OP_Int64, 0, iMem, 0, zV, P4_INT64);
danielk1977c9cf9012007-05-30 10:36:47 +00001919 }else{
drh9de221d2008-01-05 06:51:30 +00001920 codeReal(v, z, n, negFlag, iMem);
danielk1977c9cf9012007-05-30 10:36:47 +00001921 }
drhfec19aa2004-05-19 20:41:03 +00001922 }
1923}
1924
drh945498f2007-02-24 11:52:52 +00001925
1926/*
1927** Generate code that will extract the iColumn-th column from
drhe55cbd72008-03-31 23:48:03 +00001928** table pTab and store the column value in a register. An effort
1929** is made to store the column value in register iReg, but this is
1930** not guaranteed. The location of the column value is returned.
1931**
1932** There must be an open cursor to pTab in iTable when this routine
1933** is called. If iColumn<0 then code is generated that extracts the rowid.
drhda250ea2008-04-01 05:07:14 +00001934**
1935** This routine might attempt to reuse the value of the column that
1936** has already been loaded into a register. The value will always
1937** be used if it has not undergone any affinity changes. But if
1938** an affinity change has occurred, then the cached value will only be
1939** used if allowAffChng is true.
drh945498f2007-02-24 11:52:52 +00001940*/
drhe55cbd72008-03-31 23:48:03 +00001941int sqlite3ExprCodeGetColumn(
1942 Parse *pParse, /* Parsing and code generating context */
drh2133d822008-01-03 18:44:59 +00001943 Table *pTab, /* Description of the table we are reading from */
1944 int iColumn, /* Index of the table column */
1945 int iTable, /* The cursor pointing to the table */
drhda250ea2008-04-01 05:07:14 +00001946 int iReg, /* Store results here */
1947 int allowAffChng /* True if prior affinity changes are OK */
drh2133d822008-01-03 18:44:59 +00001948){
drhe55cbd72008-03-31 23:48:03 +00001949 Vdbe *v = pParse->pVdbe;
1950 int i;
drhda250ea2008-04-01 05:07:14 +00001951 struct yColCache *p;
drhe55cbd72008-03-31 23:48:03 +00001952
drhda250ea2008-04-01 05:07:14 +00001953 for(i=0, p=pParse->aColCache; i<pParse->nColCache; i++, p++){
1954 if( p->iTable==iTable && p->iColumn==iColumn
1955 && (!p->affChange || allowAffChng) ){
drhe55cbd72008-03-31 23:48:03 +00001956#if 0
1957 sqlite3VdbeAddOp0(v, OP_Noop);
drhda250ea2008-04-01 05:07:14 +00001958 VdbeComment((v, "OPT: tab%d.col%d -> r%d", iTable, iColumn, p->iReg));
drhe55cbd72008-03-31 23:48:03 +00001959#endif
drhda250ea2008-04-01 05:07:14 +00001960 return p->iReg;
drhe55cbd72008-03-31 23:48:03 +00001961 }
1962 }
1963 assert( v!=0 );
drh945498f2007-02-24 11:52:52 +00001964 if( iColumn<0 ){
1965 int op = (pTab && IsVirtual(pTab)) ? OP_VRowid : OP_Rowid;
drh2133d822008-01-03 18:44:59 +00001966 sqlite3VdbeAddOp2(v, op, iTable, iReg);
drh945498f2007-02-24 11:52:52 +00001967 }else if( pTab==0 ){
drh2133d822008-01-03 18:44:59 +00001968 sqlite3VdbeAddOp3(v, OP_Column, iTable, iColumn, iReg);
drh945498f2007-02-24 11:52:52 +00001969 }else{
1970 int op = IsVirtual(pTab) ? OP_VColumn : OP_Column;
drh2133d822008-01-03 18:44:59 +00001971 sqlite3VdbeAddOp3(v, op, iTable, iColumn, iReg);
drh945498f2007-02-24 11:52:52 +00001972 sqlite3ColumnDefault(v, pTab, iColumn);
1973#ifndef SQLITE_OMIT_FLOATING_POINT
1974 if( pTab->aCol[iColumn].affinity==SQLITE_AFF_REAL ){
drh2133d822008-01-03 18:44:59 +00001975 sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg);
drh945498f2007-02-24 11:52:52 +00001976 }
1977#endif
1978 }
drhe55cbd72008-03-31 23:48:03 +00001979 if( pParse->disableColCache==0 ){
1980 i = pParse->iColCache;
drhda250ea2008-04-01 05:07:14 +00001981 p = &pParse->aColCache[i];
1982 p->iTable = iTable;
1983 p->iColumn = iColumn;
1984 p->iReg = iReg;
drhe55cbd72008-03-31 23:48:03 +00001985 i++;
drh2f7794c2008-04-01 03:27:39 +00001986 if( i>=ArraySize(pParse->aColCache) ) i = 0;
drhe55cbd72008-03-31 23:48:03 +00001987 if( i>pParse->nColCache ) pParse->nColCache = i;
drh2f7794c2008-04-01 03:27:39 +00001988 pParse->iColCache = i;
drhe55cbd72008-03-31 23:48:03 +00001989 }
1990 return iReg;
1991}
1992
1993/*
1994** Disable (+1) or enable (-1) the adding of new column cache entries.
1995*/
1996void sqlite3ExprColumnCacheDisable(Parse *pParse, int disable){
1997 assert( disable==-1 || disable==+1 );
1998 assert( pParse->disableColCache>0 || disable==1 );
1999 pParse->disableColCache += disable;
2000}
2001
2002/*
2003** Clear all column cache entries associated with the vdbe
2004** cursor with cursor number iTable.
2005*/
2006void sqlite3ExprClearColumnCache(Parse *pParse, int iTable){
2007 if( iTable<0 ){
2008 pParse->nColCache = 0;
2009 pParse->iColCache = 0;
2010 }else{
2011 int i;
2012 for(i=0; i<pParse->nColCache; i++){
2013 if( pParse->aColCache[i].iTable==iTable ){
2014 pParse->aColCache[i] = pParse->aColCache[--pParse->nColCache];
drhda250ea2008-04-01 05:07:14 +00002015 pParse->iColCache = pParse->nColCache;
drhe55cbd72008-03-31 23:48:03 +00002016 }
2017 }
drhe55cbd72008-03-31 23:48:03 +00002018 }
2019}
2020
2021/*
drhda250ea2008-04-01 05:07:14 +00002022** Record the fact that an affinity change has occurred on iCount
2023** registers starting with iStart.
drhe55cbd72008-03-31 23:48:03 +00002024*/
drhda250ea2008-04-01 05:07:14 +00002025void sqlite3ExprCacheAffinityChange(Parse *pParse, int iStart, int iCount){
2026 int iEnd = iStart + iCount - 1;
drhe55cbd72008-03-31 23:48:03 +00002027 int i;
2028 for(i=0; i<pParse->nColCache; i++){
2029 int r = pParse->aColCache[i].iReg;
drhda250ea2008-04-01 05:07:14 +00002030 if( r>=iStart && r<=iEnd ){
2031 pParse->aColCache[i].affChange = 1;
drhe55cbd72008-03-31 23:48:03 +00002032 }
2033 }
drhe55cbd72008-03-31 23:48:03 +00002034}
2035
2036/*
2037** Generate code to moves content from one register to another.
2038** Keep the column cache up-to-date.
2039*/
2040void sqlite3ExprCodeMove(Parse *pParse, int iFrom, int iTo){
2041 int i;
2042 if( iFrom==iTo ) return;
2043 sqlite3VdbeAddOp2(pParse->pVdbe, OP_Move, iFrom, iTo);
2044 for(i=0; i<pParse->nColCache; i++){
2045 if( pParse->aColCache[i].iReg==iFrom ){
2046 pParse->aColCache[i].iReg = iTo;
2047 }
2048 }
drh945498f2007-02-24 11:52:52 +00002049}
2050
drhfec19aa2004-05-19 20:41:03 +00002051/*
drh652fbf52008-04-01 01:42:41 +00002052** Return true if any register in the range iFrom..iTo (inclusive)
2053** is used as part of the column cache.
2054*/
2055static int usedAsColumnCache(Parse *pParse, int iFrom, int iTo){
2056 int i;
2057 for(i=0; i<pParse->nColCache; i++){
2058 int r = pParse->aColCache[i].iReg;
2059 if( r>=iFrom && r<=iTo ) return 1;
2060 }
2061 return 0;
2062}
2063
2064/*
2065** Theres is a value in register iCurrent. We ultimately want
2066** the value to be in register iTarget. It might be that
2067** iCurrent and iTarget are the same register.
2068**
2069** We are going to modify the value, so we need to make sure it
2070** is not a cached register. If iCurrent is a cached register,
2071** then try to move the value over to iTarget. If iTarget is a
2072** cached register, then clear the corresponding cache line.
2073**
2074** Return the register that the value ends up in.
2075*/
2076int sqlite3ExprWritableRegister(Parse *pParse, int iCurrent, int iTarget){
drhda250ea2008-04-01 05:07:14 +00002077 int i;
drh652fbf52008-04-01 01:42:41 +00002078 assert( pParse->pVdbe!=0 );
2079 if( !usedAsColumnCache(pParse, iCurrent, iCurrent) ){
2080 return iCurrent;
2081 }
drh2f7794c2008-04-01 03:27:39 +00002082 if( iCurrent!=iTarget ){
2083 sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, iCurrent, iTarget);
2084 }
drhda250ea2008-04-01 05:07:14 +00002085 for(i=0; i<pParse->nColCache; i++){
2086 if( pParse->aColCache[i].iReg==iTarget ){
2087 pParse->aColCache[i] = pParse->aColCache[--pParse->nColCache];
2088 pParse->iColCache = pParse->nColCache;
2089 }
2090 }
drh652fbf52008-04-01 01:42:41 +00002091 return iTarget;
2092}
2093
2094/*
drhcce7d172000-05-31 15:34:51 +00002095** Generate code into the current Vdbe to evaluate the given
drh2dcef112008-01-12 19:03:48 +00002096** expression. Attempt to store the results in register "target".
2097** Return the register where results are stored.
drh389a1ad2008-01-03 23:44:53 +00002098**
drh2dcef112008-01-12 19:03:48 +00002099** With this routine, there is no guaranteed that results will
2100** be stored in target. The result might be stored in some other
2101** register if it is convenient to do so. The calling function
2102** must check the return code and move the results to the desired
2103** register.
drhcce7d172000-05-31 15:34:51 +00002104*/
drh678ccce2008-03-31 18:19:54 +00002105int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){
drh2dcef112008-01-12 19:03:48 +00002106 Vdbe *v = pParse->pVdbe; /* The VM under construction */
2107 int op; /* The opcode being coded */
2108 int inReg = target; /* Results stored in register inReg */
2109 int regFree1 = 0; /* If non-zero free this temporary register */
2110 int regFree2 = 0; /* If non-zero free this temporary register */
drh678ccce2008-03-31 18:19:54 +00002111 int r1, r2, r3, r4; /* Various register numbers */
drhffe07b22005-11-03 00:41:17 +00002112
drh389a1ad2008-01-03 23:44:53 +00002113 assert( v!=0 || pParse->db->mallocFailed );
drh9cbf3422008-01-17 16:22:13 +00002114 assert( target>0 && target<=pParse->nMem );
drh389a1ad2008-01-03 23:44:53 +00002115 if( v==0 ) return 0;
drh389a1ad2008-01-03 23:44:53 +00002116
2117 if( pExpr==0 ){
2118 op = TK_NULL;
2119 }else{
2120 op = pExpr->op;
2121 }
drhf2bc0132004-10-04 13:19:23 +00002122 switch( op ){
drh13449892005-09-07 21:22:45 +00002123 case TK_AGG_COLUMN: {
2124 AggInfo *pAggInfo = pExpr->pAggInfo;
2125 struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg];
2126 if( !pAggInfo->directMode ){
drh9de221d2008-01-05 06:51:30 +00002127 assert( pCol->iMem>0 );
2128 inReg = pCol->iMem;
drh13449892005-09-07 21:22:45 +00002129 break;
2130 }else if( pAggInfo->useSortingIdx ){
drh389a1ad2008-01-03 23:44:53 +00002131 sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdx,
2132 pCol->iSorterColumn, target);
drh13449892005-09-07 21:22:45 +00002133 break;
2134 }
2135 /* Otherwise, fall thru into the TK_COLUMN case */
2136 }
drh967e8b72000-06-21 13:59:10 +00002137 case TK_COLUMN: {
drhffe07b22005-11-03 00:41:17 +00002138 if( pExpr->iTable<0 ){
2139 /* This only happens when coding check constraints */
drhaa9b8962008-01-08 02:57:55 +00002140 assert( pParse->ckBase>0 );
2141 inReg = pExpr->iColumn + pParse->ckBase;
drhc4a3c772001-04-04 11:48:57 +00002142 }else{
drhe55cbd72008-03-31 23:48:03 +00002143 inReg = sqlite3ExprCodeGetColumn(pParse, pExpr->pTab,
drhda250ea2008-04-01 05:07:14 +00002144 pExpr->iColumn, pExpr->iTable, target,
2145 pExpr->flags & EP_AnyAff);
drh22827922000-06-06 17:27:05 +00002146 }
drhcce7d172000-05-31 15:34:51 +00002147 break;
2148 }
2149 case TK_INTEGER: {
drh9de221d2008-01-05 06:51:30 +00002150 codeInteger(v, (char*)pExpr->token.z, pExpr->token.n, 0, target);
drhfec19aa2004-05-19 20:41:03 +00002151 break;
2152 }
drh598f1342007-10-23 15:39:45 +00002153 case TK_FLOAT: {
drh9de221d2008-01-05 06:51:30 +00002154 codeReal(v, (char*)pExpr->token.z, pExpr->token.n, 0, target);
drh598f1342007-10-23 15:39:45 +00002155 break;
2156 }
drhfec19aa2004-05-19 20:41:03 +00002157 case TK_STRING: {
danielk19771e536952007-08-16 10:09:01 +00002158 sqlite3DequoteExpr(pParse->db, pExpr);
drh9de221d2008-01-05 06:51:30 +00002159 sqlite3VdbeAddOp4(v,OP_String8, 0, target, 0,
drh66a51672008-01-03 00:01:23 +00002160 (char*)pExpr->token.z, pExpr->token.n);
drhcce7d172000-05-31 15:34:51 +00002161 break;
2162 }
drhf0863fe2005-06-12 21:35:51 +00002163 case TK_NULL: {
drh9de221d2008-01-05 06:51:30 +00002164 sqlite3VdbeAddOp2(v, OP_Null, 0, target);
drhf0863fe2005-06-12 21:35:51 +00002165 break;
2166 }
danielk19775338a5f2005-01-20 13:03:10 +00002167#ifndef SQLITE_OMIT_BLOB_LITERAL
danielk1977c572ef72004-05-27 09:28:41 +00002168 case TK_BLOB: {
drh6c8c6ce2005-08-23 11:17:58 +00002169 int n;
2170 const char *z;
drhca48c902008-01-18 14:08:24 +00002171 char *zBlob;
2172 assert( pExpr->token.n>=3 );
2173 assert( pExpr->token.z[0]=='x' || pExpr->token.z[0]=='X' );
2174 assert( pExpr->token.z[1]=='\'' );
2175 assert( pExpr->token.z[pExpr->token.n-1]=='\'' );
drh6c8c6ce2005-08-23 11:17:58 +00002176 n = pExpr->token.n - 3;
drh2646da72005-12-09 20:02:05 +00002177 z = (char*)pExpr->token.z + 2;
drhca48c902008-01-18 14:08:24 +00002178 zBlob = sqlite3HexToBlob(sqlite3VdbeDb(v), z, n);
2179 sqlite3VdbeAddOp4(v, OP_Blob, n/2, target, 0, zBlob, P4_DYNAMIC);
danielk1977c572ef72004-05-27 09:28:41 +00002180 break;
2181 }
danielk19775338a5f2005-01-20 13:03:10 +00002182#endif
drh50457892003-09-06 01:10:47 +00002183 case TK_VARIABLE: {
drh9de221d2008-01-05 06:51:30 +00002184 sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iTable, target);
drh895d7472004-08-20 16:02:39 +00002185 if( pExpr->token.n>1 ){
drh66a51672008-01-03 00:01:23 +00002186 sqlite3VdbeChangeP4(v, -1, (char*)pExpr->token.z, pExpr->token.n);
drh895d7472004-08-20 16:02:39 +00002187 }
drh50457892003-09-06 01:10:47 +00002188 break;
2189 }
drh4e0cff62004-11-05 05:10:28 +00002190 case TK_REGISTER: {
drh9de221d2008-01-05 06:51:30 +00002191 inReg = pExpr->iTable;
drh4e0cff62004-11-05 05:10:28 +00002192 break;
2193 }
drh487e2622005-06-25 18:42:14 +00002194#ifndef SQLITE_OMIT_CAST
2195 case TK_CAST: {
2196 /* Expressions of the form: CAST(pLeft AS token) */
danielk1977f0113002006-01-24 12:09:17 +00002197 int aff, to_op;
drh2dcef112008-01-12 19:03:48 +00002198 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
drh8a512562005-11-14 22:29:05 +00002199 aff = sqlite3AffinityType(&pExpr->token);
danielk1977f0113002006-01-24 12:09:17 +00002200 to_op = aff - SQLITE_AFF_TEXT + OP_ToText;
2201 assert( to_op==OP_ToText || aff!=SQLITE_AFF_TEXT );
2202 assert( to_op==OP_ToBlob || aff!=SQLITE_AFF_NONE );
2203 assert( to_op==OP_ToNumeric || aff!=SQLITE_AFF_NUMERIC );
2204 assert( to_op==OP_ToInt || aff!=SQLITE_AFF_INTEGER );
2205 assert( to_op==OP_ToReal || aff!=SQLITE_AFF_REAL );
drh2dcef112008-01-12 19:03:48 +00002206 sqlite3VdbeAddOp1(v, to_op, inReg);
drhb3843a82008-04-01 12:24:11 +00002207 sqlite3ExprCacheAffinityChange(pParse, inReg, 1);
drh487e2622005-06-25 18:42:14 +00002208 break;
2209 }
2210#endif /* SQLITE_OMIT_CAST */
drhc9b84a12002-06-20 11:36:48 +00002211 case TK_LT:
2212 case TK_LE:
2213 case TK_GT:
2214 case TK_GE:
2215 case TK_NE:
2216 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00002217 assert( TK_LT==OP_Lt );
2218 assert( TK_LE==OP_Le );
2219 assert( TK_GT==OP_Gt );
2220 assert( TK_GE==OP_Ge );
2221 assert( TK_EQ==OP_Eq );
2222 assert( TK_NE==OP_Ne );
drhda250ea2008-04-01 05:07:14 +00002223 codeCompareOperands(pParse, pExpr->pLeft, &r1, &regFree1,
2224 pExpr->pRight, &r2, &regFree2);
drh35573352008-01-08 23:54:25 +00002225 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
2226 r1, r2, inReg, SQLITE_STOREP2);
danielk1977a37cdde2004-05-16 11:15:36 +00002227 break;
drhc9b84a12002-06-20 11:36:48 +00002228 }
drhcce7d172000-05-31 15:34:51 +00002229 case TK_AND:
2230 case TK_OR:
2231 case TK_PLUS:
2232 case TK_STAR:
2233 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00002234 case TK_REM:
2235 case TK_BITAND:
2236 case TK_BITOR:
drh17c40292004-07-21 02:53:29 +00002237 case TK_SLASH:
drhbf4133c2001-10-13 02:59:08 +00002238 case TK_LSHIFT:
drh855eb1c2004-08-31 13:45:11 +00002239 case TK_RSHIFT:
drh00400772000-06-16 20:51:26 +00002240 case TK_CONCAT: {
drhf2bc0132004-10-04 13:19:23 +00002241 assert( TK_AND==OP_And );
2242 assert( TK_OR==OP_Or );
2243 assert( TK_PLUS==OP_Add );
2244 assert( TK_MINUS==OP_Subtract );
2245 assert( TK_REM==OP_Remainder );
2246 assert( TK_BITAND==OP_BitAnd );
2247 assert( TK_BITOR==OP_BitOr );
2248 assert( TK_SLASH==OP_Divide );
2249 assert( TK_LSHIFT==OP_ShiftLeft );
2250 assert( TK_RSHIFT==OP_ShiftRight );
2251 assert( TK_CONCAT==OP_Concat );
drh2dcef112008-01-12 19:03:48 +00002252 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
2253 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
drh5b6afba2008-01-05 16:29:28 +00002254 sqlite3VdbeAddOp3(v, op, r2, r1, target);
drh00400772000-06-16 20:51:26 +00002255 break;
2256 }
drhcce7d172000-05-31 15:34:51 +00002257 case TK_UMINUS: {
drhfec19aa2004-05-19 20:41:03 +00002258 Expr *pLeft = pExpr->pLeft;
2259 assert( pLeft );
2260 if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
2261 Token *p = &pLeft->token;
drhfec19aa2004-05-19 20:41:03 +00002262 if( pLeft->op==TK_FLOAT ){
drh9de221d2008-01-05 06:51:30 +00002263 codeReal(v, (char*)p->z, p->n, 1, target);
drhe6840902002-03-06 03:08:25 +00002264 }else{
drh9de221d2008-01-05 06:51:30 +00002265 codeInteger(v, (char*)p->z, p->n, 1, target);
drhe6840902002-03-06 03:08:25 +00002266 }
drh3c84ddf2008-01-09 02:15:38 +00002267 }else{
drh2dcef112008-01-12 19:03:48 +00002268 regFree1 = r1 = sqlite3GetTempReg(pParse);
drh3c84ddf2008-01-09 02:15:38 +00002269 sqlite3VdbeAddOp2(v, OP_Integer, 0, r1);
drhe55cbd72008-03-31 23:48:03 +00002270 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree2);
drh2dcef112008-01-12 19:03:48 +00002271 sqlite3VdbeAddOp3(v, OP_Subtract, r2, r1, target);
drh6e142f52000-06-08 13:36:40 +00002272 }
drh3c84ddf2008-01-09 02:15:38 +00002273 inReg = target;
2274 break;
drh6e142f52000-06-08 13:36:40 +00002275 }
drhbf4133c2001-10-13 02:59:08 +00002276 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00002277 case TK_NOT: {
drhf2bc0132004-10-04 13:19:23 +00002278 assert( TK_BITNOT==OP_BitNot );
2279 assert( TK_NOT==OP_Not );
drh2dcef112008-01-12 19:03:48 +00002280 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
drh652fbf52008-04-01 01:42:41 +00002281 inReg = sqlite3ExprWritableRegister(pParse, inReg, target);
drh2dcef112008-01-12 19:03:48 +00002282 sqlite3VdbeAddOp1(v, op, inReg);
drhcce7d172000-05-31 15:34:51 +00002283 break;
2284 }
2285 case TK_ISNULL:
2286 case TK_NOTNULL: {
drh6a288a32008-01-07 19:20:24 +00002287 int addr;
drhf2bc0132004-10-04 13:19:23 +00002288 assert( TK_ISNULL==OP_IsNull );
2289 assert( TK_NOTNULL==OP_NotNull );
drh9de221d2008-01-05 06:51:30 +00002290 sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
drh2dcef112008-01-12 19:03:48 +00002291 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
2292 addr = sqlite3VdbeAddOp1(v, op, r1);
drh9de221d2008-01-05 06:51:30 +00002293 sqlite3VdbeAddOp2(v, OP_AddImm, target, -1);
drh6a288a32008-01-07 19:20:24 +00002294 sqlite3VdbeJumpHere(v, addr);
drhf2bc0132004-10-04 13:19:23 +00002295 break;
drhcce7d172000-05-31 15:34:51 +00002296 }
drh22827922000-06-06 17:27:05 +00002297 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00002298 AggInfo *pInfo = pExpr->pAggInfo;
drh7e56e712005-11-16 12:53:15 +00002299 if( pInfo==0 ){
2300 sqlite3ErrorMsg(pParse, "misuse of aggregate: %T",
2301 &pExpr->span);
2302 }else{
drh9de221d2008-01-05 06:51:30 +00002303 inReg = pInfo->aFunc[pExpr->iAgg].iMem;
drh7e56e712005-11-16 12:53:15 +00002304 }
drh22827922000-06-06 17:27:05 +00002305 break;
2306 }
drhb71090f2005-05-23 17:26:51 +00002307 case TK_CONST_FUNC:
drhcce7d172000-05-31 15:34:51 +00002308 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00002309 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00002310 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00002311 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00002312 int nId;
2313 const char *zId;
drh13449892005-09-07 21:22:45 +00002314 int constMask = 0;
danielk1977682f68b2004-06-05 10:22:17 +00002315 int i;
drh17435752007-08-16 04:30:38 +00002316 sqlite3 *db = pParse->db;
2317 u8 enc = ENC(db);
danielk1977dc1bdc42004-06-11 10:51:27 +00002318 CollSeq *pColl = 0;
drh17435752007-08-16 04:30:38 +00002319
drh2646da72005-12-09 20:02:05 +00002320 zId = (char*)pExpr->token.z;
drhb71090f2005-05-23 17:26:51 +00002321 nId = pExpr->token.n;
danielk1977d8123362004-06-12 09:25:12 +00002322 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0);
drh0bce8352002-02-28 00:41:10 +00002323 assert( pDef!=0 );
drh892d3172008-01-10 03:46:36 +00002324 if( pList ){
2325 nExpr = pList->nExpr;
drh2dcef112008-01-12 19:03:48 +00002326 r1 = sqlite3GetTempRange(pParse, nExpr);
2327 sqlite3ExprCodeExprList(pParse, pList, r1);
drh892d3172008-01-10 03:46:36 +00002328 }else{
drhd847eaa2008-01-17 17:15:56 +00002329 nExpr = r1 = 0;
drh892d3172008-01-10 03:46:36 +00002330 }
drhb7f6f682006-07-08 17:06:43 +00002331#ifndef SQLITE_OMIT_VIRTUALTABLE
drha43fa222006-07-08 18:41:37 +00002332 /* Possibly overload the function if the first argument is
2333 ** a virtual table column.
2334 **
2335 ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the
2336 ** second argument, not the first, as the argument to test to
2337 ** see if it is a column in a virtual table. This is done because
2338 ** the left operand of infix functions (the operand we want to
2339 ** control overloading) ends up as the second argument to the
2340 ** function. The expression "A glob B" is equivalent to
2341 ** "glob(B,A). We want to use the A in "A glob B" to test
2342 ** for function overloading. But we use the B term in "glob(B,A)".
2343 */
drh6a03a1c2006-07-08 18:34:59 +00002344 if( nExpr>=2 && (pExpr->flags & EP_InfixFunc) ){
drh17435752007-08-16 04:30:38 +00002345 pDef = sqlite3VtabOverloadFunction(db, pDef, nExpr, pList->a[1].pExpr);
drh6a03a1c2006-07-08 18:34:59 +00002346 }else if( nExpr>0 ){
drh17435752007-08-16 04:30:38 +00002347 pDef = sqlite3VtabOverloadFunction(db, pDef, nExpr, pList->a[0].pExpr);
drhb7f6f682006-07-08 17:06:43 +00002348 }
2349#endif
danielk1977682f68b2004-06-05 10:22:17 +00002350 for(i=0; i<nExpr && i<32; i++){
danielk1977d02eb1f2004-06-06 09:44:03 +00002351 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
drh13449892005-09-07 21:22:45 +00002352 constMask |= (1<<i);
danielk1977d02eb1f2004-06-06 09:44:03 +00002353 }
danielk1977dc1bdc42004-06-11 10:51:27 +00002354 if( pDef->needCollSeq && !pColl ){
2355 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
2356 }
2357 }
2358 if( pDef->needCollSeq ){
2359 if( !pColl ) pColl = pParse->db->pDfltColl;
drh66a51672008-01-03 00:01:23 +00002360 sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ);
danielk1977682f68b2004-06-05 10:22:17 +00002361 }
drh2dcef112008-01-12 19:03:48 +00002362 sqlite3VdbeAddOp4(v, OP_Function, constMask, r1, target,
drh66a51672008-01-03 00:01:23 +00002363 (char*)pDef, P4_FUNCDEF);
drh98757152008-01-09 23:04:12 +00002364 sqlite3VdbeChangeP5(v, nExpr);
drh2dcef112008-01-12 19:03:48 +00002365 if( nExpr ){
2366 sqlite3ReleaseTempRange(pParse, r1, nExpr);
2367 }
drhda250ea2008-04-01 05:07:14 +00002368 sqlite3ExprCacheAffinityChange(pParse, r1, nExpr);
drhcce7d172000-05-31 15:34:51 +00002369 break;
2370 }
drhfe2093d2005-01-20 22:48:47 +00002371#ifndef SQLITE_OMIT_SUBQUERY
2372 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00002373 case TK_SELECT: {
drh41714d62006-03-02 04:44:23 +00002374 if( pExpr->iColumn==0 ){
2375 sqlite3CodeSubselect(pParse, pExpr);
2376 }
drh9de221d2008-01-05 06:51:30 +00002377 inReg = pExpr->iColumn;
drh19a775c2000-06-05 18:54:46 +00002378 break;
2379 }
drhfef52082000-06-06 01:50:43 +00002380 case TK_IN: {
drh6a288a32008-01-07 19:20:24 +00002381 int j1, j2, j3, j4, j5;
drh94a11212004-09-25 13:12:14 +00002382 char affinity;
danielk19779a96b662007-11-29 17:05:18 +00002383 int eType;
danielk19779a96b662007-11-29 17:05:18 +00002384
2385 eType = sqlite3FindInIndex(pParse, pExpr, 0);
danielk1977e014a832004-05-17 10:48:57 +00002386
2387 /* Figure out the affinity to use to create a key from the results
2388 ** of the expression. affinityStr stores a static string suitable for
drh66a51672008-01-03 00:01:23 +00002389 ** P4 of OP_MakeRecord.
danielk1977e014a832004-05-17 10:48:57 +00002390 */
drh94a11212004-09-25 13:12:14 +00002391 affinity = comparisonAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +00002392
drh2dcef112008-01-12 19:03:48 +00002393 sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
danielk1977e014a832004-05-17 10:48:57 +00002394
2395 /* Code the <expr> from "<expr> IN (...)". The temporary table
2396 ** pExpr->iTable contains the values that make up the (...) set.
2397 */
drh2dcef112008-01-12 19:03:48 +00002398 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
2399 j1 = sqlite3VdbeAddOp1(v, OP_NotNull, r1);
2400 sqlite3VdbeAddOp2(v, OP_Null, 0, target);
drh6a288a32008-01-07 19:20:24 +00002401 j2 = sqlite3VdbeAddOp0(v, OP_Goto);
2402 sqlite3VdbeJumpHere(v, j1);
danielk19779a96b662007-11-29 17:05:18 +00002403 if( eType==IN_INDEX_ROWID ){
drh678ccce2008-03-31 18:19:54 +00002404 j3 = sqlite3VdbeAddOp1(v, OP_MustBeInt, r1);
drh2dcef112008-01-12 19:03:48 +00002405 j4 = sqlite3VdbeAddOp3(v, OP_NotExists, pExpr->iTable, 0, r1);
drh6a288a32008-01-07 19:20:24 +00002406 j5 = sqlite3VdbeAddOp0(v, OP_Goto);
2407 sqlite3VdbeJumpHere(v, j3);
2408 sqlite3VdbeJumpHere(v, j4);
danielk19779a96b662007-11-29 17:05:18 +00002409 }else{
drh2dcef112008-01-12 19:03:48 +00002410 r2 = regFree2 = sqlite3GetTempReg(pParse);
drh1db639c2008-01-17 02:36:28 +00002411 sqlite3VdbeAddOp4(v, OP_MakeRecord, r1, 1, r2, &affinity, 1);
drhda250ea2008-04-01 05:07:14 +00002412 sqlite3ExprCacheAffinityChange(pParse, r1, 1);
drh2dcef112008-01-12 19:03:48 +00002413 j5 = sqlite3VdbeAddOp3(v, OP_Found, pExpr->iTable, 0, r2);
danielk19779a96b662007-11-29 17:05:18 +00002414 }
drh2dcef112008-01-12 19:03:48 +00002415 sqlite3VdbeAddOp2(v, OP_AddImm, target, -1);
drh6a288a32008-01-07 19:20:24 +00002416 sqlite3VdbeJumpHere(v, j2);
2417 sqlite3VdbeJumpHere(v, j5);
drhfef52082000-06-06 01:50:43 +00002418 break;
2419 }
danielk197793758c82005-01-21 08:13:14 +00002420#endif
drh2dcef112008-01-12 19:03:48 +00002421 /*
2422 ** x BETWEEN y AND z
2423 **
2424 ** This is equivalent to
2425 **
2426 ** x>=y AND x<=z
2427 **
2428 ** X is stored in pExpr->pLeft.
2429 ** Y is stored in pExpr->pList->a[0].pExpr.
2430 ** Z is stored in pExpr->pList->a[1].pExpr.
2431 */
drhfef52082000-06-06 01:50:43 +00002432 case TK_BETWEEN: {
drhbe5c89a2004-07-26 00:31:09 +00002433 Expr *pLeft = pExpr->pLeft;
2434 struct ExprList_item *pLItem = pExpr->pList->a;
2435 Expr *pRight = pLItem->pExpr;
drh35573352008-01-08 23:54:25 +00002436
drhda250ea2008-04-01 05:07:14 +00002437 codeCompareOperands(pParse, pLeft, &r1, &regFree1,
2438 pRight, &r2, &regFree2);
drh2dcef112008-01-12 19:03:48 +00002439 r3 = sqlite3GetTempReg(pParse);
drh678ccce2008-03-31 18:19:54 +00002440 r4 = sqlite3GetTempReg(pParse);
drh35573352008-01-08 23:54:25 +00002441 codeCompare(pParse, pLeft, pRight, OP_Ge,
2442 r1, r2, r3, SQLITE_STOREP2);
drhbe5c89a2004-07-26 00:31:09 +00002443 pLItem++;
2444 pRight = pLItem->pExpr;
drh2dcef112008-01-12 19:03:48 +00002445 sqlite3ReleaseTempReg(pParse, regFree2);
2446 r2 = sqlite3ExprCodeTemp(pParse, pRight, &regFree2);
drh678ccce2008-03-31 18:19:54 +00002447 codeCompare(pParse, pLeft, pRight, OP_Le, r1, r2, r4, SQLITE_STOREP2);
2448 sqlite3VdbeAddOp3(v, OP_And, r3, r4, target);
drh2dcef112008-01-12 19:03:48 +00002449 sqlite3ReleaseTempReg(pParse, r3);
drh678ccce2008-03-31 18:19:54 +00002450 sqlite3ReleaseTempReg(pParse, r4);
drhfef52082000-06-06 01:50:43 +00002451 break;
2452 }
drh4f07e5f2007-05-14 11:34:46 +00002453 case TK_UPLUS: {
drh2dcef112008-01-12 19:03:48 +00002454 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
drha2e00042002-01-22 03:13:42 +00002455 break;
2456 }
drh2dcef112008-01-12 19:03:48 +00002457
2458 /*
2459 ** Form A:
2460 ** CASE x WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
2461 **
2462 ** Form B:
2463 ** CASE WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
2464 **
2465 ** Form A is can be transformed into the equivalent form B as follows:
2466 ** CASE WHEN x=e1 THEN r1 WHEN x=e2 THEN r2 ...
2467 ** WHEN x=eN THEN rN ELSE y END
2468 **
2469 ** X (if it exists) is in pExpr->pLeft.
2470 ** Y is in pExpr->pRight. The Y is also optional. If there is no
2471 ** ELSE clause and no other term matches, then the result of the
2472 ** exprssion is NULL.
2473 ** Ei is in pExpr->pList->a[i*2] and Ri is pExpr->pList->a[i*2+1].
2474 **
2475 ** The result of the expression is the Ri for the first matching Ei,
2476 ** or if there is no matching Ei, the ELSE term Y, or if there is
2477 ** no ELSE term, NULL.
2478 */
drh17a7f8d2002-03-24 13:13:27 +00002479 case TK_CASE: {
drh2dcef112008-01-12 19:03:48 +00002480 int endLabel; /* GOTO label for end of CASE stmt */
2481 int nextCase; /* GOTO label for next WHEN clause */
2482 int nExpr; /* 2x number of WHEN terms */
2483 int i; /* Loop counter */
2484 ExprList *pEList; /* List of WHEN terms */
2485 struct ExprList_item *aListelem; /* Array of WHEN terms */
2486 Expr opCompare; /* The X==Ei expression */
2487 Expr cacheX; /* Cached expression X */
2488 Expr *pX; /* The X expression */
2489 Expr *pTest; /* X==Ei (form A) or just Ei (form B) */
drh17a7f8d2002-03-24 13:13:27 +00002490
2491 assert(pExpr->pList);
2492 assert((pExpr->pList->nExpr % 2) == 0);
2493 assert(pExpr->pList->nExpr > 0);
drhbe5c89a2004-07-26 00:31:09 +00002494 pEList = pExpr->pList;
2495 aListelem = pEList->a;
2496 nExpr = pEList->nExpr;
drh2dcef112008-01-12 19:03:48 +00002497 endLabel = sqlite3VdbeMakeLabel(v);
2498 if( (pX = pExpr->pLeft)!=0 ){
2499 cacheX = *pX;
2500 cacheX.iTable = sqlite3ExprCodeTemp(pParse, pX, &regFree1);
2501 cacheX.op = TK_REGISTER;
drh678ccce2008-03-31 18:19:54 +00002502 cacheX.iColumn = 0;
drh2dcef112008-01-12 19:03:48 +00002503 opCompare.op = TK_EQ;
2504 opCompare.pLeft = &cacheX;
2505 pTest = &opCompare;
drh17a7f8d2002-03-24 13:13:27 +00002506 }
drh652fbf52008-04-01 01:42:41 +00002507 sqlite3ExprColumnCacheDisable(pParse, 1);
drhf5905aa2002-05-26 20:54:33 +00002508 for(i=0; i<nExpr; i=i+2){
drh2dcef112008-01-12 19:03:48 +00002509 if( pX ){
2510 opCompare.pRight = aListelem[i].pExpr;
drh17a7f8d2002-03-24 13:13:27 +00002511 }else{
drh2dcef112008-01-12 19:03:48 +00002512 pTest = aListelem[i].pExpr;
drh17a7f8d2002-03-24 13:13:27 +00002513 }
drh2dcef112008-01-12 19:03:48 +00002514 nextCase = sqlite3VdbeMakeLabel(v);
2515 sqlite3ExprIfFalse(pParse, pTest, nextCase, SQLITE_JUMPIFNULL);
drh9de221d2008-01-05 06:51:30 +00002516 sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target);
drh2dcef112008-01-12 19:03:48 +00002517 sqlite3VdbeAddOp2(v, OP_Goto, 0, endLabel);
2518 sqlite3VdbeResolveLabel(v, nextCase);
drhf570f012002-05-31 15:51:25 +00002519 }
drh17a7f8d2002-03-24 13:13:27 +00002520 if( pExpr->pRight ){
drh9de221d2008-01-05 06:51:30 +00002521 sqlite3ExprCode(pParse, pExpr->pRight, target);
drh17a7f8d2002-03-24 13:13:27 +00002522 }else{
drh9de221d2008-01-05 06:51:30 +00002523 sqlite3VdbeAddOp2(v, OP_Null, 0, target);
drh17a7f8d2002-03-24 13:13:27 +00002524 }
drh2dcef112008-01-12 19:03:48 +00002525 sqlite3VdbeResolveLabel(v, endLabel);
drhe55cbd72008-03-31 23:48:03 +00002526 sqlite3ExprColumnCacheDisable(pParse, -1);
danielk19776f349032002-06-11 02:25:40 +00002527 break;
2528 }
danielk19775338a5f2005-01-20 13:03:10 +00002529#ifndef SQLITE_OMIT_TRIGGER
danielk19776f349032002-06-11 02:25:40 +00002530 case TK_RAISE: {
2531 if( !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +00002532 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00002533 "RAISE() may only be used within a trigger-program");
drh389a1ad2008-01-03 23:44:53 +00002534 return 0;
danielk19776f349032002-06-11 02:25:40 +00002535 }
drhad6d9462004-09-19 02:15:24 +00002536 if( pExpr->iColumn!=OE_Ignore ){
2537 assert( pExpr->iColumn==OE_Rollback ||
2538 pExpr->iColumn == OE_Abort ||
2539 pExpr->iColumn == OE_Fail );
danielk19771e536952007-08-16 10:09:01 +00002540 sqlite3DequoteExpr(pParse->db, pExpr);
drh66a51672008-01-03 00:01:23 +00002541 sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn, 0,
drh2646da72005-12-09 20:02:05 +00002542 (char*)pExpr->token.z, pExpr->token.n);
danielk19776f349032002-06-11 02:25:40 +00002543 } else {
drhad6d9462004-09-19 02:15:24 +00002544 assert( pExpr->iColumn == OE_Ignore );
drh66a51672008-01-03 00:01:23 +00002545 sqlite3VdbeAddOp2(v, OP_ContextPop, 0, 0);
2546 sqlite3VdbeAddOp2(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
drhd4e70eb2008-01-02 00:34:36 +00002547 VdbeComment((v, "raise(IGNORE)"));
danielk19776f349032002-06-11 02:25:40 +00002548 }
drhffe07b22005-11-03 00:41:17 +00002549 break;
drh17a7f8d2002-03-24 13:13:27 +00002550 }
danielk19775338a5f2005-01-20 13:03:10 +00002551#endif
drhffe07b22005-11-03 00:41:17 +00002552 }
drh2dcef112008-01-12 19:03:48 +00002553 sqlite3ReleaseTempReg(pParse, regFree1);
2554 sqlite3ReleaseTempReg(pParse, regFree2);
2555 return inReg;
2556}
2557
2558/*
2559** Generate code to evaluate an expression and store the results
2560** into a register. Return the register number where the results
2561** are stored.
2562**
2563** If the register is a temporary register that can be deallocated,
drh678ccce2008-03-31 18:19:54 +00002564** then write its number into *pReg. If the result register is not
drh2dcef112008-01-12 19:03:48 +00002565** a temporary, then set *pReg to zero.
2566*/
2567int sqlite3ExprCodeTemp(Parse *pParse, Expr *pExpr, int *pReg){
2568 int r1 = sqlite3GetTempReg(pParse);
2569 int r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
2570 if( r2==r1 ){
2571 *pReg = r1;
2572 }else{
2573 sqlite3ReleaseTempReg(pParse, r1);
2574 *pReg = 0;
2575 }
2576 return r2;
2577}
2578
2579/*
2580** Generate code that will evaluate expression pExpr and store the
2581** results in register target. The results are guaranteed to appear
2582** in register target.
2583*/
2584int sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){
drh9cbf3422008-01-17 16:22:13 +00002585 int inReg;
2586
2587 assert( target>0 && target<=pParse->nMem );
2588 inReg = sqlite3ExprCodeTarget(pParse, pExpr, target);
drh0e359b32008-01-13 19:02:11 +00002589 assert( pParse->pVdbe || pParse->db->mallocFailed );
2590 if( inReg!=target && pParse->pVdbe ){
drh9cbf3422008-01-17 16:22:13 +00002591 sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, inReg, target);
drhcce7d172000-05-31 15:34:51 +00002592 }
drh389a1ad2008-01-03 23:44:53 +00002593 return target;
drhcce7d172000-05-31 15:34:51 +00002594}
2595
2596/*
drh2dcef112008-01-12 19:03:48 +00002597** Generate code that evalutes the given expression and puts the result
drhde4fcfd2008-01-19 23:50:26 +00002598** in register target.
drh25303782004-12-07 15:41:48 +00002599**
drh2dcef112008-01-12 19:03:48 +00002600** Also make a copy of the expression results into another "cache" register
2601** and modify the expression so that the next time it is evaluated,
2602** the result is a copy of the cache register.
2603**
2604** This routine is used for expressions that are used multiple
2605** times. They are evaluated once and the results of the expression
2606** are reused.
drh25303782004-12-07 15:41:48 +00002607*/
drh2dcef112008-01-12 19:03:48 +00002608int sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr, int target){
drh25303782004-12-07 15:41:48 +00002609 Vdbe *v = pParse->pVdbe;
drh2dcef112008-01-12 19:03:48 +00002610 int inReg;
2611 inReg = sqlite3ExprCode(pParse, pExpr, target);
drhde4fcfd2008-01-19 23:50:26 +00002612 assert( target>0 );
drh2dcef112008-01-12 19:03:48 +00002613 if( pExpr->op!=TK_REGISTER ){
2614 int iMem;
drhde4fcfd2008-01-19 23:50:26 +00002615 iMem = ++pParse->nMem;
2616 sqlite3VdbeAddOp2(v, OP_Copy, inReg, iMem);
drh2dcef112008-01-12 19:03:48 +00002617 pExpr->iTable = iMem;
drh678ccce2008-03-31 18:19:54 +00002618 pExpr->iColumn = pExpr->op;
drh25303782004-12-07 15:41:48 +00002619 pExpr->op = TK_REGISTER;
2620 }
drh2dcef112008-01-12 19:03:48 +00002621 return inReg;
drh25303782004-12-07 15:41:48 +00002622}
drh2dcef112008-01-12 19:03:48 +00002623
drh678ccce2008-03-31 18:19:54 +00002624/*
2625** If pExpr is a constant expression, then evaluate the expression
2626** into a register and convert the expression into a TK_REGISTER
2627** expression.
2628*/
2629static int evalConstExpr(void *pArg, Expr *pExpr){
2630 Parse *pParse = (Parse*)pArg;
2631 if( pExpr->op==TK_REGISTER ){
2632 return 1;
2633 }
2634 if( sqlite3ExprIsConstantNotJoin(pExpr) ){
2635 int r1 = ++pParse->nMem;
2636 int r2;
2637 r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
2638 if( r1!=r2 ) pParse->nMem--;
2639 pExpr->iColumn = pExpr->op;
2640 pExpr->op = TK_REGISTER;
2641 pExpr->iTable = r2;
2642 return 1;
2643 }
2644 return 0;
2645}
2646
2647/*
2648** Preevaluate constant subexpressions within pExpr and store the
2649** results in registers. Modify pExpr so that the constant subexpresions
2650** are TK_REGISTER opcodes that refer to the precomputed values.
2651*/
2652void sqlite3ExprCodeConstants(Parse *pParse, Expr *pExpr){
2653 walkExprTree(pExpr, evalConstExpr, pParse);
2654}
2655
drh25303782004-12-07 15:41:48 +00002656
2657/*
drh268380c2004-02-25 13:47:31 +00002658** Generate code that pushes the value of every element of the given
drh9cbf3422008-01-17 16:22:13 +00002659** expression list into a sequence of registers beginning at target.
drh268380c2004-02-25 13:47:31 +00002660**
drh892d3172008-01-10 03:46:36 +00002661** Return the number of elements evaluated.
drh268380c2004-02-25 13:47:31 +00002662*/
danielk19774adee202004-05-08 08:23:19 +00002663int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00002664 Parse *pParse, /* Parsing context */
drh389a1ad2008-01-03 23:44:53 +00002665 ExprList *pList, /* The expression list to be coded */
2666 int target /* Where to write results */
drh268380c2004-02-25 13:47:31 +00002667){
2668 struct ExprList_item *pItem;
drh9cbf3422008-01-17 16:22:13 +00002669 int i, n;
drh892d3172008-01-10 03:46:36 +00002670 assert( pList!=0 || pParse->db->mallocFailed );
2671 if( pList==0 ){
2672 return 0;
2673 }
drh9cbf3422008-01-17 16:22:13 +00002674 assert( target>0 );
drh268380c2004-02-25 13:47:31 +00002675 n = pList->nExpr;
drhc182d162005-08-14 20:47:16 +00002676 for(pItem=pList->a, i=n; i>0; i--, pItem++){
drh389a1ad2008-01-03 23:44:53 +00002677 sqlite3ExprCode(pParse, pItem->pExpr, target);
drh9cbf3422008-01-17 16:22:13 +00002678 target++;
drh268380c2004-02-25 13:47:31 +00002679 }
drhf9b596e2004-05-26 16:54:42 +00002680 return n;
drh268380c2004-02-25 13:47:31 +00002681}
2682
2683/*
drhcce7d172000-05-31 15:34:51 +00002684** Generate code for a boolean expression such that a jump is made
2685** to the label "dest" if the expression is true but execution
2686** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00002687**
2688** If the expression evaluates to NULL (neither true nor false), then
drh35573352008-01-08 23:54:25 +00002689** take the jump if the jumpIfNull flag is SQLITE_JUMPIFNULL.
drhf2bc0132004-10-04 13:19:23 +00002690**
2691** This code depends on the fact that certain token values (ex: TK_EQ)
2692** are the same as opcode values (ex: OP_Eq) that implement the corresponding
2693** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
2694** the make process cause these values to align. Assert()s in the code
2695** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00002696*/
danielk19774adee202004-05-08 08:23:19 +00002697void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00002698 Vdbe *v = pParse->pVdbe;
2699 int op = 0;
drh2dcef112008-01-12 19:03:48 +00002700 int regFree1 = 0;
2701 int regFree2 = 0;
2702 int r1, r2;
2703
drh35573352008-01-08 23:54:25 +00002704 assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
drhdaffd0e2001-04-11 14:28:42 +00002705 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00002706 op = pExpr->op;
2707 switch( op ){
drhcce7d172000-05-31 15:34:51 +00002708 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00002709 int d2 = sqlite3VdbeMakeLabel(v);
drh35573352008-01-08 23:54:25 +00002710 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,jumpIfNull^SQLITE_JUMPIFNULL);
drhe55cbd72008-03-31 23:48:03 +00002711 pParse->disableColCache++;
danielk19774adee202004-05-08 08:23:19 +00002712 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhe55cbd72008-03-31 23:48:03 +00002713 pParse->disableColCache--;
danielk19774adee202004-05-08 08:23:19 +00002714 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00002715 break;
2716 }
2717 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00002718 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhe55cbd72008-03-31 23:48:03 +00002719 pParse->disableColCache++;
danielk19774adee202004-05-08 08:23:19 +00002720 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhe55cbd72008-03-31 23:48:03 +00002721 pParse->disableColCache--;
drhcce7d172000-05-31 15:34:51 +00002722 break;
2723 }
2724 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00002725 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002726 break;
2727 }
2728 case TK_LT:
2729 case TK_LE:
2730 case TK_GT:
2731 case TK_GE:
2732 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00002733 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00002734 assert( TK_LT==OP_Lt );
2735 assert( TK_LE==OP_Le );
2736 assert( TK_GT==OP_Gt );
2737 assert( TK_GE==OP_Ge );
2738 assert( TK_EQ==OP_Eq );
2739 assert( TK_NE==OP_Ne );
drhda250ea2008-04-01 05:07:14 +00002740 codeCompareOperands(pParse, pExpr->pLeft, &r1, &regFree1,
2741 pExpr->pRight, &r2, &regFree2);
drh35573352008-01-08 23:54:25 +00002742 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
drh2dcef112008-01-12 19:03:48 +00002743 r1, r2, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002744 break;
2745 }
2746 case TK_ISNULL:
2747 case TK_NOTNULL: {
drhf2bc0132004-10-04 13:19:23 +00002748 assert( TK_ISNULL==OP_IsNull );
2749 assert( TK_NOTNULL==OP_NotNull );
drh2dcef112008-01-12 19:03:48 +00002750 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
2751 sqlite3VdbeAddOp2(v, op, r1, dest);
drhcce7d172000-05-31 15:34:51 +00002752 break;
2753 }
drhfef52082000-06-06 01:50:43 +00002754 case TK_BETWEEN: {
drh2dcef112008-01-12 19:03:48 +00002755 /* x BETWEEN y AND z
danielk19770202b292004-06-09 09:55:16 +00002756 **
drh2dcef112008-01-12 19:03:48 +00002757 ** Is equivalent to
2758 **
2759 ** x>=y AND x<=z
2760 **
2761 ** Code it as such, taking care to do the common subexpression
2762 ** elementation of x.
danielk19770202b292004-06-09 09:55:16 +00002763 */
drh2dcef112008-01-12 19:03:48 +00002764 Expr exprAnd;
2765 Expr compLeft;
2766 Expr compRight;
2767 Expr exprX;
danielk19770202b292004-06-09 09:55:16 +00002768
drh2dcef112008-01-12 19:03:48 +00002769 exprX = *pExpr->pLeft;
2770 exprAnd.op = TK_AND;
2771 exprAnd.pLeft = &compLeft;
2772 exprAnd.pRight = &compRight;
2773 compLeft.op = TK_GE;
2774 compLeft.pLeft = &exprX;
2775 compLeft.pRight = pExpr->pList->a[0].pExpr;
2776 compRight.op = TK_LE;
2777 compRight.pLeft = &exprX;
2778 compRight.pRight = pExpr->pList->a[1].pExpr;
2779 exprX.iTable = sqlite3ExprCodeTemp(pParse, &exprX, &regFree1);
2780 exprX.op = TK_REGISTER;
2781 sqlite3ExprIfTrue(pParse, &exprAnd, dest, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00002782 break;
2783 }
drhcce7d172000-05-31 15:34:51 +00002784 default: {
drh2dcef112008-01-12 19:03:48 +00002785 r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
2786 sqlite3VdbeAddOp3(v, OP_If, r1, dest, jumpIfNull!=0);
drhcce7d172000-05-31 15:34:51 +00002787 break;
2788 }
2789 }
drh2dcef112008-01-12 19:03:48 +00002790 sqlite3ReleaseTempReg(pParse, regFree1);
2791 sqlite3ReleaseTempReg(pParse, regFree2);
drhcce7d172000-05-31 15:34:51 +00002792}
2793
2794/*
drh66b89c82000-11-28 20:47:17 +00002795** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00002796** to the label "dest" if the expression is false but execution
2797** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00002798**
2799** If the expression evaluates to NULL (neither true nor false) then
drh35573352008-01-08 23:54:25 +00002800** jump if jumpIfNull is SQLITE_JUMPIFNULL or fall through if jumpIfNull
2801** is 0.
drhcce7d172000-05-31 15:34:51 +00002802*/
danielk19774adee202004-05-08 08:23:19 +00002803void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00002804 Vdbe *v = pParse->pVdbe;
2805 int op = 0;
drh2dcef112008-01-12 19:03:48 +00002806 int regFree1 = 0;
2807 int regFree2 = 0;
2808 int r1, r2;
2809
drh35573352008-01-08 23:54:25 +00002810 assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
drhdaffd0e2001-04-11 14:28:42 +00002811 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00002812
2813 /* The value of pExpr->op and op are related as follows:
2814 **
2815 ** pExpr->op op
2816 ** --------- ----------
2817 ** TK_ISNULL OP_NotNull
2818 ** TK_NOTNULL OP_IsNull
2819 ** TK_NE OP_Eq
2820 ** TK_EQ OP_Ne
2821 ** TK_GT OP_Le
2822 ** TK_LE OP_Gt
2823 ** TK_GE OP_Lt
2824 ** TK_LT OP_Ge
2825 **
2826 ** For other values of pExpr->op, op is undefined and unused.
2827 ** The value of TK_ and OP_ constants are arranged such that we
2828 ** can compute the mapping above using the following expression.
2829 ** Assert()s verify that the computation is correct.
2830 */
2831 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
2832
2833 /* Verify correct alignment of TK_ and OP_ constants
2834 */
2835 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
2836 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
2837 assert( pExpr->op!=TK_NE || op==OP_Eq );
2838 assert( pExpr->op!=TK_EQ || op==OP_Ne );
2839 assert( pExpr->op!=TK_LT || op==OP_Ge );
2840 assert( pExpr->op!=TK_LE || op==OP_Gt );
2841 assert( pExpr->op!=TK_GT || op==OP_Le );
2842 assert( pExpr->op!=TK_GE || op==OP_Lt );
2843
drhcce7d172000-05-31 15:34:51 +00002844 switch( pExpr->op ){
2845 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00002846 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhe55cbd72008-03-31 23:48:03 +00002847 pParse->disableColCache++;
danielk19774adee202004-05-08 08:23:19 +00002848 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhe55cbd72008-03-31 23:48:03 +00002849 pParse->disableColCache--;
drhcce7d172000-05-31 15:34:51 +00002850 break;
2851 }
2852 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00002853 int d2 = sqlite3VdbeMakeLabel(v);
drh35573352008-01-08 23:54:25 +00002854 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, jumpIfNull^SQLITE_JUMPIFNULL);
drhe55cbd72008-03-31 23:48:03 +00002855 pParse->disableColCache++;
danielk19774adee202004-05-08 08:23:19 +00002856 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhe55cbd72008-03-31 23:48:03 +00002857 pParse->disableColCache--;
danielk19774adee202004-05-08 08:23:19 +00002858 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00002859 break;
2860 }
2861 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00002862 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002863 break;
2864 }
2865 case TK_LT:
2866 case TK_LE:
2867 case TK_GT:
2868 case TK_GE:
2869 case TK_NE:
2870 case TK_EQ: {
drhda250ea2008-04-01 05:07:14 +00002871 codeCompareOperands(pParse, pExpr->pLeft, &r1, &regFree1,
2872 pExpr->pRight, &r2, &regFree2);
drh35573352008-01-08 23:54:25 +00002873 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
drh2dcef112008-01-12 19:03:48 +00002874 r1, r2, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002875 break;
2876 }
drhcce7d172000-05-31 15:34:51 +00002877 case TK_ISNULL:
2878 case TK_NOTNULL: {
drh2dcef112008-01-12 19:03:48 +00002879 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
2880 sqlite3VdbeAddOp2(v, op, r1, dest);
drhcce7d172000-05-31 15:34:51 +00002881 break;
2882 }
drhfef52082000-06-06 01:50:43 +00002883 case TK_BETWEEN: {
drh2dcef112008-01-12 19:03:48 +00002884 /* x BETWEEN y AND z
danielk19770202b292004-06-09 09:55:16 +00002885 **
drh2dcef112008-01-12 19:03:48 +00002886 ** Is equivalent to
2887 **
2888 ** x>=y AND x<=z
2889 **
2890 ** Code it as such, taking care to do the common subexpression
2891 ** elementation of x.
danielk19770202b292004-06-09 09:55:16 +00002892 */
drh2dcef112008-01-12 19:03:48 +00002893 Expr exprAnd;
2894 Expr compLeft;
2895 Expr compRight;
2896 Expr exprX;
drhbe5c89a2004-07-26 00:31:09 +00002897
drh2dcef112008-01-12 19:03:48 +00002898 exprX = *pExpr->pLeft;
2899 exprAnd.op = TK_AND;
2900 exprAnd.pLeft = &compLeft;
2901 exprAnd.pRight = &compRight;
2902 compLeft.op = TK_GE;
2903 compLeft.pLeft = &exprX;
2904 compLeft.pRight = pExpr->pList->a[0].pExpr;
2905 compRight.op = TK_LE;
2906 compRight.pLeft = &exprX;
2907 compRight.pRight = pExpr->pList->a[1].pExpr;
2908 exprX.iTable = sqlite3ExprCodeTemp(pParse, &exprX, &regFree1);
2909 exprX.op = TK_REGISTER;
2910 sqlite3ExprIfFalse(pParse, &exprAnd, dest, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00002911 break;
2912 }
drhcce7d172000-05-31 15:34:51 +00002913 default: {
drh2dcef112008-01-12 19:03:48 +00002914 r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
2915 sqlite3VdbeAddOp3(v, OP_IfNot, r1, dest, jumpIfNull!=0);
drhcce7d172000-05-31 15:34:51 +00002916 break;
2917 }
2918 }
drh2dcef112008-01-12 19:03:48 +00002919 sqlite3ReleaseTempReg(pParse, regFree1);
2920 sqlite3ReleaseTempReg(pParse, regFree2);
drhcce7d172000-05-31 15:34:51 +00002921}
drh22827922000-06-06 17:27:05 +00002922
2923/*
2924** Do a deep comparison of two expression trees. Return TRUE (non-zero)
2925** if they are identical and return FALSE if they differ in any way.
drhd40aab02007-02-24 15:29:03 +00002926**
2927** Sometimes this routine will return FALSE even if the two expressions
2928** really are equivalent. If we cannot prove that the expressions are
2929** identical, we return FALSE just to be safe. So if this routine
2930** returns false, then you do not really know for certain if the two
2931** expressions are the same. But if you get a TRUE return, then you
2932** can be sure the expressions are the same. In the places where
2933** this routine is used, it does not hurt to get an extra FALSE - that
2934** just might result in some slightly slower code. But returning
2935** an incorrect TRUE could lead to a malfunction.
drh22827922000-06-06 17:27:05 +00002936*/
danielk19774adee202004-05-08 08:23:19 +00002937int sqlite3ExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00002938 int i;
danielk19774b202ae2006-01-23 05:50:58 +00002939 if( pA==0||pB==0 ){
2940 return pB==pA;
drh22827922000-06-06 17:27:05 +00002941 }
2942 if( pA->op!=pB->op ) return 0;
drhfd357972005-09-09 01:33:19 +00002943 if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 0;
danielk19774adee202004-05-08 08:23:19 +00002944 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
2945 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00002946 if( pA->pList ){
2947 if( pB->pList==0 ) return 0;
2948 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
2949 for(i=0; i<pA->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00002950 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00002951 return 0;
2952 }
2953 }
2954 }else if( pB->pList ){
2955 return 0;
2956 }
2957 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00002958 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drhdd735212007-02-24 13:53:05 +00002959 if( pA->op!=TK_COLUMN && pA->token.z ){
drh22827922000-06-06 17:27:05 +00002960 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00002961 if( pB->token.n!=pA->token.n ) return 0;
drh2646da72005-12-09 20:02:05 +00002962 if( sqlite3StrNICmp((char*)pA->token.z,(char*)pB->token.z,pB->token.n)!=0 ){
2963 return 0;
2964 }
drh22827922000-06-06 17:27:05 +00002965 }
2966 return 1;
2967}
2968
drh13449892005-09-07 21:22:45 +00002969
drh22827922000-06-06 17:27:05 +00002970/*
drh13449892005-09-07 21:22:45 +00002971** Add a new element to the pAggInfo->aCol[] array. Return the index of
2972** the new element. Return a negative number if malloc fails.
drh22827922000-06-06 17:27:05 +00002973*/
drh17435752007-08-16 04:30:38 +00002974static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){
drh13449892005-09-07 21:22:45 +00002975 int i;
drhcf643722007-03-27 13:36:37 +00002976 pInfo->aCol = sqlite3ArrayAllocate(
drh17435752007-08-16 04:30:38 +00002977 db,
drhcf643722007-03-27 13:36:37 +00002978 pInfo->aCol,
2979 sizeof(pInfo->aCol[0]),
2980 3,
2981 &pInfo->nColumn,
2982 &pInfo->nColumnAlloc,
2983 &i
2984 );
drh13449892005-09-07 21:22:45 +00002985 return i;
2986}
2987
2988/*
2989** Add a new element to the pAggInfo->aFunc[] array. Return the index of
2990** the new element. Return a negative number if malloc fails.
2991*/
drh17435752007-08-16 04:30:38 +00002992static int addAggInfoFunc(sqlite3 *db, AggInfo *pInfo){
drh13449892005-09-07 21:22:45 +00002993 int i;
drhcf643722007-03-27 13:36:37 +00002994 pInfo->aFunc = sqlite3ArrayAllocate(
drh17435752007-08-16 04:30:38 +00002995 db,
drhcf643722007-03-27 13:36:37 +00002996 pInfo->aFunc,
2997 sizeof(pInfo->aFunc[0]),
2998 3,
2999 &pInfo->nFunc,
3000 &pInfo->nFuncAlloc,
3001 &i
3002 );
drh13449892005-09-07 21:22:45 +00003003 return i;
3004}
drh22827922000-06-06 17:27:05 +00003005
3006/*
drh626a8792005-01-17 22:08:19 +00003007** This is an xFunc for walkExprTree() used to implement
3008** sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates
3009** for additional information.
drh22827922000-06-06 17:27:05 +00003010**
drh626a8792005-01-17 22:08:19 +00003011** This routine analyzes the aggregate function at pExpr.
drh22827922000-06-06 17:27:05 +00003012*/
drh626a8792005-01-17 22:08:19 +00003013static int analyzeAggregate(void *pArg, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00003014 int i;
danielk1977a58fdfb2005-02-08 07:50:40 +00003015 NameContext *pNC = (NameContext *)pArg;
3016 Parse *pParse = pNC->pParse;
3017 SrcList *pSrcList = pNC->pSrcList;
drh13449892005-09-07 21:22:45 +00003018 AggInfo *pAggInfo = pNC->pAggInfo;
drh22827922000-06-06 17:27:05 +00003019
drh22827922000-06-06 17:27:05 +00003020 switch( pExpr->op ){
drh89c69d02007-01-04 01:20:28 +00003021 case TK_AGG_COLUMN:
drh967e8b72000-06-21 13:59:10 +00003022 case TK_COLUMN: {
drh13449892005-09-07 21:22:45 +00003023 /* Check to see if the column is in one of the tables in the FROM
3024 ** clause of the aggregate query */
3025 if( pSrcList ){
3026 struct SrcList_item *pItem = pSrcList->a;
3027 for(i=0; i<pSrcList->nSrc; i++, pItem++){
3028 struct AggInfo_col *pCol;
3029 if( pExpr->iTable==pItem->iCursor ){
3030 /* If we reach this point, it means that pExpr refers to a table
3031 ** that is in the FROM clause of the aggregate query.
3032 **
3033 ** Make an entry for the column in pAggInfo->aCol[] if there
3034 ** is not an entry there already.
3035 */
drh7f906d62007-03-12 23:48:52 +00003036 int k;
drh13449892005-09-07 21:22:45 +00003037 pCol = pAggInfo->aCol;
drh7f906d62007-03-12 23:48:52 +00003038 for(k=0; k<pAggInfo->nColumn; k++, pCol++){
drh13449892005-09-07 21:22:45 +00003039 if( pCol->iTable==pExpr->iTable &&
3040 pCol->iColumn==pExpr->iColumn ){
3041 break;
3042 }
danielk1977a58fdfb2005-02-08 07:50:40 +00003043 }
danielk19771e536952007-08-16 10:09:01 +00003044 if( (k>=pAggInfo->nColumn)
3045 && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0
3046 ){
drh7f906d62007-03-12 23:48:52 +00003047 pCol = &pAggInfo->aCol[k];
danielk19770817d0d2007-02-14 09:19:36 +00003048 pCol->pTab = pExpr->pTab;
drh13449892005-09-07 21:22:45 +00003049 pCol->iTable = pExpr->iTable;
3050 pCol->iColumn = pExpr->iColumn;
drh0a07c102008-01-03 18:03:08 +00003051 pCol->iMem = ++pParse->nMem;
drh13449892005-09-07 21:22:45 +00003052 pCol->iSorterColumn = -1;
drh5774b802005-09-07 22:48:16 +00003053 pCol->pExpr = pExpr;
drh13449892005-09-07 21:22:45 +00003054 if( pAggInfo->pGroupBy ){
3055 int j, n;
3056 ExprList *pGB = pAggInfo->pGroupBy;
3057 struct ExprList_item *pTerm = pGB->a;
3058 n = pGB->nExpr;
3059 for(j=0; j<n; j++, pTerm++){
3060 Expr *pE = pTerm->pExpr;
3061 if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable &&
3062 pE->iColumn==pExpr->iColumn ){
3063 pCol->iSorterColumn = j;
3064 break;
3065 }
3066 }
3067 }
3068 if( pCol->iSorterColumn<0 ){
3069 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
3070 }
3071 }
3072 /* There is now an entry for pExpr in pAggInfo->aCol[] (either
3073 ** because it was there before or because we just created it).
3074 ** Convert the pExpr to be a TK_AGG_COLUMN referring to that
3075 ** pAggInfo->aCol[] entry.
3076 */
3077 pExpr->pAggInfo = pAggInfo;
3078 pExpr->op = TK_AGG_COLUMN;
drh7f906d62007-03-12 23:48:52 +00003079 pExpr->iAgg = k;
drh13449892005-09-07 21:22:45 +00003080 break;
3081 } /* endif pExpr->iTable==pItem->iCursor */
3082 } /* end loop over pSrcList */
drh22827922000-06-06 17:27:05 +00003083 }
drh626a8792005-01-17 22:08:19 +00003084 return 1;
drh22827922000-06-06 17:27:05 +00003085 }
3086 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00003087 /* The pNC->nDepth==0 test causes aggregate functions in subqueries
3088 ** to be ignored */
danielk1977a58fdfb2005-02-08 07:50:40 +00003089 if( pNC->nDepth==0 ){
drh13449892005-09-07 21:22:45 +00003090 /* Check to see if pExpr is a duplicate of another aggregate
3091 ** function that is already in the pAggInfo structure
3092 */
3093 struct AggInfo_func *pItem = pAggInfo->aFunc;
3094 for(i=0; i<pAggInfo->nFunc; i++, pItem++){
3095 if( sqlite3ExprCompare(pItem->pExpr, pExpr) ){
danielk1977a58fdfb2005-02-08 07:50:40 +00003096 break;
3097 }
drh22827922000-06-06 17:27:05 +00003098 }
drh13449892005-09-07 21:22:45 +00003099 if( i>=pAggInfo->nFunc ){
3100 /* pExpr is original. Make a new entry in pAggInfo->aFunc[]
3101 */
danielk197714db2662006-01-09 16:12:04 +00003102 u8 enc = ENC(pParse->db);
danielk19771e536952007-08-16 10:09:01 +00003103 i = addAggInfoFunc(pParse->db, pAggInfo);
drh13449892005-09-07 21:22:45 +00003104 if( i>=0 ){
3105 pItem = &pAggInfo->aFunc[i];
3106 pItem->pExpr = pExpr;
drh0a07c102008-01-03 18:03:08 +00003107 pItem->iMem = ++pParse->nMem;
drh13449892005-09-07 21:22:45 +00003108 pItem->pFunc = sqlite3FindFunction(pParse->db,
drh2646da72005-12-09 20:02:05 +00003109 (char*)pExpr->token.z, pExpr->token.n,
drh13449892005-09-07 21:22:45 +00003110 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
drhfd357972005-09-09 01:33:19 +00003111 if( pExpr->flags & EP_Distinct ){
3112 pItem->iDistinct = pParse->nTab++;
3113 }else{
3114 pItem->iDistinct = -1;
3115 }
drh13449892005-09-07 21:22:45 +00003116 }
danielk1977a58fdfb2005-02-08 07:50:40 +00003117 }
drh13449892005-09-07 21:22:45 +00003118 /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
3119 */
danielk1977a58fdfb2005-02-08 07:50:40 +00003120 pExpr->iAgg = i;
drh13449892005-09-07 21:22:45 +00003121 pExpr->pAggInfo = pAggInfo;
danielk1977a58fdfb2005-02-08 07:50:40 +00003122 return 1;
drh22827922000-06-06 17:27:05 +00003123 }
drh22827922000-06-06 17:27:05 +00003124 }
3125 }
drh13449892005-09-07 21:22:45 +00003126
3127 /* Recursively walk subqueries looking for TK_COLUMN nodes that need
3128 ** to be changed to TK_AGG_COLUMN. But increment nDepth so that
3129 ** TK_AGG_FUNCTION nodes in subqueries will be unchanged.
3130 */
danielk1977a58fdfb2005-02-08 07:50:40 +00003131 if( pExpr->pSelect ){
3132 pNC->nDepth++;
3133 walkSelectExpr(pExpr->pSelect, analyzeAggregate, pNC);
3134 pNC->nDepth--;
3135 }
drh626a8792005-01-17 22:08:19 +00003136 return 0;
3137}
3138
3139/*
3140** Analyze the given expression looking for aggregate functions and
3141** for variables that need to be added to the pParse->aAgg[] array.
3142** Make additional entries to the pParse->aAgg[] array as necessary.
3143**
3144** This routine should only be called after the expression has been
3145** analyzed by sqlite3ExprResolveNames().
drh626a8792005-01-17 22:08:19 +00003146*/
drhd2b3e232008-01-23 14:51:49 +00003147void sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
danielk1977a58fdfb2005-02-08 07:50:40 +00003148 walkExprTree(pExpr, analyzeAggregate, pNC);
drh22827922000-06-06 17:27:05 +00003149}
drh5d9a4af2005-08-30 00:54:01 +00003150
3151/*
3152** Call sqlite3ExprAnalyzeAggregates() for every expression in an
3153** expression list. Return the number of errors.
3154**
3155** If an error is found, the analysis is cut short.
3156*/
drhd2b3e232008-01-23 14:51:49 +00003157void sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){
drh5d9a4af2005-08-30 00:54:01 +00003158 struct ExprList_item *pItem;
3159 int i;
drh5d9a4af2005-08-30 00:54:01 +00003160 if( pList ){
drhd2b3e232008-01-23 14:51:49 +00003161 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
3162 sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
drh5d9a4af2005-08-30 00:54:01 +00003163 }
3164 }
drh5d9a4af2005-08-30 00:54:01 +00003165}
drh892d3172008-01-10 03:46:36 +00003166
3167/*
3168** Allocate or deallocate temporary use registers during code generation.
3169*/
3170int sqlite3GetTempReg(Parse *pParse){
drhe55cbd72008-03-31 23:48:03 +00003171 int i, r;
3172 if( pParse->nTempReg==0 ){
drh892d3172008-01-10 03:46:36 +00003173 return ++pParse->nMem;
3174 }
drhe55cbd72008-03-31 23:48:03 +00003175 for(i=0; i<pParse->nTempReg; i++){
3176 r = pParse->aTempReg[i];
3177 if( usedAsColumnCache(pParse, r, r) ) continue;
3178 }
3179 if( i>=pParse->nTempReg ){
3180 return ++pParse->nMem;
3181 }
3182 while( i<pParse->nTempReg-1 ){
3183 pParse->aTempReg[i] = pParse->aTempReg[i+1];
3184 }
3185 pParse->nTempReg--;
3186 return r;
drh892d3172008-01-10 03:46:36 +00003187}
3188void sqlite3ReleaseTempReg(Parse *pParse, int iReg){
drh2dcef112008-01-12 19:03:48 +00003189 if( iReg && pParse->nTempReg<ArraySize(pParse->aTempReg) ){
3190 assert( iReg>0 );
drh892d3172008-01-10 03:46:36 +00003191 pParse->aTempReg[pParse->nTempReg++] = iReg;
3192 }
3193}
3194
3195/*
3196** Allocate or deallocate a block of nReg consecutive registers
3197*/
3198int sqlite3GetTempRange(Parse *pParse, int nReg){
drhe55cbd72008-03-31 23:48:03 +00003199 int i, n;
3200 i = pParse->iRangeReg;
3201 n = pParse->nRangeReg;
3202 if( nReg<=n && !usedAsColumnCache(pParse, i, i+n-1) ){
drh892d3172008-01-10 03:46:36 +00003203 pParse->iRangeReg += nReg;
3204 pParse->nRangeReg -= nReg;
3205 }else{
3206 i = pParse->nMem+1;
3207 pParse->nMem += nReg;
3208 }
3209 return i;
3210}
3211void sqlite3ReleaseTempRange(Parse *pParse, int iReg, int nReg){
3212 if( nReg>pParse->nRangeReg ){
3213 pParse->nRangeReg = nReg;
3214 pParse->iRangeReg = iReg;
3215 }
3216}