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