blob: 5c281fbacf49218522e37a1860df852911c2f5c0 [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**
danielk19771e536952007-08-16 10:09:01 +000015** $Id: expr.c,v 1.305 2007/08/16 10:09:03 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;
drh4305d102003-07-30 12:34:12 +0000531 pNew->nExpr = pNew->nAlloc = p->nExpr;
drh17435752007-08-16 04:30:38 +0000532 pNew->a = pItem = sqlite3DbMallocRaw(db, p->nExpr*sizeof(p->a[0]) );
danielk1977e0048402004-06-15 16:51:01 +0000533 if( pItem==0 ){
drh17435752007-08-16 04:30:38 +0000534 sqlite3_free(pNew);
danielk1977e0048402004-06-15 16:51:01 +0000535 return 0;
536 }
drh145716b2004-09-24 12:24:06 +0000537 pOldItem = p->a;
538 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
drh4b59ab52002-08-24 18:24:51 +0000539 Expr *pNewExpr, *pOldExpr;
drh17435752007-08-16 04:30:38 +0000540 pItem->pExpr = pNewExpr = sqlite3ExprDup(db, pOldExpr = pOldItem->pExpr);
drh6977fea2002-10-22 23:38:04 +0000541 if( pOldExpr->span.z!=0 && pNewExpr ){
542 /* Always make a copy of the span for top-level expressions in the
drh4b59ab52002-08-24 18:24:51 +0000543 ** expression list. The logic in SELECT processing that determines
544 ** the names of columns in the result set needs this information */
drh17435752007-08-16 04:30:38 +0000545 sqlite3TokenCopy(db, &pNewExpr->span, &pOldExpr->span);
drh4b59ab52002-08-24 18:24:51 +0000546 }
drh1f3e9052002-10-31 00:09:39 +0000547 assert( pNewExpr==0 || pNewExpr->span.z!=0
drh6f7adc82006-01-11 21:41:20 +0000548 || pOldExpr->span.z==0
drh17435752007-08-16 04:30:38 +0000549 || db->mallocFailed );
550 pItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
drh145716b2004-09-24 12:24:06 +0000551 pItem->sortOrder = pOldItem->sortOrder;
552 pItem->isAgg = pOldItem->isAgg;
drh3e7bc9c2004-02-21 19:17:17 +0000553 pItem->done = 0;
drhff78bd22002-02-27 01:47:11 +0000554 }
555 return pNew;
556}
danielk197793758c82005-01-21 08:13:14 +0000557
558/*
559** If cursors, triggers, views and subqueries are all omitted from
560** the build, then none of the following routines, except for
561** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes
562** called with a NULL argument.
563*/
danielk19776a67fe82005-02-04 04:07:16 +0000564#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \
565 || !defined(SQLITE_OMIT_SUBQUERY)
drh17435752007-08-16 04:30:38 +0000566SrcList *sqlite3SrcListDup(sqlite3 *db, SrcList *p){
drhad3cab52002-05-24 02:04:32 +0000567 SrcList *pNew;
568 int i;
drh113088e2003-03-20 01:16:58 +0000569 int nByte;
drhad3cab52002-05-24 02:04:32 +0000570 if( p==0 ) return 0;
drh113088e2003-03-20 01:16:58 +0000571 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
drh17435752007-08-16 04:30:38 +0000572 pNew = sqlite3DbMallocRaw(db, nByte );
drhad3cab52002-05-24 02:04:32 +0000573 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000574 pNew->nSrc = pNew->nAlloc = p->nSrc;
drhad3cab52002-05-24 02:04:32 +0000575 for(i=0; i<p->nSrc; i++){
drh4efc4752004-01-16 15:55:37 +0000576 struct SrcList_item *pNewItem = &pNew->a[i];
577 struct SrcList_item *pOldItem = &p->a[i];
drhed8a3bb2005-06-06 21:19:56 +0000578 Table *pTab;
drh17435752007-08-16 04:30:38 +0000579 pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase);
580 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
581 pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias);
drh4efc4752004-01-16 15:55:37 +0000582 pNewItem->jointype = pOldItem->jointype;
583 pNewItem->iCursor = pOldItem->iCursor;
danielk19771787cca2006-02-10 07:07:14 +0000584 pNewItem->isPopulated = pOldItem->isPopulated;
drhed8a3bb2005-06-06 21:19:56 +0000585 pTab = pNewItem->pTab = pOldItem->pTab;
586 if( pTab ){
587 pTab->nRef++;
danielk1977a1cb1832005-02-12 08:59:55 +0000588 }
drh17435752007-08-16 04:30:38 +0000589 pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect);
590 pNewItem->pOn = sqlite3ExprDup(db, pOldItem->pOn);
591 pNewItem->pUsing = sqlite3IdListDup(db, pOldItem->pUsing);
danielk19776c18b6e2005-01-30 09:17:58 +0000592 pNewItem->colUsed = pOldItem->colUsed;
drhad3cab52002-05-24 02:04:32 +0000593 }
594 return pNew;
595}
drh17435752007-08-16 04:30:38 +0000596IdList *sqlite3IdListDup(sqlite3 *db, IdList *p){
drhff78bd22002-02-27 01:47:11 +0000597 IdList *pNew;
598 int i;
599 if( p==0 ) return 0;
drh17435752007-08-16 04:30:38 +0000600 pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +0000601 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000602 pNew->nId = pNew->nAlloc = p->nId;
drh17435752007-08-16 04:30:38 +0000603 pNew->a = sqlite3DbMallocRaw(db, p->nId*sizeof(p->a[0]) );
danielk1977d5d56522005-03-16 12:15:20 +0000604 if( pNew->a==0 ){
drh17435752007-08-16 04:30:38 +0000605 sqlite3_free(pNew);
danielk1977d5d56522005-03-16 12:15:20 +0000606 return 0;
607 }
drhff78bd22002-02-27 01:47:11 +0000608 for(i=0; i<p->nId; i++){
drh4efc4752004-01-16 15:55:37 +0000609 struct IdList_item *pNewItem = &pNew->a[i];
610 struct IdList_item *pOldItem = &p->a[i];
drh17435752007-08-16 04:30:38 +0000611 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
drh4efc4752004-01-16 15:55:37 +0000612 pNewItem->idx = pOldItem->idx;
drhff78bd22002-02-27 01:47:11 +0000613 }
614 return pNew;
615}
drh17435752007-08-16 04:30:38 +0000616Select *sqlite3SelectDup(sqlite3 *db, Select *p){
drhff78bd22002-02-27 01:47:11 +0000617 Select *pNew;
618 if( p==0 ) return 0;
drh17435752007-08-16 04:30:38 +0000619 pNew = sqlite3DbMallocRaw(db, sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000620 if( pNew==0 ) return 0;
621 pNew->isDistinct = p->isDistinct;
drh17435752007-08-16 04:30:38 +0000622 pNew->pEList = sqlite3ExprListDup(db, p->pEList);
623 pNew->pSrc = sqlite3SrcListDup(db, p->pSrc);
624 pNew->pWhere = sqlite3ExprDup(db, p->pWhere);
625 pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy);
626 pNew->pHaving = sqlite3ExprDup(db, p->pHaving);
627 pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy);
drhff78bd22002-02-27 01:47:11 +0000628 pNew->op = p->op;
drh17435752007-08-16 04:30:38 +0000629 pNew->pPrior = sqlite3SelectDup(db, p->pPrior);
630 pNew->pLimit = sqlite3ExprDup(db, p->pLimit);
631 pNew->pOffset = sqlite3ExprDup(db, p->pOffset);
drh7b58dae2003-07-20 01:16:46 +0000632 pNew->iLimit = -1;
633 pNew->iOffset = -1;
danielk1977a1cb1832005-02-12 08:59:55 +0000634 pNew->isResolved = p->isResolved;
635 pNew->isAgg = p->isAgg;
drhb9bb7c12006-06-11 23:41:55 +0000636 pNew->usesEphm = 0;
drh8e647b82005-09-23 21:11:53 +0000637 pNew->disallowOrderBy = 0;
drh0342b1f2005-09-01 03:07:44 +0000638 pNew->pRightmost = 0;
drhb9bb7c12006-06-11 23:41:55 +0000639 pNew->addrOpenEphm[0] = -1;
640 pNew->addrOpenEphm[1] = -1;
641 pNew->addrOpenEphm[2] = -1;
drhff78bd22002-02-27 01:47:11 +0000642 return pNew;
643}
danielk197793758c82005-01-21 08:13:14 +0000644#else
drh17435752007-08-16 04:30:38 +0000645Select *sqlite3SelectDup(sqlite3 *db, Select *p){
danielk197793758c82005-01-21 08:13:14 +0000646 assert( p==0 );
647 return 0;
648}
649#endif
drhff78bd22002-02-27 01:47:11 +0000650
651
652/*
drha76b5df2002-02-23 02:32:10 +0000653** Add a new element to the end of an expression list. If pList is
654** initially NULL, then create a new expression list.
655*/
drh17435752007-08-16 04:30:38 +0000656ExprList *sqlite3ExprListAppend(
657 Parse *pParse, /* Parsing context */
658 ExprList *pList, /* List to which to append. Might be NULL */
659 Expr *pExpr, /* Expression to be appended */
660 Token *pName /* AS keyword for the expression */
661){
662 sqlite3 *db = pParse->db;
drha76b5df2002-02-23 02:32:10 +0000663 if( pList==0 ){
drh17435752007-08-16 04:30:38 +0000664 pList = sqlite3DbMallocZero(db, sizeof(ExprList) );
drha76b5df2002-02-23 02:32:10 +0000665 if( pList==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000666 goto no_mem;
drha76b5df2002-02-23 02:32:10 +0000667 }
drh4efc4752004-01-16 15:55:37 +0000668 assert( pList->nAlloc==0 );
drha76b5df2002-02-23 02:32:10 +0000669 }
drh4305d102003-07-30 12:34:12 +0000670 if( pList->nAlloc<=pList->nExpr ){
danielk1977d5d56522005-03-16 12:15:20 +0000671 struct ExprList_item *a;
672 int n = pList->nAlloc*2 + 4;
drh17435752007-08-16 04:30:38 +0000673 a = sqlite3_realloc(pList->a, n*sizeof(pList->a[0]));
danielk1977d5d56522005-03-16 12:15:20 +0000674 if( a==0 ){
675 goto no_mem;
drha76b5df2002-02-23 02:32:10 +0000676 }
danielk1977d5d56522005-03-16 12:15:20 +0000677 pList->a = a;
678 pList->nAlloc = n;
drha76b5df2002-02-23 02:32:10 +0000679 }
drh4efc4752004-01-16 15:55:37 +0000680 assert( pList->a!=0 );
681 if( pExpr || pName ){
682 struct ExprList_item *pItem = &pList->a[pList->nExpr++];
683 memset(pItem, 0, sizeof(*pItem));
drh17435752007-08-16 04:30:38 +0000684 pItem->zName = sqlite3NameFromToken(db, pName);
danielk1977e94ddc92005-03-21 03:53:38 +0000685 pItem->pExpr = pExpr;
drha76b5df2002-02-23 02:32:10 +0000686 }
687 return pList;
danielk1977d5d56522005-03-16 12:15:20 +0000688
689no_mem:
690 /* Avoid leaking memory if malloc has failed. */
drh17435752007-08-16 04:30:38 +0000691 db->mallocFailed = 1;
danielk1977d5d56522005-03-16 12:15:20 +0000692 sqlite3ExprDelete(pExpr);
693 sqlite3ExprListDelete(pList);
694 return 0;
drha76b5df2002-02-23 02:32:10 +0000695}
696
697/*
danielk19777a15a4b2007-05-08 17:54:43 +0000698** If the expression list pEList contains more than iLimit elements,
699** leave an error message in pParse.
700*/
701void sqlite3ExprListCheckLength(
702 Parse *pParse,
703 ExprList *pEList,
704 int iLimit,
705 const char *zObject
706){
danielk1977b4fc6792007-05-08 18:04:46 +0000707 if( pEList && pEList->nExpr>iLimit ){
danielk19777a15a4b2007-05-08 17:54:43 +0000708 sqlite3ErrorMsg(pParse, "too many columns in %s", zObject);
709 }
710}
711
danielk1977fc976062007-05-10 10:46:56 +0000712
713#if SQLITE_MAX_EXPR_DEPTH>0
714/* The following three functions, heightOfExpr(), heightOfExprList()
715** and heightOfSelect(), are used to determine the maximum height
716** of any expression tree referenced by the structure passed as the
717** first argument.
718**
719** If this maximum height is greater than the current value pointed
720** to by pnHeight, the second parameter, then set *pnHeight to that
721** value.
722*/
723static void heightOfExpr(Expr *p, int *pnHeight){
724 if( p ){
725 if( p->nHeight>*pnHeight ){
726 *pnHeight = p->nHeight;
727 }
728 }
729}
730static void heightOfExprList(ExprList *p, int *pnHeight){
731 if( p ){
732 int i;
733 for(i=0; i<p->nExpr; i++){
734 heightOfExpr(p->a[i].pExpr, pnHeight);
735 }
736 }
737}
738static void heightOfSelect(Select *p, int *pnHeight){
739 if( p ){
740 heightOfExpr(p->pWhere, pnHeight);
741 heightOfExpr(p->pHaving, pnHeight);
742 heightOfExpr(p->pLimit, pnHeight);
743 heightOfExpr(p->pOffset, pnHeight);
744 heightOfExprList(p->pEList, pnHeight);
745 heightOfExprList(p->pGroupBy, pnHeight);
746 heightOfExprList(p->pOrderBy, pnHeight);
747 heightOfSelect(p->pPrior, pnHeight);
748 }
749}
750
751/*
752** Set the Expr.nHeight variable in the structure passed as an
753** argument. An expression with no children, Expr.pList or
754** Expr.pSelect member has a height of 1. Any other expression
755** has a height equal to the maximum height of any other
756** referenced Expr plus one.
757*/
758void sqlite3ExprSetHeight(Expr *p){
759 int nHeight = 0;
760 heightOfExpr(p->pLeft, &nHeight);
761 heightOfExpr(p->pRight, &nHeight);
762 heightOfExprList(p->pList, &nHeight);
763 heightOfSelect(p->pSelect, &nHeight);
764 p->nHeight = nHeight + 1;
765}
766
767/*
768** Return the maximum height of any expression tree referenced
769** by the select statement passed as an argument.
770*/
771int sqlite3SelectExprHeight(Select *p){
772 int nHeight = 0;
773 heightOfSelect(p, &nHeight);
774 return nHeight;
775}
776#endif
777
danielk19777a15a4b2007-05-08 17:54:43 +0000778/*
drha76b5df2002-02-23 02:32:10 +0000779** Delete an entire expression list.
780*/
danielk19774adee202004-05-08 08:23:19 +0000781void sqlite3ExprListDelete(ExprList *pList){
drha76b5df2002-02-23 02:32:10 +0000782 int i;
drhbe5c89a2004-07-26 00:31:09 +0000783 struct ExprList_item *pItem;
drha76b5df2002-02-23 02:32:10 +0000784 if( pList==0 ) return;
drh1bdd9b52004-04-23 17:04:44 +0000785 assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
786 assert( pList->nExpr<=pList->nAlloc );
drhbe5c89a2004-07-26 00:31:09 +0000787 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
788 sqlite3ExprDelete(pItem->pExpr);
drh17435752007-08-16 04:30:38 +0000789 sqlite3_free(pItem->zName);
drha76b5df2002-02-23 02:32:10 +0000790 }
drh17435752007-08-16 04:30:38 +0000791 sqlite3_free(pList->a);
792 sqlite3_free(pList);
drha76b5df2002-02-23 02:32:10 +0000793}
794
795/*
drh626a8792005-01-17 22:08:19 +0000796** Walk an expression tree. Call xFunc for each node visited.
drh73b211a2005-01-18 04:00:42 +0000797**
drh626a8792005-01-17 22:08:19 +0000798** The return value from xFunc determines whether the tree walk continues.
799** 0 means continue walking the tree. 1 means do not walk children
800** of the current node but continue with siblings. 2 means abandon
801** the tree walk completely.
802**
803** The return value from this routine is 1 to abandon the tree walk
804** and 0 to continue.
drh87abf5c2005-08-25 12:45:04 +0000805**
806** NOTICE: This routine does *not* descend into subqueries.
drh626a8792005-01-17 22:08:19 +0000807*/
danielk1977a58fdfb2005-02-08 07:50:40 +0000808static int walkExprList(ExprList *, int (*)(void *, Expr*), void *);
drh626a8792005-01-17 22:08:19 +0000809static int walkExprTree(Expr *pExpr, int (*xFunc)(void*,Expr*), void *pArg){
drh626a8792005-01-17 22:08:19 +0000810 int rc;
811 if( pExpr==0 ) return 0;
812 rc = (*xFunc)(pArg, pExpr);
813 if( rc==0 ){
814 if( walkExprTree(pExpr->pLeft, xFunc, pArg) ) return 1;
815 if( walkExprTree(pExpr->pRight, xFunc, pArg) ) return 1;
danielk1977a58fdfb2005-02-08 07:50:40 +0000816 if( walkExprList(pExpr->pList, xFunc, pArg) ) return 1;
drh626a8792005-01-17 22:08:19 +0000817 }
818 return rc>1;
819}
820
821/*
danielk1977a58fdfb2005-02-08 07:50:40 +0000822** Call walkExprTree() for every expression in list p.
823*/
824static int walkExprList(ExprList *p, int (*xFunc)(void *, Expr*), void *pArg){
825 int i;
826 struct ExprList_item *pItem;
827 if( !p ) return 0;
828 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
829 if( walkExprTree(pItem->pExpr, xFunc, pArg) ) return 1;
830 }
831 return 0;
832}
833
834/*
835** Call walkExprTree() for every expression in Select p, not including
836** expressions that are part of sub-selects in any FROM clause or the LIMIT
837** or OFFSET expressions..
838*/
839static int walkSelectExpr(Select *p, int (*xFunc)(void *, Expr*), void *pArg){
840 walkExprList(p->pEList, xFunc, pArg);
841 walkExprTree(p->pWhere, xFunc, pArg);
842 walkExprList(p->pGroupBy, xFunc, pArg);
843 walkExprTree(p->pHaving, xFunc, pArg);
844 walkExprList(p->pOrderBy, xFunc, pArg);
danielk197715d79822007-05-15 07:00:34 +0000845 if( p->pPrior ){
846 walkSelectExpr(p->pPrior, xFunc, pArg);
847 }
danielk1977a58fdfb2005-02-08 07:50:40 +0000848 return 0;
849}
850
851
852/*
drh626a8792005-01-17 22:08:19 +0000853** This routine is designed as an xFunc for walkExprTree().
854**
855** pArg is really a pointer to an integer. If we can tell by looking
drh73b211a2005-01-18 04:00:42 +0000856** at pExpr that the expression that contains pExpr is not a constant
857** expression, then set *pArg to 0 and return 2 to abandon the tree walk.
858** If pExpr does does not disqualify the expression from being a constant
859** then do nothing.
860**
861** After walking the whole tree, if no nodes are found that disqualify
862** the expression as constant, then we assume the whole expression
863** is constant. See sqlite3ExprIsConstant() for additional information.
drh626a8792005-01-17 22:08:19 +0000864*/
865static int exprNodeIsConstant(void *pArg, Expr *pExpr){
drh0a168372007-06-08 00:20:47 +0000866 int *pN = (int*)pArg;
867
868 /* If *pArg is 3 then any term of the expression that comes from
869 ** the ON or USING clauses of a join disqualifies the expression
870 ** from being considered constant. */
871 if( (*pN)==3 && ExprHasAnyProperty(pExpr, EP_FromJoin) ){
872 *pN = 0;
873 return 2;
874 }
875
drh626a8792005-01-17 22:08:19 +0000876 switch( pExpr->op ){
drheb55bd22005-06-30 17:04:21 +0000877 /* Consider functions to be constant if all their arguments are constant
878 ** and *pArg==2 */
879 case TK_FUNCTION:
drh0a168372007-06-08 00:20:47 +0000880 if( (*pN)==2 ) return 0;
drheb55bd22005-06-30 17:04:21 +0000881 /* Fall through */
drh626a8792005-01-17 22:08:19 +0000882 case TK_ID:
883 case TK_COLUMN:
884 case TK_DOT:
885 case TK_AGG_FUNCTION:
drh13449892005-09-07 21:22:45 +0000886 case TK_AGG_COLUMN:
drhfe2093d2005-01-20 22:48:47 +0000887#ifndef SQLITE_OMIT_SUBQUERY
888 case TK_SELECT:
889 case TK_EXISTS:
890#endif
drh0a168372007-06-08 00:20:47 +0000891 *pN = 0;
drh626a8792005-01-17 22:08:19 +0000892 return 2;
drh87abf5c2005-08-25 12:45:04 +0000893 case TK_IN:
894 if( pExpr->pSelect ){
drh0a168372007-06-08 00:20:47 +0000895 *pN = 0;
drh87abf5c2005-08-25 12:45:04 +0000896 return 2;
897 }
drh626a8792005-01-17 22:08:19 +0000898 default:
899 return 0;
900 }
901}
902
903/*
drhfef52082000-06-06 01:50:43 +0000904** Walk an expression tree. Return 1 if the expression is constant
drheb55bd22005-06-30 17:04:21 +0000905** and 0 if it involves variables or function calls.
drh23989372002-05-21 13:43:04 +0000906**
907** For the purposes of this function, a double-quoted string (ex: "abc")
908** is considered a variable but a single-quoted string (ex: 'abc') is
909** a constant.
drhfef52082000-06-06 01:50:43 +0000910*/
danielk19774adee202004-05-08 08:23:19 +0000911int sqlite3ExprIsConstant(Expr *p){
drh626a8792005-01-17 22:08:19 +0000912 int isConst = 1;
913 walkExprTree(p, exprNodeIsConstant, &isConst);
914 return isConst;
drhfef52082000-06-06 01:50:43 +0000915}
916
917/*
drheb55bd22005-06-30 17:04:21 +0000918** Walk an expression tree. Return 1 if the expression is constant
drh0a168372007-06-08 00:20:47 +0000919** that does no originate from the ON or USING clauses of a join.
920** Return 0 if it involves variables or function calls or terms from
921** an ON or USING clause.
922*/
923int sqlite3ExprIsConstantNotJoin(Expr *p){
924 int isConst = 3;
925 walkExprTree(p, exprNodeIsConstant, &isConst);
926 return isConst!=0;
927}
928
929/*
930** Walk an expression tree. Return 1 if the expression is constant
drheb55bd22005-06-30 17:04:21 +0000931** or a function call with constant arguments. Return and 0 if there
932** are any variables.
933**
934** For the purposes of this function, a double-quoted string (ex: "abc")
935** is considered a variable but a single-quoted string (ex: 'abc') is
936** a constant.
937*/
938int sqlite3ExprIsConstantOrFunction(Expr *p){
939 int isConst = 2;
940 walkExprTree(p, exprNodeIsConstant, &isConst);
941 return isConst!=0;
942}
943
944/*
drh73b211a2005-01-18 04:00:42 +0000945** If the expression p codes a constant integer that is small enough
drh202b2df2004-01-06 01:13:46 +0000946** to fit in a 32-bit integer, return 1 and put the value of the integer
947** in *pValue. If the expression is not an integer or if it is too big
948** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
drhe4de1fe2002-06-02 16:09:01 +0000949*/
danielk19774adee202004-05-08 08:23:19 +0000950int sqlite3ExprIsInteger(Expr *p, int *pValue){
drhe4de1fe2002-06-02 16:09:01 +0000951 switch( p->op ){
952 case TK_INTEGER: {
drh2646da72005-12-09 20:02:05 +0000953 if( sqlite3GetInt32((char*)p->token.z, pValue) ){
drh202b2df2004-01-06 01:13:46 +0000954 return 1;
955 }
956 break;
drhe4de1fe2002-06-02 16:09:01 +0000957 }
drh4b59ab52002-08-24 18:24:51 +0000958 case TK_UPLUS: {
danielk19774adee202004-05-08 08:23:19 +0000959 return sqlite3ExprIsInteger(p->pLeft, pValue);
drh4b59ab52002-08-24 18:24:51 +0000960 }
drhe4de1fe2002-06-02 16:09:01 +0000961 case TK_UMINUS: {
962 int v;
danielk19774adee202004-05-08 08:23:19 +0000963 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
drhe4de1fe2002-06-02 16:09:01 +0000964 *pValue = -v;
965 return 1;
966 }
967 break;
968 }
969 default: break;
970 }
971 return 0;
972}
973
974/*
drhc4a3c772001-04-04 11:48:57 +0000975** Return TRUE if the given string is a row-id column name.
976*/
danielk19774adee202004-05-08 08:23:19 +0000977int sqlite3IsRowid(const char *z){
978 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
979 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
980 if( sqlite3StrICmp(z, "OID")==0 ) return 1;
drhc4a3c772001-04-04 11:48:57 +0000981 return 0;
982}
983
984/*
drh8141f612004-01-25 22:44:58 +0000985** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
986** that name in the set of source tables in pSrcList and make the pExpr
987** expression node refer back to that source column. The following changes
988** are made to pExpr:
989**
990** pExpr->iDb Set the index in db->aDb[] of the database holding
991** the table.
992** pExpr->iTable Set to the cursor number for the table obtained
993** from pSrcList.
994** pExpr->iColumn Set to the column number within the table.
drh8141f612004-01-25 22:44:58 +0000995** pExpr->op Set to TK_COLUMN.
996** pExpr->pLeft Any expression this points to is deleted
997** pExpr->pRight Any expression this points to is deleted.
998**
999** The pDbToken is the name of the database (the "X"). This value may be
1000** NULL meaning that name is of the form Y.Z or Z. Any available database
1001** can be used. The pTableToken is the name of the table (the "Y"). This
1002** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it
1003** means that the form of the name is Z and that columns from any table
1004** can be used.
1005**
1006** If the name cannot be resolved unambiguously, leave an error message
1007** in pParse and return non-zero. Return zero on success.
1008*/
1009static int lookupName(
drhffe07b22005-11-03 00:41:17 +00001010 Parse *pParse, /* The parsing context */
drh8141f612004-01-25 22:44:58 +00001011 Token *pDbToken, /* Name of the database containing table, or NULL */
1012 Token *pTableToken, /* Name of table containing column, or NULL */
1013 Token *pColumnToken, /* Name of the column. */
drh626a8792005-01-17 22:08:19 +00001014 NameContext *pNC, /* The name context used to resolve the name */
drh8141f612004-01-25 22:44:58 +00001015 Expr *pExpr /* Make this EXPR node point to the selected column */
1016){
1017 char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */
1018 char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */
1019 char *zCol = 0; /* Name of the column. The "Z" */
1020 int i, j; /* Loop counters */
1021 int cnt = 0; /* Number of matching column names */
1022 int cntTab = 0; /* Number of matching table names */
drh626a8792005-01-17 22:08:19 +00001023 sqlite3 *db = pParse->db; /* The database */
drh51669862004-12-18 18:40:26 +00001024 struct SrcList_item *pItem; /* Use for looping over pSrcList items */
1025 struct SrcList_item *pMatch = 0; /* The matching pSrcList item */
drh73b211a2005-01-18 04:00:42 +00001026 NameContext *pTopNC = pNC; /* First namecontext in the list */
drh8141f612004-01-25 22:44:58 +00001027
1028 assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
drh17435752007-08-16 04:30:38 +00001029 zDb = sqlite3NameFromToken(db, pDbToken);
1030 zTab = sqlite3NameFromToken(db, pTableToken);
1031 zCol = sqlite3NameFromToken(db, pColumnToken);
1032 if( db->mallocFailed ){
danielk1977d5d56522005-03-16 12:15:20 +00001033 goto lookupname_end;
drh8141f612004-01-25 22:44:58 +00001034 }
drh8141f612004-01-25 22:44:58 +00001035
1036 pExpr->iTable = -1;
drh626a8792005-01-17 22:08:19 +00001037 while( pNC && cnt==0 ){
drhffe07b22005-11-03 00:41:17 +00001038 ExprList *pEList;
drh626a8792005-01-17 22:08:19 +00001039 SrcList *pSrcList = pNC->pSrcList;
drh8141f612004-01-25 22:44:58 +00001040
danielk1977b3bce662005-01-29 08:32:43 +00001041 if( pSrcList ){
1042 for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
drh43617e92006-03-06 20:55:46 +00001043 Table *pTab;
1044 int iDb;
danielk1977b3bce662005-01-29 08:32:43 +00001045 Column *pCol;
1046
drh43617e92006-03-06 20:55:46 +00001047 pTab = pItem->pTab;
1048 assert( pTab!=0 );
1049 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
danielk1977b3bce662005-01-29 08:32:43 +00001050 assert( pTab->nCol>0 );
1051 if( zTab ){
1052 if( pItem->zAlias ){
1053 char *zTabName = pItem->zAlias;
1054 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
1055 }else{
1056 char *zTabName = pTab->zName;
1057 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
danielk1977da184232006-01-05 11:34:32 +00001058 if( zDb!=0 && sqlite3StrICmp(db->aDb[iDb].zName, zDb)!=0 ){
danielk1977b3bce662005-01-29 08:32:43 +00001059 continue;
1060 }
drh626a8792005-01-17 22:08:19 +00001061 }
drh8141f612004-01-25 22:44:58 +00001062 }
danielk1977b3bce662005-01-29 08:32:43 +00001063 if( 0==(cntTab++) ){
drh626a8792005-01-17 22:08:19 +00001064 pExpr->iTable = pItem->iCursor;
danielk1977da184232006-01-05 11:34:32 +00001065 pExpr->pSchema = pTab->pSchema;
danielk1977b3bce662005-01-29 08:32:43 +00001066 pMatch = pItem;
1067 }
1068 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
1069 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
danielk1977b3bf5562006-01-10 17:58:23 +00001070 const char *zColl = pTab->aCol[j].zColl;
drh873fac02005-06-06 17:11:46 +00001071 IdList *pUsing;
danielk1977b3bce662005-01-29 08:32:43 +00001072 cnt++;
1073 pExpr->iTable = pItem->iCursor;
1074 pMatch = pItem;
danielk1977da184232006-01-05 11:34:32 +00001075 pExpr->pSchema = pTab->pSchema;
danielk1977b3bce662005-01-29 08:32:43 +00001076 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
1077 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
1078 pExpr->affinity = pTab->aCol[j].affinity;
drh8b4c40d2007-02-01 23:02:45 +00001079 if( (pExpr->flags & EP_ExpCollate)==0 ){
1080 pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0);
1081 }
drh61dfc312006-12-16 16:25:15 +00001082 if( i<pSrcList->nSrc-1 ){
1083 if( pItem[1].jointype & JT_NATURAL ){
1084 /* If this match occurred in the left table of a natural join,
1085 ** then skip the right table to avoid a duplicate match */
1086 pItem++;
1087 i++;
1088 }else if( (pUsing = pItem[1].pUsing)!=0 ){
1089 /* If this match occurs on a column that is in the USING clause
1090 ** of a join, skip the search of the right table of the join
1091 ** to avoid a duplicate match there. */
1092 int k;
1093 for(k=0; k<pUsing->nId; k++){
1094 if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ){
1095 pItem++;
1096 i++;
1097 break;
1098 }
drh873fac02005-06-06 17:11:46 +00001099 }
1100 }
1101 }
danielk1977b3bce662005-01-29 08:32:43 +00001102 break;
1103 }
drh8141f612004-01-25 22:44:58 +00001104 }
1105 }
1106 }
drh626a8792005-01-17 22:08:19 +00001107
1108#ifndef SQLITE_OMIT_TRIGGER
1109 /* If we have not already resolved the name, then maybe
1110 ** it is a new.* or old.* trigger argument reference
1111 */
1112 if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
1113 TriggerStack *pTriggerStack = pParse->trigStack;
1114 Table *pTab = 0;
1115 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){
1116 pExpr->iTable = pTriggerStack->newIdx;
1117 assert( pTriggerStack->pTab );
1118 pTab = pTriggerStack->pTab;
1119 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab)==0 ){
1120 pExpr->iTable = pTriggerStack->oldIdx;
1121 assert( pTriggerStack->pTab );
1122 pTab = pTriggerStack->pTab;
1123 }
1124
1125 if( pTab ){
danielk1977f0113002006-01-24 12:09:17 +00001126 int iCol;
drh626a8792005-01-17 22:08:19 +00001127 Column *pCol = pTab->aCol;
1128
danielk1977da184232006-01-05 11:34:32 +00001129 pExpr->pSchema = pTab->pSchema;
drh626a8792005-01-17 22:08:19 +00001130 cntTab++;
danielk1977f0113002006-01-24 12:09:17 +00001131 for(iCol=0; iCol < pTab->nCol; iCol++, pCol++) {
drh626a8792005-01-17 22:08:19 +00001132 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
danielk1977f0113002006-01-24 12:09:17 +00001133 const char *zColl = pTab->aCol[iCol].zColl;
drh626a8792005-01-17 22:08:19 +00001134 cnt++;
danielk1977f0113002006-01-24 12:09:17 +00001135 pExpr->iColumn = iCol==pTab->iPKey ? -1 : iCol;
1136 pExpr->affinity = pTab->aCol[iCol].affinity;
drh8b4c40d2007-02-01 23:02:45 +00001137 if( (pExpr->flags & EP_ExpCollate)==0 ){
1138 pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0);
1139 }
danielk1977aee18ef2005-03-09 12:26:50 +00001140 pExpr->pTab = pTab;
drh626a8792005-01-17 22:08:19 +00001141 break;
1142 }
1143 }
1144 }
1145 }
drhb7f91642004-10-31 02:22:47 +00001146#endif /* !defined(SQLITE_OMIT_TRIGGER) */
drh8141f612004-01-25 22:44:58 +00001147
drh626a8792005-01-17 22:08:19 +00001148 /*
1149 ** Perhaps the name is a reference to the ROWID
1150 */
1151 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
1152 cnt = 1;
1153 pExpr->iColumn = -1;
drh8a512562005-11-14 22:29:05 +00001154 pExpr->affinity = SQLITE_AFF_INTEGER;
drh626a8792005-01-17 22:08:19 +00001155 }
drh8141f612004-01-25 22:44:58 +00001156
drh626a8792005-01-17 22:08:19 +00001157 /*
1158 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
1159 ** might refer to an result-set alias. This happens, for example, when
1160 ** we are resolving names in the WHERE clause of the following command:
1161 **
1162 ** SELECT a+b AS x FROM table WHERE x<10;
1163 **
1164 ** In cases like this, replace pExpr with a copy of the expression that
1165 ** forms the result set entry ("a+b" in the example) and return immediately.
1166 ** Note that the expression in the result set should have already been
1167 ** resolved by the time the WHERE clause is resolved.
1168 */
drhffe07b22005-11-03 00:41:17 +00001169 if( cnt==0 && (pEList = pNC->pEList)!=0 && zTab==0 ){
drh626a8792005-01-17 22:08:19 +00001170 for(j=0; j<pEList->nExpr; j++){
1171 char *zAs = pEList->a[j].zName;
1172 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
drh36379e92007-07-23 22:51:15 +00001173 Expr *pDup, *pOrig;
drh626a8792005-01-17 22:08:19 +00001174 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
drh4f07e5f2007-05-14 11:34:46 +00001175 assert( pExpr->pList==0 );
1176 assert( pExpr->pSelect==0 );
drh36379e92007-07-23 22:51:15 +00001177 pOrig = pEList->a[j].pExpr;
1178 if( !pNC->allowAgg && ExprHasProperty(pOrig, EP_Agg) ){
1179 sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs);
drh17435752007-08-16 04:30:38 +00001180 sqlite3_free(zCol);
drh36379e92007-07-23 22:51:15 +00001181 return 2;
1182 }
danielk19771e536952007-08-16 10:09:01 +00001183 pDup = sqlite3ExprDup(db, pOrig);
drh4f07e5f2007-05-14 11:34:46 +00001184 if( pExpr->flags & EP_ExpCollate ){
1185 pDup->pColl = pExpr->pColl;
1186 pDup->flags |= EP_ExpCollate;
1187 }
drh17435752007-08-16 04:30:38 +00001188 if( pExpr->span.dyn ) sqlite3_free((char*)pExpr->span.z);
1189 if( pExpr->token.dyn ) sqlite3_free((char*)pExpr->token.z);
drh4f07e5f2007-05-14 11:34:46 +00001190 memcpy(pExpr, pDup, sizeof(*pExpr));
drh17435752007-08-16 04:30:38 +00001191 sqlite3_free(pDup);
drh15ccce12005-05-23 15:06:39 +00001192 cnt = 1;
danielk1977c9cf6e32007-06-25 16:29:33 +00001193 pMatch = 0;
drh626a8792005-01-17 22:08:19 +00001194 assert( zTab==0 && zDb==0 );
drh15ccce12005-05-23 15:06:39 +00001195 goto lookupname_end_2;
drh626a8792005-01-17 22:08:19 +00001196 }
1197 }
1198 }
1199
1200 /* Advance to the next name context. The loop will exit when either
1201 ** we have a match (cnt>0) or when we run out of name contexts.
1202 */
1203 if( cnt==0 ){
1204 pNC = pNC->pNext;
1205 }
drh8141f612004-01-25 22:44:58 +00001206 }
1207
1208 /*
1209 ** If X and Y are NULL (in other words if only the column name Z is
1210 ** supplied) and the value of Z is enclosed in double-quotes, then
1211 ** Z is a string literal if it doesn't match any column names. In that
1212 ** case, we need to return right away and not make any changes to
1213 ** pExpr.
drh15ccce12005-05-23 15:06:39 +00001214 **
1215 ** Because no reference was made to outer contexts, the pNC->nRef
1216 ** fields are not changed in any context.
drh8141f612004-01-25 22:44:58 +00001217 */
1218 if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
drh17435752007-08-16 04:30:38 +00001219 sqlite3_free(zCol);
drh8141f612004-01-25 22:44:58 +00001220 return 0;
1221 }
1222
1223 /*
1224 ** cnt==0 means there was not match. cnt>1 means there were two or
1225 ** more matches. Either way, we have an error.
1226 */
1227 if( cnt!=1 ){
1228 char *z = 0;
1229 char *zErr;
1230 zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s";
1231 if( zDb ){
drhf93339d2006-01-03 15:16:26 +00001232 sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, (char*)0);
drh8141f612004-01-25 22:44:58 +00001233 }else if( zTab ){
drhf93339d2006-01-03 15:16:26 +00001234 sqlite3SetString(&z, zTab, ".", zCol, (char*)0);
drh8141f612004-01-25 22:44:58 +00001235 }else{
drh17435752007-08-16 04:30:38 +00001236 z = sqlite3StrDup(zCol);
drh8141f612004-01-25 22:44:58 +00001237 }
danielk19774adee202004-05-08 08:23:19 +00001238 sqlite3ErrorMsg(pParse, zErr, z);
drh17435752007-08-16 04:30:38 +00001239 sqlite3_free(z);
drh73b211a2005-01-18 04:00:42 +00001240 pTopNC->nErr++;
drh8141f612004-01-25 22:44:58 +00001241 }
1242
drh51669862004-12-18 18:40:26 +00001243 /* If a column from a table in pSrcList is referenced, then record
1244 ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes
1245 ** bit 0 to be set. Column 1 sets bit 1. And so forth. If the
1246 ** column number is greater than the number of bits in the bitmask
1247 ** then set the high-order bit of the bitmask.
1248 */
1249 if( pExpr->iColumn>=0 && pMatch!=0 ){
1250 int n = pExpr->iColumn;
1251 if( n>=sizeof(Bitmask)*8 ){
1252 n = sizeof(Bitmask)*8-1;
1253 }
1254 assert( pMatch->iCursor==pExpr->iTable );
drhca83ac52007-02-01 01:40:44 +00001255 pMatch->colUsed |= ((Bitmask)1)<<n;
drh51669862004-12-18 18:40:26 +00001256 }
1257
danielk1977d5d56522005-03-16 12:15:20 +00001258lookupname_end:
drh8141f612004-01-25 22:44:58 +00001259 /* Clean up and return
1260 */
drh17435752007-08-16 04:30:38 +00001261 sqlite3_free(zDb);
1262 sqlite3_free(zTab);
danielk19774adee202004-05-08 08:23:19 +00001263 sqlite3ExprDelete(pExpr->pLeft);
drh8141f612004-01-25 22:44:58 +00001264 pExpr->pLeft = 0;
danielk19774adee202004-05-08 08:23:19 +00001265 sqlite3ExprDelete(pExpr->pRight);
drh8141f612004-01-25 22:44:58 +00001266 pExpr->pRight = 0;
1267 pExpr->op = TK_COLUMN;
drh15ccce12005-05-23 15:06:39 +00001268lookupname_end_2:
drh17435752007-08-16 04:30:38 +00001269 sqlite3_free(zCol);
drh626a8792005-01-17 22:08:19 +00001270 if( cnt==1 ){
danielk1977b3bce662005-01-29 08:32:43 +00001271 assert( pNC!=0 );
drh626a8792005-01-17 22:08:19 +00001272 sqlite3AuthRead(pParse, pExpr, pNC->pSrcList);
danielk1977aee18ef2005-03-09 12:26:50 +00001273 if( pMatch && !pMatch->pSelect ){
1274 pExpr->pTab = pMatch->pTab;
1275 }
drh15ccce12005-05-23 15:06:39 +00001276 /* Increment the nRef value on all name contexts from TopNC up to
1277 ** the point where the name matched. */
1278 for(;;){
1279 assert( pTopNC!=0 );
1280 pTopNC->nRef++;
1281 if( pTopNC==pNC ) break;
1282 pTopNC = pTopNC->pNext;
1283 }
1284 return 0;
1285 } else {
1286 return 1;
drh626a8792005-01-17 22:08:19 +00001287 }
drh8141f612004-01-25 22:44:58 +00001288}
1289
1290/*
drh626a8792005-01-17 22:08:19 +00001291** This routine is designed as an xFunc for walkExprTree().
1292**
drh73b211a2005-01-18 04:00:42 +00001293** Resolve symbolic names into TK_COLUMN operators for the current
drh626a8792005-01-17 22:08:19 +00001294** node in the expression tree. Return 0 to continue the search down
drh73b211a2005-01-18 04:00:42 +00001295** the tree or 2 to abort the tree walk.
1296**
1297** This routine also does error checking and name resolution for
1298** function names. The operator for aggregate functions is changed
1299** to TK_AGG_FUNCTION.
drh626a8792005-01-17 22:08:19 +00001300*/
1301static int nameResolverStep(void *pArg, Expr *pExpr){
1302 NameContext *pNC = (NameContext*)pArg;
drh626a8792005-01-17 22:08:19 +00001303 Parse *pParse;
drh626a8792005-01-17 22:08:19 +00001304
danielk1977b3bce662005-01-29 08:32:43 +00001305 if( pExpr==0 ) return 1;
drh626a8792005-01-17 22:08:19 +00001306 assert( pNC!=0 );
drh626a8792005-01-17 22:08:19 +00001307 pParse = pNC->pParse;
danielk1977b3bce662005-01-29 08:32:43 +00001308
drh626a8792005-01-17 22:08:19 +00001309 if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return 1;
1310 ExprSetProperty(pExpr, EP_Resolved);
1311#ifndef NDEBUG
danielk1977f0113002006-01-24 12:09:17 +00001312 if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
1313 SrcList *pSrcList = pNC->pSrcList;
danielk1977940fac92005-01-23 22:41:37 +00001314 int i;
danielk1977f0113002006-01-24 12:09:17 +00001315 for(i=0; i<pNC->pSrcList->nSrc; i++){
drh626a8792005-01-17 22:08:19 +00001316 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
1317 }
1318 }
1319#endif
1320 switch( pExpr->op ){
1321 /* Double-quoted strings (ex: "abc") are used as identifiers if
1322 ** possible. Otherwise they remain as strings. Single-quoted
1323 ** strings (ex: 'abc') are always string literals.
1324 */
1325 case TK_STRING: {
1326 if( pExpr->token.z[0]=='\'' ) break;
1327 /* Fall thru into the TK_ID case if this is a double-quoted string */
1328 }
1329 /* A lone identifier is the name of a column.
1330 */
1331 case TK_ID: {
drh626a8792005-01-17 22:08:19 +00001332 lookupName(pParse, 0, 0, &pExpr->token, pNC, pExpr);
1333 return 1;
1334 }
1335
1336 /* A table name and column name: ID.ID
1337 ** Or a database, table and column: ID.ID.ID
1338 */
1339 case TK_DOT: {
1340 Token *pColumn;
1341 Token *pTable;
1342 Token *pDb;
1343 Expr *pRight;
1344
danielk1977b3bce662005-01-29 08:32:43 +00001345 /* if( pSrcList==0 ) break; */
drh626a8792005-01-17 22:08:19 +00001346 pRight = pExpr->pRight;
1347 if( pRight->op==TK_ID ){
1348 pDb = 0;
1349 pTable = &pExpr->pLeft->token;
1350 pColumn = &pRight->token;
1351 }else{
1352 assert( pRight->op==TK_DOT );
1353 pDb = &pExpr->pLeft->token;
1354 pTable = &pRight->pLeft->token;
1355 pColumn = &pRight->pRight->token;
1356 }
1357 lookupName(pParse, pDb, pTable, pColumn, pNC, pExpr);
1358 return 1;
1359 }
1360
1361 /* Resolve function names
1362 */
drhb71090f2005-05-23 17:26:51 +00001363 case TK_CONST_FUNC:
drh626a8792005-01-17 22:08:19 +00001364 case TK_FUNCTION: {
1365 ExprList *pList = pExpr->pList; /* The argument list */
1366 int n = pList ? pList->nExpr : 0; /* Number of arguments */
1367 int no_such_func = 0; /* True if no such function exists */
1368 int wrong_num_args = 0; /* True if wrong number of arguments */
1369 int is_agg = 0; /* True if is an aggregate function */
1370 int i;
drh5169bbc2006-08-24 14:59:45 +00001371 int auth; /* Authorization to use the function */
drh626a8792005-01-17 22:08:19 +00001372 int nId; /* Number of characters in function name */
1373 const char *zId; /* The function name. */
drh73b211a2005-01-18 04:00:42 +00001374 FuncDef *pDef; /* Information about the function */
danielk197714db2662006-01-09 16:12:04 +00001375 int enc = ENC(pParse->db); /* The database encoding */
drh626a8792005-01-17 22:08:19 +00001376
drh2646da72005-12-09 20:02:05 +00001377 zId = (char*)pExpr->token.z;
drhb71090f2005-05-23 17:26:51 +00001378 nId = pExpr->token.n;
drh626a8792005-01-17 22:08:19 +00001379 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
1380 if( pDef==0 ){
1381 pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
1382 if( pDef==0 ){
1383 no_such_func = 1;
1384 }else{
1385 wrong_num_args = 1;
1386 }
1387 }else{
1388 is_agg = pDef->xFunc==0;
1389 }
drh2fca7fe2006-11-23 11:59:13 +00001390#ifndef SQLITE_OMIT_AUTHORIZATION
drh5169bbc2006-08-24 14:59:45 +00001391 if( pDef ){
1392 auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0);
1393 if( auth!=SQLITE_OK ){
1394 if( auth==SQLITE_DENY ){
1395 sqlite3ErrorMsg(pParse, "not authorized to use function: %s",
1396 pDef->zName);
1397 pNC->nErr++;
1398 }
1399 pExpr->op = TK_NULL;
1400 return 1;
1401 }
1402 }
drhb8b14212006-08-24 15:18:25 +00001403#endif
drh626a8792005-01-17 22:08:19 +00001404 if( is_agg && !pNC->allowAgg ){
1405 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
1406 pNC->nErr++;
1407 is_agg = 0;
1408 }else if( no_such_func ){
1409 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
1410 pNC->nErr++;
1411 }else if( wrong_num_args ){
1412 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
1413 nId, zId);
1414 pNC->nErr++;
1415 }
1416 if( is_agg ){
1417 pExpr->op = TK_AGG_FUNCTION;
1418 pNC->hasAgg = 1;
1419 }
drh73b211a2005-01-18 04:00:42 +00001420 if( is_agg ) pNC->allowAgg = 0;
drh626a8792005-01-17 22:08:19 +00001421 for(i=0; pNC->nErr==0 && i<n; i++){
drh73b211a2005-01-18 04:00:42 +00001422 walkExprTree(pList->a[i].pExpr, nameResolverStep, pNC);
drh626a8792005-01-17 22:08:19 +00001423 }
drh73b211a2005-01-18 04:00:42 +00001424 if( is_agg ) pNC->allowAgg = 1;
drh626a8792005-01-17 22:08:19 +00001425 /* FIX ME: Compute pExpr->affinity based on the expected return
1426 ** type of the function
1427 */
1428 return is_agg;
1429 }
danielk1977b3bce662005-01-29 08:32:43 +00001430#ifndef SQLITE_OMIT_SUBQUERY
1431 case TK_SELECT:
1432 case TK_EXISTS:
1433#endif
1434 case TK_IN: {
1435 if( pExpr->pSelect ){
drh8a9f38f2005-11-05 15:07:55 +00001436 int nRef = pNC->nRef;
drh06f65412005-11-03 02:03:13 +00001437#ifndef SQLITE_OMIT_CHECK
1438 if( pNC->isCheck ){
1439 sqlite3ErrorMsg(pParse,"subqueries prohibited in CHECK constraints");
1440 }
1441#endif
danielk1977b3bce662005-01-29 08:32:43 +00001442 sqlite3SelectResolve(pParse, pExpr->pSelect, pNC);
1443 assert( pNC->nRef>=nRef );
1444 if( nRef!=pNC->nRef ){
1445 ExprSetProperty(pExpr, EP_VarSelect);
1446 }
1447 }
drh4284fb02005-11-03 12:33:28 +00001448 break;
danielk1977b3bce662005-01-29 08:32:43 +00001449 }
drh4284fb02005-11-03 12:33:28 +00001450#ifndef SQLITE_OMIT_CHECK
1451 case TK_VARIABLE: {
1452 if( pNC->isCheck ){
1453 sqlite3ErrorMsg(pParse,"parameters prohibited in CHECK constraints");
1454 }
1455 break;
1456 }
1457#endif
drh626a8792005-01-17 22:08:19 +00001458 }
1459 return 0;
1460}
1461
1462/*
drhcce7d172000-05-31 15:34:51 +00001463** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +00001464** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +00001465** index to the table in the table list and a column offset. The
1466** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
1467** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +00001468** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +00001469** VDBE cursor number for a cursor that is pointing into the referenced
1470** table. The Expr.iColumn value is changed to the index of the column
1471** of the referenced table. The Expr.iColumn value for the special
1472** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
1473** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +00001474**
drh626a8792005-01-17 22:08:19 +00001475** Also resolve function names and check the functions for proper
1476** usage. Make sure all function names are recognized and all functions
1477** have the correct number of arguments. Leave an error message
1478** in pParse->zErrMsg if anything is amiss. Return the number of errors.
1479**
drh73b211a2005-01-18 04:00:42 +00001480** If the expression contains aggregate functions then set the EP_Agg
1481** property on the expression.
drh626a8792005-01-17 22:08:19 +00001482*/
danielk1977fc976062007-05-10 10:46:56 +00001483int sqlite3ExprResolveNames(
danielk1977b3bce662005-01-29 08:32:43 +00001484 NameContext *pNC, /* Namespace to resolve expressions in. */
1485 Expr *pExpr /* The expression to be analyzed. */
drh626a8792005-01-17 22:08:19 +00001486){
drh13449892005-09-07 21:22:45 +00001487 int savedHasAgg;
drh73b211a2005-01-18 04:00:42 +00001488 if( pExpr==0 ) return 0;
danielk1977fc976062007-05-10 10:46:56 +00001489#if SQLITE_MAX_EXPR_DEPTH>0
1490 if( (pExpr->nHeight+pNC->pParse->nHeight)>SQLITE_MAX_EXPR_DEPTH ){
1491 sqlite3ErrorMsg(pNC->pParse,
1492 "Expression tree is too large (maximum depth %d)",
1493 SQLITE_MAX_EXPR_DEPTH
1494 );
1495 return 1;
1496 }
1497 pNC->pParse->nHeight += pExpr->nHeight;
1498#endif
drh13449892005-09-07 21:22:45 +00001499 savedHasAgg = pNC->hasAgg;
1500 pNC->hasAgg = 0;
danielk1977b3bce662005-01-29 08:32:43 +00001501 walkExprTree(pExpr, nameResolverStep, pNC);
danielk1977fc976062007-05-10 10:46:56 +00001502#if SQLITE_MAX_EXPR_DEPTH>0
1503 pNC->pParse->nHeight -= pExpr->nHeight;
1504#endif
danielk1977b3bce662005-01-29 08:32:43 +00001505 if( pNC->nErr>0 ){
drh73b211a2005-01-18 04:00:42 +00001506 ExprSetProperty(pExpr, EP_Error);
drh73b211a2005-01-18 04:00:42 +00001507 }
drh13449892005-09-07 21:22:45 +00001508 if( pNC->hasAgg ){
1509 ExprSetProperty(pExpr, EP_Agg);
1510 }else if( savedHasAgg ){
1511 pNC->hasAgg = 1;
1512 }
drh73b211a2005-01-18 04:00:42 +00001513 return ExprHasProperty(pExpr, EP_Error);
drh626a8792005-01-17 22:08:19 +00001514}
1515
drh1398ad32005-01-19 23:24:50 +00001516/*
1517** A pointer instance of this structure is used to pass information
1518** through walkExprTree into codeSubqueryStep().
1519*/
1520typedef struct QueryCoder QueryCoder;
1521struct QueryCoder {
1522 Parse *pParse; /* The parsing context */
1523 NameContext *pNC; /* Namespace of first enclosing query */
1524};
1525
drh626a8792005-01-17 22:08:19 +00001526
1527/*
drh9cbe6352005-11-29 03:13:21 +00001528** Generate code for scalar subqueries used as an expression
1529** and IN operators. Examples:
drh626a8792005-01-17 22:08:19 +00001530**
drh9cbe6352005-11-29 03:13:21 +00001531** (SELECT a FROM b) -- subquery
1532** EXISTS (SELECT a FROM b) -- EXISTS subquery
1533** x IN (4,5,11) -- IN operator with list on right-hand side
1534** x IN (SELECT a FROM b) -- IN operator with subquery on the right
drhfef52082000-06-06 01:50:43 +00001535**
drh9cbe6352005-11-29 03:13:21 +00001536** The pExpr parameter describes the expression that contains the IN
1537** operator or subquery.
drhcce7d172000-05-31 15:34:51 +00001538*/
drh51522cd2005-01-20 13:36:19 +00001539#ifndef SQLITE_OMIT_SUBQUERY
danielk1977b3bce662005-01-29 08:32:43 +00001540void sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){
drh57dbd7b2005-07-08 18:25:26 +00001541 int testAddr = 0; /* One-time test address */
danielk1977b3bce662005-01-29 08:32:43 +00001542 Vdbe *v = sqlite3GetVdbe(pParse);
1543 if( v==0 ) return;
1544
danielk1977fc976062007-05-10 10:46:56 +00001545
drh57dbd7b2005-07-08 18:25:26 +00001546 /* This code must be run in its entirety every time it is encountered
1547 ** if any of the following is true:
1548 **
1549 ** * The right-hand side is a correlated subquery
1550 ** * The right-hand side is an expression list containing variables
1551 ** * We are inside a trigger
1552 **
1553 ** If all of the above are false, then we can run this code just once
1554 ** save the results, and reuse the same result on subsequent invocations.
danielk1977b3bce662005-01-29 08:32:43 +00001555 */
1556 if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->trigStack ){
1557 int mem = pParse->nMem++;
1558 sqlite3VdbeAddOp(v, OP_MemLoad, mem, 0);
drh57dbd7b2005-07-08 18:25:26 +00001559 testAddr = sqlite3VdbeAddOp(v, OP_If, 0, 0);
drh17435752007-08-16 04:30:38 +00001560 assert( testAddr>0 || pParse->db->mallocFailed );
drhd654be82005-09-20 17:42:23 +00001561 sqlite3VdbeAddOp(v, OP_MemInt, 1, mem);
danielk1977b3bce662005-01-29 08:32:43 +00001562 }
1563
drhcce7d172000-05-31 15:34:51 +00001564 switch( pExpr->op ){
drhfef52082000-06-06 01:50:43 +00001565 case TK_IN: {
danielk1977e014a832004-05-17 10:48:57 +00001566 char affinity;
drhd3d39e92004-05-20 22:16:29 +00001567 KeyInfo keyInfo;
drhb9bb7c12006-06-11 23:41:55 +00001568 int addr; /* Address of OP_OpenEphemeral instruction */
drhd3d39e92004-05-20 22:16:29 +00001569
danielk1977bf3b7212004-05-18 10:06:24 +00001570 affinity = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +00001571
1572 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
drh57dbd7b2005-07-08 18:25:26 +00001573 ** expression it is handled the same way. A virtual table is
danielk1977e014a832004-05-17 10:48:57 +00001574 ** filled with single-field index keys representing the results
1575 ** from the SELECT or the <exprlist>.
1576 **
1577 ** If the 'x' expression is a column value, or the SELECT...
1578 ** statement returns a column value, then the affinity of that
1579 ** column is used to build the index keys. If both 'x' and the
1580 ** SELECT... statement are columns, then numeric affinity is used
1581 ** if either column has NUMERIC or INTEGER affinity. If neither
1582 ** 'x' nor the SELECT... statement are columns, then numeric affinity
1583 ** is used.
1584 */
1585 pExpr->iTable = pParse->nTab++;
drhb9bb7c12006-06-11 23:41:55 +00001586 addr = sqlite3VdbeAddOp(v, OP_OpenEphemeral, pExpr->iTable, 0);
drhd3d39e92004-05-20 22:16:29 +00001587 memset(&keyInfo, 0, sizeof(keyInfo));
1588 keyInfo.nField = 1;
drhf3218fe2004-05-28 08:21:02 +00001589 sqlite3VdbeAddOp(v, OP_SetNumColumns, pExpr->iTable, 1);
danielk1977e014a832004-05-17 10:48:57 +00001590
drhfef52082000-06-06 01:50:43 +00001591 if( pExpr->pSelect ){
1592 /* Case 1: expr IN (SELECT ...)
1593 **
danielk1977e014a832004-05-17 10:48:57 +00001594 ** Generate code to write the results of the select into the temporary
1595 ** table allocated and opened above.
drhfef52082000-06-06 01:50:43 +00001596 */
danielk1977e014a832004-05-17 10:48:57 +00001597 int iParm = pExpr->iTable + (((int)affinity)<<16);
drhbe5c89a2004-07-26 00:31:09 +00001598 ExprList *pEList;
danielk1977e014a832004-05-17 10:48:57 +00001599 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
drh94ccde52007-04-13 16:06:32 +00001600 if( sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0) ){
1601 return;
1602 }
drhbe5c89a2004-07-26 00:31:09 +00001603 pEList = pExpr->pSelect->pEList;
1604 if( pEList && pEList->nExpr>0 ){
danielk1977bcbb04e2007-05-29 12:11:29 +00001605 keyInfo.aColl[0] = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft,
drhbe5c89a2004-07-26 00:31:09 +00001606 pEList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001607 }
drhfef52082000-06-06 01:50:43 +00001608 }else if( pExpr->pList ){
1609 /* Case 2: expr IN (exprlist)
1610 **
drhfd131da2007-08-07 17:13:03 +00001611 ** For each expression, build an index key from the evaluation and
danielk1977e014a832004-05-17 10:48:57 +00001612 ** store it in the temporary table. If <expr> is a column, then use
1613 ** that columns affinity when building index keys. If <expr> is not
1614 ** a column, use numeric affinity.
drhfef52082000-06-06 01:50:43 +00001615 */
danielk1977e014a832004-05-17 10:48:57 +00001616 int i;
drh57dbd7b2005-07-08 18:25:26 +00001617 ExprList *pList = pExpr->pList;
1618 struct ExprList_item *pItem;
1619
danielk1977e014a832004-05-17 10:48:57 +00001620 if( !affinity ){
drh8159a352006-05-23 23:22:29 +00001621 affinity = SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +00001622 }
danielk19770202b292004-06-09 09:55:16 +00001623 keyInfo.aColl[0] = pExpr->pLeft->pColl;
danielk1977e014a832004-05-17 10:48:57 +00001624
1625 /* Loop through each expression in <exprlist>. */
drh57dbd7b2005-07-08 18:25:26 +00001626 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
1627 Expr *pE2 = pItem->pExpr;
danielk1977e014a832004-05-17 10:48:57 +00001628
drh57dbd7b2005-07-08 18:25:26 +00001629 /* If the expression is not constant then we will need to
1630 ** disable the test that was generated above that makes sure
1631 ** this code only executes once. Because for a non-constant
1632 ** expression we need to rerun this code each time.
1633 */
drh6c30be82005-07-29 15:10:17 +00001634 if( testAddr>0 && !sqlite3ExprIsConstant(pE2) ){
drhf8875402006-03-17 13:56:34 +00001635 sqlite3VdbeChangeToNoop(v, testAddr-1, 3);
drh57dbd7b2005-07-08 18:25:26 +00001636 testAddr = 0;
drh4794b982000-06-06 13:54:14 +00001637 }
danielk1977e014a832004-05-17 10:48:57 +00001638
1639 /* Evaluate the expression and insert it into the temp table */
1640 sqlite3ExprCode(pParse, pE2);
drh94a11212004-09-25 13:12:14 +00001641 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1);
drhf0863fe2005-06-12 21:35:51 +00001642 sqlite3VdbeAddOp(v, OP_IdxInsert, pExpr->iTable, 0);
drhfef52082000-06-06 01:50:43 +00001643 }
1644 }
danielk19770202b292004-06-09 09:55:16 +00001645 sqlite3VdbeChangeP3(v, addr, (void *)&keyInfo, P3_KEYINFO);
danielk1977b3bce662005-01-29 08:32:43 +00001646 break;
drhfef52082000-06-06 01:50:43 +00001647 }
1648
drh51522cd2005-01-20 13:36:19 +00001649 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001650 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +00001651 /* This has to be a scalar SELECT. Generate code to put the
1652 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +00001653 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +00001654 */
drh2646da72005-12-09 20:02:05 +00001655 static const Token one = { (u8*)"1", 0, 1 };
drh51522cd2005-01-20 13:36:19 +00001656 Select *pSel;
drhec7429a2005-10-06 16:53:14 +00001657 int iMem;
1658 int sop;
drh1398ad32005-01-19 23:24:50 +00001659
drhec7429a2005-10-06 16:53:14 +00001660 pExpr->iColumn = iMem = pParse->nMem++;
drh51522cd2005-01-20 13:36:19 +00001661 pSel = pExpr->pSelect;
1662 if( pExpr->op==TK_SELECT ){
1663 sop = SRT_Mem;
drhec7429a2005-10-06 16:53:14 +00001664 sqlite3VdbeAddOp(v, OP_MemNull, iMem, 0);
1665 VdbeComment((v, "# Init subquery result"));
drh51522cd2005-01-20 13:36:19 +00001666 }else{
drh51522cd2005-01-20 13:36:19 +00001667 sop = SRT_Exists;
drhec7429a2005-10-06 16:53:14 +00001668 sqlite3VdbeAddOp(v, OP_MemInt, 0, iMem);
1669 VdbeComment((v, "# Init EXISTS result"));
drh51522cd2005-01-20 13:36:19 +00001670 }
drhec7429a2005-10-06 16:53:14 +00001671 sqlite3ExprDelete(pSel->pLimit);
1672 pSel->pLimit = sqlite3Expr(TK_INTEGER, 0, 0, &one);
drh94ccde52007-04-13 16:06:32 +00001673 if( sqlite3Select(pParse, pSel, sop, iMem, 0, 0, 0, 0) ){
1674 return;
1675 }
danielk1977b3bce662005-01-29 08:32:43 +00001676 break;
drhcce7d172000-05-31 15:34:51 +00001677 }
1678 }
danielk1977b3bce662005-01-29 08:32:43 +00001679
drh57dbd7b2005-07-08 18:25:26 +00001680 if( testAddr ){
drhd654be82005-09-20 17:42:23 +00001681 sqlite3VdbeJumpHere(v, testAddr);
danielk1977b3bce662005-01-29 08:32:43 +00001682 }
danielk1977fc976062007-05-10 10:46:56 +00001683
danielk1977b3bce662005-01-29 08:32:43 +00001684 return;
drhcce7d172000-05-31 15:34:51 +00001685}
drh51522cd2005-01-20 13:36:19 +00001686#endif /* SQLITE_OMIT_SUBQUERY */
drhcce7d172000-05-31 15:34:51 +00001687
drhcce7d172000-05-31 15:34:51 +00001688/*
drhfec19aa2004-05-19 20:41:03 +00001689** Generate an instruction that will put the integer describe by
1690** text z[0..n-1] on the stack.
1691*/
1692static void codeInteger(Vdbe *v, const char *z, int n){
drh17435752007-08-16 04:30:38 +00001693 assert( z || v==0 || sqlite3DbOfVdbe(v)->mallocFailed );
danielk1977c9cf9012007-05-30 10:36:47 +00001694 if( z ){
1695 int i;
1696 if( sqlite3GetInt32(z, &i) ){
1697 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
1698 }else if( sqlite3FitsIn64Bits(z) ){
1699 sqlite3VdbeOp3(v, OP_Int64, 0, 0, z, n);
1700 }else{
1701 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n);
1702 }
drhfec19aa2004-05-19 20:41:03 +00001703 }
1704}
1705
drh945498f2007-02-24 11:52:52 +00001706
1707/*
1708** Generate code that will extract the iColumn-th column from
1709** table pTab and push that column value on the stack. There
1710** is an open cursor to pTab in iTable. If iColumn<0 then
1711** code is generated that extracts the rowid.
1712*/
1713void sqlite3ExprCodeGetColumn(Vdbe *v, Table *pTab, int iColumn, int iTable){
1714 if( iColumn<0 ){
1715 int op = (pTab && IsVirtual(pTab)) ? OP_VRowid : OP_Rowid;
1716 sqlite3VdbeAddOp(v, op, iTable, 0);
1717 }else if( pTab==0 ){
1718 sqlite3VdbeAddOp(v, OP_Column, iTable, iColumn);
1719 }else{
1720 int op = IsVirtual(pTab) ? OP_VColumn : OP_Column;
1721 sqlite3VdbeAddOp(v, op, iTable, iColumn);
1722 sqlite3ColumnDefault(v, pTab, iColumn);
1723#ifndef SQLITE_OMIT_FLOATING_POINT
1724 if( pTab->aCol[iColumn].affinity==SQLITE_AFF_REAL ){
1725 sqlite3VdbeAddOp(v, OP_RealAffinity, 0, 0);
1726 }
1727#endif
1728 }
1729}
1730
drhfec19aa2004-05-19 20:41:03 +00001731/*
drhcce7d172000-05-31 15:34:51 +00001732** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +00001733** expression and leave the result on the top of stack.
drhf2bc0132004-10-04 13:19:23 +00001734**
1735** This code depends on the fact that certain token values (ex: TK_EQ)
1736** are the same as opcode values (ex: OP_Eq) that implement the corresponding
1737** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
1738** the make process cause these values to align. Assert()s in the code
1739** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00001740*/
danielk19774adee202004-05-08 08:23:19 +00001741void sqlite3ExprCode(Parse *pParse, Expr *pExpr){
drhcce7d172000-05-31 15:34:51 +00001742 Vdbe *v = pParse->pVdbe;
1743 int op;
drhffe07b22005-11-03 00:41:17 +00001744 int stackChng = 1; /* Amount of change to stack depth */
1745
danielk19777977a172004-11-09 12:44:37 +00001746 if( v==0 ) return;
1747 if( pExpr==0 ){
drhf0863fe2005-06-12 21:35:51 +00001748 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
danielk19777977a172004-11-09 12:44:37 +00001749 return;
1750 }
drhf2bc0132004-10-04 13:19:23 +00001751 op = pExpr->op;
1752 switch( op ){
drh13449892005-09-07 21:22:45 +00001753 case TK_AGG_COLUMN: {
1754 AggInfo *pAggInfo = pExpr->pAggInfo;
1755 struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg];
1756 if( !pAggInfo->directMode ){
1757 sqlite3VdbeAddOp(v, OP_MemLoad, pCol->iMem, 0);
1758 break;
1759 }else if( pAggInfo->useSortingIdx ){
1760 sqlite3VdbeAddOp(v, OP_Column, pAggInfo->sortingIdx,
1761 pCol->iSorterColumn);
1762 break;
1763 }
1764 /* Otherwise, fall thru into the TK_COLUMN case */
1765 }
drh967e8b72000-06-21 13:59:10 +00001766 case TK_COLUMN: {
drhffe07b22005-11-03 00:41:17 +00001767 if( pExpr->iTable<0 ){
1768 /* This only happens when coding check constraints */
1769 assert( pParse->ckOffset>0 );
1770 sqlite3VdbeAddOp(v, OP_Dup, pParse->ckOffset-pExpr->iColumn-1, 1);
drhc4a3c772001-04-04 11:48:57 +00001771 }else{
drh945498f2007-02-24 11:52:52 +00001772 sqlite3ExprCodeGetColumn(v, pExpr->pTab, pExpr->iColumn, pExpr->iTable);
drh22827922000-06-06 17:27:05 +00001773 }
drhcce7d172000-05-31 15:34:51 +00001774 break;
1775 }
1776 case TK_INTEGER: {
drh2646da72005-12-09 20:02:05 +00001777 codeInteger(v, (char*)pExpr->token.z, pExpr->token.n);
drhfec19aa2004-05-19 20:41:03 +00001778 break;
1779 }
1780 case TK_FLOAT:
1781 case TK_STRING: {
drhf2bc0132004-10-04 13:19:23 +00001782 assert( TK_FLOAT==OP_Real );
1783 assert( TK_STRING==OP_String8 );
danielk19771e536952007-08-16 10:09:01 +00001784 sqlite3DequoteExpr(pParse->db, pExpr);
drh2646da72005-12-09 20:02:05 +00001785 sqlite3VdbeOp3(v, op, 0, 0, (char*)pExpr->token.z, pExpr->token.n);
drhcce7d172000-05-31 15:34:51 +00001786 break;
1787 }
drhf0863fe2005-06-12 21:35:51 +00001788 case TK_NULL: {
1789 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
1790 break;
1791 }
danielk19775338a5f2005-01-20 13:03:10 +00001792#ifndef SQLITE_OMIT_BLOB_LITERAL
danielk1977c572ef72004-05-27 09:28:41 +00001793 case TK_BLOB: {
drh6c8c6ce2005-08-23 11:17:58 +00001794 int n;
1795 const char *z;
drhf2bc0132004-10-04 13:19:23 +00001796 assert( TK_BLOB==OP_HexBlob );
drh6c8c6ce2005-08-23 11:17:58 +00001797 n = pExpr->token.n - 3;
drh2646da72005-12-09 20:02:05 +00001798 z = (char*)pExpr->token.z + 2;
drh6c8c6ce2005-08-23 11:17:58 +00001799 assert( n>=0 );
1800 if( n==0 ){
1801 z = "";
1802 }
1803 sqlite3VdbeOp3(v, op, 0, 0, z, n);
danielk1977c572ef72004-05-27 09:28:41 +00001804 break;
1805 }
danielk19775338a5f2005-01-20 13:03:10 +00001806#endif
drh50457892003-09-06 01:10:47 +00001807 case TK_VARIABLE: {
danielk19774adee202004-05-08 08:23:19 +00001808 sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0);
drh895d7472004-08-20 16:02:39 +00001809 if( pExpr->token.n>1 ){
drh2646da72005-12-09 20:02:05 +00001810 sqlite3VdbeChangeP3(v, -1, (char*)pExpr->token.z, pExpr->token.n);
drh895d7472004-08-20 16:02:39 +00001811 }
drh50457892003-09-06 01:10:47 +00001812 break;
1813 }
drh4e0cff62004-11-05 05:10:28 +00001814 case TK_REGISTER: {
1815 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iTable, 0);
1816 break;
1817 }
drh487e2622005-06-25 18:42:14 +00001818#ifndef SQLITE_OMIT_CAST
1819 case TK_CAST: {
1820 /* Expressions of the form: CAST(pLeft AS token) */
danielk1977f0113002006-01-24 12:09:17 +00001821 int aff, to_op;
drh487e2622005-06-25 18:42:14 +00001822 sqlite3ExprCode(pParse, pExpr->pLeft);
drh8a512562005-11-14 22:29:05 +00001823 aff = sqlite3AffinityType(&pExpr->token);
danielk1977f0113002006-01-24 12:09:17 +00001824 to_op = aff - SQLITE_AFF_TEXT + OP_ToText;
1825 assert( to_op==OP_ToText || aff!=SQLITE_AFF_TEXT );
1826 assert( to_op==OP_ToBlob || aff!=SQLITE_AFF_NONE );
1827 assert( to_op==OP_ToNumeric || aff!=SQLITE_AFF_NUMERIC );
1828 assert( to_op==OP_ToInt || aff!=SQLITE_AFF_INTEGER );
1829 assert( to_op==OP_ToReal || aff!=SQLITE_AFF_REAL );
1830 sqlite3VdbeAddOp(v, to_op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001831 stackChng = 0;
drh487e2622005-06-25 18:42:14 +00001832 break;
1833 }
1834#endif /* SQLITE_OMIT_CAST */
drhc9b84a12002-06-20 11:36:48 +00001835 case TK_LT:
1836 case TK_LE:
1837 case TK_GT:
1838 case TK_GE:
1839 case TK_NE:
1840 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00001841 assert( TK_LT==OP_Lt );
1842 assert( TK_LE==OP_Le );
1843 assert( TK_GT==OP_Gt );
1844 assert( TK_GE==OP_Ge );
1845 assert( TK_EQ==OP_Eq );
1846 assert( TK_NE==OP_Ne );
danielk1977a37cdde2004-05-16 11:15:36 +00001847 sqlite3ExprCode(pParse, pExpr->pLeft);
1848 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001849 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001850 stackChng = -1;
danielk1977a37cdde2004-05-16 11:15:36 +00001851 break;
drhc9b84a12002-06-20 11:36:48 +00001852 }
drhcce7d172000-05-31 15:34:51 +00001853 case TK_AND:
1854 case TK_OR:
1855 case TK_PLUS:
1856 case TK_STAR:
1857 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00001858 case TK_REM:
1859 case TK_BITAND:
1860 case TK_BITOR:
drh17c40292004-07-21 02:53:29 +00001861 case TK_SLASH:
drhbf4133c2001-10-13 02:59:08 +00001862 case TK_LSHIFT:
drh855eb1c2004-08-31 13:45:11 +00001863 case TK_RSHIFT:
drh00400772000-06-16 20:51:26 +00001864 case TK_CONCAT: {
drhf2bc0132004-10-04 13:19:23 +00001865 assert( TK_AND==OP_And );
1866 assert( TK_OR==OP_Or );
1867 assert( TK_PLUS==OP_Add );
1868 assert( TK_MINUS==OP_Subtract );
1869 assert( TK_REM==OP_Remainder );
1870 assert( TK_BITAND==OP_BitAnd );
1871 assert( TK_BITOR==OP_BitOr );
1872 assert( TK_SLASH==OP_Divide );
1873 assert( TK_LSHIFT==OP_ShiftLeft );
1874 assert( TK_RSHIFT==OP_ShiftRight );
1875 assert( TK_CONCAT==OP_Concat );
danielk19774adee202004-05-08 08:23:19 +00001876 sqlite3ExprCode(pParse, pExpr->pLeft);
1877 sqlite3ExprCode(pParse, pExpr->pRight);
drh855eb1c2004-08-31 13:45:11 +00001878 sqlite3VdbeAddOp(v, op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001879 stackChng = -1;
drh00400772000-06-16 20:51:26 +00001880 break;
1881 }
drhcce7d172000-05-31 15:34:51 +00001882 case TK_UMINUS: {
drhfec19aa2004-05-19 20:41:03 +00001883 Expr *pLeft = pExpr->pLeft;
1884 assert( pLeft );
1885 if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
1886 Token *p = &pLeft->token;
danielk19771e536952007-08-16 10:09:01 +00001887 char *z = sqlite3MPrintf(pParse->db, "-%.*s", p->n, p->z);
drhfec19aa2004-05-19 20:41:03 +00001888 if( pLeft->op==TK_FLOAT ){
1889 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001890 }else{
drhfec19aa2004-05-19 20:41:03 +00001891 codeInteger(v, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001892 }
drh17435752007-08-16 04:30:38 +00001893 sqlite3_free(z);
drh6e142f52000-06-08 13:36:40 +00001894 break;
1895 }
drh1ccde152000-06-17 13:12:39 +00001896 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +00001897 }
drhbf4133c2001-10-13 02:59:08 +00001898 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00001899 case TK_NOT: {
drhf2bc0132004-10-04 13:19:23 +00001900 assert( TK_BITNOT==OP_BitNot );
1901 assert( TK_NOT==OP_Not );
danielk19774adee202004-05-08 08:23:19 +00001902 sqlite3ExprCode(pParse, pExpr->pLeft);
1903 sqlite3VdbeAddOp(v, op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001904 stackChng = 0;
drhcce7d172000-05-31 15:34:51 +00001905 break;
1906 }
1907 case TK_ISNULL:
1908 case TK_NOTNULL: {
1909 int dest;
drhf2bc0132004-10-04 13:19:23 +00001910 assert( TK_ISNULL==OP_IsNull );
1911 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00001912 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1913 sqlite3ExprCode(pParse, pExpr->pLeft);
1914 dest = sqlite3VdbeCurrentAddr(v) + 2;
1915 sqlite3VdbeAddOp(v, op, 1, dest);
1916 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);
drhffe07b22005-11-03 00:41:17 +00001917 stackChng = 0;
drhf2bc0132004-10-04 13:19:23 +00001918 break;
drhcce7d172000-05-31 15:34:51 +00001919 }
drh22827922000-06-06 17:27:05 +00001920 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00001921 AggInfo *pInfo = pExpr->pAggInfo;
drh7e56e712005-11-16 12:53:15 +00001922 if( pInfo==0 ){
1923 sqlite3ErrorMsg(pParse, "misuse of aggregate: %T",
1924 &pExpr->span);
1925 }else{
1926 sqlite3VdbeAddOp(v, OP_MemLoad, pInfo->aFunc[pExpr->iAgg].iMem, 0);
1927 }
drh22827922000-06-06 17:27:05 +00001928 break;
1929 }
drhb71090f2005-05-23 17:26:51 +00001930 case TK_CONST_FUNC:
drhcce7d172000-05-31 15:34:51 +00001931 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00001932 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00001933 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00001934 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00001935 int nId;
1936 const char *zId;
drh13449892005-09-07 21:22:45 +00001937 int constMask = 0;
danielk1977682f68b2004-06-05 10:22:17 +00001938 int i;
drh17435752007-08-16 04:30:38 +00001939 sqlite3 *db = pParse->db;
1940 u8 enc = ENC(db);
danielk1977dc1bdc42004-06-11 10:51:27 +00001941 CollSeq *pColl = 0;
drh17435752007-08-16 04:30:38 +00001942
drh2646da72005-12-09 20:02:05 +00001943 zId = (char*)pExpr->token.z;
drhb71090f2005-05-23 17:26:51 +00001944 nId = pExpr->token.n;
danielk1977d8123362004-06-12 09:25:12 +00001945 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001946 assert( pDef!=0 );
drhf9b596e2004-05-26 16:54:42 +00001947 nExpr = sqlite3ExprCodeExprList(pParse, pList);
drhb7f6f682006-07-08 17:06:43 +00001948#ifndef SQLITE_OMIT_VIRTUALTABLE
drha43fa222006-07-08 18:41:37 +00001949 /* Possibly overload the function if the first argument is
1950 ** a virtual table column.
1951 **
1952 ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the
1953 ** second argument, not the first, as the argument to test to
1954 ** see if it is a column in a virtual table. This is done because
1955 ** the left operand of infix functions (the operand we want to
1956 ** control overloading) ends up as the second argument to the
1957 ** function. The expression "A glob B" is equivalent to
1958 ** "glob(B,A). We want to use the A in "A glob B" to test
1959 ** for function overloading. But we use the B term in "glob(B,A)".
1960 */
drh6a03a1c2006-07-08 18:34:59 +00001961 if( nExpr>=2 && (pExpr->flags & EP_InfixFunc) ){
drh17435752007-08-16 04:30:38 +00001962 pDef = sqlite3VtabOverloadFunction(db, pDef, nExpr, pList->a[1].pExpr);
drh6a03a1c2006-07-08 18:34:59 +00001963 }else if( nExpr>0 ){
drh17435752007-08-16 04:30:38 +00001964 pDef = sqlite3VtabOverloadFunction(db, pDef, nExpr, pList->a[0].pExpr);
drhb7f6f682006-07-08 17:06:43 +00001965 }
1966#endif
danielk1977682f68b2004-06-05 10:22:17 +00001967 for(i=0; i<nExpr && i<32; i++){
danielk1977d02eb1f2004-06-06 09:44:03 +00001968 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
drh13449892005-09-07 21:22:45 +00001969 constMask |= (1<<i);
danielk1977d02eb1f2004-06-06 09:44:03 +00001970 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001971 if( pDef->needCollSeq && !pColl ){
1972 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
1973 }
1974 }
1975 if( pDef->needCollSeq ){
1976 if( !pColl ) pColl = pParse->db->pDfltColl;
danielk1977d8123362004-06-12 09:25:12 +00001977 sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ);
danielk1977682f68b2004-06-05 10:22:17 +00001978 }
drh13449892005-09-07 21:22:45 +00001979 sqlite3VdbeOp3(v, OP_Function, constMask, nExpr, (char*)pDef, P3_FUNCDEF);
drhffe07b22005-11-03 00:41:17 +00001980 stackChng = 1-nExpr;
drhcce7d172000-05-31 15:34:51 +00001981 break;
1982 }
drhfe2093d2005-01-20 22:48:47 +00001983#ifndef SQLITE_OMIT_SUBQUERY
1984 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001985 case TK_SELECT: {
drh41714d62006-03-02 04:44:23 +00001986 if( pExpr->iColumn==0 ){
1987 sqlite3CodeSubselect(pParse, pExpr);
1988 }
danielk19774adee202004-05-08 08:23:19 +00001989 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drhad6d9462004-09-19 02:15:24 +00001990 VdbeComment((v, "# load subquery result"));
drh19a775c2000-06-05 18:54:46 +00001991 break;
1992 }
drhfef52082000-06-06 01:50:43 +00001993 case TK_IN: {
1994 int addr;
drh94a11212004-09-25 13:12:14 +00001995 char affinity;
drhafa5f682006-01-30 14:36:59 +00001996 int ckOffset = pParse->ckOffset;
danielk1977b3bce662005-01-29 08:32:43 +00001997 sqlite3CodeSubselect(pParse, pExpr);
danielk1977e014a832004-05-17 10:48:57 +00001998
1999 /* Figure out the affinity to use to create a key from the results
2000 ** of the expression. affinityStr stores a static string suitable for
danielk1977ededfd52004-06-17 07:53:01 +00002001 ** P3 of OP_MakeRecord.
danielk1977e014a832004-05-17 10:48:57 +00002002 */
drh94a11212004-09-25 13:12:14 +00002003 affinity = comparisonAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +00002004
danielk19774adee202004-05-08 08:23:19 +00002005 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
danielk1977cdbd8ef2007-05-12 06:11:12 +00002006 pParse->ckOffset = (ckOffset ? (ckOffset+1) : 0);
danielk1977e014a832004-05-17 10:48:57 +00002007
2008 /* Code the <expr> from "<expr> IN (...)". The temporary table
2009 ** pExpr->iTable contains the values that make up the (...) set.
2010 */
danielk19774adee202004-05-08 08:23:19 +00002011 sqlite3ExprCode(pParse, pExpr->pLeft);
2012 addr = sqlite3VdbeCurrentAddr(v);
danielk1977e014a832004-05-17 10:48:57 +00002013 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); /* addr + 0 */
danielk19774adee202004-05-08 08:23:19 +00002014 sqlite3VdbeAddOp(v, OP_Pop, 2, 0);
drhf0863fe2005-06-12 21:35:51 +00002015 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +00002016 sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7);
drh94a11212004-09-25 13:12:14 +00002017 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1); /* addr + 4 */
danielk1977e014a832004-05-17 10:48:57 +00002018 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7);
2019 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); /* addr + 6 */
2020
drhfef52082000-06-06 01:50:43 +00002021 break;
2022 }
danielk197793758c82005-01-21 08:13:14 +00002023#endif
drhfef52082000-06-06 01:50:43 +00002024 case TK_BETWEEN: {
drhbe5c89a2004-07-26 00:31:09 +00002025 Expr *pLeft = pExpr->pLeft;
2026 struct ExprList_item *pLItem = pExpr->pList->a;
2027 Expr *pRight = pLItem->pExpr;
2028 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00002029 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00002030 sqlite3ExprCode(pParse, pRight);
2031 codeCompare(pParse, pLeft, pRight, OP_Ge, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00002032 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
drhbe5c89a2004-07-26 00:31:09 +00002033 pLItem++;
2034 pRight = pLItem->pExpr;
2035 sqlite3ExprCode(pParse, pRight);
2036 codeCompare(pParse, pLeft, pRight, OP_Le, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00002037 sqlite3VdbeAddOp(v, OP_And, 0, 0);
drhfef52082000-06-06 01:50:43 +00002038 break;
2039 }
drh4f07e5f2007-05-14 11:34:46 +00002040 case TK_UPLUS: {
danielk19774adee202004-05-08 08:23:19 +00002041 sqlite3ExprCode(pParse, pExpr->pLeft);
drhffe07b22005-11-03 00:41:17 +00002042 stackChng = 0;
drha2e00042002-01-22 03:13:42 +00002043 break;
2044 }
drh17a7f8d2002-03-24 13:13:27 +00002045 case TK_CASE: {
2046 int expr_end_label;
drhf5905aa2002-05-26 20:54:33 +00002047 int jumpInst;
drhf5905aa2002-05-26 20:54:33 +00002048 int nExpr;
drh17a7f8d2002-03-24 13:13:27 +00002049 int i;
drhbe5c89a2004-07-26 00:31:09 +00002050 ExprList *pEList;
2051 struct ExprList_item *aListelem;
drh17a7f8d2002-03-24 13:13:27 +00002052
2053 assert(pExpr->pList);
2054 assert((pExpr->pList->nExpr % 2) == 0);
2055 assert(pExpr->pList->nExpr > 0);
drhbe5c89a2004-07-26 00:31:09 +00002056 pEList = pExpr->pList;
2057 aListelem = pEList->a;
2058 nExpr = pEList->nExpr;
danielk19774adee202004-05-08 08:23:19 +00002059 expr_end_label = sqlite3VdbeMakeLabel(v);
drh17a7f8d2002-03-24 13:13:27 +00002060 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00002061 sqlite3ExprCode(pParse, pExpr->pLeft);
drh17a7f8d2002-03-24 13:13:27 +00002062 }
drhf5905aa2002-05-26 20:54:33 +00002063 for(i=0; i<nExpr; i=i+2){
drhbe5c89a2004-07-26 00:31:09 +00002064 sqlite3ExprCode(pParse, aListelem[i].pExpr);
drh17a7f8d2002-03-24 13:13:27 +00002065 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00002066 sqlite3VdbeAddOp(v, OP_Dup, 1, 1);
drhbe5c89a2004-07-26 00:31:09 +00002067 jumpInst = codeCompare(pParse, pExpr->pLeft, aListelem[i].pExpr,
2068 OP_Ne, 0, 1);
danielk19774adee202004-05-08 08:23:19 +00002069 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00002070 }else{
danielk19774adee202004-05-08 08:23:19 +00002071 jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00002072 }
drhbe5c89a2004-07-26 00:31:09 +00002073 sqlite3ExprCode(pParse, aListelem[i+1].pExpr);
danielk19774adee202004-05-08 08:23:19 +00002074 sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label);
drhd654be82005-09-20 17:42:23 +00002075 sqlite3VdbeJumpHere(v, jumpInst);
drh17a7f8d2002-03-24 13:13:27 +00002076 }
drhf570f012002-05-31 15:51:25 +00002077 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00002078 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhf570f012002-05-31 15:51:25 +00002079 }
drh17a7f8d2002-03-24 13:13:27 +00002080 if( pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00002081 sqlite3ExprCode(pParse, pExpr->pRight);
drh17a7f8d2002-03-24 13:13:27 +00002082 }else{
drhf0863fe2005-06-12 21:35:51 +00002083 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
drh17a7f8d2002-03-24 13:13:27 +00002084 }
danielk19774adee202004-05-08 08:23:19 +00002085 sqlite3VdbeResolveLabel(v, expr_end_label);
danielk19776f349032002-06-11 02:25:40 +00002086 break;
2087 }
danielk19775338a5f2005-01-20 13:03:10 +00002088#ifndef SQLITE_OMIT_TRIGGER
danielk19776f349032002-06-11 02:25:40 +00002089 case TK_RAISE: {
2090 if( !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +00002091 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00002092 "RAISE() may only be used within a trigger-program");
drhfd131da2007-08-07 17:13:03 +00002093 return;
danielk19776f349032002-06-11 02:25:40 +00002094 }
drhad6d9462004-09-19 02:15:24 +00002095 if( pExpr->iColumn!=OE_Ignore ){
2096 assert( pExpr->iColumn==OE_Rollback ||
2097 pExpr->iColumn == OE_Abort ||
2098 pExpr->iColumn == OE_Fail );
danielk19771e536952007-08-16 10:09:01 +00002099 sqlite3DequoteExpr(pParse->db, pExpr);
drhad6d9462004-09-19 02:15:24 +00002100 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn,
drh2646da72005-12-09 20:02:05 +00002101 (char*)pExpr->token.z, pExpr->token.n);
danielk19776f349032002-06-11 02:25:40 +00002102 } else {
drhad6d9462004-09-19 02:15:24 +00002103 assert( pExpr->iColumn == OE_Ignore );
2104 sqlite3VdbeAddOp(v, OP_ContextPop, 0, 0);
2105 sqlite3VdbeAddOp(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
2106 VdbeComment((v, "# raise(IGNORE)"));
danielk19776f349032002-06-11 02:25:40 +00002107 }
drhffe07b22005-11-03 00:41:17 +00002108 stackChng = 0;
2109 break;
drh17a7f8d2002-03-24 13:13:27 +00002110 }
danielk19775338a5f2005-01-20 13:03:10 +00002111#endif
drhffe07b22005-11-03 00:41:17 +00002112 }
2113
2114 if( pParse->ckOffset ){
2115 pParse->ckOffset += stackChng;
2116 assert( pParse->ckOffset );
drhcce7d172000-05-31 15:34:51 +00002117 }
drhcce7d172000-05-31 15:34:51 +00002118}
2119
danielk197793758c82005-01-21 08:13:14 +00002120#ifndef SQLITE_OMIT_TRIGGER
drhcce7d172000-05-31 15:34:51 +00002121/*
drh25303782004-12-07 15:41:48 +00002122** Generate code that evalutes the given expression and leaves the result
2123** on the stack. See also sqlite3ExprCode().
2124**
2125** This routine might also cache the result and modify the pExpr tree
2126** so that it will make use of the cached result on subsequent evaluations
2127** rather than evaluate the whole expression again. Trivial expressions are
2128** not cached. If the expression is cached, its result is stored in a
2129** memory location.
2130*/
2131void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr){
2132 Vdbe *v = pParse->pVdbe;
2133 int iMem;
2134 int addr1, addr2;
2135 if( v==0 ) return;
2136 addr1 = sqlite3VdbeCurrentAddr(v);
2137 sqlite3ExprCode(pParse, pExpr);
2138 addr2 = sqlite3VdbeCurrentAddr(v);
2139 if( addr2>addr1+1 || sqlite3VdbeGetOp(v, addr1)->opcode==OP_Function ){
2140 iMem = pExpr->iTable = pParse->nMem++;
2141 sqlite3VdbeAddOp(v, OP_MemStore, iMem, 0);
2142 pExpr->op = TK_REGISTER;
2143 }
2144}
danielk197793758c82005-01-21 08:13:14 +00002145#endif
drh25303782004-12-07 15:41:48 +00002146
2147/*
drh268380c2004-02-25 13:47:31 +00002148** Generate code that pushes the value of every element of the given
drhf9b596e2004-05-26 16:54:42 +00002149** expression list onto the stack.
drh268380c2004-02-25 13:47:31 +00002150**
2151** Return the number of elements pushed onto the stack.
2152*/
danielk19774adee202004-05-08 08:23:19 +00002153int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00002154 Parse *pParse, /* Parsing context */
drhf9b596e2004-05-26 16:54:42 +00002155 ExprList *pList /* The expression list to be coded */
drh268380c2004-02-25 13:47:31 +00002156){
2157 struct ExprList_item *pItem;
2158 int i, n;
drh268380c2004-02-25 13:47:31 +00002159 if( pList==0 ) return 0;
drh268380c2004-02-25 13:47:31 +00002160 n = pList->nExpr;
drhc182d162005-08-14 20:47:16 +00002161 for(pItem=pList->a, i=n; i>0; i--, pItem++){
danielk19774adee202004-05-08 08:23:19 +00002162 sqlite3ExprCode(pParse, pItem->pExpr);
drh268380c2004-02-25 13:47:31 +00002163 }
drhf9b596e2004-05-26 16:54:42 +00002164 return n;
drh268380c2004-02-25 13:47:31 +00002165}
2166
2167/*
drhcce7d172000-05-31 15:34:51 +00002168** Generate code for a boolean expression such that a jump is made
2169** to the label "dest" if the expression is true but execution
2170** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00002171**
2172** If the expression evaluates to NULL (neither true nor false), then
2173** take the jump if the jumpIfNull flag is true.
drhf2bc0132004-10-04 13:19:23 +00002174**
2175** This code depends on the fact that certain token values (ex: TK_EQ)
2176** are the same as opcode values (ex: OP_Eq) that implement the corresponding
2177** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
2178** the make process cause these values to align. Assert()s in the code
2179** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00002180*/
danielk19774adee202004-05-08 08:23:19 +00002181void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00002182 Vdbe *v = pParse->pVdbe;
2183 int op = 0;
drhffe07b22005-11-03 00:41:17 +00002184 int ckOffset = pParse->ckOffset;
drhdaffd0e2001-04-11 14:28:42 +00002185 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00002186 op = pExpr->op;
2187 switch( op ){
drhcce7d172000-05-31 15:34:51 +00002188 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00002189 int d2 = sqlite3VdbeMakeLabel(v);
2190 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
2191 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
2192 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00002193 break;
2194 }
2195 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00002196 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
2197 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002198 break;
2199 }
2200 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00002201 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002202 break;
2203 }
2204 case TK_LT:
2205 case TK_LE:
2206 case TK_GT:
2207 case TK_GE:
2208 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00002209 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00002210 assert( TK_LT==OP_Lt );
2211 assert( TK_LE==OP_Le );
2212 assert( TK_GT==OP_Gt );
2213 assert( TK_GE==OP_Ge );
2214 assert( TK_EQ==OP_Eq );
2215 assert( TK_NE==OP_Ne );
danielk19774adee202004-05-08 08:23:19 +00002216 sqlite3ExprCode(pParse, pExpr->pLeft);
2217 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00002218 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002219 break;
2220 }
2221 case TK_ISNULL:
2222 case TK_NOTNULL: {
drhf2bc0132004-10-04 13:19:23 +00002223 assert( TK_ISNULL==OP_IsNull );
2224 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00002225 sqlite3ExprCode(pParse, pExpr->pLeft);
2226 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00002227 break;
2228 }
drhfef52082000-06-06 01:50:43 +00002229 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00002230 /* The expression "x BETWEEN y AND z" is implemented as:
2231 **
2232 ** 1 IF (x < y) GOTO 3
2233 ** 2 IF (x <= z) GOTO <dest>
2234 ** 3 ...
2235 */
drhf5905aa2002-05-26 20:54:33 +00002236 int addr;
drhbe5c89a2004-07-26 00:31:09 +00002237 Expr *pLeft = pExpr->pLeft;
2238 Expr *pRight = pExpr->pList->a[0].pExpr;
2239 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00002240 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00002241 sqlite3ExprCode(pParse, pRight);
2242 addr = codeCompare(pParse, pLeft, pRight, OP_Lt, 0, !jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00002243
drhbe5c89a2004-07-26 00:31:09 +00002244 pRight = pExpr->pList->a[1].pExpr;
2245 sqlite3ExprCode(pParse, pRight);
2246 codeCompare(pParse, pLeft, pRight, OP_Le, dest, jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00002247
danielk19774adee202004-05-08 08:23:19 +00002248 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
drhd654be82005-09-20 17:42:23 +00002249 sqlite3VdbeJumpHere(v, addr);
danielk19774adee202004-05-08 08:23:19 +00002250 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00002251 break;
2252 }
drhcce7d172000-05-31 15:34:51 +00002253 default: {
danielk19774adee202004-05-08 08:23:19 +00002254 sqlite3ExprCode(pParse, pExpr);
2255 sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00002256 break;
2257 }
2258 }
drhffe07b22005-11-03 00:41:17 +00002259 pParse->ckOffset = ckOffset;
drhcce7d172000-05-31 15:34:51 +00002260}
2261
2262/*
drh66b89c82000-11-28 20:47:17 +00002263** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00002264** to the label "dest" if the expression is false but execution
2265** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00002266**
2267** If the expression evaluates to NULL (neither true nor false) then
2268** jump if jumpIfNull is true or fall through if jumpIfNull is false.
drhcce7d172000-05-31 15:34:51 +00002269*/
danielk19774adee202004-05-08 08:23:19 +00002270void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00002271 Vdbe *v = pParse->pVdbe;
2272 int op = 0;
drhffe07b22005-11-03 00:41:17 +00002273 int ckOffset = pParse->ckOffset;
drhdaffd0e2001-04-11 14:28:42 +00002274 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00002275
2276 /* The value of pExpr->op and op are related as follows:
2277 **
2278 ** pExpr->op op
2279 ** --------- ----------
2280 ** TK_ISNULL OP_NotNull
2281 ** TK_NOTNULL OP_IsNull
2282 ** TK_NE OP_Eq
2283 ** TK_EQ OP_Ne
2284 ** TK_GT OP_Le
2285 ** TK_LE OP_Gt
2286 ** TK_GE OP_Lt
2287 ** TK_LT OP_Ge
2288 **
2289 ** For other values of pExpr->op, op is undefined and unused.
2290 ** The value of TK_ and OP_ constants are arranged such that we
2291 ** can compute the mapping above using the following expression.
2292 ** Assert()s verify that the computation is correct.
2293 */
2294 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
2295
2296 /* Verify correct alignment of TK_ and OP_ constants
2297 */
2298 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
2299 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
2300 assert( pExpr->op!=TK_NE || op==OP_Eq );
2301 assert( pExpr->op!=TK_EQ || op==OP_Ne );
2302 assert( pExpr->op!=TK_LT || op==OP_Ge );
2303 assert( pExpr->op!=TK_LE || op==OP_Gt );
2304 assert( pExpr->op!=TK_GT || op==OP_Le );
2305 assert( pExpr->op!=TK_GE || op==OP_Lt );
2306
drhcce7d172000-05-31 15:34:51 +00002307 switch( pExpr->op ){
2308 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00002309 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
2310 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002311 break;
2312 }
2313 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00002314 int d2 = sqlite3VdbeMakeLabel(v);
2315 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
2316 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
2317 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00002318 break;
2319 }
2320 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00002321 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002322 break;
2323 }
2324 case TK_LT:
2325 case TK_LE:
2326 case TK_GT:
2327 case TK_GE:
2328 case TK_NE:
2329 case TK_EQ: {
danielk19774adee202004-05-08 08:23:19 +00002330 sqlite3ExprCode(pParse, pExpr->pLeft);
2331 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00002332 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002333 break;
2334 }
drhcce7d172000-05-31 15:34:51 +00002335 case TK_ISNULL:
2336 case TK_NOTNULL: {
danielk19774adee202004-05-08 08:23:19 +00002337 sqlite3ExprCode(pParse, pExpr->pLeft);
2338 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00002339 break;
2340 }
drhfef52082000-06-06 01:50:43 +00002341 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00002342 /* The expression is "x BETWEEN y AND z". It is implemented as:
2343 **
2344 ** 1 IF (x >= y) GOTO 3
2345 ** 2 GOTO <dest>
2346 ** 3 IF (x > z) GOTO <dest>
2347 */
drhfef52082000-06-06 01:50:43 +00002348 int addr;
drhbe5c89a2004-07-26 00:31:09 +00002349 Expr *pLeft = pExpr->pLeft;
2350 Expr *pRight = pExpr->pList->a[0].pExpr;
2351 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00002352 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00002353 sqlite3ExprCode(pParse, pRight);
danielk19774adee202004-05-08 08:23:19 +00002354 addr = sqlite3VdbeCurrentAddr(v);
drhbe5c89a2004-07-26 00:31:09 +00002355 codeCompare(pParse, pLeft, pRight, OP_Ge, addr+3, !jumpIfNull);
2356
danielk19774adee202004-05-08 08:23:19 +00002357 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
2358 sqlite3VdbeAddOp(v, OP_Goto, 0, dest);
drhbe5c89a2004-07-26 00:31:09 +00002359 pRight = pExpr->pList->a[1].pExpr;
2360 sqlite3ExprCode(pParse, pRight);
2361 codeCompare(pParse, pLeft, pRight, OP_Gt, dest, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00002362 break;
2363 }
drhcce7d172000-05-31 15:34:51 +00002364 default: {
danielk19774adee202004-05-08 08:23:19 +00002365 sqlite3ExprCode(pParse, pExpr);
2366 sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00002367 break;
2368 }
2369 }
drhffe07b22005-11-03 00:41:17 +00002370 pParse->ckOffset = ckOffset;
drhcce7d172000-05-31 15:34:51 +00002371}
drh22827922000-06-06 17:27:05 +00002372
2373/*
2374** Do a deep comparison of two expression trees. Return TRUE (non-zero)
2375** if they are identical and return FALSE if they differ in any way.
drhd40aab02007-02-24 15:29:03 +00002376**
2377** Sometimes this routine will return FALSE even if the two expressions
2378** really are equivalent. If we cannot prove that the expressions are
2379** identical, we return FALSE just to be safe. So if this routine
2380** returns false, then you do not really know for certain if the two
2381** expressions are the same. But if you get a TRUE return, then you
2382** can be sure the expressions are the same. In the places where
2383** this routine is used, it does not hurt to get an extra FALSE - that
2384** just might result in some slightly slower code. But returning
2385** an incorrect TRUE could lead to a malfunction.
drh22827922000-06-06 17:27:05 +00002386*/
danielk19774adee202004-05-08 08:23:19 +00002387int sqlite3ExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00002388 int i;
danielk19774b202ae2006-01-23 05:50:58 +00002389 if( pA==0||pB==0 ){
2390 return pB==pA;
drh22827922000-06-06 17:27:05 +00002391 }
2392 if( pA->op!=pB->op ) return 0;
drhfd357972005-09-09 01:33:19 +00002393 if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 0;
danielk19774adee202004-05-08 08:23:19 +00002394 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
2395 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00002396 if( pA->pList ){
2397 if( pB->pList==0 ) return 0;
2398 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
2399 for(i=0; i<pA->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00002400 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00002401 return 0;
2402 }
2403 }
2404 }else if( pB->pList ){
2405 return 0;
2406 }
2407 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00002408 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drhdd735212007-02-24 13:53:05 +00002409 if( pA->op!=TK_COLUMN && pA->token.z ){
drh22827922000-06-06 17:27:05 +00002410 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00002411 if( pB->token.n!=pA->token.n ) return 0;
drh2646da72005-12-09 20:02:05 +00002412 if( sqlite3StrNICmp((char*)pA->token.z,(char*)pB->token.z,pB->token.n)!=0 ){
2413 return 0;
2414 }
drh22827922000-06-06 17:27:05 +00002415 }
2416 return 1;
2417}
2418
drh13449892005-09-07 21:22:45 +00002419
drh22827922000-06-06 17:27:05 +00002420/*
drh13449892005-09-07 21:22:45 +00002421** Add a new element to the pAggInfo->aCol[] array. Return the index of
2422** the new element. Return a negative number if malloc fails.
drh22827922000-06-06 17:27:05 +00002423*/
drh17435752007-08-16 04:30:38 +00002424static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){
drh13449892005-09-07 21:22:45 +00002425 int i;
drhcf643722007-03-27 13:36:37 +00002426 pInfo->aCol = sqlite3ArrayAllocate(
drh17435752007-08-16 04:30:38 +00002427 db,
drhcf643722007-03-27 13:36:37 +00002428 pInfo->aCol,
2429 sizeof(pInfo->aCol[0]),
2430 3,
2431 &pInfo->nColumn,
2432 &pInfo->nColumnAlloc,
2433 &i
2434 );
drh13449892005-09-07 21:22:45 +00002435 return i;
2436}
2437
2438/*
2439** Add a new element to the pAggInfo->aFunc[] array. Return the index of
2440** the new element. Return a negative number if malloc fails.
2441*/
drh17435752007-08-16 04:30:38 +00002442static int addAggInfoFunc(sqlite3 *db, AggInfo *pInfo){
drh13449892005-09-07 21:22:45 +00002443 int i;
drhcf643722007-03-27 13:36:37 +00002444 pInfo->aFunc = sqlite3ArrayAllocate(
drh17435752007-08-16 04:30:38 +00002445 db,
drhcf643722007-03-27 13:36:37 +00002446 pInfo->aFunc,
2447 sizeof(pInfo->aFunc[0]),
2448 3,
2449 &pInfo->nFunc,
2450 &pInfo->nFuncAlloc,
2451 &i
2452 );
drh13449892005-09-07 21:22:45 +00002453 return i;
2454}
drh22827922000-06-06 17:27:05 +00002455
2456/*
drh626a8792005-01-17 22:08:19 +00002457** This is an xFunc for walkExprTree() used to implement
2458** sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates
2459** for additional information.
drh22827922000-06-06 17:27:05 +00002460**
drh626a8792005-01-17 22:08:19 +00002461** This routine analyzes the aggregate function at pExpr.
drh22827922000-06-06 17:27:05 +00002462*/
drh626a8792005-01-17 22:08:19 +00002463static int analyzeAggregate(void *pArg, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00002464 int i;
danielk1977a58fdfb2005-02-08 07:50:40 +00002465 NameContext *pNC = (NameContext *)pArg;
2466 Parse *pParse = pNC->pParse;
2467 SrcList *pSrcList = pNC->pSrcList;
drh13449892005-09-07 21:22:45 +00002468 AggInfo *pAggInfo = pNC->pAggInfo;
drh22827922000-06-06 17:27:05 +00002469
drh22827922000-06-06 17:27:05 +00002470 switch( pExpr->op ){
drh89c69d02007-01-04 01:20:28 +00002471 case TK_AGG_COLUMN:
drh967e8b72000-06-21 13:59:10 +00002472 case TK_COLUMN: {
drh13449892005-09-07 21:22:45 +00002473 /* Check to see if the column is in one of the tables in the FROM
2474 ** clause of the aggregate query */
2475 if( pSrcList ){
2476 struct SrcList_item *pItem = pSrcList->a;
2477 for(i=0; i<pSrcList->nSrc; i++, pItem++){
2478 struct AggInfo_col *pCol;
2479 if( pExpr->iTable==pItem->iCursor ){
2480 /* If we reach this point, it means that pExpr refers to a table
2481 ** that is in the FROM clause of the aggregate query.
2482 **
2483 ** Make an entry for the column in pAggInfo->aCol[] if there
2484 ** is not an entry there already.
2485 */
drh7f906d62007-03-12 23:48:52 +00002486 int k;
drh13449892005-09-07 21:22:45 +00002487 pCol = pAggInfo->aCol;
drh7f906d62007-03-12 23:48:52 +00002488 for(k=0; k<pAggInfo->nColumn; k++, pCol++){
drh13449892005-09-07 21:22:45 +00002489 if( pCol->iTable==pExpr->iTable &&
2490 pCol->iColumn==pExpr->iColumn ){
2491 break;
2492 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002493 }
danielk19771e536952007-08-16 10:09:01 +00002494 if( (k>=pAggInfo->nColumn)
2495 && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0
2496 ){
drh7f906d62007-03-12 23:48:52 +00002497 pCol = &pAggInfo->aCol[k];
danielk19770817d0d2007-02-14 09:19:36 +00002498 pCol->pTab = pExpr->pTab;
drh13449892005-09-07 21:22:45 +00002499 pCol->iTable = pExpr->iTable;
2500 pCol->iColumn = pExpr->iColumn;
2501 pCol->iMem = pParse->nMem++;
2502 pCol->iSorterColumn = -1;
drh5774b802005-09-07 22:48:16 +00002503 pCol->pExpr = pExpr;
drh13449892005-09-07 21:22:45 +00002504 if( pAggInfo->pGroupBy ){
2505 int j, n;
2506 ExprList *pGB = pAggInfo->pGroupBy;
2507 struct ExprList_item *pTerm = pGB->a;
2508 n = pGB->nExpr;
2509 for(j=0; j<n; j++, pTerm++){
2510 Expr *pE = pTerm->pExpr;
2511 if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable &&
2512 pE->iColumn==pExpr->iColumn ){
2513 pCol->iSorterColumn = j;
2514 break;
2515 }
2516 }
2517 }
2518 if( pCol->iSorterColumn<0 ){
2519 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
2520 }
2521 }
2522 /* There is now an entry for pExpr in pAggInfo->aCol[] (either
2523 ** because it was there before or because we just created it).
2524 ** Convert the pExpr to be a TK_AGG_COLUMN referring to that
2525 ** pAggInfo->aCol[] entry.
2526 */
2527 pExpr->pAggInfo = pAggInfo;
2528 pExpr->op = TK_AGG_COLUMN;
drh7f906d62007-03-12 23:48:52 +00002529 pExpr->iAgg = k;
drh13449892005-09-07 21:22:45 +00002530 break;
2531 } /* endif pExpr->iTable==pItem->iCursor */
2532 } /* end loop over pSrcList */
drh22827922000-06-06 17:27:05 +00002533 }
drh626a8792005-01-17 22:08:19 +00002534 return 1;
drh22827922000-06-06 17:27:05 +00002535 }
2536 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00002537 /* The pNC->nDepth==0 test causes aggregate functions in subqueries
2538 ** to be ignored */
danielk1977a58fdfb2005-02-08 07:50:40 +00002539 if( pNC->nDepth==0 ){
drh13449892005-09-07 21:22:45 +00002540 /* Check to see if pExpr is a duplicate of another aggregate
2541 ** function that is already in the pAggInfo structure
2542 */
2543 struct AggInfo_func *pItem = pAggInfo->aFunc;
2544 for(i=0; i<pAggInfo->nFunc; i++, pItem++){
2545 if( sqlite3ExprCompare(pItem->pExpr, pExpr) ){
danielk1977a58fdfb2005-02-08 07:50:40 +00002546 break;
2547 }
drh22827922000-06-06 17:27:05 +00002548 }
drh13449892005-09-07 21:22:45 +00002549 if( i>=pAggInfo->nFunc ){
2550 /* pExpr is original. Make a new entry in pAggInfo->aFunc[]
2551 */
danielk197714db2662006-01-09 16:12:04 +00002552 u8 enc = ENC(pParse->db);
danielk19771e536952007-08-16 10:09:01 +00002553 i = addAggInfoFunc(pParse->db, pAggInfo);
drh13449892005-09-07 21:22:45 +00002554 if( i>=0 ){
2555 pItem = &pAggInfo->aFunc[i];
2556 pItem->pExpr = pExpr;
2557 pItem->iMem = pParse->nMem++;
2558 pItem->pFunc = sqlite3FindFunction(pParse->db,
drh2646da72005-12-09 20:02:05 +00002559 (char*)pExpr->token.z, pExpr->token.n,
drh13449892005-09-07 21:22:45 +00002560 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
drhfd357972005-09-09 01:33:19 +00002561 if( pExpr->flags & EP_Distinct ){
2562 pItem->iDistinct = pParse->nTab++;
2563 }else{
2564 pItem->iDistinct = -1;
2565 }
drh13449892005-09-07 21:22:45 +00002566 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002567 }
drh13449892005-09-07 21:22:45 +00002568 /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
2569 */
danielk1977a58fdfb2005-02-08 07:50:40 +00002570 pExpr->iAgg = i;
drh13449892005-09-07 21:22:45 +00002571 pExpr->pAggInfo = pAggInfo;
danielk1977a58fdfb2005-02-08 07:50:40 +00002572 return 1;
drh22827922000-06-06 17:27:05 +00002573 }
drh22827922000-06-06 17:27:05 +00002574 }
2575 }
drh13449892005-09-07 21:22:45 +00002576
2577 /* Recursively walk subqueries looking for TK_COLUMN nodes that need
2578 ** to be changed to TK_AGG_COLUMN. But increment nDepth so that
2579 ** TK_AGG_FUNCTION nodes in subqueries will be unchanged.
2580 */
danielk1977a58fdfb2005-02-08 07:50:40 +00002581 if( pExpr->pSelect ){
2582 pNC->nDepth++;
2583 walkSelectExpr(pExpr->pSelect, analyzeAggregate, pNC);
2584 pNC->nDepth--;
2585 }
drh626a8792005-01-17 22:08:19 +00002586 return 0;
2587}
2588
2589/*
2590** Analyze the given expression looking for aggregate functions and
2591** for variables that need to be added to the pParse->aAgg[] array.
2592** Make additional entries to the pParse->aAgg[] array as necessary.
2593**
2594** This routine should only be called after the expression has been
2595** analyzed by sqlite3ExprResolveNames().
2596**
2597** If errors are seen, leave an error message in zErrMsg and return
2598** the number of errors.
2599*/
danielk1977a58fdfb2005-02-08 07:50:40 +00002600int sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
2601 int nErr = pNC->pParse->nErr;
2602 walkExprTree(pExpr, analyzeAggregate, pNC);
2603 return pNC->pParse->nErr - nErr;
drh22827922000-06-06 17:27:05 +00002604}
drh5d9a4af2005-08-30 00:54:01 +00002605
2606/*
2607** Call sqlite3ExprAnalyzeAggregates() for every expression in an
2608** expression list. Return the number of errors.
2609**
2610** If an error is found, the analysis is cut short.
2611*/
2612int sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){
2613 struct ExprList_item *pItem;
2614 int i;
2615 int nErr = 0;
2616 if( pList ){
2617 for(pItem=pList->a, i=0; nErr==0 && i<pList->nExpr; i++, pItem++){
2618 nErr += sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
2619 }
2620 }
2621 return nErr;
2622}