blob: 5792073228ec118f4e911c1ff115960b93bcecf9 [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
dancfbb5e82016-07-13 19:48:13 +0000312/*
313** If the expression passed as the only argument is of type TK_VECTOR
314** return the number of expressions in the vector. Or, if the expression
315** is a sub-select, return the number of columns in the sub-select. For
316** any other type of expression, return 1.
317*/
dan71c57db2016-07-09 20:23:55 +0000318int sqlite3ExprVectorSize(Expr *pExpr){
319 if( (pExpr->flags & EP_Vector)==0 ) return 1;
320 if( pExpr->flags & EP_xIsSelect ){
321 return pExpr->x.pSelect->pEList->nExpr;
322 }
323 return pExpr->x.pList->nExpr;
324}
325
danba00e302016-07-23 20:24:06 +0000326/*
327** If the expression passed as the first argument is a TK_VECTOR, return
328** a pointer to the i'th field of the vector. Or, if the first argument
329** points to a sub-select, return a pointer to the i'th returned column
330** value. Otherwise, return a copy of the first argument.
331*/
dan71c57db2016-07-09 20:23:55 +0000332static Expr *exprVectorField(Expr *pVector, int i){
dancfbb5e82016-07-13 19:48:13 +0000333 if( (pVector->flags & EP_Vector)==0 ){
334 assert( i==0 );
335 return pVector;
336 }else if( pVector->flags & EP_xIsSelect ){
dan71c57db2016-07-09 20:23:55 +0000337 return pVector->x.pSelect->pEList->a[i].pExpr;
338 }
339 return pVector->x.pList->a[i].pExpr;
340}
341
342static void codeVectorCompare(Parse *pParse, Expr *pExpr, int dest){
343 Vdbe *v = pParse->pVdbe;
344 Expr *pLeft = pExpr->pLeft;
345 Expr *pRight = pExpr->pRight;
346 int nLeft = sqlite3ExprVectorSize(pLeft);
347 int nRight = sqlite3ExprVectorSize(pRight);
348 int addr = sqlite3VdbeMakeLabel(v);
349
350 /* Check that both sides of the comparison are vectors, and that
351 ** both are the same length. */
352 if( nLeft!=nRight ){
353 sqlite3ErrorMsg(pParse, "invalid use of row value");
354 }else{
355 int p5 = (pExpr->op==TK_IS || pExpr->op==TK_ISNOT) ? SQLITE_NULLEQ : 0;
356 int opCmp;
357 int opTest;
358 int i;
359 int p3 = 1;
360 int regLeft = 0;
361 int regRight = 0;
362
363 assert( pExpr->op==TK_EQ || pExpr->op==TK_NE
364 || pExpr->op==TK_IS || pExpr->op==TK_ISNOT
365 || pExpr->op==TK_LT || pExpr->op==TK_GT
366 || pExpr->op==TK_LE || pExpr->op==TK_GE
367 );
368
369 switch( pExpr->op ){
370 case TK_EQ:
371 case TK_IS:
372 opTest = OP_IfNot;
373 opCmp = OP_Eq;
374 break;
375
376 case TK_NE:
377 case TK_ISNOT:
378 opTest = OP_If;
379 opCmp = OP_Ne;
380 break;
381
382 case TK_LT:
383 case TK_LE:
384 case TK_GT:
385 case TK_GE:
386 opCmp = OP_Cmp;
387 opTest = OP_CmpTest;
388 p3 = pExpr->op;
389 break;
390 }
391
392 if( pLeft->flags & EP_xIsSelect ){
393 assert( pLeft->op==TK_SELECT || pLeft->op==TK_REGISTER );
394 regLeft = sqlite3ExprCodeTarget(pParse, pLeft, 1);
395 assert( regLeft!=1 );
396 }
397 if( pRight->flags & EP_xIsSelect ){
398 assert( pRight->op==TK_SELECT || pRight->op==TK_REGISTER );
399 regRight = sqlite3ExprCodeTarget(pParse, pRight, 1);
400 assert( regRight!=1 );
401 }
402 if( pParse->nErr ) return;
403
404 for(i=0; i<nLeft; i++){
405 int regFree1 = 0, regFree2 = 0;
406 Expr *pL, *pR;
407 int r1, r2;
408
409 if( regLeft ){
410 pL = pLeft->x.pSelect->pEList->a[i].pExpr;
411 r1 = regLeft+i;
412 }else{
413 pL = pLeft->x.pList->a[i].pExpr;
414 r1 = sqlite3ExprCodeTemp(pParse, pL, &regFree1);
415 }
416
417 if( regRight ){
418 pR = pRight->x.pSelect->pEList->a[i].pExpr;
419 r2 = regRight+i;
420 }else{
421 pR = pRight->x.pList->a[i].pExpr;
422 r2 = sqlite3ExprCodeTemp(pParse, pR, &regFree1);
423 }
424
425 codeCompare(pParse, pL, pR, opCmp, r1, r2, dest, SQLITE_STOREP2 | p5);
426 sqlite3VdbeAddOp3(v, opTest, dest, addr, p3);
427 sqlite3ReleaseTempReg(pParse, regFree1);
428 sqlite3ReleaseTempReg(pParse, regFree2);
429 }
430 }
431
432 sqlite3VdbeResolveLabel(v, addr);
433}
434
danielk19774b5255a2008-06-05 16:47:39 +0000435#if SQLITE_MAX_EXPR_DEPTH>0
436/*
437** Check that argument nHeight is less than or equal to the maximum
438** expression depth allowed. If it is not, leave an error message in
439** pParse.
440*/
drh7d10d5a2008-08-20 16:35:10 +0000441int sqlite3ExprCheckHeight(Parse *pParse, int nHeight){
danielk19774b5255a2008-06-05 16:47:39 +0000442 int rc = SQLITE_OK;
443 int mxHeight = pParse->db->aLimit[SQLITE_LIMIT_EXPR_DEPTH];
444 if( nHeight>mxHeight ){
445 sqlite3ErrorMsg(pParse,
446 "Expression tree is too large (maximum depth %d)", mxHeight
447 );
448 rc = SQLITE_ERROR;
449 }
450 return rc;
451}
452
453/* The following three functions, heightOfExpr(), heightOfExprList()
454** and heightOfSelect(), are used to determine the maximum height
455** of any expression tree referenced by the structure passed as the
456** first argument.
457**
458** If this maximum height is greater than the current value pointed
459** to by pnHeight, the second parameter, then set *pnHeight to that
460** value.
461*/
462static void heightOfExpr(Expr *p, int *pnHeight){
463 if( p ){
464 if( p->nHeight>*pnHeight ){
465 *pnHeight = p->nHeight;
466 }
467 }
468}
469static void heightOfExprList(ExprList *p, int *pnHeight){
470 if( p ){
471 int i;
472 for(i=0; i<p->nExpr; i++){
473 heightOfExpr(p->a[i].pExpr, pnHeight);
474 }
475 }
476}
477static void heightOfSelect(Select *p, int *pnHeight){
478 if( p ){
479 heightOfExpr(p->pWhere, pnHeight);
480 heightOfExpr(p->pHaving, pnHeight);
481 heightOfExpr(p->pLimit, pnHeight);
482 heightOfExpr(p->pOffset, pnHeight);
483 heightOfExprList(p->pEList, pnHeight);
484 heightOfExprList(p->pGroupBy, pnHeight);
485 heightOfExprList(p->pOrderBy, pnHeight);
486 heightOfSelect(p->pPrior, pnHeight);
487 }
488}
489
490/*
491** Set the Expr.nHeight variable in the structure passed as an
492** argument. An expression with no children, Expr.pList or
493** Expr.pSelect member has a height of 1. Any other expression
494** has a height equal to the maximum height of any other
495** referenced Expr plus one.
drh2308ed32015-02-09 16:09:34 +0000496**
497** Also propagate EP_Propagate flags up from Expr.x.pList to Expr.flags,
498** if appropriate.
danielk19774b5255a2008-06-05 16:47:39 +0000499*/
500static void exprSetHeight(Expr *p){
501 int nHeight = 0;
502 heightOfExpr(p->pLeft, &nHeight);
503 heightOfExpr(p->pRight, &nHeight);
danielk19776ab3a2e2009-02-19 14:39:25 +0000504 if( ExprHasProperty(p, EP_xIsSelect) ){
505 heightOfSelect(p->x.pSelect, &nHeight);
drh2308ed32015-02-09 16:09:34 +0000506 }else if( p->x.pList ){
danielk19776ab3a2e2009-02-19 14:39:25 +0000507 heightOfExprList(p->x.pList, &nHeight);
drh2308ed32015-02-09 16:09:34 +0000508 p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList);
danielk19776ab3a2e2009-02-19 14:39:25 +0000509 }
danielk19774b5255a2008-06-05 16:47:39 +0000510 p->nHeight = nHeight + 1;
511}
512
513/*
514** Set the Expr.nHeight variable using the exprSetHeight() function. If
515** the height is greater than the maximum allowed expression depth,
516** leave an error in pParse.
drh2308ed32015-02-09 16:09:34 +0000517**
518** Also propagate all EP_Propagate flags from the Expr.x.pList into
519** Expr.flags.
danielk19774b5255a2008-06-05 16:47:39 +0000520*/
drh2308ed32015-02-09 16:09:34 +0000521void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p){
drh74893a42015-03-22 10:23:17 +0000522 if( pParse->nErr ) return;
danielk19774b5255a2008-06-05 16:47:39 +0000523 exprSetHeight(p);
drh7d10d5a2008-08-20 16:35:10 +0000524 sqlite3ExprCheckHeight(pParse, p->nHeight);
danielk19774b5255a2008-06-05 16:47:39 +0000525}
526
527/*
528** Return the maximum height of any expression tree referenced
529** by the select statement passed as an argument.
530*/
531int sqlite3SelectExprHeight(Select *p){
532 int nHeight = 0;
533 heightOfSelect(p, &nHeight);
534 return nHeight;
535}
drh2308ed32015-02-09 16:09:34 +0000536#else /* ABOVE: Height enforcement enabled. BELOW: Height enforcement off */
537/*
538** Propagate all EP_Propagate flags from the Expr.x.pList into
539** Expr.flags.
540*/
541void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p){
542 if( p && p->x.pList && !ExprHasProperty(p, EP_xIsSelect) ){
543 p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList);
544 }
545}
546#define exprSetHeight(y)
danielk19774b5255a2008-06-05 16:47:39 +0000547#endif /* SQLITE_MAX_EXPR_DEPTH>0 */
548
drhbe5c89a2004-07-26 00:31:09 +0000549/*
drhb7916a72009-05-27 10:31:29 +0000550** This routine is the core allocator for Expr nodes.
551**
drha76b5df2002-02-23 02:32:10 +0000552** Construct a new expression node and return a pointer to it. Memory
drhb7916a72009-05-27 10:31:29 +0000553** for this node and for the pToken argument is a single allocation
554** obtained from sqlite3DbMalloc(). The calling function
drha76b5df2002-02-23 02:32:10 +0000555** is responsible for making sure the node eventually gets freed.
drhb7916a72009-05-27 10:31:29 +0000556**
557** If dequote is true, then the token (if it exists) is dequoted.
drhe792b5b2015-08-23 20:48:29 +0000558** If dequote is false, no dequoting is performed. The deQuote
drhb7916a72009-05-27 10:31:29 +0000559** parameter is ignored if pToken is NULL or if the token does not
560** appear to be quoted. If the quotes were of the form "..." (double-quotes)
561** then the EP_DblQuoted flag is set on the expression node.
drh33e619f2009-05-28 01:00:55 +0000562**
563** Special case: If op==TK_INTEGER and pToken points to a string that
564** can be translated into a 32-bit integer, then the token is not
565** stored in u.zToken. Instead, the integer values is written
566** into u.iValue and the EP_IntValue flag is set. No extra storage
567** is allocated to hold the integer text and the dequote flag is ignored.
drha76b5df2002-02-23 02:32:10 +0000568*/
drhb7916a72009-05-27 10:31:29 +0000569Expr *sqlite3ExprAlloc(
danielk1977a1644fd2007-08-29 12:31:25 +0000570 sqlite3 *db, /* Handle for sqlite3DbMallocZero() (may be null) */
drh17435752007-08-16 04:30:38 +0000571 int op, /* Expression opcode */
drhb7916a72009-05-27 10:31:29 +0000572 const Token *pToken, /* Token argument. Might be NULL */
573 int dequote /* True to dequote */
drh17435752007-08-16 04:30:38 +0000574){
drha76b5df2002-02-23 02:32:10 +0000575 Expr *pNew;
drh33e619f2009-05-28 01:00:55 +0000576 int nExtra = 0;
shanecf697392009-06-01 16:53:09 +0000577 int iValue = 0;
danielk1977fc976062007-05-10 10:46:56 +0000578
drh575fad62016-02-05 13:38:36 +0000579 assert( db!=0 );
drhb7916a72009-05-27 10:31:29 +0000580 if( pToken ){
drh33e619f2009-05-28 01:00:55 +0000581 if( op!=TK_INTEGER || pToken->z==0
582 || sqlite3GetInt32(pToken->z, &iValue)==0 ){
583 nExtra = pToken->n+1;
drhd50ffc42011-03-08 02:38:28 +0000584 assert( iValue>=0 );
drh33e619f2009-05-28 01:00:55 +0000585 }
drhb7916a72009-05-27 10:31:29 +0000586 }
drh575fad62016-02-05 13:38:36 +0000587 pNew = sqlite3DbMallocRawNN(db, sizeof(Expr)+nExtra);
drhb7916a72009-05-27 10:31:29 +0000588 if( pNew ){
drhca3862d2016-01-08 12:46:39 +0000589 memset(pNew, 0, sizeof(Expr));
drhb7916a72009-05-27 10:31:29 +0000590 pNew->op = (u8)op;
591 pNew->iAgg = -1;
592 if( pToken ){
drh33e619f2009-05-28 01:00:55 +0000593 if( nExtra==0 ){
594 pNew->flags |= EP_IntValue;
595 pNew->u.iValue = iValue;
596 }else{
drh33e619f2009-05-28 01:00:55 +0000597 pNew->u.zToken = (char*)&pNew[1];
drhb07028f2011-10-14 21:49:18 +0000598 assert( pToken->z!=0 || pToken->n==0 );
599 if( pToken->n ) memcpy(pNew->u.zToken, pToken->z, pToken->n);
drh33e619f2009-05-28 01:00:55 +0000600 pNew->u.zToken[pToken->n] = 0;
drh244b9d62016-04-11 19:01:08 +0000601 if( dequote && sqlite3Isquote(pNew->u.zToken[0]) ){
602 if( pNew->u.zToken[0]=='"' ) pNew->flags |= EP_DblQuoted;
drh33e619f2009-05-28 01:00:55 +0000603 sqlite3Dequote(pNew->u.zToken);
drh33e619f2009-05-28 01:00:55 +0000604 }
drhb7916a72009-05-27 10:31:29 +0000605 }
606 }
607#if SQLITE_MAX_EXPR_DEPTH>0
608 pNew->nHeight = 1;
609#endif
610 }
drha76b5df2002-02-23 02:32:10 +0000611 return pNew;
612}
613
614/*
drhb7916a72009-05-27 10:31:29 +0000615** Allocate a new expression node from a zero-terminated token that has
616** already been dequoted.
617*/
618Expr *sqlite3Expr(
619 sqlite3 *db, /* Handle for sqlite3DbMallocZero() (may be null) */
620 int op, /* Expression opcode */
621 const char *zToken /* Token argument. Might be NULL */
622){
623 Token x;
624 x.z = zToken;
625 x.n = zToken ? sqlite3Strlen30(zToken) : 0;
626 return sqlite3ExprAlloc(db, op, &x, 0);
627}
628
629/*
630** Attach subtrees pLeft and pRight to the Expr node pRoot.
631**
632** If pRoot==NULL that means that a memory allocation error has occurred.
633** In that case, delete the subtrees pLeft and pRight.
634*/
635void sqlite3ExprAttachSubtrees(
636 sqlite3 *db,
637 Expr *pRoot,
638 Expr *pLeft,
639 Expr *pRight
640){
641 if( pRoot==0 ){
642 assert( db->mallocFailed );
643 sqlite3ExprDelete(db, pLeft);
644 sqlite3ExprDelete(db, pRight);
645 }else{
646 if( pRight ){
647 pRoot->pRight = pRight;
drh885a5b02015-02-09 15:21:36 +0000648 pRoot->flags |= EP_Propagate & pRight->flags;
drhb7916a72009-05-27 10:31:29 +0000649 }
650 if( pLeft ){
651 pRoot->pLeft = pLeft;
drh885a5b02015-02-09 15:21:36 +0000652 pRoot->flags |= EP_Propagate & pLeft->flags;
drhb7916a72009-05-27 10:31:29 +0000653 }
654 exprSetHeight(pRoot);
655 }
656}
657
658/*
peter.d.reid60ec9142014-09-06 16:39:46 +0000659** Allocate an Expr node which joins as many as two subtrees.
drhb7916a72009-05-27 10:31:29 +0000660**
drhbf664462009-06-19 18:32:54 +0000661** One or both of the subtrees can be NULL. Return a pointer to the new
662** Expr node. Or, if an OOM error occurs, set pParse->db->mallocFailed,
663** free the subtrees and return NULL.
drh206f3d92006-07-11 13:15:08 +0000664*/
drh17435752007-08-16 04:30:38 +0000665Expr *sqlite3PExpr(
666 Parse *pParse, /* Parsing context */
667 int op, /* Expression opcode */
668 Expr *pLeft, /* Left operand */
669 Expr *pRight, /* Right operand */
670 const Token *pToken /* Argument token */
671){
drh5fb52ca2012-03-31 02:34:35 +0000672 Expr *p;
drh1167d322015-10-28 20:01:45 +0000673 if( op==TK_AND && pParse->nErr==0 ){
drh5fb52ca2012-03-31 02:34:35 +0000674 /* Take advantage of short-circuit false optimization for AND */
675 p = sqlite3ExprAnd(pParse->db, pLeft, pRight);
676 }else{
drh1167d322015-10-28 20:01:45 +0000677 p = sqlite3ExprAlloc(pParse->db, op & TKFLG_MASK, pToken, 1);
drh5fb52ca2012-03-31 02:34:35 +0000678 sqlite3ExprAttachSubtrees(pParse->db, p, pLeft, pRight);
679 }
dan2b359bd2010-10-28 11:31:23 +0000680 if( p ) {
681 sqlite3ExprCheckHeight(pParse, p->nHeight);
682 }
drh4e0cff62004-11-05 05:10:28 +0000683 return p;
684}
685
686/*
drh08de4f72016-04-11 01:06:47 +0000687** Add pSelect to the Expr.x.pSelect field. Or, if pExpr is NULL (due
688** do a memory allocation failure) then delete the pSelect object.
689*/
690void sqlite3PExprAddSelect(Parse *pParse, Expr *pExpr, Select *pSelect){
691 if( pExpr ){
692 pExpr->x.pSelect = pSelect;
693 ExprSetProperty(pExpr, EP_xIsSelect|EP_Subquery);
694 sqlite3ExprSetHeightAndFlags(pParse, pExpr);
695 }else{
696 assert( pParse->db->mallocFailed );
697 sqlite3SelectDelete(pParse->db, pSelect);
698 }
699}
700
701
702/*
drh991a1982014-01-02 17:57:16 +0000703** If the expression is always either TRUE or FALSE (respectively),
704** then return 1. If one cannot determine the truth value of the
705** expression at compile-time return 0.
706**
707** This is an optimization. If is OK to return 0 here even if
708** the expression really is always false or false (a false negative).
709** But it is a bug to return 1 if the expression might have different
710** boolean values in different circumstances (a false positive.)
drh5fb52ca2012-03-31 02:34:35 +0000711**
712** Note that if the expression is part of conditional for a
713** LEFT JOIN, then we cannot determine at compile-time whether or not
714** is it true or false, so always return 0.
715*/
drh991a1982014-01-02 17:57:16 +0000716static int exprAlwaysTrue(Expr *p){
717 int v = 0;
718 if( ExprHasProperty(p, EP_FromJoin) ) return 0;
719 if( !sqlite3ExprIsInteger(p, &v) ) return 0;
720 return v!=0;
721}
drh5fb52ca2012-03-31 02:34:35 +0000722static int exprAlwaysFalse(Expr *p){
723 int v = 0;
724 if( ExprHasProperty(p, EP_FromJoin) ) return 0;
725 if( !sqlite3ExprIsInteger(p, &v) ) return 0;
726 return v==0;
727}
728
729/*
drh91bb0ee2004-09-01 03:06:34 +0000730** Join two expressions using an AND operator. If either expression is
731** NULL, then just return the other expression.
drh5fb52ca2012-03-31 02:34:35 +0000732**
733** If one side or the other of the AND is known to be false, then instead
734** of returning an AND expression, just return a constant expression with
735** a value of false.
drh91bb0ee2004-09-01 03:06:34 +0000736*/
danielk19771e536952007-08-16 10:09:01 +0000737Expr *sqlite3ExprAnd(sqlite3 *db, Expr *pLeft, Expr *pRight){
drh91bb0ee2004-09-01 03:06:34 +0000738 if( pLeft==0 ){
739 return pRight;
740 }else if( pRight==0 ){
741 return pLeft;
drh5fb52ca2012-03-31 02:34:35 +0000742 }else if( exprAlwaysFalse(pLeft) || exprAlwaysFalse(pRight) ){
743 sqlite3ExprDelete(db, pLeft);
744 sqlite3ExprDelete(db, pRight);
745 return sqlite3ExprAlloc(db, TK_INTEGER, &sqlite3IntTokens[0], 0);
drh91bb0ee2004-09-01 03:06:34 +0000746 }else{
drhb7916a72009-05-27 10:31:29 +0000747 Expr *pNew = sqlite3ExprAlloc(db, TK_AND, 0, 0);
748 sqlite3ExprAttachSubtrees(db, pNew, pLeft, pRight);
749 return pNew;
drha76b5df2002-02-23 02:32:10 +0000750 }
751}
752
753/*
754** Construct a new expression node for a function with multiple
755** arguments.
756*/
drh17435752007-08-16 04:30:38 +0000757Expr *sqlite3ExprFunction(Parse *pParse, ExprList *pList, Token *pToken){
drha76b5df2002-02-23 02:32:10 +0000758 Expr *pNew;
drh633e6d52008-07-28 19:34:53 +0000759 sqlite3 *db = pParse->db;
danielk19774b202ae2006-01-23 05:50:58 +0000760 assert( pToken );
drhb7916a72009-05-27 10:31:29 +0000761 pNew = sqlite3ExprAlloc(db, TK_FUNCTION, pToken, 1);
drha76b5df2002-02-23 02:32:10 +0000762 if( pNew==0 ){
drhd9da78a2009-03-24 15:08:09 +0000763 sqlite3ExprListDelete(db, pList); /* Avoid memory leak when malloc fails */
drha76b5df2002-02-23 02:32:10 +0000764 return 0;
765 }
danielk19776ab3a2e2009-02-19 14:39:25 +0000766 pNew->x.pList = pList;
767 assert( !ExprHasProperty(pNew, EP_xIsSelect) );
drh2308ed32015-02-09 16:09:34 +0000768 sqlite3ExprSetHeightAndFlags(pParse, pNew);
drha76b5df2002-02-23 02:32:10 +0000769 return pNew;
770}
771
772/*
drhfa6bc002004-09-07 16:19:52 +0000773** Assign a variable number to an expression that encodes a wildcard
774** in the original SQL statement.
775**
776** Wildcards consisting of a single "?" are assigned the next sequential
777** variable number.
778**
779** Wildcards of the form "?nnn" are assigned the number "nnn". We make
780** sure "nnn" is not too be to avoid a denial of service attack when
781** the SQL statement comes from an external source.
782**
drh51f49f12009-05-21 20:41:32 +0000783** Wildcards of the form ":aaa", "@aaa", or "$aaa" are assigned the same number
drhfa6bc002004-09-07 16:19:52 +0000784** as the previous instance of the same wildcard. Or if this is the first
peter.d.reid60ec9142014-09-06 16:39:46 +0000785** instance of the wildcard, the next sequential variable number is
drhfa6bc002004-09-07 16:19:52 +0000786** assigned.
787*/
788void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){
drh17435752007-08-16 04:30:38 +0000789 sqlite3 *db = pParse->db;
drhb7916a72009-05-27 10:31:29 +0000790 const char *z;
drh17435752007-08-16 04:30:38 +0000791
drhfa6bc002004-09-07 16:19:52 +0000792 if( pExpr==0 ) return;
drhc5cd1242013-09-12 16:50:49 +0000793 assert( !ExprHasProperty(pExpr, EP_IntValue|EP_Reduced|EP_TokenOnly) );
drh33e619f2009-05-28 01:00:55 +0000794 z = pExpr->u.zToken;
drhb7916a72009-05-27 10:31:29 +0000795 assert( z!=0 );
796 assert( z[0]!=0 );
797 if( z[1]==0 ){
drhfa6bc002004-09-07 16:19:52 +0000798 /* Wildcard of the form "?". Assign the next variable number */
drhb7916a72009-05-27 10:31:29 +0000799 assert( z[0]=='?' );
drh8677d302009-11-04 13:17:14 +0000800 pExpr->iColumn = (ynVar)(++pParse->nVar);
drhfa6bc002004-09-07 16:19:52 +0000801 }else{
drh124c0b42011-06-01 18:15:55 +0000802 ynVar x = 0;
803 u32 n = sqlite3Strlen30(z);
804 if( z[0]=='?' ){
805 /* Wildcard of the form "?nnn". Convert "nnn" to an integer and
806 ** use it as the variable number */
807 i64 i;
808 int bOk = 0==sqlite3Atoi64(&z[1], &i, n-1, SQLITE_UTF8);
809 pExpr->iColumn = x = (ynVar)i;
810 testcase( i==0 );
811 testcase( i==1 );
812 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 );
813 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] );
814 if( bOk==0 || i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
815 sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
816 db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]);
817 x = 0;
drhfa6bc002004-09-07 16:19:52 +0000818 }
drh124c0b42011-06-01 18:15:55 +0000819 if( i>pParse->nVar ){
820 pParse->nVar = (int)i;
821 }
822 }else{
823 /* Wildcards like ":aaa", "$aaa" or "@aaa". Reuse the same variable
824 ** number as the prior appearance of the same name, or if the name
825 ** has never appeared before, reuse the same variable number
826 */
827 ynVar i;
828 for(i=0; i<pParse->nzVar; i++){
drh503a6862013-03-01 01:07:17 +0000829 if( pParse->azVar[i] && strcmp(pParse->azVar[i],z)==0 ){
drh124c0b42011-06-01 18:15:55 +0000830 pExpr->iColumn = x = (ynVar)i+1;
831 break;
832 }
833 }
834 if( x==0 ) x = pExpr->iColumn = (ynVar)(++pParse->nVar);
drhfa6bc002004-09-07 16:19:52 +0000835 }
drh124c0b42011-06-01 18:15:55 +0000836 if( x>0 ){
837 if( x>pParse->nzVar ){
838 char **a;
839 a = sqlite3DbRealloc(db, pParse->azVar, x*sizeof(a[0]));
drh4a642b62016-02-05 01:55:27 +0000840 if( a==0 ){
841 assert( db->mallocFailed ); /* Error reported through mallocFailed */
842 return;
843 }
drh124c0b42011-06-01 18:15:55 +0000844 pParse->azVar = a;
845 memset(&a[pParse->nzVar], 0, (x-pParse->nzVar)*sizeof(a[0]));
846 pParse->nzVar = x;
drhfa6bc002004-09-07 16:19:52 +0000847 }
drh124c0b42011-06-01 18:15:55 +0000848 if( z[0]!='?' || pParse->azVar[x-1]==0 ){
849 sqlite3DbFree(db, pParse->azVar[x-1]);
850 pParse->azVar[x-1] = sqlite3DbStrNDup(db, z, n);
drhfa6bc002004-09-07 16:19:52 +0000851 }
852 }
853 }
drhbb4957f2008-03-20 14:03:29 +0000854 if( !pParse->nErr && pParse->nVar>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
danielk1977832b2662007-05-09 11:37:22 +0000855 sqlite3ErrorMsg(pParse, "too many SQL variables");
856 }
drhfa6bc002004-09-07 16:19:52 +0000857}
858
859/*
danf6963f92009-11-23 14:39:14 +0000860** Recursively delete an expression tree.
drha2e00042002-01-22 03:13:42 +0000861*/
drh4f0010b2016-04-11 14:49:39 +0000862static SQLITE_NOINLINE void sqlite3ExprDeleteNN(sqlite3 *db, Expr *p){
863 assert( p!=0 );
drhd50ffc42011-03-08 02:38:28 +0000864 /* Sanity check: Assert that the IntValue is non-negative if it exists */
865 assert( !ExprHasProperty(p, EP_IntValue) || p->u.iValue>=0 );
drhc5cd1242013-09-12 16:50:49 +0000866 if( !ExprHasProperty(p, EP_TokenOnly) ){
867 /* The Expr.x union is never used at the same time as Expr.pRight */
868 assert( p->x.pList==0 || p->pRight==0 );
dan71c57db2016-07-09 20:23:55 +0000869 if( p->op!=TK_SELECT_COLUMN ) sqlite3ExprDelete(db, p->pLeft);
drh33e619f2009-05-28 01:00:55 +0000870 sqlite3ExprDelete(db, p->pRight);
drhc5cd1242013-09-12 16:50:49 +0000871 if( ExprHasProperty(p, EP_MemToken) ) sqlite3DbFree(db, p->u.zToken);
danielk19776ab3a2e2009-02-19 14:39:25 +0000872 if( ExprHasProperty(p, EP_xIsSelect) ){
873 sqlite3SelectDelete(db, p->x.pSelect);
874 }else{
875 sqlite3ExprListDelete(db, p->x.pList);
876 }
877 }
drh33e619f2009-05-28 01:00:55 +0000878 if( !ExprHasProperty(p, EP_Static) ){
879 sqlite3DbFree(db, p);
880 }
drha2e00042002-01-22 03:13:42 +0000881}
drh4f0010b2016-04-11 14:49:39 +0000882void sqlite3ExprDelete(sqlite3 *db, Expr *p){
883 if( p ) sqlite3ExprDeleteNN(db, p);
884}
drha2e00042002-01-22 03:13:42 +0000885
drhd2687b72005-08-12 22:56:09 +0000886/*
danielk19776ab3a2e2009-02-19 14:39:25 +0000887** Return the number of bytes allocated for the expression structure
888** passed as the first argument. This is always one of EXPR_FULLSIZE,
889** EXPR_REDUCEDSIZE or EXPR_TOKENONLYSIZE.
890*/
891static int exprStructSize(Expr *p){
892 if( ExprHasProperty(p, EP_TokenOnly) ) return EXPR_TOKENONLYSIZE;
danielk19776ab3a2e2009-02-19 14:39:25 +0000893 if( ExprHasProperty(p, EP_Reduced) ) return EXPR_REDUCEDSIZE;
894 return EXPR_FULLSIZE;
895}
896
897/*
drh33e619f2009-05-28 01:00:55 +0000898** The dupedExpr*Size() routines each return the number of bytes required
899** to store a copy of an expression or expression tree. They differ in
900** how much of the tree is measured.
901**
902** dupedExprStructSize() Size of only the Expr structure
903** dupedExprNodeSize() Size of Expr + space for token
904** dupedExprSize() Expr + token + subtree components
905**
906***************************************************************************
907**
908** The dupedExprStructSize() function returns two values OR-ed together:
909** (1) the space required for a copy of the Expr structure only and
910** (2) the EP_xxx flags that indicate what the structure size should be.
911** The return values is always one of:
912**
913** EXPR_FULLSIZE
914** EXPR_REDUCEDSIZE | EP_Reduced
915** EXPR_TOKENONLYSIZE | EP_TokenOnly
916**
917** The size of the structure can be found by masking the return value
918** of this routine with 0xfff. The flags can be found by masking the
919** return value with EP_Reduced|EP_TokenOnly.
920**
921** Note that with flags==EXPRDUP_REDUCE, this routines works on full-size
922** (unreduced) Expr objects as they or originally constructed by the parser.
923** During expression analysis, extra information is computed and moved into
924** later parts of teh Expr object and that extra information might get chopped
925** off if the expression is reduced. Note also that it does not work to
peter.d.reid60ec9142014-09-06 16:39:46 +0000926** make an EXPRDUP_REDUCE copy of a reduced expression. It is only legal
drh33e619f2009-05-28 01:00:55 +0000927** to reduce a pristine expression tree from the parser. The implementation
928** of dupedExprStructSize() contain multiple assert() statements that attempt
929** to enforce this constraint.
danielk19776ab3a2e2009-02-19 14:39:25 +0000930*/
931static int dupedExprStructSize(Expr *p, int flags){
932 int nSize;
drh33e619f2009-05-28 01:00:55 +0000933 assert( flags==EXPRDUP_REDUCE || flags==0 ); /* Only one flag value allowed */
drhaecd8022013-09-13 18:15:15 +0000934 assert( EXPR_FULLSIZE<=0xfff );
935 assert( (0xfff & (EP_Reduced|EP_TokenOnly))==0 );
drh3c194692016-04-11 16:43:43 +0000936 if( 0==flags ){
danielk19776ab3a2e2009-02-19 14:39:25 +0000937 nSize = EXPR_FULLSIZE;
danielk19776ab3a2e2009-02-19 14:39:25 +0000938 }else{
drhc5cd1242013-09-12 16:50:49 +0000939 assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) );
drh33e619f2009-05-28 01:00:55 +0000940 assert( !ExprHasProperty(p, EP_FromJoin) );
drhc5cd1242013-09-12 16:50:49 +0000941 assert( !ExprHasProperty(p, EP_MemToken) );
drhebb6a652013-09-12 23:42:22 +0000942 assert( !ExprHasProperty(p, EP_NoReduce) );
drhaecd8022013-09-13 18:15:15 +0000943 if( p->pLeft || p->x.pList ){
drh33e619f2009-05-28 01:00:55 +0000944 nSize = EXPR_REDUCEDSIZE | EP_Reduced;
945 }else{
drhaecd8022013-09-13 18:15:15 +0000946 assert( p->pRight==0 );
drh33e619f2009-05-28 01:00:55 +0000947 nSize = EXPR_TOKENONLYSIZE | EP_TokenOnly;
948 }
danielk19776ab3a2e2009-02-19 14:39:25 +0000949 }
950 return nSize;
951}
952
953/*
drh33e619f2009-05-28 01:00:55 +0000954** This function returns the space in bytes required to store the copy
955** of the Expr structure and a copy of the Expr.u.zToken string (if that
956** string is defined.)
danielk19776ab3a2e2009-02-19 14:39:25 +0000957*/
958static int dupedExprNodeSize(Expr *p, int flags){
drh33e619f2009-05-28 01:00:55 +0000959 int nByte = dupedExprStructSize(p, flags) & 0xfff;
960 if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
961 nByte += sqlite3Strlen30(p->u.zToken)+1;
danielk19776ab3a2e2009-02-19 14:39:25 +0000962 }
danielk1977bc739712009-03-23 04:33:32 +0000963 return ROUND8(nByte);
danielk19776ab3a2e2009-02-19 14:39:25 +0000964}
965
966/*
967** Return the number of bytes required to create a duplicate of the
968** expression passed as the first argument. The second argument is a
969** mask containing EXPRDUP_XXX flags.
970**
971** The value returned includes space to create a copy of the Expr struct
drh33e619f2009-05-28 01:00:55 +0000972** itself and the buffer referred to by Expr.u.zToken, if any.
danielk19776ab3a2e2009-02-19 14:39:25 +0000973**
974** If the EXPRDUP_REDUCE flag is set, then the return value includes
975** space to duplicate all Expr nodes in the tree formed by Expr.pLeft
976** and Expr.pRight variables (but not for any structures pointed to or
977** descended from the Expr.x.pList or Expr.x.pSelect variables).
978*/
979static int dupedExprSize(Expr *p, int flags){
980 int nByte = 0;
981 if( p ){
982 nByte = dupedExprNodeSize(p, flags);
983 if( flags&EXPRDUP_REDUCE ){
drhb7916a72009-05-27 10:31:29 +0000984 nByte += dupedExprSize(p->pLeft, flags) + dupedExprSize(p->pRight, flags);
danielk19776ab3a2e2009-02-19 14:39:25 +0000985 }
986 }
987 return nByte;
988}
989
990/*
991** This function is similar to sqlite3ExprDup(), except that if pzBuffer
992** is not NULL then *pzBuffer is assumed to point to a buffer large enough
drh33e619f2009-05-28 01:00:55 +0000993** to store the copy of expression p, the copies of p->u.zToken
danielk19776ab3a2e2009-02-19 14:39:25 +0000994** (if applicable), and the copies of the p->pLeft and p->pRight expressions,
peter.d.reid60ec9142014-09-06 16:39:46 +0000995** if any. Before returning, *pzBuffer is set to the first byte past the
danielk19776ab3a2e2009-02-19 14:39:25 +0000996** portion of the buffer copied into by this function.
997*/
drh3c194692016-04-11 16:43:43 +0000998static Expr *exprDup(sqlite3 *db, Expr *p, int dupFlags, u8 **pzBuffer){
999 Expr *pNew; /* Value to return */
1000 u8 *zAlloc; /* Memory space from which to build Expr object */
1001 u32 staticFlag; /* EP_Static if space not obtained from malloc */
1002
drh575fad62016-02-05 13:38:36 +00001003 assert( db!=0 );
drh3c194692016-04-11 16:43:43 +00001004 assert( p );
1005 assert( dupFlags==0 || dupFlags==EXPRDUP_REDUCE );
1006 assert( pzBuffer==0 || dupFlags==EXPRDUP_REDUCE );
danielk19776ab3a2e2009-02-19 14:39:25 +00001007
drh3c194692016-04-11 16:43:43 +00001008 /* Figure out where to write the new Expr structure. */
1009 if( pzBuffer ){
1010 zAlloc = *pzBuffer;
1011 staticFlag = EP_Static;
1012 }else{
1013 zAlloc = sqlite3DbMallocRawNN(db, dupedExprSize(p, dupFlags));
1014 staticFlag = 0;
1015 }
1016 pNew = (Expr *)zAlloc;
danielk19776ab3a2e2009-02-19 14:39:25 +00001017
drh3c194692016-04-11 16:43:43 +00001018 if( pNew ){
1019 /* Set nNewSize to the size allocated for the structure pointed to
1020 ** by pNew. This is either EXPR_FULLSIZE, EXPR_REDUCEDSIZE or
1021 ** EXPR_TOKENONLYSIZE. nToken is set to the number of bytes consumed
1022 ** by the copy of the p->u.zToken string (if any).
1023 */
1024 const unsigned nStructSize = dupedExprStructSize(p, dupFlags);
1025 const int nNewSize = nStructSize & 0xfff;
1026 int nToken;
1027 if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
1028 nToken = sqlite3Strlen30(p->u.zToken) + 1;
danielk19776ab3a2e2009-02-19 14:39:25 +00001029 }else{
drh3c194692016-04-11 16:43:43 +00001030 nToken = 0;
danielk19776ab3a2e2009-02-19 14:39:25 +00001031 }
drh3c194692016-04-11 16:43:43 +00001032 if( dupFlags ){
1033 assert( ExprHasProperty(p, EP_Reduced)==0 );
1034 memcpy(zAlloc, p, nNewSize);
1035 }else{
1036 u32 nSize = (u32)exprStructSize(p);
1037 memcpy(zAlloc, p, nSize);
1038 if( nSize<EXPR_FULLSIZE ){
1039 memset(&zAlloc[nSize], 0, EXPR_FULLSIZE-nSize);
1040 }
1041 }
danielk19776ab3a2e2009-02-19 14:39:25 +00001042
drh3c194692016-04-11 16:43:43 +00001043 /* Set the EP_Reduced, EP_TokenOnly, and EP_Static flags appropriately. */
1044 pNew->flags &= ~(EP_Reduced|EP_TokenOnly|EP_Static|EP_MemToken);
1045 pNew->flags |= nStructSize & (EP_Reduced|EP_TokenOnly);
1046 pNew->flags |= staticFlag;
1047
1048 /* Copy the p->u.zToken string, if any. */
1049 if( nToken ){
1050 char *zToken = pNew->u.zToken = (char*)&zAlloc[nNewSize];
1051 memcpy(zToken, p->u.zToken, nToken);
1052 }
1053
1054 if( 0==((p->flags|pNew->flags) & EP_TokenOnly) ){
1055 /* Fill in the pNew->x.pSelect or pNew->x.pList member. */
1056 if( ExprHasProperty(p, EP_xIsSelect) ){
1057 pNew->x.pSelect = sqlite3SelectDup(db, p->x.pSelect, dupFlags);
drh33e619f2009-05-28 01:00:55 +00001058 }else{
drh3c194692016-04-11 16:43:43 +00001059 pNew->x.pList = sqlite3ExprListDup(db, p->x.pList, dupFlags);
drh33e619f2009-05-28 01:00:55 +00001060 }
drh3c194692016-04-11 16:43:43 +00001061 }
1062
1063 /* Fill in pNew->pLeft and pNew->pRight. */
1064 if( ExprHasProperty(pNew, EP_Reduced|EP_TokenOnly) ){
1065 zAlloc += dupedExprNodeSize(p, dupFlags);
1066 if( ExprHasProperty(pNew, EP_Reduced) ){
1067 pNew->pLeft = p->pLeft ?
1068 exprDup(db, p->pLeft, EXPRDUP_REDUCE, &zAlloc) : 0;
1069 pNew->pRight = p->pRight ?
1070 exprDup(db, p->pRight, EXPRDUP_REDUCE, &zAlloc) : 0;
danielk19776ab3a2e2009-02-19 14:39:25 +00001071 }
drh3c194692016-04-11 16:43:43 +00001072 if( pzBuffer ){
1073 *pzBuffer = zAlloc;
danielk19776ab3a2e2009-02-19 14:39:25 +00001074 }
drh3c194692016-04-11 16:43:43 +00001075 }else{
1076 if( !ExprHasProperty(p, EP_TokenOnly) ){
1077 pNew->pLeft = sqlite3ExprDup(db, p->pLeft, 0);
1078 pNew->pRight = sqlite3ExprDup(db, p->pRight, 0);
danielk19776ab3a2e2009-02-19 14:39:25 +00001079 }
danielk19776ab3a2e2009-02-19 14:39:25 +00001080 }
1081 }
1082 return pNew;
1083}
1084
1085/*
danbfe31e72014-01-15 14:17:31 +00001086** Create and return a deep copy of the object passed as the second
1087** argument. If an OOM condition is encountered, NULL is returned
1088** and the db->mallocFailed flag set.
1089*/
daneede6a52014-01-15 19:42:23 +00001090#ifndef SQLITE_OMIT_CTE
danbfe31e72014-01-15 14:17:31 +00001091static With *withDup(sqlite3 *db, With *p){
dan4e9119d2014-01-13 15:12:23 +00001092 With *pRet = 0;
1093 if( p ){
1094 int nByte = sizeof(*p) + sizeof(p->a[0]) * (p->nCte-1);
1095 pRet = sqlite3DbMallocZero(db, nByte);
1096 if( pRet ){
1097 int i;
1098 pRet->nCte = p->nCte;
1099 for(i=0; i<p->nCte; i++){
1100 pRet->a[i].pSelect = sqlite3SelectDup(db, p->a[i].pSelect, 0);
1101 pRet->a[i].pCols = sqlite3ExprListDup(db, p->a[i].pCols, 0);
1102 pRet->a[i].zName = sqlite3DbStrDup(db, p->a[i].zName);
1103 }
1104 }
1105 }
1106 return pRet;
1107}
daneede6a52014-01-15 19:42:23 +00001108#else
1109# define withDup(x,y) 0
1110#endif
dan4e9119d2014-01-13 15:12:23 +00001111
drha76b5df2002-02-23 02:32:10 +00001112/*
drhff78bd22002-02-27 01:47:11 +00001113** The following group of routines make deep copies of expressions,
1114** expression lists, ID lists, and select statements. The copies can
1115** be deleted (by being passed to their respective ...Delete() routines)
1116** without effecting the originals.
1117**
danielk19774adee202004-05-08 08:23:19 +00001118** The expression list, ID, and source lists return by sqlite3ExprListDup(),
1119** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
drhad3cab52002-05-24 02:04:32 +00001120** by subsequent calls to sqlite*ListAppend() routines.
drhff78bd22002-02-27 01:47:11 +00001121**
drhad3cab52002-05-24 02:04:32 +00001122** Any tables that the SrcList might point to are not duplicated.
danielk19776ab3a2e2009-02-19 14:39:25 +00001123**
drhb7916a72009-05-27 10:31:29 +00001124** The flags parameter contains a combination of the EXPRDUP_XXX flags.
danielk19776ab3a2e2009-02-19 14:39:25 +00001125** If the EXPRDUP_REDUCE flag is set, then the structure returned is a
1126** truncated version of the usual Expr structure that will be stored as
1127** part of the in-memory representation of the database schema.
drhff78bd22002-02-27 01:47:11 +00001128*/
danielk19776ab3a2e2009-02-19 14:39:25 +00001129Expr *sqlite3ExprDup(sqlite3 *db, Expr *p, int flags){
drh72ea29d2015-12-08 16:58:45 +00001130 assert( flags==0 || flags==EXPRDUP_REDUCE );
drh3c194692016-04-11 16:43:43 +00001131 return p ? exprDup(db, p, flags, 0) : 0;
drhff78bd22002-02-27 01:47:11 +00001132}
danielk19776ab3a2e2009-02-19 14:39:25 +00001133ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p, int flags){
drhff78bd22002-02-27 01:47:11 +00001134 ExprList *pNew;
drh145716b2004-09-24 12:24:06 +00001135 struct ExprList_item *pItem, *pOldItem;
drhff78bd22002-02-27 01:47:11 +00001136 int i;
drh575fad62016-02-05 13:38:36 +00001137 assert( db!=0 );
drhff78bd22002-02-27 01:47:11 +00001138 if( p==0 ) return 0;
drh575fad62016-02-05 13:38:36 +00001139 pNew = sqlite3DbMallocRawNN(db, sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +00001140 if( pNew==0 ) return 0;
drhd872bb12012-02-02 01:58:08 +00001141 pNew->nExpr = i = p->nExpr;
1142 if( (flags & EXPRDUP_REDUCE)==0 ) for(i=1; i<p->nExpr; i+=i){}
drh575fad62016-02-05 13:38:36 +00001143 pNew->a = pItem = sqlite3DbMallocRawNN(db, i*sizeof(p->a[0]) );
danielk1977e0048402004-06-15 16:51:01 +00001144 if( pItem==0 ){
drh633e6d52008-07-28 19:34:53 +00001145 sqlite3DbFree(db, pNew);
danielk1977e0048402004-06-15 16:51:01 +00001146 return 0;
1147 }
drh145716b2004-09-24 12:24:06 +00001148 pOldItem = p->a;
1149 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
danielk19776ab3a2e2009-02-19 14:39:25 +00001150 Expr *pOldExpr = pOldItem->pExpr;
drhb5526ea2009-07-16 12:41:05 +00001151 pItem->pExpr = sqlite3ExprDup(db, pOldExpr, flags);
drh17435752007-08-16 04:30:38 +00001152 pItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
drhb7916a72009-05-27 10:31:29 +00001153 pItem->zSpan = sqlite3DbStrDup(db, pOldItem->zSpan);
drh145716b2004-09-24 12:24:06 +00001154 pItem->sortOrder = pOldItem->sortOrder;
drh3e7bc9c2004-02-21 19:17:17 +00001155 pItem->done = 0;
drh2c036cf2013-06-26 00:34:13 +00001156 pItem->bSpanIsTab = pOldItem->bSpanIsTab;
drhc2acc4e2013-11-15 18:15:19 +00001157 pItem->u = pOldItem->u;
drhff78bd22002-02-27 01:47:11 +00001158 }
1159 return pNew;
1160}
danielk197793758c82005-01-21 08:13:14 +00001161
1162/*
1163** If cursors, triggers, views and subqueries are all omitted from
1164** the build, then none of the following routines, except for
1165** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes
1166** called with a NULL argument.
1167*/
danielk19776a67fe82005-02-04 04:07:16 +00001168#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \
1169 || !defined(SQLITE_OMIT_SUBQUERY)
danielk19776ab3a2e2009-02-19 14:39:25 +00001170SrcList *sqlite3SrcListDup(sqlite3 *db, SrcList *p, int flags){
drhad3cab52002-05-24 02:04:32 +00001171 SrcList *pNew;
1172 int i;
drh113088e2003-03-20 01:16:58 +00001173 int nByte;
drh575fad62016-02-05 13:38:36 +00001174 assert( db!=0 );
drhad3cab52002-05-24 02:04:32 +00001175 if( p==0 ) return 0;
drh113088e2003-03-20 01:16:58 +00001176 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
drh575fad62016-02-05 13:38:36 +00001177 pNew = sqlite3DbMallocRawNN(db, nByte );
drhad3cab52002-05-24 02:04:32 +00001178 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +00001179 pNew->nSrc = pNew->nAlloc = p->nSrc;
drhad3cab52002-05-24 02:04:32 +00001180 for(i=0; i<p->nSrc; i++){
drh4efc4752004-01-16 15:55:37 +00001181 struct SrcList_item *pNewItem = &pNew->a[i];
1182 struct SrcList_item *pOldItem = &p->a[i];
drhed8a3bb2005-06-06 21:19:56 +00001183 Table *pTab;
dan41fb5cd2012-10-04 19:33:00 +00001184 pNewItem->pSchema = pOldItem->pSchema;
drh17435752007-08-16 04:30:38 +00001185 pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase);
1186 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
1187 pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias);
drh8a48b9c2015-08-19 15:20:00 +00001188 pNewItem->fg = pOldItem->fg;
drh4efc4752004-01-16 15:55:37 +00001189 pNewItem->iCursor = pOldItem->iCursor;
drh5b6a9ed2011-09-15 23:58:14 +00001190 pNewItem->addrFillSub = pOldItem->addrFillSub;
1191 pNewItem->regReturn = pOldItem->regReturn;
drh8a48b9c2015-08-19 15:20:00 +00001192 if( pNewItem->fg.isIndexedBy ){
1193 pNewItem->u1.zIndexedBy = sqlite3DbStrDup(db, pOldItem->u1.zIndexedBy);
1194 }
1195 pNewItem->pIBIndex = pOldItem->pIBIndex;
1196 if( pNewItem->fg.isTabFunc ){
1197 pNewItem->u1.pFuncArg =
1198 sqlite3ExprListDup(db, pOldItem->u1.pFuncArg, flags);
1199 }
drhed8a3bb2005-06-06 21:19:56 +00001200 pTab = pNewItem->pTab = pOldItem->pTab;
1201 if( pTab ){
1202 pTab->nRef++;
danielk1977a1cb1832005-02-12 08:59:55 +00001203 }
danielk19776ab3a2e2009-02-19 14:39:25 +00001204 pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect, flags);
1205 pNewItem->pOn = sqlite3ExprDup(db, pOldItem->pOn, flags);
drh17435752007-08-16 04:30:38 +00001206 pNewItem->pUsing = sqlite3IdListDup(db, pOldItem->pUsing);
danielk19776c18b6e2005-01-30 09:17:58 +00001207 pNewItem->colUsed = pOldItem->colUsed;
drhad3cab52002-05-24 02:04:32 +00001208 }
1209 return pNew;
1210}
drh17435752007-08-16 04:30:38 +00001211IdList *sqlite3IdListDup(sqlite3 *db, IdList *p){
drhff78bd22002-02-27 01:47:11 +00001212 IdList *pNew;
1213 int i;
drh575fad62016-02-05 13:38:36 +00001214 assert( db!=0 );
drhff78bd22002-02-27 01:47:11 +00001215 if( p==0 ) return 0;
drh575fad62016-02-05 13:38:36 +00001216 pNew = sqlite3DbMallocRawNN(db, sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +00001217 if( pNew==0 ) return 0;
drh6c535152012-02-02 03:38:30 +00001218 pNew->nId = p->nId;
drh575fad62016-02-05 13:38:36 +00001219 pNew->a = sqlite3DbMallocRawNN(db, p->nId*sizeof(p->a[0]) );
danielk1977d5d56522005-03-16 12:15:20 +00001220 if( pNew->a==0 ){
drh633e6d52008-07-28 19:34:53 +00001221 sqlite3DbFree(db, pNew);
danielk1977d5d56522005-03-16 12:15:20 +00001222 return 0;
1223 }
drh6c535152012-02-02 03:38:30 +00001224 /* Note that because the size of the allocation for p->a[] is not
1225 ** necessarily a power of two, sqlite3IdListAppend() may not be called
1226 ** on the duplicate created by this function. */
drhff78bd22002-02-27 01:47:11 +00001227 for(i=0; i<p->nId; i++){
drh4efc4752004-01-16 15:55:37 +00001228 struct IdList_item *pNewItem = &pNew->a[i];
1229 struct IdList_item *pOldItem = &p->a[i];
drh17435752007-08-16 04:30:38 +00001230 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
drh4efc4752004-01-16 15:55:37 +00001231 pNewItem->idx = pOldItem->idx;
drhff78bd22002-02-27 01:47:11 +00001232 }
1233 return pNew;
1234}
danielk19776ab3a2e2009-02-19 14:39:25 +00001235Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){
drh23b1b372011-12-07 01:55:51 +00001236 Select *pNew, *pPrior;
drh575fad62016-02-05 13:38:36 +00001237 assert( db!=0 );
drhff78bd22002-02-27 01:47:11 +00001238 if( p==0 ) return 0;
drh575fad62016-02-05 13:38:36 +00001239 pNew = sqlite3DbMallocRawNN(db, sizeof(*p) );
drhff78bd22002-02-27 01:47:11 +00001240 if( pNew==0 ) return 0;
drhb7916a72009-05-27 10:31:29 +00001241 pNew->pEList = sqlite3ExprListDup(db, p->pEList, flags);
danielk19776ab3a2e2009-02-19 14:39:25 +00001242 pNew->pSrc = sqlite3SrcListDup(db, p->pSrc, flags);
1243 pNew->pWhere = sqlite3ExprDup(db, p->pWhere, flags);
1244 pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags);
1245 pNew->pHaving = sqlite3ExprDup(db, p->pHaving, flags);
1246 pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, flags);
drhff78bd22002-02-27 01:47:11 +00001247 pNew->op = p->op;
drh23b1b372011-12-07 01:55:51 +00001248 pNew->pPrior = pPrior = sqlite3SelectDup(db, p->pPrior, flags);
1249 if( pPrior ) pPrior->pNext = pNew;
1250 pNew->pNext = 0;
danielk19776ab3a2e2009-02-19 14:39:25 +00001251 pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags);
1252 pNew->pOffset = sqlite3ExprDup(db, p->pOffset, flags);
drh92b01d52008-06-24 00:32:35 +00001253 pNew->iLimit = 0;
1254 pNew->iOffset = 0;
drh7d10d5a2008-08-20 16:35:10 +00001255 pNew->selFlags = p->selFlags & ~SF_UsesEphemeral;
drhb9bb7c12006-06-11 23:41:55 +00001256 pNew->addrOpenEphm[0] = -1;
1257 pNew->addrOpenEphm[1] = -1;
drhec2da852014-01-29 01:46:12 +00001258 pNew->nSelectRow = p->nSelectRow;
dan4e9119d2014-01-13 15:12:23 +00001259 pNew->pWith = withDup(db, p->pWith);
drheb9b8842014-09-21 00:27:26 +00001260 sqlite3SelectSetName(pNew, p->zSelName);
drhff78bd22002-02-27 01:47:11 +00001261 return pNew;
1262}
danielk197793758c82005-01-21 08:13:14 +00001263#else
danielk19776ab3a2e2009-02-19 14:39:25 +00001264Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){
danielk197793758c82005-01-21 08:13:14 +00001265 assert( p==0 );
1266 return 0;
1267}
1268#endif
drhff78bd22002-02-27 01:47:11 +00001269
1270
1271/*
drha76b5df2002-02-23 02:32:10 +00001272** Add a new element to the end of an expression list. If pList is
1273** initially NULL, then create a new expression list.
drhb7916a72009-05-27 10:31:29 +00001274**
1275** If a memory allocation error occurs, the entire list is freed and
1276** NULL is returned. If non-NULL is returned, then it is guaranteed
1277** that the new entry was successfully appended.
drha76b5df2002-02-23 02:32:10 +00001278*/
drh17435752007-08-16 04:30:38 +00001279ExprList *sqlite3ExprListAppend(
1280 Parse *pParse, /* Parsing context */
1281 ExprList *pList, /* List to which to append. Might be NULL */
drhb7916a72009-05-27 10:31:29 +00001282 Expr *pExpr /* Expression to be appended. Might be NULL */
drh17435752007-08-16 04:30:38 +00001283){
1284 sqlite3 *db = pParse->db;
drh575fad62016-02-05 13:38:36 +00001285 assert( db!=0 );
drha76b5df2002-02-23 02:32:10 +00001286 if( pList==0 ){
drh575fad62016-02-05 13:38:36 +00001287 pList = sqlite3DbMallocRawNN(db, sizeof(ExprList) );
drha76b5df2002-02-23 02:32:10 +00001288 if( pList==0 ){
danielk1977d5d56522005-03-16 12:15:20 +00001289 goto no_mem;
drha76b5df2002-02-23 02:32:10 +00001290 }
drhc263f7c2016-01-18 13:18:54 +00001291 pList->nExpr = 0;
drh575fad62016-02-05 13:38:36 +00001292 pList->a = sqlite3DbMallocRawNN(db, sizeof(pList->a[0]));
drhd872bb12012-02-02 01:58:08 +00001293 if( pList->a==0 ) goto no_mem;
1294 }else if( (pList->nExpr & (pList->nExpr-1))==0 ){
danielk1977d5d56522005-03-16 12:15:20 +00001295 struct ExprList_item *a;
drhd872bb12012-02-02 01:58:08 +00001296 assert( pList->nExpr>0 );
1297 a = sqlite3DbRealloc(db, pList->a, pList->nExpr*2*sizeof(pList->a[0]));
danielk1977d5d56522005-03-16 12:15:20 +00001298 if( a==0 ){
1299 goto no_mem;
drha76b5df2002-02-23 02:32:10 +00001300 }
danielk1977d5d56522005-03-16 12:15:20 +00001301 pList->a = a;
drha76b5df2002-02-23 02:32:10 +00001302 }
drh4efc4752004-01-16 15:55:37 +00001303 assert( pList->a!=0 );
drhb7916a72009-05-27 10:31:29 +00001304 if( 1 ){
drh4efc4752004-01-16 15:55:37 +00001305 struct ExprList_item *pItem = &pList->a[pList->nExpr++];
1306 memset(pItem, 0, sizeof(*pItem));
danielk1977e94ddc92005-03-21 03:53:38 +00001307 pItem->pExpr = pExpr;
drha76b5df2002-02-23 02:32:10 +00001308 }
1309 return pList;
danielk1977d5d56522005-03-16 12:15:20 +00001310
1311no_mem:
1312 /* Avoid leaking memory if malloc has failed. */
drh633e6d52008-07-28 19:34:53 +00001313 sqlite3ExprDelete(db, pExpr);
1314 sqlite3ExprListDelete(db, pList);
danielk1977d5d56522005-03-16 12:15:20 +00001315 return 0;
drha76b5df2002-02-23 02:32:10 +00001316}
1317
1318/*
drhbc622bc2015-08-24 15:39:42 +00001319** Set the sort order for the last element on the given ExprList.
1320*/
1321void sqlite3ExprListSetSortOrder(ExprList *p, int iSortOrder){
1322 if( p==0 ) return;
1323 assert( SQLITE_SO_UNDEFINED<0 && SQLITE_SO_ASC>=0 && SQLITE_SO_DESC>0 );
1324 assert( p->nExpr>0 );
1325 if( iSortOrder<0 ){
1326 assert( p->a[p->nExpr-1].sortOrder==SQLITE_SO_ASC );
1327 return;
1328 }
1329 p->a[p->nExpr-1].sortOrder = (u8)iSortOrder;
drhbc622bc2015-08-24 15:39:42 +00001330}
1331
1332/*
drhb7916a72009-05-27 10:31:29 +00001333** Set the ExprList.a[].zName element of the most recently added item
1334** on the expression list.
1335**
1336** pList might be NULL following an OOM error. But pName should never be
1337** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag
1338** is set.
1339*/
1340void sqlite3ExprListSetName(
1341 Parse *pParse, /* Parsing context */
1342 ExprList *pList, /* List to which to add the span. */
1343 Token *pName, /* Name to be added */
1344 int dequote /* True to cause the name to be dequoted */
1345){
1346 assert( pList!=0 || pParse->db->mallocFailed!=0 );
1347 if( pList ){
1348 struct ExprList_item *pItem;
1349 assert( pList->nExpr>0 );
1350 pItem = &pList->a[pList->nExpr-1];
1351 assert( pItem->zName==0 );
1352 pItem->zName = sqlite3DbStrNDup(pParse->db, pName->z, pName->n);
drh244b9d62016-04-11 19:01:08 +00001353 if( dequote ) sqlite3Dequote(pItem->zName);
drhb7916a72009-05-27 10:31:29 +00001354 }
1355}
1356
1357/*
1358** Set the ExprList.a[].zSpan element of the most recently added item
1359** on the expression list.
1360**
1361** pList might be NULL following an OOM error. But pSpan should never be
1362** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag
1363** is set.
1364*/
1365void sqlite3ExprListSetSpan(
1366 Parse *pParse, /* Parsing context */
1367 ExprList *pList, /* List to which to add the span. */
1368 ExprSpan *pSpan /* The span to be added */
1369){
1370 sqlite3 *db = pParse->db;
1371 assert( pList!=0 || db->mallocFailed!=0 );
1372 if( pList ){
1373 struct ExprList_item *pItem = &pList->a[pList->nExpr-1];
1374 assert( pList->nExpr>0 );
1375 assert( db->mallocFailed || pItem->pExpr==pSpan->pExpr );
1376 sqlite3DbFree(db, pItem->zSpan);
1377 pItem->zSpan = sqlite3DbStrNDup(db, (char*)pSpan->zStart,
shanecf697392009-06-01 16:53:09 +00001378 (int)(pSpan->zEnd - pSpan->zStart));
drhb7916a72009-05-27 10:31:29 +00001379 }
1380}
1381
1382/*
danielk19777a15a4b2007-05-08 17:54:43 +00001383** If the expression list pEList contains more than iLimit elements,
1384** leave an error message in pParse.
1385*/
1386void sqlite3ExprListCheckLength(
1387 Parse *pParse,
1388 ExprList *pEList,
danielk19777a15a4b2007-05-08 17:54:43 +00001389 const char *zObject
1390){
drhb1a6c3c2008-03-20 16:30:17 +00001391 int mx = pParse->db->aLimit[SQLITE_LIMIT_COLUMN];
drhc5499be2008-04-01 15:06:33 +00001392 testcase( pEList && pEList->nExpr==mx );
1393 testcase( pEList && pEList->nExpr==mx+1 );
drhb1a6c3c2008-03-20 16:30:17 +00001394 if( pEList && pEList->nExpr>mx ){
danielk19777a15a4b2007-05-08 17:54:43 +00001395 sqlite3ErrorMsg(pParse, "too many columns in %s", zObject);
1396 }
1397}
1398
1399/*
drha76b5df2002-02-23 02:32:10 +00001400** Delete an entire expression list.
1401*/
drhaffa8552016-04-11 18:25:05 +00001402static SQLITE_NOINLINE void exprListDeleteNN(sqlite3 *db, ExprList *pList){
drha76b5df2002-02-23 02:32:10 +00001403 int i;
drhbe5c89a2004-07-26 00:31:09 +00001404 struct ExprList_item *pItem;
drhd872bb12012-02-02 01:58:08 +00001405 assert( pList->a!=0 || pList->nExpr==0 );
drhbe5c89a2004-07-26 00:31:09 +00001406 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
drh633e6d52008-07-28 19:34:53 +00001407 sqlite3ExprDelete(db, pItem->pExpr);
1408 sqlite3DbFree(db, pItem->zName);
drhb7916a72009-05-27 10:31:29 +00001409 sqlite3DbFree(db, pItem->zSpan);
drha76b5df2002-02-23 02:32:10 +00001410 }
drh633e6d52008-07-28 19:34:53 +00001411 sqlite3DbFree(db, pList->a);
1412 sqlite3DbFree(db, pList);
drha76b5df2002-02-23 02:32:10 +00001413}
drhaffa8552016-04-11 18:25:05 +00001414void sqlite3ExprListDelete(sqlite3 *db, ExprList *pList){
1415 if( pList ) exprListDeleteNN(db, pList);
1416}
drha76b5df2002-02-23 02:32:10 +00001417
1418/*
drh2308ed32015-02-09 16:09:34 +00001419** Return the bitwise-OR of all Expr.flags fields in the given
1420** ExprList.
drh885a5b02015-02-09 15:21:36 +00001421*/
drh2308ed32015-02-09 16:09:34 +00001422u32 sqlite3ExprListFlags(const ExprList *pList){
drh885a5b02015-02-09 15:21:36 +00001423 int i;
drh2308ed32015-02-09 16:09:34 +00001424 u32 m = 0;
1425 if( pList ){
1426 for(i=0; i<pList->nExpr; i++){
drhd0c73052015-04-19 19:21:19 +00001427 Expr *pExpr = pList->a[i].pExpr;
drhde845c22016-03-17 19:07:52 +00001428 assert( pExpr!=0 );
1429 m |= pExpr->flags;
drh2308ed32015-02-09 16:09:34 +00001430 }
drh885a5b02015-02-09 15:21:36 +00001431 }
drh2308ed32015-02-09 16:09:34 +00001432 return m;
drh885a5b02015-02-09 15:21:36 +00001433}
1434
1435/*
drh059b2d52014-10-24 19:28:09 +00001436** These routines are Walker callbacks used to check expressions to
1437** see if they are "constant" for some definition of constant. The
1438** Walker.eCode value determines the type of "constant" we are looking
1439** for.
drh73b211a2005-01-18 04:00:42 +00001440**
drh7d10d5a2008-08-20 16:35:10 +00001441** These callback routines are used to implement the following:
drh626a8792005-01-17 22:08:19 +00001442**
drh059b2d52014-10-24 19:28:09 +00001443** sqlite3ExprIsConstant() pWalker->eCode==1
1444** sqlite3ExprIsConstantNotJoin() pWalker->eCode==2
drhfcb9f4f2015-06-01 18:13:16 +00001445** sqlite3ExprIsTableConstant() pWalker->eCode==3
drh059b2d52014-10-24 19:28:09 +00001446** sqlite3ExprIsConstantOrFunction() pWalker->eCode==4 or 5
drh87abf5c2005-08-25 12:45:04 +00001447**
drh059b2d52014-10-24 19:28:09 +00001448** In all cases, the callbacks set Walker.eCode=0 and abort if the expression
1449** is found to not be a constant.
drh87abf5c2005-08-25 12:45:04 +00001450**
drhfeada2d2014-09-24 13:20:22 +00001451** The sqlite3ExprIsConstantOrFunction() is used for evaluating expressions
drh059b2d52014-10-24 19:28:09 +00001452** in a CREATE TABLE statement. The Walker.eCode value is 5 when parsing
1453** an existing schema and 4 when processing a new statement. A bound
drhfeada2d2014-09-24 13:20:22 +00001454** parameter raises an error for new statements, but is silently converted
1455** to NULL for existing schemas. This allows sqlite_master tables that
1456** contain a bound parameter because they were generated by older versions
1457** of SQLite to be parsed by newer versions of SQLite without raising a
1458** malformed schema error.
drh626a8792005-01-17 22:08:19 +00001459*/
drh7d10d5a2008-08-20 16:35:10 +00001460static int exprNodeIsConstant(Walker *pWalker, Expr *pExpr){
drh626a8792005-01-17 22:08:19 +00001461
drh059b2d52014-10-24 19:28:09 +00001462 /* If pWalker->eCode is 2 then any term of the expression that comes from
1463 ** the ON or USING clauses of a left join disqualifies the expression
drh0a168372007-06-08 00:20:47 +00001464 ** from being considered constant. */
drh059b2d52014-10-24 19:28:09 +00001465 if( pWalker->eCode==2 && ExprHasProperty(pExpr, EP_FromJoin) ){
1466 pWalker->eCode = 0;
drh7d10d5a2008-08-20 16:35:10 +00001467 return WRC_Abort;
drh0a168372007-06-08 00:20:47 +00001468 }
1469
drh626a8792005-01-17 22:08:19 +00001470 switch( pExpr->op ){
drheb55bd22005-06-30 17:04:21 +00001471 /* Consider functions to be constant if all their arguments are constant
drh059b2d52014-10-24 19:28:09 +00001472 ** and either pWalker->eCode==4 or 5 or the function has the
1473 ** SQLITE_FUNC_CONST flag. */
drheb55bd22005-06-30 17:04:21 +00001474 case TK_FUNCTION:
drh63f84572015-02-09 14:07:07 +00001475 if( pWalker->eCode>=4 || ExprHasProperty(pExpr,EP_ConstFunc) ){
drhb1fba282013-11-21 14:33:48 +00001476 return WRC_Continue;
drh059b2d52014-10-24 19:28:09 +00001477 }else{
1478 pWalker->eCode = 0;
1479 return WRC_Abort;
drhb1fba282013-11-21 14:33:48 +00001480 }
drh626a8792005-01-17 22:08:19 +00001481 case TK_ID:
1482 case TK_COLUMN:
drh626a8792005-01-17 22:08:19 +00001483 case TK_AGG_FUNCTION:
drh13449892005-09-07 21:22:45 +00001484 case TK_AGG_COLUMN:
drhc5499be2008-04-01 15:06:33 +00001485 testcase( pExpr->op==TK_ID );
1486 testcase( pExpr->op==TK_COLUMN );
drhc5499be2008-04-01 15:06:33 +00001487 testcase( pExpr->op==TK_AGG_FUNCTION );
1488 testcase( pExpr->op==TK_AGG_COLUMN );
drh059b2d52014-10-24 19:28:09 +00001489 if( pWalker->eCode==3 && pExpr->iTable==pWalker->u.iCur ){
1490 return WRC_Continue;
1491 }else{
1492 pWalker->eCode = 0;
1493 return WRC_Abort;
1494 }
drhfeada2d2014-09-24 13:20:22 +00001495 case TK_VARIABLE:
drh059b2d52014-10-24 19:28:09 +00001496 if( pWalker->eCode==5 ){
drhfeada2d2014-09-24 13:20:22 +00001497 /* Silently convert bound parameters that appear inside of CREATE
1498 ** statements into a NULL when parsing the CREATE statement text out
1499 ** of the sqlite_master table */
1500 pExpr->op = TK_NULL;
drh059b2d52014-10-24 19:28:09 +00001501 }else if( pWalker->eCode==4 ){
drhfeada2d2014-09-24 13:20:22 +00001502 /* A bound parameter in a CREATE statement that originates from
1503 ** sqlite3_prepare() causes an error */
drh059b2d52014-10-24 19:28:09 +00001504 pWalker->eCode = 0;
drhfeada2d2014-09-24 13:20:22 +00001505 return WRC_Abort;
1506 }
1507 /* Fall through */
drh626a8792005-01-17 22:08:19 +00001508 default:
drhb74b1012009-05-28 21:04:37 +00001509 testcase( pExpr->op==TK_SELECT ); /* selectNodeIsConstant will disallow */
1510 testcase( pExpr->op==TK_EXISTS ); /* selectNodeIsConstant will disallow */
drh7d10d5a2008-08-20 16:35:10 +00001511 return WRC_Continue;
drh626a8792005-01-17 22:08:19 +00001512 }
1513}
danielk197762c14b32008-11-19 09:05:26 +00001514static int selectNodeIsConstant(Walker *pWalker, Select *NotUsed){
1515 UNUSED_PARAMETER(NotUsed);
drh059b2d52014-10-24 19:28:09 +00001516 pWalker->eCode = 0;
drh7d10d5a2008-08-20 16:35:10 +00001517 return WRC_Abort;
1518}
drh059b2d52014-10-24 19:28:09 +00001519static int exprIsConst(Expr *p, int initFlag, int iCur){
drh7d10d5a2008-08-20 16:35:10 +00001520 Walker w;
drhaa87f9a2013-04-25 00:57:10 +00001521 memset(&w, 0, sizeof(w));
drh059b2d52014-10-24 19:28:09 +00001522 w.eCode = initFlag;
drh7d10d5a2008-08-20 16:35:10 +00001523 w.xExprCallback = exprNodeIsConstant;
1524 w.xSelectCallback = selectNodeIsConstant;
drh059b2d52014-10-24 19:28:09 +00001525 w.u.iCur = iCur;
drh7d10d5a2008-08-20 16:35:10 +00001526 sqlite3WalkExpr(&w, p);
drh059b2d52014-10-24 19:28:09 +00001527 return w.eCode;
drh7d10d5a2008-08-20 16:35:10 +00001528}
drh626a8792005-01-17 22:08:19 +00001529
1530/*
drh059b2d52014-10-24 19:28:09 +00001531** Walk an expression tree. Return non-zero if the expression is constant
drheb55bd22005-06-30 17:04:21 +00001532** and 0 if it involves variables or function calls.
drh23989372002-05-21 13:43:04 +00001533**
1534** For the purposes of this function, a double-quoted string (ex: "abc")
1535** is considered a variable but a single-quoted string (ex: 'abc') is
1536** a constant.
drhfef52082000-06-06 01:50:43 +00001537*/
danielk19774adee202004-05-08 08:23:19 +00001538int sqlite3ExprIsConstant(Expr *p){
drh059b2d52014-10-24 19:28:09 +00001539 return exprIsConst(p, 1, 0);
drhfef52082000-06-06 01:50:43 +00001540}
1541
1542/*
drh059b2d52014-10-24 19:28:09 +00001543** Walk an expression tree. Return non-zero if the expression is constant
drh0a168372007-06-08 00:20:47 +00001544** that does no originate from the ON or USING clauses of a join.
1545** Return 0 if it involves variables or function calls or terms from
1546** an ON or USING clause.
1547*/
1548int sqlite3ExprIsConstantNotJoin(Expr *p){
drh059b2d52014-10-24 19:28:09 +00001549 return exprIsConst(p, 2, 0);
drh0a168372007-06-08 00:20:47 +00001550}
1551
1552/*
drhfcb9f4f2015-06-01 18:13:16 +00001553** Walk an expression tree. Return non-zero if the expression is constant
drh059b2d52014-10-24 19:28:09 +00001554** for any single row of the table with cursor iCur. In other words, the
1555** expression must not refer to any non-deterministic function nor any
1556** table other than iCur.
1557*/
1558int sqlite3ExprIsTableConstant(Expr *p, int iCur){
1559 return exprIsConst(p, 3, iCur);
1560}
1561
1562/*
1563** Walk an expression tree. Return non-zero if the expression is constant
drheb55bd22005-06-30 17:04:21 +00001564** or a function call with constant arguments. Return and 0 if there
1565** are any variables.
1566**
1567** For the purposes of this function, a double-quoted string (ex: "abc")
1568** is considered a variable but a single-quoted string (ex: 'abc') is
1569** a constant.
1570*/
drhfeada2d2014-09-24 13:20:22 +00001571int sqlite3ExprIsConstantOrFunction(Expr *p, u8 isInit){
1572 assert( isInit==0 || isInit==1 );
drh059b2d52014-10-24 19:28:09 +00001573 return exprIsConst(p, 4+isInit, 0);
drheb55bd22005-06-30 17:04:21 +00001574}
1575
drh5b88bc42013-12-07 23:35:21 +00001576#ifdef SQLITE_ENABLE_CURSOR_HINTS
1577/*
1578** Walk an expression tree. Return 1 if the expression contains a
1579** subquery of some kind. Return 0 if there are no subqueries.
1580*/
1581int sqlite3ExprContainsSubquery(Expr *p){
1582 Walker w;
1583 memset(&w, 0, sizeof(w));
drhbec24762015-08-13 20:07:13 +00001584 w.eCode = 1;
drh5b88bc42013-12-07 23:35:21 +00001585 w.xExprCallback = sqlite3ExprWalkNoop;
1586 w.xSelectCallback = selectNodeIsConstant;
1587 sqlite3WalkExpr(&w, p);
drh07194bf2015-08-13 20:34:41 +00001588 return w.eCode==0;
drh5b88bc42013-12-07 23:35:21 +00001589}
1590#endif
1591
drheb55bd22005-06-30 17:04:21 +00001592/*
drh73b211a2005-01-18 04:00:42 +00001593** If the expression p codes a constant integer that is small enough
drh202b2df2004-01-06 01:13:46 +00001594** to fit in a 32-bit integer, return 1 and put the value of the integer
1595** in *pValue. If the expression is not an integer or if it is too big
1596** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
drhe4de1fe2002-06-02 16:09:01 +00001597*/
danielk19774adee202004-05-08 08:23:19 +00001598int sqlite3ExprIsInteger(Expr *p, int *pValue){
drh92b01d52008-06-24 00:32:35 +00001599 int rc = 0;
drhcd92e842011-02-17 15:58:20 +00001600
1601 /* If an expression is an integer literal that fits in a signed 32-bit
1602 ** integer, then the EP_IntValue flag will have already been set */
1603 assert( p->op!=TK_INTEGER || (p->flags & EP_IntValue)!=0
1604 || sqlite3GetInt32(p->u.zToken, &rc)==0 );
1605
drh92b01d52008-06-24 00:32:35 +00001606 if( p->flags & EP_IntValue ){
drh33e619f2009-05-28 01:00:55 +00001607 *pValue = p->u.iValue;
drh92b01d52008-06-24 00:32:35 +00001608 return 1;
1609 }
drhe4de1fe2002-06-02 16:09:01 +00001610 switch( p->op ){
drh4b59ab52002-08-24 18:24:51 +00001611 case TK_UPLUS: {
drh92b01d52008-06-24 00:32:35 +00001612 rc = sqlite3ExprIsInteger(p->pLeft, pValue);
drhf6e369a2008-06-24 12:46:30 +00001613 break;
drh4b59ab52002-08-24 18:24:51 +00001614 }
drhe4de1fe2002-06-02 16:09:01 +00001615 case TK_UMINUS: {
1616 int v;
danielk19774adee202004-05-08 08:23:19 +00001617 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
mistachkinf6418892013-08-28 01:54:12 +00001618 assert( v!=(-2147483647-1) );
drhe4de1fe2002-06-02 16:09:01 +00001619 *pValue = -v;
drh92b01d52008-06-24 00:32:35 +00001620 rc = 1;
drhe4de1fe2002-06-02 16:09:01 +00001621 }
1622 break;
1623 }
1624 default: break;
1625 }
drh92b01d52008-06-24 00:32:35 +00001626 return rc;
drhe4de1fe2002-06-02 16:09:01 +00001627}
1628
1629/*
drh039fc322009-11-17 18:31:47 +00001630** Return FALSE if there is no chance that the expression can be NULL.
1631**
1632** If the expression might be NULL or if the expression is too complex
1633** to tell return TRUE.
1634**
1635** This routine is used as an optimization, to skip OP_IsNull opcodes
1636** when we know that a value cannot be NULL. Hence, a false positive
1637** (returning TRUE when in fact the expression can never be NULL) might
1638** be a small performance hit but is otherwise harmless. On the other
1639** hand, a false negative (returning FALSE when the result could be NULL)
1640** will likely result in an incorrect answer. So when in doubt, return
1641** TRUE.
1642*/
1643int sqlite3ExprCanBeNull(const Expr *p){
1644 u8 op;
drhcd7f4572009-11-19 14:48:40 +00001645 while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ p = p->pLeft; }
drh039fc322009-11-17 18:31:47 +00001646 op = p->op;
1647 if( op==TK_REGISTER ) op = p->op2;
1648 switch( op ){
1649 case TK_INTEGER:
1650 case TK_STRING:
1651 case TK_FLOAT:
1652 case TK_BLOB:
1653 return 0;
drh7248a8b2014-08-04 18:50:54 +00001654 case TK_COLUMN:
1655 assert( p->pTab!=0 );
drh72673a22014-12-04 16:27:17 +00001656 return ExprHasProperty(p, EP_CanBeNull) ||
1657 (p->iColumn>=0 && p->pTab->aCol[p->iColumn].notNull==0);
drh039fc322009-11-17 18:31:47 +00001658 default:
1659 return 1;
1660 }
1661}
1662
1663/*
1664** Return TRUE if the given expression is a constant which would be
1665** unchanged by OP_Affinity with the affinity given in the second
1666** argument.
1667**
1668** This routine is used to determine if the OP_Affinity operation
1669** can be omitted. When in doubt return FALSE. A false negative
1670** is harmless. A false positive, however, can result in the wrong
1671** answer.
1672*/
1673int sqlite3ExprNeedsNoAffinityChange(const Expr *p, char aff){
1674 u8 op;
drh05883a32015-06-02 15:32:08 +00001675 if( aff==SQLITE_AFF_BLOB ) return 1;
drhcd7f4572009-11-19 14:48:40 +00001676 while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ p = p->pLeft; }
drh039fc322009-11-17 18:31:47 +00001677 op = p->op;
1678 if( op==TK_REGISTER ) op = p->op2;
1679 switch( op ){
1680 case TK_INTEGER: {
1681 return aff==SQLITE_AFF_INTEGER || aff==SQLITE_AFF_NUMERIC;
1682 }
1683 case TK_FLOAT: {
1684 return aff==SQLITE_AFF_REAL || aff==SQLITE_AFF_NUMERIC;
1685 }
1686 case TK_STRING: {
1687 return aff==SQLITE_AFF_TEXT;
1688 }
1689 case TK_BLOB: {
1690 return 1;
1691 }
drh2f2855b2009-11-18 01:25:26 +00001692 case TK_COLUMN: {
drh88376ca2009-11-19 15:44:53 +00001693 assert( p->iTable>=0 ); /* p cannot be part of a CHECK constraint */
1694 return p->iColumn<0
1695 && (aff==SQLITE_AFF_INTEGER || aff==SQLITE_AFF_NUMERIC);
drh2f2855b2009-11-18 01:25:26 +00001696 }
drh039fc322009-11-17 18:31:47 +00001697 default: {
1698 return 0;
1699 }
1700 }
1701}
1702
1703/*
drhc4a3c772001-04-04 11:48:57 +00001704** Return TRUE if the given string is a row-id column name.
1705*/
danielk19774adee202004-05-08 08:23:19 +00001706int sqlite3IsRowid(const char *z){
1707 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
1708 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
1709 if( sqlite3StrICmp(z, "OID")==0 ) return 1;
drhc4a3c772001-04-04 11:48:57 +00001710 return 0;
1711}
1712
danielk19779a96b662007-11-29 17:05:18 +00001713/*
drh69c355b2016-03-09 15:34:51 +00001714** pX is the RHS of an IN operator. If pX is a SELECT statement
1715** that can be simplified to a direct table access, then return
1716** a pointer to the SELECT statement. If pX is not a SELECT statement,
1717** or if the SELECT statement needs to be manifested into a transient
1718** table, then return NULL.
danba00e302016-07-23 20:24:06 +00001719**
1720** If parameter bNullSensitive is 0, then this operation will be
1721** used in a context in which there is no difference between a result
1722** of 0 and one of NULL. For example:
1723**
1724** ... WHERE (?,?) IN (SELECT ...)
1725**
drhb287f4b2008-04-25 00:08:38 +00001726*/
1727#ifndef SQLITE_OMIT_SUBQUERY
dancfbb5e82016-07-13 19:48:13 +00001728static Select *isCandidateForInOpt(Expr *pX, int bNullSensitive){
drh69c355b2016-03-09 15:34:51 +00001729 Select *p;
drhb287f4b2008-04-25 00:08:38 +00001730 SrcList *pSrc;
1731 ExprList *pEList;
1732 Table *pTab;
dancfbb5e82016-07-13 19:48:13 +00001733 int i;
drh69c355b2016-03-09 15:34:51 +00001734 if( !ExprHasProperty(pX, EP_xIsSelect) ) return 0; /* Not a subquery */
1735 if( ExprHasProperty(pX, EP_VarSelect) ) return 0; /* Correlated subq */
1736 p = pX->x.pSelect;
drhb287f4b2008-04-25 00:08:38 +00001737 if( p->pPrior ) return 0; /* Not a compound SELECT */
drh7d10d5a2008-08-20 16:35:10 +00001738 if( p->selFlags & (SF_Distinct|SF_Aggregate) ){
drhb74b1012009-05-28 21:04:37 +00001739 testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
1740 testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
1741 return 0; /* No DISTINCT keyword and no aggregate functions */
drh7d10d5a2008-08-20 16:35:10 +00001742 }
drhb74b1012009-05-28 21:04:37 +00001743 assert( p->pGroupBy==0 ); /* Has no GROUP BY clause */
drhb287f4b2008-04-25 00:08:38 +00001744 if( p->pLimit ) return 0; /* Has no LIMIT clause */
drhb74b1012009-05-28 21:04:37 +00001745 assert( p->pOffset==0 ); /* No LIMIT means no OFFSET */
drhb287f4b2008-04-25 00:08:38 +00001746 if( p->pWhere ) return 0; /* Has no WHERE clause */
1747 pSrc = p->pSrc;
drhd1fa7bc2009-01-10 13:24:50 +00001748 assert( pSrc!=0 );
1749 if( pSrc->nSrc!=1 ) return 0; /* Single term in FROM clause */
drhb74b1012009-05-28 21:04:37 +00001750 if( pSrc->a[0].pSelect ) return 0; /* FROM is not a subquery or view */
drhb287f4b2008-04-25 00:08:38 +00001751 pTab = pSrc->a[0].pTab;
drh69c355b2016-03-09 15:34:51 +00001752 assert( pTab!=0 );
drhb74b1012009-05-28 21:04:37 +00001753 assert( pTab->pSelect==0 ); /* FROM clause is not a view */
drhb287f4b2008-04-25 00:08:38 +00001754 if( IsVirtual(pTab) ) return 0; /* FROM clause not a virtual table */
1755 pEList = p->pEList;
dancfbb5e82016-07-13 19:48:13 +00001756
1757 /* All SELECT results must be columns. If the SELECT returns more than
1758 ** one column and the bNullSensitive flag is set, all returned columns
1759 ** must be declared NOT NULL. */
1760 for(i=0; i<pEList->nExpr; i++){
1761 Expr *pRes = pEList->a[i].pExpr;
1762 if( pRes->op!=TK_COLUMN ) return 0;
1763 assert( pRes->iTable==pSrc->a[0].iCursor ); /* Not a correlated subquery */
1764 if( pEList->nExpr>1 && bNullSensitive ){
1765 if( pTab->aCol[pRes->iColumn].notNull==0 ) return 0;
1766 }
1767 }
drh69c355b2016-03-09 15:34:51 +00001768 return p;
drhb287f4b2008-04-25 00:08:38 +00001769}
1770#endif /* SQLITE_OMIT_SUBQUERY */
1771
1772/*
dan1d8cb212011-12-09 13:24:16 +00001773** Code an OP_Once instruction and allocate space for its flag. Return the
1774** address of the new instruction.
1775*/
1776int sqlite3CodeOnce(Parse *pParse){
1777 Vdbe *v = sqlite3GetVdbe(pParse); /* Virtual machine being coded */
1778 return sqlite3VdbeAddOp1(v, OP_Once, pParse->nOnce++);
1779}
1780
1781/*
drh4c259e92014-08-01 21:12:35 +00001782** Generate code that checks the left-most column of index table iCur to see if
1783** it contains any NULL entries. Cause the register at regHasNull to be set
drh6be515e2014-08-01 21:00:53 +00001784** to a non-NULL value if iCur contains no NULLs. Cause register regHasNull
1785** to be set to NULL if iCur contains one or more NULL values.
1786*/
1787static void sqlite3SetHasNullFlag(Vdbe *v, int iCur, int regHasNull){
drh728e0f92015-10-10 14:41:28 +00001788 int addr1;
drh6be515e2014-08-01 21:00:53 +00001789 sqlite3VdbeAddOp2(v, OP_Integer, 0, regHasNull);
drh728e0f92015-10-10 14:41:28 +00001790 addr1 = sqlite3VdbeAddOp1(v, OP_Rewind, iCur); VdbeCoverage(v);
drh6be515e2014-08-01 21:00:53 +00001791 sqlite3VdbeAddOp3(v, OP_Column, iCur, 0, regHasNull);
1792 sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG);
drh4c259e92014-08-01 21:12:35 +00001793 VdbeComment((v, "first_entry_in(%d)", iCur));
drh728e0f92015-10-10 14:41:28 +00001794 sqlite3VdbeJumpHere(v, addr1);
drh6be515e2014-08-01 21:00:53 +00001795}
1796
drhbb53ecb2014-08-02 21:03:33 +00001797
1798#ifndef SQLITE_OMIT_SUBQUERY
1799/*
1800** The argument is an IN operator with a list (not a subquery) on the
1801** right-hand side. Return TRUE if that list is constant.
1802*/
1803static int sqlite3InRhsIsConstant(Expr *pIn){
1804 Expr *pLHS;
1805 int res;
1806 assert( !ExprHasProperty(pIn, EP_xIsSelect) );
1807 pLHS = pIn->pLeft;
1808 pIn->pLeft = 0;
1809 res = sqlite3ExprIsConstant(pIn);
1810 pIn->pLeft = pLHS;
1811 return res;
1812}
1813#endif
1814
drh6be515e2014-08-01 21:00:53 +00001815/*
danielk19779a96b662007-11-29 17:05:18 +00001816** This function is used by the implementation of the IN (...) operator.
drhd4305ca2012-09-18 17:08:33 +00001817** The pX parameter is the expression on the RHS of the IN operator, which
1818** might be either a list of expressions or a subquery.
danielk19779a96b662007-11-29 17:05:18 +00001819**
drhd4305ca2012-09-18 17:08:33 +00001820** The job of this routine is to find or create a b-tree object that can
1821** be used either to test for membership in the RHS set or to iterate through
1822** all members of the RHS set, skipping duplicates.
1823**
drh3a856252014-08-01 14:46:57 +00001824** A cursor is opened on the b-tree object that is the RHS of the IN operator
drhd4305ca2012-09-18 17:08:33 +00001825** and pX->iTable is set to the index of that cursor.
1826**
drhb74b1012009-05-28 21:04:37 +00001827** The returned value of this function indicates the b-tree type, as follows:
danielk19779a96b662007-11-29 17:05:18 +00001828**
drh1ccce442013-03-12 20:38:51 +00001829** IN_INDEX_ROWID - The cursor was opened on a database table.
1830** IN_INDEX_INDEX_ASC - The cursor was opened on an ascending index.
1831** IN_INDEX_INDEX_DESC - The cursor was opened on a descending index.
1832** IN_INDEX_EPH - The cursor was opened on a specially created and
1833** populated epheremal table.
drhbb53ecb2014-08-02 21:03:33 +00001834** IN_INDEX_NOOP - No cursor was allocated. The IN operator must be
1835** implemented as a sequence of comparisons.
danielk19779a96b662007-11-29 17:05:18 +00001836**
drhd4305ca2012-09-18 17:08:33 +00001837** An existing b-tree might be used if the RHS expression pX is a simple
1838** subquery such as:
danielk19779a96b662007-11-29 17:05:18 +00001839**
1840** SELECT <column> FROM <table>
1841**
drhd4305ca2012-09-18 17:08:33 +00001842** If the RHS of the IN operator is a list or a more complex subquery, then
1843** an ephemeral table might need to be generated from the RHS and then
peter.d.reid60ec9142014-09-06 16:39:46 +00001844** pX->iTable made to point to the ephemeral table instead of an
drh3a856252014-08-01 14:46:57 +00001845** existing table.
drhd4305ca2012-09-18 17:08:33 +00001846**
drh3a856252014-08-01 14:46:57 +00001847** The inFlags parameter must contain exactly one of the bits
1848** IN_INDEX_MEMBERSHIP or IN_INDEX_LOOP. If inFlags contains
1849** IN_INDEX_MEMBERSHIP, then the generated table will be used for a
1850** fast membership test. When the IN_INDEX_LOOP bit is set, the
1851** IN index will be used to loop over all values of the RHS of the
1852** IN operator.
1853**
1854** When IN_INDEX_LOOP is used (and the b-tree will be used to iterate
1855** through the set members) then the b-tree must not contain duplicates.
1856** An epheremal table must be used unless the selected <column> is guaranteed
danielk19779a96b662007-11-29 17:05:18 +00001857** to be unique - either because it is an INTEGER PRIMARY KEY or it
drhb74b1012009-05-28 21:04:37 +00001858** has a UNIQUE constraint or UNIQUE index.
danielk19770cdc0222008-06-26 18:04:03 +00001859**
drh3a856252014-08-01 14:46:57 +00001860** When IN_INDEX_MEMBERSHIP is used (and the b-tree will be used
1861** for fast set membership tests) then an epheremal table must
danielk19770cdc0222008-06-26 18:04:03 +00001862** be used unless <column> is an INTEGER PRIMARY KEY or an index can
1863** be found with <column> as its left-most column.
1864**
drhbb53ecb2014-08-02 21:03:33 +00001865** If the IN_INDEX_NOOP_OK and IN_INDEX_MEMBERSHIP are both set and
1866** if the RHS of the IN operator is a list (not a subquery) then this
1867** routine might decide that creating an ephemeral b-tree for membership
1868** testing is too expensive and return IN_INDEX_NOOP. In that case, the
1869** calling routine should implement the IN operator using a sequence
1870** of Eq or Ne comparison operations.
1871**
drhb74b1012009-05-28 21:04:37 +00001872** When the b-tree is being used for membership tests, the calling function
drh3a856252014-08-01 14:46:57 +00001873** might need to know whether or not the RHS side of the IN operator
drhe21a6e12014-08-01 18:00:24 +00001874** contains a NULL. If prRhsHasNull is not a NULL pointer and
drh3a856252014-08-01 14:46:57 +00001875** if there is any chance that the (...) might contain a NULL value at
danielk19770cdc0222008-06-26 18:04:03 +00001876** runtime, then a register is allocated and the register number written
drhe21a6e12014-08-01 18:00:24 +00001877** to *prRhsHasNull. If there is no chance that the (...) contains a
1878** NULL value, then *prRhsHasNull is left unchanged.
danielk19770cdc0222008-06-26 18:04:03 +00001879**
drhe21a6e12014-08-01 18:00:24 +00001880** If a register is allocated and its location stored in *prRhsHasNull, then
drh6be515e2014-08-01 21:00:53 +00001881** the value in that register will be NULL if the b-tree contains one or more
1882** NULL values, and it will be some non-NULL value if the b-tree contains no
1883** NULL values.
danielk19779a96b662007-11-29 17:05:18 +00001884*/
danielk1977284f4ac2007-12-10 05:03:46 +00001885#ifndef SQLITE_OMIT_SUBQUERY
danba00e302016-07-23 20:24:06 +00001886int sqlite3FindInIndex(
1887 Parse *pParse,
1888 Expr *pX,
1889 u32 inFlags,
1890 int *prRhsHasNull,
1891 int *aiMap
1892){
drhb74b1012009-05-28 21:04:37 +00001893 Select *p; /* SELECT to the right of IN operator */
1894 int eType = 0; /* Type of RHS table. IN_INDEX_* */
1895 int iTab = pParse->nTab++; /* Cursor of the RHS table */
drh3a856252014-08-01 14:46:57 +00001896 int mustBeUnique; /* True if RHS must be unique */
drhb8475df2011-12-09 16:21:19 +00001897 Vdbe *v = sqlite3GetVdbe(pParse); /* Virtual machine being coded */
danielk19779a96b662007-11-29 17:05:18 +00001898
drh1450bc62009-10-30 13:25:56 +00001899 assert( pX->op==TK_IN );
drh3a856252014-08-01 14:46:57 +00001900 mustBeUnique = (inFlags & IN_INDEX_LOOP)!=0;
drh1450bc62009-10-30 13:25:56 +00001901
drhb74b1012009-05-28 21:04:37 +00001902 /* Check to see if an existing table or index can be used to
1903 ** satisfy the query. This is preferable to generating a new
1904 ** ephemeral table.
danielk19779a96b662007-11-29 17:05:18 +00001905 */
dancfbb5e82016-07-13 19:48:13 +00001906 if( pParse->nErr==0 && (p = isCandidateForInOpt(pX, prRhsHasNull!=0))!=0 ){
danielk1977e1fb65a2009-04-02 17:23:32 +00001907 sqlite3 *db = pParse->db; /* Database connection */
drhb07028f2011-10-14 21:49:18 +00001908 Table *pTab; /* Table <table>. */
danba00e302016-07-23 20:24:06 +00001909 i16 iDb; /* Database idx for pTab */
dancfbb5e82016-07-13 19:48:13 +00001910 ExprList *pEList = p->pEList;
1911 int nExpr = pEList->nExpr;
drhb07028f2011-10-14 21:49:18 +00001912
drhb07028f2011-10-14 21:49:18 +00001913 assert( p->pEList!=0 ); /* Because of isCandidateForInOpt(p) */
1914 assert( p->pEList->a[0].pExpr!=0 ); /* Because of isCandidateForInOpt(p) */
1915 assert( p->pSrc!=0 ); /* Because of isCandidateForInOpt(p) */
1916 pTab = p->pSrc->a[0].pTab;
dancfbb5e82016-07-13 19:48:13 +00001917
drhb22f7c82014-02-06 23:56:27 +00001918 /* Code an OP_Transaction and OP_TableLock for <table>. */
danielk1977e1fb65a2009-04-02 17:23:32 +00001919 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
1920 sqlite3CodeVerifySchema(pParse, iDb);
1921 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
danielk19779a96b662007-11-29 17:05:18 +00001922
1923 /* This function is only called from two places. In both cases the vdbe
1924 ** has already been allocated. So assume sqlite3GetVdbe() is always
1925 ** successful here.
1926 */
1927 assert(v);
dancfbb5e82016-07-13 19:48:13 +00001928 if( nExpr==1 && pEList->a[0].pExpr->iColumn<0 ){
drh7d176102014-02-18 03:07:12 +00001929 int iAddr = sqlite3CodeOnce(pParse);
1930 VdbeCoverage(v);
danielk19779a96b662007-11-29 17:05:18 +00001931
1932 sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
1933 eType = IN_INDEX_ROWID;
1934
1935 sqlite3VdbeJumpHere(v, iAddr);
1936 }else{
danielk1977e1fb65a2009-04-02 17:23:32 +00001937 Index *pIdx; /* Iterator variable */
dancfbb5e82016-07-13 19:48:13 +00001938 int affinity_ok = 1;
1939 int i;
1940
1941 /* Check that the affinity that will be used to perform each
1942 ** comparison is the same as the affinity of each column. If
1943 ** it not, it is not possible to use any index. */
1944 for(i=0; i<nExpr && affinity_ok; i++){
1945 Expr *pLhs = exprVectorField(pX->pLeft, i);
1946 int iCol = pEList->a[i].pExpr->iColumn;
1947 char idxaff = pTab->aCol[iCol].affinity;
1948 char cmpaff = sqlite3CompareAffinity(pLhs, idxaff);
1949 switch( cmpaff ){
1950 case SQLITE_AFF_BLOB:
1951 break;
1952 case SQLITE_AFF_TEXT:
1953 affinity_ok = (idxaff==SQLITE_AFF_TEXT);
1954 break;
1955 default:
1956 affinity_ok = sqlite3IsNumericAffinity(idxaff);
1957 }
1958 }
danielk1977e1fb65a2009-04-02 17:23:32 +00001959
drhb74b1012009-05-28 21:04:37 +00001960 /* The collation sequence used by the comparison. If an index is to
danielk19779a96b662007-11-29 17:05:18 +00001961 ** be used in place of a temp-table, it must be ordered according
danielk1977e1fb65a2009-04-02 17:23:32 +00001962 ** to this collation sequence. */
danielk19779a96b662007-11-29 17:05:18 +00001963
1964 for(pIdx=pTab->pIndex; pIdx && eType==0 && affinity_ok; pIdx=pIdx->pNext){
dancfbb5e82016-07-13 19:48:13 +00001965 if( pIdx->nKeyCol<nExpr ) continue;
1966 if( mustBeUnique && (pIdx->nKeyCol!=nExpr || !IsUniqueIndex(pIdx)) ){
1967 continue;
1968 }
1969
1970 for(i=0; i<nExpr; i++){
1971 Expr *pLhs = exprVectorField(pX->pLeft, i);
1972 Expr *pRhs = pEList->a[i].pExpr;
1973 CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pLhs, pRhs);
1974 int j;
1975
1976 for(j=0; j<nExpr; j++){
1977 if( pIdx->aiColumn[j]!=pRhs->iColumn ) continue;
1978 assert( pIdx->azColl[j] );
1979 if( sqlite3StrICmp(pReq->zName, pIdx->azColl[j])!=0 ) continue;
1980 break;
1981 }
1982 if( j==nExpr ) break;
danba00e302016-07-23 20:24:06 +00001983 if( aiMap ) aiMap[i] = j;
dancfbb5e82016-07-13 19:48:13 +00001984 }
1985
1986 if( i==nExpr ){
drh7d176102014-02-18 03:07:12 +00001987 int iAddr = sqlite3CodeOnce(pParse); VdbeCoverage(v);
drh2ec2fb22013-11-06 19:59:23 +00001988 sqlite3VdbeAddOp3(v, OP_OpenRead, iTab, pIdx->tnum, iDb);
1989 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
danielk1977207872a2008-01-03 07:54:23 +00001990 VdbeComment((v, "%s", pIdx->zName));
drh1ccce442013-03-12 20:38:51 +00001991 assert( IN_INDEX_INDEX_DESC == IN_INDEX_INDEX_ASC+1 );
1992 eType = IN_INDEX_INDEX_ASC + pIdx->aSortOrder[0];
danielk19779a96b662007-11-29 17:05:18 +00001993
dancfbb5e82016-07-13 19:48:13 +00001994 if( prRhsHasNull && nExpr==1
1995 && !pTab->aCol[pEList->a[0].pExpr->iColumn].notNull
1996 ){
dan3480bfd2016-06-16 17:14:02 +00001997#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
dancfbb5e82016-07-13 19:48:13 +00001998 i64 mask = (1<<nExpr)-1;
dan3480bfd2016-06-16 17:14:02 +00001999 sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed,
dancfbb5e82016-07-13 19:48:13 +00002000 iTab, 0, 0, (u8*)&mask, P4_INT64);
dan3480bfd2016-06-16 17:14:02 +00002001#endif
drhe21a6e12014-08-01 18:00:24 +00002002 *prRhsHasNull = ++pParse->nMem;
drh6be515e2014-08-01 21:00:53 +00002003 sqlite3SetHasNullFlag(v, iTab, *prRhsHasNull);
danielk19770cdc0222008-06-26 18:04:03 +00002004 }
drh552fd452014-02-18 01:07:38 +00002005 sqlite3VdbeJumpHere(v, iAddr);
danielk19779a96b662007-11-29 17:05:18 +00002006 }
2007 }
2008 }
2009 }
2010
drhbb53ecb2014-08-02 21:03:33 +00002011 /* If no preexisting index is available for the IN clause
2012 ** and IN_INDEX_NOOP is an allowed reply
2013 ** and the RHS of the IN operator is a list, not a subquery
dan71c57db2016-07-09 20:23:55 +00002014 ** and the RHS is not constant or has two or fewer terms,
peter.d.reid60ec9142014-09-06 16:39:46 +00002015 ** then it is not worth creating an ephemeral table to evaluate
drhbb53ecb2014-08-02 21:03:33 +00002016 ** the IN operator so return IN_INDEX_NOOP.
2017 */
2018 if( eType==0
2019 && (inFlags & IN_INDEX_NOOP_OK)
2020 && !ExprHasProperty(pX, EP_xIsSelect)
2021 && (!sqlite3InRhsIsConstant(pX) || pX->x.pList->nExpr<=2)
2022 ){
2023 eType = IN_INDEX_NOOP;
2024 }
drhbb53ecb2014-08-02 21:03:33 +00002025
danielk19779a96b662007-11-29 17:05:18 +00002026 if( eType==0 ){
drh43870062014-07-31 15:44:44 +00002027 /* Could not find an existing table or index to use as the RHS b-tree.
drhb74b1012009-05-28 21:04:37 +00002028 ** We will have to generate an ephemeral table to do the job.
2029 */
drh8e23daf2013-06-11 13:30:04 +00002030 u32 savedNQueryLoop = pParse->nQueryLoop;
danielk19770cdc0222008-06-26 18:04:03 +00002031 int rMayHaveNull = 0;
danielk197741a05b72008-10-02 13:50:55 +00002032 eType = IN_INDEX_EPH;
drh3a856252014-08-01 14:46:57 +00002033 if( inFlags & IN_INDEX_LOOP ){
drh4a5acf82013-06-18 20:06:23 +00002034 pParse->nQueryLoop = 0;
drhc5cd1242013-09-12 16:50:49 +00002035 if( pX->pLeft->iColumn<0 && !ExprHasProperty(pX, EP_xIsSelect) ){
drhcf4d38a2010-07-28 02:53:36 +00002036 eType = IN_INDEX_ROWID;
2037 }
drhe21a6e12014-08-01 18:00:24 +00002038 }else if( prRhsHasNull ){
2039 *prRhsHasNull = rMayHaveNull = ++pParse->nMem;
danielk19770cdc0222008-06-26 18:04:03 +00002040 }
danielk197741a05b72008-10-02 13:50:55 +00002041 sqlite3CodeSubselect(pParse, pX, rMayHaveNull, eType==IN_INDEX_ROWID);
drhcf4d38a2010-07-28 02:53:36 +00002042 pParse->nQueryLoop = savedNQueryLoop;
danielk19779a96b662007-11-29 17:05:18 +00002043 }else{
2044 pX->iTable = iTab;
2045 }
danba00e302016-07-23 20:24:06 +00002046
2047 if( aiMap && eType!=IN_INDEX_INDEX_ASC && eType!=IN_INDEX_INDEX_DESC ){
2048 int i, n;
2049 n = sqlite3ExprVectorSize(pX->pLeft);
2050 for(i=0; i<n; i++) aiMap[i] = i;
2051 }
danielk19779a96b662007-11-29 17:05:18 +00002052 return eType;
2053}
danielk1977284f4ac2007-12-10 05:03:46 +00002054#endif
drh626a8792005-01-17 22:08:19 +00002055
dan71c57db2016-07-09 20:23:55 +00002056static char *exprINAffinity(Parse *pParse, Expr *pExpr){
2057 Expr *pLeft = pExpr->pLeft;
2058 int nVal = sqlite3ExprVectorSize(pLeft);
2059 char *zRet;
2060
2061 zRet = sqlite3DbMallocZero(pParse->db, nVal+1);
2062 if( zRet ){
2063 int i;
2064 for(i=0; i<nVal; i++){
2065 Expr *pA;
2066 char a;
2067 if( nVal==1 ){
2068 pA = pLeft;
2069 }else{
2070 pA = exprVectorField(pLeft, i);
2071 }
2072 a = sqlite3ExprAffinity(pA);
2073 zRet[i] = sqlite3CompareAffinity(pExpr->x.pSelect->pEList->a[i].pExpr, a);
2074 }
2075 zRet[nVal] = '\0';
2076 }
2077 return zRet;
2078}
2079
drh626a8792005-01-17 22:08:19 +00002080/*
drhd4187c72010-08-30 22:15:45 +00002081** Generate code for scalar subqueries used as a subquery expression, EXISTS,
2082** or IN operators. Examples:
drh626a8792005-01-17 22:08:19 +00002083**
drh9cbe6352005-11-29 03:13:21 +00002084** (SELECT a FROM b) -- subquery
2085** EXISTS (SELECT a FROM b) -- EXISTS subquery
2086** x IN (4,5,11) -- IN operator with list on right-hand side
2087** x IN (SELECT a FROM b) -- IN operator with subquery on the right
drhfef52082000-06-06 01:50:43 +00002088**
drh9cbe6352005-11-29 03:13:21 +00002089** The pExpr parameter describes the expression that contains the IN
2090** operator or subquery.
danielk197741a05b72008-10-02 13:50:55 +00002091**
2092** If parameter isRowid is non-zero, then expression pExpr is guaranteed
2093** to be of the form "<rowid> IN (?, ?, ?)", where <rowid> is a reference
2094** to some integer key column of a table B-Tree. In this case, use an
2095** intkey B-Tree to store the set of IN(...) values instead of the usual
2096** (slower) variable length keys B-Tree.
drhfd773cf2009-05-29 14:39:07 +00002097**
2098** If rMayHaveNull is non-zero, that means that the operation is an IN
2099** (not a SELECT or EXISTS) and that the RHS might contains NULLs.
drh3a856252014-08-01 14:46:57 +00002100** All this routine does is initialize the register given by rMayHaveNull
2101** to NULL. Calling routines will take care of changing this register
2102** value to non-NULL if the RHS is NULL-free.
drh1450bc62009-10-30 13:25:56 +00002103**
2104** For a SELECT or EXISTS operator, return the register that holds the
2105** result. For IN operators or if an error occurs, the return value is 0.
drhcce7d172000-05-31 15:34:51 +00002106*/
drh51522cd2005-01-20 13:36:19 +00002107#ifndef SQLITE_OMIT_SUBQUERY
drh1450bc62009-10-30 13:25:56 +00002108int sqlite3CodeSubselect(
drhfd773cf2009-05-29 14:39:07 +00002109 Parse *pParse, /* Parsing context */
2110 Expr *pExpr, /* The IN, SELECT, or EXISTS operator */
drh6be515e2014-08-01 21:00:53 +00002111 int rHasNullFlag, /* Register that records whether NULLs exist in RHS */
drhfd773cf2009-05-29 14:39:07 +00002112 int isRowid /* If true, LHS of IN operator is a rowid */
danielk197741a05b72008-10-02 13:50:55 +00002113){
drh6be515e2014-08-01 21:00:53 +00002114 int jmpIfDynamic = -1; /* One-time test address */
drh1450bc62009-10-30 13:25:56 +00002115 int rReg = 0; /* Register storing resulting */
danielk1977b3bce662005-01-29 08:32:43 +00002116 Vdbe *v = sqlite3GetVdbe(pParse);
drh1450bc62009-10-30 13:25:56 +00002117 if( NEVER(v==0) ) return 0;
drhceea3322009-04-23 13:22:42 +00002118 sqlite3ExprCachePush(pParse);
danielk1977fc976062007-05-10 10:46:56 +00002119
drh57dbd7b2005-07-08 18:25:26 +00002120 /* This code must be run in its entirety every time it is encountered
2121 ** if any of the following is true:
2122 **
2123 ** * The right-hand side is a correlated subquery
2124 ** * The right-hand side is an expression list containing variables
2125 ** * We are inside a trigger
2126 **
2127 ** If all of the above are false, then we can run this code just once
2128 ** save the results, and reuse the same result on subsequent invocations.
danielk1977b3bce662005-01-29 08:32:43 +00002129 */
drhc5cd1242013-09-12 16:50:49 +00002130 if( !ExprHasProperty(pExpr, EP_VarSelect) ){
drh6be515e2014-08-01 21:00:53 +00002131 jmpIfDynamic = sqlite3CodeOnce(pParse); VdbeCoverage(v);
danielk1977b3bce662005-01-29 08:32:43 +00002132 }
2133
dan4a07e3d2010-11-09 14:48:59 +00002134#ifndef SQLITE_OMIT_EXPLAIN
2135 if( pParse->explain==2 ){
drh62aaa6c2015-11-21 17:27:42 +00002136 char *zMsg = sqlite3MPrintf(pParse->db, "EXECUTE %s%s SUBQUERY %d",
2137 jmpIfDynamic>=0?"":"CORRELATED ",
2138 pExpr->op==TK_IN?"LIST":"SCALAR",
2139 pParse->iNextSelectId
dan4a07e3d2010-11-09 14:48:59 +00002140 );
2141 sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC);
2142 }
2143#endif
2144
drhcce7d172000-05-31 15:34:51 +00002145 switch( pExpr->op ){
drhfef52082000-06-06 01:50:43 +00002146 case TK_IN: {
drhd4187c72010-08-30 22:15:45 +00002147 int addr; /* Address of OP_OpenEphemeral instruction */
2148 Expr *pLeft = pExpr->pLeft; /* the LHS of the IN operator */
drh323df792013-08-05 19:11:29 +00002149 KeyInfo *pKeyInfo = 0; /* Key information */
dan71c57db2016-07-09 20:23:55 +00002150 int nVal; /* Size of vector pLeft */
2151
2152 nVal = sqlite3ExprVectorSize(pLeft);
danielk1977e014a832004-05-17 10:48:57 +00002153
2154 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
drh8cff69d2009-11-12 19:59:44 +00002155 ** expression it is handled the same way. An ephemeral table is
danielk1977e014a832004-05-17 10:48:57 +00002156 ** filled with single-field index keys representing the results
2157 ** from the SELECT or the <exprlist>.
2158 **
2159 ** If the 'x' expression is a column value, or the SELECT...
2160 ** statement returns a column value, then the affinity of that
2161 ** column is used to build the index keys. If both 'x' and the
2162 ** SELECT... statement are columns, then numeric affinity is used
2163 ** if either column has NUMERIC or INTEGER affinity. If neither
2164 ** 'x' nor the SELECT... statement are columns, then numeric affinity
2165 ** is used.
2166 */
2167 pExpr->iTable = pParse->nTab++;
dan71c57db2016-07-09 20:23:55 +00002168 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral,
2169 pExpr->iTable, (isRowid?0:nVal));
2170 pKeyInfo = isRowid ? 0 : sqlite3KeyInfoAlloc(pParse->db, nVal, 1);
danielk1977e014a832004-05-17 10:48:57 +00002171
danielk19776ab3a2e2009-02-19 14:39:25 +00002172 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
drhfef52082000-06-06 01:50:43 +00002173 /* Case 1: expr IN (SELECT ...)
2174 **
danielk1977e014a832004-05-17 10:48:57 +00002175 ** Generate code to write the results of the select into the temporary
2176 ** table allocated and opened above.
drhfef52082000-06-06 01:50:43 +00002177 */
drh43870062014-07-31 15:44:44 +00002178 Select *pSelect = pExpr->x.pSelect;
dan71c57db2016-07-09 20:23:55 +00002179 ExprList *pEList = pSelect->pEList;
drh1013c932008-01-06 00:25:21 +00002180
danielk197741a05b72008-10-02 13:50:55 +00002181 assert( !isRowid );
dan71c57db2016-07-09 20:23:55 +00002182 if( pEList->nExpr!=nVal ){
2183 sqlite3ErrorMsg(pParse, "SELECT has %d columns - expected %d",
2184 pEList->nExpr, nVal);
2185 }else{
2186 SelectDest dest;
2187 int i;
2188 sqlite3SelectDestInit(&dest, SRT_Set, pExpr->iTable);
2189 dest.zAffSdst = exprINAffinity(pParse, pExpr);
2190 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
2191 pSelect->iLimit = 0;
2192 testcase( pSelect->selFlags & SF_Distinct );
2193 testcase( pKeyInfo==0 ); /* Caused by OOM in sqlite3KeyInfoAlloc() */
2194 if( sqlite3Select(pParse, pSelect, &dest) ){
2195 sqlite3DbFree(pParse->db, dest.zAffSdst);
2196 sqlite3KeyInfoUnref(pKeyInfo);
2197 return 0;
2198 }
2199 sqlite3DbFree(pParse->db, dest.zAffSdst);
2200 assert( pKeyInfo!=0 ); /* OOM will cause exit after sqlite3Select() */
2201 assert( pEList!=0 );
2202 assert( pEList->nExpr>0 );
2203 assert( sqlite3KeyInfoIsWriteable(pKeyInfo) );
2204 for(i=0; i<nVal; i++){
2205 Expr *p = (nVal>1) ? exprVectorField(pLeft, i) : pLeft;
2206 pKeyInfo->aColl[i] = sqlite3BinaryCompareCollSeq(
2207 pParse, p, pEList->a[i].pExpr
2208 );
2209 }
drh94ccde52007-04-13 16:06:32 +00002210 }
drha7d2db12010-07-14 20:23:52 +00002211 }else if( ALWAYS(pExpr->x.pList!=0) ){
drhfef52082000-06-06 01:50:43 +00002212 /* Case 2: expr IN (exprlist)
2213 **
drhfd131da2007-08-07 17:13:03 +00002214 ** For each expression, build an index key from the evaluation and
danielk1977e014a832004-05-17 10:48:57 +00002215 ** store it in the temporary table. If <expr> is a column, then use
2216 ** that columns affinity when building index keys. If <expr> is not
2217 ** a column, use numeric affinity.
drhfef52082000-06-06 01:50:43 +00002218 */
dan71c57db2016-07-09 20:23:55 +00002219 char affinity; /* Affinity of the LHS of the IN */
danielk1977e014a832004-05-17 10:48:57 +00002220 int i;
danielk19776ab3a2e2009-02-19 14:39:25 +00002221 ExprList *pList = pExpr->x.pList;
drh57dbd7b2005-07-08 18:25:26 +00002222 struct ExprList_item *pItem;
drhecc31802008-06-26 20:06:06 +00002223 int r1, r2, r3;
drh57dbd7b2005-07-08 18:25:26 +00002224
dan71c57db2016-07-09 20:23:55 +00002225 affinity = sqlite3ExprAffinity(pLeft);
danielk1977e014a832004-05-17 10:48:57 +00002226 if( !affinity ){
drh05883a32015-06-02 15:32:08 +00002227 affinity = SQLITE_AFF_BLOB;
danielk1977e014a832004-05-17 10:48:57 +00002228 }
drh323df792013-08-05 19:11:29 +00002229 if( pKeyInfo ){
drh2ec2fb22013-11-06 19:59:23 +00002230 assert( sqlite3KeyInfoIsWriteable(pKeyInfo) );
drh323df792013-08-05 19:11:29 +00002231 pKeyInfo->aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
2232 }
danielk1977e014a832004-05-17 10:48:57 +00002233
2234 /* Loop through each expression in <exprlist>. */
drh2d401ab2008-01-10 23:50:11 +00002235 r1 = sqlite3GetTempReg(pParse);
2236 r2 = sqlite3GetTempReg(pParse);
drh37e08082014-07-31 20:16:08 +00002237 if( isRowid ) sqlite3VdbeAddOp2(v, OP_Null, 0, r2);
drh57dbd7b2005-07-08 18:25:26 +00002238 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
2239 Expr *pE2 = pItem->pExpr;
drhe05c9292009-10-29 13:48:10 +00002240 int iValToIns;
danielk1977e014a832004-05-17 10:48:57 +00002241
drh57dbd7b2005-07-08 18:25:26 +00002242 /* If the expression is not constant then we will need to
2243 ** disable the test that was generated above that makes sure
2244 ** this code only executes once. Because for a non-constant
2245 ** expression we need to rerun this code each time.
2246 */
drh6be515e2014-08-01 21:00:53 +00002247 if( jmpIfDynamic>=0 && !sqlite3ExprIsConstant(pE2) ){
2248 sqlite3VdbeChangeToNoop(v, jmpIfDynamic);
2249 jmpIfDynamic = -1;
drh4794b982000-06-06 13:54:14 +00002250 }
danielk1977e014a832004-05-17 10:48:57 +00002251
2252 /* Evaluate the expression and insert it into the temp table */
drhe05c9292009-10-29 13:48:10 +00002253 if( isRowid && sqlite3ExprIsInteger(pE2, &iValToIns) ){
2254 sqlite3VdbeAddOp3(v, OP_InsertInt, pExpr->iTable, r2, iValToIns);
danielk197741a05b72008-10-02 13:50:55 +00002255 }else{
drhe05c9292009-10-29 13:48:10 +00002256 r3 = sqlite3ExprCodeTarget(pParse, pE2, r1);
2257 if( isRowid ){
2258 sqlite3VdbeAddOp2(v, OP_MustBeInt, r3,
2259 sqlite3VdbeCurrentAddr(v)+2);
drh688852a2014-02-17 22:40:43 +00002260 VdbeCoverage(v);
drhe05c9292009-10-29 13:48:10 +00002261 sqlite3VdbeAddOp3(v, OP_Insert, pExpr->iTable, r2, r3);
2262 }else{
2263 sqlite3VdbeAddOp4(v, OP_MakeRecord, r3, 1, r2, &affinity, 1);
2264 sqlite3ExprCacheAffinityChange(pParse, r3, 1);
2265 sqlite3VdbeAddOp2(v, OP_IdxInsert, pExpr->iTable, r2);
2266 }
danielk197741a05b72008-10-02 13:50:55 +00002267 }
drhfef52082000-06-06 01:50:43 +00002268 }
drh2d401ab2008-01-10 23:50:11 +00002269 sqlite3ReleaseTempReg(pParse, r1);
2270 sqlite3ReleaseTempReg(pParse, r2);
drhfef52082000-06-06 01:50:43 +00002271 }
drh323df792013-08-05 19:11:29 +00002272 if( pKeyInfo ){
drh2ec2fb22013-11-06 19:59:23 +00002273 sqlite3VdbeChangeP4(v, addr, (void *)pKeyInfo, P4_KEYINFO);
danielk197741a05b72008-10-02 13:50:55 +00002274 }
danielk1977b3bce662005-01-29 08:32:43 +00002275 break;
drhfef52082000-06-06 01:50:43 +00002276 }
2277
drh51522cd2005-01-20 13:36:19 +00002278 case TK_EXISTS:
drhfd773cf2009-05-29 14:39:07 +00002279 case TK_SELECT:
2280 default: {
drhfd773cf2009-05-29 14:39:07 +00002281 /* If this has to be a scalar SELECT. Generate code to put the
drhfef52082000-06-06 01:50:43 +00002282 ** value of this select in a memory cell and record the number
drhfd773cf2009-05-29 14:39:07 +00002283 ** of the memory cell in iColumn. If this is an EXISTS, write
2284 ** an integer 0 (not exists) or 1 (exists) into a memory cell
2285 ** and record that memory cell in iColumn.
drhfef52082000-06-06 01:50:43 +00002286 */
drhfd773cf2009-05-29 14:39:07 +00002287 Select *pSel; /* SELECT statement to encode */
2288 SelectDest dest; /* How to deal with SELECt result */
dan71c57db2016-07-09 20:23:55 +00002289 int nReg; /* Registers to allocate */
drh1398ad32005-01-19 23:24:50 +00002290
shanecf697392009-06-01 16:53:09 +00002291 testcase( pExpr->op==TK_EXISTS );
2292 testcase( pExpr->op==TK_SELECT );
2293 assert( pExpr->op==TK_EXISTS || pExpr->op==TK_SELECT );
danielk19776ab3a2e2009-02-19 14:39:25 +00002294 assert( ExprHasProperty(pExpr, EP_xIsSelect) );
dan71c57db2016-07-09 20:23:55 +00002295
danielk19776ab3a2e2009-02-19 14:39:25 +00002296 pSel = pExpr->x.pSelect;
dan71c57db2016-07-09 20:23:55 +00002297 nReg = pExpr->op==TK_SELECT ? pSel->pEList->nExpr : 1;
2298 sqlite3SelectDestInit(&dest, 0, pParse->nMem+1);
2299 pParse->nMem += nReg;
drh51522cd2005-01-20 13:36:19 +00002300 if( pExpr->op==TK_SELECT ){
danielk19776c8c8ce2008-01-02 16:27:09 +00002301 dest.eDest = SRT_Mem;
drh53932ce2014-08-29 12:29:39 +00002302 dest.iSdst = dest.iSDParm;
dan71c57db2016-07-09 20:23:55 +00002303 dest.nSdst = nReg;
2304 sqlite3VdbeAddOp3(v, OP_Null, 0, dest.iSDParm, dest.iSDParm+nReg-1);
drhd4e70eb2008-01-02 00:34:36 +00002305 VdbeComment((v, "Init subquery result"));
drh51522cd2005-01-20 13:36:19 +00002306 }else{
danielk19776c8c8ce2008-01-02 16:27:09 +00002307 dest.eDest = SRT_Exists;
drh2b596da2012-07-23 21:43:19 +00002308 sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iSDParm);
drhd4e70eb2008-01-02 00:34:36 +00002309 VdbeComment((v, "Init EXISTS result"));
drh51522cd2005-01-20 13:36:19 +00002310 }
drh633e6d52008-07-28 19:34:53 +00002311 sqlite3ExprDelete(pParse->db, pSel->pLimit);
drh094430e2010-07-14 18:24:06 +00002312 pSel->pLimit = sqlite3PExpr(pParse, TK_INTEGER, 0, 0,
2313 &sqlite3IntTokens[1]);
drh48b5b042010-12-06 18:50:32 +00002314 pSel->iLimit = 0;
drh772460f2015-04-16 14:13:12 +00002315 pSel->selFlags &= ~SF_MultiValue;
drh7d10d5a2008-08-20 16:35:10 +00002316 if( sqlite3Select(pParse, pSel, &dest) ){
drh1450bc62009-10-30 13:25:56 +00002317 return 0;
drh94ccde52007-04-13 16:06:32 +00002318 }
drh2b596da2012-07-23 21:43:19 +00002319 rReg = dest.iSDParm;
drhebb6a652013-09-12 23:42:22 +00002320 ExprSetVVAProperty(pExpr, EP_NoReduce);
danielk1977b3bce662005-01-29 08:32:43 +00002321 break;
drhcce7d172000-05-31 15:34:51 +00002322 }
2323 }
danielk1977b3bce662005-01-29 08:32:43 +00002324
drh6be515e2014-08-01 21:00:53 +00002325 if( rHasNullFlag ){
2326 sqlite3SetHasNullFlag(v, pExpr->iTable, rHasNullFlag);
danielk1977b3bce662005-01-29 08:32:43 +00002327 }
drh6be515e2014-08-01 21:00:53 +00002328
2329 if( jmpIfDynamic>=0 ){
2330 sqlite3VdbeJumpHere(v, jmpIfDynamic);
danielk1977b3bce662005-01-29 08:32:43 +00002331 }
drhd2490902014-04-13 19:28:15 +00002332 sqlite3ExprCachePop(pParse);
danielk1977fc976062007-05-10 10:46:56 +00002333
drh1450bc62009-10-30 13:25:56 +00002334 return rReg;
drhcce7d172000-05-31 15:34:51 +00002335}
drh51522cd2005-01-20 13:36:19 +00002336#endif /* SQLITE_OMIT_SUBQUERY */
drhcce7d172000-05-31 15:34:51 +00002337
drhe3365e62009-11-12 17:52:24 +00002338#ifndef SQLITE_OMIT_SUBQUERY
2339/*
2340** Generate code for an IN expression.
2341**
2342** x IN (SELECT ...)
2343** x IN (value, value, ...)
2344**
2345** The left-hand side (LHS) is a scalar expression. The right-hand side (RHS)
2346** is an array of zero or more values. The expression is true if the LHS is
2347** contained within the RHS. The value of the expression is unknown (NULL)
2348** if the LHS is NULL or if the LHS is not contained within the RHS and the
2349** RHS contains one or more NULL values.
2350**
drh6be515e2014-08-01 21:00:53 +00002351** This routine generates code that jumps to destIfFalse if the LHS is not
drhe3365e62009-11-12 17:52:24 +00002352** contained within the RHS. If due to NULLs we cannot determine if the LHS
2353** is contained in the RHS then jump to destIfNull. If the LHS is contained
2354** within the RHS then fall through.
2355*/
2356static void sqlite3ExprCodeIN(
2357 Parse *pParse, /* Parsing and code generating context */
2358 Expr *pExpr, /* The IN expression */
2359 int destIfFalse, /* Jump here if LHS is not contained in the RHS */
2360 int destIfNull /* Jump here if the results are unknown due to NULLs */
2361){
2362 int rRhsHasNull = 0; /* Register that is true if RHS contains NULL values */
drhe3365e62009-11-12 17:52:24 +00002363 int eType; /* Type of the RHS */
2364 int r1; /* Temporary use register */
2365 Vdbe *v; /* Statement under construction */
danba00e302016-07-23 20:24:06 +00002366 int *aiMap = 0; /* Map from vector field to index column */
2367 char *zAff = 0; /* Affinity string for comparisons */
2368 int nVector; /* Size of vectors for this IN(...) op */
2369 int regSelect = 0;
2370 Expr *pLeft = pExpr->pLeft;
2371 int i;
drhe3365e62009-11-12 17:52:24 +00002372
danba00e302016-07-23 20:24:06 +00002373 nVector = sqlite3ExprVectorSize(pExpr->pLeft);
2374 aiMap = (int*)sqlite3DbMallocZero(
2375 pParse->db, nVector*(sizeof(int) + sizeof(char)) + 1
2376 );
2377 if( !aiMap ) return;
2378 zAff = (char*)&aiMap[nVector];
dan71c57db2016-07-09 20:23:55 +00002379
danba00e302016-07-23 20:24:06 +00002380 /* Attempt to compute the RHS. After this step, if anything other than
2381 ** IN_INDEX_NOOP is returned, the table opened ith cursor pExpr->iTable
2382 ** contains the values that make up the RHS. If IN_INDEX_NOOP is returned,
2383 ** the RHS has not yet been coded. */
drhe3365e62009-11-12 17:52:24 +00002384 v = pParse->pVdbe;
2385 assert( v!=0 ); /* OOM detected prior to this routine */
2386 VdbeNoopComment((v, "begin IN expr"));
drhbb53ecb2014-08-02 21:03:33 +00002387 eType = sqlite3FindInIndex(pParse, pExpr,
2388 IN_INDEX_MEMBERSHIP | IN_INDEX_NOOP_OK,
danba00e302016-07-23 20:24:06 +00002389 destIfFalse==destIfNull ? 0 : &rRhsHasNull, aiMap);
drhe3365e62009-11-12 17:52:24 +00002390
danba00e302016-07-23 20:24:06 +00002391 assert( pParse->nErr || nVector==1 || eType==IN_INDEX_EPH
2392 || eType==IN_INDEX_INDEX_ASC || eType==IN_INDEX_INDEX_DESC
2393 );
drhe3365e62009-11-12 17:52:24 +00002394
danba00e302016-07-23 20:24:06 +00002395 /* Code the LHS, the <expr> from "<expr> IN (...)". If the LHS is a
2396 ** vector, then it is stored in an array of nVector registers starting
2397 ** at r1.
drhe3365e62009-11-12 17:52:24 +00002398 */
danba00e302016-07-23 20:24:06 +00002399 r1 = sqlite3GetTempRange(pParse, nVector);
drhe3365e62009-11-12 17:52:24 +00002400 sqlite3ExprCachePush(pParse);
danba00e302016-07-23 20:24:06 +00002401 if( nVector>1 && (pLeft->flags & EP_xIsSelect) ){
2402 regSelect = sqlite3CodeSubselect(pParse, pLeft, 0, 0);
2403 }
2404 for(i=0; i<nVector; i++){
2405 int iCol = aiMap[i];
2406 Expr *pLhs = exprVectorField(pLeft, i);
2407
2408 if( regSelect ){
2409 sqlite3VdbeAddOp3(v, OP_Copy, regSelect+i, r1+iCol, 0);
2410 }else{
2411 sqlite3ExprCode(pParse, pLhs, r1+iCol);
2412 }
2413
2414 zAff[iCol] = sqlite3ExprAffinity(pLhs);
2415 if( pExpr->flags & EP_xIsSelect ){
2416 zAff[iCol] = sqlite3CompareAffinity(
2417 pExpr->x.pSelect->pEList->a[iCol].pExpr, zAff[iCol]
2418 );
2419 }
2420 }
drhe3365e62009-11-12 17:52:24 +00002421
drhbb53ecb2014-08-02 21:03:33 +00002422 /* If sqlite3FindInIndex() did not find or create an index that is
2423 ** suitable for evaluating the IN operator, then evaluate using a
2424 ** sequence of comparisons.
drh094430e2010-07-14 18:24:06 +00002425 */
drhbb53ecb2014-08-02 21:03:33 +00002426 if( eType==IN_INDEX_NOOP ){
2427 ExprList *pList = pExpr->x.pList;
2428 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
2429 int labelOk = sqlite3VdbeMakeLabel(v);
2430 int r2, regToFree;
2431 int regCkNull = 0;
2432 int ii;
2433 assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
drhbb53ecb2014-08-02 21:03:33 +00002434 if( destIfNull!=destIfFalse ){
2435 regCkNull = sqlite3GetTempReg(pParse);
drha9769792014-08-04 16:39:39 +00002436 sqlite3VdbeAddOp3(v, OP_BitAnd, r1, r1, regCkNull);
drhbb53ecb2014-08-02 21:03:33 +00002437 }
2438 for(ii=0; ii<pList->nExpr; ii++){
2439 r2 = sqlite3ExprCodeTemp(pParse, pList->a[ii].pExpr, &regToFree);
drha9769792014-08-04 16:39:39 +00002440 if( regCkNull && sqlite3ExprCanBeNull(pList->a[ii].pExpr) ){
drhbb53ecb2014-08-02 21:03:33 +00002441 sqlite3VdbeAddOp3(v, OP_BitAnd, regCkNull, r2, regCkNull);
2442 }
2443 if( ii<pList->nExpr-1 || destIfNull!=destIfFalse ){
2444 sqlite3VdbeAddOp4(v, OP_Eq, r1, labelOk, r2,
drh4336b0e2014-08-05 00:53:51 +00002445 (void*)pColl, P4_COLLSEQ);
2446 VdbeCoverageIf(v, ii<pList->nExpr-1);
2447 VdbeCoverageIf(v, ii==pList->nExpr-1);
danba00e302016-07-23 20:24:06 +00002448 sqlite3VdbeChangeP5(v, zAff[0]);
drhbb53ecb2014-08-02 21:03:33 +00002449 }else{
2450 assert( destIfNull==destIfFalse );
2451 sqlite3VdbeAddOp4(v, OP_Ne, r1, destIfFalse, r2,
2452 (void*)pColl, P4_COLLSEQ); VdbeCoverage(v);
danba00e302016-07-23 20:24:06 +00002453 sqlite3VdbeChangeP5(v, zAff[0] | SQLITE_JUMPIFNULL);
drhbb53ecb2014-08-02 21:03:33 +00002454 }
2455 sqlite3ReleaseTempReg(pParse, regToFree);
2456 }
2457 if( regCkNull ){
2458 sqlite3VdbeAddOp2(v, OP_IsNull, regCkNull, destIfNull); VdbeCoverage(v);
drh076e85f2015-09-03 13:46:12 +00002459 sqlite3VdbeGoto(v, destIfFalse);
drhbb53ecb2014-08-02 21:03:33 +00002460 }
2461 sqlite3VdbeResolveLabel(v, labelOk);
2462 sqlite3ReleaseTempReg(pParse, regCkNull);
drh094430e2010-07-14 18:24:06 +00002463 }else{
drhbb53ecb2014-08-02 21:03:33 +00002464
2465 /* If the LHS is NULL, then the result is either false or NULL depending
2466 ** on whether the RHS is empty or not, respectively.
drhe3365e62009-11-12 17:52:24 +00002467 */
danba00e302016-07-23 20:24:06 +00002468 if( nVector==1 && sqlite3ExprCanBeNull(pExpr->pLeft) ){
drh7248a8b2014-08-04 18:50:54 +00002469 if( destIfNull==destIfFalse ){
2470 /* Shortcut for the common case where the false and NULL outcomes are
2471 ** the same. */
2472 sqlite3VdbeAddOp2(v, OP_IsNull, r1, destIfNull); VdbeCoverage(v);
2473 }else{
2474 int addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, r1); VdbeCoverage(v);
2475 sqlite3VdbeAddOp2(v, OP_Rewind, pExpr->iTable, destIfFalse);
2476 VdbeCoverage(v);
drh076e85f2015-09-03 13:46:12 +00002477 sqlite3VdbeGoto(v, destIfNull);
drh7248a8b2014-08-04 18:50:54 +00002478 sqlite3VdbeJumpHere(v, addr1);
2479 }
drhbb53ecb2014-08-02 21:03:33 +00002480 }
2481
2482 if( eType==IN_INDEX_ROWID ){
2483 /* In this case, the RHS is the ROWID of table b-tree
drhe3365e62009-11-12 17:52:24 +00002484 */
drheeb95652016-05-26 20:56:38 +00002485 sqlite3VdbeAddOp3(v, OP_SeekRowid, pExpr->iTable, destIfFalse, r1);
drh688852a2014-02-17 22:40:43 +00002486 VdbeCoverage(v);
danba00e302016-07-23 20:24:06 +00002487 }else if( nVector>1 && eType==IN_INDEX_EPH ){
2488 int regNull = sqlite3GetTempReg(pParse);
2489 int r2 = sqlite3GetTempReg(pParse);
2490 int r3 = sqlite3GetTempReg(pParse);
2491 int r4 = sqlite3GetTempReg(pParse);
2492 int addrNext;
2493 int addrIf;
2494
2495 if( destIfFalse!=destIfNull ){
2496 sqlite3VdbeAddOp2(v, OP_Integer, 0, regNull);
2497 }
2498 addrNext = sqlite3VdbeAddOp2(v, OP_Rewind, pExpr->iTable, destIfFalse);
2499 for(i=0; i<nVector; i++){
2500 Expr *p;
2501 CollSeq *pColl;
2502 p = exprVectorField(pLeft, i);
2503 pColl = sqlite3ExprCollSeq(pParse, p);
2504
2505 sqlite3VdbeAddOp3(v, OP_Column, pExpr->iTable, i, r2);
2506 sqlite3VdbeAddOp4(v, OP_Eq, r1+i, i?r3:r4, r2, (void*)pColl,P4_COLLSEQ);
2507 sqlite3VdbeChangeP5(v, SQLITE_STOREP2);
2508 if( i!=0 ){
2509 sqlite3VdbeAddOp3(v, OP_And, r3, r4, r4);
2510 }
2511 }
2512 addrIf = sqlite3VdbeAddOp1(v, OP_If, r4);
2513 if( destIfNull!=destIfFalse ){
2514 sqlite3VdbeAddOp2(v, OP_IfNot, r4, sqlite3VdbeCurrentAddr(v)+2);
2515 sqlite3VdbeAddOp2(v, OP_Integer, 1, regNull);
2516 }
2517 sqlite3VdbeAddOp2(v, OP_Next, pExpr->iTable, addrNext+1);
2518 if( destIfNull!=destIfFalse ){
2519 sqlite3VdbeAddOp2(v, OP_If, regNull, destIfNull);
2520 }
2521 sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfFalse);
2522 sqlite3VdbeChangeP2(v, addrIf, sqlite3VdbeCurrentAddr(v));
2523 sqlite3ReleaseTempReg(pParse, regNull);
2524 sqlite3ReleaseTempReg(pParse, r2);
2525 sqlite3ReleaseTempReg(pParse, r3);
2526 sqlite3ReleaseTempReg(pParse, r4);
drhe3365e62009-11-12 17:52:24 +00002527 }else{
drhbb53ecb2014-08-02 21:03:33 +00002528 /* In this case, the RHS is an index b-tree.
drhe3365e62009-11-12 17:52:24 +00002529 */
danba00e302016-07-23 20:24:06 +00002530 sqlite3VdbeAddOp4(v, OP_Affinity, r1, nVector, 0, zAff, nVector);
drhbb53ecb2014-08-02 21:03:33 +00002531
2532 /* If the set membership test fails, then the result of the
2533 ** "x IN (...)" expression must be either 0 or NULL. If the set
2534 ** contains no NULL values, then the result is 0. If the set
2535 ** contains one or more NULL values, then the result of the
2536 ** expression is also NULL.
drhe3365e62009-11-12 17:52:24 +00002537 */
drhbb53ecb2014-08-02 21:03:33 +00002538 assert( destIfFalse!=destIfNull || rRhsHasNull==0 );
2539 if( rRhsHasNull==0 ){
2540 /* This branch runs if it is known at compile time that the RHS
2541 ** cannot contain NULL values. This happens as the result
2542 ** of a "NOT NULL" constraint in the database schema.
2543 **
2544 ** Also run this branch if NULL is equivalent to FALSE
2545 ** for this particular IN operator.
2546 */
danba00e302016-07-23 20:24:06 +00002547 sqlite3VdbeAddOp4Int(
2548 v, OP_NotFound, pExpr->iTable, destIfFalse, r1, nVector
2549 );
drhbb53ecb2014-08-02 21:03:33 +00002550 VdbeCoverage(v);
2551 }else{
2552 /* In this branch, the RHS of the IN might contain a NULL and
2553 ** the presence of a NULL on the RHS makes a difference in the
2554 ** outcome.
2555 */
drh728e0f92015-10-10 14:41:28 +00002556 int addr1;
drhbb53ecb2014-08-02 21:03:33 +00002557
2558 /* First check to see if the LHS is contained in the RHS. If so,
2559 ** then the answer is TRUE the presence of NULLs in the RHS does
2560 ** not matter. If the LHS is not contained in the RHS, then the
2561 ** answer is NULL if the RHS contains NULLs and the answer is
2562 ** FALSE if the RHS is NULL-free.
2563 */
drh728e0f92015-10-10 14:41:28 +00002564 addr1 = sqlite3VdbeAddOp4Int(v, OP_Found, pExpr->iTable, 0, r1, 1);
drhbb53ecb2014-08-02 21:03:33 +00002565 VdbeCoverage(v);
2566 sqlite3VdbeAddOp2(v, OP_IsNull, rRhsHasNull, destIfNull);
2567 VdbeCoverage(v);
drh076e85f2015-09-03 13:46:12 +00002568 sqlite3VdbeGoto(v, destIfFalse);
drh728e0f92015-10-10 14:41:28 +00002569 sqlite3VdbeJumpHere(v, addr1);
drhbb53ecb2014-08-02 21:03:33 +00002570 }
drhe3365e62009-11-12 17:52:24 +00002571 }
drhe3365e62009-11-12 17:52:24 +00002572 }
2573 sqlite3ReleaseTempReg(pParse, r1);
drhd2490902014-04-13 19:28:15 +00002574 sqlite3ExprCachePop(pParse);
danba00e302016-07-23 20:24:06 +00002575 sqlite3DbFree(pParse->db, aiMap);
drhe3365e62009-11-12 17:52:24 +00002576 VdbeComment((v, "end IN expr"));
2577}
2578#endif /* SQLITE_OMIT_SUBQUERY */
2579
drh13573c72010-01-12 17:04:07 +00002580#ifndef SQLITE_OMIT_FLOATING_POINT
drh598f1342007-10-23 15:39:45 +00002581/*
2582** Generate an instruction that will put the floating point
drh9cbf3422008-01-17 16:22:13 +00002583** value described by z[0..n-1] into register iMem.
drh0cf19ed2007-10-23 18:55:48 +00002584**
2585** The z[] string will probably not be zero-terminated. But the
2586** z[n] character is guaranteed to be something that does not look
2587** like the continuation of the number.
drh598f1342007-10-23 15:39:45 +00002588*/
drhb7916a72009-05-27 10:31:29 +00002589static void codeReal(Vdbe *v, const char *z, int negateFlag, int iMem){
drhfd773cf2009-05-29 14:39:07 +00002590 if( ALWAYS(z!=0) ){
drh598f1342007-10-23 15:39:45 +00002591 double value;
drh9339da12010-09-30 00:50:49 +00002592 sqlite3AtoF(z, &value, sqlite3Strlen30(z), SQLITE_UTF8);
drhd0015162009-08-21 13:22:25 +00002593 assert( !sqlite3IsNaN(value) ); /* The new AtoF never returns NaN */
2594 if( negateFlag ) value = -value;
drh97bae792015-06-05 15:59:57 +00002595 sqlite3VdbeAddOp4Dup8(v, OP_Real, 0, iMem, 0, (u8*)&value, P4_REAL);
drh598f1342007-10-23 15:39:45 +00002596 }
2597}
drh13573c72010-01-12 17:04:07 +00002598#endif
drh598f1342007-10-23 15:39:45 +00002599
2600
2601/*
drhfec19aa2004-05-19 20:41:03 +00002602** Generate an instruction that will put the integer describe by
drh9cbf3422008-01-17 16:22:13 +00002603** text z[0..n-1] into register iMem.
drh0cf19ed2007-10-23 18:55:48 +00002604**
shaneh5f1d6b62010-09-30 16:51:25 +00002605** Expr.u.zToken is always UTF8 and zero-terminated.
drhfec19aa2004-05-19 20:41:03 +00002606*/
drh13573c72010-01-12 17:04:07 +00002607static void codeInteger(Parse *pParse, Expr *pExpr, int negFlag, int iMem){
2608 Vdbe *v = pParse->pVdbe;
drh92b01d52008-06-24 00:32:35 +00002609 if( pExpr->flags & EP_IntValue ){
drh33e619f2009-05-28 01:00:55 +00002610 int i = pExpr->u.iValue;
drhd50ffc42011-03-08 02:38:28 +00002611 assert( i>=0 );
drh92b01d52008-06-24 00:32:35 +00002612 if( negFlag ) i = -i;
2613 sqlite3VdbeAddOp2(v, OP_Integer, i, iMem);
drhfd773cf2009-05-29 14:39:07 +00002614 }else{
shaneh5f1d6b62010-09-30 16:51:25 +00002615 int c;
2616 i64 value;
drhfd773cf2009-05-29 14:39:07 +00002617 const char *z = pExpr->u.zToken;
2618 assert( z!=0 );
drh9296c182014-07-23 13:40:49 +00002619 c = sqlite3DecOrHexToI64(z, &value);
shaneh5f1d6b62010-09-30 16:51:25 +00002620 if( c==0 || (c==2 && negFlag) ){
drh158b9cb2011-03-05 20:59:46 +00002621 if( negFlag ){ value = c==2 ? SMALLEST_INT64 : -value; }
drh97bae792015-06-05 15:59:57 +00002622 sqlite3VdbeAddOp4Dup8(v, OP_Int64, 0, iMem, 0, (u8*)&value, P4_INT64);
danielk1977c9cf9012007-05-30 10:36:47 +00002623 }else{
drh13573c72010-01-12 17:04:07 +00002624#ifdef SQLITE_OMIT_FLOATING_POINT
2625 sqlite3ErrorMsg(pParse, "oversized integer: %s%s", negFlag ? "-" : "", z);
2626#else
drh1b7ddc52014-07-23 14:52:05 +00002627#ifndef SQLITE_OMIT_HEX_INTEGER
drh9296c182014-07-23 13:40:49 +00002628 if( sqlite3_strnicmp(z,"0x",2)==0 ){
2629 sqlite3ErrorMsg(pParse, "hex literal too big: %s", z);
drh1b7ddc52014-07-23 14:52:05 +00002630 }else
2631#endif
2632 {
drh9296c182014-07-23 13:40:49 +00002633 codeReal(v, z, negFlag, iMem);
2634 }
drh13573c72010-01-12 17:04:07 +00002635#endif
danielk1977c9cf9012007-05-30 10:36:47 +00002636 }
drhfec19aa2004-05-19 20:41:03 +00002637 }
2638}
2639
drhbea119c2016-04-11 18:15:37 +00002640#if defined(SQLITE_DEBUG)
2641/*
2642** Verify the consistency of the column cache
2643*/
2644static int cacheIsValid(Parse *pParse){
2645 int i, n;
2646 for(i=n=0; i<SQLITE_N_COLCACHE; i++){
2647 if( pParse->aColCache[i].iReg>0 ) n++;
2648 }
2649 return n==pParse->nColCache;
2650}
2651#endif
2652
drhceea3322009-04-23 13:22:42 +00002653/*
2654** Clear a cache entry.
2655*/
2656static void cacheEntryClear(Parse *pParse, struct yColCache *p){
2657 if( p->tempReg ){
2658 if( pParse->nTempReg<ArraySize(pParse->aTempReg) ){
2659 pParse->aTempReg[pParse->nTempReg++] = p->iReg;
2660 }
2661 p->tempReg = 0;
2662 }
drhbea119c2016-04-11 18:15:37 +00002663 p->iReg = 0;
2664 pParse->nColCache--;
danee65eea2016-04-16 15:03:20 +00002665 assert( pParse->db->mallocFailed || cacheIsValid(pParse) );
drhceea3322009-04-23 13:22:42 +00002666}
2667
2668
2669/*
2670** Record in the column cache that a particular column from a
2671** particular table is stored in a particular register.
2672*/
2673void sqlite3ExprCacheStore(Parse *pParse, int iTab, int iCol, int iReg){
2674 int i;
2675 int minLru;
2676 int idxLru;
2677 struct yColCache *p;
2678
dance8f53d2015-01-21 17:00:57 +00002679 /* Unless an error has occurred, register numbers are always positive. */
2680 assert( iReg>0 || pParse->nErr || pParse->db->mallocFailed );
drh20411ea2009-05-29 19:00:12 +00002681 assert( iCol>=-1 && iCol<32768 ); /* Finite column numbers */
2682
drhb6da74e2009-12-24 16:00:28 +00002683 /* The SQLITE_ColumnCache flag disables the column cache. This is used
2684 ** for testing only - to verify that SQLite always gets the same answer
2685 ** with and without the column cache.
2686 */
drh7e5418e2012-09-27 15:05:54 +00002687 if( OptimizationDisabled(pParse->db, SQLITE_ColumnCache) ) return;
drhb6da74e2009-12-24 16:00:28 +00002688
drh27ee4062009-12-30 01:13:11 +00002689 /* First replace any existing entry.
2690 **
2691 ** Actually, the way the column cache is currently used, we are guaranteed
2692 ** that the object will never already be in cache. Verify this guarantee.
2693 */
2694#ifndef NDEBUG
drhceea3322009-04-23 13:22:42 +00002695 for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
drh27ee4062009-12-30 01:13:11 +00002696 assert( p->iReg==0 || p->iTable!=iTab || p->iColumn!=iCol );
drhceea3322009-04-23 13:22:42 +00002697 }
drh27ee4062009-12-30 01:13:11 +00002698#endif
drhceea3322009-04-23 13:22:42 +00002699
2700 /* Find an empty slot and replace it */
2701 for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
2702 if( p->iReg==0 ){
2703 p->iLevel = pParse->iCacheLevel;
2704 p->iTable = iTab;
2705 p->iColumn = iCol;
2706 p->iReg = iReg;
drhceea3322009-04-23 13:22:42 +00002707 p->tempReg = 0;
2708 p->lru = pParse->iCacheCnt++;
drhbea119c2016-04-11 18:15:37 +00002709 pParse->nColCache++;
danee65eea2016-04-16 15:03:20 +00002710 assert( pParse->db->mallocFailed || cacheIsValid(pParse) );
drhceea3322009-04-23 13:22:42 +00002711 return;
2712 }
2713 }
2714
2715 /* Replace the last recently used */
2716 minLru = 0x7fffffff;
2717 idxLru = -1;
2718 for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
2719 if( p->lru<minLru ){
2720 idxLru = i;
2721 minLru = p->lru;
2722 }
2723 }
drh20411ea2009-05-29 19:00:12 +00002724 if( ALWAYS(idxLru>=0) ){
drhceea3322009-04-23 13:22:42 +00002725 p = &pParse->aColCache[idxLru];
2726 p->iLevel = pParse->iCacheLevel;
2727 p->iTable = iTab;
2728 p->iColumn = iCol;
2729 p->iReg = iReg;
drhceea3322009-04-23 13:22:42 +00002730 p->tempReg = 0;
2731 p->lru = pParse->iCacheCnt++;
drhbea119c2016-04-11 18:15:37 +00002732 assert( cacheIsValid(pParse) );
drhceea3322009-04-23 13:22:42 +00002733 return;
2734 }
2735}
2736
2737/*
drhf49f3522009-12-30 14:12:38 +00002738** Indicate that registers between iReg..iReg+nReg-1 are being overwritten.
2739** Purge the range of registers from the column cache.
drhceea3322009-04-23 13:22:42 +00002740*/
drhf49f3522009-12-30 14:12:38 +00002741void sqlite3ExprCacheRemove(Parse *pParse, int iReg, int nReg){
drhceea3322009-04-23 13:22:42 +00002742 struct yColCache *p;
drhbea119c2016-04-11 18:15:37 +00002743 if( iReg<=0 || pParse->nColCache==0 ) return;
2744 p = &pParse->aColCache[SQLITE_N_COLCACHE-1];
2745 while(1){
2746 if( p->iReg >= iReg && p->iReg < iReg+nReg ) cacheEntryClear(pParse, p);
2747 if( p==pParse->aColCache ) break;
2748 p--;
drhceea3322009-04-23 13:22:42 +00002749 }
2750}
2751
2752/*
2753** Remember the current column cache context. Any new entries added
2754** added to the column cache after this call are removed when the
2755** corresponding pop occurs.
2756*/
2757void sqlite3ExprCachePush(Parse *pParse){
2758 pParse->iCacheLevel++;
drh9ac79622013-12-18 15:11:47 +00002759#ifdef SQLITE_DEBUG
2760 if( pParse->db->flags & SQLITE_VdbeAddopTrace ){
2761 printf("PUSH to %d\n", pParse->iCacheLevel);
2762 }
2763#endif
drhceea3322009-04-23 13:22:42 +00002764}
2765
2766/*
2767** Remove from the column cache any entries that were added since the
drhd2490902014-04-13 19:28:15 +00002768** the previous sqlite3ExprCachePush operation. In other words, restore
2769** the cache to the state it was in prior the most recent Push.
drhceea3322009-04-23 13:22:42 +00002770*/
drhd2490902014-04-13 19:28:15 +00002771void sqlite3ExprCachePop(Parse *pParse){
drhceea3322009-04-23 13:22:42 +00002772 int i;
2773 struct yColCache *p;
drhd2490902014-04-13 19:28:15 +00002774 assert( pParse->iCacheLevel>=1 );
2775 pParse->iCacheLevel--;
drh9ac79622013-12-18 15:11:47 +00002776#ifdef SQLITE_DEBUG
2777 if( pParse->db->flags & SQLITE_VdbeAddopTrace ){
2778 printf("POP to %d\n", pParse->iCacheLevel);
2779 }
2780#endif
drhceea3322009-04-23 13:22:42 +00002781 for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
2782 if( p->iReg && p->iLevel>pParse->iCacheLevel ){
2783 cacheEntryClear(pParse, p);
drhceea3322009-04-23 13:22:42 +00002784 }
2785 }
2786}
drh945498f2007-02-24 11:52:52 +00002787
2788/*
drh5cd79232009-05-25 11:46:29 +00002789** When a cached column is reused, make sure that its register is
2790** no longer available as a temp register. ticket #3879: that same
2791** register might be in the cache in multiple places, so be sure to
2792** get them all.
2793*/
2794static void sqlite3ExprCachePinRegister(Parse *pParse, int iReg){
2795 int i;
2796 struct yColCache *p;
2797 for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
2798 if( p->iReg==iReg ){
2799 p->tempReg = 0;
2800 }
2801 }
2802}
2803
drh1f9ca2c2015-08-25 16:57:52 +00002804/* Generate code that will load into register regOut a value that is
2805** appropriate for the iIdxCol-th column of index pIdx.
2806*/
2807void sqlite3ExprCodeLoadIndexColumn(
2808 Parse *pParse, /* The parsing context */
2809 Index *pIdx, /* The index whose column is to be loaded */
2810 int iTabCur, /* Cursor pointing to a table row */
2811 int iIdxCol, /* The column of the index to be loaded */
2812 int regOut /* Store the index column value in this register */
2813){
2814 i16 iTabCol = pIdx->aiColumn[iIdxCol];
drh4b92f982015-09-29 17:20:14 +00002815 if( iTabCol==XN_EXPR ){
2816 assert( pIdx->aColExpr );
2817 assert( pIdx->aColExpr->nExpr>iIdxCol );
2818 pParse->iSelfTab = iTabCur;
drh1c75c9d2015-12-21 15:22:13 +00002819 sqlite3ExprCodeCopy(pParse, pIdx->aColExpr->a[iIdxCol].pExpr, regOut);
drh4b92f982015-09-29 17:20:14 +00002820 }else{
drh1f9ca2c2015-08-25 16:57:52 +00002821 sqlite3ExprCodeGetColumnOfTable(pParse->pVdbe, pIdx->pTable, iTabCur,
2822 iTabCol, regOut);
drh1f9ca2c2015-08-25 16:57:52 +00002823 }
drh1f9ca2c2015-08-25 16:57:52 +00002824}
2825
drh5cd79232009-05-25 11:46:29 +00002826/*
drh5c092e82010-05-14 19:24:02 +00002827** Generate code to extract the value of the iCol-th column of a table.
2828*/
2829void sqlite3ExprCodeGetColumnOfTable(
2830 Vdbe *v, /* The VDBE under construction */
2831 Table *pTab, /* The table containing the value */
drh313619f2013-10-31 20:34:06 +00002832 int iTabCur, /* The table cursor. Or the PK cursor for WITHOUT ROWID */
drh5c092e82010-05-14 19:24:02 +00002833 int iCol, /* Index of the column to extract */
drh313619f2013-10-31 20:34:06 +00002834 int regOut /* Extract the value into this register */
drh5c092e82010-05-14 19:24:02 +00002835){
2836 if( iCol<0 || iCol==pTab->iPKey ){
2837 sqlite3VdbeAddOp2(v, OP_Rowid, iTabCur, regOut);
2838 }else{
2839 int op = IsVirtual(pTab) ? OP_VColumn : OP_Column;
drhee0ec8e2013-10-31 17:38:01 +00002840 int x = iCol;
drh35db31b2016-06-02 23:13:21 +00002841 if( !HasRowid(pTab) && !IsVirtual(pTab) ){
drhee0ec8e2013-10-31 17:38:01 +00002842 x = sqlite3ColumnOfIndex(sqlite3PrimaryKeyIndex(pTab), iCol);
2843 }
2844 sqlite3VdbeAddOp3(v, op, iTabCur, x, regOut);
drh5c092e82010-05-14 19:24:02 +00002845 }
2846 if( iCol>=0 ){
2847 sqlite3ColumnDefault(v, pTab, iCol, regOut);
2848 }
2849}
2850
2851/*
drh945498f2007-02-24 11:52:52 +00002852** Generate code that will extract the iColumn-th column from
drhce78bc62015-10-15 19:21:51 +00002853** table pTab and store the column value in a register.
2854**
2855** An effort is made to store the column value in register iReg. This
2856** is not garanteeed for GetColumn() - the result can be stored in
2857** any register. But the result is guaranteed to land in register iReg
2858** for GetColumnToReg().
drhe55cbd72008-03-31 23:48:03 +00002859**
2860** There must be an open cursor to pTab in iTable when this routine
2861** is called. If iColumn<0 then code is generated that extracts the rowid.
drh945498f2007-02-24 11:52:52 +00002862*/
drhe55cbd72008-03-31 23:48:03 +00002863int sqlite3ExprCodeGetColumn(
2864 Parse *pParse, /* Parsing and code generating context */
drh2133d822008-01-03 18:44:59 +00002865 Table *pTab, /* Description of the table we are reading from */
2866 int iColumn, /* Index of the table column */
2867 int iTable, /* The cursor pointing to the table */
drha748fdc2012-03-28 01:34:47 +00002868 int iReg, /* Store results here */
drhce78bc62015-10-15 19:21:51 +00002869 u8 p5 /* P5 value for OP_Column + FLAGS */
drh2133d822008-01-03 18:44:59 +00002870){
drhe55cbd72008-03-31 23:48:03 +00002871 Vdbe *v = pParse->pVdbe;
2872 int i;
drhda250ea2008-04-01 05:07:14 +00002873 struct yColCache *p;
drhe55cbd72008-03-31 23:48:03 +00002874
drhceea3322009-04-23 13:22:42 +00002875 for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
drhb6da74e2009-12-24 16:00:28 +00002876 if( p->iReg>0 && p->iTable==iTable && p->iColumn==iColumn ){
drhceea3322009-04-23 13:22:42 +00002877 p->lru = pParse->iCacheCnt++;
drh5cd79232009-05-25 11:46:29 +00002878 sqlite3ExprCachePinRegister(pParse, p->iReg);
drhda250ea2008-04-01 05:07:14 +00002879 return p->iReg;
drhe55cbd72008-03-31 23:48:03 +00002880 }
2881 }
2882 assert( v!=0 );
drh5c092e82010-05-14 19:24:02 +00002883 sqlite3ExprCodeGetColumnOfTable(v, pTab, iTable, iColumn, iReg);
drha748fdc2012-03-28 01:34:47 +00002884 if( p5 ){
2885 sqlite3VdbeChangeP5(v, p5);
2886 }else{
2887 sqlite3ExprCacheStore(pParse, iTable, iColumn, iReg);
2888 }
drhe55cbd72008-03-31 23:48:03 +00002889 return iReg;
2890}
drhce78bc62015-10-15 19:21:51 +00002891void sqlite3ExprCodeGetColumnToReg(
2892 Parse *pParse, /* Parsing and code generating context */
2893 Table *pTab, /* Description of the table we are reading from */
2894 int iColumn, /* Index of the table column */
2895 int iTable, /* The cursor pointing to the table */
2896 int iReg /* Store results here */
2897){
2898 int r1 = sqlite3ExprCodeGetColumn(pParse, pTab, iColumn, iTable, iReg, 0);
2899 if( r1!=iReg ) sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, r1, iReg);
2900}
2901
drhe55cbd72008-03-31 23:48:03 +00002902
2903/*
drhceea3322009-04-23 13:22:42 +00002904** Clear all column cache entries.
drhe55cbd72008-03-31 23:48:03 +00002905*/
drhceea3322009-04-23 13:22:42 +00002906void sqlite3ExprCacheClear(Parse *pParse){
2907 int i;
2908 struct yColCache *p;
2909
drh9ac79622013-12-18 15:11:47 +00002910#if SQLITE_DEBUG
2911 if( pParse->db->flags & SQLITE_VdbeAddopTrace ){
2912 printf("CLEAR\n");
2913 }
2914#endif
drhceea3322009-04-23 13:22:42 +00002915 for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
2916 if( p->iReg ){
2917 cacheEntryClear(pParse, p);
drhe55cbd72008-03-31 23:48:03 +00002918 }
drhe55cbd72008-03-31 23:48:03 +00002919 }
2920}
2921
2922/*
drhda250ea2008-04-01 05:07:14 +00002923** Record the fact that an affinity change has occurred on iCount
2924** registers starting with iStart.
drhe55cbd72008-03-31 23:48:03 +00002925*/
drhda250ea2008-04-01 05:07:14 +00002926void sqlite3ExprCacheAffinityChange(Parse *pParse, int iStart, int iCount){
drhf49f3522009-12-30 14:12:38 +00002927 sqlite3ExprCacheRemove(pParse, iStart, iCount);
drhe55cbd72008-03-31 23:48:03 +00002928}
2929
2930/*
drhb21e7c72008-06-22 12:37:57 +00002931** Generate code to move content from registers iFrom...iFrom+nReg-1
2932** over to iTo..iTo+nReg-1. Keep the column cache up-to-date.
drhe55cbd72008-03-31 23:48:03 +00002933*/
drhb21e7c72008-06-22 12:37:57 +00002934void sqlite3ExprCodeMove(Parse *pParse, int iFrom, int iTo, int nReg){
drhe8e4af72012-09-21 00:04:28 +00002935 assert( iFrom>=iTo+nReg || iFrom+nReg<=iTo );
drh079a3072014-03-19 14:10:55 +00002936 sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move, iFrom, iTo, nReg);
drh236241a2014-09-12 17:41:30 +00002937 sqlite3ExprCacheRemove(pParse, iFrom, nReg);
drh945498f2007-02-24 11:52:52 +00002938}
2939
drhf49f3522009-12-30 14:12:38 +00002940#if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
drh92b01d52008-06-24 00:32:35 +00002941/*
drh652fbf52008-04-01 01:42:41 +00002942** Return true if any register in the range iFrom..iTo (inclusive)
2943** is used as part of the column cache.
drhf49f3522009-12-30 14:12:38 +00002944**
2945** This routine is used within assert() and testcase() macros only
2946** and does not appear in a normal build.
drh652fbf52008-04-01 01:42:41 +00002947*/
2948static int usedAsColumnCache(Parse *pParse, int iFrom, int iTo){
2949 int i;
drhceea3322009-04-23 13:22:42 +00002950 struct yColCache *p;
2951 for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
2952 int r = p->iReg;
drhf49f3522009-12-30 14:12:38 +00002953 if( r>=iFrom && r<=iTo ) return 1; /*NO_TEST*/
drh652fbf52008-04-01 01:42:41 +00002954 }
2955 return 0;
2956}
drhf49f3522009-12-30 14:12:38 +00002957#endif /* SQLITE_DEBUG || SQLITE_COVERAGE_TEST */
drh652fbf52008-04-01 01:42:41 +00002958
drhbea119c2016-04-11 18:15:37 +00002959
drh652fbf52008-04-01 01:42:41 +00002960/*
drha4c3c872013-09-12 17:29:25 +00002961** Convert an expression node to a TK_REGISTER
2962*/
2963static void exprToRegister(Expr *p, int iReg){
2964 p->op2 = p->op;
2965 p->op = TK_REGISTER;
2966 p->iTable = iReg;
2967 ExprClearProperty(p, EP_Skip);
2968}
2969
dan71c57db2016-07-09 20:23:55 +00002970static void exprCodeBetween(Parse*,Expr*,int,void(*)(Parse*,Expr*,int,int),int);
2971
drha4c3c872013-09-12 17:29:25 +00002972/*
drhcce7d172000-05-31 15:34:51 +00002973** Generate code into the current Vdbe to evaluate the given
drh2dcef112008-01-12 19:03:48 +00002974** expression. Attempt to store the results in register "target".
2975** Return the register where results are stored.
drh389a1ad2008-01-03 23:44:53 +00002976**
drh8b213892008-08-29 02:14:02 +00002977** With this routine, there is no guarantee that results will
drh2dcef112008-01-12 19:03:48 +00002978** be stored in target. The result might be stored in some other
2979** register if it is convenient to do so. The calling function
2980** must check the return code and move the results to the desired
2981** register.
drhcce7d172000-05-31 15:34:51 +00002982*/
drh678ccce2008-03-31 18:19:54 +00002983int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){
drh2dcef112008-01-12 19:03:48 +00002984 Vdbe *v = pParse->pVdbe; /* The VM under construction */
2985 int op; /* The opcode being coded */
2986 int inReg = target; /* Results stored in register inReg */
2987 int regFree1 = 0; /* If non-zero free this temporary register */
2988 int regFree2 = 0; /* If non-zero free this temporary register */
drh678ccce2008-03-31 18:19:54 +00002989 int r1, r2, r3, r4; /* Various register numbers */
drh20411ea2009-05-29 19:00:12 +00002990 sqlite3 *db = pParse->db; /* The database connection */
drh10d1edf2013-11-15 15:52:39 +00002991 Expr tempX; /* Temporary expression node */
dan71c57db2016-07-09 20:23:55 +00002992 int p5 = 0;
drhffe07b22005-11-03 00:41:17 +00002993
drh9cbf3422008-01-17 16:22:13 +00002994 assert( target>0 && target<=pParse->nMem );
drh20411ea2009-05-29 19:00:12 +00002995 if( v==0 ){
2996 assert( pParse->db->mallocFailed );
2997 return 0;
2998 }
drh389a1ad2008-01-03 23:44:53 +00002999
3000 if( pExpr==0 ){
3001 op = TK_NULL;
3002 }else{
3003 op = pExpr->op;
3004 }
drhf2bc0132004-10-04 13:19:23 +00003005 switch( op ){
drh13449892005-09-07 21:22:45 +00003006 case TK_AGG_COLUMN: {
3007 AggInfo *pAggInfo = pExpr->pAggInfo;
3008 struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg];
3009 if( !pAggInfo->directMode ){
drh9de221d2008-01-05 06:51:30 +00003010 assert( pCol->iMem>0 );
3011 inReg = pCol->iMem;
drh13449892005-09-07 21:22:45 +00003012 break;
3013 }else if( pAggInfo->useSortingIdx ){
dan5134d132011-09-02 10:31:11 +00003014 sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab,
drh389a1ad2008-01-03 23:44:53 +00003015 pCol->iSorterColumn, target);
drh13449892005-09-07 21:22:45 +00003016 break;
3017 }
3018 /* Otherwise, fall thru into the TK_COLUMN case */
3019 }
drh967e8b72000-06-21 13:59:10 +00003020 case TK_COLUMN: {
drhb2b9d3d2013-08-01 01:14:43 +00003021 int iTab = pExpr->iTable;
3022 if( iTab<0 ){
3023 if( pParse->ckBase>0 ){
3024 /* Generating CHECK constraints or inserting into partial index */
3025 inReg = pExpr->iColumn + pParse->ckBase;
3026 break;
3027 }else{
drh1f9ca2c2015-08-25 16:57:52 +00003028 /* Coding an expression that is part of an index where column names
3029 ** in the index refer to the table to which the index belongs */
3030 iTab = pParse->iSelfTab;
drhb2b9d3d2013-08-01 01:14:43 +00003031 }
drh22827922000-06-06 17:27:05 +00003032 }
drhb2b9d3d2013-08-01 01:14:43 +00003033 inReg = sqlite3ExprCodeGetColumn(pParse, pExpr->pTab,
3034 pExpr->iColumn, iTab, target,
3035 pExpr->op2);
drhcce7d172000-05-31 15:34:51 +00003036 break;
3037 }
3038 case TK_INTEGER: {
drh13573c72010-01-12 17:04:07 +00003039 codeInteger(pParse, pExpr, 0, target);
drhfec19aa2004-05-19 20:41:03 +00003040 break;
3041 }
drh13573c72010-01-12 17:04:07 +00003042#ifndef SQLITE_OMIT_FLOATING_POINT
drh598f1342007-10-23 15:39:45 +00003043 case TK_FLOAT: {
drh33e619f2009-05-28 01:00:55 +00003044 assert( !ExprHasProperty(pExpr, EP_IntValue) );
3045 codeReal(v, pExpr->u.zToken, 0, target);
drh598f1342007-10-23 15:39:45 +00003046 break;
3047 }
drh13573c72010-01-12 17:04:07 +00003048#endif
drhfec19aa2004-05-19 20:41:03 +00003049 case TK_STRING: {
drh33e619f2009-05-28 01:00:55 +00003050 assert( !ExprHasProperty(pExpr, EP_IntValue) );
drh076e85f2015-09-03 13:46:12 +00003051 sqlite3VdbeLoadString(v, target, pExpr->u.zToken);
drhcce7d172000-05-31 15:34:51 +00003052 break;
3053 }
drhf0863fe2005-06-12 21:35:51 +00003054 case TK_NULL: {
drh9de221d2008-01-05 06:51:30 +00003055 sqlite3VdbeAddOp2(v, OP_Null, 0, target);
drhf0863fe2005-06-12 21:35:51 +00003056 break;
3057 }
danielk19775338a5f2005-01-20 13:03:10 +00003058#ifndef SQLITE_OMIT_BLOB_LITERAL
danielk1977c572ef72004-05-27 09:28:41 +00003059 case TK_BLOB: {
drh6c8c6ce2005-08-23 11:17:58 +00003060 int n;
3061 const char *z;
drhca48c902008-01-18 14:08:24 +00003062 char *zBlob;
drh33e619f2009-05-28 01:00:55 +00003063 assert( !ExprHasProperty(pExpr, EP_IntValue) );
3064 assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
3065 assert( pExpr->u.zToken[1]=='\'' );
3066 z = &pExpr->u.zToken[2];
drhb7916a72009-05-27 10:31:29 +00003067 n = sqlite3Strlen30(z) - 1;
3068 assert( z[n]=='\'' );
drhca48c902008-01-18 14:08:24 +00003069 zBlob = sqlite3HexToBlob(sqlite3VdbeDb(v), z, n);
3070 sqlite3VdbeAddOp4(v, OP_Blob, n/2, target, 0, zBlob, P4_DYNAMIC);
danielk1977c572ef72004-05-27 09:28:41 +00003071 break;
3072 }
danielk19775338a5f2005-01-20 13:03:10 +00003073#endif
drh50457892003-09-06 01:10:47 +00003074 case TK_VARIABLE: {
drh33e619f2009-05-28 01:00:55 +00003075 assert( !ExprHasProperty(pExpr, EP_IntValue) );
3076 assert( pExpr->u.zToken!=0 );
3077 assert( pExpr->u.zToken[0]!=0 );
drheaf52d82010-05-12 13:50:23 +00003078 sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iColumn, target);
3079 if( pExpr->u.zToken[1]!=0 ){
drh04e9eea2011-06-01 19:16:06 +00003080 assert( pExpr->u.zToken[0]=='?'
3081 || strcmp(pExpr->u.zToken, pParse->azVar[pExpr->iColumn-1])==0 );
3082 sqlite3VdbeChangeP4(v, -1, pParse->azVar[pExpr->iColumn-1], P4_STATIC);
drh895d7472004-08-20 16:02:39 +00003083 }
drh50457892003-09-06 01:10:47 +00003084 break;
3085 }
drh4e0cff62004-11-05 05:10:28 +00003086 case TK_REGISTER: {
drh9de221d2008-01-05 06:51:30 +00003087 inReg = pExpr->iTable;
drh4e0cff62004-11-05 05:10:28 +00003088 break;
3089 }
drh487e2622005-06-25 18:42:14 +00003090#ifndef SQLITE_OMIT_CAST
3091 case TK_CAST: {
3092 /* Expressions of the form: CAST(pLeft AS token) */
drh2dcef112008-01-12 19:03:48 +00003093 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
drh1735fa82008-11-06 15:33:03 +00003094 if( inReg!=target ){
3095 sqlite3VdbeAddOp2(v, OP_SCopy, inReg, target);
3096 inReg = target;
3097 }
drh4169e432014-08-25 20:11:52 +00003098 sqlite3VdbeAddOp2(v, OP_Cast, target,
3099 sqlite3AffinityType(pExpr->u.zToken, 0));
drhc5499be2008-04-01 15:06:33 +00003100 testcase( usedAsColumnCache(pParse, inReg, inReg) );
drhb3843a82008-04-01 12:24:11 +00003101 sqlite3ExprCacheAffinityChange(pParse, inReg, 1);
drh487e2622005-06-25 18:42:14 +00003102 break;
3103 }
3104#endif /* SQLITE_OMIT_CAST */
dan71c57db2016-07-09 20:23:55 +00003105 case TK_IS:
3106 case TK_ISNOT:
3107 op = (op==TK_IS) ? TK_EQ : TK_NE;
3108 p5 = SQLITE_NULLEQ;
3109 /* fall-through */
drhc9b84a12002-06-20 11:36:48 +00003110 case TK_LT:
3111 case TK_LE:
3112 case TK_GT:
3113 case TK_GE:
3114 case TK_NE:
3115 case TK_EQ: {
dan71c57db2016-07-09 20:23:55 +00003116 Expr *pLeft = pExpr->pLeft;
3117 if( (pLeft->flags & EP_Vector) ){
3118 codeVectorCompare(pParse, pExpr, target);
3119 }else{
3120 r1 = sqlite3ExprCodeTemp(pParse, pLeft, &regFree1);
3121 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
3122 codeCompare(pParse, pLeft, pExpr->pRight, op,
3123 r1, r2, inReg, SQLITE_STOREP2 | p5);
3124 assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
3125 assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
3126 assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
3127 assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
3128 assert(TK_EQ==OP_Eq); testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq);
3129 assert(TK_NE==OP_Ne); testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne);
3130 testcase( regFree1==0 );
3131 testcase( regFree2==0 );
3132 }
drh6a2fe092009-09-23 02:29:36 +00003133 break;
3134 }
drhcce7d172000-05-31 15:34:51 +00003135 case TK_AND:
3136 case TK_OR:
3137 case TK_PLUS:
3138 case TK_STAR:
3139 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00003140 case TK_REM:
3141 case TK_BITAND:
3142 case TK_BITOR:
drh17c40292004-07-21 02:53:29 +00003143 case TK_SLASH:
drhbf4133c2001-10-13 02:59:08 +00003144 case TK_LSHIFT:
drh855eb1c2004-08-31 13:45:11 +00003145 case TK_RSHIFT:
drh00400772000-06-16 20:51:26 +00003146 case TK_CONCAT: {
drh7d176102014-02-18 03:07:12 +00003147 assert( TK_AND==OP_And ); testcase( op==TK_AND );
3148 assert( TK_OR==OP_Or ); testcase( op==TK_OR );
3149 assert( TK_PLUS==OP_Add ); testcase( op==TK_PLUS );
3150 assert( TK_MINUS==OP_Subtract ); testcase( op==TK_MINUS );
3151 assert( TK_REM==OP_Remainder ); testcase( op==TK_REM );
3152 assert( TK_BITAND==OP_BitAnd ); testcase( op==TK_BITAND );
3153 assert( TK_BITOR==OP_BitOr ); testcase( op==TK_BITOR );
3154 assert( TK_SLASH==OP_Divide ); testcase( op==TK_SLASH );
3155 assert( TK_LSHIFT==OP_ShiftLeft ); testcase( op==TK_LSHIFT );
3156 assert( TK_RSHIFT==OP_ShiftRight ); testcase( op==TK_RSHIFT );
3157 assert( TK_CONCAT==OP_Concat ); testcase( op==TK_CONCAT );
drh2dcef112008-01-12 19:03:48 +00003158 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
3159 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
drh5b6afba2008-01-05 16:29:28 +00003160 sqlite3VdbeAddOp3(v, op, r2, r1, target);
drhc5499be2008-04-01 15:06:33 +00003161 testcase( regFree1==0 );
3162 testcase( regFree2==0 );
drh00400772000-06-16 20:51:26 +00003163 break;
3164 }
drhcce7d172000-05-31 15:34:51 +00003165 case TK_UMINUS: {
drhfec19aa2004-05-19 20:41:03 +00003166 Expr *pLeft = pExpr->pLeft;
3167 assert( pLeft );
drh13573c72010-01-12 17:04:07 +00003168 if( pLeft->op==TK_INTEGER ){
3169 codeInteger(pParse, pLeft, 1, target);
3170#ifndef SQLITE_OMIT_FLOATING_POINT
3171 }else if( pLeft->op==TK_FLOAT ){
drh33e619f2009-05-28 01:00:55 +00003172 assert( !ExprHasProperty(pExpr, EP_IntValue) );
3173 codeReal(v, pLeft->u.zToken, 1, target);
drh13573c72010-01-12 17:04:07 +00003174#endif
drh3c84ddf2008-01-09 02:15:38 +00003175 }else{
drh10d1edf2013-11-15 15:52:39 +00003176 tempX.op = TK_INTEGER;
3177 tempX.flags = EP_IntValue|EP_TokenOnly;
3178 tempX.u.iValue = 0;
3179 r1 = sqlite3ExprCodeTemp(pParse, &tempX, &regFree1);
drhe55cbd72008-03-31 23:48:03 +00003180 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree2);
drh2dcef112008-01-12 19:03:48 +00003181 sqlite3VdbeAddOp3(v, OP_Subtract, r2, r1, target);
drhc5499be2008-04-01 15:06:33 +00003182 testcase( regFree2==0 );
drh6e142f52000-06-08 13:36:40 +00003183 }
drh3c84ddf2008-01-09 02:15:38 +00003184 inReg = target;
3185 break;
drh6e142f52000-06-08 13:36:40 +00003186 }
drhbf4133c2001-10-13 02:59:08 +00003187 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00003188 case TK_NOT: {
drh7d176102014-02-18 03:07:12 +00003189 assert( TK_BITNOT==OP_BitNot ); testcase( op==TK_BITNOT );
3190 assert( TK_NOT==OP_Not ); testcase( op==TK_NOT );
drhe99fa2a2008-12-15 15:27:51 +00003191 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
3192 testcase( regFree1==0 );
3193 inReg = target;
3194 sqlite3VdbeAddOp2(v, op, r1, inReg);
drhcce7d172000-05-31 15:34:51 +00003195 break;
3196 }
3197 case TK_ISNULL:
3198 case TK_NOTNULL: {
drh6a288a32008-01-07 19:20:24 +00003199 int addr;
drh7d176102014-02-18 03:07:12 +00003200 assert( TK_ISNULL==OP_IsNull ); testcase( op==TK_ISNULL );
3201 assert( TK_NOTNULL==OP_NotNull ); testcase( op==TK_NOTNULL );
drh9de221d2008-01-05 06:51:30 +00003202 sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
drh2dcef112008-01-12 19:03:48 +00003203 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
drhc5499be2008-04-01 15:06:33 +00003204 testcase( regFree1==0 );
drh2dcef112008-01-12 19:03:48 +00003205 addr = sqlite3VdbeAddOp1(v, op, r1);
drh7d176102014-02-18 03:07:12 +00003206 VdbeCoverageIf(v, op==TK_ISNULL);
3207 VdbeCoverageIf(v, op==TK_NOTNULL);
drha9769792014-08-04 16:39:39 +00003208 sqlite3VdbeAddOp2(v, OP_Integer, 0, target);
drh6a288a32008-01-07 19:20:24 +00003209 sqlite3VdbeJumpHere(v, addr);
drhf2bc0132004-10-04 13:19:23 +00003210 break;
drhcce7d172000-05-31 15:34:51 +00003211 }
drh22827922000-06-06 17:27:05 +00003212 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00003213 AggInfo *pInfo = pExpr->pAggInfo;
drh7e56e712005-11-16 12:53:15 +00003214 if( pInfo==0 ){
drh33e619f2009-05-28 01:00:55 +00003215 assert( !ExprHasProperty(pExpr, EP_IntValue) );
3216 sqlite3ErrorMsg(pParse, "misuse of aggregate: %s()", pExpr->u.zToken);
drh7e56e712005-11-16 12:53:15 +00003217 }else{
drh9de221d2008-01-05 06:51:30 +00003218 inReg = pInfo->aFunc[pExpr->iAgg].iMem;
drh7e56e712005-11-16 12:53:15 +00003219 }
drh22827922000-06-06 17:27:05 +00003220 break;
3221 }
drhcce7d172000-05-31 15:34:51 +00003222 case TK_FUNCTION: {
drh12ffee82009-04-08 13:51:51 +00003223 ExprList *pFarg; /* List of function arguments */
3224 int nFarg; /* Number of function arguments */
3225 FuncDef *pDef; /* The function definition object */
drh12ffee82009-04-08 13:51:51 +00003226 const char *zId; /* The function name */
drh693e6712014-01-24 22:58:00 +00003227 u32 constMask = 0; /* Mask of function arguments that are constant */
drh12ffee82009-04-08 13:51:51 +00003228 int i; /* Loop counter */
3229 u8 enc = ENC(db); /* The text encoding used by this database */
3230 CollSeq *pColl = 0; /* A collating sequence */
drh17435752007-08-16 04:30:38 +00003231
danielk19776ab3a2e2009-02-19 14:39:25 +00003232 assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
drhc5cd1242013-09-12 16:50:49 +00003233 if( ExprHasProperty(pExpr, EP_TokenOnly) ){
drh12ffee82009-04-08 13:51:51 +00003234 pFarg = 0;
3235 }else{
3236 pFarg = pExpr->x.pList;
3237 }
3238 nFarg = pFarg ? pFarg->nExpr : 0;
drh33e619f2009-05-28 01:00:55 +00003239 assert( !ExprHasProperty(pExpr, EP_IntValue) );
3240 zId = pExpr->u.zToken;
drh80738d92016-02-15 00:34:16 +00003241 pDef = sqlite3FindFunction(db, zId, nFarg, enc, 0);
drh2d801512016-01-14 22:19:58 +00003242 if( pDef==0 || pDef->xFinalize!=0 ){
drh80738d92016-02-15 00:34:16 +00003243 sqlite3ErrorMsg(pParse, "unknown function: %s()", zId);
drhfeb306f2009-08-18 16:05:46 +00003244 break;
3245 }
drhae6bb952009-11-11 00:24:31 +00003246
3247 /* Attempt a direct implementation of the built-in COALESCE() and
peter.d.reid60ec9142014-09-06 16:39:46 +00003248 ** IFNULL() functions. This avoids unnecessary evaluation of
drhae6bb952009-11-11 00:24:31 +00003249 ** arguments past the first non-NULL argument.
3250 */
drhd36e1042013-09-06 13:10:12 +00003251 if( pDef->funcFlags & SQLITE_FUNC_COALESCE ){
drhae6bb952009-11-11 00:24:31 +00003252 int endCoalesce = sqlite3VdbeMakeLabel(v);
3253 assert( nFarg>=2 );
3254 sqlite3ExprCode(pParse, pFarg->a[0].pExpr, target);
3255 for(i=1; i<nFarg; i++){
3256 sqlite3VdbeAddOp2(v, OP_NotNull, target, endCoalesce);
drh688852a2014-02-17 22:40:43 +00003257 VdbeCoverage(v);
drhf49f3522009-12-30 14:12:38 +00003258 sqlite3ExprCacheRemove(pParse, target, 1);
drhae6bb952009-11-11 00:24:31 +00003259 sqlite3ExprCachePush(pParse);
3260 sqlite3ExprCode(pParse, pFarg->a[i].pExpr, target);
drhd2490902014-04-13 19:28:15 +00003261 sqlite3ExprCachePop(pParse);
drhae6bb952009-11-11 00:24:31 +00003262 }
3263 sqlite3VdbeResolveLabel(v, endCoalesce);
3264 break;
3265 }
3266
drhcca9f3d2013-09-06 15:23:29 +00003267 /* The UNLIKELY() function is a no-op. The result is the value
3268 ** of the first argument.
3269 */
3270 if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){
3271 assert( nFarg>=1 );
drh5f02ab02015-06-20 13:18:34 +00003272 inReg = sqlite3ExprCodeTarget(pParse, pFarg->a[0].pExpr, target);
drhcca9f3d2013-09-06 15:23:29 +00003273 break;
3274 }
drhae6bb952009-11-11 00:24:31 +00003275
drhd1a01ed2013-11-21 16:08:52 +00003276 for(i=0; i<nFarg; i++){
3277 if( i<32 && sqlite3ExprIsConstant(pFarg->a[i].pExpr) ){
drh693e6712014-01-24 22:58:00 +00003278 testcase( i==31 );
3279 constMask |= MASKBIT32(i);
drhd1a01ed2013-11-21 16:08:52 +00003280 }
3281 if( (pDef->funcFlags & SQLITE_FUNC_NEEDCOLL)!=0 && !pColl ){
3282 pColl = sqlite3ExprCollSeq(pParse, pFarg->a[i].pExpr);
3283 }
3284 }
drh12ffee82009-04-08 13:51:51 +00003285 if( pFarg ){
drhd1a01ed2013-11-21 16:08:52 +00003286 if( constMask ){
3287 r1 = pParse->nMem+1;
3288 pParse->nMem += nFarg;
3289 }else{
3290 r1 = sqlite3GetTempRange(pParse, nFarg);
3291 }
drha748fdc2012-03-28 01:34:47 +00003292
3293 /* For length() and typeof() functions with a column argument,
3294 ** set the P5 parameter to the OP_Column opcode to OPFLAG_LENGTHARG
3295 ** or OPFLAG_TYPEOFARG respectively, to avoid unnecessary data
3296 ** loading.
3297 */
drhd36e1042013-09-06 13:10:12 +00003298 if( (pDef->funcFlags & (SQLITE_FUNC_LENGTH|SQLITE_FUNC_TYPEOF))!=0 ){
drh4e245a42012-03-30 00:00:36 +00003299 u8 exprOp;
drha748fdc2012-03-28 01:34:47 +00003300 assert( nFarg==1 );
3301 assert( pFarg->a[0].pExpr!=0 );
drh4e245a42012-03-30 00:00:36 +00003302 exprOp = pFarg->a[0].pExpr->op;
3303 if( exprOp==TK_COLUMN || exprOp==TK_AGG_COLUMN ){
drha748fdc2012-03-28 01:34:47 +00003304 assert( SQLITE_FUNC_LENGTH==OPFLAG_LENGTHARG );
3305 assert( SQLITE_FUNC_TYPEOF==OPFLAG_TYPEOFARG );
drhb1fba282013-11-21 14:33:48 +00003306 testcase( pDef->funcFlags & OPFLAG_LENGTHARG );
3307 pFarg->a[0].pExpr->op2 =
3308 pDef->funcFlags & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG);
drha748fdc2012-03-28 01:34:47 +00003309 }
3310 }
3311
drhd7d385d2009-09-03 01:18:00 +00003312 sqlite3ExprCachePush(pParse); /* Ticket 2ea2425d34be */
drh5579d592015-08-26 14:01:41 +00003313 sqlite3ExprCodeExprList(pParse, pFarg, r1, 0,
drhd1a01ed2013-11-21 16:08:52 +00003314 SQLITE_ECEL_DUP|SQLITE_ECEL_FACTOR);
drhd2490902014-04-13 19:28:15 +00003315 sqlite3ExprCachePop(pParse); /* Ticket 2ea2425d34be */
drh892d3172008-01-10 03:46:36 +00003316 }else{
drh12ffee82009-04-08 13:51:51 +00003317 r1 = 0;
drh892d3172008-01-10 03:46:36 +00003318 }
drhb7f6f682006-07-08 17:06:43 +00003319#ifndef SQLITE_OMIT_VIRTUALTABLE
drha43fa222006-07-08 18:41:37 +00003320 /* Possibly overload the function if the first argument is
3321 ** a virtual table column.
3322 **
3323 ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the
3324 ** second argument, not the first, as the argument to test to
3325 ** see if it is a column in a virtual table. This is done because
3326 ** the left operand of infix functions (the operand we want to
3327 ** control overloading) ends up as the second argument to the
3328 ** function. The expression "A glob B" is equivalent to
3329 ** "glob(B,A). We want to use the A in "A glob B" to test
3330 ** for function overloading. But we use the B term in "glob(B,A)".
3331 */
drh12ffee82009-04-08 13:51:51 +00003332 if( nFarg>=2 && (pExpr->flags & EP_InfixFunc) ){
3333 pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[1].pExpr);
3334 }else if( nFarg>0 ){
3335 pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[0].pExpr);
drhb7f6f682006-07-08 17:06:43 +00003336 }
3337#endif
drhd36e1042013-09-06 13:10:12 +00003338 if( pDef->funcFlags & SQLITE_FUNC_NEEDCOLL ){
drh8b213892008-08-29 02:14:02 +00003339 if( !pColl ) pColl = db->pDfltColl;
drh66a51672008-01-03 00:01:23 +00003340 sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ);
danielk1977682f68b2004-06-05 10:22:17 +00003341 }
drh9c7c9132015-06-26 18:16:52 +00003342 sqlite3VdbeAddOp4(v, OP_Function0, constMask, r1, target,
drh66a51672008-01-03 00:01:23 +00003343 (char*)pDef, P4_FUNCDEF);
drh12ffee82009-04-08 13:51:51 +00003344 sqlite3VdbeChangeP5(v, (u8)nFarg);
drhd1a01ed2013-11-21 16:08:52 +00003345 if( nFarg && constMask==0 ){
drh12ffee82009-04-08 13:51:51 +00003346 sqlite3ReleaseTempRange(pParse, r1, nFarg);
drh2dcef112008-01-12 19:03:48 +00003347 }
drhcce7d172000-05-31 15:34:51 +00003348 break;
3349 }
drhfe2093d2005-01-20 22:48:47 +00003350#ifndef SQLITE_OMIT_SUBQUERY
3351 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00003352 case TK_SELECT: {
drhc5499be2008-04-01 15:06:33 +00003353 testcase( op==TK_EXISTS );
3354 testcase( op==TK_SELECT );
drh1450bc62009-10-30 13:25:56 +00003355 inReg = sqlite3CodeSubselect(pParse, pExpr, 0, 0);
drh19a775c2000-06-05 18:54:46 +00003356 break;
3357 }
drhfef52082000-06-06 01:50:43 +00003358 case TK_IN: {
drhe3365e62009-11-12 17:52:24 +00003359 int destIfFalse = sqlite3VdbeMakeLabel(v);
3360 int destIfNull = sqlite3VdbeMakeLabel(v);
3361 sqlite3VdbeAddOp2(v, OP_Null, 0, target);
3362 sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull);
3363 sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
3364 sqlite3VdbeResolveLabel(v, destIfFalse);
3365 sqlite3VdbeAddOp2(v, OP_AddImm, target, 0);
3366 sqlite3VdbeResolveLabel(v, destIfNull);
drhfef52082000-06-06 01:50:43 +00003367 break;
3368 }
drhe3365e62009-11-12 17:52:24 +00003369#endif /* SQLITE_OMIT_SUBQUERY */
3370
3371
drh2dcef112008-01-12 19:03:48 +00003372 /*
3373 ** x BETWEEN y AND z
3374 **
3375 ** This is equivalent to
3376 **
3377 ** x>=y AND x<=z
3378 **
3379 ** X is stored in pExpr->pLeft.
3380 ** Y is stored in pExpr->pList->a[0].pExpr.
3381 ** Z is stored in pExpr->pList->a[1].pExpr.
3382 */
drhfef52082000-06-06 01:50:43 +00003383 case TK_BETWEEN: {
dan71c57db2016-07-09 20:23:55 +00003384 exprCodeBetween(pParse, pExpr, target, 0, 0);
3385#if 0
drhbe5c89a2004-07-26 00:31:09 +00003386 Expr *pLeft = pExpr->pLeft;
danielk19776ab3a2e2009-02-19 14:39:25 +00003387 struct ExprList_item *pLItem = pExpr->x.pList->a;
drhbe5c89a2004-07-26 00:31:09 +00003388 Expr *pRight = pLItem->pExpr;
drh35573352008-01-08 23:54:25 +00003389
drhb6da74e2009-12-24 16:00:28 +00003390 r1 = sqlite3ExprCodeTemp(pParse, pLeft, &regFree1);
3391 r2 = sqlite3ExprCodeTemp(pParse, pRight, &regFree2);
drhc5499be2008-04-01 15:06:33 +00003392 testcase( regFree1==0 );
3393 testcase( regFree2==0 );
drh2dcef112008-01-12 19:03:48 +00003394 r3 = sqlite3GetTempReg(pParse);
drh678ccce2008-03-31 18:19:54 +00003395 r4 = sqlite3GetTempReg(pParse);
drh35573352008-01-08 23:54:25 +00003396 codeCompare(pParse, pLeft, pRight, OP_Ge,
drh7d176102014-02-18 03:07:12 +00003397 r1, r2, r3, SQLITE_STOREP2); VdbeCoverage(v);
drhbe5c89a2004-07-26 00:31:09 +00003398 pLItem++;
3399 pRight = pLItem->pExpr;
drh2dcef112008-01-12 19:03:48 +00003400 sqlite3ReleaseTempReg(pParse, regFree2);
3401 r2 = sqlite3ExprCodeTemp(pParse, pRight, &regFree2);
drhc5499be2008-04-01 15:06:33 +00003402 testcase( regFree2==0 );
drh678ccce2008-03-31 18:19:54 +00003403 codeCompare(pParse, pLeft, pRight, OP_Le, r1, r2, r4, SQLITE_STOREP2);
drh688852a2014-02-17 22:40:43 +00003404 VdbeCoverage(v);
drh678ccce2008-03-31 18:19:54 +00003405 sqlite3VdbeAddOp3(v, OP_And, r3, r4, target);
drh2dcef112008-01-12 19:03:48 +00003406 sqlite3ReleaseTempReg(pParse, r3);
drh678ccce2008-03-31 18:19:54 +00003407 sqlite3ReleaseTempReg(pParse, r4);
dan71c57db2016-07-09 20:23:55 +00003408#endif
drhfef52082000-06-06 01:50:43 +00003409 break;
3410 }
drh94fa9c42016-02-27 21:16:04 +00003411 case TK_SPAN:
drhae80dde2012-12-06 21:16:43 +00003412 case TK_COLLATE:
drh4f07e5f2007-05-14 11:34:46 +00003413 case TK_UPLUS: {
drh2dcef112008-01-12 19:03:48 +00003414 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
drha2e00042002-01-22 03:13:42 +00003415 break;
3416 }
drh2dcef112008-01-12 19:03:48 +00003417
dan165921a2009-08-28 18:53:45 +00003418 case TK_TRIGGER: {
dan65a7cd12009-09-01 12:16:01 +00003419 /* If the opcode is TK_TRIGGER, then the expression is a reference
3420 ** to a column in the new.* or old.* pseudo-tables available to
3421 ** trigger programs. In this case Expr.iTable is set to 1 for the
3422 ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
3423 ** is set to the column of the pseudo-table to read, or to -1 to
3424 ** read the rowid field.
3425 **
3426 ** The expression is implemented using an OP_Param opcode. The p1
3427 ** parameter is set to 0 for an old.rowid reference, or to (i+1)
3428 ** to reference another column of the old.* pseudo-table, where
3429 ** i is the index of the column. For a new.rowid reference, p1 is
3430 ** set to (n+1), where n is the number of columns in each pseudo-table.
3431 ** For a reference to any other column in the new.* pseudo-table, p1
3432 ** is set to (n+2+i), where n and i are as defined previously. For
3433 ** example, if the table on which triggers are being fired is
3434 ** declared as:
3435 **
3436 ** CREATE TABLE t1(a, b);
3437 **
3438 ** Then p1 is interpreted as follows:
3439 **
3440 ** p1==0 -> old.rowid p1==3 -> new.rowid
3441 ** p1==1 -> old.a p1==4 -> new.a
3442 ** p1==2 -> old.b p1==5 -> new.b
3443 */
dan2832ad42009-08-31 15:27:27 +00003444 Table *pTab = pExpr->pTab;
dan65a7cd12009-09-01 12:16:01 +00003445 int p1 = pExpr->iTable * (pTab->nCol+1) + 1 + pExpr->iColumn;
3446
3447 assert( pExpr->iTable==0 || pExpr->iTable==1 );
3448 assert( pExpr->iColumn>=-1 && pExpr->iColumn<pTab->nCol );
3449 assert( pTab->iPKey<0 || pExpr->iColumn!=pTab->iPKey );
3450 assert( p1>=0 && p1<(pTab->nCol*2+2) );
3451
3452 sqlite3VdbeAddOp2(v, OP_Param, p1, target);
dan2bd93512009-08-31 08:22:46 +00003453 VdbeComment((v, "%s.%s -> $%d",
3454 (pExpr->iTable ? "new" : "old"),
dan76d462e2009-08-30 11:42:51 +00003455 (pExpr->iColumn<0 ? "rowid" : pExpr->pTab->aCol[pExpr->iColumn].zName),
dan2bd93512009-08-31 08:22:46 +00003456 target
dan165921a2009-08-28 18:53:45 +00003457 ));
dan65a7cd12009-09-01 12:16:01 +00003458
drh44dbca82010-01-13 04:22:20 +00003459#ifndef SQLITE_OMIT_FLOATING_POINT
dan65a7cd12009-09-01 12:16:01 +00003460 /* If the column has REAL affinity, it may currently be stored as an
drh113762a2014-11-19 16:36:25 +00003461 ** integer. Use OP_RealAffinity to make sure it is really real.
3462 **
3463 ** EVIDENCE-OF: R-60985-57662 SQLite will convert the value back to
3464 ** floating point when extracting it from the record. */
dan2832ad42009-08-31 15:27:27 +00003465 if( pExpr->iColumn>=0
3466 && pTab->aCol[pExpr->iColumn].affinity==SQLITE_AFF_REAL
3467 ){
3468 sqlite3VdbeAddOp1(v, OP_RealAffinity, target);
3469 }
drh44dbca82010-01-13 04:22:20 +00003470#endif
dan165921a2009-08-28 18:53:45 +00003471 break;
3472 }
3473
dan71c57db2016-07-09 20:23:55 +00003474 case TK_VECTOR: {
3475 sqlite3ErrorMsg(pParse, "invalid use of row value (1)");
3476 break;
3477 }
3478
3479 case TK_SELECT_COLUMN: {
3480 Expr *pLeft = pExpr->pLeft;
3481 assert( pLeft );
3482 assert( pLeft->op==TK_SELECT || pLeft->op==TK_REGISTER );
3483 if( pLeft->op==TK_SELECT ){
3484 pLeft->iTable = sqlite3CodeSubselect(pParse, pLeft, 0, 0);
3485 pLeft->op = TK_REGISTER;
3486 }
3487 inReg = pLeft->iTable + pExpr->iColumn;
3488 break;
3489 }
dan165921a2009-08-28 18:53:45 +00003490
drh2dcef112008-01-12 19:03:48 +00003491 /*
3492 ** Form A:
3493 ** CASE x WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
3494 **
3495 ** Form B:
3496 ** CASE WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
3497 **
3498 ** Form A is can be transformed into the equivalent form B as follows:
3499 ** CASE WHEN x=e1 THEN r1 WHEN x=e2 THEN r2 ...
3500 ** WHEN x=eN THEN rN ELSE y END
3501 **
3502 ** X (if it exists) is in pExpr->pLeft.
drhc5cd1242013-09-12 16:50:49 +00003503 ** Y is in the last element of pExpr->x.pList if pExpr->x.pList->nExpr is
3504 ** odd. The Y is also optional. If the number of elements in x.pList
3505 ** is even, then Y is omitted and the "otherwise" result is NULL.
drh2dcef112008-01-12 19:03:48 +00003506 ** Ei is in pExpr->pList->a[i*2] and Ri is pExpr->pList->a[i*2+1].
3507 **
3508 ** The result of the expression is the Ri for the first matching Ei,
3509 ** or if there is no matching Ei, the ELSE term Y, or if there is
3510 ** no ELSE term, NULL.
3511 */
drh33cd4902009-05-30 20:49:20 +00003512 default: assert( op==TK_CASE ); {
drh2dcef112008-01-12 19:03:48 +00003513 int endLabel; /* GOTO label for end of CASE stmt */
3514 int nextCase; /* GOTO label for next WHEN clause */
3515 int nExpr; /* 2x number of WHEN terms */
3516 int i; /* Loop counter */
3517 ExprList *pEList; /* List of WHEN terms */
3518 struct ExprList_item *aListelem; /* Array of WHEN terms */
3519 Expr opCompare; /* The X==Ei expression */
drh2dcef112008-01-12 19:03:48 +00003520 Expr *pX; /* The X expression */
drh1bd10f82008-12-10 21:19:56 +00003521 Expr *pTest = 0; /* X==Ei (form A) or just Ei (form B) */
drhceea3322009-04-23 13:22:42 +00003522 VVA_ONLY( int iCacheLevel = pParse->iCacheLevel; )
drh17a7f8d2002-03-24 13:13:27 +00003523
danielk19776ab3a2e2009-02-19 14:39:25 +00003524 assert( !ExprHasProperty(pExpr, EP_xIsSelect) && pExpr->x.pList );
danielk19776ab3a2e2009-02-19 14:39:25 +00003525 assert(pExpr->x.pList->nExpr > 0);
3526 pEList = pExpr->x.pList;
drhbe5c89a2004-07-26 00:31:09 +00003527 aListelem = pEList->a;
3528 nExpr = pEList->nExpr;
drh2dcef112008-01-12 19:03:48 +00003529 endLabel = sqlite3VdbeMakeLabel(v);
3530 if( (pX = pExpr->pLeft)!=0 ){
drh10d1edf2013-11-15 15:52:39 +00003531 tempX = *pX;
drh33cd4902009-05-30 20:49:20 +00003532 testcase( pX->op==TK_COLUMN );
drh10d1edf2013-11-15 15:52:39 +00003533 exprToRegister(&tempX, sqlite3ExprCodeTemp(pParse, pX, &regFree1));
drhc5499be2008-04-01 15:06:33 +00003534 testcase( regFree1==0 );
drh2dcef112008-01-12 19:03:48 +00003535 opCompare.op = TK_EQ;
drh10d1edf2013-11-15 15:52:39 +00003536 opCompare.pLeft = &tempX;
drh2dcef112008-01-12 19:03:48 +00003537 pTest = &opCompare;
drh8b1db072010-09-28 04:14:03 +00003538 /* Ticket b351d95f9cd5ef17e9d9dbae18f5ca8611190001:
3539 ** The value in regFree1 might get SCopy-ed into the file result.
3540 ** So make sure that the regFree1 register is not reused for other
3541 ** purposes and possibly overwritten. */
3542 regFree1 = 0;
drh17a7f8d2002-03-24 13:13:27 +00003543 }
drhc5cd1242013-09-12 16:50:49 +00003544 for(i=0; i<nExpr-1; i=i+2){
drhceea3322009-04-23 13:22:42 +00003545 sqlite3ExprCachePush(pParse);
drh2dcef112008-01-12 19:03:48 +00003546 if( pX ){
drh1bd10f82008-12-10 21:19:56 +00003547 assert( pTest!=0 );
drh2dcef112008-01-12 19:03:48 +00003548 opCompare.pRight = aListelem[i].pExpr;
drh17a7f8d2002-03-24 13:13:27 +00003549 }else{
drh2dcef112008-01-12 19:03:48 +00003550 pTest = aListelem[i].pExpr;
drh17a7f8d2002-03-24 13:13:27 +00003551 }
drh2dcef112008-01-12 19:03:48 +00003552 nextCase = sqlite3VdbeMakeLabel(v);
drh33cd4902009-05-30 20:49:20 +00003553 testcase( pTest->op==TK_COLUMN );
drh2dcef112008-01-12 19:03:48 +00003554 sqlite3ExprIfFalse(pParse, pTest, nextCase, SQLITE_JUMPIFNULL);
drhc5499be2008-04-01 15:06:33 +00003555 testcase( aListelem[i+1].pExpr->op==TK_COLUMN );
drh9de221d2008-01-05 06:51:30 +00003556 sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target);
drh076e85f2015-09-03 13:46:12 +00003557 sqlite3VdbeGoto(v, endLabel);
drhd2490902014-04-13 19:28:15 +00003558 sqlite3ExprCachePop(pParse);
drh2dcef112008-01-12 19:03:48 +00003559 sqlite3VdbeResolveLabel(v, nextCase);
drhf570f012002-05-31 15:51:25 +00003560 }
drhc5cd1242013-09-12 16:50:49 +00003561 if( (nExpr&1)!=0 ){
drhceea3322009-04-23 13:22:42 +00003562 sqlite3ExprCachePush(pParse);
drhc5cd1242013-09-12 16:50:49 +00003563 sqlite3ExprCode(pParse, pEList->a[nExpr-1].pExpr, target);
drhd2490902014-04-13 19:28:15 +00003564 sqlite3ExprCachePop(pParse);
drh17a7f8d2002-03-24 13:13:27 +00003565 }else{
drh9de221d2008-01-05 06:51:30 +00003566 sqlite3VdbeAddOp2(v, OP_Null, 0, target);
drh17a7f8d2002-03-24 13:13:27 +00003567 }
danielk1977c1f4a192009-04-28 12:08:15 +00003568 assert( db->mallocFailed || pParse->nErr>0
3569 || pParse->iCacheLevel==iCacheLevel );
drh2dcef112008-01-12 19:03:48 +00003570 sqlite3VdbeResolveLabel(v, endLabel);
danielk19776f349032002-06-11 02:25:40 +00003571 break;
3572 }
danielk19775338a5f2005-01-20 13:03:10 +00003573#ifndef SQLITE_OMIT_TRIGGER
danielk19776f349032002-06-11 02:25:40 +00003574 case TK_RAISE: {
dan165921a2009-08-28 18:53:45 +00003575 assert( pExpr->affinity==OE_Rollback
3576 || pExpr->affinity==OE_Abort
3577 || pExpr->affinity==OE_Fail
3578 || pExpr->affinity==OE_Ignore
3579 );
dane0af83a2009-09-08 19:15:01 +00003580 if( !pParse->pTriggerTab ){
3581 sqlite3ErrorMsg(pParse,
3582 "RAISE() may only be used within a trigger-program");
3583 return 0;
3584 }
3585 if( pExpr->affinity==OE_Abort ){
3586 sqlite3MayAbort(pParse);
3587 }
dan165921a2009-08-28 18:53:45 +00003588 assert( !ExprHasProperty(pExpr, EP_IntValue) );
dane0af83a2009-09-08 19:15:01 +00003589 if( pExpr->affinity==OE_Ignore ){
3590 sqlite3VdbeAddOp4(
3591 v, OP_Halt, SQLITE_OK, OE_Ignore, 0, pExpr->u.zToken,0);
drh688852a2014-02-17 22:40:43 +00003592 VdbeCoverage(v);
dane0af83a2009-09-08 19:15:01 +00003593 }else{
drh433dccf2013-02-09 15:37:11 +00003594 sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_TRIGGER,
drhf9c8ce32013-11-05 13:33:55 +00003595 pExpr->affinity, pExpr->u.zToken, 0, 0);
dane0af83a2009-09-08 19:15:01 +00003596 }
3597
drhffe07b22005-11-03 00:41:17 +00003598 break;
drh17a7f8d2002-03-24 13:13:27 +00003599 }
danielk19775338a5f2005-01-20 13:03:10 +00003600#endif
drhffe07b22005-11-03 00:41:17 +00003601 }
drh2dcef112008-01-12 19:03:48 +00003602 sqlite3ReleaseTempReg(pParse, regFree1);
3603 sqlite3ReleaseTempReg(pParse, regFree2);
3604 return inReg;
3605}
3606
3607/*
drhd1a01ed2013-11-21 16:08:52 +00003608** Factor out the code of the given expression to initialization time.
3609*/
drhd673cdd2013-11-21 21:23:31 +00003610void sqlite3ExprCodeAtInit(
3611 Parse *pParse, /* Parsing context */
3612 Expr *pExpr, /* The expression to code when the VDBE initializes */
3613 int regDest, /* Store the value in this register */
3614 u8 reusable /* True if this expression is reusable */
3615){
drhd1a01ed2013-11-21 16:08:52 +00003616 ExprList *p;
drhd9f158e2013-11-21 20:48:42 +00003617 assert( ConstFactorOk(pParse) );
drhd1a01ed2013-11-21 16:08:52 +00003618 p = pParse->pConstExpr;
3619 pExpr = sqlite3ExprDup(pParse->db, pExpr, 0);
3620 p = sqlite3ExprListAppend(pParse, p, pExpr);
drhd673cdd2013-11-21 21:23:31 +00003621 if( p ){
3622 struct ExprList_item *pItem = &p->a[p->nExpr-1];
3623 pItem->u.iConstExprReg = regDest;
3624 pItem->reusable = reusable;
3625 }
drhd1a01ed2013-11-21 16:08:52 +00003626 pParse->pConstExpr = p;
3627}
3628
3629/*
drh2dcef112008-01-12 19:03:48 +00003630** Generate code to evaluate an expression and store the results
3631** into a register. Return the register number where the results
3632** are stored.
3633**
3634** If the register is a temporary register that can be deallocated,
drh678ccce2008-03-31 18:19:54 +00003635** then write its number into *pReg. If the result register is not
drh2dcef112008-01-12 19:03:48 +00003636** a temporary, then set *pReg to zero.
drhf30a9692013-11-15 01:10:18 +00003637**
3638** If pExpr is a constant, then this routine might generate this
3639** code to fill the register in the initialization section of the
3640** VDBE program, in order to factor it out of the evaluation loop.
drh2dcef112008-01-12 19:03:48 +00003641*/
3642int sqlite3ExprCodeTemp(Parse *pParse, Expr *pExpr, int *pReg){
drhf30a9692013-11-15 01:10:18 +00003643 int r2;
3644 pExpr = sqlite3ExprSkipCollate(pExpr);
drhd9f158e2013-11-21 20:48:42 +00003645 if( ConstFactorOk(pParse)
drhf30a9692013-11-15 01:10:18 +00003646 && pExpr->op!=TK_REGISTER
3647 && sqlite3ExprIsConstantNotJoin(pExpr)
3648 ){
3649 ExprList *p = pParse->pConstExpr;
3650 int i;
3651 *pReg = 0;
3652 if( p ){
drhd673cdd2013-11-21 21:23:31 +00003653 struct ExprList_item *pItem;
3654 for(pItem=p->a, i=p->nExpr; i>0; pItem++, i--){
3655 if( pItem->reusable && sqlite3ExprCompare(pItem->pExpr,pExpr,-1)==0 ){
3656 return pItem->u.iConstExprReg;
drhf30a9692013-11-15 01:10:18 +00003657 }
3658 }
3659 }
drhf30a9692013-11-15 01:10:18 +00003660 r2 = ++pParse->nMem;
drhd673cdd2013-11-21 21:23:31 +00003661 sqlite3ExprCodeAtInit(pParse, pExpr, r2, 1);
drh2dcef112008-01-12 19:03:48 +00003662 }else{
drhf30a9692013-11-15 01:10:18 +00003663 int r1 = sqlite3GetTempReg(pParse);
3664 r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
3665 if( r2==r1 ){
3666 *pReg = r1;
3667 }else{
3668 sqlite3ReleaseTempReg(pParse, r1);
3669 *pReg = 0;
3670 }
drh2dcef112008-01-12 19:03:48 +00003671 }
3672 return r2;
3673}
3674
3675/*
3676** Generate code that will evaluate expression pExpr and store the
3677** results in register target. The results are guaranteed to appear
3678** in register target.
3679*/
drh05a86c52014-02-16 01:55:49 +00003680void sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){
drh9cbf3422008-01-17 16:22:13 +00003681 int inReg;
3682
3683 assert( target>0 && target<=pParse->nMem );
drhebc16712010-09-28 00:25:58 +00003684 if( pExpr && pExpr->op==TK_REGISTER ){
3685 sqlite3VdbeAddOp2(pParse->pVdbe, OP_Copy, pExpr->iTable, target);
3686 }else{
3687 inReg = sqlite3ExprCodeTarget(pParse, pExpr, target);
drh1c75c9d2015-12-21 15:22:13 +00003688 assert( pParse->pVdbe!=0 || pParse->db->mallocFailed );
drhebc16712010-09-28 00:25:58 +00003689 if( inReg!=target && pParse->pVdbe ){
3690 sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, inReg, target);
3691 }
drhcce7d172000-05-31 15:34:51 +00003692 }
drhcce7d172000-05-31 15:34:51 +00003693}
3694
3695/*
drh1c75c9d2015-12-21 15:22:13 +00003696** Make a transient copy of expression pExpr and then code it using
3697** sqlite3ExprCode(). This routine works just like sqlite3ExprCode()
3698** except that the input expression is guaranteed to be unchanged.
3699*/
3700void sqlite3ExprCodeCopy(Parse *pParse, Expr *pExpr, int target){
3701 sqlite3 *db = pParse->db;
3702 pExpr = sqlite3ExprDup(db, pExpr, 0);
3703 if( !db->mallocFailed ) sqlite3ExprCode(pParse, pExpr, target);
3704 sqlite3ExprDelete(db, pExpr);
3705}
3706
3707/*
drh05a86c52014-02-16 01:55:49 +00003708** Generate code that will evaluate expression pExpr and store the
3709** results in register target. The results are guaranteed to appear
3710** in register target. If the expression is constant, then this routine
3711** might choose to code the expression at initialization time.
3712*/
3713void sqlite3ExprCodeFactorable(Parse *pParse, Expr *pExpr, int target){
3714 if( pParse->okConstFactor && sqlite3ExprIsConstant(pExpr) ){
3715 sqlite3ExprCodeAtInit(pParse, pExpr, target, 0);
3716 }else{
3717 sqlite3ExprCode(pParse, pExpr, target);
3718 }
drhcce7d172000-05-31 15:34:51 +00003719}
3720
3721/*
peter.d.reid60ec9142014-09-06 16:39:46 +00003722** Generate code that evaluates the given expression and puts the result
drhde4fcfd2008-01-19 23:50:26 +00003723** in register target.
drh25303782004-12-07 15:41:48 +00003724**
drh2dcef112008-01-12 19:03:48 +00003725** Also make a copy of the expression results into another "cache" register
3726** and modify the expression so that the next time it is evaluated,
3727** the result is a copy of the cache register.
3728**
3729** This routine is used for expressions that are used multiple
3730** times. They are evaluated once and the results of the expression
3731** are reused.
drh25303782004-12-07 15:41:48 +00003732*/
drh05a86c52014-02-16 01:55:49 +00003733void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr, int target){
drh25303782004-12-07 15:41:48 +00003734 Vdbe *v = pParse->pVdbe;
drh05a86c52014-02-16 01:55:49 +00003735 int iMem;
3736
drhde4fcfd2008-01-19 23:50:26 +00003737 assert( target>0 );
drh05a86c52014-02-16 01:55:49 +00003738 assert( pExpr->op!=TK_REGISTER );
3739 sqlite3ExprCode(pParse, pExpr, target);
3740 iMem = ++pParse->nMem;
3741 sqlite3VdbeAddOp2(v, OP_Copy, target, iMem);
3742 exprToRegister(pExpr, iMem);
drh25303782004-12-07 15:41:48 +00003743}
drh2dcef112008-01-12 19:03:48 +00003744
drh678ccce2008-03-31 18:19:54 +00003745/*
drh268380c2004-02-25 13:47:31 +00003746** Generate code that pushes the value of every element of the given
drh9cbf3422008-01-17 16:22:13 +00003747** expression list into a sequence of registers beginning at target.
drh268380c2004-02-25 13:47:31 +00003748**
drh892d3172008-01-10 03:46:36 +00003749** Return the number of elements evaluated.
drhd1a01ed2013-11-21 16:08:52 +00003750**
3751** The SQLITE_ECEL_DUP flag prevents the arguments from being
3752** filled using OP_SCopy. OP_Copy must be used instead.
3753**
3754** The SQLITE_ECEL_FACTOR argument allows constant arguments to be
3755** factored out into initialization code.
drhb0df9632015-10-16 23:55:08 +00003756**
3757** The SQLITE_ECEL_REF flag means that expressions in the list with
3758** ExprList.a[].u.x.iOrderByCol>0 have already been evaluated and stored
3759** in registers at srcReg, and so the value can be copied from there.
drh268380c2004-02-25 13:47:31 +00003760*/
danielk19774adee202004-05-08 08:23:19 +00003761int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00003762 Parse *pParse, /* Parsing context */
drh389a1ad2008-01-03 23:44:53 +00003763 ExprList *pList, /* The expression list to be coded */
drh191b54c2008-04-15 12:14:21 +00003764 int target, /* Where to write results */
drh5579d592015-08-26 14:01:41 +00003765 int srcReg, /* Source registers if SQLITE_ECEL_REF */
drhd1a01ed2013-11-21 16:08:52 +00003766 u8 flags /* SQLITE_ECEL_* flags */
drh268380c2004-02-25 13:47:31 +00003767){
3768 struct ExprList_item *pItem;
drh5579d592015-08-26 14:01:41 +00003769 int i, j, n;
drhd1a01ed2013-11-21 16:08:52 +00003770 u8 copyOp = (flags & SQLITE_ECEL_DUP) ? OP_Copy : OP_SCopy;
drh5579d592015-08-26 14:01:41 +00003771 Vdbe *v = pParse->pVdbe;
drh9d8b3072008-08-22 16:29:51 +00003772 assert( pList!=0 );
drh9cbf3422008-01-17 16:22:13 +00003773 assert( target>0 );
drhd81a1422010-09-28 07:11:24 +00003774 assert( pParse->pVdbe!=0 ); /* Never gets this far otherwise */
drh268380c2004-02-25 13:47:31 +00003775 n = pList->nExpr;
drhd9f158e2013-11-21 20:48:42 +00003776 if( !ConstFactorOk(pParse) ) flags &= ~SQLITE_ECEL_FACTOR;
drh191b54c2008-04-15 12:14:21 +00003777 for(pItem=pList->a, i=0; i<n; i++, pItem++){
drh7445ffe2010-09-27 18:14:12 +00003778 Expr *pExpr = pItem->pExpr;
drh5579d592015-08-26 14:01:41 +00003779 if( (flags & SQLITE_ECEL_REF)!=0 && (j = pList->a[i].u.x.iOrderByCol)>0 ){
3780 sqlite3VdbeAddOp2(v, copyOp, j+srcReg-1, target+i);
3781 }else if( (flags & SQLITE_ECEL_FACTOR)!=0 && sqlite3ExprIsConstant(pExpr) ){
drhd673cdd2013-11-21 21:23:31 +00003782 sqlite3ExprCodeAtInit(pParse, pExpr, target+i, 0);
drhd1a01ed2013-11-21 16:08:52 +00003783 }else{
3784 int inReg = sqlite3ExprCodeTarget(pParse, pExpr, target+i);
3785 if( inReg!=target+i ){
drh4eded602013-12-20 15:59:20 +00003786 VdbeOp *pOp;
drh4eded602013-12-20 15:59:20 +00003787 if( copyOp==OP_Copy
3788 && (pOp=sqlite3VdbeGetOp(v, -1))->opcode==OP_Copy
3789 && pOp->p1+pOp->p3+1==inReg
3790 && pOp->p2+pOp->p3+1==target+i
3791 ){
3792 pOp->p3++;
3793 }else{
3794 sqlite3VdbeAddOp2(v, copyOp, inReg, target+i);
3795 }
drhd1a01ed2013-11-21 16:08:52 +00003796 }
drhd1766112008-09-17 00:13:12 +00003797 }
drh268380c2004-02-25 13:47:31 +00003798 }
drhf9b596e2004-05-26 16:54:42 +00003799 return n;
drh268380c2004-02-25 13:47:31 +00003800}
3801
3802/*
drh36c563a2009-11-12 13:32:22 +00003803** Generate code for a BETWEEN operator.
3804**
3805** x BETWEEN y AND z
3806**
3807** The above is equivalent to
3808**
3809** x>=y AND x<=z
3810**
3811** Code it as such, taking care to do the common subexpression
peter.d.reid60ec9142014-09-06 16:39:46 +00003812** elimination of x.
drh36c563a2009-11-12 13:32:22 +00003813*/
3814static void exprCodeBetween(
3815 Parse *pParse, /* Parsing and code generating context */
3816 Expr *pExpr, /* The BETWEEN expression */
3817 int dest, /* Jump here if the jump is taken */
dan71c57db2016-07-09 20:23:55 +00003818 void (*xJumpIf)(Parse*,Expr*,int,int),
drh36c563a2009-11-12 13:32:22 +00003819 int jumpIfNull /* Take the jump if the BETWEEN is NULL */
3820){
3821 Expr exprAnd; /* The AND operator in x>=y AND x<=z */
3822 Expr compLeft; /* The x>=y term */
3823 Expr compRight; /* The x<=z term */
3824 Expr exprX; /* The x subexpression */
3825 int regFree1 = 0; /* Temporary use register */
3826
dan71c57db2016-07-09 20:23:55 +00003827 memset(&compLeft, 0, sizeof(Expr));
3828 memset(&compRight, 0, sizeof(Expr));
3829 memset(&exprAnd, 0, sizeof(Expr));
3830
drh36c563a2009-11-12 13:32:22 +00003831 assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
3832 exprX = *pExpr->pLeft;
3833 exprAnd.op = TK_AND;
3834 exprAnd.pLeft = &compLeft;
3835 exprAnd.pRight = &compRight;
3836 compLeft.op = TK_GE;
3837 compLeft.pLeft = &exprX;
3838 compLeft.pRight = pExpr->x.pList->a[0].pExpr;
3839 compRight.op = TK_LE;
3840 compRight.pLeft = &exprX;
3841 compRight.pRight = pExpr->x.pList->a[1].pExpr;
dan71c57db2016-07-09 20:23:55 +00003842 if( (exprX.flags & EP_Vector)==0 ){
3843 exprToRegister(&exprX, sqlite3ExprCodeTemp(pParse, &exprX, &regFree1));
3844 }
3845 if( xJumpIf ){
3846 xJumpIf(pParse, &exprAnd, dest, jumpIfNull);
drh36c563a2009-11-12 13:32:22 +00003847 }else{
dan71c57db2016-07-09 20:23:55 +00003848 exprX.flags |= EP_FromJoin;
3849 sqlite3ExprCodeTarget(pParse, &exprAnd, dest);
drh36c563a2009-11-12 13:32:22 +00003850 }
3851 sqlite3ReleaseTempReg(pParse, regFree1);
3852
3853 /* Ensure adequate test coverage */
3854 testcase( jumpIfTrue==0 && jumpIfNull==0 && regFree1==0 );
3855 testcase( jumpIfTrue==0 && jumpIfNull==0 && regFree1!=0 );
3856 testcase( jumpIfTrue==0 && jumpIfNull!=0 && regFree1==0 );
3857 testcase( jumpIfTrue==0 && jumpIfNull!=0 && regFree1!=0 );
3858 testcase( jumpIfTrue!=0 && jumpIfNull==0 && regFree1==0 );
3859 testcase( jumpIfTrue!=0 && jumpIfNull==0 && regFree1!=0 );
3860 testcase( jumpIfTrue!=0 && jumpIfNull!=0 && regFree1==0 );
3861 testcase( jumpIfTrue!=0 && jumpIfNull!=0 && regFree1!=0 );
3862}
3863
3864/*
drhcce7d172000-05-31 15:34:51 +00003865** Generate code for a boolean expression such that a jump is made
3866** to the label "dest" if the expression is true but execution
3867** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00003868**
3869** If the expression evaluates to NULL (neither true nor false), then
drh35573352008-01-08 23:54:25 +00003870** take the jump if the jumpIfNull flag is SQLITE_JUMPIFNULL.
drhf2bc0132004-10-04 13:19:23 +00003871**
3872** This code depends on the fact that certain token values (ex: TK_EQ)
3873** are the same as opcode values (ex: OP_Eq) that implement the corresponding
3874** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
3875** the make process cause these values to align. Assert()s in the code
3876** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00003877*/
danielk19774adee202004-05-08 08:23:19 +00003878void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00003879 Vdbe *v = pParse->pVdbe;
3880 int op = 0;
drh2dcef112008-01-12 19:03:48 +00003881 int regFree1 = 0;
3882 int regFree2 = 0;
3883 int r1, r2;
3884
drh35573352008-01-08 23:54:25 +00003885 assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
mistachkin48864df2013-03-21 21:20:32 +00003886 if( NEVER(v==0) ) return; /* Existence of VDBE checked by caller */
drh33cd4902009-05-30 20:49:20 +00003887 if( NEVER(pExpr==0) ) return; /* No way this can happen */
drhf2bc0132004-10-04 13:19:23 +00003888 op = pExpr->op;
dan71c57db2016-07-09 20:23:55 +00003889 switch( op | (pExpr->pLeft ? (pExpr->pLeft->flags & EP_Vector) : 0)){
drhcce7d172000-05-31 15:34:51 +00003890 case TK_AND: {
danielk19774adee202004-05-08 08:23:19 +00003891 int d2 = sqlite3VdbeMakeLabel(v);
drhc5499be2008-04-01 15:06:33 +00003892 testcase( jumpIfNull==0 );
drh35573352008-01-08 23:54:25 +00003893 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,jumpIfNull^SQLITE_JUMPIFNULL);
drh54e2adb2014-01-04 15:17:04 +00003894 sqlite3ExprCachePush(pParse);
danielk19774adee202004-05-08 08:23:19 +00003895 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
3896 sqlite3VdbeResolveLabel(v, d2);
drhd2490902014-04-13 19:28:15 +00003897 sqlite3ExprCachePop(pParse);
drhcce7d172000-05-31 15:34:51 +00003898 break;
3899 }
3900 case TK_OR: {
drhc5499be2008-04-01 15:06:33 +00003901 testcase( jumpIfNull==0 );
danielk19774adee202004-05-08 08:23:19 +00003902 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drh54e2adb2014-01-04 15:17:04 +00003903 sqlite3ExprCachePush(pParse);
danielk19774adee202004-05-08 08:23:19 +00003904 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
drhd2490902014-04-13 19:28:15 +00003905 sqlite3ExprCachePop(pParse);
drhcce7d172000-05-31 15:34:51 +00003906 break;
3907 }
3908 case TK_NOT: {
drhc5499be2008-04-01 15:06:33 +00003909 testcase( jumpIfNull==0 );
danielk19774adee202004-05-08 08:23:19 +00003910 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00003911 break;
3912 }
drhde845c22016-03-17 19:07:52 +00003913 case TK_IS:
3914 case TK_ISNOT:
3915 testcase( op==TK_IS );
3916 testcase( op==TK_ISNOT );
3917 op = (op==TK_IS) ? TK_EQ : TK_NE;
3918 jumpIfNull = SQLITE_NULLEQ;
3919 /* Fall thru */
drhcce7d172000-05-31 15:34:51 +00003920 case TK_LT:
3921 case TK_LE:
3922 case TK_GT:
3923 case TK_GE:
3924 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00003925 case TK_EQ: {
drhc5499be2008-04-01 15:06:33 +00003926 testcase( jumpIfNull==0 );
drhb6da74e2009-12-24 16:00:28 +00003927 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
3928 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
drh35573352008-01-08 23:54:25 +00003929 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
drh2dcef112008-01-12 19:03:48 +00003930 r1, r2, dest, jumpIfNull);
drh7d176102014-02-18 03:07:12 +00003931 assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
3932 assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
3933 assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
3934 assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
drhde845c22016-03-17 19:07:52 +00003935 assert(TK_EQ==OP_Eq); testcase(op==OP_Eq);
3936 VdbeCoverageIf(v, op==OP_Eq && jumpIfNull==SQLITE_NULLEQ);
3937 VdbeCoverageIf(v, op==OP_Eq && jumpIfNull!=SQLITE_NULLEQ);
3938 assert(TK_NE==OP_Ne); testcase(op==OP_Ne);
3939 VdbeCoverageIf(v, op==OP_Ne && jumpIfNull==SQLITE_NULLEQ);
3940 VdbeCoverageIf(v, op==OP_Ne && jumpIfNull!=SQLITE_NULLEQ);
drh6a2fe092009-09-23 02:29:36 +00003941 testcase( regFree1==0 );
3942 testcase( regFree2==0 );
3943 break;
3944 }
drhcce7d172000-05-31 15:34:51 +00003945 case TK_ISNULL:
3946 case TK_NOTNULL: {
drh7d176102014-02-18 03:07:12 +00003947 assert( TK_ISNULL==OP_IsNull ); testcase( op==TK_ISNULL );
3948 assert( TK_NOTNULL==OP_NotNull ); testcase( op==TK_NOTNULL );
drh2dcef112008-01-12 19:03:48 +00003949 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
3950 sqlite3VdbeAddOp2(v, op, r1, dest);
drh7d176102014-02-18 03:07:12 +00003951 VdbeCoverageIf(v, op==TK_ISNULL);
3952 VdbeCoverageIf(v, op==TK_NOTNULL);
drhc5499be2008-04-01 15:06:33 +00003953 testcase( regFree1==0 );
drhcce7d172000-05-31 15:34:51 +00003954 break;
3955 }
drhfef52082000-06-06 01:50:43 +00003956 case TK_BETWEEN: {
drh5c03f302009-11-13 15:03:59 +00003957 testcase( jumpIfNull==0 );
dan71c57db2016-07-09 20:23:55 +00003958 exprCodeBetween(pParse, pExpr, dest, sqlite3ExprIfTrue, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00003959 break;
3960 }
drh84e30ca2011-02-10 17:46:14 +00003961#ifndef SQLITE_OMIT_SUBQUERY
drhe3365e62009-11-12 17:52:24 +00003962 case TK_IN: {
3963 int destIfFalse = sqlite3VdbeMakeLabel(v);
3964 int destIfNull = jumpIfNull ? dest : destIfFalse;
3965 sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull);
drh076e85f2015-09-03 13:46:12 +00003966 sqlite3VdbeGoto(v, dest);
drhe3365e62009-11-12 17:52:24 +00003967 sqlite3VdbeResolveLabel(v, destIfFalse);
3968 break;
3969 }
shanehbb201342011-02-09 19:55:20 +00003970#endif
drhcce7d172000-05-31 15:34:51 +00003971 default: {
drh991a1982014-01-02 17:57:16 +00003972 if( exprAlwaysTrue(pExpr) ){
drh076e85f2015-09-03 13:46:12 +00003973 sqlite3VdbeGoto(v, dest);
drh991a1982014-01-02 17:57:16 +00003974 }else if( exprAlwaysFalse(pExpr) ){
3975 /* No-op */
3976 }else{
3977 r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
3978 sqlite3VdbeAddOp3(v, OP_If, r1, dest, jumpIfNull!=0);
drh688852a2014-02-17 22:40:43 +00003979 VdbeCoverage(v);
drh991a1982014-01-02 17:57:16 +00003980 testcase( regFree1==0 );
3981 testcase( jumpIfNull==0 );
3982 }
drhcce7d172000-05-31 15:34:51 +00003983 break;
3984 }
3985 }
drh2dcef112008-01-12 19:03:48 +00003986 sqlite3ReleaseTempReg(pParse, regFree1);
3987 sqlite3ReleaseTempReg(pParse, regFree2);
drhcce7d172000-05-31 15:34:51 +00003988}
3989
3990/*
drh66b89c82000-11-28 20:47:17 +00003991** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00003992** to the label "dest" if the expression is false but execution
3993** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00003994**
3995** If the expression evaluates to NULL (neither true nor false) then
drh35573352008-01-08 23:54:25 +00003996** jump if jumpIfNull is SQLITE_JUMPIFNULL or fall through if jumpIfNull
3997** is 0.
drhcce7d172000-05-31 15:34:51 +00003998*/
danielk19774adee202004-05-08 08:23:19 +00003999void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00004000 Vdbe *v = pParse->pVdbe;
4001 int op = 0;
drh2dcef112008-01-12 19:03:48 +00004002 int regFree1 = 0;
4003 int regFree2 = 0;
4004 int r1, r2;
4005
drh35573352008-01-08 23:54:25 +00004006 assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
mistachkin48864df2013-03-21 21:20:32 +00004007 if( NEVER(v==0) ) return; /* Existence of VDBE checked by caller */
drh33cd4902009-05-30 20:49:20 +00004008 if( pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00004009
4010 /* The value of pExpr->op and op are related as follows:
4011 **
4012 ** pExpr->op op
4013 ** --------- ----------
4014 ** TK_ISNULL OP_NotNull
4015 ** TK_NOTNULL OP_IsNull
4016 ** TK_NE OP_Eq
4017 ** TK_EQ OP_Ne
4018 ** TK_GT OP_Le
4019 ** TK_LE OP_Gt
4020 ** TK_GE OP_Lt
4021 ** TK_LT OP_Ge
4022 **
4023 ** For other values of pExpr->op, op is undefined and unused.
4024 ** The value of TK_ and OP_ constants are arranged such that we
4025 ** can compute the mapping above using the following expression.
4026 ** Assert()s verify that the computation is correct.
4027 */
4028 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
4029
4030 /* Verify correct alignment of TK_ and OP_ constants
4031 */
4032 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
4033 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
4034 assert( pExpr->op!=TK_NE || op==OP_Eq );
4035 assert( pExpr->op!=TK_EQ || op==OP_Ne );
4036 assert( pExpr->op!=TK_LT || op==OP_Ge );
4037 assert( pExpr->op!=TK_LE || op==OP_Gt );
4038 assert( pExpr->op!=TK_GT || op==OP_Le );
4039 assert( pExpr->op!=TK_GE || op==OP_Lt );
4040
danba00e302016-07-23 20:24:06 +00004041 switch( pExpr->op ){
drhcce7d172000-05-31 15:34:51 +00004042 case TK_AND: {
drhc5499be2008-04-01 15:06:33 +00004043 testcase( jumpIfNull==0 );
danielk19774adee202004-05-08 08:23:19 +00004044 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drh54e2adb2014-01-04 15:17:04 +00004045 sqlite3ExprCachePush(pParse);
danielk19774adee202004-05-08 08:23:19 +00004046 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
drhd2490902014-04-13 19:28:15 +00004047 sqlite3ExprCachePop(pParse);
drhcce7d172000-05-31 15:34:51 +00004048 break;
4049 }
4050 case TK_OR: {
danielk19774adee202004-05-08 08:23:19 +00004051 int d2 = sqlite3VdbeMakeLabel(v);
drhc5499be2008-04-01 15:06:33 +00004052 testcase( jumpIfNull==0 );
drh35573352008-01-08 23:54:25 +00004053 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, jumpIfNull^SQLITE_JUMPIFNULL);
drh54e2adb2014-01-04 15:17:04 +00004054 sqlite3ExprCachePush(pParse);
danielk19774adee202004-05-08 08:23:19 +00004055 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
4056 sqlite3VdbeResolveLabel(v, d2);
drhd2490902014-04-13 19:28:15 +00004057 sqlite3ExprCachePop(pParse);
drhcce7d172000-05-31 15:34:51 +00004058 break;
4059 }
4060 case TK_NOT: {
drh5c03f302009-11-13 15:03:59 +00004061 testcase( jumpIfNull==0 );
danielk19774adee202004-05-08 08:23:19 +00004062 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00004063 break;
4064 }
drhde845c22016-03-17 19:07:52 +00004065 case TK_IS:
4066 case TK_ISNOT:
4067 testcase( pExpr->op==TK_IS );
4068 testcase( pExpr->op==TK_ISNOT );
4069 op = (pExpr->op==TK_IS) ? TK_NE : TK_EQ;
4070 jumpIfNull = SQLITE_NULLEQ;
4071 /* Fall thru */
drhcce7d172000-05-31 15:34:51 +00004072 case TK_LT:
4073 case TK_LE:
4074 case TK_GT:
4075 case TK_GE:
4076 case TK_NE:
4077 case TK_EQ: {
danba00e302016-07-23 20:24:06 +00004078 if( pExpr->pLeft->flags & EP_Vector ) goto default_expr;
4079
drhc5499be2008-04-01 15:06:33 +00004080 testcase( jumpIfNull==0 );
drhb6da74e2009-12-24 16:00:28 +00004081 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
4082 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
drh35573352008-01-08 23:54:25 +00004083 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
drh2dcef112008-01-12 19:03:48 +00004084 r1, r2, dest, jumpIfNull);
drh7d176102014-02-18 03:07:12 +00004085 assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
4086 assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
4087 assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
4088 assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
drhde845c22016-03-17 19:07:52 +00004089 assert(TK_EQ==OP_Eq); testcase(op==OP_Eq);
4090 VdbeCoverageIf(v, op==OP_Eq && jumpIfNull!=SQLITE_NULLEQ);
4091 VdbeCoverageIf(v, op==OP_Eq && jumpIfNull==SQLITE_NULLEQ);
4092 assert(TK_NE==OP_Ne); testcase(op==OP_Ne);
4093 VdbeCoverageIf(v, op==OP_Ne && jumpIfNull!=SQLITE_NULLEQ);
4094 VdbeCoverageIf(v, op==OP_Ne && jumpIfNull==SQLITE_NULLEQ);
drh6a2fe092009-09-23 02:29:36 +00004095 testcase( regFree1==0 );
4096 testcase( regFree2==0 );
4097 break;
4098 }
drhcce7d172000-05-31 15:34:51 +00004099 case TK_ISNULL:
4100 case TK_NOTNULL: {
drh2dcef112008-01-12 19:03:48 +00004101 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
4102 sqlite3VdbeAddOp2(v, op, r1, dest);
drh7d176102014-02-18 03:07:12 +00004103 testcase( op==TK_ISNULL ); VdbeCoverageIf(v, op==TK_ISNULL);
4104 testcase( op==TK_NOTNULL ); VdbeCoverageIf(v, op==TK_NOTNULL);
drhc5499be2008-04-01 15:06:33 +00004105 testcase( regFree1==0 );
drhcce7d172000-05-31 15:34:51 +00004106 break;
4107 }
drhfef52082000-06-06 01:50:43 +00004108 case TK_BETWEEN: {
drh5c03f302009-11-13 15:03:59 +00004109 testcase( jumpIfNull==0 );
dan71c57db2016-07-09 20:23:55 +00004110 exprCodeBetween(pParse, pExpr, dest, sqlite3ExprIfFalse, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00004111 break;
4112 }
drh84e30ca2011-02-10 17:46:14 +00004113#ifndef SQLITE_OMIT_SUBQUERY
drhe3365e62009-11-12 17:52:24 +00004114 case TK_IN: {
4115 if( jumpIfNull ){
4116 sqlite3ExprCodeIN(pParse, pExpr, dest, dest);
4117 }else{
4118 int destIfNull = sqlite3VdbeMakeLabel(v);
4119 sqlite3ExprCodeIN(pParse, pExpr, dest, destIfNull);
4120 sqlite3VdbeResolveLabel(v, destIfNull);
4121 }
4122 break;
4123 }
shanehbb201342011-02-09 19:55:20 +00004124#endif
drhcce7d172000-05-31 15:34:51 +00004125 default: {
danba00e302016-07-23 20:24:06 +00004126 default_expr:
drh991a1982014-01-02 17:57:16 +00004127 if( exprAlwaysFalse(pExpr) ){
drh076e85f2015-09-03 13:46:12 +00004128 sqlite3VdbeGoto(v, dest);
drh991a1982014-01-02 17:57:16 +00004129 }else if( exprAlwaysTrue(pExpr) ){
4130 /* no-op */
4131 }else{
4132 r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
4133 sqlite3VdbeAddOp3(v, OP_IfNot, r1, dest, jumpIfNull!=0);
drh688852a2014-02-17 22:40:43 +00004134 VdbeCoverage(v);
drh991a1982014-01-02 17:57:16 +00004135 testcase( regFree1==0 );
4136 testcase( jumpIfNull==0 );
4137 }
drhcce7d172000-05-31 15:34:51 +00004138 break;
4139 }
4140 }
drh2dcef112008-01-12 19:03:48 +00004141 sqlite3ReleaseTempReg(pParse, regFree1);
4142 sqlite3ReleaseTempReg(pParse, regFree2);
drhcce7d172000-05-31 15:34:51 +00004143}
drh22827922000-06-06 17:27:05 +00004144
4145/*
drh72bc8202015-06-11 13:58:35 +00004146** Like sqlite3ExprIfFalse() except that a copy is made of pExpr before
4147** code generation, and that copy is deleted after code generation. This
4148** ensures that the original pExpr is unchanged.
4149*/
4150void sqlite3ExprIfFalseDup(Parse *pParse, Expr *pExpr, int dest,int jumpIfNull){
4151 sqlite3 *db = pParse->db;
4152 Expr *pCopy = sqlite3ExprDup(db, pExpr, 0);
4153 if( db->mallocFailed==0 ){
4154 sqlite3ExprIfFalse(pParse, pCopy, dest, jumpIfNull);
4155 }
4156 sqlite3ExprDelete(db, pCopy);
4157}
4158
4159
4160/*
drh1d9da702010-01-07 15:17:02 +00004161** Do a deep comparison of two expression trees. Return 0 if the two
4162** expressions are completely identical. Return 1 if they differ only
4163** by a COLLATE operator at the top level. Return 2 if there are differences
4164** other than the top-level COLLATE operator.
drhd40aab02007-02-24 15:29:03 +00004165**
drh619a1302013-08-01 13:04:46 +00004166** If any subelement of pB has Expr.iTable==(-1) then it is allowed
4167** to compare equal to an equivalent element in pA with Expr.iTable==iTab.
4168**
drh66518ca2013-08-01 15:09:57 +00004169** The pA side might be using TK_REGISTER. If that is the case and pB is
4170** not using TK_REGISTER but is otherwise equivalent, then still return 0.
4171**
drh1d9da702010-01-07 15:17:02 +00004172** Sometimes this routine will return 2 even if the two expressions
drhd40aab02007-02-24 15:29:03 +00004173** really are equivalent. If we cannot prove that the expressions are
drh1d9da702010-01-07 15:17:02 +00004174** identical, we return 2 just to be safe. So if this routine
4175** returns 2, then you do not really know for certain if the two
4176** expressions are the same. But if you get a 0 or 1 return, then you
drhd40aab02007-02-24 15:29:03 +00004177** can be sure the expressions are the same. In the places where
drh1d9da702010-01-07 15:17:02 +00004178** this routine is used, it does not hurt to get an extra 2 - that
drhd40aab02007-02-24 15:29:03 +00004179** just might result in some slightly slower code. But returning
drh1d9da702010-01-07 15:17:02 +00004180** an incorrect 0 or 1 could lead to a malfunction.
drh22827922000-06-06 17:27:05 +00004181*/
drh619a1302013-08-01 13:04:46 +00004182int sqlite3ExprCompare(Expr *pA, Expr *pB, int iTab){
drh10d1edf2013-11-15 15:52:39 +00004183 u32 combinedFlags;
4184 if( pA==0 || pB==0 ){
drh1d9da702010-01-07 15:17:02 +00004185 return pB==pA ? 0 : 2;
drh22827922000-06-06 17:27:05 +00004186 }
drh10d1edf2013-11-15 15:52:39 +00004187 combinedFlags = pA->flags | pB->flags;
4188 if( combinedFlags & EP_IntValue ){
4189 if( (pA->flags&pB->flags&EP_IntValue)!=0 && pA->u.iValue==pB->u.iValue ){
4190 return 0;
4191 }
drh1d9da702010-01-07 15:17:02 +00004192 return 2;
drh22827922000-06-06 17:27:05 +00004193 }
drhc2acc4e2013-11-15 18:15:19 +00004194 if( pA->op!=pB->op ){
drh619a1302013-08-01 13:04:46 +00004195 if( pA->op==TK_COLLATE && sqlite3ExprCompare(pA->pLeft, pB, iTab)<2 ){
drhae80dde2012-12-06 21:16:43 +00004196 return 1;
4197 }
drh619a1302013-08-01 13:04:46 +00004198 if( pB->op==TK_COLLATE && sqlite3ExprCompare(pA, pB->pLeft, iTab)<2 ){
drhae80dde2012-12-06 21:16:43 +00004199 return 1;
4200 }
4201 return 2;
4202 }
drh2edc5fd2015-11-24 02:10:52 +00004203 if( pA->op!=TK_COLUMN && pA->op!=TK_AGG_COLUMN && pA->u.zToken ){
drh390b88a2015-08-31 18:13:01 +00004204 if( pA->op==TK_FUNCTION ){
4205 if( sqlite3StrICmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2;
4206 }else if( strcmp(pA->u.zToken,pB->u.zToken)!=0 ){
drhae80dde2012-12-06 21:16:43 +00004207 return pA->op==TK_COLLATE ? 1 : 2;
drh2646da72005-12-09 20:02:05 +00004208 }
drh22827922000-06-06 17:27:05 +00004209 }
drh10d1edf2013-11-15 15:52:39 +00004210 if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 2;
drh85f8aa72013-11-15 20:06:26 +00004211 if( ALWAYS((combinedFlags & EP_TokenOnly)==0) ){
drh10d1edf2013-11-15 15:52:39 +00004212 if( combinedFlags & EP_xIsSelect ) return 2;
4213 if( sqlite3ExprCompare(pA->pLeft, pB->pLeft, iTab) ) return 2;
4214 if( sqlite3ExprCompare(pA->pRight, pB->pRight, iTab) ) return 2;
4215 if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList, iTab) ) return 2;
drh7693c422015-04-17 19:41:37 +00004216 if( ALWAYS((combinedFlags & EP_Reduced)==0) && pA->op!=TK_STRING ){
drh10d1edf2013-11-15 15:52:39 +00004217 if( pA->iColumn!=pB->iColumn ) return 2;
4218 if( pA->iTable!=pB->iTable
drh85f8aa72013-11-15 20:06:26 +00004219 && (pA->iTable!=iTab || NEVER(pB->iTable>=0)) ) return 2;
drh10d1edf2013-11-15 15:52:39 +00004220 }
4221 }
drh1d9da702010-01-07 15:17:02 +00004222 return 0;
drh22827922000-06-06 17:27:05 +00004223}
4224
drh8c6f6662010-04-26 19:17:26 +00004225/*
4226** Compare two ExprList objects. Return 0 if they are identical and
4227** non-zero if they differ in any way.
4228**
drh619a1302013-08-01 13:04:46 +00004229** If any subelement of pB has Expr.iTable==(-1) then it is allowed
4230** to compare equal to an equivalent element in pA with Expr.iTable==iTab.
4231**
drh8c6f6662010-04-26 19:17:26 +00004232** This routine might return non-zero for equivalent ExprLists. The
4233** only consequence will be disabled optimizations. But this routine
4234** must never return 0 if the two ExprList objects are different, or
4235** a malfunction will result.
4236**
4237** Two NULL pointers are considered to be the same. But a NULL pointer
4238** always differs from a non-NULL pointer.
4239*/
drh619a1302013-08-01 13:04:46 +00004240int sqlite3ExprListCompare(ExprList *pA, ExprList *pB, int iTab){
drh8c6f6662010-04-26 19:17:26 +00004241 int i;
4242 if( pA==0 && pB==0 ) return 0;
4243 if( pA==0 || pB==0 ) return 1;
4244 if( pA->nExpr!=pB->nExpr ) return 1;
4245 for(i=0; i<pA->nExpr; i++){
4246 Expr *pExprA = pA->a[i].pExpr;
4247 Expr *pExprB = pB->a[i].pExpr;
4248 if( pA->a[i].sortOrder!=pB->a[i].sortOrder ) return 1;
drh619a1302013-08-01 13:04:46 +00004249 if( sqlite3ExprCompare(pExprA, pExprB, iTab) ) return 1;
drh8c6f6662010-04-26 19:17:26 +00004250 }
4251 return 0;
4252}
drh13449892005-09-07 21:22:45 +00004253
drh22827922000-06-06 17:27:05 +00004254/*
drh4bd5f732013-07-31 23:22:39 +00004255** Return true if we can prove the pE2 will always be true if pE1 is
4256** true. Return false if we cannot complete the proof or if pE2 might
4257** be false. Examples:
4258**
drh619a1302013-08-01 13:04:46 +00004259** pE1: x==5 pE2: x==5 Result: true
4260** pE1: x>0 pE2: x==5 Result: false
4261** pE1: x=21 pE2: x=21 OR y=43 Result: true
4262** pE1: x!=123 pE2: x IS NOT NULL Result: true
4263** pE1: x!=?1 pE2: x IS NOT NULL Result: true
4264** pE1: x IS NULL pE2: x IS NOT NULL Result: false
4265** pE1: x IS ?2 pE2: x IS NOT NULL Reuslt: false
drh4bd5f732013-07-31 23:22:39 +00004266**
4267** When comparing TK_COLUMN nodes between pE1 and pE2, if pE2 has
4268** Expr.iTable<0 then assume a table number given by iTab.
4269**
4270** When in doubt, return false. Returning true might give a performance
4271** improvement. Returning false might cause a performance reduction, but
4272** it will always give the correct answer and is hence always safe.
4273*/
4274int sqlite3ExprImpliesExpr(Expr *pE1, Expr *pE2, int iTab){
drh619a1302013-08-01 13:04:46 +00004275 if( sqlite3ExprCompare(pE1, pE2, iTab)==0 ){
4276 return 1;
4277 }
4278 if( pE2->op==TK_OR
4279 && (sqlite3ExprImpliesExpr(pE1, pE2->pLeft, iTab)
4280 || sqlite3ExprImpliesExpr(pE1, pE2->pRight, iTab) )
4281 ){
4282 return 1;
4283 }
4284 if( pE2->op==TK_NOTNULL
4285 && sqlite3ExprCompare(pE1->pLeft, pE2->pLeft, iTab)==0
4286 && (pE1->op!=TK_ISNULL && pE1->op!=TK_IS)
4287 ){
4288 return 1;
4289 }
4290 return 0;
drh4bd5f732013-07-31 23:22:39 +00004291}
4292
4293/*
drh030796d2012-08-23 16:18:10 +00004294** An instance of the following structure is used by the tree walker
4295** to count references to table columns in the arguments of an
drhed551b92012-08-23 19:46:11 +00004296** aggregate function, in order to implement the
4297** sqlite3FunctionThisSrc() routine.
drh374fdce2012-04-17 16:38:53 +00004298*/
drh030796d2012-08-23 16:18:10 +00004299struct SrcCount {
4300 SrcList *pSrc; /* One particular FROM clause in a nested query */
4301 int nThis; /* Number of references to columns in pSrcList */
4302 int nOther; /* Number of references to columns in other FROM clauses */
4303};
4304
4305/*
4306** Count the number of references to columns.
4307*/
4308static int exprSrcCount(Walker *pWalker, Expr *pExpr){
drhfb0a6082012-08-24 01:07:52 +00004309 /* The NEVER() on the second term is because sqlite3FunctionUsesThisSrc()
4310 ** is always called before sqlite3ExprAnalyzeAggregates() and so the
4311 ** TK_COLUMNs have not yet been converted into TK_AGG_COLUMN. If
4312 ** sqlite3FunctionUsesThisSrc() is used differently in the future, the
4313 ** NEVER() will need to be removed. */
4314 if( pExpr->op==TK_COLUMN || NEVER(pExpr->op==TK_AGG_COLUMN) ){
drh374fdce2012-04-17 16:38:53 +00004315 int i;
drh030796d2012-08-23 16:18:10 +00004316 struct SrcCount *p = pWalker->u.pSrcCount;
4317 SrcList *pSrc = p->pSrc;
drh655814d2015-01-09 01:27:29 +00004318 int nSrc = pSrc ? pSrc->nSrc : 0;
4319 for(i=0; i<nSrc; i++){
drh030796d2012-08-23 16:18:10 +00004320 if( pExpr->iTable==pSrc->a[i].iCursor ) break;
drh374fdce2012-04-17 16:38:53 +00004321 }
drh655814d2015-01-09 01:27:29 +00004322 if( i<nSrc ){
drh030796d2012-08-23 16:18:10 +00004323 p->nThis++;
4324 }else{
4325 p->nOther++;
4326 }
drh374fdce2012-04-17 16:38:53 +00004327 }
drh030796d2012-08-23 16:18:10 +00004328 return WRC_Continue;
drh374fdce2012-04-17 16:38:53 +00004329}
4330
4331/*
drh030796d2012-08-23 16:18:10 +00004332** Determine if any of the arguments to the pExpr Function reference
4333** pSrcList. Return true if they do. Also return true if the function
4334** has no arguments or has only constant arguments. Return false if pExpr
4335** references columns but not columns of tables found in pSrcList.
drh374fdce2012-04-17 16:38:53 +00004336*/
drh030796d2012-08-23 16:18:10 +00004337int sqlite3FunctionUsesThisSrc(Expr *pExpr, SrcList *pSrcList){
drh374fdce2012-04-17 16:38:53 +00004338 Walker w;
drh030796d2012-08-23 16:18:10 +00004339 struct SrcCount cnt;
drh374fdce2012-04-17 16:38:53 +00004340 assert( pExpr->op==TK_AGG_FUNCTION );
4341 memset(&w, 0, sizeof(w));
drh030796d2012-08-23 16:18:10 +00004342 w.xExprCallback = exprSrcCount;
4343 w.u.pSrcCount = &cnt;
4344 cnt.pSrc = pSrcList;
4345 cnt.nThis = 0;
4346 cnt.nOther = 0;
4347 sqlite3WalkExprList(&w, pExpr->x.pList);
4348 return cnt.nThis>0 || cnt.nOther==0;
drh374fdce2012-04-17 16:38:53 +00004349}
4350
4351/*
drh13449892005-09-07 21:22:45 +00004352** Add a new element to the pAggInfo->aCol[] array. Return the index of
4353** the new element. Return a negative number if malloc fails.
drh22827922000-06-06 17:27:05 +00004354*/
drh17435752007-08-16 04:30:38 +00004355static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){
drh13449892005-09-07 21:22:45 +00004356 int i;
drhcf643722007-03-27 13:36:37 +00004357 pInfo->aCol = sqlite3ArrayAllocate(
drh17435752007-08-16 04:30:38 +00004358 db,
drhcf643722007-03-27 13:36:37 +00004359 pInfo->aCol,
4360 sizeof(pInfo->aCol[0]),
drhcf643722007-03-27 13:36:37 +00004361 &pInfo->nColumn,
drhcf643722007-03-27 13:36:37 +00004362 &i
4363 );
drh13449892005-09-07 21:22:45 +00004364 return i;
4365}
4366
4367/*
4368** Add a new element to the pAggInfo->aFunc[] array. Return the index of
4369** the new element. Return a negative number if malloc fails.
4370*/
drh17435752007-08-16 04:30:38 +00004371static int addAggInfoFunc(sqlite3 *db, AggInfo *pInfo){
drh13449892005-09-07 21:22:45 +00004372 int i;
drhcf643722007-03-27 13:36:37 +00004373 pInfo->aFunc = sqlite3ArrayAllocate(
drh17435752007-08-16 04:30:38 +00004374 db,
drhcf643722007-03-27 13:36:37 +00004375 pInfo->aFunc,
4376 sizeof(pInfo->aFunc[0]),
drhcf643722007-03-27 13:36:37 +00004377 &pInfo->nFunc,
drhcf643722007-03-27 13:36:37 +00004378 &i
4379 );
drh13449892005-09-07 21:22:45 +00004380 return i;
4381}
drh22827922000-06-06 17:27:05 +00004382
4383/*
drh7d10d5a2008-08-20 16:35:10 +00004384** This is the xExprCallback for a tree walker. It is used to
4385** implement sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates
drh626a8792005-01-17 22:08:19 +00004386** for additional information.
drh22827922000-06-06 17:27:05 +00004387*/
drh7d10d5a2008-08-20 16:35:10 +00004388static int analyzeAggregate(Walker *pWalker, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00004389 int i;
drh7d10d5a2008-08-20 16:35:10 +00004390 NameContext *pNC = pWalker->u.pNC;
danielk1977a58fdfb2005-02-08 07:50:40 +00004391 Parse *pParse = pNC->pParse;
4392 SrcList *pSrcList = pNC->pSrcList;
drh13449892005-09-07 21:22:45 +00004393 AggInfo *pAggInfo = pNC->pAggInfo;
drh22827922000-06-06 17:27:05 +00004394
drh22827922000-06-06 17:27:05 +00004395 switch( pExpr->op ){
drh89c69d02007-01-04 01:20:28 +00004396 case TK_AGG_COLUMN:
drh967e8b72000-06-21 13:59:10 +00004397 case TK_COLUMN: {
drh8b213892008-08-29 02:14:02 +00004398 testcase( pExpr->op==TK_AGG_COLUMN );
4399 testcase( pExpr->op==TK_COLUMN );
drh13449892005-09-07 21:22:45 +00004400 /* Check to see if the column is in one of the tables in the FROM
4401 ** clause of the aggregate query */
drh20bc3932009-05-30 23:35:43 +00004402 if( ALWAYS(pSrcList!=0) ){
drh13449892005-09-07 21:22:45 +00004403 struct SrcList_item *pItem = pSrcList->a;
4404 for(i=0; i<pSrcList->nSrc; i++, pItem++){
4405 struct AggInfo_col *pCol;
drhc5cd1242013-09-12 16:50:49 +00004406 assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) );
drh13449892005-09-07 21:22:45 +00004407 if( pExpr->iTable==pItem->iCursor ){
4408 /* If we reach this point, it means that pExpr refers to a table
4409 ** that is in the FROM clause of the aggregate query.
4410 **
4411 ** Make an entry for the column in pAggInfo->aCol[] if there
4412 ** is not an entry there already.
4413 */
drh7f906d62007-03-12 23:48:52 +00004414 int k;
drh13449892005-09-07 21:22:45 +00004415 pCol = pAggInfo->aCol;
drh7f906d62007-03-12 23:48:52 +00004416 for(k=0; k<pAggInfo->nColumn; k++, pCol++){
drh13449892005-09-07 21:22:45 +00004417 if( pCol->iTable==pExpr->iTable &&
4418 pCol->iColumn==pExpr->iColumn ){
4419 break;
4420 }
danielk1977a58fdfb2005-02-08 07:50:40 +00004421 }
danielk19771e536952007-08-16 10:09:01 +00004422 if( (k>=pAggInfo->nColumn)
4423 && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0
4424 ){
drh7f906d62007-03-12 23:48:52 +00004425 pCol = &pAggInfo->aCol[k];
danielk19770817d0d2007-02-14 09:19:36 +00004426 pCol->pTab = pExpr->pTab;
drh13449892005-09-07 21:22:45 +00004427 pCol->iTable = pExpr->iTable;
4428 pCol->iColumn = pExpr->iColumn;
drh0a07c102008-01-03 18:03:08 +00004429 pCol->iMem = ++pParse->nMem;
drh13449892005-09-07 21:22:45 +00004430 pCol->iSorterColumn = -1;
drh5774b802005-09-07 22:48:16 +00004431 pCol->pExpr = pExpr;
drh13449892005-09-07 21:22:45 +00004432 if( pAggInfo->pGroupBy ){
4433 int j, n;
4434 ExprList *pGB = pAggInfo->pGroupBy;
4435 struct ExprList_item *pTerm = pGB->a;
4436 n = pGB->nExpr;
4437 for(j=0; j<n; j++, pTerm++){
4438 Expr *pE = pTerm->pExpr;
4439 if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable &&
4440 pE->iColumn==pExpr->iColumn ){
4441 pCol->iSorterColumn = j;
4442 break;
4443 }
4444 }
4445 }
4446 if( pCol->iSorterColumn<0 ){
4447 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
4448 }
4449 }
4450 /* There is now an entry for pExpr in pAggInfo->aCol[] (either
4451 ** because it was there before or because we just created it).
4452 ** Convert the pExpr to be a TK_AGG_COLUMN referring to that
4453 ** pAggInfo->aCol[] entry.
4454 */
drhebb6a652013-09-12 23:42:22 +00004455 ExprSetVVAProperty(pExpr, EP_NoReduce);
drh13449892005-09-07 21:22:45 +00004456 pExpr->pAggInfo = pAggInfo;
4457 pExpr->op = TK_AGG_COLUMN;
shanecf697392009-06-01 16:53:09 +00004458 pExpr->iAgg = (i16)k;
drh13449892005-09-07 21:22:45 +00004459 break;
4460 } /* endif pExpr->iTable==pItem->iCursor */
4461 } /* end loop over pSrcList */
drh22827922000-06-06 17:27:05 +00004462 }
drh7d10d5a2008-08-20 16:35:10 +00004463 return WRC_Prune;
drh22827922000-06-06 17:27:05 +00004464 }
4465 case TK_AGG_FUNCTION: {
drh3a8c4be2012-05-21 20:13:39 +00004466 if( (pNC->ncFlags & NC_InAggFunc)==0
drhed551b92012-08-23 19:46:11 +00004467 && pWalker->walkerDepth==pExpr->op2
drh3a8c4be2012-05-21 20:13:39 +00004468 ){
drh13449892005-09-07 21:22:45 +00004469 /* Check to see if pExpr is a duplicate of another aggregate
4470 ** function that is already in the pAggInfo structure
4471 */
4472 struct AggInfo_func *pItem = pAggInfo->aFunc;
4473 for(i=0; i<pAggInfo->nFunc; i++, pItem++){
drh619a1302013-08-01 13:04:46 +00004474 if( sqlite3ExprCompare(pItem->pExpr, pExpr, -1)==0 ){
danielk1977a58fdfb2005-02-08 07:50:40 +00004475 break;
4476 }
drh22827922000-06-06 17:27:05 +00004477 }
drh13449892005-09-07 21:22:45 +00004478 if( i>=pAggInfo->nFunc ){
4479 /* pExpr is original. Make a new entry in pAggInfo->aFunc[]
4480 */
danielk197714db2662006-01-09 16:12:04 +00004481 u8 enc = ENC(pParse->db);
danielk19771e536952007-08-16 10:09:01 +00004482 i = addAggInfoFunc(pParse->db, pAggInfo);
drh13449892005-09-07 21:22:45 +00004483 if( i>=0 ){
danielk19776ab3a2e2009-02-19 14:39:25 +00004484 assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
drh13449892005-09-07 21:22:45 +00004485 pItem = &pAggInfo->aFunc[i];
4486 pItem->pExpr = pExpr;
drh0a07c102008-01-03 18:03:08 +00004487 pItem->iMem = ++pParse->nMem;
drh33e619f2009-05-28 01:00:55 +00004488 assert( !ExprHasProperty(pExpr, EP_IntValue) );
drh13449892005-09-07 21:22:45 +00004489 pItem->pFunc = sqlite3FindFunction(pParse->db,
drh80738d92016-02-15 00:34:16 +00004490 pExpr->u.zToken,
danielk19776ab3a2e2009-02-19 14:39:25 +00004491 pExpr->x.pList ? pExpr->x.pList->nExpr : 0, enc, 0);
drhfd357972005-09-09 01:33:19 +00004492 if( pExpr->flags & EP_Distinct ){
4493 pItem->iDistinct = pParse->nTab++;
4494 }else{
4495 pItem->iDistinct = -1;
4496 }
drh13449892005-09-07 21:22:45 +00004497 }
danielk1977a58fdfb2005-02-08 07:50:40 +00004498 }
drh13449892005-09-07 21:22:45 +00004499 /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
4500 */
drhc5cd1242013-09-12 16:50:49 +00004501 assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) );
drhebb6a652013-09-12 23:42:22 +00004502 ExprSetVVAProperty(pExpr, EP_NoReduce);
shanecf697392009-06-01 16:53:09 +00004503 pExpr->iAgg = (i16)i;
drh13449892005-09-07 21:22:45 +00004504 pExpr->pAggInfo = pAggInfo;
drh6e83a572012-11-02 18:48:49 +00004505 return WRC_Prune;
4506 }else{
4507 return WRC_Continue;
drh22827922000-06-06 17:27:05 +00004508 }
drh22827922000-06-06 17:27:05 +00004509 }
4510 }
drh7d10d5a2008-08-20 16:35:10 +00004511 return WRC_Continue;
4512}
4513static int analyzeAggregatesInSelect(Walker *pWalker, Select *pSelect){
drhd5a336e2012-04-19 15:49:19 +00004514 UNUSED_PARAMETER(pWalker);
4515 UNUSED_PARAMETER(pSelect);
drh374fdce2012-04-17 16:38:53 +00004516 return WRC_Continue;
drh626a8792005-01-17 22:08:19 +00004517}
4518
4519/*
drhe8abb4c2012-11-02 18:24:57 +00004520** Analyze the pExpr expression looking for aggregate functions and
4521** for variables that need to be added to AggInfo object that pNC->pAggInfo
4522** points to. Additional entries are made on the AggInfo object as
4523** necessary.
drh626a8792005-01-17 22:08:19 +00004524**
4525** This routine should only be called after the expression has been
drh7d10d5a2008-08-20 16:35:10 +00004526** analyzed by sqlite3ResolveExprNames().
drh626a8792005-01-17 22:08:19 +00004527*/
drhd2b3e232008-01-23 14:51:49 +00004528void sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
drh7d10d5a2008-08-20 16:35:10 +00004529 Walker w;
drh374fdce2012-04-17 16:38:53 +00004530 memset(&w, 0, sizeof(w));
drh7d10d5a2008-08-20 16:35:10 +00004531 w.xExprCallback = analyzeAggregate;
4532 w.xSelectCallback = analyzeAggregatesInSelect;
4533 w.u.pNC = pNC;
drh20bc3932009-05-30 23:35:43 +00004534 assert( pNC->pSrcList!=0 );
drh7d10d5a2008-08-20 16:35:10 +00004535 sqlite3WalkExpr(&w, pExpr);
drh22827922000-06-06 17:27:05 +00004536}
drh5d9a4af2005-08-30 00:54:01 +00004537
4538/*
4539** Call sqlite3ExprAnalyzeAggregates() for every expression in an
4540** expression list. Return the number of errors.
4541**
4542** If an error is found, the analysis is cut short.
4543*/
drhd2b3e232008-01-23 14:51:49 +00004544void sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){
drh5d9a4af2005-08-30 00:54:01 +00004545 struct ExprList_item *pItem;
4546 int i;
drh5d9a4af2005-08-30 00:54:01 +00004547 if( pList ){
drhd2b3e232008-01-23 14:51:49 +00004548 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
4549 sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
drh5d9a4af2005-08-30 00:54:01 +00004550 }
4551 }
drh5d9a4af2005-08-30 00:54:01 +00004552}
drh892d3172008-01-10 03:46:36 +00004553
4554/*
drhceea3322009-04-23 13:22:42 +00004555** Allocate a single new register for use to hold some intermediate result.
drh892d3172008-01-10 03:46:36 +00004556*/
4557int sqlite3GetTempReg(Parse *pParse){
drhe55cbd72008-03-31 23:48:03 +00004558 if( pParse->nTempReg==0 ){
drh892d3172008-01-10 03:46:36 +00004559 return ++pParse->nMem;
4560 }
danielk19772f425f62008-07-04 09:41:39 +00004561 return pParse->aTempReg[--pParse->nTempReg];
drh892d3172008-01-10 03:46:36 +00004562}
drhceea3322009-04-23 13:22:42 +00004563
4564/*
4565** Deallocate a register, making available for reuse for some other
4566** purpose.
4567**
4568** If a register is currently being used by the column cache, then
peter.d.reid60ec9142014-09-06 16:39:46 +00004569** the deallocation is deferred until the column cache line that uses
drhceea3322009-04-23 13:22:42 +00004570** the register becomes stale.
4571*/
drh892d3172008-01-10 03:46:36 +00004572void sqlite3ReleaseTempReg(Parse *pParse, int iReg){
drh2dcef112008-01-12 19:03:48 +00004573 if( iReg && pParse->nTempReg<ArraySize(pParse->aTempReg) ){
drhceea3322009-04-23 13:22:42 +00004574 int i;
4575 struct yColCache *p;
4576 for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
4577 if( p->iReg==iReg ){
4578 p->tempReg = 1;
4579 return;
4580 }
4581 }
drh892d3172008-01-10 03:46:36 +00004582 pParse->aTempReg[pParse->nTempReg++] = iReg;
4583 }
4584}
4585
4586/*
4587** Allocate or deallocate a block of nReg consecutive registers
4588*/
4589int sqlite3GetTempRange(Parse *pParse, int nReg){
drhe55cbd72008-03-31 23:48:03 +00004590 int i, n;
4591 i = pParse->iRangeReg;
4592 n = pParse->nRangeReg;
drhf49f3522009-12-30 14:12:38 +00004593 if( nReg<=n ){
4594 assert( !usedAsColumnCache(pParse, i, i+n-1) );
drh892d3172008-01-10 03:46:36 +00004595 pParse->iRangeReg += nReg;
4596 pParse->nRangeReg -= nReg;
4597 }else{
4598 i = pParse->nMem+1;
4599 pParse->nMem += nReg;
4600 }
4601 return i;
4602}
4603void sqlite3ReleaseTempRange(Parse *pParse, int iReg, int nReg){
drhf49f3522009-12-30 14:12:38 +00004604 sqlite3ExprCacheRemove(pParse, iReg, nReg);
drh892d3172008-01-10 03:46:36 +00004605 if( nReg>pParse->nRangeReg ){
4606 pParse->nRangeReg = nReg;
4607 pParse->iRangeReg = iReg;
4608 }
4609}
drhcdc69552011-12-06 13:24:59 +00004610
4611/*
4612** Mark all temporary registers as being unavailable for reuse.
4613*/
4614void sqlite3ClearTempRegCache(Parse *pParse){
4615 pParse->nTempReg = 0;
4616 pParse->nRangeReg = 0;
4617}
drhbb9b5f22016-03-19 00:35:02 +00004618
4619/*
4620** Validate that no temporary register falls within the range of
4621** iFirst..iLast, inclusive. This routine is only call from within assert()
4622** statements.
4623*/
4624#ifdef SQLITE_DEBUG
4625int sqlite3NoTempsInRange(Parse *pParse, int iFirst, int iLast){
4626 int i;
4627 if( pParse->nRangeReg>0
4628 && pParse->iRangeReg+pParse->nRangeReg<iLast
4629 && pParse->iRangeReg>=iFirst
4630 ){
4631 return 0;
4632 }
4633 for(i=0; i<pParse->nTempReg; i++){
4634 if( pParse->aTempReg[i]>=iFirst && pParse->aTempReg[i]<=iLast ){
4635 return 0;
4636 }
4637 }
4638 return 1;
4639}
4640#endif /* SQLITE_DEBUG */