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