blob: e8a89f464bdd0bd6cc5e7ecaac1082609b73ab77 [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**
danielk1977e7259292006-01-13 06:33:23 +000015** $Id: expr.c,v 1.249 2006/01/13 06:33:24 danielk1977 Exp $
drhcce7d172000-05-31 15:34:51 +000016*/
17#include "sqliteInt.h"
drh04738cb2002-06-02 18:19:00 +000018#include <ctype.h>
drha2e00042002-01-22 03:13:42 +000019
danielk1977e014a832004-05-17 10:48:57 +000020/*
21** Return the 'affinity' of the expression pExpr if any.
22**
23** If pExpr is a column, a reference to a column via an 'AS' alias,
24** or a sub-select with a column as the return value, then the
25** affinity of that column is returned. Otherwise, 0x00 is returned,
26** indicating no affinity for the expression.
27**
28** i.e. the WHERE clause expresssions in the following statements all
29** have an affinity:
30**
31** CREATE TABLE t1(a);
32** SELECT * FROM t1 WHERE a;
33** SELECT a AS b FROM t1 WHERE b;
34** SELECT * FROM t1 WHERE (select a from t1);
35*/
danielk1977bf3b7212004-05-18 10:06:24 +000036char sqlite3ExprAffinity(Expr *pExpr){
drh487e2622005-06-25 18:42:14 +000037 int op = pExpr->op;
38 if( op==TK_AS ){
danielk1977bf3b7212004-05-18 10:06:24 +000039 return sqlite3ExprAffinity(pExpr->pLeft);
danielk1977a37cdde2004-05-16 11:15:36 +000040 }
drh487e2622005-06-25 18:42:14 +000041 if( op==TK_SELECT ){
danielk1977bf3b7212004-05-18 10:06:24 +000042 return sqlite3ExprAffinity(pExpr->pSelect->pEList->a[0].pExpr);
danielk1977a37cdde2004-05-16 11:15:36 +000043 }
drh487e2622005-06-25 18:42:14 +000044#ifndef SQLITE_OMIT_CAST
45 if( op==TK_CAST ){
drh8a512562005-11-14 22:29:05 +000046 return sqlite3AffinityType(&pExpr->token);
drh487e2622005-06-25 18:42:14 +000047 }
48#endif
danielk1977a37cdde2004-05-16 11:15:36 +000049 return pExpr->affinity;
50}
51
drh53db1452004-05-20 13:54:53 +000052/*
danielk19770202b292004-06-09 09:55:16 +000053** Return the default collation sequence for the expression pExpr. If
54** there is no default collation type, return 0.
55*/
danielk19777cedc8d2004-06-10 10:50:08 +000056CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
57 CollSeq *pColl = 0;
danielk19770202b292004-06-09 09:55:16 +000058 if( pExpr ){
danielk19777cedc8d2004-06-10 10:50:08 +000059 pColl = pExpr->pColl;
drh487e2622005-06-25 18:42:14 +000060 if( (pExpr->op==TK_AS || pExpr->op==TK_CAST) && !pColl ){
danielk19777cedc8d2004-06-10 10:50:08 +000061 return sqlite3ExprCollSeq(pParse, pExpr->pLeft);
danielk19770202b292004-06-09 09:55:16 +000062 }
63 }
danielk19777cedc8d2004-06-10 10:50:08 +000064 if( sqlite3CheckCollSeq(pParse, pColl) ){
65 pColl = 0;
66 }
67 return pColl;
danielk19770202b292004-06-09 09:55:16 +000068}
69
70/*
drh626a8792005-01-17 22:08:19 +000071** pExpr is an operand of a comparison operator. aff2 is the
72** type affinity of the other operand. This routine returns the
drh53db1452004-05-20 13:54:53 +000073** type affinity that should be used for the comparison operator.
74*/
danielk1977e014a832004-05-17 10:48:57 +000075char sqlite3CompareAffinity(Expr *pExpr, char aff2){
danielk1977bf3b7212004-05-18 10:06:24 +000076 char aff1 = sqlite3ExprAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +000077 if( aff1 && aff2 ){
drh8df447f2005-11-01 15:48:24 +000078 /* Both sides of the comparison are columns. If one has numeric
79 ** affinity, use that. Otherwise use no affinity.
danielk1977e014a832004-05-17 10:48:57 +000080 */
drh8a512562005-11-14 22:29:05 +000081 if( sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2) ){
danielk1977e014a832004-05-17 10:48:57 +000082 return SQLITE_AFF_NUMERIC;
83 }else{
84 return SQLITE_AFF_NONE;
85 }
86 }else if( !aff1 && !aff2 ){
drh5f6a87b2004-07-19 00:39:45 +000087 /* Neither side of the comparison is a column. Compare the
88 ** results directly.
danielk1977e014a832004-05-17 10:48:57 +000089 */
drh5f6a87b2004-07-19 00:39:45 +000090 return SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +000091 }else{
92 /* One side is a column, the other is not. Use the columns affinity. */
drhfe05af82005-07-21 03:14:59 +000093 assert( aff1==0 || aff2==0 );
danielk1977e014a832004-05-17 10:48:57 +000094 return (aff1 + aff2);
95 }
96}
97
drh53db1452004-05-20 13:54:53 +000098/*
99** pExpr is a comparison operator. Return the type affinity that should
100** be applied to both operands prior to doing the comparison.
101*/
danielk1977e014a832004-05-17 10:48:57 +0000102static char comparisonAffinity(Expr *pExpr){
103 char aff;
104 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
105 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
106 pExpr->op==TK_NE );
107 assert( pExpr->pLeft );
danielk1977bf3b7212004-05-18 10:06:24 +0000108 aff = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +0000109 if( pExpr->pRight ){
110 aff = sqlite3CompareAffinity(pExpr->pRight, aff);
111 }
112 else if( pExpr->pSelect ){
113 aff = sqlite3CompareAffinity(pExpr->pSelect->pEList->a[0].pExpr, aff);
114 }
115 else if( !aff ){
116 aff = SQLITE_AFF_NUMERIC;
117 }
118 return aff;
119}
120
121/*
122** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
123** idx_affinity is the affinity of an indexed column. Return true
124** if the index with affinity idx_affinity may be used to implement
125** the comparison in pExpr.
126*/
127int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){
128 char aff = comparisonAffinity(pExpr);
drh8a512562005-11-14 22:29:05 +0000129 switch( aff ){
130 case SQLITE_AFF_NONE:
131 return 1;
132 case SQLITE_AFF_TEXT:
133 return idx_affinity==SQLITE_AFF_TEXT;
134 default:
135 return sqlite3IsNumericAffinity(idx_affinity);
136 }
danielk1977e014a832004-05-17 10:48:57 +0000137}
138
danielk1977a37cdde2004-05-16 11:15:36 +0000139/*
140** Return the P1 value that should be used for a binary comparison
141** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
142** If jumpIfNull is true, then set the low byte of the returned
143** P1 value to tell the opcode to jump if either expression
144** evaluates to NULL.
145*/
danielk1977e014a832004-05-17 10:48:57 +0000146static int binaryCompareP1(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){
danielk1977bf3b7212004-05-18 10:06:24 +0000147 char aff = sqlite3ExprAffinity(pExpr2);
drhf0863fe2005-06-12 21:35:51 +0000148 return ((int)sqlite3CompareAffinity(pExpr1, aff))+(jumpIfNull?0x100:0);
danielk1977a37cdde2004-05-16 11:15:36 +0000149}
150
drha2e00042002-01-22 03:13:42 +0000151/*
danielk19770202b292004-06-09 09:55:16 +0000152** Return a pointer to the collation sequence that should be used by
153** a binary comparison operator comparing pLeft and pRight.
154**
155** If the left hand expression has a collating sequence type, then it is
156** used. Otherwise the collation sequence for the right hand expression
157** is used, or the default (BINARY) if neither expression has a collating
158** type.
159*/
danielk19777cedc8d2004-06-10 10:50:08 +0000160static CollSeq* binaryCompareCollSeq(Parse *pParse, Expr *pLeft, Expr *pRight){
161 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pLeft);
danielk19770202b292004-06-09 09:55:16 +0000162 if( !pColl ){
danielk19777cedc8d2004-06-10 10:50:08 +0000163 pColl = sqlite3ExprCollSeq(pParse, pRight);
danielk19770202b292004-06-09 09:55:16 +0000164 }
165 return pColl;
166}
167
168/*
drhbe5c89a2004-07-26 00:31:09 +0000169** Generate code for a comparison operator.
170*/
171static int codeCompare(
172 Parse *pParse, /* The parsing (and code generating) context */
173 Expr *pLeft, /* The left operand */
174 Expr *pRight, /* The right operand */
175 int opcode, /* The comparison opcode */
176 int dest, /* Jump here if true. */
177 int jumpIfNull /* If true, jump if either operand is NULL */
178){
179 int p1 = binaryCompareP1(pLeft, pRight, jumpIfNull);
180 CollSeq *p3 = binaryCompareCollSeq(pParse, pLeft, pRight);
drh94a11212004-09-25 13:12:14 +0000181 return sqlite3VdbeOp3(pParse->pVdbe, opcode, p1, dest, (void*)p3, P3_COLLSEQ);
drhbe5c89a2004-07-26 00:31:09 +0000182}
183
184/*
drha76b5df2002-02-23 02:32:10 +0000185** Construct a new expression node and return a pointer to it. Memory
186** for this node is obtained from sqliteMalloc(). The calling function
187** is responsible for making sure the node eventually gets freed.
188*/
drhe4e72072004-11-23 01:47:30 +0000189Expr *sqlite3Expr(int op, Expr *pLeft, Expr *pRight, const Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000190 Expr *pNew;
191 pNew = sqliteMalloc( sizeof(Expr) );
192 if( pNew==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000193 /* When malloc fails, delete pLeft and pRight. Expressions passed to
194 ** this function must always be allocated with sqlite3Expr() for this
195 ** reason.
196 */
197 sqlite3ExprDelete(pLeft);
198 sqlite3ExprDelete(pRight);
drha76b5df2002-02-23 02:32:10 +0000199 return 0;
200 }
201 pNew->op = op;
202 pNew->pLeft = pLeft;
203 pNew->pRight = pRight;
danielk1977a58fdfb2005-02-08 07:50:40 +0000204 pNew->iAgg = -1;
drha76b5df2002-02-23 02:32:10 +0000205 if( pToken ){
drh4b59ab52002-08-24 18:24:51 +0000206 assert( pToken->dyn==0 );
drh145716b2004-09-24 12:24:06 +0000207 pNew->span = pNew->token = *pToken;
208 }else if( pLeft && pRight ){
209 sqlite3ExprSpan(pNew, &pLeft->span, &pRight->span);
drha76b5df2002-02-23 02:32:10 +0000210 }
drha76b5df2002-02-23 02:32:10 +0000211 return pNew;
212}
213
214/*
drh4e0cff62004-11-05 05:10:28 +0000215** When doing a nested parse, you can include terms in an expression
216** that look like this: #0 #1 #2 ... These terms refer to elements
drh288d37f2005-06-22 08:48:06 +0000217** on the stack. "#0" means the top of the stack.
218** "#1" means the next down on the stack. And so forth.
drh4e0cff62004-11-05 05:10:28 +0000219**
220** This routine is called by the parser to deal with on of those terms.
221** It immediately generates code to store the value in a memory location.
222** The returns an expression that will code to extract the value from
223** that memory location as needed.
224*/
225Expr *sqlite3RegisterExpr(Parse *pParse, Token *pToken){
226 Vdbe *v = pParse->pVdbe;
227 Expr *p;
228 int depth;
drh4e0cff62004-11-05 05:10:28 +0000229 if( pParse->nested==0 ){
230 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", pToken);
231 return 0;
232 }
drhbb7ac002005-08-12 22:58:53 +0000233 if( v==0 ) return 0;
drh4e0cff62004-11-05 05:10:28 +0000234 p = sqlite3Expr(TK_REGISTER, 0, 0, pToken);
drh73c42a12004-11-20 18:13:10 +0000235 if( p==0 ){
236 return 0; /* Malloc failed */
237 }
drh2646da72005-12-09 20:02:05 +0000238 depth = atoi((char*)&pToken->z[1]);
drh288d37f2005-06-22 08:48:06 +0000239 p->iTable = pParse->nMem++;
240 sqlite3VdbeAddOp(v, OP_Dup, depth, 0);
241 sqlite3VdbeAddOp(v, OP_MemStore, p->iTable, 1);
drh4e0cff62004-11-05 05:10:28 +0000242 return p;
243}
244
245/*
drh91bb0ee2004-09-01 03:06:34 +0000246** Join two expressions using an AND operator. If either expression is
247** NULL, then just return the other expression.
248*/
249Expr *sqlite3ExprAnd(Expr *pLeft, Expr *pRight){
250 if( pLeft==0 ){
251 return pRight;
252 }else if( pRight==0 ){
253 return pLeft;
254 }else{
255 return sqlite3Expr(TK_AND, pLeft, pRight, 0);
256 }
257}
258
259/*
drh6977fea2002-10-22 23:38:04 +0000260** Set the Expr.span field of the given expression to span all
drha76b5df2002-02-23 02:32:10 +0000261** text between the two given tokens.
262*/
danielk19774adee202004-05-08 08:23:19 +0000263void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
drh4efc4752004-01-16 15:55:37 +0000264 assert( pRight!=0 );
265 assert( pLeft!=0 );
drh6f7adc82006-01-11 21:41:20 +0000266 if( !sqlite3ThreadDataReadOnly()->mallocFailed && pRight->z && pLeft->z ){
drhad6d9462004-09-19 02:15:24 +0000267 assert( pLeft->dyn==0 || pLeft->z[pLeft->n]==0 );
drh145716b2004-09-24 12:24:06 +0000268 if( pLeft->dyn==0 && pRight->dyn==0 ){
drh6977fea2002-10-22 23:38:04 +0000269 pExpr->span.z = pLeft->z;
drh97903fe2005-05-24 20:19:57 +0000270 pExpr->span.n = pRight->n + (pRight->z - pLeft->z);
drh4b59ab52002-08-24 18:24:51 +0000271 }else{
drh6977fea2002-10-22 23:38:04 +0000272 pExpr->span.z = 0;
drh4b59ab52002-08-24 18:24:51 +0000273 }
drha76b5df2002-02-23 02:32:10 +0000274 }
275}
276
277/*
278** Construct a new expression node for a function with multiple
279** arguments.
280*/
danielk19774adee202004-05-08 08:23:19 +0000281Expr *sqlite3ExprFunction(ExprList *pList, Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000282 Expr *pNew;
283 pNew = sqliteMalloc( sizeof(Expr) );
284 if( pNew==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000285 sqlite3ExprListDelete(pList); /* Avoid leaking memory when malloc fails */
drha76b5df2002-02-23 02:32:10 +0000286 return 0;
287 }
288 pNew->op = TK_FUNCTION;
289 pNew->pList = pList;
290 if( pToken ){
drh4b59ab52002-08-24 18:24:51 +0000291 assert( pToken->dyn==0 );
drha76b5df2002-02-23 02:32:10 +0000292 pNew->token = *pToken;
293 }else{
294 pNew->token.z = 0;
drha76b5df2002-02-23 02:32:10 +0000295 }
drh6977fea2002-10-22 23:38:04 +0000296 pNew->span = pNew->token;
drha76b5df2002-02-23 02:32:10 +0000297 return pNew;
298}
299
300/*
drhfa6bc002004-09-07 16:19:52 +0000301** Assign a variable number to an expression that encodes a wildcard
302** in the original SQL statement.
303**
304** Wildcards consisting of a single "?" are assigned the next sequential
305** variable number.
306**
307** Wildcards of the form "?nnn" are assigned the number "nnn". We make
308** sure "nnn" is not too be to avoid a denial of service attack when
309** the SQL statement comes from an external source.
310**
311** Wildcards of the form ":aaa" or "$aaa" are assigned the same number
312** as the previous instance of the same wildcard. Or if this is the first
313** instance of the wildcard, the next sequenial variable number is
314** assigned.
315*/
316void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){
317 Token *pToken;
318 if( pExpr==0 ) return;
319 pToken = &pExpr->token;
320 assert( pToken->n>=1 );
321 assert( pToken->z!=0 );
322 assert( pToken->z[0]!=0 );
323 if( pToken->n==1 ){
324 /* Wildcard of the form "?". Assign the next variable number */
325 pExpr->iTable = ++pParse->nVar;
326 }else if( pToken->z[0]=='?' ){
327 /* Wildcard of the form "?nnn". Convert "nnn" to an integer and
328 ** use it as the variable number */
329 int i;
drh2646da72005-12-09 20:02:05 +0000330 pExpr->iTable = i = atoi((char*)&pToken->z[1]);
drhfa6bc002004-09-07 16:19:52 +0000331 if( i<1 || i>SQLITE_MAX_VARIABLE_NUMBER ){
332 sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
333 SQLITE_MAX_VARIABLE_NUMBER);
334 }
335 if( i>pParse->nVar ){
336 pParse->nVar = i;
337 }
338 }else{
339 /* Wildcards of the form ":aaa" or "$aaa". Reuse the same variable
340 ** number as the prior appearance of the same name, or if the name
341 ** has never appeared before, reuse the same variable number
342 */
343 int i, n;
344 n = pToken->n;
345 for(i=0; i<pParse->nVarExpr; i++){
346 Expr *pE;
347 if( (pE = pParse->apVarExpr[i])!=0
348 && pE->token.n==n
349 && memcmp(pE->token.z, pToken->z, n)==0 ){
350 pExpr->iTable = pE->iTable;
351 break;
352 }
353 }
354 if( i>=pParse->nVarExpr ){
355 pExpr->iTable = ++pParse->nVar;
356 if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){
357 pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10;
danielk1977e7259292006-01-13 06:33:23 +0000358 sqliteReallocOrFree((void**)&pParse->apVarExpr,
drhfa6bc002004-09-07 16:19:52 +0000359 pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0]) );
360 }
drh6f7adc82006-01-11 21:41:20 +0000361 if( !sqlite3ThreadDataReadOnly()->mallocFailed ){
drhfa6bc002004-09-07 16:19:52 +0000362 assert( pParse->apVarExpr!=0 );
363 pParse->apVarExpr[pParse->nVarExpr++] = pExpr;
364 }
365 }
366 }
367}
368
369/*
drha2e00042002-01-22 03:13:42 +0000370** Recursively delete an expression tree.
371*/
danielk19774adee202004-05-08 08:23:19 +0000372void sqlite3ExprDelete(Expr *p){
drha2e00042002-01-22 03:13:42 +0000373 if( p==0 ) return;
drh4efc4752004-01-16 15:55:37 +0000374 if( p->span.dyn ) sqliteFree((char*)p->span.z);
375 if( p->token.dyn ) sqliteFree((char*)p->token.z);
danielk19774adee202004-05-08 08:23:19 +0000376 sqlite3ExprDelete(p->pLeft);
377 sqlite3ExprDelete(p->pRight);
378 sqlite3ExprListDelete(p->pList);
379 sqlite3SelectDelete(p->pSelect);
drha2e00042002-01-22 03:13:42 +0000380 sqliteFree(p);
381}
382
drhd2687b72005-08-12 22:56:09 +0000383/*
384** The Expr.token field might be a string literal that is quoted.
385** If so, remove the quotation marks.
386*/
387void sqlite3DequoteExpr(Expr *p){
388 if( ExprHasAnyProperty(p, EP_Dequoted) ){
389 return;
390 }
391 ExprSetProperty(p, EP_Dequoted);
392 if( p->token.dyn==0 ){
drhd2687b72005-08-12 22:56:09 +0000393 sqlite3TokenCopy(&p->token, &p->token);
394 }
395 sqlite3Dequote((char*)p->token.z);
396}
397
drha76b5df2002-02-23 02:32:10 +0000398
399/*
drhff78bd22002-02-27 01:47:11 +0000400** The following group of routines make deep copies of expressions,
401** expression lists, ID lists, and select statements. The copies can
402** be deleted (by being passed to their respective ...Delete() routines)
403** without effecting the originals.
404**
danielk19774adee202004-05-08 08:23:19 +0000405** The expression list, ID, and source lists return by sqlite3ExprListDup(),
406** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
drhad3cab52002-05-24 02:04:32 +0000407** by subsequent calls to sqlite*ListAppend() routines.
drhff78bd22002-02-27 01:47:11 +0000408**
drhad3cab52002-05-24 02:04:32 +0000409** Any tables that the SrcList might point to are not duplicated.
drhff78bd22002-02-27 01:47:11 +0000410*/
danielk19774adee202004-05-08 08:23:19 +0000411Expr *sqlite3ExprDup(Expr *p){
drhff78bd22002-02-27 01:47:11 +0000412 Expr *pNew;
413 if( p==0 ) return 0;
drhfcb78a42003-01-18 20:11:05 +0000414 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000415 if( pNew==0 ) return 0;
drh3b167c72002-06-28 12:18:47 +0000416 memcpy(pNew, p, sizeof(*pNew));
drh6977fea2002-10-22 23:38:04 +0000417 if( p->token.z!=0 ){
drh2646da72005-12-09 20:02:05 +0000418 pNew->token.z = (u8*)sqliteStrNDup((char*)p->token.z, p->token.n);
drh4b59ab52002-08-24 18:24:51 +0000419 pNew->token.dyn = 1;
420 }else{
drh4efc4752004-01-16 15:55:37 +0000421 assert( pNew->token.z==0 );
drh4b59ab52002-08-24 18:24:51 +0000422 }
drh6977fea2002-10-22 23:38:04 +0000423 pNew->span.z = 0;
danielk19774adee202004-05-08 08:23:19 +0000424 pNew->pLeft = sqlite3ExprDup(p->pLeft);
425 pNew->pRight = sqlite3ExprDup(p->pRight);
426 pNew->pList = sqlite3ExprListDup(p->pList);
427 pNew->pSelect = sqlite3SelectDup(p->pSelect);
danielk1977aee18ef2005-03-09 12:26:50 +0000428 pNew->pTab = p->pTab;
drhff78bd22002-02-27 01:47:11 +0000429 return pNew;
430}
danielk19774adee202004-05-08 08:23:19 +0000431void sqlite3TokenCopy(Token *pTo, Token *pFrom){
drh4b59ab52002-08-24 18:24:51 +0000432 if( pTo->dyn ) sqliteFree((char*)pTo->z);
drh4b59ab52002-08-24 18:24:51 +0000433 if( pFrom->z ){
434 pTo->n = pFrom->n;
drh2646da72005-12-09 20:02:05 +0000435 pTo->z = (u8*)sqliteStrNDup((char*)pFrom->z, pFrom->n);
drh4b59ab52002-08-24 18:24:51 +0000436 pTo->dyn = 1;
437 }else{
drh4b59ab52002-08-24 18:24:51 +0000438 pTo->z = 0;
drh4b59ab52002-08-24 18:24:51 +0000439 }
440}
danielk19774adee202004-05-08 08:23:19 +0000441ExprList *sqlite3ExprListDup(ExprList *p){
drhff78bd22002-02-27 01:47:11 +0000442 ExprList *pNew;
drh145716b2004-09-24 12:24:06 +0000443 struct ExprList_item *pItem, *pOldItem;
drhff78bd22002-02-27 01:47:11 +0000444 int i;
445 if( p==0 ) return 0;
446 pNew = sqliteMalloc( sizeof(*pNew) );
447 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000448 pNew->nExpr = pNew->nAlloc = p->nExpr;
drh3e7bc9c2004-02-21 19:17:17 +0000449 pNew->a = pItem = sqliteMalloc( p->nExpr*sizeof(p->a[0]) );
danielk1977e0048402004-06-15 16:51:01 +0000450 if( pItem==0 ){
451 sqliteFree(pNew);
452 return 0;
453 }
drh145716b2004-09-24 12:24:06 +0000454 pOldItem = p->a;
455 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
drh4b59ab52002-08-24 18:24:51 +0000456 Expr *pNewExpr, *pOldExpr;
drh145716b2004-09-24 12:24:06 +0000457 pItem->pExpr = pNewExpr = sqlite3ExprDup(pOldExpr = pOldItem->pExpr);
drh6977fea2002-10-22 23:38:04 +0000458 if( pOldExpr->span.z!=0 && pNewExpr ){
459 /* Always make a copy of the span for top-level expressions in the
drh4b59ab52002-08-24 18:24:51 +0000460 ** expression list. The logic in SELECT processing that determines
461 ** the names of columns in the result set needs this information */
danielk19774adee202004-05-08 08:23:19 +0000462 sqlite3TokenCopy(&pNewExpr->span, &pOldExpr->span);
drh4b59ab52002-08-24 18:24:51 +0000463 }
drh1f3e9052002-10-31 00:09:39 +0000464 assert( pNewExpr==0 || pNewExpr->span.z!=0
drh6f7adc82006-01-11 21:41:20 +0000465 || pOldExpr->span.z==0
466 || sqlite3ThreadDataReadOnly()->mallocFailed );
drh145716b2004-09-24 12:24:06 +0000467 pItem->zName = sqliteStrDup(pOldItem->zName);
468 pItem->sortOrder = pOldItem->sortOrder;
469 pItem->isAgg = pOldItem->isAgg;
drh3e7bc9c2004-02-21 19:17:17 +0000470 pItem->done = 0;
drhff78bd22002-02-27 01:47:11 +0000471 }
472 return pNew;
473}
danielk197793758c82005-01-21 08:13:14 +0000474
475/*
476** If cursors, triggers, views and subqueries are all omitted from
477** the build, then none of the following routines, except for
478** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes
479** called with a NULL argument.
480*/
danielk19776a67fe82005-02-04 04:07:16 +0000481#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \
482 || !defined(SQLITE_OMIT_SUBQUERY)
danielk19774adee202004-05-08 08:23:19 +0000483SrcList *sqlite3SrcListDup(SrcList *p){
drhad3cab52002-05-24 02:04:32 +0000484 SrcList *pNew;
485 int i;
drh113088e2003-03-20 01:16:58 +0000486 int nByte;
drhad3cab52002-05-24 02:04:32 +0000487 if( p==0 ) return 0;
drh113088e2003-03-20 01:16:58 +0000488 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
drh4efc4752004-01-16 15:55:37 +0000489 pNew = sqliteMallocRaw( nByte );
drhad3cab52002-05-24 02:04:32 +0000490 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000491 pNew->nSrc = pNew->nAlloc = p->nSrc;
drhad3cab52002-05-24 02:04:32 +0000492 for(i=0; i<p->nSrc; i++){
drh4efc4752004-01-16 15:55:37 +0000493 struct SrcList_item *pNewItem = &pNew->a[i];
494 struct SrcList_item *pOldItem = &p->a[i];
drhed8a3bb2005-06-06 21:19:56 +0000495 Table *pTab;
drh4efc4752004-01-16 15:55:37 +0000496 pNewItem->zDatabase = sqliteStrDup(pOldItem->zDatabase);
497 pNewItem->zName = sqliteStrDup(pOldItem->zName);
498 pNewItem->zAlias = sqliteStrDup(pOldItem->zAlias);
499 pNewItem->jointype = pOldItem->jointype;
500 pNewItem->iCursor = pOldItem->iCursor;
drhed8a3bb2005-06-06 21:19:56 +0000501 pTab = pNewItem->pTab = pOldItem->pTab;
502 if( pTab ){
503 pTab->nRef++;
danielk1977a1cb1832005-02-12 08:59:55 +0000504 }
danielk19774adee202004-05-08 08:23:19 +0000505 pNewItem->pSelect = sqlite3SelectDup(pOldItem->pSelect);
506 pNewItem->pOn = sqlite3ExprDup(pOldItem->pOn);
507 pNewItem->pUsing = sqlite3IdListDup(pOldItem->pUsing);
danielk19776c18b6e2005-01-30 09:17:58 +0000508 pNewItem->colUsed = pOldItem->colUsed;
drhad3cab52002-05-24 02:04:32 +0000509 }
510 return pNew;
511}
danielk19774adee202004-05-08 08:23:19 +0000512IdList *sqlite3IdListDup(IdList *p){
drhff78bd22002-02-27 01:47:11 +0000513 IdList *pNew;
514 int i;
515 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000516 pNew = sqliteMallocRaw( sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +0000517 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000518 pNew->nId = pNew->nAlloc = p->nId;
drh4efc4752004-01-16 15:55:37 +0000519 pNew->a = sqliteMallocRaw( p->nId*sizeof(p->a[0]) );
danielk1977d5d56522005-03-16 12:15:20 +0000520 if( pNew->a==0 ){
521 sqliteFree(pNew);
522 return 0;
523 }
drhff78bd22002-02-27 01:47:11 +0000524 for(i=0; i<p->nId; i++){
drh4efc4752004-01-16 15:55:37 +0000525 struct IdList_item *pNewItem = &pNew->a[i];
526 struct IdList_item *pOldItem = &p->a[i];
527 pNewItem->zName = sqliteStrDup(pOldItem->zName);
528 pNewItem->idx = pOldItem->idx;
drhff78bd22002-02-27 01:47:11 +0000529 }
530 return pNew;
531}
danielk19774adee202004-05-08 08:23:19 +0000532Select *sqlite3SelectDup(Select *p){
drhff78bd22002-02-27 01:47:11 +0000533 Select *pNew;
534 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000535 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000536 if( pNew==0 ) return 0;
537 pNew->isDistinct = p->isDistinct;
danielk19774adee202004-05-08 08:23:19 +0000538 pNew->pEList = sqlite3ExprListDup(p->pEList);
539 pNew->pSrc = sqlite3SrcListDup(p->pSrc);
540 pNew->pWhere = sqlite3ExprDup(p->pWhere);
541 pNew->pGroupBy = sqlite3ExprListDup(p->pGroupBy);
542 pNew->pHaving = sqlite3ExprDup(p->pHaving);
543 pNew->pOrderBy = sqlite3ExprListDup(p->pOrderBy);
drhff78bd22002-02-27 01:47:11 +0000544 pNew->op = p->op;
danielk19774adee202004-05-08 08:23:19 +0000545 pNew->pPrior = sqlite3SelectDup(p->pPrior);
danielk1977a2dc3b12005-02-05 12:48:48 +0000546 pNew->pLimit = sqlite3ExprDup(p->pLimit);
547 pNew->pOffset = sqlite3ExprDup(p->pOffset);
drh7b58dae2003-07-20 01:16:46 +0000548 pNew->iLimit = -1;
549 pNew->iOffset = -1;
danielk1977a1cb1832005-02-12 08:59:55 +0000550 pNew->isResolved = p->isResolved;
551 pNew->isAgg = p->isAgg;
drh8e647b82005-09-23 21:11:53 +0000552 pNew->usesVirt = 0;
553 pNew->disallowOrderBy = 0;
drh0342b1f2005-09-01 03:07:44 +0000554 pNew->pRightmost = 0;
555 pNew->addrOpenVirt[0] = -1;
556 pNew->addrOpenVirt[1] = -1;
557 pNew->addrOpenVirt[2] = -1;
drhff78bd22002-02-27 01:47:11 +0000558 return pNew;
559}
danielk197793758c82005-01-21 08:13:14 +0000560#else
561Select *sqlite3SelectDup(Select *p){
562 assert( p==0 );
563 return 0;
564}
565#endif
drhff78bd22002-02-27 01:47:11 +0000566
567
568/*
drha76b5df2002-02-23 02:32:10 +0000569** Add a new element to the end of an expression list. If pList is
570** initially NULL, then create a new expression list.
571*/
danielk19774adee202004-05-08 08:23:19 +0000572ExprList *sqlite3ExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){
drha76b5df2002-02-23 02:32:10 +0000573 if( pList==0 ){
574 pList = sqliteMalloc( sizeof(ExprList) );
575 if( pList==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000576 goto no_mem;
drha76b5df2002-02-23 02:32:10 +0000577 }
drh4efc4752004-01-16 15:55:37 +0000578 assert( pList->nAlloc==0 );
drha76b5df2002-02-23 02:32:10 +0000579 }
drh4305d102003-07-30 12:34:12 +0000580 if( pList->nAlloc<=pList->nExpr ){
danielk1977d5d56522005-03-16 12:15:20 +0000581 struct ExprList_item *a;
582 int n = pList->nAlloc*2 + 4;
583 a = sqliteRealloc(pList->a, n*sizeof(pList->a[0]));
584 if( a==0 ){
585 goto no_mem;
drha76b5df2002-02-23 02:32:10 +0000586 }
danielk1977d5d56522005-03-16 12:15:20 +0000587 pList->a = a;
588 pList->nAlloc = n;
drha76b5df2002-02-23 02:32:10 +0000589 }
drh4efc4752004-01-16 15:55:37 +0000590 assert( pList->a!=0 );
591 if( pExpr || pName ){
592 struct ExprList_item *pItem = &pList->a[pList->nExpr++];
593 memset(pItem, 0, sizeof(*pItem));
drha99db3b2004-06-19 14:49:12 +0000594 pItem->zName = sqlite3NameFromToken(pName);
danielk1977e94ddc92005-03-21 03:53:38 +0000595 pItem->pExpr = pExpr;
drha76b5df2002-02-23 02:32:10 +0000596 }
597 return pList;
danielk1977d5d56522005-03-16 12:15:20 +0000598
599no_mem:
600 /* Avoid leaking memory if malloc has failed. */
601 sqlite3ExprDelete(pExpr);
602 sqlite3ExprListDelete(pList);
603 return 0;
drha76b5df2002-02-23 02:32:10 +0000604}
605
606/*
607** Delete an entire expression list.
608*/
danielk19774adee202004-05-08 08:23:19 +0000609void sqlite3ExprListDelete(ExprList *pList){
drha76b5df2002-02-23 02:32:10 +0000610 int i;
drhbe5c89a2004-07-26 00:31:09 +0000611 struct ExprList_item *pItem;
drha76b5df2002-02-23 02:32:10 +0000612 if( pList==0 ) return;
drh1bdd9b52004-04-23 17:04:44 +0000613 assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
614 assert( pList->nExpr<=pList->nAlloc );
drhbe5c89a2004-07-26 00:31:09 +0000615 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
616 sqlite3ExprDelete(pItem->pExpr);
617 sqliteFree(pItem->zName);
drha76b5df2002-02-23 02:32:10 +0000618 }
619 sqliteFree(pList->a);
620 sqliteFree(pList);
621}
622
623/*
drh626a8792005-01-17 22:08:19 +0000624** Walk an expression tree. Call xFunc for each node visited.
drh73b211a2005-01-18 04:00:42 +0000625**
drh626a8792005-01-17 22:08:19 +0000626** The return value from xFunc determines whether the tree walk continues.
627** 0 means continue walking the tree. 1 means do not walk children
628** of the current node but continue with siblings. 2 means abandon
629** the tree walk completely.
630**
631** The return value from this routine is 1 to abandon the tree walk
632** and 0 to continue.
drh87abf5c2005-08-25 12:45:04 +0000633**
634** NOTICE: This routine does *not* descend into subqueries.
drh626a8792005-01-17 22:08:19 +0000635*/
danielk1977a58fdfb2005-02-08 07:50:40 +0000636static int walkExprList(ExprList *, int (*)(void *, Expr*), void *);
drh626a8792005-01-17 22:08:19 +0000637static int walkExprTree(Expr *pExpr, int (*xFunc)(void*,Expr*), void *pArg){
drh626a8792005-01-17 22:08:19 +0000638 int rc;
639 if( pExpr==0 ) return 0;
640 rc = (*xFunc)(pArg, pExpr);
641 if( rc==0 ){
642 if( walkExprTree(pExpr->pLeft, xFunc, pArg) ) return 1;
643 if( walkExprTree(pExpr->pRight, xFunc, pArg) ) return 1;
danielk1977a58fdfb2005-02-08 07:50:40 +0000644 if( walkExprList(pExpr->pList, xFunc, pArg) ) return 1;
drh626a8792005-01-17 22:08:19 +0000645 }
646 return rc>1;
647}
648
649/*
danielk1977a58fdfb2005-02-08 07:50:40 +0000650** Call walkExprTree() for every expression in list p.
651*/
652static int walkExprList(ExprList *p, int (*xFunc)(void *, Expr*), void *pArg){
653 int i;
654 struct ExprList_item *pItem;
655 if( !p ) return 0;
656 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
657 if( walkExprTree(pItem->pExpr, xFunc, pArg) ) return 1;
658 }
659 return 0;
660}
661
662/*
663** Call walkExprTree() for every expression in Select p, not including
664** expressions that are part of sub-selects in any FROM clause or the LIMIT
665** or OFFSET expressions..
666*/
667static int walkSelectExpr(Select *p, int (*xFunc)(void *, Expr*), void *pArg){
668 walkExprList(p->pEList, xFunc, pArg);
669 walkExprTree(p->pWhere, xFunc, pArg);
670 walkExprList(p->pGroupBy, xFunc, pArg);
671 walkExprTree(p->pHaving, xFunc, pArg);
672 walkExprList(p->pOrderBy, xFunc, pArg);
673 return 0;
674}
675
676
677/*
drh626a8792005-01-17 22:08:19 +0000678** This routine is designed as an xFunc for walkExprTree().
679**
680** pArg is really a pointer to an integer. If we can tell by looking
drh73b211a2005-01-18 04:00:42 +0000681** at pExpr that the expression that contains pExpr is not a constant
682** expression, then set *pArg to 0 and return 2 to abandon the tree walk.
683** If pExpr does does not disqualify the expression from being a constant
684** then do nothing.
685**
686** After walking the whole tree, if no nodes are found that disqualify
687** the expression as constant, then we assume the whole expression
688** is constant. See sqlite3ExprIsConstant() for additional information.
drh626a8792005-01-17 22:08:19 +0000689*/
690static int exprNodeIsConstant(void *pArg, Expr *pExpr){
691 switch( pExpr->op ){
drheb55bd22005-06-30 17:04:21 +0000692 /* Consider functions to be constant if all their arguments are constant
693 ** and *pArg==2 */
694 case TK_FUNCTION:
695 if( *((int*)pArg)==2 ) return 0;
696 /* Fall through */
drh626a8792005-01-17 22:08:19 +0000697 case TK_ID:
698 case TK_COLUMN:
699 case TK_DOT:
700 case TK_AGG_FUNCTION:
drh13449892005-09-07 21:22:45 +0000701 case TK_AGG_COLUMN:
drhfe2093d2005-01-20 22:48:47 +0000702#ifndef SQLITE_OMIT_SUBQUERY
703 case TK_SELECT:
704 case TK_EXISTS:
705#endif
drh626a8792005-01-17 22:08:19 +0000706 *((int*)pArg) = 0;
707 return 2;
drh87abf5c2005-08-25 12:45:04 +0000708 case TK_IN:
709 if( pExpr->pSelect ){
710 *((int*)pArg) = 0;
711 return 2;
712 }
drh626a8792005-01-17 22:08:19 +0000713 default:
714 return 0;
715 }
716}
717
718/*
drhfef52082000-06-06 01:50:43 +0000719** Walk an expression tree. Return 1 if the expression is constant
drheb55bd22005-06-30 17:04:21 +0000720** and 0 if it involves variables or function calls.
drh23989372002-05-21 13:43:04 +0000721**
722** For the purposes of this function, a double-quoted string (ex: "abc")
723** is considered a variable but a single-quoted string (ex: 'abc') is
724** a constant.
drhfef52082000-06-06 01:50:43 +0000725*/
danielk19774adee202004-05-08 08:23:19 +0000726int sqlite3ExprIsConstant(Expr *p){
drh626a8792005-01-17 22:08:19 +0000727 int isConst = 1;
728 walkExprTree(p, exprNodeIsConstant, &isConst);
729 return isConst;
drhfef52082000-06-06 01:50:43 +0000730}
731
732/*
drheb55bd22005-06-30 17:04:21 +0000733** Walk an expression tree. Return 1 if the expression is constant
734** or a function call with constant arguments. Return and 0 if there
735** are any variables.
736**
737** For the purposes of this function, a double-quoted string (ex: "abc")
738** is considered a variable but a single-quoted string (ex: 'abc') is
739** a constant.
740*/
741int sqlite3ExprIsConstantOrFunction(Expr *p){
742 int isConst = 2;
743 walkExprTree(p, exprNodeIsConstant, &isConst);
744 return isConst!=0;
745}
746
747/*
drh73b211a2005-01-18 04:00:42 +0000748** If the expression p codes a constant integer that is small enough
drh202b2df2004-01-06 01:13:46 +0000749** to fit in a 32-bit integer, return 1 and put the value of the integer
750** in *pValue. If the expression is not an integer or if it is too big
751** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
drhe4de1fe2002-06-02 16:09:01 +0000752*/
danielk19774adee202004-05-08 08:23:19 +0000753int sqlite3ExprIsInteger(Expr *p, int *pValue){
drhe4de1fe2002-06-02 16:09:01 +0000754 switch( p->op ){
755 case TK_INTEGER: {
drh2646da72005-12-09 20:02:05 +0000756 if( sqlite3GetInt32((char*)p->token.z, pValue) ){
drh202b2df2004-01-06 01:13:46 +0000757 return 1;
758 }
759 break;
drhe4de1fe2002-06-02 16:09:01 +0000760 }
drh4b59ab52002-08-24 18:24:51 +0000761 case TK_UPLUS: {
danielk19774adee202004-05-08 08:23:19 +0000762 return sqlite3ExprIsInteger(p->pLeft, pValue);
drh4b59ab52002-08-24 18:24:51 +0000763 }
drhe4de1fe2002-06-02 16:09:01 +0000764 case TK_UMINUS: {
765 int v;
danielk19774adee202004-05-08 08:23:19 +0000766 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
drhe4de1fe2002-06-02 16:09:01 +0000767 *pValue = -v;
768 return 1;
769 }
770 break;
771 }
772 default: break;
773 }
774 return 0;
775}
776
777/*
drhc4a3c772001-04-04 11:48:57 +0000778** Return TRUE if the given string is a row-id column name.
779*/
danielk19774adee202004-05-08 08:23:19 +0000780int sqlite3IsRowid(const char *z){
781 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
782 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
783 if( sqlite3StrICmp(z, "OID")==0 ) return 1;
drhc4a3c772001-04-04 11:48:57 +0000784 return 0;
785}
786
787/*
drh8141f612004-01-25 22:44:58 +0000788** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
789** that name in the set of source tables in pSrcList and make the pExpr
790** expression node refer back to that source column. The following changes
791** are made to pExpr:
792**
793** pExpr->iDb Set the index in db->aDb[] of the database holding
794** the table.
795** pExpr->iTable Set to the cursor number for the table obtained
796** from pSrcList.
797** pExpr->iColumn Set to the column number within the table.
drh8141f612004-01-25 22:44:58 +0000798** pExpr->op Set to TK_COLUMN.
799** pExpr->pLeft Any expression this points to is deleted
800** pExpr->pRight Any expression this points to is deleted.
801**
802** The pDbToken is the name of the database (the "X"). This value may be
803** NULL meaning that name is of the form Y.Z or Z. Any available database
804** can be used. The pTableToken is the name of the table (the "Y"). This
805** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it
806** means that the form of the name is Z and that columns from any table
807** can be used.
808**
809** If the name cannot be resolved unambiguously, leave an error message
810** in pParse and return non-zero. Return zero on success.
811*/
812static int lookupName(
drhffe07b22005-11-03 00:41:17 +0000813 Parse *pParse, /* The parsing context */
drh8141f612004-01-25 22:44:58 +0000814 Token *pDbToken, /* Name of the database containing table, or NULL */
815 Token *pTableToken, /* Name of table containing column, or NULL */
816 Token *pColumnToken, /* Name of the column. */
drh626a8792005-01-17 22:08:19 +0000817 NameContext *pNC, /* The name context used to resolve the name */
drh8141f612004-01-25 22:44:58 +0000818 Expr *pExpr /* Make this EXPR node point to the selected column */
819){
820 char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */
821 char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */
822 char *zCol = 0; /* Name of the column. The "Z" */
823 int i, j; /* Loop counters */
824 int cnt = 0; /* Number of matching column names */
825 int cntTab = 0; /* Number of matching table names */
drh626a8792005-01-17 22:08:19 +0000826 sqlite3 *db = pParse->db; /* The database */
drh51669862004-12-18 18:40:26 +0000827 struct SrcList_item *pItem; /* Use for looping over pSrcList items */
828 struct SrcList_item *pMatch = 0; /* The matching pSrcList item */
drh73b211a2005-01-18 04:00:42 +0000829 NameContext *pTopNC = pNC; /* First namecontext in the list */
drh8141f612004-01-25 22:44:58 +0000830
831 assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
drha99db3b2004-06-19 14:49:12 +0000832 zDb = sqlite3NameFromToken(pDbToken);
833 zTab = sqlite3NameFromToken(pTableToken);
834 zCol = sqlite3NameFromToken(pColumnToken);
drh6f7adc82006-01-11 21:41:20 +0000835 if( sqlite3ThreadDataReadOnly()->mallocFailed ){
danielk1977d5d56522005-03-16 12:15:20 +0000836 goto lookupname_end;
drh8141f612004-01-25 22:44:58 +0000837 }
drh8141f612004-01-25 22:44:58 +0000838
839 pExpr->iTable = -1;
drh626a8792005-01-17 22:08:19 +0000840 while( pNC && cnt==0 ){
drhffe07b22005-11-03 00:41:17 +0000841 ExprList *pEList;
drh626a8792005-01-17 22:08:19 +0000842 SrcList *pSrcList = pNC->pSrcList;
drh8141f612004-01-25 22:44:58 +0000843
danielk1977b3bce662005-01-29 08:32:43 +0000844 if( pSrcList ){
845 for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
846 Table *pTab = pItem->pTab;
danielk1977da184232006-01-05 11:34:32 +0000847 int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
danielk1977b3bce662005-01-29 08:32:43 +0000848 Column *pCol;
849
850 if( pTab==0 ) continue;
851 assert( pTab->nCol>0 );
852 if( zTab ){
853 if( pItem->zAlias ){
854 char *zTabName = pItem->zAlias;
855 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
856 }else{
857 char *zTabName = pTab->zName;
858 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
danielk1977da184232006-01-05 11:34:32 +0000859 if( zDb!=0 && sqlite3StrICmp(db->aDb[iDb].zName, zDb)!=0 ){
danielk1977b3bce662005-01-29 08:32:43 +0000860 continue;
861 }
drh626a8792005-01-17 22:08:19 +0000862 }
drh8141f612004-01-25 22:44:58 +0000863 }
danielk1977b3bce662005-01-29 08:32:43 +0000864 if( 0==(cntTab++) ){
drh626a8792005-01-17 22:08:19 +0000865 pExpr->iTable = pItem->iCursor;
danielk1977da184232006-01-05 11:34:32 +0000866 pExpr->pSchema = pTab->pSchema;
danielk1977b3bce662005-01-29 08:32:43 +0000867 pMatch = pItem;
868 }
869 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
870 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
danielk1977b3bf5562006-01-10 17:58:23 +0000871 const char *zColl = pTab->aCol[j].zColl;
drh873fac02005-06-06 17:11:46 +0000872 IdList *pUsing;
danielk1977b3bce662005-01-29 08:32:43 +0000873 cnt++;
874 pExpr->iTable = pItem->iCursor;
875 pMatch = pItem;
danielk1977da184232006-01-05 11:34:32 +0000876 pExpr->pSchema = pTab->pSchema;
danielk1977b3bce662005-01-29 08:32:43 +0000877 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
878 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
879 pExpr->affinity = pTab->aCol[j].affinity;
danielk1977b3bf5562006-01-10 17:58:23 +0000880 pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0);
drh355ef362005-06-06 16:59:24 +0000881 if( pItem->jointype & JT_NATURAL ){
882 /* If this match occurred in the left table of a natural join,
883 ** then skip the right table to avoid a duplicate match */
884 pItem++;
885 i++;
886 }
drh873fac02005-06-06 17:11:46 +0000887 if( (pUsing = pItem->pUsing)!=0 ){
888 /* If this match occurs on a column that is in the USING clause
889 ** of a join, skip the search of the right table of the join
890 ** to avoid a duplicate match there. */
891 int k;
892 for(k=0; k<pUsing->nId; k++){
893 if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ){
894 pItem++;
895 i++;
896 break;
897 }
898 }
899 }
danielk1977b3bce662005-01-29 08:32:43 +0000900 break;
901 }
drh8141f612004-01-25 22:44:58 +0000902 }
903 }
904 }
drh626a8792005-01-17 22:08:19 +0000905
906#ifndef SQLITE_OMIT_TRIGGER
907 /* If we have not already resolved the name, then maybe
908 ** it is a new.* or old.* trigger argument reference
909 */
910 if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
911 TriggerStack *pTriggerStack = pParse->trigStack;
912 Table *pTab = 0;
913 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){
914 pExpr->iTable = pTriggerStack->newIdx;
915 assert( pTriggerStack->pTab );
916 pTab = pTriggerStack->pTab;
917 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab)==0 ){
918 pExpr->iTable = pTriggerStack->oldIdx;
919 assert( pTriggerStack->pTab );
920 pTab = pTriggerStack->pTab;
921 }
922
923 if( pTab ){
924 int j;
925 Column *pCol = pTab->aCol;
926
danielk1977da184232006-01-05 11:34:32 +0000927 pExpr->pSchema = pTab->pSchema;
drh626a8792005-01-17 22:08:19 +0000928 cntTab++;
929 for(j=0; j < pTab->nCol; j++, pCol++) {
930 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
danielk1977b3bf5562006-01-10 17:58:23 +0000931 const char *zColl = pTab->aCol[j].zColl;
drh626a8792005-01-17 22:08:19 +0000932 cnt++;
933 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
934 pExpr->affinity = pTab->aCol[j].affinity;
danielk1977b3bf5562006-01-10 17:58:23 +0000935 pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0);
danielk1977aee18ef2005-03-09 12:26:50 +0000936 pExpr->pTab = pTab;
drh626a8792005-01-17 22:08:19 +0000937 break;
938 }
939 }
940 }
941 }
drhb7f91642004-10-31 02:22:47 +0000942#endif /* !defined(SQLITE_OMIT_TRIGGER) */
drh8141f612004-01-25 22:44:58 +0000943
drh626a8792005-01-17 22:08:19 +0000944 /*
945 ** Perhaps the name is a reference to the ROWID
946 */
947 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
948 cnt = 1;
949 pExpr->iColumn = -1;
drh8a512562005-11-14 22:29:05 +0000950 pExpr->affinity = SQLITE_AFF_INTEGER;
drh626a8792005-01-17 22:08:19 +0000951 }
drh8141f612004-01-25 22:44:58 +0000952
drh626a8792005-01-17 22:08:19 +0000953 /*
954 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
955 ** might refer to an result-set alias. This happens, for example, when
956 ** we are resolving names in the WHERE clause of the following command:
957 **
958 ** SELECT a+b AS x FROM table WHERE x<10;
959 **
960 ** In cases like this, replace pExpr with a copy of the expression that
961 ** forms the result set entry ("a+b" in the example) and return immediately.
962 ** Note that the expression in the result set should have already been
963 ** resolved by the time the WHERE clause is resolved.
964 */
drhffe07b22005-11-03 00:41:17 +0000965 if( cnt==0 && (pEList = pNC->pEList)!=0 && zTab==0 ){
drh626a8792005-01-17 22:08:19 +0000966 for(j=0; j<pEList->nExpr; j++){
967 char *zAs = pEList->a[j].zName;
968 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
969 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
970 pExpr->op = TK_AS;
971 pExpr->iColumn = j;
972 pExpr->pLeft = sqlite3ExprDup(pEList->a[j].pExpr);
drh15ccce12005-05-23 15:06:39 +0000973 cnt = 1;
drh626a8792005-01-17 22:08:19 +0000974 assert( zTab==0 && zDb==0 );
drh15ccce12005-05-23 15:06:39 +0000975 goto lookupname_end_2;
drh626a8792005-01-17 22:08:19 +0000976 }
977 }
978 }
979
980 /* Advance to the next name context. The loop will exit when either
981 ** we have a match (cnt>0) or when we run out of name contexts.
982 */
983 if( cnt==0 ){
984 pNC = pNC->pNext;
985 }
drh8141f612004-01-25 22:44:58 +0000986 }
987
988 /*
989 ** If X and Y are NULL (in other words if only the column name Z is
990 ** supplied) and the value of Z is enclosed in double-quotes, then
991 ** Z is a string literal if it doesn't match any column names. In that
992 ** case, we need to return right away and not make any changes to
993 ** pExpr.
drh15ccce12005-05-23 15:06:39 +0000994 **
995 ** Because no reference was made to outer contexts, the pNC->nRef
996 ** fields are not changed in any context.
drh8141f612004-01-25 22:44:58 +0000997 */
998 if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
999 sqliteFree(zCol);
1000 return 0;
1001 }
1002
1003 /*
1004 ** cnt==0 means there was not match. cnt>1 means there were two or
1005 ** more matches. Either way, we have an error.
1006 */
1007 if( cnt!=1 ){
1008 char *z = 0;
1009 char *zErr;
1010 zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s";
1011 if( zDb ){
drhf93339d2006-01-03 15:16:26 +00001012 sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, (char*)0);
drh8141f612004-01-25 22:44:58 +00001013 }else if( zTab ){
drhf93339d2006-01-03 15:16:26 +00001014 sqlite3SetString(&z, zTab, ".", zCol, (char*)0);
drh8141f612004-01-25 22:44:58 +00001015 }else{
1016 z = sqliteStrDup(zCol);
1017 }
danielk19774adee202004-05-08 08:23:19 +00001018 sqlite3ErrorMsg(pParse, zErr, z);
drh8141f612004-01-25 22:44:58 +00001019 sqliteFree(z);
drh73b211a2005-01-18 04:00:42 +00001020 pTopNC->nErr++;
drh8141f612004-01-25 22:44:58 +00001021 }
1022
drh51669862004-12-18 18:40:26 +00001023 /* If a column from a table in pSrcList is referenced, then record
1024 ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes
1025 ** bit 0 to be set. Column 1 sets bit 1. And so forth. If the
1026 ** column number is greater than the number of bits in the bitmask
1027 ** then set the high-order bit of the bitmask.
1028 */
1029 if( pExpr->iColumn>=0 && pMatch!=0 ){
1030 int n = pExpr->iColumn;
1031 if( n>=sizeof(Bitmask)*8 ){
1032 n = sizeof(Bitmask)*8-1;
1033 }
1034 assert( pMatch->iCursor==pExpr->iTable );
1035 pMatch->colUsed |= 1<<n;
1036 }
1037
danielk1977d5d56522005-03-16 12:15:20 +00001038lookupname_end:
drh8141f612004-01-25 22:44:58 +00001039 /* Clean up and return
1040 */
1041 sqliteFree(zDb);
1042 sqliteFree(zTab);
danielk19774adee202004-05-08 08:23:19 +00001043 sqlite3ExprDelete(pExpr->pLeft);
drh8141f612004-01-25 22:44:58 +00001044 pExpr->pLeft = 0;
danielk19774adee202004-05-08 08:23:19 +00001045 sqlite3ExprDelete(pExpr->pRight);
drh8141f612004-01-25 22:44:58 +00001046 pExpr->pRight = 0;
1047 pExpr->op = TK_COLUMN;
drh15ccce12005-05-23 15:06:39 +00001048lookupname_end_2:
1049 sqliteFree(zCol);
drh626a8792005-01-17 22:08:19 +00001050 if( cnt==1 ){
danielk1977b3bce662005-01-29 08:32:43 +00001051 assert( pNC!=0 );
drh626a8792005-01-17 22:08:19 +00001052 sqlite3AuthRead(pParse, pExpr, pNC->pSrcList);
danielk1977aee18ef2005-03-09 12:26:50 +00001053 if( pMatch && !pMatch->pSelect ){
1054 pExpr->pTab = pMatch->pTab;
1055 }
drh15ccce12005-05-23 15:06:39 +00001056 /* Increment the nRef value on all name contexts from TopNC up to
1057 ** the point where the name matched. */
1058 for(;;){
1059 assert( pTopNC!=0 );
1060 pTopNC->nRef++;
1061 if( pTopNC==pNC ) break;
1062 pTopNC = pTopNC->pNext;
1063 }
1064 return 0;
1065 } else {
1066 return 1;
drh626a8792005-01-17 22:08:19 +00001067 }
drh8141f612004-01-25 22:44:58 +00001068}
1069
1070/*
drh626a8792005-01-17 22:08:19 +00001071** This routine is designed as an xFunc for walkExprTree().
1072**
drh73b211a2005-01-18 04:00:42 +00001073** Resolve symbolic names into TK_COLUMN operators for the current
drh626a8792005-01-17 22:08:19 +00001074** node in the expression tree. Return 0 to continue the search down
drh73b211a2005-01-18 04:00:42 +00001075** the tree or 2 to abort the tree walk.
1076**
1077** This routine also does error checking and name resolution for
1078** function names. The operator for aggregate functions is changed
1079** to TK_AGG_FUNCTION.
drh626a8792005-01-17 22:08:19 +00001080*/
1081static int nameResolverStep(void *pArg, Expr *pExpr){
1082 NameContext *pNC = (NameContext*)pArg;
1083 SrcList *pSrcList;
1084 Parse *pParse;
drh626a8792005-01-17 22:08:19 +00001085
danielk1977b3bce662005-01-29 08:32:43 +00001086 if( pExpr==0 ) return 1;
drh626a8792005-01-17 22:08:19 +00001087 assert( pNC!=0 );
1088 pSrcList = pNC->pSrcList;
1089 pParse = pNC->pParse;
danielk1977b3bce662005-01-29 08:32:43 +00001090
drh626a8792005-01-17 22:08:19 +00001091 if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return 1;
1092 ExprSetProperty(pExpr, EP_Resolved);
1093#ifndef NDEBUG
drhffe07b22005-11-03 00:41:17 +00001094 if( pSrcList && pSrcList->nAlloc>0 ){
danielk1977940fac92005-01-23 22:41:37 +00001095 int i;
drh626a8792005-01-17 22:08:19 +00001096 for(i=0; i<pSrcList->nSrc; i++){
1097 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
1098 }
1099 }
1100#endif
1101 switch( pExpr->op ){
1102 /* Double-quoted strings (ex: "abc") are used as identifiers if
1103 ** possible. Otherwise they remain as strings. Single-quoted
1104 ** strings (ex: 'abc') are always string literals.
1105 */
1106 case TK_STRING: {
1107 if( pExpr->token.z[0]=='\'' ) break;
1108 /* Fall thru into the TK_ID case if this is a double-quoted string */
1109 }
1110 /* A lone identifier is the name of a column.
1111 */
1112 case TK_ID: {
drh626a8792005-01-17 22:08:19 +00001113 lookupName(pParse, 0, 0, &pExpr->token, pNC, pExpr);
1114 return 1;
1115 }
1116
1117 /* A table name and column name: ID.ID
1118 ** Or a database, table and column: ID.ID.ID
1119 */
1120 case TK_DOT: {
1121 Token *pColumn;
1122 Token *pTable;
1123 Token *pDb;
1124 Expr *pRight;
1125
danielk1977b3bce662005-01-29 08:32:43 +00001126 /* if( pSrcList==0 ) break; */
drh626a8792005-01-17 22:08:19 +00001127 pRight = pExpr->pRight;
1128 if( pRight->op==TK_ID ){
1129 pDb = 0;
1130 pTable = &pExpr->pLeft->token;
1131 pColumn = &pRight->token;
1132 }else{
1133 assert( pRight->op==TK_DOT );
1134 pDb = &pExpr->pLeft->token;
1135 pTable = &pRight->pLeft->token;
1136 pColumn = &pRight->pRight->token;
1137 }
1138 lookupName(pParse, pDb, pTable, pColumn, pNC, pExpr);
1139 return 1;
1140 }
1141
1142 /* Resolve function names
1143 */
drhb71090f2005-05-23 17:26:51 +00001144 case TK_CONST_FUNC:
drh626a8792005-01-17 22:08:19 +00001145 case TK_FUNCTION: {
1146 ExprList *pList = pExpr->pList; /* The argument list */
1147 int n = pList ? pList->nExpr : 0; /* Number of arguments */
1148 int no_such_func = 0; /* True if no such function exists */
1149 int wrong_num_args = 0; /* True if wrong number of arguments */
1150 int is_agg = 0; /* True if is an aggregate function */
1151 int i;
1152 int nId; /* Number of characters in function name */
1153 const char *zId; /* The function name. */
drh73b211a2005-01-18 04:00:42 +00001154 FuncDef *pDef; /* Information about the function */
danielk197714db2662006-01-09 16:12:04 +00001155 int enc = ENC(pParse->db); /* The database encoding */
drh626a8792005-01-17 22:08:19 +00001156
drh2646da72005-12-09 20:02:05 +00001157 zId = (char*)pExpr->token.z;
drhb71090f2005-05-23 17:26:51 +00001158 nId = pExpr->token.n;
drh626a8792005-01-17 22:08:19 +00001159 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
1160 if( pDef==0 ){
1161 pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
1162 if( pDef==0 ){
1163 no_such_func = 1;
1164 }else{
1165 wrong_num_args = 1;
1166 }
1167 }else{
1168 is_agg = pDef->xFunc==0;
1169 }
1170 if( is_agg && !pNC->allowAgg ){
1171 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
1172 pNC->nErr++;
1173 is_agg = 0;
1174 }else if( no_such_func ){
1175 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
1176 pNC->nErr++;
1177 }else if( wrong_num_args ){
1178 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
1179 nId, zId);
1180 pNC->nErr++;
1181 }
1182 if( is_agg ){
1183 pExpr->op = TK_AGG_FUNCTION;
1184 pNC->hasAgg = 1;
1185 }
drh73b211a2005-01-18 04:00:42 +00001186 if( is_agg ) pNC->allowAgg = 0;
drh626a8792005-01-17 22:08:19 +00001187 for(i=0; pNC->nErr==0 && i<n; i++){
drh73b211a2005-01-18 04:00:42 +00001188 walkExprTree(pList->a[i].pExpr, nameResolverStep, pNC);
drh626a8792005-01-17 22:08:19 +00001189 }
drh73b211a2005-01-18 04:00:42 +00001190 if( is_agg ) pNC->allowAgg = 1;
drh626a8792005-01-17 22:08:19 +00001191 /* FIX ME: Compute pExpr->affinity based on the expected return
1192 ** type of the function
1193 */
1194 return is_agg;
1195 }
danielk1977b3bce662005-01-29 08:32:43 +00001196#ifndef SQLITE_OMIT_SUBQUERY
1197 case TK_SELECT:
1198 case TK_EXISTS:
1199#endif
1200 case TK_IN: {
1201 if( pExpr->pSelect ){
drh8a9f38f2005-11-05 15:07:55 +00001202 int nRef = pNC->nRef;
drh06f65412005-11-03 02:03:13 +00001203#ifndef SQLITE_OMIT_CHECK
1204 if( pNC->isCheck ){
1205 sqlite3ErrorMsg(pParse,"subqueries prohibited in CHECK constraints");
1206 }
1207#endif
danielk1977b3bce662005-01-29 08:32:43 +00001208 sqlite3SelectResolve(pParse, pExpr->pSelect, pNC);
1209 assert( pNC->nRef>=nRef );
1210 if( nRef!=pNC->nRef ){
1211 ExprSetProperty(pExpr, EP_VarSelect);
1212 }
1213 }
drh4284fb02005-11-03 12:33:28 +00001214 break;
danielk1977b3bce662005-01-29 08:32:43 +00001215 }
drh4284fb02005-11-03 12:33:28 +00001216#ifndef SQLITE_OMIT_CHECK
1217 case TK_VARIABLE: {
1218 if( pNC->isCheck ){
1219 sqlite3ErrorMsg(pParse,"parameters prohibited in CHECK constraints");
1220 }
1221 break;
1222 }
1223#endif
drh626a8792005-01-17 22:08:19 +00001224 }
1225 return 0;
1226}
1227
1228/*
drhcce7d172000-05-31 15:34:51 +00001229** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +00001230** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +00001231** index to the table in the table list and a column offset. The
1232** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
1233** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +00001234** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +00001235** VDBE cursor number for a cursor that is pointing into the referenced
1236** table. The Expr.iColumn value is changed to the index of the column
1237** of the referenced table. The Expr.iColumn value for the special
1238** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
1239** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +00001240**
drh626a8792005-01-17 22:08:19 +00001241** Also resolve function names and check the functions for proper
1242** usage. Make sure all function names are recognized and all functions
1243** have the correct number of arguments. Leave an error message
1244** in pParse->zErrMsg if anything is amiss. Return the number of errors.
1245**
drh73b211a2005-01-18 04:00:42 +00001246** If the expression contains aggregate functions then set the EP_Agg
1247** property on the expression.
drh626a8792005-01-17 22:08:19 +00001248*/
1249int sqlite3ExprResolveNames(
danielk1977b3bce662005-01-29 08:32:43 +00001250 NameContext *pNC, /* Namespace to resolve expressions in. */
1251 Expr *pExpr /* The expression to be analyzed. */
drh626a8792005-01-17 22:08:19 +00001252){
drh13449892005-09-07 21:22:45 +00001253 int savedHasAgg;
drh73b211a2005-01-18 04:00:42 +00001254 if( pExpr==0 ) return 0;
drh13449892005-09-07 21:22:45 +00001255 savedHasAgg = pNC->hasAgg;
1256 pNC->hasAgg = 0;
danielk1977b3bce662005-01-29 08:32:43 +00001257 walkExprTree(pExpr, nameResolverStep, pNC);
1258 if( pNC->nErr>0 ){
drh73b211a2005-01-18 04:00:42 +00001259 ExprSetProperty(pExpr, EP_Error);
drh73b211a2005-01-18 04:00:42 +00001260 }
drh13449892005-09-07 21:22:45 +00001261 if( pNC->hasAgg ){
1262 ExprSetProperty(pExpr, EP_Agg);
1263 }else if( savedHasAgg ){
1264 pNC->hasAgg = 1;
1265 }
drh73b211a2005-01-18 04:00:42 +00001266 return ExprHasProperty(pExpr, EP_Error);
drh626a8792005-01-17 22:08:19 +00001267}
1268
drh1398ad32005-01-19 23:24:50 +00001269/*
1270** A pointer instance of this structure is used to pass information
1271** through walkExprTree into codeSubqueryStep().
1272*/
1273typedef struct QueryCoder QueryCoder;
1274struct QueryCoder {
1275 Parse *pParse; /* The parsing context */
1276 NameContext *pNC; /* Namespace of first enclosing query */
1277};
1278
drh626a8792005-01-17 22:08:19 +00001279
1280/*
drh9cbe6352005-11-29 03:13:21 +00001281** Generate code for scalar subqueries used as an expression
1282** and IN operators. Examples:
drh626a8792005-01-17 22:08:19 +00001283**
drh9cbe6352005-11-29 03:13:21 +00001284** (SELECT a FROM b) -- subquery
1285** EXISTS (SELECT a FROM b) -- EXISTS subquery
1286** x IN (4,5,11) -- IN operator with list on right-hand side
1287** x IN (SELECT a FROM b) -- IN operator with subquery on the right
drhfef52082000-06-06 01:50:43 +00001288**
drh9cbe6352005-11-29 03:13:21 +00001289** The pExpr parameter describes the expression that contains the IN
1290** operator or subquery.
drhcce7d172000-05-31 15:34:51 +00001291*/
drh51522cd2005-01-20 13:36:19 +00001292#ifndef SQLITE_OMIT_SUBQUERY
danielk1977b3bce662005-01-29 08:32:43 +00001293void sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){
drh57dbd7b2005-07-08 18:25:26 +00001294 int testAddr = 0; /* One-time test address */
danielk1977b3bce662005-01-29 08:32:43 +00001295 Vdbe *v = sqlite3GetVdbe(pParse);
1296 if( v==0 ) return;
1297
drh57dbd7b2005-07-08 18:25:26 +00001298 /* This code must be run in its entirety every time it is encountered
1299 ** if any of the following is true:
1300 **
1301 ** * The right-hand side is a correlated subquery
1302 ** * The right-hand side is an expression list containing variables
1303 ** * We are inside a trigger
1304 **
1305 ** If all of the above are false, then we can run this code just once
1306 ** save the results, and reuse the same result on subsequent invocations.
danielk1977b3bce662005-01-29 08:32:43 +00001307 */
1308 if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->trigStack ){
1309 int mem = pParse->nMem++;
1310 sqlite3VdbeAddOp(v, OP_MemLoad, mem, 0);
drh57dbd7b2005-07-08 18:25:26 +00001311 testAddr = sqlite3VdbeAddOp(v, OP_If, 0, 0);
drh6f7adc82006-01-11 21:41:20 +00001312 assert( testAddr>0 || sqlite3ThreadDataReadOnly()->mallocFailed );
drhd654be82005-09-20 17:42:23 +00001313 sqlite3VdbeAddOp(v, OP_MemInt, 1, mem);
danielk1977b3bce662005-01-29 08:32:43 +00001314 }
1315
drhcce7d172000-05-31 15:34:51 +00001316 switch( pExpr->op ){
drhfef52082000-06-06 01:50:43 +00001317 case TK_IN: {
danielk1977e014a832004-05-17 10:48:57 +00001318 char affinity;
drhd3d39e92004-05-20 22:16:29 +00001319 KeyInfo keyInfo;
drh9170dd72005-07-08 17:13:46 +00001320 int addr; /* Address of OP_OpenVirtual instruction */
drhd3d39e92004-05-20 22:16:29 +00001321
danielk1977bf3b7212004-05-18 10:06:24 +00001322 affinity = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +00001323
1324 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
drh57dbd7b2005-07-08 18:25:26 +00001325 ** expression it is handled the same way. A virtual table is
danielk1977e014a832004-05-17 10:48:57 +00001326 ** filled with single-field index keys representing the results
1327 ** from the SELECT or the <exprlist>.
1328 **
1329 ** If the 'x' expression is a column value, or the SELECT...
1330 ** statement returns a column value, then the affinity of that
1331 ** column is used to build the index keys. If both 'x' and the
1332 ** SELECT... statement are columns, then numeric affinity is used
1333 ** if either column has NUMERIC or INTEGER affinity. If neither
1334 ** 'x' nor the SELECT... statement are columns, then numeric affinity
1335 ** is used.
1336 */
1337 pExpr->iTable = pParse->nTab++;
drh9170dd72005-07-08 17:13:46 +00001338 addr = sqlite3VdbeAddOp(v, OP_OpenVirtual, pExpr->iTable, 0);
drhd3d39e92004-05-20 22:16:29 +00001339 memset(&keyInfo, 0, sizeof(keyInfo));
1340 keyInfo.nField = 1;
drhf3218fe2004-05-28 08:21:02 +00001341 sqlite3VdbeAddOp(v, OP_SetNumColumns, pExpr->iTable, 1);
danielk1977e014a832004-05-17 10:48:57 +00001342
drhfef52082000-06-06 01:50:43 +00001343 if( pExpr->pSelect ){
1344 /* Case 1: expr IN (SELECT ...)
1345 **
danielk1977e014a832004-05-17 10:48:57 +00001346 ** Generate code to write the results of the select into the temporary
1347 ** table allocated and opened above.
drhfef52082000-06-06 01:50:43 +00001348 */
danielk1977e014a832004-05-17 10:48:57 +00001349 int iParm = pExpr->iTable + (((int)affinity)<<16);
drhbe5c89a2004-07-26 00:31:09 +00001350 ExprList *pEList;
danielk1977e014a832004-05-17 10:48:57 +00001351 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
danielk1977b3bce662005-01-29 08:32:43 +00001352 sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001353 pEList = pExpr->pSelect->pEList;
1354 if( pEList && pEList->nExpr>0 ){
danielk19777cedc8d2004-06-10 10:50:08 +00001355 keyInfo.aColl[0] = binaryCompareCollSeq(pParse, pExpr->pLeft,
drhbe5c89a2004-07-26 00:31:09 +00001356 pEList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001357 }
drhfef52082000-06-06 01:50:43 +00001358 }else if( pExpr->pList ){
1359 /* Case 2: expr IN (exprlist)
1360 **
danielk1977e014a832004-05-17 10:48:57 +00001361 ** For each expression, build an index key from the evaluation and
1362 ** store it in the temporary table. If <expr> is a column, then use
1363 ** that columns affinity when building index keys. If <expr> is not
1364 ** a column, use numeric affinity.
drhfef52082000-06-06 01:50:43 +00001365 */
danielk1977e014a832004-05-17 10:48:57 +00001366 int i;
drh57dbd7b2005-07-08 18:25:26 +00001367 ExprList *pList = pExpr->pList;
1368 struct ExprList_item *pItem;
1369
danielk1977e014a832004-05-17 10:48:57 +00001370 if( !affinity ){
1371 affinity = SQLITE_AFF_NUMERIC;
1372 }
danielk19770202b292004-06-09 09:55:16 +00001373 keyInfo.aColl[0] = pExpr->pLeft->pColl;
danielk1977e014a832004-05-17 10:48:57 +00001374
1375 /* Loop through each expression in <exprlist>. */
drh57dbd7b2005-07-08 18:25:26 +00001376 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
1377 Expr *pE2 = pItem->pExpr;
danielk1977e014a832004-05-17 10:48:57 +00001378
drh57dbd7b2005-07-08 18:25:26 +00001379 /* If the expression is not constant then we will need to
1380 ** disable the test that was generated above that makes sure
1381 ** this code only executes once. Because for a non-constant
1382 ** expression we need to rerun this code each time.
1383 */
drh6c30be82005-07-29 15:10:17 +00001384 if( testAddr>0 && !sqlite3ExprIsConstant(pE2) ){
drh57dbd7b2005-07-08 18:25:26 +00001385 VdbeOp *aOp = sqlite3VdbeGetOp(v, testAddr-1);
1386 int i;
drhd654be82005-09-20 17:42:23 +00001387 for(i=0; i<3; i++){
drh57dbd7b2005-07-08 18:25:26 +00001388 aOp[i].opcode = OP_Noop;
1389 }
1390 testAddr = 0;
drh4794b982000-06-06 13:54:14 +00001391 }
danielk1977e014a832004-05-17 10:48:57 +00001392
1393 /* Evaluate the expression and insert it into the temp table */
1394 sqlite3ExprCode(pParse, pE2);
drh94a11212004-09-25 13:12:14 +00001395 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1);
drhf0863fe2005-06-12 21:35:51 +00001396 sqlite3VdbeAddOp(v, OP_IdxInsert, pExpr->iTable, 0);
drhfef52082000-06-06 01:50:43 +00001397 }
1398 }
danielk19770202b292004-06-09 09:55:16 +00001399 sqlite3VdbeChangeP3(v, addr, (void *)&keyInfo, P3_KEYINFO);
danielk1977b3bce662005-01-29 08:32:43 +00001400 break;
drhfef52082000-06-06 01:50:43 +00001401 }
1402
drh51522cd2005-01-20 13:36:19 +00001403 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001404 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +00001405 /* This has to be a scalar SELECT. Generate code to put the
1406 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +00001407 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +00001408 */
drh2646da72005-12-09 20:02:05 +00001409 static const Token one = { (u8*)"1", 0, 1 };
drh51522cd2005-01-20 13:36:19 +00001410 Select *pSel;
drhec7429a2005-10-06 16:53:14 +00001411 int iMem;
1412 int sop;
drh1398ad32005-01-19 23:24:50 +00001413
drhec7429a2005-10-06 16:53:14 +00001414 pExpr->iColumn = iMem = pParse->nMem++;
drh51522cd2005-01-20 13:36:19 +00001415 pSel = pExpr->pSelect;
1416 if( pExpr->op==TK_SELECT ){
1417 sop = SRT_Mem;
drhec7429a2005-10-06 16:53:14 +00001418 sqlite3VdbeAddOp(v, OP_MemNull, iMem, 0);
1419 VdbeComment((v, "# Init subquery result"));
drh51522cd2005-01-20 13:36:19 +00001420 }else{
drh51522cd2005-01-20 13:36:19 +00001421 sop = SRT_Exists;
drhec7429a2005-10-06 16:53:14 +00001422 sqlite3VdbeAddOp(v, OP_MemInt, 0, iMem);
1423 VdbeComment((v, "# Init EXISTS result"));
drh51522cd2005-01-20 13:36:19 +00001424 }
drhec7429a2005-10-06 16:53:14 +00001425 sqlite3ExprDelete(pSel->pLimit);
1426 pSel->pLimit = sqlite3Expr(TK_INTEGER, 0, 0, &one);
1427 sqlite3Select(pParse, pSel, sop, iMem, 0, 0, 0, 0);
danielk1977b3bce662005-01-29 08:32:43 +00001428 break;
drhcce7d172000-05-31 15:34:51 +00001429 }
1430 }
danielk1977b3bce662005-01-29 08:32:43 +00001431
drh57dbd7b2005-07-08 18:25:26 +00001432 if( testAddr ){
drhd654be82005-09-20 17:42:23 +00001433 sqlite3VdbeJumpHere(v, testAddr);
danielk1977b3bce662005-01-29 08:32:43 +00001434 }
1435 return;
drhcce7d172000-05-31 15:34:51 +00001436}
drh51522cd2005-01-20 13:36:19 +00001437#endif /* SQLITE_OMIT_SUBQUERY */
drhcce7d172000-05-31 15:34:51 +00001438
drhcce7d172000-05-31 15:34:51 +00001439/*
drhfec19aa2004-05-19 20:41:03 +00001440** Generate an instruction that will put the integer describe by
1441** text z[0..n-1] on the stack.
1442*/
1443static void codeInteger(Vdbe *v, const char *z, int n){
1444 int i;
drh6fec0762004-05-30 01:38:43 +00001445 if( sqlite3GetInt32(z, &i) ){
1446 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
1447 }else if( sqlite3FitsIn64Bits(z) ){
drh29dda4a2005-07-21 18:23:20 +00001448 sqlite3VdbeOp3(v, OP_Int64, 0, 0, z, n);
drhfec19aa2004-05-19 20:41:03 +00001449 }else{
1450 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n);
1451 }
1452}
1453
1454/*
drhcce7d172000-05-31 15:34:51 +00001455** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +00001456** expression and leave the result on the top of stack.
drhf2bc0132004-10-04 13:19:23 +00001457**
1458** This code depends on the fact that certain token values (ex: TK_EQ)
1459** are the same as opcode values (ex: OP_Eq) that implement the corresponding
1460** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
1461** the make process cause these values to align. Assert()s in the code
1462** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00001463*/
danielk19774adee202004-05-08 08:23:19 +00001464void sqlite3ExprCode(Parse *pParse, Expr *pExpr){
drhcce7d172000-05-31 15:34:51 +00001465 Vdbe *v = pParse->pVdbe;
1466 int op;
drhffe07b22005-11-03 00:41:17 +00001467 int stackChng = 1; /* Amount of change to stack depth */
1468
danielk19777977a172004-11-09 12:44:37 +00001469 if( v==0 ) return;
1470 if( pExpr==0 ){
drhf0863fe2005-06-12 21:35:51 +00001471 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
danielk19777977a172004-11-09 12:44:37 +00001472 return;
1473 }
drhf2bc0132004-10-04 13:19:23 +00001474 op = pExpr->op;
1475 switch( op ){
drh13449892005-09-07 21:22:45 +00001476 case TK_AGG_COLUMN: {
1477 AggInfo *pAggInfo = pExpr->pAggInfo;
1478 struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg];
1479 if( !pAggInfo->directMode ){
1480 sqlite3VdbeAddOp(v, OP_MemLoad, pCol->iMem, 0);
1481 break;
1482 }else if( pAggInfo->useSortingIdx ){
1483 sqlite3VdbeAddOp(v, OP_Column, pAggInfo->sortingIdx,
1484 pCol->iSorterColumn);
1485 break;
1486 }
1487 /* Otherwise, fall thru into the TK_COLUMN case */
1488 }
drh967e8b72000-06-21 13:59:10 +00001489 case TK_COLUMN: {
drhffe07b22005-11-03 00:41:17 +00001490 if( pExpr->iTable<0 ){
1491 /* This only happens when coding check constraints */
1492 assert( pParse->ckOffset>0 );
1493 sqlite3VdbeAddOp(v, OP_Dup, pParse->ckOffset-pExpr->iColumn-1, 1);
1494 }else if( pExpr->iColumn>=0 ){
drh8a512562005-11-14 22:29:05 +00001495 Table *pTab = pExpr->pTab;
1496 int iCol = pExpr->iColumn;
1497 sqlite3VdbeAddOp(v, OP_Column, pExpr->iTable, iCol);
1498 sqlite3ColumnDefault(v, pTab, iCol);
1499#ifndef SQLITE_OMIT_FLOATING_POINT
1500 if( pTab && pTab->aCol[iCol].affinity==SQLITE_AFF_REAL ){
1501 sqlite3VdbeAddOp(v, OP_RealAffinity, 0, 0);
1502 }
1503#endif
drhc4a3c772001-04-04 11:48:57 +00001504 }else{
drhf0863fe2005-06-12 21:35:51 +00001505 sqlite3VdbeAddOp(v, OP_Rowid, pExpr->iTable, 0);
drh22827922000-06-06 17:27:05 +00001506 }
drhcce7d172000-05-31 15:34:51 +00001507 break;
1508 }
1509 case TK_INTEGER: {
drh2646da72005-12-09 20:02:05 +00001510 codeInteger(v, (char*)pExpr->token.z, pExpr->token.n);
drhfec19aa2004-05-19 20:41:03 +00001511 break;
1512 }
1513 case TK_FLOAT:
1514 case TK_STRING: {
drhf2bc0132004-10-04 13:19:23 +00001515 assert( TK_FLOAT==OP_Real );
1516 assert( TK_STRING==OP_String8 );
drhd2687b72005-08-12 22:56:09 +00001517 sqlite3DequoteExpr(pExpr);
drh2646da72005-12-09 20:02:05 +00001518 sqlite3VdbeOp3(v, op, 0, 0, (char*)pExpr->token.z, pExpr->token.n);
drhcce7d172000-05-31 15:34:51 +00001519 break;
1520 }
drhf0863fe2005-06-12 21:35:51 +00001521 case TK_NULL: {
1522 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
1523 break;
1524 }
danielk19775338a5f2005-01-20 13:03:10 +00001525#ifndef SQLITE_OMIT_BLOB_LITERAL
danielk1977c572ef72004-05-27 09:28:41 +00001526 case TK_BLOB: {
drh6c8c6ce2005-08-23 11:17:58 +00001527 int n;
1528 const char *z;
drhf2bc0132004-10-04 13:19:23 +00001529 assert( TK_BLOB==OP_HexBlob );
drh6c8c6ce2005-08-23 11:17:58 +00001530 n = pExpr->token.n - 3;
drh2646da72005-12-09 20:02:05 +00001531 z = (char*)pExpr->token.z + 2;
drh6c8c6ce2005-08-23 11:17:58 +00001532 assert( n>=0 );
1533 if( n==0 ){
1534 z = "";
1535 }
1536 sqlite3VdbeOp3(v, op, 0, 0, z, n);
danielk1977c572ef72004-05-27 09:28:41 +00001537 break;
1538 }
danielk19775338a5f2005-01-20 13:03:10 +00001539#endif
drh50457892003-09-06 01:10:47 +00001540 case TK_VARIABLE: {
danielk19774adee202004-05-08 08:23:19 +00001541 sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0);
drh895d7472004-08-20 16:02:39 +00001542 if( pExpr->token.n>1 ){
drh2646da72005-12-09 20:02:05 +00001543 sqlite3VdbeChangeP3(v, -1, (char*)pExpr->token.z, pExpr->token.n);
drh895d7472004-08-20 16:02:39 +00001544 }
drh50457892003-09-06 01:10:47 +00001545 break;
1546 }
drh4e0cff62004-11-05 05:10:28 +00001547 case TK_REGISTER: {
1548 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iTable, 0);
1549 break;
1550 }
drh487e2622005-06-25 18:42:14 +00001551#ifndef SQLITE_OMIT_CAST
1552 case TK_CAST: {
1553 /* Expressions of the form: CAST(pLeft AS token) */
1554 int aff, op;
1555 sqlite3ExprCode(pParse, pExpr->pLeft);
drh8a512562005-11-14 22:29:05 +00001556 aff = sqlite3AffinityType(&pExpr->token);
1557 op = aff - SQLITE_AFF_TEXT + OP_ToText;
1558 assert( op==OP_ToText || aff!=SQLITE_AFF_TEXT );
1559 assert( op==OP_ToBlob || aff!=SQLITE_AFF_NONE );
1560 assert( op==OP_ToNumeric || aff!=SQLITE_AFF_NUMERIC );
1561 assert( op==OP_ToInt || aff!=SQLITE_AFF_INTEGER );
1562 assert( op==OP_ToReal || aff!=SQLITE_AFF_REAL );
drh487e2622005-06-25 18:42:14 +00001563 sqlite3VdbeAddOp(v, op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001564 stackChng = 0;
drh487e2622005-06-25 18:42:14 +00001565 break;
1566 }
1567#endif /* SQLITE_OMIT_CAST */
drhc9b84a12002-06-20 11:36:48 +00001568 case TK_LT:
1569 case TK_LE:
1570 case TK_GT:
1571 case TK_GE:
1572 case TK_NE:
1573 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00001574 assert( TK_LT==OP_Lt );
1575 assert( TK_LE==OP_Le );
1576 assert( TK_GT==OP_Gt );
1577 assert( TK_GE==OP_Ge );
1578 assert( TK_EQ==OP_Eq );
1579 assert( TK_NE==OP_Ne );
danielk1977a37cdde2004-05-16 11:15:36 +00001580 sqlite3ExprCode(pParse, pExpr->pLeft);
1581 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001582 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001583 stackChng = -1;
danielk1977a37cdde2004-05-16 11:15:36 +00001584 break;
drhc9b84a12002-06-20 11:36:48 +00001585 }
drhcce7d172000-05-31 15:34:51 +00001586 case TK_AND:
1587 case TK_OR:
1588 case TK_PLUS:
1589 case TK_STAR:
1590 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00001591 case TK_REM:
1592 case TK_BITAND:
1593 case TK_BITOR:
drh17c40292004-07-21 02:53:29 +00001594 case TK_SLASH:
drhbf4133c2001-10-13 02:59:08 +00001595 case TK_LSHIFT:
drh855eb1c2004-08-31 13:45:11 +00001596 case TK_RSHIFT:
drh00400772000-06-16 20:51:26 +00001597 case TK_CONCAT: {
drhf2bc0132004-10-04 13:19:23 +00001598 assert( TK_AND==OP_And );
1599 assert( TK_OR==OP_Or );
1600 assert( TK_PLUS==OP_Add );
1601 assert( TK_MINUS==OP_Subtract );
1602 assert( TK_REM==OP_Remainder );
1603 assert( TK_BITAND==OP_BitAnd );
1604 assert( TK_BITOR==OP_BitOr );
1605 assert( TK_SLASH==OP_Divide );
1606 assert( TK_LSHIFT==OP_ShiftLeft );
1607 assert( TK_RSHIFT==OP_ShiftRight );
1608 assert( TK_CONCAT==OP_Concat );
danielk19774adee202004-05-08 08:23:19 +00001609 sqlite3ExprCode(pParse, pExpr->pLeft);
1610 sqlite3ExprCode(pParse, pExpr->pRight);
drh855eb1c2004-08-31 13:45:11 +00001611 sqlite3VdbeAddOp(v, op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001612 stackChng = -1;
drh00400772000-06-16 20:51:26 +00001613 break;
1614 }
drhcce7d172000-05-31 15:34:51 +00001615 case TK_UMINUS: {
drhfec19aa2004-05-19 20:41:03 +00001616 Expr *pLeft = pExpr->pLeft;
1617 assert( pLeft );
1618 if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
1619 Token *p = &pLeft->token;
drh9267bdc2005-11-28 12:36:35 +00001620 char *z = sqlite3MPrintf("-%.*s", p->n, p->z);
drhfec19aa2004-05-19 20:41:03 +00001621 if( pLeft->op==TK_FLOAT ){
1622 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001623 }else{
drhfec19aa2004-05-19 20:41:03 +00001624 codeInteger(v, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001625 }
drh6e142f52000-06-08 13:36:40 +00001626 sqliteFree(z);
1627 break;
1628 }
drh1ccde152000-06-17 13:12:39 +00001629 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +00001630 }
drhbf4133c2001-10-13 02:59:08 +00001631 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00001632 case TK_NOT: {
drhf2bc0132004-10-04 13:19:23 +00001633 assert( TK_BITNOT==OP_BitNot );
1634 assert( TK_NOT==OP_Not );
danielk19774adee202004-05-08 08:23:19 +00001635 sqlite3ExprCode(pParse, pExpr->pLeft);
1636 sqlite3VdbeAddOp(v, op, 0, 0);
drhffe07b22005-11-03 00:41:17 +00001637 stackChng = 0;
drhcce7d172000-05-31 15:34:51 +00001638 break;
1639 }
1640 case TK_ISNULL:
1641 case TK_NOTNULL: {
1642 int dest;
drhf2bc0132004-10-04 13:19:23 +00001643 assert( TK_ISNULL==OP_IsNull );
1644 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00001645 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1646 sqlite3ExprCode(pParse, pExpr->pLeft);
1647 dest = sqlite3VdbeCurrentAddr(v) + 2;
1648 sqlite3VdbeAddOp(v, op, 1, dest);
1649 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);
drhffe07b22005-11-03 00:41:17 +00001650 stackChng = 0;
drhf2bc0132004-10-04 13:19:23 +00001651 break;
drhcce7d172000-05-31 15:34:51 +00001652 }
drh22827922000-06-06 17:27:05 +00001653 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00001654 AggInfo *pInfo = pExpr->pAggInfo;
drh7e56e712005-11-16 12:53:15 +00001655 if( pInfo==0 ){
1656 sqlite3ErrorMsg(pParse, "misuse of aggregate: %T",
1657 &pExpr->span);
1658 }else{
1659 sqlite3VdbeAddOp(v, OP_MemLoad, pInfo->aFunc[pExpr->iAgg].iMem, 0);
1660 }
drh22827922000-06-06 17:27:05 +00001661 break;
1662 }
drhb71090f2005-05-23 17:26:51 +00001663 case TK_CONST_FUNC:
drhcce7d172000-05-31 15:34:51 +00001664 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00001665 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00001666 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00001667 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00001668 int nId;
1669 const char *zId;
drh13449892005-09-07 21:22:45 +00001670 int constMask = 0;
danielk1977682f68b2004-06-05 10:22:17 +00001671 int i;
danielk197714db2662006-01-09 16:12:04 +00001672 u8 enc = ENC(pParse->db);
danielk1977dc1bdc42004-06-11 10:51:27 +00001673 CollSeq *pColl = 0;
drh2646da72005-12-09 20:02:05 +00001674 zId = (char*)pExpr->token.z;
drhb71090f2005-05-23 17:26:51 +00001675 nId = pExpr->token.n;
danielk1977d8123362004-06-12 09:25:12 +00001676 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001677 assert( pDef!=0 );
drhf9b596e2004-05-26 16:54:42 +00001678 nExpr = sqlite3ExprCodeExprList(pParse, pList);
danielk1977682f68b2004-06-05 10:22:17 +00001679 for(i=0; i<nExpr && i<32; i++){
danielk1977d02eb1f2004-06-06 09:44:03 +00001680 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
drh13449892005-09-07 21:22:45 +00001681 constMask |= (1<<i);
danielk1977d02eb1f2004-06-06 09:44:03 +00001682 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001683 if( pDef->needCollSeq && !pColl ){
1684 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
1685 }
1686 }
1687 if( pDef->needCollSeq ){
1688 if( !pColl ) pColl = pParse->db->pDfltColl;
danielk1977d8123362004-06-12 09:25:12 +00001689 sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ);
danielk1977682f68b2004-06-05 10:22:17 +00001690 }
drh13449892005-09-07 21:22:45 +00001691 sqlite3VdbeOp3(v, OP_Function, constMask, nExpr, (char*)pDef, P3_FUNCDEF);
drhffe07b22005-11-03 00:41:17 +00001692 stackChng = 1-nExpr;
drhcce7d172000-05-31 15:34:51 +00001693 break;
1694 }
drhfe2093d2005-01-20 22:48:47 +00001695#ifndef SQLITE_OMIT_SUBQUERY
1696 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001697 case TK_SELECT: {
danielk1977b3bce662005-01-29 08:32:43 +00001698 sqlite3CodeSubselect(pParse, pExpr);
danielk19774adee202004-05-08 08:23:19 +00001699 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drhad6d9462004-09-19 02:15:24 +00001700 VdbeComment((v, "# load subquery result"));
drh19a775c2000-06-05 18:54:46 +00001701 break;
1702 }
drhfef52082000-06-06 01:50:43 +00001703 case TK_IN: {
1704 int addr;
drh94a11212004-09-25 13:12:14 +00001705 char affinity;
danielk1977b3bce662005-01-29 08:32:43 +00001706 sqlite3CodeSubselect(pParse, pExpr);
danielk1977e014a832004-05-17 10:48:57 +00001707
1708 /* Figure out the affinity to use to create a key from the results
1709 ** of the expression. affinityStr stores a static string suitable for
danielk1977ededfd52004-06-17 07:53:01 +00001710 ** P3 of OP_MakeRecord.
danielk1977e014a832004-05-17 10:48:57 +00001711 */
drh94a11212004-09-25 13:12:14 +00001712 affinity = comparisonAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +00001713
danielk19774adee202004-05-08 08:23:19 +00001714 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
danielk1977e014a832004-05-17 10:48:57 +00001715
1716 /* Code the <expr> from "<expr> IN (...)". The temporary table
1717 ** pExpr->iTable contains the values that make up the (...) set.
1718 */
danielk19774adee202004-05-08 08:23:19 +00001719 sqlite3ExprCode(pParse, pExpr->pLeft);
1720 addr = sqlite3VdbeCurrentAddr(v);
danielk1977e014a832004-05-17 10:48:57 +00001721 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); /* addr + 0 */
danielk19774adee202004-05-08 08:23:19 +00001722 sqlite3VdbeAddOp(v, OP_Pop, 2, 0);
drhf0863fe2005-06-12 21:35:51 +00001723 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +00001724 sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7);
drh94a11212004-09-25 13:12:14 +00001725 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1); /* addr + 4 */
danielk1977e014a832004-05-17 10:48:57 +00001726 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7);
1727 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); /* addr + 6 */
1728
drhfef52082000-06-06 01:50:43 +00001729 break;
1730 }
danielk197793758c82005-01-21 08:13:14 +00001731#endif
drhfef52082000-06-06 01:50:43 +00001732 case TK_BETWEEN: {
drhbe5c89a2004-07-26 00:31:09 +00001733 Expr *pLeft = pExpr->pLeft;
1734 struct ExprList_item *pLItem = pExpr->pList->a;
1735 Expr *pRight = pLItem->pExpr;
1736 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001737 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001738 sqlite3ExprCode(pParse, pRight);
1739 codeCompare(pParse, pLeft, pRight, OP_Ge, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001740 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
drhbe5c89a2004-07-26 00:31:09 +00001741 pLItem++;
1742 pRight = pLItem->pExpr;
1743 sqlite3ExprCode(pParse, pRight);
1744 codeCompare(pParse, pLeft, pRight, OP_Le, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001745 sqlite3VdbeAddOp(v, OP_And, 0, 0);
drhfef52082000-06-06 01:50:43 +00001746 break;
1747 }
drh51e9a442004-01-16 16:42:53 +00001748 case TK_UPLUS:
drha2e00042002-01-22 03:13:42 +00001749 case TK_AS: {
danielk19774adee202004-05-08 08:23:19 +00001750 sqlite3ExprCode(pParse, pExpr->pLeft);
drhffe07b22005-11-03 00:41:17 +00001751 stackChng = 0;
drha2e00042002-01-22 03:13:42 +00001752 break;
1753 }
drh17a7f8d2002-03-24 13:13:27 +00001754 case TK_CASE: {
1755 int expr_end_label;
drhf5905aa2002-05-26 20:54:33 +00001756 int jumpInst;
drhf5905aa2002-05-26 20:54:33 +00001757 int nExpr;
drh17a7f8d2002-03-24 13:13:27 +00001758 int i;
drhbe5c89a2004-07-26 00:31:09 +00001759 ExprList *pEList;
1760 struct ExprList_item *aListelem;
drh17a7f8d2002-03-24 13:13:27 +00001761
1762 assert(pExpr->pList);
1763 assert((pExpr->pList->nExpr % 2) == 0);
1764 assert(pExpr->pList->nExpr > 0);
drhbe5c89a2004-07-26 00:31:09 +00001765 pEList = pExpr->pList;
1766 aListelem = pEList->a;
1767 nExpr = pEList->nExpr;
danielk19774adee202004-05-08 08:23:19 +00001768 expr_end_label = sqlite3VdbeMakeLabel(v);
drh17a7f8d2002-03-24 13:13:27 +00001769 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001770 sqlite3ExprCode(pParse, pExpr->pLeft);
drh17a7f8d2002-03-24 13:13:27 +00001771 }
drhf5905aa2002-05-26 20:54:33 +00001772 for(i=0; i<nExpr; i=i+2){
drhbe5c89a2004-07-26 00:31:09 +00001773 sqlite3ExprCode(pParse, aListelem[i].pExpr);
drh17a7f8d2002-03-24 13:13:27 +00001774 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001775 sqlite3VdbeAddOp(v, OP_Dup, 1, 1);
drhbe5c89a2004-07-26 00:31:09 +00001776 jumpInst = codeCompare(pParse, pExpr->pLeft, aListelem[i].pExpr,
1777 OP_Ne, 0, 1);
danielk19774adee202004-05-08 08:23:19 +00001778 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001779 }else{
danielk19774adee202004-05-08 08:23:19 +00001780 jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001781 }
drhbe5c89a2004-07-26 00:31:09 +00001782 sqlite3ExprCode(pParse, aListelem[i+1].pExpr);
danielk19774adee202004-05-08 08:23:19 +00001783 sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label);
drhd654be82005-09-20 17:42:23 +00001784 sqlite3VdbeJumpHere(v, jumpInst);
drh17a7f8d2002-03-24 13:13:27 +00001785 }
drhf570f012002-05-31 15:51:25 +00001786 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001787 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhf570f012002-05-31 15:51:25 +00001788 }
drh17a7f8d2002-03-24 13:13:27 +00001789 if( pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001790 sqlite3ExprCode(pParse, pExpr->pRight);
drh17a7f8d2002-03-24 13:13:27 +00001791 }else{
drhf0863fe2005-06-12 21:35:51 +00001792 sqlite3VdbeAddOp(v, OP_Null, 0, 0);
drh17a7f8d2002-03-24 13:13:27 +00001793 }
danielk19774adee202004-05-08 08:23:19 +00001794 sqlite3VdbeResolveLabel(v, expr_end_label);
danielk19776f349032002-06-11 02:25:40 +00001795 break;
1796 }
danielk19775338a5f2005-01-20 13:03:10 +00001797#ifndef SQLITE_OMIT_TRIGGER
danielk19776f349032002-06-11 02:25:40 +00001798 case TK_RAISE: {
1799 if( !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +00001800 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00001801 "RAISE() may only be used within a trigger-program");
danielk19776f349032002-06-11 02:25:40 +00001802 return;
1803 }
drhad6d9462004-09-19 02:15:24 +00001804 if( pExpr->iColumn!=OE_Ignore ){
1805 assert( pExpr->iColumn==OE_Rollback ||
1806 pExpr->iColumn == OE_Abort ||
1807 pExpr->iColumn == OE_Fail );
drhd2687b72005-08-12 22:56:09 +00001808 sqlite3DequoteExpr(pExpr);
drhad6d9462004-09-19 02:15:24 +00001809 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn,
drh2646da72005-12-09 20:02:05 +00001810 (char*)pExpr->token.z, pExpr->token.n);
danielk19776f349032002-06-11 02:25:40 +00001811 } else {
drhad6d9462004-09-19 02:15:24 +00001812 assert( pExpr->iColumn == OE_Ignore );
1813 sqlite3VdbeAddOp(v, OP_ContextPop, 0, 0);
1814 sqlite3VdbeAddOp(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
1815 VdbeComment((v, "# raise(IGNORE)"));
danielk19776f349032002-06-11 02:25:40 +00001816 }
drhffe07b22005-11-03 00:41:17 +00001817 stackChng = 0;
1818 break;
drh17a7f8d2002-03-24 13:13:27 +00001819 }
danielk19775338a5f2005-01-20 13:03:10 +00001820#endif
drhffe07b22005-11-03 00:41:17 +00001821 }
1822
1823 if( pParse->ckOffset ){
1824 pParse->ckOffset += stackChng;
1825 assert( pParse->ckOffset );
drhcce7d172000-05-31 15:34:51 +00001826 }
drhcce7d172000-05-31 15:34:51 +00001827}
1828
danielk197793758c82005-01-21 08:13:14 +00001829#ifndef SQLITE_OMIT_TRIGGER
drhcce7d172000-05-31 15:34:51 +00001830/*
drh25303782004-12-07 15:41:48 +00001831** Generate code that evalutes the given expression and leaves the result
1832** on the stack. See also sqlite3ExprCode().
1833**
1834** This routine might also cache the result and modify the pExpr tree
1835** so that it will make use of the cached result on subsequent evaluations
1836** rather than evaluate the whole expression again. Trivial expressions are
1837** not cached. If the expression is cached, its result is stored in a
1838** memory location.
1839*/
1840void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr){
1841 Vdbe *v = pParse->pVdbe;
1842 int iMem;
1843 int addr1, addr2;
1844 if( v==0 ) return;
1845 addr1 = sqlite3VdbeCurrentAddr(v);
1846 sqlite3ExprCode(pParse, pExpr);
1847 addr2 = sqlite3VdbeCurrentAddr(v);
1848 if( addr2>addr1+1 || sqlite3VdbeGetOp(v, addr1)->opcode==OP_Function ){
1849 iMem = pExpr->iTable = pParse->nMem++;
1850 sqlite3VdbeAddOp(v, OP_MemStore, iMem, 0);
1851 pExpr->op = TK_REGISTER;
1852 }
1853}
danielk197793758c82005-01-21 08:13:14 +00001854#endif
drh25303782004-12-07 15:41:48 +00001855
1856/*
drh268380c2004-02-25 13:47:31 +00001857** Generate code that pushes the value of every element of the given
drhf9b596e2004-05-26 16:54:42 +00001858** expression list onto the stack.
drh268380c2004-02-25 13:47:31 +00001859**
1860** Return the number of elements pushed onto the stack.
1861*/
danielk19774adee202004-05-08 08:23:19 +00001862int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00001863 Parse *pParse, /* Parsing context */
drhf9b596e2004-05-26 16:54:42 +00001864 ExprList *pList /* The expression list to be coded */
drh268380c2004-02-25 13:47:31 +00001865){
1866 struct ExprList_item *pItem;
1867 int i, n;
drh268380c2004-02-25 13:47:31 +00001868 if( pList==0 ) return 0;
drh268380c2004-02-25 13:47:31 +00001869 n = pList->nExpr;
drhc182d162005-08-14 20:47:16 +00001870 for(pItem=pList->a, i=n; i>0; i--, pItem++){
danielk19774adee202004-05-08 08:23:19 +00001871 sqlite3ExprCode(pParse, pItem->pExpr);
drh268380c2004-02-25 13:47:31 +00001872 }
drhf9b596e2004-05-26 16:54:42 +00001873 return n;
drh268380c2004-02-25 13:47:31 +00001874}
1875
1876/*
drhcce7d172000-05-31 15:34:51 +00001877** Generate code for a boolean expression such that a jump is made
1878** to the label "dest" if the expression is true but execution
1879** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00001880**
1881** If the expression evaluates to NULL (neither true nor false), then
1882** take the jump if the jumpIfNull flag is true.
drhf2bc0132004-10-04 13:19:23 +00001883**
1884** This code depends on the fact that certain token values (ex: TK_EQ)
1885** are the same as opcode values (ex: OP_Eq) that implement the corresponding
1886** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
1887** the make process cause these values to align. Assert()s in the code
1888** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00001889*/
danielk19774adee202004-05-08 08:23:19 +00001890void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001891 Vdbe *v = pParse->pVdbe;
1892 int op = 0;
drhffe07b22005-11-03 00:41:17 +00001893 int ckOffset = pParse->ckOffset;
drhdaffd0e2001-04-11 14:28:42 +00001894 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00001895 op = pExpr->op;
1896 switch( op ){
drhcce7d172000-05-31 15:34:51 +00001897 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001898 int d2 = sqlite3VdbeMakeLabel(v);
1899 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
1900 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
1901 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001902 break;
1903 }
1904 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001905 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
1906 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001907 break;
1908 }
1909 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001910 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001911 break;
1912 }
1913 case TK_LT:
1914 case TK_LE:
1915 case TK_GT:
1916 case TK_GE:
1917 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00001918 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00001919 assert( TK_LT==OP_Lt );
1920 assert( TK_LE==OP_Le );
1921 assert( TK_GT==OP_Gt );
1922 assert( TK_GE==OP_Ge );
1923 assert( TK_EQ==OP_Eq );
1924 assert( TK_NE==OP_Ne );
danielk19774adee202004-05-08 08:23:19 +00001925 sqlite3ExprCode(pParse, pExpr->pLeft);
1926 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001927 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001928 break;
1929 }
1930 case TK_ISNULL:
1931 case TK_NOTNULL: {
drhf2bc0132004-10-04 13:19:23 +00001932 assert( TK_ISNULL==OP_IsNull );
1933 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00001934 sqlite3ExprCode(pParse, pExpr->pLeft);
1935 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001936 break;
1937 }
drhfef52082000-06-06 01:50:43 +00001938 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00001939 /* The expression "x BETWEEN y AND z" is implemented as:
1940 **
1941 ** 1 IF (x < y) GOTO 3
1942 ** 2 IF (x <= z) GOTO <dest>
1943 ** 3 ...
1944 */
drhf5905aa2002-05-26 20:54:33 +00001945 int addr;
drhbe5c89a2004-07-26 00:31:09 +00001946 Expr *pLeft = pExpr->pLeft;
1947 Expr *pRight = pExpr->pList->a[0].pExpr;
1948 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001949 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001950 sqlite3ExprCode(pParse, pRight);
1951 addr = codeCompare(pParse, pLeft, pRight, OP_Lt, 0, !jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00001952
drhbe5c89a2004-07-26 00:31:09 +00001953 pRight = pExpr->pList->a[1].pExpr;
1954 sqlite3ExprCode(pParse, pRight);
1955 codeCompare(pParse, pLeft, pRight, OP_Le, dest, jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00001956
danielk19774adee202004-05-08 08:23:19 +00001957 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
drhd654be82005-09-20 17:42:23 +00001958 sqlite3VdbeJumpHere(v, addr);
danielk19774adee202004-05-08 08:23:19 +00001959 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00001960 break;
1961 }
drhcce7d172000-05-31 15:34:51 +00001962 default: {
danielk19774adee202004-05-08 08:23:19 +00001963 sqlite3ExprCode(pParse, pExpr);
1964 sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001965 break;
1966 }
1967 }
drhffe07b22005-11-03 00:41:17 +00001968 pParse->ckOffset = ckOffset;
drhcce7d172000-05-31 15:34:51 +00001969}
1970
1971/*
drh66b89c82000-11-28 20:47:17 +00001972** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00001973** to the label "dest" if the expression is false but execution
1974** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00001975**
1976** If the expression evaluates to NULL (neither true nor false) then
1977** jump if jumpIfNull is true or fall through if jumpIfNull is false.
drhcce7d172000-05-31 15:34:51 +00001978*/
danielk19774adee202004-05-08 08:23:19 +00001979void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001980 Vdbe *v = pParse->pVdbe;
1981 int op = 0;
drhffe07b22005-11-03 00:41:17 +00001982 int ckOffset = pParse->ckOffset;
drhdaffd0e2001-04-11 14:28:42 +00001983 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00001984
1985 /* The value of pExpr->op and op are related as follows:
1986 **
1987 ** pExpr->op op
1988 ** --------- ----------
1989 ** TK_ISNULL OP_NotNull
1990 ** TK_NOTNULL OP_IsNull
1991 ** TK_NE OP_Eq
1992 ** TK_EQ OP_Ne
1993 ** TK_GT OP_Le
1994 ** TK_LE OP_Gt
1995 ** TK_GE OP_Lt
1996 ** TK_LT OP_Ge
1997 **
1998 ** For other values of pExpr->op, op is undefined and unused.
1999 ** The value of TK_ and OP_ constants are arranged such that we
2000 ** can compute the mapping above using the following expression.
2001 ** Assert()s verify that the computation is correct.
2002 */
2003 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
2004
2005 /* Verify correct alignment of TK_ and OP_ constants
2006 */
2007 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
2008 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
2009 assert( pExpr->op!=TK_NE || op==OP_Eq );
2010 assert( pExpr->op!=TK_EQ || op==OP_Ne );
2011 assert( pExpr->op!=TK_LT || op==OP_Ge );
2012 assert( pExpr->op!=TK_LE || op==OP_Gt );
2013 assert( pExpr->op!=TK_GT || op==OP_Le );
2014 assert( pExpr->op!=TK_GE || op==OP_Lt );
2015
drhcce7d172000-05-31 15:34:51 +00002016 switch( pExpr->op ){
2017 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00002018 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
2019 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002020 break;
2021 }
2022 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00002023 int d2 = sqlite3VdbeMakeLabel(v);
2024 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
2025 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
2026 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00002027 break;
2028 }
2029 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00002030 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002031 break;
2032 }
2033 case TK_LT:
2034 case TK_LE:
2035 case TK_GT:
2036 case TK_GE:
2037 case TK_NE:
2038 case TK_EQ: {
danielk19774adee202004-05-08 08:23:19 +00002039 sqlite3ExprCode(pParse, pExpr->pLeft);
2040 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00002041 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00002042 break;
2043 }
drhcce7d172000-05-31 15:34:51 +00002044 case TK_ISNULL:
2045 case TK_NOTNULL: {
danielk19774adee202004-05-08 08:23:19 +00002046 sqlite3ExprCode(pParse, pExpr->pLeft);
2047 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00002048 break;
2049 }
drhfef52082000-06-06 01:50:43 +00002050 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00002051 /* The expression is "x BETWEEN y AND z". It is implemented as:
2052 **
2053 ** 1 IF (x >= y) GOTO 3
2054 ** 2 GOTO <dest>
2055 ** 3 IF (x > z) GOTO <dest>
2056 */
drhfef52082000-06-06 01:50:43 +00002057 int addr;
drhbe5c89a2004-07-26 00:31:09 +00002058 Expr *pLeft = pExpr->pLeft;
2059 Expr *pRight = pExpr->pList->a[0].pExpr;
2060 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00002061 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00002062 sqlite3ExprCode(pParse, pRight);
danielk19774adee202004-05-08 08:23:19 +00002063 addr = sqlite3VdbeCurrentAddr(v);
drhbe5c89a2004-07-26 00:31:09 +00002064 codeCompare(pParse, pLeft, pRight, OP_Ge, addr+3, !jumpIfNull);
2065
danielk19774adee202004-05-08 08:23:19 +00002066 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
2067 sqlite3VdbeAddOp(v, OP_Goto, 0, dest);
drhbe5c89a2004-07-26 00:31:09 +00002068 pRight = pExpr->pList->a[1].pExpr;
2069 sqlite3ExprCode(pParse, pRight);
2070 codeCompare(pParse, pLeft, pRight, OP_Gt, dest, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00002071 break;
2072 }
drhcce7d172000-05-31 15:34:51 +00002073 default: {
danielk19774adee202004-05-08 08:23:19 +00002074 sqlite3ExprCode(pParse, pExpr);
2075 sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00002076 break;
2077 }
2078 }
drhffe07b22005-11-03 00:41:17 +00002079 pParse->ckOffset = ckOffset;
drhcce7d172000-05-31 15:34:51 +00002080}
drh22827922000-06-06 17:27:05 +00002081
2082/*
2083** Do a deep comparison of two expression trees. Return TRUE (non-zero)
2084** if they are identical and return FALSE if they differ in any way.
2085*/
danielk19774adee202004-05-08 08:23:19 +00002086int sqlite3ExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00002087 int i;
2088 if( pA==0 ){
2089 return pB==0;
2090 }else if( pB==0 ){
2091 return 0;
2092 }
2093 if( pA->op!=pB->op ) return 0;
drhfd357972005-09-09 01:33:19 +00002094 if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 0;
danielk19774adee202004-05-08 08:23:19 +00002095 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
2096 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00002097 if( pA->pList ){
2098 if( pB->pList==0 ) return 0;
2099 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
2100 for(i=0; i<pA->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00002101 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00002102 return 0;
2103 }
2104 }
2105 }else if( pB->pList ){
2106 return 0;
2107 }
2108 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00002109 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drh22827922000-06-06 17:27:05 +00002110 if( pA->token.z ){
2111 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00002112 if( pB->token.n!=pA->token.n ) return 0;
drh2646da72005-12-09 20:02:05 +00002113 if( sqlite3StrNICmp((char*)pA->token.z,(char*)pB->token.z,pB->token.n)!=0 ){
2114 return 0;
2115 }
drh22827922000-06-06 17:27:05 +00002116 }
2117 return 1;
2118}
2119
drh13449892005-09-07 21:22:45 +00002120
drh22827922000-06-06 17:27:05 +00002121/*
drh13449892005-09-07 21:22:45 +00002122** Add a new element to the pAggInfo->aCol[] array. Return the index of
2123** the new element. Return a negative number if malloc fails.
drh22827922000-06-06 17:27:05 +00002124*/
drh13449892005-09-07 21:22:45 +00002125static int addAggInfoColumn(AggInfo *pInfo){
2126 int i;
2127 i = sqlite3ArrayAllocate((void**)&pInfo->aCol, sizeof(pInfo->aCol[0]), 3);
2128 if( i<0 ){
2129 return -1;
drh22827922000-06-06 17:27:05 +00002130 }
drh13449892005-09-07 21:22:45 +00002131 return i;
2132}
2133
2134/*
2135** Add a new element to the pAggInfo->aFunc[] array. Return the index of
2136** the new element. Return a negative number if malloc fails.
2137*/
2138static int addAggInfoFunc(AggInfo *pInfo){
2139 int i;
2140 i = sqlite3ArrayAllocate((void**)&pInfo->aFunc, sizeof(pInfo->aFunc[0]), 2);
2141 if( i<0 ){
2142 return -1;
2143 }
2144 return i;
2145}
drh22827922000-06-06 17:27:05 +00002146
2147/*
drh626a8792005-01-17 22:08:19 +00002148** This is an xFunc for walkExprTree() used to implement
2149** sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates
2150** for additional information.
drh22827922000-06-06 17:27:05 +00002151**
drh626a8792005-01-17 22:08:19 +00002152** This routine analyzes the aggregate function at pExpr.
drh22827922000-06-06 17:27:05 +00002153*/
drh626a8792005-01-17 22:08:19 +00002154static int analyzeAggregate(void *pArg, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00002155 int i;
danielk1977a58fdfb2005-02-08 07:50:40 +00002156 NameContext *pNC = (NameContext *)pArg;
2157 Parse *pParse = pNC->pParse;
2158 SrcList *pSrcList = pNC->pSrcList;
drh13449892005-09-07 21:22:45 +00002159 AggInfo *pAggInfo = pNC->pAggInfo;
2160
drh22827922000-06-06 17:27:05 +00002161
drh22827922000-06-06 17:27:05 +00002162 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00002163 case TK_COLUMN: {
drh13449892005-09-07 21:22:45 +00002164 /* Check to see if the column is in one of the tables in the FROM
2165 ** clause of the aggregate query */
2166 if( pSrcList ){
2167 struct SrcList_item *pItem = pSrcList->a;
2168 for(i=0; i<pSrcList->nSrc; i++, pItem++){
2169 struct AggInfo_col *pCol;
2170 if( pExpr->iTable==pItem->iCursor ){
2171 /* If we reach this point, it means that pExpr refers to a table
2172 ** that is in the FROM clause of the aggregate query.
2173 **
2174 ** Make an entry for the column in pAggInfo->aCol[] if there
2175 ** is not an entry there already.
2176 */
2177 pCol = pAggInfo->aCol;
2178 for(i=0; i<pAggInfo->nColumn; i++, pCol++){
2179 if( pCol->iTable==pExpr->iTable &&
2180 pCol->iColumn==pExpr->iColumn ){
2181 break;
2182 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002183 }
drh13449892005-09-07 21:22:45 +00002184 if( i>=pAggInfo->nColumn && (i = addAggInfoColumn(pAggInfo))>=0 ){
2185 pCol = &pAggInfo->aCol[i];
2186 pCol->iTable = pExpr->iTable;
2187 pCol->iColumn = pExpr->iColumn;
2188 pCol->iMem = pParse->nMem++;
2189 pCol->iSorterColumn = -1;
drh5774b802005-09-07 22:48:16 +00002190 pCol->pExpr = pExpr;
drh13449892005-09-07 21:22:45 +00002191 if( pAggInfo->pGroupBy ){
2192 int j, n;
2193 ExprList *pGB = pAggInfo->pGroupBy;
2194 struct ExprList_item *pTerm = pGB->a;
2195 n = pGB->nExpr;
2196 for(j=0; j<n; j++, pTerm++){
2197 Expr *pE = pTerm->pExpr;
2198 if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable &&
2199 pE->iColumn==pExpr->iColumn ){
2200 pCol->iSorterColumn = j;
2201 break;
2202 }
2203 }
2204 }
2205 if( pCol->iSorterColumn<0 ){
2206 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
2207 }
2208 }
2209 /* There is now an entry for pExpr in pAggInfo->aCol[] (either
2210 ** because it was there before or because we just created it).
2211 ** Convert the pExpr to be a TK_AGG_COLUMN referring to that
2212 ** pAggInfo->aCol[] entry.
2213 */
2214 pExpr->pAggInfo = pAggInfo;
2215 pExpr->op = TK_AGG_COLUMN;
2216 pExpr->iAgg = i;
2217 break;
2218 } /* endif pExpr->iTable==pItem->iCursor */
2219 } /* end loop over pSrcList */
drh22827922000-06-06 17:27:05 +00002220 }
drh626a8792005-01-17 22:08:19 +00002221 return 1;
drh22827922000-06-06 17:27:05 +00002222 }
2223 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00002224 /* The pNC->nDepth==0 test causes aggregate functions in subqueries
2225 ** to be ignored */
danielk1977a58fdfb2005-02-08 07:50:40 +00002226 if( pNC->nDepth==0 ){
drh13449892005-09-07 21:22:45 +00002227 /* Check to see if pExpr is a duplicate of another aggregate
2228 ** function that is already in the pAggInfo structure
2229 */
2230 struct AggInfo_func *pItem = pAggInfo->aFunc;
2231 for(i=0; i<pAggInfo->nFunc; i++, pItem++){
2232 if( sqlite3ExprCompare(pItem->pExpr, pExpr) ){
danielk1977a58fdfb2005-02-08 07:50:40 +00002233 break;
2234 }
drh22827922000-06-06 17:27:05 +00002235 }
drh13449892005-09-07 21:22:45 +00002236 if( i>=pAggInfo->nFunc ){
2237 /* pExpr is original. Make a new entry in pAggInfo->aFunc[]
2238 */
danielk197714db2662006-01-09 16:12:04 +00002239 u8 enc = ENC(pParse->db);
drh13449892005-09-07 21:22:45 +00002240 i = addAggInfoFunc(pAggInfo);
2241 if( i>=0 ){
2242 pItem = &pAggInfo->aFunc[i];
2243 pItem->pExpr = pExpr;
2244 pItem->iMem = pParse->nMem++;
2245 pItem->pFunc = sqlite3FindFunction(pParse->db,
drh2646da72005-12-09 20:02:05 +00002246 (char*)pExpr->token.z, pExpr->token.n,
drh13449892005-09-07 21:22:45 +00002247 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
drhfd357972005-09-09 01:33:19 +00002248 if( pExpr->flags & EP_Distinct ){
2249 pItem->iDistinct = pParse->nTab++;
2250 }else{
2251 pItem->iDistinct = -1;
2252 }
drh13449892005-09-07 21:22:45 +00002253 }
danielk1977a58fdfb2005-02-08 07:50:40 +00002254 }
drh13449892005-09-07 21:22:45 +00002255 /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
2256 */
danielk1977a58fdfb2005-02-08 07:50:40 +00002257 pExpr->iAgg = i;
drh13449892005-09-07 21:22:45 +00002258 pExpr->pAggInfo = pAggInfo;
danielk1977a58fdfb2005-02-08 07:50:40 +00002259 return 1;
drh22827922000-06-06 17:27:05 +00002260 }
drh22827922000-06-06 17:27:05 +00002261 }
2262 }
drh13449892005-09-07 21:22:45 +00002263
2264 /* Recursively walk subqueries looking for TK_COLUMN nodes that need
2265 ** to be changed to TK_AGG_COLUMN. But increment nDepth so that
2266 ** TK_AGG_FUNCTION nodes in subqueries will be unchanged.
2267 */
danielk1977a58fdfb2005-02-08 07:50:40 +00002268 if( pExpr->pSelect ){
2269 pNC->nDepth++;
2270 walkSelectExpr(pExpr->pSelect, analyzeAggregate, pNC);
2271 pNC->nDepth--;
2272 }
drh626a8792005-01-17 22:08:19 +00002273 return 0;
2274}
2275
2276/*
2277** Analyze the given expression looking for aggregate functions and
2278** for variables that need to be added to the pParse->aAgg[] array.
2279** Make additional entries to the pParse->aAgg[] array as necessary.
2280**
2281** This routine should only be called after the expression has been
2282** analyzed by sqlite3ExprResolveNames().
2283**
2284** If errors are seen, leave an error message in zErrMsg and return
2285** the number of errors.
2286*/
danielk1977a58fdfb2005-02-08 07:50:40 +00002287int sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
2288 int nErr = pNC->pParse->nErr;
2289 walkExprTree(pExpr, analyzeAggregate, pNC);
2290 return pNC->pParse->nErr - nErr;
drh22827922000-06-06 17:27:05 +00002291}
drh5d9a4af2005-08-30 00:54:01 +00002292
2293/*
2294** Call sqlite3ExprAnalyzeAggregates() for every expression in an
2295** expression list. Return the number of errors.
2296**
2297** If an error is found, the analysis is cut short.
2298*/
2299int sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){
2300 struct ExprList_item *pItem;
2301 int i;
2302 int nErr = 0;
2303 if( pList ){
2304 for(pItem=pList->a, i=0; nErr==0 && i<pList->nExpr; i++, pItem++){
2305 nErr += sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
2306 }
2307 }
2308 return nErr;
2309}