drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 1 | /* |
| 2 | ** 2008 August 18 |
| 3 | ** |
| 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
| 6 | ** |
| 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. |
| 10 | ** |
| 11 | ************************************************************************* |
| 12 | ** |
| 13 | ** This file contains routines used for walking the parser tree and |
| 14 | ** resolve all identifiers by associating them with a particular |
| 15 | ** table and column. |
| 16 | ** |
drh | 33e619f | 2009-05-28 01:00:55 +0000 | [diff] [blame^] | 17 | ** $Id: resolve.c,v 1.24 2009/05/28 01:00:55 drh Exp $ |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 18 | */ |
| 19 | #include "sqliteInt.h" |
| 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
| 22 | |
| 23 | /* |
drh | 8b21389 | 2008-08-29 02:14:02 +0000 | [diff] [blame] | 24 | ** Turn the pExpr expression into an alias for the iCol-th column of the |
| 25 | ** result set in pEList. |
| 26 | ** |
| 27 | ** If the result set column is a simple column reference, then this routine |
| 28 | ** makes an exact copy. But for any other kind of expression, this |
| 29 | ** routine make a copy of the result set column as the argument to the |
| 30 | ** TK_AS operator. The TK_AS operator causes the expression to be |
| 31 | ** evaluated just once and then reused for each alias. |
| 32 | ** |
| 33 | ** The reason for suppressing the TK_AS term when the expression is a simple |
| 34 | ** column reference is so that the column reference will be recognized as |
| 35 | ** usable by indices within the WHERE clause processing logic. |
| 36 | ** |
| 37 | ** Hack: The TK_AS operator is inhibited if zType[0]=='G'. This means |
| 38 | ** that in a GROUP BY clause, the expression is evaluated twice. Hence: |
| 39 | ** |
| 40 | ** SELECT random()%5 AS x, count(*) FROM tab GROUP BY x |
| 41 | ** |
| 42 | ** Is equivalent to: |
| 43 | ** |
| 44 | ** SELECT random()%5 AS x, count(*) FROM tab GROUP BY random()%5 |
| 45 | ** |
| 46 | ** The result of random()%5 in the GROUP BY clause is probably different |
| 47 | ** from the result in the result-set. We might fix this someday. Or |
| 48 | ** then again, we might not... |
| 49 | */ |
| 50 | static void resolveAlias( |
| 51 | Parse *pParse, /* Parsing context */ |
| 52 | ExprList *pEList, /* A result set */ |
| 53 | int iCol, /* A column in the result set. 0..pEList->nExpr-1 */ |
| 54 | Expr *pExpr, /* Transform this into an alias to the result set */ |
| 55 | const char *zType /* "GROUP" or "ORDER" or "" */ |
| 56 | ){ |
| 57 | Expr *pOrig; /* The iCol-th column of the result set */ |
| 58 | Expr *pDup; /* Copy of pOrig */ |
| 59 | sqlite3 *db; /* The database connection */ |
| 60 | |
| 61 | assert( iCol>=0 && iCol<pEList->nExpr ); |
| 62 | pOrig = pEList->a[iCol].pExpr; |
| 63 | assert( pOrig!=0 ); |
| 64 | assert( pOrig->flags & EP_Resolved ); |
| 65 | db = pParse->db; |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 66 | if( pOrig->op!=TK_COLUMN && zType[0]!='G' ){ |
| 67 | pDup = sqlite3ExprDup(db, pOrig, 0); |
drh | 8b21389 | 2008-08-29 02:14:02 +0000 | [diff] [blame] | 68 | pDup = sqlite3PExpr(pParse, TK_AS, pDup, 0, 0); |
| 69 | if( pDup==0 ) return; |
| 70 | if( pEList->a[iCol].iAlias==0 ){ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 71 | pEList->a[iCol].iAlias = (u16)(++pParse->nAlias); |
drh | 8b21389 | 2008-08-29 02:14:02 +0000 | [diff] [blame] | 72 | } |
| 73 | pDup->iTable = pEList->a[iCol].iAlias; |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 74 | }else{ |
drh | 33e619f | 2009-05-28 01:00:55 +0000 | [diff] [blame^] | 75 | char *zToken = pOrig->u.zToken; |
| 76 | pOrig->u.zToken = 0; |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 77 | pDup = sqlite3ExprDup(db, pOrig, 0); |
drh | 33e619f | 2009-05-28 01:00:55 +0000 | [diff] [blame^] | 78 | pOrig->u.zToken = zToken; |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 79 | if( pDup==0 ) return; |
| 80 | if( zToken ){ |
| 81 | assert( (pDup->flags & (EP_Reduced|EP_TokenOnly))==0 ); |
drh | 33e619f | 2009-05-28 01:00:55 +0000 | [diff] [blame^] | 82 | pDup->flags2 |= EP2_MallocedToken; |
| 83 | pDup->u.zToken = sqlite3DbStrDup(db, zToken); |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 84 | } |
drh | 8b21389 | 2008-08-29 02:14:02 +0000 | [diff] [blame] | 85 | } |
| 86 | if( pExpr->flags & EP_ExpCollate ){ |
| 87 | pDup->pColl = pExpr->pColl; |
| 88 | pDup->flags |= EP_ExpCollate; |
| 89 | } |
drh | 10fe840 | 2008-10-11 16:47:35 +0000 | [diff] [blame] | 90 | sqlite3ExprClear(db, pExpr); |
drh | 8b21389 | 2008-08-29 02:14:02 +0000 | [diff] [blame] | 91 | memcpy(pExpr, pDup, sizeof(*pExpr)); |
| 92 | sqlite3DbFree(db, pDup); |
| 93 | } |
| 94 | |
| 95 | /* |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 96 | ** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up |
| 97 | ** that name in the set of source tables in pSrcList and make the pExpr |
| 98 | ** expression node refer back to that source column. The following changes |
| 99 | ** are made to pExpr: |
| 100 | ** |
| 101 | ** pExpr->iDb Set the index in db->aDb[] of the database X |
| 102 | ** (even if X is implied). |
| 103 | ** pExpr->iTable Set to the cursor number for the table obtained |
| 104 | ** from pSrcList. |
| 105 | ** pExpr->pTab Points to the Table structure of X.Y (even if |
| 106 | ** X and/or Y are implied.) |
| 107 | ** pExpr->iColumn Set to the column number within the table. |
| 108 | ** pExpr->op Set to TK_COLUMN. |
| 109 | ** pExpr->pLeft Any expression this points to is deleted |
| 110 | ** pExpr->pRight Any expression this points to is deleted. |
| 111 | ** |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 112 | ** The zDb variable is the name of the database (the "X"). This value may be |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 113 | ** NULL meaning that name is of the form Y.Z or Z. Any available database |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 114 | ** can be used. The zTable variable is the name of the table (the "Y"). This |
| 115 | ** value can be NULL if zDb is also NULL. If zTable is NULL it |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 116 | ** means that the form of the name is Z and that columns from any table |
| 117 | ** can be used. |
| 118 | ** |
| 119 | ** If the name cannot be resolved unambiguously, leave an error message |
| 120 | ** in pParse and return non-zero. Return zero on success. |
| 121 | */ |
| 122 | static int lookupName( |
| 123 | Parse *pParse, /* The parsing context */ |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 124 | const char *zDb, /* Name of the database containing table, or NULL */ |
| 125 | const char *zTab, /* Name of table containing column, or NULL */ |
| 126 | const char *zCol, /* Name of the column. */ |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 127 | NameContext *pNC, /* The name context used to resolve the name */ |
| 128 | Expr *pExpr /* Make this EXPR node point to the selected column */ |
| 129 | ){ |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 130 | int i, j; /* Loop counters */ |
| 131 | int cnt = 0; /* Number of matching column names */ |
| 132 | int cntTab = 0; /* Number of matching table names */ |
| 133 | sqlite3 *db = pParse->db; /* The database connection */ |
| 134 | struct SrcList_item *pItem; /* Use for looping over pSrcList items */ |
| 135 | struct SrcList_item *pMatch = 0; /* The matching pSrcList item */ |
| 136 | NameContext *pTopNC = pNC; /* First namecontext in the list */ |
| 137 | Schema *pSchema = 0; /* Schema of the expression */ |
| 138 | |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 139 | assert( pNC ); /* the name context cannot be NULL. */ |
| 140 | assert( zCol ); /* The Z in X.Y.Z cannot be NULL */ |
drh | 33e619f | 2009-05-28 01:00:55 +0000 | [diff] [blame^] | 141 | assert( ~ExprHasAnyProperty(pExpr, EP_TokenOnly|EP_Reduced) ); |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 142 | |
| 143 | /* Initialize the node to no-match */ |
| 144 | pExpr->iTable = -1; |
| 145 | pExpr->pTab = 0; |
drh | 33e619f | 2009-05-28 01:00:55 +0000 | [diff] [blame^] | 146 | ExprSetIrreducible(pExpr); |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 147 | |
| 148 | /* Start at the inner-most context and move outward until a match is found */ |
| 149 | while( pNC && cnt==0 ){ |
| 150 | ExprList *pEList; |
| 151 | SrcList *pSrcList = pNC->pSrcList; |
| 152 | |
| 153 | if( pSrcList ){ |
| 154 | for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){ |
| 155 | Table *pTab; |
| 156 | int iDb; |
| 157 | Column *pCol; |
| 158 | |
| 159 | pTab = pItem->pTab; |
drh | f436620 | 2008-08-25 12:14:08 +0000 | [diff] [blame] | 160 | assert( pTab!=0 && pTab->zName!=0 ); |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 161 | iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
| 162 | assert( pTab->nCol>0 ); |
| 163 | if( zTab ){ |
| 164 | if( pItem->zAlias ){ |
| 165 | char *zTabName = pItem->zAlias; |
| 166 | if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue; |
| 167 | }else{ |
| 168 | char *zTabName = pTab->zName; |
| 169 | if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue; |
| 170 | if( zDb!=0 && sqlite3StrICmp(db->aDb[iDb].zName, zDb)!=0 ){ |
| 171 | continue; |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | if( 0==(cntTab++) ){ |
| 176 | pExpr->iTable = pItem->iCursor; |
| 177 | pExpr->pTab = pTab; |
| 178 | pSchema = pTab->pSchema; |
| 179 | pMatch = pItem; |
| 180 | } |
| 181 | for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){ |
| 182 | if( sqlite3StrICmp(pCol->zName, zCol)==0 ){ |
| 183 | IdList *pUsing; |
| 184 | cnt++; |
| 185 | pExpr->iTable = pItem->iCursor; |
| 186 | pExpr->pTab = pTab; |
| 187 | pMatch = pItem; |
| 188 | pSchema = pTab->pSchema; |
| 189 | /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */ |
| 190 | pExpr->iColumn = j==pTab->iPKey ? -1 : j; |
| 191 | if( i<pSrcList->nSrc-1 ){ |
| 192 | if( pItem[1].jointype & JT_NATURAL ){ |
| 193 | /* If this match occurred in the left table of a natural join, |
| 194 | ** then skip the right table to avoid a duplicate match */ |
| 195 | pItem++; |
| 196 | i++; |
| 197 | }else if( (pUsing = pItem[1].pUsing)!=0 ){ |
| 198 | /* If this match occurs on a column that is in the USING clause |
| 199 | ** of a join, skip the search of the right table of the join |
| 200 | ** to avoid a duplicate match there. */ |
| 201 | int k; |
| 202 | for(k=0; k<pUsing->nId; k++){ |
| 203 | if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ){ |
| 204 | pItem++; |
| 205 | i++; |
| 206 | break; |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | break; |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | #ifndef SQLITE_OMIT_TRIGGER |
| 218 | /* If we have not already resolved the name, then maybe |
| 219 | ** it is a new.* or old.* trigger argument reference |
| 220 | */ |
| 221 | if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){ |
| 222 | TriggerStack *pTriggerStack = pParse->trigStack; |
| 223 | Table *pTab = 0; |
drh | b27b7f5 | 2008-12-10 18:03:45 +0000 | [diff] [blame] | 224 | u32 *piColMask = 0; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 225 | if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){ |
| 226 | pExpr->iTable = pTriggerStack->newIdx; |
| 227 | assert( pTriggerStack->pTab ); |
| 228 | pTab = pTriggerStack->pTab; |
| 229 | piColMask = &(pTriggerStack->newColMask); |
| 230 | }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab)==0 ){ |
| 231 | pExpr->iTable = pTriggerStack->oldIdx; |
| 232 | assert( pTriggerStack->pTab ); |
| 233 | pTab = pTriggerStack->pTab; |
| 234 | piColMask = &(pTriggerStack->oldColMask); |
| 235 | } |
| 236 | |
| 237 | if( pTab ){ |
| 238 | int iCol; |
| 239 | Column *pCol = pTab->aCol; |
| 240 | |
| 241 | pSchema = pTab->pSchema; |
| 242 | cntTab++; |
| 243 | for(iCol=0; iCol < pTab->nCol; iCol++, pCol++) { |
| 244 | if( sqlite3StrICmp(pCol->zName, zCol)==0 ){ |
| 245 | cnt++; |
| 246 | pExpr->iColumn = iCol==pTab->iPKey ? -1 : iCol; |
| 247 | pExpr->pTab = pTab; |
| 248 | if( iCol>=0 ){ |
| 249 | testcase( iCol==31 ); |
| 250 | testcase( iCol==32 ); |
drh | 3500ed6 | 2009-05-05 15:46:43 +0000 | [diff] [blame] | 251 | if( iCol>=32 ){ |
| 252 | *piColMask = 0xffffffff; |
| 253 | }else{ |
| 254 | *piColMask |= ((u32)1)<<iCol; |
| 255 | } |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 256 | } |
| 257 | break; |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | } |
| 262 | #endif /* !defined(SQLITE_OMIT_TRIGGER) */ |
| 263 | |
| 264 | /* |
| 265 | ** Perhaps the name is a reference to the ROWID |
| 266 | */ |
| 267 | if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){ |
| 268 | cnt = 1; |
| 269 | pExpr->iColumn = -1; |
| 270 | pExpr->affinity = SQLITE_AFF_INTEGER; |
| 271 | } |
| 272 | |
| 273 | /* |
| 274 | ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z |
| 275 | ** might refer to an result-set alias. This happens, for example, when |
| 276 | ** we are resolving names in the WHERE clause of the following command: |
| 277 | ** |
| 278 | ** SELECT a+b AS x FROM table WHERE x<10; |
| 279 | ** |
| 280 | ** In cases like this, replace pExpr with a copy of the expression that |
| 281 | ** forms the result set entry ("a+b" in the example) and return immediately. |
| 282 | ** Note that the expression in the result set should have already been |
| 283 | ** resolved by the time the WHERE clause is resolved. |
| 284 | */ |
| 285 | if( cnt==0 && (pEList = pNC->pEList)!=0 && zTab==0 ){ |
| 286 | for(j=0; j<pEList->nExpr; j++){ |
| 287 | char *zAs = pEList->a[j].zName; |
| 288 | if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){ |
drh | 8b21389 | 2008-08-29 02:14:02 +0000 | [diff] [blame] | 289 | Expr *pOrig; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 290 | assert( pExpr->pLeft==0 && pExpr->pRight==0 ); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 291 | assert( pExpr->x.pList==0 ); |
| 292 | assert( pExpr->x.pSelect==0 ); |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 293 | pOrig = pEList->a[j].pExpr; |
| 294 | if( !pNC->allowAgg && ExprHasProperty(pOrig, EP_Agg) ){ |
| 295 | sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs); |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 296 | return 2; |
| 297 | } |
drh | 8b21389 | 2008-08-29 02:14:02 +0000 | [diff] [blame] | 298 | resolveAlias(pParse, pEList, j, pExpr, ""); |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 299 | cnt = 1; |
| 300 | pMatch = 0; |
| 301 | assert( zTab==0 && zDb==0 ); |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 302 | goto lookupname_end; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 303 | } |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | /* Advance to the next name context. The loop will exit when either |
| 308 | ** we have a match (cnt>0) or when we run out of name contexts. |
| 309 | */ |
| 310 | if( cnt==0 ){ |
| 311 | pNC = pNC->pNext; |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | /* |
| 316 | ** If X and Y are NULL (in other words if only the column name Z is |
| 317 | ** supplied) and the value of Z is enclosed in double-quotes, then |
| 318 | ** Z is a string literal if it doesn't match any column names. In that |
| 319 | ** case, we need to return right away and not make any changes to |
| 320 | ** pExpr. |
| 321 | ** |
| 322 | ** Because no reference was made to outer contexts, the pNC->nRef |
| 323 | ** fields are not changed in any context. |
| 324 | */ |
drh | 24fb627 | 2009-05-01 21:13:36 +0000 | [diff] [blame] | 325 | if( cnt==0 && zTab==0 && ExprHasProperty(pExpr,EP_DblQuoted) ){ |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 326 | pExpr->op = TK_STRING; |
drh | 1885d1c | 2008-10-19 21:03:27 +0000 | [diff] [blame] | 327 | pExpr->pTab = 0; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 328 | return 0; |
| 329 | } |
| 330 | |
| 331 | /* |
| 332 | ** cnt==0 means there was not match. cnt>1 means there were two or |
| 333 | ** more matches. Either way, we have an error. |
| 334 | */ |
| 335 | if( cnt!=1 ){ |
| 336 | const char *zErr; |
| 337 | zErr = cnt==0 ? "no such column" : "ambiguous column name"; |
| 338 | if( zDb ){ |
| 339 | sqlite3ErrorMsg(pParse, "%s: %s.%s.%s", zErr, zDb, zTab, zCol); |
| 340 | }else if( zTab ){ |
| 341 | sqlite3ErrorMsg(pParse, "%s: %s.%s", zErr, zTab, zCol); |
| 342 | }else{ |
| 343 | sqlite3ErrorMsg(pParse, "%s: %s", zErr, zCol); |
| 344 | } |
| 345 | pTopNC->nErr++; |
| 346 | } |
| 347 | |
| 348 | /* If a column from a table in pSrcList is referenced, then record |
| 349 | ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes |
| 350 | ** bit 0 to be set. Column 1 sets bit 1. And so forth. If the |
| 351 | ** column number is greater than the number of bits in the bitmask |
| 352 | ** then set the high-order bit of the bitmask. |
| 353 | */ |
danielk1977 | 2d2e7bd | 2009-02-24 10:14:40 +0000 | [diff] [blame] | 354 | if( pExpr->iColumn>=0 && pMatch!=0 ){ |
| 355 | int n = pExpr->iColumn; |
| 356 | testcase( n==BMS-1 ); |
| 357 | if( n>=BMS ){ |
| 358 | n = BMS-1; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 359 | } |
danielk1977 | 2d2e7bd | 2009-02-24 10:14:40 +0000 | [diff] [blame] | 360 | assert( pMatch->iCursor==pExpr->iTable ); |
| 361 | pMatch->colUsed |= ((Bitmask)1)<<n; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 362 | } |
| 363 | |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 364 | /* Clean up and return |
| 365 | */ |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 366 | sqlite3ExprDelete(db, pExpr->pLeft); |
| 367 | pExpr->pLeft = 0; |
| 368 | sqlite3ExprDelete(db, pExpr->pRight); |
| 369 | pExpr->pRight = 0; |
| 370 | pExpr->op = TK_COLUMN; |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 371 | lookupname_end: |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 372 | if( cnt==1 ){ |
| 373 | assert( pNC!=0 ); |
| 374 | sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList); |
| 375 | /* Increment the nRef value on all name contexts from TopNC up to |
| 376 | ** the point where the name matched. */ |
| 377 | for(;;){ |
| 378 | assert( pTopNC!=0 ); |
| 379 | pTopNC->nRef++; |
| 380 | if( pTopNC==pNC ) break; |
| 381 | pTopNC = pTopNC->pNext; |
| 382 | } |
| 383 | return 0; |
| 384 | } else { |
| 385 | return 1; |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | /* |
| 390 | ** This routine is callback for sqlite3WalkExpr(). |
| 391 | ** |
| 392 | ** Resolve symbolic names into TK_COLUMN operators for the current |
| 393 | ** node in the expression tree. Return 0 to continue the search down |
| 394 | ** the tree or 2 to abort the tree walk. |
| 395 | ** |
| 396 | ** This routine also does error checking and name resolution for |
| 397 | ** function names. The operator for aggregate functions is changed |
| 398 | ** to TK_AGG_FUNCTION. |
| 399 | */ |
| 400 | static int resolveExprStep(Walker *pWalker, Expr *pExpr){ |
| 401 | NameContext *pNC; |
| 402 | Parse *pParse; |
| 403 | |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 404 | pNC = pWalker->u.pNC; |
| 405 | assert( pNC!=0 ); |
| 406 | pParse = pNC->pParse; |
| 407 | assert( pParse==pWalker->pParse ); |
| 408 | |
| 409 | if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return WRC_Prune; |
| 410 | ExprSetProperty(pExpr, EP_Resolved); |
| 411 | #ifndef NDEBUG |
| 412 | if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){ |
| 413 | SrcList *pSrcList = pNC->pSrcList; |
| 414 | int i; |
| 415 | for(i=0; i<pNC->pSrcList->nSrc; i++){ |
| 416 | assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab); |
| 417 | } |
| 418 | } |
| 419 | #endif |
| 420 | switch( pExpr->op ){ |
drh | 41204f1 | 2008-10-06 13:54:35 +0000 | [diff] [blame] | 421 | |
shane | 273f619 | 2008-10-10 04:34:16 +0000 | [diff] [blame] | 422 | #if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY) |
drh | 41204f1 | 2008-10-06 13:54:35 +0000 | [diff] [blame] | 423 | /* The special operator TK_ROW means use the rowid for the first |
| 424 | ** column in the FROM clause. This is used by the LIMIT and ORDER BY |
| 425 | ** clause processing on UPDATE and DELETE statements. |
| 426 | */ |
| 427 | case TK_ROW: { |
| 428 | SrcList *pSrcList = pNC->pSrcList; |
| 429 | struct SrcList_item *pItem; |
| 430 | assert( pSrcList && pSrcList->nSrc==1 ); |
| 431 | pItem = pSrcList->a; |
| 432 | pExpr->op = TK_COLUMN; |
| 433 | pExpr->pTab = pItem->pTab; |
| 434 | pExpr->iTable = pItem->iCursor; |
| 435 | pExpr->iColumn = -1; |
| 436 | pExpr->affinity = SQLITE_AFF_INTEGER; |
| 437 | break; |
| 438 | } |
shane | 273f619 | 2008-10-10 04:34:16 +0000 | [diff] [blame] | 439 | #endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY) */ |
drh | 41204f1 | 2008-10-06 13:54:35 +0000 | [diff] [blame] | 440 | |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 441 | /* A lone identifier is the name of a column. |
| 442 | */ |
| 443 | case TK_ID: { |
drh | 33e619f | 2009-05-28 01:00:55 +0000 | [diff] [blame^] | 444 | lookupName(pParse, 0, 0, pExpr->u.zToken, pNC, pExpr); |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 445 | return WRC_Prune; |
| 446 | } |
| 447 | |
| 448 | /* A table name and column name: ID.ID |
| 449 | ** Or a database, table and column: ID.ID.ID |
| 450 | */ |
| 451 | case TK_DOT: { |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 452 | const char *zColumn; |
| 453 | const char *zTable; |
| 454 | const char *zDb; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 455 | Expr *pRight; |
| 456 | |
| 457 | /* if( pSrcList==0 ) break; */ |
| 458 | pRight = pExpr->pRight; |
| 459 | if( pRight->op==TK_ID ){ |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 460 | zDb = 0; |
drh | 33e619f | 2009-05-28 01:00:55 +0000 | [diff] [blame^] | 461 | zTable = pExpr->pLeft->u.zToken; |
| 462 | zColumn = pRight->u.zToken; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 463 | }else{ |
| 464 | assert( pRight->op==TK_DOT ); |
drh | 33e619f | 2009-05-28 01:00:55 +0000 | [diff] [blame^] | 465 | zDb = pExpr->pLeft->u.zToken; |
| 466 | zTable = pRight->pLeft->u.zToken; |
| 467 | zColumn = pRight->pRight->u.zToken; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 468 | } |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 469 | lookupName(pParse, zDb, zTable, zColumn, pNC, pExpr); |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 470 | return WRC_Prune; |
| 471 | } |
| 472 | |
| 473 | /* Resolve function names |
| 474 | */ |
| 475 | case TK_CONST_FUNC: |
| 476 | case TK_FUNCTION: { |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 477 | ExprList *pList = pExpr->x.pList; /* The argument list */ |
| 478 | int n = pList ? pList->nExpr : 0; /* Number of arguments */ |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 479 | int no_such_func = 0; /* True if no such function exists */ |
| 480 | int wrong_num_args = 0; /* True if wrong number of arguments */ |
| 481 | int is_agg = 0; /* True if is an aggregate function */ |
| 482 | int auth; /* Authorization to use the function */ |
| 483 | int nId; /* Number of characters in function name */ |
| 484 | const char *zId; /* The function name. */ |
| 485 | FuncDef *pDef; /* Information about the function */ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 486 | u8 enc = ENC(pParse->db); /* The database encoding */ |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 487 | |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 488 | assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); |
drh | 33e619f | 2009-05-28 01:00:55 +0000 | [diff] [blame^] | 489 | zId = pExpr->u.zToken; |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 490 | nId = sqlite3Strlen30(zId); |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 491 | pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0); |
| 492 | if( pDef==0 ){ |
| 493 | pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0); |
| 494 | if( pDef==0 ){ |
| 495 | no_such_func = 1; |
| 496 | }else{ |
| 497 | wrong_num_args = 1; |
| 498 | } |
| 499 | }else{ |
| 500 | is_agg = pDef->xFunc==0; |
| 501 | } |
| 502 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 503 | if( pDef ){ |
| 504 | auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0); |
| 505 | if( auth!=SQLITE_OK ){ |
| 506 | if( auth==SQLITE_DENY ){ |
| 507 | sqlite3ErrorMsg(pParse, "not authorized to use function: %s", |
| 508 | pDef->zName); |
| 509 | pNC->nErr++; |
| 510 | } |
| 511 | pExpr->op = TK_NULL; |
| 512 | return WRC_Prune; |
| 513 | } |
| 514 | } |
| 515 | #endif |
| 516 | if( is_agg && !pNC->allowAgg ){ |
| 517 | sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId); |
| 518 | pNC->nErr++; |
| 519 | is_agg = 0; |
| 520 | }else if( no_such_func ){ |
| 521 | sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId); |
| 522 | pNC->nErr++; |
| 523 | }else if( wrong_num_args ){ |
| 524 | sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()", |
| 525 | nId, zId); |
| 526 | pNC->nErr++; |
| 527 | } |
| 528 | if( is_agg ){ |
| 529 | pExpr->op = TK_AGG_FUNCTION; |
| 530 | pNC->hasAgg = 1; |
| 531 | } |
| 532 | if( is_agg ) pNC->allowAgg = 0; |
| 533 | sqlite3WalkExprList(pWalker, pList); |
| 534 | if( is_agg ) pNC->allowAgg = 1; |
| 535 | /* FIX ME: Compute pExpr->affinity based on the expected return |
| 536 | ** type of the function |
| 537 | */ |
| 538 | return WRC_Prune; |
| 539 | } |
| 540 | #ifndef SQLITE_OMIT_SUBQUERY |
| 541 | case TK_SELECT: |
| 542 | case TK_EXISTS: |
| 543 | #endif |
| 544 | case TK_IN: { |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 545 | if( ExprHasProperty(pExpr, EP_xIsSelect) ){ |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 546 | int nRef = pNC->nRef; |
| 547 | #ifndef SQLITE_OMIT_CHECK |
| 548 | if( pNC->isCheck ){ |
| 549 | sqlite3ErrorMsg(pParse,"subqueries prohibited in CHECK constraints"); |
| 550 | } |
| 551 | #endif |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 552 | sqlite3WalkSelect(pWalker, pExpr->x.pSelect); |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 553 | assert( pNC->nRef>=nRef ); |
| 554 | if( nRef!=pNC->nRef ){ |
| 555 | ExprSetProperty(pExpr, EP_VarSelect); |
| 556 | } |
| 557 | } |
| 558 | break; |
| 559 | } |
| 560 | #ifndef SQLITE_OMIT_CHECK |
| 561 | case TK_VARIABLE: { |
| 562 | if( pNC->isCheck ){ |
| 563 | sqlite3ErrorMsg(pParse,"parameters prohibited in CHECK constraints"); |
| 564 | } |
| 565 | break; |
| 566 | } |
| 567 | #endif |
| 568 | } |
| 569 | return (pParse->nErr || pParse->db->mallocFailed) ? WRC_Abort : WRC_Continue; |
| 570 | } |
| 571 | |
| 572 | /* |
| 573 | ** pEList is a list of expressions which are really the result set of the |
| 574 | ** a SELECT statement. pE is a term in an ORDER BY or GROUP BY clause. |
| 575 | ** This routine checks to see if pE is a simple identifier which corresponds |
| 576 | ** to the AS-name of one of the terms of the expression list. If it is, |
| 577 | ** this routine return an integer between 1 and N where N is the number of |
| 578 | ** elements in pEList, corresponding to the matching entry. If there is |
| 579 | ** no match, or if pE is not a simple identifier, then this routine |
| 580 | ** return 0. |
| 581 | ** |
| 582 | ** pEList has been resolved. pE has not. |
| 583 | */ |
| 584 | static int resolveAsName( |
| 585 | Parse *pParse, /* Parsing context for error messages */ |
| 586 | ExprList *pEList, /* List of expressions to scan */ |
| 587 | Expr *pE /* Expression we are trying to match */ |
| 588 | ){ |
| 589 | int i; /* Loop counter */ |
| 590 | |
drh | 33e619f | 2009-05-28 01:00:55 +0000 | [diff] [blame^] | 591 | if( pE->op==TK_ID || (pE->op==TK_STRING && pE->u.zToken[0]!='\'') ){ |
| 592 | char *zCol = pE->u.zToken; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 593 | for(i=0; i<pEList->nExpr; i++){ |
| 594 | char *zAs = pEList->a[i].zName; |
| 595 | if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){ |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 596 | return i+1; |
| 597 | } |
| 598 | } |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 599 | } |
| 600 | return 0; |
| 601 | } |
| 602 | |
| 603 | /* |
| 604 | ** pE is a pointer to an expression which is a single term in the |
| 605 | ** ORDER BY of a compound SELECT. The expression has not been |
| 606 | ** name resolved. |
| 607 | ** |
| 608 | ** At the point this routine is called, we already know that the |
| 609 | ** ORDER BY term is not an integer index into the result set. That |
| 610 | ** case is handled by the calling routine. |
| 611 | ** |
| 612 | ** Attempt to match pE against result set columns in the left-most |
| 613 | ** SELECT statement. Return the index i of the matching column, |
| 614 | ** as an indication to the caller that it should sort by the i-th column. |
| 615 | ** The left-most column is 1. In other words, the value returned is the |
| 616 | ** same integer value that would be used in the SQL statement to indicate |
| 617 | ** the column. |
| 618 | ** |
| 619 | ** If there is no match, return 0. Return -1 if an error occurs. |
| 620 | */ |
| 621 | static int resolveOrderByTermToExprList( |
| 622 | Parse *pParse, /* Parsing context for error messages */ |
| 623 | Select *pSelect, /* The SELECT statement with the ORDER BY clause */ |
| 624 | Expr *pE /* The specific ORDER BY term */ |
| 625 | ){ |
| 626 | int i; /* Loop counter */ |
| 627 | ExprList *pEList; /* The columns of the result set */ |
| 628 | NameContext nc; /* Name context for resolving pE */ |
| 629 | |
| 630 | assert( sqlite3ExprIsInteger(pE, &i)==0 ); |
| 631 | pEList = pSelect->pEList; |
| 632 | |
| 633 | /* Resolve all names in the ORDER BY term expression |
| 634 | */ |
| 635 | memset(&nc, 0, sizeof(nc)); |
| 636 | nc.pParse = pParse; |
| 637 | nc.pSrcList = pSelect->pSrc; |
| 638 | nc.pEList = pEList; |
| 639 | nc.allowAgg = 1; |
| 640 | nc.nErr = 0; |
| 641 | if( sqlite3ResolveExprNames(&nc, pE) ){ |
| 642 | sqlite3ErrorClear(pParse); |
| 643 | return 0; |
| 644 | } |
| 645 | |
| 646 | /* Try to match the ORDER BY expression against an expression |
| 647 | ** in the result set. Return an 1-based index of the matching |
| 648 | ** result-set entry. |
| 649 | */ |
| 650 | for(i=0; i<pEList->nExpr; i++){ |
| 651 | if( sqlite3ExprCompare(pEList->a[i].pExpr, pE) ){ |
| 652 | return i+1; |
| 653 | } |
| 654 | } |
| 655 | |
| 656 | /* If no match, return 0. */ |
| 657 | return 0; |
| 658 | } |
| 659 | |
| 660 | /* |
| 661 | ** Generate an ORDER BY or GROUP BY term out-of-range error. |
| 662 | */ |
| 663 | static void resolveOutOfRangeError( |
| 664 | Parse *pParse, /* The error context into which to write the error */ |
| 665 | const char *zType, /* "ORDER" or "GROUP" */ |
| 666 | int i, /* The index (1-based) of the term out of range */ |
| 667 | int mx /* Largest permissible value of i */ |
| 668 | ){ |
| 669 | sqlite3ErrorMsg(pParse, |
| 670 | "%r %s BY term out of range - should be " |
| 671 | "between 1 and %d", i, zType, mx); |
| 672 | } |
| 673 | |
| 674 | /* |
| 675 | ** Analyze the ORDER BY clause in a compound SELECT statement. Modify |
| 676 | ** each term of the ORDER BY clause is a constant integer between 1 |
| 677 | ** and N where N is the number of columns in the compound SELECT. |
| 678 | ** |
| 679 | ** ORDER BY terms that are already an integer between 1 and N are |
| 680 | ** unmodified. ORDER BY terms that are integers outside the range of |
| 681 | ** 1 through N generate an error. ORDER BY terms that are expressions |
| 682 | ** are matched against result set expressions of compound SELECT |
| 683 | ** beginning with the left-most SELECT and working toward the right. |
| 684 | ** At the first match, the ORDER BY expression is transformed into |
| 685 | ** the integer column number. |
| 686 | ** |
| 687 | ** Return the number of errors seen. |
| 688 | */ |
| 689 | static int resolveCompoundOrderBy( |
| 690 | Parse *pParse, /* Parsing context. Leave error messages here */ |
| 691 | Select *pSelect /* The SELECT statement containing the ORDER BY */ |
| 692 | ){ |
| 693 | int i; |
| 694 | ExprList *pOrderBy; |
| 695 | ExprList *pEList; |
| 696 | sqlite3 *db; |
| 697 | int moreToDo = 1; |
| 698 | |
| 699 | pOrderBy = pSelect->pOrderBy; |
| 700 | if( pOrderBy==0 ) return 0; |
| 701 | db = pParse->db; |
| 702 | #if SQLITE_MAX_COLUMN |
| 703 | if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){ |
| 704 | sqlite3ErrorMsg(pParse, "too many terms in ORDER BY clause"); |
| 705 | return 1; |
| 706 | } |
| 707 | #endif |
| 708 | for(i=0; i<pOrderBy->nExpr; i++){ |
| 709 | pOrderBy->a[i].done = 0; |
| 710 | } |
| 711 | pSelect->pNext = 0; |
| 712 | while( pSelect->pPrior ){ |
| 713 | pSelect->pPrior->pNext = pSelect; |
| 714 | pSelect = pSelect->pPrior; |
| 715 | } |
| 716 | while( pSelect && moreToDo ){ |
| 717 | struct ExprList_item *pItem; |
| 718 | moreToDo = 0; |
| 719 | pEList = pSelect->pEList; |
drh | 0a846f9 | 2008-08-25 17:23:29 +0000 | [diff] [blame] | 720 | assert( pEList!=0 ); |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 721 | for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){ |
| 722 | int iCol = -1; |
| 723 | Expr *pE, *pDup; |
| 724 | if( pItem->done ) continue; |
| 725 | pE = pItem->pExpr; |
| 726 | if( sqlite3ExprIsInteger(pE, &iCol) ){ |
| 727 | if( iCol<0 || iCol>pEList->nExpr ){ |
| 728 | resolveOutOfRangeError(pParse, "ORDER", i+1, pEList->nExpr); |
| 729 | return 1; |
| 730 | } |
| 731 | }else{ |
| 732 | iCol = resolveAsName(pParse, pEList, pE); |
| 733 | if( iCol==0 ){ |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 734 | pDup = sqlite3ExprDup(db, pE, 0); |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 735 | if( !db->mallocFailed ){ |
| 736 | assert(pDup); |
| 737 | iCol = resolveOrderByTermToExprList(pParse, pSelect, pDup); |
| 738 | } |
| 739 | sqlite3ExprDelete(db, pDup); |
| 740 | } |
| 741 | if( iCol<0 ){ |
| 742 | return 1; |
| 743 | } |
| 744 | } |
| 745 | if( iCol>0 ){ |
| 746 | CollSeq *pColl = pE->pColl; |
| 747 | int flags = pE->flags & EP_ExpCollate; |
| 748 | sqlite3ExprDelete(db, pE); |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 749 | pItem->pExpr = pE = sqlite3Expr(db, TK_INTEGER, 0); |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 750 | if( pE==0 ) return 1; |
| 751 | pE->pColl = pColl; |
| 752 | pE->flags |= EP_IntValue | flags; |
drh | 33e619f | 2009-05-28 01:00:55 +0000 | [diff] [blame^] | 753 | pE->u.iValue = iCol; |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 754 | pItem->iCol = (u16)iCol; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 755 | pItem->done = 1; |
| 756 | }else{ |
| 757 | moreToDo = 1; |
| 758 | } |
| 759 | } |
| 760 | pSelect = pSelect->pNext; |
| 761 | } |
| 762 | for(i=0; i<pOrderBy->nExpr; i++){ |
| 763 | if( pOrderBy->a[i].done==0 ){ |
| 764 | sqlite3ErrorMsg(pParse, "%r ORDER BY term does not match any " |
| 765 | "column in the result set", i+1); |
| 766 | return 1; |
| 767 | } |
| 768 | } |
| 769 | return 0; |
| 770 | } |
| 771 | |
| 772 | /* |
| 773 | ** Check every term in the ORDER BY or GROUP BY clause pOrderBy of |
| 774 | ** the SELECT statement pSelect. If any term is reference to a |
| 775 | ** result set expression (as determined by the ExprList.a.iCol field) |
| 776 | ** then convert that term into a copy of the corresponding result set |
| 777 | ** column. |
| 778 | ** |
| 779 | ** If any errors are detected, add an error message to pParse and |
| 780 | ** return non-zero. Return zero if no errors are seen. |
| 781 | */ |
| 782 | int sqlite3ResolveOrderGroupBy( |
| 783 | Parse *pParse, /* Parsing context. Leave error messages here */ |
| 784 | Select *pSelect, /* The SELECT statement containing the clause */ |
| 785 | ExprList *pOrderBy, /* The ORDER BY or GROUP BY clause to be processed */ |
| 786 | const char *zType /* "ORDER" or "GROUP" */ |
| 787 | ){ |
| 788 | int i; |
| 789 | sqlite3 *db = pParse->db; |
| 790 | ExprList *pEList; |
| 791 | struct ExprList_item *pItem; |
| 792 | |
| 793 | if( pOrderBy==0 || pParse->db->mallocFailed ) return 0; |
| 794 | #if SQLITE_MAX_COLUMN |
| 795 | if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){ |
| 796 | sqlite3ErrorMsg(pParse, "too many terms in %s BY clause", zType); |
| 797 | return 1; |
| 798 | } |
| 799 | #endif |
| 800 | pEList = pSelect->pEList; |
drh | 0a846f9 | 2008-08-25 17:23:29 +0000 | [diff] [blame] | 801 | assert( pEList!=0 ); /* sqlite3SelectNew() guarantees this */ |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 802 | for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){ |
| 803 | if( pItem->iCol ){ |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 804 | if( pItem->iCol>pEList->nExpr ){ |
| 805 | resolveOutOfRangeError(pParse, zType, i+1, pEList->nExpr); |
| 806 | return 1; |
| 807 | } |
drh | 8b21389 | 2008-08-29 02:14:02 +0000 | [diff] [blame] | 808 | resolveAlias(pParse, pEList, pItem->iCol-1, pItem->pExpr, zType); |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 809 | } |
| 810 | } |
| 811 | return 0; |
| 812 | } |
| 813 | |
| 814 | /* |
| 815 | ** pOrderBy is an ORDER BY or GROUP BY clause in SELECT statement pSelect. |
| 816 | ** The Name context of the SELECT statement is pNC. zType is either |
| 817 | ** "ORDER" or "GROUP" depending on which type of clause pOrderBy is. |
| 818 | ** |
| 819 | ** This routine resolves each term of the clause into an expression. |
| 820 | ** If the order-by term is an integer I between 1 and N (where N is the |
| 821 | ** number of columns in the result set of the SELECT) then the expression |
| 822 | ** in the resolution is a copy of the I-th result-set expression. If |
| 823 | ** the order-by term is an identify that corresponds to the AS-name of |
| 824 | ** a result-set expression, then the term resolves to a copy of the |
| 825 | ** result-set expression. Otherwise, the expression is resolved in |
| 826 | ** the usual way - using sqlite3ResolveExprNames(). |
| 827 | ** |
| 828 | ** This routine returns the number of errors. If errors occur, then |
| 829 | ** an appropriate error message might be left in pParse. (OOM errors |
| 830 | ** excepted.) |
| 831 | */ |
| 832 | static int resolveOrderGroupBy( |
| 833 | NameContext *pNC, /* The name context of the SELECT statement */ |
| 834 | Select *pSelect, /* The SELECT statement holding pOrderBy */ |
| 835 | ExprList *pOrderBy, /* An ORDER BY or GROUP BY clause to resolve */ |
| 836 | const char *zType /* Either "ORDER" or "GROUP", as appropriate */ |
| 837 | ){ |
| 838 | int i; /* Loop counter */ |
| 839 | int iCol; /* Column number */ |
| 840 | struct ExprList_item *pItem; /* A term of the ORDER BY clause */ |
| 841 | Parse *pParse; /* Parsing context */ |
| 842 | int nResult; /* Number of terms in the result set */ |
| 843 | |
| 844 | if( pOrderBy==0 ) return 0; |
| 845 | nResult = pSelect->pEList->nExpr; |
| 846 | pParse = pNC->pParse; |
| 847 | for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){ |
| 848 | Expr *pE = pItem->pExpr; |
| 849 | iCol = resolveAsName(pParse, pSelect->pEList, pE); |
| 850 | if( iCol<0 ){ |
| 851 | return 1; /* OOM error */ |
| 852 | } |
| 853 | if( iCol>0 ){ |
| 854 | /* If an AS-name match is found, mark this ORDER BY column as being |
| 855 | ** a copy of the iCol-th result-set column. The subsequent call to |
| 856 | ** sqlite3ResolveOrderGroupBy() will convert the expression to a |
| 857 | ** copy of the iCol-th result-set expression. */ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 858 | pItem->iCol = (u16)iCol; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 859 | continue; |
| 860 | } |
| 861 | if( sqlite3ExprIsInteger(pE, &iCol) ){ |
| 862 | /* The ORDER BY term is an integer constant. Again, set the column |
| 863 | ** number so that sqlite3ResolveOrderGroupBy() will convert the |
| 864 | ** order-by term to a copy of the result-set expression */ |
drh | 0a846f9 | 2008-08-25 17:23:29 +0000 | [diff] [blame] | 865 | if( iCol<1 ){ |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 866 | resolveOutOfRangeError(pParse, zType, i+1, nResult); |
| 867 | return 1; |
| 868 | } |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 869 | pItem->iCol = (u16)iCol; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 870 | continue; |
| 871 | } |
| 872 | |
| 873 | /* Otherwise, treat the ORDER BY term as an ordinary expression */ |
| 874 | pItem->iCol = 0; |
| 875 | if( sqlite3ResolveExprNames(pNC, pE) ){ |
| 876 | return 1; |
| 877 | } |
| 878 | } |
| 879 | return sqlite3ResolveOrderGroupBy(pParse, pSelect, pOrderBy, zType); |
| 880 | } |
| 881 | |
| 882 | /* |
| 883 | ** Resolve names in the SELECT statement p and all of its descendents. |
| 884 | */ |
| 885 | static int resolveSelectStep(Walker *pWalker, Select *p){ |
| 886 | NameContext *pOuterNC; /* Context that contains this SELECT */ |
| 887 | NameContext sNC; /* Name context of this SELECT */ |
| 888 | int isCompound; /* True if p is a compound select */ |
| 889 | int nCompound; /* Number of compound terms processed so far */ |
| 890 | Parse *pParse; /* Parsing context */ |
| 891 | ExprList *pEList; /* Result set expression list */ |
| 892 | int i; /* Loop counter */ |
| 893 | ExprList *pGroupBy; /* The GROUP BY clause */ |
| 894 | Select *pLeftmost; /* Left-most of SELECT of a compound */ |
| 895 | sqlite3 *db; /* Database connection */ |
| 896 | |
| 897 | |
drh | 0a846f9 | 2008-08-25 17:23:29 +0000 | [diff] [blame] | 898 | assert( p!=0 ); |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 899 | if( p->selFlags & SF_Resolved ){ |
| 900 | return WRC_Prune; |
| 901 | } |
| 902 | pOuterNC = pWalker->u.pNC; |
| 903 | pParse = pWalker->pParse; |
| 904 | db = pParse->db; |
| 905 | |
| 906 | /* Normally sqlite3SelectExpand() will be called first and will have |
| 907 | ** already expanded this SELECT. However, if this is a subquery within |
| 908 | ** an expression, sqlite3ResolveExprNames() will be called without a |
| 909 | ** prior call to sqlite3SelectExpand(). When that happens, let |
| 910 | ** sqlite3SelectPrep() do all of the processing for this SELECT. |
| 911 | ** sqlite3SelectPrep() will invoke both sqlite3SelectExpand() and |
| 912 | ** this routine in the correct order. |
| 913 | */ |
| 914 | if( (p->selFlags & SF_Expanded)==0 ){ |
| 915 | sqlite3SelectPrep(pParse, p, pOuterNC); |
| 916 | return (pParse->nErr || db->mallocFailed) ? WRC_Abort : WRC_Prune; |
| 917 | } |
| 918 | |
| 919 | isCompound = p->pPrior!=0; |
| 920 | nCompound = 0; |
| 921 | pLeftmost = p; |
| 922 | while( p ){ |
| 923 | assert( (p->selFlags & SF_Expanded)!=0 ); |
| 924 | assert( (p->selFlags & SF_Resolved)==0 ); |
| 925 | p->selFlags |= SF_Resolved; |
| 926 | |
| 927 | /* Resolve the expressions in the LIMIT and OFFSET clauses. These |
| 928 | ** are not allowed to refer to any names, so pass an empty NameContext. |
| 929 | */ |
| 930 | memset(&sNC, 0, sizeof(sNC)); |
| 931 | sNC.pParse = pParse; |
| 932 | if( sqlite3ResolveExprNames(&sNC, p->pLimit) || |
| 933 | sqlite3ResolveExprNames(&sNC, p->pOffset) ){ |
| 934 | return WRC_Abort; |
| 935 | } |
| 936 | |
| 937 | /* Set up the local name-context to pass to sqlite3ResolveExprNames() to |
| 938 | ** resolve the result-set expression list. |
| 939 | */ |
| 940 | sNC.allowAgg = 1; |
| 941 | sNC.pSrcList = p->pSrc; |
| 942 | sNC.pNext = pOuterNC; |
| 943 | |
| 944 | /* Resolve names in the result set. */ |
| 945 | pEList = p->pEList; |
drh | 0a846f9 | 2008-08-25 17:23:29 +0000 | [diff] [blame] | 946 | assert( pEList!=0 ); |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 947 | for(i=0; i<pEList->nExpr; i++){ |
| 948 | Expr *pX = pEList->a[i].pExpr; |
| 949 | if( sqlite3ResolveExprNames(&sNC, pX) ){ |
| 950 | return WRC_Abort; |
| 951 | } |
| 952 | } |
| 953 | |
| 954 | /* Recursively resolve names in all subqueries |
| 955 | */ |
| 956 | for(i=0; i<p->pSrc->nSrc; i++){ |
| 957 | struct SrcList_item *pItem = &p->pSrc->a[i]; |
| 958 | if( pItem->pSelect ){ |
| 959 | const char *zSavedContext = pParse->zAuthContext; |
| 960 | if( pItem->zName ) pParse->zAuthContext = pItem->zName; |
drh | cd2b561 | 2008-12-09 14:03:22 +0000 | [diff] [blame] | 961 | sqlite3ResolveSelectNames(pParse, pItem->pSelect, pOuterNC); |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 962 | pParse->zAuthContext = zSavedContext; |
| 963 | if( pParse->nErr || db->mallocFailed ) return WRC_Abort; |
| 964 | } |
| 965 | } |
| 966 | |
| 967 | /* If there are no aggregate functions in the result-set, and no GROUP BY |
| 968 | ** expression, do not allow aggregates in any of the other expressions. |
| 969 | */ |
| 970 | assert( (p->selFlags & SF_Aggregate)==0 ); |
| 971 | pGroupBy = p->pGroupBy; |
| 972 | if( pGroupBy || sNC.hasAgg ){ |
| 973 | p->selFlags |= SF_Aggregate; |
| 974 | }else{ |
| 975 | sNC.allowAgg = 0; |
| 976 | } |
| 977 | |
| 978 | /* If a HAVING clause is present, then there must be a GROUP BY clause. |
| 979 | */ |
| 980 | if( p->pHaving && !pGroupBy ){ |
| 981 | sqlite3ErrorMsg(pParse, "a GROUP BY clause is required before HAVING"); |
| 982 | return WRC_Abort; |
| 983 | } |
| 984 | |
| 985 | /* Add the expression list to the name-context before parsing the |
| 986 | ** other expressions in the SELECT statement. This is so that |
| 987 | ** expressions in the WHERE clause (etc.) can refer to expressions by |
| 988 | ** aliases in the result set. |
| 989 | ** |
| 990 | ** Minor point: If this is the case, then the expression will be |
| 991 | ** re-evaluated for each reference to it. |
| 992 | */ |
| 993 | sNC.pEList = p->pEList; |
| 994 | if( sqlite3ResolveExprNames(&sNC, p->pWhere) || |
| 995 | sqlite3ResolveExprNames(&sNC, p->pHaving) |
| 996 | ){ |
| 997 | return WRC_Abort; |
| 998 | } |
| 999 | |
| 1000 | /* The ORDER BY and GROUP BY clauses may not refer to terms in |
| 1001 | ** outer queries |
| 1002 | */ |
| 1003 | sNC.pNext = 0; |
| 1004 | sNC.allowAgg = 1; |
| 1005 | |
| 1006 | /* Process the ORDER BY clause for singleton SELECT statements. |
| 1007 | ** The ORDER BY clause for compounds SELECT statements is handled |
| 1008 | ** below, after all of the result-sets for all of the elements of |
| 1009 | ** the compound have been resolved. |
| 1010 | */ |
| 1011 | if( !isCompound && resolveOrderGroupBy(&sNC, p, p->pOrderBy, "ORDER") ){ |
| 1012 | return WRC_Abort; |
| 1013 | } |
| 1014 | if( db->mallocFailed ){ |
| 1015 | return WRC_Abort; |
| 1016 | } |
| 1017 | |
| 1018 | /* Resolve the GROUP BY clause. At the same time, make sure |
| 1019 | ** the GROUP BY clause does not contain aggregate functions. |
| 1020 | */ |
| 1021 | if( pGroupBy ){ |
| 1022 | struct ExprList_item *pItem; |
| 1023 | |
| 1024 | if( resolveOrderGroupBy(&sNC, p, pGroupBy, "GROUP") || db->mallocFailed ){ |
| 1025 | return WRC_Abort; |
| 1026 | } |
| 1027 | for(i=0, pItem=pGroupBy->a; i<pGroupBy->nExpr; i++, pItem++){ |
| 1028 | if( ExprHasProperty(pItem->pExpr, EP_Agg) ){ |
| 1029 | sqlite3ErrorMsg(pParse, "aggregate functions are not allowed in " |
| 1030 | "the GROUP BY clause"); |
| 1031 | return WRC_Abort; |
| 1032 | } |
| 1033 | } |
| 1034 | } |
| 1035 | |
| 1036 | /* Advance to the next term of the compound |
| 1037 | */ |
| 1038 | p = p->pPrior; |
| 1039 | nCompound++; |
| 1040 | } |
| 1041 | |
| 1042 | /* Resolve the ORDER BY on a compound SELECT after all terms of |
| 1043 | ** the compound have been resolved. |
| 1044 | */ |
| 1045 | if( isCompound && resolveCompoundOrderBy(pParse, pLeftmost) ){ |
| 1046 | return WRC_Abort; |
| 1047 | } |
| 1048 | |
| 1049 | return WRC_Prune; |
| 1050 | } |
| 1051 | |
| 1052 | /* |
| 1053 | ** This routine walks an expression tree and resolves references to |
| 1054 | ** table columns and result-set columns. At the same time, do error |
| 1055 | ** checking on function usage and set a flag if any aggregate functions |
| 1056 | ** are seen. |
| 1057 | ** |
| 1058 | ** To resolve table columns references we look for nodes (or subtrees) of the |
| 1059 | ** form X.Y.Z or Y.Z or just Z where |
| 1060 | ** |
| 1061 | ** X: The name of a database. Ex: "main" or "temp" or |
| 1062 | ** the symbolic name assigned to an ATTACH-ed database. |
| 1063 | ** |
| 1064 | ** Y: The name of a table in a FROM clause. Or in a trigger |
| 1065 | ** one of the special names "old" or "new". |
| 1066 | ** |
| 1067 | ** Z: The name of a column in table Y. |
| 1068 | ** |
| 1069 | ** The node at the root of the subtree is modified as follows: |
| 1070 | ** |
| 1071 | ** Expr.op Changed to TK_COLUMN |
| 1072 | ** Expr.pTab Points to the Table object for X.Y |
| 1073 | ** Expr.iColumn The column index in X.Y. -1 for the rowid. |
| 1074 | ** Expr.iTable The VDBE cursor number for X.Y |
| 1075 | ** |
| 1076 | ** |
| 1077 | ** To resolve result-set references, look for expression nodes of the |
| 1078 | ** form Z (with no X and Y prefix) where the Z matches the right-hand |
| 1079 | ** size of an AS clause in the result-set of a SELECT. The Z expression |
| 1080 | ** is replaced by a copy of the left-hand side of the result-set expression. |
| 1081 | ** Table-name and function resolution occurs on the substituted expression |
| 1082 | ** tree. For example, in: |
| 1083 | ** |
| 1084 | ** SELECT a+b AS x, c+d AS y FROM t1 ORDER BY x; |
| 1085 | ** |
| 1086 | ** The "x" term of the order by is replaced by "a+b" to render: |
| 1087 | ** |
| 1088 | ** SELECT a+b AS x, c+d AS y FROM t1 ORDER BY a+b; |
| 1089 | ** |
| 1090 | ** Function calls are checked to make sure that the function is |
| 1091 | ** defined and that the correct number of arguments are specified. |
| 1092 | ** If the function is an aggregate function, then the pNC->hasAgg is |
| 1093 | ** set and the opcode is changed from TK_FUNCTION to TK_AGG_FUNCTION. |
| 1094 | ** If an expression contains aggregate functions then the EP_Agg |
| 1095 | ** property on the expression is set. |
| 1096 | ** |
| 1097 | ** An error message is left in pParse if anything is amiss. The number |
| 1098 | ** if errors is returned. |
| 1099 | */ |
| 1100 | int sqlite3ResolveExprNames( |
| 1101 | NameContext *pNC, /* Namespace to resolve expressions in. */ |
| 1102 | Expr *pExpr /* The expression to be analyzed. */ |
| 1103 | ){ |
| 1104 | int savedHasAgg; |
| 1105 | Walker w; |
| 1106 | |
| 1107 | if( pExpr==0 ) return 0; |
| 1108 | #if SQLITE_MAX_EXPR_DEPTH>0 |
| 1109 | { |
| 1110 | Parse *pParse = pNC->pParse; |
| 1111 | if( sqlite3ExprCheckHeight(pParse, pExpr->nHeight+pNC->pParse->nHeight) ){ |
| 1112 | return 1; |
| 1113 | } |
| 1114 | pParse->nHeight += pExpr->nHeight; |
| 1115 | } |
| 1116 | #endif |
| 1117 | savedHasAgg = pNC->hasAgg; |
| 1118 | pNC->hasAgg = 0; |
| 1119 | w.xExprCallback = resolveExprStep; |
| 1120 | w.xSelectCallback = resolveSelectStep; |
| 1121 | w.pParse = pNC->pParse; |
| 1122 | w.u.pNC = pNC; |
| 1123 | sqlite3WalkExpr(&w, pExpr); |
| 1124 | #if SQLITE_MAX_EXPR_DEPTH>0 |
| 1125 | pNC->pParse->nHeight -= pExpr->nHeight; |
| 1126 | #endif |
| 1127 | if( pNC->nErr>0 ){ |
| 1128 | ExprSetProperty(pExpr, EP_Error); |
| 1129 | } |
| 1130 | if( pNC->hasAgg ){ |
| 1131 | ExprSetProperty(pExpr, EP_Agg); |
| 1132 | }else if( savedHasAgg ){ |
| 1133 | pNC->hasAgg = 1; |
| 1134 | } |
| 1135 | return ExprHasProperty(pExpr, EP_Error); |
| 1136 | } |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 1137 | |
| 1138 | |
| 1139 | /* |
| 1140 | ** Resolve all names in all expressions of a SELECT and in all |
| 1141 | ** decendents of the SELECT, including compounds off of p->pPrior, |
| 1142 | ** subqueries in expressions, and subqueries used as FROM clause |
| 1143 | ** terms. |
| 1144 | ** |
| 1145 | ** See sqlite3ResolveExprNames() for a description of the kinds of |
| 1146 | ** transformations that occur. |
| 1147 | ** |
| 1148 | ** All SELECT statements should have been expanded using |
| 1149 | ** sqlite3SelectExpand() prior to invoking this routine. |
| 1150 | */ |
| 1151 | void sqlite3ResolveSelectNames( |
| 1152 | Parse *pParse, /* The parser context */ |
| 1153 | Select *p, /* The SELECT statement being coded. */ |
| 1154 | NameContext *pOuterNC /* Name context for parent SELECT statement */ |
| 1155 | ){ |
| 1156 | Walker w; |
| 1157 | |
drh | 0a846f9 | 2008-08-25 17:23:29 +0000 | [diff] [blame] | 1158 | assert( p!=0 ); |
| 1159 | w.xExprCallback = resolveExprStep; |
| 1160 | w.xSelectCallback = resolveSelectStep; |
| 1161 | w.pParse = pParse; |
| 1162 | w.u.pNC = pOuterNC; |
| 1163 | sqlite3WalkSelect(&w, p); |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 1164 | } |