blob: c0815550686dbc842c7010cdf44cc7b6655aeb01 [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**
drh145716b2004-09-24 12:24:06 +000015** $Id: expr.c,v 1.164 2004/09/24 12:24:07 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 );
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/*
drh91bb0ee2004-09-01 03:06:34 +0000215** Join two expressions using an AND operator. If either expression is
216** NULL, then just return the other expression.
217*/
218Expr *sqlite3ExprAnd(Expr *pLeft, Expr *pRight){
219 if( pLeft==0 ){
220 return pRight;
221 }else if( pRight==0 ){
222 return pLeft;
223 }else{
224 return sqlite3Expr(TK_AND, pLeft, pRight, 0);
225 }
226}
227
228/*
drh6977fea2002-10-22 23:38:04 +0000229** Set the Expr.span field of the given expression to span all
drha76b5df2002-02-23 02:32:10 +0000230** text between the two given tokens.
231*/
danielk19774adee202004-05-08 08:23:19 +0000232void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
drh4efc4752004-01-16 15:55:37 +0000233 assert( pRight!=0 );
234 assert( pLeft!=0 );
drh71c697e2004-08-08 23:39:19 +0000235 if( !sqlite3_malloc_failed && pRight->z && pLeft->z ){
drhad6d9462004-09-19 02:15:24 +0000236 assert( pLeft->dyn==0 || pLeft->z[pLeft->n]==0 );
drh145716b2004-09-24 12:24:06 +0000237 if( pLeft->dyn==0 && pRight->dyn==0 ){
drh6977fea2002-10-22 23:38:04 +0000238 pExpr->span.z = pLeft->z;
239 pExpr->span.n = pRight->n + Addr(pRight->z) - Addr(pLeft->z);
drh4b59ab52002-08-24 18:24:51 +0000240 }else{
drh6977fea2002-10-22 23:38:04 +0000241 pExpr->span.z = 0;
drh4b59ab52002-08-24 18:24:51 +0000242 }
drha76b5df2002-02-23 02:32:10 +0000243 }
244}
245
246/*
247** Construct a new expression node for a function with multiple
248** arguments.
249*/
danielk19774adee202004-05-08 08:23:19 +0000250Expr *sqlite3ExprFunction(ExprList *pList, Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000251 Expr *pNew;
252 pNew = sqliteMalloc( sizeof(Expr) );
253 if( pNew==0 ){
danielk19774adee202004-05-08 08:23:19 +0000254 /* sqlite3ExprListDelete(pList); // Leak pList when malloc fails */
drha76b5df2002-02-23 02:32:10 +0000255 return 0;
256 }
257 pNew->op = TK_FUNCTION;
258 pNew->pList = pList;
259 if( pToken ){
drh4b59ab52002-08-24 18:24:51 +0000260 assert( pToken->dyn==0 );
drha76b5df2002-02-23 02:32:10 +0000261 pNew->token = *pToken;
262 }else{
263 pNew->token.z = 0;
drha76b5df2002-02-23 02:32:10 +0000264 }
drh6977fea2002-10-22 23:38:04 +0000265 pNew->span = pNew->token;
drha76b5df2002-02-23 02:32:10 +0000266 return pNew;
267}
268
269/*
drhfa6bc002004-09-07 16:19:52 +0000270** Assign a variable number to an expression that encodes a wildcard
271** in the original SQL statement.
272**
273** Wildcards consisting of a single "?" are assigned the next sequential
274** variable number.
275**
276** Wildcards of the form "?nnn" are assigned the number "nnn". We make
277** sure "nnn" is not too be to avoid a denial of service attack when
278** the SQL statement comes from an external source.
279**
280** Wildcards of the form ":aaa" or "$aaa" are assigned the same number
281** as the previous instance of the same wildcard. Or if this is the first
282** instance of the wildcard, the next sequenial variable number is
283** assigned.
284*/
285void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){
286 Token *pToken;
287 if( pExpr==0 ) return;
288 pToken = &pExpr->token;
289 assert( pToken->n>=1 );
290 assert( pToken->z!=0 );
291 assert( pToken->z[0]!=0 );
292 if( pToken->n==1 ){
293 /* Wildcard of the form "?". Assign the next variable number */
294 pExpr->iTable = ++pParse->nVar;
295 }else if( pToken->z[0]=='?' ){
296 /* Wildcard of the form "?nnn". Convert "nnn" to an integer and
297 ** use it as the variable number */
298 int i;
299 pExpr->iTable = i = atoi(&pToken->z[1]);
300 if( i<1 || i>SQLITE_MAX_VARIABLE_NUMBER ){
301 sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
302 SQLITE_MAX_VARIABLE_NUMBER);
303 }
304 if( i>pParse->nVar ){
305 pParse->nVar = i;
306 }
307 }else{
308 /* Wildcards of the form ":aaa" or "$aaa". Reuse the same variable
309 ** number as the prior appearance of the same name, or if the name
310 ** has never appeared before, reuse the same variable number
311 */
312 int i, n;
313 n = pToken->n;
314 for(i=0; i<pParse->nVarExpr; i++){
315 Expr *pE;
316 if( (pE = pParse->apVarExpr[i])!=0
317 && pE->token.n==n
318 && memcmp(pE->token.z, pToken->z, n)==0 ){
319 pExpr->iTable = pE->iTable;
320 break;
321 }
322 }
323 if( i>=pParse->nVarExpr ){
324 pExpr->iTable = ++pParse->nVar;
325 if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){
326 pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10;
327 pParse->apVarExpr = sqliteRealloc(pParse->apVarExpr,
328 pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0]) );
329 }
330 if( !sqlite3_malloc_failed ){
331 assert( pParse->apVarExpr!=0 );
332 pParse->apVarExpr[pParse->nVarExpr++] = pExpr;
333 }
334 }
335 }
336}
337
338/*
drha2e00042002-01-22 03:13:42 +0000339** Recursively delete an expression tree.
340*/
danielk19774adee202004-05-08 08:23:19 +0000341void sqlite3ExprDelete(Expr *p){
drha2e00042002-01-22 03:13:42 +0000342 if( p==0 ) return;
drh4efc4752004-01-16 15:55:37 +0000343 if( p->span.dyn ) sqliteFree((char*)p->span.z);
344 if( p->token.dyn ) sqliteFree((char*)p->token.z);
danielk19774adee202004-05-08 08:23:19 +0000345 sqlite3ExprDelete(p->pLeft);
346 sqlite3ExprDelete(p->pRight);
347 sqlite3ExprListDelete(p->pList);
348 sqlite3SelectDelete(p->pSelect);
drha2e00042002-01-22 03:13:42 +0000349 sqliteFree(p);
350}
351
drha76b5df2002-02-23 02:32:10 +0000352
353/*
drhff78bd22002-02-27 01:47:11 +0000354** The following group of routines make deep copies of expressions,
355** expression lists, ID lists, and select statements. The copies can
356** be deleted (by being passed to their respective ...Delete() routines)
357** without effecting the originals.
358**
danielk19774adee202004-05-08 08:23:19 +0000359** The expression list, ID, and source lists return by sqlite3ExprListDup(),
360** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
drhad3cab52002-05-24 02:04:32 +0000361** by subsequent calls to sqlite*ListAppend() routines.
drhff78bd22002-02-27 01:47:11 +0000362**
drhad3cab52002-05-24 02:04:32 +0000363** Any tables that the SrcList might point to are not duplicated.
drhff78bd22002-02-27 01:47:11 +0000364*/
danielk19774adee202004-05-08 08:23:19 +0000365Expr *sqlite3ExprDup(Expr *p){
drhff78bd22002-02-27 01:47:11 +0000366 Expr *pNew;
367 if( p==0 ) return 0;
drhfcb78a42003-01-18 20:11:05 +0000368 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000369 if( pNew==0 ) return 0;
drh3b167c72002-06-28 12:18:47 +0000370 memcpy(pNew, p, sizeof(*pNew));
drh6977fea2002-10-22 23:38:04 +0000371 if( p->token.z!=0 ){
drh4b59ab52002-08-24 18:24:51 +0000372 pNew->token.z = sqliteStrDup(p->token.z);
373 pNew->token.dyn = 1;
374 }else{
drh4efc4752004-01-16 15:55:37 +0000375 assert( pNew->token.z==0 );
drh4b59ab52002-08-24 18:24:51 +0000376 }
drh6977fea2002-10-22 23:38:04 +0000377 pNew->span.z = 0;
danielk19774adee202004-05-08 08:23:19 +0000378 pNew->pLeft = sqlite3ExprDup(p->pLeft);
379 pNew->pRight = sqlite3ExprDup(p->pRight);
380 pNew->pList = sqlite3ExprListDup(p->pList);
381 pNew->pSelect = sqlite3SelectDup(p->pSelect);
drhff78bd22002-02-27 01:47:11 +0000382 return pNew;
383}
danielk19774adee202004-05-08 08:23:19 +0000384void sqlite3TokenCopy(Token *pTo, Token *pFrom){
drh4b59ab52002-08-24 18:24:51 +0000385 if( pTo->dyn ) sqliteFree((char*)pTo->z);
drh4b59ab52002-08-24 18:24:51 +0000386 if( pFrom->z ){
387 pTo->n = pFrom->n;
388 pTo->z = sqliteStrNDup(pFrom->z, pFrom->n);
389 pTo->dyn = 1;
390 }else{
drh4b59ab52002-08-24 18:24:51 +0000391 pTo->z = 0;
drh4b59ab52002-08-24 18:24:51 +0000392 }
393}
danielk19774adee202004-05-08 08:23:19 +0000394ExprList *sqlite3ExprListDup(ExprList *p){
drhff78bd22002-02-27 01:47:11 +0000395 ExprList *pNew;
drh145716b2004-09-24 12:24:06 +0000396 struct ExprList_item *pItem, *pOldItem;
drhff78bd22002-02-27 01:47:11 +0000397 int i;
398 if( p==0 ) return 0;
399 pNew = sqliteMalloc( sizeof(*pNew) );
400 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000401 pNew->nExpr = pNew->nAlloc = p->nExpr;
drh3e7bc9c2004-02-21 19:17:17 +0000402 pNew->a = pItem = sqliteMalloc( p->nExpr*sizeof(p->a[0]) );
danielk1977e0048402004-06-15 16:51:01 +0000403 if( pItem==0 ){
404 sqliteFree(pNew);
405 return 0;
406 }
drh145716b2004-09-24 12:24:06 +0000407 pOldItem = p->a;
408 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
drh4b59ab52002-08-24 18:24:51 +0000409 Expr *pNewExpr, *pOldExpr;
drh145716b2004-09-24 12:24:06 +0000410 pItem->pExpr = pNewExpr = sqlite3ExprDup(pOldExpr = pOldItem->pExpr);
drh6977fea2002-10-22 23:38:04 +0000411 if( pOldExpr->span.z!=0 && pNewExpr ){
412 /* Always make a copy of the span for top-level expressions in the
drh4b59ab52002-08-24 18:24:51 +0000413 ** expression list. The logic in SELECT processing that determines
414 ** the names of columns in the result set needs this information */
danielk19774adee202004-05-08 08:23:19 +0000415 sqlite3TokenCopy(&pNewExpr->span, &pOldExpr->span);
drh4b59ab52002-08-24 18:24:51 +0000416 }
drh1f3e9052002-10-31 00:09:39 +0000417 assert( pNewExpr==0 || pNewExpr->span.z!=0
danielk197724b03fd2004-05-10 10:34:34 +0000418 || pOldExpr->span.z==0 || sqlite3_malloc_failed );
drh145716b2004-09-24 12:24:06 +0000419 pItem->zName = sqliteStrDup(pOldItem->zName);
420 pItem->sortOrder = pOldItem->sortOrder;
421 pItem->isAgg = pOldItem->isAgg;
drh3e7bc9c2004-02-21 19:17:17 +0000422 pItem->done = 0;
drhff78bd22002-02-27 01:47:11 +0000423 }
424 return pNew;
425}
danielk19774adee202004-05-08 08:23:19 +0000426SrcList *sqlite3SrcListDup(SrcList *p){
drhad3cab52002-05-24 02:04:32 +0000427 SrcList *pNew;
428 int i;
drh113088e2003-03-20 01:16:58 +0000429 int nByte;
drhad3cab52002-05-24 02:04:32 +0000430 if( p==0 ) return 0;
drh113088e2003-03-20 01:16:58 +0000431 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
drh4efc4752004-01-16 15:55:37 +0000432 pNew = sqliteMallocRaw( nByte );
drhad3cab52002-05-24 02:04:32 +0000433 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000434 pNew->nSrc = pNew->nAlloc = p->nSrc;
drhad3cab52002-05-24 02:04:32 +0000435 for(i=0; i<p->nSrc; i++){
drh4efc4752004-01-16 15:55:37 +0000436 struct SrcList_item *pNewItem = &pNew->a[i];
437 struct SrcList_item *pOldItem = &p->a[i];
438 pNewItem->zDatabase = sqliteStrDup(pOldItem->zDatabase);
439 pNewItem->zName = sqliteStrDup(pOldItem->zName);
440 pNewItem->zAlias = sqliteStrDup(pOldItem->zAlias);
441 pNewItem->jointype = pOldItem->jointype;
442 pNewItem->iCursor = pOldItem->iCursor;
443 pNewItem->pTab = 0;
danielk19774adee202004-05-08 08:23:19 +0000444 pNewItem->pSelect = sqlite3SelectDup(pOldItem->pSelect);
445 pNewItem->pOn = sqlite3ExprDup(pOldItem->pOn);
446 pNewItem->pUsing = sqlite3IdListDup(pOldItem->pUsing);
drhad3cab52002-05-24 02:04:32 +0000447 }
448 return pNew;
449}
danielk19774adee202004-05-08 08:23:19 +0000450IdList *sqlite3IdListDup(IdList *p){
drhff78bd22002-02-27 01:47:11 +0000451 IdList *pNew;
452 int i;
453 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000454 pNew = sqliteMallocRaw( sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +0000455 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000456 pNew->nId = pNew->nAlloc = p->nId;
drh4efc4752004-01-16 15:55:37 +0000457 pNew->a = sqliteMallocRaw( p->nId*sizeof(p->a[0]) );
drhe4697f52002-05-23 02:09:03 +0000458 if( pNew->a==0 ) return 0;
drhff78bd22002-02-27 01:47:11 +0000459 for(i=0; i<p->nId; i++){
drh4efc4752004-01-16 15:55:37 +0000460 struct IdList_item *pNewItem = &pNew->a[i];
461 struct IdList_item *pOldItem = &p->a[i];
462 pNewItem->zName = sqliteStrDup(pOldItem->zName);
463 pNewItem->idx = pOldItem->idx;
drhff78bd22002-02-27 01:47:11 +0000464 }
465 return pNew;
466}
danielk19774adee202004-05-08 08:23:19 +0000467Select *sqlite3SelectDup(Select *p){
drhff78bd22002-02-27 01:47:11 +0000468 Select *pNew;
469 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000470 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000471 if( pNew==0 ) return 0;
472 pNew->isDistinct = p->isDistinct;
danielk19774adee202004-05-08 08:23:19 +0000473 pNew->pEList = sqlite3ExprListDup(p->pEList);
474 pNew->pSrc = sqlite3SrcListDup(p->pSrc);
475 pNew->pWhere = sqlite3ExprDup(p->pWhere);
476 pNew->pGroupBy = sqlite3ExprListDup(p->pGroupBy);
477 pNew->pHaving = sqlite3ExprDup(p->pHaving);
478 pNew->pOrderBy = sqlite3ExprListDup(p->pOrderBy);
drhff78bd22002-02-27 01:47:11 +0000479 pNew->op = p->op;
danielk19774adee202004-05-08 08:23:19 +0000480 pNew->pPrior = sqlite3SelectDup(p->pPrior);
drhff78bd22002-02-27 01:47:11 +0000481 pNew->nLimit = p->nLimit;
482 pNew->nOffset = p->nOffset;
483 pNew->zSelect = 0;
drh7b58dae2003-07-20 01:16:46 +0000484 pNew->iLimit = -1;
485 pNew->iOffset = -1;
danielk1977dc1bdc42004-06-11 10:51:27 +0000486 pNew->ppOpenTemp = 0;
drhff78bd22002-02-27 01:47:11 +0000487 return pNew;
488}
489
490
491/*
drha76b5df2002-02-23 02:32:10 +0000492** Add a new element to the end of an expression list. If pList is
493** initially NULL, then create a new expression list.
494*/
danielk19774adee202004-05-08 08:23:19 +0000495ExprList *sqlite3ExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){
drha76b5df2002-02-23 02:32:10 +0000496 if( pList==0 ){
497 pList = sqliteMalloc( sizeof(ExprList) );
498 if( pList==0 ){
danielk19774adee202004-05-08 08:23:19 +0000499 /* sqlite3ExprDelete(pExpr); // Leak memory if malloc fails */
drha76b5df2002-02-23 02:32:10 +0000500 return 0;
501 }
drh4efc4752004-01-16 15:55:37 +0000502 assert( pList->nAlloc==0 );
drha76b5df2002-02-23 02:32:10 +0000503 }
drh4305d102003-07-30 12:34:12 +0000504 if( pList->nAlloc<=pList->nExpr ){
drh4305d102003-07-30 12:34:12 +0000505 pList->nAlloc = pList->nAlloc*2 + 4;
drh4efc4752004-01-16 15:55:37 +0000506 pList->a = sqliteRealloc(pList->a, pList->nAlloc*sizeof(pList->a[0]));
507 if( pList->a==0 ){
danielk19774adee202004-05-08 08:23:19 +0000508 /* sqlite3ExprDelete(pExpr); // Leak memory if malloc fails */
drh4efc4752004-01-16 15:55:37 +0000509 pList->nExpr = pList->nAlloc = 0;
drha76b5df2002-02-23 02:32:10 +0000510 return pList;
511 }
drha76b5df2002-02-23 02:32:10 +0000512 }
drh4efc4752004-01-16 15:55:37 +0000513 assert( pList->a!=0 );
514 if( pExpr || pName ){
515 struct ExprList_item *pItem = &pList->a[pList->nExpr++];
516 memset(pItem, 0, sizeof(*pItem));
517 pItem->pExpr = pExpr;
drha99db3b2004-06-19 14:49:12 +0000518 pItem->zName = sqlite3NameFromToken(pName);
drha76b5df2002-02-23 02:32:10 +0000519 }
520 return pList;
521}
522
523/*
524** Delete an entire expression list.
525*/
danielk19774adee202004-05-08 08:23:19 +0000526void sqlite3ExprListDelete(ExprList *pList){
drha76b5df2002-02-23 02:32:10 +0000527 int i;
drhbe5c89a2004-07-26 00:31:09 +0000528 struct ExprList_item *pItem;
drha76b5df2002-02-23 02:32:10 +0000529 if( pList==0 ) return;
drh1bdd9b52004-04-23 17:04:44 +0000530 assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
531 assert( pList->nExpr<=pList->nAlloc );
drhbe5c89a2004-07-26 00:31:09 +0000532 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
533 sqlite3ExprDelete(pItem->pExpr);
534 sqliteFree(pItem->zName);
drha76b5df2002-02-23 02:32:10 +0000535 }
536 sqliteFree(pList->a);
537 sqliteFree(pList);
538}
539
540/*
drhfef52082000-06-06 01:50:43 +0000541** Walk an expression tree. Return 1 if the expression is constant
542** and 0 if it involves variables.
drh23989372002-05-21 13:43:04 +0000543**
544** For the purposes of this function, a double-quoted string (ex: "abc")
545** is considered a variable but a single-quoted string (ex: 'abc') is
546** a constant.
drhfef52082000-06-06 01:50:43 +0000547*/
danielk19774adee202004-05-08 08:23:19 +0000548int sqlite3ExprIsConstant(Expr *p){
drhfef52082000-06-06 01:50:43 +0000549 switch( p->op ){
550 case TK_ID:
drh967e8b72000-06-21 13:59:10 +0000551 case TK_COLUMN:
drhfef52082000-06-06 01:50:43 +0000552 case TK_DOT:
drh7bdc0c12003-04-19 17:27:24 +0000553 case TK_FUNCTION:
drhfef52082000-06-06 01:50:43 +0000554 return 0;
drh7bdc0c12003-04-19 17:27:24 +0000555 case TK_NULL:
drh23989372002-05-21 13:43:04 +0000556 case TK_STRING:
danielk1977c572ef72004-05-27 09:28:41 +0000557 case TK_BLOB:
drh92086432002-01-22 14:11:29 +0000558 case TK_INTEGER:
559 case TK_FLOAT:
drh50457892003-09-06 01:10:47 +0000560 case TK_VARIABLE:
drh92086432002-01-22 14:11:29 +0000561 return 1;
drhfef52082000-06-06 01:50:43 +0000562 default: {
danielk19774adee202004-05-08 08:23:19 +0000563 if( p->pLeft && !sqlite3ExprIsConstant(p->pLeft) ) return 0;
564 if( p->pRight && !sqlite3ExprIsConstant(p->pRight) ) return 0;
drhfef52082000-06-06 01:50:43 +0000565 if( p->pList ){
566 int i;
567 for(i=0; i<p->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +0000568 if( !sqlite3ExprIsConstant(p->pList->a[i].pExpr) ) return 0;
drhfef52082000-06-06 01:50:43 +0000569 }
570 }
drh92086432002-01-22 14:11:29 +0000571 return p->pLeft!=0 || p->pRight!=0 || (p->pList && p->pList->nExpr>0);
drhfef52082000-06-06 01:50:43 +0000572 }
573 }
drh92086432002-01-22 14:11:29 +0000574 return 0;
drhfef52082000-06-06 01:50:43 +0000575}
576
577/*
drh202b2df2004-01-06 01:13:46 +0000578** If the given expression codes a constant integer that is small enough
579** to fit in a 32-bit integer, return 1 and put the value of the integer
580** in *pValue. If the expression is not an integer or if it is too big
581** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
drhe4de1fe2002-06-02 16:09:01 +0000582*/
danielk19774adee202004-05-08 08:23:19 +0000583int sqlite3ExprIsInteger(Expr *p, int *pValue){
drhe4de1fe2002-06-02 16:09:01 +0000584 switch( p->op ){
585 case TK_INTEGER: {
drhfec19aa2004-05-19 20:41:03 +0000586 if( sqlite3GetInt32(p->token.z, pValue) ){
drh202b2df2004-01-06 01:13:46 +0000587 return 1;
588 }
589 break;
drhe4de1fe2002-06-02 16:09:01 +0000590 }
591 case TK_STRING: {
drh4c755c02004-08-08 20:22:17 +0000592 const u8 *z = (u8*)p->token.z;
drhe4de1fe2002-06-02 16:09:01 +0000593 int n = p->token.n;
drhbd790ee2002-06-02 18:22:06 +0000594 if( n>0 && z[0]=='-' ){ z++; n--; }
drhe4de1fe2002-06-02 16:09:01 +0000595 while( n>0 && *z && isdigit(*z) ){ z++; n--; }
drhfec19aa2004-05-19 20:41:03 +0000596 if( n==0 && sqlite3GetInt32(p->token.z, pValue) ){
drhe4de1fe2002-06-02 16:09:01 +0000597 return 1;
598 }
599 break;
600 }
drh4b59ab52002-08-24 18:24:51 +0000601 case TK_UPLUS: {
danielk19774adee202004-05-08 08:23:19 +0000602 return sqlite3ExprIsInteger(p->pLeft, pValue);
drh4b59ab52002-08-24 18:24:51 +0000603 }
drhe4de1fe2002-06-02 16:09:01 +0000604 case TK_UMINUS: {
605 int v;
danielk19774adee202004-05-08 08:23:19 +0000606 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
drhe4de1fe2002-06-02 16:09:01 +0000607 *pValue = -v;
608 return 1;
609 }
610 break;
611 }
612 default: break;
613 }
614 return 0;
615}
616
617/*
drhc4a3c772001-04-04 11:48:57 +0000618** Return TRUE if the given string is a row-id column name.
619*/
danielk19774adee202004-05-08 08:23:19 +0000620int sqlite3IsRowid(const char *z){
621 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
622 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
623 if( sqlite3StrICmp(z, "OID")==0 ) return 1;
drhc4a3c772001-04-04 11:48:57 +0000624 return 0;
625}
626
627/*
drh8141f612004-01-25 22:44:58 +0000628** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
629** that name in the set of source tables in pSrcList and make the pExpr
630** expression node refer back to that source column. The following changes
631** are made to pExpr:
632**
633** pExpr->iDb Set the index in db->aDb[] of the database holding
634** the table.
635** pExpr->iTable Set to the cursor number for the table obtained
636** from pSrcList.
637** pExpr->iColumn Set to the column number within the table.
drh8141f612004-01-25 22:44:58 +0000638** pExpr->op Set to TK_COLUMN.
639** pExpr->pLeft Any expression this points to is deleted
640** pExpr->pRight Any expression this points to is deleted.
641**
642** The pDbToken is the name of the database (the "X"). This value may be
643** NULL meaning that name is of the form Y.Z or Z. Any available database
644** can be used. The pTableToken is the name of the table (the "Y"). This
645** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it
646** means that the form of the name is Z and that columns from any table
647** can be used.
648**
649** If the name cannot be resolved unambiguously, leave an error message
650** in pParse and return non-zero. Return zero on success.
651*/
652static int lookupName(
653 Parse *pParse, /* The parsing context */
654 Token *pDbToken, /* Name of the database containing table, or NULL */
655 Token *pTableToken, /* Name of table containing column, or NULL */
656 Token *pColumnToken, /* Name of the column. */
657 SrcList *pSrcList, /* List of tables used to resolve column names */
658 ExprList *pEList, /* List of expressions used to resolve "AS" */
659 Expr *pExpr /* Make this EXPR node point to the selected column */
660){
661 char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */
662 char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */
663 char *zCol = 0; /* Name of the column. The "Z" */
664 int i, j; /* Loop counters */
665 int cnt = 0; /* Number of matching column names */
666 int cntTab = 0; /* Number of matching table names */
drh9bb575f2004-09-06 17:24:11 +0000667 sqlite3 *db = pParse->db; /* The database */
drh8141f612004-01-25 22:44:58 +0000668
669 assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
drha99db3b2004-06-19 14:49:12 +0000670 zDb = sqlite3NameFromToken(pDbToken);
671 zTab = sqlite3NameFromToken(pTableToken);
672 zCol = sqlite3NameFromToken(pColumnToken);
danielk197724b03fd2004-05-10 10:34:34 +0000673 if( sqlite3_malloc_failed ){
drh8141f612004-01-25 22:44:58 +0000674 return 1; /* Leak memory (zDb and zTab) if malloc fails */
675 }
676 assert( zTab==0 || pEList==0 );
677
678 pExpr->iTable = -1;
679 for(i=0; i<pSrcList->nSrc; i++){
680 struct SrcList_item *pItem = &pSrcList->a[i];
681 Table *pTab = pItem->pTab;
682 Column *pCol;
683
684 if( pTab==0 ) continue;
685 assert( pTab->nCol>0 );
686 if( zTab ){
687 if( pItem->zAlias ){
688 char *zTabName = pItem->zAlias;
danielk19774adee202004-05-08 08:23:19 +0000689 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
drh8141f612004-01-25 22:44:58 +0000690 }else{
691 char *zTabName = pTab->zName;
danielk19774adee202004-05-08 08:23:19 +0000692 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
693 if( zDb!=0 && sqlite3StrICmp(db->aDb[pTab->iDb].zName, zDb)!=0 ){
drh8141f612004-01-25 22:44:58 +0000694 continue;
695 }
696 }
697 }
698 if( 0==(cntTab++) ){
699 pExpr->iTable = pItem->iCursor;
700 pExpr->iDb = pTab->iDb;
701 }
702 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
danielk19774adee202004-05-08 08:23:19 +0000703 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
drh8141f612004-01-25 22:44:58 +0000704 cnt++;
705 pExpr->iTable = pItem->iCursor;
706 pExpr->iDb = pTab->iDb;
707 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
708 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
danielk1977a37cdde2004-05-16 11:15:36 +0000709 pExpr->affinity = pTab->aCol[j].affinity;
danielk19770202b292004-06-09 09:55:16 +0000710 pExpr->pColl = pTab->aCol[j].pColl;
drh8141f612004-01-25 22:44:58 +0000711 break;
712 }
713 }
714 }
715
716 /* If we have not already resolved the name, then maybe
717 ** it is a new.* or old.* trigger argument reference
718 */
719 if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
720 TriggerStack *pTriggerStack = pParse->trigStack;
721 Table *pTab = 0;
danielk19774adee202004-05-08 08:23:19 +0000722 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){
drh8141f612004-01-25 22:44:58 +0000723 pExpr->iTable = pTriggerStack->newIdx;
724 assert( pTriggerStack->pTab );
725 pTab = pTriggerStack->pTab;
danielk19774adee202004-05-08 08:23:19 +0000726 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab) == 0 ){
drh8141f612004-01-25 22:44:58 +0000727 pExpr->iTable = pTriggerStack->oldIdx;
728 assert( pTriggerStack->pTab );
729 pTab = pTriggerStack->pTab;
730 }
731
732 if( pTab ){
733 int j;
734 Column *pCol = pTab->aCol;
735
736 pExpr->iDb = pTab->iDb;
737 cntTab++;
738 for(j=0; j < pTab->nCol; j++, pCol++) {
danielk19774adee202004-05-08 08:23:19 +0000739 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
drh8141f612004-01-25 22:44:58 +0000740 cnt++;
741 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
danielk1977a37cdde2004-05-16 11:15:36 +0000742 pExpr->affinity = pTab->aCol[j].affinity;
danielk19770202b292004-06-09 09:55:16 +0000743 pExpr->pColl = pTab->aCol[j].pColl;
drh8141f612004-01-25 22:44:58 +0000744 break;
745 }
746 }
747 }
748 }
749
750 /*
751 ** Perhaps the name is a reference to the ROWID
752 */
danielk19774adee202004-05-08 08:23:19 +0000753 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
drh8141f612004-01-25 22:44:58 +0000754 cnt = 1;
755 pExpr->iColumn = -1;
danielk1977a37cdde2004-05-16 11:15:36 +0000756 pExpr->affinity = SQLITE_AFF_INTEGER;
drh8141f612004-01-25 22:44:58 +0000757 }
758
759 /*
760 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
761 ** might refer to an result-set alias. This happens, for example, when
762 ** we are resolving names in the WHERE clause of the following command:
763 **
764 ** SELECT a+b AS x FROM table WHERE x<10;
765 **
766 ** In cases like this, replace pExpr with a copy of the expression that
767 ** forms the result set entry ("a+b" in the example) and return immediately.
768 ** Note that the expression in the result set should have already been
769 ** resolved by the time the WHERE clause is resolved.
770 */
771 if( cnt==0 && pEList!=0 ){
772 for(j=0; j<pEList->nExpr; j++){
773 char *zAs = pEList->a[j].zName;
danielk19774adee202004-05-08 08:23:19 +0000774 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
drh8141f612004-01-25 22:44:58 +0000775 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
776 pExpr->op = TK_AS;
777 pExpr->iColumn = j;
danielk19774adee202004-05-08 08:23:19 +0000778 pExpr->pLeft = sqlite3ExprDup(pEList->a[j].pExpr);
drh8141f612004-01-25 22:44:58 +0000779 sqliteFree(zCol);
780 assert( zTab==0 && zDb==0 );
781 return 0;
782 }
783 }
784 }
785
786 /*
787 ** If X and Y are NULL (in other words if only the column name Z is
788 ** supplied) and the value of Z is enclosed in double-quotes, then
789 ** Z is a string literal if it doesn't match any column names. In that
790 ** case, we need to return right away and not make any changes to
791 ** pExpr.
792 */
793 if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
794 sqliteFree(zCol);
795 return 0;
796 }
797
798 /*
799 ** cnt==0 means there was not match. cnt>1 means there were two or
800 ** more matches. Either way, we have an error.
801 */
802 if( cnt!=1 ){
803 char *z = 0;
804 char *zErr;
805 zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s";
806 if( zDb ){
danielk19774adee202004-05-08 08:23:19 +0000807 sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, 0);
drh8141f612004-01-25 22:44:58 +0000808 }else if( zTab ){
danielk19774adee202004-05-08 08:23:19 +0000809 sqlite3SetString(&z, zTab, ".", zCol, 0);
drh8141f612004-01-25 22:44:58 +0000810 }else{
811 z = sqliteStrDup(zCol);
812 }
danielk19774adee202004-05-08 08:23:19 +0000813 sqlite3ErrorMsg(pParse, zErr, z);
drh8141f612004-01-25 22:44:58 +0000814 sqliteFree(z);
815 }
816
817 /* Clean up and return
818 */
819 sqliteFree(zDb);
820 sqliteFree(zTab);
821 sqliteFree(zCol);
danielk19774adee202004-05-08 08:23:19 +0000822 sqlite3ExprDelete(pExpr->pLeft);
drh8141f612004-01-25 22:44:58 +0000823 pExpr->pLeft = 0;
danielk19774adee202004-05-08 08:23:19 +0000824 sqlite3ExprDelete(pExpr->pRight);
drh8141f612004-01-25 22:44:58 +0000825 pExpr->pRight = 0;
826 pExpr->op = TK_COLUMN;
danielk19774adee202004-05-08 08:23:19 +0000827 sqlite3AuthRead(pParse, pExpr, pSrcList);
drh8141f612004-01-25 22:44:58 +0000828 return cnt!=1;
829}
830
831/*
drhcce7d172000-05-31 15:34:51 +0000832** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +0000833** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +0000834** index to the table in the table list and a column offset. The
835** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
836** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +0000837** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +0000838** VDBE cursor number for a cursor that is pointing into the referenced
839** table. The Expr.iColumn value is changed to the index of the column
840** of the referenced table. The Expr.iColumn value for the special
841** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
842** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +0000843**
drhfef52082000-06-06 01:50:43 +0000844** We also check for instances of the IN operator. IN comes in two
845** forms:
846**
847** expr IN (exprlist)
848** and
849** expr IN (SELECT ...)
850**
851** The first form is handled by creating a set holding the list
852** of allowed values. The second form causes the SELECT to generate
853** a temporary table.
854**
855** This routine also looks for scalar SELECTs that are part of an expression.
drh19a775c2000-06-05 18:54:46 +0000856** If it finds any, it generates code to write the value of that select
857** into a memory cell.
drhcce7d172000-05-31 15:34:51 +0000858**
drh967e8b72000-06-21 13:59:10 +0000859** Unknown columns or tables provoke an error. The function returns
drhcce7d172000-05-31 15:34:51 +0000860** the number of errors seen and leaves an error message on pParse->zErrMsg.
861*/
danielk19774adee202004-05-08 08:23:19 +0000862int sqlite3ExprResolveIds(
drha2e00042002-01-22 03:13:42 +0000863 Parse *pParse, /* The parser context */
drh8141f612004-01-25 22:44:58 +0000864 SrcList *pSrcList, /* List of tables used to resolve column names */
drha2e00042002-01-22 03:13:42 +0000865 ExprList *pEList, /* List of expressions used to resolve "AS" */
866 Expr *pExpr /* The expression to be analyzed. */
867){
drh6a3ea0e2003-05-02 14:32:12 +0000868 int i;
869
drh8141f612004-01-25 22:44:58 +0000870 if( pExpr==0 || pSrcList==0 ) return 0;
871 for(i=0; i<pSrcList->nSrc; i++){
872 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab );
drh6a3ea0e2003-05-02 14:32:12 +0000873 }
drhcce7d172000-05-31 15:34:51 +0000874 switch( pExpr->op ){
drh23989372002-05-21 13:43:04 +0000875 /* Double-quoted strings (ex: "abc") are used as identifiers if
876 ** possible. Otherwise they remain as strings. Single-quoted
877 ** strings (ex: 'abc') are always string literals.
878 */
879 case TK_STRING: {
880 if( pExpr->token.z[0]=='\'' ) break;
881 /* Fall thru into the TK_ID case if this is a double-quoted string */
882 }
drh8141f612004-01-25 22:44:58 +0000883 /* A lone identifier is the name of a columnd.
drha2e00042002-01-22 03:13:42 +0000884 */
drhcce7d172000-05-31 15:34:51 +0000885 case TK_ID: {
drh8141f612004-01-25 22:44:58 +0000886 if( lookupName(pParse, 0, 0, &pExpr->token, pSrcList, pEList, pExpr) ){
drhcce7d172000-05-31 15:34:51 +0000887 return 1;
drhed6c8672003-01-12 18:02:16 +0000888 }
drhcce7d172000-05-31 15:34:51 +0000889 break;
890 }
891
drhd24cc422003-03-27 12:51:24 +0000892 /* A table name and column name: ID.ID
893 ** Or a database, table and column: ID.ID.ID
894 */
drhcce7d172000-05-31 15:34:51 +0000895 case TK_DOT: {
drh8141f612004-01-25 22:44:58 +0000896 Token *pColumn;
897 Token *pTable;
898 Token *pDb;
899 Expr *pRight;
drhcce7d172000-05-31 15:34:51 +0000900
drhcce7d172000-05-31 15:34:51 +0000901 pRight = pExpr->pRight;
drhd24cc422003-03-27 12:51:24 +0000902 if( pRight->op==TK_ID ){
drh8141f612004-01-25 22:44:58 +0000903 pDb = 0;
904 pTable = &pExpr->pLeft->token;
905 pColumn = &pRight->token;
drhd24cc422003-03-27 12:51:24 +0000906 }else{
drh8141f612004-01-25 22:44:58 +0000907 assert( pRight->op==TK_DOT );
908 pDb = &pExpr->pLeft->token;
909 pTable = &pRight->pLeft->token;
910 pColumn = &pRight->pRight->token;
drhd24cc422003-03-27 12:51:24 +0000911 }
drh8141f612004-01-25 22:44:58 +0000912 if( lookupName(pParse, pDb, pTable, pColumn, pSrcList, 0, pExpr) ){
drhdaffd0e2001-04-11 14:28:42 +0000913 return 1;
914 }
drhcce7d172000-05-31 15:34:51 +0000915 break;
916 }
917
drhfef52082000-06-06 01:50:43 +0000918 case TK_IN: {
danielk1977e014a832004-05-17 10:48:57 +0000919 char affinity;
danielk19774adee202004-05-08 08:23:19 +0000920 Vdbe *v = sqlite3GetVdbe(pParse);
drhd3d39e92004-05-20 22:16:29 +0000921 KeyInfo keyInfo;
danielk19770202b292004-06-09 09:55:16 +0000922 int addr; /* Address of OP_OpenTemp instruction */
drhd3d39e92004-05-20 22:16:29 +0000923
drhfef52082000-06-06 01:50:43 +0000924 if( v==0 ) return 1;
danielk19774adee202004-05-08 08:23:19 +0000925 if( sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pLeft) ){
drhcfab11b2000-06-06 03:31:22 +0000926 return 1;
927 }
danielk1977bf3b7212004-05-18 10:06:24 +0000928 affinity = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +0000929
930 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
931 ** expression it is handled the same way. A temporary table is
932 ** filled with single-field index keys representing the results
933 ** from the SELECT or the <exprlist>.
934 **
935 ** If the 'x' expression is a column value, or the SELECT...
936 ** statement returns a column value, then the affinity of that
937 ** column is used to build the index keys. If both 'x' and the
938 ** SELECT... statement are columns, then numeric affinity is used
939 ** if either column has NUMERIC or INTEGER affinity. If neither
940 ** 'x' nor the SELECT... statement are columns, then numeric affinity
941 ** is used.
942 */
943 pExpr->iTable = pParse->nTab++;
danielk19770202b292004-06-09 09:55:16 +0000944 addr = sqlite3VdbeAddOp(v, OP_OpenTemp, pExpr->iTable, 0);
drhd3d39e92004-05-20 22:16:29 +0000945 memset(&keyInfo, 0, sizeof(keyInfo));
946 keyInfo.nField = 1;
drhf3218fe2004-05-28 08:21:02 +0000947 sqlite3VdbeAddOp(v, OP_SetNumColumns, pExpr->iTable, 1);
danielk1977e014a832004-05-17 10:48:57 +0000948
drhfef52082000-06-06 01:50:43 +0000949 if( pExpr->pSelect ){
950 /* Case 1: expr IN (SELECT ...)
951 **
danielk1977e014a832004-05-17 10:48:57 +0000952 ** Generate code to write the results of the select into the temporary
953 ** table allocated and opened above.
drhfef52082000-06-06 01:50:43 +0000954 */
danielk1977e014a832004-05-17 10:48:57 +0000955 int iParm = pExpr->iTable + (((int)affinity)<<16);
drhbe5c89a2004-07-26 00:31:09 +0000956 ExprList *pEList;
danielk1977e014a832004-05-17 10:48:57 +0000957 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
danielk1977bf3b7212004-05-18 10:06:24 +0000958 sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +0000959 pEList = pExpr->pSelect->pEList;
960 if( pEList && pEList->nExpr>0 ){
danielk19777cedc8d2004-06-10 10:50:08 +0000961 keyInfo.aColl[0] = binaryCompareCollSeq(pParse, pExpr->pLeft,
drhbe5c89a2004-07-26 00:31:09 +0000962 pEList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +0000963 }
drhfef52082000-06-06 01:50:43 +0000964 }else if( pExpr->pList ){
965 /* Case 2: expr IN (exprlist)
966 **
danielk1977e014a832004-05-17 10:48:57 +0000967 ** For each expression, build an index key from the evaluation and
968 ** store it in the temporary table. If <expr> is a column, then use
969 ** that columns affinity when building index keys. If <expr> is not
970 ** a column, use numeric affinity.
drhfef52082000-06-06 01:50:43 +0000971 */
danielk1977e014a832004-05-17 10:48:57 +0000972 int i;
973 char const *affStr;
974 if( !affinity ){
975 affinity = SQLITE_AFF_NUMERIC;
976 }
977 affStr = sqlite3AffinityString(affinity);
danielk19770202b292004-06-09 09:55:16 +0000978 keyInfo.aColl[0] = pExpr->pLeft->pColl;
danielk1977e014a832004-05-17 10:48:57 +0000979
980 /* Loop through each expression in <exprlist>. */
drhfef52082000-06-06 01:50:43 +0000981 for(i=0; i<pExpr->pList->nExpr; i++){
982 Expr *pE2 = pExpr->pList->a[i].pExpr;
danielk1977e014a832004-05-17 10:48:57 +0000983
984 /* Check that the expression is constant and valid. */
danielk19774adee202004-05-08 08:23:19 +0000985 if( !sqlite3ExprIsConstant(pE2) ){
986 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +0000987 "right-hand side of IN operator must be constant");
drhfef52082000-06-06 01:50:43 +0000988 return 1;
989 }
danielk19774adee202004-05-08 08:23:19 +0000990 if( sqlite3ExprCheck(pParse, pE2, 0, 0) ){
drh4794b982000-06-06 13:54:14 +0000991 return 1;
992 }
danielk1977e014a832004-05-17 10:48:57 +0000993
994 /* Evaluate the expression and insert it into the temp table */
995 sqlite3ExprCode(pParse, pE2);
danielk1977ededfd52004-06-17 07:53:01 +0000996 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, affStr, P3_STATIC);
danielk19770f69c1e2004-05-29 11:24:50 +0000997 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +0000998 sqlite3VdbeAddOp(v, OP_PutStrKey, pExpr->iTable, 0);
drhfef52082000-06-06 01:50:43 +0000999 }
1000 }
danielk19770202b292004-06-09 09:55:16 +00001001 sqlite3VdbeChangeP3(v, addr, (void *)&keyInfo, P3_KEYINFO);
1002
drhcfab11b2000-06-06 03:31:22 +00001003 break;
drhfef52082000-06-06 01:50:43 +00001004 }
1005
drh19a775c2000-06-05 18:54:46 +00001006 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +00001007 /* This has to be a scalar SELECT. Generate code to put the
1008 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +00001009 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +00001010 */
drh967e8b72000-06-21 13:59:10 +00001011 pExpr->iColumn = pParse->nMem++;
danielk1977bf3b7212004-05-18 10:06:24 +00001012 if(sqlite3Select(pParse, pExpr->pSelect, SRT_Mem,pExpr->iColumn,0,0,0,0)){
drh19a775c2000-06-05 18:54:46 +00001013 return 1;
1014 }
1015 break;
1016 }
1017
drhcce7d172000-05-31 15:34:51 +00001018 /* For all else, just recursively walk the tree */
1019 default: {
drh4794b982000-06-06 13:54:14 +00001020 if( pExpr->pLeft
danielk19774adee202004-05-08 08:23:19 +00001021 && sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pLeft) ){
drhcce7d172000-05-31 15:34:51 +00001022 return 1;
1023 }
1024 if( pExpr->pRight
danielk19774adee202004-05-08 08:23:19 +00001025 && sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pRight) ){
drhcce7d172000-05-31 15:34:51 +00001026 return 1;
1027 }
1028 if( pExpr->pList ){
1029 int i;
1030 ExprList *pList = pExpr->pList;
1031 for(i=0; i<pList->nExpr; i++){
drh832508b2002-03-02 17:04:07 +00001032 Expr *pArg = pList->a[i].pExpr;
danielk19774adee202004-05-08 08:23:19 +00001033 if( sqlite3ExprResolveIds(pParse, pSrcList, pEList, pArg) ){
drhcce7d172000-05-31 15:34:51 +00001034 return 1;
1035 }
1036 }
1037 }
1038 }
1039 }
1040 return 0;
1041}
1042
drhcce7d172000-05-31 15:34:51 +00001043/*
drh4b59ab52002-08-24 18:24:51 +00001044** pExpr is a node that defines a function of some kind. It might
1045** be a syntactic function like "count(x)" or it might be a function
1046** that implements an operator, like "a LIKE b".
1047**
1048** This routine makes *pzName point to the name of the function and
1049** *pnName hold the number of characters in the function name.
1050*/
1051static void getFunctionName(Expr *pExpr, const char **pzName, int *pnName){
1052 switch( pExpr->op ){
1053 case TK_FUNCTION: {
1054 *pzName = pExpr->token.z;
drh6977fea2002-10-22 23:38:04 +00001055 *pnName = pExpr->token.n;
drh4b59ab52002-08-24 18:24:51 +00001056 break;
1057 }
1058 case TK_LIKE: {
1059 *pzName = "like";
1060 *pnName = 4;
1061 break;
1062 }
1063 case TK_GLOB: {
1064 *pzName = "glob";
1065 *pnName = 4;
1066 break;
1067 }
1068 default: {
1069 *pzName = "can't happen";
1070 *pnName = 12;
1071 break;
1072 }
1073 }
1074}
1075
1076/*
drhcce7d172000-05-31 15:34:51 +00001077** Error check the functions in an expression. Make sure all
1078** function names are recognized and all functions have the correct
1079** number of arguments. Leave an error message in pParse->zErrMsg
1080** if anything is amiss. Return the number of errors.
1081**
1082** if pIsAgg is not null and this expression is an aggregate function
1083** (like count(*) or max(value)) then write a 1 into *pIsAgg.
1084*/
danielk19774adee202004-05-08 08:23:19 +00001085int sqlite3ExprCheck(Parse *pParse, Expr *pExpr, int allowAgg, int *pIsAgg){
drhcce7d172000-05-31 15:34:51 +00001086 int nErr = 0;
1087 if( pExpr==0 ) return 0;
drhcce7d172000-05-31 15:34:51 +00001088 switch( pExpr->op ){
drh4b59ab52002-08-24 18:24:51 +00001089 case TK_GLOB:
1090 case TK_LIKE:
drhcce7d172000-05-31 15:34:51 +00001091 case TK_FUNCTION: {
drhc9b84a12002-06-20 11:36:48 +00001092 int n = pExpr->pList ? pExpr->pList->nExpr : 0; /* Number of arguments */
1093 int no_such_func = 0; /* True if no such function exists */
drhc9b84a12002-06-20 11:36:48 +00001094 int wrong_num_args = 0; /* True if wrong number of arguments */
1095 int is_agg = 0; /* True if is an aggregate function */
drhcce7d172000-05-31 15:34:51 +00001096 int i;
drh4b59ab52002-08-24 18:24:51 +00001097 int nId; /* Number of characters in function name */
1098 const char *zId; /* The function name. */
drh0bce8352002-02-28 00:41:10 +00001099 FuncDef *pDef;
danielk1977d8123362004-06-12 09:25:12 +00001100 int enc = pParse->db->enc;
drh0bce8352002-02-28 00:41:10 +00001101
drh4b59ab52002-08-24 18:24:51 +00001102 getFunctionName(pExpr, &zId, &nId);
danielk1977d8123362004-06-12 09:25:12 +00001103 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001104 if( pDef==0 ){
danielk1977d8123362004-06-12 09:25:12 +00001105 pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001106 if( pDef==0 ){
drh268380c2004-02-25 13:47:31 +00001107 no_such_func = 1;
drh0bce8352002-02-28 00:41:10 +00001108 }else{
1109 wrong_num_args = 1;
drhcce7d172000-05-31 15:34:51 +00001110 }
drh0bce8352002-02-28 00:41:10 +00001111 }else{
1112 is_agg = pDef->xFunc==0;
drhcce7d172000-05-31 15:34:51 +00001113 }
drh8e0a2f92002-02-23 23:45:45 +00001114 if( is_agg && !allowAgg ){
danielk19774adee202004-05-08 08:23:19 +00001115 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId, zId);
drh8e0a2f92002-02-23 23:45:45 +00001116 nErr++;
1117 is_agg = 0;
1118 }else if( no_such_func ){
danielk19774adee202004-05-08 08:23:19 +00001119 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
drhcce7d172000-05-31 15:34:51 +00001120 nErr++;
drh8e0a2f92002-02-23 23:45:45 +00001121 }else if( wrong_num_args ){
danielk19774adee202004-05-08 08:23:19 +00001122 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
drhf7a9e1a2004-02-22 18:40:56 +00001123 nId, zId);
drh8e0a2f92002-02-23 23:45:45 +00001124 nErr++;
drhcce7d172000-05-31 15:34:51 +00001125 }
drhf7a9e1a2004-02-22 18:40:56 +00001126 if( is_agg ){
1127 pExpr->op = TK_AGG_FUNCTION;
1128 if( pIsAgg ) *pIsAgg = 1;
1129 }
drhcce7d172000-05-31 15:34:51 +00001130 for(i=0; nErr==0 && i<n; i++){
danielk19774adee202004-05-08 08:23:19 +00001131 nErr = sqlite3ExprCheck(pParse, pExpr->pList->a[i].pExpr,
drh4cfa7932000-06-08 15:10:46 +00001132 allowAgg && !is_agg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +00001133 }
danielk19770202b292004-06-09 09:55:16 +00001134 /* FIX ME: Compute pExpr->affinity based on the expected return
1135 ** type of the function
1136 */
drhcce7d172000-05-31 15:34:51 +00001137 }
1138 default: {
1139 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001140 nErr = sqlite3ExprCheck(pParse, pExpr->pLeft, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +00001141 }
1142 if( nErr==0 && pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001143 nErr = sqlite3ExprCheck(pParse, pExpr->pRight, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +00001144 }
drhfef52082000-06-06 01:50:43 +00001145 if( nErr==0 && pExpr->pList ){
1146 int n = pExpr->pList->nExpr;
1147 int i;
1148 for(i=0; nErr==0 && i<n; i++){
drh22827922000-06-06 17:27:05 +00001149 Expr *pE2 = pExpr->pList->a[i].pExpr;
danielk19774adee202004-05-08 08:23:19 +00001150 nErr = sqlite3ExprCheck(pParse, pE2, allowAgg, pIsAgg);
drhfef52082000-06-06 01:50:43 +00001151 }
1152 }
drhcce7d172000-05-31 15:34:51 +00001153 break;
1154 }
1155 }
1156 return nErr;
1157}
1158
1159/*
drh290c1942004-08-21 17:54:45 +00001160** Call sqlite3ExprResolveIds() followed by sqlite3ExprCheck().
1161**
1162** This routine is provided as a convenience since it is very common
1163** to call ResolveIds() and Check() back to back.
1164*/
1165int sqlite3ExprResolveAndCheck(
1166 Parse *pParse, /* The parser context */
1167 SrcList *pSrcList, /* List of tables used to resolve column names */
1168 ExprList *pEList, /* List of expressions used to resolve "AS" */
1169 Expr *pExpr, /* The expression to be analyzed. */
1170 int allowAgg, /* True to allow aggregate expressions */
1171 int *pIsAgg /* Set to TRUE if aggregates are found */
1172){
1173 if( pExpr==0 ) return 0;
1174 if( sqlite3ExprResolveIds(pParse,pSrcList,pEList,pExpr) ){
1175 return 1;
1176 }
1177 return sqlite3ExprCheck(pParse, pExpr, allowAgg, pIsAgg);
1178}
1179
1180/*
drhfec19aa2004-05-19 20:41:03 +00001181** Generate an instruction that will put the integer describe by
1182** text z[0..n-1] on the stack.
1183*/
1184static void codeInteger(Vdbe *v, const char *z, int n){
1185 int i;
drh6fec0762004-05-30 01:38:43 +00001186 if( sqlite3GetInt32(z, &i) ){
1187 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
1188 }else if( sqlite3FitsIn64Bits(z) ){
1189 sqlite3VdbeOp3(v, OP_Integer, 0, 0, z, n);
drhfec19aa2004-05-19 20:41:03 +00001190 }else{
1191 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n);
1192 }
1193}
1194
1195/*
drhcce7d172000-05-31 15:34:51 +00001196** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +00001197** expression and leave the result on the top of stack.
drhcce7d172000-05-31 15:34:51 +00001198*/
danielk19774adee202004-05-08 08:23:19 +00001199void sqlite3ExprCode(Parse *pParse, Expr *pExpr){
drhcce7d172000-05-31 15:34:51 +00001200 Vdbe *v = pParse->pVdbe;
1201 int op;
drhdaffd0e2001-04-11 14:28:42 +00001202 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001203 switch( pExpr->op ){
1204 case TK_PLUS: op = OP_Add; break;
1205 case TK_MINUS: op = OP_Subtract; break;
1206 case TK_STAR: op = OP_Multiply; break;
1207 case TK_SLASH: op = OP_Divide; break;
1208 case TK_AND: op = OP_And; break;
1209 case TK_OR: op = OP_Or; break;
1210 case TK_LT: op = OP_Lt; break;
1211 case TK_LE: op = OP_Le; break;
1212 case TK_GT: op = OP_Gt; break;
1213 case TK_GE: op = OP_Ge; break;
1214 case TK_NE: op = OP_Ne; break;
1215 case TK_EQ: op = OP_Eq; break;
drhcce7d172000-05-31 15:34:51 +00001216 case TK_ISNULL: op = OP_IsNull; break;
1217 case TK_NOTNULL: op = OP_NotNull; break;
1218 case TK_NOT: op = OP_Not; break;
1219 case TK_UMINUS: op = OP_Negative; break;
drhbf4133c2001-10-13 02:59:08 +00001220 case TK_BITAND: op = OP_BitAnd; break;
1221 case TK_BITOR: op = OP_BitOr; break;
1222 case TK_BITNOT: op = OP_BitNot; break;
1223 case TK_LSHIFT: op = OP_ShiftLeft; break;
1224 case TK_RSHIFT: op = OP_ShiftRight; break;
1225 case TK_REM: op = OP_Remainder; break;
drhfec19aa2004-05-19 20:41:03 +00001226 case TK_FLOAT: op = OP_Real; break;
drh855eb1c2004-08-31 13:45:11 +00001227 case TK_STRING: op = OP_String8; break;
danielk1977c572ef72004-05-27 09:28:41 +00001228 case TK_BLOB: op = OP_HexBlob; break;
drh855eb1c2004-08-31 13:45:11 +00001229 case TK_CONCAT: op = OP_Concat; break;
danielk1977cfe9a692004-06-16 12:00:29 +00001230 default: op = 0; break;
drhcce7d172000-05-31 15:34:51 +00001231 }
1232 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00001233 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +00001234 if( pParse->useAgg ){
danielk19774adee202004-05-08 08:23:19 +00001235 sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drhc4a3c772001-04-04 11:48:57 +00001236 }else if( pExpr->iColumn>=0 ){
danielk19774adee202004-05-08 08:23:19 +00001237 sqlite3VdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn);
drh145716b2004-09-24 12:24:06 +00001238#ifndef NDEBUG
1239 if( pExpr->span.z && pExpr->span.n>0 && pExpr->span.n<100 ){
1240 VdbeComment((v, "# %T", &pExpr->span));
1241 }
1242#endif
drhc4a3c772001-04-04 11:48:57 +00001243 }else{
danielk19774adee202004-05-08 08:23:19 +00001244 sqlite3VdbeAddOp(v, OP_Recno, pExpr->iTable, 0);
drh22827922000-06-06 17:27:05 +00001245 }
drhcce7d172000-05-31 15:34:51 +00001246 break;
1247 }
1248 case TK_INTEGER: {
drhfec19aa2004-05-19 20:41:03 +00001249 codeInteger(v, pExpr->token.z, pExpr->token.n);
1250 break;
1251 }
1252 case TK_FLOAT:
1253 case TK_STRING: {
1254 sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z, pExpr->token.n);
danielk19774adee202004-05-08 08:23:19 +00001255 sqlite3VdbeDequoteP3(v, -1);
drhcce7d172000-05-31 15:34:51 +00001256 break;
1257 }
danielk1977c572ef72004-05-27 09:28:41 +00001258 case TK_BLOB: {
1259 sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z+1, pExpr->token.n-1);
1260 sqlite3VdbeDequoteP3(v, -1);
1261 break;
1262 }
drhcce7d172000-05-31 15:34:51 +00001263 case TK_NULL: {
danielk19770f69c1e2004-05-29 11:24:50 +00001264 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001265 break;
1266 }
drh50457892003-09-06 01:10:47 +00001267 case TK_VARIABLE: {
danielk19774adee202004-05-08 08:23:19 +00001268 sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0);
drh895d7472004-08-20 16:02:39 +00001269 if( pExpr->token.n>1 ){
1270 sqlite3VdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
1271 }
drh50457892003-09-06 01:10:47 +00001272 break;
1273 }
drhc9b84a12002-06-20 11:36:48 +00001274 case TK_LT:
1275 case TK_LE:
1276 case TK_GT:
1277 case TK_GE:
1278 case TK_NE:
1279 case TK_EQ: {
danielk1977a37cdde2004-05-16 11:15:36 +00001280 sqlite3ExprCode(pParse, pExpr->pLeft);
1281 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001282 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 0, 0);
danielk1977a37cdde2004-05-16 11:15:36 +00001283 break;
drhc9b84a12002-06-20 11:36:48 +00001284 }
drhcce7d172000-05-31 15:34:51 +00001285 case TK_AND:
1286 case TK_OR:
1287 case TK_PLUS:
1288 case TK_STAR:
1289 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00001290 case TK_REM:
1291 case TK_BITAND:
1292 case TK_BITOR:
drh17c40292004-07-21 02:53:29 +00001293 case TK_SLASH:
drhbf4133c2001-10-13 02:59:08 +00001294 case TK_LSHIFT:
drh855eb1c2004-08-31 13:45:11 +00001295 case TK_RSHIFT:
drh00400772000-06-16 20:51:26 +00001296 case TK_CONCAT: {
danielk19774adee202004-05-08 08:23:19 +00001297 sqlite3ExprCode(pParse, pExpr->pLeft);
1298 sqlite3ExprCode(pParse, pExpr->pRight);
drh855eb1c2004-08-31 13:45:11 +00001299 sqlite3VdbeAddOp(v, op, 0, 0);
drh00400772000-06-16 20:51:26 +00001300 break;
1301 }
drhcce7d172000-05-31 15:34:51 +00001302 case TK_UMINUS: {
drhfec19aa2004-05-19 20:41:03 +00001303 Expr *pLeft = pExpr->pLeft;
1304 assert( pLeft );
1305 if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
1306 Token *p = &pLeft->token;
drh6e142f52000-06-08 13:36:40 +00001307 char *z = sqliteMalloc( p->n + 2 );
1308 sprintf(z, "-%.*s", p->n, p->z);
drhfec19aa2004-05-19 20:41:03 +00001309 if( pLeft->op==TK_FLOAT ){
1310 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001311 }else{
drhfec19aa2004-05-19 20:41:03 +00001312 codeInteger(v, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001313 }
drh6e142f52000-06-08 13:36:40 +00001314 sqliteFree(z);
1315 break;
1316 }
drh1ccde152000-06-17 13:12:39 +00001317 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +00001318 }
drhbf4133c2001-10-13 02:59:08 +00001319 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00001320 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001321 sqlite3ExprCode(pParse, pExpr->pLeft);
1322 sqlite3VdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001323 break;
1324 }
1325 case TK_ISNULL:
1326 case TK_NOTNULL: {
1327 int dest;
danielk19774adee202004-05-08 08:23:19 +00001328 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1329 sqlite3ExprCode(pParse, pExpr->pLeft);
1330 dest = sqlite3VdbeCurrentAddr(v) + 2;
1331 sqlite3VdbeAddOp(v, op, 1, dest);
1332 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);
drhcce7d172000-05-31 15:34:51 +00001333 }
danielk1977a37cdde2004-05-16 11:15:36 +00001334 break;
drh22827922000-06-06 17:27:05 +00001335 case TK_AGG_FUNCTION: {
danielk19774adee202004-05-08 08:23:19 +00001336 sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drh22827922000-06-06 17:27:05 +00001337 break;
1338 }
drh4b59ab52002-08-24 18:24:51 +00001339 case TK_GLOB:
1340 case TK_LIKE:
drhcce7d172000-05-31 15:34:51 +00001341 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00001342 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00001343 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00001344 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00001345 int nId;
1346 const char *zId;
danielk1977682f68b2004-06-05 10:22:17 +00001347 int p2 = 0;
1348 int i;
danielk1977d8123362004-06-12 09:25:12 +00001349 u8 enc = pParse->db->enc;
danielk1977dc1bdc42004-06-11 10:51:27 +00001350 CollSeq *pColl = 0;
drh4b59ab52002-08-24 18:24:51 +00001351 getFunctionName(pExpr, &zId, &nId);
danielk1977d8123362004-06-12 09:25:12 +00001352 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001353 assert( pDef!=0 );
drhf9b596e2004-05-26 16:54:42 +00001354 nExpr = sqlite3ExprCodeExprList(pParse, pList);
danielk1977682f68b2004-06-05 10:22:17 +00001355 for(i=0; i<nExpr && i<32; i++){
danielk1977d02eb1f2004-06-06 09:44:03 +00001356 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
1357 p2 |= (1<<i);
1358 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001359 if( pDef->needCollSeq && !pColl ){
1360 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
1361 }
1362 }
1363 if( pDef->needCollSeq ){
1364 if( !pColl ) pColl = pParse->db->pDfltColl;
danielk1977d8123362004-06-12 09:25:12 +00001365 sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ);
danielk1977682f68b2004-06-05 10:22:17 +00001366 }
1367 sqlite3VdbeOp3(v, OP_Function, nExpr, p2, (char*)pDef, P3_FUNCDEF);
drhcce7d172000-05-31 15:34:51 +00001368 break;
1369 }
drh19a775c2000-06-05 18:54:46 +00001370 case TK_SELECT: {
danielk19774adee202004-05-08 08:23:19 +00001371 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drhad6d9462004-09-19 02:15:24 +00001372 VdbeComment((v, "# load subquery result"));
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 return;
1473 }
drhad6d9462004-09-19 02:15:24 +00001474 if( pExpr->iColumn!=OE_Ignore ){
1475 assert( pExpr->iColumn==OE_Rollback ||
1476 pExpr->iColumn == OE_Abort ||
1477 pExpr->iColumn == OE_Fail );
1478 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn,
1479 pExpr->token.z, pExpr->token.n);
1480 sqlite3VdbeDequoteP3(v, -1);
danielk19776f349032002-06-11 02:25:40 +00001481 } else {
drhad6d9462004-09-19 02:15:24 +00001482 assert( pExpr->iColumn == OE_Ignore );
1483 sqlite3VdbeAddOp(v, OP_ContextPop, 0, 0);
1484 sqlite3VdbeAddOp(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
1485 VdbeComment((v, "# raise(IGNORE)"));
danielk19776f349032002-06-11 02:25:40 +00001486 }
drh17a7f8d2002-03-24 13:13:27 +00001487 }
1488 break;
drhcce7d172000-05-31 15:34:51 +00001489 }
drhcce7d172000-05-31 15:34:51 +00001490}
1491
1492/*
drh268380c2004-02-25 13:47:31 +00001493** Generate code that pushes the value of every element of the given
drhf9b596e2004-05-26 16:54:42 +00001494** expression list onto the stack.
drh268380c2004-02-25 13:47:31 +00001495**
1496** Return the number of elements pushed onto the stack.
1497*/
danielk19774adee202004-05-08 08:23:19 +00001498int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00001499 Parse *pParse, /* Parsing context */
drhf9b596e2004-05-26 16:54:42 +00001500 ExprList *pList /* The expression list to be coded */
drh268380c2004-02-25 13:47:31 +00001501){
1502 struct ExprList_item *pItem;
1503 int i, n;
1504 Vdbe *v;
1505 if( pList==0 ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001506 v = sqlite3GetVdbe(pParse);
drh268380c2004-02-25 13:47:31 +00001507 n = pList->nExpr;
1508 for(pItem=pList->a, i=0; i<n; i++, pItem++){
danielk19774adee202004-05-08 08:23:19 +00001509 sqlite3ExprCode(pParse, pItem->pExpr);
drh268380c2004-02-25 13:47:31 +00001510 }
drhf9b596e2004-05-26 16:54:42 +00001511 return n;
drh268380c2004-02-25 13:47:31 +00001512}
1513
1514/*
drhcce7d172000-05-31 15:34:51 +00001515** Generate code for a boolean expression such that a jump is made
1516** to the label "dest" if the expression is true but execution
1517** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00001518**
1519** If the expression evaluates to NULL (neither true nor false), then
1520** take the jump if the jumpIfNull flag is true.
drhcce7d172000-05-31 15:34:51 +00001521*/
danielk19774adee202004-05-08 08:23:19 +00001522void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001523 Vdbe *v = pParse->pVdbe;
1524 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001525 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001526 switch( pExpr->op ){
1527 case TK_LT: op = OP_Lt; break;
1528 case TK_LE: op = OP_Le; break;
1529 case TK_GT: op = OP_Gt; break;
1530 case TK_GE: op = OP_Ge; break;
1531 case TK_NE: op = OP_Ne; break;
1532 case TK_EQ: op = OP_Eq; break;
drhcce7d172000-05-31 15:34:51 +00001533 case TK_ISNULL: op = OP_IsNull; break;
1534 case TK_NOTNULL: op = OP_NotNull; break;
1535 default: break;
1536 }
1537 switch( pExpr->op ){
1538 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001539 int d2 = sqlite3VdbeMakeLabel(v);
1540 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
1541 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
1542 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001543 break;
1544 }
1545 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001546 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
1547 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001548 break;
1549 }
1550 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001551 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001552 break;
1553 }
1554 case TK_LT:
1555 case TK_LE:
1556 case TK_GT:
1557 case TK_GE:
1558 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00001559 case TK_EQ: {
danielk19774adee202004-05-08 08:23:19 +00001560 sqlite3ExprCode(pParse, pExpr->pLeft);
1561 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001562 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001563 break;
1564 }
1565 case TK_ISNULL:
1566 case TK_NOTNULL: {
danielk19774adee202004-05-08 08:23:19 +00001567 sqlite3ExprCode(pParse, pExpr->pLeft);
1568 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001569 break;
1570 }
drhfef52082000-06-06 01:50:43 +00001571 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00001572 /* The expression "x BETWEEN y AND z" is implemented as:
1573 **
1574 ** 1 IF (x < y) GOTO 3
1575 ** 2 IF (x <= z) GOTO <dest>
1576 ** 3 ...
1577 */
drhf5905aa2002-05-26 20:54:33 +00001578 int addr;
drhbe5c89a2004-07-26 00:31:09 +00001579 Expr *pLeft = pExpr->pLeft;
1580 Expr *pRight = pExpr->pList->a[0].pExpr;
1581 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001582 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001583 sqlite3ExprCode(pParse, pRight);
1584 addr = codeCompare(pParse, pLeft, pRight, OP_Lt, 0, !jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00001585
drhbe5c89a2004-07-26 00:31:09 +00001586 pRight = pExpr->pList->a[1].pExpr;
1587 sqlite3ExprCode(pParse, pRight);
1588 codeCompare(pParse, pLeft, pRight, OP_Le, dest, jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00001589
danielk19774adee202004-05-08 08:23:19 +00001590 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
1591 sqlite3VdbeChangeP2(v, addr, sqlite3VdbeCurrentAddr(v));
1592 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00001593 break;
1594 }
drhcce7d172000-05-31 15:34:51 +00001595 default: {
danielk19774adee202004-05-08 08:23:19 +00001596 sqlite3ExprCode(pParse, pExpr);
1597 sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001598 break;
1599 }
1600 }
1601}
1602
1603/*
drh66b89c82000-11-28 20:47:17 +00001604** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00001605** to the label "dest" if the expression is false but execution
1606** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00001607**
1608** If the expression evaluates to NULL (neither true nor false) then
1609** jump if jumpIfNull is true or fall through if jumpIfNull is false.
drhcce7d172000-05-31 15:34:51 +00001610*/
danielk19774adee202004-05-08 08:23:19 +00001611void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001612 Vdbe *v = pParse->pVdbe;
1613 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001614 if( v==0 || pExpr==0 ) return;
drhcce7d172000-05-31 15:34:51 +00001615 switch( pExpr->op ){
1616 case TK_LT: op = OP_Ge; break;
1617 case TK_LE: op = OP_Gt; break;
1618 case TK_GT: op = OP_Le; break;
1619 case TK_GE: op = OP_Lt; break;
1620 case TK_NE: op = OP_Eq; break;
1621 case TK_EQ: op = OP_Ne; break;
drhcce7d172000-05-31 15:34:51 +00001622 case TK_ISNULL: op = OP_NotNull; break;
1623 case TK_NOTNULL: op = OP_IsNull; break;
1624 default: break;
1625 }
1626 switch( pExpr->op ){
1627 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001628 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
1629 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001630 break;
1631 }
1632 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001633 int d2 = sqlite3VdbeMakeLabel(v);
1634 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
1635 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
1636 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001637 break;
1638 }
1639 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001640 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001641 break;
1642 }
1643 case TK_LT:
1644 case TK_LE:
1645 case TK_GT:
1646 case TK_GE:
1647 case TK_NE:
1648 case TK_EQ: {
danielk19774adee202004-05-08 08:23:19 +00001649 sqlite3ExprCode(pParse, pExpr->pLeft);
1650 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001651 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001652 break;
1653 }
drhcce7d172000-05-31 15:34:51 +00001654 case TK_ISNULL:
1655 case TK_NOTNULL: {
danielk19774adee202004-05-08 08:23:19 +00001656 sqlite3ExprCode(pParse, pExpr->pLeft);
1657 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001658 break;
1659 }
drhfef52082000-06-06 01:50:43 +00001660 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00001661 /* The expression is "x BETWEEN y AND z". It is implemented as:
1662 **
1663 ** 1 IF (x >= y) GOTO 3
1664 ** 2 GOTO <dest>
1665 ** 3 IF (x > z) GOTO <dest>
1666 */
drhfef52082000-06-06 01:50:43 +00001667 int addr;
drhbe5c89a2004-07-26 00:31:09 +00001668 Expr *pLeft = pExpr->pLeft;
1669 Expr *pRight = pExpr->pList->a[0].pExpr;
1670 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001671 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001672 sqlite3ExprCode(pParse, pRight);
danielk19774adee202004-05-08 08:23:19 +00001673 addr = sqlite3VdbeCurrentAddr(v);
drhbe5c89a2004-07-26 00:31:09 +00001674 codeCompare(pParse, pLeft, pRight, OP_Ge, addr+3, !jumpIfNull);
1675
danielk19774adee202004-05-08 08:23:19 +00001676 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
1677 sqlite3VdbeAddOp(v, OP_Goto, 0, dest);
drhbe5c89a2004-07-26 00:31:09 +00001678 pRight = pExpr->pList->a[1].pExpr;
1679 sqlite3ExprCode(pParse, pRight);
1680 codeCompare(pParse, pLeft, pRight, OP_Gt, dest, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00001681 break;
1682 }
drhcce7d172000-05-31 15:34:51 +00001683 default: {
danielk19774adee202004-05-08 08:23:19 +00001684 sqlite3ExprCode(pParse, pExpr);
1685 sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001686 break;
1687 }
1688 }
1689}
drh22827922000-06-06 17:27:05 +00001690
1691/*
1692** Do a deep comparison of two expression trees. Return TRUE (non-zero)
1693** if they are identical and return FALSE if they differ in any way.
1694*/
danielk19774adee202004-05-08 08:23:19 +00001695int sqlite3ExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00001696 int i;
1697 if( pA==0 ){
1698 return pB==0;
1699 }else if( pB==0 ){
1700 return 0;
1701 }
1702 if( pA->op!=pB->op ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001703 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
1704 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00001705 if( pA->pList ){
1706 if( pB->pList==0 ) return 0;
1707 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
1708 for(i=0; i<pA->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00001709 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00001710 return 0;
1711 }
1712 }
1713 }else if( pB->pList ){
1714 return 0;
1715 }
1716 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00001717 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drh22827922000-06-06 17:27:05 +00001718 if( pA->token.z ){
1719 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00001720 if( pB->token.n!=pA->token.n ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001721 if( sqlite3StrNICmp(pA->token.z, pB->token.z, pB->token.n)!=0 ) return 0;
drh22827922000-06-06 17:27:05 +00001722 }
1723 return 1;
1724}
1725
1726/*
1727** Add a new element to the pParse->aAgg[] array and return its index.
1728*/
1729static int appendAggInfo(Parse *pParse){
1730 if( (pParse->nAgg & 0x7)==0 ){
1731 int amt = pParse->nAgg + 8;
drh6d4abfb2001-10-22 02:58:08 +00001732 AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0]));
1733 if( aAgg==0 ){
drh22827922000-06-06 17:27:05 +00001734 return -1;
1735 }
drh6d4abfb2001-10-22 02:58:08 +00001736 pParse->aAgg = aAgg;
drh22827922000-06-06 17:27:05 +00001737 }
1738 memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0]));
1739 return pParse->nAgg++;
1740}
1741
1742/*
1743** Analyze the given expression looking for aggregate functions and
1744** for variables that need to be added to the pParse->aAgg[] array.
1745** Make additional entries to the pParse->aAgg[] array as necessary.
1746**
1747** This routine should only be called after the expression has been
danielk19774adee202004-05-08 08:23:19 +00001748** analyzed by sqlite3ExprResolveIds() and sqlite3ExprCheck().
drh22827922000-06-06 17:27:05 +00001749**
1750** If errors are seen, leave an error message in zErrMsg and return
1751** the number of errors.
1752*/
danielk19774adee202004-05-08 08:23:19 +00001753int sqlite3ExprAnalyzeAggregates(Parse *pParse, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00001754 int i;
1755 AggExpr *aAgg;
1756 int nErr = 0;
1757
1758 if( pExpr==0 ) return 0;
1759 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00001760 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +00001761 aAgg = pParse->aAgg;
1762 for(i=0; i<pParse->nAgg; i++){
1763 if( aAgg[i].isAgg ) continue;
1764 if( aAgg[i].pExpr->iTable==pExpr->iTable
drh967e8b72000-06-21 13:59:10 +00001765 && aAgg[i].pExpr->iColumn==pExpr->iColumn ){
drh22827922000-06-06 17:27:05 +00001766 break;
1767 }
1768 }
1769 if( i>=pParse->nAgg ){
1770 i = appendAggInfo(pParse);
1771 if( i<0 ) return 1;
1772 pParse->aAgg[i].isAgg = 0;
1773 pParse->aAgg[i].pExpr = pExpr;
1774 }
drhaaf88722000-06-08 11:25:00 +00001775 pExpr->iAgg = i;
drh22827922000-06-06 17:27:05 +00001776 break;
1777 }
1778 case TK_AGG_FUNCTION: {
drh22827922000-06-06 17:27:05 +00001779 aAgg = pParse->aAgg;
1780 for(i=0; i<pParse->nAgg; i++){
1781 if( !aAgg[i].isAgg ) continue;
danielk19774adee202004-05-08 08:23:19 +00001782 if( sqlite3ExprCompare(aAgg[i].pExpr, pExpr) ){
drh22827922000-06-06 17:27:05 +00001783 break;
1784 }
1785 }
1786 if( i>=pParse->nAgg ){
danielk1977d8123362004-06-12 09:25:12 +00001787 u8 enc = pParse->db->enc;
drh22827922000-06-06 17:27:05 +00001788 i = appendAggInfo(pParse);
1789 if( i<0 ) return 1;
1790 pParse->aAgg[i].isAgg = 1;
1791 pParse->aAgg[i].pExpr = pExpr;
danielk19774adee202004-05-08 08:23:19 +00001792 pParse->aAgg[i].pFunc = sqlite3FindFunction(pParse->db,
drh6977fea2002-10-22 23:38:04 +00001793 pExpr->token.z, pExpr->token.n,
danielk1977d8123362004-06-12 09:25:12 +00001794 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
drh22827922000-06-06 17:27:05 +00001795 }
1796 pExpr->iAgg = i;
1797 break;
1798 }
1799 default: {
1800 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001801 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pLeft);
drh22827922000-06-06 17:27:05 +00001802 }
1803 if( nErr==0 && pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001804 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pRight);
drh22827922000-06-06 17:27:05 +00001805 }
1806 if( nErr==0 && pExpr->pList ){
1807 int n = pExpr->pList->nExpr;
1808 int i;
1809 for(i=0; nErr==0 && i<n; i++){
danielk19774adee202004-05-08 08:23:19 +00001810 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pList->a[i].pExpr);
drh22827922000-06-06 17:27:05 +00001811 }
1812 }
1813 break;
1814 }
1815 }
1816 return nErr;
1817}
drh8e0a2f92002-02-23 23:45:45 +00001818
1819/*
danielk1977d02eb1f2004-06-06 09:44:03 +00001820** Locate a user function given a name, a number of arguments and a flag
1821** indicating whether the function prefers UTF-16 over UTF-8. Return a
1822** pointer to the FuncDef structure that defines that function, or return
1823** NULL if the function does not exist.
drh8e0a2f92002-02-23 23:45:45 +00001824**
drh0bce8352002-02-28 00:41:10 +00001825** If the createFlag argument is true, then a new (blank) FuncDef
drh8e0a2f92002-02-23 23:45:45 +00001826** structure is created and liked into the "db" structure if a
1827** no matching function previously existed. When createFlag is true
1828** and the nArg parameter is -1, then only a function that accepts
1829** any number of arguments will be returned.
1830**
1831** If createFlag is false and nArg is -1, then the first valid
1832** function found is returned. A function is valid if either xFunc
1833** or xStep is non-zero.
danielk1977d02eb1f2004-06-06 09:44:03 +00001834**
1835** If createFlag is false, then a function with the required name and
1836** number of arguments may be returned even if the eTextRep flag does not
1837** match that requested.
drh8e0a2f92002-02-23 23:45:45 +00001838*/
danielk19774adee202004-05-08 08:23:19 +00001839FuncDef *sqlite3FindFunction(
drh9bb575f2004-09-06 17:24:11 +00001840 sqlite3 *db, /* An open database */
drh8e0a2f92002-02-23 23:45:45 +00001841 const char *zName, /* Name of the function. Not null-terminated */
1842 int nName, /* Number of characters in the name */
1843 int nArg, /* Number of arguments. -1 means any number */
danielk1977d8123362004-06-12 09:25:12 +00001844 u8 enc, /* Preferred text encoding */
drh8e0a2f92002-02-23 23:45:45 +00001845 int createFlag /* Create new entry if true and does not otherwise exist */
1846){
danielk1977d02eb1f2004-06-06 09:44:03 +00001847 FuncDef *p; /* Iterator variable */
1848 FuncDef *pFirst; /* First function with this name */
1849 FuncDef *pBest = 0; /* Best match found so far */
danielk1977d8123362004-06-12 09:25:12 +00001850 int bestmatch = 0;
danielk1977d02eb1f2004-06-06 09:44:03 +00001851
danielk1977d8123362004-06-12 09:25:12 +00001852
1853 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
danielk1977d02eb1f2004-06-06 09:44:03 +00001854 if( nArg<-1 ) nArg = -1;
1855
1856 pFirst = (FuncDef*)sqlite3HashFind(&db->aFunc, zName, nName);
1857 for(p=pFirst; p; p=p->pNext){
danielk1977d8123362004-06-12 09:25:12 +00001858 /* During the search for the best function definition, bestmatch is set
1859 ** as follows to indicate the quality of the match with the definition
1860 ** pointed to by pBest:
1861 **
1862 ** 0: pBest is NULL. No match has been found.
1863 ** 1: A variable arguments function that prefers UTF-8 when a UTF-16
1864 ** encoding is requested, or vice versa.
1865 ** 2: A variable arguments function that uses UTF-16BE when UTF-16LE is
1866 ** requested, or vice versa.
1867 ** 3: A variable arguments function using the same text encoding.
1868 ** 4: A function with the exact number of arguments requested that
1869 ** prefers UTF-8 when a UTF-16 encoding is requested, or vice versa.
1870 ** 5: A function with the exact number of arguments requested that
1871 ** prefers UTF-16LE when UTF-16BE is requested, or vice versa.
1872 ** 6: An exact match.
1873 **
1874 ** A larger value of 'matchqual' indicates a more desirable match.
1875 */
danielk1977e12c17b2004-06-23 12:35:14 +00001876 if( p->nArg==-1 || p->nArg==nArg || nArg==-1 ){
danielk1977d8123362004-06-12 09:25:12 +00001877 int match = 1; /* Quality of this match */
1878 if( p->nArg==nArg || nArg==-1 ){
1879 match = 4;
danielk1977d02eb1f2004-06-06 09:44:03 +00001880 }
danielk1977d8123362004-06-12 09:25:12 +00001881 if( enc==p->iPrefEnc ){
1882 match += 2;
danielk1977d02eb1f2004-06-06 09:44:03 +00001883 }
danielk1977d8123362004-06-12 09:25:12 +00001884 else if( (enc==SQLITE_UTF16LE && p->iPrefEnc==SQLITE_UTF16BE) ||
1885 (enc==SQLITE_UTF16BE && p->iPrefEnc==SQLITE_UTF16LE) ){
1886 match += 1;
1887 }
1888
1889 if( match>bestmatch ){
1890 pBest = p;
1891 bestmatch = match;
danielk1977d02eb1f2004-06-06 09:44:03 +00001892 }
1893 }
drh8e0a2f92002-02-23 23:45:45 +00001894 }
danielk1977d02eb1f2004-06-06 09:44:03 +00001895
danielk1977d8123362004-06-12 09:25:12 +00001896 /* If the createFlag parameter is true, and the seach did not reveal an
1897 ** exact match for the name, number of arguments and encoding, then add a
1898 ** new entry to the hash table and return it.
1899 */
1900 if( createFlag && bestmatch<6 &&
danielk1977d02eb1f2004-06-06 09:44:03 +00001901 (pBest = sqliteMalloc(sizeof(*pBest)+nName+1)) ){
1902 pBest->nArg = nArg;
1903 pBest->pNext = pFirst;
1904 pBest->zName = (char*)&pBest[1];
danielk1977d8123362004-06-12 09:25:12 +00001905 pBest->iPrefEnc = enc;
danielk1977d02eb1f2004-06-06 09:44:03 +00001906 memcpy(pBest->zName, zName, nName);
1907 pBest->zName[nName] = 0;
1908 sqlite3HashInsert(&db->aFunc, pBest->zName, nName, (void*)pBest);
drh8e0a2f92002-02-23 23:45:45 +00001909 }
danielk1977d02eb1f2004-06-06 09:44:03 +00001910
1911 if( pBest && (pBest->xStep || pBest->xFunc || createFlag) ){
1912 return pBest;
drh8e0a2f92002-02-23 23:45:45 +00001913 }
danielk1977d02eb1f2004-06-06 09:44:03 +00001914 return 0;
drh8e0a2f92002-02-23 23:45:45 +00001915}