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