blob: aa01fb9a39c8d0bf8c94c08a4969d93f2b75fb56 [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**
drhc5499be2008-04-01 15:06:33 +000015** $Id: expr.c,v 1.364 2008/04/01 15:06:34 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]);
drhc5499be2008-04-01 15:06:33 +0000431 testcase( i==0 );
432 testcase( i==1 );
433 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 );
434 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] );
drhbb4957f2008-03-20 14:03:29 +0000435 if( i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
drhfa6bc002004-09-07 16:19:52 +0000436 sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
drhbb4957f2008-03-20 14:03:29 +0000437 db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]);
drhfa6bc002004-09-07 16:19:52 +0000438 }
439 if( i>pParse->nVar ){
440 pParse->nVar = i;
441 }
442 }else{
443 /* Wildcards of the form ":aaa" or "$aaa". Reuse the same variable
444 ** number as the prior appearance of the same name, or if the name
445 ** has never appeared before, reuse the same variable number
446 */
447 int i, n;
448 n = pToken->n;
449 for(i=0; i<pParse->nVarExpr; i++){
450 Expr *pE;
451 if( (pE = pParse->apVarExpr[i])!=0
452 && pE->token.n==n
453 && memcmp(pE->token.z, pToken->z, n)==0 ){
454 pExpr->iTable = pE->iTable;
455 break;
456 }
457 }
458 if( i>=pParse->nVarExpr ){
459 pExpr->iTable = ++pParse->nVar;
460 if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){
461 pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10;
drh17435752007-08-16 04:30:38 +0000462 pParse->apVarExpr =
463 sqlite3DbReallocOrFree(
464 db,
465 pParse->apVarExpr,
466 pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0])
467 );
drhfa6bc002004-09-07 16:19:52 +0000468 }
drh17435752007-08-16 04:30:38 +0000469 if( !db->mallocFailed ){
drhfa6bc002004-09-07 16:19:52 +0000470 assert( pParse->apVarExpr!=0 );
471 pParse->apVarExpr[pParse->nVarExpr++] = pExpr;
472 }
473 }
474 }
drhbb4957f2008-03-20 14:03:29 +0000475 if( !pParse->nErr && pParse->nVar>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
danielk1977832b2662007-05-09 11:37:22 +0000476 sqlite3ErrorMsg(pParse, "too many SQL variables");
477 }
drhfa6bc002004-09-07 16:19:52 +0000478}
479
480/*
drha2e00042002-01-22 03:13:42 +0000481** Recursively delete an expression tree.
482*/
danielk19774adee202004-05-08 08:23:19 +0000483void sqlite3ExprDelete(Expr *p){
drha2e00042002-01-22 03:13:42 +0000484 if( p==0 ) return;
drh17435752007-08-16 04:30:38 +0000485 if( p->span.dyn ) sqlite3_free((char*)p->span.z);
486 if( p->token.dyn ) sqlite3_free((char*)p->token.z);
danielk19774adee202004-05-08 08:23:19 +0000487 sqlite3ExprDelete(p->pLeft);
488 sqlite3ExprDelete(p->pRight);
489 sqlite3ExprListDelete(p->pList);
490 sqlite3SelectDelete(p->pSelect);
drh17435752007-08-16 04:30:38 +0000491 sqlite3_free(p);
drha2e00042002-01-22 03:13:42 +0000492}
493
drhd2687b72005-08-12 22:56:09 +0000494/*
495** The Expr.token field might be a string literal that is quoted.
496** If so, remove the quotation marks.
497*/
drh17435752007-08-16 04:30:38 +0000498void sqlite3DequoteExpr(sqlite3 *db, Expr *p){
drhd2687b72005-08-12 22:56:09 +0000499 if( ExprHasAnyProperty(p, EP_Dequoted) ){
500 return;
501 }
502 ExprSetProperty(p, EP_Dequoted);
503 if( p->token.dyn==0 ){
drh17435752007-08-16 04:30:38 +0000504 sqlite3TokenCopy(db, &p->token, &p->token);
drhd2687b72005-08-12 22:56:09 +0000505 }
506 sqlite3Dequote((char*)p->token.z);
507}
508
drha76b5df2002-02-23 02:32:10 +0000509
510/*
drhff78bd22002-02-27 01:47:11 +0000511** The following group of routines make deep copies of expressions,
512** expression lists, ID lists, and select statements. The copies can
513** be deleted (by being passed to their respective ...Delete() routines)
514** without effecting the originals.
515**
danielk19774adee202004-05-08 08:23:19 +0000516** The expression list, ID, and source lists return by sqlite3ExprListDup(),
517** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
drhad3cab52002-05-24 02:04:32 +0000518** by subsequent calls to sqlite*ListAppend() routines.
drhff78bd22002-02-27 01:47:11 +0000519**
drhad3cab52002-05-24 02:04:32 +0000520** Any tables that the SrcList might point to are not duplicated.
drhff78bd22002-02-27 01:47:11 +0000521*/
danielk19771e536952007-08-16 10:09:01 +0000522Expr *sqlite3ExprDup(sqlite3 *db, Expr *p){
drhff78bd22002-02-27 01:47:11 +0000523 Expr *pNew;
524 if( p==0 ) return 0;
drh17435752007-08-16 04:30:38 +0000525 pNew = sqlite3DbMallocRaw(db, sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000526 if( pNew==0 ) return 0;
drh3b167c72002-06-28 12:18:47 +0000527 memcpy(pNew, p, sizeof(*pNew));
drh6977fea2002-10-22 23:38:04 +0000528 if( p->token.z!=0 ){
drh17435752007-08-16 04:30:38 +0000529 pNew->token.z = (u8*)sqlite3DbStrNDup(db, (char*)p->token.z, p->token.n);
drh4b59ab52002-08-24 18:24:51 +0000530 pNew->token.dyn = 1;
531 }else{
drh4efc4752004-01-16 15:55:37 +0000532 assert( pNew->token.z==0 );
drh4b59ab52002-08-24 18:24:51 +0000533 }
drh6977fea2002-10-22 23:38:04 +0000534 pNew->span.z = 0;
drh17435752007-08-16 04:30:38 +0000535 pNew->pLeft = sqlite3ExprDup(db, p->pLeft);
536 pNew->pRight = sqlite3ExprDup(db, p->pRight);
537 pNew->pList = sqlite3ExprListDup(db, p->pList);
538 pNew->pSelect = sqlite3SelectDup(db, p->pSelect);
drhff78bd22002-02-27 01:47:11 +0000539 return pNew;
540}
drh17435752007-08-16 04:30:38 +0000541void sqlite3TokenCopy(sqlite3 *db, Token *pTo, Token *pFrom){
542 if( pTo->dyn ) sqlite3_free((char*)pTo->z);
drh4b59ab52002-08-24 18:24:51 +0000543 if( pFrom->z ){
544 pTo->n = pFrom->n;
drh17435752007-08-16 04:30:38 +0000545 pTo->z = (u8*)sqlite3DbStrNDup(db, (char*)pFrom->z, pFrom->n);
drh4b59ab52002-08-24 18:24:51 +0000546 pTo->dyn = 1;
547 }else{
drh4b59ab52002-08-24 18:24:51 +0000548 pTo->z = 0;
drh4b59ab52002-08-24 18:24:51 +0000549 }
550}
drh17435752007-08-16 04:30:38 +0000551ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p){
drhff78bd22002-02-27 01:47:11 +0000552 ExprList *pNew;
drh145716b2004-09-24 12:24:06 +0000553 struct ExprList_item *pItem, *pOldItem;
drhff78bd22002-02-27 01:47:11 +0000554 int i;
555 if( p==0 ) return 0;
drh17435752007-08-16 04:30:38 +0000556 pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +0000557 if( pNew==0 ) return 0;
danielk197731dad9d2007-08-16 11:36:15 +0000558 pNew->iECursor = 0;
drh4305d102003-07-30 12:34:12 +0000559 pNew->nExpr = pNew->nAlloc = p->nExpr;
drh17435752007-08-16 04:30:38 +0000560 pNew->a = pItem = sqlite3DbMallocRaw(db, p->nExpr*sizeof(p->a[0]) );
danielk1977e0048402004-06-15 16:51:01 +0000561 if( pItem==0 ){
drh17435752007-08-16 04:30:38 +0000562 sqlite3_free(pNew);
danielk1977e0048402004-06-15 16:51:01 +0000563 return 0;
564 }
drh145716b2004-09-24 12:24:06 +0000565 pOldItem = p->a;
566 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
drh4b59ab52002-08-24 18:24:51 +0000567 Expr *pNewExpr, *pOldExpr;
drh17435752007-08-16 04:30:38 +0000568 pItem->pExpr = pNewExpr = sqlite3ExprDup(db, pOldExpr = pOldItem->pExpr);
drh6977fea2002-10-22 23:38:04 +0000569 if( pOldExpr->span.z!=0 && pNewExpr ){
570 /* Always make a copy of the span for top-level expressions in the
drh4b59ab52002-08-24 18:24:51 +0000571 ** expression list. The logic in SELECT processing that determines
572 ** the names of columns in the result set needs this information */
drh17435752007-08-16 04:30:38 +0000573 sqlite3TokenCopy(db, &pNewExpr->span, &pOldExpr->span);
drh4b59ab52002-08-24 18:24:51 +0000574 }
drh1f3e9052002-10-31 00:09:39 +0000575 assert( pNewExpr==0 || pNewExpr->span.z!=0
drh6f7adc82006-01-11 21:41:20 +0000576 || pOldExpr->span.z==0
drh17435752007-08-16 04:30:38 +0000577 || db->mallocFailed );
578 pItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
drh145716b2004-09-24 12:24:06 +0000579 pItem->sortOrder = pOldItem->sortOrder;
580 pItem->isAgg = pOldItem->isAgg;
drh3e7bc9c2004-02-21 19:17:17 +0000581 pItem->done = 0;
drhff78bd22002-02-27 01:47:11 +0000582 }
583 return pNew;
584}
danielk197793758c82005-01-21 08:13:14 +0000585
586/*
587** If cursors, triggers, views and subqueries are all omitted from
588** the build, then none of the following routines, except for
589** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes
590** called with a NULL argument.
591*/
danielk19776a67fe82005-02-04 04:07:16 +0000592#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \
593 || !defined(SQLITE_OMIT_SUBQUERY)
drh17435752007-08-16 04:30:38 +0000594SrcList *sqlite3SrcListDup(sqlite3 *db, SrcList *p){
drhad3cab52002-05-24 02:04:32 +0000595 SrcList *pNew;
596 int i;
drh113088e2003-03-20 01:16:58 +0000597 int nByte;
drhad3cab52002-05-24 02:04:32 +0000598 if( p==0 ) return 0;
drh113088e2003-03-20 01:16:58 +0000599 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
drh17435752007-08-16 04:30:38 +0000600 pNew = sqlite3DbMallocRaw(db, nByte );
drhad3cab52002-05-24 02:04:32 +0000601 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000602 pNew->nSrc = pNew->nAlloc = p->nSrc;
drhad3cab52002-05-24 02:04:32 +0000603 for(i=0; i<p->nSrc; i++){
drh4efc4752004-01-16 15:55:37 +0000604 struct SrcList_item *pNewItem = &pNew->a[i];
605 struct SrcList_item *pOldItem = &p->a[i];
drhed8a3bb2005-06-06 21:19:56 +0000606 Table *pTab;
drh17435752007-08-16 04:30:38 +0000607 pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase);
608 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
609 pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias);
drh4efc4752004-01-16 15:55:37 +0000610 pNewItem->jointype = pOldItem->jointype;
611 pNewItem->iCursor = pOldItem->iCursor;
danielk19771787cca2006-02-10 07:07:14 +0000612 pNewItem->isPopulated = pOldItem->isPopulated;
drhed8a3bb2005-06-06 21:19:56 +0000613 pTab = pNewItem->pTab = pOldItem->pTab;
614 if( pTab ){
615 pTab->nRef++;
danielk1977a1cb1832005-02-12 08:59:55 +0000616 }
drh17435752007-08-16 04:30:38 +0000617 pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect);
618 pNewItem->pOn = sqlite3ExprDup(db, pOldItem->pOn);
619 pNewItem->pUsing = sqlite3IdListDup(db, pOldItem->pUsing);
danielk19776c18b6e2005-01-30 09:17:58 +0000620 pNewItem->colUsed = pOldItem->colUsed;
drhad3cab52002-05-24 02:04:32 +0000621 }
622 return pNew;
623}
drh17435752007-08-16 04:30:38 +0000624IdList *sqlite3IdListDup(sqlite3 *db, IdList *p){
drhff78bd22002-02-27 01:47:11 +0000625 IdList *pNew;
626 int i;
627 if( p==0 ) return 0;
drh17435752007-08-16 04:30:38 +0000628 pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +0000629 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000630 pNew->nId = pNew->nAlloc = p->nId;
drh17435752007-08-16 04:30:38 +0000631 pNew->a = sqlite3DbMallocRaw(db, p->nId*sizeof(p->a[0]) );
danielk1977d5d56522005-03-16 12:15:20 +0000632 if( pNew->a==0 ){
drh17435752007-08-16 04:30:38 +0000633 sqlite3_free(pNew);
danielk1977d5d56522005-03-16 12:15:20 +0000634 return 0;
635 }
drhff78bd22002-02-27 01:47:11 +0000636 for(i=0; i<p->nId; i++){
drh4efc4752004-01-16 15:55:37 +0000637 struct IdList_item *pNewItem = &pNew->a[i];
638 struct IdList_item *pOldItem = &p->a[i];
drh17435752007-08-16 04:30:38 +0000639 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
drh4efc4752004-01-16 15:55:37 +0000640 pNewItem->idx = pOldItem->idx;
drhff78bd22002-02-27 01:47:11 +0000641 }
642 return pNew;
643}
drh17435752007-08-16 04:30:38 +0000644Select *sqlite3SelectDup(sqlite3 *db, Select *p){
drhff78bd22002-02-27 01:47:11 +0000645 Select *pNew;
646 if( p==0 ) return 0;
drh17435752007-08-16 04:30:38 +0000647 pNew = sqlite3DbMallocRaw(db, sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000648 if( pNew==0 ) return 0;
649 pNew->isDistinct = p->isDistinct;
drh17435752007-08-16 04:30:38 +0000650 pNew->pEList = sqlite3ExprListDup(db, p->pEList);
651 pNew->pSrc = sqlite3SrcListDup(db, p->pSrc);
652 pNew->pWhere = sqlite3ExprDup(db, p->pWhere);
653 pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy);
654 pNew->pHaving = sqlite3ExprDup(db, p->pHaving);
655 pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy);
drhff78bd22002-02-27 01:47:11 +0000656 pNew->op = p->op;
drh17435752007-08-16 04:30:38 +0000657 pNew->pPrior = sqlite3SelectDup(db, p->pPrior);
658 pNew->pLimit = sqlite3ExprDup(db, p->pLimit);
659 pNew->pOffset = sqlite3ExprDup(db, p->pOffset);
drh7b58dae2003-07-20 01:16:46 +0000660 pNew->iLimit = -1;
661 pNew->iOffset = -1;
danielk1977a1cb1832005-02-12 08:59:55 +0000662 pNew->isResolved = p->isResolved;
663 pNew->isAgg = p->isAgg;
drhb9bb7c12006-06-11 23:41:55 +0000664 pNew->usesEphm = 0;
drh8e647b82005-09-23 21:11:53 +0000665 pNew->disallowOrderBy = 0;
drh0342b1f2005-09-01 03:07:44 +0000666 pNew->pRightmost = 0;
drhb9bb7c12006-06-11 23:41:55 +0000667 pNew->addrOpenEphm[0] = -1;
668 pNew->addrOpenEphm[1] = -1;
669 pNew->addrOpenEphm[2] = -1;
drhff78bd22002-02-27 01:47:11 +0000670 return pNew;
671}
danielk197793758c82005-01-21 08:13:14 +0000672#else
drh17435752007-08-16 04:30:38 +0000673Select *sqlite3SelectDup(sqlite3 *db, Select *p){
danielk197793758c82005-01-21 08:13:14 +0000674 assert( p==0 );
675 return 0;
676}
677#endif
drhff78bd22002-02-27 01:47:11 +0000678
679
680/*
drha76b5df2002-02-23 02:32:10 +0000681** Add a new element to the end of an expression list. If pList is
682** initially NULL, then create a new expression list.
683*/
drh17435752007-08-16 04:30:38 +0000684ExprList *sqlite3ExprListAppend(
685 Parse *pParse, /* Parsing context */
686 ExprList *pList, /* List to which to append. Might be NULL */
687 Expr *pExpr, /* Expression to be appended */
688 Token *pName /* AS keyword for the expression */
689){
690 sqlite3 *db = pParse->db;
drha76b5df2002-02-23 02:32:10 +0000691 if( pList==0 ){
drh17435752007-08-16 04:30:38 +0000692 pList = sqlite3DbMallocZero(db, sizeof(ExprList) );
drha76b5df2002-02-23 02:32:10 +0000693 if( pList==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000694 goto no_mem;
drha76b5df2002-02-23 02:32:10 +0000695 }
drh4efc4752004-01-16 15:55:37 +0000696 assert( pList->nAlloc==0 );
drha76b5df2002-02-23 02:32:10 +0000697 }
drh4305d102003-07-30 12:34:12 +0000698 if( pList->nAlloc<=pList->nExpr ){
danielk1977d5d56522005-03-16 12:15:20 +0000699 struct ExprList_item *a;
700 int n = pList->nAlloc*2 + 4;
danielk197726783a52007-08-29 14:06:22 +0000701 a = sqlite3DbRealloc(db, pList->a, n*sizeof(pList->a[0]));
danielk1977d5d56522005-03-16 12:15:20 +0000702 if( a==0 ){
703 goto no_mem;
drha76b5df2002-02-23 02:32:10 +0000704 }
danielk1977d5d56522005-03-16 12:15:20 +0000705 pList->a = a;
706 pList->nAlloc = n;
drha76b5df2002-02-23 02:32:10 +0000707 }
drh4efc4752004-01-16 15:55:37 +0000708 assert( pList->a!=0 );
709 if( pExpr || pName ){
710 struct ExprList_item *pItem = &pList->a[pList->nExpr++];
711 memset(pItem, 0, sizeof(*pItem));
drh17435752007-08-16 04:30:38 +0000712 pItem->zName = sqlite3NameFromToken(db, pName);
danielk1977e94ddc92005-03-21 03:53:38 +0000713 pItem->pExpr = pExpr;
drha76b5df2002-02-23 02:32:10 +0000714 }
715 return pList;
danielk1977d5d56522005-03-16 12:15:20 +0000716
717no_mem:
718 /* Avoid leaking memory if malloc has failed. */
719 sqlite3ExprDelete(pExpr);
720 sqlite3ExprListDelete(pList);
721 return 0;
drha76b5df2002-02-23 02:32:10 +0000722}
723
724/*
danielk19777a15a4b2007-05-08 17:54:43 +0000725** If the expression list pEList contains more than iLimit elements,
726** leave an error message in pParse.
727*/
728void sqlite3ExprListCheckLength(
729 Parse *pParse,
730 ExprList *pEList,
danielk19777a15a4b2007-05-08 17:54:43 +0000731 const char *zObject
732){
drhb1a6c3c2008-03-20 16:30:17 +0000733 int mx = pParse->db->aLimit[SQLITE_LIMIT_COLUMN];
drhc5499be2008-04-01 15:06:33 +0000734 testcase( pEList && pEList->nExpr==mx );
735 testcase( pEList && pEList->nExpr==mx+1 );
drhb1a6c3c2008-03-20 16:30:17 +0000736 if( pEList && pEList->nExpr>mx ){
danielk19777a15a4b2007-05-08 17:54:43 +0000737 sqlite3ErrorMsg(pParse, "too many columns in %s", zObject);
738 }
739}
740
danielk1977fc976062007-05-10 10:46:56 +0000741
danielk1977fc976062007-05-10 10:46:56 +0000742/* The following three functions, heightOfExpr(), heightOfExprList()
743** and heightOfSelect(), are used to determine the maximum height
744** of any expression tree referenced by the structure passed as the
745** first argument.
746**
747** If this maximum height is greater than the current value pointed
748** to by pnHeight, the second parameter, then set *pnHeight to that
749** value.
750*/
751static void heightOfExpr(Expr *p, int *pnHeight){
752 if( p ){
753 if( p->nHeight>*pnHeight ){
754 *pnHeight = p->nHeight;
755 }
756 }
757}
758static void heightOfExprList(ExprList *p, int *pnHeight){
759 if( p ){
760 int i;
761 for(i=0; i<p->nExpr; i++){
762 heightOfExpr(p->a[i].pExpr, pnHeight);
763 }
764 }
765}
766static void heightOfSelect(Select *p, int *pnHeight){
767 if( p ){
768 heightOfExpr(p->pWhere, pnHeight);
769 heightOfExpr(p->pHaving, pnHeight);
770 heightOfExpr(p->pLimit, pnHeight);
771 heightOfExpr(p->pOffset, pnHeight);
772 heightOfExprList(p->pEList, pnHeight);
773 heightOfExprList(p->pGroupBy, pnHeight);
774 heightOfExprList(p->pOrderBy, pnHeight);
775 heightOfSelect(p->pPrior, pnHeight);
776 }
777}
778
779/*
780** Set the Expr.nHeight variable in the structure passed as an
781** argument. An expression with no children, Expr.pList or
782** Expr.pSelect member has a height of 1. Any other expression
783** has a height equal to the maximum height of any other
784** referenced Expr plus one.
785*/
786void sqlite3ExprSetHeight(Expr *p){
787 int nHeight = 0;
788 heightOfExpr(p->pLeft, &nHeight);
789 heightOfExpr(p->pRight, &nHeight);
790 heightOfExprList(p->pList, &nHeight);
791 heightOfSelect(p->pSelect, &nHeight);
792 p->nHeight = nHeight + 1;
793}
794
795/*
796** Return the maximum height of any expression tree referenced
797** by the select statement passed as an argument.
798*/
799int sqlite3SelectExprHeight(Select *p){
800 int nHeight = 0;
801 heightOfSelect(p, &nHeight);
802 return nHeight;
803}
danielk1977fc976062007-05-10 10:46:56 +0000804
danielk19777a15a4b2007-05-08 17:54:43 +0000805/*
drha76b5df2002-02-23 02:32:10 +0000806** Delete an entire expression list.
807*/
danielk19774adee202004-05-08 08:23:19 +0000808void sqlite3ExprListDelete(ExprList *pList){
drha76b5df2002-02-23 02:32:10 +0000809 int i;
drhbe5c89a2004-07-26 00:31:09 +0000810 struct ExprList_item *pItem;
drha76b5df2002-02-23 02:32:10 +0000811 if( pList==0 ) return;
drh1bdd9b52004-04-23 17:04:44 +0000812 assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
813 assert( pList->nExpr<=pList->nAlloc );
drhbe5c89a2004-07-26 00:31:09 +0000814 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
815 sqlite3ExprDelete(pItem->pExpr);
drh17435752007-08-16 04:30:38 +0000816 sqlite3_free(pItem->zName);
drha76b5df2002-02-23 02:32:10 +0000817 }
drh17435752007-08-16 04:30:38 +0000818 sqlite3_free(pList->a);
819 sqlite3_free(pList);
drha76b5df2002-02-23 02:32:10 +0000820}
821
822/*
drh678ccce2008-03-31 18:19:54 +0000823** Walk an expression tree. Call xFunc for each node visited. xFunc
824** is called on the node before xFunc is called on the nodes children.
drh73b211a2005-01-18 04:00:42 +0000825**
drh626a8792005-01-17 22:08:19 +0000826** The return value from xFunc determines whether the tree walk continues.
827** 0 means continue walking the tree. 1 means do not walk children
828** of the current node but continue with siblings. 2 means abandon
829** the tree walk completely.
830**
831** The return value from this routine is 1 to abandon the tree walk
832** and 0 to continue.
drh87abf5c2005-08-25 12:45:04 +0000833**
834** NOTICE: This routine does *not* descend into subqueries.
drh626a8792005-01-17 22:08:19 +0000835*/
danielk1977a58fdfb2005-02-08 07:50:40 +0000836static int walkExprList(ExprList *, int (*)(void *, Expr*), void *);
drh626a8792005-01-17 22:08:19 +0000837static int walkExprTree(Expr *pExpr, int (*xFunc)(void*,Expr*), void *pArg){
drh626a8792005-01-17 22:08:19 +0000838 int rc;
839 if( pExpr==0 ) return 0;
840 rc = (*xFunc)(pArg, pExpr);
841 if( rc==0 ){
842 if( walkExprTree(pExpr->pLeft, xFunc, pArg) ) return 1;
843 if( walkExprTree(pExpr->pRight, xFunc, pArg) ) return 1;
danielk1977a58fdfb2005-02-08 07:50:40 +0000844 if( walkExprList(pExpr->pList, xFunc, pArg) ) return 1;
drh626a8792005-01-17 22:08:19 +0000845 }
846 return rc>1;
847}
848
849/*
danielk1977a58fdfb2005-02-08 07:50:40 +0000850** Call walkExprTree() for every expression in list p.
851*/
852static int walkExprList(ExprList *p, int (*xFunc)(void *, Expr*), void *pArg){
853 int i;
854 struct ExprList_item *pItem;
855 if( !p ) return 0;
856 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
857 if( walkExprTree(pItem->pExpr, xFunc, pArg) ) return 1;
858 }
859 return 0;
860}
861
862/*
863** Call walkExprTree() for every expression in Select p, not including
864** expressions that are part of sub-selects in any FROM clause or the LIMIT
865** or OFFSET expressions..
866*/
867static int walkSelectExpr(Select *p, int (*xFunc)(void *, Expr*), void *pArg){
868 walkExprList(p->pEList, xFunc, pArg);
869 walkExprTree(p->pWhere, xFunc, pArg);
870 walkExprList(p->pGroupBy, xFunc, pArg);
871 walkExprTree(p->pHaving, xFunc, pArg);
872 walkExprList(p->pOrderBy, xFunc, pArg);
danielk197715d79822007-05-15 07:00:34 +0000873 if( p->pPrior ){
874 walkSelectExpr(p->pPrior, xFunc, pArg);
875 }
danielk1977a58fdfb2005-02-08 07:50:40 +0000876 return 0;
877}
878
879
880/*
drh626a8792005-01-17 22:08:19 +0000881** This routine is designed as an xFunc for walkExprTree().
882**
883** pArg is really a pointer to an integer. If we can tell by looking
drh73b211a2005-01-18 04:00:42 +0000884** at pExpr that the expression that contains pExpr is not a constant
885** expression, then set *pArg to 0 and return 2 to abandon the tree walk.
886** If pExpr does does not disqualify the expression from being a constant
887** then do nothing.
888**
889** After walking the whole tree, if no nodes are found that disqualify
890** the expression as constant, then we assume the whole expression
891** is constant. See sqlite3ExprIsConstant() for additional information.
drh626a8792005-01-17 22:08:19 +0000892*/
893static int exprNodeIsConstant(void *pArg, Expr *pExpr){
drh0a168372007-06-08 00:20:47 +0000894 int *pN = (int*)pArg;
895
896 /* If *pArg is 3 then any term of the expression that comes from
897 ** the ON or USING clauses of a join disqualifies the expression
898 ** from being considered constant. */
899 if( (*pN)==3 && ExprHasAnyProperty(pExpr, EP_FromJoin) ){
900 *pN = 0;
901 return 2;
902 }
903
drh626a8792005-01-17 22:08:19 +0000904 switch( pExpr->op ){
drheb55bd22005-06-30 17:04:21 +0000905 /* Consider functions to be constant if all their arguments are constant
906 ** and *pArg==2 */
907 case TK_FUNCTION:
drh0a168372007-06-08 00:20:47 +0000908 if( (*pN)==2 ) return 0;
drheb55bd22005-06-30 17:04:21 +0000909 /* Fall through */
drh626a8792005-01-17 22:08:19 +0000910 case TK_ID:
911 case TK_COLUMN:
912 case TK_DOT:
913 case TK_AGG_FUNCTION:
drh13449892005-09-07 21:22:45 +0000914 case TK_AGG_COLUMN:
drhfe2093d2005-01-20 22:48:47 +0000915#ifndef SQLITE_OMIT_SUBQUERY
916 case TK_SELECT:
917 case TK_EXISTS:
drhc5499be2008-04-01 15:06:33 +0000918 testcase( pExpr->op==TK_SELECT );
919 testcase( pExpr->op==TK_EXISTS );
drhfe2093d2005-01-20 22:48:47 +0000920#endif
drhc5499be2008-04-01 15:06:33 +0000921 testcase( pExpr->op==TK_ID );
922 testcase( pExpr->op==TK_COLUMN );
923 testcase( pExpr->op==TK_DOT );
924 testcase( pExpr->op==TK_AGG_FUNCTION );
925 testcase( pExpr->op==TK_AGG_COLUMN );
drh0a168372007-06-08 00:20:47 +0000926 *pN = 0;
drh626a8792005-01-17 22:08:19 +0000927 return 2;
drh87abf5c2005-08-25 12:45:04 +0000928 case TK_IN:
929 if( pExpr->pSelect ){
drh0a168372007-06-08 00:20:47 +0000930 *pN = 0;
drh87abf5c2005-08-25 12:45:04 +0000931 return 2;
932 }
drh626a8792005-01-17 22:08:19 +0000933 default:
934 return 0;
935 }
936}
937
938/*
drhfef52082000-06-06 01:50:43 +0000939** Walk an expression tree. Return 1 if the expression is constant
drheb55bd22005-06-30 17:04:21 +0000940** and 0 if it involves variables or function calls.
drh23989372002-05-21 13:43:04 +0000941**
942** For the purposes of this function, a double-quoted string (ex: "abc")
943** is considered a variable but a single-quoted string (ex: 'abc') is
944** a constant.
drhfef52082000-06-06 01:50:43 +0000945*/
danielk19774adee202004-05-08 08:23:19 +0000946int sqlite3ExprIsConstant(Expr *p){
drh626a8792005-01-17 22:08:19 +0000947 int isConst = 1;
948 walkExprTree(p, exprNodeIsConstant, &isConst);
949 return isConst;
drhfef52082000-06-06 01:50:43 +0000950}
951
952/*
drheb55bd22005-06-30 17:04:21 +0000953** Walk an expression tree. Return 1 if the expression is constant
drh0a168372007-06-08 00:20:47 +0000954** that does no originate from the ON or USING clauses of a join.
955** Return 0 if it involves variables or function calls or terms from
956** an ON or USING clause.
957*/
958int sqlite3ExprIsConstantNotJoin(Expr *p){
959 int isConst = 3;
960 walkExprTree(p, exprNodeIsConstant, &isConst);
961 return isConst!=0;
962}
963
964/*
965** Walk an expression tree. Return 1 if the expression is constant
drheb55bd22005-06-30 17:04:21 +0000966** or a function call with constant arguments. Return and 0 if there
967** are any variables.
968**
969** For the purposes of this function, a double-quoted string (ex: "abc")
970** is considered a variable but a single-quoted string (ex: 'abc') is
971** a constant.
972*/
973int sqlite3ExprIsConstantOrFunction(Expr *p){
974 int isConst = 2;
975 walkExprTree(p, exprNodeIsConstant, &isConst);
976 return isConst!=0;
977}
978
979/*
drh73b211a2005-01-18 04:00:42 +0000980** If the expression p codes a constant integer that is small enough
drh202b2df2004-01-06 01:13:46 +0000981** to fit in a 32-bit integer, return 1 and put the value of the integer
982** in *pValue. If the expression is not an integer or if it is too big
983** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
drhe4de1fe2002-06-02 16:09:01 +0000984*/
danielk19774adee202004-05-08 08:23:19 +0000985int sqlite3ExprIsInteger(Expr *p, int *pValue){
drhe4de1fe2002-06-02 16:09:01 +0000986 switch( p->op ){
987 case TK_INTEGER: {
drh2646da72005-12-09 20:02:05 +0000988 if( sqlite3GetInt32((char*)p->token.z, pValue) ){
drh202b2df2004-01-06 01:13:46 +0000989 return 1;
990 }
991 break;
drhe4de1fe2002-06-02 16:09:01 +0000992 }
drh4b59ab52002-08-24 18:24:51 +0000993 case TK_UPLUS: {
danielk19774adee202004-05-08 08:23:19 +0000994 return sqlite3ExprIsInteger(p->pLeft, pValue);
drh4b59ab52002-08-24 18:24:51 +0000995 }
drhe4de1fe2002-06-02 16:09:01 +0000996 case TK_UMINUS: {
997 int v;
danielk19774adee202004-05-08 08:23:19 +0000998 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
drhe4de1fe2002-06-02 16:09:01 +0000999 *pValue = -v;
1000 return 1;
1001 }
1002 break;
1003 }
1004 default: break;
1005 }
1006 return 0;
1007}
1008
1009/*
drhc4a3c772001-04-04 11:48:57 +00001010** Return TRUE if the given string is a row-id column name.
1011*/
danielk19774adee202004-05-08 08:23:19 +00001012int sqlite3IsRowid(const char *z){
1013 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
1014 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
1015 if( sqlite3StrICmp(z, "OID")==0 ) return 1;
drhc4a3c772001-04-04 11:48:57 +00001016 return 0;
1017}
1018
1019/*
drh8141f612004-01-25 22:44:58 +00001020** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
1021** that name in the set of source tables in pSrcList and make the pExpr
1022** expression node refer back to that source column. The following changes
1023** are made to pExpr:
1024**
1025** pExpr->iDb Set the index in db->aDb[] of the database holding
1026** the table.
1027** pExpr->iTable Set to the cursor number for the table obtained
1028** from pSrcList.
1029** pExpr->iColumn Set to the column number within the table.
drh8141f612004-01-25 22:44:58 +00001030** pExpr->op Set to TK_COLUMN.
1031** pExpr->pLeft Any expression this points to is deleted
1032** pExpr->pRight Any expression this points to is deleted.
1033**
1034** The pDbToken is the name of the database (the "X"). This value may be
1035** NULL meaning that name is of the form Y.Z or Z. Any available database
1036** can be used. The pTableToken is the name of the table (the "Y"). This
1037** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it
1038** means that the form of the name is Z and that columns from any table
1039** can be used.
1040**
1041** If the name cannot be resolved unambiguously, leave an error message
1042** in pParse and return non-zero. Return zero on success.
1043*/
1044static int lookupName(
drhffe07b22005-11-03 00:41:17 +00001045 Parse *pParse, /* The parsing context */
drh8141f612004-01-25 22:44:58 +00001046 Token *pDbToken, /* Name of the database containing table, or NULL */
1047 Token *pTableToken, /* Name of table containing column, or NULL */
1048 Token *pColumnToken, /* Name of the column. */
drh626a8792005-01-17 22:08:19 +00001049 NameContext *pNC, /* The name context used to resolve the name */
drh8141f612004-01-25 22:44:58 +00001050 Expr *pExpr /* Make this EXPR node point to the selected column */
1051){
1052 char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */
1053 char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */
1054 char *zCol = 0; /* Name of the column. The "Z" */
1055 int i, j; /* Loop counters */
1056 int cnt = 0; /* Number of matching column names */
1057 int cntTab = 0; /* Number of matching table names */
drh626a8792005-01-17 22:08:19 +00001058 sqlite3 *db = pParse->db; /* The database */
drh51669862004-12-18 18:40:26 +00001059 struct SrcList_item *pItem; /* Use for looping over pSrcList items */
1060 struct SrcList_item *pMatch = 0; /* The matching pSrcList item */
drh73b211a2005-01-18 04:00:42 +00001061 NameContext *pTopNC = pNC; /* First namecontext in the list */
drh728b5772007-09-18 15:55:07 +00001062 Schema *pSchema = 0; /* Schema of the expression */
drh8141f612004-01-25 22:44:58 +00001063
1064 assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
drh17435752007-08-16 04:30:38 +00001065 zDb = sqlite3NameFromToken(db, pDbToken);
1066 zTab = sqlite3NameFromToken(db, pTableToken);
1067 zCol = sqlite3NameFromToken(db, pColumnToken);
1068 if( db->mallocFailed ){
danielk1977d5d56522005-03-16 12:15:20 +00001069 goto lookupname_end;
drh8141f612004-01-25 22:44:58 +00001070 }
drh8141f612004-01-25 22:44:58 +00001071
1072 pExpr->iTable = -1;
drh626a8792005-01-17 22:08:19 +00001073 while( pNC && cnt==0 ){
drhffe07b22005-11-03 00:41:17 +00001074 ExprList *pEList;
drh626a8792005-01-17 22:08:19 +00001075 SrcList *pSrcList = pNC->pSrcList;
drh8141f612004-01-25 22:44:58 +00001076
danielk1977b3bce662005-01-29 08:32:43 +00001077 if( pSrcList ){
1078 for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
drh43617e92006-03-06 20:55:46 +00001079 Table *pTab;
1080 int iDb;
danielk1977b3bce662005-01-29 08:32:43 +00001081 Column *pCol;
1082
drh43617e92006-03-06 20:55:46 +00001083 pTab = pItem->pTab;
1084 assert( pTab!=0 );
1085 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
danielk1977b3bce662005-01-29 08:32:43 +00001086 assert( pTab->nCol>0 );
1087 if( zTab ){
1088 if( pItem->zAlias ){
1089 char *zTabName = pItem->zAlias;
1090 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
1091 }else{
1092 char *zTabName = pTab->zName;
1093 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
danielk1977da184232006-01-05 11:34:32 +00001094 if( zDb!=0 && sqlite3StrICmp(db->aDb[iDb].zName, zDb)!=0 ){
danielk1977b3bce662005-01-29 08:32:43 +00001095 continue;
1096 }
drh626a8792005-01-17 22:08:19 +00001097 }
drh8141f612004-01-25 22:44:58 +00001098 }
danielk1977b3bce662005-01-29 08:32:43 +00001099 if( 0==(cntTab++) ){
drh626a8792005-01-17 22:08:19 +00001100 pExpr->iTable = pItem->iCursor;
drh728b5772007-09-18 15:55:07 +00001101 pSchema = pTab->pSchema;
danielk1977b3bce662005-01-29 08:32:43 +00001102 pMatch = pItem;
1103 }
1104 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
1105 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
danielk1977b3bf5562006-01-10 17:58:23 +00001106 const char *zColl = pTab->aCol[j].zColl;
drh873fac02005-06-06 17:11:46 +00001107 IdList *pUsing;
danielk1977b3bce662005-01-29 08:32:43 +00001108 cnt++;
1109 pExpr->iTable = pItem->iCursor;
1110 pMatch = pItem;
drh728b5772007-09-18 15:55:07 +00001111 pSchema = pTab->pSchema;
danielk1977b3bce662005-01-29 08:32:43 +00001112 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
1113 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
1114 pExpr->affinity = pTab->aCol[j].affinity;
drh8b4c40d2007-02-01 23:02:45 +00001115 if( (pExpr->flags & EP_ExpCollate)==0 ){
1116 pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0);
1117 }
drh61dfc312006-12-16 16:25:15 +00001118 if( i<pSrcList->nSrc-1 ){
1119 if( pItem[1].jointype & JT_NATURAL ){
1120 /* If this match occurred in the left table of a natural join,
1121 ** then skip the right table to avoid a duplicate match */
1122 pItem++;
1123 i++;
1124 }else if( (pUsing = pItem[1].pUsing)!=0 ){
1125 /* If this match occurs on a column that is in the USING clause
1126 ** of a join, skip the search of the right table of the join
1127 ** to avoid a duplicate match there. */
1128 int k;
1129 for(k=0; k<pUsing->nId; k++){
1130 if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ){
1131 pItem++;
1132 i++;
1133 break;
1134 }
drh873fac02005-06-06 17:11:46 +00001135 }
1136 }
1137 }
danielk1977b3bce662005-01-29 08:32:43 +00001138 break;
1139 }
drh8141f612004-01-25 22:44:58 +00001140 }
1141 }
1142 }
drh626a8792005-01-17 22:08:19 +00001143
1144#ifndef SQLITE_OMIT_TRIGGER
1145 /* If we have not already resolved the name, then maybe
1146 ** it is a new.* or old.* trigger argument reference
1147 */
1148 if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
1149 TriggerStack *pTriggerStack = pParse->trigStack;
1150 Table *pTab = 0;
danielk19778f2c54e2008-01-01 19:02:09 +00001151 u32 *piColMask;
drh626a8792005-01-17 22:08:19 +00001152 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){
1153 pExpr->iTable = pTriggerStack->newIdx;
1154 assert( pTriggerStack->pTab );
1155 pTab = pTriggerStack->pTab;
danielk19778f2c54e2008-01-01 19:02:09 +00001156 piColMask = &(pTriggerStack->newColMask);
drh626a8792005-01-17 22:08:19 +00001157 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab)==0 ){
1158 pExpr->iTable = pTriggerStack->oldIdx;
1159 assert( pTriggerStack->pTab );
1160 pTab = pTriggerStack->pTab;
danielk19778f2c54e2008-01-01 19:02:09 +00001161 piColMask = &(pTriggerStack->oldColMask);
drh626a8792005-01-17 22:08:19 +00001162 }
1163
1164 if( pTab ){
danielk1977f0113002006-01-24 12:09:17 +00001165 int iCol;
drh626a8792005-01-17 22:08:19 +00001166 Column *pCol = pTab->aCol;
1167
drh728b5772007-09-18 15:55:07 +00001168 pSchema = pTab->pSchema;
drh626a8792005-01-17 22:08:19 +00001169 cntTab++;
danielk1977f0113002006-01-24 12:09:17 +00001170 for(iCol=0; iCol < pTab->nCol; iCol++, pCol++) {
drh626a8792005-01-17 22:08:19 +00001171 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
danielk1977f0113002006-01-24 12:09:17 +00001172 const char *zColl = pTab->aCol[iCol].zColl;
drh626a8792005-01-17 22:08:19 +00001173 cnt++;
danielk1977f0113002006-01-24 12:09:17 +00001174 pExpr->iColumn = iCol==pTab->iPKey ? -1 : iCol;
1175 pExpr->affinity = pTab->aCol[iCol].affinity;
drh8b4c40d2007-02-01 23:02:45 +00001176 if( (pExpr->flags & EP_ExpCollate)==0 ){
1177 pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0);
1178 }
danielk1977aee18ef2005-03-09 12:26:50 +00001179 pExpr->pTab = pTab;
danielk19778f2c54e2008-01-01 19:02:09 +00001180 if( iCol>=0 ){
drhc5499be2008-04-01 15:06:33 +00001181 testcase( iCol==31 );
1182 testcase( iCol==32 );
danielk19778f2c54e2008-01-01 19:02:09 +00001183 *piColMask |= ((u32)1<<iCol) | (iCol>=32?0xffffffff:0);
1184 }
drh626a8792005-01-17 22:08:19 +00001185 break;
1186 }
1187 }
1188 }
1189 }
drhb7f91642004-10-31 02:22:47 +00001190#endif /* !defined(SQLITE_OMIT_TRIGGER) */
drh8141f612004-01-25 22:44:58 +00001191
drh626a8792005-01-17 22:08:19 +00001192 /*
1193 ** Perhaps the name is a reference to the ROWID
1194 */
1195 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
1196 cnt = 1;
1197 pExpr->iColumn = -1;
drh8a512562005-11-14 22:29:05 +00001198 pExpr->affinity = SQLITE_AFF_INTEGER;
drh626a8792005-01-17 22:08:19 +00001199 }
drh8141f612004-01-25 22:44:58 +00001200
drh626a8792005-01-17 22:08:19 +00001201 /*
1202 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
1203 ** might refer to an result-set alias. This happens, for example, when
1204 ** we are resolving names in the WHERE clause of the following command:
1205 **
1206 ** SELECT a+b AS x FROM table WHERE x<10;
1207 **
1208 ** In cases like this, replace pExpr with a copy of the expression that
1209 ** forms the result set entry ("a+b" in the example) and return immediately.
1210 ** Note that the expression in the result set should have already been
1211 ** resolved by the time the WHERE clause is resolved.
1212 */
drhffe07b22005-11-03 00:41:17 +00001213 if( cnt==0 && (pEList = pNC->pEList)!=0 && zTab==0 ){
drh626a8792005-01-17 22:08:19 +00001214 for(j=0; j<pEList->nExpr; j++){
1215 char *zAs = pEList->a[j].zName;
1216 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
drh36379e92007-07-23 22:51:15 +00001217 Expr *pDup, *pOrig;
drh626a8792005-01-17 22:08:19 +00001218 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
drh4f07e5f2007-05-14 11:34:46 +00001219 assert( pExpr->pList==0 );
1220 assert( pExpr->pSelect==0 );
drh36379e92007-07-23 22:51:15 +00001221 pOrig = pEList->a[j].pExpr;
1222 if( !pNC->allowAgg && ExprHasProperty(pOrig, EP_Agg) ){
1223 sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs);
drh17435752007-08-16 04:30:38 +00001224 sqlite3_free(zCol);
drh36379e92007-07-23 22:51:15 +00001225 return 2;
1226 }
danielk19771e536952007-08-16 10:09:01 +00001227 pDup = sqlite3ExprDup(db, pOrig);
drh4f07e5f2007-05-14 11:34:46 +00001228 if( pExpr->flags & EP_ExpCollate ){
1229 pDup->pColl = pExpr->pColl;
1230 pDup->flags |= EP_ExpCollate;
1231 }
drh17435752007-08-16 04:30:38 +00001232 if( pExpr->span.dyn ) sqlite3_free((char*)pExpr->span.z);
1233 if( pExpr->token.dyn ) sqlite3_free((char*)pExpr->token.z);
drh4f07e5f2007-05-14 11:34:46 +00001234 memcpy(pExpr, pDup, sizeof(*pExpr));
drh17435752007-08-16 04:30:38 +00001235 sqlite3_free(pDup);
drh15ccce12005-05-23 15:06:39 +00001236 cnt = 1;
danielk1977c9cf6e32007-06-25 16:29:33 +00001237 pMatch = 0;
drh626a8792005-01-17 22:08:19 +00001238 assert( zTab==0 && zDb==0 );
drh15ccce12005-05-23 15:06:39 +00001239 goto lookupname_end_2;
drh626a8792005-01-17 22:08:19 +00001240 }
1241 }
1242 }
1243
1244 /* Advance to the next name context. The loop will exit when either
1245 ** we have a match (cnt>0) or when we run out of name contexts.
1246 */
1247 if( cnt==0 ){
1248 pNC = pNC->pNext;
1249 }
drh8141f612004-01-25 22:44:58 +00001250 }
1251
1252 /*
1253 ** If X and Y are NULL (in other words if only the column name Z is
1254 ** supplied) and the value of Z is enclosed in double-quotes, then
1255 ** Z is a string literal if it doesn't match any column names. In that
1256 ** case, we need to return right away and not make any changes to
1257 ** pExpr.
drh15ccce12005-05-23 15:06:39 +00001258 **
1259 ** Because no reference was made to outer contexts, the pNC->nRef
1260 ** fields are not changed in any context.
drh8141f612004-01-25 22:44:58 +00001261 */
1262 if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
drh17435752007-08-16 04:30:38 +00001263 sqlite3_free(zCol);
drh8141f612004-01-25 22:44:58 +00001264 return 0;
1265 }
1266
1267 /*
1268 ** cnt==0 means there was not match. cnt>1 means there were two or
1269 ** more matches. Either way, we have an error.
1270 */
1271 if( cnt!=1 ){
drhde4fcfd2008-01-19 23:50:26 +00001272 const char *zErr;
1273 zErr = cnt==0 ? "no such column" : "ambiguous column name";
drh8141f612004-01-25 22:44:58 +00001274 if( zDb ){
drhde4fcfd2008-01-19 23:50:26 +00001275 sqlite3ErrorMsg(pParse, "%s: %s.%s.%s", zErr, zDb, zTab, zCol);
drh8141f612004-01-25 22:44:58 +00001276 }else if( zTab ){
drhde4fcfd2008-01-19 23:50:26 +00001277 sqlite3ErrorMsg(pParse, "%s: %s.%s", zErr, zTab, zCol);
drh8141f612004-01-25 22:44:58 +00001278 }else{
drhde4fcfd2008-01-19 23:50:26 +00001279 sqlite3ErrorMsg(pParse, "%s: %s", zErr, zCol);
drh8141f612004-01-25 22:44:58 +00001280 }
drhde4fcfd2008-01-19 23:50:26 +00001281 pTopNC->nErr++;
drh8141f612004-01-25 22:44:58 +00001282 }
1283
drh51669862004-12-18 18:40:26 +00001284 /* If a column from a table in pSrcList is referenced, then record
1285 ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes
1286 ** bit 0 to be set. Column 1 sets bit 1. And so forth. If the
1287 ** column number is greater than the number of bits in the bitmask
1288 ** then set the high-order bit of the bitmask.
1289 */
1290 if( pExpr->iColumn>=0 && pMatch!=0 ){
1291 int n = pExpr->iColumn;
drhc5499be2008-04-01 15:06:33 +00001292 testcase( n==sizeof(Bitmask)*8-1 );
drh51669862004-12-18 18:40:26 +00001293 if( n>=sizeof(Bitmask)*8 ){
1294 n = sizeof(Bitmask)*8-1;
1295 }
1296 assert( pMatch->iCursor==pExpr->iTable );
drhca83ac52007-02-01 01:40:44 +00001297 pMatch->colUsed |= ((Bitmask)1)<<n;
drh51669862004-12-18 18:40:26 +00001298 }
1299
danielk1977d5d56522005-03-16 12:15:20 +00001300lookupname_end:
drh8141f612004-01-25 22:44:58 +00001301 /* Clean up and return
1302 */
drh17435752007-08-16 04:30:38 +00001303 sqlite3_free(zDb);
1304 sqlite3_free(zTab);
danielk19774adee202004-05-08 08:23:19 +00001305 sqlite3ExprDelete(pExpr->pLeft);
drh8141f612004-01-25 22:44:58 +00001306 pExpr->pLeft = 0;
danielk19774adee202004-05-08 08:23:19 +00001307 sqlite3ExprDelete(pExpr->pRight);
drh8141f612004-01-25 22:44:58 +00001308 pExpr->pRight = 0;
1309 pExpr->op = TK_COLUMN;
drh15ccce12005-05-23 15:06:39 +00001310lookupname_end_2:
drh17435752007-08-16 04:30:38 +00001311 sqlite3_free(zCol);
drh626a8792005-01-17 22:08:19 +00001312 if( cnt==1 ){
danielk1977b3bce662005-01-29 08:32:43 +00001313 assert( pNC!=0 );
drh728b5772007-09-18 15:55:07 +00001314 sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList);
danielk1977aee18ef2005-03-09 12:26:50 +00001315 if( pMatch && !pMatch->pSelect ){
1316 pExpr->pTab = pMatch->pTab;
1317 }
drh15ccce12005-05-23 15:06:39 +00001318 /* Increment the nRef value on all name contexts from TopNC up to
1319 ** the point where the name matched. */
1320 for(;;){
1321 assert( pTopNC!=0 );
1322 pTopNC->nRef++;
1323 if( pTopNC==pNC ) break;
1324 pTopNC = pTopNC->pNext;
1325 }
1326 return 0;
1327 } else {
1328 return 1;
drh626a8792005-01-17 22:08:19 +00001329 }
drh8141f612004-01-25 22:44:58 +00001330}
1331
1332/*
drh626a8792005-01-17 22:08:19 +00001333** This routine is designed as an xFunc for walkExprTree().
1334**
drh73b211a2005-01-18 04:00:42 +00001335** Resolve symbolic names into TK_COLUMN operators for the current
drh626a8792005-01-17 22:08:19 +00001336** node in the expression tree. Return 0 to continue the search down
drh73b211a2005-01-18 04:00:42 +00001337** the tree or 2 to abort the tree walk.
1338**
1339** This routine also does error checking and name resolution for
1340** function names. The operator for aggregate functions is changed
1341** to TK_AGG_FUNCTION.
drh626a8792005-01-17 22:08:19 +00001342*/
1343static int nameResolverStep(void *pArg, Expr *pExpr){
1344 NameContext *pNC = (NameContext*)pArg;
drh626a8792005-01-17 22:08:19 +00001345 Parse *pParse;
drh626a8792005-01-17 22:08:19 +00001346
danielk1977b3bce662005-01-29 08:32:43 +00001347 if( pExpr==0 ) return 1;
drh626a8792005-01-17 22:08:19 +00001348 assert( pNC!=0 );
drh626a8792005-01-17 22:08:19 +00001349 pParse = pNC->pParse;
danielk1977b3bce662005-01-29 08:32:43 +00001350
drh626a8792005-01-17 22:08:19 +00001351 if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return 1;
1352 ExprSetProperty(pExpr, EP_Resolved);
1353#ifndef NDEBUG
danielk1977f0113002006-01-24 12:09:17 +00001354 if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
1355 SrcList *pSrcList = pNC->pSrcList;
danielk1977940fac92005-01-23 22:41:37 +00001356 int i;
danielk1977f0113002006-01-24 12:09:17 +00001357 for(i=0; i<pNC->pSrcList->nSrc; i++){
drh626a8792005-01-17 22:08:19 +00001358 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
1359 }
1360 }
1361#endif
1362 switch( pExpr->op ){
1363 /* Double-quoted strings (ex: "abc") are used as identifiers if
1364 ** possible. Otherwise they remain as strings. Single-quoted
1365 ** strings (ex: 'abc') are always string literals.
1366 */
1367 case TK_STRING: {
1368 if( pExpr->token.z[0]=='\'' ) break;
1369 /* Fall thru into the TK_ID case if this is a double-quoted string */
1370 }
1371 /* A lone identifier is the name of a column.
1372 */
1373 case TK_ID: {
drh626a8792005-01-17 22:08:19 +00001374 lookupName(pParse, 0, 0, &pExpr->token, pNC, pExpr);
1375 return 1;
1376 }
1377
1378 /* A table name and column name: ID.ID
1379 ** Or a database, table and column: ID.ID.ID
1380 */
1381 case TK_DOT: {
1382 Token *pColumn;
1383 Token *pTable;
1384 Token *pDb;
1385 Expr *pRight;
1386
danielk1977b3bce662005-01-29 08:32:43 +00001387 /* if( pSrcList==0 ) break; */
drh626a8792005-01-17 22:08:19 +00001388 pRight = pExpr->pRight;
1389 if( pRight->op==TK_ID ){
1390 pDb = 0;
1391 pTable = &pExpr->pLeft->token;
1392 pColumn = &pRight->token;
1393 }else{
1394 assert( pRight->op==TK_DOT );
1395 pDb = &pExpr->pLeft->token;
1396 pTable = &pRight->pLeft->token;
1397 pColumn = &pRight->pRight->token;
1398 }
1399 lookupName(pParse, pDb, pTable, pColumn, pNC, pExpr);
1400 return 1;
1401 }
1402
1403 /* Resolve function names
1404 */
drhb71090f2005-05-23 17:26:51 +00001405 case TK_CONST_FUNC:
drh626a8792005-01-17 22:08:19 +00001406 case TK_FUNCTION: {
1407 ExprList *pList = pExpr->pList; /* The argument list */
1408 int n = pList ? pList->nExpr : 0; /* Number of arguments */
1409 int no_such_func = 0; /* True if no such function exists */
1410 int wrong_num_args = 0; /* True if wrong number of arguments */
1411 int is_agg = 0; /* True if is an aggregate function */
1412 int i;
drh5169bbc2006-08-24 14:59:45 +00001413 int auth; /* Authorization to use the function */
drh626a8792005-01-17 22:08:19 +00001414 int nId; /* Number of characters in function name */
1415 const char *zId; /* The function name. */
drh73b211a2005-01-18 04:00:42 +00001416 FuncDef *pDef; /* Information about the function */
danielk197714db2662006-01-09 16:12:04 +00001417 int enc = ENC(pParse->db); /* The database encoding */
drh626a8792005-01-17 22:08:19 +00001418
drh2646da72005-12-09 20:02:05 +00001419 zId = (char*)pExpr->token.z;
drhb71090f2005-05-23 17:26:51 +00001420 nId = pExpr->token.n;
drh626a8792005-01-17 22:08:19 +00001421 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
1422 if( pDef==0 ){
1423 pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
1424 if( pDef==0 ){
1425 no_such_func = 1;
1426 }else{
1427 wrong_num_args = 1;
1428 }
1429 }else{
1430 is_agg = pDef->xFunc==0;
1431 }
drh2fca7fe2006-11-23 11:59:13 +00001432#ifndef SQLITE_OMIT_AUTHORIZATION
drh5169bbc2006-08-24 14:59:45 +00001433 if( pDef ){
1434 auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0);
1435 if( auth!=SQLITE_OK ){
1436 if( auth==SQLITE_DENY ){
1437 sqlite3ErrorMsg(pParse, "not authorized to use function: %s",
1438 pDef->zName);
1439 pNC->nErr++;
1440 }
1441 pExpr->op = TK_NULL;
1442 return 1;
1443 }
1444 }
drhb8b14212006-08-24 15:18:25 +00001445#endif
drh626a8792005-01-17 22:08:19 +00001446 if( is_agg && !pNC->allowAgg ){
1447 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
1448 pNC->nErr++;
1449 is_agg = 0;
1450 }else if( no_such_func ){
1451 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
1452 pNC->nErr++;
1453 }else if( wrong_num_args ){
1454 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
1455 nId, zId);
1456 pNC->nErr++;
1457 }
1458 if( is_agg ){
1459 pExpr->op = TK_AGG_FUNCTION;
1460 pNC->hasAgg = 1;
1461 }
drh73b211a2005-01-18 04:00:42 +00001462 if( is_agg ) pNC->allowAgg = 0;
drh626a8792005-01-17 22:08:19 +00001463 for(i=0; pNC->nErr==0 && i<n; i++){
drh73b211a2005-01-18 04:00:42 +00001464 walkExprTree(pList->a[i].pExpr, nameResolverStep, pNC);
drh626a8792005-01-17 22:08:19 +00001465 }
drh73b211a2005-01-18 04:00:42 +00001466 if( is_agg ) pNC->allowAgg = 1;
drh626a8792005-01-17 22:08:19 +00001467 /* FIX ME: Compute pExpr->affinity based on the expected return
1468 ** type of the function
1469 */
1470 return is_agg;
1471 }
danielk1977b3bce662005-01-29 08:32:43 +00001472#ifndef SQLITE_OMIT_SUBQUERY
1473 case TK_SELECT:
1474 case TK_EXISTS:
1475#endif
1476 case TK_IN: {
1477 if( pExpr->pSelect ){
drh8a9f38f2005-11-05 15:07:55 +00001478 int nRef = pNC->nRef;
drh06f65412005-11-03 02:03:13 +00001479#ifndef SQLITE_OMIT_CHECK
1480 if( pNC->isCheck ){
1481 sqlite3ErrorMsg(pParse,"subqueries prohibited in CHECK constraints");
1482 }
1483#endif
danielk1977b3bce662005-01-29 08:32:43 +00001484 sqlite3SelectResolve(pParse, pExpr->pSelect, pNC);
1485 assert( pNC->nRef>=nRef );
1486 if( nRef!=pNC->nRef ){
1487 ExprSetProperty(pExpr, EP_VarSelect);
1488 }
1489 }
drh4284fb02005-11-03 12:33:28 +00001490 break;
danielk1977b3bce662005-01-29 08:32:43 +00001491 }
drh4284fb02005-11-03 12:33:28 +00001492#ifndef SQLITE_OMIT_CHECK
1493 case TK_VARIABLE: {
1494 if( pNC->isCheck ){
1495 sqlite3ErrorMsg(pParse,"parameters prohibited in CHECK constraints");
1496 }
1497 break;
1498 }
1499#endif
drh626a8792005-01-17 22:08:19 +00001500 }
1501 return 0;
1502}
1503
1504/*
drhcce7d172000-05-31 15:34:51 +00001505** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +00001506** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +00001507** index to the table in the table list and a column offset. The
1508** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
1509** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +00001510** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +00001511** VDBE cursor number for a cursor that is pointing into the referenced
1512** table. The Expr.iColumn value is changed to the index of the column
1513** of the referenced table. The Expr.iColumn value for the special
1514** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
1515** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +00001516**
drh626a8792005-01-17 22:08:19 +00001517** Also resolve function names and check the functions for proper
1518** usage. Make sure all function names are recognized and all functions
1519** have the correct number of arguments. Leave an error message
1520** in pParse->zErrMsg if anything is amiss. Return the number of errors.
1521**
drh73b211a2005-01-18 04:00:42 +00001522** If the expression contains aggregate functions then set the EP_Agg
1523** property on the expression.
drh626a8792005-01-17 22:08:19 +00001524*/
danielk1977fc976062007-05-10 10:46:56 +00001525int sqlite3ExprResolveNames(
danielk1977b3bce662005-01-29 08:32:43 +00001526 NameContext *pNC, /* Namespace to resolve expressions in. */
1527 Expr *pExpr /* The expression to be analyzed. */
drh626a8792005-01-17 22:08:19 +00001528){
drh13449892005-09-07 21:22:45 +00001529 int savedHasAgg;
drhbb4957f2008-03-20 14:03:29 +00001530
drh73b211a2005-01-18 04:00:42 +00001531 if( pExpr==0 ) return 0;
drhbb4957f2008-03-20 14:03:29 +00001532#if SQLITE_MAX_EXPR_DEPTH>0
1533 {
1534 int mxDepth = pNC->pParse->db->aLimit[SQLITE_LIMIT_EXPR_DEPTH];
1535 if( (pExpr->nHeight+pNC->pParse->nHeight)>mxDepth ){
1536 sqlite3ErrorMsg(pNC->pParse,
1537 "Expression tree is too large (maximum depth %d)", mxDepth
1538 );
1539 return 1;
1540 }
1541 pNC->pParse->nHeight += pExpr->nHeight;
danielk1977fc976062007-05-10 10:46:56 +00001542 }
danielk1977fc976062007-05-10 10:46:56 +00001543#endif
drh13449892005-09-07 21:22:45 +00001544 savedHasAgg = pNC->hasAgg;
1545 pNC->hasAgg = 0;
danielk1977b3bce662005-01-29 08:32:43 +00001546 walkExprTree(pExpr, nameResolverStep, pNC);
drhbb4957f2008-03-20 14:03:29 +00001547#if SQLITE_MAX_EXPR_DEPTH>0
danielk1977fc976062007-05-10 10:46:56 +00001548 pNC->pParse->nHeight -= pExpr->nHeight;
1549#endif
danielk1977b3bce662005-01-29 08:32:43 +00001550 if( pNC->nErr>0 ){
drh73b211a2005-01-18 04:00:42 +00001551 ExprSetProperty(pExpr, EP_Error);
drh73b211a2005-01-18 04:00:42 +00001552 }
drh13449892005-09-07 21:22:45 +00001553 if( pNC->hasAgg ){
1554 ExprSetProperty(pExpr, EP_Agg);
1555 }else if( savedHasAgg ){
1556 pNC->hasAgg = 1;
1557 }
drh73b211a2005-01-18 04:00:42 +00001558 return ExprHasProperty(pExpr, EP_Error);
drh626a8792005-01-17 22:08:19 +00001559}
1560
drh1398ad32005-01-19 23:24:50 +00001561/*
1562** A pointer instance of this structure is used to pass information
1563** through walkExprTree into codeSubqueryStep().
1564*/
1565typedef struct QueryCoder QueryCoder;
1566struct QueryCoder {
1567 Parse *pParse; /* The parsing context */
1568 NameContext *pNC; /* Namespace of first enclosing query */
1569};
1570
danielk19779a96b662007-11-29 17:05:18 +00001571#ifdef SQLITE_TEST
1572 int sqlite3_enable_in_opt = 1;
1573#else
1574 #define sqlite3_enable_in_opt 1
1575#endif
1576
1577/*
1578** This function is used by the implementation of the IN (...) operator.
1579** It's job is to find or create a b-tree structure that may be used
1580** either to test for membership of the (...) set or to iterate through
drh85b623f2007-12-13 21:54:09 +00001581** its members, skipping duplicates.
danielk19779a96b662007-11-29 17:05:18 +00001582**
1583** The cursor opened on the structure (database table, database index
1584** or ephermal table) is stored in pX->iTable before this function returns.
1585** The returned value indicates the structure type, as follows:
1586**
1587** IN_INDEX_ROWID - The cursor was opened on a database table.
drh2d401ab2008-01-10 23:50:11 +00001588** IN_INDEX_INDEX - The cursor was opened on a database index.
danielk19779a96b662007-11-29 17:05:18 +00001589** IN_INDEX_EPH - The cursor was opened on a specially created and
1590** populated epheremal table.
1591**
1592** An existing structure may only be used if the SELECT is of the simple
1593** form:
1594**
1595** SELECT <column> FROM <table>
1596**
1597** If the mustBeUnique parameter is false, the structure will be used
1598** for fast set membership tests. In this case an epheremal table must
1599** be used unless <column> is an INTEGER PRIMARY KEY or an index can
drh85b623f2007-12-13 21:54:09 +00001600** be found with <column> as its left-most column.
danielk19779a96b662007-11-29 17:05:18 +00001601**
1602** If mustBeUnique is true, then the structure will be used to iterate
1603** through the set members, skipping any duplicates. In this case an
1604** epheremal table must be used unless the selected <column> is guaranteed
1605** to be unique - either because it is an INTEGER PRIMARY KEY or it
1606** is unique by virtue of a constraint or implicit index.
1607*/
danielk1977284f4ac2007-12-10 05:03:46 +00001608#ifndef SQLITE_OMIT_SUBQUERY
danielk19779a96b662007-11-29 17:05:18 +00001609int sqlite3FindInIndex(Parse *pParse, Expr *pX, int mustBeUnique){
1610 Select *p;
1611 int eType = 0;
1612 int iTab = pParse->nTab++;
1613
1614 /* The follwing if(...) expression is true if the SELECT is of the
1615 ** simple form:
1616 **
1617 ** SELECT <column> FROM <table>
1618 **
1619 ** If this is the case, it may be possible to use an existing table
1620 ** or index instead of generating an epheremal table.
1621 */
1622 if( sqlite3_enable_in_opt
drhc81945e2008-03-10 14:12:53 +00001623 && (p=pX->pSelect)!=0 && !p->pPrior
danielk19779a96b662007-11-29 17:05:18 +00001624 && !p->isDistinct && !p->isAgg && !p->pGroupBy
1625 && p->pSrc && p->pSrc->nSrc==1 && !p->pSrc->a[0].pSelect
danielk1977b2b95d42008-03-12 10:39:00 +00001626 && p->pSrc->a[0].pTab && !p->pSrc->a[0].pTab->pSelect
danielk19779a96b662007-11-29 17:05:18 +00001627 && p->pEList->nExpr==1 && p->pEList->a[0].pExpr->op==TK_COLUMN
1628 && !p->pLimit && !p->pOffset && !p->pWhere
1629 ){
1630 sqlite3 *db = pParse->db;
1631 Index *pIdx;
1632 Expr *pExpr = p->pEList->a[0].pExpr;
1633 int iCol = pExpr->iColumn;
1634 Vdbe *v = sqlite3GetVdbe(pParse);
1635
1636 /* This function is only called from two places. In both cases the vdbe
1637 ** has already been allocated. So assume sqlite3GetVdbe() is always
1638 ** successful here.
1639 */
1640 assert(v);
1641 if( iCol<0 ){
drh0a07c102008-01-03 18:03:08 +00001642 int iMem = ++pParse->nMem;
danielk19779a96b662007-11-29 17:05:18 +00001643 int iAddr;
1644 Table *pTab = p->pSrc->a[0].pTab;
1645 int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
1646 sqlite3VdbeUsesBtree(v, iDb);
1647
drh892d3172008-01-10 03:46:36 +00001648 iAddr = sqlite3VdbeAddOp1(v, OP_If, iMem);
drh4c583122008-01-04 22:01:03 +00001649 sqlite3VdbeAddOp2(v, OP_Integer, 1, iMem);
danielk19779a96b662007-11-29 17:05:18 +00001650
1651 sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
1652 eType = IN_INDEX_ROWID;
1653
1654 sqlite3VdbeJumpHere(v, iAddr);
1655 }else{
1656 /* The collation sequence used by the comparison. If an index is to
1657 ** be used in place of a temp-table, it must be ordered according
1658 ** to this collation sequence.
1659 */
1660 CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pExpr);
1661
1662 /* Check that the affinity that will be used to perform the
1663 ** comparison is the same as the affinity of the column. If
1664 ** it is not, it is not possible to use any index.
1665 */
1666 Table *pTab = p->pSrc->a[0].pTab;
1667 char aff = comparisonAffinity(pX);
1668 int affinity_ok = (pTab->aCol[iCol].affinity==aff||aff==SQLITE_AFF_NONE);
1669
1670 for(pIdx=pTab->pIndex; pIdx && eType==0 && affinity_ok; pIdx=pIdx->pNext){
1671 if( (pIdx->aiColumn[0]==iCol)
1672 && (pReq==sqlite3FindCollSeq(db, ENC(db), pIdx->azColl[0], -1, 0))
1673 && (!mustBeUnique || (pIdx->nColumn==1 && pIdx->onError!=OE_None))
1674 ){
1675 int iDb;
drh0a07c102008-01-03 18:03:08 +00001676 int iMem = ++pParse->nMem;
danielk19779a96b662007-11-29 17:05:18 +00001677 int iAddr;
1678 char *pKey;
1679
1680 pKey = (char *)sqlite3IndexKeyinfo(pParse, pIdx);
1681 iDb = sqlite3SchemaToIndex(db, pIdx->pSchema);
1682 sqlite3VdbeUsesBtree(v, iDb);
1683
drh892d3172008-01-10 03:46:36 +00001684 iAddr = sqlite3VdbeAddOp1(v, OP_If, iMem);
drh4c583122008-01-04 22:01:03 +00001685 sqlite3VdbeAddOp2(v, OP_Integer, 1, iMem);
danielk19779a96b662007-11-29 17:05:18 +00001686
danielk1977cd3e8f72008-03-25 09:47:35 +00001687 sqlite3VdbeAddOp2(v, OP_SetNumColumns, 0, pIdx->nColumn);
danielk1977207872a2008-01-03 07:54:23 +00001688 sqlite3VdbeAddOp4(v, OP_OpenRead, iTab, pIdx->tnum, iDb,
drh66a51672008-01-03 00:01:23 +00001689 pKey,P4_KEYINFO_HANDOFF);
danielk1977207872a2008-01-03 07:54:23 +00001690 VdbeComment((v, "%s", pIdx->zName));
danielk19779a96b662007-11-29 17:05:18 +00001691 eType = IN_INDEX_INDEX;
danielk19779a96b662007-11-29 17:05:18 +00001692
1693 sqlite3VdbeJumpHere(v, iAddr);
1694 }
1695 }
1696 }
1697 }
1698
1699 if( eType==0 ){
1700 sqlite3CodeSubselect(pParse, pX);
1701 eType = IN_INDEX_EPH;
1702 }else{
1703 pX->iTable = iTab;
1704 }
1705 return eType;
1706}
danielk1977284f4ac2007-12-10 05:03:46 +00001707#endif
drh626a8792005-01-17 22:08:19 +00001708
1709/*
drh9cbe6352005-11-29 03:13:21 +00001710** Generate code for scalar subqueries used as an expression
1711** and IN operators. Examples:
drh626a8792005-01-17 22:08:19 +00001712**
drh9cbe6352005-11-29 03:13:21 +00001713** (SELECT a FROM b) -- subquery
1714** EXISTS (SELECT a FROM b) -- EXISTS subquery
1715** x IN (4,5,11) -- IN operator with list on right-hand side
1716** x IN (SELECT a FROM b) -- IN operator with subquery on the right
drhfef52082000-06-06 01:50:43 +00001717**
drh9cbe6352005-11-29 03:13:21 +00001718** The pExpr parameter describes the expression that contains the IN
1719** operator or subquery.
drhcce7d172000-05-31 15:34:51 +00001720*/
drh51522cd2005-01-20 13:36:19 +00001721#ifndef SQLITE_OMIT_SUBQUERY
danielk1977b3bce662005-01-29 08:32:43 +00001722void sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){
drh57dbd7b2005-07-08 18:25:26 +00001723 int testAddr = 0; /* One-time test address */
danielk1977b3bce662005-01-29 08:32:43 +00001724 Vdbe *v = sqlite3GetVdbe(pParse);
1725 if( v==0 ) return;
1726
danielk1977fc976062007-05-10 10:46:56 +00001727
drh57dbd7b2005-07-08 18:25:26 +00001728 /* This code must be run in its entirety every time it is encountered
1729 ** if any of the following is true:
1730 **
1731 ** * The right-hand side is a correlated subquery
1732 ** * The right-hand side is an expression list containing variables
1733 ** * We are inside a trigger
1734 **
1735 ** If all of the above are false, then we can run this code just once
1736 ** save the results, and reuse the same result on subsequent invocations.
danielk1977b3bce662005-01-29 08:32:43 +00001737 */
1738 if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->trigStack ){
drh0a07c102008-01-03 18:03:08 +00001739 int mem = ++pParse->nMem;
drh892d3172008-01-10 03:46:36 +00001740 sqlite3VdbeAddOp1(v, OP_If, mem);
1741 testAddr = sqlite3VdbeAddOp2(v, OP_Integer, 1, mem);
drh17435752007-08-16 04:30:38 +00001742 assert( testAddr>0 || pParse->db->mallocFailed );
danielk1977b3bce662005-01-29 08:32:43 +00001743 }
1744
drhcce7d172000-05-31 15:34:51 +00001745 switch( pExpr->op ){
drhfef52082000-06-06 01:50:43 +00001746 case TK_IN: {
danielk1977e014a832004-05-17 10:48:57 +00001747 char affinity;
drhd3d39e92004-05-20 22:16:29 +00001748 KeyInfo keyInfo;
drhb9bb7c12006-06-11 23:41:55 +00001749 int addr; /* Address of OP_OpenEphemeral instruction */
drhd3d39e92004-05-20 22:16:29 +00001750
danielk1977bf3b7212004-05-18 10:06:24 +00001751 affinity = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +00001752
1753 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
drh57dbd7b2005-07-08 18:25:26 +00001754 ** expression it is handled the same way. A virtual table is
danielk1977e014a832004-05-17 10:48:57 +00001755 ** filled with single-field index keys representing the results
1756 ** from the SELECT or the <exprlist>.
1757 **
1758 ** If the 'x' expression is a column value, or the SELECT...
1759 ** statement returns a column value, then the affinity of that
1760 ** column is used to build the index keys. If both 'x' and the
1761 ** SELECT... statement are columns, then numeric affinity is used
1762 ** if either column has NUMERIC or INTEGER affinity. If neither
1763 ** 'x' nor the SELECT... statement are columns, then numeric affinity
1764 ** is used.
1765 */
1766 pExpr->iTable = pParse->nTab++;
danielk1977cd3e8f72008-03-25 09:47:35 +00001767 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pExpr->iTable, 1);
drhd3d39e92004-05-20 22:16:29 +00001768 memset(&keyInfo, 0, sizeof(keyInfo));
1769 keyInfo.nField = 1;
danielk1977e014a832004-05-17 10:48:57 +00001770
drhfef52082000-06-06 01:50:43 +00001771 if( pExpr->pSelect ){
1772 /* Case 1: expr IN (SELECT ...)
1773 **
danielk1977e014a832004-05-17 10:48:57 +00001774 ** Generate code to write the results of the select into the temporary
1775 ** table allocated and opened above.
drhfef52082000-06-06 01:50:43 +00001776 */
drh1013c932008-01-06 00:25:21 +00001777 SelectDest dest;
drhbe5c89a2004-07-26 00:31:09 +00001778 ExprList *pEList;
drh1013c932008-01-06 00:25:21 +00001779
1780 sqlite3SelectDestInit(&dest, SRT_Set, pExpr->iTable);
1781 dest.affinity = (int)affinity;
danielk1977e014a832004-05-17 10:48:57 +00001782 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
danielk19776c8c8ce2008-01-02 16:27:09 +00001783 if( sqlite3Select(pParse, pExpr->pSelect, &dest, 0, 0, 0, 0) ){
drh94ccde52007-04-13 16:06:32 +00001784 return;
1785 }
drhbe5c89a2004-07-26 00:31:09 +00001786 pEList = pExpr->pSelect->pEList;
1787 if( pEList && pEList->nExpr>0 ){
danielk1977bcbb04e2007-05-29 12:11:29 +00001788 keyInfo.aColl[0] = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft,
drhbe5c89a2004-07-26 00:31:09 +00001789 pEList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001790 }
drhfef52082000-06-06 01:50:43 +00001791 }else if( pExpr->pList ){
1792 /* Case 2: expr IN (exprlist)
1793 **
drhfd131da2007-08-07 17:13:03 +00001794 ** For each expression, build an index key from the evaluation and
danielk1977e014a832004-05-17 10:48:57 +00001795 ** store it in the temporary table. If <expr> is a column, then use
1796 ** that columns affinity when building index keys. If <expr> is not
1797 ** a column, use numeric affinity.
drhfef52082000-06-06 01:50:43 +00001798 */
danielk1977e014a832004-05-17 10:48:57 +00001799 int i;
drh57dbd7b2005-07-08 18:25:26 +00001800 ExprList *pList = pExpr->pList;
1801 struct ExprList_item *pItem;
drh2d401ab2008-01-10 23:50:11 +00001802 int r1, r2;
drh57dbd7b2005-07-08 18:25:26 +00001803
danielk1977e014a832004-05-17 10:48:57 +00001804 if( !affinity ){
drh8159a352006-05-23 23:22:29 +00001805 affinity = SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +00001806 }
danielk19770202b292004-06-09 09:55:16 +00001807 keyInfo.aColl[0] = pExpr->pLeft->pColl;
danielk1977e014a832004-05-17 10:48:57 +00001808
1809 /* Loop through each expression in <exprlist>. */
drh2d401ab2008-01-10 23:50:11 +00001810 r1 = sqlite3GetTempReg(pParse);
1811 r2 = sqlite3GetTempReg(pParse);
drh57dbd7b2005-07-08 18:25:26 +00001812 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
1813 Expr *pE2 = pItem->pExpr;
danielk1977e014a832004-05-17 10:48:57 +00001814
drh57dbd7b2005-07-08 18:25:26 +00001815 /* If the expression is not constant then we will need to
1816 ** disable the test that was generated above that makes sure
1817 ** this code only executes once. Because for a non-constant
1818 ** expression we need to rerun this code each time.
1819 */
drh892d3172008-01-10 03:46:36 +00001820 if( testAddr && !sqlite3ExprIsConstant(pE2) ){
1821 sqlite3VdbeChangeToNoop(v, testAddr-1, 2);
drh57dbd7b2005-07-08 18:25:26 +00001822 testAddr = 0;
drh4794b982000-06-06 13:54:14 +00001823 }
danielk1977e014a832004-05-17 10:48:57 +00001824
1825 /* Evaluate the expression and insert it into the temp table */
drhe55cbd72008-03-31 23:48:03 +00001826 pParse->disableColCache++;
drh2d401ab2008-01-10 23:50:11 +00001827 sqlite3ExprCode(pParse, pE2, r1);
drhc5499be2008-04-01 15:06:33 +00001828 assert( pParse->disableColCache>0 );
drhe55cbd72008-03-31 23:48:03 +00001829 pParse->disableColCache--;
drh1db639c2008-01-17 02:36:28 +00001830 sqlite3VdbeAddOp4(v, OP_MakeRecord, r1, 1, r2, &affinity, 1);
drhda250ea2008-04-01 05:07:14 +00001831 sqlite3ExprCacheAffinityChange(pParse, r1, 1);
drh2d401ab2008-01-10 23:50:11 +00001832 sqlite3VdbeAddOp2(v, OP_IdxInsert, pExpr->iTable, r2);
drhfef52082000-06-06 01:50:43 +00001833 }
drh2d401ab2008-01-10 23:50:11 +00001834 sqlite3ReleaseTempReg(pParse, r1);
1835 sqlite3ReleaseTempReg(pParse, r2);
drhfef52082000-06-06 01:50:43 +00001836 }
drh66a51672008-01-03 00:01:23 +00001837 sqlite3VdbeChangeP4(v, addr, (void *)&keyInfo, P4_KEYINFO);
danielk1977b3bce662005-01-29 08:32:43 +00001838 break;
drhfef52082000-06-06 01:50:43 +00001839 }
1840
drh51522cd2005-01-20 13:36:19 +00001841 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001842 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +00001843 /* This has to be a scalar SELECT. Generate code to put the
1844 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +00001845 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +00001846 */
drh2646da72005-12-09 20:02:05 +00001847 static const Token one = { (u8*)"1", 0, 1 };
drh51522cd2005-01-20 13:36:19 +00001848 Select *pSel;
danielk19776c8c8ce2008-01-02 16:27:09 +00001849 SelectDest dest;
drh1398ad32005-01-19 23:24:50 +00001850
drh51522cd2005-01-20 13:36:19 +00001851 pSel = pExpr->pSelect;
drh1013c932008-01-06 00:25:21 +00001852 sqlite3SelectDestInit(&dest, 0, ++pParse->nMem);
drh51522cd2005-01-20 13:36:19 +00001853 if( pExpr->op==TK_SELECT ){
danielk19776c8c8ce2008-01-02 16:27:09 +00001854 dest.eDest = SRT_Mem;
drh4c583122008-01-04 22:01:03 +00001855 sqlite3VdbeAddOp2(v, OP_Null, 0, dest.iParm);
drhd4e70eb2008-01-02 00:34:36 +00001856 VdbeComment((v, "Init subquery result"));
drh51522cd2005-01-20 13:36:19 +00001857 }else{
danielk19776c8c8ce2008-01-02 16:27:09 +00001858 dest.eDest = SRT_Exists;
drh4c583122008-01-04 22:01:03 +00001859 sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iParm);
drhd4e70eb2008-01-02 00:34:36 +00001860 VdbeComment((v, "Init EXISTS result"));
drh51522cd2005-01-20 13:36:19 +00001861 }
drhec7429a2005-10-06 16:53:14 +00001862 sqlite3ExprDelete(pSel->pLimit);
danielk1977a1644fd2007-08-29 12:31:25 +00001863 pSel->pLimit = sqlite3PExpr(pParse, TK_INTEGER, 0, 0, &one);
danielk19776c8c8ce2008-01-02 16:27:09 +00001864 if( sqlite3Select(pParse, pSel, &dest, 0, 0, 0, 0) ){
drh94ccde52007-04-13 16:06:32 +00001865 return;
1866 }
danielk19776c8c8ce2008-01-02 16:27:09 +00001867 pExpr->iColumn = dest.iParm;
danielk1977b3bce662005-01-29 08:32:43 +00001868 break;
drhcce7d172000-05-31 15:34:51 +00001869 }
1870 }
danielk1977b3bce662005-01-29 08:32:43 +00001871
drh57dbd7b2005-07-08 18:25:26 +00001872 if( testAddr ){
drh892d3172008-01-10 03:46:36 +00001873 sqlite3VdbeJumpHere(v, testAddr-1);
danielk1977b3bce662005-01-29 08:32:43 +00001874 }
danielk1977fc976062007-05-10 10:46:56 +00001875
danielk1977b3bce662005-01-29 08:32:43 +00001876 return;
drhcce7d172000-05-31 15:34:51 +00001877}
drh51522cd2005-01-20 13:36:19 +00001878#endif /* SQLITE_OMIT_SUBQUERY */
drhcce7d172000-05-31 15:34:51 +00001879
drhcce7d172000-05-31 15:34:51 +00001880/*
drh598f1342007-10-23 15:39:45 +00001881** Duplicate an 8-byte value
1882*/
1883static char *dup8bytes(Vdbe *v, const char *in){
1884 char *out = sqlite3DbMallocRaw(sqlite3VdbeDb(v), 8);
1885 if( out ){
1886 memcpy(out, in, 8);
1887 }
1888 return out;
1889}
1890
1891/*
1892** Generate an instruction that will put the floating point
drh9cbf3422008-01-17 16:22:13 +00001893** value described by z[0..n-1] into register iMem.
drh0cf19ed2007-10-23 18:55:48 +00001894**
1895** The z[] string will probably not be zero-terminated. But the
1896** z[n] character is guaranteed to be something that does not look
1897** like the continuation of the number.
drh598f1342007-10-23 15:39:45 +00001898*/
drh9de221d2008-01-05 06:51:30 +00001899static void codeReal(Vdbe *v, const char *z, int n, int negateFlag, int iMem){
drh598f1342007-10-23 15:39:45 +00001900 assert( z || v==0 || sqlite3VdbeDb(v)->mallocFailed );
1901 if( z ){
1902 double value;
1903 char *zV;
drh0cf19ed2007-10-23 18:55:48 +00001904 assert( !isdigit(z[n]) );
drh598f1342007-10-23 15:39:45 +00001905 sqlite3AtoF(z, &value);
1906 if( negateFlag ) value = -value;
1907 zV = dup8bytes(v, (char*)&value);
drh9de221d2008-01-05 06:51:30 +00001908 sqlite3VdbeAddOp4(v, OP_Real, 0, iMem, 0, zV, P4_REAL);
drh598f1342007-10-23 15:39:45 +00001909 }
1910}
1911
1912
1913/*
drhfec19aa2004-05-19 20:41:03 +00001914** Generate an instruction that will put the integer describe by
drh9cbf3422008-01-17 16:22:13 +00001915** text z[0..n-1] into register iMem.
drh0cf19ed2007-10-23 18:55:48 +00001916**
1917** The z[] string will probably not be zero-terminated. But the
1918** z[n] character is guaranteed to be something that does not look
1919** like the continuation of the number.
drhfec19aa2004-05-19 20:41:03 +00001920*/
drh9de221d2008-01-05 06:51:30 +00001921static void codeInteger(Vdbe *v, const char *z, int n, int negFlag, int iMem){
drhabb6fca2007-08-16 12:24:01 +00001922 assert( z || v==0 || sqlite3VdbeDb(v)->mallocFailed );
danielk1977c9cf9012007-05-30 10:36:47 +00001923 if( z ){
1924 int i;
drh0cf19ed2007-10-23 18:55:48 +00001925 assert( !isdigit(z[n]) );
danielk1977c9cf9012007-05-30 10:36:47 +00001926 if( sqlite3GetInt32(z, &i) ){
drh9de221d2008-01-05 06:51:30 +00001927 if( negFlag ) i = -i;
1928 sqlite3VdbeAddOp2(v, OP_Integer, i, iMem);
1929 }else if( sqlite3FitsIn64Bits(z, negFlag) ){
drh598f1342007-10-23 15:39:45 +00001930 i64 value;
1931 char *zV;
1932 sqlite3Atoi64(z, &value);
drh9de221d2008-01-05 06:51:30 +00001933 if( negFlag ) value = -value;
drh598f1342007-10-23 15:39:45 +00001934 zV = dup8bytes(v, (char*)&value);
drh9de221d2008-01-05 06:51:30 +00001935 sqlite3VdbeAddOp4(v, OP_Int64, 0, iMem, 0, zV, P4_INT64);
danielk1977c9cf9012007-05-30 10:36:47 +00001936 }else{
drh9de221d2008-01-05 06:51:30 +00001937 codeReal(v, z, n, negFlag, iMem);
danielk1977c9cf9012007-05-30 10:36:47 +00001938 }
drhfec19aa2004-05-19 20:41:03 +00001939 }
1940}
1941
drh945498f2007-02-24 11:52:52 +00001942
1943/*
1944** Generate code that will extract the iColumn-th column from
drhe55cbd72008-03-31 23:48:03 +00001945** table pTab and store the column value in a register. An effort
1946** is made to store the column value in register iReg, but this is
1947** not guaranteed. The location of the column value is returned.
1948**
1949** There must be an open cursor to pTab in iTable when this routine
1950** is called. If iColumn<0 then code is generated that extracts the rowid.
drhda250ea2008-04-01 05:07:14 +00001951**
1952** This routine might attempt to reuse the value of the column that
1953** has already been loaded into a register. The value will always
1954** be used if it has not undergone any affinity changes. But if
1955** an affinity change has occurred, then the cached value will only be
1956** used if allowAffChng is true.
drh945498f2007-02-24 11:52:52 +00001957*/
drhe55cbd72008-03-31 23:48:03 +00001958int sqlite3ExprCodeGetColumn(
1959 Parse *pParse, /* Parsing and code generating context */
drh2133d822008-01-03 18:44:59 +00001960 Table *pTab, /* Description of the table we are reading from */
1961 int iColumn, /* Index of the table column */
1962 int iTable, /* The cursor pointing to the table */
drhda250ea2008-04-01 05:07:14 +00001963 int iReg, /* Store results here */
1964 int allowAffChng /* True if prior affinity changes are OK */
drh2133d822008-01-03 18:44:59 +00001965){
drhe55cbd72008-03-31 23:48:03 +00001966 Vdbe *v = pParse->pVdbe;
1967 int i;
drhda250ea2008-04-01 05:07:14 +00001968 struct yColCache *p;
drhe55cbd72008-03-31 23:48:03 +00001969
drhda250ea2008-04-01 05:07:14 +00001970 for(i=0, p=pParse->aColCache; i<pParse->nColCache; i++, p++){
1971 if( p->iTable==iTable && p->iColumn==iColumn
1972 && (!p->affChange || allowAffChng) ){
drhe55cbd72008-03-31 23:48:03 +00001973#if 0
1974 sqlite3VdbeAddOp0(v, OP_Noop);
drhda250ea2008-04-01 05:07:14 +00001975 VdbeComment((v, "OPT: tab%d.col%d -> r%d", iTable, iColumn, p->iReg));
drhe55cbd72008-03-31 23:48:03 +00001976#endif
drhda250ea2008-04-01 05:07:14 +00001977 return p->iReg;
drhe55cbd72008-03-31 23:48:03 +00001978 }
1979 }
1980 assert( v!=0 );
drh945498f2007-02-24 11:52:52 +00001981 if( iColumn<0 ){
1982 int op = (pTab && IsVirtual(pTab)) ? OP_VRowid : OP_Rowid;
drh2133d822008-01-03 18:44:59 +00001983 sqlite3VdbeAddOp2(v, op, iTable, iReg);
drh945498f2007-02-24 11:52:52 +00001984 }else if( pTab==0 ){
drh2133d822008-01-03 18:44:59 +00001985 sqlite3VdbeAddOp3(v, OP_Column, iTable, iColumn, iReg);
drh945498f2007-02-24 11:52:52 +00001986 }else{
1987 int op = IsVirtual(pTab) ? OP_VColumn : OP_Column;
drh2133d822008-01-03 18:44:59 +00001988 sqlite3VdbeAddOp3(v, op, iTable, iColumn, iReg);
drh945498f2007-02-24 11:52:52 +00001989 sqlite3ColumnDefault(v, pTab, iColumn);
1990#ifndef SQLITE_OMIT_FLOATING_POINT
1991 if( pTab->aCol[iColumn].affinity==SQLITE_AFF_REAL ){
drh2133d822008-01-03 18:44:59 +00001992 sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg);
drh945498f2007-02-24 11:52:52 +00001993 }
1994#endif
1995 }
drhe55cbd72008-03-31 23:48:03 +00001996 if( pParse->disableColCache==0 ){
1997 i = pParse->iColCache;
drhda250ea2008-04-01 05:07:14 +00001998 p = &pParse->aColCache[i];
1999 p->iTable = iTable;
2000 p->iColumn = iColumn;
2001 p->iReg = iReg;
drhc5499be2008-04-01 15:06:33 +00002002 p->affChange = 0;
drhe55cbd72008-03-31 23:48:03 +00002003 i++;
drh2f7794c2008-04-01 03:27:39 +00002004 if( i>=ArraySize(pParse->aColCache) ) i = 0;
drhe55cbd72008-03-31 23:48:03 +00002005 if( i>pParse->nColCache ) pParse->nColCache = i;
drh2f7794c2008-04-01 03:27:39 +00002006 pParse->iColCache = i;
drhe55cbd72008-03-31 23:48:03 +00002007 }
2008 return iReg;
2009}
2010
2011/*
drhe55cbd72008-03-31 23:48:03 +00002012** Clear all column cache entries associated with the vdbe
2013** cursor with cursor number iTable.
2014*/
2015void sqlite3ExprClearColumnCache(Parse *pParse, int iTable){
2016 if( iTable<0 ){
2017 pParse->nColCache = 0;
2018 pParse->iColCache = 0;
2019 }else{
2020 int i;
2021 for(i=0; i<pParse->nColCache; i++){
2022 if( pParse->aColCache[i].iTable==iTable ){
drhc5499be2008-04-01 15:06:33 +00002023 testcase( i==pParse->nColCache-1 );
drhe55cbd72008-03-31 23:48:03 +00002024 pParse->aColCache[i] = pParse->aColCache[--pParse->nColCache];
drhda250ea2008-04-01 05:07:14 +00002025 pParse->iColCache = pParse->nColCache;
drhe55cbd72008-03-31 23:48:03 +00002026 }
2027 }
drhe55cbd72008-03-31 23:48:03 +00002028 }
2029}
2030
2031/*
drhda250ea2008-04-01 05:07:14 +00002032** Record the fact that an affinity change has occurred on iCount
2033** registers starting with iStart.
drhe55cbd72008-03-31 23:48:03 +00002034*/
drhda250ea2008-04-01 05:07:14 +00002035void sqlite3ExprCacheAffinityChange(Parse *pParse, int iStart, int iCount){
2036 int iEnd = iStart + iCount - 1;
drhe55cbd72008-03-31 23:48:03 +00002037 int i;
2038 for(i=0; i<pParse->nColCache; i++){
2039 int r = pParse->aColCache[i].iReg;
drhda250ea2008-04-01 05:07:14 +00002040 if( r>=iStart && r<=iEnd ){
2041 pParse->aColCache[i].affChange = 1;
drhe55cbd72008-03-31 23:48:03 +00002042 }
2043 }
drhe55cbd72008-03-31 23:48:03 +00002044}
2045
2046/*
2047** Generate code to moves content from one register to another.
2048** Keep the column cache up-to-date.
2049*/
2050void sqlite3ExprCodeMove(Parse *pParse, int iFrom, int iTo){
2051 int i;
2052 if( iFrom==iTo ) return;
2053 sqlite3VdbeAddOp2(pParse->pVdbe, OP_Move, iFrom, iTo);
2054 for(i=0; i<pParse->nColCache; i++){
2055 if( pParse->aColCache[i].iReg==iFrom ){
2056 pParse->aColCache[i].iReg = iTo;
2057 }
2058 }
drh945498f2007-02-24 11:52:52 +00002059}
2060
drhfec19aa2004-05-19 20:41:03 +00002061/*
drh652fbf52008-04-01 01:42:41 +00002062** Return true if any register in the range iFrom..iTo (inclusive)
2063** is used as part of the column cache.
2064*/
2065static int usedAsColumnCache(Parse *pParse, int iFrom, int iTo){
2066 int i;
2067 for(i=0; i<pParse->nColCache; i++){
2068 int r = pParse->aColCache[i].iReg;
2069 if( r>=iFrom && r<=iTo ) return 1;
2070 }
2071 return 0;
2072}
2073
2074/*
2075** Theres is a value in register iCurrent. We ultimately want
2076** the value to be in register iTarget. It might be that
2077** iCurrent and iTarget are the same register.
2078**
2079** We are going to modify the value, so we need to make sure it
2080** is not a cached register. If iCurrent is a cached register,
2081** then try to move the value over to iTarget. If iTarget is a
2082** cached register, then clear the corresponding cache line.
2083**
2084** Return the register that the value ends up in.
2085*/
2086int sqlite3ExprWritableRegister(Parse *pParse, int iCurrent, int iTarget){
drhda250ea2008-04-01 05:07:14 +00002087 int i;
drh652fbf52008-04-01 01:42:41 +00002088 assert( pParse->pVdbe!=0 );
2089 if( !usedAsColumnCache(pParse, iCurrent, iCurrent) ){
2090 return iCurrent;
2091 }
drh2f7794c2008-04-01 03:27:39 +00002092 if( iCurrent!=iTarget ){
2093 sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, iCurrent, iTarget);
2094 }
drhda250ea2008-04-01 05:07:14 +00002095 for(i=0; i<pParse->nColCache; i++){
2096 if( pParse->aColCache[i].iReg==iTarget ){
2097 pParse->aColCache[i] = pParse->aColCache[--pParse->nColCache];
2098 pParse->iColCache = pParse->nColCache;
2099 }
2100 }
drh652fbf52008-04-01 01:42:41 +00002101 return iTarget;
2102}
2103
2104/*
drhcce7d172000-05-31 15:34:51 +00002105** Generate code into the current Vdbe to evaluate the given
drh2dcef112008-01-12 19:03:48 +00002106** expression. Attempt to store the results in register "target".
2107** Return the register where results are stored.
drh389a1ad2008-01-03 23:44:53 +00002108**
drh2dcef112008-01-12 19:03:48 +00002109** With this routine, there is no guaranteed that results will
2110** be stored in target. The result might be stored in some other
2111** register if it is convenient to do so. The calling function
2112** must check the return code and move the results to the desired
2113** register.
drhcce7d172000-05-31 15:34:51 +00002114*/
drh678ccce2008-03-31 18:19:54 +00002115int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){
drh2dcef112008-01-12 19:03:48 +00002116 Vdbe *v = pParse->pVdbe; /* The VM under construction */
2117 int op; /* The opcode being coded */
2118 int inReg = target; /* Results stored in register inReg */
2119 int regFree1 = 0; /* If non-zero free this temporary register */
2120 int regFree2 = 0; /* If non-zero free this temporary register */
drh678ccce2008-03-31 18:19:54 +00002121 int r1, r2, r3, r4; /* Various register numbers */
drhffe07b22005-11-03 00:41:17 +00002122
drh389a1ad2008-01-03 23:44:53 +00002123 assert( v!=0 || pParse->db->mallocFailed );
drh9cbf3422008-01-17 16:22:13 +00002124 assert( target>0 && target<=pParse->nMem );
drh389a1ad2008-01-03 23:44:53 +00002125 if( v==0 ) return 0;
drh389a1ad2008-01-03 23:44:53 +00002126
2127 if( pExpr==0 ){
2128 op = TK_NULL;
2129 }else{
2130 op = pExpr->op;
2131 }
drhf2bc0132004-10-04 13:19:23 +00002132 switch( op ){
drh13449892005-09-07 21:22:45 +00002133 case TK_AGG_COLUMN: {
2134 AggInfo *pAggInfo = pExpr->pAggInfo;
2135 struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg];
2136 if( !pAggInfo->directMode ){
drh9de221d2008-01-05 06:51:30 +00002137 assert( pCol->iMem>0 );
2138 inReg = pCol->iMem;
drh13449892005-09-07 21:22:45 +00002139 break;
2140 }else if( pAggInfo->useSortingIdx ){
drh389a1ad2008-01-03 23:44:53 +00002141 sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdx,
2142 pCol->iSorterColumn, target);
drh13449892005-09-07 21:22:45 +00002143 break;
2144 }
2145 /* Otherwise, fall thru into the TK_COLUMN case */
2146 }
drh967e8b72000-06-21 13:59:10 +00002147 case TK_COLUMN: {
drhffe07b22005-11-03 00:41:17 +00002148 if( pExpr->iTable<0 ){
2149 /* This only happens when coding check constraints */
drhaa9b8962008-01-08 02:57:55 +00002150 assert( pParse->ckBase>0 );
2151 inReg = pExpr->iColumn + pParse->ckBase;
drhc4a3c772001-04-04 11:48:57 +00002152 }else{
drhc5499be2008-04-01 15:06:33 +00002153 testcase( (pExpr->flags & EP_AnyAff)!=0 );
drhe55cbd72008-03-31 23:48:03 +00002154 inReg = sqlite3ExprCodeGetColumn(pParse, pExpr->pTab,
drhda250ea2008-04-01 05:07:14 +00002155 pExpr->iColumn, pExpr->iTable, target,
2156 pExpr->flags & EP_AnyAff);
drh22827922000-06-06 17:27:05 +00002157 }
drhcce7d172000-05-31 15:34:51 +00002158 break;
2159 }
2160 case TK_INTEGER: {
drh9de221d2008-01-05 06:51:30 +00002161 codeInteger(v, (char*)pExpr->token.z, pExpr->token.n, 0, target);
drhfec19aa2004-05-19 20:41:03 +00002162 break;
2163 }
drh598f1342007-10-23 15:39:45 +00002164 case TK_FLOAT: {
drh9de221d2008-01-05 06:51:30 +00002165 codeReal(v, (char*)pExpr->token.z, pExpr->token.n, 0, target);
drh598f1342007-10-23 15:39:45 +00002166 break;
2167 }
drhfec19aa2004-05-19 20:41:03 +00002168 case TK_STRING: {
danielk19771e536952007-08-16 10:09:01 +00002169 sqlite3DequoteExpr(pParse->db, pExpr);
drh9de221d2008-01-05 06:51:30 +00002170 sqlite3VdbeAddOp4(v,OP_String8, 0, target, 0,
drh66a51672008-01-03 00:01:23 +00002171 (char*)pExpr->token.z, pExpr->token.n);
drhcce7d172000-05-31 15:34:51 +00002172 break;
2173 }
drhf0863fe2005-06-12 21:35:51 +00002174 case TK_NULL: {
drh9de221d2008-01-05 06:51:30 +00002175 sqlite3VdbeAddOp2(v, OP_Null, 0, target);
drhf0863fe2005-06-12 21:35:51 +00002176 break;
2177 }
danielk19775338a5f2005-01-20 13:03:10 +00002178#ifndef SQLITE_OMIT_BLOB_LITERAL
danielk1977c572ef72004-05-27 09:28:41 +00002179 case TK_BLOB: {
drh6c8c6ce2005-08-23 11:17:58 +00002180 int n;
2181 const char *z;
drhca48c902008-01-18 14:08:24 +00002182 char *zBlob;
2183 assert( pExpr->token.n>=3 );
2184 assert( pExpr->token.z[0]=='x' || pExpr->token.z[0]=='X' );
2185 assert( pExpr->token.z[1]=='\'' );
2186 assert( pExpr->token.z[pExpr->token.n-1]=='\'' );
drh6c8c6ce2005-08-23 11:17:58 +00002187 n = pExpr->token.n - 3;
drh2646da72005-12-09 20:02:05 +00002188 z = (char*)pExpr->token.z + 2;
drhca48c902008-01-18 14:08:24 +00002189 zBlob = sqlite3HexToBlob(sqlite3VdbeDb(v), z, n);
2190 sqlite3VdbeAddOp4(v, OP_Blob, n/2, target, 0, zBlob, P4_DYNAMIC);
danielk1977c572ef72004-05-27 09:28:41 +00002191 break;
2192 }
danielk19775338a5f2005-01-20 13:03:10 +00002193#endif
drh50457892003-09-06 01:10:47 +00002194 case TK_VARIABLE: {
drh9de221d2008-01-05 06:51:30 +00002195 sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iTable, target);
drh895d7472004-08-20 16:02:39 +00002196 if( pExpr->token.n>1 ){
drh66a51672008-01-03 00:01:23 +00002197 sqlite3VdbeChangeP4(v, -1, (char*)pExpr->token.z, pExpr->token.n);
drh895d7472004-08-20 16:02:39 +00002198 }
drh50457892003-09-06 01:10:47 +00002199 break;
2200 }
drh4e0cff62004-11-05 05:10:28 +00002201 case TK_REGISTER: {
drh9de221d2008-01-05 06:51:30 +00002202 inReg = pExpr->iTable;
drh4e0cff62004-11-05 05:10:28 +00002203 break;
2204 }
drh487e2622005-06-25 18:42:14 +00002205#ifndef SQLITE_OMIT_CAST
2206 case TK_CAST: {
2207 /* Expressions of the form: CAST(pLeft AS token) */
danielk1977f0113002006-01-24 12:09:17 +00002208 int aff, to_op;
drh2dcef112008-01-12 19:03:48 +00002209 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
drh8a512562005-11-14 22:29:05 +00002210 aff = sqlite3AffinityType(&pExpr->token);
danielk1977f0113002006-01-24 12:09:17 +00002211 to_op = aff - SQLITE_AFF_TEXT + OP_ToText;
2212 assert( to_op==OP_ToText || aff!=SQLITE_AFF_TEXT );
2213 assert( to_op==OP_ToBlob || aff!=SQLITE_AFF_NONE );
2214 assert( to_op==OP_ToNumeric || aff!=SQLITE_AFF_NUMERIC );
2215 assert( to_op==OP_ToInt || aff!=SQLITE_AFF_INTEGER );
2216 assert( to_op==OP_ToReal || aff!=SQLITE_AFF_REAL );
drhc5499be2008-04-01 15:06:33 +00002217 testcase( to_op==OP_ToText );
2218 testcase( to_op==OP_ToBlob );
2219 testcase( to_op==OP_ToNumeric );
2220 testcase( to_op==OP_ToInt );
2221 testcase( to_op==OP_ToReal );
drh2dcef112008-01-12 19:03:48 +00002222 sqlite3VdbeAddOp1(v, to_op, inReg);
drhc5499be2008-04-01 15:06:33 +00002223 testcase( usedAsColumnCache(pParse, inReg, inReg) );
drhb3843a82008-04-01 12:24:11 +00002224 sqlite3ExprCacheAffinityChange(pParse, inReg, 1);
drh487e2622005-06-25 18:42:14 +00002225 break;
2226 }
2227#endif /* SQLITE_OMIT_CAST */
drhc9b84a12002-06-20 11:36:48 +00002228 case TK_LT:
2229 case TK_LE:
2230 case TK_GT:
2231 case TK_GE:
2232 case TK_NE:
2233 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00002234 assert( TK_LT==OP_Lt );
2235 assert( TK_LE==OP_Le );
2236 assert( TK_GT==OP_Gt );
2237 assert( TK_GE==OP_Ge );
2238 assert( TK_EQ==OP_Eq );
2239 assert( TK_NE==OP_Ne );
drhc5499be2008-04-01 15:06:33 +00002240 testcase( op==TK_LT );
2241 testcase( op==TK_LE );
2242 testcase( op==TK_GT );
2243 testcase( op==TK_GE );
2244 testcase( op==TK_EQ );
2245 testcase( op==TK_NE );
drhda250ea2008-04-01 05:07:14 +00002246 codeCompareOperands(pParse, pExpr->pLeft, &r1, &regFree1,
2247 pExpr->pRight, &r2, &regFree2);
drh35573352008-01-08 23:54:25 +00002248 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
2249 r1, r2, inReg, SQLITE_STOREP2);
drhc5499be2008-04-01 15:06:33 +00002250 testcase( regFree1==0 );
2251 testcase( regFree2==0 );
danielk1977a37cdde2004-05-16 11:15:36 +00002252 break;
drhc9b84a12002-06-20 11:36:48 +00002253 }
drhcce7d172000-05-31 15:34:51 +00002254 case TK_AND:
2255 case TK_OR:
2256 case TK_PLUS:
2257 case TK_STAR:
2258 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00002259 case TK_REM:
2260 case TK_BITAND:
2261 case TK_BITOR:
drh17c40292004-07-21 02:53:29 +00002262 case TK_SLASH:
drhbf4133c2001-10-13 02:59:08 +00002263 case TK_LSHIFT:
drh855eb1c2004-08-31 13:45:11 +00002264 case TK_RSHIFT:
drh00400772000-06-16 20:51:26 +00002265 case TK_CONCAT: {
drhf2bc0132004-10-04 13:19:23 +00002266 assert( TK_AND==OP_And );
2267 assert( TK_OR==OP_Or );
2268 assert( TK_PLUS==OP_Add );
2269 assert( TK_MINUS==OP_Subtract );
2270 assert( TK_REM==OP_Remainder );
2271 assert( TK_BITAND==OP_BitAnd );
2272 assert( TK_BITOR==OP_BitOr );
2273 assert( TK_SLASH==OP_Divide );
2274 assert( TK_LSHIFT==OP_ShiftLeft );
2275 assert( TK_RSHIFT==OP_ShiftRight );
2276 assert( TK_CONCAT==OP_Concat );
drhc5499be2008-04-01 15:06:33 +00002277 testcase( op==TK_AND );
2278 testcase( op==TK_OR );
2279 testcase( op==TK_PLUS );
2280 testcase( op==TK_MINUS );
2281 testcase( op==TK_REM );
2282 testcase( op==TK_BITAND );
2283 testcase( op==TK_BITOR );
2284 testcase( op==TK_SLASH );
2285 testcase( op==TK_LSHIFT );
2286 testcase( op==TK_RSHIFT );
2287 testcase( op==TK_CONCAT );
drh2dcef112008-01-12 19:03:48 +00002288 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
2289 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
drh5b6afba2008-01-05 16:29:28 +00002290 sqlite3VdbeAddOp3(v, op, r2, r1, target);
drhc5499be2008-04-01 15:06:33 +00002291 testcase( regFree1==0 );
2292 testcase( regFree2==0 );
drh00400772000-06-16 20:51:26 +00002293 break;
2294 }
drhcce7d172000-05-31 15:34:51 +00002295 case TK_UMINUS: {
drhfec19aa2004-05-19 20:41:03 +00002296 Expr *pLeft = pExpr->pLeft;
2297 assert( pLeft );
2298 if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
2299 Token *p = &pLeft->token;
drhfec19aa2004-05-19 20:41:03 +00002300 if( pLeft->op==TK_FLOAT ){
drh9de221d2008-01-05 06:51:30 +00002301 codeReal(v, (char*)p->z, p->n, 1, target);
drhe6840902002-03-06 03:08:25 +00002302 }else{
drh9de221d2008-01-05 06:51:30 +00002303 codeInteger(v, (char*)p->z, p->n, 1, target);
drhe6840902002-03-06 03:08:25 +00002304 }
drh3c84ddf2008-01-09 02:15:38 +00002305 }else{
drh2dcef112008-01-12 19:03:48 +00002306 regFree1 = r1 = sqlite3GetTempReg(pParse);
drh3c84ddf2008-01-09 02:15:38 +00002307 sqlite3VdbeAddOp2(v, OP_Integer, 0, r1);
drhe55cbd72008-03-31 23:48:03 +00002308 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree2);
drh2dcef112008-01-12 19:03:48 +00002309 sqlite3VdbeAddOp3(v, OP_Subtract, r2, r1, target);
drhc5499be2008-04-01 15:06:33 +00002310 testcase( regFree2==0 );
drh6e142f52000-06-08 13:36:40 +00002311 }
drh3c84ddf2008-01-09 02:15:38 +00002312 inReg = target;
2313 break;
drh6e142f52000-06-08 13:36:40 +00002314 }
drhbf4133c2001-10-13 02:59:08 +00002315 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00002316 case TK_NOT: {
drhf2bc0132004-10-04 13:19:23 +00002317 assert( TK_BITNOT==OP_BitNot );
2318 assert( TK_NOT==OP_Not );
drhc5499be2008-04-01 15:06:33 +00002319 testcase( op==TK_BITNOT );
2320 testcase( op==TK_NOT );
drh2dcef112008-01-12 19:03:48 +00002321 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
drhc5499be2008-04-01 15:06:33 +00002322 testcase( inReg==target );
2323 testcase( usedAsColumnCache(pParse, inReg, inReg) );
drh652fbf52008-04-01 01:42:41 +00002324 inReg = sqlite3ExprWritableRegister(pParse, inReg, target);
drh2dcef112008-01-12 19:03:48 +00002325 sqlite3VdbeAddOp1(v, op, inReg);
drhcce7d172000-05-31 15:34:51 +00002326 break;
2327 }
2328 case TK_ISNULL:
2329 case TK_NOTNULL: {
drh6a288a32008-01-07 19:20:24 +00002330 int addr;
drhf2bc0132004-10-04 13:19:23 +00002331 assert( TK_ISNULL==OP_IsNull );
2332 assert( TK_NOTNULL==OP_NotNull );
drhc5499be2008-04-01 15:06:33 +00002333 testcase( op==TK_ISNULL );
2334 testcase( op==TK_NOTNULL );
drh9de221d2008-01-05 06:51:30 +00002335 sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
drh2dcef112008-01-12 19:03:48 +00002336 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
drhc5499be2008-04-01 15:06:33 +00002337 testcase( regFree1==0 );
drh2dcef112008-01-12 19:03:48 +00002338 addr = sqlite3VdbeAddOp1(v, op, r1);
drh9de221d2008-01-05 06:51:30 +00002339 sqlite3VdbeAddOp2(v, OP_AddImm, target, -1);
drh6a288a32008-01-07 19:20:24 +00002340 sqlite3VdbeJumpHere(v, addr);
drhf2bc0132004-10-04 13:19:23 +00002341 break;
drhcce7d172000-05-31 15:34:51 +00002342 }
drh22827922000-06-06 17:27:05 +00002343 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00002344 AggInfo *pInfo = pExpr->pAggInfo;
drh7e56e712005-11-16 12:53:15 +00002345 if( pInfo==0 ){
2346 sqlite3ErrorMsg(pParse, "misuse of aggregate: %T",
2347 &pExpr->span);
2348 }else{
drh9de221d2008-01-05 06:51:30 +00002349 inReg = pInfo->aFunc[pExpr->iAgg].iMem;
drh7e56e712005-11-16 12:53:15 +00002350 }
drh22827922000-06-06 17:27:05 +00002351 break;
2352 }
drhb71090f2005-05-23 17:26:51 +00002353 case TK_CONST_FUNC:
drhcce7d172000-05-31 15:34:51 +00002354 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00002355 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00002356 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00002357 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00002358 int nId;
2359 const char *zId;
drh13449892005-09-07 21:22:45 +00002360 int constMask = 0;
danielk1977682f68b2004-06-05 10:22:17 +00002361 int i;
drh17435752007-08-16 04:30:38 +00002362 sqlite3 *db = pParse->db;
2363 u8 enc = ENC(db);
danielk1977dc1bdc42004-06-11 10:51:27 +00002364 CollSeq *pColl = 0;
drh17435752007-08-16 04:30:38 +00002365
drhc5499be2008-04-01 15:06:33 +00002366 testcase( op==TK_CONST_FUNC );
2367 testcase( op==TK_FUNCTION );
drh2646da72005-12-09 20:02:05 +00002368 zId = (char*)pExpr->token.z;
drhb71090f2005-05-23 17:26:51 +00002369 nId = pExpr->token.n;
danielk1977d8123362004-06-12 09:25:12 +00002370 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0);
drh0bce8352002-02-28 00:41:10 +00002371 assert( pDef!=0 );
drh892d3172008-01-10 03:46:36 +00002372 if( pList ){
2373 nExpr = pList->nExpr;
drh2dcef112008-01-12 19:03:48 +00002374 r1 = sqlite3GetTempRange(pParse, nExpr);
2375 sqlite3ExprCodeExprList(pParse, pList, r1);
drh892d3172008-01-10 03:46:36 +00002376 }else{
drhd847eaa2008-01-17 17:15:56 +00002377 nExpr = r1 = 0;
drh892d3172008-01-10 03:46:36 +00002378 }
drhb7f6f682006-07-08 17:06:43 +00002379#ifndef SQLITE_OMIT_VIRTUALTABLE
drha43fa222006-07-08 18:41:37 +00002380 /* Possibly overload the function if the first argument is
2381 ** a virtual table column.
2382 **
2383 ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the
2384 ** second argument, not the first, as the argument to test to
2385 ** see if it is a column in a virtual table. This is done because
2386 ** the left operand of infix functions (the operand we want to
2387 ** control overloading) ends up as the second argument to the
2388 ** function. The expression "A glob B" is equivalent to
2389 ** "glob(B,A). We want to use the A in "A glob B" to test
2390 ** for function overloading. But we use the B term in "glob(B,A)".
2391 */
drh6a03a1c2006-07-08 18:34:59 +00002392 if( nExpr>=2 && (pExpr->flags & EP_InfixFunc) ){
drh17435752007-08-16 04:30:38 +00002393 pDef = sqlite3VtabOverloadFunction(db, pDef, nExpr, pList->a[1].pExpr);
drh6a03a1c2006-07-08 18:34:59 +00002394 }else if( nExpr>0 ){
drh17435752007-08-16 04:30:38 +00002395 pDef = sqlite3VtabOverloadFunction(db, pDef, nExpr, pList->a[0].pExpr);
drhb7f6f682006-07-08 17:06:43 +00002396 }
2397#endif
danielk1977682f68b2004-06-05 10:22:17 +00002398 for(i=0; i<nExpr && i<32; i++){
danielk1977d02eb1f2004-06-06 09:44:03 +00002399 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
drh13449892005-09-07 21:22:45 +00002400 constMask |= (1<<i);
danielk1977d02eb1f2004-06-06 09:44:03 +00002401 }
danielk1977dc1bdc42004-06-11 10:51:27 +00002402 if( pDef->needCollSeq && !pColl ){
2403 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
2404 }
2405 }
2406 if( pDef->needCollSeq ){
2407 if( !pColl ) pColl = pParse->db->pDfltColl;
drh66a51672008-01-03 00:01:23 +00002408 sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ);
danielk1977682f68b2004-06-05 10:22:17 +00002409 }
drh2dcef112008-01-12 19:03:48 +00002410 sqlite3VdbeAddOp4(v, OP_Function, constMask, r1, target,
drh66a51672008-01-03 00:01:23 +00002411 (char*)pDef, P4_FUNCDEF);
drh98757152008-01-09 23:04:12 +00002412 sqlite3VdbeChangeP5(v, nExpr);
drh2dcef112008-01-12 19:03:48 +00002413 if( nExpr ){
2414 sqlite3ReleaseTempRange(pParse, r1, nExpr);
2415 }
drhda250ea2008-04-01 05:07:14 +00002416 sqlite3ExprCacheAffinityChange(pParse, r1, nExpr);
drhcce7d172000-05-31 15:34:51 +00002417 break;
2418 }
drhfe2093d2005-01-20 22:48:47 +00002419#ifndef SQLITE_OMIT_SUBQUERY
2420 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00002421 case TK_SELECT: {
drhc5499be2008-04-01 15:06:33 +00002422 testcase( op==TK_EXISTS );
2423 testcase( op==TK_SELECT );
drh41714d62006-03-02 04:44:23 +00002424 if( pExpr->iColumn==0 ){
2425 sqlite3CodeSubselect(pParse, pExpr);
2426 }
drh9de221d2008-01-05 06:51:30 +00002427 inReg = pExpr->iColumn;
drh19a775c2000-06-05 18:54:46 +00002428 break;
2429 }
drhfef52082000-06-06 01:50:43 +00002430 case TK_IN: {
drh6a288a32008-01-07 19:20:24 +00002431 int j1, j2, j3, j4, j5;
drh94a11212004-09-25 13:12:14 +00002432 char affinity;
danielk19779a96b662007-11-29 17:05:18 +00002433 int eType;
danielk19779a96b662007-11-29 17:05:18 +00002434
2435 eType = sqlite3FindInIndex(pParse, pExpr, 0);
danielk1977e014a832004-05-17 10:48:57 +00002436
2437 /* Figure out the affinity to use to create a key from the results
2438 ** of the expression. affinityStr stores a static string suitable for
drh66a51672008-01-03 00:01:23 +00002439 ** P4 of OP_MakeRecord.
danielk1977e014a832004-05-17 10:48:57 +00002440 */
drh94a11212004-09-25 13:12:14 +00002441 affinity = comparisonAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +00002442
drh2dcef112008-01-12 19:03:48 +00002443 sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
danielk1977e014a832004-05-17 10:48:57 +00002444
2445 /* Code the <expr> from "<expr> IN (...)". The temporary table
2446 ** pExpr->iTable contains the values that make up the (...) set.
2447 */
drh2dcef112008-01-12 19:03:48 +00002448 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
drhc5499be2008-04-01 15:06:33 +00002449 testcase( regFree1==0 );
drh2dcef112008-01-12 19:03:48 +00002450 j1 = sqlite3VdbeAddOp1(v, OP_NotNull, r1);
2451 sqlite3VdbeAddOp2(v, OP_Null, 0, target);
drh6a288a32008-01-07 19:20:24 +00002452 j2 = sqlite3VdbeAddOp0(v, OP_Goto);
2453 sqlite3VdbeJumpHere(v, j1);
danielk19779a96b662007-11-29 17:05:18 +00002454 if( eType==IN_INDEX_ROWID ){
drh678ccce2008-03-31 18:19:54 +00002455 j3 = sqlite3VdbeAddOp1(v, OP_MustBeInt, r1);
drh2dcef112008-01-12 19:03:48 +00002456 j4 = sqlite3VdbeAddOp3(v, OP_NotExists, pExpr->iTable, 0, r1);
drh6a288a32008-01-07 19:20:24 +00002457 j5 = sqlite3VdbeAddOp0(v, OP_Goto);
2458 sqlite3VdbeJumpHere(v, j3);
2459 sqlite3VdbeJumpHere(v, j4);
danielk19779a96b662007-11-29 17:05:18 +00002460 }else{
drh2dcef112008-01-12 19:03:48 +00002461 r2 = regFree2 = sqlite3GetTempReg(pParse);
drh1db639c2008-01-17 02:36:28 +00002462 sqlite3VdbeAddOp4(v, OP_MakeRecord, r1, 1, r2, &affinity, 1);
drhda250ea2008-04-01 05:07:14 +00002463 sqlite3ExprCacheAffinityChange(pParse, r1, 1);
drh2dcef112008-01-12 19:03:48 +00002464 j5 = sqlite3VdbeAddOp3(v, OP_Found, pExpr->iTable, 0, r2);
danielk19779a96b662007-11-29 17:05:18 +00002465 }
drh2dcef112008-01-12 19:03:48 +00002466 sqlite3VdbeAddOp2(v, OP_AddImm, target, -1);
drh6a288a32008-01-07 19:20:24 +00002467 sqlite3VdbeJumpHere(v, j2);
2468 sqlite3VdbeJumpHere(v, j5);
drhfef52082000-06-06 01:50:43 +00002469 break;
2470 }
danielk197793758c82005-01-21 08:13:14 +00002471#endif
drh2dcef112008-01-12 19:03:48 +00002472 /*
2473 ** x BETWEEN y AND z
2474 **
2475 ** This is equivalent to
2476 **
2477 ** x>=y AND x<=z
2478 **
2479 ** X is stored in pExpr->pLeft.
2480 ** Y is stored in pExpr->pList->a[0].pExpr.
2481 ** Z is stored in pExpr->pList->a[1].pExpr.
2482 */
drhfef52082000-06-06 01:50:43 +00002483 case TK_BETWEEN: {
drhbe5c89a2004-07-26 00:31:09 +00002484 Expr *pLeft = pExpr->pLeft;
2485 struct ExprList_item *pLItem = pExpr->pList->a;
2486 Expr *pRight = pLItem->pExpr;
drh35573352008-01-08 23:54:25 +00002487
drhda250ea2008-04-01 05:07:14 +00002488 codeCompareOperands(pParse, pLeft, &r1, &regFree1,
2489 pRight, &r2, &regFree2);
drhc5499be2008-04-01 15:06:33 +00002490 testcase( regFree1==0 );
2491 testcase( regFree2==0 );
drh2dcef112008-01-12 19:03:48 +00002492 r3 = sqlite3GetTempReg(pParse);
drh678ccce2008-03-31 18:19:54 +00002493 r4 = sqlite3GetTempReg(pParse);
drh35573352008-01-08 23:54:25 +00002494 codeCompare(pParse, pLeft, pRight, OP_Ge,
2495 r1, r2, r3, SQLITE_STOREP2);
drhbe5c89a2004-07-26 00:31:09 +00002496 pLItem++;
2497 pRight = pLItem->pExpr;
drh2dcef112008-01-12 19:03:48 +00002498 sqlite3ReleaseTempReg(pParse, regFree2);
2499 r2 = sqlite3ExprCodeTemp(pParse, pRight, &regFree2);
drhc5499be2008-04-01 15:06:33 +00002500 testcase( regFree2==0 );
drh678ccce2008-03-31 18:19:54 +00002501 codeCompare(pParse, pLeft, pRight, OP_Le, r1, r2, r4, SQLITE_STOREP2);
2502 sqlite3VdbeAddOp3(v, OP_And, r3, r4, target);
drh2dcef112008-01-12 19:03:48 +00002503 sqlite3ReleaseTempReg(pParse, r3);
drh678ccce2008-03-31 18:19:54 +00002504 sqlite3ReleaseTempReg(pParse, r4);
drhfef52082000-06-06 01:50:43 +00002505 break;
2506 }
drh4f07e5f2007-05-14 11:34:46 +00002507 case TK_UPLUS: {
drh2dcef112008-01-12 19:03:48 +00002508 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
drha2e00042002-01-22 03:13:42 +00002509 break;
2510 }
drh2dcef112008-01-12 19:03:48 +00002511
2512 /*
2513 ** Form A:
2514 ** CASE x WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
2515 **
2516 ** Form B:
2517 ** CASE WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
2518 **
2519 ** Form A is can be transformed into the equivalent form B as follows:
2520 ** CASE WHEN x=e1 THEN r1 WHEN x=e2 THEN r2 ...
2521 ** WHEN x=eN THEN rN ELSE y END
2522 **
2523 ** X (if it exists) is in pExpr->pLeft.
2524 ** Y is in pExpr->pRight. The Y is also optional. If there is no
2525 ** ELSE clause and no other term matches, then the result of the
2526 ** exprssion is NULL.
2527 ** Ei is in pExpr->pList->a[i*2] and Ri is pExpr->pList->a[i*2+1].
2528 **
2529 ** The result of the expression is the Ri for the first matching Ei,
2530 ** or if there is no matching Ei, the ELSE term Y, or if there is
2531 ** no ELSE term, NULL.
2532 */
drh17a7f8d2002-03-24 13:13:27 +00002533 case TK_CASE: {
drh2dcef112008-01-12 19:03:48 +00002534 int endLabel; /* GOTO label for end of CASE stmt */
2535 int nextCase; /* GOTO label for next WHEN clause */
2536 int nExpr; /* 2x number of WHEN terms */
2537 int i; /* Loop counter */
2538 ExprList *pEList; /* List of WHEN terms */
2539 struct ExprList_item *aListelem; /* Array of WHEN terms */
2540 Expr opCompare; /* The X==Ei expression */
2541 Expr cacheX; /* Cached expression X */
2542 Expr *pX; /* The X expression */
2543 Expr *pTest; /* X==Ei (form A) or just Ei (form B) */
drh17a7f8d2002-03-24 13:13:27 +00002544
2545 assert(pExpr->pList);
2546 assert((pExpr->pList->nExpr % 2) == 0);
2547 assert(pExpr->pList->nExpr > 0);
drhbe5c89a2004-07-26 00:31:09 +00002548 pEList = pExpr->pList;
2549 aListelem = pEList->a;
2550 nExpr = pEList->nExpr;
drh2dcef112008-01-12 19:03:48 +00002551 endLabel = sqlite3VdbeMakeLabel(v);
2552 if( (pX = pExpr->pLeft)!=0 ){
2553 cacheX = *pX;
drhc5499be2008-04-01 15:06:33 +00002554 testcase( pX->op==TK_COLUMN || pX->op==TK_REGISTER );
drh2dcef112008-01-12 19:03:48 +00002555 cacheX.iTable = sqlite3ExprCodeTemp(pParse, pX, &regFree1);
drhc5499be2008-04-01 15:06:33 +00002556 testcase( regFree1==0 );
drh2dcef112008-01-12 19:03:48 +00002557 cacheX.op = TK_REGISTER;
drh678ccce2008-03-31 18:19:54 +00002558 cacheX.iColumn = 0;
drh2dcef112008-01-12 19:03:48 +00002559 opCompare.op = TK_EQ;
2560 opCompare.pLeft = &cacheX;
2561 pTest = &opCompare;
drh17a7f8d2002-03-24 13:13:27 +00002562 }
drhc5499be2008-04-01 15:06:33 +00002563 pParse->disableColCache++;
drhf5905aa2002-05-26 20:54:33 +00002564 for(i=0; i<nExpr; i=i+2){
drh2dcef112008-01-12 19:03:48 +00002565 if( pX ){
2566 opCompare.pRight = aListelem[i].pExpr;
drh17a7f8d2002-03-24 13:13:27 +00002567 }else{
drh2dcef112008-01-12 19:03:48 +00002568 pTest = aListelem[i].pExpr;
drh17a7f8d2002-03-24 13:13:27 +00002569 }
drh2dcef112008-01-12 19:03:48 +00002570 nextCase = sqlite3VdbeMakeLabel(v);
drhc5499be2008-04-01 15:06:33 +00002571 testcase( pTest->op==TK_COLUMN || pTest->op==TK_REGISTER );
drh2dcef112008-01-12 19:03:48 +00002572 sqlite3ExprIfFalse(pParse, pTest, nextCase, SQLITE_JUMPIFNULL);
drhc5499be2008-04-01 15:06:33 +00002573 testcase( aListelem[i+1].pExpr->op==TK_COLUMN );
2574 testcase( aListelem[i+1].pExpr->op==TK_REGISTER );
drh9de221d2008-01-05 06:51:30 +00002575 sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target);
drh2dcef112008-01-12 19:03:48 +00002576 sqlite3VdbeAddOp2(v, OP_Goto, 0, endLabel);
2577 sqlite3VdbeResolveLabel(v, nextCase);
drhf570f012002-05-31 15:51:25 +00002578 }
drh17a7f8d2002-03-24 13:13:27 +00002579 if( pExpr->pRight ){
drh9de221d2008-01-05 06:51:30 +00002580 sqlite3ExprCode(pParse, pExpr->pRight, target);
drh17a7f8d2002-03-24 13:13:27 +00002581 }else{
drh9de221d2008-01-05 06:51:30 +00002582 sqlite3VdbeAddOp2(v, OP_Null, 0, target);
drh17a7f8d2002-03-24 13:13:27 +00002583 }
drh2dcef112008-01-12 19:03:48 +00002584 sqlite3VdbeResolveLabel(v, endLabel);
drhc5499be2008-04-01 15:06:33 +00002585 assert( pParse->disableColCache>0 );
2586 pParse->disableColCache--;
danielk19776f349032002-06-11 02:25:40 +00002587 break;
2588 }
danielk19775338a5f2005-01-20 13:03:10 +00002589#ifndef SQLITE_OMIT_TRIGGER
danielk19776f349032002-06-11 02:25:40 +00002590 case TK_RAISE: {
2591 if( !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +00002592 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00002593 "RAISE() may only be used within a trigger-program");
drh389a1ad2008-01-03 23:44:53 +00002594 return 0;
danielk19776f349032002-06-11 02:25:40 +00002595 }
drhad6d9462004-09-19 02:15:24 +00002596 if( pExpr->iColumn!=OE_Ignore ){
2597 assert( pExpr->iColumn==OE_Rollback ||
2598 pExpr->iColumn == OE_Abort ||
2599 pExpr->iColumn == OE_Fail );
danielk19771e536952007-08-16 10:09:01 +00002600 sqlite3DequoteExpr(pParse->db, pExpr);
drh66a51672008-01-03 00:01:23 +00002601 sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn, 0,
drh2646da72005-12-09 20:02:05 +00002602 (char*)pExpr->token.z, pExpr->token.n);
danielk19776f349032002-06-11 02:25:40 +00002603 } else {
drhad6d9462004-09-19 02:15:24 +00002604 assert( pExpr->iColumn == OE_Ignore );
drh66a51672008-01-03 00:01:23 +00002605 sqlite3VdbeAddOp2(v, OP_ContextPop, 0, 0);
2606 sqlite3VdbeAddOp2(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
drhd4e70eb2008-01-02 00:34:36 +00002607 VdbeComment((v, "raise(IGNORE)"));
danielk19776f349032002-06-11 02:25:40 +00002608 }
drhffe07b22005-11-03 00:41:17 +00002609 break;
drh17a7f8d2002-03-24 13:13:27 +00002610 }
danielk19775338a5f2005-01-20 13:03:10 +00002611#endif
drhffe07b22005-11-03 00:41:17 +00002612 }
drh2dcef112008-01-12 19:03:48 +00002613 sqlite3ReleaseTempReg(pParse, regFree1);
2614 sqlite3ReleaseTempReg(pParse, regFree2);
2615 return inReg;
2616}
2617
2618/*
2619** Generate code to evaluate an expression and store the results
2620** into a register. Return the register number where the results
2621** are stored.
2622**
2623** If the register is a temporary register that can be deallocated,
drh678ccce2008-03-31 18:19:54 +00002624** then write its number into *pReg. If the result register is not
drh2dcef112008-01-12 19:03:48 +00002625** a temporary, then set *pReg to zero.
2626*/
2627int sqlite3ExprCodeTemp(Parse *pParse, Expr *pExpr, int *pReg){
2628 int r1 = sqlite3GetTempReg(pParse);
2629 int r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
2630 if( r2==r1 ){
2631 *pReg = r1;
2632 }else{
2633 sqlite3ReleaseTempReg(pParse, r1);
2634 *pReg = 0;
2635 }
2636 return r2;
2637}
2638
2639/*
2640** Generate code that will evaluate expression pExpr and store the
2641** results in register target. The results are guaranteed to appear
2642** in register target.
2643*/
2644int sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){
drh9cbf3422008-01-17 16:22:13 +00002645 int inReg;
2646
2647 assert( target>0 && target<=pParse->nMem );
2648 inReg = sqlite3ExprCodeTarget(pParse, pExpr, target);
drh0e359b32008-01-13 19:02:11 +00002649 assert( pParse->pVdbe || pParse->db->mallocFailed );
2650 if( inReg!=target && pParse->pVdbe ){
drh9cbf3422008-01-17 16:22:13 +00002651 sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, inReg, target);
drhcce7d172000-05-31 15:34:51 +00002652 }
drh389a1ad2008-01-03 23:44:53 +00002653 return target;
drhcce7d172000-05-31 15:34:51 +00002654}
2655
2656/*
drh2dcef112008-01-12 19:03:48 +00002657** Generate code that evalutes the given expression and puts the result
drhde4fcfd2008-01-19 23:50:26 +00002658** in register target.
drh25303782004-12-07 15:41:48 +00002659**
drh2dcef112008-01-12 19:03:48 +00002660** Also make a copy of the expression results into another "cache" register
2661** and modify the expression so that the next time it is evaluated,
2662** the result is a copy of the cache register.
2663**
2664** This routine is used for expressions that are used multiple
2665** times. They are evaluated once and the results of the expression
2666** are reused.
drh25303782004-12-07 15:41:48 +00002667*/
drh2dcef112008-01-12 19:03:48 +00002668int sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr, int target){
drh25303782004-12-07 15:41:48 +00002669 Vdbe *v = pParse->pVdbe;
drh2dcef112008-01-12 19:03:48 +00002670 int inReg;
2671 inReg = sqlite3ExprCode(pParse, pExpr, target);
drhde4fcfd2008-01-19 23:50:26 +00002672 assert( target>0 );
drh2dcef112008-01-12 19:03:48 +00002673 if( pExpr->op!=TK_REGISTER ){
2674 int iMem;
drhde4fcfd2008-01-19 23:50:26 +00002675 iMem = ++pParse->nMem;
2676 sqlite3VdbeAddOp2(v, OP_Copy, inReg, iMem);
drh2dcef112008-01-12 19:03:48 +00002677 pExpr->iTable = iMem;
drh678ccce2008-03-31 18:19:54 +00002678 pExpr->iColumn = pExpr->op;
drh25303782004-12-07 15:41:48 +00002679 pExpr->op = TK_REGISTER;
2680 }
drh2dcef112008-01-12 19:03:48 +00002681 return inReg;
drh25303782004-12-07 15:41:48 +00002682}
drh2dcef112008-01-12 19:03:48 +00002683
drh678ccce2008-03-31 18:19:54 +00002684/*
2685** If pExpr is a constant expression, then evaluate the expression
2686** into a register and convert the expression into a TK_REGISTER
2687** expression.
2688*/
2689static int evalConstExpr(void *pArg, Expr *pExpr){
2690 Parse *pParse = (Parse*)pArg;
2691 if( pExpr->op==TK_REGISTER ){
2692 return 1;
2693 }
2694 if( sqlite3ExprIsConstantNotJoin(pExpr) ){
2695 int r1 = ++pParse->nMem;
2696 int r2;
2697 r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
drhc5499be2008-04-01 15:06:33 +00002698 if( r1!=r2 ) sqlite3ReleaseTempReg(pParse, r1);
drh678ccce2008-03-31 18:19:54 +00002699 pExpr->iColumn = pExpr->op;
2700 pExpr->op = TK_REGISTER;
2701 pExpr->iTable = r2;
2702 return 1;
2703 }
2704 return 0;
2705}
2706
2707/*
2708** Preevaluate constant subexpressions within pExpr and store the
2709** results in registers. Modify pExpr so that the constant subexpresions
2710** are TK_REGISTER opcodes that refer to the precomputed values.
2711*/
2712void sqlite3ExprCodeConstants(Parse *pParse, Expr *pExpr){
2713 walkExprTree(pExpr, evalConstExpr, pParse);
2714}
2715
drh25303782004-12-07 15:41:48 +00002716
2717/*
drh268380c2004-02-25 13:47:31 +00002718** Generate code that pushes the value of every element of the given
drh9cbf3422008-01-17 16:22:13 +00002719** expression list into a sequence of registers beginning at target.
drh268380c2004-02-25 13:47:31 +00002720**
drh892d3172008-01-10 03:46:36 +00002721** Return the number of elements evaluated.
drh268380c2004-02-25 13:47:31 +00002722*/
danielk19774adee202004-05-08 08:23:19 +00002723int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00002724 Parse *pParse, /* Parsing context */
drh389a1ad2008-01-03 23:44:53 +00002725 ExprList *pList, /* The expression list to be coded */
2726 int target /* Where to write results */
drh268380c2004-02-25 13:47:31 +00002727){
2728 struct ExprList_item *pItem;
drh9cbf3422008-01-17 16:22:13 +00002729 int i, n;
drh892d3172008-01-10 03:46:36 +00002730 assert( pList!=0 || pParse->db->mallocFailed );
2731 if( pList==0 ){
2732 return 0;
2733 }
drh9cbf3422008-01-17 16:22:13 +00002734 assert( target>0 );
drh268380c2004-02-25 13:47:31 +00002735 n = pList->nExpr;
drhc182d162005-08-14 20:47:16 +00002736 for(pItem=pList->a, i=n; i>0; i--, pItem++){
drh389a1ad2008-01-03 23:44:53 +00002737 sqlite3ExprCode(pParse, pItem->pExpr, target);
drh9cbf3422008-01-17 16:22:13 +00002738 target++;
drh268380c2004-02-25 13:47:31 +00002739 }
drhf9b596e2004-05-26 16:54:42 +00002740 return n;
drh268380c2004-02-25 13:47:31 +00002741}
2742
2743/*
drhcce7d172000-05-31 15:34:51 +00002744** Generate code for a boolean expression such that a jump is made
2745** to the label "dest" if the expression is true but execution
2746** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00002747**
2748** If the expression evaluates to NULL (neither true nor false), then
drh35573352008-01-08 23:54:25 +00002749** take the jump if the jumpIfNull flag is SQLITE_JUMPIFNULL.
drhf2bc0132004-10-04 13:19:23 +00002750**
2751** This code depends on the fact that certain token values (ex: TK_EQ)
2752** are the same as opcode values (ex: OP_Eq) that implement the corresponding
2753** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
2754** the make process cause these values to align. Assert()s in the code
2755** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00002756*/
danielk19774adee202004-05-08 08:23:19 +00002757void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00002758 Vdbe *v = pParse->pVdbe;
2759 int op = 0;
drh2dcef112008-01-12 19:03:48 +00002760 int regFree1 = 0;
2761 int regFree2 = 0;
2762 int r1, r2;
2763
drh35573352008-01-08 23:54:25 +00002764 assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
drhdaffd0e2001-04-11 14:28:42 +00002765 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00002766 op = pExpr->op;
2767 switch( op ){
drhcce7d172000-05-31 15:34:51 +00002768 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00002769 int d2 = sqlite3VdbeMakeLabel(v);
drhc5499be2008-04-01 15:06:33 +00002770 testcase( jumpIfNull==0 );
2771 testcase( pParse->disableColCache==0 );
drh35573352008-01-08 23:54:25 +00002772 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,jumpIfNull^SQLITE_JUMPIFNULL);
drhe55cbd72008-03-31 23:48:03 +00002773 pParse->disableColCache++;
danielk19774adee202004-05-08 08:23:19 +00002774 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhc5499be2008-04-01 15:06:33 +00002775 assert( pParse->disableColCache>0 );
drhe55cbd72008-03-31 23:48:03 +00002776 pParse->disableColCache--;
danielk19774adee202004-05-08 08:23:19 +00002777 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00002778 break;
2779 }
2780 case TK_OR: {
drhc5499be2008-04-01 15:06:33 +00002781 testcase( jumpIfNull==0 );
2782 testcase( pParse->disableColCache==0 );
danielk19774adee202004-05-08 08:23:19 +00002783 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhe55cbd72008-03-31 23:48:03 +00002784 pParse->disableColCache++;
danielk19774adee202004-05-08 08:23:19 +00002785 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhc5499be2008-04-01 15:06:33 +00002786 assert( pParse->disableColCache>0 );
drhe55cbd72008-03-31 23:48:03 +00002787 pParse->disableColCache--;
drhcce7d172000-05-31 15:34:51 +00002788 break;
2789 }
2790 case TK_NOT: {
drhc5499be2008-04-01 15:06:33 +00002791 testcase( jumpIfNull==0 );
danielk19774adee202004-05-08 08:23:19 +00002792 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002793 break;
2794 }
2795 case TK_LT:
2796 case TK_LE:
2797 case TK_GT:
2798 case TK_GE:
2799 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00002800 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00002801 assert( TK_LT==OP_Lt );
2802 assert( TK_LE==OP_Le );
2803 assert( TK_GT==OP_Gt );
2804 assert( TK_GE==OP_Ge );
2805 assert( TK_EQ==OP_Eq );
2806 assert( TK_NE==OP_Ne );
drhc5499be2008-04-01 15:06:33 +00002807 testcase( op==TK_LT );
2808 testcase( op==TK_LE );
2809 testcase( op==TK_GT );
2810 testcase( op==TK_GE );
2811 testcase( op==TK_EQ );
2812 testcase( op==TK_NE );
2813 testcase( jumpIfNull==0 );
drhda250ea2008-04-01 05:07:14 +00002814 codeCompareOperands(pParse, pExpr->pLeft, &r1, &regFree1,
2815 pExpr->pRight, &r2, &regFree2);
drh35573352008-01-08 23:54:25 +00002816 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
drh2dcef112008-01-12 19:03:48 +00002817 r1, r2, dest, jumpIfNull);
drhc5499be2008-04-01 15:06:33 +00002818 testcase( regFree1==0 );
2819 testcase( regFree2==0 );
drhcce7d172000-05-31 15:34:51 +00002820 break;
2821 }
2822 case TK_ISNULL:
2823 case TK_NOTNULL: {
drhf2bc0132004-10-04 13:19:23 +00002824 assert( TK_ISNULL==OP_IsNull );
2825 assert( TK_NOTNULL==OP_NotNull );
drhc5499be2008-04-01 15:06:33 +00002826 testcase( op==TK_ISNULL );
2827 testcase( op==TK_NOTNULL );
drh2dcef112008-01-12 19:03:48 +00002828 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
2829 sqlite3VdbeAddOp2(v, op, r1, dest);
drhc5499be2008-04-01 15:06:33 +00002830 testcase( regFree1==0 );
drhcce7d172000-05-31 15:34:51 +00002831 break;
2832 }
drhfef52082000-06-06 01:50:43 +00002833 case TK_BETWEEN: {
drh2dcef112008-01-12 19:03:48 +00002834 /* x BETWEEN y AND z
danielk19770202b292004-06-09 09:55:16 +00002835 **
drh2dcef112008-01-12 19:03:48 +00002836 ** Is equivalent to
2837 **
2838 ** x>=y AND x<=z
2839 **
2840 ** Code it as such, taking care to do the common subexpression
2841 ** elementation of x.
danielk19770202b292004-06-09 09:55:16 +00002842 */
drh2dcef112008-01-12 19:03:48 +00002843 Expr exprAnd;
2844 Expr compLeft;
2845 Expr compRight;
2846 Expr exprX;
danielk19770202b292004-06-09 09:55:16 +00002847
drh2dcef112008-01-12 19:03:48 +00002848 exprX = *pExpr->pLeft;
2849 exprAnd.op = TK_AND;
2850 exprAnd.pLeft = &compLeft;
2851 exprAnd.pRight = &compRight;
2852 compLeft.op = TK_GE;
2853 compLeft.pLeft = &exprX;
2854 compLeft.pRight = pExpr->pList->a[0].pExpr;
2855 compRight.op = TK_LE;
2856 compRight.pLeft = &exprX;
2857 compRight.pRight = pExpr->pList->a[1].pExpr;
2858 exprX.iTable = sqlite3ExprCodeTemp(pParse, &exprX, &regFree1);
drhc5499be2008-04-01 15:06:33 +00002859 testcase( regFree1==0 );
drh2dcef112008-01-12 19:03:48 +00002860 exprX.op = TK_REGISTER;
drhc5499be2008-04-01 15:06:33 +00002861 testcase( jumpIfNull==0 );
drh2dcef112008-01-12 19:03:48 +00002862 sqlite3ExprIfTrue(pParse, &exprAnd, dest, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00002863 break;
2864 }
drhcce7d172000-05-31 15:34:51 +00002865 default: {
drh2dcef112008-01-12 19:03:48 +00002866 r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
2867 sqlite3VdbeAddOp3(v, OP_If, r1, dest, jumpIfNull!=0);
drhc5499be2008-04-01 15:06:33 +00002868 testcase( regFree1==0 );
2869 testcase( jumpIfNull==0 );
drhcce7d172000-05-31 15:34:51 +00002870 break;
2871 }
2872 }
drh2dcef112008-01-12 19:03:48 +00002873 sqlite3ReleaseTempReg(pParse, regFree1);
2874 sqlite3ReleaseTempReg(pParse, regFree2);
drhcce7d172000-05-31 15:34:51 +00002875}
2876
2877/*
drh66b89c82000-11-28 20:47:17 +00002878** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00002879** to the label "dest" if the expression is false but execution
2880** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00002881**
2882** If the expression evaluates to NULL (neither true nor false) then
drh35573352008-01-08 23:54:25 +00002883** jump if jumpIfNull is SQLITE_JUMPIFNULL or fall through if jumpIfNull
2884** is 0.
drhcce7d172000-05-31 15:34:51 +00002885*/
danielk19774adee202004-05-08 08:23:19 +00002886void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00002887 Vdbe *v = pParse->pVdbe;
2888 int op = 0;
drh2dcef112008-01-12 19:03:48 +00002889 int regFree1 = 0;
2890 int regFree2 = 0;
2891 int r1, r2;
2892
drh35573352008-01-08 23:54:25 +00002893 assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
drhdaffd0e2001-04-11 14:28:42 +00002894 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00002895
2896 /* The value of pExpr->op and op are related as follows:
2897 **
2898 ** pExpr->op op
2899 ** --------- ----------
2900 ** TK_ISNULL OP_NotNull
2901 ** TK_NOTNULL OP_IsNull
2902 ** TK_NE OP_Eq
2903 ** TK_EQ OP_Ne
2904 ** TK_GT OP_Le
2905 ** TK_LE OP_Gt
2906 ** TK_GE OP_Lt
2907 ** TK_LT OP_Ge
2908 **
2909 ** For other values of pExpr->op, op is undefined and unused.
2910 ** The value of TK_ and OP_ constants are arranged such that we
2911 ** can compute the mapping above using the following expression.
2912 ** Assert()s verify that the computation is correct.
2913 */
2914 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
2915
2916 /* Verify correct alignment of TK_ and OP_ constants
2917 */
2918 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
2919 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
2920 assert( pExpr->op!=TK_NE || op==OP_Eq );
2921 assert( pExpr->op!=TK_EQ || op==OP_Ne );
2922 assert( pExpr->op!=TK_LT || op==OP_Ge );
2923 assert( pExpr->op!=TK_LE || op==OP_Gt );
2924 assert( pExpr->op!=TK_GT || op==OP_Le );
2925 assert( pExpr->op!=TK_GE || op==OP_Lt );
2926
drhcce7d172000-05-31 15:34:51 +00002927 switch( pExpr->op ){
2928 case TK_AND: {
drhc5499be2008-04-01 15:06:33 +00002929 testcase( jumpIfNull==0 );
2930 testcase( pParse->disableColCache==0 );
danielk19774adee202004-05-08 08:23:19 +00002931 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhe55cbd72008-03-31 23:48:03 +00002932 pParse->disableColCache++;
danielk19774adee202004-05-08 08:23:19 +00002933 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhc5499be2008-04-01 15:06:33 +00002934 assert( pParse->disableColCache>0 );
drhe55cbd72008-03-31 23:48:03 +00002935 pParse->disableColCache--;
drhcce7d172000-05-31 15:34:51 +00002936 break;
2937 }
2938 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00002939 int d2 = sqlite3VdbeMakeLabel(v);
drhc5499be2008-04-01 15:06:33 +00002940 testcase( jumpIfNull==0 );
2941 testcase( pParse->disableColCache==0 );
drh35573352008-01-08 23:54:25 +00002942 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, jumpIfNull^SQLITE_JUMPIFNULL);
drhe55cbd72008-03-31 23:48:03 +00002943 pParse->disableColCache++;
danielk19774adee202004-05-08 08:23:19 +00002944 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhc5499be2008-04-01 15:06:33 +00002945 assert( pParse->disableColCache>0 );
drhe55cbd72008-03-31 23:48:03 +00002946 pParse->disableColCache--;
danielk19774adee202004-05-08 08:23:19 +00002947 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00002948 break;
2949 }
2950 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00002951 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002952 break;
2953 }
2954 case TK_LT:
2955 case TK_LE:
2956 case TK_GT:
2957 case TK_GE:
2958 case TK_NE:
2959 case TK_EQ: {
drhc5499be2008-04-01 15:06:33 +00002960 testcase( op==TK_LT );
2961 testcase( op==TK_LE );
2962 testcase( op==TK_GT );
2963 testcase( op==TK_GE );
2964 testcase( op==TK_EQ );
2965 testcase( op==TK_NE );
2966 testcase( jumpIfNull==0 );
drhda250ea2008-04-01 05:07:14 +00002967 codeCompareOperands(pParse, pExpr->pLeft, &r1, &regFree1,
2968 pExpr->pRight, &r2, &regFree2);
drh35573352008-01-08 23:54:25 +00002969 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
drh2dcef112008-01-12 19:03:48 +00002970 r1, r2, dest, jumpIfNull);
drhc5499be2008-04-01 15:06:33 +00002971 testcase( regFree1==0 );
2972 testcase( regFree2==0 );
drhcce7d172000-05-31 15:34:51 +00002973 break;
2974 }
drhcce7d172000-05-31 15:34:51 +00002975 case TK_ISNULL:
2976 case TK_NOTNULL: {
drhc5499be2008-04-01 15:06:33 +00002977 testcase( op==TK_ISNULL );
2978 testcase( op==TK_NOTNULL );
drh2dcef112008-01-12 19:03:48 +00002979 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
2980 sqlite3VdbeAddOp2(v, op, r1, dest);
drhc5499be2008-04-01 15:06:33 +00002981 testcase( regFree1==0 );
drhcce7d172000-05-31 15:34:51 +00002982 break;
2983 }
drhfef52082000-06-06 01:50:43 +00002984 case TK_BETWEEN: {
drh2dcef112008-01-12 19:03:48 +00002985 /* x BETWEEN y AND z
danielk19770202b292004-06-09 09:55:16 +00002986 **
drh2dcef112008-01-12 19:03:48 +00002987 ** Is equivalent to
2988 **
2989 ** x>=y AND x<=z
2990 **
2991 ** Code it as such, taking care to do the common subexpression
2992 ** elementation of x.
danielk19770202b292004-06-09 09:55:16 +00002993 */
drh2dcef112008-01-12 19:03:48 +00002994 Expr exprAnd;
2995 Expr compLeft;
2996 Expr compRight;
2997 Expr exprX;
drhbe5c89a2004-07-26 00:31:09 +00002998
drh2dcef112008-01-12 19:03:48 +00002999 exprX = *pExpr->pLeft;
3000 exprAnd.op = TK_AND;
3001 exprAnd.pLeft = &compLeft;
3002 exprAnd.pRight = &compRight;
3003 compLeft.op = TK_GE;
3004 compLeft.pLeft = &exprX;
3005 compLeft.pRight = pExpr->pList->a[0].pExpr;
3006 compRight.op = TK_LE;
3007 compRight.pLeft = &exprX;
3008 compRight.pRight = pExpr->pList->a[1].pExpr;
3009 exprX.iTable = sqlite3ExprCodeTemp(pParse, &exprX, &regFree1);
drhc5499be2008-04-01 15:06:33 +00003010 testcase( regFree1==0 );
drh2dcef112008-01-12 19:03:48 +00003011 exprX.op = TK_REGISTER;
drhc5499be2008-04-01 15:06:33 +00003012 testcase( jumpIfNull==0 );
drh2dcef112008-01-12 19:03:48 +00003013 sqlite3ExprIfFalse(pParse, &exprAnd, dest, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00003014 break;
3015 }
drhcce7d172000-05-31 15:34:51 +00003016 default: {
drh2dcef112008-01-12 19:03:48 +00003017 r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
3018 sqlite3VdbeAddOp3(v, OP_IfNot, r1, dest, jumpIfNull!=0);
drhc5499be2008-04-01 15:06:33 +00003019 testcase( regFree1==0 );
3020 testcase( jumpIfNull==0 );
drhcce7d172000-05-31 15:34:51 +00003021 break;
3022 }
3023 }
drh2dcef112008-01-12 19:03:48 +00003024 sqlite3ReleaseTempReg(pParse, regFree1);
3025 sqlite3ReleaseTempReg(pParse, regFree2);
drhcce7d172000-05-31 15:34:51 +00003026}
drh22827922000-06-06 17:27:05 +00003027
3028/*
3029** Do a deep comparison of two expression trees. Return TRUE (non-zero)
3030** if they are identical and return FALSE if they differ in any way.
drhd40aab02007-02-24 15:29:03 +00003031**
3032** Sometimes this routine will return FALSE even if the two expressions
3033** really are equivalent. If we cannot prove that the expressions are
3034** identical, we return FALSE just to be safe. So if this routine
3035** returns false, then you do not really know for certain if the two
3036** expressions are the same. But if you get a TRUE return, then you
3037** can be sure the expressions are the same. In the places where
3038** this routine is used, it does not hurt to get an extra FALSE - that
3039** just might result in some slightly slower code. But returning
3040** an incorrect TRUE could lead to a malfunction.
drh22827922000-06-06 17:27:05 +00003041*/
danielk19774adee202004-05-08 08:23:19 +00003042int sqlite3ExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00003043 int i;
danielk19774b202ae2006-01-23 05:50:58 +00003044 if( pA==0||pB==0 ){
3045 return pB==pA;
drh22827922000-06-06 17:27:05 +00003046 }
3047 if( pA->op!=pB->op ) return 0;
drhfd357972005-09-09 01:33:19 +00003048 if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 0;
danielk19774adee202004-05-08 08:23:19 +00003049 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
3050 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00003051 if( pA->pList ){
3052 if( pB->pList==0 ) return 0;
3053 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
3054 for(i=0; i<pA->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00003055 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00003056 return 0;
3057 }
3058 }
3059 }else if( pB->pList ){
3060 return 0;
3061 }
3062 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00003063 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drhdd735212007-02-24 13:53:05 +00003064 if( pA->op!=TK_COLUMN && pA->token.z ){
drh22827922000-06-06 17:27:05 +00003065 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00003066 if( pB->token.n!=pA->token.n ) return 0;
drh2646da72005-12-09 20:02:05 +00003067 if( sqlite3StrNICmp((char*)pA->token.z,(char*)pB->token.z,pB->token.n)!=0 ){
3068 return 0;
3069 }
drh22827922000-06-06 17:27:05 +00003070 }
3071 return 1;
3072}
3073
drh13449892005-09-07 21:22:45 +00003074
drh22827922000-06-06 17:27:05 +00003075/*
drh13449892005-09-07 21:22:45 +00003076** Add a new element to the pAggInfo->aCol[] array. Return the index of
3077** the new element. Return a negative number if malloc fails.
drh22827922000-06-06 17:27:05 +00003078*/
drh17435752007-08-16 04:30:38 +00003079static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){
drh13449892005-09-07 21:22:45 +00003080 int i;
drhcf643722007-03-27 13:36:37 +00003081 pInfo->aCol = sqlite3ArrayAllocate(
drh17435752007-08-16 04:30:38 +00003082 db,
drhcf643722007-03-27 13:36:37 +00003083 pInfo->aCol,
3084 sizeof(pInfo->aCol[0]),
3085 3,
3086 &pInfo->nColumn,
3087 &pInfo->nColumnAlloc,
3088 &i
3089 );
drh13449892005-09-07 21:22:45 +00003090 return i;
3091}
3092
3093/*
3094** Add a new element to the pAggInfo->aFunc[] array. Return the index of
3095** the new element. Return a negative number if malloc fails.
3096*/
drh17435752007-08-16 04:30:38 +00003097static int addAggInfoFunc(sqlite3 *db, AggInfo *pInfo){
drh13449892005-09-07 21:22:45 +00003098 int i;
drhcf643722007-03-27 13:36:37 +00003099 pInfo->aFunc = sqlite3ArrayAllocate(
drh17435752007-08-16 04:30:38 +00003100 db,
drhcf643722007-03-27 13:36:37 +00003101 pInfo->aFunc,
3102 sizeof(pInfo->aFunc[0]),
3103 3,
3104 &pInfo->nFunc,
3105 &pInfo->nFuncAlloc,
3106 &i
3107 );
drh13449892005-09-07 21:22:45 +00003108 return i;
3109}
drh22827922000-06-06 17:27:05 +00003110
3111/*
drh626a8792005-01-17 22:08:19 +00003112** This is an xFunc for walkExprTree() used to implement
3113** sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates
3114** for additional information.
drh22827922000-06-06 17:27:05 +00003115**
drh626a8792005-01-17 22:08:19 +00003116** This routine analyzes the aggregate function at pExpr.
drh22827922000-06-06 17:27:05 +00003117*/
drh626a8792005-01-17 22:08:19 +00003118static int analyzeAggregate(void *pArg, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00003119 int i;
danielk1977a58fdfb2005-02-08 07:50:40 +00003120 NameContext *pNC = (NameContext *)pArg;
3121 Parse *pParse = pNC->pParse;
3122 SrcList *pSrcList = pNC->pSrcList;
drh13449892005-09-07 21:22:45 +00003123 AggInfo *pAggInfo = pNC->pAggInfo;
drh22827922000-06-06 17:27:05 +00003124
drh22827922000-06-06 17:27:05 +00003125 switch( pExpr->op ){
drh89c69d02007-01-04 01:20:28 +00003126 case TK_AGG_COLUMN:
drh967e8b72000-06-21 13:59:10 +00003127 case TK_COLUMN: {
drh13449892005-09-07 21:22:45 +00003128 /* Check to see if the column is in one of the tables in the FROM
3129 ** clause of the aggregate query */
3130 if( pSrcList ){
3131 struct SrcList_item *pItem = pSrcList->a;
3132 for(i=0; i<pSrcList->nSrc; i++, pItem++){
3133 struct AggInfo_col *pCol;
3134 if( pExpr->iTable==pItem->iCursor ){
3135 /* If we reach this point, it means that pExpr refers to a table
3136 ** that is in the FROM clause of the aggregate query.
3137 **
3138 ** Make an entry for the column in pAggInfo->aCol[] if there
3139 ** is not an entry there already.
3140 */
drh7f906d62007-03-12 23:48:52 +00003141 int k;
drh13449892005-09-07 21:22:45 +00003142 pCol = pAggInfo->aCol;
drh7f906d62007-03-12 23:48:52 +00003143 for(k=0; k<pAggInfo->nColumn; k++, pCol++){
drh13449892005-09-07 21:22:45 +00003144 if( pCol->iTable==pExpr->iTable &&
3145 pCol->iColumn==pExpr->iColumn ){
3146 break;
3147 }
danielk1977a58fdfb2005-02-08 07:50:40 +00003148 }
danielk19771e536952007-08-16 10:09:01 +00003149 if( (k>=pAggInfo->nColumn)
3150 && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0
3151 ){
drh7f906d62007-03-12 23:48:52 +00003152 pCol = &pAggInfo->aCol[k];
danielk19770817d0d2007-02-14 09:19:36 +00003153 pCol->pTab = pExpr->pTab;
drh13449892005-09-07 21:22:45 +00003154 pCol->iTable = pExpr->iTable;
3155 pCol->iColumn = pExpr->iColumn;
drh0a07c102008-01-03 18:03:08 +00003156 pCol->iMem = ++pParse->nMem;
drh13449892005-09-07 21:22:45 +00003157 pCol->iSorterColumn = -1;
drh5774b802005-09-07 22:48:16 +00003158 pCol->pExpr = pExpr;
drh13449892005-09-07 21:22:45 +00003159 if( pAggInfo->pGroupBy ){
3160 int j, n;
3161 ExprList *pGB = pAggInfo->pGroupBy;
3162 struct ExprList_item *pTerm = pGB->a;
3163 n = pGB->nExpr;
3164 for(j=0; j<n; j++, pTerm++){
3165 Expr *pE = pTerm->pExpr;
3166 if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable &&
3167 pE->iColumn==pExpr->iColumn ){
3168 pCol->iSorterColumn = j;
3169 break;
3170 }
3171 }
3172 }
3173 if( pCol->iSorterColumn<0 ){
3174 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
3175 }
3176 }
3177 /* There is now an entry for pExpr in pAggInfo->aCol[] (either
3178 ** because it was there before or because we just created it).
3179 ** Convert the pExpr to be a TK_AGG_COLUMN referring to that
3180 ** pAggInfo->aCol[] entry.
3181 */
3182 pExpr->pAggInfo = pAggInfo;
3183 pExpr->op = TK_AGG_COLUMN;
drh7f906d62007-03-12 23:48:52 +00003184 pExpr->iAgg = k;
drh13449892005-09-07 21:22:45 +00003185 break;
3186 } /* endif pExpr->iTable==pItem->iCursor */
3187 } /* end loop over pSrcList */
drh22827922000-06-06 17:27:05 +00003188 }
drh626a8792005-01-17 22:08:19 +00003189 return 1;
drh22827922000-06-06 17:27:05 +00003190 }
3191 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00003192 /* The pNC->nDepth==0 test causes aggregate functions in subqueries
3193 ** to be ignored */
danielk1977a58fdfb2005-02-08 07:50:40 +00003194 if( pNC->nDepth==0 ){
drh13449892005-09-07 21:22:45 +00003195 /* Check to see if pExpr is a duplicate of another aggregate
3196 ** function that is already in the pAggInfo structure
3197 */
3198 struct AggInfo_func *pItem = pAggInfo->aFunc;
3199 for(i=0; i<pAggInfo->nFunc; i++, pItem++){
3200 if( sqlite3ExprCompare(pItem->pExpr, pExpr) ){
danielk1977a58fdfb2005-02-08 07:50:40 +00003201 break;
3202 }
drh22827922000-06-06 17:27:05 +00003203 }
drh13449892005-09-07 21:22:45 +00003204 if( i>=pAggInfo->nFunc ){
3205 /* pExpr is original. Make a new entry in pAggInfo->aFunc[]
3206 */
danielk197714db2662006-01-09 16:12:04 +00003207 u8 enc = ENC(pParse->db);
danielk19771e536952007-08-16 10:09:01 +00003208 i = addAggInfoFunc(pParse->db, pAggInfo);
drh13449892005-09-07 21:22:45 +00003209 if( i>=0 ){
3210 pItem = &pAggInfo->aFunc[i];
3211 pItem->pExpr = pExpr;
drh0a07c102008-01-03 18:03:08 +00003212 pItem->iMem = ++pParse->nMem;
drh13449892005-09-07 21:22:45 +00003213 pItem->pFunc = sqlite3FindFunction(pParse->db,
drh2646da72005-12-09 20:02:05 +00003214 (char*)pExpr->token.z, pExpr->token.n,
drh13449892005-09-07 21:22:45 +00003215 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
drhfd357972005-09-09 01:33:19 +00003216 if( pExpr->flags & EP_Distinct ){
3217 pItem->iDistinct = pParse->nTab++;
3218 }else{
3219 pItem->iDistinct = -1;
3220 }
drh13449892005-09-07 21:22:45 +00003221 }
danielk1977a58fdfb2005-02-08 07:50:40 +00003222 }
drh13449892005-09-07 21:22:45 +00003223 /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
3224 */
danielk1977a58fdfb2005-02-08 07:50:40 +00003225 pExpr->iAgg = i;
drh13449892005-09-07 21:22:45 +00003226 pExpr->pAggInfo = pAggInfo;
danielk1977a58fdfb2005-02-08 07:50:40 +00003227 return 1;
drh22827922000-06-06 17:27:05 +00003228 }
drh22827922000-06-06 17:27:05 +00003229 }
3230 }
drh13449892005-09-07 21:22:45 +00003231
3232 /* Recursively walk subqueries looking for TK_COLUMN nodes that need
3233 ** to be changed to TK_AGG_COLUMN. But increment nDepth so that
3234 ** TK_AGG_FUNCTION nodes in subqueries will be unchanged.
3235 */
danielk1977a58fdfb2005-02-08 07:50:40 +00003236 if( pExpr->pSelect ){
3237 pNC->nDepth++;
3238 walkSelectExpr(pExpr->pSelect, analyzeAggregate, pNC);
3239 pNC->nDepth--;
3240 }
drh626a8792005-01-17 22:08:19 +00003241 return 0;
3242}
3243
3244/*
3245** Analyze the given expression looking for aggregate functions and
3246** for variables that need to be added to the pParse->aAgg[] array.
3247** Make additional entries to the pParse->aAgg[] array as necessary.
3248**
3249** This routine should only be called after the expression has been
3250** analyzed by sqlite3ExprResolveNames().
drh626a8792005-01-17 22:08:19 +00003251*/
drhd2b3e232008-01-23 14:51:49 +00003252void sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
danielk1977a58fdfb2005-02-08 07:50:40 +00003253 walkExprTree(pExpr, analyzeAggregate, pNC);
drh22827922000-06-06 17:27:05 +00003254}
drh5d9a4af2005-08-30 00:54:01 +00003255
3256/*
3257** Call sqlite3ExprAnalyzeAggregates() for every expression in an
3258** expression list. Return the number of errors.
3259**
3260** If an error is found, the analysis is cut short.
3261*/
drhd2b3e232008-01-23 14:51:49 +00003262void sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){
drh5d9a4af2005-08-30 00:54:01 +00003263 struct ExprList_item *pItem;
3264 int i;
drh5d9a4af2005-08-30 00:54:01 +00003265 if( pList ){
drhd2b3e232008-01-23 14:51:49 +00003266 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
3267 sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
drh5d9a4af2005-08-30 00:54:01 +00003268 }
3269 }
drh5d9a4af2005-08-30 00:54:01 +00003270}
drh892d3172008-01-10 03:46:36 +00003271
3272/*
3273** Allocate or deallocate temporary use registers during code generation.
3274*/
3275int sqlite3GetTempReg(Parse *pParse){
drhe55cbd72008-03-31 23:48:03 +00003276 int i, r;
3277 if( pParse->nTempReg==0 ){
drh892d3172008-01-10 03:46:36 +00003278 return ++pParse->nMem;
3279 }
drhe55cbd72008-03-31 23:48:03 +00003280 for(i=0; i<pParse->nTempReg; i++){
3281 r = pParse->aTempReg[i];
3282 if( usedAsColumnCache(pParse, r, r) ) continue;
3283 }
3284 if( i>=pParse->nTempReg ){
3285 return ++pParse->nMem;
3286 }
3287 while( i<pParse->nTempReg-1 ){
3288 pParse->aTempReg[i] = pParse->aTempReg[i+1];
3289 }
3290 pParse->nTempReg--;
3291 return r;
drh892d3172008-01-10 03:46:36 +00003292}
3293void sqlite3ReleaseTempReg(Parse *pParse, int iReg){
drh2dcef112008-01-12 19:03:48 +00003294 if( iReg && pParse->nTempReg<ArraySize(pParse->aTempReg) ){
drh892d3172008-01-10 03:46:36 +00003295 pParse->aTempReg[pParse->nTempReg++] = iReg;
3296 }
3297}
3298
3299/*
3300** Allocate or deallocate a block of nReg consecutive registers
3301*/
3302int sqlite3GetTempRange(Parse *pParse, int nReg){
drhe55cbd72008-03-31 23:48:03 +00003303 int i, n;
3304 i = pParse->iRangeReg;
3305 n = pParse->nRangeReg;
3306 if( nReg<=n && !usedAsColumnCache(pParse, i, i+n-1) ){
drh892d3172008-01-10 03:46:36 +00003307 pParse->iRangeReg += nReg;
3308 pParse->nRangeReg -= nReg;
3309 }else{
3310 i = pParse->nMem+1;
3311 pParse->nMem += nReg;
3312 }
3313 return i;
3314}
3315void sqlite3ReleaseTempRange(Parse *pParse, int iReg, int nReg){
3316 if( nReg>pParse->nRangeReg ){
3317 pParse->nRangeReg = nReg;
3318 pParse->iRangeReg = iReg;
3319 }
3320}