resolve.c 49 KB

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