blob: 1d1fb0e18e4b35f7955efe56f808a26c7fd86aef [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**
drhf3a65f72007-08-22 20:18:21 +000015** $Id: expr.c,v 1.308 2007/08/22 20:18:22 drh Exp $
drhcce7d172000-05-31 15:34:51 +000016*/
17#include "sqliteInt.h"
drh04738cb2002-06-02 18:19:00 +000018#include <ctype.h>
drha2e00042002-01-22 03:13:42 +000019
danielk1977e014a832004-05-17 10:48:57 +000020/*
21** Return the 'affinity' of the expression pExpr if any.
22**
23** If pExpr is a column, a reference to a column via an 'AS' alias,
24** or a sub-select with a column as the return value, then the
25** affinity of that column is returned. Otherwise, 0x00 is returned,
26** indicating no affinity for the expression.
27**
28** i.e. the WHERE clause expresssions in the following statements all
29** have an affinity:
30**
31** CREATE TABLE t1(a);
32** SELECT * FROM t1 WHERE a;
33** SELECT a AS b FROM t1 WHERE b;
34** SELECT * FROM t1 WHERE (select a from t1);
35*/
danielk1977bf3b7212004-05-18 10:06:24 +000036char sqlite3ExprAffinity(Expr *pExpr){
drh487e2622005-06-25 18:42:14 +000037 int op = pExpr->op;
drh487e2622005-06-25 18:42:14 +000038 if( op==TK_SELECT ){
danielk1977bf3b7212004-05-18 10:06:24 +000039 return sqlite3ExprAffinity(pExpr->pSelect->pEList->a[0].pExpr);
danielk1977a37cdde2004-05-16 11:15:36 +000040 }
drh487e2622005-06-25 18:42:14 +000041#ifndef SQLITE_OMIT_CAST
42 if( op==TK_CAST ){
drh8a512562005-11-14 22:29:05 +000043 return sqlite3AffinityType(&pExpr->token);
drh487e2622005-06-25 18:42:14 +000044 }
45#endif
danielk1977a37cdde2004-05-16 11:15:36 +000046 return pExpr->affinity;
47}
48
drh53db1452004-05-20 13:54:53 +000049/*
drh8b4c40d2007-02-01 23:02:45 +000050** Set the collating sequence for expression pExpr to be the collating
51** sequence named by pToken. Return a pointer to the revised expression.
drha34001c2007-02-02 12:44:37 +000052** The collating sequence is marked as "explicit" using the EP_ExpCollate
53** flag. An explicit collating sequence will override implicit
54** collating sequences.
drh8b4c40d2007-02-01 23:02:45 +000055*/
56Expr *sqlite3ExprSetColl(Parse *pParse, Expr *pExpr, Token *pName){
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 ){
drh17435752007-08-16 04:30:38 +0000278 pParse->db->mallocFailed = 1;
drh206f3d92006-07-11 13:15:08 +0000279 }
280 return pNew;
281}
282
283/*
drh4e0cff62004-11-05 05:10:28 +0000284** When doing a nested parse, you can include terms in an expression
285** that look like this: #0 #1 #2 ... These terms refer to elements
drh288d37f2005-06-22 08:48:06 +0000286** on the stack. "#0" means the top of the stack.
287** "#1" means the next down on the stack. And so forth.
drh4e0cff62004-11-05 05:10:28 +0000288**
289** This routine is called by the parser to deal with on of those terms.
290** It immediately generates code to store the value in a memory location.
291** The returns an expression that will code to extract the value from
292** that memory location as needed.
293*/
294Expr *sqlite3RegisterExpr(Parse *pParse, Token *pToken){
295 Vdbe *v = pParse->pVdbe;
296 Expr *p;
297 int depth;
drh4e0cff62004-11-05 05:10:28 +0000298 if( pParse->nested==0 ){
299 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", pToken);
drh4e05c832007-05-11 01:44:50 +0000300 return sqlite3Expr(TK_NULL, 0, 0, 0);
drh4e0cff62004-11-05 05:10:28 +0000301 }
drhbb7ac002005-08-12 22:58:53 +0000302 if( v==0 ) return 0;
drh4e0cff62004-11-05 05:10:28 +0000303 p = sqlite3Expr(TK_REGISTER, 0, 0, pToken);
drh73c42a12004-11-20 18:13:10 +0000304 if( p==0 ){
drh17435752007-08-16 04:30:38 +0000305 pParse->db->mallocFailed = 1;
drh73c42a12004-11-20 18:13:10 +0000306 return 0; /* Malloc failed */
307 }
drh2646da72005-12-09 20:02:05 +0000308 depth = atoi((char*)&pToken->z[1]);
drh288d37f2005-06-22 08:48:06 +0000309 p->iTable = pParse->nMem++;
310 sqlite3VdbeAddOp(v, OP_Dup, depth, 0);
311 sqlite3VdbeAddOp(v, OP_MemStore, p->iTable, 1);
drh4e0cff62004-11-05 05:10:28 +0000312 return p;
313}
314
315/*
drh91bb0ee2004-09-01 03:06:34 +0000316** Join two expressions using an AND operator. If either expression is
317** NULL, then just return the other expression.
318*/
danielk19771e536952007-08-16 10:09:01 +0000319Expr *sqlite3ExprAnd(sqlite3 *db, Expr *pLeft, Expr *pRight){
drh91bb0ee2004-09-01 03:06:34 +0000320 if( pLeft==0 ){
321 return pRight;
322 }else if( pRight==0 ){
323 return pLeft;
324 }else{
drh17435752007-08-16 04:30:38 +0000325 Expr *p = sqlite3Expr(TK_AND, pLeft, pRight, 0);
326 if( p==0 ){
327 db->mallocFailed = 1;
328 }
danielk19771e536952007-08-16 10:09:01 +0000329 return p;
drh91bb0ee2004-09-01 03:06:34 +0000330 }
331}
332
333/*
drh6977fea2002-10-22 23:38:04 +0000334** Set the Expr.span field of the given expression to span all
drha76b5df2002-02-23 02:32:10 +0000335** text between the two given tokens.
336*/
danielk19774adee202004-05-08 08:23:19 +0000337void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
drh4efc4752004-01-16 15:55:37 +0000338 assert( pRight!=0 );
339 assert( pLeft!=0 );
drhf3a65f72007-08-22 20:18:21 +0000340 if( pExpr && pRight->z && pLeft->z ){
drhad6d9462004-09-19 02:15:24 +0000341 assert( pLeft->dyn==0 || pLeft->z[pLeft->n]==0 );
drh145716b2004-09-24 12:24:06 +0000342 if( pLeft->dyn==0 && pRight->dyn==0 ){
drh6977fea2002-10-22 23:38:04 +0000343 pExpr->span.z = pLeft->z;
drh97903fe2005-05-24 20:19:57 +0000344 pExpr->span.n = pRight->n + (pRight->z - pLeft->z);
drh4b59ab52002-08-24 18:24:51 +0000345 }else{
drh6977fea2002-10-22 23:38:04 +0000346 pExpr->span.z = 0;
drh4b59ab52002-08-24 18:24:51 +0000347 }
drha76b5df2002-02-23 02:32:10 +0000348 }
349}
350
351/*
352** Construct a new expression node for a function with multiple
353** arguments.
354*/
drh17435752007-08-16 04:30:38 +0000355Expr *sqlite3ExprFunction(Parse *pParse, ExprList *pList, Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000356 Expr *pNew;
danielk19774b202ae2006-01-23 05:50:58 +0000357 assert( pToken );
drh17435752007-08-16 04:30:38 +0000358 pNew = sqlite3DbMallocZero(pParse->db, sizeof(Expr) );
drha76b5df2002-02-23 02:32:10 +0000359 if( pNew==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000360 sqlite3ExprListDelete(pList); /* Avoid leaking memory when malloc fails */
drha76b5df2002-02-23 02:32:10 +0000361 return 0;
362 }
363 pNew->op = TK_FUNCTION;
364 pNew->pList = pList;
danielk19774b202ae2006-01-23 05:50:58 +0000365 assert( pToken->dyn==0 );
366 pNew->token = *pToken;
drh6977fea2002-10-22 23:38:04 +0000367 pNew->span = pNew->token;
danielk1977fc976062007-05-10 10:46:56 +0000368
369 sqlite3ExprSetHeight(pNew);
drha76b5df2002-02-23 02:32:10 +0000370 return pNew;
371}
372
373/*
drhfa6bc002004-09-07 16:19:52 +0000374** Assign a variable number to an expression that encodes a wildcard
375** in the original SQL statement.
376**
377** Wildcards consisting of a single "?" are assigned the next sequential
378** variable number.
379**
380** Wildcards of the form "?nnn" are assigned the number "nnn". We make
381** sure "nnn" is not too be to avoid a denial of service attack when
382** the SQL statement comes from an external source.
383**
384** Wildcards of the form ":aaa" or "$aaa" are assigned the same number
385** as the previous instance of the same wildcard. Or if this is the first
386** instance of the wildcard, the next sequenial variable number is
387** assigned.
388*/
389void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){
390 Token *pToken;
drh17435752007-08-16 04:30:38 +0000391 sqlite3 *db = pParse->db;
392
drhfa6bc002004-09-07 16:19:52 +0000393 if( pExpr==0 ) return;
394 pToken = &pExpr->token;
395 assert( pToken->n>=1 );
396 assert( pToken->z!=0 );
397 assert( pToken->z[0]!=0 );
398 if( pToken->n==1 ){
399 /* Wildcard of the form "?". Assign the next variable number */
400 pExpr->iTable = ++pParse->nVar;
401 }else if( pToken->z[0]=='?' ){
402 /* Wildcard of the form "?nnn". Convert "nnn" to an integer and
403 ** use it as the variable number */
404 int i;
drh2646da72005-12-09 20:02:05 +0000405 pExpr->iTable = i = atoi((char*)&pToken->z[1]);
drhfa6bc002004-09-07 16:19:52 +0000406 if( i<1 || i>SQLITE_MAX_VARIABLE_NUMBER ){
407 sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
408 SQLITE_MAX_VARIABLE_NUMBER);
409 }
410 if( i>pParse->nVar ){
411 pParse->nVar = i;
412 }
413 }else{
414 /* Wildcards of the form ":aaa" or "$aaa". Reuse the same variable
415 ** number as the prior appearance of the same name, or if the name
416 ** has never appeared before, reuse the same variable number
417 */
418 int i, n;
419 n = pToken->n;
420 for(i=0; i<pParse->nVarExpr; i++){
421 Expr *pE;
422 if( (pE = pParse->apVarExpr[i])!=0
423 && pE->token.n==n
424 && memcmp(pE->token.z, pToken->z, n)==0 ){
425 pExpr->iTable = pE->iTable;
426 break;
427 }
428 }
429 if( i>=pParse->nVarExpr ){
430 pExpr->iTable = ++pParse->nVar;
431 if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){
432 pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10;
drh17435752007-08-16 04:30:38 +0000433 pParse->apVarExpr =
434 sqlite3DbReallocOrFree(
435 db,
436 pParse->apVarExpr,
437 pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0])
438 );
drhfa6bc002004-09-07 16:19:52 +0000439 }
drh17435752007-08-16 04:30:38 +0000440 if( !db->mallocFailed ){
drhfa6bc002004-09-07 16:19:52 +0000441 assert( pParse->apVarExpr!=0 );
442 pParse->apVarExpr[pParse->nVarExpr++] = pExpr;
443 }
444 }
445 }
danielk1977832b2662007-05-09 11:37:22 +0000446 if( !pParse->nErr && pParse->nVar>SQLITE_MAX_VARIABLE_NUMBER ){
447 sqlite3ErrorMsg(pParse, "too many SQL variables");
448 }
drhfa6bc002004-09-07 16:19:52 +0000449}
450
451/*
drha2e00042002-01-22 03:13:42 +0000452** Recursively delete an expression tree.
453*/
danielk19774adee202004-05-08 08:23:19 +0000454void sqlite3ExprDelete(Expr *p){
drha2e00042002-01-22 03:13:42 +0000455 if( p==0 ) return;
drh17435752007-08-16 04:30:38 +0000456 if( p->span.dyn ) sqlite3_free((char*)p->span.z);
457 if( p->token.dyn ) sqlite3_free((char*)p->token.z);
danielk19774adee202004-05-08 08:23:19 +0000458 sqlite3ExprDelete(p->pLeft);
459 sqlite3ExprDelete(p->pRight);
460 sqlite3ExprListDelete(p->pList);
461 sqlite3SelectDelete(p->pSelect);
drh17435752007-08-16 04:30:38 +0000462 sqlite3_free(p);
drha2e00042002-01-22 03:13:42 +0000463}
464
drhd2687b72005-08-12 22:56:09 +0000465/*
466** The Expr.token field might be a string literal that is quoted.
467** If so, remove the quotation marks.
468*/
drh17435752007-08-16 04:30:38 +0000469void sqlite3DequoteExpr(sqlite3 *db, Expr *p){
drhd2687b72005-08-12 22:56:09 +0000470 if( ExprHasAnyProperty(p, EP_Dequoted) ){
471 return;
472 }
473 ExprSetProperty(p, EP_Dequoted);
474 if( p->token.dyn==0 ){
drh17435752007-08-16 04:30:38 +0000475 sqlite3TokenCopy(db, &p->token, &p->token);
drhd2687b72005-08-12 22:56:09 +0000476 }
477 sqlite3Dequote((char*)p->token.z);
478}
479
drha76b5df2002-02-23 02:32:10 +0000480
481/*
drhff78bd22002-02-27 01:47:11 +0000482** The following group of routines make deep copies of expressions,
483** expression lists, ID lists, and select statements. The copies can
484** be deleted (by being passed to their respective ...Delete() routines)
485** without effecting the originals.
486**
danielk19774adee202004-05-08 08:23:19 +0000487** The expression list, ID, and source lists return by sqlite3ExprListDup(),
488** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
drhad3cab52002-05-24 02:04:32 +0000489** by subsequent calls to sqlite*ListAppend() routines.
drhff78bd22002-02-27 01:47:11 +0000490**
drhad3cab52002-05-24 02:04:32 +0000491** Any tables that the SrcList might point to are not duplicated.
drhff78bd22002-02-27 01:47:11 +0000492*/
danielk19771e536952007-08-16 10:09:01 +0000493Expr *sqlite3ExprDup(sqlite3 *db, Expr *p){
drhff78bd22002-02-27 01:47:11 +0000494 Expr *pNew;
495 if( p==0 ) return 0;
drh17435752007-08-16 04:30:38 +0000496 pNew = sqlite3DbMallocRaw(db, sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000497 if( pNew==0 ) return 0;
drh3b167c72002-06-28 12:18:47 +0000498 memcpy(pNew, p, sizeof(*pNew));
drh6977fea2002-10-22 23:38:04 +0000499 if( p->token.z!=0 ){
drh17435752007-08-16 04:30:38 +0000500 pNew->token.z = (u8*)sqlite3DbStrNDup(db, (char*)p->token.z, p->token.n);
drh4b59ab52002-08-24 18:24:51 +0000501 pNew->token.dyn = 1;
502 }else{
drh4efc4752004-01-16 15:55:37 +0000503 assert( pNew->token.z==0 );
drh4b59ab52002-08-24 18:24:51 +0000504 }
drh6977fea2002-10-22 23:38:04 +0000505 pNew->span.z = 0;
drh17435752007-08-16 04:30:38 +0000506 pNew->pLeft = sqlite3ExprDup(db, p->pLeft);
507 pNew->pRight = sqlite3ExprDup(db, p->pRight);
508 pNew->pList = sqlite3ExprListDup(db, p->pList);
509 pNew->pSelect = sqlite3SelectDup(db, p->pSelect);
drhff78bd22002-02-27 01:47:11 +0000510 return pNew;
511}
drh17435752007-08-16 04:30:38 +0000512void sqlite3TokenCopy(sqlite3 *db, Token *pTo, Token *pFrom){
513 if( pTo->dyn ) sqlite3_free((char*)pTo->z);
drh4b59ab52002-08-24 18:24:51 +0000514 if( pFrom->z ){
515 pTo->n = pFrom->n;
drh17435752007-08-16 04:30:38 +0000516 pTo->z = (u8*)sqlite3DbStrNDup(db, (char*)pFrom->z, pFrom->n);
drh4b59ab52002-08-24 18:24:51 +0000517 pTo->dyn = 1;
518 }else{
drh4b59ab52002-08-24 18:24:51 +0000519 pTo->z = 0;
drh4b59ab52002-08-24 18:24:51 +0000520 }
521}
drh17435752007-08-16 04:30:38 +0000522ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p){
drhff78bd22002-02-27 01:47:11 +0000523 ExprList *pNew;
drh145716b2004-09-24 12:24:06 +0000524 struct ExprList_item *pItem, *pOldItem;
drhff78bd22002-02-27 01:47:11 +0000525 int i;
526 if( p==0 ) return 0;
drh17435752007-08-16 04:30:38 +0000527 pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +0000528 if( pNew==0 ) return 0;
danielk197731dad9d2007-08-16 11:36:15 +0000529 pNew->iECursor = 0;
drh4305d102003-07-30 12:34:12 +0000530 pNew->nExpr = pNew->nAlloc = p->nExpr;
drh17435752007-08-16 04:30:38 +0000531 pNew->a = pItem = sqlite3DbMallocRaw(db, p->nExpr*sizeof(p->a[0]) );
danielk1977e0048402004-06-15 16:51:01 +0000532 if( pItem==0 ){
drh17435752007-08-16 04:30:38 +0000533 sqlite3_free(pNew);
danielk1977e0048402004-06-15 16:51:01 +0000534 return 0;
535 }
drh145716b2004-09-24 12:24:06 +0000536 pOldItem = p->a;
537 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
drh4b59ab52002-08-24 18:24:51 +0000538 Expr *pNewExpr, *pOldExpr;
drh17435752007-08-16 04:30:38 +0000539 pItem->pExpr = pNewExpr = sqlite3ExprDup(db, pOldExpr = pOldItem->pExpr);
drh6977fea2002-10-22 23:38:04 +0000540 if( pOldExpr->span.z!=0 && pNewExpr ){
541 /* Always make a copy of the span for top-level expressions in the
drh4b59ab52002-08-24 18:24:51 +0000542 ** expression list. The logic in SELECT processing that determines
543 ** the names of columns in the result set needs this information */
drh17435752007-08-16 04:30:38 +0000544 sqlite3TokenCopy(db, &pNewExpr->span, &pOldExpr->span);
drh4b59ab52002-08-24 18:24:51 +0000545 }
drh1f3e9052002-10-31 00:09:39 +0000546 assert( pNewExpr==0 || pNewExpr->span.z!=0
drh6f7adc82006-01-11 21:41:20 +0000547 || pOldExpr->span.z==0
drh17435752007-08-16 04:30:38 +0000548 || db->mallocFailed );
549 pItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
drh145716b2004-09-24 12:24:06 +0000550 pItem->sortOrder = pOldItem->sortOrder;
551 pItem->isAgg = pOldItem->isAgg;
drh3e7bc9c2004-02-21 19:17:17 +0000552 pItem->done = 0;
drhff78bd22002-02-27 01:47:11 +0000553 }
554 return pNew;
555}
danielk197793758c82005-01-21 08:13:14 +0000556
557/*
558** If cursors, triggers, views and subqueries are all omitted from
559** the build, then none of the following routines, except for
560** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes
561** called with a NULL argument.
562*/
danielk19776a67fe82005-02-04 04:07:16 +0000563#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \
564 || !defined(SQLITE_OMIT_SUBQUERY)
drh17435752007-08-16 04:30:38 +0000565SrcList *sqlite3SrcListDup(sqlite3 *db, SrcList *p){
drhad3cab52002-05-24 02:04:32 +0000566 SrcList *pNew;
567 int i;
drh113088e2003-03-20 01:16:58 +0000568 int nByte;
drhad3cab52002-05-24 02:04:32 +0000569 if( p==0 ) return 0;
drh113088e2003-03-20 01:16:58 +0000570 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
drh17435752007-08-16 04:30:38 +0000571 pNew = sqlite3DbMallocRaw(db, nByte );
drhad3cab52002-05-24 02:04:32 +0000572 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000573 pNew->nSrc = pNew->nAlloc = p->nSrc;
drhad3cab52002-05-24 02:04:32 +0000574 for(i=0; i<p->nSrc; i++){
drh4efc4752004-01-16 15:55:37 +0000575 struct SrcList_item *pNewItem = &pNew->a[i];
576 struct SrcList_item *pOldItem = &p->a[i];
drhed8a3bb2005-06-06 21:19:56 +0000577 Table *pTab;
drh17435752007-08-16 04:30:38 +0000578 pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase);
579 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
580 pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias);
drh4efc4752004-01-16 15:55:37 +0000581 pNewItem->jointype = pOldItem->jointype;
582 pNewItem->iCursor = pOldItem->iCursor;
danielk19771787cca2006-02-10 07:07:14 +0000583 pNewItem->isPopulated = pOldItem->isPopulated;
drhed8a3bb2005-06-06 21:19:56 +0000584 pTab = pNewItem->pTab = pOldItem->pTab;
585 if( pTab ){
586 pTab->nRef++;
danielk1977a1cb1832005-02-12 08:59:55 +0000587 }
drh17435752007-08-16 04:30:38 +0000588 pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect);
589 pNewItem->pOn = sqlite3ExprDup(db, pOldItem->pOn);
590 pNewItem->pUsing = sqlite3IdListDup(db, pOldItem->pUsing);
danielk19776c18b6e2005-01-30 09:17:58 +0000591 pNewItem->colUsed = pOldItem->colUsed;
drhad3cab52002-05-24 02:04:32 +0000592 }
593 return pNew;
594}
drh17435752007-08-16 04:30:38 +0000595IdList *sqlite3IdListDup(sqlite3 *db, IdList *p){
drhff78bd22002-02-27 01:47:11 +0000596 IdList *pNew;
597 int i;
598 if( p==0 ) return 0;
drh17435752007-08-16 04:30:38 +0000599 pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +0000600 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000601 pNew->nId = pNew->nAlloc = p->nId;
drh17435752007-08-16 04:30:38 +0000602 pNew->a = sqlite3DbMallocRaw(db, p->nId*sizeof(p->a[0]) );
danielk1977d5d56522005-03-16 12:15:20 +0000603 if( pNew->a==0 ){
drh17435752007-08-16 04:30:38 +0000604 sqlite3_free(pNew);
danielk1977d5d56522005-03-16 12:15:20 +0000605 return 0;
606 }
drhff78bd22002-02-27 01:47:11 +0000607 for(i=0; i<p->nId; i++){
drh4efc4752004-01-16 15:55:37 +0000608 struct IdList_item *pNewItem = &pNew->a[i];
609 struct IdList_item *pOldItem = &p->a[i];
drh17435752007-08-16 04:30:38 +0000610 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
drh4efc4752004-01-16 15:55:37 +0000611 pNewItem->idx = pOldItem->idx;
drhff78bd22002-02-27 01:47:11 +0000612 }
613 return pNew;
614}
drh17435752007-08-16 04:30:38 +0000615Select *sqlite3SelectDup(sqlite3 *db, Select *p){
drhff78bd22002-02-27 01:47:11 +0000616 Select *pNew;
617 if( p==0 ) return 0;
drh17435752007-08-16 04:30:38 +0000618 pNew = sqlite3DbMallocRaw(db, sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000619 if( pNew==0 ) return 0;
620 pNew->isDistinct = p->isDistinct;
drh17435752007-08-16 04:30:38 +0000621 pNew->pEList = sqlite3ExprListDup(db, p->pEList);
622 pNew->pSrc = sqlite3SrcListDup(db, p->pSrc);
623 pNew->pWhere = sqlite3ExprDup(db, p->pWhere);
624 pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy);
625 pNew->pHaving = sqlite3ExprDup(db, p->pHaving);
626 pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy);
drhff78bd22002-02-27 01:47:11 +0000627 pNew->op = p->op;
drh17435752007-08-16 04:30:38 +0000628 pNew->pPrior = sqlite3SelectDup(db, p->pPrior);
629 pNew->pLimit = sqlite3ExprDup(db, p->pLimit);
630 pNew->pOffset = sqlite3ExprDup(db, p->pOffset);
drh7b58dae2003-07-20 01:16:46 +0000631 pNew->iLimit = -1;
632 pNew->iOffset = -1;
danielk1977a1cb1832005-02-12 08:59:55 +0000633 pNew->isResolved = p->isResolved;
634 pNew->isAgg = p->isAgg;
drhb9bb7c12006-06-11 23:41:55 +0000635 pNew->usesEphm = 0;
drh8e647b82005-09-23 21:11:53 +0000636 pNew->disallowOrderBy = 0;
drh0342b1f2005-09-01 03:07:44 +0000637 pNew->pRightmost = 0;
drhb9bb7c12006-06-11 23:41:55 +0000638 pNew->addrOpenEphm[0] = -1;
639 pNew->addrOpenEphm[1] = -1;
640 pNew->addrOpenEphm[2] = -1;
drhff78bd22002-02-27 01:47:11 +0000641 return pNew;
642}
danielk197793758c82005-01-21 08:13:14 +0000643#else
drh17435752007-08-16 04:30:38 +0000644Select *sqlite3SelectDup(sqlite3 *db, Select *p){
danielk197793758c82005-01-21 08:13:14 +0000645 assert( p==0 );
646 return 0;
647}
648#endif
drhff78bd22002-02-27 01:47:11 +0000649
650
651/*
drha76b5df2002-02-23 02:32:10 +0000652** Add a new element to the end of an expression list. If pList is
653** initially NULL, then create a new expression list.
654*/
drh17435752007-08-16 04:30:38 +0000655ExprList *sqlite3ExprListAppend(
656 Parse *pParse, /* Parsing context */
657 ExprList *pList, /* List to which to append. Might be NULL */
658 Expr *pExpr, /* Expression to be appended */
659 Token *pName /* AS keyword for the expression */
660){
661 sqlite3 *db = pParse->db;
drha76b5df2002-02-23 02:32:10 +0000662 if( pList==0 ){
drh17435752007-08-16 04:30:38 +0000663 pList = sqlite3DbMallocZero(db, sizeof(ExprList) );
drha76b5df2002-02-23 02:32:10 +0000664 if( pList==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000665 goto no_mem;
drha76b5df2002-02-23 02:32:10 +0000666 }
drh4efc4752004-01-16 15:55:37 +0000667 assert( pList->nAlloc==0 );
drha76b5df2002-02-23 02:32:10 +0000668 }
drh4305d102003-07-30 12:34:12 +0000669 if( pList->nAlloc<=pList->nExpr ){
danielk1977d5d56522005-03-16 12:15:20 +0000670 struct ExprList_item *a;
671 int n = pList->nAlloc*2 + 4;
drh17435752007-08-16 04:30:38 +0000672 a = sqlite3_realloc(pList->a, n*sizeof(pList->a[0]));
danielk1977d5d56522005-03-16 12:15:20 +0000673 if( a==0 ){
674 goto no_mem;
drha76b5df2002-02-23 02:32:10 +0000675 }
danielk1977d5d56522005-03-16 12:15:20 +0000676 pList->a = a;
677 pList->nAlloc = n;
drha76b5df2002-02-23 02:32:10 +0000678 }
drh4efc4752004-01-16 15:55:37 +0000679 assert( pList->a!=0 );
680 if( pExpr || pName ){
681 struct ExprList_item *pItem = &pList->a[pList->nExpr++];
682 memset(pItem, 0, sizeof(*pItem));
drh17435752007-08-16 04:30:38 +0000683 pItem->zName = sqlite3NameFromToken(db, pName);
danielk1977e94ddc92005-03-21 03:53:38 +0000684 pItem->pExpr = pExpr;
drha76b5df2002-02-23 02:32:10 +0000685 }
686 return pList;
danielk1977d5d56522005-03-16 12:15:20 +0000687
688no_mem:
689 /* Avoid leaking memory if malloc has failed. */
drh17435752007-08-16 04:30:38 +0000690 db->mallocFailed = 1;
danielk1977d5d56522005-03-16 12:15:20 +0000691 sqlite3ExprDelete(pExpr);
692 sqlite3ExprListDelete(pList);
693 return 0;
drha76b5df2002-02-23 02:32:10 +0000694}
695
696/*
danielk19777a15a4b2007-05-08 17:54:43 +0000697** If the expression list pEList contains more than iLimit elements,
698** leave an error message in pParse.
699*/
700void sqlite3ExprListCheckLength(
701 Parse *pParse,
702 ExprList *pEList,
703 int iLimit,
704 const char *zObject
705){
danielk1977b4fc6792007-05-08 18:04:46 +0000706 if( pEList && pEList->nExpr>iLimit ){
danielk19777a15a4b2007-05-08 17:54:43 +0000707 sqlite3ErrorMsg(pParse, "too many columns in %s", zObject);
708 }
709}
710
danielk1977fc976062007-05-10 10:46:56 +0000711
712#if SQLITE_MAX_EXPR_DEPTH>0
713/* The following three functions, heightOfExpr(), heightOfExprList()
714** and heightOfSelect(), are used to determine the maximum height
715** of any expression tree referenced by the structure passed as the
716** first argument.
717**
718** If this maximum height is greater than the current value pointed
719** to by pnHeight, the second parameter, then set *pnHeight to that
720** value.
721*/
722static void heightOfExpr(Expr *p, int *pnHeight){
723 if( p ){
724 if( p->nHeight>*pnHeight ){
725 *pnHeight = p->nHeight;
726 }
727 }
728}
729static void heightOfExprList(ExprList *p, int *pnHeight){
730 if( p ){
731 int i;
732 for(i=0; i<p->nExpr; i++){
733 heightOfExpr(p->a[i].pExpr, pnHeight);
734 }
735 }
736}
737static void heightOfSelect(Select *p, int *pnHeight){
738 if( p ){
739 heightOfExpr(p->pWhere, pnHeight);
740 heightOfExpr(p->pHaving, pnHeight);
741 heightOfExpr(p->pLimit, pnHeight);
742 heightOfExpr(p->pOffset, pnHeight);
743 heightOfExprList(p->pEList, pnHeight);
744 heightOfExprList(p->pGroupBy, pnHeight);
745 heightOfExprList(p->pOrderBy, pnHeight);
746 heightOfSelect(p->pPrior, pnHeight);
747 }
748}
749
750/*
751** Set the Expr.nHeight variable in the structure passed as an
752** argument. An expression with no children, Expr.pList or
753** Expr.pSelect member has a height of 1. Any other expression
754** has a height equal to the maximum height of any other
755** referenced Expr plus one.
756*/
757void sqlite3ExprSetHeight(Expr *p){
758 int nHeight = 0;
759 heightOfExpr(p->pLeft, &nHeight);
760 heightOfExpr(p->pRight, &nHeight);
761 heightOfExprList(p->pList, &nHeight);
762 heightOfSelect(p->pSelect, &nHeight);
763 p->nHeight = nHeight + 1;
764}
765
766/*
767** Return the maximum height of any expression tree referenced
768** by the select statement passed as an argument.
769*/
770int sqlite3SelectExprHeight(Select *p){
771 int nHeight = 0;
772 heightOfSelect(p, &nHeight);
773 return nHeight;
774}
775#endif
776
danielk19777a15a4b2007-05-08 17:54:43 +0000777/*
drha76b5df2002-02-23 02:32:10 +0000778** Delete an entire expression list.
779*/
danielk19774adee202004-05-08 08:23:19 +0000780void sqlite3ExprListDelete(ExprList *pList){
drha76b5df2002-02-23 02:32:10 +0000781 int i;
drhbe5c89a2004-07-26 00:31:09 +0000782 struct ExprList_item *pItem;
drha76b5df2002-02-23 02:32:10 +0000783 if( pList==0 ) return;
drh1bdd9b52004-04-23 17:04:44 +0000784 assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
785 assert( pList->nExpr<=pList->nAlloc );
drhbe5c89a2004-07-26 00:31:09 +0000786 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
787 sqlite3ExprDelete(pItem->pExpr);
drh17435752007-08-16 04:30:38 +0000788 sqlite3_free(pItem->zName);
drha76b5df2002-02-23 02:32:10 +0000789 }
drh17435752007-08-16 04:30:38 +0000790 sqlite3_free(pList->a);
791 sqlite3_free(pList);
drha76b5df2002-02-23 02:32:10 +0000792}
793
794/*
drh626a8792005-01-17 22:08:19 +0000795** Walk an expression tree. Call xFunc for each node visited.
drh73b211a2005-01-18 04:00:42 +0000796**
drh626a8792005-01-17 22:08:19 +0000797** The return value from xFunc determines whether the tree walk continues.
798** 0 means continue walking the tree. 1 means do not walk children
799** of the current node but continue with siblings. 2 means abandon
800** the tree walk completely.
801**
802** The return value from this routine is 1 to abandon the tree walk
803** and 0 to continue.
drh87abf5c2005-08-25 12:45:04 +0000804**
805** NOTICE: This routine does *not* descend into subqueries.
drh626a8792005-01-17 22:08:19 +0000806*/
danielk1977a58fdfb2005-02-08 07:50:40 +0000807static int walkExprList(ExprList *, int (*)(void *, Expr*), void *);
drh626a8792005-01-17 22:08:19 +0000808static int walkExprTree(Expr *pExpr, int (*xFunc)(void*,Expr*), void *pArg){
drh626a8792005-01-17 22:08:19 +0000809 int rc;
810 if( pExpr==0 ) return 0;
811 rc = (*xFunc)(pArg, pExpr);
812 if( rc==0 ){
813 if( walkExprTree(pExpr->pLeft, xFunc, pArg) ) return 1;
814 if( walkExprTree(pExpr->pRight, xFunc, pArg) ) return 1;
danielk1977a58fdfb2005-02-08 07:50:40 +0000815 if( walkExprList(pExpr->pList, xFunc, pArg) ) return 1;
drh626a8792005-01-17 22:08:19 +0000816 }
817 return rc>1;
818}
819
820/*
danielk1977a58fdfb2005-02-08 07:50:40 +0000821** Call walkExprTree() for every expression in list p.
822*/
823static int walkExprList(ExprList *p, int (*xFunc)(void *, Expr*), void *pArg){
824 int i;
825 struct ExprList_item *pItem;
826 if( !p ) return 0;
827 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
828 if( walkExprTree(pItem->pExpr, xFunc, pArg) ) return 1;
829 }
830 return 0;
831}
832
833/*
834** Call walkExprTree() for every expression in Select p, not including
835** expressions that are part of sub-selects in any FROM clause or the LIMIT
836** or OFFSET expressions..
837*/
838static int walkSelectExpr(Select *p, int (*xFunc)(void *, Expr*), void *pArg){
839 walkExprList(p->pEList, xFunc, pArg);
840 walkExprTree(p->pWhere, xFunc, pArg);
841 walkExprList(p->pGroupBy, xFunc, pArg);
842 walkExprTree(p->pHaving, xFunc, pArg);
843 walkExprList(p->pOrderBy, xFunc, pArg);
danielk197715d79822007-05-15 07:00:34 +0000844 if( p->pPrior ){
845 walkSelectExpr(p->pPrior, xFunc, pArg);
846 }
danielk1977a58fdfb2005-02-08 07:50:40 +0000847 return 0;
848}
849
850
851/*
drh626a8792005-01-17 22:08:19 +0000852** This routine is designed as an xFunc for walkExprTree().
853**
854** pArg is really a pointer to an integer. If we can tell by looking
drh73b211a2005-01-18 04:00:42 +0000855** at pExpr that the expression that contains pExpr is not a constant
856** expression, then set *pArg to 0 and return 2 to abandon the tree walk.
857** If pExpr does does not disqualify the expression from being a constant
858** then do nothing.
859**
860** After walking the whole tree, if no nodes are found that disqualify
861** the expression as constant, then we assume the whole expression
862** is constant. See sqlite3ExprIsConstant() for additional information.
drh626a8792005-01-17 22:08:19 +0000863*/
864static int exprNodeIsConstant(void *pArg, Expr *pExpr){
drh0a168372007-06-08 00:20:47 +0000865 int *pN = (int*)pArg;
866
867 /* If *pArg is 3 then any term of the expression that comes from
868 ** the ON or USING clauses of a join disqualifies the expression
869 ** from being considered constant. */
870 if( (*pN)==3 && ExprHasAnyProperty(pExpr, EP_FromJoin) ){
871 *pN = 0;
872 return 2;
873 }
874
drh626a8792005-01-17 22:08:19 +0000875 switch( pExpr->op ){
drheb55bd22005-06-30 17:04:21 +0000876 /* Consider functions to be constant if all their arguments are constant
877 ** and *pArg==2 */
878 case TK_FUNCTION:
drh0a168372007-06-08 00:20:47 +0000879 if( (*pN)==2 ) return 0;
drheb55bd22005-06-30 17:04:21 +0000880 /* Fall through */
drh626a8792005-01-17 22:08:19 +0000881 case TK_ID:
882 case TK_COLUMN:
883 case TK_DOT:
884 case TK_AGG_FUNCTION:
drh13449892005-09-07 21:22:45 +0000885 case TK_AGG_COLUMN:
drhfe2093d2005-01-20 22:48:47 +0000886#ifndef SQLITE_OMIT_SUBQUERY
887 case TK_SELECT:
888 case TK_EXISTS:
889#endif
drh0a168372007-06-08 00:20:47 +0000890 *pN = 0;
drh626a8792005-01-17 22:08:19 +0000891 return 2;
drh87abf5c2005-08-25 12:45:04 +0000892 case TK_IN:
893 if( pExpr->pSelect ){
drh0a168372007-06-08 00:20:47 +0000894 *pN = 0;
drh87abf5c2005-08-25 12:45:04 +0000895 return 2;
896 }
drh626a8792005-01-17 22:08:19 +0000897 default:
898 return 0;
899 }
900}
901
902/*
drhfef52082000-06-06 01:50:43 +0000903** Walk an expression tree. Return 1 if the expression is constant
drheb55bd22005-06-30 17:04:21 +0000904** and 0 if it involves variables or function calls.
drh23989372002-05-21 13:43:04 +0000905**
906** For the purposes of this function, a double-quoted string (ex: "abc")
907** is considered a variable but a single-quoted string (ex: 'abc') is
908** a constant.
drhfef52082000-06-06 01:50:43 +0000909*/
danielk19774adee202004-05-08 08:23:19 +0000910int sqlite3ExprIsConstant(Expr *p){
drh626a8792005-01-17 22:08:19 +0000911 int isConst = 1;
912 walkExprTree(p, exprNodeIsConstant, &isConst);
913 return isConst;
drhfef52082000-06-06 01:50:43 +0000914}
915
916/*
drheb55bd22005-06-30 17:04:21 +0000917** Walk an expression tree. Return 1 if the expression is constant
drh0a168372007-06-08 00:20:47 +0000918** that does no originate from the ON or USING clauses of a join.
919** Return 0 if it involves variables or function calls or terms from
920** an ON or USING clause.
921*/
922int sqlite3ExprIsConstantNotJoin(Expr *p){
923 int isConst = 3;
924 walkExprTree(p, exprNodeIsConstant, &isConst);
925 return isConst!=0;
926}
927
928/*
929** Walk an expression tree. Return 1 if the expression is constant
drheb55bd22005-06-30 17:04:21 +0000930** or a function call with constant arguments. Return and 0 if there
931** are any variables.
932**
933** For the purposes of this function, a double-quoted string (ex: "abc")
934** is considered a variable but a single-quoted string (ex: 'abc') is
935** a constant.
936*/
937int sqlite3ExprIsConstantOrFunction(Expr *p){
938 int isConst = 2;
939 walkExprTree(p, exprNodeIsConstant, &isConst);
940 return isConst!=0;
941}
942
943/*
drh73b211a2005-01-18 04:00:42 +0000944** If the expression p codes a constant integer that is small enough
drh202b2df2004-01-06 01:13:46 +0000945** to fit in a 32-bit integer, return 1 and put the value of the integer
946** in *pValue. If the expression is not an integer or if it is too big
947** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
drhe4de1fe2002-06-02 16:09:01 +0000948*/
danielk19774adee202004-05-08 08:23:19 +0000949int sqlite3ExprIsInteger(Expr *p, int *pValue){
drhe4de1fe2002-06-02 16:09:01 +0000950 switch( p->op ){
951 case TK_INTEGER: {
drh2646da72005-12-09 20:02:05 +0000952 if( sqlite3GetInt32((char*)p->token.z, pValue) ){
drh202b2df2004-01-06 01:13:46 +0000953 return 1;
954 }
955 break;
drhe4de1fe2002-06-02 16:09:01 +0000956 }
drh4b59ab52002-08-24 18:24:51 +0000957 case TK_UPLUS: {
danielk19774adee202004-05-08 08:23:19 +0000958 return sqlite3ExprIsInteger(p->pLeft, pValue);
drh4b59ab52002-08-24 18:24:51 +0000959 }
drhe4de1fe2002-06-02 16:09:01 +0000960 case TK_UMINUS: {
961 int v;
danielk19774adee202004-05-08 08:23:19 +0000962 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
drhe4de1fe2002-06-02 16:09:01 +0000963 *pValue = -v;
964 return 1;
965 }
966 break;
967 }
968 default: break;
969 }
970 return 0;
971}
972
973/*
drhc4a3c772001-04-04 11:48:57 +0000974** Return TRUE if the given string is a row-id column name.
975*/
danielk19774adee202004-05-08 08:23:19 +0000976int sqlite3IsRowid(const char *z){
977 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
978 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
979 if( sqlite3StrICmp(z, "OID")==0 ) return 1;
drhc4a3c772001-04-04 11:48:57 +0000980 return 0;
981}
982
983/*
drh8141f612004-01-25 22:44:58 +0000984** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
985** that name in the set of source tables in pSrcList and make the pExpr
986** expression node refer back to that source column. The following changes
987** are made to pExpr:
988**
989** pExpr->iDb Set the index in db->aDb[] of the database holding
990** the table.
991** pExpr->iTable Set to the cursor number for the table obtained
992** from pSrcList.
993** pExpr->iColumn Set to the column number within the table.
drh8141f612004-01-25 22:44:58 +0000994** pExpr->op Set to TK_COLUMN.
995** pExpr->pLeft Any expression this points to is deleted
996** pExpr->pRight Any expression this points to is deleted.
997**
998** The pDbToken is the name of the database (the "X"). This value may be
999** NULL meaning that name is of the form Y.Z or Z. Any available database
1000** can be used. The pTableToken is the name of the table (the "Y"). This
1001** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it
1002** means that the form of the name is Z and that columns from any table
1003** can be used.
1004**
1005** If the name cannot be resolved unambiguously, leave an error message
1006** in pParse and return non-zero. Return zero on success.
1007*/
1008static int lookupName(
drhffe07b22005-11-03 00:41:17 +00001009 Parse *pParse, /* The parsing context */
drh8141f612004-01-25 22:44:58 +00001010 Token *pDbToken, /* Name of the database containing table, or NULL */
1011 Token *pTableToken, /* Name of table containing column, or NULL */
1012 Token *pColumnToken, /* Name of the column. */
drh626a8792005-01-17 22:08:19 +00001013 NameContext *pNC, /* The name context used to resolve the name */
drh8141f612004-01-25 22:44:58 +00001014 Expr *pExpr /* Make this EXPR node point to the selected column */
1015){
1016 char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */
1017 char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */
1018 char *zCol = 0; /* Name of the column. The "Z" */
1019 int i, j; /* Loop counters */
1020 int cnt = 0; /* Number of matching column names */
1021 int cntTab = 0; /* Number of matching table names */
drh626a8792005-01-17 22:08:19 +00001022 sqlite3 *db = pParse->db; /* The database */
drh51669862004-12-18 18:40:26 +00001023 struct SrcList_item *pItem; /* Use for looping over pSrcList items */
1024 struct SrcList_item *pMatch = 0; /* The matching pSrcList item */
drh73b211a2005-01-18 04:00:42 +00001025 NameContext *pTopNC = pNC; /* First namecontext in the list */
drh8141f612004-01-25 22:44:58 +00001026
1027 assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
drh17435752007-08-16 04:30:38 +00001028 zDb = sqlite3NameFromToken(db, pDbToken);
1029 zTab = sqlite3NameFromToken(db, pTableToken);
1030 zCol = sqlite3NameFromToken(db, pColumnToken);
1031 if( db->mallocFailed ){
danielk1977d5d56522005-03-16 12:15:20 +00001032 goto lookupname_end;
drh8141f612004-01-25 22:44:58 +00001033 }
drh8141f612004-01-25 22:44:58 +00001034
1035 pExpr->iTable = -1;
drh626a8792005-01-17 22:08:19 +00001036 while( pNC && cnt==0 ){
drhffe07b22005-11-03 00:41:17 +00001037 ExprList *pEList;
drh626a8792005-01-17 22:08:19 +00001038 SrcList *pSrcList = pNC->pSrcList;
drh8141f612004-01-25 22:44:58 +00001039
danielk1977b3bce662005-01-29 08:32:43 +00001040 if( pSrcList ){
1041 for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
drh43617e92006-03-06 20:55:46 +00001042 Table *pTab;
1043 int iDb;
danielk1977b3bce662005-01-29 08:32:43 +00001044 Column *pCol;
1045
drh43617e92006-03-06 20:55:46 +00001046 pTab = pItem->pTab;
1047 assert( pTab!=0 );
1048 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
danielk1977b3bce662005-01-29 08:32:43 +00001049 assert( pTab->nCol>0 );
1050 if( zTab ){
1051 if( pItem->zAlias ){
1052 char *zTabName = pItem->zAlias;
1053 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
1054 }else{
1055 char *zTabName = pTab->zName;
1056 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
danielk1977da184232006-01-05 11:34:32 +00001057 if( zDb!=0 && sqlite3StrICmp(db->aDb[iDb].zName, zDb)!=0 ){
danielk1977b3bce662005-01-29 08:32:43 +00001058 continue;
1059 }
drh626a8792005-01-17 22:08:19 +00001060 }
drh8141f612004-01-25 22:44:58 +00001061 }
danielk1977b3bce662005-01-29 08:32:43 +00001062 if( 0==(cntTab++) ){
drh626a8792005-01-17 22:08:19 +00001063 pExpr->iTable = pItem->iCursor;
danielk1977da184232006-01-05 11:34:32 +00001064 pExpr->pSchema = pTab->pSchema;
danielk1977b3bce662005-01-29 08:32:43 +00001065 pMatch = pItem;
1066 }
1067 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
1068 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
danielk1977b3bf5562006-01-10 17:58:23 +00001069 const char *zColl = pTab->aCol[j].zColl;
drh873fac02005-06-06 17:11:46 +00001070 IdList *pUsing;
danielk1977b3bce662005-01-29 08:32:43 +00001071 cnt++;
1072 pExpr->iTable = pItem->iCursor;
1073 pMatch = pItem;
danielk1977da184232006-01-05 11:34:32 +00001074 pExpr->pSchema = pTab->pSchema;
danielk1977b3bce662005-01-29 08:32:43 +00001075 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
1076 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
1077 pExpr->affinity = pTab->aCol[j].affinity;
drh8b4c40d2007-02-01 23:02:45 +00001078 if( (pExpr->flags & EP_ExpCollate)==0 ){
1079 pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0);
1080 }
drh61dfc312006-12-16 16:25:15 +00001081 if( i<pSrcList->nSrc-1 ){
1082 if( pItem[1].jointype & JT_NATURAL ){
1083 /* If this match occurred in the left table of a natural join,
1084 ** then skip the right table to avoid a duplicate match */
1085 pItem++;
1086 i++;
1087 }else if( (pUsing = pItem[1].pUsing)!=0 ){
1088 /* If this match occurs on a column that is in the USING clause
1089 ** of a join, skip the search of the right table of the join
1090 ** to avoid a duplicate match there. */
1091 int k;
1092 for(k=0; k<pUsing->nId; k++){
1093 if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ){
1094 pItem++;
1095 i++;
1096 break;
1097 }
drh873fac02005-06-06 17:11:46 +00001098 }
1099 }
1100 }
danielk1977b3bce662005-01-29 08:32:43 +00001101 break;
1102 }
drh8141f612004-01-25 22:44:58 +00001103 }
1104 }
1105 }
drh626a8792005-01-17 22:08:19 +00001106
1107#ifndef SQLITE_OMIT_TRIGGER
1108 /* If we have not already resolved the name, then maybe
1109 ** it is a new.* or old.* trigger argument reference
1110 */
1111 if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
1112 TriggerStack *pTriggerStack = pParse->trigStack;
1113 Table *pTab = 0;
1114 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){
1115 pExpr->iTable = pTriggerStack->newIdx;
1116 assert( pTriggerStack->pTab );
1117 pTab = pTriggerStack->pTab;
1118 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab)==0 ){
1119 pExpr->iTable = pTriggerStack->oldIdx;
1120 assert( pTriggerStack->pTab );
1121 pTab = pTriggerStack->pTab;
1122 }
1123
1124 if( pTab ){
danielk1977f0113002006-01-24 12:09:17 +00001125 int iCol;
drh626a8792005-01-17 22:08:19 +00001126 Column *pCol = pTab->aCol;
1127
danielk1977da184232006-01-05 11:34:32 +00001128 pExpr->pSchema = pTab->pSchema;
drh626a8792005-01-17 22:08:19 +00001129 cntTab++;
danielk1977f0113002006-01-24 12:09:17 +00001130 for(iCol=0; iCol < pTab->nCol; iCol++, pCol++) {
drh626a8792005-01-17 22:08:19 +00001131 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
danielk1977f0113002006-01-24 12:09:17 +00001132 const char *zColl = pTab->aCol[iCol].zColl;
drh626a8792005-01-17 22:08:19 +00001133 cnt++;
danielk1977f0113002006-01-24 12:09:17 +00001134 pExpr->iColumn = iCol==pTab->iPKey ? -1 : iCol;
1135 pExpr->affinity = pTab->aCol[iCol].affinity;
drh8b4c40d2007-02-01 23:02:45 +00001136 if( (pExpr->flags & EP_ExpCollate)==0 ){
1137 pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0);
1138 }
danielk1977aee18ef2005-03-09 12:26:50 +00001139 pExpr->pTab = pTab;
drh626a8792005-01-17 22:08:19 +00001140 break;
1141 }
1142 }
1143 }
1144 }
drhb7f91642004-10-31 02:22:47 +00001145#endif /* !defined(SQLITE_OMIT_TRIGGER) */
drh8141f612004-01-25 22:44:58 +00001146
drh626a8792005-01-17 22:08:19 +00001147 /*
1148 ** Perhaps the name is a reference to the ROWID
1149 */
1150 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
1151 cnt = 1;
1152 pExpr->iColumn = -1;
drh8a512562005-11-14 22:29:05 +00001153 pExpr->affinity = SQLITE_AFF_INTEGER;
drh626a8792005-01-17 22:08:19 +00001154 }
drh8141f612004-01-25 22:44:58 +00001155
drh626a8792005-01-17 22:08:19 +00001156 /*
1157 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
1158 ** might refer to an result-set alias. This happens, for example, when
1159 ** we are resolving names in the WHERE clause of the following command:
1160 **
1161 ** SELECT a+b AS x FROM table WHERE x<10;
1162 **
1163 ** In cases like this, replace pExpr with a copy of the expression that
1164 ** forms the result set entry ("a+b" in the example) and return immediately.
1165 ** Note that the expression in the result set should have already been
1166 ** resolved by the time the WHERE clause is resolved.
1167 */
drhffe07b22005-11-03 00:41:17 +00001168 if( cnt==0 && (pEList = pNC->pEList)!=0 && zTab==0 ){
drh626a8792005-01-17 22:08:19 +00001169 for(j=0; j<pEList->nExpr; j++){
1170 char *zAs = pEList->a[j].zName;
1171 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
drh36379e92007-07-23 22:51:15 +00001172 Expr *pDup, *pOrig;
drh626a8792005-01-17 22:08:19 +00001173 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
drh4f07e5f2007-05-14 11:34:46 +00001174 assert( pExpr->pList==0 );
1175 assert( pExpr->pSelect==0 );
drh36379e92007-07-23 22:51:15 +00001176 pOrig = pEList->a[j].pExpr;
1177 if( !pNC->allowAgg && ExprHasProperty(pOrig, EP_Agg) ){
1178 sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs);
drh17435752007-08-16 04:30:38 +00001179 sqlite3_free(zCol);
drh36379e92007-07-23 22:51:15 +00001180 return 2;
1181 }
danielk19771e536952007-08-16 10:09:01 +00001182 pDup = sqlite3ExprDup(db, pOrig);
drh4f07e5f2007-05-14 11:34:46 +00001183 if( pExpr->flags & EP_ExpCollate ){
1184 pDup->pColl = pExpr->pColl;
1185 pDup->flags |= EP_ExpCollate;
1186 }
drh17435752007-08-16 04:30:38 +00001187 if( pExpr->span.dyn ) sqlite3_free((char*)pExpr->span.z);
1188 if( pExpr->token.dyn ) sqlite3_free((char*)pExpr->token.z);
drh4f07e5f2007-05-14 11:34:46 +00001189 memcpy(pExpr, pDup, sizeof(*pExpr));
drh17435752007-08-16 04:30:38 +00001190 sqlite3_free(pDup);
drh15ccce12005-05-23 15:06:39 +00001191 cnt = 1;
danielk1977c9cf6e32007-06-25 16:29:33 +00001192 pMatch = 0;
drh626a8792005-01-17 22:08:19 +00001193 assert( zTab==0 && zDb==0 );
drh15ccce12005-05-23 15:06:39 +00001194 goto lookupname_end_2;
drh626a8792005-01-17 22:08:19 +00001195 }
1196 }
1197 }
1198
1199 /* Advance to the next name context. The loop will exit when either
1200 ** we have a match (cnt>0) or when we run out of name contexts.
1201 */
1202 if( cnt==0 ){
1203 pNC = pNC->pNext;
1204 }
drh8141f612004-01-25 22:44:58 +00001205 }
1206
1207 /*
1208 ** If X and Y are NULL (in other words if only the column name Z is
1209 ** supplied) and the value of Z is enclosed in double-quotes, then
1210 ** Z is a string literal if it doesn't match any column names. In that
1211 ** case, we need to return right away and not make any changes to
1212 ** pExpr.
drh15ccce12005-05-23 15:06:39 +00001213 **
1214 ** Because no reference was made to outer contexts, the pNC->nRef
1215 ** fields are not changed in any context.
drh8141f612004-01-25 22:44:58 +00001216 */
1217 if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
drh17435752007-08-16 04:30:38 +00001218 sqlite3_free(zCol);
drh8141f612004-01-25 22:44:58 +00001219 return 0;
1220 }
1221
1222 /*
1223 ** cnt==0 means there was not match. cnt>1 means there were two or
1224 ** more matches. Either way, we have an error.
1225 */
1226 if( cnt!=1 ){
1227 char *z = 0;
1228 char *zErr;
1229 zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s";
1230 if( zDb ){
drhf93339d2006-01-03 15:16:26 +00001231 sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, (char*)0);
drh8141f612004-01-25 22:44:58 +00001232 }else if( zTab ){
drhf93339d2006-01-03 15:16:26 +00001233 sqlite3SetString(&z, zTab, ".", zCol, (char*)0);
drh8141f612004-01-25 22:44:58 +00001234 }else{
drh17435752007-08-16 04:30:38 +00001235 z = sqlite3StrDup(zCol);
drh8141f612004-01-25 22:44:58 +00001236 }
danielk19774adee202004-05-08 08:23:19 +00001237 sqlite3ErrorMsg(pParse, zErr, z);
drh17435752007-08-16 04:30:38 +00001238 sqlite3_free(z);
drh73b211a2005-01-18 04:00:42 +00001239 pTopNC->nErr++;
drh8141f612004-01-25 22:44:58 +00001240 }
1241
drh51669862004-12-18 18:40:26 +00001242 /* If a column from a table in pSrcList is referenced, then record
1243 ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes
1244 ** bit 0 to be set. Column 1 sets bit 1. And so forth. If the
1245 ** column number is greater than the number of bits in the bitmask
1246 ** then set the high-order bit of the bitmask.
1247 */
1248 if( pExpr->iColumn>=0 && pMatch!=0 ){
1249 int n = pExpr->iColumn;
1250 if( n>=sizeof(Bitmask)*8 ){
1251 n = sizeof(Bitmask)*8-1;
1252 }
1253 assert( pMatch->iCursor==pExpr->iTable );
drhca83ac52007-02-01 01:40:44 +00001254 pMatch->colUsed |= ((Bitmask)1)<<n;
drh51669862004-12-18 18:40:26 +00001255 }
1256
danielk1977d5d56522005-03-16 12:15:20 +00001257lookupname_end:
drh8141f612004-01-25 22:44:58 +00001258 /* Clean up and return
1259 */
drh17435752007-08-16 04:30:38 +00001260 sqlite3_free(zDb);
1261 sqlite3_free(zTab);
danielk19774adee202004-05-08 08:23:19 +00001262 sqlite3ExprDelete(pExpr->pLeft);
drh8141f612004-01-25 22:44:58 +00001263 pExpr->pLeft = 0;
danielk19774adee202004-05-08 08:23:19 +00001264 sqlite3ExprDelete(pExpr->pRight);
drh8141f612004-01-25 22:44:58 +00001265 pExpr->pRight = 0;
1266 pExpr->op = TK_COLUMN;
drh15ccce12005-05-23 15:06:39 +00001267lookupname_end_2:
drh17435752007-08-16 04:30:38 +00001268 sqlite3_free(zCol);
drh626a8792005-01-17 22:08:19 +00001269 if( cnt==1 ){
danielk1977b3bce662005-01-29 08:32:43 +00001270 assert( pNC!=0 );
drh626a8792005-01-17 22:08:19 +00001271 sqlite3AuthRead(pParse, pExpr, pNC->pSrcList);
danielk1977aee18ef2005-03-09 12:26:50 +00001272 if( pMatch && !pMatch->pSelect ){
1273 pExpr->pTab = pMatch->pTab;
1274 }
drh15ccce12005-05-23 15:06:39 +00001275 /* Increment the nRef value on all name contexts from TopNC up to
1276 ** the point where the name matched. */
1277 for(;;){
1278 assert( pTopNC!=0 );
1279 pTopNC->nRef++;
1280 if( pTopNC==pNC ) break;
1281 pTopNC = pTopNC->pNext;
1282 }
1283 return 0;
1284 } else {
1285 return 1;
drh626a8792005-01-17 22:08:19 +00001286 }
drh8141f612004-01-25 22:44:58 +00001287}
1288
1289/*
drh626a8792005-01-17 22:08:19 +00001290** This routine is designed as an xFunc for walkExprTree().
1291**
drh73b211a2005-01-18 04:00:42 +00001292** Resolve symbolic names into TK_COLUMN operators for the current
drh626a8792005-01-17 22:08:19 +00001293** node in the expression tree. Return 0 to continue the search down
drh73b211a2005-01-18 04:00:42 +00001294** the tree or 2 to abort the tree walk.
1295**
1296** This routine also does error checking and name resolution for
1297** function names. The operator for aggregate functions is changed
1298** to TK_AGG_FUNCTION.
drh626a8792005-01-17 22:08:19 +00001299*/
1300static int nameResolverStep(void *pArg, Expr *pExpr){
1301 NameContext *pNC = (NameContext*)pArg;
drh626a8792005-01-17 22:08:19 +00001302 Parse *pParse;
drh626a8792005-01-17 22:08:19 +00001303
danielk1977b3bce662005-01-29 08:32:43 +00001304 if( pExpr==0 ) return 1;
drh626a8792005-01-17 22:08:19 +00001305 assert( pNC!=0 );
drh626a8792005-01-17 22:08:19 +00001306 pParse = pNC->pParse;
danielk1977b3bce662005-01-29 08:32:43 +00001307
drh626a8792005-01-17 22:08:19 +00001308 if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return 1;
1309 ExprSetProperty(pExpr, EP_Resolved);
1310#ifndef NDEBUG
danielk1977f0113002006-01-24 12:09:17 +00001311 if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
1312 SrcList *pSrcList = pNC->pSrcList;
danielk1977940fac92005-01-23 22:41:37 +00001313 int i;
danielk1977f0113002006-01-24 12:09:17 +00001314 for(i=0; i<pNC->pSrcList->nSrc; i++){
drh626a8792005-01-17 22:08:19 +00001315 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
1316 }
1317 }
1318#endif
1319 switch( pExpr->op ){
1320 /* Double-quoted strings (ex: "abc") are used as identifiers if
1321 ** possible. Otherwise they remain as strings. Single-quoted
1322 ** strings (ex: 'abc') are always string literals.
1323 */
1324 case TK_STRING: {
1325 if( pExpr->token.z[0]=='\'' ) break;
1326 /* Fall thru into the TK_ID case if this is a double-quoted string */
1327 }
1328 /* A lone identifier is the name of a column.
1329 */
1330 case TK_ID: {
drh626a8792005-01-17 22:08:19 +00001331 lookupName(pParse, 0, 0, &pExpr->token, pNC, pExpr);
1332 return 1;
1333 }
1334
1335 /* A table name and column name: ID.ID
1336 ** Or a database, table and column: ID.ID.ID
1337 */
1338 case TK_DOT: {
1339 Token *pColumn;
1340 Token *pTable;
1341 Token *pDb;
1342 Expr *pRight;
1343
danielk1977b3bce662005-01-29 08:32:43 +00001344 /* if( pSrcList==0 ) break; */
drh626a8792005-01-17 22:08:19 +00001345 pRight = pExpr->pRight;
1346 if( pRight->op==TK_ID ){
1347 pDb = 0;
1348 pTable = &pExpr->pLeft->token;
1349 pColumn = &pRight->token;
1350 }else{
1351 assert( pRight->op==TK_DOT );
1352 pDb = &pExpr->pLeft->token;
1353 pTable = &pRight->pLeft->token;
1354 pColumn = &pRight->pRight->token;
1355 }
1356 lookupName(pParse, pDb, pTable, pColumn, pNC, pExpr);
1357 return 1;
1358 }
1359
1360 /* Resolve function names
1361 */
drhb71090f2005-05-23 17:26:51 +00001362 case TK_CONST_FUNC:
drh626a8792005-01-17 22:08:19 +00001363 case TK_FUNCTION: {
1364 ExprList *pList = pExpr->pList; /* The argument list */
1365 int n = pList ? pList->nExpr : 0; /* Number of arguments */
1366 int no_such_func = 0; /* True if no such function exists */
1367 int wrong_num_args = 0; /* True if wrong number of arguments */
1368 int is_agg = 0; /* True if is an aggregate function */
1369 int i;
drh5169bbc2006-08-24 14:59:45 +00001370 int auth; /* Authorization to use the function */
drh626a8792005-01-17 22:08:19 +00001371 int nId; /* Number of characters in function name */
1372 const char *zId; /* The function name. */
drh73b211a2005-01-18 04:00:42 +00001373 FuncDef *pDef; /* Information about the function */
danielk197714db2662006-01-09 16:12:04 +00001374 int enc = ENC(pParse->db); /* The database encoding */
drh626a8792005-01-17 22:08:19 +00001375
drh2646da72005-12-09 20:02:05 +00001376 zId = (char*)pExpr->token.z;
drhb71090f2005-05-23 17:26:51 +00001377 nId = pExpr->token.n;
drh626a8792005-01-17 22:08:19 +00001378 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
1379 if( pDef==0 ){
1380 pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
1381 if( pDef==0 ){
1382 no_such_func = 1;
1383 }else{
1384 wrong_num_args = 1;
1385 }
1386 }else{
1387 is_agg = pDef->xFunc==0;
1388 }
drh2fca7fe2006-11-23 11:59:13 +00001389#ifndef SQLITE_OMIT_AUTHORIZATION
drh5169bbc2006-08-24 14:59:45 +00001390 if( pDef ){
1391 auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0);
1392 if( auth!=SQLITE_OK ){
1393 if( auth==SQLITE_DENY ){
1394 sqlite3ErrorMsg(pParse, "not authorized to use function: %s",
1395 pDef->zName);
1396 pNC->nErr++;
1397 }
1398 pExpr->op = TK_NULL;
1399 return 1;
1400 }
1401 }
drhb8b14212006-08-24 15:18:25 +00001402#endif
drh626a8792005-01-17 22:08:19 +00001403 if( is_agg && !pNC->allowAgg ){
1404 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
1405 pNC->nErr++;
1406 is_agg = 0;
1407 }else if( no_such_func ){
1408 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
1409 pNC->nErr++;
1410 }else if( wrong_num_args ){
1411 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
1412 nId, zId);
1413 pNC->nErr++;
1414 }
1415 if( is_agg ){
1416 pExpr->op = TK_AGG_FUNCTION;
1417 pNC->hasAgg = 1;
1418 }
drh73b211a2005-01-18 04:00:42 +00001419 if( is_agg ) pNC->allowAgg = 0;
drh626a8792005-01-17 22:08:19 +00001420 for(i=0; pNC->nErr==0 && i<n; i++){
drh73b211a2005-01-18 04:00:42 +00001421 walkExprTree(pList->a[i].pExpr, nameResolverStep, pNC);
drh626a8792005-01-17 22:08:19 +00001422 }
drh73b211a2005-01-18 04:00:42 +00001423 if( is_agg ) pNC->allowAgg = 1;
drh626a8792005-01-17 22:08:19 +00001424 /* FIX ME: Compute pExpr->affinity based on the expected return
1425 ** type of the function
1426 */
1427 return is_agg;
1428 }
danielk1977b3bce662005-01-29 08:32:43 +00001429#ifndef SQLITE_OMIT_SUBQUERY
1430 case TK_SELECT:
1431 case TK_EXISTS:
1432#endif
1433 case TK_IN: {
1434 if( pExpr->pSelect ){
drh8a9f38f2005-11-05 15:07:55 +00001435 int nRef = pNC->nRef;
drh06f65412005-11-03 02:03:13 +00001436#ifndef SQLITE_OMIT_CHECK
1437 if( pNC->isCheck ){
1438 sqlite3ErrorMsg(pParse,"subqueries prohibited in CHECK constraints");
1439 }
1440#endif
danielk1977b3bce662005-01-29 08:32:43 +00001441 sqlite3SelectResolve(pParse, pExpr->pSelect, pNC);
1442 assert( pNC->nRef>=nRef );
1443 if( nRef!=pNC->nRef ){
1444 ExprSetProperty(pExpr, EP_VarSelect);
1445 }
1446 }
drh4284fb02005-11-03 12:33:28 +00001447 break;
danielk1977b3bce662005-01-29 08:32:43 +00001448 }
drh4284fb02005-11-03 12:33:28 +00001449#ifndef SQLITE_OMIT_CHECK
1450 case TK_VARIABLE: {
1451 if( pNC->isCheck ){
1452 sqlite3ErrorMsg(pParse,"parameters prohibited in CHECK constraints");
1453 }
1454 break;
1455 }
1456#endif
drh626a8792005-01-17 22:08:19 +00001457 }
1458 return 0;
1459}
1460
1461/*
drhcce7d172000-05-31 15:34:51 +00001462** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +00001463** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +00001464** index to the table in the table list and a column offset. The
1465** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
1466** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +00001467** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +00001468** VDBE cursor number for a cursor that is pointing into the referenced
1469** table. The Expr.iColumn value is changed to the index of the column
1470** of the referenced table. The Expr.iColumn value for the special
1471** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
1472** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +00001473**
drh626a8792005-01-17 22:08:19 +00001474** Also resolve function names and check the functions for proper
1475** usage. Make sure all function names are recognized and all functions
1476** have the correct number of arguments. Leave an error message
1477** in pParse->zErrMsg if anything is amiss. Return the number of errors.
1478**
drh73b211a2005-01-18 04:00:42 +00001479** If the expression contains aggregate functions then set the EP_Agg
1480** property on the expression.
drh626a8792005-01-17 22:08:19 +00001481*/
danielk1977fc976062007-05-10 10:46:56 +00001482int sqlite3ExprResolveNames(
danielk1977b3bce662005-01-29 08:32:43 +00001483 NameContext *pNC, /* Namespace to resolve expressions in. */
1484 Expr *pExpr /* The expression to be analyzed. */
drh626a8792005-01-17 22:08:19 +00001485){
drh13449892005-09-07 21:22:45 +00001486 int savedHasAgg;
drh73b211a2005-01-18 04:00:42 +00001487 if( pExpr==0 ) return 0;
danielk1977fc976062007-05-10 10:46:56 +00001488#if SQLITE_MAX_EXPR_DEPTH>0
1489 if( (pExpr->nHeight+pNC->pParse->nHeight)>SQLITE_MAX_EXPR_DEPTH ){
1490 sqlite3ErrorMsg(pNC->pParse,
1491 "Expression tree is too large (maximum depth %d)",
1492 SQLITE_MAX_EXPR_DEPTH
1493 );
1494 return 1;
1495 }
1496 pNC->pParse->nHeight += pExpr->nHeight;
1497#endif
drh13449892005-09-07 21:22:45 +00001498 savedHasAgg = pNC->hasAgg;
1499 pNC->hasAgg = 0;
danielk1977b3bce662005-01-29 08:32:43 +00001500 walkExprTree(pExpr, nameResolverStep, pNC);
danielk1977fc976062007-05-10 10:46:56 +00001501#if SQLITE_MAX_EXPR_DEPTH>0
1502 pNC->pParse->nHeight -= pExpr->nHeight;
1503#endif
danielk1977b3bce662005-01-29 08:32:43 +00001504 if( pNC->nErr>0 ){
drh73b211a2005-01-18 04:00:42 +00001505 ExprSetProperty(pExpr, EP_Error);
drh73b211a2005-01-18 04:00:42 +00001506 }
drh13449892005-09-07 21:22:45 +00001507 if( pNC->hasAgg ){
1508 ExprSetProperty(pExpr, EP_Agg);
1509 }else if( savedHasAgg ){
1510 pNC->hasAgg = 1;
1511 }
drh73b211a2005-01-18 04:00:42 +00001512 return ExprHasProperty(pExpr, EP_Error);
drh626a8792005-01-17 22:08:19 +00001513}
1514
drh1398ad32005-01-19 23:24:50 +00001515/*
1516** A pointer instance of this structure is used to pass information
1517** through walkExprTree into codeSubqueryStep().
1518*/
1519typedef struct QueryCoder QueryCoder;
1520struct QueryCoder {
1521 Parse *pParse; /* The parsing context */
1522 NameContext *pNC; /* Namespace of first enclosing query */
1523};
1524
drh626a8792005-01-17 22:08:19 +00001525
1526/*
drh9cbe6352005-11-29 03:13:21 +00001527** Generate code for scalar subqueries used as an expression
1528** and IN operators. Examples:
drh626a8792005-01-17 22:08:19 +00001529**
drh9cbe6352005-11-29 03:13:21 +00001530** (SELECT a FROM b) -- subquery
1531** EXISTS (SELECT a FROM b) -- EXISTS subquery
1532** x IN (4,5,11) -- IN operator with list on right-hand side
1533** x IN (SELECT a FROM b) -- IN operator with subquery on the right
drhfef52082000-06-06 01:50:43 +00001534**
drh9cbe6352005-11-29 03:13:21 +00001535** The pExpr parameter describes the expression that contains the IN
1536** operator or subquery.
drhcce7d172000-05-31 15:34:51 +00001537*/
drh51522cd2005-01-20 13:36:19 +00001538#ifndef SQLITE_OMIT_SUBQUERY
danielk1977b3bce662005-01-29 08:32:43 +00001539void sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){
drh57dbd7b2005-07-08 18:25:26 +00001540 int testAddr = 0; /* One-time test address */
danielk1977b3bce662005-01-29 08:32:43 +00001541 Vdbe *v = sqlite3GetVdbe(pParse);
1542 if( v==0 ) return;
1543
danielk1977fc976062007-05-10 10:46:56 +00001544
drh57dbd7b2005-07-08 18:25:26 +00001545 /* This code must be run in its entirety every time it is encountered
1546 ** if any of the following is true:
1547 **
1548 ** * The right-hand side is a correlated subquery
1549 ** * The right-hand side is an expression list containing variables
1550 ** * We are inside a trigger
1551 **
1552 ** If all of the above are false, then we can run this code just once
1553 ** save the results, and reuse the same result on subsequent invocations.
danielk1977b3bce662005-01-29 08:32:43 +00001554 */
1555 if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->trigStack ){
1556 int mem = pParse->nMem++;
1557 sqlite3VdbeAddOp(v, OP_MemLoad, mem, 0);
drh57dbd7b2005-07-08 18:25:26 +00001558 testAddr = sqlite3VdbeAddOp(v, OP_If, 0, 0);
drh17435752007-08-16 04:30:38 +00001559 assert( testAddr>0 || pParse->db->mallocFailed );
drhd654be82005-09-20 17:42:23 +00001560 sqlite3VdbeAddOp(v, OP_MemInt, 1, mem);
danielk1977b3bce662005-01-29 08:32:43 +00001561 }
1562
drhcce7d172000-05-31 15:34:51 +00001563 switch( pExpr->op ){
drhfef52082000-06-06 01:50:43 +00001564 case TK_IN: {
danielk1977e014a832004-05-17 10:48:57 +00001565 char affinity;
drhd3d39e92004-05-20 22:16:29 +00001566 KeyInfo keyInfo;
drhb9bb7c12006-06-11 23:41:55 +00001567 int addr; /* Address of OP_OpenEphemeral instruction */
drhd3d39e92004-05-20 22:16:29 +00001568
danielk1977bf3b7212004-05-18 10:06:24 +00001569 affinity = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +00001570
1571 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
drh57dbd7b2005-07-08 18:25:26 +00001572 ** expression it is handled the same way. A virtual table is
danielk1977e014a832004-05-17 10:48:57 +00001573 ** filled with single-field index keys representing the results
1574 ** from the SELECT or the <exprlist>.
1575 **
1576 ** If the 'x' expression is a column value, or the SELECT...
1577 ** statement returns a column value, then the affinity of that
1578 ** column is used to build the index keys. If both 'x' and the
1579 ** SELECT... statement are columns, then numeric affinity is used
1580 ** if either column has NUMERIC or INTEGER affinity. If neither
1581 ** 'x' nor the SELECT... statement are columns, then numeric affinity
1582 ** is used.
1583 */
1584 pExpr->iTable = pParse->nTab++;
drhb9bb7c12006-06-11 23:41:55 +00001585 addr = sqlite3VdbeAddOp(v, OP_OpenEphemeral, pExpr->iTable, 0);
drhd3d39e92004-05-20 22:16:29 +00001586 memset(&keyInfo, 0, sizeof(keyInfo));
1587 keyInfo.nField = 1;
drhf3218fe2004-05-28 08:21:02 +00001588 sqlite3VdbeAddOp(v, OP_SetNumColumns, pExpr->iTable, 1);
danielk1977e014a832004-05-17 10:48:57 +00001589
drhfef52082000-06-06 01:50:43 +00001590 if( pExpr->pSelect ){
1591 /* Case 1: expr IN (SELECT ...)
1592 **
danielk1977e014a832004-05-17 10:48:57 +00001593 ** Generate code to write the results of the select into the temporary
1594 ** table allocated and opened above.
drhfef52082000-06-06 01:50:43 +00001595 */
danielk1977e014a832004-05-17 10:48:57 +00001596 int iParm = pExpr->iTable + (((int)affinity)<<16);
drhbe5c89a2004-07-26 00:31:09 +00001597 ExprList *pEList;
danielk1977e014a832004-05-17 10:48:57 +00001598 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
drh94ccde52007-04-13 16:06:32 +00001599 if( sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0) ){
1600 return;
1601 }
drhbe5c89a2004-07-26 00:31:09 +00001602 pEList = pExpr->pSelect->pEList;
1603 if( pEList && pEList->nExpr>0 ){
danielk1977bcbb04e2007-05-29 12:11:29 +00001604 keyInfo.aColl[0] = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft,
drhbe5c89a2004-07-26 00:31:09 +00001605 pEList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001606 }
drhfef52082000-06-06 01:50:43 +00001607 }else if( pExpr->pList ){
1608 /* Case 2: expr IN (exprlist)
1609 **
drhfd131da2007-08-07 17:13:03 +00001610 ** For each expression, build an index key from the evaluation and
danielk1977e014a832004-05-17 10:48:57 +00001611 ** store it in the temporary table. If <expr> is a column, then use
1612 ** that columns affinity when building index keys. If <expr> is not
1613 ** a column, use numeric affinity.
drhfef52082000-06-06 01:50:43 +00001614 */
danielk1977e014a832004-05-17 10:48:57 +00001615 int i;
drh57dbd7b2005-07-08 18:25:26 +00001616 ExprList *pList = pExpr->pList;
1617 struct ExprList_item *pItem;
1618
danielk1977e014a832004-05-17 10:48:57 +00001619 if( !affinity ){
drh8159a352006-05-23 23:22:29 +00001620 affinity = SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +00001621 }
danielk19770202b292004-06-09 09:55:16 +00001622 keyInfo.aColl[0] = pExpr->pLeft->pColl;
danielk1977e014a832004-05-17 10:48:57 +00001623
1624 /* Loop through each expression in <exprlist>. */
drh57dbd7b2005-07-08 18:25:26 +00001625 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
1626 Expr *pE2 = pItem->pExpr;
danielk1977e014a832004-05-17 10:48:57 +00001627
drh57dbd7b2005-07-08 18:25:26 +00001628 /* If the expression is not constant then we will need to
1629 ** disable the test that was generated above that makes sure
1630 ** this code only executes once. Because for a non-constant
1631 ** expression we need to rerun this code each time.
1632 */
drh6c30be82005-07-29 15:10:17 +00001633 if( testAddr>0 && !sqlite3ExprIsConstant(pE2) ){
drhf8875402006-03-17 13:56:34 +00001634 sqlite3VdbeChangeToNoop(v, testAddr-1, 3);
drh57dbd7b2005-07-08 18:25:26 +00001635 testAddr = 0;
drh4794b982000-06-06 13:54:14 +00001636 }
danielk1977e014a832004-05-17 10:48:57 +00001637
1638 /* Evaluate the expression and insert it into the temp table */
1639 sqlite3ExprCode(pParse, pE2);
drh94a11212004-09-25 13:12:14 +00001640 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1);
drhf0863fe2005-06-12 21:35:51 +00001641 sqlite3VdbeAddOp(v, OP_IdxInsert, pExpr->iTable, 0);
drhfef52082000-06-06 01:50:43 +00001642 }
1643 }
danielk19770202b292004-06-09 09:55:16 +00001644 sqlite3VdbeChangeP3(v, addr, (void *)&keyInfo, P3_KEYINFO);
danielk1977b3bce662005-01-29 08:32:43 +00001645 break;
drhfef52082000-06-06 01:50:43 +00001646 }
1647
drh51522cd2005-01-20 13:36:19 +00001648 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001649 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +00001650 /* This has to be a scalar SELECT. Generate code to put the
1651 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +00001652 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +00001653 */
drh2646da72005-12-09 20:02:05 +00001654 static const Token one = { (u8*)"1", 0, 1 };
drh51522cd2005-01-20 13:36:19 +00001655 Select *pSel;
drhec7429a2005-10-06 16:53:14 +00001656 int iMem;
1657 int sop;
drh1398ad32005-01-19 23:24:50 +00001658
drhec7429a2005-10-06 16:53:14 +00001659 pExpr->iColumn = iMem = pParse->nMem++;
drh51522cd2005-01-20 13:36:19 +00001660 pSel = pExpr->pSelect;
1661 if( pExpr->op==TK_SELECT ){
1662 sop = SRT_Mem;
drhec7429a2005-10-06 16:53:14 +00001663 sqlite3VdbeAddOp(v, OP_MemNull, iMem, 0);
1664 VdbeComment((v, "# Init subquery result"));
drh51522cd2005-01-20 13:36:19 +00001665 }else{
drh51522cd2005-01-20 13:36:19 +00001666 sop = SRT_Exists;
drhec7429a2005-10-06 16:53:14 +00001667 sqlite3VdbeAddOp(v, OP_MemInt, 0, iMem);
1668 VdbeComment((v, "# Init EXISTS result"));
drh51522cd2005-01-20 13:36:19 +00001669 }
drhec7429a2005-10-06 16:53:14 +00001670 sqlite3ExprDelete(pSel->pLimit);
1671 pSel->pLimit = sqlite3Expr(TK_INTEGER, 0, 0, &one);
drh94ccde52007-04-13 16:06:32 +00001672 if( sqlite3Select(pParse, pSel, sop, iMem, 0, 0, 0, 0) ){
1673 return;
1674 }
danielk1977b3bce662005-01-29 08:32:43 +00001675 break;
drhcce7d172000-05-31 15:34:51 +00001676 }
1677 }
danielk1977b3bce662005-01-29 08:32:43 +00001678
drh57dbd7b2005-07-08 18:25:26 +00001679 if( testAddr ){
drhd654be82005-09-20 17:42:23 +00001680 sqlite3VdbeJumpHere(v, testAddr);
danielk1977b3bce662005-01-29 08:32:43 +00001681 }
danielk1977fc976062007-05-10 10:46:56 +00001682
danielk1977b3bce662005-01-29 08:32:43 +00001683 return;
drhcce7d172000-05-31 15:34:51 +00001684}
drh51522cd2005-01-20 13:36:19 +00001685#endif /* SQLITE_OMIT_SUBQUERY */
drhcce7d172000-05-31 15:34:51 +00001686
drhcce7d172000-05-31 15:34:51 +00001687/*
drhfec19aa2004-05-19 20:41:03 +00001688** Generate an instruction that will put the integer describe by
1689** text z[0..n-1] on the stack.
1690*/
1691static void codeInteger(Vdbe *v, const char *z, int n){
drhabb6fca2007-08-16 12:24:01 +00001692 assert( z || v==0 || sqlite3VdbeDb(v)->mallocFailed );
danielk1977c9cf9012007-05-30 10:36:47 +00001693 if( z ){
1694 int i;
1695 if( sqlite3GetInt32(z, &i) ){
1696 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
1697 }else if( sqlite3FitsIn64Bits(z) ){
1698 sqlite3VdbeOp3(v, OP_Int64, 0, 0, z, n);
1699 }else{
1700 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n);
1701 }
drhfec19aa2004-05-19 20:41:03 +00001702 }
1703}
1704
drh945498f2007-02-24 11:52:52 +00001705
1706/*
1707** Generate code that will extract the iColumn-th column from
1708** table pTab and push that column value on the stack. There
1709** is an open cursor to pTab in iTable. If iColumn<0 then
1710** code is generated that extracts the rowid.
1711*/
1712void sqlite3ExprCodeGetColumn(Vdbe *v, Table *pTab, int iColumn, int iTable){
1713 if( iColumn<0 ){
1714 int op = (pTab && IsVirtual(pTab)) ? OP_VRowid : OP_Rowid;
1715 sqlite3VdbeAddOp(v, op, iTable, 0);
1716 }else if( pTab==0 ){
1717 sqlite3VdbeAddOp(v, OP_Column, iTable, iColumn);
1718 }else{
1719 int op = IsVirtual(pTab) ? OP_VColumn : OP_Column;
1720 sqlite3VdbeAddOp(v, op, iTable, iColumn);
1721 sqlite3ColumnDefault(v, pTab, iColumn);
1722#ifndef SQLITE_OMIT_FLOATING_POINT
1723 if( pTab->aCol[iColumn].affinity==SQLITE_AFF_REAL ){
1724 sqlite3VdbeAddOp(v, OP_RealAffinity, 0, 0);
1725 }
1726#endif
1727 }
1728}
1729
drhfec19aa2004-05-19 20:41:03 +00001730/*
drhcce7d172000-05-31 15:34:51 +00001731** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +00001732** expression and leave the result on the top of stack.
drhf2bc0132004-10-04 13:19:23 +00001733**
1734** This code depends on the fact that certain token values (ex: TK_EQ)
1735** are the same as opcode values (ex: OP_Eq) that implement the corresponding
1736** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
1737** the make process cause these values to align. Assert()s in the code
1738** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00001739*/
danielk19774adee202004-05-08 08:23:19 +00001740void sqlite3ExprCode(Parse *pParse, Expr *pExpr){
drhcce7d172000-05-31 15:34:51 +00001741 Vdbe *v = pParse->pVdbe;
1742 int op;
drhffe07b22005-11-03 00:41:17 +00001743 int stackChng = 1; /* Amount of change to stack depth */
1744
danielk19777977a172004-11-09 12:44:37 +00001745 if( v==0 ) return;
1746 if( pExpr==0 ){
drhf0863fe2005-06-12 21:35:51 +00001747 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
danielk19777977a172004-11-09 12:44:37 +00001748 return;
1749 }
drhf2bc0132004-10-04 13:19:23 +00001750 op = pExpr->op;
1751 switch( op ){
drh13449892005-09-07 21:22:45 +00001752 case TK_AGG_COLUMN: {
1753 AggInfo *pAggInfo = pExpr->pAggInfo;
1754 struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg];
1755 if( !pAggInfo->directMode ){
1756 sqlite3VdbeAddOp(v, OP_MemLoad, pCol->iMem, 0);
1757 break;
1758 }else if( pAggInfo->useSortingIdx ){
1759 sqlite3VdbeAddOp(v, OP_Column, pAggInfo->sortingIdx,
1760 pCol->iSorterColumn);
1761 break;
1762 }
1763 /* Otherwise, fall thru into the TK_COLUMN case */
1764 }
drh967e8b72000-06-21 13:59:10 +00001765 case TK_COLUMN: {
drhffe07b22005-11-03 00:41:17 +00001766 if( pExpr->iTable<0 ){
1767 /* This only happens when coding check constraints */
1768 assert( pParse->ckOffset>0 );
1769 sqlite3VdbeAddOp(v, OP_Dup, pParse->ckOffset-pExpr->iColumn-1, 1);
drhc4a3c772001-04-04 11:48:57 +00001770 }else{
drh945498f2007-02-24 11:52:52 +00001771 sqlite3ExprCodeGetColumn(v, pExpr->pTab, pExpr->iColumn, pExpr->iTable);
drh22827922000-06-06 17:27:05 +00001772 }
drhcce7d172000-05-31 15:34:51 +00001773 break;
1774 }
1775 case TK_INTEGER: {
drh2646da72005-12-09 20:02:05 +00001776 codeInteger(v, (char*)pExpr->token.z, pExpr->token.n);
drhfec19aa2004-05-19 20:41:03 +00001777 break;
1778 }
1779 case TK_FLOAT:
1780 case TK_STRING: {
drhf2bc0132004-10-04 13:19:23 +00001781 assert( TK_FLOAT==OP_Real );
1782 assert( TK_STRING==OP_String8 );
danielk19771e536952007-08-16 10:09:01 +00001783 sqlite3DequoteExpr(pParse->db, pExpr);
drh2646da72005-12-09 20:02:05 +00001784 sqlite3VdbeOp3(v, op, 0, 0, (char*)pExpr->token.z, pExpr->token.n);
drhcce7d172000-05-31 15:34:51 +00001785 break;
1786 }
drhf0863fe2005-06-12 21:35:51 +00001787 case TK_NULL: {
1788 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
1789 break;
1790 }
danielk19775338a5f2005-01-20 13:03:10 +00001791#ifndef SQLITE_OMIT_BLOB_LITERAL
danielk1977c572ef72004-05-27 09:28:41 +00001792 case TK_BLOB: {
drh6c8c6ce2005-08-23 11:17:58 +00001793 int n;
1794 const char *z;
drhf2bc0132004-10-04 13:19:23 +00001795 assert( TK_BLOB==OP_HexBlob );
drh6c8c6ce2005-08-23 11:17:58 +00001796 n = pExpr->token.n - 3;
drh2646da72005-12-09 20:02:05 +00001797 z = (char*)pExpr->token.z + 2;
drh6c8c6ce2005-08-23 11:17:58 +00001798 assert( n>=0 );
1799 if( n==0 ){
1800 z = "";
1801 }
1802 sqlite3VdbeOp3(v, op, 0, 0, z, n);
danielk1977c572ef72004-05-27 09:28:41 +00001803 break;
1804 }
danielk19775338a5f2005-01-20 13:03:10 +00001805#endif
drh50457892003-09-06 01:10:47 +00001806 case TK_VARIABLE: {
danielk19774adee202004-05-08 08:23:19 +00001807 sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0);
drh895d7472004-08-20 16:02:39 +00001808 if( pExpr->token.n>1 ){
drh2646da72005-12-09 20:02:05 +00001809 sqlite3VdbeChangeP3(v, -1, (char*)pExpr->token.z, pExpr->token.n);
drh895d7472004-08-20 16:02:39 +00001810 }
drh50457892003-09-06 01:10:47 +00001811 break;
1812 }
drh4e0cff62004-11-05 05:10:28 +00001813 case TK_REGISTER: {
1814 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iTable, 0);
1815 break;
1816 }
drh487e2622005-06-25 18:42:14 +00001817#ifndef SQLITE_OMIT_CAST
1818 case TK_CAST: {
1819 /* Expressions of the form: CAST(pLeft AS token) */
danielk1977f0113002006-01-24 12:09:17 +00001820 int aff, to_op;
drh487e2622005-06-25 18:42:14 +00001821 sqlite3ExprCode(pParse, pExpr->pLeft);
drh8a512562005-11-14 22:29:05 +00001822 aff = sqlite3AffinityType(&pExpr->token);
danielk1977f0113002006-01-24 12:09:17 +00001823 to_op = aff - SQLITE_AFF_TEXT + OP_ToText;
1824 assert( to_op==OP_ToText || aff!=SQLITE_AFF_TEXT );
1825 assert( to_op==OP_ToBlob || aff!=SQLITE_AFF_NONE );
1826 assert( to_op==OP_ToNumeric || aff!=SQLITE_AFF_NUMERIC );
1827 assert( to_op==OP_ToInt || aff!=SQLITE_AFF_INTEGER );
1828 assert( to_op==OP_ToReal || aff!=SQLITE_AFF_REAL );
1829 sqlite3VdbeAddOp(v, to_op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001830 stackChng = 0;
drh487e2622005-06-25 18:42:14 +00001831 break;
1832 }
1833#endif /* SQLITE_OMIT_CAST */
drhc9b84a12002-06-20 11:36:48 +00001834 case TK_LT:
1835 case TK_LE:
1836 case TK_GT:
1837 case TK_GE:
1838 case TK_NE:
1839 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00001840 assert( TK_LT==OP_Lt );
1841 assert( TK_LE==OP_Le );
1842 assert( TK_GT==OP_Gt );
1843 assert( TK_GE==OP_Ge );
1844 assert( TK_EQ==OP_Eq );
1845 assert( TK_NE==OP_Ne );
danielk1977a37cdde2004-05-16 11:15:36 +00001846 sqlite3ExprCode(pParse, pExpr->pLeft);
1847 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001848 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001849 stackChng = -1;
danielk1977a37cdde2004-05-16 11:15:36 +00001850 break;
drhc9b84a12002-06-20 11:36:48 +00001851 }
drhcce7d172000-05-31 15:34:51 +00001852 case TK_AND:
1853 case TK_OR:
1854 case TK_PLUS:
1855 case TK_STAR:
1856 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00001857 case TK_REM:
1858 case TK_BITAND:
1859 case TK_BITOR:
drh17c40292004-07-21 02:53:29 +00001860 case TK_SLASH:
drhbf4133c2001-10-13 02:59:08 +00001861 case TK_LSHIFT:
drh855eb1c2004-08-31 13:45:11 +00001862 case TK_RSHIFT:
drh00400772000-06-16 20:51:26 +00001863 case TK_CONCAT: {
drhf2bc0132004-10-04 13:19:23 +00001864 assert( TK_AND==OP_And );
1865 assert( TK_OR==OP_Or );
1866 assert( TK_PLUS==OP_Add );
1867 assert( TK_MINUS==OP_Subtract );
1868 assert( TK_REM==OP_Remainder );
1869 assert( TK_BITAND==OP_BitAnd );
1870 assert( TK_BITOR==OP_BitOr );
1871 assert( TK_SLASH==OP_Divide );
1872 assert( TK_LSHIFT==OP_ShiftLeft );
1873 assert( TK_RSHIFT==OP_ShiftRight );
1874 assert( TK_CONCAT==OP_Concat );
danielk19774adee202004-05-08 08:23:19 +00001875 sqlite3ExprCode(pParse, pExpr->pLeft);
1876 sqlite3ExprCode(pParse, pExpr->pRight);
drh855eb1c2004-08-31 13:45:11 +00001877 sqlite3VdbeAddOp(v, op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001878 stackChng = -1;
drh00400772000-06-16 20:51:26 +00001879 break;
1880 }
drhcce7d172000-05-31 15:34:51 +00001881 case TK_UMINUS: {
drhfec19aa2004-05-19 20:41:03 +00001882 Expr *pLeft = pExpr->pLeft;
1883 assert( pLeft );
1884 if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
1885 Token *p = &pLeft->token;
danielk19771e536952007-08-16 10:09:01 +00001886 char *z = sqlite3MPrintf(pParse->db, "-%.*s", p->n, p->z);
drhfec19aa2004-05-19 20:41:03 +00001887 if( pLeft->op==TK_FLOAT ){
1888 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001889 }else{
drhfec19aa2004-05-19 20:41:03 +00001890 codeInteger(v, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001891 }
drh17435752007-08-16 04:30:38 +00001892 sqlite3_free(z);
drh6e142f52000-06-08 13:36:40 +00001893 break;
1894 }
drh1ccde152000-06-17 13:12:39 +00001895 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +00001896 }
drhbf4133c2001-10-13 02:59:08 +00001897 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00001898 case TK_NOT: {
drhf2bc0132004-10-04 13:19:23 +00001899 assert( TK_BITNOT==OP_BitNot );
1900 assert( TK_NOT==OP_Not );
danielk19774adee202004-05-08 08:23:19 +00001901 sqlite3ExprCode(pParse, pExpr->pLeft);
1902 sqlite3VdbeAddOp(v, op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001903 stackChng = 0;
drhcce7d172000-05-31 15:34:51 +00001904 break;
1905 }
1906 case TK_ISNULL:
1907 case TK_NOTNULL: {
1908 int dest;
drhf2bc0132004-10-04 13:19:23 +00001909 assert( TK_ISNULL==OP_IsNull );
1910 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00001911 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1912 sqlite3ExprCode(pParse, pExpr->pLeft);
1913 dest = sqlite3VdbeCurrentAddr(v) + 2;
1914 sqlite3VdbeAddOp(v, op, 1, dest);
1915 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);
drhffe07b22005-11-03 00:41:17 +00001916 stackChng = 0;
drhf2bc0132004-10-04 13:19:23 +00001917 break;
drhcce7d172000-05-31 15:34:51 +00001918 }
drh22827922000-06-06 17:27:05 +00001919 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00001920 AggInfo *pInfo = pExpr->pAggInfo;
drh7e56e712005-11-16 12:53:15 +00001921 if( pInfo==0 ){
1922 sqlite3ErrorMsg(pParse, "misuse of aggregate: %T",
1923 &pExpr->span);
1924 }else{
1925 sqlite3VdbeAddOp(v, OP_MemLoad, pInfo->aFunc[pExpr->iAgg].iMem, 0);
1926 }
drh22827922000-06-06 17:27:05 +00001927 break;
1928 }
drhb71090f2005-05-23 17:26:51 +00001929 case TK_CONST_FUNC:
drhcce7d172000-05-31 15:34:51 +00001930 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00001931 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00001932 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00001933 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00001934 int nId;
1935 const char *zId;
drh13449892005-09-07 21:22:45 +00001936 int constMask = 0;
danielk1977682f68b2004-06-05 10:22:17 +00001937 int i;
drh17435752007-08-16 04:30:38 +00001938 sqlite3 *db = pParse->db;
1939 u8 enc = ENC(db);
danielk1977dc1bdc42004-06-11 10:51:27 +00001940 CollSeq *pColl = 0;
drh17435752007-08-16 04:30:38 +00001941
drh2646da72005-12-09 20:02:05 +00001942 zId = (char*)pExpr->token.z;
drhb71090f2005-05-23 17:26:51 +00001943 nId = pExpr->token.n;
danielk1977d8123362004-06-12 09:25:12 +00001944 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001945 assert( pDef!=0 );
drhf9b596e2004-05-26 16:54:42 +00001946 nExpr = sqlite3ExprCodeExprList(pParse, pList);
drhb7f6f682006-07-08 17:06:43 +00001947#ifndef SQLITE_OMIT_VIRTUALTABLE
drha43fa222006-07-08 18:41:37 +00001948 /* Possibly overload the function if the first argument is
1949 ** a virtual table column.
1950 **
1951 ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the
1952 ** second argument, not the first, as the argument to test to
1953 ** see if it is a column in a virtual table. This is done because
1954 ** the left operand of infix functions (the operand we want to
1955 ** control overloading) ends up as the second argument to the
1956 ** function. The expression "A glob B" is equivalent to
1957 ** "glob(B,A). We want to use the A in "A glob B" to test
1958 ** for function overloading. But we use the B term in "glob(B,A)".
1959 */
drh6a03a1c2006-07-08 18:34:59 +00001960 if( nExpr>=2 && (pExpr->flags & EP_InfixFunc) ){
drh17435752007-08-16 04:30:38 +00001961 pDef = sqlite3VtabOverloadFunction(db, pDef, nExpr, pList->a[1].pExpr);
drh6a03a1c2006-07-08 18:34:59 +00001962 }else if( nExpr>0 ){
drh17435752007-08-16 04:30:38 +00001963 pDef = sqlite3VtabOverloadFunction(db, pDef, nExpr, pList->a[0].pExpr);
drhb7f6f682006-07-08 17:06:43 +00001964 }
1965#endif
danielk1977682f68b2004-06-05 10:22:17 +00001966 for(i=0; i<nExpr && i<32; i++){
danielk1977d02eb1f2004-06-06 09:44:03 +00001967 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
drh13449892005-09-07 21:22:45 +00001968 constMask |= (1<<i);
danielk1977d02eb1f2004-06-06 09:44:03 +00001969 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001970 if( pDef->needCollSeq && !pColl ){
1971 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
1972 }
1973 }
1974 if( pDef->needCollSeq ){
1975 if( !pColl ) pColl = pParse->db->pDfltColl;
danielk1977d8123362004-06-12 09:25:12 +00001976 sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ);
danielk1977682f68b2004-06-05 10:22:17 +00001977 }
drh13449892005-09-07 21:22:45 +00001978 sqlite3VdbeOp3(v, OP_Function, constMask, nExpr, (char*)pDef, P3_FUNCDEF);
drhffe07b22005-11-03 00:41:17 +00001979 stackChng = 1-nExpr;
drhcce7d172000-05-31 15:34:51 +00001980 break;
1981 }
drhfe2093d2005-01-20 22:48:47 +00001982#ifndef SQLITE_OMIT_SUBQUERY
1983 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001984 case TK_SELECT: {
drh41714d62006-03-02 04:44:23 +00001985 if( pExpr->iColumn==0 ){
1986 sqlite3CodeSubselect(pParse, pExpr);
1987 }
danielk19774adee202004-05-08 08:23:19 +00001988 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drhad6d9462004-09-19 02:15:24 +00001989 VdbeComment((v, "# load subquery result"));
drh19a775c2000-06-05 18:54:46 +00001990 break;
1991 }
drhfef52082000-06-06 01:50:43 +00001992 case TK_IN: {
1993 int addr;
drh94a11212004-09-25 13:12:14 +00001994 char affinity;
drhafa5f682006-01-30 14:36:59 +00001995 int ckOffset = pParse->ckOffset;
danielk1977b3bce662005-01-29 08:32:43 +00001996 sqlite3CodeSubselect(pParse, pExpr);
danielk1977e014a832004-05-17 10:48:57 +00001997
1998 /* Figure out the affinity to use to create a key from the results
1999 ** of the expression. affinityStr stores a static string suitable for
danielk1977ededfd52004-06-17 07:53:01 +00002000 ** P3 of OP_MakeRecord.
danielk1977e014a832004-05-17 10:48:57 +00002001 */
drh94a11212004-09-25 13:12:14 +00002002 affinity = comparisonAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +00002003
danielk19774adee202004-05-08 08:23:19 +00002004 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
danielk1977cdbd8ef2007-05-12 06:11:12 +00002005 pParse->ckOffset = (ckOffset ? (ckOffset+1) : 0);
danielk1977e014a832004-05-17 10:48:57 +00002006
2007 /* Code the <expr> from "<expr> IN (...)". The temporary table
2008 ** pExpr->iTable contains the values that make up the (...) set.
2009 */
danielk19774adee202004-05-08 08:23:19 +00002010 sqlite3ExprCode(pParse, pExpr->pLeft);
2011 addr = sqlite3VdbeCurrentAddr(v);
danielk1977e014a832004-05-17 10:48:57 +00002012 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); /* addr + 0 */
danielk19774adee202004-05-08 08:23:19 +00002013 sqlite3VdbeAddOp(v, OP_Pop, 2, 0);
drhf0863fe2005-06-12 21:35:51 +00002014 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +00002015 sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7);
drh94a11212004-09-25 13:12:14 +00002016 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1); /* addr + 4 */
danielk1977e014a832004-05-17 10:48:57 +00002017 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7);
2018 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); /* addr + 6 */
2019
drhfef52082000-06-06 01:50:43 +00002020 break;
2021 }
danielk197793758c82005-01-21 08:13:14 +00002022#endif
drhfef52082000-06-06 01:50:43 +00002023 case TK_BETWEEN: {
drhbe5c89a2004-07-26 00:31:09 +00002024 Expr *pLeft = pExpr->pLeft;
2025 struct ExprList_item *pLItem = pExpr->pList->a;
2026 Expr *pRight = pLItem->pExpr;
2027 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00002028 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00002029 sqlite3ExprCode(pParse, pRight);
2030 codeCompare(pParse, pLeft, pRight, OP_Ge, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00002031 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
drhbe5c89a2004-07-26 00:31:09 +00002032 pLItem++;
2033 pRight = pLItem->pExpr;
2034 sqlite3ExprCode(pParse, pRight);
2035 codeCompare(pParse, pLeft, pRight, OP_Le, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00002036 sqlite3VdbeAddOp(v, OP_And, 0, 0);
drhfef52082000-06-06 01:50:43 +00002037 break;
2038 }
drh4f07e5f2007-05-14 11:34:46 +00002039 case TK_UPLUS: {
danielk19774adee202004-05-08 08:23:19 +00002040 sqlite3ExprCode(pParse, pExpr->pLeft);
drhffe07b22005-11-03 00:41:17 +00002041 stackChng = 0;
drha2e00042002-01-22 03:13:42 +00002042 break;
2043 }
drh17a7f8d2002-03-24 13:13:27 +00002044 case TK_CASE: {
2045 int expr_end_label;
drhf5905aa2002-05-26 20:54:33 +00002046 int jumpInst;
drhf5905aa2002-05-26 20:54:33 +00002047 int nExpr;
drh17a7f8d2002-03-24 13:13:27 +00002048 int i;
drhbe5c89a2004-07-26 00:31:09 +00002049 ExprList *pEList;
2050 struct ExprList_item *aListelem;
drh17a7f8d2002-03-24 13:13:27 +00002051
2052 assert(pExpr->pList);
2053 assert((pExpr->pList->nExpr % 2) == 0);
2054 assert(pExpr->pList->nExpr > 0);
drhbe5c89a2004-07-26 00:31:09 +00002055 pEList = pExpr->pList;
2056 aListelem = pEList->a;
2057 nExpr = pEList->nExpr;
danielk19774adee202004-05-08 08:23:19 +00002058 expr_end_label = sqlite3VdbeMakeLabel(v);
drh17a7f8d2002-03-24 13:13:27 +00002059 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00002060 sqlite3ExprCode(pParse, pExpr->pLeft);
drh17a7f8d2002-03-24 13:13:27 +00002061 }
drhf5905aa2002-05-26 20:54:33 +00002062 for(i=0; i<nExpr; i=i+2){
drhbe5c89a2004-07-26 00:31:09 +00002063 sqlite3ExprCode(pParse, aListelem[i].pExpr);
drh17a7f8d2002-03-24 13:13:27 +00002064 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00002065 sqlite3VdbeAddOp(v, OP_Dup, 1, 1);
drhbe5c89a2004-07-26 00:31:09 +00002066 jumpInst = codeCompare(pParse, pExpr->pLeft, aListelem[i].pExpr,
2067 OP_Ne, 0, 1);
danielk19774adee202004-05-08 08:23:19 +00002068 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00002069 }else{
danielk19774adee202004-05-08 08:23:19 +00002070 jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00002071 }
drhbe5c89a2004-07-26 00:31:09 +00002072 sqlite3ExprCode(pParse, aListelem[i+1].pExpr);
danielk19774adee202004-05-08 08:23:19 +00002073 sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label);
drhd654be82005-09-20 17:42:23 +00002074 sqlite3VdbeJumpHere(v, jumpInst);
drh17a7f8d2002-03-24 13:13:27 +00002075 }
drhf570f012002-05-31 15:51:25 +00002076 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00002077 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhf570f012002-05-31 15:51:25 +00002078 }
drh17a7f8d2002-03-24 13:13:27 +00002079 if( pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00002080 sqlite3ExprCode(pParse, pExpr->pRight);
drh17a7f8d2002-03-24 13:13:27 +00002081 }else{
drhf0863fe2005-06-12 21:35:51 +00002082 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
drh17a7f8d2002-03-24 13:13:27 +00002083 }
danielk19774adee202004-05-08 08:23:19 +00002084 sqlite3VdbeResolveLabel(v, expr_end_label);
danielk19776f349032002-06-11 02:25:40 +00002085 break;
2086 }
danielk19775338a5f2005-01-20 13:03:10 +00002087#ifndef SQLITE_OMIT_TRIGGER
danielk19776f349032002-06-11 02:25:40 +00002088 case TK_RAISE: {
2089 if( !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +00002090 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00002091 "RAISE() may only be used within a trigger-program");
drhfd131da2007-08-07 17:13:03 +00002092 return;
danielk19776f349032002-06-11 02:25:40 +00002093 }
drhad6d9462004-09-19 02:15:24 +00002094 if( pExpr->iColumn!=OE_Ignore ){
2095 assert( pExpr->iColumn==OE_Rollback ||
2096 pExpr->iColumn == OE_Abort ||
2097 pExpr->iColumn == OE_Fail );
danielk19771e536952007-08-16 10:09:01 +00002098 sqlite3DequoteExpr(pParse->db, pExpr);
drhad6d9462004-09-19 02:15:24 +00002099 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn,
drh2646da72005-12-09 20:02:05 +00002100 (char*)pExpr->token.z, pExpr->token.n);
danielk19776f349032002-06-11 02:25:40 +00002101 } else {
drhad6d9462004-09-19 02:15:24 +00002102 assert( pExpr->iColumn == OE_Ignore );
2103 sqlite3VdbeAddOp(v, OP_ContextPop, 0, 0);
2104 sqlite3VdbeAddOp(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
2105 VdbeComment((v, "# raise(IGNORE)"));
danielk19776f349032002-06-11 02:25:40 +00002106 }
drhffe07b22005-11-03 00:41:17 +00002107 stackChng = 0;
2108 break;
drh17a7f8d2002-03-24 13:13:27 +00002109 }
danielk19775338a5f2005-01-20 13:03:10 +00002110#endif
drhffe07b22005-11-03 00:41:17 +00002111 }
2112
2113 if( pParse->ckOffset ){
2114 pParse->ckOffset += stackChng;
2115 assert( pParse->ckOffset );
drhcce7d172000-05-31 15:34:51 +00002116 }
drhcce7d172000-05-31 15:34:51 +00002117}
2118
danielk197793758c82005-01-21 08:13:14 +00002119#ifndef SQLITE_OMIT_TRIGGER
drhcce7d172000-05-31 15:34:51 +00002120/*
drh25303782004-12-07 15:41:48 +00002121** Generate code that evalutes the given expression and leaves the result
2122** on the stack. See also sqlite3ExprCode().
2123**
2124** This routine might also cache the result and modify the pExpr tree
2125** so that it will make use of the cached result on subsequent evaluations
2126** rather than evaluate the whole expression again. Trivial expressions are
2127** not cached. If the expression is cached, its result is stored in a
2128** memory location.
2129*/
2130void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr){
2131 Vdbe *v = pParse->pVdbe;
2132 int iMem;
2133 int addr1, addr2;
2134 if( v==0 ) return;
2135 addr1 = sqlite3VdbeCurrentAddr(v);
2136 sqlite3ExprCode(pParse, pExpr);
2137 addr2 = sqlite3VdbeCurrentAddr(v);
2138 if( addr2>addr1+1 || sqlite3VdbeGetOp(v, addr1)->opcode==OP_Function ){
2139 iMem = pExpr->iTable = pParse->nMem++;
2140 sqlite3VdbeAddOp(v, OP_MemStore, iMem, 0);
2141 pExpr->op = TK_REGISTER;
2142 }
2143}
danielk197793758c82005-01-21 08:13:14 +00002144#endif
drh25303782004-12-07 15:41:48 +00002145
2146/*
drh268380c2004-02-25 13:47:31 +00002147** Generate code that pushes the value of every element of the given
drhf9b596e2004-05-26 16:54:42 +00002148** expression list onto the stack.
drh268380c2004-02-25 13:47:31 +00002149**
2150** Return the number of elements pushed onto the stack.
2151*/
danielk19774adee202004-05-08 08:23:19 +00002152int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00002153 Parse *pParse, /* Parsing context */
drhf9b596e2004-05-26 16:54:42 +00002154 ExprList *pList /* The expression list to be coded */
drh268380c2004-02-25 13:47:31 +00002155){
2156 struct ExprList_item *pItem;
2157 int i, n;
drh268380c2004-02-25 13:47:31 +00002158 if( pList==0 ) return 0;
drh268380c2004-02-25 13:47:31 +00002159 n = pList->nExpr;
drhc182d162005-08-14 20:47:16 +00002160 for(pItem=pList->a, i=n; i>0; i--, pItem++){
danielk19774adee202004-05-08 08:23:19 +00002161 sqlite3ExprCode(pParse, pItem->pExpr);
drh268380c2004-02-25 13:47:31 +00002162 }
drhf9b596e2004-05-26 16:54:42 +00002163 return n;
drh268380c2004-02-25 13:47:31 +00002164}
2165
2166/*
drhcce7d172000-05-31 15:34:51 +00002167** Generate code for a boolean expression such that a jump is made
2168** to the label "dest" if the expression is true but execution
2169** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00002170**
2171** If the expression evaluates to NULL (neither true nor false), then
2172** take the jump if the jumpIfNull flag is true.
drhf2bc0132004-10-04 13:19:23 +00002173**
2174** This code depends on the fact that certain token values (ex: TK_EQ)
2175** are the same as opcode values (ex: OP_Eq) that implement the corresponding
2176** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
2177** the make process cause these values to align. Assert()s in the code
2178** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00002179*/
danielk19774adee202004-05-08 08:23:19 +00002180void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00002181 Vdbe *v = pParse->pVdbe;
2182 int op = 0;
drhffe07b22005-11-03 00:41:17 +00002183 int ckOffset = pParse->ckOffset;
drhdaffd0e2001-04-11 14:28:42 +00002184 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00002185 op = pExpr->op;
2186 switch( op ){
drhcce7d172000-05-31 15:34:51 +00002187 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00002188 int d2 = sqlite3VdbeMakeLabel(v);
2189 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
2190 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
2191 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00002192 break;
2193 }
2194 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00002195 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
2196 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002197 break;
2198 }
2199 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00002200 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002201 break;
2202 }
2203 case TK_LT:
2204 case TK_LE:
2205 case TK_GT:
2206 case TK_GE:
2207 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00002208 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00002209 assert( TK_LT==OP_Lt );
2210 assert( TK_LE==OP_Le );
2211 assert( TK_GT==OP_Gt );
2212 assert( TK_GE==OP_Ge );
2213 assert( TK_EQ==OP_Eq );
2214 assert( TK_NE==OP_Ne );
danielk19774adee202004-05-08 08:23:19 +00002215 sqlite3ExprCode(pParse, pExpr->pLeft);
2216 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00002217 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002218 break;
2219 }
2220 case TK_ISNULL:
2221 case TK_NOTNULL: {
drhf2bc0132004-10-04 13:19:23 +00002222 assert( TK_ISNULL==OP_IsNull );
2223 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00002224 sqlite3ExprCode(pParse, pExpr->pLeft);
2225 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00002226 break;
2227 }
drhfef52082000-06-06 01:50:43 +00002228 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00002229 /* The expression "x BETWEEN y AND z" is implemented as:
2230 **
2231 ** 1 IF (x < y) GOTO 3
2232 ** 2 IF (x <= z) GOTO <dest>
2233 ** 3 ...
2234 */
drhf5905aa2002-05-26 20:54:33 +00002235 int addr;
drhbe5c89a2004-07-26 00:31:09 +00002236 Expr *pLeft = pExpr->pLeft;
2237 Expr *pRight = pExpr->pList->a[0].pExpr;
2238 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00002239 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00002240 sqlite3ExprCode(pParse, pRight);
2241 addr = codeCompare(pParse, pLeft, pRight, OP_Lt, 0, !jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00002242
drhbe5c89a2004-07-26 00:31:09 +00002243 pRight = pExpr->pList->a[1].pExpr;
2244 sqlite3ExprCode(pParse, pRight);
2245 codeCompare(pParse, pLeft, pRight, OP_Le, dest, jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00002246
danielk19774adee202004-05-08 08:23:19 +00002247 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
drhd654be82005-09-20 17:42:23 +00002248 sqlite3VdbeJumpHere(v, addr);
danielk19774adee202004-05-08 08:23:19 +00002249 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00002250 break;
2251 }
drhcce7d172000-05-31 15:34:51 +00002252 default: {
danielk19774adee202004-05-08 08:23:19 +00002253 sqlite3ExprCode(pParse, pExpr);
2254 sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00002255 break;
2256 }
2257 }
drhffe07b22005-11-03 00:41:17 +00002258 pParse->ckOffset = ckOffset;
drhcce7d172000-05-31 15:34:51 +00002259}
2260
2261/*
drh66b89c82000-11-28 20:47:17 +00002262** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00002263** to the label "dest" if the expression is false but execution
2264** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00002265**
2266** If the expression evaluates to NULL (neither true nor false) then
2267** jump if jumpIfNull is true or fall through if jumpIfNull is false.
drhcce7d172000-05-31 15:34:51 +00002268*/
danielk19774adee202004-05-08 08:23:19 +00002269void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00002270 Vdbe *v = pParse->pVdbe;
2271 int op = 0;
drhffe07b22005-11-03 00:41:17 +00002272 int ckOffset = pParse->ckOffset;
drhdaffd0e2001-04-11 14:28:42 +00002273 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00002274
2275 /* The value of pExpr->op and op are related as follows:
2276 **
2277 ** pExpr->op op
2278 ** --------- ----------
2279 ** TK_ISNULL OP_NotNull
2280 ** TK_NOTNULL OP_IsNull
2281 ** TK_NE OP_Eq
2282 ** TK_EQ OP_Ne
2283 ** TK_GT OP_Le
2284 ** TK_LE OP_Gt
2285 ** TK_GE OP_Lt
2286 ** TK_LT OP_Ge
2287 **
2288 ** For other values of pExpr->op, op is undefined and unused.
2289 ** The value of TK_ and OP_ constants are arranged such that we
2290 ** can compute the mapping above using the following expression.
2291 ** Assert()s verify that the computation is correct.
2292 */
2293 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
2294
2295 /* Verify correct alignment of TK_ and OP_ constants
2296 */
2297 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
2298 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
2299 assert( pExpr->op!=TK_NE || op==OP_Eq );
2300 assert( pExpr->op!=TK_EQ || op==OP_Ne );
2301 assert( pExpr->op!=TK_LT || op==OP_Ge );
2302 assert( pExpr->op!=TK_LE || op==OP_Gt );
2303 assert( pExpr->op!=TK_GT || op==OP_Le );
2304 assert( pExpr->op!=TK_GE || op==OP_Lt );
2305
drhcce7d172000-05-31 15:34:51 +00002306 switch( pExpr->op ){
2307 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00002308 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
2309 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002310 break;
2311 }
2312 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00002313 int d2 = sqlite3VdbeMakeLabel(v);
2314 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
2315 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
2316 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00002317 break;
2318 }
2319 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00002320 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002321 break;
2322 }
2323 case TK_LT:
2324 case TK_LE:
2325 case TK_GT:
2326 case TK_GE:
2327 case TK_NE:
2328 case TK_EQ: {
danielk19774adee202004-05-08 08:23:19 +00002329 sqlite3ExprCode(pParse, pExpr->pLeft);
2330 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00002331 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002332 break;
2333 }
drhcce7d172000-05-31 15:34:51 +00002334 case TK_ISNULL:
2335 case TK_NOTNULL: {
danielk19774adee202004-05-08 08:23:19 +00002336 sqlite3ExprCode(pParse, pExpr->pLeft);
2337 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00002338 break;
2339 }
drhfef52082000-06-06 01:50:43 +00002340 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00002341 /* The expression is "x BETWEEN y AND z". It is implemented as:
2342 **
2343 ** 1 IF (x >= y) GOTO 3
2344 ** 2 GOTO <dest>
2345 ** 3 IF (x > z) GOTO <dest>
2346 */
drhfef52082000-06-06 01:50:43 +00002347 int addr;
drhbe5c89a2004-07-26 00:31:09 +00002348 Expr *pLeft = pExpr->pLeft;
2349 Expr *pRight = pExpr->pList->a[0].pExpr;
2350 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00002351 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00002352 sqlite3ExprCode(pParse, pRight);
danielk19774adee202004-05-08 08:23:19 +00002353 addr = sqlite3VdbeCurrentAddr(v);
drhbe5c89a2004-07-26 00:31:09 +00002354 codeCompare(pParse, pLeft, pRight, OP_Ge, addr+3, !jumpIfNull);
2355
danielk19774adee202004-05-08 08:23:19 +00002356 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
2357 sqlite3VdbeAddOp(v, OP_Goto, 0, dest);
drhbe5c89a2004-07-26 00:31:09 +00002358 pRight = pExpr->pList->a[1].pExpr;
2359 sqlite3ExprCode(pParse, pRight);
2360 codeCompare(pParse, pLeft, pRight, OP_Gt, dest, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00002361 break;
2362 }
drhcce7d172000-05-31 15:34:51 +00002363 default: {
danielk19774adee202004-05-08 08:23:19 +00002364 sqlite3ExprCode(pParse, pExpr);
2365 sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00002366 break;
2367 }
2368 }
drhffe07b22005-11-03 00:41:17 +00002369 pParse->ckOffset = ckOffset;
drhcce7d172000-05-31 15:34:51 +00002370}
drh22827922000-06-06 17:27:05 +00002371
2372/*
2373** Do a deep comparison of two expression trees. Return TRUE (non-zero)
2374** if they are identical and return FALSE if they differ in any way.
drhd40aab02007-02-24 15:29:03 +00002375**
2376** Sometimes this routine will return FALSE even if the two expressions
2377** really are equivalent. If we cannot prove that the expressions are
2378** identical, we return FALSE just to be safe. So if this routine
2379** returns false, then you do not really know for certain if the two
2380** expressions are the same. But if you get a TRUE return, then you
2381** can be sure the expressions are the same. In the places where
2382** this routine is used, it does not hurt to get an extra FALSE - that
2383** just might result in some slightly slower code. But returning
2384** an incorrect TRUE could lead to a malfunction.
drh22827922000-06-06 17:27:05 +00002385*/
danielk19774adee202004-05-08 08:23:19 +00002386int sqlite3ExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00002387 int i;
danielk19774b202ae2006-01-23 05:50:58 +00002388 if( pA==0||pB==0 ){
2389 return pB==pA;
drh22827922000-06-06 17:27:05 +00002390 }
2391 if( pA->op!=pB->op ) return 0;
drhfd357972005-09-09 01:33:19 +00002392 if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 0;
danielk19774adee202004-05-08 08:23:19 +00002393 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
2394 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00002395 if( pA->pList ){
2396 if( pB->pList==0 ) return 0;
2397 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
2398 for(i=0; i<pA->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00002399 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00002400 return 0;
2401 }
2402 }
2403 }else if( pB->pList ){
2404 return 0;
2405 }
2406 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00002407 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drhdd735212007-02-24 13:53:05 +00002408 if( pA->op!=TK_COLUMN && pA->token.z ){
drh22827922000-06-06 17:27:05 +00002409 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00002410 if( pB->token.n!=pA->token.n ) return 0;
drh2646da72005-12-09 20:02:05 +00002411 if( sqlite3StrNICmp((char*)pA->token.z,(char*)pB->token.z,pB->token.n)!=0 ){
2412 return 0;
2413 }
drh22827922000-06-06 17:27:05 +00002414 }
2415 return 1;
2416}
2417
drh13449892005-09-07 21:22:45 +00002418
drh22827922000-06-06 17:27:05 +00002419/*
drh13449892005-09-07 21:22:45 +00002420** Add a new element to the pAggInfo->aCol[] array. Return the index of
2421** the new element. Return a negative number if malloc fails.
drh22827922000-06-06 17:27:05 +00002422*/
drh17435752007-08-16 04:30:38 +00002423static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){
drh13449892005-09-07 21:22:45 +00002424 int i;
drhcf643722007-03-27 13:36:37 +00002425 pInfo->aCol = sqlite3ArrayAllocate(
drh17435752007-08-16 04:30:38 +00002426 db,
drhcf643722007-03-27 13:36:37 +00002427 pInfo->aCol,
2428 sizeof(pInfo->aCol[0]),
2429 3,
2430 &pInfo->nColumn,
2431 &pInfo->nColumnAlloc,
2432 &i
2433 );
drh13449892005-09-07 21:22:45 +00002434 return i;
2435}
2436
2437/*
2438** Add a new element to the pAggInfo->aFunc[] array. Return the index of
2439** the new element. Return a negative number if malloc fails.
2440*/
drh17435752007-08-16 04:30:38 +00002441static int addAggInfoFunc(sqlite3 *db, AggInfo *pInfo){
drh13449892005-09-07 21:22:45 +00002442 int i;
drhcf643722007-03-27 13:36:37 +00002443 pInfo->aFunc = sqlite3ArrayAllocate(
drh17435752007-08-16 04:30:38 +00002444 db,
drhcf643722007-03-27 13:36:37 +00002445 pInfo->aFunc,
2446 sizeof(pInfo->aFunc[0]),
2447 3,
2448 &pInfo->nFunc,
2449 &pInfo->nFuncAlloc,
2450 &i
2451 );
drh13449892005-09-07 21:22:45 +00002452 return i;
2453}
drh22827922000-06-06 17:27:05 +00002454
2455/*
drh626a8792005-01-17 22:08:19 +00002456** This is an xFunc for walkExprTree() used to implement
2457** sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates
2458** for additional information.
drh22827922000-06-06 17:27:05 +00002459**
drh626a8792005-01-17 22:08:19 +00002460** This routine analyzes the aggregate function at pExpr.
drh22827922000-06-06 17:27:05 +00002461*/
drh626a8792005-01-17 22:08:19 +00002462static int analyzeAggregate(void *pArg, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00002463 int i;
danielk1977a58fdfb2005-02-08 07:50:40 +00002464 NameContext *pNC = (NameContext *)pArg;
2465 Parse *pParse = pNC->pParse;
2466 SrcList *pSrcList = pNC->pSrcList;
drh13449892005-09-07 21:22:45 +00002467 AggInfo *pAggInfo = pNC->pAggInfo;
drh22827922000-06-06 17:27:05 +00002468
drh22827922000-06-06 17:27:05 +00002469 switch( pExpr->op ){
drh89c69d02007-01-04 01:20:28 +00002470 case TK_AGG_COLUMN:
drh967e8b72000-06-21 13:59:10 +00002471 case TK_COLUMN: {
drh13449892005-09-07 21:22:45 +00002472 /* Check to see if the column is in one of the tables in the FROM
2473 ** clause of the aggregate query */
2474 if( pSrcList ){
2475 struct SrcList_item *pItem = pSrcList->a;
2476 for(i=0; i<pSrcList->nSrc; i++, pItem++){
2477 struct AggInfo_col *pCol;
2478 if( pExpr->iTable==pItem->iCursor ){
2479 /* If we reach this point, it means that pExpr refers to a table
2480 ** that is in the FROM clause of the aggregate query.
2481 **
2482 ** Make an entry for the column in pAggInfo->aCol[] if there
2483 ** is not an entry there already.
2484 */
drh7f906d62007-03-12 23:48:52 +00002485 int k;
drh13449892005-09-07 21:22:45 +00002486 pCol = pAggInfo->aCol;
drh7f906d62007-03-12 23:48:52 +00002487 for(k=0; k<pAggInfo->nColumn; k++, pCol++){
drh13449892005-09-07 21:22:45 +00002488 if( pCol->iTable==pExpr->iTable &&
2489 pCol->iColumn==pExpr->iColumn ){
2490 break;
2491 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002492 }
danielk19771e536952007-08-16 10:09:01 +00002493 if( (k>=pAggInfo->nColumn)
2494 && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0
2495 ){
drh7f906d62007-03-12 23:48:52 +00002496 pCol = &pAggInfo->aCol[k];
danielk19770817d0d2007-02-14 09:19:36 +00002497 pCol->pTab = pExpr->pTab;
drh13449892005-09-07 21:22:45 +00002498 pCol->iTable = pExpr->iTable;
2499 pCol->iColumn = pExpr->iColumn;
2500 pCol->iMem = pParse->nMem++;
2501 pCol->iSorterColumn = -1;
drh5774b802005-09-07 22:48:16 +00002502 pCol->pExpr = pExpr;
drh13449892005-09-07 21:22:45 +00002503 if( pAggInfo->pGroupBy ){
2504 int j, n;
2505 ExprList *pGB = pAggInfo->pGroupBy;
2506 struct ExprList_item *pTerm = pGB->a;
2507 n = pGB->nExpr;
2508 for(j=0; j<n; j++, pTerm++){
2509 Expr *pE = pTerm->pExpr;
2510 if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable &&
2511 pE->iColumn==pExpr->iColumn ){
2512 pCol->iSorterColumn = j;
2513 break;
2514 }
2515 }
2516 }
2517 if( pCol->iSorterColumn<0 ){
2518 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
2519 }
2520 }
2521 /* There is now an entry for pExpr in pAggInfo->aCol[] (either
2522 ** because it was there before or because we just created it).
2523 ** Convert the pExpr to be a TK_AGG_COLUMN referring to that
2524 ** pAggInfo->aCol[] entry.
2525 */
2526 pExpr->pAggInfo = pAggInfo;
2527 pExpr->op = TK_AGG_COLUMN;
drh7f906d62007-03-12 23:48:52 +00002528 pExpr->iAgg = k;
drh13449892005-09-07 21:22:45 +00002529 break;
2530 } /* endif pExpr->iTable==pItem->iCursor */
2531 } /* end loop over pSrcList */
drh22827922000-06-06 17:27:05 +00002532 }
drh626a8792005-01-17 22:08:19 +00002533 return 1;
drh22827922000-06-06 17:27:05 +00002534 }
2535 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00002536 /* The pNC->nDepth==0 test causes aggregate functions in subqueries
2537 ** to be ignored */
danielk1977a58fdfb2005-02-08 07:50:40 +00002538 if( pNC->nDepth==0 ){
drh13449892005-09-07 21:22:45 +00002539 /* Check to see if pExpr is a duplicate of another aggregate
2540 ** function that is already in the pAggInfo structure
2541 */
2542 struct AggInfo_func *pItem = pAggInfo->aFunc;
2543 for(i=0; i<pAggInfo->nFunc; i++, pItem++){
2544 if( sqlite3ExprCompare(pItem->pExpr, pExpr) ){
danielk1977a58fdfb2005-02-08 07:50:40 +00002545 break;
2546 }
drh22827922000-06-06 17:27:05 +00002547 }
drh13449892005-09-07 21:22:45 +00002548 if( i>=pAggInfo->nFunc ){
2549 /* pExpr is original. Make a new entry in pAggInfo->aFunc[]
2550 */
danielk197714db2662006-01-09 16:12:04 +00002551 u8 enc = ENC(pParse->db);
danielk19771e536952007-08-16 10:09:01 +00002552 i = addAggInfoFunc(pParse->db, pAggInfo);
drh13449892005-09-07 21:22:45 +00002553 if( i>=0 ){
2554 pItem = &pAggInfo->aFunc[i];
2555 pItem->pExpr = pExpr;
2556 pItem->iMem = pParse->nMem++;
2557 pItem->pFunc = sqlite3FindFunction(pParse->db,
drh2646da72005-12-09 20:02:05 +00002558 (char*)pExpr->token.z, pExpr->token.n,
drh13449892005-09-07 21:22:45 +00002559 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
drhfd357972005-09-09 01:33:19 +00002560 if( pExpr->flags & EP_Distinct ){
2561 pItem->iDistinct = pParse->nTab++;
2562 }else{
2563 pItem->iDistinct = -1;
2564 }
drh13449892005-09-07 21:22:45 +00002565 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002566 }
drh13449892005-09-07 21:22:45 +00002567 /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
2568 */
danielk1977a58fdfb2005-02-08 07:50:40 +00002569 pExpr->iAgg = i;
drh13449892005-09-07 21:22:45 +00002570 pExpr->pAggInfo = pAggInfo;
danielk1977a58fdfb2005-02-08 07:50:40 +00002571 return 1;
drh22827922000-06-06 17:27:05 +00002572 }
drh22827922000-06-06 17:27:05 +00002573 }
2574 }
drh13449892005-09-07 21:22:45 +00002575
2576 /* Recursively walk subqueries looking for TK_COLUMN nodes that need
2577 ** to be changed to TK_AGG_COLUMN. But increment nDepth so that
2578 ** TK_AGG_FUNCTION nodes in subqueries will be unchanged.
2579 */
danielk1977a58fdfb2005-02-08 07:50:40 +00002580 if( pExpr->pSelect ){
2581 pNC->nDepth++;
2582 walkSelectExpr(pExpr->pSelect, analyzeAggregate, pNC);
2583 pNC->nDepth--;
2584 }
drh626a8792005-01-17 22:08:19 +00002585 return 0;
2586}
2587
2588/*
2589** Analyze the given expression looking for aggregate functions and
2590** for variables that need to be added to the pParse->aAgg[] array.
2591** Make additional entries to the pParse->aAgg[] array as necessary.
2592**
2593** This routine should only be called after the expression has been
2594** analyzed by sqlite3ExprResolveNames().
2595**
2596** If errors are seen, leave an error message in zErrMsg and return
2597** the number of errors.
2598*/
danielk1977a58fdfb2005-02-08 07:50:40 +00002599int sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
2600 int nErr = pNC->pParse->nErr;
2601 walkExprTree(pExpr, analyzeAggregate, pNC);
2602 return pNC->pParse->nErr - nErr;
drh22827922000-06-06 17:27:05 +00002603}
drh5d9a4af2005-08-30 00:54:01 +00002604
2605/*
2606** Call sqlite3ExprAnalyzeAggregates() for every expression in an
2607** expression list. Return the number of errors.
2608**
2609** If an error is found, the analysis is cut short.
2610*/
2611int sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){
2612 struct ExprList_item *pItem;
2613 int i;
2614 int nErr = 0;
2615 if( pList ){
2616 for(pItem=pList->a, i=0; nErr==0 && i<pList->nExpr; i++, pItem++){
2617 nErr += sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
2618 }
2619 }
2620 return nErr;
2621}