blob: a194a26553afbe2283d09e47a6be22ffb292ba61 [file] [log] [blame]
drh7d10d5a2008-08-20 16:35:10 +00001/*
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.
drh7d10d5a2008-08-20 16:35:10 +000016*/
17#include "sqliteInt.h"
18#include <stdlib.h>
19#include <string.h>
20
21/*
drhed551b92012-08-23 19:46:11 +000022** Walk the expression tree pExpr and increase the aggregate function
23** depth (the Expr.op2 field) by N on every TK_AGG_FUNCTION node.
24** This needs to occur when copying a TK_AGG_FUNCTION node from an
25** outer query into an inner subquery.
26**
27** incrAggFunctionDepth(pExpr,n) is the main routine. incrAggDepth(..)
28** is a helper function - a callback for the tree walker.
29*/
30static int incrAggDepth(Walker *pWalker, Expr *pExpr){
31 if( pExpr->op==TK_AGG_FUNCTION ) pExpr->op2 += pWalker->u.i;
32 return WRC_Continue;
33}
34static void incrAggFunctionDepth(Expr *pExpr, int N){
35 if( N>0 ){
36 Walker w;
37 memset(&w, 0, sizeof(w));
38 w.xExprCallback = incrAggDepth;
39 w.u.i = N;
40 sqlite3WalkExpr(&w, pExpr);
41 }
42}
43
44/*
drh8b213892008-08-29 02:14:02 +000045** Turn the pExpr expression into an alias for the iCol-th column of the
46** result set in pEList.
47**
48** If the result set column is a simple column reference, then this routine
49** makes an exact copy. But for any other kind of expression, this
50** routine make a copy of the result set column as the argument to the
51** TK_AS operator. The TK_AS operator causes the expression to be
52** evaluated just once and then reused for each alias.
53**
54** The reason for suppressing the TK_AS term when the expression is a simple
55** column reference is so that the column reference will be recognized as
56** usable by indices within the WHERE clause processing logic.
57**
58** Hack: The TK_AS operator is inhibited if zType[0]=='G'. This means
59** that in a GROUP BY clause, the expression is evaluated twice. Hence:
60**
61** SELECT random()%5 AS x, count(*) FROM tab GROUP BY x
62**
63** Is equivalent to:
64**
65** SELECT random()%5 AS x, count(*) FROM tab GROUP BY random()%5
66**
67** The result of random()%5 in the GROUP BY clause is probably different
68** from the result in the result-set. We might fix this someday. Or
69** then again, we might not...
drhed551b92012-08-23 19:46:11 +000070**
drh0a8a4062012-12-07 18:38:16 +000071** If the reference is followed by a COLLATE operator, then make sure
72** the COLLATE operator is preserved. For example:
73**
74** SELECT a+b, c+d FROM t1 ORDER BY 1 COLLATE nocase;
75**
76** Should be transformed into:
77**
78** SELECT a+b, c+d FROM t1 ORDER BY (a+b) COLLATE nocase;
79**
drhed551b92012-08-23 19:46:11 +000080** The nSubquery parameter specifies how many levels of subquery the
81** alias is removed from the original expression. The usually value is
82** zero but it might be more if the alias is contained within a subquery
83** of the original expression. The Expr.op2 field of TK_AGG_FUNCTION
84** structures must be increased by the nSubquery amount.
drh8b213892008-08-29 02:14:02 +000085*/
86static void resolveAlias(
87 Parse *pParse, /* Parsing context */
88 ExprList *pEList, /* A result set */
89 int iCol, /* A column in the result set. 0..pEList->nExpr-1 */
90 Expr *pExpr, /* Transform this into an alias to the result set */
drhed551b92012-08-23 19:46:11 +000091 const char *zType, /* "GROUP" or "ORDER" or "" */
92 int nSubquery /* Number of subqueries that the label is moving */
drh8b213892008-08-29 02:14:02 +000093){
94 Expr *pOrig; /* The iCol-th column of the result set */
95 Expr *pDup; /* Copy of pOrig */
96 sqlite3 *db; /* The database connection */
97
98 assert( iCol>=0 && iCol<pEList->nExpr );
99 pOrig = pEList->a[iCol].pExpr;
100 assert( pOrig!=0 );
101 assert( pOrig->flags & EP_Resolved );
102 db = pParse->db;
drh0a8a4062012-12-07 18:38:16 +0000103 pDup = sqlite3ExprDup(db, pOrig, 0);
104 if( pDup==0 ) return;
drhb7916a72009-05-27 10:31:29 +0000105 if( pOrig->op!=TK_COLUMN && zType[0]!='G' ){
drhed551b92012-08-23 19:46:11 +0000106 incrAggFunctionDepth(pDup, nSubquery);
drh8b213892008-08-29 02:14:02 +0000107 pDup = sqlite3PExpr(pParse, TK_AS, pDup, 0, 0);
108 if( pDup==0 ) return;
109 if( pEList->a[iCol].iAlias==0 ){
drhea678832008-12-10 19:26:22 +0000110 pEList->a[iCol].iAlias = (u16)(++pParse->nAlias);
drh8b213892008-08-29 02:14:02 +0000111 }
112 pDup->iTable = pEList->a[iCol].iAlias;
113 }
drh0a8a4062012-12-07 18:38:16 +0000114 if( pExpr->op==TK_COLLATE ){
115 pDup = sqlite3ExprAddCollateString(pParse, pDup, pExpr->u.zToken);
116 }
danf6963f92009-11-23 14:39:14 +0000117
118 /* Before calling sqlite3ExprDelete(), set the EP_Static flag. This
119 ** prevents ExprDelete() from deleting the Expr structure itself,
120 ** allowing it to be repopulated by the memcpy() on the following line.
drhbd13d342012-12-07 21:02:47 +0000121 ** The pExpr->u.zToken might point into memory that will be freed by the
122 ** sqlite3DbFree(db, pDup) on the last line of this block, so be sure to
123 ** make a copy of the token before doing the sqlite3DbFree().
danf6963f92009-11-23 14:39:14 +0000124 */
125 ExprSetProperty(pExpr, EP_Static);
126 sqlite3ExprDelete(db, pExpr);
drh8b213892008-08-29 02:14:02 +0000127 memcpy(pExpr, pDup, sizeof(*pExpr));
drh0a8a4062012-12-07 18:38:16 +0000128 if( !ExprHasProperty(pExpr, EP_IntValue) && pExpr->u.zToken!=0 ){
129 assert( (pExpr->flags & (EP_Reduced|EP_TokenOnly))==0 );
130 pExpr->u.zToken = sqlite3DbStrDup(db, pExpr->u.zToken);
131 pExpr->flags2 |= EP2_MallocedToken;
132 }
drh8b213892008-08-29 02:14:02 +0000133 sqlite3DbFree(db, pDup);
134}
135
drhe802c5d2011-10-18 18:10:40 +0000136
137/*
138** Return TRUE if the name zCol occurs anywhere in the USING clause.
139**
140** Return FALSE if the USING clause is NULL or if it does not contain
141** zCol.
142*/
143static int nameInUsingClause(IdList *pUsing, const char *zCol){
144 if( pUsing ){
145 int k;
146 for(k=0; k<pUsing->nId; k++){
147 if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ) return 1;
148 }
149 }
150 return 0;
151}
152
drh3e3f1a52013-01-03 00:45:56 +0000153/*
154** Subqueries stores the original database, table and column names for their
155** result sets in ExprList.a[].zSpan, in the form "DATABASE.TABLE.COLUMN".
156** Check to see if the zSpan given to this routine matches the zDb, zTab,
157** and zCol. If any of zDb, zTab, and zCol are NULL then those fields will
158** match anything.
159*/
160int sqlite3MatchSpanName(
161 const char *zSpan,
162 const char *zCol,
163 const char *zTab,
164 const char *zDb
165){
166 int n;
167 for(n=0; ALWAYS(zSpan[n]) && zSpan[n]!='.'; n++){}
drhdd1dd482013-02-26 12:57:42 +0000168 if( zDb && (sqlite3StrNICmp(zSpan, zDb, n)!=0 || zDb[n]!=0) ){
drh3e3f1a52013-01-03 00:45:56 +0000169 return 0;
170 }
171 zSpan += n+1;
172 for(n=0; ALWAYS(zSpan[n]) && zSpan[n]!='.'; n++){}
drhdd1dd482013-02-26 12:57:42 +0000173 if( zTab && (sqlite3StrNICmp(zSpan, zTab, n)!=0 || zTab[n]!=0) ){
drh3e3f1a52013-01-03 00:45:56 +0000174 return 0;
175 }
176 zSpan += n+1;
177 if( zCol && sqlite3StrICmp(zSpan, zCol)!=0 ){
178 return 0;
179 }
180 return 1;
181}
drhe802c5d2011-10-18 18:10:40 +0000182
drh8b213892008-08-29 02:14:02 +0000183/*
drh7d10d5a2008-08-20 16:35:10 +0000184** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
185** that name in the set of source tables in pSrcList and make the pExpr
186** expression node refer back to that source column. The following changes
187** are made to pExpr:
188**
189** pExpr->iDb Set the index in db->aDb[] of the database X
190** (even if X is implied).
191** pExpr->iTable Set to the cursor number for the table obtained
192** from pSrcList.
193** pExpr->pTab Points to the Table structure of X.Y (even if
194** X and/or Y are implied.)
195** pExpr->iColumn Set to the column number within the table.
196** pExpr->op Set to TK_COLUMN.
197** pExpr->pLeft Any expression this points to is deleted
198** pExpr->pRight Any expression this points to is deleted.
199**
drhb7916a72009-05-27 10:31:29 +0000200** The zDb variable is the name of the database (the "X"). This value may be
drh7d10d5a2008-08-20 16:35:10 +0000201** NULL meaning that name is of the form Y.Z or Z. Any available database
drhb7916a72009-05-27 10:31:29 +0000202** can be used. The zTable variable is the name of the table (the "Y"). This
203** value can be NULL if zDb is also NULL. If zTable is NULL it
drh7d10d5a2008-08-20 16:35:10 +0000204** means that the form of the name is Z and that columns from any table
205** can be used.
206**
207** If the name cannot be resolved unambiguously, leave an error message
drhf7828b52009-06-15 23:15:59 +0000208** in pParse and return WRC_Abort. Return WRC_Prune on success.
drh7d10d5a2008-08-20 16:35:10 +0000209*/
210static int lookupName(
211 Parse *pParse, /* The parsing context */
drhb7916a72009-05-27 10:31:29 +0000212 const char *zDb, /* Name of the database containing table, or NULL */
213 const char *zTab, /* Name of table containing column, or NULL */
214 const char *zCol, /* Name of the column. */
drh7d10d5a2008-08-20 16:35:10 +0000215 NameContext *pNC, /* The name context used to resolve the name */
216 Expr *pExpr /* Make this EXPR node point to the selected column */
217){
drhed551b92012-08-23 19:46:11 +0000218 int i, j; /* Loop counters */
drh7d10d5a2008-08-20 16:35:10 +0000219 int cnt = 0; /* Number of matching column names */
220 int cntTab = 0; /* Number of matching table names */
drhed551b92012-08-23 19:46:11 +0000221 int nSubquery = 0; /* How many levels of subquery */
drh7d10d5a2008-08-20 16:35:10 +0000222 sqlite3 *db = pParse->db; /* The database connection */
223 struct SrcList_item *pItem; /* Use for looping over pSrcList items */
224 struct SrcList_item *pMatch = 0; /* The matching pSrcList item */
225 NameContext *pTopNC = pNC; /* First namecontext in the list */
226 Schema *pSchema = 0; /* Schema of the expression */
dan2bd93512009-08-31 08:22:46 +0000227 int isTrigger = 0;
drh7d10d5a2008-08-20 16:35:10 +0000228
drhb7916a72009-05-27 10:31:29 +0000229 assert( pNC ); /* the name context cannot be NULL. */
230 assert( zCol ); /* The Z in X.Y.Z cannot be NULL */
drh5a05be12012-10-09 18:51:44 +0000231 assert( !ExprHasAnyProperty(pExpr, EP_TokenOnly|EP_Reduced) );
drh7d10d5a2008-08-20 16:35:10 +0000232
233 /* Initialize the node to no-match */
234 pExpr->iTable = -1;
235 pExpr->pTab = 0;
drh33e619f2009-05-28 01:00:55 +0000236 ExprSetIrreducible(pExpr);
drh7d10d5a2008-08-20 16:35:10 +0000237
drh8f25d182012-12-19 02:36:45 +0000238 /* Translate the schema name in zDb into a pointer to the corresponding
239 ** schema. If not found, pSchema will remain NULL and nothing will match
240 ** resulting in an appropriate error message toward the end of this routine
241 */
242 if( zDb ){
drh1e7d43c2013-08-02 14:18:18 +0000243 testcase( pNC->ncFlags & NC_PartIdx );
244 testcase( pNC->ncFlags & NC_IsCheck );
245 if( (pNC->ncFlags & (NC_PartIdx|NC_IsCheck))!=0 ){
246 /* Silently ignore database qualifiers inside CHECK constraints and partial
247 ** indices. Do not raise errors because that might break legacy and
248 ** because it does not hurt anything to just ignore the database name. */
249 zDb = 0;
250 }else{
251 for(i=0; i<db->nDb; i++){
252 assert( db->aDb[i].zName );
253 if( sqlite3StrICmp(db->aDb[i].zName,zDb)==0 ){
254 pSchema = db->aDb[i].pSchema;
255 break;
256 }
drh8f25d182012-12-19 02:36:45 +0000257 }
258 }
259 }
260
drh7d10d5a2008-08-20 16:35:10 +0000261 /* Start at the inner-most context and move outward until a match is found */
262 while( pNC && cnt==0 ){
263 ExprList *pEList;
264 SrcList *pSrcList = pNC->pSrcList;
265
266 if( pSrcList ){
267 for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
268 Table *pTab;
drh7d10d5a2008-08-20 16:35:10 +0000269 Column *pCol;
270
271 pTab = pItem->pTab;
drhf4366202008-08-25 12:14:08 +0000272 assert( pTab!=0 && pTab->zName!=0 );
drh7d10d5a2008-08-20 16:35:10 +0000273 assert( pTab->nCol>0 );
drh8f25d182012-12-19 02:36:45 +0000274 if( pItem->pSelect && (pItem->pSelect->selFlags & SF_NestedFrom)!=0 ){
drh8f25d182012-12-19 02:36:45 +0000275 int hit = 0;
drh928d9c62013-02-07 09:33:56 +0000276 pEList = pItem->pSelect->pEList;
drh8f25d182012-12-19 02:36:45 +0000277 for(j=0; j<pEList->nExpr; j++){
drh3e3f1a52013-01-03 00:45:56 +0000278 if( sqlite3MatchSpanName(pEList->a[j].zSpan, zCol, zTab, zDb) ){
drh8f25d182012-12-19 02:36:45 +0000279 cnt++;
280 cntTab = 2;
281 pMatch = pItem;
282 pExpr->iColumn = j;
drh38b384a2013-01-03 17:34:28 +0000283 hit = 1;
drh8f25d182012-12-19 02:36:45 +0000284 }
285 }
286 if( hit || zTab==0 ) continue;
287 }
drhc75e09c2013-01-03 16:54:20 +0000288 if( zDb && pTab->pSchema!=pSchema ){
289 continue;
290 }
drh7d10d5a2008-08-20 16:35:10 +0000291 if( zTab ){
drh8f25d182012-12-19 02:36:45 +0000292 const char *zTabName = pItem->zAlias ? pItem->zAlias : pTab->zName;
293 assert( zTabName!=0 );
294 if( sqlite3StrICmp(zTabName, zTab)!=0 ){
295 continue;
drh7d10d5a2008-08-20 16:35:10 +0000296 }
297 }
298 if( 0==(cntTab++) ){
drh7d10d5a2008-08-20 16:35:10 +0000299 pMatch = pItem;
300 }
301 for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
302 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
drhe802c5d2011-10-18 18:10:40 +0000303 /* If there has been exactly one prior match and this match
304 ** is for the right-hand table of a NATURAL JOIN or is in a
305 ** USING clause, then skip this match.
306 */
307 if( cnt==1 ){
308 if( pItem->jointype & JT_NATURAL ) continue;
309 if( nameInUsingClause(pItem->pUsing, zCol) ) continue;
310 }
drh7d10d5a2008-08-20 16:35:10 +0000311 cnt++;
drh7d10d5a2008-08-20 16:35:10 +0000312 pMatch = pItem;
drh7d10d5a2008-08-20 16:35:10 +0000313 /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
shanecf697392009-06-01 16:53:09 +0000314 pExpr->iColumn = j==pTab->iPKey ? -1 : (i16)j;
drh7d10d5a2008-08-20 16:35:10 +0000315 break;
316 }
317 }
318 }
drh8f25d182012-12-19 02:36:45 +0000319 if( pMatch ){
320 pExpr->iTable = pMatch->iCursor;
321 pExpr->pTab = pMatch->pTab;
322 pSchema = pExpr->pTab->pSchema;
323 }
324 } /* if( pSrcList ) */
drh7d10d5a2008-08-20 16:35:10 +0000325
326#ifndef SQLITE_OMIT_TRIGGER
327 /* If we have not already resolved the name, then maybe
328 ** it is a new.* or old.* trigger argument reference
329 */
dan165921a2009-08-28 18:53:45 +0000330 if( zDb==0 && zTab!=0 && cnt==0 && pParse->pTriggerTab!=0 ){
dan65a7cd12009-09-01 12:16:01 +0000331 int op = pParse->eTriggerOp;
drh7d10d5a2008-08-20 16:35:10 +0000332 Table *pTab = 0;
dan65a7cd12009-09-01 12:16:01 +0000333 assert( op==TK_DELETE || op==TK_UPDATE || op==TK_INSERT );
334 if( op!=TK_DELETE && sqlite3StrICmp("new",zTab) == 0 ){
dan165921a2009-08-28 18:53:45 +0000335 pExpr->iTable = 1;
336 pTab = pParse->pTriggerTab;
dan65a7cd12009-09-01 12:16:01 +0000337 }else if( op!=TK_INSERT && sqlite3StrICmp("old",zTab)==0 ){
dan165921a2009-08-28 18:53:45 +0000338 pExpr->iTable = 0;
339 pTab = pParse->pTriggerTab;
drh7d10d5a2008-08-20 16:35:10 +0000340 }
341
342 if( pTab ){
343 int iCol;
drh7d10d5a2008-08-20 16:35:10 +0000344 pSchema = pTab->pSchema;
345 cntTab++;
drh25e978d2009-12-29 23:39:04 +0000346 for(iCol=0; iCol<pTab->nCol; iCol++){
347 Column *pCol = &pTab->aCol[iCol];
348 if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
349 if( iCol==pTab->iPKey ){
350 iCol = -1;
drh7d10d5a2008-08-20 16:35:10 +0000351 }
drh25e978d2009-12-29 23:39:04 +0000352 break;
drh7d10d5a2008-08-20 16:35:10 +0000353 }
354 }
drh25e978d2009-12-29 23:39:04 +0000355 if( iCol>=pTab->nCol && sqlite3IsRowid(zCol) ){
drhc79c7612010-01-01 18:57:48 +0000356 iCol = -1; /* IMP: R-44911-55124 */
drh25e978d2009-12-29 23:39:04 +0000357 }
dan2bd93512009-08-31 08:22:46 +0000358 if( iCol<pTab->nCol ){
359 cnt++;
360 if( iCol<0 ){
361 pExpr->affinity = SQLITE_AFF_INTEGER;
dan2832ad42009-08-31 15:27:27 +0000362 }else if( pExpr->iTable==0 ){
363 testcase( iCol==31 );
364 testcase( iCol==32 );
365 pParse->oldmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
danbb5f1682009-11-27 12:12:34 +0000366 }else{
367 testcase( iCol==31 );
368 testcase( iCol==32 );
369 pParse->newmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
dan2bd93512009-08-31 08:22:46 +0000370 }
shanecea72b22009-09-07 04:38:36 +0000371 pExpr->iColumn = (i16)iCol;
dan2bd93512009-08-31 08:22:46 +0000372 pExpr->pTab = pTab;
373 isTrigger = 1;
374 }
drh7d10d5a2008-08-20 16:35:10 +0000375 }
376 }
377#endif /* !defined(SQLITE_OMIT_TRIGGER) */
378
379 /*
380 ** Perhaps the name is a reference to the ROWID
381 */
382 if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
383 cnt = 1;
drhc79c7612010-01-01 18:57:48 +0000384 pExpr->iColumn = -1; /* IMP: R-44911-55124 */
drh7d10d5a2008-08-20 16:35:10 +0000385 pExpr->affinity = SQLITE_AFF_INTEGER;
386 }
387
388 /*
389 ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
390 ** might refer to an result-set alias. This happens, for example, when
391 ** we are resolving names in the WHERE clause of the following command:
392 **
393 ** SELECT a+b AS x FROM table WHERE x<10;
394 **
395 ** In cases like this, replace pExpr with a copy of the expression that
396 ** forms the result set entry ("a+b" in the example) and return immediately.
397 ** Note that the expression in the result set should have already been
398 ** resolved by the time the WHERE clause is resolved.
399 */
drha3a5bd92013-04-13 19:59:58 +0000400 if( (pEList = pNC->pEList)!=0
401 && zTab==0
402 && ((pNC->ncFlags & NC_AsMaybe)==0 || cnt==0)
403 ){
drh7d10d5a2008-08-20 16:35:10 +0000404 for(j=0; j<pEList->nExpr; j++){
405 char *zAs = pEList->a[j].zName;
406 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
drh8b213892008-08-29 02:14:02 +0000407 Expr *pOrig;
drh7d10d5a2008-08-20 16:35:10 +0000408 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
danielk19776ab3a2e2009-02-19 14:39:25 +0000409 assert( pExpr->x.pList==0 );
410 assert( pExpr->x.pSelect==0 );
drh7d10d5a2008-08-20 16:35:10 +0000411 pOrig = pEList->a[j].pExpr;
drha51009b2012-05-21 19:11:25 +0000412 if( (pNC->ncFlags&NC_AllowAgg)==0 && ExprHasProperty(pOrig, EP_Agg) ){
drh7d10d5a2008-08-20 16:35:10 +0000413 sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs);
drhf7828b52009-06-15 23:15:59 +0000414 return WRC_Abort;
drh7d10d5a2008-08-20 16:35:10 +0000415 }
drhed551b92012-08-23 19:46:11 +0000416 resolveAlias(pParse, pEList, j, pExpr, "", nSubquery);
drh7d10d5a2008-08-20 16:35:10 +0000417 cnt = 1;
418 pMatch = 0;
419 assert( zTab==0 && zDb==0 );
drhb7916a72009-05-27 10:31:29 +0000420 goto lookupname_end;
drh7d10d5a2008-08-20 16:35:10 +0000421 }
422 }
423 }
424
425 /* Advance to the next name context. The loop will exit when either
426 ** we have a match (cnt>0) or when we run out of name contexts.
427 */
428 if( cnt==0 ){
429 pNC = pNC->pNext;
drhed551b92012-08-23 19:46:11 +0000430 nSubquery++;
drh7d10d5a2008-08-20 16:35:10 +0000431 }
432 }
433
434 /*
435 ** If X and Y are NULL (in other words if only the column name Z is
436 ** supplied) and the value of Z is enclosed in double-quotes, then
437 ** Z is a string literal if it doesn't match any column names. In that
438 ** case, we need to return right away and not make any changes to
439 ** pExpr.
440 **
441 ** Because no reference was made to outer contexts, the pNC->nRef
442 ** fields are not changed in any context.
443 */
drh24fb6272009-05-01 21:13:36 +0000444 if( cnt==0 && zTab==0 && ExprHasProperty(pExpr,EP_DblQuoted) ){
drh7d10d5a2008-08-20 16:35:10 +0000445 pExpr->op = TK_STRING;
drh1885d1c2008-10-19 21:03:27 +0000446 pExpr->pTab = 0;
drhf7828b52009-06-15 23:15:59 +0000447 return WRC_Prune;
drh7d10d5a2008-08-20 16:35:10 +0000448 }
449
450 /*
451 ** cnt==0 means there was not match. cnt>1 means there were two or
452 ** more matches. Either way, we have an error.
453 */
454 if( cnt!=1 ){
455 const char *zErr;
456 zErr = cnt==0 ? "no such column" : "ambiguous column name";
457 if( zDb ){
458 sqlite3ErrorMsg(pParse, "%s: %s.%s.%s", zErr, zDb, zTab, zCol);
459 }else if( zTab ){
460 sqlite3ErrorMsg(pParse, "%s: %s.%s", zErr, zTab, zCol);
461 }else{
462 sqlite3ErrorMsg(pParse, "%s: %s", zErr, zCol);
463 }
dan1db95102010-06-28 10:15:19 +0000464 pParse->checkSchema = 1;
drh7d10d5a2008-08-20 16:35:10 +0000465 pTopNC->nErr++;
466 }
467
468 /* If a column from a table in pSrcList is referenced, then record
469 ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes
470 ** bit 0 to be set. Column 1 sets bit 1. And so forth. If the
471 ** column number is greater than the number of bits in the bitmask
472 ** then set the high-order bit of the bitmask.
473 */
danielk19772d2e7bd2009-02-24 10:14:40 +0000474 if( pExpr->iColumn>=0 && pMatch!=0 ){
475 int n = pExpr->iColumn;
476 testcase( n==BMS-1 );
477 if( n>=BMS ){
478 n = BMS-1;
drh7d10d5a2008-08-20 16:35:10 +0000479 }
danielk19772d2e7bd2009-02-24 10:14:40 +0000480 assert( pMatch->iCursor==pExpr->iTable );
481 pMatch->colUsed |= ((Bitmask)1)<<n;
drh7d10d5a2008-08-20 16:35:10 +0000482 }
483
drh7d10d5a2008-08-20 16:35:10 +0000484 /* Clean up and return
485 */
drh7d10d5a2008-08-20 16:35:10 +0000486 sqlite3ExprDelete(db, pExpr->pLeft);
487 pExpr->pLeft = 0;
488 sqlite3ExprDelete(db, pExpr->pRight);
489 pExpr->pRight = 0;
dan2bd93512009-08-31 08:22:46 +0000490 pExpr->op = (isTrigger ? TK_TRIGGER : TK_COLUMN);
drhb7916a72009-05-27 10:31:29 +0000491lookupname_end:
drh7d10d5a2008-08-20 16:35:10 +0000492 if( cnt==1 ){
493 assert( pNC!=0 );
drha3a5bd92013-04-13 19:59:58 +0000494 if( pExpr->op!=TK_AS ){
495 sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList);
496 }
drh7d10d5a2008-08-20 16:35:10 +0000497 /* Increment the nRef value on all name contexts from TopNC up to
498 ** the point where the name matched. */
499 for(;;){
500 assert( pTopNC!=0 );
501 pTopNC->nRef++;
502 if( pTopNC==pNC ) break;
503 pTopNC = pTopNC->pNext;
504 }
drhf7828b52009-06-15 23:15:59 +0000505 return WRC_Prune;
drh7d10d5a2008-08-20 16:35:10 +0000506 } else {
drhf7828b52009-06-15 23:15:59 +0000507 return WRC_Abort;
drh7d10d5a2008-08-20 16:35:10 +0000508 }
509}
510
511/*
danf7b0b0a2009-10-19 15:52:32 +0000512** Allocate and return a pointer to an expression to load the column iCol
drh9e481652010-04-08 17:35:34 +0000513** from datasource iSrc in SrcList pSrc.
danf7b0b0a2009-10-19 15:52:32 +0000514*/
515Expr *sqlite3CreateColumnExpr(sqlite3 *db, SrcList *pSrc, int iSrc, int iCol){
516 Expr *p = sqlite3ExprAlloc(db, TK_COLUMN, 0, 0);
517 if( p ){
518 struct SrcList_item *pItem = &pSrc->a[iSrc];
519 p->pTab = pItem->pTab;
520 p->iTable = pItem->iCursor;
521 if( p->pTab->iPKey==iCol ){
522 p->iColumn = -1;
523 }else{
drh8677d302009-11-04 13:17:14 +0000524 p->iColumn = (ynVar)iCol;
drh7caba662010-04-08 15:01:44 +0000525 testcase( iCol==BMS );
526 testcase( iCol==BMS-1 );
danf7b0b0a2009-10-19 15:52:32 +0000527 pItem->colUsed |= ((Bitmask)1)<<(iCol>=BMS ? BMS-1 : iCol);
528 }
529 ExprSetProperty(p, EP_Resolved);
530 }
531 return p;
532}
533
534/*
drh3780be12013-07-31 19:05:22 +0000535** Report an error that an expression is not valid for a partial index WHERE
536** clause.
537*/
538static void notValidPartIdxWhere(
539 Parse *pParse, /* Leave error message here */
540 NameContext *pNC, /* The name context */
541 const char *zMsg /* Type of error */
542){
543 if( (pNC->ncFlags & NC_PartIdx)!=0 ){
544 sqlite3ErrorMsg(pParse, "%s prohibited in partial index WHERE clauses",
545 zMsg);
546 }
547}
548
549#ifndef SQLITE_OMIT_CHECK
550/*
551** Report an error that an expression is not valid for a CHECK constraint.
552*/
553static void notValidCheckConstraint(
554 Parse *pParse, /* Leave error message here */
555 NameContext *pNC, /* The name context */
556 const char *zMsg /* Type of error */
557){
558 if( (pNC->ncFlags & NC_IsCheck)!=0 ){
559 sqlite3ErrorMsg(pParse,"%s prohibited in CHECK constraints", zMsg);
560 }
561}
562#else
563# define notValidCheckConstraint(P,N,M)
564#endif
565
566
567/*
drh7d10d5a2008-08-20 16:35:10 +0000568** This routine is callback for sqlite3WalkExpr().
569**
570** Resolve symbolic names into TK_COLUMN operators for the current
571** node in the expression tree. Return 0 to continue the search down
572** the tree or 2 to abort the tree walk.
573**
574** This routine also does error checking and name resolution for
575** function names. The operator for aggregate functions is changed
576** to TK_AGG_FUNCTION.
577*/
578static int resolveExprStep(Walker *pWalker, Expr *pExpr){
579 NameContext *pNC;
580 Parse *pParse;
581
drh7d10d5a2008-08-20 16:35:10 +0000582 pNC = pWalker->u.pNC;
583 assert( pNC!=0 );
584 pParse = pNC->pParse;
585 assert( pParse==pWalker->pParse );
586
587 if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return WRC_Prune;
588 ExprSetProperty(pExpr, EP_Resolved);
589#ifndef NDEBUG
590 if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
591 SrcList *pSrcList = pNC->pSrcList;
592 int i;
593 for(i=0; i<pNC->pSrcList->nSrc; i++){
594 assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
595 }
596 }
597#endif
598 switch( pExpr->op ){
drh41204f12008-10-06 13:54:35 +0000599
shane273f6192008-10-10 04:34:16 +0000600#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
drh41204f12008-10-06 13:54:35 +0000601 /* The special operator TK_ROW means use the rowid for the first
602 ** column in the FROM clause. This is used by the LIMIT and ORDER BY
603 ** clause processing on UPDATE and DELETE statements.
604 */
605 case TK_ROW: {
606 SrcList *pSrcList = pNC->pSrcList;
607 struct SrcList_item *pItem;
608 assert( pSrcList && pSrcList->nSrc==1 );
609 pItem = pSrcList->a;
610 pExpr->op = TK_COLUMN;
611 pExpr->pTab = pItem->pTab;
612 pExpr->iTable = pItem->iCursor;
613 pExpr->iColumn = -1;
614 pExpr->affinity = SQLITE_AFF_INTEGER;
615 break;
616 }
shane273f6192008-10-10 04:34:16 +0000617#endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY) */
drh41204f12008-10-06 13:54:35 +0000618
drh7d10d5a2008-08-20 16:35:10 +0000619 /* A lone identifier is the name of a column.
620 */
621 case TK_ID: {
drhf7828b52009-06-15 23:15:59 +0000622 return lookupName(pParse, 0, 0, pExpr->u.zToken, pNC, pExpr);
drh7d10d5a2008-08-20 16:35:10 +0000623 }
624
625 /* A table name and column name: ID.ID
626 ** Or a database, table and column: ID.ID.ID
627 */
628 case TK_DOT: {
drhb7916a72009-05-27 10:31:29 +0000629 const char *zColumn;
630 const char *zTable;
631 const char *zDb;
drh7d10d5a2008-08-20 16:35:10 +0000632 Expr *pRight;
633
634 /* if( pSrcList==0 ) break; */
635 pRight = pExpr->pRight;
636 if( pRight->op==TK_ID ){
drhb7916a72009-05-27 10:31:29 +0000637 zDb = 0;
drh33e619f2009-05-28 01:00:55 +0000638 zTable = pExpr->pLeft->u.zToken;
639 zColumn = pRight->u.zToken;
drh7d10d5a2008-08-20 16:35:10 +0000640 }else{
641 assert( pRight->op==TK_DOT );
drh33e619f2009-05-28 01:00:55 +0000642 zDb = pExpr->pLeft->u.zToken;
643 zTable = pRight->pLeft->u.zToken;
644 zColumn = pRight->pRight->u.zToken;
drh7d10d5a2008-08-20 16:35:10 +0000645 }
drhf7828b52009-06-15 23:15:59 +0000646 return lookupName(pParse, zDb, zTable, zColumn, pNC, pExpr);
drh7d10d5a2008-08-20 16:35:10 +0000647 }
648
649 /* Resolve function names
650 */
651 case TK_CONST_FUNC:
652 case TK_FUNCTION: {
danielk19776ab3a2e2009-02-19 14:39:25 +0000653 ExprList *pList = pExpr->x.pList; /* The argument list */
654 int n = pList ? pList->nExpr : 0; /* Number of arguments */
drh7d10d5a2008-08-20 16:35:10 +0000655 int no_such_func = 0; /* True if no such function exists */
656 int wrong_num_args = 0; /* True if wrong number of arguments */
657 int is_agg = 0; /* True if is an aggregate function */
658 int auth; /* Authorization to use the function */
659 int nId; /* Number of characters in function name */
660 const char *zId; /* The function name. */
661 FuncDef *pDef; /* Information about the function */
drhea678832008-12-10 19:26:22 +0000662 u8 enc = ENC(pParse->db); /* The database encoding */
drh7d10d5a2008-08-20 16:35:10 +0000663
drh73c0fdc2009-06-15 18:32:36 +0000664 testcase( pExpr->op==TK_CONST_FUNC );
danielk19776ab3a2e2009-02-19 14:39:25 +0000665 assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
drh3780be12013-07-31 19:05:22 +0000666 notValidPartIdxWhere(pParse, pNC, "functions");
drh33e619f2009-05-28 01:00:55 +0000667 zId = pExpr->u.zToken;
drhb7916a72009-05-27 10:31:29 +0000668 nId = sqlite3Strlen30(zId);
drh7d10d5a2008-08-20 16:35:10 +0000669 pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
670 if( pDef==0 ){
drh89d5d6a2012-04-07 00:09:21 +0000671 pDef = sqlite3FindFunction(pParse->db, zId, nId, -2, enc, 0);
drh7d10d5a2008-08-20 16:35:10 +0000672 if( pDef==0 ){
673 no_such_func = 1;
674 }else{
675 wrong_num_args = 1;
676 }
677 }else{
678 is_agg = pDef->xFunc==0;
679 }
680#ifndef SQLITE_OMIT_AUTHORIZATION
681 if( pDef ){
682 auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0);
683 if( auth!=SQLITE_OK ){
684 if( auth==SQLITE_DENY ){
685 sqlite3ErrorMsg(pParse, "not authorized to use function: %s",
686 pDef->zName);
687 pNC->nErr++;
688 }
689 pExpr->op = TK_NULL;
690 return WRC_Prune;
691 }
692 }
693#endif
drha51009b2012-05-21 19:11:25 +0000694 if( is_agg && (pNC->ncFlags & NC_AllowAgg)==0 ){
drh7d10d5a2008-08-20 16:35:10 +0000695 sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
696 pNC->nErr++;
697 is_agg = 0;
drhddd1fc72013-01-08 12:48:10 +0000698 }else if( no_such_func && pParse->db->init.busy==0 ){
drh7d10d5a2008-08-20 16:35:10 +0000699 sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
700 pNC->nErr++;
701 }else if( wrong_num_args ){
702 sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
703 nId, zId);
704 pNC->nErr++;
705 }
drha51009b2012-05-21 19:11:25 +0000706 if( is_agg ) pNC->ncFlags &= ~NC_AllowAgg;
drh7d10d5a2008-08-20 16:35:10 +0000707 sqlite3WalkExprList(pWalker, pList);
drh030796d2012-08-23 16:18:10 +0000708 if( is_agg ){
709 NameContext *pNC2 = pNC;
710 pExpr->op = TK_AGG_FUNCTION;
711 pExpr->op2 = 0;
712 while( pNC2 && !sqlite3FunctionUsesThisSrc(pExpr, pNC2->pSrcList) ){
713 pExpr->op2++;
714 pNC2 = pNC2->pNext;
715 }
716 if( pNC2 ) pNC2->ncFlags |= NC_HasAgg;
717 pNC->ncFlags |= NC_AllowAgg;
718 }
drh7d10d5a2008-08-20 16:35:10 +0000719 /* FIX ME: Compute pExpr->affinity based on the expected return
720 ** type of the function
721 */
722 return WRC_Prune;
723 }
724#ifndef SQLITE_OMIT_SUBQUERY
725 case TK_SELECT:
drh73c0fdc2009-06-15 18:32:36 +0000726 case TK_EXISTS: testcase( pExpr->op==TK_EXISTS );
drh7d10d5a2008-08-20 16:35:10 +0000727#endif
728 case TK_IN: {
drh73c0fdc2009-06-15 18:32:36 +0000729 testcase( pExpr->op==TK_IN );
danielk19776ab3a2e2009-02-19 14:39:25 +0000730 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
drh7d10d5a2008-08-20 16:35:10 +0000731 int nRef = pNC->nRef;
drh3780be12013-07-31 19:05:22 +0000732 notValidCheckConstraint(pParse, pNC, "subqueries");
733 notValidPartIdxWhere(pParse, pNC, "subqueries");
danielk19776ab3a2e2009-02-19 14:39:25 +0000734 sqlite3WalkSelect(pWalker, pExpr->x.pSelect);
drh7d10d5a2008-08-20 16:35:10 +0000735 assert( pNC->nRef>=nRef );
736 if( nRef!=pNC->nRef ){
737 ExprSetProperty(pExpr, EP_VarSelect);
738 }
739 }
740 break;
741 }
drh7d10d5a2008-08-20 16:35:10 +0000742 case TK_VARIABLE: {
drh3780be12013-07-31 19:05:22 +0000743 notValidCheckConstraint(pParse, pNC, "parameters");
744 notValidPartIdxWhere(pParse, pNC, "parameters");
drh7d10d5a2008-08-20 16:35:10 +0000745 break;
746 }
drh7d10d5a2008-08-20 16:35:10 +0000747 }
748 return (pParse->nErr || pParse->db->mallocFailed) ? WRC_Abort : WRC_Continue;
749}
750
751/*
752** pEList is a list of expressions which are really the result set of the
753** a SELECT statement. pE is a term in an ORDER BY or GROUP BY clause.
754** This routine checks to see if pE is a simple identifier which corresponds
755** to the AS-name of one of the terms of the expression list. If it is,
756** this routine return an integer between 1 and N where N is the number of
757** elements in pEList, corresponding to the matching entry. If there is
758** no match, or if pE is not a simple identifier, then this routine
759** return 0.
760**
761** pEList has been resolved. pE has not.
762*/
763static int resolveAsName(
764 Parse *pParse, /* Parsing context for error messages */
765 ExprList *pEList, /* List of expressions to scan */
766 Expr *pE /* Expression we are trying to match */
767){
768 int i; /* Loop counter */
769
shanecf697392009-06-01 16:53:09 +0000770 UNUSED_PARAMETER(pParse);
771
drh73c0fdc2009-06-15 18:32:36 +0000772 if( pE->op==TK_ID ){
drh33e619f2009-05-28 01:00:55 +0000773 char *zCol = pE->u.zToken;
drh7d10d5a2008-08-20 16:35:10 +0000774 for(i=0; i<pEList->nExpr; i++){
775 char *zAs = pEList->a[i].zName;
776 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
drh7d10d5a2008-08-20 16:35:10 +0000777 return i+1;
778 }
779 }
drh7d10d5a2008-08-20 16:35:10 +0000780 }
781 return 0;
782}
783
784/*
785** pE is a pointer to an expression which is a single term in the
786** ORDER BY of a compound SELECT. The expression has not been
787** name resolved.
788**
789** At the point this routine is called, we already know that the
790** ORDER BY term is not an integer index into the result set. That
791** case is handled by the calling routine.
792**
793** Attempt to match pE against result set columns in the left-most
794** SELECT statement. Return the index i of the matching column,
795** as an indication to the caller that it should sort by the i-th column.
796** The left-most column is 1. In other words, the value returned is the
797** same integer value that would be used in the SQL statement to indicate
798** the column.
799**
800** If there is no match, return 0. Return -1 if an error occurs.
801*/
802static int resolveOrderByTermToExprList(
803 Parse *pParse, /* Parsing context for error messages */
804 Select *pSelect, /* The SELECT statement with the ORDER BY clause */
805 Expr *pE /* The specific ORDER BY term */
806){
807 int i; /* Loop counter */
808 ExprList *pEList; /* The columns of the result set */
809 NameContext nc; /* Name context for resolving pE */
drha7564662010-02-22 19:32:31 +0000810 sqlite3 *db; /* Database connection */
811 int rc; /* Return code from subprocedures */
812 u8 savedSuppErr; /* Saved value of db->suppressErr */
drh7d10d5a2008-08-20 16:35:10 +0000813
814 assert( sqlite3ExprIsInteger(pE, &i)==0 );
815 pEList = pSelect->pEList;
816
817 /* Resolve all names in the ORDER BY term expression
818 */
819 memset(&nc, 0, sizeof(nc));
820 nc.pParse = pParse;
821 nc.pSrcList = pSelect->pSrc;
822 nc.pEList = pEList;
drha51009b2012-05-21 19:11:25 +0000823 nc.ncFlags = NC_AllowAgg;
drh7d10d5a2008-08-20 16:35:10 +0000824 nc.nErr = 0;
drha7564662010-02-22 19:32:31 +0000825 db = pParse->db;
826 savedSuppErr = db->suppressErr;
827 db->suppressErr = 1;
828 rc = sqlite3ResolveExprNames(&nc, pE);
829 db->suppressErr = savedSuppErr;
830 if( rc ) return 0;
drh7d10d5a2008-08-20 16:35:10 +0000831
832 /* Try to match the ORDER BY expression against an expression
833 ** in the result set. Return an 1-based index of the matching
834 ** result-set entry.
835 */
836 for(i=0; i<pEList->nExpr; i++){
drh619a1302013-08-01 13:04:46 +0000837 if( sqlite3ExprCompare(pEList->a[i].pExpr, pE, -1)<2 ){
drh7d10d5a2008-08-20 16:35:10 +0000838 return i+1;
839 }
840 }
841
842 /* If no match, return 0. */
843 return 0;
844}
845
846/*
847** Generate an ORDER BY or GROUP BY term out-of-range error.
848*/
849static void resolveOutOfRangeError(
850 Parse *pParse, /* The error context into which to write the error */
851 const char *zType, /* "ORDER" or "GROUP" */
852 int i, /* The index (1-based) of the term out of range */
853 int mx /* Largest permissible value of i */
854){
855 sqlite3ErrorMsg(pParse,
856 "%r %s BY term out of range - should be "
857 "between 1 and %d", i, zType, mx);
858}
859
860/*
861** Analyze the ORDER BY clause in a compound SELECT statement. Modify
862** each term of the ORDER BY clause is a constant integer between 1
863** and N where N is the number of columns in the compound SELECT.
864**
865** ORDER BY terms that are already an integer between 1 and N are
866** unmodified. ORDER BY terms that are integers outside the range of
867** 1 through N generate an error. ORDER BY terms that are expressions
868** are matched against result set expressions of compound SELECT
869** beginning with the left-most SELECT and working toward the right.
870** At the first match, the ORDER BY expression is transformed into
871** the integer column number.
872**
873** Return the number of errors seen.
874*/
875static int resolveCompoundOrderBy(
876 Parse *pParse, /* Parsing context. Leave error messages here */
877 Select *pSelect /* The SELECT statement containing the ORDER BY */
878){
879 int i;
880 ExprList *pOrderBy;
881 ExprList *pEList;
882 sqlite3 *db;
883 int moreToDo = 1;
884
885 pOrderBy = pSelect->pOrderBy;
886 if( pOrderBy==0 ) return 0;
887 db = pParse->db;
888#if SQLITE_MAX_COLUMN
889 if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
890 sqlite3ErrorMsg(pParse, "too many terms in ORDER BY clause");
891 return 1;
892 }
893#endif
894 for(i=0; i<pOrderBy->nExpr; i++){
895 pOrderBy->a[i].done = 0;
896 }
897 pSelect->pNext = 0;
898 while( pSelect->pPrior ){
899 pSelect->pPrior->pNext = pSelect;
900 pSelect = pSelect->pPrior;
901 }
902 while( pSelect && moreToDo ){
903 struct ExprList_item *pItem;
904 moreToDo = 0;
905 pEList = pSelect->pEList;
drh0a846f92008-08-25 17:23:29 +0000906 assert( pEList!=0 );
drh7d10d5a2008-08-20 16:35:10 +0000907 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
908 int iCol = -1;
909 Expr *pE, *pDup;
910 if( pItem->done ) continue;
drhbd13d342012-12-07 21:02:47 +0000911 pE = sqlite3ExprSkipCollate(pItem->pExpr);
drh7d10d5a2008-08-20 16:35:10 +0000912 if( sqlite3ExprIsInteger(pE, &iCol) ){
drh73c0fdc2009-06-15 18:32:36 +0000913 if( iCol<=0 || iCol>pEList->nExpr ){
drh7d10d5a2008-08-20 16:35:10 +0000914 resolveOutOfRangeError(pParse, "ORDER", i+1, pEList->nExpr);
915 return 1;
916 }
917 }else{
918 iCol = resolveAsName(pParse, pEList, pE);
919 if( iCol==0 ){
danielk19776ab3a2e2009-02-19 14:39:25 +0000920 pDup = sqlite3ExprDup(db, pE, 0);
drh7d10d5a2008-08-20 16:35:10 +0000921 if( !db->mallocFailed ){
922 assert(pDup);
923 iCol = resolveOrderByTermToExprList(pParse, pSelect, pDup);
924 }
925 sqlite3ExprDelete(db, pDup);
926 }
drh7d10d5a2008-08-20 16:35:10 +0000927 }
928 if( iCol>0 ){
drhbd13d342012-12-07 21:02:47 +0000929 /* Convert the ORDER BY term into an integer column number iCol,
930 ** taking care to preserve the COLLATE clause if it exists */
931 Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0);
932 if( pNew==0 ) return 1;
933 pNew->flags |= EP_IntValue;
934 pNew->u.iValue = iCol;
935 if( pItem->pExpr==pE ){
936 pItem->pExpr = pNew;
937 }else{
938 assert( pItem->pExpr->op==TK_COLLATE );
939 assert( pItem->pExpr->pLeft==pE );
940 pItem->pExpr->pLeft = pNew;
941 }
drh7d10d5a2008-08-20 16:35:10 +0000942 sqlite3ExprDelete(db, pE);
drh4b3ac732011-12-10 23:18:32 +0000943 pItem->iOrderByCol = (u16)iCol;
drh7d10d5a2008-08-20 16:35:10 +0000944 pItem->done = 1;
945 }else{
946 moreToDo = 1;
947 }
948 }
949 pSelect = pSelect->pNext;
950 }
951 for(i=0; i<pOrderBy->nExpr; i++){
952 if( pOrderBy->a[i].done==0 ){
953 sqlite3ErrorMsg(pParse, "%r ORDER BY term does not match any "
954 "column in the result set", i+1);
955 return 1;
956 }
957 }
958 return 0;
959}
960
961/*
962** Check every term in the ORDER BY or GROUP BY clause pOrderBy of
963** the SELECT statement pSelect. If any term is reference to a
964** result set expression (as determined by the ExprList.a.iCol field)
965** then convert that term into a copy of the corresponding result set
966** column.
967**
968** If any errors are detected, add an error message to pParse and
969** return non-zero. Return zero if no errors are seen.
970*/
971int sqlite3ResolveOrderGroupBy(
972 Parse *pParse, /* Parsing context. Leave error messages here */
973 Select *pSelect, /* The SELECT statement containing the clause */
974 ExprList *pOrderBy, /* The ORDER BY or GROUP BY clause to be processed */
975 const char *zType /* "ORDER" or "GROUP" */
976){
977 int i;
978 sqlite3 *db = pParse->db;
979 ExprList *pEList;
980 struct ExprList_item *pItem;
981
982 if( pOrderBy==0 || pParse->db->mallocFailed ) return 0;
983#if SQLITE_MAX_COLUMN
984 if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
985 sqlite3ErrorMsg(pParse, "too many terms in %s BY clause", zType);
986 return 1;
987 }
988#endif
989 pEList = pSelect->pEList;
drh0a846f92008-08-25 17:23:29 +0000990 assert( pEList!=0 ); /* sqlite3SelectNew() guarantees this */
drh7d10d5a2008-08-20 16:35:10 +0000991 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
drh4b3ac732011-12-10 23:18:32 +0000992 if( pItem->iOrderByCol ){
993 if( pItem->iOrderByCol>pEList->nExpr ){
drh7d10d5a2008-08-20 16:35:10 +0000994 resolveOutOfRangeError(pParse, zType, i+1, pEList->nExpr);
995 return 1;
996 }
drhed551b92012-08-23 19:46:11 +0000997 resolveAlias(pParse, pEList, pItem->iOrderByCol-1, pItem->pExpr, zType,0);
drh7d10d5a2008-08-20 16:35:10 +0000998 }
999 }
1000 return 0;
1001}
1002
1003/*
1004** pOrderBy is an ORDER BY or GROUP BY clause in SELECT statement pSelect.
1005** The Name context of the SELECT statement is pNC. zType is either
1006** "ORDER" or "GROUP" depending on which type of clause pOrderBy is.
1007**
1008** This routine resolves each term of the clause into an expression.
1009** If the order-by term is an integer I between 1 and N (where N is the
1010** number of columns in the result set of the SELECT) then the expression
1011** in the resolution is a copy of the I-th result-set expression. If
1012** the order-by term is an identify that corresponds to the AS-name of
1013** a result-set expression, then the term resolves to a copy of the
1014** result-set expression. Otherwise, the expression is resolved in
1015** the usual way - using sqlite3ResolveExprNames().
1016**
1017** This routine returns the number of errors. If errors occur, then
1018** an appropriate error message might be left in pParse. (OOM errors
1019** excepted.)
1020*/
1021static int resolveOrderGroupBy(
1022 NameContext *pNC, /* The name context of the SELECT statement */
1023 Select *pSelect, /* The SELECT statement holding pOrderBy */
1024 ExprList *pOrderBy, /* An ORDER BY or GROUP BY clause to resolve */
1025 const char *zType /* Either "ORDER" or "GROUP", as appropriate */
1026){
drh70331cd2012-04-27 01:09:06 +00001027 int i, j; /* Loop counters */
drh7d10d5a2008-08-20 16:35:10 +00001028 int iCol; /* Column number */
1029 struct ExprList_item *pItem; /* A term of the ORDER BY clause */
1030 Parse *pParse; /* Parsing context */
1031 int nResult; /* Number of terms in the result set */
1032
1033 if( pOrderBy==0 ) return 0;
1034 nResult = pSelect->pEList->nExpr;
1035 pParse = pNC->pParse;
1036 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
1037 Expr *pE = pItem->pExpr;
1038 iCol = resolveAsName(pParse, pSelect->pEList, pE);
drh7d10d5a2008-08-20 16:35:10 +00001039 if( iCol>0 ){
1040 /* If an AS-name match is found, mark this ORDER BY column as being
1041 ** a copy of the iCol-th result-set column. The subsequent call to
1042 ** sqlite3ResolveOrderGroupBy() will convert the expression to a
1043 ** copy of the iCol-th result-set expression. */
drh4b3ac732011-12-10 23:18:32 +00001044 pItem->iOrderByCol = (u16)iCol;
drh7d10d5a2008-08-20 16:35:10 +00001045 continue;
1046 }
drh0a8a4062012-12-07 18:38:16 +00001047 if( sqlite3ExprIsInteger(sqlite3ExprSkipCollate(pE), &iCol) ){
drh7d10d5a2008-08-20 16:35:10 +00001048 /* The ORDER BY term is an integer constant. Again, set the column
1049 ** number so that sqlite3ResolveOrderGroupBy() will convert the
1050 ** order-by term to a copy of the result-set expression */
drh85d641f2012-12-07 23:23:53 +00001051 if( iCol<1 || iCol>0xffff ){
drh7d10d5a2008-08-20 16:35:10 +00001052 resolveOutOfRangeError(pParse, zType, i+1, nResult);
1053 return 1;
1054 }
drh4b3ac732011-12-10 23:18:32 +00001055 pItem->iOrderByCol = (u16)iCol;
drh7d10d5a2008-08-20 16:35:10 +00001056 continue;
1057 }
1058
1059 /* Otherwise, treat the ORDER BY term as an ordinary expression */
drh4b3ac732011-12-10 23:18:32 +00001060 pItem->iOrderByCol = 0;
drh7d10d5a2008-08-20 16:35:10 +00001061 if( sqlite3ResolveExprNames(pNC, pE) ){
1062 return 1;
1063 }
drh70331cd2012-04-27 01:09:06 +00001064 for(j=0; j<pSelect->pEList->nExpr; j++){
drh619a1302013-08-01 13:04:46 +00001065 if( sqlite3ExprCompare(pE, pSelect->pEList->a[j].pExpr, -1)==0 ){
drh70331cd2012-04-27 01:09:06 +00001066 pItem->iOrderByCol = j+1;
1067 }
1068 }
drh7d10d5a2008-08-20 16:35:10 +00001069 }
1070 return sqlite3ResolveOrderGroupBy(pParse, pSelect, pOrderBy, zType);
1071}
1072
1073/*
1074** Resolve names in the SELECT statement p and all of its descendents.
1075*/
1076static int resolveSelectStep(Walker *pWalker, Select *p){
1077 NameContext *pOuterNC; /* Context that contains this SELECT */
1078 NameContext sNC; /* Name context of this SELECT */
1079 int isCompound; /* True if p is a compound select */
1080 int nCompound; /* Number of compound terms processed so far */
1081 Parse *pParse; /* Parsing context */
1082 ExprList *pEList; /* Result set expression list */
1083 int i; /* Loop counter */
1084 ExprList *pGroupBy; /* The GROUP BY clause */
1085 Select *pLeftmost; /* Left-most of SELECT of a compound */
1086 sqlite3 *db; /* Database connection */
1087
1088
drh0a846f92008-08-25 17:23:29 +00001089 assert( p!=0 );
drh7d10d5a2008-08-20 16:35:10 +00001090 if( p->selFlags & SF_Resolved ){
1091 return WRC_Prune;
1092 }
1093 pOuterNC = pWalker->u.pNC;
1094 pParse = pWalker->pParse;
1095 db = pParse->db;
1096
1097 /* Normally sqlite3SelectExpand() will be called first and will have
1098 ** already expanded this SELECT. However, if this is a subquery within
1099 ** an expression, sqlite3ResolveExprNames() will be called without a
1100 ** prior call to sqlite3SelectExpand(). When that happens, let
1101 ** sqlite3SelectPrep() do all of the processing for this SELECT.
1102 ** sqlite3SelectPrep() will invoke both sqlite3SelectExpand() and
1103 ** this routine in the correct order.
1104 */
1105 if( (p->selFlags & SF_Expanded)==0 ){
1106 sqlite3SelectPrep(pParse, p, pOuterNC);
1107 return (pParse->nErr || db->mallocFailed) ? WRC_Abort : WRC_Prune;
1108 }
1109
1110 isCompound = p->pPrior!=0;
1111 nCompound = 0;
1112 pLeftmost = p;
1113 while( p ){
1114 assert( (p->selFlags & SF_Expanded)!=0 );
1115 assert( (p->selFlags & SF_Resolved)==0 );
1116 p->selFlags |= SF_Resolved;
1117
1118 /* Resolve the expressions in the LIMIT and OFFSET clauses. These
1119 ** are not allowed to refer to any names, so pass an empty NameContext.
1120 */
1121 memset(&sNC, 0, sizeof(sNC));
1122 sNC.pParse = pParse;
1123 if( sqlite3ResolveExprNames(&sNC, p->pLimit) ||
1124 sqlite3ResolveExprNames(&sNC, p->pOffset) ){
1125 return WRC_Abort;
1126 }
1127
drh7d10d5a2008-08-20 16:35:10 +00001128 /* Recursively resolve names in all subqueries
1129 */
1130 for(i=0; i<p->pSrc->nSrc; i++){
1131 struct SrcList_item *pItem = &p->pSrc->a[i];
1132 if( pItem->pSelect ){
danda79cf02011-07-08 16:10:54 +00001133 NameContext *pNC; /* Used to iterate name contexts */
1134 int nRef = 0; /* Refcount for pOuterNC and outer contexts */
drh7d10d5a2008-08-20 16:35:10 +00001135 const char *zSavedContext = pParse->zAuthContext;
danda79cf02011-07-08 16:10:54 +00001136
1137 /* Count the total number of references to pOuterNC and all of its
1138 ** parent contexts. After resolving references to expressions in
1139 ** pItem->pSelect, check if this value has changed. If so, then
1140 ** SELECT statement pItem->pSelect must be correlated. Set the
1141 ** pItem->isCorrelated flag if this is the case. */
1142 for(pNC=pOuterNC; pNC; pNC=pNC->pNext) nRef += pNC->nRef;
1143
drh7d10d5a2008-08-20 16:35:10 +00001144 if( pItem->zName ) pParse->zAuthContext = pItem->zName;
drhcd2b5612008-12-09 14:03:22 +00001145 sqlite3ResolveSelectNames(pParse, pItem->pSelect, pOuterNC);
drh7d10d5a2008-08-20 16:35:10 +00001146 pParse->zAuthContext = zSavedContext;
1147 if( pParse->nErr || db->mallocFailed ) return WRC_Abort;
danda79cf02011-07-08 16:10:54 +00001148
1149 for(pNC=pOuterNC; pNC; pNC=pNC->pNext) nRef -= pNC->nRef;
1150 assert( pItem->isCorrelated==0 && nRef<=0 );
1151 pItem->isCorrelated = (nRef!=0);
drh7d10d5a2008-08-20 16:35:10 +00001152 }
1153 }
1154
drh92689d22012-12-18 16:07:08 +00001155 /* Set up the local name-context to pass to sqlite3ResolveExprNames() to
1156 ** resolve the result-set expression list.
1157 */
1158 sNC.ncFlags = NC_AllowAgg;
1159 sNC.pSrcList = p->pSrc;
1160 sNC.pNext = pOuterNC;
1161
1162 /* Resolve names in the result set. */
1163 pEList = p->pEList;
1164 assert( pEList!=0 );
1165 for(i=0; i<pEList->nExpr; i++){
1166 Expr *pX = pEList->a[i].pExpr;
1167 if( sqlite3ResolveExprNames(&sNC, pX) ){
1168 return WRC_Abort;
1169 }
1170 }
1171
drh7d10d5a2008-08-20 16:35:10 +00001172 /* If there are no aggregate functions in the result-set, and no GROUP BY
1173 ** expression, do not allow aggregates in any of the other expressions.
1174 */
1175 assert( (p->selFlags & SF_Aggregate)==0 );
1176 pGroupBy = p->pGroupBy;
drha51009b2012-05-21 19:11:25 +00001177 if( pGroupBy || (sNC.ncFlags & NC_HasAgg)!=0 ){
drh7d10d5a2008-08-20 16:35:10 +00001178 p->selFlags |= SF_Aggregate;
1179 }else{
drha51009b2012-05-21 19:11:25 +00001180 sNC.ncFlags &= ~NC_AllowAgg;
drh7d10d5a2008-08-20 16:35:10 +00001181 }
1182
1183 /* If a HAVING clause is present, then there must be a GROUP BY clause.
1184 */
1185 if( p->pHaving && !pGroupBy ){
1186 sqlite3ErrorMsg(pParse, "a GROUP BY clause is required before HAVING");
1187 return WRC_Abort;
1188 }
1189
1190 /* Add the expression list to the name-context before parsing the
1191 ** other expressions in the SELECT statement. This is so that
1192 ** expressions in the WHERE clause (etc.) can refer to expressions by
1193 ** aliases in the result set.
1194 **
1195 ** Minor point: If this is the case, then the expression will be
1196 ** re-evaluated for each reference to it.
1197 */
1198 sNC.pEList = p->pEList;
drha3a5bd92013-04-13 19:59:58 +00001199 sNC.ncFlags |= NC_AsMaybe;
drh58a450c2013-05-16 01:02:45 +00001200 if( sqlite3ResolveExprNames(&sNC, p->pHaving) ) return WRC_Abort;
drha3a5bd92013-04-13 19:59:58 +00001201 if( sqlite3ResolveExprNames(&sNC, p->pWhere) ) return WRC_Abort;
1202 sNC.ncFlags &= ~NC_AsMaybe;
drh7d10d5a2008-08-20 16:35:10 +00001203
1204 /* The ORDER BY and GROUP BY clauses may not refer to terms in
1205 ** outer queries
1206 */
1207 sNC.pNext = 0;
drha51009b2012-05-21 19:11:25 +00001208 sNC.ncFlags |= NC_AllowAgg;
drh7d10d5a2008-08-20 16:35:10 +00001209
1210 /* Process the ORDER BY clause for singleton SELECT statements.
1211 ** The ORDER BY clause for compounds SELECT statements is handled
1212 ** below, after all of the result-sets for all of the elements of
1213 ** the compound have been resolved.
1214 */
1215 if( !isCompound && resolveOrderGroupBy(&sNC, p, p->pOrderBy, "ORDER") ){
1216 return WRC_Abort;
1217 }
1218 if( db->mallocFailed ){
1219 return WRC_Abort;
1220 }
1221
1222 /* Resolve the GROUP BY clause. At the same time, make sure
1223 ** the GROUP BY clause does not contain aggregate functions.
1224 */
1225 if( pGroupBy ){
1226 struct ExprList_item *pItem;
1227
1228 if( resolveOrderGroupBy(&sNC, p, pGroupBy, "GROUP") || db->mallocFailed ){
1229 return WRC_Abort;
1230 }
1231 for(i=0, pItem=pGroupBy->a; i<pGroupBy->nExpr; i++, pItem++){
1232 if( ExprHasProperty(pItem->pExpr, EP_Agg) ){
1233 sqlite3ErrorMsg(pParse, "aggregate functions are not allowed in "
1234 "the GROUP BY clause");
1235 return WRC_Abort;
1236 }
1237 }
1238 }
1239
1240 /* Advance to the next term of the compound
1241 */
1242 p = p->pPrior;
1243 nCompound++;
1244 }
1245
1246 /* Resolve the ORDER BY on a compound SELECT after all terms of
1247 ** the compound have been resolved.
1248 */
1249 if( isCompound && resolveCompoundOrderBy(pParse, pLeftmost) ){
1250 return WRC_Abort;
1251 }
1252
1253 return WRC_Prune;
1254}
1255
1256/*
1257** This routine walks an expression tree and resolves references to
1258** table columns and result-set columns. At the same time, do error
1259** checking on function usage and set a flag if any aggregate functions
1260** are seen.
1261**
1262** To resolve table columns references we look for nodes (or subtrees) of the
1263** form X.Y.Z or Y.Z or just Z where
1264**
1265** X: The name of a database. Ex: "main" or "temp" or
1266** the symbolic name assigned to an ATTACH-ed database.
1267**
1268** Y: The name of a table in a FROM clause. Or in a trigger
1269** one of the special names "old" or "new".
1270**
1271** Z: The name of a column in table Y.
1272**
1273** The node at the root of the subtree is modified as follows:
1274**
1275** Expr.op Changed to TK_COLUMN
1276** Expr.pTab Points to the Table object for X.Y
1277** Expr.iColumn The column index in X.Y. -1 for the rowid.
1278** Expr.iTable The VDBE cursor number for X.Y
1279**
1280**
1281** To resolve result-set references, look for expression nodes of the
1282** form Z (with no X and Y prefix) where the Z matches the right-hand
1283** size of an AS clause in the result-set of a SELECT. The Z expression
1284** is replaced by a copy of the left-hand side of the result-set expression.
1285** Table-name and function resolution occurs on the substituted expression
1286** tree. For example, in:
1287**
1288** SELECT a+b AS x, c+d AS y FROM t1 ORDER BY x;
1289**
1290** The "x" term of the order by is replaced by "a+b" to render:
1291**
1292** SELECT a+b AS x, c+d AS y FROM t1 ORDER BY a+b;
1293**
1294** Function calls are checked to make sure that the function is
1295** defined and that the correct number of arguments are specified.
drha51009b2012-05-21 19:11:25 +00001296** If the function is an aggregate function, then the NC_HasAgg flag is
drh7d10d5a2008-08-20 16:35:10 +00001297** set and the opcode is changed from TK_FUNCTION to TK_AGG_FUNCTION.
1298** If an expression contains aggregate functions then the EP_Agg
1299** property on the expression is set.
1300**
1301** An error message is left in pParse if anything is amiss. The number
1302** if errors is returned.
1303*/
1304int sqlite3ResolveExprNames(
1305 NameContext *pNC, /* Namespace to resolve expressions in. */
1306 Expr *pExpr /* The expression to be analyzed. */
1307){
drha51009b2012-05-21 19:11:25 +00001308 u8 savedHasAgg;
drh7d10d5a2008-08-20 16:35:10 +00001309 Walker w;
1310
1311 if( pExpr==0 ) return 0;
1312#if SQLITE_MAX_EXPR_DEPTH>0
1313 {
1314 Parse *pParse = pNC->pParse;
1315 if( sqlite3ExprCheckHeight(pParse, pExpr->nHeight+pNC->pParse->nHeight) ){
1316 return 1;
1317 }
1318 pParse->nHeight += pExpr->nHeight;
1319 }
1320#endif
drha51009b2012-05-21 19:11:25 +00001321 savedHasAgg = pNC->ncFlags & NC_HasAgg;
1322 pNC->ncFlags &= ~NC_HasAgg;
drhaa87f9a2013-04-25 00:57:10 +00001323 memset(&w, 0, sizeof(w));
drh7d10d5a2008-08-20 16:35:10 +00001324 w.xExprCallback = resolveExprStep;
1325 w.xSelectCallback = resolveSelectStep;
1326 w.pParse = pNC->pParse;
1327 w.u.pNC = pNC;
1328 sqlite3WalkExpr(&w, pExpr);
1329#if SQLITE_MAX_EXPR_DEPTH>0
1330 pNC->pParse->nHeight -= pExpr->nHeight;
1331#endif
drhfd773cf2009-05-29 14:39:07 +00001332 if( pNC->nErr>0 || w.pParse->nErr>0 ){
drh7d10d5a2008-08-20 16:35:10 +00001333 ExprSetProperty(pExpr, EP_Error);
1334 }
drha51009b2012-05-21 19:11:25 +00001335 if( pNC->ncFlags & NC_HasAgg ){
drh7d10d5a2008-08-20 16:35:10 +00001336 ExprSetProperty(pExpr, EP_Agg);
1337 }else if( savedHasAgg ){
drha51009b2012-05-21 19:11:25 +00001338 pNC->ncFlags |= NC_HasAgg;
drh7d10d5a2008-08-20 16:35:10 +00001339 }
1340 return ExprHasProperty(pExpr, EP_Error);
1341}
drh7d10d5a2008-08-20 16:35:10 +00001342
1343
1344/*
1345** Resolve all names in all expressions of a SELECT and in all
1346** decendents of the SELECT, including compounds off of p->pPrior,
1347** subqueries in expressions, and subqueries used as FROM clause
1348** terms.
1349**
1350** See sqlite3ResolveExprNames() for a description of the kinds of
1351** transformations that occur.
1352**
1353** All SELECT statements should have been expanded using
1354** sqlite3SelectExpand() prior to invoking this routine.
1355*/
1356void sqlite3ResolveSelectNames(
1357 Parse *pParse, /* The parser context */
1358 Select *p, /* The SELECT statement being coded. */
1359 NameContext *pOuterNC /* Name context for parent SELECT statement */
1360){
1361 Walker w;
1362
drh0a846f92008-08-25 17:23:29 +00001363 assert( p!=0 );
drhaa87f9a2013-04-25 00:57:10 +00001364 memset(&w, 0, sizeof(w));
drh0a846f92008-08-25 17:23:29 +00001365 w.xExprCallback = resolveExprStep;
1366 w.xSelectCallback = resolveSelectStep;
1367 w.pParse = pParse;
1368 w.u.pNC = pOuterNC;
1369 sqlite3WalkSelect(&w, p);
drh7d10d5a2008-08-20 16:35:10 +00001370}
drh3780be12013-07-31 19:05:22 +00001371
1372/*
1373** Resolve names in expressions that can only reference a single table:
1374**
1375** * CHECK constraints
1376** * WHERE clauses on partial indices
1377**
1378** The Expr.iTable value for Expr.op==TK_COLUMN nodes of the expression
1379** is set to -1 and the Expr.iColumn value is set to the column number.
1380**
1381** Any errors cause an error message to be set in pParse.
1382*/
1383void sqlite3ResolveSelfReference(
1384 Parse *pParse, /* Parsing context */
1385 Table *pTab, /* The table being referenced */
1386 int type, /* NC_IsCheck or NC_PartIdx */
1387 Expr *pExpr, /* Expression to resolve. May be NULL. */
1388 ExprList *pList /* Expression list to resolve. May be NUL. */
1389){
1390 SrcList sSrc; /* Fake SrcList for pParse->pNewTable */
1391 NameContext sNC; /* Name context for pParse->pNewTable */
1392 int i; /* Loop counter */
1393
1394 assert( type==NC_IsCheck || type==NC_PartIdx );
1395 memset(&sNC, 0, sizeof(sNC));
1396 memset(&sSrc, 0, sizeof(sSrc));
1397 sSrc.nSrc = 1;
1398 sSrc.a[0].zName = pTab->zName;
1399 sSrc.a[0].pTab = pTab;
1400 sSrc.a[0].iCursor = -1;
1401 sNC.pParse = pParse;
1402 sNC.pSrcList = &sSrc;
1403 sNC.ncFlags = type;
1404 if( sqlite3ResolveExprNames(&sNC, pExpr) ) return;
1405 if( pList ){
1406 for(i=0; i<pList->nExpr; i++){
1407 if( sqlite3ResolveExprNames(&sNC, pList->a[i].pExpr) ){
1408 return;
1409 }
1410 }
1411 }
1412}