drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1 | /* |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2 | ** 2001 September 15 |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 3 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 6 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 7 | ** May you do good and not evil. |
| 8 | ** May you find forgiveness for yourself and forgive others. |
| 9 | ** May you share freely, never taking more than you give. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 10 | ** |
| 11 | ************************************************************************* |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 12 | ** This file contains routines used for analyzing expressions and |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 13 | ** for generating VDBE code that evaluates expressions in SQLite. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 14 | ** |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame^] | 15 | ** $Id: expr.c,v 1.44 2002/02/24 03:25:15 drh Exp $ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 16 | */ |
| 17 | #include "sqliteInt.h" |
| 18 | |
drh | a2e0004 | 2002-01-22 03:13:42 +0000 | [diff] [blame] | 19 | |
| 20 | /* |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 21 | ** Construct a new expression node and return a pointer to it. Memory |
| 22 | ** for this node is obtained from sqliteMalloc(). The calling function |
| 23 | ** is responsible for making sure the node eventually gets freed. |
| 24 | */ |
| 25 | Expr *sqliteExpr(int op, Expr *pLeft, Expr *pRight, Token *pToken){ |
| 26 | Expr *pNew; |
| 27 | pNew = sqliteMalloc( sizeof(Expr) ); |
| 28 | if( pNew==0 ){ |
| 29 | sqliteExprDelete(pLeft); |
| 30 | sqliteExprDelete(pRight); |
| 31 | return 0; |
| 32 | } |
| 33 | pNew->op = op; |
| 34 | pNew->pLeft = pLeft; |
| 35 | pNew->pRight = pRight; |
| 36 | if( pToken ){ |
| 37 | pNew->token = *pToken; |
| 38 | }else{ |
| 39 | pNew->token.z = 0; |
| 40 | pNew->token.n = 0; |
| 41 | } |
| 42 | if( pLeft && pRight ){ |
| 43 | sqliteExprSpan(pNew, &pLeft->span, &pRight->span); |
| 44 | }else{ |
| 45 | pNew->span = pNew->token; |
| 46 | } |
| 47 | return pNew; |
| 48 | } |
| 49 | |
| 50 | /* |
| 51 | ** Set the Expr.token field of the given expression to span all |
| 52 | ** text between the two given tokens. |
| 53 | */ |
| 54 | void sqliteExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){ |
| 55 | if( pExpr ){ |
| 56 | pExpr->span.z = pLeft->z; |
| 57 | pExpr->span.n = pRight->n + Addr(pRight->z) - Addr(pLeft->z); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | /* |
| 62 | ** Construct a new expression node for a function with multiple |
| 63 | ** arguments. |
| 64 | */ |
| 65 | Expr *sqliteExprFunction(ExprList *pList, Token *pToken){ |
| 66 | Expr *pNew; |
| 67 | pNew = sqliteMalloc( sizeof(Expr) ); |
| 68 | if( pNew==0 ){ |
| 69 | sqliteExprListDelete(pList); |
| 70 | return 0; |
| 71 | } |
| 72 | pNew->op = TK_FUNCTION; |
| 73 | pNew->pList = pList; |
| 74 | if( pToken ){ |
| 75 | pNew->token = *pToken; |
| 76 | }else{ |
| 77 | pNew->token.z = 0; |
| 78 | pNew->token.n = 0; |
| 79 | } |
| 80 | return pNew; |
| 81 | } |
| 82 | |
| 83 | /* |
drh | a2e0004 | 2002-01-22 03:13:42 +0000 | [diff] [blame] | 84 | ** Recursively delete an expression tree. |
| 85 | */ |
| 86 | void sqliteExprDelete(Expr *p){ |
| 87 | if( p==0 ) return; |
| 88 | if( p->op!=TK_AS ){ |
| 89 | if( p->pLeft ) sqliteExprDelete(p->pLeft); |
| 90 | if( p->pRight ) sqliteExprDelete(p->pRight); |
| 91 | } |
| 92 | if( p->pList ) sqliteExprListDelete(p->pList); |
| 93 | if( p->pSelect ) sqliteSelectDelete(p->pSelect); |
| 94 | sqliteFree(p); |
| 95 | } |
| 96 | |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 97 | /* |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 98 | ** The following group of functions are used to translate the string |
| 99 | ** pointers of tokens in expression from one buffer to another. |
| 100 | ** |
| 101 | ** Normally, the Expr.token.z and Expr.span.z fields point into the |
| 102 | ** original input buffer of an SQL statement. This is usually OK |
| 103 | ** since the SQL statement is executed and the expression is deleted |
| 104 | ** before the input buffer is freed. Making the tokens point to the |
| 105 | ** original input buffer saves many calls to malloc() and thus helps |
| 106 | ** the library to run faster. |
| 107 | ** |
| 108 | ** But sometimes we need an expression to persist past the time when |
| 109 | ** the input buffer is freed. (Example: The SELECT clause of a |
| 110 | ** CREATE VIEW statement contains expressions that must persist for |
| 111 | ** the life of the view.) When that happens we have to make a |
| 112 | ** persistent copy of the input buffer and translate the Expr.token.z |
| 113 | ** and Expr.span.z fields to point to the copy rather than the |
| 114 | ** original input buffer. The following group of routines to that |
| 115 | ** translation. |
| 116 | ** |
| 117 | ** The "offset" parameter is the distance from the original input buffer |
| 118 | ** to the persistent copy. These routines recursively walk the entire |
| 119 | ** expression tree and shift all tokens by "offset" amount. |
| 120 | ** |
| 121 | ** The work of figuring out the appropriate "offset" and making the |
| 122 | ** presistent copy of the input buffer is done by the calling routine. |
| 123 | */ |
| 124 | void sqliteExprMoveStrings(Expr *p, int offset){ |
| 125 | if( p==0 ) return; |
| 126 | if( p->token.z ) p->token.z += offset; |
| 127 | if( p->span.z ) p->span.z += offset; |
| 128 | if( p->pLeft ) sqliteExprMoveStrings(p->pLeft, offset); |
| 129 | if( p->pRight ) sqliteExprMoveStrings(p->pRight, offset); |
| 130 | if( p->pList ) sqliteExprListMoveStrings(p->pList, offset); |
| 131 | if( p->pSelect ) sqliteSelectMoveStrings(p->pSelect, offset); |
| 132 | } |
| 133 | void sqliteExprListMoveStrings(ExprList *pList, int offset){ |
| 134 | int i; |
| 135 | if( pList==0 ) return; |
| 136 | for(i=0; i<pList->nExpr; i++){ |
| 137 | sqliteExprMoveStrings(pList->a[i].pExpr, offset); |
| 138 | } |
| 139 | } |
| 140 | void sqliteSelectMoveStrings(Select *pSelect, int offset){ |
| 141 | if( pSelect==0 ) return; |
| 142 | sqliteExprListMoveStrings(pSelect->pEList, offset); |
| 143 | sqliteExprMoveStrings(pSelect->pWhere, offset); |
| 144 | sqliteExprListMoveStrings(pSelect->pGroupBy, offset); |
| 145 | sqliteExprMoveStrings(pSelect->pHaving, offset); |
| 146 | sqliteExprListMoveStrings(pSelect->pOrderBy, offset); |
| 147 | sqliteSelectMoveStrings(pSelect->pPrior, offset); |
| 148 | } |
| 149 | |
| 150 | /* |
| 151 | ** Add a new element to the end of an expression list. If pList is |
| 152 | ** initially NULL, then create a new expression list. |
| 153 | */ |
| 154 | ExprList *sqliteExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){ |
| 155 | int i; |
| 156 | if( pList==0 ){ |
| 157 | pList = sqliteMalloc( sizeof(ExprList) ); |
| 158 | if( pList==0 ){ |
| 159 | sqliteExprDelete(pExpr); |
| 160 | return 0; |
| 161 | } |
| 162 | } |
| 163 | if( (pList->nExpr & 7)==0 ){ |
| 164 | int n = pList->nExpr + 8; |
| 165 | struct ExprList_item *a; |
| 166 | a = sqliteRealloc(pList->a, n*sizeof(pList->a[0])); |
| 167 | if( a==0 ){ |
| 168 | sqliteExprDelete(pExpr); |
| 169 | return pList; |
| 170 | } |
| 171 | pList->a = a; |
| 172 | } |
| 173 | if( pExpr || pName ){ |
| 174 | i = pList->nExpr++; |
| 175 | pList->a[i].pExpr = pExpr; |
| 176 | pList->a[i].zName = 0; |
| 177 | if( pName ){ |
| 178 | sqliteSetNString(&pList->a[i].zName, pName->z, pName->n, 0); |
| 179 | sqliteDequote(pList->a[i].zName); |
| 180 | } |
| 181 | } |
| 182 | return pList; |
| 183 | } |
| 184 | |
| 185 | /* |
| 186 | ** Delete an entire expression list. |
| 187 | */ |
| 188 | void sqliteExprListDelete(ExprList *pList){ |
| 189 | int i; |
| 190 | if( pList==0 ) return; |
| 191 | for(i=0; i<pList->nExpr; i++){ |
| 192 | sqliteExprDelete(pList->a[i].pExpr); |
| 193 | sqliteFree(pList->a[i].zName); |
| 194 | } |
| 195 | sqliteFree(pList->a); |
| 196 | sqliteFree(pList); |
| 197 | } |
| 198 | |
| 199 | /* |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 200 | ** Walk an expression tree. Return 1 if the expression is constant |
| 201 | ** and 0 if it involves variables. |
| 202 | */ |
drh | 9208643 | 2002-01-22 14:11:29 +0000 | [diff] [blame] | 203 | int sqliteExprIsConstant(Expr *p){ |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 204 | switch( p->op ){ |
| 205 | case TK_ID: |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 206 | case TK_COLUMN: |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 207 | case TK_DOT: |
| 208 | return 0; |
drh | 9208643 | 2002-01-22 14:11:29 +0000 | [diff] [blame] | 209 | case TK_INTEGER: |
| 210 | case TK_FLOAT: |
| 211 | case TK_STRING: |
| 212 | return 1; |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 213 | default: { |
drh | 9208643 | 2002-01-22 14:11:29 +0000 | [diff] [blame] | 214 | if( p->pLeft && !sqliteExprIsConstant(p->pLeft) ) return 0; |
| 215 | if( p->pRight && !sqliteExprIsConstant(p->pRight) ) return 0; |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 216 | if( p->pList ){ |
| 217 | int i; |
| 218 | for(i=0; i<p->pList->nExpr; i++){ |
drh | 9208643 | 2002-01-22 14:11:29 +0000 | [diff] [blame] | 219 | if( !sqliteExprIsConstant(p->pList->a[i].pExpr) ) return 0; |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 220 | } |
| 221 | } |
drh | 9208643 | 2002-01-22 14:11:29 +0000 | [diff] [blame] | 222 | return p->pLeft!=0 || p->pRight!=0 || (p->pList && p->pList->nExpr>0); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 223 | } |
| 224 | } |
drh | 9208643 | 2002-01-22 14:11:29 +0000 | [diff] [blame] | 225 | return 0; |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | /* |
drh | 4794b98 | 2000-06-06 13:54:14 +0000 | [diff] [blame] | 229 | ** Walk the expression tree and process operators of the form: |
| 230 | ** |
| 231 | ** expr IN (SELECT ...) |
| 232 | ** |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 233 | ** These operators have to be processed before column names are |
drh | 4794b98 | 2000-06-06 13:54:14 +0000 | [diff] [blame] | 234 | ** resolved because each such operator increments pParse->nTab |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 235 | ** to reserve cursor numbers for its own use. But pParse->nTab |
drh | aacc543 | 2002-01-06 17:07:40 +0000 | [diff] [blame] | 236 | ** needs to be constant once we begin resolving column names. For |
| 237 | ** that reason, this procedure needs to be called on every expression |
| 238 | ** before sqliteExprResolveIds() is called on any expression. |
drh | 4794b98 | 2000-06-06 13:54:14 +0000 | [diff] [blame] | 239 | ** |
| 240 | ** Actually, the processing of IN-SELECT is only started by this |
| 241 | ** routine. This routine allocates a cursor number to the IN-SELECT |
| 242 | ** and then moves on. The code generation is done by |
| 243 | ** sqliteExprResolveIds() which must be called afterwards. |
| 244 | */ |
| 245 | void sqliteExprResolveInSelect(Parse *pParse, Expr *pExpr){ |
| 246 | if( pExpr==0 ) return; |
| 247 | if( pExpr->op==TK_IN && pExpr->pSelect!=0 ){ |
| 248 | pExpr->iTable = pParse->nTab++; |
| 249 | }else{ |
| 250 | if( pExpr->pLeft ) sqliteExprResolveInSelect(pParse, pExpr->pLeft); |
| 251 | if( pExpr->pRight ) sqliteExprResolveInSelect(pParse, pExpr->pRight); |
| 252 | if( pExpr->pList ){ |
| 253 | int i; |
| 254 | ExprList *pList = pExpr->pList; |
| 255 | for(i=0; i<pList->nExpr; i++){ |
| 256 | sqliteExprResolveInSelect(pParse, pList->a[i].pExpr); |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | /* |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 263 | ** Return TRUE if the given string is a row-id column name. |
| 264 | */ |
| 265 | static int sqliteIsRowid(const char *z){ |
| 266 | if( sqliteStrICmp(z, "_ROWID_")==0 ) return 1; |
| 267 | if( sqliteStrICmp(z, "ROWID")==0 ) return 1; |
| 268 | if( sqliteStrICmp(z, "OID")==0 ) return 1; |
| 269 | return 0; |
| 270 | } |
| 271 | |
| 272 | /* |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 273 | ** This routine walks an expression tree and resolves references to |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 274 | ** table columns. Nodes of the form ID.ID or ID resolve into an |
drh | aacc543 | 2002-01-06 17:07:40 +0000 | [diff] [blame] | 275 | ** index to the table in the table list and a column offset. The |
| 276 | ** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable |
| 277 | ** value is changed to the index of the referenced table in pTabList |
| 278 | ** plus the pParse->nTab value. This value will ultimately become the |
| 279 | ** VDBE cursor number for a cursor that is pointing into the referenced |
| 280 | ** table. The Expr.iColumn value is changed to the index of the column |
| 281 | ** of the referenced table. The Expr.iColumn value for the special |
| 282 | ** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an |
| 283 | ** alias for ROWID. |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 284 | ** |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 285 | ** We also check for instances of the IN operator. IN comes in two |
| 286 | ** forms: |
| 287 | ** |
| 288 | ** expr IN (exprlist) |
| 289 | ** and |
| 290 | ** expr IN (SELECT ...) |
| 291 | ** |
| 292 | ** The first form is handled by creating a set holding the list |
| 293 | ** of allowed values. The second form causes the SELECT to generate |
| 294 | ** a temporary table. |
| 295 | ** |
| 296 | ** This routine also looks for scalar SELECTs that are part of an expression. |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 297 | ** If it finds any, it generates code to write the value of that select |
| 298 | ** into a memory cell. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 299 | ** |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 300 | ** Unknown columns or tables provoke an error. The function returns |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 301 | ** the number of errors seen and leaves an error message on pParse->zErrMsg. |
| 302 | */ |
drh | a2e0004 | 2002-01-22 03:13:42 +0000 | [diff] [blame] | 303 | int sqliteExprResolveIds( |
| 304 | Parse *pParse, /* The parser context */ |
| 305 | IdList *pTabList, /* List of tables used to resolve column names */ |
| 306 | ExprList *pEList, /* List of expressions used to resolve "AS" */ |
| 307 | Expr *pExpr /* The expression to be analyzed. */ |
| 308 | ){ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 309 | if( pExpr==0 || pTabList==0 ) return 0; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 310 | switch( pExpr->op ){ |
drh | a2e0004 | 2002-01-22 03:13:42 +0000 | [diff] [blame] | 311 | /* A lone identifier. Try and match it as follows: |
| 312 | ** |
| 313 | ** 1. To the name of a column of one of the tables in pTabList |
| 314 | ** |
| 315 | ** 2. To the right side of an AS keyword in the column list of |
| 316 | ** a SELECT statement. (For example, match against 'x' in |
| 317 | ** "SELECT a+b AS 'x' FROM t1".) |
| 318 | ** |
| 319 | ** 3. One of the special names "ROWID", "OID", or "_ROWID_". |
| 320 | */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 321 | case TK_ID: { |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 322 | int cnt = 0; /* Number of matches */ |
| 323 | int i; /* Loop counter */ |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 324 | char *z; |
| 325 | assert( pExpr->token.z ); |
| 326 | z = sqliteStrNDup(pExpr->token.z, pExpr->token.n); |
drh | 2f4392f | 2002-02-14 21:42:51 +0000 | [diff] [blame] | 327 | sqliteDequote(z); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 328 | if( z==0 ) return 1; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 329 | for(i=0; i<pTabList->nId; i++){ |
| 330 | int j; |
| 331 | Table *pTab = pTabList->a[i].pTab; |
| 332 | if( pTab==0 ) continue; |
| 333 | for(j=0; j<pTab->nCol; j++){ |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 334 | if( sqliteStrICmp(pTab->aCol[j].zName, z)==0 ){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 335 | cnt++; |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 336 | pExpr->iTable = i + pParse->nTab; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 337 | if( j==pTab->iPKey ){ |
| 338 | /* Substitute the record number for the INTEGER PRIMARY KEY */ |
| 339 | pExpr->iColumn = -1; |
| 340 | }else{ |
| 341 | pExpr->iColumn = j; |
| 342 | } |
drh | a2e0004 | 2002-01-22 03:13:42 +0000 | [diff] [blame] | 343 | pExpr->op = TK_COLUMN; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 344 | } |
| 345 | } |
| 346 | } |
drh | a2e0004 | 2002-01-22 03:13:42 +0000 | [diff] [blame] | 347 | if( cnt==0 && pEList!=0 ){ |
| 348 | int j; |
| 349 | for(j=0; j<pEList->nExpr; j++){ |
| 350 | char *zAs = pEList->a[j].zName; |
| 351 | if( zAs!=0 && sqliteStrICmp(zAs, z)==0 ){ |
| 352 | cnt++; |
| 353 | assert( pExpr->pLeft==0 && pExpr->pRight==0 ); |
| 354 | pExpr->op = TK_AS; |
| 355 | pExpr->iColumn = j; |
| 356 | pExpr->pLeft = pEList->a[j].pExpr; |
| 357 | } |
| 358 | } |
| 359 | } |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 360 | if( cnt==0 && sqliteIsRowid(z) ){ |
| 361 | pExpr->iColumn = -1; |
| 362 | pExpr->iTable = pParse->nTab; |
| 363 | cnt = 1 + (pTabList->nId>1); |
drh | a2e0004 | 2002-01-22 03:13:42 +0000 | [diff] [blame] | 364 | pExpr->op = TK_COLUMN; |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 365 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 366 | sqliteFree(z); |
| 367 | if( cnt==0 ){ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 368 | sqliteSetNString(&pParse->zErrMsg, "no such column: ", -1, |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 369 | pExpr->token.z, pExpr->token.n, 0); |
| 370 | pParse->nErr++; |
| 371 | return 1; |
| 372 | }else if( cnt>1 ){ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 373 | sqliteSetNString(&pParse->zErrMsg, "ambiguous column name: ", -1, |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 374 | pExpr->token.z, pExpr->token.n, 0); |
| 375 | pParse->nErr++; |
| 376 | return 1; |
| 377 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 378 | break; |
| 379 | } |
| 380 | |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 381 | /* A table name and column name: ID.ID */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 382 | case TK_DOT: { |
drh | bed8690 | 2000-06-02 13:27:59 +0000 | [diff] [blame] | 383 | int cnt = 0; /* Number of matches */ |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 384 | int cntTab = 0; /* Number of matching tables */ |
drh | bed8690 | 2000-06-02 13:27:59 +0000 | [diff] [blame] | 385 | int i; /* Loop counter */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 386 | Expr *pLeft, *pRight; /* Left and right subbranches of the expr */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 387 | char *zLeft, *zRight; /* Text of an identifier */ |
| 388 | |
| 389 | pLeft = pExpr->pLeft; |
| 390 | pRight = pExpr->pRight; |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 391 | assert( pLeft && pLeft->op==TK_ID && pLeft->token.z ); |
| 392 | assert( pRight && pRight->op==TK_ID && pRight->token.z ); |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 393 | zLeft = sqliteStrNDup(pLeft->token.z, pLeft->token.n); |
| 394 | zRight = sqliteStrNDup(pRight->token.z, pRight->token.n); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 395 | if( zLeft==0 || zRight==0 ){ |
| 396 | sqliteFree(zLeft); |
| 397 | sqliteFree(zRight); |
| 398 | return 1; |
| 399 | } |
drh | 87c40e8 | 2001-07-23 14:33:02 +0000 | [diff] [blame] | 400 | sqliteDequote(zLeft); |
| 401 | sqliteDequote(zRight); |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 402 | pExpr->iTable = -1; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 403 | for(i=0; i<pTabList->nId; i++){ |
| 404 | int j; |
| 405 | char *zTab; |
| 406 | Table *pTab = pTabList->a[i].pTab; |
| 407 | if( pTab==0 ) continue; |
| 408 | if( pTabList->a[i].zAlias ){ |
| 409 | zTab = pTabList->a[i].zAlias; |
| 410 | }else{ |
| 411 | zTab = pTab->zName; |
| 412 | } |
| 413 | if( sqliteStrICmp(zTab, zLeft)!=0 ) continue; |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 414 | if( 0==(cntTab++) ) pExpr->iTable = i + pParse->nTab; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 415 | for(j=0; j<pTab->nCol; j++){ |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 416 | if( sqliteStrICmp(pTab->aCol[j].zName, zRight)==0 ){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 417 | cnt++; |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 418 | pExpr->iTable = i + pParse->nTab; |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 419 | if( j==pTab->iPKey ){ |
| 420 | /* Substitute the record number for the INTEGER PRIMARY KEY */ |
| 421 | pExpr->iColumn = -1; |
| 422 | }else{ |
| 423 | pExpr->iColumn = j; |
| 424 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 425 | } |
| 426 | } |
| 427 | } |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 428 | if( cnt==0 && cntTab==1 && sqliteIsRowid(zRight) ){ |
| 429 | cnt = 1; |
| 430 | pExpr->iColumn = -1; |
| 431 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 432 | sqliteFree(zLeft); |
| 433 | sqliteFree(zRight); |
| 434 | if( cnt==0 ){ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 435 | sqliteSetNString(&pParse->zErrMsg, "no such column: ", -1, |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 436 | pLeft->token.z, pLeft->token.n, ".", 1, |
| 437 | pRight->token.z, pRight->token.n, 0); |
| 438 | pParse->nErr++; |
| 439 | return 1; |
| 440 | }else if( cnt>1 ){ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 441 | sqliteSetNString(&pParse->zErrMsg, "ambiguous column name: ", -1, |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 442 | pLeft->token.z, pLeft->token.n, ".", 1, |
| 443 | pRight->token.z, pRight->token.n, 0); |
| 444 | pParse->nErr++; |
| 445 | return 1; |
| 446 | } |
| 447 | sqliteExprDelete(pLeft); |
| 448 | pExpr->pLeft = 0; |
| 449 | sqliteExprDelete(pRight); |
| 450 | pExpr->pRight = 0; |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 451 | pExpr->op = TK_COLUMN; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 452 | break; |
| 453 | } |
| 454 | |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 455 | case TK_IN: { |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 456 | Vdbe *v = sqliteGetVdbe(pParse); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 457 | if( v==0 ) return 1; |
drh | a2e0004 | 2002-01-22 03:13:42 +0000 | [diff] [blame] | 458 | if( sqliteExprResolveIds(pParse, pTabList, pEList, pExpr->pLeft) ){ |
drh | cfab11b | 2000-06-06 03:31:22 +0000 | [diff] [blame] | 459 | return 1; |
| 460 | } |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 461 | if( pExpr->pSelect ){ |
| 462 | /* Case 1: expr IN (SELECT ...) |
| 463 | ** |
| 464 | ** Generate code to write the results of the select into a temporary |
drh | 4794b98 | 2000-06-06 13:54:14 +0000 | [diff] [blame] | 465 | ** table. The cursor number of the temporary table has already |
| 466 | ** been put in iTable by sqliteExprResolveInSelect(). |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 467 | */ |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 468 | sqliteVdbeAddOp(v, OP_OpenTemp, pExpr->iTable, 1); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 469 | if( sqliteSelect(pParse, pExpr->pSelect, SRT_Set, pExpr->iTable) ); |
| 470 | }else if( pExpr->pList ){ |
| 471 | /* Case 2: expr IN (exprlist) |
| 472 | ** |
| 473 | ** Create a set to put the exprlist values in. The Set id is stored |
| 474 | ** in iTable. |
| 475 | */ |
| 476 | int i, iSet; |
| 477 | for(i=0; i<pExpr->pList->nExpr; i++){ |
| 478 | Expr *pE2 = pExpr->pList->a[i].pExpr; |
drh | 9208643 | 2002-01-22 14:11:29 +0000 | [diff] [blame] | 479 | if( !sqliteExprIsConstant(pE2) ){ |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 480 | sqliteSetString(&pParse->zErrMsg, |
| 481 | "right-hand side of IN operator must be constant", 0); |
| 482 | pParse->nErr++; |
| 483 | return 1; |
| 484 | } |
drh | 4794b98 | 2000-06-06 13:54:14 +0000 | [diff] [blame] | 485 | if( sqliteExprCheck(pParse, pE2, 0, 0) ){ |
| 486 | return 1; |
| 487 | } |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 488 | } |
| 489 | iSet = pExpr->iTable = pParse->nSet++; |
| 490 | for(i=0; i<pExpr->pList->nExpr; i++){ |
| 491 | Expr *pE2 = pExpr->pList->a[i].pExpr; |
| 492 | switch( pE2->op ){ |
| 493 | case TK_FLOAT: |
| 494 | case TK_INTEGER: |
| 495 | case TK_STRING: { |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 496 | int addr = sqliteVdbeAddOp(v, OP_SetInsert, iSet, 0); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 497 | assert( pE2->token.z ); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 498 | sqliteVdbeChangeP3(v, addr, pE2->token.z, pE2->token.n); |
| 499 | sqliteVdbeDequoteP3(v, addr); |
| 500 | break; |
| 501 | } |
| 502 | default: { |
| 503 | sqliteExprCode(pParse, pE2); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 504 | sqliteVdbeAddOp(v, OP_SetInsert, iSet, 0); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 505 | break; |
| 506 | } |
| 507 | } |
| 508 | } |
| 509 | } |
drh | cfab11b | 2000-06-06 03:31:22 +0000 | [diff] [blame] | 510 | break; |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 511 | } |
| 512 | |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 513 | case TK_SELECT: { |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 514 | /* This has to be a scalar SELECT. Generate code to put the |
| 515 | ** value of this select in a memory cell and record the number |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 516 | ** of the memory cell in iColumn. |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 517 | */ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 518 | pExpr->iColumn = pParse->nMem++; |
| 519 | if( sqliteSelect(pParse, pExpr->pSelect, SRT_Mem, pExpr->iColumn) ){ |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 520 | return 1; |
| 521 | } |
| 522 | break; |
| 523 | } |
| 524 | |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 525 | /* For all else, just recursively walk the tree */ |
| 526 | default: { |
drh | 4794b98 | 2000-06-06 13:54:14 +0000 | [diff] [blame] | 527 | if( pExpr->pLeft |
drh | a2e0004 | 2002-01-22 03:13:42 +0000 | [diff] [blame] | 528 | && sqliteExprResolveIds(pParse, pTabList, pEList, pExpr->pLeft) ){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 529 | return 1; |
| 530 | } |
| 531 | if( pExpr->pRight |
drh | a2e0004 | 2002-01-22 03:13:42 +0000 | [diff] [blame] | 532 | && sqliteExprResolveIds(pParse, pTabList, pEList, pExpr->pRight) ){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 533 | return 1; |
| 534 | } |
| 535 | if( pExpr->pList ){ |
| 536 | int i; |
| 537 | ExprList *pList = pExpr->pList; |
| 538 | for(i=0; i<pList->nExpr; i++){ |
drh | a2e0004 | 2002-01-22 03:13:42 +0000 | [diff] [blame] | 539 | if( sqliteExprResolveIds(pParse,pTabList,pEList,pList->a[i].pExpr) ){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 540 | return 1; |
| 541 | } |
| 542 | } |
| 543 | } |
| 544 | } |
| 545 | } |
| 546 | return 0; |
| 547 | } |
| 548 | |
| 549 | #if 0 /* NOT USED */ |
| 550 | /* |
| 551 | ** Compare a token against a string. Return TRUE if they match. |
| 552 | */ |
| 553 | static int sqliteTokenCmp(Token *pToken, const char *zStr){ |
| 554 | int n = strlen(zStr); |
| 555 | if( n!=pToken->n ) return 0; |
| 556 | return sqliteStrNICmp(pToken->z, zStr, n)==0; |
| 557 | } |
| 558 | #endif |
| 559 | |
| 560 | /* |
| 561 | ** Convert a function name into its integer identifier. Return the |
| 562 | ** identifier. Return FN_Unknown if the function name is unknown. |
| 563 | */ |
| 564 | int sqliteFuncId(Token *pToken){ |
| 565 | static const struct { |
| 566 | char *zName; |
| 567 | int len; |
| 568 | int id; |
| 569 | } aFunc[] = { |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 570 | { "count", 5, FN_Count }, |
| 571 | { "min", 3, FN_Min }, |
| 572 | { "max", 3, FN_Max }, |
| 573 | { "sum", 3, FN_Sum }, |
| 574 | { "avg", 3, FN_Avg }, |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 575 | { "length", 6, FN_Length }, |
| 576 | { "substr", 6, FN_Substr }, |
| 577 | { "abs", 3, FN_Abs }, |
| 578 | { "round", 5, FN_Round }, |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 579 | }; |
| 580 | int i; |
| 581 | for(i=0; i<ArraySize(aFunc); i++){ |
| 582 | if( aFunc[i].len==pToken->n |
| 583 | && sqliteStrNICmp(pToken->z, aFunc[i].zName, aFunc[i].len)==0 ){ |
| 584 | return aFunc[i].id; |
| 585 | } |
| 586 | } |
| 587 | return FN_Unknown; |
| 588 | } |
| 589 | |
| 590 | /* |
| 591 | ** Error check the functions in an expression. Make sure all |
| 592 | ** function names are recognized and all functions have the correct |
| 593 | ** number of arguments. Leave an error message in pParse->zErrMsg |
| 594 | ** if anything is amiss. Return the number of errors. |
| 595 | ** |
| 596 | ** if pIsAgg is not null and this expression is an aggregate function |
| 597 | ** (like count(*) or max(value)) then write a 1 into *pIsAgg. |
| 598 | */ |
| 599 | int sqliteExprCheck(Parse *pParse, Expr *pExpr, int allowAgg, int *pIsAgg){ |
| 600 | int nErr = 0; |
| 601 | if( pExpr==0 ) return 0; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 602 | switch( pExpr->op ){ |
| 603 | case TK_FUNCTION: { |
| 604 | int id = sqliteFuncId(&pExpr->token); |
| 605 | int n = pExpr->pList ? pExpr->pList->nExpr : 0; |
| 606 | int no_such_func = 0; |
| 607 | int too_many_args = 0; |
| 608 | int too_few_args = 0; |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 609 | int wrong_num_args = 0; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 610 | int is_agg = 0; |
| 611 | int i; |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 612 | pExpr->iColumn = id; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 613 | switch( id ){ |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 614 | case FN_Unknown: { |
| 615 | UserFunc *pUser = sqliteFindUserFunction(pParse->db, |
| 616 | pExpr->token.z, pExpr->token.n, n, 0); |
| 617 | if( pUser==0 ){ |
| 618 | pUser = sqliteFindUserFunction(pParse->db, |
| 619 | pExpr->token.z, pExpr->token.n, -1, 0); |
| 620 | if( pUser==0 ){ |
| 621 | no_such_func = 1; |
| 622 | }else{ |
| 623 | wrong_num_args = 1; |
| 624 | } |
| 625 | }else{ |
| 626 | is_agg = pUser->xFunc==0; |
| 627 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 628 | break; |
| 629 | } |
| 630 | case FN_Count: { |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 631 | too_many_args = n>1; |
| 632 | is_agg = 1; |
| 633 | break; |
| 634 | } |
| 635 | case FN_Max: |
| 636 | case FN_Min: { |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 637 | too_few_args = n<1; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 638 | is_agg = n==1; |
| 639 | break; |
| 640 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 641 | case FN_Avg: |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 642 | case FN_Sum: { |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 643 | too_many_args = n>1; |
| 644 | too_few_args = n<1; |
| 645 | is_agg = 1; |
| 646 | break; |
| 647 | } |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 648 | case FN_Abs: |
drh | 6ec2733 | 2000-08-28 15:51:43 +0000 | [diff] [blame] | 649 | case FN_Length: { |
| 650 | too_few_args = n<1; |
| 651 | too_many_args = n>1; |
| 652 | break; |
| 653 | } |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 654 | case FN_Round: { |
| 655 | too_few_args = n<1; |
| 656 | too_many_args = n>2; |
| 657 | break; |
| 658 | } |
drh | 6ec2733 | 2000-08-28 15:51:43 +0000 | [diff] [blame] | 659 | case FN_Substr: { |
| 660 | too_few_args = n<3; |
| 661 | too_many_args = n>3; |
| 662 | break; |
| 663 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 664 | default: break; |
| 665 | } |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 666 | if( is_agg && !allowAgg ){ |
| 667 | sqliteSetNString(&pParse->zErrMsg, "misuse of aggregate function ", -1, |
| 668 | pExpr->token.z, pExpr->token.n, "()", 2, 0); |
| 669 | pParse->nErr++; |
| 670 | nErr++; |
| 671 | is_agg = 0; |
| 672 | }else if( no_such_func ){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 673 | sqliteSetNString(&pParse->zErrMsg, "no such function: ", -1, |
| 674 | pExpr->token.z, pExpr->token.n, 0); |
| 675 | pParse->nErr++; |
| 676 | nErr++; |
| 677 | }else if( too_many_args ){ |
| 678 | sqliteSetNString(&pParse->zErrMsg, "too many arguments to function ",-1, |
| 679 | pExpr->token.z, pExpr->token.n, "()", 2, 0); |
| 680 | pParse->nErr++; |
| 681 | nErr++; |
| 682 | }else if( too_few_args ){ |
| 683 | sqliteSetNString(&pParse->zErrMsg, "too few arguments to function ",-1, |
| 684 | pExpr->token.z, pExpr->token.n, "()", 2, 0); |
| 685 | pParse->nErr++; |
| 686 | nErr++; |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 687 | }else if( wrong_num_args ){ |
| 688 | sqliteSetNString(&pParse->zErrMsg, |
| 689 | "wrong number of arguments to function ",-1, |
| 690 | pExpr->token.z, pExpr->token.n, "()", 2, 0); |
| 691 | pParse->nErr++; |
| 692 | nErr++; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 693 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 694 | if( is_agg ) pExpr->op = TK_AGG_FUNCTION; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 695 | if( is_agg && pIsAgg ) *pIsAgg = 1; |
| 696 | for(i=0; nErr==0 && i<n; i++){ |
drh | 4cfa793 | 2000-06-08 15:10:46 +0000 | [diff] [blame] | 697 | nErr = sqliteExprCheck(pParse, pExpr->pList->a[i].pExpr, |
| 698 | allowAgg && !is_agg, pIsAgg); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 699 | } |
| 700 | } |
| 701 | default: { |
| 702 | if( pExpr->pLeft ){ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 703 | nErr = sqliteExprCheck(pParse, pExpr->pLeft, allowAgg, pIsAgg); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 704 | } |
| 705 | if( nErr==0 && pExpr->pRight ){ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 706 | nErr = sqliteExprCheck(pParse, pExpr->pRight, allowAgg, pIsAgg); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 707 | } |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 708 | if( nErr==0 && pExpr->pList ){ |
| 709 | int n = pExpr->pList->nExpr; |
| 710 | int i; |
| 711 | for(i=0; nErr==0 && i<n; i++){ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 712 | Expr *pE2 = pExpr->pList->a[i].pExpr; |
| 713 | nErr = sqliteExprCheck(pParse, pE2, allowAgg, pIsAgg); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 714 | } |
| 715 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 716 | break; |
| 717 | } |
| 718 | } |
| 719 | return nErr; |
| 720 | } |
| 721 | |
| 722 | /* |
| 723 | ** Generate code into the current Vdbe to evaluate the given |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 724 | ** expression and leave the result on the top of stack. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 725 | */ |
| 726 | void sqliteExprCode(Parse *pParse, Expr *pExpr){ |
| 727 | Vdbe *v = pParse->pVdbe; |
| 728 | int op; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 729 | if( v==0 || pExpr==0 ) return; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 730 | switch( pExpr->op ){ |
| 731 | case TK_PLUS: op = OP_Add; break; |
| 732 | case TK_MINUS: op = OP_Subtract; break; |
| 733 | case TK_STAR: op = OP_Multiply; break; |
| 734 | case TK_SLASH: op = OP_Divide; break; |
| 735 | case TK_AND: op = OP_And; break; |
| 736 | case TK_OR: op = OP_Or; break; |
| 737 | case TK_LT: op = OP_Lt; break; |
| 738 | case TK_LE: op = OP_Le; break; |
| 739 | case TK_GT: op = OP_Gt; break; |
| 740 | case TK_GE: op = OP_Ge; break; |
| 741 | case TK_NE: op = OP_Ne; break; |
| 742 | case TK_EQ: op = OP_Eq; break; |
| 743 | case TK_LIKE: op = OP_Like; break; |
| 744 | case TK_GLOB: op = OP_Glob; break; |
| 745 | case TK_ISNULL: op = OP_IsNull; break; |
| 746 | case TK_NOTNULL: op = OP_NotNull; break; |
| 747 | case TK_NOT: op = OP_Not; break; |
| 748 | case TK_UMINUS: op = OP_Negative; break; |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 749 | case TK_BITAND: op = OP_BitAnd; break; |
| 750 | case TK_BITOR: op = OP_BitOr; break; |
| 751 | case TK_BITNOT: op = OP_BitNot; break; |
| 752 | case TK_LSHIFT: op = OP_ShiftLeft; break; |
| 753 | case TK_RSHIFT: op = OP_ShiftRight; break; |
| 754 | case TK_REM: op = OP_Remainder; break; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 755 | default: break; |
| 756 | } |
| 757 | switch( pExpr->op ){ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 758 | case TK_COLUMN: { |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 759 | if( pParse->useAgg ){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 760 | sqliteVdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg); |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 761 | }else if( pExpr->iColumn>=0 ){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 762 | sqliteVdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn); |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 763 | }else{ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 764 | sqliteVdbeAddOp(v, OP_Recno, pExpr->iTable, 0); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 765 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 766 | break; |
| 767 | } |
drh | ef6764a | 2002-01-30 04:32:00 +0000 | [diff] [blame] | 768 | case TK_FLOAT: |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 769 | case TK_INTEGER: { |
drh | 7a7c739 | 2001-11-24 00:31:46 +0000 | [diff] [blame] | 770 | sqliteVdbeAddOp(v, OP_String, 0, 0); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 771 | assert( pExpr->token.z ); |
drh | 7a7c739 | 2001-11-24 00:31:46 +0000 | [diff] [blame] | 772 | sqliteVdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 773 | break; |
| 774 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 775 | case TK_STRING: { |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 776 | int addr = sqliteVdbeAddOp(v, OP_String, 0, 0); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 777 | assert( pExpr->token.z ); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 778 | sqliteVdbeChangeP3(v, addr, pExpr->token.z, pExpr->token.n); |
| 779 | sqliteVdbeDequoteP3(v, addr); |
| 780 | break; |
| 781 | } |
| 782 | case TK_NULL: { |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 783 | sqliteVdbeAddOp(v, OP_String, 0, 0); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 784 | break; |
| 785 | } |
| 786 | case TK_AND: |
| 787 | case TK_OR: |
| 788 | case TK_PLUS: |
| 789 | case TK_STAR: |
| 790 | case TK_MINUS: |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 791 | case TK_REM: |
| 792 | case TK_BITAND: |
| 793 | case TK_BITOR: |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 794 | case TK_SLASH: { |
| 795 | sqliteExprCode(pParse, pExpr->pLeft); |
| 796 | sqliteExprCode(pParse, pExpr->pRight); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 797 | sqliteVdbeAddOp(v, op, 0, 0); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 798 | break; |
| 799 | } |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 800 | case TK_LSHIFT: |
| 801 | case TK_RSHIFT: { |
| 802 | sqliteExprCode(pParse, pExpr->pRight); |
| 803 | sqliteExprCode(pParse, pExpr->pLeft); |
| 804 | sqliteVdbeAddOp(v, op, 0, 0); |
| 805 | break; |
| 806 | } |
drh | 0040077 | 2000-06-16 20:51:26 +0000 | [diff] [blame] | 807 | case TK_CONCAT: { |
| 808 | sqliteExprCode(pParse, pExpr->pLeft); |
| 809 | sqliteExprCode(pParse, pExpr->pRight); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 810 | sqliteVdbeAddOp(v, OP_Concat, 2, 0); |
drh | 0040077 | 2000-06-16 20:51:26 +0000 | [diff] [blame] | 811 | break; |
| 812 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 813 | case TK_LT: |
| 814 | case TK_LE: |
| 815 | case TK_GT: |
| 816 | case TK_GE: |
| 817 | case TK_NE: |
| 818 | case TK_EQ: |
| 819 | case TK_LIKE: |
| 820 | case TK_GLOB: { |
| 821 | int dest; |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 822 | sqliteVdbeAddOp(v, OP_Integer, 1, 0); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 823 | sqliteExprCode(pParse, pExpr->pLeft); |
| 824 | sqliteExprCode(pParse, pExpr->pRight); |
| 825 | dest = sqliteVdbeCurrentAddr(v) + 2; |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 826 | sqliteVdbeAddOp(v, op, 0, dest); |
| 827 | sqliteVdbeAddOp(v, OP_AddImm, -1, 0); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 828 | break; |
| 829 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 830 | case TK_UMINUS: { |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 831 | assert( pExpr->pLeft ); |
drh | 7a7c739 | 2001-11-24 00:31:46 +0000 | [diff] [blame] | 832 | if( pExpr->pLeft->op==TK_FLOAT || pExpr->pLeft->op==TK_INTEGER ){ |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 833 | Token *p = &pExpr->pLeft->token; |
| 834 | char *z = sqliteMalloc( p->n + 2 ); |
| 835 | sprintf(z, "-%.*s", p->n, p->z); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 836 | sqliteVdbeAddOp(v, OP_String, 0, 0); |
| 837 | sqliteVdbeChangeP3(v, -1, z, p->n+1); |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 838 | sqliteFree(z); |
| 839 | break; |
| 840 | } |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 841 | /* Fall through into TK_NOT */ |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 842 | } |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 843 | case TK_BITNOT: |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 844 | case TK_NOT: { |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 845 | sqliteExprCode(pParse, pExpr->pLeft); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 846 | sqliteVdbeAddOp(v, op, 0, 0); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 847 | break; |
| 848 | } |
| 849 | case TK_ISNULL: |
| 850 | case TK_NOTNULL: { |
| 851 | int dest; |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 852 | sqliteVdbeAddOp(v, OP_Integer, 1, 0); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 853 | sqliteExprCode(pParse, pExpr->pLeft); |
| 854 | dest = sqliteVdbeCurrentAddr(v) + 2; |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 855 | sqliteVdbeAddOp(v, op, 0, dest); |
| 856 | sqliteVdbeAddOp(v, OP_AddImm, -1, 0); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 857 | break; |
| 858 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 859 | case TK_AGG_FUNCTION: { |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 860 | sqliteVdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg); |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 861 | if( pExpr->iColumn==FN_Avg ){ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 862 | assert( pParse->iAggCount>=0 && pParse->iAggCount<pParse->nAgg ); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 863 | sqliteVdbeAddOp(v, OP_AggGet, 0, pParse->iAggCount); |
| 864 | sqliteVdbeAddOp(v, OP_Divide, 0, 0); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 865 | } |
| 866 | break; |
| 867 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 868 | case TK_FUNCTION: { |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 869 | int id = pExpr->iColumn; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 870 | int op; |
| 871 | int i; |
| 872 | ExprList *pList = pExpr->pList; |
drh | 6ec2733 | 2000-08-28 15:51:43 +0000 | [diff] [blame] | 873 | switch( id ){ |
drh | 6ec2733 | 2000-08-28 15:51:43 +0000 | [diff] [blame] | 874 | case FN_Min: |
| 875 | case FN_Max: { |
| 876 | op = id==FN_Min ? OP_Min : OP_Max; |
| 877 | for(i=0; i<pList->nExpr; i++){ |
| 878 | sqliteExprCode(pParse, pList->a[i].pExpr); |
| 879 | if( i>0 ){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 880 | sqliteVdbeAddOp(v, op, 0, 0); |
drh | 6ec2733 | 2000-08-28 15:51:43 +0000 | [diff] [blame] | 881 | } |
| 882 | } |
| 883 | break; |
| 884 | } |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 885 | case FN_Abs: { |
| 886 | sqliteExprCode(pParse, pList->a[0].pExpr); |
| 887 | sqliteVdbeAddOp(v, OP_AbsValue, 0, 0); |
| 888 | break; |
| 889 | } |
| 890 | case FN_Round: { |
| 891 | if( pList->nExpr==2 ){ |
| 892 | sqliteExprCode(pParse, pList->a[1].pExpr); |
| 893 | }else{ |
| 894 | sqliteVdbeAddOp(v, OP_Integer, 0, 0); |
| 895 | } |
| 896 | sqliteExprCode(pParse, pList->a[0].pExpr); |
| 897 | sqliteVdbeAddOp(v, OP_Precision, 0, 0); |
| 898 | break; |
| 899 | } |
drh | 6ec2733 | 2000-08-28 15:51:43 +0000 | [diff] [blame] | 900 | case FN_Length: { |
| 901 | sqliteExprCode(pParse, pList->a[0].pExpr); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 902 | sqliteVdbeAddOp(v, OP_Strlen, 0, 0); |
drh | 6ec2733 | 2000-08-28 15:51:43 +0000 | [diff] [blame] | 903 | break; |
| 904 | } |
| 905 | case FN_Substr: { |
| 906 | for(i=0; i<pList->nExpr; i++){ |
| 907 | sqliteExprCode(pParse, pList->a[i].pExpr); |
| 908 | } |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 909 | sqliteVdbeAddOp(v, OP_Substr, 0, 0); |
drh | 6ec2733 | 2000-08-28 15:51:43 +0000 | [diff] [blame] | 910 | break; |
| 911 | } |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 912 | case FN_Unknown: { |
| 913 | UserFunc *pUser; |
| 914 | pUser = sqliteFindUserFunction(pParse->db, |
| 915 | pExpr->token.z, pExpr->token.n, pList->nExpr, 0); |
| 916 | assert( pUser!=0 ); |
| 917 | for(i=0; i<pList->nExpr; i++){ |
| 918 | sqliteExprCode(pParse, pList->a[i].pExpr); |
| 919 | } |
| 920 | sqliteVdbeAddOp(v, OP_UserFunc, pList->nExpr, 0); |
| 921 | sqliteVdbeChangeP3(v, -1, (char*)pUser->xFunc, P3_POINTER); |
| 922 | break; |
| 923 | } |
drh | 6ec2733 | 2000-08-28 15:51:43 +0000 | [diff] [blame] | 924 | default: { |
| 925 | /* Can't happen! */ |
| 926 | break; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 927 | } |
| 928 | } |
| 929 | break; |
| 930 | } |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 931 | case TK_SELECT: { |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 932 | sqliteVdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0); |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 933 | break; |
| 934 | } |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 935 | case TK_IN: { |
| 936 | int addr; |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 937 | sqliteVdbeAddOp(v, OP_Integer, 1, 0); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 938 | sqliteExprCode(pParse, pExpr->pLeft); |
| 939 | addr = sqliteVdbeCurrentAddr(v); |
| 940 | if( pExpr->pSelect ){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 941 | sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, addr+2); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 942 | }else{ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 943 | sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, addr+2); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 944 | } |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 945 | sqliteVdbeAddOp(v, OP_AddImm, -1, 0); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 946 | break; |
| 947 | } |
| 948 | case TK_BETWEEN: { |
| 949 | int lbl = sqliteVdbeMakeLabel(v); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 950 | sqliteVdbeAddOp(v, OP_Integer, 0, 0); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 951 | sqliteExprIfFalse(pParse, pExpr, lbl); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 952 | sqliteVdbeAddOp(v, OP_AddImm, 1, 0); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 953 | sqliteVdbeResolveLabel(v, lbl); |
| 954 | break; |
| 955 | } |
drh | a2e0004 | 2002-01-22 03:13:42 +0000 | [diff] [blame] | 956 | case TK_AS: { |
| 957 | sqliteExprCode(pParse, pExpr->pLeft); |
| 958 | break; |
| 959 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 960 | } |
| 961 | return; |
| 962 | } |
| 963 | |
| 964 | /* |
| 965 | ** Generate code for a boolean expression such that a jump is made |
| 966 | ** to the label "dest" if the expression is true but execution |
| 967 | ** continues straight thru if the expression is false. |
| 968 | */ |
| 969 | void sqliteExprIfTrue(Parse *pParse, Expr *pExpr, int dest){ |
| 970 | Vdbe *v = pParse->pVdbe; |
| 971 | int op = 0; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 972 | if( v==0 || pExpr==0 ) return; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 973 | switch( pExpr->op ){ |
| 974 | case TK_LT: op = OP_Lt; break; |
| 975 | case TK_LE: op = OP_Le; break; |
| 976 | case TK_GT: op = OP_Gt; break; |
| 977 | case TK_GE: op = OP_Ge; break; |
| 978 | case TK_NE: op = OP_Ne; break; |
| 979 | case TK_EQ: op = OP_Eq; break; |
| 980 | case TK_LIKE: op = OP_Like; break; |
| 981 | case TK_GLOB: op = OP_Glob; break; |
| 982 | case TK_ISNULL: op = OP_IsNull; break; |
| 983 | case TK_NOTNULL: op = OP_NotNull; break; |
| 984 | default: break; |
| 985 | } |
| 986 | switch( pExpr->op ){ |
| 987 | case TK_AND: { |
| 988 | int d2 = sqliteVdbeMakeLabel(v); |
| 989 | sqliteExprIfFalse(pParse, pExpr->pLeft, d2); |
| 990 | sqliteExprIfTrue(pParse, pExpr->pRight, dest); |
| 991 | sqliteVdbeResolveLabel(v, d2); |
| 992 | break; |
| 993 | } |
| 994 | case TK_OR: { |
| 995 | sqliteExprIfTrue(pParse, pExpr->pLeft, dest); |
| 996 | sqliteExprIfTrue(pParse, pExpr->pRight, dest); |
| 997 | break; |
| 998 | } |
| 999 | case TK_NOT: { |
| 1000 | sqliteExprIfFalse(pParse, pExpr->pLeft, dest); |
| 1001 | break; |
| 1002 | } |
| 1003 | case TK_LT: |
| 1004 | case TK_LE: |
| 1005 | case TK_GT: |
| 1006 | case TK_GE: |
| 1007 | case TK_NE: |
| 1008 | case TK_EQ: |
| 1009 | case TK_LIKE: |
| 1010 | case TK_GLOB: { |
| 1011 | sqliteExprCode(pParse, pExpr->pLeft); |
| 1012 | sqliteExprCode(pParse, pExpr->pRight); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1013 | sqliteVdbeAddOp(v, op, 0, dest); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1014 | break; |
| 1015 | } |
| 1016 | case TK_ISNULL: |
| 1017 | case TK_NOTNULL: { |
| 1018 | sqliteExprCode(pParse, pExpr->pLeft); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1019 | sqliteVdbeAddOp(v, op, 0, dest); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1020 | break; |
| 1021 | } |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1022 | case TK_IN: { |
drh | cfab11b | 2000-06-06 03:31:22 +0000 | [diff] [blame] | 1023 | sqliteExprCode(pParse, pExpr->pLeft); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1024 | if( pExpr->pSelect ){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1025 | sqliteVdbeAddOp(v, OP_Found, pExpr->iTable, dest); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1026 | }else{ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1027 | sqliteVdbeAddOp(v, OP_SetFound, pExpr->iTable, dest); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1028 | } |
| 1029 | break; |
| 1030 | } |
| 1031 | case TK_BETWEEN: { |
| 1032 | int lbl = sqliteVdbeMakeLabel(v); |
| 1033 | sqliteExprCode(pParse, pExpr->pLeft); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1034 | sqliteVdbeAddOp(v, OP_Dup, 0, 0); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1035 | sqliteExprCode(pParse, pExpr->pList->a[0].pExpr); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1036 | sqliteVdbeAddOp(v, OP_Lt, 0, lbl); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1037 | sqliteExprCode(pParse, pExpr->pList->a[1].pExpr); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1038 | sqliteVdbeAddOp(v, OP_Le, 0, dest); |
| 1039 | sqliteVdbeAddOp(v, OP_Integer, 0, 0); |
| 1040 | sqliteVdbeResolveLabel(v, lbl); |
| 1041 | sqliteVdbeAddOp(v, OP_Pop, 1, 0); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1042 | break; |
| 1043 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1044 | default: { |
| 1045 | sqliteExprCode(pParse, pExpr); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1046 | sqliteVdbeAddOp(v, OP_If, 0, dest); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1047 | break; |
| 1048 | } |
| 1049 | } |
| 1050 | } |
| 1051 | |
| 1052 | /* |
drh | 66b89c8 | 2000-11-28 20:47:17 +0000 | [diff] [blame] | 1053 | ** Generate code for a boolean expression such that a jump is made |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1054 | ** to the label "dest" if the expression is false but execution |
| 1055 | ** continues straight thru if the expression is true. |
| 1056 | */ |
| 1057 | void sqliteExprIfFalse(Parse *pParse, Expr *pExpr, int dest){ |
| 1058 | Vdbe *v = pParse->pVdbe; |
| 1059 | int op = 0; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1060 | if( v==0 || pExpr==0 ) return; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1061 | switch( pExpr->op ){ |
| 1062 | case TK_LT: op = OP_Ge; break; |
| 1063 | case TK_LE: op = OP_Gt; break; |
| 1064 | case TK_GT: op = OP_Le; break; |
| 1065 | case TK_GE: op = OP_Lt; break; |
| 1066 | case TK_NE: op = OP_Eq; break; |
| 1067 | case TK_EQ: op = OP_Ne; break; |
| 1068 | case TK_LIKE: op = OP_Like; break; |
| 1069 | case TK_GLOB: op = OP_Glob; break; |
| 1070 | case TK_ISNULL: op = OP_NotNull; break; |
| 1071 | case TK_NOTNULL: op = OP_IsNull; break; |
| 1072 | default: break; |
| 1073 | } |
| 1074 | switch( pExpr->op ){ |
| 1075 | case TK_AND: { |
| 1076 | sqliteExprIfFalse(pParse, pExpr->pLeft, dest); |
| 1077 | sqliteExprIfFalse(pParse, pExpr->pRight, dest); |
| 1078 | break; |
| 1079 | } |
| 1080 | case TK_OR: { |
| 1081 | int d2 = sqliteVdbeMakeLabel(v); |
| 1082 | sqliteExprIfTrue(pParse, pExpr->pLeft, d2); |
| 1083 | sqliteExprIfFalse(pParse, pExpr->pRight, dest); |
| 1084 | sqliteVdbeResolveLabel(v, d2); |
| 1085 | break; |
| 1086 | } |
| 1087 | case TK_NOT: { |
| 1088 | sqliteExprIfTrue(pParse, pExpr->pLeft, dest); |
| 1089 | break; |
| 1090 | } |
| 1091 | case TK_LT: |
| 1092 | case TK_LE: |
| 1093 | case TK_GT: |
| 1094 | case TK_GE: |
| 1095 | case TK_NE: |
| 1096 | case TK_EQ: { |
| 1097 | sqliteExprCode(pParse, pExpr->pLeft); |
| 1098 | sqliteExprCode(pParse, pExpr->pRight); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1099 | sqliteVdbeAddOp(v, op, 0, dest); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1100 | break; |
| 1101 | } |
| 1102 | case TK_LIKE: |
| 1103 | case TK_GLOB: { |
| 1104 | sqliteExprCode(pParse, pExpr->pLeft); |
| 1105 | sqliteExprCode(pParse, pExpr->pRight); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1106 | sqliteVdbeAddOp(v, op, 1, dest); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1107 | break; |
| 1108 | } |
| 1109 | case TK_ISNULL: |
| 1110 | case TK_NOTNULL: { |
| 1111 | sqliteExprCode(pParse, pExpr->pLeft); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1112 | sqliteVdbeAddOp(v, op, 0, dest); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1113 | break; |
| 1114 | } |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1115 | case TK_IN: { |
drh | cfab11b | 2000-06-06 03:31:22 +0000 | [diff] [blame] | 1116 | sqliteExprCode(pParse, pExpr->pLeft); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1117 | if( pExpr->pSelect ){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1118 | sqliteVdbeAddOp(v, OP_NotFound, pExpr->iTable, dest); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1119 | }else{ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1120 | sqliteVdbeAddOp(v, OP_SetNotFound, pExpr->iTable, dest); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1121 | } |
| 1122 | break; |
| 1123 | } |
| 1124 | case TK_BETWEEN: { |
| 1125 | int addr; |
| 1126 | sqliteExprCode(pParse, pExpr->pLeft); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1127 | sqliteVdbeAddOp(v, OP_Dup, 0, 0); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1128 | sqliteExprCode(pParse, pExpr->pList->a[0].pExpr); |
| 1129 | addr = sqliteVdbeCurrentAddr(v); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1130 | sqliteVdbeAddOp(v, OP_Ge, 0, addr+3); |
| 1131 | sqliteVdbeAddOp(v, OP_Pop, 1, 0); |
| 1132 | sqliteVdbeAddOp(v, OP_Goto, 0, dest); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1133 | sqliteExprCode(pParse, pExpr->pList->a[1].pExpr); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1134 | sqliteVdbeAddOp(v, OP_Gt, 0, dest); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1135 | break; |
| 1136 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1137 | default: { |
| 1138 | sqliteExprCode(pParse, pExpr); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1139 | sqliteVdbeAddOp(v, OP_Not, 0, 0); |
| 1140 | sqliteVdbeAddOp(v, OP_If, 0, dest); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1141 | break; |
| 1142 | } |
| 1143 | } |
| 1144 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1145 | |
| 1146 | /* |
| 1147 | ** Do a deep comparison of two expression trees. Return TRUE (non-zero) |
| 1148 | ** if they are identical and return FALSE if they differ in any way. |
| 1149 | */ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1150 | int sqliteExprCompare(Expr *pA, Expr *pB){ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1151 | int i; |
| 1152 | if( pA==0 ){ |
| 1153 | return pB==0; |
| 1154 | }else if( pB==0 ){ |
| 1155 | return 0; |
| 1156 | } |
| 1157 | if( pA->op!=pB->op ) return 0; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1158 | if( !sqliteExprCompare(pA->pLeft, pB->pLeft) ) return 0; |
| 1159 | if( !sqliteExprCompare(pA->pRight, pB->pRight) ) return 0; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1160 | if( pA->pList ){ |
| 1161 | if( pB->pList==0 ) return 0; |
| 1162 | if( pA->pList->nExpr!=pB->pList->nExpr ) return 0; |
| 1163 | for(i=0; i<pA->pList->nExpr; i++){ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1164 | if( !sqliteExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1165 | return 0; |
| 1166 | } |
| 1167 | } |
| 1168 | }else if( pB->pList ){ |
| 1169 | return 0; |
| 1170 | } |
| 1171 | if( pA->pSelect || pB->pSelect ) return 0; |
| 1172 | if( pA->token.z ){ |
| 1173 | if( pB->token.z==0 ) return 0; |
| 1174 | if( pB->token.n!=pA->token.n ) return 0; |
| 1175 | if( sqliteStrNICmp(pA->token.z, pB->token.z, pA->token.n)!=0 ) return 0; |
| 1176 | } |
| 1177 | return 1; |
| 1178 | } |
| 1179 | |
| 1180 | /* |
| 1181 | ** Add a new element to the pParse->aAgg[] array and return its index. |
| 1182 | */ |
| 1183 | static int appendAggInfo(Parse *pParse){ |
| 1184 | if( (pParse->nAgg & 0x7)==0 ){ |
| 1185 | int amt = pParse->nAgg + 8; |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 1186 | AggExpr *aAgg = sqliteRealloc(pParse->aAgg, amt*sizeof(pParse->aAgg[0])); |
| 1187 | if( aAgg==0 ){ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1188 | return -1; |
| 1189 | } |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 1190 | pParse->aAgg = aAgg; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1191 | } |
| 1192 | memset(&pParse->aAgg[pParse->nAgg], 0, sizeof(pParse->aAgg[0])); |
| 1193 | return pParse->nAgg++; |
| 1194 | } |
| 1195 | |
| 1196 | /* |
| 1197 | ** Analyze the given expression looking for aggregate functions and |
| 1198 | ** for variables that need to be added to the pParse->aAgg[] array. |
| 1199 | ** Make additional entries to the pParse->aAgg[] array as necessary. |
| 1200 | ** |
| 1201 | ** This routine should only be called after the expression has been |
| 1202 | ** analyzed by sqliteExprResolveIds() and sqliteExprCheck(). |
| 1203 | ** |
| 1204 | ** If errors are seen, leave an error message in zErrMsg and return |
| 1205 | ** the number of errors. |
| 1206 | */ |
| 1207 | int sqliteExprAnalyzeAggregates(Parse *pParse, Expr *pExpr){ |
| 1208 | int i; |
| 1209 | AggExpr *aAgg; |
| 1210 | int nErr = 0; |
| 1211 | |
| 1212 | if( pExpr==0 ) return 0; |
| 1213 | switch( pExpr->op ){ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 1214 | case TK_COLUMN: { |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1215 | aAgg = pParse->aAgg; |
| 1216 | for(i=0; i<pParse->nAgg; i++){ |
| 1217 | if( aAgg[i].isAgg ) continue; |
| 1218 | if( aAgg[i].pExpr->iTable==pExpr->iTable |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 1219 | && aAgg[i].pExpr->iColumn==pExpr->iColumn ){ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1220 | break; |
| 1221 | } |
| 1222 | } |
| 1223 | if( i>=pParse->nAgg ){ |
| 1224 | i = appendAggInfo(pParse); |
| 1225 | if( i<0 ) return 1; |
| 1226 | pParse->aAgg[i].isAgg = 0; |
| 1227 | pParse->aAgg[i].pExpr = pExpr; |
| 1228 | } |
drh | aaf8872 | 2000-06-08 11:25:00 +0000 | [diff] [blame] | 1229 | pExpr->iAgg = i; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1230 | break; |
| 1231 | } |
| 1232 | case TK_AGG_FUNCTION: { |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 1233 | if( pExpr->iColumn==FN_Count || pExpr->iColumn==FN_Avg ){ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1234 | if( pParse->iAggCount>=0 ){ |
| 1235 | i = pParse->iAggCount; |
| 1236 | }else{ |
| 1237 | i = appendAggInfo(pParse); |
| 1238 | if( i<0 ) return 1; |
| 1239 | pParse->aAgg[i].isAgg = 1; |
| 1240 | pParse->aAgg[i].pExpr = 0; |
| 1241 | pParse->iAggCount = i; |
| 1242 | } |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 1243 | if( pExpr->iColumn==FN_Count ){ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1244 | pExpr->iAgg = i; |
| 1245 | break; |
| 1246 | } |
| 1247 | } |
| 1248 | aAgg = pParse->aAgg; |
| 1249 | for(i=0; i<pParse->nAgg; i++){ |
| 1250 | if( !aAgg[i].isAgg ) continue; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1251 | if( sqliteExprCompare(aAgg[i].pExpr, pExpr) ){ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1252 | break; |
| 1253 | } |
| 1254 | } |
| 1255 | if( i>=pParse->nAgg ){ |
| 1256 | i = appendAggInfo(pParse); |
| 1257 | if( i<0 ) return 1; |
| 1258 | pParse->aAgg[i].isAgg = 1; |
| 1259 | pParse->aAgg[i].pExpr = pExpr; |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame^] | 1260 | if( pExpr->iColumn==FN_Unknown ){ |
| 1261 | pParse->aAgg[i].pUser = sqliteFindUserFunction(pParse->db, |
| 1262 | pExpr->token.z, pExpr->token.n, pExpr->pList->nExpr, 0); |
| 1263 | }else{ |
| 1264 | pParse->aAgg[i].pUser = 0; |
| 1265 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1266 | } |
| 1267 | pExpr->iAgg = i; |
| 1268 | break; |
| 1269 | } |
| 1270 | default: { |
| 1271 | if( pExpr->pLeft ){ |
| 1272 | nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pLeft); |
| 1273 | } |
| 1274 | if( nErr==0 && pExpr->pRight ){ |
| 1275 | nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pRight); |
| 1276 | } |
| 1277 | if( nErr==0 && pExpr->pList ){ |
| 1278 | int n = pExpr->pList->nExpr; |
| 1279 | int i; |
| 1280 | for(i=0; nErr==0 && i<n; i++){ |
| 1281 | nErr = sqliteExprAnalyzeAggregates(pParse, pExpr->pList->a[i].pExpr); |
| 1282 | } |
| 1283 | } |
| 1284 | break; |
| 1285 | } |
| 1286 | } |
| 1287 | return nErr; |
| 1288 | } |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 1289 | |
| 1290 | /* |
| 1291 | ** Locate a user function given a name and a number of arguments. |
| 1292 | ** Return a pointer to the UserFunc structure that defines that |
| 1293 | ** function, or return NULL if the function does not exist. |
| 1294 | ** |
| 1295 | ** If the createFlag argument is true, then a new (blank) UserFunc |
| 1296 | ** structure is created and liked into the "db" structure if a |
| 1297 | ** no matching function previously existed. When createFlag is true |
| 1298 | ** and the nArg parameter is -1, then only a function that accepts |
| 1299 | ** any number of arguments will be returned. |
| 1300 | ** |
| 1301 | ** If createFlag is false and nArg is -1, then the first valid |
| 1302 | ** function found is returned. A function is valid if either xFunc |
| 1303 | ** or xStep is non-zero. |
| 1304 | */ |
| 1305 | UserFunc *sqliteFindUserFunction( |
| 1306 | sqlite *db, /* An open database */ |
| 1307 | const char *zName, /* Name of the function. Not null-terminated */ |
| 1308 | int nName, /* Number of characters in the name */ |
| 1309 | int nArg, /* Number of arguments. -1 means any number */ |
| 1310 | int createFlag /* Create new entry if true and does not otherwise exist */ |
| 1311 | ){ |
| 1312 | UserFunc *pFirst, *p, *pMaybe; |
| 1313 | pFirst = p = (UserFunc*)sqliteHashFind(&db->userFunc, zName, nName); |
| 1314 | if( !createFlag && nArg<0 ){ |
| 1315 | while( p && p->xFunc==0 && p->xStep==0 ){ p = p->pNext; } |
| 1316 | return p; |
| 1317 | } |
| 1318 | pMaybe = 0; |
| 1319 | while( p && p->nArg!=nArg ){ |
| 1320 | if( p->nArg<0 && !createFlag && (p->xFunc || p->xStep) ) pMaybe = p; |
| 1321 | p = p->pNext; |
| 1322 | } |
| 1323 | if( p && !createFlag && p->xFunc==0 && p->xStep==0 ){ |
| 1324 | return 0; |
| 1325 | } |
| 1326 | if( p==0 && pMaybe ){ |
| 1327 | assert( createFlag==0 ); |
| 1328 | return pMaybe; |
| 1329 | } |
| 1330 | if( p==0 && createFlag ){ |
| 1331 | p = sqliteMalloc( sizeof(*p) ); |
| 1332 | p->nArg = nArg; |
| 1333 | p->pNext = pFirst; |
| 1334 | sqliteHashInsert(&db->userFunc, zName, nName, (void*)p); |
| 1335 | } |
| 1336 | return p; |
| 1337 | } |