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 | ** |
drh | d176611 | 2008-09-17 00:13:12 +0000 | [diff] [blame] | 15 | ** $Id: expr.c,v 1.394 2008/09/17 00:13:12 drh Exp $ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 16 | */ |
| 17 | #include "sqliteInt.h" |
drh | 04738cb | 2002-06-02 18:19:00 +0000 | [diff] [blame] | 18 | #include <ctype.h> |
drh | a2e0004 | 2002-01-22 03:13:42 +0000 | [diff] [blame] | 19 | |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 20 | /* |
| 21 | ** Return the 'affinity' of the expression pExpr if any. |
| 22 | ** |
| 23 | ** If pExpr is a column, a reference to a column via an 'AS' alias, |
| 24 | ** or a sub-select with a column as the return value, then the |
| 25 | ** affinity of that column is returned. Otherwise, 0x00 is returned, |
| 26 | ** indicating no affinity for the expression. |
| 27 | ** |
| 28 | ** i.e. the WHERE clause expresssions in the following statements all |
| 29 | ** have an affinity: |
| 30 | ** |
| 31 | ** CREATE TABLE t1(a); |
| 32 | ** SELECT * FROM t1 WHERE a; |
| 33 | ** SELECT a AS b FROM t1 WHERE b; |
| 34 | ** SELECT * FROM t1 WHERE (select a from t1); |
| 35 | */ |
danielk1977 | bf3b721 | 2004-05-18 10:06:24 +0000 | [diff] [blame] | 36 | char sqlite3ExprAffinity(Expr *pExpr){ |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 37 | int op = pExpr->op; |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 38 | if( op==TK_SELECT ){ |
danielk1977 | bf3b721 | 2004-05-18 10:06:24 +0000 | [diff] [blame] | 39 | return sqlite3ExprAffinity(pExpr->pSelect->pEList->a[0].pExpr); |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 40 | } |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 41 | #ifndef SQLITE_OMIT_CAST |
| 42 | if( op==TK_CAST ){ |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 43 | return sqlite3AffinityType(&pExpr->token); |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 44 | } |
| 45 | #endif |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 46 | if( (op==TK_COLUMN || op==TK_REGISTER) && pExpr->pTab!=0 ){ |
| 47 | /* op==TK_REGISTER && pExpr->pTab!=0 happens when pExpr was originally |
| 48 | ** a TK_COLUMN but was previously evaluated and cached in a register */ |
| 49 | int j = pExpr->iColumn; |
| 50 | if( j<0 ) return SQLITE_AFF_INTEGER; |
| 51 | assert( pExpr->pTab && j<pExpr->pTab->nCol ); |
| 52 | return pExpr->pTab->aCol[j].affinity; |
| 53 | } |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 54 | return pExpr->affinity; |
| 55 | } |
| 56 | |
drh | 53db145 | 2004-05-20 13:54:53 +0000 | [diff] [blame] | 57 | /* |
drh | 8b4c40d | 2007-02-01 23:02:45 +0000 | [diff] [blame] | 58 | ** Set the collating sequence for expression pExpr to be the collating |
| 59 | ** sequence named by pToken. Return a pointer to the revised expression. |
drh | a34001c | 2007-02-02 12:44:37 +0000 | [diff] [blame] | 60 | ** The collating sequence is marked as "explicit" using the EP_ExpCollate |
| 61 | ** flag. An explicit collating sequence will override implicit |
| 62 | ** collating sequences. |
drh | 8b4c40d | 2007-02-01 23:02:45 +0000 | [diff] [blame] | 63 | */ |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 64 | Expr *sqlite3ExprSetColl(Parse *pParse, Expr *pExpr, Token *pCollName){ |
danielk1977 | 3900250 | 2007-11-12 09:50:26 +0000 | [diff] [blame] | 65 | char *zColl = 0; /* Dequoted name of collation sequence */ |
drh | 8b4c40d | 2007-02-01 23:02:45 +0000 | [diff] [blame] | 66 | CollSeq *pColl; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 67 | sqlite3 *db = pParse->db; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 68 | zColl = sqlite3NameFromToken(db, pCollName); |
danielk1977 | 3900250 | 2007-11-12 09:50:26 +0000 | [diff] [blame] | 69 | if( pExpr && zColl ){ |
| 70 | pColl = sqlite3LocateCollSeq(pParse, zColl, -1); |
| 71 | if( pColl ){ |
| 72 | pExpr->pColl = pColl; |
| 73 | pExpr->flags |= EP_ExpCollate; |
| 74 | } |
drh | 8b4c40d | 2007-02-01 23:02:45 +0000 | [diff] [blame] | 75 | } |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 76 | sqlite3DbFree(db, zColl); |
drh | 8b4c40d | 2007-02-01 23:02:45 +0000 | [diff] [blame] | 77 | return pExpr; |
| 78 | } |
| 79 | |
| 80 | /* |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 81 | ** Return the default collation sequence for the expression pExpr. If |
| 82 | ** there is no default collation type, return 0. |
| 83 | */ |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 84 | CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){ |
| 85 | CollSeq *pColl = 0; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 86 | Expr *p = pExpr; |
| 87 | while( p ){ |
drh | 7e09fe0 | 2007-06-20 16:13:23 +0000 | [diff] [blame] | 88 | int op; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 89 | pColl = p->pColl; |
| 90 | if( pColl ) break; |
| 91 | op = p->op; |
| 92 | if( (op==TK_COLUMN || op==TK_REGISTER) && p->pTab!=0 ){ |
| 93 | /* op==TK_REGISTER && p->pTab!=0 happens when pExpr was originally |
| 94 | ** a TK_COLUMN but was previously evaluated and cached in a register */ |
| 95 | const char *zColl; |
| 96 | int j = p->iColumn; |
| 97 | if( j>=0 ){ |
| 98 | sqlite3 *db = pParse->db; |
| 99 | zColl = p->pTab->aCol[j].zColl; |
| 100 | pColl = sqlite3FindCollSeq(db, ENC(db), zColl, -1, 0); |
| 101 | pExpr->pColl = pColl; |
| 102 | } |
| 103 | break; |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 104 | } |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 105 | if( op!=TK_CAST && op!=TK_UPLUS ){ |
| 106 | break; |
| 107 | } |
| 108 | p = p->pLeft; |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 109 | } |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 110 | if( sqlite3CheckCollSeq(pParse, pColl) ){ |
| 111 | pColl = 0; |
| 112 | } |
| 113 | return pColl; |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | /* |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 117 | ** pExpr is an operand of a comparison operator. aff2 is the |
| 118 | ** type affinity of the other operand. This routine returns the |
drh | 53db145 | 2004-05-20 13:54:53 +0000 | [diff] [blame] | 119 | ** type affinity that should be used for the comparison operator. |
| 120 | */ |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 121 | char sqlite3CompareAffinity(Expr *pExpr, char aff2){ |
danielk1977 | bf3b721 | 2004-05-18 10:06:24 +0000 | [diff] [blame] | 122 | char aff1 = sqlite3ExprAffinity(pExpr); |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 123 | if( aff1 && aff2 ){ |
drh | 8df447f | 2005-11-01 15:48:24 +0000 | [diff] [blame] | 124 | /* Both sides of the comparison are columns. If one has numeric |
| 125 | ** affinity, use that. Otherwise use no affinity. |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 126 | */ |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 127 | if( sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2) ){ |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 128 | return SQLITE_AFF_NUMERIC; |
| 129 | }else{ |
| 130 | return SQLITE_AFF_NONE; |
| 131 | } |
| 132 | }else if( !aff1 && !aff2 ){ |
drh | 5f6a87b | 2004-07-19 00:39:45 +0000 | [diff] [blame] | 133 | /* Neither side of the comparison is a column. Compare the |
| 134 | ** results directly. |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 135 | */ |
drh | 5f6a87b | 2004-07-19 00:39:45 +0000 | [diff] [blame] | 136 | return SQLITE_AFF_NONE; |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 137 | }else{ |
| 138 | /* One side is a column, the other is not. Use the columns affinity. */ |
drh | fe05af8 | 2005-07-21 03:14:59 +0000 | [diff] [blame] | 139 | assert( aff1==0 || aff2==0 ); |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 140 | return (aff1 + aff2); |
| 141 | } |
| 142 | } |
| 143 | |
drh | 53db145 | 2004-05-20 13:54:53 +0000 | [diff] [blame] | 144 | /* |
| 145 | ** pExpr is a comparison operator. Return the type affinity that should |
| 146 | ** be applied to both operands prior to doing the comparison. |
| 147 | */ |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 148 | static char comparisonAffinity(Expr *pExpr){ |
| 149 | char aff; |
| 150 | assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT || |
| 151 | pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE || |
| 152 | pExpr->op==TK_NE ); |
| 153 | assert( pExpr->pLeft ); |
danielk1977 | bf3b721 | 2004-05-18 10:06:24 +0000 | [diff] [blame] | 154 | aff = sqlite3ExprAffinity(pExpr->pLeft); |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 155 | if( pExpr->pRight ){ |
| 156 | aff = sqlite3CompareAffinity(pExpr->pRight, aff); |
| 157 | } |
| 158 | else if( pExpr->pSelect ){ |
| 159 | aff = sqlite3CompareAffinity(pExpr->pSelect->pEList->a[0].pExpr, aff); |
| 160 | } |
| 161 | else if( !aff ){ |
drh | de087bd | 2007-02-23 03:00:44 +0000 | [diff] [blame] | 162 | aff = SQLITE_AFF_NONE; |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 163 | } |
| 164 | return aff; |
| 165 | } |
| 166 | |
| 167 | /* |
| 168 | ** pExpr is a comparison expression, eg. '=', '<', IN(...) etc. |
| 169 | ** idx_affinity is the affinity of an indexed column. Return true |
| 170 | ** if the index with affinity idx_affinity may be used to implement |
| 171 | ** the comparison in pExpr. |
| 172 | */ |
| 173 | int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){ |
| 174 | char aff = comparisonAffinity(pExpr); |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 175 | switch( aff ){ |
| 176 | case SQLITE_AFF_NONE: |
| 177 | return 1; |
| 178 | case SQLITE_AFF_TEXT: |
| 179 | return idx_affinity==SQLITE_AFF_TEXT; |
| 180 | default: |
| 181 | return sqlite3IsNumericAffinity(idx_affinity); |
| 182 | } |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 183 | } |
| 184 | |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 185 | /* |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 186 | ** Return the P5 value that should be used for a binary comparison |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 187 | ** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2. |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 188 | */ |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 189 | static u8 binaryCompareP5(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){ |
| 190 | u8 aff = (char)sqlite3ExprAffinity(pExpr2); |
| 191 | aff = sqlite3CompareAffinity(pExpr1, aff) | jumpIfNull; |
| 192 | return aff; |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 193 | } |
| 194 | |
drh | a2e0004 | 2002-01-22 03:13:42 +0000 | [diff] [blame] | 195 | /* |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 196 | ** Return a pointer to the collation sequence that should be used by |
| 197 | ** a binary comparison operator comparing pLeft and pRight. |
| 198 | ** |
| 199 | ** If the left hand expression has a collating sequence type, then it is |
| 200 | ** used. Otherwise the collation sequence for the right hand expression |
| 201 | ** is used, or the default (BINARY) if neither expression has a collating |
| 202 | ** type. |
danielk1977 | bcbb04e | 2007-05-29 12:11:29 +0000 | [diff] [blame] | 203 | ** |
| 204 | ** Argument pRight (but not pLeft) may be a null pointer. In this case, |
| 205 | ** it is not considered. |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 206 | */ |
drh | 0a0e131 | 2007-08-07 17:04:59 +0000 | [diff] [blame] | 207 | CollSeq *sqlite3BinaryCompareCollSeq( |
danielk1977 | bcbb04e | 2007-05-29 12:11:29 +0000 | [diff] [blame] | 208 | Parse *pParse, |
| 209 | Expr *pLeft, |
| 210 | Expr *pRight |
| 211 | ){ |
drh | ec41dda | 2007-02-07 13:09:45 +0000 | [diff] [blame] | 212 | CollSeq *pColl; |
| 213 | assert( pLeft ); |
drh | ec41dda | 2007-02-07 13:09:45 +0000 | [diff] [blame] | 214 | if( pLeft->flags & EP_ExpCollate ){ |
| 215 | assert( pLeft->pColl ); |
| 216 | pColl = pLeft->pColl; |
danielk1977 | bcbb04e | 2007-05-29 12:11:29 +0000 | [diff] [blame] | 217 | }else if( pRight && pRight->flags & EP_ExpCollate ){ |
drh | ec41dda | 2007-02-07 13:09:45 +0000 | [diff] [blame] | 218 | assert( pRight->pColl ); |
| 219 | pColl = pRight->pColl; |
| 220 | }else{ |
| 221 | pColl = sqlite3ExprCollSeq(pParse, pLeft); |
| 222 | if( !pColl ){ |
| 223 | pColl = sqlite3ExprCollSeq(pParse, pRight); |
| 224 | } |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 225 | } |
| 226 | return pColl; |
| 227 | } |
| 228 | |
| 229 | /* |
drh | da250ea | 2008-04-01 05:07:14 +0000 | [diff] [blame] | 230 | ** Generate the operands for a comparison operation. Before |
| 231 | ** generating the code for each operand, set the EP_AnyAff |
| 232 | ** flag on the expression so that it will be able to used a |
| 233 | ** cached column value that has previously undergone an |
| 234 | ** affinity change. |
| 235 | */ |
| 236 | static void codeCompareOperands( |
| 237 | Parse *pParse, /* Parsing and code generating context */ |
| 238 | Expr *pLeft, /* The left operand */ |
| 239 | int *pRegLeft, /* Register where left operand is stored */ |
| 240 | int *pFreeLeft, /* Free this register when done */ |
| 241 | Expr *pRight, /* The right operand */ |
| 242 | int *pRegRight, /* Register where right operand is stored */ |
| 243 | int *pFreeRight /* Write temp register for right operand there */ |
| 244 | ){ |
| 245 | while( pLeft->op==TK_UPLUS ) pLeft = pLeft->pLeft; |
| 246 | pLeft->flags |= EP_AnyAff; |
| 247 | *pRegLeft = sqlite3ExprCodeTemp(pParse, pLeft, pFreeLeft); |
| 248 | while( pRight->op==TK_UPLUS ) pRight = pRight->pLeft; |
| 249 | pRight->flags |= EP_AnyAff; |
| 250 | *pRegRight = sqlite3ExprCodeTemp(pParse, pRight, pFreeRight); |
| 251 | } |
| 252 | |
| 253 | /* |
drh | be5c89a | 2004-07-26 00:31:09 +0000 | [diff] [blame] | 254 | ** Generate code for a comparison operator. |
| 255 | */ |
| 256 | static int codeCompare( |
| 257 | Parse *pParse, /* The parsing (and code generating) context */ |
| 258 | Expr *pLeft, /* The left operand */ |
| 259 | Expr *pRight, /* The right operand */ |
| 260 | int opcode, /* The comparison opcode */ |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 261 | int in1, int in2, /* Register holding operands */ |
drh | be5c89a | 2004-07-26 00:31:09 +0000 | [diff] [blame] | 262 | int dest, /* Jump here if true. */ |
| 263 | int jumpIfNull /* If true, jump if either operand is NULL */ |
| 264 | ){ |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 265 | int p5; |
| 266 | int addr; |
| 267 | CollSeq *p4; |
| 268 | |
| 269 | p4 = sqlite3BinaryCompareCollSeq(pParse, pLeft, pRight); |
| 270 | p5 = binaryCompareP5(pLeft, pRight, jumpIfNull); |
| 271 | addr = sqlite3VdbeAddOp4(pParse->pVdbe, opcode, in2, dest, in1, |
| 272 | (void*)p4, P4_COLLSEQ); |
| 273 | sqlite3VdbeChangeP5(pParse->pVdbe, p5); |
drh | e49b146 | 2008-07-09 01:39:44 +0000 | [diff] [blame] | 274 | if( (p5 & SQLITE_AFF_MASK)!=SQLITE_AFF_NONE ){ |
drh | da250ea | 2008-04-01 05:07:14 +0000 | [diff] [blame] | 275 | sqlite3ExprCacheAffinityChange(pParse, in1, 1); |
| 276 | sqlite3ExprCacheAffinityChange(pParse, in2, 1); |
drh | 2f7794c | 2008-04-01 03:27:39 +0000 | [diff] [blame] | 277 | } |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 278 | return addr; |
drh | be5c89a | 2004-07-26 00:31:09 +0000 | [diff] [blame] | 279 | } |
| 280 | |
danielk1977 | 4b5255a | 2008-06-05 16:47:39 +0000 | [diff] [blame] | 281 | #if SQLITE_MAX_EXPR_DEPTH>0 |
| 282 | /* |
| 283 | ** Check that argument nHeight is less than or equal to the maximum |
| 284 | ** expression depth allowed. If it is not, leave an error message in |
| 285 | ** pParse. |
| 286 | */ |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 287 | int sqlite3ExprCheckHeight(Parse *pParse, int nHeight){ |
danielk1977 | 4b5255a | 2008-06-05 16:47:39 +0000 | [diff] [blame] | 288 | int rc = SQLITE_OK; |
| 289 | int mxHeight = pParse->db->aLimit[SQLITE_LIMIT_EXPR_DEPTH]; |
| 290 | if( nHeight>mxHeight ){ |
| 291 | sqlite3ErrorMsg(pParse, |
| 292 | "Expression tree is too large (maximum depth %d)", mxHeight |
| 293 | ); |
| 294 | rc = SQLITE_ERROR; |
| 295 | } |
| 296 | return rc; |
| 297 | } |
| 298 | |
| 299 | /* The following three functions, heightOfExpr(), heightOfExprList() |
| 300 | ** and heightOfSelect(), are used to determine the maximum height |
| 301 | ** of any expression tree referenced by the structure passed as the |
| 302 | ** first argument. |
| 303 | ** |
| 304 | ** If this maximum height is greater than the current value pointed |
| 305 | ** to by pnHeight, the second parameter, then set *pnHeight to that |
| 306 | ** value. |
| 307 | */ |
| 308 | static void heightOfExpr(Expr *p, int *pnHeight){ |
| 309 | if( p ){ |
| 310 | if( p->nHeight>*pnHeight ){ |
| 311 | *pnHeight = p->nHeight; |
| 312 | } |
| 313 | } |
| 314 | } |
| 315 | static void heightOfExprList(ExprList *p, int *pnHeight){ |
| 316 | if( p ){ |
| 317 | int i; |
| 318 | for(i=0; i<p->nExpr; i++){ |
| 319 | heightOfExpr(p->a[i].pExpr, pnHeight); |
| 320 | } |
| 321 | } |
| 322 | } |
| 323 | static void heightOfSelect(Select *p, int *pnHeight){ |
| 324 | if( p ){ |
| 325 | heightOfExpr(p->pWhere, pnHeight); |
| 326 | heightOfExpr(p->pHaving, pnHeight); |
| 327 | heightOfExpr(p->pLimit, pnHeight); |
| 328 | heightOfExpr(p->pOffset, pnHeight); |
| 329 | heightOfExprList(p->pEList, pnHeight); |
| 330 | heightOfExprList(p->pGroupBy, pnHeight); |
| 331 | heightOfExprList(p->pOrderBy, pnHeight); |
| 332 | heightOfSelect(p->pPrior, pnHeight); |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | /* |
| 337 | ** Set the Expr.nHeight variable in the structure passed as an |
| 338 | ** argument. An expression with no children, Expr.pList or |
| 339 | ** Expr.pSelect member has a height of 1. Any other expression |
| 340 | ** has a height equal to the maximum height of any other |
| 341 | ** referenced Expr plus one. |
| 342 | */ |
| 343 | static void exprSetHeight(Expr *p){ |
| 344 | int nHeight = 0; |
| 345 | heightOfExpr(p->pLeft, &nHeight); |
| 346 | heightOfExpr(p->pRight, &nHeight); |
| 347 | heightOfExprList(p->pList, &nHeight); |
| 348 | heightOfSelect(p->pSelect, &nHeight); |
| 349 | p->nHeight = nHeight + 1; |
| 350 | } |
| 351 | |
| 352 | /* |
| 353 | ** Set the Expr.nHeight variable using the exprSetHeight() function. If |
| 354 | ** the height is greater than the maximum allowed expression depth, |
| 355 | ** leave an error in pParse. |
| 356 | */ |
| 357 | void sqlite3ExprSetHeight(Parse *pParse, Expr *p){ |
| 358 | exprSetHeight(p); |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 359 | sqlite3ExprCheckHeight(pParse, p->nHeight); |
danielk1977 | 4b5255a | 2008-06-05 16:47:39 +0000 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | /* |
| 363 | ** Return the maximum height of any expression tree referenced |
| 364 | ** by the select statement passed as an argument. |
| 365 | */ |
| 366 | int sqlite3SelectExprHeight(Select *p){ |
| 367 | int nHeight = 0; |
| 368 | heightOfSelect(p, &nHeight); |
| 369 | return nHeight; |
| 370 | } |
| 371 | #else |
danielk1977 | 4b5255a | 2008-06-05 16:47:39 +0000 | [diff] [blame] | 372 | #define exprSetHeight(y) |
| 373 | #endif /* SQLITE_MAX_EXPR_DEPTH>0 */ |
| 374 | |
drh | be5c89a | 2004-07-26 00:31:09 +0000 | [diff] [blame] | 375 | /* |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 376 | ** Construct a new expression node and return a pointer to it. Memory |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 377 | ** for this node is obtained from sqlite3_malloc(). The calling function |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 378 | ** is responsible for making sure the node eventually gets freed. |
| 379 | */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 380 | Expr *sqlite3Expr( |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 381 | sqlite3 *db, /* Handle for sqlite3DbMallocZero() (may be null) */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 382 | int op, /* Expression opcode */ |
| 383 | Expr *pLeft, /* Left operand */ |
| 384 | Expr *pRight, /* Right operand */ |
| 385 | const Token *pToken /* Argument token */ |
| 386 | ){ |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 387 | Expr *pNew; |
drh | 26e4a8b | 2008-05-01 17:16:52 +0000 | [diff] [blame] | 388 | pNew = sqlite3DbMallocZero(db, sizeof(Expr)); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 389 | if( pNew==0 ){ |
danielk1977 | d5d5652 | 2005-03-16 12:15:20 +0000 | [diff] [blame] | 390 | /* When malloc fails, delete pLeft and pRight. Expressions passed to |
| 391 | ** this function must always be allocated with sqlite3Expr() for this |
| 392 | ** reason. |
| 393 | */ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 394 | sqlite3ExprDelete(db, pLeft); |
| 395 | sqlite3ExprDelete(db, pRight); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 396 | return 0; |
| 397 | } |
| 398 | pNew->op = op; |
| 399 | pNew->pLeft = pLeft; |
| 400 | pNew->pRight = pRight; |
danielk1977 | a58fdfb | 2005-02-08 07:50:40 +0000 | [diff] [blame] | 401 | pNew->iAgg = -1; |
drh | e49b146 | 2008-07-09 01:39:44 +0000 | [diff] [blame] | 402 | pNew->span.z = (u8*)""; |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 403 | if( pToken ){ |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 404 | assert( pToken->dyn==0 ); |
drh | 145716b | 2004-09-24 12:24:06 +0000 | [diff] [blame] | 405 | pNew->span = pNew->token = *pToken; |
drh | a34001c | 2007-02-02 12:44:37 +0000 | [diff] [blame] | 406 | }else if( pLeft ){ |
| 407 | if( pRight ){ |
drh | e49b146 | 2008-07-09 01:39:44 +0000 | [diff] [blame] | 408 | if( pRight->span.dyn==0 && pLeft->span.dyn==0 ){ |
| 409 | sqlite3ExprSpan(pNew, &pLeft->span, &pRight->span); |
| 410 | } |
drh | 5ffb3ac | 2007-04-18 17:07:57 +0000 | [diff] [blame] | 411 | if( pRight->flags & EP_ExpCollate ){ |
drh | a34001c | 2007-02-02 12:44:37 +0000 | [diff] [blame] | 412 | pNew->flags |= EP_ExpCollate; |
| 413 | pNew->pColl = pRight->pColl; |
| 414 | } |
| 415 | } |
drh | 5ffb3ac | 2007-04-18 17:07:57 +0000 | [diff] [blame] | 416 | if( pLeft->flags & EP_ExpCollate ){ |
drh | a34001c | 2007-02-02 12:44:37 +0000 | [diff] [blame] | 417 | pNew->flags |= EP_ExpCollate; |
| 418 | pNew->pColl = pLeft->pColl; |
| 419 | } |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 420 | } |
danielk1977 | fc97606 | 2007-05-10 10:46:56 +0000 | [diff] [blame] | 421 | |
danielk1977 | 4b5255a | 2008-06-05 16:47:39 +0000 | [diff] [blame] | 422 | exprSetHeight(pNew); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 423 | return pNew; |
| 424 | } |
| 425 | |
| 426 | /* |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 427 | ** Works like sqlite3Expr() except that it takes an extra Parse* |
| 428 | ** argument and notifies the associated connection object if malloc fails. |
drh | 206f3d9 | 2006-07-11 13:15:08 +0000 | [diff] [blame] | 429 | */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 430 | Expr *sqlite3PExpr( |
| 431 | Parse *pParse, /* Parsing context */ |
| 432 | int op, /* Expression opcode */ |
| 433 | Expr *pLeft, /* Left operand */ |
| 434 | Expr *pRight, /* Right operand */ |
| 435 | const Token *pToken /* Argument token */ |
| 436 | ){ |
danielk1977 | 4b5255a | 2008-06-05 16:47:39 +0000 | [diff] [blame] | 437 | Expr *p = sqlite3Expr(pParse->db, op, pLeft, pRight, pToken); |
| 438 | if( p ){ |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 439 | sqlite3ExprCheckHeight(pParse, p->nHeight); |
danielk1977 | 4b5255a | 2008-06-05 16:47:39 +0000 | [diff] [blame] | 440 | } |
| 441 | return p; |
drh | 206f3d9 | 2006-07-11 13:15:08 +0000 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | /* |
drh | 4e0cff6 | 2004-11-05 05:10:28 +0000 | [diff] [blame] | 445 | ** When doing a nested parse, you can include terms in an expression |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 446 | ** that look like this: #1 #2 ... These terms refer to registers |
| 447 | ** in the virtual machine. #N is the N-th register. |
drh | 4e0cff6 | 2004-11-05 05:10:28 +0000 | [diff] [blame] | 448 | ** |
| 449 | ** This routine is called by the parser to deal with on of those terms. |
| 450 | ** It immediately generates code to store the value in a memory location. |
| 451 | ** The returns an expression that will code to extract the value from |
| 452 | ** that memory location as needed. |
| 453 | */ |
| 454 | Expr *sqlite3RegisterExpr(Parse *pParse, Token *pToken){ |
| 455 | Vdbe *v = pParse->pVdbe; |
| 456 | Expr *p; |
drh | 4e0cff6 | 2004-11-05 05:10:28 +0000 | [diff] [blame] | 457 | if( pParse->nested==0 ){ |
| 458 | sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", pToken); |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 459 | return sqlite3PExpr(pParse, TK_NULL, 0, 0, 0); |
drh | 4e0cff6 | 2004-11-05 05:10:28 +0000 | [diff] [blame] | 460 | } |
drh | bb7ac00 | 2005-08-12 22:58:53 +0000 | [diff] [blame] | 461 | if( v==0 ) return 0; |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 462 | p = sqlite3PExpr(pParse, TK_REGISTER, 0, 0, pToken); |
drh | 73c42a1 | 2004-11-20 18:13:10 +0000 | [diff] [blame] | 463 | if( p==0 ){ |
| 464 | return 0; /* Malloc failed */ |
| 465 | } |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 466 | p->iTable = atoi((char*)&pToken->z[1]); |
drh | 4e0cff6 | 2004-11-05 05:10:28 +0000 | [diff] [blame] | 467 | return p; |
| 468 | } |
| 469 | |
| 470 | /* |
drh | 91bb0ee | 2004-09-01 03:06:34 +0000 | [diff] [blame] | 471 | ** Join two expressions using an AND operator. If either expression is |
| 472 | ** NULL, then just return the other expression. |
| 473 | */ |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 474 | Expr *sqlite3ExprAnd(sqlite3 *db, Expr *pLeft, Expr *pRight){ |
drh | 91bb0ee | 2004-09-01 03:06:34 +0000 | [diff] [blame] | 475 | if( pLeft==0 ){ |
| 476 | return pRight; |
| 477 | }else if( pRight==0 ){ |
| 478 | return pLeft; |
| 479 | }else{ |
danielk1977 | 880c15b | 2007-09-01 18:24:55 +0000 | [diff] [blame] | 480 | return sqlite3Expr(db, TK_AND, pLeft, pRight, 0); |
drh | 91bb0ee | 2004-09-01 03:06:34 +0000 | [diff] [blame] | 481 | } |
| 482 | } |
| 483 | |
| 484 | /* |
drh | 6977fea | 2002-10-22 23:38:04 +0000 | [diff] [blame] | 485 | ** Set the Expr.span field of the given expression to span all |
drh | e49b146 | 2008-07-09 01:39:44 +0000 | [diff] [blame] | 486 | ** text between the two given tokens. Both tokens must be pointing |
| 487 | ** at the same string. |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 488 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 489 | void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){ |
drh | 4efc475 | 2004-01-16 15:55:37 +0000 | [diff] [blame] | 490 | assert( pRight!=0 ); |
| 491 | assert( pLeft!=0 ); |
drh | e54a62a | 2008-07-18 17:03:52 +0000 | [diff] [blame] | 492 | if( pExpr ){ |
drh | e49b146 | 2008-07-09 01:39:44 +0000 | [diff] [blame] | 493 | pExpr->span.z = pLeft->z; |
| 494 | pExpr->span.n = pRight->n + (pRight->z - pLeft->z); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 495 | } |
| 496 | } |
| 497 | |
| 498 | /* |
| 499 | ** Construct a new expression node for a function with multiple |
| 500 | ** arguments. |
| 501 | */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 502 | Expr *sqlite3ExprFunction(Parse *pParse, ExprList *pList, Token *pToken){ |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 503 | Expr *pNew; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 504 | sqlite3 *db = pParse->db; |
danielk1977 | 4b202ae | 2006-01-23 05:50:58 +0000 | [diff] [blame] | 505 | assert( pToken ); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 506 | pNew = sqlite3DbMallocZero(db, sizeof(Expr) ); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 507 | if( pNew==0 ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 508 | sqlite3ExprListDelete(db, pList); /* Avoid leaking memory when malloc fails */ |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 509 | return 0; |
| 510 | } |
| 511 | pNew->op = TK_FUNCTION; |
| 512 | pNew->pList = pList; |
danielk1977 | 4b202ae | 2006-01-23 05:50:58 +0000 | [diff] [blame] | 513 | assert( pToken->dyn==0 ); |
| 514 | pNew->token = *pToken; |
drh | 6977fea | 2002-10-22 23:38:04 +0000 | [diff] [blame] | 515 | pNew->span = pNew->token; |
danielk1977 | fc97606 | 2007-05-10 10:46:56 +0000 | [diff] [blame] | 516 | |
danielk1977 | 4b5255a | 2008-06-05 16:47:39 +0000 | [diff] [blame] | 517 | sqlite3ExprSetHeight(pParse, pNew); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 518 | return pNew; |
| 519 | } |
| 520 | |
| 521 | /* |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 522 | ** Assign a variable number to an expression that encodes a wildcard |
| 523 | ** in the original SQL statement. |
| 524 | ** |
| 525 | ** Wildcards consisting of a single "?" are assigned the next sequential |
| 526 | ** variable number. |
| 527 | ** |
| 528 | ** Wildcards of the form "?nnn" are assigned the number "nnn". We make |
| 529 | ** sure "nnn" is not too be to avoid a denial of service attack when |
| 530 | ** the SQL statement comes from an external source. |
| 531 | ** |
| 532 | ** Wildcards of the form ":aaa" or "$aaa" are assigned the same number |
| 533 | ** as the previous instance of the same wildcard. Or if this is the first |
| 534 | ** instance of the wildcard, the next sequenial variable number is |
| 535 | ** assigned. |
| 536 | */ |
| 537 | void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){ |
| 538 | Token *pToken; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 539 | sqlite3 *db = pParse->db; |
| 540 | |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 541 | if( pExpr==0 ) return; |
| 542 | pToken = &pExpr->token; |
| 543 | assert( pToken->n>=1 ); |
| 544 | assert( pToken->z!=0 ); |
| 545 | assert( pToken->z[0]!=0 ); |
| 546 | if( pToken->n==1 ){ |
| 547 | /* Wildcard of the form "?". Assign the next variable number */ |
| 548 | pExpr->iTable = ++pParse->nVar; |
| 549 | }else if( pToken->z[0]=='?' ){ |
| 550 | /* Wildcard of the form "?nnn". Convert "nnn" to an integer and |
| 551 | ** use it as the variable number */ |
| 552 | int i; |
drh | 2646da7 | 2005-12-09 20:02:05 +0000 | [diff] [blame] | 553 | pExpr->iTable = i = atoi((char*)&pToken->z[1]); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 554 | testcase( i==0 ); |
| 555 | testcase( i==1 ); |
| 556 | testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 ); |
| 557 | testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ); |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 558 | if( i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){ |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 559 | sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d", |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 560 | db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]); |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 561 | } |
| 562 | if( i>pParse->nVar ){ |
| 563 | pParse->nVar = i; |
| 564 | } |
| 565 | }else{ |
| 566 | /* Wildcards of the form ":aaa" or "$aaa". Reuse the same variable |
| 567 | ** number as the prior appearance of the same name, or if the name |
| 568 | ** has never appeared before, reuse the same variable number |
| 569 | */ |
| 570 | int i, n; |
| 571 | n = pToken->n; |
| 572 | for(i=0; i<pParse->nVarExpr; i++){ |
| 573 | Expr *pE; |
| 574 | if( (pE = pParse->apVarExpr[i])!=0 |
| 575 | && pE->token.n==n |
| 576 | && memcmp(pE->token.z, pToken->z, n)==0 ){ |
| 577 | pExpr->iTable = pE->iTable; |
| 578 | break; |
| 579 | } |
| 580 | } |
| 581 | if( i>=pParse->nVarExpr ){ |
| 582 | pExpr->iTable = ++pParse->nVar; |
| 583 | if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){ |
| 584 | pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 585 | pParse->apVarExpr = |
| 586 | sqlite3DbReallocOrFree( |
| 587 | db, |
| 588 | pParse->apVarExpr, |
| 589 | pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0]) |
| 590 | ); |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 591 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 592 | if( !db->mallocFailed ){ |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 593 | assert( pParse->apVarExpr!=0 ); |
| 594 | pParse->apVarExpr[pParse->nVarExpr++] = pExpr; |
| 595 | } |
| 596 | } |
| 597 | } |
drh | bb4957f | 2008-03-20 14:03:29 +0000 | [diff] [blame] | 598 | if( !pParse->nErr && pParse->nVar>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){ |
danielk1977 | 832b266 | 2007-05-09 11:37:22 +0000 | [diff] [blame] | 599 | sqlite3ErrorMsg(pParse, "too many SQL variables"); |
| 600 | } |
drh | fa6bc00 | 2004-09-07 16:19:52 +0000 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | /* |
drh | a2e0004 | 2002-01-22 03:13:42 +0000 | [diff] [blame] | 604 | ** Recursively delete an expression tree. |
| 605 | */ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 606 | void sqlite3ExprDelete(sqlite3 *db, Expr *p){ |
drh | a2e0004 | 2002-01-22 03:13:42 +0000 | [diff] [blame] | 607 | if( p==0 ) return; |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 608 | if( p->span.dyn ) sqlite3DbFree(db, (char*)p->span.z); |
| 609 | if( p->token.dyn ) sqlite3DbFree(db, (char*)p->token.z); |
| 610 | sqlite3ExprDelete(db, p->pLeft); |
| 611 | sqlite3ExprDelete(db, p->pRight); |
| 612 | sqlite3ExprListDelete(db, p->pList); |
| 613 | sqlite3SelectDelete(db, p->pSelect); |
| 614 | sqlite3DbFree(db, p); |
drh | a2e0004 | 2002-01-22 03:13:42 +0000 | [diff] [blame] | 615 | } |
| 616 | |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 617 | /* |
| 618 | ** The Expr.token field might be a string literal that is quoted. |
| 619 | ** If so, remove the quotation marks. |
| 620 | */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 621 | void sqlite3DequoteExpr(sqlite3 *db, Expr *p){ |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 622 | if( ExprHasAnyProperty(p, EP_Dequoted) ){ |
| 623 | return; |
| 624 | } |
| 625 | ExprSetProperty(p, EP_Dequoted); |
| 626 | if( p->token.dyn==0 ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 627 | sqlite3TokenCopy(db, &p->token, &p->token); |
drh | d2687b7 | 2005-08-12 22:56:09 +0000 | [diff] [blame] | 628 | } |
| 629 | sqlite3Dequote((char*)p->token.z); |
| 630 | } |
| 631 | |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 632 | /* |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 633 | ** The following group of routines make deep copies of expressions, |
| 634 | ** expression lists, ID lists, and select statements. The copies can |
| 635 | ** be deleted (by being passed to their respective ...Delete() routines) |
| 636 | ** without effecting the originals. |
| 637 | ** |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 638 | ** The expression list, ID, and source lists return by sqlite3ExprListDup(), |
| 639 | ** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 640 | ** by subsequent calls to sqlite*ListAppend() routines. |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 641 | ** |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 642 | ** Any tables that the SrcList might point to are not duplicated. |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 643 | */ |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 644 | Expr *sqlite3ExprDup(sqlite3 *db, Expr *p){ |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 645 | Expr *pNew; |
| 646 | if( p==0 ) return 0; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 647 | pNew = sqlite3DbMallocRaw(db, sizeof(*p) ); |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 648 | if( pNew==0 ) return 0; |
drh | 3b167c7 | 2002-06-28 12:18:47 +0000 | [diff] [blame] | 649 | memcpy(pNew, p, sizeof(*pNew)); |
drh | 6977fea | 2002-10-22 23:38:04 +0000 | [diff] [blame] | 650 | if( p->token.z!=0 ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 651 | pNew->token.z = (u8*)sqlite3DbStrNDup(db, (char*)p->token.z, p->token.n); |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 652 | pNew->token.dyn = 1; |
| 653 | }else{ |
drh | 4efc475 | 2004-01-16 15:55:37 +0000 | [diff] [blame] | 654 | assert( pNew->token.z==0 ); |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 655 | } |
drh | 6977fea | 2002-10-22 23:38:04 +0000 | [diff] [blame] | 656 | pNew->span.z = 0; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 657 | pNew->pLeft = sqlite3ExprDup(db, p->pLeft); |
| 658 | pNew->pRight = sqlite3ExprDup(db, p->pRight); |
| 659 | pNew->pList = sqlite3ExprListDup(db, p->pList); |
| 660 | pNew->pSelect = sqlite3SelectDup(db, p->pSelect); |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 661 | return pNew; |
| 662 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 663 | void sqlite3TokenCopy(sqlite3 *db, Token *pTo, Token *pFrom){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 664 | if( pTo->dyn ) sqlite3DbFree(db, (char*)pTo->z); |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 665 | if( pFrom->z ){ |
| 666 | pTo->n = pFrom->n; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 667 | pTo->z = (u8*)sqlite3DbStrNDup(db, (char*)pFrom->z, pFrom->n); |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 668 | pTo->dyn = 1; |
| 669 | }else{ |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 670 | pTo->z = 0; |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 671 | } |
| 672 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 673 | ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p){ |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 674 | ExprList *pNew; |
drh | 145716b | 2004-09-24 12:24:06 +0000 | [diff] [blame] | 675 | struct ExprList_item *pItem, *pOldItem; |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 676 | int i; |
| 677 | if( p==0 ) return 0; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 678 | pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) ); |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 679 | if( pNew==0 ) return 0; |
danielk1977 | 31dad9d | 2007-08-16 11:36:15 +0000 | [diff] [blame] | 680 | pNew->iECursor = 0; |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 681 | pNew->nExpr = pNew->nAlloc = p->nExpr; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 682 | pNew->a = pItem = sqlite3DbMallocRaw(db, p->nExpr*sizeof(p->a[0]) ); |
danielk1977 | e004840 | 2004-06-15 16:51:01 +0000 | [diff] [blame] | 683 | if( pItem==0 ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 684 | sqlite3DbFree(db, pNew); |
danielk1977 | e004840 | 2004-06-15 16:51:01 +0000 | [diff] [blame] | 685 | return 0; |
| 686 | } |
drh | 145716b | 2004-09-24 12:24:06 +0000 | [diff] [blame] | 687 | pOldItem = p->a; |
| 688 | for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){ |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 689 | Expr *pNewExpr, *pOldExpr; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 690 | pItem->pExpr = pNewExpr = sqlite3ExprDup(db, pOldExpr = pOldItem->pExpr); |
drh | 6977fea | 2002-10-22 23:38:04 +0000 | [diff] [blame] | 691 | if( pOldExpr->span.z!=0 && pNewExpr ){ |
| 692 | /* Always make a copy of the span for top-level expressions in the |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 693 | ** expression list. The logic in SELECT processing that determines |
| 694 | ** the names of columns in the result set needs this information */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 695 | sqlite3TokenCopy(db, &pNewExpr->span, &pOldExpr->span); |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 696 | } |
drh | 1f3e905 | 2002-10-31 00:09:39 +0000 | [diff] [blame] | 697 | assert( pNewExpr==0 || pNewExpr->span.z!=0 |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 698 | || pOldExpr->span.z==0 |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 699 | || db->mallocFailed ); |
| 700 | pItem->zName = sqlite3DbStrDup(db, pOldItem->zName); |
drh | 145716b | 2004-09-24 12:24:06 +0000 | [diff] [blame] | 701 | pItem->sortOrder = pOldItem->sortOrder; |
drh | 3e7bc9c | 2004-02-21 19:17:17 +0000 | [diff] [blame] | 702 | pItem->done = 0; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 703 | pItem->iCol = pOldItem->iCol; |
drh | 8b21389 | 2008-08-29 02:14:02 +0000 | [diff] [blame] | 704 | pItem->iAlias = pOldItem->iAlias; |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 705 | } |
| 706 | return pNew; |
| 707 | } |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 708 | |
| 709 | /* |
| 710 | ** If cursors, triggers, views and subqueries are all omitted from |
| 711 | ** the build, then none of the following routines, except for |
| 712 | ** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes |
| 713 | ** called with a NULL argument. |
| 714 | */ |
danielk1977 | 6a67fe8 | 2005-02-04 04:07:16 +0000 | [diff] [blame] | 715 | #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \ |
| 716 | || !defined(SQLITE_OMIT_SUBQUERY) |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 717 | SrcList *sqlite3SrcListDup(sqlite3 *db, SrcList *p){ |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 718 | SrcList *pNew; |
| 719 | int i; |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 720 | int nByte; |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 721 | if( p==0 ) return 0; |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 722 | nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 723 | pNew = sqlite3DbMallocRaw(db, nByte ); |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 724 | if( pNew==0 ) return 0; |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 725 | pNew->nSrc = pNew->nAlloc = p->nSrc; |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 726 | for(i=0; i<p->nSrc; i++){ |
drh | 4efc475 | 2004-01-16 15:55:37 +0000 | [diff] [blame] | 727 | struct SrcList_item *pNewItem = &pNew->a[i]; |
| 728 | struct SrcList_item *pOldItem = &p->a[i]; |
drh | ed8a3bb | 2005-06-06 21:19:56 +0000 | [diff] [blame] | 729 | Table *pTab; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 730 | pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase); |
| 731 | pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName); |
| 732 | pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias); |
drh | 4efc475 | 2004-01-16 15:55:37 +0000 | [diff] [blame] | 733 | pNewItem->jointype = pOldItem->jointype; |
| 734 | pNewItem->iCursor = pOldItem->iCursor; |
danielk1977 | 1787cca | 2006-02-10 07:07:14 +0000 | [diff] [blame] | 735 | pNewItem->isPopulated = pOldItem->isPopulated; |
drh | ed8a3bb | 2005-06-06 21:19:56 +0000 | [diff] [blame] | 736 | pTab = pNewItem->pTab = pOldItem->pTab; |
| 737 | if( pTab ){ |
| 738 | pTab->nRef++; |
danielk1977 | a1cb183 | 2005-02-12 08:59:55 +0000 | [diff] [blame] | 739 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 740 | pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect); |
| 741 | pNewItem->pOn = sqlite3ExprDup(db, pOldItem->pOn); |
| 742 | pNewItem->pUsing = sqlite3IdListDup(db, pOldItem->pUsing); |
danielk1977 | 6c18b6e | 2005-01-30 09:17:58 +0000 | [diff] [blame] | 743 | pNewItem->colUsed = pOldItem->colUsed; |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 744 | } |
| 745 | return pNew; |
| 746 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 747 | IdList *sqlite3IdListDup(sqlite3 *db, IdList *p){ |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 748 | IdList *pNew; |
| 749 | int i; |
| 750 | if( p==0 ) return 0; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 751 | pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) ); |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 752 | if( pNew==0 ) return 0; |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 753 | pNew->nId = pNew->nAlloc = p->nId; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 754 | pNew->a = sqlite3DbMallocRaw(db, p->nId*sizeof(p->a[0]) ); |
danielk1977 | d5d5652 | 2005-03-16 12:15:20 +0000 | [diff] [blame] | 755 | if( pNew->a==0 ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 756 | sqlite3DbFree(db, pNew); |
danielk1977 | d5d5652 | 2005-03-16 12:15:20 +0000 | [diff] [blame] | 757 | return 0; |
| 758 | } |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 759 | for(i=0; i<p->nId; i++){ |
drh | 4efc475 | 2004-01-16 15:55:37 +0000 | [diff] [blame] | 760 | struct IdList_item *pNewItem = &pNew->a[i]; |
| 761 | struct IdList_item *pOldItem = &p->a[i]; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 762 | pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName); |
drh | 4efc475 | 2004-01-16 15:55:37 +0000 | [diff] [blame] | 763 | pNewItem->idx = pOldItem->idx; |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 764 | } |
| 765 | return pNew; |
| 766 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 767 | Select *sqlite3SelectDup(sqlite3 *db, Select *p){ |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 768 | Select *pNew; |
| 769 | if( p==0 ) return 0; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 770 | pNew = sqlite3DbMallocRaw(db, sizeof(*p) ); |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 771 | if( pNew==0 ) return 0; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 772 | pNew->pEList = sqlite3ExprListDup(db, p->pEList); |
| 773 | pNew->pSrc = sqlite3SrcListDup(db, p->pSrc); |
| 774 | pNew->pWhere = sqlite3ExprDup(db, p->pWhere); |
| 775 | pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy); |
| 776 | pNew->pHaving = sqlite3ExprDup(db, p->pHaving); |
| 777 | pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy); |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 778 | pNew->op = p->op; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 779 | pNew->pPrior = sqlite3SelectDup(db, p->pPrior); |
| 780 | pNew->pLimit = sqlite3ExprDup(db, p->pLimit); |
| 781 | pNew->pOffset = sqlite3ExprDup(db, p->pOffset); |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 782 | pNew->iLimit = 0; |
| 783 | pNew->iOffset = 0; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 784 | pNew->selFlags = p->selFlags & ~SF_UsesEphemeral; |
drh | 0342b1f | 2005-09-01 03:07:44 +0000 | [diff] [blame] | 785 | pNew->pRightmost = 0; |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 786 | pNew->addrOpenEphm[0] = -1; |
| 787 | pNew->addrOpenEphm[1] = -1; |
| 788 | pNew->addrOpenEphm[2] = -1; |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 789 | return pNew; |
| 790 | } |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 791 | #else |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 792 | Select *sqlite3SelectDup(sqlite3 *db, Select *p){ |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 793 | assert( p==0 ); |
| 794 | return 0; |
| 795 | } |
| 796 | #endif |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 797 | |
| 798 | |
| 799 | /* |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 800 | ** Add a new element to the end of an expression list. If pList is |
| 801 | ** initially NULL, then create a new expression list. |
| 802 | */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 803 | ExprList *sqlite3ExprListAppend( |
| 804 | Parse *pParse, /* Parsing context */ |
| 805 | ExprList *pList, /* List to which to append. Might be NULL */ |
| 806 | Expr *pExpr, /* Expression to be appended */ |
| 807 | Token *pName /* AS keyword for the expression */ |
| 808 | ){ |
| 809 | sqlite3 *db = pParse->db; |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 810 | if( pList==0 ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 811 | pList = sqlite3DbMallocZero(db, sizeof(ExprList) ); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 812 | if( pList==0 ){ |
danielk1977 | d5d5652 | 2005-03-16 12:15:20 +0000 | [diff] [blame] | 813 | goto no_mem; |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 814 | } |
drh | 4efc475 | 2004-01-16 15:55:37 +0000 | [diff] [blame] | 815 | assert( pList->nAlloc==0 ); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 816 | } |
drh | 4305d10 | 2003-07-30 12:34:12 +0000 | [diff] [blame] | 817 | if( pList->nAlloc<=pList->nExpr ){ |
danielk1977 | d5d5652 | 2005-03-16 12:15:20 +0000 | [diff] [blame] | 818 | struct ExprList_item *a; |
| 819 | int n = pList->nAlloc*2 + 4; |
danielk1977 | 26783a5 | 2007-08-29 14:06:22 +0000 | [diff] [blame] | 820 | a = sqlite3DbRealloc(db, pList->a, n*sizeof(pList->a[0])); |
danielk1977 | d5d5652 | 2005-03-16 12:15:20 +0000 | [diff] [blame] | 821 | if( a==0 ){ |
| 822 | goto no_mem; |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 823 | } |
danielk1977 | d5d5652 | 2005-03-16 12:15:20 +0000 | [diff] [blame] | 824 | pList->a = a; |
| 825 | pList->nAlloc = n; |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 826 | } |
drh | 4efc475 | 2004-01-16 15:55:37 +0000 | [diff] [blame] | 827 | assert( pList->a!=0 ); |
| 828 | if( pExpr || pName ){ |
| 829 | struct ExprList_item *pItem = &pList->a[pList->nExpr++]; |
| 830 | memset(pItem, 0, sizeof(*pItem)); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 831 | pItem->zName = sqlite3NameFromToken(db, pName); |
danielk1977 | e94ddc9 | 2005-03-21 03:53:38 +0000 | [diff] [blame] | 832 | pItem->pExpr = pExpr; |
drh | 8b21389 | 2008-08-29 02:14:02 +0000 | [diff] [blame] | 833 | pItem->iAlias = 0; |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 834 | } |
| 835 | return pList; |
danielk1977 | d5d5652 | 2005-03-16 12:15:20 +0000 | [diff] [blame] | 836 | |
| 837 | no_mem: |
| 838 | /* Avoid leaking memory if malloc has failed. */ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 839 | sqlite3ExprDelete(db, pExpr); |
| 840 | sqlite3ExprListDelete(db, pList); |
danielk1977 | d5d5652 | 2005-03-16 12:15:20 +0000 | [diff] [blame] | 841 | return 0; |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 842 | } |
| 843 | |
| 844 | /* |
danielk1977 | 7a15a4b | 2007-05-08 17:54:43 +0000 | [diff] [blame] | 845 | ** If the expression list pEList contains more than iLimit elements, |
| 846 | ** leave an error message in pParse. |
| 847 | */ |
| 848 | void sqlite3ExprListCheckLength( |
| 849 | Parse *pParse, |
| 850 | ExprList *pEList, |
danielk1977 | 7a15a4b | 2007-05-08 17:54:43 +0000 | [diff] [blame] | 851 | const char *zObject |
| 852 | ){ |
drh | b1a6c3c | 2008-03-20 16:30:17 +0000 | [diff] [blame] | 853 | int mx = pParse->db->aLimit[SQLITE_LIMIT_COLUMN]; |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 854 | testcase( pEList && pEList->nExpr==mx ); |
| 855 | testcase( pEList && pEList->nExpr==mx+1 ); |
drh | b1a6c3c | 2008-03-20 16:30:17 +0000 | [diff] [blame] | 856 | if( pEList && pEList->nExpr>mx ){ |
danielk1977 | 7a15a4b | 2007-05-08 17:54:43 +0000 | [diff] [blame] | 857 | sqlite3ErrorMsg(pParse, "too many columns in %s", zObject); |
| 858 | } |
| 859 | } |
| 860 | |
| 861 | /* |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 862 | ** Delete an entire expression list. |
| 863 | */ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 864 | void sqlite3ExprListDelete(sqlite3 *db, ExprList *pList){ |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 865 | int i; |
drh | be5c89a | 2004-07-26 00:31:09 +0000 | [diff] [blame] | 866 | struct ExprList_item *pItem; |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 867 | if( pList==0 ) return; |
drh | 1bdd9b5 | 2004-04-23 17:04:44 +0000 | [diff] [blame] | 868 | assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) ); |
| 869 | assert( pList->nExpr<=pList->nAlloc ); |
drh | be5c89a | 2004-07-26 00:31:09 +0000 | [diff] [blame] | 870 | for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 871 | sqlite3ExprDelete(db, pItem->pExpr); |
| 872 | sqlite3DbFree(db, pItem->zName); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 873 | } |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 874 | sqlite3DbFree(db, pList->a); |
| 875 | sqlite3DbFree(db, pList); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 876 | } |
| 877 | |
| 878 | /* |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 879 | ** These routines are Walker callbacks. Walker.u.pi is a pointer |
| 880 | ** to an integer. These routines are checking an expression to see |
| 881 | ** if it is a constant. Set *Walker.u.pi to 0 if the expression is |
| 882 | ** not constant. |
drh | 73b211a | 2005-01-18 04:00:42 +0000 | [diff] [blame] | 883 | ** |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 884 | ** These callback routines are used to implement the following: |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 885 | ** |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 886 | ** sqlite3ExprIsConstant() |
| 887 | ** sqlite3ExprIsConstantNotJoin() |
| 888 | ** sqlite3ExprIsConstantOrFunction() |
drh | 87abf5c | 2005-08-25 12:45:04 +0000 | [diff] [blame] | 889 | ** |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 890 | */ |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 891 | static int exprNodeIsConstant(Walker *pWalker, Expr *pExpr){ |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 892 | |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 893 | /* If pWalker->u.i is 3 then any term of the expression that comes from |
drh | 0a16837 | 2007-06-08 00:20:47 +0000 | [diff] [blame] | 894 | ** the ON or USING clauses of a join disqualifies the expression |
| 895 | ** from being considered constant. */ |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 896 | if( pWalker->u.i==3 && ExprHasAnyProperty(pExpr, EP_FromJoin) ){ |
| 897 | pWalker->u.i = 0; |
| 898 | return WRC_Abort; |
drh | 0a16837 | 2007-06-08 00:20:47 +0000 | [diff] [blame] | 899 | } |
| 900 | |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 901 | switch( pExpr->op ){ |
drh | eb55bd2 | 2005-06-30 17:04:21 +0000 | [diff] [blame] | 902 | /* Consider functions to be constant if all their arguments are constant |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 903 | ** and pWalker->u.i==2 */ |
drh | eb55bd2 | 2005-06-30 17:04:21 +0000 | [diff] [blame] | 904 | case TK_FUNCTION: |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 905 | if( pWalker->u.i==2 ) return 0; |
drh | eb55bd2 | 2005-06-30 17:04:21 +0000 | [diff] [blame] | 906 | /* Fall through */ |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 907 | case TK_ID: |
| 908 | case TK_COLUMN: |
| 909 | case TK_DOT: |
| 910 | case TK_AGG_FUNCTION: |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 911 | case TK_AGG_COLUMN: |
drh | fe2093d | 2005-01-20 22:48:47 +0000 | [diff] [blame] | 912 | #ifndef SQLITE_OMIT_SUBQUERY |
| 913 | case TK_SELECT: |
| 914 | case TK_EXISTS: |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 915 | testcase( pExpr->op==TK_SELECT ); |
| 916 | testcase( pExpr->op==TK_EXISTS ); |
drh | fe2093d | 2005-01-20 22:48:47 +0000 | [diff] [blame] | 917 | #endif |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 918 | testcase( pExpr->op==TK_ID ); |
| 919 | testcase( pExpr->op==TK_COLUMN ); |
| 920 | testcase( pExpr->op==TK_DOT ); |
| 921 | testcase( pExpr->op==TK_AGG_FUNCTION ); |
| 922 | testcase( pExpr->op==TK_AGG_COLUMN ); |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 923 | pWalker->u.i = 0; |
| 924 | return WRC_Abort; |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 925 | default: |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 926 | return WRC_Continue; |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 927 | } |
| 928 | } |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 929 | static int selectNodeIsConstant(Walker *pWalker, Select *pSelect){ |
| 930 | pWalker->u.i = 0; |
| 931 | return WRC_Abort; |
| 932 | } |
| 933 | static int exprIsConst(Expr *p, int initFlag){ |
| 934 | Walker w; |
| 935 | w.u.i = initFlag; |
| 936 | w.xExprCallback = exprNodeIsConstant; |
| 937 | w.xSelectCallback = selectNodeIsConstant; |
| 938 | sqlite3WalkExpr(&w, p); |
| 939 | return w.u.i; |
| 940 | } |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 941 | |
| 942 | /* |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 943 | ** Walk an expression tree. Return 1 if the expression is constant |
drh | eb55bd2 | 2005-06-30 17:04:21 +0000 | [diff] [blame] | 944 | ** and 0 if it involves variables or function calls. |
drh | 2398937 | 2002-05-21 13:43:04 +0000 | [diff] [blame] | 945 | ** |
| 946 | ** For the purposes of this function, a double-quoted string (ex: "abc") |
| 947 | ** is considered a variable but a single-quoted string (ex: 'abc') is |
| 948 | ** a constant. |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 949 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 950 | int sqlite3ExprIsConstant(Expr *p){ |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 951 | return exprIsConst(p, 1); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 952 | } |
| 953 | |
| 954 | /* |
drh | eb55bd2 | 2005-06-30 17:04:21 +0000 | [diff] [blame] | 955 | ** Walk an expression tree. Return 1 if the expression is constant |
drh | 0a16837 | 2007-06-08 00:20:47 +0000 | [diff] [blame] | 956 | ** that does no originate from the ON or USING clauses of a join. |
| 957 | ** Return 0 if it involves variables or function calls or terms from |
| 958 | ** an ON or USING clause. |
| 959 | */ |
| 960 | int sqlite3ExprIsConstantNotJoin(Expr *p){ |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 961 | return exprIsConst(p, 3); |
drh | 0a16837 | 2007-06-08 00:20:47 +0000 | [diff] [blame] | 962 | } |
| 963 | |
| 964 | /* |
| 965 | ** Walk an expression tree. Return 1 if the expression is constant |
drh | eb55bd2 | 2005-06-30 17:04:21 +0000 | [diff] [blame] | 966 | ** or a function call with constant arguments. Return and 0 if there |
| 967 | ** are any variables. |
| 968 | ** |
| 969 | ** For the purposes of this function, a double-quoted string (ex: "abc") |
| 970 | ** is considered a variable but a single-quoted string (ex: 'abc') is |
| 971 | ** a constant. |
| 972 | */ |
| 973 | int sqlite3ExprIsConstantOrFunction(Expr *p){ |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 974 | return exprIsConst(p, 2); |
drh | eb55bd2 | 2005-06-30 17:04:21 +0000 | [diff] [blame] | 975 | } |
| 976 | |
| 977 | /* |
drh | 73b211a | 2005-01-18 04:00:42 +0000 | [diff] [blame] | 978 | ** If the expression p codes a constant integer that is small enough |
drh | 202b2df | 2004-01-06 01:13:46 +0000 | [diff] [blame] | 979 | ** to fit in a 32-bit integer, return 1 and put the value of the integer |
| 980 | ** in *pValue. If the expression is not an integer or if it is too big |
| 981 | ** 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] | 982 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 983 | int sqlite3ExprIsInteger(Expr *p, int *pValue){ |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 984 | int rc = 0; |
| 985 | if( p->flags & EP_IntValue ){ |
| 986 | *pValue = p->iTable; |
| 987 | return 1; |
| 988 | } |
drh | e4de1fe | 2002-06-02 16:09:01 +0000 | [diff] [blame] | 989 | switch( p->op ){ |
| 990 | case TK_INTEGER: { |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 991 | rc = sqlite3GetInt32((char*)p->token.z, pValue); |
drh | 202b2df | 2004-01-06 01:13:46 +0000 | [diff] [blame] | 992 | break; |
drh | e4de1fe | 2002-06-02 16:09:01 +0000 | [diff] [blame] | 993 | } |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 994 | case TK_UPLUS: { |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 995 | rc = sqlite3ExprIsInteger(p->pLeft, pValue); |
drh | f6e369a | 2008-06-24 12:46:30 +0000 | [diff] [blame] | 996 | break; |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 997 | } |
drh | e4de1fe | 2002-06-02 16:09:01 +0000 | [diff] [blame] | 998 | case TK_UMINUS: { |
| 999 | int v; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1000 | if( sqlite3ExprIsInteger(p->pLeft, &v) ){ |
drh | e4de1fe | 2002-06-02 16:09:01 +0000 | [diff] [blame] | 1001 | *pValue = -v; |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 1002 | rc = 1; |
drh | e4de1fe | 2002-06-02 16:09:01 +0000 | [diff] [blame] | 1003 | } |
| 1004 | break; |
| 1005 | } |
| 1006 | default: break; |
| 1007 | } |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 1008 | if( rc ){ |
| 1009 | p->op = TK_INTEGER; |
| 1010 | p->flags |= EP_IntValue; |
| 1011 | p->iTable = *pValue; |
| 1012 | } |
| 1013 | return rc; |
drh | e4de1fe | 2002-06-02 16:09:01 +0000 | [diff] [blame] | 1014 | } |
| 1015 | |
| 1016 | /* |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1017 | ** Return TRUE if the given string is a row-id column name. |
| 1018 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1019 | int sqlite3IsRowid(const char *z){ |
| 1020 | if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1; |
| 1021 | if( sqlite3StrICmp(z, "ROWID")==0 ) return 1; |
| 1022 | if( sqlite3StrICmp(z, "OID")==0 ) return 1; |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1023 | return 0; |
| 1024 | } |
| 1025 | |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 1026 | #ifdef SQLITE_TEST |
| 1027 | int sqlite3_enable_in_opt = 1; |
| 1028 | #else |
| 1029 | #define sqlite3_enable_in_opt 1 |
| 1030 | #endif |
| 1031 | |
| 1032 | /* |
drh | b287f4b | 2008-04-25 00:08:38 +0000 | [diff] [blame] | 1033 | ** Return true if the IN operator optimization is enabled and |
| 1034 | ** the SELECT statement p exists and is of the |
| 1035 | ** simple form: |
| 1036 | ** |
| 1037 | ** SELECT <column> FROM <table> |
| 1038 | ** |
| 1039 | ** If this is the case, it may be possible to use an existing table |
| 1040 | ** or index instead of generating an epheremal table. |
| 1041 | */ |
| 1042 | #ifndef SQLITE_OMIT_SUBQUERY |
| 1043 | static int isCandidateForInOpt(Select *p){ |
| 1044 | SrcList *pSrc; |
| 1045 | ExprList *pEList; |
| 1046 | Table *pTab; |
| 1047 | if( !sqlite3_enable_in_opt ) return 0; /* IN optimization must be enabled */ |
| 1048 | if( p==0 ) return 0; /* right-hand side of IN is SELECT */ |
| 1049 | if( p->pPrior ) return 0; /* Not a compound SELECT */ |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 1050 | if( p->selFlags & (SF_Distinct|SF_Aggregate) ){ |
| 1051 | return 0; /* No DISTINCT keyword and no aggregate functions */ |
| 1052 | } |
drh | b287f4b | 2008-04-25 00:08:38 +0000 | [diff] [blame] | 1053 | if( p->pGroupBy ) return 0; /* Has no GROUP BY clause */ |
| 1054 | if( p->pLimit ) return 0; /* Has no LIMIT clause */ |
| 1055 | if( p->pOffset ) return 0; |
| 1056 | if( p->pWhere ) return 0; /* Has no WHERE clause */ |
| 1057 | pSrc = p->pSrc; |
| 1058 | if( pSrc==0 ) return 0; /* A single table in the FROM clause */ |
| 1059 | if( pSrc->nSrc!=1 ) return 0; |
| 1060 | if( pSrc->a[0].pSelect ) return 0; /* FROM clause is not a subquery */ |
| 1061 | pTab = pSrc->a[0].pTab; |
| 1062 | if( pTab==0 ) return 0; |
| 1063 | if( pTab->pSelect ) return 0; /* FROM clause is not a view */ |
| 1064 | if( IsVirtual(pTab) ) return 0; /* FROM clause not a virtual table */ |
| 1065 | pEList = p->pEList; |
| 1066 | if( pEList->nExpr!=1 ) return 0; /* One column in the result set */ |
| 1067 | if( pEList->a[0].pExpr->op!=TK_COLUMN ) return 0; /* Result is a column */ |
| 1068 | return 1; |
| 1069 | } |
| 1070 | #endif /* SQLITE_OMIT_SUBQUERY */ |
| 1071 | |
| 1072 | /* |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 1073 | ** This function is used by the implementation of the IN (...) operator. |
| 1074 | ** It's job is to find or create a b-tree structure that may be used |
| 1075 | ** either to test for membership of the (...) set or to iterate through |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 1076 | ** its members, skipping duplicates. |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 1077 | ** |
| 1078 | ** The cursor opened on the structure (database table, database index |
| 1079 | ** or ephermal table) is stored in pX->iTable before this function returns. |
| 1080 | ** The returned value indicates the structure type, as follows: |
| 1081 | ** |
| 1082 | ** IN_INDEX_ROWID - The cursor was opened on a database table. |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 1083 | ** IN_INDEX_INDEX - The cursor was opened on a database index. |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 1084 | ** IN_INDEX_EPH - The cursor was opened on a specially created and |
| 1085 | ** populated epheremal table. |
| 1086 | ** |
| 1087 | ** An existing structure may only be used if the SELECT is of the simple |
| 1088 | ** form: |
| 1089 | ** |
| 1090 | ** SELECT <column> FROM <table> |
| 1091 | ** |
danielk1977 | 0cdc022 | 2008-06-26 18:04:03 +0000 | [diff] [blame] | 1092 | ** If prNotFound parameter is 0, then the structure will be used to iterate |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 1093 | ** through the set members, skipping any duplicates. In this case an |
| 1094 | ** epheremal table must be used unless the selected <column> is guaranteed |
| 1095 | ** to be unique - either because it is an INTEGER PRIMARY KEY or it |
| 1096 | ** is unique by virtue of a constraint or implicit index. |
danielk1977 | 0cdc022 | 2008-06-26 18:04:03 +0000 | [diff] [blame] | 1097 | ** |
| 1098 | ** If the prNotFound parameter is not 0, then the structure will be used |
| 1099 | ** for fast set membership tests. In this case an epheremal table must |
| 1100 | ** be used unless <column> is an INTEGER PRIMARY KEY or an index can |
| 1101 | ** be found with <column> as its left-most column. |
| 1102 | ** |
| 1103 | ** When the structure is being used for set membership tests, the user |
| 1104 | ** needs to know whether or not the structure contains an SQL NULL |
| 1105 | ** value in order to correctly evaluate expressions like "X IN (Y, Z)". |
| 1106 | ** If there is a chance that the structure may contain a NULL value at |
| 1107 | ** runtime, then a register is allocated and the register number written |
| 1108 | ** to *prNotFound. If there is no chance that the structure contains a |
| 1109 | ** NULL value, then *prNotFound is left unchanged. |
| 1110 | ** |
| 1111 | ** If a register is allocated and its location stored in *prNotFound, then |
| 1112 | ** its initial value is NULL. If the structure does not remain constant |
| 1113 | ** for the duration of the query (i.e. the set is a correlated sub-select), |
| 1114 | ** the value of the allocated register is reset to NULL each time the |
| 1115 | ** structure is repopulated. This allows the caller to use vdbe code |
| 1116 | ** equivalent to the following: |
| 1117 | ** |
| 1118 | ** if( register==NULL ){ |
| 1119 | ** has_null = <test if data structure contains null> |
| 1120 | ** register = 1 |
| 1121 | ** } |
| 1122 | ** |
| 1123 | ** in order to avoid running the <test if data structure contains null> |
| 1124 | ** test more often than is necessary. |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 1125 | */ |
danielk1977 | 284f4ac | 2007-12-10 05:03:46 +0000 | [diff] [blame] | 1126 | #ifndef SQLITE_OMIT_SUBQUERY |
danielk1977 | 0cdc022 | 2008-06-26 18:04:03 +0000 | [diff] [blame] | 1127 | int sqlite3FindInIndex(Parse *pParse, Expr *pX, int *prNotFound){ |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 1128 | Select *p; |
| 1129 | int eType = 0; |
| 1130 | int iTab = pParse->nTab++; |
danielk1977 | 0cdc022 | 2008-06-26 18:04:03 +0000 | [diff] [blame] | 1131 | int mustBeUnique = !prNotFound; |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 1132 | |
| 1133 | /* The follwing if(...) expression is true if the SELECT is of the |
| 1134 | ** simple form: |
| 1135 | ** |
| 1136 | ** SELECT <column> FROM <table> |
| 1137 | ** |
| 1138 | ** If this is the case, it may be possible to use an existing table |
| 1139 | ** or index instead of generating an epheremal table. |
| 1140 | */ |
drh | b287f4b | 2008-04-25 00:08:38 +0000 | [diff] [blame] | 1141 | p = pX->pSelect; |
| 1142 | if( isCandidateForInOpt(p) ){ |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 1143 | sqlite3 *db = pParse->db; |
| 1144 | Index *pIdx; |
| 1145 | Expr *pExpr = p->pEList->a[0].pExpr; |
| 1146 | int iCol = pExpr->iColumn; |
| 1147 | Vdbe *v = sqlite3GetVdbe(pParse); |
| 1148 | |
| 1149 | /* This function is only called from two places. In both cases the vdbe |
| 1150 | ** has already been allocated. So assume sqlite3GetVdbe() is always |
| 1151 | ** successful here. |
| 1152 | */ |
| 1153 | assert(v); |
| 1154 | if( iCol<0 ){ |
drh | 0a07c10 | 2008-01-03 18:03:08 +0000 | [diff] [blame] | 1155 | int iMem = ++pParse->nMem; |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 1156 | int iAddr; |
| 1157 | Table *pTab = p->pSrc->a[0].pTab; |
| 1158 | int iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
| 1159 | sqlite3VdbeUsesBtree(v, iDb); |
| 1160 | |
drh | 892d317 | 2008-01-10 03:46:36 +0000 | [diff] [blame] | 1161 | iAddr = sqlite3VdbeAddOp1(v, OP_If, iMem); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1162 | sqlite3VdbeAddOp2(v, OP_Integer, 1, iMem); |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 1163 | |
| 1164 | sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead); |
| 1165 | eType = IN_INDEX_ROWID; |
| 1166 | |
| 1167 | sqlite3VdbeJumpHere(v, iAddr); |
| 1168 | }else{ |
| 1169 | /* The collation sequence used by the comparison. If an index is to |
| 1170 | ** be used in place of a temp-table, it must be ordered according |
| 1171 | ** to this collation sequence. |
| 1172 | */ |
| 1173 | CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pExpr); |
| 1174 | |
| 1175 | /* Check that the affinity that will be used to perform the |
| 1176 | ** comparison is the same as the affinity of the column. If |
| 1177 | ** it is not, it is not possible to use any index. |
| 1178 | */ |
| 1179 | Table *pTab = p->pSrc->a[0].pTab; |
| 1180 | char aff = comparisonAffinity(pX); |
| 1181 | int affinity_ok = (pTab->aCol[iCol].affinity==aff||aff==SQLITE_AFF_NONE); |
| 1182 | |
| 1183 | for(pIdx=pTab->pIndex; pIdx && eType==0 && affinity_ok; pIdx=pIdx->pNext){ |
| 1184 | if( (pIdx->aiColumn[0]==iCol) |
| 1185 | && (pReq==sqlite3FindCollSeq(db, ENC(db), pIdx->azColl[0], -1, 0)) |
| 1186 | && (!mustBeUnique || (pIdx->nColumn==1 && pIdx->onError!=OE_None)) |
| 1187 | ){ |
| 1188 | int iDb; |
drh | 0a07c10 | 2008-01-03 18:03:08 +0000 | [diff] [blame] | 1189 | int iMem = ++pParse->nMem; |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 1190 | int iAddr; |
| 1191 | char *pKey; |
| 1192 | |
| 1193 | pKey = (char *)sqlite3IndexKeyinfo(pParse, pIdx); |
| 1194 | iDb = sqlite3SchemaToIndex(db, pIdx->pSchema); |
| 1195 | sqlite3VdbeUsesBtree(v, iDb); |
| 1196 | |
drh | 892d317 | 2008-01-10 03:46:36 +0000 | [diff] [blame] | 1197 | iAddr = sqlite3VdbeAddOp1(v, OP_If, iMem); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1198 | sqlite3VdbeAddOp2(v, OP_Integer, 1, iMem); |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 1199 | |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 1200 | sqlite3VdbeAddOp2(v, OP_SetNumColumns, 0, pIdx->nColumn); |
danielk1977 | 207872a | 2008-01-03 07:54:23 +0000 | [diff] [blame] | 1201 | sqlite3VdbeAddOp4(v, OP_OpenRead, iTab, pIdx->tnum, iDb, |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1202 | pKey,P4_KEYINFO_HANDOFF); |
danielk1977 | 207872a | 2008-01-03 07:54:23 +0000 | [diff] [blame] | 1203 | VdbeComment((v, "%s", pIdx->zName)); |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 1204 | eType = IN_INDEX_INDEX; |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 1205 | |
| 1206 | sqlite3VdbeJumpHere(v, iAddr); |
danielk1977 | 0cdc022 | 2008-06-26 18:04:03 +0000 | [diff] [blame] | 1207 | if( prNotFound && !pTab->aCol[iCol].notNull ){ |
| 1208 | *prNotFound = ++pParse->nMem; |
| 1209 | } |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 1210 | } |
| 1211 | } |
| 1212 | } |
| 1213 | } |
| 1214 | |
| 1215 | if( eType==0 ){ |
danielk1977 | 0cdc022 | 2008-06-26 18:04:03 +0000 | [diff] [blame] | 1216 | int rMayHaveNull = 0; |
| 1217 | if( prNotFound ){ |
| 1218 | *prNotFound = rMayHaveNull = ++pParse->nMem; |
| 1219 | } |
| 1220 | sqlite3CodeSubselect(pParse, pX, rMayHaveNull); |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 1221 | eType = IN_INDEX_EPH; |
| 1222 | }else{ |
| 1223 | pX->iTable = iTab; |
| 1224 | } |
| 1225 | return eType; |
| 1226 | } |
danielk1977 | 284f4ac | 2007-12-10 05:03:46 +0000 | [diff] [blame] | 1227 | #endif |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 1228 | |
| 1229 | /* |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 1230 | ** Generate code for scalar subqueries used as an expression |
| 1231 | ** and IN operators. Examples: |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 1232 | ** |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 1233 | ** (SELECT a FROM b) -- subquery |
| 1234 | ** EXISTS (SELECT a FROM b) -- EXISTS subquery |
| 1235 | ** x IN (4,5,11) -- IN operator with list on right-hand side |
| 1236 | ** x IN (SELECT a FROM b) -- IN operator with subquery on the right |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1237 | ** |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 1238 | ** The pExpr parameter describes the expression that contains the IN |
| 1239 | ** operator or subquery. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1240 | */ |
drh | 51522cd | 2005-01-20 13:36:19 +0000 | [diff] [blame] | 1241 | #ifndef SQLITE_OMIT_SUBQUERY |
danielk1977 | 0cdc022 | 2008-06-26 18:04:03 +0000 | [diff] [blame] | 1242 | void sqlite3CodeSubselect(Parse *pParse, Expr *pExpr, int rMayHaveNull){ |
drh | 57dbd7b | 2005-07-08 18:25:26 +0000 | [diff] [blame] | 1243 | int testAddr = 0; /* One-time test address */ |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 1244 | Vdbe *v = sqlite3GetVdbe(pParse); |
| 1245 | if( v==0 ) return; |
| 1246 | |
danielk1977 | fc97606 | 2007-05-10 10:46:56 +0000 | [diff] [blame] | 1247 | |
drh | 57dbd7b | 2005-07-08 18:25:26 +0000 | [diff] [blame] | 1248 | /* This code must be run in its entirety every time it is encountered |
| 1249 | ** if any of the following is true: |
| 1250 | ** |
| 1251 | ** * The right-hand side is a correlated subquery |
| 1252 | ** * The right-hand side is an expression list containing variables |
| 1253 | ** * We are inside a trigger |
| 1254 | ** |
| 1255 | ** If all of the above are false, then we can run this code just once |
| 1256 | ** save the results, and reuse the same result on subsequent invocations. |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 1257 | */ |
| 1258 | if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->trigStack ){ |
drh | 0a07c10 | 2008-01-03 18:03:08 +0000 | [diff] [blame] | 1259 | int mem = ++pParse->nMem; |
drh | 892d317 | 2008-01-10 03:46:36 +0000 | [diff] [blame] | 1260 | sqlite3VdbeAddOp1(v, OP_If, mem); |
| 1261 | testAddr = sqlite3VdbeAddOp2(v, OP_Integer, 1, mem); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1262 | assert( testAddr>0 || pParse->db->mallocFailed ); |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 1263 | } |
| 1264 | |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1265 | switch( pExpr->op ){ |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1266 | case TK_IN: { |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 1267 | char affinity; |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 1268 | KeyInfo keyInfo; |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 1269 | int addr; /* Address of OP_OpenEphemeral instruction */ |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 1270 | |
danielk1977 | 0cdc022 | 2008-06-26 18:04:03 +0000 | [diff] [blame] | 1271 | if( rMayHaveNull ){ |
| 1272 | sqlite3VdbeAddOp2(v, OP_Null, 0, rMayHaveNull); |
| 1273 | } |
| 1274 | |
danielk1977 | bf3b721 | 2004-05-18 10:06:24 +0000 | [diff] [blame] | 1275 | affinity = sqlite3ExprAffinity(pExpr->pLeft); |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 1276 | |
| 1277 | /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)' |
drh | 57dbd7b | 2005-07-08 18:25:26 +0000 | [diff] [blame] | 1278 | ** expression it is handled the same way. A virtual table is |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 1279 | ** filled with single-field index keys representing the results |
| 1280 | ** from the SELECT or the <exprlist>. |
| 1281 | ** |
| 1282 | ** If the 'x' expression is a column value, or the SELECT... |
| 1283 | ** statement returns a column value, then the affinity of that |
| 1284 | ** column is used to build the index keys. If both 'x' and the |
| 1285 | ** SELECT... statement are columns, then numeric affinity is used |
| 1286 | ** if either column has NUMERIC or INTEGER affinity. If neither |
| 1287 | ** 'x' nor the SELECT... statement are columns, then numeric affinity |
| 1288 | ** is used. |
| 1289 | */ |
| 1290 | pExpr->iTable = pParse->nTab++; |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 1291 | addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pExpr->iTable, 1); |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 1292 | memset(&keyInfo, 0, sizeof(keyInfo)); |
| 1293 | keyInfo.nField = 1; |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 1294 | |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1295 | if( pExpr->pSelect ){ |
| 1296 | /* Case 1: expr IN (SELECT ...) |
| 1297 | ** |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 1298 | ** Generate code to write the results of the select into the temporary |
| 1299 | ** table allocated and opened above. |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1300 | */ |
drh | 1013c93 | 2008-01-06 00:25:21 +0000 | [diff] [blame] | 1301 | SelectDest dest; |
drh | be5c89a | 2004-07-26 00:31:09 +0000 | [diff] [blame] | 1302 | ExprList *pEList; |
drh | 1013c93 | 2008-01-06 00:25:21 +0000 | [diff] [blame] | 1303 | |
| 1304 | sqlite3SelectDestInit(&dest, SRT_Set, pExpr->iTable); |
| 1305 | dest.affinity = (int)affinity; |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 1306 | assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable ); |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 1307 | if( sqlite3Select(pParse, pExpr->pSelect, &dest) ){ |
drh | 94ccde5 | 2007-04-13 16:06:32 +0000 | [diff] [blame] | 1308 | return; |
| 1309 | } |
drh | be5c89a | 2004-07-26 00:31:09 +0000 | [diff] [blame] | 1310 | pEList = pExpr->pSelect->pEList; |
| 1311 | if( pEList && pEList->nExpr>0 ){ |
danielk1977 | bcbb04e | 2007-05-29 12:11:29 +0000 | [diff] [blame] | 1312 | keyInfo.aColl[0] = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft, |
drh | be5c89a | 2004-07-26 00:31:09 +0000 | [diff] [blame] | 1313 | pEList->a[0].pExpr); |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 1314 | } |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1315 | }else if( pExpr->pList ){ |
| 1316 | /* Case 2: expr IN (exprlist) |
| 1317 | ** |
drh | fd131da | 2007-08-07 17:13:03 +0000 | [diff] [blame] | 1318 | ** For each expression, build an index key from the evaluation and |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 1319 | ** store it in the temporary table. If <expr> is a column, then use |
| 1320 | ** that columns affinity when building index keys. If <expr> is not |
| 1321 | ** a column, use numeric affinity. |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1322 | */ |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 1323 | int i; |
drh | 57dbd7b | 2005-07-08 18:25:26 +0000 | [diff] [blame] | 1324 | ExprList *pList = pExpr->pList; |
| 1325 | struct ExprList_item *pItem; |
drh | ecc3180 | 2008-06-26 20:06:06 +0000 | [diff] [blame] | 1326 | int r1, r2, r3; |
drh | 57dbd7b | 2005-07-08 18:25:26 +0000 | [diff] [blame] | 1327 | |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 1328 | if( !affinity ){ |
drh | 8159a35 | 2006-05-23 23:22:29 +0000 | [diff] [blame] | 1329 | affinity = SQLITE_AFF_NONE; |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 1330 | } |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 1331 | keyInfo.aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft); |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 1332 | |
| 1333 | /* Loop through each expression in <exprlist>. */ |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 1334 | r1 = sqlite3GetTempReg(pParse); |
| 1335 | r2 = sqlite3GetTempReg(pParse); |
drh | 57dbd7b | 2005-07-08 18:25:26 +0000 | [diff] [blame] | 1336 | for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){ |
| 1337 | Expr *pE2 = pItem->pExpr; |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 1338 | |
drh | 57dbd7b | 2005-07-08 18:25:26 +0000 | [diff] [blame] | 1339 | /* If the expression is not constant then we will need to |
| 1340 | ** disable the test that was generated above that makes sure |
| 1341 | ** this code only executes once. Because for a non-constant |
| 1342 | ** expression we need to rerun this code each time. |
| 1343 | */ |
drh | 892d317 | 2008-01-10 03:46:36 +0000 | [diff] [blame] | 1344 | if( testAddr && !sqlite3ExprIsConstant(pE2) ){ |
| 1345 | sqlite3VdbeChangeToNoop(v, testAddr-1, 2); |
drh | 57dbd7b | 2005-07-08 18:25:26 +0000 | [diff] [blame] | 1346 | testAddr = 0; |
drh | 4794b98 | 2000-06-06 13:54:14 +0000 | [diff] [blame] | 1347 | } |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 1348 | |
| 1349 | /* Evaluate the expression and insert it into the temp table */ |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 1350 | pParse->disableColCache++; |
drh | ecc3180 | 2008-06-26 20:06:06 +0000 | [diff] [blame] | 1351 | r3 = sqlite3ExprCodeTarget(pParse, pE2, r1); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 1352 | assert( pParse->disableColCache>0 ); |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 1353 | pParse->disableColCache--; |
drh | ecc3180 | 2008-06-26 20:06:06 +0000 | [diff] [blame] | 1354 | sqlite3VdbeAddOp4(v, OP_MakeRecord, r3, 1, r2, &affinity, 1); |
drh | 3c31fc2 | 2008-06-26 21:45:26 +0000 | [diff] [blame] | 1355 | sqlite3ExprCacheAffinityChange(pParse, r3, 1); |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 1356 | sqlite3VdbeAddOp2(v, OP_IdxInsert, pExpr->iTable, r2); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1357 | } |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 1358 | sqlite3ReleaseTempReg(pParse, r1); |
| 1359 | sqlite3ReleaseTempReg(pParse, r2); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1360 | } |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1361 | sqlite3VdbeChangeP4(v, addr, (void *)&keyInfo, P4_KEYINFO); |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 1362 | break; |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1363 | } |
| 1364 | |
drh | 51522cd | 2005-01-20 13:36:19 +0000 | [diff] [blame] | 1365 | case TK_EXISTS: |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 1366 | case TK_SELECT: { |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1367 | /* This has to be a scalar SELECT. Generate code to put the |
| 1368 | ** value of this select in a memory cell and record the number |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 1369 | ** of the memory cell in iColumn. |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1370 | */ |
drh | 2646da7 | 2005-12-09 20:02:05 +0000 | [diff] [blame] | 1371 | static const Token one = { (u8*)"1", 0, 1 }; |
drh | 51522cd | 2005-01-20 13:36:19 +0000 | [diff] [blame] | 1372 | Select *pSel; |
danielk1977 | 6c8c8ce | 2008-01-02 16:27:09 +0000 | [diff] [blame] | 1373 | SelectDest dest; |
drh | 1398ad3 | 2005-01-19 23:24:50 +0000 | [diff] [blame] | 1374 | |
drh | 51522cd | 2005-01-20 13:36:19 +0000 | [diff] [blame] | 1375 | pSel = pExpr->pSelect; |
drh | 1013c93 | 2008-01-06 00:25:21 +0000 | [diff] [blame] | 1376 | sqlite3SelectDestInit(&dest, 0, ++pParse->nMem); |
drh | 51522cd | 2005-01-20 13:36:19 +0000 | [diff] [blame] | 1377 | if( pExpr->op==TK_SELECT ){ |
danielk1977 | 6c8c8ce | 2008-01-02 16:27:09 +0000 | [diff] [blame] | 1378 | dest.eDest = SRT_Mem; |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1379 | sqlite3VdbeAddOp2(v, OP_Null, 0, dest.iParm); |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1380 | VdbeComment((v, "Init subquery result")); |
drh | 51522cd | 2005-01-20 13:36:19 +0000 | [diff] [blame] | 1381 | }else{ |
danielk1977 | 6c8c8ce | 2008-01-02 16:27:09 +0000 | [diff] [blame] | 1382 | dest.eDest = SRT_Exists; |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1383 | sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iParm); |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 1384 | VdbeComment((v, "Init EXISTS result")); |
drh | 51522cd | 2005-01-20 13:36:19 +0000 | [diff] [blame] | 1385 | } |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 1386 | sqlite3ExprDelete(pParse->db, pSel->pLimit); |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 1387 | pSel->pLimit = sqlite3PExpr(pParse, TK_INTEGER, 0, 0, &one); |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 1388 | if( sqlite3Select(pParse, pSel, &dest) ){ |
drh | 94ccde5 | 2007-04-13 16:06:32 +0000 | [diff] [blame] | 1389 | return; |
| 1390 | } |
danielk1977 | 6c8c8ce | 2008-01-02 16:27:09 +0000 | [diff] [blame] | 1391 | pExpr->iColumn = dest.iParm; |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 1392 | break; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1393 | } |
| 1394 | } |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 1395 | |
drh | 57dbd7b | 2005-07-08 18:25:26 +0000 | [diff] [blame] | 1396 | if( testAddr ){ |
drh | 892d317 | 2008-01-10 03:46:36 +0000 | [diff] [blame] | 1397 | sqlite3VdbeJumpHere(v, testAddr-1); |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 1398 | } |
danielk1977 | fc97606 | 2007-05-10 10:46:56 +0000 | [diff] [blame] | 1399 | |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 1400 | return; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1401 | } |
drh | 51522cd | 2005-01-20 13:36:19 +0000 | [diff] [blame] | 1402 | #endif /* SQLITE_OMIT_SUBQUERY */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1403 | |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1404 | /* |
drh | 598f134 | 2007-10-23 15:39:45 +0000 | [diff] [blame] | 1405 | ** Duplicate an 8-byte value |
| 1406 | */ |
| 1407 | static char *dup8bytes(Vdbe *v, const char *in){ |
| 1408 | char *out = sqlite3DbMallocRaw(sqlite3VdbeDb(v), 8); |
| 1409 | if( out ){ |
| 1410 | memcpy(out, in, 8); |
| 1411 | } |
| 1412 | return out; |
| 1413 | } |
| 1414 | |
| 1415 | /* |
| 1416 | ** Generate an instruction that will put the floating point |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1417 | ** value described by z[0..n-1] into register iMem. |
drh | 0cf19ed | 2007-10-23 18:55:48 +0000 | [diff] [blame] | 1418 | ** |
| 1419 | ** The z[] string will probably not be zero-terminated. But the |
| 1420 | ** z[n] character is guaranteed to be something that does not look |
| 1421 | ** like the continuation of the number. |
drh | 598f134 | 2007-10-23 15:39:45 +0000 | [diff] [blame] | 1422 | */ |
drh | 9de221d | 2008-01-05 06:51:30 +0000 | [diff] [blame] | 1423 | static void codeReal(Vdbe *v, const char *z, int n, int negateFlag, int iMem){ |
drh | 598f134 | 2007-10-23 15:39:45 +0000 | [diff] [blame] | 1424 | assert( z || v==0 || sqlite3VdbeDb(v)->mallocFailed ); |
| 1425 | if( z ){ |
| 1426 | double value; |
| 1427 | char *zV; |
drh | 0cf19ed | 2007-10-23 18:55:48 +0000 | [diff] [blame] | 1428 | assert( !isdigit(z[n]) ); |
drh | 598f134 | 2007-10-23 15:39:45 +0000 | [diff] [blame] | 1429 | sqlite3AtoF(z, &value); |
drh | 2eaf93d | 2008-04-29 00:15:20 +0000 | [diff] [blame] | 1430 | if( sqlite3IsNaN(value) ){ |
| 1431 | sqlite3VdbeAddOp2(v, OP_Null, 0, iMem); |
| 1432 | }else{ |
| 1433 | if( negateFlag ) value = -value; |
| 1434 | zV = dup8bytes(v, (char*)&value); |
| 1435 | sqlite3VdbeAddOp4(v, OP_Real, 0, iMem, 0, zV, P4_REAL); |
| 1436 | } |
drh | 598f134 | 2007-10-23 15:39:45 +0000 | [diff] [blame] | 1437 | } |
| 1438 | } |
| 1439 | |
| 1440 | |
| 1441 | /* |
drh | fec19aa | 2004-05-19 20:41:03 +0000 | [diff] [blame] | 1442 | ** Generate an instruction that will put the integer describe by |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1443 | ** text z[0..n-1] into register iMem. |
drh | 0cf19ed | 2007-10-23 18:55:48 +0000 | [diff] [blame] | 1444 | ** |
| 1445 | ** The z[] string will probably not be zero-terminated. But the |
| 1446 | ** z[n] character is guaranteed to be something that does not look |
| 1447 | ** like the continuation of the number. |
drh | fec19aa | 2004-05-19 20:41:03 +0000 | [diff] [blame] | 1448 | */ |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 1449 | static void codeInteger(Vdbe *v, Expr *pExpr, int negFlag, int iMem){ |
| 1450 | const char *z; |
| 1451 | if( pExpr->flags & EP_IntValue ){ |
| 1452 | int i = pExpr->iTable; |
| 1453 | if( negFlag ) i = -i; |
| 1454 | sqlite3VdbeAddOp2(v, OP_Integer, i, iMem); |
| 1455 | }else if( (z = (char*)pExpr->token.z)!=0 ){ |
danielk1977 | c9cf901 | 2007-05-30 10:36:47 +0000 | [diff] [blame] | 1456 | int i; |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 1457 | int n = pExpr->token.n; |
drh | 0cf19ed | 2007-10-23 18:55:48 +0000 | [diff] [blame] | 1458 | assert( !isdigit(z[n]) ); |
danielk1977 | c9cf901 | 2007-05-30 10:36:47 +0000 | [diff] [blame] | 1459 | if( sqlite3GetInt32(z, &i) ){ |
drh | 9de221d | 2008-01-05 06:51:30 +0000 | [diff] [blame] | 1460 | if( negFlag ) i = -i; |
| 1461 | sqlite3VdbeAddOp2(v, OP_Integer, i, iMem); |
| 1462 | }else if( sqlite3FitsIn64Bits(z, negFlag) ){ |
drh | 598f134 | 2007-10-23 15:39:45 +0000 | [diff] [blame] | 1463 | i64 value; |
| 1464 | char *zV; |
| 1465 | sqlite3Atoi64(z, &value); |
drh | 9de221d | 2008-01-05 06:51:30 +0000 | [diff] [blame] | 1466 | if( negFlag ) value = -value; |
drh | 598f134 | 2007-10-23 15:39:45 +0000 | [diff] [blame] | 1467 | zV = dup8bytes(v, (char*)&value); |
drh | 9de221d | 2008-01-05 06:51:30 +0000 | [diff] [blame] | 1468 | sqlite3VdbeAddOp4(v, OP_Int64, 0, iMem, 0, zV, P4_INT64); |
danielk1977 | c9cf901 | 2007-05-30 10:36:47 +0000 | [diff] [blame] | 1469 | }else{ |
drh | 9de221d | 2008-01-05 06:51:30 +0000 | [diff] [blame] | 1470 | codeReal(v, z, n, negFlag, iMem); |
danielk1977 | c9cf901 | 2007-05-30 10:36:47 +0000 | [diff] [blame] | 1471 | } |
drh | fec19aa | 2004-05-19 20:41:03 +0000 | [diff] [blame] | 1472 | } |
| 1473 | } |
| 1474 | |
drh | 945498f | 2007-02-24 11:52:52 +0000 | [diff] [blame] | 1475 | |
| 1476 | /* |
| 1477 | ** Generate code that will extract the iColumn-th column from |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 1478 | ** table pTab and store the column value in a register. An effort |
| 1479 | ** is made to store the column value in register iReg, but this is |
| 1480 | ** not guaranteed. The location of the column value is returned. |
| 1481 | ** |
| 1482 | ** There must be an open cursor to pTab in iTable when this routine |
| 1483 | ** is called. If iColumn<0 then code is generated that extracts the rowid. |
drh | da250ea | 2008-04-01 05:07:14 +0000 | [diff] [blame] | 1484 | ** |
| 1485 | ** This routine might attempt to reuse the value of the column that |
| 1486 | ** has already been loaded into a register. The value will always |
| 1487 | ** be used if it has not undergone any affinity changes. But if |
| 1488 | ** an affinity change has occurred, then the cached value will only be |
| 1489 | ** used if allowAffChng is true. |
drh | 945498f | 2007-02-24 11:52:52 +0000 | [diff] [blame] | 1490 | */ |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 1491 | int sqlite3ExprCodeGetColumn( |
| 1492 | Parse *pParse, /* Parsing and code generating context */ |
drh | 2133d82 | 2008-01-03 18:44:59 +0000 | [diff] [blame] | 1493 | Table *pTab, /* Description of the table we are reading from */ |
| 1494 | int iColumn, /* Index of the table column */ |
| 1495 | int iTable, /* The cursor pointing to the table */ |
drh | da250ea | 2008-04-01 05:07:14 +0000 | [diff] [blame] | 1496 | int iReg, /* Store results here */ |
| 1497 | int allowAffChng /* True if prior affinity changes are OK */ |
drh | 2133d82 | 2008-01-03 18:44:59 +0000 | [diff] [blame] | 1498 | ){ |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 1499 | Vdbe *v = pParse->pVdbe; |
| 1500 | int i; |
drh | da250ea | 2008-04-01 05:07:14 +0000 | [diff] [blame] | 1501 | struct yColCache *p; |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 1502 | |
drh | da250ea | 2008-04-01 05:07:14 +0000 | [diff] [blame] | 1503 | for(i=0, p=pParse->aColCache; i<pParse->nColCache; i++, p++){ |
| 1504 | if( p->iTable==iTable && p->iColumn==iColumn |
| 1505 | && (!p->affChange || allowAffChng) ){ |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 1506 | #if 0 |
| 1507 | sqlite3VdbeAddOp0(v, OP_Noop); |
drh | da250ea | 2008-04-01 05:07:14 +0000 | [diff] [blame] | 1508 | VdbeComment((v, "OPT: tab%d.col%d -> r%d", iTable, iColumn, p->iReg)); |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 1509 | #endif |
drh | da250ea | 2008-04-01 05:07:14 +0000 | [diff] [blame] | 1510 | return p->iReg; |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 1511 | } |
| 1512 | } |
| 1513 | assert( v!=0 ); |
drh | 945498f | 2007-02-24 11:52:52 +0000 | [diff] [blame] | 1514 | if( iColumn<0 ){ |
| 1515 | int op = (pTab && IsVirtual(pTab)) ? OP_VRowid : OP_Rowid; |
drh | 2133d82 | 2008-01-03 18:44:59 +0000 | [diff] [blame] | 1516 | sqlite3VdbeAddOp2(v, op, iTable, iReg); |
drh | 945498f | 2007-02-24 11:52:52 +0000 | [diff] [blame] | 1517 | }else if( pTab==0 ){ |
drh | 2133d82 | 2008-01-03 18:44:59 +0000 | [diff] [blame] | 1518 | sqlite3VdbeAddOp3(v, OP_Column, iTable, iColumn, iReg); |
drh | 945498f | 2007-02-24 11:52:52 +0000 | [diff] [blame] | 1519 | }else{ |
| 1520 | int op = IsVirtual(pTab) ? OP_VColumn : OP_Column; |
drh | 2133d82 | 2008-01-03 18:44:59 +0000 | [diff] [blame] | 1521 | sqlite3VdbeAddOp3(v, op, iTable, iColumn, iReg); |
drh | 945498f | 2007-02-24 11:52:52 +0000 | [diff] [blame] | 1522 | sqlite3ColumnDefault(v, pTab, iColumn); |
| 1523 | #ifndef SQLITE_OMIT_FLOATING_POINT |
| 1524 | if( pTab->aCol[iColumn].affinity==SQLITE_AFF_REAL ){ |
drh | 2133d82 | 2008-01-03 18:44:59 +0000 | [diff] [blame] | 1525 | sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg); |
drh | 945498f | 2007-02-24 11:52:52 +0000 | [diff] [blame] | 1526 | } |
| 1527 | #endif |
| 1528 | } |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 1529 | if( pParse->disableColCache==0 ){ |
| 1530 | i = pParse->iColCache; |
drh | da250ea | 2008-04-01 05:07:14 +0000 | [diff] [blame] | 1531 | p = &pParse->aColCache[i]; |
| 1532 | p->iTable = iTable; |
| 1533 | p->iColumn = iColumn; |
| 1534 | p->iReg = iReg; |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 1535 | p->affChange = 0; |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 1536 | i++; |
drh | 2f7794c | 2008-04-01 03:27:39 +0000 | [diff] [blame] | 1537 | if( i>=ArraySize(pParse->aColCache) ) i = 0; |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 1538 | if( i>pParse->nColCache ) pParse->nColCache = i; |
drh | 2f7794c | 2008-04-01 03:27:39 +0000 | [diff] [blame] | 1539 | pParse->iColCache = i; |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 1540 | } |
| 1541 | return iReg; |
| 1542 | } |
| 1543 | |
| 1544 | /* |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 1545 | ** Clear all column cache entries associated with the vdbe |
| 1546 | ** cursor with cursor number iTable. |
| 1547 | */ |
| 1548 | void sqlite3ExprClearColumnCache(Parse *pParse, int iTable){ |
| 1549 | if( iTable<0 ){ |
| 1550 | pParse->nColCache = 0; |
| 1551 | pParse->iColCache = 0; |
| 1552 | }else{ |
| 1553 | int i; |
| 1554 | for(i=0; i<pParse->nColCache; i++){ |
| 1555 | if( pParse->aColCache[i].iTable==iTable ){ |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 1556 | testcase( i==pParse->nColCache-1 ); |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 1557 | pParse->aColCache[i] = pParse->aColCache[--pParse->nColCache]; |
drh | da250ea | 2008-04-01 05:07:14 +0000 | [diff] [blame] | 1558 | pParse->iColCache = pParse->nColCache; |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 1559 | } |
| 1560 | } |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 1561 | } |
| 1562 | } |
| 1563 | |
| 1564 | /* |
drh | da250ea | 2008-04-01 05:07:14 +0000 | [diff] [blame] | 1565 | ** Record the fact that an affinity change has occurred on iCount |
| 1566 | ** registers starting with iStart. |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 1567 | */ |
drh | da250ea | 2008-04-01 05:07:14 +0000 | [diff] [blame] | 1568 | void sqlite3ExprCacheAffinityChange(Parse *pParse, int iStart, int iCount){ |
| 1569 | int iEnd = iStart + iCount - 1; |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 1570 | int i; |
| 1571 | for(i=0; i<pParse->nColCache; i++){ |
| 1572 | int r = pParse->aColCache[i].iReg; |
drh | da250ea | 2008-04-01 05:07:14 +0000 | [diff] [blame] | 1573 | if( r>=iStart && r<=iEnd ){ |
| 1574 | pParse->aColCache[i].affChange = 1; |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 1575 | } |
| 1576 | } |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 1577 | } |
| 1578 | |
| 1579 | /* |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 1580 | ** Generate code to move content from registers iFrom...iFrom+nReg-1 |
| 1581 | ** over to iTo..iTo+nReg-1. Keep the column cache up-to-date. |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 1582 | */ |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 1583 | void sqlite3ExprCodeMove(Parse *pParse, int iFrom, int iTo, int nReg){ |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 1584 | int i; |
| 1585 | if( iFrom==iTo ) return; |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 1586 | sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move, iFrom, iTo, nReg); |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 1587 | for(i=0; i<pParse->nColCache; i++){ |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 1588 | int x = pParse->aColCache[i].iReg; |
| 1589 | if( x>=iFrom && x<iFrom+nReg ){ |
| 1590 | pParse->aColCache[i].iReg += iTo-iFrom; |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 1591 | } |
| 1592 | } |
drh | 945498f | 2007-02-24 11:52:52 +0000 | [diff] [blame] | 1593 | } |
| 1594 | |
drh | fec19aa | 2004-05-19 20:41:03 +0000 | [diff] [blame] | 1595 | /* |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 1596 | ** Generate code to copy content from registers iFrom...iFrom+nReg-1 |
| 1597 | ** over to iTo..iTo+nReg-1. |
| 1598 | */ |
| 1599 | void sqlite3ExprCodeCopy(Parse *pParse, int iFrom, int iTo, int nReg){ |
| 1600 | int i; |
| 1601 | if( iFrom==iTo ) return; |
| 1602 | for(i=0; i<nReg; i++){ |
| 1603 | sqlite3VdbeAddOp2(pParse->pVdbe, OP_Copy, iFrom+i, iTo+i); |
| 1604 | } |
| 1605 | } |
| 1606 | |
| 1607 | /* |
drh | 652fbf5 | 2008-04-01 01:42:41 +0000 | [diff] [blame] | 1608 | ** Return true if any register in the range iFrom..iTo (inclusive) |
| 1609 | ** is used as part of the column cache. |
| 1610 | */ |
| 1611 | static int usedAsColumnCache(Parse *pParse, int iFrom, int iTo){ |
| 1612 | int i; |
| 1613 | for(i=0; i<pParse->nColCache; i++){ |
| 1614 | int r = pParse->aColCache[i].iReg; |
| 1615 | if( r>=iFrom && r<=iTo ) return 1; |
| 1616 | } |
| 1617 | return 0; |
| 1618 | } |
| 1619 | |
| 1620 | /* |
| 1621 | ** Theres is a value in register iCurrent. We ultimately want |
| 1622 | ** the value to be in register iTarget. It might be that |
| 1623 | ** iCurrent and iTarget are the same register. |
| 1624 | ** |
| 1625 | ** We are going to modify the value, so we need to make sure it |
| 1626 | ** is not a cached register. If iCurrent is a cached register, |
| 1627 | ** then try to move the value over to iTarget. If iTarget is a |
| 1628 | ** cached register, then clear the corresponding cache line. |
| 1629 | ** |
| 1630 | ** Return the register that the value ends up in. |
| 1631 | */ |
| 1632 | int sqlite3ExprWritableRegister(Parse *pParse, int iCurrent, int iTarget){ |
drh | da250ea | 2008-04-01 05:07:14 +0000 | [diff] [blame] | 1633 | int i; |
drh | 652fbf5 | 2008-04-01 01:42:41 +0000 | [diff] [blame] | 1634 | assert( pParse->pVdbe!=0 ); |
| 1635 | if( !usedAsColumnCache(pParse, iCurrent, iCurrent) ){ |
| 1636 | return iCurrent; |
| 1637 | } |
drh | 2f7794c | 2008-04-01 03:27:39 +0000 | [diff] [blame] | 1638 | if( iCurrent!=iTarget ){ |
| 1639 | sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, iCurrent, iTarget); |
| 1640 | } |
drh | da250ea | 2008-04-01 05:07:14 +0000 | [diff] [blame] | 1641 | for(i=0; i<pParse->nColCache; i++){ |
| 1642 | if( pParse->aColCache[i].iReg==iTarget ){ |
| 1643 | pParse->aColCache[i] = pParse->aColCache[--pParse->nColCache]; |
| 1644 | pParse->iColCache = pParse->nColCache; |
| 1645 | } |
| 1646 | } |
drh | 652fbf5 | 2008-04-01 01:42:41 +0000 | [diff] [blame] | 1647 | return iTarget; |
| 1648 | } |
| 1649 | |
| 1650 | /* |
drh | 191b54c | 2008-04-15 12:14:21 +0000 | [diff] [blame] | 1651 | ** If the last instruction coded is an ephemeral copy of any of |
| 1652 | ** the registers in the nReg registers beginning with iReg, then |
| 1653 | ** convert the last instruction from OP_SCopy to OP_Copy. |
| 1654 | */ |
| 1655 | void sqlite3ExprHardCopy(Parse *pParse, int iReg, int nReg){ |
| 1656 | int addr; |
| 1657 | VdbeOp *pOp; |
| 1658 | Vdbe *v; |
| 1659 | |
| 1660 | v = pParse->pVdbe; |
| 1661 | addr = sqlite3VdbeCurrentAddr(v); |
| 1662 | pOp = sqlite3VdbeGetOp(v, addr-1); |
danielk1977 | d7eb2ed | 2008-04-24 12:36:35 +0000 | [diff] [blame] | 1663 | assert( pOp || pParse->db->mallocFailed ); |
| 1664 | if( pOp && pOp->opcode==OP_SCopy && pOp->p1>=iReg && pOp->p1<iReg+nReg ){ |
drh | 191b54c | 2008-04-15 12:14:21 +0000 | [diff] [blame] | 1665 | pOp->opcode = OP_Copy; |
| 1666 | } |
| 1667 | } |
| 1668 | |
| 1669 | /* |
drh | 8b21389 | 2008-08-29 02:14:02 +0000 | [diff] [blame] | 1670 | ** Generate code to store the value of the iAlias-th alias in register |
| 1671 | ** target. The first time this is called, pExpr is evaluated to compute |
| 1672 | ** the value of the alias. The value is stored in an auxiliary register |
| 1673 | ** and the number of that register is returned. On subsequent calls, |
| 1674 | ** the register number is returned without generating any code. |
| 1675 | ** |
| 1676 | ** Note that in order for this to work, code must be generated in the |
| 1677 | ** same order that it is executed. |
| 1678 | ** |
| 1679 | ** Aliases are numbered starting with 1. So iAlias is in the range |
| 1680 | ** of 1 to pParse->nAlias inclusive. |
| 1681 | ** |
| 1682 | ** pParse->aAlias[iAlias-1] records the register number where the value |
| 1683 | ** of the iAlias-th alias is stored. If zero, that means that the |
| 1684 | ** alias has not yet been computed. |
| 1685 | */ |
| 1686 | static int codeAlias(Parse *pParse, int iAlias, Expr *pExpr){ |
| 1687 | sqlite3 *db = pParse->db; |
| 1688 | int iReg; |
| 1689 | if( pParse->aAlias==0 ){ |
| 1690 | pParse->aAlias = sqlite3DbMallocZero(db, |
| 1691 | sizeof(pParse->aAlias[0])*pParse->nAlias ); |
| 1692 | if( db->mallocFailed ) return 0; |
| 1693 | } |
| 1694 | assert( iAlias>0 && iAlias<=pParse->nAlias ); |
| 1695 | iReg = pParse->aAlias[iAlias-1]; |
| 1696 | if( iReg==0 ){ |
| 1697 | iReg = ++pParse->nMem; |
| 1698 | sqlite3ExprCode(pParse, pExpr, iReg); |
| 1699 | pParse->aAlias[iAlias-1] = iReg; |
| 1700 | } |
| 1701 | return iReg; |
| 1702 | } |
| 1703 | |
| 1704 | /* |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1705 | ** Generate code into the current Vdbe to evaluate the given |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 1706 | ** expression. Attempt to store the results in register "target". |
| 1707 | ** Return the register where results are stored. |
drh | 389a1ad | 2008-01-03 23:44:53 +0000 | [diff] [blame] | 1708 | ** |
drh | 8b21389 | 2008-08-29 02:14:02 +0000 | [diff] [blame] | 1709 | ** With this routine, there is no guarantee that results will |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 1710 | ** be stored in target. The result might be stored in some other |
| 1711 | ** register if it is convenient to do so. The calling function |
| 1712 | ** must check the return code and move the results to the desired |
| 1713 | ** register. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1714 | */ |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 1715 | int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){ |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 1716 | Vdbe *v = pParse->pVdbe; /* The VM under construction */ |
| 1717 | int op; /* The opcode being coded */ |
| 1718 | int inReg = target; /* Results stored in register inReg */ |
| 1719 | int regFree1 = 0; /* If non-zero free this temporary register */ |
| 1720 | int regFree2 = 0; /* If non-zero free this temporary register */ |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 1721 | int r1, r2, r3, r4; /* Various register numbers */ |
drh | 8b21389 | 2008-08-29 02:14:02 +0000 | [diff] [blame] | 1722 | sqlite3 *db; |
drh | ffe07b2 | 2005-11-03 00:41:17 +0000 | [diff] [blame] | 1723 | |
drh | 8b21389 | 2008-08-29 02:14:02 +0000 | [diff] [blame] | 1724 | db = pParse->db; |
| 1725 | assert( v!=0 || db->mallocFailed ); |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1726 | assert( target>0 && target<=pParse->nMem ); |
drh | 389a1ad | 2008-01-03 23:44:53 +0000 | [diff] [blame] | 1727 | if( v==0 ) return 0; |
drh | 389a1ad | 2008-01-03 23:44:53 +0000 | [diff] [blame] | 1728 | |
| 1729 | if( pExpr==0 ){ |
| 1730 | op = TK_NULL; |
| 1731 | }else{ |
| 1732 | op = pExpr->op; |
| 1733 | } |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 1734 | switch( op ){ |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 1735 | case TK_AGG_COLUMN: { |
| 1736 | AggInfo *pAggInfo = pExpr->pAggInfo; |
| 1737 | struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg]; |
| 1738 | if( !pAggInfo->directMode ){ |
drh | 9de221d | 2008-01-05 06:51:30 +0000 | [diff] [blame] | 1739 | assert( pCol->iMem>0 ); |
| 1740 | inReg = pCol->iMem; |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 1741 | break; |
| 1742 | }else if( pAggInfo->useSortingIdx ){ |
drh | 389a1ad | 2008-01-03 23:44:53 +0000 | [diff] [blame] | 1743 | sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdx, |
| 1744 | pCol->iSorterColumn, target); |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 1745 | break; |
| 1746 | } |
| 1747 | /* Otherwise, fall thru into the TK_COLUMN case */ |
| 1748 | } |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 1749 | case TK_COLUMN: { |
drh | ffe07b2 | 2005-11-03 00:41:17 +0000 | [diff] [blame] | 1750 | if( pExpr->iTable<0 ){ |
| 1751 | /* This only happens when coding check constraints */ |
drh | aa9b896 | 2008-01-08 02:57:55 +0000 | [diff] [blame] | 1752 | assert( pParse->ckBase>0 ); |
| 1753 | inReg = pExpr->iColumn + pParse->ckBase; |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1754 | }else{ |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 1755 | testcase( (pExpr->flags & EP_AnyAff)!=0 ); |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 1756 | inReg = sqlite3ExprCodeGetColumn(pParse, pExpr->pTab, |
drh | da250ea | 2008-04-01 05:07:14 +0000 | [diff] [blame] | 1757 | pExpr->iColumn, pExpr->iTable, target, |
| 1758 | pExpr->flags & EP_AnyAff); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1759 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1760 | break; |
| 1761 | } |
| 1762 | case TK_INTEGER: { |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 1763 | codeInteger(v, pExpr, 0, target); |
drh | fec19aa | 2004-05-19 20:41:03 +0000 | [diff] [blame] | 1764 | break; |
| 1765 | } |
drh | 598f134 | 2007-10-23 15:39:45 +0000 | [diff] [blame] | 1766 | case TK_FLOAT: { |
drh | 9de221d | 2008-01-05 06:51:30 +0000 | [diff] [blame] | 1767 | codeReal(v, (char*)pExpr->token.z, pExpr->token.n, 0, target); |
drh | 598f134 | 2007-10-23 15:39:45 +0000 | [diff] [blame] | 1768 | break; |
| 1769 | } |
drh | fec19aa | 2004-05-19 20:41:03 +0000 | [diff] [blame] | 1770 | case TK_STRING: { |
drh | 8b21389 | 2008-08-29 02:14:02 +0000 | [diff] [blame] | 1771 | sqlite3DequoteExpr(db, pExpr); |
drh | 9de221d | 2008-01-05 06:51:30 +0000 | [diff] [blame] | 1772 | sqlite3VdbeAddOp4(v,OP_String8, 0, target, 0, |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1773 | (char*)pExpr->token.z, pExpr->token.n); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1774 | break; |
| 1775 | } |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 1776 | case TK_NULL: { |
drh | 9de221d | 2008-01-05 06:51:30 +0000 | [diff] [blame] | 1777 | sqlite3VdbeAddOp2(v, OP_Null, 0, target); |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 1778 | break; |
| 1779 | } |
danielk1977 | 5338a5f | 2005-01-20 13:03:10 +0000 | [diff] [blame] | 1780 | #ifndef SQLITE_OMIT_BLOB_LITERAL |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 1781 | case TK_BLOB: { |
drh | 6c8c6ce | 2005-08-23 11:17:58 +0000 | [diff] [blame] | 1782 | int n; |
| 1783 | const char *z; |
drh | ca48c90 | 2008-01-18 14:08:24 +0000 | [diff] [blame] | 1784 | char *zBlob; |
| 1785 | assert( pExpr->token.n>=3 ); |
| 1786 | assert( pExpr->token.z[0]=='x' || pExpr->token.z[0]=='X' ); |
| 1787 | assert( pExpr->token.z[1]=='\'' ); |
| 1788 | assert( pExpr->token.z[pExpr->token.n-1]=='\'' ); |
drh | 6c8c6ce | 2005-08-23 11:17:58 +0000 | [diff] [blame] | 1789 | n = pExpr->token.n - 3; |
drh | 2646da7 | 2005-12-09 20:02:05 +0000 | [diff] [blame] | 1790 | z = (char*)pExpr->token.z + 2; |
drh | ca48c90 | 2008-01-18 14:08:24 +0000 | [diff] [blame] | 1791 | zBlob = sqlite3HexToBlob(sqlite3VdbeDb(v), z, n); |
| 1792 | sqlite3VdbeAddOp4(v, OP_Blob, n/2, target, 0, zBlob, P4_DYNAMIC); |
danielk1977 | c572ef7 | 2004-05-27 09:28:41 +0000 | [diff] [blame] | 1793 | break; |
| 1794 | } |
danielk1977 | 5338a5f | 2005-01-20 13:03:10 +0000 | [diff] [blame] | 1795 | #endif |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 1796 | case TK_VARIABLE: { |
drh | 9de221d | 2008-01-05 06:51:30 +0000 | [diff] [blame] | 1797 | sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iTable, target); |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 1798 | if( pExpr->token.n>1 ){ |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 1799 | sqlite3VdbeChangeP4(v, -1, (char*)pExpr->token.z, pExpr->token.n); |
drh | 895d747 | 2004-08-20 16:02:39 +0000 | [diff] [blame] | 1800 | } |
drh | 5045789 | 2003-09-06 01:10:47 +0000 | [diff] [blame] | 1801 | break; |
| 1802 | } |
drh | 4e0cff6 | 2004-11-05 05:10:28 +0000 | [diff] [blame] | 1803 | case TK_REGISTER: { |
drh | 9de221d | 2008-01-05 06:51:30 +0000 | [diff] [blame] | 1804 | inReg = pExpr->iTable; |
drh | 4e0cff6 | 2004-11-05 05:10:28 +0000 | [diff] [blame] | 1805 | break; |
| 1806 | } |
drh | 8b21389 | 2008-08-29 02:14:02 +0000 | [diff] [blame] | 1807 | case TK_AS: { |
| 1808 | inReg = codeAlias(pParse, pExpr->iTable, pExpr->pLeft); |
| 1809 | break; |
| 1810 | } |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1811 | #ifndef SQLITE_OMIT_CAST |
| 1812 | case TK_CAST: { |
| 1813 | /* Expressions of the form: CAST(pLeft AS token) */ |
danielk1977 | f011300 | 2006-01-24 12:09:17 +0000 | [diff] [blame] | 1814 | int aff, to_op; |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 1815 | inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target); |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 1816 | aff = sqlite3AffinityType(&pExpr->token); |
danielk1977 | f011300 | 2006-01-24 12:09:17 +0000 | [diff] [blame] | 1817 | to_op = aff - SQLITE_AFF_TEXT + OP_ToText; |
| 1818 | assert( to_op==OP_ToText || aff!=SQLITE_AFF_TEXT ); |
| 1819 | assert( to_op==OP_ToBlob || aff!=SQLITE_AFF_NONE ); |
| 1820 | assert( to_op==OP_ToNumeric || aff!=SQLITE_AFF_NUMERIC ); |
| 1821 | assert( to_op==OP_ToInt || aff!=SQLITE_AFF_INTEGER ); |
| 1822 | assert( to_op==OP_ToReal || aff!=SQLITE_AFF_REAL ); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 1823 | testcase( to_op==OP_ToText ); |
| 1824 | testcase( to_op==OP_ToBlob ); |
| 1825 | testcase( to_op==OP_ToNumeric ); |
| 1826 | testcase( to_op==OP_ToInt ); |
| 1827 | testcase( to_op==OP_ToReal ); |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 1828 | sqlite3VdbeAddOp1(v, to_op, inReg); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 1829 | testcase( usedAsColumnCache(pParse, inReg, inReg) ); |
drh | b3843a8 | 2008-04-01 12:24:11 +0000 | [diff] [blame] | 1830 | sqlite3ExprCacheAffinityChange(pParse, inReg, 1); |
drh | 487e262 | 2005-06-25 18:42:14 +0000 | [diff] [blame] | 1831 | break; |
| 1832 | } |
| 1833 | #endif /* SQLITE_OMIT_CAST */ |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 1834 | case TK_LT: |
| 1835 | case TK_LE: |
| 1836 | case TK_GT: |
| 1837 | case TK_GE: |
| 1838 | case TK_NE: |
| 1839 | case TK_EQ: { |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 1840 | assert( TK_LT==OP_Lt ); |
| 1841 | assert( TK_LE==OP_Le ); |
| 1842 | assert( TK_GT==OP_Gt ); |
| 1843 | assert( TK_GE==OP_Ge ); |
| 1844 | assert( TK_EQ==OP_Eq ); |
| 1845 | assert( TK_NE==OP_Ne ); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 1846 | testcase( op==TK_LT ); |
| 1847 | testcase( op==TK_LE ); |
| 1848 | testcase( op==TK_GT ); |
| 1849 | testcase( op==TK_GE ); |
| 1850 | testcase( op==TK_EQ ); |
| 1851 | testcase( op==TK_NE ); |
drh | da250ea | 2008-04-01 05:07:14 +0000 | [diff] [blame] | 1852 | codeCompareOperands(pParse, pExpr->pLeft, &r1, ®Free1, |
| 1853 | pExpr->pRight, &r2, ®Free2); |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 1854 | codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, |
| 1855 | r1, r2, inReg, SQLITE_STOREP2); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 1856 | testcase( regFree1==0 ); |
| 1857 | testcase( regFree2==0 ); |
danielk1977 | a37cdde | 2004-05-16 11:15:36 +0000 | [diff] [blame] | 1858 | break; |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 1859 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1860 | case TK_AND: |
| 1861 | case TK_OR: |
| 1862 | case TK_PLUS: |
| 1863 | case TK_STAR: |
| 1864 | case TK_MINUS: |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1865 | case TK_REM: |
| 1866 | case TK_BITAND: |
| 1867 | case TK_BITOR: |
drh | 17c4029 | 2004-07-21 02:53:29 +0000 | [diff] [blame] | 1868 | case TK_SLASH: |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1869 | case TK_LSHIFT: |
drh | 855eb1c | 2004-08-31 13:45:11 +0000 | [diff] [blame] | 1870 | case TK_RSHIFT: |
drh | 0040077 | 2000-06-16 20:51:26 +0000 | [diff] [blame] | 1871 | case TK_CONCAT: { |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 1872 | assert( TK_AND==OP_And ); |
| 1873 | assert( TK_OR==OP_Or ); |
| 1874 | assert( TK_PLUS==OP_Add ); |
| 1875 | assert( TK_MINUS==OP_Subtract ); |
| 1876 | assert( TK_REM==OP_Remainder ); |
| 1877 | assert( TK_BITAND==OP_BitAnd ); |
| 1878 | assert( TK_BITOR==OP_BitOr ); |
| 1879 | assert( TK_SLASH==OP_Divide ); |
| 1880 | assert( TK_LSHIFT==OP_ShiftLeft ); |
| 1881 | assert( TK_RSHIFT==OP_ShiftRight ); |
| 1882 | assert( TK_CONCAT==OP_Concat ); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 1883 | testcase( op==TK_AND ); |
| 1884 | testcase( op==TK_OR ); |
| 1885 | testcase( op==TK_PLUS ); |
| 1886 | testcase( op==TK_MINUS ); |
| 1887 | testcase( op==TK_REM ); |
| 1888 | testcase( op==TK_BITAND ); |
| 1889 | testcase( op==TK_BITOR ); |
| 1890 | testcase( op==TK_SLASH ); |
| 1891 | testcase( op==TK_LSHIFT ); |
| 1892 | testcase( op==TK_RSHIFT ); |
| 1893 | testcase( op==TK_CONCAT ); |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 1894 | r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); |
| 1895 | r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); |
drh | 5b6afba | 2008-01-05 16:29:28 +0000 | [diff] [blame] | 1896 | sqlite3VdbeAddOp3(v, op, r2, r1, target); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 1897 | testcase( regFree1==0 ); |
| 1898 | testcase( regFree2==0 ); |
drh | 0040077 | 2000-06-16 20:51:26 +0000 | [diff] [blame] | 1899 | break; |
| 1900 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1901 | case TK_UMINUS: { |
drh | fec19aa | 2004-05-19 20:41:03 +0000 | [diff] [blame] | 1902 | Expr *pLeft = pExpr->pLeft; |
| 1903 | assert( pLeft ); |
| 1904 | if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){ |
drh | fec19aa | 2004-05-19 20:41:03 +0000 | [diff] [blame] | 1905 | if( pLeft->op==TK_FLOAT ){ |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 1906 | codeReal(v, (char*)pLeft->token.z, pLeft->token.n, 1, target); |
drh | e684090 | 2002-03-06 03:08:25 +0000 | [diff] [blame] | 1907 | }else{ |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 1908 | codeInteger(v, pLeft, 1, target); |
drh | e684090 | 2002-03-06 03:08:25 +0000 | [diff] [blame] | 1909 | } |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1910 | }else{ |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 1911 | regFree1 = r1 = sqlite3GetTempReg(pParse); |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1912 | sqlite3VdbeAddOp2(v, OP_Integer, 0, r1); |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 1913 | r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free2); |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 1914 | sqlite3VdbeAddOp3(v, OP_Subtract, r2, r1, target); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 1915 | testcase( regFree2==0 ); |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 1916 | } |
drh | 3c84ddf | 2008-01-09 02:15:38 +0000 | [diff] [blame] | 1917 | inReg = target; |
| 1918 | break; |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 1919 | } |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 1920 | case TK_BITNOT: |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 1921 | case TK_NOT: { |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 1922 | assert( TK_BITNOT==OP_BitNot ); |
| 1923 | assert( TK_NOT==OP_Not ); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 1924 | testcase( op==TK_BITNOT ); |
| 1925 | testcase( op==TK_NOT ); |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 1926 | inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 1927 | testcase( inReg==target ); |
| 1928 | testcase( usedAsColumnCache(pParse, inReg, inReg) ); |
drh | 652fbf5 | 2008-04-01 01:42:41 +0000 | [diff] [blame] | 1929 | inReg = sqlite3ExprWritableRegister(pParse, inReg, target); |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 1930 | sqlite3VdbeAddOp1(v, op, inReg); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1931 | break; |
| 1932 | } |
| 1933 | case TK_ISNULL: |
| 1934 | case TK_NOTNULL: { |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 1935 | int addr; |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 1936 | assert( TK_ISNULL==OP_IsNull ); |
| 1937 | assert( TK_NOTNULL==OP_NotNull ); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 1938 | testcase( op==TK_ISNULL ); |
| 1939 | testcase( op==TK_NOTNULL ); |
drh | 9de221d | 2008-01-05 06:51:30 +0000 | [diff] [blame] | 1940 | sqlite3VdbeAddOp2(v, OP_Integer, 1, target); |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 1941 | r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 1942 | testcase( regFree1==0 ); |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 1943 | addr = sqlite3VdbeAddOp1(v, op, r1); |
drh | 9de221d | 2008-01-05 06:51:30 +0000 | [diff] [blame] | 1944 | sqlite3VdbeAddOp2(v, OP_AddImm, target, -1); |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 1945 | sqlite3VdbeJumpHere(v, addr); |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 1946 | break; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1947 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1948 | case TK_AGG_FUNCTION: { |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 1949 | AggInfo *pInfo = pExpr->pAggInfo; |
drh | 7e56e71 | 2005-11-16 12:53:15 +0000 | [diff] [blame] | 1950 | if( pInfo==0 ){ |
| 1951 | sqlite3ErrorMsg(pParse, "misuse of aggregate: %T", |
| 1952 | &pExpr->span); |
| 1953 | }else{ |
drh | 9de221d | 2008-01-05 06:51:30 +0000 | [diff] [blame] | 1954 | inReg = pInfo->aFunc[pExpr->iAgg].iMem; |
drh | 7e56e71 | 2005-11-16 12:53:15 +0000 | [diff] [blame] | 1955 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1956 | break; |
| 1957 | } |
drh | b71090f | 2005-05-23 17:26:51 +0000 | [diff] [blame] | 1958 | case TK_CONST_FUNC: |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1959 | case TK_FUNCTION: { |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1960 | ExprList *pList = pExpr->pList; |
drh | 89425d5 | 2002-02-28 03:04:48 +0000 | [diff] [blame] | 1961 | int nExpr = pList ? pList->nExpr : 0; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1962 | FuncDef *pDef; |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 1963 | int nId; |
| 1964 | const char *zId; |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 1965 | int constMask = 0; |
danielk1977 | 682f68b | 2004-06-05 10:22:17 +0000 | [diff] [blame] | 1966 | int i; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1967 | u8 enc = ENC(db); |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1968 | CollSeq *pColl = 0; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1969 | |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 1970 | testcase( op==TK_CONST_FUNC ); |
| 1971 | testcase( op==TK_FUNCTION ); |
drh | 2646da7 | 2005-12-09 20:02:05 +0000 | [diff] [blame] | 1972 | zId = (char*)pExpr->token.z; |
drh | b71090f | 2005-05-23 17:26:51 +0000 | [diff] [blame] | 1973 | nId = pExpr->token.n; |
drh | 8b21389 | 2008-08-29 02:14:02 +0000 | [diff] [blame] | 1974 | pDef = sqlite3FindFunction(db, zId, nId, nExpr, enc, 0); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1975 | assert( pDef!=0 ); |
drh | 892d317 | 2008-01-10 03:46:36 +0000 | [diff] [blame] | 1976 | if( pList ){ |
| 1977 | nExpr = pList->nExpr; |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 1978 | r1 = sqlite3GetTempRange(pParse, nExpr); |
drh | 191b54c | 2008-04-15 12:14:21 +0000 | [diff] [blame] | 1979 | sqlite3ExprCodeExprList(pParse, pList, r1, 1); |
drh | 892d317 | 2008-01-10 03:46:36 +0000 | [diff] [blame] | 1980 | }else{ |
drh | d847eaa | 2008-01-17 17:15:56 +0000 | [diff] [blame] | 1981 | nExpr = r1 = 0; |
drh | 892d317 | 2008-01-10 03:46:36 +0000 | [diff] [blame] | 1982 | } |
drh | b7f6f68 | 2006-07-08 17:06:43 +0000 | [diff] [blame] | 1983 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
drh | a43fa22 | 2006-07-08 18:41:37 +0000 | [diff] [blame] | 1984 | /* Possibly overload the function if the first argument is |
| 1985 | ** a virtual table column. |
| 1986 | ** |
| 1987 | ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the |
| 1988 | ** second argument, not the first, as the argument to test to |
| 1989 | ** see if it is a column in a virtual table. This is done because |
| 1990 | ** the left operand of infix functions (the operand we want to |
| 1991 | ** control overloading) ends up as the second argument to the |
| 1992 | ** function. The expression "A glob B" is equivalent to |
| 1993 | ** "glob(B,A). We want to use the A in "A glob B" to test |
| 1994 | ** for function overloading. But we use the B term in "glob(B,A)". |
| 1995 | */ |
drh | 6a03a1c | 2006-07-08 18:34:59 +0000 | [diff] [blame] | 1996 | if( nExpr>=2 && (pExpr->flags & EP_InfixFunc) ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1997 | pDef = sqlite3VtabOverloadFunction(db, pDef, nExpr, pList->a[1].pExpr); |
drh | 6a03a1c | 2006-07-08 18:34:59 +0000 | [diff] [blame] | 1998 | }else if( nExpr>0 ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1999 | pDef = sqlite3VtabOverloadFunction(db, pDef, nExpr, pList->a[0].pExpr); |
drh | b7f6f68 | 2006-07-08 17:06:43 +0000 | [diff] [blame] | 2000 | } |
| 2001 | #endif |
danielk1977 | 682f68b | 2004-06-05 10:22:17 +0000 | [diff] [blame] | 2002 | for(i=0; i<nExpr && i<32; i++){ |
danielk1977 | d02eb1f | 2004-06-06 09:44:03 +0000 | [diff] [blame] | 2003 | if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){ |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 2004 | constMask |= (1<<i); |
danielk1977 | d02eb1f | 2004-06-06 09:44:03 +0000 | [diff] [blame] | 2005 | } |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 2006 | if( pDef->needCollSeq && !pColl ){ |
| 2007 | pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr); |
| 2008 | } |
| 2009 | } |
| 2010 | if( pDef->needCollSeq ){ |
drh | 8b21389 | 2008-08-29 02:14:02 +0000 | [diff] [blame] | 2011 | if( !pColl ) pColl = db->pDfltColl; |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 2012 | sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ); |
danielk1977 | 682f68b | 2004-06-05 10:22:17 +0000 | [diff] [blame] | 2013 | } |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2014 | sqlite3VdbeAddOp4(v, OP_Function, constMask, r1, target, |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 2015 | (char*)pDef, P4_FUNCDEF); |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 2016 | sqlite3VdbeChangeP5(v, nExpr); |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2017 | if( nExpr ){ |
| 2018 | sqlite3ReleaseTempRange(pParse, r1, nExpr); |
| 2019 | } |
drh | da250ea | 2008-04-01 05:07:14 +0000 | [diff] [blame] | 2020 | sqlite3ExprCacheAffinityChange(pParse, r1, nExpr); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 2021 | break; |
| 2022 | } |
drh | fe2093d | 2005-01-20 22:48:47 +0000 | [diff] [blame] | 2023 | #ifndef SQLITE_OMIT_SUBQUERY |
| 2024 | case TK_EXISTS: |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 2025 | case TK_SELECT: { |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 2026 | testcase( op==TK_EXISTS ); |
| 2027 | testcase( op==TK_SELECT ); |
drh | 41714d6 | 2006-03-02 04:44:23 +0000 | [diff] [blame] | 2028 | if( pExpr->iColumn==0 ){ |
danielk1977 | 0cdc022 | 2008-06-26 18:04:03 +0000 | [diff] [blame] | 2029 | sqlite3CodeSubselect(pParse, pExpr, 0); |
drh | 41714d6 | 2006-03-02 04:44:23 +0000 | [diff] [blame] | 2030 | } |
drh | 9de221d | 2008-01-05 06:51:30 +0000 | [diff] [blame] | 2031 | inReg = pExpr->iColumn; |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 2032 | break; |
| 2033 | } |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 2034 | case TK_IN: { |
danielk1977 | 0cdc022 | 2008-06-26 18:04:03 +0000 | [diff] [blame] | 2035 | int rNotFound = 0; |
| 2036 | int rMayHaveNull = 0; |
drh | 6fccc35 | 2008-06-27 00:52:45 +0000 | [diff] [blame] | 2037 | int j2, j3, j4, j5; |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 2038 | char affinity; |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 2039 | int eType; |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 2040 | |
drh | 3c31fc2 | 2008-06-26 21:45:26 +0000 | [diff] [blame] | 2041 | VdbeNoopComment((v, "begin IN expr r%d", target)); |
danielk1977 | 0cdc022 | 2008-06-26 18:04:03 +0000 | [diff] [blame] | 2042 | eType = sqlite3FindInIndex(pParse, pExpr, &rMayHaveNull); |
| 2043 | if( rMayHaveNull ){ |
| 2044 | rNotFound = ++pParse->nMem; |
| 2045 | } |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 2046 | |
| 2047 | /* Figure out the affinity to use to create a key from the results |
| 2048 | ** of the expression. affinityStr stores a static string suitable for |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 2049 | ** P4 of OP_MakeRecord. |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 2050 | */ |
drh | 94a1121 | 2004-09-25 13:12:14 +0000 | [diff] [blame] | 2051 | affinity = comparisonAffinity(pExpr); |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 2052 | |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 2053 | |
| 2054 | /* Code the <expr> from "<expr> IN (...)". The temporary table |
| 2055 | ** pExpr->iTable contains the values that make up the (...) set. |
| 2056 | */ |
drh | 66ba23c | 2008-06-27 00:47:28 +0000 | [diff] [blame] | 2057 | pParse->disableColCache++; |
| 2058 | sqlite3ExprCode(pParse, pExpr->pLeft, target); |
| 2059 | pParse->disableColCache--; |
| 2060 | j2 = sqlite3VdbeAddOp1(v, OP_IsNull, target); |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 2061 | if( eType==IN_INDEX_ROWID ){ |
drh | 66ba23c | 2008-06-27 00:47:28 +0000 | [diff] [blame] | 2062 | j3 = sqlite3VdbeAddOp1(v, OP_MustBeInt, target); |
| 2063 | j4 = sqlite3VdbeAddOp3(v, OP_NotExists, pExpr->iTable, 0, target); |
| 2064 | sqlite3VdbeAddOp2(v, OP_Integer, 1, target); |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 2065 | j5 = sqlite3VdbeAddOp0(v, OP_Goto); |
| 2066 | sqlite3VdbeJumpHere(v, j3); |
| 2067 | sqlite3VdbeJumpHere(v, j4); |
danielk1977 | 0cdc022 | 2008-06-26 18:04:03 +0000 | [diff] [blame] | 2068 | sqlite3VdbeAddOp2(v, OP_Integer, 0, target); |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 2069 | }else{ |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2070 | r2 = regFree2 = sqlite3GetTempReg(pParse); |
danielk1977 | 0cdc022 | 2008-06-26 18:04:03 +0000 | [diff] [blame] | 2071 | |
| 2072 | /* Create a record and test for set membership. If the set contains |
| 2073 | ** the value, then jump to the end of the test code. The target |
| 2074 | ** register still contains the true (1) value written to it earlier. |
| 2075 | */ |
drh | 66ba23c | 2008-06-27 00:47:28 +0000 | [diff] [blame] | 2076 | sqlite3VdbeAddOp4(v, OP_MakeRecord, target, 1, r2, &affinity, 1); |
| 2077 | sqlite3VdbeAddOp2(v, OP_Integer, 1, target); |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2078 | j5 = sqlite3VdbeAddOp3(v, OP_Found, pExpr->iTable, 0, r2); |
danielk1977 | 0cdc022 | 2008-06-26 18:04:03 +0000 | [diff] [blame] | 2079 | |
| 2080 | /* If the set membership test fails, then the result of the |
| 2081 | ** "x IN (...)" expression must be either 0 or NULL. If the set |
| 2082 | ** contains no NULL values, then the result is 0. If the set |
| 2083 | ** contains one or more NULL values, then the result of the |
| 2084 | ** expression is also NULL. |
| 2085 | */ |
| 2086 | if( rNotFound==0 ){ |
| 2087 | /* This branch runs if it is known at compile time (now) that |
| 2088 | ** the set contains no NULL values. This happens as the result |
| 2089 | ** of a "NOT NULL" constraint in the database schema. No need |
| 2090 | ** to test the data structure at runtime in this case. |
| 2091 | */ |
| 2092 | sqlite3VdbeAddOp2(v, OP_Integer, 0, target); |
| 2093 | }else{ |
| 2094 | /* This block populates the rNotFound register with either NULL |
| 2095 | ** or 0 (an integer value). If the data structure contains one |
| 2096 | ** or more NULLs, then set rNotFound to NULL. Otherwise, set it |
| 2097 | ** to 0. If register rMayHaveNull is already set to some value |
| 2098 | ** other than NULL, then the test has already been run and |
| 2099 | ** rNotFound is already populated. |
| 2100 | */ |
drh | 66ba23c | 2008-06-27 00:47:28 +0000 | [diff] [blame] | 2101 | static const char nullRecord[] = { 0x02, 0x00 }; |
danielk1977 | 0cdc022 | 2008-06-26 18:04:03 +0000 | [diff] [blame] | 2102 | j3 = sqlite3VdbeAddOp1(v, OP_NotNull, rMayHaveNull); |
| 2103 | sqlite3VdbeAddOp2(v, OP_Null, 0, rNotFound); |
drh | 66ba23c | 2008-06-27 00:47:28 +0000 | [diff] [blame] | 2104 | sqlite3VdbeAddOp4(v, OP_Blob, 2, rMayHaveNull, 0, |
| 2105 | nullRecord, P4_STATIC); |
| 2106 | j4 = sqlite3VdbeAddOp3(v, OP_Found, pExpr->iTable, 0, rMayHaveNull); |
danielk1977 | 0cdc022 | 2008-06-26 18:04:03 +0000 | [diff] [blame] | 2107 | sqlite3VdbeAddOp2(v, OP_Integer, 0, rNotFound); |
| 2108 | sqlite3VdbeJumpHere(v, j4); |
| 2109 | sqlite3VdbeJumpHere(v, j3); |
| 2110 | |
| 2111 | /* Copy the value of register rNotFound (which is either NULL or 0) |
| 2112 | ** into the target register. This will be the result of the |
| 2113 | ** expression. |
| 2114 | */ |
| 2115 | sqlite3VdbeAddOp2(v, OP_Copy, rNotFound, target); |
| 2116 | } |
danielk1977 | 9a96b66 | 2007-11-29 17:05:18 +0000 | [diff] [blame] | 2117 | } |
drh | 6a288a3 | 2008-01-07 19:20:24 +0000 | [diff] [blame] | 2118 | sqlite3VdbeJumpHere(v, j2); |
| 2119 | sqlite3VdbeJumpHere(v, j5); |
drh | 3c31fc2 | 2008-06-26 21:45:26 +0000 | [diff] [blame] | 2120 | VdbeComment((v, "end IN expr r%d", target)); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 2121 | break; |
| 2122 | } |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 2123 | #endif |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2124 | /* |
| 2125 | ** x BETWEEN y AND z |
| 2126 | ** |
| 2127 | ** This is equivalent to |
| 2128 | ** |
| 2129 | ** x>=y AND x<=z |
| 2130 | ** |
| 2131 | ** X is stored in pExpr->pLeft. |
| 2132 | ** Y is stored in pExpr->pList->a[0].pExpr. |
| 2133 | ** Z is stored in pExpr->pList->a[1].pExpr. |
| 2134 | */ |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 2135 | case TK_BETWEEN: { |
drh | be5c89a | 2004-07-26 00:31:09 +0000 | [diff] [blame] | 2136 | Expr *pLeft = pExpr->pLeft; |
| 2137 | struct ExprList_item *pLItem = pExpr->pList->a; |
| 2138 | Expr *pRight = pLItem->pExpr; |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 2139 | |
drh | da250ea | 2008-04-01 05:07:14 +0000 | [diff] [blame] | 2140 | codeCompareOperands(pParse, pLeft, &r1, ®Free1, |
| 2141 | pRight, &r2, ®Free2); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 2142 | testcase( regFree1==0 ); |
| 2143 | testcase( regFree2==0 ); |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2144 | r3 = sqlite3GetTempReg(pParse); |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2145 | r4 = sqlite3GetTempReg(pParse); |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 2146 | codeCompare(pParse, pLeft, pRight, OP_Ge, |
| 2147 | r1, r2, r3, SQLITE_STOREP2); |
drh | be5c89a | 2004-07-26 00:31:09 +0000 | [diff] [blame] | 2148 | pLItem++; |
| 2149 | pRight = pLItem->pExpr; |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2150 | sqlite3ReleaseTempReg(pParse, regFree2); |
| 2151 | r2 = sqlite3ExprCodeTemp(pParse, pRight, ®Free2); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 2152 | testcase( regFree2==0 ); |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2153 | codeCompare(pParse, pLeft, pRight, OP_Le, r1, r2, r4, SQLITE_STOREP2); |
| 2154 | sqlite3VdbeAddOp3(v, OP_And, r3, r4, target); |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2155 | sqlite3ReleaseTempReg(pParse, r3); |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2156 | sqlite3ReleaseTempReg(pParse, r4); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 2157 | break; |
| 2158 | } |
drh | 4f07e5f | 2007-05-14 11:34:46 +0000 | [diff] [blame] | 2159 | case TK_UPLUS: { |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2160 | inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target); |
drh | a2e0004 | 2002-01-22 03:13:42 +0000 | [diff] [blame] | 2161 | break; |
| 2162 | } |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2163 | |
| 2164 | /* |
| 2165 | ** Form A: |
| 2166 | ** CASE x WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END |
| 2167 | ** |
| 2168 | ** Form B: |
| 2169 | ** CASE WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END |
| 2170 | ** |
| 2171 | ** Form A is can be transformed into the equivalent form B as follows: |
| 2172 | ** CASE WHEN x=e1 THEN r1 WHEN x=e2 THEN r2 ... |
| 2173 | ** WHEN x=eN THEN rN ELSE y END |
| 2174 | ** |
| 2175 | ** X (if it exists) is in pExpr->pLeft. |
| 2176 | ** Y is in pExpr->pRight. The Y is also optional. If there is no |
| 2177 | ** ELSE clause and no other term matches, then the result of the |
| 2178 | ** exprssion is NULL. |
| 2179 | ** Ei is in pExpr->pList->a[i*2] and Ri is pExpr->pList->a[i*2+1]. |
| 2180 | ** |
| 2181 | ** The result of the expression is the Ri for the first matching Ei, |
| 2182 | ** or if there is no matching Ei, the ELSE term Y, or if there is |
| 2183 | ** no ELSE term, NULL. |
| 2184 | */ |
drh | 17a7f8d | 2002-03-24 13:13:27 +0000 | [diff] [blame] | 2185 | case TK_CASE: { |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2186 | int endLabel; /* GOTO label for end of CASE stmt */ |
| 2187 | int nextCase; /* GOTO label for next WHEN clause */ |
| 2188 | int nExpr; /* 2x number of WHEN terms */ |
| 2189 | int i; /* Loop counter */ |
| 2190 | ExprList *pEList; /* List of WHEN terms */ |
| 2191 | struct ExprList_item *aListelem; /* Array of WHEN terms */ |
| 2192 | Expr opCompare; /* The X==Ei expression */ |
| 2193 | Expr cacheX; /* Cached expression X */ |
| 2194 | Expr *pX; /* The X expression */ |
| 2195 | Expr *pTest; /* X==Ei (form A) or just Ei (form B) */ |
drh | 17a7f8d | 2002-03-24 13:13:27 +0000 | [diff] [blame] | 2196 | |
| 2197 | assert(pExpr->pList); |
| 2198 | assert((pExpr->pList->nExpr % 2) == 0); |
| 2199 | assert(pExpr->pList->nExpr > 0); |
drh | be5c89a | 2004-07-26 00:31:09 +0000 | [diff] [blame] | 2200 | pEList = pExpr->pList; |
| 2201 | aListelem = pEList->a; |
| 2202 | nExpr = pEList->nExpr; |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2203 | endLabel = sqlite3VdbeMakeLabel(v); |
| 2204 | if( (pX = pExpr->pLeft)!=0 ){ |
| 2205 | cacheX = *pX; |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 2206 | testcase( pX->op==TK_COLUMN || pX->op==TK_REGISTER ); |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2207 | cacheX.iTable = sqlite3ExprCodeTemp(pParse, pX, ®Free1); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 2208 | testcase( regFree1==0 ); |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2209 | cacheX.op = TK_REGISTER; |
| 2210 | opCompare.op = TK_EQ; |
| 2211 | opCompare.pLeft = &cacheX; |
| 2212 | pTest = &opCompare; |
drh | 17a7f8d | 2002-03-24 13:13:27 +0000 | [diff] [blame] | 2213 | } |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 2214 | pParse->disableColCache++; |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 2215 | for(i=0; i<nExpr; i=i+2){ |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2216 | if( pX ){ |
| 2217 | opCompare.pRight = aListelem[i].pExpr; |
drh | 17a7f8d | 2002-03-24 13:13:27 +0000 | [diff] [blame] | 2218 | }else{ |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2219 | pTest = aListelem[i].pExpr; |
drh | 17a7f8d | 2002-03-24 13:13:27 +0000 | [diff] [blame] | 2220 | } |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2221 | nextCase = sqlite3VdbeMakeLabel(v); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 2222 | testcase( pTest->op==TK_COLUMN || pTest->op==TK_REGISTER ); |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2223 | sqlite3ExprIfFalse(pParse, pTest, nextCase, SQLITE_JUMPIFNULL); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 2224 | testcase( aListelem[i+1].pExpr->op==TK_COLUMN ); |
| 2225 | testcase( aListelem[i+1].pExpr->op==TK_REGISTER ); |
drh | 9de221d | 2008-01-05 06:51:30 +0000 | [diff] [blame] | 2226 | sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target); |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2227 | sqlite3VdbeAddOp2(v, OP_Goto, 0, endLabel); |
| 2228 | sqlite3VdbeResolveLabel(v, nextCase); |
drh | f570f01 | 2002-05-31 15:51:25 +0000 | [diff] [blame] | 2229 | } |
drh | 17a7f8d | 2002-03-24 13:13:27 +0000 | [diff] [blame] | 2230 | if( pExpr->pRight ){ |
drh | 9de221d | 2008-01-05 06:51:30 +0000 | [diff] [blame] | 2231 | sqlite3ExprCode(pParse, pExpr->pRight, target); |
drh | 17a7f8d | 2002-03-24 13:13:27 +0000 | [diff] [blame] | 2232 | }else{ |
drh | 9de221d | 2008-01-05 06:51:30 +0000 | [diff] [blame] | 2233 | sqlite3VdbeAddOp2(v, OP_Null, 0, target); |
drh | 17a7f8d | 2002-03-24 13:13:27 +0000 | [diff] [blame] | 2234 | } |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2235 | sqlite3VdbeResolveLabel(v, endLabel); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 2236 | assert( pParse->disableColCache>0 ); |
| 2237 | pParse->disableColCache--; |
danielk1977 | 6f34903 | 2002-06-11 02:25:40 +0000 | [diff] [blame] | 2238 | break; |
| 2239 | } |
danielk1977 | 5338a5f | 2005-01-20 13:03:10 +0000 | [diff] [blame] | 2240 | #ifndef SQLITE_OMIT_TRIGGER |
danielk1977 | 6f34903 | 2002-06-11 02:25:40 +0000 | [diff] [blame] | 2241 | case TK_RAISE: { |
| 2242 | if( !pParse->trigStack ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2243 | sqlite3ErrorMsg(pParse, |
drh | da93d23 | 2003-03-31 02:12:46 +0000 | [diff] [blame] | 2244 | "RAISE() may only be used within a trigger-program"); |
drh | 389a1ad | 2008-01-03 23:44:53 +0000 | [diff] [blame] | 2245 | return 0; |
danielk1977 | 6f34903 | 2002-06-11 02:25:40 +0000 | [diff] [blame] | 2246 | } |
drh | ad6d946 | 2004-09-19 02:15:24 +0000 | [diff] [blame] | 2247 | if( pExpr->iColumn!=OE_Ignore ){ |
| 2248 | assert( pExpr->iColumn==OE_Rollback || |
| 2249 | pExpr->iColumn == OE_Abort || |
| 2250 | pExpr->iColumn == OE_Fail ); |
drh | 8b21389 | 2008-08-29 02:14:02 +0000 | [diff] [blame] | 2251 | sqlite3DequoteExpr(db, pExpr); |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 2252 | sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn, 0, |
drh | 2646da7 | 2005-12-09 20:02:05 +0000 | [diff] [blame] | 2253 | (char*)pExpr->token.z, pExpr->token.n); |
danielk1977 | 6f34903 | 2002-06-11 02:25:40 +0000 | [diff] [blame] | 2254 | } else { |
drh | ad6d946 | 2004-09-19 02:15:24 +0000 | [diff] [blame] | 2255 | assert( pExpr->iColumn == OE_Ignore ); |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 2256 | sqlite3VdbeAddOp2(v, OP_ContextPop, 0, 0); |
| 2257 | sqlite3VdbeAddOp2(v, OP_Goto, 0, pParse->trigStack->ignoreJump); |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 2258 | VdbeComment((v, "raise(IGNORE)")); |
danielk1977 | 6f34903 | 2002-06-11 02:25:40 +0000 | [diff] [blame] | 2259 | } |
drh | ffe07b2 | 2005-11-03 00:41:17 +0000 | [diff] [blame] | 2260 | break; |
drh | 17a7f8d | 2002-03-24 13:13:27 +0000 | [diff] [blame] | 2261 | } |
danielk1977 | 5338a5f | 2005-01-20 13:03:10 +0000 | [diff] [blame] | 2262 | #endif |
drh | ffe07b2 | 2005-11-03 00:41:17 +0000 | [diff] [blame] | 2263 | } |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2264 | sqlite3ReleaseTempReg(pParse, regFree1); |
| 2265 | sqlite3ReleaseTempReg(pParse, regFree2); |
| 2266 | return inReg; |
| 2267 | } |
| 2268 | |
| 2269 | /* |
| 2270 | ** Generate code to evaluate an expression and store the results |
| 2271 | ** into a register. Return the register number where the results |
| 2272 | ** are stored. |
| 2273 | ** |
| 2274 | ** If the register is a temporary register that can be deallocated, |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2275 | ** then write its number into *pReg. If the result register is not |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2276 | ** a temporary, then set *pReg to zero. |
| 2277 | */ |
| 2278 | int sqlite3ExprCodeTemp(Parse *pParse, Expr *pExpr, int *pReg){ |
| 2279 | int r1 = sqlite3GetTempReg(pParse); |
| 2280 | int r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1); |
| 2281 | if( r2==r1 ){ |
| 2282 | *pReg = r1; |
| 2283 | }else{ |
| 2284 | sqlite3ReleaseTempReg(pParse, r1); |
| 2285 | *pReg = 0; |
| 2286 | } |
| 2287 | return r2; |
| 2288 | } |
| 2289 | |
| 2290 | /* |
| 2291 | ** Generate code that will evaluate expression pExpr and store the |
| 2292 | ** results in register target. The results are guaranteed to appear |
| 2293 | ** in register target. |
| 2294 | */ |
| 2295 | int sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2296 | int inReg; |
| 2297 | |
| 2298 | assert( target>0 && target<=pParse->nMem ); |
| 2299 | inReg = sqlite3ExprCodeTarget(pParse, pExpr, target); |
drh | 0e359b3 | 2008-01-13 19:02:11 +0000 | [diff] [blame] | 2300 | assert( pParse->pVdbe || pParse->db->mallocFailed ); |
| 2301 | if( inReg!=target && pParse->pVdbe ){ |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2302 | sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, inReg, target); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 2303 | } |
drh | 389a1ad | 2008-01-03 23:44:53 +0000 | [diff] [blame] | 2304 | return target; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 2305 | } |
| 2306 | |
| 2307 | /* |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2308 | ** Generate code that evalutes the given expression and puts the result |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 2309 | ** in register target. |
drh | 2530378 | 2004-12-07 15:41:48 +0000 | [diff] [blame] | 2310 | ** |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2311 | ** Also make a copy of the expression results into another "cache" register |
| 2312 | ** and modify the expression so that the next time it is evaluated, |
| 2313 | ** the result is a copy of the cache register. |
| 2314 | ** |
| 2315 | ** This routine is used for expressions that are used multiple |
| 2316 | ** times. They are evaluated once and the results of the expression |
| 2317 | ** are reused. |
drh | 2530378 | 2004-12-07 15:41:48 +0000 | [diff] [blame] | 2318 | */ |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2319 | int sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr, int target){ |
drh | 2530378 | 2004-12-07 15:41:48 +0000 | [diff] [blame] | 2320 | Vdbe *v = pParse->pVdbe; |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2321 | int inReg; |
| 2322 | inReg = sqlite3ExprCode(pParse, pExpr, target); |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 2323 | assert( target>0 ); |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2324 | if( pExpr->op!=TK_REGISTER ){ |
| 2325 | int iMem; |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 2326 | iMem = ++pParse->nMem; |
| 2327 | sqlite3VdbeAddOp2(v, OP_Copy, inReg, iMem); |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2328 | pExpr->iTable = iMem; |
drh | 2530378 | 2004-12-07 15:41:48 +0000 | [diff] [blame] | 2329 | pExpr->op = TK_REGISTER; |
| 2330 | } |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2331 | return inReg; |
drh | 2530378 | 2004-12-07 15:41:48 +0000 | [diff] [blame] | 2332 | } |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2333 | |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2334 | /* |
drh | 47de955 | 2008-04-01 18:04:11 +0000 | [diff] [blame] | 2335 | ** Return TRUE if pExpr is an constant expression that is appropriate |
| 2336 | ** for factoring out of a loop. Appropriate expressions are: |
| 2337 | ** |
| 2338 | ** * Any expression that evaluates to two or more opcodes. |
| 2339 | ** |
| 2340 | ** * Any OP_Integer, OP_Real, OP_String, OP_Blob, OP_Null, |
| 2341 | ** or OP_Variable that does not need to be placed in a |
| 2342 | ** specific register. |
| 2343 | ** |
| 2344 | ** There is no point in factoring out single-instruction constant |
| 2345 | ** expressions that need to be placed in a particular register. |
| 2346 | ** We could factor them out, but then we would end up adding an |
| 2347 | ** OP_SCopy instruction to move the value into the correct register |
| 2348 | ** later. We might as well just use the original instruction and |
| 2349 | ** avoid the OP_SCopy. |
| 2350 | */ |
| 2351 | static int isAppropriateForFactoring(Expr *p){ |
| 2352 | if( !sqlite3ExprIsConstantNotJoin(p) ){ |
| 2353 | return 0; /* Only constant expressions are appropriate for factoring */ |
| 2354 | } |
| 2355 | if( (p->flags & EP_FixedDest)==0 ){ |
| 2356 | return 1; /* Any constant without a fixed destination is appropriate */ |
| 2357 | } |
| 2358 | while( p->op==TK_UPLUS ) p = p->pLeft; |
| 2359 | switch( p->op ){ |
| 2360 | #ifndef SQLITE_OMIT_BLOB_LITERAL |
| 2361 | case TK_BLOB: |
| 2362 | #endif |
| 2363 | case TK_VARIABLE: |
| 2364 | case TK_INTEGER: |
| 2365 | case TK_FLOAT: |
| 2366 | case TK_NULL: |
| 2367 | case TK_STRING: { |
| 2368 | testcase( p->op==TK_BLOB ); |
| 2369 | testcase( p->op==TK_VARIABLE ); |
| 2370 | testcase( p->op==TK_INTEGER ); |
| 2371 | testcase( p->op==TK_FLOAT ); |
| 2372 | testcase( p->op==TK_NULL ); |
| 2373 | testcase( p->op==TK_STRING ); |
| 2374 | /* Single-instruction constants with a fixed destination are |
| 2375 | ** better done in-line. If we factor them, they will just end |
| 2376 | ** up generating an OP_SCopy to move the value to the destination |
| 2377 | ** register. */ |
| 2378 | return 0; |
| 2379 | } |
| 2380 | case TK_UMINUS: { |
| 2381 | if( p->pLeft->op==TK_FLOAT || p->pLeft->op==TK_INTEGER ){ |
| 2382 | return 0; |
| 2383 | } |
| 2384 | break; |
| 2385 | } |
| 2386 | default: { |
| 2387 | break; |
| 2388 | } |
| 2389 | } |
| 2390 | return 1; |
| 2391 | } |
| 2392 | |
| 2393 | /* |
| 2394 | ** If pExpr is a constant expression that is appropriate for |
| 2395 | ** factoring out of a loop, then evaluate the expression |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2396 | ** into a register and convert the expression into a TK_REGISTER |
| 2397 | ** expression. |
| 2398 | */ |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 2399 | static int evalConstExpr(Walker *pWalker, Expr *pExpr){ |
| 2400 | Parse *pParse = pWalker->pParse; |
drh | 47de955 | 2008-04-01 18:04:11 +0000 | [diff] [blame] | 2401 | switch( pExpr->op ){ |
| 2402 | case TK_REGISTER: { |
| 2403 | return 1; |
| 2404 | } |
| 2405 | case TK_FUNCTION: |
| 2406 | case TK_AGG_FUNCTION: |
| 2407 | case TK_CONST_FUNC: { |
| 2408 | /* The arguments to a function have a fixed destination. |
| 2409 | ** Mark them this way to avoid generated unneeded OP_SCopy |
| 2410 | ** instructions. |
| 2411 | */ |
| 2412 | ExprList *pList = pExpr->pList; |
| 2413 | if( pList ){ |
| 2414 | int i = pList->nExpr; |
| 2415 | struct ExprList_item *pItem = pList->a; |
| 2416 | for(; i>0; i--, pItem++){ |
| 2417 | if( pItem->pExpr ) pItem->pExpr->flags |= EP_FixedDest; |
| 2418 | } |
| 2419 | } |
| 2420 | break; |
| 2421 | } |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2422 | } |
drh | 47de955 | 2008-04-01 18:04:11 +0000 | [diff] [blame] | 2423 | if( isAppropriateForFactoring(pExpr) ){ |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2424 | int r1 = ++pParse->nMem; |
| 2425 | int r2; |
| 2426 | r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 2427 | if( r1!=r2 ) sqlite3ReleaseTempReg(pParse, r1); |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2428 | pExpr->op = TK_REGISTER; |
| 2429 | pExpr->iTable = r2; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 2430 | return WRC_Prune; |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2431 | } |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 2432 | return WRC_Continue; |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2433 | } |
| 2434 | |
| 2435 | /* |
| 2436 | ** Preevaluate constant subexpressions within pExpr and store the |
| 2437 | ** results in registers. Modify pExpr so that the constant subexpresions |
| 2438 | ** are TK_REGISTER opcodes that refer to the precomputed values. |
| 2439 | */ |
| 2440 | void sqlite3ExprCodeConstants(Parse *pParse, Expr *pExpr){ |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 2441 | Walker w; |
| 2442 | w.xExprCallback = evalConstExpr; |
| 2443 | w.xSelectCallback = 0; |
| 2444 | w.pParse = pParse; |
| 2445 | sqlite3WalkExpr(&w, pExpr); |
drh | 678ccce | 2008-03-31 18:19:54 +0000 | [diff] [blame] | 2446 | } |
| 2447 | |
drh | 2530378 | 2004-12-07 15:41:48 +0000 | [diff] [blame] | 2448 | |
| 2449 | /* |
drh | 268380c | 2004-02-25 13:47:31 +0000 | [diff] [blame] | 2450 | ** Generate code that pushes the value of every element of the given |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2451 | ** expression list into a sequence of registers beginning at target. |
drh | 268380c | 2004-02-25 13:47:31 +0000 | [diff] [blame] | 2452 | ** |
drh | 892d317 | 2008-01-10 03:46:36 +0000 | [diff] [blame] | 2453 | ** Return the number of elements evaluated. |
drh | 268380c | 2004-02-25 13:47:31 +0000 | [diff] [blame] | 2454 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2455 | int sqlite3ExprCodeExprList( |
drh | 268380c | 2004-02-25 13:47:31 +0000 | [diff] [blame] | 2456 | Parse *pParse, /* Parsing context */ |
drh | 389a1ad | 2008-01-03 23:44:53 +0000 | [diff] [blame] | 2457 | ExprList *pList, /* The expression list to be coded */ |
drh | 191b54c | 2008-04-15 12:14:21 +0000 | [diff] [blame] | 2458 | int target, /* Where to write results */ |
drh | d176611 | 2008-09-17 00:13:12 +0000 | [diff] [blame] | 2459 | int doHardCopy /* Make a hard copy of every element */ |
drh | 268380c | 2004-02-25 13:47:31 +0000 | [diff] [blame] | 2460 | ){ |
| 2461 | struct ExprList_item *pItem; |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2462 | int i, n; |
drh | 9d8b307 | 2008-08-22 16:29:51 +0000 | [diff] [blame] | 2463 | assert( pList!=0 ); |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 2464 | assert( target>0 ); |
drh | 268380c | 2004-02-25 13:47:31 +0000 | [diff] [blame] | 2465 | n = pList->nExpr; |
drh | 191b54c | 2008-04-15 12:14:21 +0000 | [diff] [blame] | 2466 | for(pItem=pList->a, i=0; i<n; i++, pItem++){ |
drh | 8b21389 | 2008-08-29 02:14:02 +0000 | [diff] [blame] | 2467 | if( pItem->iAlias ){ |
| 2468 | int iReg = codeAlias(pParse, pItem->iAlias, pItem->pExpr); |
| 2469 | Vdbe *v = sqlite3GetVdbe(pParse); |
| 2470 | sqlite3VdbeAddOp2(v, OP_SCopy, iReg, target+i); |
drh | d176611 | 2008-09-17 00:13:12 +0000 | [diff] [blame] | 2471 | }else{ |
drh | 8b21389 | 2008-08-29 02:14:02 +0000 | [diff] [blame] | 2472 | sqlite3ExprCode(pParse, pItem->pExpr, target+i); |
| 2473 | } |
drh | d176611 | 2008-09-17 00:13:12 +0000 | [diff] [blame] | 2474 | if( doHardCopy ){ |
| 2475 | sqlite3ExprHardCopy(pParse, target, n); |
| 2476 | } |
drh | 268380c | 2004-02-25 13:47:31 +0000 | [diff] [blame] | 2477 | } |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 2478 | return n; |
drh | 268380c | 2004-02-25 13:47:31 +0000 | [diff] [blame] | 2479 | } |
| 2480 | |
| 2481 | /* |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 2482 | ** Generate code for a boolean expression such that a jump is made |
| 2483 | ** to the label "dest" if the expression is true but execution |
| 2484 | ** continues straight thru if the expression is false. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 2485 | ** |
| 2486 | ** If the expression evaluates to NULL (neither true nor false), then |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 2487 | ** take the jump if the jumpIfNull flag is SQLITE_JUMPIFNULL. |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 2488 | ** |
| 2489 | ** This code depends on the fact that certain token values (ex: TK_EQ) |
| 2490 | ** are the same as opcode values (ex: OP_Eq) that implement the corresponding |
| 2491 | ** operation. Special comments in vdbe.c and the mkopcodeh.awk script in |
| 2492 | ** the make process cause these values to align. Assert()s in the code |
| 2493 | ** below verify that the numbers are aligned correctly. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 2494 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2495 | void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 2496 | Vdbe *v = pParse->pVdbe; |
| 2497 | int op = 0; |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2498 | int regFree1 = 0; |
| 2499 | int regFree2 = 0; |
| 2500 | int r1, r2; |
| 2501 | |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 2502 | assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 ); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 2503 | if( v==0 || pExpr==0 ) return; |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 2504 | op = pExpr->op; |
| 2505 | switch( op ){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 2506 | case TK_AND: { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2507 | int d2 = sqlite3VdbeMakeLabel(v); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 2508 | testcase( jumpIfNull==0 ); |
| 2509 | testcase( pParse->disableColCache==0 ); |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 2510 | sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,jumpIfNull^SQLITE_JUMPIFNULL); |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 2511 | pParse->disableColCache++; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2512 | sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 2513 | assert( pParse->disableColCache>0 ); |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 2514 | pParse->disableColCache--; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2515 | sqlite3VdbeResolveLabel(v, d2); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 2516 | break; |
| 2517 | } |
| 2518 | case TK_OR: { |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 2519 | testcase( jumpIfNull==0 ); |
| 2520 | testcase( pParse->disableColCache==0 ); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2521 | sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 2522 | pParse->disableColCache++; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2523 | sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 2524 | assert( pParse->disableColCache>0 ); |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 2525 | pParse->disableColCache--; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 2526 | break; |
| 2527 | } |
| 2528 | case TK_NOT: { |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 2529 | testcase( jumpIfNull==0 ); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2530 | sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 2531 | break; |
| 2532 | } |
| 2533 | case TK_LT: |
| 2534 | case TK_LE: |
| 2535 | case TK_GT: |
| 2536 | case TK_GE: |
| 2537 | case TK_NE: |
drh | 0ac6589 | 2002-04-20 14:24:41 +0000 | [diff] [blame] | 2538 | case TK_EQ: { |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 2539 | assert( TK_LT==OP_Lt ); |
| 2540 | assert( TK_LE==OP_Le ); |
| 2541 | assert( TK_GT==OP_Gt ); |
| 2542 | assert( TK_GE==OP_Ge ); |
| 2543 | assert( TK_EQ==OP_Eq ); |
| 2544 | assert( TK_NE==OP_Ne ); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 2545 | testcase( op==TK_LT ); |
| 2546 | testcase( op==TK_LE ); |
| 2547 | testcase( op==TK_GT ); |
| 2548 | testcase( op==TK_GE ); |
| 2549 | testcase( op==TK_EQ ); |
| 2550 | testcase( op==TK_NE ); |
| 2551 | testcase( jumpIfNull==0 ); |
drh | da250ea | 2008-04-01 05:07:14 +0000 | [diff] [blame] | 2552 | codeCompareOperands(pParse, pExpr->pLeft, &r1, ®Free1, |
| 2553 | pExpr->pRight, &r2, ®Free2); |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 2554 | codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2555 | r1, r2, dest, jumpIfNull); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 2556 | testcase( regFree1==0 ); |
| 2557 | testcase( regFree2==0 ); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 2558 | break; |
| 2559 | } |
| 2560 | case TK_ISNULL: |
| 2561 | case TK_NOTNULL: { |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 2562 | assert( TK_ISNULL==OP_IsNull ); |
| 2563 | assert( TK_NOTNULL==OP_NotNull ); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 2564 | testcase( op==TK_ISNULL ); |
| 2565 | testcase( op==TK_NOTNULL ); |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2566 | r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); |
| 2567 | sqlite3VdbeAddOp2(v, op, r1, dest); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 2568 | testcase( regFree1==0 ); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 2569 | break; |
| 2570 | } |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 2571 | case TK_BETWEEN: { |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2572 | /* x BETWEEN y AND z |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 2573 | ** |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2574 | ** Is equivalent to |
| 2575 | ** |
| 2576 | ** x>=y AND x<=z |
| 2577 | ** |
| 2578 | ** Code it as such, taking care to do the common subexpression |
| 2579 | ** elementation of x. |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 2580 | */ |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2581 | Expr exprAnd; |
| 2582 | Expr compLeft; |
| 2583 | Expr compRight; |
| 2584 | Expr exprX; |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 2585 | |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2586 | exprX = *pExpr->pLeft; |
| 2587 | exprAnd.op = TK_AND; |
| 2588 | exprAnd.pLeft = &compLeft; |
| 2589 | exprAnd.pRight = &compRight; |
| 2590 | compLeft.op = TK_GE; |
| 2591 | compLeft.pLeft = &exprX; |
| 2592 | compLeft.pRight = pExpr->pList->a[0].pExpr; |
| 2593 | compRight.op = TK_LE; |
| 2594 | compRight.pLeft = &exprX; |
| 2595 | compRight.pRight = pExpr->pList->a[1].pExpr; |
| 2596 | exprX.iTable = sqlite3ExprCodeTemp(pParse, &exprX, ®Free1); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 2597 | testcase( regFree1==0 ); |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2598 | exprX.op = TK_REGISTER; |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 2599 | testcase( jumpIfNull==0 ); |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2600 | sqlite3ExprIfTrue(pParse, &exprAnd, dest, jumpIfNull); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 2601 | break; |
| 2602 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 2603 | default: { |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2604 | r1 = sqlite3ExprCodeTemp(pParse, pExpr, ®Free1); |
| 2605 | sqlite3VdbeAddOp3(v, OP_If, r1, dest, jumpIfNull!=0); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 2606 | testcase( regFree1==0 ); |
| 2607 | testcase( jumpIfNull==0 ); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 2608 | break; |
| 2609 | } |
| 2610 | } |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2611 | sqlite3ReleaseTempReg(pParse, regFree1); |
| 2612 | sqlite3ReleaseTempReg(pParse, regFree2); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 2613 | } |
| 2614 | |
| 2615 | /* |
drh | 66b89c8 | 2000-11-28 20:47:17 +0000 | [diff] [blame] | 2616 | ** Generate code for a boolean expression such that a jump is made |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 2617 | ** to the label "dest" if the expression is false but execution |
| 2618 | ** continues straight thru if the expression is true. |
drh | f5905aa | 2002-05-26 20:54:33 +0000 | [diff] [blame] | 2619 | ** |
| 2620 | ** If the expression evaluates to NULL (neither true nor false) then |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 2621 | ** jump if jumpIfNull is SQLITE_JUMPIFNULL or fall through if jumpIfNull |
| 2622 | ** is 0. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 2623 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2624 | void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 2625 | Vdbe *v = pParse->pVdbe; |
| 2626 | int op = 0; |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2627 | int regFree1 = 0; |
| 2628 | int regFree2 = 0; |
| 2629 | int r1, r2; |
| 2630 | |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 2631 | assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 ); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 2632 | if( v==0 || pExpr==0 ) return; |
drh | f2bc013 | 2004-10-04 13:19:23 +0000 | [diff] [blame] | 2633 | |
| 2634 | /* The value of pExpr->op and op are related as follows: |
| 2635 | ** |
| 2636 | ** pExpr->op op |
| 2637 | ** --------- ---------- |
| 2638 | ** TK_ISNULL OP_NotNull |
| 2639 | ** TK_NOTNULL OP_IsNull |
| 2640 | ** TK_NE OP_Eq |
| 2641 | ** TK_EQ OP_Ne |
| 2642 | ** TK_GT OP_Le |
| 2643 | ** TK_LE OP_Gt |
| 2644 | ** TK_GE OP_Lt |
| 2645 | ** TK_LT OP_Ge |
| 2646 | ** |
| 2647 | ** For other values of pExpr->op, op is undefined and unused. |
| 2648 | ** The value of TK_ and OP_ constants are arranged such that we |
| 2649 | ** can compute the mapping above using the following expression. |
| 2650 | ** Assert()s verify that the computation is correct. |
| 2651 | */ |
| 2652 | op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1); |
| 2653 | |
| 2654 | /* Verify correct alignment of TK_ and OP_ constants |
| 2655 | */ |
| 2656 | assert( pExpr->op!=TK_ISNULL || op==OP_NotNull ); |
| 2657 | assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull ); |
| 2658 | assert( pExpr->op!=TK_NE || op==OP_Eq ); |
| 2659 | assert( pExpr->op!=TK_EQ || op==OP_Ne ); |
| 2660 | assert( pExpr->op!=TK_LT || op==OP_Ge ); |
| 2661 | assert( pExpr->op!=TK_LE || op==OP_Gt ); |
| 2662 | assert( pExpr->op!=TK_GT || op==OP_Le ); |
| 2663 | assert( pExpr->op!=TK_GE || op==OP_Lt ); |
| 2664 | |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 2665 | switch( pExpr->op ){ |
| 2666 | case TK_AND: { |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 2667 | testcase( jumpIfNull==0 ); |
| 2668 | testcase( pParse->disableColCache==0 ); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2669 | sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 2670 | pParse->disableColCache++; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2671 | sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 2672 | assert( pParse->disableColCache>0 ); |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 2673 | pParse->disableColCache--; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 2674 | break; |
| 2675 | } |
| 2676 | case TK_OR: { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2677 | int d2 = sqlite3VdbeMakeLabel(v); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 2678 | testcase( jumpIfNull==0 ); |
| 2679 | testcase( pParse->disableColCache==0 ); |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 2680 | sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, jumpIfNull^SQLITE_JUMPIFNULL); |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 2681 | pParse->disableColCache++; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2682 | sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 2683 | assert( pParse->disableColCache>0 ); |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 2684 | pParse->disableColCache--; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2685 | sqlite3VdbeResolveLabel(v, d2); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 2686 | break; |
| 2687 | } |
| 2688 | case TK_NOT: { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2689 | sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 2690 | break; |
| 2691 | } |
| 2692 | case TK_LT: |
| 2693 | case TK_LE: |
| 2694 | case TK_GT: |
| 2695 | case TK_GE: |
| 2696 | case TK_NE: |
| 2697 | case TK_EQ: { |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 2698 | testcase( op==TK_LT ); |
| 2699 | testcase( op==TK_LE ); |
| 2700 | testcase( op==TK_GT ); |
| 2701 | testcase( op==TK_GE ); |
| 2702 | testcase( op==TK_EQ ); |
| 2703 | testcase( op==TK_NE ); |
| 2704 | testcase( jumpIfNull==0 ); |
drh | da250ea | 2008-04-01 05:07:14 +0000 | [diff] [blame] | 2705 | codeCompareOperands(pParse, pExpr->pLeft, &r1, ®Free1, |
| 2706 | pExpr->pRight, &r2, ®Free2); |
drh | 3557335 | 2008-01-08 23:54:25 +0000 | [diff] [blame] | 2707 | codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2708 | r1, r2, dest, jumpIfNull); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 2709 | testcase( regFree1==0 ); |
| 2710 | testcase( regFree2==0 ); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 2711 | break; |
| 2712 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 2713 | case TK_ISNULL: |
| 2714 | case TK_NOTNULL: { |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 2715 | testcase( op==TK_ISNULL ); |
| 2716 | testcase( op==TK_NOTNULL ); |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2717 | r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); |
| 2718 | sqlite3VdbeAddOp2(v, op, r1, dest); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 2719 | testcase( regFree1==0 ); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 2720 | break; |
| 2721 | } |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 2722 | case TK_BETWEEN: { |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2723 | /* x BETWEEN y AND z |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 2724 | ** |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2725 | ** Is equivalent to |
| 2726 | ** |
| 2727 | ** x>=y AND x<=z |
| 2728 | ** |
| 2729 | ** Code it as such, taking care to do the common subexpression |
| 2730 | ** elementation of x. |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 2731 | */ |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2732 | Expr exprAnd; |
| 2733 | Expr compLeft; |
| 2734 | Expr compRight; |
| 2735 | Expr exprX; |
drh | be5c89a | 2004-07-26 00:31:09 +0000 | [diff] [blame] | 2736 | |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2737 | exprX = *pExpr->pLeft; |
| 2738 | exprAnd.op = TK_AND; |
| 2739 | exprAnd.pLeft = &compLeft; |
| 2740 | exprAnd.pRight = &compRight; |
| 2741 | compLeft.op = TK_GE; |
| 2742 | compLeft.pLeft = &exprX; |
| 2743 | compLeft.pRight = pExpr->pList->a[0].pExpr; |
| 2744 | compRight.op = TK_LE; |
| 2745 | compRight.pLeft = &exprX; |
| 2746 | compRight.pRight = pExpr->pList->a[1].pExpr; |
| 2747 | exprX.iTable = sqlite3ExprCodeTemp(pParse, &exprX, ®Free1); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 2748 | testcase( regFree1==0 ); |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2749 | exprX.op = TK_REGISTER; |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 2750 | testcase( jumpIfNull==0 ); |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2751 | sqlite3ExprIfFalse(pParse, &exprAnd, dest, jumpIfNull); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 2752 | break; |
| 2753 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 2754 | default: { |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2755 | r1 = sqlite3ExprCodeTemp(pParse, pExpr, ®Free1); |
| 2756 | sqlite3VdbeAddOp3(v, OP_IfNot, r1, dest, jumpIfNull!=0); |
drh | c5499be | 2008-04-01 15:06:33 +0000 | [diff] [blame] | 2757 | testcase( regFree1==0 ); |
| 2758 | testcase( jumpIfNull==0 ); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 2759 | break; |
| 2760 | } |
| 2761 | } |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 2762 | sqlite3ReleaseTempReg(pParse, regFree1); |
| 2763 | sqlite3ReleaseTempReg(pParse, regFree2); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 2764 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 2765 | |
| 2766 | /* |
| 2767 | ** Do a deep comparison of two expression trees. Return TRUE (non-zero) |
| 2768 | ** if they are identical and return FALSE if they differ in any way. |
drh | d40aab0 | 2007-02-24 15:29:03 +0000 | [diff] [blame] | 2769 | ** |
| 2770 | ** Sometimes this routine will return FALSE even if the two expressions |
| 2771 | ** really are equivalent. If we cannot prove that the expressions are |
| 2772 | ** identical, we return FALSE just to be safe. So if this routine |
| 2773 | ** returns false, then you do not really know for certain if the two |
| 2774 | ** expressions are the same. But if you get a TRUE return, then you |
| 2775 | ** can be sure the expressions are the same. In the places where |
| 2776 | ** this routine is used, it does not hurt to get an extra FALSE - that |
| 2777 | ** just might result in some slightly slower code. But returning |
| 2778 | ** an incorrect TRUE could lead to a malfunction. |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 2779 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2780 | int sqlite3ExprCompare(Expr *pA, Expr *pB){ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 2781 | int i; |
danielk1977 | 4b202ae | 2006-01-23 05:50:58 +0000 | [diff] [blame] | 2782 | if( pA==0||pB==0 ){ |
| 2783 | return pB==pA; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 2784 | } |
| 2785 | if( pA->op!=pB->op ) return 0; |
drh | fd35797 | 2005-09-09 01:33:19 +0000 | [diff] [blame] | 2786 | if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 0; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2787 | if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0; |
| 2788 | if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 2789 | if( pA->pList ){ |
| 2790 | if( pB->pList==0 ) return 0; |
| 2791 | if( pA->pList->nExpr!=pB->pList->nExpr ) return 0; |
| 2792 | for(i=0; i<pA->pList->nExpr; i++){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2793 | if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 2794 | return 0; |
| 2795 | } |
| 2796 | } |
| 2797 | }else if( pB->pList ){ |
| 2798 | return 0; |
| 2799 | } |
| 2800 | if( pA->pSelect || pB->pSelect ) return 0; |
drh | 2f2c01e | 2002-07-02 13:05:04 +0000 | [diff] [blame] | 2801 | if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0; |
drh | dd73521 | 2007-02-24 13:53:05 +0000 | [diff] [blame] | 2802 | if( pA->op!=TK_COLUMN && pA->token.z ){ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 2803 | if( pB->token.z==0 ) return 0; |
drh | 6977fea | 2002-10-22 23:38:04 +0000 | [diff] [blame] | 2804 | if( pB->token.n!=pA->token.n ) return 0; |
drh | 2646da7 | 2005-12-09 20:02:05 +0000 | [diff] [blame] | 2805 | if( sqlite3StrNICmp((char*)pA->token.z,(char*)pB->token.z,pB->token.n)!=0 ){ |
| 2806 | return 0; |
| 2807 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 2808 | } |
| 2809 | return 1; |
| 2810 | } |
| 2811 | |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 2812 | |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 2813 | /* |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 2814 | ** Add a new element to the pAggInfo->aCol[] array. Return the index of |
| 2815 | ** the new element. Return a negative number if malloc fails. |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 2816 | */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 2817 | static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){ |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 2818 | int i; |
drh | cf64372 | 2007-03-27 13:36:37 +0000 | [diff] [blame] | 2819 | pInfo->aCol = sqlite3ArrayAllocate( |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 2820 | db, |
drh | cf64372 | 2007-03-27 13:36:37 +0000 | [diff] [blame] | 2821 | pInfo->aCol, |
| 2822 | sizeof(pInfo->aCol[0]), |
| 2823 | 3, |
| 2824 | &pInfo->nColumn, |
| 2825 | &pInfo->nColumnAlloc, |
| 2826 | &i |
| 2827 | ); |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 2828 | return i; |
| 2829 | } |
| 2830 | |
| 2831 | /* |
| 2832 | ** Add a new element to the pAggInfo->aFunc[] array. Return the index of |
| 2833 | ** the new element. Return a negative number if malloc fails. |
| 2834 | */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 2835 | static int addAggInfoFunc(sqlite3 *db, AggInfo *pInfo){ |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 2836 | int i; |
drh | cf64372 | 2007-03-27 13:36:37 +0000 | [diff] [blame] | 2837 | pInfo->aFunc = sqlite3ArrayAllocate( |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 2838 | db, |
drh | cf64372 | 2007-03-27 13:36:37 +0000 | [diff] [blame] | 2839 | pInfo->aFunc, |
| 2840 | sizeof(pInfo->aFunc[0]), |
| 2841 | 3, |
| 2842 | &pInfo->nFunc, |
| 2843 | &pInfo->nFuncAlloc, |
| 2844 | &i |
| 2845 | ); |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 2846 | return i; |
| 2847 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 2848 | |
| 2849 | /* |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 2850 | ** This is the xExprCallback for a tree walker. It is used to |
| 2851 | ** implement sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 2852 | ** for additional information. |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 2853 | */ |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 2854 | static int analyzeAggregate(Walker *pWalker, Expr *pExpr){ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 2855 | int i; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 2856 | NameContext *pNC = pWalker->u.pNC; |
danielk1977 | a58fdfb | 2005-02-08 07:50:40 +0000 | [diff] [blame] | 2857 | Parse *pParse = pNC->pParse; |
| 2858 | SrcList *pSrcList = pNC->pSrcList; |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 2859 | AggInfo *pAggInfo = pNC->pAggInfo; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 2860 | |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 2861 | switch( pExpr->op ){ |
drh | 89c69d0 | 2007-01-04 01:20:28 +0000 | [diff] [blame] | 2862 | case TK_AGG_COLUMN: |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 2863 | case TK_COLUMN: { |
drh | 8b21389 | 2008-08-29 02:14:02 +0000 | [diff] [blame] | 2864 | testcase( pExpr->op==TK_AGG_COLUMN ); |
| 2865 | testcase( pExpr->op==TK_COLUMN ); |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 2866 | /* Check to see if the column is in one of the tables in the FROM |
| 2867 | ** clause of the aggregate query */ |
| 2868 | if( pSrcList ){ |
| 2869 | struct SrcList_item *pItem = pSrcList->a; |
| 2870 | for(i=0; i<pSrcList->nSrc; i++, pItem++){ |
| 2871 | struct AggInfo_col *pCol; |
| 2872 | if( pExpr->iTable==pItem->iCursor ){ |
| 2873 | /* If we reach this point, it means that pExpr refers to a table |
| 2874 | ** that is in the FROM clause of the aggregate query. |
| 2875 | ** |
| 2876 | ** Make an entry for the column in pAggInfo->aCol[] if there |
| 2877 | ** is not an entry there already. |
| 2878 | */ |
drh | 7f906d6 | 2007-03-12 23:48:52 +0000 | [diff] [blame] | 2879 | int k; |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 2880 | pCol = pAggInfo->aCol; |
drh | 7f906d6 | 2007-03-12 23:48:52 +0000 | [diff] [blame] | 2881 | for(k=0; k<pAggInfo->nColumn; k++, pCol++){ |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 2882 | if( pCol->iTable==pExpr->iTable && |
| 2883 | pCol->iColumn==pExpr->iColumn ){ |
| 2884 | break; |
| 2885 | } |
danielk1977 | a58fdfb | 2005-02-08 07:50:40 +0000 | [diff] [blame] | 2886 | } |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 2887 | if( (k>=pAggInfo->nColumn) |
| 2888 | && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0 |
| 2889 | ){ |
drh | 7f906d6 | 2007-03-12 23:48:52 +0000 | [diff] [blame] | 2890 | pCol = &pAggInfo->aCol[k]; |
danielk1977 | 0817d0d | 2007-02-14 09:19:36 +0000 | [diff] [blame] | 2891 | pCol->pTab = pExpr->pTab; |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 2892 | pCol->iTable = pExpr->iTable; |
| 2893 | pCol->iColumn = pExpr->iColumn; |
drh | 0a07c10 | 2008-01-03 18:03:08 +0000 | [diff] [blame] | 2894 | pCol->iMem = ++pParse->nMem; |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 2895 | pCol->iSorterColumn = -1; |
drh | 5774b80 | 2005-09-07 22:48:16 +0000 | [diff] [blame] | 2896 | pCol->pExpr = pExpr; |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 2897 | if( pAggInfo->pGroupBy ){ |
| 2898 | int j, n; |
| 2899 | ExprList *pGB = pAggInfo->pGroupBy; |
| 2900 | struct ExprList_item *pTerm = pGB->a; |
| 2901 | n = pGB->nExpr; |
| 2902 | for(j=0; j<n; j++, pTerm++){ |
| 2903 | Expr *pE = pTerm->pExpr; |
| 2904 | if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable && |
| 2905 | pE->iColumn==pExpr->iColumn ){ |
| 2906 | pCol->iSorterColumn = j; |
| 2907 | break; |
| 2908 | } |
| 2909 | } |
| 2910 | } |
| 2911 | if( pCol->iSorterColumn<0 ){ |
| 2912 | pCol->iSorterColumn = pAggInfo->nSortingColumn++; |
| 2913 | } |
| 2914 | } |
| 2915 | /* There is now an entry for pExpr in pAggInfo->aCol[] (either |
| 2916 | ** because it was there before or because we just created it). |
| 2917 | ** Convert the pExpr to be a TK_AGG_COLUMN referring to that |
| 2918 | ** pAggInfo->aCol[] entry. |
| 2919 | */ |
| 2920 | pExpr->pAggInfo = pAggInfo; |
| 2921 | pExpr->op = TK_AGG_COLUMN; |
drh | 7f906d6 | 2007-03-12 23:48:52 +0000 | [diff] [blame] | 2922 | pExpr->iAgg = k; |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 2923 | break; |
| 2924 | } /* endif pExpr->iTable==pItem->iCursor */ |
| 2925 | } /* end loop over pSrcList */ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 2926 | } |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 2927 | return WRC_Prune; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 2928 | } |
| 2929 | case TK_AGG_FUNCTION: { |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 2930 | /* The pNC->nDepth==0 test causes aggregate functions in subqueries |
| 2931 | ** to be ignored */ |
danielk1977 | a58fdfb | 2005-02-08 07:50:40 +0000 | [diff] [blame] | 2932 | if( pNC->nDepth==0 ){ |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 2933 | /* Check to see if pExpr is a duplicate of another aggregate |
| 2934 | ** function that is already in the pAggInfo structure |
| 2935 | */ |
| 2936 | struct AggInfo_func *pItem = pAggInfo->aFunc; |
| 2937 | for(i=0; i<pAggInfo->nFunc; i++, pItem++){ |
| 2938 | if( sqlite3ExprCompare(pItem->pExpr, pExpr) ){ |
danielk1977 | a58fdfb | 2005-02-08 07:50:40 +0000 | [diff] [blame] | 2939 | break; |
| 2940 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 2941 | } |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 2942 | if( i>=pAggInfo->nFunc ){ |
| 2943 | /* pExpr is original. Make a new entry in pAggInfo->aFunc[] |
| 2944 | */ |
danielk1977 | 14db266 | 2006-01-09 16:12:04 +0000 | [diff] [blame] | 2945 | u8 enc = ENC(pParse->db); |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 2946 | i = addAggInfoFunc(pParse->db, pAggInfo); |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 2947 | if( i>=0 ){ |
| 2948 | pItem = &pAggInfo->aFunc[i]; |
| 2949 | pItem->pExpr = pExpr; |
drh | 0a07c10 | 2008-01-03 18:03:08 +0000 | [diff] [blame] | 2950 | pItem->iMem = ++pParse->nMem; |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 2951 | pItem->pFunc = sqlite3FindFunction(pParse->db, |
drh | 2646da7 | 2005-12-09 20:02:05 +0000 | [diff] [blame] | 2952 | (char*)pExpr->token.z, pExpr->token.n, |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 2953 | pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0); |
drh | fd35797 | 2005-09-09 01:33:19 +0000 | [diff] [blame] | 2954 | if( pExpr->flags & EP_Distinct ){ |
| 2955 | pItem->iDistinct = pParse->nTab++; |
| 2956 | }else{ |
| 2957 | pItem->iDistinct = -1; |
| 2958 | } |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 2959 | } |
danielk1977 | a58fdfb | 2005-02-08 07:50:40 +0000 | [diff] [blame] | 2960 | } |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 2961 | /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry |
| 2962 | */ |
danielk1977 | a58fdfb | 2005-02-08 07:50:40 +0000 | [diff] [blame] | 2963 | pExpr->iAgg = i; |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 2964 | pExpr->pAggInfo = pAggInfo; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 2965 | return WRC_Prune; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 2966 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 2967 | } |
| 2968 | } |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 2969 | return WRC_Continue; |
| 2970 | } |
| 2971 | static int analyzeAggregatesInSelect(Walker *pWalker, Select *pSelect){ |
| 2972 | NameContext *pNC = pWalker->u.pNC; |
| 2973 | if( pNC->nDepth==0 ){ |
danielk1977 | a58fdfb | 2005-02-08 07:50:40 +0000 | [diff] [blame] | 2974 | pNC->nDepth++; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 2975 | sqlite3WalkSelect(pWalker, pSelect); |
danielk1977 | a58fdfb | 2005-02-08 07:50:40 +0000 | [diff] [blame] | 2976 | pNC->nDepth--; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 2977 | return WRC_Prune; |
| 2978 | }else{ |
| 2979 | return WRC_Continue; |
danielk1977 | a58fdfb | 2005-02-08 07:50:40 +0000 | [diff] [blame] | 2980 | } |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 2981 | } |
| 2982 | |
| 2983 | /* |
| 2984 | ** Analyze the given expression looking for aggregate functions and |
| 2985 | ** for variables that need to be added to the pParse->aAgg[] array. |
| 2986 | ** Make additional entries to the pParse->aAgg[] array as necessary. |
| 2987 | ** |
| 2988 | ** This routine should only be called after the expression has been |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 2989 | ** analyzed by sqlite3ResolveExprNames(). |
drh | 626a879 | 2005-01-17 22:08:19 +0000 | [diff] [blame] | 2990 | */ |
drh | d2b3e23 | 2008-01-23 14:51:49 +0000 | [diff] [blame] | 2991 | void sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){ |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 2992 | Walker w; |
| 2993 | w.xExprCallback = analyzeAggregate; |
| 2994 | w.xSelectCallback = analyzeAggregatesInSelect; |
| 2995 | w.u.pNC = pNC; |
| 2996 | sqlite3WalkExpr(&w, pExpr); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 2997 | } |
drh | 5d9a4af | 2005-08-30 00:54:01 +0000 | [diff] [blame] | 2998 | |
| 2999 | /* |
| 3000 | ** Call sqlite3ExprAnalyzeAggregates() for every expression in an |
| 3001 | ** expression list. Return the number of errors. |
| 3002 | ** |
| 3003 | ** If an error is found, the analysis is cut short. |
| 3004 | */ |
drh | d2b3e23 | 2008-01-23 14:51:49 +0000 | [diff] [blame] | 3005 | void sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){ |
drh | 5d9a4af | 2005-08-30 00:54:01 +0000 | [diff] [blame] | 3006 | struct ExprList_item *pItem; |
| 3007 | int i; |
drh | 5d9a4af | 2005-08-30 00:54:01 +0000 | [diff] [blame] | 3008 | if( pList ){ |
drh | d2b3e23 | 2008-01-23 14:51:49 +0000 | [diff] [blame] | 3009 | for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){ |
| 3010 | sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr); |
drh | 5d9a4af | 2005-08-30 00:54:01 +0000 | [diff] [blame] | 3011 | } |
| 3012 | } |
drh | 5d9a4af | 2005-08-30 00:54:01 +0000 | [diff] [blame] | 3013 | } |
drh | 892d317 | 2008-01-10 03:46:36 +0000 | [diff] [blame] | 3014 | |
| 3015 | /* |
| 3016 | ** Allocate or deallocate temporary use registers during code generation. |
| 3017 | */ |
| 3018 | int sqlite3GetTempReg(Parse *pParse){ |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 3019 | if( pParse->nTempReg==0 ){ |
drh | 892d317 | 2008-01-10 03:46:36 +0000 | [diff] [blame] | 3020 | return ++pParse->nMem; |
| 3021 | } |
danielk1977 | 2f425f6 | 2008-07-04 09:41:39 +0000 | [diff] [blame] | 3022 | return pParse->aTempReg[--pParse->nTempReg]; |
drh | 892d317 | 2008-01-10 03:46:36 +0000 | [diff] [blame] | 3023 | } |
| 3024 | void sqlite3ReleaseTempReg(Parse *pParse, int iReg){ |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 3025 | if( iReg && pParse->nTempReg<ArraySize(pParse->aTempReg) ){ |
danielk1977 | a7d8b85 | 2008-07-04 09:15:11 +0000 | [diff] [blame] | 3026 | sqlite3ExprWritableRegister(pParse, iReg, iReg); |
drh | 892d317 | 2008-01-10 03:46:36 +0000 | [diff] [blame] | 3027 | pParse->aTempReg[pParse->nTempReg++] = iReg; |
| 3028 | } |
| 3029 | } |
| 3030 | |
| 3031 | /* |
| 3032 | ** Allocate or deallocate a block of nReg consecutive registers |
| 3033 | */ |
| 3034 | int sqlite3GetTempRange(Parse *pParse, int nReg){ |
drh | e55cbd7 | 2008-03-31 23:48:03 +0000 | [diff] [blame] | 3035 | int i, n; |
| 3036 | i = pParse->iRangeReg; |
| 3037 | n = pParse->nRangeReg; |
| 3038 | if( nReg<=n && !usedAsColumnCache(pParse, i, i+n-1) ){ |
drh | 892d317 | 2008-01-10 03:46:36 +0000 | [diff] [blame] | 3039 | pParse->iRangeReg += nReg; |
| 3040 | pParse->nRangeReg -= nReg; |
| 3041 | }else{ |
| 3042 | i = pParse->nMem+1; |
| 3043 | pParse->nMem += nReg; |
| 3044 | } |
| 3045 | return i; |
| 3046 | } |
| 3047 | void sqlite3ReleaseTempRange(Parse *pParse, int iReg, int nReg){ |
| 3048 | if( nReg>pParse->nRangeReg ){ |
| 3049 | pParse->nRangeReg = nReg; |
| 3050 | pParse->iRangeReg = iReg; |
| 3051 | } |
| 3052 | } |