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