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