Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(556)

Side by Side Diff: third_party/sqlite/sqlite-src-3100200/src/resolve.c

Issue 1610543003: [sql] Import reference version of SQLite 3.10.2. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 ** 2008 August 18 2 ** 2008 August 18
3 ** 3 **
4 ** The author disclaims copyright to this source code. In place of 4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing: 5 ** a legal notice, here is a blessing:
6 ** 6 **
7 ** May you do good and not evil. 7 ** May you do good and not evil.
8 ** May you find forgiveness for yourself and forgive others. 8 ** May you find forgiveness for yourself and forgive others.
9 ** May you share freely, never taking more than you give. 9 ** May you share freely, never taking more than you give.
10 ** 10 **
(...skipping 10 matching lines...) Expand all
21 /* 21 /*
22 ** Walk the expression tree pExpr and increase the aggregate function 22 ** Walk the expression tree pExpr and increase the aggregate function
23 ** depth (the Expr.op2 field) by N on every TK_AGG_FUNCTION node. 23 ** depth (the Expr.op2 field) by N on every TK_AGG_FUNCTION node.
24 ** This needs to occur when copying a TK_AGG_FUNCTION node from an 24 ** This needs to occur when copying a TK_AGG_FUNCTION node from an
25 ** outer query into an inner subquery. 25 ** outer query into an inner subquery.
26 ** 26 **
27 ** incrAggFunctionDepth(pExpr,n) is the main routine. incrAggDepth(..) 27 ** incrAggFunctionDepth(pExpr,n) is the main routine. incrAggDepth(..)
28 ** is a helper function - a callback for the tree walker. 28 ** is a helper function - a callback for the tree walker.
29 */ 29 */
30 static int incrAggDepth(Walker *pWalker, Expr *pExpr){ 30 static int incrAggDepth(Walker *pWalker, Expr *pExpr){
31 if( pExpr->op==TK_AGG_FUNCTION ) pExpr->op2 += pWalker->u.i; 31 if( pExpr->op==TK_AGG_FUNCTION ) pExpr->op2 += pWalker->u.n;
32 return WRC_Continue; 32 return WRC_Continue;
33 } 33 }
34 static void incrAggFunctionDepth(Expr *pExpr, int N){ 34 static void incrAggFunctionDepth(Expr *pExpr, int N){
35 if( N>0 ){ 35 if( N>0 ){
36 Walker w; 36 Walker w;
37 memset(&w, 0, sizeof(w)); 37 memset(&w, 0, sizeof(w));
38 w.xExprCallback = incrAggDepth; 38 w.xExprCallback = incrAggDepth;
39 w.u.i = N; 39 w.u.n = N;
40 sqlite3WalkExpr(&w, pExpr); 40 sqlite3WalkExpr(&w, pExpr);
41 } 41 }
42 } 42 }
43 43
44 /* 44 /*
45 ** Turn the pExpr expression into an alias for the iCol-th column of the 45 ** Turn the pExpr expression into an alias for the iCol-th column of the
46 ** result set in pEList. 46 ** result set in pEList.
47 ** 47 **
48 ** If the result set column is a simple column reference, then this routine
49 ** makes an exact copy. But for any other kind of expression, this
50 ** routine make a copy of the result set column as the argument to the
51 ** TK_AS operator. The TK_AS operator causes the expression to be
52 ** evaluated just once and then reused for each alias.
53 **
54 ** The reason for suppressing the TK_AS term when the expression is a simple
55 ** column reference is so that the column reference will be recognized as
56 ** usable by indices within the WHERE clause processing logic.
57 **
58 ** The TK_AS operator is inhibited if zType[0]=='G'. This means
59 ** that in a GROUP BY clause, the expression is evaluated twice. Hence:
60 **
61 ** SELECT random()%5 AS x, count(*) FROM tab GROUP BY x
62 **
63 ** Is equivalent to:
64 **
65 ** SELECT random()%5 AS x, count(*) FROM tab GROUP BY random()%5
66 **
67 ** The result of random()%5 in the GROUP BY clause is probably different
68 ** from the result in the result-set. On the other hand Standard SQL does
69 ** not allow the GROUP BY clause to contain references to result-set columns.
70 ** So this should never come up in well-formed queries.
71 **
72 ** If the reference is followed by a COLLATE operator, then make sure 48 ** If the reference is followed by a COLLATE operator, then make sure
73 ** the COLLATE operator is preserved. For example: 49 ** the COLLATE operator is preserved. For example:
74 ** 50 **
75 ** SELECT a+b, c+d FROM t1 ORDER BY 1 COLLATE nocase; 51 ** SELECT a+b, c+d FROM t1 ORDER BY 1 COLLATE nocase;
76 ** 52 **
77 ** Should be transformed into: 53 ** Should be transformed into:
78 ** 54 **
79 ** SELECT a+b, c+d FROM t1 ORDER BY (a+b) COLLATE nocase; 55 ** SELECT a+b, c+d FROM t1 ORDER BY (a+b) COLLATE nocase;
80 ** 56 **
81 ** The nSubquery parameter specifies how many levels of subquery the 57 ** The nSubquery parameter specifies how many levels of subquery the
82 ** alias is removed from the original expression. The usually value is 58 ** alias is removed from the original expression. The usual value is
83 ** zero but it might be more if the alias is contained within a subquery 59 ** zero but it might be more if the alias is contained within a subquery
84 ** of the original expression. The Expr.op2 field of TK_AGG_FUNCTION 60 ** of the original expression. The Expr.op2 field of TK_AGG_FUNCTION
85 ** structures must be increased by the nSubquery amount. 61 ** structures must be increased by the nSubquery amount.
86 */ 62 */
87 static void resolveAlias( 63 static void resolveAlias(
88 Parse *pParse, /* Parsing context */ 64 Parse *pParse, /* Parsing context */
89 ExprList *pEList, /* A result set */ 65 ExprList *pEList, /* A result set */
90 int iCol, /* A column in the result set. 0..pEList->nExpr-1 */ 66 int iCol, /* A column in the result set. 0..pEList->nExpr-1 */
91 Expr *pExpr, /* Transform this into an alias to the result set */ 67 Expr *pExpr, /* Transform this into an alias to the result set */
92 const char *zType, /* "GROUP" or "ORDER" or "" */ 68 const char *zType, /* "GROUP" or "ORDER" or "" */
93 int nSubquery /* Number of subqueries that the label is moving */ 69 int nSubquery /* Number of subqueries that the label is moving */
94 ){ 70 ){
95 Expr *pOrig; /* The iCol-th column of the result set */ 71 Expr *pOrig; /* The iCol-th column of the result set */
96 Expr *pDup; /* Copy of pOrig */ 72 Expr *pDup; /* Copy of pOrig */
97 sqlite3 *db; /* The database connection */ 73 sqlite3 *db; /* The database connection */
98 74
99 assert( iCol>=0 && iCol<pEList->nExpr ); 75 assert( iCol>=0 && iCol<pEList->nExpr );
100 pOrig = pEList->a[iCol].pExpr; 76 pOrig = pEList->a[iCol].pExpr;
101 assert( pOrig!=0 ); 77 assert( pOrig!=0 );
102 assert( pOrig->flags & EP_Resolved );
103 db = pParse->db; 78 db = pParse->db;
104 pDup = sqlite3ExprDup(db, pOrig, 0); 79 pDup = sqlite3ExprDup(db, pOrig, 0);
105 if( pDup==0 ) return; 80 if( pDup==0 ) return;
106 if( pOrig->op!=TK_COLUMN && zType[0]!='G' ){ 81 if( zType[0]!='G' ) incrAggFunctionDepth(pDup, nSubquery);
107 incrAggFunctionDepth(pDup, nSubquery);
108 pDup = sqlite3PExpr(pParse, TK_AS, pDup, 0, 0);
109 if( pDup==0 ) return;
110 ExprSetProperty(pDup, EP_Skip);
111 if( pEList->a[iCol].u.x.iAlias==0 ){
112 pEList->a[iCol].u.x.iAlias = (u16)(++pParse->nAlias);
113 }
114 pDup->iTable = pEList->a[iCol].u.x.iAlias;
115 }
116 if( pExpr->op==TK_COLLATE ){ 82 if( pExpr->op==TK_COLLATE ){
117 pDup = sqlite3ExprAddCollateString(pParse, pDup, pExpr->u.zToken); 83 pDup = sqlite3ExprAddCollateString(pParse, pDup, pExpr->u.zToken);
118 } 84 }
85 ExprSetProperty(pDup, EP_Alias);
119 86
120 /* Before calling sqlite3ExprDelete(), set the EP_Static flag. This 87 /* Before calling sqlite3ExprDelete(), set the EP_Static flag. This
121 ** prevents ExprDelete() from deleting the Expr structure itself, 88 ** prevents ExprDelete() from deleting the Expr structure itself,
122 ** allowing it to be repopulated by the memcpy() on the following line. 89 ** allowing it to be repopulated by the memcpy() on the following line.
123 ** The pExpr->u.zToken might point into memory that will be freed by the 90 ** The pExpr->u.zToken might point into memory that will be freed by the
124 ** sqlite3DbFree(db, pDup) on the last line of this block, so be sure to 91 ** sqlite3DbFree(db, pDup) on the last line of this block, so be sure to
125 ** make a copy of the token before doing the sqlite3DbFree(). 92 ** make a copy of the token before doing the sqlite3DbFree().
126 */ 93 */
127 ExprSetProperty(pExpr, EP_Static); 94 ExprSetProperty(pExpr, EP_Static);
128 sqlite3ExprDelete(db, pExpr); 95 sqlite3ExprDelete(db, pExpr);
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
240 ExprSetVVAProperty(pExpr, EP_NoReduce); 207 ExprSetVVAProperty(pExpr, EP_NoReduce);
241 208
242 /* Translate the schema name in zDb into a pointer to the corresponding 209 /* Translate the schema name in zDb into a pointer to the corresponding
243 ** schema. If not found, pSchema will remain NULL and nothing will match 210 ** schema. If not found, pSchema will remain NULL and nothing will match
244 ** resulting in an appropriate error message toward the end of this routine 211 ** resulting in an appropriate error message toward the end of this routine
245 */ 212 */
246 if( zDb ){ 213 if( zDb ){
247 testcase( pNC->ncFlags & NC_PartIdx ); 214 testcase( pNC->ncFlags & NC_PartIdx );
248 testcase( pNC->ncFlags & NC_IsCheck ); 215 testcase( pNC->ncFlags & NC_IsCheck );
249 if( (pNC->ncFlags & (NC_PartIdx|NC_IsCheck))!=0 ){ 216 if( (pNC->ncFlags & (NC_PartIdx|NC_IsCheck))!=0 ){
250 /* Silently ignore database qualifiers inside CHECK constraints and partia l 217 /* Silently ignore database qualifiers inside CHECK constraints and
251 ** indices. Do not raise errors because that might break legacy and 218 ** partial indices. Do not raise errors because that might break
252 ** because it does not hurt anything to just ignore the database name. */ 219 ** legacy and because it does not hurt anything to just ignore the
220 ** database name. */
253 zDb = 0; 221 zDb = 0;
254 }else{ 222 }else{
255 for(i=0; i<db->nDb; i++){ 223 for(i=0; i<db->nDb; i++){
256 assert( db->aDb[i].zName ); 224 assert( db->aDb[i].zName );
257 if( sqlite3StrICmp(db->aDb[i].zName,zDb)==0 ){ 225 if( sqlite3StrICmp(db->aDb[i].zName,zDb)==0 ){
258 pSchema = db->aDb[i].pSchema; 226 pSchema = db->aDb[i].pSchema;
259 break; 227 break;
260 } 228 }
261 } 229 }
262 } 230 }
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
299 if( 0==(cntTab++) ){ 267 if( 0==(cntTab++) ){
300 pMatch = pItem; 268 pMatch = pItem;
301 } 269 }
302 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){ 270 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
303 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){ 271 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
304 /* If there has been exactly one prior match and this match 272 /* If there has been exactly one prior match and this match
305 ** is for the right-hand table of a NATURAL JOIN or is in a 273 ** is for the right-hand table of a NATURAL JOIN or is in a
306 ** USING clause, then skip this match. 274 ** USING clause, then skip this match.
307 */ 275 */
308 if( cnt==1 ){ 276 if( cnt==1 ){
309 if( pItem->jointype & JT_NATURAL ) continue; 277 if( pItem->fg.jointype & JT_NATURAL ) continue;
310 if( nameInUsingClause(pItem->pUsing, zCol) ) continue; 278 if( nameInUsingClause(pItem->pUsing, zCol) ) continue;
311 } 279 }
312 cnt++; 280 cnt++;
313 pMatch = pItem; 281 pMatch = pItem;
314 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */ 282 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
315 pExpr->iColumn = j==pTab->iPKey ? -1 : (i16)j; 283 pExpr->iColumn = j==pTab->iPKey ? -1 : (i16)j;
316 break; 284 break;
317 } 285 }
318 } 286 }
319 } 287 }
320 if( pMatch ){ 288 if( pMatch ){
321 pExpr->iTable = pMatch->iCursor; 289 pExpr->iTable = pMatch->iCursor;
322 pExpr->pTab = pMatch->pTab; 290 pExpr->pTab = pMatch->pTab;
323 assert( (pMatch->jointype & JT_RIGHT)==0 ); /* RIGHT JOIN not (yet) supp orted */ 291 /* RIGHT JOIN not (yet) supported */
324 if( (pMatch->jointype & JT_LEFT)!=0 ){ 292 assert( (pMatch->fg.jointype & JT_RIGHT)==0 );
293 if( (pMatch->fg.jointype & JT_LEFT)!=0 ){
325 ExprSetProperty(pExpr, EP_CanBeNull); 294 ExprSetProperty(pExpr, EP_CanBeNull);
326 } 295 }
327 pSchema = pExpr->pTab->pSchema; 296 pSchema = pExpr->pTab->pSchema;
328 } 297 }
329 } /* if( pSrcList ) */ 298 } /* if( pSrcList ) */
330 299
331 #ifndef SQLITE_OMIT_TRIGGER 300 #ifndef SQLITE_OMIT_TRIGGER
332 /* If we have not already resolved the name, then maybe 301 /* If we have not already resolved the name, then maybe
333 ** it is a new.* or old.* trigger argument reference 302 ** it is a new.* or old.* trigger argument reference
334 */ 303 */
(...skipping 15 matching lines...) Expand all
350 pSchema = pTab->pSchema; 319 pSchema = pTab->pSchema;
351 cntTab++; 320 cntTab++;
352 for(iCol=0, pCol=pTab->aCol; iCol<pTab->nCol; iCol++, pCol++){ 321 for(iCol=0, pCol=pTab->aCol; iCol<pTab->nCol; iCol++, pCol++){
353 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){ 322 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
354 if( iCol==pTab->iPKey ){ 323 if( iCol==pTab->iPKey ){
355 iCol = -1; 324 iCol = -1;
356 } 325 }
357 break; 326 break;
358 } 327 }
359 } 328 }
360 if( iCol>=pTab->nCol && sqlite3IsRowid(zCol) && HasRowid(pTab) ){ 329 if( iCol>=pTab->nCol && sqlite3IsRowid(zCol) && VisibleRowid(pTab) ){
361 /* IMP: R-51414-32910 */ 330 /* IMP: R-51414-32910 */
362 /* IMP: R-44911-55124 */
363 iCol = -1; 331 iCol = -1;
364 } 332 }
365 if( iCol<pTab->nCol ){ 333 if( iCol<pTab->nCol ){
366 cnt++; 334 cnt++;
367 if( iCol<0 ){ 335 if( iCol<0 ){
368 pExpr->affinity = SQLITE_AFF_INTEGER; 336 pExpr->affinity = SQLITE_AFF_INTEGER;
369 }else if( pExpr->iTable==0 ){ 337 }else if( pExpr->iTable==0 ){
370 testcase( iCol==31 ); 338 testcase( iCol==31 );
371 testcase( iCol==32 ); 339 testcase( iCol==32 );
372 pParse->oldmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol)); 340 pParse->oldmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
373 }else{ 341 }else{
374 testcase( iCol==31 ); 342 testcase( iCol==31 );
375 testcase( iCol==32 ); 343 testcase( iCol==32 );
376 pParse->newmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol)); 344 pParse->newmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
377 } 345 }
378 pExpr->iColumn = (i16)iCol; 346 pExpr->iColumn = (i16)iCol;
379 pExpr->pTab = pTab; 347 pExpr->pTab = pTab;
380 isTrigger = 1; 348 isTrigger = 1;
381 } 349 }
382 } 350 }
383 } 351 }
384 #endif /* !defined(SQLITE_OMIT_TRIGGER) */ 352 #endif /* !defined(SQLITE_OMIT_TRIGGER) */
385 353
386 /* 354 /*
387 ** Perhaps the name is a reference to the ROWID 355 ** Perhaps the name is a reference to the ROWID
388 */ 356 */
389 if( cnt==0 && cntTab==1 && pMatch && sqlite3IsRowid(zCol) 357 if( cnt==0
390 && HasRowid(pMatch->pTab) ){ 358 && cntTab==1
359 && pMatch
360 && (pNC->ncFlags & NC_IdxExpr)==0
361 && sqlite3IsRowid(zCol)
362 && VisibleRowid(pMatch->pTab)
363 ){
391 cnt = 1; 364 cnt = 1;
392 pExpr->iColumn = -1; /* IMP: R-44911-55124 */ 365 pExpr->iColumn = -1;
393 pExpr->affinity = SQLITE_AFF_INTEGER; 366 pExpr->affinity = SQLITE_AFF_INTEGER;
394 } 367 }
395 368
396 /* 369 /*
397 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z 370 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
398 ** might refer to an result-set alias. This happens, for example, when 371 ** might refer to an result-set alias. This happens, for example, when
399 ** we are resolving names in the WHERE clause of the following command: 372 ** we are resolving names in the WHERE clause of the following command:
400 ** 373 **
401 ** SELECT a+b AS x FROM table WHERE x<10; 374 ** SELECT a+b AS x FROM table WHERE x<10;
402 ** 375 **
403 ** In cases like this, replace pExpr with a copy of the expression that 376 ** In cases like this, replace pExpr with a copy of the expression that
404 ** forms the result set entry ("a+b" in the example) and return immediately. 377 ** forms the result set entry ("a+b" in the example) and return immediately.
405 ** Note that the expression in the result set should have already been 378 ** Note that the expression in the result set should have already been
406 ** resolved by the time the WHERE clause is resolved. 379 ** resolved by the time the WHERE clause is resolved.
407 ** 380 **
408 ** The ability to use an output result-set column in the WHERE, GROUP BY, 381 ** The ability to use an output result-set column in the WHERE, GROUP BY,
409 ** or HAVING clauses, or as part of a larger expression in the ORDRE BY 382 ** or HAVING clauses, or as part of a larger expression in the ORDER BY
410 ** clause is not standard SQL. This is a (goofy) SQLite extension, that 383 ** clause is not standard SQL. This is a (goofy) SQLite extension, that
411 ** is supported for backwards compatibility only. TO DO: Issue a warning 384 ** is supported for backwards compatibility only. Hence, we issue a warning
412 ** on sqlite3_log() whenever the capability is used. 385 ** on sqlite3_log() whenever the capability is used.
413 */ 386 */
414 if( (pEList = pNC->pEList)!=0 387 if( (pEList = pNC->pEList)!=0
415 && zTab==0 388 && zTab==0
416 && cnt==0 389 && cnt==0
417 ){ 390 ){
418 for(j=0; j<pEList->nExpr; j++){ 391 for(j=0; j<pEList->nExpr; j++){
419 char *zAs = pEList->a[j].zName; 392 char *zAs = pEList->a[j].zName;
420 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){ 393 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
421 Expr *pOrig; 394 Expr *pOrig;
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
498 /* Clean up and return 471 /* Clean up and return
499 */ 472 */
500 sqlite3ExprDelete(db, pExpr->pLeft); 473 sqlite3ExprDelete(db, pExpr->pLeft);
501 pExpr->pLeft = 0; 474 pExpr->pLeft = 0;
502 sqlite3ExprDelete(db, pExpr->pRight); 475 sqlite3ExprDelete(db, pExpr->pRight);
503 pExpr->pRight = 0; 476 pExpr->pRight = 0;
504 pExpr->op = (isTrigger ? TK_TRIGGER : TK_COLUMN); 477 pExpr->op = (isTrigger ? TK_TRIGGER : TK_COLUMN);
505 lookupname_end: 478 lookupname_end:
506 if( cnt==1 ){ 479 if( cnt==1 ){
507 assert( pNC!=0 ); 480 assert( pNC!=0 );
508 if( pExpr->op!=TK_AS ){ 481 if( !ExprHasProperty(pExpr, EP_Alias) ){
509 sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList); 482 sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList);
510 } 483 }
511 /* Increment the nRef value on all name contexts from TopNC up to 484 /* Increment the nRef value on all name contexts from TopNC up to
512 ** the point where the name matched. */ 485 ** the point where the name matched. */
513 for(;;){ 486 for(;;){
514 assert( pTopNC!=0 ); 487 assert( pTopNC!=0 );
515 pTopNC->nRef++; 488 pTopNC->nRef++;
516 if( pTopNC==pNC ) break; 489 if( pTopNC==pNC ) break;
517 pTopNC = pTopNC->pNext; 490 pTopNC = pTopNC->pNext;
518 } 491 }
(...skipping 20 matching lines...) Expand all
539 testcase( iCol==BMS ); 512 testcase( iCol==BMS );
540 testcase( iCol==BMS-1 ); 513 testcase( iCol==BMS-1 );
541 pItem->colUsed |= ((Bitmask)1)<<(iCol>=BMS ? BMS-1 : iCol); 514 pItem->colUsed |= ((Bitmask)1)<<(iCol>=BMS ? BMS-1 : iCol);
542 } 515 }
543 ExprSetProperty(p, EP_Resolved); 516 ExprSetProperty(p, EP_Resolved);
544 } 517 }
545 return p; 518 return p;
546 } 519 }
547 520
548 /* 521 /*
549 ** Report an error that an expression is not valid for a partial index WHERE 522 ** Report an error that an expression is not valid for some set of
550 ** clause. 523 ** pNC->ncFlags values determined by validMask.
551 */ 524 */
552 static void notValidPartIdxWhere( 525 static void notValid(
553 Parse *pParse, /* Leave error message here */ 526 Parse *pParse, /* Leave error message here */
554 NameContext *pNC, /* The name context */ 527 NameContext *pNC, /* The name context */
555 const char *zMsg /* Type of error */ 528 const char *zMsg, /* Type of error */
529 int validMask /* Set of contexts for which prohibited */
556 ){ 530 ){
557 if( (pNC->ncFlags & NC_PartIdx)!=0 ){ 531 assert( (validMask&~(NC_IsCheck|NC_PartIdx|NC_IdxExpr))==0 );
558 sqlite3ErrorMsg(pParse, "%s prohibited in partial index WHERE clauses", 532 if( (pNC->ncFlags & validMask)!=0 ){
559 zMsg); 533 const char *zIn = "partial index WHERE clauses";
534 if( pNC->ncFlags & NC_IdxExpr ) zIn = "index expressions";
535 #ifndef SQLITE_OMIT_CHECK
536 else if( pNC->ncFlags & NC_IsCheck ) zIn = "CHECK constraints";
537 #endif
538 sqlite3ErrorMsg(pParse, "%s prohibited in %s", zMsg, zIn);
560 } 539 }
561 } 540 }
562 541
563 #ifndef SQLITE_OMIT_CHECK
564 /*
565 ** Report an error that an expression is not valid for a CHECK constraint.
566 */
567 static void notValidCheckConstraint(
568 Parse *pParse, /* Leave error message here */
569 NameContext *pNC, /* The name context */
570 const char *zMsg /* Type of error */
571 ){
572 if( (pNC->ncFlags & NC_IsCheck)!=0 ){
573 sqlite3ErrorMsg(pParse,"%s prohibited in CHECK constraints", zMsg);
574 }
575 }
576 #else
577 # define notValidCheckConstraint(P,N,M)
578 #endif
579
580 /* 542 /*
581 ** Expression p should encode a floating point value between 1.0 and 0.0. 543 ** Expression p should encode a floating point value between 1.0 and 0.0.
582 ** Return 1024 times this value. Or return -1 if p is not a floating point 544 ** Return 1024 times this value. Or return -1 if p is not a floating point
583 ** value between 1.0 and 0.0. 545 ** value between 1.0 and 0.0.
584 */ 546 */
585 static int exprProbability(Expr *p){ 547 static int exprProbability(Expr *p){
586 double r = -1.0; 548 double r = -1.0;
587 if( p->op!=TK_FLOAT ) return -1; 549 if( p->op!=TK_FLOAT ) return -1;
588 sqlite3AtoF(p->u.zToken, &r, sqlite3Strlen30(p->u.zToken), SQLITE_UTF8); 550 sqlite3AtoF(p->u.zToken, &r, sqlite3Strlen30(p->u.zToken), SQLITE_UTF8);
589 assert( r>=0.0 ); 551 assert( r>=0.0 );
590 if( r>1.0 ) return -1; 552 if( r>1.0 ) return -1;
591 return (int)(r*1000.0); 553 return (int)(r*134217728.0);
592 } 554 }
593 555
594 /* 556 /*
595 ** This routine is callback for sqlite3WalkExpr(). 557 ** This routine is callback for sqlite3WalkExpr().
596 ** 558 **
597 ** Resolve symbolic names into TK_COLUMN operators for the current 559 ** Resolve symbolic names into TK_COLUMN operators for the current
598 ** node in the expression tree. Return 0 to continue the search down 560 ** node in the expression tree. Return 0 to continue the search down
599 ** the tree or 2 to abort the tree walk. 561 ** the tree or 2 to abort the tree walk.
600 ** 562 **
601 ** This routine also does error checking and name resolution for 563 ** This routine also does error checking and name resolution for
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
634 struct SrcList_item *pItem; 596 struct SrcList_item *pItem;
635 assert( pSrcList && pSrcList->nSrc==1 ); 597 assert( pSrcList && pSrcList->nSrc==1 );
636 pItem = pSrcList->a; 598 pItem = pSrcList->a;
637 pExpr->op = TK_COLUMN; 599 pExpr->op = TK_COLUMN;
638 pExpr->pTab = pItem->pTab; 600 pExpr->pTab = pItem->pTab;
639 pExpr->iTable = pItem->iCursor; 601 pExpr->iTable = pItem->iCursor;
640 pExpr->iColumn = -1; 602 pExpr->iColumn = -1;
641 pExpr->affinity = SQLITE_AFF_INTEGER; 603 pExpr->affinity = SQLITE_AFF_INTEGER;
642 break; 604 break;
643 } 605 }
644 #endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUB QUERY) */ 606 #endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT)
607 && !defined(SQLITE_OMIT_SUBQUERY) */
645 608
646 /* A lone identifier is the name of a column. 609 /* A lone identifier is the name of a column.
647 */ 610 */
648 case TK_ID: { 611 case TK_ID: {
649 return lookupName(pParse, 0, 0, pExpr->u.zToken, pNC, pExpr); 612 return lookupName(pParse, 0, 0, pExpr->u.zToken, pNC, pExpr);
650 } 613 }
651 614
652 /* A table name and column name: ID.ID 615 /* A table name and column name: ID.ID
653 ** Or a database, table and column: ID.ID.ID 616 ** Or a database, table and column: ID.ID.ID
654 */ 617 */
655 case TK_DOT: { 618 case TK_DOT: {
656 const char *zColumn; 619 const char *zColumn;
657 const char *zTable; 620 const char *zTable;
658 const char *zDb; 621 const char *zDb;
659 Expr *pRight; 622 Expr *pRight;
660 623
661 /* if( pSrcList==0 ) break; */ 624 /* if( pSrcList==0 ) break; */
625 notValid(pParse, pNC, "the \".\" operator", NC_IdxExpr);
626 /*notValid(pParse, pNC, "the \".\" operator", NC_PartIdx|NC_IsCheck, 1);*/
662 pRight = pExpr->pRight; 627 pRight = pExpr->pRight;
663 if( pRight->op==TK_ID ){ 628 if( pRight->op==TK_ID ){
664 zDb = 0; 629 zDb = 0;
665 zTable = pExpr->pLeft->u.zToken; 630 zTable = pExpr->pLeft->u.zToken;
666 zColumn = pRight->u.zToken; 631 zColumn = pRight->u.zToken;
667 }else{ 632 }else{
668 assert( pRight->op==TK_DOT ); 633 assert( pRight->op==TK_DOT );
669 zDb = pExpr->pLeft->u.zToken; 634 zDb = pExpr->pLeft->u.zToken;
670 zTable = pRight->pLeft->u.zToken; 635 zTable = pRight->pLeft->u.zToken;
671 zColumn = pRight->pRight->u.zToken; 636 zColumn = pRight->pRight->u.zToken;
672 } 637 }
673 return lookupName(pParse, zDb, zTable, zColumn, pNC, pExpr); 638 return lookupName(pParse, zDb, zTable, zColumn, pNC, pExpr);
674 } 639 }
675 640
676 /* Resolve function names 641 /* Resolve function names
677 */ 642 */
678 case TK_FUNCTION: { 643 case TK_FUNCTION: {
679 ExprList *pList = pExpr->x.pList; /* The argument list */ 644 ExprList *pList = pExpr->x.pList; /* The argument list */
680 int n = pList ? pList->nExpr : 0; /* Number of arguments */ 645 int n = pList ? pList->nExpr : 0; /* Number of arguments */
681 int no_such_func = 0; /* True if no such function exists */ 646 int no_such_func = 0; /* True if no such function exists */
682 int wrong_num_args = 0; /* True if wrong number of arguments */ 647 int wrong_num_args = 0; /* True if wrong number of arguments */
683 int is_agg = 0; /* True if is an aggregate function */ 648 int is_agg = 0; /* True if is an aggregate function */
684 int auth; /* Authorization to use the function */ 649 int auth; /* Authorization to use the function */
685 int nId; /* Number of characters in function name */ 650 int nId; /* Number of characters in function name */
686 const char *zId; /* The function name. */ 651 const char *zId; /* The function name. */
687 FuncDef *pDef; /* Information about the function */ 652 FuncDef *pDef; /* Information about the function */
688 u8 enc = ENC(pParse->db); /* The database encoding */ 653 u8 enc = ENC(pParse->db); /* The database encoding */
689 654
690 assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); 655 assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
691 notValidPartIdxWhere(pParse, pNC, "functions"); 656 notValid(pParse, pNC, "functions", NC_PartIdx);
692 zId = pExpr->u.zToken; 657 zId = pExpr->u.zToken;
693 nId = sqlite3Strlen30(zId); 658 nId = sqlite3Strlen30(zId);
694 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0); 659 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
695 if( pDef==0 ){ 660 if( pDef==0 ){
696 pDef = sqlite3FindFunction(pParse->db, zId, nId, -2, enc, 0); 661 pDef = sqlite3FindFunction(pParse->db, zId, nId, -2, enc, 0);
697 if( pDef==0 ){ 662 if( pDef==0 ){
698 no_such_func = 1; 663 no_such_func = 1;
699 }else{ 664 }else{
700 wrong_num_args = 1; 665 wrong_num_args = 1;
701 } 666 }
702 }else{ 667 }else{
703 is_agg = pDef->xFunc==0; 668 is_agg = pDef->xFunc==0;
704 if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){ 669 if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){
705 ExprSetProperty(pExpr, EP_Unlikely|EP_Skip); 670 ExprSetProperty(pExpr, EP_Unlikely|EP_Skip);
706 if( n==2 ){ 671 if( n==2 ){
707 pExpr->iTable = exprProbability(pList->a[1].pExpr); 672 pExpr->iTable = exprProbability(pList->a[1].pExpr);
708 if( pExpr->iTable<0 ){ 673 if( pExpr->iTable<0 ){
709 sqlite3ErrorMsg(pParse, "second argument to likelihood() must be a " 674 sqlite3ErrorMsg(pParse,
710 "constant between 0.0 and 1.0"); 675 "second argument to likelihood() must be a "
676 "constant between 0.0 and 1.0");
711 pNC->nErr++; 677 pNC->nErr++;
712 } 678 }
713 }else{ 679 }else{
714 /* EVIDENCE-OF: R-61304-29449 The unlikely(X) function is equivalent to 680 /* EVIDENCE-OF: R-61304-29449 The unlikely(X) function is
715 ** likelihood(X, 0.0625). 681 ** equivalent to likelihood(X, 0.0625).
716 ** EVIDENCE-OF: R-01283-11636 The unlikely(X) function is short-hand for 682 ** EVIDENCE-OF: R-01283-11636 The unlikely(X) function is
717 ** likelihood(X,0.0625). 683 ** short-hand for likelihood(X,0.0625).
718 ** EVIDENCE-OF: R-36850-34127 The likely(X) function is short-hand f or 684 ** EVIDENCE-OF: R-36850-34127 The likely(X) function is short-hand
719 ** likelihood(X,0.9375). 685 ** for likelihood(X,0.9375).
720 ** EVIDENCE-OF: R-53436-40973 The likely(X) function is equivalent t o 686 ** EVIDENCE-OF: R-53436-40973 The likely(X) function is equivalent
721 ** likelihood(X,0.9375). */ 687 ** to likelihood(X,0.9375). */
722 /* TUNING: unlikely() probability is 0.0625. likely() is 0.9375 */ 688 /* TUNING: unlikely() probability is 0.0625. likely() is 0.9375 */
723 pExpr->iTable = pDef->zName[0]=='u' ? 62 : 938; 689 pExpr->iTable = pDef->zName[0]=='u' ? 8388608 : 125829120;
724 } 690 }
725 } 691 }
726 #ifndef SQLITE_OMIT_AUTHORIZATION 692 #ifndef SQLITE_OMIT_AUTHORIZATION
727 auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0); 693 auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0);
728 if( auth!=SQLITE_OK ){ 694 if( auth!=SQLITE_OK ){
729 if( auth==SQLITE_DENY ){ 695 if( auth==SQLITE_DENY ){
730 sqlite3ErrorMsg(pParse, "not authorized to use function: %s", 696 sqlite3ErrorMsg(pParse, "not authorized to use function: %s",
731 pDef->zName); 697 pDef->zName);
732 pNC->nErr++; 698 pNC->nErr++;
733 } 699 }
734 pExpr->op = TK_NULL; 700 pExpr->op = TK_NULL;
735 return WRC_Prune; 701 return WRC_Prune;
736 } 702 }
737 #endif 703 #endif
738 if( pDef->funcFlags & SQLITE_FUNC_CONSTANT ) ExprSetProperty(pExpr,EP_Co nstant); 704 if( pDef->funcFlags & (SQLITE_FUNC_CONSTANT|SQLITE_FUNC_SLOCHNG) ){
705 /* For the purposes of the EP_ConstFunc flag, date and time
706 ** functions and other functions that change slowly are considered
707 ** constant because they are constant for the duration of one query */
708 ExprSetProperty(pExpr,EP_ConstFunc);
709 }
710 if( (pDef->funcFlags & SQLITE_FUNC_CONSTANT)==0 ){
711 /* Date/time functions that use 'now', and other functions like
712 ** sqlite_version() that might change over time cannot be used
713 ** in an index. */
714 notValid(pParse, pNC, "non-deterministic functions", NC_IdxExpr);
715 }
739 } 716 }
740 if( is_agg && (pNC->ncFlags & NC_AllowAgg)==0 ){ 717 if( is_agg && (pNC->ncFlags & NC_AllowAgg)==0 ){
741 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId); 718 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
742 pNC->nErr++; 719 pNC->nErr++;
743 is_agg = 0; 720 is_agg = 0;
744 }else if( no_such_func && pParse->db->init.busy==0 ){ 721 }else if( no_such_func && pParse->db->init.busy==0 ){
745 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId); 722 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
746 pNC->nErr++; 723 pNC->nErr++;
747 }else if( wrong_num_args ){ 724 }else if( wrong_num_args ){
748 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()", 725 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
(...skipping 25 matching lines...) Expand all
774 return WRC_Prune; 751 return WRC_Prune;
775 } 752 }
776 #ifndef SQLITE_OMIT_SUBQUERY 753 #ifndef SQLITE_OMIT_SUBQUERY
777 case TK_SELECT: 754 case TK_SELECT:
778 case TK_EXISTS: testcase( pExpr->op==TK_EXISTS ); 755 case TK_EXISTS: testcase( pExpr->op==TK_EXISTS );
779 #endif 756 #endif
780 case TK_IN: { 757 case TK_IN: {
781 testcase( pExpr->op==TK_IN ); 758 testcase( pExpr->op==TK_IN );
782 if( ExprHasProperty(pExpr, EP_xIsSelect) ){ 759 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
783 int nRef = pNC->nRef; 760 int nRef = pNC->nRef;
784 notValidCheckConstraint(pParse, pNC, "subqueries"); 761 notValid(pParse, pNC, "subqueries", NC_IsCheck|NC_PartIdx|NC_IdxExpr);
785 notValidPartIdxWhere(pParse, pNC, "subqueries");
786 sqlite3WalkSelect(pWalker, pExpr->x.pSelect); 762 sqlite3WalkSelect(pWalker, pExpr->x.pSelect);
787 assert( pNC->nRef>=nRef ); 763 assert( pNC->nRef>=nRef );
788 if( nRef!=pNC->nRef ){ 764 if( nRef!=pNC->nRef ){
789 ExprSetProperty(pExpr, EP_VarSelect); 765 ExprSetProperty(pExpr, EP_VarSelect);
790 } 766 }
791 } 767 }
792 break; 768 break;
793 } 769 }
794 case TK_VARIABLE: { 770 case TK_VARIABLE: {
795 notValidCheckConstraint(pParse, pNC, "parameters"); 771 notValid(pParse, pNC, "parameters", NC_IsCheck|NC_PartIdx|NC_IdxExpr);
796 notValidPartIdxWhere(pParse, pNC, "parameters");
797 break; 772 break;
798 } 773 }
799 } 774 }
800 return (pParse->nErr || pParse->db->mallocFailed) ? WRC_Abort : WRC_Continue; 775 return (pParse->nErr || pParse->db->mallocFailed) ? WRC_Abort : WRC_Continue;
801 } 776 }
802 777
803 /* 778 /*
804 ** pEList is a list of expressions which are really the result set of the 779 ** pEList is a list of expressions which are really the result set of the
805 ** a SELECT statement. pE is a term in an ORDER BY or GROUP BY clause. 780 ** a SELECT statement. pE is a term in an ORDER BY or GROUP BY clause.
806 ** This routine checks to see if pE is a simple identifier which corresponds 781 ** This routine checks to see if pE is a simple identifier which corresponds
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after
980 if( iCol>0 ){ 955 if( iCol>0 ){
981 /* Convert the ORDER BY term into an integer column number iCol, 956 /* Convert the ORDER BY term into an integer column number iCol,
982 ** taking care to preserve the COLLATE clause if it exists */ 957 ** taking care to preserve the COLLATE clause if it exists */
983 Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0); 958 Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0);
984 if( pNew==0 ) return 1; 959 if( pNew==0 ) return 1;
985 pNew->flags |= EP_IntValue; 960 pNew->flags |= EP_IntValue;
986 pNew->u.iValue = iCol; 961 pNew->u.iValue = iCol;
987 if( pItem->pExpr==pE ){ 962 if( pItem->pExpr==pE ){
988 pItem->pExpr = pNew; 963 pItem->pExpr = pNew;
989 }else{ 964 }else{
990 assert( pItem->pExpr->op==TK_COLLATE ); 965 Expr *pParent = pItem->pExpr;
991 assert( pItem->pExpr->pLeft==pE ); 966 assert( pParent->op==TK_COLLATE );
992 pItem->pExpr->pLeft = pNew; 967 while( pParent->pLeft->op==TK_COLLATE ) pParent = pParent->pLeft;
968 assert( pParent->pLeft==pE );
969 pParent->pLeft = pNew;
993 } 970 }
994 sqlite3ExprDelete(db, pE); 971 sqlite3ExprDelete(db, pE);
995 pItem->u.x.iOrderByCol = (u16)iCol; 972 pItem->u.x.iOrderByCol = (u16)iCol;
996 pItem->done = 1; 973 pItem->done = 1;
997 }else{ 974 }else{
998 moreToDo = 1; 975 moreToDo = 1;
999 } 976 }
1000 } 977 }
1001 pSelect = pSelect->pNext; 978 pSelect = pSelect->pNext;
1002 } 979 }
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
1039 } 1016 }
1040 #endif 1017 #endif
1041 pEList = pSelect->pEList; 1018 pEList = pSelect->pEList;
1042 assert( pEList!=0 ); /* sqlite3SelectNew() guarantees this */ 1019 assert( pEList!=0 ); /* sqlite3SelectNew() guarantees this */
1043 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){ 1020 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
1044 if( pItem->u.x.iOrderByCol ){ 1021 if( pItem->u.x.iOrderByCol ){
1045 if( pItem->u.x.iOrderByCol>pEList->nExpr ){ 1022 if( pItem->u.x.iOrderByCol>pEList->nExpr ){
1046 resolveOutOfRangeError(pParse, zType, i+1, pEList->nExpr); 1023 resolveOutOfRangeError(pParse, zType, i+1, pEList->nExpr);
1047 return 1; 1024 return 1;
1048 } 1025 }
1049 resolveAlias(pParse, pEList, pItem->u.x.iOrderByCol-1, pItem->pExpr, zType ,0); 1026 resolveAlias(pParse, pEList, pItem->u.x.iOrderByCol-1, pItem->pExpr,
1027 zType,0);
1050 } 1028 }
1051 } 1029 }
1052 return 0; 1030 return 0;
1053 } 1031 }
1054 1032
1055 /* 1033 /*
1056 ** pOrderBy is an ORDER BY or GROUP BY clause in SELECT statement pSelect. 1034 ** pOrderBy is an ORDER BY or GROUP BY clause in SELECT statement pSelect.
1057 ** The Name context of the SELECT statement is pNC. zType is either 1035 ** The Name context of the SELECT statement is pNC. zType is either
1058 ** "ORDER" or "GROUP" depending on which type of clause pOrderBy is. 1036 ** "ORDER" or "GROUP" depending on which type of clause pOrderBy is.
1059 ** 1037 **
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
1127 1105
1128 /* 1106 /*
1129 ** Resolve names in the SELECT statement p and all of its descendants. 1107 ** Resolve names in the SELECT statement p and all of its descendants.
1130 */ 1108 */
1131 static int resolveSelectStep(Walker *pWalker, Select *p){ 1109 static int resolveSelectStep(Walker *pWalker, Select *p){
1132 NameContext *pOuterNC; /* Context that contains this SELECT */ 1110 NameContext *pOuterNC; /* Context that contains this SELECT */
1133 NameContext sNC; /* Name context of this SELECT */ 1111 NameContext sNC; /* Name context of this SELECT */
1134 int isCompound; /* True if p is a compound select */ 1112 int isCompound; /* True if p is a compound select */
1135 int nCompound; /* Number of compound terms processed so far */ 1113 int nCompound; /* Number of compound terms processed so far */
1136 Parse *pParse; /* Parsing context */ 1114 Parse *pParse; /* Parsing context */
1137 ExprList *pEList; /* Result set expression list */
1138 int i; /* Loop counter */ 1115 int i; /* Loop counter */
1139 ExprList *pGroupBy; /* The GROUP BY clause */ 1116 ExprList *pGroupBy; /* The GROUP BY clause */
1140 Select *pLeftmost; /* Left-most of SELECT of a compound */ 1117 Select *pLeftmost; /* Left-most of SELECT of a compound */
1141 sqlite3 *db; /* Database connection */ 1118 sqlite3 *db; /* Database connection */
1142 1119
1143 1120
1144 assert( p!=0 ); 1121 assert( p!=0 );
1145 if( p->selFlags & SF_Resolved ){ 1122 if( p->selFlags & SF_Resolved ){
1146 return WRC_Prune; 1123 return WRC_Prune;
1147 } 1124 }
(...skipping 24 matching lines...) Expand all
1172 1149
1173 /* Resolve the expressions in the LIMIT and OFFSET clauses. These 1150 /* Resolve the expressions in the LIMIT and OFFSET clauses. These
1174 ** are not allowed to refer to any names, so pass an empty NameContext. 1151 ** are not allowed to refer to any names, so pass an empty NameContext.
1175 */ 1152 */
1176 memset(&sNC, 0, sizeof(sNC)); 1153 memset(&sNC, 0, sizeof(sNC));
1177 sNC.pParse = pParse; 1154 sNC.pParse = pParse;
1178 if( sqlite3ResolveExprNames(&sNC, p->pLimit) || 1155 if( sqlite3ResolveExprNames(&sNC, p->pLimit) ||
1179 sqlite3ResolveExprNames(&sNC, p->pOffset) ){ 1156 sqlite3ResolveExprNames(&sNC, p->pOffset) ){
1180 return WRC_Abort; 1157 return WRC_Abort;
1181 } 1158 }
1159
1160 /* If the SF_Converted flags is set, then this Select object was
1161 ** was created by the convertCompoundSelectToSubquery() function.
1162 ** In this case the ORDER BY clause (p->pOrderBy) should be resolved
1163 ** as if it were part of the sub-query, not the parent. This block
1164 ** moves the pOrderBy down to the sub-query. It will be moved back
1165 ** after the names have been resolved. */
1166 if( p->selFlags & SF_Converted ){
1167 Select *pSub = p->pSrc->a[0].pSelect;
1168 assert( p->pSrc->nSrc==1 && p->pOrderBy );
1169 assert( pSub->pPrior && pSub->pOrderBy==0 );
1170 pSub->pOrderBy = p->pOrderBy;
1171 p->pOrderBy = 0;
1172 }
1182 1173
1183 /* Recursively resolve names in all subqueries 1174 /* Recursively resolve names in all subqueries
1184 */ 1175 */
1185 for(i=0; i<p->pSrc->nSrc; i++){ 1176 for(i=0; i<p->pSrc->nSrc; i++){
1186 struct SrcList_item *pItem = &p->pSrc->a[i]; 1177 struct SrcList_item *pItem = &p->pSrc->a[i];
1187 if( pItem->pSelect ){ 1178 if( pItem->pSelect ){
1188 NameContext *pNC; /* Used to iterate name contexts */ 1179 NameContext *pNC; /* Used to iterate name contexts */
1189 int nRef = 0; /* Refcount for pOuterNC and outer contexts */ 1180 int nRef = 0; /* Refcount for pOuterNC and outer contexts */
1190 const char *zSavedContext = pParse->zAuthContext; 1181 const char *zSavedContext = pParse->zAuthContext;
1191 1182
1192 /* Count the total number of references to pOuterNC and all of its 1183 /* Count the total number of references to pOuterNC and all of its
1193 ** parent contexts. After resolving references to expressions in 1184 ** parent contexts. After resolving references to expressions in
1194 ** pItem->pSelect, check if this value has changed. If so, then 1185 ** pItem->pSelect, check if this value has changed. If so, then
1195 ** SELECT statement pItem->pSelect must be correlated. Set the 1186 ** SELECT statement pItem->pSelect must be correlated. Set the
1196 ** pItem->isCorrelated flag if this is the case. */ 1187 ** pItem->fg.isCorrelated flag if this is the case. */
1197 for(pNC=pOuterNC; pNC; pNC=pNC->pNext) nRef += pNC->nRef; 1188 for(pNC=pOuterNC; pNC; pNC=pNC->pNext) nRef += pNC->nRef;
1198 1189
1199 if( pItem->zName ) pParse->zAuthContext = pItem->zName; 1190 if( pItem->zName ) pParse->zAuthContext = pItem->zName;
1200 sqlite3ResolveSelectNames(pParse, pItem->pSelect, pOuterNC); 1191 sqlite3ResolveSelectNames(pParse, pItem->pSelect, pOuterNC);
1201 pParse->zAuthContext = zSavedContext; 1192 pParse->zAuthContext = zSavedContext;
1202 if( pParse->nErr || db->mallocFailed ) return WRC_Abort; 1193 if( pParse->nErr || db->mallocFailed ) return WRC_Abort;
1203 1194
1204 for(pNC=pOuterNC; pNC; pNC=pNC->pNext) nRef -= pNC->nRef; 1195 for(pNC=pOuterNC; pNC; pNC=pNC->pNext) nRef -= pNC->nRef;
1205 assert( pItem->isCorrelated==0 && nRef<=0 ); 1196 assert( pItem->fg.isCorrelated==0 && nRef<=0 );
1206 pItem->isCorrelated = (nRef!=0); 1197 pItem->fg.isCorrelated = (nRef!=0);
1207 } 1198 }
1208 } 1199 }
1209 1200
1210 /* Set up the local name-context to pass to sqlite3ResolveExprNames() to 1201 /* Set up the local name-context to pass to sqlite3ResolveExprNames() to
1211 ** resolve the result-set expression list. 1202 ** resolve the result-set expression list.
1212 */ 1203 */
1213 sNC.ncFlags = NC_AllowAgg; 1204 sNC.ncFlags = NC_AllowAgg;
1214 sNC.pSrcList = p->pSrc; 1205 sNC.pSrcList = p->pSrc;
1215 sNC.pNext = pOuterNC; 1206 sNC.pNext = pOuterNC;
1216 1207
1217 /* Resolve names in the result set. */ 1208 /* Resolve names in the result set. */
1218 pEList = p->pEList; 1209 if( sqlite3ResolveExprListNames(&sNC, p->pEList) ) return WRC_Abort;
1219 assert( pEList!=0 );
1220 for(i=0; i<pEList->nExpr; i++){
1221 Expr *pX = pEList->a[i].pExpr;
1222 if( sqlite3ResolveExprNames(&sNC, pX) ){
1223 return WRC_Abort;
1224 }
1225 }
1226 1210
1227 /* If there are no aggregate functions in the result-set, and no GROUP BY 1211 /* If there are no aggregate functions in the result-set, and no GROUP BY
1228 ** expression, do not allow aggregates in any of the other expressions. 1212 ** expression, do not allow aggregates in any of the other expressions.
1229 */ 1213 */
1230 assert( (p->selFlags & SF_Aggregate)==0 ); 1214 assert( (p->selFlags & SF_Aggregate)==0 );
1231 pGroupBy = p->pGroupBy; 1215 pGroupBy = p->pGroupBy;
1232 if( pGroupBy || (sNC.ncFlags & NC_HasAgg)!=0 ){ 1216 if( pGroupBy || (sNC.ncFlags & NC_HasAgg)!=0 ){
1233 assert( NC_MinMaxAgg==SF_MinMaxAgg ); 1217 assert( NC_MinMaxAgg==SF_MinMaxAgg );
1234 p->selFlags |= SF_Aggregate | (sNC.ncFlags&NC_MinMaxAgg); 1218 p->selFlags |= SF_Aggregate | (sNC.ncFlags&NC_MinMaxAgg);
1235 }else{ 1219 }else{
(...skipping 12 matching lines...) Expand all
1248 ** expressions in the WHERE clause (etc.) can refer to expressions by 1232 ** expressions in the WHERE clause (etc.) can refer to expressions by
1249 ** aliases in the result set. 1233 ** aliases in the result set.
1250 ** 1234 **
1251 ** Minor point: If this is the case, then the expression will be 1235 ** Minor point: If this is the case, then the expression will be
1252 ** re-evaluated for each reference to it. 1236 ** re-evaluated for each reference to it.
1253 */ 1237 */
1254 sNC.pEList = p->pEList; 1238 sNC.pEList = p->pEList;
1255 if( sqlite3ResolveExprNames(&sNC, p->pHaving) ) return WRC_Abort; 1239 if( sqlite3ResolveExprNames(&sNC, p->pHaving) ) return WRC_Abort;
1256 if( sqlite3ResolveExprNames(&sNC, p->pWhere) ) return WRC_Abort; 1240 if( sqlite3ResolveExprNames(&sNC, p->pWhere) ) return WRC_Abort;
1257 1241
1242 /* Resolve names in table-valued-function arguments */
1243 for(i=0; i<p->pSrc->nSrc; i++){
1244 struct SrcList_item *pItem = &p->pSrc->a[i];
1245 if( pItem->fg.isTabFunc
1246 && sqlite3ResolveExprListNames(&sNC, pItem->u1.pFuncArg)
1247 ){
1248 return WRC_Abort;
1249 }
1250 }
1251
1258 /* The ORDER BY and GROUP BY clauses may not refer to terms in 1252 /* The ORDER BY and GROUP BY clauses may not refer to terms in
1259 ** outer queries 1253 ** outer queries
1260 */ 1254 */
1261 sNC.pNext = 0; 1255 sNC.pNext = 0;
1262 sNC.ncFlags |= NC_AllowAgg; 1256 sNC.ncFlags |= NC_AllowAgg;
1263 1257
1258 /* If this is a converted compound query, move the ORDER BY clause from
1259 ** the sub-query back to the parent query. At this point each term
1260 ** within the ORDER BY clause has been transformed to an integer value.
1261 ** These integers will be replaced by copies of the corresponding result
1262 ** set expressions by the call to resolveOrderGroupBy() below. */
1263 if( p->selFlags & SF_Converted ){
1264 Select *pSub = p->pSrc->a[0].pSelect;
1265 p->pOrderBy = pSub->pOrderBy;
1266 pSub->pOrderBy = 0;
1267 }
1268
1264 /* Process the ORDER BY clause for singleton SELECT statements. 1269 /* Process the ORDER BY clause for singleton SELECT statements.
1265 ** The ORDER BY clause for compounds SELECT statements is handled 1270 ** The ORDER BY clause for compounds SELECT statements is handled
1266 ** below, after all of the result-sets for all of the elements of 1271 ** below, after all of the result-sets for all of the elements of
1267 ** the compound have been resolved. 1272 ** the compound have been resolved.
1273 **
1274 ** If there is an ORDER BY clause on a term of a compound-select other
1275 ** than the right-most term, then that is a syntax error. But the error
1276 ** is not detected until much later, and so we need to go ahead and
1277 ** resolve those symbols on the incorrect ORDER BY for consistency.
1268 */ 1278 */
1269 if( !isCompound && resolveOrderGroupBy(&sNC, p, p->pOrderBy, "ORDER") ){ 1279 if( isCompound<=nCompound /* Defer right-most ORDER BY of a compound */
1280 && resolveOrderGroupBy(&sNC, p, p->pOrderBy, "ORDER")
1281 ){
1270 return WRC_Abort; 1282 return WRC_Abort;
1271 } 1283 }
1272 if( db->mallocFailed ){ 1284 if( db->mallocFailed ){
1273 return WRC_Abort; 1285 return WRC_Abort;
1274 } 1286 }
1275 1287
1276 /* Resolve the GROUP BY clause. At the same time, make sure 1288 /* Resolve the GROUP BY clause. At the same time, make sure
1277 ** the GROUP BY clause does not contain aggregate functions. 1289 ** the GROUP BY clause does not contain aggregate functions.
1278 */ 1290 */
1279 if( pGroupBy ){ 1291 if( pGroupBy ){
1280 struct ExprList_item *pItem; 1292 struct ExprList_item *pItem;
1281 1293
1282 if( resolveOrderGroupBy(&sNC, p, pGroupBy, "GROUP") || db->mallocFailed ){ 1294 if( resolveOrderGroupBy(&sNC, p, pGroupBy, "GROUP") || db->mallocFailed ){
1283 return WRC_Abort; 1295 return WRC_Abort;
1284 } 1296 }
1285 for(i=0, pItem=pGroupBy->a; i<pGroupBy->nExpr; i++, pItem++){ 1297 for(i=0, pItem=pGroupBy->a; i<pGroupBy->nExpr; i++, pItem++){
1286 if( ExprHasProperty(pItem->pExpr, EP_Agg) ){ 1298 if( ExprHasProperty(pItem->pExpr, EP_Agg) ){
1287 sqlite3ErrorMsg(pParse, "aggregate functions are not allowed in " 1299 sqlite3ErrorMsg(pParse, "aggregate functions are not allowed in "
1288 "the GROUP BY clause"); 1300 "the GROUP BY clause");
1289 return WRC_Abort; 1301 return WRC_Abort;
1290 } 1302 }
1291 } 1303 }
1292 } 1304 }
1293 1305
1306 /* If this is part of a compound SELECT, check that it has the right
1307 ** number of expressions in the select list. */
1308 if( p->pNext && p->pEList->nExpr!=p->pNext->pEList->nExpr ){
1309 sqlite3SelectWrongNumTermsError(pParse, p->pNext);
1310 return WRC_Abort;
1311 }
1312
1294 /* Advance to the next term of the compound 1313 /* Advance to the next term of the compound
1295 */ 1314 */
1296 p = p->pPrior; 1315 p = p->pPrior;
1297 nCompound++; 1316 nCompound++;
1298 } 1317 }
1299 1318
1300 /* Resolve the ORDER BY on a compound SELECT after all terms of 1319 /* Resolve the ORDER BY on a compound SELECT after all terms of
1301 ** the compound have been resolved. 1320 ** the compound have been resolved.
1302 */ 1321 */
1303 if( isCompound && resolveCompoundOrderBy(pParse, pLeftmost) ){ 1322 if( isCompound && resolveCompoundOrderBy(pParse, pLeftmost) ){
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
1386 if( pNC->nErr>0 || w.pParse->nErr>0 ){ 1405 if( pNC->nErr>0 || w.pParse->nErr>0 ){
1387 ExprSetProperty(pExpr, EP_Error); 1406 ExprSetProperty(pExpr, EP_Error);
1388 } 1407 }
1389 if( pNC->ncFlags & NC_HasAgg ){ 1408 if( pNC->ncFlags & NC_HasAgg ){
1390 ExprSetProperty(pExpr, EP_Agg); 1409 ExprSetProperty(pExpr, EP_Agg);
1391 } 1410 }
1392 pNC->ncFlags |= savedHasAgg; 1411 pNC->ncFlags |= savedHasAgg;
1393 return ExprHasProperty(pExpr, EP_Error); 1412 return ExprHasProperty(pExpr, EP_Error);
1394 } 1413 }
1395 1414
1415 /*
1416 ** Resolve all names for all expression in an expression list. This is
1417 ** just like sqlite3ResolveExprNames() except that it works for an expression
1418 ** list rather than a single expression.
1419 */
1420 int sqlite3ResolveExprListNames(
1421 NameContext *pNC, /* Namespace to resolve expressions in. */
1422 ExprList *pList /* The expression list to be analyzed. */
1423 ){
1424 int i;
1425 if( pList ){
1426 for(i=0; i<pList->nExpr; i++){
1427 if( sqlite3ResolveExprNames(pNC, pList->a[i].pExpr) ) return WRC_Abort;
1428 }
1429 }
1430 return WRC_Continue;
1431 }
1396 1432
1397 /* 1433 /*
1398 ** Resolve all names in all expressions of a SELECT and in all 1434 ** Resolve all names in all expressions of a SELECT and in all
1399 ** decendents of the SELECT, including compounds off of p->pPrior, 1435 ** decendents of the SELECT, including compounds off of p->pPrior,
1400 ** subqueries in expressions, and subqueries used as FROM clause 1436 ** subqueries in expressions, and subqueries used as FROM clause
1401 ** terms. 1437 ** terms.
1402 ** 1438 **
1403 ** See sqlite3ResolveExprNames() for a description of the kinds of 1439 ** See sqlite3ResolveExprNames() for a description of the kinds of
1404 ** transformations that occur. 1440 ** transformations that occur.
1405 ** 1441 **
(...skipping 23 matching lines...) Expand all
1429 ** * WHERE clauses on partial indices 1465 ** * WHERE clauses on partial indices
1430 ** 1466 **
1431 ** The Expr.iTable value for Expr.op==TK_COLUMN nodes of the expression 1467 ** The Expr.iTable value for Expr.op==TK_COLUMN nodes of the expression
1432 ** is set to -1 and the Expr.iColumn value is set to the column number. 1468 ** is set to -1 and the Expr.iColumn value is set to the column number.
1433 ** 1469 **
1434 ** Any errors cause an error message to be set in pParse. 1470 ** Any errors cause an error message to be set in pParse.
1435 */ 1471 */
1436 void sqlite3ResolveSelfReference( 1472 void sqlite3ResolveSelfReference(
1437 Parse *pParse, /* Parsing context */ 1473 Parse *pParse, /* Parsing context */
1438 Table *pTab, /* The table being referenced */ 1474 Table *pTab, /* The table being referenced */
1439 int type, /* NC_IsCheck or NC_PartIdx */ 1475 int type, /* NC_IsCheck or NC_PartIdx or NC_IdxExpr */
1440 Expr *pExpr, /* Expression to resolve. May be NULL. */ 1476 Expr *pExpr, /* Expression to resolve. May be NULL. */
1441 ExprList *pList /* Expression list to resolve. May be NUL. */ 1477 ExprList *pList /* Expression list to resolve. May be NUL. */
1442 ){ 1478 ){
1443 SrcList sSrc; /* Fake SrcList for pParse->pNewTable */ 1479 SrcList sSrc; /* Fake SrcList for pParse->pNewTable */
1444 NameContext sNC; /* Name context for pParse->pNewTable */ 1480 NameContext sNC; /* Name context for pParse->pNewTable */
1445 int i; /* Loop counter */
1446 1481
1447 assert( type==NC_IsCheck || type==NC_PartIdx ); 1482 assert( type==NC_IsCheck || type==NC_PartIdx || type==NC_IdxExpr );
1448 memset(&sNC, 0, sizeof(sNC)); 1483 memset(&sNC, 0, sizeof(sNC));
1449 memset(&sSrc, 0, sizeof(sSrc)); 1484 memset(&sSrc, 0, sizeof(sSrc));
1450 sSrc.nSrc = 1; 1485 sSrc.nSrc = 1;
1451 sSrc.a[0].zName = pTab->zName; 1486 sSrc.a[0].zName = pTab->zName;
1452 sSrc.a[0].pTab = pTab; 1487 sSrc.a[0].pTab = pTab;
1453 sSrc.a[0].iCursor = -1; 1488 sSrc.a[0].iCursor = -1;
1454 sNC.pParse = pParse; 1489 sNC.pParse = pParse;
1455 sNC.pSrcList = &sSrc; 1490 sNC.pSrcList = &sSrc;
1456 sNC.ncFlags = type; 1491 sNC.ncFlags = type;
1457 if( sqlite3ResolveExprNames(&sNC, pExpr) ) return; 1492 if( sqlite3ResolveExprNames(&sNC, pExpr) ) return;
1458 if( pList ){ 1493 if( pList ) sqlite3ResolveExprListNames(&sNC, pList);
1459 for(i=0; i<pList->nExpr; i++){
1460 if( sqlite3ResolveExprNames(&sNC, pList->a[i].pExpr) ){
1461 return;
1462 }
1463 }
1464 }
1465 } 1494 }
OLDNEW
« no previous file with comments | « third_party/sqlite/sqlite-src-3100200/src/random.c ('k') | third_party/sqlite/sqlite-src-3100200/src/rowset.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698