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