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