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