blob: e0c5ecfbcb79d2e092a89884e8e95ba769c6323d [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**
drh2eaf93d2008-04-29 00:15:20 +000015** $Id: expr.c,v 1.370 2008/04/29 00:15:21 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;
drh5c070532008-04-11 15:36:03 +0000269 static const Expr zeroExpr;
270 pNew = sqlite3DbMallocRaw(db, sizeof(Expr));
drha76b5df2002-02-23 02:32:10 +0000271 if( pNew==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000272 /* When malloc fails, delete pLeft and pRight. Expressions passed to
273 ** this function must always be allocated with sqlite3Expr() for this
274 ** reason.
275 */
276 sqlite3ExprDelete(pLeft);
277 sqlite3ExprDelete(pRight);
drha76b5df2002-02-23 02:32:10 +0000278 return 0;
279 }
drh5c070532008-04-11 15:36:03 +0000280 *pNew = zeroExpr;
drha76b5df2002-02-23 02:32:10 +0000281 pNew->op = op;
282 pNew->pLeft = pLeft;
283 pNew->pRight = pRight;
danielk1977a58fdfb2005-02-08 07:50:40 +0000284 pNew->iAgg = -1;
drha76b5df2002-02-23 02:32:10 +0000285 if( pToken ){
drh4b59ab52002-08-24 18:24:51 +0000286 assert( pToken->dyn==0 );
drh145716b2004-09-24 12:24:06 +0000287 pNew->span = pNew->token = *pToken;
drha34001c2007-02-02 12:44:37 +0000288 }else if( pLeft ){
289 if( pRight ){
290 sqlite3ExprSpan(pNew, &pLeft->span, &pRight->span);
drh5ffb3ac2007-04-18 17:07:57 +0000291 if( pRight->flags & EP_ExpCollate ){
drha34001c2007-02-02 12:44:37 +0000292 pNew->flags |= EP_ExpCollate;
293 pNew->pColl = pRight->pColl;
294 }
295 }
drh5ffb3ac2007-04-18 17:07:57 +0000296 if( pLeft->flags & EP_ExpCollate ){
drha34001c2007-02-02 12:44:37 +0000297 pNew->flags |= EP_ExpCollate;
298 pNew->pColl = pLeft->pColl;
299 }
drha76b5df2002-02-23 02:32:10 +0000300 }
danielk1977fc976062007-05-10 10:46:56 +0000301
302 sqlite3ExprSetHeight(pNew);
drha76b5df2002-02-23 02:32:10 +0000303 return pNew;
304}
305
306/*
drh17435752007-08-16 04:30:38 +0000307** Works like sqlite3Expr() except that it takes an extra Parse*
308** argument and notifies the associated connection object if malloc fails.
drh206f3d92006-07-11 13:15:08 +0000309*/
drh17435752007-08-16 04:30:38 +0000310Expr *sqlite3PExpr(
311 Parse *pParse, /* Parsing context */
312 int op, /* Expression opcode */
313 Expr *pLeft, /* Left operand */
314 Expr *pRight, /* Right operand */
315 const Token *pToken /* Argument token */
316){
danielk1977a1644fd2007-08-29 12:31:25 +0000317 return sqlite3Expr(pParse->db, op, pLeft, pRight, pToken);
drh206f3d92006-07-11 13:15:08 +0000318}
319
320/*
drh4e0cff62004-11-05 05:10:28 +0000321** When doing a nested parse, you can include terms in an expression
drhb7654112008-01-12 12:48:07 +0000322** that look like this: #1 #2 ... These terms refer to registers
323** in the virtual machine. #N is the N-th register.
drh4e0cff62004-11-05 05:10:28 +0000324**
325** This routine is called by the parser to deal with on of those terms.
326** It immediately generates code to store the value in a memory location.
327** The returns an expression that will code to extract the value from
328** that memory location as needed.
329*/
330Expr *sqlite3RegisterExpr(Parse *pParse, Token *pToken){
331 Vdbe *v = pParse->pVdbe;
332 Expr *p;
drh4e0cff62004-11-05 05:10:28 +0000333 if( pParse->nested==0 ){
334 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", pToken);
danielk1977a1644fd2007-08-29 12:31:25 +0000335 return sqlite3PExpr(pParse, TK_NULL, 0, 0, 0);
drh4e0cff62004-11-05 05:10:28 +0000336 }
drhbb7ac002005-08-12 22:58:53 +0000337 if( v==0 ) return 0;
danielk1977a1644fd2007-08-29 12:31:25 +0000338 p = sqlite3PExpr(pParse, TK_REGISTER, 0, 0, pToken);
drh73c42a12004-11-20 18:13:10 +0000339 if( p==0 ){
340 return 0; /* Malloc failed */
341 }
drhb7654112008-01-12 12:48:07 +0000342 p->iTable = atoi((char*)&pToken->z[1]);
drh4e0cff62004-11-05 05:10:28 +0000343 return p;
344}
345
346/*
drh91bb0ee2004-09-01 03:06:34 +0000347** Join two expressions using an AND operator. If either expression is
348** NULL, then just return the other expression.
349*/
danielk19771e536952007-08-16 10:09:01 +0000350Expr *sqlite3ExprAnd(sqlite3 *db, Expr *pLeft, Expr *pRight){
drh91bb0ee2004-09-01 03:06:34 +0000351 if( pLeft==0 ){
352 return pRight;
353 }else if( pRight==0 ){
354 return pLeft;
355 }else{
danielk1977880c15b2007-09-01 18:24:55 +0000356 return sqlite3Expr(db, TK_AND, pLeft, pRight, 0);
drh91bb0ee2004-09-01 03:06:34 +0000357 }
358}
359
360/*
drh6977fea2002-10-22 23:38:04 +0000361** Set the Expr.span field of the given expression to span all
drha76b5df2002-02-23 02:32:10 +0000362** text between the two given tokens.
363*/
danielk19774adee202004-05-08 08:23:19 +0000364void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
drh4efc4752004-01-16 15:55:37 +0000365 assert( pRight!=0 );
366 assert( pLeft!=0 );
drhf3a65f72007-08-22 20:18:21 +0000367 if( pExpr && pRight->z && pLeft->z ){
drhad6d9462004-09-19 02:15:24 +0000368 assert( pLeft->dyn==0 || pLeft->z[pLeft->n]==0 );
drh145716b2004-09-24 12:24:06 +0000369 if( pLeft->dyn==0 && pRight->dyn==0 ){
drh6977fea2002-10-22 23:38:04 +0000370 pExpr->span.z = pLeft->z;
drh97903fe2005-05-24 20:19:57 +0000371 pExpr->span.n = pRight->n + (pRight->z - pLeft->z);
drh4b59ab52002-08-24 18:24:51 +0000372 }else{
drh6977fea2002-10-22 23:38:04 +0000373 pExpr->span.z = 0;
drh4b59ab52002-08-24 18:24:51 +0000374 }
drha76b5df2002-02-23 02:32:10 +0000375 }
376}
377
378/*
379** Construct a new expression node for a function with multiple
380** arguments.
381*/
drh17435752007-08-16 04:30:38 +0000382Expr *sqlite3ExprFunction(Parse *pParse, ExprList *pList, Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000383 Expr *pNew;
danielk19774b202ae2006-01-23 05:50:58 +0000384 assert( pToken );
drh17435752007-08-16 04:30:38 +0000385 pNew = sqlite3DbMallocZero(pParse->db, sizeof(Expr) );
drha76b5df2002-02-23 02:32:10 +0000386 if( pNew==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000387 sqlite3ExprListDelete(pList); /* Avoid leaking memory when malloc fails */
drha76b5df2002-02-23 02:32:10 +0000388 return 0;
389 }
390 pNew->op = TK_FUNCTION;
391 pNew->pList = pList;
danielk19774b202ae2006-01-23 05:50:58 +0000392 assert( pToken->dyn==0 );
393 pNew->token = *pToken;
drh6977fea2002-10-22 23:38:04 +0000394 pNew->span = pNew->token;
danielk1977fc976062007-05-10 10:46:56 +0000395
396 sqlite3ExprSetHeight(pNew);
drha76b5df2002-02-23 02:32:10 +0000397 return pNew;
398}
399
400/*
drhfa6bc002004-09-07 16:19:52 +0000401** Assign a variable number to an expression that encodes a wildcard
402** in the original SQL statement.
403**
404** Wildcards consisting of a single "?" are assigned the next sequential
405** variable number.
406**
407** Wildcards of the form "?nnn" are assigned the number "nnn". We make
408** sure "nnn" is not too be to avoid a denial of service attack when
409** the SQL statement comes from an external source.
410**
411** Wildcards of the form ":aaa" or "$aaa" are assigned the same number
412** as the previous instance of the same wildcard. Or if this is the first
413** instance of the wildcard, the next sequenial variable number is
414** assigned.
415*/
416void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){
417 Token *pToken;
drh17435752007-08-16 04:30:38 +0000418 sqlite3 *db = pParse->db;
419
drhfa6bc002004-09-07 16:19:52 +0000420 if( pExpr==0 ) return;
421 pToken = &pExpr->token;
422 assert( pToken->n>=1 );
423 assert( pToken->z!=0 );
424 assert( pToken->z[0]!=0 );
425 if( pToken->n==1 ){
426 /* Wildcard of the form "?". Assign the next variable number */
427 pExpr->iTable = ++pParse->nVar;
428 }else if( pToken->z[0]=='?' ){
429 /* Wildcard of the form "?nnn". Convert "nnn" to an integer and
430 ** use it as the variable number */
431 int i;
drh2646da72005-12-09 20:02:05 +0000432 pExpr->iTable = i = atoi((char*)&pToken->z[1]);
drhc5499be2008-04-01 15:06:33 +0000433 testcase( i==0 );
434 testcase( i==1 );
435 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 );
436 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] );
drhbb4957f2008-03-20 14:03:29 +0000437 if( i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
drhfa6bc002004-09-07 16:19:52 +0000438 sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
drhbb4957f2008-03-20 14:03:29 +0000439 db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]);
drhfa6bc002004-09-07 16:19:52 +0000440 }
441 if( i>pParse->nVar ){
442 pParse->nVar = i;
443 }
444 }else{
445 /* Wildcards of the form ":aaa" or "$aaa". Reuse the same variable
446 ** number as the prior appearance of the same name, or if the name
447 ** has never appeared before, reuse the same variable number
448 */
449 int i, n;
450 n = pToken->n;
451 for(i=0; i<pParse->nVarExpr; i++){
452 Expr *pE;
453 if( (pE = pParse->apVarExpr[i])!=0
454 && pE->token.n==n
455 && memcmp(pE->token.z, pToken->z, n)==0 ){
456 pExpr->iTable = pE->iTable;
457 break;
458 }
459 }
460 if( i>=pParse->nVarExpr ){
461 pExpr->iTable = ++pParse->nVar;
462 if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){
463 pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10;
drh17435752007-08-16 04:30:38 +0000464 pParse->apVarExpr =
465 sqlite3DbReallocOrFree(
466 db,
467 pParse->apVarExpr,
468 pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0])
469 );
drhfa6bc002004-09-07 16:19:52 +0000470 }
drh17435752007-08-16 04:30:38 +0000471 if( !db->mallocFailed ){
drhfa6bc002004-09-07 16:19:52 +0000472 assert( pParse->apVarExpr!=0 );
473 pParse->apVarExpr[pParse->nVarExpr++] = pExpr;
474 }
475 }
476 }
drhbb4957f2008-03-20 14:03:29 +0000477 if( !pParse->nErr && pParse->nVar>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
danielk1977832b2662007-05-09 11:37:22 +0000478 sqlite3ErrorMsg(pParse, "too many SQL variables");
479 }
drhfa6bc002004-09-07 16:19:52 +0000480}
481
482/*
drha2e00042002-01-22 03:13:42 +0000483** Recursively delete an expression tree.
484*/
danielk19774adee202004-05-08 08:23:19 +0000485void sqlite3ExprDelete(Expr *p){
drha2e00042002-01-22 03:13:42 +0000486 if( p==0 ) return;
drh17435752007-08-16 04:30:38 +0000487 if( p->span.dyn ) sqlite3_free((char*)p->span.z);
488 if( p->token.dyn ) sqlite3_free((char*)p->token.z);
danielk19774adee202004-05-08 08:23:19 +0000489 sqlite3ExprDelete(p->pLeft);
490 sqlite3ExprDelete(p->pRight);
491 sqlite3ExprListDelete(p->pList);
492 sqlite3SelectDelete(p->pSelect);
drh17435752007-08-16 04:30:38 +0000493 sqlite3_free(p);
drha2e00042002-01-22 03:13:42 +0000494}
495
drhd2687b72005-08-12 22:56:09 +0000496/*
497** The Expr.token field might be a string literal that is quoted.
498** If so, remove the quotation marks.
499*/
drh17435752007-08-16 04:30:38 +0000500void sqlite3DequoteExpr(sqlite3 *db, Expr *p){
drhd2687b72005-08-12 22:56:09 +0000501 if( ExprHasAnyProperty(p, EP_Dequoted) ){
502 return;
503 }
504 ExprSetProperty(p, EP_Dequoted);
505 if( p->token.dyn==0 ){
drh17435752007-08-16 04:30:38 +0000506 sqlite3TokenCopy(db, &p->token, &p->token);
drhd2687b72005-08-12 22:56:09 +0000507 }
508 sqlite3Dequote((char*)p->token.z);
509}
510
drha76b5df2002-02-23 02:32:10 +0000511
512/*
drhff78bd22002-02-27 01:47:11 +0000513** The following group of routines make deep copies of expressions,
514** expression lists, ID lists, and select statements. The copies can
515** be deleted (by being passed to their respective ...Delete() routines)
516** without effecting the originals.
517**
danielk19774adee202004-05-08 08:23:19 +0000518** The expression list, ID, and source lists return by sqlite3ExprListDup(),
519** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
drhad3cab52002-05-24 02:04:32 +0000520** by subsequent calls to sqlite*ListAppend() routines.
drhff78bd22002-02-27 01:47:11 +0000521**
drhad3cab52002-05-24 02:04:32 +0000522** Any tables that the SrcList might point to are not duplicated.
drhff78bd22002-02-27 01:47:11 +0000523*/
danielk19771e536952007-08-16 10:09:01 +0000524Expr *sqlite3ExprDup(sqlite3 *db, Expr *p){
drhff78bd22002-02-27 01:47:11 +0000525 Expr *pNew;
526 if( p==0 ) return 0;
drh17435752007-08-16 04:30:38 +0000527 pNew = sqlite3DbMallocRaw(db, sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000528 if( pNew==0 ) return 0;
drh3b167c72002-06-28 12:18:47 +0000529 memcpy(pNew, p, sizeof(*pNew));
drh6977fea2002-10-22 23:38:04 +0000530 if( p->token.z!=0 ){
drh17435752007-08-16 04:30:38 +0000531 pNew->token.z = (u8*)sqlite3DbStrNDup(db, (char*)p->token.z, p->token.n);
drh4b59ab52002-08-24 18:24:51 +0000532 pNew->token.dyn = 1;
533 }else{
drh4efc4752004-01-16 15:55:37 +0000534 assert( pNew->token.z==0 );
drh4b59ab52002-08-24 18:24:51 +0000535 }
drh6977fea2002-10-22 23:38:04 +0000536 pNew->span.z = 0;
drh17435752007-08-16 04:30:38 +0000537 pNew->pLeft = sqlite3ExprDup(db, p->pLeft);
538 pNew->pRight = sqlite3ExprDup(db, p->pRight);
539 pNew->pList = sqlite3ExprListDup(db, p->pList);
540 pNew->pSelect = sqlite3SelectDup(db, p->pSelect);
drhff78bd22002-02-27 01:47:11 +0000541 return pNew;
542}
drh17435752007-08-16 04:30:38 +0000543void sqlite3TokenCopy(sqlite3 *db, Token *pTo, Token *pFrom){
544 if( pTo->dyn ) sqlite3_free((char*)pTo->z);
drh4b59ab52002-08-24 18:24:51 +0000545 if( pFrom->z ){
546 pTo->n = pFrom->n;
drh17435752007-08-16 04:30:38 +0000547 pTo->z = (u8*)sqlite3DbStrNDup(db, (char*)pFrom->z, pFrom->n);
drh4b59ab52002-08-24 18:24:51 +0000548 pTo->dyn = 1;
549 }else{
drh4b59ab52002-08-24 18:24:51 +0000550 pTo->z = 0;
drh4b59ab52002-08-24 18:24:51 +0000551 }
552}
drh17435752007-08-16 04:30:38 +0000553ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p){
drhff78bd22002-02-27 01:47:11 +0000554 ExprList *pNew;
drh145716b2004-09-24 12:24:06 +0000555 struct ExprList_item *pItem, *pOldItem;
drhff78bd22002-02-27 01:47:11 +0000556 int i;
557 if( p==0 ) return 0;
drh17435752007-08-16 04:30:38 +0000558 pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +0000559 if( pNew==0 ) return 0;
danielk197731dad9d2007-08-16 11:36:15 +0000560 pNew->iECursor = 0;
drh4305d102003-07-30 12:34:12 +0000561 pNew->nExpr = pNew->nAlloc = p->nExpr;
drh17435752007-08-16 04:30:38 +0000562 pNew->a = pItem = sqlite3DbMallocRaw(db, p->nExpr*sizeof(p->a[0]) );
danielk1977e0048402004-06-15 16:51:01 +0000563 if( pItem==0 ){
drh17435752007-08-16 04:30:38 +0000564 sqlite3_free(pNew);
danielk1977e0048402004-06-15 16:51:01 +0000565 return 0;
566 }
drh145716b2004-09-24 12:24:06 +0000567 pOldItem = p->a;
568 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
drh4b59ab52002-08-24 18:24:51 +0000569 Expr *pNewExpr, *pOldExpr;
drh17435752007-08-16 04:30:38 +0000570 pItem->pExpr = pNewExpr = sqlite3ExprDup(db, pOldExpr = pOldItem->pExpr);
drh6977fea2002-10-22 23:38:04 +0000571 if( pOldExpr->span.z!=0 && pNewExpr ){
572 /* Always make a copy of the span for top-level expressions in the
drh4b59ab52002-08-24 18:24:51 +0000573 ** expression list. The logic in SELECT processing that determines
574 ** the names of columns in the result set needs this information */
drh17435752007-08-16 04:30:38 +0000575 sqlite3TokenCopy(db, &pNewExpr->span, &pOldExpr->span);
drh4b59ab52002-08-24 18:24:51 +0000576 }
drh1f3e9052002-10-31 00:09:39 +0000577 assert( pNewExpr==0 || pNewExpr->span.z!=0
drh6f7adc82006-01-11 21:41:20 +0000578 || pOldExpr->span.z==0
drh17435752007-08-16 04:30:38 +0000579 || db->mallocFailed );
580 pItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
drh145716b2004-09-24 12:24:06 +0000581 pItem->sortOrder = pOldItem->sortOrder;
582 pItem->isAgg = pOldItem->isAgg;
drh3e7bc9c2004-02-21 19:17:17 +0000583 pItem->done = 0;
drhff78bd22002-02-27 01:47:11 +0000584 }
585 return pNew;
586}
danielk197793758c82005-01-21 08:13:14 +0000587
588/*
589** If cursors, triggers, views and subqueries are all omitted from
590** the build, then none of the following routines, except for
591** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes
592** called with a NULL argument.
593*/
danielk19776a67fe82005-02-04 04:07:16 +0000594#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \
595 || !defined(SQLITE_OMIT_SUBQUERY)
drh17435752007-08-16 04:30:38 +0000596SrcList *sqlite3SrcListDup(sqlite3 *db, SrcList *p){
drhad3cab52002-05-24 02:04:32 +0000597 SrcList *pNew;
598 int i;
drh113088e2003-03-20 01:16:58 +0000599 int nByte;
drhad3cab52002-05-24 02:04:32 +0000600 if( p==0 ) return 0;
drh113088e2003-03-20 01:16:58 +0000601 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
drh17435752007-08-16 04:30:38 +0000602 pNew = sqlite3DbMallocRaw(db, nByte );
drhad3cab52002-05-24 02:04:32 +0000603 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000604 pNew->nSrc = pNew->nAlloc = p->nSrc;
drhad3cab52002-05-24 02:04:32 +0000605 for(i=0; i<p->nSrc; i++){
drh4efc4752004-01-16 15:55:37 +0000606 struct SrcList_item *pNewItem = &pNew->a[i];
607 struct SrcList_item *pOldItem = &p->a[i];
drhed8a3bb2005-06-06 21:19:56 +0000608 Table *pTab;
drh17435752007-08-16 04:30:38 +0000609 pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase);
610 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
611 pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias);
drh4efc4752004-01-16 15:55:37 +0000612 pNewItem->jointype = pOldItem->jointype;
613 pNewItem->iCursor = pOldItem->iCursor;
danielk19771787cca2006-02-10 07:07:14 +0000614 pNewItem->isPopulated = pOldItem->isPopulated;
drhed8a3bb2005-06-06 21:19:56 +0000615 pTab = pNewItem->pTab = pOldItem->pTab;
616 if( pTab ){
617 pTab->nRef++;
danielk1977a1cb1832005-02-12 08:59:55 +0000618 }
drh17435752007-08-16 04:30:38 +0000619 pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect);
620 pNewItem->pOn = sqlite3ExprDup(db, pOldItem->pOn);
621 pNewItem->pUsing = sqlite3IdListDup(db, pOldItem->pUsing);
danielk19776c18b6e2005-01-30 09:17:58 +0000622 pNewItem->colUsed = pOldItem->colUsed;
drhad3cab52002-05-24 02:04:32 +0000623 }
624 return pNew;
625}
drh17435752007-08-16 04:30:38 +0000626IdList *sqlite3IdListDup(sqlite3 *db, IdList *p){
drhff78bd22002-02-27 01:47:11 +0000627 IdList *pNew;
628 int i;
629 if( p==0 ) return 0;
drh17435752007-08-16 04:30:38 +0000630 pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +0000631 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000632 pNew->nId = pNew->nAlloc = p->nId;
drh17435752007-08-16 04:30:38 +0000633 pNew->a = sqlite3DbMallocRaw(db, p->nId*sizeof(p->a[0]) );
danielk1977d5d56522005-03-16 12:15:20 +0000634 if( pNew->a==0 ){
drh17435752007-08-16 04:30:38 +0000635 sqlite3_free(pNew);
danielk1977d5d56522005-03-16 12:15:20 +0000636 return 0;
637 }
drhff78bd22002-02-27 01:47:11 +0000638 for(i=0; i<p->nId; i++){
drh4efc4752004-01-16 15:55:37 +0000639 struct IdList_item *pNewItem = &pNew->a[i];
640 struct IdList_item *pOldItem = &p->a[i];
drh17435752007-08-16 04:30:38 +0000641 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
drh4efc4752004-01-16 15:55:37 +0000642 pNewItem->idx = pOldItem->idx;
drhff78bd22002-02-27 01:47:11 +0000643 }
644 return pNew;
645}
drh17435752007-08-16 04:30:38 +0000646Select *sqlite3SelectDup(sqlite3 *db, Select *p){
drhff78bd22002-02-27 01:47:11 +0000647 Select *pNew;
648 if( p==0 ) return 0;
drh17435752007-08-16 04:30:38 +0000649 pNew = sqlite3DbMallocRaw(db, sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000650 if( pNew==0 ) return 0;
651 pNew->isDistinct = p->isDistinct;
drh17435752007-08-16 04:30:38 +0000652 pNew->pEList = sqlite3ExprListDup(db, p->pEList);
653 pNew->pSrc = sqlite3SrcListDup(db, p->pSrc);
654 pNew->pWhere = sqlite3ExprDup(db, p->pWhere);
655 pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy);
656 pNew->pHaving = sqlite3ExprDup(db, p->pHaving);
657 pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy);
drhff78bd22002-02-27 01:47:11 +0000658 pNew->op = p->op;
drh17435752007-08-16 04:30:38 +0000659 pNew->pPrior = sqlite3SelectDup(db, p->pPrior);
660 pNew->pLimit = sqlite3ExprDup(db, p->pLimit);
661 pNew->pOffset = sqlite3ExprDup(db, p->pOffset);
drh7b58dae2003-07-20 01:16:46 +0000662 pNew->iLimit = -1;
663 pNew->iOffset = -1;
danielk1977a1cb1832005-02-12 08:59:55 +0000664 pNew->isResolved = p->isResolved;
665 pNew->isAgg = p->isAgg;
drhb9bb7c12006-06-11 23:41:55 +0000666 pNew->usesEphm = 0;
drh8e647b82005-09-23 21:11:53 +0000667 pNew->disallowOrderBy = 0;
drh0342b1f2005-09-01 03:07:44 +0000668 pNew->pRightmost = 0;
drhb9bb7c12006-06-11 23:41:55 +0000669 pNew->addrOpenEphm[0] = -1;
670 pNew->addrOpenEphm[1] = -1;
671 pNew->addrOpenEphm[2] = -1;
drhff78bd22002-02-27 01:47:11 +0000672 return pNew;
673}
danielk197793758c82005-01-21 08:13:14 +0000674#else
drh17435752007-08-16 04:30:38 +0000675Select *sqlite3SelectDup(sqlite3 *db, Select *p){
danielk197793758c82005-01-21 08:13:14 +0000676 assert( p==0 );
677 return 0;
678}
679#endif
drhff78bd22002-02-27 01:47:11 +0000680
681
682/*
drha76b5df2002-02-23 02:32:10 +0000683** Add a new element to the end of an expression list. If pList is
684** initially NULL, then create a new expression list.
685*/
drh17435752007-08-16 04:30:38 +0000686ExprList *sqlite3ExprListAppend(
687 Parse *pParse, /* Parsing context */
688 ExprList *pList, /* List to which to append. Might be NULL */
689 Expr *pExpr, /* Expression to be appended */
690 Token *pName /* AS keyword for the expression */
691){
692 sqlite3 *db = pParse->db;
drha76b5df2002-02-23 02:32:10 +0000693 if( pList==0 ){
drh17435752007-08-16 04:30:38 +0000694 pList = sqlite3DbMallocZero(db, sizeof(ExprList) );
drha76b5df2002-02-23 02:32:10 +0000695 if( pList==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000696 goto no_mem;
drha76b5df2002-02-23 02:32:10 +0000697 }
drh4efc4752004-01-16 15:55:37 +0000698 assert( pList->nAlloc==0 );
drha76b5df2002-02-23 02:32:10 +0000699 }
drh4305d102003-07-30 12:34:12 +0000700 if( pList->nAlloc<=pList->nExpr ){
danielk1977d5d56522005-03-16 12:15:20 +0000701 struct ExprList_item *a;
702 int n = pList->nAlloc*2 + 4;
danielk197726783a52007-08-29 14:06:22 +0000703 a = sqlite3DbRealloc(db, pList->a, n*sizeof(pList->a[0]));
danielk1977d5d56522005-03-16 12:15:20 +0000704 if( a==0 ){
705 goto no_mem;
drha76b5df2002-02-23 02:32:10 +0000706 }
danielk1977d5d56522005-03-16 12:15:20 +0000707 pList->a = a;
708 pList->nAlloc = n;
drha76b5df2002-02-23 02:32:10 +0000709 }
drh4efc4752004-01-16 15:55:37 +0000710 assert( pList->a!=0 );
711 if( pExpr || pName ){
712 struct ExprList_item *pItem = &pList->a[pList->nExpr++];
713 memset(pItem, 0, sizeof(*pItem));
drh17435752007-08-16 04:30:38 +0000714 pItem->zName = sqlite3NameFromToken(db, pName);
danielk1977e94ddc92005-03-21 03:53:38 +0000715 pItem->pExpr = pExpr;
drha76b5df2002-02-23 02:32:10 +0000716 }
717 return pList;
danielk1977d5d56522005-03-16 12:15:20 +0000718
719no_mem:
720 /* Avoid leaking memory if malloc has failed. */
721 sqlite3ExprDelete(pExpr);
722 sqlite3ExprListDelete(pList);
723 return 0;
drha76b5df2002-02-23 02:32:10 +0000724}
725
726/*
danielk19777a15a4b2007-05-08 17:54:43 +0000727** If the expression list pEList contains more than iLimit elements,
728** leave an error message in pParse.
729*/
730void sqlite3ExprListCheckLength(
731 Parse *pParse,
732 ExprList *pEList,
danielk19777a15a4b2007-05-08 17:54:43 +0000733 const char *zObject
734){
drhb1a6c3c2008-03-20 16:30:17 +0000735 int mx = pParse->db->aLimit[SQLITE_LIMIT_COLUMN];
drhc5499be2008-04-01 15:06:33 +0000736 testcase( pEList && pEList->nExpr==mx );
737 testcase( pEList && pEList->nExpr==mx+1 );
drhb1a6c3c2008-03-20 16:30:17 +0000738 if( pEList && pEList->nExpr>mx ){
danielk19777a15a4b2007-05-08 17:54:43 +0000739 sqlite3ErrorMsg(pParse, "too many columns in %s", zObject);
740 }
741}
742
danielk1977fc976062007-05-10 10:46:56 +0000743
danielk1977fc976062007-05-10 10:46:56 +0000744/* The following three functions, heightOfExpr(), heightOfExprList()
745** and heightOfSelect(), are used to determine the maximum height
746** of any expression tree referenced by the structure passed as the
747** first argument.
748**
749** If this maximum height is greater than the current value pointed
750** to by pnHeight, the second parameter, then set *pnHeight to that
751** value.
752*/
753static void heightOfExpr(Expr *p, int *pnHeight){
754 if( p ){
755 if( p->nHeight>*pnHeight ){
756 *pnHeight = p->nHeight;
757 }
758 }
759}
760static void heightOfExprList(ExprList *p, int *pnHeight){
761 if( p ){
762 int i;
763 for(i=0; i<p->nExpr; i++){
764 heightOfExpr(p->a[i].pExpr, pnHeight);
765 }
766 }
767}
768static void heightOfSelect(Select *p, int *pnHeight){
769 if( p ){
770 heightOfExpr(p->pWhere, pnHeight);
771 heightOfExpr(p->pHaving, pnHeight);
772 heightOfExpr(p->pLimit, pnHeight);
773 heightOfExpr(p->pOffset, pnHeight);
774 heightOfExprList(p->pEList, pnHeight);
775 heightOfExprList(p->pGroupBy, pnHeight);
776 heightOfExprList(p->pOrderBy, pnHeight);
777 heightOfSelect(p->pPrior, pnHeight);
778 }
779}
780
781/*
782** Set the Expr.nHeight variable in the structure passed as an
783** argument. An expression with no children, Expr.pList or
784** Expr.pSelect member has a height of 1. Any other expression
785** has a height equal to the maximum height of any other
786** referenced Expr plus one.
787*/
788void sqlite3ExprSetHeight(Expr *p){
789 int nHeight = 0;
790 heightOfExpr(p->pLeft, &nHeight);
791 heightOfExpr(p->pRight, &nHeight);
792 heightOfExprList(p->pList, &nHeight);
793 heightOfSelect(p->pSelect, &nHeight);
794 p->nHeight = nHeight + 1;
795}
796
797/*
798** Return the maximum height of any expression tree referenced
799** by the select statement passed as an argument.
800*/
801int sqlite3SelectExprHeight(Select *p){
802 int nHeight = 0;
803 heightOfSelect(p, &nHeight);
804 return nHeight;
805}
danielk1977fc976062007-05-10 10:46:56 +0000806
danielk19777a15a4b2007-05-08 17:54:43 +0000807/*
drha76b5df2002-02-23 02:32:10 +0000808** Delete an entire expression list.
809*/
danielk19774adee202004-05-08 08:23:19 +0000810void sqlite3ExprListDelete(ExprList *pList){
drha76b5df2002-02-23 02:32:10 +0000811 int i;
drhbe5c89a2004-07-26 00:31:09 +0000812 struct ExprList_item *pItem;
drha76b5df2002-02-23 02:32:10 +0000813 if( pList==0 ) return;
drh1bdd9b52004-04-23 17:04:44 +0000814 assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
815 assert( pList->nExpr<=pList->nAlloc );
drhbe5c89a2004-07-26 00:31:09 +0000816 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
817 sqlite3ExprDelete(pItem->pExpr);
drh17435752007-08-16 04:30:38 +0000818 sqlite3_free(pItem->zName);
drha76b5df2002-02-23 02:32:10 +0000819 }
drh17435752007-08-16 04:30:38 +0000820 sqlite3_free(pList->a);
821 sqlite3_free(pList);
drha76b5df2002-02-23 02:32:10 +0000822}
823
824/*
drh678ccce2008-03-31 18:19:54 +0000825** Walk an expression tree. Call xFunc for each node visited. xFunc
826** is called on the node before xFunc is called on the nodes children.
drh73b211a2005-01-18 04:00:42 +0000827**
drh626a8792005-01-17 22:08:19 +0000828** The return value from xFunc determines whether the tree walk continues.
829** 0 means continue walking the tree. 1 means do not walk children
830** of the current node but continue with siblings. 2 means abandon
831** the tree walk completely.
832**
833** The return value from this routine is 1 to abandon the tree walk
834** and 0 to continue.
drh87abf5c2005-08-25 12:45:04 +0000835**
836** NOTICE: This routine does *not* descend into subqueries.
drh626a8792005-01-17 22:08:19 +0000837*/
danielk1977a58fdfb2005-02-08 07:50:40 +0000838static int walkExprList(ExprList *, int (*)(void *, Expr*), void *);
drh626a8792005-01-17 22:08:19 +0000839static int walkExprTree(Expr *pExpr, int (*xFunc)(void*,Expr*), void *pArg){
drh626a8792005-01-17 22:08:19 +0000840 int rc;
841 if( pExpr==0 ) return 0;
842 rc = (*xFunc)(pArg, pExpr);
843 if( rc==0 ){
844 if( walkExprTree(pExpr->pLeft, xFunc, pArg) ) return 1;
845 if( walkExprTree(pExpr->pRight, xFunc, pArg) ) return 1;
danielk1977a58fdfb2005-02-08 07:50:40 +0000846 if( walkExprList(pExpr->pList, xFunc, pArg) ) return 1;
drh626a8792005-01-17 22:08:19 +0000847 }
848 return rc>1;
849}
850
851/*
danielk1977a58fdfb2005-02-08 07:50:40 +0000852** Call walkExprTree() for every expression in list p.
853*/
854static int walkExprList(ExprList *p, int (*xFunc)(void *, Expr*), void *pArg){
855 int i;
856 struct ExprList_item *pItem;
857 if( !p ) return 0;
858 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
859 if( walkExprTree(pItem->pExpr, xFunc, pArg) ) return 1;
860 }
861 return 0;
862}
863
864/*
865** Call walkExprTree() for every expression in Select p, not including
866** expressions that are part of sub-selects in any FROM clause or the LIMIT
867** or OFFSET expressions..
868*/
869static int walkSelectExpr(Select *p, int (*xFunc)(void *, Expr*), void *pArg){
870 walkExprList(p->pEList, xFunc, pArg);
871 walkExprTree(p->pWhere, xFunc, pArg);
872 walkExprList(p->pGroupBy, xFunc, pArg);
873 walkExprTree(p->pHaving, xFunc, pArg);
874 walkExprList(p->pOrderBy, xFunc, pArg);
danielk197715d79822007-05-15 07:00:34 +0000875 if( p->pPrior ){
876 walkSelectExpr(p->pPrior, xFunc, pArg);
877 }
danielk1977a58fdfb2005-02-08 07:50:40 +0000878 return 0;
879}
880
881
882/*
drh626a8792005-01-17 22:08:19 +0000883** This routine is designed as an xFunc for walkExprTree().
884**
885** pArg is really a pointer to an integer. If we can tell by looking
drh73b211a2005-01-18 04:00:42 +0000886** at pExpr that the expression that contains pExpr is not a constant
887** expression, then set *pArg to 0 and return 2 to abandon the tree walk.
888** If pExpr does does not disqualify the expression from being a constant
889** then do nothing.
890**
891** After walking the whole tree, if no nodes are found that disqualify
892** the expression as constant, then we assume the whole expression
893** is constant. See sqlite3ExprIsConstant() for additional information.
drh626a8792005-01-17 22:08:19 +0000894*/
895static int exprNodeIsConstant(void *pArg, Expr *pExpr){
drh0a168372007-06-08 00:20:47 +0000896 int *pN = (int*)pArg;
897
898 /* If *pArg is 3 then any term of the expression that comes from
899 ** the ON or USING clauses of a join disqualifies the expression
900 ** from being considered constant. */
901 if( (*pN)==3 && ExprHasAnyProperty(pExpr, EP_FromJoin) ){
902 *pN = 0;
903 return 2;
904 }
905
drh626a8792005-01-17 22:08:19 +0000906 switch( pExpr->op ){
drheb55bd22005-06-30 17:04:21 +0000907 /* Consider functions to be constant if all their arguments are constant
908 ** and *pArg==2 */
909 case TK_FUNCTION:
drh0a168372007-06-08 00:20:47 +0000910 if( (*pN)==2 ) return 0;
drheb55bd22005-06-30 17:04:21 +0000911 /* Fall through */
drh626a8792005-01-17 22:08:19 +0000912 case TK_ID:
913 case TK_COLUMN:
914 case TK_DOT:
915 case TK_AGG_FUNCTION:
drh13449892005-09-07 21:22:45 +0000916 case TK_AGG_COLUMN:
drhfe2093d2005-01-20 22:48:47 +0000917#ifndef SQLITE_OMIT_SUBQUERY
918 case TK_SELECT:
919 case TK_EXISTS:
drhc5499be2008-04-01 15:06:33 +0000920 testcase( pExpr->op==TK_SELECT );
921 testcase( pExpr->op==TK_EXISTS );
drhfe2093d2005-01-20 22:48:47 +0000922#endif
drhc5499be2008-04-01 15:06:33 +0000923 testcase( pExpr->op==TK_ID );
924 testcase( pExpr->op==TK_COLUMN );
925 testcase( pExpr->op==TK_DOT );
926 testcase( pExpr->op==TK_AGG_FUNCTION );
927 testcase( pExpr->op==TK_AGG_COLUMN );
drh0a168372007-06-08 00:20:47 +0000928 *pN = 0;
drh626a8792005-01-17 22:08:19 +0000929 return 2;
drh87abf5c2005-08-25 12:45:04 +0000930 case TK_IN:
931 if( pExpr->pSelect ){
drh0a168372007-06-08 00:20:47 +0000932 *pN = 0;
drh87abf5c2005-08-25 12:45:04 +0000933 return 2;
934 }
drh626a8792005-01-17 22:08:19 +0000935 default:
936 return 0;
937 }
938}
939
940/*
drhfef52082000-06-06 01:50:43 +0000941** Walk an expression tree. Return 1 if the expression is constant
drheb55bd22005-06-30 17:04:21 +0000942** and 0 if it involves variables or function calls.
drh23989372002-05-21 13:43:04 +0000943**
944** For the purposes of this function, a double-quoted string (ex: "abc")
945** is considered a variable but a single-quoted string (ex: 'abc') is
946** a constant.
drhfef52082000-06-06 01:50:43 +0000947*/
danielk19774adee202004-05-08 08:23:19 +0000948int sqlite3ExprIsConstant(Expr *p){
drh626a8792005-01-17 22:08:19 +0000949 int isConst = 1;
950 walkExprTree(p, exprNodeIsConstant, &isConst);
951 return isConst;
drhfef52082000-06-06 01:50:43 +0000952}
953
954/*
drheb55bd22005-06-30 17:04:21 +0000955** Walk an expression tree. Return 1 if the expression is constant
drh0a168372007-06-08 00:20:47 +0000956** that does no originate from the ON or USING clauses of a join.
957** Return 0 if it involves variables or function calls or terms from
958** an ON or USING clause.
959*/
960int sqlite3ExprIsConstantNotJoin(Expr *p){
961 int isConst = 3;
962 walkExprTree(p, exprNodeIsConstant, &isConst);
963 return isConst!=0;
964}
965
966/*
967** Walk an expression tree. Return 1 if the expression is constant
drheb55bd22005-06-30 17:04:21 +0000968** or a function call with constant arguments. Return and 0 if there
969** are any variables.
970**
971** For the purposes of this function, a double-quoted string (ex: "abc")
972** is considered a variable but a single-quoted string (ex: 'abc') is
973** a constant.
974*/
975int sqlite3ExprIsConstantOrFunction(Expr *p){
976 int isConst = 2;
977 walkExprTree(p, exprNodeIsConstant, &isConst);
978 return isConst!=0;
979}
980
981/*
drh73b211a2005-01-18 04:00:42 +0000982** If the expression p codes a constant integer that is small enough
drh202b2df2004-01-06 01:13:46 +0000983** to fit in a 32-bit integer, return 1 and put the value of the integer
984** in *pValue. If the expression is not an integer or if it is too big
985** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
drhe4de1fe2002-06-02 16:09:01 +0000986*/
danielk19774adee202004-05-08 08:23:19 +0000987int sqlite3ExprIsInteger(Expr *p, int *pValue){
drhe4de1fe2002-06-02 16:09:01 +0000988 switch( p->op ){
989 case TK_INTEGER: {
drh2646da72005-12-09 20:02:05 +0000990 if( sqlite3GetInt32((char*)p->token.z, pValue) ){
drh202b2df2004-01-06 01:13:46 +0000991 return 1;
992 }
993 break;
drhe4de1fe2002-06-02 16:09:01 +0000994 }
drh4b59ab52002-08-24 18:24:51 +0000995 case TK_UPLUS: {
danielk19774adee202004-05-08 08:23:19 +0000996 return sqlite3ExprIsInteger(p->pLeft, pValue);
drh4b59ab52002-08-24 18:24:51 +0000997 }
drhe4de1fe2002-06-02 16:09:01 +0000998 case TK_UMINUS: {
999 int v;
danielk19774adee202004-05-08 08:23:19 +00001000 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
drhe4de1fe2002-06-02 16:09:01 +00001001 *pValue = -v;
1002 return 1;
1003 }
1004 break;
1005 }
1006 default: break;
1007 }
1008 return 0;
1009}
1010
1011/*
drhc4a3c772001-04-04 11:48:57 +00001012** Return TRUE if the given string is a row-id column name.
1013*/
danielk19774adee202004-05-08 08:23:19 +00001014int sqlite3IsRowid(const char *z){
1015 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
1016 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
1017 if( sqlite3StrICmp(z, "OID")==0 ) return 1;
drhc4a3c772001-04-04 11:48:57 +00001018 return 0;
1019}
1020
1021/*
drh8141f612004-01-25 22:44:58 +00001022** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
1023** that name in the set of source tables in pSrcList and make the pExpr
1024** expression node refer back to that source column. The following changes
1025** are made to pExpr:
1026**
1027** pExpr->iDb Set the index in db->aDb[] of the database holding
1028** the table.
1029** pExpr->iTable Set to the cursor number for the table obtained
1030** from pSrcList.
1031** pExpr->iColumn Set to the column number within the table.
drh8141f612004-01-25 22:44:58 +00001032** pExpr->op Set to TK_COLUMN.
1033** pExpr->pLeft Any expression this points to is deleted
1034** pExpr->pRight Any expression this points to is deleted.
1035**
1036** The pDbToken is the name of the database (the "X"). This value may be
1037** NULL meaning that name is of the form Y.Z or Z. Any available database
1038** can be used. The pTableToken is the name of the table (the "Y"). This
1039** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it
1040** means that the form of the name is Z and that columns from any table
1041** can be used.
1042**
1043** If the name cannot be resolved unambiguously, leave an error message
1044** in pParse and return non-zero. Return zero on success.
1045*/
1046static int lookupName(
drhffe07b22005-11-03 00:41:17 +00001047 Parse *pParse, /* The parsing context */
drh8141f612004-01-25 22:44:58 +00001048 Token *pDbToken, /* Name of the database containing table, or NULL */
1049 Token *pTableToken, /* Name of table containing column, or NULL */
1050 Token *pColumnToken, /* Name of the column. */
drh626a8792005-01-17 22:08:19 +00001051 NameContext *pNC, /* The name context used to resolve the name */
drh8141f612004-01-25 22:44:58 +00001052 Expr *pExpr /* Make this EXPR node point to the selected column */
1053){
1054 char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */
1055 char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */
1056 char *zCol = 0; /* Name of the column. The "Z" */
1057 int i, j; /* Loop counters */
1058 int cnt = 0; /* Number of matching column names */
1059 int cntTab = 0; /* Number of matching table names */
drh626a8792005-01-17 22:08:19 +00001060 sqlite3 *db = pParse->db; /* The database */
drh51669862004-12-18 18:40:26 +00001061 struct SrcList_item *pItem; /* Use for looping over pSrcList items */
1062 struct SrcList_item *pMatch = 0; /* The matching pSrcList item */
drh73b211a2005-01-18 04:00:42 +00001063 NameContext *pTopNC = pNC; /* First namecontext in the list */
drh728b5772007-09-18 15:55:07 +00001064 Schema *pSchema = 0; /* Schema of the expression */
drh8141f612004-01-25 22:44:58 +00001065
1066 assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
drh17435752007-08-16 04:30:38 +00001067 zDb = sqlite3NameFromToken(db, pDbToken);
1068 zTab = sqlite3NameFromToken(db, pTableToken);
1069 zCol = sqlite3NameFromToken(db, pColumnToken);
1070 if( db->mallocFailed ){
danielk1977d5d56522005-03-16 12:15:20 +00001071 goto lookupname_end;
drh8141f612004-01-25 22:44:58 +00001072 }
drh8141f612004-01-25 22:44:58 +00001073
1074 pExpr->iTable = -1;
drh626a8792005-01-17 22:08:19 +00001075 while( pNC && cnt==0 ){
drhffe07b22005-11-03 00:41:17 +00001076 ExprList *pEList;
drh626a8792005-01-17 22:08:19 +00001077 SrcList *pSrcList = pNC->pSrcList;
drh8141f612004-01-25 22:44:58 +00001078
danielk1977b3bce662005-01-29 08:32:43 +00001079 if( pSrcList ){
1080 for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
drh43617e92006-03-06 20:55:46 +00001081 Table *pTab;
1082 int iDb;
danielk1977b3bce662005-01-29 08:32:43 +00001083 Column *pCol;
1084
drh43617e92006-03-06 20:55:46 +00001085 pTab = pItem->pTab;
1086 assert( pTab!=0 );
1087 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
danielk1977b3bce662005-01-29 08:32:43 +00001088 assert( pTab->nCol>0 );
1089 if( zTab ){
1090 if( pItem->zAlias ){
1091 char *zTabName = pItem->zAlias;
1092 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
1093 }else{
1094 char *zTabName = pTab->zName;
1095 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
danielk1977da184232006-01-05 11:34:32 +00001096 if( zDb!=0 && sqlite3StrICmp(db->aDb[iDb].zName, zDb)!=0 ){
danielk1977b3bce662005-01-29 08:32:43 +00001097 continue;
1098 }
drh626a8792005-01-17 22:08:19 +00001099 }
drh8141f612004-01-25 22:44:58 +00001100 }
danielk1977b3bce662005-01-29 08:32:43 +00001101 if( 0==(cntTab++) ){
drh626a8792005-01-17 22:08:19 +00001102 pExpr->iTable = pItem->iCursor;
drh728b5772007-09-18 15:55:07 +00001103 pSchema = pTab->pSchema;
danielk1977b3bce662005-01-29 08:32:43 +00001104 pMatch = pItem;
1105 }
1106 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
1107 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
danielk1977b3bf5562006-01-10 17:58:23 +00001108 const char *zColl = pTab->aCol[j].zColl;
drh873fac02005-06-06 17:11:46 +00001109 IdList *pUsing;
danielk1977b3bce662005-01-29 08:32:43 +00001110 cnt++;
1111 pExpr->iTable = pItem->iCursor;
1112 pMatch = pItem;
drh728b5772007-09-18 15:55:07 +00001113 pSchema = pTab->pSchema;
danielk1977b3bce662005-01-29 08:32:43 +00001114 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
1115 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
1116 pExpr->affinity = pTab->aCol[j].affinity;
drh8b4c40d2007-02-01 23:02:45 +00001117 if( (pExpr->flags & EP_ExpCollate)==0 ){
1118 pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0);
1119 }
drh61dfc312006-12-16 16:25:15 +00001120 if( i<pSrcList->nSrc-1 ){
1121 if( pItem[1].jointype & JT_NATURAL ){
1122 /* If this match occurred in the left table of a natural join,
1123 ** then skip the right table to avoid a duplicate match */
1124 pItem++;
1125 i++;
1126 }else if( (pUsing = pItem[1].pUsing)!=0 ){
1127 /* If this match occurs on a column that is in the USING clause
1128 ** of a join, skip the search of the right table of the join
1129 ** to avoid a duplicate match there. */
1130 int k;
1131 for(k=0; k<pUsing->nId; k++){
1132 if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ){
1133 pItem++;
1134 i++;
1135 break;
1136 }
drh873fac02005-06-06 17:11:46 +00001137 }
1138 }
1139 }
danielk1977b3bce662005-01-29 08:32:43 +00001140 break;
1141 }
drh8141f612004-01-25 22:44:58 +00001142 }
1143 }
1144 }
drh626a8792005-01-17 22:08:19 +00001145
1146#ifndef SQLITE_OMIT_TRIGGER
1147 /* If we have not already resolved the name, then maybe
1148 ** it is a new.* or old.* trigger argument reference
1149 */
1150 if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
1151 TriggerStack *pTriggerStack = pParse->trigStack;
1152 Table *pTab = 0;
danielk19778f2c54e2008-01-01 19:02:09 +00001153 u32 *piColMask;
drh626a8792005-01-17 22:08:19 +00001154 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){
1155 pExpr->iTable = pTriggerStack->newIdx;
1156 assert( pTriggerStack->pTab );
1157 pTab = pTriggerStack->pTab;
danielk19778f2c54e2008-01-01 19:02:09 +00001158 piColMask = &(pTriggerStack->newColMask);
drh626a8792005-01-17 22:08:19 +00001159 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab)==0 ){
1160 pExpr->iTable = pTriggerStack->oldIdx;
1161 assert( pTriggerStack->pTab );
1162 pTab = pTriggerStack->pTab;
danielk19778f2c54e2008-01-01 19:02:09 +00001163 piColMask = &(pTriggerStack->oldColMask);
drh626a8792005-01-17 22:08:19 +00001164 }
1165
1166 if( pTab ){
danielk1977f0113002006-01-24 12:09:17 +00001167 int iCol;
drh626a8792005-01-17 22:08:19 +00001168 Column *pCol = pTab->aCol;
1169
drh728b5772007-09-18 15:55:07 +00001170 pSchema = pTab->pSchema;
drh626a8792005-01-17 22:08:19 +00001171 cntTab++;
danielk1977f0113002006-01-24 12:09:17 +00001172 for(iCol=0; iCol < pTab->nCol; iCol++, pCol++) {
drh626a8792005-01-17 22:08:19 +00001173 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
danielk1977f0113002006-01-24 12:09:17 +00001174 const char *zColl = pTab->aCol[iCol].zColl;
drh626a8792005-01-17 22:08:19 +00001175 cnt++;
danielk1977f0113002006-01-24 12:09:17 +00001176 pExpr->iColumn = iCol==pTab->iPKey ? -1 : iCol;
1177 pExpr->affinity = pTab->aCol[iCol].affinity;
drh8b4c40d2007-02-01 23:02:45 +00001178 if( (pExpr->flags & EP_ExpCollate)==0 ){
1179 pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0);
1180 }
danielk1977aee18ef2005-03-09 12:26:50 +00001181 pExpr->pTab = pTab;
danielk19778f2c54e2008-01-01 19:02:09 +00001182 if( iCol>=0 ){
drhc5499be2008-04-01 15:06:33 +00001183 testcase( iCol==31 );
1184 testcase( iCol==32 );
danielk19778f2c54e2008-01-01 19:02:09 +00001185 *piColMask |= ((u32)1<<iCol) | (iCol>=32?0xffffffff:0);
1186 }
drh626a8792005-01-17 22:08:19 +00001187 break;
1188 }
1189 }
1190 }
1191 }
drhb7f91642004-10-31 02:22:47 +00001192#endif /* !defined(SQLITE_OMIT_TRIGGER) */
drh8141f612004-01-25 22:44:58 +00001193
drh626a8792005-01-17 22:08:19 +00001194 /*
1195 ** Perhaps the name is a reference to the ROWID
1196 */
1197 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
1198 cnt = 1;
1199 pExpr->iColumn = -1;
drh8a512562005-11-14 22:29:05 +00001200 pExpr->affinity = SQLITE_AFF_INTEGER;
drh626a8792005-01-17 22:08:19 +00001201 }
drh8141f612004-01-25 22:44:58 +00001202
drh626a8792005-01-17 22:08:19 +00001203 /*
1204 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
1205 ** might refer to an result-set alias. This happens, for example, when
1206 ** we are resolving names in the WHERE clause of the following command:
1207 **
1208 ** SELECT a+b AS x FROM table WHERE x<10;
1209 **
1210 ** In cases like this, replace pExpr with a copy of the expression that
1211 ** forms the result set entry ("a+b" in the example) and return immediately.
1212 ** Note that the expression in the result set should have already been
1213 ** resolved by the time the WHERE clause is resolved.
1214 */
drhffe07b22005-11-03 00:41:17 +00001215 if( cnt==0 && (pEList = pNC->pEList)!=0 && zTab==0 ){
drh626a8792005-01-17 22:08:19 +00001216 for(j=0; j<pEList->nExpr; j++){
1217 char *zAs = pEList->a[j].zName;
1218 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
drh36379e92007-07-23 22:51:15 +00001219 Expr *pDup, *pOrig;
drh626a8792005-01-17 22:08:19 +00001220 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
drh4f07e5f2007-05-14 11:34:46 +00001221 assert( pExpr->pList==0 );
1222 assert( pExpr->pSelect==0 );
drh36379e92007-07-23 22:51:15 +00001223 pOrig = pEList->a[j].pExpr;
1224 if( !pNC->allowAgg && ExprHasProperty(pOrig, EP_Agg) ){
1225 sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs);
drh17435752007-08-16 04:30:38 +00001226 sqlite3_free(zCol);
drh36379e92007-07-23 22:51:15 +00001227 return 2;
1228 }
danielk19771e536952007-08-16 10:09:01 +00001229 pDup = sqlite3ExprDup(db, pOrig);
drh4f07e5f2007-05-14 11:34:46 +00001230 if( pExpr->flags & EP_ExpCollate ){
1231 pDup->pColl = pExpr->pColl;
1232 pDup->flags |= EP_ExpCollate;
1233 }
drh17435752007-08-16 04:30:38 +00001234 if( pExpr->span.dyn ) sqlite3_free((char*)pExpr->span.z);
1235 if( pExpr->token.dyn ) sqlite3_free((char*)pExpr->token.z);
drh4f07e5f2007-05-14 11:34:46 +00001236 memcpy(pExpr, pDup, sizeof(*pExpr));
drh17435752007-08-16 04:30:38 +00001237 sqlite3_free(pDup);
drh15ccce12005-05-23 15:06:39 +00001238 cnt = 1;
danielk1977c9cf6e32007-06-25 16:29:33 +00001239 pMatch = 0;
drh626a8792005-01-17 22:08:19 +00001240 assert( zTab==0 && zDb==0 );
drh15ccce12005-05-23 15:06:39 +00001241 goto lookupname_end_2;
drh626a8792005-01-17 22:08:19 +00001242 }
1243 }
1244 }
1245
1246 /* Advance to the next name context. The loop will exit when either
1247 ** we have a match (cnt>0) or when we run out of name contexts.
1248 */
1249 if( cnt==0 ){
1250 pNC = pNC->pNext;
1251 }
drh8141f612004-01-25 22:44:58 +00001252 }
1253
1254 /*
1255 ** If X and Y are NULL (in other words if only the column name Z is
1256 ** supplied) and the value of Z is enclosed in double-quotes, then
1257 ** Z is a string literal if it doesn't match any column names. In that
1258 ** case, we need to return right away and not make any changes to
1259 ** pExpr.
drh15ccce12005-05-23 15:06:39 +00001260 **
1261 ** Because no reference was made to outer contexts, the pNC->nRef
1262 ** fields are not changed in any context.
drh8141f612004-01-25 22:44:58 +00001263 */
1264 if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
drh17435752007-08-16 04:30:38 +00001265 sqlite3_free(zCol);
drh8141f612004-01-25 22:44:58 +00001266 return 0;
1267 }
1268
1269 /*
1270 ** cnt==0 means there was not match. cnt>1 means there were two or
1271 ** more matches. Either way, we have an error.
1272 */
1273 if( cnt!=1 ){
drhde4fcfd2008-01-19 23:50:26 +00001274 const char *zErr;
1275 zErr = cnt==0 ? "no such column" : "ambiguous column name";
drh8141f612004-01-25 22:44:58 +00001276 if( zDb ){
drhde4fcfd2008-01-19 23:50:26 +00001277 sqlite3ErrorMsg(pParse, "%s: %s.%s.%s", zErr, zDb, zTab, zCol);
drh8141f612004-01-25 22:44:58 +00001278 }else if( zTab ){
drhde4fcfd2008-01-19 23:50:26 +00001279 sqlite3ErrorMsg(pParse, "%s: %s.%s", zErr, zTab, zCol);
drh8141f612004-01-25 22:44:58 +00001280 }else{
drhde4fcfd2008-01-19 23:50:26 +00001281 sqlite3ErrorMsg(pParse, "%s: %s", zErr, zCol);
drh8141f612004-01-25 22:44:58 +00001282 }
drhde4fcfd2008-01-19 23:50:26 +00001283 pTopNC->nErr++;
drh8141f612004-01-25 22:44:58 +00001284 }
1285
drh51669862004-12-18 18:40:26 +00001286 /* If a column from a table in pSrcList is referenced, then record
1287 ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes
1288 ** bit 0 to be set. Column 1 sets bit 1. And so forth. If the
1289 ** column number is greater than the number of bits in the bitmask
1290 ** then set the high-order bit of the bitmask.
1291 */
1292 if( pExpr->iColumn>=0 && pMatch!=0 ){
1293 int n = pExpr->iColumn;
drhc5499be2008-04-01 15:06:33 +00001294 testcase( n==sizeof(Bitmask)*8-1 );
drh51669862004-12-18 18:40:26 +00001295 if( n>=sizeof(Bitmask)*8 ){
1296 n = sizeof(Bitmask)*8-1;
1297 }
1298 assert( pMatch->iCursor==pExpr->iTable );
drhca83ac52007-02-01 01:40:44 +00001299 pMatch->colUsed |= ((Bitmask)1)<<n;
drh51669862004-12-18 18:40:26 +00001300 }
1301
danielk1977d5d56522005-03-16 12:15:20 +00001302lookupname_end:
drh8141f612004-01-25 22:44:58 +00001303 /* Clean up and return
1304 */
drh17435752007-08-16 04:30:38 +00001305 sqlite3_free(zDb);
1306 sqlite3_free(zTab);
danielk19774adee202004-05-08 08:23:19 +00001307 sqlite3ExprDelete(pExpr->pLeft);
drh8141f612004-01-25 22:44:58 +00001308 pExpr->pLeft = 0;
danielk19774adee202004-05-08 08:23:19 +00001309 sqlite3ExprDelete(pExpr->pRight);
drh8141f612004-01-25 22:44:58 +00001310 pExpr->pRight = 0;
1311 pExpr->op = TK_COLUMN;
drh15ccce12005-05-23 15:06:39 +00001312lookupname_end_2:
drh17435752007-08-16 04:30:38 +00001313 sqlite3_free(zCol);
drh626a8792005-01-17 22:08:19 +00001314 if( cnt==1 ){
danielk1977b3bce662005-01-29 08:32:43 +00001315 assert( pNC!=0 );
drh728b5772007-09-18 15:55:07 +00001316 sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList);
danielk1977aee18ef2005-03-09 12:26:50 +00001317 if( pMatch && !pMatch->pSelect ){
1318 pExpr->pTab = pMatch->pTab;
1319 }
drh15ccce12005-05-23 15:06:39 +00001320 /* Increment the nRef value on all name contexts from TopNC up to
1321 ** the point where the name matched. */
1322 for(;;){
1323 assert( pTopNC!=0 );
1324 pTopNC->nRef++;
1325 if( pTopNC==pNC ) break;
1326 pTopNC = pTopNC->pNext;
1327 }
1328 return 0;
1329 } else {
1330 return 1;
drh626a8792005-01-17 22:08:19 +00001331 }
drh8141f612004-01-25 22:44:58 +00001332}
1333
1334/*
drh626a8792005-01-17 22:08:19 +00001335** This routine is designed as an xFunc for walkExprTree().
1336**
drh73b211a2005-01-18 04:00:42 +00001337** Resolve symbolic names into TK_COLUMN operators for the current
drh626a8792005-01-17 22:08:19 +00001338** node in the expression tree. Return 0 to continue the search down
drh73b211a2005-01-18 04:00:42 +00001339** the tree or 2 to abort the tree walk.
1340**
1341** This routine also does error checking and name resolution for
1342** function names. The operator for aggregate functions is changed
1343** to TK_AGG_FUNCTION.
drh626a8792005-01-17 22:08:19 +00001344*/
1345static int nameResolverStep(void *pArg, Expr *pExpr){
1346 NameContext *pNC = (NameContext*)pArg;
drh626a8792005-01-17 22:08:19 +00001347 Parse *pParse;
drh626a8792005-01-17 22:08:19 +00001348
danielk1977b3bce662005-01-29 08:32:43 +00001349 if( pExpr==0 ) return 1;
drh626a8792005-01-17 22:08:19 +00001350 assert( pNC!=0 );
drh626a8792005-01-17 22:08:19 +00001351 pParse = pNC->pParse;
danielk1977b3bce662005-01-29 08:32:43 +00001352
drh626a8792005-01-17 22:08:19 +00001353 if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return 1;
1354 ExprSetProperty(pExpr, EP_Resolved);
1355#ifndef NDEBUG
danielk1977f0113002006-01-24 12:09:17 +00001356 if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
1357 SrcList *pSrcList = pNC->pSrcList;
danielk1977940fac92005-01-23 22:41:37 +00001358 int i;
danielk1977f0113002006-01-24 12:09:17 +00001359 for(i=0; i<pNC->pSrcList->nSrc; i++){
drh626a8792005-01-17 22:08:19 +00001360 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
1361 }
1362 }
1363#endif
1364 switch( pExpr->op ){
1365 /* Double-quoted strings (ex: "abc") are used as identifiers if
1366 ** possible. Otherwise they remain as strings. Single-quoted
1367 ** strings (ex: 'abc') are always string literals.
1368 */
1369 case TK_STRING: {
1370 if( pExpr->token.z[0]=='\'' ) break;
1371 /* Fall thru into the TK_ID case if this is a double-quoted string */
1372 }
1373 /* A lone identifier is the name of a column.
1374 */
1375 case TK_ID: {
drh626a8792005-01-17 22:08:19 +00001376 lookupName(pParse, 0, 0, &pExpr->token, pNC, pExpr);
1377 return 1;
1378 }
1379
1380 /* A table name and column name: ID.ID
1381 ** Or a database, table and column: ID.ID.ID
1382 */
1383 case TK_DOT: {
1384 Token *pColumn;
1385 Token *pTable;
1386 Token *pDb;
1387 Expr *pRight;
1388
danielk1977b3bce662005-01-29 08:32:43 +00001389 /* if( pSrcList==0 ) break; */
drh626a8792005-01-17 22:08:19 +00001390 pRight = pExpr->pRight;
1391 if( pRight->op==TK_ID ){
1392 pDb = 0;
1393 pTable = &pExpr->pLeft->token;
1394 pColumn = &pRight->token;
1395 }else{
1396 assert( pRight->op==TK_DOT );
1397 pDb = &pExpr->pLeft->token;
1398 pTable = &pRight->pLeft->token;
1399 pColumn = &pRight->pRight->token;
1400 }
1401 lookupName(pParse, pDb, pTable, pColumn, pNC, pExpr);
1402 return 1;
1403 }
1404
1405 /* Resolve function names
1406 */
drhb71090f2005-05-23 17:26:51 +00001407 case TK_CONST_FUNC:
drh626a8792005-01-17 22:08:19 +00001408 case TK_FUNCTION: {
1409 ExprList *pList = pExpr->pList; /* The argument list */
1410 int n = pList ? pList->nExpr : 0; /* Number of arguments */
1411 int no_such_func = 0; /* True if no such function exists */
1412 int wrong_num_args = 0; /* True if wrong number of arguments */
1413 int is_agg = 0; /* True if is an aggregate function */
1414 int i;
drh5169bbc2006-08-24 14:59:45 +00001415 int auth; /* Authorization to use the function */
drh626a8792005-01-17 22:08:19 +00001416 int nId; /* Number of characters in function name */
1417 const char *zId; /* The function name. */
drh73b211a2005-01-18 04:00:42 +00001418 FuncDef *pDef; /* Information about the function */
danielk197714db2662006-01-09 16:12:04 +00001419 int enc = ENC(pParse->db); /* The database encoding */
drh626a8792005-01-17 22:08:19 +00001420
drh2646da72005-12-09 20:02:05 +00001421 zId = (char*)pExpr->token.z;
drhb71090f2005-05-23 17:26:51 +00001422 nId = pExpr->token.n;
drh626a8792005-01-17 22:08:19 +00001423 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
1424 if( pDef==0 ){
1425 pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
1426 if( pDef==0 ){
1427 no_such_func = 1;
1428 }else{
1429 wrong_num_args = 1;
1430 }
1431 }else{
1432 is_agg = pDef->xFunc==0;
1433 }
drh2fca7fe2006-11-23 11:59:13 +00001434#ifndef SQLITE_OMIT_AUTHORIZATION
drh5169bbc2006-08-24 14:59:45 +00001435 if( pDef ){
1436 auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0);
1437 if( auth!=SQLITE_OK ){
1438 if( auth==SQLITE_DENY ){
1439 sqlite3ErrorMsg(pParse, "not authorized to use function: %s",
1440 pDef->zName);
1441 pNC->nErr++;
1442 }
1443 pExpr->op = TK_NULL;
1444 return 1;
1445 }
1446 }
drhb8b14212006-08-24 15:18:25 +00001447#endif
drh626a8792005-01-17 22:08:19 +00001448 if( is_agg && !pNC->allowAgg ){
1449 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
1450 pNC->nErr++;
1451 is_agg = 0;
1452 }else if( no_such_func ){
1453 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
1454 pNC->nErr++;
1455 }else if( wrong_num_args ){
1456 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
1457 nId, zId);
1458 pNC->nErr++;
1459 }
1460 if( is_agg ){
1461 pExpr->op = TK_AGG_FUNCTION;
1462 pNC->hasAgg = 1;
1463 }
drh73b211a2005-01-18 04:00:42 +00001464 if( is_agg ) pNC->allowAgg = 0;
drh626a8792005-01-17 22:08:19 +00001465 for(i=0; pNC->nErr==0 && i<n; i++){
drh73b211a2005-01-18 04:00:42 +00001466 walkExprTree(pList->a[i].pExpr, nameResolverStep, pNC);
drh626a8792005-01-17 22:08:19 +00001467 }
drh73b211a2005-01-18 04:00:42 +00001468 if( is_agg ) pNC->allowAgg = 1;
drh626a8792005-01-17 22:08:19 +00001469 /* FIX ME: Compute pExpr->affinity based on the expected return
1470 ** type of the function
1471 */
1472 return is_agg;
1473 }
danielk1977b3bce662005-01-29 08:32:43 +00001474#ifndef SQLITE_OMIT_SUBQUERY
1475 case TK_SELECT:
1476 case TK_EXISTS:
1477#endif
1478 case TK_IN: {
1479 if( pExpr->pSelect ){
drh8a9f38f2005-11-05 15:07:55 +00001480 int nRef = pNC->nRef;
drh06f65412005-11-03 02:03:13 +00001481#ifndef SQLITE_OMIT_CHECK
1482 if( pNC->isCheck ){
1483 sqlite3ErrorMsg(pParse,"subqueries prohibited in CHECK constraints");
1484 }
1485#endif
danielk1977b3bce662005-01-29 08:32:43 +00001486 sqlite3SelectResolve(pParse, pExpr->pSelect, pNC);
1487 assert( pNC->nRef>=nRef );
1488 if( nRef!=pNC->nRef ){
1489 ExprSetProperty(pExpr, EP_VarSelect);
1490 }
1491 }
drh4284fb02005-11-03 12:33:28 +00001492 break;
danielk1977b3bce662005-01-29 08:32:43 +00001493 }
drh4284fb02005-11-03 12:33:28 +00001494#ifndef SQLITE_OMIT_CHECK
1495 case TK_VARIABLE: {
1496 if( pNC->isCheck ){
1497 sqlite3ErrorMsg(pParse,"parameters prohibited in CHECK constraints");
1498 }
1499 break;
1500 }
1501#endif
drh626a8792005-01-17 22:08:19 +00001502 }
1503 return 0;
1504}
1505
1506/*
drhcce7d172000-05-31 15:34:51 +00001507** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +00001508** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +00001509** index to the table in the table list and a column offset. The
1510** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
1511** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +00001512** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +00001513** VDBE cursor number for a cursor that is pointing into the referenced
1514** table. The Expr.iColumn value is changed to the index of the column
1515** of the referenced table. The Expr.iColumn value for the special
1516** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
1517** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +00001518**
drh626a8792005-01-17 22:08:19 +00001519** Also resolve function names and check the functions for proper
1520** usage. Make sure all function names are recognized and all functions
1521** have the correct number of arguments. Leave an error message
1522** in pParse->zErrMsg if anything is amiss. Return the number of errors.
1523**
drh73b211a2005-01-18 04:00:42 +00001524** If the expression contains aggregate functions then set the EP_Agg
1525** property on the expression.
drh626a8792005-01-17 22:08:19 +00001526*/
danielk1977fc976062007-05-10 10:46:56 +00001527int sqlite3ExprResolveNames(
danielk1977b3bce662005-01-29 08:32:43 +00001528 NameContext *pNC, /* Namespace to resolve expressions in. */
1529 Expr *pExpr /* The expression to be analyzed. */
drh626a8792005-01-17 22:08:19 +00001530){
drh13449892005-09-07 21:22:45 +00001531 int savedHasAgg;
drhbb4957f2008-03-20 14:03:29 +00001532
drh73b211a2005-01-18 04:00:42 +00001533 if( pExpr==0 ) return 0;
drhbb4957f2008-03-20 14:03:29 +00001534#if SQLITE_MAX_EXPR_DEPTH>0
1535 {
1536 int mxDepth = pNC->pParse->db->aLimit[SQLITE_LIMIT_EXPR_DEPTH];
1537 if( (pExpr->nHeight+pNC->pParse->nHeight)>mxDepth ){
1538 sqlite3ErrorMsg(pNC->pParse,
1539 "Expression tree is too large (maximum depth %d)", mxDepth
1540 );
1541 return 1;
1542 }
1543 pNC->pParse->nHeight += pExpr->nHeight;
danielk1977fc976062007-05-10 10:46:56 +00001544 }
danielk1977fc976062007-05-10 10:46:56 +00001545#endif
drh13449892005-09-07 21:22:45 +00001546 savedHasAgg = pNC->hasAgg;
1547 pNC->hasAgg = 0;
danielk1977b3bce662005-01-29 08:32:43 +00001548 walkExprTree(pExpr, nameResolverStep, pNC);
drhbb4957f2008-03-20 14:03:29 +00001549#if SQLITE_MAX_EXPR_DEPTH>0
danielk1977fc976062007-05-10 10:46:56 +00001550 pNC->pParse->nHeight -= pExpr->nHeight;
1551#endif
danielk1977b3bce662005-01-29 08:32:43 +00001552 if( pNC->nErr>0 ){
drh73b211a2005-01-18 04:00:42 +00001553 ExprSetProperty(pExpr, EP_Error);
drh73b211a2005-01-18 04:00:42 +00001554 }
drh13449892005-09-07 21:22:45 +00001555 if( pNC->hasAgg ){
1556 ExprSetProperty(pExpr, EP_Agg);
1557 }else if( savedHasAgg ){
1558 pNC->hasAgg = 1;
1559 }
drh73b211a2005-01-18 04:00:42 +00001560 return ExprHasProperty(pExpr, EP_Error);
drh626a8792005-01-17 22:08:19 +00001561}
1562
drh1398ad32005-01-19 23:24:50 +00001563/*
1564** A pointer instance of this structure is used to pass information
1565** through walkExprTree into codeSubqueryStep().
1566*/
1567typedef struct QueryCoder QueryCoder;
1568struct QueryCoder {
1569 Parse *pParse; /* The parsing context */
1570 NameContext *pNC; /* Namespace of first enclosing query */
1571};
1572
danielk19779a96b662007-11-29 17:05:18 +00001573#ifdef SQLITE_TEST
1574 int sqlite3_enable_in_opt = 1;
1575#else
1576 #define sqlite3_enable_in_opt 1
1577#endif
1578
1579/*
drhb287f4b2008-04-25 00:08:38 +00001580** Return true if the IN operator optimization is enabled and
1581** the SELECT statement p exists and is of the
1582** simple form:
1583**
1584** SELECT <column> FROM <table>
1585**
1586** If this is the case, it may be possible to use an existing table
1587** or index instead of generating an epheremal table.
1588*/
1589#ifndef SQLITE_OMIT_SUBQUERY
1590static int isCandidateForInOpt(Select *p){
1591 SrcList *pSrc;
1592 ExprList *pEList;
1593 Table *pTab;
1594 if( !sqlite3_enable_in_opt ) return 0; /* IN optimization must be enabled */
1595 if( p==0 ) return 0; /* right-hand side of IN is SELECT */
1596 if( p->pPrior ) return 0; /* Not a compound SELECT */
1597 if( p->isDistinct ) return 0; /* No DISTINCT keyword */
1598 if( p->isAgg ) return 0; /* Contains no aggregate functions */
1599 if( p->pGroupBy ) return 0; /* Has no GROUP BY clause */
1600 if( p->pLimit ) return 0; /* Has no LIMIT clause */
1601 if( p->pOffset ) return 0;
1602 if( p->pWhere ) return 0; /* Has no WHERE clause */
1603 pSrc = p->pSrc;
1604 if( pSrc==0 ) return 0; /* A single table in the FROM clause */
1605 if( pSrc->nSrc!=1 ) return 0;
1606 if( pSrc->a[0].pSelect ) return 0; /* FROM clause is not a subquery */
1607 pTab = pSrc->a[0].pTab;
1608 if( pTab==0 ) return 0;
1609 if( pTab->pSelect ) return 0; /* FROM clause is not a view */
1610 if( IsVirtual(pTab) ) return 0; /* FROM clause not a virtual table */
1611 pEList = p->pEList;
1612 if( pEList->nExpr!=1 ) return 0; /* One column in the result set */
1613 if( pEList->a[0].pExpr->op!=TK_COLUMN ) return 0; /* Result is a column */
1614 return 1;
1615}
1616#endif /* SQLITE_OMIT_SUBQUERY */
1617
1618/*
danielk19779a96b662007-11-29 17:05:18 +00001619** This function is used by the implementation of the IN (...) operator.
1620** It's job is to find or create a b-tree structure that may be used
1621** either to test for membership of the (...) set or to iterate through
drh85b623f2007-12-13 21:54:09 +00001622** its members, skipping duplicates.
danielk19779a96b662007-11-29 17:05:18 +00001623**
1624** The cursor opened on the structure (database table, database index
1625** or ephermal table) is stored in pX->iTable before this function returns.
1626** The returned value indicates the structure type, as follows:
1627**
1628** IN_INDEX_ROWID - The cursor was opened on a database table.
drh2d401ab2008-01-10 23:50:11 +00001629** IN_INDEX_INDEX - The cursor was opened on a database index.
danielk19779a96b662007-11-29 17:05:18 +00001630** IN_INDEX_EPH - The cursor was opened on a specially created and
1631** populated epheremal table.
1632**
1633** An existing structure may only be used if the SELECT is of the simple
1634** form:
1635**
1636** SELECT <column> FROM <table>
1637**
1638** If the mustBeUnique parameter is false, the structure will be used
1639** for fast set membership tests. In this case an epheremal table must
1640** be used unless <column> is an INTEGER PRIMARY KEY or an index can
drh85b623f2007-12-13 21:54:09 +00001641** be found with <column> as its left-most column.
danielk19779a96b662007-11-29 17:05:18 +00001642**
1643** If mustBeUnique is true, then the structure will be used to iterate
1644** through the set members, skipping any duplicates. In this case an
1645** epheremal table must be used unless the selected <column> is guaranteed
1646** to be unique - either because it is an INTEGER PRIMARY KEY or it
1647** is unique by virtue of a constraint or implicit index.
1648*/
danielk1977284f4ac2007-12-10 05:03:46 +00001649#ifndef SQLITE_OMIT_SUBQUERY
danielk19779a96b662007-11-29 17:05:18 +00001650int sqlite3FindInIndex(Parse *pParse, Expr *pX, int mustBeUnique){
1651 Select *p;
1652 int eType = 0;
1653 int iTab = pParse->nTab++;
1654
1655 /* The follwing if(...) expression is true if the SELECT is of the
1656 ** simple form:
1657 **
1658 ** SELECT <column> FROM <table>
1659 **
1660 ** If this is the case, it may be possible to use an existing table
1661 ** or index instead of generating an epheremal table.
1662 */
drhb287f4b2008-04-25 00:08:38 +00001663 p = pX->pSelect;
1664 if( isCandidateForInOpt(p) ){
danielk19779a96b662007-11-29 17:05:18 +00001665 sqlite3 *db = pParse->db;
1666 Index *pIdx;
1667 Expr *pExpr = p->pEList->a[0].pExpr;
1668 int iCol = pExpr->iColumn;
1669 Vdbe *v = sqlite3GetVdbe(pParse);
1670
1671 /* This function is only called from two places. In both cases the vdbe
1672 ** has already been allocated. So assume sqlite3GetVdbe() is always
1673 ** successful here.
1674 */
1675 assert(v);
1676 if( iCol<0 ){
drh0a07c102008-01-03 18:03:08 +00001677 int iMem = ++pParse->nMem;
danielk19779a96b662007-11-29 17:05:18 +00001678 int iAddr;
1679 Table *pTab = p->pSrc->a[0].pTab;
1680 int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
1681 sqlite3VdbeUsesBtree(v, iDb);
1682
drh892d3172008-01-10 03:46:36 +00001683 iAddr = sqlite3VdbeAddOp1(v, OP_If, iMem);
drh4c583122008-01-04 22:01:03 +00001684 sqlite3VdbeAddOp2(v, OP_Integer, 1, iMem);
danielk19779a96b662007-11-29 17:05:18 +00001685
1686 sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
1687 eType = IN_INDEX_ROWID;
1688
1689 sqlite3VdbeJumpHere(v, iAddr);
1690 }else{
1691 /* The collation sequence used by the comparison. If an index is to
1692 ** be used in place of a temp-table, it must be ordered according
1693 ** to this collation sequence.
1694 */
1695 CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pExpr);
1696
1697 /* Check that the affinity that will be used to perform the
1698 ** comparison is the same as the affinity of the column. If
1699 ** it is not, it is not possible to use any index.
1700 */
1701 Table *pTab = p->pSrc->a[0].pTab;
1702 char aff = comparisonAffinity(pX);
1703 int affinity_ok = (pTab->aCol[iCol].affinity==aff||aff==SQLITE_AFF_NONE);
1704
1705 for(pIdx=pTab->pIndex; pIdx && eType==0 && affinity_ok; pIdx=pIdx->pNext){
1706 if( (pIdx->aiColumn[0]==iCol)
1707 && (pReq==sqlite3FindCollSeq(db, ENC(db), pIdx->azColl[0], -1, 0))
1708 && (!mustBeUnique || (pIdx->nColumn==1 && pIdx->onError!=OE_None))
1709 ){
1710 int iDb;
drh0a07c102008-01-03 18:03:08 +00001711 int iMem = ++pParse->nMem;
danielk19779a96b662007-11-29 17:05:18 +00001712 int iAddr;
1713 char *pKey;
1714
1715 pKey = (char *)sqlite3IndexKeyinfo(pParse, pIdx);
1716 iDb = sqlite3SchemaToIndex(db, pIdx->pSchema);
1717 sqlite3VdbeUsesBtree(v, iDb);
1718
drh892d3172008-01-10 03:46:36 +00001719 iAddr = sqlite3VdbeAddOp1(v, OP_If, iMem);
drh4c583122008-01-04 22:01:03 +00001720 sqlite3VdbeAddOp2(v, OP_Integer, 1, iMem);
danielk19779a96b662007-11-29 17:05:18 +00001721
danielk1977cd3e8f72008-03-25 09:47:35 +00001722 sqlite3VdbeAddOp2(v, OP_SetNumColumns, 0, pIdx->nColumn);
danielk1977207872a2008-01-03 07:54:23 +00001723 sqlite3VdbeAddOp4(v, OP_OpenRead, iTab, pIdx->tnum, iDb,
drh66a51672008-01-03 00:01:23 +00001724 pKey,P4_KEYINFO_HANDOFF);
danielk1977207872a2008-01-03 07:54:23 +00001725 VdbeComment((v, "%s", pIdx->zName));
danielk19779a96b662007-11-29 17:05:18 +00001726 eType = IN_INDEX_INDEX;
danielk19779a96b662007-11-29 17:05:18 +00001727
1728 sqlite3VdbeJumpHere(v, iAddr);
1729 }
1730 }
1731 }
1732 }
1733
1734 if( eType==0 ){
1735 sqlite3CodeSubselect(pParse, pX);
1736 eType = IN_INDEX_EPH;
1737 }else{
1738 pX->iTable = iTab;
1739 }
1740 return eType;
1741}
danielk1977284f4ac2007-12-10 05:03:46 +00001742#endif
drh626a8792005-01-17 22:08:19 +00001743
1744/*
drh9cbe6352005-11-29 03:13:21 +00001745** Generate code for scalar subqueries used as an expression
1746** and IN operators. Examples:
drh626a8792005-01-17 22:08:19 +00001747**
drh9cbe6352005-11-29 03:13:21 +00001748** (SELECT a FROM b) -- subquery
1749** EXISTS (SELECT a FROM b) -- EXISTS subquery
1750** x IN (4,5,11) -- IN operator with list on right-hand side
1751** x IN (SELECT a FROM b) -- IN operator with subquery on the right
drhfef52082000-06-06 01:50:43 +00001752**
drh9cbe6352005-11-29 03:13:21 +00001753** The pExpr parameter describes the expression that contains the IN
1754** operator or subquery.
drhcce7d172000-05-31 15:34:51 +00001755*/
drh51522cd2005-01-20 13:36:19 +00001756#ifndef SQLITE_OMIT_SUBQUERY
danielk1977b3bce662005-01-29 08:32:43 +00001757void sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){
drh57dbd7b2005-07-08 18:25:26 +00001758 int testAddr = 0; /* One-time test address */
danielk1977b3bce662005-01-29 08:32:43 +00001759 Vdbe *v = sqlite3GetVdbe(pParse);
1760 if( v==0 ) return;
1761
danielk1977fc976062007-05-10 10:46:56 +00001762
drh57dbd7b2005-07-08 18:25:26 +00001763 /* This code must be run in its entirety every time it is encountered
1764 ** if any of the following is true:
1765 **
1766 ** * The right-hand side is a correlated subquery
1767 ** * The right-hand side is an expression list containing variables
1768 ** * We are inside a trigger
1769 **
1770 ** If all of the above are false, then we can run this code just once
1771 ** save the results, and reuse the same result on subsequent invocations.
danielk1977b3bce662005-01-29 08:32:43 +00001772 */
1773 if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->trigStack ){
drh0a07c102008-01-03 18:03:08 +00001774 int mem = ++pParse->nMem;
drh892d3172008-01-10 03:46:36 +00001775 sqlite3VdbeAddOp1(v, OP_If, mem);
1776 testAddr = sqlite3VdbeAddOp2(v, OP_Integer, 1, mem);
drh17435752007-08-16 04:30:38 +00001777 assert( testAddr>0 || pParse->db->mallocFailed );
danielk1977b3bce662005-01-29 08:32:43 +00001778 }
1779
drhcce7d172000-05-31 15:34:51 +00001780 switch( pExpr->op ){
drhfef52082000-06-06 01:50:43 +00001781 case TK_IN: {
danielk1977e014a832004-05-17 10:48:57 +00001782 char affinity;
drhd3d39e92004-05-20 22:16:29 +00001783 KeyInfo keyInfo;
drhb9bb7c12006-06-11 23:41:55 +00001784 int addr; /* Address of OP_OpenEphemeral instruction */
drhd3d39e92004-05-20 22:16:29 +00001785
danielk1977bf3b7212004-05-18 10:06:24 +00001786 affinity = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +00001787
1788 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
drh57dbd7b2005-07-08 18:25:26 +00001789 ** expression it is handled the same way. A virtual table is
danielk1977e014a832004-05-17 10:48:57 +00001790 ** filled with single-field index keys representing the results
1791 ** from the SELECT or the <exprlist>.
1792 **
1793 ** If the 'x' expression is a column value, or the SELECT...
1794 ** statement returns a column value, then the affinity of that
1795 ** column is used to build the index keys. If both 'x' and the
1796 ** SELECT... statement are columns, then numeric affinity is used
1797 ** if either column has NUMERIC or INTEGER affinity. If neither
1798 ** 'x' nor the SELECT... statement are columns, then numeric affinity
1799 ** is used.
1800 */
1801 pExpr->iTable = pParse->nTab++;
danielk1977cd3e8f72008-03-25 09:47:35 +00001802 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pExpr->iTable, 1);
drhd3d39e92004-05-20 22:16:29 +00001803 memset(&keyInfo, 0, sizeof(keyInfo));
1804 keyInfo.nField = 1;
danielk1977e014a832004-05-17 10:48:57 +00001805
drhfef52082000-06-06 01:50:43 +00001806 if( pExpr->pSelect ){
1807 /* Case 1: expr IN (SELECT ...)
1808 **
danielk1977e014a832004-05-17 10:48:57 +00001809 ** Generate code to write the results of the select into the temporary
1810 ** table allocated and opened above.
drhfef52082000-06-06 01:50:43 +00001811 */
drh1013c932008-01-06 00:25:21 +00001812 SelectDest dest;
drhbe5c89a2004-07-26 00:31:09 +00001813 ExprList *pEList;
drh1013c932008-01-06 00:25:21 +00001814
1815 sqlite3SelectDestInit(&dest, SRT_Set, pExpr->iTable);
1816 dest.affinity = (int)affinity;
danielk1977e014a832004-05-17 10:48:57 +00001817 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
danielk19776c8c8ce2008-01-02 16:27:09 +00001818 if( sqlite3Select(pParse, pExpr->pSelect, &dest, 0, 0, 0, 0) ){
drh94ccde52007-04-13 16:06:32 +00001819 return;
1820 }
drhbe5c89a2004-07-26 00:31:09 +00001821 pEList = pExpr->pSelect->pEList;
1822 if( pEList && pEList->nExpr>0 ){
danielk1977bcbb04e2007-05-29 12:11:29 +00001823 keyInfo.aColl[0] = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft,
drhbe5c89a2004-07-26 00:31:09 +00001824 pEList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001825 }
drhfef52082000-06-06 01:50:43 +00001826 }else if( pExpr->pList ){
1827 /* Case 2: expr IN (exprlist)
1828 **
drhfd131da2007-08-07 17:13:03 +00001829 ** For each expression, build an index key from the evaluation and
danielk1977e014a832004-05-17 10:48:57 +00001830 ** store it in the temporary table. If <expr> is a column, then use
1831 ** that columns affinity when building index keys. If <expr> is not
1832 ** a column, use numeric affinity.
drhfef52082000-06-06 01:50:43 +00001833 */
danielk1977e014a832004-05-17 10:48:57 +00001834 int i;
drh57dbd7b2005-07-08 18:25:26 +00001835 ExprList *pList = pExpr->pList;
1836 struct ExprList_item *pItem;
drh2d401ab2008-01-10 23:50:11 +00001837 int r1, r2;
drh57dbd7b2005-07-08 18:25:26 +00001838
danielk1977e014a832004-05-17 10:48:57 +00001839 if( !affinity ){
drh8159a352006-05-23 23:22:29 +00001840 affinity = SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +00001841 }
danielk19770202b292004-06-09 09:55:16 +00001842 keyInfo.aColl[0] = pExpr->pLeft->pColl;
danielk1977e014a832004-05-17 10:48:57 +00001843
1844 /* Loop through each expression in <exprlist>. */
drh2d401ab2008-01-10 23:50:11 +00001845 r1 = sqlite3GetTempReg(pParse);
1846 r2 = sqlite3GetTempReg(pParse);
drh57dbd7b2005-07-08 18:25:26 +00001847 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
1848 Expr *pE2 = pItem->pExpr;
danielk1977e014a832004-05-17 10:48:57 +00001849
drh57dbd7b2005-07-08 18:25:26 +00001850 /* If the expression is not constant then we will need to
1851 ** disable the test that was generated above that makes sure
1852 ** this code only executes once. Because for a non-constant
1853 ** expression we need to rerun this code each time.
1854 */
drh892d3172008-01-10 03:46:36 +00001855 if( testAddr && !sqlite3ExprIsConstant(pE2) ){
1856 sqlite3VdbeChangeToNoop(v, testAddr-1, 2);
drh57dbd7b2005-07-08 18:25:26 +00001857 testAddr = 0;
drh4794b982000-06-06 13:54:14 +00001858 }
danielk1977e014a832004-05-17 10:48:57 +00001859
1860 /* Evaluate the expression and insert it into the temp table */
drhe55cbd72008-03-31 23:48:03 +00001861 pParse->disableColCache++;
drh2d401ab2008-01-10 23:50:11 +00001862 sqlite3ExprCode(pParse, pE2, r1);
drhc5499be2008-04-01 15:06:33 +00001863 assert( pParse->disableColCache>0 );
drhe55cbd72008-03-31 23:48:03 +00001864 pParse->disableColCache--;
drh1db639c2008-01-17 02:36:28 +00001865 sqlite3VdbeAddOp4(v, OP_MakeRecord, r1, 1, r2, &affinity, 1);
drhda250ea2008-04-01 05:07:14 +00001866 sqlite3ExprCacheAffinityChange(pParse, r1, 1);
drh2d401ab2008-01-10 23:50:11 +00001867 sqlite3VdbeAddOp2(v, OP_IdxInsert, pExpr->iTable, r2);
drhfef52082000-06-06 01:50:43 +00001868 }
drh2d401ab2008-01-10 23:50:11 +00001869 sqlite3ReleaseTempReg(pParse, r1);
1870 sqlite3ReleaseTempReg(pParse, r2);
drhfef52082000-06-06 01:50:43 +00001871 }
drh66a51672008-01-03 00:01:23 +00001872 sqlite3VdbeChangeP4(v, addr, (void *)&keyInfo, P4_KEYINFO);
danielk1977b3bce662005-01-29 08:32:43 +00001873 break;
drhfef52082000-06-06 01:50:43 +00001874 }
1875
drh51522cd2005-01-20 13:36:19 +00001876 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001877 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +00001878 /* This has to be a scalar SELECT. Generate code to put the
1879 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +00001880 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +00001881 */
drh2646da72005-12-09 20:02:05 +00001882 static const Token one = { (u8*)"1", 0, 1 };
drh51522cd2005-01-20 13:36:19 +00001883 Select *pSel;
danielk19776c8c8ce2008-01-02 16:27:09 +00001884 SelectDest dest;
drh1398ad32005-01-19 23:24:50 +00001885
drh51522cd2005-01-20 13:36:19 +00001886 pSel = pExpr->pSelect;
drh1013c932008-01-06 00:25:21 +00001887 sqlite3SelectDestInit(&dest, 0, ++pParse->nMem);
drh51522cd2005-01-20 13:36:19 +00001888 if( pExpr->op==TK_SELECT ){
danielk19776c8c8ce2008-01-02 16:27:09 +00001889 dest.eDest = SRT_Mem;
drh4c583122008-01-04 22:01:03 +00001890 sqlite3VdbeAddOp2(v, OP_Null, 0, dest.iParm);
drhd4e70eb2008-01-02 00:34:36 +00001891 VdbeComment((v, "Init subquery result"));
drh51522cd2005-01-20 13:36:19 +00001892 }else{
danielk19776c8c8ce2008-01-02 16:27:09 +00001893 dest.eDest = SRT_Exists;
drh4c583122008-01-04 22:01:03 +00001894 sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iParm);
drhd4e70eb2008-01-02 00:34:36 +00001895 VdbeComment((v, "Init EXISTS result"));
drh51522cd2005-01-20 13:36:19 +00001896 }
drhec7429a2005-10-06 16:53:14 +00001897 sqlite3ExprDelete(pSel->pLimit);
danielk1977a1644fd2007-08-29 12:31:25 +00001898 pSel->pLimit = sqlite3PExpr(pParse, TK_INTEGER, 0, 0, &one);
danielk19776c8c8ce2008-01-02 16:27:09 +00001899 if( sqlite3Select(pParse, pSel, &dest, 0, 0, 0, 0) ){
drh94ccde52007-04-13 16:06:32 +00001900 return;
1901 }
danielk19776c8c8ce2008-01-02 16:27:09 +00001902 pExpr->iColumn = dest.iParm;
danielk1977b3bce662005-01-29 08:32:43 +00001903 break;
drhcce7d172000-05-31 15:34:51 +00001904 }
1905 }
danielk1977b3bce662005-01-29 08:32:43 +00001906
drh57dbd7b2005-07-08 18:25:26 +00001907 if( testAddr ){
drh892d3172008-01-10 03:46:36 +00001908 sqlite3VdbeJumpHere(v, testAddr-1);
danielk1977b3bce662005-01-29 08:32:43 +00001909 }
danielk1977fc976062007-05-10 10:46:56 +00001910
danielk1977b3bce662005-01-29 08:32:43 +00001911 return;
drhcce7d172000-05-31 15:34:51 +00001912}
drh51522cd2005-01-20 13:36:19 +00001913#endif /* SQLITE_OMIT_SUBQUERY */
drhcce7d172000-05-31 15:34:51 +00001914
drhcce7d172000-05-31 15:34:51 +00001915/*
drh598f1342007-10-23 15:39:45 +00001916** Duplicate an 8-byte value
1917*/
1918static char *dup8bytes(Vdbe *v, const char *in){
1919 char *out = sqlite3DbMallocRaw(sqlite3VdbeDb(v), 8);
1920 if( out ){
1921 memcpy(out, in, 8);
1922 }
1923 return out;
1924}
1925
1926/*
1927** Generate an instruction that will put the floating point
drh9cbf3422008-01-17 16:22:13 +00001928** value described by z[0..n-1] into register iMem.
drh0cf19ed2007-10-23 18:55:48 +00001929**
1930** The z[] string will probably not be zero-terminated. But the
1931** z[n] character is guaranteed to be something that does not look
1932** like the continuation of the number.
drh598f1342007-10-23 15:39:45 +00001933*/
drh9de221d2008-01-05 06:51:30 +00001934static void codeReal(Vdbe *v, const char *z, int n, int negateFlag, int iMem){
drh598f1342007-10-23 15:39:45 +00001935 assert( z || v==0 || sqlite3VdbeDb(v)->mallocFailed );
1936 if( z ){
1937 double value;
1938 char *zV;
drh0cf19ed2007-10-23 18:55:48 +00001939 assert( !isdigit(z[n]) );
drh598f1342007-10-23 15:39:45 +00001940 sqlite3AtoF(z, &value);
drh2eaf93d2008-04-29 00:15:20 +00001941 if( sqlite3IsNaN(value) ){
1942 sqlite3VdbeAddOp2(v, OP_Null, 0, iMem);
1943 }else{
1944 if( negateFlag ) value = -value;
1945 zV = dup8bytes(v, (char*)&value);
1946 sqlite3VdbeAddOp4(v, OP_Real, 0, iMem, 0, zV, P4_REAL);
1947 }
drh598f1342007-10-23 15:39:45 +00001948 }
1949}
1950
1951
1952/*
drhfec19aa2004-05-19 20:41:03 +00001953** Generate an instruction that will put the integer describe by
drh9cbf3422008-01-17 16:22:13 +00001954** text z[0..n-1] into register iMem.
drh0cf19ed2007-10-23 18:55:48 +00001955**
1956** The z[] string will probably not be zero-terminated. But the
1957** z[n] character is guaranteed to be something that does not look
1958** like the continuation of the number.
drhfec19aa2004-05-19 20:41:03 +00001959*/
drh9de221d2008-01-05 06:51:30 +00001960static void codeInteger(Vdbe *v, const char *z, int n, int negFlag, int iMem){
drhabb6fca2007-08-16 12:24:01 +00001961 assert( z || v==0 || sqlite3VdbeDb(v)->mallocFailed );
danielk1977c9cf9012007-05-30 10:36:47 +00001962 if( z ){
1963 int i;
drh0cf19ed2007-10-23 18:55:48 +00001964 assert( !isdigit(z[n]) );
danielk1977c9cf9012007-05-30 10:36:47 +00001965 if( sqlite3GetInt32(z, &i) ){
drh9de221d2008-01-05 06:51:30 +00001966 if( negFlag ) i = -i;
1967 sqlite3VdbeAddOp2(v, OP_Integer, i, iMem);
1968 }else if( sqlite3FitsIn64Bits(z, negFlag) ){
drh598f1342007-10-23 15:39:45 +00001969 i64 value;
1970 char *zV;
1971 sqlite3Atoi64(z, &value);
drh9de221d2008-01-05 06:51:30 +00001972 if( negFlag ) value = -value;
drh598f1342007-10-23 15:39:45 +00001973 zV = dup8bytes(v, (char*)&value);
drh9de221d2008-01-05 06:51:30 +00001974 sqlite3VdbeAddOp4(v, OP_Int64, 0, iMem, 0, zV, P4_INT64);
danielk1977c9cf9012007-05-30 10:36:47 +00001975 }else{
drh9de221d2008-01-05 06:51:30 +00001976 codeReal(v, z, n, negFlag, iMem);
danielk1977c9cf9012007-05-30 10:36:47 +00001977 }
drhfec19aa2004-05-19 20:41:03 +00001978 }
1979}
1980
drh945498f2007-02-24 11:52:52 +00001981
1982/*
1983** Generate code that will extract the iColumn-th column from
drhe55cbd72008-03-31 23:48:03 +00001984** table pTab and store the column value in a register. An effort
1985** is made to store the column value in register iReg, but this is
1986** not guaranteed. The location of the column value is returned.
1987**
1988** There must be an open cursor to pTab in iTable when this routine
1989** is called. If iColumn<0 then code is generated that extracts the rowid.
drhda250ea2008-04-01 05:07:14 +00001990**
1991** This routine might attempt to reuse the value of the column that
1992** has already been loaded into a register. The value will always
1993** be used if it has not undergone any affinity changes. But if
1994** an affinity change has occurred, then the cached value will only be
1995** used if allowAffChng is true.
drh945498f2007-02-24 11:52:52 +00001996*/
drhe55cbd72008-03-31 23:48:03 +00001997int sqlite3ExprCodeGetColumn(
1998 Parse *pParse, /* Parsing and code generating context */
drh2133d822008-01-03 18:44:59 +00001999 Table *pTab, /* Description of the table we are reading from */
2000 int iColumn, /* Index of the table column */
2001 int iTable, /* The cursor pointing to the table */
drhda250ea2008-04-01 05:07:14 +00002002 int iReg, /* Store results here */
2003 int allowAffChng /* True if prior affinity changes are OK */
drh2133d822008-01-03 18:44:59 +00002004){
drhe55cbd72008-03-31 23:48:03 +00002005 Vdbe *v = pParse->pVdbe;
2006 int i;
drhda250ea2008-04-01 05:07:14 +00002007 struct yColCache *p;
drhe55cbd72008-03-31 23:48:03 +00002008
drhda250ea2008-04-01 05:07:14 +00002009 for(i=0, p=pParse->aColCache; i<pParse->nColCache; i++, p++){
2010 if( p->iTable==iTable && p->iColumn==iColumn
2011 && (!p->affChange || allowAffChng) ){
drhe55cbd72008-03-31 23:48:03 +00002012#if 0
2013 sqlite3VdbeAddOp0(v, OP_Noop);
drhda250ea2008-04-01 05:07:14 +00002014 VdbeComment((v, "OPT: tab%d.col%d -> r%d", iTable, iColumn, p->iReg));
drhe55cbd72008-03-31 23:48:03 +00002015#endif
drhda250ea2008-04-01 05:07:14 +00002016 return p->iReg;
drhe55cbd72008-03-31 23:48:03 +00002017 }
2018 }
2019 assert( v!=0 );
drh945498f2007-02-24 11:52:52 +00002020 if( iColumn<0 ){
2021 int op = (pTab && IsVirtual(pTab)) ? OP_VRowid : OP_Rowid;
drh2133d822008-01-03 18:44:59 +00002022 sqlite3VdbeAddOp2(v, op, iTable, iReg);
drh945498f2007-02-24 11:52:52 +00002023 }else if( pTab==0 ){
drh2133d822008-01-03 18:44:59 +00002024 sqlite3VdbeAddOp3(v, OP_Column, iTable, iColumn, iReg);
drh945498f2007-02-24 11:52:52 +00002025 }else{
2026 int op = IsVirtual(pTab) ? OP_VColumn : OP_Column;
drh2133d822008-01-03 18:44:59 +00002027 sqlite3VdbeAddOp3(v, op, iTable, iColumn, iReg);
drh945498f2007-02-24 11:52:52 +00002028 sqlite3ColumnDefault(v, pTab, iColumn);
2029#ifndef SQLITE_OMIT_FLOATING_POINT
2030 if( pTab->aCol[iColumn].affinity==SQLITE_AFF_REAL ){
drh2133d822008-01-03 18:44:59 +00002031 sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg);
drh945498f2007-02-24 11:52:52 +00002032 }
2033#endif
2034 }
drhe55cbd72008-03-31 23:48:03 +00002035 if( pParse->disableColCache==0 ){
2036 i = pParse->iColCache;
drhda250ea2008-04-01 05:07:14 +00002037 p = &pParse->aColCache[i];
2038 p->iTable = iTable;
2039 p->iColumn = iColumn;
2040 p->iReg = iReg;
drhc5499be2008-04-01 15:06:33 +00002041 p->affChange = 0;
drhe55cbd72008-03-31 23:48:03 +00002042 i++;
drh2f7794c2008-04-01 03:27:39 +00002043 if( i>=ArraySize(pParse->aColCache) ) i = 0;
drhe55cbd72008-03-31 23:48:03 +00002044 if( i>pParse->nColCache ) pParse->nColCache = i;
drh2f7794c2008-04-01 03:27:39 +00002045 pParse->iColCache = i;
drhe55cbd72008-03-31 23:48:03 +00002046 }
2047 return iReg;
2048}
2049
2050/*
drhe55cbd72008-03-31 23:48:03 +00002051** Clear all column cache entries associated with the vdbe
2052** cursor with cursor number iTable.
2053*/
2054void sqlite3ExprClearColumnCache(Parse *pParse, int iTable){
2055 if( iTable<0 ){
2056 pParse->nColCache = 0;
2057 pParse->iColCache = 0;
2058 }else{
2059 int i;
2060 for(i=0; i<pParse->nColCache; i++){
2061 if( pParse->aColCache[i].iTable==iTable ){
drhc5499be2008-04-01 15:06:33 +00002062 testcase( i==pParse->nColCache-1 );
drhe55cbd72008-03-31 23:48:03 +00002063 pParse->aColCache[i] = pParse->aColCache[--pParse->nColCache];
drhda250ea2008-04-01 05:07:14 +00002064 pParse->iColCache = pParse->nColCache;
drhe55cbd72008-03-31 23:48:03 +00002065 }
2066 }
drhe55cbd72008-03-31 23:48:03 +00002067 }
2068}
2069
2070/*
drhda250ea2008-04-01 05:07:14 +00002071** Record the fact that an affinity change has occurred on iCount
2072** registers starting with iStart.
drhe55cbd72008-03-31 23:48:03 +00002073*/
drhda250ea2008-04-01 05:07:14 +00002074void sqlite3ExprCacheAffinityChange(Parse *pParse, int iStart, int iCount){
2075 int iEnd = iStart + iCount - 1;
drhe55cbd72008-03-31 23:48:03 +00002076 int i;
2077 for(i=0; i<pParse->nColCache; i++){
2078 int r = pParse->aColCache[i].iReg;
drhda250ea2008-04-01 05:07:14 +00002079 if( r>=iStart && r<=iEnd ){
2080 pParse->aColCache[i].affChange = 1;
drhe55cbd72008-03-31 23:48:03 +00002081 }
2082 }
drhe55cbd72008-03-31 23:48:03 +00002083}
2084
2085/*
2086** Generate code to moves content from one register to another.
2087** Keep the column cache up-to-date.
2088*/
2089void sqlite3ExprCodeMove(Parse *pParse, int iFrom, int iTo){
2090 int i;
2091 if( iFrom==iTo ) return;
2092 sqlite3VdbeAddOp2(pParse->pVdbe, OP_Move, iFrom, iTo);
2093 for(i=0; i<pParse->nColCache; i++){
2094 if( pParse->aColCache[i].iReg==iFrom ){
2095 pParse->aColCache[i].iReg = iTo;
2096 }
2097 }
drh945498f2007-02-24 11:52:52 +00002098}
2099
drhfec19aa2004-05-19 20:41:03 +00002100/*
drh652fbf52008-04-01 01:42:41 +00002101** Return true if any register in the range iFrom..iTo (inclusive)
2102** is used as part of the column cache.
2103*/
2104static int usedAsColumnCache(Parse *pParse, int iFrom, int iTo){
2105 int i;
2106 for(i=0; i<pParse->nColCache; i++){
2107 int r = pParse->aColCache[i].iReg;
2108 if( r>=iFrom && r<=iTo ) return 1;
2109 }
2110 return 0;
2111}
2112
2113/*
2114** Theres is a value in register iCurrent. We ultimately want
2115** the value to be in register iTarget. It might be that
2116** iCurrent and iTarget are the same register.
2117**
2118** We are going to modify the value, so we need to make sure it
2119** is not a cached register. If iCurrent is a cached register,
2120** then try to move the value over to iTarget. If iTarget is a
2121** cached register, then clear the corresponding cache line.
2122**
2123** Return the register that the value ends up in.
2124*/
2125int sqlite3ExprWritableRegister(Parse *pParse, int iCurrent, int iTarget){
drhda250ea2008-04-01 05:07:14 +00002126 int i;
drh652fbf52008-04-01 01:42:41 +00002127 assert( pParse->pVdbe!=0 );
2128 if( !usedAsColumnCache(pParse, iCurrent, iCurrent) ){
2129 return iCurrent;
2130 }
drh2f7794c2008-04-01 03:27:39 +00002131 if( iCurrent!=iTarget ){
2132 sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, iCurrent, iTarget);
2133 }
drhda250ea2008-04-01 05:07:14 +00002134 for(i=0; i<pParse->nColCache; i++){
2135 if( pParse->aColCache[i].iReg==iTarget ){
2136 pParse->aColCache[i] = pParse->aColCache[--pParse->nColCache];
2137 pParse->iColCache = pParse->nColCache;
2138 }
2139 }
drh652fbf52008-04-01 01:42:41 +00002140 return iTarget;
2141}
2142
2143/*
drh191b54c2008-04-15 12:14:21 +00002144** If the last instruction coded is an ephemeral copy of any of
2145** the registers in the nReg registers beginning with iReg, then
2146** convert the last instruction from OP_SCopy to OP_Copy.
2147*/
2148void sqlite3ExprHardCopy(Parse *pParse, int iReg, int nReg){
2149 int addr;
2150 VdbeOp *pOp;
2151 Vdbe *v;
2152
2153 v = pParse->pVdbe;
2154 addr = sqlite3VdbeCurrentAddr(v);
2155 pOp = sqlite3VdbeGetOp(v, addr-1);
danielk1977d7eb2ed2008-04-24 12:36:35 +00002156 assert( pOp || pParse->db->mallocFailed );
2157 if( pOp && pOp->opcode==OP_SCopy && pOp->p1>=iReg && pOp->p1<iReg+nReg ){
drh191b54c2008-04-15 12:14:21 +00002158 pOp->opcode = OP_Copy;
2159 }
2160}
2161
2162/*
drhcce7d172000-05-31 15:34:51 +00002163** Generate code into the current Vdbe to evaluate the given
drh2dcef112008-01-12 19:03:48 +00002164** expression. Attempt to store the results in register "target".
2165** Return the register where results are stored.
drh389a1ad2008-01-03 23:44:53 +00002166**
drh2dcef112008-01-12 19:03:48 +00002167** With this routine, there is no guaranteed that results will
2168** be stored in target. The result might be stored in some other
2169** register if it is convenient to do so. The calling function
2170** must check the return code and move the results to the desired
2171** register.
drhcce7d172000-05-31 15:34:51 +00002172*/
drh678ccce2008-03-31 18:19:54 +00002173int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){
drh2dcef112008-01-12 19:03:48 +00002174 Vdbe *v = pParse->pVdbe; /* The VM under construction */
2175 int op; /* The opcode being coded */
2176 int inReg = target; /* Results stored in register inReg */
2177 int regFree1 = 0; /* If non-zero free this temporary register */
2178 int regFree2 = 0; /* If non-zero free this temporary register */
drh678ccce2008-03-31 18:19:54 +00002179 int r1, r2, r3, r4; /* Various register numbers */
drhffe07b22005-11-03 00:41:17 +00002180
drh389a1ad2008-01-03 23:44:53 +00002181 assert( v!=0 || pParse->db->mallocFailed );
drh9cbf3422008-01-17 16:22:13 +00002182 assert( target>0 && target<=pParse->nMem );
drh389a1ad2008-01-03 23:44:53 +00002183 if( v==0 ) return 0;
drh389a1ad2008-01-03 23:44:53 +00002184
2185 if( pExpr==0 ){
2186 op = TK_NULL;
2187 }else{
2188 op = pExpr->op;
2189 }
drhf2bc0132004-10-04 13:19:23 +00002190 switch( op ){
drh13449892005-09-07 21:22:45 +00002191 case TK_AGG_COLUMN: {
2192 AggInfo *pAggInfo = pExpr->pAggInfo;
2193 struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg];
2194 if( !pAggInfo->directMode ){
drh9de221d2008-01-05 06:51:30 +00002195 assert( pCol->iMem>0 );
2196 inReg = pCol->iMem;
drh13449892005-09-07 21:22:45 +00002197 break;
2198 }else if( pAggInfo->useSortingIdx ){
drh389a1ad2008-01-03 23:44:53 +00002199 sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdx,
2200 pCol->iSorterColumn, target);
drh13449892005-09-07 21:22:45 +00002201 break;
2202 }
2203 /* Otherwise, fall thru into the TK_COLUMN case */
2204 }
drh967e8b72000-06-21 13:59:10 +00002205 case TK_COLUMN: {
drhffe07b22005-11-03 00:41:17 +00002206 if( pExpr->iTable<0 ){
2207 /* This only happens when coding check constraints */
drhaa9b8962008-01-08 02:57:55 +00002208 assert( pParse->ckBase>0 );
2209 inReg = pExpr->iColumn + pParse->ckBase;
drhc4a3c772001-04-04 11:48:57 +00002210 }else{
drhc5499be2008-04-01 15:06:33 +00002211 testcase( (pExpr->flags & EP_AnyAff)!=0 );
drhe55cbd72008-03-31 23:48:03 +00002212 inReg = sqlite3ExprCodeGetColumn(pParse, pExpr->pTab,
drhda250ea2008-04-01 05:07:14 +00002213 pExpr->iColumn, pExpr->iTable, target,
2214 pExpr->flags & EP_AnyAff);
drh22827922000-06-06 17:27:05 +00002215 }
drhcce7d172000-05-31 15:34:51 +00002216 break;
2217 }
2218 case TK_INTEGER: {
drh9de221d2008-01-05 06:51:30 +00002219 codeInteger(v, (char*)pExpr->token.z, pExpr->token.n, 0, target);
drhfec19aa2004-05-19 20:41:03 +00002220 break;
2221 }
drh598f1342007-10-23 15:39:45 +00002222 case TK_FLOAT: {
drh9de221d2008-01-05 06:51:30 +00002223 codeReal(v, (char*)pExpr->token.z, pExpr->token.n, 0, target);
drh598f1342007-10-23 15:39:45 +00002224 break;
2225 }
drhfec19aa2004-05-19 20:41:03 +00002226 case TK_STRING: {
danielk19771e536952007-08-16 10:09:01 +00002227 sqlite3DequoteExpr(pParse->db, pExpr);
drh9de221d2008-01-05 06:51:30 +00002228 sqlite3VdbeAddOp4(v,OP_String8, 0, target, 0,
drh66a51672008-01-03 00:01:23 +00002229 (char*)pExpr->token.z, pExpr->token.n);
drhcce7d172000-05-31 15:34:51 +00002230 break;
2231 }
drhf0863fe2005-06-12 21:35:51 +00002232 case TK_NULL: {
drh9de221d2008-01-05 06:51:30 +00002233 sqlite3VdbeAddOp2(v, OP_Null, 0, target);
drhf0863fe2005-06-12 21:35:51 +00002234 break;
2235 }
danielk19775338a5f2005-01-20 13:03:10 +00002236#ifndef SQLITE_OMIT_BLOB_LITERAL
danielk1977c572ef72004-05-27 09:28:41 +00002237 case TK_BLOB: {
drh6c8c6ce2005-08-23 11:17:58 +00002238 int n;
2239 const char *z;
drhca48c902008-01-18 14:08:24 +00002240 char *zBlob;
2241 assert( pExpr->token.n>=3 );
2242 assert( pExpr->token.z[0]=='x' || pExpr->token.z[0]=='X' );
2243 assert( pExpr->token.z[1]=='\'' );
2244 assert( pExpr->token.z[pExpr->token.n-1]=='\'' );
drh6c8c6ce2005-08-23 11:17:58 +00002245 n = pExpr->token.n - 3;
drh2646da72005-12-09 20:02:05 +00002246 z = (char*)pExpr->token.z + 2;
drhca48c902008-01-18 14:08:24 +00002247 zBlob = sqlite3HexToBlob(sqlite3VdbeDb(v), z, n);
2248 sqlite3VdbeAddOp4(v, OP_Blob, n/2, target, 0, zBlob, P4_DYNAMIC);
danielk1977c572ef72004-05-27 09:28:41 +00002249 break;
2250 }
danielk19775338a5f2005-01-20 13:03:10 +00002251#endif
drh50457892003-09-06 01:10:47 +00002252 case TK_VARIABLE: {
drh9de221d2008-01-05 06:51:30 +00002253 sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iTable, target);
drh895d7472004-08-20 16:02:39 +00002254 if( pExpr->token.n>1 ){
drh66a51672008-01-03 00:01:23 +00002255 sqlite3VdbeChangeP4(v, -1, (char*)pExpr->token.z, pExpr->token.n);
drh895d7472004-08-20 16:02:39 +00002256 }
drh50457892003-09-06 01:10:47 +00002257 break;
2258 }
drh4e0cff62004-11-05 05:10:28 +00002259 case TK_REGISTER: {
drh9de221d2008-01-05 06:51:30 +00002260 inReg = pExpr->iTable;
drh4e0cff62004-11-05 05:10:28 +00002261 break;
2262 }
drh487e2622005-06-25 18:42:14 +00002263#ifndef SQLITE_OMIT_CAST
2264 case TK_CAST: {
2265 /* Expressions of the form: CAST(pLeft AS token) */
danielk1977f0113002006-01-24 12:09:17 +00002266 int aff, to_op;
drh2dcef112008-01-12 19:03:48 +00002267 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
drh8a512562005-11-14 22:29:05 +00002268 aff = sqlite3AffinityType(&pExpr->token);
danielk1977f0113002006-01-24 12:09:17 +00002269 to_op = aff - SQLITE_AFF_TEXT + OP_ToText;
2270 assert( to_op==OP_ToText || aff!=SQLITE_AFF_TEXT );
2271 assert( to_op==OP_ToBlob || aff!=SQLITE_AFF_NONE );
2272 assert( to_op==OP_ToNumeric || aff!=SQLITE_AFF_NUMERIC );
2273 assert( to_op==OP_ToInt || aff!=SQLITE_AFF_INTEGER );
2274 assert( to_op==OP_ToReal || aff!=SQLITE_AFF_REAL );
drhc5499be2008-04-01 15:06:33 +00002275 testcase( to_op==OP_ToText );
2276 testcase( to_op==OP_ToBlob );
2277 testcase( to_op==OP_ToNumeric );
2278 testcase( to_op==OP_ToInt );
2279 testcase( to_op==OP_ToReal );
drh2dcef112008-01-12 19:03:48 +00002280 sqlite3VdbeAddOp1(v, to_op, inReg);
drhc5499be2008-04-01 15:06:33 +00002281 testcase( usedAsColumnCache(pParse, inReg, inReg) );
drhb3843a82008-04-01 12:24:11 +00002282 sqlite3ExprCacheAffinityChange(pParse, inReg, 1);
drh487e2622005-06-25 18:42:14 +00002283 break;
2284 }
2285#endif /* SQLITE_OMIT_CAST */
drhc9b84a12002-06-20 11:36:48 +00002286 case TK_LT:
2287 case TK_LE:
2288 case TK_GT:
2289 case TK_GE:
2290 case TK_NE:
2291 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00002292 assert( TK_LT==OP_Lt );
2293 assert( TK_LE==OP_Le );
2294 assert( TK_GT==OP_Gt );
2295 assert( TK_GE==OP_Ge );
2296 assert( TK_EQ==OP_Eq );
2297 assert( TK_NE==OP_Ne );
drhc5499be2008-04-01 15:06:33 +00002298 testcase( op==TK_LT );
2299 testcase( op==TK_LE );
2300 testcase( op==TK_GT );
2301 testcase( op==TK_GE );
2302 testcase( op==TK_EQ );
2303 testcase( op==TK_NE );
drhda250ea2008-04-01 05:07:14 +00002304 codeCompareOperands(pParse, pExpr->pLeft, &r1, &regFree1,
2305 pExpr->pRight, &r2, &regFree2);
drh35573352008-01-08 23:54:25 +00002306 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
2307 r1, r2, inReg, SQLITE_STOREP2);
drhc5499be2008-04-01 15:06:33 +00002308 testcase( regFree1==0 );
2309 testcase( regFree2==0 );
danielk1977a37cdde2004-05-16 11:15:36 +00002310 break;
drhc9b84a12002-06-20 11:36:48 +00002311 }
drhcce7d172000-05-31 15:34:51 +00002312 case TK_AND:
2313 case TK_OR:
2314 case TK_PLUS:
2315 case TK_STAR:
2316 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00002317 case TK_REM:
2318 case TK_BITAND:
2319 case TK_BITOR:
drh17c40292004-07-21 02:53:29 +00002320 case TK_SLASH:
drhbf4133c2001-10-13 02:59:08 +00002321 case TK_LSHIFT:
drh855eb1c2004-08-31 13:45:11 +00002322 case TK_RSHIFT:
drh00400772000-06-16 20:51:26 +00002323 case TK_CONCAT: {
drhf2bc0132004-10-04 13:19:23 +00002324 assert( TK_AND==OP_And );
2325 assert( TK_OR==OP_Or );
2326 assert( TK_PLUS==OP_Add );
2327 assert( TK_MINUS==OP_Subtract );
2328 assert( TK_REM==OP_Remainder );
2329 assert( TK_BITAND==OP_BitAnd );
2330 assert( TK_BITOR==OP_BitOr );
2331 assert( TK_SLASH==OP_Divide );
2332 assert( TK_LSHIFT==OP_ShiftLeft );
2333 assert( TK_RSHIFT==OP_ShiftRight );
2334 assert( TK_CONCAT==OP_Concat );
drhc5499be2008-04-01 15:06:33 +00002335 testcase( op==TK_AND );
2336 testcase( op==TK_OR );
2337 testcase( op==TK_PLUS );
2338 testcase( op==TK_MINUS );
2339 testcase( op==TK_REM );
2340 testcase( op==TK_BITAND );
2341 testcase( op==TK_BITOR );
2342 testcase( op==TK_SLASH );
2343 testcase( op==TK_LSHIFT );
2344 testcase( op==TK_RSHIFT );
2345 testcase( op==TK_CONCAT );
drh2dcef112008-01-12 19:03:48 +00002346 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
2347 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
drh5b6afba2008-01-05 16:29:28 +00002348 sqlite3VdbeAddOp3(v, op, r2, r1, target);
drhc5499be2008-04-01 15:06:33 +00002349 testcase( regFree1==0 );
2350 testcase( regFree2==0 );
drh00400772000-06-16 20:51:26 +00002351 break;
2352 }
drhcce7d172000-05-31 15:34:51 +00002353 case TK_UMINUS: {
drhfec19aa2004-05-19 20:41:03 +00002354 Expr *pLeft = pExpr->pLeft;
2355 assert( pLeft );
2356 if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
2357 Token *p = &pLeft->token;
drhfec19aa2004-05-19 20:41:03 +00002358 if( pLeft->op==TK_FLOAT ){
drh9de221d2008-01-05 06:51:30 +00002359 codeReal(v, (char*)p->z, p->n, 1, target);
drhe6840902002-03-06 03:08:25 +00002360 }else{
drh9de221d2008-01-05 06:51:30 +00002361 codeInteger(v, (char*)p->z, p->n, 1, target);
drhe6840902002-03-06 03:08:25 +00002362 }
drh3c84ddf2008-01-09 02:15:38 +00002363 }else{
drh2dcef112008-01-12 19:03:48 +00002364 regFree1 = r1 = sqlite3GetTempReg(pParse);
drh3c84ddf2008-01-09 02:15:38 +00002365 sqlite3VdbeAddOp2(v, OP_Integer, 0, r1);
drhe55cbd72008-03-31 23:48:03 +00002366 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree2);
drh2dcef112008-01-12 19:03:48 +00002367 sqlite3VdbeAddOp3(v, OP_Subtract, r2, r1, target);
drhc5499be2008-04-01 15:06:33 +00002368 testcase( regFree2==0 );
drh6e142f52000-06-08 13:36:40 +00002369 }
drh3c84ddf2008-01-09 02:15:38 +00002370 inReg = target;
2371 break;
drh6e142f52000-06-08 13:36:40 +00002372 }
drhbf4133c2001-10-13 02:59:08 +00002373 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00002374 case TK_NOT: {
drhf2bc0132004-10-04 13:19:23 +00002375 assert( TK_BITNOT==OP_BitNot );
2376 assert( TK_NOT==OP_Not );
drhc5499be2008-04-01 15:06:33 +00002377 testcase( op==TK_BITNOT );
2378 testcase( op==TK_NOT );
drh2dcef112008-01-12 19:03:48 +00002379 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
drhc5499be2008-04-01 15:06:33 +00002380 testcase( inReg==target );
2381 testcase( usedAsColumnCache(pParse, inReg, inReg) );
drh652fbf52008-04-01 01:42:41 +00002382 inReg = sqlite3ExprWritableRegister(pParse, inReg, target);
drh2dcef112008-01-12 19:03:48 +00002383 sqlite3VdbeAddOp1(v, op, inReg);
drhcce7d172000-05-31 15:34:51 +00002384 break;
2385 }
2386 case TK_ISNULL:
2387 case TK_NOTNULL: {
drh6a288a32008-01-07 19:20:24 +00002388 int addr;
drhf2bc0132004-10-04 13:19:23 +00002389 assert( TK_ISNULL==OP_IsNull );
2390 assert( TK_NOTNULL==OP_NotNull );
drhc5499be2008-04-01 15:06:33 +00002391 testcase( op==TK_ISNULL );
2392 testcase( op==TK_NOTNULL );
drh9de221d2008-01-05 06:51:30 +00002393 sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
drh2dcef112008-01-12 19:03:48 +00002394 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
drhc5499be2008-04-01 15:06:33 +00002395 testcase( regFree1==0 );
drh2dcef112008-01-12 19:03:48 +00002396 addr = sqlite3VdbeAddOp1(v, op, r1);
drh9de221d2008-01-05 06:51:30 +00002397 sqlite3VdbeAddOp2(v, OP_AddImm, target, -1);
drh6a288a32008-01-07 19:20:24 +00002398 sqlite3VdbeJumpHere(v, addr);
drhf2bc0132004-10-04 13:19:23 +00002399 break;
drhcce7d172000-05-31 15:34:51 +00002400 }
drh22827922000-06-06 17:27:05 +00002401 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00002402 AggInfo *pInfo = pExpr->pAggInfo;
drh7e56e712005-11-16 12:53:15 +00002403 if( pInfo==0 ){
2404 sqlite3ErrorMsg(pParse, "misuse of aggregate: %T",
2405 &pExpr->span);
2406 }else{
drh9de221d2008-01-05 06:51:30 +00002407 inReg = pInfo->aFunc[pExpr->iAgg].iMem;
drh7e56e712005-11-16 12:53:15 +00002408 }
drh22827922000-06-06 17:27:05 +00002409 break;
2410 }
drhb71090f2005-05-23 17:26:51 +00002411 case TK_CONST_FUNC:
drhcce7d172000-05-31 15:34:51 +00002412 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00002413 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00002414 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00002415 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00002416 int nId;
2417 const char *zId;
drh13449892005-09-07 21:22:45 +00002418 int constMask = 0;
danielk1977682f68b2004-06-05 10:22:17 +00002419 int i;
drh17435752007-08-16 04:30:38 +00002420 sqlite3 *db = pParse->db;
2421 u8 enc = ENC(db);
danielk1977dc1bdc42004-06-11 10:51:27 +00002422 CollSeq *pColl = 0;
drh17435752007-08-16 04:30:38 +00002423
drhc5499be2008-04-01 15:06:33 +00002424 testcase( op==TK_CONST_FUNC );
2425 testcase( op==TK_FUNCTION );
drh2646da72005-12-09 20:02:05 +00002426 zId = (char*)pExpr->token.z;
drhb71090f2005-05-23 17:26:51 +00002427 nId = pExpr->token.n;
danielk1977d8123362004-06-12 09:25:12 +00002428 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0);
drh0bce8352002-02-28 00:41:10 +00002429 assert( pDef!=0 );
drh892d3172008-01-10 03:46:36 +00002430 if( pList ){
2431 nExpr = pList->nExpr;
drh2dcef112008-01-12 19:03:48 +00002432 r1 = sqlite3GetTempRange(pParse, nExpr);
drh191b54c2008-04-15 12:14:21 +00002433 sqlite3ExprCodeExprList(pParse, pList, r1, 1);
drh892d3172008-01-10 03:46:36 +00002434 }else{
drhd847eaa2008-01-17 17:15:56 +00002435 nExpr = r1 = 0;
drh892d3172008-01-10 03:46:36 +00002436 }
drhb7f6f682006-07-08 17:06:43 +00002437#ifndef SQLITE_OMIT_VIRTUALTABLE
drha43fa222006-07-08 18:41:37 +00002438 /* Possibly overload the function if the first argument is
2439 ** a virtual table column.
2440 **
2441 ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the
2442 ** second argument, not the first, as the argument to test to
2443 ** see if it is a column in a virtual table. This is done because
2444 ** the left operand of infix functions (the operand we want to
2445 ** control overloading) ends up as the second argument to the
2446 ** function. The expression "A glob B" is equivalent to
2447 ** "glob(B,A). We want to use the A in "A glob B" to test
2448 ** for function overloading. But we use the B term in "glob(B,A)".
2449 */
drh6a03a1c2006-07-08 18:34:59 +00002450 if( nExpr>=2 && (pExpr->flags & EP_InfixFunc) ){
drh17435752007-08-16 04:30:38 +00002451 pDef = sqlite3VtabOverloadFunction(db, pDef, nExpr, pList->a[1].pExpr);
drh6a03a1c2006-07-08 18:34:59 +00002452 }else if( nExpr>0 ){
drh17435752007-08-16 04:30:38 +00002453 pDef = sqlite3VtabOverloadFunction(db, pDef, nExpr, pList->a[0].pExpr);
drhb7f6f682006-07-08 17:06:43 +00002454 }
2455#endif
danielk1977682f68b2004-06-05 10:22:17 +00002456 for(i=0; i<nExpr && i<32; i++){
danielk1977d02eb1f2004-06-06 09:44:03 +00002457 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
drh13449892005-09-07 21:22:45 +00002458 constMask |= (1<<i);
danielk1977d02eb1f2004-06-06 09:44:03 +00002459 }
danielk1977dc1bdc42004-06-11 10:51:27 +00002460 if( pDef->needCollSeq && !pColl ){
2461 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
2462 }
2463 }
2464 if( pDef->needCollSeq ){
2465 if( !pColl ) pColl = pParse->db->pDfltColl;
drh66a51672008-01-03 00:01:23 +00002466 sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ);
danielk1977682f68b2004-06-05 10:22:17 +00002467 }
drh2dcef112008-01-12 19:03:48 +00002468 sqlite3VdbeAddOp4(v, OP_Function, constMask, r1, target,
drh66a51672008-01-03 00:01:23 +00002469 (char*)pDef, P4_FUNCDEF);
drh98757152008-01-09 23:04:12 +00002470 sqlite3VdbeChangeP5(v, nExpr);
drh2dcef112008-01-12 19:03:48 +00002471 if( nExpr ){
2472 sqlite3ReleaseTempRange(pParse, r1, nExpr);
2473 }
drhda250ea2008-04-01 05:07:14 +00002474 sqlite3ExprCacheAffinityChange(pParse, r1, nExpr);
drhcce7d172000-05-31 15:34:51 +00002475 break;
2476 }
drhfe2093d2005-01-20 22:48:47 +00002477#ifndef SQLITE_OMIT_SUBQUERY
2478 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00002479 case TK_SELECT: {
drhc5499be2008-04-01 15:06:33 +00002480 testcase( op==TK_EXISTS );
2481 testcase( op==TK_SELECT );
drh41714d62006-03-02 04:44:23 +00002482 if( pExpr->iColumn==0 ){
2483 sqlite3CodeSubselect(pParse, pExpr);
2484 }
drh9de221d2008-01-05 06:51:30 +00002485 inReg = pExpr->iColumn;
drh19a775c2000-06-05 18:54:46 +00002486 break;
2487 }
drhfef52082000-06-06 01:50:43 +00002488 case TK_IN: {
drh6a288a32008-01-07 19:20:24 +00002489 int j1, j2, j3, j4, j5;
drh94a11212004-09-25 13:12:14 +00002490 char affinity;
danielk19779a96b662007-11-29 17:05:18 +00002491 int eType;
danielk19779a96b662007-11-29 17:05:18 +00002492
2493 eType = sqlite3FindInIndex(pParse, pExpr, 0);
danielk1977e014a832004-05-17 10:48:57 +00002494
2495 /* Figure out the affinity to use to create a key from the results
2496 ** of the expression. affinityStr stores a static string suitable for
drh66a51672008-01-03 00:01:23 +00002497 ** P4 of OP_MakeRecord.
danielk1977e014a832004-05-17 10:48:57 +00002498 */
drh94a11212004-09-25 13:12:14 +00002499 affinity = comparisonAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +00002500
drh2dcef112008-01-12 19:03:48 +00002501 sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
danielk1977e014a832004-05-17 10:48:57 +00002502
2503 /* Code the <expr> from "<expr> IN (...)". The temporary table
2504 ** pExpr->iTable contains the values that make up the (...) set.
2505 */
drh2dcef112008-01-12 19:03:48 +00002506 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
drhc5499be2008-04-01 15:06:33 +00002507 testcase( regFree1==0 );
drh2dcef112008-01-12 19:03:48 +00002508 j1 = sqlite3VdbeAddOp1(v, OP_NotNull, r1);
2509 sqlite3VdbeAddOp2(v, OP_Null, 0, target);
drh6a288a32008-01-07 19:20:24 +00002510 j2 = sqlite3VdbeAddOp0(v, OP_Goto);
2511 sqlite3VdbeJumpHere(v, j1);
danielk19779a96b662007-11-29 17:05:18 +00002512 if( eType==IN_INDEX_ROWID ){
drh678ccce2008-03-31 18:19:54 +00002513 j3 = sqlite3VdbeAddOp1(v, OP_MustBeInt, r1);
drh2dcef112008-01-12 19:03:48 +00002514 j4 = sqlite3VdbeAddOp3(v, OP_NotExists, pExpr->iTable, 0, r1);
drh6a288a32008-01-07 19:20:24 +00002515 j5 = sqlite3VdbeAddOp0(v, OP_Goto);
2516 sqlite3VdbeJumpHere(v, j3);
2517 sqlite3VdbeJumpHere(v, j4);
danielk19779a96b662007-11-29 17:05:18 +00002518 }else{
drh2dcef112008-01-12 19:03:48 +00002519 r2 = regFree2 = sqlite3GetTempReg(pParse);
drh1db639c2008-01-17 02:36:28 +00002520 sqlite3VdbeAddOp4(v, OP_MakeRecord, r1, 1, r2, &affinity, 1);
drhda250ea2008-04-01 05:07:14 +00002521 sqlite3ExprCacheAffinityChange(pParse, r1, 1);
drh2dcef112008-01-12 19:03:48 +00002522 j5 = sqlite3VdbeAddOp3(v, OP_Found, pExpr->iTable, 0, r2);
danielk19779a96b662007-11-29 17:05:18 +00002523 }
drh2dcef112008-01-12 19:03:48 +00002524 sqlite3VdbeAddOp2(v, OP_AddImm, target, -1);
drh6a288a32008-01-07 19:20:24 +00002525 sqlite3VdbeJumpHere(v, j2);
2526 sqlite3VdbeJumpHere(v, j5);
drhfef52082000-06-06 01:50:43 +00002527 break;
2528 }
danielk197793758c82005-01-21 08:13:14 +00002529#endif
drh2dcef112008-01-12 19:03:48 +00002530 /*
2531 ** x BETWEEN y AND z
2532 **
2533 ** This is equivalent to
2534 **
2535 ** x>=y AND x<=z
2536 **
2537 ** X is stored in pExpr->pLeft.
2538 ** Y is stored in pExpr->pList->a[0].pExpr.
2539 ** Z is stored in pExpr->pList->a[1].pExpr.
2540 */
drhfef52082000-06-06 01:50:43 +00002541 case TK_BETWEEN: {
drhbe5c89a2004-07-26 00:31:09 +00002542 Expr *pLeft = pExpr->pLeft;
2543 struct ExprList_item *pLItem = pExpr->pList->a;
2544 Expr *pRight = pLItem->pExpr;
drh35573352008-01-08 23:54:25 +00002545
drhda250ea2008-04-01 05:07:14 +00002546 codeCompareOperands(pParse, pLeft, &r1, &regFree1,
2547 pRight, &r2, &regFree2);
drhc5499be2008-04-01 15:06:33 +00002548 testcase( regFree1==0 );
2549 testcase( regFree2==0 );
drh2dcef112008-01-12 19:03:48 +00002550 r3 = sqlite3GetTempReg(pParse);
drh678ccce2008-03-31 18:19:54 +00002551 r4 = sqlite3GetTempReg(pParse);
drh35573352008-01-08 23:54:25 +00002552 codeCompare(pParse, pLeft, pRight, OP_Ge,
2553 r1, r2, r3, SQLITE_STOREP2);
drhbe5c89a2004-07-26 00:31:09 +00002554 pLItem++;
2555 pRight = pLItem->pExpr;
drh2dcef112008-01-12 19:03:48 +00002556 sqlite3ReleaseTempReg(pParse, regFree2);
2557 r2 = sqlite3ExprCodeTemp(pParse, pRight, &regFree2);
drhc5499be2008-04-01 15:06:33 +00002558 testcase( regFree2==0 );
drh678ccce2008-03-31 18:19:54 +00002559 codeCompare(pParse, pLeft, pRight, OP_Le, r1, r2, r4, SQLITE_STOREP2);
2560 sqlite3VdbeAddOp3(v, OP_And, r3, r4, target);
drh2dcef112008-01-12 19:03:48 +00002561 sqlite3ReleaseTempReg(pParse, r3);
drh678ccce2008-03-31 18:19:54 +00002562 sqlite3ReleaseTempReg(pParse, r4);
drhfef52082000-06-06 01:50:43 +00002563 break;
2564 }
drh4f07e5f2007-05-14 11:34:46 +00002565 case TK_UPLUS: {
drh2dcef112008-01-12 19:03:48 +00002566 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
drha2e00042002-01-22 03:13:42 +00002567 break;
2568 }
drh2dcef112008-01-12 19:03:48 +00002569
2570 /*
2571 ** Form A:
2572 ** CASE x WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
2573 **
2574 ** Form B:
2575 ** CASE WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
2576 **
2577 ** Form A is can be transformed into the equivalent form B as follows:
2578 ** CASE WHEN x=e1 THEN r1 WHEN x=e2 THEN r2 ...
2579 ** WHEN x=eN THEN rN ELSE y END
2580 **
2581 ** X (if it exists) is in pExpr->pLeft.
2582 ** Y is in pExpr->pRight. The Y is also optional. If there is no
2583 ** ELSE clause and no other term matches, then the result of the
2584 ** exprssion is NULL.
2585 ** Ei is in pExpr->pList->a[i*2] and Ri is pExpr->pList->a[i*2+1].
2586 **
2587 ** The result of the expression is the Ri for the first matching Ei,
2588 ** or if there is no matching Ei, the ELSE term Y, or if there is
2589 ** no ELSE term, NULL.
2590 */
drh17a7f8d2002-03-24 13:13:27 +00002591 case TK_CASE: {
drh2dcef112008-01-12 19:03:48 +00002592 int endLabel; /* GOTO label for end of CASE stmt */
2593 int nextCase; /* GOTO label for next WHEN clause */
2594 int nExpr; /* 2x number of WHEN terms */
2595 int i; /* Loop counter */
2596 ExprList *pEList; /* List of WHEN terms */
2597 struct ExprList_item *aListelem; /* Array of WHEN terms */
2598 Expr opCompare; /* The X==Ei expression */
2599 Expr cacheX; /* Cached expression X */
2600 Expr *pX; /* The X expression */
2601 Expr *pTest; /* X==Ei (form A) or just Ei (form B) */
drh17a7f8d2002-03-24 13:13:27 +00002602
2603 assert(pExpr->pList);
2604 assert((pExpr->pList->nExpr % 2) == 0);
2605 assert(pExpr->pList->nExpr > 0);
drhbe5c89a2004-07-26 00:31:09 +00002606 pEList = pExpr->pList;
2607 aListelem = pEList->a;
2608 nExpr = pEList->nExpr;
drh2dcef112008-01-12 19:03:48 +00002609 endLabel = sqlite3VdbeMakeLabel(v);
2610 if( (pX = pExpr->pLeft)!=0 ){
2611 cacheX = *pX;
drhc5499be2008-04-01 15:06:33 +00002612 testcase( pX->op==TK_COLUMN || pX->op==TK_REGISTER );
drh2dcef112008-01-12 19:03:48 +00002613 cacheX.iTable = sqlite3ExprCodeTemp(pParse, pX, &regFree1);
drhc5499be2008-04-01 15:06:33 +00002614 testcase( regFree1==0 );
drh2dcef112008-01-12 19:03:48 +00002615 cacheX.op = TK_REGISTER;
drh678ccce2008-03-31 18:19:54 +00002616 cacheX.iColumn = 0;
drh2dcef112008-01-12 19:03:48 +00002617 opCompare.op = TK_EQ;
2618 opCompare.pLeft = &cacheX;
2619 pTest = &opCompare;
drh17a7f8d2002-03-24 13:13:27 +00002620 }
drhc5499be2008-04-01 15:06:33 +00002621 pParse->disableColCache++;
drhf5905aa2002-05-26 20:54:33 +00002622 for(i=0; i<nExpr; i=i+2){
drh2dcef112008-01-12 19:03:48 +00002623 if( pX ){
2624 opCompare.pRight = aListelem[i].pExpr;
drh17a7f8d2002-03-24 13:13:27 +00002625 }else{
drh2dcef112008-01-12 19:03:48 +00002626 pTest = aListelem[i].pExpr;
drh17a7f8d2002-03-24 13:13:27 +00002627 }
drh2dcef112008-01-12 19:03:48 +00002628 nextCase = sqlite3VdbeMakeLabel(v);
drhc5499be2008-04-01 15:06:33 +00002629 testcase( pTest->op==TK_COLUMN || pTest->op==TK_REGISTER );
drh2dcef112008-01-12 19:03:48 +00002630 sqlite3ExprIfFalse(pParse, pTest, nextCase, SQLITE_JUMPIFNULL);
drhc5499be2008-04-01 15:06:33 +00002631 testcase( aListelem[i+1].pExpr->op==TK_COLUMN );
2632 testcase( aListelem[i+1].pExpr->op==TK_REGISTER );
drh9de221d2008-01-05 06:51:30 +00002633 sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target);
drh2dcef112008-01-12 19:03:48 +00002634 sqlite3VdbeAddOp2(v, OP_Goto, 0, endLabel);
2635 sqlite3VdbeResolveLabel(v, nextCase);
drhf570f012002-05-31 15:51:25 +00002636 }
drh17a7f8d2002-03-24 13:13:27 +00002637 if( pExpr->pRight ){
drh9de221d2008-01-05 06:51:30 +00002638 sqlite3ExprCode(pParse, pExpr->pRight, target);
drh17a7f8d2002-03-24 13:13:27 +00002639 }else{
drh9de221d2008-01-05 06:51:30 +00002640 sqlite3VdbeAddOp2(v, OP_Null, 0, target);
drh17a7f8d2002-03-24 13:13:27 +00002641 }
drh2dcef112008-01-12 19:03:48 +00002642 sqlite3VdbeResolveLabel(v, endLabel);
drhc5499be2008-04-01 15:06:33 +00002643 assert( pParse->disableColCache>0 );
2644 pParse->disableColCache--;
danielk19776f349032002-06-11 02:25:40 +00002645 break;
2646 }
danielk19775338a5f2005-01-20 13:03:10 +00002647#ifndef SQLITE_OMIT_TRIGGER
danielk19776f349032002-06-11 02:25:40 +00002648 case TK_RAISE: {
2649 if( !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +00002650 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00002651 "RAISE() may only be used within a trigger-program");
drh389a1ad2008-01-03 23:44:53 +00002652 return 0;
danielk19776f349032002-06-11 02:25:40 +00002653 }
drhad6d9462004-09-19 02:15:24 +00002654 if( pExpr->iColumn!=OE_Ignore ){
2655 assert( pExpr->iColumn==OE_Rollback ||
2656 pExpr->iColumn == OE_Abort ||
2657 pExpr->iColumn == OE_Fail );
danielk19771e536952007-08-16 10:09:01 +00002658 sqlite3DequoteExpr(pParse->db, pExpr);
drh66a51672008-01-03 00:01:23 +00002659 sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn, 0,
drh2646da72005-12-09 20:02:05 +00002660 (char*)pExpr->token.z, pExpr->token.n);
danielk19776f349032002-06-11 02:25:40 +00002661 } else {
drhad6d9462004-09-19 02:15:24 +00002662 assert( pExpr->iColumn == OE_Ignore );
drh66a51672008-01-03 00:01:23 +00002663 sqlite3VdbeAddOp2(v, OP_ContextPop, 0, 0);
2664 sqlite3VdbeAddOp2(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
drhd4e70eb2008-01-02 00:34:36 +00002665 VdbeComment((v, "raise(IGNORE)"));
danielk19776f349032002-06-11 02:25:40 +00002666 }
drhffe07b22005-11-03 00:41:17 +00002667 break;
drh17a7f8d2002-03-24 13:13:27 +00002668 }
danielk19775338a5f2005-01-20 13:03:10 +00002669#endif
drhffe07b22005-11-03 00:41:17 +00002670 }
drh2dcef112008-01-12 19:03:48 +00002671 sqlite3ReleaseTempReg(pParse, regFree1);
2672 sqlite3ReleaseTempReg(pParse, regFree2);
2673 return inReg;
2674}
2675
2676/*
2677** Generate code to evaluate an expression and store the results
2678** into a register. Return the register number where the results
2679** are stored.
2680**
2681** If the register is a temporary register that can be deallocated,
drh678ccce2008-03-31 18:19:54 +00002682** then write its number into *pReg. If the result register is not
drh2dcef112008-01-12 19:03:48 +00002683** a temporary, then set *pReg to zero.
2684*/
2685int sqlite3ExprCodeTemp(Parse *pParse, Expr *pExpr, int *pReg){
2686 int r1 = sqlite3GetTempReg(pParse);
2687 int r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
2688 if( r2==r1 ){
2689 *pReg = r1;
2690 }else{
2691 sqlite3ReleaseTempReg(pParse, r1);
2692 *pReg = 0;
2693 }
2694 return r2;
2695}
2696
2697/*
2698** Generate code that will evaluate expression pExpr and store the
2699** results in register target. The results are guaranteed to appear
2700** in register target.
2701*/
2702int sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){
drh9cbf3422008-01-17 16:22:13 +00002703 int inReg;
2704
2705 assert( target>0 && target<=pParse->nMem );
2706 inReg = sqlite3ExprCodeTarget(pParse, pExpr, target);
drh0e359b32008-01-13 19:02:11 +00002707 assert( pParse->pVdbe || pParse->db->mallocFailed );
2708 if( inReg!=target && pParse->pVdbe ){
drh9cbf3422008-01-17 16:22:13 +00002709 sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, inReg, target);
drhcce7d172000-05-31 15:34:51 +00002710 }
drh389a1ad2008-01-03 23:44:53 +00002711 return target;
drhcce7d172000-05-31 15:34:51 +00002712}
2713
2714/*
drh2dcef112008-01-12 19:03:48 +00002715** Generate code that evalutes the given expression and puts the result
drhde4fcfd2008-01-19 23:50:26 +00002716** in register target.
drh25303782004-12-07 15:41:48 +00002717**
drh2dcef112008-01-12 19:03:48 +00002718** Also make a copy of the expression results into another "cache" register
2719** and modify the expression so that the next time it is evaluated,
2720** the result is a copy of the cache register.
2721**
2722** This routine is used for expressions that are used multiple
2723** times. They are evaluated once and the results of the expression
2724** are reused.
drh25303782004-12-07 15:41:48 +00002725*/
drh2dcef112008-01-12 19:03:48 +00002726int sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr, int target){
drh25303782004-12-07 15:41:48 +00002727 Vdbe *v = pParse->pVdbe;
drh2dcef112008-01-12 19:03:48 +00002728 int inReg;
2729 inReg = sqlite3ExprCode(pParse, pExpr, target);
drhde4fcfd2008-01-19 23:50:26 +00002730 assert( target>0 );
drh2dcef112008-01-12 19:03:48 +00002731 if( pExpr->op!=TK_REGISTER ){
2732 int iMem;
drhde4fcfd2008-01-19 23:50:26 +00002733 iMem = ++pParse->nMem;
2734 sqlite3VdbeAddOp2(v, OP_Copy, inReg, iMem);
drh2dcef112008-01-12 19:03:48 +00002735 pExpr->iTable = iMem;
drh678ccce2008-03-31 18:19:54 +00002736 pExpr->iColumn = pExpr->op;
drh25303782004-12-07 15:41:48 +00002737 pExpr->op = TK_REGISTER;
2738 }
drh2dcef112008-01-12 19:03:48 +00002739 return inReg;
drh25303782004-12-07 15:41:48 +00002740}
drh2dcef112008-01-12 19:03:48 +00002741
drh678ccce2008-03-31 18:19:54 +00002742/*
drh47de9552008-04-01 18:04:11 +00002743** Return TRUE if pExpr is an constant expression that is appropriate
2744** for factoring out of a loop. Appropriate expressions are:
2745**
2746** * Any expression that evaluates to two or more opcodes.
2747**
2748** * Any OP_Integer, OP_Real, OP_String, OP_Blob, OP_Null,
2749** or OP_Variable that does not need to be placed in a
2750** specific register.
2751**
2752** There is no point in factoring out single-instruction constant
2753** expressions that need to be placed in a particular register.
2754** We could factor them out, but then we would end up adding an
2755** OP_SCopy instruction to move the value into the correct register
2756** later. We might as well just use the original instruction and
2757** avoid the OP_SCopy.
2758*/
2759static int isAppropriateForFactoring(Expr *p){
2760 if( !sqlite3ExprIsConstantNotJoin(p) ){
2761 return 0; /* Only constant expressions are appropriate for factoring */
2762 }
2763 if( (p->flags & EP_FixedDest)==0 ){
2764 return 1; /* Any constant without a fixed destination is appropriate */
2765 }
2766 while( p->op==TK_UPLUS ) p = p->pLeft;
2767 switch( p->op ){
2768#ifndef SQLITE_OMIT_BLOB_LITERAL
2769 case TK_BLOB:
2770#endif
2771 case TK_VARIABLE:
2772 case TK_INTEGER:
2773 case TK_FLOAT:
2774 case TK_NULL:
2775 case TK_STRING: {
2776 testcase( p->op==TK_BLOB );
2777 testcase( p->op==TK_VARIABLE );
2778 testcase( p->op==TK_INTEGER );
2779 testcase( p->op==TK_FLOAT );
2780 testcase( p->op==TK_NULL );
2781 testcase( p->op==TK_STRING );
2782 /* Single-instruction constants with a fixed destination are
2783 ** better done in-line. If we factor them, they will just end
2784 ** up generating an OP_SCopy to move the value to the destination
2785 ** register. */
2786 return 0;
2787 }
2788 case TK_UMINUS: {
2789 if( p->pLeft->op==TK_FLOAT || p->pLeft->op==TK_INTEGER ){
2790 return 0;
2791 }
2792 break;
2793 }
2794 default: {
2795 break;
2796 }
2797 }
2798 return 1;
2799}
2800
2801/*
2802** If pExpr is a constant expression that is appropriate for
2803** factoring out of a loop, then evaluate the expression
drh678ccce2008-03-31 18:19:54 +00002804** into a register and convert the expression into a TK_REGISTER
2805** expression.
2806*/
2807static int evalConstExpr(void *pArg, Expr *pExpr){
2808 Parse *pParse = (Parse*)pArg;
drh47de9552008-04-01 18:04:11 +00002809 switch( pExpr->op ){
2810 case TK_REGISTER: {
2811 return 1;
2812 }
2813 case TK_FUNCTION:
2814 case TK_AGG_FUNCTION:
2815 case TK_CONST_FUNC: {
2816 /* The arguments to a function have a fixed destination.
2817 ** Mark them this way to avoid generated unneeded OP_SCopy
2818 ** instructions.
2819 */
2820 ExprList *pList = pExpr->pList;
2821 if( pList ){
2822 int i = pList->nExpr;
2823 struct ExprList_item *pItem = pList->a;
2824 for(; i>0; i--, pItem++){
2825 if( pItem->pExpr ) pItem->pExpr->flags |= EP_FixedDest;
2826 }
2827 }
2828 break;
2829 }
drh678ccce2008-03-31 18:19:54 +00002830 }
drh47de9552008-04-01 18:04:11 +00002831 if( isAppropriateForFactoring(pExpr) ){
drh678ccce2008-03-31 18:19:54 +00002832 int r1 = ++pParse->nMem;
2833 int r2;
2834 r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
drhc5499be2008-04-01 15:06:33 +00002835 if( r1!=r2 ) sqlite3ReleaseTempReg(pParse, r1);
drh678ccce2008-03-31 18:19:54 +00002836 pExpr->iColumn = pExpr->op;
2837 pExpr->op = TK_REGISTER;
2838 pExpr->iTable = r2;
2839 return 1;
2840 }
2841 return 0;
2842}
2843
2844/*
2845** Preevaluate constant subexpressions within pExpr and store the
2846** results in registers. Modify pExpr so that the constant subexpresions
2847** are TK_REGISTER opcodes that refer to the precomputed values.
2848*/
2849void sqlite3ExprCodeConstants(Parse *pParse, Expr *pExpr){
2850 walkExprTree(pExpr, evalConstExpr, pParse);
2851}
2852
drh25303782004-12-07 15:41:48 +00002853
2854/*
drh268380c2004-02-25 13:47:31 +00002855** Generate code that pushes the value of every element of the given
drh9cbf3422008-01-17 16:22:13 +00002856** expression list into a sequence of registers beginning at target.
drh268380c2004-02-25 13:47:31 +00002857**
drh892d3172008-01-10 03:46:36 +00002858** Return the number of elements evaluated.
drh268380c2004-02-25 13:47:31 +00002859*/
danielk19774adee202004-05-08 08:23:19 +00002860int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00002861 Parse *pParse, /* Parsing context */
drh389a1ad2008-01-03 23:44:53 +00002862 ExprList *pList, /* The expression list to be coded */
drh191b54c2008-04-15 12:14:21 +00002863 int target, /* Where to write results */
2864 int doHardCopy /* Call sqlite3ExprHardCopy on each element if true */
drh268380c2004-02-25 13:47:31 +00002865){
2866 struct ExprList_item *pItem;
drh9cbf3422008-01-17 16:22:13 +00002867 int i, n;
drh892d3172008-01-10 03:46:36 +00002868 assert( pList!=0 || pParse->db->mallocFailed );
2869 if( pList==0 ){
2870 return 0;
2871 }
drh9cbf3422008-01-17 16:22:13 +00002872 assert( target>0 );
drh268380c2004-02-25 13:47:31 +00002873 n = pList->nExpr;
drh191b54c2008-04-15 12:14:21 +00002874 for(pItem=pList->a, i=0; i<n; i++, pItem++){
2875 sqlite3ExprCode(pParse, pItem->pExpr, target+i);
2876 if( doHardCopy ) sqlite3ExprHardCopy(pParse, target, n);
drh268380c2004-02-25 13:47:31 +00002877 }
drhf9b596e2004-05-26 16:54:42 +00002878 return n;
drh268380c2004-02-25 13:47:31 +00002879}
2880
2881/*
drhcce7d172000-05-31 15:34:51 +00002882** Generate code for a boolean expression such that a jump is made
2883** to the label "dest" if the expression is true but execution
2884** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00002885**
2886** If the expression evaluates to NULL (neither true nor false), then
drh35573352008-01-08 23:54:25 +00002887** take the jump if the jumpIfNull flag is SQLITE_JUMPIFNULL.
drhf2bc0132004-10-04 13:19:23 +00002888**
2889** This code depends on the fact that certain token values (ex: TK_EQ)
2890** are the same as opcode values (ex: OP_Eq) that implement the corresponding
2891** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
2892** the make process cause these values to align. Assert()s in the code
2893** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00002894*/
danielk19774adee202004-05-08 08:23:19 +00002895void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00002896 Vdbe *v = pParse->pVdbe;
2897 int op = 0;
drh2dcef112008-01-12 19:03:48 +00002898 int regFree1 = 0;
2899 int regFree2 = 0;
2900 int r1, r2;
2901
drh35573352008-01-08 23:54:25 +00002902 assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
drhdaffd0e2001-04-11 14:28:42 +00002903 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00002904 op = pExpr->op;
2905 switch( op ){
drhcce7d172000-05-31 15:34:51 +00002906 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00002907 int d2 = sqlite3VdbeMakeLabel(v);
drhc5499be2008-04-01 15:06:33 +00002908 testcase( jumpIfNull==0 );
2909 testcase( pParse->disableColCache==0 );
drh35573352008-01-08 23:54:25 +00002910 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,jumpIfNull^SQLITE_JUMPIFNULL);
drhe55cbd72008-03-31 23:48:03 +00002911 pParse->disableColCache++;
danielk19774adee202004-05-08 08:23:19 +00002912 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhc5499be2008-04-01 15:06:33 +00002913 assert( pParse->disableColCache>0 );
drhe55cbd72008-03-31 23:48:03 +00002914 pParse->disableColCache--;
danielk19774adee202004-05-08 08:23:19 +00002915 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00002916 break;
2917 }
2918 case TK_OR: {
drhc5499be2008-04-01 15:06:33 +00002919 testcase( jumpIfNull==0 );
2920 testcase( pParse->disableColCache==0 );
danielk19774adee202004-05-08 08:23:19 +00002921 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhe55cbd72008-03-31 23:48:03 +00002922 pParse->disableColCache++;
danielk19774adee202004-05-08 08:23:19 +00002923 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhc5499be2008-04-01 15:06:33 +00002924 assert( pParse->disableColCache>0 );
drhe55cbd72008-03-31 23:48:03 +00002925 pParse->disableColCache--;
drhcce7d172000-05-31 15:34:51 +00002926 break;
2927 }
2928 case TK_NOT: {
drhc5499be2008-04-01 15:06:33 +00002929 testcase( jumpIfNull==0 );
danielk19774adee202004-05-08 08:23:19 +00002930 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002931 break;
2932 }
2933 case TK_LT:
2934 case TK_LE:
2935 case TK_GT:
2936 case TK_GE:
2937 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00002938 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00002939 assert( TK_LT==OP_Lt );
2940 assert( TK_LE==OP_Le );
2941 assert( TK_GT==OP_Gt );
2942 assert( TK_GE==OP_Ge );
2943 assert( TK_EQ==OP_Eq );
2944 assert( TK_NE==OP_Ne );
drhc5499be2008-04-01 15:06:33 +00002945 testcase( op==TK_LT );
2946 testcase( op==TK_LE );
2947 testcase( op==TK_GT );
2948 testcase( op==TK_GE );
2949 testcase( op==TK_EQ );
2950 testcase( op==TK_NE );
2951 testcase( jumpIfNull==0 );
drhda250ea2008-04-01 05:07:14 +00002952 codeCompareOperands(pParse, pExpr->pLeft, &r1, &regFree1,
2953 pExpr->pRight, &r2, &regFree2);
drh35573352008-01-08 23:54:25 +00002954 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
drh2dcef112008-01-12 19:03:48 +00002955 r1, r2, dest, jumpIfNull);
drhc5499be2008-04-01 15:06:33 +00002956 testcase( regFree1==0 );
2957 testcase( regFree2==0 );
drhcce7d172000-05-31 15:34:51 +00002958 break;
2959 }
2960 case TK_ISNULL:
2961 case TK_NOTNULL: {
drhf2bc0132004-10-04 13:19:23 +00002962 assert( TK_ISNULL==OP_IsNull );
2963 assert( TK_NOTNULL==OP_NotNull );
drhc5499be2008-04-01 15:06:33 +00002964 testcase( op==TK_ISNULL );
2965 testcase( op==TK_NOTNULL );
drh2dcef112008-01-12 19:03:48 +00002966 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
2967 sqlite3VdbeAddOp2(v, op, r1, dest);
drhc5499be2008-04-01 15:06:33 +00002968 testcase( regFree1==0 );
drhcce7d172000-05-31 15:34:51 +00002969 break;
2970 }
drhfef52082000-06-06 01:50:43 +00002971 case TK_BETWEEN: {
drh2dcef112008-01-12 19:03:48 +00002972 /* x BETWEEN y AND z
danielk19770202b292004-06-09 09:55:16 +00002973 **
drh2dcef112008-01-12 19:03:48 +00002974 ** Is equivalent to
2975 **
2976 ** x>=y AND x<=z
2977 **
2978 ** Code it as such, taking care to do the common subexpression
2979 ** elementation of x.
danielk19770202b292004-06-09 09:55:16 +00002980 */
drh2dcef112008-01-12 19:03:48 +00002981 Expr exprAnd;
2982 Expr compLeft;
2983 Expr compRight;
2984 Expr exprX;
danielk19770202b292004-06-09 09:55:16 +00002985
drh2dcef112008-01-12 19:03:48 +00002986 exprX = *pExpr->pLeft;
2987 exprAnd.op = TK_AND;
2988 exprAnd.pLeft = &compLeft;
2989 exprAnd.pRight = &compRight;
2990 compLeft.op = TK_GE;
2991 compLeft.pLeft = &exprX;
2992 compLeft.pRight = pExpr->pList->a[0].pExpr;
2993 compRight.op = TK_LE;
2994 compRight.pLeft = &exprX;
2995 compRight.pRight = pExpr->pList->a[1].pExpr;
2996 exprX.iTable = sqlite3ExprCodeTemp(pParse, &exprX, &regFree1);
drhc5499be2008-04-01 15:06:33 +00002997 testcase( regFree1==0 );
drh2dcef112008-01-12 19:03:48 +00002998 exprX.op = TK_REGISTER;
drhc5499be2008-04-01 15:06:33 +00002999 testcase( jumpIfNull==0 );
drh2dcef112008-01-12 19:03:48 +00003000 sqlite3ExprIfTrue(pParse, &exprAnd, dest, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00003001 break;
3002 }
drhcce7d172000-05-31 15:34:51 +00003003 default: {
drh2dcef112008-01-12 19:03:48 +00003004 r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
3005 sqlite3VdbeAddOp3(v, OP_If, r1, dest, jumpIfNull!=0);
drhc5499be2008-04-01 15:06:33 +00003006 testcase( regFree1==0 );
3007 testcase( jumpIfNull==0 );
drhcce7d172000-05-31 15:34:51 +00003008 break;
3009 }
3010 }
drh2dcef112008-01-12 19:03:48 +00003011 sqlite3ReleaseTempReg(pParse, regFree1);
3012 sqlite3ReleaseTempReg(pParse, regFree2);
drhcce7d172000-05-31 15:34:51 +00003013}
3014
3015/*
drh66b89c82000-11-28 20:47:17 +00003016** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00003017** to the label "dest" if the expression is false but execution
3018** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00003019**
3020** If the expression evaluates to NULL (neither true nor false) then
drh35573352008-01-08 23:54:25 +00003021** jump if jumpIfNull is SQLITE_JUMPIFNULL or fall through if jumpIfNull
3022** is 0.
drhcce7d172000-05-31 15:34:51 +00003023*/
danielk19774adee202004-05-08 08:23:19 +00003024void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00003025 Vdbe *v = pParse->pVdbe;
3026 int op = 0;
drh2dcef112008-01-12 19:03:48 +00003027 int regFree1 = 0;
3028 int regFree2 = 0;
3029 int r1, r2;
3030
drh35573352008-01-08 23:54:25 +00003031 assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
drhdaffd0e2001-04-11 14:28:42 +00003032 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00003033
3034 /* The value of pExpr->op and op are related as follows:
3035 **
3036 ** pExpr->op op
3037 ** --------- ----------
3038 ** TK_ISNULL OP_NotNull
3039 ** TK_NOTNULL OP_IsNull
3040 ** TK_NE OP_Eq
3041 ** TK_EQ OP_Ne
3042 ** TK_GT OP_Le
3043 ** TK_LE OP_Gt
3044 ** TK_GE OP_Lt
3045 ** TK_LT OP_Ge
3046 **
3047 ** For other values of pExpr->op, op is undefined and unused.
3048 ** The value of TK_ and OP_ constants are arranged such that we
3049 ** can compute the mapping above using the following expression.
3050 ** Assert()s verify that the computation is correct.
3051 */
3052 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
3053
3054 /* Verify correct alignment of TK_ and OP_ constants
3055 */
3056 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
3057 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
3058 assert( pExpr->op!=TK_NE || op==OP_Eq );
3059 assert( pExpr->op!=TK_EQ || op==OP_Ne );
3060 assert( pExpr->op!=TK_LT || op==OP_Ge );
3061 assert( pExpr->op!=TK_LE || op==OP_Gt );
3062 assert( pExpr->op!=TK_GT || op==OP_Le );
3063 assert( pExpr->op!=TK_GE || op==OP_Lt );
3064
drhcce7d172000-05-31 15:34:51 +00003065 switch( pExpr->op ){
3066 case TK_AND: {
drhc5499be2008-04-01 15:06:33 +00003067 testcase( jumpIfNull==0 );
3068 testcase( pParse->disableColCache==0 );
danielk19774adee202004-05-08 08:23:19 +00003069 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhe55cbd72008-03-31 23:48:03 +00003070 pParse->disableColCache++;
danielk19774adee202004-05-08 08:23:19 +00003071 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhc5499be2008-04-01 15:06:33 +00003072 assert( pParse->disableColCache>0 );
drhe55cbd72008-03-31 23:48:03 +00003073 pParse->disableColCache--;
drhcce7d172000-05-31 15:34:51 +00003074 break;
3075 }
3076 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00003077 int d2 = sqlite3VdbeMakeLabel(v);
drhc5499be2008-04-01 15:06:33 +00003078 testcase( jumpIfNull==0 );
3079 testcase( pParse->disableColCache==0 );
drh35573352008-01-08 23:54:25 +00003080 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, jumpIfNull^SQLITE_JUMPIFNULL);
drhe55cbd72008-03-31 23:48:03 +00003081 pParse->disableColCache++;
danielk19774adee202004-05-08 08:23:19 +00003082 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhc5499be2008-04-01 15:06:33 +00003083 assert( pParse->disableColCache>0 );
drhe55cbd72008-03-31 23:48:03 +00003084 pParse->disableColCache--;
danielk19774adee202004-05-08 08:23:19 +00003085 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00003086 break;
3087 }
3088 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00003089 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00003090 break;
3091 }
3092 case TK_LT:
3093 case TK_LE:
3094 case TK_GT:
3095 case TK_GE:
3096 case TK_NE:
3097 case TK_EQ: {
drhc5499be2008-04-01 15:06:33 +00003098 testcase( op==TK_LT );
3099 testcase( op==TK_LE );
3100 testcase( op==TK_GT );
3101 testcase( op==TK_GE );
3102 testcase( op==TK_EQ );
3103 testcase( op==TK_NE );
3104 testcase( jumpIfNull==0 );
drhda250ea2008-04-01 05:07:14 +00003105 codeCompareOperands(pParse, pExpr->pLeft, &r1, &regFree1,
3106 pExpr->pRight, &r2, &regFree2);
drh35573352008-01-08 23:54:25 +00003107 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
drh2dcef112008-01-12 19:03:48 +00003108 r1, r2, dest, jumpIfNull);
drhc5499be2008-04-01 15:06:33 +00003109 testcase( regFree1==0 );
3110 testcase( regFree2==0 );
drhcce7d172000-05-31 15:34:51 +00003111 break;
3112 }
drhcce7d172000-05-31 15:34:51 +00003113 case TK_ISNULL:
3114 case TK_NOTNULL: {
drhc5499be2008-04-01 15:06:33 +00003115 testcase( op==TK_ISNULL );
3116 testcase( op==TK_NOTNULL );
drh2dcef112008-01-12 19:03:48 +00003117 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
3118 sqlite3VdbeAddOp2(v, op, r1, dest);
drhc5499be2008-04-01 15:06:33 +00003119 testcase( regFree1==0 );
drhcce7d172000-05-31 15:34:51 +00003120 break;
3121 }
drhfef52082000-06-06 01:50:43 +00003122 case TK_BETWEEN: {
drh2dcef112008-01-12 19:03:48 +00003123 /* x BETWEEN y AND z
danielk19770202b292004-06-09 09:55:16 +00003124 **
drh2dcef112008-01-12 19:03:48 +00003125 ** Is equivalent to
3126 **
3127 ** x>=y AND x<=z
3128 **
3129 ** Code it as such, taking care to do the common subexpression
3130 ** elementation of x.
danielk19770202b292004-06-09 09:55:16 +00003131 */
drh2dcef112008-01-12 19:03:48 +00003132 Expr exprAnd;
3133 Expr compLeft;
3134 Expr compRight;
3135 Expr exprX;
drhbe5c89a2004-07-26 00:31:09 +00003136
drh2dcef112008-01-12 19:03:48 +00003137 exprX = *pExpr->pLeft;
3138 exprAnd.op = TK_AND;
3139 exprAnd.pLeft = &compLeft;
3140 exprAnd.pRight = &compRight;
3141 compLeft.op = TK_GE;
3142 compLeft.pLeft = &exprX;
3143 compLeft.pRight = pExpr->pList->a[0].pExpr;
3144 compRight.op = TK_LE;
3145 compRight.pLeft = &exprX;
3146 compRight.pRight = pExpr->pList->a[1].pExpr;
3147 exprX.iTable = sqlite3ExprCodeTemp(pParse, &exprX, &regFree1);
drhc5499be2008-04-01 15:06:33 +00003148 testcase( regFree1==0 );
drh2dcef112008-01-12 19:03:48 +00003149 exprX.op = TK_REGISTER;
drhc5499be2008-04-01 15:06:33 +00003150 testcase( jumpIfNull==0 );
drh2dcef112008-01-12 19:03:48 +00003151 sqlite3ExprIfFalse(pParse, &exprAnd, dest, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00003152 break;
3153 }
drhcce7d172000-05-31 15:34:51 +00003154 default: {
drh2dcef112008-01-12 19:03:48 +00003155 r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
3156 sqlite3VdbeAddOp3(v, OP_IfNot, r1, dest, jumpIfNull!=0);
drhc5499be2008-04-01 15:06:33 +00003157 testcase( regFree1==0 );
3158 testcase( jumpIfNull==0 );
drhcce7d172000-05-31 15:34:51 +00003159 break;
3160 }
3161 }
drh2dcef112008-01-12 19:03:48 +00003162 sqlite3ReleaseTempReg(pParse, regFree1);
3163 sqlite3ReleaseTempReg(pParse, regFree2);
drhcce7d172000-05-31 15:34:51 +00003164}
drh22827922000-06-06 17:27:05 +00003165
3166/*
3167** Do a deep comparison of two expression trees. Return TRUE (non-zero)
3168** if they are identical and return FALSE if they differ in any way.
drhd40aab02007-02-24 15:29:03 +00003169**
3170** Sometimes this routine will return FALSE even if the two expressions
3171** really are equivalent. If we cannot prove that the expressions are
3172** identical, we return FALSE just to be safe. So if this routine
3173** returns false, then you do not really know for certain if the two
3174** expressions are the same. But if you get a TRUE return, then you
3175** can be sure the expressions are the same. In the places where
3176** this routine is used, it does not hurt to get an extra FALSE - that
3177** just might result in some slightly slower code. But returning
3178** an incorrect TRUE could lead to a malfunction.
drh22827922000-06-06 17:27:05 +00003179*/
danielk19774adee202004-05-08 08:23:19 +00003180int sqlite3ExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00003181 int i;
danielk19774b202ae2006-01-23 05:50:58 +00003182 if( pA==0||pB==0 ){
3183 return pB==pA;
drh22827922000-06-06 17:27:05 +00003184 }
3185 if( pA->op!=pB->op ) return 0;
drhfd357972005-09-09 01:33:19 +00003186 if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 0;
danielk19774adee202004-05-08 08:23:19 +00003187 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
3188 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00003189 if( pA->pList ){
3190 if( pB->pList==0 ) return 0;
3191 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
3192 for(i=0; i<pA->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00003193 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00003194 return 0;
3195 }
3196 }
3197 }else if( pB->pList ){
3198 return 0;
3199 }
3200 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00003201 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drhdd735212007-02-24 13:53:05 +00003202 if( pA->op!=TK_COLUMN && pA->token.z ){
drh22827922000-06-06 17:27:05 +00003203 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00003204 if( pB->token.n!=pA->token.n ) return 0;
drh2646da72005-12-09 20:02:05 +00003205 if( sqlite3StrNICmp((char*)pA->token.z,(char*)pB->token.z,pB->token.n)!=0 ){
3206 return 0;
3207 }
drh22827922000-06-06 17:27:05 +00003208 }
3209 return 1;
3210}
3211
drh13449892005-09-07 21:22:45 +00003212
drh22827922000-06-06 17:27:05 +00003213/*
drh13449892005-09-07 21:22:45 +00003214** Add a new element to the pAggInfo->aCol[] array. Return the index of
3215** the new element. Return a negative number if malloc fails.
drh22827922000-06-06 17:27:05 +00003216*/
drh17435752007-08-16 04:30:38 +00003217static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){
drh13449892005-09-07 21:22:45 +00003218 int i;
drhcf643722007-03-27 13:36:37 +00003219 pInfo->aCol = sqlite3ArrayAllocate(
drh17435752007-08-16 04:30:38 +00003220 db,
drhcf643722007-03-27 13:36:37 +00003221 pInfo->aCol,
3222 sizeof(pInfo->aCol[0]),
3223 3,
3224 &pInfo->nColumn,
3225 &pInfo->nColumnAlloc,
3226 &i
3227 );
drh13449892005-09-07 21:22:45 +00003228 return i;
3229}
3230
3231/*
3232** Add a new element to the pAggInfo->aFunc[] array. Return the index of
3233** the new element. Return a negative number if malloc fails.
3234*/
drh17435752007-08-16 04:30:38 +00003235static int addAggInfoFunc(sqlite3 *db, AggInfo *pInfo){
drh13449892005-09-07 21:22:45 +00003236 int i;
drhcf643722007-03-27 13:36:37 +00003237 pInfo->aFunc = sqlite3ArrayAllocate(
drh17435752007-08-16 04:30:38 +00003238 db,
drhcf643722007-03-27 13:36:37 +00003239 pInfo->aFunc,
3240 sizeof(pInfo->aFunc[0]),
3241 3,
3242 &pInfo->nFunc,
3243 &pInfo->nFuncAlloc,
3244 &i
3245 );
drh13449892005-09-07 21:22:45 +00003246 return i;
3247}
drh22827922000-06-06 17:27:05 +00003248
3249/*
drh626a8792005-01-17 22:08:19 +00003250** This is an xFunc for walkExprTree() used to implement
3251** sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates
3252** for additional information.
drh22827922000-06-06 17:27:05 +00003253**
drh626a8792005-01-17 22:08:19 +00003254** This routine analyzes the aggregate function at pExpr.
drh22827922000-06-06 17:27:05 +00003255*/
drh626a8792005-01-17 22:08:19 +00003256static int analyzeAggregate(void *pArg, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00003257 int i;
danielk1977a58fdfb2005-02-08 07:50:40 +00003258 NameContext *pNC = (NameContext *)pArg;
3259 Parse *pParse = pNC->pParse;
3260 SrcList *pSrcList = pNC->pSrcList;
drh13449892005-09-07 21:22:45 +00003261 AggInfo *pAggInfo = pNC->pAggInfo;
drh22827922000-06-06 17:27:05 +00003262
drh22827922000-06-06 17:27:05 +00003263 switch( pExpr->op ){
drh89c69d02007-01-04 01:20:28 +00003264 case TK_AGG_COLUMN:
drh967e8b72000-06-21 13:59:10 +00003265 case TK_COLUMN: {
drh13449892005-09-07 21:22:45 +00003266 /* Check to see if the column is in one of the tables in the FROM
3267 ** clause of the aggregate query */
3268 if( pSrcList ){
3269 struct SrcList_item *pItem = pSrcList->a;
3270 for(i=0; i<pSrcList->nSrc; i++, pItem++){
3271 struct AggInfo_col *pCol;
3272 if( pExpr->iTable==pItem->iCursor ){
3273 /* If we reach this point, it means that pExpr refers to a table
3274 ** that is in the FROM clause of the aggregate query.
3275 **
3276 ** Make an entry for the column in pAggInfo->aCol[] if there
3277 ** is not an entry there already.
3278 */
drh7f906d62007-03-12 23:48:52 +00003279 int k;
drh13449892005-09-07 21:22:45 +00003280 pCol = pAggInfo->aCol;
drh7f906d62007-03-12 23:48:52 +00003281 for(k=0; k<pAggInfo->nColumn; k++, pCol++){
drh13449892005-09-07 21:22:45 +00003282 if( pCol->iTable==pExpr->iTable &&
3283 pCol->iColumn==pExpr->iColumn ){
3284 break;
3285 }
danielk1977a58fdfb2005-02-08 07:50:40 +00003286 }
danielk19771e536952007-08-16 10:09:01 +00003287 if( (k>=pAggInfo->nColumn)
3288 && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0
3289 ){
drh7f906d62007-03-12 23:48:52 +00003290 pCol = &pAggInfo->aCol[k];
danielk19770817d0d2007-02-14 09:19:36 +00003291 pCol->pTab = pExpr->pTab;
drh13449892005-09-07 21:22:45 +00003292 pCol->iTable = pExpr->iTable;
3293 pCol->iColumn = pExpr->iColumn;
drh0a07c102008-01-03 18:03:08 +00003294 pCol->iMem = ++pParse->nMem;
drh13449892005-09-07 21:22:45 +00003295 pCol->iSorterColumn = -1;
drh5774b802005-09-07 22:48:16 +00003296 pCol->pExpr = pExpr;
drh13449892005-09-07 21:22:45 +00003297 if( pAggInfo->pGroupBy ){
3298 int j, n;
3299 ExprList *pGB = pAggInfo->pGroupBy;
3300 struct ExprList_item *pTerm = pGB->a;
3301 n = pGB->nExpr;
3302 for(j=0; j<n; j++, pTerm++){
3303 Expr *pE = pTerm->pExpr;
3304 if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable &&
3305 pE->iColumn==pExpr->iColumn ){
3306 pCol->iSorterColumn = j;
3307 break;
3308 }
3309 }
3310 }
3311 if( pCol->iSorterColumn<0 ){
3312 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
3313 }
3314 }
3315 /* There is now an entry for pExpr in pAggInfo->aCol[] (either
3316 ** because it was there before or because we just created it).
3317 ** Convert the pExpr to be a TK_AGG_COLUMN referring to that
3318 ** pAggInfo->aCol[] entry.
3319 */
3320 pExpr->pAggInfo = pAggInfo;
3321 pExpr->op = TK_AGG_COLUMN;
drh7f906d62007-03-12 23:48:52 +00003322 pExpr->iAgg = k;
drh13449892005-09-07 21:22:45 +00003323 break;
3324 } /* endif pExpr->iTable==pItem->iCursor */
3325 } /* end loop over pSrcList */
drh22827922000-06-06 17:27:05 +00003326 }
drh626a8792005-01-17 22:08:19 +00003327 return 1;
drh22827922000-06-06 17:27:05 +00003328 }
3329 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00003330 /* The pNC->nDepth==0 test causes aggregate functions in subqueries
3331 ** to be ignored */
danielk1977a58fdfb2005-02-08 07:50:40 +00003332 if( pNC->nDepth==0 ){
drh13449892005-09-07 21:22:45 +00003333 /* Check to see if pExpr is a duplicate of another aggregate
3334 ** function that is already in the pAggInfo structure
3335 */
3336 struct AggInfo_func *pItem = pAggInfo->aFunc;
3337 for(i=0; i<pAggInfo->nFunc; i++, pItem++){
3338 if( sqlite3ExprCompare(pItem->pExpr, pExpr) ){
danielk1977a58fdfb2005-02-08 07:50:40 +00003339 break;
3340 }
drh22827922000-06-06 17:27:05 +00003341 }
drh13449892005-09-07 21:22:45 +00003342 if( i>=pAggInfo->nFunc ){
3343 /* pExpr is original. Make a new entry in pAggInfo->aFunc[]
3344 */
danielk197714db2662006-01-09 16:12:04 +00003345 u8 enc = ENC(pParse->db);
danielk19771e536952007-08-16 10:09:01 +00003346 i = addAggInfoFunc(pParse->db, pAggInfo);
drh13449892005-09-07 21:22:45 +00003347 if( i>=0 ){
3348 pItem = &pAggInfo->aFunc[i];
3349 pItem->pExpr = pExpr;
drh0a07c102008-01-03 18:03:08 +00003350 pItem->iMem = ++pParse->nMem;
drh13449892005-09-07 21:22:45 +00003351 pItem->pFunc = sqlite3FindFunction(pParse->db,
drh2646da72005-12-09 20:02:05 +00003352 (char*)pExpr->token.z, pExpr->token.n,
drh13449892005-09-07 21:22:45 +00003353 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
drhfd357972005-09-09 01:33:19 +00003354 if( pExpr->flags & EP_Distinct ){
3355 pItem->iDistinct = pParse->nTab++;
3356 }else{
3357 pItem->iDistinct = -1;
3358 }
drh13449892005-09-07 21:22:45 +00003359 }
danielk1977a58fdfb2005-02-08 07:50:40 +00003360 }
drh13449892005-09-07 21:22:45 +00003361 /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
3362 */
danielk1977a58fdfb2005-02-08 07:50:40 +00003363 pExpr->iAgg = i;
drh13449892005-09-07 21:22:45 +00003364 pExpr->pAggInfo = pAggInfo;
danielk1977a58fdfb2005-02-08 07:50:40 +00003365 return 1;
drh22827922000-06-06 17:27:05 +00003366 }
drh22827922000-06-06 17:27:05 +00003367 }
3368 }
drh13449892005-09-07 21:22:45 +00003369
3370 /* Recursively walk subqueries looking for TK_COLUMN nodes that need
3371 ** to be changed to TK_AGG_COLUMN. But increment nDepth so that
3372 ** TK_AGG_FUNCTION nodes in subqueries will be unchanged.
3373 */
danielk1977a58fdfb2005-02-08 07:50:40 +00003374 if( pExpr->pSelect ){
3375 pNC->nDepth++;
3376 walkSelectExpr(pExpr->pSelect, analyzeAggregate, pNC);
3377 pNC->nDepth--;
3378 }
drh626a8792005-01-17 22:08:19 +00003379 return 0;
3380}
3381
3382/*
3383** Analyze the given expression looking for aggregate functions and
3384** for variables that need to be added to the pParse->aAgg[] array.
3385** Make additional entries to the pParse->aAgg[] array as necessary.
3386**
3387** This routine should only be called after the expression has been
3388** analyzed by sqlite3ExprResolveNames().
drh626a8792005-01-17 22:08:19 +00003389*/
drhd2b3e232008-01-23 14:51:49 +00003390void sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
danielk1977a58fdfb2005-02-08 07:50:40 +00003391 walkExprTree(pExpr, analyzeAggregate, pNC);
drh22827922000-06-06 17:27:05 +00003392}
drh5d9a4af2005-08-30 00:54:01 +00003393
3394/*
3395** Call sqlite3ExprAnalyzeAggregates() for every expression in an
3396** expression list. Return the number of errors.
3397**
3398** If an error is found, the analysis is cut short.
3399*/
drhd2b3e232008-01-23 14:51:49 +00003400void sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){
drh5d9a4af2005-08-30 00:54:01 +00003401 struct ExprList_item *pItem;
3402 int i;
drh5d9a4af2005-08-30 00:54:01 +00003403 if( pList ){
drhd2b3e232008-01-23 14:51:49 +00003404 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
3405 sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
drh5d9a4af2005-08-30 00:54:01 +00003406 }
3407 }
drh5d9a4af2005-08-30 00:54:01 +00003408}
drh892d3172008-01-10 03:46:36 +00003409
3410/*
3411** Allocate or deallocate temporary use registers during code generation.
3412*/
3413int sqlite3GetTempReg(Parse *pParse){
drhe55cbd72008-03-31 23:48:03 +00003414 int i, r;
3415 if( pParse->nTempReg==0 ){
drh892d3172008-01-10 03:46:36 +00003416 return ++pParse->nMem;
3417 }
drhe55cbd72008-03-31 23:48:03 +00003418 for(i=0; i<pParse->nTempReg; i++){
3419 r = pParse->aTempReg[i];
3420 if( usedAsColumnCache(pParse, r, r) ) continue;
3421 }
3422 if( i>=pParse->nTempReg ){
3423 return ++pParse->nMem;
3424 }
3425 while( i<pParse->nTempReg-1 ){
3426 pParse->aTempReg[i] = pParse->aTempReg[i+1];
3427 }
3428 pParse->nTempReg--;
3429 return r;
drh892d3172008-01-10 03:46:36 +00003430}
3431void sqlite3ReleaseTempReg(Parse *pParse, int iReg){
drh2dcef112008-01-12 19:03:48 +00003432 if( iReg && pParse->nTempReg<ArraySize(pParse->aTempReg) ){
drh892d3172008-01-10 03:46:36 +00003433 pParse->aTempReg[pParse->nTempReg++] = iReg;
3434 }
3435}
3436
3437/*
3438** Allocate or deallocate a block of nReg consecutive registers
3439*/
3440int sqlite3GetTempRange(Parse *pParse, int nReg){
drhe55cbd72008-03-31 23:48:03 +00003441 int i, n;
3442 i = pParse->iRangeReg;
3443 n = pParse->nRangeReg;
3444 if( nReg<=n && !usedAsColumnCache(pParse, i, i+n-1) ){
drh892d3172008-01-10 03:46:36 +00003445 pParse->iRangeReg += nReg;
3446 pParse->nRangeReg -= nReg;
3447 }else{
3448 i = pParse->nMem+1;
3449 pParse->nMem += nReg;
3450 }
3451 return i;
3452}
3453void sqlite3ReleaseTempRange(Parse *pParse, int iReg, int nReg){
3454 if( nReg>pParse->nRangeReg ){
3455 pParse->nRangeReg = nReg;
3456 pParse->iRangeReg = iReg;
3457 }
3458}