blob: d7acec8401c76d48397731bdcefc2e91b3010bde [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**
danielk197731dad9d2007-08-16 11:36:15 +000015** $Id: expr.c,v 1.306 2007/08/16 11:36:15 danielk1977 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){
57 CollSeq *pColl;
58 if( pExpr==0 ) return 0;
59 pColl = sqlite3LocateCollSeq(pParse, (char*)pName->z, pName->n);
60 if( pColl ){
61 pExpr->pColl = pColl;
62 pExpr->flags |= EP_ExpCollate;
63 }
64 return pExpr;
65}
66
67/*
danielk19770202b292004-06-09 09:55:16 +000068** Return the default collation sequence for the expression pExpr. If
69** there is no default collation type, return 0.
70*/
danielk19777cedc8d2004-06-10 10:50:08 +000071CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
72 CollSeq *pColl = 0;
danielk19770202b292004-06-09 09:55:16 +000073 if( pExpr ){
drh7e09fe02007-06-20 16:13:23 +000074 int op;
danielk19777cedc8d2004-06-10 10:50:08 +000075 pColl = pExpr->pColl;
drh7e09fe02007-06-20 16:13:23 +000076 op = pExpr->op;
77 if( (op==TK_CAST || op==TK_UPLUS) && !pColl ){
danielk19777cedc8d2004-06-10 10:50:08 +000078 return sqlite3ExprCollSeq(pParse, pExpr->pLeft);
danielk19770202b292004-06-09 09:55:16 +000079 }
80 }
danielk19777cedc8d2004-06-10 10:50:08 +000081 if( sqlite3CheckCollSeq(pParse, pColl) ){
82 pColl = 0;
83 }
84 return pColl;
danielk19770202b292004-06-09 09:55:16 +000085}
86
87/*
drh626a8792005-01-17 22:08:19 +000088** pExpr is an operand of a comparison operator. aff2 is the
89** type affinity of the other operand. This routine returns the
drh53db1452004-05-20 13:54:53 +000090** type affinity that should be used for the comparison operator.
91*/
danielk1977e014a832004-05-17 10:48:57 +000092char sqlite3CompareAffinity(Expr *pExpr, char aff2){
danielk1977bf3b7212004-05-18 10:06:24 +000093 char aff1 = sqlite3ExprAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +000094 if( aff1 && aff2 ){
drh8df447f2005-11-01 15:48:24 +000095 /* Both sides of the comparison are columns. If one has numeric
96 ** affinity, use that. Otherwise use no affinity.
danielk1977e014a832004-05-17 10:48:57 +000097 */
drh8a512562005-11-14 22:29:05 +000098 if( sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2) ){
danielk1977e014a832004-05-17 10:48:57 +000099 return SQLITE_AFF_NUMERIC;
100 }else{
101 return SQLITE_AFF_NONE;
102 }
103 }else if( !aff1 && !aff2 ){
drh5f6a87b2004-07-19 00:39:45 +0000104 /* Neither side of the comparison is a column. Compare the
105 ** results directly.
danielk1977e014a832004-05-17 10:48:57 +0000106 */
drh5f6a87b2004-07-19 00:39:45 +0000107 return SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +0000108 }else{
109 /* One side is a column, the other is not. Use the columns affinity. */
drhfe05af82005-07-21 03:14:59 +0000110 assert( aff1==0 || aff2==0 );
danielk1977e014a832004-05-17 10:48:57 +0000111 return (aff1 + aff2);
112 }
113}
114
drh53db1452004-05-20 13:54:53 +0000115/*
116** pExpr is a comparison operator. Return the type affinity that should
117** be applied to both operands prior to doing the comparison.
118*/
danielk1977e014a832004-05-17 10:48:57 +0000119static char comparisonAffinity(Expr *pExpr){
120 char aff;
121 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
122 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
123 pExpr->op==TK_NE );
124 assert( pExpr->pLeft );
danielk1977bf3b7212004-05-18 10:06:24 +0000125 aff = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +0000126 if( pExpr->pRight ){
127 aff = sqlite3CompareAffinity(pExpr->pRight, aff);
128 }
129 else if( pExpr->pSelect ){
130 aff = sqlite3CompareAffinity(pExpr->pSelect->pEList->a[0].pExpr, aff);
131 }
132 else if( !aff ){
drhde087bd2007-02-23 03:00:44 +0000133 aff = SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +0000134 }
135 return aff;
136}
137
138/*
139** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
140** idx_affinity is the affinity of an indexed column. Return true
141** if the index with affinity idx_affinity may be used to implement
142** the comparison in pExpr.
143*/
144int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){
145 char aff = comparisonAffinity(pExpr);
drh8a512562005-11-14 22:29:05 +0000146 switch( aff ){
147 case SQLITE_AFF_NONE:
148 return 1;
149 case SQLITE_AFF_TEXT:
150 return idx_affinity==SQLITE_AFF_TEXT;
151 default:
152 return sqlite3IsNumericAffinity(idx_affinity);
153 }
danielk1977e014a832004-05-17 10:48:57 +0000154}
155
danielk1977a37cdde2004-05-16 11:15:36 +0000156/*
157** Return the P1 value that should be used for a binary comparison
158** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
159** If jumpIfNull is true, then set the low byte of the returned
160** P1 value to tell the opcode to jump if either expression
161** evaluates to NULL.
162*/
danielk1977e014a832004-05-17 10:48:57 +0000163static int binaryCompareP1(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){
danielk1977bf3b7212004-05-18 10:06:24 +0000164 char aff = sqlite3ExprAffinity(pExpr2);
drhf0863fe2005-06-12 21:35:51 +0000165 return ((int)sqlite3CompareAffinity(pExpr1, aff))+(jumpIfNull?0x100:0);
danielk1977a37cdde2004-05-16 11:15:36 +0000166}
167
drha2e00042002-01-22 03:13:42 +0000168/*
danielk19770202b292004-06-09 09:55:16 +0000169** Return a pointer to the collation sequence that should be used by
170** a binary comparison operator comparing pLeft and pRight.
171**
172** If the left hand expression has a collating sequence type, then it is
173** used. Otherwise the collation sequence for the right hand expression
174** is used, or the default (BINARY) if neither expression has a collating
175** type.
danielk1977bcbb04e2007-05-29 12:11:29 +0000176**
177** Argument pRight (but not pLeft) may be a null pointer. In this case,
178** it is not considered.
danielk19770202b292004-06-09 09:55:16 +0000179*/
drh0a0e1312007-08-07 17:04:59 +0000180CollSeq *sqlite3BinaryCompareCollSeq(
danielk1977bcbb04e2007-05-29 12:11:29 +0000181 Parse *pParse,
182 Expr *pLeft,
183 Expr *pRight
184){
drhec41dda2007-02-07 13:09:45 +0000185 CollSeq *pColl;
186 assert( pLeft );
drhec41dda2007-02-07 13:09:45 +0000187 if( pLeft->flags & EP_ExpCollate ){
188 assert( pLeft->pColl );
189 pColl = pLeft->pColl;
danielk1977bcbb04e2007-05-29 12:11:29 +0000190 }else if( pRight && pRight->flags & EP_ExpCollate ){
drhec41dda2007-02-07 13:09:45 +0000191 assert( pRight->pColl );
192 pColl = pRight->pColl;
193 }else{
194 pColl = sqlite3ExprCollSeq(pParse, pLeft);
195 if( !pColl ){
196 pColl = sqlite3ExprCollSeq(pParse, pRight);
197 }
danielk19770202b292004-06-09 09:55:16 +0000198 }
199 return pColl;
200}
201
202/*
drhbe5c89a2004-07-26 00:31:09 +0000203** Generate code for a comparison operator.
204*/
205static int codeCompare(
206 Parse *pParse, /* The parsing (and code generating) context */
207 Expr *pLeft, /* The left operand */
208 Expr *pRight, /* The right operand */
209 int opcode, /* The comparison opcode */
210 int dest, /* Jump here if true. */
211 int jumpIfNull /* If true, jump if either operand is NULL */
212){
213 int p1 = binaryCompareP1(pLeft, pRight, jumpIfNull);
danielk1977bcbb04e2007-05-29 12:11:29 +0000214 CollSeq *p3 = sqlite3BinaryCompareCollSeq(pParse, pLeft, pRight);
drh94a11212004-09-25 13:12:14 +0000215 return sqlite3VdbeOp3(pParse->pVdbe, opcode, p1, dest, (void*)p3, P3_COLLSEQ);
drhbe5c89a2004-07-26 00:31:09 +0000216}
217
218/*
drha76b5df2002-02-23 02:32:10 +0000219** Construct a new expression node and return a pointer to it. Memory
drh17435752007-08-16 04:30:38 +0000220** for this node is obtained from sqlite3_malloc(). The calling function
drha76b5df2002-02-23 02:32:10 +0000221** is responsible for making sure the node eventually gets freed.
222*/
drh17435752007-08-16 04:30:38 +0000223Expr *sqlite3Expr(
224 int op, /* Expression opcode */
225 Expr *pLeft, /* Left operand */
226 Expr *pRight, /* Right operand */
227 const Token *pToken /* Argument token */
228){
drha76b5df2002-02-23 02:32:10 +0000229 Expr *pNew;
drh17435752007-08-16 04:30:38 +0000230 pNew = sqlite3MallocZero( sizeof(Expr) );
drha76b5df2002-02-23 02:32:10 +0000231 if( pNew==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000232 /* When malloc fails, delete pLeft and pRight. Expressions passed to
233 ** this function must always be allocated with sqlite3Expr() for this
234 ** reason.
235 */
236 sqlite3ExprDelete(pLeft);
237 sqlite3ExprDelete(pRight);
drha76b5df2002-02-23 02:32:10 +0000238 return 0;
239 }
240 pNew->op = op;
241 pNew->pLeft = pLeft;
242 pNew->pRight = pRight;
danielk1977a58fdfb2005-02-08 07:50:40 +0000243 pNew->iAgg = -1;
drha76b5df2002-02-23 02:32:10 +0000244 if( pToken ){
drh4b59ab52002-08-24 18:24:51 +0000245 assert( pToken->dyn==0 );
drh145716b2004-09-24 12:24:06 +0000246 pNew->span = pNew->token = *pToken;
drha34001c2007-02-02 12:44:37 +0000247 }else if( pLeft ){
248 if( pRight ){
249 sqlite3ExprSpan(pNew, &pLeft->span, &pRight->span);
drh5ffb3ac2007-04-18 17:07:57 +0000250 if( pRight->flags & EP_ExpCollate ){
drha34001c2007-02-02 12:44:37 +0000251 pNew->flags |= EP_ExpCollate;
252 pNew->pColl = pRight->pColl;
253 }
254 }
drh5ffb3ac2007-04-18 17:07:57 +0000255 if( pLeft->flags & EP_ExpCollate ){
drha34001c2007-02-02 12:44:37 +0000256 pNew->flags |= EP_ExpCollate;
257 pNew->pColl = pLeft->pColl;
258 }
drha76b5df2002-02-23 02:32:10 +0000259 }
danielk1977fc976062007-05-10 10:46:56 +0000260
261 sqlite3ExprSetHeight(pNew);
drha76b5df2002-02-23 02:32:10 +0000262 return pNew;
263}
264
265/*
drh17435752007-08-16 04:30:38 +0000266** Works like sqlite3Expr() except that it takes an extra Parse*
267** argument and notifies the associated connection object if malloc fails.
drh206f3d92006-07-11 13:15:08 +0000268*/
drh17435752007-08-16 04:30:38 +0000269Expr *sqlite3PExpr(
270 Parse *pParse, /* Parsing context */
271 int op, /* Expression opcode */
272 Expr *pLeft, /* Left operand */
273 Expr *pRight, /* Right operand */
274 const Token *pToken /* Argument token */
275){
drh206f3d92006-07-11 13:15:08 +0000276 Expr *pNew = sqlite3Expr(op, pLeft, pRight, pToken);
277 if( pNew==0 ){
278 sqlite3ExprDelete(pLeft);
279 sqlite3ExprDelete(pRight);
drh17435752007-08-16 04:30:38 +0000280 pParse->db->mallocFailed = 1;
drh206f3d92006-07-11 13:15:08 +0000281 }
282 return pNew;
283}
284
285/*
drh4e0cff62004-11-05 05:10:28 +0000286** When doing a nested parse, you can include terms in an expression
287** that look like this: #0 #1 #2 ... These terms refer to elements
drh288d37f2005-06-22 08:48:06 +0000288** on the stack. "#0" means the top of the stack.
289** "#1" means the next down on the stack. And so forth.
drh4e0cff62004-11-05 05:10:28 +0000290**
291** This routine is called by the parser to deal with on of those terms.
292** It immediately generates code to store the value in a memory location.
293** The returns an expression that will code to extract the value from
294** that memory location as needed.
295*/
296Expr *sqlite3RegisterExpr(Parse *pParse, Token *pToken){
297 Vdbe *v = pParse->pVdbe;
298 Expr *p;
299 int depth;
drh4e0cff62004-11-05 05:10:28 +0000300 if( pParse->nested==0 ){
301 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", pToken);
drh4e05c832007-05-11 01:44:50 +0000302 return sqlite3Expr(TK_NULL, 0, 0, 0);
drh4e0cff62004-11-05 05:10:28 +0000303 }
drhbb7ac002005-08-12 22:58:53 +0000304 if( v==0 ) return 0;
drh4e0cff62004-11-05 05:10:28 +0000305 p = sqlite3Expr(TK_REGISTER, 0, 0, pToken);
drh73c42a12004-11-20 18:13:10 +0000306 if( p==0 ){
drh17435752007-08-16 04:30:38 +0000307 pParse->db->mallocFailed = 1;
drh73c42a12004-11-20 18:13:10 +0000308 return 0; /* Malloc failed */
309 }
drh2646da72005-12-09 20:02:05 +0000310 depth = atoi((char*)&pToken->z[1]);
drh288d37f2005-06-22 08:48:06 +0000311 p->iTable = pParse->nMem++;
312 sqlite3VdbeAddOp(v, OP_Dup, depth, 0);
313 sqlite3VdbeAddOp(v, OP_MemStore, p->iTable, 1);
drh4e0cff62004-11-05 05:10:28 +0000314 return p;
315}
316
317/*
drh91bb0ee2004-09-01 03:06:34 +0000318** Join two expressions using an AND operator. If either expression is
319** NULL, then just return the other expression.
320*/
danielk19771e536952007-08-16 10:09:01 +0000321Expr *sqlite3ExprAnd(sqlite3 *db, Expr *pLeft, Expr *pRight){
drh91bb0ee2004-09-01 03:06:34 +0000322 if( pLeft==0 ){
323 return pRight;
324 }else if( pRight==0 ){
325 return pLeft;
326 }else{
drh17435752007-08-16 04:30:38 +0000327 Expr *p = sqlite3Expr(TK_AND, pLeft, pRight, 0);
328 if( p==0 ){
329 db->mallocFailed = 1;
330 }
danielk19771e536952007-08-16 10:09:01 +0000331 return p;
drh91bb0ee2004-09-01 03:06:34 +0000332 }
333}
334
335/*
drh6977fea2002-10-22 23:38:04 +0000336** Set the Expr.span field of the given expression to span all
drha76b5df2002-02-23 02:32:10 +0000337** text between the two given tokens.
338*/
danielk19774adee202004-05-08 08:23:19 +0000339void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
drh4efc4752004-01-16 15:55:37 +0000340 assert( pRight!=0 );
341 assert( pLeft!=0 );
drh17435752007-08-16 04:30:38 +0000342 if( pRight->z && pLeft->z ){
drhad6d9462004-09-19 02:15:24 +0000343 assert( pLeft->dyn==0 || pLeft->z[pLeft->n]==0 );
drh145716b2004-09-24 12:24:06 +0000344 if( pLeft->dyn==0 && pRight->dyn==0 ){
drh6977fea2002-10-22 23:38:04 +0000345 pExpr->span.z = pLeft->z;
drh97903fe2005-05-24 20:19:57 +0000346 pExpr->span.n = pRight->n + (pRight->z - pLeft->z);
drh4b59ab52002-08-24 18:24:51 +0000347 }else{
drh6977fea2002-10-22 23:38:04 +0000348 pExpr->span.z = 0;
drh4b59ab52002-08-24 18:24:51 +0000349 }
drha76b5df2002-02-23 02:32:10 +0000350 }
351}
352
353/*
354** Construct a new expression node for a function with multiple
355** arguments.
356*/
drh17435752007-08-16 04:30:38 +0000357Expr *sqlite3ExprFunction(Parse *pParse, ExprList *pList, Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000358 Expr *pNew;
danielk19774b202ae2006-01-23 05:50:58 +0000359 assert( pToken );
drh17435752007-08-16 04:30:38 +0000360 pNew = sqlite3DbMallocZero(pParse->db, sizeof(Expr) );
drha76b5df2002-02-23 02:32:10 +0000361 if( pNew==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000362 sqlite3ExprListDelete(pList); /* Avoid leaking memory when malloc fails */
drha76b5df2002-02-23 02:32:10 +0000363 return 0;
364 }
365 pNew->op = TK_FUNCTION;
366 pNew->pList = pList;
danielk19774b202ae2006-01-23 05:50:58 +0000367 assert( pToken->dyn==0 );
368 pNew->token = *pToken;
drh6977fea2002-10-22 23:38:04 +0000369 pNew->span = pNew->token;
danielk1977fc976062007-05-10 10:46:56 +0000370
371 sqlite3ExprSetHeight(pNew);
drha76b5df2002-02-23 02:32:10 +0000372 return pNew;
373}
374
375/*
drhfa6bc002004-09-07 16:19:52 +0000376** Assign a variable number to an expression that encodes a wildcard
377** in the original SQL statement.
378**
379** Wildcards consisting of a single "?" are assigned the next sequential
380** variable number.
381**
382** Wildcards of the form "?nnn" are assigned the number "nnn". We make
383** sure "nnn" is not too be to avoid a denial of service attack when
384** the SQL statement comes from an external source.
385**
386** Wildcards of the form ":aaa" or "$aaa" are assigned the same number
387** as the previous instance of the same wildcard. Or if this is the first
388** instance of the wildcard, the next sequenial variable number is
389** assigned.
390*/
391void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){
392 Token *pToken;
drh17435752007-08-16 04:30:38 +0000393 sqlite3 *db = pParse->db;
394
drhfa6bc002004-09-07 16:19:52 +0000395 if( pExpr==0 ) return;
396 pToken = &pExpr->token;
397 assert( pToken->n>=1 );
398 assert( pToken->z!=0 );
399 assert( pToken->z[0]!=0 );
400 if( pToken->n==1 ){
401 /* Wildcard of the form "?". Assign the next variable number */
402 pExpr->iTable = ++pParse->nVar;
403 }else if( pToken->z[0]=='?' ){
404 /* Wildcard of the form "?nnn". Convert "nnn" to an integer and
405 ** use it as the variable number */
406 int i;
drh2646da72005-12-09 20:02:05 +0000407 pExpr->iTable = i = atoi((char*)&pToken->z[1]);
drhfa6bc002004-09-07 16:19:52 +0000408 if( i<1 || i>SQLITE_MAX_VARIABLE_NUMBER ){
409 sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
410 SQLITE_MAX_VARIABLE_NUMBER);
411 }
412 if( i>pParse->nVar ){
413 pParse->nVar = i;
414 }
415 }else{
416 /* Wildcards of the form ":aaa" or "$aaa". Reuse the same variable
417 ** number as the prior appearance of the same name, or if the name
418 ** has never appeared before, reuse the same variable number
419 */
420 int i, n;
421 n = pToken->n;
422 for(i=0; i<pParse->nVarExpr; i++){
423 Expr *pE;
424 if( (pE = pParse->apVarExpr[i])!=0
425 && pE->token.n==n
426 && memcmp(pE->token.z, pToken->z, n)==0 ){
427 pExpr->iTable = pE->iTable;
428 break;
429 }
430 }
431 if( i>=pParse->nVarExpr ){
432 pExpr->iTable = ++pParse->nVar;
433 if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){
434 pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10;
drh17435752007-08-16 04:30:38 +0000435 pParse->apVarExpr =
436 sqlite3DbReallocOrFree(
437 db,
438 pParse->apVarExpr,
439 pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0])
440 );
drhfa6bc002004-09-07 16:19:52 +0000441 }
drh17435752007-08-16 04:30:38 +0000442 if( !db->mallocFailed ){
drhfa6bc002004-09-07 16:19:52 +0000443 assert( pParse->apVarExpr!=0 );
444 pParse->apVarExpr[pParse->nVarExpr++] = pExpr;
445 }
446 }
447 }
danielk1977832b2662007-05-09 11:37:22 +0000448 if( !pParse->nErr && pParse->nVar>SQLITE_MAX_VARIABLE_NUMBER ){
449 sqlite3ErrorMsg(pParse, "too many SQL variables");
450 }
drhfa6bc002004-09-07 16:19:52 +0000451}
452
453/*
drha2e00042002-01-22 03:13:42 +0000454** Recursively delete an expression tree.
455*/
danielk19774adee202004-05-08 08:23:19 +0000456void sqlite3ExprDelete(Expr *p){
drha2e00042002-01-22 03:13:42 +0000457 if( p==0 ) return;
drh17435752007-08-16 04:30:38 +0000458 if( p->span.dyn ) sqlite3_free((char*)p->span.z);
459 if( p->token.dyn ) sqlite3_free((char*)p->token.z);
danielk19774adee202004-05-08 08:23:19 +0000460 sqlite3ExprDelete(p->pLeft);
461 sqlite3ExprDelete(p->pRight);
462 sqlite3ExprListDelete(p->pList);
463 sqlite3SelectDelete(p->pSelect);
drh17435752007-08-16 04:30:38 +0000464 sqlite3_free(p);
drha2e00042002-01-22 03:13:42 +0000465}
466
drhd2687b72005-08-12 22:56:09 +0000467/*
468** The Expr.token field might be a string literal that is quoted.
469** If so, remove the quotation marks.
470*/
drh17435752007-08-16 04:30:38 +0000471void sqlite3DequoteExpr(sqlite3 *db, Expr *p){
drhd2687b72005-08-12 22:56:09 +0000472 if( ExprHasAnyProperty(p, EP_Dequoted) ){
473 return;
474 }
475 ExprSetProperty(p, EP_Dequoted);
476 if( p->token.dyn==0 ){
drh17435752007-08-16 04:30:38 +0000477 sqlite3TokenCopy(db, &p->token, &p->token);
drhd2687b72005-08-12 22:56:09 +0000478 }
479 sqlite3Dequote((char*)p->token.z);
480}
481
drha76b5df2002-02-23 02:32:10 +0000482
483/*
drhff78bd22002-02-27 01:47:11 +0000484** The following group of routines make deep copies of expressions,
485** expression lists, ID lists, and select statements. The copies can
486** be deleted (by being passed to their respective ...Delete() routines)
487** without effecting the originals.
488**
danielk19774adee202004-05-08 08:23:19 +0000489** The expression list, ID, and source lists return by sqlite3ExprListDup(),
490** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
drhad3cab52002-05-24 02:04:32 +0000491** by subsequent calls to sqlite*ListAppend() routines.
drhff78bd22002-02-27 01:47:11 +0000492**
drhad3cab52002-05-24 02:04:32 +0000493** Any tables that the SrcList might point to are not duplicated.
drhff78bd22002-02-27 01:47:11 +0000494*/
danielk19771e536952007-08-16 10:09:01 +0000495Expr *sqlite3ExprDup(sqlite3 *db, Expr *p){
drhff78bd22002-02-27 01:47:11 +0000496 Expr *pNew;
497 if( p==0 ) return 0;
drh17435752007-08-16 04:30:38 +0000498 pNew = sqlite3DbMallocRaw(db, sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000499 if( pNew==0 ) return 0;
drh3b167c72002-06-28 12:18:47 +0000500 memcpy(pNew, p, sizeof(*pNew));
drh6977fea2002-10-22 23:38:04 +0000501 if( p->token.z!=0 ){
drh17435752007-08-16 04:30:38 +0000502 pNew->token.z = (u8*)sqlite3DbStrNDup(db, (char*)p->token.z, p->token.n);
drh4b59ab52002-08-24 18:24:51 +0000503 pNew->token.dyn = 1;
504 }else{
drh4efc4752004-01-16 15:55:37 +0000505 assert( pNew->token.z==0 );
drh4b59ab52002-08-24 18:24:51 +0000506 }
drh6977fea2002-10-22 23:38:04 +0000507 pNew->span.z = 0;
drh17435752007-08-16 04:30:38 +0000508 pNew->pLeft = sqlite3ExprDup(db, p->pLeft);
509 pNew->pRight = sqlite3ExprDup(db, p->pRight);
510 pNew->pList = sqlite3ExprListDup(db, p->pList);
511 pNew->pSelect = sqlite3SelectDup(db, p->pSelect);
drhff78bd22002-02-27 01:47:11 +0000512 return pNew;
513}
drh17435752007-08-16 04:30:38 +0000514void sqlite3TokenCopy(sqlite3 *db, Token *pTo, Token *pFrom){
515 if( pTo->dyn ) sqlite3_free((char*)pTo->z);
drh4b59ab52002-08-24 18:24:51 +0000516 if( pFrom->z ){
517 pTo->n = pFrom->n;
drh17435752007-08-16 04:30:38 +0000518 pTo->z = (u8*)sqlite3DbStrNDup(db, (char*)pFrom->z, pFrom->n);
drh4b59ab52002-08-24 18:24:51 +0000519 pTo->dyn = 1;
520 }else{
drh4b59ab52002-08-24 18:24:51 +0000521 pTo->z = 0;
drh4b59ab52002-08-24 18:24:51 +0000522 }
523}
drh17435752007-08-16 04:30:38 +0000524ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p){
drhff78bd22002-02-27 01:47:11 +0000525 ExprList *pNew;
drh145716b2004-09-24 12:24:06 +0000526 struct ExprList_item *pItem, *pOldItem;
drhff78bd22002-02-27 01:47:11 +0000527 int i;
528 if( p==0 ) return 0;
drh17435752007-08-16 04:30:38 +0000529 pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +0000530 if( pNew==0 ) return 0;
danielk197731dad9d2007-08-16 11:36:15 +0000531 pNew->iECursor = 0;
drh4305d102003-07-30 12:34:12 +0000532 pNew->nExpr = pNew->nAlloc = p->nExpr;
drh17435752007-08-16 04:30:38 +0000533 pNew->a = pItem = sqlite3DbMallocRaw(db, p->nExpr*sizeof(p->a[0]) );
danielk1977e0048402004-06-15 16:51:01 +0000534 if( pItem==0 ){
drh17435752007-08-16 04:30:38 +0000535 sqlite3_free(pNew);
danielk1977e0048402004-06-15 16:51:01 +0000536 return 0;
537 }
drh145716b2004-09-24 12:24:06 +0000538 pOldItem = p->a;
539 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
drh4b59ab52002-08-24 18:24:51 +0000540 Expr *pNewExpr, *pOldExpr;
drh17435752007-08-16 04:30:38 +0000541 pItem->pExpr = pNewExpr = sqlite3ExprDup(db, pOldExpr = pOldItem->pExpr);
drh6977fea2002-10-22 23:38:04 +0000542 if( pOldExpr->span.z!=0 && pNewExpr ){
543 /* Always make a copy of the span for top-level expressions in the
drh4b59ab52002-08-24 18:24:51 +0000544 ** expression list. The logic in SELECT processing that determines
545 ** the names of columns in the result set needs this information */
drh17435752007-08-16 04:30:38 +0000546 sqlite3TokenCopy(db, &pNewExpr->span, &pOldExpr->span);
drh4b59ab52002-08-24 18:24:51 +0000547 }
drh1f3e9052002-10-31 00:09:39 +0000548 assert( pNewExpr==0 || pNewExpr->span.z!=0
drh6f7adc82006-01-11 21:41:20 +0000549 || pOldExpr->span.z==0
drh17435752007-08-16 04:30:38 +0000550 || db->mallocFailed );
551 pItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
drh145716b2004-09-24 12:24:06 +0000552 pItem->sortOrder = pOldItem->sortOrder;
553 pItem->isAgg = pOldItem->isAgg;
drh3e7bc9c2004-02-21 19:17:17 +0000554 pItem->done = 0;
drhff78bd22002-02-27 01:47:11 +0000555 }
556 return pNew;
557}
danielk197793758c82005-01-21 08:13:14 +0000558
559/*
560** If cursors, triggers, views and subqueries are all omitted from
561** the build, then none of the following routines, except for
562** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes
563** called with a NULL argument.
564*/
danielk19776a67fe82005-02-04 04:07:16 +0000565#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \
566 || !defined(SQLITE_OMIT_SUBQUERY)
drh17435752007-08-16 04:30:38 +0000567SrcList *sqlite3SrcListDup(sqlite3 *db, SrcList *p){
drhad3cab52002-05-24 02:04:32 +0000568 SrcList *pNew;
569 int i;
drh113088e2003-03-20 01:16:58 +0000570 int nByte;
drhad3cab52002-05-24 02:04:32 +0000571 if( p==0 ) return 0;
drh113088e2003-03-20 01:16:58 +0000572 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
drh17435752007-08-16 04:30:38 +0000573 pNew = sqlite3DbMallocRaw(db, nByte );
drhad3cab52002-05-24 02:04:32 +0000574 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000575 pNew->nSrc = pNew->nAlloc = p->nSrc;
drhad3cab52002-05-24 02:04:32 +0000576 for(i=0; i<p->nSrc; i++){
drh4efc4752004-01-16 15:55:37 +0000577 struct SrcList_item *pNewItem = &pNew->a[i];
578 struct SrcList_item *pOldItem = &p->a[i];
drhed8a3bb2005-06-06 21:19:56 +0000579 Table *pTab;
drh17435752007-08-16 04:30:38 +0000580 pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase);
581 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
582 pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias);
drh4efc4752004-01-16 15:55:37 +0000583 pNewItem->jointype = pOldItem->jointype;
584 pNewItem->iCursor = pOldItem->iCursor;
danielk19771787cca2006-02-10 07:07:14 +0000585 pNewItem->isPopulated = pOldItem->isPopulated;
drhed8a3bb2005-06-06 21:19:56 +0000586 pTab = pNewItem->pTab = pOldItem->pTab;
587 if( pTab ){
588 pTab->nRef++;
danielk1977a1cb1832005-02-12 08:59:55 +0000589 }
drh17435752007-08-16 04:30:38 +0000590 pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect);
591 pNewItem->pOn = sqlite3ExprDup(db, pOldItem->pOn);
592 pNewItem->pUsing = sqlite3IdListDup(db, pOldItem->pUsing);
danielk19776c18b6e2005-01-30 09:17:58 +0000593 pNewItem->colUsed = pOldItem->colUsed;
drhad3cab52002-05-24 02:04:32 +0000594 }
595 return pNew;
596}
drh17435752007-08-16 04:30:38 +0000597IdList *sqlite3IdListDup(sqlite3 *db, IdList *p){
drhff78bd22002-02-27 01:47:11 +0000598 IdList *pNew;
599 int i;
600 if( p==0 ) return 0;
drh17435752007-08-16 04:30:38 +0000601 pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +0000602 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000603 pNew->nId = pNew->nAlloc = p->nId;
drh17435752007-08-16 04:30:38 +0000604 pNew->a = sqlite3DbMallocRaw(db, p->nId*sizeof(p->a[0]) );
danielk1977d5d56522005-03-16 12:15:20 +0000605 if( pNew->a==0 ){
drh17435752007-08-16 04:30:38 +0000606 sqlite3_free(pNew);
danielk1977d5d56522005-03-16 12:15:20 +0000607 return 0;
608 }
drhff78bd22002-02-27 01:47:11 +0000609 for(i=0; i<p->nId; i++){
drh4efc4752004-01-16 15:55:37 +0000610 struct IdList_item *pNewItem = &pNew->a[i];
611 struct IdList_item *pOldItem = &p->a[i];
drh17435752007-08-16 04:30:38 +0000612 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
drh4efc4752004-01-16 15:55:37 +0000613 pNewItem->idx = pOldItem->idx;
drhff78bd22002-02-27 01:47:11 +0000614 }
615 return pNew;
616}
drh17435752007-08-16 04:30:38 +0000617Select *sqlite3SelectDup(sqlite3 *db, Select *p){
drhff78bd22002-02-27 01:47:11 +0000618 Select *pNew;
619 if( p==0 ) return 0;
drh17435752007-08-16 04:30:38 +0000620 pNew = sqlite3DbMallocRaw(db, sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000621 if( pNew==0 ) return 0;
622 pNew->isDistinct = p->isDistinct;
drh17435752007-08-16 04:30:38 +0000623 pNew->pEList = sqlite3ExprListDup(db, p->pEList);
624 pNew->pSrc = sqlite3SrcListDup(db, p->pSrc);
625 pNew->pWhere = sqlite3ExprDup(db, p->pWhere);
626 pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy);
627 pNew->pHaving = sqlite3ExprDup(db, p->pHaving);
628 pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy);
drhff78bd22002-02-27 01:47:11 +0000629 pNew->op = p->op;
drh17435752007-08-16 04:30:38 +0000630 pNew->pPrior = sqlite3SelectDup(db, p->pPrior);
631 pNew->pLimit = sqlite3ExprDup(db, p->pLimit);
632 pNew->pOffset = sqlite3ExprDup(db, p->pOffset);
drh7b58dae2003-07-20 01:16:46 +0000633 pNew->iLimit = -1;
634 pNew->iOffset = -1;
danielk1977a1cb1832005-02-12 08:59:55 +0000635 pNew->isResolved = p->isResolved;
636 pNew->isAgg = p->isAgg;
drhb9bb7c12006-06-11 23:41:55 +0000637 pNew->usesEphm = 0;
drh8e647b82005-09-23 21:11:53 +0000638 pNew->disallowOrderBy = 0;
drh0342b1f2005-09-01 03:07:44 +0000639 pNew->pRightmost = 0;
drhb9bb7c12006-06-11 23:41:55 +0000640 pNew->addrOpenEphm[0] = -1;
641 pNew->addrOpenEphm[1] = -1;
642 pNew->addrOpenEphm[2] = -1;
drhff78bd22002-02-27 01:47:11 +0000643 return pNew;
644}
danielk197793758c82005-01-21 08:13:14 +0000645#else
drh17435752007-08-16 04:30:38 +0000646Select *sqlite3SelectDup(sqlite3 *db, Select *p){
danielk197793758c82005-01-21 08:13:14 +0000647 assert( p==0 );
648 return 0;
649}
650#endif
drhff78bd22002-02-27 01:47:11 +0000651
652
653/*
drha76b5df2002-02-23 02:32:10 +0000654** Add a new element to the end of an expression list. If pList is
655** initially NULL, then create a new expression list.
656*/
drh17435752007-08-16 04:30:38 +0000657ExprList *sqlite3ExprListAppend(
658 Parse *pParse, /* Parsing context */
659 ExprList *pList, /* List to which to append. Might be NULL */
660 Expr *pExpr, /* Expression to be appended */
661 Token *pName /* AS keyword for the expression */
662){
663 sqlite3 *db = pParse->db;
drha76b5df2002-02-23 02:32:10 +0000664 if( pList==0 ){
drh17435752007-08-16 04:30:38 +0000665 pList = sqlite3DbMallocZero(db, sizeof(ExprList) );
drha76b5df2002-02-23 02:32:10 +0000666 if( pList==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000667 goto no_mem;
drha76b5df2002-02-23 02:32:10 +0000668 }
drh4efc4752004-01-16 15:55:37 +0000669 assert( pList->nAlloc==0 );
drha76b5df2002-02-23 02:32:10 +0000670 }
drh4305d102003-07-30 12:34:12 +0000671 if( pList->nAlloc<=pList->nExpr ){
danielk1977d5d56522005-03-16 12:15:20 +0000672 struct ExprList_item *a;
673 int n = pList->nAlloc*2 + 4;
drh17435752007-08-16 04:30:38 +0000674 a = sqlite3_realloc(pList->a, n*sizeof(pList->a[0]));
danielk1977d5d56522005-03-16 12:15:20 +0000675 if( a==0 ){
676 goto no_mem;
drha76b5df2002-02-23 02:32:10 +0000677 }
danielk1977d5d56522005-03-16 12:15:20 +0000678 pList->a = a;
679 pList->nAlloc = n;
drha76b5df2002-02-23 02:32:10 +0000680 }
drh4efc4752004-01-16 15:55:37 +0000681 assert( pList->a!=0 );
682 if( pExpr || pName ){
683 struct ExprList_item *pItem = &pList->a[pList->nExpr++];
684 memset(pItem, 0, sizeof(*pItem));
drh17435752007-08-16 04:30:38 +0000685 pItem->zName = sqlite3NameFromToken(db, pName);
danielk1977e94ddc92005-03-21 03:53:38 +0000686 pItem->pExpr = pExpr;
drha76b5df2002-02-23 02:32:10 +0000687 }
688 return pList;
danielk1977d5d56522005-03-16 12:15:20 +0000689
690no_mem:
691 /* Avoid leaking memory if malloc has failed. */
drh17435752007-08-16 04:30:38 +0000692 db->mallocFailed = 1;
danielk1977d5d56522005-03-16 12:15:20 +0000693 sqlite3ExprDelete(pExpr);
694 sqlite3ExprListDelete(pList);
695 return 0;
drha76b5df2002-02-23 02:32:10 +0000696}
697
698/*
danielk19777a15a4b2007-05-08 17:54:43 +0000699** If the expression list pEList contains more than iLimit elements,
700** leave an error message in pParse.
701*/
702void sqlite3ExprListCheckLength(
703 Parse *pParse,
704 ExprList *pEList,
705 int iLimit,
706 const char *zObject
707){
danielk1977b4fc6792007-05-08 18:04:46 +0000708 if( pEList && pEList->nExpr>iLimit ){
danielk19777a15a4b2007-05-08 17:54:43 +0000709 sqlite3ErrorMsg(pParse, "too many columns in %s", zObject);
710 }
711}
712
danielk1977fc976062007-05-10 10:46:56 +0000713
714#if SQLITE_MAX_EXPR_DEPTH>0
715/* The following three functions, heightOfExpr(), heightOfExprList()
716** and heightOfSelect(), are used to determine the maximum height
717** of any expression tree referenced by the structure passed as the
718** first argument.
719**
720** If this maximum height is greater than the current value pointed
721** to by pnHeight, the second parameter, then set *pnHeight to that
722** value.
723*/
724static void heightOfExpr(Expr *p, int *pnHeight){
725 if( p ){
726 if( p->nHeight>*pnHeight ){
727 *pnHeight = p->nHeight;
728 }
729 }
730}
731static void heightOfExprList(ExprList *p, int *pnHeight){
732 if( p ){
733 int i;
734 for(i=0; i<p->nExpr; i++){
735 heightOfExpr(p->a[i].pExpr, pnHeight);
736 }
737 }
738}
739static void heightOfSelect(Select *p, int *pnHeight){
740 if( p ){
741 heightOfExpr(p->pWhere, pnHeight);
742 heightOfExpr(p->pHaving, pnHeight);
743 heightOfExpr(p->pLimit, pnHeight);
744 heightOfExpr(p->pOffset, pnHeight);
745 heightOfExprList(p->pEList, pnHeight);
746 heightOfExprList(p->pGroupBy, pnHeight);
747 heightOfExprList(p->pOrderBy, pnHeight);
748 heightOfSelect(p->pPrior, pnHeight);
749 }
750}
751
752/*
753** Set the Expr.nHeight variable in the structure passed as an
754** argument. An expression with no children, Expr.pList or
755** Expr.pSelect member has a height of 1. Any other expression
756** has a height equal to the maximum height of any other
757** referenced Expr plus one.
758*/
759void sqlite3ExprSetHeight(Expr *p){
760 int nHeight = 0;
761 heightOfExpr(p->pLeft, &nHeight);
762 heightOfExpr(p->pRight, &nHeight);
763 heightOfExprList(p->pList, &nHeight);
764 heightOfSelect(p->pSelect, &nHeight);
765 p->nHeight = nHeight + 1;
766}
767
768/*
769** Return the maximum height of any expression tree referenced
770** by the select statement passed as an argument.
771*/
772int sqlite3SelectExprHeight(Select *p){
773 int nHeight = 0;
774 heightOfSelect(p, &nHeight);
775 return nHeight;
776}
777#endif
778
danielk19777a15a4b2007-05-08 17:54:43 +0000779/*
drha76b5df2002-02-23 02:32:10 +0000780** Delete an entire expression list.
781*/
danielk19774adee202004-05-08 08:23:19 +0000782void sqlite3ExprListDelete(ExprList *pList){
drha76b5df2002-02-23 02:32:10 +0000783 int i;
drhbe5c89a2004-07-26 00:31:09 +0000784 struct ExprList_item *pItem;
drha76b5df2002-02-23 02:32:10 +0000785 if( pList==0 ) return;
drh1bdd9b52004-04-23 17:04:44 +0000786 assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
787 assert( pList->nExpr<=pList->nAlloc );
drhbe5c89a2004-07-26 00:31:09 +0000788 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
789 sqlite3ExprDelete(pItem->pExpr);
drh17435752007-08-16 04:30:38 +0000790 sqlite3_free(pItem->zName);
drha76b5df2002-02-23 02:32:10 +0000791 }
drh17435752007-08-16 04:30:38 +0000792 sqlite3_free(pList->a);
793 sqlite3_free(pList);
drha76b5df2002-02-23 02:32:10 +0000794}
795
796/*
drh626a8792005-01-17 22:08:19 +0000797** Walk an expression tree. Call xFunc for each node visited.
drh73b211a2005-01-18 04:00:42 +0000798**
drh626a8792005-01-17 22:08:19 +0000799** The return value from xFunc determines whether the tree walk continues.
800** 0 means continue walking the tree. 1 means do not walk children
801** of the current node but continue with siblings. 2 means abandon
802** the tree walk completely.
803**
804** The return value from this routine is 1 to abandon the tree walk
805** and 0 to continue.
drh87abf5c2005-08-25 12:45:04 +0000806**
807** NOTICE: This routine does *not* descend into subqueries.
drh626a8792005-01-17 22:08:19 +0000808*/
danielk1977a58fdfb2005-02-08 07:50:40 +0000809static int walkExprList(ExprList *, int (*)(void *, Expr*), void *);
drh626a8792005-01-17 22:08:19 +0000810static int walkExprTree(Expr *pExpr, int (*xFunc)(void*,Expr*), void *pArg){
drh626a8792005-01-17 22:08:19 +0000811 int rc;
812 if( pExpr==0 ) return 0;
813 rc = (*xFunc)(pArg, pExpr);
814 if( rc==0 ){
815 if( walkExprTree(pExpr->pLeft, xFunc, pArg) ) return 1;
816 if( walkExprTree(pExpr->pRight, xFunc, pArg) ) return 1;
danielk1977a58fdfb2005-02-08 07:50:40 +0000817 if( walkExprList(pExpr->pList, xFunc, pArg) ) return 1;
drh626a8792005-01-17 22:08:19 +0000818 }
819 return rc>1;
820}
821
822/*
danielk1977a58fdfb2005-02-08 07:50:40 +0000823** Call walkExprTree() for every expression in list p.
824*/
825static int walkExprList(ExprList *p, int (*xFunc)(void *, Expr*), void *pArg){
826 int i;
827 struct ExprList_item *pItem;
828 if( !p ) return 0;
829 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
830 if( walkExprTree(pItem->pExpr, xFunc, pArg) ) return 1;
831 }
832 return 0;
833}
834
835/*
836** Call walkExprTree() for every expression in Select p, not including
837** expressions that are part of sub-selects in any FROM clause or the LIMIT
838** or OFFSET expressions..
839*/
840static int walkSelectExpr(Select *p, int (*xFunc)(void *, Expr*), void *pArg){
841 walkExprList(p->pEList, xFunc, pArg);
842 walkExprTree(p->pWhere, xFunc, pArg);
843 walkExprList(p->pGroupBy, xFunc, pArg);
844 walkExprTree(p->pHaving, xFunc, pArg);
845 walkExprList(p->pOrderBy, xFunc, pArg);
danielk197715d79822007-05-15 07:00:34 +0000846 if( p->pPrior ){
847 walkSelectExpr(p->pPrior, xFunc, pArg);
848 }
danielk1977a58fdfb2005-02-08 07:50:40 +0000849 return 0;
850}
851
852
853/*
drh626a8792005-01-17 22:08:19 +0000854** This routine is designed as an xFunc for walkExprTree().
855**
856** pArg is really a pointer to an integer. If we can tell by looking
drh73b211a2005-01-18 04:00:42 +0000857** at pExpr that the expression that contains pExpr is not a constant
858** expression, then set *pArg to 0 and return 2 to abandon the tree walk.
859** If pExpr does does not disqualify the expression from being a constant
860** then do nothing.
861**
862** After walking the whole tree, if no nodes are found that disqualify
863** the expression as constant, then we assume the whole expression
864** is constant. See sqlite3ExprIsConstant() for additional information.
drh626a8792005-01-17 22:08:19 +0000865*/
866static int exprNodeIsConstant(void *pArg, Expr *pExpr){
drh0a168372007-06-08 00:20:47 +0000867 int *pN = (int*)pArg;
868
869 /* If *pArg is 3 then any term of the expression that comes from
870 ** the ON or USING clauses of a join disqualifies the expression
871 ** from being considered constant. */
872 if( (*pN)==3 && ExprHasAnyProperty(pExpr, EP_FromJoin) ){
873 *pN = 0;
874 return 2;
875 }
876
drh626a8792005-01-17 22:08:19 +0000877 switch( pExpr->op ){
drheb55bd22005-06-30 17:04:21 +0000878 /* Consider functions to be constant if all their arguments are constant
879 ** and *pArg==2 */
880 case TK_FUNCTION:
drh0a168372007-06-08 00:20:47 +0000881 if( (*pN)==2 ) return 0;
drheb55bd22005-06-30 17:04:21 +0000882 /* Fall through */
drh626a8792005-01-17 22:08:19 +0000883 case TK_ID:
884 case TK_COLUMN:
885 case TK_DOT:
886 case TK_AGG_FUNCTION:
drh13449892005-09-07 21:22:45 +0000887 case TK_AGG_COLUMN:
drhfe2093d2005-01-20 22:48:47 +0000888#ifndef SQLITE_OMIT_SUBQUERY
889 case TK_SELECT:
890 case TK_EXISTS:
891#endif
drh0a168372007-06-08 00:20:47 +0000892 *pN = 0;
drh626a8792005-01-17 22:08:19 +0000893 return 2;
drh87abf5c2005-08-25 12:45:04 +0000894 case TK_IN:
895 if( pExpr->pSelect ){
drh0a168372007-06-08 00:20:47 +0000896 *pN = 0;
drh87abf5c2005-08-25 12:45:04 +0000897 return 2;
898 }
drh626a8792005-01-17 22:08:19 +0000899 default:
900 return 0;
901 }
902}
903
904/*
drhfef52082000-06-06 01:50:43 +0000905** Walk an expression tree. Return 1 if the expression is constant
drheb55bd22005-06-30 17:04:21 +0000906** and 0 if it involves variables or function calls.
drh23989372002-05-21 13:43:04 +0000907**
908** For the purposes of this function, a double-quoted string (ex: "abc")
909** is considered a variable but a single-quoted string (ex: 'abc') is
910** a constant.
drhfef52082000-06-06 01:50:43 +0000911*/
danielk19774adee202004-05-08 08:23:19 +0000912int sqlite3ExprIsConstant(Expr *p){
drh626a8792005-01-17 22:08:19 +0000913 int isConst = 1;
914 walkExprTree(p, exprNodeIsConstant, &isConst);
915 return isConst;
drhfef52082000-06-06 01:50:43 +0000916}
917
918/*
drheb55bd22005-06-30 17:04:21 +0000919** Walk an expression tree. Return 1 if the expression is constant
drh0a168372007-06-08 00:20:47 +0000920** that does no originate from the ON or USING clauses of a join.
921** Return 0 if it involves variables or function calls or terms from
922** an ON or USING clause.
923*/
924int sqlite3ExprIsConstantNotJoin(Expr *p){
925 int isConst = 3;
926 walkExprTree(p, exprNodeIsConstant, &isConst);
927 return isConst!=0;
928}
929
930/*
931** Walk an expression tree. Return 1 if the expression is constant
drheb55bd22005-06-30 17:04:21 +0000932** or a function call with constant arguments. Return and 0 if there
933** are any variables.
934**
935** For the purposes of this function, a double-quoted string (ex: "abc")
936** is considered a variable but a single-quoted string (ex: 'abc') is
937** a constant.
938*/
939int sqlite3ExprIsConstantOrFunction(Expr *p){
940 int isConst = 2;
941 walkExprTree(p, exprNodeIsConstant, &isConst);
942 return isConst!=0;
943}
944
945/*
drh73b211a2005-01-18 04:00:42 +0000946** If the expression p codes a constant integer that is small enough
drh202b2df2004-01-06 01:13:46 +0000947** to fit in a 32-bit integer, return 1 and put the value of the integer
948** in *pValue. If the expression is not an integer or if it is too big
949** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
drhe4de1fe2002-06-02 16:09:01 +0000950*/
danielk19774adee202004-05-08 08:23:19 +0000951int sqlite3ExprIsInteger(Expr *p, int *pValue){
drhe4de1fe2002-06-02 16:09:01 +0000952 switch( p->op ){
953 case TK_INTEGER: {
drh2646da72005-12-09 20:02:05 +0000954 if( sqlite3GetInt32((char*)p->token.z, pValue) ){
drh202b2df2004-01-06 01:13:46 +0000955 return 1;
956 }
957 break;
drhe4de1fe2002-06-02 16:09:01 +0000958 }
drh4b59ab52002-08-24 18:24:51 +0000959 case TK_UPLUS: {
danielk19774adee202004-05-08 08:23:19 +0000960 return sqlite3ExprIsInteger(p->pLeft, pValue);
drh4b59ab52002-08-24 18:24:51 +0000961 }
drhe4de1fe2002-06-02 16:09:01 +0000962 case TK_UMINUS: {
963 int v;
danielk19774adee202004-05-08 08:23:19 +0000964 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
drhe4de1fe2002-06-02 16:09:01 +0000965 *pValue = -v;
966 return 1;
967 }
968 break;
969 }
970 default: break;
971 }
972 return 0;
973}
974
975/*
drhc4a3c772001-04-04 11:48:57 +0000976** Return TRUE if the given string is a row-id column name.
977*/
danielk19774adee202004-05-08 08:23:19 +0000978int sqlite3IsRowid(const char *z){
979 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
980 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
981 if( sqlite3StrICmp(z, "OID")==0 ) return 1;
drhc4a3c772001-04-04 11:48:57 +0000982 return 0;
983}
984
985/*
drh8141f612004-01-25 22:44:58 +0000986** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
987** that name in the set of source tables in pSrcList and make the pExpr
988** expression node refer back to that source column. The following changes
989** are made to pExpr:
990**
991** pExpr->iDb Set the index in db->aDb[] of the database holding
992** the table.
993** pExpr->iTable Set to the cursor number for the table obtained
994** from pSrcList.
995** pExpr->iColumn Set to the column number within the table.
drh8141f612004-01-25 22:44:58 +0000996** pExpr->op Set to TK_COLUMN.
997** pExpr->pLeft Any expression this points to is deleted
998** pExpr->pRight Any expression this points to is deleted.
999**
1000** The pDbToken is the name of the database (the "X"). This value may be
1001** NULL meaning that name is of the form Y.Z or Z. Any available database
1002** can be used. The pTableToken is the name of the table (the "Y"). This
1003** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it
1004** means that the form of the name is Z and that columns from any table
1005** can be used.
1006**
1007** If the name cannot be resolved unambiguously, leave an error message
1008** in pParse and return non-zero. Return zero on success.
1009*/
1010static int lookupName(
drhffe07b22005-11-03 00:41:17 +00001011 Parse *pParse, /* The parsing context */
drh8141f612004-01-25 22:44:58 +00001012 Token *pDbToken, /* Name of the database containing table, or NULL */
1013 Token *pTableToken, /* Name of table containing column, or NULL */
1014 Token *pColumnToken, /* Name of the column. */
drh626a8792005-01-17 22:08:19 +00001015 NameContext *pNC, /* The name context used to resolve the name */
drh8141f612004-01-25 22:44:58 +00001016 Expr *pExpr /* Make this EXPR node point to the selected column */
1017){
1018 char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */
1019 char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */
1020 char *zCol = 0; /* Name of the column. The "Z" */
1021 int i, j; /* Loop counters */
1022 int cnt = 0; /* Number of matching column names */
1023 int cntTab = 0; /* Number of matching table names */
drh626a8792005-01-17 22:08:19 +00001024 sqlite3 *db = pParse->db; /* The database */
drh51669862004-12-18 18:40:26 +00001025 struct SrcList_item *pItem; /* Use for looping over pSrcList items */
1026 struct SrcList_item *pMatch = 0; /* The matching pSrcList item */
drh73b211a2005-01-18 04:00:42 +00001027 NameContext *pTopNC = pNC; /* First namecontext in the list */
drh8141f612004-01-25 22:44:58 +00001028
1029 assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
drh17435752007-08-16 04:30:38 +00001030 zDb = sqlite3NameFromToken(db, pDbToken);
1031 zTab = sqlite3NameFromToken(db, pTableToken);
1032 zCol = sqlite3NameFromToken(db, pColumnToken);
1033 if( db->mallocFailed ){
danielk1977d5d56522005-03-16 12:15:20 +00001034 goto lookupname_end;
drh8141f612004-01-25 22:44:58 +00001035 }
drh8141f612004-01-25 22:44:58 +00001036
1037 pExpr->iTable = -1;
drh626a8792005-01-17 22:08:19 +00001038 while( pNC && cnt==0 ){
drhffe07b22005-11-03 00:41:17 +00001039 ExprList *pEList;
drh626a8792005-01-17 22:08:19 +00001040 SrcList *pSrcList = pNC->pSrcList;
drh8141f612004-01-25 22:44:58 +00001041
danielk1977b3bce662005-01-29 08:32:43 +00001042 if( pSrcList ){
1043 for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
drh43617e92006-03-06 20:55:46 +00001044 Table *pTab;
1045 int iDb;
danielk1977b3bce662005-01-29 08:32:43 +00001046 Column *pCol;
1047
drh43617e92006-03-06 20:55:46 +00001048 pTab = pItem->pTab;
1049 assert( pTab!=0 );
1050 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
danielk1977b3bce662005-01-29 08:32:43 +00001051 assert( pTab->nCol>0 );
1052 if( zTab ){
1053 if( pItem->zAlias ){
1054 char *zTabName = pItem->zAlias;
1055 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
1056 }else{
1057 char *zTabName = pTab->zName;
1058 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
danielk1977da184232006-01-05 11:34:32 +00001059 if( zDb!=0 && sqlite3StrICmp(db->aDb[iDb].zName, zDb)!=0 ){
danielk1977b3bce662005-01-29 08:32:43 +00001060 continue;
1061 }
drh626a8792005-01-17 22:08:19 +00001062 }
drh8141f612004-01-25 22:44:58 +00001063 }
danielk1977b3bce662005-01-29 08:32:43 +00001064 if( 0==(cntTab++) ){
drh626a8792005-01-17 22:08:19 +00001065 pExpr->iTable = pItem->iCursor;
danielk1977da184232006-01-05 11:34:32 +00001066 pExpr->pSchema = pTab->pSchema;
danielk1977b3bce662005-01-29 08:32:43 +00001067 pMatch = pItem;
1068 }
1069 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
1070 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
danielk1977b3bf5562006-01-10 17:58:23 +00001071 const char *zColl = pTab->aCol[j].zColl;
drh873fac02005-06-06 17:11:46 +00001072 IdList *pUsing;
danielk1977b3bce662005-01-29 08:32:43 +00001073 cnt++;
1074 pExpr->iTable = pItem->iCursor;
1075 pMatch = pItem;
danielk1977da184232006-01-05 11:34:32 +00001076 pExpr->pSchema = pTab->pSchema;
danielk1977b3bce662005-01-29 08:32:43 +00001077 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
1078 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
1079 pExpr->affinity = pTab->aCol[j].affinity;
drh8b4c40d2007-02-01 23:02:45 +00001080 if( (pExpr->flags & EP_ExpCollate)==0 ){
1081 pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0);
1082 }
drh61dfc312006-12-16 16:25:15 +00001083 if( i<pSrcList->nSrc-1 ){
1084 if( pItem[1].jointype & JT_NATURAL ){
1085 /* If this match occurred in the left table of a natural join,
1086 ** then skip the right table to avoid a duplicate match */
1087 pItem++;
1088 i++;
1089 }else if( (pUsing = pItem[1].pUsing)!=0 ){
1090 /* If this match occurs on a column that is in the USING clause
1091 ** of a join, skip the search of the right table of the join
1092 ** to avoid a duplicate match there. */
1093 int k;
1094 for(k=0; k<pUsing->nId; k++){
1095 if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ){
1096 pItem++;
1097 i++;
1098 break;
1099 }
drh873fac02005-06-06 17:11:46 +00001100 }
1101 }
1102 }
danielk1977b3bce662005-01-29 08:32:43 +00001103 break;
1104 }
drh8141f612004-01-25 22:44:58 +00001105 }
1106 }
1107 }
drh626a8792005-01-17 22:08:19 +00001108
1109#ifndef SQLITE_OMIT_TRIGGER
1110 /* If we have not already resolved the name, then maybe
1111 ** it is a new.* or old.* trigger argument reference
1112 */
1113 if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
1114 TriggerStack *pTriggerStack = pParse->trigStack;
1115 Table *pTab = 0;
1116 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){
1117 pExpr->iTable = pTriggerStack->newIdx;
1118 assert( pTriggerStack->pTab );
1119 pTab = pTriggerStack->pTab;
1120 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab)==0 ){
1121 pExpr->iTable = pTriggerStack->oldIdx;
1122 assert( pTriggerStack->pTab );
1123 pTab = pTriggerStack->pTab;
1124 }
1125
1126 if( pTab ){
danielk1977f0113002006-01-24 12:09:17 +00001127 int iCol;
drh626a8792005-01-17 22:08:19 +00001128 Column *pCol = pTab->aCol;
1129
danielk1977da184232006-01-05 11:34:32 +00001130 pExpr->pSchema = pTab->pSchema;
drh626a8792005-01-17 22:08:19 +00001131 cntTab++;
danielk1977f0113002006-01-24 12:09:17 +00001132 for(iCol=0; iCol < pTab->nCol; iCol++, pCol++) {
drh626a8792005-01-17 22:08:19 +00001133 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
danielk1977f0113002006-01-24 12:09:17 +00001134 const char *zColl = pTab->aCol[iCol].zColl;
drh626a8792005-01-17 22:08:19 +00001135 cnt++;
danielk1977f0113002006-01-24 12:09:17 +00001136 pExpr->iColumn = iCol==pTab->iPKey ? -1 : iCol;
1137 pExpr->affinity = pTab->aCol[iCol].affinity;
drh8b4c40d2007-02-01 23:02:45 +00001138 if( (pExpr->flags & EP_ExpCollate)==0 ){
1139 pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0);
1140 }
danielk1977aee18ef2005-03-09 12:26:50 +00001141 pExpr->pTab = pTab;
drh626a8792005-01-17 22:08:19 +00001142 break;
1143 }
1144 }
1145 }
1146 }
drhb7f91642004-10-31 02:22:47 +00001147#endif /* !defined(SQLITE_OMIT_TRIGGER) */
drh8141f612004-01-25 22:44:58 +00001148
drh626a8792005-01-17 22:08:19 +00001149 /*
1150 ** Perhaps the name is a reference to the ROWID
1151 */
1152 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
1153 cnt = 1;
1154 pExpr->iColumn = -1;
drh8a512562005-11-14 22:29:05 +00001155 pExpr->affinity = SQLITE_AFF_INTEGER;
drh626a8792005-01-17 22:08:19 +00001156 }
drh8141f612004-01-25 22:44:58 +00001157
drh626a8792005-01-17 22:08:19 +00001158 /*
1159 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
1160 ** might refer to an result-set alias. This happens, for example, when
1161 ** we are resolving names in the WHERE clause of the following command:
1162 **
1163 ** SELECT a+b AS x FROM table WHERE x<10;
1164 **
1165 ** In cases like this, replace pExpr with a copy of the expression that
1166 ** forms the result set entry ("a+b" in the example) and return immediately.
1167 ** Note that the expression in the result set should have already been
1168 ** resolved by the time the WHERE clause is resolved.
1169 */
drhffe07b22005-11-03 00:41:17 +00001170 if( cnt==0 && (pEList = pNC->pEList)!=0 && zTab==0 ){
drh626a8792005-01-17 22:08:19 +00001171 for(j=0; j<pEList->nExpr; j++){
1172 char *zAs = pEList->a[j].zName;
1173 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
drh36379e92007-07-23 22:51:15 +00001174 Expr *pDup, *pOrig;
drh626a8792005-01-17 22:08:19 +00001175 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
drh4f07e5f2007-05-14 11:34:46 +00001176 assert( pExpr->pList==0 );
1177 assert( pExpr->pSelect==0 );
drh36379e92007-07-23 22:51:15 +00001178 pOrig = pEList->a[j].pExpr;
1179 if( !pNC->allowAgg && ExprHasProperty(pOrig, EP_Agg) ){
1180 sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs);
drh17435752007-08-16 04:30:38 +00001181 sqlite3_free(zCol);
drh36379e92007-07-23 22:51:15 +00001182 return 2;
1183 }
danielk19771e536952007-08-16 10:09:01 +00001184 pDup = sqlite3ExprDup(db, pOrig);
drh4f07e5f2007-05-14 11:34:46 +00001185 if( pExpr->flags & EP_ExpCollate ){
1186 pDup->pColl = pExpr->pColl;
1187 pDup->flags |= EP_ExpCollate;
1188 }
drh17435752007-08-16 04:30:38 +00001189 if( pExpr->span.dyn ) sqlite3_free((char*)pExpr->span.z);
1190 if( pExpr->token.dyn ) sqlite3_free((char*)pExpr->token.z);
drh4f07e5f2007-05-14 11:34:46 +00001191 memcpy(pExpr, pDup, sizeof(*pExpr));
drh17435752007-08-16 04:30:38 +00001192 sqlite3_free(pDup);
drh15ccce12005-05-23 15:06:39 +00001193 cnt = 1;
danielk1977c9cf6e32007-06-25 16:29:33 +00001194 pMatch = 0;
drh626a8792005-01-17 22:08:19 +00001195 assert( zTab==0 && zDb==0 );
drh15ccce12005-05-23 15:06:39 +00001196 goto lookupname_end_2;
drh626a8792005-01-17 22:08:19 +00001197 }
1198 }
1199 }
1200
1201 /* Advance to the next name context. The loop will exit when either
1202 ** we have a match (cnt>0) or when we run out of name contexts.
1203 */
1204 if( cnt==0 ){
1205 pNC = pNC->pNext;
1206 }
drh8141f612004-01-25 22:44:58 +00001207 }
1208
1209 /*
1210 ** If X and Y are NULL (in other words if only the column name Z is
1211 ** supplied) and the value of Z is enclosed in double-quotes, then
1212 ** Z is a string literal if it doesn't match any column names. In that
1213 ** case, we need to return right away and not make any changes to
1214 ** pExpr.
drh15ccce12005-05-23 15:06:39 +00001215 **
1216 ** Because no reference was made to outer contexts, the pNC->nRef
1217 ** fields are not changed in any context.
drh8141f612004-01-25 22:44:58 +00001218 */
1219 if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
drh17435752007-08-16 04:30:38 +00001220 sqlite3_free(zCol);
drh8141f612004-01-25 22:44:58 +00001221 return 0;
1222 }
1223
1224 /*
1225 ** cnt==0 means there was not match. cnt>1 means there were two or
1226 ** more matches. Either way, we have an error.
1227 */
1228 if( cnt!=1 ){
1229 char *z = 0;
1230 char *zErr;
1231 zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s";
1232 if( zDb ){
drhf93339d2006-01-03 15:16:26 +00001233 sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, (char*)0);
drh8141f612004-01-25 22:44:58 +00001234 }else if( zTab ){
drhf93339d2006-01-03 15:16:26 +00001235 sqlite3SetString(&z, zTab, ".", zCol, (char*)0);
drh8141f612004-01-25 22:44:58 +00001236 }else{
drh17435752007-08-16 04:30:38 +00001237 z = sqlite3StrDup(zCol);
drh8141f612004-01-25 22:44:58 +00001238 }
danielk19774adee202004-05-08 08:23:19 +00001239 sqlite3ErrorMsg(pParse, zErr, z);
drh17435752007-08-16 04:30:38 +00001240 sqlite3_free(z);
drh73b211a2005-01-18 04:00:42 +00001241 pTopNC->nErr++;
drh8141f612004-01-25 22:44:58 +00001242 }
1243
drh51669862004-12-18 18:40:26 +00001244 /* If a column from a table in pSrcList is referenced, then record
1245 ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes
1246 ** bit 0 to be set. Column 1 sets bit 1. And so forth. If the
1247 ** column number is greater than the number of bits in the bitmask
1248 ** then set the high-order bit of the bitmask.
1249 */
1250 if( pExpr->iColumn>=0 && pMatch!=0 ){
1251 int n = pExpr->iColumn;
1252 if( n>=sizeof(Bitmask)*8 ){
1253 n = sizeof(Bitmask)*8-1;
1254 }
1255 assert( pMatch->iCursor==pExpr->iTable );
drhca83ac52007-02-01 01:40:44 +00001256 pMatch->colUsed |= ((Bitmask)1)<<n;
drh51669862004-12-18 18:40:26 +00001257 }
1258
danielk1977d5d56522005-03-16 12:15:20 +00001259lookupname_end:
drh8141f612004-01-25 22:44:58 +00001260 /* Clean up and return
1261 */
drh17435752007-08-16 04:30:38 +00001262 sqlite3_free(zDb);
1263 sqlite3_free(zTab);
danielk19774adee202004-05-08 08:23:19 +00001264 sqlite3ExprDelete(pExpr->pLeft);
drh8141f612004-01-25 22:44:58 +00001265 pExpr->pLeft = 0;
danielk19774adee202004-05-08 08:23:19 +00001266 sqlite3ExprDelete(pExpr->pRight);
drh8141f612004-01-25 22:44:58 +00001267 pExpr->pRight = 0;
1268 pExpr->op = TK_COLUMN;
drh15ccce12005-05-23 15:06:39 +00001269lookupname_end_2:
drh17435752007-08-16 04:30:38 +00001270 sqlite3_free(zCol);
drh626a8792005-01-17 22:08:19 +00001271 if( cnt==1 ){
danielk1977b3bce662005-01-29 08:32:43 +00001272 assert( pNC!=0 );
drh626a8792005-01-17 22:08:19 +00001273 sqlite3AuthRead(pParse, pExpr, pNC->pSrcList);
danielk1977aee18ef2005-03-09 12:26:50 +00001274 if( pMatch && !pMatch->pSelect ){
1275 pExpr->pTab = pMatch->pTab;
1276 }
drh15ccce12005-05-23 15:06:39 +00001277 /* Increment the nRef value on all name contexts from TopNC up to
1278 ** the point where the name matched. */
1279 for(;;){
1280 assert( pTopNC!=0 );
1281 pTopNC->nRef++;
1282 if( pTopNC==pNC ) break;
1283 pTopNC = pTopNC->pNext;
1284 }
1285 return 0;
1286 } else {
1287 return 1;
drh626a8792005-01-17 22:08:19 +00001288 }
drh8141f612004-01-25 22:44:58 +00001289}
1290
1291/*
drh626a8792005-01-17 22:08:19 +00001292** This routine is designed as an xFunc for walkExprTree().
1293**
drh73b211a2005-01-18 04:00:42 +00001294** Resolve symbolic names into TK_COLUMN operators for the current
drh626a8792005-01-17 22:08:19 +00001295** node in the expression tree. Return 0 to continue the search down
drh73b211a2005-01-18 04:00:42 +00001296** the tree or 2 to abort the tree walk.
1297**
1298** This routine also does error checking and name resolution for
1299** function names. The operator for aggregate functions is changed
1300** to TK_AGG_FUNCTION.
drh626a8792005-01-17 22:08:19 +00001301*/
1302static int nameResolverStep(void *pArg, Expr *pExpr){
1303 NameContext *pNC = (NameContext*)pArg;
drh626a8792005-01-17 22:08:19 +00001304 Parse *pParse;
drh626a8792005-01-17 22:08:19 +00001305
danielk1977b3bce662005-01-29 08:32:43 +00001306 if( pExpr==0 ) return 1;
drh626a8792005-01-17 22:08:19 +00001307 assert( pNC!=0 );
drh626a8792005-01-17 22:08:19 +00001308 pParse = pNC->pParse;
danielk1977b3bce662005-01-29 08:32:43 +00001309
drh626a8792005-01-17 22:08:19 +00001310 if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return 1;
1311 ExprSetProperty(pExpr, EP_Resolved);
1312#ifndef NDEBUG
danielk1977f0113002006-01-24 12:09:17 +00001313 if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
1314 SrcList *pSrcList = pNC->pSrcList;
danielk1977940fac92005-01-23 22:41:37 +00001315 int i;
danielk1977f0113002006-01-24 12:09:17 +00001316 for(i=0; i<pNC->pSrcList->nSrc; i++){
drh626a8792005-01-17 22:08:19 +00001317 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
1318 }
1319 }
1320#endif
1321 switch( pExpr->op ){
1322 /* Double-quoted strings (ex: "abc") are used as identifiers if
1323 ** possible. Otherwise they remain as strings. Single-quoted
1324 ** strings (ex: 'abc') are always string literals.
1325 */
1326 case TK_STRING: {
1327 if( pExpr->token.z[0]=='\'' ) break;
1328 /* Fall thru into the TK_ID case if this is a double-quoted string */
1329 }
1330 /* A lone identifier is the name of a column.
1331 */
1332 case TK_ID: {
drh626a8792005-01-17 22:08:19 +00001333 lookupName(pParse, 0, 0, &pExpr->token, pNC, pExpr);
1334 return 1;
1335 }
1336
1337 /* A table name and column name: ID.ID
1338 ** Or a database, table and column: ID.ID.ID
1339 */
1340 case TK_DOT: {
1341 Token *pColumn;
1342 Token *pTable;
1343 Token *pDb;
1344 Expr *pRight;
1345
danielk1977b3bce662005-01-29 08:32:43 +00001346 /* if( pSrcList==0 ) break; */
drh626a8792005-01-17 22:08:19 +00001347 pRight = pExpr->pRight;
1348 if( pRight->op==TK_ID ){
1349 pDb = 0;
1350 pTable = &pExpr->pLeft->token;
1351 pColumn = &pRight->token;
1352 }else{
1353 assert( pRight->op==TK_DOT );
1354 pDb = &pExpr->pLeft->token;
1355 pTable = &pRight->pLeft->token;
1356 pColumn = &pRight->pRight->token;
1357 }
1358 lookupName(pParse, pDb, pTable, pColumn, pNC, pExpr);
1359 return 1;
1360 }
1361
1362 /* Resolve function names
1363 */
drhb71090f2005-05-23 17:26:51 +00001364 case TK_CONST_FUNC:
drh626a8792005-01-17 22:08:19 +00001365 case TK_FUNCTION: {
1366 ExprList *pList = pExpr->pList; /* The argument list */
1367 int n = pList ? pList->nExpr : 0; /* Number of arguments */
1368 int no_such_func = 0; /* True if no such function exists */
1369 int wrong_num_args = 0; /* True if wrong number of arguments */
1370 int is_agg = 0; /* True if is an aggregate function */
1371 int i;
drh5169bbc2006-08-24 14:59:45 +00001372 int auth; /* Authorization to use the function */
drh626a8792005-01-17 22:08:19 +00001373 int nId; /* Number of characters in function name */
1374 const char *zId; /* The function name. */
drh73b211a2005-01-18 04:00:42 +00001375 FuncDef *pDef; /* Information about the function */
danielk197714db2662006-01-09 16:12:04 +00001376 int enc = ENC(pParse->db); /* The database encoding */
drh626a8792005-01-17 22:08:19 +00001377
drh2646da72005-12-09 20:02:05 +00001378 zId = (char*)pExpr->token.z;
drhb71090f2005-05-23 17:26:51 +00001379 nId = pExpr->token.n;
drh626a8792005-01-17 22:08:19 +00001380 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
1381 if( pDef==0 ){
1382 pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
1383 if( pDef==0 ){
1384 no_such_func = 1;
1385 }else{
1386 wrong_num_args = 1;
1387 }
1388 }else{
1389 is_agg = pDef->xFunc==0;
1390 }
drh2fca7fe2006-11-23 11:59:13 +00001391#ifndef SQLITE_OMIT_AUTHORIZATION
drh5169bbc2006-08-24 14:59:45 +00001392 if( pDef ){
1393 auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0);
1394 if( auth!=SQLITE_OK ){
1395 if( auth==SQLITE_DENY ){
1396 sqlite3ErrorMsg(pParse, "not authorized to use function: %s",
1397 pDef->zName);
1398 pNC->nErr++;
1399 }
1400 pExpr->op = TK_NULL;
1401 return 1;
1402 }
1403 }
drhb8b14212006-08-24 15:18:25 +00001404#endif
drh626a8792005-01-17 22:08:19 +00001405 if( is_agg && !pNC->allowAgg ){
1406 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
1407 pNC->nErr++;
1408 is_agg = 0;
1409 }else if( no_such_func ){
1410 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
1411 pNC->nErr++;
1412 }else if( wrong_num_args ){
1413 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
1414 nId, zId);
1415 pNC->nErr++;
1416 }
1417 if( is_agg ){
1418 pExpr->op = TK_AGG_FUNCTION;
1419 pNC->hasAgg = 1;
1420 }
drh73b211a2005-01-18 04:00:42 +00001421 if( is_agg ) pNC->allowAgg = 0;
drh626a8792005-01-17 22:08:19 +00001422 for(i=0; pNC->nErr==0 && i<n; i++){
drh73b211a2005-01-18 04:00:42 +00001423 walkExprTree(pList->a[i].pExpr, nameResolverStep, pNC);
drh626a8792005-01-17 22:08:19 +00001424 }
drh73b211a2005-01-18 04:00:42 +00001425 if( is_agg ) pNC->allowAgg = 1;
drh626a8792005-01-17 22:08:19 +00001426 /* FIX ME: Compute pExpr->affinity based on the expected return
1427 ** type of the function
1428 */
1429 return is_agg;
1430 }
danielk1977b3bce662005-01-29 08:32:43 +00001431#ifndef SQLITE_OMIT_SUBQUERY
1432 case TK_SELECT:
1433 case TK_EXISTS:
1434#endif
1435 case TK_IN: {
1436 if( pExpr->pSelect ){
drh8a9f38f2005-11-05 15:07:55 +00001437 int nRef = pNC->nRef;
drh06f65412005-11-03 02:03:13 +00001438#ifndef SQLITE_OMIT_CHECK
1439 if( pNC->isCheck ){
1440 sqlite3ErrorMsg(pParse,"subqueries prohibited in CHECK constraints");
1441 }
1442#endif
danielk1977b3bce662005-01-29 08:32:43 +00001443 sqlite3SelectResolve(pParse, pExpr->pSelect, pNC);
1444 assert( pNC->nRef>=nRef );
1445 if( nRef!=pNC->nRef ){
1446 ExprSetProperty(pExpr, EP_VarSelect);
1447 }
1448 }
drh4284fb02005-11-03 12:33:28 +00001449 break;
danielk1977b3bce662005-01-29 08:32:43 +00001450 }
drh4284fb02005-11-03 12:33:28 +00001451#ifndef SQLITE_OMIT_CHECK
1452 case TK_VARIABLE: {
1453 if( pNC->isCheck ){
1454 sqlite3ErrorMsg(pParse,"parameters prohibited in CHECK constraints");
1455 }
1456 break;
1457 }
1458#endif
drh626a8792005-01-17 22:08:19 +00001459 }
1460 return 0;
1461}
1462
1463/*
drhcce7d172000-05-31 15:34:51 +00001464** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +00001465** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +00001466** index to the table in the table list and a column offset. The
1467** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
1468** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +00001469** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +00001470** VDBE cursor number for a cursor that is pointing into the referenced
1471** table. The Expr.iColumn value is changed to the index of the column
1472** of the referenced table. The Expr.iColumn value for the special
1473** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
1474** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +00001475**
drh626a8792005-01-17 22:08:19 +00001476** Also resolve function names and check the functions for proper
1477** usage. Make sure all function names are recognized and all functions
1478** have the correct number of arguments. Leave an error message
1479** in pParse->zErrMsg if anything is amiss. Return the number of errors.
1480**
drh73b211a2005-01-18 04:00:42 +00001481** If the expression contains aggregate functions then set the EP_Agg
1482** property on the expression.
drh626a8792005-01-17 22:08:19 +00001483*/
danielk1977fc976062007-05-10 10:46:56 +00001484int sqlite3ExprResolveNames(
danielk1977b3bce662005-01-29 08:32:43 +00001485 NameContext *pNC, /* Namespace to resolve expressions in. */
1486 Expr *pExpr /* The expression to be analyzed. */
drh626a8792005-01-17 22:08:19 +00001487){
drh13449892005-09-07 21:22:45 +00001488 int savedHasAgg;
drh73b211a2005-01-18 04:00:42 +00001489 if( pExpr==0 ) return 0;
danielk1977fc976062007-05-10 10:46:56 +00001490#if SQLITE_MAX_EXPR_DEPTH>0
1491 if( (pExpr->nHeight+pNC->pParse->nHeight)>SQLITE_MAX_EXPR_DEPTH ){
1492 sqlite3ErrorMsg(pNC->pParse,
1493 "Expression tree is too large (maximum depth %d)",
1494 SQLITE_MAX_EXPR_DEPTH
1495 );
1496 return 1;
1497 }
1498 pNC->pParse->nHeight += pExpr->nHeight;
1499#endif
drh13449892005-09-07 21:22:45 +00001500 savedHasAgg = pNC->hasAgg;
1501 pNC->hasAgg = 0;
danielk1977b3bce662005-01-29 08:32:43 +00001502 walkExprTree(pExpr, nameResolverStep, pNC);
danielk1977fc976062007-05-10 10:46:56 +00001503#if SQLITE_MAX_EXPR_DEPTH>0
1504 pNC->pParse->nHeight -= pExpr->nHeight;
1505#endif
danielk1977b3bce662005-01-29 08:32:43 +00001506 if( pNC->nErr>0 ){
drh73b211a2005-01-18 04:00:42 +00001507 ExprSetProperty(pExpr, EP_Error);
drh73b211a2005-01-18 04:00:42 +00001508 }
drh13449892005-09-07 21:22:45 +00001509 if( pNC->hasAgg ){
1510 ExprSetProperty(pExpr, EP_Agg);
1511 }else if( savedHasAgg ){
1512 pNC->hasAgg = 1;
1513 }
drh73b211a2005-01-18 04:00:42 +00001514 return ExprHasProperty(pExpr, EP_Error);
drh626a8792005-01-17 22:08:19 +00001515}
1516
drh1398ad32005-01-19 23:24:50 +00001517/*
1518** A pointer instance of this structure is used to pass information
1519** through walkExprTree into codeSubqueryStep().
1520*/
1521typedef struct QueryCoder QueryCoder;
1522struct QueryCoder {
1523 Parse *pParse; /* The parsing context */
1524 NameContext *pNC; /* Namespace of first enclosing query */
1525};
1526
drh626a8792005-01-17 22:08:19 +00001527
1528/*
drh9cbe6352005-11-29 03:13:21 +00001529** Generate code for scalar subqueries used as an expression
1530** and IN operators. Examples:
drh626a8792005-01-17 22:08:19 +00001531**
drh9cbe6352005-11-29 03:13:21 +00001532** (SELECT a FROM b) -- subquery
1533** EXISTS (SELECT a FROM b) -- EXISTS subquery
1534** x IN (4,5,11) -- IN operator with list on right-hand side
1535** x IN (SELECT a FROM b) -- IN operator with subquery on the right
drhfef52082000-06-06 01:50:43 +00001536**
drh9cbe6352005-11-29 03:13:21 +00001537** The pExpr parameter describes the expression that contains the IN
1538** operator or subquery.
drhcce7d172000-05-31 15:34:51 +00001539*/
drh51522cd2005-01-20 13:36:19 +00001540#ifndef SQLITE_OMIT_SUBQUERY
danielk1977b3bce662005-01-29 08:32:43 +00001541void sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){
drh57dbd7b2005-07-08 18:25:26 +00001542 int testAddr = 0; /* One-time test address */
danielk1977b3bce662005-01-29 08:32:43 +00001543 Vdbe *v = sqlite3GetVdbe(pParse);
1544 if( v==0 ) return;
1545
danielk1977fc976062007-05-10 10:46:56 +00001546
drh57dbd7b2005-07-08 18:25:26 +00001547 /* This code must be run in its entirety every time it is encountered
1548 ** if any of the following is true:
1549 **
1550 ** * The right-hand side is a correlated subquery
1551 ** * The right-hand side is an expression list containing variables
1552 ** * We are inside a trigger
1553 **
1554 ** If all of the above are false, then we can run this code just once
1555 ** save the results, and reuse the same result on subsequent invocations.
danielk1977b3bce662005-01-29 08:32:43 +00001556 */
1557 if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->trigStack ){
1558 int mem = pParse->nMem++;
1559 sqlite3VdbeAddOp(v, OP_MemLoad, mem, 0);
drh57dbd7b2005-07-08 18:25:26 +00001560 testAddr = sqlite3VdbeAddOp(v, OP_If, 0, 0);
drh17435752007-08-16 04:30:38 +00001561 assert( testAddr>0 || pParse->db->mallocFailed );
drhd654be82005-09-20 17:42:23 +00001562 sqlite3VdbeAddOp(v, OP_MemInt, 1, mem);
danielk1977b3bce662005-01-29 08:32:43 +00001563 }
1564
drhcce7d172000-05-31 15:34:51 +00001565 switch( pExpr->op ){
drhfef52082000-06-06 01:50:43 +00001566 case TK_IN: {
danielk1977e014a832004-05-17 10:48:57 +00001567 char affinity;
drhd3d39e92004-05-20 22:16:29 +00001568 KeyInfo keyInfo;
drhb9bb7c12006-06-11 23:41:55 +00001569 int addr; /* Address of OP_OpenEphemeral instruction */
drhd3d39e92004-05-20 22:16:29 +00001570
danielk1977bf3b7212004-05-18 10:06:24 +00001571 affinity = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +00001572
1573 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
drh57dbd7b2005-07-08 18:25:26 +00001574 ** expression it is handled the same way. A virtual table is
danielk1977e014a832004-05-17 10:48:57 +00001575 ** filled with single-field index keys representing the results
1576 ** from the SELECT or the <exprlist>.
1577 **
1578 ** If the 'x' expression is a column value, or the SELECT...
1579 ** statement returns a column value, then the affinity of that
1580 ** column is used to build the index keys. If both 'x' and the
1581 ** SELECT... statement are columns, then numeric affinity is used
1582 ** if either column has NUMERIC or INTEGER affinity. If neither
1583 ** 'x' nor the SELECT... statement are columns, then numeric affinity
1584 ** is used.
1585 */
1586 pExpr->iTable = pParse->nTab++;
drhb9bb7c12006-06-11 23:41:55 +00001587 addr = sqlite3VdbeAddOp(v, OP_OpenEphemeral, pExpr->iTable, 0);
drhd3d39e92004-05-20 22:16:29 +00001588 memset(&keyInfo, 0, sizeof(keyInfo));
1589 keyInfo.nField = 1;
drhf3218fe2004-05-28 08:21:02 +00001590 sqlite3VdbeAddOp(v, OP_SetNumColumns, pExpr->iTable, 1);
danielk1977e014a832004-05-17 10:48:57 +00001591
drhfef52082000-06-06 01:50:43 +00001592 if( pExpr->pSelect ){
1593 /* Case 1: expr IN (SELECT ...)
1594 **
danielk1977e014a832004-05-17 10:48:57 +00001595 ** Generate code to write the results of the select into the temporary
1596 ** table allocated and opened above.
drhfef52082000-06-06 01:50:43 +00001597 */
danielk1977e014a832004-05-17 10:48:57 +00001598 int iParm = pExpr->iTable + (((int)affinity)<<16);
drhbe5c89a2004-07-26 00:31:09 +00001599 ExprList *pEList;
danielk1977e014a832004-05-17 10:48:57 +00001600 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
drh94ccde52007-04-13 16:06:32 +00001601 if( sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0) ){
1602 return;
1603 }
drhbe5c89a2004-07-26 00:31:09 +00001604 pEList = pExpr->pSelect->pEList;
1605 if( pEList && pEList->nExpr>0 ){
danielk1977bcbb04e2007-05-29 12:11:29 +00001606 keyInfo.aColl[0] = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft,
drhbe5c89a2004-07-26 00:31:09 +00001607 pEList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001608 }
drhfef52082000-06-06 01:50:43 +00001609 }else if( pExpr->pList ){
1610 /* Case 2: expr IN (exprlist)
1611 **
drhfd131da2007-08-07 17:13:03 +00001612 ** For each expression, build an index key from the evaluation and
danielk1977e014a832004-05-17 10:48:57 +00001613 ** store it in the temporary table. If <expr> is a column, then use
1614 ** that columns affinity when building index keys. If <expr> is not
1615 ** a column, use numeric affinity.
drhfef52082000-06-06 01:50:43 +00001616 */
danielk1977e014a832004-05-17 10:48:57 +00001617 int i;
drh57dbd7b2005-07-08 18:25:26 +00001618 ExprList *pList = pExpr->pList;
1619 struct ExprList_item *pItem;
1620
danielk1977e014a832004-05-17 10:48:57 +00001621 if( !affinity ){
drh8159a352006-05-23 23:22:29 +00001622 affinity = SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +00001623 }
danielk19770202b292004-06-09 09:55:16 +00001624 keyInfo.aColl[0] = pExpr->pLeft->pColl;
danielk1977e014a832004-05-17 10:48:57 +00001625
1626 /* Loop through each expression in <exprlist>. */
drh57dbd7b2005-07-08 18:25:26 +00001627 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
1628 Expr *pE2 = pItem->pExpr;
danielk1977e014a832004-05-17 10:48:57 +00001629
drh57dbd7b2005-07-08 18:25:26 +00001630 /* If the expression is not constant then we will need to
1631 ** disable the test that was generated above that makes sure
1632 ** this code only executes once. Because for a non-constant
1633 ** expression we need to rerun this code each time.
1634 */
drh6c30be82005-07-29 15:10:17 +00001635 if( testAddr>0 && !sqlite3ExprIsConstant(pE2) ){
drhf8875402006-03-17 13:56:34 +00001636 sqlite3VdbeChangeToNoop(v, testAddr-1, 3);
drh57dbd7b2005-07-08 18:25:26 +00001637 testAddr = 0;
drh4794b982000-06-06 13:54:14 +00001638 }
danielk1977e014a832004-05-17 10:48:57 +00001639
1640 /* Evaluate the expression and insert it into the temp table */
1641 sqlite3ExprCode(pParse, pE2);
drh94a11212004-09-25 13:12:14 +00001642 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1);
drhf0863fe2005-06-12 21:35:51 +00001643 sqlite3VdbeAddOp(v, OP_IdxInsert, pExpr->iTable, 0);
drhfef52082000-06-06 01:50:43 +00001644 }
1645 }
danielk19770202b292004-06-09 09:55:16 +00001646 sqlite3VdbeChangeP3(v, addr, (void *)&keyInfo, P3_KEYINFO);
danielk1977b3bce662005-01-29 08:32:43 +00001647 break;
drhfef52082000-06-06 01:50:43 +00001648 }
1649
drh51522cd2005-01-20 13:36:19 +00001650 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001651 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +00001652 /* This has to be a scalar SELECT. Generate code to put the
1653 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +00001654 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +00001655 */
drh2646da72005-12-09 20:02:05 +00001656 static const Token one = { (u8*)"1", 0, 1 };
drh51522cd2005-01-20 13:36:19 +00001657 Select *pSel;
drhec7429a2005-10-06 16:53:14 +00001658 int iMem;
1659 int sop;
drh1398ad32005-01-19 23:24:50 +00001660
drhec7429a2005-10-06 16:53:14 +00001661 pExpr->iColumn = iMem = pParse->nMem++;
drh51522cd2005-01-20 13:36:19 +00001662 pSel = pExpr->pSelect;
1663 if( pExpr->op==TK_SELECT ){
1664 sop = SRT_Mem;
drhec7429a2005-10-06 16:53:14 +00001665 sqlite3VdbeAddOp(v, OP_MemNull, iMem, 0);
1666 VdbeComment((v, "# Init subquery result"));
drh51522cd2005-01-20 13:36:19 +00001667 }else{
drh51522cd2005-01-20 13:36:19 +00001668 sop = SRT_Exists;
drhec7429a2005-10-06 16:53:14 +00001669 sqlite3VdbeAddOp(v, OP_MemInt, 0, iMem);
1670 VdbeComment((v, "# Init EXISTS result"));
drh51522cd2005-01-20 13:36:19 +00001671 }
drhec7429a2005-10-06 16:53:14 +00001672 sqlite3ExprDelete(pSel->pLimit);
1673 pSel->pLimit = sqlite3Expr(TK_INTEGER, 0, 0, &one);
drh94ccde52007-04-13 16:06:32 +00001674 if( sqlite3Select(pParse, pSel, sop, iMem, 0, 0, 0, 0) ){
1675 return;
1676 }
danielk1977b3bce662005-01-29 08:32:43 +00001677 break;
drhcce7d172000-05-31 15:34:51 +00001678 }
1679 }
danielk1977b3bce662005-01-29 08:32:43 +00001680
drh57dbd7b2005-07-08 18:25:26 +00001681 if( testAddr ){
drhd654be82005-09-20 17:42:23 +00001682 sqlite3VdbeJumpHere(v, testAddr);
danielk1977b3bce662005-01-29 08:32:43 +00001683 }
danielk1977fc976062007-05-10 10:46:56 +00001684
danielk1977b3bce662005-01-29 08:32:43 +00001685 return;
drhcce7d172000-05-31 15:34:51 +00001686}
drh51522cd2005-01-20 13:36:19 +00001687#endif /* SQLITE_OMIT_SUBQUERY */
drhcce7d172000-05-31 15:34:51 +00001688
drhcce7d172000-05-31 15:34:51 +00001689/*
drhfec19aa2004-05-19 20:41:03 +00001690** Generate an instruction that will put the integer describe by
1691** text z[0..n-1] on the stack.
1692*/
1693static void codeInteger(Vdbe *v, const char *z, int n){
drh17435752007-08-16 04:30:38 +00001694 assert( z || v==0 || sqlite3DbOfVdbe(v)->mallocFailed );
danielk1977c9cf9012007-05-30 10:36:47 +00001695 if( z ){
1696 int i;
1697 if( sqlite3GetInt32(z, &i) ){
1698 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
1699 }else if( sqlite3FitsIn64Bits(z) ){
1700 sqlite3VdbeOp3(v, OP_Int64, 0, 0, z, n);
1701 }else{
1702 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n);
1703 }
drhfec19aa2004-05-19 20:41:03 +00001704 }
1705}
1706
drh945498f2007-02-24 11:52:52 +00001707
1708/*
1709** Generate code that will extract the iColumn-th column from
1710** table pTab and push that column value on the stack. There
1711** is an open cursor to pTab in iTable. If iColumn<0 then
1712** code is generated that extracts the rowid.
1713*/
1714void sqlite3ExprCodeGetColumn(Vdbe *v, Table *pTab, int iColumn, int iTable){
1715 if( iColumn<0 ){
1716 int op = (pTab && IsVirtual(pTab)) ? OP_VRowid : OP_Rowid;
1717 sqlite3VdbeAddOp(v, op, iTable, 0);
1718 }else if( pTab==0 ){
1719 sqlite3VdbeAddOp(v, OP_Column, iTable, iColumn);
1720 }else{
1721 int op = IsVirtual(pTab) ? OP_VColumn : OP_Column;
1722 sqlite3VdbeAddOp(v, op, iTable, iColumn);
1723 sqlite3ColumnDefault(v, pTab, iColumn);
1724#ifndef SQLITE_OMIT_FLOATING_POINT
1725 if( pTab->aCol[iColumn].affinity==SQLITE_AFF_REAL ){
1726 sqlite3VdbeAddOp(v, OP_RealAffinity, 0, 0);
1727 }
1728#endif
1729 }
1730}
1731
drhfec19aa2004-05-19 20:41:03 +00001732/*
drhcce7d172000-05-31 15:34:51 +00001733** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +00001734** expression and leave the result on the top of stack.
drhf2bc0132004-10-04 13:19:23 +00001735**
1736** This code depends on the fact that certain token values (ex: TK_EQ)
1737** are the same as opcode values (ex: OP_Eq) that implement the corresponding
1738** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
1739** the make process cause these values to align. Assert()s in the code
1740** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00001741*/
danielk19774adee202004-05-08 08:23:19 +00001742void sqlite3ExprCode(Parse *pParse, Expr *pExpr){
drhcce7d172000-05-31 15:34:51 +00001743 Vdbe *v = pParse->pVdbe;
1744 int op;
drhffe07b22005-11-03 00:41:17 +00001745 int stackChng = 1; /* Amount of change to stack depth */
1746
danielk19777977a172004-11-09 12:44:37 +00001747 if( v==0 ) return;
1748 if( pExpr==0 ){
drhf0863fe2005-06-12 21:35:51 +00001749 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
danielk19777977a172004-11-09 12:44:37 +00001750 return;
1751 }
drhf2bc0132004-10-04 13:19:23 +00001752 op = pExpr->op;
1753 switch( op ){
drh13449892005-09-07 21:22:45 +00001754 case TK_AGG_COLUMN: {
1755 AggInfo *pAggInfo = pExpr->pAggInfo;
1756 struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg];
1757 if( !pAggInfo->directMode ){
1758 sqlite3VdbeAddOp(v, OP_MemLoad, pCol->iMem, 0);
1759 break;
1760 }else if( pAggInfo->useSortingIdx ){
1761 sqlite3VdbeAddOp(v, OP_Column, pAggInfo->sortingIdx,
1762 pCol->iSorterColumn);
1763 break;
1764 }
1765 /* Otherwise, fall thru into the TK_COLUMN case */
1766 }
drh967e8b72000-06-21 13:59:10 +00001767 case TK_COLUMN: {
drhffe07b22005-11-03 00:41:17 +00001768 if( pExpr->iTable<0 ){
1769 /* This only happens when coding check constraints */
1770 assert( pParse->ckOffset>0 );
1771 sqlite3VdbeAddOp(v, OP_Dup, pParse->ckOffset-pExpr->iColumn-1, 1);
drhc4a3c772001-04-04 11:48:57 +00001772 }else{
drh945498f2007-02-24 11:52:52 +00001773 sqlite3ExprCodeGetColumn(v, pExpr->pTab, pExpr->iColumn, pExpr->iTable);
drh22827922000-06-06 17:27:05 +00001774 }
drhcce7d172000-05-31 15:34:51 +00001775 break;
1776 }
1777 case TK_INTEGER: {
drh2646da72005-12-09 20:02:05 +00001778 codeInteger(v, (char*)pExpr->token.z, pExpr->token.n);
drhfec19aa2004-05-19 20:41:03 +00001779 break;
1780 }
1781 case TK_FLOAT:
1782 case TK_STRING: {
drhf2bc0132004-10-04 13:19:23 +00001783 assert( TK_FLOAT==OP_Real );
1784 assert( TK_STRING==OP_String8 );
danielk19771e536952007-08-16 10:09:01 +00001785 sqlite3DequoteExpr(pParse->db, pExpr);
drh2646da72005-12-09 20:02:05 +00001786 sqlite3VdbeOp3(v, op, 0, 0, (char*)pExpr->token.z, pExpr->token.n);
drhcce7d172000-05-31 15:34:51 +00001787 break;
1788 }
drhf0863fe2005-06-12 21:35:51 +00001789 case TK_NULL: {
1790 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
1791 break;
1792 }
danielk19775338a5f2005-01-20 13:03:10 +00001793#ifndef SQLITE_OMIT_BLOB_LITERAL
danielk1977c572ef72004-05-27 09:28:41 +00001794 case TK_BLOB: {
drh6c8c6ce2005-08-23 11:17:58 +00001795 int n;
1796 const char *z;
drhf2bc0132004-10-04 13:19:23 +00001797 assert( TK_BLOB==OP_HexBlob );
drh6c8c6ce2005-08-23 11:17:58 +00001798 n = pExpr->token.n - 3;
drh2646da72005-12-09 20:02:05 +00001799 z = (char*)pExpr->token.z + 2;
drh6c8c6ce2005-08-23 11:17:58 +00001800 assert( n>=0 );
1801 if( n==0 ){
1802 z = "";
1803 }
1804 sqlite3VdbeOp3(v, op, 0, 0, z, n);
danielk1977c572ef72004-05-27 09:28:41 +00001805 break;
1806 }
danielk19775338a5f2005-01-20 13:03:10 +00001807#endif
drh50457892003-09-06 01:10:47 +00001808 case TK_VARIABLE: {
danielk19774adee202004-05-08 08:23:19 +00001809 sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0);
drh895d7472004-08-20 16:02:39 +00001810 if( pExpr->token.n>1 ){
drh2646da72005-12-09 20:02:05 +00001811 sqlite3VdbeChangeP3(v, -1, (char*)pExpr->token.z, pExpr->token.n);
drh895d7472004-08-20 16:02:39 +00001812 }
drh50457892003-09-06 01:10:47 +00001813 break;
1814 }
drh4e0cff62004-11-05 05:10:28 +00001815 case TK_REGISTER: {
1816 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iTable, 0);
1817 break;
1818 }
drh487e2622005-06-25 18:42:14 +00001819#ifndef SQLITE_OMIT_CAST
1820 case TK_CAST: {
1821 /* Expressions of the form: CAST(pLeft AS token) */
danielk1977f0113002006-01-24 12:09:17 +00001822 int aff, to_op;
drh487e2622005-06-25 18:42:14 +00001823 sqlite3ExprCode(pParse, pExpr->pLeft);
drh8a512562005-11-14 22:29:05 +00001824 aff = sqlite3AffinityType(&pExpr->token);
danielk1977f0113002006-01-24 12:09:17 +00001825 to_op = aff - SQLITE_AFF_TEXT + OP_ToText;
1826 assert( to_op==OP_ToText || aff!=SQLITE_AFF_TEXT );
1827 assert( to_op==OP_ToBlob || aff!=SQLITE_AFF_NONE );
1828 assert( to_op==OP_ToNumeric || aff!=SQLITE_AFF_NUMERIC );
1829 assert( to_op==OP_ToInt || aff!=SQLITE_AFF_INTEGER );
1830 assert( to_op==OP_ToReal || aff!=SQLITE_AFF_REAL );
1831 sqlite3VdbeAddOp(v, to_op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001832 stackChng = 0;
drh487e2622005-06-25 18:42:14 +00001833 break;
1834 }
1835#endif /* SQLITE_OMIT_CAST */
drhc9b84a12002-06-20 11:36:48 +00001836 case TK_LT:
1837 case TK_LE:
1838 case TK_GT:
1839 case TK_GE:
1840 case TK_NE:
1841 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00001842 assert( TK_LT==OP_Lt );
1843 assert( TK_LE==OP_Le );
1844 assert( TK_GT==OP_Gt );
1845 assert( TK_GE==OP_Ge );
1846 assert( TK_EQ==OP_Eq );
1847 assert( TK_NE==OP_Ne );
danielk1977a37cdde2004-05-16 11:15:36 +00001848 sqlite3ExprCode(pParse, pExpr->pLeft);
1849 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001850 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001851 stackChng = -1;
danielk1977a37cdde2004-05-16 11:15:36 +00001852 break;
drhc9b84a12002-06-20 11:36:48 +00001853 }
drhcce7d172000-05-31 15:34:51 +00001854 case TK_AND:
1855 case TK_OR:
1856 case TK_PLUS:
1857 case TK_STAR:
1858 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00001859 case TK_REM:
1860 case TK_BITAND:
1861 case TK_BITOR:
drh17c40292004-07-21 02:53:29 +00001862 case TK_SLASH:
drhbf4133c2001-10-13 02:59:08 +00001863 case TK_LSHIFT:
drh855eb1c2004-08-31 13:45:11 +00001864 case TK_RSHIFT:
drh00400772000-06-16 20:51:26 +00001865 case TK_CONCAT: {
drhf2bc0132004-10-04 13:19:23 +00001866 assert( TK_AND==OP_And );
1867 assert( TK_OR==OP_Or );
1868 assert( TK_PLUS==OP_Add );
1869 assert( TK_MINUS==OP_Subtract );
1870 assert( TK_REM==OP_Remainder );
1871 assert( TK_BITAND==OP_BitAnd );
1872 assert( TK_BITOR==OP_BitOr );
1873 assert( TK_SLASH==OP_Divide );
1874 assert( TK_LSHIFT==OP_ShiftLeft );
1875 assert( TK_RSHIFT==OP_ShiftRight );
1876 assert( TK_CONCAT==OP_Concat );
danielk19774adee202004-05-08 08:23:19 +00001877 sqlite3ExprCode(pParse, pExpr->pLeft);
1878 sqlite3ExprCode(pParse, pExpr->pRight);
drh855eb1c2004-08-31 13:45:11 +00001879 sqlite3VdbeAddOp(v, op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001880 stackChng = -1;
drh00400772000-06-16 20:51:26 +00001881 break;
1882 }
drhcce7d172000-05-31 15:34:51 +00001883 case TK_UMINUS: {
drhfec19aa2004-05-19 20:41:03 +00001884 Expr *pLeft = pExpr->pLeft;
1885 assert( pLeft );
1886 if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
1887 Token *p = &pLeft->token;
danielk19771e536952007-08-16 10:09:01 +00001888 char *z = sqlite3MPrintf(pParse->db, "-%.*s", p->n, p->z);
drhfec19aa2004-05-19 20:41:03 +00001889 if( pLeft->op==TK_FLOAT ){
1890 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001891 }else{
drhfec19aa2004-05-19 20:41:03 +00001892 codeInteger(v, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001893 }
drh17435752007-08-16 04:30:38 +00001894 sqlite3_free(z);
drh6e142f52000-06-08 13:36:40 +00001895 break;
1896 }
drh1ccde152000-06-17 13:12:39 +00001897 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +00001898 }
drhbf4133c2001-10-13 02:59:08 +00001899 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00001900 case TK_NOT: {
drhf2bc0132004-10-04 13:19:23 +00001901 assert( TK_BITNOT==OP_BitNot );
1902 assert( TK_NOT==OP_Not );
danielk19774adee202004-05-08 08:23:19 +00001903 sqlite3ExprCode(pParse, pExpr->pLeft);
1904 sqlite3VdbeAddOp(v, op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001905 stackChng = 0;
drhcce7d172000-05-31 15:34:51 +00001906 break;
1907 }
1908 case TK_ISNULL:
1909 case TK_NOTNULL: {
1910 int dest;
drhf2bc0132004-10-04 13:19:23 +00001911 assert( TK_ISNULL==OP_IsNull );
1912 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00001913 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1914 sqlite3ExprCode(pParse, pExpr->pLeft);
1915 dest = sqlite3VdbeCurrentAddr(v) + 2;
1916 sqlite3VdbeAddOp(v, op, 1, dest);
1917 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);
drhffe07b22005-11-03 00:41:17 +00001918 stackChng = 0;
drhf2bc0132004-10-04 13:19:23 +00001919 break;
drhcce7d172000-05-31 15:34:51 +00001920 }
drh22827922000-06-06 17:27:05 +00001921 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00001922 AggInfo *pInfo = pExpr->pAggInfo;
drh7e56e712005-11-16 12:53:15 +00001923 if( pInfo==0 ){
1924 sqlite3ErrorMsg(pParse, "misuse of aggregate: %T",
1925 &pExpr->span);
1926 }else{
1927 sqlite3VdbeAddOp(v, OP_MemLoad, pInfo->aFunc[pExpr->iAgg].iMem, 0);
1928 }
drh22827922000-06-06 17:27:05 +00001929 break;
1930 }
drhb71090f2005-05-23 17:26:51 +00001931 case TK_CONST_FUNC:
drhcce7d172000-05-31 15:34:51 +00001932 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00001933 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00001934 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00001935 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00001936 int nId;
1937 const char *zId;
drh13449892005-09-07 21:22:45 +00001938 int constMask = 0;
danielk1977682f68b2004-06-05 10:22:17 +00001939 int i;
drh17435752007-08-16 04:30:38 +00001940 sqlite3 *db = pParse->db;
1941 u8 enc = ENC(db);
danielk1977dc1bdc42004-06-11 10:51:27 +00001942 CollSeq *pColl = 0;
drh17435752007-08-16 04:30:38 +00001943
drh2646da72005-12-09 20:02:05 +00001944 zId = (char*)pExpr->token.z;
drhb71090f2005-05-23 17:26:51 +00001945 nId = pExpr->token.n;
danielk1977d8123362004-06-12 09:25:12 +00001946 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001947 assert( pDef!=0 );
drhf9b596e2004-05-26 16:54:42 +00001948 nExpr = sqlite3ExprCodeExprList(pParse, pList);
drhb7f6f682006-07-08 17:06:43 +00001949#ifndef SQLITE_OMIT_VIRTUALTABLE
drha43fa222006-07-08 18:41:37 +00001950 /* Possibly overload the function if the first argument is
1951 ** a virtual table column.
1952 **
1953 ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the
1954 ** second argument, not the first, as the argument to test to
1955 ** see if it is a column in a virtual table. This is done because
1956 ** the left operand of infix functions (the operand we want to
1957 ** control overloading) ends up as the second argument to the
1958 ** function. The expression "A glob B" is equivalent to
1959 ** "glob(B,A). We want to use the A in "A glob B" to test
1960 ** for function overloading. But we use the B term in "glob(B,A)".
1961 */
drh6a03a1c2006-07-08 18:34:59 +00001962 if( nExpr>=2 && (pExpr->flags & EP_InfixFunc) ){
drh17435752007-08-16 04:30:38 +00001963 pDef = sqlite3VtabOverloadFunction(db, pDef, nExpr, pList->a[1].pExpr);
drh6a03a1c2006-07-08 18:34:59 +00001964 }else if( nExpr>0 ){
drh17435752007-08-16 04:30:38 +00001965 pDef = sqlite3VtabOverloadFunction(db, pDef, nExpr, pList->a[0].pExpr);
drhb7f6f682006-07-08 17:06:43 +00001966 }
1967#endif
danielk1977682f68b2004-06-05 10:22:17 +00001968 for(i=0; i<nExpr && i<32; i++){
danielk1977d02eb1f2004-06-06 09:44:03 +00001969 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
drh13449892005-09-07 21:22:45 +00001970 constMask |= (1<<i);
danielk1977d02eb1f2004-06-06 09:44:03 +00001971 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001972 if( pDef->needCollSeq && !pColl ){
1973 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
1974 }
1975 }
1976 if( pDef->needCollSeq ){
1977 if( !pColl ) pColl = pParse->db->pDfltColl;
danielk1977d8123362004-06-12 09:25:12 +00001978 sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ);
danielk1977682f68b2004-06-05 10:22:17 +00001979 }
drh13449892005-09-07 21:22:45 +00001980 sqlite3VdbeOp3(v, OP_Function, constMask, nExpr, (char*)pDef, P3_FUNCDEF);
drhffe07b22005-11-03 00:41:17 +00001981 stackChng = 1-nExpr;
drhcce7d172000-05-31 15:34:51 +00001982 break;
1983 }
drhfe2093d2005-01-20 22:48:47 +00001984#ifndef SQLITE_OMIT_SUBQUERY
1985 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001986 case TK_SELECT: {
drh41714d62006-03-02 04:44:23 +00001987 if( pExpr->iColumn==0 ){
1988 sqlite3CodeSubselect(pParse, pExpr);
1989 }
danielk19774adee202004-05-08 08:23:19 +00001990 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drhad6d9462004-09-19 02:15:24 +00001991 VdbeComment((v, "# load subquery result"));
drh19a775c2000-06-05 18:54:46 +00001992 break;
1993 }
drhfef52082000-06-06 01:50:43 +00001994 case TK_IN: {
1995 int addr;
drh94a11212004-09-25 13:12:14 +00001996 char affinity;
drhafa5f682006-01-30 14:36:59 +00001997 int ckOffset = pParse->ckOffset;
danielk1977b3bce662005-01-29 08:32:43 +00001998 sqlite3CodeSubselect(pParse, pExpr);
danielk1977e014a832004-05-17 10:48:57 +00001999
2000 /* Figure out the affinity to use to create a key from the results
2001 ** of the expression. affinityStr stores a static string suitable for
danielk1977ededfd52004-06-17 07:53:01 +00002002 ** P3 of OP_MakeRecord.
danielk1977e014a832004-05-17 10:48:57 +00002003 */
drh94a11212004-09-25 13:12:14 +00002004 affinity = comparisonAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +00002005
danielk19774adee202004-05-08 08:23:19 +00002006 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
danielk1977cdbd8ef2007-05-12 06:11:12 +00002007 pParse->ckOffset = (ckOffset ? (ckOffset+1) : 0);
danielk1977e014a832004-05-17 10:48:57 +00002008
2009 /* Code the <expr> from "<expr> IN (...)". The temporary table
2010 ** pExpr->iTable contains the values that make up the (...) set.
2011 */
danielk19774adee202004-05-08 08:23:19 +00002012 sqlite3ExprCode(pParse, pExpr->pLeft);
2013 addr = sqlite3VdbeCurrentAddr(v);
danielk1977e014a832004-05-17 10:48:57 +00002014 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); /* addr + 0 */
danielk19774adee202004-05-08 08:23:19 +00002015 sqlite3VdbeAddOp(v, OP_Pop, 2, 0);
drhf0863fe2005-06-12 21:35:51 +00002016 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +00002017 sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7);
drh94a11212004-09-25 13:12:14 +00002018 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1); /* addr + 4 */
danielk1977e014a832004-05-17 10:48:57 +00002019 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7);
2020 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); /* addr + 6 */
2021
drhfef52082000-06-06 01:50:43 +00002022 break;
2023 }
danielk197793758c82005-01-21 08:13:14 +00002024#endif
drhfef52082000-06-06 01:50:43 +00002025 case TK_BETWEEN: {
drhbe5c89a2004-07-26 00:31:09 +00002026 Expr *pLeft = pExpr->pLeft;
2027 struct ExprList_item *pLItem = pExpr->pList->a;
2028 Expr *pRight = pLItem->pExpr;
2029 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00002030 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00002031 sqlite3ExprCode(pParse, pRight);
2032 codeCompare(pParse, pLeft, pRight, OP_Ge, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00002033 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
drhbe5c89a2004-07-26 00:31:09 +00002034 pLItem++;
2035 pRight = pLItem->pExpr;
2036 sqlite3ExprCode(pParse, pRight);
2037 codeCompare(pParse, pLeft, pRight, OP_Le, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00002038 sqlite3VdbeAddOp(v, OP_And, 0, 0);
drhfef52082000-06-06 01:50:43 +00002039 break;
2040 }
drh4f07e5f2007-05-14 11:34:46 +00002041 case TK_UPLUS: {
danielk19774adee202004-05-08 08:23:19 +00002042 sqlite3ExprCode(pParse, pExpr->pLeft);
drhffe07b22005-11-03 00:41:17 +00002043 stackChng = 0;
drha2e00042002-01-22 03:13:42 +00002044 break;
2045 }
drh17a7f8d2002-03-24 13:13:27 +00002046 case TK_CASE: {
2047 int expr_end_label;
drhf5905aa2002-05-26 20:54:33 +00002048 int jumpInst;
drhf5905aa2002-05-26 20:54:33 +00002049 int nExpr;
drh17a7f8d2002-03-24 13:13:27 +00002050 int i;
drhbe5c89a2004-07-26 00:31:09 +00002051 ExprList *pEList;
2052 struct ExprList_item *aListelem;
drh17a7f8d2002-03-24 13:13:27 +00002053
2054 assert(pExpr->pList);
2055 assert((pExpr->pList->nExpr % 2) == 0);
2056 assert(pExpr->pList->nExpr > 0);
drhbe5c89a2004-07-26 00:31:09 +00002057 pEList = pExpr->pList;
2058 aListelem = pEList->a;
2059 nExpr = pEList->nExpr;
danielk19774adee202004-05-08 08:23:19 +00002060 expr_end_label = sqlite3VdbeMakeLabel(v);
drh17a7f8d2002-03-24 13:13:27 +00002061 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00002062 sqlite3ExprCode(pParse, pExpr->pLeft);
drh17a7f8d2002-03-24 13:13:27 +00002063 }
drhf5905aa2002-05-26 20:54:33 +00002064 for(i=0; i<nExpr; i=i+2){
drhbe5c89a2004-07-26 00:31:09 +00002065 sqlite3ExprCode(pParse, aListelem[i].pExpr);
drh17a7f8d2002-03-24 13:13:27 +00002066 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00002067 sqlite3VdbeAddOp(v, OP_Dup, 1, 1);
drhbe5c89a2004-07-26 00:31:09 +00002068 jumpInst = codeCompare(pParse, pExpr->pLeft, aListelem[i].pExpr,
2069 OP_Ne, 0, 1);
danielk19774adee202004-05-08 08:23:19 +00002070 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00002071 }else{
danielk19774adee202004-05-08 08:23:19 +00002072 jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00002073 }
drhbe5c89a2004-07-26 00:31:09 +00002074 sqlite3ExprCode(pParse, aListelem[i+1].pExpr);
danielk19774adee202004-05-08 08:23:19 +00002075 sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label);
drhd654be82005-09-20 17:42:23 +00002076 sqlite3VdbeJumpHere(v, jumpInst);
drh17a7f8d2002-03-24 13:13:27 +00002077 }
drhf570f012002-05-31 15:51:25 +00002078 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00002079 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhf570f012002-05-31 15:51:25 +00002080 }
drh17a7f8d2002-03-24 13:13:27 +00002081 if( pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00002082 sqlite3ExprCode(pParse, pExpr->pRight);
drh17a7f8d2002-03-24 13:13:27 +00002083 }else{
drhf0863fe2005-06-12 21:35:51 +00002084 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
drh17a7f8d2002-03-24 13:13:27 +00002085 }
danielk19774adee202004-05-08 08:23:19 +00002086 sqlite3VdbeResolveLabel(v, expr_end_label);
danielk19776f349032002-06-11 02:25:40 +00002087 break;
2088 }
danielk19775338a5f2005-01-20 13:03:10 +00002089#ifndef SQLITE_OMIT_TRIGGER
danielk19776f349032002-06-11 02:25:40 +00002090 case TK_RAISE: {
2091 if( !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +00002092 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00002093 "RAISE() may only be used within a trigger-program");
drhfd131da2007-08-07 17:13:03 +00002094 return;
danielk19776f349032002-06-11 02:25:40 +00002095 }
drhad6d9462004-09-19 02:15:24 +00002096 if( pExpr->iColumn!=OE_Ignore ){
2097 assert( pExpr->iColumn==OE_Rollback ||
2098 pExpr->iColumn == OE_Abort ||
2099 pExpr->iColumn == OE_Fail );
danielk19771e536952007-08-16 10:09:01 +00002100 sqlite3DequoteExpr(pParse->db, pExpr);
drhad6d9462004-09-19 02:15:24 +00002101 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn,
drh2646da72005-12-09 20:02:05 +00002102 (char*)pExpr->token.z, pExpr->token.n);
danielk19776f349032002-06-11 02:25:40 +00002103 } else {
drhad6d9462004-09-19 02:15:24 +00002104 assert( pExpr->iColumn == OE_Ignore );
2105 sqlite3VdbeAddOp(v, OP_ContextPop, 0, 0);
2106 sqlite3VdbeAddOp(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
2107 VdbeComment((v, "# raise(IGNORE)"));
danielk19776f349032002-06-11 02:25:40 +00002108 }
drhffe07b22005-11-03 00:41:17 +00002109 stackChng = 0;
2110 break;
drh17a7f8d2002-03-24 13:13:27 +00002111 }
danielk19775338a5f2005-01-20 13:03:10 +00002112#endif
drhffe07b22005-11-03 00:41:17 +00002113 }
2114
2115 if( pParse->ckOffset ){
2116 pParse->ckOffset += stackChng;
2117 assert( pParse->ckOffset );
drhcce7d172000-05-31 15:34:51 +00002118 }
drhcce7d172000-05-31 15:34:51 +00002119}
2120
danielk197793758c82005-01-21 08:13:14 +00002121#ifndef SQLITE_OMIT_TRIGGER
drhcce7d172000-05-31 15:34:51 +00002122/*
drh25303782004-12-07 15:41:48 +00002123** Generate code that evalutes the given expression and leaves the result
2124** on the stack. See also sqlite3ExprCode().
2125**
2126** This routine might also cache the result and modify the pExpr tree
2127** so that it will make use of the cached result on subsequent evaluations
2128** rather than evaluate the whole expression again. Trivial expressions are
2129** not cached. If the expression is cached, its result is stored in a
2130** memory location.
2131*/
2132void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr){
2133 Vdbe *v = pParse->pVdbe;
2134 int iMem;
2135 int addr1, addr2;
2136 if( v==0 ) return;
2137 addr1 = sqlite3VdbeCurrentAddr(v);
2138 sqlite3ExprCode(pParse, pExpr);
2139 addr2 = sqlite3VdbeCurrentAddr(v);
2140 if( addr2>addr1+1 || sqlite3VdbeGetOp(v, addr1)->opcode==OP_Function ){
2141 iMem = pExpr->iTable = pParse->nMem++;
2142 sqlite3VdbeAddOp(v, OP_MemStore, iMem, 0);
2143 pExpr->op = TK_REGISTER;
2144 }
2145}
danielk197793758c82005-01-21 08:13:14 +00002146#endif
drh25303782004-12-07 15:41:48 +00002147
2148/*
drh268380c2004-02-25 13:47:31 +00002149** Generate code that pushes the value of every element of the given
drhf9b596e2004-05-26 16:54:42 +00002150** expression list onto the stack.
drh268380c2004-02-25 13:47:31 +00002151**
2152** Return the number of elements pushed onto the stack.
2153*/
danielk19774adee202004-05-08 08:23:19 +00002154int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00002155 Parse *pParse, /* Parsing context */
drhf9b596e2004-05-26 16:54:42 +00002156 ExprList *pList /* The expression list to be coded */
drh268380c2004-02-25 13:47:31 +00002157){
2158 struct ExprList_item *pItem;
2159 int i, n;
drh268380c2004-02-25 13:47:31 +00002160 if( pList==0 ) return 0;
drh268380c2004-02-25 13:47:31 +00002161 n = pList->nExpr;
drhc182d162005-08-14 20:47:16 +00002162 for(pItem=pList->a, i=n; i>0; i--, pItem++){
danielk19774adee202004-05-08 08:23:19 +00002163 sqlite3ExprCode(pParse, pItem->pExpr);
drh268380c2004-02-25 13:47:31 +00002164 }
drhf9b596e2004-05-26 16:54:42 +00002165 return n;
drh268380c2004-02-25 13:47:31 +00002166}
2167
2168/*
drhcce7d172000-05-31 15:34:51 +00002169** Generate code for a boolean expression such that a jump is made
2170** to the label "dest" if the expression is true but execution
2171** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00002172**
2173** If the expression evaluates to NULL (neither true nor false), then
2174** take the jump if the jumpIfNull flag is true.
drhf2bc0132004-10-04 13:19:23 +00002175**
2176** This code depends on the fact that certain token values (ex: TK_EQ)
2177** are the same as opcode values (ex: OP_Eq) that implement the corresponding
2178** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
2179** the make process cause these values to align. Assert()s in the code
2180** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00002181*/
danielk19774adee202004-05-08 08:23:19 +00002182void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00002183 Vdbe *v = pParse->pVdbe;
2184 int op = 0;
drhffe07b22005-11-03 00:41:17 +00002185 int ckOffset = pParse->ckOffset;
drhdaffd0e2001-04-11 14:28:42 +00002186 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00002187 op = pExpr->op;
2188 switch( op ){
drhcce7d172000-05-31 15:34:51 +00002189 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00002190 int d2 = sqlite3VdbeMakeLabel(v);
2191 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
2192 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
2193 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00002194 break;
2195 }
2196 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00002197 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
2198 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002199 break;
2200 }
2201 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00002202 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002203 break;
2204 }
2205 case TK_LT:
2206 case TK_LE:
2207 case TK_GT:
2208 case TK_GE:
2209 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00002210 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00002211 assert( TK_LT==OP_Lt );
2212 assert( TK_LE==OP_Le );
2213 assert( TK_GT==OP_Gt );
2214 assert( TK_GE==OP_Ge );
2215 assert( TK_EQ==OP_Eq );
2216 assert( TK_NE==OP_Ne );
danielk19774adee202004-05-08 08:23:19 +00002217 sqlite3ExprCode(pParse, pExpr->pLeft);
2218 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00002219 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002220 break;
2221 }
2222 case TK_ISNULL:
2223 case TK_NOTNULL: {
drhf2bc0132004-10-04 13:19:23 +00002224 assert( TK_ISNULL==OP_IsNull );
2225 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00002226 sqlite3ExprCode(pParse, pExpr->pLeft);
2227 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00002228 break;
2229 }
drhfef52082000-06-06 01:50:43 +00002230 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00002231 /* The expression "x BETWEEN y AND z" is implemented as:
2232 **
2233 ** 1 IF (x < y) GOTO 3
2234 ** 2 IF (x <= z) GOTO <dest>
2235 ** 3 ...
2236 */
drhf5905aa2002-05-26 20:54:33 +00002237 int addr;
drhbe5c89a2004-07-26 00:31:09 +00002238 Expr *pLeft = pExpr->pLeft;
2239 Expr *pRight = pExpr->pList->a[0].pExpr;
2240 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00002241 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00002242 sqlite3ExprCode(pParse, pRight);
2243 addr = codeCompare(pParse, pLeft, pRight, OP_Lt, 0, !jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00002244
drhbe5c89a2004-07-26 00:31:09 +00002245 pRight = pExpr->pList->a[1].pExpr;
2246 sqlite3ExprCode(pParse, pRight);
2247 codeCompare(pParse, pLeft, pRight, OP_Le, dest, jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00002248
danielk19774adee202004-05-08 08:23:19 +00002249 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
drhd654be82005-09-20 17:42:23 +00002250 sqlite3VdbeJumpHere(v, addr);
danielk19774adee202004-05-08 08:23:19 +00002251 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00002252 break;
2253 }
drhcce7d172000-05-31 15:34:51 +00002254 default: {
danielk19774adee202004-05-08 08:23:19 +00002255 sqlite3ExprCode(pParse, pExpr);
2256 sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00002257 break;
2258 }
2259 }
drhffe07b22005-11-03 00:41:17 +00002260 pParse->ckOffset = ckOffset;
drhcce7d172000-05-31 15:34:51 +00002261}
2262
2263/*
drh66b89c82000-11-28 20:47:17 +00002264** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00002265** to the label "dest" if the expression is false but execution
2266** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00002267**
2268** If the expression evaluates to NULL (neither true nor false) then
2269** jump if jumpIfNull is true or fall through if jumpIfNull is false.
drhcce7d172000-05-31 15:34:51 +00002270*/
danielk19774adee202004-05-08 08:23:19 +00002271void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00002272 Vdbe *v = pParse->pVdbe;
2273 int op = 0;
drhffe07b22005-11-03 00:41:17 +00002274 int ckOffset = pParse->ckOffset;
drhdaffd0e2001-04-11 14:28:42 +00002275 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00002276
2277 /* The value of pExpr->op and op are related as follows:
2278 **
2279 ** pExpr->op op
2280 ** --------- ----------
2281 ** TK_ISNULL OP_NotNull
2282 ** TK_NOTNULL OP_IsNull
2283 ** TK_NE OP_Eq
2284 ** TK_EQ OP_Ne
2285 ** TK_GT OP_Le
2286 ** TK_LE OP_Gt
2287 ** TK_GE OP_Lt
2288 ** TK_LT OP_Ge
2289 **
2290 ** For other values of pExpr->op, op is undefined and unused.
2291 ** The value of TK_ and OP_ constants are arranged such that we
2292 ** can compute the mapping above using the following expression.
2293 ** Assert()s verify that the computation is correct.
2294 */
2295 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
2296
2297 /* Verify correct alignment of TK_ and OP_ constants
2298 */
2299 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
2300 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
2301 assert( pExpr->op!=TK_NE || op==OP_Eq );
2302 assert( pExpr->op!=TK_EQ || op==OP_Ne );
2303 assert( pExpr->op!=TK_LT || op==OP_Ge );
2304 assert( pExpr->op!=TK_LE || op==OP_Gt );
2305 assert( pExpr->op!=TK_GT || op==OP_Le );
2306 assert( pExpr->op!=TK_GE || op==OP_Lt );
2307
drhcce7d172000-05-31 15:34:51 +00002308 switch( pExpr->op ){
2309 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00002310 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
2311 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002312 break;
2313 }
2314 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00002315 int d2 = sqlite3VdbeMakeLabel(v);
2316 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
2317 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
2318 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00002319 break;
2320 }
2321 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00002322 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002323 break;
2324 }
2325 case TK_LT:
2326 case TK_LE:
2327 case TK_GT:
2328 case TK_GE:
2329 case TK_NE:
2330 case TK_EQ: {
danielk19774adee202004-05-08 08:23:19 +00002331 sqlite3ExprCode(pParse, pExpr->pLeft);
2332 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00002333 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002334 break;
2335 }
drhcce7d172000-05-31 15:34:51 +00002336 case TK_ISNULL:
2337 case TK_NOTNULL: {
danielk19774adee202004-05-08 08:23:19 +00002338 sqlite3ExprCode(pParse, pExpr->pLeft);
2339 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00002340 break;
2341 }
drhfef52082000-06-06 01:50:43 +00002342 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00002343 /* The expression is "x BETWEEN y AND z". It is implemented as:
2344 **
2345 ** 1 IF (x >= y) GOTO 3
2346 ** 2 GOTO <dest>
2347 ** 3 IF (x > z) GOTO <dest>
2348 */
drhfef52082000-06-06 01:50:43 +00002349 int addr;
drhbe5c89a2004-07-26 00:31:09 +00002350 Expr *pLeft = pExpr->pLeft;
2351 Expr *pRight = pExpr->pList->a[0].pExpr;
2352 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00002353 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00002354 sqlite3ExprCode(pParse, pRight);
danielk19774adee202004-05-08 08:23:19 +00002355 addr = sqlite3VdbeCurrentAddr(v);
drhbe5c89a2004-07-26 00:31:09 +00002356 codeCompare(pParse, pLeft, pRight, OP_Ge, addr+3, !jumpIfNull);
2357
danielk19774adee202004-05-08 08:23:19 +00002358 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
2359 sqlite3VdbeAddOp(v, OP_Goto, 0, dest);
drhbe5c89a2004-07-26 00:31:09 +00002360 pRight = pExpr->pList->a[1].pExpr;
2361 sqlite3ExprCode(pParse, pRight);
2362 codeCompare(pParse, pLeft, pRight, OP_Gt, dest, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00002363 break;
2364 }
drhcce7d172000-05-31 15:34:51 +00002365 default: {
danielk19774adee202004-05-08 08:23:19 +00002366 sqlite3ExprCode(pParse, pExpr);
2367 sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00002368 break;
2369 }
2370 }
drhffe07b22005-11-03 00:41:17 +00002371 pParse->ckOffset = ckOffset;
drhcce7d172000-05-31 15:34:51 +00002372}
drh22827922000-06-06 17:27:05 +00002373
2374/*
2375** Do a deep comparison of two expression trees. Return TRUE (non-zero)
2376** if they are identical and return FALSE if they differ in any way.
drhd40aab02007-02-24 15:29:03 +00002377**
2378** Sometimes this routine will return FALSE even if the two expressions
2379** really are equivalent. If we cannot prove that the expressions are
2380** identical, we return FALSE just to be safe. So if this routine
2381** returns false, then you do not really know for certain if the two
2382** expressions are the same. But if you get a TRUE return, then you
2383** can be sure the expressions are the same. In the places where
2384** this routine is used, it does not hurt to get an extra FALSE - that
2385** just might result in some slightly slower code. But returning
2386** an incorrect TRUE could lead to a malfunction.
drh22827922000-06-06 17:27:05 +00002387*/
danielk19774adee202004-05-08 08:23:19 +00002388int sqlite3ExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00002389 int i;
danielk19774b202ae2006-01-23 05:50:58 +00002390 if( pA==0||pB==0 ){
2391 return pB==pA;
drh22827922000-06-06 17:27:05 +00002392 }
2393 if( pA->op!=pB->op ) return 0;
drhfd357972005-09-09 01:33:19 +00002394 if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 0;
danielk19774adee202004-05-08 08:23:19 +00002395 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
2396 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00002397 if( pA->pList ){
2398 if( pB->pList==0 ) return 0;
2399 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
2400 for(i=0; i<pA->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00002401 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00002402 return 0;
2403 }
2404 }
2405 }else if( pB->pList ){
2406 return 0;
2407 }
2408 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00002409 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drhdd735212007-02-24 13:53:05 +00002410 if( pA->op!=TK_COLUMN && pA->token.z ){
drh22827922000-06-06 17:27:05 +00002411 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00002412 if( pB->token.n!=pA->token.n ) return 0;
drh2646da72005-12-09 20:02:05 +00002413 if( sqlite3StrNICmp((char*)pA->token.z,(char*)pB->token.z,pB->token.n)!=0 ){
2414 return 0;
2415 }
drh22827922000-06-06 17:27:05 +00002416 }
2417 return 1;
2418}
2419
drh13449892005-09-07 21:22:45 +00002420
drh22827922000-06-06 17:27:05 +00002421/*
drh13449892005-09-07 21:22:45 +00002422** Add a new element to the pAggInfo->aCol[] array. Return the index of
2423** the new element. Return a negative number if malloc fails.
drh22827922000-06-06 17:27:05 +00002424*/
drh17435752007-08-16 04:30:38 +00002425static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){
drh13449892005-09-07 21:22:45 +00002426 int i;
drhcf643722007-03-27 13:36:37 +00002427 pInfo->aCol = sqlite3ArrayAllocate(
drh17435752007-08-16 04:30:38 +00002428 db,
drhcf643722007-03-27 13:36:37 +00002429 pInfo->aCol,
2430 sizeof(pInfo->aCol[0]),
2431 3,
2432 &pInfo->nColumn,
2433 &pInfo->nColumnAlloc,
2434 &i
2435 );
drh13449892005-09-07 21:22:45 +00002436 return i;
2437}
2438
2439/*
2440** Add a new element to the pAggInfo->aFunc[] array. Return the index of
2441** the new element. Return a negative number if malloc fails.
2442*/
drh17435752007-08-16 04:30:38 +00002443static int addAggInfoFunc(sqlite3 *db, AggInfo *pInfo){
drh13449892005-09-07 21:22:45 +00002444 int i;
drhcf643722007-03-27 13:36:37 +00002445 pInfo->aFunc = sqlite3ArrayAllocate(
drh17435752007-08-16 04:30:38 +00002446 db,
drhcf643722007-03-27 13:36:37 +00002447 pInfo->aFunc,
2448 sizeof(pInfo->aFunc[0]),
2449 3,
2450 &pInfo->nFunc,
2451 &pInfo->nFuncAlloc,
2452 &i
2453 );
drh13449892005-09-07 21:22:45 +00002454 return i;
2455}
drh22827922000-06-06 17:27:05 +00002456
2457/*
drh626a8792005-01-17 22:08:19 +00002458** This is an xFunc for walkExprTree() used to implement
2459** sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates
2460** for additional information.
drh22827922000-06-06 17:27:05 +00002461**
drh626a8792005-01-17 22:08:19 +00002462** This routine analyzes the aggregate function at pExpr.
drh22827922000-06-06 17:27:05 +00002463*/
drh626a8792005-01-17 22:08:19 +00002464static int analyzeAggregate(void *pArg, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00002465 int i;
danielk1977a58fdfb2005-02-08 07:50:40 +00002466 NameContext *pNC = (NameContext *)pArg;
2467 Parse *pParse = pNC->pParse;
2468 SrcList *pSrcList = pNC->pSrcList;
drh13449892005-09-07 21:22:45 +00002469 AggInfo *pAggInfo = pNC->pAggInfo;
drh22827922000-06-06 17:27:05 +00002470
drh22827922000-06-06 17:27:05 +00002471 switch( pExpr->op ){
drh89c69d02007-01-04 01:20:28 +00002472 case TK_AGG_COLUMN:
drh967e8b72000-06-21 13:59:10 +00002473 case TK_COLUMN: {
drh13449892005-09-07 21:22:45 +00002474 /* Check to see if the column is in one of the tables in the FROM
2475 ** clause of the aggregate query */
2476 if( pSrcList ){
2477 struct SrcList_item *pItem = pSrcList->a;
2478 for(i=0; i<pSrcList->nSrc; i++, pItem++){
2479 struct AggInfo_col *pCol;
2480 if( pExpr->iTable==pItem->iCursor ){
2481 /* If we reach this point, it means that pExpr refers to a table
2482 ** that is in the FROM clause of the aggregate query.
2483 **
2484 ** Make an entry for the column in pAggInfo->aCol[] if there
2485 ** is not an entry there already.
2486 */
drh7f906d62007-03-12 23:48:52 +00002487 int k;
drh13449892005-09-07 21:22:45 +00002488 pCol = pAggInfo->aCol;
drh7f906d62007-03-12 23:48:52 +00002489 for(k=0; k<pAggInfo->nColumn; k++, pCol++){
drh13449892005-09-07 21:22:45 +00002490 if( pCol->iTable==pExpr->iTable &&
2491 pCol->iColumn==pExpr->iColumn ){
2492 break;
2493 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002494 }
danielk19771e536952007-08-16 10:09:01 +00002495 if( (k>=pAggInfo->nColumn)
2496 && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0
2497 ){
drh7f906d62007-03-12 23:48:52 +00002498 pCol = &pAggInfo->aCol[k];
danielk19770817d0d2007-02-14 09:19:36 +00002499 pCol->pTab = pExpr->pTab;
drh13449892005-09-07 21:22:45 +00002500 pCol->iTable = pExpr->iTable;
2501 pCol->iColumn = pExpr->iColumn;
2502 pCol->iMem = pParse->nMem++;
2503 pCol->iSorterColumn = -1;
drh5774b802005-09-07 22:48:16 +00002504 pCol->pExpr = pExpr;
drh13449892005-09-07 21:22:45 +00002505 if( pAggInfo->pGroupBy ){
2506 int j, n;
2507 ExprList *pGB = pAggInfo->pGroupBy;
2508 struct ExprList_item *pTerm = pGB->a;
2509 n = pGB->nExpr;
2510 for(j=0; j<n; j++, pTerm++){
2511 Expr *pE = pTerm->pExpr;
2512 if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable &&
2513 pE->iColumn==pExpr->iColumn ){
2514 pCol->iSorterColumn = j;
2515 break;
2516 }
2517 }
2518 }
2519 if( pCol->iSorterColumn<0 ){
2520 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
2521 }
2522 }
2523 /* There is now an entry for pExpr in pAggInfo->aCol[] (either
2524 ** because it was there before or because we just created it).
2525 ** Convert the pExpr to be a TK_AGG_COLUMN referring to that
2526 ** pAggInfo->aCol[] entry.
2527 */
2528 pExpr->pAggInfo = pAggInfo;
2529 pExpr->op = TK_AGG_COLUMN;
drh7f906d62007-03-12 23:48:52 +00002530 pExpr->iAgg = k;
drh13449892005-09-07 21:22:45 +00002531 break;
2532 } /* endif pExpr->iTable==pItem->iCursor */
2533 } /* end loop over pSrcList */
drh22827922000-06-06 17:27:05 +00002534 }
drh626a8792005-01-17 22:08:19 +00002535 return 1;
drh22827922000-06-06 17:27:05 +00002536 }
2537 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00002538 /* The pNC->nDepth==0 test causes aggregate functions in subqueries
2539 ** to be ignored */
danielk1977a58fdfb2005-02-08 07:50:40 +00002540 if( pNC->nDepth==0 ){
drh13449892005-09-07 21:22:45 +00002541 /* Check to see if pExpr is a duplicate of another aggregate
2542 ** function that is already in the pAggInfo structure
2543 */
2544 struct AggInfo_func *pItem = pAggInfo->aFunc;
2545 for(i=0; i<pAggInfo->nFunc; i++, pItem++){
2546 if( sqlite3ExprCompare(pItem->pExpr, pExpr) ){
danielk1977a58fdfb2005-02-08 07:50:40 +00002547 break;
2548 }
drh22827922000-06-06 17:27:05 +00002549 }
drh13449892005-09-07 21:22:45 +00002550 if( i>=pAggInfo->nFunc ){
2551 /* pExpr is original. Make a new entry in pAggInfo->aFunc[]
2552 */
danielk197714db2662006-01-09 16:12:04 +00002553 u8 enc = ENC(pParse->db);
danielk19771e536952007-08-16 10:09:01 +00002554 i = addAggInfoFunc(pParse->db, pAggInfo);
drh13449892005-09-07 21:22:45 +00002555 if( i>=0 ){
2556 pItem = &pAggInfo->aFunc[i];
2557 pItem->pExpr = pExpr;
2558 pItem->iMem = pParse->nMem++;
2559 pItem->pFunc = sqlite3FindFunction(pParse->db,
drh2646da72005-12-09 20:02:05 +00002560 (char*)pExpr->token.z, pExpr->token.n,
drh13449892005-09-07 21:22:45 +00002561 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
drhfd357972005-09-09 01:33:19 +00002562 if( pExpr->flags & EP_Distinct ){
2563 pItem->iDistinct = pParse->nTab++;
2564 }else{
2565 pItem->iDistinct = -1;
2566 }
drh13449892005-09-07 21:22:45 +00002567 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002568 }
drh13449892005-09-07 21:22:45 +00002569 /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
2570 */
danielk1977a58fdfb2005-02-08 07:50:40 +00002571 pExpr->iAgg = i;
drh13449892005-09-07 21:22:45 +00002572 pExpr->pAggInfo = pAggInfo;
danielk1977a58fdfb2005-02-08 07:50:40 +00002573 return 1;
drh22827922000-06-06 17:27:05 +00002574 }
drh22827922000-06-06 17:27:05 +00002575 }
2576 }
drh13449892005-09-07 21:22:45 +00002577
2578 /* Recursively walk subqueries looking for TK_COLUMN nodes that need
2579 ** to be changed to TK_AGG_COLUMN. But increment nDepth so that
2580 ** TK_AGG_FUNCTION nodes in subqueries will be unchanged.
2581 */
danielk1977a58fdfb2005-02-08 07:50:40 +00002582 if( pExpr->pSelect ){
2583 pNC->nDepth++;
2584 walkSelectExpr(pExpr->pSelect, analyzeAggregate, pNC);
2585 pNC->nDepth--;
2586 }
drh626a8792005-01-17 22:08:19 +00002587 return 0;
2588}
2589
2590/*
2591** Analyze the given expression looking for aggregate functions and
2592** for variables that need to be added to the pParse->aAgg[] array.
2593** Make additional entries to the pParse->aAgg[] array as necessary.
2594**
2595** This routine should only be called after the expression has been
2596** analyzed by sqlite3ExprResolveNames().
2597**
2598** If errors are seen, leave an error message in zErrMsg and return
2599** the number of errors.
2600*/
danielk1977a58fdfb2005-02-08 07:50:40 +00002601int sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
2602 int nErr = pNC->pParse->nErr;
2603 walkExprTree(pExpr, analyzeAggregate, pNC);
2604 return pNC->pParse->nErr - nErr;
drh22827922000-06-06 17:27:05 +00002605}
drh5d9a4af2005-08-30 00:54:01 +00002606
2607/*
2608** Call sqlite3ExprAnalyzeAggregates() for every expression in an
2609** expression list. Return the number of errors.
2610**
2611** If an error is found, the analysis is cut short.
2612*/
2613int sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){
2614 struct ExprList_item *pItem;
2615 int i;
2616 int nErr = 0;
2617 if( pList ){
2618 for(pItem=pList->a, i=0; nErr==0 && i<pList->nExpr; i++, pItem++){
2619 nErr += sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
2620 }
2621 }
2622 return nErr;
2623}