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