blob: 7941c228fdd0005fac6a14ac72b0746c485fe5c0 [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*/
15#include "sqliteInt.h"
drha2e00042002-01-22 03:13:42 +000016
danielk1977e014a832004-05-17 10:48:57 +000017/*
18** Return the 'affinity' of the expression pExpr if any.
19**
20** If pExpr is a column, a reference to a column via an 'AS' alias,
21** or a sub-select with a column as the return value, then the
22** affinity of that column is returned. Otherwise, 0x00 is returned,
23** indicating no affinity for the expression.
24**
peter.d.reid60ec9142014-09-06 16:39:46 +000025** i.e. the WHERE clause expressions in the following statements all
danielk1977e014a832004-05-17 10:48:57 +000026** have an affinity:
27**
28** CREATE TABLE t1(a);
29** SELECT * FROM t1 WHERE a;
30** SELECT a AS b FROM t1 WHERE b;
31** SELECT * FROM t1 WHERE (select a from t1);
32*/
danielk1977bf3b7212004-05-18 10:06:24 +000033char sqlite3ExprAffinity(Expr *pExpr){
drh580c8c12012-12-08 03:34:04 +000034 int op;
35 pExpr = sqlite3ExprSkipCollate(pExpr);
mistachkin9bec6fb2014-06-26 21:28:21 +000036 if( pExpr->flags & EP_Generic ) return 0;
drh580c8c12012-12-08 03:34:04 +000037 op = pExpr->op;
drh487e2622005-06-25 18:42:14 +000038 if( op==TK_SELECT ){
danielk19776ab3a2e2009-02-19 14:39:25 +000039 assert( pExpr->flags&EP_xIsSelect );
40 return sqlite3ExprAffinity(pExpr->x.pSelect->pEList->a[0].pExpr);
danielk1977a37cdde2004-05-16 11:15:36 +000041 }
drh487e2622005-06-25 18:42:14 +000042#ifndef SQLITE_OMIT_CAST
43 if( op==TK_CAST ){
drh33e619f2009-05-28 01:00:55 +000044 assert( !ExprHasProperty(pExpr, EP_IntValue) );
drhfdaac672013-10-04 15:30:21 +000045 return sqlite3AffinityType(pExpr->u.zToken, 0);
drh487e2622005-06-25 18:42:14 +000046 }
47#endif
danielk1977259a4552008-11-12 08:07:12 +000048 if( (op==TK_AGG_COLUMN || op==TK_COLUMN || op==TK_REGISTER)
49 && pExpr->pTab!=0
50 ){
drh7d10d5a2008-08-20 16:35:10 +000051 /* op==TK_REGISTER && pExpr->pTab!=0 happens when pExpr was originally
52 ** a TK_COLUMN but was previously evaluated and cached in a register */
53 int j = pExpr->iColumn;
54 if( j<0 ) return SQLITE_AFF_INTEGER;
55 assert( pExpr->pTab && j<pExpr->pTab->nCol );
56 return pExpr->pTab->aCol[j].affinity;
57 }
danielk1977a37cdde2004-05-16 11:15:36 +000058 return pExpr->affinity;
59}
60
drh53db1452004-05-20 13:54:53 +000061/*
drh8b4c40d2007-02-01 23:02:45 +000062** Set the collating sequence for expression pExpr to be the collating
drhae80dde2012-12-06 21:16:43 +000063** sequence named by pToken. Return a pointer to a new Expr node that
64** implements the COLLATE operator.
drh0a8a4062012-12-07 18:38:16 +000065**
66** If a memory allocation error occurs, that fact is recorded in pParse->db
67** and the pExpr parameter is returned unchanged.
drh8b4c40d2007-02-01 23:02:45 +000068*/
drh4ef7efa2014-03-20 15:14:08 +000069Expr *sqlite3ExprAddCollateToken(
70 Parse *pParse, /* Parsing context */
71 Expr *pExpr, /* Add the "COLLATE" clause to this expression */
dan80103fc2015-03-20 08:43:59 +000072 const Token *pCollName, /* Name of collating sequence */
73 int dequote /* True to dequote pCollName */
drh4ef7efa2014-03-20 15:14:08 +000074){
drh0a8a4062012-12-07 18:38:16 +000075 if( pCollName->n>0 ){
dan80103fc2015-03-20 08:43:59 +000076 Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLLATE, pCollName, dequote);
drh0a8a4062012-12-07 18:38:16 +000077 if( pNew ){
78 pNew->pLeft = pExpr;
drha4c3c872013-09-12 17:29:25 +000079 pNew->flags |= EP_Collate|EP_Skip;
drh0a8a4062012-12-07 18:38:16 +000080 pExpr = pNew;
81 }
drhae80dde2012-12-06 21:16:43 +000082 }
drh0a8a4062012-12-07 18:38:16 +000083 return pExpr;
84}
85Expr *sqlite3ExprAddCollateString(Parse *pParse, Expr *pExpr, const char *zC){
drh261d8a52012-12-08 21:36:26 +000086 Token s;
87 assert( zC!=0 );
drh40aced52016-01-22 17:48:09 +000088 sqlite3TokenInit(&s, (char*)zC);
dan80103fc2015-03-20 08:43:59 +000089 return sqlite3ExprAddCollateToken(pParse, pExpr, &s, 0);
drh0a8a4062012-12-07 18:38:16 +000090}
91
92/*
drh0b8d2552015-09-05 22:36:07 +000093** Skip over any TK_COLLATE operators and any unlikely()
drha4c3c872013-09-12 17:29:25 +000094** or likelihood() function at the root of an expression.
drh0a8a4062012-12-07 18:38:16 +000095*/
96Expr *sqlite3ExprSkipCollate(Expr *pExpr){
drha4c3c872013-09-12 17:29:25 +000097 while( pExpr && ExprHasProperty(pExpr, EP_Skip) ){
98 if( ExprHasProperty(pExpr, EP_Unlikely) ){
drhcca9f3d2013-09-06 15:23:29 +000099 assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
100 assert( pExpr->x.pList->nExpr>0 );
drha4c3c872013-09-12 17:29:25 +0000101 assert( pExpr->op==TK_FUNCTION );
drhcca9f3d2013-09-06 15:23:29 +0000102 pExpr = pExpr->x.pList->a[0].pExpr;
103 }else{
drh0b8d2552015-09-05 22:36:07 +0000104 assert( pExpr->op==TK_COLLATE );
drha4c3c872013-09-12 17:29:25 +0000105 pExpr = pExpr->pLeft;
drhcca9f3d2013-09-06 15:23:29 +0000106 }
drha4c3c872013-09-12 17:29:25 +0000107 }
drh0a8a4062012-12-07 18:38:16 +0000108 return pExpr;
drh8b4c40d2007-02-01 23:02:45 +0000109}
110
111/*
drhae80dde2012-12-06 21:16:43 +0000112** Return the collation sequence for the expression pExpr. If
113** there is no defined collating sequence, return NULL.
114**
115** The collating sequence might be determined by a COLLATE operator
116** or by the presence of a column with a defined collating sequence.
117** COLLATE operators take first precedence. Left operands take
118** precedence over right operands.
danielk19770202b292004-06-09 09:55:16 +0000119*/
danielk19777cedc8d2004-06-10 10:50:08 +0000120CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
drhae80dde2012-12-06 21:16:43 +0000121 sqlite3 *db = pParse->db;
danielk19777cedc8d2004-06-10 10:50:08 +0000122 CollSeq *pColl = 0;
drh7d10d5a2008-08-20 16:35:10 +0000123 Expr *p = pExpr;
drh261d8a52012-12-08 21:36:26 +0000124 while( p ){
drhae80dde2012-12-06 21:16:43 +0000125 int op = p->op;
drhfbb24d12014-03-20 17:03:30 +0000126 if( p->flags & EP_Generic ) break;
drhae80dde2012-12-06 21:16:43 +0000127 if( op==TK_CAST || op==TK_UPLUS ){
128 p = p->pLeft;
129 continue;
130 }
dan36e78302013-08-21 12:04:32 +0000131 if( op==TK_COLLATE || (op==TK_REGISTER && p->op2==TK_COLLATE) ){
drh6fdd3d82013-05-15 17:47:12 +0000132 pColl = sqlite3GetCollSeq(pParse, ENC(db), 0, p->u.zToken);
drhae80dde2012-12-06 21:16:43 +0000133 break;
134 }
drha58d4a92015-01-27 13:17:05 +0000135 if( (op==TK_AGG_COLUMN || op==TK_COLUMN
drhae80dde2012-12-06 21:16:43 +0000136 || op==TK_REGISTER || op==TK_TRIGGER)
drha58d4a92015-01-27 13:17:05 +0000137 && p->pTab!=0
drhae80dde2012-12-06 21:16:43 +0000138 ){
drh7d10d5a2008-08-20 16:35:10 +0000139 /* op==TK_REGISTER && p->pTab!=0 happens when pExpr was originally
140 ** a TK_COLUMN but was previously evaluated and cached in a register */
drh7d10d5a2008-08-20 16:35:10 +0000141 int j = p->iColumn;
142 if( j>=0 ){
drhae80dde2012-12-06 21:16:43 +0000143 const char *zColl = p->pTab->aCol[j].zColl;
drhc4a64fa2009-05-11 20:53:28 +0000144 pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0);
drh7d10d5a2008-08-20 16:35:10 +0000145 }
146 break;
danielk19770202b292004-06-09 09:55:16 +0000147 }
drhae80dde2012-12-06 21:16:43 +0000148 if( p->flags & EP_Collate ){
drh2308ed32015-02-09 16:09:34 +0000149 if( p->pLeft && (p->pLeft->flags & EP_Collate)!=0 ){
drhae80dde2012-12-06 21:16:43 +0000150 p = p->pLeft;
151 }else{
drh2308ed32015-02-09 16:09:34 +0000152 Expr *pNext = p->pRight;
drh6728cd92015-02-09 18:28:03 +0000153 /* The Expr.x union is never used at the same time as Expr.pRight */
154 assert( p->x.pList==0 || p->pRight==0 );
155 /* p->flags holds EP_Collate and p->pLeft->flags does not. And
156 ** p->x.pSelect cannot. So if p->x.pLeft exists, it must hold at
157 ** least one EP_Collate. Thus the following two ALWAYS. */
158 if( p->x.pList!=0 && ALWAYS(!ExprHasProperty(p, EP_xIsSelect)) ){
drh2308ed32015-02-09 16:09:34 +0000159 int i;
drh6728cd92015-02-09 18:28:03 +0000160 for(i=0; ALWAYS(i<p->x.pList->nExpr); i++){
drh2308ed32015-02-09 16:09:34 +0000161 if( ExprHasProperty(p->x.pList->a[i].pExpr, EP_Collate) ){
162 pNext = p->x.pList->a[i].pExpr;
163 break;
164 }
165 }
166 }
167 p = pNext;
drhae80dde2012-12-06 21:16:43 +0000168 }
169 }else{
drh7d10d5a2008-08-20 16:35:10 +0000170 break;
171 }
danielk19770202b292004-06-09 09:55:16 +0000172 }
danielk19777cedc8d2004-06-10 10:50:08 +0000173 if( sqlite3CheckCollSeq(pParse, pColl) ){
174 pColl = 0;
175 }
176 return pColl;
danielk19770202b292004-06-09 09:55:16 +0000177}
178
179/*
drh626a8792005-01-17 22:08:19 +0000180** pExpr is an operand of a comparison operator. aff2 is the
181** type affinity of the other operand. This routine returns the
drh53db1452004-05-20 13:54:53 +0000182** type affinity that should be used for the comparison operator.
183*/
danielk1977e014a832004-05-17 10:48:57 +0000184char sqlite3CompareAffinity(Expr *pExpr, char aff2){
danielk1977bf3b7212004-05-18 10:06:24 +0000185 char aff1 = sqlite3ExprAffinity(pExpr);
danielk1977e014a832004-05-17 10:48:57 +0000186 if( aff1 && aff2 ){
drh8df447f2005-11-01 15:48:24 +0000187 /* Both sides of the comparison are columns. If one has numeric
188 ** affinity, use that. Otherwise use no affinity.
danielk1977e014a832004-05-17 10:48:57 +0000189 */
drh8a512562005-11-14 22:29:05 +0000190 if( sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2) ){
danielk1977e014a832004-05-17 10:48:57 +0000191 return SQLITE_AFF_NUMERIC;
192 }else{
drh05883a32015-06-02 15:32:08 +0000193 return SQLITE_AFF_BLOB;
danielk1977e014a832004-05-17 10:48:57 +0000194 }
195 }else if( !aff1 && !aff2 ){
drh5f6a87b2004-07-19 00:39:45 +0000196 /* Neither side of the comparison is a column. Compare the
197 ** results directly.
danielk1977e014a832004-05-17 10:48:57 +0000198 */
drh05883a32015-06-02 15:32:08 +0000199 return SQLITE_AFF_BLOB;
danielk1977e014a832004-05-17 10:48:57 +0000200 }else{
201 /* One side is a column, the other is not. Use the columns affinity. */
drhfe05af82005-07-21 03:14:59 +0000202 assert( aff1==0 || aff2==0 );
danielk1977e014a832004-05-17 10:48:57 +0000203 return (aff1 + aff2);
204 }
205}
206
drh53db1452004-05-20 13:54:53 +0000207/*
208** pExpr is a comparison operator. Return the type affinity that should
209** be applied to both operands prior to doing the comparison.
210*/
danielk1977e014a832004-05-17 10:48:57 +0000211static char comparisonAffinity(Expr *pExpr){
212 char aff;
213 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
214 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
drh6a2fe092009-09-23 02:29:36 +0000215 pExpr->op==TK_NE || pExpr->op==TK_IS || pExpr->op==TK_ISNOT );
danielk1977e014a832004-05-17 10:48:57 +0000216 assert( pExpr->pLeft );
danielk1977bf3b7212004-05-18 10:06:24 +0000217 aff = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +0000218 if( pExpr->pRight ){
219 aff = sqlite3CompareAffinity(pExpr->pRight, aff);
danielk19776ab3a2e2009-02-19 14:39:25 +0000220 }else if( ExprHasProperty(pExpr, EP_xIsSelect) ){
221 aff = sqlite3CompareAffinity(pExpr->x.pSelect->pEList->a[0].pExpr, aff);
222 }else if( !aff ){
drh05883a32015-06-02 15:32:08 +0000223 aff = SQLITE_AFF_BLOB;
danielk1977e014a832004-05-17 10:48:57 +0000224 }
225 return aff;
226}
227
228/*
229** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
230** idx_affinity is the affinity of an indexed column. Return true
231** if the index with affinity idx_affinity may be used to implement
232** the comparison in pExpr.
233*/
234int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){
235 char aff = comparisonAffinity(pExpr);
drh8a512562005-11-14 22:29:05 +0000236 switch( aff ){
drh05883a32015-06-02 15:32:08 +0000237 case SQLITE_AFF_BLOB:
drh8a512562005-11-14 22:29:05 +0000238 return 1;
239 case SQLITE_AFF_TEXT:
240 return idx_affinity==SQLITE_AFF_TEXT;
241 default:
242 return sqlite3IsNumericAffinity(idx_affinity);
243 }
danielk1977e014a832004-05-17 10:48:57 +0000244}
245
danielk1977a37cdde2004-05-16 11:15:36 +0000246/*
drh35573352008-01-08 23:54:25 +0000247** Return the P5 value that should be used for a binary comparison
danielk1977a37cdde2004-05-16 11:15:36 +0000248** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
danielk1977a37cdde2004-05-16 11:15:36 +0000249*/
drh35573352008-01-08 23:54:25 +0000250static u8 binaryCompareP5(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){
251 u8 aff = (char)sqlite3ExprAffinity(pExpr2);
drh1bd10f82008-12-10 21:19:56 +0000252 aff = (u8)sqlite3CompareAffinity(pExpr1, aff) | (u8)jumpIfNull;
drh35573352008-01-08 23:54:25 +0000253 return aff;
danielk1977a37cdde2004-05-16 11:15:36 +0000254}
255
drha2e00042002-01-22 03:13:42 +0000256/*
danielk19770202b292004-06-09 09:55:16 +0000257** Return a pointer to the collation sequence that should be used by
258** a binary comparison operator comparing pLeft and pRight.
259**
260** If the left hand expression has a collating sequence type, then it is
261** used. Otherwise the collation sequence for the right hand expression
262** is used, or the default (BINARY) if neither expression has a collating
263** type.
danielk1977bcbb04e2007-05-29 12:11:29 +0000264**
265** Argument pRight (but not pLeft) may be a null pointer. In this case,
266** it is not considered.
danielk19770202b292004-06-09 09:55:16 +0000267*/
drh0a0e1312007-08-07 17:04:59 +0000268CollSeq *sqlite3BinaryCompareCollSeq(
danielk1977bcbb04e2007-05-29 12:11:29 +0000269 Parse *pParse,
270 Expr *pLeft,
271 Expr *pRight
272){
drhec41dda2007-02-07 13:09:45 +0000273 CollSeq *pColl;
274 assert( pLeft );
drhae80dde2012-12-06 21:16:43 +0000275 if( pLeft->flags & EP_Collate ){
276 pColl = sqlite3ExprCollSeq(pParse, pLeft);
277 }else if( pRight && (pRight->flags & EP_Collate)!=0 ){
278 pColl = sqlite3ExprCollSeq(pParse, pRight);
drhec41dda2007-02-07 13:09:45 +0000279 }else{
280 pColl = sqlite3ExprCollSeq(pParse, pLeft);
281 if( !pColl ){
282 pColl = sqlite3ExprCollSeq(pParse, pRight);
283 }
danielk19770202b292004-06-09 09:55:16 +0000284 }
285 return pColl;
286}
287
288/*
drhbe5c89a2004-07-26 00:31:09 +0000289** Generate code for a comparison operator.
290*/
291static int codeCompare(
292 Parse *pParse, /* The parsing (and code generating) context */
293 Expr *pLeft, /* The left operand */
294 Expr *pRight, /* The right operand */
295 int opcode, /* The comparison opcode */
drh35573352008-01-08 23:54:25 +0000296 int in1, int in2, /* Register holding operands */
drhbe5c89a2004-07-26 00:31:09 +0000297 int dest, /* Jump here if true. */
298 int jumpIfNull /* If true, jump if either operand is NULL */
299){
drh35573352008-01-08 23:54:25 +0000300 int p5;
301 int addr;
302 CollSeq *p4;
303
304 p4 = sqlite3BinaryCompareCollSeq(pParse, pLeft, pRight);
305 p5 = binaryCompareP5(pLeft, pRight, jumpIfNull);
306 addr = sqlite3VdbeAddOp4(pParse->pVdbe, opcode, in2, dest, in1,
307 (void*)p4, P4_COLLSEQ);
drh1bd10f82008-12-10 21:19:56 +0000308 sqlite3VdbeChangeP5(pParse->pVdbe, (u8)p5);
drh35573352008-01-08 23:54:25 +0000309 return addr;
drhbe5c89a2004-07-26 00:31:09 +0000310}
311
dan71c57db2016-07-09 20:23:55 +0000312int sqlite3ExprVectorSize(Expr *pExpr){
313 if( (pExpr->flags & EP_Vector)==0 ) return 1;
314 if( pExpr->flags & EP_xIsSelect ){
315 return pExpr->x.pSelect->pEList->nExpr;
316 }
317 return pExpr->x.pList->nExpr;
318}
319
320static Expr *exprVectorField(Expr *pVector, int i){
321 if( pVector->flags & EP_xIsSelect ){
322 return pVector->x.pSelect->pEList->a[i].pExpr;
323 }
324 return pVector->x.pList->a[i].pExpr;
325}
326
327static void codeVectorCompare(Parse *pParse, Expr *pExpr, int dest){
328 Vdbe *v = pParse->pVdbe;
329 Expr *pLeft = pExpr->pLeft;
330 Expr *pRight = pExpr->pRight;
331 int nLeft = sqlite3ExprVectorSize(pLeft);
332 int nRight = sqlite3ExprVectorSize(pRight);
333 int addr = sqlite3VdbeMakeLabel(v);
334
335 /* Check that both sides of the comparison are vectors, and that
336 ** both are the same length. */
337 if( nLeft!=nRight ){
338 sqlite3ErrorMsg(pParse, "invalid use of row value");
339 }else{
340 int p5 = (pExpr->op==TK_IS || pExpr->op==TK_ISNOT) ? SQLITE_NULLEQ : 0;
341 int opCmp;
342 int opTest;
343 int i;
344 int p3 = 1;
345 int regLeft = 0;
346 int regRight = 0;
347
348 assert( pExpr->op==TK_EQ || pExpr->op==TK_NE
349 || pExpr->op==TK_IS || pExpr->op==TK_ISNOT
350 || pExpr->op==TK_LT || pExpr->op==TK_GT
351 || pExpr->op==TK_LE || pExpr->op==TK_GE
352 );
353
354 switch( pExpr->op ){
355 case TK_EQ:
356 case TK_IS:
357 opTest = OP_IfNot;
358 opCmp = OP_Eq;
359 break;
360
361 case TK_NE:
362 case TK_ISNOT:
363 opTest = OP_If;
364 opCmp = OP_Ne;
365 break;
366
367 case TK_LT:
368 case TK_LE:
369 case TK_GT:
370 case TK_GE:
371 opCmp = OP_Cmp;
372 opTest = OP_CmpTest;
373 p3 = pExpr->op;
374 break;
375 }
376
377 if( pLeft->flags & EP_xIsSelect ){
378 assert( pLeft->op==TK_SELECT || pLeft->op==TK_REGISTER );
379 regLeft = sqlite3ExprCodeTarget(pParse, pLeft, 1);
380 assert( regLeft!=1 );
381 }
382 if( pRight->flags & EP_xIsSelect ){
383 assert( pRight->op==TK_SELECT || pRight->op==TK_REGISTER );
384 regRight = sqlite3ExprCodeTarget(pParse, pRight, 1);
385 assert( regRight!=1 );
386 }
387 if( pParse->nErr ) return;
388
389 for(i=0; i<nLeft; i++){
390 int regFree1 = 0, regFree2 = 0;
391 Expr *pL, *pR;
392 int r1, r2;
393
394 if( regLeft ){
395 pL = pLeft->x.pSelect->pEList->a[i].pExpr;
396 r1 = regLeft+i;
397 }else{
398 pL = pLeft->x.pList->a[i].pExpr;
399 r1 = sqlite3ExprCodeTemp(pParse, pL, &regFree1);
400 }
401
402 if( regRight ){
403 pR = pRight->x.pSelect->pEList->a[i].pExpr;
404 r2 = regRight+i;
405 }else{
406 pR = pRight->x.pList->a[i].pExpr;
407 r2 = sqlite3ExprCodeTemp(pParse, pR, &regFree1);
408 }
409
410 codeCompare(pParse, pL, pR, opCmp, r1, r2, dest, SQLITE_STOREP2 | p5);
411 sqlite3VdbeAddOp3(v, opTest, dest, addr, p3);
412 sqlite3ReleaseTempReg(pParse, regFree1);
413 sqlite3ReleaseTempReg(pParse, regFree2);
414 }
415 }
416
417 sqlite3VdbeResolveLabel(v, addr);
418}
419
danielk19774b5255a2008-06-05 16:47:39 +0000420#if SQLITE_MAX_EXPR_DEPTH>0
421/*
422** Check that argument nHeight is less than or equal to the maximum
423** expression depth allowed. If it is not, leave an error message in
424** pParse.
425*/
drh7d10d5a2008-08-20 16:35:10 +0000426int sqlite3ExprCheckHeight(Parse *pParse, int nHeight){
danielk19774b5255a2008-06-05 16:47:39 +0000427 int rc = SQLITE_OK;
428 int mxHeight = pParse->db->aLimit[SQLITE_LIMIT_EXPR_DEPTH];
429 if( nHeight>mxHeight ){
430 sqlite3ErrorMsg(pParse,
431 "Expression tree is too large (maximum depth %d)", mxHeight
432 );
433 rc = SQLITE_ERROR;
434 }
435 return rc;
436}
437
438/* The following three functions, heightOfExpr(), heightOfExprList()
439** and heightOfSelect(), are used to determine the maximum height
440** of any expression tree referenced by the structure passed as the
441** first argument.
442**
443** If this maximum height is greater than the current value pointed
444** to by pnHeight, the second parameter, then set *pnHeight to that
445** value.
446*/
447static void heightOfExpr(Expr *p, int *pnHeight){
448 if( p ){
449 if( p->nHeight>*pnHeight ){
450 *pnHeight = p->nHeight;
451 }
452 }
453}
454static void heightOfExprList(ExprList *p, int *pnHeight){
455 if( p ){
456 int i;
457 for(i=0; i<p->nExpr; i++){
458 heightOfExpr(p->a[i].pExpr, pnHeight);
459 }
460 }
461}
462static void heightOfSelect(Select *p, int *pnHeight){
463 if( p ){
464 heightOfExpr(p->pWhere, pnHeight);
465 heightOfExpr(p->pHaving, pnHeight);
466 heightOfExpr(p->pLimit, pnHeight);
467 heightOfExpr(p->pOffset, pnHeight);
468 heightOfExprList(p->pEList, pnHeight);
469 heightOfExprList(p->pGroupBy, pnHeight);
470 heightOfExprList(p->pOrderBy, pnHeight);
471 heightOfSelect(p->pPrior, pnHeight);
472 }
473}
474
475/*
476** Set the Expr.nHeight variable in the structure passed as an
477** argument. An expression with no children, Expr.pList or
478** Expr.pSelect member has a height of 1. Any other expression
479** has a height equal to the maximum height of any other
480** referenced Expr plus one.
drh2308ed32015-02-09 16:09:34 +0000481**
482** Also propagate EP_Propagate flags up from Expr.x.pList to Expr.flags,
483** if appropriate.
danielk19774b5255a2008-06-05 16:47:39 +0000484*/
485static void exprSetHeight(Expr *p){
486 int nHeight = 0;
487 heightOfExpr(p->pLeft, &nHeight);
488 heightOfExpr(p->pRight, &nHeight);
danielk19776ab3a2e2009-02-19 14:39:25 +0000489 if( ExprHasProperty(p, EP_xIsSelect) ){
490 heightOfSelect(p->x.pSelect, &nHeight);
drh2308ed32015-02-09 16:09:34 +0000491 }else if( p->x.pList ){
danielk19776ab3a2e2009-02-19 14:39:25 +0000492 heightOfExprList(p->x.pList, &nHeight);
drh2308ed32015-02-09 16:09:34 +0000493 p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList);
danielk19776ab3a2e2009-02-19 14:39:25 +0000494 }
danielk19774b5255a2008-06-05 16:47:39 +0000495 p->nHeight = nHeight + 1;
496}
497
498/*
499** Set the Expr.nHeight variable using the exprSetHeight() function. If
500** the height is greater than the maximum allowed expression depth,
501** leave an error in pParse.
drh2308ed32015-02-09 16:09:34 +0000502**
503** Also propagate all EP_Propagate flags from the Expr.x.pList into
504** Expr.flags.
danielk19774b5255a2008-06-05 16:47:39 +0000505*/
drh2308ed32015-02-09 16:09:34 +0000506void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p){
drh74893a42015-03-22 10:23:17 +0000507 if( pParse->nErr ) return;
danielk19774b5255a2008-06-05 16:47:39 +0000508 exprSetHeight(p);
drh7d10d5a2008-08-20 16:35:10 +0000509 sqlite3ExprCheckHeight(pParse, p->nHeight);
danielk19774b5255a2008-06-05 16:47:39 +0000510}
511
512/*
513** Return the maximum height of any expression tree referenced
514** by the select statement passed as an argument.
515*/
516int sqlite3SelectExprHeight(Select *p){
517 int nHeight = 0;
518 heightOfSelect(p, &nHeight);
519 return nHeight;
520}
drh2308ed32015-02-09 16:09:34 +0000521#else /* ABOVE: Height enforcement enabled. BELOW: Height enforcement off */
522/*
523** Propagate all EP_Propagate flags from the Expr.x.pList into
524** Expr.flags.
525*/
526void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p){
527 if( p && p->x.pList && !ExprHasProperty(p, EP_xIsSelect) ){
528 p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList);
529 }
530}
531#define exprSetHeight(y)
danielk19774b5255a2008-06-05 16:47:39 +0000532#endif /* SQLITE_MAX_EXPR_DEPTH>0 */
533
drhbe5c89a2004-07-26 00:31:09 +0000534/*
drhb7916a72009-05-27 10:31:29 +0000535** This routine is the core allocator for Expr nodes.
536**
drha76b5df2002-02-23 02:32:10 +0000537** Construct a new expression node and return a pointer to it. Memory
drhb7916a72009-05-27 10:31:29 +0000538** for this node and for the pToken argument is a single allocation
539** obtained from sqlite3DbMalloc(). The calling function
drha76b5df2002-02-23 02:32:10 +0000540** is responsible for making sure the node eventually gets freed.
drhb7916a72009-05-27 10:31:29 +0000541**
542** If dequote is true, then the token (if it exists) is dequoted.
drhe792b5b2015-08-23 20:48:29 +0000543** If dequote is false, no dequoting is performed. The deQuote
drhb7916a72009-05-27 10:31:29 +0000544** parameter is ignored if pToken is NULL or if the token does not
545** appear to be quoted. If the quotes were of the form "..." (double-quotes)
546** then the EP_DblQuoted flag is set on the expression node.
drh33e619f2009-05-28 01:00:55 +0000547**
548** Special case: If op==TK_INTEGER and pToken points to a string that
549** can be translated into a 32-bit integer, then the token is not
550** stored in u.zToken. Instead, the integer values is written
551** into u.iValue and the EP_IntValue flag is set. No extra storage
552** is allocated to hold the integer text and the dequote flag is ignored.
drha76b5df2002-02-23 02:32:10 +0000553*/
drhb7916a72009-05-27 10:31:29 +0000554Expr *sqlite3ExprAlloc(
danielk1977a1644fd2007-08-29 12:31:25 +0000555 sqlite3 *db, /* Handle for sqlite3DbMallocZero() (may be null) */
drh17435752007-08-16 04:30:38 +0000556 int op, /* Expression opcode */
drhb7916a72009-05-27 10:31:29 +0000557 const Token *pToken, /* Token argument. Might be NULL */
558 int dequote /* True to dequote */
drh17435752007-08-16 04:30:38 +0000559){
drha76b5df2002-02-23 02:32:10 +0000560 Expr *pNew;
drh33e619f2009-05-28 01:00:55 +0000561 int nExtra = 0;
shanecf697392009-06-01 16:53:09 +0000562 int iValue = 0;
danielk1977fc976062007-05-10 10:46:56 +0000563
drh575fad62016-02-05 13:38:36 +0000564 assert( db!=0 );
drhb7916a72009-05-27 10:31:29 +0000565 if( pToken ){
drh33e619f2009-05-28 01:00:55 +0000566 if( op!=TK_INTEGER || pToken->z==0
567 || sqlite3GetInt32(pToken->z, &iValue)==0 ){
568 nExtra = pToken->n+1;
drhd50ffc42011-03-08 02:38:28 +0000569 assert( iValue>=0 );
drh33e619f2009-05-28 01:00:55 +0000570 }
drhb7916a72009-05-27 10:31:29 +0000571 }
drh575fad62016-02-05 13:38:36 +0000572 pNew = sqlite3DbMallocRawNN(db, sizeof(Expr)+nExtra);
drhb7916a72009-05-27 10:31:29 +0000573 if( pNew ){
drhca3862d2016-01-08 12:46:39 +0000574 memset(pNew, 0, sizeof(Expr));
drhb7916a72009-05-27 10:31:29 +0000575 pNew->op = (u8)op;
576 pNew->iAgg = -1;
577 if( pToken ){
drh33e619f2009-05-28 01:00:55 +0000578 if( nExtra==0 ){
579 pNew->flags |= EP_IntValue;
580 pNew->u.iValue = iValue;
581 }else{
drh33e619f2009-05-28 01:00:55 +0000582 pNew->u.zToken = (char*)&pNew[1];
drhb07028f2011-10-14 21:49:18 +0000583 assert( pToken->z!=0 || pToken->n==0 );
584 if( pToken->n ) memcpy(pNew->u.zToken, pToken->z, pToken->n);
drh33e619f2009-05-28 01:00:55 +0000585 pNew->u.zToken[pToken->n] = 0;
drh244b9d62016-04-11 19:01:08 +0000586 if( dequote && sqlite3Isquote(pNew->u.zToken[0]) ){
587 if( pNew->u.zToken[0]=='"' ) pNew->flags |= EP_DblQuoted;
drh33e619f2009-05-28 01:00:55 +0000588 sqlite3Dequote(pNew->u.zToken);
drh33e619f2009-05-28 01:00:55 +0000589 }
drhb7916a72009-05-27 10:31:29 +0000590 }
591 }
592#if SQLITE_MAX_EXPR_DEPTH>0
593 pNew->nHeight = 1;
594#endif
595 }
drha76b5df2002-02-23 02:32:10 +0000596 return pNew;
597}
598
599/*
drhb7916a72009-05-27 10:31:29 +0000600** Allocate a new expression node from a zero-terminated token that has
601** already been dequoted.
602*/
603Expr *sqlite3Expr(
604 sqlite3 *db, /* Handle for sqlite3DbMallocZero() (may be null) */
605 int op, /* Expression opcode */
606 const char *zToken /* Token argument. Might be NULL */
607){
608 Token x;
609 x.z = zToken;
610 x.n = zToken ? sqlite3Strlen30(zToken) : 0;
611 return sqlite3ExprAlloc(db, op, &x, 0);
612}
613
614/*
615** Attach subtrees pLeft and pRight to the Expr node pRoot.
616**
617** If pRoot==NULL that means that a memory allocation error has occurred.
618** In that case, delete the subtrees pLeft and pRight.
619*/
620void sqlite3ExprAttachSubtrees(
621 sqlite3 *db,
622 Expr *pRoot,
623 Expr *pLeft,
624 Expr *pRight
625){
626 if( pRoot==0 ){
627 assert( db->mallocFailed );
628 sqlite3ExprDelete(db, pLeft);
629 sqlite3ExprDelete(db, pRight);
630 }else{
631 if( pRight ){
632 pRoot->pRight = pRight;
drh885a5b02015-02-09 15:21:36 +0000633 pRoot->flags |= EP_Propagate & pRight->flags;
drhb7916a72009-05-27 10:31:29 +0000634 }
635 if( pLeft ){
636 pRoot->pLeft = pLeft;
drh885a5b02015-02-09 15:21:36 +0000637 pRoot->flags |= EP_Propagate & pLeft->flags;
drhb7916a72009-05-27 10:31:29 +0000638 }
639 exprSetHeight(pRoot);
640 }
641}
642
643/*
peter.d.reid60ec9142014-09-06 16:39:46 +0000644** Allocate an Expr node which joins as many as two subtrees.
drhb7916a72009-05-27 10:31:29 +0000645**
drhbf664462009-06-19 18:32:54 +0000646** One or both of the subtrees can be NULL. Return a pointer to the new
647** Expr node. Or, if an OOM error occurs, set pParse->db->mallocFailed,
648** free the subtrees and return NULL.
drh206f3d92006-07-11 13:15:08 +0000649*/
drh17435752007-08-16 04:30:38 +0000650Expr *sqlite3PExpr(
651 Parse *pParse, /* Parsing context */
652 int op, /* Expression opcode */
653 Expr *pLeft, /* Left operand */
654 Expr *pRight, /* Right operand */
655 const Token *pToken /* Argument token */
656){
drh5fb52ca2012-03-31 02:34:35 +0000657 Expr *p;
drh1167d322015-10-28 20:01:45 +0000658 if( op==TK_AND && pParse->nErr==0 ){
drh5fb52ca2012-03-31 02:34:35 +0000659 /* Take advantage of short-circuit false optimization for AND */
660 p = sqlite3ExprAnd(pParse->db, pLeft, pRight);
661 }else{
drh1167d322015-10-28 20:01:45 +0000662 p = sqlite3ExprAlloc(pParse->db, op & TKFLG_MASK, pToken, 1);
drh5fb52ca2012-03-31 02:34:35 +0000663 sqlite3ExprAttachSubtrees(pParse->db, p, pLeft, pRight);
664 }
dan2b359bd2010-10-28 11:31:23 +0000665 if( p ) {
666 sqlite3ExprCheckHeight(pParse, p->nHeight);
667 }
drh4e0cff62004-11-05 05:10:28 +0000668 return p;
669}
670
671/*
drh08de4f72016-04-11 01:06:47 +0000672** Add pSelect to the Expr.x.pSelect field. Or, if pExpr is NULL (due
673** do a memory allocation failure) then delete the pSelect object.
674*/
675void sqlite3PExprAddSelect(Parse *pParse, Expr *pExpr, Select *pSelect){
676 if( pExpr ){
677 pExpr->x.pSelect = pSelect;
678 ExprSetProperty(pExpr, EP_xIsSelect|EP_Subquery);
679 sqlite3ExprSetHeightAndFlags(pParse, pExpr);
680 }else{
681 assert( pParse->db->mallocFailed );
682 sqlite3SelectDelete(pParse->db, pSelect);
683 }
684}
685
686
687/*
drh991a1982014-01-02 17:57:16 +0000688** If the expression is always either TRUE or FALSE (respectively),
689** then return 1. If one cannot determine the truth value of the
690** expression at compile-time return 0.
691**
692** This is an optimization. If is OK to return 0 here even if
693** the expression really is always false or false (a false negative).
694** But it is a bug to return 1 if the expression might have different
695** boolean values in different circumstances (a false positive.)
drh5fb52ca2012-03-31 02:34:35 +0000696**
697** Note that if the expression is part of conditional for a
698** LEFT JOIN, then we cannot determine at compile-time whether or not
699** is it true or false, so always return 0.
700*/
drh991a1982014-01-02 17:57:16 +0000701static int exprAlwaysTrue(Expr *p){
702 int v = 0;
703 if( ExprHasProperty(p, EP_FromJoin) ) return 0;
704 if( !sqlite3ExprIsInteger(p, &v) ) return 0;
705 return v!=0;
706}
drh5fb52ca2012-03-31 02:34:35 +0000707static int exprAlwaysFalse(Expr *p){
708 int v = 0;
709 if( ExprHasProperty(p, EP_FromJoin) ) return 0;
710 if( !sqlite3ExprIsInteger(p, &v) ) return 0;
711 return v==0;
712}
713
714/*
drh91bb0ee2004-09-01 03:06:34 +0000715** Join two expressions using an AND operator. If either expression is
716** NULL, then just return the other expression.
drh5fb52ca2012-03-31 02:34:35 +0000717**
718** If one side or the other of the AND is known to be false, then instead
719** of returning an AND expression, just return a constant expression with
720** a value of false.
drh91bb0ee2004-09-01 03:06:34 +0000721*/
danielk19771e536952007-08-16 10:09:01 +0000722Expr *sqlite3ExprAnd(sqlite3 *db, Expr *pLeft, Expr *pRight){
drh91bb0ee2004-09-01 03:06:34 +0000723 if( pLeft==0 ){
724 return pRight;
725 }else if( pRight==0 ){
726 return pLeft;
drh5fb52ca2012-03-31 02:34:35 +0000727 }else if( exprAlwaysFalse(pLeft) || exprAlwaysFalse(pRight) ){
728 sqlite3ExprDelete(db, pLeft);
729 sqlite3ExprDelete(db, pRight);
730 return sqlite3ExprAlloc(db, TK_INTEGER, &sqlite3IntTokens[0], 0);
drh91bb0ee2004-09-01 03:06:34 +0000731 }else{
drhb7916a72009-05-27 10:31:29 +0000732 Expr *pNew = sqlite3ExprAlloc(db, TK_AND, 0, 0);
733 sqlite3ExprAttachSubtrees(db, pNew, pLeft, pRight);
734 return pNew;
drha76b5df2002-02-23 02:32:10 +0000735 }
736}
737
738/*
739** Construct a new expression node for a function with multiple
740** arguments.
741*/
drh17435752007-08-16 04:30:38 +0000742Expr *sqlite3ExprFunction(Parse *pParse, ExprList *pList, Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000743 Expr *pNew;
drh633e6d52008-07-28 19:34:53 +0000744 sqlite3 *db = pParse->db;
danielk19774b202ae2006-01-23 05:50:58 +0000745 assert( pToken );
drhb7916a72009-05-27 10:31:29 +0000746 pNew = sqlite3ExprAlloc(db, TK_FUNCTION, pToken, 1);
drha76b5df2002-02-23 02:32:10 +0000747 if( pNew==0 ){
drhd9da78a2009-03-24 15:08:09 +0000748 sqlite3ExprListDelete(db, pList); /* Avoid memory leak when malloc fails */
drha76b5df2002-02-23 02:32:10 +0000749 return 0;
750 }
danielk19776ab3a2e2009-02-19 14:39:25 +0000751 pNew->x.pList = pList;
752 assert( !ExprHasProperty(pNew, EP_xIsSelect) );
drh2308ed32015-02-09 16:09:34 +0000753 sqlite3ExprSetHeightAndFlags(pParse, pNew);
drha76b5df2002-02-23 02:32:10 +0000754 return pNew;
755}
756
757/*
drhfa6bc002004-09-07 16:19:52 +0000758** Assign a variable number to an expression that encodes a wildcard
759** in the original SQL statement.
760**
761** Wildcards consisting of a single "?" are assigned the next sequential
762** variable number.
763**
764** Wildcards of the form "?nnn" are assigned the number "nnn". We make
765** sure "nnn" is not too be to avoid a denial of service attack when
766** the SQL statement comes from an external source.
767**
drh51f49f12009-05-21 20:41:32 +0000768** Wildcards of the form ":aaa", "@aaa", or "$aaa" are assigned the same number
drhfa6bc002004-09-07 16:19:52 +0000769** as the previous instance of the same wildcard. Or if this is the first
peter.d.reid60ec9142014-09-06 16:39:46 +0000770** instance of the wildcard, the next sequential variable number is
drhfa6bc002004-09-07 16:19:52 +0000771** assigned.
772*/
773void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){
drh17435752007-08-16 04:30:38 +0000774 sqlite3 *db = pParse->db;
drhb7916a72009-05-27 10:31:29 +0000775 const char *z;
drh17435752007-08-16 04:30:38 +0000776
drhfa6bc002004-09-07 16:19:52 +0000777 if( pExpr==0 ) return;
drhc5cd1242013-09-12 16:50:49 +0000778 assert( !ExprHasProperty(pExpr, EP_IntValue|EP_Reduced|EP_TokenOnly) );
drh33e619f2009-05-28 01:00:55 +0000779 z = pExpr->u.zToken;
drhb7916a72009-05-27 10:31:29 +0000780 assert( z!=0 );
781 assert( z[0]!=0 );
782 if( z[1]==0 ){
drhfa6bc002004-09-07 16:19:52 +0000783 /* Wildcard of the form "?". Assign the next variable number */
drhb7916a72009-05-27 10:31:29 +0000784 assert( z[0]=='?' );
drh8677d302009-11-04 13:17:14 +0000785 pExpr->iColumn = (ynVar)(++pParse->nVar);
drhfa6bc002004-09-07 16:19:52 +0000786 }else{
drh124c0b42011-06-01 18:15:55 +0000787 ynVar x = 0;
788 u32 n = sqlite3Strlen30(z);
789 if( z[0]=='?' ){
790 /* Wildcard of the form "?nnn". Convert "nnn" to an integer and
791 ** use it as the variable number */
792 i64 i;
793 int bOk = 0==sqlite3Atoi64(&z[1], &i, n-1, SQLITE_UTF8);
794 pExpr->iColumn = x = (ynVar)i;
795 testcase( i==0 );
796 testcase( i==1 );
797 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 );
798 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] );
799 if( bOk==0 || i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
800 sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
801 db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]);
802 x = 0;
drhfa6bc002004-09-07 16:19:52 +0000803 }
drh124c0b42011-06-01 18:15:55 +0000804 if( i>pParse->nVar ){
805 pParse->nVar = (int)i;
806 }
807 }else{
808 /* Wildcards like ":aaa", "$aaa" or "@aaa". Reuse the same variable
809 ** number as the prior appearance of the same name, or if the name
810 ** has never appeared before, reuse the same variable number
811 */
812 ynVar i;
813 for(i=0; i<pParse->nzVar; i++){
drh503a6862013-03-01 01:07:17 +0000814 if( pParse->azVar[i] && strcmp(pParse->azVar[i],z)==0 ){
drh124c0b42011-06-01 18:15:55 +0000815 pExpr->iColumn = x = (ynVar)i+1;
816 break;
817 }
818 }
819 if( x==0 ) x = pExpr->iColumn = (ynVar)(++pParse->nVar);
drhfa6bc002004-09-07 16:19:52 +0000820 }
drh124c0b42011-06-01 18:15:55 +0000821 if( x>0 ){
822 if( x>pParse->nzVar ){
823 char **a;
824 a = sqlite3DbRealloc(db, pParse->azVar, x*sizeof(a[0]));
drh4a642b62016-02-05 01:55:27 +0000825 if( a==0 ){
826 assert( db->mallocFailed ); /* Error reported through mallocFailed */
827 return;
828 }
drh124c0b42011-06-01 18:15:55 +0000829 pParse->azVar = a;
830 memset(&a[pParse->nzVar], 0, (x-pParse->nzVar)*sizeof(a[0]));
831 pParse->nzVar = x;
drhfa6bc002004-09-07 16:19:52 +0000832 }
drh124c0b42011-06-01 18:15:55 +0000833 if( z[0]!='?' || pParse->azVar[x-1]==0 ){
834 sqlite3DbFree(db, pParse->azVar[x-1]);
835 pParse->azVar[x-1] = sqlite3DbStrNDup(db, z, n);
drhfa6bc002004-09-07 16:19:52 +0000836 }
837 }
838 }
drhbb4957f2008-03-20 14:03:29 +0000839 if( !pParse->nErr && pParse->nVar>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
danielk1977832b2662007-05-09 11:37:22 +0000840 sqlite3ErrorMsg(pParse, "too many SQL variables");
841 }
drhfa6bc002004-09-07 16:19:52 +0000842}
843
844/*
danf6963f92009-11-23 14:39:14 +0000845** Recursively delete an expression tree.
drha2e00042002-01-22 03:13:42 +0000846*/
drh4f0010b2016-04-11 14:49:39 +0000847static SQLITE_NOINLINE void sqlite3ExprDeleteNN(sqlite3 *db, Expr *p){
848 assert( p!=0 );
drhd50ffc42011-03-08 02:38:28 +0000849 /* Sanity check: Assert that the IntValue is non-negative if it exists */
850 assert( !ExprHasProperty(p, EP_IntValue) || p->u.iValue>=0 );
drhc5cd1242013-09-12 16:50:49 +0000851 if( !ExprHasProperty(p, EP_TokenOnly) ){
852 /* The Expr.x union is never used at the same time as Expr.pRight */
853 assert( p->x.pList==0 || p->pRight==0 );
dan71c57db2016-07-09 20:23:55 +0000854 if( p->op!=TK_SELECT_COLUMN ) sqlite3ExprDelete(db, p->pLeft);
drh33e619f2009-05-28 01:00:55 +0000855 sqlite3ExprDelete(db, p->pRight);
drhc5cd1242013-09-12 16:50:49 +0000856 if( ExprHasProperty(p, EP_MemToken) ) sqlite3DbFree(db, p->u.zToken);
danielk19776ab3a2e2009-02-19 14:39:25 +0000857 if( ExprHasProperty(p, EP_xIsSelect) ){
858 sqlite3SelectDelete(db, p->x.pSelect);
859 }else{
860 sqlite3ExprListDelete(db, p->x.pList);
861 }
862 }
drh33e619f2009-05-28 01:00:55 +0000863 if( !ExprHasProperty(p, EP_Static) ){
864 sqlite3DbFree(db, p);
865 }
drha2e00042002-01-22 03:13:42 +0000866}
drh4f0010b2016-04-11 14:49:39 +0000867void sqlite3ExprDelete(sqlite3 *db, Expr *p){
868 if( p ) sqlite3ExprDeleteNN(db, p);
869}
drha2e00042002-01-22 03:13:42 +0000870
drhd2687b72005-08-12 22:56:09 +0000871/*
danielk19776ab3a2e2009-02-19 14:39:25 +0000872** Return the number of bytes allocated for the expression structure
873** passed as the first argument. This is always one of EXPR_FULLSIZE,
874** EXPR_REDUCEDSIZE or EXPR_TOKENONLYSIZE.
875*/
876static int exprStructSize(Expr *p){
877 if( ExprHasProperty(p, EP_TokenOnly) ) return EXPR_TOKENONLYSIZE;
danielk19776ab3a2e2009-02-19 14:39:25 +0000878 if( ExprHasProperty(p, EP_Reduced) ) return EXPR_REDUCEDSIZE;
879 return EXPR_FULLSIZE;
880}
881
882/*
drh33e619f2009-05-28 01:00:55 +0000883** The dupedExpr*Size() routines each return the number of bytes required
884** to store a copy of an expression or expression tree. They differ in
885** how much of the tree is measured.
886**
887** dupedExprStructSize() Size of only the Expr structure
888** dupedExprNodeSize() Size of Expr + space for token
889** dupedExprSize() Expr + token + subtree components
890**
891***************************************************************************
892**
893** The dupedExprStructSize() function returns two values OR-ed together:
894** (1) the space required for a copy of the Expr structure only and
895** (2) the EP_xxx flags that indicate what the structure size should be.
896** The return values is always one of:
897**
898** EXPR_FULLSIZE
899** EXPR_REDUCEDSIZE | EP_Reduced
900** EXPR_TOKENONLYSIZE | EP_TokenOnly
901**
902** The size of the structure can be found by masking the return value
903** of this routine with 0xfff. The flags can be found by masking the
904** return value with EP_Reduced|EP_TokenOnly.
905**
906** Note that with flags==EXPRDUP_REDUCE, this routines works on full-size
907** (unreduced) Expr objects as they or originally constructed by the parser.
908** During expression analysis, extra information is computed and moved into
909** later parts of teh Expr object and that extra information might get chopped
910** off if the expression is reduced. Note also that it does not work to
peter.d.reid60ec9142014-09-06 16:39:46 +0000911** make an EXPRDUP_REDUCE copy of a reduced expression. It is only legal
drh33e619f2009-05-28 01:00:55 +0000912** to reduce a pristine expression tree from the parser. The implementation
913** of dupedExprStructSize() contain multiple assert() statements that attempt
914** to enforce this constraint.
danielk19776ab3a2e2009-02-19 14:39:25 +0000915*/
916static int dupedExprStructSize(Expr *p, int flags){
917 int nSize;
drh33e619f2009-05-28 01:00:55 +0000918 assert( flags==EXPRDUP_REDUCE || flags==0 ); /* Only one flag value allowed */
drhaecd8022013-09-13 18:15:15 +0000919 assert( EXPR_FULLSIZE<=0xfff );
920 assert( (0xfff & (EP_Reduced|EP_TokenOnly))==0 );
drh3c194692016-04-11 16:43:43 +0000921 if( 0==flags ){
danielk19776ab3a2e2009-02-19 14:39:25 +0000922 nSize = EXPR_FULLSIZE;
danielk19776ab3a2e2009-02-19 14:39:25 +0000923 }else{
drhc5cd1242013-09-12 16:50:49 +0000924 assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) );
drh33e619f2009-05-28 01:00:55 +0000925 assert( !ExprHasProperty(p, EP_FromJoin) );
drhc5cd1242013-09-12 16:50:49 +0000926 assert( !ExprHasProperty(p, EP_MemToken) );
drhebb6a652013-09-12 23:42:22 +0000927 assert( !ExprHasProperty(p, EP_NoReduce) );
drhaecd8022013-09-13 18:15:15 +0000928 if( p->pLeft || p->x.pList ){
drh33e619f2009-05-28 01:00:55 +0000929 nSize = EXPR_REDUCEDSIZE | EP_Reduced;
930 }else{
drhaecd8022013-09-13 18:15:15 +0000931 assert( p->pRight==0 );
drh33e619f2009-05-28 01:00:55 +0000932 nSize = EXPR_TOKENONLYSIZE | EP_TokenOnly;
933 }
danielk19776ab3a2e2009-02-19 14:39:25 +0000934 }
935 return nSize;
936}
937
938/*
drh33e619f2009-05-28 01:00:55 +0000939** This function returns the space in bytes required to store the copy
940** of the Expr structure and a copy of the Expr.u.zToken string (if that
941** string is defined.)
danielk19776ab3a2e2009-02-19 14:39:25 +0000942*/
943static int dupedExprNodeSize(Expr *p, int flags){
drh33e619f2009-05-28 01:00:55 +0000944 int nByte = dupedExprStructSize(p, flags) & 0xfff;
945 if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
946 nByte += sqlite3Strlen30(p->u.zToken)+1;
danielk19776ab3a2e2009-02-19 14:39:25 +0000947 }
danielk1977bc739712009-03-23 04:33:32 +0000948 return ROUND8(nByte);
danielk19776ab3a2e2009-02-19 14:39:25 +0000949}
950
951/*
952** Return the number of bytes required to create a duplicate of the
953** expression passed as the first argument. The second argument is a
954** mask containing EXPRDUP_XXX flags.
955**
956** The value returned includes space to create a copy of the Expr struct
drh33e619f2009-05-28 01:00:55 +0000957** itself and the buffer referred to by Expr.u.zToken, if any.
danielk19776ab3a2e2009-02-19 14:39:25 +0000958**
959** If the EXPRDUP_REDUCE flag is set, then the return value includes
960** space to duplicate all Expr nodes in the tree formed by Expr.pLeft
961** and Expr.pRight variables (but not for any structures pointed to or
962** descended from the Expr.x.pList or Expr.x.pSelect variables).
963*/
964static int dupedExprSize(Expr *p, int flags){
965 int nByte = 0;
966 if( p ){
967 nByte = dupedExprNodeSize(p, flags);
968 if( flags&EXPRDUP_REDUCE ){
drhb7916a72009-05-27 10:31:29 +0000969 nByte += dupedExprSize(p->pLeft, flags) + dupedExprSize(p->pRight, flags);
danielk19776ab3a2e2009-02-19 14:39:25 +0000970 }
971 }
972 return nByte;
973}
974
975/*
976** This function is similar to sqlite3ExprDup(), except that if pzBuffer
977** is not NULL then *pzBuffer is assumed to point to a buffer large enough
drh33e619f2009-05-28 01:00:55 +0000978** to store the copy of expression p, the copies of p->u.zToken
danielk19776ab3a2e2009-02-19 14:39:25 +0000979** (if applicable), and the copies of the p->pLeft and p->pRight expressions,
peter.d.reid60ec9142014-09-06 16:39:46 +0000980** if any. Before returning, *pzBuffer is set to the first byte past the
danielk19776ab3a2e2009-02-19 14:39:25 +0000981** portion of the buffer copied into by this function.
982*/
drh3c194692016-04-11 16:43:43 +0000983static Expr *exprDup(sqlite3 *db, Expr *p, int dupFlags, u8 **pzBuffer){
984 Expr *pNew; /* Value to return */
985 u8 *zAlloc; /* Memory space from which to build Expr object */
986 u32 staticFlag; /* EP_Static if space not obtained from malloc */
987
drh575fad62016-02-05 13:38:36 +0000988 assert( db!=0 );
drh3c194692016-04-11 16:43:43 +0000989 assert( p );
990 assert( dupFlags==0 || dupFlags==EXPRDUP_REDUCE );
991 assert( pzBuffer==0 || dupFlags==EXPRDUP_REDUCE );
danielk19776ab3a2e2009-02-19 14:39:25 +0000992
drh3c194692016-04-11 16:43:43 +0000993 /* Figure out where to write the new Expr structure. */
994 if( pzBuffer ){
995 zAlloc = *pzBuffer;
996 staticFlag = EP_Static;
997 }else{
998 zAlloc = sqlite3DbMallocRawNN(db, dupedExprSize(p, dupFlags));
999 staticFlag = 0;
1000 }
1001 pNew = (Expr *)zAlloc;
danielk19776ab3a2e2009-02-19 14:39:25 +00001002
drh3c194692016-04-11 16:43:43 +00001003 if( pNew ){
1004 /* Set nNewSize to the size allocated for the structure pointed to
1005 ** by pNew. This is either EXPR_FULLSIZE, EXPR_REDUCEDSIZE or
1006 ** EXPR_TOKENONLYSIZE. nToken is set to the number of bytes consumed
1007 ** by the copy of the p->u.zToken string (if any).
1008 */
1009 const unsigned nStructSize = dupedExprStructSize(p, dupFlags);
1010 const int nNewSize = nStructSize & 0xfff;
1011 int nToken;
1012 if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
1013 nToken = sqlite3Strlen30(p->u.zToken) + 1;
danielk19776ab3a2e2009-02-19 14:39:25 +00001014 }else{
drh3c194692016-04-11 16:43:43 +00001015 nToken = 0;
danielk19776ab3a2e2009-02-19 14:39:25 +00001016 }
drh3c194692016-04-11 16:43:43 +00001017 if( dupFlags ){
1018 assert( ExprHasProperty(p, EP_Reduced)==0 );
1019 memcpy(zAlloc, p, nNewSize);
1020 }else{
1021 u32 nSize = (u32)exprStructSize(p);
1022 memcpy(zAlloc, p, nSize);
1023 if( nSize<EXPR_FULLSIZE ){
1024 memset(&zAlloc[nSize], 0, EXPR_FULLSIZE-nSize);
1025 }
1026 }
danielk19776ab3a2e2009-02-19 14:39:25 +00001027
drh3c194692016-04-11 16:43:43 +00001028 /* Set the EP_Reduced, EP_TokenOnly, and EP_Static flags appropriately. */
1029 pNew->flags &= ~(EP_Reduced|EP_TokenOnly|EP_Static|EP_MemToken);
1030 pNew->flags |= nStructSize & (EP_Reduced|EP_TokenOnly);
1031 pNew->flags |= staticFlag;
1032
1033 /* Copy the p->u.zToken string, if any. */
1034 if( nToken ){
1035 char *zToken = pNew->u.zToken = (char*)&zAlloc[nNewSize];
1036 memcpy(zToken, p->u.zToken, nToken);
1037 }
1038
1039 if( 0==((p->flags|pNew->flags) & EP_TokenOnly) ){
1040 /* Fill in the pNew->x.pSelect or pNew->x.pList member. */
1041 if( ExprHasProperty(p, EP_xIsSelect) ){
1042 pNew->x.pSelect = sqlite3SelectDup(db, p->x.pSelect, dupFlags);
drh33e619f2009-05-28 01:00:55 +00001043 }else{
drh3c194692016-04-11 16:43:43 +00001044 pNew->x.pList = sqlite3ExprListDup(db, p->x.pList, dupFlags);
drh33e619f2009-05-28 01:00:55 +00001045 }
drh3c194692016-04-11 16:43:43 +00001046 }
1047
1048 /* Fill in pNew->pLeft and pNew->pRight. */
1049 if( ExprHasProperty(pNew, EP_Reduced|EP_TokenOnly) ){
1050 zAlloc += dupedExprNodeSize(p, dupFlags);
1051 if( ExprHasProperty(pNew, EP_Reduced) ){
1052 pNew->pLeft = p->pLeft ?
1053 exprDup(db, p->pLeft, EXPRDUP_REDUCE, &zAlloc) : 0;
1054 pNew->pRight = p->pRight ?
1055 exprDup(db, p->pRight, EXPRDUP_REDUCE, &zAlloc) : 0;
danielk19776ab3a2e2009-02-19 14:39:25 +00001056 }
drh3c194692016-04-11 16:43:43 +00001057 if( pzBuffer ){
1058 *pzBuffer = zAlloc;
danielk19776ab3a2e2009-02-19 14:39:25 +00001059 }
drh3c194692016-04-11 16:43:43 +00001060 }else{
1061 if( !ExprHasProperty(p, EP_TokenOnly) ){
1062 pNew->pLeft = sqlite3ExprDup(db, p->pLeft, 0);
1063 pNew->pRight = sqlite3ExprDup(db, p->pRight, 0);
danielk19776ab3a2e2009-02-19 14:39:25 +00001064 }
danielk19776ab3a2e2009-02-19 14:39:25 +00001065 }
1066 }
1067 return pNew;
1068}
1069
1070/*
danbfe31e72014-01-15 14:17:31 +00001071** Create and return a deep copy of the object passed as the second
1072** argument. If an OOM condition is encountered, NULL is returned
1073** and the db->mallocFailed flag set.
1074*/
daneede6a52014-01-15 19:42:23 +00001075#ifndef SQLITE_OMIT_CTE
danbfe31e72014-01-15 14:17:31 +00001076static With *withDup(sqlite3 *db, With *p){
dan4e9119d2014-01-13 15:12:23 +00001077 With *pRet = 0;
1078 if( p ){
1079 int nByte = sizeof(*p) + sizeof(p->a[0]) * (p->nCte-1);
1080 pRet = sqlite3DbMallocZero(db, nByte);
1081 if( pRet ){
1082 int i;
1083 pRet->nCte = p->nCte;
1084 for(i=0; i<p->nCte; i++){
1085 pRet->a[i].pSelect = sqlite3SelectDup(db, p->a[i].pSelect, 0);
1086 pRet->a[i].pCols = sqlite3ExprListDup(db, p->a[i].pCols, 0);
1087 pRet->a[i].zName = sqlite3DbStrDup(db, p->a[i].zName);
1088 }
1089 }
1090 }
1091 return pRet;
1092}
daneede6a52014-01-15 19:42:23 +00001093#else
1094# define withDup(x,y) 0
1095#endif
dan4e9119d2014-01-13 15:12:23 +00001096
drha76b5df2002-02-23 02:32:10 +00001097/*
drhff78bd22002-02-27 01:47:11 +00001098** The following group of routines make deep copies of expressions,
1099** expression lists, ID lists, and select statements. The copies can
1100** be deleted (by being passed to their respective ...Delete() routines)
1101** without effecting the originals.
1102**
danielk19774adee202004-05-08 08:23:19 +00001103** The expression list, ID, and source lists return by sqlite3ExprListDup(),
1104** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
drhad3cab52002-05-24 02:04:32 +00001105** by subsequent calls to sqlite*ListAppend() routines.
drhff78bd22002-02-27 01:47:11 +00001106**
drhad3cab52002-05-24 02:04:32 +00001107** Any tables that the SrcList might point to are not duplicated.
danielk19776ab3a2e2009-02-19 14:39:25 +00001108**
drhb7916a72009-05-27 10:31:29 +00001109** The flags parameter contains a combination of the EXPRDUP_XXX flags.
danielk19776ab3a2e2009-02-19 14:39:25 +00001110** If the EXPRDUP_REDUCE flag is set, then the structure returned is a
1111** truncated version of the usual Expr structure that will be stored as
1112** part of the in-memory representation of the database schema.
drhff78bd22002-02-27 01:47:11 +00001113*/
danielk19776ab3a2e2009-02-19 14:39:25 +00001114Expr *sqlite3ExprDup(sqlite3 *db, Expr *p, int flags){
drh72ea29d2015-12-08 16:58:45 +00001115 assert( flags==0 || flags==EXPRDUP_REDUCE );
drh3c194692016-04-11 16:43:43 +00001116 return p ? exprDup(db, p, flags, 0) : 0;
drhff78bd22002-02-27 01:47:11 +00001117}
danielk19776ab3a2e2009-02-19 14:39:25 +00001118ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p, int flags){
drhff78bd22002-02-27 01:47:11 +00001119 ExprList *pNew;
drh145716b2004-09-24 12:24:06 +00001120 struct ExprList_item *pItem, *pOldItem;
drhff78bd22002-02-27 01:47:11 +00001121 int i;
drh575fad62016-02-05 13:38:36 +00001122 assert( db!=0 );
drhff78bd22002-02-27 01:47:11 +00001123 if( p==0 ) return 0;
drh575fad62016-02-05 13:38:36 +00001124 pNew = sqlite3DbMallocRawNN(db, sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +00001125 if( pNew==0 ) return 0;
drhd872bb12012-02-02 01:58:08 +00001126 pNew->nExpr = i = p->nExpr;
1127 if( (flags & EXPRDUP_REDUCE)==0 ) for(i=1; i<p->nExpr; i+=i){}
drh575fad62016-02-05 13:38:36 +00001128 pNew->a = pItem = sqlite3DbMallocRawNN(db, i*sizeof(p->a[0]) );
danielk1977e0048402004-06-15 16:51:01 +00001129 if( pItem==0 ){
drh633e6d52008-07-28 19:34:53 +00001130 sqlite3DbFree(db, pNew);
danielk1977e0048402004-06-15 16:51:01 +00001131 return 0;
1132 }
drh145716b2004-09-24 12:24:06 +00001133 pOldItem = p->a;
1134 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
danielk19776ab3a2e2009-02-19 14:39:25 +00001135 Expr *pOldExpr = pOldItem->pExpr;
drhb5526ea2009-07-16 12:41:05 +00001136 pItem->pExpr = sqlite3ExprDup(db, pOldExpr, flags);
drh17435752007-08-16 04:30:38 +00001137 pItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
drhb7916a72009-05-27 10:31:29 +00001138 pItem->zSpan = sqlite3DbStrDup(db, pOldItem->zSpan);
drh145716b2004-09-24 12:24:06 +00001139 pItem->sortOrder = pOldItem->sortOrder;
drh3e7bc9c2004-02-21 19:17:17 +00001140 pItem->done = 0;
drh2c036cf2013-06-26 00:34:13 +00001141 pItem->bSpanIsTab = pOldItem->bSpanIsTab;
drhc2acc4e2013-11-15 18:15:19 +00001142 pItem->u = pOldItem->u;
drhff78bd22002-02-27 01:47:11 +00001143 }
1144 return pNew;
1145}
danielk197793758c82005-01-21 08:13:14 +00001146
1147/*
1148** If cursors, triggers, views and subqueries are all omitted from
1149** the build, then none of the following routines, except for
1150** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes
1151** called with a NULL argument.
1152*/
danielk19776a67fe82005-02-04 04:07:16 +00001153#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \
1154 || !defined(SQLITE_OMIT_SUBQUERY)
danielk19776ab3a2e2009-02-19 14:39:25 +00001155SrcList *sqlite3SrcListDup(sqlite3 *db, SrcList *p, int flags){
drhad3cab52002-05-24 02:04:32 +00001156 SrcList *pNew;
1157 int i;
drh113088e2003-03-20 01:16:58 +00001158 int nByte;
drh575fad62016-02-05 13:38:36 +00001159 assert( db!=0 );
drhad3cab52002-05-24 02:04:32 +00001160 if( p==0 ) return 0;
drh113088e2003-03-20 01:16:58 +00001161 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
drh575fad62016-02-05 13:38:36 +00001162 pNew = sqlite3DbMallocRawNN(db, nByte );
drhad3cab52002-05-24 02:04:32 +00001163 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +00001164 pNew->nSrc = pNew->nAlloc = p->nSrc;
drhad3cab52002-05-24 02:04:32 +00001165 for(i=0; i<p->nSrc; i++){
drh4efc4752004-01-16 15:55:37 +00001166 struct SrcList_item *pNewItem = &pNew->a[i];
1167 struct SrcList_item *pOldItem = &p->a[i];
drhed8a3bb2005-06-06 21:19:56 +00001168 Table *pTab;
dan41fb5cd2012-10-04 19:33:00 +00001169 pNewItem->pSchema = pOldItem->pSchema;
drh17435752007-08-16 04:30:38 +00001170 pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase);
1171 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
1172 pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias);
drh8a48b9c2015-08-19 15:20:00 +00001173 pNewItem->fg = pOldItem->fg;
drh4efc4752004-01-16 15:55:37 +00001174 pNewItem->iCursor = pOldItem->iCursor;
drh5b6a9ed2011-09-15 23:58:14 +00001175 pNewItem->addrFillSub = pOldItem->addrFillSub;
1176 pNewItem->regReturn = pOldItem->regReturn;
drh8a48b9c2015-08-19 15:20:00 +00001177 if( pNewItem->fg.isIndexedBy ){
1178 pNewItem->u1.zIndexedBy = sqlite3DbStrDup(db, pOldItem->u1.zIndexedBy);
1179 }
1180 pNewItem->pIBIndex = pOldItem->pIBIndex;
1181 if( pNewItem->fg.isTabFunc ){
1182 pNewItem->u1.pFuncArg =
1183 sqlite3ExprListDup(db, pOldItem->u1.pFuncArg, flags);
1184 }
drhed8a3bb2005-06-06 21:19:56 +00001185 pTab = pNewItem->pTab = pOldItem->pTab;
1186 if( pTab ){
1187 pTab->nRef++;
danielk1977a1cb1832005-02-12 08:59:55 +00001188 }
danielk19776ab3a2e2009-02-19 14:39:25 +00001189 pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect, flags);
1190 pNewItem->pOn = sqlite3ExprDup(db, pOldItem->pOn, flags);
drh17435752007-08-16 04:30:38 +00001191 pNewItem->pUsing = sqlite3IdListDup(db, pOldItem->pUsing);
danielk19776c18b6e2005-01-30 09:17:58 +00001192 pNewItem->colUsed = pOldItem->colUsed;
drhad3cab52002-05-24 02:04:32 +00001193 }
1194 return pNew;
1195}
drh17435752007-08-16 04:30:38 +00001196IdList *sqlite3IdListDup(sqlite3 *db, IdList *p){
drhff78bd22002-02-27 01:47:11 +00001197 IdList *pNew;
1198 int i;
drh575fad62016-02-05 13:38:36 +00001199 assert( db!=0 );
drhff78bd22002-02-27 01:47:11 +00001200 if( p==0 ) return 0;
drh575fad62016-02-05 13:38:36 +00001201 pNew = sqlite3DbMallocRawNN(db, sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +00001202 if( pNew==0 ) return 0;
drh6c535152012-02-02 03:38:30 +00001203 pNew->nId = p->nId;
drh575fad62016-02-05 13:38:36 +00001204 pNew->a = sqlite3DbMallocRawNN(db, p->nId*sizeof(p->a[0]) );
danielk1977d5d56522005-03-16 12:15:20 +00001205 if( pNew->a==0 ){
drh633e6d52008-07-28 19:34:53 +00001206 sqlite3DbFree(db, pNew);
danielk1977d5d56522005-03-16 12:15:20 +00001207 return 0;
1208 }
drh6c535152012-02-02 03:38:30 +00001209 /* Note that because the size of the allocation for p->a[] is not
1210 ** necessarily a power of two, sqlite3IdListAppend() may not be called
1211 ** on the duplicate created by this function. */
drhff78bd22002-02-27 01:47:11 +00001212 for(i=0; i<p->nId; i++){
drh4efc4752004-01-16 15:55:37 +00001213 struct IdList_item *pNewItem = &pNew->a[i];
1214 struct IdList_item *pOldItem = &p->a[i];
drh17435752007-08-16 04:30:38 +00001215 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
drh4efc4752004-01-16 15:55:37 +00001216 pNewItem->idx = pOldItem->idx;
drhff78bd22002-02-27 01:47:11 +00001217 }
1218 return pNew;
1219}
danielk19776ab3a2e2009-02-19 14:39:25 +00001220Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){
drh23b1b372011-12-07 01:55:51 +00001221 Select *pNew, *pPrior;
drh575fad62016-02-05 13:38:36 +00001222 assert( db!=0 );
drhff78bd22002-02-27 01:47:11 +00001223 if( p==0 ) return 0;
drh575fad62016-02-05 13:38:36 +00001224 pNew = sqlite3DbMallocRawNN(db, sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +00001225 if( pNew==0 ) return 0;
drhb7916a72009-05-27 10:31:29 +00001226 pNew->pEList = sqlite3ExprListDup(db, p->pEList, flags);
danielk19776ab3a2e2009-02-19 14:39:25 +00001227 pNew->pSrc = sqlite3SrcListDup(db, p->pSrc, flags);
1228 pNew->pWhere = sqlite3ExprDup(db, p->pWhere, flags);
1229 pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags);
1230 pNew->pHaving = sqlite3ExprDup(db, p->pHaving, flags);
1231 pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, flags);
drhff78bd22002-02-27 01:47:11 +00001232 pNew->op = p->op;
drh23b1b372011-12-07 01:55:51 +00001233 pNew->pPrior = pPrior = sqlite3SelectDup(db, p->pPrior, flags);
1234 if( pPrior ) pPrior->pNext = pNew;
1235 pNew->pNext = 0;
danielk19776ab3a2e2009-02-19 14:39:25 +00001236 pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags);
1237 pNew->pOffset = sqlite3ExprDup(db, p->pOffset, flags);
drh92b01d52008-06-24 00:32:35 +00001238 pNew->iLimit = 0;
1239 pNew->iOffset = 0;
drh7d10d5a2008-08-20 16:35:10 +00001240 pNew->selFlags = p->selFlags & ~SF_UsesEphemeral;
drhb9bb7c12006-06-11 23:41:55 +00001241 pNew->addrOpenEphm[0] = -1;
1242 pNew->addrOpenEphm[1] = -1;
drhec2da852014-01-29 01:46:12 +00001243 pNew->nSelectRow = p->nSelectRow;
dan4e9119d2014-01-13 15:12:23 +00001244 pNew->pWith = withDup(db, p->pWith);
drheb9b8842014-09-21 00:27:26 +00001245 sqlite3SelectSetName(pNew, p->zSelName);
drhff78bd22002-02-27 01:47:11 +00001246 return pNew;
1247}
danielk197793758c82005-01-21 08:13:14 +00001248#else
danielk19776ab3a2e2009-02-19 14:39:25 +00001249Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){
danielk197793758c82005-01-21 08:13:14 +00001250 assert( p==0 );
1251 return 0;
1252}
1253#endif
drhff78bd22002-02-27 01:47:11 +00001254
1255
1256/*
drha76b5df2002-02-23 02:32:10 +00001257** Add a new element to the end of an expression list. If pList is
1258** initially NULL, then create a new expression list.
drhb7916a72009-05-27 10:31:29 +00001259**
1260** If a memory allocation error occurs, the entire list is freed and
1261** NULL is returned. If non-NULL is returned, then it is guaranteed
1262** that the new entry was successfully appended.
drha76b5df2002-02-23 02:32:10 +00001263*/
drh17435752007-08-16 04:30:38 +00001264ExprList *sqlite3ExprListAppend(
1265 Parse *pParse, /* Parsing context */
1266 ExprList *pList, /* List to which to append. Might be NULL */
drhb7916a72009-05-27 10:31:29 +00001267 Expr *pExpr /* Expression to be appended. Might be NULL */
drh17435752007-08-16 04:30:38 +00001268){
1269 sqlite3 *db = pParse->db;
drh575fad62016-02-05 13:38:36 +00001270 assert( db!=0 );
drha76b5df2002-02-23 02:32:10 +00001271 if( pList==0 ){
drh575fad62016-02-05 13:38:36 +00001272 pList = sqlite3DbMallocRawNN(db, sizeof(ExprList) );
drha76b5df2002-02-23 02:32:10 +00001273 if( pList==0 ){
danielk1977d5d56522005-03-16 12:15:20 +00001274 goto no_mem;
drha76b5df2002-02-23 02:32:10 +00001275 }
drhc263f7c2016-01-18 13:18:54 +00001276 pList->nExpr = 0;
drh575fad62016-02-05 13:38:36 +00001277 pList->a = sqlite3DbMallocRawNN(db, sizeof(pList->a[0]));
drhd872bb12012-02-02 01:58:08 +00001278 if( pList->a==0 ) goto no_mem;
1279 }else if( (pList->nExpr & (pList->nExpr-1))==0 ){
danielk1977d5d56522005-03-16 12:15:20 +00001280 struct ExprList_item *a;
drhd872bb12012-02-02 01:58:08 +00001281 assert( pList->nExpr>0 );
1282 a = sqlite3DbRealloc(db, pList->a, pList->nExpr*2*sizeof(pList->a[0]));
danielk1977d5d56522005-03-16 12:15:20 +00001283 if( a==0 ){
1284 goto no_mem;
drha76b5df2002-02-23 02:32:10 +00001285 }
danielk1977d5d56522005-03-16 12:15:20 +00001286 pList->a = a;
drha76b5df2002-02-23 02:32:10 +00001287 }
drh4efc4752004-01-16 15:55:37 +00001288 assert( pList->a!=0 );
drhb7916a72009-05-27 10:31:29 +00001289 if( 1 ){
drh4efc4752004-01-16 15:55:37 +00001290 struct ExprList_item *pItem = &pList->a[pList->nExpr++];
1291 memset(pItem, 0, sizeof(*pItem));
danielk1977e94ddc92005-03-21 03:53:38 +00001292 pItem->pExpr = pExpr;
drha76b5df2002-02-23 02:32:10 +00001293 }
1294 return pList;
danielk1977d5d56522005-03-16 12:15:20 +00001295
1296no_mem:
1297 /* Avoid leaking memory if malloc has failed. */
drh633e6d52008-07-28 19:34:53 +00001298 sqlite3ExprDelete(db, pExpr);
1299 sqlite3ExprListDelete(db, pList);
danielk1977d5d56522005-03-16 12:15:20 +00001300 return 0;
drha76b5df2002-02-23 02:32:10 +00001301}
1302
1303/*
drhbc622bc2015-08-24 15:39:42 +00001304** Set the sort order for the last element on the given ExprList.
1305*/
1306void sqlite3ExprListSetSortOrder(ExprList *p, int iSortOrder){
1307 if( p==0 ) return;
1308 assert( SQLITE_SO_UNDEFINED<0 && SQLITE_SO_ASC>=0 && SQLITE_SO_DESC>0 );
1309 assert( p->nExpr>0 );
1310 if( iSortOrder<0 ){
1311 assert( p->a[p->nExpr-1].sortOrder==SQLITE_SO_ASC );
1312 return;
1313 }
1314 p->a[p->nExpr-1].sortOrder = (u8)iSortOrder;
drhbc622bc2015-08-24 15:39:42 +00001315}
1316
1317/*
drhb7916a72009-05-27 10:31:29 +00001318** Set the ExprList.a[].zName element of the most recently added item
1319** on the expression list.
1320**
1321** pList might be NULL following an OOM error. But pName should never be
1322** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag
1323** is set.
1324*/
1325void sqlite3ExprListSetName(
1326 Parse *pParse, /* Parsing context */
1327 ExprList *pList, /* List to which to add the span. */
1328 Token *pName, /* Name to be added */
1329 int dequote /* True to cause the name to be dequoted */
1330){
1331 assert( pList!=0 || pParse->db->mallocFailed!=0 );
1332 if( pList ){
1333 struct ExprList_item *pItem;
1334 assert( pList->nExpr>0 );
1335 pItem = &pList->a[pList->nExpr-1];
1336 assert( pItem->zName==0 );
1337 pItem->zName = sqlite3DbStrNDup(pParse->db, pName->z, pName->n);
drh244b9d62016-04-11 19:01:08 +00001338 if( dequote ) sqlite3Dequote(pItem->zName);
drhb7916a72009-05-27 10:31:29 +00001339 }
1340}
1341
1342/*
1343** Set the ExprList.a[].zSpan element of the most recently added item
1344** on the expression list.
1345**
1346** pList might be NULL following an OOM error. But pSpan should never be
1347** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag
1348** is set.
1349*/
1350void sqlite3ExprListSetSpan(
1351 Parse *pParse, /* Parsing context */
1352 ExprList *pList, /* List to which to add the span. */
1353 ExprSpan *pSpan /* The span to be added */
1354){
1355 sqlite3 *db = pParse->db;
1356 assert( pList!=0 || db->mallocFailed!=0 );
1357 if( pList ){
1358 struct ExprList_item *pItem = &pList->a[pList->nExpr-1];
1359 assert( pList->nExpr>0 );
1360 assert( db->mallocFailed || pItem->pExpr==pSpan->pExpr );
1361 sqlite3DbFree(db, pItem->zSpan);
1362 pItem->zSpan = sqlite3DbStrNDup(db, (char*)pSpan->zStart,
shanecf697392009-06-01 16:53:09 +00001363 (int)(pSpan->zEnd - pSpan->zStart));
drhb7916a72009-05-27 10:31:29 +00001364 }
1365}
1366
1367/*
danielk19777a15a4b2007-05-08 17:54:43 +00001368** If the expression list pEList contains more than iLimit elements,
1369** leave an error message in pParse.
1370*/
1371void sqlite3ExprListCheckLength(
1372 Parse *pParse,
1373 ExprList *pEList,
danielk19777a15a4b2007-05-08 17:54:43 +00001374 const char *zObject
1375){
drhb1a6c3c2008-03-20 16:30:17 +00001376 int mx = pParse->db->aLimit[SQLITE_LIMIT_COLUMN];
drhc5499be2008-04-01 15:06:33 +00001377 testcase( pEList && pEList->nExpr==mx );
1378 testcase( pEList && pEList->nExpr==mx+1 );
drhb1a6c3c2008-03-20 16:30:17 +00001379 if( pEList && pEList->nExpr>mx ){
danielk19777a15a4b2007-05-08 17:54:43 +00001380 sqlite3ErrorMsg(pParse, "too many columns in %s", zObject);
1381 }
1382}
1383
1384/*
drha76b5df2002-02-23 02:32:10 +00001385** Delete an entire expression list.
1386*/
drhaffa8552016-04-11 18:25:05 +00001387static SQLITE_NOINLINE void exprListDeleteNN(sqlite3 *db, ExprList *pList){
drha76b5df2002-02-23 02:32:10 +00001388 int i;
drhbe5c89a2004-07-26 00:31:09 +00001389 struct ExprList_item *pItem;
drhd872bb12012-02-02 01:58:08 +00001390 assert( pList->a!=0 || pList->nExpr==0 );
drhbe5c89a2004-07-26 00:31:09 +00001391 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
drh633e6d52008-07-28 19:34:53 +00001392 sqlite3ExprDelete(db, pItem->pExpr);
1393 sqlite3DbFree(db, pItem->zName);
drhb7916a72009-05-27 10:31:29 +00001394 sqlite3DbFree(db, pItem->zSpan);
drha76b5df2002-02-23 02:32:10 +00001395 }
drh633e6d52008-07-28 19:34:53 +00001396 sqlite3DbFree(db, pList->a);
1397 sqlite3DbFree(db, pList);
drha76b5df2002-02-23 02:32:10 +00001398}
drhaffa8552016-04-11 18:25:05 +00001399void sqlite3ExprListDelete(sqlite3 *db, ExprList *pList){
1400 if( pList ) exprListDeleteNN(db, pList);
1401}
drha76b5df2002-02-23 02:32:10 +00001402
1403/*
drh2308ed32015-02-09 16:09:34 +00001404** Return the bitwise-OR of all Expr.flags fields in the given
1405** ExprList.
drh885a5b02015-02-09 15:21:36 +00001406*/
drh2308ed32015-02-09 16:09:34 +00001407u32 sqlite3ExprListFlags(const ExprList *pList){
drh885a5b02015-02-09 15:21:36 +00001408 int i;
drh2308ed32015-02-09 16:09:34 +00001409 u32 m = 0;
1410 if( pList ){
1411 for(i=0; i<pList->nExpr; i++){
drhd0c73052015-04-19 19:21:19 +00001412 Expr *pExpr = pList->a[i].pExpr;
drhde845c22016-03-17 19:07:52 +00001413 assert( pExpr!=0 );
1414 m |= pExpr->flags;
drh2308ed32015-02-09 16:09:34 +00001415 }
drh885a5b02015-02-09 15:21:36 +00001416 }
drh2308ed32015-02-09 16:09:34 +00001417 return m;
drh885a5b02015-02-09 15:21:36 +00001418}
1419
1420/*
drh059b2d52014-10-24 19:28:09 +00001421** These routines are Walker callbacks used to check expressions to
1422** see if they are "constant" for some definition of constant. The
1423** Walker.eCode value determines the type of "constant" we are looking
1424** for.
drh73b211a2005-01-18 04:00:42 +00001425**
drh7d10d5a2008-08-20 16:35:10 +00001426** These callback routines are used to implement the following:
drh626a8792005-01-17 22:08:19 +00001427**
drh059b2d52014-10-24 19:28:09 +00001428** sqlite3ExprIsConstant() pWalker->eCode==1
1429** sqlite3ExprIsConstantNotJoin() pWalker->eCode==2
drhfcb9f4f2015-06-01 18:13:16 +00001430** sqlite3ExprIsTableConstant() pWalker->eCode==3
drh059b2d52014-10-24 19:28:09 +00001431** sqlite3ExprIsConstantOrFunction() pWalker->eCode==4 or 5
drh87abf5c2005-08-25 12:45:04 +00001432**
drh059b2d52014-10-24 19:28:09 +00001433** In all cases, the callbacks set Walker.eCode=0 and abort if the expression
1434** is found to not be a constant.
drh87abf5c2005-08-25 12:45:04 +00001435**
drhfeada2d2014-09-24 13:20:22 +00001436** The sqlite3ExprIsConstantOrFunction() is used for evaluating expressions
drh059b2d52014-10-24 19:28:09 +00001437** in a CREATE TABLE statement. The Walker.eCode value is 5 when parsing
1438** an existing schema and 4 when processing a new statement. A bound
drhfeada2d2014-09-24 13:20:22 +00001439** parameter raises an error for new statements, but is silently converted
1440** to NULL for existing schemas. This allows sqlite_master tables that
1441** contain a bound parameter because they were generated by older versions
1442** of SQLite to be parsed by newer versions of SQLite without raising a
1443** malformed schema error.
drh626a8792005-01-17 22:08:19 +00001444*/
drh7d10d5a2008-08-20 16:35:10 +00001445static int exprNodeIsConstant(Walker *pWalker, Expr *pExpr){
drh626a8792005-01-17 22:08:19 +00001446
drh059b2d52014-10-24 19:28:09 +00001447 /* If pWalker->eCode is 2 then any term of the expression that comes from
1448 ** the ON or USING clauses of a left join disqualifies the expression
drh0a168372007-06-08 00:20:47 +00001449 ** from being considered constant. */
drh059b2d52014-10-24 19:28:09 +00001450 if( pWalker->eCode==2 && ExprHasProperty(pExpr, EP_FromJoin) ){
1451 pWalker->eCode = 0;
drh7d10d5a2008-08-20 16:35:10 +00001452 return WRC_Abort;
drh0a168372007-06-08 00:20:47 +00001453 }
1454
drh626a8792005-01-17 22:08:19 +00001455 switch( pExpr->op ){
drheb55bd22005-06-30 17:04:21 +00001456 /* Consider functions to be constant if all their arguments are constant
drh059b2d52014-10-24 19:28:09 +00001457 ** and either pWalker->eCode==4 or 5 or the function has the
1458 ** SQLITE_FUNC_CONST flag. */
drheb55bd22005-06-30 17:04:21 +00001459 case TK_FUNCTION:
drh63f84572015-02-09 14:07:07 +00001460 if( pWalker->eCode>=4 || ExprHasProperty(pExpr,EP_ConstFunc) ){
drhb1fba282013-11-21 14:33:48 +00001461 return WRC_Continue;
drh059b2d52014-10-24 19:28:09 +00001462 }else{
1463 pWalker->eCode = 0;
1464 return WRC_Abort;
drhb1fba282013-11-21 14:33:48 +00001465 }
drh626a8792005-01-17 22:08:19 +00001466 case TK_ID:
1467 case TK_COLUMN:
drh626a8792005-01-17 22:08:19 +00001468 case TK_AGG_FUNCTION:
drh13449892005-09-07 21:22:45 +00001469 case TK_AGG_COLUMN:
drhc5499be2008-04-01 15:06:33 +00001470 testcase( pExpr->op==TK_ID );
1471 testcase( pExpr->op==TK_COLUMN );
drhc5499be2008-04-01 15:06:33 +00001472 testcase( pExpr->op==TK_AGG_FUNCTION );
1473 testcase( pExpr->op==TK_AGG_COLUMN );
drh059b2d52014-10-24 19:28:09 +00001474 if( pWalker->eCode==3 && pExpr->iTable==pWalker->u.iCur ){
1475 return WRC_Continue;
1476 }else{
1477 pWalker->eCode = 0;
1478 return WRC_Abort;
1479 }
drhfeada2d2014-09-24 13:20:22 +00001480 case TK_VARIABLE:
drh059b2d52014-10-24 19:28:09 +00001481 if( pWalker->eCode==5 ){
drhfeada2d2014-09-24 13:20:22 +00001482 /* Silently convert bound parameters that appear inside of CREATE
1483 ** statements into a NULL when parsing the CREATE statement text out
1484 ** of the sqlite_master table */
1485 pExpr->op = TK_NULL;
drh059b2d52014-10-24 19:28:09 +00001486 }else if( pWalker->eCode==4 ){
drhfeada2d2014-09-24 13:20:22 +00001487 /* A bound parameter in a CREATE statement that originates from
1488 ** sqlite3_prepare() causes an error */
drh059b2d52014-10-24 19:28:09 +00001489 pWalker->eCode = 0;
drhfeada2d2014-09-24 13:20:22 +00001490 return WRC_Abort;
1491 }
1492 /* Fall through */
drh626a8792005-01-17 22:08:19 +00001493 default:
drhb74b1012009-05-28 21:04:37 +00001494 testcase( pExpr->op==TK_SELECT ); /* selectNodeIsConstant will disallow */
1495 testcase( pExpr->op==TK_EXISTS ); /* selectNodeIsConstant will disallow */
drh7d10d5a2008-08-20 16:35:10 +00001496 return WRC_Continue;
drh626a8792005-01-17 22:08:19 +00001497 }
1498}
danielk197762c14b32008-11-19 09:05:26 +00001499static int selectNodeIsConstant(Walker *pWalker, Select *NotUsed){
1500 UNUSED_PARAMETER(NotUsed);
drh059b2d52014-10-24 19:28:09 +00001501 pWalker->eCode = 0;
drh7d10d5a2008-08-20 16:35:10 +00001502 return WRC_Abort;
1503}
drh059b2d52014-10-24 19:28:09 +00001504static int exprIsConst(Expr *p, int initFlag, int iCur){
drh7d10d5a2008-08-20 16:35:10 +00001505 Walker w;
drhaa87f9a2013-04-25 00:57:10 +00001506 memset(&w, 0, sizeof(w));
drh059b2d52014-10-24 19:28:09 +00001507 w.eCode = initFlag;
drh7d10d5a2008-08-20 16:35:10 +00001508 w.xExprCallback = exprNodeIsConstant;
1509 w.xSelectCallback = selectNodeIsConstant;
drh059b2d52014-10-24 19:28:09 +00001510 w.u.iCur = iCur;
drh7d10d5a2008-08-20 16:35:10 +00001511 sqlite3WalkExpr(&w, p);
drh059b2d52014-10-24 19:28:09 +00001512 return w.eCode;
drh7d10d5a2008-08-20 16:35:10 +00001513}
drh626a8792005-01-17 22:08:19 +00001514
1515/*
drh059b2d52014-10-24 19:28:09 +00001516** Walk an expression tree. Return non-zero if the expression is constant
drheb55bd22005-06-30 17:04:21 +00001517** and 0 if it involves variables or function calls.
drh23989372002-05-21 13:43:04 +00001518**
1519** For the purposes of this function, a double-quoted string (ex: "abc")
1520** is considered a variable but a single-quoted string (ex: 'abc') is
1521** a constant.
drhfef52082000-06-06 01:50:43 +00001522*/
danielk19774adee202004-05-08 08:23:19 +00001523int sqlite3ExprIsConstant(Expr *p){
drh059b2d52014-10-24 19:28:09 +00001524 return exprIsConst(p, 1, 0);
drhfef52082000-06-06 01:50:43 +00001525}
1526
1527/*
drh059b2d52014-10-24 19:28:09 +00001528** Walk an expression tree. Return non-zero if the expression is constant
drh0a168372007-06-08 00:20:47 +00001529** that does no originate from the ON or USING clauses of a join.
1530** Return 0 if it involves variables or function calls or terms from
1531** an ON or USING clause.
1532*/
1533int sqlite3ExprIsConstantNotJoin(Expr *p){
drh059b2d52014-10-24 19:28:09 +00001534 return exprIsConst(p, 2, 0);
drh0a168372007-06-08 00:20:47 +00001535}
1536
1537/*
drhfcb9f4f2015-06-01 18:13:16 +00001538** Walk an expression tree. Return non-zero if the expression is constant
drh059b2d52014-10-24 19:28:09 +00001539** for any single row of the table with cursor iCur. In other words, the
1540** expression must not refer to any non-deterministic function nor any
1541** table other than iCur.
1542*/
1543int sqlite3ExprIsTableConstant(Expr *p, int iCur){
1544 return exprIsConst(p, 3, iCur);
1545}
1546
1547/*
1548** Walk an expression tree. Return non-zero if the expression is constant
drheb55bd22005-06-30 17:04:21 +00001549** or a function call with constant arguments. Return and 0 if there
1550** are any variables.
1551**
1552** For the purposes of this function, a double-quoted string (ex: "abc")
1553** is considered a variable but a single-quoted string (ex: 'abc') is
1554** a constant.
1555*/
drhfeada2d2014-09-24 13:20:22 +00001556int sqlite3ExprIsConstantOrFunction(Expr *p, u8 isInit){
1557 assert( isInit==0 || isInit==1 );
drh059b2d52014-10-24 19:28:09 +00001558 return exprIsConst(p, 4+isInit, 0);
drheb55bd22005-06-30 17:04:21 +00001559}
1560
drh5b88bc42013-12-07 23:35:21 +00001561#ifdef SQLITE_ENABLE_CURSOR_HINTS
1562/*
1563** Walk an expression tree. Return 1 if the expression contains a
1564** subquery of some kind. Return 0 if there are no subqueries.
1565*/
1566int sqlite3ExprContainsSubquery(Expr *p){
1567 Walker w;
1568 memset(&w, 0, sizeof(w));
drhbec24762015-08-13 20:07:13 +00001569 w.eCode = 1;
drh5b88bc42013-12-07 23:35:21 +00001570 w.xExprCallback = sqlite3ExprWalkNoop;
1571 w.xSelectCallback = selectNodeIsConstant;
1572 sqlite3WalkExpr(&w, p);
drh07194bf2015-08-13 20:34:41 +00001573 return w.eCode==0;
drh5b88bc42013-12-07 23:35:21 +00001574}
1575#endif
1576
drheb55bd22005-06-30 17:04:21 +00001577/*
drh73b211a2005-01-18 04:00:42 +00001578** If the expression p codes a constant integer that is small enough
drh202b2df2004-01-06 01:13:46 +00001579** to fit in a 32-bit integer, return 1 and put the value of the integer
1580** in *pValue. If the expression is not an integer or if it is too big
1581** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
drhe4de1fe2002-06-02 16:09:01 +00001582*/
danielk19774adee202004-05-08 08:23:19 +00001583int sqlite3ExprIsInteger(Expr *p, int *pValue){
drh92b01d52008-06-24 00:32:35 +00001584 int rc = 0;
drhcd92e842011-02-17 15:58:20 +00001585
1586 /* If an expression is an integer literal that fits in a signed 32-bit
1587 ** integer, then the EP_IntValue flag will have already been set */
1588 assert( p->op!=TK_INTEGER || (p->flags & EP_IntValue)!=0
1589 || sqlite3GetInt32(p->u.zToken, &rc)==0 );
1590
drh92b01d52008-06-24 00:32:35 +00001591 if( p->flags & EP_IntValue ){
drh33e619f2009-05-28 01:00:55 +00001592 *pValue = p->u.iValue;
drh92b01d52008-06-24 00:32:35 +00001593 return 1;
1594 }
drhe4de1fe2002-06-02 16:09:01 +00001595 switch( p->op ){
drh4b59ab52002-08-24 18:24:51 +00001596 case TK_UPLUS: {
drh92b01d52008-06-24 00:32:35 +00001597 rc = sqlite3ExprIsInteger(p->pLeft, pValue);
drhf6e369a2008-06-24 12:46:30 +00001598 break;
drh4b59ab52002-08-24 18:24:51 +00001599 }
drhe4de1fe2002-06-02 16:09:01 +00001600 case TK_UMINUS: {
1601 int v;
danielk19774adee202004-05-08 08:23:19 +00001602 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
mistachkinf6418892013-08-28 01:54:12 +00001603 assert( v!=(-2147483647-1) );
drhe4de1fe2002-06-02 16:09:01 +00001604 *pValue = -v;
drh92b01d52008-06-24 00:32:35 +00001605 rc = 1;
drhe4de1fe2002-06-02 16:09:01 +00001606 }
1607 break;
1608 }
1609 default: break;
1610 }
drh92b01d52008-06-24 00:32:35 +00001611 return rc;
drhe4de1fe2002-06-02 16:09:01 +00001612}
1613
1614/*
drh039fc322009-11-17 18:31:47 +00001615** Return FALSE if there is no chance that the expression can be NULL.
1616**
1617** If the expression might be NULL or if the expression is too complex
1618** to tell return TRUE.
1619**
1620** This routine is used as an optimization, to skip OP_IsNull opcodes
1621** when we know that a value cannot be NULL. Hence, a false positive
1622** (returning TRUE when in fact the expression can never be NULL) might
1623** be a small performance hit but is otherwise harmless. On the other
1624** hand, a false negative (returning FALSE when the result could be NULL)
1625** will likely result in an incorrect answer. So when in doubt, return
1626** TRUE.
1627*/
1628int sqlite3ExprCanBeNull(const Expr *p){
1629 u8 op;
drhcd7f4572009-11-19 14:48:40 +00001630 while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ p = p->pLeft; }
drh039fc322009-11-17 18:31:47 +00001631 op = p->op;
1632 if( op==TK_REGISTER ) op = p->op2;
1633 switch( op ){
1634 case TK_INTEGER:
1635 case TK_STRING:
1636 case TK_FLOAT:
1637 case TK_BLOB:
1638 return 0;
drh7248a8b2014-08-04 18:50:54 +00001639 case TK_COLUMN:
1640 assert( p->pTab!=0 );
drh72673a22014-12-04 16:27:17 +00001641 return ExprHasProperty(p, EP_CanBeNull) ||
1642 (p->iColumn>=0 && p->pTab->aCol[p->iColumn].notNull==0);
drh039fc322009-11-17 18:31:47 +00001643 default:
1644 return 1;
1645 }
1646}
1647
1648/*
1649** Return TRUE if the given expression is a constant which would be
1650** unchanged by OP_Affinity with the affinity given in the second
1651** argument.
1652**
1653** This routine is used to determine if the OP_Affinity operation
1654** can be omitted. When in doubt return FALSE. A false negative
1655** is harmless. A false positive, however, can result in the wrong
1656** answer.
1657*/
1658int sqlite3ExprNeedsNoAffinityChange(const Expr *p, char aff){
1659 u8 op;
drh05883a32015-06-02 15:32:08 +00001660 if( aff==SQLITE_AFF_BLOB ) return 1;
drhcd7f4572009-11-19 14:48:40 +00001661 while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ p = p->pLeft; }
drh039fc322009-11-17 18:31:47 +00001662 op = p->op;
1663 if( op==TK_REGISTER ) op = p->op2;
1664 switch( op ){
1665 case TK_INTEGER: {
1666 return aff==SQLITE_AFF_INTEGER || aff==SQLITE_AFF_NUMERIC;
1667 }
1668 case TK_FLOAT: {
1669 return aff==SQLITE_AFF_REAL || aff==SQLITE_AFF_NUMERIC;
1670 }
1671 case TK_STRING: {
1672 return aff==SQLITE_AFF_TEXT;
1673 }
1674 case TK_BLOB: {
1675 return 1;
1676 }
drh2f2855b2009-11-18 01:25:26 +00001677 case TK_COLUMN: {
drh88376ca2009-11-19 15:44:53 +00001678 assert( p->iTable>=0 ); /* p cannot be part of a CHECK constraint */
1679 return p->iColumn<0
1680 && (aff==SQLITE_AFF_INTEGER || aff==SQLITE_AFF_NUMERIC);
drh2f2855b2009-11-18 01:25:26 +00001681 }
drh039fc322009-11-17 18:31:47 +00001682 default: {
1683 return 0;
1684 }
1685 }
1686}
1687
1688/*
drhc4a3c772001-04-04 11:48:57 +00001689** Return TRUE if the given string is a row-id column name.
1690*/
danielk19774adee202004-05-08 08:23:19 +00001691int sqlite3IsRowid(const char *z){
1692 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
1693 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
1694 if( sqlite3StrICmp(z, "OID")==0 ) return 1;
drhc4a3c772001-04-04 11:48:57 +00001695 return 0;
1696}
1697
danielk19779a96b662007-11-29 17:05:18 +00001698/*
drh69c355b2016-03-09 15:34:51 +00001699** pX is the RHS of an IN operator. If pX is a SELECT statement
1700** that can be simplified to a direct table access, then return
1701** a pointer to the SELECT statement. If pX is not a SELECT statement,
1702** or if the SELECT statement needs to be manifested into a transient
1703** table, then return NULL.
drhb287f4b2008-04-25 00:08:38 +00001704*/
1705#ifndef SQLITE_OMIT_SUBQUERY
drh69c355b2016-03-09 15:34:51 +00001706static Select *isCandidateForInOpt(Expr *pX){
1707 Select *p;
drhb287f4b2008-04-25 00:08:38 +00001708 SrcList *pSrc;
1709 ExprList *pEList;
drh69c355b2016-03-09 15:34:51 +00001710 Expr *pRes;
drhb287f4b2008-04-25 00:08:38 +00001711 Table *pTab;
drh69c355b2016-03-09 15:34:51 +00001712 if( !ExprHasProperty(pX, EP_xIsSelect) ) return 0; /* Not a subquery */
1713 if( ExprHasProperty(pX, EP_VarSelect) ) return 0; /* Correlated subq */
1714 p = pX->x.pSelect;
drhb287f4b2008-04-25 00:08:38 +00001715 if( p->pPrior ) return 0; /* Not a compound SELECT */
drh7d10d5a2008-08-20 16:35:10 +00001716 if( p->selFlags & (SF_Distinct|SF_Aggregate) ){
drhb74b1012009-05-28 21:04:37 +00001717 testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
1718 testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
1719 return 0; /* No DISTINCT keyword and no aggregate functions */
drh7d10d5a2008-08-20 16:35:10 +00001720 }
drhb74b1012009-05-28 21:04:37 +00001721 assert( p->pGroupBy==0 ); /* Has no GROUP BY clause */
drhb287f4b2008-04-25 00:08:38 +00001722 if( p->pLimit ) return 0; /* Has no LIMIT clause */
drhb74b1012009-05-28 21:04:37 +00001723 assert( p->pOffset==0 ); /* No LIMIT means no OFFSET */
drhb287f4b2008-04-25 00:08:38 +00001724 if( p->pWhere ) return 0; /* Has no WHERE clause */
1725 pSrc = p->pSrc;
drhd1fa7bc2009-01-10 13:24:50 +00001726 assert( pSrc!=0 );
1727 if( pSrc->nSrc!=1 ) return 0; /* Single term in FROM clause */
drhb74b1012009-05-28 21:04:37 +00001728 if( pSrc->a[0].pSelect ) return 0; /* FROM is not a subquery or view */
drhb287f4b2008-04-25 00:08:38 +00001729 pTab = pSrc->a[0].pTab;
drh69c355b2016-03-09 15:34:51 +00001730 assert( pTab!=0 );
drhb74b1012009-05-28 21:04:37 +00001731 assert( pTab->pSelect==0 ); /* FROM clause is not a view */
drhb287f4b2008-04-25 00:08:38 +00001732 if( IsVirtual(pTab) ) return 0; /* FROM clause not a virtual table */
1733 pEList = p->pEList;
1734 if( pEList->nExpr!=1 ) return 0; /* One column in the result set */
drh90730c92016-03-09 15:09:22 +00001735 pRes = pEList->a[0].pExpr;
1736 if( pRes->op!=TK_COLUMN ) return 0; /* Result is a column */
drh69c355b2016-03-09 15:34:51 +00001737 assert( pRes->iTable==pSrc->a[0].iCursor ); /* Not a correlated subquery */
1738 return p;
drhb287f4b2008-04-25 00:08:38 +00001739}
1740#endif /* SQLITE_OMIT_SUBQUERY */
1741
1742/*
dan1d8cb212011-12-09 13:24:16 +00001743** Code an OP_Once instruction and allocate space for its flag. Return the
1744** address of the new instruction.
1745*/
1746int sqlite3CodeOnce(Parse *pParse){
1747 Vdbe *v = sqlite3GetVdbe(pParse); /* Virtual machine being coded */
1748 return sqlite3VdbeAddOp1(v, OP_Once, pParse->nOnce++);
1749}
1750
1751/*
drh4c259e92014-08-01 21:12:35 +00001752** Generate code that checks the left-most column of index table iCur to see if
1753** it contains any NULL entries. Cause the register at regHasNull to be set
drh6be515e2014-08-01 21:00:53 +00001754** to a non-NULL value if iCur contains no NULLs. Cause register regHasNull
1755** to be set to NULL if iCur contains one or more NULL values.
1756*/
1757static void sqlite3SetHasNullFlag(Vdbe *v, int iCur, int regHasNull){
drh728e0f92015-10-10 14:41:28 +00001758 int addr1;
drh6be515e2014-08-01 21:00:53 +00001759 sqlite3VdbeAddOp2(v, OP_Integer, 0, regHasNull);
drh728e0f92015-10-10 14:41:28 +00001760 addr1 = sqlite3VdbeAddOp1(v, OP_Rewind, iCur); VdbeCoverage(v);
drh6be515e2014-08-01 21:00:53 +00001761 sqlite3VdbeAddOp3(v, OP_Column, iCur, 0, regHasNull);
1762 sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG);
drh4c259e92014-08-01 21:12:35 +00001763 VdbeComment((v, "first_entry_in(%d)", iCur));
drh728e0f92015-10-10 14:41:28 +00001764 sqlite3VdbeJumpHere(v, addr1);
drh6be515e2014-08-01 21:00:53 +00001765}
1766
drhbb53ecb2014-08-02 21:03:33 +00001767
1768#ifndef SQLITE_OMIT_SUBQUERY
1769/*
1770** The argument is an IN operator with a list (not a subquery) on the
1771** right-hand side. Return TRUE if that list is constant.
1772*/
1773static int sqlite3InRhsIsConstant(Expr *pIn){
1774 Expr *pLHS;
1775 int res;
1776 assert( !ExprHasProperty(pIn, EP_xIsSelect) );
1777 pLHS = pIn->pLeft;
1778 pIn->pLeft = 0;
1779 res = sqlite3ExprIsConstant(pIn);
1780 pIn->pLeft = pLHS;
1781 return res;
1782}
1783#endif
1784
drh6be515e2014-08-01 21:00:53 +00001785/*
danielk19779a96b662007-11-29 17:05:18 +00001786** This function is used by the implementation of the IN (...) operator.
drhd4305ca2012-09-18 17:08:33 +00001787** The pX parameter is the expression on the RHS of the IN operator, which
1788** might be either a list of expressions or a subquery.
danielk19779a96b662007-11-29 17:05:18 +00001789**
drhd4305ca2012-09-18 17:08:33 +00001790** The job of this routine is to find or create a b-tree object that can
1791** be used either to test for membership in the RHS set or to iterate through
1792** all members of the RHS set, skipping duplicates.
1793**
drh3a856252014-08-01 14:46:57 +00001794** A cursor is opened on the b-tree object that is the RHS of the IN operator
drhd4305ca2012-09-18 17:08:33 +00001795** and pX->iTable is set to the index of that cursor.
1796**
drhb74b1012009-05-28 21:04:37 +00001797** The returned value of this function indicates the b-tree type, as follows:
danielk19779a96b662007-11-29 17:05:18 +00001798**
drh1ccce442013-03-12 20:38:51 +00001799** IN_INDEX_ROWID - The cursor was opened on a database table.
1800** IN_INDEX_INDEX_ASC - The cursor was opened on an ascending index.
1801** IN_INDEX_INDEX_DESC - The cursor was opened on a descending index.
1802** IN_INDEX_EPH - The cursor was opened on a specially created and
1803** populated epheremal table.
drhbb53ecb2014-08-02 21:03:33 +00001804** IN_INDEX_NOOP - No cursor was allocated. The IN operator must be
1805** implemented as a sequence of comparisons.
danielk19779a96b662007-11-29 17:05:18 +00001806**
drhd4305ca2012-09-18 17:08:33 +00001807** An existing b-tree might be used if the RHS expression pX is a simple
1808** subquery such as:
danielk19779a96b662007-11-29 17:05:18 +00001809**
1810** SELECT <column> FROM <table>
1811**
drhd4305ca2012-09-18 17:08:33 +00001812** If the RHS of the IN operator is a list or a more complex subquery, then
1813** an ephemeral table might need to be generated from the RHS and then
peter.d.reid60ec9142014-09-06 16:39:46 +00001814** pX->iTable made to point to the ephemeral table instead of an
drh3a856252014-08-01 14:46:57 +00001815** existing table.
drhd4305ca2012-09-18 17:08:33 +00001816**
drh3a856252014-08-01 14:46:57 +00001817** The inFlags parameter must contain exactly one of the bits
1818** IN_INDEX_MEMBERSHIP or IN_INDEX_LOOP. If inFlags contains
1819** IN_INDEX_MEMBERSHIP, then the generated table will be used for a
1820** fast membership test. When the IN_INDEX_LOOP bit is set, the
1821** IN index will be used to loop over all values of the RHS of the
1822** IN operator.
1823**
1824** When IN_INDEX_LOOP is used (and the b-tree will be used to iterate
1825** through the set members) then the b-tree must not contain duplicates.
1826** An epheremal table must be used unless the selected <column> is guaranteed
danielk19779a96b662007-11-29 17:05:18 +00001827** to be unique - either because it is an INTEGER PRIMARY KEY or it
drhb74b1012009-05-28 21:04:37 +00001828** has a UNIQUE constraint or UNIQUE index.
danielk19770cdc0222008-06-26 18:04:03 +00001829**
drh3a856252014-08-01 14:46:57 +00001830** When IN_INDEX_MEMBERSHIP is used (and the b-tree will be used
1831** for fast set membership tests) then an epheremal table must
danielk19770cdc0222008-06-26 18:04:03 +00001832** be used unless <column> is an INTEGER PRIMARY KEY or an index can
1833** be found with <column> as its left-most column.
1834**
drhbb53ecb2014-08-02 21:03:33 +00001835** If the IN_INDEX_NOOP_OK and IN_INDEX_MEMBERSHIP are both set and
1836** if the RHS of the IN operator is a list (not a subquery) then this
1837** routine might decide that creating an ephemeral b-tree for membership
1838** testing is too expensive and return IN_INDEX_NOOP. In that case, the
1839** calling routine should implement the IN operator using a sequence
1840** of Eq or Ne comparison operations.
1841**
drhb74b1012009-05-28 21:04:37 +00001842** When the b-tree is being used for membership tests, the calling function
drh3a856252014-08-01 14:46:57 +00001843** might need to know whether or not the RHS side of the IN operator
drhe21a6e12014-08-01 18:00:24 +00001844** contains a NULL. If prRhsHasNull is not a NULL pointer and
drh3a856252014-08-01 14:46:57 +00001845** if there is any chance that the (...) might contain a NULL value at
danielk19770cdc0222008-06-26 18:04:03 +00001846** runtime, then a register is allocated and the register number written
drhe21a6e12014-08-01 18:00:24 +00001847** to *prRhsHasNull. If there is no chance that the (...) contains a
1848** NULL value, then *prRhsHasNull is left unchanged.
danielk19770cdc0222008-06-26 18:04:03 +00001849**
drhe21a6e12014-08-01 18:00:24 +00001850** If a register is allocated and its location stored in *prRhsHasNull, then
drh6be515e2014-08-01 21:00:53 +00001851** the value in that register will be NULL if the b-tree contains one or more
1852** NULL values, and it will be some non-NULL value if the b-tree contains no
1853** NULL values.
danielk19779a96b662007-11-29 17:05:18 +00001854*/
danielk1977284f4ac2007-12-10 05:03:46 +00001855#ifndef SQLITE_OMIT_SUBQUERY
drhe21a6e12014-08-01 18:00:24 +00001856int sqlite3FindInIndex(Parse *pParse, Expr *pX, u32 inFlags, int *prRhsHasNull){
drhb74b1012009-05-28 21:04:37 +00001857 Select *p; /* SELECT to the right of IN operator */
1858 int eType = 0; /* Type of RHS table. IN_INDEX_* */
1859 int iTab = pParse->nTab++; /* Cursor of the RHS table */
drh3a856252014-08-01 14:46:57 +00001860 int mustBeUnique; /* True if RHS must be unique */
drhb8475df2011-12-09 16:21:19 +00001861 Vdbe *v = sqlite3GetVdbe(pParse); /* Virtual machine being coded */
danielk19779a96b662007-11-29 17:05:18 +00001862
drh1450bc62009-10-30 13:25:56 +00001863 assert( pX->op==TK_IN );
drh3a856252014-08-01 14:46:57 +00001864 mustBeUnique = (inFlags & IN_INDEX_LOOP)!=0;
drh1450bc62009-10-30 13:25:56 +00001865
drhb74b1012009-05-28 21:04:37 +00001866 /* Check to see if an existing table or index can be used to
1867 ** satisfy the query. This is preferable to generating a new
1868 ** ephemeral table.
danielk19779a96b662007-11-29 17:05:18 +00001869 */
drh69c355b2016-03-09 15:34:51 +00001870 if( pParse->nErr==0 && (p = isCandidateForInOpt(pX))!=0 ){
danielk1977e1fb65a2009-04-02 17:23:32 +00001871 sqlite3 *db = pParse->db; /* Database connection */
drhb07028f2011-10-14 21:49:18 +00001872 Table *pTab; /* Table <table>. */
1873 Expr *pExpr; /* Expression <column> */
drhbbbdc832013-10-22 18:01:40 +00001874 i16 iCol; /* Index of column <column> */
1875 i16 iDb; /* Database idx for pTab */
drhb07028f2011-10-14 21:49:18 +00001876
drhb07028f2011-10-14 21:49:18 +00001877 assert( p->pEList!=0 ); /* Because of isCandidateForInOpt(p) */
1878 assert( p->pEList->a[0].pExpr!=0 ); /* Because of isCandidateForInOpt(p) */
1879 assert( p->pSrc!=0 ); /* Because of isCandidateForInOpt(p) */
1880 pTab = p->pSrc->a[0].pTab;
1881 pExpr = p->pEList->a[0].pExpr;
drhbbbdc832013-10-22 18:01:40 +00001882 iCol = (i16)pExpr->iColumn;
danielk1977e1fb65a2009-04-02 17:23:32 +00001883
drhb22f7c82014-02-06 23:56:27 +00001884 /* Code an OP_Transaction and OP_TableLock for <table>. */
danielk1977e1fb65a2009-04-02 17:23:32 +00001885 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
1886 sqlite3CodeVerifySchema(pParse, iDb);
1887 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
danielk19779a96b662007-11-29 17:05:18 +00001888
1889 /* This function is only called from two places. In both cases the vdbe
1890 ** has already been allocated. So assume sqlite3GetVdbe() is always
1891 ** successful here.
1892 */
1893 assert(v);
1894 if( iCol<0 ){
drh7d176102014-02-18 03:07:12 +00001895 int iAddr = sqlite3CodeOnce(pParse);
1896 VdbeCoverage(v);
danielk19779a96b662007-11-29 17:05:18 +00001897
1898 sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
1899 eType = IN_INDEX_ROWID;
1900
1901 sqlite3VdbeJumpHere(v, iAddr);
1902 }else{
danielk1977e1fb65a2009-04-02 17:23:32 +00001903 Index *pIdx; /* Iterator variable */
1904
drhb74b1012009-05-28 21:04:37 +00001905 /* The collation sequence used by the comparison. If an index is to
danielk19779a96b662007-11-29 17:05:18 +00001906 ** be used in place of a temp-table, it must be ordered according
danielk1977e1fb65a2009-04-02 17:23:32 +00001907 ** to this collation sequence. */
danielk19779a96b662007-11-29 17:05:18 +00001908 CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pExpr);
1909
1910 /* Check that the affinity that will be used to perform the
1911 ** comparison is the same as the affinity of the column. If
1912 ** it is not, it is not possible to use any index.
1913 */
drhdbaee5e2012-09-18 19:29:06 +00001914 int affinity_ok = sqlite3IndexAffinityOk(pX, pTab->aCol[iCol].affinity);
danielk19779a96b662007-11-29 17:05:18 +00001915
1916 for(pIdx=pTab->pIndex; pIdx && eType==0 && affinity_ok; pIdx=pIdx->pNext){
1917 if( (pIdx->aiColumn[0]==iCol)
drhb74b1012009-05-28 21:04:37 +00001918 && sqlite3FindCollSeq(db, ENC(db), pIdx->azColl[0], 0)==pReq
drh5f1d1d92014-07-31 22:59:04 +00001919 && (!mustBeUnique || (pIdx->nKeyCol==1 && IsUniqueIndex(pIdx)))
danielk19779a96b662007-11-29 17:05:18 +00001920 ){
drh7d176102014-02-18 03:07:12 +00001921 int iAddr = sqlite3CodeOnce(pParse); VdbeCoverage(v);
drh2ec2fb22013-11-06 19:59:23 +00001922 sqlite3VdbeAddOp3(v, OP_OpenRead, iTab, pIdx->tnum, iDb);
1923 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
danielk1977207872a2008-01-03 07:54:23 +00001924 VdbeComment((v, "%s", pIdx->zName));
drh1ccce442013-03-12 20:38:51 +00001925 assert( IN_INDEX_INDEX_DESC == IN_INDEX_INDEX_ASC+1 );
1926 eType = IN_INDEX_INDEX_ASC + pIdx->aSortOrder[0];
danielk19779a96b662007-11-29 17:05:18 +00001927
drhe21a6e12014-08-01 18:00:24 +00001928 if( prRhsHasNull && !pTab->aCol[iCol].notNull ){
dan3480bfd2016-06-16 17:14:02 +00001929#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
1930 const i64 sOne = 1;
1931 sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed,
1932 iTab, 0, 0, (u8*)&sOne, P4_INT64);
1933#endif
drhe21a6e12014-08-01 18:00:24 +00001934 *prRhsHasNull = ++pParse->nMem;
drh6be515e2014-08-01 21:00:53 +00001935 sqlite3SetHasNullFlag(v, iTab, *prRhsHasNull);
danielk19770cdc0222008-06-26 18:04:03 +00001936 }
drh552fd452014-02-18 01:07:38 +00001937 sqlite3VdbeJumpHere(v, iAddr);
danielk19779a96b662007-11-29 17:05:18 +00001938 }
1939 }
1940 }
1941 }
1942
drhbb53ecb2014-08-02 21:03:33 +00001943 /* If no preexisting index is available for the IN clause
1944 ** and IN_INDEX_NOOP is an allowed reply
1945 ** and the RHS of the IN operator is a list, not a subquery
dan71c57db2016-07-09 20:23:55 +00001946 ** and the RHS is not constant or has two or fewer terms,
peter.d.reid60ec9142014-09-06 16:39:46 +00001947 ** then it is not worth creating an ephemeral table to evaluate
drhbb53ecb2014-08-02 21:03:33 +00001948 ** the IN operator so return IN_INDEX_NOOP.
1949 */
1950 if( eType==0
1951 && (inFlags & IN_INDEX_NOOP_OK)
1952 && !ExprHasProperty(pX, EP_xIsSelect)
1953 && (!sqlite3InRhsIsConstant(pX) || pX->x.pList->nExpr<=2)
1954 ){
1955 eType = IN_INDEX_NOOP;
1956 }
1957
1958
danielk19779a96b662007-11-29 17:05:18 +00001959 if( eType==0 ){
drh43870062014-07-31 15:44:44 +00001960 /* Could not find an existing table or index to use as the RHS b-tree.
drhb74b1012009-05-28 21:04:37 +00001961 ** We will have to generate an ephemeral table to do the job.
1962 */
drh8e23daf2013-06-11 13:30:04 +00001963 u32 savedNQueryLoop = pParse->nQueryLoop;
danielk19770cdc0222008-06-26 18:04:03 +00001964 int rMayHaveNull = 0;
danielk197741a05b72008-10-02 13:50:55 +00001965 eType = IN_INDEX_EPH;
drh3a856252014-08-01 14:46:57 +00001966 if( inFlags & IN_INDEX_LOOP ){
drh4a5acf82013-06-18 20:06:23 +00001967 pParse->nQueryLoop = 0;
drhc5cd1242013-09-12 16:50:49 +00001968 if( pX->pLeft->iColumn<0 && !ExprHasProperty(pX, EP_xIsSelect) ){
drhcf4d38a2010-07-28 02:53:36 +00001969 eType = IN_INDEX_ROWID;
1970 }
drhe21a6e12014-08-01 18:00:24 +00001971 }else if( prRhsHasNull ){
1972 *prRhsHasNull = rMayHaveNull = ++pParse->nMem;
danielk19770cdc0222008-06-26 18:04:03 +00001973 }
danielk197741a05b72008-10-02 13:50:55 +00001974 sqlite3CodeSubselect(pParse, pX, rMayHaveNull, eType==IN_INDEX_ROWID);
drhcf4d38a2010-07-28 02:53:36 +00001975 pParse->nQueryLoop = savedNQueryLoop;
danielk19779a96b662007-11-29 17:05:18 +00001976 }else{
1977 pX->iTable = iTab;
1978 }
1979 return eType;
1980}
danielk1977284f4ac2007-12-10 05:03:46 +00001981#endif
drh626a8792005-01-17 22:08:19 +00001982
dan71c57db2016-07-09 20:23:55 +00001983static char *exprINAffinity(Parse *pParse, Expr *pExpr){
1984 Expr *pLeft = pExpr->pLeft;
1985 int nVal = sqlite3ExprVectorSize(pLeft);
1986 char *zRet;
1987
1988 zRet = sqlite3DbMallocZero(pParse->db, nVal+1);
1989 if( zRet ){
1990 int i;
1991 for(i=0; i<nVal; i++){
1992 Expr *pA;
1993 char a;
1994 if( nVal==1 ){
1995 pA = pLeft;
1996 }else{
1997 pA = exprVectorField(pLeft, i);
1998 }
1999 a = sqlite3ExprAffinity(pA);
2000 zRet[i] = sqlite3CompareAffinity(pExpr->x.pSelect->pEList->a[i].pExpr, a);
2001 }
2002 zRet[nVal] = '\0';
2003 }
2004 return zRet;
2005}
2006
drh626a8792005-01-17 22:08:19 +00002007/*
drhd4187c72010-08-30 22:15:45 +00002008** Generate code for scalar subqueries used as a subquery expression, EXISTS,
2009** or IN operators. Examples:
drh626a8792005-01-17 22:08:19 +00002010**
drh9cbe6352005-11-29 03:13:21 +00002011** (SELECT a FROM b) -- subquery
2012** EXISTS (SELECT a FROM b) -- EXISTS subquery
2013** x IN (4,5,11) -- IN operator with list on right-hand side
2014** x IN (SELECT a FROM b) -- IN operator with subquery on the right
drhfef52082000-06-06 01:50:43 +00002015**
drh9cbe6352005-11-29 03:13:21 +00002016** The pExpr parameter describes the expression that contains the IN
2017** operator or subquery.
danielk197741a05b72008-10-02 13:50:55 +00002018**
2019** If parameter isRowid is non-zero, then expression pExpr is guaranteed
2020** to be of the form "<rowid> IN (?, ?, ?)", where <rowid> is a reference
2021** to some integer key column of a table B-Tree. In this case, use an
2022** intkey B-Tree to store the set of IN(...) values instead of the usual
2023** (slower) variable length keys B-Tree.
drhfd773cf2009-05-29 14:39:07 +00002024**
2025** If rMayHaveNull is non-zero, that means that the operation is an IN
2026** (not a SELECT or EXISTS) and that the RHS might contains NULLs.
drh3a856252014-08-01 14:46:57 +00002027** All this routine does is initialize the register given by rMayHaveNull
2028** to NULL. Calling routines will take care of changing this register
2029** value to non-NULL if the RHS is NULL-free.
drh1450bc62009-10-30 13:25:56 +00002030**
2031** For a SELECT or EXISTS operator, return the register that holds the
2032** result. For IN operators or if an error occurs, the return value is 0.
drhcce7d172000-05-31 15:34:51 +00002033*/
drh51522cd2005-01-20 13:36:19 +00002034#ifndef SQLITE_OMIT_SUBQUERY
drh1450bc62009-10-30 13:25:56 +00002035int sqlite3CodeSubselect(
drhfd773cf2009-05-29 14:39:07 +00002036 Parse *pParse, /* Parsing context */
2037 Expr *pExpr, /* The IN, SELECT, or EXISTS operator */
drh6be515e2014-08-01 21:00:53 +00002038 int rHasNullFlag, /* Register that records whether NULLs exist in RHS */
drhfd773cf2009-05-29 14:39:07 +00002039 int isRowid /* If true, LHS of IN operator is a rowid */
danielk197741a05b72008-10-02 13:50:55 +00002040){
drh6be515e2014-08-01 21:00:53 +00002041 int jmpIfDynamic = -1; /* One-time test address */
drh1450bc62009-10-30 13:25:56 +00002042 int rReg = 0; /* Register storing resulting */
danielk1977b3bce662005-01-29 08:32:43 +00002043 Vdbe *v = sqlite3GetVdbe(pParse);
drh1450bc62009-10-30 13:25:56 +00002044 if( NEVER(v==0) ) return 0;
drhceea3322009-04-23 13:22:42 +00002045 sqlite3ExprCachePush(pParse);
danielk1977fc976062007-05-10 10:46:56 +00002046
drh57dbd7b2005-07-08 18:25:26 +00002047 /* This code must be run in its entirety every time it is encountered
2048 ** if any of the following is true:
2049 **
2050 ** * The right-hand side is a correlated subquery
2051 ** * The right-hand side is an expression list containing variables
2052 ** * We are inside a trigger
2053 **
2054 ** If all of the above are false, then we can run this code just once
2055 ** save the results, and reuse the same result on subsequent invocations.
danielk1977b3bce662005-01-29 08:32:43 +00002056 */
drhc5cd1242013-09-12 16:50:49 +00002057 if( !ExprHasProperty(pExpr, EP_VarSelect) ){
drh6be515e2014-08-01 21:00:53 +00002058 jmpIfDynamic = sqlite3CodeOnce(pParse); VdbeCoverage(v);
danielk1977b3bce662005-01-29 08:32:43 +00002059 }
2060
dan4a07e3d2010-11-09 14:48:59 +00002061#ifndef SQLITE_OMIT_EXPLAIN
2062 if( pParse->explain==2 ){
drh62aaa6c2015-11-21 17:27:42 +00002063 char *zMsg = sqlite3MPrintf(pParse->db, "EXECUTE %s%s SUBQUERY %d",
2064 jmpIfDynamic>=0?"":"CORRELATED ",
2065 pExpr->op==TK_IN?"LIST":"SCALAR",
2066 pParse->iNextSelectId
dan4a07e3d2010-11-09 14:48:59 +00002067 );
2068 sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC);
2069 }
2070#endif
2071
drhcce7d172000-05-31 15:34:51 +00002072 switch( pExpr->op ){
drhfef52082000-06-06 01:50:43 +00002073 case TK_IN: {
drhd4187c72010-08-30 22:15:45 +00002074 int addr; /* Address of OP_OpenEphemeral instruction */
2075 Expr *pLeft = pExpr->pLeft; /* the LHS of the IN operator */
drh323df792013-08-05 19:11:29 +00002076 KeyInfo *pKeyInfo = 0; /* Key information */
dan71c57db2016-07-09 20:23:55 +00002077 int nVal; /* Size of vector pLeft */
2078
2079 nVal = sqlite3ExprVectorSize(pLeft);
danielk1977e014a832004-05-17 10:48:57 +00002080
2081 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
drh8cff69d2009-11-12 19:59:44 +00002082 ** expression it is handled the same way. An ephemeral table is
danielk1977e014a832004-05-17 10:48:57 +00002083 ** filled with single-field index keys representing the results
2084 ** from the SELECT or the <exprlist>.
2085 **
2086 ** If the 'x' expression is a column value, or the SELECT...
2087 ** statement returns a column value, then the affinity of that
2088 ** column is used to build the index keys. If both 'x' and the
2089 ** SELECT... statement are columns, then numeric affinity is used
2090 ** if either column has NUMERIC or INTEGER affinity. If neither
2091 ** 'x' nor the SELECT... statement are columns, then numeric affinity
2092 ** is used.
2093 */
2094 pExpr->iTable = pParse->nTab++;
dan71c57db2016-07-09 20:23:55 +00002095 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral,
2096 pExpr->iTable, (isRowid?0:nVal));
2097 pKeyInfo = isRowid ? 0 : sqlite3KeyInfoAlloc(pParse->db, nVal, 1);
danielk1977e014a832004-05-17 10:48:57 +00002098
danielk19776ab3a2e2009-02-19 14:39:25 +00002099 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
drhfef52082000-06-06 01:50:43 +00002100 /* Case 1: expr IN (SELECT ...)
2101 **
danielk1977e014a832004-05-17 10:48:57 +00002102 ** Generate code to write the results of the select into the temporary
2103 ** table allocated and opened above.
drhfef52082000-06-06 01:50:43 +00002104 */
drh43870062014-07-31 15:44:44 +00002105 Select *pSelect = pExpr->x.pSelect;
dan71c57db2016-07-09 20:23:55 +00002106 ExprList *pEList = pSelect->pEList;
drh1013c932008-01-06 00:25:21 +00002107
danielk197741a05b72008-10-02 13:50:55 +00002108 assert( !isRowid );
dan71c57db2016-07-09 20:23:55 +00002109 if( pEList->nExpr!=nVal ){
2110 sqlite3ErrorMsg(pParse, "SELECT has %d columns - expected %d",
2111 pEList->nExpr, nVal);
2112 }else{
2113 SelectDest dest;
2114 int i;
2115 sqlite3SelectDestInit(&dest, SRT_Set, pExpr->iTable);
2116 dest.zAffSdst = exprINAffinity(pParse, pExpr);
2117 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
2118 pSelect->iLimit = 0;
2119 testcase( pSelect->selFlags & SF_Distinct );
2120 testcase( pKeyInfo==0 ); /* Caused by OOM in sqlite3KeyInfoAlloc() */
2121 if( sqlite3Select(pParse, pSelect, &dest) ){
2122 sqlite3DbFree(pParse->db, dest.zAffSdst);
2123 sqlite3KeyInfoUnref(pKeyInfo);
2124 return 0;
2125 }
2126 sqlite3DbFree(pParse->db, dest.zAffSdst);
2127 assert( pKeyInfo!=0 ); /* OOM will cause exit after sqlite3Select() */
2128 assert( pEList!=0 );
2129 assert( pEList->nExpr>0 );
2130 assert( sqlite3KeyInfoIsWriteable(pKeyInfo) );
2131 for(i=0; i<nVal; i++){
2132 Expr *p = (nVal>1) ? exprVectorField(pLeft, i) : pLeft;
2133 pKeyInfo->aColl[i] = sqlite3BinaryCompareCollSeq(
2134 pParse, p, pEList->a[i].pExpr
2135 );
2136 }
drh94ccde52007-04-13 16:06:32 +00002137 }
drha7d2db12010-07-14 20:23:52 +00002138 }else if( ALWAYS(pExpr->x.pList!=0) ){
drhfef52082000-06-06 01:50:43 +00002139 /* Case 2: expr IN (exprlist)
2140 **
drhfd131da2007-08-07 17:13:03 +00002141 ** For each expression, build an index key from the evaluation and
danielk1977e014a832004-05-17 10:48:57 +00002142 ** store it in the temporary table. If <expr> is a column, then use
2143 ** that columns affinity when building index keys. If <expr> is not
2144 ** a column, use numeric affinity.
drhfef52082000-06-06 01:50:43 +00002145 */
dan71c57db2016-07-09 20:23:55 +00002146 char affinity; /* Affinity of the LHS of the IN */
danielk1977e014a832004-05-17 10:48:57 +00002147 int i;
danielk19776ab3a2e2009-02-19 14:39:25 +00002148 ExprList *pList = pExpr->x.pList;
drh57dbd7b2005-07-08 18:25:26 +00002149 struct ExprList_item *pItem;
drhecc31802008-06-26 20:06:06 +00002150 int r1, r2, r3;
drh57dbd7b2005-07-08 18:25:26 +00002151
dan71c57db2016-07-09 20:23:55 +00002152 affinity = sqlite3ExprAffinity(pLeft);
danielk1977e014a832004-05-17 10:48:57 +00002153 if( !affinity ){
drh05883a32015-06-02 15:32:08 +00002154 affinity = SQLITE_AFF_BLOB;
danielk1977e014a832004-05-17 10:48:57 +00002155 }
drh323df792013-08-05 19:11:29 +00002156 if( pKeyInfo ){
drh2ec2fb22013-11-06 19:59:23 +00002157 assert( sqlite3KeyInfoIsWriteable(pKeyInfo) );
drh323df792013-08-05 19:11:29 +00002158 pKeyInfo->aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
2159 }
danielk1977e014a832004-05-17 10:48:57 +00002160
2161 /* Loop through each expression in <exprlist>. */
drh2d401ab2008-01-10 23:50:11 +00002162 r1 = sqlite3GetTempReg(pParse);
2163 r2 = sqlite3GetTempReg(pParse);
drh37e08082014-07-31 20:16:08 +00002164 if( isRowid ) sqlite3VdbeAddOp2(v, OP_Null, 0, r2);
drh57dbd7b2005-07-08 18:25:26 +00002165 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
2166 Expr *pE2 = pItem->pExpr;
drhe05c9292009-10-29 13:48:10 +00002167 int iValToIns;
danielk1977e014a832004-05-17 10:48:57 +00002168
drh57dbd7b2005-07-08 18:25:26 +00002169 /* If the expression is not constant then we will need to
2170 ** disable the test that was generated above that makes sure
2171 ** this code only executes once. Because for a non-constant
2172 ** expression we need to rerun this code each time.
2173 */
drh6be515e2014-08-01 21:00:53 +00002174 if( jmpIfDynamic>=0 && !sqlite3ExprIsConstant(pE2) ){
2175 sqlite3VdbeChangeToNoop(v, jmpIfDynamic);
2176 jmpIfDynamic = -1;
drh4794b982000-06-06 13:54:14 +00002177 }
danielk1977e014a832004-05-17 10:48:57 +00002178
2179 /* Evaluate the expression and insert it into the temp table */
drhe05c9292009-10-29 13:48:10 +00002180 if( isRowid && sqlite3ExprIsInteger(pE2, &iValToIns) ){
2181 sqlite3VdbeAddOp3(v, OP_InsertInt, pExpr->iTable, r2, iValToIns);
danielk197741a05b72008-10-02 13:50:55 +00002182 }else{
drhe05c9292009-10-29 13:48:10 +00002183 r3 = sqlite3ExprCodeTarget(pParse, pE2, r1);
2184 if( isRowid ){
2185 sqlite3VdbeAddOp2(v, OP_MustBeInt, r3,
2186 sqlite3VdbeCurrentAddr(v)+2);
drh688852a2014-02-17 22:40:43 +00002187 VdbeCoverage(v);
drhe05c9292009-10-29 13:48:10 +00002188 sqlite3VdbeAddOp3(v, OP_Insert, pExpr->iTable, r2, r3);
2189 }else{
2190 sqlite3VdbeAddOp4(v, OP_MakeRecord, r3, 1, r2, &affinity, 1);
2191 sqlite3ExprCacheAffinityChange(pParse, r3, 1);
2192 sqlite3VdbeAddOp2(v, OP_IdxInsert, pExpr->iTable, r2);
2193 }
danielk197741a05b72008-10-02 13:50:55 +00002194 }
drhfef52082000-06-06 01:50:43 +00002195 }
drh2d401ab2008-01-10 23:50:11 +00002196 sqlite3ReleaseTempReg(pParse, r1);
2197 sqlite3ReleaseTempReg(pParse, r2);
drhfef52082000-06-06 01:50:43 +00002198 }
drh323df792013-08-05 19:11:29 +00002199 if( pKeyInfo ){
drh2ec2fb22013-11-06 19:59:23 +00002200 sqlite3VdbeChangeP4(v, addr, (void *)pKeyInfo, P4_KEYINFO);
danielk197741a05b72008-10-02 13:50:55 +00002201 }
danielk1977b3bce662005-01-29 08:32:43 +00002202 break;
drhfef52082000-06-06 01:50:43 +00002203 }
2204
drh51522cd2005-01-20 13:36:19 +00002205 case TK_EXISTS:
drhfd773cf2009-05-29 14:39:07 +00002206 case TK_SELECT:
2207 default: {
drhfd773cf2009-05-29 14:39:07 +00002208 /* If this has to be a scalar SELECT. Generate code to put the
drhfef52082000-06-06 01:50:43 +00002209 ** value of this select in a memory cell and record the number
drhfd773cf2009-05-29 14:39:07 +00002210 ** of the memory cell in iColumn. If this is an EXISTS, write
2211 ** an integer 0 (not exists) or 1 (exists) into a memory cell
2212 ** and record that memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +00002213 */
drhfd773cf2009-05-29 14:39:07 +00002214 Select *pSel; /* SELECT statement to encode */
2215 SelectDest dest; /* How to deal with SELECt result */
dan71c57db2016-07-09 20:23:55 +00002216 int nReg; /* Registers to allocate */
drh1398ad32005-01-19 23:24:50 +00002217
shanecf697392009-06-01 16:53:09 +00002218 testcase( pExpr->op==TK_EXISTS );
2219 testcase( pExpr->op==TK_SELECT );
2220 assert( pExpr->op==TK_EXISTS || pExpr->op==TK_SELECT );
danielk19776ab3a2e2009-02-19 14:39:25 +00002221 assert( ExprHasProperty(pExpr, EP_xIsSelect) );
dan71c57db2016-07-09 20:23:55 +00002222
danielk19776ab3a2e2009-02-19 14:39:25 +00002223 pSel = pExpr->x.pSelect;
dan71c57db2016-07-09 20:23:55 +00002224 nReg = pExpr->op==TK_SELECT ? pSel->pEList->nExpr : 1;
2225 sqlite3SelectDestInit(&dest, 0, pParse->nMem+1);
2226 pParse->nMem += nReg;
drh51522cd2005-01-20 13:36:19 +00002227 if( pExpr->op==TK_SELECT ){
danielk19776c8c8ce2008-01-02 16:27:09 +00002228 dest.eDest = SRT_Mem;
drh53932ce2014-08-29 12:29:39 +00002229 dest.iSdst = dest.iSDParm;
dan71c57db2016-07-09 20:23:55 +00002230 dest.nSdst = nReg;
2231 sqlite3VdbeAddOp3(v, OP_Null, 0, dest.iSDParm, dest.iSDParm+nReg-1);
drhd4e70eb2008-01-02 00:34:36 +00002232 VdbeComment((v, "Init subquery result"));
drh51522cd2005-01-20 13:36:19 +00002233 }else{
danielk19776c8c8ce2008-01-02 16:27:09 +00002234 dest.eDest = SRT_Exists;
drh2b596da2012-07-23 21:43:19 +00002235 sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iSDParm);
drhd4e70eb2008-01-02 00:34:36 +00002236 VdbeComment((v, "Init EXISTS result"));
drh51522cd2005-01-20 13:36:19 +00002237 }
drh633e6d52008-07-28 19:34:53 +00002238 sqlite3ExprDelete(pParse->db, pSel->pLimit);
drh094430e2010-07-14 18:24:06 +00002239 pSel->pLimit = sqlite3PExpr(pParse, TK_INTEGER, 0, 0,
2240 &sqlite3IntTokens[1]);
drh48b5b042010-12-06 18:50:32 +00002241 pSel->iLimit = 0;
drh772460f2015-04-16 14:13:12 +00002242 pSel->selFlags &= ~SF_MultiValue;
drh7d10d5a2008-08-20 16:35:10 +00002243 if( sqlite3Select(pParse, pSel, &dest) ){
drh1450bc62009-10-30 13:25:56 +00002244 return 0;
drh94ccde52007-04-13 16:06:32 +00002245 }
drh2b596da2012-07-23 21:43:19 +00002246 rReg = dest.iSDParm;
drhebb6a652013-09-12 23:42:22 +00002247 ExprSetVVAProperty(pExpr, EP_NoReduce);
danielk1977b3bce662005-01-29 08:32:43 +00002248 break;
drhcce7d172000-05-31 15:34:51 +00002249 }
2250 }
danielk1977b3bce662005-01-29 08:32:43 +00002251
drh6be515e2014-08-01 21:00:53 +00002252 if( rHasNullFlag ){
2253 sqlite3SetHasNullFlag(v, pExpr->iTable, rHasNullFlag);
danielk1977b3bce662005-01-29 08:32:43 +00002254 }
drh6be515e2014-08-01 21:00:53 +00002255
2256 if( jmpIfDynamic>=0 ){
2257 sqlite3VdbeJumpHere(v, jmpIfDynamic);
danielk1977b3bce662005-01-29 08:32:43 +00002258 }
drhd2490902014-04-13 19:28:15 +00002259 sqlite3ExprCachePop(pParse);
danielk1977fc976062007-05-10 10:46:56 +00002260
drh1450bc62009-10-30 13:25:56 +00002261 return rReg;
drhcce7d172000-05-31 15:34:51 +00002262}
drh51522cd2005-01-20 13:36:19 +00002263#endif /* SQLITE_OMIT_SUBQUERY */
drhcce7d172000-05-31 15:34:51 +00002264
drhe3365e62009-11-12 17:52:24 +00002265#ifndef SQLITE_OMIT_SUBQUERY
dan71c57db2016-07-09 20:23:55 +00002266void exprCodeVectorIN(
2267 Parse *pParse, /* Parsing and code generating context */
2268 Expr *pExpr, /* The IN expression */
2269 int destIfFalse, /* Jump here if LHS is not contained in the RHS */
2270 int destIfNull /* Jump here if the results are unknown due to NULLs */
2271){
2272 int i;
2273 int addrNext;
2274 int iSkip;
2275 int r1;
2276 int r2 = sqlite3GetTempReg(pParse);
2277 int r3 = sqlite3GetTempReg(pParse);
2278 int r4 = sqlite3GetTempReg(pParse);
2279 int regResult = sqlite3GetTempReg(pParse);
2280 int nVal = sqlite3ExprVectorSize(pExpr->pLeft);
2281
2282 Expr *pLeft = pExpr->pLeft;
2283 Vdbe *v = pParse->pVdbe;
2284
2285 /* Code the LHS, the <expr> from "<expr> IN (...)". Leave the results in
2286 ** an array of nVal registers starting at r1. */
2287 sqlite3ExprCachePush(pParse);
2288 if( pLeft->flags & EP_xIsSelect ){
2289 r1 = sqlite3CodeSubselect(pParse, pLeft, 0, 0);
2290 }else{
2291 r1 = pParse->nMem + 1;
2292 pParse->nMem += nVal;
2293 sqlite3ExprCodeExprList(pParse, pLeft->x.pList, r1, 0, 0);
2294 }
2295
2296 /* Generate an epheremal index containing the contents of the SELECT
2297 ** to the right of the "<expr> IN (SELECT ...)" expression. The cursor
2298 ** number for the epheremal table is left in pExpr->iTable. */
2299 assert( pExpr->flags & EP_xIsSelect );
2300 sqlite3CodeSubselect(pParse, pExpr, 0, 0);
2301
2302 sqlite3VdbeAddOp2(v, OP_Integer, 0, regResult);
2303
2304 /* Iterate through the ephemeral table just populated */
2305 addrNext = 1 + sqlite3VdbeAddOp2(v, OP_Rewind, pExpr->iTable, destIfFalse);
2306 for(i=0; i<nVal; i++){
2307 Expr *p;
2308 CollSeq *pColl;
2309 p = exprVectorField(pLeft, i);
2310 pColl = sqlite3ExprCollSeq(pParse, p);
2311 sqlite3VdbeAddOp3(v, OP_Column, pExpr->iTable, i, r2);
2312 sqlite3VdbeAddOp4(v, OP_Eq, r1+i, i==0?r3:r4, r2, (void*)pColl,P4_COLLSEQ);
2313 sqlite3VdbeChangeP5(v, SQLITE_STOREP2);
2314 VdbeCoverage(v);
2315 if( i!=0 ){
2316 sqlite3VdbeAddOp3(v, OP_And, r3, r4, r4);
2317 }
2318 }
2319 sqlite3VdbeAddOp2(v, OP_If, r4, sqlite3VdbeCurrentAddr(v)+6);
2320 sqlite3VdbeAddOp2(v, OP_IfNot, r4, sqlite3VdbeCurrentAddr(v)+2);
2321 sqlite3VdbeAddOp2(v, OP_Null, 0, regResult);
2322 sqlite3VdbeAddOp2(v, OP_Next, pExpr->iTable, addrNext);
2323 sqlite3VdbeAddOp3(v, OP_If, regResult, destIfNull, 1);
2324 sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfFalse);
2325
2326 sqlite3ReleaseTempReg(pParse, r2);
2327 sqlite3ReleaseTempReg(pParse, r3);
2328 sqlite3ReleaseTempReg(pParse, r4);
2329 sqlite3ReleaseTempReg(pParse, regResult);
2330 sqlite3ExprCachePop(pParse);
2331}
2332#endif
2333
2334#ifndef SQLITE_OMIT_SUBQUERY
drhe3365e62009-11-12 17:52:24 +00002335/*
2336** Generate code for an IN expression.
2337**
2338** x IN (SELECT ...)
2339** x IN (value, value, ...)
2340**
2341** The left-hand side (LHS) is a scalar expression. The right-hand side (RHS)
2342** is an array of zero or more values. The expression is true if the LHS is
2343** contained within the RHS. The value of the expression is unknown (NULL)
2344** if the LHS is NULL or if the LHS is not contained within the RHS and the
2345** RHS contains one or more NULL values.
2346**
drh6be515e2014-08-01 21:00:53 +00002347** This routine generates code that jumps to destIfFalse if the LHS is not
drhe3365e62009-11-12 17:52:24 +00002348** contained within the RHS. If due to NULLs we cannot determine if the LHS
2349** is contained in the RHS then jump to destIfNull. If the LHS is contained
2350** within the RHS then fall through.
2351*/
2352static void sqlite3ExprCodeIN(
2353 Parse *pParse, /* Parsing and code generating context */
2354 Expr *pExpr, /* The IN expression */
2355 int destIfFalse, /* Jump here if LHS is not contained in the RHS */
2356 int destIfNull /* Jump here if the results are unknown due to NULLs */
2357){
2358 int rRhsHasNull = 0; /* Register that is true if RHS contains NULL values */
2359 char affinity; /* Comparison affinity to use */
2360 int eType; /* Type of the RHS */
2361 int r1; /* Temporary use register */
2362 Vdbe *v; /* Statement under construction */
2363
dan71c57db2016-07-09 20:23:55 +00002364 if( pExpr->pLeft->flags & EP_Vector ){
2365 return exprCodeVectorIN(pParse, pExpr, destIfFalse, destIfNull);
2366 }
2367
drhe3365e62009-11-12 17:52:24 +00002368 /* Compute the RHS. After this step, the table with cursor
2369 ** pExpr->iTable will contains the values that make up the RHS.
2370 */
2371 v = pParse->pVdbe;
2372 assert( v!=0 ); /* OOM detected prior to this routine */
2373 VdbeNoopComment((v, "begin IN expr"));
drhbb53ecb2014-08-02 21:03:33 +00002374 eType = sqlite3FindInIndex(pParse, pExpr,
2375 IN_INDEX_MEMBERSHIP | IN_INDEX_NOOP_OK,
drh3a856252014-08-01 14:46:57 +00002376 destIfFalse==destIfNull ? 0 : &rRhsHasNull);
drhe3365e62009-11-12 17:52:24 +00002377
2378 /* Figure out the affinity to use to create a key from the results
2379 ** of the expression. affinityStr stores a static string suitable for
2380 ** P4 of OP_MakeRecord.
2381 */
2382 affinity = comparisonAffinity(pExpr);
2383
2384 /* Code the LHS, the <expr> from "<expr> IN (...)".
2385 */
2386 sqlite3ExprCachePush(pParse);
2387 r1 = sqlite3GetTempReg(pParse);
2388 sqlite3ExprCode(pParse, pExpr->pLeft, r1);
drhe3365e62009-11-12 17:52:24 +00002389
drhbb53ecb2014-08-02 21:03:33 +00002390 /* If sqlite3FindInIndex() did not find or create an index that is
2391 ** suitable for evaluating the IN operator, then evaluate using a
2392 ** sequence of comparisons.
drh094430e2010-07-14 18:24:06 +00002393 */
drhbb53ecb2014-08-02 21:03:33 +00002394 if( eType==IN_INDEX_NOOP ){
2395 ExprList *pList = pExpr->x.pList;
2396 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
2397 int labelOk = sqlite3VdbeMakeLabel(v);
2398 int r2, regToFree;
2399 int regCkNull = 0;
2400 int ii;
2401 assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
drhbb53ecb2014-08-02 21:03:33 +00002402 if( destIfNull!=destIfFalse ){
2403 regCkNull = sqlite3GetTempReg(pParse);
drha9769792014-08-04 16:39:39 +00002404 sqlite3VdbeAddOp3(v, OP_BitAnd, r1, r1, regCkNull);
drhbb53ecb2014-08-02 21:03:33 +00002405 }
2406 for(ii=0; ii<pList->nExpr; ii++){
2407 r2 = sqlite3ExprCodeTemp(pParse, pList->a[ii].pExpr, &regToFree);
drha9769792014-08-04 16:39:39 +00002408 if( regCkNull && sqlite3ExprCanBeNull(pList->a[ii].pExpr) ){
drhbb53ecb2014-08-02 21:03:33 +00002409 sqlite3VdbeAddOp3(v, OP_BitAnd, regCkNull, r2, regCkNull);
2410 }
2411 if( ii<pList->nExpr-1 || destIfNull!=destIfFalse ){
2412 sqlite3VdbeAddOp4(v, OP_Eq, r1, labelOk, r2,
drh4336b0e2014-08-05 00:53:51 +00002413 (void*)pColl, P4_COLLSEQ);
2414 VdbeCoverageIf(v, ii<pList->nExpr-1);
2415 VdbeCoverageIf(v, ii==pList->nExpr-1);
drhbb53ecb2014-08-02 21:03:33 +00002416 sqlite3VdbeChangeP5(v, affinity);
2417 }else{
2418 assert( destIfNull==destIfFalse );
2419 sqlite3VdbeAddOp4(v, OP_Ne, r1, destIfFalse, r2,
2420 (void*)pColl, P4_COLLSEQ); VdbeCoverage(v);
2421 sqlite3VdbeChangeP5(v, affinity | SQLITE_JUMPIFNULL);
2422 }
2423 sqlite3ReleaseTempReg(pParse, regToFree);
2424 }
2425 if( regCkNull ){
2426 sqlite3VdbeAddOp2(v, OP_IsNull, regCkNull, destIfNull); VdbeCoverage(v);
drh076e85f2015-09-03 13:46:12 +00002427 sqlite3VdbeGoto(v, destIfFalse);
drhbb53ecb2014-08-02 21:03:33 +00002428 }
2429 sqlite3VdbeResolveLabel(v, labelOk);
2430 sqlite3ReleaseTempReg(pParse, regCkNull);
drh094430e2010-07-14 18:24:06 +00002431 }else{
drhbb53ecb2014-08-02 21:03:33 +00002432
2433 /* If the LHS is NULL, then the result is either false or NULL depending
2434 ** on whether the RHS is empty or not, respectively.
drhe3365e62009-11-12 17:52:24 +00002435 */
drh7248a8b2014-08-04 18:50:54 +00002436 if( sqlite3ExprCanBeNull(pExpr->pLeft) ){
2437 if( destIfNull==destIfFalse ){
2438 /* Shortcut for the common case where the false and NULL outcomes are
2439 ** the same. */
2440 sqlite3VdbeAddOp2(v, OP_IsNull, r1, destIfNull); VdbeCoverage(v);
2441 }else{
2442 int addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, r1); VdbeCoverage(v);
2443 sqlite3VdbeAddOp2(v, OP_Rewind, pExpr->iTable, destIfFalse);
2444 VdbeCoverage(v);
drh076e85f2015-09-03 13:46:12 +00002445 sqlite3VdbeGoto(v, destIfNull);
drh7248a8b2014-08-04 18:50:54 +00002446 sqlite3VdbeJumpHere(v, addr1);
2447 }
drhbb53ecb2014-08-02 21:03:33 +00002448 }
2449
2450 if( eType==IN_INDEX_ROWID ){
2451 /* In this case, the RHS is the ROWID of table b-tree
drhe3365e62009-11-12 17:52:24 +00002452 */
drheeb95652016-05-26 20:56:38 +00002453 sqlite3VdbeAddOp3(v, OP_SeekRowid, pExpr->iTable, destIfFalse, r1);
drh688852a2014-02-17 22:40:43 +00002454 VdbeCoverage(v);
drhe3365e62009-11-12 17:52:24 +00002455 }else{
drhbb53ecb2014-08-02 21:03:33 +00002456 /* In this case, the RHS is an index b-tree.
drhe3365e62009-11-12 17:52:24 +00002457 */
drhbb53ecb2014-08-02 21:03:33 +00002458 sqlite3VdbeAddOp4(v, OP_Affinity, r1, 1, 0, &affinity, 1);
2459
2460 /* If the set membership test fails, then the result of the
2461 ** "x IN (...)" expression must be either 0 or NULL. If the set
2462 ** contains no NULL values, then the result is 0. If the set
2463 ** contains one or more NULL values, then the result of the
2464 ** expression is also NULL.
drhe3365e62009-11-12 17:52:24 +00002465 */
drhbb53ecb2014-08-02 21:03:33 +00002466 assert( destIfFalse!=destIfNull || rRhsHasNull==0 );
2467 if( rRhsHasNull==0 ){
2468 /* This branch runs if it is known at compile time that the RHS
2469 ** cannot contain NULL values. This happens as the result
2470 ** of a "NOT NULL" constraint in the database schema.
2471 **
2472 ** Also run this branch if NULL is equivalent to FALSE
2473 ** for this particular IN operator.
2474 */
2475 sqlite3VdbeAddOp4Int(v, OP_NotFound, pExpr->iTable, destIfFalse, r1, 1);
2476 VdbeCoverage(v);
2477 }else{
2478 /* In this branch, the RHS of the IN might contain a NULL and
2479 ** the presence of a NULL on the RHS makes a difference in the
2480 ** outcome.
2481 */
drh728e0f92015-10-10 14:41:28 +00002482 int addr1;
drhbb53ecb2014-08-02 21:03:33 +00002483
2484 /* First check to see if the LHS is contained in the RHS. If so,
2485 ** then the answer is TRUE the presence of NULLs in the RHS does
2486 ** not matter. If the LHS is not contained in the RHS, then the
2487 ** answer is NULL if the RHS contains NULLs and the answer is
2488 ** FALSE if the RHS is NULL-free.
2489 */
drh728e0f92015-10-10 14:41:28 +00002490 addr1 = sqlite3VdbeAddOp4Int(v, OP_Found, pExpr->iTable, 0, r1, 1);
drhbb53ecb2014-08-02 21:03:33 +00002491 VdbeCoverage(v);
2492 sqlite3VdbeAddOp2(v, OP_IsNull, rRhsHasNull, destIfNull);
2493 VdbeCoverage(v);
drh076e85f2015-09-03 13:46:12 +00002494 sqlite3VdbeGoto(v, destIfFalse);
drh728e0f92015-10-10 14:41:28 +00002495 sqlite3VdbeJumpHere(v, addr1);
drhbb53ecb2014-08-02 21:03:33 +00002496 }
drhe3365e62009-11-12 17:52:24 +00002497 }
drhe3365e62009-11-12 17:52:24 +00002498 }
2499 sqlite3ReleaseTempReg(pParse, r1);
drhd2490902014-04-13 19:28:15 +00002500 sqlite3ExprCachePop(pParse);
drhe3365e62009-11-12 17:52:24 +00002501 VdbeComment((v, "end IN expr"));
2502}
2503#endif /* SQLITE_OMIT_SUBQUERY */
2504
drh13573c72010-01-12 17:04:07 +00002505#ifndef SQLITE_OMIT_FLOATING_POINT
drh598f1342007-10-23 15:39:45 +00002506/*
2507** Generate an instruction that will put the floating point
drh9cbf3422008-01-17 16:22:13 +00002508** value described by z[0..n-1] into register iMem.
drh0cf19ed2007-10-23 18:55:48 +00002509**
2510** The z[] string will probably not be zero-terminated. But the
2511** z[n] character is guaranteed to be something that does not look
2512** like the continuation of the number.
drh598f1342007-10-23 15:39:45 +00002513*/
drhb7916a72009-05-27 10:31:29 +00002514static void codeReal(Vdbe *v, const char *z, int negateFlag, int iMem){
drhfd773cf2009-05-29 14:39:07 +00002515 if( ALWAYS(z!=0) ){
drh598f1342007-10-23 15:39:45 +00002516 double value;
drh9339da12010-09-30 00:50:49 +00002517 sqlite3AtoF(z, &value, sqlite3Strlen30(z), SQLITE_UTF8);
drhd0015162009-08-21 13:22:25 +00002518 assert( !sqlite3IsNaN(value) ); /* The new AtoF never returns NaN */
2519 if( negateFlag ) value = -value;
drh97bae792015-06-05 15:59:57 +00002520 sqlite3VdbeAddOp4Dup8(v, OP_Real, 0, iMem, 0, (u8*)&value, P4_REAL);
drh598f1342007-10-23 15:39:45 +00002521 }
2522}
drh13573c72010-01-12 17:04:07 +00002523#endif
drh598f1342007-10-23 15:39:45 +00002524
2525
2526/*
drhfec19aa2004-05-19 20:41:03 +00002527** Generate an instruction that will put the integer describe by
drh9cbf3422008-01-17 16:22:13 +00002528** text z[0..n-1] into register iMem.
drh0cf19ed2007-10-23 18:55:48 +00002529**
shaneh5f1d6b62010-09-30 16:51:25 +00002530** Expr.u.zToken is always UTF8 and zero-terminated.
drhfec19aa2004-05-19 20:41:03 +00002531*/
drh13573c72010-01-12 17:04:07 +00002532static void codeInteger(Parse *pParse, Expr *pExpr, int negFlag, int iMem){
2533 Vdbe *v = pParse->pVdbe;
drh92b01d52008-06-24 00:32:35 +00002534 if( pExpr->flags & EP_IntValue ){
drh33e619f2009-05-28 01:00:55 +00002535 int i = pExpr->u.iValue;
drhd50ffc42011-03-08 02:38:28 +00002536 assert( i>=0 );
drh92b01d52008-06-24 00:32:35 +00002537 if( negFlag ) i = -i;
2538 sqlite3VdbeAddOp2(v, OP_Integer, i, iMem);
drhfd773cf2009-05-29 14:39:07 +00002539 }else{
shaneh5f1d6b62010-09-30 16:51:25 +00002540 int c;
2541 i64 value;
drhfd773cf2009-05-29 14:39:07 +00002542 const char *z = pExpr->u.zToken;
2543 assert( z!=0 );
drh9296c182014-07-23 13:40:49 +00002544 c = sqlite3DecOrHexToI64(z, &value);
shaneh5f1d6b62010-09-30 16:51:25 +00002545 if( c==0 || (c==2 && negFlag) ){
drh158b9cb2011-03-05 20:59:46 +00002546 if( negFlag ){ value = c==2 ? SMALLEST_INT64 : -value; }
drh97bae792015-06-05 15:59:57 +00002547 sqlite3VdbeAddOp4Dup8(v, OP_Int64, 0, iMem, 0, (u8*)&value, P4_INT64);
danielk1977c9cf9012007-05-30 10:36:47 +00002548 }else{
drh13573c72010-01-12 17:04:07 +00002549#ifdef SQLITE_OMIT_FLOATING_POINT
2550 sqlite3ErrorMsg(pParse, "oversized integer: %s%s", negFlag ? "-" : "", z);
2551#else
drh1b7ddc52014-07-23 14:52:05 +00002552#ifndef SQLITE_OMIT_HEX_INTEGER
drh9296c182014-07-23 13:40:49 +00002553 if( sqlite3_strnicmp(z,"0x",2)==0 ){
2554 sqlite3ErrorMsg(pParse, "hex literal too big: %s", z);
drh1b7ddc52014-07-23 14:52:05 +00002555 }else
2556#endif
2557 {
drh9296c182014-07-23 13:40:49 +00002558 codeReal(v, z, negFlag, iMem);
2559 }
drh13573c72010-01-12 17:04:07 +00002560#endif
danielk1977c9cf9012007-05-30 10:36:47 +00002561 }
drhfec19aa2004-05-19 20:41:03 +00002562 }
2563}
2564
drhbea119c2016-04-11 18:15:37 +00002565#if defined(SQLITE_DEBUG)
2566/*
2567** Verify the consistency of the column cache
2568*/
2569static int cacheIsValid(Parse *pParse){
2570 int i, n;
2571 for(i=n=0; i<SQLITE_N_COLCACHE; i++){
2572 if( pParse->aColCache[i].iReg>0 ) n++;
2573 }
2574 return n==pParse->nColCache;
2575}
2576#endif
2577
drhceea3322009-04-23 13:22:42 +00002578/*
2579** Clear a cache entry.
2580*/
2581static void cacheEntryClear(Parse *pParse, struct yColCache *p){
2582 if( p->tempReg ){
2583 if( pParse->nTempReg<ArraySize(pParse->aTempReg) ){
2584 pParse->aTempReg[pParse->nTempReg++] = p->iReg;
2585 }
2586 p->tempReg = 0;
2587 }
drhbea119c2016-04-11 18:15:37 +00002588 p->iReg = 0;
2589 pParse->nColCache--;
danee65eea2016-04-16 15:03:20 +00002590 assert( pParse->db->mallocFailed || cacheIsValid(pParse) );
drhceea3322009-04-23 13:22:42 +00002591}
2592
2593
2594/*
2595** Record in the column cache that a particular column from a
2596** particular table is stored in a particular register.
2597*/
2598void sqlite3ExprCacheStore(Parse *pParse, int iTab, int iCol, int iReg){
2599 int i;
2600 int minLru;
2601 int idxLru;
2602 struct yColCache *p;
2603
dance8f53d2015-01-21 17:00:57 +00002604 /* Unless an error has occurred, register numbers are always positive. */
2605 assert( iReg>0 || pParse->nErr || pParse->db->mallocFailed );
drh20411ea2009-05-29 19:00:12 +00002606 assert( iCol>=-1 && iCol<32768 ); /* Finite column numbers */
2607
drhb6da74e2009-12-24 16:00:28 +00002608 /* The SQLITE_ColumnCache flag disables the column cache. This is used
2609 ** for testing only - to verify that SQLite always gets the same answer
2610 ** with and without the column cache.
2611 */
drh7e5418e2012-09-27 15:05:54 +00002612 if( OptimizationDisabled(pParse->db, SQLITE_ColumnCache) ) return;
drhb6da74e2009-12-24 16:00:28 +00002613
drh27ee4062009-12-30 01:13:11 +00002614 /* First replace any existing entry.
2615 **
2616 ** Actually, the way the column cache is currently used, we are guaranteed
2617 ** that the object will never already be in cache. Verify this guarantee.
2618 */
2619#ifndef NDEBUG
drhceea3322009-04-23 13:22:42 +00002620 for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
drh27ee4062009-12-30 01:13:11 +00002621 assert( p->iReg==0 || p->iTable!=iTab || p->iColumn!=iCol );
drhceea3322009-04-23 13:22:42 +00002622 }
drh27ee4062009-12-30 01:13:11 +00002623#endif
drhceea3322009-04-23 13:22:42 +00002624
2625 /* Find an empty slot and replace it */
2626 for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
2627 if( p->iReg==0 ){
2628 p->iLevel = pParse->iCacheLevel;
2629 p->iTable = iTab;
2630 p->iColumn = iCol;
2631 p->iReg = iReg;
drhceea3322009-04-23 13:22:42 +00002632 p->tempReg = 0;
2633 p->lru = pParse->iCacheCnt++;
drhbea119c2016-04-11 18:15:37 +00002634 pParse->nColCache++;
danee65eea2016-04-16 15:03:20 +00002635 assert( pParse->db->mallocFailed || cacheIsValid(pParse) );
drhceea3322009-04-23 13:22:42 +00002636 return;
2637 }
2638 }
2639
2640 /* Replace the last recently used */
2641 minLru = 0x7fffffff;
2642 idxLru = -1;
2643 for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
2644 if( p->lru<minLru ){
2645 idxLru = i;
2646 minLru = p->lru;
2647 }
2648 }
drh20411ea2009-05-29 19:00:12 +00002649 if( ALWAYS(idxLru>=0) ){
drhceea3322009-04-23 13:22:42 +00002650 p = &pParse->aColCache[idxLru];
2651 p->iLevel = pParse->iCacheLevel;
2652 p->iTable = iTab;
2653 p->iColumn = iCol;
2654 p->iReg = iReg;
drhceea3322009-04-23 13:22:42 +00002655 p->tempReg = 0;
2656 p->lru = pParse->iCacheCnt++;
drhbea119c2016-04-11 18:15:37 +00002657 assert( cacheIsValid(pParse) );
drhceea3322009-04-23 13:22:42 +00002658 return;
2659 }
2660}
2661
2662/*
drhf49f3522009-12-30 14:12:38 +00002663** Indicate that registers between iReg..iReg+nReg-1 are being overwritten.
2664** Purge the range of registers from the column cache.
drhceea3322009-04-23 13:22:42 +00002665*/
drhf49f3522009-12-30 14:12:38 +00002666void sqlite3ExprCacheRemove(Parse *pParse, int iReg, int nReg){
drhceea3322009-04-23 13:22:42 +00002667 struct yColCache *p;
drhbea119c2016-04-11 18:15:37 +00002668 if( iReg<=0 || pParse->nColCache==0 ) return;
2669 p = &pParse->aColCache[SQLITE_N_COLCACHE-1];
2670 while(1){
2671 if( p->iReg >= iReg && p->iReg < iReg+nReg ) cacheEntryClear(pParse, p);
2672 if( p==pParse->aColCache ) break;
2673 p--;
drhceea3322009-04-23 13:22:42 +00002674 }
2675}
2676
2677/*
2678** Remember the current column cache context. Any new entries added
2679** added to the column cache after this call are removed when the
2680** corresponding pop occurs.
2681*/
2682void sqlite3ExprCachePush(Parse *pParse){
2683 pParse->iCacheLevel++;
drh9ac79622013-12-18 15:11:47 +00002684#ifdef SQLITE_DEBUG
2685 if( pParse->db->flags & SQLITE_VdbeAddopTrace ){
2686 printf("PUSH to %d\n", pParse->iCacheLevel);
2687 }
2688#endif
drhceea3322009-04-23 13:22:42 +00002689}
2690
2691/*
2692** Remove from the column cache any entries that were added since the
drhd2490902014-04-13 19:28:15 +00002693** the previous sqlite3ExprCachePush operation. In other words, restore
2694** the cache to the state it was in prior the most recent Push.
drhceea3322009-04-23 13:22:42 +00002695*/
drhd2490902014-04-13 19:28:15 +00002696void sqlite3ExprCachePop(Parse *pParse){
drhceea3322009-04-23 13:22:42 +00002697 int i;
2698 struct yColCache *p;
drhd2490902014-04-13 19:28:15 +00002699 assert( pParse->iCacheLevel>=1 );
2700 pParse->iCacheLevel--;
drh9ac79622013-12-18 15:11:47 +00002701#ifdef SQLITE_DEBUG
2702 if( pParse->db->flags & SQLITE_VdbeAddopTrace ){
2703 printf("POP to %d\n", pParse->iCacheLevel);
2704 }
2705#endif
drhceea3322009-04-23 13:22:42 +00002706 for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
2707 if( p->iReg && p->iLevel>pParse->iCacheLevel ){
2708 cacheEntryClear(pParse, p);
drhceea3322009-04-23 13:22:42 +00002709 }
2710 }
2711}
drh945498f2007-02-24 11:52:52 +00002712
2713/*
drh5cd79232009-05-25 11:46:29 +00002714** When a cached column is reused, make sure that its register is
2715** no longer available as a temp register. ticket #3879: that same
2716** register might be in the cache in multiple places, so be sure to
2717** get them all.
2718*/
2719static void sqlite3ExprCachePinRegister(Parse *pParse, int iReg){
2720 int i;
2721 struct yColCache *p;
2722 for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
2723 if( p->iReg==iReg ){
2724 p->tempReg = 0;
2725 }
2726 }
2727}
2728
drh1f9ca2c2015-08-25 16:57:52 +00002729/* Generate code that will load into register regOut a value that is
2730** appropriate for the iIdxCol-th column of index pIdx.
2731*/
2732void sqlite3ExprCodeLoadIndexColumn(
2733 Parse *pParse, /* The parsing context */
2734 Index *pIdx, /* The index whose column is to be loaded */
2735 int iTabCur, /* Cursor pointing to a table row */
2736 int iIdxCol, /* The column of the index to be loaded */
2737 int regOut /* Store the index column value in this register */
2738){
2739 i16 iTabCol = pIdx->aiColumn[iIdxCol];
drh4b92f982015-09-29 17:20:14 +00002740 if( iTabCol==XN_EXPR ){
2741 assert( pIdx->aColExpr );
2742 assert( pIdx->aColExpr->nExpr>iIdxCol );
2743 pParse->iSelfTab = iTabCur;
drh1c75c9d2015-12-21 15:22:13 +00002744 sqlite3ExprCodeCopy(pParse, pIdx->aColExpr->a[iIdxCol].pExpr, regOut);
drh4b92f982015-09-29 17:20:14 +00002745 }else{
drh1f9ca2c2015-08-25 16:57:52 +00002746 sqlite3ExprCodeGetColumnOfTable(pParse->pVdbe, pIdx->pTable, iTabCur,
2747 iTabCol, regOut);
drh1f9ca2c2015-08-25 16:57:52 +00002748 }
drh1f9ca2c2015-08-25 16:57:52 +00002749}
2750
drh5cd79232009-05-25 11:46:29 +00002751/*
drh5c092e82010-05-14 19:24:02 +00002752** Generate code to extract the value of the iCol-th column of a table.
2753*/
2754void sqlite3ExprCodeGetColumnOfTable(
2755 Vdbe *v, /* The VDBE under construction */
2756 Table *pTab, /* The table containing the value */
drh313619f2013-10-31 20:34:06 +00002757 int iTabCur, /* The table cursor. Or the PK cursor for WITHOUT ROWID */
drh5c092e82010-05-14 19:24:02 +00002758 int iCol, /* Index of the column to extract */
drh313619f2013-10-31 20:34:06 +00002759 int regOut /* Extract the value into this register */
drh5c092e82010-05-14 19:24:02 +00002760){
2761 if( iCol<0 || iCol==pTab->iPKey ){
2762 sqlite3VdbeAddOp2(v, OP_Rowid, iTabCur, regOut);
2763 }else{
2764 int op = IsVirtual(pTab) ? OP_VColumn : OP_Column;
drhee0ec8e2013-10-31 17:38:01 +00002765 int x = iCol;
drh35db31b2016-06-02 23:13:21 +00002766 if( !HasRowid(pTab) && !IsVirtual(pTab) ){
drhee0ec8e2013-10-31 17:38:01 +00002767 x = sqlite3ColumnOfIndex(sqlite3PrimaryKeyIndex(pTab), iCol);
2768 }
2769 sqlite3VdbeAddOp3(v, op, iTabCur, x, regOut);
drh5c092e82010-05-14 19:24:02 +00002770 }
2771 if( iCol>=0 ){
2772 sqlite3ColumnDefault(v, pTab, iCol, regOut);
2773 }
2774}
2775
2776/*
drh945498f2007-02-24 11:52:52 +00002777** Generate code that will extract the iColumn-th column from
drhce78bc62015-10-15 19:21:51 +00002778** table pTab and store the column value in a register.
2779**
2780** An effort is made to store the column value in register iReg. This
2781** is not garanteeed for GetColumn() - the result can be stored in
2782** any register. But the result is guaranteed to land in register iReg
2783** for GetColumnToReg().
drhe55cbd72008-03-31 23:48:03 +00002784**
2785** There must be an open cursor to pTab in iTable when this routine
2786** is called. If iColumn<0 then code is generated that extracts the rowid.
drh945498f2007-02-24 11:52:52 +00002787*/
drhe55cbd72008-03-31 23:48:03 +00002788int sqlite3ExprCodeGetColumn(
2789 Parse *pParse, /* Parsing and code generating context */
drh2133d822008-01-03 18:44:59 +00002790 Table *pTab, /* Description of the table we are reading from */
2791 int iColumn, /* Index of the table column */
2792 int iTable, /* The cursor pointing to the table */
drha748fdc2012-03-28 01:34:47 +00002793 int iReg, /* Store results here */
drhce78bc62015-10-15 19:21:51 +00002794 u8 p5 /* P5 value for OP_Column + FLAGS */
drh2133d822008-01-03 18:44:59 +00002795){
drhe55cbd72008-03-31 23:48:03 +00002796 Vdbe *v = pParse->pVdbe;
2797 int i;
drhda250ea2008-04-01 05:07:14 +00002798 struct yColCache *p;
drhe55cbd72008-03-31 23:48:03 +00002799
drhceea3322009-04-23 13:22:42 +00002800 for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
drhb6da74e2009-12-24 16:00:28 +00002801 if( p->iReg>0 && p->iTable==iTable && p->iColumn==iColumn ){
drhceea3322009-04-23 13:22:42 +00002802 p->lru = pParse->iCacheCnt++;
drh5cd79232009-05-25 11:46:29 +00002803 sqlite3ExprCachePinRegister(pParse, p->iReg);
drhda250ea2008-04-01 05:07:14 +00002804 return p->iReg;
drhe55cbd72008-03-31 23:48:03 +00002805 }
2806 }
2807 assert( v!=0 );
drh5c092e82010-05-14 19:24:02 +00002808 sqlite3ExprCodeGetColumnOfTable(v, pTab, iTable, iColumn, iReg);
drha748fdc2012-03-28 01:34:47 +00002809 if( p5 ){
2810 sqlite3VdbeChangeP5(v, p5);
2811 }else{
2812 sqlite3ExprCacheStore(pParse, iTable, iColumn, iReg);
2813 }
drhe55cbd72008-03-31 23:48:03 +00002814 return iReg;
2815}
drhce78bc62015-10-15 19:21:51 +00002816void sqlite3ExprCodeGetColumnToReg(
2817 Parse *pParse, /* Parsing and code generating context */
2818 Table *pTab, /* Description of the table we are reading from */
2819 int iColumn, /* Index of the table column */
2820 int iTable, /* The cursor pointing to the table */
2821 int iReg /* Store results here */
2822){
2823 int r1 = sqlite3ExprCodeGetColumn(pParse, pTab, iColumn, iTable, iReg, 0);
2824 if( r1!=iReg ) sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, r1, iReg);
2825}
2826
drhe55cbd72008-03-31 23:48:03 +00002827
2828/*
drhceea3322009-04-23 13:22:42 +00002829** Clear all column cache entries.
drhe55cbd72008-03-31 23:48:03 +00002830*/
drhceea3322009-04-23 13:22:42 +00002831void sqlite3ExprCacheClear(Parse *pParse){
2832 int i;
2833 struct yColCache *p;
2834
drh9ac79622013-12-18 15:11:47 +00002835#if SQLITE_DEBUG
2836 if( pParse->db->flags & SQLITE_VdbeAddopTrace ){
2837 printf("CLEAR\n");
2838 }
2839#endif
drhceea3322009-04-23 13:22:42 +00002840 for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
2841 if( p->iReg ){
2842 cacheEntryClear(pParse, p);
drhe55cbd72008-03-31 23:48:03 +00002843 }
drhe55cbd72008-03-31 23:48:03 +00002844 }
2845}
2846
2847/*
drhda250ea2008-04-01 05:07:14 +00002848** Record the fact that an affinity change has occurred on iCount
2849** registers starting with iStart.
drhe55cbd72008-03-31 23:48:03 +00002850*/
drhda250ea2008-04-01 05:07:14 +00002851void sqlite3ExprCacheAffinityChange(Parse *pParse, int iStart, int iCount){
drhf49f3522009-12-30 14:12:38 +00002852 sqlite3ExprCacheRemove(pParse, iStart, iCount);
drhe55cbd72008-03-31 23:48:03 +00002853}
2854
2855/*
drhb21e7c72008-06-22 12:37:57 +00002856** Generate code to move content from registers iFrom...iFrom+nReg-1
2857** over to iTo..iTo+nReg-1. Keep the column cache up-to-date.
drhe55cbd72008-03-31 23:48:03 +00002858*/
drhb21e7c72008-06-22 12:37:57 +00002859void sqlite3ExprCodeMove(Parse *pParse, int iFrom, int iTo, int nReg){
drhe8e4af72012-09-21 00:04:28 +00002860 assert( iFrom>=iTo+nReg || iFrom+nReg<=iTo );
drh079a3072014-03-19 14:10:55 +00002861 sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move, iFrom, iTo, nReg);
drh236241a2014-09-12 17:41:30 +00002862 sqlite3ExprCacheRemove(pParse, iFrom, nReg);
drh945498f2007-02-24 11:52:52 +00002863}
2864
drhf49f3522009-12-30 14:12:38 +00002865#if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
drh92b01d52008-06-24 00:32:35 +00002866/*
drh652fbf52008-04-01 01:42:41 +00002867** Return true if any register in the range iFrom..iTo (inclusive)
2868** is used as part of the column cache.
drhf49f3522009-12-30 14:12:38 +00002869**
2870** This routine is used within assert() and testcase() macros only
2871** and does not appear in a normal build.
drh652fbf52008-04-01 01:42:41 +00002872*/
2873static int usedAsColumnCache(Parse *pParse, int iFrom, int iTo){
2874 int i;
drhceea3322009-04-23 13:22:42 +00002875 struct yColCache *p;
2876 for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
2877 int r = p->iReg;
drhf49f3522009-12-30 14:12:38 +00002878 if( r>=iFrom && r<=iTo ) return 1; /*NO_TEST*/
drh652fbf52008-04-01 01:42:41 +00002879 }
2880 return 0;
2881}
drhf49f3522009-12-30 14:12:38 +00002882#endif /* SQLITE_DEBUG || SQLITE_COVERAGE_TEST */
drh652fbf52008-04-01 01:42:41 +00002883
drhbea119c2016-04-11 18:15:37 +00002884
drh652fbf52008-04-01 01:42:41 +00002885/*
drha4c3c872013-09-12 17:29:25 +00002886** Convert an expression node to a TK_REGISTER
2887*/
2888static void exprToRegister(Expr *p, int iReg){
2889 p->op2 = p->op;
2890 p->op = TK_REGISTER;
2891 p->iTable = iReg;
2892 ExprClearProperty(p, EP_Skip);
2893}
2894
dan71c57db2016-07-09 20:23:55 +00002895static void exprCodeBetween(Parse*,Expr*,int,void(*)(Parse*,Expr*,int,int),int);
2896
drha4c3c872013-09-12 17:29:25 +00002897/*
drhcce7d172000-05-31 15:34:51 +00002898** Generate code into the current Vdbe to evaluate the given
drh2dcef112008-01-12 19:03:48 +00002899** expression. Attempt to store the results in register "target".
2900** Return the register where results are stored.
drh389a1ad2008-01-03 23:44:53 +00002901**
drh8b213892008-08-29 02:14:02 +00002902** With this routine, there is no guarantee that results will
drh2dcef112008-01-12 19:03:48 +00002903** be stored in target. The result might be stored in some other
2904** register if it is convenient to do so. The calling function
2905** must check the return code and move the results to the desired
2906** register.
drhcce7d172000-05-31 15:34:51 +00002907*/
drh678ccce2008-03-31 18:19:54 +00002908int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){
drh2dcef112008-01-12 19:03:48 +00002909 Vdbe *v = pParse->pVdbe; /* The VM under construction */
2910 int op; /* The opcode being coded */
2911 int inReg = target; /* Results stored in register inReg */
2912 int regFree1 = 0; /* If non-zero free this temporary register */
2913 int regFree2 = 0; /* If non-zero free this temporary register */
drh678ccce2008-03-31 18:19:54 +00002914 int r1, r2, r3, r4; /* Various register numbers */
drh20411ea2009-05-29 19:00:12 +00002915 sqlite3 *db = pParse->db; /* The database connection */
drh10d1edf2013-11-15 15:52:39 +00002916 Expr tempX; /* Temporary expression node */
dan71c57db2016-07-09 20:23:55 +00002917 int p5 = 0;
drhffe07b22005-11-03 00:41:17 +00002918
drh9cbf3422008-01-17 16:22:13 +00002919 assert( target>0 && target<=pParse->nMem );
drh20411ea2009-05-29 19:00:12 +00002920 if( v==0 ){
2921 assert( pParse->db->mallocFailed );
2922 return 0;
2923 }
drh389a1ad2008-01-03 23:44:53 +00002924
2925 if( pExpr==0 ){
2926 op = TK_NULL;
2927 }else{
2928 op = pExpr->op;
2929 }
drhf2bc0132004-10-04 13:19:23 +00002930 switch( op ){
drh13449892005-09-07 21:22:45 +00002931 case TK_AGG_COLUMN: {
2932 AggInfo *pAggInfo = pExpr->pAggInfo;
2933 struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg];
2934 if( !pAggInfo->directMode ){
drh9de221d2008-01-05 06:51:30 +00002935 assert( pCol->iMem>0 );
2936 inReg = pCol->iMem;
drh13449892005-09-07 21:22:45 +00002937 break;
2938 }else if( pAggInfo->useSortingIdx ){
dan5134d132011-09-02 10:31:11 +00002939 sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab,
drh389a1ad2008-01-03 23:44:53 +00002940 pCol->iSorterColumn, target);
drh13449892005-09-07 21:22:45 +00002941 break;
2942 }
2943 /* Otherwise, fall thru into the TK_COLUMN case */
2944 }
drh967e8b72000-06-21 13:59:10 +00002945 case TK_COLUMN: {
drhb2b9d3d2013-08-01 01:14:43 +00002946 int iTab = pExpr->iTable;
2947 if( iTab<0 ){
2948 if( pParse->ckBase>0 ){
2949 /* Generating CHECK constraints or inserting into partial index */
2950 inReg = pExpr->iColumn + pParse->ckBase;
2951 break;
2952 }else{
drh1f9ca2c2015-08-25 16:57:52 +00002953 /* Coding an expression that is part of an index where column names
2954 ** in the index refer to the table to which the index belongs */
2955 iTab = pParse->iSelfTab;
drhb2b9d3d2013-08-01 01:14:43 +00002956 }
drh22827922000-06-06 17:27:05 +00002957 }
drhb2b9d3d2013-08-01 01:14:43 +00002958 inReg = sqlite3ExprCodeGetColumn(pParse, pExpr->pTab,
2959 pExpr->iColumn, iTab, target,
2960 pExpr->op2);
drhcce7d172000-05-31 15:34:51 +00002961 break;
2962 }
2963 case TK_INTEGER: {
drh13573c72010-01-12 17:04:07 +00002964 codeInteger(pParse, pExpr, 0, target);
drhfec19aa2004-05-19 20:41:03 +00002965 break;
2966 }
drh13573c72010-01-12 17:04:07 +00002967#ifndef SQLITE_OMIT_FLOATING_POINT
drh598f1342007-10-23 15:39:45 +00002968 case TK_FLOAT: {
drh33e619f2009-05-28 01:00:55 +00002969 assert( !ExprHasProperty(pExpr, EP_IntValue) );
2970 codeReal(v, pExpr->u.zToken, 0, target);
drh598f1342007-10-23 15:39:45 +00002971 break;
2972 }
drh13573c72010-01-12 17:04:07 +00002973#endif
drhfec19aa2004-05-19 20:41:03 +00002974 case TK_STRING: {
drh33e619f2009-05-28 01:00:55 +00002975 assert( !ExprHasProperty(pExpr, EP_IntValue) );
drh076e85f2015-09-03 13:46:12 +00002976 sqlite3VdbeLoadString(v, target, pExpr->u.zToken);
drhcce7d172000-05-31 15:34:51 +00002977 break;
2978 }
drhf0863fe2005-06-12 21:35:51 +00002979 case TK_NULL: {
drh9de221d2008-01-05 06:51:30 +00002980 sqlite3VdbeAddOp2(v, OP_Null, 0, target);
drhf0863fe2005-06-12 21:35:51 +00002981 break;
2982 }
danielk19775338a5f2005-01-20 13:03:10 +00002983#ifndef SQLITE_OMIT_BLOB_LITERAL
danielk1977c572ef72004-05-27 09:28:41 +00002984 case TK_BLOB: {
drh6c8c6ce2005-08-23 11:17:58 +00002985 int n;
2986 const char *z;
drhca48c902008-01-18 14:08:24 +00002987 char *zBlob;
drh33e619f2009-05-28 01:00:55 +00002988 assert( !ExprHasProperty(pExpr, EP_IntValue) );
2989 assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
2990 assert( pExpr->u.zToken[1]=='\'' );
2991 z = &pExpr->u.zToken[2];
drhb7916a72009-05-27 10:31:29 +00002992 n = sqlite3Strlen30(z) - 1;
2993 assert( z[n]=='\'' );
drhca48c902008-01-18 14:08:24 +00002994 zBlob = sqlite3HexToBlob(sqlite3VdbeDb(v), z, n);
2995 sqlite3VdbeAddOp4(v, OP_Blob, n/2, target, 0, zBlob, P4_DYNAMIC);
danielk1977c572ef72004-05-27 09:28:41 +00002996 break;
2997 }
danielk19775338a5f2005-01-20 13:03:10 +00002998#endif
drh50457892003-09-06 01:10:47 +00002999 case TK_VARIABLE: {
drh33e619f2009-05-28 01:00:55 +00003000 assert( !ExprHasProperty(pExpr, EP_IntValue) );
3001 assert( pExpr->u.zToken!=0 );
3002 assert( pExpr->u.zToken[0]!=0 );
drheaf52d82010-05-12 13:50:23 +00003003 sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iColumn, target);
3004 if( pExpr->u.zToken[1]!=0 ){
drh04e9eea2011-06-01 19:16:06 +00003005 assert( pExpr->u.zToken[0]=='?'
3006 || strcmp(pExpr->u.zToken, pParse->azVar[pExpr->iColumn-1])==0 );
3007 sqlite3VdbeChangeP4(v, -1, pParse->azVar[pExpr->iColumn-1], P4_STATIC);
drh895d7472004-08-20 16:02:39 +00003008 }
drh50457892003-09-06 01:10:47 +00003009 break;
3010 }
drh4e0cff62004-11-05 05:10:28 +00003011 case TK_REGISTER: {
drh9de221d2008-01-05 06:51:30 +00003012 inReg = pExpr->iTable;
drh4e0cff62004-11-05 05:10:28 +00003013 break;
3014 }
drh487e2622005-06-25 18:42:14 +00003015#ifndef SQLITE_OMIT_CAST
3016 case TK_CAST: {
3017 /* Expressions of the form: CAST(pLeft AS token) */
drh2dcef112008-01-12 19:03:48 +00003018 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
drh1735fa82008-11-06 15:33:03 +00003019 if( inReg!=target ){
3020 sqlite3VdbeAddOp2(v, OP_SCopy, inReg, target);
3021 inReg = target;
3022 }
drh4169e432014-08-25 20:11:52 +00003023 sqlite3VdbeAddOp2(v, OP_Cast, target,
3024 sqlite3AffinityType(pExpr->u.zToken, 0));
drhc5499be2008-04-01 15:06:33 +00003025 testcase( usedAsColumnCache(pParse, inReg, inReg) );
drhb3843a82008-04-01 12:24:11 +00003026 sqlite3ExprCacheAffinityChange(pParse, inReg, 1);
drh487e2622005-06-25 18:42:14 +00003027 break;
3028 }
3029#endif /* SQLITE_OMIT_CAST */
dan71c57db2016-07-09 20:23:55 +00003030 case TK_IS:
3031 case TK_ISNOT:
3032 op = (op==TK_IS) ? TK_EQ : TK_NE;
3033 p5 = SQLITE_NULLEQ;
3034 /* fall-through */
drhc9b84a12002-06-20 11:36:48 +00003035 case TK_LT:
3036 case TK_LE:
3037 case TK_GT:
3038 case TK_GE:
3039 case TK_NE:
3040 case TK_EQ: {
dan71c57db2016-07-09 20:23:55 +00003041 Expr *pLeft = pExpr->pLeft;
3042 if( (pLeft->flags & EP_Vector) ){
3043 codeVectorCompare(pParse, pExpr, target);
3044 }else{
3045 r1 = sqlite3ExprCodeTemp(pParse, pLeft, &regFree1);
3046 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
3047 codeCompare(pParse, pLeft, pExpr->pRight, op,
3048 r1, r2, inReg, SQLITE_STOREP2 | p5);
3049 assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
3050 assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
3051 assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
3052 assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
3053 assert(TK_EQ==OP_Eq); testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq);
3054 assert(TK_NE==OP_Ne); testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne);
3055 testcase( regFree1==0 );
3056 testcase( regFree2==0 );
3057 }
drh6a2fe092009-09-23 02:29:36 +00003058 break;
3059 }
drhcce7d172000-05-31 15:34:51 +00003060 case TK_AND:
3061 case TK_OR:
3062 case TK_PLUS:
3063 case TK_STAR:
3064 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00003065 case TK_REM:
3066 case TK_BITAND:
3067 case TK_BITOR:
drh17c40292004-07-21 02:53:29 +00003068 case TK_SLASH:
drhbf4133c2001-10-13 02:59:08 +00003069 case TK_LSHIFT:
drh855eb1c2004-08-31 13:45:11 +00003070 case TK_RSHIFT:
drh00400772000-06-16 20:51:26 +00003071 case TK_CONCAT: {
drh7d176102014-02-18 03:07:12 +00003072 assert( TK_AND==OP_And ); testcase( op==TK_AND );
3073 assert( TK_OR==OP_Or ); testcase( op==TK_OR );
3074 assert( TK_PLUS==OP_Add ); testcase( op==TK_PLUS );
3075 assert( TK_MINUS==OP_Subtract ); testcase( op==TK_MINUS );
3076 assert( TK_REM==OP_Remainder ); testcase( op==TK_REM );
3077 assert( TK_BITAND==OP_BitAnd ); testcase( op==TK_BITAND );
3078 assert( TK_BITOR==OP_BitOr ); testcase( op==TK_BITOR );
3079 assert( TK_SLASH==OP_Divide ); testcase( op==TK_SLASH );
3080 assert( TK_LSHIFT==OP_ShiftLeft ); testcase( op==TK_LSHIFT );
3081 assert( TK_RSHIFT==OP_ShiftRight ); testcase( op==TK_RSHIFT );
3082 assert( TK_CONCAT==OP_Concat ); testcase( op==TK_CONCAT );
drh2dcef112008-01-12 19:03:48 +00003083 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
3084 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
drh5b6afba2008-01-05 16:29:28 +00003085 sqlite3VdbeAddOp3(v, op, r2, r1, target);
drhc5499be2008-04-01 15:06:33 +00003086 testcase( regFree1==0 );
3087 testcase( regFree2==0 );
drh00400772000-06-16 20:51:26 +00003088 break;
3089 }
drhcce7d172000-05-31 15:34:51 +00003090 case TK_UMINUS: {
drhfec19aa2004-05-19 20:41:03 +00003091 Expr *pLeft = pExpr->pLeft;
3092 assert( pLeft );
drh13573c72010-01-12 17:04:07 +00003093 if( pLeft->op==TK_INTEGER ){
3094 codeInteger(pParse, pLeft, 1, target);
3095#ifndef SQLITE_OMIT_FLOATING_POINT
3096 }else if( pLeft->op==TK_FLOAT ){
drh33e619f2009-05-28 01:00:55 +00003097 assert( !ExprHasProperty(pExpr, EP_IntValue) );
3098 codeReal(v, pLeft->u.zToken, 1, target);
drh13573c72010-01-12 17:04:07 +00003099#endif
drh3c84ddf2008-01-09 02:15:38 +00003100 }else{
drh10d1edf2013-11-15 15:52:39 +00003101 tempX.op = TK_INTEGER;
3102 tempX.flags = EP_IntValue|EP_TokenOnly;
3103 tempX.u.iValue = 0;
3104 r1 = sqlite3ExprCodeTemp(pParse, &tempX, &regFree1);
drhe55cbd72008-03-31 23:48:03 +00003105 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree2);
drh2dcef112008-01-12 19:03:48 +00003106 sqlite3VdbeAddOp3(v, OP_Subtract, r2, r1, target);
drhc5499be2008-04-01 15:06:33 +00003107 testcase( regFree2==0 );
drh6e142f52000-06-08 13:36:40 +00003108 }
drh3c84ddf2008-01-09 02:15:38 +00003109 inReg = target;
3110 break;
drh6e142f52000-06-08 13:36:40 +00003111 }
drhbf4133c2001-10-13 02:59:08 +00003112 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00003113 case TK_NOT: {
drh7d176102014-02-18 03:07:12 +00003114 assert( TK_BITNOT==OP_BitNot ); testcase( op==TK_BITNOT );
3115 assert( TK_NOT==OP_Not ); testcase( op==TK_NOT );
drhe99fa2a2008-12-15 15:27:51 +00003116 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
3117 testcase( regFree1==0 );
3118 inReg = target;
3119 sqlite3VdbeAddOp2(v, op, r1, inReg);
drhcce7d172000-05-31 15:34:51 +00003120 break;
3121 }
3122 case TK_ISNULL:
3123 case TK_NOTNULL: {
drh6a288a32008-01-07 19:20:24 +00003124 int addr;
drh7d176102014-02-18 03:07:12 +00003125 assert( TK_ISNULL==OP_IsNull ); testcase( op==TK_ISNULL );
3126 assert( TK_NOTNULL==OP_NotNull ); testcase( op==TK_NOTNULL );
drh9de221d2008-01-05 06:51:30 +00003127 sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
drh2dcef112008-01-12 19:03:48 +00003128 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
drhc5499be2008-04-01 15:06:33 +00003129 testcase( regFree1==0 );
drh2dcef112008-01-12 19:03:48 +00003130 addr = sqlite3VdbeAddOp1(v, op, r1);
drh7d176102014-02-18 03:07:12 +00003131 VdbeCoverageIf(v, op==TK_ISNULL);
3132 VdbeCoverageIf(v, op==TK_NOTNULL);
drha9769792014-08-04 16:39:39 +00003133 sqlite3VdbeAddOp2(v, OP_Integer, 0, target);
drh6a288a32008-01-07 19:20:24 +00003134 sqlite3VdbeJumpHere(v, addr);
drhf2bc0132004-10-04 13:19:23 +00003135 break;
drhcce7d172000-05-31 15:34:51 +00003136 }
drh22827922000-06-06 17:27:05 +00003137 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00003138 AggInfo *pInfo = pExpr->pAggInfo;
drh7e56e712005-11-16 12:53:15 +00003139 if( pInfo==0 ){
drh33e619f2009-05-28 01:00:55 +00003140 assert( !ExprHasProperty(pExpr, EP_IntValue) );
3141 sqlite3ErrorMsg(pParse, "misuse of aggregate: %s()", pExpr->u.zToken);
drh7e56e712005-11-16 12:53:15 +00003142 }else{
drh9de221d2008-01-05 06:51:30 +00003143 inReg = pInfo->aFunc[pExpr->iAgg].iMem;
drh7e56e712005-11-16 12:53:15 +00003144 }
drh22827922000-06-06 17:27:05 +00003145 break;
3146 }
drhcce7d172000-05-31 15:34:51 +00003147 case TK_FUNCTION: {
drh12ffee82009-04-08 13:51:51 +00003148 ExprList *pFarg; /* List of function arguments */
3149 int nFarg; /* Number of function arguments */
3150 FuncDef *pDef; /* The function definition object */
drh12ffee82009-04-08 13:51:51 +00003151 const char *zId; /* The function name */
drh693e6712014-01-24 22:58:00 +00003152 u32 constMask = 0; /* Mask of function arguments that are constant */
drh12ffee82009-04-08 13:51:51 +00003153 int i; /* Loop counter */
3154 u8 enc = ENC(db); /* The text encoding used by this database */
3155 CollSeq *pColl = 0; /* A collating sequence */
drh17435752007-08-16 04:30:38 +00003156
danielk19776ab3a2e2009-02-19 14:39:25 +00003157 assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
drhc5cd1242013-09-12 16:50:49 +00003158 if( ExprHasProperty(pExpr, EP_TokenOnly) ){
drh12ffee82009-04-08 13:51:51 +00003159 pFarg = 0;
3160 }else{
3161 pFarg = pExpr->x.pList;
3162 }
3163 nFarg = pFarg ? pFarg->nExpr : 0;
drh33e619f2009-05-28 01:00:55 +00003164 assert( !ExprHasProperty(pExpr, EP_IntValue) );
3165 zId = pExpr->u.zToken;
drh80738d92016-02-15 00:34:16 +00003166 pDef = sqlite3FindFunction(db, zId, nFarg, enc, 0);
drh2d801512016-01-14 22:19:58 +00003167 if( pDef==0 || pDef->xFinalize!=0 ){
drh80738d92016-02-15 00:34:16 +00003168 sqlite3ErrorMsg(pParse, "unknown function: %s()", zId);
drhfeb306f2009-08-18 16:05:46 +00003169 break;
3170 }
drhae6bb952009-11-11 00:24:31 +00003171
3172 /* Attempt a direct implementation of the built-in COALESCE() and
peter.d.reid60ec9142014-09-06 16:39:46 +00003173 ** IFNULL() functions. This avoids unnecessary evaluation of
drhae6bb952009-11-11 00:24:31 +00003174 ** arguments past the first non-NULL argument.
3175 */
drhd36e1042013-09-06 13:10:12 +00003176 if( pDef->funcFlags & SQLITE_FUNC_COALESCE ){
drhae6bb952009-11-11 00:24:31 +00003177 int endCoalesce = sqlite3VdbeMakeLabel(v);
3178 assert( nFarg>=2 );
3179 sqlite3ExprCode(pParse, pFarg->a[0].pExpr, target);
3180 for(i=1; i<nFarg; i++){
3181 sqlite3VdbeAddOp2(v, OP_NotNull, target, endCoalesce);
drh688852a2014-02-17 22:40:43 +00003182 VdbeCoverage(v);
drhf49f3522009-12-30 14:12:38 +00003183 sqlite3ExprCacheRemove(pParse, target, 1);
drhae6bb952009-11-11 00:24:31 +00003184 sqlite3ExprCachePush(pParse);
3185 sqlite3ExprCode(pParse, pFarg->a[i].pExpr, target);
drhd2490902014-04-13 19:28:15 +00003186 sqlite3ExprCachePop(pParse);
drhae6bb952009-11-11 00:24:31 +00003187 }
3188 sqlite3VdbeResolveLabel(v, endCoalesce);
3189 break;
3190 }
3191
drhcca9f3d2013-09-06 15:23:29 +00003192 /* The UNLIKELY() function is a no-op. The result is the value
3193 ** of the first argument.
3194 */
3195 if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){
3196 assert( nFarg>=1 );
drh5f02ab02015-06-20 13:18:34 +00003197 inReg = sqlite3ExprCodeTarget(pParse, pFarg->a[0].pExpr, target);
drhcca9f3d2013-09-06 15:23:29 +00003198 break;
3199 }
drhae6bb952009-11-11 00:24:31 +00003200
drhd1a01ed2013-11-21 16:08:52 +00003201 for(i=0; i<nFarg; i++){
3202 if( i<32 && sqlite3ExprIsConstant(pFarg->a[i].pExpr) ){
drh693e6712014-01-24 22:58:00 +00003203 testcase( i==31 );
3204 constMask |= MASKBIT32(i);
drhd1a01ed2013-11-21 16:08:52 +00003205 }
3206 if( (pDef->funcFlags & SQLITE_FUNC_NEEDCOLL)!=0 && !pColl ){
3207 pColl = sqlite3ExprCollSeq(pParse, pFarg->a[i].pExpr);
3208 }
3209 }
drh12ffee82009-04-08 13:51:51 +00003210 if( pFarg ){
drhd1a01ed2013-11-21 16:08:52 +00003211 if( constMask ){
3212 r1 = pParse->nMem+1;
3213 pParse->nMem += nFarg;
3214 }else{
3215 r1 = sqlite3GetTempRange(pParse, nFarg);
3216 }
drha748fdc2012-03-28 01:34:47 +00003217
3218 /* For length() and typeof() functions with a column argument,
3219 ** set the P5 parameter to the OP_Column opcode to OPFLAG_LENGTHARG
3220 ** or OPFLAG_TYPEOFARG respectively, to avoid unnecessary data
3221 ** loading.
3222 */
drhd36e1042013-09-06 13:10:12 +00003223 if( (pDef->funcFlags & (SQLITE_FUNC_LENGTH|SQLITE_FUNC_TYPEOF))!=0 ){
drh4e245a42012-03-30 00:00:36 +00003224 u8 exprOp;
drha748fdc2012-03-28 01:34:47 +00003225 assert( nFarg==1 );
3226 assert( pFarg->a[0].pExpr!=0 );
drh4e245a42012-03-30 00:00:36 +00003227 exprOp = pFarg->a[0].pExpr->op;
3228 if( exprOp==TK_COLUMN || exprOp==TK_AGG_COLUMN ){
drha748fdc2012-03-28 01:34:47 +00003229 assert( SQLITE_FUNC_LENGTH==OPFLAG_LENGTHARG );
3230 assert( SQLITE_FUNC_TYPEOF==OPFLAG_TYPEOFARG );
drhb1fba282013-11-21 14:33:48 +00003231 testcase( pDef->funcFlags & OPFLAG_LENGTHARG );
3232 pFarg->a[0].pExpr->op2 =
3233 pDef->funcFlags & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG);
drha748fdc2012-03-28 01:34:47 +00003234 }
3235 }
3236
drhd7d385d2009-09-03 01:18:00 +00003237 sqlite3ExprCachePush(pParse); /* Ticket 2ea2425d34be */
drh5579d592015-08-26 14:01:41 +00003238 sqlite3ExprCodeExprList(pParse, pFarg, r1, 0,
drhd1a01ed2013-11-21 16:08:52 +00003239 SQLITE_ECEL_DUP|SQLITE_ECEL_FACTOR);
drhd2490902014-04-13 19:28:15 +00003240 sqlite3ExprCachePop(pParse); /* Ticket 2ea2425d34be */
drh892d3172008-01-10 03:46:36 +00003241 }else{
drh12ffee82009-04-08 13:51:51 +00003242 r1 = 0;
drh892d3172008-01-10 03:46:36 +00003243 }
drhb7f6f682006-07-08 17:06:43 +00003244#ifndef SQLITE_OMIT_VIRTUALTABLE
drha43fa222006-07-08 18:41:37 +00003245 /* Possibly overload the function if the first argument is
3246 ** a virtual table column.
3247 **
3248 ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the
3249 ** second argument, not the first, as the argument to test to
3250 ** see if it is a column in a virtual table. This is done because
3251 ** the left operand of infix functions (the operand we want to
3252 ** control overloading) ends up as the second argument to the
3253 ** function. The expression "A glob B" is equivalent to
3254 ** "glob(B,A). We want to use the A in "A glob B" to test
3255 ** for function overloading. But we use the B term in "glob(B,A)".
3256 */
drh12ffee82009-04-08 13:51:51 +00003257 if( nFarg>=2 && (pExpr->flags & EP_InfixFunc) ){
3258 pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[1].pExpr);
3259 }else if( nFarg>0 ){
3260 pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[0].pExpr);
drhb7f6f682006-07-08 17:06:43 +00003261 }
3262#endif
drhd36e1042013-09-06 13:10:12 +00003263 if( pDef->funcFlags & SQLITE_FUNC_NEEDCOLL ){
drh8b213892008-08-29 02:14:02 +00003264 if( !pColl ) pColl = db->pDfltColl;
drh66a51672008-01-03 00:01:23 +00003265 sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ);
danielk1977682f68b2004-06-05 10:22:17 +00003266 }
drh9c7c9132015-06-26 18:16:52 +00003267 sqlite3VdbeAddOp4(v, OP_Function0, constMask, r1, target,
drh66a51672008-01-03 00:01:23 +00003268 (char*)pDef, P4_FUNCDEF);
drh12ffee82009-04-08 13:51:51 +00003269 sqlite3VdbeChangeP5(v, (u8)nFarg);
drhd1a01ed2013-11-21 16:08:52 +00003270 if( nFarg && constMask==0 ){
drh12ffee82009-04-08 13:51:51 +00003271 sqlite3ReleaseTempRange(pParse, r1, nFarg);
drh2dcef112008-01-12 19:03:48 +00003272 }
drhcce7d172000-05-31 15:34:51 +00003273 break;
3274 }
drhfe2093d2005-01-20 22:48:47 +00003275#ifndef SQLITE_OMIT_SUBQUERY
3276 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00003277 case TK_SELECT: {
drhc5499be2008-04-01 15:06:33 +00003278 testcase( op==TK_EXISTS );
3279 testcase( op==TK_SELECT );
drh1450bc62009-10-30 13:25:56 +00003280 inReg = sqlite3CodeSubselect(pParse, pExpr, 0, 0);
drh19a775c2000-06-05 18:54:46 +00003281 break;
3282 }
drhfef52082000-06-06 01:50:43 +00003283 case TK_IN: {
drhe3365e62009-11-12 17:52:24 +00003284 int destIfFalse = sqlite3VdbeMakeLabel(v);
3285 int destIfNull = sqlite3VdbeMakeLabel(v);
3286 sqlite3VdbeAddOp2(v, OP_Null, 0, target);
3287 sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull);
3288 sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
3289 sqlite3VdbeResolveLabel(v, destIfFalse);
3290 sqlite3VdbeAddOp2(v, OP_AddImm, target, 0);
3291 sqlite3VdbeResolveLabel(v, destIfNull);
drhfef52082000-06-06 01:50:43 +00003292 break;
3293 }
drhe3365e62009-11-12 17:52:24 +00003294#endif /* SQLITE_OMIT_SUBQUERY */
3295
3296
drh2dcef112008-01-12 19:03:48 +00003297 /*
3298 ** x BETWEEN y AND z
3299 **
3300 ** This is equivalent to
3301 **
3302 ** x>=y AND x<=z
3303 **
3304 ** X is stored in pExpr->pLeft.
3305 ** Y is stored in pExpr->pList->a[0].pExpr.
3306 ** Z is stored in pExpr->pList->a[1].pExpr.
3307 */
drhfef52082000-06-06 01:50:43 +00003308 case TK_BETWEEN: {
dan71c57db2016-07-09 20:23:55 +00003309 exprCodeBetween(pParse, pExpr, target, 0, 0);
3310#if 0
drhbe5c89a2004-07-26 00:31:09 +00003311 Expr *pLeft = pExpr->pLeft;
danielk19776ab3a2e2009-02-19 14:39:25 +00003312 struct ExprList_item *pLItem = pExpr->x.pList->a;
drhbe5c89a2004-07-26 00:31:09 +00003313 Expr *pRight = pLItem->pExpr;
drh35573352008-01-08 23:54:25 +00003314
drhb6da74e2009-12-24 16:00:28 +00003315 r1 = sqlite3ExprCodeTemp(pParse, pLeft, &regFree1);
3316 r2 = sqlite3ExprCodeTemp(pParse, pRight, &regFree2);
drhc5499be2008-04-01 15:06:33 +00003317 testcase( regFree1==0 );
3318 testcase( regFree2==0 );
drh2dcef112008-01-12 19:03:48 +00003319 r3 = sqlite3GetTempReg(pParse);
drh678ccce2008-03-31 18:19:54 +00003320 r4 = sqlite3GetTempReg(pParse);
drh35573352008-01-08 23:54:25 +00003321 codeCompare(pParse, pLeft, pRight, OP_Ge,
drh7d176102014-02-18 03:07:12 +00003322 r1, r2, r3, SQLITE_STOREP2); VdbeCoverage(v);
drhbe5c89a2004-07-26 00:31:09 +00003323 pLItem++;
3324 pRight = pLItem->pExpr;
drh2dcef112008-01-12 19:03:48 +00003325 sqlite3ReleaseTempReg(pParse, regFree2);
3326 r2 = sqlite3ExprCodeTemp(pParse, pRight, &regFree2);
drhc5499be2008-04-01 15:06:33 +00003327 testcase( regFree2==0 );
drh678ccce2008-03-31 18:19:54 +00003328 codeCompare(pParse, pLeft, pRight, OP_Le, r1, r2, r4, SQLITE_STOREP2);
drh688852a2014-02-17 22:40:43 +00003329 VdbeCoverage(v);
drh678ccce2008-03-31 18:19:54 +00003330 sqlite3VdbeAddOp3(v, OP_And, r3, r4, target);
drh2dcef112008-01-12 19:03:48 +00003331 sqlite3ReleaseTempReg(pParse, r3);
drh678ccce2008-03-31 18:19:54 +00003332 sqlite3ReleaseTempReg(pParse, r4);
dan71c57db2016-07-09 20:23:55 +00003333#endif
drhfef52082000-06-06 01:50:43 +00003334 break;
3335 }
drh94fa9c42016-02-27 21:16:04 +00003336 case TK_SPAN:
drhae80dde2012-12-06 21:16:43 +00003337 case TK_COLLATE:
drh4f07e5f2007-05-14 11:34:46 +00003338 case TK_UPLUS: {
drh2dcef112008-01-12 19:03:48 +00003339 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
drha2e00042002-01-22 03:13:42 +00003340 break;
3341 }
drh2dcef112008-01-12 19:03:48 +00003342
dan165921a2009-08-28 18:53:45 +00003343 case TK_TRIGGER: {
dan65a7cd12009-09-01 12:16:01 +00003344 /* If the opcode is TK_TRIGGER, then the expression is a reference
3345 ** to a column in the new.* or old.* pseudo-tables available to
3346 ** trigger programs. In this case Expr.iTable is set to 1 for the
3347 ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
3348 ** is set to the column of the pseudo-table to read, or to -1 to
3349 ** read the rowid field.
3350 **
3351 ** The expression is implemented using an OP_Param opcode. The p1
3352 ** parameter is set to 0 for an old.rowid reference, or to (i+1)
3353 ** to reference another column of the old.* pseudo-table, where
3354 ** i is the index of the column. For a new.rowid reference, p1 is
3355 ** set to (n+1), where n is the number of columns in each pseudo-table.
3356 ** For a reference to any other column in the new.* pseudo-table, p1
3357 ** is set to (n+2+i), where n and i are as defined previously. For
3358 ** example, if the table on which triggers are being fired is
3359 ** declared as:
3360 **
3361 ** CREATE TABLE t1(a, b);
3362 **
3363 ** Then p1 is interpreted as follows:
3364 **
3365 ** p1==0 -> old.rowid p1==3 -> new.rowid
3366 ** p1==1 -> old.a p1==4 -> new.a
3367 ** p1==2 -> old.b p1==5 -> new.b
3368 */
dan2832ad42009-08-31 15:27:27 +00003369 Table *pTab = pExpr->pTab;
dan65a7cd12009-09-01 12:16:01 +00003370 int p1 = pExpr->iTable * (pTab->nCol+1) + 1 + pExpr->iColumn;
3371
3372 assert( pExpr->iTable==0 || pExpr->iTable==1 );
3373 assert( pExpr->iColumn>=-1 && pExpr->iColumn<pTab->nCol );
3374 assert( pTab->iPKey<0 || pExpr->iColumn!=pTab->iPKey );
3375 assert( p1>=0 && p1<(pTab->nCol*2+2) );
3376
3377 sqlite3VdbeAddOp2(v, OP_Param, p1, target);
dan2bd93512009-08-31 08:22:46 +00003378 VdbeComment((v, "%s.%s -> $%d",
3379 (pExpr->iTable ? "new" : "old"),
dan76d462e2009-08-30 11:42:51 +00003380 (pExpr->iColumn<0 ? "rowid" : pExpr->pTab->aCol[pExpr->iColumn].zName),
dan2bd93512009-08-31 08:22:46 +00003381 target
dan165921a2009-08-28 18:53:45 +00003382 ));
dan65a7cd12009-09-01 12:16:01 +00003383
drh44dbca82010-01-13 04:22:20 +00003384#ifndef SQLITE_OMIT_FLOATING_POINT
dan65a7cd12009-09-01 12:16:01 +00003385 /* If the column has REAL affinity, it may currently be stored as an
drh113762a2014-11-19 16:36:25 +00003386 ** integer. Use OP_RealAffinity to make sure it is really real.
3387 **
3388 ** EVIDENCE-OF: R-60985-57662 SQLite will convert the value back to
3389 ** floating point when extracting it from the record. */
dan2832ad42009-08-31 15:27:27 +00003390 if( pExpr->iColumn>=0
3391 && pTab->aCol[pExpr->iColumn].affinity==SQLITE_AFF_REAL
3392 ){
3393 sqlite3VdbeAddOp1(v, OP_RealAffinity, target);
3394 }
drh44dbca82010-01-13 04:22:20 +00003395#endif
dan165921a2009-08-28 18:53:45 +00003396 break;
3397 }
3398
dan71c57db2016-07-09 20:23:55 +00003399 case TK_VECTOR: {
3400 sqlite3ErrorMsg(pParse, "invalid use of row value (1)");
3401 break;
3402 }
3403
3404 case TK_SELECT_COLUMN: {
3405 Expr *pLeft = pExpr->pLeft;
3406 assert( pLeft );
3407 assert( pLeft->op==TK_SELECT || pLeft->op==TK_REGISTER );
3408 if( pLeft->op==TK_SELECT ){
3409 pLeft->iTable = sqlite3CodeSubselect(pParse, pLeft, 0, 0);
3410 pLeft->op = TK_REGISTER;
3411 }
3412 inReg = pLeft->iTable + pExpr->iColumn;
3413 break;
3414 }
dan165921a2009-08-28 18:53:45 +00003415
drh2dcef112008-01-12 19:03:48 +00003416 /*
3417 ** Form A:
3418 ** CASE x WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
3419 **
3420 ** Form B:
3421 ** CASE WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
3422 **
3423 ** Form A is can be transformed into the equivalent form B as follows:
3424 ** CASE WHEN x=e1 THEN r1 WHEN x=e2 THEN r2 ...
3425 ** WHEN x=eN THEN rN ELSE y END
3426 **
3427 ** X (if it exists) is in pExpr->pLeft.
drhc5cd1242013-09-12 16:50:49 +00003428 ** Y is in the last element of pExpr->x.pList if pExpr->x.pList->nExpr is
3429 ** odd. The Y is also optional. If the number of elements in x.pList
3430 ** is even, then Y is omitted and the "otherwise" result is NULL.
drh2dcef112008-01-12 19:03:48 +00003431 ** Ei is in pExpr->pList->a[i*2] and Ri is pExpr->pList->a[i*2+1].
3432 **
3433 ** The result of the expression is the Ri for the first matching Ei,
3434 ** or if there is no matching Ei, the ELSE term Y, or if there is
3435 ** no ELSE term, NULL.
3436 */
drh33cd4902009-05-30 20:49:20 +00003437 default: assert( op==TK_CASE ); {
drh2dcef112008-01-12 19:03:48 +00003438 int endLabel; /* GOTO label for end of CASE stmt */
3439 int nextCase; /* GOTO label for next WHEN clause */
3440 int nExpr; /* 2x number of WHEN terms */
3441 int i; /* Loop counter */
3442 ExprList *pEList; /* List of WHEN terms */
3443 struct ExprList_item *aListelem; /* Array of WHEN terms */
3444 Expr opCompare; /* The X==Ei expression */
drh2dcef112008-01-12 19:03:48 +00003445 Expr *pX; /* The X expression */
drh1bd10f82008-12-10 21:19:56 +00003446 Expr *pTest = 0; /* X==Ei (form A) or just Ei (form B) */
drhceea3322009-04-23 13:22:42 +00003447 VVA_ONLY( int iCacheLevel = pParse->iCacheLevel; )
drh17a7f8d2002-03-24 13:13:27 +00003448
danielk19776ab3a2e2009-02-19 14:39:25 +00003449 assert( !ExprHasProperty(pExpr, EP_xIsSelect) && pExpr->x.pList );
danielk19776ab3a2e2009-02-19 14:39:25 +00003450 assert(pExpr->x.pList->nExpr > 0);
3451 pEList = pExpr->x.pList;
drhbe5c89a2004-07-26 00:31:09 +00003452 aListelem = pEList->a;
3453 nExpr = pEList->nExpr;
drh2dcef112008-01-12 19:03:48 +00003454 endLabel = sqlite3VdbeMakeLabel(v);
3455 if( (pX = pExpr->pLeft)!=0 ){
drh10d1edf2013-11-15 15:52:39 +00003456 tempX = *pX;
drh33cd4902009-05-30 20:49:20 +00003457 testcase( pX->op==TK_COLUMN );
drh10d1edf2013-11-15 15:52:39 +00003458 exprToRegister(&tempX, sqlite3ExprCodeTemp(pParse, pX, &regFree1));
drhc5499be2008-04-01 15:06:33 +00003459 testcase( regFree1==0 );
drh2dcef112008-01-12 19:03:48 +00003460 opCompare.op = TK_EQ;
drh10d1edf2013-11-15 15:52:39 +00003461 opCompare.pLeft = &tempX;
drh2dcef112008-01-12 19:03:48 +00003462 pTest = &opCompare;
drh8b1db072010-09-28 04:14:03 +00003463 /* Ticket b351d95f9cd5ef17e9d9dbae18f5ca8611190001:
3464 ** The value in regFree1 might get SCopy-ed into the file result.
3465 ** So make sure that the regFree1 register is not reused for other
3466 ** purposes and possibly overwritten. */
3467 regFree1 = 0;
drh17a7f8d2002-03-24 13:13:27 +00003468 }
drhc5cd1242013-09-12 16:50:49 +00003469 for(i=0; i<nExpr-1; i=i+2){
drhceea3322009-04-23 13:22:42 +00003470 sqlite3ExprCachePush(pParse);
drh2dcef112008-01-12 19:03:48 +00003471 if( pX ){
drh1bd10f82008-12-10 21:19:56 +00003472 assert( pTest!=0 );
drh2dcef112008-01-12 19:03:48 +00003473 opCompare.pRight = aListelem[i].pExpr;
drh17a7f8d2002-03-24 13:13:27 +00003474 }else{
drh2dcef112008-01-12 19:03:48 +00003475 pTest = aListelem[i].pExpr;
drh17a7f8d2002-03-24 13:13:27 +00003476 }
drh2dcef112008-01-12 19:03:48 +00003477 nextCase = sqlite3VdbeMakeLabel(v);
drh33cd4902009-05-30 20:49:20 +00003478 testcase( pTest->op==TK_COLUMN );
drh2dcef112008-01-12 19:03:48 +00003479 sqlite3ExprIfFalse(pParse, pTest, nextCase, SQLITE_JUMPIFNULL);
drhc5499be2008-04-01 15:06:33 +00003480 testcase( aListelem[i+1].pExpr->op==TK_COLUMN );
drh9de221d2008-01-05 06:51:30 +00003481 sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target);
drh076e85f2015-09-03 13:46:12 +00003482 sqlite3VdbeGoto(v, endLabel);
drhd2490902014-04-13 19:28:15 +00003483 sqlite3ExprCachePop(pParse);
drh2dcef112008-01-12 19:03:48 +00003484 sqlite3VdbeResolveLabel(v, nextCase);
drhf570f012002-05-31 15:51:25 +00003485 }
drhc5cd1242013-09-12 16:50:49 +00003486 if( (nExpr&1)!=0 ){
drhceea3322009-04-23 13:22:42 +00003487 sqlite3ExprCachePush(pParse);
drhc5cd1242013-09-12 16:50:49 +00003488 sqlite3ExprCode(pParse, pEList->a[nExpr-1].pExpr, target);
drhd2490902014-04-13 19:28:15 +00003489 sqlite3ExprCachePop(pParse);
drh17a7f8d2002-03-24 13:13:27 +00003490 }else{
drh9de221d2008-01-05 06:51:30 +00003491 sqlite3VdbeAddOp2(v, OP_Null, 0, target);
drh17a7f8d2002-03-24 13:13:27 +00003492 }
danielk1977c1f4a192009-04-28 12:08:15 +00003493 assert( db->mallocFailed || pParse->nErr>0
3494 || pParse->iCacheLevel==iCacheLevel );
drh2dcef112008-01-12 19:03:48 +00003495 sqlite3VdbeResolveLabel(v, endLabel);
danielk19776f349032002-06-11 02:25:40 +00003496 break;
3497 }
danielk19775338a5f2005-01-20 13:03:10 +00003498#ifndef SQLITE_OMIT_TRIGGER
danielk19776f349032002-06-11 02:25:40 +00003499 case TK_RAISE: {
dan165921a2009-08-28 18:53:45 +00003500 assert( pExpr->affinity==OE_Rollback
3501 || pExpr->affinity==OE_Abort
3502 || pExpr->affinity==OE_Fail
3503 || pExpr->affinity==OE_Ignore
3504 );
dane0af83a2009-09-08 19:15:01 +00003505 if( !pParse->pTriggerTab ){
3506 sqlite3ErrorMsg(pParse,
3507 "RAISE() may only be used within a trigger-program");
3508 return 0;
3509 }
3510 if( pExpr->affinity==OE_Abort ){
3511 sqlite3MayAbort(pParse);
3512 }
dan165921a2009-08-28 18:53:45 +00003513 assert( !ExprHasProperty(pExpr, EP_IntValue) );
dane0af83a2009-09-08 19:15:01 +00003514 if( pExpr->affinity==OE_Ignore ){
3515 sqlite3VdbeAddOp4(
3516 v, OP_Halt, SQLITE_OK, OE_Ignore, 0, pExpr->u.zToken,0);
drh688852a2014-02-17 22:40:43 +00003517 VdbeCoverage(v);
dane0af83a2009-09-08 19:15:01 +00003518 }else{
drh433dccf2013-02-09 15:37:11 +00003519 sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_TRIGGER,
drhf9c8ce32013-11-05 13:33:55 +00003520 pExpr->affinity, pExpr->u.zToken, 0, 0);
dane0af83a2009-09-08 19:15:01 +00003521 }
3522
drhffe07b22005-11-03 00:41:17 +00003523 break;
drh17a7f8d2002-03-24 13:13:27 +00003524 }
danielk19775338a5f2005-01-20 13:03:10 +00003525#endif
drhffe07b22005-11-03 00:41:17 +00003526 }
drh2dcef112008-01-12 19:03:48 +00003527 sqlite3ReleaseTempReg(pParse, regFree1);
3528 sqlite3ReleaseTempReg(pParse, regFree2);
3529 return inReg;
3530}
3531
3532/*
drhd1a01ed2013-11-21 16:08:52 +00003533** Factor out the code of the given expression to initialization time.
3534*/
drhd673cdd2013-11-21 21:23:31 +00003535void sqlite3ExprCodeAtInit(
3536 Parse *pParse, /* Parsing context */
3537 Expr *pExpr, /* The expression to code when the VDBE initializes */
3538 int regDest, /* Store the value in this register */
3539 u8 reusable /* True if this expression is reusable */
3540){
drhd1a01ed2013-11-21 16:08:52 +00003541 ExprList *p;
drhd9f158e2013-11-21 20:48:42 +00003542 assert( ConstFactorOk(pParse) );
drhd1a01ed2013-11-21 16:08:52 +00003543 p = pParse->pConstExpr;
3544 pExpr = sqlite3ExprDup(pParse->db, pExpr, 0);
3545 p = sqlite3ExprListAppend(pParse, p, pExpr);
drhd673cdd2013-11-21 21:23:31 +00003546 if( p ){
3547 struct ExprList_item *pItem = &p->a[p->nExpr-1];
3548 pItem->u.iConstExprReg = regDest;
3549 pItem->reusable = reusable;
3550 }
drhd1a01ed2013-11-21 16:08:52 +00003551 pParse->pConstExpr = p;
3552}
3553
3554/*
drh2dcef112008-01-12 19:03:48 +00003555** Generate code to evaluate an expression and store the results
3556** into a register. Return the register number where the results
3557** are stored.
3558**
3559** If the register is a temporary register that can be deallocated,
drh678ccce2008-03-31 18:19:54 +00003560** then write its number into *pReg. If the result register is not
drh2dcef112008-01-12 19:03:48 +00003561** a temporary, then set *pReg to zero.
drhf30a9692013-11-15 01:10:18 +00003562**
3563** If pExpr is a constant, then this routine might generate this
3564** code to fill the register in the initialization section of the
3565** VDBE program, in order to factor it out of the evaluation loop.
drh2dcef112008-01-12 19:03:48 +00003566*/
3567int sqlite3ExprCodeTemp(Parse *pParse, Expr *pExpr, int *pReg){
drhf30a9692013-11-15 01:10:18 +00003568 int r2;
3569 pExpr = sqlite3ExprSkipCollate(pExpr);
drhd9f158e2013-11-21 20:48:42 +00003570 if( ConstFactorOk(pParse)
drhf30a9692013-11-15 01:10:18 +00003571 && pExpr->op!=TK_REGISTER
3572 && sqlite3ExprIsConstantNotJoin(pExpr)
3573 ){
3574 ExprList *p = pParse->pConstExpr;
3575 int i;
3576 *pReg = 0;
3577 if( p ){
drhd673cdd2013-11-21 21:23:31 +00003578 struct ExprList_item *pItem;
3579 for(pItem=p->a, i=p->nExpr; i>0; pItem++, i--){
3580 if( pItem->reusable && sqlite3ExprCompare(pItem->pExpr,pExpr,-1)==0 ){
3581 return pItem->u.iConstExprReg;
drhf30a9692013-11-15 01:10:18 +00003582 }
3583 }
3584 }
drhf30a9692013-11-15 01:10:18 +00003585 r2 = ++pParse->nMem;
drhd673cdd2013-11-21 21:23:31 +00003586 sqlite3ExprCodeAtInit(pParse, pExpr, r2, 1);
drh2dcef112008-01-12 19:03:48 +00003587 }else{
drhf30a9692013-11-15 01:10:18 +00003588 int r1 = sqlite3GetTempReg(pParse);
3589 r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
3590 if( r2==r1 ){
3591 *pReg = r1;
3592 }else{
3593 sqlite3ReleaseTempReg(pParse, r1);
3594 *pReg = 0;
3595 }
drh2dcef112008-01-12 19:03:48 +00003596 }
3597 return r2;
3598}
3599
3600/*
3601** Generate code that will evaluate expression pExpr and store the
3602** results in register target. The results are guaranteed to appear
3603** in register target.
3604*/
drh05a86c52014-02-16 01:55:49 +00003605void sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){
drh9cbf3422008-01-17 16:22:13 +00003606 int inReg;
3607
3608 assert( target>0 && target<=pParse->nMem );
drhebc16712010-09-28 00:25:58 +00003609 if( pExpr && pExpr->op==TK_REGISTER ){
3610 sqlite3VdbeAddOp2(pParse->pVdbe, OP_Copy, pExpr->iTable, target);
3611 }else{
3612 inReg = sqlite3ExprCodeTarget(pParse, pExpr, target);
drh1c75c9d2015-12-21 15:22:13 +00003613 assert( pParse->pVdbe!=0 || pParse->db->mallocFailed );
drhebc16712010-09-28 00:25:58 +00003614 if( inReg!=target && pParse->pVdbe ){
3615 sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, inReg, target);
3616 }
drhcce7d172000-05-31 15:34:51 +00003617 }
drhcce7d172000-05-31 15:34:51 +00003618}
3619
3620/*
drh1c75c9d2015-12-21 15:22:13 +00003621** Make a transient copy of expression pExpr and then code it using
3622** sqlite3ExprCode(). This routine works just like sqlite3ExprCode()
3623** except that the input expression is guaranteed to be unchanged.
3624*/
3625void sqlite3ExprCodeCopy(Parse *pParse, Expr *pExpr, int target){
3626 sqlite3 *db = pParse->db;
3627 pExpr = sqlite3ExprDup(db, pExpr, 0);
3628 if( !db->mallocFailed ) sqlite3ExprCode(pParse, pExpr, target);
3629 sqlite3ExprDelete(db, pExpr);
3630}
3631
3632/*
drh05a86c52014-02-16 01:55:49 +00003633** Generate code that will evaluate expression pExpr and store the
3634** results in register target. The results are guaranteed to appear
3635** in register target. If the expression is constant, then this routine
3636** might choose to code the expression at initialization time.
3637*/
3638void sqlite3ExprCodeFactorable(Parse *pParse, Expr *pExpr, int target){
3639 if( pParse->okConstFactor && sqlite3ExprIsConstant(pExpr) ){
3640 sqlite3ExprCodeAtInit(pParse, pExpr, target, 0);
3641 }else{
3642 sqlite3ExprCode(pParse, pExpr, target);
3643 }
drhcce7d172000-05-31 15:34:51 +00003644}
3645
3646/*
peter.d.reid60ec9142014-09-06 16:39:46 +00003647** Generate code that evaluates the given expression and puts the result
drhde4fcfd2008-01-19 23:50:26 +00003648** in register target.
drh25303782004-12-07 15:41:48 +00003649**
drh2dcef112008-01-12 19:03:48 +00003650** Also make a copy of the expression results into another "cache" register
3651** and modify the expression so that the next time it is evaluated,
3652** the result is a copy of the cache register.
3653**
3654** This routine is used for expressions that are used multiple
3655** times. They are evaluated once and the results of the expression
3656** are reused.
drh25303782004-12-07 15:41:48 +00003657*/
drh05a86c52014-02-16 01:55:49 +00003658void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr, int target){
drh25303782004-12-07 15:41:48 +00003659 Vdbe *v = pParse->pVdbe;
drh05a86c52014-02-16 01:55:49 +00003660 int iMem;
3661
drhde4fcfd2008-01-19 23:50:26 +00003662 assert( target>0 );
drh05a86c52014-02-16 01:55:49 +00003663 assert( pExpr->op!=TK_REGISTER );
3664 sqlite3ExprCode(pParse, pExpr, target);
3665 iMem = ++pParse->nMem;
3666 sqlite3VdbeAddOp2(v, OP_Copy, target, iMem);
3667 exprToRegister(pExpr, iMem);
drh25303782004-12-07 15:41:48 +00003668}
drh2dcef112008-01-12 19:03:48 +00003669
drh678ccce2008-03-31 18:19:54 +00003670/*
drh268380c2004-02-25 13:47:31 +00003671** Generate code that pushes the value of every element of the given
drh9cbf3422008-01-17 16:22:13 +00003672** expression list into a sequence of registers beginning at target.
drh268380c2004-02-25 13:47:31 +00003673**
drh892d3172008-01-10 03:46:36 +00003674** Return the number of elements evaluated.
drhd1a01ed2013-11-21 16:08:52 +00003675**
3676** The SQLITE_ECEL_DUP flag prevents the arguments from being
3677** filled using OP_SCopy. OP_Copy must be used instead.
3678**
3679** The SQLITE_ECEL_FACTOR argument allows constant arguments to be
3680** factored out into initialization code.
drhb0df9632015-10-16 23:55:08 +00003681**
3682** The SQLITE_ECEL_REF flag means that expressions in the list with
3683** ExprList.a[].u.x.iOrderByCol>0 have already been evaluated and stored
3684** in registers at srcReg, and so the value can be copied from there.
drh268380c2004-02-25 13:47:31 +00003685*/
danielk19774adee202004-05-08 08:23:19 +00003686int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00003687 Parse *pParse, /* Parsing context */
drh389a1ad2008-01-03 23:44:53 +00003688 ExprList *pList, /* The expression list to be coded */
drh191b54c2008-04-15 12:14:21 +00003689 int target, /* Where to write results */
drh5579d592015-08-26 14:01:41 +00003690 int srcReg, /* Source registers if SQLITE_ECEL_REF */
drhd1a01ed2013-11-21 16:08:52 +00003691 u8 flags /* SQLITE_ECEL_* flags */
drh268380c2004-02-25 13:47:31 +00003692){
3693 struct ExprList_item *pItem;
drh5579d592015-08-26 14:01:41 +00003694 int i, j, n;
drhd1a01ed2013-11-21 16:08:52 +00003695 u8 copyOp = (flags & SQLITE_ECEL_DUP) ? OP_Copy : OP_SCopy;
drh5579d592015-08-26 14:01:41 +00003696 Vdbe *v = pParse->pVdbe;
drh9d8b3072008-08-22 16:29:51 +00003697 assert( pList!=0 );
drh9cbf3422008-01-17 16:22:13 +00003698 assert( target>0 );
drhd81a1422010-09-28 07:11:24 +00003699 assert( pParse->pVdbe!=0 ); /* Never gets this far otherwise */
drh268380c2004-02-25 13:47:31 +00003700 n = pList->nExpr;
drhd9f158e2013-11-21 20:48:42 +00003701 if( !ConstFactorOk(pParse) ) flags &= ~SQLITE_ECEL_FACTOR;
drh191b54c2008-04-15 12:14:21 +00003702 for(pItem=pList->a, i=0; i<n; i++, pItem++){
drh7445ffe2010-09-27 18:14:12 +00003703 Expr *pExpr = pItem->pExpr;
drh5579d592015-08-26 14:01:41 +00003704 if( (flags & SQLITE_ECEL_REF)!=0 && (j = pList->a[i].u.x.iOrderByCol)>0 ){
3705 sqlite3VdbeAddOp2(v, copyOp, j+srcReg-1, target+i);
3706 }else if( (flags & SQLITE_ECEL_FACTOR)!=0 && sqlite3ExprIsConstant(pExpr) ){
drhd673cdd2013-11-21 21:23:31 +00003707 sqlite3ExprCodeAtInit(pParse, pExpr, target+i, 0);
drhd1a01ed2013-11-21 16:08:52 +00003708 }else{
3709 int inReg = sqlite3ExprCodeTarget(pParse, pExpr, target+i);
3710 if( inReg!=target+i ){
drh4eded602013-12-20 15:59:20 +00003711 VdbeOp *pOp;
drh4eded602013-12-20 15:59:20 +00003712 if( copyOp==OP_Copy
3713 && (pOp=sqlite3VdbeGetOp(v, -1))->opcode==OP_Copy
3714 && pOp->p1+pOp->p3+1==inReg
3715 && pOp->p2+pOp->p3+1==target+i
3716 ){
3717 pOp->p3++;
3718 }else{
3719 sqlite3VdbeAddOp2(v, copyOp, inReg, target+i);
3720 }
drhd1a01ed2013-11-21 16:08:52 +00003721 }
drhd1766112008-09-17 00:13:12 +00003722 }
drh268380c2004-02-25 13:47:31 +00003723 }
drhf9b596e2004-05-26 16:54:42 +00003724 return n;
drh268380c2004-02-25 13:47:31 +00003725}
3726
3727/*
drh36c563a2009-11-12 13:32:22 +00003728** Generate code for a BETWEEN operator.
3729**
3730** x BETWEEN y AND z
3731**
3732** The above is equivalent to
3733**
3734** x>=y AND x<=z
3735**
3736** Code it as such, taking care to do the common subexpression
peter.d.reid60ec9142014-09-06 16:39:46 +00003737** elimination of x.
drh36c563a2009-11-12 13:32:22 +00003738*/
3739static void exprCodeBetween(
3740 Parse *pParse, /* Parsing and code generating context */
3741 Expr *pExpr, /* The BETWEEN expression */
3742 int dest, /* Jump here if the jump is taken */
dan71c57db2016-07-09 20:23:55 +00003743 void (*xJumpIf)(Parse*,Expr*,int,int),
drh36c563a2009-11-12 13:32:22 +00003744 int jumpIfNull /* Take the jump if the BETWEEN is NULL */
3745){
3746 Expr exprAnd; /* The AND operator in x>=y AND x<=z */
3747 Expr compLeft; /* The x>=y term */
3748 Expr compRight; /* The x<=z term */
3749 Expr exprX; /* The x subexpression */
3750 int regFree1 = 0; /* Temporary use register */
3751
dan71c57db2016-07-09 20:23:55 +00003752 memset(&compLeft, 0, sizeof(Expr));
3753 memset(&compRight, 0, sizeof(Expr));
3754 memset(&exprAnd, 0, sizeof(Expr));
3755
drh36c563a2009-11-12 13:32:22 +00003756 assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
3757 exprX = *pExpr->pLeft;
3758 exprAnd.op = TK_AND;
3759 exprAnd.pLeft = &compLeft;
3760 exprAnd.pRight = &compRight;
3761 compLeft.op = TK_GE;
3762 compLeft.pLeft = &exprX;
3763 compLeft.pRight = pExpr->x.pList->a[0].pExpr;
3764 compRight.op = TK_LE;
3765 compRight.pLeft = &exprX;
3766 compRight.pRight = pExpr->x.pList->a[1].pExpr;
dan71c57db2016-07-09 20:23:55 +00003767 if( (exprX.flags & EP_Vector)==0 ){
3768 exprToRegister(&exprX, sqlite3ExprCodeTemp(pParse, &exprX, &regFree1));
3769 }
3770 if( xJumpIf ){
3771 xJumpIf(pParse, &exprAnd, dest, jumpIfNull);
drh36c563a2009-11-12 13:32:22 +00003772 }else{
dan71c57db2016-07-09 20:23:55 +00003773 exprX.flags |= EP_FromJoin;
3774 sqlite3ExprCodeTarget(pParse, &exprAnd, dest);
drh36c563a2009-11-12 13:32:22 +00003775 }
3776 sqlite3ReleaseTempReg(pParse, regFree1);
3777
3778 /* Ensure adequate test coverage */
3779 testcase( jumpIfTrue==0 && jumpIfNull==0 && regFree1==0 );
3780 testcase( jumpIfTrue==0 && jumpIfNull==0 && regFree1!=0 );
3781 testcase( jumpIfTrue==0 && jumpIfNull!=0 && regFree1==0 );
3782 testcase( jumpIfTrue==0 && jumpIfNull!=0 && regFree1!=0 );
3783 testcase( jumpIfTrue!=0 && jumpIfNull==0 && regFree1==0 );
3784 testcase( jumpIfTrue!=0 && jumpIfNull==0 && regFree1!=0 );
3785 testcase( jumpIfTrue!=0 && jumpIfNull!=0 && regFree1==0 );
3786 testcase( jumpIfTrue!=0 && jumpIfNull!=0 && regFree1!=0 );
3787}
3788
3789/*
drhcce7d172000-05-31 15:34:51 +00003790** Generate code for a boolean expression such that a jump is made
3791** to the label "dest" if the expression is true but execution
3792** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00003793**
3794** If the expression evaluates to NULL (neither true nor false), then
drh35573352008-01-08 23:54:25 +00003795** take the jump if the jumpIfNull flag is SQLITE_JUMPIFNULL.
drhf2bc0132004-10-04 13:19:23 +00003796**
3797** This code depends on the fact that certain token values (ex: TK_EQ)
3798** are the same as opcode values (ex: OP_Eq) that implement the corresponding
3799** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
3800** the make process cause these values to align. Assert()s in the code
3801** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00003802*/
danielk19774adee202004-05-08 08:23:19 +00003803void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00003804 Vdbe *v = pParse->pVdbe;
3805 int op = 0;
drh2dcef112008-01-12 19:03:48 +00003806 int regFree1 = 0;
3807 int regFree2 = 0;
3808 int r1, r2;
3809
drh35573352008-01-08 23:54:25 +00003810 assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
mistachkin48864df2013-03-21 21:20:32 +00003811 if( NEVER(v==0) ) return; /* Existence of VDBE checked by caller */
drh33cd4902009-05-30 20:49:20 +00003812 if( NEVER(pExpr==0) ) return; /* No way this can happen */
drhf2bc0132004-10-04 13:19:23 +00003813 op = pExpr->op;
dan71c57db2016-07-09 20:23:55 +00003814 switch( op | (pExpr->pLeft ? (pExpr->pLeft->flags & EP_Vector) : 0)){
drhcce7d172000-05-31 15:34:51 +00003815 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00003816 int d2 = sqlite3VdbeMakeLabel(v);
drhc5499be2008-04-01 15:06:33 +00003817 testcase( jumpIfNull==0 );
drh35573352008-01-08 23:54:25 +00003818 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,jumpIfNull^SQLITE_JUMPIFNULL);
drh54e2adb2014-01-04 15:17:04 +00003819 sqlite3ExprCachePush(pParse);
danielk19774adee202004-05-08 08:23:19 +00003820 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
3821 sqlite3VdbeResolveLabel(v, d2);
drhd2490902014-04-13 19:28:15 +00003822 sqlite3ExprCachePop(pParse);
drhcce7d172000-05-31 15:34:51 +00003823 break;
3824 }
3825 case TK_OR: {
drhc5499be2008-04-01 15:06:33 +00003826 testcase( jumpIfNull==0 );
danielk19774adee202004-05-08 08:23:19 +00003827 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drh54e2adb2014-01-04 15:17:04 +00003828 sqlite3ExprCachePush(pParse);
danielk19774adee202004-05-08 08:23:19 +00003829 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhd2490902014-04-13 19:28:15 +00003830 sqlite3ExprCachePop(pParse);
drhcce7d172000-05-31 15:34:51 +00003831 break;
3832 }
3833 case TK_NOT: {
drhc5499be2008-04-01 15:06:33 +00003834 testcase( jumpIfNull==0 );
danielk19774adee202004-05-08 08:23:19 +00003835 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00003836 break;
3837 }
drhde845c22016-03-17 19:07:52 +00003838 case TK_IS:
3839 case TK_ISNOT:
3840 testcase( op==TK_IS );
3841 testcase( op==TK_ISNOT );
3842 op = (op==TK_IS) ? TK_EQ : TK_NE;
3843 jumpIfNull = SQLITE_NULLEQ;
3844 /* Fall thru */
drhcce7d172000-05-31 15:34:51 +00003845 case TK_LT:
3846 case TK_LE:
3847 case TK_GT:
3848 case TK_GE:
3849 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00003850 case TK_EQ: {
drhc5499be2008-04-01 15:06:33 +00003851 testcase( jumpIfNull==0 );
drhb6da74e2009-12-24 16:00:28 +00003852 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
3853 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
drh35573352008-01-08 23:54:25 +00003854 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
drh2dcef112008-01-12 19:03:48 +00003855 r1, r2, dest, jumpIfNull);
drh7d176102014-02-18 03:07:12 +00003856 assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
3857 assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
3858 assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
3859 assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
drhde845c22016-03-17 19:07:52 +00003860 assert(TK_EQ==OP_Eq); testcase(op==OP_Eq);
3861 VdbeCoverageIf(v, op==OP_Eq && jumpIfNull==SQLITE_NULLEQ);
3862 VdbeCoverageIf(v, op==OP_Eq && jumpIfNull!=SQLITE_NULLEQ);
3863 assert(TK_NE==OP_Ne); testcase(op==OP_Ne);
3864 VdbeCoverageIf(v, op==OP_Ne && jumpIfNull==SQLITE_NULLEQ);
3865 VdbeCoverageIf(v, op==OP_Ne && jumpIfNull!=SQLITE_NULLEQ);
drh6a2fe092009-09-23 02:29:36 +00003866 testcase( regFree1==0 );
3867 testcase( regFree2==0 );
3868 break;
3869 }
drhcce7d172000-05-31 15:34:51 +00003870 case TK_ISNULL:
3871 case TK_NOTNULL: {
drh7d176102014-02-18 03:07:12 +00003872 assert( TK_ISNULL==OP_IsNull ); testcase( op==TK_ISNULL );
3873 assert( TK_NOTNULL==OP_NotNull ); testcase( op==TK_NOTNULL );
drh2dcef112008-01-12 19:03:48 +00003874 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
3875 sqlite3VdbeAddOp2(v, op, r1, dest);
drh7d176102014-02-18 03:07:12 +00003876 VdbeCoverageIf(v, op==TK_ISNULL);
3877 VdbeCoverageIf(v, op==TK_NOTNULL);
drhc5499be2008-04-01 15:06:33 +00003878 testcase( regFree1==0 );
drhcce7d172000-05-31 15:34:51 +00003879 break;
3880 }
drhfef52082000-06-06 01:50:43 +00003881 case TK_BETWEEN: {
drh5c03f302009-11-13 15:03:59 +00003882 testcase( jumpIfNull==0 );
dan71c57db2016-07-09 20:23:55 +00003883 exprCodeBetween(pParse, pExpr, dest, sqlite3ExprIfTrue, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00003884 break;
3885 }
drh84e30ca2011-02-10 17:46:14 +00003886#ifndef SQLITE_OMIT_SUBQUERY
drhe3365e62009-11-12 17:52:24 +00003887 case TK_IN: {
3888 int destIfFalse = sqlite3VdbeMakeLabel(v);
3889 int destIfNull = jumpIfNull ? dest : destIfFalse;
3890 sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull);
drh076e85f2015-09-03 13:46:12 +00003891 sqlite3VdbeGoto(v, dest);
drhe3365e62009-11-12 17:52:24 +00003892 sqlite3VdbeResolveLabel(v, destIfFalse);
3893 break;
3894 }
shanehbb201342011-02-09 19:55:20 +00003895#endif
drhcce7d172000-05-31 15:34:51 +00003896 default: {
drh991a1982014-01-02 17:57:16 +00003897 if( exprAlwaysTrue(pExpr) ){
drh076e85f2015-09-03 13:46:12 +00003898 sqlite3VdbeGoto(v, dest);
drh991a1982014-01-02 17:57:16 +00003899 }else if( exprAlwaysFalse(pExpr) ){
3900 /* No-op */
3901 }else{
3902 r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
3903 sqlite3VdbeAddOp3(v, OP_If, r1, dest, jumpIfNull!=0);
drh688852a2014-02-17 22:40:43 +00003904 VdbeCoverage(v);
drh991a1982014-01-02 17:57:16 +00003905 testcase( regFree1==0 );
3906 testcase( jumpIfNull==0 );
3907 }
drhcce7d172000-05-31 15:34:51 +00003908 break;
3909 }
3910 }
drh2dcef112008-01-12 19:03:48 +00003911 sqlite3ReleaseTempReg(pParse, regFree1);
3912 sqlite3ReleaseTempReg(pParse, regFree2);
drhcce7d172000-05-31 15:34:51 +00003913}
3914
3915/*
drh66b89c82000-11-28 20:47:17 +00003916** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00003917** to the label "dest" if the expression is false but execution
3918** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00003919**
3920** If the expression evaluates to NULL (neither true nor false) then
drh35573352008-01-08 23:54:25 +00003921** jump if jumpIfNull is SQLITE_JUMPIFNULL or fall through if jumpIfNull
3922** is 0.
drhcce7d172000-05-31 15:34:51 +00003923*/
danielk19774adee202004-05-08 08:23:19 +00003924void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00003925 Vdbe *v = pParse->pVdbe;
3926 int op = 0;
drh2dcef112008-01-12 19:03:48 +00003927 int regFree1 = 0;
3928 int regFree2 = 0;
3929 int r1, r2;
3930
drh35573352008-01-08 23:54:25 +00003931 assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
mistachkin48864df2013-03-21 21:20:32 +00003932 if( NEVER(v==0) ) return; /* Existence of VDBE checked by caller */
drh33cd4902009-05-30 20:49:20 +00003933 if( pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00003934
3935 /* The value of pExpr->op and op are related as follows:
3936 **
3937 ** pExpr->op op
3938 ** --------- ----------
3939 ** TK_ISNULL OP_NotNull
3940 ** TK_NOTNULL OP_IsNull
3941 ** TK_NE OP_Eq
3942 ** TK_EQ OP_Ne
3943 ** TK_GT OP_Le
3944 ** TK_LE OP_Gt
3945 ** TK_GE OP_Lt
3946 ** TK_LT OP_Ge
3947 **
3948 ** For other values of pExpr->op, op is undefined and unused.
3949 ** The value of TK_ and OP_ constants are arranged such that we
3950 ** can compute the mapping above using the following expression.
3951 ** Assert()s verify that the computation is correct.
3952 */
3953 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
3954
3955 /* Verify correct alignment of TK_ and OP_ constants
3956 */
3957 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
3958 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
3959 assert( pExpr->op!=TK_NE || op==OP_Eq );
3960 assert( pExpr->op!=TK_EQ || op==OP_Ne );
3961 assert( pExpr->op!=TK_LT || op==OP_Ge );
3962 assert( pExpr->op!=TK_LE || op==OP_Gt );
3963 assert( pExpr->op!=TK_GT || op==OP_Le );
3964 assert( pExpr->op!=TK_GE || op==OP_Lt );
3965
dan71c57db2016-07-09 20:23:55 +00003966 switch( pExpr->op | (pExpr->pLeft ? (pExpr->pLeft->flags & EP_Vector) : 0)){
drhcce7d172000-05-31 15:34:51 +00003967 case TK_AND: {
drhc5499be2008-04-01 15:06:33 +00003968 testcase( jumpIfNull==0 );
danielk19774adee202004-05-08 08:23:19 +00003969 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drh54e2adb2014-01-04 15:17:04 +00003970 sqlite3ExprCachePush(pParse);
danielk19774adee202004-05-08 08:23:19 +00003971 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhd2490902014-04-13 19:28:15 +00003972 sqlite3ExprCachePop(pParse);
drhcce7d172000-05-31 15:34:51 +00003973 break;
3974 }
3975 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00003976 int d2 = sqlite3VdbeMakeLabel(v);
drhc5499be2008-04-01 15:06:33 +00003977 testcase( jumpIfNull==0 );
drh35573352008-01-08 23:54:25 +00003978 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, jumpIfNull^SQLITE_JUMPIFNULL);
drh54e2adb2014-01-04 15:17:04 +00003979 sqlite3ExprCachePush(pParse);
danielk19774adee202004-05-08 08:23:19 +00003980 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
3981 sqlite3VdbeResolveLabel(v, d2);
drhd2490902014-04-13 19:28:15 +00003982 sqlite3ExprCachePop(pParse);
drhcce7d172000-05-31 15:34:51 +00003983 break;
3984 }
3985 case TK_NOT: {
drh5c03f302009-11-13 15:03:59 +00003986 testcase( jumpIfNull==0 );
danielk19774adee202004-05-08 08:23:19 +00003987 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00003988 break;
3989 }
drhde845c22016-03-17 19:07:52 +00003990 case TK_IS:
3991 case TK_ISNOT:
3992 testcase( pExpr->op==TK_IS );
3993 testcase( pExpr->op==TK_ISNOT );
3994 op = (pExpr->op==TK_IS) ? TK_NE : TK_EQ;
3995 jumpIfNull = SQLITE_NULLEQ;
3996 /* Fall thru */
drhcce7d172000-05-31 15:34:51 +00003997 case TK_LT:
3998 case TK_LE:
3999 case TK_GT:
4000 case TK_GE:
4001 case TK_NE:
4002 case TK_EQ: {
drhc5499be2008-04-01 15:06:33 +00004003 testcase( jumpIfNull==0 );
drhb6da74e2009-12-24 16:00:28 +00004004 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
4005 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
drh35573352008-01-08 23:54:25 +00004006 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
drh2dcef112008-01-12 19:03:48 +00004007 r1, r2, dest, jumpIfNull);
drh7d176102014-02-18 03:07:12 +00004008 assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
4009 assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
4010 assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
4011 assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
drhde845c22016-03-17 19:07:52 +00004012 assert(TK_EQ==OP_Eq); testcase(op==OP_Eq);
4013 VdbeCoverageIf(v, op==OP_Eq && jumpIfNull!=SQLITE_NULLEQ);
4014 VdbeCoverageIf(v, op==OP_Eq && jumpIfNull==SQLITE_NULLEQ);
4015 assert(TK_NE==OP_Ne); testcase(op==OP_Ne);
4016 VdbeCoverageIf(v, op==OP_Ne && jumpIfNull!=SQLITE_NULLEQ);
4017 VdbeCoverageIf(v, op==OP_Ne && jumpIfNull==SQLITE_NULLEQ);
drh6a2fe092009-09-23 02:29:36 +00004018 testcase( regFree1==0 );
4019 testcase( regFree2==0 );
4020 break;
4021 }
drhcce7d172000-05-31 15:34:51 +00004022 case TK_ISNULL:
4023 case TK_NOTNULL: {
drh2dcef112008-01-12 19:03:48 +00004024 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
4025 sqlite3VdbeAddOp2(v, op, r1, dest);
drh7d176102014-02-18 03:07:12 +00004026 testcase( op==TK_ISNULL ); VdbeCoverageIf(v, op==TK_ISNULL);
4027 testcase( op==TK_NOTNULL ); VdbeCoverageIf(v, op==TK_NOTNULL);
drhc5499be2008-04-01 15:06:33 +00004028 testcase( regFree1==0 );
drhcce7d172000-05-31 15:34:51 +00004029 break;
4030 }
drhfef52082000-06-06 01:50:43 +00004031 case TK_BETWEEN: {
drh5c03f302009-11-13 15:03:59 +00004032 testcase( jumpIfNull==0 );
dan71c57db2016-07-09 20:23:55 +00004033 exprCodeBetween(pParse, pExpr, dest, sqlite3ExprIfFalse, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00004034 break;
4035 }
drh84e30ca2011-02-10 17:46:14 +00004036#ifndef SQLITE_OMIT_SUBQUERY
drhe3365e62009-11-12 17:52:24 +00004037 case TK_IN: {
4038 if( jumpIfNull ){
4039 sqlite3ExprCodeIN(pParse, pExpr, dest, dest);
4040 }else{
4041 int destIfNull = sqlite3VdbeMakeLabel(v);
4042 sqlite3ExprCodeIN(pParse, pExpr, dest, destIfNull);
4043 sqlite3VdbeResolveLabel(v, destIfNull);
4044 }
4045 break;
4046 }
shanehbb201342011-02-09 19:55:20 +00004047#endif
drhcce7d172000-05-31 15:34:51 +00004048 default: {
drh991a1982014-01-02 17:57:16 +00004049 if( exprAlwaysFalse(pExpr) ){
drh076e85f2015-09-03 13:46:12 +00004050 sqlite3VdbeGoto(v, dest);
drh991a1982014-01-02 17:57:16 +00004051 }else if( exprAlwaysTrue(pExpr) ){
4052 /* no-op */
4053 }else{
4054 r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
4055 sqlite3VdbeAddOp3(v, OP_IfNot, r1, dest, jumpIfNull!=0);
drh688852a2014-02-17 22:40:43 +00004056 VdbeCoverage(v);
drh991a1982014-01-02 17:57:16 +00004057 testcase( regFree1==0 );
4058 testcase( jumpIfNull==0 );
4059 }
drhcce7d172000-05-31 15:34:51 +00004060 break;
4061 }
4062 }
drh2dcef112008-01-12 19:03:48 +00004063 sqlite3ReleaseTempReg(pParse, regFree1);
4064 sqlite3ReleaseTempReg(pParse, regFree2);
drhcce7d172000-05-31 15:34:51 +00004065}
drh22827922000-06-06 17:27:05 +00004066
4067/*
drh72bc8202015-06-11 13:58:35 +00004068** Like sqlite3ExprIfFalse() except that a copy is made of pExpr before
4069** code generation, and that copy is deleted after code generation. This
4070** ensures that the original pExpr is unchanged.
4071*/
4072void sqlite3ExprIfFalseDup(Parse *pParse, Expr *pExpr, int dest,int jumpIfNull){
4073 sqlite3 *db = pParse->db;
4074 Expr *pCopy = sqlite3ExprDup(db, pExpr, 0);
4075 if( db->mallocFailed==0 ){
4076 sqlite3ExprIfFalse(pParse, pCopy, dest, jumpIfNull);
4077 }
4078 sqlite3ExprDelete(db, pCopy);
4079}
4080
4081
4082/*
drh1d9da702010-01-07 15:17:02 +00004083** Do a deep comparison of two expression trees. Return 0 if the two
4084** expressions are completely identical. Return 1 if they differ only
4085** by a COLLATE operator at the top level. Return 2 if there are differences
4086** other than the top-level COLLATE operator.
drhd40aab02007-02-24 15:29:03 +00004087**
drh619a1302013-08-01 13:04:46 +00004088** If any subelement of pB has Expr.iTable==(-1) then it is allowed
4089** to compare equal to an equivalent element in pA with Expr.iTable==iTab.
4090**
drh66518ca2013-08-01 15:09:57 +00004091** The pA side might be using TK_REGISTER. If that is the case and pB is
4092** not using TK_REGISTER but is otherwise equivalent, then still return 0.
4093**
drh1d9da702010-01-07 15:17:02 +00004094** Sometimes this routine will return 2 even if the two expressions
drhd40aab02007-02-24 15:29:03 +00004095** really are equivalent. If we cannot prove that the expressions are
drh1d9da702010-01-07 15:17:02 +00004096** identical, we return 2 just to be safe. So if this routine
4097** returns 2, then you do not really know for certain if the two
4098** expressions are the same. But if you get a 0 or 1 return, then you
drhd40aab02007-02-24 15:29:03 +00004099** can be sure the expressions are the same. In the places where
drh1d9da702010-01-07 15:17:02 +00004100** this routine is used, it does not hurt to get an extra 2 - that
drhd40aab02007-02-24 15:29:03 +00004101** just might result in some slightly slower code. But returning
drh1d9da702010-01-07 15:17:02 +00004102** an incorrect 0 or 1 could lead to a malfunction.
drh22827922000-06-06 17:27:05 +00004103*/
drh619a1302013-08-01 13:04:46 +00004104int sqlite3ExprCompare(Expr *pA, Expr *pB, int iTab){
drh10d1edf2013-11-15 15:52:39 +00004105 u32 combinedFlags;
4106 if( pA==0 || pB==0 ){
drh1d9da702010-01-07 15:17:02 +00004107 return pB==pA ? 0 : 2;
drh22827922000-06-06 17:27:05 +00004108 }
drh10d1edf2013-11-15 15:52:39 +00004109 combinedFlags = pA->flags | pB->flags;
4110 if( combinedFlags & EP_IntValue ){
4111 if( (pA->flags&pB->flags&EP_IntValue)!=0 && pA->u.iValue==pB->u.iValue ){
4112 return 0;
4113 }
drh1d9da702010-01-07 15:17:02 +00004114 return 2;
drh22827922000-06-06 17:27:05 +00004115 }
drhc2acc4e2013-11-15 18:15:19 +00004116 if( pA->op!=pB->op ){
drh619a1302013-08-01 13:04:46 +00004117 if( pA->op==TK_COLLATE && sqlite3ExprCompare(pA->pLeft, pB, iTab)<2 ){
drhae80dde2012-12-06 21:16:43 +00004118 return 1;
4119 }
drh619a1302013-08-01 13:04:46 +00004120 if( pB->op==TK_COLLATE && sqlite3ExprCompare(pA, pB->pLeft, iTab)<2 ){
drhae80dde2012-12-06 21:16:43 +00004121 return 1;
4122 }
4123 return 2;
4124 }
drh2edc5fd2015-11-24 02:10:52 +00004125 if( pA->op!=TK_COLUMN && pA->op!=TK_AGG_COLUMN && pA->u.zToken ){
drh390b88a2015-08-31 18:13:01 +00004126 if( pA->op==TK_FUNCTION ){
4127 if( sqlite3StrICmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2;
4128 }else if( strcmp(pA->u.zToken,pB->u.zToken)!=0 ){
drhae80dde2012-12-06 21:16:43 +00004129 return pA->op==TK_COLLATE ? 1 : 2;
drh2646da72005-12-09 20:02:05 +00004130 }
drh22827922000-06-06 17:27:05 +00004131 }
drh10d1edf2013-11-15 15:52:39 +00004132 if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 2;
drh85f8aa72013-11-15 20:06:26 +00004133 if( ALWAYS((combinedFlags & EP_TokenOnly)==0) ){
drh10d1edf2013-11-15 15:52:39 +00004134 if( combinedFlags & EP_xIsSelect ) return 2;
4135 if( sqlite3ExprCompare(pA->pLeft, pB->pLeft, iTab) ) return 2;
4136 if( sqlite3ExprCompare(pA->pRight, pB->pRight, iTab) ) return 2;
4137 if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList, iTab) ) return 2;
drh7693c422015-04-17 19:41:37 +00004138 if( ALWAYS((combinedFlags & EP_Reduced)==0) && pA->op!=TK_STRING ){
drh10d1edf2013-11-15 15:52:39 +00004139 if( pA->iColumn!=pB->iColumn ) return 2;
4140 if( pA->iTable!=pB->iTable
drh85f8aa72013-11-15 20:06:26 +00004141 && (pA->iTable!=iTab || NEVER(pB->iTable>=0)) ) return 2;
drh10d1edf2013-11-15 15:52:39 +00004142 }
4143 }
drh1d9da702010-01-07 15:17:02 +00004144 return 0;
drh22827922000-06-06 17:27:05 +00004145}
4146
drh8c6f6662010-04-26 19:17:26 +00004147/*
4148** Compare two ExprList objects. Return 0 if they are identical and
4149** non-zero if they differ in any way.
4150**
drh619a1302013-08-01 13:04:46 +00004151** If any subelement of pB has Expr.iTable==(-1) then it is allowed
4152** to compare equal to an equivalent element in pA with Expr.iTable==iTab.
4153**
drh8c6f6662010-04-26 19:17:26 +00004154** This routine might return non-zero for equivalent ExprLists. The
4155** only consequence will be disabled optimizations. But this routine
4156** must never return 0 if the two ExprList objects are different, or
4157** a malfunction will result.
4158**
4159** Two NULL pointers are considered to be the same. But a NULL pointer
4160** always differs from a non-NULL pointer.
4161*/
drh619a1302013-08-01 13:04:46 +00004162int sqlite3ExprListCompare(ExprList *pA, ExprList *pB, int iTab){
drh8c6f6662010-04-26 19:17:26 +00004163 int i;
4164 if( pA==0 && pB==0 ) return 0;
4165 if( pA==0 || pB==0 ) return 1;
4166 if( pA->nExpr!=pB->nExpr ) return 1;
4167 for(i=0; i<pA->nExpr; i++){
4168 Expr *pExprA = pA->a[i].pExpr;
4169 Expr *pExprB = pB->a[i].pExpr;
4170 if( pA->a[i].sortOrder!=pB->a[i].sortOrder ) return 1;
drh619a1302013-08-01 13:04:46 +00004171 if( sqlite3ExprCompare(pExprA, pExprB, iTab) ) return 1;
drh8c6f6662010-04-26 19:17:26 +00004172 }
4173 return 0;
4174}
drh13449892005-09-07 21:22:45 +00004175
drh22827922000-06-06 17:27:05 +00004176/*
drh4bd5f732013-07-31 23:22:39 +00004177** Return true if we can prove the pE2 will always be true if pE1 is
4178** true. Return false if we cannot complete the proof or if pE2 might
4179** be false. Examples:
4180**
drh619a1302013-08-01 13:04:46 +00004181** pE1: x==5 pE2: x==5 Result: true
4182** pE1: x>0 pE2: x==5 Result: false
4183** pE1: x=21 pE2: x=21 OR y=43 Result: true
4184** pE1: x!=123 pE2: x IS NOT NULL Result: true
4185** pE1: x!=?1 pE2: x IS NOT NULL Result: true
4186** pE1: x IS NULL pE2: x IS NOT NULL Result: false
4187** pE1: x IS ?2 pE2: x IS NOT NULL Reuslt: false
drh4bd5f732013-07-31 23:22:39 +00004188**
4189** When comparing TK_COLUMN nodes between pE1 and pE2, if pE2 has
4190** Expr.iTable<0 then assume a table number given by iTab.
4191**
4192** When in doubt, return false. Returning true might give a performance
4193** improvement. Returning false might cause a performance reduction, but
4194** it will always give the correct answer and is hence always safe.
4195*/
4196int sqlite3ExprImpliesExpr(Expr *pE1, Expr *pE2, int iTab){
drh619a1302013-08-01 13:04:46 +00004197 if( sqlite3ExprCompare(pE1, pE2, iTab)==0 ){
4198 return 1;
4199 }
4200 if( pE2->op==TK_OR
4201 && (sqlite3ExprImpliesExpr(pE1, pE2->pLeft, iTab)
4202 || sqlite3ExprImpliesExpr(pE1, pE2->pRight, iTab) )
4203 ){
4204 return 1;
4205 }
4206 if( pE2->op==TK_NOTNULL
4207 && sqlite3ExprCompare(pE1->pLeft, pE2->pLeft, iTab)==0
4208 && (pE1->op!=TK_ISNULL && pE1->op!=TK_IS)
4209 ){
4210 return 1;
4211 }
4212 return 0;
drh4bd5f732013-07-31 23:22:39 +00004213}
4214
4215/*
drh030796d2012-08-23 16:18:10 +00004216** An instance of the following structure is used by the tree walker
4217** to count references to table columns in the arguments of an
drhed551b92012-08-23 19:46:11 +00004218** aggregate function, in order to implement the
4219** sqlite3FunctionThisSrc() routine.
drh374fdce2012-04-17 16:38:53 +00004220*/
drh030796d2012-08-23 16:18:10 +00004221struct SrcCount {
4222 SrcList *pSrc; /* One particular FROM clause in a nested query */
4223 int nThis; /* Number of references to columns in pSrcList */
4224 int nOther; /* Number of references to columns in other FROM clauses */
4225};
4226
4227/*
4228** Count the number of references to columns.
4229*/
4230static int exprSrcCount(Walker *pWalker, Expr *pExpr){
drhfb0a6082012-08-24 01:07:52 +00004231 /* The NEVER() on the second term is because sqlite3FunctionUsesThisSrc()
4232 ** is always called before sqlite3ExprAnalyzeAggregates() and so the
4233 ** TK_COLUMNs have not yet been converted into TK_AGG_COLUMN. If
4234 ** sqlite3FunctionUsesThisSrc() is used differently in the future, the
4235 ** NEVER() will need to be removed. */
4236 if( pExpr->op==TK_COLUMN || NEVER(pExpr->op==TK_AGG_COLUMN) ){
drh374fdce2012-04-17 16:38:53 +00004237 int i;
drh030796d2012-08-23 16:18:10 +00004238 struct SrcCount *p = pWalker->u.pSrcCount;
4239 SrcList *pSrc = p->pSrc;
drh655814d2015-01-09 01:27:29 +00004240 int nSrc = pSrc ? pSrc->nSrc : 0;
4241 for(i=0; i<nSrc; i++){
drh030796d2012-08-23 16:18:10 +00004242 if( pExpr->iTable==pSrc->a[i].iCursor ) break;
drh374fdce2012-04-17 16:38:53 +00004243 }
drh655814d2015-01-09 01:27:29 +00004244 if( i<nSrc ){
drh030796d2012-08-23 16:18:10 +00004245 p->nThis++;
4246 }else{
4247 p->nOther++;
4248 }
drh374fdce2012-04-17 16:38:53 +00004249 }
drh030796d2012-08-23 16:18:10 +00004250 return WRC_Continue;
drh374fdce2012-04-17 16:38:53 +00004251}
4252
4253/*
drh030796d2012-08-23 16:18:10 +00004254** Determine if any of the arguments to the pExpr Function reference
4255** pSrcList. Return true if they do. Also return true if the function
4256** has no arguments or has only constant arguments. Return false if pExpr
4257** references columns but not columns of tables found in pSrcList.
drh374fdce2012-04-17 16:38:53 +00004258*/
drh030796d2012-08-23 16:18:10 +00004259int sqlite3FunctionUsesThisSrc(Expr *pExpr, SrcList *pSrcList){
drh374fdce2012-04-17 16:38:53 +00004260 Walker w;
drh030796d2012-08-23 16:18:10 +00004261 struct SrcCount cnt;
drh374fdce2012-04-17 16:38:53 +00004262 assert( pExpr->op==TK_AGG_FUNCTION );
4263 memset(&w, 0, sizeof(w));
drh030796d2012-08-23 16:18:10 +00004264 w.xExprCallback = exprSrcCount;
4265 w.u.pSrcCount = &cnt;
4266 cnt.pSrc = pSrcList;
4267 cnt.nThis = 0;
4268 cnt.nOther = 0;
4269 sqlite3WalkExprList(&w, pExpr->x.pList);
4270 return cnt.nThis>0 || cnt.nOther==0;
drh374fdce2012-04-17 16:38:53 +00004271}
4272
4273/*
drh13449892005-09-07 21:22:45 +00004274** Add a new element to the pAggInfo->aCol[] array. Return the index of
4275** the new element. Return a negative number if malloc fails.
drh22827922000-06-06 17:27:05 +00004276*/
drh17435752007-08-16 04:30:38 +00004277static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){
drh13449892005-09-07 21:22:45 +00004278 int i;
drhcf643722007-03-27 13:36:37 +00004279 pInfo->aCol = sqlite3ArrayAllocate(
drh17435752007-08-16 04:30:38 +00004280 db,
drhcf643722007-03-27 13:36:37 +00004281 pInfo->aCol,
4282 sizeof(pInfo->aCol[0]),
drhcf643722007-03-27 13:36:37 +00004283 &pInfo->nColumn,
drhcf643722007-03-27 13:36:37 +00004284 &i
4285 );
drh13449892005-09-07 21:22:45 +00004286 return i;
4287}
4288
4289/*
4290** Add a new element to the pAggInfo->aFunc[] array. Return the index of
4291** the new element. Return a negative number if malloc fails.
4292*/
drh17435752007-08-16 04:30:38 +00004293static int addAggInfoFunc(sqlite3 *db, AggInfo *pInfo){
drh13449892005-09-07 21:22:45 +00004294 int i;
drhcf643722007-03-27 13:36:37 +00004295 pInfo->aFunc = sqlite3ArrayAllocate(
drh17435752007-08-16 04:30:38 +00004296 db,
drhcf643722007-03-27 13:36:37 +00004297 pInfo->aFunc,
4298 sizeof(pInfo->aFunc[0]),
drhcf643722007-03-27 13:36:37 +00004299 &pInfo->nFunc,
drhcf643722007-03-27 13:36:37 +00004300 &i
4301 );
drh13449892005-09-07 21:22:45 +00004302 return i;
4303}
drh22827922000-06-06 17:27:05 +00004304
4305/*
drh7d10d5a2008-08-20 16:35:10 +00004306** This is the xExprCallback for a tree walker. It is used to
4307** implement sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates
drh626a8792005-01-17 22:08:19 +00004308** for additional information.
drh22827922000-06-06 17:27:05 +00004309*/
drh7d10d5a2008-08-20 16:35:10 +00004310static int analyzeAggregate(Walker *pWalker, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00004311 int i;
drh7d10d5a2008-08-20 16:35:10 +00004312 NameContext *pNC = pWalker->u.pNC;
danielk1977a58fdfb2005-02-08 07:50:40 +00004313 Parse *pParse = pNC->pParse;
4314 SrcList *pSrcList = pNC->pSrcList;
drh13449892005-09-07 21:22:45 +00004315 AggInfo *pAggInfo = pNC->pAggInfo;
drh22827922000-06-06 17:27:05 +00004316
drh22827922000-06-06 17:27:05 +00004317 switch( pExpr->op ){
drh89c69d02007-01-04 01:20:28 +00004318 case TK_AGG_COLUMN:
drh967e8b72000-06-21 13:59:10 +00004319 case TK_COLUMN: {
drh8b213892008-08-29 02:14:02 +00004320 testcase( pExpr->op==TK_AGG_COLUMN );
4321 testcase( pExpr->op==TK_COLUMN );
drh13449892005-09-07 21:22:45 +00004322 /* Check to see if the column is in one of the tables in the FROM
4323 ** clause of the aggregate query */
drh20bc3932009-05-30 23:35:43 +00004324 if( ALWAYS(pSrcList!=0) ){
drh13449892005-09-07 21:22:45 +00004325 struct SrcList_item *pItem = pSrcList->a;
4326 for(i=0; i<pSrcList->nSrc; i++, pItem++){
4327 struct AggInfo_col *pCol;
drhc5cd1242013-09-12 16:50:49 +00004328 assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) );
drh13449892005-09-07 21:22:45 +00004329 if( pExpr->iTable==pItem->iCursor ){
4330 /* If we reach this point, it means that pExpr refers to a table
4331 ** that is in the FROM clause of the aggregate query.
4332 **
4333 ** Make an entry for the column in pAggInfo->aCol[] if there
4334 ** is not an entry there already.
4335 */
drh7f906d62007-03-12 23:48:52 +00004336 int k;
drh13449892005-09-07 21:22:45 +00004337 pCol = pAggInfo->aCol;
drh7f906d62007-03-12 23:48:52 +00004338 for(k=0; k<pAggInfo->nColumn; k++, pCol++){
drh13449892005-09-07 21:22:45 +00004339 if( pCol->iTable==pExpr->iTable &&
4340 pCol->iColumn==pExpr->iColumn ){
4341 break;
4342 }
danielk1977a58fdfb2005-02-08 07:50:40 +00004343 }
danielk19771e536952007-08-16 10:09:01 +00004344 if( (k>=pAggInfo->nColumn)
4345 && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0
4346 ){
drh7f906d62007-03-12 23:48:52 +00004347 pCol = &pAggInfo->aCol[k];
danielk19770817d0d2007-02-14 09:19:36 +00004348 pCol->pTab = pExpr->pTab;
drh13449892005-09-07 21:22:45 +00004349 pCol->iTable = pExpr->iTable;
4350 pCol->iColumn = pExpr->iColumn;
drh0a07c102008-01-03 18:03:08 +00004351 pCol->iMem = ++pParse->nMem;
drh13449892005-09-07 21:22:45 +00004352 pCol->iSorterColumn = -1;
drh5774b802005-09-07 22:48:16 +00004353 pCol->pExpr = pExpr;
drh13449892005-09-07 21:22:45 +00004354 if( pAggInfo->pGroupBy ){
4355 int j, n;
4356 ExprList *pGB = pAggInfo->pGroupBy;
4357 struct ExprList_item *pTerm = pGB->a;
4358 n = pGB->nExpr;
4359 for(j=0; j<n; j++, pTerm++){
4360 Expr *pE = pTerm->pExpr;
4361 if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable &&
4362 pE->iColumn==pExpr->iColumn ){
4363 pCol->iSorterColumn = j;
4364 break;
4365 }
4366 }
4367 }
4368 if( pCol->iSorterColumn<0 ){
4369 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
4370 }
4371 }
4372 /* There is now an entry for pExpr in pAggInfo->aCol[] (either
4373 ** because it was there before or because we just created it).
4374 ** Convert the pExpr to be a TK_AGG_COLUMN referring to that
4375 ** pAggInfo->aCol[] entry.
4376 */
drhebb6a652013-09-12 23:42:22 +00004377 ExprSetVVAProperty(pExpr, EP_NoReduce);
drh13449892005-09-07 21:22:45 +00004378 pExpr->pAggInfo = pAggInfo;
4379 pExpr->op = TK_AGG_COLUMN;
shanecf697392009-06-01 16:53:09 +00004380 pExpr->iAgg = (i16)k;
drh13449892005-09-07 21:22:45 +00004381 break;
4382 } /* endif pExpr->iTable==pItem->iCursor */
4383 } /* end loop over pSrcList */
drh22827922000-06-06 17:27:05 +00004384 }
drh7d10d5a2008-08-20 16:35:10 +00004385 return WRC_Prune;
drh22827922000-06-06 17:27:05 +00004386 }
4387 case TK_AGG_FUNCTION: {
drh3a8c4be2012-05-21 20:13:39 +00004388 if( (pNC->ncFlags & NC_InAggFunc)==0
drhed551b92012-08-23 19:46:11 +00004389 && pWalker->walkerDepth==pExpr->op2
drh3a8c4be2012-05-21 20:13:39 +00004390 ){
drh13449892005-09-07 21:22:45 +00004391 /* Check to see if pExpr is a duplicate of another aggregate
4392 ** function that is already in the pAggInfo structure
4393 */
4394 struct AggInfo_func *pItem = pAggInfo->aFunc;
4395 for(i=0; i<pAggInfo->nFunc; i++, pItem++){
drh619a1302013-08-01 13:04:46 +00004396 if( sqlite3ExprCompare(pItem->pExpr, pExpr, -1)==0 ){
danielk1977a58fdfb2005-02-08 07:50:40 +00004397 break;
4398 }
drh22827922000-06-06 17:27:05 +00004399 }
drh13449892005-09-07 21:22:45 +00004400 if( i>=pAggInfo->nFunc ){
4401 /* pExpr is original. Make a new entry in pAggInfo->aFunc[]
4402 */
danielk197714db2662006-01-09 16:12:04 +00004403 u8 enc = ENC(pParse->db);
danielk19771e536952007-08-16 10:09:01 +00004404 i = addAggInfoFunc(pParse->db, pAggInfo);
drh13449892005-09-07 21:22:45 +00004405 if( i>=0 ){
danielk19776ab3a2e2009-02-19 14:39:25 +00004406 assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
drh13449892005-09-07 21:22:45 +00004407 pItem = &pAggInfo->aFunc[i];
4408 pItem->pExpr = pExpr;
drh0a07c102008-01-03 18:03:08 +00004409 pItem->iMem = ++pParse->nMem;
drh33e619f2009-05-28 01:00:55 +00004410 assert( !ExprHasProperty(pExpr, EP_IntValue) );
drh13449892005-09-07 21:22:45 +00004411 pItem->pFunc = sqlite3FindFunction(pParse->db,
drh80738d92016-02-15 00:34:16 +00004412 pExpr->u.zToken,
danielk19776ab3a2e2009-02-19 14:39:25 +00004413 pExpr->x.pList ? pExpr->x.pList->nExpr : 0, enc, 0);
drhfd357972005-09-09 01:33:19 +00004414 if( pExpr->flags & EP_Distinct ){
4415 pItem->iDistinct = pParse->nTab++;
4416 }else{
4417 pItem->iDistinct = -1;
4418 }
drh13449892005-09-07 21:22:45 +00004419 }
danielk1977a58fdfb2005-02-08 07:50:40 +00004420 }
drh13449892005-09-07 21:22:45 +00004421 /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
4422 */
drhc5cd1242013-09-12 16:50:49 +00004423 assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) );
drhebb6a652013-09-12 23:42:22 +00004424 ExprSetVVAProperty(pExpr, EP_NoReduce);
shanecf697392009-06-01 16:53:09 +00004425 pExpr->iAgg = (i16)i;
drh13449892005-09-07 21:22:45 +00004426 pExpr->pAggInfo = pAggInfo;
drh6e83a572012-11-02 18:48:49 +00004427 return WRC_Prune;
4428 }else{
4429 return WRC_Continue;
drh22827922000-06-06 17:27:05 +00004430 }
drh22827922000-06-06 17:27:05 +00004431 }
4432 }
drh7d10d5a2008-08-20 16:35:10 +00004433 return WRC_Continue;
4434}
4435static int analyzeAggregatesInSelect(Walker *pWalker, Select *pSelect){
drhd5a336e2012-04-19 15:49:19 +00004436 UNUSED_PARAMETER(pWalker);
4437 UNUSED_PARAMETER(pSelect);
drh374fdce2012-04-17 16:38:53 +00004438 return WRC_Continue;
drh626a8792005-01-17 22:08:19 +00004439}
4440
4441/*
drhe8abb4c2012-11-02 18:24:57 +00004442** Analyze the pExpr expression looking for aggregate functions and
4443** for variables that need to be added to AggInfo object that pNC->pAggInfo
4444** points to. Additional entries are made on the AggInfo object as
4445** necessary.
drh626a8792005-01-17 22:08:19 +00004446**
4447** This routine should only be called after the expression has been
drh7d10d5a2008-08-20 16:35:10 +00004448** analyzed by sqlite3ResolveExprNames().
drh626a8792005-01-17 22:08:19 +00004449*/
drhd2b3e232008-01-23 14:51:49 +00004450void sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
drh7d10d5a2008-08-20 16:35:10 +00004451 Walker w;
drh374fdce2012-04-17 16:38:53 +00004452 memset(&w, 0, sizeof(w));
drh7d10d5a2008-08-20 16:35:10 +00004453 w.xExprCallback = analyzeAggregate;
4454 w.xSelectCallback = analyzeAggregatesInSelect;
4455 w.u.pNC = pNC;
drh20bc3932009-05-30 23:35:43 +00004456 assert( pNC->pSrcList!=0 );
drh7d10d5a2008-08-20 16:35:10 +00004457 sqlite3WalkExpr(&w, pExpr);
drh22827922000-06-06 17:27:05 +00004458}
drh5d9a4af2005-08-30 00:54:01 +00004459
4460/*
4461** Call sqlite3ExprAnalyzeAggregates() for every expression in an
4462** expression list. Return the number of errors.
4463**
4464** If an error is found, the analysis is cut short.
4465*/
drhd2b3e232008-01-23 14:51:49 +00004466void sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){
drh5d9a4af2005-08-30 00:54:01 +00004467 struct ExprList_item *pItem;
4468 int i;
drh5d9a4af2005-08-30 00:54:01 +00004469 if( pList ){
drhd2b3e232008-01-23 14:51:49 +00004470 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
4471 sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
drh5d9a4af2005-08-30 00:54:01 +00004472 }
4473 }
drh5d9a4af2005-08-30 00:54:01 +00004474}
drh892d3172008-01-10 03:46:36 +00004475
4476/*
drhceea3322009-04-23 13:22:42 +00004477** Allocate a single new register for use to hold some intermediate result.
drh892d3172008-01-10 03:46:36 +00004478*/
4479int sqlite3GetTempReg(Parse *pParse){
drhe55cbd72008-03-31 23:48:03 +00004480 if( pParse->nTempReg==0 ){
drh892d3172008-01-10 03:46:36 +00004481 return ++pParse->nMem;
4482 }
danielk19772f425f62008-07-04 09:41:39 +00004483 return pParse->aTempReg[--pParse->nTempReg];
drh892d3172008-01-10 03:46:36 +00004484}
drhceea3322009-04-23 13:22:42 +00004485
4486/*
4487** Deallocate a register, making available for reuse for some other
4488** purpose.
4489**
4490** If a register is currently being used by the column cache, then
peter.d.reid60ec9142014-09-06 16:39:46 +00004491** the deallocation is deferred until the column cache line that uses
drhceea3322009-04-23 13:22:42 +00004492** the register becomes stale.
4493*/
drh892d3172008-01-10 03:46:36 +00004494void sqlite3ReleaseTempReg(Parse *pParse, int iReg){
drh2dcef112008-01-12 19:03:48 +00004495 if( iReg && pParse->nTempReg<ArraySize(pParse->aTempReg) ){
drhceea3322009-04-23 13:22:42 +00004496 int i;
4497 struct yColCache *p;
4498 for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
4499 if( p->iReg==iReg ){
4500 p->tempReg = 1;
4501 return;
4502 }
4503 }
drh892d3172008-01-10 03:46:36 +00004504 pParse->aTempReg[pParse->nTempReg++] = iReg;
4505 }
4506}
4507
4508/*
4509** Allocate or deallocate a block of nReg consecutive registers
4510*/
4511int sqlite3GetTempRange(Parse *pParse, int nReg){
drhe55cbd72008-03-31 23:48:03 +00004512 int i, n;
4513 i = pParse->iRangeReg;
4514 n = pParse->nRangeReg;
drhf49f3522009-12-30 14:12:38 +00004515 if( nReg<=n ){
4516 assert( !usedAsColumnCache(pParse, i, i+n-1) );
drh892d3172008-01-10 03:46:36 +00004517 pParse->iRangeReg += nReg;
4518 pParse->nRangeReg -= nReg;
4519 }else{
4520 i = pParse->nMem+1;
4521 pParse->nMem += nReg;
4522 }
4523 return i;
4524}
4525void sqlite3ReleaseTempRange(Parse *pParse, int iReg, int nReg){
drhf49f3522009-12-30 14:12:38 +00004526 sqlite3ExprCacheRemove(pParse, iReg, nReg);
drh892d3172008-01-10 03:46:36 +00004527 if( nReg>pParse->nRangeReg ){
4528 pParse->nRangeReg = nReg;
4529 pParse->iRangeReg = iReg;
4530 }
4531}
drhcdc69552011-12-06 13:24:59 +00004532
4533/*
4534** Mark all temporary registers as being unavailable for reuse.
4535*/
4536void sqlite3ClearTempRegCache(Parse *pParse){
4537 pParse->nTempReg = 0;
4538 pParse->nRangeReg = 0;
4539}
drhbb9b5f22016-03-19 00:35:02 +00004540
4541/*
4542** Validate that no temporary register falls within the range of
4543** iFirst..iLast, inclusive. This routine is only call from within assert()
4544** statements.
4545*/
4546#ifdef SQLITE_DEBUG
4547int sqlite3NoTempsInRange(Parse *pParse, int iFirst, int iLast){
4548 int i;
4549 if( pParse->nRangeReg>0
4550 && pParse->iRangeReg+pParse->nRangeReg<iLast
4551 && pParse->iRangeReg>=iFirst
4552 ){
4553 return 0;
4554 }
4555 for(i=0; i<pParse->nTempReg; i++){
4556 if( pParse->aTempReg[i]>=iFirst && pParse->aTempReg[i]<=iLast ){
4557 return 0;
4558 }
4559 }
4560 return 1;
4561}
4562#endif /* SQLITE_DEBUG */