blob: 514566aaa1f678d4c553b76d7808f4cfdd6a9907 [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**
drh51522cd2005-01-20 13:36:19 +000015** $Id: expr.c,v 1.185 2005/01/20 13:36:20 drh Exp $
drhcce7d172000-05-31 15:34:51 +000016*/
17#include "sqliteInt.h"
drh04738cb2002-06-02 18:19:00 +000018#include <ctype.h>
drha2e00042002-01-22 03:13:42 +000019
danielk1977e014a832004-05-17 10:48:57 +000020/*
21** Return the 'affinity' of the expression pExpr if any.
22**
23** If pExpr is a column, a reference to a column via an 'AS' alias,
24** or a sub-select with a column as the return value, then the
25** affinity of that column is returned. Otherwise, 0x00 is returned,
26** indicating no affinity for the expression.
27**
28** i.e. the WHERE clause expresssions in the following statements all
29** have an affinity:
30**
31** CREATE TABLE t1(a);
32** SELECT * FROM t1 WHERE a;
33** SELECT a AS b FROM t1 WHERE b;
34** SELECT * FROM t1 WHERE (select a from t1);
35*/
danielk1977bf3b7212004-05-18 10:06:24 +000036char sqlite3ExprAffinity(Expr *pExpr){
danielk1977a37cdde2004-05-16 11:15:36 +000037 if( pExpr->op==TK_AS ){
danielk1977bf3b7212004-05-18 10:06:24 +000038 return sqlite3ExprAffinity(pExpr->pLeft);
danielk1977a37cdde2004-05-16 11:15:36 +000039 }
40 if( pExpr->op==TK_SELECT ){
danielk1977bf3b7212004-05-18 10:06:24 +000041 return sqlite3ExprAffinity(pExpr->pSelect->pEList->a[0].pExpr);
danielk1977a37cdde2004-05-16 11:15:36 +000042 }
43 return pExpr->affinity;
44}
45
drh53db1452004-05-20 13:54:53 +000046/*
danielk19770202b292004-06-09 09:55:16 +000047** Return the default collation sequence for the expression pExpr. If
48** there is no default collation type, return 0.
49*/
danielk19777cedc8d2004-06-10 10:50:08 +000050CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
51 CollSeq *pColl = 0;
danielk19770202b292004-06-09 09:55:16 +000052 if( pExpr ){
danielk19777cedc8d2004-06-10 10:50:08 +000053 pColl = pExpr->pColl;
54 if( pExpr->op==TK_AS && !pColl ){
55 return sqlite3ExprCollSeq(pParse, pExpr->pLeft);
danielk19770202b292004-06-09 09:55:16 +000056 }
57 }
danielk19777cedc8d2004-06-10 10:50:08 +000058 if( sqlite3CheckCollSeq(pParse, pColl) ){
59 pColl = 0;
60 }
61 return pColl;
danielk19770202b292004-06-09 09:55:16 +000062}
63
64/*
drh626a8792005-01-17 22:08:19 +000065** pExpr is an operand of a comparison operator. aff2 is the
66** type affinity of the other operand. This routine returns the
drh53db1452004-05-20 13:54:53 +000067** type affinity that should be used for the comparison operator.
68*/
danielk1977e014a832004-05-17 10:48:57 +000069char sqlite3CompareAffinity(Expr *pExpr, char aff2){
danielk1977bf3b7212004-05-18 10:06:24 +000070 char aff1 = sqlite3ExprAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +000071 if( aff1 && aff2 ){
72 /* Both sides of the comparison are columns. If one has numeric or
73 ** integer affinity, use that. Otherwise use no affinity.
74 */
75 if( aff1==SQLITE_AFF_INTEGER || aff2==SQLITE_AFF_INTEGER ){
76 return SQLITE_AFF_INTEGER;
77 }else if( aff1==SQLITE_AFF_NUMERIC || aff2==SQLITE_AFF_NUMERIC ){
78 return SQLITE_AFF_NUMERIC;
79 }else{
80 return SQLITE_AFF_NONE;
81 }
82 }else if( !aff1 && !aff2 ){
drh5f6a87b2004-07-19 00:39:45 +000083 /* Neither side of the comparison is a column. Compare the
84 ** results directly.
danielk1977e014a832004-05-17 10:48:57 +000085 */
drh5f6a87b2004-07-19 00:39:45 +000086 /* return SQLITE_AFF_NUMERIC; // Ticket #805 */
87 return SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +000088 }else{
89 /* One side is a column, the other is not. Use the columns affinity. */
90 return (aff1 + aff2);
91 }
92}
93
drh53db1452004-05-20 13:54:53 +000094/*
95** pExpr is a comparison operator. Return the type affinity that should
96** be applied to both operands prior to doing the comparison.
97*/
danielk1977e014a832004-05-17 10:48:57 +000098static char comparisonAffinity(Expr *pExpr){
99 char aff;
100 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
101 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
102 pExpr->op==TK_NE );
103 assert( pExpr->pLeft );
danielk1977bf3b7212004-05-18 10:06:24 +0000104 aff = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +0000105 if( pExpr->pRight ){
106 aff = sqlite3CompareAffinity(pExpr->pRight, aff);
107 }
108 else if( pExpr->pSelect ){
109 aff = sqlite3CompareAffinity(pExpr->pSelect->pEList->a[0].pExpr, aff);
110 }
111 else if( !aff ){
112 aff = SQLITE_AFF_NUMERIC;
113 }
114 return aff;
115}
116
117/*
118** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
119** idx_affinity is the affinity of an indexed column. Return true
120** if the index with affinity idx_affinity may be used to implement
121** the comparison in pExpr.
122*/
123int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){
124 char aff = comparisonAffinity(pExpr);
125 return
126 (aff==SQLITE_AFF_NONE) ||
127 (aff==SQLITE_AFF_NUMERIC && idx_affinity==SQLITE_AFF_INTEGER) ||
128 (aff==SQLITE_AFF_INTEGER && idx_affinity==SQLITE_AFF_NUMERIC) ||
129 (aff==idx_affinity);
130}
131
danielk1977a37cdde2004-05-16 11:15:36 +0000132/*
133** Return the P1 value that should be used for a binary comparison
134** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
135** If jumpIfNull is true, then set the low byte of the returned
136** P1 value to tell the opcode to jump if either expression
137** evaluates to NULL.
138*/
danielk1977e014a832004-05-17 10:48:57 +0000139static int binaryCompareP1(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){
danielk1977bf3b7212004-05-18 10:06:24 +0000140 char aff = sqlite3ExprAffinity(pExpr2);
danielk1977e014a832004-05-17 10:48:57 +0000141 return (((int)sqlite3CompareAffinity(pExpr1, aff))<<8)+(jumpIfNull?1:0);
danielk1977a37cdde2004-05-16 11:15:36 +0000142}
143
drha2e00042002-01-22 03:13:42 +0000144/*
danielk19770202b292004-06-09 09:55:16 +0000145** Return a pointer to the collation sequence that should be used by
146** a binary comparison operator comparing pLeft and pRight.
147**
148** If the left hand expression has a collating sequence type, then it is
149** used. Otherwise the collation sequence for the right hand expression
150** is used, or the default (BINARY) if neither expression has a collating
151** type.
152*/
danielk19777cedc8d2004-06-10 10:50:08 +0000153static CollSeq* binaryCompareCollSeq(Parse *pParse, Expr *pLeft, Expr *pRight){
154 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pLeft);
danielk19770202b292004-06-09 09:55:16 +0000155 if( !pColl ){
danielk19777cedc8d2004-06-10 10:50:08 +0000156 pColl = sqlite3ExprCollSeq(pParse, pRight);
danielk19770202b292004-06-09 09:55:16 +0000157 }
158 return pColl;
159}
160
161/*
drhbe5c89a2004-07-26 00:31:09 +0000162** Generate code for a comparison operator.
163*/
164static int codeCompare(
165 Parse *pParse, /* The parsing (and code generating) context */
166 Expr *pLeft, /* The left operand */
167 Expr *pRight, /* The right operand */
168 int opcode, /* The comparison opcode */
169 int dest, /* Jump here if true. */
170 int jumpIfNull /* If true, jump if either operand is NULL */
171){
172 int p1 = binaryCompareP1(pLeft, pRight, jumpIfNull);
173 CollSeq *p3 = binaryCompareCollSeq(pParse, pLeft, pRight);
drh94a11212004-09-25 13:12:14 +0000174 return sqlite3VdbeOp3(pParse->pVdbe, opcode, p1, dest, (void*)p3, P3_COLLSEQ);
drhbe5c89a2004-07-26 00:31:09 +0000175}
176
177/*
drha76b5df2002-02-23 02:32:10 +0000178** Construct a new expression node and return a pointer to it. Memory
179** for this node is obtained from sqliteMalloc(). The calling function
180** is responsible for making sure the node eventually gets freed.
181*/
drhe4e72072004-11-23 01:47:30 +0000182Expr *sqlite3Expr(int op, Expr *pLeft, Expr *pRight, const Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000183 Expr *pNew;
184 pNew = sqliteMalloc( sizeof(Expr) );
185 if( pNew==0 ){
drh4efc4752004-01-16 15:55:37 +0000186 /* When malloc fails, we leak memory from pLeft and pRight */
drha76b5df2002-02-23 02:32:10 +0000187 return 0;
188 }
189 pNew->op = op;
190 pNew->pLeft = pLeft;
191 pNew->pRight = pRight;
192 if( pToken ){
drh4b59ab52002-08-24 18:24:51 +0000193 assert( pToken->dyn==0 );
drh145716b2004-09-24 12:24:06 +0000194 pNew->span = pNew->token = *pToken;
195 }else if( pLeft && pRight ){
196 sqlite3ExprSpan(pNew, &pLeft->span, &pRight->span);
drha76b5df2002-02-23 02:32:10 +0000197 }
drha76b5df2002-02-23 02:32:10 +0000198 return pNew;
199}
200
201/*
drh4e0cff62004-11-05 05:10:28 +0000202** When doing a nested parse, you can include terms in an expression
203** that look like this: #0 #1 #2 ... These terms refer to elements
204** on the stack. "#0" (or just "#") means the top of the stack.
drh2958a4e2004-11-12 03:56:15 +0000205** "#1" means the next down on the stack. And so forth. #-1 means
206** memory location 0. #-2 means memory location 1. And so forth.
drh4e0cff62004-11-05 05:10:28 +0000207**
208** This routine is called by the parser to deal with on of those terms.
209** It immediately generates code to store the value in a memory location.
210** The returns an expression that will code to extract the value from
211** that memory location as needed.
212*/
213Expr *sqlite3RegisterExpr(Parse *pParse, Token *pToken){
214 Vdbe *v = pParse->pVdbe;
215 Expr *p;
216 int depth;
217 if( v==0 ) return 0;
218 if( pParse->nested==0 ){
219 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", pToken);
220 return 0;
221 }
222 p = sqlite3Expr(TK_REGISTER, 0, 0, pToken);
drh73c42a12004-11-20 18:13:10 +0000223 if( p==0 ){
224 return 0; /* Malloc failed */
225 }
drh4e0cff62004-11-05 05:10:28 +0000226 depth = atoi(&pToken->z[1]);
drh2958a4e2004-11-12 03:56:15 +0000227 if( depth>=0 ){
228 p->iTable = pParse->nMem++;
229 sqlite3VdbeAddOp(v, OP_Dup, depth, 0);
230 sqlite3VdbeAddOp(v, OP_MemStore, p->iTable, 1);
231 }else{
232 p->iTable = -1-depth;
233 }
drh4e0cff62004-11-05 05:10:28 +0000234 return p;
235}
236
237/*
drh91bb0ee2004-09-01 03:06:34 +0000238** Join two expressions using an AND operator. If either expression is
239** NULL, then just return the other expression.
240*/
241Expr *sqlite3ExprAnd(Expr *pLeft, Expr *pRight){
242 if( pLeft==0 ){
243 return pRight;
244 }else if( pRight==0 ){
245 return pLeft;
246 }else{
247 return sqlite3Expr(TK_AND, pLeft, pRight, 0);
248 }
249}
250
251/*
drh6977fea2002-10-22 23:38:04 +0000252** Set the Expr.span field of the given expression to span all
drha76b5df2002-02-23 02:32:10 +0000253** text between the two given tokens.
254*/
danielk19774adee202004-05-08 08:23:19 +0000255void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
drh4efc4752004-01-16 15:55:37 +0000256 assert( pRight!=0 );
257 assert( pLeft!=0 );
drh71c697e2004-08-08 23:39:19 +0000258 if( !sqlite3_malloc_failed && pRight->z && pLeft->z ){
drhad6d9462004-09-19 02:15:24 +0000259 assert( pLeft->dyn==0 || pLeft->z[pLeft->n]==0 );
drh145716b2004-09-24 12:24:06 +0000260 if( pLeft->dyn==0 && pRight->dyn==0 ){
drh6977fea2002-10-22 23:38:04 +0000261 pExpr->span.z = pLeft->z;
262 pExpr->span.n = pRight->n + Addr(pRight->z) - Addr(pLeft->z);
drh4b59ab52002-08-24 18:24:51 +0000263 }else{
drh6977fea2002-10-22 23:38:04 +0000264 pExpr->span.z = 0;
drh4b59ab52002-08-24 18:24:51 +0000265 }
drha76b5df2002-02-23 02:32:10 +0000266 }
267}
268
269/*
270** Construct a new expression node for a function with multiple
271** arguments.
272*/
danielk19774adee202004-05-08 08:23:19 +0000273Expr *sqlite3ExprFunction(ExprList *pList, Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000274 Expr *pNew;
275 pNew = sqliteMalloc( sizeof(Expr) );
276 if( pNew==0 ){
danielk19774adee202004-05-08 08:23:19 +0000277 /* sqlite3ExprListDelete(pList); // Leak pList when malloc fails */
drha76b5df2002-02-23 02:32:10 +0000278 return 0;
279 }
280 pNew->op = TK_FUNCTION;
281 pNew->pList = pList;
282 if( pToken ){
drh4b59ab52002-08-24 18:24:51 +0000283 assert( pToken->dyn==0 );
drha76b5df2002-02-23 02:32:10 +0000284 pNew->token = *pToken;
285 }else{
286 pNew->token.z = 0;
drha76b5df2002-02-23 02:32:10 +0000287 }
drh6977fea2002-10-22 23:38:04 +0000288 pNew->span = pNew->token;
drha76b5df2002-02-23 02:32:10 +0000289 return pNew;
290}
291
292/*
drhfa6bc002004-09-07 16:19:52 +0000293** Assign a variable number to an expression that encodes a wildcard
294** in the original SQL statement.
295**
296** Wildcards consisting of a single "?" are assigned the next sequential
297** variable number.
298**
299** Wildcards of the form "?nnn" are assigned the number "nnn". We make
300** sure "nnn" is not too be to avoid a denial of service attack when
301** the SQL statement comes from an external source.
302**
303** Wildcards of the form ":aaa" or "$aaa" are assigned the same number
304** as the previous instance of the same wildcard. Or if this is the first
305** instance of the wildcard, the next sequenial variable number is
306** assigned.
307*/
308void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){
309 Token *pToken;
310 if( pExpr==0 ) return;
311 pToken = &pExpr->token;
312 assert( pToken->n>=1 );
313 assert( pToken->z!=0 );
314 assert( pToken->z[0]!=0 );
315 if( pToken->n==1 ){
316 /* Wildcard of the form "?". Assign the next variable number */
317 pExpr->iTable = ++pParse->nVar;
318 }else if( pToken->z[0]=='?' ){
319 /* Wildcard of the form "?nnn". Convert "nnn" to an integer and
320 ** use it as the variable number */
321 int i;
322 pExpr->iTable = i = atoi(&pToken->z[1]);
323 if( i<1 || i>SQLITE_MAX_VARIABLE_NUMBER ){
324 sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
325 SQLITE_MAX_VARIABLE_NUMBER);
326 }
327 if( i>pParse->nVar ){
328 pParse->nVar = i;
329 }
330 }else{
331 /* Wildcards of the form ":aaa" or "$aaa". Reuse the same variable
332 ** number as the prior appearance of the same name, or if the name
333 ** has never appeared before, reuse the same variable number
334 */
335 int i, n;
336 n = pToken->n;
337 for(i=0; i<pParse->nVarExpr; i++){
338 Expr *pE;
339 if( (pE = pParse->apVarExpr[i])!=0
340 && pE->token.n==n
341 && memcmp(pE->token.z, pToken->z, n)==0 ){
342 pExpr->iTable = pE->iTable;
343 break;
344 }
345 }
346 if( i>=pParse->nVarExpr ){
347 pExpr->iTable = ++pParse->nVar;
348 if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){
349 pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10;
350 pParse->apVarExpr = sqliteRealloc(pParse->apVarExpr,
351 pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0]) );
352 }
353 if( !sqlite3_malloc_failed ){
354 assert( pParse->apVarExpr!=0 );
355 pParse->apVarExpr[pParse->nVarExpr++] = pExpr;
356 }
357 }
358 }
359}
360
361/*
drha2e00042002-01-22 03:13:42 +0000362** Recursively delete an expression tree.
363*/
danielk19774adee202004-05-08 08:23:19 +0000364void sqlite3ExprDelete(Expr *p){
drha2e00042002-01-22 03:13:42 +0000365 if( p==0 ) return;
drh4efc4752004-01-16 15:55:37 +0000366 if( p->span.dyn ) sqliteFree((char*)p->span.z);
367 if( p->token.dyn ) sqliteFree((char*)p->token.z);
danielk19774adee202004-05-08 08:23:19 +0000368 sqlite3ExprDelete(p->pLeft);
369 sqlite3ExprDelete(p->pRight);
370 sqlite3ExprListDelete(p->pList);
371 sqlite3SelectDelete(p->pSelect);
drha2e00042002-01-22 03:13:42 +0000372 sqliteFree(p);
373}
374
drha76b5df2002-02-23 02:32:10 +0000375
376/*
drhff78bd22002-02-27 01:47:11 +0000377** The following group of routines make deep copies of expressions,
378** expression lists, ID lists, and select statements. The copies can
379** be deleted (by being passed to their respective ...Delete() routines)
380** without effecting the originals.
381**
danielk19774adee202004-05-08 08:23:19 +0000382** The expression list, ID, and source lists return by sqlite3ExprListDup(),
383** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
drhad3cab52002-05-24 02:04:32 +0000384** by subsequent calls to sqlite*ListAppend() routines.
drhff78bd22002-02-27 01:47:11 +0000385**
drhad3cab52002-05-24 02:04:32 +0000386** Any tables that the SrcList might point to are not duplicated.
drhff78bd22002-02-27 01:47:11 +0000387*/
danielk19774adee202004-05-08 08:23:19 +0000388Expr *sqlite3ExprDup(Expr *p){
drhff78bd22002-02-27 01:47:11 +0000389 Expr *pNew;
390 if( p==0 ) return 0;
drhfcb78a42003-01-18 20:11:05 +0000391 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000392 if( pNew==0 ) return 0;
drh3b167c72002-06-28 12:18:47 +0000393 memcpy(pNew, p, sizeof(*pNew));
drh6977fea2002-10-22 23:38:04 +0000394 if( p->token.z!=0 ){
drhb9ecf6f2004-11-20 20:44:13 +0000395 pNew->token.z = sqliteStrNDup(p->token.z, p->token.n);
drh4b59ab52002-08-24 18:24:51 +0000396 pNew->token.dyn = 1;
397 }else{
drh4efc4752004-01-16 15:55:37 +0000398 assert( pNew->token.z==0 );
drh4b59ab52002-08-24 18:24:51 +0000399 }
drh6977fea2002-10-22 23:38:04 +0000400 pNew->span.z = 0;
danielk19774adee202004-05-08 08:23:19 +0000401 pNew->pLeft = sqlite3ExprDup(p->pLeft);
402 pNew->pRight = sqlite3ExprDup(p->pRight);
403 pNew->pList = sqlite3ExprListDup(p->pList);
404 pNew->pSelect = sqlite3SelectDup(p->pSelect);
drhff78bd22002-02-27 01:47:11 +0000405 return pNew;
406}
danielk19774adee202004-05-08 08:23:19 +0000407void sqlite3TokenCopy(Token *pTo, Token *pFrom){
drh4b59ab52002-08-24 18:24:51 +0000408 if( pTo->dyn ) sqliteFree((char*)pTo->z);
drh4b59ab52002-08-24 18:24:51 +0000409 if( pFrom->z ){
410 pTo->n = pFrom->n;
411 pTo->z = sqliteStrNDup(pFrom->z, pFrom->n);
412 pTo->dyn = 1;
413 }else{
drh4b59ab52002-08-24 18:24:51 +0000414 pTo->z = 0;
drh4b59ab52002-08-24 18:24:51 +0000415 }
416}
danielk19774adee202004-05-08 08:23:19 +0000417ExprList *sqlite3ExprListDup(ExprList *p){
drhff78bd22002-02-27 01:47:11 +0000418 ExprList *pNew;
drh145716b2004-09-24 12:24:06 +0000419 struct ExprList_item *pItem, *pOldItem;
drhff78bd22002-02-27 01:47:11 +0000420 int i;
421 if( p==0 ) return 0;
422 pNew = sqliteMalloc( sizeof(*pNew) );
423 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000424 pNew->nExpr = pNew->nAlloc = p->nExpr;
drh3e7bc9c2004-02-21 19:17:17 +0000425 pNew->a = pItem = sqliteMalloc( p->nExpr*sizeof(p->a[0]) );
danielk1977e0048402004-06-15 16:51:01 +0000426 if( pItem==0 ){
427 sqliteFree(pNew);
428 return 0;
429 }
drh145716b2004-09-24 12:24:06 +0000430 pOldItem = p->a;
431 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
drh4b59ab52002-08-24 18:24:51 +0000432 Expr *pNewExpr, *pOldExpr;
drh145716b2004-09-24 12:24:06 +0000433 pItem->pExpr = pNewExpr = sqlite3ExprDup(pOldExpr = pOldItem->pExpr);
drh6977fea2002-10-22 23:38:04 +0000434 if( pOldExpr->span.z!=0 && pNewExpr ){
435 /* Always make a copy of the span for top-level expressions in the
drh4b59ab52002-08-24 18:24:51 +0000436 ** expression list. The logic in SELECT processing that determines
437 ** the names of columns in the result set needs this information */
danielk19774adee202004-05-08 08:23:19 +0000438 sqlite3TokenCopy(&pNewExpr->span, &pOldExpr->span);
drh4b59ab52002-08-24 18:24:51 +0000439 }
drh1f3e9052002-10-31 00:09:39 +0000440 assert( pNewExpr==0 || pNewExpr->span.z!=0
danielk197724b03fd2004-05-10 10:34:34 +0000441 || pOldExpr->span.z==0 || sqlite3_malloc_failed );
drh145716b2004-09-24 12:24:06 +0000442 pItem->zName = sqliteStrDup(pOldItem->zName);
443 pItem->sortOrder = pOldItem->sortOrder;
444 pItem->isAgg = pOldItem->isAgg;
drh3e7bc9c2004-02-21 19:17:17 +0000445 pItem->done = 0;
drhff78bd22002-02-27 01:47:11 +0000446 }
447 return pNew;
448}
danielk19774adee202004-05-08 08:23:19 +0000449SrcList *sqlite3SrcListDup(SrcList *p){
drhad3cab52002-05-24 02:04:32 +0000450 SrcList *pNew;
451 int i;
drh113088e2003-03-20 01:16:58 +0000452 int nByte;
drhad3cab52002-05-24 02:04:32 +0000453 if( p==0 ) return 0;
drh113088e2003-03-20 01:16:58 +0000454 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
drh4efc4752004-01-16 15:55:37 +0000455 pNew = sqliteMallocRaw( nByte );
drhad3cab52002-05-24 02:04:32 +0000456 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000457 pNew->nSrc = pNew->nAlloc = p->nSrc;
drhad3cab52002-05-24 02:04:32 +0000458 for(i=0; i<p->nSrc; i++){
drh4efc4752004-01-16 15:55:37 +0000459 struct SrcList_item *pNewItem = &pNew->a[i];
460 struct SrcList_item *pOldItem = &p->a[i];
461 pNewItem->zDatabase = sqliteStrDup(pOldItem->zDatabase);
462 pNewItem->zName = sqliteStrDup(pOldItem->zName);
463 pNewItem->zAlias = sqliteStrDup(pOldItem->zAlias);
464 pNewItem->jointype = pOldItem->jointype;
465 pNewItem->iCursor = pOldItem->iCursor;
466 pNewItem->pTab = 0;
danielk19774adee202004-05-08 08:23:19 +0000467 pNewItem->pSelect = sqlite3SelectDup(pOldItem->pSelect);
468 pNewItem->pOn = sqlite3ExprDup(pOldItem->pOn);
469 pNewItem->pUsing = sqlite3IdListDup(pOldItem->pUsing);
drhad3cab52002-05-24 02:04:32 +0000470 }
471 return pNew;
472}
danielk19774adee202004-05-08 08:23:19 +0000473IdList *sqlite3IdListDup(IdList *p){
drhff78bd22002-02-27 01:47:11 +0000474 IdList *pNew;
475 int i;
476 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000477 pNew = sqliteMallocRaw( sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +0000478 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000479 pNew->nId = pNew->nAlloc = p->nId;
drh4efc4752004-01-16 15:55:37 +0000480 pNew->a = sqliteMallocRaw( p->nId*sizeof(p->a[0]) );
drhe4697f52002-05-23 02:09:03 +0000481 if( pNew->a==0 ) return 0;
drhff78bd22002-02-27 01:47:11 +0000482 for(i=0; i<p->nId; i++){
drh4efc4752004-01-16 15:55:37 +0000483 struct IdList_item *pNewItem = &pNew->a[i];
484 struct IdList_item *pOldItem = &p->a[i];
485 pNewItem->zName = sqliteStrDup(pOldItem->zName);
486 pNewItem->idx = pOldItem->idx;
drhff78bd22002-02-27 01:47:11 +0000487 }
488 return pNew;
489}
danielk19774adee202004-05-08 08:23:19 +0000490Select *sqlite3SelectDup(Select *p){
drhff78bd22002-02-27 01:47:11 +0000491 Select *pNew;
492 if( p==0 ) return 0;
drh4efc4752004-01-16 15:55:37 +0000493 pNew = sqliteMallocRaw( sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000494 if( pNew==0 ) return 0;
495 pNew->isDistinct = p->isDistinct;
danielk19774adee202004-05-08 08:23:19 +0000496 pNew->pEList = sqlite3ExprListDup(p->pEList);
497 pNew->pSrc = sqlite3SrcListDup(p->pSrc);
498 pNew->pWhere = sqlite3ExprDup(p->pWhere);
499 pNew->pGroupBy = sqlite3ExprListDup(p->pGroupBy);
500 pNew->pHaving = sqlite3ExprDup(p->pHaving);
501 pNew->pOrderBy = sqlite3ExprListDup(p->pOrderBy);
drhff78bd22002-02-27 01:47:11 +0000502 pNew->op = p->op;
danielk19774adee202004-05-08 08:23:19 +0000503 pNew->pPrior = sqlite3SelectDup(p->pPrior);
drhff78bd22002-02-27 01:47:11 +0000504 pNew->nLimit = p->nLimit;
505 pNew->nOffset = p->nOffset;
drh7b58dae2003-07-20 01:16:46 +0000506 pNew->iLimit = -1;
507 pNew->iOffset = -1;
danielk1977dc1bdc42004-06-11 10:51:27 +0000508 pNew->ppOpenTemp = 0;
drhb6c29892004-11-22 19:12:19 +0000509 pNew->pFetch = 0;
drhff78bd22002-02-27 01:47:11 +0000510 return pNew;
511}
512
513
514/*
drha76b5df2002-02-23 02:32:10 +0000515** Add a new element to the end of an expression list. If pList is
516** initially NULL, then create a new expression list.
517*/
danielk19774adee202004-05-08 08:23:19 +0000518ExprList *sqlite3ExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){
drha76b5df2002-02-23 02:32:10 +0000519 if( pList==0 ){
520 pList = sqliteMalloc( sizeof(ExprList) );
521 if( pList==0 ){
danielk19774adee202004-05-08 08:23:19 +0000522 /* sqlite3ExprDelete(pExpr); // Leak memory if malloc fails */
drha76b5df2002-02-23 02:32:10 +0000523 return 0;
524 }
drh4efc4752004-01-16 15:55:37 +0000525 assert( pList->nAlloc==0 );
drha76b5df2002-02-23 02:32:10 +0000526 }
drh4305d102003-07-30 12:34:12 +0000527 if( pList->nAlloc<=pList->nExpr ){
drh4305d102003-07-30 12:34:12 +0000528 pList->nAlloc = pList->nAlloc*2 + 4;
drh4efc4752004-01-16 15:55:37 +0000529 pList->a = sqliteRealloc(pList->a, pList->nAlloc*sizeof(pList->a[0]));
530 if( pList->a==0 ){
danielk19774adee202004-05-08 08:23:19 +0000531 /* sqlite3ExprDelete(pExpr); // Leak memory if malloc fails */
drh4efc4752004-01-16 15:55:37 +0000532 pList->nExpr = pList->nAlloc = 0;
drha76b5df2002-02-23 02:32:10 +0000533 return pList;
534 }
drha76b5df2002-02-23 02:32:10 +0000535 }
drh4efc4752004-01-16 15:55:37 +0000536 assert( pList->a!=0 );
537 if( pExpr || pName ){
538 struct ExprList_item *pItem = &pList->a[pList->nExpr++];
539 memset(pItem, 0, sizeof(*pItem));
540 pItem->pExpr = pExpr;
drha99db3b2004-06-19 14:49:12 +0000541 pItem->zName = sqlite3NameFromToken(pName);
drha76b5df2002-02-23 02:32:10 +0000542 }
543 return pList;
544}
545
546/*
547** Delete an entire expression list.
548*/
danielk19774adee202004-05-08 08:23:19 +0000549void sqlite3ExprListDelete(ExprList *pList){
drha76b5df2002-02-23 02:32:10 +0000550 int i;
drhbe5c89a2004-07-26 00:31:09 +0000551 struct ExprList_item *pItem;
drha76b5df2002-02-23 02:32:10 +0000552 if( pList==0 ) return;
drh1bdd9b52004-04-23 17:04:44 +0000553 assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
554 assert( pList->nExpr<=pList->nAlloc );
drhbe5c89a2004-07-26 00:31:09 +0000555 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
556 sqlite3ExprDelete(pItem->pExpr);
557 sqliteFree(pItem->zName);
drha76b5df2002-02-23 02:32:10 +0000558 }
559 sqliteFree(pList->a);
560 sqliteFree(pList);
561}
562
563/*
drh626a8792005-01-17 22:08:19 +0000564** Walk an expression tree. Call xFunc for each node visited.
drh73b211a2005-01-18 04:00:42 +0000565**
drh626a8792005-01-17 22:08:19 +0000566** The return value from xFunc determines whether the tree walk continues.
567** 0 means continue walking the tree. 1 means do not walk children
568** of the current node but continue with siblings. 2 means abandon
569** the tree walk completely.
570**
571** The return value from this routine is 1 to abandon the tree walk
572** and 0 to continue.
573*/
574static int walkExprTree(Expr *pExpr, int (*xFunc)(void*,Expr*), void *pArg){
575 ExprList *pList;
576 int rc;
577 if( pExpr==0 ) return 0;
578 rc = (*xFunc)(pArg, pExpr);
579 if( rc==0 ){
580 if( walkExprTree(pExpr->pLeft, xFunc, pArg) ) return 1;
581 if( walkExprTree(pExpr->pRight, xFunc, pArg) ) return 1;
582 pList = pExpr->pList;
583 if( pList ){
584 int i;
585 struct ExprList_item *pItem;
586 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
587 if( walkExprTree(pItem->pExpr, xFunc, pArg) ) return 1;
588 }
589 }
590 }
591 return rc>1;
592}
593
594/*
595** This routine is designed as an xFunc for walkExprTree().
596**
597** pArg is really a pointer to an integer. If we can tell by looking
drh73b211a2005-01-18 04:00:42 +0000598** at pExpr that the expression that contains pExpr is not a constant
599** expression, then set *pArg to 0 and return 2 to abandon the tree walk.
600** If pExpr does does not disqualify the expression from being a constant
601** then do nothing.
602**
603** After walking the whole tree, if no nodes are found that disqualify
604** the expression as constant, then we assume the whole expression
605** is constant. See sqlite3ExprIsConstant() for additional information.
drh626a8792005-01-17 22:08:19 +0000606*/
607static int exprNodeIsConstant(void *pArg, Expr *pExpr){
608 switch( pExpr->op ){
609 case TK_ID:
610 case TK_COLUMN:
611 case TK_DOT:
612 case TK_AGG_FUNCTION:
613 case TK_FUNCTION:
614 *((int*)pArg) = 0;
615 return 2;
616 default:
617 return 0;
618 }
619}
620
621/*
drhfef52082000-06-06 01:50:43 +0000622** Walk an expression tree. Return 1 if the expression is constant
623** and 0 if it involves variables.
drh23989372002-05-21 13:43:04 +0000624**
625** For the purposes of this function, a double-quoted string (ex: "abc")
626** is considered a variable but a single-quoted string (ex: 'abc') is
627** a constant.
drhfef52082000-06-06 01:50:43 +0000628*/
danielk19774adee202004-05-08 08:23:19 +0000629int sqlite3ExprIsConstant(Expr *p){
drh626a8792005-01-17 22:08:19 +0000630 int isConst = 1;
631 walkExprTree(p, exprNodeIsConstant, &isConst);
632 return isConst;
drhfef52082000-06-06 01:50:43 +0000633}
634
635/*
drh73b211a2005-01-18 04:00:42 +0000636** If the expression p codes a constant integer that is small enough
drh202b2df2004-01-06 01:13:46 +0000637** to fit in a 32-bit integer, return 1 and put the value of the integer
638** in *pValue. If the expression is not an integer or if it is too big
639** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
drhe4de1fe2002-06-02 16:09:01 +0000640*/
danielk19774adee202004-05-08 08:23:19 +0000641int sqlite3ExprIsInteger(Expr *p, int *pValue){
drhe4de1fe2002-06-02 16:09:01 +0000642 switch( p->op ){
643 case TK_INTEGER: {
drhfec19aa2004-05-19 20:41:03 +0000644 if( sqlite3GetInt32(p->token.z, pValue) ){
drh202b2df2004-01-06 01:13:46 +0000645 return 1;
646 }
647 break;
drhe4de1fe2002-06-02 16:09:01 +0000648 }
drh4b59ab52002-08-24 18:24:51 +0000649 case TK_UPLUS: {
danielk19774adee202004-05-08 08:23:19 +0000650 return sqlite3ExprIsInteger(p->pLeft, pValue);
drh4b59ab52002-08-24 18:24:51 +0000651 }
drhe4de1fe2002-06-02 16:09:01 +0000652 case TK_UMINUS: {
653 int v;
danielk19774adee202004-05-08 08:23:19 +0000654 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
drhe4de1fe2002-06-02 16:09:01 +0000655 *pValue = -v;
656 return 1;
657 }
658 break;
659 }
660 default: break;
661 }
662 return 0;
663}
664
665/*
drhc4a3c772001-04-04 11:48:57 +0000666** Return TRUE if the given string is a row-id column name.
667*/
danielk19774adee202004-05-08 08:23:19 +0000668int sqlite3IsRowid(const char *z){
669 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
670 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
671 if( sqlite3StrICmp(z, "OID")==0 ) return 1;
drhc4a3c772001-04-04 11:48:57 +0000672 return 0;
673}
674
675/*
drh8141f612004-01-25 22:44:58 +0000676** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
677** that name in the set of source tables in pSrcList and make the pExpr
678** expression node refer back to that source column. The following changes
679** are made to pExpr:
680**
681** pExpr->iDb Set the index in db->aDb[] of the database holding
682** the table.
683** pExpr->iTable Set to the cursor number for the table obtained
684** from pSrcList.
685** pExpr->iColumn Set to the column number within the table.
drh8141f612004-01-25 22:44:58 +0000686** pExpr->op Set to TK_COLUMN.
687** pExpr->pLeft Any expression this points to is deleted
688** pExpr->pRight Any expression this points to is deleted.
689**
690** The pDbToken is the name of the database (the "X"). This value may be
691** NULL meaning that name is of the form Y.Z or Z. Any available database
692** can be used. The pTableToken is the name of the table (the "Y"). This
693** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it
694** means that the form of the name is Z and that columns from any table
695** can be used.
696**
697** If the name cannot be resolved unambiguously, leave an error message
698** in pParse and return non-zero. Return zero on success.
699*/
700static int lookupName(
701 Parse *pParse, /* The parsing context */
702 Token *pDbToken, /* Name of the database containing table, or NULL */
703 Token *pTableToken, /* Name of table containing column, or NULL */
704 Token *pColumnToken, /* Name of the column. */
drh626a8792005-01-17 22:08:19 +0000705 NameContext *pNC, /* The name context used to resolve the name */
drh8141f612004-01-25 22:44:58 +0000706 Expr *pExpr /* Make this EXPR node point to the selected column */
707){
708 char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */
709 char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */
710 char *zCol = 0; /* Name of the column. The "Z" */
711 int i, j; /* Loop counters */
712 int cnt = 0; /* Number of matching column names */
713 int cntTab = 0; /* Number of matching table names */
drh626a8792005-01-17 22:08:19 +0000714 sqlite3 *db = pParse->db; /* The database */
drh51669862004-12-18 18:40:26 +0000715 struct SrcList_item *pItem; /* Use for looping over pSrcList items */
716 struct SrcList_item *pMatch = 0; /* The matching pSrcList item */
drh73b211a2005-01-18 04:00:42 +0000717 NameContext *pTopNC = pNC; /* First namecontext in the list */
drh8141f612004-01-25 22:44:58 +0000718
719 assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
drha99db3b2004-06-19 14:49:12 +0000720 zDb = sqlite3NameFromToken(pDbToken);
721 zTab = sqlite3NameFromToken(pTableToken);
722 zCol = sqlite3NameFromToken(pColumnToken);
danielk197724b03fd2004-05-10 10:34:34 +0000723 if( sqlite3_malloc_failed ){
drh8141f612004-01-25 22:44:58 +0000724 return 1; /* Leak memory (zDb and zTab) if malloc fails */
725 }
drh8141f612004-01-25 22:44:58 +0000726
727 pExpr->iTable = -1;
drh626a8792005-01-17 22:08:19 +0000728 while( pNC && cnt==0 ){
729 SrcList *pSrcList = pNC->pSrcList;
730 ExprList *pEList = pNC->pEList;
drh8141f612004-01-25 22:44:58 +0000731
drh626a8792005-01-17 22:08:19 +0000732 pNC->nRef++;
733 /* assert( zTab==0 || pEList==0 ); */
734 for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
735 Table *pTab = pItem->pTab;
736 Column *pCol;
737
738 if( pTab==0 ) continue;
739 assert( pTab->nCol>0 );
740 if( zTab ){
741 if( pItem->zAlias ){
742 char *zTabName = pItem->zAlias;
743 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
744 }else{
745 char *zTabName = pTab->zName;
746 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
747 if( zDb!=0 && sqlite3StrICmp(db->aDb[pTab->iDb].zName, zDb)!=0 ){
748 continue;
749 }
drh8141f612004-01-25 22:44:58 +0000750 }
751 }
drh626a8792005-01-17 22:08:19 +0000752 if( 0==(cntTab++) ){
drh8141f612004-01-25 22:44:58 +0000753 pExpr->iTable = pItem->iCursor;
754 pExpr->iDb = pTab->iDb;
drh626a8792005-01-17 22:08:19 +0000755 pMatch = pItem;
drh8141f612004-01-25 22:44:58 +0000756 }
drh626a8792005-01-17 22:08:19 +0000757 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
danielk19774adee202004-05-08 08:23:19 +0000758 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
drh8141f612004-01-25 22:44:58 +0000759 cnt++;
drh626a8792005-01-17 22:08:19 +0000760 pExpr->iTable = pItem->iCursor;
761 pMatch = pItem;
762 pExpr->iDb = pTab->iDb;
763 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
drh8141f612004-01-25 22:44:58 +0000764 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
danielk1977a37cdde2004-05-16 11:15:36 +0000765 pExpr->affinity = pTab->aCol[j].affinity;
danielk19770202b292004-06-09 09:55:16 +0000766 pExpr->pColl = pTab->aCol[j].pColl;
drh8141f612004-01-25 22:44:58 +0000767 break;
768 }
769 }
770 }
drh626a8792005-01-17 22:08:19 +0000771
772#ifndef SQLITE_OMIT_TRIGGER
773 /* If we have not already resolved the name, then maybe
774 ** it is a new.* or old.* trigger argument reference
775 */
776 if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
777 TriggerStack *pTriggerStack = pParse->trigStack;
778 Table *pTab = 0;
779 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){
780 pExpr->iTable = pTriggerStack->newIdx;
781 assert( pTriggerStack->pTab );
782 pTab = pTriggerStack->pTab;
783 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab)==0 ){
784 pExpr->iTable = pTriggerStack->oldIdx;
785 assert( pTriggerStack->pTab );
786 pTab = pTriggerStack->pTab;
787 }
788
789 if( pTab ){
790 int j;
791 Column *pCol = pTab->aCol;
792
793 pExpr->iDb = pTab->iDb;
794 cntTab++;
795 for(j=0; j < pTab->nCol; j++, pCol++) {
796 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
797 cnt++;
798 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
799 pExpr->affinity = pTab->aCol[j].affinity;
800 pExpr->pColl = pTab->aCol[j].pColl;
801 break;
802 }
803 }
804 }
805 }
drhb7f91642004-10-31 02:22:47 +0000806#endif /* !defined(SQLITE_OMIT_TRIGGER) */
drh8141f612004-01-25 22:44:58 +0000807
drh626a8792005-01-17 22:08:19 +0000808 /*
809 ** Perhaps the name is a reference to the ROWID
810 */
811 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
812 cnt = 1;
813 pExpr->iColumn = -1;
814 pExpr->affinity = SQLITE_AFF_INTEGER;
815 }
drh8141f612004-01-25 22:44:58 +0000816
drh626a8792005-01-17 22:08:19 +0000817 /*
818 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
819 ** might refer to an result-set alias. This happens, for example, when
820 ** we are resolving names in the WHERE clause of the following command:
821 **
822 ** SELECT a+b AS x FROM table WHERE x<10;
823 **
824 ** In cases like this, replace pExpr with a copy of the expression that
825 ** forms the result set entry ("a+b" in the example) and return immediately.
826 ** Note that the expression in the result set should have already been
827 ** resolved by the time the WHERE clause is resolved.
828 */
drh79d5f632005-01-18 17:20:10 +0000829 if( cnt==0 && pEList!=0 && zTab==0 ){
drh626a8792005-01-17 22:08:19 +0000830 for(j=0; j<pEList->nExpr; j++){
831 char *zAs = pEList->a[j].zName;
832 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
833 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
834 pExpr->op = TK_AS;
835 pExpr->iColumn = j;
836 pExpr->pLeft = sqlite3ExprDup(pEList->a[j].pExpr);
837 sqliteFree(zCol);
838 assert( zTab==0 && zDb==0 );
839 return 0;
840 }
841 }
842 }
843
844 /* Advance to the next name context. The loop will exit when either
845 ** we have a match (cnt>0) or when we run out of name contexts.
846 */
847 if( cnt==0 ){
848 pNC = pNC->pNext;
849 }
drh8141f612004-01-25 22:44:58 +0000850 }
851
852 /*
853 ** If X and Y are NULL (in other words if only the column name Z is
854 ** supplied) and the value of Z is enclosed in double-quotes, then
855 ** Z is a string literal if it doesn't match any column names. In that
856 ** case, we need to return right away and not make any changes to
857 ** pExpr.
858 */
859 if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
860 sqliteFree(zCol);
861 return 0;
862 }
863
864 /*
865 ** cnt==0 means there was not match. cnt>1 means there were two or
866 ** more matches. Either way, we have an error.
867 */
868 if( cnt!=1 ){
869 char *z = 0;
870 char *zErr;
871 zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s";
872 if( zDb ){
danielk19774adee202004-05-08 08:23:19 +0000873 sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, 0);
drh8141f612004-01-25 22:44:58 +0000874 }else if( zTab ){
danielk19774adee202004-05-08 08:23:19 +0000875 sqlite3SetString(&z, zTab, ".", zCol, 0);
drh8141f612004-01-25 22:44:58 +0000876 }else{
877 z = sqliteStrDup(zCol);
878 }
danielk19774adee202004-05-08 08:23:19 +0000879 sqlite3ErrorMsg(pParse, zErr, z);
drh8141f612004-01-25 22:44:58 +0000880 sqliteFree(z);
drh73b211a2005-01-18 04:00:42 +0000881 pTopNC->nErr++;
drh8141f612004-01-25 22:44:58 +0000882 }
883
drh51669862004-12-18 18:40:26 +0000884 /* If a column from a table in pSrcList is referenced, then record
885 ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes
886 ** bit 0 to be set. Column 1 sets bit 1. And so forth. If the
887 ** column number is greater than the number of bits in the bitmask
888 ** then set the high-order bit of the bitmask.
889 */
890 if( pExpr->iColumn>=0 && pMatch!=0 ){
891 int n = pExpr->iColumn;
892 if( n>=sizeof(Bitmask)*8 ){
893 n = sizeof(Bitmask)*8-1;
894 }
895 assert( pMatch->iCursor==pExpr->iTable );
896 pMatch->colUsed |= 1<<n;
897 }
898
drh8141f612004-01-25 22:44:58 +0000899 /* Clean up and return
900 */
901 sqliteFree(zDb);
902 sqliteFree(zTab);
903 sqliteFree(zCol);
danielk19774adee202004-05-08 08:23:19 +0000904 sqlite3ExprDelete(pExpr->pLeft);
drh8141f612004-01-25 22:44:58 +0000905 pExpr->pLeft = 0;
danielk19774adee202004-05-08 08:23:19 +0000906 sqlite3ExprDelete(pExpr->pRight);
drh8141f612004-01-25 22:44:58 +0000907 pExpr->pRight = 0;
908 pExpr->op = TK_COLUMN;
drh626a8792005-01-17 22:08:19 +0000909 if( cnt==1 ){
910 assert( pNC!=0 && pNC->pSrcList!=0 );
911 sqlite3AuthRead(pParse, pExpr, pNC->pSrcList);
912 }
drh8141f612004-01-25 22:44:58 +0000913 return cnt!=1;
914}
915
916/*
drh626a8792005-01-17 22:08:19 +0000917** pExpr is a node that defines a function of some kind. It might
918** be a syntactic function like "count(x)" or it might be a function
919** that implements an operator, like "a LIKE b".
920**
921** This routine makes *pzName point to the name of the function and
922** *pnName hold the number of characters in the function name.
923*/
924static void getFunctionName(Expr *pExpr, const char **pzName, int *pnName){
925 switch( pExpr->op ){
926 case TK_FUNCTION: {
927 *pzName = pExpr->token.z;
928 *pnName = pExpr->token.n;
929 break;
930 }
931 case TK_LIKE: {
932 *pzName = "like";
933 *pnName = 4;
934 break;
935 }
936 case TK_GLOB: {
937 *pzName = "glob";
938 *pnName = 4;
939 break;
940 }
941 case TK_CTIME: {
942 *pzName = "current_time";
943 *pnName = 12;
944 break;
945 }
946 case TK_CDATE: {
947 *pzName = "current_date";
948 *pnName = 12;
949 break;
950 }
951 case TK_CTIMESTAMP: {
952 *pzName = "current_timestamp";
953 *pnName = 17;
954 break;
955 }
drh626a8792005-01-17 22:08:19 +0000956 }
957}
958
959/*
960** This routine is designed as an xFunc for walkExprTree().
961**
drh73b211a2005-01-18 04:00:42 +0000962** Resolve symbolic names into TK_COLUMN operators for the current
drh626a8792005-01-17 22:08:19 +0000963** node in the expression tree. Return 0 to continue the search down
drh73b211a2005-01-18 04:00:42 +0000964** the tree or 2 to abort the tree walk.
965**
966** This routine also does error checking and name resolution for
967** function names. The operator for aggregate functions is changed
968** to TK_AGG_FUNCTION.
drh626a8792005-01-17 22:08:19 +0000969*/
970static int nameResolverStep(void *pArg, Expr *pExpr){
971 NameContext *pNC = (NameContext*)pArg;
972 SrcList *pSrcList;
973 Parse *pParse;
974 int i;
975
976 assert( pNC!=0 );
977 pSrcList = pNC->pSrcList;
978 pParse = pNC->pParse;
979 if( pExpr==0 ) return 1;
980 if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return 1;
981 ExprSetProperty(pExpr, EP_Resolved);
982#ifndef NDEBUG
983 if( pSrcList ){
984 for(i=0; i<pSrcList->nSrc; i++){
985 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
986 }
987 }
988#endif
989 switch( pExpr->op ){
990 /* Double-quoted strings (ex: "abc") are used as identifiers if
991 ** possible. Otherwise they remain as strings. Single-quoted
992 ** strings (ex: 'abc') are always string literals.
993 */
994 case TK_STRING: {
995 if( pExpr->token.z[0]=='\'' ) break;
996 /* Fall thru into the TK_ID case if this is a double-quoted string */
997 }
998 /* A lone identifier is the name of a column.
999 */
1000 case TK_ID: {
1001 if( pSrcList==0 ) break;
1002 lookupName(pParse, 0, 0, &pExpr->token, pNC, pExpr);
1003 return 1;
1004 }
1005
1006 /* A table name and column name: ID.ID
1007 ** Or a database, table and column: ID.ID.ID
1008 */
1009 case TK_DOT: {
1010 Token *pColumn;
1011 Token *pTable;
1012 Token *pDb;
1013 Expr *pRight;
1014
1015 if( pSrcList==0 ) break;
1016 pRight = pExpr->pRight;
1017 if( pRight->op==TK_ID ){
1018 pDb = 0;
1019 pTable = &pExpr->pLeft->token;
1020 pColumn = &pRight->token;
1021 }else{
1022 assert( pRight->op==TK_DOT );
1023 pDb = &pExpr->pLeft->token;
1024 pTable = &pRight->pLeft->token;
1025 pColumn = &pRight->pRight->token;
1026 }
1027 lookupName(pParse, pDb, pTable, pColumn, pNC, pExpr);
1028 return 1;
1029 }
1030
1031 /* Resolve function names
1032 */
1033 case TK_CTIME:
1034 case TK_CTIMESTAMP:
1035 case TK_CDATE:
1036 /* Note: The above three were a seperate case in sqlmoto. Reason? */
1037 case TK_GLOB:
1038 case TK_LIKE:
1039 case TK_FUNCTION: {
1040 ExprList *pList = pExpr->pList; /* The argument list */
1041 int n = pList ? pList->nExpr : 0; /* Number of arguments */
1042 int no_such_func = 0; /* True if no such function exists */
1043 int wrong_num_args = 0; /* True if wrong number of arguments */
1044 int is_agg = 0; /* True if is an aggregate function */
1045 int i;
1046 int nId; /* Number of characters in function name */
1047 const char *zId; /* The function name. */
drh73b211a2005-01-18 04:00:42 +00001048 FuncDef *pDef; /* Information about the function */
1049 int enc = pParse->db->enc; /* The database encoding */
drh626a8792005-01-17 22:08:19 +00001050
1051 getFunctionName(pExpr, &zId, &nId);
1052 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
1053 if( pDef==0 ){
1054 pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
1055 if( pDef==0 ){
1056 no_such_func = 1;
1057 }else{
1058 wrong_num_args = 1;
1059 }
1060 }else{
1061 is_agg = pDef->xFunc==0;
1062 }
1063 if( is_agg && !pNC->allowAgg ){
1064 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
1065 pNC->nErr++;
1066 is_agg = 0;
1067 }else if( no_such_func ){
1068 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
1069 pNC->nErr++;
1070 }else if( wrong_num_args ){
1071 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
1072 nId, zId);
1073 pNC->nErr++;
1074 }
1075 if( is_agg ){
1076 pExpr->op = TK_AGG_FUNCTION;
1077 pNC->hasAgg = 1;
1078 }
drh73b211a2005-01-18 04:00:42 +00001079 if( is_agg ) pNC->allowAgg = 0;
drh626a8792005-01-17 22:08:19 +00001080 for(i=0; pNC->nErr==0 && i<n; i++){
drh73b211a2005-01-18 04:00:42 +00001081 walkExprTree(pList->a[i].pExpr, nameResolverStep, pNC);
drh626a8792005-01-17 22:08:19 +00001082 }
drh73b211a2005-01-18 04:00:42 +00001083 if( is_agg ) pNC->allowAgg = 1;
drh626a8792005-01-17 22:08:19 +00001084 /* FIX ME: Compute pExpr->affinity based on the expected return
1085 ** type of the function
1086 */
1087 return is_agg;
1088 }
1089 }
1090 return 0;
1091}
1092
drh1398ad32005-01-19 23:24:50 +00001093/* Forward declaration */
1094static int sqlite3ExprCodeSubquery(Parse*, NameContext*, Expr*);
1095
drh626a8792005-01-17 22:08:19 +00001096/*
drhcce7d172000-05-31 15:34:51 +00001097** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +00001098** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +00001099** index to the table in the table list and a column offset. The
1100** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
1101** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +00001102** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +00001103** VDBE cursor number for a cursor that is pointing into the referenced
1104** table. The Expr.iColumn value is changed to the index of the column
1105** of the referenced table. The Expr.iColumn value for the special
1106** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
1107** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +00001108**
drh626a8792005-01-17 22:08:19 +00001109** Also resolve function names and check the functions for proper
1110** usage. Make sure all function names are recognized and all functions
1111** have the correct number of arguments. Leave an error message
1112** in pParse->zErrMsg if anything is amiss. Return the number of errors.
1113**
drh73b211a2005-01-18 04:00:42 +00001114** If the expression contains aggregate functions then set the EP_Agg
1115** property on the expression.
drh626a8792005-01-17 22:08:19 +00001116*/
1117int sqlite3ExprResolveNames(
drh1398ad32005-01-19 23:24:50 +00001118 Parse *pParse, /* The parser context */
1119 SrcList *pSrcList, /* List of tables used to resolve column names */
1120 ExprList *pEList, /* List of expressions used to resolve "AS" */
1121 NameContext *pNC, /* Namespace of enclosing statement */
1122 Expr *pExpr, /* The expression to be analyzed. */
1123 int allowAgg, /* True to allow aggregate expressions */
1124 int codeSubquery /* If true, then generate code for subqueries too */
drh626a8792005-01-17 22:08:19 +00001125){
1126 NameContext sNC;
1127
drh73b211a2005-01-18 04:00:42 +00001128 if( pExpr==0 ) return 0;
drh626a8792005-01-17 22:08:19 +00001129 memset(&sNC, 0, sizeof(sNC));
1130 sNC.pSrcList = pSrcList;
1131 sNC.pParse = pParse;
1132 sNC.pEList = pEList;
1133 sNC.allowAgg = allowAgg;
drh1398ad32005-01-19 23:24:50 +00001134 sNC.pNext = pNC;
drh626a8792005-01-17 22:08:19 +00001135 walkExprTree(pExpr, nameResolverStep, &sNC);
drh73b211a2005-01-18 04:00:42 +00001136 if( sNC.hasAgg ){
1137 ExprSetProperty(pExpr, EP_Agg);
drh626a8792005-01-17 22:08:19 +00001138 }
drh73b211a2005-01-18 04:00:42 +00001139 if( sNC.nErr>0 ){
1140 ExprSetProperty(pExpr, EP_Error);
drh1398ad32005-01-19 23:24:50 +00001141 }else if( codeSubquery && sqlite3ExprCodeSubquery(pParse, &sNC, pExpr) ){
drh73b211a2005-01-18 04:00:42 +00001142 return 1;
1143 }
1144 return ExprHasProperty(pExpr, EP_Error);
drh626a8792005-01-17 22:08:19 +00001145}
1146
drh1398ad32005-01-19 23:24:50 +00001147/*
1148** A pointer instance of this structure is used to pass information
1149** through walkExprTree into codeSubqueryStep().
1150*/
1151typedef struct QueryCoder QueryCoder;
1152struct QueryCoder {
1153 Parse *pParse; /* The parsing context */
1154 NameContext *pNC; /* Namespace of first enclosing query */
1155};
1156
drh626a8792005-01-17 22:08:19 +00001157
1158/*
1159** Generate code for subqueries and IN operators.
1160**
drh73b211a2005-01-18 04:00:42 +00001161** IN operators comes in two forms:
drhfef52082000-06-06 01:50:43 +00001162**
1163** expr IN (exprlist)
1164** and
1165** expr IN (SELECT ...)
1166**
1167** The first form is handled by creating a set holding the list
1168** of allowed values. The second form causes the SELECT to generate
1169** a temporary table.
1170**
1171** This routine also looks for scalar SELECTs that are part of an expression.
drh19a775c2000-06-05 18:54:46 +00001172** If it finds any, it generates code to write the value of that select
1173** into a memory cell.
drh73b211a2005-01-18 04:00:42 +00001174**
1175** This routine is a callback for wallExprTree() used to implement
1176** sqlite3ExprCodeSubquery(). See comments on those routines for
1177** additional information.
drhcce7d172000-05-31 15:34:51 +00001178*/
drh51522cd2005-01-20 13:36:19 +00001179#ifndef SQLITE_OMIT_SUBQUERY
drh626a8792005-01-17 22:08:19 +00001180static int codeSubqueryStep(void *pArg, Expr *pExpr){
drh1398ad32005-01-19 23:24:50 +00001181 QueryCoder *pCoder = (QueryCoder*)pArg;
1182 Parse *pParse = pCoder->pParse;
drh6a3ea0e2003-05-02 14:32:12 +00001183
drhcce7d172000-05-31 15:34:51 +00001184 switch( pExpr->op ){
drhfef52082000-06-06 01:50:43 +00001185 case TK_IN: {
danielk1977e014a832004-05-17 10:48:57 +00001186 char affinity;
danielk19774adee202004-05-08 08:23:19 +00001187 Vdbe *v = sqlite3GetVdbe(pParse);
drhd3d39e92004-05-20 22:16:29 +00001188 KeyInfo keyInfo;
danielk19770202b292004-06-09 09:55:16 +00001189 int addr; /* Address of OP_OpenTemp instruction */
drhd3d39e92004-05-20 22:16:29 +00001190
drh626a8792005-01-17 22:08:19 +00001191 if( v==0 ) return 2;
danielk1977bf3b7212004-05-18 10:06:24 +00001192 affinity = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +00001193
1194 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
1195 ** expression it is handled the same way. A temporary table is
1196 ** filled with single-field index keys representing the results
1197 ** from the SELECT or the <exprlist>.
1198 **
1199 ** If the 'x' expression is a column value, or the SELECT...
1200 ** statement returns a column value, then the affinity of that
1201 ** column is used to build the index keys. If both 'x' and the
1202 ** SELECT... statement are columns, then numeric affinity is used
1203 ** if either column has NUMERIC or INTEGER affinity. If neither
1204 ** 'x' nor the SELECT... statement are columns, then numeric affinity
1205 ** is used.
1206 */
1207 pExpr->iTable = pParse->nTab++;
danielk19770202b292004-06-09 09:55:16 +00001208 addr = sqlite3VdbeAddOp(v, OP_OpenTemp, pExpr->iTable, 0);
drhd3d39e92004-05-20 22:16:29 +00001209 memset(&keyInfo, 0, sizeof(keyInfo));
1210 keyInfo.nField = 1;
drhf3218fe2004-05-28 08:21:02 +00001211 sqlite3VdbeAddOp(v, OP_SetNumColumns, pExpr->iTable, 1);
danielk1977e014a832004-05-17 10:48:57 +00001212
drhfef52082000-06-06 01:50:43 +00001213 if( pExpr->pSelect ){
1214 /* Case 1: expr IN (SELECT ...)
1215 **
danielk1977e014a832004-05-17 10:48:57 +00001216 ** Generate code to write the results of the select into the temporary
1217 ** table allocated and opened above.
drhfef52082000-06-06 01:50:43 +00001218 */
danielk1977e014a832004-05-17 10:48:57 +00001219 int iParm = pExpr->iTable + (((int)affinity)<<16);
drhbe5c89a2004-07-26 00:31:09 +00001220 ExprList *pEList;
danielk1977e014a832004-05-17 10:48:57 +00001221 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
drh1398ad32005-01-19 23:24:50 +00001222 sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001223 pEList = pExpr->pSelect->pEList;
1224 if( pEList && pEList->nExpr>0 ){
danielk19777cedc8d2004-06-10 10:50:08 +00001225 keyInfo.aColl[0] = binaryCompareCollSeq(pParse, pExpr->pLeft,
drhbe5c89a2004-07-26 00:31:09 +00001226 pEList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001227 }
drhfef52082000-06-06 01:50:43 +00001228 }else if( pExpr->pList ){
1229 /* Case 2: expr IN (exprlist)
1230 **
danielk1977e014a832004-05-17 10:48:57 +00001231 ** For each expression, build an index key from the evaluation and
1232 ** store it in the temporary table. If <expr> is a column, then use
1233 ** that columns affinity when building index keys. If <expr> is not
1234 ** a column, use numeric affinity.
drhfef52082000-06-06 01:50:43 +00001235 */
danielk1977e014a832004-05-17 10:48:57 +00001236 int i;
danielk1977e014a832004-05-17 10:48:57 +00001237 if( !affinity ){
1238 affinity = SQLITE_AFF_NUMERIC;
1239 }
danielk19770202b292004-06-09 09:55:16 +00001240 keyInfo.aColl[0] = pExpr->pLeft->pColl;
danielk1977e014a832004-05-17 10:48:57 +00001241
1242 /* Loop through each expression in <exprlist>. */
drhfef52082000-06-06 01:50:43 +00001243 for(i=0; i<pExpr->pList->nExpr; i++){
1244 Expr *pE2 = pExpr->pList->a[i].pExpr;
danielk1977e014a832004-05-17 10:48:57 +00001245
1246 /* Check that the expression is constant and valid. */
danielk19774adee202004-05-08 08:23:19 +00001247 if( !sqlite3ExprIsConstant(pE2) ){
1248 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00001249 "right-hand side of IN operator must be constant");
drh626a8792005-01-17 22:08:19 +00001250 return 2;
drhfef52082000-06-06 01:50:43 +00001251 }
drh1398ad32005-01-19 23:24:50 +00001252 if( sqlite3ExprResolveNames(pParse, 0, 0, 0, pE2, 0, 0) ){
drh626a8792005-01-17 22:08:19 +00001253 return 2;
drh4794b982000-06-06 13:54:14 +00001254 }
danielk1977e014a832004-05-17 10:48:57 +00001255
1256 /* Evaluate the expression and insert it into the temp table */
1257 sqlite3ExprCode(pParse, pE2);
drh94a11212004-09-25 13:12:14 +00001258 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1);
danielk19770f69c1e2004-05-29 11:24:50 +00001259 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +00001260 sqlite3VdbeAddOp(v, OP_PutStrKey, pExpr->iTable, 0);
drhfef52082000-06-06 01:50:43 +00001261 }
1262 }
danielk19770202b292004-06-09 09:55:16 +00001263 sqlite3VdbeChangeP3(v, addr, (void *)&keyInfo, P3_KEYINFO);
drh626a8792005-01-17 22:08:19 +00001264 return 1;
drhfef52082000-06-06 01:50:43 +00001265 }
1266
drh51522cd2005-01-20 13:36:19 +00001267 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001268 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +00001269 /* This has to be a scalar SELECT. Generate code to put the
1270 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +00001271 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +00001272 */
drh1398ad32005-01-19 23:24:50 +00001273 NameContext *pNC;
1274 int nRef;
1275 Vdbe *v;
1276 int addr;
drh51522cd2005-01-20 13:36:19 +00001277 int sop;
1278 Select *pSel;
drh1398ad32005-01-19 23:24:50 +00001279
1280 pNC = pCoder->pNC;
1281 if( pNC ) nRef = pNC->nRef;
drh3119bc42005-01-20 01:51:25 +00001282 sqlite3CodeVerifySchema(pParse, -1); /* Insert the cookie verifier Goto */
drh1398ad32005-01-19 23:24:50 +00001283 v = sqlite3GetVdbe(pParse);
1284 addr = sqlite3VdbeAddOp(v, OP_Goto, 0, 0);
drh967e8b72000-06-21 13:59:10 +00001285 pExpr->iColumn = pParse->nMem++;
drh51522cd2005-01-20 13:36:19 +00001286 pSel = pExpr->pSelect;
1287 if( pExpr->op==TK_SELECT ){
1288 sop = SRT_Mem;
1289 }else{
1290 static const Token one = { "1", 0, 1 };
1291 sop = SRT_Exists;
1292 sqlite3ExprListDelete(pSel->pEList);
1293 pSel->pEList = sqlite3ExprListAppend(0,
1294 sqlite3Expr(TK_INTEGER, 0, 0, &one), 0);
1295 }
1296 sqlite3Select(pParse, pSel, sop, pExpr->iColumn, 0, 0, 0, 0, pNC);
drh1398ad32005-01-19 23:24:50 +00001297 if( pNC && pNC->nRef>nRef ){
1298 /* Subquery value changes. Evaluate at each use */
1299 pExpr->iTable = addr+1;
1300 sqlite3VdbeAddOp(v, OP_Return, 0, 0);
1301 sqlite3VdbeChangeP2(v, addr, sqlite3VdbeCurrentAddr(v));
1302 }else{
1303 /* Subquery value is constant. evaluate only once. */
1304 pExpr->iTable = -1;
1305 sqlite3VdbeChangeP2(v, addr, addr+1);
1306 }
drh626a8792005-01-17 22:08:19 +00001307 return 1;
drhcce7d172000-05-31 15:34:51 +00001308 }
1309 }
1310 return 0;
1311}
drh51522cd2005-01-20 13:36:19 +00001312#endif /* SQLITE_OMIT_SUBQUERY */
drhcce7d172000-05-31 15:34:51 +00001313
drhcce7d172000-05-31 15:34:51 +00001314/*
drh73b211a2005-01-18 04:00:42 +00001315** Generate code to evaluate subqueries and IN operators contained
1316** in expression pExpr.
drh4b59ab52002-08-24 18:24:51 +00001317*/
drh1398ad32005-01-19 23:24:50 +00001318static int sqlite3ExprCodeSubquery(
1319 Parse *pParse, /* Parser */
1320 NameContext *pNC, /* First enclosing namespace. Often NULL */
1321 Expr *pExpr /* Subquery to be coded */
1322){
drh51522cd2005-01-20 13:36:19 +00001323#ifndef SQLITE_OMIT_SUBQUERY
drh1398ad32005-01-19 23:24:50 +00001324 QueryCoder sCoder;
1325 sCoder.pParse = pParse;
1326 sCoder.pNC = pNC;
1327 walkExprTree(pExpr, codeSubqueryStep, &sCoder);
drh51522cd2005-01-20 13:36:19 +00001328#endif
drh626a8792005-01-17 22:08:19 +00001329 return 0;
drh290c1942004-08-21 17:54:45 +00001330}
1331
1332/*
drhfec19aa2004-05-19 20:41:03 +00001333** Generate an instruction that will put the integer describe by
1334** text z[0..n-1] on the stack.
1335*/
1336static void codeInteger(Vdbe *v, const char *z, int n){
1337 int i;
drh6fec0762004-05-30 01:38:43 +00001338 if( sqlite3GetInt32(z, &i) ){
1339 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
1340 }else if( sqlite3FitsIn64Bits(z) ){
1341 sqlite3VdbeOp3(v, OP_Integer, 0, 0, z, n);
drhfec19aa2004-05-19 20:41:03 +00001342 }else{
1343 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n);
1344 }
1345}
1346
1347/*
drhcce7d172000-05-31 15:34:51 +00001348** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +00001349** expression and leave the result on the top of stack.
drhf2bc0132004-10-04 13:19:23 +00001350**
1351** This code depends on the fact that certain token values (ex: TK_EQ)
1352** are the same as opcode values (ex: OP_Eq) that implement the corresponding
1353** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
1354** the make process cause these values to align. Assert()s in the code
1355** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00001356*/
danielk19774adee202004-05-08 08:23:19 +00001357void sqlite3ExprCode(Parse *pParse, Expr *pExpr){
drhcce7d172000-05-31 15:34:51 +00001358 Vdbe *v = pParse->pVdbe;
1359 int op;
danielk19777977a172004-11-09 12:44:37 +00001360 if( v==0 ) return;
1361 if( pExpr==0 ){
1362 sqlite3VdbeAddOp(v, OP_String8, 0, 0); /* Empty expression evals to NULL */
1363 return;
1364 }
drhf2bc0132004-10-04 13:19:23 +00001365 op = pExpr->op;
1366 switch( op ){
drh967e8b72000-06-21 13:59:10 +00001367 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +00001368 if( pParse->useAgg ){
danielk19774adee202004-05-08 08:23:19 +00001369 sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drhc4a3c772001-04-04 11:48:57 +00001370 }else if( pExpr->iColumn>=0 ){
danielk19774adee202004-05-08 08:23:19 +00001371 sqlite3VdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn);
drh145716b2004-09-24 12:24:06 +00001372#ifndef NDEBUG
1373 if( pExpr->span.z && pExpr->span.n>0 && pExpr->span.n<100 ){
1374 VdbeComment((v, "# %T", &pExpr->span));
1375 }
1376#endif
drhc4a3c772001-04-04 11:48:57 +00001377 }else{
danielk19774adee202004-05-08 08:23:19 +00001378 sqlite3VdbeAddOp(v, OP_Recno, pExpr->iTable, 0);
drh22827922000-06-06 17:27:05 +00001379 }
drhcce7d172000-05-31 15:34:51 +00001380 break;
1381 }
1382 case TK_INTEGER: {
drhfec19aa2004-05-19 20:41:03 +00001383 codeInteger(v, pExpr->token.z, pExpr->token.n);
1384 break;
1385 }
1386 case TK_FLOAT:
1387 case TK_STRING: {
drhf2bc0132004-10-04 13:19:23 +00001388 assert( TK_FLOAT==OP_Real );
1389 assert( TK_STRING==OP_String8 );
drhfec19aa2004-05-19 20:41:03 +00001390 sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z, pExpr->token.n);
danielk19774adee202004-05-08 08:23:19 +00001391 sqlite3VdbeDequoteP3(v, -1);
drhcce7d172000-05-31 15:34:51 +00001392 break;
1393 }
danielk19775338a5f2005-01-20 13:03:10 +00001394#ifndef SQLITE_OMIT_BLOB_LITERAL
danielk1977c572ef72004-05-27 09:28:41 +00001395 case TK_BLOB: {
drhf2bc0132004-10-04 13:19:23 +00001396 assert( TK_BLOB==OP_HexBlob );
danielk1977c572ef72004-05-27 09:28:41 +00001397 sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z+1, pExpr->token.n-1);
1398 sqlite3VdbeDequoteP3(v, -1);
1399 break;
1400 }
danielk19775338a5f2005-01-20 13:03:10 +00001401#endif
drhcce7d172000-05-31 15:34:51 +00001402 case TK_NULL: {
danielk19770f69c1e2004-05-29 11:24:50 +00001403 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001404 break;
1405 }
drh50457892003-09-06 01:10:47 +00001406 case TK_VARIABLE: {
danielk19774adee202004-05-08 08:23:19 +00001407 sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0);
drh895d7472004-08-20 16:02:39 +00001408 if( pExpr->token.n>1 ){
1409 sqlite3VdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
1410 }
drh50457892003-09-06 01:10:47 +00001411 break;
1412 }
drh4e0cff62004-11-05 05:10:28 +00001413 case TK_REGISTER: {
1414 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iTable, 0);
1415 break;
1416 }
drhc9b84a12002-06-20 11:36:48 +00001417 case TK_LT:
1418 case TK_LE:
1419 case TK_GT:
1420 case TK_GE:
1421 case TK_NE:
1422 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00001423 assert( TK_LT==OP_Lt );
1424 assert( TK_LE==OP_Le );
1425 assert( TK_GT==OP_Gt );
1426 assert( TK_GE==OP_Ge );
1427 assert( TK_EQ==OP_Eq );
1428 assert( TK_NE==OP_Ne );
danielk1977a37cdde2004-05-16 11:15:36 +00001429 sqlite3ExprCode(pParse, pExpr->pLeft);
1430 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001431 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 0, 0);
danielk1977a37cdde2004-05-16 11:15:36 +00001432 break;
drhc9b84a12002-06-20 11:36:48 +00001433 }
drhcce7d172000-05-31 15:34:51 +00001434 case TK_AND:
1435 case TK_OR:
1436 case TK_PLUS:
1437 case TK_STAR:
1438 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00001439 case TK_REM:
1440 case TK_BITAND:
1441 case TK_BITOR:
drh17c40292004-07-21 02:53:29 +00001442 case TK_SLASH:
drhbf4133c2001-10-13 02:59:08 +00001443 case TK_LSHIFT:
drh855eb1c2004-08-31 13:45:11 +00001444 case TK_RSHIFT:
drh00400772000-06-16 20:51:26 +00001445 case TK_CONCAT: {
drhf2bc0132004-10-04 13:19:23 +00001446 assert( TK_AND==OP_And );
1447 assert( TK_OR==OP_Or );
1448 assert( TK_PLUS==OP_Add );
1449 assert( TK_MINUS==OP_Subtract );
1450 assert( TK_REM==OP_Remainder );
1451 assert( TK_BITAND==OP_BitAnd );
1452 assert( TK_BITOR==OP_BitOr );
1453 assert( TK_SLASH==OP_Divide );
1454 assert( TK_LSHIFT==OP_ShiftLeft );
1455 assert( TK_RSHIFT==OP_ShiftRight );
1456 assert( TK_CONCAT==OP_Concat );
danielk19774adee202004-05-08 08:23:19 +00001457 sqlite3ExprCode(pParse, pExpr->pLeft);
1458 sqlite3ExprCode(pParse, pExpr->pRight);
drh855eb1c2004-08-31 13:45:11 +00001459 sqlite3VdbeAddOp(v, op, 0, 0);
drh00400772000-06-16 20:51:26 +00001460 break;
1461 }
drhcce7d172000-05-31 15:34:51 +00001462 case TK_UMINUS: {
drhfec19aa2004-05-19 20:41:03 +00001463 Expr *pLeft = pExpr->pLeft;
1464 assert( pLeft );
1465 if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
1466 Token *p = &pLeft->token;
drh6e142f52000-06-08 13:36:40 +00001467 char *z = sqliteMalloc( p->n + 2 );
1468 sprintf(z, "-%.*s", p->n, p->z);
drhfec19aa2004-05-19 20:41:03 +00001469 if( pLeft->op==TK_FLOAT ){
1470 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001471 }else{
drhfec19aa2004-05-19 20:41:03 +00001472 codeInteger(v, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001473 }
drh6e142f52000-06-08 13:36:40 +00001474 sqliteFree(z);
1475 break;
1476 }
drh1ccde152000-06-17 13:12:39 +00001477 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +00001478 }
drhbf4133c2001-10-13 02:59:08 +00001479 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00001480 case TK_NOT: {
drhf2bc0132004-10-04 13:19:23 +00001481 assert( TK_BITNOT==OP_BitNot );
1482 assert( TK_NOT==OP_Not );
danielk19774adee202004-05-08 08:23:19 +00001483 sqlite3ExprCode(pParse, pExpr->pLeft);
1484 sqlite3VdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001485 break;
1486 }
1487 case TK_ISNULL:
1488 case TK_NOTNULL: {
1489 int dest;
drhf2bc0132004-10-04 13:19:23 +00001490 assert( TK_ISNULL==OP_IsNull );
1491 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00001492 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1493 sqlite3ExprCode(pParse, pExpr->pLeft);
1494 dest = sqlite3VdbeCurrentAddr(v) + 2;
1495 sqlite3VdbeAddOp(v, op, 1, dest);
1496 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);
drhf2bc0132004-10-04 13:19:23 +00001497 break;
drhcce7d172000-05-31 15:34:51 +00001498 }
drh22827922000-06-06 17:27:05 +00001499 case TK_AGG_FUNCTION: {
danielk19774adee202004-05-08 08:23:19 +00001500 sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drh22827922000-06-06 17:27:05 +00001501 break;
1502 }
danielk19777977a172004-11-09 12:44:37 +00001503 case TK_CDATE:
1504 case TK_CTIME:
1505 case TK_CTIMESTAMP:
drh4b59ab52002-08-24 18:24:51 +00001506 case TK_GLOB:
1507 case TK_LIKE:
drhcce7d172000-05-31 15:34:51 +00001508 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00001509 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00001510 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00001511 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00001512 int nId;
1513 const char *zId;
danielk1977682f68b2004-06-05 10:22:17 +00001514 int p2 = 0;
1515 int i;
danielk1977d8123362004-06-12 09:25:12 +00001516 u8 enc = pParse->db->enc;
danielk1977dc1bdc42004-06-11 10:51:27 +00001517 CollSeq *pColl = 0;
drh4b59ab52002-08-24 18:24:51 +00001518 getFunctionName(pExpr, &zId, &nId);
danielk1977d8123362004-06-12 09:25:12 +00001519 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001520 assert( pDef!=0 );
drhf9b596e2004-05-26 16:54:42 +00001521 nExpr = sqlite3ExprCodeExprList(pParse, pList);
danielk1977682f68b2004-06-05 10:22:17 +00001522 for(i=0; i<nExpr && i<32; i++){
danielk1977d02eb1f2004-06-06 09:44:03 +00001523 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
1524 p2 |= (1<<i);
1525 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001526 if( pDef->needCollSeq && !pColl ){
1527 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
1528 }
1529 }
1530 if( pDef->needCollSeq ){
1531 if( !pColl ) pColl = pParse->db->pDfltColl;
danielk1977d8123362004-06-12 09:25:12 +00001532 sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ);
danielk1977682f68b2004-06-05 10:22:17 +00001533 }
1534 sqlite3VdbeOp3(v, OP_Function, nExpr, p2, (char*)pDef, P3_FUNCDEF);
drhcce7d172000-05-31 15:34:51 +00001535 break;
1536 }
drh19a775c2000-06-05 18:54:46 +00001537 case TK_SELECT: {
drh1398ad32005-01-19 23:24:50 +00001538 if( pExpr->iTable>=0 ){
1539 sqlite3VdbeAddOp(v, OP_Gosub, 0, pExpr->iTable);
1540 VdbeComment((v, "# run subquery"));
1541 }
danielk19774adee202004-05-08 08:23:19 +00001542 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drhad6d9462004-09-19 02:15:24 +00001543 VdbeComment((v, "# load subquery result"));
drh19a775c2000-06-05 18:54:46 +00001544 break;
1545 }
drhfef52082000-06-06 01:50:43 +00001546 case TK_IN: {
1547 int addr;
drh94a11212004-09-25 13:12:14 +00001548 char affinity;
danielk1977e014a832004-05-17 10:48:57 +00001549
1550 /* Figure out the affinity to use to create a key from the results
1551 ** of the expression. affinityStr stores a static string suitable for
danielk1977ededfd52004-06-17 07:53:01 +00001552 ** P3 of OP_MakeRecord.
danielk1977e014a832004-05-17 10:48:57 +00001553 */
drh94a11212004-09-25 13:12:14 +00001554 affinity = comparisonAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +00001555
danielk19774adee202004-05-08 08:23:19 +00001556 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
danielk1977e014a832004-05-17 10:48:57 +00001557
1558 /* Code the <expr> from "<expr> IN (...)". The temporary table
1559 ** pExpr->iTable contains the values that make up the (...) set.
1560 */
danielk19774adee202004-05-08 08:23:19 +00001561 sqlite3ExprCode(pParse, pExpr->pLeft);
1562 addr = sqlite3VdbeCurrentAddr(v);
danielk1977e014a832004-05-17 10:48:57 +00001563 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); /* addr + 0 */
danielk19774adee202004-05-08 08:23:19 +00001564 sqlite3VdbeAddOp(v, OP_Pop, 2, 0);
danielk19770f69c1e2004-05-29 11:24:50 +00001565 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +00001566 sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7);
drh94a11212004-09-25 13:12:14 +00001567 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1); /* addr + 4 */
danielk1977e014a832004-05-17 10:48:57 +00001568 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7);
1569 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); /* addr + 6 */
1570
drhfef52082000-06-06 01:50:43 +00001571 break;
1572 }
1573 case TK_BETWEEN: {
drhbe5c89a2004-07-26 00:31:09 +00001574 Expr *pLeft = pExpr->pLeft;
1575 struct ExprList_item *pLItem = pExpr->pList->a;
1576 Expr *pRight = pLItem->pExpr;
1577 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001578 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001579 sqlite3ExprCode(pParse, pRight);
1580 codeCompare(pParse, pLeft, pRight, OP_Ge, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001581 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
drhbe5c89a2004-07-26 00:31:09 +00001582 pLItem++;
1583 pRight = pLItem->pExpr;
1584 sqlite3ExprCode(pParse, pRight);
1585 codeCompare(pParse, pLeft, pRight, OP_Le, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001586 sqlite3VdbeAddOp(v, OP_And, 0, 0);
drhfef52082000-06-06 01:50:43 +00001587 break;
1588 }
drh51e9a442004-01-16 16:42:53 +00001589 case TK_UPLUS:
drha2e00042002-01-22 03:13:42 +00001590 case TK_AS: {
danielk19774adee202004-05-08 08:23:19 +00001591 sqlite3ExprCode(pParse, pExpr->pLeft);
drha2e00042002-01-22 03:13:42 +00001592 break;
1593 }
drh17a7f8d2002-03-24 13:13:27 +00001594 case TK_CASE: {
1595 int expr_end_label;
drhf5905aa2002-05-26 20:54:33 +00001596 int jumpInst;
1597 int addr;
1598 int nExpr;
drh17a7f8d2002-03-24 13:13:27 +00001599 int i;
drhbe5c89a2004-07-26 00:31:09 +00001600 ExprList *pEList;
1601 struct ExprList_item *aListelem;
drh17a7f8d2002-03-24 13:13:27 +00001602
1603 assert(pExpr->pList);
1604 assert((pExpr->pList->nExpr % 2) == 0);
1605 assert(pExpr->pList->nExpr > 0);
drhbe5c89a2004-07-26 00:31:09 +00001606 pEList = pExpr->pList;
1607 aListelem = pEList->a;
1608 nExpr = pEList->nExpr;
danielk19774adee202004-05-08 08:23:19 +00001609 expr_end_label = sqlite3VdbeMakeLabel(v);
drh17a7f8d2002-03-24 13:13:27 +00001610 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001611 sqlite3ExprCode(pParse, pExpr->pLeft);
drh17a7f8d2002-03-24 13:13:27 +00001612 }
drhf5905aa2002-05-26 20:54:33 +00001613 for(i=0; i<nExpr; i=i+2){
drhbe5c89a2004-07-26 00:31:09 +00001614 sqlite3ExprCode(pParse, aListelem[i].pExpr);
drh17a7f8d2002-03-24 13:13:27 +00001615 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001616 sqlite3VdbeAddOp(v, OP_Dup, 1, 1);
drhbe5c89a2004-07-26 00:31:09 +00001617 jumpInst = codeCompare(pParse, pExpr->pLeft, aListelem[i].pExpr,
1618 OP_Ne, 0, 1);
danielk19774adee202004-05-08 08:23:19 +00001619 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001620 }else{
danielk19774adee202004-05-08 08:23:19 +00001621 jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001622 }
drhbe5c89a2004-07-26 00:31:09 +00001623 sqlite3ExprCode(pParse, aListelem[i+1].pExpr);
danielk19774adee202004-05-08 08:23:19 +00001624 sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label);
1625 addr = sqlite3VdbeCurrentAddr(v);
1626 sqlite3VdbeChangeP2(v, jumpInst, addr);
drh17a7f8d2002-03-24 13:13:27 +00001627 }
drhf570f012002-05-31 15:51:25 +00001628 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001629 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhf570f012002-05-31 15:51:25 +00001630 }
drh17a7f8d2002-03-24 13:13:27 +00001631 if( pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001632 sqlite3ExprCode(pParse, pExpr->pRight);
drh17a7f8d2002-03-24 13:13:27 +00001633 }else{
danielk19770f69c1e2004-05-29 11:24:50 +00001634 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
drh17a7f8d2002-03-24 13:13:27 +00001635 }
danielk19774adee202004-05-08 08:23:19 +00001636 sqlite3VdbeResolveLabel(v, expr_end_label);
danielk19776f349032002-06-11 02:25:40 +00001637 break;
1638 }
danielk19775338a5f2005-01-20 13:03:10 +00001639#ifndef SQLITE_OMIT_TRIGGER
danielk19776f349032002-06-11 02:25:40 +00001640 case TK_RAISE: {
1641 if( !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +00001642 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00001643 "RAISE() may only be used within a trigger-program");
danielk19776f349032002-06-11 02:25:40 +00001644 return;
1645 }
drhad6d9462004-09-19 02:15:24 +00001646 if( pExpr->iColumn!=OE_Ignore ){
1647 assert( pExpr->iColumn==OE_Rollback ||
1648 pExpr->iColumn == OE_Abort ||
1649 pExpr->iColumn == OE_Fail );
1650 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn,
1651 pExpr->token.z, pExpr->token.n);
1652 sqlite3VdbeDequoteP3(v, -1);
danielk19776f349032002-06-11 02:25:40 +00001653 } else {
drhad6d9462004-09-19 02:15:24 +00001654 assert( pExpr->iColumn == OE_Ignore );
1655 sqlite3VdbeAddOp(v, OP_ContextPop, 0, 0);
1656 sqlite3VdbeAddOp(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
1657 VdbeComment((v, "# raise(IGNORE)"));
danielk19776f349032002-06-11 02:25:40 +00001658 }
drh17a7f8d2002-03-24 13:13:27 +00001659 }
danielk19775338a5f2005-01-20 13:03:10 +00001660#endif
drh17a7f8d2002-03-24 13:13:27 +00001661 break;
drhcce7d172000-05-31 15:34:51 +00001662 }
drhcce7d172000-05-31 15:34:51 +00001663}
1664
1665/*
drh25303782004-12-07 15:41:48 +00001666** Generate code that evalutes the given expression and leaves the result
1667** on the stack. See also sqlite3ExprCode().
1668**
1669** This routine might also cache the result and modify the pExpr tree
1670** so that it will make use of the cached result on subsequent evaluations
1671** rather than evaluate the whole expression again. Trivial expressions are
1672** not cached. If the expression is cached, its result is stored in a
1673** memory location.
1674*/
1675void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr){
1676 Vdbe *v = pParse->pVdbe;
1677 int iMem;
1678 int addr1, addr2;
1679 if( v==0 ) return;
1680 addr1 = sqlite3VdbeCurrentAddr(v);
1681 sqlite3ExprCode(pParse, pExpr);
1682 addr2 = sqlite3VdbeCurrentAddr(v);
1683 if( addr2>addr1+1 || sqlite3VdbeGetOp(v, addr1)->opcode==OP_Function ){
1684 iMem = pExpr->iTable = pParse->nMem++;
1685 sqlite3VdbeAddOp(v, OP_MemStore, iMem, 0);
1686 pExpr->op = TK_REGISTER;
1687 }
1688}
1689
1690/*
drh268380c2004-02-25 13:47:31 +00001691** Generate code that pushes the value of every element of the given
drhf9b596e2004-05-26 16:54:42 +00001692** expression list onto the stack.
drh268380c2004-02-25 13:47:31 +00001693**
1694** Return the number of elements pushed onto the stack.
1695*/
danielk19774adee202004-05-08 08:23:19 +00001696int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00001697 Parse *pParse, /* Parsing context */
drhf9b596e2004-05-26 16:54:42 +00001698 ExprList *pList /* The expression list to be coded */
drh268380c2004-02-25 13:47:31 +00001699){
1700 struct ExprList_item *pItem;
1701 int i, n;
1702 Vdbe *v;
1703 if( pList==0 ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001704 v = sqlite3GetVdbe(pParse);
drh268380c2004-02-25 13:47:31 +00001705 n = pList->nExpr;
1706 for(pItem=pList->a, i=0; i<n; i++, pItem++){
danielk19774adee202004-05-08 08:23:19 +00001707 sqlite3ExprCode(pParse, pItem->pExpr);
drh268380c2004-02-25 13:47:31 +00001708 }
drhf9b596e2004-05-26 16:54:42 +00001709 return n;
drh268380c2004-02-25 13:47:31 +00001710}
1711
1712/*
drhcce7d172000-05-31 15:34:51 +00001713** Generate code for a boolean expression such that a jump is made
1714** to the label "dest" if the expression is true but execution
1715** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00001716**
1717** If the expression evaluates to NULL (neither true nor false), then
1718** take the jump if the jumpIfNull flag is true.
drhf2bc0132004-10-04 13:19:23 +00001719**
1720** This code depends on the fact that certain token values (ex: TK_EQ)
1721** are the same as opcode values (ex: OP_Eq) that implement the corresponding
1722** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
1723** the make process cause these values to align. Assert()s in the code
1724** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00001725*/
danielk19774adee202004-05-08 08:23:19 +00001726void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001727 Vdbe *v = pParse->pVdbe;
1728 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001729 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00001730 op = pExpr->op;
1731 switch( op ){
drhcce7d172000-05-31 15:34:51 +00001732 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001733 int d2 = sqlite3VdbeMakeLabel(v);
1734 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
1735 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
1736 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001737 break;
1738 }
1739 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001740 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
1741 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001742 break;
1743 }
1744 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001745 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001746 break;
1747 }
1748 case TK_LT:
1749 case TK_LE:
1750 case TK_GT:
1751 case TK_GE:
1752 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00001753 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00001754 assert( TK_LT==OP_Lt );
1755 assert( TK_LE==OP_Le );
1756 assert( TK_GT==OP_Gt );
1757 assert( TK_GE==OP_Ge );
1758 assert( TK_EQ==OP_Eq );
1759 assert( TK_NE==OP_Ne );
danielk19774adee202004-05-08 08:23:19 +00001760 sqlite3ExprCode(pParse, pExpr->pLeft);
1761 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001762 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001763 break;
1764 }
1765 case TK_ISNULL:
1766 case TK_NOTNULL: {
drhf2bc0132004-10-04 13:19:23 +00001767 assert( TK_ISNULL==OP_IsNull );
1768 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00001769 sqlite3ExprCode(pParse, pExpr->pLeft);
1770 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001771 break;
1772 }
drhfef52082000-06-06 01:50:43 +00001773 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00001774 /* The expression "x BETWEEN y AND z" is implemented as:
1775 **
1776 ** 1 IF (x < y) GOTO 3
1777 ** 2 IF (x <= z) GOTO <dest>
1778 ** 3 ...
1779 */
drhf5905aa2002-05-26 20:54:33 +00001780 int addr;
drhbe5c89a2004-07-26 00:31:09 +00001781 Expr *pLeft = pExpr->pLeft;
1782 Expr *pRight = pExpr->pList->a[0].pExpr;
1783 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001784 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001785 sqlite3ExprCode(pParse, pRight);
1786 addr = codeCompare(pParse, pLeft, pRight, OP_Lt, 0, !jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00001787
drhbe5c89a2004-07-26 00:31:09 +00001788 pRight = pExpr->pList->a[1].pExpr;
1789 sqlite3ExprCode(pParse, pRight);
1790 codeCompare(pParse, pLeft, pRight, OP_Le, dest, jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00001791
danielk19774adee202004-05-08 08:23:19 +00001792 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
1793 sqlite3VdbeChangeP2(v, addr, sqlite3VdbeCurrentAddr(v));
1794 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00001795 break;
1796 }
drhcce7d172000-05-31 15:34:51 +00001797 default: {
danielk19774adee202004-05-08 08:23:19 +00001798 sqlite3ExprCode(pParse, pExpr);
1799 sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001800 break;
1801 }
1802 }
1803}
1804
1805/*
drh66b89c82000-11-28 20:47:17 +00001806** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00001807** to the label "dest" if the expression is false but execution
1808** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00001809**
1810** If the expression evaluates to NULL (neither true nor false) then
1811** jump if jumpIfNull is true or fall through if jumpIfNull is false.
drhcce7d172000-05-31 15:34:51 +00001812*/
danielk19774adee202004-05-08 08:23:19 +00001813void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001814 Vdbe *v = pParse->pVdbe;
1815 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001816 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00001817
1818 /* The value of pExpr->op and op are related as follows:
1819 **
1820 ** pExpr->op op
1821 ** --------- ----------
1822 ** TK_ISNULL OP_NotNull
1823 ** TK_NOTNULL OP_IsNull
1824 ** TK_NE OP_Eq
1825 ** TK_EQ OP_Ne
1826 ** TK_GT OP_Le
1827 ** TK_LE OP_Gt
1828 ** TK_GE OP_Lt
1829 ** TK_LT OP_Ge
1830 **
1831 ** For other values of pExpr->op, op is undefined and unused.
1832 ** The value of TK_ and OP_ constants are arranged such that we
1833 ** can compute the mapping above using the following expression.
1834 ** Assert()s verify that the computation is correct.
1835 */
1836 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
1837
1838 /* Verify correct alignment of TK_ and OP_ constants
1839 */
1840 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
1841 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
1842 assert( pExpr->op!=TK_NE || op==OP_Eq );
1843 assert( pExpr->op!=TK_EQ || op==OP_Ne );
1844 assert( pExpr->op!=TK_LT || op==OP_Ge );
1845 assert( pExpr->op!=TK_LE || op==OP_Gt );
1846 assert( pExpr->op!=TK_GT || op==OP_Le );
1847 assert( pExpr->op!=TK_GE || op==OP_Lt );
1848
drhcce7d172000-05-31 15:34:51 +00001849 switch( pExpr->op ){
1850 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001851 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
1852 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001853 break;
1854 }
1855 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001856 int d2 = sqlite3VdbeMakeLabel(v);
1857 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
1858 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
1859 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001860 break;
1861 }
1862 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001863 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001864 break;
1865 }
1866 case TK_LT:
1867 case TK_LE:
1868 case TK_GT:
1869 case TK_GE:
1870 case TK_NE:
1871 case TK_EQ: {
danielk19774adee202004-05-08 08:23:19 +00001872 sqlite3ExprCode(pParse, pExpr->pLeft);
1873 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001874 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001875 break;
1876 }
drhcce7d172000-05-31 15:34:51 +00001877 case TK_ISNULL:
1878 case TK_NOTNULL: {
danielk19774adee202004-05-08 08:23:19 +00001879 sqlite3ExprCode(pParse, pExpr->pLeft);
1880 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001881 break;
1882 }
drhfef52082000-06-06 01:50:43 +00001883 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00001884 /* The expression is "x BETWEEN y AND z". It is implemented as:
1885 **
1886 ** 1 IF (x >= y) GOTO 3
1887 ** 2 GOTO <dest>
1888 ** 3 IF (x > z) GOTO <dest>
1889 */
drhfef52082000-06-06 01:50:43 +00001890 int addr;
drhbe5c89a2004-07-26 00:31:09 +00001891 Expr *pLeft = pExpr->pLeft;
1892 Expr *pRight = pExpr->pList->a[0].pExpr;
1893 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001894 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001895 sqlite3ExprCode(pParse, pRight);
danielk19774adee202004-05-08 08:23:19 +00001896 addr = sqlite3VdbeCurrentAddr(v);
drhbe5c89a2004-07-26 00:31:09 +00001897 codeCompare(pParse, pLeft, pRight, OP_Ge, addr+3, !jumpIfNull);
1898
danielk19774adee202004-05-08 08:23:19 +00001899 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
1900 sqlite3VdbeAddOp(v, OP_Goto, 0, dest);
drhbe5c89a2004-07-26 00:31:09 +00001901 pRight = pExpr->pList->a[1].pExpr;
1902 sqlite3ExprCode(pParse, pRight);
1903 codeCompare(pParse, pLeft, pRight, OP_Gt, dest, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00001904 break;
1905 }
drhcce7d172000-05-31 15:34:51 +00001906 default: {
danielk19774adee202004-05-08 08:23:19 +00001907 sqlite3ExprCode(pParse, pExpr);
1908 sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001909 break;
1910 }
1911 }
1912}
drh22827922000-06-06 17:27:05 +00001913
1914/*
1915** Do a deep comparison of two expression trees. Return TRUE (non-zero)
1916** if they are identical and return FALSE if they differ in any way.
1917*/
danielk19774adee202004-05-08 08:23:19 +00001918int sqlite3ExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00001919 int i;
1920 if( pA==0 ){
1921 return pB==0;
1922 }else if( pB==0 ){
1923 return 0;
1924 }
1925 if( pA->op!=pB->op ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001926 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
1927 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00001928 if( pA->pList ){
1929 if( pB->pList==0 ) return 0;
1930 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
1931 for(i=0; i<pA->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00001932 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00001933 return 0;
1934 }
1935 }
1936 }else if( pB->pList ){
1937 return 0;
1938 }
1939 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00001940 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drh22827922000-06-06 17:27:05 +00001941 if( pA->token.z ){
1942 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00001943 if( pB->token.n!=pA->token.n ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001944 if( sqlite3StrNICmp(pA->token.z, pB->token.z, pB->token.n)!=0 ) return 0;
drh22827922000-06-06 17:27:05 +00001945 }
1946 return 1;
1947}
1948
1949/*
1950** Add a new element to the pParse->aAgg[] array and return its index.
drh73b211a2005-01-18 04:00:42 +00001951** The new element is initialized to zero. The calling function is
1952** expected to fill it in.
drh22827922000-06-06 17:27:05 +00001953*/
1954static int appendAggInfo(Parse *pParse){
1955 if( (pParse->nAgg & 0x7)==0 ){
1956 int amt = pParse->nAgg + 8;
drh6d4abfb2001-10-22 02:58:08 +00001957 AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0]));
1958 if( aAgg==0 ){
drh22827922000-06-06 17:27:05 +00001959 return -1;
1960 }
drh6d4abfb2001-10-22 02:58:08 +00001961 pParse->aAgg = aAgg;
drh22827922000-06-06 17:27:05 +00001962 }
1963 memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0]));
1964 return pParse->nAgg++;
1965}
1966
1967/*
drh626a8792005-01-17 22:08:19 +00001968** This is an xFunc for walkExprTree() used to implement
1969** sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates
1970** for additional information.
drh22827922000-06-06 17:27:05 +00001971**
drh626a8792005-01-17 22:08:19 +00001972** This routine analyzes the aggregate function at pExpr.
drh22827922000-06-06 17:27:05 +00001973*/
drh626a8792005-01-17 22:08:19 +00001974static int analyzeAggregate(void *pArg, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00001975 int i;
1976 AggExpr *aAgg;
drh626a8792005-01-17 22:08:19 +00001977 Parse *pParse = (Parse*)pArg;
drh22827922000-06-06 17:27:05 +00001978
drh22827922000-06-06 17:27:05 +00001979 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00001980 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +00001981 aAgg = pParse->aAgg;
1982 for(i=0; i<pParse->nAgg; i++){
1983 if( aAgg[i].isAgg ) continue;
1984 if( aAgg[i].pExpr->iTable==pExpr->iTable
drh967e8b72000-06-21 13:59:10 +00001985 && aAgg[i].pExpr->iColumn==pExpr->iColumn ){
drh22827922000-06-06 17:27:05 +00001986 break;
1987 }
1988 }
1989 if( i>=pParse->nAgg ){
1990 i = appendAggInfo(pParse);
1991 if( i<0 ) return 1;
1992 pParse->aAgg[i].isAgg = 0;
1993 pParse->aAgg[i].pExpr = pExpr;
1994 }
drhaaf88722000-06-08 11:25:00 +00001995 pExpr->iAgg = i;
drh626a8792005-01-17 22:08:19 +00001996 return 1;
drh22827922000-06-06 17:27:05 +00001997 }
1998 case TK_AGG_FUNCTION: {
drh22827922000-06-06 17:27:05 +00001999 aAgg = pParse->aAgg;
2000 for(i=0; i<pParse->nAgg; i++){
2001 if( !aAgg[i].isAgg ) continue;
danielk19774adee202004-05-08 08:23:19 +00002002 if( sqlite3ExprCompare(aAgg[i].pExpr, pExpr) ){
drh22827922000-06-06 17:27:05 +00002003 break;
2004 }
2005 }
2006 if( i>=pParse->nAgg ){
danielk1977d8123362004-06-12 09:25:12 +00002007 u8 enc = pParse->db->enc;
drh22827922000-06-06 17:27:05 +00002008 i = appendAggInfo(pParse);
2009 if( i<0 ) return 1;
2010 pParse->aAgg[i].isAgg = 1;
2011 pParse->aAgg[i].pExpr = pExpr;
danielk19774adee202004-05-08 08:23:19 +00002012 pParse->aAgg[i].pFunc = sqlite3FindFunction(pParse->db,
drh6977fea2002-10-22 23:38:04 +00002013 pExpr->token.z, pExpr->token.n,
danielk1977d8123362004-06-12 09:25:12 +00002014 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
drh22827922000-06-06 17:27:05 +00002015 }
2016 pExpr->iAgg = i;
drh626a8792005-01-17 22:08:19 +00002017 return 1;
drh22827922000-06-06 17:27:05 +00002018 }
2019 }
drh626a8792005-01-17 22:08:19 +00002020 return 0;
2021}
2022
2023/*
2024** Analyze the given expression looking for aggregate functions and
2025** for variables that need to be added to the pParse->aAgg[] array.
2026** Make additional entries to the pParse->aAgg[] array as necessary.
2027**
2028** This routine should only be called after the expression has been
2029** analyzed by sqlite3ExprResolveNames().
2030**
2031** If errors are seen, leave an error message in zErrMsg and return
2032** the number of errors.
2033*/
2034int sqlite3ExprAnalyzeAggregates(Parse *pParse, Expr *pExpr){
2035 int nErr = pParse->nErr;
2036 walkExprTree(pExpr, analyzeAggregate, pParse);
2037 return pParse->nErr - nErr;
drh22827922000-06-06 17:27:05 +00002038}
drh8e0a2f92002-02-23 23:45:45 +00002039
2040/*
danielk1977d02eb1f2004-06-06 09:44:03 +00002041** Locate a user function given a name, a number of arguments and a flag
2042** indicating whether the function prefers UTF-16 over UTF-8. Return a
2043** pointer to the FuncDef structure that defines that function, or return
2044** NULL if the function does not exist.
drh8e0a2f92002-02-23 23:45:45 +00002045**
drh0bce8352002-02-28 00:41:10 +00002046** If the createFlag argument is true, then a new (blank) FuncDef
drh8e0a2f92002-02-23 23:45:45 +00002047** structure is created and liked into the "db" structure if a
2048** no matching function previously existed. When createFlag is true
2049** and the nArg parameter is -1, then only a function that accepts
2050** any number of arguments will be returned.
2051**
2052** If createFlag is false and nArg is -1, then the first valid
2053** function found is returned. A function is valid if either xFunc
2054** or xStep is non-zero.
danielk1977d02eb1f2004-06-06 09:44:03 +00002055**
2056** If createFlag is false, then a function with the required name and
2057** number of arguments may be returned even if the eTextRep flag does not
2058** match that requested.
drh8e0a2f92002-02-23 23:45:45 +00002059*/
danielk19774adee202004-05-08 08:23:19 +00002060FuncDef *sqlite3FindFunction(
drh9bb575f2004-09-06 17:24:11 +00002061 sqlite3 *db, /* An open database */
drh8e0a2f92002-02-23 23:45:45 +00002062 const char *zName, /* Name of the function. Not null-terminated */
2063 int nName, /* Number of characters in the name */
2064 int nArg, /* Number of arguments. -1 means any number */
danielk1977d8123362004-06-12 09:25:12 +00002065 u8 enc, /* Preferred text encoding */
drh8e0a2f92002-02-23 23:45:45 +00002066 int createFlag /* Create new entry if true and does not otherwise exist */
2067){
danielk1977d02eb1f2004-06-06 09:44:03 +00002068 FuncDef *p; /* Iterator variable */
2069 FuncDef *pFirst; /* First function with this name */
2070 FuncDef *pBest = 0; /* Best match found so far */
danielk1977d8123362004-06-12 09:25:12 +00002071 int bestmatch = 0;
danielk1977d02eb1f2004-06-06 09:44:03 +00002072
danielk1977d8123362004-06-12 09:25:12 +00002073
2074 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
danielk1977d02eb1f2004-06-06 09:44:03 +00002075 if( nArg<-1 ) nArg = -1;
2076
2077 pFirst = (FuncDef*)sqlite3HashFind(&db->aFunc, zName, nName);
2078 for(p=pFirst; p; p=p->pNext){
danielk1977d8123362004-06-12 09:25:12 +00002079 /* During the search for the best function definition, bestmatch is set
2080 ** as follows to indicate the quality of the match with the definition
2081 ** pointed to by pBest:
2082 **
2083 ** 0: pBest is NULL. No match has been found.
2084 ** 1: A variable arguments function that prefers UTF-8 when a UTF-16
2085 ** encoding is requested, or vice versa.
2086 ** 2: A variable arguments function that uses UTF-16BE when UTF-16LE is
2087 ** requested, or vice versa.
2088 ** 3: A variable arguments function using the same text encoding.
2089 ** 4: A function with the exact number of arguments requested that
2090 ** prefers UTF-8 when a UTF-16 encoding is requested, or vice versa.
2091 ** 5: A function with the exact number of arguments requested that
2092 ** prefers UTF-16LE when UTF-16BE is requested, or vice versa.
2093 ** 6: An exact match.
2094 **
2095 ** A larger value of 'matchqual' indicates a more desirable match.
2096 */
danielk1977e12c17b2004-06-23 12:35:14 +00002097 if( p->nArg==-1 || p->nArg==nArg || nArg==-1 ){
danielk1977d8123362004-06-12 09:25:12 +00002098 int match = 1; /* Quality of this match */
2099 if( p->nArg==nArg || nArg==-1 ){
2100 match = 4;
danielk1977d02eb1f2004-06-06 09:44:03 +00002101 }
danielk1977d8123362004-06-12 09:25:12 +00002102 if( enc==p->iPrefEnc ){
2103 match += 2;
danielk1977d02eb1f2004-06-06 09:44:03 +00002104 }
danielk1977d8123362004-06-12 09:25:12 +00002105 else if( (enc==SQLITE_UTF16LE && p->iPrefEnc==SQLITE_UTF16BE) ||
2106 (enc==SQLITE_UTF16BE && p->iPrefEnc==SQLITE_UTF16LE) ){
2107 match += 1;
2108 }
2109
2110 if( match>bestmatch ){
2111 pBest = p;
2112 bestmatch = match;
danielk1977d02eb1f2004-06-06 09:44:03 +00002113 }
2114 }
drh8e0a2f92002-02-23 23:45:45 +00002115 }
danielk1977d02eb1f2004-06-06 09:44:03 +00002116
danielk1977d8123362004-06-12 09:25:12 +00002117 /* If the createFlag parameter is true, and the seach did not reveal an
2118 ** exact match for the name, number of arguments and encoding, then add a
2119 ** new entry to the hash table and return it.
2120 */
2121 if( createFlag && bestmatch<6 &&
danielk1977d02eb1f2004-06-06 09:44:03 +00002122 (pBest = sqliteMalloc(sizeof(*pBest)+nName+1)) ){
2123 pBest->nArg = nArg;
2124 pBest->pNext = pFirst;
2125 pBest->zName = (char*)&pBest[1];
danielk1977d8123362004-06-12 09:25:12 +00002126 pBest->iPrefEnc = enc;
danielk1977d02eb1f2004-06-06 09:44:03 +00002127 memcpy(pBest->zName, zName, nName);
2128 pBest->zName[nName] = 0;
danielk19772c336542005-01-13 02:14:23 +00002129 if( pBest==sqlite3HashInsert(&db->aFunc,pBest->zName,nName,(void*)pBest) ){
2130 sqliteFree(pBest);
2131 return 0;
2132 }
drh8e0a2f92002-02-23 23:45:45 +00002133 }
danielk1977d02eb1f2004-06-06 09:44:03 +00002134
2135 if( pBest && (pBest->xStep || pBest->xFunc || createFlag) ){
2136 return pBest;
drh8e0a2f92002-02-23 23:45:45 +00002137 }
danielk1977d02eb1f2004-06-06 09:44:03 +00002138 return 0;
drh8e0a2f92002-02-23 23:45:45 +00002139}