blob: 86cb54ad3982e3c503f09e4fe1e989a81aa0807b [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**
drhfa6bc002004-09-07 16:19:52 +000015** $Id: expr.c,v 1.161 2004/09/07 16:19:53 drh Exp $
drhcce7d172000-05-31 15:34:51 +000016*/
17#include "sqliteInt.h"
drh04738cb2002-06-02 18:19:00 +000018#include <ctype.h>
drha2e00042002-01-22 03:13:42 +000019
danielk1977e014a832004-05-17 10:48:57 +000020char const *sqlite3AffinityString(char affinity){
21 switch( affinity ){
22 case SQLITE_AFF_INTEGER: return "i";
23 case SQLITE_AFF_NUMERIC: return "n";
24 case SQLITE_AFF_TEXT: return "t";
25 case SQLITE_AFF_NONE: return "o";
26 default:
27 assert(0);
28 }
danielk1977e0d4b062004-06-28 01:11:46 +000029 return 0;
danielk1977e014a832004-05-17 10:48:57 +000030}
31
32
33/*
34** Return the 'affinity' of the expression pExpr if any.
35**
36** If pExpr is a column, a reference to a column via an 'AS' alias,
37** or a sub-select with a column as the return value, then the
38** affinity of that column is returned. Otherwise, 0x00 is returned,
39** indicating no affinity for the expression.
40**
41** i.e. the WHERE clause expresssions in the following statements all
42** have an affinity:
43**
44** CREATE TABLE t1(a);
45** SELECT * FROM t1 WHERE a;
46** SELECT a AS b FROM t1 WHERE b;
47** SELECT * FROM t1 WHERE (select a from t1);
48*/
danielk1977bf3b7212004-05-18 10:06:24 +000049char sqlite3ExprAffinity(Expr *pExpr){
danielk1977a37cdde2004-05-16 11:15:36 +000050 if( pExpr->op==TK_AS ){
danielk1977bf3b7212004-05-18 10:06:24 +000051 return sqlite3ExprAffinity(pExpr->pLeft);
danielk1977a37cdde2004-05-16 11:15:36 +000052 }
53 if( pExpr->op==TK_SELECT ){
danielk1977bf3b7212004-05-18 10:06:24 +000054 return sqlite3ExprAffinity(pExpr->pSelect->pEList->a[0].pExpr);
danielk1977a37cdde2004-05-16 11:15:36 +000055 }
56 return pExpr->affinity;
57}
58
drh53db1452004-05-20 13:54:53 +000059/*
danielk19770202b292004-06-09 09:55:16 +000060** Return the default collation sequence for the expression pExpr. If
61** there is no default collation type, return 0.
62*/
danielk19777cedc8d2004-06-10 10:50:08 +000063CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
64 CollSeq *pColl = 0;
danielk19770202b292004-06-09 09:55:16 +000065 if( pExpr ){
danielk19777cedc8d2004-06-10 10:50:08 +000066 pColl = pExpr->pColl;
67 if( pExpr->op==TK_AS && !pColl ){
68 return sqlite3ExprCollSeq(pParse, pExpr->pLeft);
danielk19770202b292004-06-09 09:55:16 +000069 }
70 }
danielk19777cedc8d2004-06-10 10:50:08 +000071 if( sqlite3CheckCollSeq(pParse, pColl) ){
72 pColl = 0;
73 }
74 return pColl;
danielk19770202b292004-06-09 09:55:16 +000075}
76
77/*
drh53db1452004-05-20 13:54:53 +000078** pExpr is the left operand of a comparison operator. aff2 is the
79** type affinity of the right operand. This routine returns the
80** type affinity that should be used for the comparison operator.
81*/
danielk1977e014a832004-05-17 10:48:57 +000082char sqlite3CompareAffinity(Expr *pExpr, char aff2){
danielk1977bf3b7212004-05-18 10:06:24 +000083 char aff1 = sqlite3ExprAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +000084 if( aff1 && aff2 ){
85 /* Both sides of the comparison are columns. If one has numeric or
86 ** integer affinity, use that. Otherwise use no affinity.
87 */
88 if( aff1==SQLITE_AFF_INTEGER || aff2==SQLITE_AFF_INTEGER ){
89 return SQLITE_AFF_INTEGER;
90 }else if( aff1==SQLITE_AFF_NUMERIC || aff2==SQLITE_AFF_NUMERIC ){
91 return SQLITE_AFF_NUMERIC;
92 }else{
93 return SQLITE_AFF_NONE;
94 }
95 }else if( !aff1 && !aff2 ){
drh5f6a87b2004-07-19 00:39:45 +000096 /* Neither side of the comparison is a column. Compare the
97 ** results directly.
danielk1977e014a832004-05-17 10:48:57 +000098 */
drh5f6a87b2004-07-19 00:39:45 +000099 /* return SQLITE_AFF_NUMERIC; // Ticket #805 */
100 return SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +0000101 }else{
102 /* One side is a column, the other is not. Use the columns affinity. */
103 return (aff1 + aff2);
104 }
105}
106
drh53db1452004-05-20 13:54:53 +0000107/*
108** pExpr is a comparison operator. Return the type affinity that should
109** be applied to both operands prior to doing the comparison.
110*/
danielk1977e014a832004-05-17 10:48:57 +0000111static char comparisonAffinity(Expr *pExpr){
112 char aff;
113 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
114 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
115 pExpr->op==TK_NE );
116 assert( pExpr->pLeft );
danielk1977bf3b7212004-05-18 10:06:24 +0000117 aff = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +0000118 if( pExpr->pRight ){
119 aff = sqlite3CompareAffinity(pExpr->pRight, aff);
120 }
121 else if( pExpr->pSelect ){
122 aff = sqlite3CompareAffinity(pExpr->pSelect->pEList->a[0].pExpr, aff);
123 }
124 else if( !aff ){
125 aff = SQLITE_AFF_NUMERIC;
126 }
127 return aff;
128}
129
130/*
131** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
132** idx_affinity is the affinity of an indexed column. Return true
133** if the index with affinity idx_affinity may be used to implement
134** the comparison in pExpr.
135*/
136int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){
137 char aff = comparisonAffinity(pExpr);
138 return
139 (aff==SQLITE_AFF_NONE) ||
140 (aff==SQLITE_AFF_NUMERIC && idx_affinity==SQLITE_AFF_INTEGER) ||
141 (aff==SQLITE_AFF_INTEGER && idx_affinity==SQLITE_AFF_NUMERIC) ||
142 (aff==idx_affinity);
143}
144
danielk1977a37cdde2004-05-16 11:15:36 +0000145/*
146** Return the P1 value that should be used for a binary comparison
147** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
148** If jumpIfNull is true, then set the low byte of the returned
149** P1 value to tell the opcode to jump if either expression
150** evaluates to NULL.
151*/
danielk1977e014a832004-05-17 10:48:57 +0000152static int binaryCompareP1(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){
danielk1977bf3b7212004-05-18 10:06:24 +0000153 char aff = sqlite3ExprAffinity(pExpr2);
danielk1977e014a832004-05-17 10:48:57 +0000154 return (((int)sqlite3CompareAffinity(pExpr1, aff))<<8)+(jumpIfNull?1:0);
danielk1977a37cdde2004-05-16 11:15:36 +0000155}
156
drha2e00042002-01-22 03:13:42 +0000157/*
danielk19770202b292004-06-09 09:55:16 +0000158** Return a pointer to the collation sequence that should be used by
159** a binary comparison operator comparing pLeft and pRight.
160**
161** If the left hand expression has a collating sequence type, then it is
162** used. Otherwise the collation sequence for the right hand expression
163** is used, or the default (BINARY) if neither expression has a collating
164** type.
165*/
danielk19777cedc8d2004-06-10 10:50:08 +0000166static CollSeq* binaryCompareCollSeq(Parse *pParse, Expr *pLeft, Expr *pRight){
167 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pLeft);
danielk19770202b292004-06-09 09:55:16 +0000168 if( !pColl ){
danielk19777cedc8d2004-06-10 10:50:08 +0000169 pColl = sqlite3ExprCollSeq(pParse, pRight);
danielk19770202b292004-06-09 09:55:16 +0000170 }
171 return pColl;
172}
173
174/*
drhbe5c89a2004-07-26 00:31:09 +0000175** Generate code for a comparison operator.
176*/
177static int codeCompare(
178 Parse *pParse, /* The parsing (and code generating) context */
179 Expr *pLeft, /* The left operand */
180 Expr *pRight, /* The right operand */
181 int opcode, /* The comparison opcode */
182 int dest, /* Jump here if true. */
183 int jumpIfNull /* If true, jump if either operand is NULL */
184){
185 int p1 = binaryCompareP1(pLeft, pRight, jumpIfNull);
186 CollSeq *p3 = binaryCompareCollSeq(pParse, pLeft, pRight);
187 return sqlite3VdbeOp3(pParse->pVdbe, opcode, p1, dest, (void *)p3, P3_COLLSEQ);
188}
189
190/*
drha76b5df2002-02-23 02:32:10 +0000191** Construct a new expression node and return a pointer to it. Memory
192** for this node is obtained from sqliteMalloc(). The calling function
193** is responsible for making sure the node eventually gets freed.
194*/
danielk19774adee202004-05-08 08:23:19 +0000195Expr *sqlite3Expr(int op, Expr *pLeft, Expr *pRight, Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000196 Expr *pNew;
197 pNew = sqliteMalloc( sizeof(Expr) );
198 if( pNew==0 ){
drh4efc4752004-01-16 15:55:37 +0000199 /* When malloc fails, we leak memory from pLeft and pRight */
drha76b5df2002-02-23 02:32:10 +0000200 return 0;
201 }
202 pNew->op = op;
203 pNew->pLeft = pLeft;
204 pNew->pRight = pRight;
205 if( pToken ){
drh4b59ab52002-08-24 18:24:51 +0000206 assert( pToken->dyn==0 );
drha76b5df2002-02-23 02:32:10 +0000207 pNew->token = *pToken;
drh6977fea2002-10-22 23:38:04 +0000208 pNew->span = *pToken;
drha76b5df2002-02-23 02:32:10 +0000209 }else{
drh4efc4752004-01-16 15:55:37 +0000210 assert( pNew->token.dyn==0 );
211 assert( pNew->token.z==0 );
212 assert( pNew->token.n==0 );
drh6977fea2002-10-22 23:38:04 +0000213 if( pLeft && pRight ){
danielk19774adee202004-05-08 08:23:19 +0000214 sqlite3ExprSpan(pNew, &pLeft->span, &pRight->span);
drh6977fea2002-10-22 23:38:04 +0000215 }else{
216 pNew->span = pNew->token;
217 }
drha76b5df2002-02-23 02:32:10 +0000218 }
drha76b5df2002-02-23 02:32:10 +0000219 return pNew;
220}
221
222/*
drh91bb0ee2004-09-01 03:06:34 +0000223** Join two expressions using an AND operator. If either expression is
224** NULL, then just return the other expression.
225*/
226Expr *sqlite3ExprAnd(Expr *pLeft, Expr *pRight){
227 if( pLeft==0 ){
228 return pRight;
229 }else if( pRight==0 ){
230 return pLeft;
231 }else{
232 return sqlite3Expr(TK_AND, pLeft, pRight, 0);
233 }
234}
235
236/*
drh6977fea2002-10-22 23:38:04 +0000237** Set the Expr.span field of the given expression to span all
drha76b5df2002-02-23 02:32:10 +0000238** text between the two given tokens.
239*/
danielk19774adee202004-05-08 08:23:19 +0000240void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
drh4efc4752004-01-16 15:55:37 +0000241 assert( pRight!=0 );
242 assert( pLeft!=0 );
drh71c697e2004-08-08 23:39:19 +0000243 if( !sqlite3_malloc_failed && pRight->z && pLeft->z ){
drh4b59ab52002-08-24 18:24:51 +0000244 if( pLeft->dyn==0 && pRight->dyn==0 ){
drh6977fea2002-10-22 23:38:04 +0000245 pExpr->span.z = pLeft->z;
246 pExpr->span.n = pRight->n + Addr(pRight->z) - Addr(pLeft->z);
drh4b59ab52002-08-24 18:24:51 +0000247 }else{
drh6977fea2002-10-22 23:38:04 +0000248 pExpr->span.z = 0;
drh4b59ab52002-08-24 18:24:51 +0000249 }
drha76b5df2002-02-23 02:32:10 +0000250 }
251}
252
253/*
254** Construct a new expression node for a function with multiple
255** arguments.
256*/
danielk19774adee202004-05-08 08:23:19 +0000257Expr *sqlite3ExprFunction(ExprList *pList, Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000258 Expr *pNew;
259 pNew = sqliteMalloc( sizeof(Expr) );
260 if( pNew==0 ){
danielk19774adee202004-05-08 08:23:19 +0000261 /* sqlite3ExprListDelete(pList); // Leak pList when malloc fails */
drha76b5df2002-02-23 02:32:10 +0000262 return 0;
263 }
264 pNew->op = TK_FUNCTION;
265 pNew->pList = pList;
266 if( pToken ){
drh4b59ab52002-08-24 18:24:51 +0000267 assert( pToken->dyn==0 );
drha76b5df2002-02-23 02:32:10 +0000268 pNew->token = *pToken;
269 }else{
270 pNew->token.z = 0;
drha76b5df2002-02-23 02:32:10 +0000271 }
drh6977fea2002-10-22 23:38:04 +0000272 pNew->span = pNew->token;
drha76b5df2002-02-23 02:32:10 +0000273 return pNew;
274}
275
276/*
drhfa6bc002004-09-07 16:19:52 +0000277** Assign a variable number to an expression that encodes a wildcard
278** in the original SQL statement.
279**
280** Wildcards consisting of a single "?" are assigned the next sequential
281** variable number.
282**
283** Wildcards of the form "?nnn" are assigned the number "nnn". We make
284** sure "nnn" is not too be to avoid a denial of service attack when
285** the SQL statement comes from an external source.
286**
287** Wildcards of the form ":aaa" or "$aaa" are assigned the same number
288** as the previous instance of the same wildcard. Or if this is the first
289** instance of the wildcard, the next sequenial variable number is
290** assigned.
291*/
292void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){
293 Token *pToken;
294 if( pExpr==0 ) return;
295 pToken = &pExpr->token;
296 assert( pToken->n>=1 );
297 assert( pToken->z!=0 );
298 assert( pToken->z[0]!=0 );
299 if( pToken->n==1 ){
300 /* Wildcard of the form "?". Assign the next variable number */
301 pExpr->iTable = ++pParse->nVar;
302 }else if( pToken->z[0]=='?' ){
303 /* Wildcard of the form "?nnn". Convert "nnn" to an integer and
304 ** use it as the variable number */
305 int i;
306 pExpr->iTable = i = atoi(&pToken->z[1]);
307 if( i<1 || i>SQLITE_MAX_VARIABLE_NUMBER ){
308 sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
309 SQLITE_MAX_VARIABLE_NUMBER);
310 }
311 if( i>pParse->nVar ){
312 pParse->nVar = i;
313 }
314 }else{
315 /* Wildcards of the form ":aaa" or "$aaa". Reuse the same variable
316 ** number as the prior appearance of the same name, or if the name
317 ** has never appeared before, reuse the same variable number
318 */
319 int i, n;
320 n = pToken->n;
321 for(i=0; i<pParse->nVarExpr; i++){
322 Expr *pE;
323 if( (pE = pParse->apVarExpr[i])!=0
324 && pE->token.n==n
325 && memcmp(pE->token.z, pToken->z, n)==0 ){
326 pExpr->iTable = pE->iTable;
327 break;
328 }
329 }
330 if( i>=pParse->nVarExpr ){
331 pExpr->iTable = ++pParse->nVar;
332 if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){
333 pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10;
334 pParse->apVarExpr = sqliteRealloc(pParse->apVarExpr,
335 pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0]) );
336 }
337 if( !sqlite3_malloc_failed ){
338 assert( pParse->apVarExpr!=0 );
339 pParse->apVarExpr[pParse->nVarExpr++] = pExpr;
340 }
341 }
342 }
343}
344
345/*
drha2e00042002-01-22 03:13:42 +0000346** Recursively delete an expression tree.
347*/
danielk19774adee202004-05-08 08:23:19 +0000348void sqlite3ExprDelete(Expr *p){
drha2e00042002-01-22 03:13:42 +0000349 if( p==0 ) return;
drh4efc4752004-01-16 15:55:37 +0000350 if( p->span.dyn ) sqliteFree((char*)p->span.z);
351 if( p->token.dyn ) sqliteFree((char*)p->token.z);
danielk19774adee202004-05-08 08:23:19 +0000352 sqlite3ExprDelete(p->pLeft);
353 sqlite3ExprDelete(p->pRight);
354 sqlite3ExprListDelete(p->pList);
355 sqlite3SelectDelete(p->pSelect);
drha2e00042002-01-22 03:13:42 +0000356 sqliteFree(p);
357}
358
drha76b5df2002-02-23 02:32:10 +0000359
360/*
drhff78bd22002-02-27 01:47:11 +0000361** The following group of routines make deep copies of expressions,
362** expression lists, ID lists, and select statements. The copies can
363** be deleted (by being passed to their respective ...Delete() routines)
364** without effecting the originals.
365**
danielk19774adee202004-05-08 08:23:19 +0000366** The expression list, ID, and source lists return by sqlite3ExprListDup(),
367** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
drhad3cab52002-05-24 02:04:32 +0000368** by subsequent calls to sqlite*ListAppend() routines.
drhff78bd22002-02-27 01:47:11 +0000369**
drhad3cab52002-05-24 02:04:32 +0000370** Any tables that the SrcList might point to are not duplicated.
drhff78bd22002-02-27 01:47:11 +0000371*/
danielk19774adee202004-05-08 08:23:19 +0000372Expr *sqlite3ExprDup(Expr *p){
drhff78bd22002-02-27 01:47:11 +0000373 Expr *pNew;
374 if( p==0 ) return 0;
drhfcb78a42003-01-18 20:11:05 +0000375 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000376 if( pNew==0 ) return 0;
drh3b167c72002-06-28 12:18:47 +0000377 memcpy(pNew, p, sizeof(*pNew));
drh6977fea2002-10-22 23:38:04 +0000378 if( p->token.z!=0 ){
drh4b59ab52002-08-24 18:24:51 +0000379 pNew->token.z = sqliteStrDup(p->token.z);
380 pNew->token.dyn = 1;
381 }else{
drh4efc4752004-01-16 15:55:37 +0000382 assert( pNew->token.z==0 );
drh4b59ab52002-08-24 18:24:51 +0000383 }
drh6977fea2002-10-22 23:38:04 +0000384 pNew->span.z = 0;
danielk19774adee202004-05-08 08:23:19 +0000385 pNew->pLeft = sqlite3ExprDup(p->pLeft);
386 pNew->pRight = sqlite3ExprDup(p->pRight);
387 pNew->pList = sqlite3ExprListDup(p->pList);
388 pNew->pSelect = sqlite3SelectDup(p->pSelect);
drhff78bd22002-02-27 01:47:11 +0000389 return pNew;
390}
danielk19774adee202004-05-08 08:23:19 +0000391void sqlite3TokenCopy(Token *pTo, Token *pFrom){
drh4b59ab52002-08-24 18:24:51 +0000392 if( pTo->dyn ) sqliteFree((char*)pTo->z);
drh4b59ab52002-08-24 18:24:51 +0000393 if( pFrom->z ){
394 pTo->n = pFrom->n;
395 pTo->z = sqliteStrNDup(pFrom->z, pFrom->n);
396 pTo->dyn = 1;
397 }else{
drh4b59ab52002-08-24 18:24:51 +0000398 pTo->z = 0;
drh4b59ab52002-08-24 18:24:51 +0000399 }
400}
danielk19774adee202004-05-08 08:23:19 +0000401ExprList *sqlite3ExprListDup(ExprList *p){
drhff78bd22002-02-27 01:47:11 +0000402 ExprList *pNew;
drh3e7bc9c2004-02-21 19:17:17 +0000403 struct ExprList_item *pItem;
drhff78bd22002-02-27 01:47:11 +0000404 int i;
405 if( p==0 ) return 0;
406 pNew = sqliteMalloc( sizeof(*pNew) );
407 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000408 pNew->nExpr = pNew->nAlloc = p->nExpr;
drh3e7bc9c2004-02-21 19:17:17 +0000409 pNew->a = pItem = sqliteMalloc( p->nExpr*sizeof(p->a[0]) );
danielk1977e0048402004-06-15 16:51:01 +0000410 if( pItem==0 ){
411 sqliteFree(pNew);
412 return 0;
413 }
drh1bdd9b52004-04-23 17:04:44 +0000414 for(i=0; i<p->nExpr; i++, pItem++){
drh4b59ab52002-08-24 18:24:51 +0000415 Expr *pNewExpr, *pOldExpr;
danielk19774adee202004-05-08 08:23:19 +0000416 pItem->pExpr = pNewExpr = sqlite3ExprDup(pOldExpr = p->a[i].pExpr);
drh6977fea2002-10-22 23:38:04 +0000417 if( pOldExpr->span.z!=0 && pNewExpr ){
418 /* Always make a copy of the span for top-level expressions in the
drh4b59ab52002-08-24 18:24:51 +0000419 ** expression list. The logic in SELECT processing that determines
420 ** the names of columns in the result set needs this information */
danielk19774adee202004-05-08 08:23:19 +0000421 sqlite3TokenCopy(&pNewExpr->span, &pOldExpr->span);
drh4b59ab52002-08-24 18:24:51 +0000422 }
drh1f3e9052002-10-31 00:09:39 +0000423 assert( pNewExpr==0 || pNewExpr->span.z!=0
danielk197724b03fd2004-05-10 10:34:34 +0000424 || pOldExpr->span.z==0 || sqlite3_malloc_failed );
drh3e7bc9c2004-02-21 19:17:17 +0000425 pItem->zName = sqliteStrDup(p->a[i].zName);
426 pItem->sortOrder = p->a[i].sortOrder;
427 pItem->isAgg = p->a[i].isAgg;
428 pItem->done = 0;
drhff78bd22002-02-27 01:47:11 +0000429 }
430 return pNew;
431}
danielk19774adee202004-05-08 08:23:19 +0000432SrcList *sqlite3SrcListDup(SrcList *p){
drhad3cab52002-05-24 02:04:32 +0000433 SrcList *pNew;
434 int i;
drh113088e2003-03-20 01:16:58 +0000435 int nByte;
drhad3cab52002-05-24 02:04:32 +0000436 if( p==0 ) return 0;
drh113088e2003-03-20 01:16:58 +0000437 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
drh4efc4752004-01-16 15:55:37 +0000438 pNew = sqliteMallocRaw( nByte );
drhad3cab52002-05-24 02:04:32 +0000439 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000440 pNew->nSrc = pNew->nAlloc = p->nSrc;
drhad3cab52002-05-24 02:04:32 +0000441 for(i=0; i<p->nSrc; i++){
drh4efc4752004-01-16 15:55:37 +0000442 struct SrcList_item *pNewItem = &pNew->a[i];
443 struct SrcList_item *pOldItem = &p->a[i];
444 pNewItem->zDatabase = sqliteStrDup(pOldItem->zDatabase);
445 pNewItem->zName = sqliteStrDup(pOldItem->zName);
446 pNewItem->zAlias = sqliteStrDup(pOldItem->zAlias);
447 pNewItem->jointype = pOldItem->jointype;
448 pNewItem->iCursor = pOldItem->iCursor;
449 pNewItem->pTab = 0;
danielk19774adee202004-05-08 08:23:19 +0000450 pNewItem->pSelect = sqlite3SelectDup(pOldItem->pSelect);
451 pNewItem->pOn = sqlite3ExprDup(pOldItem->pOn);
452 pNewItem->pUsing = sqlite3IdListDup(pOldItem->pUsing);
drhad3cab52002-05-24 02:04:32 +0000453 }
454 return pNew;
455}
danielk19774adee202004-05-08 08:23:19 +0000456IdList *sqlite3IdListDup(IdList *p){
drhff78bd22002-02-27 01:47:11 +0000457 IdList *pNew;
458 int i;
459 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000460 pNew = sqliteMallocRaw( sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +0000461 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000462 pNew->nId = pNew->nAlloc = p->nId;
drh4efc4752004-01-16 15:55:37 +0000463 pNew->a = sqliteMallocRaw( p->nId*sizeof(p->a[0]) );
drhe4697f52002-05-23 02:09:03 +0000464 if( pNew->a==0 ) return 0;
drhff78bd22002-02-27 01:47:11 +0000465 for(i=0; i<p->nId; i++){
drh4efc4752004-01-16 15:55:37 +0000466 struct IdList_item *pNewItem = &pNew->a[i];
467 struct IdList_item *pOldItem = &p->a[i];
468 pNewItem->zName = sqliteStrDup(pOldItem->zName);
469 pNewItem->idx = pOldItem->idx;
drhff78bd22002-02-27 01:47:11 +0000470 }
471 return pNew;
472}
danielk19774adee202004-05-08 08:23:19 +0000473Select *sqlite3SelectDup(Select *p){
drhff78bd22002-02-27 01:47:11 +0000474 Select *pNew;
475 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000476 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000477 if( pNew==0 ) return 0;
478 pNew->isDistinct = p->isDistinct;
danielk19774adee202004-05-08 08:23:19 +0000479 pNew->pEList = sqlite3ExprListDup(p->pEList);
480 pNew->pSrc = sqlite3SrcListDup(p->pSrc);
481 pNew->pWhere = sqlite3ExprDup(p->pWhere);
482 pNew->pGroupBy = sqlite3ExprListDup(p->pGroupBy);
483 pNew->pHaving = sqlite3ExprDup(p->pHaving);
484 pNew->pOrderBy = sqlite3ExprListDup(p->pOrderBy);
drhff78bd22002-02-27 01:47:11 +0000485 pNew->op = p->op;
danielk19774adee202004-05-08 08:23:19 +0000486 pNew->pPrior = sqlite3SelectDup(p->pPrior);
drhff78bd22002-02-27 01:47:11 +0000487 pNew->nLimit = p->nLimit;
488 pNew->nOffset = p->nOffset;
489 pNew->zSelect = 0;
drh7b58dae2003-07-20 01:16:46 +0000490 pNew->iLimit = -1;
491 pNew->iOffset = -1;
danielk1977dc1bdc42004-06-11 10:51:27 +0000492 pNew->ppOpenTemp = 0;
drhff78bd22002-02-27 01:47:11 +0000493 return pNew;
494}
495
496
497/*
drha76b5df2002-02-23 02:32:10 +0000498** Add a new element to the end of an expression list. If pList is
499** initially NULL, then create a new expression list.
500*/
danielk19774adee202004-05-08 08:23:19 +0000501ExprList *sqlite3ExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){
drha76b5df2002-02-23 02:32:10 +0000502 if( pList==0 ){
503 pList = sqliteMalloc( sizeof(ExprList) );
504 if( pList==0 ){
danielk19774adee202004-05-08 08:23:19 +0000505 /* sqlite3ExprDelete(pExpr); // Leak memory if malloc fails */
drha76b5df2002-02-23 02:32:10 +0000506 return 0;
507 }
drh4efc4752004-01-16 15:55:37 +0000508 assert( pList->nAlloc==0 );
drha76b5df2002-02-23 02:32:10 +0000509 }
drh4305d102003-07-30 12:34:12 +0000510 if( pList->nAlloc<=pList->nExpr ){
drh4305d102003-07-30 12:34:12 +0000511 pList->nAlloc = pList->nAlloc*2 + 4;
drh4efc4752004-01-16 15:55:37 +0000512 pList->a = sqliteRealloc(pList->a, pList->nAlloc*sizeof(pList->a[0]));
513 if( pList->a==0 ){
danielk19774adee202004-05-08 08:23:19 +0000514 /* sqlite3ExprDelete(pExpr); // Leak memory if malloc fails */
drh4efc4752004-01-16 15:55:37 +0000515 pList->nExpr = pList->nAlloc = 0;
drha76b5df2002-02-23 02:32:10 +0000516 return pList;
517 }
drha76b5df2002-02-23 02:32:10 +0000518 }
drh4efc4752004-01-16 15:55:37 +0000519 assert( pList->a!=0 );
520 if( pExpr || pName ){
521 struct ExprList_item *pItem = &pList->a[pList->nExpr++];
522 memset(pItem, 0, sizeof(*pItem));
523 pItem->pExpr = pExpr;
drha99db3b2004-06-19 14:49:12 +0000524 pItem->zName = sqlite3NameFromToken(pName);
drha76b5df2002-02-23 02:32:10 +0000525 }
526 return pList;
527}
528
529/*
530** Delete an entire expression list.
531*/
danielk19774adee202004-05-08 08:23:19 +0000532void sqlite3ExprListDelete(ExprList *pList){
drha76b5df2002-02-23 02:32:10 +0000533 int i;
drhbe5c89a2004-07-26 00:31:09 +0000534 struct ExprList_item *pItem;
drha76b5df2002-02-23 02:32:10 +0000535 if( pList==0 ) return;
drh1bdd9b52004-04-23 17:04:44 +0000536 assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
537 assert( pList->nExpr<=pList->nAlloc );
drhbe5c89a2004-07-26 00:31:09 +0000538 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
539 sqlite3ExprDelete(pItem->pExpr);
540 sqliteFree(pItem->zName);
drha76b5df2002-02-23 02:32:10 +0000541 }
542 sqliteFree(pList->a);
543 sqliteFree(pList);
544}
545
546/*
drhfef52082000-06-06 01:50:43 +0000547** Walk an expression tree. Return 1 if the expression is constant
548** and 0 if it involves variables.
drh23989372002-05-21 13:43:04 +0000549**
550** For the purposes of this function, a double-quoted string (ex: "abc")
551** is considered a variable but a single-quoted string (ex: 'abc') is
552** a constant.
drhfef52082000-06-06 01:50:43 +0000553*/
danielk19774adee202004-05-08 08:23:19 +0000554int sqlite3ExprIsConstant(Expr *p){
drhfef52082000-06-06 01:50:43 +0000555 switch( p->op ){
556 case TK_ID:
drh967e8b72000-06-21 13:59:10 +0000557 case TK_COLUMN:
drhfef52082000-06-06 01:50:43 +0000558 case TK_DOT:
drh7bdc0c12003-04-19 17:27:24 +0000559 case TK_FUNCTION:
drhfef52082000-06-06 01:50:43 +0000560 return 0;
drh7bdc0c12003-04-19 17:27:24 +0000561 case TK_NULL:
drh23989372002-05-21 13:43:04 +0000562 case TK_STRING:
danielk1977c572ef72004-05-27 09:28:41 +0000563 case TK_BLOB:
drh92086432002-01-22 14:11:29 +0000564 case TK_INTEGER:
565 case TK_FLOAT:
drh50457892003-09-06 01:10:47 +0000566 case TK_VARIABLE:
drh92086432002-01-22 14:11:29 +0000567 return 1;
drhfef52082000-06-06 01:50:43 +0000568 default: {
danielk19774adee202004-05-08 08:23:19 +0000569 if( p->pLeft && !sqlite3ExprIsConstant(p->pLeft) ) return 0;
570 if( p->pRight && !sqlite3ExprIsConstant(p->pRight) ) return 0;
drhfef52082000-06-06 01:50:43 +0000571 if( p->pList ){
572 int i;
573 for(i=0; i<p->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +0000574 if( !sqlite3ExprIsConstant(p->pList->a[i].pExpr) ) return 0;
drhfef52082000-06-06 01:50:43 +0000575 }
576 }
drh92086432002-01-22 14:11:29 +0000577 return p->pLeft!=0 || p->pRight!=0 || (p->pList && p->pList->nExpr>0);
drhfef52082000-06-06 01:50:43 +0000578 }
579 }
drh92086432002-01-22 14:11:29 +0000580 return 0;
drhfef52082000-06-06 01:50:43 +0000581}
582
583/*
drh202b2df2004-01-06 01:13:46 +0000584** If the given expression codes a constant integer that is small enough
585** to fit in a 32-bit integer, return 1 and put the value of the integer
586** in *pValue. If the expression is not an integer or if it is too big
587** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
drhe4de1fe2002-06-02 16:09:01 +0000588*/
danielk19774adee202004-05-08 08:23:19 +0000589int sqlite3ExprIsInteger(Expr *p, int *pValue){
drhe4de1fe2002-06-02 16:09:01 +0000590 switch( p->op ){
591 case TK_INTEGER: {
drhfec19aa2004-05-19 20:41:03 +0000592 if( sqlite3GetInt32(p->token.z, pValue) ){
drh202b2df2004-01-06 01:13:46 +0000593 return 1;
594 }
595 break;
drhe4de1fe2002-06-02 16:09:01 +0000596 }
597 case TK_STRING: {
drh4c755c02004-08-08 20:22:17 +0000598 const u8 *z = (u8*)p->token.z;
drhe4de1fe2002-06-02 16:09:01 +0000599 int n = p->token.n;
drhbd790ee2002-06-02 18:22:06 +0000600 if( n>0 && z[0]=='-' ){ z++; n--; }
drhe4de1fe2002-06-02 16:09:01 +0000601 while( n>0 && *z && isdigit(*z) ){ z++; n--; }
drhfec19aa2004-05-19 20:41:03 +0000602 if( n==0 && sqlite3GetInt32(p->token.z, pValue) ){
drhe4de1fe2002-06-02 16:09:01 +0000603 return 1;
604 }
605 break;
606 }
drh4b59ab52002-08-24 18:24:51 +0000607 case TK_UPLUS: {
danielk19774adee202004-05-08 08:23:19 +0000608 return sqlite3ExprIsInteger(p->pLeft, pValue);
drh4b59ab52002-08-24 18:24:51 +0000609 }
drhe4de1fe2002-06-02 16:09:01 +0000610 case TK_UMINUS: {
611 int v;
danielk19774adee202004-05-08 08:23:19 +0000612 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
drhe4de1fe2002-06-02 16:09:01 +0000613 *pValue = -v;
614 return 1;
615 }
616 break;
617 }
618 default: break;
619 }
620 return 0;
621}
622
623/*
drhc4a3c772001-04-04 11:48:57 +0000624** Return TRUE if the given string is a row-id column name.
625*/
danielk19774adee202004-05-08 08:23:19 +0000626int sqlite3IsRowid(const char *z){
627 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
628 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
629 if( sqlite3StrICmp(z, "OID")==0 ) return 1;
drhc4a3c772001-04-04 11:48:57 +0000630 return 0;
631}
632
633/*
drh8141f612004-01-25 22:44:58 +0000634** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
635** that name in the set of source tables in pSrcList and make the pExpr
636** expression node refer back to that source column. The following changes
637** are made to pExpr:
638**
639** pExpr->iDb Set the index in db->aDb[] of the database holding
640** the table.
641** pExpr->iTable Set to the cursor number for the table obtained
642** from pSrcList.
643** pExpr->iColumn Set to the column number within the table.
drh8141f612004-01-25 22:44:58 +0000644** pExpr->op Set to TK_COLUMN.
645** pExpr->pLeft Any expression this points to is deleted
646** pExpr->pRight Any expression this points to is deleted.
647**
648** The pDbToken is the name of the database (the "X"). This value may be
649** NULL meaning that name is of the form Y.Z or Z. Any available database
650** can be used. The pTableToken is the name of the table (the "Y"). This
651** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it
652** means that the form of the name is Z and that columns from any table
653** can be used.
654**
655** If the name cannot be resolved unambiguously, leave an error message
656** in pParse and return non-zero. Return zero on success.
657*/
658static int lookupName(
659 Parse *pParse, /* The parsing context */
660 Token *pDbToken, /* Name of the database containing table, or NULL */
661 Token *pTableToken, /* Name of table containing column, or NULL */
662 Token *pColumnToken, /* Name of the column. */
663 SrcList *pSrcList, /* List of tables used to resolve column names */
664 ExprList *pEList, /* List of expressions used to resolve "AS" */
665 Expr *pExpr /* Make this EXPR node point to the selected column */
666){
667 char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */
668 char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */
669 char *zCol = 0; /* Name of the column. The "Z" */
670 int i, j; /* Loop counters */
671 int cnt = 0; /* Number of matching column names */
672 int cntTab = 0; /* Number of matching table names */
drh9bb575f2004-09-06 17:24:11 +0000673 sqlite3 *db = pParse->db; /* The database */
drh8141f612004-01-25 22:44:58 +0000674
675 assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
drha99db3b2004-06-19 14:49:12 +0000676 zDb = sqlite3NameFromToken(pDbToken);
677 zTab = sqlite3NameFromToken(pTableToken);
678 zCol = sqlite3NameFromToken(pColumnToken);
danielk197724b03fd2004-05-10 10:34:34 +0000679 if( sqlite3_malloc_failed ){
drh8141f612004-01-25 22:44:58 +0000680 return 1; /* Leak memory (zDb and zTab) if malloc fails */
681 }
682 assert( zTab==0 || pEList==0 );
683
684 pExpr->iTable = -1;
685 for(i=0; i<pSrcList->nSrc; i++){
686 struct SrcList_item *pItem = &pSrcList->a[i];
687 Table *pTab = pItem->pTab;
688 Column *pCol;
689
690 if( pTab==0 ) continue;
691 assert( pTab->nCol>0 );
692 if( zTab ){
693 if( pItem->zAlias ){
694 char *zTabName = pItem->zAlias;
danielk19774adee202004-05-08 08:23:19 +0000695 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
drh8141f612004-01-25 22:44:58 +0000696 }else{
697 char *zTabName = pTab->zName;
danielk19774adee202004-05-08 08:23:19 +0000698 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
699 if( zDb!=0 && sqlite3StrICmp(db->aDb[pTab->iDb].zName, zDb)!=0 ){
drh8141f612004-01-25 22:44:58 +0000700 continue;
701 }
702 }
703 }
704 if( 0==(cntTab++) ){
705 pExpr->iTable = pItem->iCursor;
706 pExpr->iDb = pTab->iDb;
707 }
708 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
danielk19774adee202004-05-08 08:23:19 +0000709 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
drh8141f612004-01-25 22:44:58 +0000710 cnt++;
711 pExpr->iTable = pItem->iCursor;
712 pExpr->iDb = pTab->iDb;
713 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
714 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
danielk1977a37cdde2004-05-16 11:15:36 +0000715 pExpr->affinity = pTab->aCol[j].affinity;
danielk19770202b292004-06-09 09:55:16 +0000716 pExpr->pColl = pTab->aCol[j].pColl;
drh8141f612004-01-25 22:44:58 +0000717 break;
718 }
719 }
720 }
721
722 /* If we have not already resolved the name, then maybe
723 ** it is a new.* or old.* trigger argument reference
724 */
725 if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
726 TriggerStack *pTriggerStack = pParse->trigStack;
727 Table *pTab = 0;
danielk19774adee202004-05-08 08:23:19 +0000728 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){
drh8141f612004-01-25 22:44:58 +0000729 pExpr->iTable = pTriggerStack->newIdx;
730 assert( pTriggerStack->pTab );
731 pTab = pTriggerStack->pTab;
danielk19774adee202004-05-08 08:23:19 +0000732 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab) == 0 ){
drh8141f612004-01-25 22:44:58 +0000733 pExpr->iTable = pTriggerStack->oldIdx;
734 assert( pTriggerStack->pTab );
735 pTab = pTriggerStack->pTab;
736 }
737
738 if( pTab ){
739 int j;
740 Column *pCol = pTab->aCol;
741
742 pExpr->iDb = pTab->iDb;
743 cntTab++;
744 for(j=0; j < pTab->nCol; j++, pCol++) {
danielk19774adee202004-05-08 08:23:19 +0000745 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
drh8141f612004-01-25 22:44:58 +0000746 cnt++;
747 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
danielk1977a37cdde2004-05-16 11:15:36 +0000748 pExpr->affinity = pTab->aCol[j].affinity;
danielk19770202b292004-06-09 09:55:16 +0000749 pExpr->pColl = pTab->aCol[j].pColl;
drh8141f612004-01-25 22:44:58 +0000750 break;
751 }
752 }
753 }
754 }
755
756 /*
757 ** Perhaps the name is a reference to the ROWID
758 */
danielk19774adee202004-05-08 08:23:19 +0000759 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
drh8141f612004-01-25 22:44:58 +0000760 cnt = 1;
761 pExpr->iColumn = -1;
danielk1977a37cdde2004-05-16 11:15:36 +0000762 pExpr->affinity = SQLITE_AFF_INTEGER;
drh8141f612004-01-25 22:44:58 +0000763 }
764
765 /*
766 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
767 ** might refer to an result-set alias. This happens, for example, when
768 ** we are resolving names in the WHERE clause of the following command:
769 **
770 ** SELECT a+b AS x FROM table WHERE x<10;
771 **
772 ** In cases like this, replace pExpr with a copy of the expression that
773 ** forms the result set entry ("a+b" in the example) and return immediately.
774 ** Note that the expression in the result set should have already been
775 ** resolved by the time the WHERE clause is resolved.
776 */
777 if( cnt==0 && pEList!=0 ){
778 for(j=0; j<pEList->nExpr; j++){
779 char *zAs = pEList->a[j].zName;
danielk19774adee202004-05-08 08:23:19 +0000780 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
drh8141f612004-01-25 22:44:58 +0000781 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
782 pExpr->op = TK_AS;
783 pExpr->iColumn = j;
danielk19774adee202004-05-08 08:23:19 +0000784 pExpr->pLeft = sqlite3ExprDup(pEList->a[j].pExpr);
drh8141f612004-01-25 22:44:58 +0000785 sqliteFree(zCol);
786 assert( zTab==0 && zDb==0 );
787 return 0;
788 }
789 }
790 }
791
792 /*
793 ** If X and Y are NULL (in other words if only the column name Z is
794 ** supplied) and the value of Z is enclosed in double-quotes, then
795 ** Z is a string literal if it doesn't match any column names. In that
796 ** case, we need to return right away and not make any changes to
797 ** pExpr.
798 */
799 if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
800 sqliteFree(zCol);
801 return 0;
802 }
803
804 /*
805 ** cnt==0 means there was not match. cnt>1 means there were two or
806 ** more matches. Either way, we have an error.
807 */
808 if( cnt!=1 ){
809 char *z = 0;
810 char *zErr;
811 zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s";
812 if( zDb ){
danielk19774adee202004-05-08 08:23:19 +0000813 sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, 0);
drh8141f612004-01-25 22:44:58 +0000814 }else if( zTab ){
danielk19774adee202004-05-08 08:23:19 +0000815 sqlite3SetString(&z, zTab, ".", zCol, 0);
drh8141f612004-01-25 22:44:58 +0000816 }else{
817 z = sqliteStrDup(zCol);
818 }
danielk19774adee202004-05-08 08:23:19 +0000819 sqlite3ErrorMsg(pParse, zErr, z);
drh8141f612004-01-25 22:44:58 +0000820 sqliteFree(z);
821 }
822
823 /* Clean up and return
824 */
825 sqliteFree(zDb);
826 sqliteFree(zTab);
827 sqliteFree(zCol);
danielk19774adee202004-05-08 08:23:19 +0000828 sqlite3ExprDelete(pExpr->pLeft);
drh8141f612004-01-25 22:44:58 +0000829 pExpr->pLeft = 0;
danielk19774adee202004-05-08 08:23:19 +0000830 sqlite3ExprDelete(pExpr->pRight);
drh8141f612004-01-25 22:44:58 +0000831 pExpr->pRight = 0;
832 pExpr->op = TK_COLUMN;
danielk19774adee202004-05-08 08:23:19 +0000833 sqlite3AuthRead(pParse, pExpr, pSrcList);
drh8141f612004-01-25 22:44:58 +0000834 return cnt!=1;
835}
836
837/*
drhcce7d172000-05-31 15:34:51 +0000838** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +0000839** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +0000840** index to the table in the table list and a column offset. The
841** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
842** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +0000843** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +0000844** VDBE cursor number for a cursor that is pointing into the referenced
845** table. The Expr.iColumn value is changed to the index of the column
846** of the referenced table. The Expr.iColumn value for the special
847** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
848** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +0000849**
drhfef52082000-06-06 01:50:43 +0000850** We also check for instances of the IN operator. IN comes in two
851** forms:
852**
853** expr IN (exprlist)
854** and
855** expr IN (SELECT ...)
856**
857** The first form is handled by creating a set holding the list
858** of allowed values. The second form causes the SELECT to generate
859** a temporary table.
860**
861** This routine also looks for scalar SELECTs that are part of an expression.
drh19a775c2000-06-05 18:54:46 +0000862** If it finds any, it generates code to write the value of that select
863** into a memory cell.
drhcce7d172000-05-31 15:34:51 +0000864**
drh967e8b72000-06-21 13:59:10 +0000865** Unknown columns or tables provoke an error. The function returns
drhcce7d172000-05-31 15:34:51 +0000866** the number of errors seen and leaves an error message on pParse->zErrMsg.
867*/
danielk19774adee202004-05-08 08:23:19 +0000868int sqlite3ExprResolveIds(
drha2e00042002-01-22 03:13:42 +0000869 Parse *pParse, /* The parser context */
drh8141f612004-01-25 22:44:58 +0000870 SrcList *pSrcList, /* List of tables used to resolve column names */
drha2e00042002-01-22 03:13:42 +0000871 ExprList *pEList, /* List of expressions used to resolve "AS" */
872 Expr *pExpr /* The expression to be analyzed. */
873){
drh6a3ea0e2003-05-02 14:32:12 +0000874 int i;
875
drh8141f612004-01-25 22:44:58 +0000876 if( pExpr==0 || pSrcList==0 ) return 0;
877 for(i=0; i<pSrcList->nSrc; i++){
878 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab );
drh6a3ea0e2003-05-02 14:32:12 +0000879 }
drhcce7d172000-05-31 15:34:51 +0000880 switch( pExpr->op ){
drh23989372002-05-21 13:43:04 +0000881 /* Double-quoted strings (ex: "abc") are used as identifiers if
882 ** possible. Otherwise they remain as strings. Single-quoted
883 ** strings (ex: 'abc') are always string literals.
884 */
885 case TK_STRING: {
886 if( pExpr->token.z[0]=='\'' ) break;
887 /* Fall thru into the TK_ID case if this is a double-quoted string */
888 }
drh8141f612004-01-25 22:44:58 +0000889 /* A lone identifier is the name of a columnd.
drha2e00042002-01-22 03:13:42 +0000890 */
drhcce7d172000-05-31 15:34:51 +0000891 case TK_ID: {
drh8141f612004-01-25 22:44:58 +0000892 if( lookupName(pParse, 0, 0, &pExpr->token, pSrcList, pEList, pExpr) ){
drhcce7d172000-05-31 15:34:51 +0000893 return 1;
drhed6c8672003-01-12 18:02:16 +0000894 }
drhcce7d172000-05-31 15:34:51 +0000895 break;
896 }
897
drhd24cc422003-03-27 12:51:24 +0000898 /* A table name and column name: ID.ID
899 ** Or a database, table and column: ID.ID.ID
900 */
drhcce7d172000-05-31 15:34:51 +0000901 case TK_DOT: {
drh8141f612004-01-25 22:44:58 +0000902 Token *pColumn;
903 Token *pTable;
904 Token *pDb;
905 Expr *pRight;
drhcce7d172000-05-31 15:34:51 +0000906
drhcce7d172000-05-31 15:34:51 +0000907 pRight = pExpr->pRight;
drhd24cc422003-03-27 12:51:24 +0000908 if( pRight->op==TK_ID ){
drh8141f612004-01-25 22:44:58 +0000909 pDb = 0;
910 pTable = &pExpr->pLeft->token;
911 pColumn = &pRight->token;
drhd24cc422003-03-27 12:51:24 +0000912 }else{
drh8141f612004-01-25 22:44:58 +0000913 assert( pRight->op==TK_DOT );
914 pDb = &pExpr->pLeft->token;
915 pTable = &pRight->pLeft->token;
916 pColumn = &pRight->pRight->token;
drhd24cc422003-03-27 12:51:24 +0000917 }
drh8141f612004-01-25 22:44:58 +0000918 if( lookupName(pParse, pDb, pTable, pColumn, pSrcList, 0, pExpr) ){
drhdaffd0e2001-04-11 14:28:42 +0000919 return 1;
920 }
drhcce7d172000-05-31 15:34:51 +0000921 break;
922 }
923
drhfef52082000-06-06 01:50:43 +0000924 case TK_IN: {
danielk1977e014a832004-05-17 10:48:57 +0000925 char affinity;
danielk19774adee202004-05-08 08:23:19 +0000926 Vdbe *v = sqlite3GetVdbe(pParse);
drhd3d39e92004-05-20 22:16:29 +0000927 KeyInfo keyInfo;
danielk19770202b292004-06-09 09:55:16 +0000928 int addr; /* Address of OP_OpenTemp instruction */
drhd3d39e92004-05-20 22:16:29 +0000929
drhfef52082000-06-06 01:50:43 +0000930 if( v==0 ) return 1;
danielk19774adee202004-05-08 08:23:19 +0000931 if( sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pLeft) ){
drhcfab11b2000-06-06 03:31:22 +0000932 return 1;
933 }
danielk1977bf3b7212004-05-18 10:06:24 +0000934 affinity = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +0000935
936 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
937 ** expression it is handled the same way. A temporary table is
938 ** filled with single-field index keys representing the results
939 ** from the SELECT or the <exprlist>.
940 **
941 ** If the 'x' expression is a column value, or the SELECT...
942 ** statement returns a column value, then the affinity of that
943 ** column is used to build the index keys. If both 'x' and the
944 ** SELECT... statement are columns, then numeric affinity is used
945 ** if either column has NUMERIC or INTEGER affinity. If neither
946 ** 'x' nor the SELECT... statement are columns, then numeric affinity
947 ** is used.
948 */
949 pExpr->iTable = pParse->nTab++;
danielk19770202b292004-06-09 09:55:16 +0000950 addr = sqlite3VdbeAddOp(v, OP_OpenTemp, pExpr->iTable, 0);
drhd3d39e92004-05-20 22:16:29 +0000951 memset(&keyInfo, 0, sizeof(keyInfo));
952 keyInfo.nField = 1;
drhf3218fe2004-05-28 08:21:02 +0000953 sqlite3VdbeAddOp(v, OP_SetNumColumns, pExpr->iTable, 1);
danielk1977e014a832004-05-17 10:48:57 +0000954
drhfef52082000-06-06 01:50:43 +0000955 if( pExpr->pSelect ){
956 /* Case 1: expr IN (SELECT ...)
957 **
danielk1977e014a832004-05-17 10:48:57 +0000958 ** Generate code to write the results of the select into the temporary
959 ** table allocated and opened above.
drhfef52082000-06-06 01:50:43 +0000960 */
danielk1977e014a832004-05-17 10:48:57 +0000961 int iParm = pExpr->iTable + (((int)affinity)<<16);
drhbe5c89a2004-07-26 00:31:09 +0000962 ExprList *pEList;
danielk1977e014a832004-05-17 10:48:57 +0000963 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
danielk1977bf3b7212004-05-18 10:06:24 +0000964 sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +0000965 pEList = pExpr->pSelect->pEList;
966 if( pEList && pEList->nExpr>0 ){
danielk19777cedc8d2004-06-10 10:50:08 +0000967 keyInfo.aColl[0] = binaryCompareCollSeq(pParse, pExpr->pLeft,
drhbe5c89a2004-07-26 00:31:09 +0000968 pEList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +0000969 }
drhfef52082000-06-06 01:50:43 +0000970 }else if( pExpr->pList ){
971 /* Case 2: expr IN (exprlist)
972 **
danielk1977e014a832004-05-17 10:48:57 +0000973 ** For each expression, build an index key from the evaluation and
974 ** store it in the temporary table. If <expr> is a column, then use
975 ** that columns affinity when building index keys. If <expr> is not
976 ** a column, use numeric affinity.
drhfef52082000-06-06 01:50:43 +0000977 */
danielk1977e014a832004-05-17 10:48:57 +0000978 int i;
979 char const *affStr;
980 if( !affinity ){
981 affinity = SQLITE_AFF_NUMERIC;
982 }
983 affStr = sqlite3AffinityString(affinity);
danielk19770202b292004-06-09 09:55:16 +0000984 keyInfo.aColl[0] = pExpr->pLeft->pColl;
danielk1977e014a832004-05-17 10:48:57 +0000985
986 /* Loop through each expression in <exprlist>. */
drhfef52082000-06-06 01:50:43 +0000987 for(i=0; i<pExpr->pList->nExpr; i++){
988 Expr *pE2 = pExpr->pList->a[i].pExpr;
danielk1977e014a832004-05-17 10:48:57 +0000989
990 /* Check that the expression is constant and valid. */
danielk19774adee202004-05-08 08:23:19 +0000991 if( !sqlite3ExprIsConstant(pE2) ){
992 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +0000993 "right-hand side of IN operator must be constant");
drhfef52082000-06-06 01:50:43 +0000994 return 1;
995 }
danielk19774adee202004-05-08 08:23:19 +0000996 if( sqlite3ExprCheck(pParse, pE2, 0, 0) ){
drh4794b982000-06-06 13:54:14 +0000997 return 1;
998 }
danielk1977e014a832004-05-17 10:48:57 +0000999
1000 /* Evaluate the expression and insert it into the temp table */
1001 sqlite3ExprCode(pParse, pE2);
danielk1977ededfd52004-06-17 07:53:01 +00001002 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, affStr, P3_STATIC);
danielk19770f69c1e2004-05-29 11:24:50 +00001003 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +00001004 sqlite3VdbeAddOp(v, OP_PutStrKey, pExpr->iTable, 0);
drhfef52082000-06-06 01:50:43 +00001005 }
1006 }
danielk19770202b292004-06-09 09:55:16 +00001007 sqlite3VdbeChangeP3(v, addr, (void *)&keyInfo, P3_KEYINFO);
1008
drhcfab11b2000-06-06 03:31:22 +00001009 break;
drhfef52082000-06-06 01:50:43 +00001010 }
1011
drh19a775c2000-06-05 18:54:46 +00001012 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +00001013 /* This has to be a scalar SELECT. Generate code to put the
1014 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +00001015 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +00001016 */
drh967e8b72000-06-21 13:59:10 +00001017 pExpr->iColumn = pParse->nMem++;
danielk1977bf3b7212004-05-18 10:06:24 +00001018 if(sqlite3Select(pParse, pExpr->pSelect, SRT_Mem,pExpr->iColumn,0,0,0,0)){
drh19a775c2000-06-05 18:54:46 +00001019 return 1;
1020 }
1021 break;
1022 }
1023
drhcce7d172000-05-31 15:34:51 +00001024 /* For all else, just recursively walk the tree */
1025 default: {
drh4794b982000-06-06 13:54:14 +00001026 if( pExpr->pLeft
danielk19774adee202004-05-08 08:23:19 +00001027 && sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pLeft) ){
drhcce7d172000-05-31 15:34:51 +00001028 return 1;
1029 }
1030 if( pExpr->pRight
danielk19774adee202004-05-08 08:23:19 +00001031 && sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pRight) ){
drhcce7d172000-05-31 15:34:51 +00001032 return 1;
1033 }
1034 if( pExpr->pList ){
1035 int i;
1036 ExprList *pList = pExpr->pList;
1037 for(i=0; i<pList->nExpr; i++){
drh832508b2002-03-02 17:04:07 +00001038 Expr *pArg = pList->a[i].pExpr;
danielk19774adee202004-05-08 08:23:19 +00001039 if( sqlite3ExprResolveIds(pParse, pSrcList, pEList, pArg) ){
drhcce7d172000-05-31 15:34:51 +00001040 return 1;
1041 }
1042 }
1043 }
1044 }
1045 }
1046 return 0;
1047}
1048
drhcce7d172000-05-31 15:34:51 +00001049/*
drh4b59ab52002-08-24 18:24:51 +00001050** pExpr is a node that defines a function of some kind. It might
1051** be a syntactic function like "count(x)" or it might be a function
1052** that implements an operator, like "a LIKE b".
1053**
1054** This routine makes *pzName point to the name of the function and
1055** *pnName hold the number of characters in the function name.
1056*/
1057static void getFunctionName(Expr *pExpr, const char **pzName, int *pnName){
1058 switch( pExpr->op ){
1059 case TK_FUNCTION: {
1060 *pzName = pExpr->token.z;
drh6977fea2002-10-22 23:38:04 +00001061 *pnName = pExpr->token.n;
drh4b59ab52002-08-24 18:24:51 +00001062 break;
1063 }
1064 case TK_LIKE: {
1065 *pzName = "like";
1066 *pnName = 4;
1067 break;
1068 }
1069 case TK_GLOB: {
1070 *pzName = "glob";
1071 *pnName = 4;
1072 break;
1073 }
1074 default: {
1075 *pzName = "can't happen";
1076 *pnName = 12;
1077 break;
1078 }
1079 }
1080}
1081
1082/*
drhcce7d172000-05-31 15:34:51 +00001083** Error check the functions in an expression. Make sure all
1084** function names are recognized and all functions have the correct
1085** number of arguments. Leave an error message in pParse->zErrMsg
1086** if anything is amiss. Return the number of errors.
1087**
1088** if pIsAgg is not null and this expression is an aggregate function
1089** (like count(*) or max(value)) then write a 1 into *pIsAgg.
1090*/
danielk19774adee202004-05-08 08:23:19 +00001091int sqlite3ExprCheck(Parse *pParse, Expr *pExpr, int allowAgg, int *pIsAgg){
drhcce7d172000-05-31 15:34:51 +00001092 int nErr = 0;
1093 if( pExpr==0 ) return 0;
drhcce7d172000-05-31 15:34:51 +00001094 switch( pExpr->op ){
drh4b59ab52002-08-24 18:24:51 +00001095 case TK_GLOB:
1096 case TK_LIKE:
drhcce7d172000-05-31 15:34:51 +00001097 case TK_FUNCTION: {
drhc9b84a12002-06-20 11:36:48 +00001098 int n = pExpr->pList ? pExpr->pList->nExpr : 0; /* Number of arguments */
1099 int no_such_func = 0; /* True if no such function exists */
drhc9b84a12002-06-20 11:36:48 +00001100 int wrong_num_args = 0; /* True if wrong number of arguments */
1101 int is_agg = 0; /* True if is an aggregate function */
drhcce7d172000-05-31 15:34:51 +00001102 int i;
drh4b59ab52002-08-24 18:24:51 +00001103 int nId; /* Number of characters in function name */
1104 const char *zId; /* The function name. */
drh0bce8352002-02-28 00:41:10 +00001105 FuncDef *pDef;
danielk1977d8123362004-06-12 09:25:12 +00001106 int enc = pParse->db->enc;
drh0bce8352002-02-28 00:41:10 +00001107
drh4b59ab52002-08-24 18:24:51 +00001108 getFunctionName(pExpr, &zId, &nId);
danielk1977d8123362004-06-12 09:25:12 +00001109 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001110 if( pDef==0 ){
danielk1977d8123362004-06-12 09:25:12 +00001111 pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001112 if( pDef==0 ){
drh268380c2004-02-25 13:47:31 +00001113 no_such_func = 1;
drh0bce8352002-02-28 00:41:10 +00001114 }else{
1115 wrong_num_args = 1;
drhcce7d172000-05-31 15:34:51 +00001116 }
drh0bce8352002-02-28 00:41:10 +00001117 }else{
1118 is_agg = pDef->xFunc==0;
drhcce7d172000-05-31 15:34:51 +00001119 }
drh8e0a2f92002-02-23 23:45:45 +00001120 if( is_agg && !allowAgg ){
danielk19774adee202004-05-08 08:23:19 +00001121 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId, zId);
drh8e0a2f92002-02-23 23:45:45 +00001122 nErr++;
1123 is_agg = 0;
1124 }else if( no_such_func ){
danielk19774adee202004-05-08 08:23:19 +00001125 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
drhcce7d172000-05-31 15:34:51 +00001126 nErr++;
drh8e0a2f92002-02-23 23:45:45 +00001127 }else if( wrong_num_args ){
danielk19774adee202004-05-08 08:23:19 +00001128 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
drhf7a9e1a2004-02-22 18:40:56 +00001129 nId, zId);
drh8e0a2f92002-02-23 23:45:45 +00001130 nErr++;
drhcce7d172000-05-31 15:34:51 +00001131 }
drhf7a9e1a2004-02-22 18:40:56 +00001132 if( is_agg ){
1133 pExpr->op = TK_AGG_FUNCTION;
1134 if( pIsAgg ) *pIsAgg = 1;
1135 }
drhcce7d172000-05-31 15:34:51 +00001136 for(i=0; nErr==0 && i<n; i++){
danielk19774adee202004-05-08 08:23:19 +00001137 nErr = sqlite3ExprCheck(pParse, pExpr->pList->a[i].pExpr,
drh4cfa7932000-06-08 15:10:46 +00001138 allowAgg && !is_agg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +00001139 }
danielk19770202b292004-06-09 09:55:16 +00001140 /* FIX ME: Compute pExpr->affinity based on the expected return
1141 ** type of the function
1142 */
drhcce7d172000-05-31 15:34:51 +00001143 }
1144 default: {
1145 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001146 nErr = sqlite3ExprCheck(pParse, pExpr->pLeft, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +00001147 }
1148 if( nErr==0 && pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001149 nErr = sqlite3ExprCheck(pParse, pExpr->pRight, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +00001150 }
drhfef52082000-06-06 01:50:43 +00001151 if( nErr==0 && pExpr->pList ){
1152 int n = pExpr->pList->nExpr;
1153 int i;
1154 for(i=0; nErr==0 && i<n; i++){
drh22827922000-06-06 17:27:05 +00001155 Expr *pE2 = pExpr->pList->a[i].pExpr;
danielk19774adee202004-05-08 08:23:19 +00001156 nErr = sqlite3ExprCheck(pParse, pE2, allowAgg, pIsAgg);
drhfef52082000-06-06 01:50:43 +00001157 }
1158 }
drhcce7d172000-05-31 15:34:51 +00001159 break;
1160 }
1161 }
1162 return nErr;
1163}
1164
1165/*
drh290c1942004-08-21 17:54:45 +00001166** Call sqlite3ExprResolveIds() followed by sqlite3ExprCheck().
1167**
1168** This routine is provided as a convenience since it is very common
1169** to call ResolveIds() and Check() back to back.
1170*/
1171int sqlite3ExprResolveAndCheck(
1172 Parse *pParse, /* The parser context */
1173 SrcList *pSrcList, /* List of tables used to resolve column names */
1174 ExprList *pEList, /* List of expressions used to resolve "AS" */
1175 Expr *pExpr, /* The expression to be analyzed. */
1176 int allowAgg, /* True to allow aggregate expressions */
1177 int *pIsAgg /* Set to TRUE if aggregates are found */
1178){
1179 if( pExpr==0 ) return 0;
1180 if( sqlite3ExprResolveIds(pParse,pSrcList,pEList,pExpr) ){
1181 return 1;
1182 }
1183 return sqlite3ExprCheck(pParse, pExpr, allowAgg, pIsAgg);
1184}
1185
1186/*
drhfec19aa2004-05-19 20:41:03 +00001187** Generate an instruction that will put the integer describe by
1188** text z[0..n-1] on the stack.
1189*/
1190static void codeInteger(Vdbe *v, const char *z, int n){
1191 int i;
drh6fec0762004-05-30 01:38:43 +00001192 if( sqlite3GetInt32(z, &i) ){
1193 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
1194 }else if( sqlite3FitsIn64Bits(z) ){
1195 sqlite3VdbeOp3(v, OP_Integer, 0, 0, z, n);
drhfec19aa2004-05-19 20:41:03 +00001196 }else{
1197 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n);
1198 }
1199}
1200
1201/*
drhcce7d172000-05-31 15:34:51 +00001202** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +00001203** expression and leave the result on the top of stack.
drhcce7d172000-05-31 15:34:51 +00001204*/
danielk19774adee202004-05-08 08:23:19 +00001205void sqlite3ExprCode(Parse *pParse, Expr *pExpr){
drhcce7d172000-05-31 15:34:51 +00001206 Vdbe *v = pParse->pVdbe;
1207 int op;
drhdaffd0e2001-04-11 14:28:42 +00001208 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001209 switch( pExpr->op ){
1210 case TK_PLUS: op = OP_Add; break;
1211 case TK_MINUS: op = OP_Subtract; break;
1212 case TK_STAR: op = OP_Multiply; break;
1213 case TK_SLASH: op = OP_Divide; break;
1214 case TK_AND: op = OP_And; break;
1215 case TK_OR: op = OP_Or; break;
1216 case TK_LT: op = OP_Lt; break;
1217 case TK_LE: op = OP_Le; break;
1218 case TK_GT: op = OP_Gt; break;
1219 case TK_GE: op = OP_Ge; break;
1220 case TK_NE: op = OP_Ne; break;
1221 case TK_EQ: op = OP_Eq; break;
drhcce7d172000-05-31 15:34:51 +00001222 case TK_ISNULL: op = OP_IsNull; break;
1223 case TK_NOTNULL: op = OP_NotNull; break;
1224 case TK_NOT: op = OP_Not; break;
1225 case TK_UMINUS: op = OP_Negative; break;
drhbf4133c2001-10-13 02:59:08 +00001226 case TK_BITAND: op = OP_BitAnd; break;
1227 case TK_BITOR: op = OP_BitOr; break;
1228 case TK_BITNOT: op = OP_BitNot; break;
1229 case TK_LSHIFT: op = OP_ShiftLeft; break;
1230 case TK_RSHIFT: op = OP_ShiftRight; break;
1231 case TK_REM: op = OP_Remainder; break;
drhfec19aa2004-05-19 20:41:03 +00001232 case TK_FLOAT: op = OP_Real; break;
drh855eb1c2004-08-31 13:45:11 +00001233 case TK_STRING: op = OP_String8; break;
danielk1977c572ef72004-05-27 09:28:41 +00001234 case TK_BLOB: op = OP_HexBlob; break;
drh855eb1c2004-08-31 13:45:11 +00001235 case TK_CONCAT: op = OP_Concat; break;
danielk1977cfe9a692004-06-16 12:00:29 +00001236 default: op = 0; break;
drhcce7d172000-05-31 15:34:51 +00001237 }
1238 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00001239 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +00001240 if( pParse->useAgg ){
danielk19774adee202004-05-08 08:23:19 +00001241 sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drhc4a3c772001-04-04 11:48:57 +00001242 }else if( pExpr->iColumn>=0 ){
danielk19774adee202004-05-08 08:23:19 +00001243 sqlite3VdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn);
drhc4a3c772001-04-04 11:48:57 +00001244 }else{
danielk19774adee202004-05-08 08:23:19 +00001245 sqlite3VdbeAddOp(v, OP_Recno, pExpr->iTable, 0);
drh22827922000-06-06 17:27:05 +00001246 }
drhcce7d172000-05-31 15:34:51 +00001247 break;
1248 }
1249 case TK_INTEGER: {
drhfec19aa2004-05-19 20:41:03 +00001250 codeInteger(v, pExpr->token.z, pExpr->token.n);
1251 break;
1252 }
1253 case TK_FLOAT:
1254 case TK_STRING: {
1255 sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z, pExpr->token.n);
danielk19774adee202004-05-08 08:23:19 +00001256 sqlite3VdbeDequoteP3(v, -1);
drhcce7d172000-05-31 15:34:51 +00001257 break;
1258 }
danielk1977c572ef72004-05-27 09:28:41 +00001259 case TK_BLOB: {
1260 sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z+1, pExpr->token.n-1);
1261 sqlite3VdbeDequoteP3(v, -1);
1262 break;
1263 }
drhcce7d172000-05-31 15:34:51 +00001264 case TK_NULL: {
danielk19770f69c1e2004-05-29 11:24:50 +00001265 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001266 break;
1267 }
drh50457892003-09-06 01:10:47 +00001268 case TK_VARIABLE: {
danielk19774adee202004-05-08 08:23:19 +00001269 sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0);
drh895d7472004-08-20 16:02:39 +00001270 if( pExpr->token.n>1 ){
1271 sqlite3VdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
1272 }
drh50457892003-09-06 01:10:47 +00001273 break;
1274 }
drhc9b84a12002-06-20 11:36:48 +00001275 case TK_LT:
1276 case TK_LE:
1277 case TK_GT:
1278 case TK_GE:
1279 case TK_NE:
1280 case TK_EQ: {
danielk1977a37cdde2004-05-16 11:15:36 +00001281 sqlite3ExprCode(pParse, pExpr->pLeft);
1282 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001283 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 0, 0);
danielk1977a37cdde2004-05-16 11:15:36 +00001284 break;
drhc9b84a12002-06-20 11:36:48 +00001285 }
drhcce7d172000-05-31 15:34:51 +00001286 case TK_AND:
1287 case TK_OR:
1288 case TK_PLUS:
1289 case TK_STAR:
1290 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00001291 case TK_REM:
1292 case TK_BITAND:
1293 case TK_BITOR:
drh17c40292004-07-21 02:53:29 +00001294 case TK_SLASH:
drhbf4133c2001-10-13 02:59:08 +00001295 case TK_LSHIFT:
drh855eb1c2004-08-31 13:45:11 +00001296 case TK_RSHIFT:
drh00400772000-06-16 20:51:26 +00001297 case TK_CONCAT: {
danielk19774adee202004-05-08 08:23:19 +00001298 sqlite3ExprCode(pParse, pExpr->pLeft);
1299 sqlite3ExprCode(pParse, pExpr->pRight);
drh855eb1c2004-08-31 13:45:11 +00001300 sqlite3VdbeAddOp(v, op, 0, 0);
drh00400772000-06-16 20:51:26 +00001301 break;
1302 }
drhcce7d172000-05-31 15:34:51 +00001303 case TK_UMINUS: {
drhfec19aa2004-05-19 20:41:03 +00001304 Expr *pLeft = pExpr->pLeft;
1305 assert( pLeft );
1306 if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
1307 Token *p = &pLeft->token;
drh6e142f52000-06-08 13:36:40 +00001308 char *z = sqliteMalloc( p->n + 2 );
1309 sprintf(z, "-%.*s", p->n, p->z);
drhfec19aa2004-05-19 20:41:03 +00001310 if( pLeft->op==TK_FLOAT ){
1311 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001312 }else{
drhfec19aa2004-05-19 20:41:03 +00001313 codeInteger(v, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001314 }
drh6e142f52000-06-08 13:36:40 +00001315 sqliteFree(z);
1316 break;
1317 }
drh1ccde152000-06-17 13:12:39 +00001318 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +00001319 }
drhbf4133c2001-10-13 02:59:08 +00001320 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00001321 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001322 sqlite3ExprCode(pParse, pExpr->pLeft);
1323 sqlite3VdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001324 break;
1325 }
1326 case TK_ISNULL:
1327 case TK_NOTNULL: {
1328 int dest;
danielk19774adee202004-05-08 08:23:19 +00001329 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1330 sqlite3ExprCode(pParse, pExpr->pLeft);
1331 dest = sqlite3VdbeCurrentAddr(v) + 2;
1332 sqlite3VdbeAddOp(v, op, 1, dest);
1333 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);
drhcce7d172000-05-31 15:34:51 +00001334 }
danielk1977a37cdde2004-05-16 11:15:36 +00001335 break;
drh22827922000-06-06 17:27:05 +00001336 case TK_AGG_FUNCTION: {
danielk19774adee202004-05-08 08:23:19 +00001337 sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drh22827922000-06-06 17:27:05 +00001338 break;
1339 }
drh4b59ab52002-08-24 18:24:51 +00001340 case TK_GLOB:
1341 case TK_LIKE:
drhcce7d172000-05-31 15:34:51 +00001342 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00001343 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00001344 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00001345 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00001346 int nId;
1347 const char *zId;
danielk1977682f68b2004-06-05 10:22:17 +00001348 int p2 = 0;
1349 int i;
danielk1977d8123362004-06-12 09:25:12 +00001350 u8 enc = pParse->db->enc;
danielk1977dc1bdc42004-06-11 10:51:27 +00001351 CollSeq *pColl = 0;
drh4b59ab52002-08-24 18:24:51 +00001352 getFunctionName(pExpr, &zId, &nId);
danielk1977d8123362004-06-12 09:25:12 +00001353 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001354 assert( pDef!=0 );
drhf9b596e2004-05-26 16:54:42 +00001355 nExpr = sqlite3ExprCodeExprList(pParse, pList);
danielk1977682f68b2004-06-05 10:22:17 +00001356 for(i=0; i<nExpr && i<32; i++){
danielk1977d02eb1f2004-06-06 09:44:03 +00001357 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
1358 p2 |= (1<<i);
1359 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001360 if( pDef->needCollSeq && !pColl ){
1361 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
1362 }
1363 }
1364 if( pDef->needCollSeq ){
1365 if( !pColl ) pColl = pParse->db->pDfltColl;
danielk1977d8123362004-06-12 09:25:12 +00001366 sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ);
danielk1977682f68b2004-06-05 10:22:17 +00001367 }
1368 sqlite3VdbeOp3(v, OP_Function, nExpr, p2, (char*)pDef, P3_FUNCDEF);
drhcce7d172000-05-31 15:34:51 +00001369 break;
1370 }
drh19a775c2000-06-05 18:54:46 +00001371 case TK_SELECT: {
danielk19774adee202004-05-08 08:23:19 +00001372 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drh19a775c2000-06-05 18:54:46 +00001373 break;
1374 }
drhfef52082000-06-06 01:50:43 +00001375 case TK_IN: {
1376 int addr;
danielk1977e014a832004-05-17 10:48:57 +00001377 char const *affStr;
1378
1379 /* Figure out the affinity to use to create a key from the results
1380 ** of the expression. affinityStr stores a static string suitable for
danielk1977ededfd52004-06-17 07:53:01 +00001381 ** P3 of OP_MakeRecord.
danielk1977e014a832004-05-17 10:48:57 +00001382 */
1383 affStr = sqlite3AffinityString(comparisonAffinity(pExpr));
1384
danielk19774adee202004-05-08 08:23:19 +00001385 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
danielk1977e014a832004-05-17 10:48:57 +00001386
1387 /* Code the <expr> from "<expr> IN (...)". The temporary table
1388 ** pExpr->iTable contains the values that make up the (...) set.
1389 */
danielk19774adee202004-05-08 08:23:19 +00001390 sqlite3ExprCode(pParse, pExpr->pLeft);
1391 addr = sqlite3VdbeCurrentAddr(v);
danielk1977e014a832004-05-17 10:48:57 +00001392 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); /* addr + 0 */
danielk19774adee202004-05-08 08:23:19 +00001393 sqlite3VdbeAddOp(v, OP_Pop, 2, 0);
danielk19770f69c1e2004-05-29 11:24:50 +00001394 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +00001395 sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7);
danielk1977ededfd52004-06-17 07:53:01 +00001396 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, affStr, P3_STATIC); /* addr + 4 */
danielk1977e014a832004-05-17 10:48:57 +00001397 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7);
1398 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); /* addr + 6 */
1399
drhfef52082000-06-06 01:50:43 +00001400 break;
1401 }
1402 case TK_BETWEEN: {
drhbe5c89a2004-07-26 00:31:09 +00001403 Expr *pLeft = pExpr->pLeft;
1404 struct ExprList_item *pLItem = pExpr->pList->a;
1405 Expr *pRight = pLItem->pExpr;
1406 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001407 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001408 sqlite3ExprCode(pParse, pRight);
1409 codeCompare(pParse, pLeft, pRight, OP_Ge, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001410 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
drhbe5c89a2004-07-26 00:31:09 +00001411 pLItem++;
1412 pRight = pLItem->pExpr;
1413 sqlite3ExprCode(pParse, pRight);
1414 codeCompare(pParse, pLeft, pRight, OP_Le, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001415 sqlite3VdbeAddOp(v, OP_And, 0, 0);
drhfef52082000-06-06 01:50:43 +00001416 break;
1417 }
drh51e9a442004-01-16 16:42:53 +00001418 case TK_UPLUS:
drha2e00042002-01-22 03:13:42 +00001419 case TK_AS: {
danielk19774adee202004-05-08 08:23:19 +00001420 sqlite3ExprCode(pParse, pExpr->pLeft);
drha2e00042002-01-22 03:13:42 +00001421 break;
1422 }
drh17a7f8d2002-03-24 13:13:27 +00001423 case TK_CASE: {
1424 int expr_end_label;
drhf5905aa2002-05-26 20:54:33 +00001425 int jumpInst;
1426 int addr;
1427 int nExpr;
drh17a7f8d2002-03-24 13:13:27 +00001428 int i;
drhbe5c89a2004-07-26 00:31:09 +00001429 ExprList *pEList;
1430 struct ExprList_item *aListelem;
drh17a7f8d2002-03-24 13:13:27 +00001431
1432 assert(pExpr->pList);
1433 assert((pExpr->pList->nExpr % 2) == 0);
1434 assert(pExpr->pList->nExpr > 0);
drhbe5c89a2004-07-26 00:31:09 +00001435 pEList = pExpr->pList;
1436 aListelem = pEList->a;
1437 nExpr = pEList->nExpr;
danielk19774adee202004-05-08 08:23:19 +00001438 expr_end_label = sqlite3VdbeMakeLabel(v);
drh17a7f8d2002-03-24 13:13:27 +00001439 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001440 sqlite3ExprCode(pParse, pExpr->pLeft);
drh17a7f8d2002-03-24 13:13:27 +00001441 }
drhf5905aa2002-05-26 20:54:33 +00001442 for(i=0; i<nExpr; i=i+2){
drhbe5c89a2004-07-26 00:31:09 +00001443 sqlite3ExprCode(pParse, aListelem[i].pExpr);
drh17a7f8d2002-03-24 13:13:27 +00001444 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001445 sqlite3VdbeAddOp(v, OP_Dup, 1, 1);
drhbe5c89a2004-07-26 00:31:09 +00001446 jumpInst = codeCompare(pParse, pExpr->pLeft, aListelem[i].pExpr,
1447 OP_Ne, 0, 1);
danielk19774adee202004-05-08 08:23:19 +00001448 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001449 }else{
danielk19774adee202004-05-08 08:23:19 +00001450 jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001451 }
drhbe5c89a2004-07-26 00:31:09 +00001452 sqlite3ExprCode(pParse, aListelem[i+1].pExpr);
danielk19774adee202004-05-08 08:23:19 +00001453 sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label);
1454 addr = sqlite3VdbeCurrentAddr(v);
1455 sqlite3VdbeChangeP2(v, jumpInst, addr);
drh17a7f8d2002-03-24 13:13:27 +00001456 }
drhf570f012002-05-31 15:51:25 +00001457 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001458 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhf570f012002-05-31 15:51:25 +00001459 }
drh17a7f8d2002-03-24 13:13:27 +00001460 if( pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001461 sqlite3ExprCode(pParse, pExpr->pRight);
drh17a7f8d2002-03-24 13:13:27 +00001462 }else{
danielk19770f69c1e2004-05-29 11:24:50 +00001463 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
drh17a7f8d2002-03-24 13:13:27 +00001464 }
danielk19774adee202004-05-08 08:23:19 +00001465 sqlite3VdbeResolveLabel(v, expr_end_label);
danielk19776f349032002-06-11 02:25:40 +00001466 break;
1467 }
1468 case TK_RAISE: {
1469 if( !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +00001470 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00001471 "RAISE() may only be used within a trigger-program");
danielk19776f349032002-06-11 02:25:40 +00001472 pParse->nErr++;
1473 return;
1474 }
1475 if( pExpr->iColumn == OE_Rollback ||
1476 pExpr->iColumn == OE_Abort ||
1477 pExpr->iColumn == OE_Fail ){
danielk19774adee202004-05-08 08:23:19 +00001478 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn,
drh701a0ae2004-02-22 20:05:00 +00001479 pExpr->token.z, pExpr->token.n);
danielk19774adee202004-05-08 08:23:19 +00001480 sqlite3VdbeDequoteP3(v, -1);
danielk19776f349032002-06-11 02:25:40 +00001481 } else {
1482 assert( pExpr->iColumn == OE_Ignore );
danielk19774adee202004-05-08 08:23:19 +00001483 sqlite3VdbeOp3(v, OP_Goto, 0, pParse->trigStack->ignoreJump,
drh701a0ae2004-02-22 20:05:00 +00001484 "(IGNORE jump)", 0);
danielk19776f349032002-06-11 02:25:40 +00001485 }
drh17a7f8d2002-03-24 13:13:27 +00001486 }
1487 break;
drhcce7d172000-05-31 15:34:51 +00001488 }
drhcce7d172000-05-31 15:34:51 +00001489}
1490
1491/*
drh268380c2004-02-25 13:47:31 +00001492** Generate code that pushes the value of every element of the given
drhf9b596e2004-05-26 16:54:42 +00001493** expression list onto the stack.
drh268380c2004-02-25 13:47:31 +00001494**
1495** Return the number of elements pushed onto the stack.
1496*/
danielk19774adee202004-05-08 08:23:19 +00001497int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00001498 Parse *pParse, /* Parsing context */
drhf9b596e2004-05-26 16:54:42 +00001499 ExprList *pList /* The expression list to be coded */
drh268380c2004-02-25 13:47:31 +00001500){
1501 struct ExprList_item *pItem;
1502 int i, n;
1503 Vdbe *v;
1504 if( pList==0 ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001505 v = sqlite3GetVdbe(pParse);
drh268380c2004-02-25 13:47:31 +00001506 n = pList->nExpr;
1507 for(pItem=pList->a, i=0; i<n; i++, pItem++){
danielk19774adee202004-05-08 08:23:19 +00001508 sqlite3ExprCode(pParse, pItem->pExpr);
drh268380c2004-02-25 13:47:31 +00001509 }
drhf9b596e2004-05-26 16:54:42 +00001510 return n;
drh268380c2004-02-25 13:47:31 +00001511}
1512
1513/*
drhcce7d172000-05-31 15:34:51 +00001514** Generate code for a boolean expression such that a jump is made
1515** to the label "dest" if the expression is true but execution
1516** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00001517**
1518** If the expression evaluates to NULL (neither true nor false), then
1519** take the jump if the jumpIfNull flag is true.
drhcce7d172000-05-31 15:34:51 +00001520*/
danielk19774adee202004-05-08 08:23:19 +00001521void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001522 Vdbe *v = pParse->pVdbe;
1523 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001524 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001525 switch( pExpr->op ){
1526 case TK_LT: op = OP_Lt; break;
1527 case TK_LE: op = OP_Le; break;
1528 case TK_GT: op = OP_Gt; break;
1529 case TK_GE: op = OP_Ge; break;
1530 case TK_NE: op = OP_Ne; break;
1531 case TK_EQ: op = OP_Eq; break;
drhcce7d172000-05-31 15:34:51 +00001532 case TK_ISNULL: op = OP_IsNull; break;
1533 case TK_NOTNULL: op = OP_NotNull; break;
1534 default: break;
1535 }
1536 switch( pExpr->op ){
1537 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001538 int d2 = sqlite3VdbeMakeLabel(v);
1539 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
1540 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
1541 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001542 break;
1543 }
1544 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001545 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
1546 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001547 break;
1548 }
1549 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001550 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001551 break;
1552 }
1553 case TK_LT:
1554 case TK_LE:
1555 case TK_GT:
1556 case TK_GE:
1557 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00001558 case TK_EQ: {
danielk19774adee202004-05-08 08:23:19 +00001559 sqlite3ExprCode(pParse, pExpr->pLeft);
1560 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001561 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001562 break;
1563 }
1564 case TK_ISNULL:
1565 case TK_NOTNULL: {
danielk19774adee202004-05-08 08:23:19 +00001566 sqlite3ExprCode(pParse, pExpr->pLeft);
1567 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001568 break;
1569 }
drhfef52082000-06-06 01:50:43 +00001570 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00001571 /* The expression "x BETWEEN y AND z" is implemented as:
1572 **
1573 ** 1 IF (x < y) GOTO 3
1574 ** 2 IF (x <= z) GOTO <dest>
1575 ** 3 ...
1576 */
drhf5905aa2002-05-26 20:54:33 +00001577 int addr;
drhbe5c89a2004-07-26 00:31:09 +00001578 Expr *pLeft = pExpr->pLeft;
1579 Expr *pRight = pExpr->pList->a[0].pExpr;
1580 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001581 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001582 sqlite3ExprCode(pParse, pRight);
1583 addr = codeCompare(pParse, pLeft, pRight, OP_Lt, 0, !jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00001584
drhbe5c89a2004-07-26 00:31:09 +00001585 pRight = pExpr->pList->a[1].pExpr;
1586 sqlite3ExprCode(pParse, pRight);
1587 codeCompare(pParse, pLeft, pRight, OP_Le, dest, jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00001588
danielk19774adee202004-05-08 08:23:19 +00001589 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
1590 sqlite3VdbeChangeP2(v, addr, sqlite3VdbeCurrentAddr(v));
1591 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00001592 break;
1593 }
drhcce7d172000-05-31 15:34:51 +00001594 default: {
danielk19774adee202004-05-08 08:23:19 +00001595 sqlite3ExprCode(pParse, pExpr);
1596 sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001597 break;
1598 }
1599 }
1600}
1601
1602/*
drh66b89c82000-11-28 20:47:17 +00001603** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00001604** to the label "dest" if the expression is false but execution
1605** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00001606**
1607** If the expression evaluates to NULL (neither true nor false) then
1608** jump if jumpIfNull is true or fall through if jumpIfNull is false.
drhcce7d172000-05-31 15:34:51 +00001609*/
danielk19774adee202004-05-08 08:23:19 +00001610void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001611 Vdbe *v = pParse->pVdbe;
1612 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001613 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001614 switch( pExpr->op ){
1615 case TK_LT: op = OP_Ge; break;
1616 case TK_LE: op = OP_Gt; break;
1617 case TK_GT: op = OP_Le; break;
1618 case TK_GE: op = OP_Lt; break;
1619 case TK_NE: op = OP_Eq; break;
1620 case TK_EQ: op = OP_Ne; break;
drhcce7d172000-05-31 15:34:51 +00001621 case TK_ISNULL: op = OP_NotNull; break;
1622 case TK_NOTNULL: op = OP_IsNull; break;
1623 default: break;
1624 }
1625 switch( pExpr->op ){
1626 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001627 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
1628 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001629 break;
1630 }
1631 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001632 int d2 = sqlite3VdbeMakeLabel(v);
1633 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
1634 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
1635 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001636 break;
1637 }
1638 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001639 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001640 break;
1641 }
1642 case TK_LT:
1643 case TK_LE:
1644 case TK_GT:
1645 case TK_GE:
1646 case TK_NE:
1647 case TK_EQ: {
danielk19774adee202004-05-08 08:23:19 +00001648 sqlite3ExprCode(pParse, pExpr->pLeft);
1649 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001650 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001651 break;
1652 }
drhcce7d172000-05-31 15:34:51 +00001653 case TK_ISNULL:
1654 case TK_NOTNULL: {
danielk19774adee202004-05-08 08:23:19 +00001655 sqlite3ExprCode(pParse, pExpr->pLeft);
1656 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001657 break;
1658 }
drhfef52082000-06-06 01:50:43 +00001659 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00001660 /* The expression is "x BETWEEN y AND z". It is implemented as:
1661 **
1662 ** 1 IF (x >= y) GOTO 3
1663 ** 2 GOTO <dest>
1664 ** 3 IF (x > z) GOTO <dest>
1665 */
drhfef52082000-06-06 01:50:43 +00001666 int addr;
drhbe5c89a2004-07-26 00:31:09 +00001667 Expr *pLeft = pExpr->pLeft;
1668 Expr *pRight = pExpr->pList->a[0].pExpr;
1669 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001670 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001671 sqlite3ExprCode(pParse, pRight);
danielk19774adee202004-05-08 08:23:19 +00001672 addr = sqlite3VdbeCurrentAddr(v);
drhbe5c89a2004-07-26 00:31:09 +00001673 codeCompare(pParse, pLeft, pRight, OP_Ge, addr+3, !jumpIfNull);
1674
danielk19774adee202004-05-08 08:23:19 +00001675 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
1676 sqlite3VdbeAddOp(v, OP_Goto, 0, dest);
drhbe5c89a2004-07-26 00:31:09 +00001677 pRight = pExpr->pList->a[1].pExpr;
1678 sqlite3ExprCode(pParse, pRight);
1679 codeCompare(pParse, pLeft, pRight, OP_Gt, dest, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00001680 break;
1681 }
drhcce7d172000-05-31 15:34:51 +00001682 default: {
danielk19774adee202004-05-08 08:23:19 +00001683 sqlite3ExprCode(pParse, pExpr);
1684 sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001685 break;
1686 }
1687 }
1688}
drh22827922000-06-06 17:27:05 +00001689
1690/*
1691** Do a deep comparison of two expression trees. Return TRUE (non-zero)
1692** if they are identical and return FALSE if they differ in any way.
1693*/
danielk19774adee202004-05-08 08:23:19 +00001694int sqlite3ExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00001695 int i;
1696 if( pA==0 ){
1697 return pB==0;
1698 }else if( pB==0 ){
1699 return 0;
1700 }
1701 if( pA->op!=pB->op ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001702 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
1703 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00001704 if( pA->pList ){
1705 if( pB->pList==0 ) return 0;
1706 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
1707 for(i=0; i<pA->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00001708 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00001709 return 0;
1710 }
1711 }
1712 }else if( pB->pList ){
1713 return 0;
1714 }
1715 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00001716 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drh22827922000-06-06 17:27:05 +00001717 if( pA->token.z ){
1718 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00001719 if( pB->token.n!=pA->token.n ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001720 if( sqlite3StrNICmp(pA->token.z, pB->token.z, pB->token.n)!=0 ) return 0;
drh22827922000-06-06 17:27:05 +00001721 }
1722 return 1;
1723}
1724
1725/*
1726** Add a new element to the pParse->aAgg[] array and return its index.
1727*/
1728static int appendAggInfo(Parse *pParse){
1729 if( (pParse->nAgg & 0x7)==0 ){
1730 int amt = pParse->nAgg + 8;
drh6d4abfb2001-10-22 02:58:08 +00001731 AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0]));
1732 if( aAgg==0 ){
drh22827922000-06-06 17:27:05 +00001733 return -1;
1734 }
drh6d4abfb2001-10-22 02:58:08 +00001735 pParse->aAgg = aAgg;
drh22827922000-06-06 17:27:05 +00001736 }
1737 memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0]));
1738 return pParse->nAgg++;
1739}
1740
1741/*
1742** Analyze the given expression looking for aggregate functions and
1743** for variables that need to be added to the pParse->aAgg[] array.
1744** Make additional entries to the pParse->aAgg[] array as necessary.
1745**
1746** This routine should only be called after the expression has been
danielk19774adee202004-05-08 08:23:19 +00001747** analyzed by sqlite3ExprResolveIds() and sqlite3ExprCheck().
drh22827922000-06-06 17:27:05 +00001748**
1749** If errors are seen, leave an error message in zErrMsg and return
1750** the number of errors.
1751*/
danielk19774adee202004-05-08 08:23:19 +00001752int sqlite3ExprAnalyzeAggregates(Parse *pParse, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00001753 int i;
1754 AggExpr *aAgg;
1755 int nErr = 0;
1756
1757 if( pExpr==0 ) return 0;
1758 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00001759 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +00001760 aAgg = pParse->aAgg;
1761 for(i=0; i<pParse->nAgg; i++){
1762 if( aAgg[i].isAgg ) continue;
1763 if( aAgg[i].pExpr->iTable==pExpr->iTable
drh967e8b72000-06-21 13:59:10 +00001764 && aAgg[i].pExpr->iColumn==pExpr->iColumn ){
drh22827922000-06-06 17:27:05 +00001765 break;
1766 }
1767 }
1768 if( i>=pParse->nAgg ){
1769 i = appendAggInfo(pParse);
1770 if( i<0 ) return 1;
1771 pParse->aAgg[i].isAgg = 0;
1772 pParse->aAgg[i].pExpr = pExpr;
1773 }
drhaaf88722000-06-08 11:25:00 +00001774 pExpr->iAgg = i;
drh22827922000-06-06 17:27:05 +00001775 break;
1776 }
1777 case TK_AGG_FUNCTION: {
drh22827922000-06-06 17:27:05 +00001778 aAgg = pParse->aAgg;
1779 for(i=0; i<pParse->nAgg; i++){
1780 if( !aAgg[i].isAgg ) continue;
danielk19774adee202004-05-08 08:23:19 +00001781 if( sqlite3ExprCompare(aAgg[i].pExpr, pExpr) ){
drh22827922000-06-06 17:27:05 +00001782 break;
1783 }
1784 }
1785 if( i>=pParse->nAgg ){
danielk1977d8123362004-06-12 09:25:12 +00001786 u8 enc = pParse->db->enc;
drh22827922000-06-06 17:27:05 +00001787 i = appendAggInfo(pParse);
1788 if( i<0 ) return 1;
1789 pParse->aAgg[i].isAgg = 1;
1790 pParse->aAgg[i].pExpr = pExpr;
danielk19774adee202004-05-08 08:23:19 +00001791 pParse->aAgg[i].pFunc = sqlite3FindFunction(pParse->db,
drh6977fea2002-10-22 23:38:04 +00001792 pExpr->token.z, pExpr->token.n,
danielk1977d8123362004-06-12 09:25:12 +00001793 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
drh22827922000-06-06 17:27:05 +00001794 }
1795 pExpr->iAgg = i;
1796 break;
1797 }
1798 default: {
1799 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001800 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pLeft);
drh22827922000-06-06 17:27:05 +00001801 }
1802 if( nErr==0 && pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001803 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pRight);
drh22827922000-06-06 17:27:05 +00001804 }
1805 if( nErr==0 && pExpr->pList ){
1806 int n = pExpr->pList->nExpr;
1807 int i;
1808 for(i=0; nErr==0 && i<n; i++){
danielk19774adee202004-05-08 08:23:19 +00001809 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pList->a[i].pExpr);
drh22827922000-06-06 17:27:05 +00001810 }
1811 }
1812 break;
1813 }
1814 }
1815 return nErr;
1816}
drh8e0a2f92002-02-23 23:45:45 +00001817
1818/*
danielk1977d02eb1f2004-06-06 09:44:03 +00001819** Locate a user function given a name, a number of arguments and a flag
1820** indicating whether the function prefers UTF-16 over UTF-8. Return a
1821** pointer to the FuncDef structure that defines that function, or return
1822** NULL if the function does not exist.
drh8e0a2f92002-02-23 23:45:45 +00001823**
drh0bce8352002-02-28 00:41:10 +00001824** If the createFlag argument is true, then a new (blank) FuncDef
drh8e0a2f92002-02-23 23:45:45 +00001825** structure is created and liked into the "db" structure if a
1826** no matching function previously existed. When createFlag is true
1827** and the nArg parameter is -1, then only a function that accepts
1828** any number of arguments will be returned.
1829**
1830** If createFlag is false and nArg is -1, then the first valid
1831** function found is returned. A function is valid if either xFunc
1832** or xStep is non-zero.
danielk1977d02eb1f2004-06-06 09:44:03 +00001833**
1834** If createFlag is false, then a function with the required name and
1835** number of arguments may be returned even if the eTextRep flag does not
1836** match that requested.
drh8e0a2f92002-02-23 23:45:45 +00001837*/
danielk19774adee202004-05-08 08:23:19 +00001838FuncDef *sqlite3FindFunction(
drh9bb575f2004-09-06 17:24:11 +00001839 sqlite3 *db, /* An open database */
drh8e0a2f92002-02-23 23:45:45 +00001840 const char *zName, /* Name of the function. Not null-terminated */
1841 int nName, /* Number of characters in the name */
1842 int nArg, /* Number of arguments. -1 means any number */
danielk1977d8123362004-06-12 09:25:12 +00001843 u8 enc, /* Preferred text encoding */
drh8e0a2f92002-02-23 23:45:45 +00001844 int createFlag /* Create new entry if true and does not otherwise exist */
1845){
danielk1977d02eb1f2004-06-06 09:44:03 +00001846 FuncDef *p; /* Iterator variable */
1847 FuncDef *pFirst; /* First function with this name */
1848 FuncDef *pBest = 0; /* Best match found so far */
danielk1977d8123362004-06-12 09:25:12 +00001849 int bestmatch = 0;
danielk1977d02eb1f2004-06-06 09:44:03 +00001850
danielk1977d8123362004-06-12 09:25:12 +00001851
1852 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
danielk1977d02eb1f2004-06-06 09:44:03 +00001853 if( nArg<-1 ) nArg = -1;
1854
1855 pFirst = (FuncDef*)sqlite3HashFind(&db->aFunc, zName, nName);
1856 for(p=pFirst; p; p=p->pNext){
danielk1977d8123362004-06-12 09:25:12 +00001857 /* During the search for the best function definition, bestmatch is set
1858 ** as follows to indicate the quality of the match with the definition
1859 ** pointed to by pBest:
1860 **
1861 ** 0: pBest is NULL. No match has been found.
1862 ** 1: A variable arguments function that prefers UTF-8 when a UTF-16
1863 ** encoding is requested, or vice versa.
1864 ** 2: A variable arguments function that uses UTF-16BE when UTF-16LE is
1865 ** requested, or vice versa.
1866 ** 3: A variable arguments function using the same text encoding.
1867 ** 4: A function with the exact number of arguments requested that
1868 ** prefers UTF-8 when a UTF-16 encoding is requested, or vice versa.
1869 ** 5: A function with the exact number of arguments requested that
1870 ** prefers UTF-16LE when UTF-16BE is requested, or vice versa.
1871 ** 6: An exact match.
1872 **
1873 ** A larger value of 'matchqual' indicates a more desirable match.
1874 */
danielk1977e12c17b2004-06-23 12:35:14 +00001875 if( p->nArg==-1 || p->nArg==nArg || nArg==-1 ){
danielk1977d8123362004-06-12 09:25:12 +00001876 int match = 1; /* Quality of this match */
1877 if( p->nArg==nArg || nArg==-1 ){
1878 match = 4;
danielk1977d02eb1f2004-06-06 09:44:03 +00001879 }
danielk1977d8123362004-06-12 09:25:12 +00001880 if( enc==p->iPrefEnc ){
1881 match += 2;
danielk1977d02eb1f2004-06-06 09:44:03 +00001882 }
danielk1977d8123362004-06-12 09:25:12 +00001883 else if( (enc==SQLITE_UTF16LE && p->iPrefEnc==SQLITE_UTF16BE) ||
1884 (enc==SQLITE_UTF16BE && p->iPrefEnc==SQLITE_UTF16LE) ){
1885 match += 1;
1886 }
1887
1888 if( match>bestmatch ){
1889 pBest = p;
1890 bestmatch = match;
danielk1977d02eb1f2004-06-06 09:44:03 +00001891 }
1892 }
drh8e0a2f92002-02-23 23:45:45 +00001893 }
danielk1977d02eb1f2004-06-06 09:44:03 +00001894
danielk1977d8123362004-06-12 09:25:12 +00001895 /* If the createFlag parameter is true, and the seach did not reveal an
1896 ** exact match for the name, number of arguments and encoding, then add a
1897 ** new entry to the hash table and return it.
1898 */
1899 if( createFlag && bestmatch<6 &&
danielk1977d02eb1f2004-06-06 09:44:03 +00001900 (pBest = sqliteMalloc(sizeof(*pBest)+nName+1)) ){
1901 pBest->nArg = nArg;
1902 pBest->pNext = pFirst;
1903 pBest->zName = (char*)&pBest[1];
danielk1977d8123362004-06-12 09:25:12 +00001904 pBest->iPrefEnc = enc;
danielk1977d02eb1f2004-06-06 09:44:03 +00001905 memcpy(pBest->zName, zName, nName);
1906 pBest->zName[nName] = 0;
1907 sqlite3HashInsert(&db->aFunc, pBest->zName, nName, (void*)pBest);
drh8e0a2f92002-02-23 23:45:45 +00001908 }
danielk1977d02eb1f2004-06-06 09:44:03 +00001909
1910 if( pBest && (pBest->xStep || pBest->xFunc || createFlag) ){
1911 return pBest;
drh8e0a2f92002-02-23 23:45:45 +00001912 }
danielk1977d02eb1f2004-06-06 09:44:03 +00001913 return 0;
drh8e0a2f92002-02-23 23:45:45 +00001914}