drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1 | /* |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2 | ** 2001 September 15 |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 3 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 6 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 7 | ** 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. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 10 | ** |
| 11 | ************************************************************************* |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 12 | ** This file contains routines used for analyzing expressions and |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 13 | ** for generating VDBE code that evaluates expressions in SQLite. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 14 | */ |
| 15 | #include "sqliteInt.h" |
drh | a2e0004 | 2002-01-22 03:13:42 +0000 | [diff] [blame] | 16 | |
drh | 12abf40 | 2016-08-22 14:30:05 +0000 | [diff] [blame] | 17 | /* Forward declarations */ |
| 18 | static void exprCodeBetween(Parse*,Expr*,int,void(*)(Parse*,Expr*,int,int),int); |
| 19 | static int exprCodeVector(Parse *pParse, Expr *p, int *piToFree); |
| 20 | |
drh | 0dfa4f6 | 2016-08-26 13:19:49 +0000 | [diff] [blame] | 21 | /* |
| 22 | ** Return the affinity character for a single column of a table. |
| 23 | */ |
| 24 | char sqlite3TableColumnAffinity(Table *pTab, int iCol){ |
| 25 | assert( iCol<pTab->nCol ); |
| 26 | return iCol>=0 ? pTab->aCol[iCol].affinity : SQLITE_AFF_INTEGER; |
| 27 | } |
drh | 12abf40 | 2016-08-22 14:30:05 +0000 | [diff] [blame] | 28 | |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 29 | /* |
| 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.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 37 | ** i.e. the WHERE clause expressions in the following statements all |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 38 | ** 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 | */ |
danielk1977 | bf3b721 | 2004-05-18 10:06:24 +0000 | [diff] [blame] | 45 | char sqlite3ExprAffinity(Expr *pExpr){ |
drh | 580c8c1 | 2012-12-08 03:34:04 +0000 | [diff] [blame] | 46 | int op; |
drh | a7d6db6 | 2019-06-11 21:02:15 +0000 | [diff] [blame] | 47 | while( ExprHasProperty(pExpr, EP_Skip) ){ |
| 48 | assert( pExpr->op==TK_COLLATE ); |
| 49 | pExpr = pExpr->pLeft; |
| 50 | assert( pExpr!=0 ); |
| 51 | } |
drh | 580c8c1 | 2012-12-08 03:34:04 +0000 | [diff] [blame] | 52 | op = pExpr->op; |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 53 | if( op==TK_SELECT ){ |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 54 | assert( pExpr->flags&EP_xIsSelect ); |
| 55 | return sqlite3ExprAffinity(pExpr->x.pSelect->pEList->a[0].pExpr); |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 56 | } |
drh | db45bd5 | 2016-08-22 00:48:58 +0000 | [diff] [blame] | 57 | if( op==TK_REGISTER ) op = pExpr->op2; |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 58 | #ifndef SQLITE_OMIT_CAST |
| 59 | if( op==TK_CAST ){ |
drh | 33e619f | 2009-05-28 01:00:55 +0000 | [diff] [blame] | 60 | assert( !ExprHasProperty(pExpr, EP_IntValue) ); |
drh | fdaac67 | 2013-10-04 15:30:21 +0000 | [diff] [blame] | 61 | return sqlite3AffinityType(pExpr->u.zToken, 0); |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 62 | } |
| 63 | #endif |
drh | eda079c | 2018-09-20 19:02:15 +0000 | [diff] [blame] | 64 | if( (op==TK_AGG_COLUMN || op==TK_COLUMN) && pExpr->y.pTab ){ |
| 65 | return sqlite3TableColumnAffinity(pExpr->y.pTab, pExpr->iColumn); |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 66 | } |
dan | 80aa545 | 2016-09-03 19:52:12 +0000 | [diff] [blame] | 67 | 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 | } |
drh | 1194904 | 2019-08-05 18:01:42 +0000 | [diff] [blame] | 73 | return pExpr->affExpr; |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 74 | } |
| 75 | |
drh | 53db145 | 2004-05-20 13:54:53 +0000 | [diff] [blame] | 76 | /* |
drh | 8b4c40d | 2007-02-01 23:02:45 +0000 | [diff] [blame] | 77 | ** Set the collating sequence for expression pExpr to be the collating |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 78 | ** sequence named by pToken. Return a pointer to a new Expr node that |
| 79 | ** implements the COLLATE operator. |
drh | 0a8a406 | 2012-12-07 18:38:16 +0000 | [diff] [blame] | 80 | ** |
| 81 | ** If a memory allocation error occurs, that fact is recorded in pParse->db |
| 82 | ** and the pExpr parameter is returned unchanged. |
drh | 8b4c40d | 2007-02-01 23:02:45 +0000 | [diff] [blame] | 83 | */ |
drh | 4ef7efa | 2014-03-20 15:14:08 +0000 | [diff] [blame] | 84 | Expr *sqlite3ExprAddCollateToken( |
| 85 | Parse *pParse, /* Parsing context */ |
| 86 | Expr *pExpr, /* Add the "COLLATE" clause to this expression */ |
dan | 80103fc | 2015-03-20 08:43:59 +0000 | [diff] [blame] | 87 | const Token *pCollName, /* Name of collating sequence */ |
| 88 | int dequote /* True to dequote pCollName */ |
drh | 4ef7efa | 2014-03-20 15:14:08 +0000 | [diff] [blame] | 89 | ){ |
drh | 0a8a406 | 2012-12-07 18:38:16 +0000 | [diff] [blame] | 90 | if( pCollName->n>0 ){ |
dan | 80103fc | 2015-03-20 08:43:59 +0000 | [diff] [blame] | 91 | Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLLATE, pCollName, dequote); |
drh | 0a8a406 | 2012-12-07 18:38:16 +0000 | [diff] [blame] | 92 | if( pNew ){ |
| 93 | pNew->pLeft = pExpr; |
drh | a4c3c87 | 2013-09-12 17:29:25 +0000 | [diff] [blame] | 94 | pNew->flags |= EP_Collate|EP_Skip; |
drh | 0a8a406 | 2012-12-07 18:38:16 +0000 | [diff] [blame] | 95 | pExpr = pNew; |
| 96 | } |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 97 | } |
drh | 0a8a406 | 2012-12-07 18:38:16 +0000 | [diff] [blame] | 98 | return pExpr; |
| 99 | } |
| 100 | Expr *sqlite3ExprAddCollateString(Parse *pParse, Expr *pExpr, const char *zC){ |
drh | 261d8a5 | 2012-12-08 21:36:26 +0000 | [diff] [blame] | 101 | Token s; |
| 102 | assert( zC!=0 ); |
drh | 40aced5 | 2016-01-22 17:48:09 +0000 | [diff] [blame] | 103 | sqlite3TokenInit(&s, (char*)zC); |
dan | 80103fc | 2015-03-20 08:43:59 +0000 | [diff] [blame] | 104 | return sqlite3ExprAddCollateToken(pParse, pExpr, &s, 0); |
drh | 0a8a406 | 2012-12-07 18:38:16 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /* |
drh | 0d950af | 2019-08-22 16:38:42 +0000 | [diff] [blame] | 108 | ** Skip over any TK_COLLATE operators. |
drh | 0a8a406 | 2012-12-07 18:38:16 +0000 | [diff] [blame] | 109 | */ |
| 110 | Expr *sqlite3ExprSkipCollate(Expr *pExpr){ |
drh | 0d950af | 2019-08-22 16:38:42 +0000 | [diff] [blame] | 111 | while( pExpr && ExprHasProperty(pExpr, EP_Skip) ){ |
| 112 | assert( pExpr->op==TK_COLLATE ); |
| 113 | pExpr = pExpr->pLeft; |
| 114 | } |
| 115 | return pExpr; |
| 116 | } |
| 117 | |
| 118 | /* |
| 119 | ** Skip over any TK_COLLATE operators and/or any unlikely() |
| 120 | ** or likelihood() or likely() functions at the root of an |
| 121 | ** expression. |
| 122 | */ |
| 123 | Expr *sqlite3ExprSkipCollateAndLikely(Expr *pExpr){ |
drh | a7d6db6 | 2019-06-11 21:02:15 +0000 | [diff] [blame] | 124 | while( pExpr && ExprHasProperty(pExpr, EP_Skip|EP_Unlikely) ){ |
drh | a4c3c87 | 2013-09-12 17:29:25 +0000 | [diff] [blame] | 125 | if( ExprHasProperty(pExpr, EP_Unlikely) ){ |
drh | cca9f3d | 2013-09-06 15:23:29 +0000 | [diff] [blame] | 126 | assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); |
| 127 | assert( pExpr->x.pList->nExpr>0 ); |
drh | a4c3c87 | 2013-09-12 17:29:25 +0000 | [diff] [blame] | 128 | assert( pExpr->op==TK_FUNCTION ); |
drh | cca9f3d | 2013-09-06 15:23:29 +0000 | [diff] [blame] | 129 | pExpr = pExpr->x.pList->a[0].pExpr; |
| 130 | }else{ |
drh | 0b8d255 | 2015-09-05 22:36:07 +0000 | [diff] [blame] | 131 | assert( pExpr->op==TK_COLLATE ); |
drh | a4c3c87 | 2013-09-12 17:29:25 +0000 | [diff] [blame] | 132 | pExpr = pExpr->pLeft; |
drh | cca9f3d | 2013-09-06 15:23:29 +0000 | [diff] [blame] | 133 | } |
drh | a4c3c87 | 2013-09-12 17:29:25 +0000 | [diff] [blame] | 134 | } |
drh | 0a8a406 | 2012-12-07 18:38:16 +0000 | [diff] [blame] | 135 | return pExpr; |
drh | 8b4c40d | 2007-02-01 23:02:45 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | /* |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 139 | ** Return the collation sequence for the expression pExpr. If |
| 140 | ** there is no defined collating sequence, return NULL. |
| 141 | ** |
drh | 70efa84 | 2017-09-28 01:58:23 +0000 | [diff] [blame] | 142 | ** See also: sqlite3ExprNNCollSeq() |
| 143 | ** |
| 144 | ** The sqlite3ExprNNCollSeq() works the same exact that it returns the |
| 145 | ** default collation if pExpr has no defined collation. |
| 146 | ** |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 147 | ** The collating sequence might be determined by a COLLATE operator |
| 148 | ** or by the presence of a column with a defined collating sequence. |
| 149 | ** COLLATE operators take first precedence. Left operands take |
| 150 | ** precedence over right operands. |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 151 | */ |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 152 | CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){ |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 153 | sqlite3 *db = pParse->db; |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 154 | CollSeq *pColl = 0; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 155 | Expr *p = pExpr; |
drh | 261d8a5 | 2012-12-08 21:36:26 +0000 | [diff] [blame] | 156 | while( p ){ |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 157 | int op = p->op; |
drh | cb0e04f | 2018-12-12 21:34:17 +0000 | [diff] [blame] | 158 | if( op==TK_REGISTER ) op = p->op2; |
| 159 | if( (op==TK_AGG_COLUMN || op==TK_COLUMN || op==TK_TRIGGER) |
drh | eda079c | 2018-09-20 19:02:15 +0000 | [diff] [blame] | 160 | && p->y.pTab!=0 |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 161 | ){ |
drh | eda079c | 2018-09-20 19:02:15 +0000 | [diff] [blame] | 162 | /* op==TK_REGISTER && p->y.pTab!=0 happens when pExpr was originally |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 163 | ** a TK_COLUMN but was previously evaluated and cached in a register */ |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 164 | int j = p->iColumn; |
| 165 | if( j>=0 ){ |
drh | eda079c | 2018-09-20 19:02:15 +0000 | [diff] [blame] | 166 | const char *zColl = p->y.pTab->aCol[j].zColl; |
drh | c4a64fa | 2009-05-11 20:53:28 +0000 | [diff] [blame] | 167 | pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0); |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 168 | } |
| 169 | break; |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 170 | } |
drh | e081d73 | 2018-07-27 18:19:12 +0000 | [diff] [blame] | 171 | if( op==TK_CAST || op==TK_UPLUS ){ |
| 172 | p = p->pLeft; |
| 173 | continue; |
| 174 | } |
drh | cb0e04f | 2018-12-12 21:34:17 +0000 | [diff] [blame] | 175 | if( op==TK_COLLATE ){ |
drh | e081d73 | 2018-07-27 18:19:12 +0000 | [diff] [blame] | 176 | pColl = sqlite3GetCollSeq(pParse, ENC(db), 0, p->u.zToken); |
| 177 | break; |
| 178 | } |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 179 | if( p->flags & EP_Collate ){ |
drh | 2308ed3 | 2015-02-09 16:09:34 +0000 | [diff] [blame] | 180 | if( p->pLeft && (p->pLeft->flags & EP_Collate)!=0 ){ |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 181 | p = p->pLeft; |
| 182 | }else{ |
drh | 2308ed3 | 2015-02-09 16:09:34 +0000 | [diff] [blame] | 183 | Expr *pNext = p->pRight; |
drh | 6728cd9 | 2015-02-09 18:28:03 +0000 | [diff] [blame] | 184 | /* The Expr.x union is never used at the same time as Expr.pRight */ |
| 185 | assert( p->x.pList==0 || p->pRight==0 ); |
| 186 | /* p->flags holds EP_Collate and p->pLeft->flags does not. And |
| 187 | ** p->x.pSelect cannot. So if p->x.pLeft exists, it must hold at |
| 188 | ** least one EP_Collate. Thus the following two ALWAYS. */ |
| 189 | if( p->x.pList!=0 && ALWAYS(!ExprHasProperty(p, EP_xIsSelect)) ){ |
drh | 2308ed3 | 2015-02-09 16:09:34 +0000 | [diff] [blame] | 190 | int i; |
drh | 6728cd9 | 2015-02-09 18:28:03 +0000 | [diff] [blame] | 191 | for(i=0; ALWAYS(i<p->x.pList->nExpr); i++){ |
drh | 2308ed3 | 2015-02-09 16:09:34 +0000 | [diff] [blame] | 192 | if( ExprHasProperty(p->x.pList->a[i].pExpr, EP_Collate) ){ |
| 193 | pNext = p->x.pList->a[i].pExpr; |
| 194 | break; |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | p = pNext; |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 199 | } |
| 200 | }else{ |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 201 | break; |
| 202 | } |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 203 | } |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 204 | if( sqlite3CheckCollSeq(pParse, pColl) ){ |
| 205 | pColl = 0; |
| 206 | } |
| 207 | return pColl; |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | /* |
drh | 70efa84 | 2017-09-28 01:58:23 +0000 | [diff] [blame] | 211 | ** Return the collation sequence for the expression pExpr. If |
| 212 | ** there is no defined collating sequence, return a pointer to the |
| 213 | ** defautl collation sequence. |
| 214 | ** |
| 215 | ** See also: sqlite3ExprCollSeq() |
| 216 | ** |
| 217 | ** The sqlite3ExprCollSeq() routine works the same except that it |
| 218 | ** returns NULL if there is no defined collation. |
| 219 | */ |
| 220 | CollSeq *sqlite3ExprNNCollSeq(Parse *pParse, Expr *pExpr){ |
| 221 | CollSeq *p = sqlite3ExprCollSeq(pParse, pExpr); |
| 222 | if( p==0 ) p = pParse->db->pDfltColl; |
| 223 | assert( p!=0 ); |
| 224 | return p; |
| 225 | } |
| 226 | |
| 227 | /* |
| 228 | ** Return TRUE if the two expressions have equivalent collating sequences. |
| 229 | */ |
| 230 | int sqlite3ExprCollSeqMatch(Parse *pParse, Expr *pE1, Expr *pE2){ |
| 231 | CollSeq *pColl1 = sqlite3ExprNNCollSeq(pParse, pE1); |
| 232 | CollSeq *pColl2 = sqlite3ExprNNCollSeq(pParse, pE2); |
| 233 | return sqlite3StrICmp(pColl1->zName, pColl2->zName)==0; |
| 234 | } |
| 235 | |
| 236 | /* |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 237 | ** pExpr is an operand of a comparison operator. aff2 is the |
| 238 | ** type affinity of the other operand. This routine returns the |
drh | 53db145 | 2004-05-20 13:54:53 +0000 | [diff] [blame] | 239 | ** type affinity that should be used for the comparison operator. |
| 240 | */ |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 241 | char sqlite3CompareAffinity(Expr *pExpr, char aff2){ |
danielk1977 | bf3b721 | 2004-05-18 10:06:24 +0000 | [diff] [blame] | 242 | char aff1 = sqlite3ExprAffinity(pExpr); |
drh | 96fb16e | 2019-08-06 14:37:24 +0000 | [diff] [blame] | 243 | if( aff1>SQLITE_AFF_NONE && aff2>SQLITE_AFF_NONE ){ |
drh | 8df447f | 2005-11-01 15:48:24 +0000 | [diff] [blame] | 244 | /* Both sides of the comparison are columns. If one has numeric |
| 245 | ** affinity, use that. Otherwise use no affinity. |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 246 | */ |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 247 | if( sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2) ){ |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 248 | return SQLITE_AFF_NUMERIC; |
| 249 | }else{ |
drh | 05883a3 | 2015-06-02 15:32:08 +0000 | [diff] [blame] | 250 | return SQLITE_AFF_BLOB; |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 251 | } |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 252 | }else{ |
| 253 | /* One side is a column, the other is not. Use the columns affinity. */ |
drh | 96fb16e | 2019-08-06 14:37:24 +0000 | [diff] [blame] | 254 | assert( aff1<=SQLITE_AFF_NONE || aff2<=SQLITE_AFF_NONE ); |
| 255 | return (aff1<=SQLITE_AFF_NONE ? aff2 : aff1) | SQLITE_AFF_NONE; |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 256 | } |
| 257 | } |
| 258 | |
drh | 53db145 | 2004-05-20 13:54:53 +0000 | [diff] [blame] | 259 | /* |
| 260 | ** pExpr is a comparison operator. Return the type affinity that should |
| 261 | ** be applied to both operands prior to doing the comparison. |
| 262 | */ |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 263 | static char comparisonAffinity(Expr *pExpr){ |
| 264 | char aff; |
| 265 | assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT || |
| 266 | pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE || |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 267 | pExpr->op==TK_NE || pExpr->op==TK_IS || pExpr->op==TK_ISNOT ); |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 268 | assert( pExpr->pLeft ); |
danielk1977 | bf3b721 | 2004-05-18 10:06:24 +0000 | [diff] [blame] | 269 | aff = sqlite3ExprAffinity(pExpr->pLeft); |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 270 | if( pExpr->pRight ){ |
| 271 | aff = sqlite3CompareAffinity(pExpr->pRight, aff); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 272 | }else if( ExprHasProperty(pExpr, EP_xIsSelect) ){ |
| 273 | aff = sqlite3CompareAffinity(pExpr->x.pSelect->pEList->a[0].pExpr, aff); |
drh | 13ac46e | 2017-02-11 13:51:23 +0000 | [diff] [blame] | 274 | }else if( aff==0 ){ |
drh | 05883a3 | 2015-06-02 15:32:08 +0000 | [diff] [blame] | 275 | aff = SQLITE_AFF_BLOB; |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 276 | } |
| 277 | return aff; |
| 278 | } |
| 279 | |
| 280 | /* |
| 281 | ** pExpr is a comparison expression, eg. '=', '<', IN(...) etc. |
| 282 | ** idx_affinity is the affinity of an indexed column. Return true |
| 283 | ** if the index with affinity idx_affinity may be used to implement |
| 284 | ** the comparison in pExpr. |
| 285 | */ |
| 286 | int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){ |
| 287 | char aff = comparisonAffinity(pExpr); |
drh | 915e434 | 2019-08-06 15:18:15 +0000 | [diff] [blame] | 288 | if( aff<SQLITE_AFF_TEXT ){ |
| 289 | return 1; |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 290 | } |
drh | 915e434 | 2019-08-06 15:18:15 +0000 | [diff] [blame] | 291 | if( aff==SQLITE_AFF_TEXT ){ |
| 292 | return idx_affinity==SQLITE_AFF_TEXT; |
| 293 | } |
| 294 | return sqlite3IsNumericAffinity(idx_affinity); |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 295 | } |
| 296 | |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 297 | /* |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 298 | ** Return the P5 value that should be used for a binary comparison |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 299 | ** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2. |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 300 | */ |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 301 | static u8 binaryCompareP5(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){ |
| 302 | u8 aff = (char)sqlite3ExprAffinity(pExpr2); |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 303 | aff = (u8)sqlite3CompareAffinity(pExpr1, aff) | (u8)jumpIfNull; |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 304 | return aff; |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 305 | } |
| 306 | |
drh | a2e0004 | 2002-01-22 03:13:42 +0000 | [diff] [blame] | 307 | /* |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 308 | ** Return a pointer to the collation sequence that should be used by |
| 309 | ** a binary comparison operator comparing pLeft and pRight. |
| 310 | ** |
| 311 | ** If the left hand expression has a collating sequence type, then it is |
| 312 | ** used. Otherwise the collation sequence for the right hand expression |
| 313 | ** is used, or the default (BINARY) if neither expression has a collating |
| 314 | ** type. |
danielk1977 | bcbb04e | 2007-05-29 12:11:29 +0000 | [diff] [blame] | 315 | ** |
| 316 | ** Argument pRight (but not pLeft) may be a null pointer. In this case, |
| 317 | ** it is not considered. |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 318 | */ |
drh | 0a0e131 | 2007-08-07 17:04:59 +0000 | [diff] [blame] | 319 | CollSeq *sqlite3BinaryCompareCollSeq( |
danielk1977 | bcbb04e | 2007-05-29 12:11:29 +0000 | [diff] [blame] | 320 | Parse *pParse, |
| 321 | Expr *pLeft, |
| 322 | Expr *pRight |
| 323 | ){ |
drh | ec41dda | 2007-02-07 13:09:45 +0000 | [diff] [blame] | 324 | CollSeq *pColl; |
| 325 | assert( pLeft ); |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 326 | if( pLeft->flags & EP_Collate ){ |
| 327 | pColl = sqlite3ExprCollSeq(pParse, pLeft); |
| 328 | }else if( pRight && (pRight->flags & EP_Collate)!=0 ){ |
| 329 | pColl = sqlite3ExprCollSeq(pParse, pRight); |
drh | ec41dda | 2007-02-07 13:09:45 +0000 | [diff] [blame] | 330 | }else{ |
| 331 | pColl = sqlite3ExprCollSeq(pParse, pLeft); |
| 332 | if( !pColl ){ |
| 333 | pColl = sqlite3ExprCollSeq(pParse, pRight); |
| 334 | } |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 335 | } |
| 336 | return pColl; |
| 337 | } |
| 338 | |
| 339 | /* |
drh | be5c89a | 2004-07-26 00:31:09 +0000 | [diff] [blame] | 340 | ** Generate code for a comparison operator. |
| 341 | */ |
| 342 | static int codeCompare( |
| 343 | Parse *pParse, /* The parsing (and code generating) context */ |
| 344 | Expr *pLeft, /* The left operand */ |
| 345 | Expr *pRight, /* The right operand */ |
| 346 | int opcode, /* The comparison opcode */ |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 347 | int in1, int in2, /* Register holding operands */ |
drh | be5c89a | 2004-07-26 00:31:09 +0000 | [diff] [blame] | 348 | int dest, /* Jump here if true. */ |
| 349 | int jumpIfNull /* If true, jump if either operand is NULL */ |
| 350 | ){ |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 351 | int p5; |
| 352 | int addr; |
| 353 | CollSeq *p4; |
| 354 | |
| 355 | p4 = sqlite3BinaryCompareCollSeq(pParse, pLeft, pRight); |
| 356 | p5 = binaryCompareP5(pLeft, pRight, jumpIfNull); |
| 357 | addr = sqlite3VdbeAddOp4(pParse->pVdbe, opcode, in2, dest, in1, |
| 358 | (void*)p4, P4_COLLSEQ); |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 359 | sqlite3VdbeChangeP5(pParse->pVdbe, (u8)p5); |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 360 | return addr; |
drh | be5c89a | 2004-07-26 00:31:09 +0000 | [diff] [blame] | 361 | } |
| 362 | |
dan | cfbb5e8 | 2016-07-13 19:48:13 +0000 | [diff] [blame] | 363 | /* |
dan | 870a070 | 2016-08-01 16:37:43 +0000 | [diff] [blame] | 364 | ** Return true if expression pExpr is a vector, or false otherwise. |
drh | d832da7 | 2016-08-20 21:11:25 +0000 | [diff] [blame] | 365 | ** |
| 366 | ** A vector is defined as any expression that results in two or more |
| 367 | ** columns of result. Every TK_VECTOR node is an vector because the |
| 368 | ** parser will not generate a TK_VECTOR with fewer than two entries. |
| 369 | ** But a TK_SELECT might be either a vector or a scalar. It is only |
| 370 | ** considered a vector if it has two or more result columns. |
dan | 870a070 | 2016-08-01 16:37:43 +0000 | [diff] [blame] | 371 | */ |
| 372 | int sqlite3ExprIsVector(Expr *pExpr){ |
drh | 76dbe7a | 2016-08-20 21:02:38 +0000 | [diff] [blame] | 373 | return sqlite3ExprVectorSize(pExpr)>1; |
dan | 870a070 | 2016-08-01 16:37:43 +0000 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | /* |
dan | cfbb5e8 | 2016-07-13 19:48:13 +0000 | [diff] [blame] | 377 | ** If the expression passed as the only argument is of type TK_VECTOR |
| 378 | ** return the number of expressions in the vector. Or, if the expression |
| 379 | ** is a sub-select, return the number of columns in the sub-select. For |
| 380 | ** any other type of expression, return 1. |
| 381 | */ |
dan | 71c57db | 2016-07-09 20:23:55 +0000 | [diff] [blame] | 382 | int sqlite3ExprVectorSize(Expr *pExpr){ |
drh | 12abf40 | 2016-08-22 14:30:05 +0000 | [diff] [blame] | 383 | u8 op = pExpr->op; |
| 384 | if( op==TK_REGISTER ) op = pExpr->op2; |
| 385 | if( op==TK_VECTOR ){ |
drh | 76dbe7a | 2016-08-20 21:02:38 +0000 | [diff] [blame] | 386 | return pExpr->x.pList->nExpr; |
drh | 12abf40 | 2016-08-22 14:30:05 +0000 | [diff] [blame] | 387 | }else if( op==TK_SELECT ){ |
dan | 71c57db | 2016-07-09 20:23:55 +0000 | [diff] [blame] | 388 | return pExpr->x.pSelect->pEList->nExpr; |
drh | 76dbe7a | 2016-08-20 21:02:38 +0000 | [diff] [blame] | 389 | }else{ |
| 390 | return 1; |
dan | 71c57db | 2016-07-09 20:23:55 +0000 | [diff] [blame] | 391 | } |
dan | 71c57db | 2016-07-09 20:23:55 +0000 | [diff] [blame] | 392 | } |
| 393 | |
dan | ba00e30 | 2016-07-23 20:24:06 +0000 | [diff] [blame] | 394 | /* |
drh | fc7f27b | 2016-08-20 00:07:01 +0000 | [diff] [blame] | 395 | ** Return a pointer to a subexpression of pVector that is the i-th |
| 396 | ** column of the vector (numbered starting with 0). The caller must |
| 397 | ** ensure that i is within range. |
| 398 | ** |
drh | 76dbe7a | 2016-08-20 21:02:38 +0000 | [diff] [blame] | 399 | ** If pVector is really a scalar (and "scalar" here includes subqueries |
| 400 | ** that return a single column!) then return pVector unmodified. |
| 401 | ** |
drh | fc7f27b | 2016-08-20 00:07:01 +0000 | [diff] [blame] | 402 | ** pVector retains ownership of the returned subexpression. |
| 403 | ** |
| 404 | ** If the vector is a (SELECT ...) then the expression returned is |
drh | 76dbe7a | 2016-08-20 21:02:38 +0000 | [diff] [blame] | 405 | ** just the expression for the i-th term of the result set, and may |
| 406 | ** not be ready for evaluation because the table cursor has not yet |
| 407 | ** been positioned. |
dan | ba00e30 | 2016-07-23 20:24:06 +0000 | [diff] [blame] | 408 | */ |
drh | fc7f27b | 2016-08-20 00:07:01 +0000 | [diff] [blame] | 409 | Expr *sqlite3VectorFieldSubexpr(Expr *pVector, int i){ |
dan | 870a070 | 2016-08-01 16:37:43 +0000 | [diff] [blame] | 410 | assert( i<sqlite3ExprVectorSize(pVector) ); |
| 411 | if( sqlite3ExprIsVector(pVector) ){ |
drh | 9f24b53 | 2016-09-05 22:50:48 +0000 | [diff] [blame] | 412 | assert( pVector->op2==0 || pVector->op==TK_REGISTER ); |
| 413 | if( pVector->op==TK_SELECT || pVector->op2==TK_SELECT ){ |
dan | 870a070 | 2016-08-01 16:37:43 +0000 | [diff] [blame] | 414 | return pVector->x.pSelect->pEList->a[i].pExpr; |
| 415 | }else{ |
| 416 | return pVector->x.pList->a[i].pExpr; |
| 417 | } |
dan | 71c57db | 2016-07-09 20:23:55 +0000 | [diff] [blame] | 418 | } |
dan | 870a070 | 2016-08-01 16:37:43 +0000 | [diff] [blame] | 419 | return pVector; |
dan | 71c57db | 2016-07-09 20:23:55 +0000 | [diff] [blame] | 420 | } |
drh | fc7f27b | 2016-08-20 00:07:01 +0000 | [diff] [blame] | 421 | |
drh | fc7f27b | 2016-08-20 00:07:01 +0000 | [diff] [blame] | 422 | /* |
| 423 | ** Compute and return a new Expr object which when passed to |
| 424 | ** sqlite3ExprCode() will generate all necessary code to compute |
| 425 | ** the iField-th column of the vector expression pVector. |
| 426 | ** |
drh | 8762ec1 | 2016-08-20 01:06:22 +0000 | [diff] [blame] | 427 | ** It is ok for pVector to be a scalar (as long as iField==0). |
| 428 | ** In that case, this routine works like sqlite3ExprDup(). |
| 429 | ** |
drh | fc7f27b | 2016-08-20 00:07:01 +0000 | [diff] [blame] | 430 | ** The caller owns the returned Expr object and is responsible for |
| 431 | ** ensuring that the returned value eventually gets freed. |
| 432 | ** |
drh | 8762ec1 | 2016-08-20 01:06:22 +0000 | [diff] [blame] | 433 | ** The caller retains ownership of pVector. If pVector is a TK_SELECT, |
dan | fad0e70 | 2016-09-06 12:04:50 +0000 | [diff] [blame] | 434 | ** then the returned object will reference pVector and so pVector must remain |
drh | 8762ec1 | 2016-08-20 01:06:22 +0000 | [diff] [blame] | 435 | ** valid for the life of the returned object. If pVector is a TK_VECTOR |
| 436 | ** or a scalar expression, then it can be deleted as soon as this routine |
drh | 76dbe7a | 2016-08-20 21:02:38 +0000 | [diff] [blame] | 437 | ** returns. |
drh | 8762ec1 | 2016-08-20 01:06:22 +0000 | [diff] [blame] | 438 | ** |
| 439 | ** A trick to cause a TK_SELECT pVector to be deleted together with |
| 440 | ** the returned Expr object is to attach the pVector to the pRight field |
| 441 | ** of the returned TK_SELECT_COLUMN Expr object. |
drh | fc7f27b | 2016-08-20 00:07:01 +0000 | [diff] [blame] | 442 | */ |
| 443 | Expr *sqlite3ExprForVectorField( |
| 444 | Parse *pParse, /* Parsing context */ |
| 445 | Expr *pVector, /* The vector. List of expressions or a sub-SELECT */ |
drh | a1251bc | 2016-08-20 00:51:37 +0000 | [diff] [blame] | 446 | int iField /* Which column of the vector to return */ |
drh | fc7f27b | 2016-08-20 00:07:01 +0000 | [diff] [blame] | 447 | ){ |
| 448 | Expr *pRet; |
drh | a1251bc | 2016-08-20 00:51:37 +0000 | [diff] [blame] | 449 | if( pVector->op==TK_SELECT ){ |
| 450 | assert( pVector->flags & EP_xIsSelect ); |
drh | fc7f27b | 2016-08-20 00:07:01 +0000 | [diff] [blame] | 451 | /* The TK_SELECT_COLUMN Expr node: |
| 452 | ** |
drh | 966e291 | 2017-01-03 02:58:01 +0000 | [diff] [blame] | 453 | ** pLeft: pVector containing TK_SELECT. Not deleted. |
drh | 8762ec1 | 2016-08-20 01:06:22 +0000 | [diff] [blame] | 454 | ** pRight: not used. But recursively deleted. |
drh | fc7f27b | 2016-08-20 00:07:01 +0000 | [diff] [blame] | 455 | ** iColumn: Index of a column in pVector |
drh | 966e291 | 2017-01-03 02:58:01 +0000 | [diff] [blame] | 456 | ** iTable: 0 or the number of columns on the LHS of an assignment |
drh | fc7f27b | 2016-08-20 00:07:01 +0000 | [diff] [blame] | 457 | ** pLeft->iTable: First in an array of register holding result, or 0 |
| 458 | ** if the result is not yet computed. |
| 459 | ** |
| 460 | ** sqlite3ExprDelete() specifically skips the recursive delete of |
| 461 | ** pLeft on TK_SELECT_COLUMN nodes. But pRight is followed, so pVector |
drh | 8762ec1 | 2016-08-20 01:06:22 +0000 | [diff] [blame] | 462 | ** can be attached to pRight to cause this node to take ownership of |
| 463 | ** pVector. Typically there will be multiple TK_SELECT_COLUMN nodes |
| 464 | ** with the same pLeft pointer to the pVector, but only one of them |
| 465 | ** will own the pVector. |
drh | fc7f27b | 2016-08-20 00:07:01 +0000 | [diff] [blame] | 466 | */ |
drh | abfd35e | 2016-12-06 22:47:23 +0000 | [diff] [blame] | 467 | pRet = sqlite3PExpr(pParse, TK_SELECT_COLUMN, 0, 0); |
drh | 8bd0d58 | 2016-08-20 18:06:14 +0000 | [diff] [blame] | 468 | if( pRet ){ |
| 469 | pRet->iColumn = iField; |
| 470 | pRet->pLeft = pVector; |
| 471 | } |
drh | fc7f27b | 2016-08-20 00:07:01 +0000 | [diff] [blame] | 472 | assert( pRet==0 || pRet->iTable==0 ); |
| 473 | }else{ |
drh | a1251bc | 2016-08-20 00:51:37 +0000 | [diff] [blame] | 474 | if( pVector->op==TK_VECTOR ) pVector = pVector->x.pList->a[iField].pExpr; |
| 475 | pRet = sqlite3ExprDup(pParse->db, pVector, 0); |
dan | dfb5c96 | 2019-01-15 20:51:35 +0000 | [diff] [blame] | 476 | sqlite3RenameTokenRemap(pParse, pRet, pVector); |
drh | fc7f27b | 2016-08-20 00:07:01 +0000 | [diff] [blame] | 477 | } |
| 478 | return pRet; |
| 479 | } |
dan | 71c57db | 2016-07-09 20:23:55 +0000 | [diff] [blame] | 480 | |
dan | 5c288b9 | 2016-07-30 21:02:33 +0000 | [diff] [blame] | 481 | /* |
| 482 | ** If expression pExpr is of type TK_SELECT, generate code to evaluate |
| 483 | ** it. Return the register in which the result is stored (or, if the |
| 484 | ** sub-select returns more than one column, the first in an array |
| 485 | ** of registers in which the result is stored). |
| 486 | ** |
| 487 | ** If pExpr is not a TK_SELECT expression, return 0. |
| 488 | */ |
| 489 | static int exprCodeSubselect(Parse *pParse, Expr *pExpr){ |
dan | 8da209b | 2016-07-26 18:06:08 +0000 | [diff] [blame] | 490 | int reg = 0; |
dan | f9b2e05 | 2016-08-02 17:45:00 +0000 | [diff] [blame] | 491 | #ifndef SQLITE_OMIT_SUBQUERY |
dan | 5c288b9 | 2016-07-30 21:02:33 +0000 | [diff] [blame] | 492 | if( pExpr->op==TK_SELECT ){ |
drh | 85bcdce | 2018-12-23 21:27:29 +0000 | [diff] [blame] | 493 | reg = sqlite3CodeSubselect(pParse, pExpr); |
dan | 8da209b | 2016-07-26 18:06:08 +0000 | [diff] [blame] | 494 | } |
dan | f9b2e05 | 2016-08-02 17:45:00 +0000 | [diff] [blame] | 495 | #endif |
dan | 8da209b | 2016-07-26 18:06:08 +0000 | [diff] [blame] | 496 | return reg; |
| 497 | } |
| 498 | |
dan | 5c288b9 | 2016-07-30 21:02:33 +0000 | [diff] [blame] | 499 | /* |
| 500 | ** Argument pVector points to a vector expression - either a TK_VECTOR |
dan | 870a070 | 2016-08-01 16:37:43 +0000 | [diff] [blame] | 501 | ** or TK_SELECT that returns more than one column. This function returns |
| 502 | ** the register number of a register that contains the value of |
| 503 | ** element iField of the vector. |
| 504 | ** |
| 505 | ** If pVector is a TK_SELECT expression, then code for it must have |
| 506 | ** already been generated using the exprCodeSubselect() routine. In this |
| 507 | ** case parameter regSelect should be the first in an array of registers |
| 508 | ** containing the results of the sub-select. |
| 509 | ** |
| 510 | ** If pVector is of type TK_VECTOR, then code for the requested field |
| 511 | ** is generated. In this case (*pRegFree) may be set to the number of |
| 512 | ** a temporary register to be freed by the caller before returning. |
dan | 5c288b9 | 2016-07-30 21:02:33 +0000 | [diff] [blame] | 513 | ** |
| 514 | ** Before returning, output parameter (*ppExpr) is set to point to the |
| 515 | ** Expr object corresponding to element iElem of the vector. |
dan | 5c288b9 | 2016-07-30 21:02:33 +0000 | [diff] [blame] | 516 | */ |
| 517 | static int exprVectorRegister( |
| 518 | Parse *pParse, /* Parse context */ |
| 519 | Expr *pVector, /* Vector to extract element from */ |
dan | 870a070 | 2016-08-01 16:37:43 +0000 | [diff] [blame] | 520 | int iField, /* Field to extract from pVector */ |
dan | 5c288b9 | 2016-07-30 21:02:33 +0000 | [diff] [blame] | 521 | int regSelect, /* First in array of registers */ |
| 522 | Expr **ppExpr, /* OUT: Expression element */ |
| 523 | int *pRegFree /* OUT: Temp register to free */ |
| 524 | ){ |
drh | 12abf40 | 2016-08-22 14:30:05 +0000 | [diff] [blame] | 525 | u8 op = pVector->op; |
drh | c1bcd9c | 2016-09-05 19:57:46 +0000 | [diff] [blame] | 526 | assert( op==TK_VECTOR || op==TK_REGISTER || op==TK_SELECT ); |
drh | 12abf40 | 2016-08-22 14:30:05 +0000 | [diff] [blame] | 527 | if( op==TK_REGISTER ){ |
| 528 | *ppExpr = sqlite3VectorFieldSubexpr(pVector, iField); |
| 529 | return pVector->iTable+iField; |
| 530 | } |
| 531 | if( op==TK_SELECT ){ |
dan | 870a070 | 2016-08-01 16:37:43 +0000 | [diff] [blame] | 532 | *ppExpr = pVector->x.pSelect->pEList->a[iField].pExpr; |
| 533 | return regSelect+iField; |
dan | 5c288b9 | 2016-07-30 21:02:33 +0000 | [diff] [blame] | 534 | } |
dan | 870a070 | 2016-08-01 16:37:43 +0000 | [diff] [blame] | 535 | *ppExpr = pVector->x.pList->a[iField].pExpr; |
dan | 5c288b9 | 2016-07-30 21:02:33 +0000 | [diff] [blame] | 536 | return sqlite3ExprCodeTemp(pParse, *ppExpr, pRegFree); |
| 537 | } |
| 538 | |
| 539 | /* |
| 540 | ** Expression pExpr is a comparison between two vector values. Compute |
drh | 79752b6 | 2016-08-13 10:02:17 +0000 | [diff] [blame] | 541 | ** the result of the comparison (1, 0, or NULL) and write that |
| 542 | ** result into register dest. |
| 543 | ** |
| 544 | ** The caller must satisfy the following preconditions: |
| 545 | ** |
| 546 | ** if pExpr->op==TK_IS: op==TK_EQ and p5==SQLITE_NULLEQ |
| 547 | ** if pExpr->op==TK_ISNOT: op==TK_NE and p5==SQLITE_NULLEQ |
| 548 | ** otherwise: op==pExpr->op and p5==0 |
dan | 5c288b9 | 2016-07-30 21:02:33 +0000 | [diff] [blame] | 549 | */ |
drh | 79752b6 | 2016-08-13 10:02:17 +0000 | [diff] [blame] | 550 | static void codeVectorCompare( |
| 551 | Parse *pParse, /* Code generator context */ |
| 552 | Expr *pExpr, /* The comparison operation */ |
| 553 | int dest, /* Write results into this register */ |
| 554 | u8 op, /* Comparison operator */ |
| 555 | u8 p5 /* SQLITE_NULLEQ or zero */ |
| 556 | ){ |
dan | 71c57db | 2016-07-09 20:23:55 +0000 | [diff] [blame] | 557 | Vdbe *v = pParse->pVdbe; |
| 558 | Expr *pLeft = pExpr->pLeft; |
| 559 | Expr *pRight = pExpr->pRight; |
| 560 | int nLeft = sqlite3ExprVectorSize(pLeft); |
drh | b29e60c | 2016-09-05 12:02:34 +0000 | [diff] [blame] | 561 | int i; |
| 562 | int regLeft = 0; |
| 563 | int regRight = 0; |
| 564 | u8 opx = op; |
drh | ec4ccdb | 2018-12-29 02:26:59 +0000 | [diff] [blame] | 565 | int addrDone = sqlite3VdbeMakeLabel(pParse); |
dan | 71c57db | 2016-07-09 20:23:55 +0000 | [diff] [blame] | 566 | |
drh | 245ce62 | 2017-01-01 12:44:07 +0000 | [diff] [blame] | 567 | if( nLeft!=sqlite3ExprVectorSize(pRight) ){ |
| 568 | sqlite3ErrorMsg(pParse, "row value misused"); |
| 569 | return; |
| 570 | } |
drh | b29e60c | 2016-09-05 12:02:34 +0000 | [diff] [blame] | 571 | assert( pExpr->op==TK_EQ || pExpr->op==TK_NE |
| 572 | || pExpr->op==TK_IS || pExpr->op==TK_ISNOT |
| 573 | || pExpr->op==TK_LT || pExpr->op==TK_GT |
| 574 | || pExpr->op==TK_LE || pExpr->op==TK_GE |
| 575 | ); |
| 576 | assert( pExpr->op==op || (pExpr->op==TK_IS && op==TK_EQ) |
| 577 | || (pExpr->op==TK_ISNOT && op==TK_NE) ); |
| 578 | assert( p5==0 || pExpr->op!=op ); |
| 579 | assert( p5==SQLITE_NULLEQ || pExpr->op==op ); |
dan | 71c57db | 2016-07-09 20:23:55 +0000 | [diff] [blame] | 580 | |
drh | b29e60c | 2016-09-05 12:02:34 +0000 | [diff] [blame] | 581 | p5 |= SQLITE_STOREP2; |
| 582 | if( opx==TK_LE ) opx = TK_LT; |
| 583 | if( opx==TK_GE ) opx = TK_GT; |
dan | 71c57db | 2016-07-09 20:23:55 +0000 | [diff] [blame] | 584 | |
drh | b29e60c | 2016-09-05 12:02:34 +0000 | [diff] [blame] | 585 | regLeft = exprCodeSubselect(pParse, pLeft); |
| 586 | regRight = exprCodeSubselect(pParse, pRight); |
dan | 71c57db | 2016-07-09 20:23:55 +0000 | [diff] [blame] | 587 | |
drh | b29e60c | 2016-09-05 12:02:34 +0000 | [diff] [blame] | 588 | for(i=0; 1 /*Loop exits by "break"*/; i++){ |
| 589 | int regFree1 = 0, regFree2 = 0; |
| 590 | Expr *pL, *pR; |
| 591 | int r1, r2; |
| 592 | assert( i>=0 && i<nLeft ); |
drh | b29e60c | 2016-09-05 12:02:34 +0000 | [diff] [blame] | 593 | r1 = exprVectorRegister(pParse, pLeft, i, regLeft, &pL, ®Free1); |
| 594 | r2 = exprVectorRegister(pParse, pRight, i, regRight, &pR, ®Free2); |
| 595 | codeCompare(pParse, pL, pR, opx, r1, r2, dest, p5); |
| 596 | testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt); |
| 597 | testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le); |
| 598 | testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt); |
| 599 | testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge); |
| 600 | testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq); |
| 601 | testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne); |
| 602 | sqlite3ReleaseTempReg(pParse, regFree1); |
| 603 | sqlite3ReleaseTempReg(pParse, regFree2); |
drh | b29e60c | 2016-09-05 12:02:34 +0000 | [diff] [blame] | 604 | if( i==nLeft-1 ){ |
| 605 | break; |
dan | 71c57db | 2016-07-09 20:23:55 +0000 | [diff] [blame] | 606 | } |
drh | b29e60c | 2016-09-05 12:02:34 +0000 | [diff] [blame] | 607 | if( opx==TK_EQ ){ |
| 608 | sqlite3VdbeAddOp2(v, OP_IfNot, dest, addrDone); VdbeCoverage(v); |
| 609 | p5 |= SQLITE_KEEPNULL; |
| 610 | }else if( opx==TK_NE ){ |
| 611 | sqlite3VdbeAddOp2(v, OP_If, dest, addrDone); VdbeCoverage(v); |
| 612 | p5 |= SQLITE_KEEPNULL; |
| 613 | }else{ |
| 614 | assert( op==TK_LT || op==TK_GT || op==TK_LE || op==TK_GE ); |
| 615 | sqlite3VdbeAddOp2(v, OP_ElseNotEq, 0, addrDone); |
| 616 | VdbeCoverageIf(v, op==TK_LT); |
| 617 | VdbeCoverageIf(v, op==TK_GT); |
| 618 | VdbeCoverageIf(v, op==TK_LE); |
| 619 | VdbeCoverageIf(v, op==TK_GE); |
| 620 | if( i==nLeft-2 ) opx = op; |
| 621 | } |
dan | 71c57db | 2016-07-09 20:23:55 +0000 | [diff] [blame] | 622 | } |
drh | b29e60c | 2016-09-05 12:02:34 +0000 | [diff] [blame] | 623 | sqlite3VdbeResolveLabel(v, addrDone); |
dan | 71c57db | 2016-07-09 20:23:55 +0000 | [diff] [blame] | 624 | } |
| 625 | |
danielk1977 | 4b5255a | 2008-06-05 16:47:39 +0000 | [diff] [blame] | 626 | #if SQLITE_MAX_EXPR_DEPTH>0 |
| 627 | /* |
| 628 | ** Check that argument nHeight is less than or equal to the maximum |
| 629 | ** expression depth allowed. If it is not, leave an error message in |
| 630 | ** pParse. |
| 631 | */ |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 632 | int sqlite3ExprCheckHeight(Parse *pParse, int nHeight){ |
danielk1977 | 4b5255a | 2008-06-05 16:47:39 +0000 | [diff] [blame] | 633 | int rc = SQLITE_OK; |
| 634 | int mxHeight = pParse->db->aLimit[SQLITE_LIMIT_EXPR_DEPTH]; |
| 635 | if( nHeight>mxHeight ){ |
| 636 | sqlite3ErrorMsg(pParse, |
| 637 | "Expression tree is too large (maximum depth %d)", mxHeight |
| 638 | ); |
| 639 | rc = SQLITE_ERROR; |
| 640 | } |
| 641 | return rc; |
| 642 | } |
| 643 | |
| 644 | /* The following three functions, heightOfExpr(), heightOfExprList() |
| 645 | ** and heightOfSelect(), are used to determine the maximum height |
| 646 | ** of any expression tree referenced by the structure passed as the |
| 647 | ** first argument. |
| 648 | ** |
| 649 | ** If this maximum height is greater than the current value pointed |
| 650 | ** to by pnHeight, the second parameter, then set *pnHeight to that |
| 651 | ** value. |
| 652 | */ |
| 653 | static void heightOfExpr(Expr *p, int *pnHeight){ |
| 654 | if( p ){ |
| 655 | if( p->nHeight>*pnHeight ){ |
| 656 | *pnHeight = p->nHeight; |
| 657 | } |
| 658 | } |
| 659 | } |
| 660 | static void heightOfExprList(ExprList *p, int *pnHeight){ |
| 661 | if( p ){ |
| 662 | int i; |
| 663 | for(i=0; i<p->nExpr; i++){ |
| 664 | heightOfExpr(p->a[i].pExpr, pnHeight); |
| 665 | } |
| 666 | } |
| 667 | } |
dan | 1a3a308 | 2018-01-18 19:00:54 +0000 | [diff] [blame] | 668 | static void heightOfSelect(Select *pSelect, int *pnHeight){ |
| 669 | Select *p; |
| 670 | for(p=pSelect; p; p=p->pPrior){ |
danielk1977 | 4b5255a | 2008-06-05 16:47:39 +0000 | [diff] [blame] | 671 | heightOfExpr(p->pWhere, pnHeight); |
| 672 | heightOfExpr(p->pHaving, pnHeight); |
| 673 | heightOfExpr(p->pLimit, pnHeight); |
danielk1977 | 4b5255a | 2008-06-05 16:47:39 +0000 | [diff] [blame] | 674 | heightOfExprList(p->pEList, pnHeight); |
| 675 | heightOfExprList(p->pGroupBy, pnHeight); |
| 676 | heightOfExprList(p->pOrderBy, pnHeight); |
danielk1977 | 4b5255a | 2008-06-05 16:47:39 +0000 | [diff] [blame] | 677 | } |
| 678 | } |
| 679 | |
| 680 | /* |
| 681 | ** Set the Expr.nHeight variable in the structure passed as an |
| 682 | ** argument. An expression with no children, Expr.pList or |
| 683 | ** Expr.pSelect member has a height of 1. Any other expression |
| 684 | ** has a height equal to the maximum height of any other |
| 685 | ** referenced Expr plus one. |
drh | 2308ed3 | 2015-02-09 16:09:34 +0000 | [diff] [blame] | 686 | ** |
| 687 | ** Also propagate EP_Propagate flags up from Expr.x.pList to Expr.flags, |
| 688 | ** if appropriate. |
danielk1977 | 4b5255a | 2008-06-05 16:47:39 +0000 | [diff] [blame] | 689 | */ |
| 690 | static void exprSetHeight(Expr *p){ |
| 691 | int nHeight = 0; |
| 692 | heightOfExpr(p->pLeft, &nHeight); |
| 693 | heightOfExpr(p->pRight, &nHeight); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 694 | if( ExprHasProperty(p, EP_xIsSelect) ){ |
| 695 | heightOfSelect(p->x.pSelect, &nHeight); |
drh | 2308ed3 | 2015-02-09 16:09:34 +0000 | [diff] [blame] | 696 | }else if( p->x.pList ){ |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 697 | heightOfExprList(p->x.pList, &nHeight); |
drh | 2308ed3 | 2015-02-09 16:09:34 +0000 | [diff] [blame] | 698 | p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 699 | } |
danielk1977 | 4b5255a | 2008-06-05 16:47:39 +0000 | [diff] [blame] | 700 | p->nHeight = nHeight + 1; |
| 701 | } |
| 702 | |
| 703 | /* |
| 704 | ** Set the Expr.nHeight variable using the exprSetHeight() function. If |
| 705 | ** the height is greater than the maximum allowed expression depth, |
| 706 | ** leave an error in pParse. |
drh | 2308ed3 | 2015-02-09 16:09:34 +0000 | [diff] [blame] | 707 | ** |
| 708 | ** Also propagate all EP_Propagate flags from the Expr.x.pList into |
| 709 | ** Expr.flags. |
danielk1977 | 4b5255a | 2008-06-05 16:47:39 +0000 | [diff] [blame] | 710 | */ |
drh | 2308ed3 | 2015-02-09 16:09:34 +0000 | [diff] [blame] | 711 | void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p){ |
drh | 74893a4 | 2015-03-22 10:23:17 +0000 | [diff] [blame] | 712 | if( pParse->nErr ) return; |
danielk1977 | 4b5255a | 2008-06-05 16:47:39 +0000 | [diff] [blame] | 713 | exprSetHeight(p); |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 714 | sqlite3ExprCheckHeight(pParse, p->nHeight); |
danielk1977 | 4b5255a | 2008-06-05 16:47:39 +0000 | [diff] [blame] | 715 | } |
| 716 | |
| 717 | /* |
| 718 | ** Return the maximum height of any expression tree referenced |
| 719 | ** by the select statement passed as an argument. |
| 720 | */ |
| 721 | int sqlite3SelectExprHeight(Select *p){ |
| 722 | int nHeight = 0; |
| 723 | heightOfSelect(p, &nHeight); |
| 724 | return nHeight; |
| 725 | } |
drh | 2308ed3 | 2015-02-09 16:09:34 +0000 | [diff] [blame] | 726 | #else /* ABOVE: Height enforcement enabled. BELOW: Height enforcement off */ |
| 727 | /* |
| 728 | ** Propagate all EP_Propagate flags from the Expr.x.pList into |
| 729 | ** Expr.flags. |
| 730 | */ |
| 731 | void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p){ |
| 732 | if( p && p->x.pList && !ExprHasProperty(p, EP_xIsSelect) ){ |
| 733 | p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList); |
| 734 | } |
| 735 | } |
| 736 | #define exprSetHeight(y) |
danielk1977 | 4b5255a | 2008-06-05 16:47:39 +0000 | [diff] [blame] | 737 | #endif /* SQLITE_MAX_EXPR_DEPTH>0 */ |
| 738 | |
drh | be5c89a | 2004-07-26 00:31:09 +0000 | [diff] [blame] | 739 | /* |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 740 | ** This routine is the core allocator for Expr nodes. |
| 741 | ** |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 742 | ** Construct a new expression node and return a pointer to it. Memory |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 743 | ** for this node and for the pToken argument is a single allocation |
| 744 | ** obtained from sqlite3DbMalloc(). The calling function |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 745 | ** is responsible for making sure the node eventually gets freed. |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 746 | ** |
| 747 | ** If dequote is true, then the token (if it exists) is dequoted. |
drh | e792b5b | 2015-08-23 20:48:29 +0000 | [diff] [blame] | 748 | ** If dequote is false, no dequoting is performed. The deQuote |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 749 | ** parameter is ignored if pToken is NULL or if the token does not |
| 750 | ** appear to be quoted. If the quotes were of the form "..." (double-quotes) |
| 751 | ** then the EP_DblQuoted flag is set on the expression node. |
drh | 33e619f | 2009-05-28 01:00:55 +0000 | [diff] [blame] | 752 | ** |
| 753 | ** Special case: If op==TK_INTEGER and pToken points to a string that |
| 754 | ** can be translated into a 32-bit integer, then the token is not |
| 755 | ** stored in u.zToken. Instead, the integer values is written |
| 756 | ** into u.iValue and the EP_IntValue flag is set. No extra storage |
| 757 | ** is allocated to hold the integer text and the dequote flag is ignored. |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 758 | */ |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 759 | Expr *sqlite3ExprAlloc( |
drh | cca8a4a | 2016-10-03 12:56:48 +0000 | [diff] [blame] | 760 | sqlite3 *db, /* Handle for sqlite3DbMallocRawNN() */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 761 | int op, /* Expression opcode */ |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 762 | const Token *pToken, /* Token argument. Might be NULL */ |
| 763 | int dequote /* True to dequote */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 764 | ){ |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 765 | Expr *pNew; |
drh | 33e619f | 2009-05-28 01:00:55 +0000 | [diff] [blame] | 766 | int nExtra = 0; |
shane | cf69739 | 2009-06-01 16:53:09 +0000 | [diff] [blame] | 767 | int iValue = 0; |
danielk1977 | fc97606 | 2007-05-10 10:46:56 +0000 | [diff] [blame] | 768 | |
drh | 575fad6 | 2016-02-05 13:38:36 +0000 | [diff] [blame] | 769 | assert( db!=0 ); |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 770 | if( pToken ){ |
drh | 33e619f | 2009-05-28 01:00:55 +0000 | [diff] [blame] | 771 | if( op!=TK_INTEGER || pToken->z==0 |
| 772 | || sqlite3GetInt32(pToken->z, &iValue)==0 ){ |
| 773 | nExtra = pToken->n+1; |
drh | d50ffc4 | 2011-03-08 02:38:28 +0000 | [diff] [blame] | 774 | assert( iValue>=0 ); |
drh | 33e619f | 2009-05-28 01:00:55 +0000 | [diff] [blame] | 775 | } |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 776 | } |
drh | 575fad6 | 2016-02-05 13:38:36 +0000 | [diff] [blame] | 777 | pNew = sqlite3DbMallocRawNN(db, sizeof(Expr)+nExtra); |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 778 | if( pNew ){ |
drh | ca3862d | 2016-01-08 12:46:39 +0000 | [diff] [blame] | 779 | memset(pNew, 0, sizeof(Expr)); |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 780 | pNew->op = (u8)op; |
| 781 | pNew->iAgg = -1; |
| 782 | if( pToken ){ |
drh | 33e619f | 2009-05-28 01:00:55 +0000 | [diff] [blame] | 783 | if( nExtra==0 ){ |
drh | ad31727 | 2019-04-19 16:21:51 +0000 | [diff] [blame] | 784 | pNew->flags |= EP_IntValue|EP_Leaf|(iValue?EP_IsTrue:EP_IsFalse); |
drh | 33e619f | 2009-05-28 01:00:55 +0000 | [diff] [blame] | 785 | pNew->u.iValue = iValue; |
| 786 | }else{ |
drh | 33e619f | 2009-05-28 01:00:55 +0000 | [diff] [blame] | 787 | pNew->u.zToken = (char*)&pNew[1]; |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 788 | assert( pToken->z!=0 || pToken->n==0 ); |
| 789 | if( pToken->n ) memcpy(pNew->u.zToken, pToken->z, pToken->n); |
drh | 33e619f | 2009-05-28 01:00:55 +0000 | [diff] [blame] | 790 | pNew->u.zToken[pToken->n] = 0; |
drh | 244b9d6 | 2016-04-11 19:01:08 +0000 | [diff] [blame] | 791 | if( dequote && sqlite3Isquote(pNew->u.zToken[0]) ){ |
drh | 51d35b0 | 2019-01-11 13:32:23 +0000 | [diff] [blame] | 792 | sqlite3DequoteExpr(pNew); |
drh | 33e619f | 2009-05-28 01:00:55 +0000 | [diff] [blame] | 793 | } |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 794 | } |
| 795 | } |
| 796 | #if SQLITE_MAX_EXPR_DEPTH>0 |
| 797 | pNew->nHeight = 1; |
| 798 | #endif |
| 799 | } |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 800 | return pNew; |
| 801 | } |
| 802 | |
| 803 | /* |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 804 | ** Allocate a new expression node from a zero-terminated token that has |
| 805 | ** already been dequoted. |
| 806 | */ |
| 807 | Expr *sqlite3Expr( |
| 808 | sqlite3 *db, /* Handle for sqlite3DbMallocZero() (may be null) */ |
| 809 | int op, /* Expression opcode */ |
| 810 | const char *zToken /* Token argument. Might be NULL */ |
| 811 | ){ |
| 812 | Token x; |
| 813 | x.z = zToken; |
drh | b40f06c | 2017-08-21 02:20:57 +0000 | [diff] [blame] | 814 | x.n = sqlite3Strlen30(zToken); |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 815 | return sqlite3ExprAlloc(db, op, &x, 0); |
| 816 | } |
| 817 | |
| 818 | /* |
| 819 | ** Attach subtrees pLeft and pRight to the Expr node pRoot. |
| 820 | ** |
| 821 | ** If pRoot==NULL that means that a memory allocation error has occurred. |
| 822 | ** In that case, delete the subtrees pLeft and pRight. |
| 823 | */ |
| 824 | void sqlite3ExprAttachSubtrees( |
| 825 | sqlite3 *db, |
| 826 | Expr *pRoot, |
| 827 | Expr *pLeft, |
| 828 | Expr *pRight |
| 829 | ){ |
| 830 | if( pRoot==0 ){ |
| 831 | assert( db->mallocFailed ); |
| 832 | sqlite3ExprDelete(db, pLeft); |
| 833 | sqlite3ExprDelete(db, pRight); |
| 834 | }else{ |
| 835 | if( pRight ){ |
| 836 | pRoot->pRight = pRight; |
drh | 885a5b0 | 2015-02-09 15:21:36 +0000 | [diff] [blame] | 837 | pRoot->flags |= EP_Propagate & pRight->flags; |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 838 | } |
| 839 | if( pLeft ){ |
| 840 | pRoot->pLeft = pLeft; |
drh | 885a5b0 | 2015-02-09 15:21:36 +0000 | [diff] [blame] | 841 | pRoot->flags |= EP_Propagate & pLeft->flags; |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 842 | } |
| 843 | exprSetHeight(pRoot); |
| 844 | } |
| 845 | } |
| 846 | |
| 847 | /* |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 848 | ** Allocate an Expr node which joins as many as two subtrees. |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 849 | ** |
drh | bf66446 | 2009-06-19 18:32:54 +0000 | [diff] [blame] | 850 | ** One or both of the subtrees can be NULL. Return a pointer to the new |
| 851 | ** Expr node. Or, if an OOM error occurs, set pParse->db->mallocFailed, |
| 852 | ** free the subtrees and return NULL. |
drh | 206f3d9 | 2006-07-11 13:15:08 +0000 | [diff] [blame] | 853 | */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 854 | Expr *sqlite3PExpr( |
| 855 | Parse *pParse, /* Parsing context */ |
| 856 | int op, /* Expression opcode */ |
| 857 | Expr *pLeft, /* Left operand */ |
drh | abfd35e | 2016-12-06 22:47:23 +0000 | [diff] [blame] | 858 | Expr *pRight /* Right operand */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 859 | ){ |
drh | 5fb52ca | 2012-03-31 02:34:35 +0000 | [diff] [blame] | 860 | Expr *p; |
drh | d5c851c | 2019-04-19 13:38:34 +0000 | [diff] [blame] | 861 | p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr)); |
| 862 | if( p ){ |
| 863 | memset(p, 0, sizeof(Expr)); |
| 864 | p->op = op & 0xff; |
| 865 | p->iAgg = -1; |
drh | 5fb52ca | 2012-03-31 02:34:35 +0000 | [diff] [blame] | 866 | sqlite3ExprAttachSubtrees(pParse->db, p, pLeft, pRight); |
dan | 2b359bd | 2010-10-28 11:31:23 +0000 | [diff] [blame] | 867 | sqlite3ExprCheckHeight(pParse, p->nHeight); |
drh | d5c851c | 2019-04-19 13:38:34 +0000 | [diff] [blame] | 868 | }else{ |
| 869 | sqlite3ExprDelete(pParse->db, pLeft); |
| 870 | sqlite3ExprDelete(pParse->db, pRight); |
dan | 2b359bd | 2010-10-28 11:31:23 +0000 | [diff] [blame] | 871 | } |
drh | 4e0cff6 | 2004-11-05 05:10:28 +0000 | [diff] [blame] | 872 | return p; |
| 873 | } |
| 874 | |
| 875 | /* |
drh | 08de4f7 | 2016-04-11 01:06:47 +0000 | [diff] [blame] | 876 | ** Add pSelect to the Expr.x.pSelect field. Or, if pExpr is NULL (due |
| 877 | ** do a memory allocation failure) then delete the pSelect object. |
| 878 | */ |
| 879 | void sqlite3PExprAddSelect(Parse *pParse, Expr *pExpr, Select *pSelect){ |
| 880 | if( pExpr ){ |
| 881 | pExpr->x.pSelect = pSelect; |
| 882 | ExprSetProperty(pExpr, EP_xIsSelect|EP_Subquery); |
| 883 | sqlite3ExprSetHeightAndFlags(pParse, pExpr); |
| 884 | }else{ |
| 885 | assert( pParse->db->mallocFailed ); |
| 886 | sqlite3SelectDelete(pParse->db, pSelect); |
| 887 | } |
| 888 | } |
| 889 | |
| 890 | |
| 891 | /* |
drh | 91bb0ee | 2004-09-01 03:06:34 +0000 | [diff] [blame] | 892 | ** Join two expressions using an AND operator. If either expression is |
| 893 | ** NULL, then just return the other expression. |
drh | 5fb52ca | 2012-03-31 02:34:35 +0000 | [diff] [blame] | 894 | ** |
| 895 | ** If one side or the other of the AND is known to be false, then instead |
| 896 | ** of returning an AND expression, just return a constant expression with |
| 897 | ** a value of false. |
drh | 91bb0ee | 2004-09-01 03:06:34 +0000 | [diff] [blame] | 898 | */ |
drh | d5c851c | 2019-04-19 13:38:34 +0000 | [diff] [blame] | 899 | Expr *sqlite3ExprAnd(Parse *pParse, Expr *pLeft, Expr *pRight){ |
| 900 | sqlite3 *db = pParse->db; |
| 901 | if( pLeft==0 ){ |
drh | 91bb0ee | 2004-09-01 03:06:34 +0000 | [diff] [blame] | 902 | return pRight; |
| 903 | }else if( pRight==0 ){ |
| 904 | return pLeft; |
drh | ad31727 | 2019-04-19 16:21:51 +0000 | [diff] [blame] | 905 | }else if( ExprAlwaysFalse(pLeft) || ExprAlwaysFalse(pRight) ){ |
drh | 8e34e40 | 2019-06-11 10:43:56 +0000 | [diff] [blame] | 906 | sqlite3ExprUnmapAndDelete(pParse, pLeft); |
| 907 | sqlite3ExprUnmapAndDelete(pParse, pRight); |
drh | 5fb52ca | 2012-03-31 02:34:35 +0000 | [diff] [blame] | 908 | return sqlite3ExprAlloc(db, TK_INTEGER, &sqlite3IntTokens[0], 0); |
drh | 91bb0ee | 2004-09-01 03:06:34 +0000 | [diff] [blame] | 909 | }else{ |
drh | d5c851c | 2019-04-19 13:38:34 +0000 | [diff] [blame] | 910 | return sqlite3PExpr(pParse, TK_AND, pLeft, pRight); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 911 | } |
| 912 | } |
| 913 | |
| 914 | /* |
| 915 | ** Construct a new expression node for a function with multiple |
| 916 | ** arguments. |
| 917 | */ |
drh | 954733b | 2018-07-27 23:33:16 +0000 | [diff] [blame] | 918 | Expr *sqlite3ExprFunction( |
| 919 | Parse *pParse, /* Parsing context */ |
| 920 | ExprList *pList, /* Argument list */ |
| 921 | Token *pToken, /* Name of the function */ |
| 922 | int eDistinct /* SF_Distinct or SF_ALL or 0 */ |
| 923 | ){ |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 924 | Expr *pNew; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 925 | sqlite3 *db = pParse->db; |
danielk1977 | 4b202ae | 2006-01-23 05:50:58 +0000 | [diff] [blame] | 926 | assert( pToken ); |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 927 | pNew = sqlite3ExprAlloc(db, TK_FUNCTION, pToken, 1); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 928 | if( pNew==0 ){ |
drh | d9da78a | 2009-03-24 15:08:09 +0000 | [diff] [blame] | 929 | sqlite3ExprListDelete(db, pList); /* Avoid memory leak when malloc fails */ |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 930 | return 0; |
| 931 | } |
drh | 954733b | 2018-07-27 23:33:16 +0000 | [diff] [blame] | 932 | if( pList && pList->nExpr > pParse->db->aLimit[SQLITE_LIMIT_FUNCTION_ARG] ){ |
| 933 | sqlite3ErrorMsg(pParse, "too many arguments on function %T", pToken); |
| 934 | } |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 935 | pNew->x.pList = pList; |
drh | fca2355 | 2017-10-28 20:51:54 +0000 | [diff] [blame] | 936 | ExprSetProperty(pNew, EP_HasFunc); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 937 | assert( !ExprHasProperty(pNew, EP_xIsSelect) ); |
drh | 2308ed3 | 2015-02-09 16:09:34 +0000 | [diff] [blame] | 938 | sqlite3ExprSetHeightAndFlags(pParse, pNew); |
drh | 954733b | 2018-07-27 23:33:16 +0000 | [diff] [blame] | 939 | if( eDistinct==SF_Distinct ) ExprSetProperty(pNew, EP_Distinct); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 940 | return pNew; |
| 941 | } |
| 942 | |
| 943 | /* |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 944 | ** Assign a variable number to an expression that encodes a wildcard |
| 945 | ** in the original SQL statement. |
| 946 | ** |
| 947 | ** Wildcards consisting of a single "?" are assigned the next sequential |
| 948 | ** variable number. |
| 949 | ** |
| 950 | ** Wildcards of the form "?nnn" are assigned the number "nnn". We make |
drh | 9bf755c | 2016-12-23 03:59:31 +0000 | [diff] [blame] | 951 | ** sure "nnn" is not too big to avoid a denial of service attack when |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 952 | ** the SQL statement comes from an external source. |
| 953 | ** |
drh | 51f49f1 | 2009-05-21 20:41:32 +0000 | [diff] [blame] | 954 | ** Wildcards of the form ":aaa", "@aaa", or "$aaa" are assigned the same number |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 955 | ** as the previous instance of the same wildcard. Or if this is the first |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 956 | ** instance of the wildcard, the next sequential variable number is |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 957 | ** assigned. |
| 958 | */ |
drh | de25a88 | 2016-10-03 15:28:24 +0000 | [diff] [blame] | 959 | void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr, u32 n){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 960 | sqlite3 *db = pParse->db; |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 961 | const char *z; |
drh | f326d66 | 2016-12-23 13:30:53 +0000 | [diff] [blame] | 962 | ynVar x; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 963 | |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 964 | if( pExpr==0 ) return; |
drh | c5cd124 | 2013-09-12 16:50:49 +0000 | [diff] [blame] | 965 | assert( !ExprHasProperty(pExpr, EP_IntValue|EP_Reduced|EP_TokenOnly) ); |
drh | 33e619f | 2009-05-28 01:00:55 +0000 | [diff] [blame] | 966 | z = pExpr->u.zToken; |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 967 | assert( z!=0 ); |
| 968 | assert( z[0]!=0 ); |
mistachkin | b1ed717 | 2017-04-14 14:50:34 +0000 | [diff] [blame] | 969 | assert( n==(u32)sqlite3Strlen30(z) ); |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 970 | if( z[1]==0 ){ |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 971 | /* Wildcard of the form "?". Assign the next variable number */ |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 972 | assert( z[0]=='?' ); |
drh | f326d66 | 2016-12-23 13:30:53 +0000 | [diff] [blame] | 973 | x = (ynVar)(++pParse->nVar); |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 974 | }else{ |
drh | f326d66 | 2016-12-23 13:30:53 +0000 | [diff] [blame] | 975 | int doAdd = 0; |
drh | 124c0b4 | 2011-06-01 18:15:55 +0000 | [diff] [blame] | 976 | if( z[0]=='?' ){ |
| 977 | /* Wildcard of the form "?nnn". Convert "nnn" to an integer and |
| 978 | ** use it as the variable number */ |
| 979 | i64 i; |
drh | 18814df | 2017-01-31 03:52:34 +0000 | [diff] [blame] | 980 | int bOk; |
| 981 | if( n==2 ){ /*OPTIMIZATION-IF-TRUE*/ |
| 982 | i = z[1]-'0'; /* The common case of ?N for a single digit N */ |
| 983 | bOk = 1; |
| 984 | }else{ |
| 985 | bOk = 0==sqlite3Atoi64(&z[1], &i, n-1, SQLITE_UTF8); |
| 986 | } |
drh | 124c0b4 | 2011-06-01 18:15:55 +0000 | [diff] [blame] | 987 | testcase( i==0 ); |
| 988 | testcase( i==1 ); |
| 989 | testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 ); |
| 990 | testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ); |
| 991 | if( bOk==0 || i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){ |
| 992 | sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d", |
| 993 | db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]); |
drh | c9b3928 | 2016-10-03 16:33:14 +0000 | [diff] [blame] | 994 | return; |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 995 | } |
drh | 8e74e7b | 2017-01-31 12:41:48 +0000 | [diff] [blame] | 996 | x = (ynVar)i; |
drh | f326d66 | 2016-12-23 13:30:53 +0000 | [diff] [blame] | 997 | if( x>pParse->nVar ){ |
| 998 | pParse->nVar = (int)x; |
| 999 | doAdd = 1; |
| 1000 | }else if( sqlite3VListNumToName(pParse->pVList, x)==0 ){ |
| 1001 | doAdd = 1; |
drh | 124c0b4 | 2011-06-01 18:15:55 +0000 | [diff] [blame] | 1002 | } |
| 1003 | }else{ |
| 1004 | /* Wildcards like ":aaa", "$aaa" or "@aaa". Reuse the same variable |
| 1005 | ** number as the prior appearance of the same name, or if the name |
| 1006 | ** has never appeared before, reuse the same variable number |
| 1007 | */ |
drh | 9bf755c | 2016-12-23 03:59:31 +0000 | [diff] [blame] | 1008 | x = (ynVar)sqlite3VListNameToNum(pParse->pVList, z, n); |
| 1009 | if( x==0 ){ |
| 1010 | x = (ynVar)(++pParse->nVar); |
drh | f326d66 | 2016-12-23 13:30:53 +0000 | [diff] [blame] | 1011 | doAdd = 1; |
drh | 124c0b4 | 2011-06-01 18:15:55 +0000 | [diff] [blame] | 1012 | } |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 1013 | } |
drh | f326d66 | 2016-12-23 13:30:53 +0000 | [diff] [blame] | 1014 | if( doAdd ){ |
| 1015 | pParse->pVList = sqlite3VListAdd(db, pParse->pVList, z, n, x); |
| 1016 | } |
| 1017 | } |
| 1018 | pExpr->iColumn = x; |
| 1019 | if( x>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){ |
danielk1977 | 832b266 | 2007-05-09 11:37:22 +0000 | [diff] [blame] | 1020 | sqlite3ErrorMsg(pParse, "too many SQL variables"); |
| 1021 | } |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 1022 | } |
| 1023 | |
| 1024 | /* |
dan | f6963f9 | 2009-11-23 14:39:14 +0000 | [diff] [blame] | 1025 | ** Recursively delete an expression tree. |
drh | a2e0004 | 2002-01-22 03:13:42 +0000 | [diff] [blame] | 1026 | */ |
drh | 4f0010b | 2016-04-11 14:49:39 +0000 | [diff] [blame] | 1027 | static SQLITE_NOINLINE void sqlite3ExprDeleteNN(sqlite3 *db, Expr *p){ |
| 1028 | assert( p!=0 ); |
drh | d50ffc4 | 2011-03-08 02:38:28 +0000 | [diff] [blame] | 1029 | /* Sanity check: Assert that the IntValue is non-negative if it exists */ |
| 1030 | assert( !ExprHasProperty(p, EP_IntValue) || p->u.iValue>=0 ); |
drh | eda079c | 2018-09-20 19:02:15 +0000 | [diff] [blame] | 1031 | |
| 1032 | assert( !ExprHasProperty(p, EP_WinFunc) || p->y.pWin!=0 || db->mallocFailed ); |
| 1033 | assert( p->op!=TK_FUNCTION || ExprHasProperty(p, EP_TokenOnly|EP_Reduced) |
dan | 4f9adee | 2019-07-13 16:22:50 +0000 | [diff] [blame] | 1034 | || p->y.pWin==0 || ExprHasProperty(p, EP_WinFunc) ); |
drh | 209bc52 | 2016-09-23 21:36:24 +0000 | [diff] [blame] | 1035 | #ifdef SQLITE_DEBUG |
| 1036 | if( ExprHasProperty(p, EP_Leaf) && !ExprHasProperty(p, EP_TokenOnly) ){ |
| 1037 | assert( p->pLeft==0 ); |
| 1038 | assert( p->pRight==0 ); |
| 1039 | assert( p->x.pSelect==0 ); |
| 1040 | } |
| 1041 | #endif |
| 1042 | if( !ExprHasProperty(p, (EP_TokenOnly|EP_Leaf)) ){ |
drh | c5cd124 | 2013-09-12 16:50:49 +0000 | [diff] [blame] | 1043 | /* The Expr.x union is never used at the same time as Expr.pRight */ |
| 1044 | assert( p->x.pList==0 || p->pRight==0 ); |
drh | 4910a76 | 2016-09-03 01:46:15 +0000 | [diff] [blame] | 1045 | if( p->pLeft && p->op!=TK_SELECT_COLUMN ) sqlite3ExprDeleteNN(db, p->pLeft); |
drh | d108667 | 2017-07-07 13:59:34 +0000 | [diff] [blame] | 1046 | if( p->pRight ){ |
dan | 4f9adee | 2019-07-13 16:22:50 +0000 | [diff] [blame] | 1047 | assert( !ExprHasProperty(p, EP_WinFunc) ); |
drh | d108667 | 2017-07-07 13:59:34 +0000 | [diff] [blame] | 1048 | sqlite3ExprDeleteNN(db, p->pRight); |
| 1049 | }else if( ExprHasProperty(p, EP_xIsSelect) ){ |
dan | 4f9adee | 2019-07-13 16:22:50 +0000 | [diff] [blame] | 1050 | assert( !ExprHasProperty(p, EP_WinFunc) ); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1051 | sqlite3SelectDelete(db, p->x.pSelect); |
| 1052 | }else{ |
| 1053 | sqlite3ExprListDelete(db, p->x.pList); |
dan | 6ba7ab0 | 2019-07-02 11:56:47 +0000 | [diff] [blame] | 1054 | #ifndef SQLITE_OMIT_WINDOWFUNC |
dan | 4f9adee | 2019-07-13 16:22:50 +0000 | [diff] [blame] | 1055 | if( ExprHasProperty(p, EP_WinFunc) ){ |
| 1056 | sqlite3WindowDelete(db, p->y.pWin); |
dan | 8117f11 | 2019-07-10 20:16:53 +0000 | [diff] [blame] | 1057 | } |
dan | 6ba7ab0 | 2019-07-02 11:56:47 +0000 | [diff] [blame] | 1058 | #endif |
dan | 8117f11 | 2019-07-10 20:16:53 +0000 | [diff] [blame] | 1059 | } |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1060 | } |
drh | 209bc52 | 2016-09-23 21:36:24 +0000 | [diff] [blame] | 1061 | if( ExprHasProperty(p, EP_MemToken) ) sqlite3DbFree(db, p->u.zToken); |
drh | 33e619f | 2009-05-28 01:00:55 +0000 | [diff] [blame] | 1062 | if( !ExprHasProperty(p, EP_Static) ){ |
drh | dbd6a7d | 2017-04-05 12:39:49 +0000 | [diff] [blame] | 1063 | sqlite3DbFreeNN(db, p); |
drh | 33e619f | 2009-05-28 01:00:55 +0000 | [diff] [blame] | 1064 | } |
drh | a2e0004 | 2002-01-22 03:13:42 +0000 | [diff] [blame] | 1065 | } |
drh | 4f0010b | 2016-04-11 14:49:39 +0000 | [diff] [blame] | 1066 | void sqlite3ExprDelete(sqlite3 *db, Expr *p){ |
| 1067 | if( p ) sqlite3ExprDeleteNN(db, p); |
| 1068 | } |
drh | a2e0004 | 2002-01-22 03:13:42 +0000 | [diff] [blame] | 1069 | |
drh | 8e34e40 | 2019-06-11 10:43:56 +0000 | [diff] [blame] | 1070 | /* Invoke sqlite3RenameExprUnmap() and sqlite3ExprDelete() on the |
| 1071 | ** expression. |
| 1072 | */ |
| 1073 | void sqlite3ExprUnmapAndDelete(Parse *pParse, Expr *p){ |
| 1074 | if( p ){ |
| 1075 | if( IN_RENAME_OBJECT ){ |
| 1076 | sqlite3RenameExprUnmap(pParse, p); |
| 1077 | } |
| 1078 | sqlite3ExprDeleteNN(pParse->db, p); |
| 1079 | } |
| 1080 | } |
| 1081 | |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 1082 | /* |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1083 | ** Return the number of bytes allocated for the expression structure |
| 1084 | ** passed as the first argument. This is always one of EXPR_FULLSIZE, |
| 1085 | ** EXPR_REDUCEDSIZE or EXPR_TOKENONLYSIZE. |
| 1086 | */ |
| 1087 | static int exprStructSize(Expr *p){ |
| 1088 | if( ExprHasProperty(p, EP_TokenOnly) ) return EXPR_TOKENONLYSIZE; |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1089 | if( ExprHasProperty(p, EP_Reduced) ) return EXPR_REDUCEDSIZE; |
| 1090 | return EXPR_FULLSIZE; |
| 1091 | } |
| 1092 | |
| 1093 | /* |
drh | 33e619f | 2009-05-28 01:00:55 +0000 | [diff] [blame] | 1094 | ** The dupedExpr*Size() routines each return the number of bytes required |
| 1095 | ** to store a copy of an expression or expression tree. They differ in |
| 1096 | ** how much of the tree is measured. |
| 1097 | ** |
| 1098 | ** dupedExprStructSize() Size of only the Expr structure |
| 1099 | ** dupedExprNodeSize() Size of Expr + space for token |
| 1100 | ** dupedExprSize() Expr + token + subtree components |
| 1101 | ** |
| 1102 | *************************************************************************** |
| 1103 | ** |
| 1104 | ** The dupedExprStructSize() function returns two values OR-ed together: |
| 1105 | ** (1) the space required for a copy of the Expr structure only and |
| 1106 | ** (2) the EP_xxx flags that indicate what the structure size should be. |
| 1107 | ** The return values is always one of: |
| 1108 | ** |
| 1109 | ** EXPR_FULLSIZE |
| 1110 | ** EXPR_REDUCEDSIZE | EP_Reduced |
| 1111 | ** EXPR_TOKENONLYSIZE | EP_TokenOnly |
| 1112 | ** |
| 1113 | ** The size of the structure can be found by masking the return value |
| 1114 | ** of this routine with 0xfff. The flags can be found by masking the |
| 1115 | ** return value with EP_Reduced|EP_TokenOnly. |
| 1116 | ** |
| 1117 | ** Note that with flags==EXPRDUP_REDUCE, this routines works on full-size |
| 1118 | ** (unreduced) Expr objects as they or originally constructed by the parser. |
| 1119 | ** During expression analysis, extra information is computed and moved into |
dan | c95f38d | 2018-06-18 20:34:43 +0000 | [diff] [blame] | 1120 | ** later parts of the Expr object and that extra information might get chopped |
drh | 33e619f | 2009-05-28 01:00:55 +0000 | [diff] [blame] | 1121 | ** off if the expression is reduced. Note also that it does not work to |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 1122 | ** make an EXPRDUP_REDUCE copy of a reduced expression. It is only legal |
drh | 33e619f | 2009-05-28 01:00:55 +0000 | [diff] [blame] | 1123 | ** to reduce a pristine expression tree from the parser. The implementation |
| 1124 | ** of dupedExprStructSize() contain multiple assert() statements that attempt |
| 1125 | ** to enforce this constraint. |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1126 | */ |
| 1127 | static int dupedExprStructSize(Expr *p, int flags){ |
| 1128 | int nSize; |
drh | 33e619f | 2009-05-28 01:00:55 +0000 | [diff] [blame] | 1129 | assert( flags==EXPRDUP_REDUCE || flags==0 ); /* Only one flag value allowed */ |
drh | aecd802 | 2013-09-13 18:15:15 +0000 | [diff] [blame] | 1130 | assert( EXPR_FULLSIZE<=0xfff ); |
| 1131 | assert( (0xfff & (EP_Reduced|EP_TokenOnly))==0 ); |
dan | 67a9b8e | 2018-06-22 20:51:35 +0000 | [diff] [blame] | 1132 | if( 0==flags || p->op==TK_SELECT_COLUMN |
| 1133 | #ifndef SQLITE_OMIT_WINDOWFUNC |
drh | eda079c | 2018-09-20 19:02:15 +0000 | [diff] [blame] | 1134 | || ExprHasProperty(p, EP_WinFunc) |
dan | 67a9b8e | 2018-06-22 20:51:35 +0000 | [diff] [blame] | 1135 | #endif |
| 1136 | ){ |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1137 | nSize = EXPR_FULLSIZE; |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1138 | }else{ |
drh | c5cd124 | 2013-09-12 16:50:49 +0000 | [diff] [blame] | 1139 | assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) ); |
drh | 33e619f | 2009-05-28 01:00:55 +0000 | [diff] [blame] | 1140 | assert( !ExprHasProperty(p, EP_FromJoin) ); |
drh | c5cd124 | 2013-09-12 16:50:49 +0000 | [diff] [blame] | 1141 | assert( !ExprHasProperty(p, EP_MemToken) ); |
drh | ebb6a65 | 2013-09-12 23:42:22 +0000 | [diff] [blame] | 1142 | assert( !ExprHasProperty(p, EP_NoReduce) ); |
drh | aecd802 | 2013-09-13 18:15:15 +0000 | [diff] [blame] | 1143 | if( p->pLeft || p->x.pList ){ |
drh | 33e619f | 2009-05-28 01:00:55 +0000 | [diff] [blame] | 1144 | nSize = EXPR_REDUCEDSIZE | EP_Reduced; |
| 1145 | }else{ |
drh | aecd802 | 2013-09-13 18:15:15 +0000 | [diff] [blame] | 1146 | assert( p->pRight==0 ); |
drh | 33e619f | 2009-05-28 01:00:55 +0000 | [diff] [blame] | 1147 | nSize = EXPR_TOKENONLYSIZE | EP_TokenOnly; |
| 1148 | } |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1149 | } |
| 1150 | return nSize; |
| 1151 | } |
| 1152 | |
| 1153 | /* |
drh | 33e619f | 2009-05-28 01:00:55 +0000 | [diff] [blame] | 1154 | ** This function returns the space in bytes required to store the copy |
| 1155 | ** of the Expr structure and a copy of the Expr.u.zToken string (if that |
| 1156 | ** string is defined.) |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1157 | */ |
| 1158 | static int dupedExprNodeSize(Expr *p, int flags){ |
drh | 33e619f | 2009-05-28 01:00:55 +0000 | [diff] [blame] | 1159 | int nByte = dupedExprStructSize(p, flags) & 0xfff; |
| 1160 | if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){ |
drh | 7301e77 | 2018-10-31 20:52:00 +0000 | [diff] [blame] | 1161 | nByte += sqlite3Strlen30NN(p->u.zToken)+1; |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1162 | } |
danielk1977 | bc73971 | 2009-03-23 04:33:32 +0000 | [diff] [blame] | 1163 | return ROUND8(nByte); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1164 | } |
| 1165 | |
| 1166 | /* |
| 1167 | ** Return the number of bytes required to create a duplicate of the |
| 1168 | ** expression passed as the first argument. The second argument is a |
| 1169 | ** mask containing EXPRDUP_XXX flags. |
| 1170 | ** |
| 1171 | ** The value returned includes space to create a copy of the Expr struct |
drh | 33e619f | 2009-05-28 01:00:55 +0000 | [diff] [blame] | 1172 | ** itself and the buffer referred to by Expr.u.zToken, if any. |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1173 | ** |
| 1174 | ** If the EXPRDUP_REDUCE flag is set, then the return value includes |
| 1175 | ** space to duplicate all Expr nodes in the tree formed by Expr.pLeft |
| 1176 | ** and Expr.pRight variables (but not for any structures pointed to or |
| 1177 | ** descended from the Expr.x.pList or Expr.x.pSelect variables). |
| 1178 | */ |
| 1179 | static int dupedExprSize(Expr *p, int flags){ |
| 1180 | int nByte = 0; |
| 1181 | if( p ){ |
| 1182 | nByte = dupedExprNodeSize(p, flags); |
| 1183 | if( flags&EXPRDUP_REDUCE ){ |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 1184 | nByte += dupedExprSize(p->pLeft, flags) + dupedExprSize(p->pRight, flags); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1185 | } |
| 1186 | } |
| 1187 | return nByte; |
| 1188 | } |
| 1189 | |
| 1190 | /* |
| 1191 | ** This function is similar to sqlite3ExprDup(), except that if pzBuffer |
| 1192 | ** is not NULL then *pzBuffer is assumed to point to a buffer large enough |
drh | 33e619f | 2009-05-28 01:00:55 +0000 | [diff] [blame] | 1193 | ** to store the copy of expression p, the copies of p->u.zToken |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1194 | ** (if applicable), and the copies of the p->pLeft and p->pRight expressions, |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 1195 | ** if any. Before returning, *pzBuffer is set to the first byte past the |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1196 | ** portion of the buffer copied into by this function. |
| 1197 | */ |
drh | 3c19469 | 2016-04-11 16:43:43 +0000 | [diff] [blame] | 1198 | static Expr *exprDup(sqlite3 *db, Expr *p, int dupFlags, u8 **pzBuffer){ |
| 1199 | Expr *pNew; /* Value to return */ |
| 1200 | u8 *zAlloc; /* Memory space from which to build Expr object */ |
| 1201 | u32 staticFlag; /* EP_Static if space not obtained from malloc */ |
| 1202 | |
drh | 575fad6 | 2016-02-05 13:38:36 +0000 | [diff] [blame] | 1203 | assert( db!=0 ); |
drh | 3c19469 | 2016-04-11 16:43:43 +0000 | [diff] [blame] | 1204 | assert( p ); |
| 1205 | assert( dupFlags==0 || dupFlags==EXPRDUP_REDUCE ); |
| 1206 | assert( pzBuffer==0 || dupFlags==EXPRDUP_REDUCE ); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1207 | |
drh | 3c19469 | 2016-04-11 16:43:43 +0000 | [diff] [blame] | 1208 | /* Figure out where to write the new Expr structure. */ |
| 1209 | if( pzBuffer ){ |
| 1210 | zAlloc = *pzBuffer; |
| 1211 | staticFlag = EP_Static; |
| 1212 | }else{ |
| 1213 | zAlloc = sqlite3DbMallocRawNN(db, dupedExprSize(p, dupFlags)); |
| 1214 | staticFlag = 0; |
| 1215 | } |
| 1216 | pNew = (Expr *)zAlloc; |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1217 | |
drh | 3c19469 | 2016-04-11 16:43:43 +0000 | [diff] [blame] | 1218 | if( pNew ){ |
| 1219 | /* Set nNewSize to the size allocated for the structure pointed to |
| 1220 | ** by pNew. This is either EXPR_FULLSIZE, EXPR_REDUCEDSIZE or |
| 1221 | ** EXPR_TOKENONLYSIZE. nToken is set to the number of bytes consumed |
| 1222 | ** by the copy of the p->u.zToken string (if any). |
| 1223 | */ |
| 1224 | const unsigned nStructSize = dupedExprStructSize(p, dupFlags); |
| 1225 | const int nNewSize = nStructSize & 0xfff; |
| 1226 | int nToken; |
| 1227 | if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){ |
| 1228 | nToken = sqlite3Strlen30(p->u.zToken) + 1; |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1229 | }else{ |
drh | 3c19469 | 2016-04-11 16:43:43 +0000 | [diff] [blame] | 1230 | nToken = 0; |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1231 | } |
drh | 3c19469 | 2016-04-11 16:43:43 +0000 | [diff] [blame] | 1232 | if( dupFlags ){ |
| 1233 | assert( ExprHasProperty(p, EP_Reduced)==0 ); |
| 1234 | memcpy(zAlloc, p, nNewSize); |
| 1235 | }else{ |
| 1236 | u32 nSize = (u32)exprStructSize(p); |
| 1237 | memcpy(zAlloc, p, nSize); |
| 1238 | if( nSize<EXPR_FULLSIZE ){ |
| 1239 | memset(&zAlloc[nSize], 0, EXPR_FULLSIZE-nSize); |
| 1240 | } |
| 1241 | } |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1242 | |
drh | 3c19469 | 2016-04-11 16:43:43 +0000 | [diff] [blame] | 1243 | /* Set the EP_Reduced, EP_TokenOnly, and EP_Static flags appropriately. */ |
| 1244 | pNew->flags &= ~(EP_Reduced|EP_TokenOnly|EP_Static|EP_MemToken); |
| 1245 | pNew->flags |= nStructSize & (EP_Reduced|EP_TokenOnly); |
| 1246 | pNew->flags |= staticFlag; |
| 1247 | |
| 1248 | /* Copy the p->u.zToken string, if any. */ |
| 1249 | if( nToken ){ |
| 1250 | char *zToken = pNew->u.zToken = (char*)&zAlloc[nNewSize]; |
| 1251 | memcpy(zToken, p->u.zToken, nToken); |
| 1252 | } |
| 1253 | |
drh | 209bc52 | 2016-09-23 21:36:24 +0000 | [diff] [blame] | 1254 | if( 0==((p->flags|pNew->flags) & (EP_TokenOnly|EP_Leaf)) ){ |
drh | 3c19469 | 2016-04-11 16:43:43 +0000 | [diff] [blame] | 1255 | /* Fill in the pNew->x.pSelect or pNew->x.pList member. */ |
| 1256 | if( ExprHasProperty(p, EP_xIsSelect) ){ |
| 1257 | pNew->x.pSelect = sqlite3SelectDup(db, p->x.pSelect, dupFlags); |
drh | 33e619f | 2009-05-28 01:00:55 +0000 | [diff] [blame] | 1258 | }else{ |
drh | 3c19469 | 2016-04-11 16:43:43 +0000 | [diff] [blame] | 1259 | pNew->x.pList = sqlite3ExprListDup(db, p->x.pList, dupFlags); |
drh | 33e619f | 2009-05-28 01:00:55 +0000 | [diff] [blame] | 1260 | } |
drh | 3c19469 | 2016-04-11 16:43:43 +0000 | [diff] [blame] | 1261 | } |
| 1262 | |
| 1263 | /* Fill in pNew->pLeft and pNew->pRight. */ |
dan | 4f9adee | 2019-07-13 16:22:50 +0000 | [diff] [blame] | 1264 | if( ExprHasProperty(pNew, EP_Reduced|EP_TokenOnly|EP_WinFunc) ){ |
drh | 5398806 | 2018-09-24 15:39:30 +0000 | [diff] [blame] | 1265 | zAlloc += dupedExprNodeSize(p, dupFlags); |
drh | 209bc52 | 2016-09-23 21:36:24 +0000 | [diff] [blame] | 1266 | if( !ExprHasProperty(pNew, EP_TokenOnly|EP_Leaf) ){ |
drh | 3c19469 | 2016-04-11 16:43:43 +0000 | [diff] [blame] | 1267 | pNew->pLeft = p->pLeft ? |
| 1268 | exprDup(db, p->pLeft, EXPRDUP_REDUCE, &zAlloc) : 0; |
| 1269 | pNew->pRight = p->pRight ? |
| 1270 | exprDup(db, p->pRight, EXPRDUP_REDUCE, &zAlloc) : 0; |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1271 | } |
dan | 67a9b8e | 2018-06-22 20:51:35 +0000 | [diff] [blame] | 1272 | #ifndef SQLITE_OMIT_WINDOWFUNC |
drh | eda079c | 2018-09-20 19:02:15 +0000 | [diff] [blame] | 1273 | if( ExprHasProperty(p, EP_WinFunc) ){ |
| 1274 | pNew->y.pWin = sqlite3WindowDup(db, pNew, p->y.pWin); |
| 1275 | assert( ExprHasProperty(pNew, EP_WinFunc) ); |
dan | e2f781b | 2018-05-17 19:24:08 +0000 | [diff] [blame] | 1276 | } |
dan | 67a9b8e | 2018-06-22 20:51:35 +0000 | [diff] [blame] | 1277 | #endif /* SQLITE_OMIT_WINDOWFUNC */ |
drh | 5398806 | 2018-09-24 15:39:30 +0000 | [diff] [blame] | 1278 | if( pzBuffer ){ |
| 1279 | *pzBuffer = zAlloc; |
| 1280 | } |
| 1281 | }else{ |
drh | 209bc52 | 2016-09-23 21:36:24 +0000 | [diff] [blame] | 1282 | if( !ExprHasProperty(p, EP_TokenOnly|EP_Leaf) ){ |
drh | 9854260 | 2016-08-20 17:00:16 +0000 | [diff] [blame] | 1283 | if( pNew->op==TK_SELECT_COLUMN ){ |
| 1284 | pNew->pLeft = p->pLeft; |
drh | 47073f6 | 2017-01-02 22:36:32 +0000 | [diff] [blame] | 1285 | assert( p->iColumn==0 || p->pRight==0 ); |
| 1286 | assert( p->pRight==0 || p->pRight==p->pLeft ); |
drh | 9854260 | 2016-08-20 17:00:16 +0000 | [diff] [blame] | 1287 | }else{ |
| 1288 | pNew->pLeft = sqlite3ExprDup(db, p->pLeft, 0); |
| 1289 | } |
drh | 3c19469 | 2016-04-11 16:43:43 +0000 | [diff] [blame] | 1290 | pNew->pRight = sqlite3ExprDup(db, p->pRight, 0); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1291 | } |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1292 | } |
| 1293 | } |
| 1294 | return pNew; |
| 1295 | } |
| 1296 | |
| 1297 | /* |
dan | bfe31e7 | 2014-01-15 14:17:31 +0000 | [diff] [blame] | 1298 | ** Create and return a deep copy of the object passed as the second |
| 1299 | ** argument. If an OOM condition is encountered, NULL is returned |
| 1300 | ** and the db->mallocFailed flag set. |
| 1301 | */ |
dan | eede6a5 | 2014-01-15 19:42:23 +0000 | [diff] [blame] | 1302 | #ifndef SQLITE_OMIT_CTE |
dan | bfe31e7 | 2014-01-15 14:17:31 +0000 | [diff] [blame] | 1303 | static With *withDup(sqlite3 *db, With *p){ |
dan | 4e9119d | 2014-01-13 15:12:23 +0000 | [diff] [blame] | 1304 | With *pRet = 0; |
| 1305 | if( p ){ |
drh | d4de9f7 | 2019-04-14 00:34:20 +0000 | [diff] [blame] | 1306 | sqlite3_int64 nByte = sizeof(*p) + sizeof(p->a[0]) * (p->nCte-1); |
dan | 4e9119d | 2014-01-13 15:12:23 +0000 | [diff] [blame] | 1307 | pRet = sqlite3DbMallocZero(db, nByte); |
| 1308 | if( pRet ){ |
| 1309 | int i; |
| 1310 | pRet->nCte = p->nCte; |
| 1311 | for(i=0; i<p->nCte; i++){ |
| 1312 | pRet->a[i].pSelect = sqlite3SelectDup(db, p->a[i].pSelect, 0); |
| 1313 | pRet->a[i].pCols = sqlite3ExprListDup(db, p->a[i].pCols, 0); |
| 1314 | pRet->a[i].zName = sqlite3DbStrDup(db, p->a[i].zName); |
| 1315 | } |
| 1316 | } |
| 1317 | } |
| 1318 | return pRet; |
| 1319 | } |
dan | eede6a5 | 2014-01-15 19:42:23 +0000 | [diff] [blame] | 1320 | #else |
| 1321 | # define withDup(x,y) 0 |
| 1322 | #endif |
dan | 4e9119d | 2014-01-13 15:12:23 +0000 | [diff] [blame] | 1323 | |
drh | a838997 | 2018-12-06 22:04:19 +0000 | [diff] [blame] | 1324 | #ifndef SQLITE_OMIT_WINDOWFUNC |
| 1325 | /* |
| 1326 | ** The gatherSelectWindows() procedure and its helper routine |
| 1327 | ** gatherSelectWindowsCallback() are used to scan all the expressions |
| 1328 | ** an a newly duplicated SELECT statement and gather all of the Window |
| 1329 | ** objects found there, assembling them onto the linked list at Select->pWin. |
| 1330 | */ |
| 1331 | static int gatherSelectWindowsCallback(Walker *pWalker, Expr *pExpr){ |
dan | 6ba7ab0 | 2019-07-02 11:56:47 +0000 | [diff] [blame] | 1332 | if( pExpr->op==TK_FUNCTION && ExprHasProperty(pExpr, EP_WinFunc) ){ |
dan | 75b0821 | 2019-07-22 16:20:03 +0000 | [diff] [blame] | 1333 | Select *pSelect = pWalker->u.pSelect; |
| 1334 | Window *pWin = pExpr->y.pWin; |
| 1335 | assert( pWin ); |
dan | 4f9adee | 2019-07-13 16:22:50 +0000 | [diff] [blame] | 1336 | assert( IsWindowFunc(pExpr) ); |
dan | e0ae3f6 | 2019-07-22 17:28:43 +0000 | [diff] [blame] | 1337 | assert( pWin->ppThis==0 ); |
dan | a3fcc00 | 2019-08-15 13:53:22 +0000 | [diff] [blame] | 1338 | sqlite3WindowLink(pSelect, pWin); |
drh | a838997 | 2018-12-06 22:04:19 +0000 | [diff] [blame] | 1339 | } |
| 1340 | return WRC_Continue; |
| 1341 | } |
drh | a37b6a5 | 2018-12-06 22:12:18 +0000 | [diff] [blame] | 1342 | static int gatherSelectWindowsSelectCallback(Walker *pWalker, Select *p){ |
| 1343 | return p==pWalker->u.pSelect ? WRC_Continue : WRC_Prune; |
| 1344 | } |
drh | a838997 | 2018-12-06 22:04:19 +0000 | [diff] [blame] | 1345 | static void gatherSelectWindows(Select *p){ |
| 1346 | Walker w; |
| 1347 | w.xExprCallback = gatherSelectWindowsCallback; |
drh | a37b6a5 | 2018-12-06 22:12:18 +0000 | [diff] [blame] | 1348 | w.xSelectCallback = gatherSelectWindowsSelectCallback; |
| 1349 | w.xSelectCallback2 = 0; |
drh | 9c46c66 | 2019-02-01 15:06:27 +0000 | [diff] [blame] | 1350 | w.pParse = 0; |
drh | a838997 | 2018-12-06 22:04:19 +0000 | [diff] [blame] | 1351 | w.u.pSelect = p; |
drh | a37b6a5 | 2018-12-06 22:12:18 +0000 | [diff] [blame] | 1352 | sqlite3WalkSelect(&w, p); |
drh | a838997 | 2018-12-06 22:04:19 +0000 | [diff] [blame] | 1353 | } |
| 1354 | #endif |
| 1355 | |
| 1356 | |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 1357 | /* |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 1358 | ** The following group of routines make deep copies of expressions, |
| 1359 | ** expression lists, ID lists, and select statements. The copies can |
| 1360 | ** be deleted (by being passed to their respective ...Delete() routines) |
| 1361 | ** without effecting the originals. |
| 1362 | ** |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1363 | ** The expression list, ID, and source lists return by sqlite3ExprListDup(), |
| 1364 | ** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 1365 | ** by subsequent calls to sqlite*ListAppend() routines. |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 1366 | ** |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 1367 | ** Any tables that the SrcList might point to are not duplicated. |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1368 | ** |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 1369 | ** The flags parameter contains a combination of the EXPRDUP_XXX flags. |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1370 | ** If the EXPRDUP_REDUCE flag is set, then the structure returned is a |
| 1371 | ** truncated version of the usual Expr structure that will be stored as |
| 1372 | ** part of the in-memory representation of the database schema. |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 1373 | */ |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1374 | Expr *sqlite3ExprDup(sqlite3 *db, Expr *p, int flags){ |
drh | 72ea29d | 2015-12-08 16:58:45 +0000 | [diff] [blame] | 1375 | assert( flags==0 || flags==EXPRDUP_REDUCE ); |
drh | 3c19469 | 2016-04-11 16:43:43 +0000 | [diff] [blame] | 1376 | return p ? exprDup(db, p, flags, 0) : 0; |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 1377 | } |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1378 | ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p, int flags){ |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 1379 | ExprList *pNew; |
drh | 145716b | 2004-09-24 12:24:06 +0000 | [diff] [blame] | 1380 | struct ExprList_item *pItem, *pOldItem; |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 1381 | int i; |
drh | b163748 | 2017-01-03 00:27:16 +0000 | [diff] [blame] | 1382 | Expr *pPriorSelectCol = 0; |
drh | 575fad6 | 2016-02-05 13:38:36 +0000 | [diff] [blame] | 1383 | assert( db!=0 ); |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 1384 | if( p==0 ) return 0; |
drh | 9725819 | 2017-09-17 19:45:28 +0000 | [diff] [blame] | 1385 | pNew = sqlite3DbMallocRawNN(db, sqlite3DbMallocSize(db, p)); |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 1386 | if( pNew==0 ) return 0; |
drh | a19543f | 2017-09-15 15:17:48 +0000 | [diff] [blame] | 1387 | pNew->nExpr = p->nExpr; |
drh | 4360617 | 2017-04-05 11:32:13 +0000 | [diff] [blame] | 1388 | pItem = pNew->a; |
drh | 145716b | 2004-09-24 12:24:06 +0000 | [diff] [blame] | 1389 | pOldItem = p->a; |
| 1390 | for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){ |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1391 | Expr *pOldExpr = pOldItem->pExpr; |
drh | 47073f6 | 2017-01-02 22:36:32 +0000 | [diff] [blame] | 1392 | Expr *pNewExpr; |
drh | b5526ea | 2009-07-16 12:41:05 +0000 | [diff] [blame] | 1393 | pItem->pExpr = sqlite3ExprDup(db, pOldExpr, flags); |
drh | 47073f6 | 2017-01-02 22:36:32 +0000 | [diff] [blame] | 1394 | if( pOldExpr |
| 1395 | && pOldExpr->op==TK_SELECT_COLUMN |
| 1396 | && (pNewExpr = pItem->pExpr)!=0 |
| 1397 | ){ |
| 1398 | assert( pNewExpr->iColumn==0 || i>0 ); |
| 1399 | if( pNewExpr->iColumn==0 ){ |
| 1400 | assert( pOldExpr->pLeft==pOldExpr->pRight ); |
drh | b163748 | 2017-01-03 00:27:16 +0000 | [diff] [blame] | 1401 | pPriorSelectCol = pNewExpr->pLeft = pNewExpr->pRight; |
| 1402 | }else{ |
| 1403 | assert( i>0 ); |
| 1404 | assert( pItem[-1].pExpr!=0 ); |
| 1405 | assert( pNewExpr->iColumn==pItem[-1].pExpr->iColumn+1 ); |
| 1406 | assert( pPriorSelectCol==pItem[-1].pExpr->pLeft ); |
| 1407 | pNewExpr->pLeft = pPriorSelectCol; |
drh | 47073f6 | 2017-01-02 22:36:32 +0000 | [diff] [blame] | 1408 | } |
| 1409 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1410 | pItem->zName = sqlite3DbStrDup(db, pOldItem->zName); |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 1411 | pItem->zSpan = sqlite3DbStrDup(db, pOldItem->zSpan); |
dan | 6e11892 | 2019-08-12 16:36:38 +0000 | [diff] [blame] | 1412 | pItem->sortFlags = pOldItem->sortFlags; |
drh | 3e7bc9c | 2004-02-21 19:17:17 +0000 | [diff] [blame] | 1413 | pItem->done = 0; |
dan | ae8e45c | 2019-08-19 19:59:50 +0000 | [diff] [blame] | 1414 | pItem->bNulls = pOldItem->bNulls; |
drh | 2c036cf | 2013-06-26 00:34:13 +0000 | [diff] [blame] | 1415 | pItem->bSpanIsTab = pOldItem->bSpanIsTab; |
dan | 24e25d3 | 2018-04-14 18:46:20 +0000 | [diff] [blame] | 1416 | pItem->bSorterRef = pOldItem->bSorterRef; |
drh | c2acc4e | 2013-11-15 18:15:19 +0000 | [diff] [blame] | 1417 | pItem->u = pOldItem->u; |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 1418 | } |
| 1419 | return pNew; |
| 1420 | } |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 1421 | |
| 1422 | /* |
| 1423 | ** If cursors, triggers, views and subqueries are all omitted from |
| 1424 | ** the build, then none of the following routines, except for |
| 1425 | ** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes |
| 1426 | ** called with a NULL argument. |
| 1427 | */ |
danielk1977 | 6a67fe8 | 2005-02-04 04:07:16 +0000 | [diff] [blame] | 1428 | #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \ |
| 1429 | || !defined(SQLITE_OMIT_SUBQUERY) |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1430 | SrcList *sqlite3SrcListDup(sqlite3 *db, SrcList *p, int flags){ |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 1431 | SrcList *pNew; |
| 1432 | int i; |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 1433 | int nByte; |
drh | 575fad6 | 2016-02-05 13:38:36 +0000 | [diff] [blame] | 1434 | assert( db!=0 ); |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 1435 | if( p==0 ) return 0; |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 1436 | nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0); |
drh | 575fad6 | 2016-02-05 13:38:36 +0000 | [diff] [blame] | 1437 | pNew = sqlite3DbMallocRawNN(db, nByte ); |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 1438 | if( pNew==0 ) return 0; |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 1439 | pNew->nSrc = pNew->nAlloc = p->nSrc; |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 1440 | for(i=0; i<p->nSrc; i++){ |
drh | 4efc475 | 2004-01-16 15:55:37 +0000 | [diff] [blame] | 1441 | struct SrcList_item *pNewItem = &pNew->a[i]; |
| 1442 | struct SrcList_item *pOldItem = &p->a[i]; |
drh | ed8a3bb | 2005-06-06 21:19:56 +0000 | [diff] [blame] | 1443 | Table *pTab; |
dan | 41fb5cd | 2012-10-04 19:33:00 +0000 | [diff] [blame] | 1444 | pNewItem->pSchema = pOldItem->pSchema; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1445 | pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase); |
| 1446 | pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName); |
| 1447 | pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias); |
drh | 8a48b9c | 2015-08-19 15:20:00 +0000 | [diff] [blame] | 1448 | pNewItem->fg = pOldItem->fg; |
drh | 4efc475 | 2004-01-16 15:55:37 +0000 | [diff] [blame] | 1449 | pNewItem->iCursor = pOldItem->iCursor; |
drh | 5b6a9ed | 2011-09-15 23:58:14 +0000 | [diff] [blame] | 1450 | pNewItem->addrFillSub = pOldItem->addrFillSub; |
| 1451 | pNewItem->regReturn = pOldItem->regReturn; |
drh | 8a48b9c | 2015-08-19 15:20:00 +0000 | [diff] [blame] | 1452 | if( pNewItem->fg.isIndexedBy ){ |
| 1453 | pNewItem->u1.zIndexedBy = sqlite3DbStrDup(db, pOldItem->u1.zIndexedBy); |
| 1454 | } |
| 1455 | pNewItem->pIBIndex = pOldItem->pIBIndex; |
| 1456 | if( pNewItem->fg.isTabFunc ){ |
| 1457 | pNewItem->u1.pFuncArg = |
| 1458 | sqlite3ExprListDup(db, pOldItem->u1.pFuncArg, flags); |
| 1459 | } |
drh | ed8a3bb | 2005-06-06 21:19:56 +0000 | [diff] [blame] | 1460 | pTab = pNewItem->pTab = pOldItem->pTab; |
| 1461 | if( pTab ){ |
drh | 79df778 | 2016-12-14 14:07:35 +0000 | [diff] [blame] | 1462 | pTab->nTabRef++; |
danielk1977 | a1cb183 | 2005-02-12 08:59:55 +0000 | [diff] [blame] | 1463 | } |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1464 | pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect, flags); |
| 1465 | pNewItem->pOn = sqlite3ExprDup(db, pOldItem->pOn, flags); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1466 | pNewItem->pUsing = sqlite3IdListDup(db, pOldItem->pUsing); |
danielk1977 | 6c18b6e | 2005-01-30 09:17:58 +0000 | [diff] [blame] | 1467 | pNewItem->colUsed = pOldItem->colUsed; |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 1468 | } |
| 1469 | return pNew; |
| 1470 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1471 | IdList *sqlite3IdListDup(sqlite3 *db, IdList *p){ |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 1472 | IdList *pNew; |
| 1473 | int i; |
drh | 575fad6 | 2016-02-05 13:38:36 +0000 | [diff] [blame] | 1474 | assert( db!=0 ); |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 1475 | if( p==0 ) return 0; |
drh | 575fad6 | 2016-02-05 13:38:36 +0000 | [diff] [blame] | 1476 | pNew = sqlite3DbMallocRawNN(db, sizeof(*pNew) ); |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 1477 | if( pNew==0 ) return 0; |
drh | 6c53515 | 2012-02-02 03:38:30 +0000 | [diff] [blame] | 1478 | pNew->nId = p->nId; |
drh | 575fad6 | 2016-02-05 13:38:36 +0000 | [diff] [blame] | 1479 | pNew->a = sqlite3DbMallocRawNN(db, p->nId*sizeof(p->a[0]) ); |
danielk1977 | d5d5652 | 2005-03-16 12:15:20 +0000 | [diff] [blame] | 1480 | if( pNew->a==0 ){ |
drh | dbd6a7d | 2017-04-05 12:39:49 +0000 | [diff] [blame] | 1481 | sqlite3DbFreeNN(db, pNew); |
danielk1977 | d5d5652 | 2005-03-16 12:15:20 +0000 | [diff] [blame] | 1482 | return 0; |
| 1483 | } |
drh | 6c53515 | 2012-02-02 03:38:30 +0000 | [diff] [blame] | 1484 | /* Note that because the size of the allocation for p->a[] is not |
| 1485 | ** necessarily a power of two, sqlite3IdListAppend() may not be called |
| 1486 | ** on the duplicate created by this function. */ |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 1487 | for(i=0; i<p->nId; i++){ |
drh | 4efc475 | 2004-01-16 15:55:37 +0000 | [diff] [blame] | 1488 | struct IdList_item *pNewItem = &pNew->a[i]; |
| 1489 | struct IdList_item *pOldItem = &p->a[i]; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1490 | pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName); |
drh | 4efc475 | 2004-01-16 15:55:37 +0000 | [diff] [blame] | 1491 | pNewItem->idx = pOldItem->idx; |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 1492 | } |
| 1493 | return pNew; |
| 1494 | } |
dan | a746620 | 2017-02-03 14:44:52 +0000 | [diff] [blame] | 1495 | Select *sqlite3SelectDup(sqlite3 *db, Select *pDup, int flags){ |
| 1496 | Select *pRet = 0; |
| 1497 | Select *pNext = 0; |
| 1498 | Select **pp = &pRet; |
| 1499 | Select *p; |
| 1500 | |
drh | 575fad6 | 2016-02-05 13:38:36 +0000 | [diff] [blame] | 1501 | assert( db!=0 ); |
dan | a746620 | 2017-02-03 14:44:52 +0000 | [diff] [blame] | 1502 | for(p=pDup; p; p=p->pPrior){ |
| 1503 | Select *pNew = sqlite3DbMallocRawNN(db, sizeof(*p) ); |
| 1504 | if( pNew==0 ) break; |
| 1505 | pNew->pEList = sqlite3ExprListDup(db, p->pEList, flags); |
| 1506 | pNew->pSrc = sqlite3SrcListDup(db, p->pSrc, flags); |
| 1507 | pNew->pWhere = sqlite3ExprDup(db, p->pWhere, flags); |
| 1508 | pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags); |
| 1509 | pNew->pHaving = sqlite3ExprDup(db, p->pHaving, flags); |
| 1510 | pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, flags); |
| 1511 | pNew->op = p->op; |
| 1512 | pNew->pNext = pNext; |
| 1513 | pNew->pPrior = 0; |
| 1514 | pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags); |
dan | a746620 | 2017-02-03 14:44:52 +0000 | [diff] [blame] | 1515 | pNew->iLimit = 0; |
| 1516 | pNew->iOffset = 0; |
| 1517 | pNew->selFlags = p->selFlags & ~SF_UsesEphemeral; |
| 1518 | pNew->addrOpenEphm[0] = -1; |
| 1519 | pNew->addrOpenEphm[1] = -1; |
| 1520 | pNew->nSelectRow = p->nSelectRow; |
| 1521 | pNew->pWith = withDup(db, p->pWith); |
dan | 67a9b8e | 2018-06-22 20:51:35 +0000 | [diff] [blame] | 1522 | #ifndef SQLITE_OMIT_WINDOWFUNC |
dan | 2e362f9 | 2018-05-17 14:26:27 +0000 | [diff] [blame] | 1523 | pNew->pWin = 0; |
dan | c95f38d | 2018-06-18 20:34:43 +0000 | [diff] [blame] | 1524 | pNew->pWinDefn = sqlite3WindowListDup(db, p->pWinDefn); |
dan | 4780b9a | 2019-08-20 14:43:01 +0000 | [diff] [blame] | 1525 | if( p->pWin && db->mallocFailed==0 ) gatherSelectWindows(pNew); |
dan | 67a9b8e | 2018-06-22 20:51:35 +0000 | [diff] [blame] | 1526 | #endif |
drh | fef3776 | 2018-07-10 19:48:35 +0000 | [diff] [blame] | 1527 | pNew->selId = p->selId; |
dan | a746620 | 2017-02-03 14:44:52 +0000 | [diff] [blame] | 1528 | *pp = pNew; |
| 1529 | pp = &pNew->pPrior; |
| 1530 | pNext = pNew; |
| 1531 | } |
| 1532 | |
| 1533 | return pRet; |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 1534 | } |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 1535 | #else |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 1536 | Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){ |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 1537 | assert( p==0 ); |
| 1538 | return 0; |
| 1539 | } |
| 1540 | #endif |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 1541 | |
| 1542 | |
| 1543 | /* |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 1544 | ** Add a new element to the end of an expression list. If pList is |
| 1545 | ** initially NULL, then create a new expression list. |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 1546 | ** |
drh | a19543f | 2017-09-15 15:17:48 +0000 | [diff] [blame] | 1547 | ** The pList argument must be either NULL or a pointer to an ExprList |
| 1548 | ** obtained from a prior call to sqlite3ExprListAppend(). This routine |
| 1549 | ** may not be used with an ExprList obtained from sqlite3ExprListDup(). |
| 1550 | ** Reason: This routine assumes that the number of slots in pList->a[] |
| 1551 | ** is a power of two. That is true for sqlite3ExprListAppend() returns |
| 1552 | ** but is not necessarily true from the return value of sqlite3ExprListDup(). |
| 1553 | ** |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 1554 | ** If a memory allocation error occurs, the entire list is freed and |
| 1555 | ** NULL is returned. If non-NULL is returned, then it is guaranteed |
| 1556 | ** that the new entry was successfully appended. |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 1557 | */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1558 | ExprList *sqlite3ExprListAppend( |
| 1559 | Parse *pParse, /* Parsing context */ |
| 1560 | ExprList *pList, /* List to which to append. Might be NULL */ |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 1561 | Expr *pExpr /* Expression to be appended. Might be NULL */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1562 | ){ |
drh | 4360617 | 2017-04-05 11:32:13 +0000 | [diff] [blame] | 1563 | struct ExprList_item *pItem; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1564 | sqlite3 *db = pParse->db; |
drh | 575fad6 | 2016-02-05 13:38:36 +0000 | [diff] [blame] | 1565 | assert( db!=0 ); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 1566 | if( pList==0 ){ |
drh | 575fad6 | 2016-02-05 13:38:36 +0000 | [diff] [blame] | 1567 | pList = sqlite3DbMallocRawNN(db, sizeof(ExprList) ); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 1568 | if( pList==0 ){ |
danielk1977 | d5d5652 | 2005-03-16 12:15:20 +0000 | [diff] [blame] | 1569 | goto no_mem; |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 1570 | } |
drh | c263f7c | 2016-01-18 13:18:54 +0000 | [diff] [blame] | 1571 | pList->nExpr = 0; |
drh | a19543f | 2017-09-15 15:17:48 +0000 | [diff] [blame] | 1572 | }else if( (pList->nExpr & (pList->nExpr-1))==0 ){ |
drh | 4360617 | 2017-04-05 11:32:13 +0000 | [diff] [blame] | 1573 | ExprList *pNew; |
| 1574 | pNew = sqlite3DbRealloc(db, pList, |
drh | 0aa3231 | 2019-04-13 04:01:12 +0000 | [diff] [blame] | 1575 | sizeof(*pList)+(2*(sqlite3_int64)pList->nExpr-1)*sizeof(pList->a[0])); |
drh | 4360617 | 2017-04-05 11:32:13 +0000 | [diff] [blame] | 1576 | if( pNew==0 ){ |
danielk1977 | d5d5652 | 2005-03-16 12:15:20 +0000 | [diff] [blame] | 1577 | goto no_mem; |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 1578 | } |
drh | 4360617 | 2017-04-05 11:32:13 +0000 | [diff] [blame] | 1579 | pList = pNew; |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 1580 | } |
drh | 4360617 | 2017-04-05 11:32:13 +0000 | [diff] [blame] | 1581 | pItem = &pList->a[pList->nExpr++]; |
drh | a8b9793 | 2017-05-31 02:58:30 +0000 | [diff] [blame] | 1582 | assert( offsetof(struct ExprList_item,zName)==sizeof(pItem->pExpr) ); |
| 1583 | assert( offsetof(struct ExprList_item,pExpr)==0 ); |
| 1584 | memset(&pItem->zName,0,sizeof(*pItem)-offsetof(struct ExprList_item,zName)); |
drh | 4360617 | 2017-04-05 11:32:13 +0000 | [diff] [blame] | 1585 | pItem->pExpr = pExpr; |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 1586 | return pList; |
danielk1977 | d5d5652 | 2005-03-16 12:15:20 +0000 | [diff] [blame] | 1587 | |
| 1588 | no_mem: |
| 1589 | /* Avoid leaking memory if malloc has failed. */ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1590 | sqlite3ExprDelete(db, pExpr); |
| 1591 | sqlite3ExprListDelete(db, pList); |
danielk1977 | d5d5652 | 2005-03-16 12:15:20 +0000 | [diff] [blame] | 1592 | return 0; |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 1593 | } |
| 1594 | |
| 1595 | /* |
drh | 8762ec1 | 2016-08-20 01:06:22 +0000 | [diff] [blame] | 1596 | ** pColumns and pExpr form a vector assignment which is part of the SET |
| 1597 | ** clause of an UPDATE statement. Like this: |
drh | a1251bc | 2016-08-20 00:51:37 +0000 | [diff] [blame] | 1598 | ** |
| 1599 | ** (a,b,c) = (expr1,expr2,expr3) |
| 1600 | ** Or: (a,b,c) = (SELECT x,y,z FROM ....) |
| 1601 | ** |
| 1602 | ** For each term of the vector assignment, append new entries to the |
drh | b67343d | 2017-01-03 11:59:54 +0000 | [diff] [blame] | 1603 | ** expression list pList. In the case of a subquery on the RHS, append |
drh | a1251bc | 2016-08-20 00:51:37 +0000 | [diff] [blame] | 1604 | ** TK_SELECT_COLUMN expressions. |
| 1605 | */ |
| 1606 | ExprList *sqlite3ExprListAppendVector( |
| 1607 | Parse *pParse, /* Parsing context */ |
| 1608 | ExprList *pList, /* List to which to append. Might be NULL */ |
| 1609 | IdList *pColumns, /* List of names of LHS of the assignment */ |
| 1610 | Expr *pExpr /* Vector expression to be appended. Might be NULL */ |
| 1611 | ){ |
| 1612 | sqlite3 *db = pParse->db; |
| 1613 | int n; |
| 1614 | int i; |
drh | 66860af | 2016-08-23 18:30:10 +0000 | [diff] [blame] | 1615 | int iFirst = pList ? pList->nExpr : 0; |
drh | 321e828 | 2016-08-24 17:49:07 +0000 | [diff] [blame] | 1616 | /* pColumns can only be NULL due to an OOM but an OOM will cause an |
| 1617 | ** exit prior to this routine being invoked */ |
| 1618 | if( NEVER(pColumns==0) ) goto vector_append_error; |
drh | a1251bc | 2016-08-20 00:51:37 +0000 | [diff] [blame] | 1619 | if( pExpr==0 ) goto vector_append_error; |
drh | 966e291 | 2017-01-03 02:58:01 +0000 | [diff] [blame] | 1620 | |
| 1621 | /* If the RHS is a vector, then we can immediately check to see that |
| 1622 | ** the size of the RHS and LHS match. But if the RHS is a SELECT, |
| 1623 | ** wildcards ("*") in the result set of the SELECT must be expanded before |
| 1624 | ** we can do the size check, so defer the size check until code generation. |
| 1625 | */ |
| 1626 | if( pExpr->op!=TK_SELECT && pColumns->nId!=(n=sqlite3ExprVectorSize(pExpr)) ){ |
drh | a1251bc | 2016-08-20 00:51:37 +0000 | [diff] [blame] | 1627 | sqlite3ErrorMsg(pParse, "%d columns assigned %d values", |
| 1628 | pColumns->nId, n); |
| 1629 | goto vector_append_error; |
| 1630 | } |
drh | 966e291 | 2017-01-03 02:58:01 +0000 | [diff] [blame] | 1631 | |
| 1632 | for(i=0; i<pColumns->nId; i++){ |
drh | a1251bc | 2016-08-20 00:51:37 +0000 | [diff] [blame] | 1633 | Expr *pSubExpr = sqlite3ExprForVectorField(pParse, pExpr, i); |
drh | 554a9dc | 2019-08-26 14:18:28 +0000 | [diff] [blame] | 1634 | assert( pSubExpr!=0 || db->mallocFailed ); |
| 1635 | assert( pSubExpr==0 || pSubExpr->iTable==0 ); |
| 1636 | if( pSubExpr==0 ) continue; |
| 1637 | pSubExpr->iTable = pColumns->nId; |
drh | a1251bc | 2016-08-20 00:51:37 +0000 | [diff] [blame] | 1638 | pList = sqlite3ExprListAppend(pParse, pList, pSubExpr); |
| 1639 | if( pList ){ |
drh | 66860af | 2016-08-23 18:30:10 +0000 | [diff] [blame] | 1640 | assert( pList->nExpr==iFirst+i+1 ); |
drh | a1251bc | 2016-08-20 00:51:37 +0000 | [diff] [blame] | 1641 | pList->a[pList->nExpr-1].zName = pColumns->a[i].zName; |
| 1642 | pColumns->a[i].zName = 0; |
| 1643 | } |
| 1644 | } |
drh | 966e291 | 2017-01-03 02:58:01 +0000 | [diff] [blame] | 1645 | |
drh | ffe2805 | 2017-05-06 18:09:36 +0000 | [diff] [blame] | 1646 | if( !db->mallocFailed && pExpr->op==TK_SELECT && ALWAYS(pList!=0) ){ |
drh | f4dd26c | 2017-04-05 11:49:06 +0000 | [diff] [blame] | 1647 | Expr *pFirst = pList->a[iFirst].pExpr; |
| 1648 | assert( pFirst!=0 ); |
| 1649 | assert( pFirst->op==TK_SELECT_COLUMN ); |
drh | 966e291 | 2017-01-03 02:58:01 +0000 | [diff] [blame] | 1650 | |
drh | f4dd26c | 2017-04-05 11:49:06 +0000 | [diff] [blame] | 1651 | /* Store the SELECT statement in pRight so it will be deleted when |
| 1652 | ** sqlite3ExprListDelete() is called */ |
| 1653 | pFirst->pRight = pExpr; |
| 1654 | pExpr = 0; |
drh | 966e291 | 2017-01-03 02:58:01 +0000 | [diff] [blame] | 1655 | |
drh | f4dd26c | 2017-04-05 11:49:06 +0000 | [diff] [blame] | 1656 | /* Remember the size of the LHS in iTable so that we can check that |
| 1657 | ** the RHS and LHS sizes match during code generation. */ |
| 1658 | pFirst->iTable = pColumns->nId; |
drh | a1251bc | 2016-08-20 00:51:37 +0000 | [diff] [blame] | 1659 | } |
| 1660 | |
| 1661 | vector_append_error: |
drh | 8e34e40 | 2019-06-11 10:43:56 +0000 | [diff] [blame] | 1662 | sqlite3ExprUnmapAndDelete(pParse, pExpr); |
drh | a1251bc | 2016-08-20 00:51:37 +0000 | [diff] [blame] | 1663 | sqlite3IdListDelete(db, pColumns); |
| 1664 | return pList; |
| 1665 | } |
| 1666 | |
| 1667 | /* |
drh | bc622bc | 2015-08-24 15:39:42 +0000 | [diff] [blame] | 1668 | ** Set the sort order for the last element on the given ExprList. |
| 1669 | */ |
dan | 6e11892 | 2019-08-12 16:36:38 +0000 | [diff] [blame] | 1670 | void sqlite3ExprListSetSortOrder(ExprList *p, int iSortOrder, int eNulls){ |
dan | 9105fd5 | 2019-08-19 17:26:32 +0000 | [diff] [blame] | 1671 | struct ExprList_item *pItem; |
drh | bc622bc | 2015-08-24 15:39:42 +0000 | [diff] [blame] | 1672 | if( p==0 ) return; |
drh | bc622bc | 2015-08-24 15:39:42 +0000 | [diff] [blame] | 1673 | assert( p->nExpr>0 ); |
dan | 6e11892 | 2019-08-12 16:36:38 +0000 | [diff] [blame] | 1674 | |
| 1675 | assert( SQLITE_SO_UNDEFINED<0 && SQLITE_SO_ASC==0 && SQLITE_SO_DESC>0 ); |
| 1676 | assert( iSortOrder==SQLITE_SO_UNDEFINED |
| 1677 | || iSortOrder==SQLITE_SO_ASC |
| 1678 | || iSortOrder==SQLITE_SO_DESC |
| 1679 | ); |
| 1680 | assert( eNulls==SQLITE_SO_UNDEFINED |
| 1681 | || eNulls==SQLITE_SO_ASC |
| 1682 | || eNulls==SQLITE_SO_DESC |
| 1683 | ); |
| 1684 | |
dan | 9105fd5 | 2019-08-19 17:26:32 +0000 | [diff] [blame] | 1685 | pItem = &p->a[p->nExpr-1]; |
| 1686 | assert( pItem->bNulls==0 ); |
| 1687 | if( iSortOrder==SQLITE_SO_UNDEFINED ){ |
| 1688 | iSortOrder = SQLITE_SO_ASC; |
drh | bc622bc | 2015-08-24 15:39:42 +0000 | [diff] [blame] | 1689 | } |
dan | 9105fd5 | 2019-08-19 17:26:32 +0000 | [diff] [blame] | 1690 | pItem->sortFlags = (u8)iSortOrder; |
| 1691 | |
| 1692 | if( eNulls!=SQLITE_SO_UNDEFINED ){ |
| 1693 | pItem->bNulls = 1; |
| 1694 | if( iSortOrder!=eNulls ){ |
| 1695 | pItem->sortFlags |= KEYINFO_ORDER_BIGNULL; |
| 1696 | } |
drh | bc622bc | 2015-08-24 15:39:42 +0000 | [diff] [blame] | 1697 | } |
drh | bc622bc | 2015-08-24 15:39:42 +0000 | [diff] [blame] | 1698 | } |
| 1699 | |
| 1700 | /* |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 1701 | ** Set the ExprList.a[].zName element of the most recently added item |
| 1702 | ** on the expression list. |
| 1703 | ** |
| 1704 | ** pList might be NULL following an OOM error. But pName should never be |
| 1705 | ** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag |
| 1706 | ** is set. |
| 1707 | */ |
| 1708 | void sqlite3ExprListSetName( |
| 1709 | Parse *pParse, /* Parsing context */ |
| 1710 | ExprList *pList, /* List to which to add the span. */ |
| 1711 | Token *pName, /* Name to be added */ |
| 1712 | int dequote /* True to cause the name to be dequoted */ |
| 1713 | ){ |
| 1714 | assert( pList!=0 || pParse->db->mallocFailed!=0 ); |
| 1715 | if( pList ){ |
| 1716 | struct ExprList_item *pItem; |
| 1717 | assert( pList->nExpr>0 ); |
| 1718 | pItem = &pList->a[pList->nExpr-1]; |
| 1719 | assert( pItem->zName==0 ); |
| 1720 | pItem->zName = sqlite3DbStrNDup(pParse->db, pName->z, pName->n); |
drh | 244b9d6 | 2016-04-11 19:01:08 +0000 | [diff] [blame] | 1721 | if( dequote ) sqlite3Dequote(pItem->zName); |
dan | c9461ec | 2018-08-29 21:00:16 +0000 | [diff] [blame] | 1722 | if( IN_RENAME_OBJECT ){ |
dan | 07e9523 | 2018-08-21 16:32:53 +0000 | [diff] [blame] | 1723 | sqlite3RenameTokenMap(pParse, (void*)pItem->zName, pName); |
dan | 5be60c5 | 2018-08-15 20:28:39 +0000 | [diff] [blame] | 1724 | } |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 1725 | } |
| 1726 | } |
| 1727 | |
| 1728 | /* |
| 1729 | ** Set the ExprList.a[].zSpan element of the most recently added item |
| 1730 | ** on the expression list. |
| 1731 | ** |
| 1732 | ** pList might be NULL following an OOM error. But pSpan should never be |
| 1733 | ** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag |
| 1734 | ** is set. |
| 1735 | */ |
| 1736 | void sqlite3ExprListSetSpan( |
| 1737 | Parse *pParse, /* Parsing context */ |
| 1738 | ExprList *pList, /* List to which to add the span. */ |
drh | 1be266b | 2017-12-24 00:18:47 +0000 | [diff] [blame] | 1739 | const char *zStart, /* Start of the span */ |
| 1740 | const char *zEnd /* End of the span */ |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 1741 | ){ |
| 1742 | sqlite3 *db = pParse->db; |
| 1743 | assert( pList!=0 || db->mallocFailed!=0 ); |
| 1744 | if( pList ){ |
| 1745 | struct ExprList_item *pItem = &pList->a[pList->nExpr-1]; |
| 1746 | assert( pList->nExpr>0 ); |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 1747 | sqlite3DbFree(db, pItem->zSpan); |
drh | 9b2e043 | 2017-12-27 19:43:22 +0000 | [diff] [blame] | 1748 | pItem->zSpan = sqlite3DbSpanDup(db, zStart, zEnd); |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 1749 | } |
| 1750 | } |
| 1751 | |
| 1752 | /* |
danielk1977 | 7a15a4b | 2007-05-08 17:54:43 +0000 | [diff] [blame] | 1753 | ** If the expression list pEList contains more than iLimit elements, |
| 1754 | ** leave an error message in pParse. |
| 1755 | */ |
| 1756 | void sqlite3ExprListCheckLength( |
| 1757 | Parse *pParse, |
| 1758 | ExprList *pEList, |
danielk1977 | 7a15a4b | 2007-05-08 17:54:43 +0000 | [diff] [blame] | 1759 | const char *zObject |
| 1760 | ){ |
drh | b1a6c3c | 2008-03-20 16:30:17 +0000 | [diff] [blame] | 1761 | int mx = pParse->db->aLimit[SQLITE_LIMIT_COLUMN]; |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 1762 | testcase( pEList && pEList->nExpr==mx ); |
| 1763 | testcase( pEList && pEList->nExpr==mx+1 ); |
drh | b1a6c3c | 2008-03-20 16:30:17 +0000 | [diff] [blame] | 1764 | if( pEList && pEList->nExpr>mx ){ |
danielk1977 | 7a15a4b | 2007-05-08 17:54:43 +0000 | [diff] [blame] | 1765 | sqlite3ErrorMsg(pParse, "too many columns in %s", zObject); |
| 1766 | } |
| 1767 | } |
| 1768 | |
| 1769 | /* |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 1770 | ** Delete an entire expression list. |
| 1771 | */ |
drh | affa855 | 2016-04-11 18:25:05 +0000 | [diff] [blame] | 1772 | static SQLITE_NOINLINE void exprListDeleteNN(sqlite3 *db, ExprList *pList){ |
drh | ac48b75 | 2017-04-05 11:57:56 +0000 | [diff] [blame] | 1773 | int i = pList->nExpr; |
| 1774 | struct ExprList_item *pItem = pList->a; |
| 1775 | assert( pList->nExpr>0 ); |
| 1776 | do{ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1777 | sqlite3ExprDelete(db, pItem->pExpr); |
| 1778 | sqlite3DbFree(db, pItem->zName); |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 1779 | sqlite3DbFree(db, pItem->zSpan); |
drh | ac48b75 | 2017-04-05 11:57:56 +0000 | [diff] [blame] | 1780 | pItem++; |
| 1781 | }while( --i>0 ); |
drh | dbd6a7d | 2017-04-05 12:39:49 +0000 | [diff] [blame] | 1782 | sqlite3DbFreeNN(db, pList); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 1783 | } |
drh | affa855 | 2016-04-11 18:25:05 +0000 | [diff] [blame] | 1784 | void sqlite3ExprListDelete(sqlite3 *db, ExprList *pList){ |
| 1785 | if( pList ) exprListDeleteNN(db, pList); |
| 1786 | } |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 1787 | |
| 1788 | /* |
drh | 2308ed3 | 2015-02-09 16:09:34 +0000 | [diff] [blame] | 1789 | ** Return the bitwise-OR of all Expr.flags fields in the given |
| 1790 | ** ExprList. |
drh | 885a5b0 | 2015-02-09 15:21:36 +0000 | [diff] [blame] | 1791 | */ |
drh | 2308ed3 | 2015-02-09 16:09:34 +0000 | [diff] [blame] | 1792 | u32 sqlite3ExprListFlags(const ExprList *pList){ |
drh | 885a5b0 | 2015-02-09 15:21:36 +0000 | [diff] [blame] | 1793 | int i; |
drh | 2308ed3 | 2015-02-09 16:09:34 +0000 | [diff] [blame] | 1794 | u32 m = 0; |
drh | 508e2d0 | 2017-09-30 01:25:04 +0000 | [diff] [blame] | 1795 | assert( pList!=0 ); |
| 1796 | for(i=0; i<pList->nExpr; i++){ |
| 1797 | Expr *pExpr = pList->a[i].pExpr; |
| 1798 | assert( pExpr!=0 ); |
| 1799 | m |= pExpr->flags; |
drh | 885a5b0 | 2015-02-09 15:21:36 +0000 | [diff] [blame] | 1800 | } |
drh | 2308ed3 | 2015-02-09 16:09:34 +0000 | [diff] [blame] | 1801 | return m; |
drh | 885a5b0 | 2015-02-09 15:21:36 +0000 | [diff] [blame] | 1802 | } |
| 1803 | |
| 1804 | /* |
drh | 7e6f980 | 2017-09-04 00:33:04 +0000 | [diff] [blame] | 1805 | ** This is a SELECT-node callback for the expression walker that |
| 1806 | ** always "fails". By "fail" in this case, we mean set |
| 1807 | ** pWalker->eCode to zero and abort. |
| 1808 | ** |
| 1809 | ** This callback is used by multiple expression walkers. |
| 1810 | */ |
| 1811 | int sqlite3SelectWalkFail(Walker *pWalker, Select *NotUsed){ |
| 1812 | UNUSED_PARAMETER(NotUsed); |
| 1813 | pWalker->eCode = 0; |
| 1814 | return WRC_Abort; |
| 1815 | } |
| 1816 | |
| 1817 | /* |
drh | 171d16b | 2018-02-26 20:15:54 +0000 | [diff] [blame] | 1818 | ** If the input expression is an ID with the name "true" or "false" |
drh | 96acafb | 2018-02-27 14:49:25 +0000 | [diff] [blame] | 1819 | ** then convert it into an TK_TRUEFALSE term. Return non-zero if |
| 1820 | ** the conversion happened, and zero if the expression is unaltered. |
drh | 171d16b | 2018-02-26 20:15:54 +0000 | [diff] [blame] | 1821 | */ |
| 1822 | int sqlite3ExprIdToTrueFalse(Expr *pExpr){ |
| 1823 | assert( pExpr->op==TK_ID || pExpr->op==TK_STRING ); |
drh | 51d35b0 | 2019-01-11 13:32:23 +0000 | [diff] [blame] | 1824 | if( !ExprHasProperty(pExpr, EP_Quoted) |
| 1825 | && (sqlite3StrICmp(pExpr->u.zToken, "true")==0 |
| 1826 | || sqlite3StrICmp(pExpr->u.zToken, "false")==0) |
drh | 171d16b | 2018-02-26 20:15:54 +0000 | [diff] [blame] | 1827 | ){ |
| 1828 | pExpr->op = TK_TRUEFALSE; |
drh | ad31727 | 2019-04-19 16:21:51 +0000 | [diff] [blame] | 1829 | ExprSetProperty(pExpr, pExpr->u.zToken[4]==0 ? EP_IsTrue : EP_IsFalse); |
drh | 171d16b | 2018-02-26 20:15:54 +0000 | [diff] [blame] | 1830 | return 1; |
| 1831 | } |
| 1832 | return 0; |
| 1833 | } |
| 1834 | |
drh | 43c4ac8 | 2018-02-26 21:26:27 +0000 | [diff] [blame] | 1835 | /* |
drh | 96acafb | 2018-02-27 14:49:25 +0000 | [diff] [blame] | 1836 | ** The argument must be a TK_TRUEFALSE Expr node. Return 1 if it is TRUE |
drh | 43c4ac8 | 2018-02-26 21:26:27 +0000 | [diff] [blame] | 1837 | ** and 0 if it is FALSE. |
| 1838 | */ |
drh | 96acafb | 2018-02-27 14:49:25 +0000 | [diff] [blame] | 1839 | int sqlite3ExprTruthValue(const Expr *pExpr){ |
dan | 6ece353 | 2019-06-12 13:49:32 +0000 | [diff] [blame] | 1840 | pExpr = sqlite3ExprSkipCollate((Expr*)pExpr); |
drh | 43c4ac8 | 2018-02-26 21:26:27 +0000 | [diff] [blame] | 1841 | assert( pExpr->op==TK_TRUEFALSE ); |
| 1842 | assert( sqlite3StrICmp(pExpr->u.zToken,"true")==0 |
| 1843 | || sqlite3StrICmp(pExpr->u.zToken,"false")==0 ); |
| 1844 | return pExpr->u.zToken[4]==0; |
| 1845 | } |
| 1846 | |
drh | 17180fc | 2019-04-19 17:26:19 +0000 | [diff] [blame] | 1847 | /* |
| 1848 | ** If pExpr is an AND or OR expression, try to simplify it by eliminating |
| 1849 | ** terms that are always true or false. Return the simplified expression. |
| 1850 | ** Or return the original expression if no simplification is possible. |
| 1851 | ** |
| 1852 | ** Examples: |
| 1853 | ** |
| 1854 | ** (x<10) AND true => (x<10) |
| 1855 | ** (x<10) AND false => false |
| 1856 | ** (x<10) AND (y=22 OR false) => (x<10) AND (y=22) |
| 1857 | ** (x<10) AND (y=22 OR true) => (x<10) |
| 1858 | ** (y=22) OR true => true |
| 1859 | */ |
| 1860 | Expr *sqlite3ExprSimplifiedAndOr(Expr *pExpr){ |
| 1861 | assert( pExpr!=0 ); |
| 1862 | if( pExpr->op==TK_AND || pExpr->op==TK_OR ){ |
| 1863 | Expr *pRight = sqlite3ExprSimplifiedAndOr(pExpr->pRight); |
| 1864 | Expr *pLeft = sqlite3ExprSimplifiedAndOr(pExpr->pLeft); |
| 1865 | if( ExprAlwaysTrue(pLeft) || ExprAlwaysFalse(pRight) ){ |
| 1866 | pExpr = pExpr->op==TK_AND ? pRight : pLeft; |
| 1867 | }else if( ExprAlwaysTrue(pRight) || ExprAlwaysFalse(pLeft) ){ |
| 1868 | pExpr = pExpr->op==TK_AND ? pLeft : pRight; |
| 1869 | } |
| 1870 | } |
| 1871 | return pExpr; |
| 1872 | } |
| 1873 | |
drh | 171d16b | 2018-02-26 20:15:54 +0000 | [diff] [blame] | 1874 | |
| 1875 | /* |
drh | 059b2d5 | 2014-10-24 19:28:09 +0000 | [diff] [blame] | 1876 | ** These routines are Walker callbacks used to check expressions to |
| 1877 | ** see if they are "constant" for some definition of constant. The |
| 1878 | ** Walker.eCode value determines the type of "constant" we are looking |
| 1879 | ** for. |
drh | 73b211a | 2005-01-18 04:00:42 +0000 | [diff] [blame] | 1880 | ** |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 1881 | ** These callback routines are used to implement the following: |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 1882 | ** |
drh | 059b2d5 | 2014-10-24 19:28:09 +0000 | [diff] [blame] | 1883 | ** sqlite3ExprIsConstant() pWalker->eCode==1 |
| 1884 | ** sqlite3ExprIsConstantNotJoin() pWalker->eCode==2 |
drh | fcb9f4f | 2015-06-01 18:13:16 +0000 | [diff] [blame] | 1885 | ** sqlite3ExprIsTableConstant() pWalker->eCode==3 |
drh | 059b2d5 | 2014-10-24 19:28:09 +0000 | [diff] [blame] | 1886 | ** sqlite3ExprIsConstantOrFunction() pWalker->eCode==4 or 5 |
drh | 87abf5c | 2005-08-25 12:45:04 +0000 | [diff] [blame] | 1887 | ** |
drh | 059b2d5 | 2014-10-24 19:28:09 +0000 | [diff] [blame] | 1888 | ** In all cases, the callbacks set Walker.eCode=0 and abort if the expression |
| 1889 | ** is found to not be a constant. |
drh | 87abf5c | 2005-08-25 12:45:04 +0000 | [diff] [blame] | 1890 | ** |
drh | feada2d | 2014-09-24 13:20:22 +0000 | [diff] [blame] | 1891 | ** The sqlite3ExprIsConstantOrFunction() is used for evaluating expressions |
drh | 059b2d5 | 2014-10-24 19:28:09 +0000 | [diff] [blame] | 1892 | ** in a CREATE TABLE statement. The Walker.eCode value is 5 when parsing |
| 1893 | ** an existing schema and 4 when processing a new statement. A bound |
drh | feada2d | 2014-09-24 13:20:22 +0000 | [diff] [blame] | 1894 | ** parameter raises an error for new statements, but is silently converted |
| 1895 | ** to NULL for existing schemas. This allows sqlite_master tables that |
| 1896 | ** contain a bound parameter because they were generated by older versions |
| 1897 | ** of SQLite to be parsed by newer versions of SQLite without raising a |
| 1898 | ** malformed schema error. |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 1899 | */ |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 1900 | static int exprNodeIsConstant(Walker *pWalker, Expr *pExpr){ |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 1901 | |
drh | 059b2d5 | 2014-10-24 19:28:09 +0000 | [diff] [blame] | 1902 | /* If pWalker->eCode is 2 then any term of the expression that comes from |
| 1903 | ** the ON or USING clauses of a left join disqualifies the expression |
drh | 0a16837 | 2007-06-08 00:20:47 +0000 | [diff] [blame] | 1904 | ** from being considered constant. */ |
drh | 059b2d5 | 2014-10-24 19:28:09 +0000 | [diff] [blame] | 1905 | if( pWalker->eCode==2 && ExprHasProperty(pExpr, EP_FromJoin) ){ |
| 1906 | pWalker->eCode = 0; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 1907 | return WRC_Abort; |
drh | 0a16837 | 2007-06-08 00:20:47 +0000 | [diff] [blame] | 1908 | } |
| 1909 | |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 1910 | switch( pExpr->op ){ |
drh | eb55bd2 | 2005-06-30 17:04:21 +0000 | [diff] [blame] | 1911 | /* Consider functions to be constant if all their arguments are constant |
drh | 059b2d5 | 2014-10-24 19:28:09 +0000 | [diff] [blame] | 1912 | ** and either pWalker->eCode==4 or 5 or the function has the |
| 1913 | ** SQLITE_FUNC_CONST flag. */ |
drh | eb55bd2 | 2005-06-30 17:04:21 +0000 | [diff] [blame] | 1914 | case TK_FUNCTION: |
drh | 63f8457 | 2015-02-09 14:07:07 +0000 | [diff] [blame] | 1915 | if( pWalker->eCode>=4 || ExprHasProperty(pExpr,EP_ConstFunc) ){ |
drh | b1fba28 | 2013-11-21 14:33:48 +0000 | [diff] [blame] | 1916 | return WRC_Continue; |
drh | 059b2d5 | 2014-10-24 19:28:09 +0000 | [diff] [blame] | 1917 | }else{ |
| 1918 | pWalker->eCode = 0; |
| 1919 | return WRC_Abort; |
drh | b1fba28 | 2013-11-21 14:33:48 +0000 | [diff] [blame] | 1920 | } |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 1921 | case TK_ID: |
drh | 171d16b | 2018-02-26 20:15:54 +0000 | [diff] [blame] | 1922 | /* Convert "true" or "false" in a DEFAULT clause into the |
| 1923 | ** appropriate TK_TRUEFALSE operator */ |
drh | e39ef31 | 2018-02-27 00:58:13 +0000 | [diff] [blame] | 1924 | if( sqlite3ExprIdToTrueFalse(pExpr) ){ |
drh | 171d16b | 2018-02-26 20:15:54 +0000 | [diff] [blame] | 1925 | return WRC_Prune; |
| 1926 | } |
| 1927 | /* Fall thru */ |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 1928 | case TK_COLUMN: |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 1929 | case TK_AGG_FUNCTION: |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 1930 | case TK_AGG_COLUMN: |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 1931 | testcase( pExpr->op==TK_ID ); |
| 1932 | testcase( pExpr->op==TK_COLUMN ); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 1933 | testcase( pExpr->op==TK_AGG_FUNCTION ); |
| 1934 | testcase( pExpr->op==TK_AGG_COLUMN ); |
drh | 07aded6 | 2018-07-28 16:24:08 +0000 | [diff] [blame] | 1935 | if( ExprHasProperty(pExpr, EP_FixedCol) && pWalker->eCode!=2 ){ |
drh | efad2e2 | 2018-07-27 16:57:11 +0000 | [diff] [blame] | 1936 | return WRC_Continue; |
| 1937 | } |
drh | 059b2d5 | 2014-10-24 19:28:09 +0000 | [diff] [blame] | 1938 | if( pWalker->eCode==3 && pExpr->iTable==pWalker->u.iCur ){ |
| 1939 | return WRC_Continue; |
drh | 059b2d5 | 2014-10-24 19:28:09 +0000 | [diff] [blame] | 1940 | } |
drh | f43ce0b | 2017-05-25 00:08:48 +0000 | [diff] [blame] | 1941 | /* Fall through */ |
| 1942 | case TK_IF_NULL_ROW: |
drh | 6e341b9 | 2018-04-17 18:50:40 +0000 | [diff] [blame] | 1943 | case TK_REGISTER: |
drh | 9916048 | 2018-04-18 01:34:39 +0000 | [diff] [blame] | 1944 | testcase( pExpr->op==TK_REGISTER ); |
drh | f43ce0b | 2017-05-25 00:08:48 +0000 | [diff] [blame] | 1945 | testcase( pExpr->op==TK_IF_NULL_ROW ); |
| 1946 | pWalker->eCode = 0; |
| 1947 | return WRC_Abort; |
drh | feada2d | 2014-09-24 13:20:22 +0000 | [diff] [blame] | 1948 | case TK_VARIABLE: |
drh | 059b2d5 | 2014-10-24 19:28:09 +0000 | [diff] [blame] | 1949 | if( pWalker->eCode==5 ){ |
drh | feada2d | 2014-09-24 13:20:22 +0000 | [diff] [blame] | 1950 | /* Silently convert bound parameters that appear inside of CREATE |
| 1951 | ** statements into a NULL when parsing the CREATE statement text out |
| 1952 | ** of the sqlite_master table */ |
| 1953 | pExpr->op = TK_NULL; |
drh | 059b2d5 | 2014-10-24 19:28:09 +0000 | [diff] [blame] | 1954 | }else if( pWalker->eCode==4 ){ |
drh | feada2d | 2014-09-24 13:20:22 +0000 | [diff] [blame] | 1955 | /* A bound parameter in a CREATE statement that originates from |
| 1956 | ** sqlite3_prepare() causes an error */ |
drh | 059b2d5 | 2014-10-24 19:28:09 +0000 | [diff] [blame] | 1957 | pWalker->eCode = 0; |
drh | feada2d | 2014-09-24 13:20:22 +0000 | [diff] [blame] | 1958 | return WRC_Abort; |
| 1959 | } |
| 1960 | /* Fall through */ |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 1961 | default: |
drh | 6e341b9 | 2018-04-17 18:50:40 +0000 | [diff] [blame] | 1962 | testcase( pExpr->op==TK_SELECT ); /* sqlite3SelectWalkFail() disallows */ |
| 1963 | testcase( pExpr->op==TK_EXISTS ); /* sqlite3SelectWalkFail() disallows */ |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 1964 | return WRC_Continue; |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 1965 | } |
| 1966 | } |
drh | 059b2d5 | 2014-10-24 19:28:09 +0000 | [diff] [blame] | 1967 | static int exprIsConst(Expr *p, int initFlag, int iCur){ |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 1968 | Walker w; |
drh | 059b2d5 | 2014-10-24 19:28:09 +0000 | [diff] [blame] | 1969 | w.eCode = initFlag; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 1970 | w.xExprCallback = exprNodeIsConstant; |
drh | 7e6f980 | 2017-09-04 00:33:04 +0000 | [diff] [blame] | 1971 | w.xSelectCallback = sqlite3SelectWalkFail; |
drh | 979dd1b | 2017-05-29 14:26:07 +0000 | [diff] [blame] | 1972 | #ifdef SQLITE_DEBUG |
| 1973 | w.xSelectCallback2 = sqlite3SelectWalkAssert2; |
| 1974 | #endif |
drh | 059b2d5 | 2014-10-24 19:28:09 +0000 | [diff] [blame] | 1975 | w.u.iCur = iCur; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 1976 | sqlite3WalkExpr(&w, p); |
drh | 059b2d5 | 2014-10-24 19:28:09 +0000 | [diff] [blame] | 1977 | return w.eCode; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 1978 | } |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 1979 | |
| 1980 | /* |
drh | 059b2d5 | 2014-10-24 19:28:09 +0000 | [diff] [blame] | 1981 | ** Walk an expression tree. Return non-zero if the expression is constant |
drh | eb55bd2 | 2005-06-30 17:04:21 +0000 | [diff] [blame] | 1982 | ** and 0 if it involves variables or function calls. |
drh | 2398937 | 2002-05-21 13:43:04 +0000 | [diff] [blame] | 1983 | ** |
| 1984 | ** For the purposes of this function, a double-quoted string (ex: "abc") |
| 1985 | ** is considered a variable but a single-quoted string (ex: 'abc') is |
| 1986 | ** a constant. |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1987 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1988 | int sqlite3ExprIsConstant(Expr *p){ |
drh | 059b2d5 | 2014-10-24 19:28:09 +0000 | [diff] [blame] | 1989 | return exprIsConst(p, 1, 0); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1990 | } |
| 1991 | |
| 1992 | /* |
drh | 07aded6 | 2018-07-28 16:24:08 +0000 | [diff] [blame] | 1993 | ** Walk an expression tree. Return non-zero if |
| 1994 | ** |
| 1995 | ** (1) the expression is constant, and |
| 1996 | ** (2) the expression does originate in the ON or USING clause |
| 1997 | ** of a LEFT JOIN, and |
| 1998 | ** (3) the expression does not contain any EP_FixedCol TK_COLUMN |
| 1999 | ** operands created by the constant propagation optimization. |
| 2000 | ** |
| 2001 | ** When this routine returns true, it indicates that the expression |
| 2002 | ** can be added to the pParse->pConstExpr list and evaluated once when |
| 2003 | ** the prepared statement starts up. See sqlite3ExprCodeAtInit(). |
drh | 0a16837 | 2007-06-08 00:20:47 +0000 | [diff] [blame] | 2004 | */ |
| 2005 | int sqlite3ExprIsConstantNotJoin(Expr *p){ |
drh | 059b2d5 | 2014-10-24 19:28:09 +0000 | [diff] [blame] | 2006 | return exprIsConst(p, 2, 0); |
drh | 0a16837 | 2007-06-08 00:20:47 +0000 | [diff] [blame] | 2007 | } |
| 2008 | |
| 2009 | /* |
drh | fcb9f4f | 2015-06-01 18:13:16 +0000 | [diff] [blame] | 2010 | ** Walk an expression tree. Return non-zero if the expression is constant |
drh | 059b2d5 | 2014-10-24 19:28:09 +0000 | [diff] [blame] | 2011 | ** for any single row of the table with cursor iCur. In other words, the |
| 2012 | ** expression must not refer to any non-deterministic function nor any |
| 2013 | ** table other than iCur. |
| 2014 | */ |
| 2015 | int sqlite3ExprIsTableConstant(Expr *p, int iCur){ |
| 2016 | return exprIsConst(p, 3, iCur); |
| 2017 | } |
| 2018 | |
dan | ab31a84 | 2017-04-29 20:53:09 +0000 | [diff] [blame] | 2019 | |
| 2020 | /* |
| 2021 | ** sqlite3WalkExpr() callback used by sqlite3ExprIsConstantOrGroupBy(). |
| 2022 | */ |
| 2023 | static int exprNodeIsConstantOrGroupBy(Walker *pWalker, Expr *pExpr){ |
| 2024 | ExprList *pGroupBy = pWalker->u.pGroupBy; |
| 2025 | int i; |
| 2026 | |
| 2027 | /* Check if pExpr is identical to any GROUP BY term. If so, consider |
| 2028 | ** it constant. */ |
| 2029 | for(i=0; i<pGroupBy->nExpr; i++){ |
| 2030 | Expr *p = pGroupBy->a[i].pExpr; |
dan | 5aa550c | 2017-06-24 18:10:29 +0000 | [diff] [blame] | 2031 | if( sqlite3ExprCompare(0, pExpr, p, -1)<2 ){ |
drh | 70efa84 | 2017-09-28 01:58:23 +0000 | [diff] [blame] | 2032 | CollSeq *pColl = sqlite3ExprNNCollSeq(pWalker->pParse, p); |
drh | efad2e2 | 2018-07-27 16:57:11 +0000 | [diff] [blame] | 2033 | if( sqlite3IsBinary(pColl) ){ |
dan | ab31a84 | 2017-04-29 20:53:09 +0000 | [diff] [blame] | 2034 | return WRC_Prune; |
| 2035 | } |
| 2036 | } |
| 2037 | } |
| 2038 | |
| 2039 | /* Check if pExpr is a sub-select. If so, consider it variable. */ |
| 2040 | if( ExprHasProperty(pExpr, EP_xIsSelect) ){ |
| 2041 | pWalker->eCode = 0; |
| 2042 | return WRC_Abort; |
| 2043 | } |
| 2044 | |
| 2045 | return exprNodeIsConstant(pWalker, pExpr); |
| 2046 | } |
| 2047 | |
| 2048 | /* |
| 2049 | ** Walk the expression tree passed as the first argument. Return non-zero |
| 2050 | ** if the expression consists entirely of constants or copies of terms |
| 2051 | ** in pGroupBy that sort with the BINARY collation sequence. |
drh | ab31400 | 2017-05-02 16:46:41 +0000 | [diff] [blame] | 2052 | ** |
| 2053 | ** This routine is used to determine if a term of the HAVING clause can |
| 2054 | ** be promoted into the WHERE clause. In order for such a promotion to work, |
| 2055 | ** the value of the HAVING clause term must be the same for all members of |
| 2056 | ** a "group". The requirement that the GROUP BY term must be BINARY |
| 2057 | ** assumes that no other collating sequence will have a finer-grained |
| 2058 | ** grouping than binary. In other words (A=B COLLATE binary) implies |
| 2059 | ** A=B in every other collating sequence. The requirement that the |
| 2060 | ** GROUP BY be BINARY is stricter than necessary. It would also work |
| 2061 | ** to promote HAVING clauses that use the same alternative collating |
| 2062 | ** sequence as the GROUP BY term, but that is much harder to check, |
| 2063 | ** alternative collating sequences are uncommon, and this is only an |
| 2064 | ** optimization, so we take the easy way out and simply require the |
| 2065 | ** GROUP BY to use the BINARY collating sequence. |
dan | ab31a84 | 2017-04-29 20:53:09 +0000 | [diff] [blame] | 2066 | */ |
| 2067 | int sqlite3ExprIsConstantOrGroupBy(Parse *pParse, Expr *p, ExprList *pGroupBy){ |
| 2068 | Walker w; |
dan | ab31a84 | 2017-04-29 20:53:09 +0000 | [diff] [blame] | 2069 | w.eCode = 1; |
| 2070 | w.xExprCallback = exprNodeIsConstantOrGroupBy; |
drh | 979dd1b | 2017-05-29 14:26:07 +0000 | [diff] [blame] | 2071 | w.xSelectCallback = 0; |
dan | ab31a84 | 2017-04-29 20:53:09 +0000 | [diff] [blame] | 2072 | w.u.pGroupBy = pGroupBy; |
| 2073 | w.pParse = pParse; |
| 2074 | sqlite3WalkExpr(&w, p); |
| 2075 | return w.eCode; |
| 2076 | } |
| 2077 | |
drh | 059b2d5 | 2014-10-24 19:28:09 +0000 | [diff] [blame] | 2078 | /* |
| 2079 | ** Walk an expression tree. Return non-zero if the expression is constant |
drh | eb55bd2 | 2005-06-30 17:04:21 +0000 | [diff] [blame] | 2080 | ** or a function call with constant arguments. Return and 0 if there |
| 2081 | ** are any variables. |
| 2082 | ** |
| 2083 | ** For the purposes of this function, a double-quoted string (ex: "abc") |
| 2084 | ** is considered a variable but a single-quoted string (ex: 'abc') is |
| 2085 | ** a constant. |
| 2086 | */ |
drh | feada2d | 2014-09-24 13:20:22 +0000 | [diff] [blame] | 2087 | int sqlite3ExprIsConstantOrFunction(Expr *p, u8 isInit){ |
| 2088 | assert( isInit==0 || isInit==1 ); |
drh | 059b2d5 | 2014-10-24 19:28:09 +0000 | [diff] [blame] | 2089 | return exprIsConst(p, 4+isInit, 0); |
drh | eb55bd2 | 2005-06-30 17:04:21 +0000 | [diff] [blame] | 2090 | } |
| 2091 | |
drh | 5b88bc4 | 2013-12-07 23:35:21 +0000 | [diff] [blame] | 2092 | #ifdef SQLITE_ENABLE_CURSOR_HINTS |
| 2093 | /* |
| 2094 | ** Walk an expression tree. Return 1 if the expression contains a |
| 2095 | ** subquery of some kind. Return 0 if there are no subqueries. |
| 2096 | */ |
| 2097 | int sqlite3ExprContainsSubquery(Expr *p){ |
| 2098 | Walker w; |
drh | bec2476 | 2015-08-13 20:07:13 +0000 | [diff] [blame] | 2099 | w.eCode = 1; |
drh | 5b88bc4 | 2013-12-07 23:35:21 +0000 | [diff] [blame] | 2100 | w.xExprCallback = sqlite3ExprWalkNoop; |
drh | 7e6f980 | 2017-09-04 00:33:04 +0000 | [diff] [blame] | 2101 | w.xSelectCallback = sqlite3SelectWalkFail; |
drh | 979dd1b | 2017-05-29 14:26:07 +0000 | [diff] [blame] | 2102 | #ifdef SQLITE_DEBUG |
| 2103 | w.xSelectCallback2 = sqlite3SelectWalkAssert2; |
| 2104 | #endif |
drh | 5b88bc4 | 2013-12-07 23:35:21 +0000 | [diff] [blame] | 2105 | sqlite3WalkExpr(&w, p); |
drh | 07194bf | 2015-08-13 20:34:41 +0000 | [diff] [blame] | 2106 | return w.eCode==0; |
drh | 5b88bc4 | 2013-12-07 23:35:21 +0000 | [diff] [blame] | 2107 | } |
| 2108 | #endif |
| 2109 | |
drh | eb55bd2 | 2005-06-30 17:04:21 +0000 | [diff] [blame] | 2110 | /* |
drh | 73b211a | 2005-01-18 04:00:42 +0000 | [diff] [blame] | 2111 | ** If the expression p codes a constant integer that is small enough |
drh | 202b2df | 2004-01-06 01:13:46 +0000 | [diff] [blame] | 2112 | ** to fit in a 32-bit integer, return 1 and put the value of the integer |
| 2113 | ** in *pValue. If the expression is not an integer or if it is too big |
| 2114 | ** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged. |
drh | e4de1fe | 2002-06-02 16:09:01 +0000 | [diff] [blame] | 2115 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2116 | int sqlite3ExprIsInteger(Expr *p, int *pValue){ |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 2117 | int rc = 0; |
drh | 1d2d71a | 2019-04-19 23:05:56 +0000 | [diff] [blame] | 2118 | if( NEVER(p==0) ) return 0; /* Used to only happen following on OOM */ |
drh | cd92e84 | 2011-02-17 15:58:20 +0000 | [diff] [blame] | 2119 | |
| 2120 | /* If an expression is an integer literal that fits in a signed 32-bit |
| 2121 | ** integer, then the EP_IntValue flag will have already been set */ |
| 2122 | assert( p->op!=TK_INTEGER || (p->flags & EP_IntValue)!=0 |
| 2123 | || sqlite3GetInt32(p->u.zToken, &rc)==0 ); |
| 2124 | |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 2125 | if( p->flags & EP_IntValue ){ |
drh | 33e619f | 2009-05-28 01:00:55 +0000 | [diff] [blame] | 2126 | *pValue = p->u.iValue; |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 2127 | return 1; |
| 2128 | } |
drh | e4de1fe | 2002-06-02 16:09:01 +0000 | [diff] [blame] | 2129 | switch( p->op ){ |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 2130 | case TK_UPLUS: { |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 2131 | rc = sqlite3ExprIsInteger(p->pLeft, pValue); |
drh | f6e369a | 2008-06-24 12:46:30 +0000 | [diff] [blame] | 2132 | break; |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 2133 | } |
drh | e4de1fe | 2002-06-02 16:09:01 +0000 | [diff] [blame] | 2134 | case TK_UMINUS: { |
| 2135 | int v; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2136 | if( sqlite3ExprIsInteger(p->pLeft, &v) ){ |
mistachkin | f641889 | 2013-08-28 01:54:12 +0000 | [diff] [blame] | 2137 | assert( v!=(-2147483647-1) ); |
drh | e4de1fe | 2002-06-02 16:09:01 +0000 | [diff] [blame] | 2138 | *pValue = -v; |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 2139 | rc = 1; |
drh | e4de1fe | 2002-06-02 16:09:01 +0000 | [diff] [blame] | 2140 | } |
| 2141 | break; |
| 2142 | } |
| 2143 | default: break; |
| 2144 | } |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 2145 | return rc; |
drh | e4de1fe | 2002-06-02 16:09:01 +0000 | [diff] [blame] | 2146 | } |
| 2147 | |
| 2148 | /* |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2149 | ** Return FALSE if there is no chance that the expression can be NULL. |
| 2150 | ** |
| 2151 | ** If the expression might be NULL or if the expression is too complex |
| 2152 | ** to tell return TRUE. |
| 2153 | ** |
| 2154 | ** This routine is used as an optimization, to skip OP_IsNull opcodes |
| 2155 | ** when we know that a value cannot be NULL. Hence, a false positive |
| 2156 | ** (returning TRUE when in fact the expression can never be NULL) might |
| 2157 | ** be a small performance hit but is otherwise harmless. On the other |
| 2158 | ** hand, a false negative (returning FALSE when the result could be NULL) |
| 2159 | ** will likely result in an incorrect answer. So when in doubt, return |
| 2160 | ** TRUE. |
| 2161 | */ |
| 2162 | int sqlite3ExprCanBeNull(const Expr *p){ |
| 2163 | u8 op; |
drh | 9bfb079 | 2018-12-22 01:13:25 +0000 | [diff] [blame] | 2164 | while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ |
| 2165 | p = p->pLeft; |
| 2166 | } |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2167 | op = p->op; |
| 2168 | if( op==TK_REGISTER ) op = p->op2; |
| 2169 | switch( op ){ |
| 2170 | case TK_INTEGER: |
| 2171 | case TK_STRING: |
| 2172 | case TK_FLOAT: |
| 2173 | case TK_BLOB: |
| 2174 | return 0; |
drh | 7248a8b | 2014-08-04 18:50:54 +0000 | [diff] [blame] | 2175 | case TK_COLUMN: |
drh | 72673a2 | 2014-12-04 16:27:17 +0000 | [diff] [blame] | 2176 | return ExprHasProperty(p, EP_CanBeNull) || |
drh | eda079c | 2018-09-20 19:02:15 +0000 | [diff] [blame] | 2177 | p->y.pTab==0 || /* Reference to column of index on expression */ |
| 2178 | (p->iColumn>=0 && p->y.pTab->aCol[p->iColumn].notNull==0); |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2179 | default: |
| 2180 | return 1; |
| 2181 | } |
| 2182 | } |
| 2183 | |
| 2184 | /* |
| 2185 | ** Return TRUE if the given expression is a constant which would be |
| 2186 | ** unchanged by OP_Affinity with the affinity given in the second |
| 2187 | ** argument. |
| 2188 | ** |
| 2189 | ** This routine is used to determine if the OP_Affinity operation |
| 2190 | ** can be omitted. When in doubt return FALSE. A false negative |
| 2191 | ** is harmless. A false positive, however, can result in the wrong |
| 2192 | ** answer. |
| 2193 | */ |
| 2194 | int sqlite3ExprNeedsNoAffinityChange(const Expr *p, char aff){ |
| 2195 | u8 op; |
drh | af86640 | 2019-08-22 11:11:28 +0000 | [diff] [blame] | 2196 | int unaryMinus = 0; |
drh | 05883a3 | 2015-06-02 15:32:08 +0000 | [diff] [blame] | 2197 | if( aff==SQLITE_AFF_BLOB ) return 1; |
drh | af86640 | 2019-08-22 11:11:28 +0000 | [diff] [blame] | 2198 | while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ |
| 2199 | if( p->op==TK_UMINUS ) unaryMinus = 1; |
| 2200 | p = p->pLeft; |
| 2201 | } |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2202 | op = p->op; |
| 2203 | if( op==TK_REGISTER ) op = p->op2; |
| 2204 | switch( op ){ |
| 2205 | case TK_INTEGER: { |
| 2206 | return aff==SQLITE_AFF_INTEGER || aff==SQLITE_AFF_NUMERIC; |
| 2207 | } |
| 2208 | case TK_FLOAT: { |
| 2209 | return aff==SQLITE_AFF_REAL || aff==SQLITE_AFF_NUMERIC; |
| 2210 | } |
| 2211 | case TK_STRING: { |
drh | af86640 | 2019-08-22 11:11:28 +0000 | [diff] [blame] | 2212 | return !unaryMinus && aff==SQLITE_AFF_TEXT; |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2213 | } |
| 2214 | case TK_BLOB: { |
drh | af86640 | 2019-08-22 11:11:28 +0000 | [diff] [blame] | 2215 | return !unaryMinus; |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2216 | } |
drh | 2f2855b | 2009-11-18 01:25:26 +0000 | [diff] [blame] | 2217 | case TK_COLUMN: { |
drh | 88376ca | 2009-11-19 15:44:53 +0000 | [diff] [blame] | 2218 | assert( p->iTable>=0 ); /* p cannot be part of a CHECK constraint */ |
| 2219 | return p->iColumn<0 |
| 2220 | && (aff==SQLITE_AFF_INTEGER || aff==SQLITE_AFF_NUMERIC); |
drh | 2f2855b | 2009-11-18 01:25:26 +0000 | [diff] [blame] | 2221 | } |
drh | 039fc32 | 2009-11-17 18:31:47 +0000 | [diff] [blame] | 2222 | default: { |
| 2223 | return 0; |
| 2224 | } |
| 2225 | } |
| 2226 | } |
| 2227 | |
| 2228 | /* |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 2229 | ** Return TRUE if the given string is a row-id column name. |
| 2230 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2231 | int sqlite3IsRowid(const char *z){ |
| 2232 | if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1; |
| 2233 | if( sqlite3StrICmp(z, "ROWID")==0 ) return 1; |
| 2234 | if( sqlite3StrICmp(z, "OID")==0 ) return 1; |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 2235 | return 0; |
| 2236 | } |
| 2237 | |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 2238 | /* |
drh | 69c355b | 2016-03-09 15:34:51 +0000 | [diff] [blame] | 2239 | ** pX is the RHS of an IN operator. If pX is a SELECT statement |
| 2240 | ** that can be simplified to a direct table access, then return |
| 2241 | ** a pointer to the SELECT statement. If pX is not a SELECT statement, |
| 2242 | ** or if the SELECT statement needs to be manifested into a transient |
| 2243 | ** table, then return NULL. |
drh | b287f4b | 2008-04-25 00:08:38 +0000 | [diff] [blame] | 2244 | */ |
| 2245 | #ifndef SQLITE_OMIT_SUBQUERY |
dan | 7b35a77 | 2016-07-28 19:47:15 +0000 | [diff] [blame] | 2246 | static Select *isCandidateForInOpt(Expr *pX){ |
drh | 69c355b | 2016-03-09 15:34:51 +0000 | [diff] [blame] | 2247 | Select *p; |
drh | b287f4b | 2008-04-25 00:08:38 +0000 | [diff] [blame] | 2248 | SrcList *pSrc; |
| 2249 | ExprList *pEList; |
| 2250 | Table *pTab; |
dan | cfbb5e8 | 2016-07-13 19:48:13 +0000 | [diff] [blame] | 2251 | int i; |
drh | 69c355b | 2016-03-09 15:34:51 +0000 | [diff] [blame] | 2252 | if( !ExprHasProperty(pX, EP_xIsSelect) ) return 0; /* Not a subquery */ |
| 2253 | if( ExprHasProperty(pX, EP_VarSelect) ) return 0; /* Correlated subq */ |
| 2254 | p = pX->x.pSelect; |
drh | b287f4b | 2008-04-25 00:08:38 +0000 | [diff] [blame] | 2255 | if( p->pPrior ) return 0; /* Not a compound SELECT */ |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 2256 | if( p->selFlags & (SF_Distinct|SF_Aggregate) ){ |
drh | b74b101 | 2009-05-28 21:04:37 +0000 | [diff] [blame] | 2257 | testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct ); |
| 2258 | testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate ); |
| 2259 | return 0; /* No DISTINCT keyword and no aggregate functions */ |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 2260 | } |
drh | b74b101 | 2009-05-28 21:04:37 +0000 | [diff] [blame] | 2261 | assert( p->pGroupBy==0 ); /* Has no GROUP BY clause */ |
drh | b287f4b | 2008-04-25 00:08:38 +0000 | [diff] [blame] | 2262 | if( p->pLimit ) return 0; /* Has no LIMIT clause */ |
drh | b287f4b | 2008-04-25 00:08:38 +0000 | [diff] [blame] | 2263 | if( p->pWhere ) return 0; /* Has no WHERE clause */ |
| 2264 | pSrc = p->pSrc; |
drh | d1fa7bc | 2009-01-10 13:24:50 +0000 | [diff] [blame] | 2265 | assert( pSrc!=0 ); |
| 2266 | if( pSrc->nSrc!=1 ) return 0; /* Single term in FROM clause */ |
drh | b74b101 | 2009-05-28 21:04:37 +0000 | [diff] [blame] | 2267 | if( pSrc->a[0].pSelect ) return 0; /* FROM is not a subquery or view */ |
drh | b287f4b | 2008-04-25 00:08:38 +0000 | [diff] [blame] | 2268 | pTab = pSrc->a[0].pTab; |
drh | 69c355b | 2016-03-09 15:34:51 +0000 | [diff] [blame] | 2269 | assert( pTab!=0 ); |
drh | b74b101 | 2009-05-28 21:04:37 +0000 | [diff] [blame] | 2270 | assert( pTab->pSelect==0 ); /* FROM clause is not a view */ |
drh | b287f4b | 2008-04-25 00:08:38 +0000 | [diff] [blame] | 2271 | if( IsVirtual(pTab) ) return 0; /* FROM clause not a virtual table */ |
| 2272 | pEList = p->pEList; |
drh | ac6b47d | 2016-08-24 00:51:48 +0000 | [diff] [blame] | 2273 | assert( pEList!=0 ); |
dan | 7b35a77 | 2016-07-28 19:47:15 +0000 | [diff] [blame] | 2274 | /* All SELECT results must be columns. */ |
dan | cfbb5e8 | 2016-07-13 19:48:13 +0000 | [diff] [blame] | 2275 | for(i=0; i<pEList->nExpr; i++){ |
| 2276 | Expr *pRes = pEList->a[i].pExpr; |
| 2277 | if( pRes->op!=TK_COLUMN ) return 0; |
| 2278 | assert( pRes->iTable==pSrc->a[0].iCursor ); /* Not a correlated subquery */ |
dan | cfbb5e8 | 2016-07-13 19:48:13 +0000 | [diff] [blame] | 2279 | } |
drh | 69c355b | 2016-03-09 15:34:51 +0000 | [diff] [blame] | 2280 | return p; |
drh | b287f4b | 2008-04-25 00:08:38 +0000 | [diff] [blame] | 2281 | } |
| 2282 | #endif /* SQLITE_OMIT_SUBQUERY */ |
| 2283 | |
dan | f9b2e05 | 2016-08-02 17:45:00 +0000 | [diff] [blame] | 2284 | #ifndef SQLITE_OMIT_SUBQUERY |
dan | 1d8cb21 | 2011-12-09 13:24:16 +0000 | [diff] [blame] | 2285 | /* |
drh | 4c259e9 | 2014-08-01 21:12:35 +0000 | [diff] [blame] | 2286 | ** Generate code that checks the left-most column of index table iCur to see if |
| 2287 | ** it contains any NULL entries. Cause the register at regHasNull to be set |
drh | 6be515e | 2014-08-01 21:00:53 +0000 | [diff] [blame] | 2288 | ** to a non-NULL value if iCur contains no NULLs. Cause register regHasNull |
| 2289 | ** to be set to NULL if iCur contains one or more NULL values. |
| 2290 | */ |
| 2291 | static void sqlite3SetHasNullFlag(Vdbe *v, int iCur, int regHasNull){ |
drh | 728e0f9 | 2015-10-10 14:41:28 +0000 | [diff] [blame] | 2292 | int addr1; |
drh | 6be515e | 2014-08-01 21:00:53 +0000 | [diff] [blame] | 2293 | sqlite3VdbeAddOp2(v, OP_Integer, 0, regHasNull); |
drh | 728e0f9 | 2015-10-10 14:41:28 +0000 | [diff] [blame] | 2294 | addr1 = sqlite3VdbeAddOp1(v, OP_Rewind, iCur); VdbeCoverage(v); |
drh | 6be515e | 2014-08-01 21:00:53 +0000 | [diff] [blame] | 2295 | sqlite3VdbeAddOp3(v, OP_Column, iCur, 0, regHasNull); |
| 2296 | sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG); |
drh | 4c259e9 | 2014-08-01 21:12:35 +0000 | [diff] [blame] | 2297 | VdbeComment((v, "first_entry_in(%d)", iCur)); |
drh | 728e0f9 | 2015-10-10 14:41:28 +0000 | [diff] [blame] | 2298 | sqlite3VdbeJumpHere(v, addr1); |
drh | 6be515e | 2014-08-01 21:00:53 +0000 | [diff] [blame] | 2299 | } |
dan | f9b2e05 | 2016-08-02 17:45:00 +0000 | [diff] [blame] | 2300 | #endif |
drh | 6be515e | 2014-08-01 21:00:53 +0000 | [diff] [blame] | 2301 | |
drh | bb53ecb | 2014-08-02 21:03:33 +0000 | [diff] [blame] | 2302 | |
| 2303 | #ifndef SQLITE_OMIT_SUBQUERY |
| 2304 | /* |
| 2305 | ** The argument is an IN operator with a list (not a subquery) on the |
| 2306 | ** right-hand side. Return TRUE if that list is constant. |
| 2307 | */ |
| 2308 | static int sqlite3InRhsIsConstant(Expr *pIn){ |
| 2309 | Expr *pLHS; |
| 2310 | int res; |
| 2311 | assert( !ExprHasProperty(pIn, EP_xIsSelect) ); |
| 2312 | pLHS = pIn->pLeft; |
| 2313 | pIn->pLeft = 0; |
| 2314 | res = sqlite3ExprIsConstant(pIn); |
| 2315 | pIn->pLeft = pLHS; |
| 2316 | return res; |
| 2317 | } |
| 2318 | #endif |
| 2319 | |
drh | 6be515e | 2014-08-01 21:00:53 +0000 | [diff] [blame] | 2320 | /* |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 2321 | ** This function is used by the implementation of the IN (...) operator. |
drh | d4305ca | 2012-09-18 17:08:33 +0000 | [diff] [blame] | 2322 | ** The pX parameter is the expression on the RHS of the IN operator, which |
| 2323 | ** might be either a list of expressions or a subquery. |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 2324 | ** |
drh | d4305ca | 2012-09-18 17:08:33 +0000 | [diff] [blame] | 2325 | ** The job of this routine is to find or create a b-tree object that can |
| 2326 | ** be used either to test for membership in the RHS set or to iterate through |
| 2327 | ** all members of the RHS set, skipping duplicates. |
| 2328 | ** |
drh | 3a85625 | 2014-08-01 14:46:57 +0000 | [diff] [blame] | 2329 | ** A cursor is opened on the b-tree object that is the RHS of the IN operator |
drh | d4305ca | 2012-09-18 17:08:33 +0000 | [diff] [blame] | 2330 | ** and pX->iTable is set to the index of that cursor. |
| 2331 | ** |
drh | b74b101 | 2009-05-28 21:04:37 +0000 | [diff] [blame] | 2332 | ** The returned value of this function indicates the b-tree type, as follows: |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 2333 | ** |
drh | 1ccce44 | 2013-03-12 20:38:51 +0000 | [diff] [blame] | 2334 | ** IN_INDEX_ROWID - The cursor was opened on a database table. |
| 2335 | ** IN_INDEX_INDEX_ASC - The cursor was opened on an ascending index. |
| 2336 | ** IN_INDEX_INDEX_DESC - The cursor was opened on a descending index. |
| 2337 | ** IN_INDEX_EPH - The cursor was opened on a specially created and |
| 2338 | ** populated epheremal table. |
drh | bb53ecb | 2014-08-02 21:03:33 +0000 | [diff] [blame] | 2339 | ** IN_INDEX_NOOP - No cursor was allocated. The IN operator must be |
| 2340 | ** implemented as a sequence of comparisons. |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 2341 | ** |
drh | d4305ca | 2012-09-18 17:08:33 +0000 | [diff] [blame] | 2342 | ** An existing b-tree might be used if the RHS expression pX is a simple |
| 2343 | ** subquery such as: |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 2344 | ** |
dan | 553168c | 2016-08-01 20:14:31 +0000 | [diff] [blame] | 2345 | ** SELECT <column1>, <column2>... FROM <table> |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 2346 | ** |
drh | d4305ca | 2012-09-18 17:08:33 +0000 | [diff] [blame] | 2347 | ** If the RHS of the IN operator is a list or a more complex subquery, then |
| 2348 | ** an ephemeral table might need to be generated from the RHS and then |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 2349 | ** pX->iTable made to point to the ephemeral table instead of an |
drh | 3a85625 | 2014-08-01 14:46:57 +0000 | [diff] [blame] | 2350 | ** existing table. |
drh | d4305ca | 2012-09-18 17:08:33 +0000 | [diff] [blame] | 2351 | ** |
drh | 7fc0ba0 | 2017-11-17 15:02:00 +0000 | [diff] [blame] | 2352 | ** The inFlags parameter must contain, at a minimum, one of the bits |
| 2353 | ** IN_INDEX_MEMBERSHIP or IN_INDEX_LOOP but not both. If inFlags contains |
| 2354 | ** IN_INDEX_MEMBERSHIP, then the generated table will be used for a fast |
| 2355 | ** membership test. When the IN_INDEX_LOOP bit is set, the IN index will |
| 2356 | ** be used to loop over all values of the RHS of the IN operator. |
drh | 3a85625 | 2014-08-01 14:46:57 +0000 | [diff] [blame] | 2357 | ** |
| 2358 | ** When IN_INDEX_LOOP is used (and the b-tree will be used to iterate |
| 2359 | ** through the set members) then the b-tree must not contain duplicates. |
drh | 7fc0ba0 | 2017-11-17 15:02:00 +0000 | [diff] [blame] | 2360 | ** An epheremal table will be created unless the selected columns are guaranteed |
dan | 553168c | 2016-08-01 20:14:31 +0000 | [diff] [blame] | 2361 | ** to be unique - either because it is an INTEGER PRIMARY KEY or due to |
| 2362 | ** a UNIQUE constraint or index. |
danielk1977 | 0cdc022 | 2008-06-26 18:04:03 +0000 | [diff] [blame] | 2363 | ** |
drh | 3a85625 | 2014-08-01 14:46:57 +0000 | [diff] [blame] | 2364 | ** When IN_INDEX_MEMBERSHIP is used (and the b-tree will be used |
| 2365 | ** for fast set membership tests) then an epheremal table must |
dan | 553168c | 2016-08-01 20:14:31 +0000 | [diff] [blame] | 2366 | ** be used unless <columns> is a single INTEGER PRIMARY KEY column or an |
| 2367 | ** index can be found with the specified <columns> as its left-most. |
danielk1977 | 0cdc022 | 2008-06-26 18:04:03 +0000 | [diff] [blame] | 2368 | ** |
drh | bb53ecb | 2014-08-02 21:03:33 +0000 | [diff] [blame] | 2369 | ** If the IN_INDEX_NOOP_OK and IN_INDEX_MEMBERSHIP are both set and |
| 2370 | ** if the RHS of the IN operator is a list (not a subquery) then this |
| 2371 | ** routine might decide that creating an ephemeral b-tree for membership |
| 2372 | ** testing is too expensive and return IN_INDEX_NOOP. In that case, the |
| 2373 | ** calling routine should implement the IN operator using a sequence |
| 2374 | ** of Eq or Ne comparison operations. |
| 2375 | ** |
drh | b74b101 | 2009-05-28 21:04:37 +0000 | [diff] [blame] | 2376 | ** When the b-tree is being used for membership tests, the calling function |
drh | 3a85625 | 2014-08-01 14:46:57 +0000 | [diff] [blame] | 2377 | ** might need to know whether or not the RHS side of the IN operator |
drh | e21a6e1 | 2014-08-01 18:00:24 +0000 | [diff] [blame] | 2378 | ** contains a NULL. If prRhsHasNull is not a NULL pointer and |
drh | 3a85625 | 2014-08-01 14:46:57 +0000 | [diff] [blame] | 2379 | ** if there is any chance that the (...) might contain a NULL value at |
danielk1977 | 0cdc022 | 2008-06-26 18:04:03 +0000 | [diff] [blame] | 2380 | ** runtime, then a register is allocated and the register number written |
drh | e21a6e1 | 2014-08-01 18:00:24 +0000 | [diff] [blame] | 2381 | ** to *prRhsHasNull. If there is no chance that the (...) contains a |
| 2382 | ** NULL value, then *prRhsHasNull is left unchanged. |
danielk1977 | 0cdc022 | 2008-06-26 18:04:03 +0000 | [diff] [blame] | 2383 | ** |
drh | e21a6e1 | 2014-08-01 18:00:24 +0000 | [diff] [blame] | 2384 | ** If a register is allocated and its location stored in *prRhsHasNull, then |
drh | 6be515e | 2014-08-01 21:00:53 +0000 | [diff] [blame] | 2385 | ** the value in that register will be NULL if the b-tree contains one or more |
| 2386 | ** NULL values, and it will be some non-NULL value if the b-tree contains no |
| 2387 | ** NULL values. |
dan | 553168c | 2016-08-01 20:14:31 +0000 | [diff] [blame] | 2388 | ** |
| 2389 | ** If the aiMap parameter is not NULL, it must point to an array containing |
| 2390 | ** one element for each column returned by the SELECT statement on the RHS |
| 2391 | ** of the IN(...) operator. The i'th entry of the array is populated with the |
| 2392 | ** offset of the index column that matches the i'th column returned by the |
| 2393 | ** SELECT. For example, if the expression and selected index are: |
| 2394 | ** |
| 2395 | ** (?,?,?) IN (SELECT a, b, c FROM t1) |
| 2396 | ** CREATE INDEX i1 ON t1(b, c, a); |
| 2397 | ** |
| 2398 | ** then aiMap[] is populated with {2, 0, 1}. |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 2399 | */ |
danielk1977 | 284f4ac | 2007-12-10 05:03:46 +0000 | [diff] [blame] | 2400 | #ifndef SQLITE_OMIT_SUBQUERY |
dan | ba00e30 | 2016-07-23 20:24:06 +0000 | [diff] [blame] | 2401 | int sqlite3FindInIndex( |
drh | 6fc8f36 | 2016-08-26 19:31:29 +0000 | [diff] [blame] | 2402 | Parse *pParse, /* Parsing context */ |
| 2403 | Expr *pX, /* The right-hand side (RHS) of the IN operator */ |
| 2404 | u32 inFlags, /* IN_INDEX_LOOP, _MEMBERSHIP, and/or _NOOP_OK */ |
| 2405 | int *prRhsHasNull, /* Register holding NULL status. See notes */ |
drh | 2c04131 | 2018-12-24 02:34:49 +0000 | [diff] [blame] | 2406 | int *aiMap, /* Mapping from Index fields to RHS fields */ |
| 2407 | int *piTab /* OUT: index to use */ |
dan | ba00e30 | 2016-07-23 20:24:06 +0000 | [diff] [blame] | 2408 | ){ |
drh | b74b101 | 2009-05-28 21:04:37 +0000 | [diff] [blame] | 2409 | Select *p; /* SELECT to the right of IN operator */ |
| 2410 | int eType = 0; /* Type of RHS table. IN_INDEX_* */ |
| 2411 | int iTab = pParse->nTab++; /* Cursor of the RHS table */ |
drh | 3a85625 | 2014-08-01 14:46:57 +0000 | [diff] [blame] | 2412 | int mustBeUnique; /* True if RHS must be unique */ |
drh | b8475df | 2011-12-09 16:21:19 +0000 | [diff] [blame] | 2413 | Vdbe *v = sqlite3GetVdbe(pParse); /* Virtual machine being coded */ |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 2414 | |
drh | 1450bc6 | 2009-10-30 13:25:56 +0000 | [diff] [blame] | 2415 | assert( pX->op==TK_IN ); |
drh | 3a85625 | 2014-08-01 14:46:57 +0000 | [diff] [blame] | 2416 | mustBeUnique = (inFlags & IN_INDEX_LOOP)!=0; |
drh | 1450bc6 | 2009-10-30 13:25:56 +0000 | [diff] [blame] | 2417 | |
dan | 7b35a77 | 2016-07-28 19:47:15 +0000 | [diff] [blame] | 2418 | /* If the RHS of this IN(...) operator is a SELECT, and if it matters |
| 2419 | ** whether or not the SELECT result contains NULL values, check whether |
dan | 870a070 | 2016-08-01 16:37:43 +0000 | [diff] [blame] | 2420 | ** or not NULL is actually possible (it may not be, for example, due |
dan | 7b35a77 | 2016-07-28 19:47:15 +0000 | [diff] [blame] | 2421 | ** to NOT NULL constraints in the schema). If no NULL values are possible, |
dan | 870a070 | 2016-08-01 16:37:43 +0000 | [diff] [blame] | 2422 | ** set prRhsHasNull to 0 before continuing. */ |
dan | 7b35a77 | 2016-07-28 19:47:15 +0000 | [diff] [blame] | 2423 | if( prRhsHasNull && (pX->flags & EP_xIsSelect) ){ |
| 2424 | int i; |
| 2425 | ExprList *pEList = pX->x.pSelect->pEList; |
| 2426 | for(i=0; i<pEList->nExpr; i++){ |
| 2427 | if( sqlite3ExprCanBeNull(pEList->a[i].pExpr) ) break; |
| 2428 | } |
| 2429 | if( i==pEList->nExpr ){ |
| 2430 | prRhsHasNull = 0; |
| 2431 | } |
| 2432 | } |
| 2433 | |
drh | b74b101 | 2009-05-28 21:04:37 +0000 | [diff] [blame] | 2434 | /* Check to see if an existing table or index can be used to |
| 2435 | ** satisfy the query. This is preferable to generating a new |
dan | 7b35a77 | 2016-07-28 19:47:15 +0000 | [diff] [blame] | 2436 | ** ephemeral table. */ |
| 2437 | if( pParse->nErr==0 && (p = isCandidateForInOpt(pX))!=0 ){ |
danielk1977 | e1fb65a | 2009-04-02 17:23:32 +0000 | [diff] [blame] | 2438 | sqlite3 *db = pParse->db; /* Database connection */ |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 2439 | Table *pTab; /* Table <table>. */ |
dan | ba00e30 | 2016-07-23 20:24:06 +0000 | [diff] [blame] | 2440 | i16 iDb; /* Database idx for pTab */ |
dan | cfbb5e8 | 2016-07-13 19:48:13 +0000 | [diff] [blame] | 2441 | ExprList *pEList = p->pEList; |
| 2442 | int nExpr = pEList->nExpr; |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 2443 | |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 2444 | assert( p->pEList!=0 ); /* Because of isCandidateForInOpt(p) */ |
| 2445 | assert( p->pEList->a[0].pExpr!=0 ); /* Because of isCandidateForInOpt(p) */ |
| 2446 | assert( p->pSrc!=0 ); /* Because of isCandidateForInOpt(p) */ |
| 2447 | pTab = p->pSrc->a[0].pTab; |
dan | cfbb5e8 | 2016-07-13 19:48:13 +0000 | [diff] [blame] | 2448 | |
drh | b22f7c8 | 2014-02-06 23:56:27 +0000 | [diff] [blame] | 2449 | /* Code an OP_Transaction and OP_TableLock for <table>. */ |
danielk1977 | e1fb65a | 2009-04-02 17:23:32 +0000 | [diff] [blame] | 2450 | iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
| 2451 | sqlite3CodeVerifySchema(pParse, iDb); |
| 2452 | sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName); |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 2453 | |
drh | a84a283 | 2016-08-26 21:15:35 +0000 | [diff] [blame] | 2454 | assert(v); /* sqlite3GetVdbe() has always been previously called */ |
dan | cfbb5e8 | 2016-07-13 19:48:13 +0000 | [diff] [blame] | 2455 | if( nExpr==1 && pEList->a[0].pExpr->iColumn<0 ){ |
drh | 62659b2 | 2016-08-24 18:51:23 +0000 | [diff] [blame] | 2456 | /* The "x IN (SELECT rowid FROM table)" case */ |
drh | 511f9e8 | 2016-09-22 18:53:13 +0000 | [diff] [blame] | 2457 | int iAddr = sqlite3VdbeAddOp0(v, OP_Once); |
drh | 7d17610 | 2014-02-18 03:07:12 +0000 | [diff] [blame] | 2458 | VdbeCoverage(v); |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 2459 | |
| 2460 | sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead); |
| 2461 | eType = IN_INDEX_ROWID; |
drh | d885209 | 2018-08-16 15:29:40 +0000 | [diff] [blame] | 2462 | ExplainQueryPlan((pParse, 0, |
| 2463 | "USING ROWID SEARCH ON TABLE %s FOR IN-OPERATOR",pTab->zName)); |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 2464 | sqlite3VdbeJumpHere(v, iAddr); |
| 2465 | }else{ |
danielk1977 | e1fb65a | 2009-04-02 17:23:32 +0000 | [diff] [blame] | 2466 | Index *pIdx; /* Iterator variable */ |
dan | cfbb5e8 | 2016-07-13 19:48:13 +0000 | [diff] [blame] | 2467 | int affinity_ok = 1; |
| 2468 | int i; |
| 2469 | |
| 2470 | /* Check that the affinity that will be used to perform each |
drh | 62659b2 | 2016-08-24 18:51:23 +0000 | [diff] [blame] | 2471 | ** comparison is the same as the affinity of each column in table |
| 2472 | ** on the RHS of the IN operator. If it not, it is not possible to |
| 2473 | ** use any index of the RHS table. */ |
dan | cfbb5e8 | 2016-07-13 19:48:13 +0000 | [diff] [blame] | 2474 | for(i=0; i<nExpr && affinity_ok; i++){ |
drh | fc7f27b | 2016-08-20 00:07:01 +0000 | [diff] [blame] | 2475 | Expr *pLhs = sqlite3VectorFieldSubexpr(pX->pLeft, i); |
dan | cfbb5e8 | 2016-07-13 19:48:13 +0000 | [diff] [blame] | 2476 | int iCol = pEList->a[i].pExpr->iColumn; |
drh | 0dfa4f6 | 2016-08-26 13:19:49 +0000 | [diff] [blame] | 2477 | char idxaff = sqlite3TableColumnAffinity(pTab,iCol); /* RHS table */ |
dan | cfbb5e8 | 2016-07-13 19:48:13 +0000 | [diff] [blame] | 2478 | char cmpaff = sqlite3CompareAffinity(pLhs, idxaff); |
drh | 62659b2 | 2016-08-24 18:51:23 +0000 | [diff] [blame] | 2479 | testcase( cmpaff==SQLITE_AFF_BLOB ); |
| 2480 | testcase( cmpaff==SQLITE_AFF_TEXT ); |
dan | cfbb5e8 | 2016-07-13 19:48:13 +0000 | [diff] [blame] | 2481 | switch( cmpaff ){ |
| 2482 | case SQLITE_AFF_BLOB: |
| 2483 | break; |
| 2484 | case SQLITE_AFF_TEXT: |
drh | 62659b2 | 2016-08-24 18:51:23 +0000 | [diff] [blame] | 2485 | /* sqlite3CompareAffinity() only returns TEXT if one side or the |
| 2486 | ** other has no affinity and the other side is TEXT. Hence, |
| 2487 | ** the only way for cmpaff to be TEXT is for idxaff to be TEXT |
| 2488 | ** and for the term on the LHS of the IN to have no affinity. */ |
| 2489 | assert( idxaff==SQLITE_AFF_TEXT ); |
dan | cfbb5e8 | 2016-07-13 19:48:13 +0000 | [diff] [blame] | 2490 | break; |
| 2491 | default: |
| 2492 | affinity_ok = sqlite3IsNumericAffinity(idxaff); |
| 2493 | } |
| 2494 | } |
danielk1977 | e1fb65a | 2009-04-02 17:23:32 +0000 | [diff] [blame] | 2495 | |
drh | a84a283 | 2016-08-26 21:15:35 +0000 | [diff] [blame] | 2496 | if( affinity_ok ){ |
| 2497 | /* Search for an existing index that will work for this IN operator */ |
| 2498 | for(pIdx=pTab->pIndex; pIdx && eType==0; pIdx=pIdx->pNext){ |
| 2499 | Bitmask colUsed; /* Columns of the index used */ |
| 2500 | Bitmask mCol; /* Mask for the current column */ |
| 2501 | if( pIdx->nColumn<nExpr ) continue; |
drh | d4a4a36 | 2018-12-08 20:30:31 +0000 | [diff] [blame] | 2502 | if( pIdx->pPartIdxWhere!=0 ) continue; |
drh | a84a283 | 2016-08-26 21:15:35 +0000 | [diff] [blame] | 2503 | /* Maximum nColumn is BMS-2, not BMS-1, so that we can compute |
| 2504 | ** BITMASK(nExpr) without overflowing */ |
| 2505 | testcase( pIdx->nColumn==BMS-2 ); |
| 2506 | testcase( pIdx->nColumn==BMS-1 ); |
| 2507 | if( pIdx->nColumn>=BMS-1 ) continue; |
| 2508 | if( mustBeUnique ){ |
| 2509 | if( pIdx->nKeyCol>nExpr |
| 2510 | ||(pIdx->nColumn>nExpr && !IsUniqueIndex(pIdx)) |
| 2511 | ){ |
| 2512 | continue; /* This index is not unique over the IN RHS columns */ |
dan | 7b35a77 | 2016-07-28 19:47:15 +0000 | [diff] [blame] | 2513 | } |
danielk1977 | 0cdc022 | 2008-06-26 18:04:03 +0000 | [diff] [blame] | 2514 | } |
drh | a84a283 | 2016-08-26 21:15:35 +0000 | [diff] [blame] | 2515 | |
| 2516 | colUsed = 0; /* Columns of index used so far */ |
| 2517 | for(i=0; i<nExpr; i++){ |
| 2518 | Expr *pLhs = sqlite3VectorFieldSubexpr(pX->pLeft, i); |
| 2519 | Expr *pRhs = pEList->a[i].pExpr; |
| 2520 | CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pLhs, pRhs); |
| 2521 | int j; |
| 2522 | |
| 2523 | assert( pReq!=0 || pRhs->iColumn==XN_ROWID || pParse->nErr ); |
| 2524 | for(j=0; j<nExpr; j++){ |
| 2525 | if( pIdx->aiColumn[j]!=pRhs->iColumn ) continue; |
| 2526 | assert( pIdx->azColl[j] ); |
drh | 106526e | 2016-08-26 22:09:01 +0000 | [diff] [blame] | 2527 | if( pReq!=0 && sqlite3StrICmp(pReq->zName, pIdx->azColl[j])!=0 ){ |
| 2528 | continue; |
| 2529 | } |
drh | a84a283 | 2016-08-26 21:15:35 +0000 | [diff] [blame] | 2530 | break; |
| 2531 | } |
| 2532 | if( j==nExpr ) break; |
| 2533 | mCol = MASKBIT(j); |
| 2534 | if( mCol & colUsed ) break; /* Each column used only once */ |
| 2535 | colUsed |= mCol; |
| 2536 | if( aiMap ) aiMap[i] = j; |
| 2537 | } |
| 2538 | |
| 2539 | assert( i==nExpr || colUsed!=(MASKBIT(nExpr)-1) ); |
| 2540 | if( colUsed==(MASKBIT(nExpr)-1) ){ |
| 2541 | /* If we reach this point, that means the index pIdx is usable */ |
drh | 511f9e8 | 2016-09-22 18:53:13 +0000 | [diff] [blame] | 2542 | int iAddr = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v); |
drh | e2ca99c | 2018-05-02 00:33:43 +0000 | [diff] [blame] | 2543 | ExplainQueryPlan((pParse, 0, |
| 2544 | "USING INDEX %s FOR IN-OPERATOR",pIdx->zName)); |
drh | a84a283 | 2016-08-26 21:15:35 +0000 | [diff] [blame] | 2545 | sqlite3VdbeAddOp3(v, OP_OpenRead, iTab, pIdx->tnum, iDb); |
| 2546 | sqlite3VdbeSetP4KeyInfo(pParse, pIdx); |
| 2547 | VdbeComment((v, "%s", pIdx->zName)); |
| 2548 | assert( IN_INDEX_INDEX_DESC == IN_INDEX_INDEX_ASC+1 ); |
| 2549 | eType = IN_INDEX_INDEX_ASC + pIdx->aSortOrder[0]; |
| 2550 | |
| 2551 | if( prRhsHasNull ){ |
drh | b80dbdc | 2016-09-09 15:12:41 +0000 | [diff] [blame] | 2552 | #ifdef SQLITE_ENABLE_COLUMN_USED_MASK |
drh | a84a283 | 2016-08-26 21:15:35 +0000 | [diff] [blame] | 2553 | i64 mask = (1<<nExpr)-1; |
| 2554 | sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed, |
| 2555 | iTab, 0, 0, (u8*)&mask, P4_INT64); |
drh | b80dbdc | 2016-09-09 15:12:41 +0000 | [diff] [blame] | 2556 | #endif |
| 2557 | *prRhsHasNull = ++pParse->nMem; |
drh | a84a283 | 2016-08-26 21:15:35 +0000 | [diff] [blame] | 2558 | if( nExpr==1 ){ |
| 2559 | sqlite3SetHasNullFlag(v, iTab, *prRhsHasNull); |
| 2560 | } |
| 2561 | } |
| 2562 | sqlite3VdbeJumpHere(v, iAddr); |
| 2563 | } |
| 2564 | } /* End loop over indexes */ |
| 2565 | } /* End if( affinity_ok ) */ |
| 2566 | } /* End if not an rowid index */ |
| 2567 | } /* End attempt to optimize using an index */ |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 2568 | |
drh | bb53ecb | 2014-08-02 21:03:33 +0000 | [diff] [blame] | 2569 | /* If no preexisting index is available for the IN clause |
| 2570 | ** and IN_INDEX_NOOP is an allowed reply |
| 2571 | ** and the RHS of the IN operator is a list, not a subquery |
dan | 71c57db | 2016-07-09 20:23:55 +0000 | [diff] [blame] | 2572 | ** and the RHS is not constant or has two or fewer terms, |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 2573 | ** then it is not worth creating an ephemeral table to evaluate |
drh | bb53ecb | 2014-08-02 21:03:33 +0000 | [diff] [blame] | 2574 | ** the IN operator so return IN_INDEX_NOOP. |
| 2575 | */ |
| 2576 | if( eType==0 |
| 2577 | && (inFlags & IN_INDEX_NOOP_OK) |
| 2578 | && !ExprHasProperty(pX, EP_xIsSelect) |
| 2579 | && (!sqlite3InRhsIsConstant(pX) || pX->x.pList->nExpr<=2) |
| 2580 | ){ |
| 2581 | eType = IN_INDEX_NOOP; |
| 2582 | } |
drh | bb53ecb | 2014-08-02 21:03:33 +0000 | [diff] [blame] | 2583 | |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 2584 | if( eType==0 ){ |
drh | 4387006 | 2014-07-31 15:44:44 +0000 | [diff] [blame] | 2585 | /* Could not find an existing table or index to use as the RHS b-tree. |
drh | b74b101 | 2009-05-28 21:04:37 +0000 | [diff] [blame] | 2586 | ** We will have to generate an ephemeral table to do the job. |
| 2587 | */ |
drh | 8e23daf | 2013-06-11 13:30:04 +0000 | [diff] [blame] | 2588 | u32 savedNQueryLoop = pParse->nQueryLoop; |
danielk1977 | 0cdc022 | 2008-06-26 18:04:03 +0000 | [diff] [blame] | 2589 | int rMayHaveNull = 0; |
danielk1977 | 41a05b7 | 2008-10-02 13:50:55 +0000 | [diff] [blame] | 2590 | eType = IN_INDEX_EPH; |
drh | 3a85625 | 2014-08-01 14:46:57 +0000 | [diff] [blame] | 2591 | if( inFlags & IN_INDEX_LOOP ){ |
drh | 4a5acf8 | 2013-06-18 20:06:23 +0000 | [diff] [blame] | 2592 | pParse->nQueryLoop = 0; |
drh | e21a6e1 | 2014-08-01 18:00:24 +0000 | [diff] [blame] | 2593 | }else if( prRhsHasNull ){ |
| 2594 | *prRhsHasNull = rMayHaveNull = ++pParse->nMem; |
danielk1977 | 0cdc022 | 2008-06-26 18:04:03 +0000 | [diff] [blame] | 2595 | } |
drh | 85bcdce | 2018-12-23 21:27:29 +0000 | [diff] [blame] | 2596 | assert( pX->op==TK_IN ); |
drh | 50ef671 | 2019-02-22 23:29:56 +0000 | [diff] [blame] | 2597 | sqlite3CodeRhsOfIN(pParse, pX, iTab); |
drh | 85bcdce | 2018-12-23 21:27:29 +0000 | [diff] [blame] | 2598 | if( rMayHaveNull ){ |
drh | 2c04131 | 2018-12-24 02:34:49 +0000 | [diff] [blame] | 2599 | sqlite3SetHasNullFlag(v, iTab, rMayHaveNull); |
drh | 85bcdce | 2018-12-23 21:27:29 +0000 | [diff] [blame] | 2600 | } |
drh | cf4d38a | 2010-07-28 02:53:36 +0000 | [diff] [blame] | 2601 | pParse->nQueryLoop = savedNQueryLoop; |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 2602 | } |
dan | ba00e30 | 2016-07-23 20:24:06 +0000 | [diff] [blame] | 2603 | |
| 2604 | if( aiMap && eType!=IN_INDEX_INDEX_ASC && eType!=IN_INDEX_INDEX_DESC ){ |
| 2605 | int i, n; |
| 2606 | n = sqlite3ExprVectorSize(pX->pLeft); |
| 2607 | for(i=0; i<n; i++) aiMap[i] = i; |
| 2608 | } |
drh | 2c04131 | 2018-12-24 02:34:49 +0000 | [diff] [blame] | 2609 | *piTab = iTab; |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 2610 | return eType; |
| 2611 | } |
danielk1977 | 284f4ac | 2007-12-10 05:03:46 +0000 | [diff] [blame] | 2612 | #endif |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 2613 | |
dan | f9b2e05 | 2016-08-02 17:45:00 +0000 | [diff] [blame] | 2614 | #ifndef SQLITE_OMIT_SUBQUERY |
dan | 553168c | 2016-08-01 20:14:31 +0000 | [diff] [blame] | 2615 | /* |
| 2616 | ** Argument pExpr is an (?, ?...) IN(...) expression. This |
| 2617 | ** function allocates and returns a nul-terminated string containing |
| 2618 | ** the affinities to be used for each column of the comparison. |
| 2619 | ** |
| 2620 | ** It is the responsibility of the caller to ensure that the returned |
| 2621 | ** string is eventually freed using sqlite3DbFree(). |
| 2622 | */ |
dan | 71c57db | 2016-07-09 20:23:55 +0000 | [diff] [blame] | 2623 | static char *exprINAffinity(Parse *pParse, Expr *pExpr){ |
| 2624 | Expr *pLeft = pExpr->pLeft; |
| 2625 | int nVal = sqlite3ExprVectorSize(pLeft); |
dan | 553168c | 2016-08-01 20:14:31 +0000 | [diff] [blame] | 2626 | Select *pSelect = (pExpr->flags & EP_xIsSelect) ? pExpr->x.pSelect : 0; |
dan | 71c57db | 2016-07-09 20:23:55 +0000 | [diff] [blame] | 2627 | char *zRet; |
| 2628 | |
dan | 553168c | 2016-08-01 20:14:31 +0000 | [diff] [blame] | 2629 | assert( pExpr->op==TK_IN ); |
drh | 5c258dc | 2017-02-16 17:18:07 +0000 | [diff] [blame] | 2630 | zRet = sqlite3DbMallocRaw(pParse->db, nVal+1); |
dan | 71c57db | 2016-07-09 20:23:55 +0000 | [diff] [blame] | 2631 | if( zRet ){ |
| 2632 | int i; |
| 2633 | for(i=0; i<nVal; i++){ |
drh | fc7f27b | 2016-08-20 00:07:01 +0000 | [diff] [blame] | 2634 | Expr *pA = sqlite3VectorFieldSubexpr(pLeft, i); |
dan | 553168c | 2016-08-01 20:14:31 +0000 | [diff] [blame] | 2635 | char a = sqlite3ExprAffinity(pA); |
| 2636 | if( pSelect ){ |
| 2637 | zRet[i] = sqlite3CompareAffinity(pSelect->pEList->a[i].pExpr, a); |
| 2638 | }else{ |
| 2639 | zRet[i] = a; |
dan | 71c57db | 2016-07-09 20:23:55 +0000 | [diff] [blame] | 2640 | } |
dan | 71c57db | 2016-07-09 20:23:55 +0000 | [diff] [blame] | 2641 | } |
| 2642 | zRet[nVal] = '\0'; |
| 2643 | } |
| 2644 | return zRet; |
| 2645 | } |
dan | f9b2e05 | 2016-08-02 17:45:00 +0000 | [diff] [blame] | 2646 | #endif |
dan | 71c57db | 2016-07-09 20:23:55 +0000 | [diff] [blame] | 2647 | |
dan | 8da209b | 2016-07-26 18:06:08 +0000 | [diff] [blame] | 2648 | #ifndef SQLITE_OMIT_SUBQUERY |
| 2649 | /* |
| 2650 | ** Load the Parse object passed as the first argument with an error |
| 2651 | ** message of the form: |
| 2652 | ** |
| 2653 | ** "sub-select returns N columns - expected M" |
| 2654 | */ |
| 2655 | void sqlite3SubselectError(Parse *pParse, int nActual, int nExpect){ |
| 2656 | const char *zFmt = "sub-select returns %d columns - expected %d"; |
| 2657 | sqlite3ErrorMsg(pParse, zFmt, nActual, nExpect); |
| 2658 | } |
| 2659 | #endif |
| 2660 | |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 2661 | /* |
dan | 44c5604 | 2016-12-07 15:38:37 +0000 | [diff] [blame] | 2662 | ** Expression pExpr is a vector that has been used in a context where |
| 2663 | ** it is not permitted. If pExpr is a sub-select vector, this routine |
| 2664 | ** loads the Parse object with a message of the form: |
| 2665 | ** |
| 2666 | ** "sub-select returns N columns - expected 1" |
| 2667 | ** |
| 2668 | ** Or, if it is a regular scalar vector: |
| 2669 | ** |
| 2670 | ** "row value misused" |
| 2671 | */ |
| 2672 | void sqlite3VectorErrorMsg(Parse *pParse, Expr *pExpr){ |
| 2673 | #ifndef SQLITE_OMIT_SUBQUERY |
| 2674 | if( pExpr->flags & EP_xIsSelect ){ |
| 2675 | sqlite3SubselectError(pParse, pExpr->x.pSelect->pEList->nExpr, 1); |
| 2676 | }else |
| 2677 | #endif |
| 2678 | { |
| 2679 | sqlite3ErrorMsg(pParse, "row value misused"); |
| 2680 | } |
| 2681 | } |
| 2682 | |
drh | 85bcdce | 2018-12-23 21:27:29 +0000 | [diff] [blame] | 2683 | #ifndef SQLITE_OMIT_SUBQUERY |
dan | 44c5604 | 2016-12-07 15:38:37 +0000 | [diff] [blame] | 2684 | /* |
drh | 85bcdce | 2018-12-23 21:27:29 +0000 | [diff] [blame] | 2685 | ** Generate code that will construct an ephemeral table containing all terms |
| 2686 | ** in the RHS of an IN operator. The IN operator can be in either of two |
| 2687 | ** forms: |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 2688 | ** |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 2689 | ** x IN (4,5,11) -- IN operator with list on right-hand side |
| 2690 | ** x IN (SELECT a FROM b) -- IN operator with subquery on the right |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 2691 | ** |
drh | 2c04131 | 2018-12-24 02:34:49 +0000 | [diff] [blame] | 2692 | ** The pExpr parameter is the IN operator. The cursor number for the |
| 2693 | ** constructed ephermeral table is returned. The first time the ephemeral |
| 2694 | ** table is computed, the cursor number is also stored in pExpr->iTable, |
| 2695 | ** however the cursor number returned might not be the same, as it might |
| 2696 | ** have been duplicated using OP_OpenDup. |
danielk1977 | 41a05b7 | 2008-10-02 13:50:55 +0000 | [diff] [blame] | 2697 | ** |
drh | 85bcdce | 2018-12-23 21:27:29 +0000 | [diff] [blame] | 2698 | ** If the LHS expression ("x" in the examples) is a column value, or |
| 2699 | ** the SELECT statement returns a column value, then the affinity of that |
| 2700 | ** column is used to build the index keys. If both 'x' and the |
| 2701 | ** SELECT... statement are columns, then numeric affinity is used |
| 2702 | ** if either column has NUMERIC or INTEGER affinity. If neither |
| 2703 | ** 'x' nor the SELECT... statement are columns, then numeric affinity |
| 2704 | ** is used. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 2705 | */ |
drh | 85bcdce | 2018-12-23 21:27:29 +0000 | [diff] [blame] | 2706 | void sqlite3CodeRhsOfIN( |
drh | fd773cf | 2009-05-29 14:39:07 +0000 | [diff] [blame] | 2707 | Parse *pParse, /* Parsing context */ |
drh | 85bcdce | 2018-12-23 21:27:29 +0000 | [diff] [blame] | 2708 | Expr *pExpr, /* The IN operator */ |
drh | 50ef671 | 2019-02-22 23:29:56 +0000 | [diff] [blame] | 2709 | int iTab /* Use this cursor number */ |
danielk1977 | 41a05b7 | 2008-10-02 13:50:55 +0000 | [diff] [blame] | 2710 | ){ |
drh | 2c04131 | 2018-12-24 02:34:49 +0000 | [diff] [blame] | 2711 | int addrOnce = 0; /* Address of the OP_Once instruction at top */ |
drh | 85bcdce | 2018-12-23 21:27:29 +0000 | [diff] [blame] | 2712 | int addr; /* Address of OP_OpenEphemeral instruction */ |
| 2713 | Expr *pLeft; /* the LHS of the IN operator */ |
| 2714 | KeyInfo *pKeyInfo = 0; /* Key information */ |
| 2715 | int nVal; /* Size of vector pLeft */ |
| 2716 | Vdbe *v; /* The prepared statement under construction */ |
danielk1977 | fc97606 | 2007-05-10 10:46:56 +0000 | [diff] [blame] | 2717 | |
drh | 2c04131 | 2018-12-24 02:34:49 +0000 | [diff] [blame] | 2718 | v = pParse->pVdbe; |
drh | 85bcdce | 2018-12-23 21:27:29 +0000 | [diff] [blame] | 2719 | assert( v!=0 ); |
| 2720 | |
drh | 2c04131 | 2018-12-24 02:34:49 +0000 | [diff] [blame] | 2721 | /* The evaluation of the IN must be repeated every time it |
drh | 39a1181 | 2016-08-19 19:12:58 +0000 | [diff] [blame] | 2722 | ** is encountered if any of the following is true: |
drh | 57dbd7b | 2005-07-08 18:25:26 +0000 | [diff] [blame] | 2723 | ** |
| 2724 | ** * The right-hand side is a correlated subquery |
| 2725 | ** * The right-hand side is an expression list containing variables |
| 2726 | ** * We are inside a trigger |
| 2727 | ** |
drh | 2c04131 | 2018-12-24 02:34:49 +0000 | [diff] [blame] | 2728 | ** If all of the above are false, then we can compute the RHS just once |
| 2729 | ** and reuse it many names. |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 2730 | */ |
drh | efb699f | 2018-12-24 11:55:44 +0000 | [diff] [blame] | 2731 | if( !ExprHasProperty(pExpr, EP_VarSelect) && pParse->iSelfTab==0 ){ |
drh | 2c04131 | 2018-12-24 02:34:49 +0000 | [diff] [blame] | 2732 | /* Reuse of the RHS is allowed */ |
| 2733 | /* If this routine has already been coded, but the previous code |
| 2734 | ** might not have been invoked yet, so invoke it now as a subroutine. |
| 2735 | */ |
| 2736 | if( ExprHasProperty(pExpr, EP_Subrtn) ){ |
drh | f9231c3 | 2018-12-31 21:43:55 +0000 | [diff] [blame] | 2737 | addrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v); |
drh | bd462bc | 2018-12-24 20:21:06 +0000 | [diff] [blame] | 2738 | if( ExprHasProperty(pExpr, EP_xIsSelect) ){ |
| 2739 | ExplainQueryPlan((pParse, 0, "REUSE LIST SUBQUERY %d", |
| 2740 | pExpr->x.pSelect->selId)); |
| 2741 | } |
drh | 2c04131 | 2018-12-24 02:34:49 +0000 | [diff] [blame] | 2742 | sqlite3VdbeAddOp2(v, OP_Gosub, pExpr->y.sub.regReturn, |
| 2743 | pExpr->y.sub.iAddr); |
| 2744 | sqlite3VdbeAddOp2(v, OP_OpenDup, iTab, pExpr->iTable); |
drh | f9231c3 | 2018-12-31 21:43:55 +0000 | [diff] [blame] | 2745 | sqlite3VdbeJumpHere(v, addrOnce); |
drh | 2c04131 | 2018-12-24 02:34:49 +0000 | [diff] [blame] | 2746 | return; |
| 2747 | } |
| 2748 | |
| 2749 | /* Begin coding the subroutine */ |
| 2750 | ExprSetProperty(pExpr, EP_Subrtn); |
| 2751 | pExpr->y.sub.regReturn = ++pParse->nMem; |
| 2752 | pExpr->y.sub.iAddr = |
| 2753 | sqlite3VdbeAddOp2(v, OP_Integer, 0, pExpr->y.sub.regReturn) + 1; |
| 2754 | VdbeComment((v, "return address")); |
| 2755 | |
| 2756 | addrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v); |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 2757 | } |
| 2758 | |
drh | 85bcdce | 2018-12-23 21:27:29 +0000 | [diff] [blame] | 2759 | /* Check to see if this is a vector IN operator */ |
| 2760 | pLeft = pExpr->pLeft; |
| 2761 | nVal = sqlite3ExprVectorSize(pLeft); |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 2762 | |
drh | 85bcdce | 2018-12-23 21:27:29 +0000 | [diff] [blame] | 2763 | /* Construct the ephemeral table that will contain the content of |
| 2764 | ** RHS of the IN operator. |
| 2765 | */ |
drh | 2c04131 | 2018-12-24 02:34:49 +0000 | [diff] [blame] | 2766 | pExpr->iTable = iTab; |
drh | 50ef671 | 2019-02-22 23:29:56 +0000 | [diff] [blame] | 2767 | addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pExpr->iTable, nVal); |
drh | 2c04131 | 2018-12-24 02:34:49 +0000 | [diff] [blame] | 2768 | #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS |
| 2769 | if( ExprHasProperty(pExpr, EP_xIsSelect) ){ |
| 2770 | VdbeComment((v, "Result of SELECT %u", pExpr->x.pSelect->selId)); |
| 2771 | }else{ |
| 2772 | VdbeComment((v, "RHS of IN operator")); |
| 2773 | } |
| 2774 | #endif |
drh | 50ef671 | 2019-02-22 23:29:56 +0000 | [diff] [blame] | 2775 | pKeyInfo = sqlite3KeyInfoAlloc(pParse->db, nVal, 1); |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 2776 | |
drh | 85bcdce | 2018-12-23 21:27:29 +0000 | [diff] [blame] | 2777 | if( ExprHasProperty(pExpr, EP_xIsSelect) ){ |
| 2778 | /* Case 1: expr IN (SELECT ...) |
| 2779 | ** |
| 2780 | ** Generate code to write the results of the select into the temporary |
| 2781 | ** table allocated and opened above. |
| 2782 | */ |
| 2783 | Select *pSelect = pExpr->x.pSelect; |
| 2784 | ExprList *pEList = pSelect->pEList; |
drh | 1013c93 | 2008-01-06 00:25:21 +0000 | [diff] [blame] | 2785 | |
drh | 2c04131 | 2018-12-24 02:34:49 +0000 | [diff] [blame] | 2786 | ExplainQueryPlan((pParse, 1, "%sLIST SUBQUERY %d", |
| 2787 | addrOnce?"":"CORRELATED ", pSelect->selId |
drh | 85bcdce | 2018-12-23 21:27:29 +0000 | [diff] [blame] | 2788 | )); |
drh | 85bcdce | 2018-12-23 21:27:29 +0000 | [diff] [blame] | 2789 | /* If the LHS and RHS of the IN operator do not match, that |
| 2790 | ** error will have been caught long before we reach this point. */ |
| 2791 | if( ALWAYS(pEList->nExpr==nVal) ){ |
| 2792 | SelectDest dest; |
| 2793 | int i; |
drh | bd462bc | 2018-12-24 20:21:06 +0000 | [diff] [blame] | 2794 | sqlite3SelectDestInit(&dest, SRT_Set, iTab); |
drh | 85bcdce | 2018-12-23 21:27:29 +0000 | [diff] [blame] | 2795 | dest.zAffSdst = exprINAffinity(pParse, pExpr); |
| 2796 | pSelect->iLimit = 0; |
| 2797 | testcase( pSelect->selFlags & SF_Distinct ); |
| 2798 | testcase( pKeyInfo==0 ); /* Caused by OOM in sqlite3KeyInfoAlloc() */ |
| 2799 | if( sqlite3Select(pParse, pSelect, &dest) ){ |
| 2800 | sqlite3DbFree(pParse->db, dest.zAffSdst); |
| 2801 | sqlite3KeyInfoUnref(pKeyInfo); |
| 2802 | return; |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 2803 | } |
drh | 85bcdce | 2018-12-23 21:27:29 +0000 | [diff] [blame] | 2804 | sqlite3DbFree(pParse->db, dest.zAffSdst); |
| 2805 | assert( pKeyInfo!=0 ); /* OOM will cause exit after sqlite3Select() */ |
| 2806 | assert( pEList!=0 ); |
| 2807 | assert( pEList->nExpr>0 ); |
| 2808 | assert( sqlite3KeyInfoIsWriteable(pKeyInfo) ); |
| 2809 | for(i=0; i<nVal; i++){ |
| 2810 | Expr *p = sqlite3VectorFieldSubexpr(pLeft, i); |
| 2811 | pKeyInfo->aColl[i] = sqlite3BinaryCompareCollSeq( |
| 2812 | pParse, p, pEList->a[i].pExpr |
| 2813 | ); |
danielk1977 | 41a05b7 | 2008-10-02 13:50:55 +0000 | [diff] [blame] | 2814 | } |
drh | 85bcdce | 2018-12-23 21:27:29 +0000 | [diff] [blame] | 2815 | } |
| 2816 | }else if( ALWAYS(pExpr->x.pList!=0) ){ |
| 2817 | /* Case 2: expr IN (exprlist) |
| 2818 | ** |
| 2819 | ** For each expression, build an index key from the evaluation and |
| 2820 | ** store it in the temporary table. If <expr> is a column, then use |
| 2821 | ** that columns affinity when building index keys. If <expr> is not |
| 2822 | ** a column, use numeric affinity. |
| 2823 | */ |
| 2824 | char affinity; /* Affinity of the LHS of the IN */ |
| 2825 | int i; |
| 2826 | ExprList *pList = pExpr->x.pList; |
| 2827 | struct ExprList_item *pItem; |
dan | c324d44 | 2019-08-17 19:13:49 +0000 | [diff] [blame] | 2828 | int r1, r2; |
drh | 85bcdce | 2018-12-23 21:27:29 +0000 | [diff] [blame] | 2829 | affinity = sqlite3ExprAffinity(pLeft); |
drh | 96fb16e | 2019-08-06 14:37:24 +0000 | [diff] [blame] | 2830 | if( affinity<=SQLITE_AFF_NONE ){ |
drh | 85bcdce | 2018-12-23 21:27:29 +0000 | [diff] [blame] | 2831 | affinity = SQLITE_AFF_BLOB; |
| 2832 | } |
| 2833 | if( pKeyInfo ){ |
| 2834 | assert( sqlite3KeyInfoIsWriteable(pKeyInfo) ); |
| 2835 | pKeyInfo->aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 2836 | } |
| 2837 | |
drh | 85bcdce | 2018-12-23 21:27:29 +0000 | [diff] [blame] | 2838 | /* Loop through each expression in <exprlist>. */ |
| 2839 | r1 = sqlite3GetTempReg(pParse); |
| 2840 | r2 = sqlite3GetTempReg(pParse); |
drh | 85bcdce | 2018-12-23 21:27:29 +0000 | [diff] [blame] | 2841 | for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){ |
| 2842 | Expr *pE2 = pItem->pExpr; |
drh | 85bcdce | 2018-12-23 21:27:29 +0000 | [diff] [blame] | 2843 | |
| 2844 | /* If the expression is not constant then we will need to |
| 2845 | ** disable the test that was generated above that makes sure |
| 2846 | ** this code only executes once. Because for a non-constant |
| 2847 | ** expression we need to rerun this code each time. |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 2848 | */ |
drh | 2c04131 | 2018-12-24 02:34:49 +0000 | [diff] [blame] | 2849 | if( addrOnce && !sqlite3ExprIsConstant(pE2) ){ |
| 2850 | sqlite3VdbeChangeToNoop(v, addrOnce); |
dan | 7ac0e56 | 2019-05-18 21:22:25 +0000 | [diff] [blame] | 2851 | ExprClearProperty(pExpr, EP_Subrtn); |
drh | 2c04131 | 2018-12-24 02:34:49 +0000 | [diff] [blame] | 2852 | addrOnce = 0; |
drh | 85bcdce | 2018-12-23 21:27:29 +0000 | [diff] [blame] | 2853 | } |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 2854 | |
drh | 85bcdce | 2018-12-23 21:27:29 +0000 | [diff] [blame] | 2855 | /* Evaluate the expression and insert it into the temp table */ |
dan | c324d44 | 2019-08-17 19:13:49 +0000 | [diff] [blame] | 2856 | sqlite3ExprCode(pParse, pE2, r1); |
| 2857 | sqlite3VdbeAddOp4(v, OP_MakeRecord, r1, 1, r2, &affinity, 1); |
| 2858 | sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iTab, r2, r1, 1); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 2859 | } |
drh | 85bcdce | 2018-12-23 21:27:29 +0000 | [diff] [blame] | 2860 | sqlite3ReleaseTempReg(pParse, r1); |
| 2861 | sqlite3ReleaseTempReg(pParse, r2); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 2862 | } |
drh | 85bcdce | 2018-12-23 21:27:29 +0000 | [diff] [blame] | 2863 | if( pKeyInfo ){ |
| 2864 | sqlite3VdbeChangeP4(v, addr, (void *)pKeyInfo, P4_KEYINFO); |
| 2865 | } |
drh | 2c04131 | 2018-12-24 02:34:49 +0000 | [diff] [blame] | 2866 | if( addrOnce ){ |
| 2867 | sqlite3VdbeJumpHere(v, addrOnce); |
| 2868 | /* Subroutine return */ |
| 2869 | sqlite3VdbeAddOp1(v, OP_Return, pExpr->y.sub.regReturn); |
| 2870 | sqlite3VdbeChangeP1(v, pExpr->y.sub.iAddr-1, sqlite3VdbeCurrentAddr(v)-1); |
drh | 85bcdce | 2018-12-23 21:27:29 +0000 | [diff] [blame] | 2871 | } |
| 2872 | } |
| 2873 | #endif /* SQLITE_OMIT_SUBQUERY */ |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 2874 | |
drh | 85bcdce | 2018-12-23 21:27:29 +0000 | [diff] [blame] | 2875 | /* |
| 2876 | ** Generate code for scalar subqueries used as a subquery expression |
| 2877 | ** or EXISTS operator: |
| 2878 | ** |
| 2879 | ** (SELECT a FROM b) -- subquery |
| 2880 | ** EXISTS (SELECT a FROM b) -- EXISTS subquery |
| 2881 | ** |
| 2882 | ** The pExpr parameter is the SELECT or EXISTS operator to be coded. |
| 2883 | ** |
drh | d86fe44 | 2019-08-26 13:45:49 +0000 | [diff] [blame] | 2884 | ** Return the register that holds the result. For a multi-column SELECT, |
drh | 85bcdce | 2018-12-23 21:27:29 +0000 | [diff] [blame] | 2885 | ** the result is stored in a contiguous array of registers and the |
| 2886 | ** return value is the register of the left-most result column. |
| 2887 | ** Return 0 if an error occurs. |
| 2888 | */ |
| 2889 | #ifndef SQLITE_OMIT_SUBQUERY |
| 2890 | int sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){ |
drh | 2c04131 | 2018-12-24 02:34:49 +0000 | [diff] [blame] | 2891 | int addrOnce = 0; /* Address of OP_Once at top of subroutine */ |
drh | 85bcdce | 2018-12-23 21:27:29 +0000 | [diff] [blame] | 2892 | int rReg = 0; /* Register storing resulting */ |
| 2893 | Select *pSel; /* SELECT statement to encode */ |
| 2894 | SelectDest dest; /* How to deal with SELECT result */ |
| 2895 | int nReg; /* Registers to allocate */ |
| 2896 | Expr *pLimit; /* New limit expression */ |
drh | 2c04131 | 2018-12-24 02:34:49 +0000 | [diff] [blame] | 2897 | |
| 2898 | Vdbe *v = pParse->pVdbe; |
drh | 85bcdce | 2018-12-23 21:27:29 +0000 | [diff] [blame] | 2899 | assert( v!=0 ); |
drh | bd462bc | 2018-12-24 20:21:06 +0000 | [diff] [blame] | 2900 | testcase( pExpr->op==TK_EXISTS ); |
| 2901 | testcase( pExpr->op==TK_SELECT ); |
| 2902 | assert( pExpr->op==TK_EXISTS || pExpr->op==TK_SELECT ); |
| 2903 | assert( ExprHasProperty(pExpr, EP_xIsSelect) ); |
| 2904 | pSel = pExpr->x.pSelect; |
drh | 85bcdce | 2018-12-23 21:27:29 +0000 | [diff] [blame] | 2905 | |
drh | 5198ff5 | 2018-12-24 12:09:47 +0000 | [diff] [blame] | 2906 | /* The evaluation of the EXISTS/SELECT must be repeated every time it |
drh | 85bcdce | 2018-12-23 21:27:29 +0000 | [diff] [blame] | 2907 | ** is encountered if any of the following is true: |
| 2908 | ** |
| 2909 | ** * The right-hand side is a correlated subquery |
| 2910 | ** * The right-hand side is an expression list containing variables |
| 2911 | ** * We are inside a trigger |
| 2912 | ** |
| 2913 | ** If all of the above are false, then we can run this code just once |
| 2914 | ** save the results, and reuse the same result on subsequent invocations. |
| 2915 | */ |
| 2916 | if( !ExprHasProperty(pExpr, EP_VarSelect) ){ |
drh | 5198ff5 | 2018-12-24 12:09:47 +0000 | [diff] [blame] | 2917 | /* If this routine has already been coded, then invoke it as a |
| 2918 | ** subroutine. */ |
| 2919 | if( ExprHasProperty(pExpr, EP_Subrtn) ){ |
drh | bd462bc | 2018-12-24 20:21:06 +0000 | [diff] [blame] | 2920 | ExplainQueryPlan((pParse, 0, "REUSE SUBQUERY %d", pSel->selId)); |
drh | 5198ff5 | 2018-12-24 12:09:47 +0000 | [diff] [blame] | 2921 | sqlite3VdbeAddOp2(v, OP_Gosub, pExpr->y.sub.regReturn, |
| 2922 | pExpr->y.sub.iAddr); |
| 2923 | return pExpr->iTable; |
| 2924 | } |
| 2925 | |
| 2926 | /* Begin coding the subroutine */ |
| 2927 | ExprSetProperty(pExpr, EP_Subrtn); |
| 2928 | pExpr->y.sub.regReturn = ++pParse->nMem; |
| 2929 | pExpr->y.sub.iAddr = |
| 2930 | sqlite3VdbeAddOp2(v, OP_Integer, 0, pExpr->y.sub.regReturn) + 1; |
| 2931 | VdbeComment((v, "return address")); |
| 2932 | |
drh | 2c04131 | 2018-12-24 02:34:49 +0000 | [diff] [blame] | 2933 | addrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v); |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 2934 | } |
drh | 85bcdce | 2018-12-23 21:27:29 +0000 | [diff] [blame] | 2935 | |
| 2936 | /* For a SELECT, generate code to put the values for all columns of |
| 2937 | ** the first row into an array of registers and return the index of |
| 2938 | ** the first register. |
| 2939 | ** |
| 2940 | ** If this is an EXISTS, write an integer 0 (not exists) or 1 (exists) |
| 2941 | ** into a register and return that register number. |
| 2942 | ** |
| 2943 | ** In both cases, the query is augmented with "LIMIT 1". Any |
| 2944 | ** preexisting limit is discarded in place of the new LIMIT 1. |
| 2945 | */ |
drh | bd462bc | 2018-12-24 20:21:06 +0000 | [diff] [blame] | 2946 | ExplainQueryPlan((pParse, 1, "%sSCALAR SUBQUERY %d", |
| 2947 | addrOnce?"":"CORRELATED ", pSel->selId)); |
drh | 85bcdce | 2018-12-23 21:27:29 +0000 | [diff] [blame] | 2948 | nReg = pExpr->op==TK_SELECT ? pSel->pEList->nExpr : 1; |
| 2949 | sqlite3SelectDestInit(&dest, 0, pParse->nMem+1); |
| 2950 | pParse->nMem += nReg; |
| 2951 | if( pExpr->op==TK_SELECT ){ |
| 2952 | dest.eDest = SRT_Mem; |
| 2953 | dest.iSdst = dest.iSDParm; |
| 2954 | dest.nSdst = nReg; |
| 2955 | sqlite3VdbeAddOp3(v, OP_Null, 0, dest.iSDParm, dest.iSDParm+nReg-1); |
| 2956 | VdbeComment((v, "Init subquery result")); |
| 2957 | }else{ |
| 2958 | dest.eDest = SRT_Exists; |
| 2959 | sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iSDParm); |
| 2960 | VdbeComment((v, "Init EXISTS result")); |
| 2961 | } |
| 2962 | pLimit = sqlite3ExprAlloc(pParse->db, TK_INTEGER,&sqlite3IntTokens[1], 0); |
| 2963 | if( pSel->pLimit ){ |
| 2964 | sqlite3ExprDelete(pParse->db, pSel->pLimit->pLeft); |
| 2965 | pSel->pLimit->pLeft = pLimit; |
| 2966 | }else{ |
| 2967 | pSel->pLimit = sqlite3PExpr(pParse, TK_LIMIT, pLimit, 0); |
| 2968 | } |
| 2969 | pSel->iLimit = 0; |
| 2970 | if( sqlite3Select(pParse, pSel, &dest) ){ |
| 2971 | return 0; |
| 2972 | } |
drh | 2c04131 | 2018-12-24 02:34:49 +0000 | [diff] [blame] | 2973 | pExpr->iTable = rReg = dest.iSDParm; |
drh | 85bcdce | 2018-12-23 21:27:29 +0000 | [diff] [blame] | 2974 | ExprSetVVAProperty(pExpr, EP_NoReduce); |
drh | 2c04131 | 2018-12-24 02:34:49 +0000 | [diff] [blame] | 2975 | if( addrOnce ){ |
| 2976 | sqlite3VdbeJumpHere(v, addrOnce); |
danielk1977 | fc97606 | 2007-05-10 10:46:56 +0000 | [diff] [blame] | 2977 | |
drh | 5198ff5 | 2018-12-24 12:09:47 +0000 | [diff] [blame] | 2978 | /* Subroutine return */ |
| 2979 | sqlite3VdbeAddOp1(v, OP_Return, pExpr->y.sub.regReturn); |
| 2980 | sqlite3VdbeChangeP1(v, pExpr->y.sub.iAddr-1, sqlite3VdbeCurrentAddr(v)-1); |
| 2981 | } |
drh | 2c04131 | 2018-12-24 02:34:49 +0000 | [diff] [blame] | 2982 | |
drh | 1450bc6 | 2009-10-30 13:25:56 +0000 | [diff] [blame] | 2983 | return rReg; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 2984 | } |
drh | 51522cd | 2005-01-20 13:36:19 +0000 | [diff] [blame] | 2985 | #endif /* SQLITE_OMIT_SUBQUERY */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 2986 | |
drh | e3365e6 | 2009-11-12 17:52:24 +0000 | [diff] [blame] | 2987 | #ifndef SQLITE_OMIT_SUBQUERY |
| 2988 | /* |
dan | 7b35a77 | 2016-07-28 19:47:15 +0000 | [diff] [blame] | 2989 | ** Expr pIn is an IN(...) expression. This function checks that the |
| 2990 | ** sub-select on the RHS of the IN() operator has the same number of |
| 2991 | ** columns as the vector on the LHS. Or, if the RHS of the IN() is not |
| 2992 | ** a sub-query, that the LHS is a vector of size 1. |
| 2993 | */ |
| 2994 | int sqlite3ExprCheckIN(Parse *pParse, Expr *pIn){ |
| 2995 | int nVector = sqlite3ExprVectorSize(pIn->pLeft); |
| 2996 | if( (pIn->flags & EP_xIsSelect) ){ |
| 2997 | if( nVector!=pIn->x.pSelect->pEList->nExpr ){ |
| 2998 | sqlite3SubselectError(pParse, pIn->x.pSelect->pEList->nExpr, nVector); |
| 2999 | return 1; |
| 3000 | } |
| 3001 | }else if( nVector!=1 ){ |
dan | 44c5604 | 2016-12-07 15:38:37 +0000 | [diff] [blame] | 3002 | sqlite3VectorErrorMsg(pParse, pIn->pLeft); |
dan | 7b35a77 | 2016-07-28 19:47:15 +0000 | [diff] [blame] | 3003 | return 1; |
| 3004 | } |
| 3005 | return 0; |
| 3006 | } |
| 3007 | #endif |
| 3008 | |
| 3009 | #ifndef SQLITE_OMIT_SUBQUERY |
| 3010 | /* |
drh | e3365e6 | 2009-11-12 17:52:24 +0000 | [diff] [blame] | 3011 | ** Generate code for an IN expression. |
| 3012 | ** |
| 3013 | ** x IN (SELECT ...) |
| 3014 | ** x IN (value, value, ...) |
| 3015 | ** |
drh | ecb87ac | 2016-08-25 15:46:25 +0000 | [diff] [blame] | 3016 | ** The left-hand side (LHS) is a scalar or vector expression. The |
drh | e347d3e | 2016-08-25 21:14:34 +0000 | [diff] [blame] | 3017 | ** right-hand side (RHS) is an array of zero or more scalar values, or a |
| 3018 | ** subquery. If the RHS is a subquery, the number of result columns must |
| 3019 | ** match the number of columns in the vector on the LHS. If the RHS is |
| 3020 | ** a list of values, the LHS must be a scalar. |
| 3021 | ** |
| 3022 | ** The IN operator is true if the LHS value is contained within the RHS. |
| 3023 | ** The result is false if the LHS is definitely not in the RHS. The |
| 3024 | ** result is NULL if the presence of the LHS in the RHS cannot be |
| 3025 | ** determined due to NULLs. |
drh | e3365e6 | 2009-11-12 17:52:24 +0000 | [diff] [blame] | 3026 | ** |
drh | 6be515e | 2014-08-01 21:00:53 +0000 | [diff] [blame] | 3027 | ** This routine generates code that jumps to destIfFalse if the LHS is not |
drh | e3365e6 | 2009-11-12 17:52:24 +0000 | [diff] [blame] | 3028 | ** contained within the RHS. If due to NULLs we cannot determine if the LHS |
| 3029 | ** is contained in the RHS then jump to destIfNull. If the LHS is contained |
| 3030 | ** within the RHS then fall through. |
drh | ecb87ac | 2016-08-25 15:46:25 +0000 | [diff] [blame] | 3031 | ** |
| 3032 | ** See the separate in-operator.md documentation file in the canonical |
| 3033 | ** SQLite source tree for additional information. |
drh | e3365e6 | 2009-11-12 17:52:24 +0000 | [diff] [blame] | 3034 | */ |
| 3035 | static void sqlite3ExprCodeIN( |
| 3036 | Parse *pParse, /* Parsing and code generating context */ |
| 3037 | Expr *pExpr, /* The IN expression */ |
| 3038 | int destIfFalse, /* Jump here if LHS is not contained in the RHS */ |
| 3039 | int destIfNull /* Jump here if the results are unknown due to NULLs */ |
| 3040 | ){ |
| 3041 | int rRhsHasNull = 0; /* Register that is true if RHS contains NULL values */ |
drh | e3365e6 | 2009-11-12 17:52:24 +0000 | [diff] [blame] | 3042 | int eType; /* Type of the RHS */ |
drh | e347d3e | 2016-08-25 21:14:34 +0000 | [diff] [blame] | 3043 | int rLhs; /* Register(s) holding the LHS values */ |
| 3044 | int rLhsOrig; /* LHS values prior to reordering by aiMap[] */ |
drh | e3365e6 | 2009-11-12 17:52:24 +0000 | [diff] [blame] | 3045 | Vdbe *v; /* Statement under construction */ |
dan | ba00e30 | 2016-07-23 20:24:06 +0000 | [diff] [blame] | 3046 | int *aiMap = 0; /* Map from vector field to index column */ |
| 3047 | char *zAff = 0; /* Affinity string for comparisons */ |
drh | e347d3e | 2016-08-25 21:14:34 +0000 | [diff] [blame] | 3048 | int nVector; /* Size of vectors for this IN operator */ |
| 3049 | int iDummy; /* Dummy parameter to exprCodeVector() */ |
| 3050 | Expr *pLeft; /* The LHS of the IN operator */ |
| 3051 | int i; /* loop counter */ |
| 3052 | int destStep2; /* Where to jump when NULLs seen in step 2 */ |
| 3053 | int destStep6 = 0; /* Start of code for Step 6 */ |
| 3054 | int addrTruthOp; /* Address of opcode that determines the IN is true */ |
| 3055 | int destNotNull; /* Jump here if a comparison is not true in step 6 */ |
| 3056 | int addrTop; /* Top of the step-6 loop */ |
drh | 2c04131 | 2018-12-24 02:34:49 +0000 | [diff] [blame] | 3057 | int iTab = 0; /* Index to use */ |
drh | e3365e6 | 2009-11-12 17:52:24 +0000 | [diff] [blame] | 3058 | |
drh | e347d3e | 2016-08-25 21:14:34 +0000 | [diff] [blame] | 3059 | pLeft = pExpr->pLeft; |
dan | 7b35a77 | 2016-07-28 19:47:15 +0000 | [diff] [blame] | 3060 | if( sqlite3ExprCheckIN(pParse, pExpr) ) return; |
dan | 553168c | 2016-08-01 20:14:31 +0000 | [diff] [blame] | 3061 | zAff = exprINAffinity(pParse, pExpr); |
dan | ba00e30 | 2016-07-23 20:24:06 +0000 | [diff] [blame] | 3062 | nVector = sqlite3ExprVectorSize(pExpr->pLeft); |
| 3063 | aiMap = (int*)sqlite3DbMallocZero( |
| 3064 | pParse->db, nVector*(sizeof(int) + sizeof(char)) + 1 |
| 3065 | ); |
drh | e347d3e | 2016-08-25 21:14:34 +0000 | [diff] [blame] | 3066 | if( pParse->db->mallocFailed ) goto sqlite3ExprCodeIN_oom_error; |
dan | 7b35a77 | 2016-07-28 19:47:15 +0000 | [diff] [blame] | 3067 | |
dan | ba00e30 | 2016-07-23 20:24:06 +0000 | [diff] [blame] | 3068 | /* Attempt to compute the RHS. After this step, if anything other than |
drh | 2c04131 | 2018-12-24 02:34:49 +0000 | [diff] [blame] | 3069 | ** IN_INDEX_NOOP is returned, the table opened with cursor iTab |
dan | ba00e30 | 2016-07-23 20:24:06 +0000 | [diff] [blame] | 3070 | ** contains the values that make up the RHS. If IN_INDEX_NOOP is returned, |
| 3071 | ** the RHS has not yet been coded. */ |
drh | e3365e6 | 2009-11-12 17:52:24 +0000 | [diff] [blame] | 3072 | v = pParse->pVdbe; |
| 3073 | assert( v!=0 ); /* OOM detected prior to this routine */ |
| 3074 | VdbeNoopComment((v, "begin IN expr")); |
drh | bb53ecb | 2014-08-02 21:03:33 +0000 | [diff] [blame] | 3075 | eType = sqlite3FindInIndex(pParse, pExpr, |
| 3076 | IN_INDEX_MEMBERSHIP | IN_INDEX_NOOP_OK, |
drh | 2c04131 | 2018-12-24 02:34:49 +0000 | [diff] [blame] | 3077 | destIfFalse==destIfNull ? 0 : &rRhsHasNull, |
| 3078 | aiMap, &iTab); |
drh | e3365e6 | 2009-11-12 17:52:24 +0000 | [diff] [blame] | 3079 | |
dan | ba00e30 | 2016-07-23 20:24:06 +0000 | [diff] [blame] | 3080 | assert( pParse->nErr || nVector==1 || eType==IN_INDEX_EPH |
| 3081 | || eType==IN_INDEX_INDEX_ASC || eType==IN_INDEX_INDEX_DESC |
| 3082 | ); |
drh | ecb87ac | 2016-08-25 15:46:25 +0000 | [diff] [blame] | 3083 | #ifdef SQLITE_DEBUG |
| 3084 | /* Confirm that aiMap[] contains nVector integer values between 0 and |
| 3085 | ** nVector-1. */ |
| 3086 | for(i=0; i<nVector; i++){ |
| 3087 | int j, cnt; |
| 3088 | for(cnt=j=0; j<nVector; j++) if( aiMap[j]==i ) cnt++; |
| 3089 | assert( cnt==1 ); |
| 3090 | } |
| 3091 | #endif |
drh | e3365e6 | 2009-11-12 17:52:24 +0000 | [diff] [blame] | 3092 | |
dan | ba00e30 | 2016-07-23 20:24:06 +0000 | [diff] [blame] | 3093 | /* Code the LHS, the <expr> from "<expr> IN (...)". If the LHS is a |
| 3094 | ** vector, then it is stored in an array of nVector registers starting |
| 3095 | ** at r1. |
drh | e347d3e | 2016-08-25 21:14:34 +0000 | [diff] [blame] | 3096 | ** |
| 3097 | ** sqlite3FindInIndex() might have reordered the fields of the LHS vector |
| 3098 | ** so that the fields are in the same order as an existing index. The |
| 3099 | ** aiMap[] array contains a mapping from the original LHS field order to |
| 3100 | ** the field order that matches the RHS index. |
drh | e3365e6 | 2009-11-12 17:52:24 +0000 | [diff] [blame] | 3101 | */ |
drh | e347d3e | 2016-08-25 21:14:34 +0000 | [diff] [blame] | 3102 | rLhsOrig = exprCodeVector(pParse, pLeft, &iDummy); |
| 3103 | for(i=0; i<nVector && aiMap[i]==i; i++){} /* Are LHS fields reordered? */ |
drh | ecb87ac | 2016-08-25 15:46:25 +0000 | [diff] [blame] | 3104 | if( i==nVector ){ |
drh | e347d3e | 2016-08-25 21:14:34 +0000 | [diff] [blame] | 3105 | /* LHS fields are not reordered */ |
| 3106 | rLhs = rLhsOrig; |
drh | ecb87ac | 2016-08-25 15:46:25 +0000 | [diff] [blame] | 3107 | }else{ |
| 3108 | /* Need to reorder the LHS fields according to aiMap */ |
drh | e347d3e | 2016-08-25 21:14:34 +0000 | [diff] [blame] | 3109 | rLhs = sqlite3GetTempRange(pParse, nVector); |
drh | ecb87ac | 2016-08-25 15:46:25 +0000 | [diff] [blame] | 3110 | for(i=0; i<nVector; i++){ |
drh | e347d3e | 2016-08-25 21:14:34 +0000 | [diff] [blame] | 3111 | sqlite3VdbeAddOp3(v, OP_Copy, rLhsOrig+i, rLhs+aiMap[i], 0); |
drh | ecb87ac | 2016-08-25 15:46:25 +0000 | [diff] [blame] | 3112 | } |
dan | ba00e30 | 2016-07-23 20:24:06 +0000 | [diff] [blame] | 3113 | } |
drh | e3365e6 | 2009-11-12 17:52:24 +0000 | [diff] [blame] | 3114 | |
drh | bb53ecb | 2014-08-02 21:03:33 +0000 | [diff] [blame] | 3115 | /* If sqlite3FindInIndex() did not find or create an index that is |
| 3116 | ** suitable for evaluating the IN operator, then evaluate using a |
| 3117 | ** sequence of comparisons. |
drh | e347d3e | 2016-08-25 21:14:34 +0000 | [diff] [blame] | 3118 | ** |
| 3119 | ** This is step (1) in the in-operator.md optimized algorithm. |
drh | 094430e | 2010-07-14 18:24:06 +0000 | [diff] [blame] | 3120 | */ |
drh | bb53ecb | 2014-08-02 21:03:33 +0000 | [diff] [blame] | 3121 | if( eType==IN_INDEX_NOOP ){ |
| 3122 | ExprList *pList = pExpr->x.pList; |
| 3123 | CollSeq *pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft); |
drh | ec4ccdb | 2018-12-29 02:26:59 +0000 | [diff] [blame] | 3124 | int labelOk = sqlite3VdbeMakeLabel(pParse); |
drh | bb53ecb | 2014-08-02 21:03:33 +0000 | [diff] [blame] | 3125 | int r2, regToFree; |
| 3126 | int regCkNull = 0; |
| 3127 | int ii; |
| 3128 | assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); |
drh | bb53ecb | 2014-08-02 21:03:33 +0000 | [diff] [blame] | 3129 | if( destIfNull!=destIfFalse ){ |
| 3130 | regCkNull = sqlite3GetTempReg(pParse); |
drh | e347d3e | 2016-08-25 21:14:34 +0000 | [diff] [blame] | 3131 | sqlite3VdbeAddOp3(v, OP_BitAnd, rLhs, rLhs, regCkNull); |
drh | bb53ecb | 2014-08-02 21:03:33 +0000 | [diff] [blame] | 3132 | } |
| 3133 | for(ii=0; ii<pList->nExpr; ii++){ |
| 3134 | r2 = sqlite3ExprCodeTemp(pParse, pList->a[ii].pExpr, ®ToFree); |
drh | a976979 | 2014-08-04 16:39:39 +0000 | [diff] [blame] | 3135 | if( regCkNull && sqlite3ExprCanBeNull(pList->a[ii].pExpr) ){ |
drh | bb53ecb | 2014-08-02 21:03:33 +0000 | [diff] [blame] | 3136 | sqlite3VdbeAddOp3(v, OP_BitAnd, regCkNull, r2, regCkNull); |
| 3137 | } |
| 3138 | if( ii<pList->nExpr-1 || destIfNull!=destIfFalse ){ |
drh | e347d3e | 2016-08-25 21:14:34 +0000 | [diff] [blame] | 3139 | sqlite3VdbeAddOp4(v, OP_Eq, rLhs, labelOk, r2, |
drh | 4336b0e | 2014-08-05 00:53:51 +0000 | [diff] [blame] | 3140 | (void*)pColl, P4_COLLSEQ); |
| 3141 | VdbeCoverageIf(v, ii<pList->nExpr-1); |
| 3142 | VdbeCoverageIf(v, ii==pList->nExpr-1); |
dan | ba00e30 | 2016-07-23 20:24:06 +0000 | [diff] [blame] | 3143 | sqlite3VdbeChangeP5(v, zAff[0]); |
drh | bb53ecb | 2014-08-02 21:03:33 +0000 | [diff] [blame] | 3144 | }else{ |
| 3145 | assert( destIfNull==destIfFalse ); |
drh | e347d3e | 2016-08-25 21:14:34 +0000 | [diff] [blame] | 3146 | sqlite3VdbeAddOp4(v, OP_Ne, rLhs, destIfFalse, r2, |
drh | bb53ecb | 2014-08-02 21:03:33 +0000 | [diff] [blame] | 3147 | (void*)pColl, P4_COLLSEQ); VdbeCoverage(v); |
dan | ba00e30 | 2016-07-23 20:24:06 +0000 | [diff] [blame] | 3148 | sqlite3VdbeChangeP5(v, zAff[0] | SQLITE_JUMPIFNULL); |
drh | bb53ecb | 2014-08-02 21:03:33 +0000 | [diff] [blame] | 3149 | } |
| 3150 | sqlite3ReleaseTempReg(pParse, regToFree); |
| 3151 | } |
| 3152 | if( regCkNull ){ |
| 3153 | sqlite3VdbeAddOp2(v, OP_IsNull, regCkNull, destIfNull); VdbeCoverage(v); |
drh | 076e85f | 2015-09-03 13:46:12 +0000 | [diff] [blame] | 3154 | sqlite3VdbeGoto(v, destIfFalse); |
drh | bb53ecb | 2014-08-02 21:03:33 +0000 | [diff] [blame] | 3155 | } |
| 3156 | sqlite3VdbeResolveLabel(v, labelOk); |
| 3157 | sqlite3ReleaseTempReg(pParse, regCkNull); |
drh | e347d3e | 2016-08-25 21:14:34 +0000 | [diff] [blame] | 3158 | goto sqlite3ExprCodeIN_finished; |
| 3159 | } |
| 3160 | |
| 3161 | /* Step 2: Check to see if the LHS contains any NULL columns. If the |
| 3162 | ** LHS does contain NULLs then the result must be either FALSE or NULL. |
| 3163 | ** We will then skip the binary search of the RHS. |
| 3164 | */ |
| 3165 | if( destIfNull==destIfFalse ){ |
| 3166 | destStep2 = destIfFalse; |
drh | 094430e | 2010-07-14 18:24:06 +0000 | [diff] [blame] | 3167 | }else{ |
drh | ec4ccdb | 2018-12-29 02:26:59 +0000 | [diff] [blame] | 3168 | destStep2 = destStep6 = sqlite3VdbeMakeLabel(pParse); |
drh | e347d3e | 2016-08-25 21:14:34 +0000 | [diff] [blame] | 3169 | } |
| 3170 | for(i=0; i<nVector; i++){ |
| 3171 | Expr *p = sqlite3VectorFieldSubexpr(pExpr->pLeft, i); |
| 3172 | if( sqlite3ExprCanBeNull(p) ){ |
| 3173 | sqlite3VdbeAddOp2(v, OP_IsNull, rLhs+i, destStep2); |
dan | d49fd4e | 2016-07-27 19:33:04 +0000 | [diff] [blame] | 3174 | VdbeCoverage(v); |
drh | e3365e6 | 2009-11-12 17:52:24 +0000 | [diff] [blame] | 3175 | } |
drh | e3365e6 | 2009-11-12 17:52:24 +0000 | [diff] [blame] | 3176 | } |
drh | e347d3e | 2016-08-25 21:14:34 +0000 | [diff] [blame] | 3177 | |
| 3178 | /* Step 3. The LHS is now known to be non-NULL. Do the binary search |
| 3179 | ** of the RHS using the LHS as a probe. If found, the result is |
| 3180 | ** true. |
| 3181 | */ |
| 3182 | if( eType==IN_INDEX_ROWID ){ |
| 3183 | /* In this case, the RHS is the ROWID of table b-tree and so we also |
| 3184 | ** know that the RHS is non-NULL. Hence, we combine steps 3 and 4 |
| 3185 | ** into a single opcode. */ |
drh | 2c04131 | 2018-12-24 02:34:49 +0000 | [diff] [blame] | 3186 | sqlite3VdbeAddOp3(v, OP_SeekRowid, iTab, destIfFalse, rLhs); |
drh | e347d3e | 2016-08-25 21:14:34 +0000 | [diff] [blame] | 3187 | VdbeCoverage(v); |
| 3188 | addrTruthOp = sqlite3VdbeAddOp0(v, OP_Goto); /* Return True */ |
| 3189 | }else{ |
| 3190 | sqlite3VdbeAddOp4(v, OP_Affinity, rLhs, nVector, 0, zAff, nVector); |
| 3191 | if( destIfFalse==destIfNull ){ |
| 3192 | /* Combine Step 3 and Step 5 into a single opcode */ |
drh | 2c04131 | 2018-12-24 02:34:49 +0000 | [diff] [blame] | 3193 | sqlite3VdbeAddOp4Int(v, OP_NotFound, iTab, destIfFalse, |
drh | e347d3e | 2016-08-25 21:14:34 +0000 | [diff] [blame] | 3194 | rLhs, nVector); VdbeCoverage(v); |
| 3195 | goto sqlite3ExprCodeIN_finished; |
| 3196 | } |
| 3197 | /* Ordinary Step 3, for the case where FALSE and NULL are distinct */ |
drh | 2c04131 | 2018-12-24 02:34:49 +0000 | [diff] [blame] | 3198 | addrTruthOp = sqlite3VdbeAddOp4Int(v, OP_Found, iTab, 0, |
drh | e347d3e | 2016-08-25 21:14:34 +0000 | [diff] [blame] | 3199 | rLhs, nVector); VdbeCoverage(v); |
| 3200 | } |
| 3201 | |
| 3202 | /* Step 4. If the RHS is known to be non-NULL and we did not find |
| 3203 | ** an match on the search above, then the result must be FALSE. |
| 3204 | */ |
| 3205 | if( rRhsHasNull && nVector==1 ){ |
| 3206 | sqlite3VdbeAddOp2(v, OP_NotNull, rRhsHasNull, destIfFalse); |
| 3207 | VdbeCoverage(v); |
| 3208 | } |
| 3209 | |
| 3210 | /* Step 5. If we do not care about the difference between NULL and |
| 3211 | ** FALSE, then just return false. |
| 3212 | */ |
| 3213 | if( destIfFalse==destIfNull ) sqlite3VdbeGoto(v, destIfFalse); |
| 3214 | |
| 3215 | /* Step 6: Loop through rows of the RHS. Compare each row to the LHS. |
| 3216 | ** If any comparison is NULL, then the result is NULL. If all |
| 3217 | ** comparisons are FALSE then the final result is FALSE. |
| 3218 | ** |
| 3219 | ** For a scalar LHS, it is sufficient to check just the first row |
| 3220 | ** of the RHS. |
| 3221 | */ |
| 3222 | if( destStep6 ) sqlite3VdbeResolveLabel(v, destStep6); |
drh | 2c04131 | 2018-12-24 02:34:49 +0000 | [diff] [blame] | 3223 | addrTop = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, destIfFalse); |
drh | e347d3e | 2016-08-25 21:14:34 +0000 | [diff] [blame] | 3224 | VdbeCoverage(v); |
| 3225 | if( nVector>1 ){ |
drh | ec4ccdb | 2018-12-29 02:26:59 +0000 | [diff] [blame] | 3226 | destNotNull = sqlite3VdbeMakeLabel(pParse); |
drh | e347d3e | 2016-08-25 21:14:34 +0000 | [diff] [blame] | 3227 | }else{ |
| 3228 | /* For nVector==1, combine steps 6 and 7 by immediately returning |
| 3229 | ** FALSE if the first comparison is not NULL */ |
| 3230 | destNotNull = destIfFalse; |
| 3231 | } |
| 3232 | for(i=0; i<nVector; i++){ |
| 3233 | Expr *p; |
| 3234 | CollSeq *pColl; |
| 3235 | int r3 = sqlite3GetTempReg(pParse); |
| 3236 | p = sqlite3VectorFieldSubexpr(pLeft, i); |
| 3237 | pColl = sqlite3ExprCollSeq(pParse, p); |
drh | 2c04131 | 2018-12-24 02:34:49 +0000 | [diff] [blame] | 3238 | sqlite3VdbeAddOp3(v, OP_Column, iTab, i, r3); |
drh | e347d3e | 2016-08-25 21:14:34 +0000 | [diff] [blame] | 3239 | sqlite3VdbeAddOp4(v, OP_Ne, rLhs+i, destNotNull, r3, |
| 3240 | (void*)pColl, P4_COLLSEQ); |
| 3241 | VdbeCoverage(v); |
| 3242 | sqlite3ReleaseTempReg(pParse, r3); |
| 3243 | } |
| 3244 | sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfNull); |
| 3245 | if( nVector>1 ){ |
| 3246 | sqlite3VdbeResolveLabel(v, destNotNull); |
drh | 2c04131 | 2018-12-24 02:34:49 +0000 | [diff] [blame] | 3247 | sqlite3VdbeAddOp2(v, OP_Next, iTab, addrTop+1); |
drh | e347d3e | 2016-08-25 21:14:34 +0000 | [diff] [blame] | 3248 | VdbeCoverage(v); |
| 3249 | |
| 3250 | /* Step 7: If we reach this point, we know that the result must |
| 3251 | ** be false. */ |
| 3252 | sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfFalse); |
| 3253 | } |
| 3254 | |
| 3255 | /* Jumps here in order to return true. */ |
| 3256 | sqlite3VdbeJumpHere(v, addrTruthOp); |
| 3257 | |
| 3258 | sqlite3ExprCodeIN_finished: |
| 3259 | if( rLhs!=rLhsOrig ) sqlite3ReleaseTempReg(pParse, rLhs); |
drh | ecb87ac | 2016-08-25 15:46:25 +0000 | [diff] [blame] | 3260 | VdbeComment((v, "end IN expr")); |
drh | e347d3e | 2016-08-25 21:14:34 +0000 | [diff] [blame] | 3261 | sqlite3ExprCodeIN_oom_error: |
dan | ba00e30 | 2016-07-23 20:24:06 +0000 | [diff] [blame] | 3262 | sqlite3DbFree(pParse->db, aiMap); |
dan | 553168c | 2016-08-01 20:14:31 +0000 | [diff] [blame] | 3263 | sqlite3DbFree(pParse->db, zAff); |
drh | e3365e6 | 2009-11-12 17:52:24 +0000 | [diff] [blame] | 3264 | } |
| 3265 | #endif /* SQLITE_OMIT_SUBQUERY */ |
| 3266 | |
drh | 13573c7 | 2010-01-12 17:04:07 +0000 | [diff] [blame] | 3267 | #ifndef SQLITE_OMIT_FLOATING_POINT |
drh | 598f134 | 2007-10-23 15:39:45 +0000 | [diff] [blame] | 3268 | /* |
| 3269 | ** Generate an instruction that will put the floating point |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3270 | ** value described by z[0..n-1] into register iMem. |
drh | 0cf19ed | 2007-10-23 18:55:48 +0000 | [diff] [blame] | 3271 | ** |
| 3272 | ** The z[] string will probably not be zero-terminated. But the |
| 3273 | ** z[n] character is guaranteed to be something that does not look |
| 3274 | ** like the continuation of the number. |
drh | 598f134 | 2007-10-23 15:39:45 +0000 | [diff] [blame] | 3275 | */ |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 3276 | static void codeReal(Vdbe *v, const char *z, int negateFlag, int iMem){ |
drh | fd773cf | 2009-05-29 14:39:07 +0000 | [diff] [blame] | 3277 | if( ALWAYS(z!=0) ){ |
drh | 598f134 | 2007-10-23 15:39:45 +0000 | [diff] [blame] | 3278 | double value; |
drh | 9339da1 | 2010-09-30 00:50:49 +0000 | [diff] [blame] | 3279 | sqlite3AtoF(z, &value, sqlite3Strlen30(z), SQLITE_UTF8); |
drh | d001516 | 2009-08-21 13:22:25 +0000 | [diff] [blame] | 3280 | assert( !sqlite3IsNaN(value) ); /* The new AtoF never returns NaN */ |
| 3281 | if( negateFlag ) value = -value; |
drh | 97bae79 | 2015-06-05 15:59:57 +0000 | [diff] [blame] | 3282 | sqlite3VdbeAddOp4Dup8(v, OP_Real, 0, iMem, 0, (u8*)&value, P4_REAL); |
drh | 598f134 | 2007-10-23 15:39:45 +0000 | [diff] [blame] | 3283 | } |
| 3284 | } |
drh | 13573c7 | 2010-01-12 17:04:07 +0000 | [diff] [blame] | 3285 | #endif |
drh | 598f134 | 2007-10-23 15:39:45 +0000 | [diff] [blame] | 3286 | |
| 3287 | |
| 3288 | /* |
drh | fec19aa | 2004-05-19 20:41:03 +0000 | [diff] [blame] | 3289 | ** Generate an instruction that will put the integer describe by |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3290 | ** text z[0..n-1] into register iMem. |
drh | 0cf19ed | 2007-10-23 18:55:48 +0000 | [diff] [blame] | 3291 | ** |
shaneh | 5f1d6b6 | 2010-09-30 16:51:25 +0000 | [diff] [blame] | 3292 | ** Expr.u.zToken is always UTF8 and zero-terminated. |
drh | fec19aa | 2004-05-19 20:41:03 +0000 | [diff] [blame] | 3293 | */ |
drh | 13573c7 | 2010-01-12 17:04:07 +0000 | [diff] [blame] | 3294 | static void codeInteger(Parse *pParse, Expr *pExpr, int negFlag, int iMem){ |
| 3295 | Vdbe *v = pParse->pVdbe; |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 3296 | if( pExpr->flags & EP_IntValue ){ |
drh | 33e619f | 2009-05-28 01:00:55 +0000 | [diff] [blame] | 3297 | int i = pExpr->u.iValue; |
drh | d50ffc4 | 2011-03-08 02:38:28 +0000 | [diff] [blame] | 3298 | assert( i>=0 ); |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 3299 | if( negFlag ) i = -i; |
| 3300 | sqlite3VdbeAddOp2(v, OP_Integer, i, iMem); |
drh | fd773cf | 2009-05-29 14:39:07 +0000 | [diff] [blame] | 3301 | }else{ |
shaneh | 5f1d6b6 | 2010-09-30 16:51:25 +0000 | [diff] [blame] | 3302 | int c; |
| 3303 | i64 value; |
drh | fd773cf | 2009-05-29 14:39:07 +0000 | [diff] [blame] | 3304 | const char *z = pExpr->u.zToken; |
| 3305 | assert( z!=0 ); |
drh | 9296c18 | 2014-07-23 13:40:49 +0000 | [diff] [blame] | 3306 | c = sqlite3DecOrHexToI64(z, &value); |
drh | 84d4f1a | 2017-09-20 10:47:10 +0000 | [diff] [blame] | 3307 | if( (c==3 && !negFlag) || (c==2) || (negFlag && value==SMALLEST_INT64)){ |
drh | 13573c7 | 2010-01-12 17:04:07 +0000 | [diff] [blame] | 3308 | #ifdef SQLITE_OMIT_FLOATING_POINT |
| 3309 | sqlite3ErrorMsg(pParse, "oversized integer: %s%s", negFlag ? "-" : "", z); |
| 3310 | #else |
drh | 1b7ddc5 | 2014-07-23 14:52:05 +0000 | [diff] [blame] | 3311 | #ifndef SQLITE_OMIT_HEX_INTEGER |
drh | 9296c18 | 2014-07-23 13:40:49 +0000 | [diff] [blame] | 3312 | if( sqlite3_strnicmp(z,"0x",2)==0 ){ |
drh | 77320ea | 2016-11-30 14:47:37 +0000 | [diff] [blame] | 3313 | sqlite3ErrorMsg(pParse, "hex literal too big: %s%s", negFlag?"-":"",z); |
drh | 1b7ddc5 | 2014-07-23 14:52:05 +0000 | [diff] [blame] | 3314 | }else |
| 3315 | #endif |
| 3316 | { |
drh | 9296c18 | 2014-07-23 13:40:49 +0000 | [diff] [blame] | 3317 | codeReal(v, z, negFlag, iMem); |
| 3318 | } |
drh | 13573c7 | 2010-01-12 17:04:07 +0000 | [diff] [blame] | 3319 | #endif |
drh | 77320ea | 2016-11-30 14:47:37 +0000 | [diff] [blame] | 3320 | }else{ |
drh | 84d4f1a | 2017-09-20 10:47:10 +0000 | [diff] [blame] | 3321 | if( negFlag ){ value = c==3 ? SMALLEST_INT64 : -value; } |
drh | 77320ea | 2016-11-30 14:47:37 +0000 | [diff] [blame] | 3322 | sqlite3VdbeAddOp4Dup8(v, OP_Int64, 0, iMem, 0, (u8*)&value, P4_INT64); |
danielk1977 | c9cf901 | 2007-05-30 10:36:47 +0000 | [diff] [blame] | 3323 | } |
drh | fec19aa | 2004-05-19 20:41:03 +0000 | [diff] [blame] | 3324 | } |
| 3325 | } |
| 3326 | |
drh | 5cd7923 | 2009-05-25 11:46:29 +0000 | [diff] [blame] | 3327 | |
drh | 1f9ca2c | 2015-08-25 16:57:52 +0000 | [diff] [blame] | 3328 | /* Generate code that will load into register regOut a value that is |
| 3329 | ** appropriate for the iIdxCol-th column of index pIdx. |
| 3330 | */ |
| 3331 | void sqlite3ExprCodeLoadIndexColumn( |
| 3332 | Parse *pParse, /* The parsing context */ |
| 3333 | Index *pIdx, /* The index whose column is to be loaded */ |
| 3334 | int iTabCur, /* Cursor pointing to a table row */ |
| 3335 | int iIdxCol, /* The column of the index to be loaded */ |
| 3336 | int regOut /* Store the index column value in this register */ |
| 3337 | ){ |
| 3338 | i16 iTabCol = pIdx->aiColumn[iIdxCol]; |
drh | 4b92f98 | 2015-09-29 17:20:14 +0000 | [diff] [blame] | 3339 | if( iTabCol==XN_EXPR ){ |
| 3340 | assert( pIdx->aColExpr ); |
| 3341 | assert( pIdx->aColExpr->nExpr>iIdxCol ); |
drh | 3e34eab | 2017-07-19 19:48:40 +0000 | [diff] [blame] | 3342 | pParse->iSelfTab = iTabCur + 1; |
drh | 1c75c9d | 2015-12-21 15:22:13 +0000 | [diff] [blame] | 3343 | sqlite3ExprCodeCopy(pParse, pIdx->aColExpr->a[iIdxCol].pExpr, regOut); |
drh | 3e34eab | 2017-07-19 19:48:40 +0000 | [diff] [blame] | 3344 | pParse->iSelfTab = 0; |
drh | 4b92f98 | 2015-09-29 17:20:14 +0000 | [diff] [blame] | 3345 | }else{ |
drh | 1f9ca2c | 2015-08-25 16:57:52 +0000 | [diff] [blame] | 3346 | sqlite3ExprCodeGetColumnOfTable(pParse->pVdbe, pIdx->pTable, iTabCur, |
| 3347 | iTabCol, regOut); |
drh | 1f9ca2c | 2015-08-25 16:57:52 +0000 | [diff] [blame] | 3348 | } |
drh | 1f9ca2c | 2015-08-25 16:57:52 +0000 | [diff] [blame] | 3349 | } |
| 3350 | |
drh | 5cd7923 | 2009-05-25 11:46:29 +0000 | [diff] [blame] | 3351 | /* |
drh | 5c092e8 | 2010-05-14 19:24:02 +0000 | [diff] [blame] | 3352 | ** Generate code to extract the value of the iCol-th column of a table. |
| 3353 | */ |
| 3354 | void sqlite3ExprCodeGetColumnOfTable( |
| 3355 | Vdbe *v, /* The VDBE under construction */ |
| 3356 | Table *pTab, /* The table containing the value */ |
drh | 313619f | 2013-10-31 20:34:06 +0000 | [diff] [blame] | 3357 | int iTabCur, /* The table cursor. Or the PK cursor for WITHOUT ROWID */ |
drh | 5c092e8 | 2010-05-14 19:24:02 +0000 | [diff] [blame] | 3358 | int iCol, /* Index of the column to extract */ |
drh | 313619f | 2013-10-31 20:34:06 +0000 | [diff] [blame] | 3359 | int regOut /* Extract the value into this register */ |
drh | 5c092e8 | 2010-05-14 19:24:02 +0000 | [diff] [blame] | 3360 | ){ |
drh | aca19e1 | 2017-04-07 19:41:31 +0000 | [diff] [blame] | 3361 | if( pTab==0 ){ |
| 3362 | sqlite3VdbeAddOp3(v, OP_Column, iTabCur, iCol, regOut); |
| 3363 | return; |
| 3364 | } |
drh | 5c092e8 | 2010-05-14 19:24:02 +0000 | [diff] [blame] | 3365 | if( iCol<0 || iCol==pTab->iPKey ){ |
| 3366 | sqlite3VdbeAddOp2(v, OP_Rowid, iTabCur, regOut); |
| 3367 | }else{ |
| 3368 | int op = IsVirtual(pTab) ? OP_VColumn : OP_Column; |
drh | ee0ec8e | 2013-10-31 17:38:01 +0000 | [diff] [blame] | 3369 | int x = iCol; |
drh | 35db31b | 2016-06-02 23:13:21 +0000 | [diff] [blame] | 3370 | if( !HasRowid(pTab) && !IsVirtual(pTab) ){ |
drh | ee0ec8e | 2013-10-31 17:38:01 +0000 | [diff] [blame] | 3371 | x = sqlite3ColumnOfIndex(sqlite3PrimaryKeyIndex(pTab), iCol); |
| 3372 | } |
| 3373 | sqlite3VdbeAddOp3(v, op, iTabCur, x, regOut); |
drh | 5c092e8 | 2010-05-14 19:24:02 +0000 | [diff] [blame] | 3374 | } |
| 3375 | if( iCol>=0 ){ |
| 3376 | sqlite3ColumnDefault(v, pTab, iCol, regOut); |
| 3377 | } |
| 3378 | } |
| 3379 | |
| 3380 | /* |
drh | 945498f | 2007-02-24 11:52:52 +0000 | [diff] [blame] | 3381 | ** Generate code that will extract the iColumn-th column from |
drh | 8c60719 | 2018-08-04 15:53:55 +0000 | [diff] [blame] | 3382 | ** table pTab and store the column value in register iReg. |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 3383 | ** |
| 3384 | ** There must be an open cursor to pTab in iTable when this routine |
| 3385 | ** is called. If iColumn<0 then code is generated that extracts the rowid. |
drh | 945498f | 2007-02-24 11:52:52 +0000 | [diff] [blame] | 3386 | */ |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 3387 | int sqlite3ExprCodeGetColumn( |
| 3388 | Parse *pParse, /* Parsing and code generating context */ |
drh | 2133d82 | 2008-01-03 18:44:59 +0000 | [diff] [blame] | 3389 | Table *pTab, /* Description of the table we are reading from */ |
| 3390 | int iColumn, /* Index of the table column */ |
| 3391 | int iTable, /* The cursor pointing to the table */ |
drh | a748fdc | 2012-03-28 01:34:47 +0000 | [diff] [blame] | 3392 | int iReg, /* Store results here */ |
drh | ce78bc6 | 2015-10-15 19:21:51 +0000 | [diff] [blame] | 3393 | u8 p5 /* P5 value for OP_Column + FLAGS */ |
drh | 2133d82 | 2008-01-03 18:44:59 +0000 | [diff] [blame] | 3394 | ){ |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 3395 | Vdbe *v = pParse->pVdbe; |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 3396 | assert( v!=0 ); |
drh | 5c092e8 | 2010-05-14 19:24:02 +0000 | [diff] [blame] | 3397 | sqlite3ExprCodeGetColumnOfTable(v, pTab, iTable, iColumn, iReg); |
drh | a748fdc | 2012-03-28 01:34:47 +0000 | [diff] [blame] | 3398 | if( p5 ){ |
| 3399 | sqlite3VdbeChangeP5(v, p5); |
drh | a748fdc | 2012-03-28 01:34:47 +0000 | [diff] [blame] | 3400 | } |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 3401 | return iReg; |
| 3402 | } |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 3403 | |
| 3404 | /* |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 3405 | ** Generate code to move content from registers iFrom...iFrom+nReg-1 |
drh | 36a5d88 | 2018-08-04 17:15:56 +0000 | [diff] [blame] | 3406 | ** over to iTo..iTo+nReg-1. |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 3407 | */ |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 3408 | void sqlite3ExprCodeMove(Parse *pParse, int iFrom, int iTo, int nReg){ |
drh | e8e4af7 | 2012-09-21 00:04:28 +0000 | [diff] [blame] | 3409 | assert( iFrom>=iTo+nReg || iFrom+nReg<=iTo ); |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 3410 | sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move, iFrom, iTo, nReg); |
drh | 945498f | 2007-02-24 11:52:52 +0000 | [diff] [blame] | 3411 | } |
| 3412 | |
drh | 652fbf5 | 2008-04-01 01:42:41 +0000 | [diff] [blame] | 3413 | /* |
drh | 12abf40 | 2016-08-22 14:30:05 +0000 | [diff] [blame] | 3414 | ** Convert a scalar expression node to a TK_REGISTER referencing |
| 3415 | ** register iReg. The caller must ensure that iReg already contains |
| 3416 | ** the correct value for the expression. |
drh | a4c3c87 | 2013-09-12 17:29:25 +0000 | [diff] [blame] | 3417 | */ |
dan | 069d1b1 | 2019-06-16 08:58:14 +0000 | [diff] [blame] | 3418 | static void exprToRegister(Expr *pExpr, int iReg){ |
drh | 0d950af | 2019-08-22 16:38:42 +0000 | [diff] [blame] | 3419 | Expr *p = sqlite3ExprSkipCollateAndLikely(pExpr); |
drh | a4c3c87 | 2013-09-12 17:29:25 +0000 | [diff] [blame] | 3420 | p->op2 = p->op; |
| 3421 | p->op = TK_REGISTER; |
| 3422 | p->iTable = iReg; |
| 3423 | ExprClearProperty(p, EP_Skip); |
| 3424 | } |
| 3425 | |
drh | 12abf40 | 2016-08-22 14:30:05 +0000 | [diff] [blame] | 3426 | /* |
| 3427 | ** Evaluate an expression (either a vector or a scalar expression) and store |
| 3428 | ** the result in continguous temporary registers. Return the index of |
| 3429 | ** the first register used to store the result. |
| 3430 | ** |
| 3431 | ** If the returned result register is a temporary scalar, then also write |
| 3432 | ** that register number into *piFreeable. If the returned result register |
| 3433 | ** is not a temporary or if the expression is a vector set *piFreeable |
| 3434 | ** to 0. |
| 3435 | */ |
| 3436 | static int exprCodeVector(Parse *pParse, Expr *p, int *piFreeable){ |
| 3437 | int iResult; |
| 3438 | int nResult = sqlite3ExprVectorSize(p); |
| 3439 | if( nResult==1 ){ |
| 3440 | iResult = sqlite3ExprCodeTemp(pParse, p, piFreeable); |
| 3441 | }else{ |
| 3442 | *piFreeable = 0; |
| 3443 | if( p->op==TK_SELECT ){ |
drh | dd1bb43 | 2017-05-15 15:12:24 +0000 | [diff] [blame] | 3444 | #if SQLITE_OMIT_SUBQUERY |
| 3445 | iResult = 0; |
| 3446 | #else |
drh | 85bcdce | 2018-12-23 21:27:29 +0000 | [diff] [blame] | 3447 | iResult = sqlite3CodeSubselect(pParse, p); |
drh | dd1bb43 | 2017-05-15 15:12:24 +0000 | [diff] [blame] | 3448 | #endif |
drh | 12abf40 | 2016-08-22 14:30:05 +0000 | [diff] [blame] | 3449 | }else{ |
| 3450 | int i; |
| 3451 | iResult = pParse->nMem+1; |
| 3452 | pParse->nMem += nResult; |
| 3453 | for(i=0; i<nResult; i++){ |
dan | 4b72524 | 2016-11-23 19:31:18 +0000 | [diff] [blame] | 3454 | sqlite3ExprCodeFactorable(pParse, p->x.pList->a[i].pExpr, i+iResult); |
drh | 12abf40 | 2016-08-22 14:30:05 +0000 | [diff] [blame] | 3455 | } |
| 3456 | } |
| 3457 | } |
| 3458 | return iResult; |
| 3459 | } |
| 3460 | |
dan | 71c57db | 2016-07-09 20:23:55 +0000 | [diff] [blame] | 3461 | |
drh | a4c3c87 | 2013-09-12 17:29:25 +0000 | [diff] [blame] | 3462 | /* |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 3463 | ** Generate code into the current Vdbe to evaluate the given |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 3464 | ** expression. Attempt to store the results in register "target". |
| 3465 | ** Return the register where results are stored. |
drh | 389a1ad | 2008-01-03 23:44:53 +0000 | [diff] [blame] | 3466 | ** |
drh | 8b21389 | 2008-08-29 02:14:02 +0000 | [diff] [blame] | 3467 | ** With this routine, there is no guarantee that results will |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 3468 | ** be stored in target. The result might be stored in some other |
| 3469 | ** register if it is convenient to do so. The calling function |
| 3470 | ** must check the return code and move the results to the desired |
| 3471 | ** register. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 3472 | */ |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 3473 | int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){ |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 3474 | Vdbe *v = pParse->pVdbe; /* The VM under construction */ |
| 3475 | int op; /* The opcode being coded */ |
| 3476 | int inReg = target; /* Results stored in register inReg */ |
| 3477 | int regFree1 = 0; /* If non-zero free this temporary register */ |
| 3478 | int regFree2 = 0; /* If non-zero free this temporary register */ |
dan | 7b35a77 | 2016-07-28 19:47:15 +0000 | [diff] [blame] | 3479 | int r1, r2; /* Various register numbers */ |
drh | 10d1edf | 2013-11-15 15:52:39 +0000 | [diff] [blame] | 3480 | Expr tempX; /* Temporary expression node */ |
dan | 71c57db | 2016-07-09 20:23:55 +0000 | [diff] [blame] | 3481 | int p5 = 0; |
drh | ffe07b2 | 2005-11-03 00:41:17 +0000 | [diff] [blame] | 3482 | |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 3483 | assert( target>0 && target<=pParse->nMem ); |
drh | 20411ea | 2009-05-29 19:00:12 +0000 | [diff] [blame] | 3484 | if( v==0 ){ |
| 3485 | assert( pParse->db->mallocFailed ); |
| 3486 | return 0; |
| 3487 | } |
drh | 389a1ad | 2008-01-03 23:44:53 +0000 | [diff] [blame] | 3488 | |
drh | 1efa802 | 2018-04-28 04:16:43 +0000 | [diff] [blame] | 3489 | expr_code_doover: |
drh | 389a1ad | 2008-01-03 23:44:53 +0000 | [diff] [blame] | 3490 | if( pExpr==0 ){ |
| 3491 | op = TK_NULL; |
| 3492 | }else{ |
| 3493 | op = pExpr->op; |
| 3494 | } |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 3495 | switch( op ){ |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 3496 | case TK_AGG_COLUMN: { |
| 3497 | AggInfo *pAggInfo = pExpr->pAggInfo; |
| 3498 | struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg]; |
| 3499 | if( !pAggInfo->directMode ){ |
drh | 9de221d | 2008-01-05 06:51:30 +0000 | [diff] [blame] | 3500 | assert( pCol->iMem>0 ); |
drh | c332cc3 | 2016-09-19 10:24:19 +0000 | [diff] [blame] | 3501 | return pCol->iMem; |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 3502 | }else if( pAggInfo->useSortingIdx ){ |
dan | 5134d13 | 2011-09-02 10:31:11 +0000 | [diff] [blame] | 3503 | sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab, |
drh | 389a1ad | 2008-01-03 23:44:53 +0000 | [diff] [blame] | 3504 | pCol->iSorterColumn, target); |
drh | c332cc3 | 2016-09-19 10:24:19 +0000 | [diff] [blame] | 3505 | return target; |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 3506 | } |
| 3507 | /* Otherwise, fall thru into the TK_COLUMN case */ |
| 3508 | } |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 3509 | case TK_COLUMN: { |
drh | b2b9d3d | 2013-08-01 01:14:43 +0000 | [diff] [blame] | 3510 | int iTab = pExpr->iTable; |
drh | efad2e2 | 2018-07-27 16:57:11 +0000 | [diff] [blame] | 3511 | if( ExprHasProperty(pExpr, EP_FixedCol) ){ |
drh | d98f532 | 2018-08-09 18:36:54 +0000 | [diff] [blame] | 3512 | /* This COLUMN expression is really a constant due to WHERE clause |
| 3513 | ** constraints, and that constant is coded by the pExpr->pLeft |
| 3514 | ** expresssion. However, make sure the constant has the correct |
| 3515 | ** datatype by applying the Affinity of the table column to the |
| 3516 | ** constant. |
| 3517 | */ |
| 3518 | int iReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft,target); |
drh | eda079c | 2018-09-20 19:02:15 +0000 | [diff] [blame] | 3519 | int aff = sqlite3TableColumnAffinity(pExpr->y.pTab, pExpr->iColumn); |
drh | 96fb16e | 2019-08-06 14:37:24 +0000 | [diff] [blame] | 3520 | if( aff>SQLITE_AFF_BLOB ){ |
drh | d98f532 | 2018-08-09 18:36:54 +0000 | [diff] [blame] | 3521 | static const char zAff[] = "B\000C\000D\000E"; |
| 3522 | assert( SQLITE_AFF_BLOB=='A' ); |
| 3523 | assert( SQLITE_AFF_TEXT=='B' ); |
| 3524 | if( iReg!=target ){ |
| 3525 | sqlite3VdbeAddOp2(v, OP_SCopy, iReg, target); |
| 3526 | iReg = target; |
| 3527 | } |
| 3528 | sqlite3VdbeAddOp4(v, OP_Affinity, iReg, 1, 0, |
| 3529 | &zAff[(aff-'B')*2], P4_STATIC); |
| 3530 | } |
| 3531 | return iReg; |
drh | efad2e2 | 2018-07-27 16:57:11 +0000 | [diff] [blame] | 3532 | } |
drh | b2b9d3d | 2013-08-01 01:14:43 +0000 | [diff] [blame] | 3533 | if( iTab<0 ){ |
drh | 6e97f8e | 2017-07-20 13:17:08 +0000 | [diff] [blame] | 3534 | if( pParse->iSelfTab<0 ){ |
drh | b2b9d3d | 2013-08-01 01:14:43 +0000 | [diff] [blame] | 3535 | /* Generating CHECK constraints or inserting into partial index */ |
drh | 6e97f8e | 2017-07-20 13:17:08 +0000 | [diff] [blame] | 3536 | return pExpr->iColumn - pParse->iSelfTab; |
drh | b2b9d3d | 2013-08-01 01:14:43 +0000 | [diff] [blame] | 3537 | }else{ |
drh | 1f9ca2c | 2015-08-25 16:57:52 +0000 | [diff] [blame] | 3538 | /* Coding an expression that is part of an index where column names |
| 3539 | ** in the index refer to the table to which the index belongs */ |
drh | 3e34eab | 2017-07-19 19:48:40 +0000 | [diff] [blame] | 3540 | iTab = pParse->iSelfTab - 1; |
drh | b2b9d3d | 2013-08-01 01:14:43 +0000 | [diff] [blame] | 3541 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 3542 | } |
drh | eda079c | 2018-09-20 19:02:15 +0000 | [diff] [blame] | 3543 | return sqlite3ExprCodeGetColumn(pParse, pExpr->y.pTab, |
drh | b2b9d3d | 2013-08-01 01:14:43 +0000 | [diff] [blame] | 3544 | pExpr->iColumn, iTab, target, |
| 3545 | pExpr->op2); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 3546 | } |
| 3547 | case TK_INTEGER: { |
drh | 13573c7 | 2010-01-12 17:04:07 +0000 | [diff] [blame] | 3548 | codeInteger(pParse, pExpr, 0, target); |
drh | c332cc3 | 2016-09-19 10:24:19 +0000 | [diff] [blame] | 3549 | return target; |
drh | fec19aa | 2004-05-19 20:41:03 +0000 | [diff] [blame] | 3550 | } |
drh | 8abed7b | 2018-02-26 18:49:05 +0000 | [diff] [blame] | 3551 | case TK_TRUEFALSE: { |
drh | 96acafb | 2018-02-27 14:49:25 +0000 | [diff] [blame] | 3552 | sqlite3VdbeAddOp2(v, OP_Integer, sqlite3ExprTruthValue(pExpr), target); |
drh | 007c843 | 2018-02-26 03:20:18 +0000 | [diff] [blame] | 3553 | return target; |
| 3554 | } |
drh | 13573c7 | 2010-01-12 17:04:07 +0000 | [diff] [blame] | 3555 | #ifndef SQLITE_OMIT_FLOATING_POINT |
drh | 598f134 | 2007-10-23 15:39:45 +0000 | [diff] [blame] | 3556 | case TK_FLOAT: { |
drh | 33e619f | 2009-05-28 01:00:55 +0000 | [diff] [blame] | 3557 | assert( !ExprHasProperty(pExpr, EP_IntValue) ); |
| 3558 | codeReal(v, pExpr->u.zToken, 0, target); |
drh | c332cc3 | 2016-09-19 10:24:19 +0000 | [diff] [blame] | 3559 | return target; |
drh | 598f134 | 2007-10-23 15:39:45 +0000 | [diff] [blame] | 3560 | } |
drh | 13573c7 | 2010-01-12 17:04:07 +0000 | [diff] [blame] | 3561 | #endif |
drh | fec19aa | 2004-05-19 20:41:03 +0000 | [diff] [blame] | 3562 | case TK_STRING: { |
drh | 33e619f | 2009-05-28 01:00:55 +0000 | [diff] [blame] | 3563 | assert( !ExprHasProperty(pExpr, EP_IntValue) ); |
drh | 076e85f | 2015-09-03 13:46:12 +0000 | [diff] [blame] | 3564 | sqlite3VdbeLoadString(v, target, pExpr->u.zToken); |
drh | c332cc3 | 2016-09-19 10:24:19 +0000 | [diff] [blame] | 3565 | return target; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 3566 | } |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3567 | case TK_NULL: { |
drh | 9de221d | 2008-01-05 06:51:30 +0000 | [diff] [blame] | 3568 | sqlite3VdbeAddOp2(v, OP_Null, 0, target); |
drh | c332cc3 | 2016-09-19 10:24:19 +0000 | [diff] [blame] | 3569 | return target; |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 3570 | } |
danielk1977 | 5338a5f | 2005-01-20 13:03:10 +0000 | [diff] [blame] | 3571 | #ifndef SQLITE_OMIT_BLOB_LITERAL |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 3572 | case TK_BLOB: { |
drh | 6c8c6ce | 2005-08-23 11:17:58 +0000 | [diff] [blame] | 3573 | int n; |
| 3574 | const char *z; |
drh | ca48c90 | 2008-01-18 14:08:24 +0000 | [diff] [blame] | 3575 | char *zBlob; |
drh | 33e619f | 2009-05-28 01:00:55 +0000 | [diff] [blame] | 3576 | assert( !ExprHasProperty(pExpr, EP_IntValue) ); |
| 3577 | assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' ); |
| 3578 | assert( pExpr->u.zToken[1]=='\'' ); |
| 3579 | z = &pExpr->u.zToken[2]; |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 3580 | n = sqlite3Strlen30(z) - 1; |
| 3581 | assert( z[n]=='\'' ); |
drh | ca48c90 | 2008-01-18 14:08:24 +0000 | [diff] [blame] | 3582 | zBlob = sqlite3HexToBlob(sqlite3VdbeDb(v), z, n); |
| 3583 | sqlite3VdbeAddOp4(v, OP_Blob, n/2, target, 0, zBlob, P4_DYNAMIC); |
drh | c332cc3 | 2016-09-19 10:24:19 +0000 | [diff] [blame] | 3584 | return target; |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 3585 | } |
danielk1977 | 5338a5f | 2005-01-20 13:03:10 +0000 | [diff] [blame] | 3586 | #endif |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 3587 | case TK_VARIABLE: { |
drh | 33e619f | 2009-05-28 01:00:55 +0000 | [diff] [blame] | 3588 | assert( !ExprHasProperty(pExpr, EP_IntValue) ); |
| 3589 | assert( pExpr->u.zToken!=0 ); |
| 3590 | assert( pExpr->u.zToken[0]!=0 ); |
drh | eaf52d8 | 2010-05-12 13:50:23 +0000 | [diff] [blame] | 3591 | sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iColumn, target); |
| 3592 | if( pExpr->u.zToken[1]!=0 ){ |
drh | 9bf755c | 2016-12-23 03:59:31 +0000 | [diff] [blame] | 3593 | const char *z = sqlite3VListNumToName(pParse->pVList, pExpr->iColumn); |
drh | f326d66 | 2016-12-23 13:30:53 +0000 | [diff] [blame] | 3594 | assert( pExpr->u.zToken[0]=='?' || strcmp(pExpr->u.zToken, z)==0 ); |
drh | ce1bbe5 | 2016-12-23 13:52:45 +0000 | [diff] [blame] | 3595 | pParse->pVList[0] = 0; /* Indicate VList may no longer be enlarged */ |
drh | f326d66 | 2016-12-23 13:30:53 +0000 | [diff] [blame] | 3596 | sqlite3VdbeAppendP4(v, (char*)z, P4_STATIC); |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 3597 | } |
drh | c332cc3 | 2016-09-19 10:24:19 +0000 | [diff] [blame] | 3598 | return target; |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 3599 | } |
drh | 4e0cff6 | 2004-11-05 05:10:28 +0000 | [diff] [blame] | 3600 | case TK_REGISTER: { |
drh | c332cc3 | 2016-09-19 10:24:19 +0000 | [diff] [blame] | 3601 | return pExpr->iTable; |
drh | 4e0cff6 | 2004-11-05 05:10:28 +0000 | [diff] [blame] | 3602 | } |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 3603 | #ifndef SQLITE_OMIT_CAST |
| 3604 | case TK_CAST: { |
| 3605 | /* Expressions of the form: CAST(pLeft AS token) */ |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 3606 | inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target); |
drh | 1735fa8 | 2008-11-06 15:33:03 +0000 | [diff] [blame] | 3607 | if( inReg!=target ){ |
| 3608 | sqlite3VdbeAddOp2(v, OP_SCopy, inReg, target); |
| 3609 | inReg = target; |
| 3610 | } |
drh | 4169e43 | 2014-08-25 20:11:52 +0000 | [diff] [blame] | 3611 | sqlite3VdbeAddOp2(v, OP_Cast, target, |
| 3612 | sqlite3AffinityType(pExpr->u.zToken, 0)); |
drh | c332cc3 | 2016-09-19 10:24:19 +0000 | [diff] [blame] | 3613 | return inReg; |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 3614 | } |
| 3615 | #endif /* SQLITE_OMIT_CAST */ |
dan | 71c57db | 2016-07-09 20:23:55 +0000 | [diff] [blame] | 3616 | case TK_IS: |
| 3617 | case TK_ISNOT: |
| 3618 | op = (op==TK_IS) ? TK_EQ : TK_NE; |
| 3619 | p5 = SQLITE_NULLEQ; |
| 3620 | /* fall-through */ |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 3621 | case TK_LT: |
| 3622 | case TK_LE: |
| 3623 | case TK_GT: |
| 3624 | case TK_GE: |
| 3625 | case TK_NE: |
| 3626 | case TK_EQ: { |
dan | 71c57db | 2016-07-09 20:23:55 +0000 | [diff] [blame] | 3627 | Expr *pLeft = pExpr->pLeft; |
dan | 625015e | 2016-07-30 16:39:28 +0000 | [diff] [blame] | 3628 | if( sqlite3ExprIsVector(pLeft) ){ |
drh | 79752b6 | 2016-08-13 10:02:17 +0000 | [diff] [blame] | 3629 | codeVectorCompare(pParse, pExpr, target, op, p5); |
dan | 71c57db | 2016-07-09 20:23:55 +0000 | [diff] [blame] | 3630 | }else{ |
| 3631 | r1 = sqlite3ExprCodeTemp(pParse, pLeft, ®Free1); |
| 3632 | r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); |
| 3633 | codeCompare(pParse, pLeft, pExpr->pRight, op, |
| 3634 | r1, r2, inReg, SQLITE_STOREP2 | p5); |
| 3635 | assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt); |
| 3636 | assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le); |
| 3637 | assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt); |
| 3638 | assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge); |
| 3639 | assert(TK_EQ==OP_Eq); testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq); |
| 3640 | assert(TK_NE==OP_Ne); testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne); |
| 3641 | testcase( regFree1==0 ); |
| 3642 | testcase( regFree2==0 ); |
| 3643 | } |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 3644 | break; |
| 3645 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 3646 | case TK_AND: |
| 3647 | case TK_OR: |
| 3648 | case TK_PLUS: |
| 3649 | case TK_STAR: |
| 3650 | case TK_MINUS: |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 3651 | case TK_REM: |
| 3652 | case TK_BITAND: |
| 3653 | case TK_BITOR: |
drh | 17c4029 | 2004-07-21 02:53:29 +0000 | [diff] [blame] | 3654 | case TK_SLASH: |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 3655 | case TK_LSHIFT: |
drh | 855eb1c | 2004-08-31 13:45:11 +0000 | [diff] [blame] | 3656 | case TK_RSHIFT: |
drh | 0040077 | 2000-06-16 20:51:26 +0000 | [diff] [blame] | 3657 | case TK_CONCAT: { |
drh | 7d17610 | 2014-02-18 03:07:12 +0000 | [diff] [blame] | 3658 | assert( TK_AND==OP_And ); testcase( op==TK_AND ); |
| 3659 | assert( TK_OR==OP_Or ); testcase( op==TK_OR ); |
| 3660 | assert( TK_PLUS==OP_Add ); testcase( op==TK_PLUS ); |
| 3661 | assert( TK_MINUS==OP_Subtract ); testcase( op==TK_MINUS ); |
| 3662 | assert( TK_REM==OP_Remainder ); testcase( op==TK_REM ); |
| 3663 | assert( TK_BITAND==OP_BitAnd ); testcase( op==TK_BITAND ); |
| 3664 | assert( TK_BITOR==OP_BitOr ); testcase( op==TK_BITOR ); |
| 3665 | assert( TK_SLASH==OP_Divide ); testcase( op==TK_SLASH ); |
| 3666 | assert( TK_LSHIFT==OP_ShiftLeft ); testcase( op==TK_LSHIFT ); |
| 3667 | assert( TK_RSHIFT==OP_ShiftRight ); testcase( op==TK_RSHIFT ); |
| 3668 | assert( TK_CONCAT==OP_Concat ); testcase( op==TK_CONCAT ); |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 3669 | r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); |
| 3670 | r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 3671 | sqlite3VdbeAddOp3(v, op, r2, r1, target); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 3672 | testcase( regFree1==0 ); |
| 3673 | testcase( regFree2==0 ); |
drh | 0040077 | 2000-06-16 20:51:26 +0000 | [diff] [blame] | 3674 | break; |
| 3675 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 3676 | case TK_UMINUS: { |
drh | fec19aa | 2004-05-19 20:41:03 +0000 | [diff] [blame] | 3677 | Expr *pLeft = pExpr->pLeft; |
| 3678 | assert( pLeft ); |
drh | 13573c7 | 2010-01-12 17:04:07 +0000 | [diff] [blame] | 3679 | if( pLeft->op==TK_INTEGER ){ |
| 3680 | codeInteger(pParse, pLeft, 1, target); |
drh | c332cc3 | 2016-09-19 10:24:19 +0000 | [diff] [blame] | 3681 | return target; |
drh | 13573c7 | 2010-01-12 17:04:07 +0000 | [diff] [blame] | 3682 | #ifndef SQLITE_OMIT_FLOATING_POINT |
| 3683 | }else if( pLeft->op==TK_FLOAT ){ |
drh | 33e619f | 2009-05-28 01:00:55 +0000 | [diff] [blame] | 3684 | assert( !ExprHasProperty(pExpr, EP_IntValue) ); |
| 3685 | codeReal(v, pLeft->u.zToken, 1, target); |
drh | c332cc3 | 2016-09-19 10:24:19 +0000 | [diff] [blame] | 3686 | return target; |
drh | 13573c7 | 2010-01-12 17:04:07 +0000 | [diff] [blame] | 3687 | #endif |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 3688 | }else{ |
drh | 10d1edf | 2013-11-15 15:52:39 +0000 | [diff] [blame] | 3689 | tempX.op = TK_INTEGER; |
| 3690 | tempX.flags = EP_IntValue|EP_TokenOnly; |
| 3691 | tempX.u.iValue = 0; |
| 3692 | r1 = sqlite3ExprCodeTemp(pParse, &tempX, ®Free1); |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 3693 | r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free2); |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 3694 | sqlite3VdbeAddOp3(v, OP_Subtract, r2, r1, target); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 3695 | testcase( regFree2==0 ); |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 3696 | } |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 3697 | break; |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 3698 | } |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 3699 | case TK_BITNOT: |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 3700 | case TK_NOT: { |
drh | 7d17610 | 2014-02-18 03:07:12 +0000 | [diff] [blame] | 3701 | assert( TK_BITNOT==OP_BitNot ); testcase( op==TK_BITNOT ); |
| 3702 | assert( TK_NOT==OP_Not ); testcase( op==TK_NOT ); |
drh | e99fa2a | 2008-12-15 15:27:51 +0000 | [diff] [blame] | 3703 | r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); |
| 3704 | testcase( regFree1==0 ); |
drh | e99fa2a | 2008-12-15 15:27:51 +0000 | [diff] [blame] | 3705 | sqlite3VdbeAddOp2(v, op, r1, inReg); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 3706 | break; |
| 3707 | } |
drh | 8abed7b | 2018-02-26 18:49:05 +0000 | [diff] [blame] | 3708 | case TK_TRUTH: { |
drh | 96acafb | 2018-02-27 14:49:25 +0000 | [diff] [blame] | 3709 | int isTrue; /* IS TRUE or IS NOT TRUE */ |
| 3710 | int bNormal; /* IS TRUE or IS FALSE */ |
drh | 007c843 | 2018-02-26 03:20:18 +0000 | [diff] [blame] | 3711 | r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); |
| 3712 | testcase( regFree1==0 ); |
drh | 96acafb | 2018-02-27 14:49:25 +0000 | [diff] [blame] | 3713 | isTrue = sqlite3ExprTruthValue(pExpr->pRight); |
| 3714 | bNormal = pExpr->op2==TK_IS; |
| 3715 | testcase( isTrue && bNormal); |
| 3716 | testcase( !isTrue && bNormal); |
| 3717 | sqlite3VdbeAddOp4Int(v, OP_IsTrue, r1, inReg, !isTrue, isTrue ^ bNormal); |
drh | 007c843 | 2018-02-26 03:20:18 +0000 | [diff] [blame] | 3718 | break; |
| 3719 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 3720 | case TK_ISNULL: |
| 3721 | case TK_NOTNULL: { |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 3722 | int addr; |
drh | 7d17610 | 2014-02-18 03:07:12 +0000 | [diff] [blame] | 3723 | assert( TK_ISNULL==OP_IsNull ); testcase( op==TK_ISNULL ); |
| 3724 | assert( TK_NOTNULL==OP_NotNull ); testcase( op==TK_NOTNULL ); |
drh | 9de221d | 2008-01-05 06:51:30 +0000 | [diff] [blame] | 3725 | sqlite3VdbeAddOp2(v, OP_Integer, 1, target); |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 3726 | r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 3727 | testcase( regFree1==0 ); |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 3728 | addr = sqlite3VdbeAddOp1(v, op, r1); |
drh | 7d17610 | 2014-02-18 03:07:12 +0000 | [diff] [blame] | 3729 | VdbeCoverageIf(v, op==TK_ISNULL); |
| 3730 | VdbeCoverageIf(v, op==TK_NOTNULL); |
drh | a976979 | 2014-08-04 16:39:39 +0000 | [diff] [blame] | 3731 | sqlite3VdbeAddOp2(v, OP_Integer, 0, target); |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 3732 | sqlite3VdbeJumpHere(v, addr); |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 3733 | break; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 3734 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 3735 | case TK_AGG_FUNCTION: { |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 3736 | AggInfo *pInfo = pExpr->pAggInfo; |
drh | 7e56e71 | 2005-11-16 12:53:15 +0000 | [diff] [blame] | 3737 | if( pInfo==0 ){ |
drh | 33e619f | 2009-05-28 01:00:55 +0000 | [diff] [blame] | 3738 | assert( !ExprHasProperty(pExpr, EP_IntValue) ); |
| 3739 | sqlite3ErrorMsg(pParse, "misuse of aggregate: %s()", pExpr->u.zToken); |
drh | 7e56e71 | 2005-11-16 12:53:15 +0000 | [diff] [blame] | 3740 | }else{ |
drh | c332cc3 | 2016-09-19 10:24:19 +0000 | [diff] [blame] | 3741 | return pInfo->aFunc[pExpr->iAgg].iMem; |
drh | 7e56e71 | 2005-11-16 12:53:15 +0000 | [diff] [blame] | 3742 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 3743 | break; |
| 3744 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 3745 | case TK_FUNCTION: { |
drh | 12ffee8 | 2009-04-08 13:51:51 +0000 | [diff] [blame] | 3746 | ExprList *pFarg; /* List of function arguments */ |
| 3747 | int nFarg; /* Number of function arguments */ |
| 3748 | FuncDef *pDef; /* The function definition object */ |
drh | 12ffee8 | 2009-04-08 13:51:51 +0000 | [diff] [blame] | 3749 | const char *zId; /* The function name */ |
drh | 693e671 | 2014-01-24 22:58:00 +0000 | [diff] [blame] | 3750 | u32 constMask = 0; /* Mask of function arguments that are constant */ |
drh | 12ffee8 | 2009-04-08 13:51:51 +0000 | [diff] [blame] | 3751 | int i; /* Loop counter */ |
drh | c332cc3 | 2016-09-19 10:24:19 +0000 | [diff] [blame] | 3752 | sqlite3 *db = pParse->db; /* The database connection */ |
drh | 12ffee8 | 2009-04-08 13:51:51 +0000 | [diff] [blame] | 3753 | u8 enc = ENC(db); /* The text encoding used by this database */ |
| 3754 | CollSeq *pColl = 0; /* A collating sequence */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 3755 | |
dan | 67a9b8e | 2018-06-22 20:51:35 +0000 | [diff] [blame] | 3756 | #ifndef SQLITE_OMIT_WINDOWFUNC |
drh | eda079c | 2018-09-20 19:02:15 +0000 | [diff] [blame] | 3757 | if( ExprHasProperty(pExpr, EP_WinFunc) ){ |
| 3758 | return pExpr->y.pWin->regResult; |
dan | 86fb6e1 | 2018-05-16 20:58:07 +0000 | [diff] [blame] | 3759 | } |
dan | 67a9b8e | 2018-06-22 20:51:35 +0000 | [diff] [blame] | 3760 | #endif |
dan | 86fb6e1 | 2018-05-16 20:58:07 +0000 | [diff] [blame] | 3761 | |
drh | 1e9b53f | 2017-01-04 01:07:24 +0000 | [diff] [blame] | 3762 | if( ConstFactorOk(pParse) && sqlite3ExprIsConstantNotJoin(pExpr) ){ |
drh | 49c5ab2 | 2017-01-04 04:18:00 +0000 | [diff] [blame] | 3763 | /* SQL functions can be expensive. So try to move constant functions |
drh | ad879ff | 2017-01-04 04:10:02 +0000 | [diff] [blame] | 3764 | ** out of the inner loop, even if that means an extra OP_Copy. */ |
| 3765 | return sqlite3ExprCodeAtInit(pParse, pExpr, -1); |
drh | 1e9b53f | 2017-01-04 01:07:24 +0000 | [diff] [blame] | 3766 | } |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 3767 | assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); |
drh | c5cd124 | 2013-09-12 16:50:49 +0000 | [diff] [blame] | 3768 | if( ExprHasProperty(pExpr, EP_TokenOnly) ){ |
drh | 12ffee8 | 2009-04-08 13:51:51 +0000 | [diff] [blame] | 3769 | pFarg = 0; |
| 3770 | }else{ |
| 3771 | pFarg = pExpr->x.pList; |
| 3772 | } |
| 3773 | nFarg = pFarg ? pFarg->nExpr : 0; |
drh | 33e619f | 2009-05-28 01:00:55 +0000 | [diff] [blame] | 3774 | assert( !ExprHasProperty(pExpr, EP_IntValue) ); |
| 3775 | zId = pExpr->u.zToken; |
drh | 80738d9 | 2016-02-15 00:34:16 +0000 | [diff] [blame] | 3776 | pDef = sqlite3FindFunction(db, zId, nFarg, enc, 0); |
drh | cc15313 | 2016-08-04 12:35:17 +0000 | [diff] [blame] | 3777 | #ifdef SQLITE_ENABLE_UNKNOWN_SQL_FUNCTION |
| 3778 | if( pDef==0 && pParse->explain ){ |
| 3779 | pDef = sqlite3FindFunction(db, "unknown", nFarg, enc, 0); |
| 3780 | } |
| 3781 | #endif |
dan | b6e9f7a | 2018-05-19 14:15:29 +0000 | [diff] [blame] | 3782 | if( pDef==0 || pDef->xFinalize!=0 ){ |
drh | 80738d9 | 2016-02-15 00:34:16 +0000 | [diff] [blame] | 3783 | sqlite3ErrorMsg(pParse, "unknown function: %s()", zId); |
drh | feb306f | 2009-08-18 16:05:46 +0000 | [diff] [blame] | 3784 | break; |
| 3785 | } |
drh | ae6bb95 | 2009-11-11 00:24:31 +0000 | [diff] [blame] | 3786 | |
| 3787 | /* Attempt a direct implementation of the built-in COALESCE() and |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 3788 | ** IFNULL() functions. This avoids unnecessary evaluation of |
drh | ae6bb95 | 2009-11-11 00:24:31 +0000 | [diff] [blame] | 3789 | ** arguments past the first non-NULL argument. |
| 3790 | */ |
drh | d36e104 | 2013-09-06 13:10:12 +0000 | [diff] [blame] | 3791 | if( pDef->funcFlags & SQLITE_FUNC_COALESCE ){ |
drh | ec4ccdb | 2018-12-29 02:26:59 +0000 | [diff] [blame] | 3792 | int endCoalesce = sqlite3VdbeMakeLabel(pParse); |
drh | ae6bb95 | 2009-11-11 00:24:31 +0000 | [diff] [blame] | 3793 | assert( nFarg>=2 ); |
| 3794 | sqlite3ExprCode(pParse, pFarg->a[0].pExpr, target); |
| 3795 | for(i=1; i<nFarg; i++){ |
| 3796 | sqlite3VdbeAddOp2(v, OP_NotNull, target, endCoalesce); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 3797 | VdbeCoverage(v); |
drh | ae6bb95 | 2009-11-11 00:24:31 +0000 | [diff] [blame] | 3798 | sqlite3ExprCode(pParse, pFarg->a[i].pExpr, target); |
drh | ae6bb95 | 2009-11-11 00:24:31 +0000 | [diff] [blame] | 3799 | } |
| 3800 | sqlite3VdbeResolveLabel(v, endCoalesce); |
| 3801 | break; |
| 3802 | } |
| 3803 | |
drh | cca9f3d | 2013-09-06 15:23:29 +0000 | [diff] [blame] | 3804 | /* The UNLIKELY() function is a no-op. The result is the value |
| 3805 | ** of the first argument. |
| 3806 | */ |
| 3807 | if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){ |
| 3808 | assert( nFarg>=1 ); |
drh | c332cc3 | 2016-09-19 10:24:19 +0000 | [diff] [blame] | 3809 | return sqlite3ExprCodeTarget(pParse, pFarg->a[0].pExpr, target); |
drh | cca9f3d | 2013-09-06 15:23:29 +0000 | [diff] [blame] | 3810 | } |
drh | ae6bb95 | 2009-11-11 00:24:31 +0000 | [diff] [blame] | 3811 | |
drh | 5424075 | 2017-01-03 14:39:30 +0000 | [diff] [blame] | 3812 | #ifdef SQLITE_DEBUG |
drh | a1a523a | 2016-12-26 00:18:36 +0000 | [diff] [blame] | 3813 | /* The AFFINITY() function evaluates to a string that describes |
| 3814 | ** the type affinity of the argument. This is used for testing of |
| 3815 | ** the SQLite type logic. |
| 3816 | */ |
| 3817 | if( pDef->funcFlags & SQLITE_FUNC_AFFINITY ){ |
| 3818 | const char *azAff[] = { "blob", "text", "numeric", "integer", "real" }; |
| 3819 | char aff; |
| 3820 | assert( nFarg==1 ); |
| 3821 | aff = sqlite3ExprAffinity(pFarg->a[0].pExpr); |
| 3822 | sqlite3VdbeLoadString(v, target, |
drh | 96fb16e | 2019-08-06 14:37:24 +0000 | [diff] [blame] | 3823 | (aff<=SQLITE_AFF_NONE) ? "none" : azAff[aff-SQLITE_AFF_BLOB]); |
drh | a1a523a | 2016-12-26 00:18:36 +0000 | [diff] [blame] | 3824 | return target; |
| 3825 | } |
drh | 5424075 | 2017-01-03 14:39:30 +0000 | [diff] [blame] | 3826 | #endif |
drh | a1a523a | 2016-12-26 00:18:36 +0000 | [diff] [blame] | 3827 | |
drh | d1a01ed | 2013-11-21 16:08:52 +0000 | [diff] [blame] | 3828 | for(i=0; i<nFarg; i++){ |
| 3829 | if( i<32 && sqlite3ExprIsConstant(pFarg->a[i].pExpr) ){ |
drh | 693e671 | 2014-01-24 22:58:00 +0000 | [diff] [blame] | 3830 | testcase( i==31 ); |
| 3831 | constMask |= MASKBIT32(i); |
drh | d1a01ed | 2013-11-21 16:08:52 +0000 | [diff] [blame] | 3832 | } |
| 3833 | if( (pDef->funcFlags & SQLITE_FUNC_NEEDCOLL)!=0 && !pColl ){ |
| 3834 | pColl = sqlite3ExprCollSeq(pParse, pFarg->a[i].pExpr); |
| 3835 | } |
| 3836 | } |
drh | 12ffee8 | 2009-04-08 13:51:51 +0000 | [diff] [blame] | 3837 | if( pFarg ){ |
drh | d1a01ed | 2013-11-21 16:08:52 +0000 | [diff] [blame] | 3838 | if( constMask ){ |
| 3839 | r1 = pParse->nMem+1; |
| 3840 | pParse->nMem += nFarg; |
| 3841 | }else{ |
| 3842 | r1 = sqlite3GetTempRange(pParse, nFarg); |
| 3843 | } |
drh | a748fdc | 2012-03-28 01:34:47 +0000 | [diff] [blame] | 3844 | |
| 3845 | /* For length() and typeof() functions with a column argument, |
| 3846 | ** set the P5 parameter to the OP_Column opcode to OPFLAG_LENGTHARG |
| 3847 | ** or OPFLAG_TYPEOFARG respectively, to avoid unnecessary data |
| 3848 | ** loading. |
| 3849 | */ |
drh | d36e104 | 2013-09-06 13:10:12 +0000 | [diff] [blame] | 3850 | if( (pDef->funcFlags & (SQLITE_FUNC_LENGTH|SQLITE_FUNC_TYPEOF))!=0 ){ |
drh | 4e245a4 | 2012-03-30 00:00:36 +0000 | [diff] [blame] | 3851 | u8 exprOp; |
drh | a748fdc | 2012-03-28 01:34:47 +0000 | [diff] [blame] | 3852 | assert( nFarg==1 ); |
| 3853 | assert( pFarg->a[0].pExpr!=0 ); |
drh | 4e245a4 | 2012-03-30 00:00:36 +0000 | [diff] [blame] | 3854 | exprOp = pFarg->a[0].pExpr->op; |
| 3855 | if( exprOp==TK_COLUMN || exprOp==TK_AGG_COLUMN ){ |
drh | a748fdc | 2012-03-28 01:34:47 +0000 | [diff] [blame] | 3856 | assert( SQLITE_FUNC_LENGTH==OPFLAG_LENGTHARG ); |
| 3857 | assert( SQLITE_FUNC_TYPEOF==OPFLAG_TYPEOFARG ); |
drh | b1fba28 | 2013-11-21 14:33:48 +0000 | [diff] [blame] | 3858 | testcase( pDef->funcFlags & OPFLAG_LENGTHARG ); |
| 3859 | pFarg->a[0].pExpr->op2 = |
| 3860 | pDef->funcFlags & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG); |
drh | a748fdc | 2012-03-28 01:34:47 +0000 | [diff] [blame] | 3861 | } |
| 3862 | } |
| 3863 | |
drh | 5579d59 | 2015-08-26 14:01:41 +0000 | [diff] [blame] | 3864 | sqlite3ExprCodeExprList(pParse, pFarg, r1, 0, |
drh | d1a01ed | 2013-11-21 16:08:52 +0000 | [diff] [blame] | 3865 | SQLITE_ECEL_DUP|SQLITE_ECEL_FACTOR); |
drh | 892d317 | 2008-01-10 03:46:36 +0000 | [diff] [blame] | 3866 | }else{ |
drh | 12ffee8 | 2009-04-08 13:51:51 +0000 | [diff] [blame] | 3867 | r1 = 0; |
drh | 892d317 | 2008-01-10 03:46:36 +0000 | [diff] [blame] | 3868 | } |
drh | b7f6f68 | 2006-07-08 17:06:43 +0000 | [diff] [blame] | 3869 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | a43fa22 | 2006-07-08 18:41:37 +0000 | [diff] [blame] | 3870 | /* Possibly overload the function if the first argument is |
| 3871 | ** a virtual table column. |
| 3872 | ** |
| 3873 | ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the |
| 3874 | ** second argument, not the first, as the argument to test to |
| 3875 | ** see if it is a column in a virtual table. This is done because |
| 3876 | ** the left operand of infix functions (the operand we want to |
| 3877 | ** control overloading) ends up as the second argument to the |
| 3878 | ** function. The expression "A glob B" is equivalent to |
| 3879 | ** "glob(B,A). We want to use the A in "A glob B" to test |
| 3880 | ** for function overloading. But we use the B term in "glob(B,A)". |
| 3881 | */ |
drh | 5915506 | 2018-05-26 18:03:48 +0000 | [diff] [blame] | 3882 | if( nFarg>=2 && ExprHasProperty(pExpr, EP_InfixFunc) ){ |
drh | 12ffee8 | 2009-04-08 13:51:51 +0000 | [diff] [blame] | 3883 | pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[1].pExpr); |
| 3884 | }else if( nFarg>0 ){ |
| 3885 | pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[0].pExpr); |
drh | b7f6f68 | 2006-07-08 17:06:43 +0000 | [diff] [blame] | 3886 | } |
| 3887 | #endif |
drh | d36e104 | 2013-09-06 13:10:12 +0000 | [diff] [blame] | 3888 | if( pDef->funcFlags & SQLITE_FUNC_NEEDCOLL ){ |
drh | 8b21389 | 2008-08-29 02:14:02 +0000 | [diff] [blame] | 3889 | if( !pColl ) pColl = db->pDfltColl; |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 3890 | sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ); |
danielk1977 | 682f68b | 2004-06-05 10:22:17 +0000 | [diff] [blame] | 3891 | } |
drh | 092457b | 2017-12-29 15:04:49 +0000 | [diff] [blame] | 3892 | #ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC |
| 3893 | if( pDef->funcFlags & SQLITE_FUNC_OFFSET ){ |
drh | 2fc865c | 2017-12-16 20:20:37 +0000 | [diff] [blame] | 3894 | Expr *pArg = pFarg->a[0].pExpr; |
| 3895 | if( pArg->op==TK_COLUMN ){ |
drh | 092457b | 2017-12-29 15:04:49 +0000 | [diff] [blame] | 3896 | sqlite3VdbeAddOp3(v, OP_Offset, pArg->iTable, pArg->iColumn, target); |
drh | 2fc865c | 2017-12-16 20:20:37 +0000 | [diff] [blame] | 3897 | }else{ |
| 3898 | sqlite3VdbeAddOp2(v, OP_Null, 0, target); |
| 3899 | } |
drh | 092457b | 2017-12-29 15:04:49 +0000 | [diff] [blame] | 3900 | }else |
| 3901 | #endif |
| 3902 | { |
drh | 2fc865c | 2017-12-16 20:20:37 +0000 | [diff] [blame] | 3903 | sqlite3VdbeAddOp4(v, pParse->iSelfTab ? OP_PureFunc0 : OP_Function0, |
| 3904 | constMask, r1, target, (char*)pDef, P4_FUNCDEF); |
| 3905 | sqlite3VdbeChangeP5(v, (u8)nFarg); |
| 3906 | } |
drh | d1a01ed | 2013-11-21 16:08:52 +0000 | [diff] [blame] | 3907 | if( nFarg && constMask==0 ){ |
drh | 12ffee8 | 2009-04-08 13:51:51 +0000 | [diff] [blame] | 3908 | sqlite3ReleaseTempRange(pParse, r1, nFarg); |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 3909 | } |
drh | c332cc3 | 2016-09-19 10:24:19 +0000 | [diff] [blame] | 3910 | return target; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 3911 | } |
drh | fe2093d | 2005-01-20 22:48:47 +0000 | [diff] [blame] | 3912 | #ifndef SQLITE_OMIT_SUBQUERY |
| 3913 | case TK_EXISTS: |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 3914 | case TK_SELECT: { |
dan | 8da209b | 2016-07-26 18:06:08 +0000 | [diff] [blame] | 3915 | int nCol; |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 3916 | testcase( op==TK_EXISTS ); |
| 3917 | testcase( op==TK_SELECT ); |
dan | 8da209b | 2016-07-26 18:06:08 +0000 | [diff] [blame] | 3918 | if( op==TK_SELECT && (nCol = pExpr->x.pSelect->pEList->nExpr)!=1 ){ |
| 3919 | sqlite3SubselectError(pParse, nCol, 1); |
| 3920 | }else{ |
drh | 85bcdce | 2018-12-23 21:27:29 +0000 | [diff] [blame] | 3921 | return sqlite3CodeSubselect(pParse, pExpr); |
dan | 8da209b | 2016-07-26 18:06:08 +0000 | [diff] [blame] | 3922 | } |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 3923 | break; |
| 3924 | } |
drh | fc7f27b | 2016-08-20 00:07:01 +0000 | [diff] [blame] | 3925 | case TK_SELECT_COLUMN: { |
drh | 966e291 | 2017-01-03 02:58:01 +0000 | [diff] [blame] | 3926 | int n; |
drh | fc7f27b | 2016-08-20 00:07:01 +0000 | [diff] [blame] | 3927 | if( pExpr->pLeft->iTable==0 ){ |
drh | 85bcdce | 2018-12-23 21:27:29 +0000 | [diff] [blame] | 3928 | pExpr->pLeft->iTable = sqlite3CodeSubselect(pParse, pExpr->pLeft); |
drh | fc7f27b | 2016-08-20 00:07:01 +0000 | [diff] [blame] | 3929 | } |
drh | 966e291 | 2017-01-03 02:58:01 +0000 | [diff] [blame] | 3930 | assert( pExpr->iTable==0 || pExpr->pLeft->op==TK_SELECT ); |
drh | 554a9dc | 2019-08-26 14:18:28 +0000 | [diff] [blame] | 3931 | if( pExpr->iTable!=0 |
| 3932 | && pExpr->iTable!=(n = sqlite3ExprVectorSize(pExpr->pLeft)) |
drh | 966e291 | 2017-01-03 02:58:01 +0000 | [diff] [blame] | 3933 | ){ |
| 3934 | sqlite3ErrorMsg(pParse, "%d columns assigned %d values", |
| 3935 | pExpr->iTable, n); |
| 3936 | } |
drh | c332cc3 | 2016-09-19 10:24:19 +0000 | [diff] [blame] | 3937 | return pExpr->pLeft->iTable + pExpr->iColumn; |
drh | fc7f27b | 2016-08-20 00:07:01 +0000 | [diff] [blame] | 3938 | } |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 3939 | case TK_IN: { |
drh | ec4ccdb | 2018-12-29 02:26:59 +0000 | [diff] [blame] | 3940 | int destIfFalse = sqlite3VdbeMakeLabel(pParse); |
| 3941 | int destIfNull = sqlite3VdbeMakeLabel(pParse); |
drh | e3365e6 | 2009-11-12 17:52:24 +0000 | [diff] [blame] | 3942 | sqlite3VdbeAddOp2(v, OP_Null, 0, target); |
| 3943 | sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull); |
| 3944 | sqlite3VdbeAddOp2(v, OP_Integer, 1, target); |
| 3945 | sqlite3VdbeResolveLabel(v, destIfFalse); |
| 3946 | sqlite3VdbeAddOp2(v, OP_AddImm, target, 0); |
| 3947 | sqlite3VdbeResolveLabel(v, destIfNull); |
drh | c332cc3 | 2016-09-19 10:24:19 +0000 | [diff] [blame] | 3948 | return target; |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 3949 | } |
drh | e3365e6 | 2009-11-12 17:52:24 +0000 | [diff] [blame] | 3950 | #endif /* SQLITE_OMIT_SUBQUERY */ |
| 3951 | |
| 3952 | |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 3953 | /* |
| 3954 | ** x BETWEEN y AND z |
| 3955 | ** |
| 3956 | ** This is equivalent to |
| 3957 | ** |
| 3958 | ** x>=y AND x<=z |
| 3959 | ** |
| 3960 | ** X is stored in pExpr->pLeft. |
| 3961 | ** Y is stored in pExpr->pList->a[0].pExpr. |
| 3962 | ** Z is stored in pExpr->pList->a[1].pExpr. |
| 3963 | */ |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 3964 | case TK_BETWEEN: { |
dan | 71c57db | 2016-07-09 20:23:55 +0000 | [diff] [blame] | 3965 | exprCodeBetween(pParse, pExpr, target, 0, 0); |
drh | c332cc3 | 2016-09-19 10:24:19 +0000 | [diff] [blame] | 3966 | return target; |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 3967 | } |
drh | 94fa9c4 | 2016-02-27 21:16:04 +0000 | [diff] [blame] | 3968 | case TK_SPAN: |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 3969 | case TK_COLLATE: |
drh | 4f07e5f | 2007-05-14 11:34:46 +0000 | [diff] [blame] | 3970 | case TK_UPLUS: { |
drh | 1efa802 | 2018-04-28 04:16:43 +0000 | [diff] [blame] | 3971 | pExpr = pExpr->pLeft; |
drh | 59ee43a | 2018-05-29 15:18:31 +0000 | [diff] [blame] | 3972 | goto expr_code_doover; /* 2018-04-28: Prevent deep recursion. OSSFuzz. */ |
drh | a2e0004 | 2002-01-22 03:13:42 +0000 | [diff] [blame] | 3973 | } |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 3974 | |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 3975 | case TK_TRIGGER: { |
dan | 65a7cd1 | 2009-09-01 12:16:01 +0000 | [diff] [blame] | 3976 | /* If the opcode is TK_TRIGGER, then the expression is a reference |
| 3977 | ** to a column in the new.* or old.* pseudo-tables available to |
| 3978 | ** trigger programs. In this case Expr.iTable is set to 1 for the |
| 3979 | ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn |
| 3980 | ** is set to the column of the pseudo-table to read, or to -1 to |
| 3981 | ** read the rowid field. |
| 3982 | ** |
| 3983 | ** The expression is implemented using an OP_Param opcode. The p1 |
| 3984 | ** parameter is set to 0 for an old.rowid reference, or to (i+1) |
| 3985 | ** to reference another column of the old.* pseudo-table, where |
| 3986 | ** i is the index of the column. For a new.rowid reference, p1 is |
| 3987 | ** set to (n+1), where n is the number of columns in each pseudo-table. |
| 3988 | ** For a reference to any other column in the new.* pseudo-table, p1 |
| 3989 | ** is set to (n+2+i), where n and i are as defined previously. For |
| 3990 | ** example, if the table on which triggers are being fired is |
| 3991 | ** declared as: |
| 3992 | ** |
| 3993 | ** CREATE TABLE t1(a, b); |
| 3994 | ** |
| 3995 | ** Then p1 is interpreted as follows: |
| 3996 | ** |
| 3997 | ** p1==0 -> old.rowid p1==3 -> new.rowid |
| 3998 | ** p1==1 -> old.a p1==4 -> new.a |
| 3999 | ** p1==2 -> old.b p1==5 -> new.b |
| 4000 | */ |
drh | eda079c | 2018-09-20 19:02:15 +0000 | [diff] [blame] | 4001 | Table *pTab = pExpr->y.pTab; |
dan | 65a7cd1 | 2009-09-01 12:16:01 +0000 | [diff] [blame] | 4002 | int p1 = pExpr->iTable * (pTab->nCol+1) + 1 + pExpr->iColumn; |
| 4003 | |
| 4004 | assert( pExpr->iTable==0 || pExpr->iTable==1 ); |
| 4005 | assert( pExpr->iColumn>=-1 && pExpr->iColumn<pTab->nCol ); |
| 4006 | assert( pTab->iPKey<0 || pExpr->iColumn!=pTab->iPKey ); |
| 4007 | assert( p1>=0 && p1<(pTab->nCol*2+2) ); |
| 4008 | |
| 4009 | sqlite3VdbeAddOp2(v, OP_Param, p1, target); |
drh | 896494e | 2018-04-26 12:27:03 +0000 | [diff] [blame] | 4010 | VdbeComment((v, "r[%d]=%s.%s", target, |
dan | 2bd9351 | 2009-08-31 08:22:46 +0000 | [diff] [blame] | 4011 | (pExpr->iTable ? "new" : "old"), |
drh | eda079c | 2018-09-20 19:02:15 +0000 | [diff] [blame] | 4012 | (pExpr->iColumn<0 ? "rowid" : pExpr->y.pTab->aCol[pExpr->iColumn].zName) |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 4013 | )); |
dan | 65a7cd1 | 2009-09-01 12:16:01 +0000 | [diff] [blame] | 4014 | |
drh | 44dbca8 | 2010-01-13 04:22:20 +0000 | [diff] [blame] | 4015 | #ifndef SQLITE_OMIT_FLOATING_POINT |
dan | 65a7cd1 | 2009-09-01 12:16:01 +0000 | [diff] [blame] | 4016 | /* If the column has REAL affinity, it may currently be stored as an |
drh | 113762a | 2014-11-19 16:36:25 +0000 | [diff] [blame] | 4017 | ** integer. Use OP_RealAffinity to make sure it is really real. |
| 4018 | ** |
| 4019 | ** EVIDENCE-OF: R-60985-57662 SQLite will convert the value back to |
| 4020 | ** floating point when extracting it from the record. */ |
dan | 2832ad4 | 2009-08-31 15:27:27 +0000 | [diff] [blame] | 4021 | if( pExpr->iColumn>=0 |
| 4022 | && pTab->aCol[pExpr->iColumn].affinity==SQLITE_AFF_REAL |
| 4023 | ){ |
| 4024 | sqlite3VdbeAddOp1(v, OP_RealAffinity, target); |
| 4025 | } |
drh | 44dbca8 | 2010-01-13 04:22:20 +0000 | [diff] [blame] | 4026 | #endif |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 4027 | break; |
| 4028 | } |
| 4029 | |
dan | 71c57db | 2016-07-09 20:23:55 +0000 | [diff] [blame] | 4030 | case TK_VECTOR: { |
drh | e835bc1 | 2016-08-23 19:02:55 +0000 | [diff] [blame] | 4031 | sqlite3ErrorMsg(pParse, "row value misused"); |
dan | 71c57db | 2016-07-09 20:23:55 +0000 | [diff] [blame] | 4032 | break; |
| 4033 | } |
| 4034 | |
drh | 9e9a67a | 2019-08-17 17:07:15 +0000 | [diff] [blame] | 4035 | /* TK_IF_NULL_ROW Expr nodes are inserted ahead of expressions |
| 4036 | ** that derive from the right-hand table of a LEFT JOIN. The |
| 4037 | ** Expr.iTable value is the table number for the right-hand table. |
| 4038 | ** The expression is only evaluated if that table is not currently |
| 4039 | ** on a LEFT JOIN NULL row. |
| 4040 | */ |
drh | 31d6fd5 | 2017-04-14 19:03:10 +0000 | [diff] [blame] | 4041 | case TK_IF_NULL_ROW: { |
| 4042 | int addrINR; |
drh | 9e9a67a | 2019-08-17 17:07:15 +0000 | [diff] [blame] | 4043 | u8 okConstFactor = pParse->okConstFactor; |
drh | 31d6fd5 | 2017-04-14 19:03:10 +0000 | [diff] [blame] | 4044 | addrINR = sqlite3VdbeAddOp1(v, OP_IfNullRow, pExpr->iTable); |
drh | 9e9a67a | 2019-08-17 17:07:15 +0000 | [diff] [blame] | 4045 | /* Temporarily disable factoring of constant expressions, since |
| 4046 | ** even though expressions may appear to be constant, they are not |
| 4047 | ** really constant because they originate from the right-hand side |
| 4048 | ** of a LEFT JOIN. */ |
| 4049 | pParse->okConstFactor = 0; |
drh | 31d6fd5 | 2017-04-14 19:03:10 +0000 | [diff] [blame] | 4050 | inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target); |
drh | 9e9a67a | 2019-08-17 17:07:15 +0000 | [diff] [blame] | 4051 | pParse->okConstFactor = okConstFactor; |
drh | 31d6fd5 | 2017-04-14 19:03:10 +0000 | [diff] [blame] | 4052 | sqlite3VdbeJumpHere(v, addrINR); |
| 4053 | sqlite3VdbeChangeP3(v, addrINR, inReg); |
| 4054 | break; |
| 4055 | } |
| 4056 | |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 4057 | /* |
| 4058 | ** Form A: |
| 4059 | ** CASE x WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END |
| 4060 | ** |
| 4061 | ** Form B: |
| 4062 | ** CASE WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END |
| 4063 | ** |
| 4064 | ** Form A is can be transformed into the equivalent form B as follows: |
| 4065 | ** CASE WHEN x=e1 THEN r1 WHEN x=e2 THEN r2 ... |
| 4066 | ** WHEN x=eN THEN rN ELSE y END |
| 4067 | ** |
| 4068 | ** X (if it exists) is in pExpr->pLeft. |
drh | c5cd124 | 2013-09-12 16:50:49 +0000 | [diff] [blame] | 4069 | ** Y is in the last element of pExpr->x.pList if pExpr->x.pList->nExpr is |
| 4070 | ** odd. The Y is also optional. If the number of elements in x.pList |
| 4071 | ** is even, then Y is omitted and the "otherwise" result is NULL. |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 4072 | ** Ei is in pExpr->pList->a[i*2] and Ri is pExpr->pList->a[i*2+1]. |
| 4073 | ** |
| 4074 | ** The result of the expression is the Ri for the first matching Ei, |
| 4075 | ** or if there is no matching Ei, the ELSE term Y, or if there is |
| 4076 | ** no ELSE term, NULL. |
| 4077 | */ |
drh | 33cd490 | 2009-05-30 20:49:20 +0000 | [diff] [blame] | 4078 | default: assert( op==TK_CASE ); { |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 4079 | int endLabel; /* GOTO label for end of CASE stmt */ |
| 4080 | int nextCase; /* GOTO label for next WHEN clause */ |
| 4081 | int nExpr; /* 2x number of WHEN terms */ |
| 4082 | int i; /* Loop counter */ |
| 4083 | ExprList *pEList; /* List of WHEN terms */ |
| 4084 | struct ExprList_item *aListelem; /* Array of WHEN terms */ |
| 4085 | Expr opCompare; /* The X==Ei expression */ |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 4086 | Expr *pX; /* The X expression */ |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 4087 | Expr *pTest = 0; /* X==Ei (form A) or just Ei (form B) */ |
dan | 8b65e59 | 2019-07-17 14:34:17 +0000 | [diff] [blame] | 4088 | Expr *pDel = 0; |
| 4089 | sqlite3 *db = pParse->db; |
drh | 17a7f8d | 2002-03-24 13:13:27 +0000 | [diff] [blame] | 4090 | |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 4091 | assert( !ExprHasProperty(pExpr, EP_xIsSelect) && pExpr->x.pList ); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 4092 | assert(pExpr->x.pList->nExpr > 0); |
| 4093 | pEList = pExpr->x.pList; |
drh | be5c89a | 2004-07-26 00:31:09 +0000 | [diff] [blame] | 4094 | aListelem = pEList->a; |
| 4095 | nExpr = pEList->nExpr; |
drh | ec4ccdb | 2018-12-29 02:26:59 +0000 | [diff] [blame] | 4096 | endLabel = sqlite3VdbeMakeLabel(pParse); |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 4097 | if( (pX = pExpr->pLeft)!=0 ){ |
dan | 8b65e59 | 2019-07-17 14:34:17 +0000 | [diff] [blame] | 4098 | pDel = sqlite3ExprDup(db, pX, 0); |
| 4099 | if( db->mallocFailed ){ |
| 4100 | sqlite3ExprDelete(db, pDel); |
| 4101 | break; |
| 4102 | } |
drh | 33cd490 | 2009-05-30 20:49:20 +0000 | [diff] [blame] | 4103 | testcase( pX->op==TK_COLUMN ); |
dan | 8b65e59 | 2019-07-17 14:34:17 +0000 | [diff] [blame] | 4104 | exprToRegister(pDel, exprCodeVector(pParse, pDel, ®Free1)); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 4105 | testcase( regFree1==0 ); |
drh | abb9d5f | 2016-08-23 17:30:55 +0000 | [diff] [blame] | 4106 | memset(&opCompare, 0, sizeof(opCompare)); |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 4107 | opCompare.op = TK_EQ; |
dan | 8b65e59 | 2019-07-17 14:34:17 +0000 | [diff] [blame] | 4108 | opCompare.pLeft = pDel; |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 4109 | pTest = &opCompare; |
drh | 8b1db07 | 2010-09-28 04:14:03 +0000 | [diff] [blame] | 4110 | /* Ticket b351d95f9cd5ef17e9d9dbae18f5ca8611190001: |
| 4111 | ** The value in regFree1 might get SCopy-ed into the file result. |
| 4112 | ** So make sure that the regFree1 register is not reused for other |
| 4113 | ** purposes and possibly overwritten. */ |
| 4114 | regFree1 = 0; |
drh | 17a7f8d | 2002-03-24 13:13:27 +0000 | [diff] [blame] | 4115 | } |
drh | c5cd124 | 2013-09-12 16:50:49 +0000 | [diff] [blame] | 4116 | for(i=0; i<nExpr-1; i=i+2){ |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 4117 | if( pX ){ |
drh | 1bd10f8 | 2008-12-10 21:19:56 +0000 | [diff] [blame] | 4118 | assert( pTest!=0 ); |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 4119 | opCompare.pRight = aListelem[i].pExpr; |
drh | 17a7f8d | 2002-03-24 13:13:27 +0000 | [diff] [blame] | 4120 | }else{ |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 4121 | pTest = aListelem[i].pExpr; |
drh | 17a7f8d | 2002-03-24 13:13:27 +0000 | [diff] [blame] | 4122 | } |
drh | ec4ccdb | 2018-12-29 02:26:59 +0000 | [diff] [blame] | 4123 | nextCase = sqlite3VdbeMakeLabel(pParse); |
drh | 33cd490 | 2009-05-30 20:49:20 +0000 | [diff] [blame] | 4124 | testcase( pTest->op==TK_COLUMN ); |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 4125 | sqlite3ExprIfFalse(pParse, pTest, nextCase, SQLITE_JUMPIFNULL); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 4126 | testcase( aListelem[i+1].pExpr->op==TK_COLUMN ); |
drh | 9de221d | 2008-01-05 06:51:30 +0000 | [diff] [blame] | 4127 | sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target); |
drh | 076e85f | 2015-09-03 13:46:12 +0000 | [diff] [blame] | 4128 | sqlite3VdbeGoto(v, endLabel); |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 4129 | sqlite3VdbeResolveLabel(v, nextCase); |
drh | f570f01 | 2002-05-31 15:51:25 +0000 | [diff] [blame] | 4130 | } |
drh | c5cd124 | 2013-09-12 16:50:49 +0000 | [diff] [blame] | 4131 | if( (nExpr&1)!=0 ){ |
drh | c5cd124 | 2013-09-12 16:50:49 +0000 | [diff] [blame] | 4132 | sqlite3ExprCode(pParse, pEList->a[nExpr-1].pExpr, target); |
drh | 17a7f8d | 2002-03-24 13:13:27 +0000 | [diff] [blame] | 4133 | }else{ |
drh | 9de221d | 2008-01-05 06:51:30 +0000 | [diff] [blame] | 4134 | sqlite3VdbeAddOp2(v, OP_Null, 0, target); |
drh | 17a7f8d | 2002-03-24 13:13:27 +0000 | [diff] [blame] | 4135 | } |
dan | 8b65e59 | 2019-07-17 14:34:17 +0000 | [diff] [blame] | 4136 | sqlite3ExprDelete(db, pDel); |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 4137 | sqlite3VdbeResolveLabel(v, endLabel); |
danielk1977 | 6f34903 | 2002-06-11 02:25:40 +0000 | [diff] [blame] | 4138 | break; |
| 4139 | } |
danielk1977 | 5338a5f | 2005-01-20 13:03:10 +0000 | [diff] [blame] | 4140 | #ifndef SQLITE_OMIT_TRIGGER |
danielk1977 | 6f34903 | 2002-06-11 02:25:40 +0000 | [diff] [blame] | 4141 | case TK_RAISE: { |
drh | 1194904 | 2019-08-05 18:01:42 +0000 | [diff] [blame] | 4142 | assert( pExpr->affExpr==OE_Rollback |
| 4143 | || pExpr->affExpr==OE_Abort |
| 4144 | || pExpr->affExpr==OE_Fail |
| 4145 | || pExpr->affExpr==OE_Ignore |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 4146 | ); |
dan | e0af83a | 2009-09-08 19:15:01 +0000 | [diff] [blame] | 4147 | if( !pParse->pTriggerTab ){ |
| 4148 | sqlite3ErrorMsg(pParse, |
| 4149 | "RAISE() may only be used within a trigger-program"); |
| 4150 | return 0; |
| 4151 | } |
drh | 1194904 | 2019-08-05 18:01:42 +0000 | [diff] [blame] | 4152 | if( pExpr->affExpr==OE_Abort ){ |
dan | e0af83a | 2009-09-08 19:15:01 +0000 | [diff] [blame] | 4153 | sqlite3MayAbort(pParse); |
| 4154 | } |
dan | 165921a | 2009-08-28 18:53:45 +0000 | [diff] [blame] | 4155 | assert( !ExprHasProperty(pExpr, EP_IntValue) ); |
drh | 1194904 | 2019-08-05 18:01:42 +0000 | [diff] [blame] | 4156 | if( pExpr->affExpr==OE_Ignore ){ |
dan | e0af83a | 2009-09-08 19:15:01 +0000 | [diff] [blame] | 4157 | sqlite3VdbeAddOp4( |
| 4158 | v, OP_Halt, SQLITE_OK, OE_Ignore, 0, pExpr->u.zToken,0); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 4159 | VdbeCoverage(v); |
dan | e0af83a | 2009-09-08 19:15:01 +0000 | [diff] [blame] | 4160 | }else{ |
drh | 433dccf | 2013-02-09 15:37:11 +0000 | [diff] [blame] | 4161 | sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_TRIGGER, |
drh | 1194904 | 2019-08-05 18:01:42 +0000 | [diff] [blame] | 4162 | pExpr->affExpr, pExpr->u.zToken, 0, 0); |
dan | e0af83a | 2009-09-08 19:15:01 +0000 | [diff] [blame] | 4163 | } |
| 4164 | |
drh | ffe07b2 | 2005-11-03 00:41:17 +0000 | [diff] [blame] | 4165 | break; |
drh | 17a7f8d | 2002-03-24 13:13:27 +0000 | [diff] [blame] | 4166 | } |
danielk1977 | 5338a5f | 2005-01-20 13:03:10 +0000 | [diff] [blame] | 4167 | #endif |
drh | ffe07b2 | 2005-11-03 00:41:17 +0000 | [diff] [blame] | 4168 | } |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 4169 | sqlite3ReleaseTempReg(pParse, regFree1); |
| 4170 | sqlite3ReleaseTempReg(pParse, regFree2); |
| 4171 | return inReg; |
| 4172 | } |
| 4173 | |
| 4174 | /* |
drh | d1a01ed | 2013-11-21 16:08:52 +0000 | [diff] [blame] | 4175 | ** Factor out the code of the given expression to initialization time. |
drh | 1e9b53f | 2017-01-04 01:07:24 +0000 | [diff] [blame] | 4176 | ** |
drh | ad879ff | 2017-01-04 04:10:02 +0000 | [diff] [blame] | 4177 | ** If regDest>=0 then the result is always stored in that register and the |
| 4178 | ** result is not reusable. If regDest<0 then this routine is free to |
| 4179 | ** store the value whereever it wants. The register where the expression |
| 4180 | ** is stored is returned. When regDest<0, two identical expressions will |
| 4181 | ** code to the same register. |
drh | d1a01ed | 2013-11-21 16:08:52 +0000 | [diff] [blame] | 4182 | */ |
drh | 1e9b53f | 2017-01-04 01:07:24 +0000 | [diff] [blame] | 4183 | int sqlite3ExprCodeAtInit( |
drh | d673cdd | 2013-11-21 21:23:31 +0000 | [diff] [blame] | 4184 | Parse *pParse, /* Parsing context */ |
| 4185 | Expr *pExpr, /* The expression to code when the VDBE initializes */ |
drh | ad879ff | 2017-01-04 04:10:02 +0000 | [diff] [blame] | 4186 | int regDest /* Store the value in this register */ |
drh | d673cdd | 2013-11-21 21:23:31 +0000 | [diff] [blame] | 4187 | ){ |
drh | d1a01ed | 2013-11-21 16:08:52 +0000 | [diff] [blame] | 4188 | ExprList *p; |
drh | d9f158e | 2013-11-21 20:48:42 +0000 | [diff] [blame] | 4189 | assert( ConstFactorOk(pParse) ); |
drh | d1a01ed | 2013-11-21 16:08:52 +0000 | [diff] [blame] | 4190 | p = pParse->pConstExpr; |
drh | ad879ff | 2017-01-04 04:10:02 +0000 | [diff] [blame] | 4191 | if( regDest<0 && p ){ |
| 4192 | struct ExprList_item *pItem; |
| 4193 | int i; |
| 4194 | for(pItem=p->a, i=p->nExpr; i>0; pItem++, i--){ |
dan | 5aa550c | 2017-06-24 18:10:29 +0000 | [diff] [blame] | 4195 | if( pItem->reusable && sqlite3ExprCompare(0,pItem->pExpr,pExpr,-1)==0 ){ |
drh | ad879ff | 2017-01-04 04:10:02 +0000 | [diff] [blame] | 4196 | return pItem->u.iConstExprReg; |
drh | 1e9b53f | 2017-01-04 01:07:24 +0000 | [diff] [blame] | 4197 | } |
| 4198 | } |
drh | 1e9b53f | 2017-01-04 01:07:24 +0000 | [diff] [blame] | 4199 | } |
drh | d1a01ed | 2013-11-21 16:08:52 +0000 | [diff] [blame] | 4200 | pExpr = sqlite3ExprDup(pParse->db, pExpr, 0); |
| 4201 | p = sqlite3ExprListAppend(pParse, p, pExpr); |
drh | d673cdd | 2013-11-21 21:23:31 +0000 | [diff] [blame] | 4202 | if( p ){ |
| 4203 | struct ExprList_item *pItem = &p->a[p->nExpr-1]; |
drh | ad879ff | 2017-01-04 04:10:02 +0000 | [diff] [blame] | 4204 | pItem->reusable = regDest<0; |
| 4205 | if( regDest<0 ) regDest = ++pParse->nMem; |
drh | d673cdd | 2013-11-21 21:23:31 +0000 | [diff] [blame] | 4206 | pItem->u.iConstExprReg = regDest; |
drh | d673cdd | 2013-11-21 21:23:31 +0000 | [diff] [blame] | 4207 | } |
drh | d1a01ed | 2013-11-21 16:08:52 +0000 | [diff] [blame] | 4208 | pParse->pConstExpr = p; |
drh | 1e9b53f | 2017-01-04 01:07:24 +0000 | [diff] [blame] | 4209 | return regDest; |
drh | d1a01ed | 2013-11-21 16:08:52 +0000 | [diff] [blame] | 4210 | } |
| 4211 | |
| 4212 | /* |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 4213 | ** Generate code to evaluate an expression and store the results |
| 4214 | ** into a register. Return the register number where the results |
| 4215 | ** are stored. |
| 4216 | ** |
| 4217 | ** If the register is a temporary register that can be deallocated, |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 4218 | ** then write its number into *pReg. If the result register is not |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 4219 | ** a temporary, then set *pReg to zero. |
drh | f30a969 | 2013-11-15 01:10:18 +0000 | [diff] [blame] | 4220 | ** |
| 4221 | ** If pExpr is a constant, then this routine might generate this |
| 4222 | ** code to fill the register in the initialization section of the |
| 4223 | ** VDBE program, in order to factor it out of the evaluation loop. |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 4224 | */ |
| 4225 | int sqlite3ExprCodeTemp(Parse *pParse, Expr *pExpr, int *pReg){ |
drh | f30a969 | 2013-11-15 01:10:18 +0000 | [diff] [blame] | 4226 | int r2; |
drh | 0d950af | 2019-08-22 16:38:42 +0000 | [diff] [blame] | 4227 | pExpr = sqlite3ExprSkipCollateAndLikely(pExpr); |
drh | d9f158e | 2013-11-21 20:48:42 +0000 | [diff] [blame] | 4228 | if( ConstFactorOk(pParse) |
drh | f30a969 | 2013-11-15 01:10:18 +0000 | [diff] [blame] | 4229 | && pExpr->op!=TK_REGISTER |
| 4230 | && sqlite3ExprIsConstantNotJoin(pExpr) |
| 4231 | ){ |
drh | f30a969 | 2013-11-15 01:10:18 +0000 | [diff] [blame] | 4232 | *pReg = 0; |
drh | ad879ff | 2017-01-04 04:10:02 +0000 | [diff] [blame] | 4233 | r2 = sqlite3ExprCodeAtInit(pParse, pExpr, -1); |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 4234 | }else{ |
drh | f30a969 | 2013-11-15 01:10:18 +0000 | [diff] [blame] | 4235 | int r1 = sqlite3GetTempReg(pParse); |
| 4236 | r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1); |
| 4237 | if( r2==r1 ){ |
| 4238 | *pReg = r1; |
| 4239 | }else{ |
| 4240 | sqlite3ReleaseTempReg(pParse, r1); |
| 4241 | *pReg = 0; |
| 4242 | } |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 4243 | } |
| 4244 | return r2; |
| 4245 | } |
| 4246 | |
| 4247 | /* |
| 4248 | ** Generate code that will evaluate expression pExpr and store the |
| 4249 | ** results in register target. The results are guaranteed to appear |
| 4250 | ** in register target. |
| 4251 | */ |
drh | 05a86c5 | 2014-02-16 01:55:49 +0000 | [diff] [blame] | 4252 | void sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4253 | int inReg; |
| 4254 | |
| 4255 | assert( target>0 && target<=pParse->nMem ); |
drh | ebc1671 | 2010-09-28 00:25:58 +0000 | [diff] [blame] | 4256 | if( pExpr && pExpr->op==TK_REGISTER ){ |
| 4257 | sqlite3VdbeAddOp2(pParse->pVdbe, OP_Copy, pExpr->iTable, target); |
| 4258 | }else{ |
| 4259 | inReg = sqlite3ExprCodeTarget(pParse, pExpr, target); |
drh | 1c75c9d | 2015-12-21 15:22:13 +0000 | [diff] [blame] | 4260 | assert( pParse->pVdbe!=0 || pParse->db->mallocFailed ); |
drh | ebc1671 | 2010-09-28 00:25:58 +0000 | [diff] [blame] | 4261 | if( inReg!=target && pParse->pVdbe ){ |
| 4262 | sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, inReg, target); |
| 4263 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 4264 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 4265 | } |
| 4266 | |
| 4267 | /* |
drh | 1c75c9d | 2015-12-21 15:22:13 +0000 | [diff] [blame] | 4268 | ** Make a transient copy of expression pExpr and then code it using |
| 4269 | ** sqlite3ExprCode(). This routine works just like sqlite3ExprCode() |
| 4270 | ** except that the input expression is guaranteed to be unchanged. |
| 4271 | */ |
| 4272 | void sqlite3ExprCodeCopy(Parse *pParse, Expr *pExpr, int target){ |
| 4273 | sqlite3 *db = pParse->db; |
| 4274 | pExpr = sqlite3ExprDup(db, pExpr, 0); |
| 4275 | if( !db->mallocFailed ) sqlite3ExprCode(pParse, pExpr, target); |
| 4276 | sqlite3ExprDelete(db, pExpr); |
| 4277 | } |
| 4278 | |
| 4279 | /* |
drh | 05a86c5 | 2014-02-16 01:55:49 +0000 | [diff] [blame] | 4280 | ** Generate code that will evaluate expression pExpr and store the |
| 4281 | ** results in register target. The results are guaranteed to appear |
| 4282 | ** in register target. If the expression is constant, then this routine |
| 4283 | ** might choose to code the expression at initialization time. |
| 4284 | */ |
| 4285 | void sqlite3ExprCodeFactorable(Parse *pParse, Expr *pExpr, int target){ |
drh | b8b0669 | 2018-08-04 15:16:20 +0000 | [diff] [blame] | 4286 | if( pParse->okConstFactor && sqlite3ExprIsConstantNotJoin(pExpr) ){ |
drh | ad879ff | 2017-01-04 04:10:02 +0000 | [diff] [blame] | 4287 | sqlite3ExprCodeAtInit(pParse, pExpr, target); |
drh | 05a86c5 | 2014-02-16 01:55:49 +0000 | [diff] [blame] | 4288 | }else{ |
| 4289 | sqlite3ExprCode(pParse, pExpr, target); |
| 4290 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 4291 | } |
| 4292 | |
| 4293 | /* |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 4294 | ** Generate code that evaluates the given expression and puts the result |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 4295 | ** in register target. |
drh | 2530378 | 2004-12-07 15:41:48 +0000 | [diff] [blame] | 4296 | ** |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 4297 | ** Also make a copy of the expression results into another "cache" register |
| 4298 | ** and modify the expression so that the next time it is evaluated, |
| 4299 | ** the result is a copy of the cache register. |
| 4300 | ** |
| 4301 | ** This routine is used for expressions that are used multiple |
| 4302 | ** times. They are evaluated once and the results of the expression |
| 4303 | ** are reused. |
drh | 2530378 | 2004-12-07 15:41:48 +0000 | [diff] [blame] | 4304 | */ |
drh | 05a86c5 | 2014-02-16 01:55:49 +0000 | [diff] [blame] | 4305 | void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr, int target){ |
drh | 2530378 | 2004-12-07 15:41:48 +0000 | [diff] [blame] | 4306 | Vdbe *v = pParse->pVdbe; |
drh | 05a86c5 | 2014-02-16 01:55:49 +0000 | [diff] [blame] | 4307 | int iMem; |
| 4308 | |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 4309 | assert( target>0 ); |
drh | 05a86c5 | 2014-02-16 01:55:49 +0000 | [diff] [blame] | 4310 | assert( pExpr->op!=TK_REGISTER ); |
| 4311 | sqlite3ExprCode(pParse, pExpr, target); |
| 4312 | iMem = ++pParse->nMem; |
| 4313 | sqlite3VdbeAddOp2(v, OP_Copy, target, iMem); |
| 4314 | exprToRegister(pExpr, iMem); |
drh | 2530378 | 2004-12-07 15:41:48 +0000 | [diff] [blame] | 4315 | } |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 4316 | |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 4317 | /* |
drh | 268380c | 2004-02-25 13:47:31 +0000 | [diff] [blame] | 4318 | ** Generate code that pushes the value of every element of the given |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4319 | ** expression list into a sequence of registers beginning at target. |
drh | 268380c | 2004-02-25 13:47:31 +0000 | [diff] [blame] | 4320 | ** |
drh | 3df6c3b | 2017-09-15 15:38:01 +0000 | [diff] [blame] | 4321 | ** Return the number of elements evaluated. The number returned will |
| 4322 | ** usually be pList->nExpr but might be reduced if SQLITE_ECEL_OMITREF |
| 4323 | ** is defined. |
drh | d1a01ed | 2013-11-21 16:08:52 +0000 | [diff] [blame] | 4324 | ** |
| 4325 | ** The SQLITE_ECEL_DUP flag prevents the arguments from being |
| 4326 | ** filled using OP_SCopy. OP_Copy must be used instead. |
| 4327 | ** |
| 4328 | ** The SQLITE_ECEL_FACTOR argument allows constant arguments to be |
| 4329 | ** factored out into initialization code. |
drh | b0df963 | 2015-10-16 23:55:08 +0000 | [diff] [blame] | 4330 | ** |
| 4331 | ** The SQLITE_ECEL_REF flag means that expressions in the list with |
| 4332 | ** ExprList.a[].u.x.iOrderByCol>0 have already been evaluated and stored |
| 4333 | ** in registers at srcReg, and so the value can be copied from there. |
drh | 3df6c3b | 2017-09-15 15:38:01 +0000 | [diff] [blame] | 4334 | ** If SQLITE_ECEL_OMITREF is also set, then the values with u.x.iOrderByCol>0 |
| 4335 | ** are simply omitted rather than being copied from srcReg. |
drh | 268380c | 2004-02-25 13:47:31 +0000 | [diff] [blame] | 4336 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 4337 | int sqlite3ExprCodeExprList( |
drh | 268380c | 2004-02-25 13:47:31 +0000 | [diff] [blame] | 4338 | Parse *pParse, /* Parsing context */ |
drh | 389a1ad | 2008-01-03 23:44:53 +0000 | [diff] [blame] | 4339 | ExprList *pList, /* The expression list to be coded */ |
drh | 191b54c | 2008-04-15 12:14:21 +0000 | [diff] [blame] | 4340 | int target, /* Where to write results */ |
drh | 5579d59 | 2015-08-26 14:01:41 +0000 | [diff] [blame] | 4341 | int srcReg, /* Source registers if SQLITE_ECEL_REF */ |
drh | d1a01ed | 2013-11-21 16:08:52 +0000 | [diff] [blame] | 4342 | u8 flags /* SQLITE_ECEL_* flags */ |
drh | 268380c | 2004-02-25 13:47:31 +0000 | [diff] [blame] | 4343 | ){ |
| 4344 | struct ExprList_item *pItem; |
drh | 5579d59 | 2015-08-26 14:01:41 +0000 | [diff] [blame] | 4345 | int i, j, n; |
drh | d1a01ed | 2013-11-21 16:08:52 +0000 | [diff] [blame] | 4346 | u8 copyOp = (flags & SQLITE_ECEL_DUP) ? OP_Copy : OP_SCopy; |
drh | 5579d59 | 2015-08-26 14:01:41 +0000 | [diff] [blame] | 4347 | Vdbe *v = pParse->pVdbe; |
drh | 9d8b307 | 2008-08-22 16:29:51 +0000 | [diff] [blame] | 4348 | assert( pList!=0 ); |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 4349 | assert( target>0 ); |
drh | d81a142 | 2010-09-28 07:11:24 +0000 | [diff] [blame] | 4350 | assert( pParse->pVdbe!=0 ); /* Never gets this far otherwise */ |
drh | 268380c | 2004-02-25 13:47:31 +0000 | [diff] [blame] | 4351 | n = pList->nExpr; |
drh | d9f158e | 2013-11-21 20:48:42 +0000 | [diff] [blame] | 4352 | if( !ConstFactorOk(pParse) ) flags &= ~SQLITE_ECEL_FACTOR; |
drh | 191b54c | 2008-04-15 12:14:21 +0000 | [diff] [blame] | 4353 | for(pItem=pList->a, i=0; i<n; i++, pItem++){ |
drh | 7445ffe | 2010-09-27 18:14:12 +0000 | [diff] [blame] | 4354 | Expr *pExpr = pItem->pExpr; |
dan | 24e25d3 | 2018-04-14 18:46:20 +0000 | [diff] [blame] | 4355 | #ifdef SQLITE_ENABLE_SORTER_REFERENCES |
| 4356 | if( pItem->bSorterRef ){ |
| 4357 | i--; |
| 4358 | n--; |
| 4359 | }else |
| 4360 | #endif |
dan | 257c13f | 2016-11-10 20:14:06 +0000 | [diff] [blame] | 4361 | if( (flags & SQLITE_ECEL_REF)!=0 && (j = pItem->u.x.iOrderByCol)>0 ){ |
| 4362 | if( flags & SQLITE_ECEL_OMITREF ){ |
| 4363 | i--; |
| 4364 | n--; |
| 4365 | }else{ |
| 4366 | sqlite3VdbeAddOp2(v, copyOp, j+srcReg-1, target+i); |
| 4367 | } |
drh | b8b0669 | 2018-08-04 15:16:20 +0000 | [diff] [blame] | 4368 | }else if( (flags & SQLITE_ECEL_FACTOR)!=0 |
| 4369 | && sqlite3ExprIsConstantNotJoin(pExpr) |
| 4370 | ){ |
drh | ad879ff | 2017-01-04 04:10:02 +0000 | [diff] [blame] | 4371 | sqlite3ExprCodeAtInit(pParse, pExpr, target+i); |
drh | d1a01ed | 2013-11-21 16:08:52 +0000 | [diff] [blame] | 4372 | }else{ |
| 4373 | int inReg = sqlite3ExprCodeTarget(pParse, pExpr, target+i); |
| 4374 | if( inReg!=target+i ){ |
drh | 4eded60 | 2013-12-20 15:59:20 +0000 | [diff] [blame] | 4375 | VdbeOp *pOp; |
drh | 4eded60 | 2013-12-20 15:59:20 +0000 | [diff] [blame] | 4376 | if( copyOp==OP_Copy |
| 4377 | && (pOp=sqlite3VdbeGetOp(v, -1))->opcode==OP_Copy |
| 4378 | && pOp->p1+pOp->p3+1==inReg |
| 4379 | && pOp->p2+pOp->p3+1==target+i |
| 4380 | ){ |
| 4381 | pOp->p3++; |
| 4382 | }else{ |
| 4383 | sqlite3VdbeAddOp2(v, copyOp, inReg, target+i); |
| 4384 | } |
drh | d1a01ed | 2013-11-21 16:08:52 +0000 | [diff] [blame] | 4385 | } |
drh | d176611 | 2008-09-17 00:13:12 +0000 | [diff] [blame] | 4386 | } |
drh | 268380c | 2004-02-25 13:47:31 +0000 | [diff] [blame] | 4387 | } |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 4388 | return n; |
drh | 268380c | 2004-02-25 13:47:31 +0000 | [diff] [blame] | 4389 | } |
| 4390 | |
| 4391 | /* |
drh | 36c563a | 2009-11-12 13:32:22 +0000 | [diff] [blame] | 4392 | ** Generate code for a BETWEEN operator. |
| 4393 | ** |
| 4394 | ** x BETWEEN y AND z |
| 4395 | ** |
| 4396 | ** The above is equivalent to |
| 4397 | ** |
| 4398 | ** x>=y AND x<=z |
| 4399 | ** |
| 4400 | ** Code it as such, taking care to do the common subexpression |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 4401 | ** elimination of x. |
drh | 84b19a3 | 2016-08-20 22:49:28 +0000 | [diff] [blame] | 4402 | ** |
| 4403 | ** The xJumpIf parameter determines details: |
| 4404 | ** |
| 4405 | ** NULL: Store the boolean result in reg[dest] |
| 4406 | ** sqlite3ExprIfTrue: Jump to dest if true |
| 4407 | ** sqlite3ExprIfFalse: Jump to dest if false |
| 4408 | ** |
| 4409 | ** The jumpIfNull parameter is ignored if xJumpIf is NULL. |
drh | 36c563a | 2009-11-12 13:32:22 +0000 | [diff] [blame] | 4410 | */ |
| 4411 | static void exprCodeBetween( |
| 4412 | Parse *pParse, /* Parsing and code generating context */ |
| 4413 | Expr *pExpr, /* The BETWEEN expression */ |
drh | 84b19a3 | 2016-08-20 22:49:28 +0000 | [diff] [blame] | 4414 | int dest, /* Jump destination or storage location */ |
| 4415 | void (*xJump)(Parse*,Expr*,int,int), /* Action to take */ |
drh | 36c563a | 2009-11-12 13:32:22 +0000 | [diff] [blame] | 4416 | int jumpIfNull /* Take the jump if the BETWEEN is NULL */ |
| 4417 | ){ |
dan | 8b65e59 | 2019-07-17 14:34:17 +0000 | [diff] [blame] | 4418 | Expr exprAnd; /* The AND operator in x>=y AND x<=z */ |
drh | 36c563a | 2009-11-12 13:32:22 +0000 | [diff] [blame] | 4419 | Expr compLeft; /* The x>=y term */ |
| 4420 | Expr compRight; /* The x<=z term */ |
drh | db45bd5 | 2016-08-22 00:48:58 +0000 | [diff] [blame] | 4421 | int regFree1 = 0; /* Temporary use register */ |
dan | 8b65e59 | 2019-07-17 14:34:17 +0000 | [diff] [blame] | 4422 | Expr *pDel = 0; |
| 4423 | sqlite3 *db = pParse->db; |
drh | 84b19a3 | 2016-08-20 22:49:28 +0000 | [diff] [blame] | 4424 | |
dan | 71c57db | 2016-07-09 20:23:55 +0000 | [diff] [blame] | 4425 | memset(&compLeft, 0, sizeof(Expr)); |
| 4426 | memset(&compRight, 0, sizeof(Expr)); |
| 4427 | memset(&exprAnd, 0, sizeof(Expr)); |
drh | db45bd5 | 2016-08-22 00:48:58 +0000 | [diff] [blame] | 4428 | |
| 4429 | assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); |
dan | 8b65e59 | 2019-07-17 14:34:17 +0000 | [diff] [blame] | 4430 | pDel = sqlite3ExprDup(db, pExpr->pLeft, 0); |
| 4431 | if( db->mallocFailed==0 ){ |
| 4432 | exprAnd.op = TK_AND; |
| 4433 | exprAnd.pLeft = &compLeft; |
| 4434 | exprAnd.pRight = &compRight; |
| 4435 | compLeft.op = TK_GE; |
| 4436 | compLeft.pLeft = pDel; |
| 4437 | compLeft.pRight = pExpr->x.pList->a[0].pExpr; |
| 4438 | compRight.op = TK_LE; |
| 4439 | compRight.pLeft = pDel; |
| 4440 | compRight.pRight = pExpr->x.pList->a[1].pExpr; |
| 4441 | exprToRegister(pDel, exprCodeVector(pParse, pDel, ®Free1)); |
| 4442 | if( xJump ){ |
| 4443 | xJump(pParse, &exprAnd, dest, jumpIfNull); |
| 4444 | }else{ |
| 4445 | /* Mark the expression is being from the ON or USING clause of a join |
| 4446 | ** so that the sqlite3ExprCodeTarget() routine will not attempt to move |
| 4447 | ** it into the Parse.pConstExpr list. We should use a new bit for this, |
| 4448 | ** for clarity, but we are out of bits in the Expr.flags field so we |
| 4449 | ** have to reuse the EP_FromJoin bit. Bummer. */ |
| 4450 | pDel->flags |= EP_FromJoin; |
| 4451 | sqlite3ExprCodeTarget(pParse, &exprAnd, dest); |
| 4452 | } |
| 4453 | sqlite3ReleaseTempReg(pParse, regFree1); |
drh | 36c563a | 2009-11-12 13:32:22 +0000 | [diff] [blame] | 4454 | } |
dan | 8b65e59 | 2019-07-17 14:34:17 +0000 | [diff] [blame] | 4455 | sqlite3ExprDelete(db, pDel); |
drh | 36c563a | 2009-11-12 13:32:22 +0000 | [diff] [blame] | 4456 | |
| 4457 | /* Ensure adequate test coverage */ |
drh | db45bd5 | 2016-08-22 00:48:58 +0000 | [diff] [blame] | 4458 | testcase( xJump==sqlite3ExprIfTrue && jumpIfNull==0 && regFree1==0 ); |
| 4459 | testcase( xJump==sqlite3ExprIfTrue && jumpIfNull==0 && regFree1!=0 ); |
| 4460 | testcase( xJump==sqlite3ExprIfTrue && jumpIfNull!=0 && regFree1==0 ); |
| 4461 | testcase( xJump==sqlite3ExprIfTrue && jumpIfNull!=0 && regFree1!=0 ); |
| 4462 | testcase( xJump==sqlite3ExprIfFalse && jumpIfNull==0 && regFree1==0 ); |
| 4463 | testcase( xJump==sqlite3ExprIfFalse && jumpIfNull==0 && regFree1!=0 ); |
| 4464 | testcase( xJump==sqlite3ExprIfFalse && jumpIfNull!=0 && regFree1==0 ); |
| 4465 | testcase( xJump==sqlite3ExprIfFalse && jumpIfNull!=0 && regFree1!=0 ); |
drh | 84b19a3 | 2016-08-20 22:49:28 +0000 | [diff] [blame] | 4466 | testcase( xJump==0 ); |
drh | 36c563a | 2009-11-12 13:32:22 +0000 | [diff] [blame] | 4467 | } |
| 4468 | |
| 4469 | /* |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 4470 | ** Generate code for a boolean expression such that a jump is made |
| 4471 | ** to the label "dest" if the expression is true but execution |
| 4472 | ** continues straight thru if the expression is false. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 4473 | ** |
| 4474 | ** If the expression evaluates to NULL (neither true nor false), then |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 4475 | ** take the jump if the jumpIfNull flag is SQLITE_JUMPIFNULL. |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 4476 | ** |
| 4477 | ** This code depends on the fact that certain token values (ex: TK_EQ) |
| 4478 | ** are the same as opcode values (ex: OP_Eq) that implement the corresponding |
| 4479 | ** operation. Special comments in vdbe.c and the mkopcodeh.awk script in |
| 4480 | ** the make process cause these values to align. Assert()s in the code |
| 4481 | ** below verify that the numbers are aligned correctly. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 4482 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 4483 | void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 4484 | Vdbe *v = pParse->pVdbe; |
| 4485 | int op = 0; |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 4486 | int regFree1 = 0; |
| 4487 | int regFree2 = 0; |
| 4488 | int r1, r2; |
| 4489 | |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 4490 | assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 ); |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 4491 | if( NEVER(v==0) ) return; /* Existence of VDBE checked by caller */ |
drh | 33cd490 | 2009-05-30 20:49:20 +0000 | [diff] [blame] | 4492 | if( NEVER(pExpr==0) ) return; /* No way this can happen */ |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 4493 | op = pExpr->op; |
dan | 7b35a77 | 2016-07-28 19:47:15 +0000 | [diff] [blame] | 4494 | switch( op ){ |
drh | 17180fc | 2019-04-19 17:26:19 +0000 | [diff] [blame] | 4495 | case TK_AND: |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 4496 | case TK_OR: { |
drh | 17180fc | 2019-04-19 17:26:19 +0000 | [diff] [blame] | 4497 | Expr *pAlt = sqlite3ExprSimplifiedAndOr(pExpr); |
| 4498 | if( pAlt!=pExpr ){ |
| 4499 | sqlite3ExprIfTrue(pParse, pAlt, dest, jumpIfNull); |
| 4500 | }else if( op==TK_AND ){ |
| 4501 | int d2 = sqlite3VdbeMakeLabel(pParse); |
| 4502 | testcase( jumpIfNull==0 ); |
| 4503 | sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, |
| 4504 | jumpIfNull^SQLITE_JUMPIFNULL); |
| 4505 | sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); |
| 4506 | sqlite3VdbeResolveLabel(v, d2); |
| 4507 | }else{ |
| 4508 | testcase( jumpIfNull==0 ); |
| 4509 | sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); |
| 4510 | sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); |
| 4511 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 4512 | break; |
| 4513 | } |
| 4514 | case TK_NOT: { |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 4515 | testcase( jumpIfNull==0 ); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 4516 | sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 4517 | break; |
| 4518 | } |
drh | 8abed7b | 2018-02-26 18:49:05 +0000 | [diff] [blame] | 4519 | case TK_TRUTH: { |
drh | 96acafb | 2018-02-27 14:49:25 +0000 | [diff] [blame] | 4520 | int isNot; /* IS NOT TRUE or IS NOT FALSE */ |
| 4521 | int isTrue; /* IS TRUE or IS NOT TRUE */ |
drh | 007c843 | 2018-02-26 03:20:18 +0000 | [diff] [blame] | 4522 | testcase( jumpIfNull==0 ); |
drh | 8abed7b | 2018-02-26 18:49:05 +0000 | [diff] [blame] | 4523 | isNot = pExpr->op2==TK_ISNOT; |
drh | 96acafb | 2018-02-27 14:49:25 +0000 | [diff] [blame] | 4524 | isTrue = sqlite3ExprTruthValue(pExpr->pRight); |
drh | 43c4ac8 | 2018-02-26 21:26:27 +0000 | [diff] [blame] | 4525 | testcase( isTrue && isNot ); |
drh | 96acafb | 2018-02-27 14:49:25 +0000 | [diff] [blame] | 4526 | testcase( !isTrue && isNot ); |
drh | 43c4ac8 | 2018-02-26 21:26:27 +0000 | [diff] [blame] | 4527 | if( isTrue ^ isNot ){ |
drh | 8abed7b | 2018-02-26 18:49:05 +0000 | [diff] [blame] | 4528 | sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, |
| 4529 | isNot ? SQLITE_JUMPIFNULL : 0); |
| 4530 | }else{ |
| 4531 | sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, |
| 4532 | isNot ? SQLITE_JUMPIFNULL : 0); |
| 4533 | } |
drh | 007c843 | 2018-02-26 03:20:18 +0000 | [diff] [blame] | 4534 | break; |
| 4535 | } |
drh | de845c2 | 2016-03-17 19:07:52 +0000 | [diff] [blame] | 4536 | case TK_IS: |
| 4537 | case TK_ISNOT: |
| 4538 | testcase( op==TK_IS ); |
| 4539 | testcase( op==TK_ISNOT ); |
| 4540 | op = (op==TK_IS) ? TK_EQ : TK_NE; |
| 4541 | jumpIfNull = SQLITE_NULLEQ; |
| 4542 | /* Fall thru */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 4543 | case TK_LT: |
| 4544 | case TK_LE: |
| 4545 | case TK_GT: |
| 4546 | case TK_GE: |
| 4547 | case TK_NE: |
drh | 0ac6589 | 2002-04-20 14:24:41 +0000 | [diff] [blame] | 4548 | case TK_EQ: { |
dan | 625015e | 2016-07-30 16:39:28 +0000 | [diff] [blame] | 4549 | if( sqlite3ExprIsVector(pExpr->pLeft) ) goto default_expr; |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 4550 | testcase( jumpIfNull==0 ); |
drh | b6da74e | 2009-12-24 16:00:28 +0000 | [diff] [blame] | 4551 | r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); |
| 4552 | r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 4553 | codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 4554 | r1, r2, dest, jumpIfNull); |
drh | 7d17610 | 2014-02-18 03:07:12 +0000 | [diff] [blame] | 4555 | assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt); |
| 4556 | assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le); |
| 4557 | assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt); |
| 4558 | assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge); |
drh | de845c2 | 2016-03-17 19:07:52 +0000 | [diff] [blame] | 4559 | assert(TK_EQ==OP_Eq); testcase(op==OP_Eq); |
| 4560 | VdbeCoverageIf(v, op==OP_Eq && jumpIfNull==SQLITE_NULLEQ); |
| 4561 | VdbeCoverageIf(v, op==OP_Eq && jumpIfNull!=SQLITE_NULLEQ); |
| 4562 | assert(TK_NE==OP_Ne); testcase(op==OP_Ne); |
| 4563 | VdbeCoverageIf(v, op==OP_Ne && jumpIfNull==SQLITE_NULLEQ); |
| 4564 | VdbeCoverageIf(v, op==OP_Ne && jumpIfNull!=SQLITE_NULLEQ); |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 4565 | testcase( regFree1==0 ); |
| 4566 | testcase( regFree2==0 ); |
| 4567 | break; |
| 4568 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 4569 | case TK_ISNULL: |
| 4570 | case TK_NOTNULL: { |
drh | 7d17610 | 2014-02-18 03:07:12 +0000 | [diff] [blame] | 4571 | assert( TK_ISNULL==OP_IsNull ); testcase( op==TK_ISNULL ); |
| 4572 | assert( TK_NOTNULL==OP_NotNull ); testcase( op==TK_NOTNULL ); |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 4573 | r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); |
| 4574 | sqlite3VdbeAddOp2(v, op, r1, dest); |
drh | 7d17610 | 2014-02-18 03:07:12 +0000 | [diff] [blame] | 4575 | VdbeCoverageIf(v, op==TK_ISNULL); |
| 4576 | VdbeCoverageIf(v, op==TK_NOTNULL); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 4577 | testcase( regFree1==0 ); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 4578 | break; |
| 4579 | } |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 4580 | case TK_BETWEEN: { |
drh | 5c03f30 | 2009-11-13 15:03:59 +0000 | [diff] [blame] | 4581 | testcase( jumpIfNull==0 ); |
dan | 71c57db | 2016-07-09 20:23:55 +0000 | [diff] [blame] | 4582 | exprCodeBetween(pParse, pExpr, dest, sqlite3ExprIfTrue, jumpIfNull); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 4583 | break; |
| 4584 | } |
drh | 84e30ca | 2011-02-10 17:46:14 +0000 | [diff] [blame] | 4585 | #ifndef SQLITE_OMIT_SUBQUERY |
drh | e3365e6 | 2009-11-12 17:52:24 +0000 | [diff] [blame] | 4586 | case TK_IN: { |
drh | ec4ccdb | 2018-12-29 02:26:59 +0000 | [diff] [blame] | 4587 | int destIfFalse = sqlite3VdbeMakeLabel(pParse); |
drh | e3365e6 | 2009-11-12 17:52:24 +0000 | [diff] [blame] | 4588 | int destIfNull = jumpIfNull ? dest : destIfFalse; |
| 4589 | sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull); |
drh | 076e85f | 2015-09-03 13:46:12 +0000 | [diff] [blame] | 4590 | sqlite3VdbeGoto(v, dest); |
drh | e3365e6 | 2009-11-12 17:52:24 +0000 | [diff] [blame] | 4591 | sqlite3VdbeResolveLabel(v, destIfFalse); |
| 4592 | break; |
| 4593 | } |
shaneh | bb20134 | 2011-02-09 19:55:20 +0000 | [diff] [blame] | 4594 | #endif |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 4595 | default: { |
dan | 7b35a77 | 2016-07-28 19:47:15 +0000 | [diff] [blame] | 4596 | default_expr: |
drh | ad31727 | 2019-04-19 16:21:51 +0000 | [diff] [blame] | 4597 | if( ExprAlwaysTrue(pExpr) ){ |
drh | 076e85f | 2015-09-03 13:46:12 +0000 | [diff] [blame] | 4598 | sqlite3VdbeGoto(v, dest); |
drh | ad31727 | 2019-04-19 16:21:51 +0000 | [diff] [blame] | 4599 | }else if( ExprAlwaysFalse(pExpr) ){ |
drh | 991a198 | 2014-01-02 17:57:16 +0000 | [diff] [blame] | 4600 | /* No-op */ |
| 4601 | }else{ |
| 4602 | r1 = sqlite3ExprCodeTemp(pParse, pExpr, ®Free1); |
| 4603 | sqlite3VdbeAddOp3(v, OP_If, r1, dest, jumpIfNull!=0); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 4604 | VdbeCoverage(v); |
drh | 991a198 | 2014-01-02 17:57:16 +0000 | [diff] [blame] | 4605 | testcase( regFree1==0 ); |
| 4606 | testcase( jumpIfNull==0 ); |
| 4607 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 4608 | break; |
| 4609 | } |
| 4610 | } |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 4611 | sqlite3ReleaseTempReg(pParse, regFree1); |
| 4612 | sqlite3ReleaseTempReg(pParse, regFree2); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 4613 | } |
| 4614 | |
| 4615 | /* |
drh | 66b89c8 | 2000-11-28 20:47:17 +0000 | [diff] [blame] | 4616 | ** Generate code for a boolean expression such that a jump is made |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 4617 | ** to the label "dest" if the expression is false but execution |
| 4618 | ** continues straight thru if the expression is true. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 4619 | ** |
| 4620 | ** If the expression evaluates to NULL (neither true nor false) then |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 4621 | ** jump if jumpIfNull is SQLITE_JUMPIFNULL or fall through if jumpIfNull |
| 4622 | ** is 0. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 4623 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 4624 | void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 4625 | Vdbe *v = pParse->pVdbe; |
| 4626 | int op = 0; |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 4627 | int regFree1 = 0; |
| 4628 | int regFree2 = 0; |
| 4629 | int r1, r2; |
| 4630 | |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 4631 | assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 ); |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 4632 | if( NEVER(v==0) ) return; /* Existence of VDBE checked by caller */ |
drh | 33cd490 | 2009-05-30 20:49:20 +0000 | [diff] [blame] | 4633 | if( pExpr==0 ) return; |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 4634 | |
| 4635 | /* The value of pExpr->op and op are related as follows: |
| 4636 | ** |
| 4637 | ** pExpr->op op |
| 4638 | ** --------- ---------- |
| 4639 | ** TK_ISNULL OP_NotNull |
| 4640 | ** TK_NOTNULL OP_IsNull |
| 4641 | ** TK_NE OP_Eq |
| 4642 | ** TK_EQ OP_Ne |
| 4643 | ** TK_GT OP_Le |
| 4644 | ** TK_LE OP_Gt |
| 4645 | ** TK_GE OP_Lt |
| 4646 | ** TK_LT OP_Ge |
| 4647 | ** |
| 4648 | ** For other values of pExpr->op, op is undefined and unused. |
| 4649 | ** The value of TK_ and OP_ constants are arranged such that we |
| 4650 | ** can compute the mapping above using the following expression. |
| 4651 | ** Assert()s verify that the computation is correct. |
| 4652 | */ |
| 4653 | op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1); |
| 4654 | |
| 4655 | /* Verify correct alignment of TK_ and OP_ constants |
| 4656 | */ |
| 4657 | assert( pExpr->op!=TK_ISNULL || op==OP_NotNull ); |
| 4658 | assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull ); |
| 4659 | assert( pExpr->op!=TK_NE || op==OP_Eq ); |
| 4660 | assert( pExpr->op!=TK_EQ || op==OP_Ne ); |
| 4661 | assert( pExpr->op!=TK_LT || op==OP_Ge ); |
| 4662 | assert( pExpr->op!=TK_LE || op==OP_Gt ); |
| 4663 | assert( pExpr->op!=TK_GT || op==OP_Le ); |
| 4664 | assert( pExpr->op!=TK_GE || op==OP_Lt ); |
| 4665 | |
dan | ba00e30 | 2016-07-23 20:24:06 +0000 | [diff] [blame] | 4666 | switch( pExpr->op ){ |
drh | 17180fc | 2019-04-19 17:26:19 +0000 | [diff] [blame] | 4667 | case TK_AND: |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 4668 | case TK_OR: { |
drh | 17180fc | 2019-04-19 17:26:19 +0000 | [diff] [blame] | 4669 | Expr *pAlt = sqlite3ExprSimplifiedAndOr(pExpr); |
| 4670 | if( pAlt!=pExpr ){ |
| 4671 | sqlite3ExprIfFalse(pParse, pAlt, dest, jumpIfNull); |
| 4672 | }else if( pExpr->op==TK_AND ){ |
| 4673 | testcase( jumpIfNull==0 ); |
| 4674 | sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); |
| 4675 | sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); |
| 4676 | }else{ |
| 4677 | int d2 = sqlite3VdbeMakeLabel(pParse); |
| 4678 | testcase( jumpIfNull==0 ); |
| 4679 | sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, |
| 4680 | jumpIfNull^SQLITE_JUMPIFNULL); |
| 4681 | sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); |
| 4682 | sqlite3VdbeResolveLabel(v, d2); |
| 4683 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 4684 | break; |
| 4685 | } |
| 4686 | case TK_NOT: { |
drh | 5c03f30 | 2009-11-13 15:03:59 +0000 | [diff] [blame] | 4687 | testcase( jumpIfNull==0 ); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 4688 | sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 4689 | break; |
| 4690 | } |
drh | 8abed7b | 2018-02-26 18:49:05 +0000 | [diff] [blame] | 4691 | case TK_TRUTH: { |
drh | 96acafb | 2018-02-27 14:49:25 +0000 | [diff] [blame] | 4692 | int isNot; /* IS NOT TRUE or IS NOT FALSE */ |
| 4693 | int isTrue; /* IS TRUE or IS NOT TRUE */ |
drh | 8abed7b | 2018-02-26 18:49:05 +0000 | [diff] [blame] | 4694 | testcase( jumpIfNull==0 ); |
drh | 8abed7b | 2018-02-26 18:49:05 +0000 | [diff] [blame] | 4695 | isNot = pExpr->op2==TK_ISNOT; |
drh | 96acafb | 2018-02-27 14:49:25 +0000 | [diff] [blame] | 4696 | isTrue = sqlite3ExprTruthValue(pExpr->pRight); |
drh | 43c4ac8 | 2018-02-26 21:26:27 +0000 | [diff] [blame] | 4697 | testcase( isTrue && isNot ); |
drh | 96acafb | 2018-02-27 14:49:25 +0000 | [diff] [blame] | 4698 | testcase( !isTrue && isNot ); |
drh | 43c4ac8 | 2018-02-26 21:26:27 +0000 | [diff] [blame] | 4699 | if( isTrue ^ isNot ){ |
drh | 8abed7b | 2018-02-26 18:49:05 +0000 | [diff] [blame] | 4700 | /* IS TRUE and IS NOT FALSE */ |
| 4701 | sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, |
| 4702 | isNot ? 0 : SQLITE_JUMPIFNULL); |
| 4703 | |
| 4704 | }else{ |
| 4705 | /* IS FALSE and IS NOT TRUE */ |
| 4706 | sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, |
drh | 96acafb | 2018-02-27 14:49:25 +0000 | [diff] [blame] | 4707 | isNot ? 0 : SQLITE_JUMPIFNULL); |
drh | 8abed7b | 2018-02-26 18:49:05 +0000 | [diff] [blame] | 4708 | } |
drh | 007c843 | 2018-02-26 03:20:18 +0000 | [diff] [blame] | 4709 | break; |
| 4710 | } |
drh | de845c2 | 2016-03-17 19:07:52 +0000 | [diff] [blame] | 4711 | case TK_IS: |
| 4712 | case TK_ISNOT: |
| 4713 | testcase( pExpr->op==TK_IS ); |
| 4714 | testcase( pExpr->op==TK_ISNOT ); |
| 4715 | op = (pExpr->op==TK_IS) ? TK_NE : TK_EQ; |
| 4716 | jumpIfNull = SQLITE_NULLEQ; |
| 4717 | /* Fall thru */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 4718 | case TK_LT: |
| 4719 | case TK_LE: |
| 4720 | case TK_GT: |
| 4721 | case TK_GE: |
| 4722 | case TK_NE: |
| 4723 | case TK_EQ: { |
dan | 625015e | 2016-07-30 16:39:28 +0000 | [diff] [blame] | 4724 | if( sqlite3ExprIsVector(pExpr->pLeft) ) goto default_expr; |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 4725 | testcase( jumpIfNull==0 ); |
drh | b6da74e | 2009-12-24 16:00:28 +0000 | [diff] [blame] | 4726 | r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); |
| 4727 | r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 4728 | codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 4729 | r1, r2, dest, jumpIfNull); |
drh | 7d17610 | 2014-02-18 03:07:12 +0000 | [diff] [blame] | 4730 | assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt); |
| 4731 | assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le); |
| 4732 | assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt); |
| 4733 | assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge); |
drh | de845c2 | 2016-03-17 19:07:52 +0000 | [diff] [blame] | 4734 | assert(TK_EQ==OP_Eq); testcase(op==OP_Eq); |
| 4735 | VdbeCoverageIf(v, op==OP_Eq && jumpIfNull!=SQLITE_NULLEQ); |
| 4736 | VdbeCoverageIf(v, op==OP_Eq && jumpIfNull==SQLITE_NULLEQ); |
| 4737 | assert(TK_NE==OP_Ne); testcase(op==OP_Ne); |
| 4738 | VdbeCoverageIf(v, op==OP_Ne && jumpIfNull!=SQLITE_NULLEQ); |
| 4739 | VdbeCoverageIf(v, op==OP_Ne && jumpIfNull==SQLITE_NULLEQ); |
drh | 6a2fe09 | 2009-09-23 02:29:36 +0000 | [diff] [blame] | 4740 | testcase( regFree1==0 ); |
| 4741 | testcase( regFree2==0 ); |
| 4742 | break; |
| 4743 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 4744 | case TK_ISNULL: |
| 4745 | case TK_NOTNULL: { |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 4746 | r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); |
| 4747 | sqlite3VdbeAddOp2(v, op, r1, dest); |
drh | 7d17610 | 2014-02-18 03:07:12 +0000 | [diff] [blame] | 4748 | testcase( op==TK_ISNULL ); VdbeCoverageIf(v, op==TK_ISNULL); |
| 4749 | testcase( op==TK_NOTNULL ); VdbeCoverageIf(v, op==TK_NOTNULL); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 4750 | testcase( regFree1==0 ); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 4751 | break; |
| 4752 | } |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 4753 | case TK_BETWEEN: { |
drh | 5c03f30 | 2009-11-13 15:03:59 +0000 | [diff] [blame] | 4754 | testcase( jumpIfNull==0 ); |
dan | 71c57db | 2016-07-09 20:23:55 +0000 | [diff] [blame] | 4755 | exprCodeBetween(pParse, pExpr, dest, sqlite3ExprIfFalse, jumpIfNull); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 4756 | break; |
| 4757 | } |
drh | 84e30ca | 2011-02-10 17:46:14 +0000 | [diff] [blame] | 4758 | #ifndef SQLITE_OMIT_SUBQUERY |
drh | e3365e6 | 2009-11-12 17:52:24 +0000 | [diff] [blame] | 4759 | case TK_IN: { |
| 4760 | if( jumpIfNull ){ |
| 4761 | sqlite3ExprCodeIN(pParse, pExpr, dest, dest); |
| 4762 | }else{ |
drh | ec4ccdb | 2018-12-29 02:26:59 +0000 | [diff] [blame] | 4763 | int destIfNull = sqlite3VdbeMakeLabel(pParse); |
drh | e3365e6 | 2009-11-12 17:52:24 +0000 | [diff] [blame] | 4764 | sqlite3ExprCodeIN(pParse, pExpr, dest, destIfNull); |
| 4765 | sqlite3VdbeResolveLabel(v, destIfNull); |
| 4766 | } |
| 4767 | break; |
| 4768 | } |
shaneh | bb20134 | 2011-02-09 19:55:20 +0000 | [diff] [blame] | 4769 | #endif |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 4770 | default: { |
dan | ba00e30 | 2016-07-23 20:24:06 +0000 | [diff] [blame] | 4771 | default_expr: |
drh | ad31727 | 2019-04-19 16:21:51 +0000 | [diff] [blame] | 4772 | if( ExprAlwaysFalse(pExpr) ){ |
drh | 076e85f | 2015-09-03 13:46:12 +0000 | [diff] [blame] | 4773 | sqlite3VdbeGoto(v, dest); |
drh | ad31727 | 2019-04-19 16:21:51 +0000 | [diff] [blame] | 4774 | }else if( ExprAlwaysTrue(pExpr) ){ |
drh | 991a198 | 2014-01-02 17:57:16 +0000 | [diff] [blame] | 4775 | /* no-op */ |
| 4776 | }else{ |
| 4777 | r1 = sqlite3ExprCodeTemp(pParse, pExpr, ®Free1); |
| 4778 | sqlite3VdbeAddOp3(v, OP_IfNot, r1, dest, jumpIfNull!=0); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 4779 | VdbeCoverage(v); |
drh | 991a198 | 2014-01-02 17:57:16 +0000 | [diff] [blame] | 4780 | testcase( regFree1==0 ); |
| 4781 | testcase( jumpIfNull==0 ); |
| 4782 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 4783 | break; |
| 4784 | } |
| 4785 | } |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 4786 | sqlite3ReleaseTempReg(pParse, regFree1); |
| 4787 | sqlite3ReleaseTempReg(pParse, regFree2); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 4788 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 4789 | |
| 4790 | /* |
drh | 72bc820 | 2015-06-11 13:58:35 +0000 | [diff] [blame] | 4791 | ** Like sqlite3ExprIfFalse() except that a copy is made of pExpr before |
| 4792 | ** code generation, and that copy is deleted after code generation. This |
| 4793 | ** ensures that the original pExpr is unchanged. |
| 4794 | */ |
| 4795 | void sqlite3ExprIfFalseDup(Parse *pParse, Expr *pExpr, int dest,int jumpIfNull){ |
| 4796 | sqlite3 *db = pParse->db; |
| 4797 | Expr *pCopy = sqlite3ExprDup(db, pExpr, 0); |
| 4798 | if( db->mallocFailed==0 ){ |
| 4799 | sqlite3ExprIfFalse(pParse, pCopy, dest, jumpIfNull); |
| 4800 | } |
| 4801 | sqlite3ExprDelete(db, pCopy); |
| 4802 | } |
| 4803 | |
dan | 5aa550c | 2017-06-24 18:10:29 +0000 | [diff] [blame] | 4804 | /* |
| 4805 | ** Expression pVar is guaranteed to be an SQL variable. pExpr may be any |
| 4806 | ** type of expression. |
| 4807 | ** |
| 4808 | ** If pExpr is a simple SQL value - an integer, real, string, blob |
| 4809 | ** or NULL value - then the VDBE currently being prepared is configured |
| 4810 | ** to re-prepare each time a new value is bound to variable pVar. |
| 4811 | ** |
| 4812 | ** Additionally, if pExpr is a simple SQL value and the value is the |
| 4813 | ** same as that currently bound to variable pVar, non-zero is returned. |
| 4814 | ** Otherwise, if the values are not the same or if pExpr is not a simple |
| 4815 | ** SQL value, zero is returned. |
| 4816 | */ |
| 4817 | static int exprCompareVariable(Parse *pParse, Expr *pVar, Expr *pExpr){ |
| 4818 | int res = 0; |
drh | c080422 | 2017-06-28 21:47:16 +0000 | [diff] [blame] | 4819 | int iVar; |
| 4820 | sqlite3_value *pL, *pR = 0; |
| 4821 | |
| 4822 | sqlite3ValueFromExpr(pParse->db, pExpr, SQLITE_UTF8, SQLITE_AFF_BLOB, &pR); |
| 4823 | if( pR ){ |
| 4824 | iVar = pVar->iColumn; |
dan | 5aa550c | 2017-06-24 18:10:29 +0000 | [diff] [blame] | 4825 | sqlite3VdbeSetVarmask(pParse->pVdbe, iVar); |
dan | 5aa550c | 2017-06-24 18:10:29 +0000 | [diff] [blame] | 4826 | pL = sqlite3VdbeGetBoundValue(pParse->pReprepare, iVar, SQLITE_AFF_BLOB); |
drh | 5aa307e | 2017-06-29 01:23:12 +0000 | [diff] [blame] | 4827 | if( pL ){ |
| 4828 | if( sqlite3_value_type(pL)==SQLITE_TEXT ){ |
| 4829 | sqlite3_value_text(pL); /* Make sure the encoding is UTF-8 */ |
| 4830 | } |
| 4831 | res = 0==sqlite3MemCompare(pL, pR, 0); |
dan | 5aa550c | 2017-06-24 18:10:29 +0000 | [diff] [blame] | 4832 | } |
drh | c080422 | 2017-06-28 21:47:16 +0000 | [diff] [blame] | 4833 | sqlite3ValueFree(pR); |
| 4834 | sqlite3ValueFree(pL); |
dan | 5aa550c | 2017-06-24 18:10:29 +0000 | [diff] [blame] | 4835 | } |
| 4836 | |
| 4837 | return res; |
| 4838 | } |
drh | 72bc820 | 2015-06-11 13:58:35 +0000 | [diff] [blame] | 4839 | |
| 4840 | /* |
drh | 1d9da70 | 2010-01-07 15:17:02 +0000 | [diff] [blame] | 4841 | ** Do a deep comparison of two expression trees. Return 0 if the two |
| 4842 | ** expressions are completely identical. Return 1 if they differ only |
| 4843 | ** by a COLLATE operator at the top level. Return 2 if there are differences |
| 4844 | ** other than the top-level COLLATE operator. |
drh | d40aab0 | 2007-02-24 15:29:03 +0000 | [diff] [blame] | 4845 | ** |
drh | 619a130 | 2013-08-01 13:04:46 +0000 | [diff] [blame] | 4846 | ** If any subelement of pB has Expr.iTable==(-1) then it is allowed |
| 4847 | ** to compare equal to an equivalent element in pA with Expr.iTable==iTab. |
| 4848 | ** |
drh | 66518ca | 2013-08-01 15:09:57 +0000 | [diff] [blame] | 4849 | ** The pA side might be using TK_REGISTER. If that is the case and pB is |
| 4850 | ** not using TK_REGISTER but is otherwise equivalent, then still return 0. |
| 4851 | ** |
drh | 1d9da70 | 2010-01-07 15:17:02 +0000 | [diff] [blame] | 4852 | ** Sometimes this routine will return 2 even if the two expressions |
drh | d40aab0 | 2007-02-24 15:29:03 +0000 | [diff] [blame] | 4853 | ** really are equivalent. If we cannot prove that the expressions are |
drh | 1d9da70 | 2010-01-07 15:17:02 +0000 | [diff] [blame] | 4854 | ** identical, we return 2 just to be safe. So if this routine |
| 4855 | ** returns 2, then you do not really know for certain if the two |
| 4856 | ** expressions are the same. But if you get a 0 or 1 return, then you |
drh | d40aab0 | 2007-02-24 15:29:03 +0000 | [diff] [blame] | 4857 | ** can be sure the expressions are the same. In the places where |
drh | 1d9da70 | 2010-01-07 15:17:02 +0000 | [diff] [blame] | 4858 | ** this routine is used, it does not hurt to get an extra 2 - that |
drh | d40aab0 | 2007-02-24 15:29:03 +0000 | [diff] [blame] | 4859 | ** just might result in some slightly slower code. But returning |
drh | 1d9da70 | 2010-01-07 15:17:02 +0000 | [diff] [blame] | 4860 | ** an incorrect 0 or 1 could lead to a malfunction. |
dan | 5aa550c | 2017-06-24 18:10:29 +0000 | [diff] [blame] | 4861 | ** |
drh | c080422 | 2017-06-28 21:47:16 +0000 | [diff] [blame] | 4862 | ** If pParse is not NULL then TK_VARIABLE terms in pA with bindings in |
| 4863 | ** pParse->pReprepare can be matched against literals in pB. The |
| 4864 | ** pParse->pVdbe->expmask bitmask is updated for each variable referenced. |
| 4865 | ** If pParse is NULL (the normal case) then any TK_VARIABLE term in |
| 4866 | ** Argument pParse should normally be NULL. If it is not NULL and pA or |
| 4867 | ** pB causes a return value of 2. |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 4868 | */ |
dan | 5aa550c | 2017-06-24 18:10:29 +0000 | [diff] [blame] | 4869 | int sqlite3ExprCompare(Parse *pParse, Expr *pA, Expr *pB, int iTab){ |
drh | 10d1edf | 2013-11-15 15:52:39 +0000 | [diff] [blame] | 4870 | u32 combinedFlags; |
| 4871 | if( pA==0 || pB==0 ){ |
drh | 1d9da70 | 2010-01-07 15:17:02 +0000 | [diff] [blame] | 4872 | return pB==pA ? 0 : 2; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 4873 | } |
dan | 5aa550c | 2017-06-24 18:10:29 +0000 | [diff] [blame] | 4874 | if( pParse && pA->op==TK_VARIABLE && exprCompareVariable(pParse, pA, pB) ){ |
| 4875 | return 0; |
| 4876 | } |
drh | 10d1edf | 2013-11-15 15:52:39 +0000 | [diff] [blame] | 4877 | combinedFlags = pA->flags | pB->flags; |
| 4878 | if( combinedFlags & EP_IntValue ){ |
| 4879 | if( (pA->flags&pB->flags&EP_IntValue)!=0 && pA->u.iValue==pB->u.iValue ){ |
| 4880 | return 0; |
| 4881 | } |
drh | 1d9da70 | 2010-01-07 15:17:02 +0000 | [diff] [blame] | 4882 | return 2; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 4883 | } |
dan | 16dd398 | 2018-12-20 15:04:38 +0000 | [diff] [blame] | 4884 | if( pA->op!=pB->op || pA->op==TK_RAISE ){ |
dan | 5aa550c | 2017-06-24 18:10:29 +0000 | [diff] [blame] | 4885 | if( pA->op==TK_COLLATE && sqlite3ExprCompare(pParse, pA->pLeft,pB,iTab)<2 ){ |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 4886 | return 1; |
| 4887 | } |
dan | 5aa550c | 2017-06-24 18:10:29 +0000 | [diff] [blame] | 4888 | if( pB->op==TK_COLLATE && sqlite3ExprCompare(pParse, pA,pB->pLeft,iTab)<2 ){ |
drh | ae80dde | 2012-12-06 21:16:43 +0000 | [diff] [blame] | 4889 | return 1; |
| 4890 | } |
| 4891 | return 2; |
| 4892 | } |
drh | 2edc5fd | 2015-11-24 02:10:52 +0000 | [diff] [blame] | 4893 | if( pA->op!=TK_COLUMN && pA->op!=TK_AGG_COLUMN && pA->u.zToken ){ |
dan | 4f9adee | 2019-07-13 16:22:50 +0000 | [diff] [blame] | 4894 | if( pA->op==TK_FUNCTION || pA->op==TK_AGG_FUNCTION ){ |
drh | 390b88a | 2015-08-31 18:13:01 +0000 | [diff] [blame] | 4895 | if( sqlite3StrICmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2; |
drh | eda079c | 2018-09-20 19:02:15 +0000 | [diff] [blame] | 4896 | #ifndef SQLITE_OMIT_WINDOWFUNC |
dan | 4f9adee | 2019-07-13 16:22:50 +0000 | [diff] [blame] | 4897 | assert( pA->op==pB->op ); |
| 4898 | if( ExprHasProperty(pA,EP_WinFunc)!=ExprHasProperty(pB,EP_WinFunc) ){ |
| 4899 | return 2; |
| 4900 | } |
drh | eda079c | 2018-09-20 19:02:15 +0000 | [diff] [blame] | 4901 | if( ExprHasProperty(pA,EP_WinFunc) ){ |
dan | 4f9adee | 2019-07-13 16:22:50 +0000 | [diff] [blame] | 4902 | if( sqlite3WindowCompare(pParse, pA->y.pWin, pB->y.pWin, 1)!=0 ){ |
| 4903 | return 2; |
| 4904 | } |
drh | eda079c | 2018-09-20 19:02:15 +0000 | [diff] [blame] | 4905 | } |
| 4906 | #endif |
drh | f20bbc5 | 2019-01-17 01:06:00 +0000 | [diff] [blame] | 4907 | }else if( pA->op==TK_NULL ){ |
| 4908 | return 0; |
drh | d5af542 | 2018-04-13 14:27:01 +0000 | [diff] [blame] | 4909 | }else if( pA->op==TK_COLLATE ){ |
drh | e79f629 | 2018-04-18 17:52:28 +0000 | [diff] [blame] | 4910 | if( sqlite3_stricmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2; |
drh | f20bbc5 | 2019-01-17 01:06:00 +0000 | [diff] [blame] | 4911 | }else if( ALWAYS(pB->u.zToken!=0) && strcmp(pA->u.zToken,pB->u.zToken)!=0 ){ |
drh | d5af542 | 2018-04-13 14:27:01 +0000 | [diff] [blame] | 4912 | return 2; |
drh | 2646da7 | 2005-12-09 20:02:05 +0000 | [diff] [blame] | 4913 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 4914 | } |
drh | 10d1edf | 2013-11-15 15:52:39 +0000 | [diff] [blame] | 4915 | if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 2; |
drh | 89b6de0 | 2018-12-12 20:11:23 +0000 | [diff] [blame] | 4916 | if( (combinedFlags & EP_TokenOnly)==0 ){ |
drh | 10d1edf | 2013-11-15 15:52:39 +0000 | [diff] [blame] | 4917 | if( combinedFlags & EP_xIsSelect ) return 2; |
drh | efad2e2 | 2018-07-27 16:57:11 +0000 | [diff] [blame] | 4918 | if( (combinedFlags & EP_FixedCol)==0 |
| 4919 | && sqlite3ExprCompare(pParse, pA->pLeft, pB->pLeft, iTab) ) return 2; |
dan | 5aa550c | 2017-06-24 18:10:29 +0000 | [diff] [blame] | 4920 | if( sqlite3ExprCompare(pParse, pA->pRight, pB->pRight, iTab) ) return 2; |
drh | 10d1edf | 2013-11-15 15:52:39 +0000 | [diff] [blame] | 4921 | if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList, iTab) ) return 2; |
drh | 03c5c21 | 2018-12-12 11:23:40 +0000 | [diff] [blame] | 4922 | if( pA->op!=TK_STRING |
| 4923 | && pA->op!=TK_TRUEFALSE |
| 4924 | && (combinedFlags & EP_Reduced)==0 |
| 4925 | ){ |
drh | 10d1edf | 2013-11-15 15:52:39 +0000 | [diff] [blame] | 4926 | if( pA->iColumn!=pB->iColumn ) return 2; |
dan | 8ac02a9 | 2019-05-20 10:36:15 +0000 | [diff] [blame] | 4927 | if( pA->op2!=pB->op2 ) return 2; |
drh | 10d1edf | 2013-11-15 15:52:39 +0000 | [diff] [blame] | 4928 | if( pA->iTable!=pB->iTable |
drh | 85f8aa7 | 2013-11-15 20:06:26 +0000 | [diff] [blame] | 4929 | && (pA->iTable!=iTab || NEVER(pB->iTable>=0)) ) return 2; |
drh | 10d1edf | 2013-11-15 15:52:39 +0000 | [diff] [blame] | 4930 | } |
| 4931 | } |
drh | 1d9da70 | 2010-01-07 15:17:02 +0000 | [diff] [blame] | 4932 | return 0; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 4933 | } |
| 4934 | |
drh | 8c6f666 | 2010-04-26 19:17:26 +0000 | [diff] [blame] | 4935 | /* |
| 4936 | ** Compare two ExprList objects. Return 0 if they are identical and |
| 4937 | ** non-zero if they differ in any way. |
| 4938 | ** |
drh | 619a130 | 2013-08-01 13:04:46 +0000 | [diff] [blame] | 4939 | ** If any subelement of pB has Expr.iTable==(-1) then it is allowed |
| 4940 | ** to compare equal to an equivalent element in pA with Expr.iTable==iTab. |
| 4941 | ** |
drh | 8c6f666 | 2010-04-26 19:17:26 +0000 | [diff] [blame] | 4942 | ** This routine might return non-zero for equivalent ExprLists. The |
| 4943 | ** only consequence will be disabled optimizations. But this routine |
| 4944 | ** must never return 0 if the two ExprList objects are different, or |
| 4945 | ** a malfunction will result. |
| 4946 | ** |
| 4947 | ** Two NULL pointers are considered to be the same. But a NULL pointer |
| 4948 | ** always differs from a non-NULL pointer. |
| 4949 | */ |
drh | 619a130 | 2013-08-01 13:04:46 +0000 | [diff] [blame] | 4950 | int sqlite3ExprListCompare(ExprList *pA, ExprList *pB, int iTab){ |
drh | 8c6f666 | 2010-04-26 19:17:26 +0000 | [diff] [blame] | 4951 | int i; |
| 4952 | if( pA==0 && pB==0 ) return 0; |
| 4953 | if( pA==0 || pB==0 ) return 1; |
| 4954 | if( pA->nExpr!=pB->nExpr ) return 1; |
| 4955 | for(i=0; i<pA->nExpr; i++){ |
| 4956 | Expr *pExprA = pA->a[i].pExpr; |
| 4957 | Expr *pExprB = pB->a[i].pExpr; |
dan | 6e11892 | 2019-08-12 16:36:38 +0000 | [diff] [blame] | 4958 | if( pA->a[i].sortFlags!=pB->a[i].sortFlags ) return 1; |
dan | 5aa550c | 2017-06-24 18:10:29 +0000 | [diff] [blame] | 4959 | if( sqlite3ExprCompare(0, pExprA, pExprB, iTab) ) return 1; |
drh | 8c6f666 | 2010-04-26 19:17:26 +0000 | [diff] [blame] | 4960 | } |
| 4961 | return 0; |
| 4962 | } |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 4963 | |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 4964 | /* |
drh | f9463df | 2017-02-11 14:59:58 +0000 | [diff] [blame] | 4965 | ** Like sqlite3ExprCompare() except COLLATE operators at the top-level |
| 4966 | ** are ignored. |
| 4967 | */ |
| 4968 | int sqlite3ExprCompareSkip(Expr *pA, Expr *pB, int iTab){ |
dan | 5aa550c | 2017-06-24 18:10:29 +0000 | [diff] [blame] | 4969 | return sqlite3ExprCompare(0, |
drh | 0d950af | 2019-08-22 16:38:42 +0000 | [diff] [blame] | 4970 | sqlite3ExprSkipCollateAndLikely(pA), |
| 4971 | sqlite3ExprSkipCollateAndLikely(pB), |
drh | f9463df | 2017-02-11 14:59:58 +0000 | [diff] [blame] | 4972 | iTab); |
| 4973 | } |
| 4974 | |
| 4975 | /* |
drh | c51cf86 | 2019-05-11 19:36:03 +0000 | [diff] [blame] | 4976 | ** Return non-zero if Expr p can only be true if pNN is not NULL. |
| 4977 | */ |
| 4978 | static int exprImpliesNotNull( |
| 4979 | Parse *pParse, /* Parsing context */ |
| 4980 | Expr *p, /* The expression to be checked */ |
| 4981 | Expr *pNN, /* The expression that is NOT NULL */ |
| 4982 | int iTab, /* Table being evaluated */ |
| 4983 | int seenNot /* True if p is an operand of NOT */ |
| 4984 | ){ |
| 4985 | assert( p ); |
| 4986 | assert( pNN ); |
drh | 14c865e | 2019-08-10 15:06:03 +0000 | [diff] [blame] | 4987 | if( sqlite3ExprCompare(pParse, p, pNN, iTab)==0 ){ |
| 4988 | return pNN->op!=TK_NULL; |
| 4989 | } |
drh | c51cf86 | 2019-05-11 19:36:03 +0000 | [diff] [blame] | 4990 | switch( p->op ){ |
| 4991 | case TK_IN: { |
| 4992 | if( seenNot && ExprHasProperty(p, EP_xIsSelect) ) return 0; |
| 4993 | assert( ExprHasProperty(p,EP_xIsSelect) |
| 4994 | || (p->x.pList!=0 && p->x.pList->nExpr>0) ); |
| 4995 | return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, seenNot); |
| 4996 | } |
| 4997 | case TK_BETWEEN: { |
| 4998 | ExprList *pList = p->x.pList; |
| 4999 | assert( pList!=0 ); |
| 5000 | assert( pList->nExpr==2 ); |
| 5001 | if( seenNot ) return 0; |
| 5002 | if( exprImpliesNotNull(pParse, pList->a[0].pExpr, pNN, iTab, seenNot) |
| 5003 | || exprImpliesNotNull(pParse, pList->a[1].pExpr, pNN, iTab, seenNot) |
| 5004 | ){ |
| 5005 | return 1; |
| 5006 | } |
| 5007 | return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, seenNot); |
| 5008 | } |
| 5009 | case TK_EQ: |
| 5010 | case TK_NE: |
| 5011 | case TK_LT: |
| 5012 | case TK_LE: |
| 5013 | case TK_GT: |
| 5014 | case TK_GE: |
| 5015 | case TK_PLUS: |
| 5016 | case TK_MINUS: |
| 5017 | case TK_STAR: |
| 5018 | case TK_REM: |
| 5019 | case TK_BITAND: |
| 5020 | case TK_BITOR: |
| 5021 | case TK_SLASH: |
| 5022 | case TK_LSHIFT: |
| 5023 | case TK_RSHIFT: |
| 5024 | case TK_CONCAT: { |
| 5025 | if( exprImpliesNotNull(pParse, p->pRight, pNN, iTab, seenNot) ) return 1; |
| 5026 | /* Fall thru into the next case */ |
| 5027 | } |
| 5028 | case TK_SPAN: |
| 5029 | case TK_COLLATE: |
drh | c51cf86 | 2019-05-11 19:36:03 +0000 | [diff] [blame] | 5030 | case TK_UPLUS: |
| 5031 | case TK_UMINUS: { |
| 5032 | return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, seenNot); |
| 5033 | } |
| 5034 | case TK_TRUTH: { |
| 5035 | if( seenNot ) return 0; |
| 5036 | if( p->op2!=TK_IS ) return 0; |
| 5037 | return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, seenNot); |
| 5038 | } |
dan | 1cd382e | 2019-08-29 15:06:35 +0000 | [diff] [blame] | 5039 | case TK_BITNOT: |
drh | c51cf86 | 2019-05-11 19:36:03 +0000 | [diff] [blame] | 5040 | case TK_NOT: { |
| 5041 | return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1); |
| 5042 | } |
| 5043 | } |
| 5044 | return 0; |
| 5045 | } |
| 5046 | |
| 5047 | /* |
drh | 4bd5f73 | 2013-07-31 23:22:39 +0000 | [diff] [blame] | 5048 | ** Return true if we can prove the pE2 will always be true if pE1 is |
| 5049 | ** true. Return false if we cannot complete the proof or if pE2 might |
| 5050 | ** be false. Examples: |
| 5051 | ** |
drh | 619a130 | 2013-08-01 13:04:46 +0000 | [diff] [blame] | 5052 | ** pE1: x==5 pE2: x==5 Result: true |
| 5053 | ** pE1: x>0 pE2: x==5 Result: false |
| 5054 | ** pE1: x=21 pE2: x=21 OR y=43 Result: true |
| 5055 | ** pE1: x!=123 pE2: x IS NOT NULL Result: true |
| 5056 | ** pE1: x!=?1 pE2: x IS NOT NULL Result: true |
| 5057 | ** pE1: x IS NULL pE2: x IS NOT NULL Result: false |
| 5058 | ** pE1: x IS ?2 pE2: x IS NOT NULL Reuslt: false |
drh | 4bd5f73 | 2013-07-31 23:22:39 +0000 | [diff] [blame] | 5059 | ** |
| 5060 | ** When comparing TK_COLUMN nodes between pE1 and pE2, if pE2 has |
| 5061 | ** Expr.iTable<0 then assume a table number given by iTab. |
| 5062 | ** |
drh | c080422 | 2017-06-28 21:47:16 +0000 | [diff] [blame] | 5063 | ** If pParse is not NULL, then the values of bound variables in pE1 are |
| 5064 | ** compared against literal values in pE2 and pParse->pVdbe->expmask is |
| 5065 | ** modified to record which bound variables are referenced. If pParse |
| 5066 | ** is NULL, then false will be returned if pE1 contains any bound variables. |
| 5067 | ** |
drh | 4bd5f73 | 2013-07-31 23:22:39 +0000 | [diff] [blame] | 5068 | ** When in doubt, return false. Returning true might give a performance |
| 5069 | ** improvement. Returning false might cause a performance reduction, but |
| 5070 | ** it will always give the correct answer and is hence always safe. |
| 5071 | */ |
dan | 5aa550c | 2017-06-24 18:10:29 +0000 | [diff] [blame] | 5072 | int sqlite3ExprImpliesExpr(Parse *pParse, Expr *pE1, Expr *pE2, int iTab){ |
| 5073 | if( sqlite3ExprCompare(pParse, pE1, pE2, iTab)==0 ){ |
drh | 619a130 | 2013-08-01 13:04:46 +0000 | [diff] [blame] | 5074 | return 1; |
| 5075 | } |
| 5076 | if( pE2->op==TK_OR |
dan | 5aa550c | 2017-06-24 18:10:29 +0000 | [diff] [blame] | 5077 | && (sqlite3ExprImpliesExpr(pParse, pE1, pE2->pLeft, iTab) |
| 5078 | || sqlite3ExprImpliesExpr(pParse, pE1, pE2->pRight, iTab) ) |
drh | 619a130 | 2013-08-01 13:04:46 +0000 | [diff] [blame] | 5079 | ){ |
| 5080 | return 1; |
| 5081 | } |
drh | 664d6d1 | 2019-05-04 17:32:07 +0000 | [diff] [blame] | 5082 | if( pE2->op==TK_NOTNULL |
drh | c51cf86 | 2019-05-11 19:36:03 +0000 | [diff] [blame] | 5083 | && exprImpliesNotNull(pParse, pE1, pE2->pLeft, iTab, 0) |
drh | 664d6d1 | 2019-05-04 17:32:07 +0000 | [diff] [blame] | 5084 | ){ |
drh | c51cf86 | 2019-05-11 19:36:03 +0000 | [diff] [blame] | 5085 | return 1; |
drh | 619a130 | 2013-08-01 13:04:46 +0000 | [diff] [blame] | 5086 | } |
| 5087 | return 0; |
drh | 4bd5f73 | 2013-07-31 23:22:39 +0000 | [diff] [blame] | 5088 | } |
| 5089 | |
| 5090 | /* |
drh | 2589787 | 2018-03-20 21:16:15 +0000 | [diff] [blame] | 5091 | ** This is the Expr node callback for sqlite3ExprImpliesNotNullRow(). |
| 5092 | ** If the expression node requires that the table at pWalker->iCur |
drh | f8937f9 | 2018-09-23 02:01:42 +0000 | [diff] [blame] | 5093 | ** have one or more non-NULL column, then set pWalker->eCode to 1 and abort. |
| 5094 | ** |
| 5095 | ** This routine controls an optimization. False positives (setting |
| 5096 | ** pWalker->eCode to 1 when it should not be) are deadly, but false-negatives |
| 5097 | ** (never setting pWalker->eCode) is a harmless missed optimization. |
drh | 2589787 | 2018-03-20 21:16:15 +0000 | [diff] [blame] | 5098 | */ |
| 5099 | static int impliesNotNullRow(Walker *pWalker, Expr *pExpr){ |
drh | f8937f9 | 2018-09-23 02:01:42 +0000 | [diff] [blame] | 5100 | testcase( pExpr->op==TK_AGG_COLUMN ); |
drh | 821b610 | 2018-03-24 18:01:51 +0000 | [diff] [blame] | 5101 | testcase( pExpr->op==TK_AGG_FUNCTION ); |
drh | 2589787 | 2018-03-20 21:16:15 +0000 | [diff] [blame] | 5102 | if( ExprHasProperty(pExpr, EP_FromJoin) ) return WRC_Prune; |
| 5103 | switch( pExpr->op ){ |
dan | 0493222 | 2018-04-10 15:31:56 +0000 | [diff] [blame] | 5104 | case TK_ISNOT: |
dan | a1054dc | 2018-04-10 12:10:01 +0000 | [diff] [blame] | 5105 | case TK_NOT: |
dan | b6a9121 | 2019-08-29 15:50:16 +0000 | [diff] [blame^] | 5106 | case TK_BITNOT: |
drh | 2589787 | 2018-03-20 21:16:15 +0000 | [diff] [blame] | 5107 | case TK_ISNULL: |
drh | d579367 | 2019-02-05 14:36:33 +0000 | [diff] [blame] | 5108 | case TK_NOTNULL: |
drh | 2589787 | 2018-03-20 21:16:15 +0000 | [diff] [blame] | 5109 | case TK_IS: |
| 5110 | case TK_OR: |
drh | 2c49206 | 2018-03-24 13:24:02 +0000 | [diff] [blame] | 5111 | case TK_CASE: |
drh | e3eff26 | 2018-03-24 15:47:31 +0000 | [diff] [blame] | 5112 | case TK_IN: |
drh | 2589787 | 2018-03-20 21:16:15 +0000 | [diff] [blame] | 5113 | case TK_FUNCTION: |
dan | 0493222 | 2018-04-10 15:31:56 +0000 | [diff] [blame] | 5114 | testcase( pExpr->op==TK_ISNOT ); |
| 5115 | testcase( pExpr->op==TK_NOT ); |
drh | 821b610 | 2018-03-24 18:01:51 +0000 | [diff] [blame] | 5116 | testcase( pExpr->op==TK_ISNULL ); |
drh | d579367 | 2019-02-05 14:36:33 +0000 | [diff] [blame] | 5117 | testcase( pExpr->op==TK_NOTNULL ); |
drh | 821b610 | 2018-03-24 18:01:51 +0000 | [diff] [blame] | 5118 | testcase( pExpr->op==TK_IS ); |
| 5119 | testcase( pExpr->op==TK_OR ); |
| 5120 | testcase( pExpr->op==TK_CASE ); |
| 5121 | testcase( pExpr->op==TK_IN ); |
| 5122 | testcase( pExpr->op==TK_FUNCTION ); |
drh | 2589787 | 2018-03-20 21:16:15 +0000 | [diff] [blame] | 5123 | return WRC_Prune; |
| 5124 | case TK_COLUMN: |
drh | 2589787 | 2018-03-20 21:16:15 +0000 | [diff] [blame] | 5125 | if( pWalker->u.iCur==pExpr->iTable ){ |
| 5126 | pWalker->eCode = 1; |
| 5127 | return WRC_Abort; |
| 5128 | } |
| 5129 | return WRC_Prune; |
drh | 9881155 | 2018-04-03 14:04:48 +0000 | [diff] [blame] | 5130 | |
| 5131 | /* Virtual tables are allowed to use constraints like x=NULL. So |
| 5132 | ** a term of the form x=y does not prove that y is not null if x |
| 5133 | ** is the column of a virtual table */ |
| 5134 | case TK_EQ: |
| 5135 | case TK_NE: |
| 5136 | case TK_LT: |
| 5137 | case TK_LE: |
| 5138 | case TK_GT: |
| 5139 | case TK_GE: |
| 5140 | testcase( pExpr->op==TK_EQ ); |
| 5141 | testcase( pExpr->op==TK_NE ); |
| 5142 | testcase( pExpr->op==TK_LT ); |
| 5143 | testcase( pExpr->op==TK_LE ); |
| 5144 | testcase( pExpr->op==TK_GT ); |
| 5145 | testcase( pExpr->op==TK_GE ); |
drh | eda079c | 2018-09-20 19:02:15 +0000 | [diff] [blame] | 5146 | if( (pExpr->pLeft->op==TK_COLUMN && IsVirtual(pExpr->pLeft->y.pTab)) |
| 5147 | || (pExpr->pRight->op==TK_COLUMN && IsVirtual(pExpr->pRight->y.pTab)) |
drh | 9881155 | 2018-04-03 14:04:48 +0000 | [diff] [blame] | 5148 | ){ |
| 5149 | return WRC_Prune; |
| 5150 | } |
drh | 2589787 | 2018-03-20 21:16:15 +0000 | [diff] [blame] | 5151 | default: |
| 5152 | return WRC_Continue; |
| 5153 | } |
| 5154 | } |
| 5155 | |
| 5156 | /* |
| 5157 | ** Return true (non-zero) if expression p can only be true if at least |
| 5158 | ** one column of table iTab is non-null. In other words, return true |
| 5159 | ** if expression p will always be NULL or false if every column of iTab |
| 5160 | ** is NULL. |
| 5161 | ** |
drh | 821b610 | 2018-03-24 18:01:51 +0000 | [diff] [blame] | 5162 | ** False negatives are acceptable. In other words, it is ok to return |
| 5163 | ** zero even if expression p will never be true of every column of iTab |
| 5164 | ** is NULL. A false negative is merely a missed optimization opportunity. |
| 5165 | ** |
| 5166 | ** False positives are not allowed, however. A false positive may result |
| 5167 | ** in an incorrect answer. |
| 5168 | ** |
drh | 2589787 | 2018-03-20 21:16:15 +0000 | [diff] [blame] | 5169 | ** Terms of p that are marked with EP_FromJoin (and hence that come from |
| 5170 | ** the ON or USING clauses of LEFT JOINS) are excluded from the analysis. |
| 5171 | ** |
| 5172 | ** This routine is used to check if a LEFT JOIN can be converted into |
| 5173 | ** an ordinary JOIN. The p argument is the WHERE clause. If the WHERE |
| 5174 | ** clause requires that some column of the right table of the LEFT JOIN |
| 5175 | ** be non-NULL, then the LEFT JOIN can be safely converted into an |
| 5176 | ** ordinary join. |
| 5177 | */ |
| 5178 | int sqlite3ExprImpliesNonNullRow(Expr *p, int iTab){ |
| 5179 | Walker w; |
drh | 0d950af | 2019-08-22 16:38:42 +0000 | [diff] [blame] | 5180 | p = sqlite3ExprSkipCollateAndLikely(p); |
drh | d6db659 | 2019-04-01 19:42:42 +0000 | [diff] [blame] | 5181 | while( p ){ |
| 5182 | if( p->op==TK_NOTNULL ){ |
| 5183 | p = p->pLeft; |
| 5184 | }else if( p->op==TK_AND ){ |
| 5185 | if( sqlite3ExprImpliesNonNullRow(p->pLeft, iTab) ) return 1; |
| 5186 | p = p->pRight; |
| 5187 | }else{ |
| 5188 | break; |
| 5189 | } |
| 5190 | } |
drh | 2589787 | 2018-03-20 21:16:15 +0000 | [diff] [blame] | 5191 | w.xExprCallback = impliesNotNullRow; |
| 5192 | w.xSelectCallback = 0; |
| 5193 | w.xSelectCallback2 = 0; |
| 5194 | w.eCode = 0; |
| 5195 | w.u.iCur = iTab; |
| 5196 | sqlite3WalkExpr(&w, p); |
| 5197 | return w.eCode; |
| 5198 | } |
| 5199 | |
| 5200 | /* |
drh | 030796d | 2012-08-23 16:18:10 +0000 | [diff] [blame] | 5201 | ** An instance of the following structure is used by the tree walker |
drh | 2409f8a | 2016-07-27 18:27:02 +0000 | [diff] [blame] | 5202 | ** to determine if an expression can be evaluated by reference to the |
| 5203 | ** index only, without having to do a search for the corresponding |
| 5204 | ** table entry. The IdxCover.pIdx field is the index. IdxCover.iCur |
| 5205 | ** is the cursor for the table. |
| 5206 | */ |
| 5207 | struct IdxCover { |
| 5208 | Index *pIdx; /* The index to be tested for coverage */ |
| 5209 | int iCur; /* Cursor number for the table corresponding to the index */ |
| 5210 | }; |
| 5211 | |
| 5212 | /* |
| 5213 | ** Check to see if there are references to columns in table |
| 5214 | ** pWalker->u.pIdxCover->iCur can be satisfied using the index |
| 5215 | ** pWalker->u.pIdxCover->pIdx. |
| 5216 | */ |
| 5217 | static int exprIdxCover(Walker *pWalker, Expr *pExpr){ |
| 5218 | if( pExpr->op==TK_COLUMN |
| 5219 | && pExpr->iTable==pWalker->u.pIdxCover->iCur |
| 5220 | && sqlite3ColumnOfIndex(pWalker->u.pIdxCover->pIdx, pExpr->iColumn)<0 |
| 5221 | ){ |
| 5222 | pWalker->eCode = 1; |
| 5223 | return WRC_Abort; |
| 5224 | } |
| 5225 | return WRC_Continue; |
| 5226 | } |
| 5227 | |
| 5228 | /* |
drh | e604ec0 | 2016-07-27 19:20:58 +0000 | [diff] [blame] | 5229 | ** Determine if an index pIdx on table with cursor iCur contains will |
| 5230 | ** the expression pExpr. Return true if the index does cover the |
| 5231 | ** expression and false if the pExpr expression references table columns |
| 5232 | ** that are not found in the index pIdx. |
drh | 2409f8a | 2016-07-27 18:27:02 +0000 | [diff] [blame] | 5233 | ** |
| 5234 | ** An index covering an expression means that the expression can be |
| 5235 | ** evaluated using only the index and without having to lookup the |
| 5236 | ** corresponding table entry. |
| 5237 | */ |
| 5238 | int sqlite3ExprCoveredByIndex( |
| 5239 | Expr *pExpr, /* The index to be tested */ |
| 5240 | int iCur, /* The cursor number for the corresponding table */ |
| 5241 | Index *pIdx /* The index that might be used for coverage */ |
| 5242 | ){ |
| 5243 | Walker w; |
| 5244 | struct IdxCover xcov; |
| 5245 | memset(&w, 0, sizeof(w)); |
| 5246 | xcov.iCur = iCur; |
| 5247 | xcov.pIdx = pIdx; |
| 5248 | w.xExprCallback = exprIdxCover; |
| 5249 | w.u.pIdxCover = &xcov; |
| 5250 | sqlite3WalkExpr(&w, pExpr); |
| 5251 | return !w.eCode; |
| 5252 | } |
| 5253 | |
| 5254 | |
| 5255 | /* |
| 5256 | ** An instance of the following structure is used by the tree walker |
drh | 030796d | 2012-08-23 16:18:10 +0000 | [diff] [blame] | 5257 | ** to count references to table columns in the arguments of an |
drh | ed551b9 | 2012-08-23 19:46:11 +0000 | [diff] [blame] | 5258 | ** aggregate function, in order to implement the |
| 5259 | ** sqlite3FunctionThisSrc() routine. |
drh | 374fdce | 2012-04-17 16:38:53 +0000 | [diff] [blame] | 5260 | */ |
drh | 030796d | 2012-08-23 16:18:10 +0000 | [diff] [blame] | 5261 | struct SrcCount { |
| 5262 | SrcList *pSrc; /* One particular FROM clause in a nested query */ |
| 5263 | int nThis; /* Number of references to columns in pSrcList */ |
| 5264 | int nOther; /* Number of references to columns in other FROM clauses */ |
| 5265 | }; |
| 5266 | |
| 5267 | /* |
| 5268 | ** Count the number of references to columns. |
| 5269 | */ |
| 5270 | static int exprSrcCount(Walker *pWalker, Expr *pExpr){ |
drh | fb0a608 | 2012-08-24 01:07:52 +0000 | [diff] [blame] | 5271 | /* The NEVER() on the second term is because sqlite3FunctionUsesThisSrc() |
| 5272 | ** is always called before sqlite3ExprAnalyzeAggregates() and so the |
| 5273 | ** TK_COLUMNs have not yet been converted into TK_AGG_COLUMN. If |
| 5274 | ** sqlite3FunctionUsesThisSrc() is used differently in the future, the |
| 5275 | ** NEVER() will need to be removed. */ |
| 5276 | if( pExpr->op==TK_COLUMN || NEVER(pExpr->op==TK_AGG_COLUMN) ){ |
drh | 374fdce | 2012-04-17 16:38:53 +0000 | [diff] [blame] | 5277 | int i; |
drh | 030796d | 2012-08-23 16:18:10 +0000 | [diff] [blame] | 5278 | struct SrcCount *p = pWalker->u.pSrcCount; |
| 5279 | SrcList *pSrc = p->pSrc; |
drh | 655814d | 2015-01-09 01:27:29 +0000 | [diff] [blame] | 5280 | int nSrc = pSrc ? pSrc->nSrc : 0; |
| 5281 | for(i=0; i<nSrc; i++){ |
drh | 030796d | 2012-08-23 16:18:10 +0000 | [diff] [blame] | 5282 | if( pExpr->iTable==pSrc->a[i].iCursor ) break; |
drh | 374fdce | 2012-04-17 16:38:53 +0000 | [diff] [blame] | 5283 | } |
drh | 655814d | 2015-01-09 01:27:29 +0000 | [diff] [blame] | 5284 | if( i<nSrc ){ |
drh | 030796d | 2012-08-23 16:18:10 +0000 | [diff] [blame] | 5285 | p->nThis++; |
| 5286 | }else{ |
| 5287 | p->nOther++; |
| 5288 | } |
drh | 374fdce | 2012-04-17 16:38:53 +0000 | [diff] [blame] | 5289 | } |
drh | 030796d | 2012-08-23 16:18:10 +0000 | [diff] [blame] | 5290 | return WRC_Continue; |
drh | 374fdce | 2012-04-17 16:38:53 +0000 | [diff] [blame] | 5291 | } |
| 5292 | |
| 5293 | /* |
drh | 030796d | 2012-08-23 16:18:10 +0000 | [diff] [blame] | 5294 | ** Determine if any of the arguments to the pExpr Function reference |
| 5295 | ** pSrcList. Return true if they do. Also return true if the function |
| 5296 | ** has no arguments or has only constant arguments. Return false if pExpr |
| 5297 | ** references columns but not columns of tables found in pSrcList. |
drh | 374fdce | 2012-04-17 16:38:53 +0000 | [diff] [blame] | 5298 | */ |
drh | 030796d | 2012-08-23 16:18:10 +0000 | [diff] [blame] | 5299 | int sqlite3FunctionUsesThisSrc(Expr *pExpr, SrcList *pSrcList){ |
drh | 374fdce | 2012-04-17 16:38:53 +0000 | [diff] [blame] | 5300 | Walker w; |
drh | 030796d | 2012-08-23 16:18:10 +0000 | [diff] [blame] | 5301 | struct SrcCount cnt; |
drh | 374fdce | 2012-04-17 16:38:53 +0000 | [diff] [blame] | 5302 | assert( pExpr->op==TK_AGG_FUNCTION ); |
drh | 030796d | 2012-08-23 16:18:10 +0000 | [diff] [blame] | 5303 | w.xExprCallback = exprSrcCount; |
drh | 979dd1b | 2017-05-29 14:26:07 +0000 | [diff] [blame] | 5304 | w.xSelectCallback = 0; |
drh | 030796d | 2012-08-23 16:18:10 +0000 | [diff] [blame] | 5305 | w.u.pSrcCount = &cnt; |
| 5306 | cnt.pSrc = pSrcList; |
| 5307 | cnt.nThis = 0; |
| 5308 | cnt.nOther = 0; |
| 5309 | sqlite3WalkExprList(&w, pExpr->x.pList); |
| 5310 | return cnt.nThis>0 || cnt.nOther==0; |
drh | 374fdce | 2012-04-17 16:38:53 +0000 | [diff] [blame] | 5311 | } |
| 5312 | |
| 5313 | /* |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 5314 | ** Add a new element to the pAggInfo->aCol[] array. Return the index of |
| 5315 | ** the new element. Return a negative number if malloc fails. |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 5316 | */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 5317 | static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){ |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 5318 | int i; |
drh | cf64372 | 2007-03-27 13:36:37 +0000 | [diff] [blame] | 5319 | pInfo->aCol = sqlite3ArrayAllocate( |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 5320 | db, |
drh | cf64372 | 2007-03-27 13:36:37 +0000 | [diff] [blame] | 5321 | pInfo->aCol, |
| 5322 | sizeof(pInfo->aCol[0]), |
drh | cf64372 | 2007-03-27 13:36:37 +0000 | [diff] [blame] | 5323 | &pInfo->nColumn, |
drh | cf64372 | 2007-03-27 13:36:37 +0000 | [diff] [blame] | 5324 | &i |
| 5325 | ); |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 5326 | return i; |
| 5327 | } |
| 5328 | |
| 5329 | /* |
| 5330 | ** Add a new element to the pAggInfo->aFunc[] array. Return the index of |
| 5331 | ** the new element. Return a negative number if malloc fails. |
| 5332 | */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 5333 | static int addAggInfoFunc(sqlite3 *db, AggInfo *pInfo){ |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 5334 | int i; |
drh | cf64372 | 2007-03-27 13:36:37 +0000 | [diff] [blame] | 5335 | pInfo->aFunc = sqlite3ArrayAllocate( |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 5336 | db, |
drh | cf64372 | 2007-03-27 13:36:37 +0000 | [diff] [blame] | 5337 | pInfo->aFunc, |
| 5338 | sizeof(pInfo->aFunc[0]), |
drh | cf64372 | 2007-03-27 13:36:37 +0000 | [diff] [blame] | 5339 | &pInfo->nFunc, |
drh | cf64372 | 2007-03-27 13:36:37 +0000 | [diff] [blame] | 5340 | &i |
| 5341 | ); |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 5342 | return i; |
| 5343 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 5344 | |
| 5345 | /* |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 5346 | ** This is the xExprCallback for a tree walker. It is used to |
| 5347 | ** implement sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 5348 | ** for additional information. |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 5349 | */ |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 5350 | static int analyzeAggregate(Walker *pWalker, Expr *pExpr){ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 5351 | int i; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 5352 | NameContext *pNC = pWalker->u.pNC; |
danielk1977 | a58fdfb | 2005-02-08 07:50:40 +0000 | [diff] [blame] | 5353 | Parse *pParse = pNC->pParse; |
| 5354 | SrcList *pSrcList = pNC->pSrcList; |
drh | 25c3b8c | 2018-04-16 10:34:13 +0000 | [diff] [blame] | 5355 | AggInfo *pAggInfo = pNC->uNC.pAggInfo; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 5356 | |
drh | 25c3b8c | 2018-04-16 10:34:13 +0000 | [diff] [blame] | 5357 | assert( pNC->ncFlags & NC_UAggInfo ); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 5358 | switch( pExpr->op ){ |
drh | 89c69d0 | 2007-01-04 01:20:28 +0000 | [diff] [blame] | 5359 | case TK_AGG_COLUMN: |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 5360 | case TK_COLUMN: { |
drh | 8b21389 | 2008-08-29 02:14:02 +0000 | [diff] [blame] | 5361 | testcase( pExpr->op==TK_AGG_COLUMN ); |
| 5362 | testcase( pExpr->op==TK_COLUMN ); |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 5363 | /* Check to see if the column is in one of the tables in the FROM |
| 5364 | ** clause of the aggregate query */ |
drh | 20bc393 | 2009-05-30 23:35:43 +0000 | [diff] [blame] | 5365 | if( ALWAYS(pSrcList!=0) ){ |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 5366 | struct SrcList_item *pItem = pSrcList->a; |
| 5367 | for(i=0; i<pSrcList->nSrc; i++, pItem++){ |
| 5368 | struct AggInfo_col *pCol; |
drh | c5cd124 | 2013-09-12 16:50:49 +0000 | [diff] [blame] | 5369 | assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) ); |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 5370 | if( pExpr->iTable==pItem->iCursor ){ |
| 5371 | /* If we reach this point, it means that pExpr refers to a table |
| 5372 | ** that is in the FROM clause of the aggregate query. |
| 5373 | ** |
| 5374 | ** Make an entry for the column in pAggInfo->aCol[] if there |
| 5375 | ** is not an entry there already. |
| 5376 | */ |
drh | 7f906d6 | 2007-03-12 23:48:52 +0000 | [diff] [blame] | 5377 | int k; |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 5378 | pCol = pAggInfo->aCol; |
drh | 7f906d6 | 2007-03-12 23:48:52 +0000 | [diff] [blame] | 5379 | for(k=0; k<pAggInfo->nColumn; k++, pCol++){ |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 5380 | if( pCol->iTable==pExpr->iTable && |
| 5381 | pCol->iColumn==pExpr->iColumn ){ |
| 5382 | break; |
| 5383 | } |
danielk1977 | a58fdfb | 2005-02-08 07:50:40 +0000 | [diff] [blame] | 5384 | } |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 5385 | if( (k>=pAggInfo->nColumn) |
| 5386 | && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0 |
| 5387 | ){ |
drh | 7f906d6 | 2007-03-12 23:48:52 +0000 | [diff] [blame] | 5388 | pCol = &pAggInfo->aCol[k]; |
drh | eda079c | 2018-09-20 19:02:15 +0000 | [diff] [blame] | 5389 | pCol->pTab = pExpr->y.pTab; |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 5390 | pCol->iTable = pExpr->iTable; |
| 5391 | pCol->iColumn = pExpr->iColumn; |
drh | 0a07c10 | 2008-01-03 18:03:08 +0000 | [diff] [blame] | 5392 | pCol->iMem = ++pParse->nMem; |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 5393 | pCol->iSorterColumn = -1; |
drh | 5774b80 | 2005-09-07 22:48:16 +0000 | [diff] [blame] | 5394 | pCol->pExpr = pExpr; |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 5395 | if( pAggInfo->pGroupBy ){ |
| 5396 | int j, n; |
| 5397 | ExprList *pGB = pAggInfo->pGroupBy; |
| 5398 | struct ExprList_item *pTerm = pGB->a; |
| 5399 | n = pGB->nExpr; |
| 5400 | for(j=0; j<n; j++, pTerm++){ |
| 5401 | Expr *pE = pTerm->pExpr; |
| 5402 | if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable && |
| 5403 | pE->iColumn==pExpr->iColumn ){ |
| 5404 | pCol->iSorterColumn = j; |
| 5405 | break; |
| 5406 | } |
| 5407 | } |
| 5408 | } |
| 5409 | if( pCol->iSorterColumn<0 ){ |
| 5410 | pCol->iSorterColumn = pAggInfo->nSortingColumn++; |
| 5411 | } |
| 5412 | } |
| 5413 | /* There is now an entry for pExpr in pAggInfo->aCol[] (either |
| 5414 | ** because it was there before or because we just created it). |
| 5415 | ** Convert the pExpr to be a TK_AGG_COLUMN referring to that |
| 5416 | ** pAggInfo->aCol[] entry. |
| 5417 | */ |
drh | ebb6a65 | 2013-09-12 23:42:22 +0000 | [diff] [blame] | 5418 | ExprSetVVAProperty(pExpr, EP_NoReduce); |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 5419 | pExpr->pAggInfo = pAggInfo; |
| 5420 | pExpr->op = TK_AGG_COLUMN; |
shane | cf69739 | 2009-06-01 16:53:09 +0000 | [diff] [blame] | 5421 | pExpr->iAgg = (i16)k; |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 5422 | break; |
| 5423 | } /* endif pExpr->iTable==pItem->iCursor */ |
| 5424 | } /* end loop over pSrcList */ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 5425 | } |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 5426 | return WRC_Prune; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 5427 | } |
| 5428 | case TK_AGG_FUNCTION: { |
drh | 3a8c4be | 2012-05-21 20:13:39 +0000 | [diff] [blame] | 5429 | if( (pNC->ncFlags & NC_InAggFunc)==0 |
drh | ed551b9 | 2012-08-23 19:46:11 +0000 | [diff] [blame] | 5430 | && pWalker->walkerDepth==pExpr->op2 |
drh | 3a8c4be | 2012-05-21 20:13:39 +0000 | [diff] [blame] | 5431 | ){ |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 5432 | /* Check to see if pExpr is a duplicate of another aggregate |
| 5433 | ** function that is already in the pAggInfo structure |
| 5434 | */ |
| 5435 | struct AggInfo_func *pItem = pAggInfo->aFunc; |
| 5436 | for(i=0; i<pAggInfo->nFunc; i++, pItem++){ |
dan | 5aa550c | 2017-06-24 18:10:29 +0000 | [diff] [blame] | 5437 | if( sqlite3ExprCompare(0, pItem->pExpr, pExpr, -1)==0 ){ |
danielk1977 | a58fdfb | 2005-02-08 07:50:40 +0000 | [diff] [blame] | 5438 | break; |
| 5439 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 5440 | } |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 5441 | if( i>=pAggInfo->nFunc ){ |
| 5442 | /* pExpr is original. Make a new entry in pAggInfo->aFunc[] |
| 5443 | */ |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 5444 | u8 enc = ENC(pParse->db); |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 5445 | i = addAggInfoFunc(pParse->db, pAggInfo); |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 5446 | if( i>=0 ){ |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 5447 | assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 5448 | pItem = &pAggInfo->aFunc[i]; |
| 5449 | pItem->pExpr = pExpr; |
drh | 0a07c10 | 2008-01-03 18:03:08 +0000 | [diff] [blame] | 5450 | pItem->iMem = ++pParse->nMem; |
drh | 33e619f | 2009-05-28 01:00:55 +0000 | [diff] [blame] | 5451 | assert( !ExprHasProperty(pExpr, EP_IntValue) ); |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 5452 | pItem->pFunc = sqlite3FindFunction(pParse->db, |
drh | 80738d9 | 2016-02-15 00:34:16 +0000 | [diff] [blame] | 5453 | pExpr->u.zToken, |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 5454 | pExpr->x.pList ? pExpr->x.pList->nExpr : 0, enc, 0); |
drh | fd35797 | 2005-09-09 01:33:19 +0000 | [diff] [blame] | 5455 | if( pExpr->flags & EP_Distinct ){ |
| 5456 | pItem->iDistinct = pParse->nTab++; |
| 5457 | }else{ |
| 5458 | pItem->iDistinct = -1; |
| 5459 | } |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 5460 | } |
danielk1977 | a58fdfb | 2005-02-08 07:50:40 +0000 | [diff] [blame] | 5461 | } |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 5462 | /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry |
| 5463 | */ |
drh | c5cd124 | 2013-09-12 16:50:49 +0000 | [diff] [blame] | 5464 | assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) ); |
drh | ebb6a65 | 2013-09-12 23:42:22 +0000 | [diff] [blame] | 5465 | ExprSetVVAProperty(pExpr, EP_NoReduce); |
shane | cf69739 | 2009-06-01 16:53:09 +0000 | [diff] [blame] | 5466 | pExpr->iAgg = (i16)i; |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 5467 | pExpr->pAggInfo = pAggInfo; |
drh | 6e83a57 | 2012-11-02 18:48:49 +0000 | [diff] [blame] | 5468 | return WRC_Prune; |
| 5469 | }else{ |
| 5470 | return WRC_Continue; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 5471 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 5472 | } |
| 5473 | } |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 5474 | return WRC_Continue; |
| 5475 | } |
| 5476 | static int analyzeAggregatesInSelect(Walker *pWalker, Select *pSelect){ |
drh | d5a336e | 2012-04-19 15:49:19 +0000 | [diff] [blame] | 5477 | UNUSED_PARAMETER(pSelect); |
drh | 979dd1b | 2017-05-29 14:26:07 +0000 | [diff] [blame] | 5478 | pWalker->walkerDepth++; |
drh | 374fdce | 2012-04-17 16:38:53 +0000 | [diff] [blame] | 5479 | return WRC_Continue; |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 5480 | } |
drh | 979dd1b | 2017-05-29 14:26:07 +0000 | [diff] [blame] | 5481 | static void analyzeAggregatesInSelectEnd(Walker *pWalker, Select *pSelect){ |
| 5482 | UNUSED_PARAMETER(pSelect); |
| 5483 | pWalker->walkerDepth--; |
| 5484 | } |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 5485 | |
| 5486 | /* |
drh | e8abb4c | 2012-11-02 18:24:57 +0000 | [diff] [blame] | 5487 | ** Analyze the pExpr expression looking for aggregate functions and |
| 5488 | ** for variables that need to be added to AggInfo object that pNC->pAggInfo |
| 5489 | ** points to. Additional entries are made on the AggInfo object as |
| 5490 | ** necessary. |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 5491 | ** |
| 5492 | ** This routine should only be called after the expression has been |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 5493 | ** analyzed by sqlite3ResolveExprNames(). |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 5494 | */ |
drh | d2b3e23 | 2008-01-23 14:51:49 +0000 | [diff] [blame] | 5495 | void sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){ |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 5496 | Walker w; |
| 5497 | w.xExprCallback = analyzeAggregate; |
| 5498 | w.xSelectCallback = analyzeAggregatesInSelect; |
drh | 979dd1b | 2017-05-29 14:26:07 +0000 | [diff] [blame] | 5499 | w.xSelectCallback2 = analyzeAggregatesInSelectEnd; |
| 5500 | w.walkerDepth = 0; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 5501 | w.u.pNC = pNC; |
dan | d999503 | 2019-01-23 16:59:24 +0000 | [diff] [blame] | 5502 | w.pParse = 0; |
drh | 20bc393 | 2009-05-30 23:35:43 +0000 | [diff] [blame] | 5503 | assert( pNC->pSrcList!=0 ); |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 5504 | sqlite3WalkExpr(&w, pExpr); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 5505 | } |
drh | 5d9a4af | 2005-08-30 00:54:01 +0000 | [diff] [blame] | 5506 | |
| 5507 | /* |
| 5508 | ** Call sqlite3ExprAnalyzeAggregates() for every expression in an |
| 5509 | ** expression list. Return the number of errors. |
| 5510 | ** |
| 5511 | ** If an error is found, the analysis is cut short. |
| 5512 | */ |
drh | d2b3e23 | 2008-01-23 14:51:49 +0000 | [diff] [blame] | 5513 | void sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){ |
drh | 5d9a4af | 2005-08-30 00:54:01 +0000 | [diff] [blame] | 5514 | struct ExprList_item *pItem; |
| 5515 | int i; |
drh | 5d9a4af | 2005-08-30 00:54:01 +0000 | [diff] [blame] | 5516 | if( pList ){ |
drh | d2b3e23 | 2008-01-23 14:51:49 +0000 | [diff] [blame] | 5517 | for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){ |
| 5518 | sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr); |
drh | 5d9a4af | 2005-08-30 00:54:01 +0000 | [diff] [blame] | 5519 | } |
| 5520 | } |
drh | 5d9a4af | 2005-08-30 00:54:01 +0000 | [diff] [blame] | 5521 | } |
drh | 892d317 | 2008-01-10 03:46:36 +0000 | [diff] [blame] | 5522 | |
| 5523 | /* |
drh | ceea332 | 2009-04-23 13:22:42 +0000 | [diff] [blame] | 5524 | ** Allocate a single new register for use to hold some intermediate result. |
drh | 892d317 | 2008-01-10 03:46:36 +0000 | [diff] [blame] | 5525 | */ |
| 5526 | int sqlite3GetTempReg(Parse *pParse){ |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 5527 | if( pParse->nTempReg==0 ){ |
drh | 892d317 | 2008-01-10 03:46:36 +0000 | [diff] [blame] | 5528 | return ++pParse->nMem; |
| 5529 | } |
danielk1977 | 2f425f6 | 2008-07-04 09:41:39 +0000 | [diff] [blame] | 5530 | return pParse->aTempReg[--pParse->nTempReg]; |
drh | 892d317 | 2008-01-10 03:46:36 +0000 | [diff] [blame] | 5531 | } |
drh | ceea332 | 2009-04-23 13:22:42 +0000 | [diff] [blame] | 5532 | |
| 5533 | /* |
| 5534 | ** Deallocate a register, making available for reuse for some other |
| 5535 | ** purpose. |
drh | ceea332 | 2009-04-23 13:22:42 +0000 | [diff] [blame] | 5536 | */ |
drh | 892d317 | 2008-01-10 03:46:36 +0000 | [diff] [blame] | 5537 | void sqlite3ReleaseTempReg(Parse *pParse, int iReg){ |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 5538 | if( iReg && pParse->nTempReg<ArraySize(pParse->aTempReg) ){ |
drh | 892d317 | 2008-01-10 03:46:36 +0000 | [diff] [blame] | 5539 | pParse->aTempReg[pParse->nTempReg++] = iReg; |
| 5540 | } |
| 5541 | } |
| 5542 | |
| 5543 | /* |
drh | ed24da4 | 2016-09-06 14:37:05 +0000 | [diff] [blame] | 5544 | ** Allocate or deallocate a block of nReg consecutive registers. |
drh | 892d317 | 2008-01-10 03:46:36 +0000 | [diff] [blame] | 5545 | */ |
| 5546 | int sqlite3GetTempRange(Parse *pParse, int nReg){ |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 5547 | int i, n; |
drh | ed24da4 | 2016-09-06 14:37:05 +0000 | [diff] [blame] | 5548 | if( nReg==1 ) return sqlite3GetTempReg(pParse); |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 5549 | i = pParse->iRangeReg; |
| 5550 | n = pParse->nRangeReg; |
drh | f49f352 | 2009-12-30 14:12:38 +0000 | [diff] [blame] | 5551 | if( nReg<=n ){ |
drh | 892d317 | 2008-01-10 03:46:36 +0000 | [diff] [blame] | 5552 | pParse->iRangeReg += nReg; |
| 5553 | pParse->nRangeReg -= nReg; |
| 5554 | }else{ |
| 5555 | i = pParse->nMem+1; |
| 5556 | pParse->nMem += nReg; |
| 5557 | } |
| 5558 | return i; |
| 5559 | } |
| 5560 | void sqlite3ReleaseTempRange(Parse *pParse, int iReg, int nReg){ |
drh | ed24da4 | 2016-09-06 14:37:05 +0000 | [diff] [blame] | 5561 | if( nReg==1 ){ |
| 5562 | sqlite3ReleaseTempReg(pParse, iReg); |
| 5563 | return; |
| 5564 | } |
drh | 892d317 | 2008-01-10 03:46:36 +0000 | [diff] [blame] | 5565 | if( nReg>pParse->nRangeReg ){ |
| 5566 | pParse->nRangeReg = nReg; |
| 5567 | pParse->iRangeReg = iReg; |
| 5568 | } |
| 5569 | } |
drh | cdc6955 | 2011-12-06 13:24:59 +0000 | [diff] [blame] | 5570 | |
| 5571 | /* |
| 5572 | ** Mark all temporary registers as being unavailable for reuse. |
| 5573 | */ |
| 5574 | void sqlite3ClearTempRegCache(Parse *pParse){ |
| 5575 | pParse->nTempReg = 0; |
| 5576 | pParse->nRangeReg = 0; |
| 5577 | } |
drh | bb9b5f2 | 2016-03-19 00:35:02 +0000 | [diff] [blame] | 5578 | |
| 5579 | /* |
| 5580 | ** Validate that no temporary register falls within the range of |
| 5581 | ** iFirst..iLast, inclusive. This routine is only call from within assert() |
| 5582 | ** statements. |
| 5583 | */ |
| 5584 | #ifdef SQLITE_DEBUG |
| 5585 | int sqlite3NoTempsInRange(Parse *pParse, int iFirst, int iLast){ |
| 5586 | int i; |
| 5587 | if( pParse->nRangeReg>0 |
drh | 3963e58 | 2017-07-15 20:33:19 +0000 | [diff] [blame] | 5588 | && pParse->iRangeReg+pParse->nRangeReg > iFirst |
| 5589 | && pParse->iRangeReg <= iLast |
drh | bb9b5f2 | 2016-03-19 00:35:02 +0000 | [diff] [blame] | 5590 | ){ |
| 5591 | return 0; |
| 5592 | } |
| 5593 | for(i=0; i<pParse->nTempReg; i++){ |
| 5594 | if( pParse->aTempReg[i]>=iFirst && pParse->aTempReg[i]<=iLast ){ |
| 5595 | return 0; |
| 5596 | } |
| 5597 | } |
| 5598 | return 1; |
| 5599 | } |
| 5600 | #endif /* SQLITE_DEBUG */ |