blob: 8e125624af92894e7d6e55dd00a8355374f6a7d9 [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**
drh018d1a42005-01-15 01:52:31 +000015** $Id: expr.c,v 1.178 2005/01/15 01:52:32 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/*
drh53db1452004-05-20 13:54:53 +000065** pExpr is the left operand of a comparison operator. aff2 is the
66** type affinity of the right operand. This routine returns the
67** 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/*
drhfef52082000-06-06 01:50:43 +0000564** Walk an expression tree. Return 1 if the expression is constant
565** and 0 if it involves variables.
drh23989372002-05-21 13:43:04 +0000566**
567** For the purposes of this function, a double-quoted string (ex: "abc")
568** is considered a variable but a single-quoted string (ex: 'abc') is
569** a constant.
drhfef52082000-06-06 01:50:43 +0000570*/
danielk19774adee202004-05-08 08:23:19 +0000571int sqlite3ExprIsConstant(Expr *p){
drhfef52082000-06-06 01:50:43 +0000572 switch( p->op ){
573 case TK_ID:
drh967e8b72000-06-21 13:59:10 +0000574 case TK_COLUMN:
drhfef52082000-06-06 01:50:43 +0000575 case TK_DOT:
drh7bdc0c12003-04-19 17:27:24 +0000576 case TK_FUNCTION:
drhfef52082000-06-06 01:50:43 +0000577 return 0;
drh7bdc0c12003-04-19 17:27:24 +0000578 case TK_NULL:
drh23989372002-05-21 13:43:04 +0000579 case TK_STRING:
danielk1977c572ef72004-05-27 09:28:41 +0000580 case TK_BLOB:
drh92086432002-01-22 14:11:29 +0000581 case TK_INTEGER:
582 case TK_FLOAT:
drh50457892003-09-06 01:10:47 +0000583 case TK_VARIABLE:
danielk19777977a172004-11-09 12:44:37 +0000584 case TK_CTIME:
585 case TK_CTIMESTAMP:
586 case TK_CDATE:
drh92086432002-01-22 14:11:29 +0000587 return 1;
drhfef52082000-06-06 01:50:43 +0000588 default: {
danielk19774adee202004-05-08 08:23:19 +0000589 if( p->pLeft && !sqlite3ExprIsConstant(p->pLeft) ) return 0;
590 if( p->pRight && !sqlite3ExprIsConstant(p->pRight) ) return 0;
drhfef52082000-06-06 01:50:43 +0000591 if( p->pList ){
592 int i;
593 for(i=0; i<p->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +0000594 if( !sqlite3ExprIsConstant(p->pList->a[i].pExpr) ) return 0;
drhfef52082000-06-06 01:50:43 +0000595 }
596 }
drh92086432002-01-22 14:11:29 +0000597 return p->pLeft!=0 || p->pRight!=0 || (p->pList && p->pList->nExpr>0);
drhfef52082000-06-06 01:50:43 +0000598 }
599 }
drh92086432002-01-22 14:11:29 +0000600 return 0;
drhfef52082000-06-06 01:50:43 +0000601}
602
603/*
drh202b2df2004-01-06 01:13:46 +0000604** If the given expression codes a constant integer that is small enough
605** to fit in a 32-bit integer, return 1 and put the value of the integer
606** in *pValue. If the expression is not an integer or if it is too big
607** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
drhe4de1fe2002-06-02 16:09:01 +0000608*/
danielk19774adee202004-05-08 08:23:19 +0000609int sqlite3ExprIsInteger(Expr *p, int *pValue){
drhe4de1fe2002-06-02 16:09:01 +0000610 switch( p->op ){
611 case TK_INTEGER: {
drhfec19aa2004-05-19 20:41:03 +0000612 if( sqlite3GetInt32(p->token.z, pValue) ){
drh202b2df2004-01-06 01:13:46 +0000613 return 1;
614 }
615 break;
drhe4de1fe2002-06-02 16:09:01 +0000616 }
drh4b59ab52002-08-24 18:24:51 +0000617 case TK_UPLUS: {
danielk19774adee202004-05-08 08:23:19 +0000618 return sqlite3ExprIsInteger(p->pLeft, pValue);
drh4b59ab52002-08-24 18:24:51 +0000619 }
drhe4de1fe2002-06-02 16:09:01 +0000620 case TK_UMINUS: {
621 int v;
danielk19774adee202004-05-08 08:23:19 +0000622 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
drhe4de1fe2002-06-02 16:09:01 +0000623 *pValue = -v;
624 return 1;
625 }
626 break;
627 }
628 default: break;
629 }
630 return 0;
631}
632
633/*
drhc4a3c772001-04-04 11:48:57 +0000634** Return TRUE if the given string is a row-id column name.
635*/
danielk19774adee202004-05-08 08:23:19 +0000636int sqlite3IsRowid(const char *z){
637 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
638 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
639 if( sqlite3StrICmp(z, "OID")==0 ) return 1;
drhc4a3c772001-04-04 11:48:57 +0000640 return 0;
641}
642
643/*
drh8141f612004-01-25 22:44:58 +0000644** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
645** that name in the set of source tables in pSrcList and make the pExpr
646** expression node refer back to that source column. The following changes
647** are made to pExpr:
648**
649** pExpr->iDb Set the index in db->aDb[] of the database holding
650** the table.
651** pExpr->iTable Set to the cursor number for the table obtained
652** from pSrcList.
653** pExpr->iColumn Set to the column number within the table.
drh8141f612004-01-25 22:44:58 +0000654** pExpr->op Set to TK_COLUMN.
655** pExpr->pLeft Any expression this points to is deleted
656** pExpr->pRight Any expression this points to is deleted.
657**
658** The pDbToken is the name of the database (the "X"). This value may be
659** NULL meaning that name is of the form Y.Z or Z. Any available database
660** can be used. The pTableToken is the name of the table (the "Y"). This
661** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it
662** means that the form of the name is Z and that columns from any table
663** can be used.
664**
665** If the name cannot be resolved unambiguously, leave an error message
666** in pParse and return non-zero. Return zero on success.
667*/
668static int lookupName(
669 Parse *pParse, /* The parsing context */
670 Token *pDbToken, /* Name of the database containing table, or NULL */
671 Token *pTableToken, /* Name of table containing column, or NULL */
672 Token *pColumnToken, /* Name of the column. */
673 SrcList *pSrcList, /* List of tables used to resolve column names */
674 ExprList *pEList, /* List of expressions used to resolve "AS" */
675 Expr *pExpr /* Make this EXPR node point to the selected column */
676){
677 char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */
678 char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */
679 char *zCol = 0; /* Name of the column. The "Z" */
680 int i, j; /* Loop counters */
681 int cnt = 0; /* Number of matching column names */
682 int cntTab = 0; /* Number of matching table names */
drh51669862004-12-18 18:40:26 +0000683 sqlite3 *db = pParse->db; /* The database */
684 struct SrcList_item *pItem; /* Use for looping over pSrcList items */
685 struct SrcList_item *pMatch = 0; /* The matching pSrcList item */
drh8141f612004-01-25 22:44:58 +0000686
687 assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
drha99db3b2004-06-19 14:49:12 +0000688 zDb = sqlite3NameFromToken(pDbToken);
689 zTab = sqlite3NameFromToken(pTableToken);
690 zCol = sqlite3NameFromToken(pColumnToken);
danielk197724b03fd2004-05-10 10:34:34 +0000691 if( sqlite3_malloc_failed ){
drh8141f612004-01-25 22:44:58 +0000692 return 1; /* Leak memory (zDb and zTab) if malloc fails */
693 }
694 assert( zTab==0 || pEList==0 );
695
696 pExpr->iTable = -1;
drh51669862004-12-18 18:40:26 +0000697 for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
drh8141f612004-01-25 22:44:58 +0000698 Table *pTab = pItem->pTab;
699 Column *pCol;
700
701 if( pTab==0 ) continue;
702 assert( pTab->nCol>0 );
703 if( zTab ){
704 if( pItem->zAlias ){
705 char *zTabName = pItem->zAlias;
danielk19774adee202004-05-08 08:23:19 +0000706 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
drh8141f612004-01-25 22:44:58 +0000707 }else{
708 char *zTabName = pTab->zName;
danielk19774adee202004-05-08 08:23:19 +0000709 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
710 if( zDb!=0 && sqlite3StrICmp(db->aDb[pTab->iDb].zName, zDb)!=0 ){
drh8141f612004-01-25 22:44:58 +0000711 continue;
712 }
713 }
714 }
715 if( 0==(cntTab++) ){
716 pExpr->iTable = pItem->iCursor;
717 pExpr->iDb = pTab->iDb;
drh51669862004-12-18 18:40:26 +0000718 pMatch = pItem;
drh8141f612004-01-25 22:44:58 +0000719 }
720 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
danielk19774adee202004-05-08 08:23:19 +0000721 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
drh8141f612004-01-25 22:44:58 +0000722 cnt++;
723 pExpr->iTable = pItem->iCursor;
drh51669862004-12-18 18:40:26 +0000724 pMatch = pItem;
drh8141f612004-01-25 22:44:58 +0000725 pExpr->iDb = pTab->iDb;
726 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
727 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
danielk1977a37cdde2004-05-16 11:15:36 +0000728 pExpr->affinity = pTab->aCol[j].affinity;
danielk19770202b292004-06-09 09:55:16 +0000729 pExpr->pColl = pTab->aCol[j].pColl;
drh8141f612004-01-25 22:44:58 +0000730 break;
731 }
732 }
733 }
734
drhb7f91642004-10-31 02:22:47 +0000735#ifndef SQLITE_OMIT_TRIGGER
drh8141f612004-01-25 22:44:58 +0000736 /* If we have not already resolved the name, then maybe
737 ** it is a new.* or old.* trigger argument reference
738 */
739 if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
740 TriggerStack *pTriggerStack = pParse->trigStack;
741 Table *pTab = 0;
danielk19774adee202004-05-08 08:23:19 +0000742 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){
drh8141f612004-01-25 22:44:58 +0000743 pExpr->iTable = pTriggerStack->newIdx;
744 assert( pTriggerStack->pTab );
745 pTab = pTriggerStack->pTab;
danielk19774adee202004-05-08 08:23:19 +0000746 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab) == 0 ){
drh8141f612004-01-25 22:44:58 +0000747 pExpr->iTable = pTriggerStack->oldIdx;
748 assert( pTriggerStack->pTab );
749 pTab = pTriggerStack->pTab;
750 }
751
752 if( pTab ){
753 int j;
754 Column *pCol = pTab->aCol;
755
756 pExpr->iDb = pTab->iDb;
757 cntTab++;
758 for(j=0; j < pTab->nCol; j++, pCol++) {
danielk19774adee202004-05-08 08:23:19 +0000759 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
drh8141f612004-01-25 22:44:58 +0000760 cnt++;
761 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
danielk1977a37cdde2004-05-16 11:15:36 +0000762 pExpr->affinity = pTab->aCol[j].affinity;
danielk19770202b292004-06-09 09:55:16 +0000763 pExpr->pColl = pTab->aCol[j].pColl;
drh8141f612004-01-25 22:44:58 +0000764 break;
765 }
766 }
767 }
768 }
drhb7f91642004-10-31 02:22:47 +0000769#endif /* !defined(SQLITE_OMIT_TRIGGER) */
drh8141f612004-01-25 22:44:58 +0000770
771 /*
772 ** Perhaps the name is a reference to the ROWID
773 */
danielk19774adee202004-05-08 08:23:19 +0000774 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
drh8141f612004-01-25 22:44:58 +0000775 cnt = 1;
776 pExpr->iColumn = -1;
danielk1977a37cdde2004-05-16 11:15:36 +0000777 pExpr->affinity = SQLITE_AFF_INTEGER;
drh8141f612004-01-25 22:44:58 +0000778 }
779
780 /*
781 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
782 ** might refer to an result-set alias. This happens, for example, when
783 ** we are resolving names in the WHERE clause of the following command:
784 **
785 ** SELECT a+b AS x FROM table WHERE x<10;
786 **
787 ** In cases like this, replace pExpr with a copy of the expression that
788 ** forms the result set entry ("a+b" in the example) and return immediately.
789 ** Note that the expression in the result set should have already been
790 ** resolved by the time the WHERE clause is resolved.
791 */
792 if( cnt==0 && pEList!=0 ){
793 for(j=0; j<pEList->nExpr; j++){
794 char *zAs = pEList->a[j].zName;
danielk19774adee202004-05-08 08:23:19 +0000795 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
drh8141f612004-01-25 22:44:58 +0000796 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
797 pExpr->op = TK_AS;
798 pExpr->iColumn = j;
danielk19774adee202004-05-08 08:23:19 +0000799 pExpr->pLeft = sqlite3ExprDup(pEList->a[j].pExpr);
drh8141f612004-01-25 22:44:58 +0000800 sqliteFree(zCol);
801 assert( zTab==0 && zDb==0 );
802 return 0;
803 }
804 }
805 }
806
807 /*
808 ** If X and Y are NULL (in other words if only the column name Z is
809 ** supplied) and the value of Z is enclosed in double-quotes, then
810 ** Z is a string literal if it doesn't match any column names. In that
811 ** case, we need to return right away and not make any changes to
812 ** pExpr.
813 */
814 if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
815 sqliteFree(zCol);
816 return 0;
817 }
818
819 /*
820 ** cnt==0 means there was not match. cnt>1 means there were two or
821 ** more matches. Either way, we have an error.
822 */
823 if( cnt!=1 ){
824 char *z = 0;
825 char *zErr;
826 zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s";
827 if( zDb ){
danielk19774adee202004-05-08 08:23:19 +0000828 sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, 0);
drh8141f612004-01-25 22:44:58 +0000829 }else if( zTab ){
danielk19774adee202004-05-08 08:23:19 +0000830 sqlite3SetString(&z, zTab, ".", zCol, 0);
drh8141f612004-01-25 22:44:58 +0000831 }else{
832 z = sqliteStrDup(zCol);
833 }
danielk19774adee202004-05-08 08:23:19 +0000834 sqlite3ErrorMsg(pParse, zErr, z);
drh8141f612004-01-25 22:44:58 +0000835 sqliteFree(z);
836 }
837
drh51669862004-12-18 18:40:26 +0000838 /* If a column from a table in pSrcList is referenced, then record
839 ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes
840 ** bit 0 to be set. Column 1 sets bit 1. And so forth. If the
841 ** column number is greater than the number of bits in the bitmask
842 ** then set the high-order bit of the bitmask.
843 */
844 if( pExpr->iColumn>=0 && pMatch!=0 ){
845 int n = pExpr->iColumn;
846 if( n>=sizeof(Bitmask)*8 ){
847 n = sizeof(Bitmask)*8-1;
848 }
849 assert( pMatch->iCursor==pExpr->iTable );
850 pMatch->colUsed |= 1<<n;
851 }
852
drh8141f612004-01-25 22:44:58 +0000853 /* Clean up and return
854 */
855 sqliteFree(zDb);
856 sqliteFree(zTab);
857 sqliteFree(zCol);
danielk19774adee202004-05-08 08:23:19 +0000858 sqlite3ExprDelete(pExpr->pLeft);
drh8141f612004-01-25 22:44:58 +0000859 pExpr->pLeft = 0;
danielk19774adee202004-05-08 08:23:19 +0000860 sqlite3ExprDelete(pExpr->pRight);
drh8141f612004-01-25 22:44:58 +0000861 pExpr->pRight = 0;
862 pExpr->op = TK_COLUMN;
danielk19774adee202004-05-08 08:23:19 +0000863 sqlite3AuthRead(pParse, pExpr, pSrcList);
drh8141f612004-01-25 22:44:58 +0000864 return cnt!=1;
865}
866
867/*
drhcce7d172000-05-31 15:34:51 +0000868** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +0000869** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +0000870** index to the table in the table list and a column offset. The
871** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
872** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +0000873** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +0000874** VDBE cursor number for a cursor that is pointing into the referenced
875** table. The Expr.iColumn value is changed to the index of the column
876** of the referenced table. The Expr.iColumn value for the special
877** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
878** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +0000879**
drhfef52082000-06-06 01:50:43 +0000880** We also check for instances of the IN operator. IN comes in two
881** forms:
882**
883** expr IN (exprlist)
884** and
885** expr IN (SELECT ...)
886**
887** The first form is handled by creating a set holding the list
888** of allowed values. The second form causes the SELECT to generate
889** a temporary table.
890**
891** This routine also looks for scalar SELECTs that are part of an expression.
drh19a775c2000-06-05 18:54:46 +0000892** If it finds any, it generates code to write the value of that select
893** into a memory cell.
drhcce7d172000-05-31 15:34:51 +0000894**
drh967e8b72000-06-21 13:59:10 +0000895** Unknown columns or tables provoke an error. The function returns
drhcce7d172000-05-31 15:34:51 +0000896** the number of errors seen and leaves an error message on pParse->zErrMsg.
897*/
danielk19774adee202004-05-08 08:23:19 +0000898int sqlite3ExprResolveIds(
drha2e00042002-01-22 03:13:42 +0000899 Parse *pParse, /* The parser context */
drh8141f612004-01-25 22:44:58 +0000900 SrcList *pSrcList, /* List of tables used to resolve column names */
drha2e00042002-01-22 03:13:42 +0000901 ExprList *pEList, /* List of expressions used to resolve "AS" */
902 Expr *pExpr /* The expression to be analyzed. */
903){
drh6a3ea0e2003-05-02 14:32:12 +0000904 int i;
905
drh8141f612004-01-25 22:44:58 +0000906 if( pExpr==0 || pSrcList==0 ) return 0;
907 for(i=0; i<pSrcList->nSrc; i++){
908 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab );
drh6a3ea0e2003-05-02 14:32:12 +0000909 }
drhcce7d172000-05-31 15:34:51 +0000910 switch( pExpr->op ){
drh23989372002-05-21 13:43:04 +0000911 /* Double-quoted strings (ex: "abc") are used as identifiers if
912 ** possible. Otherwise they remain as strings. Single-quoted
913 ** strings (ex: 'abc') are always string literals.
914 */
915 case TK_STRING: {
916 if( pExpr->token.z[0]=='\'' ) break;
917 /* Fall thru into the TK_ID case if this is a double-quoted string */
918 }
drh8141f612004-01-25 22:44:58 +0000919 /* A lone identifier is the name of a columnd.
drha2e00042002-01-22 03:13:42 +0000920 */
drhcce7d172000-05-31 15:34:51 +0000921 case TK_ID: {
drh8141f612004-01-25 22:44:58 +0000922 if( lookupName(pParse, 0, 0, &pExpr->token, pSrcList, pEList, pExpr) ){
drhcce7d172000-05-31 15:34:51 +0000923 return 1;
drhed6c8672003-01-12 18:02:16 +0000924 }
drhcce7d172000-05-31 15:34:51 +0000925 break;
926 }
927
drhd24cc422003-03-27 12:51:24 +0000928 /* A table name and column name: ID.ID
929 ** Or a database, table and column: ID.ID.ID
930 */
drhcce7d172000-05-31 15:34:51 +0000931 case TK_DOT: {
drh8141f612004-01-25 22:44:58 +0000932 Token *pColumn;
933 Token *pTable;
934 Token *pDb;
935 Expr *pRight;
drhcce7d172000-05-31 15:34:51 +0000936
drhcce7d172000-05-31 15:34:51 +0000937 pRight = pExpr->pRight;
drhd24cc422003-03-27 12:51:24 +0000938 if( pRight->op==TK_ID ){
drh8141f612004-01-25 22:44:58 +0000939 pDb = 0;
940 pTable = &pExpr->pLeft->token;
941 pColumn = &pRight->token;
drhd24cc422003-03-27 12:51:24 +0000942 }else{
drh8141f612004-01-25 22:44:58 +0000943 assert( pRight->op==TK_DOT );
944 pDb = &pExpr->pLeft->token;
945 pTable = &pRight->pLeft->token;
946 pColumn = &pRight->pRight->token;
drhd24cc422003-03-27 12:51:24 +0000947 }
drh8141f612004-01-25 22:44:58 +0000948 if( lookupName(pParse, pDb, pTable, pColumn, pSrcList, 0, pExpr) ){
drhdaffd0e2001-04-11 14:28:42 +0000949 return 1;
950 }
drhcce7d172000-05-31 15:34:51 +0000951 break;
952 }
953
drhfef52082000-06-06 01:50:43 +0000954 case TK_IN: {
danielk1977e014a832004-05-17 10:48:57 +0000955 char affinity;
danielk19774adee202004-05-08 08:23:19 +0000956 Vdbe *v = sqlite3GetVdbe(pParse);
drhd3d39e92004-05-20 22:16:29 +0000957 KeyInfo keyInfo;
danielk19770202b292004-06-09 09:55:16 +0000958 int addr; /* Address of OP_OpenTemp instruction */
drhd3d39e92004-05-20 22:16:29 +0000959
drhfef52082000-06-06 01:50:43 +0000960 if( v==0 ) return 1;
danielk19774adee202004-05-08 08:23:19 +0000961 if( sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pLeft) ){
drhcfab11b2000-06-06 03:31:22 +0000962 return 1;
963 }
danielk1977bf3b7212004-05-18 10:06:24 +0000964 affinity = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +0000965
966 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
967 ** expression it is handled the same way. A temporary table is
968 ** filled with single-field index keys representing the results
969 ** from the SELECT or the <exprlist>.
970 **
971 ** If the 'x' expression is a column value, or the SELECT...
972 ** statement returns a column value, then the affinity of that
973 ** column is used to build the index keys. If both 'x' and the
974 ** SELECT... statement are columns, then numeric affinity is used
975 ** if either column has NUMERIC or INTEGER affinity. If neither
976 ** 'x' nor the SELECT... statement are columns, then numeric affinity
977 ** is used.
978 */
979 pExpr->iTable = pParse->nTab++;
danielk19770202b292004-06-09 09:55:16 +0000980 addr = sqlite3VdbeAddOp(v, OP_OpenTemp, pExpr->iTable, 0);
drhd3d39e92004-05-20 22:16:29 +0000981 memset(&keyInfo, 0, sizeof(keyInfo));
982 keyInfo.nField = 1;
drhf3218fe2004-05-28 08:21:02 +0000983 sqlite3VdbeAddOp(v, OP_SetNumColumns, pExpr->iTable, 1);
danielk1977e014a832004-05-17 10:48:57 +0000984
drhfef52082000-06-06 01:50:43 +0000985 if( pExpr->pSelect ){
986 /* Case 1: expr IN (SELECT ...)
987 **
danielk1977e014a832004-05-17 10:48:57 +0000988 ** Generate code to write the results of the select into the temporary
989 ** table allocated and opened above.
drhfef52082000-06-06 01:50:43 +0000990 */
danielk1977e014a832004-05-17 10:48:57 +0000991 int iParm = pExpr->iTable + (((int)affinity)<<16);
drhbe5c89a2004-07-26 00:31:09 +0000992 ExprList *pEList;
danielk1977e014a832004-05-17 10:48:57 +0000993 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
danielk1977bf3b7212004-05-18 10:06:24 +0000994 sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +0000995 pEList = pExpr->pSelect->pEList;
996 if( pEList && pEList->nExpr>0 ){
danielk19777cedc8d2004-06-10 10:50:08 +0000997 keyInfo.aColl[0] = binaryCompareCollSeq(pParse, pExpr->pLeft,
drhbe5c89a2004-07-26 00:31:09 +0000998 pEList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +0000999 }
drhfef52082000-06-06 01:50:43 +00001000 }else if( pExpr->pList ){
1001 /* Case 2: expr IN (exprlist)
1002 **
danielk1977e014a832004-05-17 10:48:57 +00001003 ** For each expression, build an index key from the evaluation and
1004 ** store it in the temporary table. If <expr> is a column, then use
1005 ** that columns affinity when building index keys. If <expr> is not
1006 ** a column, use numeric affinity.
drhfef52082000-06-06 01:50:43 +00001007 */
danielk1977e014a832004-05-17 10:48:57 +00001008 int i;
danielk1977e014a832004-05-17 10:48:57 +00001009 if( !affinity ){
1010 affinity = SQLITE_AFF_NUMERIC;
1011 }
danielk19770202b292004-06-09 09:55:16 +00001012 keyInfo.aColl[0] = pExpr->pLeft->pColl;
danielk1977e014a832004-05-17 10:48:57 +00001013
1014 /* Loop through each expression in <exprlist>. */
drhfef52082000-06-06 01:50:43 +00001015 for(i=0; i<pExpr->pList->nExpr; i++){
1016 Expr *pE2 = pExpr->pList->a[i].pExpr;
danielk1977e014a832004-05-17 10:48:57 +00001017
1018 /* Check that the expression is constant and valid. */
danielk19774adee202004-05-08 08:23:19 +00001019 if( !sqlite3ExprIsConstant(pE2) ){
1020 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00001021 "right-hand side of IN operator must be constant");
drhfef52082000-06-06 01:50:43 +00001022 return 1;
1023 }
danielk19774adee202004-05-08 08:23:19 +00001024 if( sqlite3ExprCheck(pParse, pE2, 0, 0) ){
drh4794b982000-06-06 13:54:14 +00001025 return 1;
1026 }
danielk1977e014a832004-05-17 10:48:57 +00001027
1028 /* Evaluate the expression and insert it into the temp table */
1029 sqlite3ExprCode(pParse, pE2);
drh94a11212004-09-25 13:12:14 +00001030 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1);
danielk19770f69c1e2004-05-29 11:24:50 +00001031 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +00001032 sqlite3VdbeAddOp(v, OP_PutStrKey, pExpr->iTable, 0);
drhfef52082000-06-06 01:50:43 +00001033 }
1034 }
danielk19770202b292004-06-09 09:55:16 +00001035 sqlite3VdbeChangeP3(v, addr, (void *)&keyInfo, P3_KEYINFO);
1036
drhcfab11b2000-06-06 03:31:22 +00001037 break;
drhfef52082000-06-06 01:50:43 +00001038 }
1039
drh19a775c2000-06-05 18:54:46 +00001040 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +00001041 /* This has to be a scalar SELECT. Generate code to put the
1042 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +00001043 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +00001044 */
drh967e8b72000-06-21 13:59:10 +00001045 pExpr->iColumn = pParse->nMem++;
danielk1977bf3b7212004-05-18 10:06:24 +00001046 if(sqlite3Select(pParse, pExpr->pSelect, SRT_Mem,pExpr->iColumn,0,0,0,0)){
drh19a775c2000-06-05 18:54:46 +00001047 return 1;
1048 }
1049 break;
1050 }
1051
drhcce7d172000-05-31 15:34:51 +00001052 /* For all else, just recursively walk the tree */
1053 default: {
drh4794b982000-06-06 13:54:14 +00001054 if( pExpr->pLeft
danielk19774adee202004-05-08 08:23:19 +00001055 && sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pLeft) ){
drhcce7d172000-05-31 15:34:51 +00001056 return 1;
1057 }
1058 if( pExpr->pRight
danielk19774adee202004-05-08 08:23:19 +00001059 && sqlite3ExprResolveIds(pParse, pSrcList, pEList, pExpr->pRight) ){
drhcce7d172000-05-31 15:34:51 +00001060 return 1;
1061 }
1062 if( pExpr->pList ){
1063 int i;
1064 ExprList *pList = pExpr->pList;
1065 for(i=0; i<pList->nExpr; i++){
drh832508b2002-03-02 17:04:07 +00001066 Expr *pArg = pList->a[i].pExpr;
danielk19774adee202004-05-08 08:23:19 +00001067 if( sqlite3ExprResolveIds(pParse, pSrcList, pEList, pArg) ){
drhcce7d172000-05-31 15:34:51 +00001068 return 1;
1069 }
1070 }
1071 }
1072 }
1073 }
1074 return 0;
1075}
1076
drhcce7d172000-05-31 15:34:51 +00001077/*
drh4b59ab52002-08-24 18:24:51 +00001078** pExpr is a node that defines a function of some kind. It might
1079** be a syntactic function like "count(x)" or it might be a function
1080** that implements an operator, like "a LIKE b".
1081**
1082** This routine makes *pzName point to the name of the function and
1083** *pnName hold the number of characters in the function name.
1084*/
1085static void getFunctionName(Expr *pExpr, const char **pzName, int *pnName){
1086 switch( pExpr->op ){
1087 case TK_FUNCTION: {
1088 *pzName = pExpr->token.z;
drh6977fea2002-10-22 23:38:04 +00001089 *pnName = pExpr->token.n;
drh4b59ab52002-08-24 18:24:51 +00001090 break;
1091 }
1092 case TK_LIKE: {
1093 *pzName = "like";
1094 *pnName = 4;
1095 break;
1096 }
1097 case TK_GLOB: {
1098 *pzName = "glob";
1099 *pnName = 4;
1100 break;
1101 }
danielk19777977a172004-11-09 12:44:37 +00001102 case TK_CTIME: {
1103 *pzName = "current_time";
1104 *pnName = 12;
1105 break;
1106 }
1107 case TK_CDATE: {
1108 *pzName = "current_date";
1109 *pnName = 12;
1110 break;
1111 }
1112 case TK_CTIMESTAMP: {
1113 *pzName = "current_timestamp";
1114 *pnName = 17;
1115 break;
1116 }
drh4b59ab52002-08-24 18:24:51 +00001117 default: {
1118 *pzName = "can't happen";
1119 *pnName = 12;
1120 break;
1121 }
1122 }
1123}
1124
1125/*
drhcce7d172000-05-31 15:34:51 +00001126** Error check the functions in an expression. Make sure all
1127** function names are recognized and all functions have the correct
1128** number of arguments. Leave an error message in pParse->zErrMsg
1129** if anything is amiss. Return the number of errors.
1130**
1131** if pIsAgg is not null and this expression is an aggregate function
1132** (like count(*) or max(value)) then write a 1 into *pIsAgg.
1133*/
danielk19774adee202004-05-08 08:23:19 +00001134int sqlite3ExprCheck(Parse *pParse, Expr *pExpr, int allowAgg, int *pIsAgg){
drhcce7d172000-05-31 15:34:51 +00001135 int nErr = 0;
1136 if( pExpr==0 ) return 0;
drhcce7d172000-05-31 15:34:51 +00001137 switch( pExpr->op ){
danielk19777977a172004-11-09 12:44:37 +00001138 case TK_CTIME:
1139 case TK_CTIMESTAMP:
1140 case TK_CDATE:
1141 /* Note: The above three were a seperate case in sqlmoto. Reason? */
drh4b59ab52002-08-24 18:24:51 +00001142 case TK_GLOB:
1143 case TK_LIKE:
drhcce7d172000-05-31 15:34:51 +00001144 case TK_FUNCTION: {
drhc9b84a12002-06-20 11:36:48 +00001145 int n = pExpr->pList ? pExpr->pList->nExpr : 0; /* Number of arguments */
1146 int no_such_func = 0; /* True if no such function exists */
drhc9b84a12002-06-20 11:36:48 +00001147 int wrong_num_args = 0; /* True if wrong number of arguments */
1148 int is_agg = 0; /* True if is an aggregate function */
drhcce7d172000-05-31 15:34:51 +00001149 int i;
drh4b59ab52002-08-24 18:24:51 +00001150 int nId; /* Number of characters in function name */
1151 const char *zId; /* The function name. */
drh0bce8352002-02-28 00:41:10 +00001152 FuncDef *pDef;
danielk1977d8123362004-06-12 09:25:12 +00001153 int enc = pParse->db->enc;
drh0bce8352002-02-28 00:41:10 +00001154
drh4b59ab52002-08-24 18:24:51 +00001155 getFunctionName(pExpr, &zId, &nId);
danielk1977d8123362004-06-12 09:25:12 +00001156 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001157 if( pDef==0 ){
danielk1977d8123362004-06-12 09:25:12 +00001158 pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001159 if( pDef==0 ){
drh268380c2004-02-25 13:47:31 +00001160 no_such_func = 1;
drh0bce8352002-02-28 00:41:10 +00001161 }else{
1162 wrong_num_args = 1;
drhcce7d172000-05-31 15:34:51 +00001163 }
drh0bce8352002-02-28 00:41:10 +00001164 }else{
1165 is_agg = pDef->xFunc==0;
drhcce7d172000-05-31 15:34:51 +00001166 }
drh8e0a2f92002-02-23 23:45:45 +00001167 if( is_agg && !allowAgg ){
danielk19774adee202004-05-08 08:23:19 +00001168 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId, zId);
drh8e0a2f92002-02-23 23:45:45 +00001169 nErr++;
1170 is_agg = 0;
1171 }else if( no_such_func ){
danielk19774adee202004-05-08 08:23:19 +00001172 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
drhcce7d172000-05-31 15:34:51 +00001173 nErr++;
drh8e0a2f92002-02-23 23:45:45 +00001174 }else if( wrong_num_args ){
danielk19774adee202004-05-08 08:23:19 +00001175 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
drhf7a9e1a2004-02-22 18:40:56 +00001176 nId, zId);
drh8e0a2f92002-02-23 23:45:45 +00001177 nErr++;
drhcce7d172000-05-31 15:34:51 +00001178 }
drhf7a9e1a2004-02-22 18:40:56 +00001179 if( is_agg ){
1180 pExpr->op = TK_AGG_FUNCTION;
1181 if( pIsAgg ) *pIsAgg = 1;
1182 }
drhcce7d172000-05-31 15:34:51 +00001183 for(i=0; nErr==0 && i<n; i++){
danielk19774adee202004-05-08 08:23:19 +00001184 nErr = sqlite3ExprCheck(pParse, pExpr->pList->a[i].pExpr,
drh4cfa7932000-06-08 15:10:46 +00001185 allowAgg && !is_agg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +00001186 }
danielk19770202b292004-06-09 09:55:16 +00001187 /* FIX ME: Compute pExpr->affinity based on the expected return
1188 ** type of the function
1189 */
drhcce7d172000-05-31 15:34:51 +00001190 }
1191 default: {
1192 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001193 nErr = sqlite3ExprCheck(pParse, pExpr->pLeft, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +00001194 }
1195 if( nErr==0 && pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001196 nErr = sqlite3ExprCheck(pParse, pExpr->pRight, allowAgg, pIsAgg);
drhcce7d172000-05-31 15:34:51 +00001197 }
drhfef52082000-06-06 01:50:43 +00001198 if( nErr==0 && pExpr->pList ){
1199 int n = pExpr->pList->nExpr;
1200 int i;
1201 for(i=0; nErr==0 && i<n; i++){
drh22827922000-06-06 17:27:05 +00001202 Expr *pE2 = pExpr->pList->a[i].pExpr;
danielk19774adee202004-05-08 08:23:19 +00001203 nErr = sqlite3ExprCheck(pParse, pE2, allowAgg, pIsAgg);
drhfef52082000-06-06 01:50:43 +00001204 }
1205 }
drhcce7d172000-05-31 15:34:51 +00001206 break;
1207 }
1208 }
1209 return nErr;
1210}
1211
1212/*
drh290c1942004-08-21 17:54:45 +00001213** Call sqlite3ExprResolveIds() followed by sqlite3ExprCheck().
1214**
1215** This routine is provided as a convenience since it is very common
1216** to call ResolveIds() and Check() back to back.
1217*/
1218int sqlite3ExprResolveAndCheck(
1219 Parse *pParse, /* The parser context */
1220 SrcList *pSrcList, /* List of tables used to resolve column names */
1221 ExprList *pEList, /* List of expressions used to resolve "AS" */
1222 Expr *pExpr, /* The expression to be analyzed. */
1223 int allowAgg, /* True to allow aggregate expressions */
1224 int *pIsAgg /* Set to TRUE if aggregates are found */
1225){
1226 if( pExpr==0 ) return 0;
1227 if( sqlite3ExprResolveIds(pParse,pSrcList,pEList,pExpr) ){
1228 return 1;
1229 }
1230 return sqlite3ExprCheck(pParse, pExpr, allowAgg, pIsAgg);
1231}
1232
1233/*
drhfec19aa2004-05-19 20:41:03 +00001234** Generate an instruction that will put the integer describe by
1235** text z[0..n-1] on the stack.
1236*/
1237static void codeInteger(Vdbe *v, const char *z, int n){
1238 int i;
drh6fec0762004-05-30 01:38:43 +00001239 if( sqlite3GetInt32(z, &i) ){
1240 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
1241 }else if( sqlite3FitsIn64Bits(z) ){
1242 sqlite3VdbeOp3(v, OP_Integer, 0, 0, z, n);
drhfec19aa2004-05-19 20:41:03 +00001243 }else{
1244 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n);
1245 }
1246}
1247
1248/*
drhcce7d172000-05-31 15:34:51 +00001249** Generate code into the current Vdbe to evaluate the given
drh1ccde152000-06-17 13:12:39 +00001250** expression and leave the result on the top of stack.
drhf2bc0132004-10-04 13:19:23 +00001251**
1252** This code depends on the fact that certain token values (ex: TK_EQ)
1253** are the same as opcode values (ex: OP_Eq) that implement the corresponding
1254** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
1255** the make process cause these values to align. Assert()s in the code
1256** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00001257*/
danielk19774adee202004-05-08 08:23:19 +00001258void sqlite3ExprCode(Parse *pParse, Expr *pExpr){
drhcce7d172000-05-31 15:34:51 +00001259 Vdbe *v = pParse->pVdbe;
1260 int op;
danielk19777977a172004-11-09 12:44:37 +00001261 if( v==0 ) return;
1262 if( pExpr==0 ){
1263 sqlite3VdbeAddOp(v, OP_String8, 0, 0); /* Empty expression evals to NULL */
1264 return;
1265 }
drhf2bc0132004-10-04 13:19:23 +00001266 op = pExpr->op;
1267 switch( op ){
drh967e8b72000-06-21 13:59:10 +00001268 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +00001269 if( pParse->useAgg ){
danielk19774adee202004-05-08 08:23:19 +00001270 sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drhc4a3c772001-04-04 11:48:57 +00001271 }else if( pExpr->iColumn>=0 ){
danielk19774adee202004-05-08 08:23:19 +00001272 sqlite3VdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn);
drh145716b2004-09-24 12:24:06 +00001273#ifndef NDEBUG
1274 if( pExpr->span.z && pExpr->span.n>0 && pExpr->span.n<100 ){
1275 VdbeComment((v, "# %T", &pExpr->span));
1276 }
1277#endif
drhc4a3c772001-04-04 11:48:57 +00001278 }else{
danielk19774adee202004-05-08 08:23:19 +00001279 sqlite3VdbeAddOp(v, OP_Recno, pExpr->iTable, 0);
drh22827922000-06-06 17:27:05 +00001280 }
drhcce7d172000-05-31 15:34:51 +00001281 break;
1282 }
1283 case TK_INTEGER: {
drhfec19aa2004-05-19 20:41:03 +00001284 codeInteger(v, pExpr->token.z, pExpr->token.n);
1285 break;
1286 }
1287 case TK_FLOAT:
1288 case TK_STRING: {
drhf2bc0132004-10-04 13:19:23 +00001289 assert( TK_FLOAT==OP_Real );
1290 assert( TK_STRING==OP_String8 );
drhfec19aa2004-05-19 20:41:03 +00001291 sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z, pExpr->token.n);
danielk19774adee202004-05-08 08:23:19 +00001292 sqlite3VdbeDequoteP3(v, -1);
drhcce7d172000-05-31 15:34:51 +00001293 break;
1294 }
danielk1977c572ef72004-05-27 09:28:41 +00001295 case TK_BLOB: {
drhf2bc0132004-10-04 13:19:23 +00001296 assert( TK_BLOB==OP_HexBlob );
danielk1977c572ef72004-05-27 09:28:41 +00001297 sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z+1, pExpr->token.n-1);
1298 sqlite3VdbeDequoteP3(v, -1);
1299 break;
1300 }
drhcce7d172000-05-31 15:34:51 +00001301 case TK_NULL: {
danielk19770f69c1e2004-05-29 11:24:50 +00001302 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001303 break;
1304 }
drh50457892003-09-06 01:10:47 +00001305 case TK_VARIABLE: {
danielk19774adee202004-05-08 08:23:19 +00001306 sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0);
drh895d7472004-08-20 16:02:39 +00001307 if( pExpr->token.n>1 ){
1308 sqlite3VdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);
1309 }
drh50457892003-09-06 01:10:47 +00001310 break;
1311 }
drh4e0cff62004-11-05 05:10:28 +00001312 case TK_REGISTER: {
1313 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iTable, 0);
1314 break;
1315 }
drhc9b84a12002-06-20 11:36:48 +00001316 case TK_LT:
1317 case TK_LE:
1318 case TK_GT:
1319 case TK_GE:
1320 case TK_NE:
1321 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00001322 assert( TK_LT==OP_Lt );
1323 assert( TK_LE==OP_Le );
1324 assert( TK_GT==OP_Gt );
1325 assert( TK_GE==OP_Ge );
1326 assert( TK_EQ==OP_Eq );
1327 assert( TK_NE==OP_Ne );
danielk1977a37cdde2004-05-16 11:15:36 +00001328 sqlite3ExprCode(pParse, pExpr->pLeft);
1329 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001330 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 0, 0);
danielk1977a37cdde2004-05-16 11:15:36 +00001331 break;
drhc9b84a12002-06-20 11:36:48 +00001332 }
drhcce7d172000-05-31 15:34:51 +00001333 case TK_AND:
1334 case TK_OR:
1335 case TK_PLUS:
1336 case TK_STAR:
1337 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00001338 case TK_REM:
1339 case TK_BITAND:
1340 case TK_BITOR:
drh17c40292004-07-21 02:53:29 +00001341 case TK_SLASH:
drhbf4133c2001-10-13 02:59:08 +00001342 case TK_LSHIFT:
drh855eb1c2004-08-31 13:45:11 +00001343 case TK_RSHIFT:
drh00400772000-06-16 20:51:26 +00001344 case TK_CONCAT: {
drhf2bc0132004-10-04 13:19:23 +00001345 assert( TK_AND==OP_And );
1346 assert( TK_OR==OP_Or );
1347 assert( TK_PLUS==OP_Add );
1348 assert( TK_MINUS==OP_Subtract );
1349 assert( TK_REM==OP_Remainder );
1350 assert( TK_BITAND==OP_BitAnd );
1351 assert( TK_BITOR==OP_BitOr );
1352 assert( TK_SLASH==OP_Divide );
1353 assert( TK_LSHIFT==OP_ShiftLeft );
1354 assert( TK_RSHIFT==OP_ShiftRight );
1355 assert( TK_CONCAT==OP_Concat );
danielk19774adee202004-05-08 08:23:19 +00001356 sqlite3ExprCode(pParse, pExpr->pLeft);
1357 sqlite3ExprCode(pParse, pExpr->pRight);
drh855eb1c2004-08-31 13:45:11 +00001358 sqlite3VdbeAddOp(v, op, 0, 0);
drh00400772000-06-16 20:51:26 +00001359 break;
1360 }
drhcce7d172000-05-31 15:34:51 +00001361 case TK_UMINUS: {
drhfec19aa2004-05-19 20:41:03 +00001362 Expr *pLeft = pExpr->pLeft;
1363 assert( pLeft );
1364 if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
1365 Token *p = &pLeft->token;
drh6e142f52000-06-08 13:36:40 +00001366 char *z = sqliteMalloc( p->n + 2 );
1367 sprintf(z, "-%.*s", p->n, p->z);
drhfec19aa2004-05-19 20:41:03 +00001368 if( pLeft->op==TK_FLOAT ){
1369 sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001370 }else{
drhfec19aa2004-05-19 20:41:03 +00001371 codeInteger(v, z, p->n+1);
drhe6840902002-03-06 03:08:25 +00001372 }
drh6e142f52000-06-08 13:36:40 +00001373 sqliteFree(z);
1374 break;
1375 }
drh1ccde152000-06-17 13:12:39 +00001376 /* Fall through into TK_NOT */
drh6e142f52000-06-08 13:36:40 +00001377 }
drhbf4133c2001-10-13 02:59:08 +00001378 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00001379 case TK_NOT: {
drhf2bc0132004-10-04 13:19:23 +00001380 assert( TK_BITNOT==OP_BitNot );
1381 assert( TK_NOT==OP_Not );
danielk19774adee202004-05-08 08:23:19 +00001382 sqlite3ExprCode(pParse, pExpr->pLeft);
1383 sqlite3VdbeAddOp(v, op, 0, 0);
drhcce7d172000-05-31 15:34:51 +00001384 break;
1385 }
1386 case TK_ISNULL:
1387 case TK_NOTNULL: {
1388 int dest;
drhf2bc0132004-10-04 13:19:23 +00001389 assert( TK_ISNULL==OP_IsNull );
1390 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00001391 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
1392 sqlite3ExprCode(pParse, pExpr->pLeft);
1393 dest = sqlite3VdbeCurrentAddr(v) + 2;
1394 sqlite3VdbeAddOp(v, op, 1, dest);
1395 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);
drhf2bc0132004-10-04 13:19:23 +00001396 break;
drhcce7d172000-05-31 15:34:51 +00001397 }
drh22827922000-06-06 17:27:05 +00001398 case TK_AGG_FUNCTION: {
danielk19774adee202004-05-08 08:23:19 +00001399 sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);
drh22827922000-06-06 17:27:05 +00001400 break;
1401 }
danielk19777977a172004-11-09 12:44:37 +00001402 case TK_CDATE:
1403 case TK_CTIME:
1404 case TK_CTIMESTAMP:
drh4b59ab52002-08-24 18:24:51 +00001405 case TK_GLOB:
1406 case TK_LIKE:
drhcce7d172000-05-31 15:34:51 +00001407 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00001408 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00001409 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00001410 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00001411 int nId;
1412 const char *zId;
danielk1977682f68b2004-06-05 10:22:17 +00001413 int p2 = 0;
1414 int i;
danielk1977d8123362004-06-12 09:25:12 +00001415 u8 enc = pParse->db->enc;
danielk1977dc1bdc42004-06-11 10:51:27 +00001416 CollSeq *pColl = 0;
drh4b59ab52002-08-24 18:24:51 +00001417 getFunctionName(pExpr, &zId, &nId);
danielk1977d8123362004-06-12 09:25:12 +00001418 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0);
drh0bce8352002-02-28 00:41:10 +00001419 assert( pDef!=0 );
drhf9b596e2004-05-26 16:54:42 +00001420 nExpr = sqlite3ExprCodeExprList(pParse, pList);
danielk1977682f68b2004-06-05 10:22:17 +00001421 for(i=0; i<nExpr && i<32; i++){
danielk1977d02eb1f2004-06-06 09:44:03 +00001422 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
1423 p2 |= (1<<i);
1424 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001425 if( pDef->needCollSeq && !pColl ){
1426 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
1427 }
1428 }
1429 if( pDef->needCollSeq ){
1430 if( !pColl ) pColl = pParse->db->pDfltColl;
danielk1977d8123362004-06-12 09:25:12 +00001431 sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ);
danielk1977682f68b2004-06-05 10:22:17 +00001432 }
1433 sqlite3VdbeOp3(v, OP_Function, nExpr, p2, (char*)pDef, P3_FUNCDEF);
drhcce7d172000-05-31 15:34:51 +00001434 break;
1435 }
drh19a775c2000-06-05 18:54:46 +00001436 case TK_SELECT: {
danielk19774adee202004-05-08 08:23:19 +00001437 sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
drhad6d9462004-09-19 02:15:24 +00001438 VdbeComment((v, "# load subquery result"));
drh19a775c2000-06-05 18:54:46 +00001439 break;
1440 }
drhfef52082000-06-06 01:50:43 +00001441 case TK_IN: {
1442 int addr;
drh94a11212004-09-25 13:12:14 +00001443 char affinity;
danielk1977e014a832004-05-17 10:48:57 +00001444
1445 /* Figure out the affinity to use to create a key from the results
1446 ** of the expression. affinityStr stores a static string suitable for
danielk1977ededfd52004-06-17 07:53:01 +00001447 ** P3 of OP_MakeRecord.
danielk1977e014a832004-05-17 10:48:57 +00001448 */
drh94a11212004-09-25 13:12:14 +00001449 affinity = comparisonAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +00001450
danielk19774adee202004-05-08 08:23:19 +00001451 sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
danielk1977e014a832004-05-17 10:48:57 +00001452
1453 /* Code the <expr> from "<expr> IN (...)". The temporary table
1454 ** pExpr->iTable contains the values that make up the (...) set.
1455 */
danielk19774adee202004-05-08 08:23:19 +00001456 sqlite3ExprCode(pParse, pExpr->pLeft);
1457 addr = sqlite3VdbeCurrentAddr(v);
danielk1977e014a832004-05-17 10:48:57 +00001458 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); /* addr + 0 */
danielk19774adee202004-05-08 08:23:19 +00001459 sqlite3VdbeAddOp(v, OP_Pop, 2, 0);
danielk19770f69c1e2004-05-29 11:24:50 +00001460 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +00001461 sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7);
drh94a11212004-09-25 13:12:14 +00001462 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1); /* addr + 4 */
danielk1977e014a832004-05-17 10:48:57 +00001463 sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7);
1464 sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); /* addr + 6 */
1465
drhfef52082000-06-06 01:50:43 +00001466 break;
1467 }
1468 case TK_BETWEEN: {
drhbe5c89a2004-07-26 00:31:09 +00001469 Expr *pLeft = pExpr->pLeft;
1470 struct ExprList_item *pLItem = pExpr->pList->a;
1471 Expr *pRight = pLItem->pExpr;
1472 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001473 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001474 sqlite3ExprCode(pParse, pRight);
1475 codeCompare(pParse, pLeft, pRight, OP_Ge, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001476 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
drhbe5c89a2004-07-26 00:31:09 +00001477 pLItem++;
1478 pRight = pLItem->pExpr;
1479 sqlite3ExprCode(pParse, pRight);
1480 codeCompare(pParse, pLeft, pRight, OP_Le, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00001481 sqlite3VdbeAddOp(v, OP_And, 0, 0);
drhfef52082000-06-06 01:50:43 +00001482 break;
1483 }
drh51e9a442004-01-16 16:42:53 +00001484 case TK_UPLUS:
drha2e00042002-01-22 03:13:42 +00001485 case TK_AS: {
danielk19774adee202004-05-08 08:23:19 +00001486 sqlite3ExprCode(pParse, pExpr->pLeft);
drha2e00042002-01-22 03:13:42 +00001487 break;
1488 }
drh17a7f8d2002-03-24 13:13:27 +00001489 case TK_CASE: {
1490 int expr_end_label;
drhf5905aa2002-05-26 20:54:33 +00001491 int jumpInst;
1492 int addr;
1493 int nExpr;
drh17a7f8d2002-03-24 13:13:27 +00001494 int i;
drhbe5c89a2004-07-26 00:31:09 +00001495 ExprList *pEList;
1496 struct ExprList_item *aListelem;
drh17a7f8d2002-03-24 13:13:27 +00001497
1498 assert(pExpr->pList);
1499 assert((pExpr->pList->nExpr % 2) == 0);
1500 assert(pExpr->pList->nExpr > 0);
drhbe5c89a2004-07-26 00:31:09 +00001501 pEList = pExpr->pList;
1502 aListelem = pEList->a;
1503 nExpr = pEList->nExpr;
danielk19774adee202004-05-08 08:23:19 +00001504 expr_end_label = sqlite3VdbeMakeLabel(v);
drh17a7f8d2002-03-24 13:13:27 +00001505 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001506 sqlite3ExprCode(pParse, pExpr->pLeft);
drh17a7f8d2002-03-24 13:13:27 +00001507 }
drhf5905aa2002-05-26 20:54:33 +00001508 for(i=0; i<nExpr; i=i+2){
drhbe5c89a2004-07-26 00:31:09 +00001509 sqlite3ExprCode(pParse, aListelem[i].pExpr);
drh17a7f8d2002-03-24 13:13:27 +00001510 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001511 sqlite3VdbeAddOp(v, OP_Dup, 1, 1);
drhbe5c89a2004-07-26 00:31:09 +00001512 jumpInst = codeCompare(pParse, pExpr->pLeft, aListelem[i].pExpr,
1513 OP_Ne, 0, 1);
danielk19774adee202004-05-08 08:23:19 +00001514 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001515 }else{
danielk19774adee202004-05-08 08:23:19 +00001516 jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0);
drh17a7f8d2002-03-24 13:13:27 +00001517 }
drhbe5c89a2004-07-26 00:31:09 +00001518 sqlite3ExprCode(pParse, aListelem[i+1].pExpr);
danielk19774adee202004-05-08 08:23:19 +00001519 sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label);
1520 addr = sqlite3VdbeCurrentAddr(v);
1521 sqlite3VdbeChangeP2(v, jumpInst, addr);
drh17a7f8d2002-03-24 13:13:27 +00001522 }
drhf570f012002-05-31 15:51:25 +00001523 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001524 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhf570f012002-05-31 15:51:25 +00001525 }
drh17a7f8d2002-03-24 13:13:27 +00001526 if( pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001527 sqlite3ExprCode(pParse, pExpr->pRight);
drh17a7f8d2002-03-24 13:13:27 +00001528 }else{
danielk19770f69c1e2004-05-29 11:24:50 +00001529 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
drh17a7f8d2002-03-24 13:13:27 +00001530 }
danielk19774adee202004-05-08 08:23:19 +00001531 sqlite3VdbeResolveLabel(v, expr_end_label);
danielk19776f349032002-06-11 02:25:40 +00001532 break;
1533 }
1534 case TK_RAISE: {
1535 if( !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +00001536 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00001537 "RAISE() may only be used within a trigger-program");
danielk19776f349032002-06-11 02:25:40 +00001538 return;
1539 }
drhad6d9462004-09-19 02:15:24 +00001540 if( pExpr->iColumn!=OE_Ignore ){
1541 assert( pExpr->iColumn==OE_Rollback ||
1542 pExpr->iColumn == OE_Abort ||
1543 pExpr->iColumn == OE_Fail );
1544 sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn,
1545 pExpr->token.z, pExpr->token.n);
1546 sqlite3VdbeDequoteP3(v, -1);
danielk19776f349032002-06-11 02:25:40 +00001547 } else {
drhad6d9462004-09-19 02:15:24 +00001548 assert( pExpr->iColumn == OE_Ignore );
1549 sqlite3VdbeAddOp(v, OP_ContextPop, 0, 0);
1550 sqlite3VdbeAddOp(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
1551 VdbeComment((v, "# raise(IGNORE)"));
danielk19776f349032002-06-11 02:25:40 +00001552 }
drh17a7f8d2002-03-24 13:13:27 +00001553 }
1554 break;
drhcce7d172000-05-31 15:34:51 +00001555 }
drhcce7d172000-05-31 15:34:51 +00001556}
1557
1558/*
drh25303782004-12-07 15:41:48 +00001559** Generate code that evalutes the given expression and leaves the result
1560** on the stack. See also sqlite3ExprCode().
1561**
1562** This routine might also cache the result and modify the pExpr tree
1563** so that it will make use of the cached result on subsequent evaluations
1564** rather than evaluate the whole expression again. Trivial expressions are
1565** not cached. If the expression is cached, its result is stored in a
1566** memory location.
1567*/
1568void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr){
1569 Vdbe *v = pParse->pVdbe;
1570 int iMem;
1571 int addr1, addr2;
1572 if( v==0 ) return;
1573 addr1 = sqlite3VdbeCurrentAddr(v);
1574 sqlite3ExprCode(pParse, pExpr);
1575 addr2 = sqlite3VdbeCurrentAddr(v);
1576 if( addr2>addr1+1 || sqlite3VdbeGetOp(v, addr1)->opcode==OP_Function ){
1577 iMem = pExpr->iTable = pParse->nMem++;
1578 sqlite3VdbeAddOp(v, OP_MemStore, iMem, 0);
1579 pExpr->op = TK_REGISTER;
1580 }
1581}
1582
1583/*
drh268380c2004-02-25 13:47:31 +00001584** Generate code that pushes the value of every element of the given
drhf9b596e2004-05-26 16:54:42 +00001585** expression list onto the stack.
drh268380c2004-02-25 13:47:31 +00001586**
1587** Return the number of elements pushed onto the stack.
1588*/
danielk19774adee202004-05-08 08:23:19 +00001589int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00001590 Parse *pParse, /* Parsing context */
drhf9b596e2004-05-26 16:54:42 +00001591 ExprList *pList /* The expression list to be coded */
drh268380c2004-02-25 13:47:31 +00001592){
1593 struct ExprList_item *pItem;
1594 int i, n;
1595 Vdbe *v;
1596 if( pList==0 ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001597 v = sqlite3GetVdbe(pParse);
drh268380c2004-02-25 13:47:31 +00001598 n = pList->nExpr;
1599 for(pItem=pList->a, i=0; i<n; i++, pItem++){
danielk19774adee202004-05-08 08:23:19 +00001600 sqlite3ExprCode(pParse, pItem->pExpr);
drh268380c2004-02-25 13:47:31 +00001601 }
drhf9b596e2004-05-26 16:54:42 +00001602 return n;
drh268380c2004-02-25 13:47:31 +00001603}
1604
1605/*
drhcce7d172000-05-31 15:34:51 +00001606** Generate code for a boolean expression such that a jump is made
1607** to the label "dest" if the expression is true but execution
1608** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00001609**
1610** If the expression evaluates to NULL (neither true nor false), then
1611** take the jump if the jumpIfNull flag is true.
drhf2bc0132004-10-04 13:19:23 +00001612**
1613** This code depends on the fact that certain token values (ex: TK_EQ)
1614** are the same as opcode values (ex: OP_Eq) that implement the corresponding
1615** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
1616** the make process cause these values to align. Assert()s in the code
1617** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00001618*/
danielk19774adee202004-05-08 08:23:19 +00001619void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001620 Vdbe *v = pParse->pVdbe;
1621 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001622 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00001623 op = pExpr->op;
1624 switch( op ){
drhcce7d172000-05-31 15:34:51 +00001625 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001626 int d2 = sqlite3VdbeMakeLabel(v);
1627 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
1628 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
1629 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001630 break;
1631 }
1632 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001633 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
1634 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001635 break;
1636 }
1637 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001638 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001639 break;
1640 }
1641 case TK_LT:
1642 case TK_LE:
1643 case TK_GT:
1644 case TK_GE:
1645 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00001646 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00001647 assert( TK_LT==OP_Lt );
1648 assert( TK_LE==OP_Le );
1649 assert( TK_GT==OP_Gt );
1650 assert( TK_GE==OP_Ge );
1651 assert( TK_EQ==OP_Eq );
1652 assert( TK_NE==OP_Ne );
danielk19774adee202004-05-08 08:23:19 +00001653 sqlite3ExprCode(pParse, pExpr->pLeft);
1654 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001655 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001656 break;
1657 }
1658 case TK_ISNULL:
1659 case TK_NOTNULL: {
drhf2bc0132004-10-04 13:19:23 +00001660 assert( TK_ISNULL==OP_IsNull );
1661 assert( TK_NOTNULL==OP_NotNull );
danielk19774adee202004-05-08 08:23:19 +00001662 sqlite3ExprCode(pParse, pExpr->pLeft);
1663 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001664 break;
1665 }
drhfef52082000-06-06 01:50:43 +00001666 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00001667 /* The expression "x BETWEEN y AND z" is implemented as:
1668 **
1669 ** 1 IF (x < y) GOTO 3
1670 ** 2 IF (x <= z) GOTO <dest>
1671 ** 3 ...
1672 */
drhf5905aa2002-05-26 20:54:33 +00001673 int addr;
drhbe5c89a2004-07-26 00:31:09 +00001674 Expr *pLeft = pExpr->pLeft;
1675 Expr *pRight = pExpr->pList->a[0].pExpr;
1676 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001677 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001678 sqlite3ExprCode(pParse, pRight);
1679 addr = codeCompare(pParse, pLeft, pRight, OP_Lt, 0, !jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00001680
drhbe5c89a2004-07-26 00:31:09 +00001681 pRight = pExpr->pList->a[1].pExpr;
1682 sqlite3ExprCode(pParse, pRight);
1683 codeCompare(pParse, pLeft, pRight, OP_Le, dest, jumpIfNull);
danielk19770202b292004-06-09 09:55:16 +00001684
danielk19774adee202004-05-08 08:23:19 +00001685 sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
1686 sqlite3VdbeChangeP2(v, addr, sqlite3VdbeCurrentAddr(v));
1687 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
drhfef52082000-06-06 01:50:43 +00001688 break;
1689 }
drhcce7d172000-05-31 15:34:51 +00001690 default: {
danielk19774adee202004-05-08 08:23:19 +00001691 sqlite3ExprCode(pParse, pExpr);
1692 sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001693 break;
1694 }
1695 }
1696}
1697
1698/*
drh66b89c82000-11-28 20:47:17 +00001699** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00001700** to the label "dest" if the expression is false but execution
1701** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00001702**
1703** If the expression evaluates to NULL (neither true nor false) then
1704** jump if jumpIfNull is true or fall through if jumpIfNull is false.
drhcce7d172000-05-31 15:34:51 +00001705*/
danielk19774adee202004-05-08 08:23:19 +00001706void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00001707 Vdbe *v = pParse->pVdbe;
1708 int op = 0;
drhdaffd0e2001-04-11 14:28:42 +00001709 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00001710
1711 /* The value of pExpr->op and op are related as follows:
1712 **
1713 ** pExpr->op op
1714 ** --------- ----------
1715 ** TK_ISNULL OP_NotNull
1716 ** TK_NOTNULL OP_IsNull
1717 ** TK_NE OP_Eq
1718 ** TK_EQ OP_Ne
1719 ** TK_GT OP_Le
1720 ** TK_LE OP_Gt
1721 ** TK_GE OP_Lt
1722 ** TK_LT OP_Ge
1723 **
1724 ** For other values of pExpr->op, op is undefined and unused.
1725 ** The value of TK_ and OP_ constants are arranged such that we
1726 ** can compute the mapping above using the following expression.
1727 ** Assert()s verify that the computation is correct.
1728 */
1729 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
1730
1731 /* Verify correct alignment of TK_ and OP_ constants
1732 */
1733 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
1734 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
1735 assert( pExpr->op!=TK_NE || op==OP_Eq );
1736 assert( pExpr->op!=TK_EQ || op==OP_Ne );
1737 assert( pExpr->op!=TK_LT || op==OP_Ge );
1738 assert( pExpr->op!=TK_LE || op==OP_Gt );
1739 assert( pExpr->op!=TK_GT || op==OP_Le );
1740 assert( pExpr->op!=TK_GE || op==OP_Lt );
1741
drhcce7d172000-05-31 15:34:51 +00001742 switch( pExpr->op ){
1743 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00001744 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
1745 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001746 break;
1747 }
1748 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00001749 int d2 = sqlite3VdbeMakeLabel(v);
1750 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
1751 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
1752 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00001753 break;
1754 }
1755 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00001756 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001757 break;
1758 }
1759 case TK_LT:
1760 case TK_LE:
1761 case TK_GT:
1762 case TK_GE:
1763 case TK_NE:
1764 case TK_EQ: {
danielk19774adee202004-05-08 08:23:19 +00001765 sqlite3ExprCode(pParse, pExpr->pLeft);
1766 sqlite3ExprCode(pParse, pExpr->pRight);
drhbe5c89a2004-07-26 00:31:09 +00001767 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00001768 break;
1769 }
drhcce7d172000-05-31 15:34:51 +00001770 case TK_ISNULL:
1771 case TK_NOTNULL: {
danielk19774adee202004-05-08 08:23:19 +00001772 sqlite3ExprCode(pParse, pExpr->pLeft);
1773 sqlite3VdbeAddOp(v, op, 1, dest);
drhcce7d172000-05-31 15:34:51 +00001774 break;
1775 }
drhfef52082000-06-06 01:50:43 +00001776 case TK_BETWEEN: {
danielk19770202b292004-06-09 09:55:16 +00001777 /* The expression is "x BETWEEN y AND z". It is implemented as:
1778 **
1779 ** 1 IF (x >= y) GOTO 3
1780 ** 2 GOTO <dest>
1781 ** 3 IF (x > z) GOTO <dest>
1782 */
drhfef52082000-06-06 01:50:43 +00001783 int addr;
drhbe5c89a2004-07-26 00:31:09 +00001784 Expr *pLeft = pExpr->pLeft;
1785 Expr *pRight = pExpr->pList->a[0].pExpr;
1786 sqlite3ExprCode(pParse, pLeft);
danielk19774adee202004-05-08 08:23:19 +00001787 sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
drhbe5c89a2004-07-26 00:31:09 +00001788 sqlite3ExprCode(pParse, pRight);
danielk19774adee202004-05-08 08:23:19 +00001789 addr = sqlite3VdbeCurrentAddr(v);
drhbe5c89a2004-07-26 00:31:09 +00001790 codeCompare(pParse, pLeft, pRight, OP_Ge, addr+3, !jumpIfNull);
1791
danielk19774adee202004-05-08 08:23:19 +00001792 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
1793 sqlite3VdbeAddOp(v, OP_Goto, 0, dest);
drhbe5c89a2004-07-26 00:31:09 +00001794 pRight = pExpr->pList->a[1].pExpr;
1795 sqlite3ExprCode(pParse, pRight);
1796 codeCompare(pParse, pLeft, pRight, OP_Gt, dest, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00001797 break;
1798 }
drhcce7d172000-05-31 15:34:51 +00001799 default: {
danielk19774adee202004-05-08 08:23:19 +00001800 sqlite3ExprCode(pParse, pExpr);
1801 sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
drhcce7d172000-05-31 15:34:51 +00001802 break;
1803 }
1804 }
1805}
drh22827922000-06-06 17:27:05 +00001806
1807/*
1808** Do a deep comparison of two expression trees. Return TRUE (non-zero)
1809** if they are identical and return FALSE if they differ in any way.
1810*/
danielk19774adee202004-05-08 08:23:19 +00001811int sqlite3ExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00001812 int i;
1813 if( pA==0 ){
1814 return pB==0;
1815 }else if( pB==0 ){
1816 return 0;
1817 }
1818 if( pA->op!=pB->op ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001819 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
1820 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00001821 if( pA->pList ){
1822 if( pB->pList==0 ) return 0;
1823 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
1824 for(i=0; i<pA->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00001825 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00001826 return 0;
1827 }
1828 }
1829 }else if( pB->pList ){
1830 return 0;
1831 }
1832 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00001833 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drh22827922000-06-06 17:27:05 +00001834 if( pA->token.z ){
1835 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00001836 if( pB->token.n!=pA->token.n ) return 0;
danielk19774adee202004-05-08 08:23:19 +00001837 if( sqlite3StrNICmp(pA->token.z, pB->token.z, pB->token.n)!=0 ) return 0;
drh22827922000-06-06 17:27:05 +00001838 }
1839 return 1;
1840}
1841
1842/*
1843** Add a new element to the pParse->aAgg[] array and return its index.
1844*/
1845static int appendAggInfo(Parse *pParse){
1846 if( (pParse->nAgg & 0x7)==0 ){
1847 int amt = pParse->nAgg + 8;
drh6d4abfb2001-10-22 02:58:08 +00001848 AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0]));
1849 if( aAgg==0 ){
drh22827922000-06-06 17:27:05 +00001850 return -1;
1851 }
drh6d4abfb2001-10-22 02:58:08 +00001852 pParse->aAgg = aAgg;
drh22827922000-06-06 17:27:05 +00001853 }
1854 memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0]));
1855 return pParse->nAgg++;
1856}
1857
1858/*
1859** Analyze the given expression looking for aggregate functions and
1860** for variables that need to be added to the pParse->aAgg[] array.
1861** Make additional entries to the pParse->aAgg[] array as necessary.
1862**
1863** This routine should only be called after the expression has been
danielk19774adee202004-05-08 08:23:19 +00001864** analyzed by sqlite3ExprResolveIds() and sqlite3ExprCheck().
drh22827922000-06-06 17:27:05 +00001865**
1866** If errors are seen, leave an error message in zErrMsg and return
1867** the number of errors.
1868*/
danielk19774adee202004-05-08 08:23:19 +00001869int sqlite3ExprAnalyzeAggregates(Parse *pParse, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00001870 int i;
1871 AggExpr *aAgg;
1872 int nErr = 0;
1873
1874 if( pExpr==0 ) return 0;
1875 switch( pExpr->op ){
drh967e8b72000-06-21 13:59:10 +00001876 case TK_COLUMN: {
drh22827922000-06-06 17:27:05 +00001877 aAgg = pParse->aAgg;
1878 for(i=0; i<pParse->nAgg; i++){
1879 if( aAgg[i].isAgg ) continue;
1880 if( aAgg[i].pExpr->iTable==pExpr->iTable
drh967e8b72000-06-21 13:59:10 +00001881 && aAgg[i].pExpr->iColumn==pExpr->iColumn ){
drh22827922000-06-06 17:27:05 +00001882 break;
1883 }
1884 }
1885 if( i>=pParse->nAgg ){
1886 i = appendAggInfo(pParse);
1887 if( i<0 ) return 1;
1888 pParse->aAgg[i].isAgg = 0;
1889 pParse->aAgg[i].pExpr = pExpr;
1890 }
drhaaf88722000-06-08 11:25:00 +00001891 pExpr->iAgg = i;
drh22827922000-06-06 17:27:05 +00001892 break;
1893 }
1894 case TK_AGG_FUNCTION: {
drh22827922000-06-06 17:27:05 +00001895 aAgg = pParse->aAgg;
1896 for(i=0; i<pParse->nAgg; i++){
1897 if( !aAgg[i].isAgg ) continue;
danielk19774adee202004-05-08 08:23:19 +00001898 if( sqlite3ExprCompare(aAgg[i].pExpr, pExpr) ){
drh22827922000-06-06 17:27:05 +00001899 break;
1900 }
1901 }
1902 if( i>=pParse->nAgg ){
danielk1977d8123362004-06-12 09:25:12 +00001903 u8 enc = pParse->db->enc;
drh22827922000-06-06 17:27:05 +00001904 i = appendAggInfo(pParse);
1905 if( i<0 ) return 1;
1906 pParse->aAgg[i].isAgg = 1;
1907 pParse->aAgg[i].pExpr = pExpr;
danielk19774adee202004-05-08 08:23:19 +00001908 pParse->aAgg[i].pFunc = sqlite3FindFunction(pParse->db,
drh6977fea2002-10-22 23:38:04 +00001909 pExpr->token.z, pExpr->token.n,
danielk1977d8123362004-06-12 09:25:12 +00001910 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
drh22827922000-06-06 17:27:05 +00001911 }
1912 pExpr->iAgg = i;
1913 break;
1914 }
1915 default: {
1916 if( pExpr->pLeft ){
danielk19774adee202004-05-08 08:23:19 +00001917 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pLeft);
drh22827922000-06-06 17:27:05 +00001918 }
1919 if( nErr==0 && pExpr->pRight ){
danielk19774adee202004-05-08 08:23:19 +00001920 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pRight);
drh22827922000-06-06 17:27:05 +00001921 }
1922 if( nErr==0 && pExpr->pList ){
1923 int n = pExpr->pList->nExpr;
1924 int i;
1925 for(i=0; nErr==0 && i<n; i++){
danielk19774adee202004-05-08 08:23:19 +00001926 nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pList->a[i].pExpr);
drh22827922000-06-06 17:27:05 +00001927 }
1928 }
1929 break;
1930 }
1931 }
1932 return nErr;
1933}
drh8e0a2f92002-02-23 23:45:45 +00001934
1935/*
danielk1977d02eb1f2004-06-06 09:44:03 +00001936** Locate a user function given a name, a number of arguments and a flag
1937** indicating whether the function prefers UTF-16 over UTF-8. Return a
1938** pointer to the FuncDef structure that defines that function, or return
1939** NULL if the function does not exist.
drh8e0a2f92002-02-23 23:45:45 +00001940**
drh0bce8352002-02-28 00:41:10 +00001941** If the createFlag argument is true, then a new (blank) FuncDef
drh8e0a2f92002-02-23 23:45:45 +00001942** structure is created and liked into the "db" structure if a
1943** no matching function previously existed. When createFlag is true
1944** and the nArg parameter is -1, then only a function that accepts
1945** any number of arguments will be returned.
1946**
1947** If createFlag is false and nArg is -1, then the first valid
1948** function found is returned. A function is valid if either xFunc
1949** or xStep is non-zero.
danielk1977d02eb1f2004-06-06 09:44:03 +00001950**
1951** If createFlag is false, then a function with the required name and
1952** number of arguments may be returned even if the eTextRep flag does not
1953** match that requested.
drh8e0a2f92002-02-23 23:45:45 +00001954*/
danielk19774adee202004-05-08 08:23:19 +00001955FuncDef *sqlite3FindFunction(
drh9bb575f2004-09-06 17:24:11 +00001956 sqlite3 *db, /* An open database */
drh8e0a2f92002-02-23 23:45:45 +00001957 const char *zName, /* Name of the function. Not null-terminated */
1958 int nName, /* Number of characters in the name */
1959 int nArg, /* Number of arguments. -1 means any number */
danielk1977d8123362004-06-12 09:25:12 +00001960 u8 enc, /* Preferred text encoding */
drh8e0a2f92002-02-23 23:45:45 +00001961 int createFlag /* Create new entry if true and does not otherwise exist */
1962){
danielk1977d02eb1f2004-06-06 09:44:03 +00001963 FuncDef *p; /* Iterator variable */
1964 FuncDef *pFirst; /* First function with this name */
1965 FuncDef *pBest = 0; /* Best match found so far */
danielk1977d8123362004-06-12 09:25:12 +00001966 int bestmatch = 0;
danielk1977d02eb1f2004-06-06 09:44:03 +00001967
danielk1977d8123362004-06-12 09:25:12 +00001968
1969 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
danielk1977d02eb1f2004-06-06 09:44:03 +00001970 if( nArg<-1 ) nArg = -1;
1971
1972 pFirst = (FuncDef*)sqlite3HashFind(&db->aFunc, zName, nName);
1973 for(p=pFirst; p; p=p->pNext){
danielk1977d8123362004-06-12 09:25:12 +00001974 /* During the search for the best function definition, bestmatch is set
1975 ** as follows to indicate the quality of the match with the definition
1976 ** pointed to by pBest:
1977 **
1978 ** 0: pBest is NULL. No match has been found.
1979 ** 1: A variable arguments function that prefers UTF-8 when a UTF-16
1980 ** encoding is requested, or vice versa.
1981 ** 2: A variable arguments function that uses UTF-16BE when UTF-16LE is
1982 ** requested, or vice versa.
1983 ** 3: A variable arguments function using the same text encoding.
1984 ** 4: A function with the exact number of arguments requested that
1985 ** prefers UTF-8 when a UTF-16 encoding is requested, or vice versa.
1986 ** 5: A function with the exact number of arguments requested that
1987 ** prefers UTF-16LE when UTF-16BE is requested, or vice versa.
1988 ** 6: An exact match.
1989 **
1990 ** A larger value of 'matchqual' indicates a more desirable match.
1991 */
danielk1977e12c17b2004-06-23 12:35:14 +00001992 if( p->nArg==-1 || p->nArg==nArg || nArg==-1 ){
danielk1977d8123362004-06-12 09:25:12 +00001993 int match = 1; /* Quality of this match */
1994 if( p->nArg==nArg || nArg==-1 ){
1995 match = 4;
danielk1977d02eb1f2004-06-06 09:44:03 +00001996 }
danielk1977d8123362004-06-12 09:25:12 +00001997 if( enc==p->iPrefEnc ){
1998 match += 2;
danielk1977d02eb1f2004-06-06 09:44:03 +00001999 }
danielk1977d8123362004-06-12 09:25:12 +00002000 else if( (enc==SQLITE_UTF16LE && p->iPrefEnc==SQLITE_UTF16BE) ||
2001 (enc==SQLITE_UTF16BE && p->iPrefEnc==SQLITE_UTF16LE) ){
2002 match += 1;
2003 }
2004
2005 if( match>bestmatch ){
2006 pBest = p;
2007 bestmatch = match;
danielk1977d02eb1f2004-06-06 09:44:03 +00002008 }
2009 }
drh8e0a2f92002-02-23 23:45:45 +00002010 }
danielk1977d02eb1f2004-06-06 09:44:03 +00002011
danielk1977d8123362004-06-12 09:25:12 +00002012 /* If the createFlag parameter is true, and the seach did not reveal an
2013 ** exact match for the name, number of arguments and encoding, then add a
2014 ** new entry to the hash table and return it.
2015 */
2016 if( createFlag && bestmatch<6 &&
danielk1977d02eb1f2004-06-06 09:44:03 +00002017 (pBest = sqliteMalloc(sizeof(*pBest)+nName+1)) ){
2018 pBest->nArg = nArg;
2019 pBest->pNext = pFirst;
2020 pBest->zName = (char*)&pBest[1];
danielk1977d8123362004-06-12 09:25:12 +00002021 pBest->iPrefEnc = enc;
danielk1977d02eb1f2004-06-06 09:44:03 +00002022 memcpy(pBest->zName, zName, nName);
2023 pBest->zName[nName] = 0;
danielk19772c336542005-01-13 02:14:23 +00002024 if( pBest==sqlite3HashInsert(&db->aFunc,pBest->zName,nName,(void*)pBest) ){
2025 sqliteFree(pBest);
2026 return 0;
2027 }
drh8e0a2f92002-02-23 23:45:45 +00002028 }
danielk1977d02eb1f2004-06-06 09:44:03 +00002029
2030 if( pBest && (pBest->xStep || pBest->xFunc || createFlag) ){
2031 return pBest;
drh8e0a2f92002-02-23 23:45:45 +00002032 }
danielk1977d02eb1f2004-06-06 09:44:03 +00002033 return 0;
drh8e0a2f92002-02-23 23:45:45 +00002034}