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