blob: cc795c09f0b561273775d00ff49ce1ea4238ff58 [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
drh12abf402016-08-22 14:30:05 +000017/* Forward declarations */
18static void exprCodeBetween(Parse*,Expr*,int,void(*)(Parse*,Expr*,int,int),int);
19static int exprCodeVector(Parse *pParse, Expr *p, int *piToFree);
20
drh0dfa4f62016-08-26 13:19:49 +000021/*
22** Return the affinity character for a single column of a table.
23*/
24char sqlite3TableColumnAffinity(Table *pTab, int iCol){
25 assert( iCol<pTab->nCol );
26 return iCol>=0 ? pTab->aCol[iCol].affinity : SQLITE_AFF_INTEGER;
27}
drh12abf402016-08-22 14:30:05 +000028
danielk1977e014a832004-05-17 10:48:57 +000029/*
30** Return the 'affinity' of the expression pExpr if any.
31**
32** If pExpr is a column, a reference to a column via an 'AS' alias,
33** or a sub-select with a column as the return value, then the
34** affinity of that column is returned. Otherwise, 0x00 is returned,
35** indicating no affinity for the expression.
36**
peter.d.reid60ec9142014-09-06 16:39:46 +000037** i.e. the WHERE clause expressions in the following statements all
danielk1977e014a832004-05-17 10:48:57 +000038** have an affinity:
39**
40** CREATE TABLE t1(a);
41** SELECT * FROM t1 WHERE a;
42** SELECT a AS b FROM t1 WHERE b;
43** SELECT * FROM t1 WHERE (select a from t1);
44*/
danielk1977bf3b7212004-05-18 10:06:24 +000045char sqlite3ExprAffinity(Expr *pExpr){
drh580c8c12012-12-08 03:34:04 +000046 int op;
drha7d6db62019-06-11 21:02:15 +000047 while( ExprHasProperty(pExpr, EP_Skip) ){
48 assert( pExpr->op==TK_COLLATE );
49 pExpr = pExpr->pLeft;
50 assert( pExpr!=0 );
51 }
drh580c8c12012-12-08 03:34:04 +000052 op = pExpr->op;
drh487e2622005-06-25 18:42:14 +000053 if( op==TK_SELECT ){
danielk19776ab3a2e2009-02-19 14:39:25 +000054 assert( pExpr->flags&EP_xIsSelect );
55 return sqlite3ExprAffinity(pExpr->x.pSelect->pEList->a[0].pExpr);
danielk1977a37cdde2004-05-16 11:15:36 +000056 }
drhdb45bd52016-08-22 00:48:58 +000057 if( op==TK_REGISTER ) op = pExpr->op2;
drh487e2622005-06-25 18:42:14 +000058#ifndef SQLITE_OMIT_CAST
59 if( op==TK_CAST ){
drh33e619f2009-05-28 01:00:55 +000060 assert( !ExprHasProperty(pExpr, EP_IntValue) );
drhfdaac672013-10-04 15:30:21 +000061 return sqlite3AffinityType(pExpr->u.zToken, 0);
drh487e2622005-06-25 18:42:14 +000062 }
63#endif
drheda079c2018-09-20 19:02:15 +000064 if( (op==TK_AGG_COLUMN || op==TK_COLUMN) && pExpr->y.pTab ){
65 return sqlite3TableColumnAffinity(pExpr->y.pTab, pExpr->iColumn);
drh7d10d5a2008-08-20 16:35:10 +000066 }
dan80aa5452016-09-03 19:52:12 +000067 if( op==TK_SELECT_COLUMN ){
68 assert( pExpr->pLeft->flags&EP_xIsSelect );
69 return sqlite3ExprAffinity(
70 pExpr->pLeft->x.pSelect->pEList->a[pExpr->iColumn].pExpr
71 );
72 }
drhdb36e252019-10-22 19:51:29 +000073 if( op==TK_VECTOR ){
74 return sqlite3ExprAffinity(pExpr->x.pList->a[0].pExpr);
75 }
drh11949042019-08-05 18:01:42 +000076 return pExpr->affExpr;
danielk1977a37cdde2004-05-16 11:15:36 +000077}
78
drh53db1452004-05-20 13:54:53 +000079/*
drh8b4c40d2007-02-01 23:02:45 +000080** Set the collating sequence for expression pExpr to be the collating
drhae80dde2012-12-06 21:16:43 +000081** sequence named by pToken. Return a pointer to a new Expr node that
82** implements the COLLATE operator.
drh0a8a4062012-12-07 18:38:16 +000083**
84** If a memory allocation error occurs, that fact is recorded in pParse->db
85** and the pExpr parameter is returned unchanged.
drh8b4c40d2007-02-01 23:02:45 +000086*/
drh4ef7efa2014-03-20 15:14:08 +000087Expr *sqlite3ExprAddCollateToken(
88 Parse *pParse, /* Parsing context */
89 Expr *pExpr, /* Add the "COLLATE" clause to this expression */
dan80103fc2015-03-20 08:43:59 +000090 const Token *pCollName, /* Name of collating sequence */
91 int dequote /* True to dequote pCollName */
drh4ef7efa2014-03-20 15:14:08 +000092){
drh0a8a4062012-12-07 18:38:16 +000093 if( pCollName->n>0 ){
dan80103fc2015-03-20 08:43:59 +000094 Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLLATE, pCollName, dequote);
drh0a8a4062012-12-07 18:38:16 +000095 if( pNew ){
96 pNew->pLeft = pExpr;
drha4c3c872013-09-12 17:29:25 +000097 pNew->flags |= EP_Collate|EP_Skip;
drh0a8a4062012-12-07 18:38:16 +000098 pExpr = pNew;
99 }
drhae80dde2012-12-06 21:16:43 +0000100 }
drh0a8a4062012-12-07 18:38:16 +0000101 return pExpr;
102}
103Expr *sqlite3ExprAddCollateString(Parse *pParse, Expr *pExpr, const char *zC){
drh261d8a52012-12-08 21:36:26 +0000104 Token s;
105 assert( zC!=0 );
drh40aced52016-01-22 17:48:09 +0000106 sqlite3TokenInit(&s, (char*)zC);
dan80103fc2015-03-20 08:43:59 +0000107 return sqlite3ExprAddCollateToken(pParse, pExpr, &s, 0);
drh0a8a4062012-12-07 18:38:16 +0000108}
109
110/*
drh0d950af2019-08-22 16:38:42 +0000111** Skip over any TK_COLLATE operators.
drh0a8a4062012-12-07 18:38:16 +0000112*/
113Expr *sqlite3ExprSkipCollate(Expr *pExpr){
drh0d950af2019-08-22 16:38:42 +0000114 while( pExpr && ExprHasProperty(pExpr, EP_Skip) ){
115 assert( pExpr->op==TK_COLLATE );
116 pExpr = pExpr->pLeft;
117 }
118 return pExpr;
119}
120
121/*
122** Skip over any TK_COLLATE operators and/or any unlikely()
123** or likelihood() or likely() functions at the root of an
124** expression.
125*/
126Expr *sqlite3ExprSkipCollateAndLikely(Expr *pExpr){
drha7d6db62019-06-11 21:02:15 +0000127 while( pExpr && ExprHasProperty(pExpr, EP_Skip|EP_Unlikely) ){
drha4c3c872013-09-12 17:29:25 +0000128 if( ExprHasProperty(pExpr, EP_Unlikely) ){
drhcca9f3d2013-09-06 15:23:29 +0000129 assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
130 assert( pExpr->x.pList->nExpr>0 );
drha4c3c872013-09-12 17:29:25 +0000131 assert( pExpr->op==TK_FUNCTION );
drhcca9f3d2013-09-06 15:23:29 +0000132 pExpr = pExpr->x.pList->a[0].pExpr;
133 }else{
drh0b8d2552015-09-05 22:36:07 +0000134 assert( pExpr->op==TK_COLLATE );
drha4c3c872013-09-12 17:29:25 +0000135 pExpr = pExpr->pLeft;
drhcca9f3d2013-09-06 15:23:29 +0000136 }
drha4c3c872013-09-12 17:29:25 +0000137 }
drh0a8a4062012-12-07 18:38:16 +0000138 return pExpr;
drh8b4c40d2007-02-01 23:02:45 +0000139}
140
141/*
drhae80dde2012-12-06 21:16:43 +0000142** Return the collation sequence for the expression pExpr. If
143** there is no defined collating sequence, return NULL.
144**
drh70efa842017-09-28 01:58:23 +0000145** See also: sqlite3ExprNNCollSeq()
146**
147** The sqlite3ExprNNCollSeq() works the same exact that it returns the
148** default collation if pExpr has no defined collation.
149**
drhae80dde2012-12-06 21:16:43 +0000150** The collating sequence might be determined by a COLLATE operator
151** or by the presence of a column with a defined collating sequence.
152** COLLATE operators take first precedence. Left operands take
153** precedence over right operands.
danielk19770202b292004-06-09 09:55:16 +0000154*/
danielk19777cedc8d2004-06-10 10:50:08 +0000155CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
drhae80dde2012-12-06 21:16:43 +0000156 sqlite3 *db = pParse->db;
danielk19777cedc8d2004-06-10 10:50:08 +0000157 CollSeq *pColl = 0;
drh7d10d5a2008-08-20 16:35:10 +0000158 Expr *p = pExpr;
drh261d8a52012-12-08 21:36:26 +0000159 while( p ){
drhae80dde2012-12-06 21:16:43 +0000160 int op = p->op;
drhcb0e04f2018-12-12 21:34:17 +0000161 if( op==TK_REGISTER ) op = p->op2;
162 if( (op==TK_AGG_COLUMN || op==TK_COLUMN || op==TK_TRIGGER)
drheda079c2018-09-20 19:02:15 +0000163 && p->y.pTab!=0
drhae80dde2012-12-06 21:16:43 +0000164 ){
drheda079c2018-09-20 19:02:15 +0000165 /* op==TK_REGISTER && p->y.pTab!=0 happens when pExpr was originally
drh7d10d5a2008-08-20 16:35:10 +0000166 ** a TK_COLUMN but was previously evaluated and cached in a register */
drh7d10d5a2008-08-20 16:35:10 +0000167 int j = p->iColumn;
168 if( j>=0 ){
drheda079c2018-09-20 19:02:15 +0000169 const char *zColl = p->y.pTab->aCol[j].zColl;
drhc4a64fa2009-05-11 20:53:28 +0000170 pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0);
drh7d10d5a2008-08-20 16:35:10 +0000171 }
172 break;
danielk19770202b292004-06-09 09:55:16 +0000173 }
drhe081d732018-07-27 18:19:12 +0000174 if( op==TK_CAST || op==TK_UPLUS ){
175 p = p->pLeft;
176 continue;
177 }
drh269d3222019-10-23 18:09:39 +0000178 if( op==TK_VECTOR ){
179 p = p->x.pList->a[0].pExpr;
180 continue;
181 }
drhcb0e04f2018-12-12 21:34:17 +0000182 if( op==TK_COLLATE ){
drhe081d732018-07-27 18:19:12 +0000183 pColl = sqlite3GetCollSeq(pParse, ENC(db), 0, p->u.zToken);
184 break;
185 }
drhae80dde2012-12-06 21:16:43 +0000186 if( p->flags & EP_Collate ){
drh2308ed32015-02-09 16:09:34 +0000187 if( p->pLeft && (p->pLeft->flags & EP_Collate)!=0 ){
drhae80dde2012-12-06 21:16:43 +0000188 p = p->pLeft;
189 }else{
drh2308ed32015-02-09 16:09:34 +0000190 Expr *pNext = p->pRight;
drh6728cd92015-02-09 18:28:03 +0000191 /* The Expr.x union is never used at the same time as Expr.pRight */
192 assert( p->x.pList==0 || p->pRight==0 );
drh92a28242019-10-09 15:37:58 +0000193 if( p->x.pList!=0
194 && !db->mallocFailed
195 && ALWAYS(!ExprHasProperty(p, EP_xIsSelect))
196 ){
drh2308ed32015-02-09 16:09:34 +0000197 int i;
drh95768022019-11-10 10:08:03 +0000198 for(i=0; i<p->x.pList->nExpr; i++){
drh2308ed32015-02-09 16:09:34 +0000199 if( ExprHasProperty(p->x.pList->a[i].pExpr, EP_Collate) ){
200 pNext = p->x.pList->a[i].pExpr;
201 break;
202 }
203 }
204 }
205 p = pNext;
drhae80dde2012-12-06 21:16:43 +0000206 }
207 }else{
drh7d10d5a2008-08-20 16:35:10 +0000208 break;
209 }
danielk19770202b292004-06-09 09:55:16 +0000210 }
danielk19777cedc8d2004-06-10 10:50:08 +0000211 if( sqlite3CheckCollSeq(pParse, pColl) ){
212 pColl = 0;
213 }
214 return pColl;
danielk19770202b292004-06-09 09:55:16 +0000215}
216
217/*
drh70efa842017-09-28 01:58:23 +0000218** Return the collation sequence for the expression pExpr. If
219** there is no defined collating sequence, return a pointer to the
220** defautl collation sequence.
221**
222** See also: sqlite3ExprCollSeq()
223**
224** The sqlite3ExprCollSeq() routine works the same except that it
225** returns NULL if there is no defined collation.
226*/
227CollSeq *sqlite3ExprNNCollSeq(Parse *pParse, Expr *pExpr){
228 CollSeq *p = sqlite3ExprCollSeq(pParse, pExpr);
229 if( p==0 ) p = pParse->db->pDfltColl;
230 assert( p!=0 );
231 return p;
232}
233
234/*
235** Return TRUE if the two expressions have equivalent collating sequences.
236*/
237int sqlite3ExprCollSeqMatch(Parse *pParse, Expr *pE1, Expr *pE2){
238 CollSeq *pColl1 = sqlite3ExprNNCollSeq(pParse, pE1);
239 CollSeq *pColl2 = sqlite3ExprNNCollSeq(pParse, pE2);
240 return sqlite3StrICmp(pColl1->zName, pColl2->zName)==0;
241}
242
243/*
drh626a8792005-01-17 22:08:19 +0000244** pExpr is an operand of a comparison operator. aff2 is the
245** type affinity of the other operand. This routine returns the
drh53db1452004-05-20 13:54:53 +0000246** type affinity that should be used for the comparison operator.
247*/
danielk1977e014a832004-05-17 10:48:57 +0000248char sqlite3CompareAffinity(Expr *pExpr, char aff2){
danielk1977bf3b7212004-05-18 10:06:24 +0000249 char aff1 = sqlite3ExprAffinity(pExpr);
drh96fb16e2019-08-06 14:37:24 +0000250 if( aff1>SQLITE_AFF_NONE && aff2>SQLITE_AFF_NONE ){
drh8df447f2005-11-01 15:48:24 +0000251 /* Both sides of the comparison are columns. If one has numeric
252 ** affinity, use that. Otherwise use no affinity.
danielk1977e014a832004-05-17 10:48:57 +0000253 */
drh8a512562005-11-14 22:29:05 +0000254 if( sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2) ){
danielk1977e014a832004-05-17 10:48:57 +0000255 return SQLITE_AFF_NUMERIC;
256 }else{
drh05883a32015-06-02 15:32:08 +0000257 return SQLITE_AFF_BLOB;
danielk1977e014a832004-05-17 10:48:57 +0000258 }
danielk1977e014a832004-05-17 10:48:57 +0000259 }else{
260 /* One side is a column, the other is not. Use the columns affinity. */
drh96fb16e2019-08-06 14:37:24 +0000261 assert( aff1<=SQLITE_AFF_NONE || aff2<=SQLITE_AFF_NONE );
262 return (aff1<=SQLITE_AFF_NONE ? aff2 : aff1) | SQLITE_AFF_NONE;
danielk1977e014a832004-05-17 10:48:57 +0000263 }
264}
265
drh53db1452004-05-20 13:54:53 +0000266/*
267** pExpr is a comparison operator. Return the type affinity that should
268** be applied to both operands prior to doing the comparison.
269*/
danielk1977e014a832004-05-17 10:48:57 +0000270static char comparisonAffinity(Expr *pExpr){
271 char aff;
272 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
273 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
drh6a2fe092009-09-23 02:29:36 +0000274 pExpr->op==TK_NE || pExpr->op==TK_IS || pExpr->op==TK_ISNOT );
danielk1977e014a832004-05-17 10:48:57 +0000275 assert( pExpr->pLeft );
danielk1977bf3b7212004-05-18 10:06:24 +0000276 aff = sqlite3ExprAffinity(pExpr->pLeft);
danielk1977e014a832004-05-17 10:48:57 +0000277 if( pExpr->pRight ){
278 aff = sqlite3CompareAffinity(pExpr->pRight, aff);
danielk19776ab3a2e2009-02-19 14:39:25 +0000279 }else if( ExprHasProperty(pExpr, EP_xIsSelect) ){
280 aff = sqlite3CompareAffinity(pExpr->x.pSelect->pEList->a[0].pExpr, aff);
drh13ac46e2017-02-11 13:51:23 +0000281 }else if( aff==0 ){
drh05883a32015-06-02 15:32:08 +0000282 aff = SQLITE_AFF_BLOB;
danielk1977e014a832004-05-17 10:48:57 +0000283 }
284 return aff;
285}
286
287/*
288** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
289** idx_affinity is the affinity of an indexed column. Return true
290** if the index with affinity idx_affinity may be used to implement
291** the comparison in pExpr.
292*/
293int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){
294 char aff = comparisonAffinity(pExpr);
drh915e4342019-08-06 15:18:15 +0000295 if( aff<SQLITE_AFF_TEXT ){
296 return 1;
drh8a512562005-11-14 22:29:05 +0000297 }
drh915e4342019-08-06 15:18:15 +0000298 if( aff==SQLITE_AFF_TEXT ){
299 return idx_affinity==SQLITE_AFF_TEXT;
300 }
301 return sqlite3IsNumericAffinity(idx_affinity);
danielk1977e014a832004-05-17 10:48:57 +0000302}
303
danielk1977a37cdde2004-05-16 11:15:36 +0000304/*
drh35573352008-01-08 23:54:25 +0000305** Return the P5 value that should be used for a binary comparison
danielk1977a37cdde2004-05-16 11:15:36 +0000306** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
danielk1977a37cdde2004-05-16 11:15:36 +0000307*/
drh35573352008-01-08 23:54:25 +0000308static u8 binaryCompareP5(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){
309 u8 aff = (char)sqlite3ExprAffinity(pExpr2);
drh1bd10f82008-12-10 21:19:56 +0000310 aff = (u8)sqlite3CompareAffinity(pExpr1, aff) | (u8)jumpIfNull;
drh35573352008-01-08 23:54:25 +0000311 return aff;
danielk1977a37cdde2004-05-16 11:15:36 +0000312}
313
drha2e00042002-01-22 03:13:42 +0000314/*
danielk19770202b292004-06-09 09:55:16 +0000315** Return a pointer to the collation sequence that should be used by
316** a binary comparison operator comparing pLeft and pRight.
317**
318** If the left hand expression has a collating sequence type, then it is
319** used. Otherwise the collation sequence for the right hand expression
320** is used, or the default (BINARY) if neither expression has a collating
321** type.
danielk1977bcbb04e2007-05-29 12:11:29 +0000322**
323** Argument pRight (but not pLeft) may be a null pointer. In this case,
324** it is not considered.
danielk19770202b292004-06-09 09:55:16 +0000325*/
drh0a0e1312007-08-07 17:04:59 +0000326CollSeq *sqlite3BinaryCompareCollSeq(
danielk1977bcbb04e2007-05-29 12:11:29 +0000327 Parse *pParse,
328 Expr *pLeft,
329 Expr *pRight
330){
drhec41dda2007-02-07 13:09:45 +0000331 CollSeq *pColl;
332 assert( pLeft );
drhae80dde2012-12-06 21:16:43 +0000333 if( pLeft->flags & EP_Collate ){
334 pColl = sqlite3ExprCollSeq(pParse, pLeft);
335 }else if( pRight && (pRight->flags & EP_Collate)!=0 ){
336 pColl = sqlite3ExprCollSeq(pParse, pRight);
drhec41dda2007-02-07 13:09:45 +0000337 }else{
338 pColl = sqlite3ExprCollSeq(pParse, pLeft);
339 if( !pColl ){
340 pColl = sqlite3ExprCollSeq(pParse, pRight);
341 }
danielk19770202b292004-06-09 09:55:16 +0000342 }
343 return pColl;
344}
345
drh898c5272019-10-22 00:03:41 +0000346/* Expresssion p is a comparison operator. Return a collation sequence
347** appropriate for the comparison operator.
348**
349** This is normally just a wrapper around sqlite3BinaryCompareCollSeq().
350** However, if the OP_Commuted flag is set, then the order of the operands
351** is reversed in the sqlite3BinaryCompareCollSeq() call so that the
352** correct collating sequence is found.
353*/
354CollSeq *sqlite3ExprCompareCollSeq(Parse *pParse, Expr *p){
355 if( ExprHasProperty(p, EP_Commuted) ){
356 return sqlite3BinaryCompareCollSeq(pParse, p->pRight, p->pLeft);
357 }else{
358 return sqlite3BinaryCompareCollSeq(pParse, p->pLeft, p->pRight);
359 }
360}
361
danielk19770202b292004-06-09 09:55:16 +0000362/*
drhbe5c89a2004-07-26 00:31:09 +0000363** Generate code for a comparison operator.
364*/
365static int codeCompare(
366 Parse *pParse, /* The parsing (and code generating) context */
367 Expr *pLeft, /* The left operand */
368 Expr *pRight, /* The right operand */
369 int opcode, /* The comparison opcode */
drh35573352008-01-08 23:54:25 +0000370 int in1, int in2, /* Register holding operands */
drhbe5c89a2004-07-26 00:31:09 +0000371 int dest, /* Jump here if true. */
drh898c5272019-10-22 00:03:41 +0000372 int jumpIfNull, /* If true, jump if either operand is NULL */
373 int isCommuted /* The comparison has been commuted */
drhbe5c89a2004-07-26 00:31:09 +0000374){
drh35573352008-01-08 23:54:25 +0000375 int p5;
376 int addr;
377 CollSeq *p4;
378
drh86541862019-12-19 20:37:32 +0000379 if( pParse->nErr ) return 0;
drh898c5272019-10-22 00:03:41 +0000380 if( isCommuted ){
381 p4 = sqlite3BinaryCompareCollSeq(pParse, pRight, pLeft);
382 }else{
383 p4 = sqlite3BinaryCompareCollSeq(pParse, pLeft, pRight);
384 }
drh35573352008-01-08 23:54:25 +0000385 p5 = binaryCompareP5(pLeft, pRight, jumpIfNull);
386 addr = sqlite3VdbeAddOp4(pParse->pVdbe, opcode, in2, dest, in1,
387 (void*)p4, P4_COLLSEQ);
drh1bd10f82008-12-10 21:19:56 +0000388 sqlite3VdbeChangeP5(pParse->pVdbe, (u8)p5);
drh35573352008-01-08 23:54:25 +0000389 return addr;
drhbe5c89a2004-07-26 00:31:09 +0000390}
391
dancfbb5e82016-07-13 19:48:13 +0000392/*
dan870a0702016-08-01 16:37:43 +0000393** Return true if expression pExpr is a vector, or false otherwise.
drhd832da72016-08-20 21:11:25 +0000394**
395** A vector is defined as any expression that results in two or more
396** columns of result. Every TK_VECTOR node is an vector because the
397** parser will not generate a TK_VECTOR with fewer than two entries.
398** But a TK_SELECT might be either a vector or a scalar. It is only
399** considered a vector if it has two or more result columns.
dan870a0702016-08-01 16:37:43 +0000400*/
401int sqlite3ExprIsVector(Expr *pExpr){
drh76dbe7a2016-08-20 21:02:38 +0000402 return sqlite3ExprVectorSize(pExpr)>1;
dan870a0702016-08-01 16:37:43 +0000403}
404
405/*
dancfbb5e82016-07-13 19:48:13 +0000406** If the expression passed as the only argument is of type TK_VECTOR
407** return the number of expressions in the vector. Or, if the expression
408** is a sub-select, return the number of columns in the sub-select. For
409** any other type of expression, return 1.
410*/
dan71c57db2016-07-09 20:23:55 +0000411int sqlite3ExprVectorSize(Expr *pExpr){
drh12abf402016-08-22 14:30:05 +0000412 u8 op = pExpr->op;
413 if( op==TK_REGISTER ) op = pExpr->op2;
414 if( op==TK_VECTOR ){
drh76dbe7a2016-08-20 21:02:38 +0000415 return pExpr->x.pList->nExpr;
drh12abf402016-08-22 14:30:05 +0000416 }else if( op==TK_SELECT ){
dan71c57db2016-07-09 20:23:55 +0000417 return pExpr->x.pSelect->pEList->nExpr;
drh76dbe7a2016-08-20 21:02:38 +0000418 }else{
419 return 1;
dan71c57db2016-07-09 20:23:55 +0000420 }
dan71c57db2016-07-09 20:23:55 +0000421}
422
danba00e302016-07-23 20:24:06 +0000423/*
drhfc7f27b2016-08-20 00:07:01 +0000424** Return a pointer to a subexpression of pVector that is the i-th
425** column of the vector (numbered starting with 0). The caller must
426** ensure that i is within range.
427**
drh76dbe7a2016-08-20 21:02:38 +0000428** If pVector is really a scalar (and "scalar" here includes subqueries
429** that return a single column!) then return pVector unmodified.
430**
drhfc7f27b2016-08-20 00:07:01 +0000431** pVector retains ownership of the returned subexpression.
432**
433** If the vector is a (SELECT ...) then the expression returned is
drh76dbe7a2016-08-20 21:02:38 +0000434** just the expression for the i-th term of the result set, and may
435** not be ready for evaluation because the table cursor has not yet
436** been positioned.
danba00e302016-07-23 20:24:06 +0000437*/
drhfc7f27b2016-08-20 00:07:01 +0000438Expr *sqlite3VectorFieldSubexpr(Expr *pVector, int i){
dan870a0702016-08-01 16:37:43 +0000439 assert( i<sqlite3ExprVectorSize(pVector) );
440 if( sqlite3ExprIsVector(pVector) ){
drh9f24b532016-09-05 22:50:48 +0000441 assert( pVector->op2==0 || pVector->op==TK_REGISTER );
442 if( pVector->op==TK_SELECT || pVector->op2==TK_SELECT ){
dan870a0702016-08-01 16:37:43 +0000443 return pVector->x.pSelect->pEList->a[i].pExpr;
444 }else{
445 return pVector->x.pList->a[i].pExpr;
446 }
dan71c57db2016-07-09 20:23:55 +0000447 }
dan870a0702016-08-01 16:37:43 +0000448 return pVector;
dan71c57db2016-07-09 20:23:55 +0000449}
drhfc7f27b2016-08-20 00:07:01 +0000450
drhfc7f27b2016-08-20 00:07:01 +0000451/*
452** Compute and return a new Expr object which when passed to
453** sqlite3ExprCode() will generate all necessary code to compute
454** the iField-th column of the vector expression pVector.
455**
drh8762ec12016-08-20 01:06:22 +0000456** It is ok for pVector to be a scalar (as long as iField==0).
457** In that case, this routine works like sqlite3ExprDup().
458**
drhfc7f27b2016-08-20 00:07:01 +0000459** The caller owns the returned Expr object and is responsible for
460** ensuring that the returned value eventually gets freed.
461**
drh8762ec12016-08-20 01:06:22 +0000462** The caller retains ownership of pVector. If pVector is a TK_SELECT,
danfad0e702016-09-06 12:04:50 +0000463** then the returned object will reference pVector and so pVector must remain
drh8762ec12016-08-20 01:06:22 +0000464** valid for the life of the returned object. If pVector is a TK_VECTOR
465** or a scalar expression, then it can be deleted as soon as this routine
drh76dbe7a2016-08-20 21:02:38 +0000466** returns.
drh8762ec12016-08-20 01:06:22 +0000467**
468** A trick to cause a TK_SELECT pVector to be deleted together with
469** the returned Expr object is to attach the pVector to the pRight field
470** of the returned TK_SELECT_COLUMN Expr object.
drhfc7f27b2016-08-20 00:07:01 +0000471*/
472Expr *sqlite3ExprForVectorField(
473 Parse *pParse, /* Parsing context */
474 Expr *pVector, /* The vector. List of expressions or a sub-SELECT */
drha1251bc2016-08-20 00:51:37 +0000475 int iField /* Which column of the vector to return */
drhfc7f27b2016-08-20 00:07:01 +0000476){
477 Expr *pRet;
drha1251bc2016-08-20 00:51:37 +0000478 if( pVector->op==TK_SELECT ){
479 assert( pVector->flags & EP_xIsSelect );
drhfc7f27b2016-08-20 00:07:01 +0000480 /* The TK_SELECT_COLUMN Expr node:
481 **
drh966e2912017-01-03 02:58:01 +0000482 ** pLeft: pVector containing TK_SELECT. Not deleted.
drh8762ec12016-08-20 01:06:22 +0000483 ** pRight: not used. But recursively deleted.
drhfc7f27b2016-08-20 00:07:01 +0000484 ** iColumn: Index of a column in pVector
drh966e2912017-01-03 02:58:01 +0000485 ** iTable: 0 or the number of columns on the LHS of an assignment
drhfc7f27b2016-08-20 00:07:01 +0000486 ** pLeft->iTable: First in an array of register holding result, or 0
487 ** if the result is not yet computed.
488 **
489 ** sqlite3ExprDelete() specifically skips the recursive delete of
490 ** pLeft on TK_SELECT_COLUMN nodes. But pRight is followed, so pVector
drh8762ec12016-08-20 01:06:22 +0000491 ** can be attached to pRight to cause this node to take ownership of
492 ** pVector. Typically there will be multiple TK_SELECT_COLUMN nodes
493 ** with the same pLeft pointer to the pVector, but only one of them
494 ** will own the pVector.
drhfc7f27b2016-08-20 00:07:01 +0000495 */
drhabfd35e2016-12-06 22:47:23 +0000496 pRet = sqlite3PExpr(pParse, TK_SELECT_COLUMN, 0, 0);
drh8bd0d582016-08-20 18:06:14 +0000497 if( pRet ){
498 pRet->iColumn = iField;
499 pRet->pLeft = pVector;
500 }
drhfc7f27b2016-08-20 00:07:01 +0000501 assert( pRet==0 || pRet->iTable==0 );
502 }else{
drha1251bc2016-08-20 00:51:37 +0000503 if( pVector->op==TK_VECTOR ) pVector = pVector->x.pList->a[iField].pExpr;
504 pRet = sqlite3ExprDup(pParse->db, pVector, 0);
dandfb5c962019-01-15 20:51:35 +0000505 sqlite3RenameTokenRemap(pParse, pRet, pVector);
drhfc7f27b2016-08-20 00:07:01 +0000506 }
507 return pRet;
508}
dan71c57db2016-07-09 20:23:55 +0000509
dan5c288b92016-07-30 21:02:33 +0000510/*
511** If expression pExpr is of type TK_SELECT, generate code to evaluate
512** it. Return the register in which the result is stored (or, if the
513** sub-select returns more than one column, the first in an array
514** of registers in which the result is stored).
515**
516** If pExpr is not a TK_SELECT expression, return 0.
517*/
518static int exprCodeSubselect(Parse *pParse, Expr *pExpr){
dan8da209b2016-07-26 18:06:08 +0000519 int reg = 0;
danf9b2e052016-08-02 17:45:00 +0000520#ifndef SQLITE_OMIT_SUBQUERY
dan5c288b92016-07-30 21:02:33 +0000521 if( pExpr->op==TK_SELECT ){
drh85bcdce2018-12-23 21:27:29 +0000522 reg = sqlite3CodeSubselect(pParse, pExpr);
dan8da209b2016-07-26 18:06:08 +0000523 }
danf9b2e052016-08-02 17:45:00 +0000524#endif
dan8da209b2016-07-26 18:06:08 +0000525 return reg;
526}
527
dan5c288b92016-07-30 21:02:33 +0000528/*
529** Argument pVector points to a vector expression - either a TK_VECTOR
dan870a0702016-08-01 16:37:43 +0000530** or TK_SELECT that returns more than one column. This function returns
531** the register number of a register that contains the value of
532** element iField of the vector.
533**
534** If pVector is a TK_SELECT expression, then code for it must have
535** already been generated using the exprCodeSubselect() routine. In this
536** case parameter regSelect should be the first in an array of registers
537** containing the results of the sub-select.
538**
539** If pVector is of type TK_VECTOR, then code for the requested field
540** is generated. In this case (*pRegFree) may be set to the number of
541** a temporary register to be freed by the caller before returning.
dan5c288b92016-07-30 21:02:33 +0000542**
543** Before returning, output parameter (*ppExpr) is set to point to the
544** Expr object corresponding to element iElem of the vector.
dan5c288b92016-07-30 21:02:33 +0000545*/
546static int exprVectorRegister(
547 Parse *pParse, /* Parse context */
548 Expr *pVector, /* Vector to extract element from */
dan870a0702016-08-01 16:37:43 +0000549 int iField, /* Field to extract from pVector */
dan5c288b92016-07-30 21:02:33 +0000550 int regSelect, /* First in array of registers */
551 Expr **ppExpr, /* OUT: Expression element */
552 int *pRegFree /* OUT: Temp register to free */
553){
drh12abf402016-08-22 14:30:05 +0000554 u8 op = pVector->op;
drhc1bcd9c2016-09-05 19:57:46 +0000555 assert( op==TK_VECTOR || op==TK_REGISTER || op==TK_SELECT );
drh12abf402016-08-22 14:30:05 +0000556 if( op==TK_REGISTER ){
557 *ppExpr = sqlite3VectorFieldSubexpr(pVector, iField);
558 return pVector->iTable+iField;
559 }
560 if( op==TK_SELECT ){
dan870a0702016-08-01 16:37:43 +0000561 *ppExpr = pVector->x.pSelect->pEList->a[iField].pExpr;
562 return regSelect+iField;
dan5c288b92016-07-30 21:02:33 +0000563 }
dan870a0702016-08-01 16:37:43 +0000564 *ppExpr = pVector->x.pList->a[iField].pExpr;
dan5c288b92016-07-30 21:02:33 +0000565 return sqlite3ExprCodeTemp(pParse, *ppExpr, pRegFree);
566}
567
568/*
569** Expression pExpr is a comparison between two vector values. Compute
drh79752b62016-08-13 10:02:17 +0000570** the result of the comparison (1, 0, or NULL) and write that
571** result into register dest.
572**
573** The caller must satisfy the following preconditions:
574**
575** if pExpr->op==TK_IS: op==TK_EQ and p5==SQLITE_NULLEQ
576** if pExpr->op==TK_ISNOT: op==TK_NE and p5==SQLITE_NULLEQ
577** otherwise: op==pExpr->op and p5==0
dan5c288b92016-07-30 21:02:33 +0000578*/
drh79752b62016-08-13 10:02:17 +0000579static void codeVectorCompare(
580 Parse *pParse, /* Code generator context */
581 Expr *pExpr, /* The comparison operation */
582 int dest, /* Write results into this register */
583 u8 op, /* Comparison operator */
584 u8 p5 /* SQLITE_NULLEQ or zero */
585){
dan71c57db2016-07-09 20:23:55 +0000586 Vdbe *v = pParse->pVdbe;
587 Expr *pLeft = pExpr->pLeft;
588 Expr *pRight = pExpr->pRight;
589 int nLeft = sqlite3ExprVectorSize(pLeft);
drhb29e60c2016-09-05 12:02:34 +0000590 int i;
591 int regLeft = 0;
592 int regRight = 0;
593 u8 opx = op;
drhec4ccdb2018-12-29 02:26:59 +0000594 int addrDone = sqlite3VdbeMakeLabel(pParse);
drh898c5272019-10-22 00:03:41 +0000595 int isCommuted = ExprHasProperty(pExpr,EP_Commuted);
dan71c57db2016-07-09 20:23:55 +0000596
drh70d6b832019-12-30 23:50:19 +0000597 if( pParse->nErr ) return;
drh245ce622017-01-01 12:44:07 +0000598 if( nLeft!=sqlite3ExprVectorSize(pRight) ){
599 sqlite3ErrorMsg(pParse, "row value misused");
600 return;
601 }
drhb29e60c2016-09-05 12:02:34 +0000602 assert( pExpr->op==TK_EQ || pExpr->op==TK_NE
603 || pExpr->op==TK_IS || pExpr->op==TK_ISNOT
604 || pExpr->op==TK_LT || pExpr->op==TK_GT
605 || pExpr->op==TK_LE || pExpr->op==TK_GE
606 );
607 assert( pExpr->op==op || (pExpr->op==TK_IS && op==TK_EQ)
608 || (pExpr->op==TK_ISNOT && op==TK_NE) );
609 assert( p5==0 || pExpr->op!=op );
610 assert( p5==SQLITE_NULLEQ || pExpr->op==op );
dan71c57db2016-07-09 20:23:55 +0000611
drhb29e60c2016-09-05 12:02:34 +0000612 p5 |= SQLITE_STOREP2;
613 if( opx==TK_LE ) opx = TK_LT;
614 if( opx==TK_GE ) opx = TK_GT;
dan71c57db2016-07-09 20:23:55 +0000615
drhb29e60c2016-09-05 12:02:34 +0000616 regLeft = exprCodeSubselect(pParse, pLeft);
617 regRight = exprCodeSubselect(pParse, pRight);
dan71c57db2016-07-09 20:23:55 +0000618
drhb29e60c2016-09-05 12:02:34 +0000619 for(i=0; 1 /*Loop exits by "break"*/; i++){
620 int regFree1 = 0, regFree2 = 0;
621 Expr *pL, *pR;
622 int r1, r2;
623 assert( i>=0 && i<nLeft );
drhb29e60c2016-09-05 12:02:34 +0000624 r1 = exprVectorRegister(pParse, pLeft, i, regLeft, &pL, &regFree1);
625 r2 = exprVectorRegister(pParse, pRight, i, regRight, &pR, &regFree2);
drh898c5272019-10-22 00:03:41 +0000626 codeCompare(pParse, pL, pR, opx, r1, r2, dest, p5, isCommuted);
drhb29e60c2016-09-05 12:02:34 +0000627 testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
628 testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
629 testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
630 testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
631 testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq);
632 testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne);
633 sqlite3ReleaseTempReg(pParse, regFree1);
634 sqlite3ReleaseTempReg(pParse, regFree2);
drhb29e60c2016-09-05 12:02:34 +0000635 if( i==nLeft-1 ){
636 break;
dan71c57db2016-07-09 20:23:55 +0000637 }
drhb29e60c2016-09-05 12:02:34 +0000638 if( opx==TK_EQ ){
639 sqlite3VdbeAddOp2(v, OP_IfNot, dest, addrDone); VdbeCoverage(v);
640 p5 |= SQLITE_KEEPNULL;
641 }else if( opx==TK_NE ){
642 sqlite3VdbeAddOp2(v, OP_If, dest, addrDone); VdbeCoverage(v);
643 p5 |= SQLITE_KEEPNULL;
644 }else{
645 assert( op==TK_LT || op==TK_GT || op==TK_LE || op==TK_GE );
646 sqlite3VdbeAddOp2(v, OP_ElseNotEq, 0, addrDone);
647 VdbeCoverageIf(v, op==TK_LT);
648 VdbeCoverageIf(v, op==TK_GT);
649 VdbeCoverageIf(v, op==TK_LE);
650 VdbeCoverageIf(v, op==TK_GE);
651 if( i==nLeft-2 ) opx = op;
652 }
dan71c57db2016-07-09 20:23:55 +0000653 }
drhb29e60c2016-09-05 12:02:34 +0000654 sqlite3VdbeResolveLabel(v, addrDone);
dan71c57db2016-07-09 20:23:55 +0000655}
656
danielk19774b5255a2008-06-05 16:47:39 +0000657#if SQLITE_MAX_EXPR_DEPTH>0
658/*
659** Check that argument nHeight is less than or equal to the maximum
660** expression depth allowed. If it is not, leave an error message in
661** pParse.
662*/
drh7d10d5a2008-08-20 16:35:10 +0000663int sqlite3ExprCheckHeight(Parse *pParse, int nHeight){
danielk19774b5255a2008-06-05 16:47:39 +0000664 int rc = SQLITE_OK;
665 int mxHeight = pParse->db->aLimit[SQLITE_LIMIT_EXPR_DEPTH];
666 if( nHeight>mxHeight ){
667 sqlite3ErrorMsg(pParse,
668 "Expression tree is too large (maximum depth %d)", mxHeight
669 );
670 rc = SQLITE_ERROR;
671 }
672 return rc;
673}
674
675/* The following three functions, heightOfExpr(), heightOfExprList()
676** and heightOfSelect(), are used to determine the maximum height
677** of any expression tree referenced by the structure passed as the
678** first argument.
679**
680** If this maximum height is greater than the current value pointed
681** to by pnHeight, the second parameter, then set *pnHeight to that
682** value.
683*/
684static void heightOfExpr(Expr *p, int *pnHeight){
685 if( p ){
686 if( p->nHeight>*pnHeight ){
687 *pnHeight = p->nHeight;
688 }
689 }
690}
691static void heightOfExprList(ExprList *p, int *pnHeight){
692 if( p ){
693 int i;
694 for(i=0; i<p->nExpr; i++){
695 heightOfExpr(p->a[i].pExpr, pnHeight);
696 }
697 }
698}
dan1a3a3082018-01-18 19:00:54 +0000699static void heightOfSelect(Select *pSelect, int *pnHeight){
700 Select *p;
701 for(p=pSelect; p; p=p->pPrior){
danielk19774b5255a2008-06-05 16:47:39 +0000702 heightOfExpr(p->pWhere, pnHeight);
703 heightOfExpr(p->pHaving, pnHeight);
704 heightOfExpr(p->pLimit, pnHeight);
danielk19774b5255a2008-06-05 16:47:39 +0000705 heightOfExprList(p->pEList, pnHeight);
706 heightOfExprList(p->pGroupBy, pnHeight);
707 heightOfExprList(p->pOrderBy, pnHeight);
danielk19774b5255a2008-06-05 16:47:39 +0000708 }
709}
710
711/*
712** Set the Expr.nHeight variable in the structure passed as an
713** argument. An expression with no children, Expr.pList or
714** Expr.pSelect member has a height of 1. Any other expression
715** has a height equal to the maximum height of any other
716** referenced Expr plus one.
drh2308ed32015-02-09 16:09:34 +0000717**
718** Also propagate EP_Propagate flags up from Expr.x.pList to Expr.flags,
719** if appropriate.
danielk19774b5255a2008-06-05 16:47:39 +0000720*/
721static void exprSetHeight(Expr *p){
722 int nHeight = 0;
723 heightOfExpr(p->pLeft, &nHeight);
724 heightOfExpr(p->pRight, &nHeight);
danielk19776ab3a2e2009-02-19 14:39:25 +0000725 if( ExprHasProperty(p, EP_xIsSelect) ){
726 heightOfSelect(p->x.pSelect, &nHeight);
drh2308ed32015-02-09 16:09:34 +0000727 }else if( p->x.pList ){
danielk19776ab3a2e2009-02-19 14:39:25 +0000728 heightOfExprList(p->x.pList, &nHeight);
drh2308ed32015-02-09 16:09:34 +0000729 p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList);
danielk19776ab3a2e2009-02-19 14:39:25 +0000730 }
danielk19774b5255a2008-06-05 16:47:39 +0000731 p->nHeight = nHeight + 1;
732}
733
734/*
735** Set the Expr.nHeight variable using the exprSetHeight() function. If
736** the height is greater than the maximum allowed expression depth,
737** leave an error in pParse.
drh2308ed32015-02-09 16:09:34 +0000738**
739** Also propagate all EP_Propagate flags from the Expr.x.pList into
740** Expr.flags.
danielk19774b5255a2008-06-05 16:47:39 +0000741*/
drh2308ed32015-02-09 16:09:34 +0000742void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p){
drh74893a42015-03-22 10:23:17 +0000743 if( pParse->nErr ) return;
danielk19774b5255a2008-06-05 16:47:39 +0000744 exprSetHeight(p);
drh7d10d5a2008-08-20 16:35:10 +0000745 sqlite3ExprCheckHeight(pParse, p->nHeight);
danielk19774b5255a2008-06-05 16:47:39 +0000746}
747
748/*
749** Return the maximum height of any expression tree referenced
750** by the select statement passed as an argument.
751*/
752int sqlite3SelectExprHeight(Select *p){
753 int nHeight = 0;
754 heightOfSelect(p, &nHeight);
755 return nHeight;
756}
drh2308ed32015-02-09 16:09:34 +0000757#else /* ABOVE: Height enforcement enabled. BELOW: Height enforcement off */
758/*
759** Propagate all EP_Propagate flags from the Expr.x.pList into
760** Expr.flags.
761*/
762void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p){
763 if( p && p->x.pList && !ExprHasProperty(p, EP_xIsSelect) ){
764 p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList);
765 }
766}
767#define exprSetHeight(y)
danielk19774b5255a2008-06-05 16:47:39 +0000768#endif /* SQLITE_MAX_EXPR_DEPTH>0 */
769
drhbe5c89a2004-07-26 00:31:09 +0000770/*
drhb7916a72009-05-27 10:31:29 +0000771** This routine is the core allocator for Expr nodes.
772**
drha76b5df2002-02-23 02:32:10 +0000773** Construct a new expression node and return a pointer to it. Memory
drhb7916a72009-05-27 10:31:29 +0000774** for this node and for the pToken argument is a single allocation
775** obtained from sqlite3DbMalloc(). The calling function
drha76b5df2002-02-23 02:32:10 +0000776** is responsible for making sure the node eventually gets freed.
drhb7916a72009-05-27 10:31:29 +0000777**
778** If dequote is true, then the token (if it exists) is dequoted.
drhe792b5b2015-08-23 20:48:29 +0000779** If dequote is false, no dequoting is performed. The deQuote
drhb7916a72009-05-27 10:31:29 +0000780** parameter is ignored if pToken is NULL or if the token does not
781** appear to be quoted. If the quotes were of the form "..." (double-quotes)
782** then the EP_DblQuoted flag is set on the expression node.
drh33e619f2009-05-28 01:00:55 +0000783**
784** Special case: If op==TK_INTEGER and pToken points to a string that
785** can be translated into a 32-bit integer, then the token is not
786** stored in u.zToken. Instead, the integer values is written
787** into u.iValue and the EP_IntValue flag is set. No extra storage
788** is allocated to hold the integer text and the dequote flag is ignored.
drha76b5df2002-02-23 02:32:10 +0000789*/
drhb7916a72009-05-27 10:31:29 +0000790Expr *sqlite3ExprAlloc(
drhcca8a4a2016-10-03 12:56:48 +0000791 sqlite3 *db, /* Handle for sqlite3DbMallocRawNN() */
drh17435752007-08-16 04:30:38 +0000792 int op, /* Expression opcode */
drhb7916a72009-05-27 10:31:29 +0000793 const Token *pToken, /* Token argument. Might be NULL */
794 int dequote /* True to dequote */
drh17435752007-08-16 04:30:38 +0000795){
drha76b5df2002-02-23 02:32:10 +0000796 Expr *pNew;
drh33e619f2009-05-28 01:00:55 +0000797 int nExtra = 0;
shanecf697392009-06-01 16:53:09 +0000798 int iValue = 0;
danielk1977fc976062007-05-10 10:46:56 +0000799
drh575fad62016-02-05 13:38:36 +0000800 assert( db!=0 );
drhb7916a72009-05-27 10:31:29 +0000801 if( pToken ){
drh33e619f2009-05-28 01:00:55 +0000802 if( op!=TK_INTEGER || pToken->z==0
803 || sqlite3GetInt32(pToken->z, &iValue)==0 ){
804 nExtra = pToken->n+1;
drhd50ffc42011-03-08 02:38:28 +0000805 assert( iValue>=0 );
drh33e619f2009-05-28 01:00:55 +0000806 }
drhb7916a72009-05-27 10:31:29 +0000807 }
drh575fad62016-02-05 13:38:36 +0000808 pNew = sqlite3DbMallocRawNN(db, sizeof(Expr)+nExtra);
drhb7916a72009-05-27 10:31:29 +0000809 if( pNew ){
drhca3862d2016-01-08 12:46:39 +0000810 memset(pNew, 0, sizeof(Expr));
drhb7916a72009-05-27 10:31:29 +0000811 pNew->op = (u8)op;
812 pNew->iAgg = -1;
813 if( pToken ){
drh33e619f2009-05-28 01:00:55 +0000814 if( nExtra==0 ){
drhad317272019-04-19 16:21:51 +0000815 pNew->flags |= EP_IntValue|EP_Leaf|(iValue?EP_IsTrue:EP_IsFalse);
drh33e619f2009-05-28 01:00:55 +0000816 pNew->u.iValue = iValue;
817 }else{
drh33e619f2009-05-28 01:00:55 +0000818 pNew->u.zToken = (char*)&pNew[1];
drhb07028f2011-10-14 21:49:18 +0000819 assert( pToken->z!=0 || pToken->n==0 );
820 if( pToken->n ) memcpy(pNew->u.zToken, pToken->z, pToken->n);
drh33e619f2009-05-28 01:00:55 +0000821 pNew->u.zToken[pToken->n] = 0;
drh244b9d62016-04-11 19:01:08 +0000822 if( dequote && sqlite3Isquote(pNew->u.zToken[0]) ){
drh51d35b02019-01-11 13:32:23 +0000823 sqlite3DequoteExpr(pNew);
drh33e619f2009-05-28 01:00:55 +0000824 }
drhb7916a72009-05-27 10:31:29 +0000825 }
826 }
827#if SQLITE_MAX_EXPR_DEPTH>0
828 pNew->nHeight = 1;
829#endif
830 }
drha76b5df2002-02-23 02:32:10 +0000831 return pNew;
832}
833
834/*
drhb7916a72009-05-27 10:31:29 +0000835** Allocate a new expression node from a zero-terminated token that has
836** already been dequoted.
837*/
838Expr *sqlite3Expr(
839 sqlite3 *db, /* Handle for sqlite3DbMallocZero() (may be null) */
840 int op, /* Expression opcode */
841 const char *zToken /* Token argument. Might be NULL */
842){
843 Token x;
844 x.z = zToken;
drhb40f06c2017-08-21 02:20:57 +0000845 x.n = sqlite3Strlen30(zToken);
drhb7916a72009-05-27 10:31:29 +0000846 return sqlite3ExprAlloc(db, op, &x, 0);
847}
848
849/*
850** Attach subtrees pLeft and pRight to the Expr node pRoot.
851**
852** If pRoot==NULL that means that a memory allocation error has occurred.
853** In that case, delete the subtrees pLeft and pRight.
854*/
855void sqlite3ExprAttachSubtrees(
856 sqlite3 *db,
857 Expr *pRoot,
858 Expr *pLeft,
859 Expr *pRight
860){
861 if( pRoot==0 ){
862 assert( db->mallocFailed );
863 sqlite3ExprDelete(db, pLeft);
864 sqlite3ExprDelete(db, pRight);
865 }else{
866 if( pRight ){
867 pRoot->pRight = pRight;
drh885a5b02015-02-09 15:21:36 +0000868 pRoot->flags |= EP_Propagate & pRight->flags;
drhb7916a72009-05-27 10:31:29 +0000869 }
870 if( pLeft ){
871 pRoot->pLeft = pLeft;
drh885a5b02015-02-09 15:21:36 +0000872 pRoot->flags |= EP_Propagate & pLeft->flags;
drhb7916a72009-05-27 10:31:29 +0000873 }
874 exprSetHeight(pRoot);
875 }
876}
877
878/*
peter.d.reid60ec9142014-09-06 16:39:46 +0000879** Allocate an Expr node which joins as many as two subtrees.
drhb7916a72009-05-27 10:31:29 +0000880**
drhbf664462009-06-19 18:32:54 +0000881** One or both of the subtrees can be NULL. Return a pointer to the new
882** Expr node. Or, if an OOM error occurs, set pParse->db->mallocFailed,
883** free the subtrees and return NULL.
drh206f3d92006-07-11 13:15:08 +0000884*/
drh17435752007-08-16 04:30:38 +0000885Expr *sqlite3PExpr(
886 Parse *pParse, /* Parsing context */
887 int op, /* Expression opcode */
888 Expr *pLeft, /* Left operand */
drhabfd35e2016-12-06 22:47:23 +0000889 Expr *pRight /* Right operand */
drh17435752007-08-16 04:30:38 +0000890){
drh5fb52ca2012-03-31 02:34:35 +0000891 Expr *p;
drhd5c851c2019-04-19 13:38:34 +0000892 p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr));
893 if( p ){
894 memset(p, 0, sizeof(Expr));
895 p->op = op & 0xff;
896 p->iAgg = -1;
drh5fb52ca2012-03-31 02:34:35 +0000897 sqlite3ExprAttachSubtrees(pParse->db, p, pLeft, pRight);
dan2b359bd2010-10-28 11:31:23 +0000898 sqlite3ExprCheckHeight(pParse, p->nHeight);
drhd5c851c2019-04-19 13:38:34 +0000899 }else{
900 sqlite3ExprDelete(pParse->db, pLeft);
901 sqlite3ExprDelete(pParse->db, pRight);
dan2b359bd2010-10-28 11:31:23 +0000902 }
drh4e0cff62004-11-05 05:10:28 +0000903 return p;
904}
905
906/*
drh08de4f72016-04-11 01:06:47 +0000907** Add pSelect to the Expr.x.pSelect field. Or, if pExpr is NULL (due
908** do a memory allocation failure) then delete the pSelect object.
909*/
910void sqlite3PExprAddSelect(Parse *pParse, Expr *pExpr, Select *pSelect){
911 if( pExpr ){
912 pExpr->x.pSelect = pSelect;
913 ExprSetProperty(pExpr, EP_xIsSelect|EP_Subquery);
914 sqlite3ExprSetHeightAndFlags(pParse, pExpr);
915 }else{
916 assert( pParse->db->mallocFailed );
917 sqlite3SelectDelete(pParse->db, pSelect);
918 }
919}
920
921
922/*
drh91bb0ee2004-09-01 03:06:34 +0000923** Join two expressions using an AND operator. If either expression is
924** NULL, then just return the other expression.
drh5fb52ca2012-03-31 02:34:35 +0000925**
926** If one side or the other of the AND is known to be false, then instead
927** of returning an AND expression, just return a constant expression with
928** a value of false.
drh91bb0ee2004-09-01 03:06:34 +0000929*/
drhd5c851c2019-04-19 13:38:34 +0000930Expr *sqlite3ExprAnd(Parse *pParse, Expr *pLeft, Expr *pRight){
931 sqlite3 *db = pParse->db;
932 if( pLeft==0 ){
drh91bb0ee2004-09-01 03:06:34 +0000933 return pRight;
934 }else if( pRight==0 ){
935 return pLeft;
dan2b6e6702019-12-30 06:55:31 +0000936 }else if( (ExprAlwaysFalse(pLeft) || ExprAlwaysFalse(pRight))
937 && !IN_RENAME_OBJECT
938 ){
939 sqlite3ExprDelete(db, pLeft);
940 sqlite3ExprDelete(db, pRight);
drh5776ee52019-09-23 12:38:10 +0000941 return sqlite3Expr(db, TK_INTEGER, "0");
drh91bb0ee2004-09-01 03:06:34 +0000942 }else{
drhd5c851c2019-04-19 13:38:34 +0000943 return sqlite3PExpr(pParse, TK_AND, pLeft, pRight);
drha76b5df2002-02-23 02:32:10 +0000944 }
945}
946
947/*
948** Construct a new expression node for a function with multiple
949** arguments.
950*/
drh954733b2018-07-27 23:33:16 +0000951Expr *sqlite3ExprFunction(
952 Parse *pParse, /* Parsing context */
953 ExprList *pList, /* Argument list */
954 Token *pToken, /* Name of the function */
955 int eDistinct /* SF_Distinct or SF_ALL or 0 */
956){
drha76b5df2002-02-23 02:32:10 +0000957 Expr *pNew;
drh633e6d52008-07-28 19:34:53 +0000958 sqlite3 *db = pParse->db;
danielk19774b202ae2006-01-23 05:50:58 +0000959 assert( pToken );
drhb7916a72009-05-27 10:31:29 +0000960 pNew = sqlite3ExprAlloc(db, TK_FUNCTION, pToken, 1);
drha76b5df2002-02-23 02:32:10 +0000961 if( pNew==0 ){
drhd9da78a2009-03-24 15:08:09 +0000962 sqlite3ExprListDelete(db, pList); /* Avoid memory leak when malloc fails */
drha76b5df2002-02-23 02:32:10 +0000963 return 0;
964 }
drh954733b2018-07-27 23:33:16 +0000965 if( pList && pList->nExpr > pParse->db->aLimit[SQLITE_LIMIT_FUNCTION_ARG] ){
966 sqlite3ErrorMsg(pParse, "too many arguments on function %T", pToken);
967 }
danielk19776ab3a2e2009-02-19 14:39:25 +0000968 pNew->x.pList = pList;
drhfca23552017-10-28 20:51:54 +0000969 ExprSetProperty(pNew, EP_HasFunc);
danielk19776ab3a2e2009-02-19 14:39:25 +0000970 assert( !ExprHasProperty(pNew, EP_xIsSelect) );
drh2308ed32015-02-09 16:09:34 +0000971 sqlite3ExprSetHeightAndFlags(pParse, pNew);
drh954733b2018-07-27 23:33:16 +0000972 if( eDistinct==SF_Distinct ) ExprSetProperty(pNew, EP_Distinct);
drha76b5df2002-02-23 02:32:10 +0000973 return pNew;
974}
975
976/*
drhfa6bc002004-09-07 16:19:52 +0000977** Assign a variable number to an expression that encodes a wildcard
978** in the original SQL statement.
979**
980** Wildcards consisting of a single "?" are assigned the next sequential
981** variable number.
982**
983** Wildcards of the form "?nnn" are assigned the number "nnn". We make
drh9bf755c2016-12-23 03:59:31 +0000984** sure "nnn" is not too big to avoid a denial of service attack when
drhfa6bc002004-09-07 16:19:52 +0000985** the SQL statement comes from an external source.
986**
drh51f49f12009-05-21 20:41:32 +0000987** Wildcards of the form ":aaa", "@aaa", or "$aaa" are assigned the same number
drhfa6bc002004-09-07 16:19:52 +0000988** as the previous instance of the same wildcard. Or if this is the first
peter.d.reid60ec9142014-09-06 16:39:46 +0000989** instance of the wildcard, the next sequential variable number is
drhfa6bc002004-09-07 16:19:52 +0000990** assigned.
991*/
drhde25a882016-10-03 15:28:24 +0000992void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr, u32 n){
drh17435752007-08-16 04:30:38 +0000993 sqlite3 *db = pParse->db;
drhb7916a72009-05-27 10:31:29 +0000994 const char *z;
drhf326d662016-12-23 13:30:53 +0000995 ynVar x;
drh17435752007-08-16 04:30:38 +0000996
drhfa6bc002004-09-07 16:19:52 +0000997 if( pExpr==0 ) return;
drhc5cd1242013-09-12 16:50:49 +0000998 assert( !ExprHasProperty(pExpr, EP_IntValue|EP_Reduced|EP_TokenOnly) );
drh33e619f2009-05-28 01:00:55 +0000999 z = pExpr->u.zToken;
drhb7916a72009-05-27 10:31:29 +00001000 assert( z!=0 );
1001 assert( z[0]!=0 );
mistachkinb1ed7172017-04-14 14:50:34 +00001002 assert( n==(u32)sqlite3Strlen30(z) );
drhb7916a72009-05-27 10:31:29 +00001003 if( z[1]==0 ){
drhfa6bc002004-09-07 16:19:52 +00001004 /* Wildcard of the form "?". Assign the next variable number */
drhb7916a72009-05-27 10:31:29 +00001005 assert( z[0]=='?' );
drhf326d662016-12-23 13:30:53 +00001006 x = (ynVar)(++pParse->nVar);
drhfa6bc002004-09-07 16:19:52 +00001007 }else{
drhf326d662016-12-23 13:30:53 +00001008 int doAdd = 0;
drh124c0b42011-06-01 18:15:55 +00001009 if( z[0]=='?' ){
1010 /* Wildcard of the form "?nnn". Convert "nnn" to an integer and
1011 ** use it as the variable number */
1012 i64 i;
drh18814df2017-01-31 03:52:34 +00001013 int bOk;
1014 if( n==2 ){ /*OPTIMIZATION-IF-TRUE*/
1015 i = z[1]-'0'; /* The common case of ?N for a single digit N */
1016 bOk = 1;
1017 }else{
1018 bOk = 0==sqlite3Atoi64(&z[1], &i, n-1, SQLITE_UTF8);
1019 }
drh124c0b42011-06-01 18:15:55 +00001020 testcase( i==0 );
1021 testcase( i==1 );
1022 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 );
1023 testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] );
1024 if( bOk==0 || i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
1025 sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
1026 db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]);
drhc9b39282016-10-03 16:33:14 +00001027 return;
drhfa6bc002004-09-07 16:19:52 +00001028 }
drh8e74e7b2017-01-31 12:41:48 +00001029 x = (ynVar)i;
drhf326d662016-12-23 13:30:53 +00001030 if( x>pParse->nVar ){
1031 pParse->nVar = (int)x;
1032 doAdd = 1;
1033 }else if( sqlite3VListNumToName(pParse->pVList, x)==0 ){
1034 doAdd = 1;
drh124c0b42011-06-01 18:15:55 +00001035 }
1036 }else{
1037 /* Wildcards like ":aaa", "$aaa" or "@aaa". Reuse the same variable
1038 ** number as the prior appearance of the same name, or if the name
1039 ** has never appeared before, reuse the same variable number
1040 */
drh9bf755c2016-12-23 03:59:31 +00001041 x = (ynVar)sqlite3VListNameToNum(pParse->pVList, z, n);
1042 if( x==0 ){
1043 x = (ynVar)(++pParse->nVar);
drhf326d662016-12-23 13:30:53 +00001044 doAdd = 1;
drh124c0b42011-06-01 18:15:55 +00001045 }
drhfa6bc002004-09-07 16:19:52 +00001046 }
drhf326d662016-12-23 13:30:53 +00001047 if( doAdd ){
1048 pParse->pVList = sqlite3VListAdd(db, pParse->pVList, z, n, x);
1049 }
1050 }
1051 pExpr->iColumn = x;
1052 if( x>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
danielk1977832b2662007-05-09 11:37:22 +00001053 sqlite3ErrorMsg(pParse, "too many SQL variables");
1054 }
drhfa6bc002004-09-07 16:19:52 +00001055}
1056
1057/*
danf6963f92009-11-23 14:39:14 +00001058** Recursively delete an expression tree.
drha2e00042002-01-22 03:13:42 +00001059*/
drh4f0010b2016-04-11 14:49:39 +00001060static SQLITE_NOINLINE void sqlite3ExprDeleteNN(sqlite3 *db, Expr *p){
1061 assert( p!=0 );
drhd50ffc42011-03-08 02:38:28 +00001062 /* Sanity check: Assert that the IntValue is non-negative if it exists */
1063 assert( !ExprHasProperty(p, EP_IntValue) || p->u.iValue>=0 );
drheda079c2018-09-20 19:02:15 +00001064
1065 assert( !ExprHasProperty(p, EP_WinFunc) || p->y.pWin!=0 || db->mallocFailed );
1066 assert( p->op!=TK_FUNCTION || ExprHasProperty(p, EP_TokenOnly|EP_Reduced)
dan4f9adee2019-07-13 16:22:50 +00001067 || p->y.pWin==0 || ExprHasProperty(p, EP_WinFunc) );
drh209bc522016-09-23 21:36:24 +00001068#ifdef SQLITE_DEBUG
1069 if( ExprHasProperty(p, EP_Leaf) && !ExprHasProperty(p, EP_TokenOnly) ){
1070 assert( p->pLeft==0 );
1071 assert( p->pRight==0 );
1072 assert( p->x.pSelect==0 );
1073 }
1074#endif
1075 if( !ExprHasProperty(p, (EP_TokenOnly|EP_Leaf)) ){
drhc5cd1242013-09-12 16:50:49 +00001076 /* The Expr.x union is never used at the same time as Expr.pRight */
1077 assert( p->x.pList==0 || p->pRight==0 );
drh4910a762016-09-03 01:46:15 +00001078 if( p->pLeft && p->op!=TK_SELECT_COLUMN ) sqlite3ExprDeleteNN(db, p->pLeft);
drhd1086672017-07-07 13:59:34 +00001079 if( p->pRight ){
dan4f9adee2019-07-13 16:22:50 +00001080 assert( !ExprHasProperty(p, EP_WinFunc) );
drhd1086672017-07-07 13:59:34 +00001081 sqlite3ExprDeleteNN(db, p->pRight);
1082 }else if( ExprHasProperty(p, EP_xIsSelect) ){
dan4f9adee2019-07-13 16:22:50 +00001083 assert( !ExprHasProperty(p, EP_WinFunc) );
danielk19776ab3a2e2009-02-19 14:39:25 +00001084 sqlite3SelectDelete(db, p->x.pSelect);
1085 }else{
1086 sqlite3ExprListDelete(db, p->x.pList);
dan6ba7ab02019-07-02 11:56:47 +00001087#ifndef SQLITE_OMIT_WINDOWFUNC
dan4f9adee2019-07-13 16:22:50 +00001088 if( ExprHasProperty(p, EP_WinFunc) ){
1089 sqlite3WindowDelete(db, p->y.pWin);
dan8117f112019-07-10 20:16:53 +00001090 }
dan6ba7ab02019-07-02 11:56:47 +00001091#endif
dan8117f112019-07-10 20:16:53 +00001092 }
danielk19776ab3a2e2009-02-19 14:39:25 +00001093 }
drh209bc522016-09-23 21:36:24 +00001094 if( ExprHasProperty(p, EP_MemToken) ) sqlite3DbFree(db, p->u.zToken);
drh33e619f2009-05-28 01:00:55 +00001095 if( !ExprHasProperty(p, EP_Static) ){
drhdbd6a7d2017-04-05 12:39:49 +00001096 sqlite3DbFreeNN(db, p);
drh33e619f2009-05-28 01:00:55 +00001097 }
drha2e00042002-01-22 03:13:42 +00001098}
drh4f0010b2016-04-11 14:49:39 +00001099void sqlite3ExprDelete(sqlite3 *db, Expr *p){
1100 if( p ) sqlite3ExprDeleteNN(db, p);
1101}
drha2e00042002-01-22 03:13:42 +00001102
drh8e34e402019-06-11 10:43:56 +00001103/* Invoke sqlite3RenameExprUnmap() and sqlite3ExprDelete() on the
1104** expression.
1105*/
1106void sqlite3ExprUnmapAndDelete(Parse *pParse, Expr *p){
1107 if( p ){
1108 if( IN_RENAME_OBJECT ){
1109 sqlite3RenameExprUnmap(pParse, p);
1110 }
1111 sqlite3ExprDeleteNN(pParse->db, p);
1112 }
1113}
1114
drhd2687b72005-08-12 22:56:09 +00001115/*
danielk19776ab3a2e2009-02-19 14:39:25 +00001116** Return the number of bytes allocated for the expression structure
1117** passed as the first argument. This is always one of EXPR_FULLSIZE,
1118** EXPR_REDUCEDSIZE or EXPR_TOKENONLYSIZE.
1119*/
1120static int exprStructSize(Expr *p){
1121 if( ExprHasProperty(p, EP_TokenOnly) ) return EXPR_TOKENONLYSIZE;
danielk19776ab3a2e2009-02-19 14:39:25 +00001122 if( ExprHasProperty(p, EP_Reduced) ) return EXPR_REDUCEDSIZE;
1123 return EXPR_FULLSIZE;
1124}
1125
1126/*
drh33e619f2009-05-28 01:00:55 +00001127** The dupedExpr*Size() routines each return the number of bytes required
1128** to store a copy of an expression or expression tree. They differ in
1129** how much of the tree is measured.
1130**
1131** dupedExprStructSize() Size of only the Expr structure
1132** dupedExprNodeSize() Size of Expr + space for token
1133** dupedExprSize() Expr + token + subtree components
1134**
1135***************************************************************************
1136**
1137** The dupedExprStructSize() function returns two values OR-ed together:
1138** (1) the space required for a copy of the Expr structure only and
1139** (2) the EP_xxx flags that indicate what the structure size should be.
1140** The return values is always one of:
1141**
1142** EXPR_FULLSIZE
1143** EXPR_REDUCEDSIZE | EP_Reduced
1144** EXPR_TOKENONLYSIZE | EP_TokenOnly
1145**
1146** The size of the structure can be found by masking the return value
1147** of this routine with 0xfff. The flags can be found by masking the
1148** return value with EP_Reduced|EP_TokenOnly.
1149**
1150** Note that with flags==EXPRDUP_REDUCE, this routines works on full-size
1151** (unreduced) Expr objects as they or originally constructed by the parser.
1152** During expression analysis, extra information is computed and moved into
danc95f38d2018-06-18 20:34:43 +00001153** later parts of the Expr object and that extra information might get chopped
drh33e619f2009-05-28 01:00:55 +00001154** off if the expression is reduced. Note also that it does not work to
peter.d.reid60ec9142014-09-06 16:39:46 +00001155** make an EXPRDUP_REDUCE copy of a reduced expression. It is only legal
drh33e619f2009-05-28 01:00:55 +00001156** to reduce a pristine expression tree from the parser. The implementation
1157** of dupedExprStructSize() contain multiple assert() statements that attempt
1158** to enforce this constraint.
danielk19776ab3a2e2009-02-19 14:39:25 +00001159*/
1160static int dupedExprStructSize(Expr *p, int flags){
1161 int nSize;
drh33e619f2009-05-28 01:00:55 +00001162 assert( flags==EXPRDUP_REDUCE || flags==0 ); /* Only one flag value allowed */
drhaecd8022013-09-13 18:15:15 +00001163 assert( EXPR_FULLSIZE<=0xfff );
1164 assert( (0xfff & (EP_Reduced|EP_TokenOnly))==0 );
dan67a9b8e2018-06-22 20:51:35 +00001165 if( 0==flags || p->op==TK_SELECT_COLUMN
1166#ifndef SQLITE_OMIT_WINDOWFUNC
drheda079c2018-09-20 19:02:15 +00001167 || ExprHasProperty(p, EP_WinFunc)
dan67a9b8e2018-06-22 20:51:35 +00001168#endif
1169 ){
danielk19776ab3a2e2009-02-19 14:39:25 +00001170 nSize = EXPR_FULLSIZE;
danielk19776ab3a2e2009-02-19 14:39:25 +00001171 }else{
drhc5cd1242013-09-12 16:50:49 +00001172 assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) );
drh33e619f2009-05-28 01:00:55 +00001173 assert( !ExprHasProperty(p, EP_FromJoin) );
drhc5cd1242013-09-12 16:50:49 +00001174 assert( !ExprHasProperty(p, EP_MemToken) );
drhebb6a652013-09-12 23:42:22 +00001175 assert( !ExprHasProperty(p, EP_NoReduce) );
drhaecd8022013-09-13 18:15:15 +00001176 if( p->pLeft || p->x.pList ){
drh33e619f2009-05-28 01:00:55 +00001177 nSize = EXPR_REDUCEDSIZE | EP_Reduced;
1178 }else{
drhaecd8022013-09-13 18:15:15 +00001179 assert( p->pRight==0 );
drh33e619f2009-05-28 01:00:55 +00001180 nSize = EXPR_TOKENONLYSIZE | EP_TokenOnly;
1181 }
danielk19776ab3a2e2009-02-19 14:39:25 +00001182 }
1183 return nSize;
1184}
1185
1186/*
drh33e619f2009-05-28 01:00:55 +00001187** This function returns the space in bytes required to store the copy
1188** of the Expr structure and a copy of the Expr.u.zToken string (if that
1189** string is defined.)
danielk19776ab3a2e2009-02-19 14:39:25 +00001190*/
1191static int dupedExprNodeSize(Expr *p, int flags){
drh33e619f2009-05-28 01:00:55 +00001192 int nByte = dupedExprStructSize(p, flags) & 0xfff;
1193 if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
drh7301e772018-10-31 20:52:00 +00001194 nByte += sqlite3Strlen30NN(p->u.zToken)+1;
danielk19776ab3a2e2009-02-19 14:39:25 +00001195 }
danielk1977bc739712009-03-23 04:33:32 +00001196 return ROUND8(nByte);
danielk19776ab3a2e2009-02-19 14:39:25 +00001197}
1198
1199/*
1200** Return the number of bytes required to create a duplicate of the
1201** expression passed as the first argument. The second argument is a
1202** mask containing EXPRDUP_XXX flags.
1203**
1204** The value returned includes space to create a copy of the Expr struct
drh33e619f2009-05-28 01:00:55 +00001205** itself and the buffer referred to by Expr.u.zToken, if any.
danielk19776ab3a2e2009-02-19 14:39:25 +00001206**
1207** If the EXPRDUP_REDUCE flag is set, then the return value includes
1208** space to duplicate all Expr nodes in the tree formed by Expr.pLeft
1209** and Expr.pRight variables (but not for any structures pointed to or
1210** descended from the Expr.x.pList or Expr.x.pSelect variables).
1211*/
1212static int dupedExprSize(Expr *p, int flags){
1213 int nByte = 0;
1214 if( p ){
1215 nByte = dupedExprNodeSize(p, flags);
1216 if( flags&EXPRDUP_REDUCE ){
drhb7916a72009-05-27 10:31:29 +00001217 nByte += dupedExprSize(p->pLeft, flags) + dupedExprSize(p->pRight, flags);
danielk19776ab3a2e2009-02-19 14:39:25 +00001218 }
1219 }
1220 return nByte;
1221}
1222
1223/*
1224** This function is similar to sqlite3ExprDup(), except that if pzBuffer
1225** is not NULL then *pzBuffer is assumed to point to a buffer large enough
drh33e619f2009-05-28 01:00:55 +00001226** to store the copy of expression p, the copies of p->u.zToken
danielk19776ab3a2e2009-02-19 14:39:25 +00001227** (if applicable), and the copies of the p->pLeft and p->pRight expressions,
peter.d.reid60ec9142014-09-06 16:39:46 +00001228** if any. Before returning, *pzBuffer is set to the first byte past the
danielk19776ab3a2e2009-02-19 14:39:25 +00001229** portion of the buffer copied into by this function.
1230*/
drh3c194692016-04-11 16:43:43 +00001231static Expr *exprDup(sqlite3 *db, Expr *p, int dupFlags, u8 **pzBuffer){
1232 Expr *pNew; /* Value to return */
1233 u8 *zAlloc; /* Memory space from which to build Expr object */
1234 u32 staticFlag; /* EP_Static if space not obtained from malloc */
1235
drh575fad62016-02-05 13:38:36 +00001236 assert( db!=0 );
drh3c194692016-04-11 16:43:43 +00001237 assert( p );
1238 assert( dupFlags==0 || dupFlags==EXPRDUP_REDUCE );
1239 assert( pzBuffer==0 || dupFlags==EXPRDUP_REDUCE );
danielk19776ab3a2e2009-02-19 14:39:25 +00001240
drh3c194692016-04-11 16:43:43 +00001241 /* Figure out where to write the new Expr structure. */
1242 if( pzBuffer ){
1243 zAlloc = *pzBuffer;
1244 staticFlag = EP_Static;
1245 }else{
1246 zAlloc = sqlite3DbMallocRawNN(db, dupedExprSize(p, dupFlags));
1247 staticFlag = 0;
1248 }
1249 pNew = (Expr *)zAlloc;
danielk19776ab3a2e2009-02-19 14:39:25 +00001250
drh3c194692016-04-11 16:43:43 +00001251 if( pNew ){
1252 /* Set nNewSize to the size allocated for the structure pointed to
1253 ** by pNew. This is either EXPR_FULLSIZE, EXPR_REDUCEDSIZE or
1254 ** EXPR_TOKENONLYSIZE. nToken is set to the number of bytes consumed
1255 ** by the copy of the p->u.zToken string (if any).
1256 */
1257 const unsigned nStructSize = dupedExprStructSize(p, dupFlags);
1258 const int nNewSize = nStructSize & 0xfff;
1259 int nToken;
1260 if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
1261 nToken = sqlite3Strlen30(p->u.zToken) + 1;
danielk19776ab3a2e2009-02-19 14:39:25 +00001262 }else{
drh3c194692016-04-11 16:43:43 +00001263 nToken = 0;
danielk19776ab3a2e2009-02-19 14:39:25 +00001264 }
drh3c194692016-04-11 16:43:43 +00001265 if( dupFlags ){
1266 assert( ExprHasProperty(p, EP_Reduced)==0 );
1267 memcpy(zAlloc, p, nNewSize);
1268 }else{
1269 u32 nSize = (u32)exprStructSize(p);
1270 memcpy(zAlloc, p, nSize);
1271 if( nSize<EXPR_FULLSIZE ){
1272 memset(&zAlloc[nSize], 0, EXPR_FULLSIZE-nSize);
1273 }
1274 }
danielk19776ab3a2e2009-02-19 14:39:25 +00001275
drh3c194692016-04-11 16:43:43 +00001276 /* Set the EP_Reduced, EP_TokenOnly, and EP_Static flags appropriately. */
1277 pNew->flags &= ~(EP_Reduced|EP_TokenOnly|EP_Static|EP_MemToken);
1278 pNew->flags |= nStructSize & (EP_Reduced|EP_TokenOnly);
1279 pNew->flags |= staticFlag;
1280
1281 /* Copy the p->u.zToken string, if any. */
1282 if( nToken ){
1283 char *zToken = pNew->u.zToken = (char*)&zAlloc[nNewSize];
1284 memcpy(zToken, p->u.zToken, nToken);
1285 }
1286
drh209bc522016-09-23 21:36:24 +00001287 if( 0==((p->flags|pNew->flags) & (EP_TokenOnly|EP_Leaf)) ){
drh3c194692016-04-11 16:43:43 +00001288 /* Fill in the pNew->x.pSelect or pNew->x.pList member. */
1289 if( ExprHasProperty(p, EP_xIsSelect) ){
1290 pNew->x.pSelect = sqlite3SelectDup(db, p->x.pSelect, dupFlags);
drh33e619f2009-05-28 01:00:55 +00001291 }else{
drh3c194692016-04-11 16:43:43 +00001292 pNew->x.pList = sqlite3ExprListDup(db, p->x.pList, dupFlags);
drh33e619f2009-05-28 01:00:55 +00001293 }
drh3c194692016-04-11 16:43:43 +00001294 }
1295
1296 /* Fill in pNew->pLeft and pNew->pRight. */
dan4f9adee2019-07-13 16:22:50 +00001297 if( ExprHasProperty(pNew, EP_Reduced|EP_TokenOnly|EP_WinFunc) ){
drh53988062018-09-24 15:39:30 +00001298 zAlloc += dupedExprNodeSize(p, dupFlags);
drh209bc522016-09-23 21:36:24 +00001299 if( !ExprHasProperty(pNew, EP_TokenOnly|EP_Leaf) ){
drh3c194692016-04-11 16:43:43 +00001300 pNew->pLeft = p->pLeft ?
1301 exprDup(db, p->pLeft, EXPRDUP_REDUCE, &zAlloc) : 0;
1302 pNew->pRight = p->pRight ?
1303 exprDup(db, p->pRight, EXPRDUP_REDUCE, &zAlloc) : 0;
danielk19776ab3a2e2009-02-19 14:39:25 +00001304 }
dan67a9b8e2018-06-22 20:51:35 +00001305#ifndef SQLITE_OMIT_WINDOWFUNC
drheda079c2018-09-20 19:02:15 +00001306 if( ExprHasProperty(p, EP_WinFunc) ){
1307 pNew->y.pWin = sqlite3WindowDup(db, pNew, p->y.pWin);
1308 assert( ExprHasProperty(pNew, EP_WinFunc) );
dane2f781b2018-05-17 19:24:08 +00001309 }
dan67a9b8e2018-06-22 20:51:35 +00001310#endif /* SQLITE_OMIT_WINDOWFUNC */
drh53988062018-09-24 15:39:30 +00001311 if( pzBuffer ){
1312 *pzBuffer = zAlloc;
1313 }
1314 }else{
drh209bc522016-09-23 21:36:24 +00001315 if( !ExprHasProperty(p, EP_TokenOnly|EP_Leaf) ){
drh98542602016-08-20 17:00:16 +00001316 if( pNew->op==TK_SELECT_COLUMN ){
1317 pNew->pLeft = p->pLeft;
drh47073f62017-01-02 22:36:32 +00001318 assert( p->iColumn==0 || p->pRight==0 );
1319 assert( p->pRight==0 || p->pRight==p->pLeft );
drh98542602016-08-20 17:00:16 +00001320 }else{
1321 pNew->pLeft = sqlite3ExprDup(db, p->pLeft, 0);
1322 }
drh3c194692016-04-11 16:43:43 +00001323 pNew->pRight = sqlite3ExprDup(db, p->pRight, 0);
danielk19776ab3a2e2009-02-19 14:39:25 +00001324 }
danielk19776ab3a2e2009-02-19 14:39:25 +00001325 }
1326 }
1327 return pNew;
1328}
1329
1330/*
danbfe31e72014-01-15 14:17:31 +00001331** Create and return a deep copy of the object passed as the second
1332** argument. If an OOM condition is encountered, NULL is returned
1333** and the db->mallocFailed flag set.
1334*/
daneede6a52014-01-15 19:42:23 +00001335#ifndef SQLITE_OMIT_CTE
danbfe31e72014-01-15 14:17:31 +00001336static With *withDup(sqlite3 *db, With *p){
dan4e9119d2014-01-13 15:12:23 +00001337 With *pRet = 0;
1338 if( p ){
drhd4de9f72019-04-14 00:34:20 +00001339 sqlite3_int64 nByte = sizeof(*p) + sizeof(p->a[0]) * (p->nCte-1);
dan4e9119d2014-01-13 15:12:23 +00001340 pRet = sqlite3DbMallocZero(db, nByte);
1341 if( pRet ){
1342 int i;
1343 pRet->nCte = p->nCte;
1344 for(i=0; i<p->nCte; i++){
1345 pRet->a[i].pSelect = sqlite3SelectDup(db, p->a[i].pSelect, 0);
1346 pRet->a[i].pCols = sqlite3ExprListDup(db, p->a[i].pCols, 0);
1347 pRet->a[i].zName = sqlite3DbStrDup(db, p->a[i].zName);
1348 }
1349 }
1350 }
1351 return pRet;
1352}
daneede6a52014-01-15 19:42:23 +00001353#else
1354# define withDup(x,y) 0
1355#endif
dan4e9119d2014-01-13 15:12:23 +00001356
drha8389972018-12-06 22:04:19 +00001357#ifndef SQLITE_OMIT_WINDOWFUNC
1358/*
1359** The gatherSelectWindows() procedure and its helper routine
1360** gatherSelectWindowsCallback() are used to scan all the expressions
1361** an a newly duplicated SELECT statement and gather all of the Window
1362** objects found there, assembling them onto the linked list at Select->pWin.
1363*/
1364static int gatherSelectWindowsCallback(Walker *pWalker, Expr *pExpr){
dan6ba7ab02019-07-02 11:56:47 +00001365 if( pExpr->op==TK_FUNCTION && ExprHasProperty(pExpr, EP_WinFunc) ){
dan75b08212019-07-22 16:20:03 +00001366 Select *pSelect = pWalker->u.pSelect;
1367 Window *pWin = pExpr->y.pWin;
1368 assert( pWin );
dan4f9adee2019-07-13 16:22:50 +00001369 assert( IsWindowFunc(pExpr) );
dane0ae3f62019-07-22 17:28:43 +00001370 assert( pWin->ppThis==0 );
dana3fcc002019-08-15 13:53:22 +00001371 sqlite3WindowLink(pSelect, pWin);
drha8389972018-12-06 22:04:19 +00001372 }
1373 return WRC_Continue;
1374}
drha37b6a52018-12-06 22:12:18 +00001375static int gatherSelectWindowsSelectCallback(Walker *pWalker, Select *p){
1376 return p==pWalker->u.pSelect ? WRC_Continue : WRC_Prune;
1377}
drha8389972018-12-06 22:04:19 +00001378static void gatherSelectWindows(Select *p){
1379 Walker w;
1380 w.xExprCallback = gatherSelectWindowsCallback;
drha37b6a52018-12-06 22:12:18 +00001381 w.xSelectCallback = gatherSelectWindowsSelectCallback;
1382 w.xSelectCallback2 = 0;
drh9c46c662019-02-01 15:06:27 +00001383 w.pParse = 0;
drha8389972018-12-06 22:04:19 +00001384 w.u.pSelect = p;
drha37b6a52018-12-06 22:12:18 +00001385 sqlite3WalkSelect(&w, p);
drha8389972018-12-06 22:04:19 +00001386}
1387#endif
1388
1389
drha76b5df2002-02-23 02:32:10 +00001390/*
drhff78bd22002-02-27 01:47:11 +00001391** The following group of routines make deep copies of expressions,
1392** expression lists, ID lists, and select statements. The copies can
1393** be deleted (by being passed to their respective ...Delete() routines)
1394** without effecting the originals.
1395**
danielk19774adee202004-05-08 08:23:19 +00001396** The expression list, ID, and source lists return by sqlite3ExprListDup(),
1397** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
drhad3cab52002-05-24 02:04:32 +00001398** by subsequent calls to sqlite*ListAppend() routines.
drhff78bd22002-02-27 01:47:11 +00001399**
drhad3cab52002-05-24 02:04:32 +00001400** Any tables that the SrcList might point to are not duplicated.
danielk19776ab3a2e2009-02-19 14:39:25 +00001401**
drhb7916a72009-05-27 10:31:29 +00001402** The flags parameter contains a combination of the EXPRDUP_XXX flags.
danielk19776ab3a2e2009-02-19 14:39:25 +00001403** If the EXPRDUP_REDUCE flag is set, then the structure returned is a
1404** truncated version of the usual Expr structure that will be stored as
1405** part of the in-memory representation of the database schema.
drhff78bd22002-02-27 01:47:11 +00001406*/
danielk19776ab3a2e2009-02-19 14:39:25 +00001407Expr *sqlite3ExprDup(sqlite3 *db, Expr *p, int flags){
drh72ea29d2015-12-08 16:58:45 +00001408 assert( flags==0 || flags==EXPRDUP_REDUCE );
drh3c194692016-04-11 16:43:43 +00001409 return p ? exprDup(db, p, flags, 0) : 0;
drhff78bd22002-02-27 01:47:11 +00001410}
danielk19776ab3a2e2009-02-19 14:39:25 +00001411ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p, int flags){
drhff78bd22002-02-27 01:47:11 +00001412 ExprList *pNew;
drh145716b2004-09-24 12:24:06 +00001413 struct ExprList_item *pItem, *pOldItem;
drhff78bd22002-02-27 01:47:11 +00001414 int i;
drhb1637482017-01-03 00:27:16 +00001415 Expr *pPriorSelectCol = 0;
drh575fad62016-02-05 13:38:36 +00001416 assert( db!=0 );
drhff78bd22002-02-27 01:47:11 +00001417 if( p==0 ) return 0;
drh97258192017-09-17 19:45:28 +00001418 pNew = sqlite3DbMallocRawNN(db, sqlite3DbMallocSize(db, p));
drhff78bd22002-02-27 01:47:11 +00001419 if( pNew==0 ) return 0;
drha19543f2017-09-15 15:17:48 +00001420 pNew->nExpr = p->nExpr;
drh43606172017-04-05 11:32:13 +00001421 pItem = pNew->a;
drh145716b2004-09-24 12:24:06 +00001422 pOldItem = p->a;
1423 for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
danielk19776ab3a2e2009-02-19 14:39:25 +00001424 Expr *pOldExpr = pOldItem->pExpr;
drh47073f62017-01-02 22:36:32 +00001425 Expr *pNewExpr;
drhb5526ea2009-07-16 12:41:05 +00001426 pItem->pExpr = sqlite3ExprDup(db, pOldExpr, flags);
drh47073f62017-01-02 22:36:32 +00001427 if( pOldExpr
1428 && pOldExpr->op==TK_SELECT_COLUMN
1429 && (pNewExpr = pItem->pExpr)!=0
1430 ){
1431 assert( pNewExpr->iColumn==0 || i>0 );
1432 if( pNewExpr->iColumn==0 ){
1433 assert( pOldExpr->pLeft==pOldExpr->pRight );
drhb1637482017-01-03 00:27:16 +00001434 pPriorSelectCol = pNewExpr->pLeft = pNewExpr->pRight;
1435 }else{
1436 assert( i>0 );
1437 assert( pItem[-1].pExpr!=0 );
1438 assert( pNewExpr->iColumn==pItem[-1].pExpr->iColumn+1 );
1439 assert( pPriorSelectCol==pItem[-1].pExpr->pLeft );
1440 pNewExpr->pLeft = pPriorSelectCol;
drh47073f62017-01-02 22:36:32 +00001441 }
1442 }
drh41cee662019-12-12 20:22:34 +00001443 pItem->zEName = sqlite3DbStrDup(db, pOldItem->zEName);
dan6e118922019-08-12 16:36:38 +00001444 pItem->sortFlags = pOldItem->sortFlags;
drhcbb9da32019-12-12 22:11:33 +00001445 pItem->eEName = pOldItem->eEName;
drh3e7bc9c2004-02-21 19:17:17 +00001446 pItem->done = 0;
danae8e45c2019-08-19 19:59:50 +00001447 pItem->bNulls = pOldItem->bNulls;
dan24e25d32018-04-14 18:46:20 +00001448 pItem->bSorterRef = pOldItem->bSorterRef;
drhc2acc4e2013-11-15 18:15:19 +00001449 pItem->u = pOldItem->u;
drhff78bd22002-02-27 01:47:11 +00001450 }
1451 return pNew;
1452}
danielk197793758c82005-01-21 08:13:14 +00001453
1454/*
1455** If cursors, triggers, views and subqueries are all omitted from
1456** the build, then none of the following routines, except for
1457** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes
1458** called with a NULL argument.
1459*/
danielk19776a67fe82005-02-04 04:07:16 +00001460#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \
1461 || !defined(SQLITE_OMIT_SUBQUERY)
danielk19776ab3a2e2009-02-19 14:39:25 +00001462SrcList *sqlite3SrcListDup(sqlite3 *db, SrcList *p, int flags){
drhad3cab52002-05-24 02:04:32 +00001463 SrcList *pNew;
1464 int i;
drh113088e2003-03-20 01:16:58 +00001465 int nByte;
drh575fad62016-02-05 13:38:36 +00001466 assert( db!=0 );
drhad3cab52002-05-24 02:04:32 +00001467 if( p==0 ) return 0;
drh113088e2003-03-20 01:16:58 +00001468 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
drh575fad62016-02-05 13:38:36 +00001469 pNew = sqlite3DbMallocRawNN(db, nByte );
drhad3cab52002-05-24 02:04:32 +00001470 if( pNew==0 ) return 0;
drh4305d102003-07-30 12:34:12 +00001471 pNew->nSrc = pNew->nAlloc = p->nSrc;
drhad3cab52002-05-24 02:04:32 +00001472 for(i=0; i<p->nSrc; i++){
drh4efc4752004-01-16 15:55:37 +00001473 struct SrcList_item *pNewItem = &pNew->a[i];
1474 struct SrcList_item *pOldItem = &p->a[i];
drhed8a3bb2005-06-06 21:19:56 +00001475 Table *pTab;
dan41fb5cd2012-10-04 19:33:00 +00001476 pNewItem->pSchema = pOldItem->pSchema;
drh17435752007-08-16 04:30:38 +00001477 pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase);
1478 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
1479 pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias);
drh8a48b9c2015-08-19 15:20:00 +00001480 pNewItem->fg = pOldItem->fg;
drh4efc4752004-01-16 15:55:37 +00001481 pNewItem->iCursor = pOldItem->iCursor;
drh5b6a9ed2011-09-15 23:58:14 +00001482 pNewItem->addrFillSub = pOldItem->addrFillSub;
1483 pNewItem->regReturn = pOldItem->regReturn;
drh8a48b9c2015-08-19 15:20:00 +00001484 if( pNewItem->fg.isIndexedBy ){
1485 pNewItem->u1.zIndexedBy = sqlite3DbStrDup(db, pOldItem->u1.zIndexedBy);
1486 }
1487 pNewItem->pIBIndex = pOldItem->pIBIndex;
1488 if( pNewItem->fg.isTabFunc ){
1489 pNewItem->u1.pFuncArg =
1490 sqlite3ExprListDup(db, pOldItem->u1.pFuncArg, flags);
1491 }
drhed8a3bb2005-06-06 21:19:56 +00001492 pTab = pNewItem->pTab = pOldItem->pTab;
1493 if( pTab ){
drh79df7782016-12-14 14:07:35 +00001494 pTab->nTabRef++;
danielk1977a1cb1832005-02-12 08:59:55 +00001495 }
danielk19776ab3a2e2009-02-19 14:39:25 +00001496 pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect, flags);
1497 pNewItem->pOn = sqlite3ExprDup(db, pOldItem->pOn, flags);
drh17435752007-08-16 04:30:38 +00001498 pNewItem->pUsing = sqlite3IdListDup(db, pOldItem->pUsing);
danielk19776c18b6e2005-01-30 09:17:58 +00001499 pNewItem->colUsed = pOldItem->colUsed;
drhad3cab52002-05-24 02:04:32 +00001500 }
1501 return pNew;
1502}
drh17435752007-08-16 04:30:38 +00001503IdList *sqlite3IdListDup(sqlite3 *db, IdList *p){
drhff78bd22002-02-27 01:47:11 +00001504 IdList *pNew;
1505 int i;
drh575fad62016-02-05 13:38:36 +00001506 assert( db!=0 );
drhff78bd22002-02-27 01:47:11 +00001507 if( p==0 ) return 0;
drh575fad62016-02-05 13:38:36 +00001508 pNew = sqlite3DbMallocRawNN(db, sizeof(*pNew) );
drhff78bd22002-02-27 01:47:11 +00001509 if( pNew==0 ) return 0;
drh6c535152012-02-02 03:38:30 +00001510 pNew->nId = p->nId;
drh575fad62016-02-05 13:38:36 +00001511 pNew->a = sqlite3DbMallocRawNN(db, p->nId*sizeof(p->a[0]) );
danielk1977d5d56522005-03-16 12:15:20 +00001512 if( pNew->a==0 ){
drhdbd6a7d2017-04-05 12:39:49 +00001513 sqlite3DbFreeNN(db, pNew);
danielk1977d5d56522005-03-16 12:15:20 +00001514 return 0;
1515 }
drh6c535152012-02-02 03:38:30 +00001516 /* Note that because the size of the allocation for p->a[] is not
1517 ** necessarily a power of two, sqlite3IdListAppend() may not be called
1518 ** on the duplicate created by this function. */
drhff78bd22002-02-27 01:47:11 +00001519 for(i=0; i<p->nId; i++){
drh4efc4752004-01-16 15:55:37 +00001520 struct IdList_item *pNewItem = &pNew->a[i];
1521 struct IdList_item *pOldItem = &p->a[i];
drh17435752007-08-16 04:30:38 +00001522 pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
drh4efc4752004-01-16 15:55:37 +00001523 pNewItem->idx = pOldItem->idx;
drhff78bd22002-02-27 01:47:11 +00001524 }
1525 return pNew;
1526}
dana7466202017-02-03 14:44:52 +00001527Select *sqlite3SelectDup(sqlite3 *db, Select *pDup, int flags){
1528 Select *pRet = 0;
1529 Select *pNext = 0;
1530 Select **pp = &pRet;
1531 Select *p;
1532
drh575fad62016-02-05 13:38:36 +00001533 assert( db!=0 );
dana7466202017-02-03 14:44:52 +00001534 for(p=pDup; p; p=p->pPrior){
1535 Select *pNew = sqlite3DbMallocRawNN(db, sizeof(*p) );
1536 if( pNew==0 ) break;
1537 pNew->pEList = sqlite3ExprListDup(db, p->pEList, flags);
1538 pNew->pSrc = sqlite3SrcListDup(db, p->pSrc, flags);
1539 pNew->pWhere = sqlite3ExprDup(db, p->pWhere, flags);
1540 pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags);
1541 pNew->pHaving = sqlite3ExprDup(db, p->pHaving, flags);
1542 pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, flags);
1543 pNew->op = p->op;
1544 pNew->pNext = pNext;
1545 pNew->pPrior = 0;
1546 pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags);
dana7466202017-02-03 14:44:52 +00001547 pNew->iLimit = 0;
1548 pNew->iOffset = 0;
1549 pNew->selFlags = p->selFlags & ~SF_UsesEphemeral;
1550 pNew->addrOpenEphm[0] = -1;
1551 pNew->addrOpenEphm[1] = -1;
1552 pNew->nSelectRow = p->nSelectRow;
1553 pNew->pWith = withDup(db, p->pWith);
dan67a9b8e2018-06-22 20:51:35 +00001554#ifndef SQLITE_OMIT_WINDOWFUNC
dan2e362f92018-05-17 14:26:27 +00001555 pNew->pWin = 0;
danc95f38d2018-06-18 20:34:43 +00001556 pNew->pWinDefn = sqlite3WindowListDup(db, p->pWinDefn);
dan4780b9a2019-08-20 14:43:01 +00001557 if( p->pWin && db->mallocFailed==0 ) gatherSelectWindows(pNew);
dan67a9b8e2018-06-22 20:51:35 +00001558#endif
drhfef37762018-07-10 19:48:35 +00001559 pNew->selId = p->selId;
dana7466202017-02-03 14:44:52 +00001560 *pp = pNew;
1561 pp = &pNew->pPrior;
1562 pNext = pNew;
1563 }
1564
1565 return pRet;
drhff78bd22002-02-27 01:47:11 +00001566}
danielk197793758c82005-01-21 08:13:14 +00001567#else
danielk19776ab3a2e2009-02-19 14:39:25 +00001568Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){
danielk197793758c82005-01-21 08:13:14 +00001569 assert( p==0 );
1570 return 0;
1571}
1572#endif
drhff78bd22002-02-27 01:47:11 +00001573
1574
1575/*
drha76b5df2002-02-23 02:32:10 +00001576** Add a new element to the end of an expression list. If pList is
1577** initially NULL, then create a new expression list.
drhb7916a72009-05-27 10:31:29 +00001578**
drha19543f2017-09-15 15:17:48 +00001579** The pList argument must be either NULL or a pointer to an ExprList
1580** obtained from a prior call to sqlite3ExprListAppend(). This routine
1581** may not be used with an ExprList obtained from sqlite3ExprListDup().
1582** Reason: This routine assumes that the number of slots in pList->a[]
1583** is a power of two. That is true for sqlite3ExprListAppend() returns
1584** but is not necessarily true from the return value of sqlite3ExprListDup().
1585**
drhb7916a72009-05-27 10:31:29 +00001586** If a memory allocation error occurs, the entire list is freed and
1587** NULL is returned. If non-NULL is returned, then it is guaranteed
1588** that the new entry was successfully appended.
drha76b5df2002-02-23 02:32:10 +00001589*/
drh17435752007-08-16 04:30:38 +00001590ExprList *sqlite3ExprListAppend(
1591 Parse *pParse, /* Parsing context */
1592 ExprList *pList, /* List to which to append. Might be NULL */
drhb7916a72009-05-27 10:31:29 +00001593 Expr *pExpr /* Expression to be appended. Might be NULL */
drh17435752007-08-16 04:30:38 +00001594){
drh43606172017-04-05 11:32:13 +00001595 struct ExprList_item *pItem;
drh17435752007-08-16 04:30:38 +00001596 sqlite3 *db = pParse->db;
drh575fad62016-02-05 13:38:36 +00001597 assert( db!=0 );
drha76b5df2002-02-23 02:32:10 +00001598 if( pList==0 ){
drh575fad62016-02-05 13:38:36 +00001599 pList = sqlite3DbMallocRawNN(db, sizeof(ExprList) );
drha76b5df2002-02-23 02:32:10 +00001600 if( pList==0 ){
danielk1977d5d56522005-03-16 12:15:20 +00001601 goto no_mem;
drha76b5df2002-02-23 02:32:10 +00001602 }
drhc263f7c2016-01-18 13:18:54 +00001603 pList->nExpr = 0;
drha19543f2017-09-15 15:17:48 +00001604 }else if( (pList->nExpr & (pList->nExpr-1))==0 ){
drh43606172017-04-05 11:32:13 +00001605 ExprList *pNew;
1606 pNew = sqlite3DbRealloc(db, pList,
drh0aa32312019-04-13 04:01:12 +00001607 sizeof(*pList)+(2*(sqlite3_int64)pList->nExpr-1)*sizeof(pList->a[0]));
drh43606172017-04-05 11:32:13 +00001608 if( pNew==0 ){
danielk1977d5d56522005-03-16 12:15:20 +00001609 goto no_mem;
drha76b5df2002-02-23 02:32:10 +00001610 }
drh43606172017-04-05 11:32:13 +00001611 pList = pNew;
drha76b5df2002-02-23 02:32:10 +00001612 }
drh43606172017-04-05 11:32:13 +00001613 pItem = &pList->a[pList->nExpr++];
drh41cee662019-12-12 20:22:34 +00001614 assert( offsetof(struct ExprList_item,zEName)==sizeof(pItem->pExpr) );
drha8b97932017-05-31 02:58:30 +00001615 assert( offsetof(struct ExprList_item,pExpr)==0 );
drh41cee662019-12-12 20:22:34 +00001616 memset(&pItem->zEName,0,sizeof(*pItem)-offsetof(struct ExprList_item,zEName));
drh43606172017-04-05 11:32:13 +00001617 pItem->pExpr = pExpr;
drha76b5df2002-02-23 02:32:10 +00001618 return pList;
danielk1977d5d56522005-03-16 12:15:20 +00001619
1620no_mem:
1621 /* Avoid leaking memory if malloc has failed. */
drh633e6d52008-07-28 19:34:53 +00001622 sqlite3ExprDelete(db, pExpr);
1623 sqlite3ExprListDelete(db, pList);
danielk1977d5d56522005-03-16 12:15:20 +00001624 return 0;
drha76b5df2002-02-23 02:32:10 +00001625}
1626
1627/*
drh8762ec12016-08-20 01:06:22 +00001628** pColumns and pExpr form a vector assignment which is part of the SET
1629** clause of an UPDATE statement. Like this:
drha1251bc2016-08-20 00:51:37 +00001630**
1631** (a,b,c) = (expr1,expr2,expr3)
1632** Or: (a,b,c) = (SELECT x,y,z FROM ....)
1633**
1634** For each term of the vector assignment, append new entries to the
drhb67343d2017-01-03 11:59:54 +00001635** expression list pList. In the case of a subquery on the RHS, append
drha1251bc2016-08-20 00:51:37 +00001636** TK_SELECT_COLUMN expressions.
1637*/
1638ExprList *sqlite3ExprListAppendVector(
1639 Parse *pParse, /* Parsing context */
1640 ExprList *pList, /* List to which to append. Might be NULL */
1641 IdList *pColumns, /* List of names of LHS of the assignment */
1642 Expr *pExpr /* Vector expression to be appended. Might be NULL */
1643){
1644 sqlite3 *db = pParse->db;
1645 int n;
1646 int i;
drh66860af2016-08-23 18:30:10 +00001647 int iFirst = pList ? pList->nExpr : 0;
drh321e8282016-08-24 17:49:07 +00001648 /* pColumns can only be NULL due to an OOM but an OOM will cause an
1649 ** exit prior to this routine being invoked */
1650 if( NEVER(pColumns==0) ) goto vector_append_error;
drha1251bc2016-08-20 00:51:37 +00001651 if( pExpr==0 ) goto vector_append_error;
drh966e2912017-01-03 02:58:01 +00001652
1653 /* If the RHS is a vector, then we can immediately check to see that
1654 ** the size of the RHS and LHS match. But if the RHS is a SELECT,
1655 ** wildcards ("*") in the result set of the SELECT must be expanded before
1656 ** we can do the size check, so defer the size check until code generation.
1657 */
1658 if( pExpr->op!=TK_SELECT && pColumns->nId!=(n=sqlite3ExprVectorSize(pExpr)) ){
drha1251bc2016-08-20 00:51:37 +00001659 sqlite3ErrorMsg(pParse, "%d columns assigned %d values",
1660 pColumns->nId, n);
1661 goto vector_append_error;
1662 }
drh966e2912017-01-03 02:58:01 +00001663
1664 for(i=0; i<pColumns->nId; i++){
drha1251bc2016-08-20 00:51:37 +00001665 Expr *pSubExpr = sqlite3ExprForVectorField(pParse, pExpr, i);
drh554a9dc2019-08-26 14:18:28 +00001666 assert( pSubExpr!=0 || db->mallocFailed );
1667 assert( pSubExpr==0 || pSubExpr->iTable==0 );
1668 if( pSubExpr==0 ) continue;
1669 pSubExpr->iTable = pColumns->nId;
drha1251bc2016-08-20 00:51:37 +00001670 pList = sqlite3ExprListAppend(pParse, pList, pSubExpr);
1671 if( pList ){
drh66860af2016-08-23 18:30:10 +00001672 assert( pList->nExpr==iFirst+i+1 );
drh41cee662019-12-12 20:22:34 +00001673 pList->a[pList->nExpr-1].zEName = pColumns->a[i].zName;
drha1251bc2016-08-20 00:51:37 +00001674 pColumns->a[i].zName = 0;
1675 }
1676 }
drh966e2912017-01-03 02:58:01 +00001677
drhffe28052017-05-06 18:09:36 +00001678 if( !db->mallocFailed && pExpr->op==TK_SELECT && ALWAYS(pList!=0) ){
drhf4dd26c2017-04-05 11:49:06 +00001679 Expr *pFirst = pList->a[iFirst].pExpr;
1680 assert( pFirst!=0 );
1681 assert( pFirst->op==TK_SELECT_COLUMN );
drh966e2912017-01-03 02:58:01 +00001682
drhf4dd26c2017-04-05 11:49:06 +00001683 /* Store the SELECT statement in pRight so it will be deleted when
1684 ** sqlite3ExprListDelete() is called */
1685 pFirst->pRight = pExpr;
1686 pExpr = 0;
drh966e2912017-01-03 02:58:01 +00001687
drhf4dd26c2017-04-05 11:49:06 +00001688 /* Remember the size of the LHS in iTable so that we can check that
1689 ** the RHS and LHS sizes match during code generation. */
1690 pFirst->iTable = pColumns->nId;
drha1251bc2016-08-20 00:51:37 +00001691 }
1692
1693vector_append_error:
drh8e34e402019-06-11 10:43:56 +00001694 sqlite3ExprUnmapAndDelete(pParse, pExpr);
drha1251bc2016-08-20 00:51:37 +00001695 sqlite3IdListDelete(db, pColumns);
1696 return pList;
1697}
1698
1699/*
drhbc622bc2015-08-24 15:39:42 +00001700** Set the sort order for the last element on the given ExprList.
1701*/
dan6e118922019-08-12 16:36:38 +00001702void sqlite3ExprListSetSortOrder(ExprList *p, int iSortOrder, int eNulls){
dan9105fd52019-08-19 17:26:32 +00001703 struct ExprList_item *pItem;
drhbc622bc2015-08-24 15:39:42 +00001704 if( p==0 ) return;
drhbc622bc2015-08-24 15:39:42 +00001705 assert( p->nExpr>0 );
dan6e118922019-08-12 16:36:38 +00001706
1707 assert( SQLITE_SO_UNDEFINED<0 && SQLITE_SO_ASC==0 && SQLITE_SO_DESC>0 );
1708 assert( iSortOrder==SQLITE_SO_UNDEFINED
1709 || iSortOrder==SQLITE_SO_ASC
1710 || iSortOrder==SQLITE_SO_DESC
1711 );
1712 assert( eNulls==SQLITE_SO_UNDEFINED
1713 || eNulls==SQLITE_SO_ASC
1714 || eNulls==SQLITE_SO_DESC
1715 );
1716
dan9105fd52019-08-19 17:26:32 +00001717 pItem = &p->a[p->nExpr-1];
1718 assert( pItem->bNulls==0 );
1719 if( iSortOrder==SQLITE_SO_UNDEFINED ){
1720 iSortOrder = SQLITE_SO_ASC;
drhbc622bc2015-08-24 15:39:42 +00001721 }
dan9105fd52019-08-19 17:26:32 +00001722 pItem->sortFlags = (u8)iSortOrder;
1723
1724 if( eNulls!=SQLITE_SO_UNDEFINED ){
1725 pItem->bNulls = 1;
1726 if( iSortOrder!=eNulls ){
1727 pItem->sortFlags |= KEYINFO_ORDER_BIGNULL;
1728 }
drhbc622bc2015-08-24 15:39:42 +00001729 }
drhbc622bc2015-08-24 15:39:42 +00001730}
1731
1732/*
drh41cee662019-12-12 20:22:34 +00001733** Set the ExprList.a[].zEName element of the most recently added item
drhb7916a72009-05-27 10:31:29 +00001734** on the expression list.
1735**
1736** pList might be NULL following an OOM error. But pName should never be
1737** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag
1738** is set.
1739*/
1740void sqlite3ExprListSetName(
1741 Parse *pParse, /* Parsing context */
1742 ExprList *pList, /* List to which to add the span. */
1743 Token *pName, /* Name to be added */
1744 int dequote /* True to cause the name to be dequoted */
1745){
1746 assert( pList!=0 || pParse->db->mallocFailed!=0 );
1747 if( pList ){
1748 struct ExprList_item *pItem;
1749 assert( pList->nExpr>0 );
1750 pItem = &pList->a[pList->nExpr-1];
drh41cee662019-12-12 20:22:34 +00001751 assert( pItem->zEName==0 );
drhc4938ea2019-12-13 00:49:42 +00001752 assert( pItem->eEName==ENAME_NAME );
drh41cee662019-12-12 20:22:34 +00001753 pItem->zEName = sqlite3DbStrNDup(pParse->db, pName->z, pName->n);
1754 if( dequote ) sqlite3Dequote(pItem->zEName);
danc9461ec2018-08-29 21:00:16 +00001755 if( IN_RENAME_OBJECT ){
drh41cee662019-12-12 20:22:34 +00001756 sqlite3RenameTokenMap(pParse, (void*)pItem->zEName, pName);
dan5be60c52018-08-15 20:28:39 +00001757 }
drhb7916a72009-05-27 10:31:29 +00001758 }
1759}
1760
1761/*
1762** Set the ExprList.a[].zSpan element of the most recently added item
1763** on the expression list.
1764**
1765** pList might be NULL following an OOM error. But pSpan should never be
1766** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag
1767** is set.
1768*/
1769void sqlite3ExprListSetSpan(
1770 Parse *pParse, /* Parsing context */
1771 ExprList *pList, /* List to which to add the span. */
drh1be266b2017-12-24 00:18:47 +00001772 const char *zStart, /* Start of the span */
1773 const char *zEnd /* End of the span */
drhb7916a72009-05-27 10:31:29 +00001774){
1775 sqlite3 *db = pParse->db;
1776 assert( pList!=0 || db->mallocFailed!=0 );
1777 if( pList ){
1778 struct ExprList_item *pItem = &pList->a[pList->nExpr-1];
1779 assert( pList->nExpr>0 );
drhcbb9da32019-12-12 22:11:33 +00001780 if( pItem->zEName==0 ){
1781 pItem->zEName = sqlite3DbSpanDup(db, zStart, zEnd);
1782 pItem->eEName = ENAME_SPAN;
1783 }
drhb7916a72009-05-27 10:31:29 +00001784 }
1785}
1786
1787/*
danielk19777a15a4b2007-05-08 17:54:43 +00001788** If the expression list pEList contains more than iLimit elements,
1789** leave an error message in pParse.
1790*/
1791void sqlite3ExprListCheckLength(
1792 Parse *pParse,
1793 ExprList *pEList,
danielk19777a15a4b2007-05-08 17:54:43 +00001794 const char *zObject
1795){
drhb1a6c3c2008-03-20 16:30:17 +00001796 int mx = pParse->db->aLimit[SQLITE_LIMIT_COLUMN];
drhc5499be2008-04-01 15:06:33 +00001797 testcase( pEList && pEList->nExpr==mx );
1798 testcase( pEList && pEList->nExpr==mx+1 );
drhb1a6c3c2008-03-20 16:30:17 +00001799 if( pEList && pEList->nExpr>mx ){
danielk19777a15a4b2007-05-08 17:54:43 +00001800 sqlite3ErrorMsg(pParse, "too many columns in %s", zObject);
1801 }
1802}
1803
1804/*
drha76b5df2002-02-23 02:32:10 +00001805** Delete an entire expression list.
1806*/
drhaffa8552016-04-11 18:25:05 +00001807static SQLITE_NOINLINE void exprListDeleteNN(sqlite3 *db, ExprList *pList){
drhac48b752017-04-05 11:57:56 +00001808 int i = pList->nExpr;
1809 struct ExprList_item *pItem = pList->a;
1810 assert( pList->nExpr>0 );
1811 do{
drh633e6d52008-07-28 19:34:53 +00001812 sqlite3ExprDelete(db, pItem->pExpr);
drh41cee662019-12-12 20:22:34 +00001813 sqlite3DbFree(db, pItem->zEName);
drhac48b752017-04-05 11:57:56 +00001814 pItem++;
1815 }while( --i>0 );
drhdbd6a7d2017-04-05 12:39:49 +00001816 sqlite3DbFreeNN(db, pList);
drha76b5df2002-02-23 02:32:10 +00001817}
drhaffa8552016-04-11 18:25:05 +00001818void sqlite3ExprListDelete(sqlite3 *db, ExprList *pList){
1819 if( pList ) exprListDeleteNN(db, pList);
1820}
drha76b5df2002-02-23 02:32:10 +00001821
1822/*
drh2308ed32015-02-09 16:09:34 +00001823** Return the bitwise-OR of all Expr.flags fields in the given
1824** ExprList.
drh885a5b02015-02-09 15:21:36 +00001825*/
drh2308ed32015-02-09 16:09:34 +00001826u32 sqlite3ExprListFlags(const ExprList *pList){
drh885a5b02015-02-09 15:21:36 +00001827 int i;
drh2308ed32015-02-09 16:09:34 +00001828 u32 m = 0;
drh508e2d02017-09-30 01:25:04 +00001829 assert( pList!=0 );
1830 for(i=0; i<pList->nExpr; i++){
1831 Expr *pExpr = pList->a[i].pExpr;
1832 assert( pExpr!=0 );
1833 m |= pExpr->flags;
drh885a5b02015-02-09 15:21:36 +00001834 }
drh2308ed32015-02-09 16:09:34 +00001835 return m;
drh885a5b02015-02-09 15:21:36 +00001836}
1837
1838/*
drh7e6f9802017-09-04 00:33:04 +00001839** This is a SELECT-node callback for the expression walker that
1840** always "fails". By "fail" in this case, we mean set
1841** pWalker->eCode to zero and abort.
1842**
1843** This callback is used by multiple expression walkers.
1844*/
1845int sqlite3SelectWalkFail(Walker *pWalker, Select *NotUsed){
1846 UNUSED_PARAMETER(NotUsed);
1847 pWalker->eCode = 0;
1848 return WRC_Abort;
1849}
1850
1851/*
drh0cbec592020-01-03 02:20:37 +00001852** Check the input string to see if it is "true" or "false" (in any case).
1853**
1854** If the string is.... Return
1855** "true" EP_IsTrue
1856** "false" EP_IsFalse
1857** anything else 0
1858*/
1859u32 sqlite3IsTrueOrFalse(const char *zIn){
1860 if( sqlite3StrICmp(zIn, "true")==0 ) return EP_IsTrue;
1861 if( sqlite3StrICmp(zIn, "false")==0 ) return EP_IsFalse;
1862 return 0;
1863}
1864
1865
1866/*
drh171d16b2018-02-26 20:15:54 +00001867** If the input expression is an ID with the name "true" or "false"
drh96acafb2018-02-27 14:49:25 +00001868** then convert it into an TK_TRUEFALSE term. Return non-zero if
1869** the conversion happened, and zero if the expression is unaltered.
drh171d16b2018-02-26 20:15:54 +00001870*/
1871int sqlite3ExprIdToTrueFalse(Expr *pExpr){
drh0cbec592020-01-03 02:20:37 +00001872 u32 v;
drh171d16b2018-02-26 20:15:54 +00001873 assert( pExpr->op==TK_ID || pExpr->op==TK_STRING );
drh51d35b02019-01-11 13:32:23 +00001874 if( !ExprHasProperty(pExpr, EP_Quoted)
drh0cbec592020-01-03 02:20:37 +00001875 && (v = sqlite3IsTrueOrFalse(pExpr->u.zToken))!=0
drh171d16b2018-02-26 20:15:54 +00001876 ){
1877 pExpr->op = TK_TRUEFALSE;
drh0cbec592020-01-03 02:20:37 +00001878 ExprSetProperty(pExpr, v);
drh171d16b2018-02-26 20:15:54 +00001879 return 1;
1880 }
1881 return 0;
1882}
1883
drh43c4ac82018-02-26 21:26:27 +00001884/*
drh96acafb2018-02-27 14:49:25 +00001885** The argument must be a TK_TRUEFALSE Expr node. Return 1 if it is TRUE
drh43c4ac82018-02-26 21:26:27 +00001886** and 0 if it is FALSE.
1887*/
drh96acafb2018-02-27 14:49:25 +00001888int sqlite3ExprTruthValue(const Expr *pExpr){
dan6ece3532019-06-12 13:49:32 +00001889 pExpr = sqlite3ExprSkipCollate((Expr*)pExpr);
drh43c4ac82018-02-26 21:26:27 +00001890 assert( pExpr->op==TK_TRUEFALSE );
1891 assert( sqlite3StrICmp(pExpr->u.zToken,"true")==0
1892 || sqlite3StrICmp(pExpr->u.zToken,"false")==0 );
1893 return pExpr->u.zToken[4]==0;
1894}
1895
drh17180fc2019-04-19 17:26:19 +00001896/*
1897** If pExpr is an AND or OR expression, try to simplify it by eliminating
1898** terms that are always true or false. Return the simplified expression.
1899** Or return the original expression if no simplification is possible.
1900**
1901** Examples:
1902**
1903** (x<10) AND true => (x<10)
1904** (x<10) AND false => false
1905** (x<10) AND (y=22 OR false) => (x<10) AND (y=22)
1906** (x<10) AND (y=22 OR true) => (x<10)
1907** (y=22) OR true => true
1908*/
1909Expr *sqlite3ExprSimplifiedAndOr(Expr *pExpr){
1910 assert( pExpr!=0 );
1911 if( pExpr->op==TK_AND || pExpr->op==TK_OR ){
1912 Expr *pRight = sqlite3ExprSimplifiedAndOr(pExpr->pRight);
1913 Expr *pLeft = sqlite3ExprSimplifiedAndOr(pExpr->pLeft);
1914 if( ExprAlwaysTrue(pLeft) || ExprAlwaysFalse(pRight) ){
1915 pExpr = pExpr->op==TK_AND ? pRight : pLeft;
1916 }else if( ExprAlwaysTrue(pRight) || ExprAlwaysFalse(pLeft) ){
1917 pExpr = pExpr->op==TK_AND ? pLeft : pRight;
1918 }
1919 }
1920 return pExpr;
1921}
1922
drh171d16b2018-02-26 20:15:54 +00001923
1924/*
drh059b2d52014-10-24 19:28:09 +00001925** These routines are Walker callbacks used to check expressions to
1926** see if they are "constant" for some definition of constant. The
1927** Walker.eCode value determines the type of "constant" we are looking
1928** for.
drh73b211a2005-01-18 04:00:42 +00001929**
drh7d10d5a2008-08-20 16:35:10 +00001930** These callback routines are used to implement the following:
drh626a8792005-01-17 22:08:19 +00001931**
drh059b2d52014-10-24 19:28:09 +00001932** sqlite3ExprIsConstant() pWalker->eCode==1
1933** sqlite3ExprIsConstantNotJoin() pWalker->eCode==2
drhfcb9f4f2015-06-01 18:13:16 +00001934** sqlite3ExprIsTableConstant() pWalker->eCode==3
drh059b2d52014-10-24 19:28:09 +00001935** sqlite3ExprIsConstantOrFunction() pWalker->eCode==4 or 5
drh87abf5c2005-08-25 12:45:04 +00001936**
drh059b2d52014-10-24 19:28:09 +00001937** In all cases, the callbacks set Walker.eCode=0 and abort if the expression
1938** is found to not be a constant.
drh87abf5c2005-08-25 12:45:04 +00001939**
drhfeada2d2014-09-24 13:20:22 +00001940** The sqlite3ExprIsConstantOrFunction() is used for evaluating expressions
drh059b2d52014-10-24 19:28:09 +00001941** in a CREATE TABLE statement. The Walker.eCode value is 5 when parsing
1942** an existing schema and 4 when processing a new statement. A bound
drhfeada2d2014-09-24 13:20:22 +00001943** parameter raises an error for new statements, but is silently converted
1944** to NULL for existing schemas. This allows sqlite_master tables that
1945** contain a bound parameter because they were generated by older versions
1946** of SQLite to be parsed by newer versions of SQLite without raising a
1947** malformed schema error.
drh626a8792005-01-17 22:08:19 +00001948*/
drh7d10d5a2008-08-20 16:35:10 +00001949static int exprNodeIsConstant(Walker *pWalker, Expr *pExpr){
drh626a8792005-01-17 22:08:19 +00001950
drh059b2d52014-10-24 19:28:09 +00001951 /* If pWalker->eCode is 2 then any term of the expression that comes from
1952 ** the ON or USING clauses of a left join disqualifies the expression
drh0a168372007-06-08 00:20:47 +00001953 ** from being considered constant. */
drh059b2d52014-10-24 19:28:09 +00001954 if( pWalker->eCode==2 && ExprHasProperty(pExpr, EP_FromJoin) ){
1955 pWalker->eCode = 0;
drh7d10d5a2008-08-20 16:35:10 +00001956 return WRC_Abort;
drh0a168372007-06-08 00:20:47 +00001957 }
1958
drh626a8792005-01-17 22:08:19 +00001959 switch( pExpr->op ){
drheb55bd22005-06-30 17:04:21 +00001960 /* Consider functions to be constant if all their arguments are constant
drh059b2d52014-10-24 19:28:09 +00001961 ** and either pWalker->eCode==4 or 5 or the function has the
1962 ** SQLITE_FUNC_CONST flag. */
drheb55bd22005-06-30 17:04:21 +00001963 case TK_FUNCTION:
drha634c9e2019-12-04 19:45:52 +00001964 if( (pWalker->eCode>=4 || ExprHasProperty(pExpr,EP_ConstFunc))
1965 && !ExprHasProperty(pExpr, EP_WinFunc)
1966 ){
drhb1fba282013-11-21 14:33:48 +00001967 return WRC_Continue;
drh059b2d52014-10-24 19:28:09 +00001968 }else{
1969 pWalker->eCode = 0;
1970 return WRC_Abort;
drhb1fba282013-11-21 14:33:48 +00001971 }
drh626a8792005-01-17 22:08:19 +00001972 case TK_ID:
drh171d16b2018-02-26 20:15:54 +00001973 /* Convert "true" or "false" in a DEFAULT clause into the
1974 ** appropriate TK_TRUEFALSE operator */
drhe39ef312018-02-27 00:58:13 +00001975 if( sqlite3ExprIdToTrueFalse(pExpr) ){
drh171d16b2018-02-26 20:15:54 +00001976 return WRC_Prune;
1977 }
1978 /* Fall thru */
drh626a8792005-01-17 22:08:19 +00001979 case TK_COLUMN:
drh626a8792005-01-17 22:08:19 +00001980 case TK_AGG_FUNCTION:
drh13449892005-09-07 21:22:45 +00001981 case TK_AGG_COLUMN:
drhc5499be2008-04-01 15:06:33 +00001982 testcase( pExpr->op==TK_ID );
1983 testcase( pExpr->op==TK_COLUMN );
drhc5499be2008-04-01 15:06:33 +00001984 testcase( pExpr->op==TK_AGG_FUNCTION );
1985 testcase( pExpr->op==TK_AGG_COLUMN );
drh07aded62018-07-28 16:24:08 +00001986 if( ExprHasProperty(pExpr, EP_FixedCol) && pWalker->eCode!=2 ){
drhefad2e22018-07-27 16:57:11 +00001987 return WRC_Continue;
1988 }
drh059b2d52014-10-24 19:28:09 +00001989 if( pWalker->eCode==3 && pExpr->iTable==pWalker->u.iCur ){
1990 return WRC_Continue;
drh059b2d52014-10-24 19:28:09 +00001991 }
drhf43ce0b2017-05-25 00:08:48 +00001992 /* Fall through */
1993 case TK_IF_NULL_ROW:
drh6e341b92018-04-17 18:50:40 +00001994 case TK_REGISTER:
drh99160482018-04-18 01:34:39 +00001995 testcase( pExpr->op==TK_REGISTER );
drhf43ce0b2017-05-25 00:08:48 +00001996 testcase( pExpr->op==TK_IF_NULL_ROW );
1997 pWalker->eCode = 0;
1998 return WRC_Abort;
drhfeada2d2014-09-24 13:20:22 +00001999 case TK_VARIABLE:
drh059b2d52014-10-24 19:28:09 +00002000 if( pWalker->eCode==5 ){
drhfeada2d2014-09-24 13:20:22 +00002001 /* Silently convert bound parameters that appear inside of CREATE
2002 ** statements into a NULL when parsing the CREATE statement text out
2003 ** of the sqlite_master table */
2004 pExpr->op = TK_NULL;
drh059b2d52014-10-24 19:28:09 +00002005 }else if( pWalker->eCode==4 ){
drhfeada2d2014-09-24 13:20:22 +00002006 /* A bound parameter in a CREATE statement that originates from
2007 ** sqlite3_prepare() causes an error */
drh059b2d52014-10-24 19:28:09 +00002008 pWalker->eCode = 0;
drhfeada2d2014-09-24 13:20:22 +00002009 return WRC_Abort;
2010 }
2011 /* Fall through */
drh626a8792005-01-17 22:08:19 +00002012 default:
drh6e341b92018-04-17 18:50:40 +00002013 testcase( pExpr->op==TK_SELECT ); /* sqlite3SelectWalkFail() disallows */
2014 testcase( pExpr->op==TK_EXISTS ); /* sqlite3SelectWalkFail() disallows */
drh7d10d5a2008-08-20 16:35:10 +00002015 return WRC_Continue;
drh626a8792005-01-17 22:08:19 +00002016 }
2017}
drh059b2d52014-10-24 19:28:09 +00002018static int exprIsConst(Expr *p, int initFlag, int iCur){
drh7d10d5a2008-08-20 16:35:10 +00002019 Walker w;
drh059b2d52014-10-24 19:28:09 +00002020 w.eCode = initFlag;
drh7d10d5a2008-08-20 16:35:10 +00002021 w.xExprCallback = exprNodeIsConstant;
drh7e6f9802017-09-04 00:33:04 +00002022 w.xSelectCallback = sqlite3SelectWalkFail;
drh979dd1b2017-05-29 14:26:07 +00002023#ifdef SQLITE_DEBUG
2024 w.xSelectCallback2 = sqlite3SelectWalkAssert2;
2025#endif
drh059b2d52014-10-24 19:28:09 +00002026 w.u.iCur = iCur;
drh7d10d5a2008-08-20 16:35:10 +00002027 sqlite3WalkExpr(&w, p);
drh059b2d52014-10-24 19:28:09 +00002028 return w.eCode;
drh7d10d5a2008-08-20 16:35:10 +00002029}
drh626a8792005-01-17 22:08:19 +00002030
2031/*
drh059b2d52014-10-24 19:28:09 +00002032** Walk an expression tree. Return non-zero if the expression is constant
drheb55bd22005-06-30 17:04:21 +00002033** and 0 if it involves variables or function calls.
drh23989372002-05-21 13:43:04 +00002034**
2035** For the purposes of this function, a double-quoted string (ex: "abc")
2036** is considered a variable but a single-quoted string (ex: 'abc') is
2037** a constant.
drhfef52082000-06-06 01:50:43 +00002038*/
danielk19774adee202004-05-08 08:23:19 +00002039int sqlite3ExprIsConstant(Expr *p){
drh059b2d52014-10-24 19:28:09 +00002040 return exprIsConst(p, 1, 0);
drhfef52082000-06-06 01:50:43 +00002041}
2042
2043/*
drh07aded62018-07-28 16:24:08 +00002044** Walk an expression tree. Return non-zero if
2045**
2046** (1) the expression is constant, and
2047** (2) the expression does originate in the ON or USING clause
2048** of a LEFT JOIN, and
2049** (3) the expression does not contain any EP_FixedCol TK_COLUMN
2050** operands created by the constant propagation optimization.
2051**
2052** When this routine returns true, it indicates that the expression
2053** can be added to the pParse->pConstExpr list and evaluated once when
2054** the prepared statement starts up. See sqlite3ExprCodeAtInit().
drh0a168372007-06-08 00:20:47 +00002055*/
2056int sqlite3ExprIsConstantNotJoin(Expr *p){
drh059b2d52014-10-24 19:28:09 +00002057 return exprIsConst(p, 2, 0);
drh0a168372007-06-08 00:20:47 +00002058}
2059
2060/*
drhfcb9f4f2015-06-01 18:13:16 +00002061** Walk an expression tree. Return non-zero if the expression is constant
drh059b2d52014-10-24 19:28:09 +00002062** for any single row of the table with cursor iCur. In other words, the
2063** expression must not refer to any non-deterministic function nor any
2064** table other than iCur.
2065*/
2066int sqlite3ExprIsTableConstant(Expr *p, int iCur){
2067 return exprIsConst(p, 3, iCur);
2068}
2069
danab31a842017-04-29 20:53:09 +00002070
2071/*
2072** sqlite3WalkExpr() callback used by sqlite3ExprIsConstantOrGroupBy().
2073*/
2074static int exprNodeIsConstantOrGroupBy(Walker *pWalker, Expr *pExpr){
2075 ExprList *pGroupBy = pWalker->u.pGroupBy;
2076 int i;
2077
2078 /* Check if pExpr is identical to any GROUP BY term. If so, consider
2079 ** it constant. */
2080 for(i=0; i<pGroupBy->nExpr; i++){
2081 Expr *p = pGroupBy->a[i].pExpr;
dan5aa550c2017-06-24 18:10:29 +00002082 if( sqlite3ExprCompare(0, pExpr, p, -1)<2 ){
drh70efa842017-09-28 01:58:23 +00002083 CollSeq *pColl = sqlite3ExprNNCollSeq(pWalker->pParse, p);
drhefad2e22018-07-27 16:57:11 +00002084 if( sqlite3IsBinary(pColl) ){
danab31a842017-04-29 20:53:09 +00002085 return WRC_Prune;
2086 }
2087 }
2088 }
2089
2090 /* Check if pExpr is a sub-select. If so, consider it variable. */
2091 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
2092 pWalker->eCode = 0;
2093 return WRC_Abort;
2094 }
2095
2096 return exprNodeIsConstant(pWalker, pExpr);
2097}
2098
2099/*
2100** Walk the expression tree passed as the first argument. Return non-zero
2101** if the expression consists entirely of constants or copies of terms
2102** in pGroupBy that sort with the BINARY collation sequence.
drhab314002017-05-02 16:46:41 +00002103**
2104** This routine is used to determine if a term of the HAVING clause can
2105** be promoted into the WHERE clause. In order for such a promotion to work,
2106** the value of the HAVING clause term must be the same for all members of
2107** a "group". The requirement that the GROUP BY term must be BINARY
2108** assumes that no other collating sequence will have a finer-grained
2109** grouping than binary. In other words (A=B COLLATE binary) implies
2110** A=B in every other collating sequence. The requirement that the
2111** GROUP BY be BINARY is stricter than necessary. It would also work
2112** to promote HAVING clauses that use the same alternative collating
2113** sequence as the GROUP BY term, but that is much harder to check,
2114** alternative collating sequences are uncommon, and this is only an
2115** optimization, so we take the easy way out and simply require the
2116** GROUP BY to use the BINARY collating sequence.
danab31a842017-04-29 20:53:09 +00002117*/
2118int sqlite3ExprIsConstantOrGroupBy(Parse *pParse, Expr *p, ExprList *pGroupBy){
2119 Walker w;
danab31a842017-04-29 20:53:09 +00002120 w.eCode = 1;
2121 w.xExprCallback = exprNodeIsConstantOrGroupBy;
drh979dd1b2017-05-29 14:26:07 +00002122 w.xSelectCallback = 0;
danab31a842017-04-29 20:53:09 +00002123 w.u.pGroupBy = pGroupBy;
2124 w.pParse = pParse;
2125 sqlite3WalkExpr(&w, p);
2126 return w.eCode;
2127}
2128
drh059b2d52014-10-24 19:28:09 +00002129/*
2130** Walk an expression tree. Return non-zero if the expression is constant
drheb55bd22005-06-30 17:04:21 +00002131** or a function call with constant arguments. Return and 0 if there
2132** are any variables.
2133**
2134** For the purposes of this function, a double-quoted string (ex: "abc")
2135** is considered a variable but a single-quoted string (ex: 'abc') is
2136** a constant.
2137*/
drhfeada2d2014-09-24 13:20:22 +00002138int sqlite3ExprIsConstantOrFunction(Expr *p, u8 isInit){
2139 assert( isInit==0 || isInit==1 );
drh059b2d52014-10-24 19:28:09 +00002140 return exprIsConst(p, 4+isInit, 0);
drheb55bd22005-06-30 17:04:21 +00002141}
2142
drh5b88bc42013-12-07 23:35:21 +00002143#ifdef SQLITE_ENABLE_CURSOR_HINTS
2144/*
2145** Walk an expression tree. Return 1 if the expression contains a
2146** subquery of some kind. Return 0 if there are no subqueries.
2147*/
2148int sqlite3ExprContainsSubquery(Expr *p){
2149 Walker w;
drhbec24762015-08-13 20:07:13 +00002150 w.eCode = 1;
drh5b88bc42013-12-07 23:35:21 +00002151 w.xExprCallback = sqlite3ExprWalkNoop;
drh7e6f9802017-09-04 00:33:04 +00002152 w.xSelectCallback = sqlite3SelectWalkFail;
drh979dd1b2017-05-29 14:26:07 +00002153#ifdef SQLITE_DEBUG
2154 w.xSelectCallback2 = sqlite3SelectWalkAssert2;
2155#endif
drh5b88bc42013-12-07 23:35:21 +00002156 sqlite3WalkExpr(&w, p);
drh07194bf2015-08-13 20:34:41 +00002157 return w.eCode==0;
drh5b88bc42013-12-07 23:35:21 +00002158}
2159#endif
2160
drheb55bd22005-06-30 17:04:21 +00002161/*
drh73b211a2005-01-18 04:00:42 +00002162** If the expression p codes a constant integer that is small enough
drh202b2df2004-01-06 01:13:46 +00002163** to fit in a 32-bit integer, return 1 and put the value of the integer
2164** in *pValue. If the expression is not an integer or if it is too big
2165** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
drhe4de1fe2002-06-02 16:09:01 +00002166*/
danielk19774adee202004-05-08 08:23:19 +00002167int sqlite3ExprIsInteger(Expr *p, int *pValue){
drh92b01d52008-06-24 00:32:35 +00002168 int rc = 0;
drh1d2d71a2019-04-19 23:05:56 +00002169 if( NEVER(p==0) ) return 0; /* Used to only happen following on OOM */
drhcd92e842011-02-17 15:58:20 +00002170
2171 /* If an expression is an integer literal that fits in a signed 32-bit
2172 ** integer, then the EP_IntValue flag will have already been set */
2173 assert( p->op!=TK_INTEGER || (p->flags & EP_IntValue)!=0
2174 || sqlite3GetInt32(p->u.zToken, &rc)==0 );
2175
drh92b01d52008-06-24 00:32:35 +00002176 if( p->flags & EP_IntValue ){
drh33e619f2009-05-28 01:00:55 +00002177 *pValue = p->u.iValue;
drh92b01d52008-06-24 00:32:35 +00002178 return 1;
2179 }
drhe4de1fe2002-06-02 16:09:01 +00002180 switch( p->op ){
drh4b59ab52002-08-24 18:24:51 +00002181 case TK_UPLUS: {
drh92b01d52008-06-24 00:32:35 +00002182 rc = sqlite3ExprIsInteger(p->pLeft, pValue);
drhf6e369a2008-06-24 12:46:30 +00002183 break;
drh4b59ab52002-08-24 18:24:51 +00002184 }
drhe4de1fe2002-06-02 16:09:01 +00002185 case TK_UMINUS: {
2186 int v;
danielk19774adee202004-05-08 08:23:19 +00002187 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
mistachkinf6418892013-08-28 01:54:12 +00002188 assert( v!=(-2147483647-1) );
drhe4de1fe2002-06-02 16:09:01 +00002189 *pValue = -v;
drh92b01d52008-06-24 00:32:35 +00002190 rc = 1;
drhe4de1fe2002-06-02 16:09:01 +00002191 }
2192 break;
2193 }
2194 default: break;
2195 }
drh92b01d52008-06-24 00:32:35 +00002196 return rc;
drhe4de1fe2002-06-02 16:09:01 +00002197}
2198
2199/*
drh039fc322009-11-17 18:31:47 +00002200** Return FALSE if there is no chance that the expression can be NULL.
2201**
2202** If the expression might be NULL or if the expression is too complex
2203** to tell return TRUE.
2204**
2205** This routine is used as an optimization, to skip OP_IsNull opcodes
2206** when we know that a value cannot be NULL. Hence, a false positive
2207** (returning TRUE when in fact the expression can never be NULL) might
2208** be a small performance hit but is otherwise harmless. On the other
2209** hand, a false negative (returning FALSE when the result could be NULL)
2210** will likely result in an incorrect answer. So when in doubt, return
2211** TRUE.
2212*/
2213int sqlite3ExprCanBeNull(const Expr *p){
2214 u8 op;
drh9bfb0792018-12-22 01:13:25 +00002215 while( p->op==TK_UPLUS || p->op==TK_UMINUS ){
2216 p = p->pLeft;
2217 }
drh039fc322009-11-17 18:31:47 +00002218 op = p->op;
2219 if( op==TK_REGISTER ) op = p->op2;
2220 switch( op ){
2221 case TK_INTEGER:
2222 case TK_STRING:
2223 case TK_FLOAT:
2224 case TK_BLOB:
2225 return 0;
drh7248a8b2014-08-04 18:50:54 +00002226 case TK_COLUMN:
drh72673a22014-12-04 16:27:17 +00002227 return ExprHasProperty(p, EP_CanBeNull) ||
drheda079c2018-09-20 19:02:15 +00002228 p->y.pTab==0 || /* Reference to column of index on expression */
drh4eac5f02019-12-24 15:01:17 +00002229 (p->iColumn>=0
2230 && ALWAYS(p->y.pTab->aCol!=0) /* Defense against OOM problems */
2231 && p->y.pTab->aCol[p->iColumn].notNull==0);
drh039fc322009-11-17 18:31:47 +00002232 default:
2233 return 1;
2234 }
2235}
2236
2237/*
2238** Return TRUE if the given expression is a constant which would be
2239** unchanged by OP_Affinity with the affinity given in the second
2240** argument.
2241**
2242** This routine is used to determine if the OP_Affinity operation
2243** can be omitted. When in doubt return FALSE. A false negative
2244** is harmless. A false positive, however, can result in the wrong
2245** answer.
2246*/
2247int sqlite3ExprNeedsNoAffinityChange(const Expr *p, char aff){
2248 u8 op;
drhaf866402019-08-22 11:11:28 +00002249 int unaryMinus = 0;
drh05883a32015-06-02 15:32:08 +00002250 if( aff==SQLITE_AFF_BLOB ) return 1;
drhaf866402019-08-22 11:11:28 +00002251 while( p->op==TK_UPLUS || p->op==TK_UMINUS ){
2252 if( p->op==TK_UMINUS ) unaryMinus = 1;
2253 p = p->pLeft;
2254 }
drh039fc322009-11-17 18:31:47 +00002255 op = p->op;
2256 if( op==TK_REGISTER ) op = p->op2;
2257 switch( op ){
2258 case TK_INTEGER: {
drh6a198652019-08-31 01:33:19 +00002259 return aff>=SQLITE_AFF_NUMERIC;
drh039fc322009-11-17 18:31:47 +00002260 }
2261 case TK_FLOAT: {
drh6a198652019-08-31 01:33:19 +00002262 return aff>=SQLITE_AFF_NUMERIC;
drh039fc322009-11-17 18:31:47 +00002263 }
2264 case TK_STRING: {
drhaf866402019-08-22 11:11:28 +00002265 return !unaryMinus && aff==SQLITE_AFF_TEXT;
drh039fc322009-11-17 18:31:47 +00002266 }
2267 case TK_BLOB: {
drhaf866402019-08-22 11:11:28 +00002268 return !unaryMinus;
drh039fc322009-11-17 18:31:47 +00002269 }
drh2f2855b2009-11-18 01:25:26 +00002270 case TK_COLUMN: {
drh88376ca2009-11-19 15:44:53 +00002271 assert( p->iTable>=0 ); /* p cannot be part of a CHECK constraint */
drh6a198652019-08-31 01:33:19 +00002272 return aff>=SQLITE_AFF_NUMERIC && p->iColumn<0;
drh2f2855b2009-11-18 01:25:26 +00002273 }
drh039fc322009-11-17 18:31:47 +00002274 default: {
2275 return 0;
2276 }
2277 }
2278}
2279
2280/*
drhc4a3c772001-04-04 11:48:57 +00002281** Return TRUE if the given string is a row-id column name.
2282*/
danielk19774adee202004-05-08 08:23:19 +00002283int sqlite3IsRowid(const char *z){
2284 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
2285 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
2286 if( sqlite3StrICmp(z, "OID")==0 ) return 1;
drhc4a3c772001-04-04 11:48:57 +00002287 return 0;
2288}
2289
danielk19779a96b662007-11-29 17:05:18 +00002290/*
drh69c355b2016-03-09 15:34:51 +00002291** pX is the RHS of an IN operator. If pX is a SELECT statement
2292** that can be simplified to a direct table access, then return
2293** a pointer to the SELECT statement. If pX is not a SELECT statement,
2294** or if the SELECT statement needs to be manifested into a transient
2295** table, then return NULL.
drhb287f4b2008-04-25 00:08:38 +00002296*/
2297#ifndef SQLITE_OMIT_SUBQUERY
dan7b35a772016-07-28 19:47:15 +00002298static Select *isCandidateForInOpt(Expr *pX){
drh69c355b2016-03-09 15:34:51 +00002299 Select *p;
drhb287f4b2008-04-25 00:08:38 +00002300 SrcList *pSrc;
2301 ExprList *pEList;
2302 Table *pTab;
dancfbb5e82016-07-13 19:48:13 +00002303 int i;
drh69c355b2016-03-09 15:34:51 +00002304 if( !ExprHasProperty(pX, EP_xIsSelect) ) return 0; /* Not a subquery */
2305 if( ExprHasProperty(pX, EP_VarSelect) ) return 0; /* Correlated subq */
2306 p = pX->x.pSelect;
drhb287f4b2008-04-25 00:08:38 +00002307 if( p->pPrior ) return 0; /* Not a compound SELECT */
drh7d10d5a2008-08-20 16:35:10 +00002308 if( p->selFlags & (SF_Distinct|SF_Aggregate) ){
drhb74b1012009-05-28 21:04:37 +00002309 testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
2310 testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
2311 return 0; /* No DISTINCT keyword and no aggregate functions */
drh7d10d5a2008-08-20 16:35:10 +00002312 }
drhb97f3532019-12-28 02:40:49 +00002313 if( p->pGroupBy ) return 0; /* Has no GROUP BY clause */
drhb287f4b2008-04-25 00:08:38 +00002314 if( p->pLimit ) return 0; /* Has no LIMIT clause */
drhb287f4b2008-04-25 00:08:38 +00002315 if( p->pWhere ) return 0; /* Has no WHERE clause */
2316 pSrc = p->pSrc;
drhd1fa7bc2009-01-10 13:24:50 +00002317 assert( pSrc!=0 );
2318 if( pSrc->nSrc!=1 ) return 0; /* Single term in FROM clause */
drhb74b1012009-05-28 21:04:37 +00002319 if( pSrc->a[0].pSelect ) return 0; /* FROM is not a subquery or view */
drhb287f4b2008-04-25 00:08:38 +00002320 pTab = pSrc->a[0].pTab;
drh69c355b2016-03-09 15:34:51 +00002321 assert( pTab!=0 );
drhb74b1012009-05-28 21:04:37 +00002322 assert( pTab->pSelect==0 ); /* FROM clause is not a view */
drhb287f4b2008-04-25 00:08:38 +00002323 if( IsVirtual(pTab) ) return 0; /* FROM clause not a virtual table */
2324 pEList = p->pEList;
drhac6b47d2016-08-24 00:51:48 +00002325 assert( pEList!=0 );
dan7b35a772016-07-28 19:47:15 +00002326 /* All SELECT results must be columns. */
dancfbb5e82016-07-13 19:48:13 +00002327 for(i=0; i<pEList->nExpr; i++){
2328 Expr *pRes = pEList->a[i].pExpr;
2329 if( pRes->op!=TK_COLUMN ) return 0;
2330 assert( pRes->iTable==pSrc->a[0].iCursor ); /* Not a correlated subquery */
dancfbb5e82016-07-13 19:48:13 +00002331 }
drh69c355b2016-03-09 15:34:51 +00002332 return p;
drhb287f4b2008-04-25 00:08:38 +00002333}
2334#endif /* SQLITE_OMIT_SUBQUERY */
2335
danf9b2e052016-08-02 17:45:00 +00002336#ifndef SQLITE_OMIT_SUBQUERY
dan1d8cb212011-12-09 13:24:16 +00002337/*
drh4c259e92014-08-01 21:12:35 +00002338** Generate code that checks the left-most column of index table iCur to see if
2339** it contains any NULL entries. Cause the register at regHasNull to be set
drh6be515e2014-08-01 21:00:53 +00002340** to a non-NULL value if iCur contains no NULLs. Cause register regHasNull
2341** to be set to NULL if iCur contains one or more NULL values.
2342*/
2343static void sqlite3SetHasNullFlag(Vdbe *v, int iCur, int regHasNull){
drh728e0f92015-10-10 14:41:28 +00002344 int addr1;
drh6be515e2014-08-01 21:00:53 +00002345 sqlite3VdbeAddOp2(v, OP_Integer, 0, regHasNull);
drh728e0f92015-10-10 14:41:28 +00002346 addr1 = sqlite3VdbeAddOp1(v, OP_Rewind, iCur); VdbeCoverage(v);
drh6be515e2014-08-01 21:00:53 +00002347 sqlite3VdbeAddOp3(v, OP_Column, iCur, 0, regHasNull);
2348 sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG);
drh4c259e92014-08-01 21:12:35 +00002349 VdbeComment((v, "first_entry_in(%d)", iCur));
drh728e0f92015-10-10 14:41:28 +00002350 sqlite3VdbeJumpHere(v, addr1);
drh6be515e2014-08-01 21:00:53 +00002351}
danf9b2e052016-08-02 17:45:00 +00002352#endif
drh6be515e2014-08-01 21:00:53 +00002353
drhbb53ecb2014-08-02 21:03:33 +00002354
2355#ifndef SQLITE_OMIT_SUBQUERY
2356/*
2357** The argument is an IN operator with a list (not a subquery) on the
2358** right-hand side. Return TRUE if that list is constant.
2359*/
2360static int sqlite3InRhsIsConstant(Expr *pIn){
2361 Expr *pLHS;
2362 int res;
2363 assert( !ExprHasProperty(pIn, EP_xIsSelect) );
2364 pLHS = pIn->pLeft;
2365 pIn->pLeft = 0;
2366 res = sqlite3ExprIsConstant(pIn);
2367 pIn->pLeft = pLHS;
2368 return res;
2369}
2370#endif
2371
drh6be515e2014-08-01 21:00:53 +00002372/*
danielk19779a96b662007-11-29 17:05:18 +00002373** This function is used by the implementation of the IN (...) operator.
drhd4305ca2012-09-18 17:08:33 +00002374** The pX parameter is the expression on the RHS of the IN operator, which
2375** might be either a list of expressions or a subquery.
danielk19779a96b662007-11-29 17:05:18 +00002376**
drhd4305ca2012-09-18 17:08:33 +00002377** The job of this routine is to find or create a b-tree object that can
2378** be used either to test for membership in the RHS set or to iterate through
2379** all members of the RHS set, skipping duplicates.
2380**
drh3a856252014-08-01 14:46:57 +00002381** A cursor is opened on the b-tree object that is the RHS of the IN operator
drhd4305ca2012-09-18 17:08:33 +00002382** and pX->iTable is set to the index of that cursor.
2383**
drhb74b1012009-05-28 21:04:37 +00002384** The returned value of this function indicates the b-tree type, as follows:
danielk19779a96b662007-11-29 17:05:18 +00002385**
drh1ccce442013-03-12 20:38:51 +00002386** IN_INDEX_ROWID - The cursor was opened on a database table.
2387** IN_INDEX_INDEX_ASC - The cursor was opened on an ascending index.
2388** IN_INDEX_INDEX_DESC - The cursor was opened on a descending index.
2389** IN_INDEX_EPH - The cursor was opened on a specially created and
2390** populated epheremal table.
drhbb53ecb2014-08-02 21:03:33 +00002391** IN_INDEX_NOOP - No cursor was allocated. The IN operator must be
2392** implemented as a sequence of comparisons.
danielk19779a96b662007-11-29 17:05:18 +00002393**
drhd4305ca2012-09-18 17:08:33 +00002394** An existing b-tree might be used if the RHS expression pX is a simple
2395** subquery such as:
danielk19779a96b662007-11-29 17:05:18 +00002396**
dan553168c2016-08-01 20:14:31 +00002397** SELECT <column1>, <column2>... FROM <table>
danielk19779a96b662007-11-29 17:05:18 +00002398**
drhd4305ca2012-09-18 17:08:33 +00002399** If the RHS of the IN operator is a list or a more complex subquery, then
2400** an ephemeral table might need to be generated from the RHS and then
peter.d.reid60ec9142014-09-06 16:39:46 +00002401** pX->iTable made to point to the ephemeral table instead of an
drh3a856252014-08-01 14:46:57 +00002402** existing table.
drhd4305ca2012-09-18 17:08:33 +00002403**
drh7fc0ba02017-11-17 15:02:00 +00002404** The inFlags parameter must contain, at a minimum, one of the bits
2405** IN_INDEX_MEMBERSHIP or IN_INDEX_LOOP but not both. If inFlags contains
2406** IN_INDEX_MEMBERSHIP, then the generated table will be used for a fast
2407** membership test. When the IN_INDEX_LOOP bit is set, the IN index will
2408** be used to loop over all values of the RHS of the IN operator.
drh3a856252014-08-01 14:46:57 +00002409**
2410** When IN_INDEX_LOOP is used (and the b-tree will be used to iterate
2411** through the set members) then the b-tree must not contain duplicates.
drh7fc0ba02017-11-17 15:02:00 +00002412** An epheremal table will be created unless the selected columns are guaranteed
dan553168c2016-08-01 20:14:31 +00002413** to be unique - either because it is an INTEGER PRIMARY KEY or due to
2414** a UNIQUE constraint or index.
danielk19770cdc0222008-06-26 18:04:03 +00002415**
drh3a856252014-08-01 14:46:57 +00002416** When IN_INDEX_MEMBERSHIP is used (and the b-tree will be used
2417** for fast set membership tests) then an epheremal table must
dan553168c2016-08-01 20:14:31 +00002418** be used unless <columns> is a single INTEGER PRIMARY KEY column or an
2419** index can be found with the specified <columns> as its left-most.
danielk19770cdc0222008-06-26 18:04:03 +00002420**
drhbb53ecb2014-08-02 21:03:33 +00002421** If the IN_INDEX_NOOP_OK and IN_INDEX_MEMBERSHIP are both set and
2422** if the RHS of the IN operator is a list (not a subquery) then this
2423** routine might decide that creating an ephemeral b-tree for membership
2424** testing is too expensive and return IN_INDEX_NOOP. In that case, the
2425** calling routine should implement the IN operator using a sequence
2426** of Eq or Ne comparison operations.
2427**
drhb74b1012009-05-28 21:04:37 +00002428** When the b-tree is being used for membership tests, the calling function
drh3a856252014-08-01 14:46:57 +00002429** might need to know whether or not the RHS side of the IN operator
drhe21a6e12014-08-01 18:00:24 +00002430** contains a NULL. If prRhsHasNull is not a NULL pointer and
drh3a856252014-08-01 14:46:57 +00002431** if there is any chance that the (...) might contain a NULL value at
danielk19770cdc0222008-06-26 18:04:03 +00002432** runtime, then a register is allocated and the register number written
drhe21a6e12014-08-01 18:00:24 +00002433** to *prRhsHasNull. If there is no chance that the (...) contains a
2434** NULL value, then *prRhsHasNull is left unchanged.
danielk19770cdc0222008-06-26 18:04:03 +00002435**
drhe21a6e12014-08-01 18:00:24 +00002436** If a register is allocated and its location stored in *prRhsHasNull, then
drh6be515e2014-08-01 21:00:53 +00002437** the value in that register will be NULL if the b-tree contains one or more
2438** NULL values, and it will be some non-NULL value if the b-tree contains no
2439** NULL values.
dan553168c2016-08-01 20:14:31 +00002440**
2441** If the aiMap parameter is not NULL, it must point to an array containing
2442** one element for each column returned by the SELECT statement on the RHS
2443** of the IN(...) operator. The i'th entry of the array is populated with the
2444** offset of the index column that matches the i'th column returned by the
2445** SELECT. For example, if the expression and selected index are:
2446**
2447** (?,?,?) IN (SELECT a, b, c FROM t1)
2448** CREATE INDEX i1 ON t1(b, c, a);
2449**
2450** then aiMap[] is populated with {2, 0, 1}.
danielk19779a96b662007-11-29 17:05:18 +00002451*/
danielk1977284f4ac2007-12-10 05:03:46 +00002452#ifndef SQLITE_OMIT_SUBQUERY
danba00e302016-07-23 20:24:06 +00002453int sqlite3FindInIndex(
drh6fc8f362016-08-26 19:31:29 +00002454 Parse *pParse, /* Parsing context */
drh0167ef22019-09-02 01:25:07 +00002455 Expr *pX, /* The IN expression */
drh6fc8f362016-08-26 19:31:29 +00002456 u32 inFlags, /* IN_INDEX_LOOP, _MEMBERSHIP, and/or _NOOP_OK */
2457 int *prRhsHasNull, /* Register holding NULL status. See notes */
drh2c041312018-12-24 02:34:49 +00002458 int *aiMap, /* Mapping from Index fields to RHS fields */
2459 int *piTab /* OUT: index to use */
danba00e302016-07-23 20:24:06 +00002460){
drhb74b1012009-05-28 21:04:37 +00002461 Select *p; /* SELECT to the right of IN operator */
2462 int eType = 0; /* Type of RHS table. IN_INDEX_* */
2463 int iTab = pParse->nTab++; /* Cursor of the RHS table */
drh3a856252014-08-01 14:46:57 +00002464 int mustBeUnique; /* True if RHS must be unique */
drhb8475df2011-12-09 16:21:19 +00002465 Vdbe *v = sqlite3GetVdbe(pParse); /* Virtual machine being coded */
danielk19779a96b662007-11-29 17:05:18 +00002466
drh1450bc62009-10-30 13:25:56 +00002467 assert( pX->op==TK_IN );
drh3a856252014-08-01 14:46:57 +00002468 mustBeUnique = (inFlags & IN_INDEX_LOOP)!=0;
drh1450bc62009-10-30 13:25:56 +00002469
dan7b35a772016-07-28 19:47:15 +00002470 /* If the RHS of this IN(...) operator is a SELECT, and if it matters
2471 ** whether or not the SELECT result contains NULL values, check whether
dan870a0702016-08-01 16:37:43 +00002472 ** or not NULL is actually possible (it may not be, for example, due
dan7b35a772016-07-28 19:47:15 +00002473 ** to NOT NULL constraints in the schema). If no NULL values are possible,
dan870a0702016-08-01 16:37:43 +00002474 ** set prRhsHasNull to 0 before continuing. */
dan7b35a772016-07-28 19:47:15 +00002475 if( prRhsHasNull && (pX->flags & EP_xIsSelect) ){
2476 int i;
2477 ExprList *pEList = pX->x.pSelect->pEList;
2478 for(i=0; i<pEList->nExpr; i++){
2479 if( sqlite3ExprCanBeNull(pEList->a[i].pExpr) ) break;
2480 }
2481 if( i==pEList->nExpr ){
2482 prRhsHasNull = 0;
2483 }
2484 }
2485
drhb74b1012009-05-28 21:04:37 +00002486 /* Check to see if an existing table or index can be used to
2487 ** satisfy the query. This is preferable to generating a new
dan7b35a772016-07-28 19:47:15 +00002488 ** ephemeral table. */
2489 if( pParse->nErr==0 && (p = isCandidateForInOpt(pX))!=0 ){
danielk1977e1fb65a2009-04-02 17:23:32 +00002490 sqlite3 *db = pParse->db; /* Database connection */
drhb07028f2011-10-14 21:49:18 +00002491 Table *pTab; /* Table <table>. */
danba00e302016-07-23 20:24:06 +00002492 i16 iDb; /* Database idx for pTab */
dancfbb5e82016-07-13 19:48:13 +00002493 ExprList *pEList = p->pEList;
2494 int nExpr = pEList->nExpr;
drhb07028f2011-10-14 21:49:18 +00002495
drhb07028f2011-10-14 21:49:18 +00002496 assert( p->pEList!=0 ); /* Because of isCandidateForInOpt(p) */
2497 assert( p->pEList->a[0].pExpr!=0 ); /* Because of isCandidateForInOpt(p) */
2498 assert( p->pSrc!=0 ); /* Because of isCandidateForInOpt(p) */
2499 pTab = p->pSrc->a[0].pTab;
dancfbb5e82016-07-13 19:48:13 +00002500
drhb22f7c82014-02-06 23:56:27 +00002501 /* Code an OP_Transaction and OP_TableLock for <table>. */
danielk1977e1fb65a2009-04-02 17:23:32 +00002502 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
2503 sqlite3CodeVerifySchema(pParse, iDb);
2504 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
danielk19779a96b662007-11-29 17:05:18 +00002505
drha84a2832016-08-26 21:15:35 +00002506 assert(v); /* sqlite3GetVdbe() has always been previously called */
dancfbb5e82016-07-13 19:48:13 +00002507 if( nExpr==1 && pEList->a[0].pExpr->iColumn<0 ){
drh62659b22016-08-24 18:51:23 +00002508 /* The "x IN (SELECT rowid FROM table)" case */
drh511f9e82016-09-22 18:53:13 +00002509 int iAddr = sqlite3VdbeAddOp0(v, OP_Once);
drh7d176102014-02-18 03:07:12 +00002510 VdbeCoverage(v);
danielk19779a96b662007-11-29 17:05:18 +00002511
2512 sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
2513 eType = IN_INDEX_ROWID;
drhd8852092018-08-16 15:29:40 +00002514 ExplainQueryPlan((pParse, 0,
2515 "USING ROWID SEARCH ON TABLE %s FOR IN-OPERATOR",pTab->zName));
danielk19779a96b662007-11-29 17:05:18 +00002516 sqlite3VdbeJumpHere(v, iAddr);
2517 }else{
danielk1977e1fb65a2009-04-02 17:23:32 +00002518 Index *pIdx; /* Iterator variable */
dancfbb5e82016-07-13 19:48:13 +00002519 int affinity_ok = 1;
2520 int i;
2521
2522 /* Check that the affinity that will be used to perform each
drh62659b22016-08-24 18:51:23 +00002523 ** comparison is the same as the affinity of each column in table
2524 ** on the RHS of the IN operator. If it not, it is not possible to
2525 ** use any index of the RHS table. */
dancfbb5e82016-07-13 19:48:13 +00002526 for(i=0; i<nExpr && affinity_ok; i++){
drhfc7f27b2016-08-20 00:07:01 +00002527 Expr *pLhs = sqlite3VectorFieldSubexpr(pX->pLeft, i);
dancfbb5e82016-07-13 19:48:13 +00002528 int iCol = pEList->a[i].pExpr->iColumn;
drh0dfa4f62016-08-26 13:19:49 +00002529 char idxaff = sqlite3TableColumnAffinity(pTab,iCol); /* RHS table */
dancfbb5e82016-07-13 19:48:13 +00002530 char cmpaff = sqlite3CompareAffinity(pLhs, idxaff);
drh62659b22016-08-24 18:51:23 +00002531 testcase( cmpaff==SQLITE_AFF_BLOB );
2532 testcase( cmpaff==SQLITE_AFF_TEXT );
dancfbb5e82016-07-13 19:48:13 +00002533 switch( cmpaff ){
2534 case SQLITE_AFF_BLOB:
2535 break;
2536 case SQLITE_AFF_TEXT:
drh62659b22016-08-24 18:51:23 +00002537 /* sqlite3CompareAffinity() only returns TEXT if one side or the
2538 ** other has no affinity and the other side is TEXT. Hence,
2539 ** the only way for cmpaff to be TEXT is for idxaff to be TEXT
2540 ** and for the term on the LHS of the IN to have no affinity. */
2541 assert( idxaff==SQLITE_AFF_TEXT );
dancfbb5e82016-07-13 19:48:13 +00002542 break;
2543 default:
2544 affinity_ok = sqlite3IsNumericAffinity(idxaff);
2545 }
2546 }
danielk1977e1fb65a2009-04-02 17:23:32 +00002547
drha84a2832016-08-26 21:15:35 +00002548 if( affinity_ok ){
2549 /* Search for an existing index that will work for this IN operator */
2550 for(pIdx=pTab->pIndex; pIdx && eType==0; pIdx=pIdx->pNext){
2551 Bitmask colUsed; /* Columns of the index used */
2552 Bitmask mCol; /* Mask for the current column */
2553 if( pIdx->nColumn<nExpr ) continue;
drhd4a4a362018-12-08 20:30:31 +00002554 if( pIdx->pPartIdxWhere!=0 ) continue;
drha84a2832016-08-26 21:15:35 +00002555 /* Maximum nColumn is BMS-2, not BMS-1, so that we can compute
2556 ** BITMASK(nExpr) without overflowing */
2557 testcase( pIdx->nColumn==BMS-2 );
2558 testcase( pIdx->nColumn==BMS-1 );
2559 if( pIdx->nColumn>=BMS-1 ) continue;
2560 if( mustBeUnique ){
2561 if( pIdx->nKeyCol>nExpr
2562 ||(pIdx->nColumn>nExpr && !IsUniqueIndex(pIdx))
2563 ){
2564 continue; /* This index is not unique over the IN RHS columns */
dan7b35a772016-07-28 19:47:15 +00002565 }
danielk19770cdc0222008-06-26 18:04:03 +00002566 }
drha84a2832016-08-26 21:15:35 +00002567
2568 colUsed = 0; /* Columns of index used so far */
2569 for(i=0; i<nExpr; i++){
2570 Expr *pLhs = sqlite3VectorFieldSubexpr(pX->pLeft, i);
2571 Expr *pRhs = pEList->a[i].pExpr;
2572 CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pLhs, pRhs);
2573 int j;
2574
2575 assert( pReq!=0 || pRhs->iColumn==XN_ROWID || pParse->nErr );
2576 for(j=0; j<nExpr; j++){
2577 if( pIdx->aiColumn[j]!=pRhs->iColumn ) continue;
2578 assert( pIdx->azColl[j] );
drh106526e2016-08-26 22:09:01 +00002579 if( pReq!=0 && sqlite3StrICmp(pReq->zName, pIdx->azColl[j])!=0 ){
2580 continue;
2581 }
drha84a2832016-08-26 21:15:35 +00002582 break;
2583 }
2584 if( j==nExpr ) break;
2585 mCol = MASKBIT(j);
2586 if( mCol & colUsed ) break; /* Each column used only once */
2587 colUsed |= mCol;
2588 if( aiMap ) aiMap[i] = j;
2589 }
2590
2591 assert( i==nExpr || colUsed!=(MASKBIT(nExpr)-1) );
2592 if( colUsed==(MASKBIT(nExpr)-1) ){
2593 /* If we reach this point, that means the index pIdx is usable */
drh511f9e82016-09-22 18:53:13 +00002594 int iAddr = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
drhe2ca99c2018-05-02 00:33:43 +00002595 ExplainQueryPlan((pParse, 0,
2596 "USING INDEX %s FOR IN-OPERATOR",pIdx->zName));
drha84a2832016-08-26 21:15:35 +00002597 sqlite3VdbeAddOp3(v, OP_OpenRead, iTab, pIdx->tnum, iDb);
2598 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
2599 VdbeComment((v, "%s", pIdx->zName));
2600 assert( IN_INDEX_INDEX_DESC == IN_INDEX_INDEX_ASC+1 );
2601 eType = IN_INDEX_INDEX_ASC + pIdx->aSortOrder[0];
2602
2603 if( prRhsHasNull ){
drhb80dbdc2016-09-09 15:12:41 +00002604#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
drha84a2832016-08-26 21:15:35 +00002605 i64 mask = (1<<nExpr)-1;
2606 sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed,
2607 iTab, 0, 0, (u8*)&mask, P4_INT64);
drhb80dbdc2016-09-09 15:12:41 +00002608#endif
2609 *prRhsHasNull = ++pParse->nMem;
drha84a2832016-08-26 21:15:35 +00002610 if( nExpr==1 ){
2611 sqlite3SetHasNullFlag(v, iTab, *prRhsHasNull);
2612 }
2613 }
2614 sqlite3VdbeJumpHere(v, iAddr);
2615 }
2616 } /* End loop over indexes */
2617 } /* End if( affinity_ok ) */
2618 } /* End if not an rowid index */
2619 } /* End attempt to optimize using an index */
danielk19779a96b662007-11-29 17:05:18 +00002620
drhbb53ecb2014-08-02 21:03:33 +00002621 /* If no preexisting index is available for the IN clause
2622 ** and IN_INDEX_NOOP is an allowed reply
2623 ** and the RHS of the IN operator is a list, not a subquery
dan71c57db2016-07-09 20:23:55 +00002624 ** and the RHS is not constant or has two or fewer terms,
peter.d.reid60ec9142014-09-06 16:39:46 +00002625 ** then it is not worth creating an ephemeral table to evaluate
drhbb53ecb2014-08-02 21:03:33 +00002626 ** the IN operator so return IN_INDEX_NOOP.
2627 */
2628 if( eType==0
2629 && (inFlags & IN_INDEX_NOOP_OK)
2630 && !ExprHasProperty(pX, EP_xIsSelect)
2631 && (!sqlite3InRhsIsConstant(pX) || pX->x.pList->nExpr<=2)
2632 ){
2633 eType = IN_INDEX_NOOP;
2634 }
drhbb53ecb2014-08-02 21:03:33 +00002635
danielk19779a96b662007-11-29 17:05:18 +00002636 if( eType==0 ){
drh43870062014-07-31 15:44:44 +00002637 /* Could not find an existing table or index to use as the RHS b-tree.
drhb74b1012009-05-28 21:04:37 +00002638 ** We will have to generate an ephemeral table to do the job.
2639 */
drh8e23daf2013-06-11 13:30:04 +00002640 u32 savedNQueryLoop = pParse->nQueryLoop;
danielk19770cdc0222008-06-26 18:04:03 +00002641 int rMayHaveNull = 0;
danielk197741a05b72008-10-02 13:50:55 +00002642 eType = IN_INDEX_EPH;
drh3a856252014-08-01 14:46:57 +00002643 if( inFlags & IN_INDEX_LOOP ){
drh4a5acf82013-06-18 20:06:23 +00002644 pParse->nQueryLoop = 0;
drhe21a6e12014-08-01 18:00:24 +00002645 }else if( prRhsHasNull ){
2646 *prRhsHasNull = rMayHaveNull = ++pParse->nMem;
danielk19770cdc0222008-06-26 18:04:03 +00002647 }
drh85bcdce2018-12-23 21:27:29 +00002648 assert( pX->op==TK_IN );
drh50ef6712019-02-22 23:29:56 +00002649 sqlite3CodeRhsOfIN(pParse, pX, iTab);
drh85bcdce2018-12-23 21:27:29 +00002650 if( rMayHaveNull ){
drh2c041312018-12-24 02:34:49 +00002651 sqlite3SetHasNullFlag(v, iTab, rMayHaveNull);
drh85bcdce2018-12-23 21:27:29 +00002652 }
drhcf4d38a2010-07-28 02:53:36 +00002653 pParse->nQueryLoop = savedNQueryLoop;
danielk19779a96b662007-11-29 17:05:18 +00002654 }
danba00e302016-07-23 20:24:06 +00002655
2656 if( aiMap && eType!=IN_INDEX_INDEX_ASC && eType!=IN_INDEX_INDEX_DESC ){
2657 int i, n;
2658 n = sqlite3ExprVectorSize(pX->pLeft);
2659 for(i=0; i<n; i++) aiMap[i] = i;
2660 }
drh2c041312018-12-24 02:34:49 +00002661 *piTab = iTab;
danielk19779a96b662007-11-29 17:05:18 +00002662 return eType;
2663}
danielk1977284f4ac2007-12-10 05:03:46 +00002664#endif
drh626a8792005-01-17 22:08:19 +00002665
danf9b2e052016-08-02 17:45:00 +00002666#ifndef SQLITE_OMIT_SUBQUERY
dan553168c2016-08-01 20:14:31 +00002667/*
2668** Argument pExpr is an (?, ?...) IN(...) expression. This
2669** function allocates and returns a nul-terminated string containing
2670** the affinities to be used for each column of the comparison.
2671**
2672** It is the responsibility of the caller to ensure that the returned
2673** string is eventually freed using sqlite3DbFree().
2674*/
dan71c57db2016-07-09 20:23:55 +00002675static char *exprINAffinity(Parse *pParse, Expr *pExpr){
2676 Expr *pLeft = pExpr->pLeft;
2677 int nVal = sqlite3ExprVectorSize(pLeft);
dan553168c2016-08-01 20:14:31 +00002678 Select *pSelect = (pExpr->flags & EP_xIsSelect) ? pExpr->x.pSelect : 0;
dan71c57db2016-07-09 20:23:55 +00002679 char *zRet;
2680
dan553168c2016-08-01 20:14:31 +00002681 assert( pExpr->op==TK_IN );
drh5c258dc2017-02-16 17:18:07 +00002682 zRet = sqlite3DbMallocRaw(pParse->db, nVal+1);
dan71c57db2016-07-09 20:23:55 +00002683 if( zRet ){
2684 int i;
2685 for(i=0; i<nVal; i++){
drhfc7f27b2016-08-20 00:07:01 +00002686 Expr *pA = sqlite3VectorFieldSubexpr(pLeft, i);
dan553168c2016-08-01 20:14:31 +00002687 char a = sqlite3ExprAffinity(pA);
2688 if( pSelect ){
2689 zRet[i] = sqlite3CompareAffinity(pSelect->pEList->a[i].pExpr, a);
2690 }else{
2691 zRet[i] = a;
dan71c57db2016-07-09 20:23:55 +00002692 }
dan71c57db2016-07-09 20:23:55 +00002693 }
2694 zRet[nVal] = '\0';
2695 }
2696 return zRet;
2697}
danf9b2e052016-08-02 17:45:00 +00002698#endif
dan71c57db2016-07-09 20:23:55 +00002699
dan8da209b2016-07-26 18:06:08 +00002700#ifndef SQLITE_OMIT_SUBQUERY
2701/*
2702** Load the Parse object passed as the first argument with an error
2703** message of the form:
2704**
2705** "sub-select returns N columns - expected M"
2706*/
2707void sqlite3SubselectError(Parse *pParse, int nActual, int nExpect){
drha9ebfe22019-12-25 23:54:21 +00002708 if( pParse->nErr==0 ){
2709 const char *zFmt = "sub-select returns %d columns - expected %d";
2710 sqlite3ErrorMsg(pParse, zFmt, nActual, nExpect);
2711 }
dan8da209b2016-07-26 18:06:08 +00002712}
2713#endif
2714
drh626a8792005-01-17 22:08:19 +00002715/*
dan44c56042016-12-07 15:38:37 +00002716** Expression pExpr is a vector that has been used in a context where
2717** it is not permitted. If pExpr is a sub-select vector, this routine
2718** loads the Parse object with a message of the form:
2719**
2720** "sub-select returns N columns - expected 1"
2721**
2722** Or, if it is a regular scalar vector:
2723**
2724** "row value misused"
2725*/
2726void sqlite3VectorErrorMsg(Parse *pParse, Expr *pExpr){
2727#ifndef SQLITE_OMIT_SUBQUERY
2728 if( pExpr->flags & EP_xIsSelect ){
2729 sqlite3SubselectError(pParse, pExpr->x.pSelect->pEList->nExpr, 1);
2730 }else
2731#endif
2732 {
2733 sqlite3ErrorMsg(pParse, "row value misused");
2734 }
2735}
2736
drh85bcdce2018-12-23 21:27:29 +00002737#ifndef SQLITE_OMIT_SUBQUERY
dan44c56042016-12-07 15:38:37 +00002738/*
drh85bcdce2018-12-23 21:27:29 +00002739** Generate code that will construct an ephemeral table containing all terms
2740** in the RHS of an IN operator. The IN operator can be in either of two
2741** forms:
drh626a8792005-01-17 22:08:19 +00002742**
drh9cbe6352005-11-29 03:13:21 +00002743** x IN (4,5,11) -- IN operator with list on right-hand side
2744** x IN (SELECT a FROM b) -- IN operator with subquery on the right
drhfef52082000-06-06 01:50:43 +00002745**
drh2c041312018-12-24 02:34:49 +00002746** The pExpr parameter is the IN operator. The cursor number for the
2747** constructed ephermeral table is returned. The first time the ephemeral
2748** table is computed, the cursor number is also stored in pExpr->iTable,
2749** however the cursor number returned might not be the same, as it might
2750** have been duplicated using OP_OpenDup.
danielk197741a05b72008-10-02 13:50:55 +00002751**
drh85bcdce2018-12-23 21:27:29 +00002752** If the LHS expression ("x" in the examples) is a column value, or
2753** the SELECT statement returns a column value, then the affinity of that
2754** column is used to build the index keys. If both 'x' and the
2755** SELECT... statement are columns, then numeric affinity is used
2756** if either column has NUMERIC or INTEGER affinity. If neither
2757** 'x' nor the SELECT... statement are columns, then numeric affinity
2758** is used.
drhcce7d172000-05-31 15:34:51 +00002759*/
drh85bcdce2018-12-23 21:27:29 +00002760void sqlite3CodeRhsOfIN(
drhfd773cf2009-05-29 14:39:07 +00002761 Parse *pParse, /* Parsing context */
drh85bcdce2018-12-23 21:27:29 +00002762 Expr *pExpr, /* The IN operator */
drh50ef6712019-02-22 23:29:56 +00002763 int iTab /* Use this cursor number */
danielk197741a05b72008-10-02 13:50:55 +00002764){
drh2c041312018-12-24 02:34:49 +00002765 int addrOnce = 0; /* Address of the OP_Once instruction at top */
drh85bcdce2018-12-23 21:27:29 +00002766 int addr; /* Address of OP_OpenEphemeral instruction */
2767 Expr *pLeft; /* the LHS of the IN operator */
2768 KeyInfo *pKeyInfo = 0; /* Key information */
2769 int nVal; /* Size of vector pLeft */
2770 Vdbe *v; /* The prepared statement under construction */
danielk1977fc976062007-05-10 10:46:56 +00002771
drh2c041312018-12-24 02:34:49 +00002772 v = pParse->pVdbe;
drh85bcdce2018-12-23 21:27:29 +00002773 assert( v!=0 );
2774
drh2c041312018-12-24 02:34:49 +00002775 /* The evaluation of the IN must be repeated every time it
drh39a11812016-08-19 19:12:58 +00002776 ** is encountered if any of the following is true:
drh57dbd7b2005-07-08 18:25:26 +00002777 **
2778 ** * The right-hand side is a correlated subquery
2779 ** * The right-hand side is an expression list containing variables
2780 ** * We are inside a trigger
2781 **
drh2c041312018-12-24 02:34:49 +00002782 ** If all of the above are false, then we can compute the RHS just once
2783 ** and reuse it many names.
danielk1977b3bce662005-01-29 08:32:43 +00002784 */
drhefb699f2018-12-24 11:55:44 +00002785 if( !ExprHasProperty(pExpr, EP_VarSelect) && pParse->iSelfTab==0 ){
drh2c041312018-12-24 02:34:49 +00002786 /* Reuse of the RHS is allowed */
2787 /* If this routine has already been coded, but the previous code
2788 ** might not have been invoked yet, so invoke it now as a subroutine.
2789 */
2790 if( ExprHasProperty(pExpr, EP_Subrtn) ){
drhf9231c32018-12-31 21:43:55 +00002791 addrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
drhbd462bc2018-12-24 20:21:06 +00002792 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
2793 ExplainQueryPlan((pParse, 0, "REUSE LIST SUBQUERY %d",
2794 pExpr->x.pSelect->selId));
2795 }
drh2c041312018-12-24 02:34:49 +00002796 sqlite3VdbeAddOp2(v, OP_Gosub, pExpr->y.sub.regReturn,
2797 pExpr->y.sub.iAddr);
2798 sqlite3VdbeAddOp2(v, OP_OpenDup, iTab, pExpr->iTable);
drhf9231c32018-12-31 21:43:55 +00002799 sqlite3VdbeJumpHere(v, addrOnce);
drh2c041312018-12-24 02:34:49 +00002800 return;
2801 }
2802
2803 /* Begin coding the subroutine */
2804 ExprSetProperty(pExpr, EP_Subrtn);
2805 pExpr->y.sub.regReturn = ++pParse->nMem;
2806 pExpr->y.sub.iAddr =
2807 sqlite3VdbeAddOp2(v, OP_Integer, 0, pExpr->y.sub.regReturn) + 1;
2808 VdbeComment((v, "return address"));
2809
2810 addrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
danielk1977b3bce662005-01-29 08:32:43 +00002811 }
2812
drh85bcdce2018-12-23 21:27:29 +00002813 /* Check to see if this is a vector IN operator */
2814 pLeft = pExpr->pLeft;
2815 nVal = sqlite3ExprVectorSize(pLeft);
danielk1977e014a832004-05-17 10:48:57 +00002816
drh85bcdce2018-12-23 21:27:29 +00002817 /* Construct the ephemeral table that will contain the content of
2818 ** RHS of the IN operator.
2819 */
drh2c041312018-12-24 02:34:49 +00002820 pExpr->iTable = iTab;
drh50ef6712019-02-22 23:29:56 +00002821 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pExpr->iTable, nVal);
drh2c041312018-12-24 02:34:49 +00002822#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
2823 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
2824 VdbeComment((v, "Result of SELECT %u", pExpr->x.pSelect->selId));
2825 }else{
2826 VdbeComment((v, "RHS of IN operator"));
2827 }
2828#endif
drh50ef6712019-02-22 23:29:56 +00002829 pKeyInfo = sqlite3KeyInfoAlloc(pParse->db, nVal, 1);
danielk1977e014a832004-05-17 10:48:57 +00002830
drh85bcdce2018-12-23 21:27:29 +00002831 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
2832 /* Case 1: expr IN (SELECT ...)
2833 **
2834 ** Generate code to write the results of the select into the temporary
2835 ** table allocated and opened above.
2836 */
2837 Select *pSelect = pExpr->x.pSelect;
2838 ExprList *pEList = pSelect->pEList;
drh1013c932008-01-06 00:25:21 +00002839
drh2c041312018-12-24 02:34:49 +00002840 ExplainQueryPlan((pParse, 1, "%sLIST SUBQUERY %d",
2841 addrOnce?"":"CORRELATED ", pSelect->selId
drh85bcdce2018-12-23 21:27:29 +00002842 ));
drh85bcdce2018-12-23 21:27:29 +00002843 /* If the LHS and RHS of the IN operator do not match, that
2844 ** error will have been caught long before we reach this point. */
2845 if( ALWAYS(pEList->nExpr==nVal) ){
2846 SelectDest dest;
2847 int i;
drhbd462bc2018-12-24 20:21:06 +00002848 sqlite3SelectDestInit(&dest, SRT_Set, iTab);
drh85bcdce2018-12-23 21:27:29 +00002849 dest.zAffSdst = exprINAffinity(pParse, pExpr);
2850 pSelect->iLimit = 0;
2851 testcase( pSelect->selFlags & SF_Distinct );
2852 testcase( pKeyInfo==0 ); /* Caused by OOM in sqlite3KeyInfoAlloc() */
2853 if( sqlite3Select(pParse, pSelect, &dest) ){
2854 sqlite3DbFree(pParse->db, dest.zAffSdst);
2855 sqlite3KeyInfoUnref(pKeyInfo);
2856 return;
drhfef52082000-06-06 01:50:43 +00002857 }
drh85bcdce2018-12-23 21:27:29 +00002858 sqlite3DbFree(pParse->db, dest.zAffSdst);
2859 assert( pKeyInfo!=0 ); /* OOM will cause exit after sqlite3Select() */
2860 assert( pEList!=0 );
2861 assert( pEList->nExpr>0 );
2862 assert( sqlite3KeyInfoIsWriteable(pKeyInfo) );
2863 for(i=0; i<nVal; i++){
2864 Expr *p = sqlite3VectorFieldSubexpr(pLeft, i);
2865 pKeyInfo->aColl[i] = sqlite3BinaryCompareCollSeq(
2866 pParse, p, pEList->a[i].pExpr
2867 );
danielk197741a05b72008-10-02 13:50:55 +00002868 }
drh85bcdce2018-12-23 21:27:29 +00002869 }
2870 }else if( ALWAYS(pExpr->x.pList!=0) ){
2871 /* Case 2: expr IN (exprlist)
2872 **
2873 ** For each expression, build an index key from the evaluation and
2874 ** store it in the temporary table. If <expr> is a column, then use
2875 ** that columns affinity when building index keys. If <expr> is not
2876 ** a column, use numeric affinity.
2877 */
2878 char affinity; /* Affinity of the LHS of the IN */
2879 int i;
2880 ExprList *pList = pExpr->x.pList;
2881 struct ExprList_item *pItem;
danc324d442019-08-17 19:13:49 +00002882 int r1, r2;
drh85bcdce2018-12-23 21:27:29 +00002883 affinity = sqlite3ExprAffinity(pLeft);
drh96fb16e2019-08-06 14:37:24 +00002884 if( affinity<=SQLITE_AFF_NONE ){
drh85bcdce2018-12-23 21:27:29 +00002885 affinity = SQLITE_AFF_BLOB;
2886 }
2887 if( pKeyInfo ){
2888 assert( sqlite3KeyInfoIsWriteable(pKeyInfo) );
2889 pKeyInfo->aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
drhfef52082000-06-06 01:50:43 +00002890 }
2891
drh85bcdce2018-12-23 21:27:29 +00002892 /* Loop through each expression in <exprlist>. */
2893 r1 = sqlite3GetTempReg(pParse);
2894 r2 = sqlite3GetTempReg(pParse);
drh85bcdce2018-12-23 21:27:29 +00002895 for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
2896 Expr *pE2 = pItem->pExpr;
drh85bcdce2018-12-23 21:27:29 +00002897
2898 /* If the expression is not constant then we will need to
2899 ** disable the test that was generated above that makes sure
2900 ** this code only executes once. Because for a non-constant
2901 ** expression we need to rerun this code each time.
drhfef52082000-06-06 01:50:43 +00002902 */
drh2c041312018-12-24 02:34:49 +00002903 if( addrOnce && !sqlite3ExprIsConstant(pE2) ){
2904 sqlite3VdbeChangeToNoop(v, addrOnce);
dan7ac0e562019-05-18 21:22:25 +00002905 ExprClearProperty(pExpr, EP_Subrtn);
drh2c041312018-12-24 02:34:49 +00002906 addrOnce = 0;
drh85bcdce2018-12-23 21:27:29 +00002907 }
drh1398ad32005-01-19 23:24:50 +00002908
drh85bcdce2018-12-23 21:27:29 +00002909 /* Evaluate the expression and insert it into the temp table */
danc324d442019-08-17 19:13:49 +00002910 sqlite3ExprCode(pParse, pE2, r1);
2911 sqlite3VdbeAddOp4(v, OP_MakeRecord, r1, 1, r2, &affinity, 1);
2912 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iTab, r2, r1, 1);
drhcce7d172000-05-31 15:34:51 +00002913 }
drh85bcdce2018-12-23 21:27:29 +00002914 sqlite3ReleaseTempReg(pParse, r1);
2915 sqlite3ReleaseTempReg(pParse, r2);
drhcce7d172000-05-31 15:34:51 +00002916 }
drh85bcdce2018-12-23 21:27:29 +00002917 if( pKeyInfo ){
2918 sqlite3VdbeChangeP4(v, addr, (void *)pKeyInfo, P4_KEYINFO);
2919 }
drh2c041312018-12-24 02:34:49 +00002920 if( addrOnce ){
2921 sqlite3VdbeJumpHere(v, addrOnce);
2922 /* Subroutine return */
2923 sqlite3VdbeAddOp1(v, OP_Return, pExpr->y.sub.regReturn);
2924 sqlite3VdbeChangeP1(v, pExpr->y.sub.iAddr-1, sqlite3VdbeCurrentAddr(v)-1);
drh6d2566d2019-09-18 20:34:54 +00002925 sqlite3ClearTempRegCache(pParse);
drh85bcdce2018-12-23 21:27:29 +00002926 }
2927}
2928#endif /* SQLITE_OMIT_SUBQUERY */
danielk1977b3bce662005-01-29 08:32:43 +00002929
drh85bcdce2018-12-23 21:27:29 +00002930/*
2931** Generate code for scalar subqueries used as a subquery expression
2932** or EXISTS operator:
2933**
2934** (SELECT a FROM b) -- subquery
2935** EXISTS (SELECT a FROM b) -- EXISTS subquery
2936**
2937** The pExpr parameter is the SELECT or EXISTS operator to be coded.
2938**
drhd86fe442019-08-26 13:45:49 +00002939** Return the register that holds the result. For a multi-column SELECT,
drh85bcdce2018-12-23 21:27:29 +00002940** the result is stored in a contiguous array of registers and the
2941** return value is the register of the left-most result column.
2942** Return 0 if an error occurs.
2943*/
2944#ifndef SQLITE_OMIT_SUBQUERY
2945int sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){
drh2c041312018-12-24 02:34:49 +00002946 int addrOnce = 0; /* Address of OP_Once at top of subroutine */
drh85bcdce2018-12-23 21:27:29 +00002947 int rReg = 0; /* Register storing resulting */
2948 Select *pSel; /* SELECT statement to encode */
2949 SelectDest dest; /* How to deal with SELECT result */
2950 int nReg; /* Registers to allocate */
2951 Expr *pLimit; /* New limit expression */
drh2c041312018-12-24 02:34:49 +00002952
2953 Vdbe *v = pParse->pVdbe;
drh85bcdce2018-12-23 21:27:29 +00002954 assert( v!=0 );
drhbd462bc2018-12-24 20:21:06 +00002955 testcase( pExpr->op==TK_EXISTS );
2956 testcase( pExpr->op==TK_SELECT );
2957 assert( pExpr->op==TK_EXISTS || pExpr->op==TK_SELECT );
2958 assert( ExprHasProperty(pExpr, EP_xIsSelect) );
2959 pSel = pExpr->x.pSelect;
drh85bcdce2018-12-23 21:27:29 +00002960
drh5198ff52018-12-24 12:09:47 +00002961 /* The evaluation of the EXISTS/SELECT must be repeated every time it
drh85bcdce2018-12-23 21:27:29 +00002962 ** is encountered if any of the following is true:
2963 **
2964 ** * The right-hand side is a correlated subquery
2965 ** * The right-hand side is an expression list containing variables
2966 ** * We are inside a trigger
2967 **
2968 ** If all of the above are false, then we can run this code just once
2969 ** save the results, and reuse the same result on subsequent invocations.
2970 */
2971 if( !ExprHasProperty(pExpr, EP_VarSelect) ){
drh5198ff52018-12-24 12:09:47 +00002972 /* If this routine has already been coded, then invoke it as a
2973 ** subroutine. */
2974 if( ExprHasProperty(pExpr, EP_Subrtn) ){
drhbd462bc2018-12-24 20:21:06 +00002975 ExplainQueryPlan((pParse, 0, "REUSE SUBQUERY %d", pSel->selId));
drh5198ff52018-12-24 12:09:47 +00002976 sqlite3VdbeAddOp2(v, OP_Gosub, pExpr->y.sub.regReturn,
2977 pExpr->y.sub.iAddr);
2978 return pExpr->iTable;
2979 }
2980
2981 /* Begin coding the subroutine */
2982 ExprSetProperty(pExpr, EP_Subrtn);
2983 pExpr->y.sub.regReturn = ++pParse->nMem;
2984 pExpr->y.sub.iAddr =
2985 sqlite3VdbeAddOp2(v, OP_Integer, 0, pExpr->y.sub.regReturn) + 1;
2986 VdbeComment((v, "return address"));
2987
drh2c041312018-12-24 02:34:49 +00002988 addrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
danielk1977b3bce662005-01-29 08:32:43 +00002989 }
drh85bcdce2018-12-23 21:27:29 +00002990
2991 /* For a SELECT, generate code to put the values for all columns of
2992 ** the first row into an array of registers and return the index of
2993 ** the first register.
2994 **
2995 ** If this is an EXISTS, write an integer 0 (not exists) or 1 (exists)
2996 ** into a register and return that register number.
2997 **
2998 ** In both cases, the query is augmented with "LIMIT 1". Any
2999 ** preexisting limit is discarded in place of the new LIMIT 1.
3000 */
drhbd462bc2018-12-24 20:21:06 +00003001 ExplainQueryPlan((pParse, 1, "%sSCALAR SUBQUERY %d",
3002 addrOnce?"":"CORRELATED ", pSel->selId));
drh85bcdce2018-12-23 21:27:29 +00003003 nReg = pExpr->op==TK_SELECT ? pSel->pEList->nExpr : 1;
3004 sqlite3SelectDestInit(&dest, 0, pParse->nMem+1);
3005 pParse->nMem += nReg;
3006 if( pExpr->op==TK_SELECT ){
3007 dest.eDest = SRT_Mem;
3008 dest.iSdst = dest.iSDParm;
3009 dest.nSdst = nReg;
3010 sqlite3VdbeAddOp3(v, OP_Null, 0, dest.iSDParm, dest.iSDParm+nReg-1);
3011 VdbeComment((v, "Init subquery result"));
3012 }else{
3013 dest.eDest = SRT_Exists;
3014 sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iSDParm);
3015 VdbeComment((v, "Init EXISTS result"));
3016 }
drh85bcdce2018-12-23 21:27:29 +00003017 if( pSel->pLimit ){
drh7ca13472019-09-23 11:55:22 +00003018 /* The subquery already has a limit. If the pre-existing limit is X
3019 ** then make the new limit X<>0 so that the new limit is either 1 or 0 */
3020 sqlite3 *db = pParse->db;
drh5776ee52019-09-23 12:38:10 +00003021 pLimit = sqlite3Expr(db, TK_INTEGER, "0");
drh7ca13472019-09-23 11:55:22 +00003022 if( pLimit ){
3023 pLimit->affExpr = SQLITE_AFF_NUMERIC;
3024 pLimit = sqlite3PExpr(pParse, TK_NE,
3025 sqlite3ExprDup(db, pSel->pLimit->pLeft, 0), pLimit);
3026 }
3027 sqlite3ExprDelete(db, pSel->pLimit->pLeft);
drh85bcdce2018-12-23 21:27:29 +00003028 pSel->pLimit->pLeft = pLimit;
3029 }else{
drh7ca13472019-09-23 11:55:22 +00003030 /* If there is no pre-existing limit add a limit of 1 */
drh5776ee52019-09-23 12:38:10 +00003031 pLimit = sqlite3Expr(pParse->db, TK_INTEGER, "1");
drh85bcdce2018-12-23 21:27:29 +00003032 pSel->pLimit = sqlite3PExpr(pParse, TK_LIMIT, pLimit, 0);
3033 }
3034 pSel->iLimit = 0;
3035 if( sqlite3Select(pParse, pSel, &dest) ){
3036 return 0;
3037 }
drh2c041312018-12-24 02:34:49 +00003038 pExpr->iTable = rReg = dest.iSDParm;
drh85bcdce2018-12-23 21:27:29 +00003039 ExprSetVVAProperty(pExpr, EP_NoReduce);
drh2c041312018-12-24 02:34:49 +00003040 if( addrOnce ){
3041 sqlite3VdbeJumpHere(v, addrOnce);
danielk1977fc976062007-05-10 10:46:56 +00003042
drh5198ff52018-12-24 12:09:47 +00003043 /* Subroutine return */
3044 sqlite3VdbeAddOp1(v, OP_Return, pExpr->y.sub.regReturn);
3045 sqlite3VdbeChangeP1(v, pExpr->y.sub.iAddr-1, sqlite3VdbeCurrentAddr(v)-1);
drh6d2566d2019-09-18 20:34:54 +00003046 sqlite3ClearTempRegCache(pParse);
drh5198ff52018-12-24 12:09:47 +00003047 }
drh2c041312018-12-24 02:34:49 +00003048
drh1450bc62009-10-30 13:25:56 +00003049 return rReg;
drhcce7d172000-05-31 15:34:51 +00003050}
drh51522cd2005-01-20 13:36:19 +00003051#endif /* SQLITE_OMIT_SUBQUERY */
drhcce7d172000-05-31 15:34:51 +00003052
drhe3365e62009-11-12 17:52:24 +00003053#ifndef SQLITE_OMIT_SUBQUERY
3054/*
dan7b35a772016-07-28 19:47:15 +00003055** Expr pIn is an IN(...) expression. This function checks that the
3056** sub-select on the RHS of the IN() operator has the same number of
3057** columns as the vector on the LHS. Or, if the RHS of the IN() is not
3058** a sub-query, that the LHS is a vector of size 1.
3059*/
3060int sqlite3ExprCheckIN(Parse *pParse, Expr *pIn){
3061 int nVector = sqlite3ExprVectorSize(pIn->pLeft);
3062 if( (pIn->flags & EP_xIsSelect) ){
3063 if( nVector!=pIn->x.pSelect->pEList->nExpr ){
3064 sqlite3SubselectError(pParse, pIn->x.pSelect->pEList->nExpr, nVector);
3065 return 1;
3066 }
3067 }else if( nVector!=1 ){
dan44c56042016-12-07 15:38:37 +00003068 sqlite3VectorErrorMsg(pParse, pIn->pLeft);
dan7b35a772016-07-28 19:47:15 +00003069 return 1;
3070 }
3071 return 0;
3072}
3073#endif
3074
3075#ifndef SQLITE_OMIT_SUBQUERY
3076/*
drhe3365e62009-11-12 17:52:24 +00003077** Generate code for an IN expression.
3078**
3079** x IN (SELECT ...)
3080** x IN (value, value, ...)
3081**
drhecb87ac2016-08-25 15:46:25 +00003082** The left-hand side (LHS) is a scalar or vector expression. The
drhe347d3e2016-08-25 21:14:34 +00003083** right-hand side (RHS) is an array of zero or more scalar values, or a
3084** subquery. If the RHS is a subquery, the number of result columns must
3085** match the number of columns in the vector on the LHS. If the RHS is
3086** a list of values, the LHS must be a scalar.
3087**
3088** The IN operator is true if the LHS value is contained within the RHS.
3089** The result is false if the LHS is definitely not in the RHS. The
3090** result is NULL if the presence of the LHS in the RHS cannot be
3091** determined due to NULLs.
drhe3365e62009-11-12 17:52:24 +00003092**
drh6be515e2014-08-01 21:00:53 +00003093** This routine generates code that jumps to destIfFalse if the LHS is not
drhe3365e62009-11-12 17:52:24 +00003094** contained within the RHS. If due to NULLs we cannot determine if the LHS
3095** is contained in the RHS then jump to destIfNull. If the LHS is contained
3096** within the RHS then fall through.
drhecb87ac2016-08-25 15:46:25 +00003097**
3098** See the separate in-operator.md documentation file in the canonical
3099** SQLite source tree for additional information.
drhe3365e62009-11-12 17:52:24 +00003100*/
3101static void sqlite3ExprCodeIN(
3102 Parse *pParse, /* Parsing and code generating context */
3103 Expr *pExpr, /* The IN expression */
3104 int destIfFalse, /* Jump here if LHS is not contained in the RHS */
3105 int destIfNull /* Jump here if the results are unknown due to NULLs */
3106){
3107 int rRhsHasNull = 0; /* Register that is true if RHS contains NULL values */
drhe3365e62009-11-12 17:52:24 +00003108 int eType; /* Type of the RHS */
drhe347d3e2016-08-25 21:14:34 +00003109 int rLhs; /* Register(s) holding the LHS values */
3110 int rLhsOrig; /* LHS values prior to reordering by aiMap[] */
drhe3365e62009-11-12 17:52:24 +00003111 Vdbe *v; /* Statement under construction */
danba00e302016-07-23 20:24:06 +00003112 int *aiMap = 0; /* Map from vector field to index column */
3113 char *zAff = 0; /* Affinity string for comparisons */
drhe347d3e2016-08-25 21:14:34 +00003114 int nVector; /* Size of vectors for this IN operator */
3115 int iDummy; /* Dummy parameter to exprCodeVector() */
3116 Expr *pLeft; /* The LHS of the IN operator */
3117 int i; /* loop counter */
3118 int destStep2; /* Where to jump when NULLs seen in step 2 */
3119 int destStep6 = 0; /* Start of code for Step 6 */
3120 int addrTruthOp; /* Address of opcode that determines the IN is true */
3121 int destNotNull; /* Jump here if a comparison is not true in step 6 */
3122 int addrTop; /* Top of the step-6 loop */
drh2c041312018-12-24 02:34:49 +00003123 int iTab = 0; /* Index to use */
drhe3365e62009-11-12 17:52:24 +00003124
drhe347d3e2016-08-25 21:14:34 +00003125 pLeft = pExpr->pLeft;
dan7b35a772016-07-28 19:47:15 +00003126 if( sqlite3ExprCheckIN(pParse, pExpr) ) return;
dan553168c2016-08-01 20:14:31 +00003127 zAff = exprINAffinity(pParse, pExpr);
danba00e302016-07-23 20:24:06 +00003128 nVector = sqlite3ExprVectorSize(pExpr->pLeft);
3129 aiMap = (int*)sqlite3DbMallocZero(
3130 pParse->db, nVector*(sizeof(int) + sizeof(char)) + 1
3131 );
drhe347d3e2016-08-25 21:14:34 +00003132 if( pParse->db->mallocFailed ) goto sqlite3ExprCodeIN_oom_error;
dan7b35a772016-07-28 19:47:15 +00003133
danba00e302016-07-23 20:24:06 +00003134 /* Attempt to compute the RHS. After this step, if anything other than
drh2c041312018-12-24 02:34:49 +00003135 ** IN_INDEX_NOOP is returned, the table opened with cursor iTab
danba00e302016-07-23 20:24:06 +00003136 ** contains the values that make up the RHS. If IN_INDEX_NOOP is returned,
3137 ** the RHS has not yet been coded. */
drhe3365e62009-11-12 17:52:24 +00003138 v = pParse->pVdbe;
3139 assert( v!=0 ); /* OOM detected prior to this routine */
3140 VdbeNoopComment((v, "begin IN expr"));
drhbb53ecb2014-08-02 21:03:33 +00003141 eType = sqlite3FindInIndex(pParse, pExpr,
3142 IN_INDEX_MEMBERSHIP | IN_INDEX_NOOP_OK,
drh2c041312018-12-24 02:34:49 +00003143 destIfFalse==destIfNull ? 0 : &rRhsHasNull,
3144 aiMap, &iTab);
drhe3365e62009-11-12 17:52:24 +00003145
danba00e302016-07-23 20:24:06 +00003146 assert( pParse->nErr || nVector==1 || eType==IN_INDEX_EPH
3147 || eType==IN_INDEX_INDEX_ASC || eType==IN_INDEX_INDEX_DESC
3148 );
drhecb87ac2016-08-25 15:46:25 +00003149#ifdef SQLITE_DEBUG
3150 /* Confirm that aiMap[] contains nVector integer values between 0 and
3151 ** nVector-1. */
3152 for(i=0; i<nVector; i++){
3153 int j, cnt;
3154 for(cnt=j=0; j<nVector; j++) if( aiMap[j]==i ) cnt++;
3155 assert( cnt==1 );
3156 }
3157#endif
drhe3365e62009-11-12 17:52:24 +00003158
danba00e302016-07-23 20:24:06 +00003159 /* Code the LHS, the <expr> from "<expr> IN (...)". If the LHS is a
3160 ** vector, then it is stored in an array of nVector registers starting
3161 ** at r1.
drhe347d3e2016-08-25 21:14:34 +00003162 **
3163 ** sqlite3FindInIndex() might have reordered the fields of the LHS vector
3164 ** so that the fields are in the same order as an existing index. The
3165 ** aiMap[] array contains a mapping from the original LHS field order to
3166 ** the field order that matches the RHS index.
drhe3365e62009-11-12 17:52:24 +00003167 */
drhe347d3e2016-08-25 21:14:34 +00003168 rLhsOrig = exprCodeVector(pParse, pLeft, &iDummy);
3169 for(i=0; i<nVector && aiMap[i]==i; i++){} /* Are LHS fields reordered? */
drhecb87ac2016-08-25 15:46:25 +00003170 if( i==nVector ){
drhe347d3e2016-08-25 21:14:34 +00003171 /* LHS fields are not reordered */
3172 rLhs = rLhsOrig;
drhecb87ac2016-08-25 15:46:25 +00003173 }else{
3174 /* Need to reorder the LHS fields according to aiMap */
drhe347d3e2016-08-25 21:14:34 +00003175 rLhs = sqlite3GetTempRange(pParse, nVector);
drhecb87ac2016-08-25 15:46:25 +00003176 for(i=0; i<nVector; i++){
drhe347d3e2016-08-25 21:14:34 +00003177 sqlite3VdbeAddOp3(v, OP_Copy, rLhsOrig+i, rLhs+aiMap[i], 0);
drhecb87ac2016-08-25 15:46:25 +00003178 }
danba00e302016-07-23 20:24:06 +00003179 }
drhe3365e62009-11-12 17:52:24 +00003180
drhbb53ecb2014-08-02 21:03:33 +00003181 /* If sqlite3FindInIndex() did not find or create an index that is
3182 ** suitable for evaluating the IN operator, then evaluate using a
3183 ** sequence of comparisons.
drhe347d3e2016-08-25 21:14:34 +00003184 **
3185 ** This is step (1) in the in-operator.md optimized algorithm.
drh094430e2010-07-14 18:24:06 +00003186 */
drhbb53ecb2014-08-02 21:03:33 +00003187 if( eType==IN_INDEX_NOOP ){
3188 ExprList *pList = pExpr->x.pList;
3189 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
drhec4ccdb2018-12-29 02:26:59 +00003190 int labelOk = sqlite3VdbeMakeLabel(pParse);
drhbb53ecb2014-08-02 21:03:33 +00003191 int r2, regToFree;
3192 int regCkNull = 0;
3193 int ii;
drhdd668c22019-09-02 02:21:58 +00003194 int bLhsReal; /* True if the LHS of the IN has REAL affinity */
drhbb53ecb2014-08-02 21:03:33 +00003195 assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
drhbb53ecb2014-08-02 21:03:33 +00003196 if( destIfNull!=destIfFalse ){
3197 regCkNull = sqlite3GetTempReg(pParse);
drhe347d3e2016-08-25 21:14:34 +00003198 sqlite3VdbeAddOp3(v, OP_BitAnd, rLhs, rLhs, regCkNull);
drhbb53ecb2014-08-02 21:03:33 +00003199 }
drhdd668c22019-09-02 02:21:58 +00003200 bLhsReal = sqlite3ExprAffinity(pExpr->pLeft)==SQLITE_AFF_REAL;
drhbb53ecb2014-08-02 21:03:33 +00003201 for(ii=0; ii<pList->nExpr; ii++){
drhdd668c22019-09-02 02:21:58 +00003202 if( bLhsReal ){
drh4fc83652019-09-02 22:13:06 +00003203 r2 = regToFree = sqlite3GetTempReg(pParse);
3204 sqlite3ExprCode(pParse, pList->a[ii].pExpr, r2);
drhdd668c22019-09-02 02:21:58 +00003205 sqlite3VdbeAddOp4(v, OP_Affinity, r2, 1, 0, "E", P4_STATIC);
drh4fc83652019-09-02 22:13:06 +00003206 }else{
3207 r2 = sqlite3ExprCodeTemp(pParse, pList->a[ii].pExpr, &regToFree);
drhdd668c22019-09-02 02:21:58 +00003208 }
drha9769792014-08-04 16:39:39 +00003209 if( regCkNull && sqlite3ExprCanBeNull(pList->a[ii].pExpr) ){
drhbb53ecb2014-08-02 21:03:33 +00003210 sqlite3VdbeAddOp3(v, OP_BitAnd, regCkNull, r2, regCkNull);
3211 }
3212 if( ii<pList->nExpr-1 || destIfNull!=destIfFalse ){
drh47994882019-12-22 23:48:36 +00003213 int op = rLhs!=r2 ? OP_Eq : OP_NotNull;
3214 sqlite3VdbeAddOp4(v, op, rLhs, labelOk, r2,
drh4336b0e2014-08-05 00:53:51 +00003215 (void*)pColl, P4_COLLSEQ);
drh47994882019-12-22 23:48:36 +00003216 VdbeCoverageIf(v, ii<pList->nExpr-1 && op==OP_Eq);
3217 VdbeCoverageIf(v, ii==pList->nExpr-1 && op==OP_Eq);
3218 VdbeCoverageIf(v, ii<pList->nExpr-1 && op==OP_NotNull);
3219 VdbeCoverageIf(v, ii==pList->nExpr-1 && op==OP_NotNull);
danba00e302016-07-23 20:24:06 +00003220 sqlite3VdbeChangeP5(v, zAff[0]);
drhbb53ecb2014-08-02 21:03:33 +00003221 }else{
drh47994882019-12-22 23:48:36 +00003222 int op = rLhs!=r2 ? OP_Ne : OP_IsNull;
drhbb53ecb2014-08-02 21:03:33 +00003223 assert( destIfNull==destIfFalse );
drh47994882019-12-22 23:48:36 +00003224 sqlite3VdbeAddOp4(v, op, rLhs, destIfFalse, r2,
3225 (void*)pColl, P4_COLLSEQ);
3226 VdbeCoverageIf(v, op==OP_Ne);
3227 VdbeCoverageIf(v, op==OP_IsNull);
danba00e302016-07-23 20:24:06 +00003228 sqlite3VdbeChangeP5(v, zAff[0] | SQLITE_JUMPIFNULL);
drhbb53ecb2014-08-02 21:03:33 +00003229 }
3230 sqlite3ReleaseTempReg(pParse, regToFree);
3231 }
3232 if( regCkNull ){
3233 sqlite3VdbeAddOp2(v, OP_IsNull, regCkNull, destIfNull); VdbeCoverage(v);
drh076e85f2015-09-03 13:46:12 +00003234 sqlite3VdbeGoto(v, destIfFalse);
drhbb53ecb2014-08-02 21:03:33 +00003235 }
3236 sqlite3VdbeResolveLabel(v, labelOk);
3237 sqlite3ReleaseTempReg(pParse, regCkNull);
drhe347d3e2016-08-25 21:14:34 +00003238 goto sqlite3ExprCodeIN_finished;
3239 }
3240
3241 /* Step 2: Check to see if the LHS contains any NULL columns. If the
3242 ** LHS does contain NULLs then the result must be either FALSE or NULL.
3243 ** We will then skip the binary search of the RHS.
3244 */
3245 if( destIfNull==destIfFalse ){
3246 destStep2 = destIfFalse;
drh094430e2010-07-14 18:24:06 +00003247 }else{
drhec4ccdb2018-12-29 02:26:59 +00003248 destStep2 = destStep6 = sqlite3VdbeMakeLabel(pParse);
drhe347d3e2016-08-25 21:14:34 +00003249 }
drh4eac5f02019-12-24 15:01:17 +00003250 if( pParse->nErr ) goto sqlite3ExprCodeIN_finished;
drhe347d3e2016-08-25 21:14:34 +00003251 for(i=0; i<nVector; i++){
3252 Expr *p = sqlite3VectorFieldSubexpr(pExpr->pLeft, i);
3253 if( sqlite3ExprCanBeNull(p) ){
3254 sqlite3VdbeAddOp2(v, OP_IsNull, rLhs+i, destStep2);
dand49fd4e2016-07-27 19:33:04 +00003255 VdbeCoverage(v);
drhe3365e62009-11-12 17:52:24 +00003256 }
drhe3365e62009-11-12 17:52:24 +00003257 }
drhe347d3e2016-08-25 21:14:34 +00003258
3259 /* Step 3. The LHS is now known to be non-NULL. Do the binary search
3260 ** of the RHS using the LHS as a probe. If found, the result is
3261 ** true.
3262 */
3263 if( eType==IN_INDEX_ROWID ){
3264 /* In this case, the RHS is the ROWID of table b-tree and so we also
3265 ** know that the RHS is non-NULL. Hence, we combine steps 3 and 4
3266 ** into a single opcode. */
drh2c041312018-12-24 02:34:49 +00003267 sqlite3VdbeAddOp3(v, OP_SeekRowid, iTab, destIfFalse, rLhs);
drhe347d3e2016-08-25 21:14:34 +00003268 VdbeCoverage(v);
3269 addrTruthOp = sqlite3VdbeAddOp0(v, OP_Goto); /* Return True */
3270 }else{
3271 sqlite3VdbeAddOp4(v, OP_Affinity, rLhs, nVector, 0, zAff, nVector);
3272 if( destIfFalse==destIfNull ){
3273 /* Combine Step 3 and Step 5 into a single opcode */
drh2c041312018-12-24 02:34:49 +00003274 sqlite3VdbeAddOp4Int(v, OP_NotFound, iTab, destIfFalse,
drhe347d3e2016-08-25 21:14:34 +00003275 rLhs, nVector); VdbeCoverage(v);
3276 goto sqlite3ExprCodeIN_finished;
3277 }
3278 /* Ordinary Step 3, for the case where FALSE and NULL are distinct */
drh2c041312018-12-24 02:34:49 +00003279 addrTruthOp = sqlite3VdbeAddOp4Int(v, OP_Found, iTab, 0,
drhe347d3e2016-08-25 21:14:34 +00003280 rLhs, nVector); VdbeCoverage(v);
3281 }
3282
3283 /* Step 4. If the RHS is known to be non-NULL and we did not find
3284 ** an match on the search above, then the result must be FALSE.
3285 */
3286 if( rRhsHasNull && nVector==1 ){
3287 sqlite3VdbeAddOp2(v, OP_NotNull, rRhsHasNull, destIfFalse);
3288 VdbeCoverage(v);
3289 }
3290
3291 /* Step 5. If we do not care about the difference between NULL and
3292 ** FALSE, then just return false.
3293 */
3294 if( destIfFalse==destIfNull ) sqlite3VdbeGoto(v, destIfFalse);
3295
3296 /* Step 6: Loop through rows of the RHS. Compare each row to the LHS.
3297 ** If any comparison is NULL, then the result is NULL. If all
3298 ** comparisons are FALSE then the final result is FALSE.
3299 **
3300 ** For a scalar LHS, it is sufficient to check just the first row
3301 ** of the RHS.
3302 */
3303 if( destStep6 ) sqlite3VdbeResolveLabel(v, destStep6);
drh2c041312018-12-24 02:34:49 +00003304 addrTop = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, destIfFalse);
drhe347d3e2016-08-25 21:14:34 +00003305 VdbeCoverage(v);
3306 if( nVector>1 ){
drhec4ccdb2018-12-29 02:26:59 +00003307 destNotNull = sqlite3VdbeMakeLabel(pParse);
drhe347d3e2016-08-25 21:14:34 +00003308 }else{
3309 /* For nVector==1, combine steps 6 and 7 by immediately returning
3310 ** FALSE if the first comparison is not NULL */
3311 destNotNull = destIfFalse;
3312 }
3313 for(i=0; i<nVector; i++){
3314 Expr *p;
3315 CollSeq *pColl;
3316 int r3 = sqlite3GetTempReg(pParse);
3317 p = sqlite3VectorFieldSubexpr(pLeft, i);
3318 pColl = sqlite3ExprCollSeq(pParse, p);
drh2c041312018-12-24 02:34:49 +00003319 sqlite3VdbeAddOp3(v, OP_Column, iTab, i, r3);
drhe347d3e2016-08-25 21:14:34 +00003320 sqlite3VdbeAddOp4(v, OP_Ne, rLhs+i, destNotNull, r3,
3321 (void*)pColl, P4_COLLSEQ);
3322 VdbeCoverage(v);
3323 sqlite3ReleaseTempReg(pParse, r3);
3324 }
3325 sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfNull);
3326 if( nVector>1 ){
3327 sqlite3VdbeResolveLabel(v, destNotNull);
drh2c041312018-12-24 02:34:49 +00003328 sqlite3VdbeAddOp2(v, OP_Next, iTab, addrTop+1);
drhe347d3e2016-08-25 21:14:34 +00003329 VdbeCoverage(v);
3330
3331 /* Step 7: If we reach this point, we know that the result must
3332 ** be false. */
3333 sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfFalse);
3334 }
3335
3336 /* Jumps here in order to return true. */
3337 sqlite3VdbeJumpHere(v, addrTruthOp);
3338
3339sqlite3ExprCodeIN_finished:
3340 if( rLhs!=rLhsOrig ) sqlite3ReleaseTempReg(pParse, rLhs);
drhecb87ac2016-08-25 15:46:25 +00003341 VdbeComment((v, "end IN expr"));
drhe347d3e2016-08-25 21:14:34 +00003342sqlite3ExprCodeIN_oom_error:
danba00e302016-07-23 20:24:06 +00003343 sqlite3DbFree(pParse->db, aiMap);
dan553168c2016-08-01 20:14:31 +00003344 sqlite3DbFree(pParse->db, zAff);
drhe3365e62009-11-12 17:52:24 +00003345}
3346#endif /* SQLITE_OMIT_SUBQUERY */
3347
drh13573c72010-01-12 17:04:07 +00003348#ifndef SQLITE_OMIT_FLOATING_POINT
drh598f1342007-10-23 15:39:45 +00003349/*
3350** Generate an instruction that will put the floating point
drh9cbf3422008-01-17 16:22:13 +00003351** value described by z[0..n-1] into register iMem.
drh0cf19ed2007-10-23 18:55:48 +00003352**
3353** The z[] string will probably not be zero-terminated. But the
3354** z[n] character is guaranteed to be something that does not look
3355** like the continuation of the number.
drh598f1342007-10-23 15:39:45 +00003356*/
drhb7916a72009-05-27 10:31:29 +00003357static void codeReal(Vdbe *v, const char *z, int negateFlag, int iMem){
drhfd773cf2009-05-29 14:39:07 +00003358 if( ALWAYS(z!=0) ){
drh598f1342007-10-23 15:39:45 +00003359 double value;
drh9339da12010-09-30 00:50:49 +00003360 sqlite3AtoF(z, &value, sqlite3Strlen30(z), SQLITE_UTF8);
drhd0015162009-08-21 13:22:25 +00003361 assert( !sqlite3IsNaN(value) ); /* The new AtoF never returns NaN */
3362 if( negateFlag ) value = -value;
drh97bae792015-06-05 15:59:57 +00003363 sqlite3VdbeAddOp4Dup8(v, OP_Real, 0, iMem, 0, (u8*)&value, P4_REAL);
drh598f1342007-10-23 15:39:45 +00003364 }
3365}
drh13573c72010-01-12 17:04:07 +00003366#endif
drh598f1342007-10-23 15:39:45 +00003367
3368
3369/*
drhfec19aa2004-05-19 20:41:03 +00003370** Generate an instruction that will put the integer describe by
drh9cbf3422008-01-17 16:22:13 +00003371** text z[0..n-1] into register iMem.
drh0cf19ed2007-10-23 18:55:48 +00003372**
shaneh5f1d6b62010-09-30 16:51:25 +00003373** Expr.u.zToken is always UTF8 and zero-terminated.
drhfec19aa2004-05-19 20:41:03 +00003374*/
drh13573c72010-01-12 17:04:07 +00003375static void codeInteger(Parse *pParse, Expr *pExpr, int negFlag, int iMem){
3376 Vdbe *v = pParse->pVdbe;
drh92b01d52008-06-24 00:32:35 +00003377 if( pExpr->flags & EP_IntValue ){
drh33e619f2009-05-28 01:00:55 +00003378 int i = pExpr->u.iValue;
drhd50ffc42011-03-08 02:38:28 +00003379 assert( i>=0 );
drh92b01d52008-06-24 00:32:35 +00003380 if( negFlag ) i = -i;
3381 sqlite3VdbeAddOp2(v, OP_Integer, i, iMem);
drhfd773cf2009-05-29 14:39:07 +00003382 }else{
shaneh5f1d6b62010-09-30 16:51:25 +00003383 int c;
3384 i64 value;
drhfd773cf2009-05-29 14:39:07 +00003385 const char *z = pExpr->u.zToken;
3386 assert( z!=0 );
drh9296c182014-07-23 13:40:49 +00003387 c = sqlite3DecOrHexToI64(z, &value);
drh84d4f1a2017-09-20 10:47:10 +00003388 if( (c==3 && !negFlag) || (c==2) || (negFlag && value==SMALLEST_INT64)){
drh13573c72010-01-12 17:04:07 +00003389#ifdef SQLITE_OMIT_FLOATING_POINT
3390 sqlite3ErrorMsg(pParse, "oversized integer: %s%s", negFlag ? "-" : "", z);
3391#else
drh1b7ddc52014-07-23 14:52:05 +00003392#ifndef SQLITE_OMIT_HEX_INTEGER
drh9296c182014-07-23 13:40:49 +00003393 if( sqlite3_strnicmp(z,"0x",2)==0 ){
drh77320ea2016-11-30 14:47:37 +00003394 sqlite3ErrorMsg(pParse, "hex literal too big: %s%s", negFlag?"-":"",z);
drh1b7ddc52014-07-23 14:52:05 +00003395 }else
3396#endif
3397 {
drh9296c182014-07-23 13:40:49 +00003398 codeReal(v, z, negFlag, iMem);
3399 }
drh13573c72010-01-12 17:04:07 +00003400#endif
drh77320ea2016-11-30 14:47:37 +00003401 }else{
drh84d4f1a2017-09-20 10:47:10 +00003402 if( negFlag ){ value = c==3 ? SMALLEST_INT64 : -value; }
drh77320ea2016-11-30 14:47:37 +00003403 sqlite3VdbeAddOp4Dup8(v, OP_Int64, 0, iMem, 0, (u8*)&value, P4_INT64);
danielk1977c9cf9012007-05-30 10:36:47 +00003404 }
drhfec19aa2004-05-19 20:41:03 +00003405 }
3406}
3407
drh5cd79232009-05-25 11:46:29 +00003408
drh1f9ca2c2015-08-25 16:57:52 +00003409/* Generate code that will load into register regOut a value that is
3410** appropriate for the iIdxCol-th column of index pIdx.
3411*/
3412void sqlite3ExprCodeLoadIndexColumn(
3413 Parse *pParse, /* The parsing context */
3414 Index *pIdx, /* The index whose column is to be loaded */
3415 int iTabCur, /* Cursor pointing to a table row */
3416 int iIdxCol, /* The column of the index to be loaded */
3417 int regOut /* Store the index column value in this register */
3418){
3419 i16 iTabCol = pIdx->aiColumn[iIdxCol];
drh4b92f982015-09-29 17:20:14 +00003420 if( iTabCol==XN_EXPR ){
3421 assert( pIdx->aColExpr );
3422 assert( pIdx->aColExpr->nExpr>iIdxCol );
drh3e34eab2017-07-19 19:48:40 +00003423 pParse->iSelfTab = iTabCur + 1;
drh1c75c9d2015-12-21 15:22:13 +00003424 sqlite3ExprCodeCopy(pParse, pIdx->aColExpr->a[iIdxCol].pExpr, regOut);
drh3e34eab2017-07-19 19:48:40 +00003425 pParse->iSelfTab = 0;
drh4b92f982015-09-29 17:20:14 +00003426 }else{
drh6df9c4b2019-10-18 12:52:08 +00003427 sqlite3ExprCodeGetColumnOfTable(pParse->pVdbe, pIdx->pTable, iTabCur,
drh1f9ca2c2015-08-25 16:57:52 +00003428 iTabCol, regOut);
drh1f9ca2c2015-08-25 16:57:52 +00003429 }
drh1f9ca2c2015-08-25 16:57:52 +00003430}
3431
drhe70fa7f2019-10-22 21:01:34 +00003432#ifndef SQLITE_OMIT_GENERATED_COLUMNS
3433/*
3434** Generate code that will compute the value of generated column pCol
3435** and store the result in register regOut
3436*/
3437void sqlite3ExprCodeGeneratedColumn(
3438 Parse *pParse,
3439 Column *pCol,
3440 int regOut
3441){
drh4dad7ed2019-12-16 16:52:22 +00003442 int iAddr;
3443 Vdbe *v = pParse->pVdbe;
3444 assert( v!=0 );
3445 assert( pParse->iSelfTab!=0 );
3446 if( pParse->iSelfTab>0 ){
3447 iAddr = sqlite3VdbeAddOp3(v, OP_IfNullRow, pParse->iSelfTab-1, 0, regOut);
3448 }else{
3449 iAddr = 0;
3450 }
drhe70fa7f2019-10-22 21:01:34 +00003451 sqlite3ExprCode(pParse, pCol->pDflt, regOut);
3452 if( pCol->affinity>=SQLITE_AFF_TEXT ){
drh4dad7ed2019-12-16 16:52:22 +00003453 sqlite3VdbeAddOp4(v, OP_Affinity, regOut, 1, 0, &pCol->affinity, 1);
drhe70fa7f2019-10-22 21:01:34 +00003454 }
drh4dad7ed2019-12-16 16:52:22 +00003455 if( iAddr ) sqlite3VdbeJumpHere(v, iAddr);
drhe70fa7f2019-10-22 21:01:34 +00003456}
3457#endif /* SQLITE_OMIT_GENERATED_COLUMNS */
3458
drh5cd79232009-05-25 11:46:29 +00003459/*
drh5c092e82010-05-14 19:24:02 +00003460** Generate code to extract the value of the iCol-th column of a table.
3461*/
3462void sqlite3ExprCodeGetColumnOfTable(
drh6df9c4b2019-10-18 12:52:08 +00003463 Vdbe *v, /* Parsing context */
drh5c092e82010-05-14 19:24:02 +00003464 Table *pTab, /* The table containing the value */
drh313619f2013-10-31 20:34:06 +00003465 int iTabCur, /* The table cursor. Or the PK cursor for WITHOUT ROWID */
drh5c092e82010-05-14 19:24:02 +00003466 int iCol, /* Index of the column to extract */
drh313619f2013-10-31 20:34:06 +00003467 int regOut /* Extract the value into this register */
drh5c092e82010-05-14 19:24:02 +00003468){
drhab45fc02019-10-16 22:01:56 +00003469 Column *pCol;
drh81f7b372019-10-16 12:18:59 +00003470 assert( v!=0 );
drhaca19e12017-04-07 19:41:31 +00003471 if( pTab==0 ){
3472 sqlite3VdbeAddOp3(v, OP_Column, iTabCur, iCol, regOut);
3473 return;
3474 }
drh5c092e82010-05-14 19:24:02 +00003475 if( iCol<0 || iCol==pTab->iPKey ){
3476 sqlite3VdbeAddOp2(v, OP_Rowid, iTabCur, regOut);
3477 }else{
drh81f7b372019-10-16 12:18:59 +00003478 int op;
3479 int x;
3480 if( IsVirtual(pTab) ){
3481 op = OP_VColumn;
3482 x = iCol;
3483#ifndef SQLITE_OMIT_GENERATED_COLUMNS
drhab45fc02019-10-16 22:01:56 +00003484 }else if( (pCol = &pTab->aCol[iCol])->colFlags & COLFLAG_VIRTUAL ){
drh6df9c4b2019-10-18 12:52:08 +00003485 Parse *pParse = sqlite3VdbeParser(v);
drhab45fc02019-10-16 22:01:56 +00003486 if( pCol->colFlags & COLFLAG_BUSY ){
3487 sqlite3ErrorMsg(pParse, "generated column loop on \"%s\"", pCol->zName);
3488 }else{
3489 int savedSelfTab = pParse->iSelfTab;
3490 pCol->colFlags |= COLFLAG_BUSY;
3491 pParse->iSelfTab = iTabCur+1;
drhe70fa7f2019-10-22 21:01:34 +00003492 sqlite3ExprCodeGeneratedColumn(pParse, pCol, regOut);
drhab45fc02019-10-16 22:01:56 +00003493 pParse->iSelfTab = savedSelfTab;
3494 pCol->colFlags &= ~COLFLAG_BUSY;
3495 }
drh81f7b372019-10-16 12:18:59 +00003496 return;
3497#endif
3498 }else if( !HasRowid(pTab) ){
drhc5f808d2019-10-19 15:01:52 +00003499 testcase( iCol!=sqlite3TableColumnToStorage(pTab, iCol) );
drhb9bcf7c2019-10-19 13:29:10 +00003500 x = sqlite3TableColumnToIndex(sqlite3PrimaryKeyIndex(pTab), iCol);
drh81f7b372019-10-16 12:18:59 +00003501 op = OP_Column;
3502 }else{
drhb9bcf7c2019-10-19 13:29:10 +00003503 x = sqlite3TableColumnToStorage(pTab,iCol);
drhc5f808d2019-10-19 15:01:52 +00003504 testcase( x!=iCol );
drh81f7b372019-10-16 12:18:59 +00003505 op = OP_Column;
drhee0ec8e2013-10-31 17:38:01 +00003506 }
3507 sqlite3VdbeAddOp3(v, op, iTabCur, x, regOut);
drh5c092e82010-05-14 19:24:02 +00003508 sqlite3ColumnDefault(v, pTab, iCol, regOut);
3509 }
3510}
3511
3512/*
drh945498f2007-02-24 11:52:52 +00003513** Generate code that will extract the iColumn-th column from
drh8c607192018-08-04 15:53:55 +00003514** table pTab and store the column value in register iReg.
drhe55cbd72008-03-31 23:48:03 +00003515**
3516** There must be an open cursor to pTab in iTable when this routine
3517** is called. If iColumn<0 then code is generated that extracts the rowid.
drh945498f2007-02-24 11:52:52 +00003518*/
drhe55cbd72008-03-31 23:48:03 +00003519int sqlite3ExprCodeGetColumn(
3520 Parse *pParse, /* Parsing and code generating context */
drh2133d822008-01-03 18:44:59 +00003521 Table *pTab, /* Description of the table we are reading from */
3522 int iColumn, /* Index of the table column */
3523 int iTable, /* The cursor pointing to the table */
drha748fdc2012-03-28 01:34:47 +00003524 int iReg, /* Store results here */
drhce78bc62015-10-15 19:21:51 +00003525 u8 p5 /* P5 value for OP_Column + FLAGS */
drh2133d822008-01-03 18:44:59 +00003526){
drh81f7b372019-10-16 12:18:59 +00003527 assert( pParse->pVdbe!=0 );
drh6df9c4b2019-10-18 12:52:08 +00003528 sqlite3ExprCodeGetColumnOfTable(pParse->pVdbe, pTab, iTable, iColumn, iReg);
drha748fdc2012-03-28 01:34:47 +00003529 if( p5 ){
drh99670ab2019-12-14 17:43:37 +00003530 VdbeOp *pOp = sqlite3VdbeGetOp(pParse->pVdbe,-1);
3531 if( pOp->opcode==OP_Column ) pOp->p5 = p5;
drha748fdc2012-03-28 01:34:47 +00003532 }
drhe55cbd72008-03-31 23:48:03 +00003533 return iReg;
3534}
drhe55cbd72008-03-31 23:48:03 +00003535
3536/*
drhb21e7c72008-06-22 12:37:57 +00003537** Generate code to move content from registers iFrom...iFrom+nReg-1
drh36a5d882018-08-04 17:15:56 +00003538** over to iTo..iTo+nReg-1.
drhe55cbd72008-03-31 23:48:03 +00003539*/
drhb21e7c72008-06-22 12:37:57 +00003540void sqlite3ExprCodeMove(Parse *pParse, int iFrom, int iTo, int nReg){
drh079a3072014-03-19 14:10:55 +00003541 sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move, iFrom, iTo, nReg);
drh945498f2007-02-24 11:52:52 +00003542}
3543
drh652fbf52008-04-01 01:42:41 +00003544/*
drh12abf402016-08-22 14:30:05 +00003545** Convert a scalar expression node to a TK_REGISTER referencing
3546** register iReg. The caller must ensure that iReg already contains
3547** the correct value for the expression.
drha4c3c872013-09-12 17:29:25 +00003548*/
dan069d1b12019-06-16 08:58:14 +00003549static void exprToRegister(Expr *pExpr, int iReg){
drh0d950af2019-08-22 16:38:42 +00003550 Expr *p = sqlite3ExprSkipCollateAndLikely(pExpr);
drha4c3c872013-09-12 17:29:25 +00003551 p->op2 = p->op;
3552 p->op = TK_REGISTER;
3553 p->iTable = iReg;
3554 ExprClearProperty(p, EP_Skip);
3555}
3556
drh12abf402016-08-22 14:30:05 +00003557/*
3558** Evaluate an expression (either a vector or a scalar expression) and store
3559** the result in continguous temporary registers. Return the index of
3560** the first register used to store the result.
3561**
3562** If the returned result register is a temporary scalar, then also write
3563** that register number into *piFreeable. If the returned result register
3564** is not a temporary or if the expression is a vector set *piFreeable
3565** to 0.
3566*/
3567static int exprCodeVector(Parse *pParse, Expr *p, int *piFreeable){
3568 int iResult;
3569 int nResult = sqlite3ExprVectorSize(p);
3570 if( nResult==1 ){
3571 iResult = sqlite3ExprCodeTemp(pParse, p, piFreeable);
3572 }else{
3573 *piFreeable = 0;
3574 if( p->op==TK_SELECT ){
drhdd1bb432017-05-15 15:12:24 +00003575#if SQLITE_OMIT_SUBQUERY
3576 iResult = 0;
3577#else
drh85bcdce2018-12-23 21:27:29 +00003578 iResult = sqlite3CodeSubselect(pParse, p);
drhdd1bb432017-05-15 15:12:24 +00003579#endif
drh12abf402016-08-22 14:30:05 +00003580 }else{
3581 int i;
3582 iResult = pParse->nMem+1;
3583 pParse->nMem += nResult;
3584 for(i=0; i<nResult; i++){
dan4b725242016-11-23 19:31:18 +00003585 sqlite3ExprCodeFactorable(pParse, p->x.pList->a[i].pExpr, i+iResult);
drh12abf402016-08-22 14:30:05 +00003586 }
3587 }
3588 }
3589 return iResult;
3590}
3591
drh25c42962020-01-01 13:55:08 +00003592/*
3593** Generate code to implement special SQL functions that are implemented
3594** in-line rather than by using the usual callbacks.
3595*/
3596static int exprCodeInlineFunction(
3597 Parse *pParse, /* Parsing context */
3598 ExprList *pFarg, /* List of function arguments */
3599 int iFuncId, /* Function ID. One of the INTFUNC_... values */
3600 int target /* Store function result in this register */
3601){
3602 int nFarg;
3603 Vdbe *v = pParse->pVdbe;
3604 assert( v!=0 );
3605 assert( pFarg!=0 );
3606 nFarg = pFarg->nExpr;
3607 assert( nFarg>0 ); /* All in-line functions have at least one argument */
3608 switch( iFuncId ){
3609 case INLINEFUNC_coalesce: {
3610 /* Attempt a direct implementation of the built-in COALESCE() and
3611 ** IFNULL() functions. This avoids unnecessary evaluation of
3612 ** arguments past the first non-NULL argument.
3613 */
3614 int endCoalesce = sqlite3VdbeMakeLabel(pParse);
3615 int i;
3616 assert( nFarg>=2 );
3617 sqlite3ExprCode(pParse, pFarg->a[0].pExpr, target);
3618 for(i=1; i<nFarg; i++){
3619 sqlite3VdbeAddOp2(v, OP_NotNull, target, endCoalesce);
3620 VdbeCoverage(v);
3621 sqlite3ExprCode(pParse, pFarg->a[i].pExpr, target);
3622 }
3623 sqlite3VdbeResolveLabel(v, endCoalesce);
3624 break;
3625 }
3626
drh171c50e2020-01-01 15:43:30 +00003627 default: {
drh25c42962020-01-01 13:55:08 +00003628 /* The UNLIKELY() function is a no-op. The result is the value
3629 ** of the first argument.
3630 */
drh171c50e2020-01-01 15:43:30 +00003631 assert( nFarg==1 || nFarg==2 );
drh25c42962020-01-01 13:55:08 +00003632 target = sqlite3ExprCodeTarget(pParse, pFarg->a[0].pExpr, target);
3633 break;
3634 }
3635
drh171c50e2020-01-01 15:43:30 +00003636 /***********************************************************************
3637 ** Test-only SQL functions that are only usable if enabled
3638 ** via SQLITE_TESTCTRL_INTERNAL_FUNCTIONS
3639 */
3640 case INLINEFUNC_expr_compare: {
3641 /* Compare two expressions using sqlite3ExprCompare() */
3642 assert( nFarg==2 );
3643 sqlite3VdbeAddOp2(v, OP_Integer,
3644 sqlite3ExprCompare(0,pFarg->a[0].pExpr, pFarg->a[1].pExpr,-1),
3645 target);
3646 break;
3647 }
3648
3649 case INLINEFUNC_expr_implies_expr: {
3650 /* Compare two expressions using sqlite3ExprImpliesExpr() */
3651 assert( nFarg==2 );
3652 sqlite3VdbeAddOp2(v, OP_Integer,
3653 sqlite3ExprImpliesExpr(pParse,pFarg->a[0].pExpr, pFarg->a[1].pExpr,-1),
3654 target);
3655 break;
3656 }
3657
3658 case INLINEFUNC_implies_nonnull_row: {
3659 /* REsult of sqlite3ExprImpliesNonNullRow() */
3660 Expr *pA1;
3661 assert( nFarg==2 );
3662 pA1 = pFarg->a[1].pExpr;
3663 if( pA1->op==TK_COLUMN ){
3664 sqlite3VdbeAddOp2(v, OP_Integer,
3665 sqlite3ExprImpliesNonNullRow(pFarg->a[0].pExpr,pA1->iTable),
3666 target);
3667 }else{
3668 sqlite3VdbeAddOp2(v, OP_Null, 0, target);
3669 }
3670 break;
3671 }
3672
drh25c42962020-01-01 13:55:08 +00003673#ifdef SQLITE_DEBUG
3674 case INLINEFUNC_affinity: {
3675 /* The AFFINITY() function evaluates to a string that describes
3676 ** the type affinity of the argument. This is used for testing of
3677 ** the SQLite type logic.
3678 */
3679 const char *azAff[] = { "blob", "text", "numeric", "integer", "real" };
3680 char aff;
3681 assert( nFarg==1 );
3682 aff = sqlite3ExprAffinity(pFarg->a[0].pExpr);
3683 sqlite3VdbeLoadString(v, target,
3684 (aff<=SQLITE_AFF_NONE) ? "none" : azAff[aff-SQLITE_AFF_BLOB]);
3685 break;
3686 }
3687#endif
3688 }
3689 return target;
3690}
3691
dan71c57db2016-07-09 20:23:55 +00003692
drha4c3c872013-09-12 17:29:25 +00003693/*
drhcce7d172000-05-31 15:34:51 +00003694** Generate code into the current Vdbe to evaluate the given
drh2dcef112008-01-12 19:03:48 +00003695** expression. Attempt to store the results in register "target".
3696** Return the register where results are stored.
drh389a1ad2008-01-03 23:44:53 +00003697**
drh8b213892008-08-29 02:14:02 +00003698** With this routine, there is no guarantee that results will
drh2dcef112008-01-12 19:03:48 +00003699** be stored in target. The result might be stored in some other
3700** register if it is convenient to do so. The calling function
3701** must check the return code and move the results to the desired
3702** register.
drhcce7d172000-05-31 15:34:51 +00003703*/
drh678ccce2008-03-31 18:19:54 +00003704int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){
drh2dcef112008-01-12 19:03:48 +00003705 Vdbe *v = pParse->pVdbe; /* The VM under construction */
3706 int op; /* The opcode being coded */
3707 int inReg = target; /* Results stored in register inReg */
3708 int regFree1 = 0; /* If non-zero free this temporary register */
3709 int regFree2 = 0; /* If non-zero free this temporary register */
dan7b35a772016-07-28 19:47:15 +00003710 int r1, r2; /* Various register numbers */
drh10d1edf2013-11-15 15:52:39 +00003711 Expr tempX; /* Temporary expression node */
dan71c57db2016-07-09 20:23:55 +00003712 int p5 = 0;
drhffe07b22005-11-03 00:41:17 +00003713
drh9cbf3422008-01-17 16:22:13 +00003714 assert( target>0 && target<=pParse->nMem );
drh20411ea2009-05-29 19:00:12 +00003715 if( v==0 ){
3716 assert( pParse->db->mallocFailed );
3717 return 0;
3718 }
drh389a1ad2008-01-03 23:44:53 +00003719
drh1efa8022018-04-28 04:16:43 +00003720expr_code_doover:
drh389a1ad2008-01-03 23:44:53 +00003721 if( pExpr==0 ){
3722 op = TK_NULL;
3723 }else{
3724 op = pExpr->op;
3725 }
drhf2bc0132004-10-04 13:19:23 +00003726 switch( op ){
drh13449892005-09-07 21:22:45 +00003727 case TK_AGG_COLUMN: {
3728 AggInfo *pAggInfo = pExpr->pAggInfo;
3729 struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg];
3730 if( !pAggInfo->directMode ){
drh9de221d2008-01-05 06:51:30 +00003731 assert( pCol->iMem>0 );
drhc332cc32016-09-19 10:24:19 +00003732 return pCol->iMem;
drh13449892005-09-07 21:22:45 +00003733 }else if( pAggInfo->useSortingIdx ){
dan5134d132011-09-02 10:31:11 +00003734 sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab,
drh389a1ad2008-01-03 23:44:53 +00003735 pCol->iSorterColumn, target);
drhc332cc32016-09-19 10:24:19 +00003736 return target;
drh13449892005-09-07 21:22:45 +00003737 }
3738 /* Otherwise, fall thru into the TK_COLUMN case */
3739 }
drh967e8b72000-06-21 13:59:10 +00003740 case TK_COLUMN: {
drhb2b9d3d2013-08-01 01:14:43 +00003741 int iTab = pExpr->iTable;
drh67b9ba12019-12-20 20:45:02 +00003742 int iReg;
drhefad2e22018-07-27 16:57:11 +00003743 if( ExprHasProperty(pExpr, EP_FixedCol) ){
drhd98f5322018-08-09 18:36:54 +00003744 /* This COLUMN expression is really a constant due to WHERE clause
3745 ** constraints, and that constant is coded by the pExpr->pLeft
3746 ** expresssion. However, make sure the constant has the correct
3747 ** datatype by applying the Affinity of the table column to the
3748 ** constant.
3749 */
drh57f7ece2019-11-21 18:28:44 +00003750 int aff;
drh67b9ba12019-12-20 20:45:02 +00003751 iReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft,target);
drh57f7ece2019-11-21 18:28:44 +00003752 if( pExpr->y.pTab ){
3753 aff = sqlite3TableColumnAffinity(pExpr->y.pTab, pExpr->iColumn);
3754 }else{
3755 aff = pExpr->affExpr;
3756 }
drh96fb16e2019-08-06 14:37:24 +00003757 if( aff>SQLITE_AFF_BLOB ){
drhd98f5322018-08-09 18:36:54 +00003758 static const char zAff[] = "B\000C\000D\000E";
3759 assert( SQLITE_AFF_BLOB=='A' );
3760 assert( SQLITE_AFF_TEXT=='B' );
3761 if( iReg!=target ){
3762 sqlite3VdbeAddOp2(v, OP_SCopy, iReg, target);
3763 iReg = target;
3764 }
3765 sqlite3VdbeAddOp4(v, OP_Affinity, iReg, 1, 0,
3766 &zAff[(aff-'B')*2], P4_STATIC);
3767 }
3768 return iReg;
drhefad2e22018-07-27 16:57:11 +00003769 }
drhb2b9d3d2013-08-01 01:14:43 +00003770 if( iTab<0 ){
drh6e97f8e2017-07-20 13:17:08 +00003771 if( pParse->iSelfTab<0 ){
drh9942ef02019-10-18 02:19:18 +00003772 /* Other columns in the same row for CHECK constraints or
3773 ** generated columns or for inserting into partial index.
3774 ** The row is unpacked into registers beginning at
3775 ** 0-(pParse->iSelfTab). The rowid (if any) is in a register
3776 ** immediately prior to the first column.
3777 */
3778 Column *pCol;
3779 Table *pTab = pExpr->y.pTab;
3780 int iSrc;
drhc5f808d2019-10-19 15:01:52 +00003781 int iCol = pExpr->iColumn;
drh9942ef02019-10-18 02:19:18 +00003782 assert( pTab!=0 );
drhc5f808d2019-10-19 15:01:52 +00003783 assert( iCol>=XN_ROWID );
drhb0cbcd02019-12-21 14:09:30 +00003784 assert( iCol<pTab->nCol );
drhc5f808d2019-10-19 15:01:52 +00003785 if( iCol<0 ){
drh9942ef02019-10-18 02:19:18 +00003786 return -1-pParse->iSelfTab;
3787 }
drhc5f808d2019-10-19 15:01:52 +00003788 pCol = pTab->aCol + iCol;
3789 testcase( iCol!=sqlite3TableColumnToStorage(pTab,iCol) );
3790 iSrc = sqlite3TableColumnToStorage(pTab, iCol) - pParse->iSelfTab;
drh9942ef02019-10-18 02:19:18 +00003791#ifndef SQLITE_OMIT_GENERATED_COLUMNS
3792 if( pCol->colFlags & COLFLAG_GENERATED ){
drh4e8e5332019-11-07 02:32:54 +00003793 if( pCol->colFlags & COLFLAG_BUSY ){
3794 sqlite3ErrorMsg(pParse, "generated column loop on \"%s\"",
3795 pCol->zName);
3796 return 0;
3797 }
3798 pCol->colFlags |= COLFLAG_BUSY;
3799 if( pCol->colFlags & COLFLAG_NOTAVAIL ){
3800 sqlite3ExprCodeGeneratedColumn(pParse, pCol, iSrc);
3801 }
3802 pCol->colFlags &= ~(COLFLAG_BUSY|COLFLAG_NOTAVAIL);
drhdd6cc9b2019-10-19 18:47:27 +00003803 return iSrc;
drh9942ef02019-10-18 02:19:18 +00003804 }else
3805#endif /* SQLITE_OMIT_GENERATED_COLUMNS */
3806 if( pCol->affinity==SQLITE_AFF_REAL ){
3807 sqlite3VdbeAddOp2(v, OP_SCopy, iSrc, target);
drhbffdd632019-09-02 00:58:44 +00003808 sqlite3VdbeAddOp1(v, OP_RealAffinity, target);
3809 return target;
3810 }else{
drh9942ef02019-10-18 02:19:18 +00003811 return iSrc;
drhbffdd632019-09-02 00:58:44 +00003812 }
drhb2b9d3d2013-08-01 01:14:43 +00003813 }else{
drh1f9ca2c2015-08-25 16:57:52 +00003814 /* Coding an expression that is part of an index where column names
3815 ** in the index refer to the table to which the index belongs */
drh3e34eab2017-07-19 19:48:40 +00003816 iTab = pParse->iSelfTab - 1;
drhb2b9d3d2013-08-01 01:14:43 +00003817 }
drh22827922000-06-06 17:27:05 +00003818 }
drh67b9ba12019-12-20 20:45:02 +00003819 iReg = sqlite3ExprCodeGetColumn(pParse, pExpr->y.pTab,
drhb2b9d3d2013-08-01 01:14:43 +00003820 pExpr->iColumn, iTab, target,
3821 pExpr->op2);
drh67b9ba12019-12-20 20:45:02 +00003822 if( pExpr->y.pTab==0 && pExpr->affExpr==SQLITE_AFF_REAL ){
3823 sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg);
3824 }
3825 return iReg;
drhcce7d172000-05-31 15:34:51 +00003826 }
3827 case TK_INTEGER: {
drh13573c72010-01-12 17:04:07 +00003828 codeInteger(pParse, pExpr, 0, target);
drhc332cc32016-09-19 10:24:19 +00003829 return target;
drhfec19aa2004-05-19 20:41:03 +00003830 }
drh8abed7b2018-02-26 18:49:05 +00003831 case TK_TRUEFALSE: {
drh96acafb2018-02-27 14:49:25 +00003832 sqlite3VdbeAddOp2(v, OP_Integer, sqlite3ExprTruthValue(pExpr), target);
drh007c8432018-02-26 03:20:18 +00003833 return target;
3834 }
drh13573c72010-01-12 17:04:07 +00003835#ifndef SQLITE_OMIT_FLOATING_POINT
drh598f1342007-10-23 15:39:45 +00003836 case TK_FLOAT: {
drh33e619f2009-05-28 01:00:55 +00003837 assert( !ExprHasProperty(pExpr, EP_IntValue) );
3838 codeReal(v, pExpr->u.zToken, 0, target);
drhc332cc32016-09-19 10:24:19 +00003839 return target;
drh598f1342007-10-23 15:39:45 +00003840 }
drh13573c72010-01-12 17:04:07 +00003841#endif
drhfec19aa2004-05-19 20:41:03 +00003842 case TK_STRING: {
drh33e619f2009-05-28 01:00:55 +00003843 assert( !ExprHasProperty(pExpr, EP_IntValue) );
drh076e85f2015-09-03 13:46:12 +00003844 sqlite3VdbeLoadString(v, target, pExpr->u.zToken);
drhc332cc32016-09-19 10:24:19 +00003845 return target;
drhcce7d172000-05-31 15:34:51 +00003846 }
drhaac30f92019-12-14 15:01:55 +00003847 default: {
drhc29af652019-12-18 21:22:40 +00003848 /* Make NULL the default case so that if a bug causes an illegal
3849 ** Expr node to be passed into this function, it will be handled
drh9524a7e2019-12-22 18:06:49 +00003850 ** sanely and not crash. But keep the assert() to bring the problem
3851 ** to the attention of the developers. */
3852 assert( op==TK_NULL );
drh9de221d2008-01-05 06:51:30 +00003853 sqlite3VdbeAddOp2(v, OP_Null, 0, target);
drhc332cc32016-09-19 10:24:19 +00003854 return target;
drhf0863fe2005-06-12 21:35:51 +00003855 }
danielk19775338a5f2005-01-20 13:03:10 +00003856#ifndef SQLITE_OMIT_BLOB_LITERAL
danielk1977c572ef72004-05-27 09:28:41 +00003857 case TK_BLOB: {
drh6c8c6ce2005-08-23 11:17:58 +00003858 int n;
3859 const char *z;
drhca48c902008-01-18 14:08:24 +00003860 char *zBlob;
drh33e619f2009-05-28 01:00:55 +00003861 assert( !ExprHasProperty(pExpr, EP_IntValue) );
3862 assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
3863 assert( pExpr->u.zToken[1]=='\'' );
3864 z = &pExpr->u.zToken[2];
drhb7916a72009-05-27 10:31:29 +00003865 n = sqlite3Strlen30(z) - 1;
3866 assert( z[n]=='\'' );
drhca48c902008-01-18 14:08:24 +00003867 zBlob = sqlite3HexToBlob(sqlite3VdbeDb(v), z, n);
3868 sqlite3VdbeAddOp4(v, OP_Blob, n/2, target, 0, zBlob, P4_DYNAMIC);
drhc332cc32016-09-19 10:24:19 +00003869 return target;
danielk1977c572ef72004-05-27 09:28:41 +00003870 }
danielk19775338a5f2005-01-20 13:03:10 +00003871#endif
drh50457892003-09-06 01:10:47 +00003872 case TK_VARIABLE: {
drh33e619f2009-05-28 01:00:55 +00003873 assert( !ExprHasProperty(pExpr, EP_IntValue) );
3874 assert( pExpr->u.zToken!=0 );
3875 assert( pExpr->u.zToken[0]!=0 );
drheaf52d82010-05-12 13:50:23 +00003876 sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iColumn, target);
3877 if( pExpr->u.zToken[1]!=0 ){
drh9bf755c2016-12-23 03:59:31 +00003878 const char *z = sqlite3VListNumToName(pParse->pVList, pExpr->iColumn);
drh9524a7e2019-12-22 18:06:49 +00003879 assert( pExpr->u.zToken[0]=='?' || (z && !strcmp(pExpr->u.zToken, z)) );
drhce1bbe52016-12-23 13:52:45 +00003880 pParse->pVList[0] = 0; /* Indicate VList may no longer be enlarged */
drhf326d662016-12-23 13:30:53 +00003881 sqlite3VdbeAppendP4(v, (char*)z, P4_STATIC);
drh895d7472004-08-20 16:02:39 +00003882 }
drhc332cc32016-09-19 10:24:19 +00003883 return target;
drh50457892003-09-06 01:10:47 +00003884 }
drh4e0cff62004-11-05 05:10:28 +00003885 case TK_REGISTER: {
drhc332cc32016-09-19 10:24:19 +00003886 return pExpr->iTable;
drh4e0cff62004-11-05 05:10:28 +00003887 }
drh487e2622005-06-25 18:42:14 +00003888#ifndef SQLITE_OMIT_CAST
3889 case TK_CAST: {
3890 /* Expressions of the form: CAST(pLeft AS token) */
drh2dcef112008-01-12 19:03:48 +00003891 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
drh1735fa82008-11-06 15:33:03 +00003892 if( inReg!=target ){
3893 sqlite3VdbeAddOp2(v, OP_SCopy, inReg, target);
3894 inReg = target;
3895 }
drh4169e432014-08-25 20:11:52 +00003896 sqlite3VdbeAddOp2(v, OP_Cast, target,
3897 sqlite3AffinityType(pExpr->u.zToken, 0));
drhc332cc32016-09-19 10:24:19 +00003898 return inReg;
drh487e2622005-06-25 18:42:14 +00003899 }
3900#endif /* SQLITE_OMIT_CAST */
dan71c57db2016-07-09 20:23:55 +00003901 case TK_IS:
3902 case TK_ISNOT:
3903 op = (op==TK_IS) ? TK_EQ : TK_NE;
3904 p5 = SQLITE_NULLEQ;
3905 /* fall-through */
drhc9b84a12002-06-20 11:36:48 +00003906 case TK_LT:
3907 case TK_LE:
3908 case TK_GT:
3909 case TK_GE:
3910 case TK_NE:
3911 case TK_EQ: {
dan71c57db2016-07-09 20:23:55 +00003912 Expr *pLeft = pExpr->pLeft;
dan625015e2016-07-30 16:39:28 +00003913 if( sqlite3ExprIsVector(pLeft) ){
drh79752b62016-08-13 10:02:17 +00003914 codeVectorCompare(pParse, pExpr, target, op, p5);
dan71c57db2016-07-09 20:23:55 +00003915 }else{
3916 r1 = sqlite3ExprCodeTemp(pParse, pLeft, &regFree1);
3917 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
3918 codeCompare(pParse, pLeft, pExpr->pRight, op,
drh898c5272019-10-22 00:03:41 +00003919 r1, r2, inReg, SQLITE_STOREP2 | p5,
3920 ExprHasProperty(pExpr,EP_Commuted));
dan71c57db2016-07-09 20:23:55 +00003921 assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
3922 assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
3923 assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
3924 assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
3925 assert(TK_EQ==OP_Eq); testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq);
3926 assert(TK_NE==OP_Ne); testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne);
3927 testcase( regFree1==0 );
3928 testcase( regFree2==0 );
3929 }
drh6a2fe092009-09-23 02:29:36 +00003930 break;
3931 }
drhcce7d172000-05-31 15:34:51 +00003932 case TK_AND:
3933 case TK_OR:
3934 case TK_PLUS:
3935 case TK_STAR:
3936 case TK_MINUS:
drhbf4133c2001-10-13 02:59:08 +00003937 case TK_REM:
3938 case TK_BITAND:
3939 case TK_BITOR:
drh17c40292004-07-21 02:53:29 +00003940 case TK_SLASH:
drhbf4133c2001-10-13 02:59:08 +00003941 case TK_LSHIFT:
drh855eb1c2004-08-31 13:45:11 +00003942 case TK_RSHIFT:
drh00400772000-06-16 20:51:26 +00003943 case TK_CONCAT: {
drh7d176102014-02-18 03:07:12 +00003944 assert( TK_AND==OP_And ); testcase( op==TK_AND );
3945 assert( TK_OR==OP_Or ); testcase( op==TK_OR );
3946 assert( TK_PLUS==OP_Add ); testcase( op==TK_PLUS );
3947 assert( TK_MINUS==OP_Subtract ); testcase( op==TK_MINUS );
3948 assert( TK_REM==OP_Remainder ); testcase( op==TK_REM );
3949 assert( TK_BITAND==OP_BitAnd ); testcase( op==TK_BITAND );
3950 assert( TK_BITOR==OP_BitOr ); testcase( op==TK_BITOR );
3951 assert( TK_SLASH==OP_Divide ); testcase( op==TK_SLASH );
3952 assert( TK_LSHIFT==OP_ShiftLeft ); testcase( op==TK_LSHIFT );
3953 assert( TK_RSHIFT==OP_ShiftRight ); testcase( op==TK_RSHIFT );
3954 assert( TK_CONCAT==OP_Concat ); testcase( op==TK_CONCAT );
drh2dcef112008-01-12 19:03:48 +00003955 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
3956 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
drh5b6afba2008-01-05 16:29:28 +00003957 sqlite3VdbeAddOp3(v, op, r2, r1, target);
drhc5499be2008-04-01 15:06:33 +00003958 testcase( regFree1==0 );
3959 testcase( regFree2==0 );
drh00400772000-06-16 20:51:26 +00003960 break;
3961 }
drhcce7d172000-05-31 15:34:51 +00003962 case TK_UMINUS: {
drhfec19aa2004-05-19 20:41:03 +00003963 Expr *pLeft = pExpr->pLeft;
3964 assert( pLeft );
drh13573c72010-01-12 17:04:07 +00003965 if( pLeft->op==TK_INTEGER ){
3966 codeInteger(pParse, pLeft, 1, target);
drhc332cc32016-09-19 10:24:19 +00003967 return target;
drh13573c72010-01-12 17:04:07 +00003968#ifndef SQLITE_OMIT_FLOATING_POINT
3969 }else if( pLeft->op==TK_FLOAT ){
drh33e619f2009-05-28 01:00:55 +00003970 assert( !ExprHasProperty(pExpr, EP_IntValue) );
3971 codeReal(v, pLeft->u.zToken, 1, target);
drhc332cc32016-09-19 10:24:19 +00003972 return target;
drh13573c72010-01-12 17:04:07 +00003973#endif
drh3c84ddf2008-01-09 02:15:38 +00003974 }else{
drh10d1edf2013-11-15 15:52:39 +00003975 tempX.op = TK_INTEGER;
3976 tempX.flags = EP_IntValue|EP_TokenOnly;
3977 tempX.u.iValue = 0;
3978 r1 = sqlite3ExprCodeTemp(pParse, &tempX, &regFree1);
drhe55cbd72008-03-31 23:48:03 +00003979 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree2);
drh2dcef112008-01-12 19:03:48 +00003980 sqlite3VdbeAddOp3(v, OP_Subtract, r2, r1, target);
drhc5499be2008-04-01 15:06:33 +00003981 testcase( regFree2==0 );
drh6e142f52000-06-08 13:36:40 +00003982 }
drh3c84ddf2008-01-09 02:15:38 +00003983 break;
drh6e142f52000-06-08 13:36:40 +00003984 }
drhbf4133c2001-10-13 02:59:08 +00003985 case TK_BITNOT:
drh6e142f52000-06-08 13:36:40 +00003986 case TK_NOT: {
drh7d176102014-02-18 03:07:12 +00003987 assert( TK_BITNOT==OP_BitNot ); testcase( op==TK_BITNOT );
3988 assert( TK_NOT==OP_Not ); testcase( op==TK_NOT );
drhe99fa2a2008-12-15 15:27:51 +00003989 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
3990 testcase( regFree1==0 );
drhe99fa2a2008-12-15 15:27:51 +00003991 sqlite3VdbeAddOp2(v, op, r1, inReg);
drhcce7d172000-05-31 15:34:51 +00003992 break;
3993 }
drh8abed7b2018-02-26 18:49:05 +00003994 case TK_TRUTH: {
drh96acafb2018-02-27 14:49:25 +00003995 int isTrue; /* IS TRUE or IS NOT TRUE */
3996 int bNormal; /* IS TRUE or IS FALSE */
drh007c8432018-02-26 03:20:18 +00003997 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
3998 testcase( regFree1==0 );
drh96acafb2018-02-27 14:49:25 +00003999 isTrue = sqlite3ExprTruthValue(pExpr->pRight);
4000 bNormal = pExpr->op2==TK_IS;
4001 testcase( isTrue && bNormal);
4002 testcase( !isTrue && bNormal);
4003 sqlite3VdbeAddOp4Int(v, OP_IsTrue, r1, inReg, !isTrue, isTrue ^ bNormal);
drh007c8432018-02-26 03:20:18 +00004004 break;
4005 }
drhcce7d172000-05-31 15:34:51 +00004006 case TK_ISNULL:
4007 case TK_NOTNULL: {
drh6a288a32008-01-07 19:20:24 +00004008 int addr;
drh7d176102014-02-18 03:07:12 +00004009 assert( TK_ISNULL==OP_IsNull ); testcase( op==TK_ISNULL );
4010 assert( TK_NOTNULL==OP_NotNull ); testcase( op==TK_NOTNULL );
drh9de221d2008-01-05 06:51:30 +00004011 sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
drh2dcef112008-01-12 19:03:48 +00004012 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
drhc5499be2008-04-01 15:06:33 +00004013 testcase( regFree1==0 );
drh2dcef112008-01-12 19:03:48 +00004014 addr = sqlite3VdbeAddOp1(v, op, r1);
drh7d176102014-02-18 03:07:12 +00004015 VdbeCoverageIf(v, op==TK_ISNULL);
4016 VdbeCoverageIf(v, op==TK_NOTNULL);
drha9769792014-08-04 16:39:39 +00004017 sqlite3VdbeAddOp2(v, OP_Integer, 0, target);
drh6a288a32008-01-07 19:20:24 +00004018 sqlite3VdbeJumpHere(v, addr);
drhf2bc0132004-10-04 13:19:23 +00004019 break;
drhcce7d172000-05-31 15:34:51 +00004020 }
drh22827922000-06-06 17:27:05 +00004021 case TK_AGG_FUNCTION: {
drh13449892005-09-07 21:22:45 +00004022 AggInfo *pInfo = pExpr->pAggInfo;
drh7e56e712005-11-16 12:53:15 +00004023 if( pInfo==0 ){
drh33e619f2009-05-28 01:00:55 +00004024 assert( !ExprHasProperty(pExpr, EP_IntValue) );
4025 sqlite3ErrorMsg(pParse, "misuse of aggregate: %s()", pExpr->u.zToken);
drh7e56e712005-11-16 12:53:15 +00004026 }else{
drhc332cc32016-09-19 10:24:19 +00004027 return pInfo->aFunc[pExpr->iAgg].iMem;
drh7e56e712005-11-16 12:53:15 +00004028 }
drh22827922000-06-06 17:27:05 +00004029 break;
4030 }
drhcce7d172000-05-31 15:34:51 +00004031 case TK_FUNCTION: {
drh12ffee82009-04-08 13:51:51 +00004032 ExprList *pFarg; /* List of function arguments */
4033 int nFarg; /* Number of function arguments */
4034 FuncDef *pDef; /* The function definition object */
drh12ffee82009-04-08 13:51:51 +00004035 const char *zId; /* The function name */
drh693e6712014-01-24 22:58:00 +00004036 u32 constMask = 0; /* Mask of function arguments that are constant */
drh12ffee82009-04-08 13:51:51 +00004037 int i; /* Loop counter */
drhc332cc32016-09-19 10:24:19 +00004038 sqlite3 *db = pParse->db; /* The database connection */
drh12ffee82009-04-08 13:51:51 +00004039 u8 enc = ENC(db); /* The text encoding used by this database */
4040 CollSeq *pColl = 0; /* A collating sequence */
drh17435752007-08-16 04:30:38 +00004041
dan67a9b8e2018-06-22 20:51:35 +00004042#ifndef SQLITE_OMIT_WINDOWFUNC
drheda079c2018-09-20 19:02:15 +00004043 if( ExprHasProperty(pExpr, EP_WinFunc) ){
4044 return pExpr->y.pWin->regResult;
dan86fb6e12018-05-16 20:58:07 +00004045 }
dan67a9b8e2018-06-22 20:51:35 +00004046#endif
dan86fb6e12018-05-16 20:58:07 +00004047
drh1e9b53f2017-01-04 01:07:24 +00004048 if( ConstFactorOk(pParse) && sqlite3ExprIsConstantNotJoin(pExpr) ){
drh49c5ab22017-01-04 04:18:00 +00004049 /* SQL functions can be expensive. So try to move constant functions
drhad879ff2017-01-04 04:10:02 +00004050 ** out of the inner loop, even if that means an extra OP_Copy. */
4051 return sqlite3ExprCodeAtInit(pParse, pExpr, -1);
drh1e9b53f2017-01-04 01:07:24 +00004052 }
danielk19776ab3a2e2009-02-19 14:39:25 +00004053 assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
drhc5cd1242013-09-12 16:50:49 +00004054 if( ExprHasProperty(pExpr, EP_TokenOnly) ){
drh12ffee82009-04-08 13:51:51 +00004055 pFarg = 0;
4056 }else{
4057 pFarg = pExpr->x.pList;
4058 }
4059 nFarg = pFarg ? pFarg->nExpr : 0;
drh33e619f2009-05-28 01:00:55 +00004060 assert( !ExprHasProperty(pExpr, EP_IntValue) );
4061 zId = pExpr->u.zToken;
drh80738d92016-02-15 00:34:16 +00004062 pDef = sqlite3FindFunction(db, zId, nFarg, enc, 0);
drhcc153132016-08-04 12:35:17 +00004063#ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
4064 if( pDef==0 && pParse->explain ){
4065 pDef = sqlite3FindFunction(db, "unknown", nFarg, enc, 0);
4066 }
4067#endif
danb6e9f7a2018-05-19 14:15:29 +00004068 if( pDef==0 || pDef->xFinalize!=0 ){
drh80738d92016-02-15 00:34:16 +00004069 sqlite3ErrorMsg(pParse, "unknown function: %s()", zId);
drhfeb306f2009-08-18 16:05:46 +00004070 break;
4071 }
drh25c42962020-01-01 13:55:08 +00004072 if( pDef->funcFlags & SQLITE_FUNC_INLINE ){
4073 return exprCodeInlineFunction(pParse, pFarg,
4074 SQLITE_PTR_TO_INT(pDef->pUserData), target);
drhae6bb952009-11-11 00:24:31 +00004075 }
4076
drhd1a01ed2013-11-21 16:08:52 +00004077 for(i=0; i<nFarg; i++){
4078 if( i<32 && sqlite3ExprIsConstant(pFarg->a[i].pExpr) ){
drh693e6712014-01-24 22:58:00 +00004079 testcase( i==31 );
4080 constMask |= MASKBIT32(i);
drhd1a01ed2013-11-21 16:08:52 +00004081 }
4082 if( (pDef->funcFlags & SQLITE_FUNC_NEEDCOLL)!=0 && !pColl ){
4083 pColl = sqlite3ExprCollSeq(pParse, pFarg->a[i].pExpr);
4084 }
4085 }
drh12ffee82009-04-08 13:51:51 +00004086 if( pFarg ){
drhd1a01ed2013-11-21 16:08:52 +00004087 if( constMask ){
4088 r1 = pParse->nMem+1;
4089 pParse->nMem += nFarg;
4090 }else{
4091 r1 = sqlite3GetTempRange(pParse, nFarg);
4092 }
drha748fdc2012-03-28 01:34:47 +00004093
4094 /* For length() and typeof() functions with a column argument,
4095 ** set the P5 parameter to the OP_Column opcode to OPFLAG_LENGTHARG
4096 ** or OPFLAG_TYPEOFARG respectively, to avoid unnecessary data
4097 ** loading.
4098 */
drhd36e1042013-09-06 13:10:12 +00004099 if( (pDef->funcFlags & (SQLITE_FUNC_LENGTH|SQLITE_FUNC_TYPEOF))!=0 ){
drh4e245a42012-03-30 00:00:36 +00004100 u8 exprOp;
drha748fdc2012-03-28 01:34:47 +00004101 assert( nFarg==1 );
4102 assert( pFarg->a[0].pExpr!=0 );
drh4e245a42012-03-30 00:00:36 +00004103 exprOp = pFarg->a[0].pExpr->op;
4104 if( exprOp==TK_COLUMN || exprOp==TK_AGG_COLUMN ){
drha748fdc2012-03-28 01:34:47 +00004105 assert( SQLITE_FUNC_LENGTH==OPFLAG_LENGTHARG );
4106 assert( SQLITE_FUNC_TYPEOF==OPFLAG_TYPEOFARG );
drhb1fba282013-11-21 14:33:48 +00004107 testcase( pDef->funcFlags & OPFLAG_LENGTHARG );
4108 pFarg->a[0].pExpr->op2 =
4109 pDef->funcFlags & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG);
drha748fdc2012-03-28 01:34:47 +00004110 }
4111 }
4112
drh5579d592015-08-26 14:01:41 +00004113 sqlite3ExprCodeExprList(pParse, pFarg, r1, 0,
drhd1a01ed2013-11-21 16:08:52 +00004114 SQLITE_ECEL_DUP|SQLITE_ECEL_FACTOR);
drh892d3172008-01-10 03:46:36 +00004115 }else{
drh12ffee82009-04-08 13:51:51 +00004116 r1 = 0;
drh892d3172008-01-10 03:46:36 +00004117 }
drhb7f6f682006-07-08 17:06:43 +00004118#ifndef SQLITE_OMIT_VIRTUALTABLE
drha43fa222006-07-08 18:41:37 +00004119 /* Possibly overload the function if the first argument is
4120 ** a virtual table column.
4121 **
4122 ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the
4123 ** second argument, not the first, as the argument to test to
4124 ** see if it is a column in a virtual table. This is done because
4125 ** the left operand of infix functions (the operand we want to
4126 ** control overloading) ends up as the second argument to the
4127 ** function. The expression "A glob B" is equivalent to
4128 ** "glob(B,A). We want to use the A in "A glob B" to test
4129 ** for function overloading. But we use the B term in "glob(B,A)".
4130 */
drh59155062018-05-26 18:03:48 +00004131 if( nFarg>=2 && ExprHasProperty(pExpr, EP_InfixFunc) ){
drh12ffee82009-04-08 13:51:51 +00004132 pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[1].pExpr);
4133 }else if( nFarg>0 ){
4134 pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[0].pExpr);
drhb7f6f682006-07-08 17:06:43 +00004135 }
4136#endif
drhd36e1042013-09-06 13:10:12 +00004137 if( pDef->funcFlags & SQLITE_FUNC_NEEDCOLL ){
drh8b213892008-08-29 02:14:02 +00004138 if( !pColl ) pColl = db->pDfltColl;
drh66a51672008-01-03 00:01:23 +00004139 sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ);
danielk1977682f68b2004-06-05 10:22:17 +00004140 }
drh092457b2017-12-29 15:04:49 +00004141#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
4142 if( pDef->funcFlags & SQLITE_FUNC_OFFSET ){
drh2fc865c2017-12-16 20:20:37 +00004143 Expr *pArg = pFarg->a[0].pExpr;
4144 if( pArg->op==TK_COLUMN ){
drh092457b2017-12-29 15:04:49 +00004145 sqlite3VdbeAddOp3(v, OP_Offset, pArg->iTable, pArg->iColumn, target);
drh2fc865c2017-12-16 20:20:37 +00004146 }else{
4147 sqlite3VdbeAddOp2(v, OP_Null, 0, target);
4148 }
drh092457b2017-12-29 15:04:49 +00004149 }else
4150#endif
4151 {
drh920cf592019-10-30 16:29:02 +00004152 sqlite3VdbeAddFunctionCall(pParse, constMask, r1, target, nFarg,
drh20cee7d2019-10-30 18:50:08 +00004153 pDef, pExpr->op2);
drh2fc865c2017-12-16 20:20:37 +00004154 }
drh13d79502019-12-23 02:18:49 +00004155 if( nFarg ){
4156 if( constMask==0 ){
4157 sqlite3ReleaseTempRange(pParse, r1, nFarg);
4158 }else{
drh3aef2fb2020-01-02 17:46:02 +00004159 sqlite3VdbeReleaseRegisters(pParse, r1, nFarg, constMask, 1);
drh13d79502019-12-23 02:18:49 +00004160 }
drh2dcef112008-01-12 19:03:48 +00004161 }
drhc332cc32016-09-19 10:24:19 +00004162 return target;
drhcce7d172000-05-31 15:34:51 +00004163 }
drhfe2093d2005-01-20 22:48:47 +00004164#ifndef SQLITE_OMIT_SUBQUERY
4165 case TK_EXISTS:
drh19a775c2000-06-05 18:54:46 +00004166 case TK_SELECT: {
dan8da209b2016-07-26 18:06:08 +00004167 int nCol;
drhc5499be2008-04-01 15:06:33 +00004168 testcase( op==TK_EXISTS );
4169 testcase( op==TK_SELECT );
dan8da209b2016-07-26 18:06:08 +00004170 if( op==TK_SELECT && (nCol = pExpr->x.pSelect->pEList->nExpr)!=1 ){
4171 sqlite3SubselectError(pParse, nCol, 1);
4172 }else{
drh85bcdce2018-12-23 21:27:29 +00004173 return sqlite3CodeSubselect(pParse, pExpr);
dan8da209b2016-07-26 18:06:08 +00004174 }
drh19a775c2000-06-05 18:54:46 +00004175 break;
4176 }
drhfc7f27b2016-08-20 00:07:01 +00004177 case TK_SELECT_COLUMN: {
drh966e2912017-01-03 02:58:01 +00004178 int n;
drhfc7f27b2016-08-20 00:07:01 +00004179 if( pExpr->pLeft->iTable==0 ){
drh85bcdce2018-12-23 21:27:29 +00004180 pExpr->pLeft->iTable = sqlite3CodeSubselect(pParse, pExpr->pLeft);
drhfc7f27b2016-08-20 00:07:01 +00004181 }
drh966e2912017-01-03 02:58:01 +00004182 assert( pExpr->iTable==0 || pExpr->pLeft->op==TK_SELECT );
drh554a9dc2019-08-26 14:18:28 +00004183 if( pExpr->iTable!=0
4184 && pExpr->iTable!=(n = sqlite3ExprVectorSize(pExpr->pLeft))
drh966e2912017-01-03 02:58:01 +00004185 ){
4186 sqlite3ErrorMsg(pParse, "%d columns assigned %d values",
4187 pExpr->iTable, n);
4188 }
drhc332cc32016-09-19 10:24:19 +00004189 return pExpr->pLeft->iTable + pExpr->iColumn;
drhfc7f27b2016-08-20 00:07:01 +00004190 }
drhfef52082000-06-06 01:50:43 +00004191 case TK_IN: {
drhec4ccdb2018-12-29 02:26:59 +00004192 int destIfFalse = sqlite3VdbeMakeLabel(pParse);
4193 int destIfNull = sqlite3VdbeMakeLabel(pParse);
drhe3365e62009-11-12 17:52:24 +00004194 sqlite3VdbeAddOp2(v, OP_Null, 0, target);
4195 sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull);
4196 sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
4197 sqlite3VdbeResolveLabel(v, destIfFalse);
4198 sqlite3VdbeAddOp2(v, OP_AddImm, target, 0);
4199 sqlite3VdbeResolveLabel(v, destIfNull);
drhc332cc32016-09-19 10:24:19 +00004200 return target;
drhfef52082000-06-06 01:50:43 +00004201 }
drhe3365e62009-11-12 17:52:24 +00004202#endif /* SQLITE_OMIT_SUBQUERY */
4203
4204
drh2dcef112008-01-12 19:03:48 +00004205 /*
4206 ** x BETWEEN y AND z
4207 **
4208 ** This is equivalent to
4209 **
4210 ** x>=y AND x<=z
4211 **
4212 ** X is stored in pExpr->pLeft.
4213 ** Y is stored in pExpr->pList->a[0].pExpr.
4214 ** Z is stored in pExpr->pList->a[1].pExpr.
4215 */
drhfef52082000-06-06 01:50:43 +00004216 case TK_BETWEEN: {
dan71c57db2016-07-09 20:23:55 +00004217 exprCodeBetween(pParse, pExpr, target, 0, 0);
drhc332cc32016-09-19 10:24:19 +00004218 return target;
drhfef52082000-06-06 01:50:43 +00004219 }
drh94fa9c42016-02-27 21:16:04 +00004220 case TK_SPAN:
drhae80dde2012-12-06 21:16:43 +00004221 case TK_COLLATE:
drh4f07e5f2007-05-14 11:34:46 +00004222 case TK_UPLUS: {
drh1efa8022018-04-28 04:16:43 +00004223 pExpr = pExpr->pLeft;
drh59ee43a2018-05-29 15:18:31 +00004224 goto expr_code_doover; /* 2018-04-28: Prevent deep recursion. OSSFuzz. */
drha2e00042002-01-22 03:13:42 +00004225 }
drh2dcef112008-01-12 19:03:48 +00004226
dan165921a2009-08-28 18:53:45 +00004227 case TK_TRIGGER: {
dan65a7cd12009-09-01 12:16:01 +00004228 /* If the opcode is TK_TRIGGER, then the expression is a reference
4229 ** to a column in the new.* or old.* pseudo-tables available to
4230 ** trigger programs. In this case Expr.iTable is set to 1 for the
4231 ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
4232 ** is set to the column of the pseudo-table to read, or to -1 to
4233 ** read the rowid field.
4234 **
4235 ** The expression is implemented using an OP_Param opcode. The p1
4236 ** parameter is set to 0 for an old.rowid reference, or to (i+1)
4237 ** to reference another column of the old.* pseudo-table, where
4238 ** i is the index of the column. For a new.rowid reference, p1 is
4239 ** set to (n+1), where n is the number of columns in each pseudo-table.
4240 ** For a reference to any other column in the new.* pseudo-table, p1
4241 ** is set to (n+2+i), where n and i are as defined previously. For
4242 ** example, if the table on which triggers are being fired is
4243 ** declared as:
4244 **
4245 ** CREATE TABLE t1(a, b);
4246 **
4247 ** Then p1 is interpreted as follows:
4248 **
4249 ** p1==0 -> old.rowid p1==3 -> new.rowid
4250 ** p1==1 -> old.a p1==4 -> new.a
4251 ** p1==2 -> old.b p1==5 -> new.b
4252 */
drheda079c2018-09-20 19:02:15 +00004253 Table *pTab = pExpr->y.pTab;
drhdd6cc9b2019-10-19 18:47:27 +00004254 int iCol = pExpr->iColumn;
4255 int p1 = pExpr->iTable * (pTab->nCol+1) + 1
drh7fe2fc02019-12-07 00:22:18 +00004256 + sqlite3TableColumnToStorage(pTab, iCol);
dan65a7cd12009-09-01 12:16:01 +00004257
4258 assert( pExpr->iTable==0 || pExpr->iTable==1 );
drhdd6cc9b2019-10-19 18:47:27 +00004259 assert( iCol>=-1 && iCol<pTab->nCol );
4260 assert( pTab->iPKey<0 || iCol!=pTab->iPKey );
dan65a7cd12009-09-01 12:16:01 +00004261 assert( p1>=0 && p1<(pTab->nCol*2+2) );
4262
4263 sqlite3VdbeAddOp2(v, OP_Param, p1, target);
drh896494e2018-04-26 12:27:03 +00004264 VdbeComment((v, "r[%d]=%s.%s", target,
dan2bd93512009-08-31 08:22:46 +00004265 (pExpr->iTable ? "new" : "old"),
drhdd6cc9b2019-10-19 18:47:27 +00004266 (pExpr->iColumn<0 ? "rowid" : pExpr->y.pTab->aCol[iCol].zName)
dan165921a2009-08-28 18:53:45 +00004267 ));
dan65a7cd12009-09-01 12:16:01 +00004268
drh44dbca82010-01-13 04:22:20 +00004269#ifndef SQLITE_OMIT_FLOATING_POINT
dan65a7cd12009-09-01 12:16:01 +00004270 /* If the column has REAL affinity, it may currently be stored as an
drh113762a2014-11-19 16:36:25 +00004271 ** integer. Use OP_RealAffinity to make sure it is really real.
4272 **
4273 ** EVIDENCE-OF: R-60985-57662 SQLite will convert the value back to
4274 ** floating point when extracting it from the record. */
drhdd6cc9b2019-10-19 18:47:27 +00004275 if( iCol>=0 && pTab->aCol[iCol].affinity==SQLITE_AFF_REAL ){
dan2832ad42009-08-31 15:27:27 +00004276 sqlite3VdbeAddOp1(v, OP_RealAffinity, target);
4277 }
drh44dbca82010-01-13 04:22:20 +00004278#endif
dan165921a2009-08-28 18:53:45 +00004279 break;
4280 }
4281
dan71c57db2016-07-09 20:23:55 +00004282 case TK_VECTOR: {
drhe835bc12016-08-23 19:02:55 +00004283 sqlite3ErrorMsg(pParse, "row value misused");
dan71c57db2016-07-09 20:23:55 +00004284 break;
4285 }
4286
drh9e9a67a2019-08-17 17:07:15 +00004287 /* TK_IF_NULL_ROW Expr nodes are inserted ahead of expressions
4288 ** that derive from the right-hand table of a LEFT JOIN. The
4289 ** Expr.iTable value is the table number for the right-hand table.
4290 ** The expression is only evaluated if that table is not currently
4291 ** on a LEFT JOIN NULL row.
4292 */
drh31d6fd52017-04-14 19:03:10 +00004293 case TK_IF_NULL_ROW: {
4294 int addrINR;
drh9e9a67a2019-08-17 17:07:15 +00004295 u8 okConstFactor = pParse->okConstFactor;
drh31d6fd52017-04-14 19:03:10 +00004296 addrINR = sqlite3VdbeAddOp1(v, OP_IfNullRow, pExpr->iTable);
drh9e9a67a2019-08-17 17:07:15 +00004297 /* Temporarily disable factoring of constant expressions, since
4298 ** even though expressions may appear to be constant, they are not
4299 ** really constant because they originate from the right-hand side
4300 ** of a LEFT JOIN. */
4301 pParse->okConstFactor = 0;
drh31d6fd52017-04-14 19:03:10 +00004302 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
drh9e9a67a2019-08-17 17:07:15 +00004303 pParse->okConstFactor = okConstFactor;
drh31d6fd52017-04-14 19:03:10 +00004304 sqlite3VdbeJumpHere(v, addrINR);
4305 sqlite3VdbeChangeP3(v, addrINR, inReg);
4306 break;
4307 }
4308
drh2dcef112008-01-12 19:03:48 +00004309 /*
4310 ** Form A:
4311 ** CASE x WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
4312 **
4313 ** Form B:
4314 ** CASE WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
4315 **
4316 ** Form A is can be transformed into the equivalent form B as follows:
4317 ** CASE WHEN x=e1 THEN r1 WHEN x=e2 THEN r2 ...
4318 ** WHEN x=eN THEN rN ELSE y END
4319 **
4320 ** X (if it exists) is in pExpr->pLeft.
drhc5cd1242013-09-12 16:50:49 +00004321 ** Y is in the last element of pExpr->x.pList if pExpr->x.pList->nExpr is
4322 ** odd. The Y is also optional. If the number of elements in x.pList
4323 ** is even, then Y is omitted and the "otherwise" result is NULL.
drh2dcef112008-01-12 19:03:48 +00004324 ** Ei is in pExpr->pList->a[i*2] and Ri is pExpr->pList->a[i*2+1].
4325 **
4326 ** The result of the expression is the Ri for the first matching Ei,
4327 ** or if there is no matching Ei, the ELSE term Y, or if there is
4328 ** no ELSE term, NULL.
4329 */
drhaac30f92019-12-14 15:01:55 +00004330 case TK_CASE: {
drh2dcef112008-01-12 19:03:48 +00004331 int endLabel; /* GOTO label for end of CASE stmt */
4332 int nextCase; /* GOTO label for next WHEN clause */
4333 int nExpr; /* 2x number of WHEN terms */
4334 int i; /* Loop counter */
4335 ExprList *pEList; /* List of WHEN terms */
4336 struct ExprList_item *aListelem; /* Array of WHEN terms */
4337 Expr opCompare; /* The X==Ei expression */
drh2dcef112008-01-12 19:03:48 +00004338 Expr *pX; /* The X expression */
drh1bd10f82008-12-10 21:19:56 +00004339 Expr *pTest = 0; /* X==Ei (form A) or just Ei (form B) */
dan8b65e592019-07-17 14:34:17 +00004340 Expr *pDel = 0;
4341 sqlite3 *db = pParse->db;
drh17a7f8d2002-03-24 13:13:27 +00004342
danielk19776ab3a2e2009-02-19 14:39:25 +00004343 assert( !ExprHasProperty(pExpr, EP_xIsSelect) && pExpr->x.pList );
danielk19776ab3a2e2009-02-19 14:39:25 +00004344 assert(pExpr->x.pList->nExpr > 0);
4345 pEList = pExpr->x.pList;
drhbe5c89a2004-07-26 00:31:09 +00004346 aListelem = pEList->a;
4347 nExpr = pEList->nExpr;
drhec4ccdb2018-12-29 02:26:59 +00004348 endLabel = sqlite3VdbeMakeLabel(pParse);
drh2dcef112008-01-12 19:03:48 +00004349 if( (pX = pExpr->pLeft)!=0 ){
dan8b65e592019-07-17 14:34:17 +00004350 pDel = sqlite3ExprDup(db, pX, 0);
4351 if( db->mallocFailed ){
4352 sqlite3ExprDelete(db, pDel);
4353 break;
4354 }
drh33cd4902009-05-30 20:49:20 +00004355 testcase( pX->op==TK_COLUMN );
dan8b65e592019-07-17 14:34:17 +00004356 exprToRegister(pDel, exprCodeVector(pParse, pDel, &regFree1));
drhc5499be2008-04-01 15:06:33 +00004357 testcase( regFree1==0 );
drhabb9d5f2016-08-23 17:30:55 +00004358 memset(&opCompare, 0, sizeof(opCompare));
drh2dcef112008-01-12 19:03:48 +00004359 opCompare.op = TK_EQ;
dan8b65e592019-07-17 14:34:17 +00004360 opCompare.pLeft = pDel;
drh2dcef112008-01-12 19:03:48 +00004361 pTest = &opCompare;
drh8b1db072010-09-28 04:14:03 +00004362 /* Ticket b351d95f9cd5ef17e9d9dbae18f5ca8611190001:
4363 ** The value in regFree1 might get SCopy-ed into the file result.
4364 ** So make sure that the regFree1 register is not reused for other
4365 ** purposes and possibly overwritten. */
4366 regFree1 = 0;
drh17a7f8d2002-03-24 13:13:27 +00004367 }
drhc5cd1242013-09-12 16:50:49 +00004368 for(i=0; i<nExpr-1; i=i+2){
drh2dcef112008-01-12 19:03:48 +00004369 if( pX ){
drh1bd10f82008-12-10 21:19:56 +00004370 assert( pTest!=0 );
drh2dcef112008-01-12 19:03:48 +00004371 opCompare.pRight = aListelem[i].pExpr;
drh17a7f8d2002-03-24 13:13:27 +00004372 }else{
drh2dcef112008-01-12 19:03:48 +00004373 pTest = aListelem[i].pExpr;
drh17a7f8d2002-03-24 13:13:27 +00004374 }
drhec4ccdb2018-12-29 02:26:59 +00004375 nextCase = sqlite3VdbeMakeLabel(pParse);
drh33cd4902009-05-30 20:49:20 +00004376 testcase( pTest->op==TK_COLUMN );
drh2dcef112008-01-12 19:03:48 +00004377 sqlite3ExprIfFalse(pParse, pTest, nextCase, SQLITE_JUMPIFNULL);
drhc5499be2008-04-01 15:06:33 +00004378 testcase( aListelem[i+1].pExpr->op==TK_COLUMN );
drh9de221d2008-01-05 06:51:30 +00004379 sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target);
drh076e85f2015-09-03 13:46:12 +00004380 sqlite3VdbeGoto(v, endLabel);
drh2dcef112008-01-12 19:03:48 +00004381 sqlite3VdbeResolveLabel(v, nextCase);
drhf570f012002-05-31 15:51:25 +00004382 }
drhc5cd1242013-09-12 16:50:49 +00004383 if( (nExpr&1)!=0 ){
drhc5cd1242013-09-12 16:50:49 +00004384 sqlite3ExprCode(pParse, pEList->a[nExpr-1].pExpr, target);
drh17a7f8d2002-03-24 13:13:27 +00004385 }else{
drh9de221d2008-01-05 06:51:30 +00004386 sqlite3VdbeAddOp2(v, OP_Null, 0, target);
drh17a7f8d2002-03-24 13:13:27 +00004387 }
dan8b65e592019-07-17 14:34:17 +00004388 sqlite3ExprDelete(db, pDel);
drh2dcef112008-01-12 19:03:48 +00004389 sqlite3VdbeResolveLabel(v, endLabel);
danielk19776f349032002-06-11 02:25:40 +00004390 break;
4391 }
danielk19775338a5f2005-01-20 13:03:10 +00004392#ifndef SQLITE_OMIT_TRIGGER
danielk19776f349032002-06-11 02:25:40 +00004393 case TK_RAISE: {
drh11949042019-08-05 18:01:42 +00004394 assert( pExpr->affExpr==OE_Rollback
4395 || pExpr->affExpr==OE_Abort
4396 || pExpr->affExpr==OE_Fail
4397 || pExpr->affExpr==OE_Ignore
dan165921a2009-08-28 18:53:45 +00004398 );
dane0af83a2009-09-08 19:15:01 +00004399 if( !pParse->pTriggerTab ){
4400 sqlite3ErrorMsg(pParse,
4401 "RAISE() may only be used within a trigger-program");
4402 return 0;
4403 }
drh11949042019-08-05 18:01:42 +00004404 if( pExpr->affExpr==OE_Abort ){
dane0af83a2009-09-08 19:15:01 +00004405 sqlite3MayAbort(pParse);
4406 }
dan165921a2009-08-28 18:53:45 +00004407 assert( !ExprHasProperty(pExpr, EP_IntValue) );
drh11949042019-08-05 18:01:42 +00004408 if( pExpr->affExpr==OE_Ignore ){
dane0af83a2009-09-08 19:15:01 +00004409 sqlite3VdbeAddOp4(
4410 v, OP_Halt, SQLITE_OK, OE_Ignore, 0, pExpr->u.zToken,0);
drh688852a2014-02-17 22:40:43 +00004411 VdbeCoverage(v);
dane0af83a2009-09-08 19:15:01 +00004412 }else{
drh433dccf2013-02-09 15:37:11 +00004413 sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_TRIGGER,
drh11949042019-08-05 18:01:42 +00004414 pExpr->affExpr, pExpr->u.zToken, 0, 0);
dane0af83a2009-09-08 19:15:01 +00004415 }
4416
drhffe07b22005-11-03 00:41:17 +00004417 break;
drh17a7f8d2002-03-24 13:13:27 +00004418 }
danielk19775338a5f2005-01-20 13:03:10 +00004419#endif
drhffe07b22005-11-03 00:41:17 +00004420 }
drh2dcef112008-01-12 19:03:48 +00004421 sqlite3ReleaseTempReg(pParse, regFree1);
4422 sqlite3ReleaseTempReg(pParse, regFree2);
4423 return inReg;
4424}
4425
4426/*
drhd1a01ed2013-11-21 16:08:52 +00004427** Factor out the code of the given expression to initialization time.
drh1e9b53f2017-01-04 01:07:24 +00004428**
drhad879ff2017-01-04 04:10:02 +00004429** If regDest>=0 then the result is always stored in that register and the
4430** result is not reusable. If regDest<0 then this routine is free to
4431** store the value whereever it wants. The register where the expression
4432** is stored is returned. When regDest<0, two identical expressions will
4433** code to the same register.
drhd1a01ed2013-11-21 16:08:52 +00004434*/
drh1e9b53f2017-01-04 01:07:24 +00004435int sqlite3ExprCodeAtInit(
drhd673cdd2013-11-21 21:23:31 +00004436 Parse *pParse, /* Parsing context */
4437 Expr *pExpr, /* The expression to code when the VDBE initializes */
drhad879ff2017-01-04 04:10:02 +00004438 int regDest /* Store the value in this register */
drhd673cdd2013-11-21 21:23:31 +00004439){
drhd1a01ed2013-11-21 16:08:52 +00004440 ExprList *p;
drhd9f158e2013-11-21 20:48:42 +00004441 assert( ConstFactorOk(pParse) );
drhd1a01ed2013-11-21 16:08:52 +00004442 p = pParse->pConstExpr;
drhad879ff2017-01-04 04:10:02 +00004443 if( regDest<0 && p ){
4444 struct ExprList_item *pItem;
4445 int i;
4446 for(pItem=p->a, i=p->nExpr; i>0; pItem++, i--){
dan5aa550c2017-06-24 18:10:29 +00004447 if( pItem->reusable && sqlite3ExprCompare(0,pItem->pExpr,pExpr,-1)==0 ){
drhad879ff2017-01-04 04:10:02 +00004448 return pItem->u.iConstExprReg;
drh1e9b53f2017-01-04 01:07:24 +00004449 }
4450 }
drh1e9b53f2017-01-04 01:07:24 +00004451 }
drhd1a01ed2013-11-21 16:08:52 +00004452 pExpr = sqlite3ExprDup(pParse->db, pExpr, 0);
4453 p = sqlite3ExprListAppend(pParse, p, pExpr);
drhd673cdd2013-11-21 21:23:31 +00004454 if( p ){
4455 struct ExprList_item *pItem = &p->a[p->nExpr-1];
drhad879ff2017-01-04 04:10:02 +00004456 pItem->reusable = regDest<0;
4457 if( regDest<0 ) regDest = ++pParse->nMem;
drhd673cdd2013-11-21 21:23:31 +00004458 pItem->u.iConstExprReg = regDest;
drhd673cdd2013-11-21 21:23:31 +00004459 }
drhd1a01ed2013-11-21 16:08:52 +00004460 pParse->pConstExpr = p;
drh1e9b53f2017-01-04 01:07:24 +00004461 return regDest;
drhd1a01ed2013-11-21 16:08:52 +00004462}
4463
4464/*
drh2dcef112008-01-12 19:03:48 +00004465** Generate code to evaluate an expression and store the results
4466** into a register. Return the register number where the results
4467** are stored.
4468**
4469** If the register is a temporary register that can be deallocated,
drh678ccce2008-03-31 18:19:54 +00004470** then write its number into *pReg. If the result register is not
drh2dcef112008-01-12 19:03:48 +00004471** a temporary, then set *pReg to zero.
drhf30a9692013-11-15 01:10:18 +00004472**
4473** If pExpr is a constant, then this routine might generate this
4474** code to fill the register in the initialization section of the
4475** VDBE program, in order to factor it out of the evaluation loop.
drh2dcef112008-01-12 19:03:48 +00004476*/
4477int sqlite3ExprCodeTemp(Parse *pParse, Expr *pExpr, int *pReg){
drhf30a9692013-11-15 01:10:18 +00004478 int r2;
drh0d950af2019-08-22 16:38:42 +00004479 pExpr = sqlite3ExprSkipCollateAndLikely(pExpr);
drhd9f158e2013-11-21 20:48:42 +00004480 if( ConstFactorOk(pParse)
drhf30a9692013-11-15 01:10:18 +00004481 && pExpr->op!=TK_REGISTER
4482 && sqlite3ExprIsConstantNotJoin(pExpr)
4483 ){
drhf30a9692013-11-15 01:10:18 +00004484 *pReg = 0;
drhad879ff2017-01-04 04:10:02 +00004485 r2 = sqlite3ExprCodeAtInit(pParse, pExpr, -1);
drh2dcef112008-01-12 19:03:48 +00004486 }else{
drhf30a9692013-11-15 01:10:18 +00004487 int r1 = sqlite3GetTempReg(pParse);
4488 r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
4489 if( r2==r1 ){
4490 *pReg = r1;
4491 }else{
4492 sqlite3ReleaseTempReg(pParse, r1);
4493 *pReg = 0;
4494 }
drh2dcef112008-01-12 19:03:48 +00004495 }
4496 return r2;
4497}
4498
4499/*
4500** Generate code that will evaluate expression pExpr and store the
4501** results in register target. The results are guaranteed to appear
4502** in register target.
4503*/
drh05a86c52014-02-16 01:55:49 +00004504void sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){
drh9cbf3422008-01-17 16:22:13 +00004505 int inReg;
4506
4507 assert( target>0 && target<=pParse->nMem );
drhf5f19152019-10-21 01:04:11 +00004508 inReg = sqlite3ExprCodeTarget(pParse, pExpr, target);
4509 assert( pParse->pVdbe!=0 || pParse->db->mallocFailed );
4510 if( inReg!=target && pParse->pVdbe ){
drh629b88c2020-01-02 02:50:45 +00004511 u8 op;
4512 if( ExprHasProperty(pExpr,EP_Subquery) ){
4513 op = OP_Copy;
4514 }else{
4515 op = OP_SCopy;
4516 }
4517 sqlite3VdbeAddOp2(pParse->pVdbe, op, inReg, target);
drhcce7d172000-05-31 15:34:51 +00004518 }
drhcce7d172000-05-31 15:34:51 +00004519}
4520
4521/*
drh1c75c9d2015-12-21 15:22:13 +00004522** Make a transient copy of expression pExpr and then code it using
4523** sqlite3ExprCode(). This routine works just like sqlite3ExprCode()
4524** except that the input expression is guaranteed to be unchanged.
4525*/
4526void sqlite3ExprCodeCopy(Parse *pParse, Expr *pExpr, int target){
4527 sqlite3 *db = pParse->db;
4528 pExpr = sqlite3ExprDup(db, pExpr, 0);
4529 if( !db->mallocFailed ) sqlite3ExprCode(pParse, pExpr, target);
4530 sqlite3ExprDelete(db, pExpr);
4531}
4532
4533/*
drh05a86c52014-02-16 01:55:49 +00004534** Generate code that will evaluate expression pExpr and store the
4535** results in register target. The results are guaranteed to appear
4536** in register target. If the expression is constant, then this routine
4537** might choose to code the expression at initialization time.
4538*/
4539void sqlite3ExprCodeFactorable(Parse *pParse, Expr *pExpr, int target){
drhb8b06692018-08-04 15:16:20 +00004540 if( pParse->okConstFactor && sqlite3ExprIsConstantNotJoin(pExpr) ){
drhad879ff2017-01-04 04:10:02 +00004541 sqlite3ExprCodeAtInit(pParse, pExpr, target);
drh05a86c52014-02-16 01:55:49 +00004542 }else{
4543 sqlite3ExprCode(pParse, pExpr, target);
4544 }
drhcce7d172000-05-31 15:34:51 +00004545}
4546
4547/*
drh268380c2004-02-25 13:47:31 +00004548** Generate code that pushes the value of every element of the given
drh9cbf3422008-01-17 16:22:13 +00004549** expression list into a sequence of registers beginning at target.
drh268380c2004-02-25 13:47:31 +00004550**
drh3df6c3b2017-09-15 15:38:01 +00004551** Return the number of elements evaluated. The number returned will
4552** usually be pList->nExpr but might be reduced if SQLITE_ECEL_OMITREF
4553** is defined.
drhd1a01ed2013-11-21 16:08:52 +00004554**
4555** The SQLITE_ECEL_DUP flag prevents the arguments from being
4556** filled using OP_SCopy. OP_Copy must be used instead.
4557**
4558** The SQLITE_ECEL_FACTOR argument allows constant arguments to be
4559** factored out into initialization code.
drhb0df9632015-10-16 23:55:08 +00004560**
4561** The SQLITE_ECEL_REF flag means that expressions in the list with
4562** ExprList.a[].u.x.iOrderByCol>0 have already been evaluated and stored
4563** in registers at srcReg, and so the value can be copied from there.
drh3df6c3b2017-09-15 15:38:01 +00004564** If SQLITE_ECEL_OMITREF is also set, then the values with u.x.iOrderByCol>0
4565** are simply omitted rather than being copied from srcReg.
drh268380c2004-02-25 13:47:31 +00004566*/
danielk19774adee202004-05-08 08:23:19 +00004567int sqlite3ExprCodeExprList(
drh268380c2004-02-25 13:47:31 +00004568 Parse *pParse, /* Parsing context */
drh389a1ad2008-01-03 23:44:53 +00004569 ExprList *pList, /* The expression list to be coded */
drh191b54c2008-04-15 12:14:21 +00004570 int target, /* Where to write results */
drh5579d592015-08-26 14:01:41 +00004571 int srcReg, /* Source registers if SQLITE_ECEL_REF */
drhd1a01ed2013-11-21 16:08:52 +00004572 u8 flags /* SQLITE_ECEL_* flags */
drh268380c2004-02-25 13:47:31 +00004573){
4574 struct ExprList_item *pItem;
drh5579d592015-08-26 14:01:41 +00004575 int i, j, n;
drhd1a01ed2013-11-21 16:08:52 +00004576 u8 copyOp = (flags & SQLITE_ECEL_DUP) ? OP_Copy : OP_SCopy;
drh5579d592015-08-26 14:01:41 +00004577 Vdbe *v = pParse->pVdbe;
drh9d8b3072008-08-22 16:29:51 +00004578 assert( pList!=0 );
drh9cbf3422008-01-17 16:22:13 +00004579 assert( target>0 );
drhd81a1422010-09-28 07:11:24 +00004580 assert( pParse->pVdbe!=0 ); /* Never gets this far otherwise */
drh268380c2004-02-25 13:47:31 +00004581 n = pList->nExpr;
drhd9f158e2013-11-21 20:48:42 +00004582 if( !ConstFactorOk(pParse) ) flags &= ~SQLITE_ECEL_FACTOR;
drh191b54c2008-04-15 12:14:21 +00004583 for(pItem=pList->a, i=0; i<n; i++, pItem++){
drh7445ffe2010-09-27 18:14:12 +00004584 Expr *pExpr = pItem->pExpr;
dan24e25d32018-04-14 18:46:20 +00004585#ifdef SQLITE_ENABLE_SORTER_REFERENCES
4586 if( pItem->bSorterRef ){
4587 i--;
4588 n--;
4589 }else
4590#endif
dan257c13f2016-11-10 20:14:06 +00004591 if( (flags & SQLITE_ECEL_REF)!=0 && (j = pItem->u.x.iOrderByCol)>0 ){
4592 if( flags & SQLITE_ECEL_OMITREF ){
4593 i--;
4594 n--;
4595 }else{
4596 sqlite3VdbeAddOp2(v, copyOp, j+srcReg-1, target+i);
4597 }
drhb8b06692018-08-04 15:16:20 +00004598 }else if( (flags & SQLITE_ECEL_FACTOR)!=0
4599 && sqlite3ExprIsConstantNotJoin(pExpr)
4600 ){
drhad879ff2017-01-04 04:10:02 +00004601 sqlite3ExprCodeAtInit(pParse, pExpr, target+i);
drhd1a01ed2013-11-21 16:08:52 +00004602 }else{
4603 int inReg = sqlite3ExprCodeTarget(pParse, pExpr, target+i);
4604 if( inReg!=target+i ){
drh4eded602013-12-20 15:59:20 +00004605 VdbeOp *pOp;
drh4eded602013-12-20 15:59:20 +00004606 if( copyOp==OP_Copy
4607 && (pOp=sqlite3VdbeGetOp(v, -1))->opcode==OP_Copy
4608 && pOp->p1+pOp->p3+1==inReg
4609 && pOp->p2+pOp->p3+1==target+i
4610 ){
4611 pOp->p3++;
4612 }else{
4613 sqlite3VdbeAddOp2(v, copyOp, inReg, target+i);
4614 }
drhd1a01ed2013-11-21 16:08:52 +00004615 }
drhd1766112008-09-17 00:13:12 +00004616 }
drh268380c2004-02-25 13:47:31 +00004617 }
drhf9b596e2004-05-26 16:54:42 +00004618 return n;
drh268380c2004-02-25 13:47:31 +00004619}
4620
4621/*
drh36c563a2009-11-12 13:32:22 +00004622** Generate code for a BETWEEN operator.
4623**
4624** x BETWEEN y AND z
4625**
4626** The above is equivalent to
4627**
4628** x>=y AND x<=z
4629**
4630** Code it as such, taking care to do the common subexpression
peter.d.reid60ec9142014-09-06 16:39:46 +00004631** elimination of x.
drh84b19a32016-08-20 22:49:28 +00004632**
4633** The xJumpIf parameter determines details:
4634**
4635** NULL: Store the boolean result in reg[dest]
4636** sqlite3ExprIfTrue: Jump to dest if true
4637** sqlite3ExprIfFalse: Jump to dest if false
4638**
4639** The jumpIfNull parameter is ignored if xJumpIf is NULL.
drh36c563a2009-11-12 13:32:22 +00004640*/
4641static void exprCodeBetween(
4642 Parse *pParse, /* Parsing and code generating context */
4643 Expr *pExpr, /* The BETWEEN expression */
drh84b19a32016-08-20 22:49:28 +00004644 int dest, /* Jump destination or storage location */
4645 void (*xJump)(Parse*,Expr*,int,int), /* Action to take */
drh36c563a2009-11-12 13:32:22 +00004646 int jumpIfNull /* Take the jump if the BETWEEN is NULL */
4647){
dan8b65e592019-07-17 14:34:17 +00004648 Expr exprAnd; /* The AND operator in x>=y AND x<=z */
drh36c563a2009-11-12 13:32:22 +00004649 Expr compLeft; /* The x>=y term */
4650 Expr compRight; /* The x<=z term */
drhdb45bd52016-08-22 00:48:58 +00004651 int regFree1 = 0; /* Temporary use register */
dan8b65e592019-07-17 14:34:17 +00004652 Expr *pDel = 0;
4653 sqlite3 *db = pParse->db;
drh84b19a32016-08-20 22:49:28 +00004654
dan71c57db2016-07-09 20:23:55 +00004655 memset(&compLeft, 0, sizeof(Expr));
4656 memset(&compRight, 0, sizeof(Expr));
4657 memset(&exprAnd, 0, sizeof(Expr));
drhdb45bd52016-08-22 00:48:58 +00004658
4659 assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
dan8b65e592019-07-17 14:34:17 +00004660 pDel = sqlite3ExprDup(db, pExpr->pLeft, 0);
4661 if( db->mallocFailed==0 ){
4662 exprAnd.op = TK_AND;
4663 exprAnd.pLeft = &compLeft;
4664 exprAnd.pRight = &compRight;
4665 compLeft.op = TK_GE;
4666 compLeft.pLeft = pDel;
4667 compLeft.pRight = pExpr->x.pList->a[0].pExpr;
4668 compRight.op = TK_LE;
4669 compRight.pLeft = pDel;
4670 compRight.pRight = pExpr->x.pList->a[1].pExpr;
4671 exprToRegister(pDel, exprCodeVector(pParse, pDel, &regFree1));
4672 if( xJump ){
4673 xJump(pParse, &exprAnd, dest, jumpIfNull);
4674 }else{
4675 /* Mark the expression is being from the ON or USING clause of a join
4676 ** so that the sqlite3ExprCodeTarget() routine will not attempt to move
4677 ** it into the Parse.pConstExpr list. We should use a new bit for this,
4678 ** for clarity, but we are out of bits in the Expr.flags field so we
4679 ** have to reuse the EP_FromJoin bit. Bummer. */
4680 pDel->flags |= EP_FromJoin;
4681 sqlite3ExprCodeTarget(pParse, &exprAnd, dest);
4682 }
4683 sqlite3ReleaseTempReg(pParse, regFree1);
drh36c563a2009-11-12 13:32:22 +00004684 }
dan8b65e592019-07-17 14:34:17 +00004685 sqlite3ExprDelete(db, pDel);
drh36c563a2009-11-12 13:32:22 +00004686
4687 /* Ensure adequate test coverage */
drhdb45bd52016-08-22 00:48:58 +00004688 testcase( xJump==sqlite3ExprIfTrue && jumpIfNull==0 && regFree1==0 );
4689 testcase( xJump==sqlite3ExprIfTrue && jumpIfNull==0 && regFree1!=0 );
4690 testcase( xJump==sqlite3ExprIfTrue && jumpIfNull!=0 && regFree1==0 );
4691 testcase( xJump==sqlite3ExprIfTrue && jumpIfNull!=0 && regFree1!=0 );
4692 testcase( xJump==sqlite3ExprIfFalse && jumpIfNull==0 && regFree1==0 );
4693 testcase( xJump==sqlite3ExprIfFalse && jumpIfNull==0 && regFree1!=0 );
4694 testcase( xJump==sqlite3ExprIfFalse && jumpIfNull!=0 && regFree1==0 );
4695 testcase( xJump==sqlite3ExprIfFalse && jumpIfNull!=0 && regFree1!=0 );
drh84b19a32016-08-20 22:49:28 +00004696 testcase( xJump==0 );
drh36c563a2009-11-12 13:32:22 +00004697}
4698
4699/*
drhcce7d172000-05-31 15:34:51 +00004700** Generate code for a boolean expression such that a jump is made
4701** to the label "dest" if the expression is true but execution
4702** continues straight thru if the expression is false.
drhf5905aa2002-05-26 20:54:33 +00004703**
4704** If the expression evaluates to NULL (neither true nor false), then
drh35573352008-01-08 23:54:25 +00004705** take the jump if the jumpIfNull flag is SQLITE_JUMPIFNULL.
drhf2bc0132004-10-04 13:19:23 +00004706**
4707** This code depends on the fact that certain token values (ex: TK_EQ)
4708** are the same as opcode values (ex: OP_Eq) that implement the corresponding
4709** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
4710** the make process cause these values to align. Assert()s in the code
4711** below verify that the numbers are aligned correctly.
drhcce7d172000-05-31 15:34:51 +00004712*/
danielk19774adee202004-05-08 08:23:19 +00004713void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00004714 Vdbe *v = pParse->pVdbe;
4715 int op = 0;
drh2dcef112008-01-12 19:03:48 +00004716 int regFree1 = 0;
4717 int regFree2 = 0;
4718 int r1, r2;
4719
drh35573352008-01-08 23:54:25 +00004720 assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
mistachkin48864df2013-03-21 21:20:32 +00004721 if( NEVER(v==0) ) return; /* Existence of VDBE checked by caller */
drh33cd4902009-05-30 20:49:20 +00004722 if( NEVER(pExpr==0) ) return; /* No way this can happen */
drhf2bc0132004-10-04 13:19:23 +00004723 op = pExpr->op;
dan7b35a772016-07-28 19:47:15 +00004724 switch( op ){
drh17180fc2019-04-19 17:26:19 +00004725 case TK_AND:
drhcce7d172000-05-31 15:34:51 +00004726 case TK_OR: {
drh17180fc2019-04-19 17:26:19 +00004727 Expr *pAlt = sqlite3ExprSimplifiedAndOr(pExpr);
4728 if( pAlt!=pExpr ){
4729 sqlite3ExprIfTrue(pParse, pAlt, dest, jumpIfNull);
4730 }else if( op==TK_AND ){
4731 int d2 = sqlite3VdbeMakeLabel(pParse);
4732 testcase( jumpIfNull==0 );
4733 sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,
4734 jumpIfNull^SQLITE_JUMPIFNULL);
4735 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
4736 sqlite3VdbeResolveLabel(v, d2);
4737 }else{
4738 testcase( jumpIfNull==0 );
4739 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
4740 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
4741 }
drhcce7d172000-05-31 15:34:51 +00004742 break;
4743 }
4744 case TK_NOT: {
drhc5499be2008-04-01 15:06:33 +00004745 testcase( jumpIfNull==0 );
danielk19774adee202004-05-08 08:23:19 +00004746 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00004747 break;
4748 }
drh8abed7b2018-02-26 18:49:05 +00004749 case TK_TRUTH: {
drh96acafb2018-02-27 14:49:25 +00004750 int isNot; /* IS NOT TRUE or IS NOT FALSE */
4751 int isTrue; /* IS TRUE or IS NOT TRUE */
drh007c8432018-02-26 03:20:18 +00004752 testcase( jumpIfNull==0 );
drh8abed7b2018-02-26 18:49:05 +00004753 isNot = pExpr->op2==TK_ISNOT;
drh96acafb2018-02-27 14:49:25 +00004754 isTrue = sqlite3ExprTruthValue(pExpr->pRight);
drh43c4ac82018-02-26 21:26:27 +00004755 testcase( isTrue && isNot );
drh96acafb2018-02-27 14:49:25 +00004756 testcase( !isTrue && isNot );
drh43c4ac82018-02-26 21:26:27 +00004757 if( isTrue ^ isNot ){
drh8abed7b2018-02-26 18:49:05 +00004758 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest,
4759 isNot ? SQLITE_JUMPIFNULL : 0);
4760 }else{
4761 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest,
4762 isNot ? SQLITE_JUMPIFNULL : 0);
4763 }
drh007c8432018-02-26 03:20:18 +00004764 break;
4765 }
drhde845c22016-03-17 19:07:52 +00004766 case TK_IS:
4767 case TK_ISNOT:
4768 testcase( op==TK_IS );
4769 testcase( op==TK_ISNOT );
4770 op = (op==TK_IS) ? TK_EQ : TK_NE;
4771 jumpIfNull = SQLITE_NULLEQ;
4772 /* Fall thru */
drhcce7d172000-05-31 15:34:51 +00004773 case TK_LT:
4774 case TK_LE:
4775 case TK_GT:
4776 case TK_GE:
4777 case TK_NE:
drh0ac65892002-04-20 14:24:41 +00004778 case TK_EQ: {
dan625015e2016-07-30 16:39:28 +00004779 if( sqlite3ExprIsVector(pExpr->pLeft) ) goto default_expr;
drhc5499be2008-04-01 15:06:33 +00004780 testcase( jumpIfNull==0 );
drhb6da74e2009-12-24 16:00:28 +00004781 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
4782 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
drh35573352008-01-08 23:54:25 +00004783 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
drh898c5272019-10-22 00:03:41 +00004784 r1, r2, dest, jumpIfNull, ExprHasProperty(pExpr,EP_Commuted));
drh7d176102014-02-18 03:07:12 +00004785 assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
4786 assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
4787 assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
4788 assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
drhde845c22016-03-17 19:07:52 +00004789 assert(TK_EQ==OP_Eq); testcase(op==OP_Eq);
4790 VdbeCoverageIf(v, op==OP_Eq && jumpIfNull==SQLITE_NULLEQ);
4791 VdbeCoverageIf(v, op==OP_Eq && jumpIfNull!=SQLITE_NULLEQ);
4792 assert(TK_NE==OP_Ne); testcase(op==OP_Ne);
4793 VdbeCoverageIf(v, op==OP_Ne && jumpIfNull==SQLITE_NULLEQ);
4794 VdbeCoverageIf(v, op==OP_Ne && jumpIfNull!=SQLITE_NULLEQ);
drh6a2fe092009-09-23 02:29:36 +00004795 testcase( regFree1==0 );
4796 testcase( regFree2==0 );
4797 break;
4798 }
drhcce7d172000-05-31 15:34:51 +00004799 case TK_ISNULL:
4800 case TK_NOTNULL: {
drh7d176102014-02-18 03:07:12 +00004801 assert( TK_ISNULL==OP_IsNull ); testcase( op==TK_ISNULL );
4802 assert( TK_NOTNULL==OP_NotNull ); testcase( op==TK_NOTNULL );
drh2dcef112008-01-12 19:03:48 +00004803 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
4804 sqlite3VdbeAddOp2(v, op, r1, dest);
drh7d176102014-02-18 03:07:12 +00004805 VdbeCoverageIf(v, op==TK_ISNULL);
4806 VdbeCoverageIf(v, op==TK_NOTNULL);
drhc5499be2008-04-01 15:06:33 +00004807 testcase( regFree1==0 );
drhcce7d172000-05-31 15:34:51 +00004808 break;
4809 }
drhfef52082000-06-06 01:50:43 +00004810 case TK_BETWEEN: {
drh5c03f302009-11-13 15:03:59 +00004811 testcase( jumpIfNull==0 );
dan71c57db2016-07-09 20:23:55 +00004812 exprCodeBetween(pParse, pExpr, dest, sqlite3ExprIfTrue, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00004813 break;
4814 }
drh84e30ca2011-02-10 17:46:14 +00004815#ifndef SQLITE_OMIT_SUBQUERY
drhe3365e62009-11-12 17:52:24 +00004816 case TK_IN: {
drhec4ccdb2018-12-29 02:26:59 +00004817 int destIfFalse = sqlite3VdbeMakeLabel(pParse);
drhe3365e62009-11-12 17:52:24 +00004818 int destIfNull = jumpIfNull ? dest : destIfFalse;
4819 sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull);
drh076e85f2015-09-03 13:46:12 +00004820 sqlite3VdbeGoto(v, dest);
drhe3365e62009-11-12 17:52:24 +00004821 sqlite3VdbeResolveLabel(v, destIfFalse);
4822 break;
4823 }
shanehbb201342011-02-09 19:55:20 +00004824#endif
drhcce7d172000-05-31 15:34:51 +00004825 default: {
dan7b35a772016-07-28 19:47:15 +00004826 default_expr:
drhad317272019-04-19 16:21:51 +00004827 if( ExprAlwaysTrue(pExpr) ){
drh076e85f2015-09-03 13:46:12 +00004828 sqlite3VdbeGoto(v, dest);
drhad317272019-04-19 16:21:51 +00004829 }else if( ExprAlwaysFalse(pExpr) ){
drh991a1982014-01-02 17:57:16 +00004830 /* No-op */
4831 }else{
4832 r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
4833 sqlite3VdbeAddOp3(v, OP_If, r1, dest, jumpIfNull!=0);
drh688852a2014-02-17 22:40:43 +00004834 VdbeCoverage(v);
drh991a1982014-01-02 17:57:16 +00004835 testcase( regFree1==0 );
4836 testcase( jumpIfNull==0 );
4837 }
drhcce7d172000-05-31 15:34:51 +00004838 break;
4839 }
4840 }
drh2dcef112008-01-12 19:03:48 +00004841 sqlite3ReleaseTempReg(pParse, regFree1);
4842 sqlite3ReleaseTempReg(pParse, regFree2);
drhcce7d172000-05-31 15:34:51 +00004843}
4844
4845/*
drh66b89c82000-11-28 20:47:17 +00004846** Generate code for a boolean expression such that a jump is made
drhcce7d172000-05-31 15:34:51 +00004847** to the label "dest" if the expression is false but execution
4848** continues straight thru if the expression is true.
drhf5905aa2002-05-26 20:54:33 +00004849**
4850** If the expression evaluates to NULL (neither true nor false) then
drh35573352008-01-08 23:54:25 +00004851** jump if jumpIfNull is SQLITE_JUMPIFNULL or fall through if jumpIfNull
4852** is 0.
drhcce7d172000-05-31 15:34:51 +00004853*/
danielk19774adee202004-05-08 08:23:19 +00004854void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
drhcce7d172000-05-31 15:34:51 +00004855 Vdbe *v = pParse->pVdbe;
4856 int op = 0;
drh2dcef112008-01-12 19:03:48 +00004857 int regFree1 = 0;
4858 int regFree2 = 0;
4859 int r1, r2;
4860
drh35573352008-01-08 23:54:25 +00004861 assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
mistachkin48864df2013-03-21 21:20:32 +00004862 if( NEVER(v==0) ) return; /* Existence of VDBE checked by caller */
drh33cd4902009-05-30 20:49:20 +00004863 if( pExpr==0 ) return;
drhf2bc0132004-10-04 13:19:23 +00004864
4865 /* The value of pExpr->op and op are related as follows:
4866 **
4867 ** pExpr->op op
4868 ** --------- ----------
4869 ** TK_ISNULL OP_NotNull
4870 ** TK_NOTNULL OP_IsNull
4871 ** TK_NE OP_Eq
4872 ** TK_EQ OP_Ne
4873 ** TK_GT OP_Le
4874 ** TK_LE OP_Gt
4875 ** TK_GE OP_Lt
4876 ** TK_LT OP_Ge
4877 **
4878 ** For other values of pExpr->op, op is undefined and unused.
4879 ** The value of TK_ and OP_ constants are arranged such that we
4880 ** can compute the mapping above using the following expression.
4881 ** Assert()s verify that the computation is correct.
4882 */
4883 op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
4884
4885 /* Verify correct alignment of TK_ and OP_ constants
4886 */
4887 assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
4888 assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
4889 assert( pExpr->op!=TK_NE || op==OP_Eq );
4890 assert( pExpr->op!=TK_EQ || op==OP_Ne );
4891 assert( pExpr->op!=TK_LT || op==OP_Ge );
4892 assert( pExpr->op!=TK_LE || op==OP_Gt );
4893 assert( pExpr->op!=TK_GT || op==OP_Le );
4894 assert( pExpr->op!=TK_GE || op==OP_Lt );
4895
danba00e302016-07-23 20:24:06 +00004896 switch( pExpr->op ){
drh17180fc2019-04-19 17:26:19 +00004897 case TK_AND:
drhcce7d172000-05-31 15:34:51 +00004898 case TK_OR: {
drh17180fc2019-04-19 17:26:19 +00004899 Expr *pAlt = sqlite3ExprSimplifiedAndOr(pExpr);
4900 if( pAlt!=pExpr ){
4901 sqlite3ExprIfFalse(pParse, pAlt, dest, jumpIfNull);
4902 }else if( pExpr->op==TK_AND ){
4903 testcase( jumpIfNull==0 );
4904 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
4905 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
4906 }else{
4907 int d2 = sqlite3VdbeMakeLabel(pParse);
4908 testcase( jumpIfNull==0 );
4909 sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2,
4910 jumpIfNull^SQLITE_JUMPIFNULL);
4911 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
4912 sqlite3VdbeResolveLabel(v, d2);
4913 }
drhcce7d172000-05-31 15:34:51 +00004914 break;
4915 }
4916 case TK_NOT: {
drh5c03f302009-11-13 15:03:59 +00004917 testcase( jumpIfNull==0 );
danielk19774adee202004-05-08 08:23:19 +00004918 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
drhcce7d172000-05-31 15:34:51 +00004919 break;
4920 }
drh8abed7b2018-02-26 18:49:05 +00004921 case TK_TRUTH: {
drh96acafb2018-02-27 14:49:25 +00004922 int isNot; /* IS NOT TRUE or IS NOT FALSE */
4923 int isTrue; /* IS TRUE or IS NOT TRUE */
drh8abed7b2018-02-26 18:49:05 +00004924 testcase( jumpIfNull==0 );
drh8abed7b2018-02-26 18:49:05 +00004925 isNot = pExpr->op2==TK_ISNOT;
drh96acafb2018-02-27 14:49:25 +00004926 isTrue = sqlite3ExprTruthValue(pExpr->pRight);
drh43c4ac82018-02-26 21:26:27 +00004927 testcase( isTrue && isNot );
drh96acafb2018-02-27 14:49:25 +00004928 testcase( !isTrue && isNot );
drh43c4ac82018-02-26 21:26:27 +00004929 if( isTrue ^ isNot ){
drh8abed7b2018-02-26 18:49:05 +00004930 /* IS TRUE and IS NOT FALSE */
4931 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest,
4932 isNot ? 0 : SQLITE_JUMPIFNULL);
4933
4934 }else{
4935 /* IS FALSE and IS NOT TRUE */
4936 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest,
drh96acafb2018-02-27 14:49:25 +00004937 isNot ? 0 : SQLITE_JUMPIFNULL);
drh8abed7b2018-02-26 18:49:05 +00004938 }
drh007c8432018-02-26 03:20:18 +00004939 break;
4940 }
drhde845c22016-03-17 19:07:52 +00004941 case TK_IS:
4942 case TK_ISNOT:
4943 testcase( pExpr->op==TK_IS );
4944 testcase( pExpr->op==TK_ISNOT );
4945 op = (pExpr->op==TK_IS) ? TK_NE : TK_EQ;
4946 jumpIfNull = SQLITE_NULLEQ;
4947 /* Fall thru */
drhcce7d172000-05-31 15:34:51 +00004948 case TK_LT:
4949 case TK_LE:
4950 case TK_GT:
4951 case TK_GE:
4952 case TK_NE:
4953 case TK_EQ: {
dan625015e2016-07-30 16:39:28 +00004954 if( sqlite3ExprIsVector(pExpr->pLeft) ) goto default_expr;
drhc5499be2008-04-01 15:06:33 +00004955 testcase( jumpIfNull==0 );
drhb6da74e2009-12-24 16:00:28 +00004956 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
4957 r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
drh35573352008-01-08 23:54:25 +00004958 codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
drh898c5272019-10-22 00:03:41 +00004959 r1, r2, dest, jumpIfNull,ExprHasProperty(pExpr,EP_Commuted));
drh7d176102014-02-18 03:07:12 +00004960 assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
4961 assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
4962 assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
4963 assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
drhde845c22016-03-17 19:07:52 +00004964 assert(TK_EQ==OP_Eq); testcase(op==OP_Eq);
4965 VdbeCoverageIf(v, op==OP_Eq && jumpIfNull!=SQLITE_NULLEQ);
4966 VdbeCoverageIf(v, op==OP_Eq && jumpIfNull==SQLITE_NULLEQ);
4967 assert(TK_NE==OP_Ne); testcase(op==OP_Ne);
4968 VdbeCoverageIf(v, op==OP_Ne && jumpIfNull!=SQLITE_NULLEQ);
4969 VdbeCoverageIf(v, op==OP_Ne && jumpIfNull==SQLITE_NULLEQ);
drh6a2fe092009-09-23 02:29:36 +00004970 testcase( regFree1==0 );
4971 testcase( regFree2==0 );
4972 break;
4973 }
drhcce7d172000-05-31 15:34:51 +00004974 case TK_ISNULL:
4975 case TK_NOTNULL: {
drh2dcef112008-01-12 19:03:48 +00004976 r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
4977 sqlite3VdbeAddOp2(v, op, r1, dest);
drh7d176102014-02-18 03:07:12 +00004978 testcase( op==TK_ISNULL ); VdbeCoverageIf(v, op==TK_ISNULL);
4979 testcase( op==TK_NOTNULL ); VdbeCoverageIf(v, op==TK_NOTNULL);
drhc5499be2008-04-01 15:06:33 +00004980 testcase( regFree1==0 );
drhcce7d172000-05-31 15:34:51 +00004981 break;
4982 }
drhfef52082000-06-06 01:50:43 +00004983 case TK_BETWEEN: {
drh5c03f302009-11-13 15:03:59 +00004984 testcase( jumpIfNull==0 );
dan71c57db2016-07-09 20:23:55 +00004985 exprCodeBetween(pParse, pExpr, dest, sqlite3ExprIfFalse, jumpIfNull);
drhfef52082000-06-06 01:50:43 +00004986 break;
4987 }
drh84e30ca2011-02-10 17:46:14 +00004988#ifndef SQLITE_OMIT_SUBQUERY
drhe3365e62009-11-12 17:52:24 +00004989 case TK_IN: {
4990 if( jumpIfNull ){
4991 sqlite3ExprCodeIN(pParse, pExpr, dest, dest);
4992 }else{
drhec4ccdb2018-12-29 02:26:59 +00004993 int destIfNull = sqlite3VdbeMakeLabel(pParse);
drhe3365e62009-11-12 17:52:24 +00004994 sqlite3ExprCodeIN(pParse, pExpr, dest, destIfNull);
4995 sqlite3VdbeResolveLabel(v, destIfNull);
4996 }
4997 break;
4998 }
shanehbb201342011-02-09 19:55:20 +00004999#endif
drhcce7d172000-05-31 15:34:51 +00005000 default: {
danba00e302016-07-23 20:24:06 +00005001 default_expr:
drhad317272019-04-19 16:21:51 +00005002 if( ExprAlwaysFalse(pExpr) ){
drh076e85f2015-09-03 13:46:12 +00005003 sqlite3VdbeGoto(v, dest);
drhad317272019-04-19 16:21:51 +00005004 }else if( ExprAlwaysTrue(pExpr) ){
drh991a1982014-01-02 17:57:16 +00005005 /* no-op */
5006 }else{
5007 r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
5008 sqlite3VdbeAddOp3(v, OP_IfNot, r1, dest, jumpIfNull!=0);
drh688852a2014-02-17 22:40:43 +00005009 VdbeCoverage(v);
drh991a1982014-01-02 17:57:16 +00005010 testcase( regFree1==0 );
5011 testcase( jumpIfNull==0 );
5012 }
drhcce7d172000-05-31 15:34:51 +00005013 break;
5014 }
5015 }
drh2dcef112008-01-12 19:03:48 +00005016 sqlite3ReleaseTempReg(pParse, regFree1);
5017 sqlite3ReleaseTempReg(pParse, regFree2);
drhcce7d172000-05-31 15:34:51 +00005018}
drh22827922000-06-06 17:27:05 +00005019
5020/*
drh72bc8202015-06-11 13:58:35 +00005021** Like sqlite3ExprIfFalse() except that a copy is made of pExpr before
5022** code generation, and that copy is deleted after code generation. This
5023** ensures that the original pExpr is unchanged.
5024*/
5025void sqlite3ExprIfFalseDup(Parse *pParse, Expr *pExpr, int dest,int jumpIfNull){
5026 sqlite3 *db = pParse->db;
5027 Expr *pCopy = sqlite3ExprDup(db, pExpr, 0);
5028 if( db->mallocFailed==0 ){
5029 sqlite3ExprIfFalse(pParse, pCopy, dest, jumpIfNull);
5030 }
5031 sqlite3ExprDelete(db, pCopy);
5032}
5033
dan5aa550c2017-06-24 18:10:29 +00005034/*
5035** Expression pVar is guaranteed to be an SQL variable. pExpr may be any
5036** type of expression.
5037**
5038** If pExpr is a simple SQL value - an integer, real, string, blob
5039** or NULL value - then the VDBE currently being prepared is configured
5040** to re-prepare each time a new value is bound to variable pVar.
5041**
5042** Additionally, if pExpr is a simple SQL value and the value is the
5043** same as that currently bound to variable pVar, non-zero is returned.
5044** Otherwise, if the values are not the same or if pExpr is not a simple
5045** SQL value, zero is returned.
5046*/
5047static int exprCompareVariable(Parse *pParse, Expr *pVar, Expr *pExpr){
5048 int res = 0;
drhc0804222017-06-28 21:47:16 +00005049 int iVar;
5050 sqlite3_value *pL, *pR = 0;
5051
5052 sqlite3ValueFromExpr(pParse->db, pExpr, SQLITE_UTF8, SQLITE_AFF_BLOB, &pR);
5053 if( pR ){
5054 iVar = pVar->iColumn;
dan5aa550c2017-06-24 18:10:29 +00005055 sqlite3VdbeSetVarmask(pParse->pVdbe, iVar);
dan5aa550c2017-06-24 18:10:29 +00005056 pL = sqlite3VdbeGetBoundValue(pParse->pReprepare, iVar, SQLITE_AFF_BLOB);
drh5aa307e2017-06-29 01:23:12 +00005057 if( pL ){
5058 if( sqlite3_value_type(pL)==SQLITE_TEXT ){
5059 sqlite3_value_text(pL); /* Make sure the encoding is UTF-8 */
5060 }
5061 res = 0==sqlite3MemCompare(pL, pR, 0);
dan5aa550c2017-06-24 18:10:29 +00005062 }
drhc0804222017-06-28 21:47:16 +00005063 sqlite3ValueFree(pR);
5064 sqlite3ValueFree(pL);
dan5aa550c2017-06-24 18:10:29 +00005065 }
5066
5067 return res;
5068}
drh72bc8202015-06-11 13:58:35 +00005069
5070/*
drh1d9da702010-01-07 15:17:02 +00005071** Do a deep comparison of two expression trees. Return 0 if the two
5072** expressions are completely identical. Return 1 if they differ only
5073** by a COLLATE operator at the top level. Return 2 if there are differences
5074** other than the top-level COLLATE operator.
drhd40aab02007-02-24 15:29:03 +00005075**
drh619a1302013-08-01 13:04:46 +00005076** If any subelement of pB has Expr.iTable==(-1) then it is allowed
5077** to compare equal to an equivalent element in pA with Expr.iTable==iTab.
5078**
drh66518ca2013-08-01 15:09:57 +00005079** The pA side might be using TK_REGISTER. If that is the case and pB is
5080** not using TK_REGISTER but is otherwise equivalent, then still return 0.
5081**
drh1d9da702010-01-07 15:17:02 +00005082** Sometimes this routine will return 2 even if the two expressions
drhd40aab02007-02-24 15:29:03 +00005083** really are equivalent. If we cannot prove that the expressions are
drh1d9da702010-01-07 15:17:02 +00005084** identical, we return 2 just to be safe. So if this routine
5085** returns 2, then you do not really know for certain if the two
5086** expressions are the same. But if you get a 0 or 1 return, then you
drhd40aab02007-02-24 15:29:03 +00005087** can be sure the expressions are the same. In the places where
drh1d9da702010-01-07 15:17:02 +00005088** this routine is used, it does not hurt to get an extra 2 - that
drhd40aab02007-02-24 15:29:03 +00005089** just might result in some slightly slower code. But returning
drh1d9da702010-01-07 15:17:02 +00005090** an incorrect 0 or 1 could lead to a malfunction.
dan5aa550c2017-06-24 18:10:29 +00005091**
drhc0804222017-06-28 21:47:16 +00005092** If pParse is not NULL then TK_VARIABLE terms in pA with bindings in
5093** pParse->pReprepare can be matched against literals in pB. The
5094** pParse->pVdbe->expmask bitmask is updated for each variable referenced.
5095** If pParse is NULL (the normal case) then any TK_VARIABLE term in
5096** Argument pParse should normally be NULL. If it is not NULL and pA or
5097** pB causes a return value of 2.
drh22827922000-06-06 17:27:05 +00005098*/
dan5aa550c2017-06-24 18:10:29 +00005099int sqlite3ExprCompare(Parse *pParse, Expr *pA, Expr *pB, int iTab){
drh10d1edf2013-11-15 15:52:39 +00005100 u32 combinedFlags;
5101 if( pA==0 || pB==0 ){
drh1d9da702010-01-07 15:17:02 +00005102 return pB==pA ? 0 : 2;
drh22827922000-06-06 17:27:05 +00005103 }
dan5aa550c2017-06-24 18:10:29 +00005104 if( pParse && pA->op==TK_VARIABLE && exprCompareVariable(pParse, pA, pB) ){
5105 return 0;
5106 }
drh10d1edf2013-11-15 15:52:39 +00005107 combinedFlags = pA->flags | pB->flags;
5108 if( combinedFlags & EP_IntValue ){
5109 if( (pA->flags&pB->flags&EP_IntValue)!=0 && pA->u.iValue==pB->u.iValue ){
5110 return 0;
5111 }
drh1d9da702010-01-07 15:17:02 +00005112 return 2;
drh22827922000-06-06 17:27:05 +00005113 }
dan16dd3982018-12-20 15:04:38 +00005114 if( pA->op!=pB->op || pA->op==TK_RAISE ){
dan5aa550c2017-06-24 18:10:29 +00005115 if( pA->op==TK_COLLATE && sqlite3ExprCompare(pParse, pA->pLeft,pB,iTab)<2 ){
drhae80dde2012-12-06 21:16:43 +00005116 return 1;
5117 }
dan5aa550c2017-06-24 18:10:29 +00005118 if( pB->op==TK_COLLATE && sqlite3ExprCompare(pParse, pA,pB->pLeft,iTab)<2 ){
drhae80dde2012-12-06 21:16:43 +00005119 return 1;
5120 }
5121 return 2;
5122 }
drh2edc5fd2015-11-24 02:10:52 +00005123 if( pA->op!=TK_COLUMN && pA->op!=TK_AGG_COLUMN && pA->u.zToken ){
dan4f9adee2019-07-13 16:22:50 +00005124 if( pA->op==TK_FUNCTION || pA->op==TK_AGG_FUNCTION ){
drh390b88a2015-08-31 18:13:01 +00005125 if( sqlite3StrICmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2;
drheda079c2018-09-20 19:02:15 +00005126#ifndef SQLITE_OMIT_WINDOWFUNC
dan4f9adee2019-07-13 16:22:50 +00005127 assert( pA->op==pB->op );
5128 if( ExprHasProperty(pA,EP_WinFunc)!=ExprHasProperty(pB,EP_WinFunc) ){
5129 return 2;
5130 }
drheda079c2018-09-20 19:02:15 +00005131 if( ExprHasProperty(pA,EP_WinFunc) ){
dan4f9adee2019-07-13 16:22:50 +00005132 if( sqlite3WindowCompare(pParse, pA->y.pWin, pB->y.pWin, 1)!=0 ){
5133 return 2;
5134 }
drheda079c2018-09-20 19:02:15 +00005135 }
5136#endif
drhf20bbc52019-01-17 01:06:00 +00005137 }else if( pA->op==TK_NULL ){
5138 return 0;
drhd5af5422018-04-13 14:27:01 +00005139 }else if( pA->op==TK_COLLATE ){
drhe79f6292018-04-18 17:52:28 +00005140 if( sqlite3_stricmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2;
drhf20bbc52019-01-17 01:06:00 +00005141 }else if( ALWAYS(pB->u.zToken!=0) && strcmp(pA->u.zToken,pB->u.zToken)!=0 ){
drhd5af5422018-04-13 14:27:01 +00005142 return 2;
drh2646da72005-12-09 20:02:05 +00005143 }
drh22827922000-06-06 17:27:05 +00005144 }
drh898c5272019-10-22 00:03:41 +00005145 if( (pA->flags & (EP_Distinct|EP_Commuted))
5146 != (pB->flags & (EP_Distinct|EP_Commuted)) ) return 2;
drh89b6de02018-12-12 20:11:23 +00005147 if( (combinedFlags & EP_TokenOnly)==0 ){
drh10d1edf2013-11-15 15:52:39 +00005148 if( combinedFlags & EP_xIsSelect ) return 2;
drhefad2e22018-07-27 16:57:11 +00005149 if( (combinedFlags & EP_FixedCol)==0
5150 && sqlite3ExprCompare(pParse, pA->pLeft, pB->pLeft, iTab) ) return 2;
dan5aa550c2017-06-24 18:10:29 +00005151 if( sqlite3ExprCompare(pParse, pA->pRight, pB->pRight, iTab) ) return 2;
drh10d1edf2013-11-15 15:52:39 +00005152 if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList, iTab) ) return 2;
drh03c5c212018-12-12 11:23:40 +00005153 if( pA->op!=TK_STRING
5154 && pA->op!=TK_TRUEFALSE
5155 && (combinedFlags & EP_Reduced)==0
5156 ){
drh10d1edf2013-11-15 15:52:39 +00005157 if( pA->iColumn!=pB->iColumn ) return 2;
drh04307c82019-10-31 13:16:26 +00005158 if( pA->op2!=pB->op2 ){
5159 if( pA->op==TK_TRUTH ) return 2;
5160 if( pA->op==TK_FUNCTION && iTab<0 ){
5161 /* Ex: CREATE TABLE t1(a CHECK( a<julianday('now') ));
5162 ** INSERT INTO t1(a) VALUES(julianday('now')+10);
5163 ** Without this test, sqlite3ExprCodeAtInit() will run on the
5164 ** the julianday() of INSERT first, and remember that expression.
5165 ** Then sqlite3ExprCodeInit() will see the julianday() in the CHECK
5166 ** constraint as redundant, reusing the one from the INSERT, even
5167 ** though the julianday() in INSERT lacks the critical NC_IsCheck
5168 ** flag. See ticket [830277d9db6c3ba1] (2019-10-30)
5169 */
5170 return 2;
5171 }
5172 }
drh0f28e1b2019-10-28 13:07:01 +00005173 if( pA->op!=TK_IN && pA->iTable!=pB->iTable && pA->iTable!=iTab ){
5174 return 2;
5175 }
drh10d1edf2013-11-15 15:52:39 +00005176 }
5177 }
drh1d9da702010-01-07 15:17:02 +00005178 return 0;
drh22827922000-06-06 17:27:05 +00005179}
5180
drh8c6f6662010-04-26 19:17:26 +00005181/*
5182** Compare two ExprList objects. Return 0 if they are identical and
5183** non-zero if they differ in any way.
5184**
drh619a1302013-08-01 13:04:46 +00005185** If any subelement of pB has Expr.iTable==(-1) then it is allowed
5186** to compare equal to an equivalent element in pA with Expr.iTable==iTab.
5187**
drh8c6f6662010-04-26 19:17:26 +00005188** This routine might return non-zero for equivalent ExprLists. The
5189** only consequence will be disabled optimizations. But this routine
5190** must never return 0 if the two ExprList objects are different, or
5191** a malfunction will result.
5192**
5193** Two NULL pointers are considered to be the same. But a NULL pointer
5194** always differs from a non-NULL pointer.
5195*/
drh619a1302013-08-01 13:04:46 +00005196int sqlite3ExprListCompare(ExprList *pA, ExprList *pB, int iTab){
drh8c6f6662010-04-26 19:17:26 +00005197 int i;
5198 if( pA==0 && pB==0 ) return 0;
5199 if( pA==0 || pB==0 ) return 1;
5200 if( pA->nExpr!=pB->nExpr ) return 1;
5201 for(i=0; i<pA->nExpr; i++){
5202 Expr *pExprA = pA->a[i].pExpr;
5203 Expr *pExprB = pB->a[i].pExpr;
dan6e118922019-08-12 16:36:38 +00005204 if( pA->a[i].sortFlags!=pB->a[i].sortFlags ) return 1;
dan5aa550c2017-06-24 18:10:29 +00005205 if( sqlite3ExprCompare(0, pExprA, pExprB, iTab) ) return 1;
drh8c6f6662010-04-26 19:17:26 +00005206 }
5207 return 0;
5208}
drh13449892005-09-07 21:22:45 +00005209
drh22827922000-06-06 17:27:05 +00005210/*
drhf9463df2017-02-11 14:59:58 +00005211** Like sqlite3ExprCompare() except COLLATE operators at the top-level
5212** are ignored.
5213*/
5214int sqlite3ExprCompareSkip(Expr *pA, Expr *pB, int iTab){
dan5aa550c2017-06-24 18:10:29 +00005215 return sqlite3ExprCompare(0,
drh0d950af2019-08-22 16:38:42 +00005216 sqlite3ExprSkipCollateAndLikely(pA),
5217 sqlite3ExprSkipCollateAndLikely(pB),
drhf9463df2017-02-11 14:59:58 +00005218 iTab);
5219}
5220
5221/*
drhc51cf862019-05-11 19:36:03 +00005222** Return non-zero if Expr p can only be true if pNN is not NULL.
drh7a231b42019-08-30 15:11:08 +00005223**
5224** Or if seenNot is true, return non-zero if Expr p can only be
5225** non-NULL if pNN is not NULL
drhc51cf862019-05-11 19:36:03 +00005226*/
5227static int exprImpliesNotNull(
5228 Parse *pParse, /* Parsing context */
5229 Expr *p, /* The expression to be checked */
5230 Expr *pNN, /* The expression that is NOT NULL */
5231 int iTab, /* Table being evaluated */
drh7a231b42019-08-30 15:11:08 +00005232 int seenNot /* Return true only if p can be any non-NULL value */
drhc51cf862019-05-11 19:36:03 +00005233){
5234 assert( p );
5235 assert( pNN );
drh14c865e2019-08-10 15:06:03 +00005236 if( sqlite3ExprCompare(pParse, p, pNN, iTab)==0 ){
5237 return pNN->op!=TK_NULL;
5238 }
drhc51cf862019-05-11 19:36:03 +00005239 switch( p->op ){
5240 case TK_IN: {
5241 if( seenNot && ExprHasProperty(p, EP_xIsSelect) ) return 0;
5242 assert( ExprHasProperty(p,EP_xIsSelect)
5243 || (p->x.pList!=0 && p->x.pList->nExpr>0) );
drhae144a12019-08-30 16:00:58 +00005244 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1);
drhc51cf862019-05-11 19:36:03 +00005245 }
5246 case TK_BETWEEN: {
5247 ExprList *pList = p->x.pList;
5248 assert( pList!=0 );
5249 assert( pList->nExpr==2 );
5250 if( seenNot ) return 0;
drh7a231b42019-08-30 15:11:08 +00005251 if( exprImpliesNotNull(pParse, pList->a[0].pExpr, pNN, iTab, 1)
5252 || exprImpliesNotNull(pParse, pList->a[1].pExpr, pNN, iTab, 1)
drhc51cf862019-05-11 19:36:03 +00005253 ){
5254 return 1;
5255 }
drh7a231b42019-08-30 15:11:08 +00005256 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1);
drhc51cf862019-05-11 19:36:03 +00005257 }
5258 case TK_EQ:
5259 case TK_NE:
5260 case TK_LT:
5261 case TK_LE:
5262 case TK_GT:
5263 case TK_GE:
5264 case TK_PLUS:
5265 case TK_MINUS:
dan9d23ea72019-08-29 19:34:29 +00005266 case TK_BITOR:
5267 case TK_LSHIFT:
5268 case TK_RSHIFT:
5269 case TK_CONCAT:
5270 seenNot = 1;
5271 /* Fall thru */
drhc51cf862019-05-11 19:36:03 +00005272 case TK_STAR:
5273 case TK_REM:
5274 case TK_BITAND:
dan9d23ea72019-08-29 19:34:29 +00005275 case TK_SLASH: {
drhc51cf862019-05-11 19:36:03 +00005276 if( exprImpliesNotNull(pParse, p->pRight, pNN, iTab, seenNot) ) return 1;
5277 /* Fall thru into the next case */
5278 }
5279 case TK_SPAN:
5280 case TK_COLLATE:
drhc51cf862019-05-11 19:36:03 +00005281 case TK_UPLUS:
5282 case TK_UMINUS: {
5283 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, seenNot);
5284 }
5285 case TK_TRUTH: {
5286 if( seenNot ) return 0;
5287 if( p->op2!=TK_IS ) return 0;
drh38cefc82019-08-30 13:07:06 +00005288 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1);
drhc51cf862019-05-11 19:36:03 +00005289 }
dan1cd382e2019-08-29 15:06:35 +00005290 case TK_BITNOT:
drhc51cf862019-05-11 19:36:03 +00005291 case TK_NOT: {
5292 return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1);
5293 }
5294 }
5295 return 0;
5296}
5297
5298/*
drh4bd5f732013-07-31 23:22:39 +00005299** Return true if we can prove the pE2 will always be true if pE1 is
5300** true. Return false if we cannot complete the proof or if pE2 might
5301** be false. Examples:
5302**
drh619a1302013-08-01 13:04:46 +00005303** pE1: x==5 pE2: x==5 Result: true
5304** pE1: x>0 pE2: x==5 Result: false
5305** pE1: x=21 pE2: x=21 OR y=43 Result: true
5306** pE1: x!=123 pE2: x IS NOT NULL Result: true
5307** pE1: x!=?1 pE2: x IS NOT NULL Result: true
5308** pE1: x IS NULL pE2: x IS NOT NULL Result: false
5309** pE1: x IS ?2 pE2: x IS NOT NULL Reuslt: false
drh4bd5f732013-07-31 23:22:39 +00005310**
5311** When comparing TK_COLUMN nodes between pE1 and pE2, if pE2 has
5312** Expr.iTable<0 then assume a table number given by iTab.
5313**
drhc0804222017-06-28 21:47:16 +00005314** If pParse is not NULL, then the values of bound variables in pE1 are
5315** compared against literal values in pE2 and pParse->pVdbe->expmask is
5316** modified to record which bound variables are referenced. If pParse
5317** is NULL, then false will be returned if pE1 contains any bound variables.
5318**
drh4bd5f732013-07-31 23:22:39 +00005319** When in doubt, return false. Returning true might give a performance
5320** improvement. Returning false might cause a performance reduction, but
5321** it will always give the correct answer and is hence always safe.
5322*/
dan5aa550c2017-06-24 18:10:29 +00005323int sqlite3ExprImpliesExpr(Parse *pParse, Expr *pE1, Expr *pE2, int iTab){
5324 if( sqlite3ExprCompare(pParse, pE1, pE2, iTab)==0 ){
drh619a1302013-08-01 13:04:46 +00005325 return 1;
5326 }
5327 if( pE2->op==TK_OR
dan5aa550c2017-06-24 18:10:29 +00005328 && (sqlite3ExprImpliesExpr(pParse, pE1, pE2->pLeft, iTab)
5329 || sqlite3ExprImpliesExpr(pParse, pE1, pE2->pRight, iTab) )
drh619a1302013-08-01 13:04:46 +00005330 ){
5331 return 1;
5332 }
drh664d6d12019-05-04 17:32:07 +00005333 if( pE2->op==TK_NOTNULL
drhc51cf862019-05-11 19:36:03 +00005334 && exprImpliesNotNull(pParse, pE1, pE2->pLeft, iTab, 0)
drh664d6d12019-05-04 17:32:07 +00005335 ){
drhc51cf862019-05-11 19:36:03 +00005336 return 1;
drh619a1302013-08-01 13:04:46 +00005337 }
5338 return 0;
drh4bd5f732013-07-31 23:22:39 +00005339}
5340
5341/*
drh6c68d752019-11-04 02:05:52 +00005342** This is the Expr node callback for sqlite3ExprImpliesNonNullRow().
drh25897872018-03-20 21:16:15 +00005343** If the expression node requires that the table at pWalker->iCur
drhf8937f92018-09-23 02:01:42 +00005344** have one or more non-NULL column, then set pWalker->eCode to 1 and abort.
5345**
5346** This routine controls an optimization. False positives (setting
5347** pWalker->eCode to 1 when it should not be) are deadly, but false-negatives
5348** (never setting pWalker->eCode) is a harmless missed optimization.
drh25897872018-03-20 21:16:15 +00005349*/
5350static int impliesNotNullRow(Walker *pWalker, Expr *pExpr){
drhf8937f92018-09-23 02:01:42 +00005351 testcase( pExpr->op==TK_AGG_COLUMN );
drh821b6102018-03-24 18:01:51 +00005352 testcase( pExpr->op==TK_AGG_FUNCTION );
drh25897872018-03-20 21:16:15 +00005353 if( ExprHasProperty(pExpr, EP_FromJoin) ) return WRC_Prune;
5354 switch( pExpr->op ){
dan04932222018-04-10 15:31:56 +00005355 case TK_ISNOT:
drh25897872018-03-20 21:16:15 +00005356 case TK_ISNULL:
drhd5793672019-02-05 14:36:33 +00005357 case TK_NOTNULL:
drh25897872018-03-20 21:16:15 +00005358 case TK_IS:
5359 case TK_OR:
drh6c68d752019-11-04 02:05:52 +00005360 case TK_VECTOR:
drh2c492062018-03-24 13:24:02 +00005361 case TK_CASE:
drhe3eff262018-03-24 15:47:31 +00005362 case TK_IN:
drh25897872018-03-20 21:16:15 +00005363 case TK_FUNCTION:
danda03c1e2019-10-09 21:14:00 +00005364 case TK_TRUTH:
dan04932222018-04-10 15:31:56 +00005365 testcase( pExpr->op==TK_ISNOT );
drh821b6102018-03-24 18:01:51 +00005366 testcase( pExpr->op==TK_ISNULL );
drhd5793672019-02-05 14:36:33 +00005367 testcase( pExpr->op==TK_NOTNULL );
drh821b6102018-03-24 18:01:51 +00005368 testcase( pExpr->op==TK_IS );
5369 testcase( pExpr->op==TK_OR );
drh6c68d752019-11-04 02:05:52 +00005370 testcase( pExpr->op==TK_VECTOR );
drh821b6102018-03-24 18:01:51 +00005371 testcase( pExpr->op==TK_CASE );
5372 testcase( pExpr->op==TK_IN );
5373 testcase( pExpr->op==TK_FUNCTION );
danda03c1e2019-10-09 21:14:00 +00005374 testcase( pExpr->op==TK_TRUTH );
drh25897872018-03-20 21:16:15 +00005375 return WRC_Prune;
5376 case TK_COLUMN:
drh25897872018-03-20 21:16:15 +00005377 if( pWalker->u.iCur==pExpr->iTable ){
5378 pWalker->eCode = 1;
5379 return WRC_Abort;
5380 }
5381 return WRC_Prune;
drh98811552018-04-03 14:04:48 +00005382
dan9d23ea72019-08-29 19:34:29 +00005383 case TK_AND:
drhaef81672020-01-01 16:43:41 +00005384 if( pWalker->eCode==0 ){
5385 sqlite3WalkExpr(pWalker, pExpr->pLeft);
5386 if( pWalker->eCode ){
5387 pWalker->eCode = 0;
5388 sqlite3WalkExpr(pWalker, pExpr->pRight);
5389 }
dan9d23ea72019-08-29 19:34:29 +00005390 }
5391 return WRC_Prune;
5392
5393 case TK_BETWEEN:
dan1d24a532019-12-23 15:17:11 +00005394 if( sqlite3WalkExpr(pWalker, pExpr->pLeft)==WRC_Abort ){
5395 assert( pWalker->eCode );
5396 return WRC_Abort;
5397 }
dan9d23ea72019-08-29 19:34:29 +00005398 return WRC_Prune;
5399
drh98811552018-04-03 14:04:48 +00005400 /* Virtual tables are allowed to use constraints like x=NULL. So
5401 ** a term of the form x=y does not prove that y is not null if x
5402 ** is the column of a virtual table */
5403 case TK_EQ:
5404 case TK_NE:
5405 case TK_LT:
5406 case TK_LE:
5407 case TK_GT:
5408 case TK_GE:
5409 testcase( pExpr->op==TK_EQ );
5410 testcase( pExpr->op==TK_NE );
5411 testcase( pExpr->op==TK_LT );
5412 testcase( pExpr->op==TK_LE );
5413 testcase( pExpr->op==TK_GT );
5414 testcase( pExpr->op==TK_GE );
drheda079c2018-09-20 19:02:15 +00005415 if( (pExpr->pLeft->op==TK_COLUMN && IsVirtual(pExpr->pLeft->y.pTab))
5416 || (pExpr->pRight->op==TK_COLUMN && IsVirtual(pExpr->pRight->y.pTab))
drh98811552018-04-03 14:04:48 +00005417 ){
5418 return WRC_Prune;
5419 }
dan9d23ea72019-08-29 19:34:29 +00005420
drh25897872018-03-20 21:16:15 +00005421 default:
5422 return WRC_Continue;
5423 }
5424}
5425
5426/*
5427** Return true (non-zero) if expression p can only be true if at least
5428** one column of table iTab is non-null. In other words, return true
5429** if expression p will always be NULL or false if every column of iTab
5430** is NULL.
5431**
drh821b6102018-03-24 18:01:51 +00005432** False negatives are acceptable. In other words, it is ok to return
5433** zero even if expression p will never be true of every column of iTab
5434** is NULL. A false negative is merely a missed optimization opportunity.
5435**
5436** False positives are not allowed, however. A false positive may result
5437** in an incorrect answer.
5438**
drh25897872018-03-20 21:16:15 +00005439** Terms of p that are marked with EP_FromJoin (and hence that come from
5440** the ON or USING clauses of LEFT JOINS) are excluded from the analysis.
5441**
5442** This routine is used to check if a LEFT JOIN can be converted into
5443** an ordinary JOIN. The p argument is the WHERE clause. If the WHERE
5444** clause requires that some column of the right table of the LEFT JOIN
5445** be non-NULL, then the LEFT JOIN can be safely converted into an
5446** ordinary join.
5447*/
5448int sqlite3ExprImpliesNonNullRow(Expr *p, int iTab){
5449 Walker w;
drh0d950af2019-08-22 16:38:42 +00005450 p = sqlite3ExprSkipCollateAndLikely(p);
drh4a254f92019-10-11 16:01:21 +00005451 if( p==0 ) return 0;
5452 if( p->op==TK_NOTNULL ){
dan0287c952019-10-10 17:09:44 +00005453 p = p->pLeft;
drha1698992019-10-11 17:14:40 +00005454 }else{
5455 while( p->op==TK_AND ){
5456 if( sqlite3ExprImpliesNonNullRow(p->pLeft, iTab) ) return 1;
5457 p = p->pRight;
5458 }
drhd6db6592019-04-01 19:42:42 +00005459 }
drh25897872018-03-20 21:16:15 +00005460 w.xExprCallback = impliesNotNullRow;
5461 w.xSelectCallback = 0;
5462 w.xSelectCallback2 = 0;
5463 w.eCode = 0;
5464 w.u.iCur = iTab;
5465 sqlite3WalkExpr(&w, p);
5466 return w.eCode;
5467}
5468
5469/*
drh030796d2012-08-23 16:18:10 +00005470** An instance of the following structure is used by the tree walker
drh2409f8a2016-07-27 18:27:02 +00005471** to determine if an expression can be evaluated by reference to the
5472** index only, without having to do a search for the corresponding
5473** table entry. The IdxCover.pIdx field is the index. IdxCover.iCur
5474** is the cursor for the table.
5475*/
5476struct IdxCover {
5477 Index *pIdx; /* The index to be tested for coverage */
5478 int iCur; /* Cursor number for the table corresponding to the index */
5479};
5480
5481/*
5482** Check to see if there are references to columns in table
5483** pWalker->u.pIdxCover->iCur can be satisfied using the index
5484** pWalker->u.pIdxCover->pIdx.
5485*/
5486static int exprIdxCover(Walker *pWalker, Expr *pExpr){
5487 if( pExpr->op==TK_COLUMN
5488 && pExpr->iTable==pWalker->u.pIdxCover->iCur
drhb9bcf7c2019-10-19 13:29:10 +00005489 && sqlite3TableColumnToIndex(pWalker->u.pIdxCover->pIdx, pExpr->iColumn)<0
drh2409f8a2016-07-27 18:27:02 +00005490 ){
5491 pWalker->eCode = 1;
5492 return WRC_Abort;
5493 }
5494 return WRC_Continue;
5495}
5496
5497/*
drhe604ec02016-07-27 19:20:58 +00005498** Determine if an index pIdx on table with cursor iCur contains will
5499** the expression pExpr. Return true if the index does cover the
5500** expression and false if the pExpr expression references table columns
5501** that are not found in the index pIdx.
drh2409f8a2016-07-27 18:27:02 +00005502**
5503** An index covering an expression means that the expression can be
5504** evaluated using only the index and without having to lookup the
5505** corresponding table entry.
5506*/
5507int sqlite3ExprCoveredByIndex(
5508 Expr *pExpr, /* The index to be tested */
5509 int iCur, /* The cursor number for the corresponding table */
5510 Index *pIdx /* The index that might be used for coverage */
5511){
5512 Walker w;
5513 struct IdxCover xcov;
5514 memset(&w, 0, sizeof(w));
5515 xcov.iCur = iCur;
5516 xcov.pIdx = pIdx;
5517 w.xExprCallback = exprIdxCover;
5518 w.u.pIdxCover = &xcov;
5519 sqlite3WalkExpr(&w, pExpr);
5520 return !w.eCode;
5521}
5522
5523
5524/*
5525** An instance of the following structure is used by the tree walker
drh030796d2012-08-23 16:18:10 +00005526** to count references to table columns in the arguments of an
drhed551b92012-08-23 19:46:11 +00005527** aggregate function, in order to implement the
5528** sqlite3FunctionThisSrc() routine.
drh374fdce2012-04-17 16:38:53 +00005529*/
drh030796d2012-08-23 16:18:10 +00005530struct SrcCount {
5531 SrcList *pSrc; /* One particular FROM clause in a nested query */
5532 int nThis; /* Number of references to columns in pSrcList */
5533 int nOther; /* Number of references to columns in other FROM clauses */
5534};
5535
5536/*
5537** Count the number of references to columns.
5538*/
5539static int exprSrcCount(Walker *pWalker, Expr *pExpr){
danb4b36302019-12-27 20:06:32 +00005540 /* There was once a NEVER() on the second term on the grounds that
5541 ** sqlite3FunctionUsesThisSrc() was always called before
5542 ** sqlite3ExprAnalyzeAggregates() and so the TK_COLUMNs have not yet
5543 ** been converted into TK_AGG_COLUMN. But this is no longer true due
5544 ** to window functions - sqlite3WindowRewrite() may now indirectly call
5545 ** FunctionUsesThisSrc() when creating a new sub-select. */
5546 if( pExpr->op==TK_COLUMN || pExpr->op==TK_AGG_COLUMN ){
drh374fdce2012-04-17 16:38:53 +00005547 int i;
drh030796d2012-08-23 16:18:10 +00005548 struct SrcCount *p = pWalker->u.pSrcCount;
5549 SrcList *pSrc = p->pSrc;
drh655814d2015-01-09 01:27:29 +00005550 int nSrc = pSrc ? pSrc->nSrc : 0;
5551 for(i=0; i<nSrc; i++){
drh030796d2012-08-23 16:18:10 +00005552 if( pExpr->iTable==pSrc->a[i].iCursor ) break;
drh374fdce2012-04-17 16:38:53 +00005553 }
drh655814d2015-01-09 01:27:29 +00005554 if( i<nSrc ){
drh030796d2012-08-23 16:18:10 +00005555 p->nThis++;
drh80f6bfc2019-08-31 20:13:30 +00005556 }else if( nSrc==0 || pExpr->iTable<pSrc->a[0].iCursor ){
5557 /* In a well-formed parse tree (no name resolution errors),
drh35a38e02019-08-31 20:29:28 +00005558 ** TK_COLUMN nodes with smaller Expr.iTable values are in an
drh80f6bfc2019-08-31 20:13:30 +00005559 ** outer context. Those are the only ones to count as "other" */
drh030796d2012-08-23 16:18:10 +00005560 p->nOther++;
5561 }
drh374fdce2012-04-17 16:38:53 +00005562 }
drh030796d2012-08-23 16:18:10 +00005563 return WRC_Continue;
drh374fdce2012-04-17 16:38:53 +00005564}
5565
5566/*
drh030796d2012-08-23 16:18:10 +00005567** Determine if any of the arguments to the pExpr Function reference
5568** pSrcList. Return true if they do. Also return true if the function
5569** has no arguments or has only constant arguments. Return false if pExpr
5570** references columns but not columns of tables found in pSrcList.
drh374fdce2012-04-17 16:38:53 +00005571*/
drh030796d2012-08-23 16:18:10 +00005572int sqlite3FunctionUsesThisSrc(Expr *pExpr, SrcList *pSrcList){
drh374fdce2012-04-17 16:38:53 +00005573 Walker w;
drh030796d2012-08-23 16:18:10 +00005574 struct SrcCount cnt;
drh374fdce2012-04-17 16:38:53 +00005575 assert( pExpr->op==TK_AGG_FUNCTION );
drh80f6bfc2019-08-31 20:13:30 +00005576 memset(&w, 0, sizeof(w));
drh030796d2012-08-23 16:18:10 +00005577 w.xExprCallback = exprSrcCount;
drh80f6bfc2019-08-31 20:13:30 +00005578 w.xSelectCallback = sqlite3SelectWalkNoop;
drh030796d2012-08-23 16:18:10 +00005579 w.u.pSrcCount = &cnt;
5580 cnt.pSrc = pSrcList;
5581 cnt.nThis = 0;
5582 cnt.nOther = 0;
5583 sqlite3WalkExprList(&w, pExpr->x.pList);
dan5e484cb2019-12-27 08:57:08 +00005584#ifndef SQLITE_OMIT_WINDOWFUNC
5585 if( ExprHasProperty(pExpr, EP_WinFunc) ){
5586 sqlite3WalkExpr(&w, pExpr->y.pWin->pFilter);
5587 }
5588#endif
drh030796d2012-08-23 16:18:10 +00005589 return cnt.nThis>0 || cnt.nOther==0;
drh374fdce2012-04-17 16:38:53 +00005590}
5591
5592/*
drh13449892005-09-07 21:22:45 +00005593** Add a new element to the pAggInfo->aCol[] array. Return the index of
5594** the new element. Return a negative number if malloc fails.
drh22827922000-06-06 17:27:05 +00005595*/
drh17435752007-08-16 04:30:38 +00005596static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){
drh13449892005-09-07 21:22:45 +00005597 int i;
drhcf643722007-03-27 13:36:37 +00005598 pInfo->aCol = sqlite3ArrayAllocate(
drh17435752007-08-16 04:30:38 +00005599 db,
drhcf643722007-03-27 13:36:37 +00005600 pInfo->aCol,
5601 sizeof(pInfo->aCol[0]),
drhcf643722007-03-27 13:36:37 +00005602 &pInfo->nColumn,
drhcf643722007-03-27 13:36:37 +00005603 &i
5604 );
drh13449892005-09-07 21:22:45 +00005605 return i;
5606}
5607
5608/*
5609** Add a new element to the pAggInfo->aFunc[] array. Return the index of
5610** the new element. Return a negative number if malloc fails.
5611*/
drh17435752007-08-16 04:30:38 +00005612static int addAggInfoFunc(sqlite3 *db, AggInfo *pInfo){
drh13449892005-09-07 21:22:45 +00005613 int i;
drhcf643722007-03-27 13:36:37 +00005614 pInfo->aFunc = sqlite3ArrayAllocate(
drh17435752007-08-16 04:30:38 +00005615 db,
drhcf643722007-03-27 13:36:37 +00005616 pInfo->aFunc,
5617 sizeof(pInfo->aFunc[0]),
drhcf643722007-03-27 13:36:37 +00005618 &pInfo->nFunc,
drhcf643722007-03-27 13:36:37 +00005619 &i
5620 );
drh13449892005-09-07 21:22:45 +00005621 return i;
5622}
drh22827922000-06-06 17:27:05 +00005623
5624/*
drh7d10d5a2008-08-20 16:35:10 +00005625** This is the xExprCallback for a tree walker. It is used to
5626** implement sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates
drh626a8792005-01-17 22:08:19 +00005627** for additional information.
drh22827922000-06-06 17:27:05 +00005628*/
drh7d10d5a2008-08-20 16:35:10 +00005629static int analyzeAggregate(Walker *pWalker, Expr *pExpr){
drh22827922000-06-06 17:27:05 +00005630 int i;
drh7d10d5a2008-08-20 16:35:10 +00005631 NameContext *pNC = pWalker->u.pNC;
danielk1977a58fdfb2005-02-08 07:50:40 +00005632 Parse *pParse = pNC->pParse;
5633 SrcList *pSrcList = pNC->pSrcList;
drh25c3b8c2018-04-16 10:34:13 +00005634 AggInfo *pAggInfo = pNC->uNC.pAggInfo;
drh22827922000-06-06 17:27:05 +00005635
drh25c3b8c2018-04-16 10:34:13 +00005636 assert( pNC->ncFlags & NC_UAggInfo );
drh22827922000-06-06 17:27:05 +00005637 switch( pExpr->op ){
drh89c69d02007-01-04 01:20:28 +00005638 case TK_AGG_COLUMN:
drh967e8b72000-06-21 13:59:10 +00005639 case TK_COLUMN: {
drh8b213892008-08-29 02:14:02 +00005640 testcase( pExpr->op==TK_AGG_COLUMN );
5641 testcase( pExpr->op==TK_COLUMN );
drh13449892005-09-07 21:22:45 +00005642 /* Check to see if the column is in one of the tables in the FROM
5643 ** clause of the aggregate query */
drh20bc3932009-05-30 23:35:43 +00005644 if( ALWAYS(pSrcList!=0) ){
drh13449892005-09-07 21:22:45 +00005645 struct SrcList_item *pItem = pSrcList->a;
5646 for(i=0; i<pSrcList->nSrc; i++, pItem++){
5647 struct AggInfo_col *pCol;
drhc5cd1242013-09-12 16:50:49 +00005648 assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) );
drh13449892005-09-07 21:22:45 +00005649 if( pExpr->iTable==pItem->iCursor ){
5650 /* If we reach this point, it means that pExpr refers to a table
5651 ** that is in the FROM clause of the aggregate query.
5652 **
5653 ** Make an entry for the column in pAggInfo->aCol[] if there
5654 ** is not an entry there already.
5655 */
drh7f906d62007-03-12 23:48:52 +00005656 int k;
drh13449892005-09-07 21:22:45 +00005657 pCol = pAggInfo->aCol;
drh7f906d62007-03-12 23:48:52 +00005658 for(k=0; k<pAggInfo->nColumn; k++, pCol++){
drh13449892005-09-07 21:22:45 +00005659 if( pCol->iTable==pExpr->iTable &&
5660 pCol->iColumn==pExpr->iColumn ){
5661 break;
5662 }
danielk1977a58fdfb2005-02-08 07:50:40 +00005663 }
danielk19771e536952007-08-16 10:09:01 +00005664 if( (k>=pAggInfo->nColumn)
5665 && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0
5666 ){
drh7f906d62007-03-12 23:48:52 +00005667 pCol = &pAggInfo->aCol[k];
drheda079c2018-09-20 19:02:15 +00005668 pCol->pTab = pExpr->y.pTab;
drh13449892005-09-07 21:22:45 +00005669 pCol->iTable = pExpr->iTable;
5670 pCol->iColumn = pExpr->iColumn;
drh0a07c102008-01-03 18:03:08 +00005671 pCol->iMem = ++pParse->nMem;
drh13449892005-09-07 21:22:45 +00005672 pCol->iSorterColumn = -1;
drh5774b802005-09-07 22:48:16 +00005673 pCol->pExpr = pExpr;
drh13449892005-09-07 21:22:45 +00005674 if( pAggInfo->pGroupBy ){
5675 int j, n;
5676 ExprList *pGB = pAggInfo->pGroupBy;
5677 struct ExprList_item *pTerm = pGB->a;
5678 n = pGB->nExpr;
5679 for(j=0; j<n; j++, pTerm++){
5680 Expr *pE = pTerm->pExpr;
5681 if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable &&
5682 pE->iColumn==pExpr->iColumn ){
5683 pCol->iSorterColumn = j;
5684 break;
5685 }
5686 }
5687 }
5688 if( pCol->iSorterColumn<0 ){
5689 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
5690 }
5691 }
5692 /* There is now an entry for pExpr in pAggInfo->aCol[] (either
5693 ** because it was there before or because we just created it).
5694 ** Convert the pExpr to be a TK_AGG_COLUMN referring to that
5695 ** pAggInfo->aCol[] entry.
5696 */
drhebb6a652013-09-12 23:42:22 +00005697 ExprSetVVAProperty(pExpr, EP_NoReduce);
drh13449892005-09-07 21:22:45 +00005698 pExpr->pAggInfo = pAggInfo;
5699 pExpr->op = TK_AGG_COLUMN;
shanecf697392009-06-01 16:53:09 +00005700 pExpr->iAgg = (i16)k;
drh13449892005-09-07 21:22:45 +00005701 break;
5702 } /* endif pExpr->iTable==pItem->iCursor */
5703 } /* end loop over pSrcList */
drh22827922000-06-06 17:27:05 +00005704 }
drh7d10d5a2008-08-20 16:35:10 +00005705 return WRC_Prune;
drh22827922000-06-06 17:27:05 +00005706 }
5707 case TK_AGG_FUNCTION: {
drh3a8c4be2012-05-21 20:13:39 +00005708 if( (pNC->ncFlags & NC_InAggFunc)==0
drhed551b92012-08-23 19:46:11 +00005709 && pWalker->walkerDepth==pExpr->op2
drh3a8c4be2012-05-21 20:13:39 +00005710 ){
drh13449892005-09-07 21:22:45 +00005711 /* Check to see if pExpr is a duplicate of another aggregate
5712 ** function that is already in the pAggInfo structure
5713 */
5714 struct AggInfo_func *pItem = pAggInfo->aFunc;
5715 for(i=0; i<pAggInfo->nFunc; i++, pItem++){
dan5aa550c2017-06-24 18:10:29 +00005716 if( sqlite3ExprCompare(0, pItem->pExpr, pExpr, -1)==0 ){
danielk1977a58fdfb2005-02-08 07:50:40 +00005717 break;
5718 }
drh22827922000-06-06 17:27:05 +00005719 }
drh13449892005-09-07 21:22:45 +00005720 if( i>=pAggInfo->nFunc ){
5721 /* pExpr is original. Make a new entry in pAggInfo->aFunc[]
5722 */
danielk197714db2662006-01-09 16:12:04 +00005723 u8 enc = ENC(pParse->db);
danielk19771e536952007-08-16 10:09:01 +00005724 i = addAggInfoFunc(pParse->db, pAggInfo);
drh13449892005-09-07 21:22:45 +00005725 if( i>=0 ){
danielk19776ab3a2e2009-02-19 14:39:25 +00005726 assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
drh13449892005-09-07 21:22:45 +00005727 pItem = &pAggInfo->aFunc[i];
5728 pItem->pExpr = pExpr;
drh0a07c102008-01-03 18:03:08 +00005729 pItem->iMem = ++pParse->nMem;
drh33e619f2009-05-28 01:00:55 +00005730 assert( !ExprHasProperty(pExpr, EP_IntValue) );
drh13449892005-09-07 21:22:45 +00005731 pItem->pFunc = sqlite3FindFunction(pParse->db,
drh80738d92016-02-15 00:34:16 +00005732 pExpr->u.zToken,
danielk19776ab3a2e2009-02-19 14:39:25 +00005733 pExpr->x.pList ? pExpr->x.pList->nExpr : 0, enc, 0);
drhfd357972005-09-09 01:33:19 +00005734 if( pExpr->flags & EP_Distinct ){
5735 pItem->iDistinct = pParse->nTab++;
5736 }else{
5737 pItem->iDistinct = -1;
5738 }
drh13449892005-09-07 21:22:45 +00005739 }
danielk1977a58fdfb2005-02-08 07:50:40 +00005740 }
drh13449892005-09-07 21:22:45 +00005741 /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
5742 */
drhc5cd1242013-09-12 16:50:49 +00005743 assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) );
drhebb6a652013-09-12 23:42:22 +00005744 ExprSetVVAProperty(pExpr, EP_NoReduce);
shanecf697392009-06-01 16:53:09 +00005745 pExpr->iAgg = (i16)i;
drh13449892005-09-07 21:22:45 +00005746 pExpr->pAggInfo = pAggInfo;
drh6e83a572012-11-02 18:48:49 +00005747 return WRC_Prune;
5748 }else{
5749 return WRC_Continue;
drh22827922000-06-06 17:27:05 +00005750 }
drh22827922000-06-06 17:27:05 +00005751 }
5752 }
drh7d10d5a2008-08-20 16:35:10 +00005753 return WRC_Continue;
5754}
5755static int analyzeAggregatesInSelect(Walker *pWalker, Select *pSelect){
drhd5a336e2012-04-19 15:49:19 +00005756 UNUSED_PARAMETER(pSelect);
drh979dd1b2017-05-29 14:26:07 +00005757 pWalker->walkerDepth++;
drh374fdce2012-04-17 16:38:53 +00005758 return WRC_Continue;
drh626a8792005-01-17 22:08:19 +00005759}
drh979dd1b2017-05-29 14:26:07 +00005760static void analyzeAggregatesInSelectEnd(Walker *pWalker, Select *pSelect){
5761 UNUSED_PARAMETER(pSelect);
5762 pWalker->walkerDepth--;
5763}
drh626a8792005-01-17 22:08:19 +00005764
5765/*
drhe8abb4c2012-11-02 18:24:57 +00005766** Analyze the pExpr expression looking for aggregate functions and
5767** for variables that need to be added to AggInfo object that pNC->pAggInfo
5768** points to. Additional entries are made on the AggInfo object as
5769** necessary.
drh626a8792005-01-17 22:08:19 +00005770**
5771** This routine should only be called after the expression has been
drh7d10d5a2008-08-20 16:35:10 +00005772** analyzed by sqlite3ResolveExprNames().
drh626a8792005-01-17 22:08:19 +00005773*/
drhd2b3e232008-01-23 14:51:49 +00005774void sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
drh7d10d5a2008-08-20 16:35:10 +00005775 Walker w;
5776 w.xExprCallback = analyzeAggregate;
5777 w.xSelectCallback = analyzeAggregatesInSelect;
drh979dd1b2017-05-29 14:26:07 +00005778 w.xSelectCallback2 = analyzeAggregatesInSelectEnd;
5779 w.walkerDepth = 0;
drh7d10d5a2008-08-20 16:35:10 +00005780 w.u.pNC = pNC;
dand9995032019-01-23 16:59:24 +00005781 w.pParse = 0;
drh20bc3932009-05-30 23:35:43 +00005782 assert( pNC->pSrcList!=0 );
drh7d10d5a2008-08-20 16:35:10 +00005783 sqlite3WalkExpr(&w, pExpr);
drh22827922000-06-06 17:27:05 +00005784}
drh5d9a4af2005-08-30 00:54:01 +00005785
5786/*
5787** Call sqlite3ExprAnalyzeAggregates() for every expression in an
5788** expression list. Return the number of errors.
5789**
5790** If an error is found, the analysis is cut short.
5791*/
drhd2b3e232008-01-23 14:51:49 +00005792void sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){
drh5d9a4af2005-08-30 00:54:01 +00005793 struct ExprList_item *pItem;
5794 int i;
drh5d9a4af2005-08-30 00:54:01 +00005795 if( pList ){
drhd2b3e232008-01-23 14:51:49 +00005796 for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
5797 sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
drh5d9a4af2005-08-30 00:54:01 +00005798 }
5799 }
drh5d9a4af2005-08-30 00:54:01 +00005800}
drh892d3172008-01-10 03:46:36 +00005801
5802/*
drhceea3322009-04-23 13:22:42 +00005803** Allocate a single new register for use to hold some intermediate result.
drh892d3172008-01-10 03:46:36 +00005804*/
5805int sqlite3GetTempReg(Parse *pParse){
drhe55cbd72008-03-31 23:48:03 +00005806 if( pParse->nTempReg==0 ){
drh892d3172008-01-10 03:46:36 +00005807 return ++pParse->nMem;
5808 }
danielk19772f425f62008-07-04 09:41:39 +00005809 return pParse->aTempReg[--pParse->nTempReg];
drh892d3172008-01-10 03:46:36 +00005810}
drhceea3322009-04-23 13:22:42 +00005811
5812/*
5813** Deallocate a register, making available for reuse for some other
5814** purpose.
drhceea3322009-04-23 13:22:42 +00005815*/
drh892d3172008-01-10 03:46:36 +00005816void sqlite3ReleaseTempReg(Parse *pParse, int iReg){
drh13d79502019-12-23 02:18:49 +00005817 if( iReg ){
drh3aef2fb2020-01-02 17:46:02 +00005818 sqlite3VdbeReleaseRegisters(pParse, iReg, 1, 0, 0);
drh13d79502019-12-23 02:18:49 +00005819 if( pParse->nTempReg<ArraySize(pParse->aTempReg) ){
5820 pParse->aTempReg[pParse->nTempReg++] = iReg;
5821 }
drh892d3172008-01-10 03:46:36 +00005822 }
5823}
5824
5825/*
drhed24da42016-09-06 14:37:05 +00005826** Allocate or deallocate a block of nReg consecutive registers.
drh892d3172008-01-10 03:46:36 +00005827*/
5828int sqlite3GetTempRange(Parse *pParse, int nReg){
drhe55cbd72008-03-31 23:48:03 +00005829 int i, n;
drhed24da42016-09-06 14:37:05 +00005830 if( nReg==1 ) return sqlite3GetTempReg(pParse);
drhe55cbd72008-03-31 23:48:03 +00005831 i = pParse->iRangeReg;
5832 n = pParse->nRangeReg;
drhf49f3522009-12-30 14:12:38 +00005833 if( nReg<=n ){
drh892d3172008-01-10 03:46:36 +00005834 pParse->iRangeReg += nReg;
5835 pParse->nRangeReg -= nReg;
5836 }else{
5837 i = pParse->nMem+1;
5838 pParse->nMem += nReg;
5839 }
5840 return i;
5841}
5842void sqlite3ReleaseTempRange(Parse *pParse, int iReg, int nReg){
drhed24da42016-09-06 14:37:05 +00005843 if( nReg==1 ){
5844 sqlite3ReleaseTempReg(pParse, iReg);
5845 return;
5846 }
drh3aef2fb2020-01-02 17:46:02 +00005847 sqlite3VdbeReleaseRegisters(pParse, iReg, nReg, 0, 0);
drh892d3172008-01-10 03:46:36 +00005848 if( nReg>pParse->nRangeReg ){
5849 pParse->nRangeReg = nReg;
5850 pParse->iRangeReg = iReg;
5851 }
5852}
drhcdc69552011-12-06 13:24:59 +00005853
5854/*
5855** Mark all temporary registers as being unavailable for reuse.
drh6d2566d2019-09-18 20:34:54 +00005856**
5857** Always invoke this procedure after coding a subroutine or co-routine
5858** that might be invoked from other parts of the code, to ensure that
5859** the sub/co-routine does not use registers in common with the code that
5860** invokes the sub/co-routine.
drhcdc69552011-12-06 13:24:59 +00005861*/
5862void sqlite3ClearTempRegCache(Parse *pParse){
5863 pParse->nTempReg = 0;
5864 pParse->nRangeReg = 0;
5865}
drhbb9b5f22016-03-19 00:35:02 +00005866
5867/*
5868** Validate that no temporary register falls within the range of
5869** iFirst..iLast, inclusive. This routine is only call from within assert()
5870** statements.
5871*/
5872#ifdef SQLITE_DEBUG
5873int sqlite3NoTempsInRange(Parse *pParse, int iFirst, int iLast){
5874 int i;
5875 if( pParse->nRangeReg>0
drh3963e582017-07-15 20:33:19 +00005876 && pParse->iRangeReg+pParse->nRangeReg > iFirst
5877 && pParse->iRangeReg <= iLast
drhbb9b5f22016-03-19 00:35:02 +00005878 ){
5879 return 0;
5880 }
5881 for(i=0; i<pParse->nTempReg; i++){
5882 if( pParse->aTempReg[i]>=iFirst && pParse->aTempReg[i]<=iLast ){
5883 return 0;
5884 }
5885 }
5886 return 1;
5887}
5888#endif /* SQLITE_DEBUG */