blob: 074e3649cc36d652468108745ce134c96147d395 [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**
drha33cb5f2008-08-07 13:05:34 +000015** $Id: expr.c,v 1.388 2008/08/07 13:05:35 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){
drh487e2622005-06-25 18:42:14 +000037 int op = pExpr->op;
drh487e2622005-06-25 18:42:14 +000038 if( op==TK_SELECT ){
danielk1977bf3b7212004-05-18 10:06:24 +000039 return sqlite3ExprAffinity(pExpr->pSelect->pEList->a[0].pExpr);
danielk1977a37cdde2004-05-16 11:15:36 +000040 }
drh487e2622005-06-25 18:42:14 +000041#ifndef SQLITE_OMIT_CAST
42 if( op==TK_CAST ){
drh8a512562005-11-14 22:29:05 +000043 return sqlite3AffinityType(&pExpr->token);
drh487e2622005-06-25 18:42:14 +000044 }
45#endif
danielk1977a37cdde2004-05-16 11:15:36 +000046 return pExpr->affinity;
47}
48
drh53db1452004-05-20 13:54:53 +000049/*
drh8b4c40d2007-02-01 23:02:45 +000050** Set the collating sequence for expression pExpr to be the collating
51** sequence named by pToken. Return a pointer to the revised expression.
drha34001c2007-02-02 12:44:37 +000052** The collating sequence is marked as "explicit" using the EP_ExpCollate
53** flag. An explicit collating sequence will override implicit
54** collating sequences.
drh8b4c40d2007-02-01 23:02:45 +000055*/
56Expr *sqlite3ExprSetColl(Parse *pParse, Expr *pExpr, Token *pName){
danielk197739002502007-11-12 09:50:26 +000057 char *zColl = 0; /* Dequoted name of collation sequence */
drh8b4c40d2007-02-01 23:02:45 +000058 CollSeq *pColl;
drh633e6d52008-07-28 19:34:53 +000059 sqlite3 *db = pParse->db;
60 zColl = sqlite3NameFromToken(db, pName);
danielk197739002502007-11-12 09:50:26 +000061 if( pExpr && zColl ){
62 pColl = sqlite3LocateCollSeq(pParse, zColl, -1);
63 if( pColl ){
64 pExpr->pColl = pColl;
65 pExpr->flags |= EP_ExpCollate;
66 }
drh8b4c40d2007-02-01 23:02:45 +000067 }
drh633e6d52008-07-28 19:34:53 +000068 sqlite3DbFree(db, zColl);
drh8b4c40d2007-02-01 23:02:45 +000069 return pExpr;
70}
71
72/*
danielk19770202b292004-06-09 09:55:16 +000073** Return the default collation sequence for the expression pExpr. If
74** there is no default collation type, return 0.
75*/
danielk19777cedc8d2004-06-10 10:50:08 +000076CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
77 CollSeq *pColl = 0;
danielk19770202b292004-06-09 09:55:16 +000078 if( pExpr ){
drh7e09fe02007-06-20 16:13:23 +000079 int op;
danielk19777cedc8d2004-06-10 10:50:08 +000080 pColl = pExpr->pColl;
drh7e09fe02007-06-20 16:13:23 +000081 op = pExpr->op;
82 if( (op==TK_CAST || op==TK_UPLUS) && !pColl ){
danielk19777cedc8d2004-06-10 10:50:08 +000083 return sqlite3ExprCollSeq(pParse, pExpr->pLeft);
danielk19770202b292004-06-09 09:55:16 +000084 }
85 }
danielk19777cedc8d2004-06-10 10:50:08 +000086 if( sqlite3CheckCollSeq(pParse, pColl) ){
87 pColl = 0;
88 }
89 return pColl;
danielk19770202b292004-06-09 09:55:16 +000090}
91
92/*
drh626a8792005-01-17 22:08:19 +000093** pExpr is an operand of a comparison operator. aff2 is the
94** type affinity of the other operand. This routine returns the
drh53db1452004-05-20 13:54:53 +000095** type affinity that should be used for the comparison operator.
96*/
danielk1977e014a832004-05-17 10:48:57 +000097char sqlite3CompareAffinity(Expr *pExpr, char aff2){
danielk1977bf3b7212004-05-18 10:06:24 +000098 char aff1 = sqlite3ExprAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +000099 if( aff1 && aff2 ){
drh8df447f2005-11-01 15:48:24 +0000100 /* Both sides of the comparison are columns. If one has numeric
101 ** affinity, use that. Otherwise use no affinity.
danielk1977e014a832004-05-17 10:48:57 +0000102 */
drh8a512562005-11-14 22:29:05 +0000103 if( sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2) ){
danielk1977e014a832004-05-17 10:48:57 +0000104 return SQLITE_AFF_NUMERIC;
105 }else{
106 return SQLITE_AFF_NONE;
107 }
108 }else if( !aff1 && !aff2 ){
drh5f6a87b2004-07-19 00:39:45 +0000109 /* Neither side of the comparison is a column. Compare the
110 ** results directly.
danielk1977e014a832004-05-17 10:48:57 +0000111 */
drh5f6a87b2004-07-19 00:39:45 +0000112 return SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +0000113 }else{
114 /* One side is a column, the other is not. Use the columns affinity. */
drhfe05af82005-07-21 03:14:59 +0000115 assert( aff1==0 || aff2==0 );
danielk1977e014a832004-05-17 10:48:57 +0000116 return (aff1 + aff2);
117 }
118}
119
drh53db1452004-05-20 13:54:53 +0000120/*
121** pExpr is a comparison operator. Return the type affinity that should
122** be applied to both operands prior to doing the comparison.
123*/
danielk1977e014a832004-05-17 10:48:57 +0000124static char comparisonAffinity(Expr *pExpr){
125 char aff;
126 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
127 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
128 pExpr->op==TK_NE );
129 assert( pExpr->pLeft );
danielk1977bf3b7212004-05-18 10:06:24 +0000130 aff = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +0000131 if( pExpr->pRight ){
132 aff = sqlite3CompareAffinity(pExpr->pRight, aff);
133 }
134 else if( pExpr->pSelect ){
135 aff = sqlite3CompareAffinity(pExpr->pSelect->pEList->a[0].pExpr, aff);
136 }
137 else if( !aff ){
drhde087bd2007-02-23 03:00:44 +0000138 aff = SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +0000139 }
140 return aff;
141}
142
143/*
144** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
145** idx_affinity is the affinity of an indexed column. Return true
146** if the index with affinity idx_affinity may be used to implement
147** the comparison in pExpr.
148*/
149int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){
150 char aff = comparisonAffinity(pExpr);
drh8a512562005-11-14 22:29:05 +0000151 switch( aff ){
152 case SQLITE_AFF_NONE:
153 return 1;
154 case SQLITE_AFF_TEXT:
155 return idx_affinity==SQLITE_AFF_TEXT;
156 default:
157 return sqlite3IsNumericAffinity(idx_affinity);
158 }
danielk1977e014a832004-05-17 10:48:57 +0000159}
160
danielk1977a37cdde2004-05-16 11:15:36 +0000161/*
drh35573352008-01-08 23:54:25 +0000162** Return the P5 value that should be used for a binary comparison
danielk1977a37cdde2004-05-16 11:15:36 +0000163** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
danielk1977a37cdde2004-05-16 11:15:36 +0000164*/
drh35573352008-01-08 23:54:25 +0000165static u8 binaryCompareP5(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){
166 u8 aff = (char)sqlite3ExprAffinity(pExpr2);
167 aff = sqlite3CompareAffinity(pExpr1, aff) | jumpIfNull;
168 return aff;
danielk1977a37cdde2004-05-16 11:15:36 +0000169}
170
drha2e00042002-01-22 03:13:42 +0000171/*
danielk19770202b292004-06-09 09:55:16 +0000172** Return a pointer to the collation sequence that should be used by
173** a binary comparison operator comparing pLeft and pRight.
174**
175** If the left hand expression has a collating sequence type, then it is
176** used. Otherwise the collation sequence for the right hand expression
177** is used, or the default (BINARY) if neither expression has a collating
178** type.
danielk1977bcbb04e2007-05-29 12:11:29 +0000179**
180** Argument pRight (but not pLeft) may be a null pointer. In this case,
181** it is not considered.
danielk19770202b292004-06-09 09:55:16 +0000182*/
drh0a0e1312007-08-07 17:04:59 +0000183CollSeq *sqlite3BinaryCompareCollSeq(
danielk1977bcbb04e2007-05-29 12:11:29 +0000184 Parse *pParse,
185 Expr *pLeft,
186 Expr *pRight
187){
drhec41dda2007-02-07 13:09:45 +0000188 CollSeq *pColl;
189 assert( pLeft );
drhec41dda2007-02-07 13:09:45 +0000190 if( pLeft->flags & EP_ExpCollate ){
191 assert( pLeft->pColl );
192 pColl = pLeft->pColl;
danielk1977bcbb04e2007-05-29 12:11:29 +0000193 }else if( pRight && pRight->flags & EP_ExpCollate ){
drhec41dda2007-02-07 13:09:45 +0000194 assert( pRight->pColl );
195 pColl = pRight->pColl;
196 }else{
197 pColl = sqlite3ExprCollSeq(pParse, pLeft);
198 if( !pColl ){
199 pColl = sqlite3ExprCollSeq(pParse, pRight);
200 }
danielk19770202b292004-06-09 09:55:16 +0000201 }
202 return pColl;
203}
204
205/*
drhda250ea2008-04-01 05:07:14 +0000206** Generate the operands for a comparison operation. Before
207** generating the code for each operand, set the EP_AnyAff
208** flag on the expression so that it will be able to used a
209** cached column value that has previously undergone an
210** affinity change.
211*/
212static void codeCompareOperands(
213 Parse *pParse, /* Parsing and code generating context */
214 Expr *pLeft, /* The left operand */
215 int *pRegLeft, /* Register where left operand is stored */
216 int *pFreeLeft, /* Free this register when done */
217 Expr *pRight, /* The right operand */
218 int *pRegRight, /* Register where right operand is stored */
219 int *pFreeRight /* Write temp register for right operand there */
220){
221 while( pLeft->op==TK_UPLUS ) pLeft = pLeft->pLeft;
222 pLeft->flags |= EP_AnyAff;
223 *pRegLeft = sqlite3ExprCodeTemp(pParse, pLeft, pFreeLeft);
224 while( pRight->op==TK_UPLUS ) pRight = pRight->pLeft;
225 pRight->flags |= EP_AnyAff;
226 *pRegRight = sqlite3ExprCodeTemp(pParse, pRight, pFreeRight);
227}
228
229/*
drhbe5c89a2004-07-26 00:31:09 +0000230** Generate code for a comparison operator.
231*/
232static int codeCompare(
233 Parse *pParse, /* The parsing (and code generating) context */
234 Expr *pLeft, /* The left operand */
235 Expr *pRight, /* The right operand */
236 int opcode, /* The comparison opcode */
drh35573352008-01-08 23:54:25 +0000237 int in1, int in2, /* Register holding operands */
drhbe5c89a2004-07-26 00:31:09 +0000238 int dest, /* Jump here if true. */
239 int jumpIfNull /* If true, jump if either operand is NULL */
240){
drh35573352008-01-08 23:54:25 +0000241 int p5;
242 int addr;
243 CollSeq *p4;
244
245 p4 = sqlite3BinaryCompareCollSeq(pParse, pLeft, pRight);
246 p5 = binaryCompareP5(pLeft, pRight, jumpIfNull);
247 addr = sqlite3VdbeAddOp4(pParse->pVdbe, opcode, in2, dest, in1,
248 (void*)p4, P4_COLLSEQ);
249 sqlite3VdbeChangeP5(pParse->pVdbe, p5);
drhe49b1462008-07-09 01:39:44 +0000250 if( (p5 & SQLITE_AFF_MASK)!=SQLITE_AFF_NONE ){
drhda250ea2008-04-01 05:07:14 +0000251 sqlite3ExprCacheAffinityChange(pParse, in1, 1);
252 sqlite3ExprCacheAffinityChange(pParse, in2, 1);
drh2f7794c2008-04-01 03:27:39 +0000253 }
drh35573352008-01-08 23:54:25 +0000254 return addr;
drhbe5c89a2004-07-26 00:31:09 +0000255}
256
danielk19774b5255a2008-06-05 16:47:39 +0000257#if SQLITE_MAX_EXPR_DEPTH>0
258/*
259** Check that argument nHeight is less than or equal to the maximum
260** expression depth allowed. If it is not, leave an error message in
261** pParse.
262*/
263static int checkExprHeight(Parse *pParse, int nHeight){
264 int rc = SQLITE_OK;
265 int mxHeight = pParse->db->aLimit[SQLITE_LIMIT_EXPR_DEPTH];
266 if( nHeight>mxHeight ){
267 sqlite3ErrorMsg(pParse,
268 "Expression tree is too large (maximum depth %d)", mxHeight
269 );
270 rc = SQLITE_ERROR;
271 }
272 return rc;
273}
274
275/* The following three functions, heightOfExpr(), heightOfExprList()
276** and heightOfSelect(), are used to determine the maximum height
277** of any expression tree referenced by the structure passed as the
278** first argument.
279**
280** If this maximum height is greater than the current value pointed
281** to by pnHeight, the second parameter, then set *pnHeight to that
282** value.
283*/
284static void heightOfExpr(Expr *p, int *pnHeight){
285 if( p ){
286 if( p->nHeight>*pnHeight ){
287 *pnHeight = p->nHeight;
288 }
289 }
290}
291static void heightOfExprList(ExprList *p, int *pnHeight){
292 if( p ){
293 int i;
294 for(i=0; i<p->nExpr; i++){
295 heightOfExpr(p->a[i].pExpr, pnHeight);
296 }
297 }
298}
299static void heightOfSelect(Select *p, int *pnHeight){
300 if( p ){
301 heightOfExpr(p->pWhere, pnHeight);
302 heightOfExpr(p->pHaving, pnHeight);
303 heightOfExpr(p->pLimit, pnHeight);
304 heightOfExpr(p->pOffset, pnHeight);
305 heightOfExprList(p->pEList, pnHeight);
306 heightOfExprList(p->pGroupBy, pnHeight);
307 heightOfExprList(p->pOrderBy, pnHeight);
308 heightOfSelect(p->pPrior, pnHeight);
309 }
310}
311
312/*
313** Set the Expr.nHeight variable in the structure passed as an
314** argument. An expression with no children, Expr.pList or
315** Expr.pSelect member has a height of 1. Any other expression
316** has a height equal to the maximum height of any other
317** referenced Expr plus one.
318*/
319static void exprSetHeight(Expr *p){
320 int nHeight = 0;
321 heightOfExpr(p->pLeft, &nHeight);
322 heightOfExpr(p->pRight, &nHeight);
323 heightOfExprList(p->pList, &nHeight);
324 heightOfSelect(p->pSelect, &nHeight);
325 p->nHeight = nHeight + 1;
326}
327
328/*
329** Set the Expr.nHeight variable using the exprSetHeight() function. If
330** the height is greater than the maximum allowed expression depth,
331** leave an error in pParse.
332*/
333void sqlite3ExprSetHeight(Parse *pParse, Expr *p){
334 exprSetHeight(p);
335 checkExprHeight(pParse, p->nHeight);
336}
337
338/*
339** Return the maximum height of any expression tree referenced
340** by the select statement passed as an argument.
341*/
342int sqlite3SelectExprHeight(Select *p){
343 int nHeight = 0;
344 heightOfSelect(p, &nHeight);
345 return nHeight;
346}
347#else
348 #define checkExprHeight(x,y)
349 #define exprSetHeight(y)
350#endif /* SQLITE_MAX_EXPR_DEPTH>0 */
351
drhbe5c89a2004-07-26 00:31:09 +0000352/*
drha76b5df2002-02-23 02:32:10 +0000353** Construct a new expression node and return a pointer to it. Memory
drh17435752007-08-16 04:30:38 +0000354** for this node is obtained from sqlite3_malloc(). The calling function
drha76b5df2002-02-23 02:32:10 +0000355** is responsible for making sure the node eventually gets freed.
356*/
drh17435752007-08-16 04:30:38 +0000357Expr *sqlite3Expr(
danielk1977a1644fd2007-08-29 12:31:25 +0000358 sqlite3 *db, /* Handle for sqlite3DbMallocZero() (may be null) */
drh17435752007-08-16 04:30:38 +0000359 int op, /* Expression opcode */
360 Expr *pLeft, /* Left operand */
361 Expr *pRight, /* Right operand */
362 const Token *pToken /* Argument token */
363){
drha76b5df2002-02-23 02:32:10 +0000364 Expr *pNew;
drh26e4a8b2008-05-01 17:16:52 +0000365 pNew = sqlite3DbMallocZero(db, sizeof(Expr));
drha76b5df2002-02-23 02:32:10 +0000366 if( pNew==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000367 /* When malloc fails, delete pLeft and pRight. Expressions passed to
368 ** this function must always be allocated with sqlite3Expr() for this
369 ** reason.
370 */
drh633e6d52008-07-28 19:34:53 +0000371 sqlite3ExprDelete(db, pLeft);
372 sqlite3ExprDelete(db, pRight);
drha76b5df2002-02-23 02:32:10 +0000373 return 0;
374 }
375 pNew->op = op;
376 pNew->pLeft = pLeft;
377 pNew->pRight = pRight;
danielk1977a58fdfb2005-02-08 07:50:40 +0000378 pNew->iAgg = -1;
drhe49b1462008-07-09 01:39:44 +0000379 pNew->span.z = (u8*)"";
drha76b5df2002-02-23 02:32:10 +0000380 if( pToken ){
drh4b59ab52002-08-24 18:24:51 +0000381 assert( pToken->dyn==0 );
drh145716b2004-09-24 12:24:06 +0000382 pNew->span = pNew->token = *pToken;
drha34001c2007-02-02 12:44:37 +0000383 }else if( pLeft ){
384 if( pRight ){
drhe49b1462008-07-09 01:39:44 +0000385 if( pRight->span.dyn==0 && pLeft->span.dyn==0 ){
386 sqlite3ExprSpan(pNew, &pLeft->span, &pRight->span);
387 }
drh5ffb3ac2007-04-18 17:07:57 +0000388 if( pRight->flags & EP_ExpCollate ){
drha34001c2007-02-02 12:44:37 +0000389 pNew->flags |= EP_ExpCollate;
390 pNew->pColl = pRight->pColl;
391 }
392 }
drh5ffb3ac2007-04-18 17:07:57 +0000393 if( pLeft->flags & EP_ExpCollate ){
drha34001c2007-02-02 12:44:37 +0000394 pNew->flags |= EP_ExpCollate;
395 pNew->pColl = pLeft->pColl;
396 }
drha76b5df2002-02-23 02:32:10 +0000397 }
danielk1977fc976062007-05-10 10:46:56 +0000398
danielk19774b5255a2008-06-05 16:47:39 +0000399 exprSetHeight(pNew);
drha76b5df2002-02-23 02:32:10 +0000400 return pNew;
401}
402
403/*
drh17435752007-08-16 04:30:38 +0000404** Works like sqlite3Expr() except that it takes an extra Parse*
405** argument and notifies the associated connection object if malloc fails.
drh206f3d92006-07-11 13:15:08 +0000406*/
drh17435752007-08-16 04:30:38 +0000407Expr *sqlite3PExpr(
408 Parse *pParse, /* Parsing context */
409 int op, /* Expression opcode */
410 Expr *pLeft, /* Left operand */
411 Expr *pRight, /* Right operand */
412 const Token *pToken /* Argument token */
413){
danielk19774b5255a2008-06-05 16:47:39 +0000414 Expr *p = sqlite3Expr(pParse->db, op, pLeft, pRight, pToken);
415 if( p ){
416 checkExprHeight(pParse, p->nHeight);
417 }
418 return p;
drh206f3d92006-07-11 13:15:08 +0000419}
420
421/*
drh4e0cff62004-11-05 05:10:28 +0000422** When doing a nested parse, you can include terms in an expression
drhb7654112008-01-12 12:48:07 +0000423** that look like this: #1 #2 ... These terms refer to registers
424** in the virtual machine. #N is the N-th register.
drh4e0cff62004-11-05 05:10:28 +0000425**
426** This routine is called by the parser to deal with on of those terms.
427** It immediately generates code to store the value in a memory location.
428** The returns an expression that will code to extract the value from
429** that memory location as needed.
430*/
431Expr *sqlite3RegisterExpr(Parse *pParse, Token *pToken){
432 Vdbe *v = pParse->pVdbe;
433 Expr *p;
drh4e0cff62004-11-05 05:10:28 +0000434 if( pParse->nested==0 ){
435 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", pToken);
danielk1977a1644fd2007-08-29 12:31:25 +0000436 return sqlite3PExpr(pParse, TK_NULL, 0, 0, 0);
drh4e0cff62004-11-05 05:10:28 +0000437 }
drhbb7ac002005-08-12 22:58:53 +0000438 if( v==0 ) return 0;
danielk1977a1644fd2007-08-29 12:31:25 +0000439 p = sqlite3PExpr(pParse, TK_REGISTER, 0, 0, pToken);
drh73c42a12004-11-20 18:13:10 +0000440 if( p==0 ){
441 return 0; /* Malloc failed */
442 }
drhb7654112008-01-12 12:48:07 +0000443 p->iTable = atoi((char*)&pToken->z[1]);
drh4e0cff62004-11-05 05:10:28 +0000444 return p;
445}
446
447/*
drh91bb0ee2004-09-01 03:06:34 +0000448** Join two expressions using an AND operator. If either expression is
449** NULL, then just return the other expression.
450*/
danielk19771e536952007-08-16 10:09:01 +0000451Expr *sqlite3ExprAnd(sqlite3 *db, Expr *pLeft, Expr *pRight){
drh91bb0ee2004-09-01 03:06:34 +0000452 if( pLeft==0 ){
453 return pRight;
454 }else if( pRight==0 ){
455 return pLeft;
456 }else{
danielk1977880c15b2007-09-01 18:24:55 +0000457 return sqlite3Expr(db, TK_AND, pLeft, pRight, 0);
drh91bb0ee2004-09-01 03:06:34 +0000458 }
459}
460
461/*
drh6977fea2002-10-22 23:38:04 +0000462** Set the Expr.span field of the given expression to span all
drhe49b1462008-07-09 01:39:44 +0000463** text between the two given tokens. Both tokens must be pointing
464** at the same string.
drha76b5df2002-02-23 02:32:10 +0000465*/
danielk19774adee202004-05-08 08:23:19 +0000466void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
drh4efc4752004-01-16 15:55:37 +0000467 assert( pRight!=0 );
468 assert( pLeft!=0 );
drhe54a62a2008-07-18 17:03:52 +0000469 if( pExpr ){
drhe49b1462008-07-09 01:39:44 +0000470 pExpr->span.z = pLeft->z;
471 pExpr->span.n = pRight->n + (pRight->z - pLeft->z);
drha76b5df2002-02-23 02:32:10 +0000472 }
473}
474
475/*
476** Construct a new expression node for a function with multiple
477** arguments.
478*/
drh17435752007-08-16 04:30:38 +0000479Expr *sqlite3ExprFunction(Parse *pParse, ExprList *pList, Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000480 Expr *pNew;
drh633e6d52008-07-28 19:34:53 +0000481 sqlite3 *db = pParse->db;
danielk19774b202ae2006-01-23 05:50:58 +0000482 assert( pToken );
drh633e6d52008-07-28 19:34:53 +0000483 pNew = sqlite3DbMallocZero(db, sizeof(Expr) );
drha76b5df2002-02-23 02:32:10 +0000484 if( pNew==0 ){
drh633e6d52008-07-28 19:34:53 +0000485 sqlite3ExprListDelete(db, pList); /* Avoid leaking memory when malloc fails */
drha76b5df2002-02-23 02:32:10 +0000486 return 0;
487 }
488 pNew->op = TK_FUNCTION;
489 pNew->pList = pList;
danielk19774b202ae2006-01-23 05:50:58 +0000490 assert( pToken->dyn==0 );
491 pNew->token = *pToken;
drh6977fea2002-10-22 23:38:04 +0000492 pNew->span = pNew->token;
danielk1977fc976062007-05-10 10:46:56 +0000493
danielk19774b5255a2008-06-05 16:47:39 +0000494 sqlite3ExprSetHeight(pParse, pNew);
drha76b5df2002-02-23 02:32:10 +0000495 return pNew;
496}
497
498/*
drhfa6bc002004-09-07 16:19:52 +0000499** Assign a variable number to an expression that encodes a wildcard
500** in the original SQL statement.
501**
502** Wildcards consisting of a single "?" are assigned the next sequential
503** variable number.
504**
505** Wildcards of the form "?nnn" are assigned the number "nnn". We make
506** sure "nnn" is not too be to avoid a denial of service attack when
507** the SQL statement comes from an external source.
508**
509** Wildcards of the form ":aaa" or "$aaa" are assigned the same number
510** as the previous instance of the same wildcard. Or if this is the first
511** instance of the wildcard, the next sequenial variable number is
512** assigned.
513*/
514void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){
515 Token *pToken;
drh17435752007-08-16 04:30:38 +0000516 sqlite3 *db = pParse->db;
517
drhfa6bc002004-09-07 16:19:52 +0000518 if( pExpr==0 ) return;
519 pToken = &pExpr->token;
520 assert( pToken->n>=1 );
521 assert( pToken->z!=0 );
522 assert( pToken->z[0]!=0 );
523 if( pToken->n==1 ){
524 /* Wildcard of the form "?". Assign the next variable number */
525 pExpr->iTable = ++pParse->nVar;
526 }else if( pToken->z[0]=='?' ){
527 /* Wildcard of the form "?nnn". Convert "nnn" to an integer and
528 ** use it as the variable number */
529 int i;
drh2646da72005-12-09 20:02:05 +0000530 pExpr->iTable = i = atoi((char*)&pToken->z[1]);
drhc5499be2008-04-01 15:06:33 +0000531 testcase( i==0 );
532 testcase( i==1 );
533 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 );
534 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] );
drhbb4957f2008-03-20 14:03:29 +0000535 if( i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
drhfa6bc002004-09-07 16:19:52 +0000536 sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
drhbb4957f2008-03-20 14:03:29 +0000537 db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]);
drhfa6bc002004-09-07 16:19:52 +0000538 }
539 if( i>pParse->nVar ){
540 pParse->nVar = i;
541 }
542 }else{
543 /* Wildcards of the form ":aaa" or "$aaa". Reuse the same variable
544 ** number as the prior appearance of the same name, or if the name
545 ** has never appeared before, reuse the same variable number
546 */
547 int i, n;
548 n = pToken->n;
549 for(i=0; i<pParse->nVarExpr; i++){
550 Expr *pE;
551 if( (pE = pParse->apVarExpr[i])!=0
552 && pE->token.n==n
553 && memcmp(pE->token.z, pToken->z, n)==0 ){
554 pExpr->iTable = pE->iTable;
555 break;
556 }
557 }
558 if( i>=pParse->nVarExpr ){
559 pExpr->iTable = ++pParse->nVar;
560 if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){
561 pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10;
drh17435752007-08-16 04:30:38 +0000562 pParse->apVarExpr =
563 sqlite3DbReallocOrFree(
564 db,
565 pParse->apVarExpr,
566 pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0])
567 );
drhfa6bc002004-09-07 16:19:52 +0000568 }
drh17435752007-08-16 04:30:38 +0000569 if( !db->mallocFailed ){
drhfa6bc002004-09-07 16:19:52 +0000570 assert( pParse->apVarExpr!=0 );
571 pParse->apVarExpr[pParse->nVarExpr++] = pExpr;
572 }
573 }
574 }
drhbb4957f2008-03-20 14:03:29 +0000575 if( !pParse->nErr && pParse->nVar>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
danielk1977832b2662007-05-09 11:37:22 +0000576 sqlite3ErrorMsg(pParse, "too many SQL variables");
577 }
drhfa6bc002004-09-07 16:19:52 +0000578}
579
580/*
drha2e00042002-01-22 03:13:42 +0000581** Recursively delete an expression tree.
582*/
drh633e6d52008-07-28 19:34:53 +0000583void sqlite3ExprDelete(sqlite3 *db, Expr *p){
drha2e00042002-01-22 03:13:42 +0000584 if( p==0 ) return;
drh633e6d52008-07-28 19:34:53 +0000585 if( p->span.dyn ) sqlite3DbFree(db, (char*)p->span.z);
586 if( p->token.dyn ) sqlite3DbFree(db, (char*)p->token.z);
587 sqlite3ExprDelete(db, p->pLeft);
588 sqlite3ExprDelete(db, p->pRight);
589 sqlite3ExprListDelete(db, p->pList);
590 sqlite3SelectDelete(db, p->pSelect);
591 sqlite3DbFree(db, p);
drha2e00042002-01-22 03:13:42 +0000592}
593
drhd2687b72005-08-12 22:56:09 +0000594/*
595** The Expr.token field might be a string literal that is quoted.
596** If so, remove the quotation marks.
597*/
drh17435752007-08-16 04:30:38 +0000598void sqlite3DequoteExpr(sqlite3 *db, Expr *p){
drhd2687b72005-08-12 22:56:09 +0000599 if( ExprHasAnyProperty(p, EP_Dequoted) ){
600 return;
601 }
602 ExprSetProperty(p, EP_Dequoted);
603 if( p->token.dyn==0 ){
drh17435752007-08-16 04:30:38 +0000604 sqlite3TokenCopy(db, &p->token, &p->token);
drhd2687b72005-08-12 22:56:09 +0000605 }
606 sqlite3Dequote((char*)p->token.z);
607}
608
drha76b5df2002-02-23 02:32:10 +0000609
610/*
drhff78bd22002-02-27 01:47:11 +0000611** The following group of routines make deep copies of expressions,
612** expression lists, ID lists, and select statements. The copies can
613** be deleted (by being passed to their respective ...Delete() routines)
614** without effecting the originals.
615**
danielk19774adee202004-05-08 08:23:19 +0000616** The expression list, ID, and source lists return by sqlite3ExprListDup(),
617** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
drhad3cab52002-05-24 02:04:32 +0000618** by subsequent calls to sqlite*ListAppend() routines.
drhff78bd22002-02-27 01:47:11 +0000619**
drhad3cab52002-05-24 02:04:32 +0000620** Any tables that the SrcList might point to are not duplicated.
drhff78bd22002-02-27 01:47:11 +0000621*/
danielk19771e536952007-08-16 10:09:01 +0000622Expr *sqlite3ExprDup(sqlite3 *db, Expr *p){
drhff78bd22002-02-27 01:47:11 +0000623 Expr *pNew;
624 if( p==0 ) return 0;
drh17435752007-08-16 04:30:38 +0000625 pNew = sqlite3DbMallocRaw(db, sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000626 if( pNew==0 ) return 0;
drh3b167c72002-06-28 12:18:47 +0000627 memcpy(pNew, p, sizeof(*pNew));
drh6977fea2002-10-22 23:38:04 +0000628 if( p->token.z!=0 ){
drh17435752007-08-16 04:30:38 +0000629 pNew->token.z = (u8*)sqlite3DbStrNDup(db, (char*)p->token.z, p->token.n);
drh4b59ab52002-08-24 18:24:51 +0000630 pNew->token.dyn = 1;
631 }else{
drh4efc4752004-01-16 15:55:37 +0000632 assert( pNew->token.z==0 );
drh4b59ab52002-08-24 18:24:51 +0000633 }
drh6977fea2002-10-22 23:38:04 +0000634 pNew->span.z = 0;
drh17435752007-08-16 04:30:38 +0000635 pNew->pLeft = sqlite3ExprDup(db, p->pLeft);
636 pNew->pRight = sqlite3ExprDup(db, p->pRight);
637 pNew->pList = sqlite3ExprListDup(db, p->pList);
638 pNew->pSelect = sqlite3SelectDup(db, p->pSelect);
drhff78bd22002-02-27 01:47:11 +0000639 return pNew;
640}
drh17435752007-08-16 04:30:38 +0000641void sqlite3TokenCopy(sqlite3 *db, Token *pTo, Token *pFrom){
drh633e6d52008-07-28 19:34:53 +0000642 if( pTo->dyn ) sqlite3DbFree(db, (char*)pTo->z);
drh4b59ab52002-08-24 18:24:51 +0000643 if( pFrom->z ){
644 pTo->n = pFrom->n;
drh17435752007-08-16 04:30:38 +0000645 pTo->z = (u8*)sqlite3DbStrNDup(db, (char*)pFrom->z, pFrom->n);
drh4b59ab52002-08-24 18:24:51 +0000646 pTo->dyn = 1;
647 }else{
drh4b59ab52002-08-24 18:24:51 +0000648 pTo->z = 0;
drh4b59ab52002-08-24 18:24:51 +0000649 }
650}
drh17435752007-08-16 04:30:38 +0000651ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p){
drhff78bd22002-02-27 01:47:11 +0000652 ExprList *pNew;
drh145716b2004-09-24 12:24:06 +0000653 struct ExprList_item *pItem, *pOldItem;
drhff78bd22002-02-27 01:47:11 +0000654 int i;
655 if( p==0 ) return 0;
drh17435752007-08-16 04:30:38 +0000656 pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +0000657 if( pNew==0 ) return 0;
danielk197731dad9d2007-08-16 11:36:15 +0000658 pNew->iECursor = 0;
drh4305d102003-07-30 12:34:12 +0000659 pNew->nExpr = pNew->nAlloc = p->nExpr;
drh17435752007-08-16 04:30:38 +0000660 pNew->a = pItem = sqlite3DbMallocRaw(db, p->nExpr*sizeof(p->a[0]) );
danielk1977e0048402004-06-15 16:51:01 +0000661 if( pItem==0 ){
drh633e6d52008-07-28 19:34:53 +0000662 sqlite3DbFree(db, pNew);
danielk1977e0048402004-06-15 16:51:01 +0000663 return 0;
664 }
drh145716b2004-09-24 12:24:06 +0000665 pOldItem = p->a;
666 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
drh4b59ab52002-08-24 18:24:51 +0000667 Expr *pNewExpr, *pOldExpr;
drh17435752007-08-16 04:30:38 +0000668 pItem->pExpr = pNewExpr = sqlite3ExprDup(db, pOldExpr = pOldItem->pExpr);
drh6977fea2002-10-22 23:38:04 +0000669 if( pOldExpr->span.z!=0 && pNewExpr ){
670 /* Always make a copy of the span for top-level expressions in the
drh4b59ab52002-08-24 18:24:51 +0000671 ** expression list. The logic in SELECT processing that determines
672 ** the names of columns in the result set needs this information */
drh17435752007-08-16 04:30:38 +0000673 sqlite3TokenCopy(db, &pNewExpr->span, &pOldExpr->span);
drh4b59ab52002-08-24 18:24:51 +0000674 }
drh1f3e9052002-10-31 00:09:39 +0000675 assert( pNewExpr==0 || pNewExpr->span.z!=0
drh6f7adc82006-01-11 21:41:20 +0000676 || pOldExpr->span.z==0
drh17435752007-08-16 04:30:38 +0000677 || db->mallocFailed );
678 pItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
drh145716b2004-09-24 12:24:06 +0000679 pItem->sortOrder = pOldItem->sortOrder;
680 pItem->isAgg = pOldItem->isAgg;
drh3e7bc9c2004-02-21 19:17:17 +0000681 pItem->done = 0;
drhff78bd22002-02-27 01:47:11 +0000682 }
683 return pNew;
684}
danielk197793758c82005-01-21 08:13:14 +0000685
686/*
687** If cursors, triggers, views and subqueries are all omitted from
688** the build, then none of the following routines, except for
689** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes
690** called with a NULL argument.
691*/
danielk19776a67fe82005-02-04 04:07:16 +0000692#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \
693 || !defined(SQLITE_OMIT_SUBQUERY)
drh17435752007-08-16 04:30:38 +0000694SrcList *sqlite3SrcListDup(sqlite3 *db, SrcList *p){
drhad3cab52002-05-24 02:04:32 +0000695 SrcList *pNew;
696 int i;
drh113088e2003-03-20 01:16:58 +0000697 int nByte;
drhad3cab52002-05-24 02:04:32 +0000698 if( p==0 ) return 0;
drh113088e2003-03-20 01:16:58 +0000699 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
drh17435752007-08-16 04:30:38 +0000700 pNew = sqlite3DbMallocRaw(db, nByte );
drhad3cab52002-05-24 02:04:32 +0000701 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000702 pNew->nSrc = pNew->nAlloc = p->nSrc;
drhad3cab52002-05-24 02:04:32 +0000703 for(i=0; i<p->nSrc; i++){
drh4efc4752004-01-16 15:55:37 +0000704 struct SrcList_item *pNewItem = &pNew->a[i];
705 struct SrcList_item *pOldItem = &p->a[i];
drhed8a3bb2005-06-06 21:19:56 +0000706 Table *pTab;
drh17435752007-08-16 04:30:38 +0000707 pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase);
708 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
709 pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias);
drh4efc4752004-01-16 15:55:37 +0000710 pNewItem->jointype = pOldItem->jointype;
711 pNewItem->iCursor = pOldItem->iCursor;
danielk19771787cca2006-02-10 07:07:14 +0000712 pNewItem->isPopulated = pOldItem->isPopulated;
drhed8a3bb2005-06-06 21:19:56 +0000713 pTab = pNewItem->pTab = pOldItem->pTab;
714 if( pTab ){
715 pTab->nRef++;
danielk1977a1cb1832005-02-12 08:59:55 +0000716 }
drh17435752007-08-16 04:30:38 +0000717 pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect);
718 pNewItem->pOn = sqlite3ExprDup(db, pOldItem->pOn);
719 pNewItem->pUsing = sqlite3IdListDup(db, pOldItem->pUsing);
danielk19776c18b6e2005-01-30 09:17:58 +0000720 pNewItem->colUsed = pOldItem->colUsed;
drhad3cab52002-05-24 02:04:32 +0000721 }
722 return pNew;
723}
drh17435752007-08-16 04:30:38 +0000724IdList *sqlite3IdListDup(sqlite3 *db, IdList *p){
drhff78bd22002-02-27 01:47:11 +0000725 IdList *pNew;
726 int i;
727 if( p==0 ) return 0;
drh17435752007-08-16 04:30:38 +0000728 pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +0000729 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000730 pNew->nId = pNew->nAlloc = p->nId;
drh17435752007-08-16 04:30:38 +0000731 pNew->a = sqlite3DbMallocRaw(db, p->nId*sizeof(p->a[0]) );
danielk1977d5d56522005-03-16 12:15:20 +0000732 if( pNew->a==0 ){
drh633e6d52008-07-28 19:34:53 +0000733 sqlite3DbFree(db, pNew);
danielk1977d5d56522005-03-16 12:15:20 +0000734 return 0;
735 }
drhff78bd22002-02-27 01:47:11 +0000736 for(i=0; i<p->nId; i++){
drh4efc4752004-01-16 15:55:37 +0000737 struct IdList_item *pNewItem = &pNew->a[i];
738 struct IdList_item *pOldItem = &p->a[i];
drh17435752007-08-16 04:30:38 +0000739 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
drh4efc4752004-01-16 15:55:37 +0000740 pNewItem->idx = pOldItem->idx;
drhff78bd22002-02-27 01:47:11 +0000741 }
742 return pNew;
743}
drh17435752007-08-16 04:30:38 +0000744Select *sqlite3SelectDup(sqlite3 *db, Select *p){
drhff78bd22002-02-27 01:47:11 +0000745 Select *pNew;
746 if( p==0 ) return 0;
drh17435752007-08-16 04:30:38 +0000747 pNew = sqlite3DbMallocRaw(db, sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000748 if( pNew==0 ) return 0;
749 pNew->isDistinct = p->isDistinct;
drh17435752007-08-16 04:30:38 +0000750 pNew->pEList = sqlite3ExprListDup(db, p->pEList);
751 pNew->pSrc = sqlite3SrcListDup(db, p->pSrc);
752 pNew->pWhere = sqlite3ExprDup(db, p->pWhere);
753 pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy);
754 pNew->pHaving = sqlite3ExprDup(db, p->pHaving);
755 pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy);
drhff78bd22002-02-27 01:47:11 +0000756 pNew->op = p->op;
drh17435752007-08-16 04:30:38 +0000757 pNew->pPrior = sqlite3SelectDup(db, p->pPrior);
758 pNew->pLimit = sqlite3ExprDup(db, p->pLimit);
759 pNew->pOffset = sqlite3ExprDup(db, p->pOffset);
drh92b01d52008-06-24 00:32:35 +0000760 pNew->iLimit = 0;
761 pNew->iOffset = 0;
danielk1977a1cb1832005-02-12 08:59:55 +0000762 pNew->isResolved = p->isResolved;
763 pNew->isAgg = p->isAgg;
drhb9bb7c12006-06-11 23:41:55 +0000764 pNew->usesEphm = 0;
drh8e647b82005-09-23 21:11:53 +0000765 pNew->disallowOrderBy = 0;
drh0342b1f2005-09-01 03:07:44 +0000766 pNew->pRightmost = 0;
drhb9bb7c12006-06-11 23:41:55 +0000767 pNew->addrOpenEphm[0] = -1;
768 pNew->addrOpenEphm[1] = -1;
769 pNew->addrOpenEphm[2] = -1;
drhff78bd22002-02-27 01:47:11 +0000770 return pNew;
771}
danielk197793758c82005-01-21 08:13:14 +0000772#else
drh17435752007-08-16 04:30:38 +0000773Select *sqlite3SelectDup(sqlite3 *db, Select *p){
danielk197793758c82005-01-21 08:13:14 +0000774 assert( p==0 );
775 return 0;
776}
777#endif
drhff78bd22002-02-27 01:47:11 +0000778
779
780/*
drha76b5df2002-02-23 02:32:10 +0000781** Add a new element to the end of an expression list. If pList is
782** initially NULL, then create a new expression list.
783*/
drh17435752007-08-16 04:30:38 +0000784ExprList *sqlite3ExprListAppend(
785 Parse *pParse, /* Parsing context */
786 ExprList *pList, /* List to which to append. Might be NULL */
787 Expr *pExpr, /* Expression to be appended */
788 Token *pName /* AS keyword for the expression */
789){
790 sqlite3 *db = pParse->db;
drha76b5df2002-02-23 02:32:10 +0000791 if( pList==0 ){
drh17435752007-08-16 04:30:38 +0000792 pList = sqlite3DbMallocZero(db, sizeof(ExprList) );
drha76b5df2002-02-23 02:32:10 +0000793 if( pList==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000794 goto no_mem;
drha76b5df2002-02-23 02:32:10 +0000795 }
drh4efc4752004-01-16 15:55:37 +0000796 assert( pList->nAlloc==0 );
drha76b5df2002-02-23 02:32:10 +0000797 }
drh4305d102003-07-30 12:34:12 +0000798 if( pList->nAlloc<=pList->nExpr ){
danielk1977d5d56522005-03-16 12:15:20 +0000799 struct ExprList_item *a;
800 int n = pList->nAlloc*2 + 4;
danielk197726783a52007-08-29 14:06:22 +0000801 a = sqlite3DbRealloc(db, pList->a, n*sizeof(pList->a[0]));
danielk1977d5d56522005-03-16 12:15:20 +0000802 if( a==0 ){
803 goto no_mem;
drha76b5df2002-02-23 02:32:10 +0000804 }
danielk1977d5d56522005-03-16 12:15:20 +0000805 pList->a = a;
806 pList->nAlloc = n;
drha76b5df2002-02-23 02:32:10 +0000807 }
drh4efc4752004-01-16 15:55:37 +0000808 assert( pList->a!=0 );
809 if( pExpr || pName ){
810 struct ExprList_item *pItem = &pList->a[pList->nExpr++];
811 memset(pItem, 0, sizeof(*pItem));
drh17435752007-08-16 04:30:38 +0000812 pItem->zName = sqlite3NameFromToken(db, pName);
danielk1977e94ddc92005-03-21 03:53:38 +0000813 pItem->pExpr = pExpr;
drha76b5df2002-02-23 02:32:10 +0000814 }
815 return pList;
danielk1977d5d56522005-03-16 12:15:20 +0000816
817no_mem:
818 /* Avoid leaking memory if malloc has failed. */
drh633e6d52008-07-28 19:34:53 +0000819 sqlite3ExprDelete(db, pExpr);
820 sqlite3ExprListDelete(db, pList);
danielk1977d5d56522005-03-16 12:15:20 +0000821 return 0;
drha76b5df2002-02-23 02:32:10 +0000822}
823
824/*
danielk19777a15a4b2007-05-08 17:54:43 +0000825** If the expression list pEList contains more than iLimit elements,
826** leave an error message in pParse.
827*/
828void sqlite3ExprListCheckLength(
829 Parse *pParse,
830 ExprList *pEList,
danielk19777a15a4b2007-05-08 17:54:43 +0000831 const char *zObject
832){
drhb1a6c3c2008-03-20 16:30:17 +0000833 int mx = pParse->db->aLimit[SQLITE_LIMIT_COLUMN];
drhc5499be2008-04-01 15:06:33 +0000834 testcase( pEList && pEList->nExpr==mx );
835 testcase( pEList && pEList->nExpr==mx+1 );
drhb1a6c3c2008-03-20 16:30:17 +0000836 if( pEList && pEList->nExpr>mx ){
danielk19777a15a4b2007-05-08 17:54:43 +0000837 sqlite3ErrorMsg(pParse, "too many columns in %s", zObject);
838 }
839}
840
841/*
drha76b5df2002-02-23 02:32:10 +0000842** Delete an entire expression list.
843*/
drh633e6d52008-07-28 19:34:53 +0000844void sqlite3ExprListDelete(sqlite3 *db, ExprList *pList){
drha76b5df2002-02-23 02:32:10 +0000845 int i;
drhbe5c89a2004-07-26 00:31:09 +0000846 struct ExprList_item *pItem;
drha76b5df2002-02-23 02:32:10 +0000847 if( pList==0 ) return;
drh1bdd9b52004-04-23 17:04:44 +0000848 assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
849 assert( pList->nExpr<=pList->nAlloc );
drhbe5c89a2004-07-26 00:31:09 +0000850 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
drh633e6d52008-07-28 19:34:53 +0000851 sqlite3ExprDelete(db, pItem->pExpr);
852 sqlite3DbFree(db, pItem->zName);
drha76b5df2002-02-23 02:32:10 +0000853 }
drh633e6d52008-07-28 19:34:53 +0000854 sqlite3DbFree(db, pList->a);
855 sqlite3DbFree(db, pList);
drha76b5df2002-02-23 02:32:10 +0000856}
857
858/*
drh678ccce2008-03-31 18:19:54 +0000859** Walk an expression tree. Call xFunc for each node visited. xFunc
860** is called on the node before xFunc is called on the nodes children.
drh73b211a2005-01-18 04:00:42 +0000861**
drh626a8792005-01-17 22:08:19 +0000862** The return value from xFunc determines whether the tree walk continues.
863** 0 means continue walking the tree. 1 means do not walk children
864** of the current node but continue with siblings. 2 means abandon
865** the tree walk completely.
866**
867** The return value from this routine is 1 to abandon the tree walk
868** and 0 to continue.
drh87abf5c2005-08-25 12:45:04 +0000869**
870** NOTICE: This routine does *not* descend into subqueries.
drh626a8792005-01-17 22:08:19 +0000871*/
danielk1977a58fdfb2005-02-08 07:50:40 +0000872static int walkExprList(ExprList *, int (*)(void *, Expr*), void *);
drh626a8792005-01-17 22:08:19 +0000873static int walkExprTree(Expr *pExpr, int (*xFunc)(void*,Expr*), void *pArg){
drh626a8792005-01-17 22:08:19 +0000874 int rc;
875 if( pExpr==0 ) return 0;
876 rc = (*xFunc)(pArg, pExpr);
877 if( rc==0 ){
878 if( walkExprTree(pExpr->pLeft, xFunc, pArg) ) return 1;
879 if( walkExprTree(pExpr->pRight, xFunc, pArg) ) return 1;
danielk1977a58fdfb2005-02-08 07:50:40 +0000880 if( walkExprList(pExpr->pList, xFunc, pArg) ) return 1;
drh626a8792005-01-17 22:08:19 +0000881 }
882 return rc>1;
883}
884
885/*
danielk1977a58fdfb2005-02-08 07:50:40 +0000886** Call walkExprTree() for every expression in list p.
887*/
888static int walkExprList(ExprList *p, int (*xFunc)(void *, Expr*), void *pArg){
889 int i;
890 struct ExprList_item *pItem;
891 if( !p ) return 0;
892 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
893 if( walkExprTree(pItem->pExpr, xFunc, pArg) ) return 1;
894 }
895 return 0;
896}
897
898/*
899** Call walkExprTree() for every expression in Select p, not including
900** expressions that are part of sub-selects in any FROM clause or the LIMIT
901** or OFFSET expressions..
902*/
903static int walkSelectExpr(Select *p, int (*xFunc)(void *, Expr*), void *pArg){
904 walkExprList(p->pEList, xFunc, pArg);
905 walkExprTree(p->pWhere, xFunc, pArg);
906 walkExprList(p->pGroupBy, xFunc, pArg);
907 walkExprTree(p->pHaving, xFunc, pArg);
908 walkExprList(p->pOrderBy, xFunc, pArg);
danielk197715d79822007-05-15 07:00:34 +0000909 if( p->pPrior ){
910 walkSelectExpr(p->pPrior, xFunc, pArg);
911 }
danielk1977a58fdfb2005-02-08 07:50:40 +0000912 return 0;
913}
914
915
916/*
drh626a8792005-01-17 22:08:19 +0000917** This routine is designed as an xFunc for walkExprTree().
918**
919** pArg is really a pointer to an integer. If we can tell by looking
drh73b211a2005-01-18 04:00:42 +0000920** at pExpr that the expression that contains pExpr is not a constant
921** expression, then set *pArg to 0 and return 2 to abandon the tree walk.
922** If pExpr does does not disqualify the expression from being a constant
923** then do nothing.
924**
925** After walking the whole tree, if no nodes are found that disqualify
926** the expression as constant, then we assume the whole expression
927** is constant. See sqlite3ExprIsConstant() for additional information.
drh626a8792005-01-17 22:08:19 +0000928*/
929static int exprNodeIsConstant(void *pArg, Expr *pExpr){
drh0a168372007-06-08 00:20:47 +0000930 int *pN = (int*)pArg;
931
932 /* If *pArg is 3 then any term of the expression that comes from
933 ** the ON or USING clauses of a join disqualifies the expression
934 ** from being considered constant. */
935 if( (*pN)==3 && ExprHasAnyProperty(pExpr, EP_FromJoin) ){
936 *pN = 0;
937 return 2;
938 }
939
drh626a8792005-01-17 22:08:19 +0000940 switch( pExpr->op ){
drheb55bd22005-06-30 17:04:21 +0000941 /* Consider functions to be constant if all their arguments are constant
942 ** and *pArg==2 */
943 case TK_FUNCTION:
drh0a168372007-06-08 00:20:47 +0000944 if( (*pN)==2 ) return 0;
drheb55bd22005-06-30 17:04:21 +0000945 /* Fall through */
drh626a8792005-01-17 22:08:19 +0000946 case TK_ID:
947 case TK_COLUMN:
948 case TK_DOT:
949 case TK_AGG_FUNCTION:
drh13449892005-09-07 21:22:45 +0000950 case TK_AGG_COLUMN:
drhfe2093d2005-01-20 22:48:47 +0000951#ifndef SQLITE_OMIT_SUBQUERY
952 case TK_SELECT:
953 case TK_EXISTS:
drhc5499be2008-04-01 15:06:33 +0000954 testcase( pExpr->op==TK_SELECT );
955 testcase( pExpr->op==TK_EXISTS );
drhfe2093d2005-01-20 22:48:47 +0000956#endif
drhc5499be2008-04-01 15:06:33 +0000957 testcase( pExpr->op==TK_ID );
958 testcase( pExpr->op==TK_COLUMN );
959 testcase( pExpr->op==TK_DOT );
960 testcase( pExpr->op==TK_AGG_FUNCTION );
961 testcase( pExpr->op==TK_AGG_COLUMN );
drh0a168372007-06-08 00:20:47 +0000962 *pN = 0;
drh626a8792005-01-17 22:08:19 +0000963 return 2;
drh87abf5c2005-08-25 12:45:04 +0000964 case TK_IN:
965 if( pExpr->pSelect ){
drh0a168372007-06-08 00:20:47 +0000966 *pN = 0;
drh87abf5c2005-08-25 12:45:04 +0000967 return 2;
968 }
drh626a8792005-01-17 22:08:19 +0000969 default:
970 return 0;
971 }
972}
973
974/*
drhfef52082000-06-06 01:50:43 +0000975** Walk an expression tree. Return 1 if the expression is constant
drheb55bd22005-06-30 17:04:21 +0000976** and 0 if it involves variables or function calls.
drh23989372002-05-21 13:43:04 +0000977**
978** For the purposes of this function, a double-quoted string (ex: "abc")
979** is considered a variable but a single-quoted string (ex: 'abc') is
980** a constant.
drhfef52082000-06-06 01:50:43 +0000981*/
danielk19774adee202004-05-08 08:23:19 +0000982int sqlite3ExprIsConstant(Expr *p){
drh626a8792005-01-17 22:08:19 +0000983 int isConst = 1;
984 walkExprTree(p, exprNodeIsConstant, &isConst);
985 return isConst;
drhfef52082000-06-06 01:50:43 +0000986}
987
988/*
drheb55bd22005-06-30 17:04:21 +0000989** Walk an expression tree. Return 1 if the expression is constant
drh0a168372007-06-08 00:20:47 +0000990** that does no originate from the ON or USING clauses of a join.
991** Return 0 if it involves variables or function calls or terms from
992** an ON or USING clause.
993*/
994int sqlite3ExprIsConstantNotJoin(Expr *p){
995 int isConst = 3;
996 walkExprTree(p, exprNodeIsConstant, &isConst);
997 return isConst!=0;
998}
999
1000/*
1001** Walk an expression tree. Return 1 if the expression is constant
drheb55bd22005-06-30 17:04:21 +00001002** or a function call with constant arguments. Return and 0 if there
1003** are any variables.
1004**
1005** For the purposes of this function, a double-quoted string (ex: "abc")
1006** is considered a variable but a single-quoted string (ex: 'abc') is
1007** a constant.
1008*/
1009int sqlite3ExprIsConstantOrFunction(Expr *p){
1010 int isConst = 2;
1011 walkExprTree(p, exprNodeIsConstant, &isConst);
1012 return isConst!=0;
1013}
1014
1015/*
drh73b211a2005-01-18 04:00:42 +00001016** If the expression p codes a constant integer that is small enough
drh202b2df2004-01-06 01:13:46 +00001017** to fit in a 32-bit integer, return 1 and put the value of the integer
1018** in *pValue. If the expression is not an integer or if it is too big
1019** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
drhe4de1fe2002-06-02 16:09:01 +00001020*/
danielk19774adee202004-05-08 08:23:19 +00001021int sqlite3ExprIsInteger(Expr *p, int *pValue){
drh92b01d52008-06-24 00:32:35 +00001022 int rc = 0;
1023 if( p->flags & EP_IntValue ){
1024 *pValue = p->iTable;
1025 return 1;
1026 }
drhe4de1fe2002-06-02 16:09:01 +00001027 switch( p->op ){
1028 case TK_INTEGER: {
drh92b01d52008-06-24 00:32:35 +00001029 rc = sqlite3GetInt32((char*)p->token.z, pValue);
drh202b2df2004-01-06 01:13:46 +00001030 break;
drhe4de1fe2002-06-02 16:09:01 +00001031 }
drh4b59ab52002-08-24 18:24:51 +00001032 case TK_UPLUS: {
drh92b01d52008-06-24 00:32:35 +00001033 rc = sqlite3ExprIsInteger(p->pLeft, pValue);
drhf6e369a2008-06-24 12:46:30 +00001034 break;
drh4b59ab52002-08-24 18:24:51 +00001035 }
drhe4de1fe2002-06-02 16:09:01 +00001036 case TK_UMINUS: {
1037 int v;
danielk19774adee202004-05-08 08:23:19 +00001038 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
drhe4de1fe2002-06-02 16:09:01 +00001039 *pValue = -v;
drh92b01d52008-06-24 00:32:35 +00001040 rc = 1;
drhe4de1fe2002-06-02 16:09:01 +00001041 }
1042 break;
1043 }
1044 default: break;
1045 }
drh92b01d52008-06-24 00:32:35 +00001046 if( rc ){
1047 p->op = TK_INTEGER;
1048 p->flags |= EP_IntValue;
1049 p->iTable = *pValue;
1050 }
1051 return rc;
drhe4de1fe2002-06-02 16:09:01 +00001052}
1053
1054/*
drhc4a3c772001-04-04 11:48:57 +00001055** Return TRUE if the given string is a row-id column name.
1056*/
danielk19774adee202004-05-08 08:23:19 +00001057int sqlite3IsRowid(const char *z){
1058 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
1059 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
1060 if( sqlite3StrICmp(z, "OID")==0 ) return 1;
drhc4a3c772001-04-04 11:48:57 +00001061 return 0;
1062}
1063
1064/*
drh8141f612004-01-25 22:44:58 +00001065** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
1066** that name in the set of source tables in pSrcList and make the pExpr
1067** expression node refer back to that source column. The following changes
1068** are made to pExpr:
1069**
1070** pExpr->iDb Set the index in db->aDb[] of the database holding
1071** the table.
1072** pExpr->iTable Set to the cursor number for the table obtained
1073** from pSrcList.
1074** pExpr->iColumn Set to the column number within the table.
drh8141f612004-01-25 22:44:58 +00001075** pExpr->op Set to TK_COLUMN.
1076** pExpr->pLeft Any expression this points to is deleted
1077** pExpr->pRight Any expression this points to is deleted.
1078**
1079** The pDbToken is the name of the database (the "X"). This value may be
1080** NULL meaning that name is of the form Y.Z or Z. Any available database
1081** can be used. The pTableToken is the name of the table (the "Y"). This
1082** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it
1083** means that the form of the name is Z and that columns from any table
1084** can be used.
1085**
1086** If the name cannot be resolved unambiguously, leave an error message
1087** in pParse and return non-zero. Return zero on success.
1088*/
1089static int lookupName(
drhffe07b22005-11-03 00:41:17 +00001090 Parse *pParse, /* The parsing context */
drh8141f612004-01-25 22:44:58 +00001091 Token *pDbToken, /* Name of the database containing table, or NULL */
1092 Token *pTableToken, /* Name of table containing column, or NULL */
1093 Token *pColumnToken, /* Name of the column. */
drh626a8792005-01-17 22:08:19 +00001094 NameContext *pNC, /* The name context used to resolve the name */
drh8141f612004-01-25 22:44:58 +00001095 Expr *pExpr /* Make this EXPR node point to the selected column */
1096){
1097 char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */
1098 char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */
1099 char *zCol = 0; /* Name of the column. The "Z" */
1100 int i, j; /* Loop counters */
1101 int cnt = 0; /* Number of matching column names */
1102 int cntTab = 0; /* Number of matching table names */
drh626a8792005-01-17 22:08:19 +00001103 sqlite3 *db = pParse->db; /* The database */
drh51669862004-12-18 18:40:26 +00001104 struct SrcList_item *pItem; /* Use for looping over pSrcList items */
1105 struct SrcList_item *pMatch = 0; /* The matching pSrcList item */
drh73b211a2005-01-18 04:00:42 +00001106 NameContext *pTopNC = pNC; /* First namecontext in the list */
drh728b5772007-09-18 15:55:07 +00001107 Schema *pSchema = 0; /* Schema of the expression */
drh8141f612004-01-25 22:44:58 +00001108
1109 assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
drh17435752007-08-16 04:30:38 +00001110 zDb = sqlite3NameFromToken(db, pDbToken);
1111 zTab = sqlite3NameFromToken(db, pTableToken);
1112 zCol = sqlite3NameFromToken(db, pColumnToken);
1113 if( db->mallocFailed ){
danielk1977d5d56522005-03-16 12:15:20 +00001114 goto lookupname_end;
drh8141f612004-01-25 22:44:58 +00001115 }
drh8141f612004-01-25 22:44:58 +00001116
1117 pExpr->iTable = -1;
drh626a8792005-01-17 22:08:19 +00001118 while( pNC && cnt==0 ){
drhffe07b22005-11-03 00:41:17 +00001119 ExprList *pEList;
drh626a8792005-01-17 22:08:19 +00001120 SrcList *pSrcList = pNC->pSrcList;
drh8141f612004-01-25 22:44:58 +00001121
danielk1977b3bce662005-01-29 08:32:43 +00001122 if( pSrcList ){
1123 for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
drh43617e92006-03-06 20:55:46 +00001124 Table *pTab;
1125 int iDb;
danielk1977b3bce662005-01-29 08:32:43 +00001126 Column *pCol;
1127
drh43617e92006-03-06 20:55:46 +00001128 pTab = pItem->pTab;
1129 assert( pTab!=0 );
1130 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
danielk1977b3bce662005-01-29 08:32:43 +00001131 assert( pTab->nCol>0 );
1132 if( zTab ){
1133 if( pItem->zAlias ){
1134 char *zTabName = pItem->zAlias;
1135 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
1136 }else{
1137 char *zTabName = pTab->zName;
1138 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
danielk1977da184232006-01-05 11:34:32 +00001139 if( zDb!=0 && sqlite3StrICmp(db->aDb[iDb].zName, zDb)!=0 ){
danielk1977b3bce662005-01-29 08:32:43 +00001140 continue;
1141 }
drh626a8792005-01-17 22:08:19 +00001142 }
drh8141f612004-01-25 22:44:58 +00001143 }
danielk1977b3bce662005-01-29 08:32:43 +00001144 if( 0==(cntTab++) ){
drh626a8792005-01-17 22:08:19 +00001145 pExpr->iTable = pItem->iCursor;
drh728b5772007-09-18 15:55:07 +00001146 pSchema = pTab->pSchema;
danielk1977b3bce662005-01-29 08:32:43 +00001147 pMatch = pItem;
1148 }
1149 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
1150 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
danielk1977b3bf5562006-01-10 17:58:23 +00001151 const char *zColl = pTab->aCol[j].zColl;
drh873fac02005-06-06 17:11:46 +00001152 IdList *pUsing;
danielk1977b3bce662005-01-29 08:32:43 +00001153 cnt++;
1154 pExpr->iTable = pItem->iCursor;
1155 pMatch = pItem;
drh728b5772007-09-18 15:55:07 +00001156 pSchema = pTab->pSchema;
danielk1977b3bce662005-01-29 08:32:43 +00001157 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
1158 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
1159 pExpr->affinity = pTab->aCol[j].affinity;
drh8b4c40d2007-02-01 23:02:45 +00001160 if( (pExpr->flags & EP_ExpCollate)==0 ){
1161 pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0);
1162 }
drh61dfc312006-12-16 16:25:15 +00001163 if( i<pSrcList->nSrc-1 ){
1164 if( pItem[1].jointype & JT_NATURAL ){
1165 /* If this match occurred in the left table of a natural join,
1166 ** then skip the right table to avoid a duplicate match */
1167 pItem++;
1168 i++;
1169 }else if( (pUsing = pItem[1].pUsing)!=0 ){
1170 /* If this match occurs on a column that is in the USING clause
1171 ** of a join, skip the search of the right table of the join
1172 ** to avoid a duplicate match there. */
1173 int k;
1174 for(k=0; k<pUsing->nId; k++){
1175 if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ){
1176 pItem++;
1177 i++;
1178 break;
1179 }
drh873fac02005-06-06 17:11:46 +00001180 }
1181 }
1182 }
danielk1977b3bce662005-01-29 08:32:43 +00001183 break;
1184 }
drh8141f612004-01-25 22:44:58 +00001185 }
1186 }
1187 }
drh626a8792005-01-17 22:08:19 +00001188
1189#ifndef SQLITE_OMIT_TRIGGER
1190 /* If we have not already resolved the name, then maybe
1191 ** it is a new.* or old.* trigger argument reference
1192 */
1193 if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
1194 TriggerStack *pTriggerStack = pParse->trigStack;
1195 Table *pTab = 0;
danielk19778f2c54e2008-01-01 19:02:09 +00001196 u32 *piColMask;
drh626a8792005-01-17 22:08:19 +00001197 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){
1198 pExpr->iTable = pTriggerStack->newIdx;
1199 assert( pTriggerStack->pTab );
1200 pTab = pTriggerStack->pTab;
danielk19778f2c54e2008-01-01 19:02:09 +00001201 piColMask = &(pTriggerStack->newColMask);
drh626a8792005-01-17 22:08:19 +00001202 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab)==0 ){
1203 pExpr->iTable = pTriggerStack->oldIdx;
1204 assert( pTriggerStack->pTab );
1205 pTab = pTriggerStack->pTab;
danielk19778f2c54e2008-01-01 19:02:09 +00001206 piColMask = &(pTriggerStack->oldColMask);
drh626a8792005-01-17 22:08:19 +00001207 }
1208
1209 if( pTab ){
danielk1977f0113002006-01-24 12:09:17 +00001210 int iCol;
drh626a8792005-01-17 22:08:19 +00001211 Column *pCol = pTab->aCol;
1212
drh728b5772007-09-18 15:55:07 +00001213 pSchema = pTab->pSchema;
drh626a8792005-01-17 22:08:19 +00001214 cntTab++;
danielk1977f0113002006-01-24 12:09:17 +00001215 for(iCol=0; iCol < pTab->nCol; iCol++, pCol++) {
drh626a8792005-01-17 22:08:19 +00001216 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
danielk1977f0113002006-01-24 12:09:17 +00001217 const char *zColl = pTab->aCol[iCol].zColl;
drh626a8792005-01-17 22:08:19 +00001218 cnt++;
danielk1977f0113002006-01-24 12:09:17 +00001219 pExpr->iColumn = iCol==pTab->iPKey ? -1 : iCol;
1220 pExpr->affinity = pTab->aCol[iCol].affinity;
drh8b4c40d2007-02-01 23:02:45 +00001221 if( (pExpr->flags & EP_ExpCollate)==0 ){
1222 pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0);
1223 }
danielk1977aee18ef2005-03-09 12:26:50 +00001224 pExpr->pTab = pTab;
danielk19778f2c54e2008-01-01 19:02:09 +00001225 if( iCol>=0 ){
drhc5499be2008-04-01 15:06:33 +00001226 testcase( iCol==31 );
1227 testcase( iCol==32 );
danielk19778f2c54e2008-01-01 19:02:09 +00001228 *piColMask |= ((u32)1<<iCol) | (iCol>=32?0xffffffff:0);
1229 }
drh626a8792005-01-17 22:08:19 +00001230 break;
1231 }
1232 }
1233 }
1234 }
drhb7f91642004-10-31 02:22:47 +00001235#endif /* !defined(SQLITE_OMIT_TRIGGER) */
drh8141f612004-01-25 22:44:58 +00001236
drh626a8792005-01-17 22:08:19 +00001237 /*
1238 ** Perhaps the name is a reference to the ROWID
1239 */
1240 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
1241 cnt = 1;
1242 pExpr->iColumn = -1;
drh8a512562005-11-14 22:29:05 +00001243 pExpr->affinity = SQLITE_AFF_INTEGER;
drh626a8792005-01-17 22:08:19 +00001244 }
drh8141f612004-01-25 22:44:58 +00001245
drh626a8792005-01-17 22:08:19 +00001246 /*
1247 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
1248 ** might refer to an result-set alias. This happens, for example, when
1249 ** we are resolving names in the WHERE clause of the following command:
1250 **
1251 ** SELECT a+b AS x FROM table WHERE x<10;
1252 **
1253 ** In cases like this, replace pExpr with a copy of the expression that
1254 ** forms the result set entry ("a+b" in the example) and return immediately.
1255 ** Note that the expression in the result set should have already been
1256 ** resolved by the time the WHERE clause is resolved.
1257 */
drhffe07b22005-11-03 00:41:17 +00001258 if( cnt==0 && (pEList = pNC->pEList)!=0 && zTab==0 ){
drh626a8792005-01-17 22:08:19 +00001259 for(j=0; j<pEList->nExpr; j++){
1260 char *zAs = pEList->a[j].zName;
1261 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
drh36379e92007-07-23 22:51:15 +00001262 Expr *pDup, *pOrig;
drh626a8792005-01-17 22:08:19 +00001263 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
drh4f07e5f2007-05-14 11:34:46 +00001264 assert( pExpr->pList==0 );
1265 assert( pExpr->pSelect==0 );
drh36379e92007-07-23 22:51:15 +00001266 pOrig = pEList->a[j].pExpr;
1267 if( !pNC->allowAgg && ExprHasProperty(pOrig, EP_Agg) ){
1268 sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs);
drh633e6d52008-07-28 19:34:53 +00001269 sqlite3DbFree(db, zCol);
drh36379e92007-07-23 22:51:15 +00001270 return 2;
1271 }
danielk19771e536952007-08-16 10:09:01 +00001272 pDup = sqlite3ExprDup(db, pOrig);
drh4f07e5f2007-05-14 11:34:46 +00001273 if( pExpr->flags & EP_ExpCollate ){
1274 pDup->pColl = pExpr->pColl;
1275 pDup->flags |= EP_ExpCollate;
1276 }
drh633e6d52008-07-28 19:34:53 +00001277 if( pExpr->span.dyn ) sqlite3DbFree(db, (char*)pExpr->span.z);
1278 if( pExpr->token.dyn ) sqlite3DbFree(db, (char*)pExpr->token.z);
drh4f07e5f2007-05-14 11:34:46 +00001279 memcpy(pExpr, pDup, sizeof(*pExpr));
drh633e6d52008-07-28 19:34:53 +00001280 sqlite3DbFree(db, pDup);
drh15ccce12005-05-23 15:06:39 +00001281 cnt = 1;
danielk1977c9cf6e32007-06-25 16:29:33 +00001282 pMatch = 0;
drh626a8792005-01-17 22:08:19 +00001283 assert( zTab==0 && zDb==0 );
drh15ccce12005-05-23 15:06:39 +00001284 goto lookupname_end_2;
drh626a8792005-01-17 22:08:19 +00001285 }
1286 }
1287 }
1288
1289 /* Advance to the next name context. The loop will exit when either
1290 ** we have a match (cnt>0) or when we run out of name contexts.
1291 */
1292 if( cnt==0 ){
1293 pNC = pNC->pNext;
1294 }
drh8141f612004-01-25 22:44:58 +00001295 }
1296
1297 /*
1298 ** If X and Y are NULL (in other words if only the column name Z is
1299 ** supplied) and the value of Z is enclosed in double-quotes, then
1300 ** Z is a string literal if it doesn't match any column names. In that
1301 ** case, we need to return right away and not make any changes to
1302 ** pExpr.
drh15ccce12005-05-23 15:06:39 +00001303 **
1304 ** Because no reference was made to outer contexts, the pNC->nRef
1305 ** fields are not changed in any context.
drh8141f612004-01-25 22:44:58 +00001306 */
1307 if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
drh633e6d52008-07-28 19:34:53 +00001308 sqlite3DbFree(db, zCol);
drha33cb5f2008-08-07 13:05:34 +00001309 pExpr->op = TK_STRING;
drh8141f612004-01-25 22:44:58 +00001310 return 0;
1311 }
1312
1313 /*
1314 ** cnt==0 means there was not match. cnt>1 means there were two or
1315 ** more matches. Either way, we have an error.
1316 */
1317 if( cnt!=1 ){
drhde4fcfd2008-01-19 23:50:26 +00001318 const char *zErr;
1319 zErr = cnt==0 ? "no such column" : "ambiguous column name";
drh8141f612004-01-25 22:44:58 +00001320 if( zDb ){
drhde4fcfd2008-01-19 23:50:26 +00001321 sqlite3ErrorMsg(pParse, "%s: %s.%s.%s", zErr, zDb, zTab, zCol);
drh8141f612004-01-25 22:44:58 +00001322 }else if( zTab ){
drhde4fcfd2008-01-19 23:50:26 +00001323 sqlite3ErrorMsg(pParse, "%s: %s.%s", zErr, zTab, zCol);
drh8141f612004-01-25 22:44:58 +00001324 }else{
drhde4fcfd2008-01-19 23:50:26 +00001325 sqlite3ErrorMsg(pParse, "%s: %s", zErr, zCol);
drh8141f612004-01-25 22:44:58 +00001326 }
drhde4fcfd2008-01-19 23:50:26 +00001327 pTopNC->nErr++;
drh8141f612004-01-25 22:44:58 +00001328 }
1329
drh51669862004-12-18 18:40:26 +00001330 /* If a column from a table in pSrcList is referenced, then record
1331 ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes
1332 ** bit 0 to be set. Column 1 sets bit 1. And so forth. If the
1333 ** column number is greater than the number of bits in the bitmask
1334 ** then set the high-order bit of the bitmask.
1335 */
1336 if( pExpr->iColumn>=0 && pMatch!=0 ){
1337 int n = pExpr->iColumn;
drhc5499be2008-04-01 15:06:33 +00001338 testcase( n==sizeof(Bitmask)*8-1 );
drh51669862004-12-18 18:40:26 +00001339 if( n>=sizeof(Bitmask)*8 ){
1340 n = sizeof(Bitmask)*8-1;
1341 }
1342 assert( pMatch->iCursor==pExpr->iTable );
drhca83ac52007-02-01 01:40:44 +00001343 pMatch->colUsed |= ((Bitmask)1)<<n;
drh51669862004-12-18 18:40:26 +00001344 }
1345
danielk1977d5d56522005-03-16 12:15:20 +00001346lookupname_end:
drh8141f612004-01-25 22:44:58 +00001347 /* Clean up and return
1348 */
drh633e6d52008-07-28 19:34:53 +00001349 sqlite3DbFree(db, zDb);
1350 sqlite3DbFree(db, zTab);
1351 sqlite3ExprDelete(db, pExpr->pLeft);
drh8141f612004-01-25 22:44:58 +00001352 pExpr->pLeft = 0;
drh633e6d52008-07-28 19:34:53 +00001353 sqlite3ExprDelete(db, pExpr->pRight);
drh8141f612004-01-25 22:44:58 +00001354 pExpr->pRight = 0;
1355 pExpr->op = TK_COLUMN;
drh15ccce12005-05-23 15:06:39 +00001356lookupname_end_2:
drh633e6d52008-07-28 19:34:53 +00001357 sqlite3DbFree(db, zCol);
drh626a8792005-01-17 22:08:19 +00001358 if( cnt==1 ){
danielk1977b3bce662005-01-29 08:32:43 +00001359 assert( pNC!=0 );
drh728b5772007-09-18 15:55:07 +00001360 sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList);
danielk1977aee18ef2005-03-09 12:26:50 +00001361 if( pMatch && !pMatch->pSelect ){
1362 pExpr->pTab = pMatch->pTab;
1363 }
drh15ccce12005-05-23 15:06:39 +00001364 /* Increment the nRef value on all name contexts from TopNC up to
1365 ** the point where the name matched. */
1366 for(;;){
1367 assert( pTopNC!=0 );
1368 pTopNC->nRef++;
1369 if( pTopNC==pNC ) break;
1370 pTopNC = pTopNC->pNext;
1371 }
1372 return 0;
1373 } else {
1374 return 1;
drh626a8792005-01-17 22:08:19 +00001375 }
drh8141f612004-01-25 22:44:58 +00001376}
1377
1378/*
drh626a8792005-01-17 22:08:19 +00001379** This routine is designed as an xFunc for walkExprTree().
1380**
drh73b211a2005-01-18 04:00:42 +00001381** Resolve symbolic names into TK_COLUMN operators for the current
drh626a8792005-01-17 22:08:19 +00001382** node in the expression tree. Return 0 to continue the search down
drh73b211a2005-01-18 04:00:42 +00001383** the tree or 2 to abort the tree walk.
1384**
1385** This routine also does error checking and name resolution for
1386** function names. The operator for aggregate functions is changed
1387** to TK_AGG_FUNCTION.
drh626a8792005-01-17 22:08:19 +00001388*/
1389static int nameResolverStep(void *pArg, Expr *pExpr){
1390 NameContext *pNC = (NameContext*)pArg;
drh626a8792005-01-17 22:08:19 +00001391 Parse *pParse;
drh626a8792005-01-17 22:08:19 +00001392
danielk1977b3bce662005-01-29 08:32:43 +00001393 if( pExpr==0 ) return 1;
drh626a8792005-01-17 22:08:19 +00001394 assert( pNC!=0 );
drh626a8792005-01-17 22:08:19 +00001395 pParse = pNC->pParse;
danielk1977b3bce662005-01-29 08:32:43 +00001396
drh626a8792005-01-17 22:08:19 +00001397 if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return 1;
1398 ExprSetProperty(pExpr, EP_Resolved);
1399#ifndef NDEBUG
danielk1977f0113002006-01-24 12:09:17 +00001400 if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
1401 SrcList *pSrcList = pNC->pSrcList;
danielk1977940fac92005-01-23 22:41:37 +00001402 int i;
danielk1977f0113002006-01-24 12:09:17 +00001403 for(i=0; i<pNC->pSrcList->nSrc; i++){
drh626a8792005-01-17 22:08:19 +00001404 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
1405 }
1406 }
1407#endif
1408 switch( pExpr->op ){
drh626a8792005-01-17 22:08:19 +00001409 /* A lone identifier is the name of a column.
1410 */
1411 case TK_ID: {
drh626a8792005-01-17 22:08:19 +00001412 lookupName(pParse, 0, 0, &pExpr->token, pNC, pExpr);
1413 return 1;
1414 }
1415
1416 /* A table name and column name: ID.ID
1417 ** Or a database, table and column: ID.ID.ID
1418 */
1419 case TK_DOT: {
1420 Token *pColumn;
1421 Token *pTable;
1422 Token *pDb;
1423 Expr *pRight;
1424
danielk1977b3bce662005-01-29 08:32:43 +00001425 /* if( pSrcList==0 ) break; */
drh626a8792005-01-17 22:08:19 +00001426 pRight = pExpr->pRight;
1427 if( pRight->op==TK_ID ){
1428 pDb = 0;
1429 pTable = &pExpr->pLeft->token;
1430 pColumn = &pRight->token;
1431 }else{
1432 assert( pRight->op==TK_DOT );
1433 pDb = &pExpr->pLeft->token;
1434 pTable = &pRight->pLeft->token;
1435 pColumn = &pRight->pRight->token;
1436 }
1437 lookupName(pParse, pDb, pTable, pColumn, pNC, pExpr);
1438 return 1;
1439 }
1440
1441 /* Resolve function names
1442 */
drhb71090f2005-05-23 17:26:51 +00001443 case TK_CONST_FUNC:
drh626a8792005-01-17 22:08:19 +00001444 case TK_FUNCTION: {
1445 ExprList *pList = pExpr->pList; /* The argument list */
1446 int n = pList ? pList->nExpr : 0; /* Number of arguments */
1447 int no_such_func = 0; /* True if no such function exists */
1448 int wrong_num_args = 0; /* True if wrong number of arguments */
1449 int is_agg = 0; /* True if is an aggregate function */
1450 int i;
drh5169bbc2006-08-24 14:59:45 +00001451 int auth; /* Authorization to use the function */
drh626a8792005-01-17 22:08:19 +00001452 int nId; /* Number of characters in function name */
1453 const char *zId; /* The function name. */
drh73b211a2005-01-18 04:00:42 +00001454 FuncDef *pDef; /* Information about the function */
danielk197714db2662006-01-09 16:12:04 +00001455 int enc = ENC(pParse->db); /* The database encoding */
drh626a8792005-01-17 22:08:19 +00001456
drh2646da72005-12-09 20:02:05 +00001457 zId = (char*)pExpr->token.z;
drhb71090f2005-05-23 17:26:51 +00001458 nId = pExpr->token.n;
drh626a8792005-01-17 22:08:19 +00001459 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
1460 if( pDef==0 ){
1461 pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
1462 if( pDef==0 ){
1463 no_such_func = 1;
1464 }else{
1465 wrong_num_args = 1;
1466 }
1467 }else{
1468 is_agg = pDef->xFunc==0;
1469 }
drh2fca7fe2006-11-23 11:59:13 +00001470#ifndef SQLITE_OMIT_AUTHORIZATION
drh5169bbc2006-08-24 14:59:45 +00001471 if( pDef ){
1472 auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0);
1473 if( auth!=SQLITE_OK ){
1474 if( auth==SQLITE_DENY ){
1475 sqlite3ErrorMsg(pParse, "not authorized to use function: %s",
1476 pDef->zName);
1477 pNC->nErr++;
1478 }
1479 pExpr->op = TK_NULL;
1480 return 1;
1481 }
1482 }
drhb8b14212006-08-24 15:18:25 +00001483#endif
drh626a8792005-01-17 22:08:19 +00001484 if( is_agg && !pNC->allowAgg ){
1485 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
1486 pNC->nErr++;
1487 is_agg = 0;
1488 }else if( no_such_func ){
1489 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
1490 pNC->nErr++;
1491 }else if( wrong_num_args ){
1492 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
1493 nId, zId);
1494 pNC->nErr++;
1495 }
1496 if( is_agg ){
1497 pExpr->op = TK_AGG_FUNCTION;
1498 pNC->hasAgg = 1;
1499 }
drh73b211a2005-01-18 04:00:42 +00001500 if( is_agg ) pNC->allowAgg = 0;
drh626a8792005-01-17 22:08:19 +00001501 for(i=0; pNC->nErr==0 && i<n; i++){
drh73b211a2005-01-18 04:00:42 +00001502 walkExprTree(pList->a[i].pExpr, nameResolverStep, pNC);
drh626a8792005-01-17 22:08:19 +00001503 }
drh73b211a2005-01-18 04:00:42 +00001504 if( is_agg ) pNC->allowAgg = 1;
drh626a8792005-01-17 22:08:19 +00001505 /* FIX ME: Compute pExpr->affinity based on the expected return
1506 ** type of the function
1507 */
1508 return is_agg;
1509 }
danielk1977b3bce662005-01-29 08:32:43 +00001510#ifndef SQLITE_OMIT_SUBQUERY
1511 case TK_SELECT:
1512 case TK_EXISTS:
1513#endif
1514 case TK_IN: {
1515 if( pExpr->pSelect ){
drh8a9f38f2005-11-05 15:07:55 +00001516 int nRef = pNC->nRef;
drh06f65412005-11-03 02:03:13 +00001517#ifndef SQLITE_OMIT_CHECK
1518 if( pNC->isCheck ){
1519 sqlite3ErrorMsg(pParse,"subqueries prohibited in CHECK constraints");
1520 }
1521#endif
danielk1977b3bce662005-01-29 08:32:43 +00001522 sqlite3SelectResolve(pParse, pExpr->pSelect, pNC);
1523 assert( pNC->nRef>=nRef );
1524 if( nRef!=pNC->nRef ){
1525 ExprSetProperty(pExpr, EP_VarSelect);
1526 }
1527 }
drh4284fb02005-11-03 12:33:28 +00001528 break;
danielk1977b3bce662005-01-29 08:32:43 +00001529 }
drh4284fb02005-11-03 12:33:28 +00001530#ifndef SQLITE_OMIT_CHECK
1531 case TK_VARIABLE: {
1532 if( pNC->isCheck ){
1533 sqlite3ErrorMsg(pParse,"parameters prohibited in CHECK constraints");
1534 }
1535 break;
1536 }
1537#endif
drh626a8792005-01-17 22:08:19 +00001538 }
1539 return 0;
1540}
1541
1542/*
drhcce7d172000-05-31 15:34:51 +00001543** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +00001544** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +00001545** index to the table in the table list and a column offset. The
1546** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
1547** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +00001548** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +00001549** VDBE cursor number for a cursor that is pointing into the referenced
1550** table. The Expr.iColumn value is changed to the index of the column
1551** of the referenced table. The Expr.iColumn value for the special
1552** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
1553** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +00001554**
drh626a8792005-01-17 22:08:19 +00001555** Also resolve function names and check the functions for proper
1556** usage. Make sure all function names are recognized and all functions
1557** have the correct number of arguments. Leave an error message
1558** in pParse->zErrMsg if anything is amiss. Return the number of errors.
1559**
drh73b211a2005-01-18 04:00:42 +00001560** If the expression contains aggregate functions then set the EP_Agg
1561** property on the expression.
drh626a8792005-01-17 22:08:19 +00001562*/
danielk1977fc976062007-05-10 10:46:56 +00001563int sqlite3ExprResolveNames(
danielk1977b3bce662005-01-29 08:32:43 +00001564 NameContext *pNC, /* Namespace to resolve expressions in. */
1565 Expr *pExpr /* The expression to be analyzed. */
drh626a8792005-01-17 22:08:19 +00001566){
drh13449892005-09-07 21:22:45 +00001567 int savedHasAgg;
drhbb4957f2008-03-20 14:03:29 +00001568
drh73b211a2005-01-18 04:00:42 +00001569 if( pExpr==0 ) return 0;
drhbb4957f2008-03-20 14:03:29 +00001570#if SQLITE_MAX_EXPR_DEPTH>0
1571 {
danielk19774b5255a2008-06-05 16:47:39 +00001572 if( checkExprHeight(pNC->pParse, pExpr->nHeight + pNC->pParse->nHeight) ){
drhbb4957f2008-03-20 14:03:29 +00001573 return 1;
1574 }
1575 pNC->pParse->nHeight += pExpr->nHeight;
danielk1977fc976062007-05-10 10:46:56 +00001576 }
danielk1977fc976062007-05-10 10:46:56 +00001577#endif
drh13449892005-09-07 21:22:45 +00001578 savedHasAgg = pNC->hasAgg;
1579 pNC->hasAgg = 0;
danielk1977b3bce662005-01-29 08:32:43 +00001580 walkExprTree(pExpr, nameResolverStep, pNC);
drhbb4957f2008-03-20 14:03:29 +00001581#if SQLITE_MAX_EXPR_DEPTH>0
danielk1977fc976062007-05-10 10:46:56 +00001582 pNC->pParse->nHeight -= pExpr->nHeight;
1583#endif
danielk1977b3bce662005-01-29 08:32:43 +00001584 if( pNC->nErr>0 ){
drh73b211a2005-01-18 04:00:42 +00001585 ExprSetProperty(pExpr, EP_Error);
drh73b211a2005-01-18 04:00:42 +00001586 }
drh13449892005-09-07 21:22:45 +00001587 if( pNC->hasAgg ){
1588 ExprSetProperty(pExpr, EP_Agg);
1589 }else if( savedHasAgg ){
1590 pNC->hasAgg = 1;
1591 }
drh73b211a2005-01-18 04:00:42 +00001592 return ExprHasProperty(pExpr, EP_Error);
drh626a8792005-01-17 22:08:19 +00001593}
1594
drh1398ad32005-01-19 23:24:50 +00001595/*
1596** A pointer instance of this structure is used to pass information
1597** through walkExprTree into codeSubqueryStep().
1598*/
1599typedef struct QueryCoder QueryCoder;
1600struct QueryCoder {
1601 Parse *pParse; /* The parsing context */
1602 NameContext *pNC; /* Namespace of first enclosing query */
1603};
1604
danielk19779a96b662007-11-29 17:05:18 +00001605#ifdef SQLITE_TEST
1606 int sqlite3_enable_in_opt = 1;
1607#else
1608 #define sqlite3_enable_in_opt 1
1609#endif
1610
1611/*
drhb287f4b2008-04-25 00:08:38 +00001612** Return true if the IN operator optimization is enabled and
1613** the SELECT statement p exists and is of the
1614** simple form:
1615**
1616** SELECT <column> FROM <table>
1617**
1618** If this is the case, it may be possible to use an existing table
1619** or index instead of generating an epheremal table.
1620*/
1621#ifndef SQLITE_OMIT_SUBQUERY
1622static int isCandidateForInOpt(Select *p){
1623 SrcList *pSrc;
1624 ExprList *pEList;
1625 Table *pTab;
1626 if( !sqlite3_enable_in_opt ) return 0; /* IN optimization must be enabled */
1627 if( p==0 ) return 0; /* right-hand side of IN is SELECT */
1628 if( p->pPrior ) return 0; /* Not a compound SELECT */
1629 if( p->isDistinct ) return 0; /* No DISTINCT keyword */
1630 if( p->isAgg ) return 0; /* Contains no aggregate functions */
1631 if( p->pGroupBy ) return 0; /* Has no GROUP BY clause */
1632 if( p->pLimit ) return 0; /* Has no LIMIT clause */
1633 if( p->pOffset ) return 0;
1634 if( p->pWhere ) return 0; /* Has no WHERE clause */
1635 pSrc = p->pSrc;
1636 if( pSrc==0 ) return 0; /* A single table in the FROM clause */
1637 if( pSrc->nSrc!=1 ) return 0;
1638 if( pSrc->a[0].pSelect ) return 0; /* FROM clause is not a subquery */
1639 pTab = pSrc->a[0].pTab;
1640 if( pTab==0 ) return 0;
1641 if( pTab->pSelect ) return 0; /* FROM clause is not a view */
1642 if( IsVirtual(pTab) ) return 0; /* FROM clause not a virtual table */
1643 pEList = p->pEList;
1644 if( pEList->nExpr!=1 ) return 0; /* One column in the result set */
1645 if( pEList->a[0].pExpr->op!=TK_COLUMN ) return 0; /* Result is a column */
1646 return 1;
1647}
1648#endif /* SQLITE_OMIT_SUBQUERY */
1649
1650/*
danielk19779a96b662007-11-29 17:05:18 +00001651** This function is used by the implementation of the IN (...) operator.
1652** It's job is to find or create a b-tree structure that may be used
1653** either to test for membership of the (...) set or to iterate through
drh85b623f2007-12-13 21:54:09 +00001654** its members, skipping duplicates.
danielk19779a96b662007-11-29 17:05:18 +00001655**
1656** The cursor opened on the structure (database table, database index
1657** or ephermal table) is stored in pX->iTable before this function returns.
1658** The returned value indicates the structure type, as follows:
1659**
1660** IN_INDEX_ROWID - The cursor was opened on a database table.
drh2d401ab2008-01-10 23:50:11 +00001661** IN_INDEX_INDEX - The cursor was opened on a database index.
danielk19779a96b662007-11-29 17:05:18 +00001662** IN_INDEX_EPH - The cursor was opened on a specially created and
1663** populated epheremal table.
1664**
1665** An existing structure may only be used if the SELECT is of the simple
1666** form:
1667**
1668** SELECT <column> FROM <table>
1669**
danielk19770cdc0222008-06-26 18:04:03 +00001670** If prNotFound parameter is 0, then the structure will be used to iterate
danielk19779a96b662007-11-29 17:05:18 +00001671** through the set members, skipping any duplicates. In this case an
1672** epheremal table must be used unless the selected <column> is guaranteed
1673** to be unique - either because it is an INTEGER PRIMARY KEY or it
1674** is unique by virtue of a constraint or implicit index.
danielk19770cdc0222008-06-26 18:04:03 +00001675**
1676** If the prNotFound parameter is not 0, then the structure will be used
1677** for fast set membership tests. In this case an epheremal table must
1678** be used unless <column> is an INTEGER PRIMARY KEY or an index can
1679** be found with <column> as its left-most column.
1680**
1681** When the structure is being used for set membership tests, the user
1682** needs to know whether or not the structure contains an SQL NULL
1683** value in order to correctly evaluate expressions like "X IN (Y, Z)".
1684** If there is a chance that the structure may contain a NULL value at
1685** runtime, then a register is allocated and the register number written
1686** to *prNotFound. If there is no chance that the structure contains a
1687** NULL value, then *prNotFound is left unchanged.
1688**
1689** If a register is allocated and its location stored in *prNotFound, then
1690** its initial value is NULL. If the structure does not remain constant
1691** for the duration of the query (i.e. the set is a correlated sub-select),
1692** the value of the allocated register is reset to NULL each time the
1693** structure is repopulated. This allows the caller to use vdbe code
1694** equivalent to the following:
1695**
1696** if( register==NULL ){
1697** has_null = <test if data structure contains null>
1698** register = 1
1699** }
1700**
1701** in order to avoid running the <test if data structure contains null>
1702** test more often than is necessary.
danielk19779a96b662007-11-29 17:05:18 +00001703*/
danielk1977284f4ac2007-12-10 05:03:46 +00001704#ifndef SQLITE_OMIT_SUBQUERY
danielk19770cdc0222008-06-26 18:04:03 +00001705int sqlite3FindInIndex(Parse *pParse, Expr *pX, int *prNotFound){
danielk19779a96b662007-11-29 17:05:18 +00001706 Select *p;
1707 int eType = 0;
1708 int iTab = pParse->nTab++;
danielk19770cdc0222008-06-26 18:04:03 +00001709 int mustBeUnique = !prNotFound;
danielk19779a96b662007-11-29 17:05:18 +00001710
1711 /* The follwing if(...) expression is true if the SELECT is of the
1712 ** simple form:
1713 **
1714 ** SELECT <column> FROM <table>
1715 **
1716 ** If this is the case, it may be possible to use an existing table
1717 ** or index instead of generating an epheremal table.
1718 */
drhb287f4b2008-04-25 00:08:38 +00001719 p = pX->pSelect;
1720 if( isCandidateForInOpt(p) ){
danielk19779a96b662007-11-29 17:05:18 +00001721 sqlite3 *db = pParse->db;
1722 Index *pIdx;
1723 Expr *pExpr = p->pEList->a[0].pExpr;
1724 int iCol = pExpr->iColumn;
1725 Vdbe *v = sqlite3GetVdbe(pParse);
1726
1727 /* This function is only called from two places. In both cases the vdbe
1728 ** has already been allocated. So assume sqlite3GetVdbe() is always
1729 ** successful here.
1730 */
1731 assert(v);
1732 if( iCol<0 ){
drh0a07c102008-01-03 18:03:08 +00001733 int iMem = ++pParse->nMem;
danielk19779a96b662007-11-29 17:05:18 +00001734 int iAddr;
1735 Table *pTab = p->pSrc->a[0].pTab;
1736 int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
1737 sqlite3VdbeUsesBtree(v, iDb);
1738
drh892d3172008-01-10 03:46:36 +00001739 iAddr = sqlite3VdbeAddOp1(v, OP_If, iMem);
drh4c583122008-01-04 22:01:03 +00001740 sqlite3VdbeAddOp2(v, OP_Integer, 1, iMem);
danielk19779a96b662007-11-29 17:05:18 +00001741
1742 sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
1743 eType = IN_INDEX_ROWID;
1744
1745 sqlite3VdbeJumpHere(v, iAddr);
1746 }else{
1747 /* The collation sequence used by the comparison. If an index is to
1748 ** be used in place of a temp-table, it must be ordered according
1749 ** to this collation sequence.
1750 */
1751 CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pExpr);
1752
1753 /* Check that the affinity that will be used to perform the
1754 ** comparison is the same as the affinity of the column. If
1755 ** it is not, it is not possible to use any index.
1756 */
1757 Table *pTab = p->pSrc->a[0].pTab;
1758 char aff = comparisonAffinity(pX);
1759 int affinity_ok = (pTab->aCol[iCol].affinity==aff||aff==SQLITE_AFF_NONE);
1760
1761 for(pIdx=pTab->pIndex; pIdx && eType==0 && affinity_ok; pIdx=pIdx->pNext){
1762 if( (pIdx->aiColumn[0]==iCol)
1763 && (pReq==sqlite3FindCollSeq(db, ENC(db), pIdx->azColl[0], -1, 0))
1764 && (!mustBeUnique || (pIdx->nColumn==1 && pIdx->onError!=OE_None))
1765 ){
1766 int iDb;
drh0a07c102008-01-03 18:03:08 +00001767 int iMem = ++pParse->nMem;
danielk19779a96b662007-11-29 17:05:18 +00001768 int iAddr;
1769 char *pKey;
1770
1771 pKey = (char *)sqlite3IndexKeyinfo(pParse, pIdx);
1772 iDb = sqlite3SchemaToIndex(db, pIdx->pSchema);
1773 sqlite3VdbeUsesBtree(v, iDb);
1774
drh892d3172008-01-10 03:46:36 +00001775 iAddr = sqlite3VdbeAddOp1(v, OP_If, iMem);
drh4c583122008-01-04 22:01:03 +00001776 sqlite3VdbeAddOp2(v, OP_Integer, 1, iMem);
danielk19779a96b662007-11-29 17:05:18 +00001777
danielk1977cd3e8f72008-03-25 09:47:35 +00001778 sqlite3VdbeAddOp2(v, OP_SetNumColumns, 0, pIdx->nColumn);
danielk1977207872a2008-01-03 07:54:23 +00001779 sqlite3VdbeAddOp4(v, OP_OpenRead, iTab, pIdx->tnum, iDb,
drh66a51672008-01-03 00:01:23 +00001780 pKey,P4_KEYINFO_HANDOFF);
danielk1977207872a2008-01-03 07:54:23 +00001781 VdbeComment((v, "%s", pIdx->zName));
danielk19779a96b662007-11-29 17:05:18 +00001782 eType = IN_INDEX_INDEX;
danielk19779a96b662007-11-29 17:05:18 +00001783
1784 sqlite3VdbeJumpHere(v, iAddr);
danielk19770cdc0222008-06-26 18:04:03 +00001785 if( prNotFound && !pTab->aCol[iCol].notNull ){
1786 *prNotFound = ++pParse->nMem;
1787 }
danielk19779a96b662007-11-29 17:05:18 +00001788 }
1789 }
1790 }
1791 }
1792
1793 if( eType==0 ){
danielk19770cdc0222008-06-26 18:04:03 +00001794 int rMayHaveNull = 0;
1795 if( prNotFound ){
1796 *prNotFound = rMayHaveNull = ++pParse->nMem;
1797 }
1798 sqlite3CodeSubselect(pParse, pX, rMayHaveNull);
danielk19779a96b662007-11-29 17:05:18 +00001799 eType = IN_INDEX_EPH;
1800 }else{
1801 pX->iTable = iTab;
1802 }
1803 return eType;
1804}
danielk1977284f4ac2007-12-10 05:03:46 +00001805#endif
drh626a8792005-01-17 22:08:19 +00001806
1807/*
drh9cbe6352005-11-29 03:13:21 +00001808** Generate code for scalar subqueries used as an expression
1809** and IN operators. Examples:
drh626a8792005-01-17 22:08:19 +00001810**
drh9cbe6352005-11-29 03:13:21 +00001811** (SELECT a FROM b) -- subquery
1812** EXISTS (SELECT a FROM b) -- EXISTS subquery
1813** x IN (4,5,11) -- IN operator with list on right-hand side
1814** x IN (SELECT a FROM b) -- IN operator with subquery on the right
drhfef52082000-06-06 01:50:43 +00001815**
drh9cbe6352005-11-29 03:13:21 +00001816** The pExpr parameter describes the expression that contains the IN
1817** operator or subquery.
drhcce7d172000-05-31 15:34:51 +00001818*/
drh51522cd2005-01-20 13:36:19 +00001819#ifndef SQLITE_OMIT_SUBQUERY
danielk19770cdc0222008-06-26 18:04:03 +00001820void sqlite3CodeSubselect(Parse *pParse, Expr *pExpr, int rMayHaveNull){
drh57dbd7b2005-07-08 18:25:26 +00001821 int testAddr = 0; /* One-time test address */
danielk1977b3bce662005-01-29 08:32:43 +00001822 Vdbe *v = sqlite3GetVdbe(pParse);
1823 if( v==0 ) return;
1824
danielk1977fc976062007-05-10 10:46:56 +00001825
drh57dbd7b2005-07-08 18:25:26 +00001826 /* This code must be run in its entirety every time it is encountered
1827 ** if any of the following is true:
1828 **
1829 ** * The right-hand side is a correlated subquery
1830 ** * The right-hand side is an expression list containing variables
1831 ** * We are inside a trigger
1832 **
1833 ** If all of the above are false, then we can run this code just once
1834 ** save the results, and reuse the same result on subsequent invocations.
danielk1977b3bce662005-01-29 08:32:43 +00001835 */
1836 if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->trigStack ){
drh0a07c102008-01-03 18:03:08 +00001837 int mem = ++pParse->nMem;
drh892d3172008-01-10 03:46:36 +00001838 sqlite3VdbeAddOp1(v, OP_If, mem);
1839 testAddr = sqlite3VdbeAddOp2(v, OP_Integer, 1, mem);
drh17435752007-08-16 04:30:38 +00001840 assert( testAddr>0 || pParse->db->mallocFailed );
danielk1977b3bce662005-01-29 08:32:43 +00001841 }
1842
drhcce7d172000-05-31 15:34:51 +00001843 switch( pExpr->op ){
drhfef52082000-06-06 01:50:43 +00001844 case TK_IN: {
danielk1977e014a832004-05-17 10:48:57 +00001845 char affinity;
drhd3d39e92004-05-20 22:16:29 +00001846 KeyInfo keyInfo;
drhb9bb7c12006-06-11 23:41:55 +00001847 int addr; /* Address of OP_OpenEphemeral instruction */
drhd3d39e92004-05-20 22:16:29 +00001848
danielk19770cdc0222008-06-26 18:04:03 +00001849 if( rMayHaveNull ){
1850 sqlite3VdbeAddOp2(v, OP_Null, 0, rMayHaveNull);
1851 }
1852
danielk1977bf3b7212004-05-18 10:06:24 +00001853 affinity = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +00001854
1855 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
drh57dbd7b2005-07-08 18:25:26 +00001856 ** expression it is handled the same way. A virtual table is
danielk1977e014a832004-05-17 10:48:57 +00001857 ** filled with single-field index keys representing the results
1858 ** from the SELECT or the <exprlist>.
1859 **
1860 ** If the 'x' expression is a column value, or the SELECT...
1861 ** statement returns a column value, then the affinity of that
1862 ** column is used to build the index keys. If both 'x' and the
1863 ** SELECT... statement are columns, then numeric affinity is used
1864 ** if either column has NUMERIC or INTEGER affinity. If neither
1865 ** 'x' nor the SELECT... statement are columns, then numeric affinity
1866 ** is used.
1867 */
1868 pExpr->iTable = pParse->nTab++;
danielk1977cd3e8f72008-03-25 09:47:35 +00001869 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pExpr->iTable, 1);
drhd3d39e92004-05-20 22:16:29 +00001870 memset(&keyInfo, 0, sizeof(keyInfo));
1871 keyInfo.nField = 1;
danielk1977e014a832004-05-17 10:48:57 +00001872
drhfef52082000-06-06 01:50:43 +00001873 if( pExpr->pSelect ){
1874 /* Case 1: expr IN (SELECT ...)
1875 **
danielk1977e014a832004-05-17 10:48:57 +00001876 ** Generate code to write the results of the select into the temporary
1877 ** table allocated and opened above.
drhfef52082000-06-06 01:50:43 +00001878 */
drh1013c932008-01-06 00:25:21 +00001879 SelectDest dest;
drhbe5c89a2004-07-26 00:31:09 +00001880 ExprList *pEList;
drh1013c932008-01-06 00:25:21 +00001881
1882 sqlite3SelectDestInit(&dest, SRT_Set, pExpr->iTable);
1883 dest.affinity = (int)affinity;
danielk1977e014a832004-05-17 10:48:57 +00001884 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
drha9671a22008-07-08 23:40:20 +00001885 if( sqlite3Select(pParse, pExpr->pSelect, &dest, 0, 0, 0) ){
drh94ccde52007-04-13 16:06:32 +00001886 return;
1887 }
drhbe5c89a2004-07-26 00:31:09 +00001888 pEList = pExpr->pSelect->pEList;
1889 if( pEList && pEList->nExpr>0 ){
danielk1977bcbb04e2007-05-29 12:11:29 +00001890 keyInfo.aColl[0] = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft,
drhbe5c89a2004-07-26 00:31:09 +00001891 pEList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001892 }
drhfef52082000-06-06 01:50:43 +00001893 }else if( pExpr->pList ){
1894 /* Case 2: expr IN (exprlist)
1895 **
drhfd131da2007-08-07 17:13:03 +00001896 ** For each expression, build an index key from the evaluation and
danielk1977e014a832004-05-17 10:48:57 +00001897 ** store it in the temporary table. If <expr> is a column, then use
1898 ** that columns affinity when building index keys. If <expr> is not
1899 ** a column, use numeric affinity.
drhfef52082000-06-06 01:50:43 +00001900 */
danielk1977e014a832004-05-17 10:48:57 +00001901 int i;
drh57dbd7b2005-07-08 18:25:26 +00001902 ExprList *pList = pExpr->pList;
1903 struct ExprList_item *pItem;
drhecc31802008-06-26 20:06:06 +00001904 int r1, r2, r3;
drh57dbd7b2005-07-08 18:25:26 +00001905
danielk1977e014a832004-05-17 10:48:57 +00001906 if( !affinity ){
drh8159a352006-05-23 23:22:29 +00001907 affinity = SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +00001908 }
danielk19770202b292004-06-09 09:55:16 +00001909 keyInfo.aColl[0] = pExpr->pLeft->pColl;
danielk1977e014a832004-05-17 10:48:57 +00001910
1911 /* Loop through each expression in <exprlist>. */
drh2d401ab2008-01-10 23:50:11 +00001912 r1 = sqlite3GetTempReg(pParse);
1913 r2 = sqlite3GetTempReg(pParse);
drh57dbd7b2005-07-08 18:25:26 +00001914 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
1915 Expr *pE2 = pItem->pExpr;
danielk1977e014a832004-05-17 10:48:57 +00001916
drh57dbd7b2005-07-08 18:25:26 +00001917 /* If the expression is not constant then we will need to
1918 ** disable the test that was generated above that makes sure
1919 ** this code only executes once. Because for a non-constant
1920 ** expression we need to rerun this code each time.
1921 */
drh892d3172008-01-10 03:46:36 +00001922 if( testAddr && !sqlite3ExprIsConstant(pE2) ){
1923 sqlite3VdbeChangeToNoop(v, testAddr-1, 2);
drh57dbd7b2005-07-08 18:25:26 +00001924 testAddr = 0;
drh4794b982000-06-06 13:54:14 +00001925 }
danielk1977e014a832004-05-17 10:48:57 +00001926
1927 /* Evaluate the expression and insert it into the temp table */
drhe55cbd72008-03-31 23:48:03 +00001928 pParse->disableColCache++;
drhecc31802008-06-26 20:06:06 +00001929 r3 = sqlite3ExprCodeTarget(pParse, pE2, r1);
drhc5499be2008-04-01 15:06:33 +00001930 assert( pParse->disableColCache>0 );
drhe55cbd72008-03-31 23:48:03 +00001931 pParse->disableColCache--;
drhecc31802008-06-26 20:06:06 +00001932 sqlite3VdbeAddOp4(v, OP_MakeRecord, r3, 1, r2, &affinity, 1);
drh3c31fc22008-06-26 21:45:26 +00001933 sqlite3ExprCacheAffinityChange(pParse, r3, 1);
drh2d401ab2008-01-10 23:50:11 +00001934 sqlite3VdbeAddOp2(v, OP_IdxInsert, pExpr->iTable, r2);
drhfef52082000-06-06 01:50:43 +00001935 }
drh2d401ab2008-01-10 23:50:11 +00001936 sqlite3ReleaseTempReg(pParse, r1);
1937 sqlite3ReleaseTempReg(pParse, r2);
drhfef52082000-06-06 01:50:43 +00001938 }
drh66a51672008-01-03 00:01:23 +00001939 sqlite3VdbeChangeP4(v, addr, (void *)&keyInfo, P4_KEYINFO);
danielk1977b3bce662005-01-29 08:32:43 +00001940 break;
drhfef52082000-06-06 01:50:43 +00001941 }
1942
drh51522cd2005-01-20 13:36:19 +00001943 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001944 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +00001945 /* This has to be a scalar SELECT. Generate code to put the
1946 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +00001947 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +00001948 */
drh2646da72005-12-09 20:02:05 +00001949 static const Token one = { (u8*)"1", 0, 1 };
drh51522cd2005-01-20 13:36:19 +00001950 Select *pSel;
danielk19776c8c8ce2008-01-02 16:27:09 +00001951 SelectDest dest;
drh1398ad32005-01-19 23:24:50 +00001952
drh51522cd2005-01-20 13:36:19 +00001953 pSel = pExpr->pSelect;
drh1013c932008-01-06 00:25:21 +00001954 sqlite3SelectDestInit(&dest, 0, ++pParse->nMem);
drh51522cd2005-01-20 13:36:19 +00001955 if( pExpr->op==TK_SELECT ){
danielk19776c8c8ce2008-01-02 16:27:09 +00001956 dest.eDest = SRT_Mem;
drh4c583122008-01-04 22:01:03 +00001957 sqlite3VdbeAddOp2(v, OP_Null, 0, dest.iParm);
drhd4e70eb2008-01-02 00:34:36 +00001958 VdbeComment((v, "Init subquery result"));
drh51522cd2005-01-20 13:36:19 +00001959 }else{
danielk19776c8c8ce2008-01-02 16:27:09 +00001960 dest.eDest = SRT_Exists;
drh4c583122008-01-04 22:01:03 +00001961 sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iParm);
drhd4e70eb2008-01-02 00:34:36 +00001962 VdbeComment((v, "Init EXISTS result"));
drh51522cd2005-01-20 13:36:19 +00001963 }
drh633e6d52008-07-28 19:34:53 +00001964 sqlite3ExprDelete(pParse->db, pSel->pLimit);
danielk1977a1644fd2007-08-29 12:31:25 +00001965 pSel->pLimit = sqlite3PExpr(pParse, TK_INTEGER, 0, 0, &one);
drha9671a22008-07-08 23:40:20 +00001966 if( sqlite3Select(pParse, pSel, &dest, 0, 0, 0) ){
drh94ccde52007-04-13 16:06:32 +00001967 return;
1968 }
danielk19776c8c8ce2008-01-02 16:27:09 +00001969 pExpr->iColumn = dest.iParm;
danielk1977b3bce662005-01-29 08:32:43 +00001970 break;
drhcce7d172000-05-31 15:34:51 +00001971 }
1972 }
danielk1977b3bce662005-01-29 08:32:43 +00001973
drh57dbd7b2005-07-08 18:25:26 +00001974 if( testAddr ){
drh892d3172008-01-10 03:46:36 +00001975 sqlite3VdbeJumpHere(v, testAddr-1);
danielk1977b3bce662005-01-29 08:32:43 +00001976 }
danielk1977fc976062007-05-10 10:46:56 +00001977
danielk1977b3bce662005-01-29 08:32:43 +00001978 return;
drhcce7d172000-05-31 15:34:51 +00001979}
drh51522cd2005-01-20 13:36:19 +00001980#endif /* SQLITE_OMIT_SUBQUERY */
drhcce7d172000-05-31 15:34:51 +00001981
drhcce7d172000-05-31 15:34:51 +00001982/*
drh598f1342007-10-23 15:39:45 +00001983** Duplicate an 8-byte value
1984*/
1985static char *dup8bytes(Vdbe *v, const char *in){
1986 char *out = sqlite3DbMallocRaw(sqlite3VdbeDb(v), 8);
1987 if( out ){
1988 memcpy(out, in, 8);
1989 }
1990 return out;
1991}
1992
1993/*
1994** Generate an instruction that will put the floating point
drh9cbf3422008-01-17 16:22:13 +00001995** value described by z[0..n-1] into register iMem.
drh0cf19ed2007-10-23 18:55:48 +00001996**
1997** The z[] string will probably not be zero-terminated. But the
1998** z[n] character is guaranteed to be something that does not look
1999** like the continuation of the number.
drh598f1342007-10-23 15:39:45 +00002000*/
drh9de221d2008-01-05 06:51:30 +00002001static void codeReal(Vdbe *v, const char *z, int n, int negateFlag, int iMem){
drh598f1342007-10-23 15:39:45 +00002002 assert( z || v==0 || sqlite3VdbeDb(v)->mallocFailed );
2003 if( z ){
2004 double value;
2005 char *zV;
drh0cf19ed2007-10-23 18:55:48 +00002006 assert( !isdigit(z[n]) );
drh598f1342007-10-23 15:39:45 +00002007 sqlite3AtoF(z, &value);
drh2eaf93d2008-04-29 00:15:20 +00002008 if( sqlite3IsNaN(value) ){
2009 sqlite3VdbeAddOp2(v, OP_Null, 0, iMem);
2010 }else{
2011 if( negateFlag ) value = -value;
2012 zV = dup8bytes(v, (char*)&value);
2013 sqlite3VdbeAddOp4(v, OP_Real, 0, iMem, 0, zV, P4_REAL);
2014 }
drh598f1342007-10-23 15:39:45 +00002015 }
2016}
2017
2018
2019/*
drhfec19aa2004-05-19 20:41:03 +00002020** Generate an instruction that will put the integer describe by
drh9cbf3422008-01-17 16:22:13 +00002021** text z[0..n-1] into register iMem.
drh0cf19ed2007-10-23 18:55:48 +00002022**
2023** The z[] string will probably not be zero-terminated. But the
2024** z[n] character is guaranteed to be something that does not look
2025** like the continuation of the number.
drhfec19aa2004-05-19 20:41:03 +00002026*/
drh92b01d52008-06-24 00:32:35 +00002027static void codeInteger(Vdbe *v, Expr *pExpr, int negFlag, int iMem){
2028 const char *z;
2029 if( pExpr->flags & EP_IntValue ){
2030 int i = pExpr->iTable;
2031 if( negFlag ) i = -i;
2032 sqlite3VdbeAddOp2(v, OP_Integer, i, iMem);
2033 }else if( (z = (char*)pExpr->token.z)!=0 ){
danielk1977c9cf9012007-05-30 10:36:47 +00002034 int i;
drh92b01d52008-06-24 00:32:35 +00002035 int n = pExpr->token.n;
drh0cf19ed2007-10-23 18:55:48 +00002036 assert( !isdigit(z[n]) );
danielk1977c9cf9012007-05-30 10:36:47 +00002037 if( sqlite3GetInt32(z, &i) ){
drh9de221d2008-01-05 06:51:30 +00002038 if( negFlag ) i = -i;
2039 sqlite3VdbeAddOp2(v, OP_Integer, i, iMem);
2040 }else if( sqlite3FitsIn64Bits(z, negFlag) ){
drh598f1342007-10-23 15:39:45 +00002041 i64 value;
2042 char *zV;
2043 sqlite3Atoi64(z, &value);
drh9de221d2008-01-05 06:51:30 +00002044 if( negFlag ) value = -value;
drh598f1342007-10-23 15:39:45 +00002045 zV = dup8bytes(v, (char*)&value);
drh9de221d2008-01-05 06:51:30 +00002046 sqlite3VdbeAddOp4(v, OP_Int64, 0, iMem, 0, zV, P4_INT64);
danielk1977c9cf9012007-05-30 10:36:47 +00002047 }else{
drh9de221d2008-01-05 06:51:30 +00002048 codeReal(v, z, n, negFlag, iMem);
danielk1977c9cf9012007-05-30 10:36:47 +00002049 }
drhfec19aa2004-05-19 20:41:03 +00002050 }
2051}
2052
drh945498f2007-02-24 11:52:52 +00002053
2054/*
2055** Generate code that will extract the iColumn-th column from
drhe55cbd72008-03-31 23:48:03 +00002056** table pTab and store the column value in a register. An effort
2057** is made to store the column value in register iReg, but this is
2058** not guaranteed. The location of the column value is returned.
2059**
2060** There must be an open cursor to pTab in iTable when this routine
2061** is called. If iColumn<0 then code is generated that extracts the rowid.
drhda250ea2008-04-01 05:07:14 +00002062**
2063** This routine might attempt to reuse the value of the column that
2064** has already been loaded into a register. The value will always
2065** be used if it has not undergone any affinity changes. But if
2066** an affinity change has occurred, then the cached value will only be
2067** used if allowAffChng is true.
drh945498f2007-02-24 11:52:52 +00002068*/
drhe55cbd72008-03-31 23:48:03 +00002069int sqlite3ExprCodeGetColumn(
2070 Parse *pParse, /* Parsing and code generating context */
drh2133d822008-01-03 18:44:59 +00002071 Table *pTab, /* Description of the table we are reading from */
2072 int iColumn, /* Index of the table column */
2073 int iTable, /* The cursor pointing to the table */
drhda250ea2008-04-01 05:07:14 +00002074 int iReg, /* Store results here */
2075 int allowAffChng /* True if prior affinity changes are OK */
drh2133d822008-01-03 18:44:59 +00002076){
drhe55cbd72008-03-31 23:48:03 +00002077 Vdbe *v = pParse->pVdbe;
2078 int i;
drhda250ea2008-04-01 05:07:14 +00002079 struct yColCache *p;
drhe55cbd72008-03-31 23:48:03 +00002080
drhda250ea2008-04-01 05:07:14 +00002081 for(i=0, p=pParse->aColCache; i<pParse->nColCache; i++, p++){
2082 if( p->iTable==iTable && p->iColumn==iColumn
2083 && (!p->affChange || allowAffChng) ){
drhe55cbd72008-03-31 23:48:03 +00002084#if 0
2085 sqlite3VdbeAddOp0(v, OP_Noop);
drhda250ea2008-04-01 05:07:14 +00002086 VdbeComment((v, "OPT: tab%d.col%d -> r%d", iTable, iColumn, p->iReg));
drhe55cbd72008-03-31 23:48:03 +00002087#endif
drhda250ea2008-04-01 05:07:14 +00002088 return p->iReg;
drhe55cbd72008-03-31 23:48:03 +00002089 }
2090 }
2091 assert( v!=0 );
drh945498f2007-02-24 11:52:52 +00002092 if( iColumn<0 ){
2093 int op = (pTab && IsVirtual(pTab)) ? OP_VRowid : OP_Rowid;
drh2133d822008-01-03 18:44:59 +00002094 sqlite3VdbeAddOp2(v, op, iTable, iReg);
drh945498f2007-02-24 11:52:52 +00002095 }else if( pTab==0 ){
drh2133d822008-01-03 18:44:59 +00002096 sqlite3VdbeAddOp3(v, OP_Column, iTable, iColumn, iReg);
drh945498f2007-02-24 11:52:52 +00002097 }else{
2098 int op = IsVirtual(pTab) ? OP_VColumn : OP_Column;
drh2133d822008-01-03 18:44:59 +00002099 sqlite3VdbeAddOp3(v, op, iTable, iColumn, iReg);
drh945498f2007-02-24 11:52:52 +00002100 sqlite3ColumnDefault(v, pTab, iColumn);
2101#ifndef SQLITE_OMIT_FLOATING_POINT
2102 if( pTab->aCol[iColumn].affinity==SQLITE_AFF_REAL ){
drh2133d822008-01-03 18:44:59 +00002103 sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg);
drh945498f2007-02-24 11:52:52 +00002104 }
2105#endif
2106 }
drhe55cbd72008-03-31 23:48:03 +00002107 if( pParse->disableColCache==0 ){
2108 i = pParse->iColCache;
drhda250ea2008-04-01 05:07:14 +00002109 p = &pParse->aColCache[i];
2110 p->iTable = iTable;
2111 p->iColumn = iColumn;
2112 p->iReg = iReg;
drhc5499be2008-04-01 15:06:33 +00002113 p->affChange = 0;
drhe55cbd72008-03-31 23:48:03 +00002114 i++;
drh2f7794c2008-04-01 03:27:39 +00002115 if( i>=ArraySize(pParse->aColCache) ) i = 0;
drhe55cbd72008-03-31 23:48:03 +00002116 if( i>pParse->nColCache ) pParse->nColCache = i;
drh2f7794c2008-04-01 03:27:39 +00002117 pParse->iColCache = i;
drhe55cbd72008-03-31 23:48:03 +00002118 }
2119 return iReg;
2120}
2121
2122/*
drhe55cbd72008-03-31 23:48:03 +00002123** Clear all column cache entries associated with the vdbe
2124** cursor with cursor number iTable.
2125*/
2126void sqlite3ExprClearColumnCache(Parse *pParse, int iTable){
2127 if( iTable<0 ){
2128 pParse->nColCache = 0;
2129 pParse->iColCache = 0;
2130 }else{
2131 int i;
2132 for(i=0; i<pParse->nColCache; i++){
2133 if( pParse->aColCache[i].iTable==iTable ){
drhc5499be2008-04-01 15:06:33 +00002134 testcase( i==pParse->nColCache-1 );
drhe55cbd72008-03-31 23:48:03 +00002135 pParse->aColCache[i] = pParse->aColCache[--pParse->nColCache];
drhda250ea2008-04-01 05:07:14 +00002136 pParse->iColCache = pParse->nColCache;
drhe55cbd72008-03-31 23:48:03 +00002137 }
2138 }
drhe55cbd72008-03-31 23:48:03 +00002139 }
2140}
2141
2142/*
drhda250ea2008-04-01 05:07:14 +00002143** Record the fact that an affinity change has occurred on iCount
2144** registers starting with iStart.
drhe55cbd72008-03-31 23:48:03 +00002145*/
drhda250ea2008-04-01 05:07:14 +00002146void sqlite3ExprCacheAffinityChange(Parse *pParse, int iStart, int iCount){
2147 int iEnd = iStart + iCount - 1;
drhe55cbd72008-03-31 23:48:03 +00002148 int i;
2149 for(i=0; i<pParse->nColCache; i++){
2150 int r = pParse->aColCache[i].iReg;
drhda250ea2008-04-01 05:07:14 +00002151 if( r>=iStart && r<=iEnd ){
2152 pParse->aColCache[i].affChange = 1;
drhe55cbd72008-03-31 23:48:03 +00002153 }
2154 }
drhe55cbd72008-03-31 23:48:03 +00002155}
2156
2157/*
drhb21e7c72008-06-22 12:37:57 +00002158** Generate code to move content from registers iFrom...iFrom+nReg-1
2159** over to iTo..iTo+nReg-1. Keep the column cache up-to-date.
drhe55cbd72008-03-31 23:48:03 +00002160*/
drhb21e7c72008-06-22 12:37:57 +00002161void sqlite3ExprCodeMove(Parse *pParse, int iFrom, int iTo, int nReg){
drhe55cbd72008-03-31 23:48:03 +00002162 int i;
2163 if( iFrom==iTo ) return;
drhb21e7c72008-06-22 12:37:57 +00002164 sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move, iFrom, iTo, nReg);
drhe55cbd72008-03-31 23:48:03 +00002165 for(i=0; i<pParse->nColCache; i++){
drhb21e7c72008-06-22 12:37:57 +00002166 int x = pParse->aColCache[i].iReg;
2167 if( x>=iFrom && x<iFrom+nReg ){
2168 pParse->aColCache[i].iReg += iTo-iFrom;
drhe55cbd72008-03-31 23:48:03 +00002169 }
2170 }
drh945498f2007-02-24 11:52:52 +00002171}
2172
drhfec19aa2004-05-19 20:41:03 +00002173/*
drh92b01d52008-06-24 00:32:35 +00002174** Generate code to copy content from registers iFrom...iFrom+nReg-1
2175** over to iTo..iTo+nReg-1.
2176*/
2177void sqlite3ExprCodeCopy(Parse *pParse, int iFrom, int iTo, int nReg){
2178 int i;
2179 if( iFrom==iTo ) return;
2180 for(i=0; i<nReg; i++){
2181 sqlite3VdbeAddOp2(pParse->pVdbe, OP_Copy, iFrom+i, iTo+i);
2182 }
2183}
2184
2185/*
drh652fbf52008-04-01 01:42:41 +00002186** Return true if any register in the range iFrom..iTo (inclusive)
2187** is used as part of the column cache.
2188*/
2189static int usedAsColumnCache(Parse *pParse, int iFrom, int iTo){
2190 int i;
2191 for(i=0; i<pParse->nColCache; i++){
2192 int r = pParse->aColCache[i].iReg;
2193 if( r>=iFrom && r<=iTo ) return 1;
2194 }
2195 return 0;
2196}
2197
2198/*
2199** Theres is a value in register iCurrent. We ultimately want
2200** the value to be in register iTarget. It might be that
2201** iCurrent and iTarget are the same register.
2202**
2203** We are going to modify the value, so we need to make sure it
2204** is not a cached register. If iCurrent is a cached register,
2205** then try to move the value over to iTarget. If iTarget is a
2206** cached register, then clear the corresponding cache line.
2207**
2208** Return the register that the value ends up in.
2209*/
2210int sqlite3ExprWritableRegister(Parse *pParse, int iCurrent, int iTarget){
drhda250ea2008-04-01 05:07:14 +00002211 int i;
drh652fbf52008-04-01 01:42:41 +00002212 assert( pParse->pVdbe!=0 );
2213 if( !usedAsColumnCache(pParse, iCurrent, iCurrent) ){
2214 return iCurrent;
2215 }
drh2f7794c2008-04-01 03:27:39 +00002216 if( iCurrent!=iTarget ){
2217 sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, iCurrent, iTarget);
2218 }
drhda250ea2008-04-01 05:07:14 +00002219 for(i=0; i<pParse->nColCache; i++){
2220 if( pParse->aColCache[i].iReg==iTarget ){
2221 pParse->aColCache[i] = pParse->aColCache[--pParse->nColCache];
2222 pParse->iColCache = pParse->nColCache;
2223 }
2224 }
drh652fbf52008-04-01 01:42:41 +00002225 return iTarget;
2226}
2227
2228/*
drh191b54c2008-04-15 12:14:21 +00002229** If the last instruction coded is an ephemeral copy of any of
2230** the registers in the nReg registers beginning with iReg, then
2231** convert the last instruction from OP_SCopy to OP_Copy.
2232*/
2233void sqlite3ExprHardCopy(Parse *pParse, int iReg, int nReg){
2234 int addr;
2235 VdbeOp *pOp;
2236 Vdbe *v;
2237
2238 v = pParse->pVdbe;
2239 addr = sqlite3VdbeCurrentAddr(v);
2240 pOp = sqlite3VdbeGetOp(v, addr-1);
danielk1977d7eb2ed2008-04-24 12:36:35 +00002241 assert( pOp || pParse->db->mallocFailed );
2242 if( pOp && pOp->opcode==OP_SCopy && pOp->p1>=iReg && pOp->p1<iReg+nReg ){
drh191b54c2008-04-15 12:14:21 +00002243 pOp->opcode = OP_Copy;
2244 }
2245}
2246
2247/*
drhcce7d172000-05-31 15:34:51 +00002248** Generate code into the current Vdbe to evaluate the given
drh2dcef112008-01-12 19:03:48 +00002249** expression. Attempt to store the results in register "target".
2250** Return the register where results are stored.
drh389a1ad2008-01-03 23:44:53 +00002251**
drh2dcef112008-01-12 19:03:48 +00002252** With this routine, there is no guaranteed that results will
2253** be stored in target. The result might be stored in some other
2254** register if it is convenient to do so. The calling function
2255** must check the return code and move the results to the desired
2256** register.
drhcce7d172000-05-31 15:34:51 +00002257*/
drh678ccce2008-03-31 18:19:54 +00002258int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){
drh2dcef112008-01-12 19:03:48 +00002259 Vdbe *v = pParse->pVdbe; /* The VM under construction */
2260 int op; /* The opcode being coded */
2261 int inReg = target; /* Results stored in register inReg */
2262 int regFree1 = 0; /* If non-zero free this temporary register */
2263 int regFree2 = 0; /* If non-zero free this temporary register */
drh678ccce2008-03-31 18:19:54 +00002264 int r1, r2, r3, r4; /* Various register numbers */
drhffe07b22005-11-03 00:41:17 +00002265
drh389a1ad2008-01-03 23:44:53 +00002266 assert( v!=0 || pParse->db->mallocFailed );
drh9cbf3422008-01-17 16:22:13 +00002267 assert( target>0 && target<=pParse->nMem );
drh389a1ad2008-01-03 23:44:53 +00002268 if( v==0 ) return 0;
drh389a1ad2008-01-03 23:44:53 +00002269
2270 if( pExpr==0 ){
2271 op = TK_NULL;
2272 }else{
2273 op = pExpr->op;
2274 }
drhf2bc0132004-10-04 13:19:23 +00002275 switch( op ){
drh13449892005-09-07 21:22:45 +00002276 case TK_AGG_COLUMN: {
2277 AggInfo *pAggInfo = pExpr->pAggInfo;
2278 struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg];
2279 if( !pAggInfo->directMode ){
drh9de221d2008-01-05 06:51:30 +00002280 assert( pCol->iMem>0 );
2281 inReg = pCol->iMem;
drh13449892005-09-07 21:22:45 +00002282 break;
2283 }else if( pAggInfo->useSortingIdx ){
drh389a1ad2008-01-03 23:44:53 +00002284 sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdx,
2285 pCol->iSorterColumn, target);
drh13449892005-09-07 21:22:45 +00002286 break;
2287 }
2288 /* Otherwise, fall thru into the TK_COLUMN case */
2289 }
drh967e8b72000-06-21 13:59:10 +00002290 case TK_COLUMN: {
drhffe07b22005-11-03 00:41:17 +00002291 if( pExpr->iTable<0 ){
2292 /* This only happens when coding check constraints */
drhaa9b8962008-01-08 02:57:55 +00002293 assert( pParse->ckBase>0 );
2294 inReg = pExpr->iColumn + pParse->ckBase;
drhc4a3c772001-04-04 11:48:57 +00002295 }else{
drhc5499be2008-04-01 15:06:33 +00002296 testcase( (pExpr->flags & EP_AnyAff)!=0 );
drhe55cbd72008-03-31 23:48:03 +00002297 inReg = sqlite3ExprCodeGetColumn(pParse, pExpr->pTab,
drhda250ea2008-04-01 05:07:14 +00002298 pExpr->iColumn, pExpr->iTable, target,
2299 pExpr->flags & EP_AnyAff);
drh22827922000-06-06 17:27:05 +00002300 }
drhcce7d172000-05-31 15:34:51 +00002301 break;
2302 }
2303 case TK_INTEGER: {
drh92b01d52008-06-24 00:32:35 +00002304 codeInteger(v, pExpr, 0, target);
drhfec19aa2004-05-19 20:41:03 +00002305 break;
2306 }
drh598f1342007-10-23 15:39:45 +00002307 case TK_FLOAT: {
drh9de221d2008-01-05 06:51:30 +00002308 codeReal(v, (char*)pExpr->token.z, pExpr->token.n, 0, target);
drh598f1342007-10-23 15:39:45 +00002309 break;
2310 }
drhfec19aa2004-05-19 20:41:03 +00002311 case TK_STRING: {
danielk19771e536952007-08-16 10:09:01 +00002312 sqlite3DequoteExpr(pParse->db, pExpr);
drh9de221d2008-01-05 06:51:30 +00002313 sqlite3VdbeAddOp4(v,OP_String8, 0, target, 0,
drh66a51672008-01-03 00:01:23 +00002314 (char*)pExpr->token.z, pExpr->token.n);
drhcce7d172000-05-31 15:34:51 +00002315 break;
2316 }
drhf0863fe2005-06-12 21:35:51 +00002317 case TK_NULL: {
drh9de221d2008-01-05 06:51:30 +00002318 sqlite3VdbeAddOp2(v, OP_Null, 0, target);
drhf0863fe2005-06-12 21:35:51 +00002319 break;
2320 }
danielk19775338a5f2005-01-20 13:03:10 +00002321#ifndef SQLITE_OMIT_BLOB_LITERAL
danielk1977c572ef72004-05-27 09:28:41 +00002322 case TK_BLOB: {
drh6c8c6ce2005-08-23 11:17:58 +00002323 int n;
2324 const char *z;
drhca48c902008-01-18 14:08:24 +00002325 char *zBlob;
2326 assert( pExpr->token.n>=3 );
2327 assert( pExpr->token.z[0]=='x' || pExpr->token.z[0]=='X' );
2328 assert( pExpr->token.z[1]=='\'' );
2329 assert( pExpr->token.z[pExpr->token.n-1]=='\'' );
drh6c8c6ce2005-08-23 11:17:58 +00002330 n = pExpr->token.n - 3;
drh2646da72005-12-09 20:02:05 +00002331 z = (char*)pExpr->token.z + 2;
drhca48c902008-01-18 14:08:24 +00002332 zBlob = sqlite3HexToBlob(sqlite3VdbeDb(v), z, n);
2333 sqlite3VdbeAddOp4(v, OP_Blob, n/2, target, 0, zBlob, P4_DYNAMIC);
danielk1977c572ef72004-05-27 09:28:41 +00002334 break;
2335 }
danielk19775338a5f2005-01-20 13:03:10 +00002336#endif
drh50457892003-09-06 01:10:47 +00002337 case TK_VARIABLE: {
drh9de221d2008-01-05 06:51:30 +00002338 sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iTable, target);
drh895d7472004-08-20 16:02:39 +00002339 if( pExpr->token.n>1 ){
drh66a51672008-01-03 00:01:23 +00002340 sqlite3VdbeChangeP4(v, -1, (char*)pExpr->token.z, pExpr->token.n);
drh895d7472004-08-20 16:02:39 +00002341 }
drh50457892003-09-06 01:10:47 +00002342 break;
2343 }
drh4e0cff62004-11-05 05:10:28 +00002344 case TK_REGISTER: {
drh9de221d2008-01-05 06:51:30 +00002345 inReg = pExpr->iTable;
drh4e0cff62004-11-05 05:10:28 +00002346 break;
2347 }
drh487e2622005-06-25 18:42:14 +00002348#ifndef SQLITE_OMIT_CAST
2349 case TK_CAST: {
2350 /* Expressions of the form: CAST(pLeft AS token) */
danielk1977f0113002006-01-24 12:09:17 +00002351 int aff, to_op;
drh2dcef112008-01-12 19:03:48 +00002352 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
drh8a512562005-11-14 22:29:05 +00002353 aff = sqlite3AffinityType(&pExpr->token);
danielk1977f0113002006-01-24 12:09:17 +00002354 to_op = aff - SQLITE_AFF_TEXT + OP_ToText;
2355 assert( to_op==OP_ToText || aff!=SQLITE_AFF_TEXT );
2356 assert( to_op==OP_ToBlob || aff!=SQLITE_AFF_NONE );
2357 assert( to_op==OP_ToNumeric || aff!=SQLITE_AFF_NUMERIC );
2358 assert( to_op==OP_ToInt || aff!=SQLITE_AFF_INTEGER );
2359 assert( to_op==OP_ToReal || aff!=SQLITE_AFF_REAL );
drhc5499be2008-04-01 15:06:33 +00002360 testcase( to_op==OP_ToText );
2361 testcase( to_op==OP_ToBlob );
2362 testcase( to_op==OP_ToNumeric );
2363 testcase( to_op==OP_ToInt );
2364 testcase( to_op==OP_ToReal );
drh2dcef112008-01-12 19:03:48 +00002365 sqlite3VdbeAddOp1(v, to_op, inReg);
drhc5499be2008-04-01 15:06:33 +00002366 testcase( usedAsColumnCache(pParse, inReg, inReg) );
drhb3843a82008-04-01 12:24:11 +00002367 sqlite3ExprCacheAffinityChange(pParse, inReg, 1);
drh487e2622005-06-25 18:42:14 +00002368 break;
2369 }
2370#endif /* SQLITE_OMIT_CAST */
drhc9b84a12002-06-20 11:36:48 +00002371 case TK_LT:
2372 case TK_LE:
2373 case TK_GT:
2374 case TK_GE:
2375 case TK_NE:
2376 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00002377 assert( TK_LT==OP_Lt );
2378 assert( TK_LE==OP_Le );
2379 assert( TK_GT==OP_Gt );
2380 assert( TK_GE==OP_Ge );
2381 assert( TK_EQ==OP_Eq );
2382 assert( TK_NE==OP_Ne );
drhc5499be2008-04-01 15:06:33 +00002383 testcase( op==TK_LT );
2384 testcase( op==TK_LE );
2385 testcase( op==TK_GT );
2386 testcase( op==TK_GE );
2387 testcase( op==TK_EQ );
2388 testcase( op==TK_NE );
drhda250ea2008-04-01 05:07:14 +00002389 codeCompareOperands(pParse, pExpr->pLeft, &r1, &regFree1,
2390 pExpr->pRight, &r2, &regFree2);
drh35573352008-01-08 23:54:25 +00002391 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
2392 r1, r2, inReg, SQLITE_STOREP2);
drhc5499be2008-04-01 15:06:33 +00002393 testcase( regFree1==0 );
2394 testcase( regFree2==0 );
danielk1977a37cdde2004-05-16 11:15:36 +00002395 break;
drhc9b84a12002-06-20 11:36:48 +00002396 }
drhcce7d172000-05-31 15:34:51 +00002397 case TK_AND:
2398 case TK_OR:
2399 case TK_PLUS:
2400 case TK_STAR:
2401 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00002402 case TK_REM:
2403 case TK_BITAND:
2404 case TK_BITOR:
drh17c40292004-07-21 02:53:29 +00002405 case TK_SLASH:
drhbf4133c2001-10-13 02:59:08 +00002406 case TK_LSHIFT:
drh855eb1c2004-08-31 13:45:11 +00002407 case TK_RSHIFT:
drh00400772000-06-16 20:51:26 +00002408 case TK_CONCAT: {
drhf2bc0132004-10-04 13:19:23 +00002409 assert( TK_AND==OP_And );
2410 assert( TK_OR==OP_Or );
2411 assert( TK_PLUS==OP_Add );
2412 assert( TK_MINUS==OP_Subtract );
2413 assert( TK_REM==OP_Remainder );
2414 assert( TK_BITAND==OP_BitAnd );
2415 assert( TK_BITOR==OP_BitOr );
2416 assert( TK_SLASH==OP_Divide );
2417 assert( TK_LSHIFT==OP_ShiftLeft );
2418 assert( TK_RSHIFT==OP_ShiftRight );
2419 assert( TK_CONCAT==OP_Concat );
drhc5499be2008-04-01 15:06:33 +00002420 testcase( op==TK_AND );
2421 testcase( op==TK_OR );
2422 testcase( op==TK_PLUS );
2423 testcase( op==TK_MINUS );
2424 testcase( op==TK_REM );
2425 testcase( op==TK_BITAND );
2426 testcase( op==TK_BITOR );
2427 testcase( op==TK_SLASH );
2428 testcase( op==TK_LSHIFT );
2429 testcase( op==TK_RSHIFT );
2430 testcase( op==TK_CONCAT );
drh2dcef112008-01-12 19:03:48 +00002431 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
2432 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
drh5b6afba2008-01-05 16:29:28 +00002433 sqlite3VdbeAddOp3(v, op, r2, r1, target);
drhc5499be2008-04-01 15:06:33 +00002434 testcase( regFree1==0 );
2435 testcase( regFree2==0 );
drh00400772000-06-16 20:51:26 +00002436 break;
2437 }
drhcce7d172000-05-31 15:34:51 +00002438 case TK_UMINUS: {
drhfec19aa2004-05-19 20:41:03 +00002439 Expr *pLeft = pExpr->pLeft;
2440 assert( pLeft );
2441 if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
drhfec19aa2004-05-19 20:41:03 +00002442 if( pLeft->op==TK_FLOAT ){
drh92b01d52008-06-24 00:32:35 +00002443 codeReal(v, (char*)pLeft->token.z, pLeft->token.n, 1, target);
drhe6840902002-03-06 03:08:25 +00002444 }else{
drh92b01d52008-06-24 00:32:35 +00002445 codeInteger(v, pLeft, 1, target);
drhe6840902002-03-06 03:08:25 +00002446 }
drh3c84ddf2008-01-09 02:15:38 +00002447 }else{
drh2dcef112008-01-12 19:03:48 +00002448 regFree1 = r1 = sqlite3GetTempReg(pParse);
drh3c84ddf2008-01-09 02:15:38 +00002449 sqlite3VdbeAddOp2(v, OP_Integer, 0, r1);
drhe55cbd72008-03-31 23:48:03 +00002450 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree2);
drh2dcef112008-01-12 19:03:48 +00002451 sqlite3VdbeAddOp3(v, OP_Subtract, r2, r1, target);
drhc5499be2008-04-01 15:06:33 +00002452 testcase( regFree2==0 );
drh6e142f52000-06-08 13:36:40 +00002453 }
drh3c84ddf2008-01-09 02:15:38 +00002454 inReg = target;
2455 break;
drh6e142f52000-06-08 13:36:40 +00002456 }
drhbf4133c2001-10-13 02:59:08 +00002457 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00002458 case TK_NOT: {
drhf2bc0132004-10-04 13:19:23 +00002459 assert( TK_BITNOT==OP_BitNot );
2460 assert( TK_NOT==OP_Not );
drhc5499be2008-04-01 15:06:33 +00002461 testcase( op==TK_BITNOT );
2462 testcase( op==TK_NOT );
drh2dcef112008-01-12 19:03:48 +00002463 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
drhc5499be2008-04-01 15:06:33 +00002464 testcase( inReg==target );
2465 testcase( usedAsColumnCache(pParse, inReg, inReg) );
drh652fbf52008-04-01 01:42:41 +00002466 inReg = sqlite3ExprWritableRegister(pParse, inReg, target);
drh2dcef112008-01-12 19:03:48 +00002467 sqlite3VdbeAddOp1(v, op, inReg);
drhcce7d172000-05-31 15:34:51 +00002468 break;
2469 }
2470 case TK_ISNULL:
2471 case TK_NOTNULL: {
drh6a288a32008-01-07 19:20:24 +00002472 int addr;
drhf2bc0132004-10-04 13:19:23 +00002473 assert( TK_ISNULL==OP_IsNull );
2474 assert( TK_NOTNULL==OP_NotNull );
drhc5499be2008-04-01 15:06:33 +00002475 testcase( op==TK_ISNULL );
2476 testcase( op==TK_NOTNULL );
drh9de221d2008-01-05 06:51:30 +00002477 sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
drh2dcef112008-01-12 19:03:48 +00002478 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
drhc5499be2008-04-01 15:06:33 +00002479 testcase( regFree1==0 );
drh2dcef112008-01-12 19:03:48 +00002480 addr = sqlite3VdbeAddOp1(v, op, r1);
drh9de221d2008-01-05 06:51:30 +00002481 sqlite3VdbeAddOp2(v, OP_AddImm, target, -1);
drh6a288a32008-01-07 19:20:24 +00002482 sqlite3VdbeJumpHere(v, addr);
drhf2bc0132004-10-04 13:19:23 +00002483 break;
drhcce7d172000-05-31 15:34:51 +00002484 }
drh22827922000-06-06 17:27:05 +00002485 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00002486 AggInfo *pInfo = pExpr->pAggInfo;
drh7e56e712005-11-16 12:53:15 +00002487 if( pInfo==0 ){
2488 sqlite3ErrorMsg(pParse, "misuse of aggregate: %T",
2489 &pExpr->span);
2490 }else{
drh9de221d2008-01-05 06:51:30 +00002491 inReg = pInfo->aFunc[pExpr->iAgg].iMem;
drh7e56e712005-11-16 12:53:15 +00002492 }
drh22827922000-06-06 17:27:05 +00002493 break;
2494 }
drhb71090f2005-05-23 17:26:51 +00002495 case TK_CONST_FUNC:
drhcce7d172000-05-31 15:34:51 +00002496 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00002497 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00002498 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00002499 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00002500 int nId;
2501 const char *zId;
drh13449892005-09-07 21:22:45 +00002502 int constMask = 0;
danielk1977682f68b2004-06-05 10:22:17 +00002503 int i;
drh17435752007-08-16 04:30:38 +00002504 sqlite3 *db = pParse->db;
2505 u8 enc = ENC(db);
danielk1977dc1bdc42004-06-11 10:51:27 +00002506 CollSeq *pColl = 0;
drh17435752007-08-16 04:30:38 +00002507
drhc5499be2008-04-01 15:06:33 +00002508 testcase( op==TK_CONST_FUNC );
2509 testcase( op==TK_FUNCTION );
drh2646da72005-12-09 20:02:05 +00002510 zId = (char*)pExpr->token.z;
drhb71090f2005-05-23 17:26:51 +00002511 nId = pExpr->token.n;
danielk1977d8123362004-06-12 09:25:12 +00002512 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0);
drh0bce8352002-02-28 00:41:10 +00002513 assert( pDef!=0 );
drh892d3172008-01-10 03:46:36 +00002514 if( pList ){
2515 nExpr = pList->nExpr;
drh2dcef112008-01-12 19:03:48 +00002516 r1 = sqlite3GetTempRange(pParse, nExpr);
drh191b54c2008-04-15 12:14:21 +00002517 sqlite3ExprCodeExprList(pParse, pList, r1, 1);
drh892d3172008-01-10 03:46:36 +00002518 }else{
drhd847eaa2008-01-17 17:15:56 +00002519 nExpr = r1 = 0;
drh892d3172008-01-10 03:46:36 +00002520 }
drhb7f6f682006-07-08 17:06:43 +00002521#ifndef SQLITE_OMIT_VIRTUALTABLE
drha43fa222006-07-08 18:41:37 +00002522 /* Possibly overload the function if the first argument is
2523 ** a virtual table column.
2524 **
2525 ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the
2526 ** second argument, not the first, as the argument to test to
2527 ** see if it is a column in a virtual table. This is done because
2528 ** the left operand of infix functions (the operand we want to
2529 ** control overloading) ends up as the second argument to the
2530 ** function. The expression "A glob B" is equivalent to
2531 ** "glob(B,A). We want to use the A in "A glob B" to test
2532 ** for function overloading. But we use the B term in "glob(B,A)".
2533 */
drh6a03a1c2006-07-08 18:34:59 +00002534 if( nExpr>=2 && (pExpr->flags & EP_InfixFunc) ){
drh17435752007-08-16 04:30:38 +00002535 pDef = sqlite3VtabOverloadFunction(db, pDef, nExpr, pList->a[1].pExpr);
drh6a03a1c2006-07-08 18:34:59 +00002536 }else if( nExpr>0 ){
drh17435752007-08-16 04:30:38 +00002537 pDef = sqlite3VtabOverloadFunction(db, pDef, nExpr, pList->a[0].pExpr);
drhb7f6f682006-07-08 17:06:43 +00002538 }
2539#endif
danielk1977682f68b2004-06-05 10:22:17 +00002540 for(i=0; i<nExpr && i<32; i++){
danielk1977d02eb1f2004-06-06 09:44:03 +00002541 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
drh13449892005-09-07 21:22:45 +00002542 constMask |= (1<<i);
danielk1977d02eb1f2004-06-06 09:44:03 +00002543 }
danielk1977dc1bdc42004-06-11 10:51:27 +00002544 if( pDef->needCollSeq && !pColl ){
2545 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
2546 }
2547 }
2548 if( pDef->needCollSeq ){
2549 if( !pColl ) pColl = pParse->db->pDfltColl;
drh66a51672008-01-03 00:01:23 +00002550 sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ);
danielk1977682f68b2004-06-05 10:22:17 +00002551 }
drh2dcef112008-01-12 19:03:48 +00002552 sqlite3VdbeAddOp4(v, OP_Function, constMask, r1, target,
drh66a51672008-01-03 00:01:23 +00002553 (char*)pDef, P4_FUNCDEF);
drh98757152008-01-09 23:04:12 +00002554 sqlite3VdbeChangeP5(v, nExpr);
drh2dcef112008-01-12 19:03:48 +00002555 if( nExpr ){
2556 sqlite3ReleaseTempRange(pParse, r1, nExpr);
2557 }
drhda250ea2008-04-01 05:07:14 +00002558 sqlite3ExprCacheAffinityChange(pParse, r1, nExpr);
drhcce7d172000-05-31 15:34:51 +00002559 break;
2560 }
drhfe2093d2005-01-20 22:48:47 +00002561#ifndef SQLITE_OMIT_SUBQUERY
2562 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00002563 case TK_SELECT: {
drhc5499be2008-04-01 15:06:33 +00002564 testcase( op==TK_EXISTS );
2565 testcase( op==TK_SELECT );
drh41714d62006-03-02 04:44:23 +00002566 if( pExpr->iColumn==0 ){
danielk19770cdc0222008-06-26 18:04:03 +00002567 sqlite3CodeSubselect(pParse, pExpr, 0);
drh41714d62006-03-02 04:44:23 +00002568 }
drh9de221d2008-01-05 06:51:30 +00002569 inReg = pExpr->iColumn;
drh19a775c2000-06-05 18:54:46 +00002570 break;
2571 }
drhfef52082000-06-06 01:50:43 +00002572 case TK_IN: {
danielk19770cdc0222008-06-26 18:04:03 +00002573 int rNotFound = 0;
2574 int rMayHaveNull = 0;
drh6fccc352008-06-27 00:52:45 +00002575 int j2, j3, j4, j5;
drh94a11212004-09-25 13:12:14 +00002576 char affinity;
danielk19779a96b662007-11-29 17:05:18 +00002577 int eType;
danielk19779a96b662007-11-29 17:05:18 +00002578
drh3c31fc22008-06-26 21:45:26 +00002579 VdbeNoopComment((v, "begin IN expr r%d", target));
danielk19770cdc0222008-06-26 18:04:03 +00002580 eType = sqlite3FindInIndex(pParse, pExpr, &rMayHaveNull);
2581 if( rMayHaveNull ){
2582 rNotFound = ++pParse->nMem;
2583 }
danielk1977e014a832004-05-17 10:48:57 +00002584
2585 /* Figure out the affinity to use to create a key from the results
2586 ** of the expression. affinityStr stores a static string suitable for
drh66a51672008-01-03 00:01:23 +00002587 ** P4 of OP_MakeRecord.
danielk1977e014a832004-05-17 10:48:57 +00002588 */
drh94a11212004-09-25 13:12:14 +00002589 affinity = comparisonAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +00002590
danielk1977e014a832004-05-17 10:48:57 +00002591
2592 /* Code the <expr> from "<expr> IN (...)". The temporary table
2593 ** pExpr->iTable contains the values that make up the (...) set.
2594 */
drh66ba23c2008-06-27 00:47:28 +00002595 pParse->disableColCache++;
2596 sqlite3ExprCode(pParse, pExpr->pLeft, target);
2597 pParse->disableColCache--;
2598 j2 = sqlite3VdbeAddOp1(v, OP_IsNull, target);
danielk19779a96b662007-11-29 17:05:18 +00002599 if( eType==IN_INDEX_ROWID ){
drh66ba23c2008-06-27 00:47:28 +00002600 j3 = sqlite3VdbeAddOp1(v, OP_MustBeInt, target);
2601 j4 = sqlite3VdbeAddOp3(v, OP_NotExists, pExpr->iTable, 0, target);
2602 sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
drh6a288a32008-01-07 19:20:24 +00002603 j5 = sqlite3VdbeAddOp0(v, OP_Goto);
2604 sqlite3VdbeJumpHere(v, j3);
2605 sqlite3VdbeJumpHere(v, j4);
danielk19770cdc0222008-06-26 18:04:03 +00002606 sqlite3VdbeAddOp2(v, OP_Integer, 0, target);
danielk19779a96b662007-11-29 17:05:18 +00002607 }else{
drh2dcef112008-01-12 19:03:48 +00002608 r2 = regFree2 = sqlite3GetTempReg(pParse);
danielk19770cdc0222008-06-26 18:04:03 +00002609
2610 /* Create a record and test for set membership. If the set contains
2611 ** the value, then jump to the end of the test code. The target
2612 ** register still contains the true (1) value written to it earlier.
2613 */
drh66ba23c2008-06-27 00:47:28 +00002614 sqlite3VdbeAddOp4(v, OP_MakeRecord, target, 1, r2, &affinity, 1);
2615 sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
drh2dcef112008-01-12 19:03:48 +00002616 j5 = sqlite3VdbeAddOp3(v, OP_Found, pExpr->iTable, 0, r2);
danielk19770cdc0222008-06-26 18:04:03 +00002617
2618 /* If the set membership test fails, then the result of the
2619 ** "x IN (...)" expression must be either 0 or NULL. If the set
2620 ** contains no NULL values, then the result is 0. If the set
2621 ** contains one or more NULL values, then the result of the
2622 ** expression is also NULL.
2623 */
2624 if( rNotFound==0 ){
2625 /* This branch runs if it is known at compile time (now) that
2626 ** the set contains no NULL values. This happens as the result
2627 ** of a "NOT NULL" constraint in the database schema. No need
2628 ** to test the data structure at runtime in this case.
2629 */
2630 sqlite3VdbeAddOp2(v, OP_Integer, 0, target);
2631 }else{
2632 /* This block populates the rNotFound register with either NULL
2633 ** or 0 (an integer value). If the data structure contains one
2634 ** or more NULLs, then set rNotFound to NULL. Otherwise, set it
2635 ** to 0. If register rMayHaveNull is already set to some value
2636 ** other than NULL, then the test has already been run and
2637 ** rNotFound is already populated.
2638 */
drh66ba23c2008-06-27 00:47:28 +00002639 static const char nullRecord[] = { 0x02, 0x00 };
danielk19770cdc0222008-06-26 18:04:03 +00002640 j3 = sqlite3VdbeAddOp1(v, OP_NotNull, rMayHaveNull);
2641 sqlite3VdbeAddOp2(v, OP_Null, 0, rNotFound);
drh66ba23c2008-06-27 00:47:28 +00002642 sqlite3VdbeAddOp4(v, OP_Blob, 2, rMayHaveNull, 0,
2643 nullRecord, P4_STATIC);
2644 j4 = sqlite3VdbeAddOp3(v, OP_Found, pExpr->iTable, 0, rMayHaveNull);
danielk19770cdc0222008-06-26 18:04:03 +00002645 sqlite3VdbeAddOp2(v, OP_Integer, 0, rNotFound);
2646 sqlite3VdbeJumpHere(v, j4);
2647 sqlite3VdbeJumpHere(v, j3);
2648
2649 /* Copy the value of register rNotFound (which is either NULL or 0)
2650 ** into the target register. This will be the result of the
2651 ** expression.
2652 */
2653 sqlite3VdbeAddOp2(v, OP_Copy, rNotFound, target);
2654 }
danielk19779a96b662007-11-29 17:05:18 +00002655 }
drh6a288a32008-01-07 19:20:24 +00002656 sqlite3VdbeJumpHere(v, j2);
2657 sqlite3VdbeJumpHere(v, j5);
drh3c31fc22008-06-26 21:45:26 +00002658 VdbeComment((v, "end IN expr r%d", target));
drhfef52082000-06-06 01:50:43 +00002659 break;
2660 }
danielk197793758c82005-01-21 08:13:14 +00002661#endif
drh2dcef112008-01-12 19:03:48 +00002662 /*
2663 ** x BETWEEN y AND z
2664 **
2665 ** This is equivalent to
2666 **
2667 ** x>=y AND x<=z
2668 **
2669 ** X is stored in pExpr->pLeft.
2670 ** Y is stored in pExpr->pList->a[0].pExpr.
2671 ** Z is stored in pExpr->pList->a[1].pExpr.
2672 */
drhfef52082000-06-06 01:50:43 +00002673 case TK_BETWEEN: {
drhbe5c89a2004-07-26 00:31:09 +00002674 Expr *pLeft = pExpr->pLeft;
2675 struct ExprList_item *pLItem = pExpr->pList->a;
2676 Expr *pRight = pLItem->pExpr;
drh35573352008-01-08 23:54:25 +00002677
drhda250ea2008-04-01 05:07:14 +00002678 codeCompareOperands(pParse, pLeft, &r1, &regFree1,
2679 pRight, &r2, &regFree2);
drhc5499be2008-04-01 15:06:33 +00002680 testcase( regFree1==0 );
2681 testcase( regFree2==0 );
drh2dcef112008-01-12 19:03:48 +00002682 r3 = sqlite3GetTempReg(pParse);
drh678ccce2008-03-31 18:19:54 +00002683 r4 = sqlite3GetTempReg(pParse);
drh35573352008-01-08 23:54:25 +00002684 codeCompare(pParse, pLeft, pRight, OP_Ge,
2685 r1, r2, r3, SQLITE_STOREP2);
drhbe5c89a2004-07-26 00:31:09 +00002686 pLItem++;
2687 pRight = pLItem->pExpr;
drh2dcef112008-01-12 19:03:48 +00002688 sqlite3ReleaseTempReg(pParse, regFree2);
2689 r2 = sqlite3ExprCodeTemp(pParse, pRight, &regFree2);
drhc5499be2008-04-01 15:06:33 +00002690 testcase( regFree2==0 );
drh678ccce2008-03-31 18:19:54 +00002691 codeCompare(pParse, pLeft, pRight, OP_Le, r1, r2, r4, SQLITE_STOREP2);
2692 sqlite3VdbeAddOp3(v, OP_And, r3, r4, target);
drh2dcef112008-01-12 19:03:48 +00002693 sqlite3ReleaseTempReg(pParse, r3);
drh678ccce2008-03-31 18:19:54 +00002694 sqlite3ReleaseTempReg(pParse, r4);
drhfef52082000-06-06 01:50:43 +00002695 break;
2696 }
drh4f07e5f2007-05-14 11:34:46 +00002697 case TK_UPLUS: {
drh2dcef112008-01-12 19:03:48 +00002698 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
drha2e00042002-01-22 03:13:42 +00002699 break;
2700 }
drh2dcef112008-01-12 19:03:48 +00002701
2702 /*
2703 ** Form A:
2704 ** CASE x WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
2705 **
2706 ** Form B:
2707 ** CASE WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
2708 **
2709 ** Form A is can be transformed into the equivalent form B as follows:
2710 ** CASE WHEN x=e1 THEN r1 WHEN x=e2 THEN r2 ...
2711 ** WHEN x=eN THEN rN ELSE y END
2712 **
2713 ** X (if it exists) is in pExpr->pLeft.
2714 ** Y is in pExpr->pRight. The Y is also optional. If there is no
2715 ** ELSE clause and no other term matches, then the result of the
2716 ** exprssion is NULL.
2717 ** Ei is in pExpr->pList->a[i*2] and Ri is pExpr->pList->a[i*2+1].
2718 **
2719 ** The result of the expression is the Ri for the first matching Ei,
2720 ** or if there is no matching Ei, the ELSE term Y, or if there is
2721 ** no ELSE term, NULL.
2722 */
drh17a7f8d2002-03-24 13:13:27 +00002723 case TK_CASE: {
drh2dcef112008-01-12 19:03:48 +00002724 int endLabel; /* GOTO label for end of CASE stmt */
2725 int nextCase; /* GOTO label for next WHEN clause */
2726 int nExpr; /* 2x number of WHEN terms */
2727 int i; /* Loop counter */
2728 ExprList *pEList; /* List of WHEN terms */
2729 struct ExprList_item *aListelem; /* Array of WHEN terms */
2730 Expr opCompare; /* The X==Ei expression */
2731 Expr cacheX; /* Cached expression X */
2732 Expr *pX; /* The X expression */
2733 Expr *pTest; /* X==Ei (form A) or just Ei (form B) */
drh17a7f8d2002-03-24 13:13:27 +00002734
2735 assert(pExpr->pList);
2736 assert((pExpr->pList->nExpr % 2) == 0);
2737 assert(pExpr->pList->nExpr > 0);
drhbe5c89a2004-07-26 00:31:09 +00002738 pEList = pExpr->pList;
2739 aListelem = pEList->a;
2740 nExpr = pEList->nExpr;
drh2dcef112008-01-12 19:03:48 +00002741 endLabel = sqlite3VdbeMakeLabel(v);
2742 if( (pX = pExpr->pLeft)!=0 ){
2743 cacheX = *pX;
drhc5499be2008-04-01 15:06:33 +00002744 testcase( pX->op==TK_COLUMN || pX->op==TK_REGISTER );
drh2dcef112008-01-12 19:03:48 +00002745 cacheX.iTable = sqlite3ExprCodeTemp(pParse, pX, &regFree1);
drhc5499be2008-04-01 15:06:33 +00002746 testcase( regFree1==0 );
drh2dcef112008-01-12 19:03:48 +00002747 cacheX.op = TK_REGISTER;
drh678ccce2008-03-31 18:19:54 +00002748 cacheX.iColumn = 0;
drh2dcef112008-01-12 19:03:48 +00002749 opCompare.op = TK_EQ;
2750 opCompare.pLeft = &cacheX;
2751 pTest = &opCompare;
drh17a7f8d2002-03-24 13:13:27 +00002752 }
drhc5499be2008-04-01 15:06:33 +00002753 pParse->disableColCache++;
drhf5905aa2002-05-26 20:54:33 +00002754 for(i=0; i<nExpr; i=i+2){
drh2dcef112008-01-12 19:03:48 +00002755 if( pX ){
2756 opCompare.pRight = aListelem[i].pExpr;
drh17a7f8d2002-03-24 13:13:27 +00002757 }else{
drh2dcef112008-01-12 19:03:48 +00002758 pTest = aListelem[i].pExpr;
drh17a7f8d2002-03-24 13:13:27 +00002759 }
drh2dcef112008-01-12 19:03:48 +00002760 nextCase = sqlite3VdbeMakeLabel(v);
drhc5499be2008-04-01 15:06:33 +00002761 testcase( pTest->op==TK_COLUMN || pTest->op==TK_REGISTER );
drh2dcef112008-01-12 19:03:48 +00002762 sqlite3ExprIfFalse(pParse, pTest, nextCase, SQLITE_JUMPIFNULL);
drhc5499be2008-04-01 15:06:33 +00002763 testcase( aListelem[i+1].pExpr->op==TK_COLUMN );
2764 testcase( aListelem[i+1].pExpr->op==TK_REGISTER );
drh9de221d2008-01-05 06:51:30 +00002765 sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target);
drh2dcef112008-01-12 19:03:48 +00002766 sqlite3VdbeAddOp2(v, OP_Goto, 0, endLabel);
2767 sqlite3VdbeResolveLabel(v, nextCase);
drhf570f012002-05-31 15:51:25 +00002768 }
drh17a7f8d2002-03-24 13:13:27 +00002769 if( pExpr->pRight ){
drh9de221d2008-01-05 06:51:30 +00002770 sqlite3ExprCode(pParse, pExpr->pRight, target);
drh17a7f8d2002-03-24 13:13:27 +00002771 }else{
drh9de221d2008-01-05 06:51:30 +00002772 sqlite3VdbeAddOp2(v, OP_Null, 0, target);
drh17a7f8d2002-03-24 13:13:27 +00002773 }
drh2dcef112008-01-12 19:03:48 +00002774 sqlite3VdbeResolveLabel(v, endLabel);
drhc5499be2008-04-01 15:06:33 +00002775 assert( pParse->disableColCache>0 );
2776 pParse->disableColCache--;
danielk19776f349032002-06-11 02:25:40 +00002777 break;
2778 }
danielk19775338a5f2005-01-20 13:03:10 +00002779#ifndef SQLITE_OMIT_TRIGGER
danielk19776f349032002-06-11 02:25:40 +00002780 case TK_RAISE: {
2781 if( !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +00002782 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00002783 "RAISE() may only be used within a trigger-program");
drh389a1ad2008-01-03 23:44:53 +00002784 return 0;
danielk19776f349032002-06-11 02:25:40 +00002785 }
drhad6d9462004-09-19 02:15:24 +00002786 if( pExpr->iColumn!=OE_Ignore ){
2787 assert( pExpr->iColumn==OE_Rollback ||
2788 pExpr->iColumn == OE_Abort ||
2789 pExpr->iColumn == OE_Fail );
danielk19771e536952007-08-16 10:09:01 +00002790 sqlite3DequoteExpr(pParse->db, pExpr);
drh66a51672008-01-03 00:01:23 +00002791 sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn, 0,
drh2646da72005-12-09 20:02:05 +00002792 (char*)pExpr->token.z, pExpr->token.n);
danielk19776f349032002-06-11 02:25:40 +00002793 } else {
drhad6d9462004-09-19 02:15:24 +00002794 assert( pExpr->iColumn == OE_Ignore );
drh66a51672008-01-03 00:01:23 +00002795 sqlite3VdbeAddOp2(v, OP_ContextPop, 0, 0);
2796 sqlite3VdbeAddOp2(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
drhd4e70eb2008-01-02 00:34:36 +00002797 VdbeComment((v, "raise(IGNORE)"));
danielk19776f349032002-06-11 02:25:40 +00002798 }
drhffe07b22005-11-03 00:41:17 +00002799 break;
drh17a7f8d2002-03-24 13:13:27 +00002800 }
danielk19775338a5f2005-01-20 13:03:10 +00002801#endif
drhffe07b22005-11-03 00:41:17 +00002802 }
drh2dcef112008-01-12 19:03:48 +00002803 sqlite3ReleaseTempReg(pParse, regFree1);
2804 sqlite3ReleaseTempReg(pParse, regFree2);
2805 return inReg;
2806}
2807
2808/*
2809** Generate code to evaluate an expression and store the results
2810** into a register. Return the register number where the results
2811** are stored.
2812**
2813** If the register is a temporary register that can be deallocated,
drh678ccce2008-03-31 18:19:54 +00002814** then write its number into *pReg. If the result register is not
drh2dcef112008-01-12 19:03:48 +00002815** a temporary, then set *pReg to zero.
2816*/
2817int sqlite3ExprCodeTemp(Parse *pParse, Expr *pExpr, int *pReg){
2818 int r1 = sqlite3GetTempReg(pParse);
2819 int r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
2820 if( r2==r1 ){
2821 *pReg = r1;
2822 }else{
2823 sqlite3ReleaseTempReg(pParse, r1);
2824 *pReg = 0;
2825 }
2826 return r2;
2827}
2828
2829/*
2830** Generate code that will evaluate expression pExpr and store the
2831** results in register target. The results are guaranteed to appear
2832** in register target.
2833*/
2834int sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){
drh9cbf3422008-01-17 16:22:13 +00002835 int inReg;
2836
2837 assert( target>0 && target<=pParse->nMem );
2838 inReg = sqlite3ExprCodeTarget(pParse, pExpr, target);
drh0e359b32008-01-13 19:02:11 +00002839 assert( pParse->pVdbe || pParse->db->mallocFailed );
2840 if( inReg!=target && pParse->pVdbe ){
drh9cbf3422008-01-17 16:22:13 +00002841 sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, inReg, target);
drhcce7d172000-05-31 15:34:51 +00002842 }
drh389a1ad2008-01-03 23:44:53 +00002843 return target;
drhcce7d172000-05-31 15:34:51 +00002844}
2845
2846/*
drh2dcef112008-01-12 19:03:48 +00002847** Generate code that evalutes the given expression and puts the result
drhde4fcfd2008-01-19 23:50:26 +00002848** in register target.
drh25303782004-12-07 15:41:48 +00002849**
drh2dcef112008-01-12 19:03:48 +00002850** Also make a copy of the expression results into another "cache" register
2851** and modify the expression so that the next time it is evaluated,
2852** the result is a copy of the cache register.
2853**
2854** This routine is used for expressions that are used multiple
2855** times. They are evaluated once and the results of the expression
2856** are reused.
drh25303782004-12-07 15:41:48 +00002857*/
drh2dcef112008-01-12 19:03:48 +00002858int sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr, int target){
drh25303782004-12-07 15:41:48 +00002859 Vdbe *v = pParse->pVdbe;
drh2dcef112008-01-12 19:03:48 +00002860 int inReg;
2861 inReg = sqlite3ExprCode(pParse, pExpr, target);
drhde4fcfd2008-01-19 23:50:26 +00002862 assert( target>0 );
drh2dcef112008-01-12 19:03:48 +00002863 if( pExpr->op!=TK_REGISTER ){
2864 int iMem;
drhde4fcfd2008-01-19 23:50:26 +00002865 iMem = ++pParse->nMem;
2866 sqlite3VdbeAddOp2(v, OP_Copy, inReg, iMem);
drh2dcef112008-01-12 19:03:48 +00002867 pExpr->iTable = iMem;
drh678ccce2008-03-31 18:19:54 +00002868 pExpr->iColumn = pExpr->op;
drh25303782004-12-07 15:41:48 +00002869 pExpr->op = TK_REGISTER;
2870 }
drh2dcef112008-01-12 19:03:48 +00002871 return inReg;
drh25303782004-12-07 15:41:48 +00002872}
drh2dcef112008-01-12 19:03:48 +00002873
drh678ccce2008-03-31 18:19:54 +00002874/*
drh47de9552008-04-01 18:04:11 +00002875** Return TRUE if pExpr is an constant expression that is appropriate
2876** for factoring out of a loop. Appropriate expressions are:
2877**
2878** * Any expression that evaluates to two or more opcodes.
2879**
2880** * Any OP_Integer, OP_Real, OP_String, OP_Blob, OP_Null,
2881** or OP_Variable that does not need to be placed in a
2882** specific register.
2883**
2884** There is no point in factoring out single-instruction constant
2885** expressions that need to be placed in a particular register.
2886** We could factor them out, but then we would end up adding an
2887** OP_SCopy instruction to move the value into the correct register
2888** later. We might as well just use the original instruction and
2889** avoid the OP_SCopy.
2890*/
2891static int isAppropriateForFactoring(Expr *p){
2892 if( !sqlite3ExprIsConstantNotJoin(p) ){
2893 return 0; /* Only constant expressions are appropriate for factoring */
2894 }
2895 if( (p->flags & EP_FixedDest)==0 ){
2896 return 1; /* Any constant without a fixed destination is appropriate */
2897 }
2898 while( p->op==TK_UPLUS ) p = p->pLeft;
2899 switch( p->op ){
2900#ifndef SQLITE_OMIT_BLOB_LITERAL
2901 case TK_BLOB:
2902#endif
2903 case TK_VARIABLE:
2904 case TK_INTEGER:
2905 case TK_FLOAT:
2906 case TK_NULL:
2907 case TK_STRING: {
2908 testcase( p->op==TK_BLOB );
2909 testcase( p->op==TK_VARIABLE );
2910 testcase( p->op==TK_INTEGER );
2911 testcase( p->op==TK_FLOAT );
2912 testcase( p->op==TK_NULL );
2913 testcase( p->op==TK_STRING );
2914 /* Single-instruction constants with a fixed destination are
2915 ** better done in-line. If we factor them, they will just end
2916 ** up generating an OP_SCopy to move the value to the destination
2917 ** register. */
2918 return 0;
2919 }
2920 case TK_UMINUS: {
2921 if( p->pLeft->op==TK_FLOAT || p->pLeft->op==TK_INTEGER ){
2922 return 0;
2923 }
2924 break;
2925 }
2926 default: {
2927 break;
2928 }
2929 }
2930 return 1;
2931}
2932
2933/*
2934** If pExpr is a constant expression that is appropriate for
2935** factoring out of a loop, then evaluate the expression
drh678ccce2008-03-31 18:19:54 +00002936** into a register and convert the expression into a TK_REGISTER
2937** expression.
2938*/
2939static int evalConstExpr(void *pArg, Expr *pExpr){
2940 Parse *pParse = (Parse*)pArg;
drh47de9552008-04-01 18:04:11 +00002941 switch( pExpr->op ){
2942 case TK_REGISTER: {
2943 return 1;
2944 }
2945 case TK_FUNCTION:
2946 case TK_AGG_FUNCTION:
2947 case TK_CONST_FUNC: {
2948 /* The arguments to a function have a fixed destination.
2949 ** Mark them this way to avoid generated unneeded OP_SCopy
2950 ** instructions.
2951 */
2952 ExprList *pList = pExpr->pList;
2953 if( pList ){
2954 int i = pList->nExpr;
2955 struct ExprList_item *pItem = pList->a;
2956 for(; i>0; i--, pItem++){
2957 if( pItem->pExpr ) pItem->pExpr->flags |= EP_FixedDest;
2958 }
2959 }
2960 break;
2961 }
drh678ccce2008-03-31 18:19:54 +00002962 }
drh47de9552008-04-01 18:04:11 +00002963 if( isAppropriateForFactoring(pExpr) ){
drh678ccce2008-03-31 18:19:54 +00002964 int r1 = ++pParse->nMem;
2965 int r2;
2966 r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
drhc5499be2008-04-01 15:06:33 +00002967 if( r1!=r2 ) sqlite3ReleaseTempReg(pParse, r1);
drh678ccce2008-03-31 18:19:54 +00002968 pExpr->iColumn = pExpr->op;
2969 pExpr->op = TK_REGISTER;
2970 pExpr->iTable = r2;
2971 return 1;
2972 }
2973 return 0;
2974}
2975
2976/*
2977** Preevaluate constant subexpressions within pExpr and store the
2978** results in registers. Modify pExpr so that the constant subexpresions
2979** are TK_REGISTER opcodes that refer to the precomputed values.
2980*/
2981void sqlite3ExprCodeConstants(Parse *pParse, Expr *pExpr){
2982 walkExprTree(pExpr, evalConstExpr, pParse);
2983}
2984
drh25303782004-12-07 15:41:48 +00002985
2986/*
drh268380c2004-02-25 13:47:31 +00002987** Generate code that pushes the value of every element of the given
drh9cbf3422008-01-17 16:22:13 +00002988** expression list into a sequence of registers beginning at target.
drh268380c2004-02-25 13:47:31 +00002989**
drh892d3172008-01-10 03:46:36 +00002990** Return the number of elements evaluated.
drh268380c2004-02-25 13:47:31 +00002991*/
danielk19774adee202004-05-08 08:23:19 +00002992int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00002993 Parse *pParse, /* Parsing context */
drh389a1ad2008-01-03 23:44:53 +00002994 ExprList *pList, /* The expression list to be coded */
drh191b54c2008-04-15 12:14:21 +00002995 int target, /* Where to write results */
2996 int doHardCopy /* Call sqlite3ExprHardCopy on each element if true */
drh268380c2004-02-25 13:47:31 +00002997){
2998 struct ExprList_item *pItem;
drh9cbf3422008-01-17 16:22:13 +00002999 int i, n;
drh892d3172008-01-10 03:46:36 +00003000 assert( pList!=0 || pParse->db->mallocFailed );
3001 if( pList==0 ){
3002 return 0;
3003 }
drh9cbf3422008-01-17 16:22:13 +00003004 assert( target>0 );
drh268380c2004-02-25 13:47:31 +00003005 n = pList->nExpr;
drh191b54c2008-04-15 12:14:21 +00003006 for(pItem=pList->a, i=0; i<n; i++, pItem++){
3007 sqlite3ExprCode(pParse, pItem->pExpr, target+i);
3008 if( doHardCopy ) sqlite3ExprHardCopy(pParse, target, n);
drh268380c2004-02-25 13:47:31 +00003009 }
drhf9b596e2004-05-26 16:54:42 +00003010 return n;
drh268380c2004-02-25 13:47:31 +00003011}
3012
3013/*
drhcce7d172000-05-31 15:34:51 +00003014** Generate code for a boolean expression such that a jump is made
3015** to the label "dest" if the expression is true but execution
3016** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00003017**
3018** If the expression evaluates to NULL (neither true nor false), then
drh35573352008-01-08 23:54:25 +00003019** take the jump if the jumpIfNull flag is SQLITE_JUMPIFNULL.
drhf2bc0132004-10-04 13:19:23 +00003020**
3021** This code depends on the fact that certain token values (ex: TK_EQ)
3022** are the same as opcode values (ex: OP_Eq) that implement the corresponding
3023** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
3024** the make process cause these values to align. Assert()s in the code
3025** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00003026*/
danielk19774adee202004-05-08 08:23:19 +00003027void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00003028 Vdbe *v = pParse->pVdbe;
3029 int op = 0;
drh2dcef112008-01-12 19:03:48 +00003030 int regFree1 = 0;
3031 int regFree2 = 0;
3032 int r1, r2;
3033
drh35573352008-01-08 23:54:25 +00003034 assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
drhdaffd0e2001-04-11 14:28:42 +00003035 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00003036 op = pExpr->op;
3037 switch( op ){
drhcce7d172000-05-31 15:34:51 +00003038 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00003039 int d2 = sqlite3VdbeMakeLabel(v);
drhc5499be2008-04-01 15:06:33 +00003040 testcase( jumpIfNull==0 );
3041 testcase( pParse->disableColCache==0 );
drh35573352008-01-08 23:54:25 +00003042 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,jumpIfNull^SQLITE_JUMPIFNULL);
drhe55cbd72008-03-31 23:48:03 +00003043 pParse->disableColCache++;
danielk19774adee202004-05-08 08:23:19 +00003044 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhc5499be2008-04-01 15:06:33 +00003045 assert( pParse->disableColCache>0 );
drhe55cbd72008-03-31 23:48:03 +00003046 pParse->disableColCache--;
danielk19774adee202004-05-08 08:23:19 +00003047 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00003048 break;
3049 }
3050 case TK_OR: {
drhc5499be2008-04-01 15:06:33 +00003051 testcase( jumpIfNull==0 );
3052 testcase( pParse->disableColCache==0 );
danielk19774adee202004-05-08 08:23:19 +00003053 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhe55cbd72008-03-31 23:48:03 +00003054 pParse->disableColCache++;
danielk19774adee202004-05-08 08:23:19 +00003055 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhc5499be2008-04-01 15:06:33 +00003056 assert( pParse->disableColCache>0 );
drhe55cbd72008-03-31 23:48:03 +00003057 pParse->disableColCache--;
drhcce7d172000-05-31 15:34:51 +00003058 break;
3059 }
3060 case TK_NOT: {
drhc5499be2008-04-01 15:06:33 +00003061 testcase( jumpIfNull==0 );
danielk19774adee202004-05-08 08:23:19 +00003062 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00003063 break;
3064 }
3065 case TK_LT:
3066 case TK_LE:
3067 case TK_GT:
3068 case TK_GE:
3069 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00003070 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00003071 assert( TK_LT==OP_Lt );
3072 assert( TK_LE==OP_Le );
3073 assert( TK_GT==OP_Gt );
3074 assert( TK_GE==OP_Ge );
3075 assert( TK_EQ==OP_Eq );
3076 assert( TK_NE==OP_Ne );
drhc5499be2008-04-01 15:06:33 +00003077 testcase( op==TK_LT );
3078 testcase( op==TK_LE );
3079 testcase( op==TK_GT );
3080 testcase( op==TK_GE );
3081 testcase( op==TK_EQ );
3082 testcase( op==TK_NE );
3083 testcase( jumpIfNull==0 );
drhda250ea2008-04-01 05:07:14 +00003084 codeCompareOperands(pParse, pExpr->pLeft, &r1, &regFree1,
3085 pExpr->pRight, &r2, &regFree2);
drh35573352008-01-08 23:54:25 +00003086 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
drh2dcef112008-01-12 19:03:48 +00003087 r1, r2, dest, jumpIfNull);
drhc5499be2008-04-01 15:06:33 +00003088 testcase( regFree1==0 );
3089 testcase( regFree2==0 );
drhcce7d172000-05-31 15:34:51 +00003090 break;
3091 }
3092 case TK_ISNULL:
3093 case TK_NOTNULL: {
drhf2bc0132004-10-04 13:19:23 +00003094 assert( TK_ISNULL==OP_IsNull );
3095 assert( TK_NOTNULL==OP_NotNull );
drhc5499be2008-04-01 15:06:33 +00003096 testcase( op==TK_ISNULL );
3097 testcase( op==TK_NOTNULL );
drh2dcef112008-01-12 19:03:48 +00003098 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
3099 sqlite3VdbeAddOp2(v, op, r1, dest);
drhc5499be2008-04-01 15:06:33 +00003100 testcase( regFree1==0 );
drhcce7d172000-05-31 15:34:51 +00003101 break;
3102 }
drhfef52082000-06-06 01:50:43 +00003103 case TK_BETWEEN: {
drh2dcef112008-01-12 19:03:48 +00003104 /* x BETWEEN y AND z
danielk19770202b292004-06-09 09:55:16 +00003105 **
drh2dcef112008-01-12 19:03:48 +00003106 ** Is equivalent to
3107 **
3108 ** x>=y AND x<=z
3109 **
3110 ** Code it as such, taking care to do the common subexpression
3111 ** elementation of x.
danielk19770202b292004-06-09 09:55:16 +00003112 */
drh2dcef112008-01-12 19:03:48 +00003113 Expr exprAnd;
3114 Expr compLeft;
3115 Expr compRight;
3116 Expr exprX;
danielk19770202b292004-06-09 09:55:16 +00003117
drh2dcef112008-01-12 19:03:48 +00003118 exprX = *pExpr->pLeft;
3119 exprAnd.op = TK_AND;
3120 exprAnd.pLeft = &compLeft;
3121 exprAnd.pRight = &compRight;
3122 compLeft.op = TK_GE;
3123 compLeft.pLeft = &exprX;
3124 compLeft.pRight = pExpr->pList->a[0].pExpr;
3125 compRight.op = TK_LE;
3126 compRight.pLeft = &exprX;
3127 compRight.pRight = pExpr->pList->a[1].pExpr;
3128 exprX.iTable = sqlite3ExprCodeTemp(pParse, &exprX, &regFree1);
drhc5499be2008-04-01 15:06:33 +00003129 testcase( regFree1==0 );
drh2dcef112008-01-12 19:03:48 +00003130 exprX.op = TK_REGISTER;
drhc5499be2008-04-01 15:06:33 +00003131 testcase( jumpIfNull==0 );
drh2dcef112008-01-12 19:03:48 +00003132 sqlite3ExprIfTrue(pParse, &exprAnd, dest, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00003133 break;
3134 }
drhcce7d172000-05-31 15:34:51 +00003135 default: {
drh2dcef112008-01-12 19:03:48 +00003136 r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
3137 sqlite3VdbeAddOp3(v, OP_If, r1, dest, jumpIfNull!=0);
drhc5499be2008-04-01 15:06:33 +00003138 testcase( regFree1==0 );
3139 testcase( jumpIfNull==0 );
drhcce7d172000-05-31 15:34:51 +00003140 break;
3141 }
3142 }
drh2dcef112008-01-12 19:03:48 +00003143 sqlite3ReleaseTempReg(pParse, regFree1);
3144 sqlite3ReleaseTempReg(pParse, regFree2);
drhcce7d172000-05-31 15:34:51 +00003145}
3146
3147/*
drh66b89c82000-11-28 20:47:17 +00003148** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00003149** to the label "dest" if the expression is false but execution
3150** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00003151**
3152** If the expression evaluates to NULL (neither true nor false) then
drh35573352008-01-08 23:54:25 +00003153** jump if jumpIfNull is SQLITE_JUMPIFNULL or fall through if jumpIfNull
3154** is 0.
drhcce7d172000-05-31 15:34:51 +00003155*/
danielk19774adee202004-05-08 08:23:19 +00003156void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00003157 Vdbe *v = pParse->pVdbe;
3158 int op = 0;
drh2dcef112008-01-12 19:03:48 +00003159 int regFree1 = 0;
3160 int regFree2 = 0;
3161 int r1, r2;
3162
drh35573352008-01-08 23:54:25 +00003163 assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
drhdaffd0e2001-04-11 14:28:42 +00003164 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00003165
3166 /* The value of pExpr->op and op are related as follows:
3167 **
3168 ** pExpr->op op
3169 ** --------- ----------
3170 ** TK_ISNULL OP_NotNull
3171 ** TK_NOTNULL OP_IsNull
3172 ** TK_NE OP_Eq
3173 ** TK_EQ OP_Ne
3174 ** TK_GT OP_Le
3175 ** TK_LE OP_Gt
3176 ** TK_GE OP_Lt
3177 ** TK_LT OP_Ge
3178 **
3179 ** For other values of pExpr->op, op is undefined and unused.
3180 ** The value of TK_ and OP_ constants are arranged such that we
3181 ** can compute the mapping above using the following expression.
3182 ** Assert()s verify that the computation is correct.
3183 */
3184 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
3185
3186 /* Verify correct alignment of TK_ and OP_ constants
3187 */
3188 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
3189 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
3190 assert( pExpr->op!=TK_NE || op==OP_Eq );
3191 assert( pExpr->op!=TK_EQ || op==OP_Ne );
3192 assert( pExpr->op!=TK_LT || op==OP_Ge );
3193 assert( pExpr->op!=TK_LE || op==OP_Gt );
3194 assert( pExpr->op!=TK_GT || op==OP_Le );
3195 assert( pExpr->op!=TK_GE || op==OP_Lt );
3196
drhcce7d172000-05-31 15:34:51 +00003197 switch( pExpr->op ){
3198 case TK_AND: {
drhc5499be2008-04-01 15:06:33 +00003199 testcase( jumpIfNull==0 );
3200 testcase( pParse->disableColCache==0 );
danielk19774adee202004-05-08 08:23:19 +00003201 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhe55cbd72008-03-31 23:48:03 +00003202 pParse->disableColCache++;
danielk19774adee202004-05-08 08:23:19 +00003203 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhc5499be2008-04-01 15:06:33 +00003204 assert( pParse->disableColCache>0 );
drhe55cbd72008-03-31 23:48:03 +00003205 pParse->disableColCache--;
drhcce7d172000-05-31 15:34:51 +00003206 break;
3207 }
3208 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00003209 int d2 = sqlite3VdbeMakeLabel(v);
drhc5499be2008-04-01 15:06:33 +00003210 testcase( jumpIfNull==0 );
3211 testcase( pParse->disableColCache==0 );
drh35573352008-01-08 23:54:25 +00003212 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, jumpIfNull^SQLITE_JUMPIFNULL);
drhe55cbd72008-03-31 23:48:03 +00003213 pParse->disableColCache++;
danielk19774adee202004-05-08 08:23:19 +00003214 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhc5499be2008-04-01 15:06:33 +00003215 assert( pParse->disableColCache>0 );
drhe55cbd72008-03-31 23:48:03 +00003216 pParse->disableColCache--;
danielk19774adee202004-05-08 08:23:19 +00003217 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00003218 break;
3219 }
3220 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00003221 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00003222 break;
3223 }
3224 case TK_LT:
3225 case TK_LE:
3226 case TK_GT:
3227 case TK_GE:
3228 case TK_NE:
3229 case TK_EQ: {
drhc5499be2008-04-01 15:06:33 +00003230 testcase( op==TK_LT );
3231 testcase( op==TK_LE );
3232 testcase( op==TK_GT );
3233 testcase( op==TK_GE );
3234 testcase( op==TK_EQ );
3235 testcase( op==TK_NE );
3236 testcase( jumpIfNull==0 );
drhda250ea2008-04-01 05:07:14 +00003237 codeCompareOperands(pParse, pExpr->pLeft, &r1, &regFree1,
3238 pExpr->pRight, &r2, &regFree2);
drh35573352008-01-08 23:54:25 +00003239 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
drh2dcef112008-01-12 19:03:48 +00003240 r1, r2, dest, jumpIfNull);
drhc5499be2008-04-01 15:06:33 +00003241 testcase( regFree1==0 );
3242 testcase( regFree2==0 );
drhcce7d172000-05-31 15:34:51 +00003243 break;
3244 }
drhcce7d172000-05-31 15:34:51 +00003245 case TK_ISNULL:
3246 case TK_NOTNULL: {
drhc5499be2008-04-01 15:06:33 +00003247 testcase( op==TK_ISNULL );
3248 testcase( op==TK_NOTNULL );
drh2dcef112008-01-12 19:03:48 +00003249 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
3250 sqlite3VdbeAddOp2(v, op, r1, dest);
drhc5499be2008-04-01 15:06:33 +00003251 testcase( regFree1==0 );
drhcce7d172000-05-31 15:34:51 +00003252 break;
3253 }
drhfef52082000-06-06 01:50:43 +00003254 case TK_BETWEEN: {
drh2dcef112008-01-12 19:03:48 +00003255 /* x BETWEEN y AND z
danielk19770202b292004-06-09 09:55:16 +00003256 **
drh2dcef112008-01-12 19:03:48 +00003257 ** Is equivalent to
3258 **
3259 ** x>=y AND x<=z
3260 **
3261 ** Code it as such, taking care to do the common subexpression
3262 ** elementation of x.
danielk19770202b292004-06-09 09:55:16 +00003263 */
drh2dcef112008-01-12 19:03:48 +00003264 Expr exprAnd;
3265 Expr compLeft;
3266 Expr compRight;
3267 Expr exprX;
drhbe5c89a2004-07-26 00:31:09 +00003268
drh2dcef112008-01-12 19:03:48 +00003269 exprX = *pExpr->pLeft;
3270 exprAnd.op = TK_AND;
3271 exprAnd.pLeft = &compLeft;
3272 exprAnd.pRight = &compRight;
3273 compLeft.op = TK_GE;
3274 compLeft.pLeft = &exprX;
3275 compLeft.pRight = pExpr->pList->a[0].pExpr;
3276 compRight.op = TK_LE;
3277 compRight.pLeft = &exprX;
3278 compRight.pRight = pExpr->pList->a[1].pExpr;
3279 exprX.iTable = sqlite3ExprCodeTemp(pParse, &exprX, &regFree1);
drhc5499be2008-04-01 15:06:33 +00003280 testcase( regFree1==0 );
drh2dcef112008-01-12 19:03:48 +00003281 exprX.op = TK_REGISTER;
drhc5499be2008-04-01 15:06:33 +00003282 testcase( jumpIfNull==0 );
drh2dcef112008-01-12 19:03:48 +00003283 sqlite3ExprIfFalse(pParse, &exprAnd, dest, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00003284 break;
3285 }
drhcce7d172000-05-31 15:34:51 +00003286 default: {
drh2dcef112008-01-12 19:03:48 +00003287 r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
3288 sqlite3VdbeAddOp3(v, OP_IfNot, r1, dest, jumpIfNull!=0);
drhc5499be2008-04-01 15:06:33 +00003289 testcase( regFree1==0 );
3290 testcase( jumpIfNull==0 );
drhcce7d172000-05-31 15:34:51 +00003291 break;
3292 }
3293 }
drh2dcef112008-01-12 19:03:48 +00003294 sqlite3ReleaseTempReg(pParse, regFree1);
3295 sqlite3ReleaseTempReg(pParse, regFree2);
drhcce7d172000-05-31 15:34:51 +00003296}
drh22827922000-06-06 17:27:05 +00003297
3298/*
3299** Do a deep comparison of two expression trees. Return TRUE (non-zero)
3300** if they are identical and return FALSE if they differ in any way.
drhd40aab02007-02-24 15:29:03 +00003301**
3302** Sometimes this routine will return FALSE even if the two expressions
3303** really are equivalent. If we cannot prove that the expressions are
3304** identical, we return FALSE just to be safe. So if this routine
3305** returns false, then you do not really know for certain if the two
3306** expressions are the same. But if you get a TRUE return, then you
3307** can be sure the expressions are the same. In the places where
3308** this routine is used, it does not hurt to get an extra FALSE - that
3309** just might result in some slightly slower code. But returning
3310** an incorrect TRUE could lead to a malfunction.
drh22827922000-06-06 17:27:05 +00003311*/
danielk19774adee202004-05-08 08:23:19 +00003312int sqlite3ExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00003313 int i;
danielk19774b202ae2006-01-23 05:50:58 +00003314 if( pA==0||pB==0 ){
3315 return pB==pA;
drh22827922000-06-06 17:27:05 +00003316 }
3317 if( pA->op!=pB->op ) return 0;
drhfd357972005-09-09 01:33:19 +00003318 if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 0;
danielk19774adee202004-05-08 08:23:19 +00003319 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
3320 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00003321 if( pA->pList ){
3322 if( pB->pList==0 ) return 0;
3323 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
3324 for(i=0; i<pA->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00003325 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00003326 return 0;
3327 }
3328 }
3329 }else if( pB->pList ){
3330 return 0;
3331 }
3332 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00003333 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drhdd735212007-02-24 13:53:05 +00003334 if( pA->op!=TK_COLUMN && pA->token.z ){
drh22827922000-06-06 17:27:05 +00003335 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00003336 if( pB->token.n!=pA->token.n ) return 0;
drh2646da72005-12-09 20:02:05 +00003337 if( sqlite3StrNICmp((char*)pA->token.z,(char*)pB->token.z,pB->token.n)!=0 ){
3338 return 0;
3339 }
drh22827922000-06-06 17:27:05 +00003340 }
3341 return 1;
3342}
3343
drh13449892005-09-07 21:22:45 +00003344
drh22827922000-06-06 17:27:05 +00003345/*
drh13449892005-09-07 21:22:45 +00003346** Add a new element to the pAggInfo->aCol[] array. Return the index of
3347** the new element. Return a negative number if malloc fails.
drh22827922000-06-06 17:27:05 +00003348*/
drh17435752007-08-16 04:30:38 +00003349static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){
drh13449892005-09-07 21:22:45 +00003350 int i;
drhcf643722007-03-27 13:36:37 +00003351 pInfo->aCol = sqlite3ArrayAllocate(
drh17435752007-08-16 04:30:38 +00003352 db,
drhcf643722007-03-27 13:36:37 +00003353 pInfo->aCol,
3354 sizeof(pInfo->aCol[0]),
3355 3,
3356 &pInfo->nColumn,
3357 &pInfo->nColumnAlloc,
3358 &i
3359 );
drh13449892005-09-07 21:22:45 +00003360 return i;
3361}
3362
3363/*
3364** Add a new element to the pAggInfo->aFunc[] array. Return the index of
3365** the new element. Return a negative number if malloc fails.
3366*/
drh17435752007-08-16 04:30:38 +00003367static int addAggInfoFunc(sqlite3 *db, AggInfo *pInfo){
drh13449892005-09-07 21:22:45 +00003368 int i;
drhcf643722007-03-27 13:36:37 +00003369 pInfo->aFunc = sqlite3ArrayAllocate(
drh17435752007-08-16 04:30:38 +00003370 db,
drhcf643722007-03-27 13:36:37 +00003371 pInfo->aFunc,
3372 sizeof(pInfo->aFunc[0]),
3373 3,
3374 &pInfo->nFunc,
3375 &pInfo->nFuncAlloc,
3376 &i
3377 );
drh13449892005-09-07 21:22:45 +00003378 return i;
3379}
drh22827922000-06-06 17:27:05 +00003380
3381/*
drh626a8792005-01-17 22:08:19 +00003382** This is an xFunc for walkExprTree() used to implement
3383** sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates
3384** for additional information.
drh22827922000-06-06 17:27:05 +00003385**
drh626a8792005-01-17 22:08:19 +00003386** This routine analyzes the aggregate function at pExpr.
drh22827922000-06-06 17:27:05 +00003387*/
drh626a8792005-01-17 22:08:19 +00003388static int analyzeAggregate(void *pArg, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00003389 int i;
danielk1977a58fdfb2005-02-08 07:50:40 +00003390 NameContext *pNC = (NameContext *)pArg;
3391 Parse *pParse = pNC->pParse;
3392 SrcList *pSrcList = pNC->pSrcList;
drh13449892005-09-07 21:22:45 +00003393 AggInfo *pAggInfo = pNC->pAggInfo;
drh22827922000-06-06 17:27:05 +00003394
drh22827922000-06-06 17:27:05 +00003395 switch( pExpr->op ){
drh89c69d02007-01-04 01:20:28 +00003396 case TK_AGG_COLUMN:
drh967e8b72000-06-21 13:59:10 +00003397 case TK_COLUMN: {
drh13449892005-09-07 21:22:45 +00003398 /* Check to see if the column is in one of the tables in the FROM
3399 ** clause of the aggregate query */
3400 if( pSrcList ){
3401 struct SrcList_item *pItem = pSrcList->a;
3402 for(i=0; i<pSrcList->nSrc; i++, pItem++){
3403 struct AggInfo_col *pCol;
3404 if( pExpr->iTable==pItem->iCursor ){
3405 /* If we reach this point, it means that pExpr refers to a table
3406 ** that is in the FROM clause of the aggregate query.
3407 **
3408 ** Make an entry for the column in pAggInfo->aCol[] if there
3409 ** is not an entry there already.
3410 */
drh7f906d62007-03-12 23:48:52 +00003411 int k;
drh13449892005-09-07 21:22:45 +00003412 pCol = pAggInfo->aCol;
drh7f906d62007-03-12 23:48:52 +00003413 for(k=0; k<pAggInfo->nColumn; k++, pCol++){
drh13449892005-09-07 21:22:45 +00003414 if( pCol->iTable==pExpr->iTable &&
3415 pCol->iColumn==pExpr->iColumn ){
3416 break;
3417 }
danielk1977a58fdfb2005-02-08 07:50:40 +00003418 }
danielk19771e536952007-08-16 10:09:01 +00003419 if( (k>=pAggInfo->nColumn)
3420 && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0
3421 ){
drh7f906d62007-03-12 23:48:52 +00003422 pCol = &pAggInfo->aCol[k];
danielk19770817d0d2007-02-14 09:19:36 +00003423 pCol->pTab = pExpr->pTab;
drh13449892005-09-07 21:22:45 +00003424 pCol->iTable = pExpr->iTable;
3425 pCol->iColumn = pExpr->iColumn;
drh0a07c102008-01-03 18:03:08 +00003426 pCol->iMem = ++pParse->nMem;
drh13449892005-09-07 21:22:45 +00003427 pCol->iSorterColumn = -1;
drh5774b802005-09-07 22:48:16 +00003428 pCol->pExpr = pExpr;
drh13449892005-09-07 21:22:45 +00003429 if( pAggInfo->pGroupBy ){
3430 int j, n;
3431 ExprList *pGB = pAggInfo->pGroupBy;
3432 struct ExprList_item *pTerm = pGB->a;
3433 n = pGB->nExpr;
3434 for(j=0; j<n; j++, pTerm++){
3435 Expr *pE = pTerm->pExpr;
3436 if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable &&
3437 pE->iColumn==pExpr->iColumn ){
3438 pCol->iSorterColumn = j;
3439 break;
3440 }
3441 }
3442 }
3443 if( pCol->iSorterColumn<0 ){
3444 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
3445 }
3446 }
3447 /* There is now an entry for pExpr in pAggInfo->aCol[] (either
3448 ** because it was there before or because we just created it).
3449 ** Convert the pExpr to be a TK_AGG_COLUMN referring to that
3450 ** pAggInfo->aCol[] entry.
3451 */
3452 pExpr->pAggInfo = pAggInfo;
3453 pExpr->op = TK_AGG_COLUMN;
drh7f906d62007-03-12 23:48:52 +00003454 pExpr->iAgg = k;
drh13449892005-09-07 21:22:45 +00003455 break;
3456 } /* endif pExpr->iTable==pItem->iCursor */
3457 } /* end loop over pSrcList */
drh22827922000-06-06 17:27:05 +00003458 }
drh626a8792005-01-17 22:08:19 +00003459 return 1;
drh22827922000-06-06 17:27:05 +00003460 }
3461 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00003462 /* The pNC->nDepth==0 test causes aggregate functions in subqueries
3463 ** to be ignored */
danielk1977a58fdfb2005-02-08 07:50:40 +00003464 if( pNC->nDepth==0 ){
drh13449892005-09-07 21:22:45 +00003465 /* Check to see if pExpr is a duplicate of another aggregate
3466 ** function that is already in the pAggInfo structure
3467 */
3468 struct AggInfo_func *pItem = pAggInfo->aFunc;
3469 for(i=0; i<pAggInfo->nFunc; i++, pItem++){
3470 if( sqlite3ExprCompare(pItem->pExpr, pExpr) ){
danielk1977a58fdfb2005-02-08 07:50:40 +00003471 break;
3472 }
drh22827922000-06-06 17:27:05 +00003473 }
drh13449892005-09-07 21:22:45 +00003474 if( i>=pAggInfo->nFunc ){
3475 /* pExpr is original. Make a new entry in pAggInfo->aFunc[]
3476 */
danielk197714db2662006-01-09 16:12:04 +00003477 u8 enc = ENC(pParse->db);
danielk19771e536952007-08-16 10:09:01 +00003478 i = addAggInfoFunc(pParse->db, pAggInfo);
drh13449892005-09-07 21:22:45 +00003479 if( i>=0 ){
3480 pItem = &pAggInfo->aFunc[i];
3481 pItem->pExpr = pExpr;
drh0a07c102008-01-03 18:03:08 +00003482 pItem->iMem = ++pParse->nMem;
drh13449892005-09-07 21:22:45 +00003483 pItem->pFunc = sqlite3FindFunction(pParse->db,
drh2646da72005-12-09 20:02:05 +00003484 (char*)pExpr->token.z, pExpr->token.n,
drh13449892005-09-07 21:22:45 +00003485 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
drhfd357972005-09-09 01:33:19 +00003486 if( pExpr->flags & EP_Distinct ){
3487 pItem->iDistinct = pParse->nTab++;
3488 }else{
3489 pItem->iDistinct = -1;
3490 }
drh13449892005-09-07 21:22:45 +00003491 }
danielk1977a58fdfb2005-02-08 07:50:40 +00003492 }
drh13449892005-09-07 21:22:45 +00003493 /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
3494 */
danielk1977a58fdfb2005-02-08 07:50:40 +00003495 pExpr->iAgg = i;
drh13449892005-09-07 21:22:45 +00003496 pExpr->pAggInfo = pAggInfo;
danielk1977a58fdfb2005-02-08 07:50:40 +00003497 return 1;
drh22827922000-06-06 17:27:05 +00003498 }
drh22827922000-06-06 17:27:05 +00003499 }
3500 }
drh13449892005-09-07 21:22:45 +00003501
3502 /* Recursively walk subqueries looking for TK_COLUMN nodes that need
3503 ** to be changed to TK_AGG_COLUMN. But increment nDepth so that
3504 ** TK_AGG_FUNCTION nodes in subqueries will be unchanged.
3505 */
danielk1977a58fdfb2005-02-08 07:50:40 +00003506 if( pExpr->pSelect ){
3507 pNC->nDepth++;
3508 walkSelectExpr(pExpr->pSelect, analyzeAggregate, pNC);
3509 pNC->nDepth--;
3510 }
drh626a8792005-01-17 22:08:19 +00003511 return 0;
3512}
3513
3514/*
3515** Analyze the given expression looking for aggregate functions and
3516** for variables that need to be added to the pParse->aAgg[] array.
3517** Make additional entries to the pParse->aAgg[] array as necessary.
3518**
3519** This routine should only be called after the expression has been
3520** analyzed by sqlite3ExprResolveNames().
drh626a8792005-01-17 22:08:19 +00003521*/
drhd2b3e232008-01-23 14:51:49 +00003522void sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
danielk1977a58fdfb2005-02-08 07:50:40 +00003523 walkExprTree(pExpr, analyzeAggregate, pNC);
drh22827922000-06-06 17:27:05 +00003524}
drh5d9a4af2005-08-30 00:54:01 +00003525
3526/*
3527** Call sqlite3ExprAnalyzeAggregates() for every expression in an
3528** expression list. Return the number of errors.
3529**
3530** If an error is found, the analysis is cut short.
3531*/
drhd2b3e232008-01-23 14:51:49 +00003532void sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){
drh5d9a4af2005-08-30 00:54:01 +00003533 struct ExprList_item *pItem;
3534 int i;
drh5d9a4af2005-08-30 00:54:01 +00003535 if( pList ){
drhd2b3e232008-01-23 14:51:49 +00003536 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
3537 sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
drh5d9a4af2005-08-30 00:54:01 +00003538 }
3539 }
drh5d9a4af2005-08-30 00:54:01 +00003540}
drh892d3172008-01-10 03:46:36 +00003541
3542/*
3543** Allocate or deallocate temporary use registers during code generation.
3544*/
3545int sqlite3GetTempReg(Parse *pParse){
drhe55cbd72008-03-31 23:48:03 +00003546 if( pParse->nTempReg==0 ){
drh892d3172008-01-10 03:46:36 +00003547 return ++pParse->nMem;
3548 }
danielk19772f425f62008-07-04 09:41:39 +00003549 return pParse->aTempReg[--pParse->nTempReg];
drh892d3172008-01-10 03:46:36 +00003550}
3551void sqlite3ReleaseTempReg(Parse *pParse, int iReg){
drh2dcef112008-01-12 19:03:48 +00003552 if( iReg && pParse->nTempReg<ArraySize(pParse->aTempReg) ){
danielk1977a7d8b852008-07-04 09:15:11 +00003553 sqlite3ExprWritableRegister(pParse, iReg, iReg);
drh892d3172008-01-10 03:46:36 +00003554 pParse->aTempReg[pParse->nTempReg++] = iReg;
3555 }
3556}
3557
3558/*
3559** Allocate or deallocate a block of nReg consecutive registers
3560*/
3561int sqlite3GetTempRange(Parse *pParse, int nReg){
drhe55cbd72008-03-31 23:48:03 +00003562 int i, n;
3563 i = pParse->iRangeReg;
3564 n = pParse->nRangeReg;
3565 if( nReg<=n && !usedAsColumnCache(pParse, i, i+n-1) ){
drh892d3172008-01-10 03:46:36 +00003566 pParse->iRangeReg += nReg;
3567 pParse->nRangeReg -= nReg;
3568 }else{
3569 i = pParse->nMem+1;
3570 pParse->nMem += nReg;
3571 }
3572 return i;
3573}
3574void sqlite3ReleaseTempRange(Parse *pParse, int iReg, int nReg){
3575 if( nReg>pParse->nRangeReg ){
3576 pParse->nRangeReg = nReg;
3577 pParse->iRangeReg = iReg;
3578 }
3579}