blob: 87be4a91574ebcb90278cfcc511969a3883f0ef1 [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**
drhe54a62a2008-07-18 17:03:52 +000015** $Id: expr.c,v 1.386 2008/07/18 17:03:53 drh Exp $
drhcce7d172000-05-31 15:34:51 +000016*/
17#include "sqliteInt.h"
drh04738cb2002-06-02 18:19:00 +000018#include <ctype.h>
drha2e00042002-01-22 03:13:42 +000019
danielk1977e014a832004-05-17 10:48:57 +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;
danielk197739002502007-11-12 09:50:26 +000059 zColl = sqlite3NameFromToken(pParse->db, pName);
60 if( pExpr && zColl ){
61 pColl = sqlite3LocateCollSeq(pParse, zColl, -1);
62 if( pColl ){
63 pExpr->pColl = pColl;
64 pExpr->flags |= EP_ExpCollate;
65 }
drh8b4c40d2007-02-01 23:02:45 +000066 }
danielk197739002502007-11-12 09:50:26 +000067 sqlite3_free(zColl);
drh8b4c40d2007-02-01 23:02:45 +000068 return pExpr;
69}
70
71/*
danielk19770202b292004-06-09 09:55:16 +000072** Return the default collation sequence for the expression pExpr. If
73** there is no default collation type, return 0.
74*/
danielk19777cedc8d2004-06-10 10:50:08 +000075CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
76 CollSeq *pColl = 0;
danielk19770202b292004-06-09 09:55:16 +000077 if( pExpr ){
drh7e09fe02007-06-20 16:13:23 +000078 int op;
danielk19777cedc8d2004-06-10 10:50:08 +000079 pColl = pExpr->pColl;
drh7e09fe02007-06-20 16:13:23 +000080 op = pExpr->op;
81 if( (op==TK_CAST || op==TK_UPLUS) && !pColl ){
danielk19777cedc8d2004-06-10 10:50:08 +000082 return sqlite3ExprCollSeq(pParse, pExpr->pLeft);
danielk19770202b292004-06-09 09:55:16 +000083 }
84 }
danielk19777cedc8d2004-06-10 10:50:08 +000085 if( sqlite3CheckCollSeq(pParse, pColl) ){
86 pColl = 0;
87 }
88 return pColl;
danielk19770202b292004-06-09 09:55:16 +000089}
90
91/*
drh626a8792005-01-17 22:08:19 +000092** pExpr is an operand of a comparison operator. aff2 is the
93** type affinity of the other operand. This routine returns the
drh53db1452004-05-20 13:54:53 +000094** type affinity that should be used for the comparison operator.
95*/
danielk1977e014a832004-05-17 10:48:57 +000096char sqlite3CompareAffinity(Expr *pExpr, char aff2){
danielk1977bf3b7212004-05-18 10:06:24 +000097 char aff1 = sqlite3ExprAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +000098 if( aff1 && aff2 ){
drh8df447f2005-11-01 15:48:24 +000099 /* Both sides of the comparison are columns. If one has numeric
100 ** affinity, use that. Otherwise use no affinity.
danielk1977e014a832004-05-17 10:48:57 +0000101 */
drh8a512562005-11-14 22:29:05 +0000102 if( sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2) ){
danielk1977e014a832004-05-17 10:48:57 +0000103 return SQLITE_AFF_NUMERIC;
104 }else{
105 return SQLITE_AFF_NONE;
106 }
107 }else if( !aff1 && !aff2 ){
drh5f6a87b2004-07-19 00:39:45 +0000108 /* Neither side of the comparison is a column. Compare the
109 ** results directly.
danielk1977e014a832004-05-17 10:48:57 +0000110 */
drh5f6a87b2004-07-19 00:39:45 +0000111 return SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +0000112 }else{
113 /* One side is a column, the other is not. Use the columns affinity. */
drhfe05af82005-07-21 03:14:59 +0000114 assert( aff1==0 || aff2==0 );
danielk1977e014a832004-05-17 10:48:57 +0000115 return (aff1 + aff2);
116 }
117}
118
drh53db1452004-05-20 13:54:53 +0000119/*
120** pExpr is a comparison operator. Return the type affinity that should
121** be applied to both operands prior to doing the comparison.
122*/
danielk1977e014a832004-05-17 10:48:57 +0000123static char comparisonAffinity(Expr *pExpr){
124 char aff;
125 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
126 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
127 pExpr->op==TK_NE );
128 assert( pExpr->pLeft );
danielk1977bf3b7212004-05-18 10:06:24 +0000129 aff = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +0000130 if( pExpr->pRight ){
131 aff = sqlite3CompareAffinity(pExpr->pRight, aff);
132 }
133 else if( pExpr->pSelect ){
134 aff = sqlite3CompareAffinity(pExpr->pSelect->pEList->a[0].pExpr, aff);
135 }
136 else if( !aff ){
drhde087bd2007-02-23 03:00:44 +0000137 aff = SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +0000138 }
139 return aff;
140}
141
142/*
143** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
144** idx_affinity is the affinity of an indexed column. Return true
145** if the index with affinity idx_affinity may be used to implement
146** the comparison in pExpr.
147*/
148int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){
149 char aff = comparisonAffinity(pExpr);
drh8a512562005-11-14 22:29:05 +0000150 switch( aff ){
151 case SQLITE_AFF_NONE:
152 return 1;
153 case SQLITE_AFF_TEXT:
154 return idx_affinity==SQLITE_AFF_TEXT;
155 default:
156 return sqlite3IsNumericAffinity(idx_affinity);
157 }
danielk1977e014a832004-05-17 10:48:57 +0000158}
159
danielk1977a37cdde2004-05-16 11:15:36 +0000160/*
drh35573352008-01-08 23:54:25 +0000161** Return the P5 value that should be used for a binary comparison
danielk1977a37cdde2004-05-16 11:15:36 +0000162** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
danielk1977a37cdde2004-05-16 11:15:36 +0000163*/
drh35573352008-01-08 23:54:25 +0000164static u8 binaryCompareP5(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){
165 u8 aff = (char)sqlite3ExprAffinity(pExpr2);
166 aff = sqlite3CompareAffinity(pExpr1, aff) | jumpIfNull;
167 return aff;
danielk1977a37cdde2004-05-16 11:15:36 +0000168}
169
drha2e00042002-01-22 03:13:42 +0000170/*
danielk19770202b292004-06-09 09:55:16 +0000171** Return a pointer to the collation sequence that should be used by
172** a binary comparison operator comparing pLeft and pRight.
173**
174** If the left hand expression has a collating sequence type, then it is
175** used. Otherwise the collation sequence for the right hand expression
176** is used, or the default (BINARY) if neither expression has a collating
177** type.
danielk1977bcbb04e2007-05-29 12:11:29 +0000178**
179** Argument pRight (but not pLeft) may be a null pointer. In this case,
180** it is not considered.
danielk19770202b292004-06-09 09:55:16 +0000181*/
drh0a0e1312007-08-07 17:04:59 +0000182CollSeq *sqlite3BinaryCompareCollSeq(
danielk1977bcbb04e2007-05-29 12:11:29 +0000183 Parse *pParse,
184 Expr *pLeft,
185 Expr *pRight
186){
drhec41dda2007-02-07 13:09:45 +0000187 CollSeq *pColl;
188 assert( pLeft );
drhec41dda2007-02-07 13:09:45 +0000189 if( pLeft->flags & EP_ExpCollate ){
190 assert( pLeft->pColl );
191 pColl = pLeft->pColl;
danielk1977bcbb04e2007-05-29 12:11:29 +0000192 }else if( pRight && pRight->flags & EP_ExpCollate ){
drhec41dda2007-02-07 13:09:45 +0000193 assert( pRight->pColl );
194 pColl = pRight->pColl;
195 }else{
196 pColl = sqlite3ExprCollSeq(pParse, pLeft);
197 if( !pColl ){
198 pColl = sqlite3ExprCollSeq(pParse, pRight);
199 }
danielk19770202b292004-06-09 09:55:16 +0000200 }
201 return pColl;
202}
203
204/*
drhda250ea2008-04-01 05:07:14 +0000205** Generate the operands for a comparison operation. Before
206** generating the code for each operand, set the EP_AnyAff
207** flag on the expression so that it will be able to used a
208** cached column value that has previously undergone an
209** affinity change.
210*/
211static void codeCompareOperands(
212 Parse *pParse, /* Parsing and code generating context */
213 Expr *pLeft, /* The left operand */
214 int *pRegLeft, /* Register where left operand is stored */
215 int *pFreeLeft, /* Free this register when done */
216 Expr *pRight, /* The right operand */
217 int *pRegRight, /* Register where right operand is stored */
218 int *pFreeRight /* Write temp register for right operand there */
219){
220 while( pLeft->op==TK_UPLUS ) pLeft = pLeft->pLeft;
221 pLeft->flags |= EP_AnyAff;
222 *pRegLeft = sqlite3ExprCodeTemp(pParse, pLeft, pFreeLeft);
223 while( pRight->op==TK_UPLUS ) pRight = pRight->pLeft;
224 pRight->flags |= EP_AnyAff;
225 *pRegRight = sqlite3ExprCodeTemp(pParse, pRight, pFreeRight);
226}
227
228/*
drhbe5c89a2004-07-26 00:31:09 +0000229** Generate code for a comparison operator.
230*/
231static int codeCompare(
232 Parse *pParse, /* The parsing (and code generating) context */
233 Expr *pLeft, /* The left operand */
234 Expr *pRight, /* The right operand */
235 int opcode, /* The comparison opcode */
drh35573352008-01-08 23:54:25 +0000236 int in1, int in2, /* Register holding operands */
drhbe5c89a2004-07-26 00:31:09 +0000237 int dest, /* Jump here if true. */
238 int jumpIfNull /* If true, jump if either operand is NULL */
239){
drh35573352008-01-08 23:54:25 +0000240 int p5;
241 int addr;
242 CollSeq *p4;
243
244 p4 = sqlite3BinaryCompareCollSeq(pParse, pLeft, pRight);
245 p5 = binaryCompareP5(pLeft, pRight, jumpIfNull);
246 addr = sqlite3VdbeAddOp4(pParse->pVdbe, opcode, in2, dest, in1,
247 (void*)p4, P4_COLLSEQ);
248 sqlite3VdbeChangeP5(pParse->pVdbe, p5);
drhe49b1462008-07-09 01:39:44 +0000249 if( (p5 & SQLITE_AFF_MASK)!=SQLITE_AFF_NONE ){
drhda250ea2008-04-01 05:07:14 +0000250 sqlite3ExprCacheAffinityChange(pParse, in1, 1);
251 sqlite3ExprCacheAffinityChange(pParse, in2, 1);
drh2f7794c2008-04-01 03:27:39 +0000252 }
drh35573352008-01-08 23:54:25 +0000253 return addr;
drhbe5c89a2004-07-26 00:31:09 +0000254}
255
danielk19774b5255a2008-06-05 16:47:39 +0000256#if SQLITE_MAX_EXPR_DEPTH>0
257/*
258** Check that argument nHeight is less than or equal to the maximum
259** expression depth allowed. If it is not, leave an error message in
260** pParse.
261*/
262static int checkExprHeight(Parse *pParse, int nHeight){
263 int rc = SQLITE_OK;
264 int mxHeight = pParse->db->aLimit[SQLITE_LIMIT_EXPR_DEPTH];
265 if( nHeight>mxHeight ){
266 sqlite3ErrorMsg(pParse,
267 "Expression tree is too large (maximum depth %d)", mxHeight
268 );
269 rc = SQLITE_ERROR;
270 }
271 return rc;
272}
273
274/* The following three functions, heightOfExpr(), heightOfExprList()
275** and heightOfSelect(), are used to determine the maximum height
276** of any expression tree referenced by the structure passed as the
277** first argument.
278**
279** If this maximum height is greater than the current value pointed
280** to by pnHeight, the second parameter, then set *pnHeight to that
281** value.
282*/
283static void heightOfExpr(Expr *p, int *pnHeight){
284 if( p ){
285 if( p->nHeight>*pnHeight ){
286 *pnHeight = p->nHeight;
287 }
288 }
289}
290static void heightOfExprList(ExprList *p, int *pnHeight){
291 if( p ){
292 int i;
293 for(i=0; i<p->nExpr; i++){
294 heightOfExpr(p->a[i].pExpr, pnHeight);
295 }
296 }
297}
298static void heightOfSelect(Select *p, int *pnHeight){
299 if( p ){
300 heightOfExpr(p->pWhere, pnHeight);
301 heightOfExpr(p->pHaving, pnHeight);
302 heightOfExpr(p->pLimit, pnHeight);
303 heightOfExpr(p->pOffset, pnHeight);
304 heightOfExprList(p->pEList, pnHeight);
305 heightOfExprList(p->pGroupBy, pnHeight);
306 heightOfExprList(p->pOrderBy, pnHeight);
307 heightOfSelect(p->pPrior, pnHeight);
308 }
309}
310
311/*
312** Set the Expr.nHeight variable in the structure passed as an
313** argument. An expression with no children, Expr.pList or
314** Expr.pSelect member has a height of 1. Any other expression
315** has a height equal to the maximum height of any other
316** referenced Expr plus one.
317*/
318static void exprSetHeight(Expr *p){
319 int nHeight = 0;
320 heightOfExpr(p->pLeft, &nHeight);
321 heightOfExpr(p->pRight, &nHeight);
322 heightOfExprList(p->pList, &nHeight);
323 heightOfSelect(p->pSelect, &nHeight);
324 p->nHeight = nHeight + 1;
325}
326
327/*
328** Set the Expr.nHeight variable using the exprSetHeight() function. If
329** the height is greater than the maximum allowed expression depth,
330** leave an error in pParse.
331*/
332void sqlite3ExprSetHeight(Parse *pParse, Expr *p){
333 exprSetHeight(p);
334 checkExprHeight(pParse, p->nHeight);
335}
336
337/*
338** Return the maximum height of any expression tree referenced
339** by the select statement passed as an argument.
340*/
341int sqlite3SelectExprHeight(Select *p){
342 int nHeight = 0;
343 heightOfSelect(p, &nHeight);
344 return nHeight;
345}
346#else
347 #define checkExprHeight(x,y)
348 #define exprSetHeight(y)
349#endif /* SQLITE_MAX_EXPR_DEPTH>0 */
350
drhbe5c89a2004-07-26 00:31:09 +0000351/*
drha76b5df2002-02-23 02:32:10 +0000352** Construct a new expression node and return a pointer to it. Memory
drh17435752007-08-16 04:30:38 +0000353** for this node is obtained from sqlite3_malloc(). The calling function
drha76b5df2002-02-23 02:32:10 +0000354** is responsible for making sure the node eventually gets freed.
355*/
drh17435752007-08-16 04:30:38 +0000356Expr *sqlite3Expr(
danielk1977a1644fd2007-08-29 12:31:25 +0000357 sqlite3 *db, /* Handle for sqlite3DbMallocZero() (may be null) */
drh17435752007-08-16 04:30:38 +0000358 int op, /* Expression opcode */
359 Expr *pLeft, /* Left operand */
360 Expr *pRight, /* Right operand */
361 const Token *pToken /* Argument token */
362){
drha76b5df2002-02-23 02:32:10 +0000363 Expr *pNew;
drh26e4a8b2008-05-01 17:16:52 +0000364 pNew = sqlite3DbMallocZero(db, sizeof(Expr));
drha76b5df2002-02-23 02:32:10 +0000365 if( pNew==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000366 /* When malloc fails, delete pLeft and pRight. Expressions passed to
367 ** this function must always be allocated with sqlite3Expr() for this
368 ** reason.
369 */
370 sqlite3ExprDelete(pLeft);
371 sqlite3ExprDelete(pRight);
drha76b5df2002-02-23 02:32:10 +0000372 return 0;
373 }
374 pNew->op = op;
375 pNew->pLeft = pLeft;
376 pNew->pRight = pRight;
danielk1977a58fdfb2005-02-08 07:50:40 +0000377 pNew->iAgg = -1;
drhe49b1462008-07-09 01:39:44 +0000378 pNew->span.z = (u8*)"";
drha76b5df2002-02-23 02:32:10 +0000379 if( pToken ){
drh4b59ab52002-08-24 18:24:51 +0000380 assert( pToken->dyn==0 );
drh145716b2004-09-24 12:24:06 +0000381 pNew->span = pNew->token = *pToken;
drha34001c2007-02-02 12:44:37 +0000382 }else if( pLeft ){
383 if( pRight ){
drhe49b1462008-07-09 01:39:44 +0000384 if( pRight->span.dyn==0 && pLeft->span.dyn==0 ){
385 sqlite3ExprSpan(pNew, &pLeft->span, &pRight->span);
386 }
drh5ffb3ac2007-04-18 17:07:57 +0000387 if( pRight->flags & EP_ExpCollate ){
drha34001c2007-02-02 12:44:37 +0000388 pNew->flags |= EP_ExpCollate;
389 pNew->pColl = pRight->pColl;
390 }
391 }
drh5ffb3ac2007-04-18 17:07:57 +0000392 if( pLeft->flags & EP_ExpCollate ){
drha34001c2007-02-02 12:44:37 +0000393 pNew->flags |= EP_ExpCollate;
394 pNew->pColl = pLeft->pColl;
395 }
drha76b5df2002-02-23 02:32:10 +0000396 }
danielk1977fc976062007-05-10 10:46:56 +0000397
danielk19774b5255a2008-06-05 16:47:39 +0000398 exprSetHeight(pNew);
drha76b5df2002-02-23 02:32:10 +0000399 return pNew;
400}
401
402/*
drh17435752007-08-16 04:30:38 +0000403** Works like sqlite3Expr() except that it takes an extra Parse*
404** argument and notifies the associated connection object if malloc fails.
drh206f3d92006-07-11 13:15:08 +0000405*/
drh17435752007-08-16 04:30:38 +0000406Expr *sqlite3PExpr(
407 Parse *pParse, /* Parsing context */
408 int op, /* Expression opcode */
409 Expr *pLeft, /* Left operand */
410 Expr *pRight, /* Right operand */
411 const Token *pToken /* Argument token */
412){
danielk19774b5255a2008-06-05 16:47:39 +0000413 Expr *p = sqlite3Expr(pParse->db, op, pLeft, pRight, pToken);
414 if( p ){
415 checkExprHeight(pParse, p->nHeight);
416 }
417 return p;
drh206f3d92006-07-11 13:15:08 +0000418}
419
420/*
drh4e0cff62004-11-05 05:10:28 +0000421** When doing a nested parse, you can include terms in an expression
drhb7654112008-01-12 12:48:07 +0000422** that look like this: #1 #2 ... These terms refer to registers
423** in the virtual machine. #N is the N-th register.
drh4e0cff62004-11-05 05:10:28 +0000424**
425** This routine is called by the parser to deal with on of those terms.
426** It immediately generates code to store the value in a memory location.
427** The returns an expression that will code to extract the value from
428** that memory location as needed.
429*/
430Expr *sqlite3RegisterExpr(Parse *pParse, Token *pToken){
431 Vdbe *v = pParse->pVdbe;
432 Expr *p;
drh4e0cff62004-11-05 05:10:28 +0000433 if( pParse->nested==0 ){
434 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", pToken);
danielk1977a1644fd2007-08-29 12:31:25 +0000435 return sqlite3PExpr(pParse, TK_NULL, 0, 0, 0);
drh4e0cff62004-11-05 05:10:28 +0000436 }
drhbb7ac002005-08-12 22:58:53 +0000437 if( v==0 ) return 0;
danielk1977a1644fd2007-08-29 12:31:25 +0000438 p = sqlite3PExpr(pParse, TK_REGISTER, 0, 0, pToken);
drh73c42a12004-11-20 18:13:10 +0000439 if( p==0 ){
440 return 0; /* Malloc failed */
441 }
drhb7654112008-01-12 12:48:07 +0000442 p->iTable = atoi((char*)&pToken->z[1]);
drh4e0cff62004-11-05 05:10:28 +0000443 return p;
444}
445
446/*
drh91bb0ee2004-09-01 03:06:34 +0000447** Join two expressions using an AND operator. If either expression is
448** NULL, then just return the other expression.
449*/
danielk19771e536952007-08-16 10:09:01 +0000450Expr *sqlite3ExprAnd(sqlite3 *db, Expr *pLeft, Expr *pRight){
drh91bb0ee2004-09-01 03:06:34 +0000451 if( pLeft==0 ){
452 return pRight;
453 }else if( pRight==0 ){
454 return pLeft;
455 }else{
danielk1977880c15b2007-09-01 18:24:55 +0000456 return sqlite3Expr(db, TK_AND, pLeft, pRight, 0);
drh91bb0ee2004-09-01 03:06:34 +0000457 }
458}
459
460/*
drh6977fea2002-10-22 23:38:04 +0000461** Set the Expr.span field of the given expression to span all
drhe49b1462008-07-09 01:39:44 +0000462** text between the two given tokens. Both tokens must be pointing
463** at the same string.
drha76b5df2002-02-23 02:32:10 +0000464*/
danielk19774adee202004-05-08 08:23:19 +0000465void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
drh4efc4752004-01-16 15:55:37 +0000466 assert( pRight!=0 );
467 assert( pLeft!=0 );
drhe54a62a2008-07-18 17:03:52 +0000468 if( pExpr ){
drhe49b1462008-07-09 01:39:44 +0000469 pExpr->span.z = pLeft->z;
470 pExpr->span.n = pRight->n + (pRight->z - pLeft->z);
drha76b5df2002-02-23 02:32:10 +0000471 }
472}
473
474/*
475** Construct a new expression node for a function with multiple
476** arguments.
477*/
drh17435752007-08-16 04:30:38 +0000478Expr *sqlite3ExprFunction(Parse *pParse, ExprList *pList, Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000479 Expr *pNew;
danielk19774b202ae2006-01-23 05:50:58 +0000480 assert( pToken );
drh17435752007-08-16 04:30:38 +0000481 pNew = sqlite3DbMallocZero(pParse->db, sizeof(Expr) );
drha76b5df2002-02-23 02:32:10 +0000482 if( pNew==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000483 sqlite3ExprListDelete(pList); /* Avoid leaking memory when malloc fails */
drha76b5df2002-02-23 02:32:10 +0000484 return 0;
485 }
486 pNew->op = TK_FUNCTION;
487 pNew->pList = pList;
danielk19774b202ae2006-01-23 05:50:58 +0000488 assert( pToken->dyn==0 );
489 pNew->token = *pToken;
drh6977fea2002-10-22 23:38:04 +0000490 pNew->span = pNew->token;
danielk1977fc976062007-05-10 10:46:56 +0000491
danielk19774b5255a2008-06-05 16:47:39 +0000492 sqlite3ExprSetHeight(pParse, pNew);
drha76b5df2002-02-23 02:32:10 +0000493 return pNew;
494}
495
496/*
drhfa6bc002004-09-07 16:19:52 +0000497** Assign a variable number to an expression that encodes a wildcard
498** in the original SQL statement.
499**
500** Wildcards consisting of a single "?" are assigned the next sequential
501** variable number.
502**
503** Wildcards of the form "?nnn" are assigned the number "nnn". We make
504** sure "nnn" is not too be to avoid a denial of service attack when
505** the SQL statement comes from an external source.
506**
507** Wildcards of the form ":aaa" or "$aaa" are assigned the same number
508** as the previous instance of the same wildcard. Or if this is the first
509** instance of the wildcard, the next sequenial variable number is
510** assigned.
511*/
512void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){
513 Token *pToken;
drh17435752007-08-16 04:30:38 +0000514 sqlite3 *db = pParse->db;
515
drhfa6bc002004-09-07 16:19:52 +0000516 if( pExpr==0 ) return;
517 pToken = &pExpr->token;
518 assert( pToken->n>=1 );
519 assert( pToken->z!=0 );
520 assert( pToken->z[0]!=0 );
521 if( pToken->n==1 ){
522 /* Wildcard of the form "?". Assign the next variable number */
523 pExpr->iTable = ++pParse->nVar;
524 }else if( pToken->z[0]=='?' ){
525 /* Wildcard of the form "?nnn". Convert "nnn" to an integer and
526 ** use it as the variable number */
527 int i;
drh2646da72005-12-09 20:02:05 +0000528 pExpr->iTable = i = atoi((char*)&pToken->z[1]);
drhc5499be2008-04-01 15:06:33 +0000529 testcase( i==0 );
530 testcase( i==1 );
531 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 );
532 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] );
drhbb4957f2008-03-20 14:03:29 +0000533 if( i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
drhfa6bc002004-09-07 16:19:52 +0000534 sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
drhbb4957f2008-03-20 14:03:29 +0000535 db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]);
drhfa6bc002004-09-07 16:19:52 +0000536 }
537 if( i>pParse->nVar ){
538 pParse->nVar = i;
539 }
540 }else{
541 /* Wildcards of the form ":aaa" or "$aaa". Reuse the same variable
542 ** number as the prior appearance of the same name, or if the name
543 ** has never appeared before, reuse the same variable number
544 */
545 int i, n;
546 n = pToken->n;
547 for(i=0; i<pParse->nVarExpr; i++){
548 Expr *pE;
549 if( (pE = pParse->apVarExpr[i])!=0
550 && pE->token.n==n
551 && memcmp(pE->token.z, pToken->z, n)==0 ){
552 pExpr->iTable = pE->iTable;
553 break;
554 }
555 }
556 if( i>=pParse->nVarExpr ){
557 pExpr->iTable = ++pParse->nVar;
558 if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){
559 pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10;
drh17435752007-08-16 04:30:38 +0000560 pParse->apVarExpr =
561 sqlite3DbReallocOrFree(
562 db,
563 pParse->apVarExpr,
564 pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0])
565 );
drhfa6bc002004-09-07 16:19:52 +0000566 }
drh17435752007-08-16 04:30:38 +0000567 if( !db->mallocFailed ){
drhfa6bc002004-09-07 16:19:52 +0000568 assert( pParse->apVarExpr!=0 );
569 pParse->apVarExpr[pParse->nVarExpr++] = pExpr;
570 }
571 }
572 }
drhbb4957f2008-03-20 14:03:29 +0000573 if( !pParse->nErr && pParse->nVar>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
danielk1977832b2662007-05-09 11:37:22 +0000574 sqlite3ErrorMsg(pParse, "too many SQL variables");
575 }
drhfa6bc002004-09-07 16:19:52 +0000576}
577
578/*
drha2e00042002-01-22 03:13:42 +0000579** Recursively delete an expression tree.
580*/
danielk19774adee202004-05-08 08:23:19 +0000581void sqlite3ExprDelete(Expr *p){
drha2e00042002-01-22 03:13:42 +0000582 if( p==0 ) return;
drh17435752007-08-16 04:30:38 +0000583 if( p->span.dyn ) sqlite3_free((char*)p->span.z);
584 if( p->token.dyn ) sqlite3_free((char*)p->token.z);
danielk19774adee202004-05-08 08:23:19 +0000585 sqlite3ExprDelete(p->pLeft);
586 sqlite3ExprDelete(p->pRight);
587 sqlite3ExprListDelete(p->pList);
588 sqlite3SelectDelete(p->pSelect);
drh17435752007-08-16 04:30:38 +0000589 sqlite3_free(p);
drha2e00042002-01-22 03:13:42 +0000590}
591
drhd2687b72005-08-12 22:56:09 +0000592/*
593** The Expr.token field might be a string literal that is quoted.
594** If so, remove the quotation marks.
595*/
drh17435752007-08-16 04:30:38 +0000596void sqlite3DequoteExpr(sqlite3 *db, Expr *p){
drhd2687b72005-08-12 22:56:09 +0000597 if( ExprHasAnyProperty(p, EP_Dequoted) ){
598 return;
599 }
600 ExprSetProperty(p, EP_Dequoted);
601 if( p->token.dyn==0 ){
drh17435752007-08-16 04:30:38 +0000602 sqlite3TokenCopy(db, &p->token, &p->token);
drhd2687b72005-08-12 22:56:09 +0000603 }
604 sqlite3Dequote((char*)p->token.z);
605}
606
drha76b5df2002-02-23 02:32:10 +0000607
608/*
drhff78bd22002-02-27 01:47:11 +0000609** The following group of routines make deep copies of expressions,
610** expression lists, ID lists, and select statements. The copies can
611** be deleted (by being passed to their respective ...Delete() routines)
612** without effecting the originals.
613**
danielk19774adee202004-05-08 08:23:19 +0000614** The expression list, ID, and source lists return by sqlite3ExprListDup(),
615** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
drhad3cab52002-05-24 02:04:32 +0000616** by subsequent calls to sqlite*ListAppend() routines.
drhff78bd22002-02-27 01:47:11 +0000617**
drhad3cab52002-05-24 02:04:32 +0000618** Any tables that the SrcList might point to are not duplicated.
drhff78bd22002-02-27 01:47:11 +0000619*/
danielk19771e536952007-08-16 10:09:01 +0000620Expr *sqlite3ExprDup(sqlite3 *db, Expr *p){
drhff78bd22002-02-27 01:47:11 +0000621 Expr *pNew;
622 if( p==0 ) return 0;
drh17435752007-08-16 04:30:38 +0000623 pNew = sqlite3DbMallocRaw(db, sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000624 if( pNew==0 ) return 0;
drh3b167c72002-06-28 12:18:47 +0000625 memcpy(pNew, p, sizeof(*pNew));
drh6977fea2002-10-22 23:38:04 +0000626 if( p->token.z!=0 ){
drh17435752007-08-16 04:30:38 +0000627 pNew->token.z = (u8*)sqlite3DbStrNDup(db, (char*)p->token.z, p->token.n);
drh4b59ab52002-08-24 18:24:51 +0000628 pNew->token.dyn = 1;
629 }else{
drh4efc4752004-01-16 15:55:37 +0000630 assert( pNew->token.z==0 );
drh4b59ab52002-08-24 18:24:51 +0000631 }
drh6977fea2002-10-22 23:38:04 +0000632 pNew->span.z = 0;
drh17435752007-08-16 04:30:38 +0000633 pNew->pLeft = sqlite3ExprDup(db, p->pLeft);
634 pNew->pRight = sqlite3ExprDup(db, p->pRight);
635 pNew->pList = sqlite3ExprListDup(db, p->pList);
636 pNew->pSelect = sqlite3SelectDup(db, p->pSelect);
drhff78bd22002-02-27 01:47:11 +0000637 return pNew;
638}
drh17435752007-08-16 04:30:38 +0000639void sqlite3TokenCopy(sqlite3 *db, Token *pTo, Token *pFrom){
640 if( pTo->dyn ) sqlite3_free((char*)pTo->z);
drh4b59ab52002-08-24 18:24:51 +0000641 if( pFrom->z ){
642 pTo->n = pFrom->n;
drh17435752007-08-16 04:30:38 +0000643 pTo->z = (u8*)sqlite3DbStrNDup(db, (char*)pFrom->z, pFrom->n);
drh4b59ab52002-08-24 18:24:51 +0000644 pTo->dyn = 1;
645 }else{
drh4b59ab52002-08-24 18:24:51 +0000646 pTo->z = 0;
drh4b59ab52002-08-24 18:24:51 +0000647 }
648}
drh17435752007-08-16 04:30:38 +0000649ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p){
drhff78bd22002-02-27 01:47:11 +0000650 ExprList *pNew;
drh145716b2004-09-24 12:24:06 +0000651 struct ExprList_item *pItem, *pOldItem;
drhff78bd22002-02-27 01:47:11 +0000652 int i;
653 if( p==0 ) return 0;
drh17435752007-08-16 04:30:38 +0000654 pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +0000655 if( pNew==0 ) return 0;
danielk197731dad9d2007-08-16 11:36:15 +0000656 pNew->iECursor = 0;
drh4305d102003-07-30 12:34:12 +0000657 pNew->nExpr = pNew->nAlloc = p->nExpr;
drh17435752007-08-16 04:30:38 +0000658 pNew->a = pItem = sqlite3DbMallocRaw(db, p->nExpr*sizeof(p->a[0]) );
danielk1977e0048402004-06-15 16:51:01 +0000659 if( pItem==0 ){
drh17435752007-08-16 04:30:38 +0000660 sqlite3_free(pNew);
danielk1977e0048402004-06-15 16:51:01 +0000661 return 0;
662 }
drh145716b2004-09-24 12:24:06 +0000663 pOldItem = p->a;
664 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
drh4b59ab52002-08-24 18:24:51 +0000665 Expr *pNewExpr, *pOldExpr;
drh17435752007-08-16 04:30:38 +0000666 pItem->pExpr = pNewExpr = sqlite3ExprDup(db, pOldExpr = pOldItem->pExpr);
drh6977fea2002-10-22 23:38:04 +0000667 if( pOldExpr->span.z!=0 && pNewExpr ){
668 /* Always make a copy of the span for top-level expressions in the
drh4b59ab52002-08-24 18:24:51 +0000669 ** expression list. The logic in SELECT processing that determines
670 ** the names of columns in the result set needs this information */
drh17435752007-08-16 04:30:38 +0000671 sqlite3TokenCopy(db, &pNewExpr->span, &pOldExpr->span);
drh4b59ab52002-08-24 18:24:51 +0000672 }
drh1f3e9052002-10-31 00:09:39 +0000673 assert( pNewExpr==0 || pNewExpr->span.z!=0
drh6f7adc82006-01-11 21:41:20 +0000674 || pOldExpr->span.z==0
drh17435752007-08-16 04:30:38 +0000675 || db->mallocFailed );
676 pItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
drh145716b2004-09-24 12:24:06 +0000677 pItem->sortOrder = pOldItem->sortOrder;
678 pItem->isAgg = pOldItem->isAgg;
drh3e7bc9c2004-02-21 19:17:17 +0000679 pItem->done = 0;
drhff78bd22002-02-27 01:47:11 +0000680 }
681 return pNew;
682}
danielk197793758c82005-01-21 08:13:14 +0000683
684/*
685** If cursors, triggers, views and subqueries are all omitted from
686** the build, then none of the following routines, except for
687** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes
688** called with a NULL argument.
689*/
danielk19776a67fe82005-02-04 04:07:16 +0000690#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \
691 || !defined(SQLITE_OMIT_SUBQUERY)
drh17435752007-08-16 04:30:38 +0000692SrcList *sqlite3SrcListDup(sqlite3 *db, SrcList *p){
drhad3cab52002-05-24 02:04:32 +0000693 SrcList *pNew;
694 int i;
drh113088e2003-03-20 01:16:58 +0000695 int nByte;
drhad3cab52002-05-24 02:04:32 +0000696 if( p==0 ) return 0;
drh113088e2003-03-20 01:16:58 +0000697 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
drh17435752007-08-16 04:30:38 +0000698 pNew = sqlite3DbMallocRaw(db, nByte );
drhad3cab52002-05-24 02:04:32 +0000699 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000700 pNew->nSrc = pNew->nAlloc = p->nSrc;
drhad3cab52002-05-24 02:04:32 +0000701 for(i=0; i<p->nSrc; i++){
drh4efc4752004-01-16 15:55:37 +0000702 struct SrcList_item *pNewItem = &pNew->a[i];
703 struct SrcList_item *pOldItem = &p->a[i];
drhed8a3bb2005-06-06 21:19:56 +0000704 Table *pTab;
drh17435752007-08-16 04:30:38 +0000705 pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase);
706 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
707 pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias);
drh4efc4752004-01-16 15:55:37 +0000708 pNewItem->jointype = pOldItem->jointype;
709 pNewItem->iCursor = pOldItem->iCursor;
danielk19771787cca2006-02-10 07:07:14 +0000710 pNewItem->isPopulated = pOldItem->isPopulated;
drhed8a3bb2005-06-06 21:19:56 +0000711 pTab = pNewItem->pTab = pOldItem->pTab;
712 if( pTab ){
713 pTab->nRef++;
danielk1977a1cb1832005-02-12 08:59:55 +0000714 }
drh17435752007-08-16 04:30:38 +0000715 pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect);
716 pNewItem->pOn = sqlite3ExprDup(db, pOldItem->pOn);
717 pNewItem->pUsing = sqlite3IdListDup(db, pOldItem->pUsing);
danielk19776c18b6e2005-01-30 09:17:58 +0000718 pNewItem->colUsed = pOldItem->colUsed;
drhad3cab52002-05-24 02:04:32 +0000719 }
720 return pNew;
721}
drh17435752007-08-16 04:30:38 +0000722IdList *sqlite3IdListDup(sqlite3 *db, IdList *p){
drhff78bd22002-02-27 01:47:11 +0000723 IdList *pNew;
724 int i;
725 if( p==0 ) return 0;
drh17435752007-08-16 04:30:38 +0000726 pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +0000727 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +0000728 pNew->nId = pNew->nAlloc = p->nId;
drh17435752007-08-16 04:30:38 +0000729 pNew->a = sqlite3DbMallocRaw(db, p->nId*sizeof(p->a[0]) );
danielk1977d5d56522005-03-16 12:15:20 +0000730 if( pNew->a==0 ){
drh17435752007-08-16 04:30:38 +0000731 sqlite3_free(pNew);
danielk1977d5d56522005-03-16 12:15:20 +0000732 return 0;
733 }
drhff78bd22002-02-27 01:47:11 +0000734 for(i=0; i<p->nId; i++){
drh4efc4752004-01-16 15:55:37 +0000735 struct IdList_item *pNewItem = &pNew->a[i];
736 struct IdList_item *pOldItem = &p->a[i];
drh17435752007-08-16 04:30:38 +0000737 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
drh4efc4752004-01-16 15:55:37 +0000738 pNewItem->idx = pOldItem->idx;
drhff78bd22002-02-27 01:47:11 +0000739 }
740 return pNew;
741}
drh17435752007-08-16 04:30:38 +0000742Select *sqlite3SelectDup(sqlite3 *db, Select *p){
drhff78bd22002-02-27 01:47:11 +0000743 Select *pNew;
744 if( p==0 ) return 0;
drh17435752007-08-16 04:30:38 +0000745 pNew = sqlite3DbMallocRaw(db, sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +0000746 if( pNew==0 ) return 0;
747 pNew->isDistinct = p->isDistinct;
drh17435752007-08-16 04:30:38 +0000748 pNew->pEList = sqlite3ExprListDup(db, p->pEList);
749 pNew->pSrc = sqlite3SrcListDup(db, p->pSrc);
750 pNew->pWhere = sqlite3ExprDup(db, p->pWhere);
751 pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy);
752 pNew->pHaving = sqlite3ExprDup(db, p->pHaving);
753 pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy);
drhff78bd22002-02-27 01:47:11 +0000754 pNew->op = p->op;
drh17435752007-08-16 04:30:38 +0000755 pNew->pPrior = sqlite3SelectDup(db, p->pPrior);
756 pNew->pLimit = sqlite3ExprDup(db, p->pLimit);
757 pNew->pOffset = sqlite3ExprDup(db, p->pOffset);
drh92b01d52008-06-24 00:32:35 +0000758 pNew->iLimit = 0;
759 pNew->iOffset = 0;
danielk1977a1cb1832005-02-12 08:59:55 +0000760 pNew->isResolved = p->isResolved;
761 pNew->isAgg = p->isAgg;
drhb9bb7c12006-06-11 23:41:55 +0000762 pNew->usesEphm = 0;
drh8e647b82005-09-23 21:11:53 +0000763 pNew->disallowOrderBy = 0;
drh0342b1f2005-09-01 03:07:44 +0000764 pNew->pRightmost = 0;
drhb9bb7c12006-06-11 23:41:55 +0000765 pNew->addrOpenEphm[0] = -1;
766 pNew->addrOpenEphm[1] = -1;
767 pNew->addrOpenEphm[2] = -1;
drhff78bd22002-02-27 01:47:11 +0000768 return pNew;
769}
danielk197793758c82005-01-21 08:13:14 +0000770#else
drh17435752007-08-16 04:30:38 +0000771Select *sqlite3SelectDup(sqlite3 *db, Select *p){
danielk197793758c82005-01-21 08:13:14 +0000772 assert( p==0 );
773 return 0;
774}
775#endif
drhff78bd22002-02-27 01:47:11 +0000776
777
778/*
drha76b5df2002-02-23 02:32:10 +0000779** Add a new element to the end of an expression list. If pList is
780** initially NULL, then create a new expression list.
781*/
drh17435752007-08-16 04:30:38 +0000782ExprList *sqlite3ExprListAppend(
783 Parse *pParse, /* Parsing context */
784 ExprList *pList, /* List to which to append. Might be NULL */
785 Expr *pExpr, /* Expression to be appended */
786 Token *pName /* AS keyword for the expression */
787){
788 sqlite3 *db = pParse->db;
drha76b5df2002-02-23 02:32:10 +0000789 if( pList==0 ){
drh17435752007-08-16 04:30:38 +0000790 pList = sqlite3DbMallocZero(db, sizeof(ExprList) );
drha76b5df2002-02-23 02:32:10 +0000791 if( pList==0 ){
danielk1977d5d56522005-03-16 12:15:20 +0000792 goto no_mem;
drha76b5df2002-02-23 02:32:10 +0000793 }
drh4efc4752004-01-16 15:55:37 +0000794 assert( pList->nAlloc==0 );
drha76b5df2002-02-23 02:32:10 +0000795 }
drh4305d102003-07-30 12:34:12 +0000796 if( pList->nAlloc<=pList->nExpr ){
danielk1977d5d56522005-03-16 12:15:20 +0000797 struct ExprList_item *a;
798 int n = pList->nAlloc*2 + 4;
danielk197726783a52007-08-29 14:06:22 +0000799 a = sqlite3DbRealloc(db, pList->a, n*sizeof(pList->a[0]));
danielk1977d5d56522005-03-16 12:15:20 +0000800 if( a==0 ){
801 goto no_mem;
drha76b5df2002-02-23 02:32:10 +0000802 }
danielk1977d5d56522005-03-16 12:15:20 +0000803 pList->a = a;
804 pList->nAlloc = n;
drha76b5df2002-02-23 02:32:10 +0000805 }
drh4efc4752004-01-16 15:55:37 +0000806 assert( pList->a!=0 );
807 if( pExpr || pName ){
808 struct ExprList_item *pItem = &pList->a[pList->nExpr++];
809 memset(pItem, 0, sizeof(*pItem));
drh17435752007-08-16 04:30:38 +0000810 pItem->zName = sqlite3NameFromToken(db, pName);
danielk1977e94ddc92005-03-21 03:53:38 +0000811 pItem->pExpr = pExpr;
drha76b5df2002-02-23 02:32:10 +0000812 }
813 return pList;
danielk1977d5d56522005-03-16 12:15:20 +0000814
815no_mem:
816 /* Avoid leaking memory if malloc has failed. */
817 sqlite3ExprDelete(pExpr);
818 sqlite3ExprListDelete(pList);
819 return 0;
drha76b5df2002-02-23 02:32:10 +0000820}
821
822/*
danielk19777a15a4b2007-05-08 17:54:43 +0000823** If the expression list pEList contains more than iLimit elements,
824** leave an error message in pParse.
825*/
826void sqlite3ExprListCheckLength(
827 Parse *pParse,
828 ExprList *pEList,
danielk19777a15a4b2007-05-08 17:54:43 +0000829 const char *zObject
830){
drhb1a6c3c2008-03-20 16:30:17 +0000831 int mx = pParse->db->aLimit[SQLITE_LIMIT_COLUMN];
drhc5499be2008-04-01 15:06:33 +0000832 testcase( pEList && pEList->nExpr==mx );
833 testcase( pEList && pEList->nExpr==mx+1 );
drhb1a6c3c2008-03-20 16:30:17 +0000834 if( pEList && pEList->nExpr>mx ){
danielk19777a15a4b2007-05-08 17:54:43 +0000835 sqlite3ErrorMsg(pParse, "too many columns in %s", zObject);
836 }
837}
838
839/*
drha76b5df2002-02-23 02:32:10 +0000840** Delete an entire expression list.
841*/
danielk19774adee202004-05-08 08:23:19 +0000842void sqlite3ExprListDelete(ExprList *pList){
drha76b5df2002-02-23 02:32:10 +0000843 int i;
drhbe5c89a2004-07-26 00:31:09 +0000844 struct ExprList_item *pItem;
drha76b5df2002-02-23 02:32:10 +0000845 if( pList==0 ) return;
drh1bdd9b52004-04-23 17:04:44 +0000846 assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
847 assert( pList->nExpr<=pList->nAlloc );
drhbe5c89a2004-07-26 00:31:09 +0000848 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
849 sqlite3ExprDelete(pItem->pExpr);
drh17435752007-08-16 04:30:38 +0000850 sqlite3_free(pItem->zName);
drha76b5df2002-02-23 02:32:10 +0000851 }
drh17435752007-08-16 04:30:38 +0000852 sqlite3_free(pList->a);
853 sqlite3_free(pList);
drha76b5df2002-02-23 02:32:10 +0000854}
855
856/*
drh678ccce2008-03-31 18:19:54 +0000857** Walk an expression tree. Call xFunc for each node visited. xFunc
858** is called on the node before xFunc is called on the nodes children.
drh73b211a2005-01-18 04:00:42 +0000859**
drh626a8792005-01-17 22:08:19 +0000860** The return value from xFunc determines whether the tree walk continues.
861** 0 means continue walking the tree. 1 means do not walk children
862** of the current node but continue with siblings. 2 means abandon
863** the tree walk completely.
864**
865** The return value from this routine is 1 to abandon the tree walk
866** and 0 to continue.
drh87abf5c2005-08-25 12:45:04 +0000867**
868** NOTICE: This routine does *not* descend into subqueries.
drh626a8792005-01-17 22:08:19 +0000869*/
danielk1977a58fdfb2005-02-08 07:50:40 +0000870static int walkExprList(ExprList *, int (*)(void *, Expr*), void *);
drh626a8792005-01-17 22:08:19 +0000871static int walkExprTree(Expr *pExpr, int (*xFunc)(void*,Expr*), void *pArg){
drh626a8792005-01-17 22:08:19 +0000872 int rc;
873 if( pExpr==0 ) return 0;
874 rc = (*xFunc)(pArg, pExpr);
875 if( rc==0 ){
876 if( walkExprTree(pExpr->pLeft, xFunc, pArg) ) return 1;
877 if( walkExprTree(pExpr->pRight, xFunc, pArg) ) return 1;
danielk1977a58fdfb2005-02-08 07:50:40 +0000878 if( walkExprList(pExpr->pList, xFunc, pArg) ) return 1;
drh626a8792005-01-17 22:08:19 +0000879 }
880 return rc>1;
881}
882
883/*
danielk1977a58fdfb2005-02-08 07:50:40 +0000884** Call walkExprTree() for every expression in list p.
885*/
886static int walkExprList(ExprList *p, int (*xFunc)(void *, Expr*), void *pArg){
887 int i;
888 struct ExprList_item *pItem;
889 if( !p ) return 0;
890 for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
891 if( walkExprTree(pItem->pExpr, xFunc, pArg) ) return 1;
892 }
893 return 0;
894}
895
896/*
897** Call walkExprTree() for every expression in Select p, not including
898** expressions that are part of sub-selects in any FROM clause or the LIMIT
899** or OFFSET expressions..
900*/
901static int walkSelectExpr(Select *p, int (*xFunc)(void *, Expr*), void *pArg){
902 walkExprList(p->pEList, xFunc, pArg);
903 walkExprTree(p->pWhere, xFunc, pArg);
904 walkExprList(p->pGroupBy, xFunc, pArg);
905 walkExprTree(p->pHaving, xFunc, pArg);
906 walkExprList(p->pOrderBy, xFunc, pArg);
danielk197715d79822007-05-15 07:00:34 +0000907 if( p->pPrior ){
908 walkSelectExpr(p->pPrior, xFunc, pArg);
909 }
danielk1977a58fdfb2005-02-08 07:50:40 +0000910 return 0;
911}
912
913
914/*
drh626a8792005-01-17 22:08:19 +0000915** This routine is designed as an xFunc for walkExprTree().
916**
917** pArg is really a pointer to an integer. If we can tell by looking
drh73b211a2005-01-18 04:00:42 +0000918** at pExpr that the expression that contains pExpr is not a constant
919** expression, then set *pArg to 0 and return 2 to abandon the tree walk.
920** If pExpr does does not disqualify the expression from being a constant
921** then do nothing.
922**
923** After walking the whole tree, if no nodes are found that disqualify
924** the expression as constant, then we assume the whole expression
925** is constant. See sqlite3ExprIsConstant() for additional information.
drh626a8792005-01-17 22:08:19 +0000926*/
927static int exprNodeIsConstant(void *pArg, Expr *pExpr){
drh0a168372007-06-08 00:20:47 +0000928 int *pN = (int*)pArg;
929
930 /* If *pArg is 3 then any term of the expression that comes from
931 ** the ON or USING clauses of a join disqualifies the expression
932 ** from being considered constant. */
933 if( (*pN)==3 && ExprHasAnyProperty(pExpr, EP_FromJoin) ){
934 *pN = 0;
935 return 2;
936 }
937
drh626a8792005-01-17 22:08:19 +0000938 switch( pExpr->op ){
drheb55bd22005-06-30 17:04:21 +0000939 /* Consider functions to be constant if all their arguments are constant
940 ** and *pArg==2 */
941 case TK_FUNCTION:
drh0a168372007-06-08 00:20:47 +0000942 if( (*pN)==2 ) return 0;
drheb55bd22005-06-30 17:04:21 +0000943 /* Fall through */
drh626a8792005-01-17 22:08:19 +0000944 case TK_ID:
945 case TK_COLUMN:
946 case TK_DOT:
947 case TK_AGG_FUNCTION:
drh13449892005-09-07 21:22:45 +0000948 case TK_AGG_COLUMN:
drhfe2093d2005-01-20 22:48:47 +0000949#ifndef SQLITE_OMIT_SUBQUERY
950 case TK_SELECT:
951 case TK_EXISTS:
drhc5499be2008-04-01 15:06:33 +0000952 testcase( pExpr->op==TK_SELECT );
953 testcase( pExpr->op==TK_EXISTS );
drhfe2093d2005-01-20 22:48:47 +0000954#endif
drhc5499be2008-04-01 15:06:33 +0000955 testcase( pExpr->op==TK_ID );
956 testcase( pExpr->op==TK_COLUMN );
957 testcase( pExpr->op==TK_DOT );
958 testcase( pExpr->op==TK_AGG_FUNCTION );
959 testcase( pExpr->op==TK_AGG_COLUMN );
drh0a168372007-06-08 00:20:47 +0000960 *pN = 0;
drh626a8792005-01-17 22:08:19 +0000961 return 2;
drh87abf5c2005-08-25 12:45:04 +0000962 case TK_IN:
963 if( pExpr->pSelect ){
drh0a168372007-06-08 00:20:47 +0000964 *pN = 0;
drh87abf5c2005-08-25 12:45:04 +0000965 return 2;
966 }
drh626a8792005-01-17 22:08:19 +0000967 default:
968 return 0;
969 }
970}
971
972/*
drhfef52082000-06-06 01:50:43 +0000973** Walk an expression tree. Return 1 if the expression is constant
drheb55bd22005-06-30 17:04:21 +0000974** and 0 if it involves variables or function calls.
drh23989372002-05-21 13:43:04 +0000975**
976** For the purposes of this function, a double-quoted string (ex: "abc")
977** is considered a variable but a single-quoted string (ex: 'abc') is
978** a constant.
drhfef52082000-06-06 01:50:43 +0000979*/
danielk19774adee202004-05-08 08:23:19 +0000980int sqlite3ExprIsConstant(Expr *p){
drh626a8792005-01-17 22:08:19 +0000981 int isConst = 1;
982 walkExprTree(p, exprNodeIsConstant, &isConst);
983 return isConst;
drhfef52082000-06-06 01:50:43 +0000984}
985
986/*
drheb55bd22005-06-30 17:04:21 +0000987** Walk an expression tree. Return 1 if the expression is constant
drh0a168372007-06-08 00:20:47 +0000988** that does no originate from the ON or USING clauses of a join.
989** Return 0 if it involves variables or function calls or terms from
990** an ON or USING clause.
991*/
992int sqlite3ExprIsConstantNotJoin(Expr *p){
993 int isConst = 3;
994 walkExprTree(p, exprNodeIsConstant, &isConst);
995 return isConst!=0;
996}
997
998/*
999** Walk an expression tree. Return 1 if the expression is constant
drheb55bd22005-06-30 17:04:21 +00001000** or a function call with constant arguments. Return and 0 if there
1001** are any variables.
1002**
1003** For the purposes of this function, a double-quoted string (ex: "abc")
1004** is considered a variable but a single-quoted string (ex: 'abc') is
1005** a constant.
1006*/
1007int sqlite3ExprIsConstantOrFunction(Expr *p){
1008 int isConst = 2;
1009 walkExprTree(p, exprNodeIsConstant, &isConst);
1010 return isConst!=0;
1011}
1012
1013/*
drh73b211a2005-01-18 04:00:42 +00001014** If the expression p codes a constant integer that is small enough
drh202b2df2004-01-06 01:13:46 +00001015** to fit in a 32-bit integer, return 1 and put the value of the integer
1016** in *pValue. If the expression is not an integer or if it is too big
1017** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
drhe4de1fe2002-06-02 16:09:01 +00001018*/
danielk19774adee202004-05-08 08:23:19 +00001019int sqlite3ExprIsInteger(Expr *p, int *pValue){
drh92b01d52008-06-24 00:32:35 +00001020 int rc = 0;
1021 if( p->flags & EP_IntValue ){
1022 *pValue = p->iTable;
1023 return 1;
1024 }
drhe4de1fe2002-06-02 16:09:01 +00001025 switch( p->op ){
1026 case TK_INTEGER: {
drh92b01d52008-06-24 00:32:35 +00001027 rc = sqlite3GetInt32((char*)p->token.z, pValue);
drh202b2df2004-01-06 01:13:46 +00001028 break;
drhe4de1fe2002-06-02 16:09:01 +00001029 }
drh4b59ab52002-08-24 18:24:51 +00001030 case TK_UPLUS: {
drh92b01d52008-06-24 00:32:35 +00001031 rc = sqlite3ExprIsInteger(p->pLeft, pValue);
drhf6e369a2008-06-24 12:46:30 +00001032 break;
drh4b59ab52002-08-24 18:24:51 +00001033 }
drhe4de1fe2002-06-02 16:09:01 +00001034 case TK_UMINUS: {
1035 int v;
danielk19774adee202004-05-08 08:23:19 +00001036 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
drhe4de1fe2002-06-02 16:09:01 +00001037 *pValue = -v;
drh92b01d52008-06-24 00:32:35 +00001038 rc = 1;
drhe4de1fe2002-06-02 16:09:01 +00001039 }
1040 break;
1041 }
1042 default: break;
1043 }
drh92b01d52008-06-24 00:32:35 +00001044 if( rc ){
1045 p->op = TK_INTEGER;
1046 p->flags |= EP_IntValue;
1047 p->iTable = *pValue;
1048 }
1049 return rc;
drhe4de1fe2002-06-02 16:09:01 +00001050}
1051
1052/*
drhc4a3c772001-04-04 11:48:57 +00001053** Return TRUE if the given string is a row-id column name.
1054*/
danielk19774adee202004-05-08 08:23:19 +00001055int sqlite3IsRowid(const char *z){
1056 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
1057 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
1058 if( sqlite3StrICmp(z, "OID")==0 ) return 1;
drhc4a3c772001-04-04 11:48:57 +00001059 return 0;
1060}
1061
1062/*
drh8141f612004-01-25 22:44:58 +00001063** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
1064** that name in the set of source tables in pSrcList and make the pExpr
1065** expression node refer back to that source column. The following changes
1066** are made to pExpr:
1067**
1068** pExpr->iDb Set the index in db->aDb[] of the database holding
1069** the table.
1070** pExpr->iTable Set to the cursor number for the table obtained
1071** from pSrcList.
1072** pExpr->iColumn Set to the column number within the table.
drh8141f612004-01-25 22:44:58 +00001073** pExpr->op Set to TK_COLUMN.
1074** pExpr->pLeft Any expression this points to is deleted
1075** pExpr->pRight Any expression this points to is deleted.
1076**
1077** The pDbToken is the name of the database (the "X"). This value may be
1078** NULL meaning that name is of the form Y.Z or Z. Any available database
1079** can be used. The pTableToken is the name of the table (the "Y"). This
1080** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it
1081** means that the form of the name is Z and that columns from any table
1082** can be used.
1083**
1084** If the name cannot be resolved unambiguously, leave an error message
1085** in pParse and return non-zero. Return zero on success.
1086*/
1087static int lookupName(
drhffe07b22005-11-03 00:41:17 +00001088 Parse *pParse, /* The parsing context */
drh8141f612004-01-25 22:44:58 +00001089 Token *pDbToken, /* Name of the database containing table, or NULL */
1090 Token *pTableToken, /* Name of table containing column, or NULL */
1091 Token *pColumnToken, /* Name of the column. */
drh626a8792005-01-17 22:08:19 +00001092 NameContext *pNC, /* The name context used to resolve the name */
drh8141f612004-01-25 22:44:58 +00001093 Expr *pExpr /* Make this EXPR node point to the selected column */
1094){
1095 char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */
1096 char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */
1097 char *zCol = 0; /* Name of the column. The "Z" */
1098 int i, j; /* Loop counters */
1099 int cnt = 0; /* Number of matching column names */
1100 int cntTab = 0; /* Number of matching table names */
drh626a8792005-01-17 22:08:19 +00001101 sqlite3 *db = pParse->db; /* The database */
drh51669862004-12-18 18:40:26 +00001102 struct SrcList_item *pItem; /* Use for looping over pSrcList items */
1103 struct SrcList_item *pMatch = 0; /* The matching pSrcList item */
drh73b211a2005-01-18 04:00:42 +00001104 NameContext *pTopNC = pNC; /* First namecontext in the list */
drh728b5772007-09-18 15:55:07 +00001105 Schema *pSchema = 0; /* Schema of the expression */
drh8141f612004-01-25 22:44:58 +00001106
1107 assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
drh17435752007-08-16 04:30:38 +00001108 zDb = sqlite3NameFromToken(db, pDbToken);
1109 zTab = sqlite3NameFromToken(db, pTableToken);
1110 zCol = sqlite3NameFromToken(db, pColumnToken);
1111 if( db->mallocFailed ){
danielk1977d5d56522005-03-16 12:15:20 +00001112 goto lookupname_end;
drh8141f612004-01-25 22:44:58 +00001113 }
drh8141f612004-01-25 22:44:58 +00001114
1115 pExpr->iTable = -1;
drh626a8792005-01-17 22:08:19 +00001116 while( pNC && cnt==0 ){
drhffe07b22005-11-03 00:41:17 +00001117 ExprList *pEList;
drh626a8792005-01-17 22:08:19 +00001118 SrcList *pSrcList = pNC->pSrcList;
drh8141f612004-01-25 22:44:58 +00001119
danielk1977b3bce662005-01-29 08:32:43 +00001120 if( pSrcList ){
1121 for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
drh43617e92006-03-06 20:55:46 +00001122 Table *pTab;
1123 int iDb;
danielk1977b3bce662005-01-29 08:32:43 +00001124 Column *pCol;
1125
drh43617e92006-03-06 20:55:46 +00001126 pTab = pItem->pTab;
1127 assert( pTab!=0 );
1128 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
danielk1977b3bce662005-01-29 08:32:43 +00001129 assert( pTab->nCol>0 );
1130 if( zTab ){
1131 if( pItem->zAlias ){
1132 char *zTabName = pItem->zAlias;
1133 if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
1134 }else{
1135 char *zTabName = pTab->zName;
1136 if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
danielk1977da184232006-01-05 11:34:32 +00001137 if( zDb!=0 && sqlite3StrICmp(db->aDb[iDb].zName, zDb)!=0 ){
danielk1977b3bce662005-01-29 08:32:43 +00001138 continue;
1139 }
drh626a8792005-01-17 22:08:19 +00001140 }
drh8141f612004-01-25 22:44:58 +00001141 }
danielk1977b3bce662005-01-29 08:32:43 +00001142 if( 0==(cntTab++) ){
drh626a8792005-01-17 22:08:19 +00001143 pExpr->iTable = pItem->iCursor;
drh728b5772007-09-18 15:55:07 +00001144 pSchema = pTab->pSchema;
danielk1977b3bce662005-01-29 08:32:43 +00001145 pMatch = pItem;
1146 }
1147 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
1148 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
danielk1977b3bf5562006-01-10 17:58:23 +00001149 const char *zColl = pTab->aCol[j].zColl;
drh873fac02005-06-06 17:11:46 +00001150 IdList *pUsing;
danielk1977b3bce662005-01-29 08:32:43 +00001151 cnt++;
1152 pExpr->iTable = pItem->iCursor;
1153 pMatch = pItem;
drh728b5772007-09-18 15:55:07 +00001154 pSchema = pTab->pSchema;
danielk1977b3bce662005-01-29 08:32:43 +00001155 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
1156 pExpr->iColumn = j==pTab->iPKey ? -1 : j;
1157 pExpr->affinity = pTab->aCol[j].affinity;
drh8b4c40d2007-02-01 23:02:45 +00001158 if( (pExpr->flags & EP_ExpCollate)==0 ){
1159 pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0);
1160 }
drh61dfc312006-12-16 16:25:15 +00001161 if( i<pSrcList->nSrc-1 ){
1162 if( pItem[1].jointype & JT_NATURAL ){
1163 /* If this match occurred in the left table of a natural join,
1164 ** then skip the right table to avoid a duplicate match */
1165 pItem++;
1166 i++;
1167 }else if( (pUsing = pItem[1].pUsing)!=0 ){
1168 /* If this match occurs on a column that is in the USING clause
1169 ** of a join, skip the search of the right table of the join
1170 ** to avoid a duplicate match there. */
1171 int k;
1172 for(k=0; k<pUsing->nId; k++){
1173 if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ){
1174 pItem++;
1175 i++;
1176 break;
1177 }
drh873fac02005-06-06 17:11:46 +00001178 }
1179 }
1180 }
danielk1977b3bce662005-01-29 08:32:43 +00001181 break;
1182 }
drh8141f612004-01-25 22:44:58 +00001183 }
1184 }
1185 }
drh626a8792005-01-17 22:08:19 +00001186
1187#ifndef SQLITE_OMIT_TRIGGER
1188 /* If we have not already resolved the name, then maybe
1189 ** it is a new.* or old.* trigger argument reference
1190 */
1191 if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
1192 TriggerStack *pTriggerStack = pParse->trigStack;
1193 Table *pTab = 0;
danielk19778f2c54e2008-01-01 19:02:09 +00001194 u32 *piColMask;
drh626a8792005-01-17 22:08:19 +00001195 if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){
1196 pExpr->iTable = pTriggerStack->newIdx;
1197 assert( pTriggerStack->pTab );
1198 pTab = pTriggerStack->pTab;
danielk19778f2c54e2008-01-01 19:02:09 +00001199 piColMask = &(pTriggerStack->newColMask);
drh626a8792005-01-17 22:08:19 +00001200 }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab)==0 ){
1201 pExpr->iTable = pTriggerStack->oldIdx;
1202 assert( pTriggerStack->pTab );
1203 pTab = pTriggerStack->pTab;
danielk19778f2c54e2008-01-01 19:02:09 +00001204 piColMask = &(pTriggerStack->oldColMask);
drh626a8792005-01-17 22:08:19 +00001205 }
1206
1207 if( pTab ){
danielk1977f0113002006-01-24 12:09:17 +00001208 int iCol;
drh626a8792005-01-17 22:08:19 +00001209 Column *pCol = pTab->aCol;
1210
drh728b5772007-09-18 15:55:07 +00001211 pSchema = pTab->pSchema;
drh626a8792005-01-17 22:08:19 +00001212 cntTab++;
danielk1977f0113002006-01-24 12:09:17 +00001213 for(iCol=0; iCol < pTab->nCol; iCol++, pCol++) {
drh626a8792005-01-17 22:08:19 +00001214 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
danielk1977f0113002006-01-24 12:09:17 +00001215 const char *zColl = pTab->aCol[iCol].zColl;
drh626a8792005-01-17 22:08:19 +00001216 cnt++;
danielk1977f0113002006-01-24 12:09:17 +00001217 pExpr->iColumn = iCol==pTab->iPKey ? -1 : iCol;
1218 pExpr->affinity = pTab->aCol[iCol].affinity;
drh8b4c40d2007-02-01 23:02:45 +00001219 if( (pExpr->flags & EP_ExpCollate)==0 ){
1220 pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0);
1221 }
danielk1977aee18ef2005-03-09 12:26:50 +00001222 pExpr->pTab = pTab;
danielk19778f2c54e2008-01-01 19:02:09 +00001223 if( iCol>=0 ){
drhc5499be2008-04-01 15:06:33 +00001224 testcase( iCol==31 );
1225 testcase( iCol==32 );
danielk19778f2c54e2008-01-01 19:02:09 +00001226 *piColMask |= ((u32)1<<iCol) | (iCol>=32?0xffffffff:0);
1227 }
drh626a8792005-01-17 22:08:19 +00001228 break;
1229 }
1230 }
1231 }
1232 }
drhb7f91642004-10-31 02:22:47 +00001233#endif /* !defined(SQLITE_OMIT_TRIGGER) */
drh8141f612004-01-25 22:44:58 +00001234
drh626a8792005-01-17 22:08:19 +00001235 /*
1236 ** Perhaps the name is a reference to the ROWID
1237 */
1238 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
1239 cnt = 1;
1240 pExpr->iColumn = -1;
drh8a512562005-11-14 22:29:05 +00001241 pExpr->affinity = SQLITE_AFF_INTEGER;
drh626a8792005-01-17 22:08:19 +00001242 }
drh8141f612004-01-25 22:44:58 +00001243
drh626a8792005-01-17 22:08:19 +00001244 /*
1245 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
1246 ** might refer to an result-set alias. This happens, for example, when
1247 ** we are resolving names in the WHERE clause of the following command:
1248 **
1249 ** SELECT a+b AS x FROM table WHERE x<10;
1250 **
1251 ** In cases like this, replace pExpr with a copy of the expression that
1252 ** forms the result set entry ("a+b" in the example) and return immediately.
1253 ** Note that the expression in the result set should have already been
1254 ** resolved by the time the WHERE clause is resolved.
1255 */
drhffe07b22005-11-03 00:41:17 +00001256 if( cnt==0 && (pEList = pNC->pEList)!=0 && zTab==0 ){
drh626a8792005-01-17 22:08:19 +00001257 for(j=0; j<pEList->nExpr; j++){
1258 char *zAs = pEList->a[j].zName;
1259 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
drh36379e92007-07-23 22:51:15 +00001260 Expr *pDup, *pOrig;
drh626a8792005-01-17 22:08:19 +00001261 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
drh4f07e5f2007-05-14 11:34:46 +00001262 assert( pExpr->pList==0 );
1263 assert( pExpr->pSelect==0 );
drh36379e92007-07-23 22:51:15 +00001264 pOrig = pEList->a[j].pExpr;
1265 if( !pNC->allowAgg && ExprHasProperty(pOrig, EP_Agg) ){
1266 sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs);
drh17435752007-08-16 04:30:38 +00001267 sqlite3_free(zCol);
drh36379e92007-07-23 22:51:15 +00001268 return 2;
1269 }
danielk19771e536952007-08-16 10:09:01 +00001270 pDup = sqlite3ExprDup(db, pOrig);
drh4f07e5f2007-05-14 11:34:46 +00001271 if( pExpr->flags & EP_ExpCollate ){
1272 pDup->pColl = pExpr->pColl;
1273 pDup->flags |= EP_ExpCollate;
1274 }
drh17435752007-08-16 04:30:38 +00001275 if( pExpr->span.dyn ) sqlite3_free((char*)pExpr->span.z);
1276 if( pExpr->token.dyn ) sqlite3_free((char*)pExpr->token.z);
drh4f07e5f2007-05-14 11:34:46 +00001277 memcpy(pExpr, pDup, sizeof(*pExpr));
drh17435752007-08-16 04:30:38 +00001278 sqlite3_free(pDup);
drh15ccce12005-05-23 15:06:39 +00001279 cnt = 1;
danielk1977c9cf6e32007-06-25 16:29:33 +00001280 pMatch = 0;
drh626a8792005-01-17 22:08:19 +00001281 assert( zTab==0 && zDb==0 );
drh15ccce12005-05-23 15:06:39 +00001282 goto lookupname_end_2;
drh626a8792005-01-17 22:08:19 +00001283 }
1284 }
1285 }
1286
1287 /* Advance to the next name context. The loop will exit when either
1288 ** we have a match (cnt>0) or when we run out of name contexts.
1289 */
1290 if( cnt==0 ){
1291 pNC = pNC->pNext;
1292 }
drh8141f612004-01-25 22:44:58 +00001293 }
1294
1295 /*
1296 ** If X and Y are NULL (in other words if only the column name Z is
1297 ** supplied) and the value of Z is enclosed in double-quotes, then
1298 ** Z is a string literal if it doesn't match any column names. In that
1299 ** case, we need to return right away and not make any changes to
1300 ** pExpr.
drh15ccce12005-05-23 15:06:39 +00001301 **
1302 ** Because no reference was made to outer contexts, the pNC->nRef
1303 ** fields are not changed in any context.
drh8141f612004-01-25 22:44:58 +00001304 */
1305 if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
drh17435752007-08-16 04:30:38 +00001306 sqlite3_free(zCol);
drh8141f612004-01-25 22:44:58 +00001307 return 0;
1308 }
1309
1310 /*
1311 ** cnt==0 means there was not match. cnt>1 means there were two or
1312 ** more matches. Either way, we have an error.
1313 */
1314 if( cnt!=1 ){
drhde4fcfd2008-01-19 23:50:26 +00001315 const char *zErr;
1316 zErr = cnt==0 ? "no such column" : "ambiguous column name";
drh8141f612004-01-25 22:44:58 +00001317 if( zDb ){
drhde4fcfd2008-01-19 23:50:26 +00001318 sqlite3ErrorMsg(pParse, "%s: %s.%s.%s", zErr, zDb, zTab, zCol);
drh8141f612004-01-25 22:44:58 +00001319 }else if( zTab ){
drhde4fcfd2008-01-19 23:50:26 +00001320 sqlite3ErrorMsg(pParse, "%s: %s.%s", zErr, zTab, zCol);
drh8141f612004-01-25 22:44:58 +00001321 }else{
drhde4fcfd2008-01-19 23:50:26 +00001322 sqlite3ErrorMsg(pParse, "%s: %s", zErr, zCol);
drh8141f612004-01-25 22:44:58 +00001323 }
drhde4fcfd2008-01-19 23:50:26 +00001324 pTopNC->nErr++;
drh8141f612004-01-25 22:44:58 +00001325 }
1326
drh51669862004-12-18 18:40:26 +00001327 /* If a column from a table in pSrcList is referenced, then record
1328 ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes
1329 ** bit 0 to be set. Column 1 sets bit 1. And so forth. If the
1330 ** column number is greater than the number of bits in the bitmask
1331 ** then set the high-order bit of the bitmask.
1332 */
1333 if( pExpr->iColumn>=0 && pMatch!=0 ){
1334 int n = pExpr->iColumn;
drhc5499be2008-04-01 15:06:33 +00001335 testcase( n==sizeof(Bitmask)*8-1 );
drh51669862004-12-18 18:40:26 +00001336 if( n>=sizeof(Bitmask)*8 ){
1337 n = sizeof(Bitmask)*8-1;
1338 }
1339 assert( pMatch->iCursor==pExpr->iTable );
drhca83ac52007-02-01 01:40:44 +00001340 pMatch->colUsed |= ((Bitmask)1)<<n;
drh51669862004-12-18 18:40:26 +00001341 }
1342
danielk1977d5d56522005-03-16 12:15:20 +00001343lookupname_end:
drh8141f612004-01-25 22:44:58 +00001344 /* Clean up and return
1345 */
drh17435752007-08-16 04:30:38 +00001346 sqlite3_free(zDb);
1347 sqlite3_free(zTab);
danielk19774adee202004-05-08 08:23:19 +00001348 sqlite3ExprDelete(pExpr->pLeft);
drh8141f612004-01-25 22:44:58 +00001349 pExpr->pLeft = 0;
danielk19774adee202004-05-08 08:23:19 +00001350 sqlite3ExprDelete(pExpr->pRight);
drh8141f612004-01-25 22:44:58 +00001351 pExpr->pRight = 0;
1352 pExpr->op = TK_COLUMN;
drh15ccce12005-05-23 15:06:39 +00001353lookupname_end_2:
drh17435752007-08-16 04:30:38 +00001354 sqlite3_free(zCol);
drh626a8792005-01-17 22:08:19 +00001355 if( cnt==1 ){
danielk1977b3bce662005-01-29 08:32:43 +00001356 assert( pNC!=0 );
drh728b5772007-09-18 15:55:07 +00001357 sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList);
danielk1977aee18ef2005-03-09 12:26:50 +00001358 if( pMatch && !pMatch->pSelect ){
1359 pExpr->pTab = pMatch->pTab;
1360 }
drh15ccce12005-05-23 15:06:39 +00001361 /* Increment the nRef value on all name contexts from TopNC up to
1362 ** the point where the name matched. */
1363 for(;;){
1364 assert( pTopNC!=0 );
1365 pTopNC->nRef++;
1366 if( pTopNC==pNC ) break;
1367 pTopNC = pTopNC->pNext;
1368 }
1369 return 0;
1370 } else {
1371 return 1;
drh626a8792005-01-17 22:08:19 +00001372 }
drh8141f612004-01-25 22:44:58 +00001373}
1374
1375/*
drh626a8792005-01-17 22:08:19 +00001376** This routine is designed as an xFunc for walkExprTree().
1377**
drh73b211a2005-01-18 04:00:42 +00001378** Resolve symbolic names into TK_COLUMN operators for the current
drh626a8792005-01-17 22:08:19 +00001379** node in the expression tree. Return 0 to continue the search down
drh73b211a2005-01-18 04:00:42 +00001380** the tree or 2 to abort the tree walk.
1381**
1382** This routine also does error checking and name resolution for
1383** function names. The operator for aggregate functions is changed
1384** to TK_AGG_FUNCTION.
drh626a8792005-01-17 22:08:19 +00001385*/
1386static int nameResolverStep(void *pArg, Expr *pExpr){
1387 NameContext *pNC = (NameContext*)pArg;
drh626a8792005-01-17 22:08:19 +00001388 Parse *pParse;
drh626a8792005-01-17 22:08:19 +00001389
danielk1977b3bce662005-01-29 08:32:43 +00001390 if( pExpr==0 ) return 1;
drh626a8792005-01-17 22:08:19 +00001391 assert( pNC!=0 );
drh626a8792005-01-17 22:08:19 +00001392 pParse = pNC->pParse;
danielk1977b3bce662005-01-29 08:32:43 +00001393
drh626a8792005-01-17 22:08:19 +00001394 if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return 1;
1395 ExprSetProperty(pExpr, EP_Resolved);
1396#ifndef NDEBUG
danielk1977f0113002006-01-24 12:09:17 +00001397 if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
1398 SrcList *pSrcList = pNC->pSrcList;
danielk1977940fac92005-01-23 22:41:37 +00001399 int i;
danielk1977f0113002006-01-24 12:09:17 +00001400 for(i=0; i<pNC->pSrcList->nSrc; i++){
drh626a8792005-01-17 22:08:19 +00001401 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
1402 }
1403 }
1404#endif
1405 switch( pExpr->op ){
1406 /* Double-quoted strings (ex: "abc") are used as identifiers if
1407 ** possible. Otherwise they remain as strings. Single-quoted
1408 ** strings (ex: 'abc') are always string literals.
1409 */
1410 case TK_STRING: {
1411 if( pExpr->token.z[0]=='\'' ) break;
1412 /* Fall thru into the TK_ID case if this is a double-quoted string */
1413 }
1414 /* A lone identifier is the name of a column.
1415 */
1416 case TK_ID: {
drh626a8792005-01-17 22:08:19 +00001417 lookupName(pParse, 0, 0, &pExpr->token, pNC, pExpr);
1418 return 1;
1419 }
1420
1421 /* A table name and column name: ID.ID
1422 ** Or a database, table and column: ID.ID.ID
1423 */
1424 case TK_DOT: {
1425 Token *pColumn;
1426 Token *pTable;
1427 Token *pDb;
1428 Expr *pRight;
1429
danielk1977b3bce662005-01-29 08:32:43 +00001430 /* if( pSrcList==0 ) break; */
drh626a8792005-01-17 22:08:19 +00001431 pRight = pExpr->pRight;
1432 if( pRight->op==TK_ID ){
1433 pDb = 0;
1434 pTable = &pExpr->pLeft->token;
1435 pColumn = &pRight->token;
1436 }else{
1437 assert( pRight->op==TK_DOT );
1438 pDb = &pExpr->pLeft->token;
1439 pTable = &pRight->pLeft->token;
1440 pColumn = &pRight->pRight->token;
1441 }
1442 lookupName(pParse, pDb, pTable, pColumn, pNC, pExpr);
1443 return 1;
1444 }
1445
1446 /* Resolve function names
1447 */
drhb71090f2005-05-23 17:26:51 +00001448 case TK_CONST_FUNC:
drh626a8792005-01-17 22:08:19 +00001449 case TK_FUNCTION: {
1450 ExprList *pList = pExpr->pList; /* The argument list */
1451 int n = pList ? pList->nExpr : 0; /* Number of arguments */
1452 int no_such_func = 0; /* True if no such function exists */
1453 int wrong_num_args = 0; /* True if wrong number of arguments */
1454 int is_agg = 0; /* True if is an aggregate function */
1455 int i;
drh5169bbc2006-08-24 14:59:45 +00001456 int auth; /* Authorization to use the function */
drh626a8792005-01-17 22:08:19 +00001457 int nId; /* Number of characters in function name */
1458 const char *zId; /* The function name. */
drh73b211a2005-01-18 04:00:42 +00001459 FuncDef *pDef; /* Information about the function */
danielk197714db2662006-01-09 16:12:04 +00001460 int enc = ENC(pParse->db); /* The database encoding */
drh626a8792005-01-17 22:08:19 +00001461
drh2646da72005-12-09 20:02:05 +00001462 zId = (char*)pExpr->token.z;
drhb71090f2005-05-23 17:26:51 +00001463 nId = pExpr->token.n;
drh626a8792005-01-17 22:08:19 +00001464 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
1465 if( pDef==0 ){
1466 pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
1467 if( pDef==0 ){
1468 no_such_func = 1;
1469 }else{
1470 wrong_num_args = 1;
1471 }
1472 }else{
1473 is_agg = pDef->xFunc==0;
1474 }
drh2fca7fe2006-11-23 11:59:13 +00001475#ifndef SQLITE_OMIT_AUTHORIZATION
drh5169bbc2006-08-24 14:59:45 +00001476 if( pDef ){
1477 auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0);
1478 if( auth!=SQLITE_OK ){
1479 if( auth==SQLITE_DENY ){
1480 sqlite3ErrorMsg(pParse, "not authorized to use function: %s",
1481 pDef->zName);
1482 pNC->nErr++;
1483 }
1484 pExpr->op = TK_NULL;
1485 return 1;
1486 }
1487 }
drhb8b14212006-08-24 15:18:25 +00001488#endif
drh626a8792005-01-17 22:08:19 +00001489 if( is_agg && !pNC->allowAgg ){
1490 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
1491 pNC->nErr++;
1492 is_agg = 0;
1493 }else if( no_such_func ){
1494 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
1495 pNC->nErr++;
1496 }else if( wrong_num_args ){
1497 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
1498 nId, zId);
1499 pNC->nErr++;
1500 }
1501 if( is_agg ){
1502 pExpr->op = TK_AGG_FUNCTION;
1503 pNC->hasAgg = 1;
1504 }
drh73b211a2005-01-18 04:00:42 +00001505 if( is_agg ) pNC->allowAgg = 0;
drh626a8792005-01-17 22:08:19 +00001506 for(i=0; pNC->nErr==0 && i<n; i++){
drh73b211a2005-01-18 04:00:42 +00001507 walkExprTree(pList->a[i].pExpr, nameResolverStep, pNC);
drh626a8792005-01-17 22:08:19 +00001508 }
drh73b211a2005-01-18 04:00:42 +00001509 if( is_agg ) pNC->allowAgg = 1;
drh626a8792005-01-17 22:08:19 +00001510 /* FIX ME: Compute pExpr->affinity based on the expected return
1511 ** type of the function
1512 */
1513 return is_agg;
1514 }
danielk1977b3bce662005-01-29 08:32:43 +00001515#ifndef SQLITE_OMIT_SUBQUERY
1516 case TK_SELECT:
1517 case TK_EXISTS:
1518#endif
1519 case TK_IN: {
1520 if( pExpr->pSelect ){
drh8a9f38f2005-11-05 15:07:55 +00001521 int nRef = pNC->nRef;
drh06f65412005-11-03 02:03:13 +00001522#ifndef SQLITE_OMIT_CHECK
1523 if( pNC->isCheck ){
1524 sqlite3ErrorMsg(pParse,"subqueries prohibited in CHECK constraints");
1525 }
1526#endif
danielk1977b3bce662005-01-29 08:32:43 +00001527 sqlite3SelectResolve(pParse, pExpr->pSelect, pNC);
1528 assert( pNC->nRef>=nRef );
1529 if( nRef!=pNC->nRef ){
1530 ExprSetProperty(pExpr, EP_VarSelect);
1531 }
1532 }
drh4284fb02005-11-03 12:33:28 +00001533 break;
danielk1977b3bce662005-01-29 08:32:43 +00001534 }
drh4284fb02005-11-03 12:33:28 +00001535#ifndef SQLITE_OMIT_CHECK
1536 case TK_VARIABLE: {
1537 if( pNC->isCheck ){
1538 sqlite3ErrorMsg(pParse,"parameters prohibited in CHECK constraints");
1539 }
1540 break;
1541 }
1542#endif
drh626a8792005-01-17 22:08:19 +00001543 }
1544 return 0;
1545}
1546
1547/*
drhcce7d172000-05-31 15:34:51 +00001548** This routine walks an expression tree and resolves references to
drh967e8b72000-06-21 13:59:10 +00001549** table columns. Nodes of the form ID.ID or ID resolve into an
drhaacc5432002-01-06 17:07:40 +00001550** index to the table in the table list and a column offset. The
1551** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
1552** value is changed to the index of the referenced table in pTabList
drh832508b2002-03-02 17:04:07 +00001553** plus the "base" value. The base value will ultimately become the
drhaacc5432002-01-06 17:07:40 +00001554** VDBE cursor number for a cursor that is pointing into the referenced
1555** table. The Expr.iColumn value is changed to the index of the column
1556** of the referenced table. The Expr.iColumn value for the special
1557** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
1558** alias for ROWID.
drh19a775c2000-06-05 18:54:46 +00001559**
drh626a8792005-01-17 22:08:19 +00001560** Also resolve function names and check the functions for proper
1561** usage. Make sure all function names are recognized and all functions
1562** have the correct number of arguments. Leave an error message
1563** in pParse->zErrMsg if anything is amiss. Return the number of errors.
1564**
drh73b211a2005-01-18 04:00:42 +00001565** If the expression contains aggregate functions then set the EP_Agg
1566** property on the expression.
drh626a8792005-01-17 22:08:19 +00001567*/
danielk1977fc976062007-05-10 10:46:56 +00001568int sqlite3ExprResolveNames(
danielk1977b3bce662005-01-29 08:32:43 +00001569 NameContext *pNC, /* Namespace to resolve expressions in. */
1570 Expr *pExpr /* The expression to be analyzed. */
drh626a8792005-01-17 22:08:19 +00001571){
drh13449892005-09-07 21:22:45 +00001572 int savedHasAgg;
drhbb4957f2008-03-20 14:03:29 +00001573
drh73b211a2005-01-18 04:00:42 +00001574 if( pExpr==0 ) return 0;
drhbb4957f2008-03-20 14:03:29 +00001575#if SQLITE_MAX_EXPR_DEPTH>0
1576 {
danielk19774b5255a2008-06-05 16:47:39 +00001577 if( checkExprHeight(pNC->pParse, pExpr->nHeight + pNC->pParse->nHeight) ){
drhbb4957f2008-03-20 14:03:29 +00001578 return 1;
1579 }
1580 pNC->pParse->nHeight += pExpr->nHeight;
danielk1977fc976062007-05-10 10:46:56 +00001581 }
danielk1977fc976062007-05-10 10:46:56 +00001582#endif
drh13449892005-09-07 21:22:45 +00001583 savedHasAgg = pNC->hasAgg;
1584 pNC->hasAgg = 0;
danielk1977b3bce662005-01-29 08:32:43 +00001585 walkExprTree(pExpr, nameResolverStep, pNC);
drhbb4957f2008-03-20 14:03:29 +00001586#if SQLITE_MAX_EXPR_DEPTH>0
danielk1977fc976062007-05-10 10:46:56 +00001587 pNC->pParse->nHeight -= pExpr->nHeight;
1588#endif
danielk1977b3bce662005-01-29 08:32:43 +00001589 if( pNC->nErr>0 ){
drh73b211a2005-01-18 04:00:42 +00001590 ExprSetProperty(pExpr, EP_Error);
drh73b211a2005-01-18 04:00:42 +00001591 }
drh13449892005-09-07 21:22:45 +00001592 if( pNC->hasAgg ){
1593 ExprSetProperty(pExpr, EP_Agg);
1594 }else if( savedHasAgg ){
1595 pNC->hasAgg = 1;
1596 }
drh73b211a2005-01-18 04:00:42 +00001597 return ExprHasProperty(pExpr, EP_Error);
drh626a8792005-01-17 22:08:19 +00001598}
1599
drh1398ad32005-01-19 23:24:50 +00001600/*
1601** A pointer instance of this structure is used to pass information
1602** through walkExprTree into codeSubqueryStep().
1603*/
1604typedef struct QueryCoder QueryCoder;
1605struct QueryCoder {
1606 Parse *pParse; /* The parsing context */
1607 NameContext *pNC; /* Namespace of first enclosing query */
1608};
1609
danielk19779a96b662007-11-29 17:05:18 +00001610#ifdef SQLITE_TEST
1611 int sqlite3_enable_in_opt = 1;
1612#else
1613 #define sqlite3_enable_in_opt 1
1614#endif
1615
1616/*
drhb287f4b2008-04-25 00:08:38 +00001617** Return true if the IN operator optimization is enabled and
1618** the SELECT statement p exists and is of the
1619** simple form:
1620**
1621** SELECT <column> FROM <table>
1622**
1623** If this is the case, it may be possible to use an existing table
1624** or index instead of generating an epheremal table.
1625*/
1626#ifndef SQLITE_OMIT_SUBQUERY
1627static int isCandidateForInOpt(Select *p){
1628 SrcList *pSrc;
1629 ExprList *pEList;
1630 Table *pTab;
1631 if( !sqlite3_enable_in_opt ) return 0; /* IN optimization must be enabled */
1632 if( p==0 ) return 0; /* right-hand side of IN is SELECT */
1633 if( p->pPrior ) return 0; /* Not a compound SELECT */
1634 if( p->isDistinct ) return 0; /* No DISTINCT keyword */
1635 if( p->isAgg ) return 0; /* Contains no aggregate functions */
1636 if( p->pGroupBy ) return 0; /* Has no GROUP BY clause */
1637 if( p->pLimit ) return 0; /* Has no LIMIT clause */
1638 if( p->pOffset ) return 0;
1639 if( p->pWhere ) return 0; /* Has no WHERE clause */
1640 pSrc = p->pSrc;
1641 if( pSrc==0 ) return 0; /* A single table in the FROM clause */
1642 if( pSrc->nSrc!=1 ) return 0;
1643 if( pSrc->a[0].pSelect ) return 0; /* FROM clause is not a subquery */
1644 pTab = pSrc->a[0].pTab;
1645 if( pTab==0 ) return 0;
1646 if( pTab->pSelect ) return 0; /* FROM clause is not a view */
1647 if( IsVirtual(pTab) ) return 0; /* FROM clause not a virtual table */
1648 pEList = p->pEList;
1649 if( pEList->nExpr!=1 ) return 0; /* One column in the result set */
1650 if( pEList->a[0].pExpr->op!=TK_COLUMN ) return 0; /* Result is a column */
1651 return 1;
1652}
1653#endif /* SQLITE_OMIT_SUBQUERY */
1654
1655/*
danielk19779a96b662007-11-29 17:05:18 +00001656** This function is used by the implementation of the IN (...) operator.
1657** It's job is to find or create a b-tree structure that may be used
1658** either to test for membership of the (...) set or to iterate through
drh85b623f2007-12-13 21:54:09 +00001659** its members, skipping duplicates.
danielk19779a96b662007-11-29 17:05:18 +00001660**
1661** The cursor opened on the structure (database table, database index
1662** or ephermal table) is stored in pX->iTable before this function returns.
1663** The returned value indicates the structure type, as follows:
1664**
1665** IN_INDEX_ROWID - The cursor was opened on a database table.
drh2d401ab2008-01-10 23:50:11 +00001666** IN_INDEX_INDEX - The cursor was opened on a database index.
danielk19779a96b662007-11-29 17:05:18 +00001667** IN_INDEX_EPH - The cursor was opened on a specially created and
1668** populated epheremal table.
1669**
1670** An existing structure may only be used if the SELECT is of the simple
1671** form:
1672**
1673** SELECT <column> FROM <table>
1674**
danielk19770cdc0222008-06-26 18:04:03 +00001675** If prNotFound parameter is 0, then the structure will be used to iterate
danielk19779a96b662007-11-29 17:05:18 +00001676** through the set members, skipping any duplicates. In this case an
1677** epheremal table must be used unless the selected <column> is guaranteed
1678** to be unique - either because it is an INTEGER PRIMARY KEY or it
1679** is unique by virtue of a constraint or implicit index.
danielk19770cdc0222008-06-26 18:04:03 +00001680**
1681** If the prNotFound parameter is not 0, then the structure will be used
1682** for fast set membership tests. In this case an epheremal table must
1683** be used unless <column> is an INTEGER PRIMARY KEY or an index can
1684** be found with <column> as its left-most column.
1685**
1686** When the structure is being used for set membership tests, the user
1687** needs to know whether or not the structure contains an SQL NULL
1688** value in order to correctly evaluate expressions like "X IN (Y, Z)".
1689** If there is a chance that the structure may contain a NULL value at
1690** runtime, then a register is allocated and the register number written
1691** to *prNotFound. If there is no chance that the structure contains a
1692** NULL value, then *prNotFound is left unchanged.
1693**
1694** If a register is allocated and its location stored in *prNotFound, then
1695** its initial value is NULL. If the structure does not remain constant
1696** for the duration of the query (i.e. the set is a correlated sub-select),
1697** the value of the allocated register is reset to NULL each time the
1698** structure is repopulated. This allows the caller to use vdbe code
1699** equivalent to the following:
1700**
1701** if( register==NULL ){
1702** has_null = <test if data structure contains null>
1703** register = 1
1704** }
1705**
1706** in order to avoid running the <test if data structure contains null>
1707** test more often than is necessary.
danielk19779a96b662007-11-29 17:05:18 +00001708*/
danielk1977284f4ac2007-12-10 05:03:46 +00001709#ifndef SQLITE_OMIT_SUBQUERY
danielk19770cdc0222008-06-26 18:04:03 +00001710int sqlite3FindInIndex(Parse *pParse, Expr *pX, int *prNotFound){
danielk19779a96b662007-11-29 17:05:18 +00001711 Select *p;
1712 int eType = 0;
1713 int iTab = pParse->nTab++;
danielk19770cdc0222008-06-26 18:04:03 +00001714 int mustBeUnique = !prNotFound;
danielk19779a96b662007-11-29 17:05:18 +00001715
1716 /* The follwing if(...) expression is true if the SELECT is of the
1717 ** simple form:
1718 **
1719 ** SELECT <column> FROM <table>
1720 **
1721 ** If this is the case, it may be possible to use an existing table
1722 ** or index instead of generating an epheremal table.
1723 */
drhb287f4b2008-04-25 00:08:38 +00001724 p = pX->pSelect;
1725 if( isCandidateForInOpt(p) ){
danielk19779a96b662007-11-29 17:05:18 +00001726 sqlite3 *db = pParse->db;
1727 Index *pIdx;
1728 Expr *pExpr = p->pEList->a[0].pExpr;
1729 int iCol = pExpr->iColumn;
1730 Vdbe *v = sqlite3GetVdbe(pParse);
1731
1732 /* This function is only called from two places. In both cases the vdbe
1733 ** has already been allocated. So assume sqlite3GetVdbe() is always
1734 ** successful here.
1735 */
1736 assert(v);
1737 if( iCol<0 ){
drh0a07c102008-01-03 18:03:08 +00001738 int iMem = ++pParse->nMem;
danielk19779a96b662007-11-29 17:05:18 +00001739 int iAddr;
1740 Table *pTab = p->pSrc->a[0].pTab;
1741 int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
1742 sqlite3VdbeUsesBtree(v, iDb);
1743
drh892d3172008-01-10 03:46:36 +00001744 iAddr = sqlite3VdbeAddOp1(v, OP_If, iMem);
drh4c583122008-01-04 22:01:03 +00001745 sqlite3VdbeAddOp2(v, OP_Integer, 1, iMem);
danielk19779a96b662007-11-29 17:05:18 +00001746
1747 sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
1748 eType = IN_INDEX_ROWID;
1749
1750 sqlite3VdbeJumpHere(v, iAddr);
1751 }else{
1752 /* The collation sequence used by the comparison. If an index is to
1753 ** be used in place of a temp-table, it must be ordered according
1754 ** to this collation sequence.
1755 */
1756 CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pExpr);
1757
1758 /* Check that the affinity that will be used to perform the
1759 ** comparison is the same as the affinity of the column. If
1760 ** it is not, it is not possible to use any index.
1761 */
1762 Table *pTab = p->pSrc->a[0].pTab;
1763 char aff = comparisonAffinity(pX);
1764 int affinity_ok = (pTab->aCol[iCol].affinity==aff||aff==SQLITE_AFF_NONE);
1765
1766 for(pIdx=pTab->pIndex; pIdx && eType==0 && affinity_ok; pIdx=pIdx->pNext){
1767 if( (pIdx->aiColumn[0]==iCol)
1768 && (pReq==sqlite3FindCollSeq(db, ENC(db), pIdx->azColl[0], -1, 0))
1769 && (!mustBeUnique || (pIdx->nColumn==1 && pIdx->onError!=OE_None))
1770 ){
1771 int iDb;
drh0a07c102008-01-03 18:03:08 +00001772 int iMem = ++pParse->nMem;
danielk19779a96b662007-11-29 17:05:18 +00001773 int iAddr;
1774 char *pKey;
1775
1776 pKey = (char *)sqlite3IndexKeyinfo(pParse, pIdx);
1777 iDb = sqlite3SchemaToIndex(db, pIdx->pSchema);
1778 sqlite3VdbeUsesBtree(v, iDb);
1779
drh892d3172008-01-10 03:46:36 +00001780 iAddr = sqlite3VdbeAddOp1(v, OP_If, iMem);
drh4c583122008-01-04 22:01:03 +00001781 sqlite3VdbeAddOp2(v, OP_Integer, 1, iMem);
danielk19779a96b662007-11-29 17:05:18 +00001782
danielk1977cd3e8f72008-03-25 09:47:35 +00001783 sqlite3VdbeAddOp2(v, OP_SetNumColumns, 0, pIdx->nColumn);
danielk1977207872a2008-01-03 07:54:23 +00001784 sqlite3VdbeAddOp4(v, OP_OpenRead, iTab, pIdx->tnum, iDb,
drh66a51672008-01-03 00:01:23 +00001785 pKey,P4_KEYINFO_HANDOFF);
danielk1977207872a2008-01-03 07:54:23 +00001786 VdbeComment((v, "%s", pIdx->zName));
danielk19779a96b662007-11-29 17:05:18 +00001787 eType = IN_INDEX_INDEX;
danielk19779a96b662007-11-29 17:05:18 +00001788
1789 sqlite3VdbeJumpHere(v, iAddr);
danielk19770cdc0222008-06-26 18:04:03 +00001790 if( prNotFound && !pTab->aCol[iCol].notNull ){
1791 *prNotFound = ++pParse->nMem;
1792 }
danielk19779a96b662007-11-29 17:05:18 +00001793 }
1794 }
1795 }
1796 }
1797
1798 if( eType==0 ){
danielk19770cdc0222008-06-26 18:04:03 +00001799 int rMayHaveNull = 0;
1800 if( prNotFound ){
1801 *prNotFound = rMayHaveNull = ++pParse->nMem;
1802 }
1803 sqlite3CodeSubselect(pParse, pX, rMayHaveNull);
danielk19779a96b662007-11-29 17:05:18 +00001804 eType = IN_INDEX_EPH;
1805 }else{
1806 pX->iTable = iTab;
1807 }
1808 return eType;
1809}
danielk1977284f4ac2007-12-10 05:03:46 +00001810#endif
drh626a8792005-01-17 22:08:19 +00001811
1812/*
drh9cbe6352005-11-29 03:13:21 +00001813** Generate code for scalar subqueries used as an expression
1814** and IN operators. Examples:
drh626a8792005-01-17 22:08:19 +00001815**
drh9cbe6352005-11-29 03:13:21 +00001816** (SELECT a FROM b) -- subquery
1817** EXISTS (SELECT a FROM b) -- EXISTS subquery
1818** x IN (4,5,11) -- IN operator with list on right-hand side
1819** x IN (SELECT a FROM b) -- IN operator with subquery on the right
drhfef52082000-06-06 01:50:43 +00001820**
drh9cbe6352005-11-29 03:13:21 +00001821** The pExpr parameter describes the expression that contains the IN
1822** operator or subquery.
drhcce7d172000-05-31 15:34:51 +00001823*/
drh51522cd2005-01-20 13:36:19 +00001824#ifndef SQLITE_OMIT_SUBQUERY
danielk19770cdc0222008-06-26 18:04:03 +00001825void sqlite3CodeSubselect(Parse *pParse, Expr *pExpr, int rMayHaveNull){
drh57dbd7b2005-07-08 18:25:26 +00001826 int testAddr = 0; /* One-time test address */
danielk1977b3bce662005-01-29 08:32:43 +00001827 Vdbe *v = sqlite3GetVdbe(pParse);
1828 if( v==0 ) return;
1829
danielk1977fc976062007-05-10 10:46:56 +00001830
drh57dbd7b2005-07-08 18:25:26 +00001831 /* This code must be run in its entirety every time it is encountered
1832 ** if any of the following is true:
1833 **
1834 ** * The right-hand side is a correlated subquery
1835 ** * The right-hand side is an expression list containing variables
1836 ** * We are inside a trigger
1837 **
1838 ** If all of the above are false, then we can run this code just once
1839 ** save the results, and reuse the same result on subsequent invocations.
danielk1977b3bce662005-01-29 08:32:43 +00001840 */
1841 if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->trigStack ){
drh0a07c102008-01-03 18:03:08 +00001842 int mem = ++pParse->nMem;
drh892d3172008-01-10 03:46:36 +00001843 sqlite3VdbeAddOp1(v, OP_If, mem);
1844 testAddr = sqlite3VdbeAddOp2(v, OP_Integer, 1, mem);
drh17435752007-08-16 04:30:38 +00001845 assert( testAddr>0 || pParse->db->mallocFailed );
danielk1977b3bce662005-01-29 08:32:43 +00001846 }
1847
drhcce7d172000-05-31 15:34:51 +00001848 switch( pExpr->op ){
drhfef52082000-06-06 01:50:43 +00001849 case TK_IN: {
danielk1977e014a832004-05-17 10:48:57 +00001850 char affinity;
drhd3d39e92004-05-20 22:16:29 +00001851 KeyInfo keyInfo;
drhb9bb7c12006-06-11 23:41:55 +00001852 int addr; /* Address of OP_OpenEphemeral instruction */
drhd3d39e92004-05-20 22:16:29 +00001853
danielk19770cdc0222008-06-26 18:04:03 +00001854 if( rMayHaveNull ){
1855 sqlite3VdbeAddOp2(v, OP_Null, 0, rMayHaveNull);
1856 }
1857
danielk1977bf3b7212004-05-18 10:06:24 +00001858 affinity = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +00001859
1860 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
drh57dbd7b2005-07-08 18:25:26 +00001861 ** expression it is handled the same way. A virtual table is
danielk1977e014a832004-05-17 10:48:57 +00001862 ** filled with single-field index keys representing the results
1863 ** from the SELECT or the <exprlist>.
1864 **
1865 ** If the 'x' expression is a column value, or the SELECT...
1866 ** statement returns a column value, then the affinity of that
1867 ** column is used to build the index keys. If both 'x' and the
1868 ** SELECT... statement are columns, then numeric affinity is used
1869 ** if either column has NUMERIC or INTEGER affinity. If neither
1870 ** 'x' nor the SELECT... statement are columns, then numeric affinity
1871 ** is used.
1872 */
1873 pExpr->iTable = pParse->nTab++;
danielk1977cd3e8f72008-03-25 09:47:35 +00001874 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pExpr->iTable, 1);
drhd3d39e92004-05-20 22:16:29 +00001875 memset(&keyInfo, 0, sizeof(keyInfo));
1876 keyInfo.nField = 1;
danielk1977e014a832004-05-17 10:48:57 +00001877
drhfef52082000-06-06 01:50:43 +00001878 if( pExpr->pSelect ){
1879 /* Case 1: expr IN (SELECT ...)
1880 **
danielk1977e014a832004-05-17 10:48:57 +00001881 ** Generate code to write the results of the select into the temporary
1882 ** table allocated and opened above.
drhfef52082000-06-06 01:50:43 +00001883 */
drh1013c932008-01-06 00:25:21 +00001884 SelectDest dest;
drhbe5c89a2004-07-26 00:31:09 +00001885 ExprList *pEList;
drh1013c932008-01-06 00:25:21 +00001886
1887 sqlite3SelectDestInit(&dest, SRT_Set, pExpr->iTable);
1888 dest.affinity = (int)affinity;
danielk1977e014a832004-05-17 10:48:57 +00001889 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
drha9671a22008-07-08 23:40:20 +00001890 if( sqlite3Select(pParse, pExpr->pSelect, &dest, 0, 0, 0) ){
drh94ccde52007-04-13 16:06:32 +00001891 return;
1892 }
drhbe5c89a2004-07-26 00:31:09 +00001893 pEList = pExpr->pSelect->pEList;
1894 if( pEList && pEList->nExpr>0 ){
danielk1977bcbb04e2007-05-29 12:11:29 +00001895 keyInfo.aColl[0] = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft,
drhbe5c89a2004-07-26 00:31:09 +00001896 pEList->a[0].pExpr);
danielk19770202b292004-06-09 09:55:16 +00001897 }
drhfef52082000-06-06 01:50:43 +00001898 }else if( pExpr->pList ){
1899 /* Case 2: expr IN (exprlist)
1900 **
drhfd131da2007-08-07 17:13:03 +00001901 ** For each expression, build an index key from the evaluation and
danielk1977e014a832004-05-17 10:48:57 +00001902 ** store it in the temporary table. If <expr> is a column, then use
1903 ** that columns affinity when building index keys. If <expr> is not
1904 ** a column, use numeric affinity.
drhfef52082000-06-06 01:50:43 +00001905 */
danielk1977e014a832004-05-17 10:48:57 +00001906 int i;
drh57dbd7b2005-07-08 18:25:26 +00001907 ExprList *pList = pExpr->pList;
1908 struct ExprList_item *pItem;
drhecc31802008-06-26 20:06:06 +00001909 int r1, r2, r3;
drh57dbd7b2005-07-08 18:25:26 +00001910
danielk1977e014a832004-05-17 10:48:57 +00001911 if( !affinity ){
drh8159a352006-05-23 23:22:29 +00001912 affinity = SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +00001913 }
danielk19770202b292004-06-09 09:55:16 +00001914 keyInfo.aColl[0] = pExpr->pLeft->pColl;
danielk1977e014a832004-05-17 10:48:57 +00001915
1916 /* Loop through each expression in <exprlist>. */
drh2d401ab2008-01-10 23:50:11 +00001917 r1 = sqlite3GetTempReg(pParse);
1918 r2 = sqlite3GetTempReg(pParse);
drh57dbd7b2005-07-08 18:25:26 +00001919 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
1920 Expr *pE2 = pItem->pExpr;
danielk1977e014a832004-05-17 10:48:57 +00001921
drh57dbd7b2005-07-08 18:25:26 +00001922 /* If the expression is not constant then we will need to
1923 ** disable the test that was generated above that makes sure
1924 ** this code only executes once. Because for a non-constant
1925 ** expression we need to rerun this code each time.
1926 */
drh892d3172008-01-10 03:46:36 +00001927 if( testAddr && !sqlite3ExprIsConstant(pE2) ){
1928 sqlite3VdbeChangeToNoop(v, testAddr-1, 2);
drh57dbd7b2005-07-08 18:25:26 +00001929 testAddr = 0;
drh4794b982000-06-06 13:54:14 +00001930 }
danielk1977e014a832004-05-17 10:48:57 +00001931
1932 /* Evaluate the expression and insert it into the temp table */
drhe55cbd72008-03-31 23:48:03 +00001933 pParse->disableColCache++;
drhecc31802008-06-26 20:06:06 +00001934 r3 = sqlite3ExprCodeTarget(pParse, pE2, r1);
drhc5499be2008-04-01 15:06:33 +00001935 assert( pParse->disableColCache>0 );
drhe55cbd72008-03-31 23:48:03 +00001936 pParse->disableColCache--;
drhecc31802008-06-26 20:06:06 +00001937 sqlite3VdbeAddOp4(v, OP_MakeRecord, r3, 1, r2, &affinity, 1);
drh3c31fc22008-06-26 21:45:26 +00001938 sqlite3ExprCacheAffinityChange(pParse, r3, 1);
drh2d401ab2008-01-10 23:50:11 +00001939 sqlite3VdbeAddOp2(v, OP_IdxInsert, pExpr->iTable, r2);
drhfef52082000-06-06 01:50:43 +00001940 }
drh2d401ab2008-01-10 23:50:11 +00001941 sqlite3ReleaseTempReg(pParse, r1);
1942 sqlite3ReleaseTempReg(pParse, r2);
drhfef52082000-06-06 01:50:43 +00001943 }
drh66a51672008-01-03 00:01:23 +00001944 sqlite3VdbeChangeP4(v, addr, (void *)&keyInfo, P4_KEYINFO);
danielk1977b3bce662005-01-29 08:32:43 +00001945 break;
drhfef52082000-06-06 01:50:43 +00001946 }
1947
drh51522cd2005-01-20 13:36:19 +00001948 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00001949 case TK_SELECT: {
drhfef52082000-06-06 01:50:43 +00001950 /* This has to be a scalar SELECT. Generate code to put the
1951 ** value of this select in a memory cell and record the number
drh967e8b72000-06-21 13:59:10 +00001952 ** of the memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +00001953 */
drh2646da72005-12-09 20:02:05 +00001954 static const Token one = { (u8*)"1", 0, 1 };
drh51522cd2005-01-20 13:36:19 +00001955 Select *pSel;
danielk19776c8c8ce2008-01-02 16:27:09 +00001956 SelectDest dest;
drh1398ad32005-01-19 23:24:50 +00001957
drh51522cd2005-01-20 13:36:19 +00001958 pSel = pExpr->pSelect;
drh1013c932008-01-06 00:25:21 +00001959 sqlite3SelectDestInit(&dest, 0, ++pParse->nMem);
drh51522cd2005-01-20 13:36:19 +00001960 if( pExpr->op==TK_SELECT ){
danielk19776c8c8ce2008-01-02 16:27:09 +00001961 dest.eDest = SRT_Mem;
drh4c583122008-01-04 22:01:03 +00001962 sqlite3VdbeAddOp2(v, OP_Null, 0, dest.iParm);
drhd4e70eb2008-01-02 00:34:36 +00001963 VdbeComment((v, "Init subquery result"));
drh51522cd2005-01-20 13:36:19 +00001964 }else{
danielk19776c8c8ce2008-01-02 16:27:09 +00001965 dest.eDest = SRT_Exists;
drh4c583122008-01-04 22:01:03 +00001966 sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iParm);
drhd4e70eb2008-01-02 00:34:36 +00001967 VdbeComment((v, "Init EXISTS result"));
drh51522cd2005-01-20 13:36:19 +00001968 }
drhec7429a2005-10-06 16:53:14 +00001969 sqlite3ExprDelete(pSel->pLimit);
danielk1977a1644fd2007-08-29 12:31:25 +00001970 pSel->pLimit = sqlite3PExpr(pParse, TK_INTEGER, 0, 0, &one);
drha9671a22008-07-08 23:40:20 +00001971 if( sqlite3Select(pParse, pSel, &dest, 0, 0, 0) ){
drh94ccde52007-04-13 16:06:32 +00001972 return;
1973 }
danielk19776c8c8ce2008-01-02 16:27:09 +00001974 pExpr->iColumn = dest.iParm;
danielk1977b3bce662005-01-29 08:32:43 +00001975 break;
drhcce7d172000-05-31 15:34:51 +00001976 }
1977 }
danielk1977b3bce662005-01-29 08:32:43 +00001978
drh57dbd7b2005-07-08 18:25:26 +00001979 if( testAddr ){
drh892d3172008-01-10 03:46:36 +00001980 sqlite3VdbeJumpHere(v, testAddr-1);
danielk1977b3bce662005-01-29 08:32:43 +00001981 }
danielk1977fc976062007-05-10 10:46:56 +00001982
danielk1977b3bce662005-01-29 08:32:43 +00001983 return;
drhcce7d172000-05-31 15:34:51 +00001984}
drh51522cd2005-01-20 13:36:19 +00001985#endif /* SQLITE_OMIT_SUBQUERY */
drhcce7d172000-05-31 15:34:51 +00001986
drhcce7d172000-05-31 15:34:51 +00001987/*
drh598f1342007-10-23 15:39:45 +00001988** Duplicate an 8-byte value
1989*/
1990static char *dup8bytes(Vdbe *v, const char *in){
1991 char *out = sqlite3DbMallocRaw(sqlite3VdbeDb(v), 8);
1992 if( out ){
1993 memcpy(out, in, 8);
1994 }
1995 return out;
1996}
1997
1998/*
1999** Generate an instruction that will put the floating point
drh9cbf3422008-01-17 16:22:13 +00002000** value described by z[0..n-1] into register iMem.
drh0cf19ed2007-10-23 18:55:48 +00002001**
2002** The z[] string will probably not be zero-terminated. But the
2003** z[n] character is guaranteed to be something that does not look
2004** like the continuation of the number.
drh598f1342007-10-23 15:39:45 +00002005*/
drh9de221d2008-01-05 06:51:30 +00002006static void codeReal(Vdbe *v, const char *z, int n, int negateFlag, int iMem){
drh598f1342007-10-23 15:39:45 +00002007 assert( z || v==0 || sqlite3VdbeDb(v)->mallocFailed );
2008 if( z ){
2009 double value;
2010 char *zV;
drh0cf19ed2007-10-23 18:55:48 +00002011 assert( !isdigit(z[n]) );
drh598f1342007-10-23 15:39:45 +00002012 sqlite3AtoF(z, &value);
drh2eaf93d2008-04-29 00:15:20 +00002013 if( sqlite3IsNaN(value) ){
2014 sqlite3VdbeAddOp2(v, OP_Null, 0, iMem);
2015 }else{
2016 if( negateFlag ) value = -value;
2017 zV = dup8bytes(v, (char*)&value);
2018 sqlite3VdbeAddOp4(v, OP_Real, 0, iMem, 0, zV, P4_REAL);
2019 }
drh598f1342007-10-23 15:39:45 +00002020 }
2021}
2022
2023
2024/*
drhfec19aa2004-05-19 20:41:03 +00002025** Generate an instruction that will put the integer describe by
drh9cbf3422008-01-17 16:22:13 +00002026** text z[0..n-1] into register iMem.
drh0cf19ed2007-10-23 18:55:48 +00002027**
2028** The z[] string will probably not be zero-terminated. But the
2029** z[n] character is guaranteed to be something that does not look
2030** like the continuation of the number.
drhfec19aa2004-05-19 20:41:03 +00002031*/
drh92b01d52008-06-24 00:32:35 +00002032static void codeInteger(Vdbe *v, Expr *pExpr, int negFlag, int iMem){
2033 const char *z;
2034 if( pExpr->flags & EP_IntValue ){
2035 int i = pExpr->iTable;
2036 if( negFlag ) i = -i;
2037 sqlite3VdbeAddOp2(v, OP_Integer, i, iMem);
2038 }else if( (z = (char*)pExpr->token.z)!=0 ){
danielk1977c9cf9012007-05-30 10:36:47 +00002039 int i;
drh92b01d52008-06-24 00:32:35 +00002040 int n = pExpr->token.n;
drh0cf19ed2007-10-23 18:55:48 +00002041 assert( !isdigit(z[n]) );
danielk1977c9cf9012007-05-30 10:36:47 +00002042 if( sqlite3GetInt32(z, &i) ){
drh9de221d2008-01-05 06:51:30 +00002043 if( negFlag ) i = -i;
2044 sqlite3VdbeAddOp2(v, OP_Integer, i, iMem);
2045 }else if( sqlite3FitsIn64Bits(z, negFlag) ){
drh598f1342007-10-23 15:39:45 +00002046 i64 value;
2047 char *zV;
2048 sqlite3Atoi64(z, &value);
drh9de221d2008-01-05 06:51:30 +00002049 if( negFlag ) value = -value;
drh598f1342007-10-23 15:39:45 +00002050 zV = dup8bytes(v, (char*)&value);
drh9de221d2008-01-05 06:51:30 +00002051 sqlite3VdbeAddOp4(v, OP_Int64, 0, iMem, 0, zV, P4_INT64);
danielk1977c9cf9012007-05-30 10:36:47 +00002052 }else{
drh9de221d2008-01-05 06:51:30 +00002053 codeReal(v, z, n, negFlag, iMem);
danielk1977c9cf9012007-05-30 10:36:47 +00002054 }
drhfec19aa2004-05-19 20:41:03 +00002055 }
2056}
2057
drh945498f2007-02-24 11:52:52 +00002058
2059/*
2060** Generate code that will extract the iColumn-th column from
drhe55cbd72008-03-31 23:48:03 +00002061** table pTab and store the column value in a register. An effort
2062** is made to store the column value in register iReg, but this is
2063** not guaranteed. The location of the column value is returned.
2064**
2065** There must be an open cursor to pTab in iTable when this routine
2066** is called. If iColumn<0 then code is generated that extracts the rowid.
drhda250ea2008-04-01 05:07:14 +00002067**
2068** This routine might attempt to reuse the value of the column that
2069** has already been loaded into a register. The value will always
2070** be used if it has not undergone any affinity changes. But if
2071** an affinity change has occurred, then the cached value will only be
2072** used if allowAffChng is true.
drh945498f2007-02-24 11:52:52 +00002073*/
drhe55cbd72008-03-31 23:48:03 +00002074int sqlite3ExprCodeGetColumn(
2075 Parse *pParse, /* Parsing and code generating context */
drh2133d822008-01-03 18:44:59 +00002076 Table *pTab, /* Description of the table we are reading from */
2077 int iColumn, /* Index of the table column */
2078 int iTable, /* The cursor pointing to the table */
drhda250ea2008-04-01 05:07:14 +00002079 int iReg, /* Store results here */
2080 int allowAffChng /* True if prior affinity changes are OK */
drh2133d822008-01-03 18:44:59 +00002081){
drhe55cbd72008-03-31 23:48:03 +00002082 Vdbe *v = pParse->pVdbe;
2083 int i;
drhda250ea2008-04-01 05:07:14 +00002084 struct yColCache *p;
drhe55cbd72008-03-31 23:48:03 +00002085
drhda250ea2008-04-01 05:07:14 +00002086 for(i=0, p=pParse->aColCache; i<pParse->nColCache; i++, p++){
2087 if( p->iTable==iTable && p->iColumn==iColumn
2088 && (!p->affChange || allowAffChng) ){
drhe55cbd72008-03-31 23:48:03 +00002089#if 0
2090 sqlite3VdbeAddOp0(v, OP_Noop);
drhda250ea2008-04-01 05:07:14 +00002091 VdbeComment((v, "OPT: tab%d.col%d -> r%d", iTable, iColumn, p->iReg));
drhe55cbd72008-03-31 23:48:03 +00002092#endif
drhda250ea2008-04-01 05:07:14 +00002093 return p->iReg;
drhe55cbd72008-03-31 23:48:03 +00002094 }
2095 }
2096 assert( v!=0 );
drh945498f2007-02-24 11:52:52 +00002097 if( iColumn<0 ){
2098 int op = (pTab && IsVirtual(pTab)) ? OP_VRowid : OP_Rowid;
drh2133d822008-01-03 18:44:59 +00002099 sqlite3VdbeAddOp2(v, op, iTable, iReg);
drh945498f2007-02-24 11:52:52 +00002100 }else if( pTab==0 ){
drh2133d822008-01-03 18:44:59 +00002101 sqlite3VdbeAddOp3(v, OP_Column, iTable, iColumn, iReg);
drh945498f2007-02-24 11:52:52 +00002102 }else{
2103 int op = IsVirtual(pTab) ? OP_VColumn : OP_Column;
drh2133d822008-01-03 18:44:59 +00002104 sqlite3VdbeAddOp3(v, op, iTable, iColumn, iReg);
drh945498f2007-02-24 11:52:52 +00002105 sqlite3ColumnDefault(v, pTab, iColumn);
2106#ifndef SQLITE_OMIT_FLOATING_POINT
2107 if( pTab->aCol[iColumn].affinity==SQLITE_AFF_REAL ){
drh2133d822008-01-03 18:44:59 +00002108 sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg);
drh945498f2007-02-24 11:52:52 +00002109 }
2110#endif
2111 }
drhe55cbd72008-03-31 23:48:03 +00002112 if( pParse->disableColCache==0 ){
2113 i = pParse->iColCache;
drhda250ea2008-04-01 05:07:14 +00002114 p = &pParse->aColCache[i];
2115 p->iTable = iTable;
2116 p->iColumn = iColumn;
2117 p->iReg = iReg;
drhc5499be2008-04-01 15:06:33 +00002118 p->affChange = 0;
drhe55cbd72008-03-31 23:48:03 +00002119 i++;
drh2f7794c2008-04-01 03:27:39 +00002120 if( i>=ArraySize(pParse->aColCache) ) i = 0;
drhe55cbd72008-03-31 23:48:03 +00002121 if( i>pParse->nColCache ) pParse->nColCache = i;
drh2f7794c2008-04-01 03:27:39 +00002122 pParse->iColCache = i;
drhe55cbd72008-03-31 23:48:03 +00002123 }
2124 return iReg;
2125}
2126
2127/*
drhe55cbd72008-03-31 23:48:03 +00002128** Clear all column cache entries associated with the vdbe
2129** cursor with cursor number iTable.
2130*/
2131void sqlite3ExprClearColumnCache(Parse *pParse, int iTable){
2132 if( iTable<0 ){
2133 pParse->nColCache = 0;
2134 pParse->iColCache = 0;
2135 }else{
2136 int i;
2137 for(i=0; i<pParse->nColCache; i++){
2138 if( pParse->aColCache[i].iTable==iTable ){
drhc5499be2008-04-01 15:06:33 +00002139 testcase( i==pParse->nColCache-1 );
drhe55cbd72008-03-31 23:48:03 +00002140 pParse->aColCache[i] = pParse->aColCache[--pParse->nColCache];
drhda250ea2008-04-01 05:07:14 +00002141 pParse->iColCache = pParse->nColCache;
drhe55cbd72008-03-31 23:48:03 +00002142 }
2143 }
drhe55cbd72008-03-31 23:48:03 +00002144 }
2145}
2146
2147/*
drhda250ea2008-04-01 05:07:14 +00002148** Record the fact that an affinity change has occurred on iCount
2149** registers starting with iStart.
drhe55cbd72008-03-31 23:48:03 +00002150*/
drhda250ea2008-04-01 05:07:14 +00002151void sqlite3ExprCacheAffinityChange(Parse *pParse, int iStart, int iCount){
2152 int iEnd = iStart + iCount - 1;
drhe55cbd72008-03-31 23:48:03 +00002153 int i;
2154 for(i=0; i<pParse->nColCache; i++){
2155 int r = pParse->aColCache[i].iReg;
drhda250ea2008-04-01 05:07:14 +00002156 if( r>=iStart && r<=iEnd ){
2157 pParse->aColCache[i].affChange = 1;
drhe55cbd72008-03-31 23:48:03 +00002158 }
2159 }
drhe55cbd72008-03-31 23:48:03 +00002160}
2161
2162/*
drhb21e7c72008-06-22 12:37:57 +00002163** Generate code to move content from registers iFrom...iFrom+nReg-1
2164** over to iTo..iTo+nReg-1. Keep the column cache up-to-date.
drhe55cbd72008-03-31 23:48:03 +00002165*/
drhb21e7c72008-06-22 12:37:57 +00002166void sqlite3ExprCodeMove(Parse *pParse, int iFrom, int iTo, int nReg){
drhe55cbd72008-03-31 23:48:03 +00002167 int i;
2168 if( iFrom==iTo ) return;
drhb21e7c72008-06-22 12:37:57 +00002169 sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move, iFrom, iTo, nReg);
drhe55cbd72008-03-31 23:48:03 +00002170 for(i=0; i<pParse->nColCache; i++){
drhb21e7c72008-06-22 12:37:57 +00002171 int x = pParse->aColCache[i].iReg;
2172 if( x>=iFrom && x<iFrom+nReg ){
2173 pParse->aColCache[i].iReg += iTo-iFrom;
drhe55cbd72008-03-31 23:48:03 +00002174 }
2175 }
drh945498f2007-02-24 11:52:52 +00002176}
2177
drhfec19aa2004-05-19 20:41:03 +00002178/*
drh92b01d52008-06-24 00:32:35 +00002179** Generate code to copy content from registers iFrom...iFrom+nReg-1
2180** over to iTo..iTo+nReg-1.
2181*/
2182void sqlite3ExprCodeCopy(Parse *pParse, int iFrom, int iTo, int nReg){
2183 int i;
2184 if( iFrom==iTo ) return;
2185 for(i=0; i<nReg; i++){
2186 sqlite3VdbeAddOp2(pParse->pVdbe, OP_Copy, iFrom+i, iTo+i);
2187 }
2188}
2189
2190/*
drh652fbf52008-04-01 01:42:41 +00002191** Return true if any register in the range iFrom..iTo (inclusive)
2192** is used as part of the column cache.
2193*/
2194static int usedAsColumnCache(Parse *pParse, int iFrom, int iTo){
2195 int i;
2196 for(i=0; i<pParse->nColCache; i++){
2197 int r = pParse->aColCache[i].iReg;
2198 if( r>=iFrom && r<=iTo ) return 1;
2199 }
2200 return 0;
2201}
2202
2203/*
2204** Theres is a value in register iCurrent. We ultimately want
2205** the value to be in register iTarget. It might be that
2206** iCurrent and iTarget are the same register.
2207**
2208** We are going to modify the value, so we need to make sure it
2209** is not a cached register. If iCurrent is a cached register,
2210** then try to move the value over to iTarget. If iTarget is a
2211** cached register, then clear the corresponding cache line.
2212**
2213** Return the register that the value ends up in.
2214*/
2215int sqlite3ExprWritableRegister(Parse *pParse, int iCurrent, int iTarget){
drhda250ea2008-04-01 05:07:14 +00002216 int i;
drh652fbf52008-04-01 01:42:41 +00002217 assert( pParse->pVdbe!=0 );
2218 if( !usedAsColumnCache(pParse, iCurrent, iCurrent) ){
2219 return iCurrent;
2220 }
drh2f7794c2008-04-01 03:27:39 +00002221 if( iCurrent!=iTarget ){
2222 sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, iCurrent, iTarget);
2223 }
drhda250ea2008-04-01 05:07:14 +00002224 for(i=0; i<pParse->nColCache; i++){
2225 if( pParse->aColCache[i].iReg==iTarget ){
2226 pParse->aColCache[i] = pParse->aColCache[--pParse->nColCache];
2227 pParse->iColCache = pParse->nColCache;
2228 }
2229 }
drh652fbf52008-04-01 01:42:41 +00002230 return iTarget;
2231}
2232
2233/*
drh191b54c2008-04-15 12:14:21 +00002234** If the last instruction coded is an ephemeral copy of any of
2235** the registers in the nReg registers beginning with iReg, then
2236** convert the last instruction from OP_SCopy to OP_Copy.
2237*/
2238void sqlite3ExprHardCopy(Parse *pParse, int iReg, int nReg){
2239 int addr;
2240 VdbeOp *pOp;
2241 Vdbe *v;
2242
2243 v = pParse->pVdbe;
2244 addr = sqlite3VdbeCurrentAddr(v);
2245 pOp = sqlite3VdbeGetOp(v, addr-1);
danielk1977d7eb2ed2008-04-24 12:36:35 +00002246 assert( pOp || pParse->db->mallocFailed );
2247 if( pOp && pOp->opcode==OP_SCopy && pOp->p1>=iReg && pOp->p1<iReg+nReg ){
drh191b54c2008-04-15 12:14:21 +00002248 pOp->opcode = OP_Copy;
2249 }
2250}
2251
2252/*
drhcce7d172000-05-31 15:34:51 +00002253** Generate code into the current Vdbe to evaluate the given
drh2dcef112008-01-12 19:03:48 +00002254** expression. Attempt to store the results in register "target".
2255** Return the register where results are stored.
drh389a1ad2008-01-03 23:44:53 +00002256**
drh2dcef112008-01-12 19:03:48 +00002257** With this routine, there is no guaranteed that results will
2258** be stored in target. The result might be stored in some other
2259** register if it is convenient to do so. The calling function
2260** must check the return code and move the results to the desired
2261** register.
drhcce7d172000-05-31 15:34:51 +00002262*/
drh678ccce2008-03-31 18:19:54 +00002263int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){
drh2dcef112008-01-12 19:03:48 +00002264 Vdbe *v = pParse->pVdbe; /* The VM under construction */
2265 int op; /* The opcode being coded */
2266 int inReg = target; /* Results stored in register inReg */
2267 int regFree1 = 0; /* If non-zero free this temporary register */
2268 int regFree2 = 0; /* If non-zero free this temporary register */
drh678ccce2008-03-31 18:19:54 +00002269 int r1, r2, r3, r4; /* Various register numbers */
drhffe07b22005-11-03 00:41:17 +00002270
drh389a1ad2008-01-03 23:44:53 +00002271 assert( v!=0 || pParse->db->mallocFailed );
drh9cbf3422008-01-17 16:22:13 +00002272 assert( target>0 && target<=pParse->nMem );
drh389a1ad2008-01-03 23:44:53 +00002273 if( v==0 ) return 0;
drh389a1ad2008-01-03 23:44:53 +00002274
2275 if( pExpr==0 ){
2276 op = TK_NULL;
2277 }else{
2278 op = pExpr->op;
2279 }
drhf2bc0132004-10-04 13:19:23 +00002280 switch( op ){
drh13449892005-09-07 21:22:45 +00002281 case TK_AGG_COLUMN: {
2282 AggInfo *pAggInfo = pExpr->pAggInfo;
2283 struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg];
2284 if( !pAggInfo->directMode ){
drh9de221d2008-01-05 06:51:30 +00002285 assert( pCol->iMem>0 );
2286 inReg = pCol->iMem;
drh13449892005-09-07 21:22:45 +00002287 break;
2288 }else if( pAggInfo->useSortingIdx ){
drh389a1ad2008-01-03 23:44:53 +00002289 sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdx,
2290 pCol->iSorterColumn, target);
drh13449892005-09-07 21:22:45 +00002291 break;
2292 }
2293 /* Otherwise, fall thru into the TK_COLUMN case */
2294 }
drh967e8b72000-06-21 13:59:10 +00002295 case TK_COLUMN: {
drhffe07b22005-11-03 00:41:17 +00002296 if( pExpr->iTable<0 ){
2297 /* This only happens when coding check constraints */
drhaa9b8962008-01-08 02:57:55 +00002298 assert( pParse->ckBase>0 );
2299 inReg = pExpr->iColumn + pParse->ckBase;
drhc4a3c772001-04-04 11:48:57 +00002300 }else{
drhc5499be2008-04-01 15:06:33 +00002301 testcase( (pExpr->flags & EP_AnyAff)!=0 );
drhe55cbd72008-03-31 23:48:03 +00002302 inReg = sqlite3ExprCodeGetColumn(pParse, pExpr->pTab,
drhda250ea2008-04-01 05:07:14 +00002303 pExpr->iColumn, pExpr->iTable, target,
2304 pExpr->flags & EP_AnyAff);
drh22827922000-06-06 17:27:05 +00002305 }
drhcce7d172000-05-31 15:34:51 +00002306 break;
2307 }
2308 case TK_INTEGER: {
drh92b01d52008-06-24 00:32:35 +00002309 codeInteger(v, pExpr, 0, target);
drhfec19aa2004-05-19 20:41:03 +00002310 break;
2311 }
drh598f1342007-10-23 15:39:45 +00002312 case TK_FLOAT: {
drh9de221d2008-01-05 06:51:30 +00002313 codeReal(v, (char*)pExpr->token.z, pExpr->token.n, 0, target);
drh598f1342007-10-23 15:39:45 +00002314 break;
2315 }
drhfec19aa2004-05-19 20:41:03 +00002316 case TK_STRING: {
danielk19771e536952007-08-16 10:09:01 +00002317 sqlite3DequoteExpr(pParse->db, pExpr);
drh9de221d2008-01-05 06:51:30 +00002318 sqlite3VdbeAddOp4(v,OP_String8, 0, target, 0,
drh66a51672008-01-03 00:01:23 +00002319 (char*)pExpr->token.z, pExpr->token.n);
drhcce7d172000-05-31 15:34:51 +00002320 break;
2321 }
drhf0863fe2005-06-12 21:35:51 +00002322 case TK_NULL: {
drh9de221d2008-01-05 06:51:30 +00002323 sqlite3VdbeAddOp2(v, OP_Null, 0, target);
drhf0863fe2005-06-12 21:35:51 +00002324 break;
2325 }
danielk19775338a5f2005-01-20 13:03:10 +00002326#ifndef SQLITE_OMIT_BLOB_LITERAL
danielk1977c572ef72004-05-27 09:28:41 +00002327 case TK_BLOB: {
drh6c8c6ce2005-08-23 11:17:58 +00002328 int n;
2329 const char *z;
drhca48c902008-01-18 14:08:24 +00002330 char *zBlob;
2331 assert( pExpr->token.n>=3 );
2332 assert( pExpr->token.z[0]=='x' || pExpr->token.z[0]=='X' );
2333 assert( pExpr->token.z[1]=='\'' );
2334 assert( pExpr->token.z[pExpr->token.n-1]=='\'' );
drh6c8c6ce2005-08-23 11:17:58 +00002335 n = pExpr->token.n - 3;
drh2646da72005-12-09 20:02:05 +00002336 z = (char*)pExpr->token.z + 2;
drhca48c902008-01-18 14:08:24 +00002337 zBlob = sqlite3HexToBlob(sqlite3VdbeDb(v), z, n);
2338 sqlite3VdbeAddOp4(v, OP_Blob, n/2, target, 0, zBlob, P4_DYNAMIC);
danielk1977c572ef72004-05-27 09:28:41 +00002339 break;
2340 }
danielk19775338a5f2005-01-20 13:03:10 +00002341#endif
drh50457892003-09-06 01:10:47 +00002342 case TK_VARIABLE: {
drh9de221d2008-01-05 06:51:30 +00002343 sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iTable, target);
drh895d7472004-08-20 16:02:39 +00002344 if( pExpr->token.n>1 ){
drh66a51672008-01-03 00:01:23 +00002345 sqlite3VdbeChangeP4(v, -1, (char*)pExpr->token.z, pExpr->token.n);
drh895d7472004-08-20 16:02:39 +00002346 }
drh50457892003-09-06 01:10:47 +00002347 break;
2348 }
drh4e0cff62004-11-05 05:10:28 +00002349 case TK_REGISTER: {
drh9de221d2008-01-05 06:51:30 +00002350 inReg = pExpr->iTable;
drh4e0cff62004-11-05 05:10:28 +00002351 break;
2352 }
drh487e2622005-06-25 18:42:14 +00002353#ifndef SQLITE_OMIT_CAST
2354 case TK_CAST: {
2355 /* Expressions of the form: CAST(pLeft AS token) */
danielk1977f0113002006-01-24 12:09:17 +00002356 int aff, to_op;
drh2dcef112008-01-12 19:03:48 +00002357 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
drh8a512562005-11-14 22:29:05 +00002358 aff = sqlite3AffinityType(&pExpr->token);
danielk1977f0113002006-01-24 12:09:17 +00002359 to_op = aff - SQLITE_AFF_TEXT + OP_ToText;
2360 assert( to_op==OP_ToText || aff!=SQLITE_AFF_TEXT );
2361 assert( to_op==OP_ToBlob || aff!=SQLITE_AFF_NONE );
2362 assert( to_op==OP_ToNumeric || aff!=SQLITE_AFF_NUMERIC );
2363 assert( to_op==OP_ToInt || aff!=SQLITE_AFF_INTEGER );
2364 assert( to_op==OP_ToReal || aff!=SQLITE_AFF_REAL );
drhc5499be2008-04-01 15:06:33 +00002365 testcase( to_op==OP_ToText );
2366 testcase( to_op==OP_ToBlob );
2367 testcase( to_op==OP_ToNumeric );
2368 testcase( to_op==OP_ToInt );
2369 testcase( to_op==OP_ToReal );
drh2dcef112008-01-12 19:03:48 +00002370 sqlite3VdbeAddOp1(v, to_op, inReg);
drhc5499be2008-04-01 15:06:33 +00002371 testcase( usedAsColumnCache(pParse, inReg, inReg) );
drhb3843a82008-04-01 12:24:11 +00002372 sqlite3ExprCacheAffinityChange(pParse, inReg, 1);
drh487e2622005-06-25 18:42:14 +00002373 break;
2374 }
2375#endif /* SQLITE_OMIT_CAST */
drhc9b84a12002-06-20 11:36:48 +00002376 case TK_LT:
2377 case TK_LE:
2378 case TK_GT:
2379 case TK_GE:
2380 case TK_NE:
2381 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00002382 assert( TK_LT==OP_Lt );
2383 assert( TK_LE==OP_Le );
2384 assert( TK_GT==OP_Gt );
2385 assert( TK_GE==OP_Ge );
2386 assert( TK_EQ==OP_Eq );
2387 assert( TK_NE==OP_Ne );
drhc5499be2008-04-01 15:06:33 +00002388 testcase( op==TK_LT );
2389 testcase( op==TK_LE );
2390 testcase( op==TK_GT );
2391 testcase( op==TK_GE );
2392 testcase( op==TK_EQ );
2393 testcase( op==TK_NE );
drhda250ea2008-04-01 05:07:14 +00002394 codeCompareOperands(pParse, pExpr->pLeft, &r1, &regFree1,
2395 pExpr->pRight, &r2, &regFree2);
drh35573352008-01-08 23:54:25 +00002396 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
2397 r1, r2, inReg, SQLITE_STOREP2);
drhc5499be2008-04-01 15:06:33 +00002398 testcase( regFree1==0 );
2399 testcase( regFree2==0 );
danielk1977a37cdde2004-05-16 11:15:36 +00002400 break;
drhc9b84a12002-06-20 11:36:48 +00002401 }
drhcce7d172000-05-31 15:34:51 +00002402 case TK_AND:
2403 case TK_OR:
2404 case TK_PLUS:
2405 case TK_STAR:
2406 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00002407 case TK_REM:
2408 case TK_BITAND:
2409 case TK_BITOR:
drh17c40292004-07-21 02:53:29 +00002410 case TK_SLASH:
drhbf4133c2001-10-13 02:59:08 +00002411 case TK_LSHIFT:
drh855eb1c2004-08-31 13:45:11 +00002412 case TK_RSHIFT:
drh00400772000-06-16 20:51:26 +00002413 case TK_CONCAT: {
drhf2bc0132004-10-04 13:19:23 +00002414 assert( TK_AND==OP_And );
2415 assert( TK_OR==OP_Or );
2416 assert( TK_PLUS==OP_Add );
2417 assert( TK_MINUS==OP_Subtract );
2418 assert( TK_REM==OP_Remainder );
2419 assert( TK_BITAND==OP_BitAnd );
2420 assert( TK_BITOR==OP_BitOr );
2421 assert( TK_SLASH==OP_Divide );
2422 assert( TK_LSHIFT==OP_ShiftLeft );
2423 assert( TK_RSHIFT==OP_ShiftRight );
2424 assert( TK_CONCAT==OP_Concat );
drhc5499be2008-04-01 15:06:33 +00002425 testcase( op==TK_AND );
2426 testcase( op==TK_OR );
2427 testcase( op==TK_PLUS );
2428 testcase( op==TK_MINUS );
2429 testcase( op==TK_REM );
2430 testcase( op==TK_BITAND );
2431 testcase( op==TK_BITOR );
2432 testcase( op==TK_SLASH );
2433 testcase( op==TK_LSHIFT );
2434 testcase( op==TK_RSHIFT );
2435 testcase( op==TK_CONCAT );
drh2dcef112008-01-12 19:03:48 +00002436 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
2437 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
drh5b6afba2008-01-05 16:29:28 +00002438 sqlite3VdbeAddOp3(v, op, r2, r1, target);
drhc5499be2008-04-01 15:06:33 +00002439 testcase( regFree1==0 );
2440 testcase( regFree2==0 );
drh00400772000-06-16 20:51:26 +00002441 break;
2442 }
drhcce7d172000-05-31 15:34:51 +00002443 case TK_UMINUS: {
drhfec19aa2004-05-19 20:41:03 +00002444 Expr *pLeft = pExpr->pLeft;
2445 assert( pLeft );
2446 if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
drhfec19aa2004-05-19 20:41:03 +00002447 if( pLeft->op==TK_FLOAT ){
drh92b01d52008-06-24 00:32:35 +00002448 codeReal(v, (char*)pLeft->token.z, pLeft->token.n, 1, target);
drhe6840902002-03-06 03:08:25 +00002449 }else{
drh92b01d52008-06-24 00:32:35 +00002450 codeInteger(v, pLeft, 1, target);
drhe6840902002-03-06 03:08:25 +00002451 }
drh3c84ddf2008-01-09 02:15:38 +00002452 }else{
drh2dcef112008-01-12 19:03:48 +00002453 regFree1 = r1 = sqlite3GetTempReg(pParse);
drh3c84ddf2008-01-09 02:15:38 +00002454 sqlite3VdbeAddOp2(v, OP_Integer, 0, r1);
drhe55cbd72008-03-31 23:48:03 +00002455 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree2);
drh2dcef112008-01-12 19:03:48 +00002456 sqlite3VdbeAddOp3(v, OP_Subtract, r2, r1, target);
drhc5499be2008-04-01 15:06:33 +00002457 testcase( regFree2==0 );
drh6e142f52000-06-08 13:36:40 +00002458 }
drh3c84ddf2008-01-09 02:15:38 +00002459 inReg = target;
2460 break;
drh6e142f52000-06-08 13:36:40 +00002461 }
drhbf4133c2001-10-13 02:59:08 +00002462 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00002463 case TK_NOT: {
drhf2bc0132004-10-04 13:19:23 +00002464 assert( TK_BITNOT==OP_BitNot );
2465 assert( TK_NOT==OP_Not );
drhc5499be2008-04-01 15:06:33 +00002466 testcase( op==TK_BITNOT );
2467 testcase( op==TK_NOT );
drh2dcef112008-01-12 19:03:48 +00002468 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
drhc5499be2008-04-01 15:06:33 +00002469 testcase( inReg==target );
2470 testcase( usedAsColumnCache(pParse, inReg, inReg) );
drh652fbf52008-04-01 01:42:41 +00002471 inReg = sqlite3ExprWritableRegister(pParse, inReg, target);
drh2dcef112008-01-12 19:03:48 +00002472 sqlite3VdbeAddOp1(v, op, inReg);
drhcce7d172000-05-31 15:34:51 +00002473 break;
2474 }
2475 case TK_ISNULL:
2476 case TK_NOTNULL: {
drh6a288a32008-01-07 19:20:24 +00002477 int addr;
drhf2bc0132004-10-04 13:19:23 +00002478 assert( TK_ISNULL==OP_IsNull );
2479 assert( TK_NOTNULL==OP_NotNull );
drhc5499be2008-04-01 15:06:33 +00002480 testcase( op==TK_ISNULL );
2481 testcase( op==TK_NOTNULL );
drh9de221d2008-01-05 06:51:30 +00002482 sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
drh2dcef112008-01-12 19:03:48 +00002483 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
drhc5499be2008-04-01 15:06:33 +00002484 testcase( regFree1==0 );
drh2dcef112008-01-12 19:03:48 +00002485 addr = sqlite3VdbeAddOp1(v, op, r1);
drh9de221d2008-01-05 06:51:30 +00002486 sqlite3VdbeAddOp2(v, OP_AddImm, target, -1);
drh6a288a32008-01-07 19:20:24 +00002487 sqlite3VdbeJumpHere(v, addr);
drhf2bc0132004-10-04 13:19:23 +00002488 break;
drhcce7d172000-05-31 15:34:51 +00002489 }
drh22827922000-06-06 17:27:05 +00002490 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00002491 AggInfo *pInfo = pExpr->pAggInfo;
drh7e56e712005-11-16 12:53:15 +00002492 if( pInfo==0 ){
2493 sqlite3ErrorMsg(pParse, "misuse of aggregate: %T",
2494 &pExpr->span);
2495 }else{
drh9de221d2008-01-05 06:51:30 +00002496 inReg = pInfo->aFunc[pExpr->iAgg].iMem;
drh7e56e712005-11-16 12:53:15 +00002497 }
drh22827922000-06-06 17:27:05 +00002498 break;
2499 }
drhb71090f2005-05-23 17:26:51 +00002500 case TK_CONST_FUNC:
drhcce7d172000-05-31 15:34:51 +00002501 case TK_FUNCTION: {
drhcce7d172000-05-31 15:34:51 +00002502 ExprList *pList = pExpr->pList;
drh89425d52002-02-28 03:04:48 +00002503 int nExpr = pList ? pList->nExpr : 0;
drh0bce8352002-02-28 00:41:10 +00002504 FuncDef *pDef;
drh4b59ab52002-08-24 18:24:51 +00002505 int nId;
2506 const char *zId;
drh13449892005-09-07 21:22:45 +00002507 int constMask = 0;
danielk1977682f68b2004-06-05 10:22:17 +00002508 int i;
drh17435752007-08-16 04:30:38 +00002509 sqlite3 *db = pParse->db;
2510 u8 enc = ENC(db);
danielk1977dc1bdc42004-06-11 10:51:27 +00002511 CollSeq *pColl = 0;
drh17435752007-08-16 04:30:38 +00002512
drhc5499be2008-04-01 15:06:33 +00002513 testcase( op==TK_CONST_FUNC );
2514 testcase( op==TK_FUNCTION );
drh2646da72005-12-09 20:02:05 +00002515 zId = (char*)pExpr->token.z;
drhb71090f2005-05-23 17:26:51 +00002516 nId = pExpr->token.n;
danielk1977d8123362004-06-12 09:25:12 +00002517 pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0);
drh0bce8352002-02-28 00:41:10 +00002518 assert( pDef!=0 );
drh892d3172008-01-10 03:46:36 +00002519 if( pList ){
2520 nExpr = pList->nExpr;
drh2dcef112008-01-12 19:03:48 +00002521 r1 = sqlite3GetTempRange(pParse, nExpr);
drh191b54c2008-04-15 12:14:21 +00002522 sqlite3ExprCodeExprList(pParse, pList, r1, 1);
drh892d3172008-01-10 03:46:36 +00002523 }else{
drhd847eaa2008-01-17 17:15:56 +00002524 nExpr = r1 = 0;
drh892d3172008-01-10 03:46:36 +00002525 }
drhb7f6f682006-07-08 17:06:43 +00002526#ifndef SQLITE_OMIT_VIRTUALTABLE
drha43fa222006-07-08 18:41:37 +00002527 /* Possibly overload the function if the first argument is
2528 ** a virtual table column.
2529 **
2530 ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the
2531 ** second argument, not the first, as the argument to test to
2532 ** see if it is a column in a virtual table. This is done because
2533 ** the left operand of infix functions (the operand we want to
2534 ** control overloading) ends up as the second argument to the
2535 ** function. The expression "A glob B" is equivalent to
2536 ** "glob(B,A). We want to use the A in "A glob B" to test
2537 ** for function overloading. But we use the B term in "glob(B,A)".
2538 */
drh6a03a1c2006-07-08 18:34:59 +00002539 if( nExpr>=2 && (pExpr->flags & EP_InfixFunc) ){
drh17435752007-08-16 04:30:38 +00002540 pDef = sqlite3VtabOverloadFunction(db, pDef, nExpr, pList->a[1].pExpr);
drh6a03a1c2006-07-08 18:34:59 +00002541 }else if( nExpr>0 ){
drh17435752007-08-16 04:30:38 +00002542 pDef = sqlite3VtabOverloadFunction(db, pDef, nExpr, pList->a[0].pExpr);
drhb7f6f682006-07-08 17:06:43 +00002543 }
2544#endif
danielk1977682f68b2004-06-05 10:22:17 +00002545 for(i=0; i<nExpr && i<32; i++){
danielk1977d02eb1f2004-06-06 09:44:03 +00002546 if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
drh13449892005-09-07 21:22:45 +00002547 constMask |= (1<<i);
danielk1977d02eb1f2004-06-06 09:44:03 +00002548 }
danielk1977dc1bdc42004-06-11 10:51:27 +00002549 if( pDef->needCollSeq && !pColl ){
2550 pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
2551 }
2552 }
2553 if( pDef->needCollSeq ){
2554 if( !pColl ) pColl = pParse->db->pDfltColl;
drh66a51672008-01-03 00:01:23 +00002555 sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ);
danielk1977682f68b2004-06-05 10:22:17 +00002556 }
drh2dcef112008-01-12 19:03:48 +00002557 sqlite3VdbeAddOp4(v, OP_Function, constMask, r1, target,
drh66a51672008-01-03 00:01:23 +00002558 (char*)pDef, P4_FUNCDEF);
drh98757152008-01-09 23:04:12 +00002559 sqlite3VdbeChangeP5(v, nExpr);
drh2dcef112008-01-12 19:03:48 +00002560 if( nExpr ){
2561 sqlite3ReleaseTempRange(pParse, r1, nExpr);
2562 }
drhda250ea2008-04-01 05:07:14 +00002563 sqlite3ExprCacheAffinityChange(pParse, r1, nExpr);
drhcce7d172000-05-31 15:34:51 +00002564 break;
2565 }
drhfe2093d2005-01-20 22:48:47 +00002566#ifndef SQLITE_OMIT_SUBQUERY
2567 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00002568 case TK_SELECT: {
drhc5499be2008-04-01 15:06:33 +00002569 testcase( op==TK_EXISTS );
2570 testcase( op==TK_SELECT );
drh41714d62006-03-02 04:44:23 +00002571 if( pExpr->iColumn==0 ){
danielk19770cdc0222008-06-26 18:04:03 +00002572 sqlite3CodeSubselect(pParse, pExpr, 0);
drh41714d62006-03-02 04:44:23 +00002573 }
drh9de221d2008-01-05 06:51:30 +00002574 inReg = pExpr->iColumn;
drh19a775c2000-06-05 18:54:46 +00002575 break;
2576 }
drhfef52082000-06-06 01:50:43 +00002577 case TK_IN: {
danielk19770cdc0222008-06-26 18:04:03 +00002578 int rNotFound = 0;
2579 int rMayHaveNull = 0;
drh6fccc352008-06-27 00:52:45 +00002580 int j2, j3, j4, j5;
drh94a11212004-09-25 13:12:14 +00002581 char affinity;
danielk19779a96b662007-11-29 17:05:18 +00002582 int eType;
danielk19779a96b662007-11-29 17:05:18 +00002583
drh3c31fc22008-06-26 21:45:26 +00002584 VdbeNoopComment((v, "begin IN expr r%d", target));
danielk19770cdc0222008-06-26 18:04:03 +00002585 eType = sqlite3FindInIndex(pParse, pExpr, &rMayHaveNull);
2586 if( rMayHaveNull ){
2587 rNotFound = ++pParse->nMem;
2588 }
danielk1977e014a832004-05-17 10:48:57 +00002589
2590 /* Figure out the affinity to use to create a key from the results
2591 ** of the expression. affinityStr stores a static string suitable for
drh66a51672008-01-03 00:01:23 +00002592 ** P4 of OP_MakeRecord.
danielk1977e014a832004-05-17 10:48:57 +00002593 */
drh94a11212004-09-25 13:12:14 +00002594 affinity = comparisonAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +00002595
danielk1977e014a832004-05-17 10:48:57 +00002596
2597 /* Code the <expr> from "<expr> IN (...)". The temporary table
2598 ** pExpr->iTable contains the values that make up the (...) set.
2599 */
drh66ba23c2008-06-27 00:47:28 +00002600 pParse->disableColCache++;
2601 sqlite3ExprCode(pParse, pExpr->pLeft, target);
2602 pParse->disableColCache--;
2603 j2 = sqlite3VdbeAddOp1(v, OP_IsNull, target);
danielk19779a96b662007-11-29 17:05:18 +00002604 if( eType==IN_INDEX_ROWID ){
drh66ba23c2008-06-27 00:47:28 +00002605 j3 = sqlite3VdbeAddOp1(v, OP_MustBeInt, target);
2606 j4 = sqlite3VdbeAddOp3(v, OP_NotExists, pExpr->iTable, 0, target);
2607 sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
drh6a288a32008-01-07 19:20:24 +00002608 j5 = sqlite3VdbeAddOp0(v, OP_Goto);
2609 sqlite3VdbeJumpHere(v, j3);
2610 sqlite3VdbeJumpHere(v, j4);
danielk19770cdc0222008-06-26 18:04:03 +00002611 sqlite3VdbeAddOp2(v, OP_Integer, 0, target);
danielk19779a96b662007-11-29 17:05:18 +00002612 }else{
drh2dcef112008-01-12 19:03:48 +00002613 r2 = regFree2 = sqlite3GetTempReg(pParse);
danielk19770cdc0222008-06-26 18:04:03 +00002614
2615 /* Create a record and test for set membership. If the set contains
2616 ** the value, then jump to the end of the test code. The target
2617 ** register still contains the true (1) value written to it earlier.
2618 */
drh66ba23c2008-06-27 00:47:28 +00002619 sqlite3VdbeAddOp4(v, OP_MakeRecord, target, 1, r2, &affinity, 1);
2620 sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
drh2dcef112008-01-12 19:03:48 +00002621 j5 = sqlite3VdbeAddOp3(v, OP_Found, pExpr->iTable, 0, r2);
danielk19770cdc0222008-06-26 18:04:03 +00002622
2623 /* If the set membership test fails, then the result of the
2624 ** "x IN (...)" expression must be either 0 or NULL. If the set
2625 ** contains no NULL values, then the result is 0. If the set
2626 ** contains one or more NULL values, then the result of the
2627 ** expression is also NULL.
2628 */
2629 if( rNotFound==0 ){
2630 /* This branch runs if it is known at compile time (now) that
2631 ** the set contains no NULL values. This happens as the result
2632 ** of a "NOT NULL" constraint in the database schema. No need
2633 ** to test the data structure at runtime in this case.
2634 */
2635 sqlite3VdbeAddOp2(v, OP_Integer, 0, target);
2636 }else{
2637 /* This block populates the rNotFound register with either NULL
2638 ** or 0 (an integer value). If the data structure contains one
2639 ** or more NULLs, then set rNotFound to NULL. Otherwise, set it
2640 ** to 0. If register rMayHaveNull is already set to some value
2641 ** other than NULL, then the test has already been run and
2642 ** rNotFound is already populated.
2643 */
drh66ba23c2008-06-27 00:47:28 +00002644 static const char nullRecord[] = { 0x02, 0x00 };
danielk19770cdc0222008-06-26 18:04:03 +00002645 j3 = sqlite3VdbeAddOp1(v, OP_NotNull, rMayHaveNull);
2646 sqlite3VdbeAddOp2(v, OP_Null, 0, rNotFound);
drh66ba23c2008-06-27 00:47:28 +00002647 sqlite3VdbeAddOp4(v, OP_Blob, 2, rMayHaveNull, 0,
2648 nullRecord, P4_STATIC);
2649 j4 = sqlite3VdbeAddOp3(v, OP_Found, pExpr->iTable, 0, rMayHaveNull);
danielk19770cdc0222008-06-26 18:04:03 +00002650 sqlite3VdbeAddOp2(v, OP_Integer, 0, rNotFound);
2651 sqlite3VdbeJumpHere(v, j4);
2652 sqlite3VdbeJumpHere(v, j3);
2653
2654 /* Copy the value of register rNotFound (which is either NULL or 0)
2655 ** into the target register. This will be the result of the
2656 ** expression.
2657 */
2658 sqlite3VdbeAddOp2(v, OP_Copy, rNotFound, target);
2659 }
danielk19779a96b662007-11-29 17:05:18 +00002660 }
drh6a288a32008-01-07 19:20:24 +00002661 sqlite3VdbeJumpHere(v, j2);
2662 sqlite3VdbeJumpHere(v, j5);
drh3c31fc22008-06-26 21:45:26 +00002663 VdbeComment((v, "end IN expr r%d", target));
drhfef52082000-06-06 01:50:43 +00002664 break;
2665 }
danielk197793758c82005-01-21 08:13:14 +00002666#endif
drh2dcef112008-01-12 19:03:48 +00002667 /*
2668 ** x BETWEEN y AND z
2669 **
2670 ** This is equivalent to
2671 **
2672 ** x>=y AND x<=z
2673 **
2674 ** X is stored in pExpr->pLeft.
2675 ** Y is stored in pExpr->pList->a[0].pExpr.
2676 ** Z is stored in pExpr->pList->a[1].pExpr.
2677 */
drhfef52082000-06-06 01:50:43 +00002678 case TK_BETWEEN: {
drhbe5c89a2004-07-26 00:31:09 +00002679 Expr *pLeft = pExpr->pLeft;
2680 struct ExprList_item *pLItem = pExpr->pList->a;
2681 Expr *pRight = pLItem->pExpr;
drh35573352008-01-08 23:54:25 +00002682
drhda250ea2008-04-01 05:07:14 +00002683 codeCompareOperands(pParse, pLeft, &r1, &regFree1,
2684 pRight, &r2, &regFree2);
drhc5499be2008-04-01 15:06:33 +00002685 testcase( regFree1==0 );
2686 testcase( regFree2==0 );
drh2dcef112008-01-12 19:03:48 +00002687 r3 = sqlite3GetTempReg(pParse);
drh678ccce2008-03-31 18:19:54 +00002688 r4 = sqlite3GetTempReg(pParse);
drh35573352008-01-08 23:54:25 +00002689 codeCompare(pParse, pLeft, pRight, OP_Ge,
2690 r1, r2, r3, SQLITE_STOREP2);
drhbe5c89a2004-07-26 00:31:09 +00002691 pLItem++;
2692 pRight = pLItem->pExpr;
drh2dcef112008-01-12 19:03:48 +00002693 sqlite3ReleaseTempReg(pParse, regFree2);
2694 r2 = sqlite3ExprCodeTemp(pParse, pRight, &regFree2);
drhc5499be2008-04-01 15:06:33 +00002695 testcase( regFree2==0 );
drh678ccce2008-03-31 18:19:54 +00002696 codeCompare(pParse, pLeft, pRight, OP_Le, r1, r2, r4, SQLITE_STOREP2);
2697 sqlite3VdbeAddOp3(v, OP_And, r3, r4, target);
drh2dcef112008-01-12 19:03:48 +00002698 sqlite3ReleaseTempReg(pParse, r3);
drh678ccce2008-03-31 18:19:54 +00002699 sqlite3ReleaseTempReg(pParse, r4);
drhfef52082000-06-06 01:50:43 +00002700 break;
2701 }
drh4f07e5f2007-05-14 11:34:46 +00002702 case TK_UPLUS: {
drh2dcef112008-01-12 19:03:48 +00002703 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
drha2e00042002-01-22 03:13:42 +00002704 break;
2705 }
drh2dcef112008-01-12 19:03:48 +00002706
2707 /*
2708 ** Form A:
2709 ** CASE x WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
2710 **
2711 ** Form B:
2712 ** CASE WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
2713 **
2714 ** Form A is can be transformed into the equivalent form B as follows:
2715 ** CASE WHEN x=e1 THEN r1 WHEN x=e2 THEN r2 ...
2716 ** WHEN x=eN THEN rN ELSE y END
2717 **
2718 ** X (if it exists) is in pExpr->pLeft.
2719 ** Y is in pExpr->pRight. The Y is also optional. If there is no
2720 ** ELSE clause and no other term matches, then the result of the
2721 ** exprssion is NULL.
2722 ** Ei is in pExpr->pList->a[i*2] and Ri is pExpr->pList->a[i*2+1].
2723 **
2724 ** The result of the expression is the Ri for the first matching Ei,
2725 ** or if there is no matching Ei, the ELSE term Y, or if there is
2726 ** no ELSE term, NULL.
2727 */
drh17a7f8d2002-03-24 13:13:27 +00002728 case TK_CASE: {
drh2dcef112008-01-12 19:03:48 +00002729 int endLabel; /* GOTO label for end of CASE stmt */
2730 int nextCase; /* GOTO label for next WHEN clause */
2731 int nExpr; /* 2x number of WHEN terms */
2732 int i; /* Loop counter */
2733 ExprList *pEList; /* List of WHEN terms */
2734 struct ExprList_item *aListelem; /* Array of WHEN terms */
2735 Expr opCompare; /* The X==Ei expression */
2736 Expr cacheX; /* Cached expression X */
2737 Expr *pX; /* The X expression */
2738 Expr *pTest; /* X==Ei (form A) or just Ei (form B) */
drh17a7f8d2002-03-24 13:13:27 +00002739
2740 assert(pExpr->pList);
2741 assert((pExpr->pList->nExpr % 2) == 0);
2742 assert(pExpr->pList->nExpr > 0);
drhbe5c89a2004-07-26 00:31:09 +00002743 pEList = pExpr->pList;
2744 aListelem = pEList->a;
2745 nExpr = pEList->nExpr;
drh2dcef112008-01-12 19:03:48 +00002746 endLabel = sqlite3VdbeMakeLabel(v);
2747 if( (pX = pExpr->pLeft)!=0 ){
2748 cacheX = *pX;
drhc5499be2008-04-01 15:06:33 +00002749 testcase( pX->op==TK_COLUMN || pX->op==TK_REGISTER );
drh2dcef112008-01-12 19:03:48 +00002750 cacheX.iTable = sqlite3ExprCodeTemp(pParse, pX, &regFree1);
drhc5499be2008-04-01 15:06:33 +00002751 testcase( regFree1==0 );
drh2dcef112008-01-12 19:03:48 +00002752 cacheX.op = TK_REGISTER;
drh678ccce2008-03-31 18:19:54 +00002753 cacheX.iColumn = 0;
drh2dcef112008-01-12 19:03:48 +00002754 opCompare.op = TK_EQ;
2755 opCompare.pLeft = &cacheX;
2756 pTest = &opCompare;
drh17a7f8d2002-03-24 13:13:27 +00002757 }
drhc5499be2008-04-01 15:06:33 +00002758 pParse->disableColCache++;
drhf5905aa2002-05-26 20:54:33 +00002759 for(i=0; i<nExpr; i=i+2){
drh2dcef112008-01-12 19:03:48 +00002760 if( pX ){
2761 opCompare.pRight = aListelem[i].pExpr;
drh17a7f8d2002-03-24 13:13:27 +00002762 }else{
drh2dcef112008-01-12 19:03:48 +00002763 pTest = aListelem[i].pExpr;
drh17a7f8d2002-03-24 13:13:27 +00002764 }
drh2dcef112008-01-12 19:03:48 +00002765 nextCase = sqlite3VdbeMakeLabel(v);
drhc5499be2008-04-01 15:06:33 +00002766 testcase( pTest->op==TK_COLUMN || pTest->op==TK_REGISTER );
drh2dcef112008-01-12 19:03:48 +00002767 sqlite3ExprIfFalse(pParse, pTest, nextCase, SQLITE_JUMPIFNULL);
drhc5499be2008-04-01 15:06:33 +00002768 testcase( aListelem[i+1].pExpr->op==TK_COLUMN );
2769 testcase( aListelem[i+1].pExpr->op==TK_REGISTER );
drh9de221d2008-01-05 06:51:30 +00002770 sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target);
drh2dcef112008-01-12 19:03:48 +00002771 sqlite3VdbeAddOp2(v, OP_Goto, 0, endLabel);
2772 sqlite3VdbeResolveLabel(v, nextCase);
drhf570f012002-05-31 15:51:25 +00002773 }
drh17a7f8d2002-03-24 13:13:27 +00002774 if( pExpr->pRight ){
drh9de221d2008-01-05 06:51:30 +00002775 sqlite3ExprCode(pParse, pExpr->pRight, target);
drh17a7f8d2002-03-24 13:13:27 +00002776 }else{
drh9de221d2008-01-05 06:51:30 +00002777 sqlite3VdbeAddOp2(v, OP_Null, 0, target);
drh17a7f8d2002-03-24 13:13:27 +00002778 }
drh2dcef112008-01-12 19:03:48 +00002779 sqlite3VdbeResolveLabel(v, endLabel);
drhc5499be2008-04-01 15:06:33 +00002780 assert( pParse->disableColCache>0 );
2781 pParse->disableColCache--;
danielk19776f349032002-06-11 02:25:40 +00002782 break;
2783 }
danielk19775338a5f2005-01-20 13:03:10 +00002784#ifndef SQLITE_OMIT_TRIGGER
danielk19776f349032002-06-11 02:25:40 +00002785 case TK_RAISE: {
2786 if( !pParse->trigStack ){
danielk19774adee202004-05-08 08:23:19 +00002787 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00002788 "RAISE() may only be used within a trigger-program");
drh389a1ad2008-01-03 23:44:53 +00002789 return 0;
danielk19776f349032002-06-11 02:25:40 +00002790 }
drhad6d9462004-09-19 02:15:24 +00002791 if( pExpr->iColumn!=OE_Ignore ){
2792 assert( pExpr->iColumn==OE_Rollback ||
2793 pExpr->iColumn == OE_Abort ||
2794 pExpr->iColumn == OE_Fail );
danielk19771e536952007-08-16 10:09:01 +00002795 sqlite3DequoteExpr(pParse->db, pExpr);
drh66a51672008-01-03 00:01:23 +00002796 sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn, 0,
drh2646da72005-12-09 20:02:05 +00002797 (char*)pExpr->token.z, pExpr->token.n);
danielk19776f349032002-06-11 02:25:40 +00002798 } else {
drhad6d9462004-09-19 02:15:24 +00002799 assert( pExpr->iColumn == OE_Ignore );
drh66a51672008-01-03 00:01:23 +00002800 sqlite3VdbeAddOp2(v, OP_ContextPop, 0, 0);
2801 sqlite3VdbeAddOp2(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
drhd4e70eb2008-01-02 00:34:36 +00002802 VdbeComment((v, "raise(IGNORE)"));
danielk19776f349032002-06-11 02:25:40 +00002803 }
drhffe07b22005-11-03 00:41:17 +00002804 break;
drh17a7f8d2002-03-24 13:13:27 +00002805 }
danielk19775338a5f2005-01-20 13:03:10 +00002806#endif
drhffe07b22005-11-03 00:41:17 +00002807 }
drh2dcef112008-01-12 19:03:48 +00002808 sqlite3ReleaseTempReg(pParse, regFree1);
2809 sqlite3ReleaseTempReg(pParse, regFree2);
2810 return inReg;
2811}
2812
2813/*
2814** Generate code to evaluate an expression and store the results
2815** into a register. Return the register number where the results
2816** are stored.
2817**
2818** If the register is a temporary register that can be deallocated,
drh678ccce2008-03-31 18:19:54 +00002819** then write its number into *pReg. If the result register is not
drh2dcef112008-01-12 19:03:48 +00002820** a temporary, then set *pReg to zero.
2821*/
2822int sqlite3ExprCodeTemp(Parse *pParse, Expr *pExpr, int *pReg){
2823 int r1 = sqlite3GetTempReg(pParse);
2824 int r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
2825 if( r2==r1 ){
2826 *pReg = r1;
2827 }else{
2828 sqlite3ReleaseTempReg(pParse, r1);
2829 *pReg = 0;
2830 }
2831 return r2;
2832}
2833
2834/*
2835** Generate code that will evaluate expression pExpr and store the
2836** results in register target. The results are guaranteed to appear
2837** in register target.
2838*/
2839int sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){
drh9cbf3422008-01-17 16:22:13 +00002840 int inReg;
2841
2842 assert( target>0 && target<=pParse->nMem );
2843 inReg = sqlite3ExprCodeTarget(pParse, pExpr, target);
drh0e359b32008-01-13 19:02:11 +00002844 assert( pParse->pVdbe || pParse->db->mallocFailed );
2845 if( inReg!=target && pParse->pVdbe ){
drh9cbf3422008-01-17 16:22:13 +00002846 sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, inReg, target);
drhcce7d172000-05-31 15:34:51 +00002847 }
drh389a1ad2008-01-03 23:44:53 +00002848 return target;
drhcce7d172000-05-31 15:34:51 +00002849}
2850
2851/*
drh2dcef112008-01-12 19:03:48 +00002852** Generate code that evalutes the given expression and puts the result
drhde4fcfd2008-01-19 23:50:26 +00002853** in register target.
drh25303782004-12-07 15:41:48 +00002854**
drh2dcef112008-01-12 19:03:48 +00002855** Also make a copy of the expression results into another "cache" register
2856** and modify the expression so that the next time it is evaluated,
2857** the result is a copy of the cache register.
2858**
2859** This routine is used for expressions that are used multiple
2860** times. They are evaluated once and the results of the expression
2861** are reused.
drh25303782004-12-07 15:41:48 +00002862*/
drh2dcef112008-01-12 19:03:48 +00002863int sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr, int target){
drh25303782004-12-07 15:41:48 +00002864 Vdbe *v = pParse->pVdbe;
drh2dcef112008-01-12 19:03:48 +00002865 int inReg;
2866 inReg = sqlite3ExprCode(pParse, pExpr, target);
drhde4fcfd2008-01-19 23:50:26 +00002867 assert( target>0 );
drh2dcef112008-01-12 19:03:48 +00002868 if( pExpr->op!=TK_REGISTER ){
2869 int iMem;
drhde4fcfd2008-01-19 23:50:26 +00002870 iMem = ++pParse->nMem;
2871 sqlite3VdbeAddOp2(v, OP_Copy, inReg, iMem);
drh2dcef112008-01-12 19:03:48 +00002872 pExpr->iTable = iMem;
drh678ccce2008-03-31 18:19:54 +00002873 pExpr->iColumn = pExpr->op;
drh25303782004-12-07 15:41:48 +00002874 pExpr->op = TK_REGISTER;
2875 }
drh2dcef112008-01-12 19:03:48 +00002876 return inReg;
drh25303782004-12-07 15:41:48 +00002877}
drh2dcef112008-01-12 19:03:48 +00002878
drh678ccce2008-03-31 18:19:54 +00002879/*
drh47de9552008-04-01 18:04:11 +00002880** Return TRUE if pExpr is an constant expression that is appropriate
2881** for factoring out of a loop. Appropriate expressions are:
2882**
2883** * Any expression that evaluates to two or more opcodes.
2884**
2885** * Any OP_Integer, OP_Real, OP_String, OP_Blob, OP_Null,
2886** or OP_Variable that does not need to be placed in a
2887** specific register.
2888**
2889** There is no point in factoring out single-instruction constant
2890** expressions that need to be placed in a particular register.
2891** We could factor them out, but then we would end up adding an
2892** OP_SCopy instruction to move the value into the correct register
2893** later. We might as well just use the original instruction and
2894** avoid the OP_SCopy.
2895*/
2896static int isAppropriateForFactoring(Expr *p){
2897 if( !sqlite3ExprIsConstantNotJoin(p) ){
2898 return 0; /* Only constant expressions are appropriate for factoring */
2899 }
2900 if( (p->flags & EP_FixedDest)==0 ){
2901 return 1; /* Any constant without a fixed destination is appropriate */
2902 }
2903 while( p->op==TK_UPLUS ) p = p->pLeft;
2904 switch( p->op ){
2905#ifndef SQLITE_OMIT_BLOB_LITERAL
2906 case TK_BLOB:
2907#endif
2908 case TK_VARIABLE:
2909 case TK_INTEGER:
2910 case TK_FLOAT:
2911 case TK_NULL:
2912 case TK_STRING: {
2913 testcase( p->op==TK_BLOB );
2914 testcase( p->op==TK_VARIABLE );
2915 testcase( p->op==TK_INTEGER );
2916 testcase( p->op==TK_FLOAT );
2917 testcase( p->op==TK_NULL );
2918 testcase( p->op==TK_STRING );
2919 /* Single-instruction constants with a fixed destination are
2920 ** better done in-line. If we factor them, they will just end
2921 ** up generating an OP_SCopy to move the value to the destination
2922 ** register. */
2923 return 0;
2924 }
2925 case TK_UMINUS: {
2926 if( p->pLeft->op==TK_FLOAT || p->pLeft->op==TK_INTEGER ){
2927 return 0;
2928 }
2929 break;
2930 }
2931 default: {
2932 break;
2933 }
2934 }
2935 return 1;
2936}
2937
2938/*
2939** If pExpr is a constant expression that is appropriate for
2940** factoring out of a loop, then evaluate the expression
drh678ccce2008-03-31 18:19:54 +00002941** into a register and convert the expression into a TK_REGISTER
2942** expression.
2943*/
2944static int evalConstExpr(void *pArg, Expr *pExpr){
2945 Parse *pParse = (Parse*)pArg;
drh47de9552008-04-01 18:04:11 +00002946 switch( pExpr->op ){
2947 case TK_REGISTER: {
2948 return 1;
2949 }
2950 case TK_FUNCTION:
2951 case TK_AGG_FUNCTION:
2952 case TK_CONST_FUNC: {
2953 /* The arguments to a function have a fixed destination.
2954 ** Mark them this way to avoid generated unneeded OP_SCopy
2955 ** instructions.
2956 */
2957 ExprList *pList = pExpr->pList;
2958 if( pList ){
2959 int i = pList->nExpr;
2960 struct ExprList_item *pItem = pList->a;
2961 for(; i>0; i--, pItem++){
2962 if( pItem->pExpr ) pItem->pExpr->flags |= EP_FixedDest;
2963 }
2964 }
2965 break;
2966 }
drh678ccce2008-03-31 18:19:54 +00002967 }
drh47de9552008-04-01 18:04:11 +00002968 if( isAppropriateForFactoring(pExpr) ){
drh678ccce2008-03-31 18:19:54 +00002969 int r1 = ++pParse->nMem;
2970 int r2;
2971 r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
drhc5499be2008-04-01 15:06:33 +00002972 if( r1!=r2 ) sqlite3ReleaseTempReg(pParse, r1);
drh678ccce2008-03-31 18:19:54 +00002973 pExpr->iColumn = pExpr->op;
2974 pExpr->op = TK_REGISTER;
2975 pExpr->iTable = r2;
2976 return 1;
2977 }
2978 return 0;
2979}
2980
2981/*
2982** Preevaluate constant subexpressions within pExpr and store the
2983** results in registers. Modify pExpr so that the constant subexpresions
2984** are TK_REGISTER opcodes that refer to the precomputed values.
2985*/
2986void sqlite3ExprCodeConstants(Parse *pParse, Expr *pExpr){
2987 walkExprTree(pExpr, evalConstExpr, pParse);
2988}
2989
drh25303782004-12-07 15:41:48 +00002990
2991/*
drh268380c2004-02-25 13:47:31 +00002992** Generate code that pushes the value of every element of the given
drh9cbf3422008-01-17 16:22:13 +00002993** expression list into a sequence of registers beginning at target.
drh268380c2004-02-25 13:47:31 +00002994**
drh892d3172008-01-10 03:46:36 +00002995** Return the number of elements evaluated.
drh268380c2004-02-25 13:47:31 +00002996*/
danielk19774adee202004-05-08 08:23:19 +00002997int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00002998 Parse *pParse, /* Parsing context */
drh389a1ad2008-01-03 23:44:53 +00002999 ExprList *pList, /* The expression list to be coded */
drh191b54c2008-04-15 12:14:21 +00003000 int target, /* Where to write results */
3001 int doHardCopy /* Call sqlite3ExprHardCopy on each element if true */
drh268380c2004-02-25 13:47:31 +00003002){
3003 struct ExprList_item *pItem;
drh9cbf3422008-01-17 16:22:13 +00003004 int i, n;
drh892d3172008-01-10 03:46:36 +00003005 assert( pList!=0 || pParse->db->mallocFailed );
3006 if( pList==0 ){
3007 return 0;
3008 }
drh9cbf3422008-01-17 16:22:13 +00003009 assert( target>0 );
drh268380c2004-02-25 13:47:31 +00003010 n = pList->nExpr;
drh191b54c2008-04-15 12:14:21 +00003011 for(pItem=pList->a, i=0; i<n; i++, pItem++){
3012 sqlite3ExprCode(pParse, pItem->pExpr, target+i);
3013 if( doHardCopy ) sqlite3ExprHardCopy(pParse, target, n);
drh268380c2004-02-25 13:47:31 +00003014 }
drhf9b596e2004-05-26 16:54:42 +00003015 return n;
drh268380c2004-02-25 13:47:31 +00003016}
3017
3018/*
drhcce7d172000-05-31 15:34:51 +00003019** Generate code for a boolean expression such that a jump is made
3020** to the label "dest" if the expression is true but execution
3021** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00003022**
3023** If the expression evaluates to NULL (neither true nor false), then
drh35573352008-01-08 23:54:25 +00003024** take the jump if the jumpIfNull flag is SQLITE_JUMPIFNULL.
drhf2bc0132004-10-04 13:19:23 +00003025**
3026** This code depends on the fact that certain token values (ex: TK_EQ)
3027** are the same as opcode values (ex: OP_Eq) that implement the corresponding
3028** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
3029** the make process cause these values to align. Assert()s in the code
3030** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00003031*/
danielk19774adee202004-05-08 08:23:19 +00003032void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00003033 Vdbe *v = pParse->pVdbe;
3034 int op = 0;
drh2dcef112008-01-12 19:03:48 +00003035 int regFree1 = 0;
3036 int regFree2 = 0;
3037 int r1, r2;
3038
drh35573352008-01-08 23:54:25 +00003039 assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
drhdaffd0e2001-04-11 14:28:42 +00003040 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00003041 op = pExpr->op;
3042 switch( op ){
drhcce7d172000-05-31 15:34:51 +00003043 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00003044 int d2 = sqlite3VdbeMakeLabel(v);
drhc5499be2008-04-01 15:06:33 +00003045 testcase( jumpIfNull==0 );
3046 testcase( pParse->disableColCache==0 );
drh35573352008-01-08 23:54:25 +00003047 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,jumpIfNull^SQLITE_JUMPIFNULL);
drhe55cbd72008-03-31 23:48:03 +00003048 pParse->disableColCache++;
danielk19774adee202004-05-08 08:23:19 +00003049 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhc5499be2008-04-01 15:06:33 +00003050 assert( pParse->disableColCache>0 );
drhe55cbd72008-03-31 23:48:03 +00003051 pParse->disableColCache--;
danielk19774adee202004-05-08 08:23:19 +00003052 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00003053 break;
3054 }
3055 case TK_OR: {
drhc5499be2008-04-01 15:06:33 +00003056 testcase( jumpIfNull==0 );
3057 testcase( pParse->disableColCache==0 );
danielk19774adee202004-05-08 08:23:19 +00003058 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhe55cbd72008-03-31 23:48:03 +00003059 pParse->disableColCache++;
danielk19774adee202004-05-08 08:23:19 +00003060 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhc5499be2008-04-01 15:06:33 +00003061 assert( pParse->disableColCache>0 );
drhe55cbd72008-03-31 23:48:03 +00003062 pParse->disableColCache--;
drhcce7d172000-05-31 15:34:51 +00003063 break;
3064 }
3065 case TK_NOT: {
drhc5499be2008-04-01 15:06:33 +00003066 testcase( jumpIfNull==0 );
danielk19774adee202004-05-08 08:23:19 +00003067 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00003068 break;
3069 }
3070 case TK_LT:
3071 case TK_LE:
3072 case TK_GT:
3073 case TK_GE:
3074 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00003075 case TK_EQ: {
drhf2bc0132004-10-04 13:19:23 +00003076 assert( TK_LT==OP_Lt );
3077 assert( TK_LE==OP_Le );
3078 assert( TK_GT==OP_Gt );
3079 assert( TK_GE==OP_Ge );
3080 assert( TK_EQ==OP_Eq );
3081 assert( TK_NE==OP_Ne );
drhc5499be2008-04-01 15:06:33 +00003082 testcase( op==TK_LT );
3083 testcase( op==TK_LE );
3084 testcase( op==TK_GT );
3085 testcase( op==TK_GE );
3086 testcase( op==TK_EQ );
3087 testcase( op==TK_NE );
3088 testcase( jumpIfNull==0 );
drhda250ea2008-04-01 05:07:14 +00003089 codeCompareOperands(pParse, pExpr->pLeft, &r1, &regFree1,
3090 pExpr->pRight, &r2, &regFree2);
drh35573352008-01-08 23:54:25 +00003091 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
drh2dcef112008-01-12 19:03:48 +00003092 r1, r2, dest, jumpIfNull);
drhc5499be2008-04-01 15:06:33 +00003093 testcase( regFree1==0 );
3094 testcase( regFree2==0 );
drhcce7d172000-05-31 15:34:51 +00003095 break;
3096 }
3097 case TK_ISNULL:
3098 case TK_NOTNULL: {
drhf2bc0132004-10-04 13:19:23 +00003099 assert( TK_ISNULL==OP_IsNull );
3100 assert( TK_NOTNULL==OP_NotNull );
drhc5499be2008-04-01 15:06:33 +00003101 testcase( op==TK_ISNULL );
3102 testcase( op==TK_NOTNULL );
drh2dcef112008-01-12 19:03:48 +00003103 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
3104 sqlite3VdbeAddOp2(v, op, r1, dest);
drhc5499be2008-04-01 15:06:33 +00003105 testcase( regFree1==0 );
drhcce7d172000-05-31 15:34:51 +00003106 break;
3107 }
drhfef52082000-06-06 01:50:43 +00003108 case TK_BETWEEN: {
drh2dcef112008-01-12 19:03:48 +00003109 /* x BETWEEN y AND z
danielk19770202b292004-06-09 09:55:16 +00003110 **
drh2dcef112008-01-12 19:03:48 +00003111 ** Is equivalent to
3112 **
3113 ** x>=y AND x<=z
3114 **
3115 ** Code it as such, taking care to do the common subexpression
3116 ** elementation of x.
danielk19770202b292004-06-09 09:55:16 +00003117 */
drh2dcef112008-01-12 19:03:48 +00003118 Expr exprAnd;
3119 Expr compLeft;
3120 Expr compRight;
3121 Expr exprX;
danielk19770202b292004-06-09 09:55:16 +00003122
drh2dcef112008-01-12 19:03:48 +00003123 exprX = *pExpr->pLeft;
3124 exprAnd.op = TK_AND;
3125 exprAnd.pLeft = &compLeft;
3126 exprAnd.pRight = &compRight;
3127 compLeft.op = TK_GE;
3128 compLeft.pLeft = &exprX;
3129 compLeft.pRight = pExpr->pList->a[0].pExpr;
3130 compRight.op = TK_LE;
3131 compRight.pLeft = &exprX;
3132 compRight.pRight = pExpr->pList->a[1].pExpr;
3133 exprX.iTable = sqlite3ExprCodeTemp(pParse, &exprX, &regFree1);
drhc5499be2008-04-01 15:06:33 +00003134 testcase( regFree1==0 );
drh2dcef112008-01-12 19:03:48 +00003135 exprX.op = TK_REGISTER;
drhc5499be2008-04-01 15:06:33 +00003136 testcase( jumpIfNull==0 );
drh2dcef112008-01-12 19:03:48 +00003137 sqlite3ExprIfTrue(pParse, &exprAnd, dest, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00003138 break;
3139 }
drhcce7d172000-05-31 15:34:51 +00003140 default: {
drh2dcef112008-01-12 19:03:48 +00003141 r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
3142 sqlite3VdbeAddOp3(v, OP_If, r1, dest, jumpIfNull!=0);
drhc5499be2008-04-01 15:06:33 +00003143 testcase( regFree1==0 );
3144 testcase( jumpIfNull==0 );
drhcce7d172000-05-31 15:34:51 +00003145 break;
3146 }
3147 }
drh2dcef112008-01-12 19:03:48 +00003148 sqlite3ReleaseTempReg(pParse, regFree1);
3149 sqlite3ReleaseTempReg(pParse, regFree2);
drhcce7d172000-05-31 15:34:51 +00003150}
3151
3152/*
drh66b89c82000-11-28 20:47:17 +00003153** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00003154** to the label "dest" if the expression is false but execution
3155** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00003156**
3157** If the expression evaluates to NULL (neither true nor false) then
drh35573352008-01-08 23:54:25 +00003158** jump if jumpIfNull is SQLITE_JUMPIFNULL or fall through if jumpIfNull
3159** is 0.
drhcce7d172000-05-31 15:34:51 +00003160*/
danielk19774adee202004-05-08 08:23:19 +00003161void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00003162 Vdbe *v = pParse->pVdbe;
3163 int op = 0;
drh2dcef112008-01-12 19:03:48 +00003164 int regFree1 = 0;
3165 int regFree2 = 0;
3166 int r1, r2;
3167
drh35573352008-01-08 23:54:25 +00003168 assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
drhdaffd0e2001-04-11 14:28:42 +00003169 if( v==0 || pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00003170
3171 /* The value of pExpr->op and op are related as follows:
3172 **
3173 ** pExpr->op op
3174 ** --------- ----------
3175 ** TK_ISNULL OP_NotNull
3176 ** TK_NOTNULL OP_IsNull
3177 ** TK_NE OP_Eq
3178 ** TK_EQ OP_Ne
3179 ** TK_GT OP_Le
3180 ** TK_LE OP_Gt
3181 ** TK_GE OP_Lt
3182 ** TK_LT OP_Ge
3183 **
3184 ** For other values of pExpr->op, op is undefined and unused.
3185 ** The value of TK_ and OP_ constants are arranged such that we
3186 ** can compute the mapping above using the following expression.
3187 ** Assert()s verify that the computation is correct.
3188 */
3189 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
3190
3191 /* Verify correct alignment of TK_ and OP_ constants
3192 */
3193 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
3194 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
3195 assert( pExpr->op!=TK_NE || op==OP_Eq );
3196 assert( pExpr->op!=TK_EQ || op==OP_Ne );
3197 assert( pExpr->op!=TK_LT || op==OP_Ge );
3198 assert( pExpr->op!=TK_LE || op==OP_Gt );
3199 assert( pExpr->op!=TK_GT || op==OP_Le );
3200 assert( pExpr->op!=TK_GE || op==OP_Lt );
3201
drhcce7d172000-05-31 15:34:51 +00003202 switch( pExpr->op ){
3203 case TK_AND: {
drhc5499be2008-04-01 15:06:33 +00003204 testcase( jumpIfNull==0 );
3205 testcase( pParse->disableColCache==0 );
danielk19774adee202004-05-08 08:23:19 +00003206 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhe55cbd72008-03-31 23:48:03 +00003207 pParse->disableColCache++;
danielk19774adee202004-05-08 08:23:19 +00003208 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhc5499be2008-04-01 15:06:33 +00003209 assert( pParse->disableColCache>0 );
drhe55cbd72008-03-31 23:48:03 +00003210 pParse->disableColCache--;
drhcce7d172000-05-31 15:34:51 +00003211 break;
3212 }
3213 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00003214 int d2 = sqlite3VdbeMakeLabel(v);
drhc5499be2008-04-01 15:06:33 +00003215 testcase( jumpIfNull==0 );
3216 testcase( pParse->disableColCache==0 );
drh35573352008-01-08 23:54:25 +00003217 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, jumpIfNull^SQLITE_JUMPIFNULL);
drhe55cbd72008-03-31 23:48:03 +00003218 pParse->disableColCache++;
danielk19774adee202004-05-08 08:23:19 +00003219 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhc5499be2008-04-01 15:06:33 +00003220 assert( pParse->disableColCache>0 );
drhe55cbd72008-03-31 23:48:03 +00003221 pParse->disableColCache--;
danielk19774adee202004-05-08 08:23:19 +00003222 sqlite3VdbeResolveLabel(v, d2);
drhcce7d172000-05-31 15:34:51 +00003223 break;
3224 }
3225 case TK_NOT: {
danielk19774adee202004-05-08 08:23:19 +00003226 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00003227 break;
3228 }
3229 case TK_LT:
3230 case TK_LE:
3231 case TK_GT:
3232 case TK_GE:
3233 case TK_NE:
3234 case TK_EQ: {
drhc5499be2008-04-01 15:06:33 +00003235 testcase( op==TK_LT );
3236 testcase( op==TK_LE );
3237 testcase( op==TK_GT );
3238 testcase( op==TK_GE );
3239 testcase( op==TK_EQ );
3240 testcase( op==TK_NE );
3241 testcase( jumpIfNull==0 );
drhda250ea2008-04-01 05:07:14 +00003242 codeCompareOperands(pParse, pExpr->pLeft, &r1, &regFree1,
3243 pExpr->pRight, &r2, &regFree2);
drh35573352008-01-08 23:54:25 +00003244 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
drh2dcef112008-01-12 19:03:48 +00003245 r1, r2, dest, jumpIfNull);
drhc5499be2008-04-01 15:06:33 +00003246 testcase( regFree1==0 );
3247 testcase( regFree2==0 );
drhcce7d172000-05-31 15:34:51 +00003248 break;
3249 }
drhcce7d172000-05-31 15:34:51 +00003250 case TK_ISNULL:
3251 case TK_NOTNULL: {
drhc5499be2008-04-01 15:06:33 +00003252 testcase( op==TK_ISNULL );
3253 testcase( op==TK_NOTNULL );
drh2dcef112008-01-12 19:03:48 +00003254 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
3255 sqlite3VdbeAddOp2(v, op, r1, dest);
drhc5499be2008-04-01 15:06:33 +00003256 testcase( regFree1==0 );
drhcce7d172000-05-31 15:34:51 +00003257 break;
3258 }
drhfef52082000-06-06 01:50:43 +00003259 case TK_BETWEEN: {
drh2dcef112008-01-12 19:03:48 +00003260 /* x BETWEEN y AND z
danielk19770202b292004-06-09 09:55:16 +00003261 **
drh2dcef112008-01-12 19:03:48 +00003262 ** Is equivalent to
3263 **
3264 ** x>=y AND x<=z
3265 **
3266 ** Code it as such, taking care to do the common subexpression
3267 ** elementation of x.
danielk19770202b292004-06-09 09:55:16 +00003268 */
drh2dcef112008-01-12 19:03:48 +00003269 Expr exprAnd;
3270 Expr compLeft;
3271 Expr compRight;
3272 Expr exprX;
drhbe5c89a2004-07-26 00:31:09 +00003273
drh2dcef112008-01-12 19:03:48 +00003274 exprX = *pExpr->pLeft;
3275 exprAnd.op = TK_AND;
3276 exprAnd.pLeft = &compLeft;
3277 exprAnd.pRight = &compRight;
3278 compLeft.op = TK_GE;
3279 compLeft.pLeft = &exprX;
3280 compLeft.pRight = pExpr->pList->a[0].pExpr;
3281 compRight.op = TK_LE;
3282 compRight.pLeft = &exprX;
3283 compRight.pRight = pExpr->pList->a[1].pExpr;
3284 exprX.iTable = sqlite3ExprCodeTemp(pParse, &exprX, &regFree1);
drhc5499be2008-04-01 15:06:33 +00003285 testcase( regFree1==0 );
drh2dcef112008-01-12 19:03:48 +00003286 exprX.op = TK_REGISTER;
drhc5499be2008-04-01 15:06:33 +00003287 testcase( jumpIfNull==0 );
drh2dcef112008-01-12 19:03:48 +00003288 sqlite3ExprIfFalse(pParse, &exprAnd, dest, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00003289 break;
3290 }
drhcce7d172000-05-31 15:34:51 +00003291 default: {
drh2dcef112008-01-12 19:03:48 +00003292 r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
3293 sqlite3VdbeAddOp3(v, OP_IfNot, r1, dest, jumpIfNull!=0);
drhc5499be2008-04-01 15:06:33 +00003294 testcase( regFree1==0 );
3295 testcase( jumpIfNull==0 );
drhcce7d172000-05-31 15:34:51 +00003296 break;
3297 }
3298 }
drh2dcef112008-01-12 19:03:48 +00003299 sqlite3ReleaseTempReg(pParse, regFree1);
3300 sqlite3ReleaseTempReg(pParse, regFree2);
drhcce7d172000-05-31 15:34:51 +00003301}
drh22827922000-06-06 17:27:05 +00003302
3303/*
3304** Do a deep comparison of two expression trees. Return TRUE (non-zero)
3305** if they are identical and return FALSE if they differ in any way.
drhd40aab02007-02-24 15:29:03 +00003306**
3307** Sometimes this routine will return FALSE even if the two expressions
3308** really are equivalent. If we cannot prove that the expressions are
3309** identical, we return FALSE just to be safe. So if this routine
3310** returns false, then you do not really know for certain if the two
3311** expressions are the same. But if you get a TRUE return, then you
3312** can be sure the expressions are the same. In the places where
3313** this routine is used, it does not hurt to get an extra FALSE - that
3314** just might result in some slightly slower code. But returning
3315** an incorrect TRUE could lead to a malfunction.
drh22827922000-06-06 17:27:05 +00003316*/
danielk19774adee202004-05-08 08:23:19 +00003317int sqlite3ExprCompare(Expr *pA, Expr *pB){
drh22827922000-06-06 17:27:05 +00003318 int i;
danielk19774b202ae2006-01-23 05:50:58 +00003319 if( pA==0||pB==0 ){
3320 return pB==pA;
drh22827922000-06-06 17:27:05 +00003321 }
3322 if( pA->op!=pB->op ) return 0;
drhfd357972005-09-09 01:33:19 +00003323 if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 0;
danielk19774adee202004-05-08 08:23:19 +00003324 if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
3325 if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
drh22827922000-06-06 17:27:05 +00003326 if( pA->pList ){
3327 if( pB->pList==0 ) return 0;
3328 if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
3329 for(i=0; i<pA->pList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00003330 if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
drh22827922000-06-06 17:27:05 +00003331 return 0;
3332 }
3333 }
3334 }else if( pB->pList ){
3335 return 0;
3336 }
3337 if( pA->pSelect || pB->pSelect ) return 0;
drh2f2c01e2002-07-02 13:05:04 +00003338 if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
drhdd735212007-02-24 13:53:05 +00003339 if( pA->op!=TK_COLUMN && pA->token.z ){
drh22827922000-06-06 17:27:05 +00003340 if( pB->token.z==0 ) return 0;
drh6977fea2002-10-22 23:38:04 +00003341 if( pB->token.n!=pA->token.n ) return 0;
drh2646da72005-12-09 20:02:05 +00003342 if( sqlite3StrNICmp((char*)pA->token.z,(char*)pB->token.z,pB->token.n)!=0 ){
3343 return 0;
3344 }
drh22827922000-06-06 17:27:05 +00003345 }
3346 return 1;
3347}
3348
drh13449892005-09-07 21:22:45 +00003349
drh22827922000-06-06 17:27:05 +00003350/*
drh13449892005-09-07 21:22:45 +00003351** Add a new element to the pAggInfo->aCol[] array. Return the index of
3352** the new element. Return a negative number if malloc fails.
drh22827922000-06-06 17:27:05 +00003353*/
drh17435752007-08-16 04:30:38 +00003354static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){
drh13449892005-09-07 21:22:45 +00003355 int i;
drhcf643722007-03-27 13:36:37 +00003356 pInfo->aCol = sqlite3ArrayAllocate(
drh17435752007-08-16 04:30:38 +00003357 db,
drhcf643722007-03-27 13:36:37 +00003358 pInfo->aCol,
3359 sizeof(pInfo->aCol[0]),
3360 3,
3361 &pInfo->nColumn,
3362 &pInfo->nColumnAlloc,
3363 &i
3364 );
drh13449892005-09-07 21:22:45 +00003365 return i;
3366}
3367
3368/*
3369** Add a new element to the pAggInfo->aFunc[] array. Return the index of
3370** the new element. Return a negative number if malloc fails.
3371*/
drh17435752007-08-16 04:30:38 +00003372static int addAggInfoFunc(sqlite3 *db, AggInfo *pInfo){
drh13449892005-09-07 21:22:45 +00003373 int i;
drhcf643722007-03-27 13:36:37 +00003374 pInfo->aFunc = sqlite3ArrayAllocate(
drh17435752007-08-16 04:30:38 +00003375 db,
drhcf643722007-03-27 13:36:37 +00003376 pInfo->aFunc,
3377 sizeof(pInfo->aFunc[0]),
3378 3,
3379 &pInfo->nFunc,
3380 &pInfo->nFuncAlloc,
3381 &i
3382 );
drh13449892005-09-07 21:22:45 +00003383 return i;
3384}
drh22827922000-06-06 17:27:05 +00003385
3386/*
drh626a8792005-01-17 22:08:19 +00003387** This is an xFunc for walkExprTree() used to implement
3388** sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates
3389** for additional information.
drh22827922000-06-06 17:27:05 +00003390**
drh626a8792005-01-17 22:08:19 +00003391** This routine analyzes the aggregate function at pExpr.
drh22827922000-06-06 17:27:05 +00003392*/
drh626a8792005-01-17 22:08:19 +00003393static int analyzeAggregate(void *pArg, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00003394 int i;
danielk1977a58fdfb2005-02-08 07:50:40 +00003395 NameContext *pNC = (NameContext *)pArg;
3396 Parse *pParse = pNC->pParse;
3397 SrcList *pSrcList = pNC->pSrcList;
drh13449892005-09-07 21:22:45 +00003398 AggInfo *pAggInfo = pNC->pAggInfo;
drh22827922000-06-06 17:27:05 +00003399
drh22827922000-06-06 17:27:05 +00003400 switch( pExpr->op ){
drh89c69d02007-01-04 01:20:28 +00003401 case TK_AGG_COLUMN:
drh967e8b72000-06-21 13:59:10 +00003402 case TK_COLUMN: {
drh13449892005-09-07 21:22:45 +00003403 /* Check to see if the column is in one of the tables in the FROM
3404 ** clause of the aggregate query */
3405 if( pSrcList ){
3406 struct SrcList_item *pItem = pSrcList->a;
3407 for(i=0; i<pSrcList->nSrc; i++, pItem++){
3408 struct AggInfo_col *pCol;
3409 if( pExpr->iTable==pItem->iCursor ){
3410 /* If we reach this point, it means that pExpr refers to a table
3411 ** that is in the FROM clause of the aggregate query.
3412 **
3413 ** Make an entry for the column in pAggInfo->aCol[] if there
3414 ** is not an entry there already.
3415 */
drh7f906d62007-03-12 23:48:52 +00003416 int k;
drh13449892005-09-07 21:22:45 +00003417 pCol = pAggInfo->aCol;
drh7f906d62007-03-12 23:48:52 +00003418 for(k=0; k<pAggInfo->nColumn; k++, pCol++){
drh13449892005-09-07 21:22:45 +00003419 if( pCol->iTable==pExpr->iTable &&
3420 pCol->iColumn==pExpr->iColumn ){
3421 break;
3422 }
danielk1977a58fdfb2005-02-08 07:50:40 +00003423 }
danielk19771e536952007-08-16 10:09:01 +00003424 if( (k>=pAggInfo->nColumn)
3425 && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0
3426 ){
drh7f906d62007-03-12 23:48:52 +00003427 pCol = &pAggInfo->aCol[k];
danielk19770817d0d2007-02-14 09:19:36 +00003428 pCol->pTab = pExpr->pTab;
drh13449892005-09-07 21:22:45 +00003429 pCol->iTable = pExpr->iTable;
3430 pCol->iColumn = pExpr->iColumn;
drh0a07c102008-01-03 18:03:08 +00003431 pCol->iMem = ++pParse->nMem;
drh13449892005-09-07 21:22:45 +00003432 pCol->iSorterColumn = -1;
drh5774b802005-09-07 22:48:16 +00003433 pCol->pExpr = pExpr;
drh13449892005-09-07 21:22:45 +00003434 if( pAggInfo->pGroupBy ){
3435 int j, n;
3436 ExprList *pGB = pAggInfo->pGroupBy;
3437 struct ExprList_item *pTerm = pGB->a;
3438 n = pGB->nExpr;
3439 for(j=0; j<n; j++, pTerm++){
3440 Expr *pE = pTerm->pExpr;
3441 if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable &&
3442 pE->iColumn==pExpr->iColumn ){
3443 pCol->iSorterColumn = j;
3444 break;
3445 }
3446 }
3447 }
3448 if( pCol->iSorterColumn<0 ){
3449 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
3450 }
3451 }
3452 /* There is now an entry for pExpr in pAggInfo->aCol[] (either
3453 ** because it was there before or because we just created it).
3454 ** Convert the pExpr to be a TK_AGG_COLUMN referring to that
3455 ** pAggInfo->aCol[] entry.
3456 */
3457 pExpr->pAggInfo = pAggInfo;
3458 pExpr->op = TK_AGG_COLUMN;
drh7f906d62007-03-12 23:48:52 +00003459 pExpr->iAgg = k;
drh13449892005-09-07 21:22:45 +00003460 break;
3461 } /* endif pExpr->iTable==pItem->iCursor */
3462 } /* end loop over pSrcList */
drh22827922000-06-06 17:27:05 +00003463 }
drh626a8792005-01-17 22:08:19 +00003464 return 1;
drh22827922000-06-06 17:27:05 +00003465 }
3466 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00003467 /* The pNC->nDepth==0 test causes aggregate functions in subqueries
3468 ** to be ignored */
danielk1977a58fdfb2005-02-08 07:50:40 +00003469 if( pNC->nDepth==0 ){
drh13449892005-09-07 21:22:45 +00003470 /* Check to see if pExpr is a duplicate of another aggregate
3471 ** function that is already in the pAggInfo structure
3472 */
3473 struct AggInfo_func *pItem = pAggInfo->aFunc;
3474 for(i=0; i<pAggInfo->nFunc; i++, pItem++){
3475 if( sqlite3ExprCompare(pItem->pExpr, pExpr) ){
danielk1977a58fdfb2005-02-08 07:50:40 +00003476 break;
3477 }
drh22827922000-06-06 17:27:05 +00003478 }
drh13449892005-09-07 21:22:45 +00003479 if( i>=pAggInfo->nFunc ){
3480 /* pExpr is original. Make a new entry in pAggInfo->aFunc[]
3481 */
danielk197714db2662006-01-09 16:12:04 +00003482 u8 enc = ENC(pParse->db);
danielk19771e536952007-08-16 10:09:01 +00003483 i = addAggInfoFunc(pParse->db, pAggInfo);
drh13449892005-09-07 21:22:45 +00003484 if( i>=0 ){
3485 pItem = &pAggInfo->aFunc[i];
3486 pItem->pExpr = pExpr;
drh0a07c102008-01-03 18:03:08 +00003487 pItem->iMem = ++pParse->nMem;
drh13449892005-09-07 21:22:45 +00003488 pItem->pFunc = sqlite3FindFunction(pParse->db,
drh2646da72005-12-09 20:02:05 +00003489 (char*)pExpr->token.z, pExpr->token.n,
drh13449892005-09-07 21:22:45 +00003490 pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
drhfd357972005-09-09 01:33:19 +00003491 if( pExpr->flags & EP_Distinct ){
3492 pItem->iDistinct = pParse->nTab++;
3493 }else{
3494 pItem->iDistinct = -1;
3495 }
drh13449892005-09-07 21:22:45 +00003496 }
danielk1977a58fdfb2005-02-08 07:50:40 +00003497 }
drh13449892005-09-07 21:22:45 +00003498 /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
3499 */
danielk1977a58fdfb2005-02-08 07:50:40 +00003500 pExpr->iAgg = i;
drh13449892005-09-07 21:22:45 +00003501 pExpr->pAggInfo = pAggInfo;
danielk1977a58fdfb2005-02-08 07:50:40 +00003502 return 1;
drh22827922000-06-06 17:27:05 +00003503 }
drh22827922000-06-06 17:27:05 +00003504 }
3505 }
drh13449892005-09-07 21:22:45 +00003506
3507 /* Recursively walk subqueries looking for TK_COLUMN nodes that need
3508 ** to be changed to TK_AGG_COLUMN. But increment nDepth so that
3509 ** TK_AGG_FUNCTION nodes in subqueries will be unchanged.
3510 */
danielk1977a58fdfb2005-02-08 07:50:40 +00003511 if( pExpr->pSelect ){
3512 pNC->nDepth++;
3513 walkSelectExpr(pExpr->pSelect, analyzeAggregate, pNC);
3514 pNC->nDepth--;
3515 }
drh626a8792005-01-17 22:08:19 +00003516 return 0;
3517}
3518
3519/*
3520** Analyze the given expression looking for aggregate functions and
3521** for variables that need to be added to the pParse->aAgg[] array.
3522** Make additional entries to the pParse->aAgg[] array as necessary.
3523**
3524** This routine should only be called after the expression has been
3525** analyzed by sqlite3ExprResolveNames().
drh626a8792005-01-17 22:08:19 +00003526*/
drhd2b3e232008-01-23 14:51:49 +00003527void sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
danielk1977a58fdfb2005-02-08 07:50:40 +00003528 walkExprTree(pExpr, analyzeAggregate, pNC);
drh22827922000-06-06 17:27:05 +00003529}
drh5d9a4af2005-08-30 00:54:01 +00003530
3531/*
3532** Call sqlite3ExprAnalyzeAggregates() for every expression in an
3533** expression list. Return the number of errors.
3534**
3535** If an error is found, the analysis is cut short.
3536*/
drhd2b3e232008-01-23 14:51:49 +00003537void sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){
drh5d9a4af2005-08-30 00:54:01 +00003538 struct ExprList_item *pItem;
3539 int i;
drh5d9a4af2005-08-30 00:54:01 +00003540 if( pList ){
drhd2b3e232008-01-23 14:51:49 +00003541 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
3542 sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
drh5d9a4af2005-08-30 00:54:01 +00003543 }
3544 }
drh5d9a4af2005-08-30 00:54:01 +00003545}
drh892d3172008-01-10 03:46:36 +00003546
3547/*
3548** Allocate or deallocate temporary use registers during code generation.
3549*/
3550int sqlite3GetTempReg(Parse *pParse){
drhe55cbd72008-03-31 23:48:03 +00003551 if( pParse->nTempReg==0 ){
drh892d3172008-01-10 03:46:36 +00003552 return ++pParse->nMem;
3553 }
danielk19772f425f62008-07-04 09:41:39 +00003554 return pParse->aTempReg[--pParse->nTempReg];
drh892d3172008-01-10 03:46:36 +00003555}
3556void sqlite3ReleaseTempReg(Parse *pParse, int iReg){
drh2dcef112008-01-12 19:03:48 +00003557 if( iReg && pParse->nTempReg<ArraySize(pParse->aTempReg) ){
danielk1977a7d8b852008-07-04 09:15:11 +00003558 sqlite3ExprWritableRegister(pParse, iReg, iReg);
drh892d3172008-01-10 03:46:36 +00003559 pParse->aTempReg[pParse->nTempReg++] = iReg;
3560 }
3561}
3562
3563/*
3564** Allocate or deallocate a block of nReg consecutive registers
3565*/
3566int sqlite3GetTempRange(Parse *pParse, int nReg){
drhe55cbd72008-03-31 23:48:03 +00003567 int i, n;
3568 i = pParse->iRangeReg;
3569 n = pParse->nRangeReg;
3570 if( nReg<=n && !usedAsColumnCache(pParse, i, i+n-1) ){
drh892d3172008-01-10 03:46:36 +00003571 pParse->iRangeReg += nReg;
3572 pParse->nRangeReg -= nReg;
3573 }else{
3574 i = pParse->nMem+1;
3575 pParse->nMem += nReg;
3576 }
3577 return i;
3578}
3579void sqlite3ReleaseTempRange(Parse *pParse, int iReg, int nReg){
3580 if( nReg>pParse->nRangeReg ){
3581 pParse->nRangeReg = nReg;
3582 pParse->iRangeReg = iReg;
3583 }
3584}