blob: 74c4c04dc86d2f01cf249f2b69619afdf6b47b73 [file] [log] [blame]
drhcce7d172000-05-31 15:34:51 +00001/*
drhb19a2bc2001-09-16 00:13:26 +00002** 2001 September 15
drhcce7d172000-05-31 15:34:51 +00003**
drhb19a2bc2001-09-16 00:13:26 +00004** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
drhcce7d172000-05-31 15:34:51 +00006**
drhb19a2bc2001-09-16 00:13:26 +00007** 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.
drhcce7d172000-05-31 15:34:51 +000010**
11*************************************************************************
12** This file contains C code routines that are called by the parser
drhb19a2bc2001-09-16 00:13:26 +000013** to handle SELECT statements in SQLite.
drhcce7d172000-05-31 15:34:51 +000014**
drhfe2093d2005-01-20 22:48:47 +000015** $Id: select.c,v 1.232 2005/01/20 22:48:48 drh Exp $
drhcce7d172000-05-31 15:34:51 +000016*/
17#include "sqliteInt.h"
18
drh315555c2002-10-20 15:53:03 +000019
drhcce7d172000-05-31 15:34:51 +000020/*
drh9bb61fe2000-06-05 16:01:39 +000021** Allocate a new Select structure and return a pointer to that
22** structure.
drhcce7d172000-05-31 15:34:51 +000023*/
danielk19774adee202004-05-08 08:23:19 +000024Select *sqlite3SelectNew(
drhdaffd0e2001-04-11 14:28:42 +000025 ExprList *pEList, /* which columns to include in the result */
drhad3cab52002-05-24 02:04:32 +000026 SrcList *pSrc, /* the FROM clause -- which tables to scan */
drhdaffd0e2001-04-11 14:28:42 +000027 Expr *pWhere, /* the WHERE clause */
28 ExprList *pGroupBy, /* the GROUP BY clause */
29 Expr *pHaving, /* the HAVING clause */
30 ExprList *pOrderBy, /* the ORDER BY clause */
drh9bbca4c2001-11-06 04:00:18 +000031 int isDistinct, /* true if the DISTINCT keyword is present */
32 int nLimit, /* LIMIT value. -1 means not used */
drhef0cae52003-07-16 02:19:37 +000033 int nOffset /* OFFSET value. 0 means no offset */
drh9bb61fe2000-06-05 16:01:39 +000034){
35 Select *pNew;
36 pNew = sqliteMalloc( sizeof(*pNew) );
drhdaffd0e2001-04-11 14:28:42 +000037 if( pNew==0 ){
danielk19774adee202004-05-08 08:23:19 +000038 sqlite3ExprListDelete(pEList);
39 sqlite3SrcListDelete(pSrc);
40 sqlite3ExprDelete(pWhere);
41 sqlite3ExprListDelete(pGroupBy);
42 sqlite3ExprDelete(pHaving);
43 sqlite3ExprListDelete(pOrderBy);
drhdaffd0e2001-04-11 14:28:42 +000044 }else{
drhb733d032004-01-24 20:18:12 +000045 if( pEList==0 ){
danielk19774adee202004-05-08 08:23:19 +000046 pEList = sqlite3ExprListAppend(0, sqlite3Expr(TK_ALL,0,0,0), 0);
drhb733d032004-01-24 20:18:12 +000047 }
drhdaffd0e2001-04-11 14:28:42 +000048 pNew->pEList = pEList;
49 pNew->pSrc = pSrc;
50 pNew->pWhere = pWhere;
51 pNew->pGroupBy = pGroupBy;
52 pNew->pHaving = pHaving;
53 pNew->pOrderBy = pOrderBy;
54 pNew->isDistinct = isDistinct;
55 pNew->op = TK_SELECT;
drh9bbca4c2001-11-06 04:00:18 +000056 pNew->nLimit = nLimit;
57 pNew->nOffset = nOffset;
drh7b58dae2003-07-20 01:16:46 +000058 pNew->iLimit = -1;
59 pNew->iOffset = -1;
drhdaffd0e2001-04-11 14:28:42 +000060 }
drh9bb61fe2000-06-05 16:01:39 +000061 return pNew;
62}
63
64/*
drh01f3f252002-05-24 16:14:15 +000065** Given 1 to 3 identifiers preceeding the JOIN keyword, determine the
66** type of join. Return an integer constant that expresses that type
67** in terms of the following bit values:
68**
69** JT_INNER
70** JT_OUTER
71** JT_NATURAL
72** JT_LEFT
73** JT_RIGHT
74**
75** A full outer join is the combination of JT_LEFT and JT_RIGHT.
76**
77** If an illegal or unsupported join type is seen, then still return
78** a join type, but put an error in the pParse structure.
79*/
danielk19774adee202004-05-08 08:23:19 +000080int sqlite3JoinType(Parse *pParse, Token *pA, Token *pB, Token *pC){
drh01f3f252002-05-24 16:14:15 +000081 int jointype = 0;
82 Token *apAll[3];
83 Token *p;
drh57196282004-10-06 15:41:16 +000084 static const struct {
drh01f3f252002-05-24 16:14:15 +000085 const char *zKeyword;
drh290c1942004-08-21 17:54:45 +000086 u8 nChar;
87 u8 code;
drh01f3f252002-05-24 16:14:15 +000088 } keywords[] = {
89 { "natural", 7, JT_NATURAL },
drh195e6962002-05-25 00:18:20 +000090 { "left", 4, JT_LEFT|JT_OUTER },
91 { "right", 5, JT_RIGHT|JT_OUTER },
92 { "full", 4, JT_LEFT|JT_RIGHT|JT_OUTER },
drh01f3f252002-05-24 16:14:15 +000093 { "outer", 5, JT_OUTER },
94 { "inner", 5, JT_INNER },
95 { "cross", 5, JT_INNER },
96 };
97 int i, j;
98 apAll[0] = pA;
99 apAll[1] = pB;
100 apAll[2] = pC;
drh195e6962002-05-25 00:18:20 +0000101 for(i=0; i<3 && apAll[i]; i++){
drh01f3f252002-05-24 16:14:15 +0000102 p = apAll[i];
103 for(j=0; j<sizeof(keywords)/sizeof(keywords[0]); j++){
104 if( p->n==keywords[j].nChar
danielk19774adee202004-05-08 08:23:19 +0000105 && sqlite3StrNICmp(p->z, keywords[j].zKeyword, p->n)==0 ){
drh01f3f252002-05-24 16:14:15 +0000106 jointype |= keywords[j].code;
107 break;
108 }
109 }
110 if( j>=sizeof(keywords)/sizeof(keywords[0]) ){
111 jointype |= JT_ERROR;
112 break;
113 }
114 }
drhad2d8302002-05-24 20:31:36 +0000115 if(
116 (jointype & (JT_INNER|JT_OUTER))==(JT_INNER|JT_OUTER) ||
drh195e6962002-05-25 00:18:20 +0000117 (jointype & JT_ERROR)!=0
drhad2d8302002-05-24 20:31:36 +0000118 ){
drhae29ffb2004-09-25 14:39:18 +0000119 const char *zSp1 = " ";
120 const char *zSp2 = " ";
121 if( pB==0 ){ zSp1++; }
122 if( pC==0 ){ zSp2++; }
123 sqlite3ErrorMsg(pParse, "unknown or unsupported join type: "
124 "%T%s%T%s%T", pA, zSp1, pB, zSp2, pC);
drh01f3f252002-05-24 16:14:15 +0000125 jointype = JT_INNER;
drh195e6962002-05-25 00:18:20 +0000126 }else if( jointype & JT_RIGHT ){
danielk19774adee202004-05-08 08:23:19 +0000127 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +0000128 "RIGHT and FULL OUTER JOINs are not currently supported");
drh195e6962002-05-25 00:18:20 +0000129 jointype = JT_INNER;
drh01f3f252002-05-24 16:14:15 +0000130 }
131 return jointype;
132}
133
134/*
drhad2d8302002-05-24 20:31:36 +0000135** Return the index of a column in a table. Return -1 if the column
136** is not contained in the table.
137*/
138static int columnIndex(Table *pTab, const char *zCol){
139 int i;
140 for(i=0; i<pTab->nCol; i++){
danielk19774adee202004-05-08 08:23:19 +0000141 if( sqlite3StrICmp(pTab->aCol[i].zName, zCol)==0 ) return i;
drhad2d8302002-05-24 20:31:36 +0000142 }
143 return -1;
144}
145
146/*
drh91bb0ee2004-09-01 03:06:34 +0000147** Set the value of a token to a '\000'-terminated string.
148*/
149static void setToken(Token *p, const char *z){
150 p->z = z;
151 p->n = strlen(z);
152 p->dyn = 0;
153}
154
155
156/*
drhad2d8302002-05-24 20:31:36 +0000157** Add a term to the WHERE expression in *ppExpr that requires the
158** zCol column to be equal in the two tables pTab1 and pTab2.
159*/
160static void addWhereTerm(
161 const char *zCol, /* Name of the column */
162 const Table *pTab1, /* First table */
drh030530d2005-01-18 17:40:04 +0000163 const char *zAlias1, /* Alias for first table. May be NULL */
drhad2d8302002-05-24 20:31:36 +0000164 const Table *pTab2, /* Second table */
drh030530d2005-01-18 17:40:04 +0000165 const char *zAlias2, /* Alias for second table. May be NULL */
drhad2d8302002-05-24 20:31:36 +0000166 Expr **ppExpr /* Add the equality term to this expression */
167){
168 Token dummy;
169 Expr *pE1a, *pE1b, *pE1c;
170 Expr *pE2a, *pE2b, *pE2c;
171 Expr *pE;
172
drh91bb0ee2004-09-01 03:06:34 +0000173 setToken(&dummy, zCol);
danielk19774adee202004-05-08 08:23:19 +0000174 pE1a = sqlite3Expr(TK_ID, 0, 0, &dummy);
175 pE2a = sqlite3Expr(TK_ID, 0, 0, &dummy);
drh030530d2005-01-18 17:40:04 +0000176 if( zAlias1==0 ){
177 zAlias1 = pTab1->zName;
178 }
179 setToken(&dummy, zAlias1);
danielk19774adee202004-05-08 08:23:19 +0000180 pE1b = sqlite3Expr(TK_ID, 0, 0, &dummy);
drh030530d2005-01-18 17:40:04 +0000181 if( zAlias2==0 ){
182 zAlias2 = pTab2->zName;
183 }
184 setToken(&dummy, zAlias2);
danielk19774adee202004-05-08 08:23:19 +0000185 pE2b = sqlite3Expr(TK_ID, 0, 0, &dummy);
186 pE1c = sqlite3Expr(TK_DOT, pE1b, pE1a, 0);
187 pE2c = sqlite3Expr(TK_DOT, pE2b, pE2a, 0);
188 pE = sqlite3Expr(TK_EQ, pE1c, pE2c, 0);
drh1f162302002-10-27 19:35:33 +0000189 ExprSetProperty(pE, EP_FromJoin);
drh91bb0ee2004-09-01 03:06:34 +0000190 *ppExpr = sqlite3ExprAnd(*ppExpr, pE);
drhad2d8302002-05-24 20:31:36 +0000191}
192
193/*
drh1f162302002-10-27 19:35:33 +0000194** Set the EP_FromJoin property on all terms of the given expression.
drh1cc093c2002-06-24 22:01:57 +0000195**
drhe78e8282003-01-19 03:59:45 +0000196** The EP_FromJoin property is used on terms of an expression to tell
drh1cc093c2002-06-24 22:01:57 +0000197** the LEFT OUTER JOIN processing logic that this term is part of the
drh1f162302002-10-27 19:35:33 +0000198** join restriction specified in the ON or USING clause and not a part
199** of the more general WHERE clause. These terms are moved over to the
200** WHERE clause during join processing but we need to remember that they
201** originated in the ON or USING clause.
drh1cc093c2002-06-24 22:01:57 +0000202*/
203static void setJoinExpr(Expr *p){
204 while( p ){
drh1f162302002-10-27 19:35:33 +0000205 ExprSetProperty(p, EP_FromJoin);
drh1cc093c2002-06-24 22:01:57 +0000206 setJoinExpr(p->pLeft);
207 p = p->pRight;
208 }
209}
210
211/*
drhad2d8302002-05-24 20:31:36 +0000212** This routine processes the join information for a SELECT statement.
213** ON and USING clauses are converted into extra terms of the WHERE clause.
214** NATURAL joins also create extra WHERE clause terms.
215**
drh91bb0ee2004-09-01 03:06:34 +0000216** The terms of a FROM clause are contained in the Select.pSrc structure.
217** The left most table is the first entry in Select.pSrc. The right-most
218** table is the last entry. The join operator is held in the entry to
219** the left. Thus entry 0 contains the join operator for the join between
220** entries 0 and 1. Any ON or USING clauses associated with the join are
221** also attached to the left entry.
222**
drhad2d8302002-05-24 20:31:36 +0000223** This routine returns the number of errors encountered.
224*/
225static int sqliteProcessJoin(Parse *pParse, Select *p){
drh91bb0ee2004-09-01 03:06:34 +0000226 SrcList *pSrc; /* All tables in the FROM clause */
227 int i, j; /* Loop counters */
228 struct SrcList_item *pLeft; /* Left table being joined */
229 struct SrcList_item *pRight; /* Right table being joined */
drhad2d8302002-05-24 20:31:36 +0000230
drh91bb0ee2004-09-01 03:06:34 +0000231 pSrc = p->pSrc;
232 pLeft = &pSrc->a[0];
233 pRight = &pLeft[1];
234 for(i=0; i<pSrc->nSrc-1; i++, pRight++, pLeft++){
235 Table *pLeftTab = pLeft->pTab;
236 Table *pRightTab = pRight->pTab;
237
238 if( pLeftTab==0 || pRightTab==0 ) continue;
drhad2d8302002-05-24 20:31:36 +0000239
240 /* When the NATURAL keyword is present, add WHERE clause terms for
241 ** every column that the two tables have in common.
242 */
drh91bb0ee2004-09-01 03:06:34 +0000243 if( pLeft->jointype & JT_NATURAL ){
244 if( pLeft->pOn || pLeft->pUsing ){
danielk19774adee202004-05-08 08:23:19 +0000245 sqlite3ErrorMsg(pParse, "a NATURAL join may not have "
drhad2d8302002-05-24 20:31:36 +0000246 "an ON or USING clause", 0);
drhad2d8302002-05-24 20:31:36 +0000247 return 1;
248 }
drh91bb0ee2004-09-01 03:06:34 +0000249 for(j=0; j<pLeftTab->nCol; j++){
250 char *zName = pLeftTab->aCol[j].zName;
251 if( columnIndex(pRightTab, zName)>=0 ){
drh030530d2005-01-18 17:40:04 +0000252 addWhereTerm(zName, pLeftTab, pLeft->zAlias,
253 pRightTab, pRight->zAlias, &p->pWhere);
drhad2d8302002-05-24 20:31:36 +0000254 }
255 }
256 }
257
258 /* Disallow both ON and USING clauses in the same join
259 */
drh91bb0ee2004-09-01 03:06:34 +0000260 if( pLeft->pOn && pLeft->pUsing ){
danielk19774adee202004-05-08 08:23:19 +0000261 sqlite3ErrorMsg(pParse, "cannot have both ON and USING "
drhda93d232003-03-31 02:12:46 +0000262 "clauses in the same join");
drhad2d8302002-05-24 20:31:36 +0000263 return 1;
264 }
265
266 /* Add the ON clause to the end of the WHERE clause, connected by
drh91bb0ee2004-09-01 03:06:34 +0000267 ** an AND operator.
drhad2d8302002-05-24 20:31:36 +0000268 */
drh91bb0ee2004-09-01 03:06:34 +0000269 if( pLeft->pOn ){
270 setJoinExpr(pLeft->pOn);
271 p->pWhere = sqlite3ExprAnd(p->pWhere, pLeft->pOn);
272 pLeft->pOn = 0;
drhad2d8302002-05-24 20:31:36 +0000273 }
274
275 /* Create extra terms on the WHERE clause for each column named
276 ** in the USING clause. Example: If the two tables to be joined are
277 ** A and B and the USING clause names X, Y, and Z, then add this
278 ** to the WHERE clause: A.X=B.X AND A.Y=B.Y AND A.Z=B.Z
279 ** Report an error if any column mentioned in the USING clause is
280 ** not contained in both tables to be joined.
281 */
drh91bb0ee2004-09-01 03:06:34 +0000282 if( pLeft->pUsing ){
283 IdList *pList = pLeft->pUsing;
drhad2d8302002-05-24 20:31:36 +0000284 for(j=0; j<pList->nId; j++){
drh91bb0ee2004-09-01 03:06:34 +0000285 char *zName = pList->a[j].zName;
286 if( columnIndex(pLeftTab, zName)<0 || columnIndex(pRightTab, zName)<0 ){
danielk19774adee202004-05-08 08:23:19 +0000287 sqlite3ErrorMsg(pParse, "cannot join using column %s - column "
drh91bb0ee2004-09-01 03:06:34 +0000288 "not present in both tables", zName);
drhad2d8302002-05-24 20:31:36 +0000289 return 1;
290 }
drh030530d2005-01-18 17:40:04 +0000291 addWhereTerm(zName, pLeftTab, pLeft->zAlias,
292 pRightTab, pRight->zAlias, &p->pWhere);
drhad2d8302002-05-24 20:31:36 +0000293 }
294 }
295 }
296 return 0;
297}
298
299/*
drh9bb61fe2000-06-05 16:01:39 +0000300** Delete the given Select structure and all of its substructures.
301*/
danielk19774adee202004-05-08 08:23:19 +0000302void sqlite3SelectDelete(Select *p){
drh82c3d632000-06-06 21:56:07 +0000303 if( p==0 ) return;
danielk19774adee202004-05-08 08:23:19 +0000304 sqlite3ExprListDelete(p->pEList);
305 sqlite3SrcListDelete(p->pSrc);
306 sqlite3ExprDelete(p->pWhere);
307 sqlite3ExprListDelete(p->pGroupBy);
308 sqlite3ExprDelete(p->pHaving);
309 sqlite3ExprListDelete(p->pOrderBy);
310 sqlite3SelectDelete(p->pPrior);
drh9bb61fe2000-06-05 16:01:39 +0000311 sqliteFree(p);
312}
313
314/*
drh22827922000-06-06 17:27:05 +0000315** Delete the aggregate information from the parse structure.
316*/
drh1d83f052002-02-17 00:30:36 +0000317static void sqliteAggregateInfoReset(Parse *pParse){
drh22827922000-06-06 17:27:05 +0000318 sqliteFree(pParse->aAgg);
319 pParse->aAgg = 0;
320 pParse->nAgg = 0;
drh22827922000-06-06 17:27:05 +0000321 pParse->useAgg = 0;
322}
323
324/*
drhc926afb2002-06-20 03:38:26 +0000325** Insert code into "v" that will push the record on the top of the
326** stack into the sorter.
327*/
328static void pushOntoSorter(Parse *pParse, Vdbe *v, ExprList *pOrderBy){
drhc926afb2002-06-20 03:38:26 +0000329 int i;
drhc926afb2002-06-20 03:38:26 +0000330 for(i=0; i<pOrderBy->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +0000331 sqlite3ExprCode(pParse, pOrderBy->a[i].pExpr);
drhc926afb2002-06-20 03:38:26 +0000332 }
danielk1977ededfd52004-06-17 07:53:01 +0000333 sqlite3VdbeAddOp(v, OP_MakeRecord, pOrderBy->nExpr, 0);
danielk19774adee202004-05-08 08:23:19 +0000334 sqlite3VdbeAddOp(v, OP_SortPut, 0, 0);
drhc926afb2002-06-20 03:38:26 +0000335}
336
337/*
drhea48eb22004-07-19 23:16:38 +0000338** Add code to implement the OFFSET and LIMIT
339*/
340static void codeLimiter(
drhbab39e12004-07-19 23:38:11 +0000341 Vdbe *v, /* Generate code into this VM */
drhea48eb22004-07-19 23:16:38 +0000342 Select *p, /* The SELECT statement being coded */
343 int iContinue, /* Jump here to skip the current record */
344 int iBreak, /* Jump here to end the loop */
345 int nPop /* Number of times to pop stack when jumping */
346){
drhea48eb22004-07-19 23:16:38 +0000347 if( p->iOffset>=0 ){
348 int addr = sqlite3VdbeCurrentAddr(v) + 2;
349 if( nPop>0 ) addr++;
350 sqlite3VdbeAddOp(v, OP_MemIncr, p->iOffset, addr);
351 if( nPop>0 ){
352 sqlite3VdbeAddOp(v, OP_Pop, nPop, 0);
353 }
354 sqlite3VdbeAddOp(v, OP_Goto, 0, iContinue);
drhad6d9462004-09-19 02:15:24 +0000355 VdbeComment((v, "# skip OFFSET records"));
drhea48eb22004-07-19 23:16:38 +0000356 }
357 if( p->iLimit>=0 ){
358 sqlite3VdbeAddOp(v, OP_MemIncr, p->iLimit, iBreak);
drhad6d9462004-09-19 02:15:24 +0000359 VdbeComment((v, "# exit when LIMIT reached"));
drhea48eb22004-07-19 23:16:38 +0000360 }
361}
362
363/*
drh22827922000-06-06 17:27:05 +0000364** This routine generates the code for the inside of the inner loop
365** of a SELECT.
drh82c3d632000-06-06 21:56:07 +0000366**
drh38640e12002-07-05 21:42:36 +0000367** If srcTab and nColumn are both zero, then the pEList expressions
368** are evaluated in order to get the data for this row. If nColumn>0
369** then data is pulled from srcTab and pEList is used only to get the
370** datatypes for each column.
drh22827922000-06-06 17:27:05 +0000371*/
372static int selectInnerLoop(
373 Parse *pParse, /* The parser context */
drhdf199a22002-06-14 22:38:41 +0000374 Select *p, /* The complete select statement being coded */
drh22827922000-06-06 17:27:05 +0000375 ExprList *pEList, /* List of values being extracted */
drh82c3d632000-06-06 21:56:07 +0000376 int srcTab, /* Pull data from this table */
drh967e8b72000-06-21 13:59:10 +0000377 int nColumn, /* Number of columns in the source table */
drh22827922000-06-06 17:27:05 +0000378 ExprList *pOrderBy, /* If not NULL, sort results using this key */
379 int distinct, /* If >=0, make sure results are distinct */
380 int eDest, /* How to dispose of the results */
381 int iParm, /* An argument to the disposal method */
382 int iContinue, /* Jump here to continue with next row */
danielk197784ac9d02004-05-18 09:58:06 +0000383 int iBreak, /* Jump here to break out of the inner loop */
384 char *aff /* affinity string if eDest is SRT_Union */
drh22827922000-06-06 17:27:05 +0000385){
386 Vdbe *v = pParse->pVdbe;
387 int i;
drhea48eb22004-07-19 23:16:38 +0000388 int hasDistinct; /* True if the DISTINCT keyword is present */
drh38640e12002-07-05 21:42:36 +0000389
drhdaffd0e2001-04-11 14:28:42 +0000390 if( v==0 ) return 0;
drh38640e12002-07-05 21:42:36 +0000391 assert( pEList!=0 );
drh22827922000-06-06 17:27:05 +0000392
drhdf199a22002-06-14 22:38:41 +0000393 /* If there was a LIMIT clause on the SELECT statement, then do the check
394 ** to see if this row should be output.
395 */
drhea48eb22004-07-19 23:16:38 +0000396 hasDistinct = distinct>=0 && pEList && pEList->nExpr>0;
397 if( pOrderBy==0 && !hasDistinct ){
drhbab39e12004-07-19 23:38:11 +0000398 codeLimiter(v, p, iContinue, iBreak, 0);
drhdf199a22002-06-14 22:38:41 +0000399 }
400
drh967e8b72000-06-21 13:59:10 +0000401 /* Pull the requested columns.
drh22827922000-06-06 17:27:05 +0000402 */
drh38640e12002-07-05 21:42:36 +0000403 if( nColumn>0 ){
drh967e8b72000-06-21 13:59:10 +0000404 for(i=0; i<nColumn; i++){
danielk19774adee202004-05-08 08:23:19 +0000405 sqlite3VdbeAddOp(v, OP_Column, srcTab, i);
drh82c3d632000-06-06 21:56:07 +0000406 }
drh38640e12002-07-05 21:42:36 +0000407 }else{
408 nColumn = pEList->nExpr;
409 for(i=0; i<pEList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +0000410 sqlite3ExprCode(pParse, pEList->a[i].pExpr);
drh38640e12002-07-05 21:42:36 +0000411 }
drh22827922000-06-06 17:27:05 +0000412 }
413
drhdaffd0e2001-04-11 14:28:42 +0000414 /* If the DISTINCT keyword was present on the SELECT statement
415 ** and this row has been seen before, then do not make this row
416 ** part of the result.
drh22827922000-06-06 17:27:05 +0000417 */
drhea48eb22004-07-19 23:16:38 +0000418 if( hasDistinct ){
drh0bd1f4e2002-06-06 18:54:39 +0000419#if NULL_ALWAYS_DISTINCT
danielk19774adee202004-05-08 08:23:19 +0000420 sqlite3VdbeAddOp(v, OP_IsNull, -pEList->nExpr, sqlite3VdbeCurrentAddr(v)+7);
drh0bd1f4e2002-06-06 18:54:39 +0000421#endif
danielk1977ededfd52004-06-17 07:53:01 +0000422 /* Deliberately leave the affinity string off of the following
423 ** OP_MakeRecord */
424 sqlite3VdbeAddOp(v, OP_MakeRecord, pEList->nExpr * -1, 0);
danielk19774adee202004-05-08 08:23:19 +0000425 sqlite3VdbeAddOp(v, OP_Distinct, distinct, sqlite3VdbeCurrentAddr(v)+3);
426 sqlite3VdbeAddOp(v, OP_Pop, pEList->nExpr+1, 0);
427 sqlite3VdbeAddOp(v, OP_Goto, 0, iContinue);
drhad6d9462004-09-19 02:15:24 +0000428 VdbeComment((v, "# skip indistinct records"));
danielk19770f69c1e2004-05-29 11:24:50 +0000429 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk19774adee202004-05-08 08:23:19 +0000430 sqlite3VdbeAddOp(v, OP_PutStrKey, distinct, 0);
drhea48eb22004-07-19 23:16:38 +0000431 if( pOrderBy==0 ){
drhbab39e12004-07-19 23:38:11 +0000432 codeLimiter(v, p, iContinue, iBreak, nColumn);
drhea48eb22004-07-19 23:16:38 +0000433 }
drh22827922000-06-06 17:27:05 +0000434 }
drh82c3d632000-06-06 21:56:07 +0000435
drhc926afb2002-06-20 03:38:26 +0000436 switch( eDest ){
danielk19775338a5f2005-01-20 13:03:10 +0000437#ifndef SQLITE_OMIT_COMPOUND_SELECT
drhc926afb2002-06-20 03:38:26 +0000438 /* In this mode, write each query result to the key of the temporary
439 ** table iParm.
440 */
441 case SRT_Union: {
danielk19774adee202004-05-08 08:23:19 +0000442 sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, NULL_ALWAYS_DISTINCT);
danielk197784ac9d02004-05-18 09:58:06 +0000443 sqlite3VdbeChangeP3(v, -1, aff, P3_STATIC);
danielk19770f69c1e2004-05-29 11:24:50 +0000444 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk19774adee202004-05-08 08:23:19 +0000445 sqlite3VdbeAddOp(v, OP_PutStrKey, iParm, 0);
drhc926afb2002-06-20 03:38:26 +0000446 break;
drh22827922000-06-06 17:27:05 +0000447 }
drh22827922000-06-06 17:27:05 +0000448
drhc926afb2002-06-20 03:38:26 +0000449 /* Construct a record from the query result, but instead of
450 ** saving that record, use it as a key to delete elements from
451 ** the temporary table iParm.
452 */
453 case SRT_Except: {
454 int addr;
danielk19774adee202004-05-08 08:23:19 +0000455 addr = sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, NULL_ALWAYS_DISTINCT);
danielk197784ac9d02004-05-18 09:58:06 +0000456 sqlite3VdbeChangeP3(v, -1, aff, P3_STATIC);
danielk19774adee202004-05-08 08:23:19 +0000457 sqlite3VdbeAddOp(v, OP_NotFound, iParm, addr+3);
458 sqlite3VdbeAddOp(v, OP_Delete, iParm, 0);
drhc926afb2002-06-20 03:38:26 +0000459 break;
460 }
danielk19775338a5f2005-01-20 13:03:10 +0000461#endif
462
463 /* Store the result as data using a unique key.
464 */
465 case SRT_Table:
466 case SRT_TempTable: {
467 sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, 0);
468 if( pOrderBy ){
469 pushOntoSorter(pParse, v, pOrderBy);
470 }else{
471 sqlite3VdbeAddOp(v, OP_NewRecno, iParm, 0);
472 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
473 sqlite3VdbeAddOp(v, OP_PutIntKey, iParm, 0);
474 }
475 break;
476 }
drh5974a302000-06-07 14:42:26 +0000477
drhc926afb2002-06-20 03:38:26 +0000478 /* If we are creating a set for an "expr IN (SELECT ...)" construct,
479 ** then there should be a single item on the stack. Write this
480 ** item into the set table with bogus data.
481 */
482 case SRT_Set: {
danielk19774adee202004-05-08 08:23:19 +0000483 int addr1 = sqlite3VdbeCurrentAddr(v);
drh52b36ca2004-01-14 13:38:54 +0000484 int addr2;
danielk1977e014a832004-05-17 10:48:57 +0000485
drhc926afb2002-06-20 03:38:26 +0000486 assert( nColumn==1 );
danielk19774adee202004-05-08 08:23:19 +0000487 sqlite3VdbeAddOp(v, OP_NotNull, -1, addr1+3);
488 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
489 addr2 = sqlite3VdbeAddOp(v, OP_Goto, 0, 0);
drhc926afb2002-06-20 03:38:26 +0000490 if( pOrderBy ){
491 pushOntoSorter(pParse, v, pOrderBy);
492 }else{
danielk1977e014a832004-05-17 10:48:57 +0000493 char aff = (iParm>>16)&0xFF;
494 aff = sqlite3CompareAffinity(pEList->a[0].pExpr, aff);
drh94a11212004-09-25 13:12:14 +0000495 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &aff, 1);
danielk19770f69c1e2004-05-29 11:24:50 +0000496 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +0000497 sqlite3VdbeAddOp(v, OP_PutStrKey, (iParm&0x0000FFFF), 0);
drhc926afb2002-06-20 03:38:26 +0000498 }
danielk19774adee202004-05-08 08:23:19 +0000499 sqlite3VdbeChangeP2(v, addr2, sqlite3VdbeCurrentAddr(v));
drhc926afb2002-06-20 03:38:26 +0000500 break;
501 }
drh22827922000-06-06 17:27:05 +0000502
drhc926afb2002-06-20 03:38:26 +0000503 /* If this is a scalar select that is part of an expression, then
504 ** store the results in the appropriate memory cell and break out
505 ** of the scan loop.
506 */
drh51522cd2005-01-20 13:36:19 +0000507 case SRT_Exists:
drhc926afb2002-06-20 03:38:26 +0000508 case SRT_Mem: {
509 assert( nColumn==1 );
510 if( pOrderBy ){
511 pushOntoSorter(pParse, v, pOrderBy);
512 }else{
danielk19774adee202004-05-08 08:23:19 +0000513 sqlite3VdbeAddOp(v, OP_MemStore, iParm, 1);
514 sqlite3VdbeAddOp(v, OP_Goto, 0, iBreak);
drhc926afb2002-06-20 03:38:26 +0000515 }
516 break;
517 }
drh22827922000-06-06 17:27:05 +0000518
drhf46f9052002-06-22 02:33:38 +0000519 /* Send the data to the callback function.
520 */
521 case SRT_Callback:
522 case SRT_Sorter: {
523 if( pOrderBy ){
drhce665cf2004-05-21 03:01:58 +0000524 sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, 0);
drhf46f9052002-06-22 02:33:38 +0000525 pushOntoSorter(pParse, v, pOrderBy);
526 }else{
527 assert( eDest==SRT_Callback );
danielk19774adee202004-05-08 08:23:19 +0000528 sqlite3VdbeAddOp(v, OP_Callback, nColumn, 0);
drhf46f9052002-06-22 02:33:38 +0000529 }
530 break;
531 }
532
drh142e30d2002-08-28 03:00:58 +0000533 /* Invoke a subroutine to handle the results. The subroutine itself
534 ** is responsible for popping the results off of the stack.
535 */
536 case SRT_Subroutine: {
drhac82fcf2002-09-08 17:23:41 +0000537 if( pOrderBy ){
danielk19774adee202004-05-08 08:23:19 +0000538 sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, 0);
drhac82fcf2002-09-08 17:23:41 +0000539 pushOntoSorter(pParse, v, pOrderBy);
540 }else{
danielk19774adee202004-05-08 08:23:19 +0000541 sqlite3VdbeAddOp(v, OP_Gosub, 0, iParm);
drhac82fcf2002-09-08 17:23:41 +0000542 }
drh142e30d2002-08-28 03:00:58 +0000543 break;
544 }
545
drhc926afb2002-06-20 03:38:26 +0000546 /* Discard the results. This is used for SELECT statements inside
547 ** the body of a TRIGGER. The purpose of such selects is to call
548 ** user-defined functions that have side effects. We do not care
549 ** about the actual results of the select.
550 */
drhc926afb2002-06-20 03:38:26 +0000551 default: {
drhf46f9052002-06-22 02:33:38 +0000552 assert( eDest==SRT_Discard );
danielk19774adee202004-05-08 08:23:19 +0000553 sqlite3VdbeAddOp(v, OP_Pop, nColumn, 0);
drhc926afb2002-06-20 03:38:26 +0000554 break;
555 }
drh82c3d632000-06-06 21:56:07 +0000556 }
557 return 0;
558}
559
560/*
drhd8bc7082000-06-07 23:51:50 +0000561** If the inner loop was generated using a non-null pOrderBy argument,
562** then the results were placed in a sorter. After the loop is terminated
563** we need to run the sorter and output the results. The following
564** routine generates the code needed to do that.
565*/
drhc926afb2002-06-20 03:38:26 +0000566static void generateSortTail(
drhffbc3082004-05-21 01:29:06 +0000567 Parse *pParse, /* The parsing context */
drhc926afb2002-06-20 03:38:26 +0000568 Select *p, /* The SELECT statement */
569 Vdbe *v, /* Generate code into this VDBE */
570 int nColumn, /* Number of columns of data */
571 int eDest, /* Write the sorted results here */
572 int iParm /* Optional parameter associated with eDest */
573){
danielk19774adee202004-05-08 08:23:19 +0000574 int end1 = sqlite3VdbeMakeLabel(v);
575 int end2 = sqlite3VdbeMakeLabel(v);
drhd8bc7082000-06-07 23:51:50 +0000576 int addr;
drhffbc3082004-05-21 01:29:06 +0000577 KeyInfo *pInfo;
578 ExprList *pOrderBy;
579 int nCol, i;
drh9bb575f2004-09-06 17:24:11 +0000580 sqlite3 *db = pParse->db;
drhffbc3082004-05-21 01:29:06 +0000581
drhf46f9052002-06-22 02:33:38 +0000582 if( eDest==SRT_Sorter ) return;
drhffbc3082004-05-21 01:29:06 +0000583 pOrderBy = p->pOrderBy;
584 nCol = pOrderBy->nExpr;
585 pInfo = sqliteMalloc( sizeof(*pInfo) + nCol*(sizeof(CollSeq*)+1) );
586 if( pInfo==0 ) return;
587 pInfo->aSortOrder = (char*)&pInfo->aColl[nCol];
588 pInfo->nField = nCol;
589 for(i=0; i<nCol; i++){
danielk19770202b292004-06-09 09:55:16 +0000590 /* If a collation sequence was specified explicity, then it
591 ** is stored in pOrderBy->a[i].zName. Otherwise, use the default
592 ** collation type for the expression.
593 */
danielk19777cedc8d2004-06-10 10:50:08 +0000594 pInfo->aColl[i] = sqlite3ExprCollSeq(pParse, pOrderBy->a[i].pExpr);
danielk19770202b292004-06-09 09:55:16 +0000595 if( !pInfo->aColl[i] ){
596 pInfo->aColl[i] = db->pDfltColl;
597 }
drhffbc3082004-05-21 01:29:06 +0000598 pInfo->aSortOrder[i] = pOrderBy->a[i].sortOrder;
599 }
600 sqlite3VdbeOp3(v, OP_Sort, 0, 0, (char*)pInfo, P3_KEYINFO_HANDOFF);
danielk19774adee202004-05-08 08:23:19 +0000601 addr = sqlite3VdbeAddOp(v, OP_SortNext, 0, end1);
drhbab39e12004-07-19 23:38:11 +0000602 codeLimiter(v, p, addr, end2, 1);
drhc926afb2002-06-20 03:38:26 +0000603 switch( eDest ){
drhc926afb2002-06-20 03:38:26 +0000604 case SRT_Table:
605 case SRT_TempTable: {
danielk19774adee202004-05-08 08:23:19 +0000606 sqlite3VdbeAddOp(v, OP_NewRecno, iParm, 0);
607 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
608 sqlite3VdbeAddOp(v, OP_PutIntKey, iParm, 0);
drhc926afb2002-06-20 03:38:26 +0000609 break;
610 }
611 case SRT_Set: {
612 assert( nColumn==1 );
danielk19774adee202004-05-08 08:23:19 +0000613 sqlite3VdbeAddOp(v, OP_NotNull, -1, sqlite3VdbeCurrentAddr(v)+3);
614 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
615 sqlite3VdbeAddOp(v, OP_Goto, 0, sqlite3VdbeCurrentAddr(v)+3);
danielk1977ededfd52004-06-17 07:53:01 +0000616 sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, "n", P3_STATIC);
danielk19770f69c1e2004-05-29 11:24:50 +0000617 sqlite3VdbeAddOp(v, OP_String8, 0, 0);
danielk1977e014a832004-05-17 10:48:57 +0000618 sqlite3VdbeAddOp(v, OP_PutStrKey, (iParm&0x0000FFFF), 0);
drhc926afb2002-06-20 03:38:26 +0000619 break;
620 }
drh51522cd2005-01-20 13:36:19 +0000621 case SRT_Exists:
drhc926afb2002-06-20 03:38:26 +0000622 case SRT_Mem: {
623 assert( nColumn==1 );
danielk19774adee202004-05-08 08:23:19 +0000624 sqlite3VdbeAddOp(v, OP_MemStore, iParm, 1);
625 sqlite3VdbeAddOp(v, OP_Goto, 0, end1);
drhc926afb2002-06-20 03:38:26 +0000626 break;
627 }
drhce665cf2004-05-21 03:01:58 +0000628 case SRT_Callback:
drhac82fcf2002-09-08 17:23:41 +0000629 case SRT_Subroutine: {
630 int i;
danielk197784ac9d02004-05-18 09:58:06 +0000631 sqlite3VdbeAddOp(v, OP_Integer, p->pEList->nExpr, 0);
632 sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
drhac82fcf2002-09-08 17:23:41 +0000633 for(i=0; i<nColumn; i++){
danielk19774adee202004-05-08 08:23:19 +0000634 sqlite3VdbeAddOp(v, OP_Column, -1-i, i);
drhac82fcf2002-09-08 17:23:41 +0000635 }
drhce665cf2004-05-21 03:01:58 +0000636 if( eDest==SRT_Callback ){
637 sqlite3VdbeAddOp(v, OP_Callback, nColumn, 0);
638 }else{
639 sqlite3VdbeAddOp(v, OP_Gosub, 0, iParm);
640 }
danielk197784ac9d02004-05-18 09:58:06 +0000641 sqlite3VdbeAddOp(v, OP_Pop, 2, 0);
drhac82fcf2002-09-08 17:23:41 +0000642 break;
643 }
drhc926afb2002-06-20 03:38:26 +0000644 default: {
drhf46f9052002-06-22 02:33:38 +0000645 /* Do nothing */
drhc926afb2002-06-20 03:38:26 +0000646 break;
647 }
648 }
danielk19774adee202004-05-08 08:23:19 +0000649 sqlite3VdbeAddOp(v, OP_Goto, 0, addr);
650 sqlite3VdbeResolveLabel(v, end2);
651 sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
652 sqlite3VdbeResolveLabel(v, end1);
653 sqlite3VdbeAddOp(v, OP_SortReset, 0, 0);
drhd8bc7082000-06-07 23:51:50 +0000654}
655
656/*
danielk1977517eb642004-06-07 10:00:31 +0000657** Return a pointer to a string containing the 'declaration type' of the
658** expression pExpr. The string may be treated as static by the caller.
drhe78e8282003-01-19 03:59:45 +0000659**
danielk1977517eb642004-06-07 10:00:31 +0000660** If the declaration type is the exact datatype definition extracted from
661** the original CREATE TABLE statement if the expression is a column.
662**
663** The declaration type for an expression is either TEXT, NUMERIC or ANY.
664** The declaration type for a ROWID field is INTEGER.
665*/
666static const char *columnType(Parse *pParse, SrcList *pTabList, Expr *pExpr){
danielk197700e279d2004-06-21 07:36:32 +0000667 char const *zType;
danielk1977517eb642004-06-07 10:00:31 +0000668 int j;
danielk197700e279d2004-06-21 07:36:32 +0000669 if( pExpr==0 || pTabList==0 ) return 0;
670
drh1398ad32005-01-19 23:24:50 +0000671 sqlite3ExprResolveNames(pParse, pTabList, 0, 0, pExpr, 1, 0);
danielk19775338a5f2005-01-20 13:03:10 +0000672
673 /* The TK_AS operator can only occur in ORDER BY, GROUP BY, HAVING,
674 ** and LIMIT clauses. But pExpr originates in the result set of a
675 ** SELECT. So pExpr can never contain an AS operator.
676 */
677 assert( pExpr->op!=TK_AS );
678
danielk197700e279d2004-06-21 07:36:32 +0000679 switch( pExpr->op ){
680 case TK_COLUMN: {
681 Table *pTab;
682 int iCol = pExpr->iColumn;
683 for(j=0; j<pTabList->nSrc && pTabList->a[j].iCursor!=pExpr->iTable; j++){}
684 assert( j<pTabList->nSrc );
685 pTab = pTabList->a[j].pTab;
686 if( iCol<0 ) iCol = pTab->iPKey;
687 assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
688 if( iCol<0 ){
689 zType = "INTEGER";
690 }else{
691 zType = pTab->aCol[iCol].zType;
692 }
693 break;
danielk1977517eb642004-06-07 10:00:31 +0000694 }
danielk197700e279d2004-06-21 07:36:32 +0000695 case TK_SELECT: {
696 Select *pS = pExpr->pSelect;
697 zType = columnType(pParse, pS->pSrc, pS->pEList->a[0].pExpr);
698 break;
danielk1977517eb642004-06-07 10:00:31 +0000699 }
danielk197700e279d2004-06-21 07:36:32 +0000700 default:
701 zType = 0;
danielk1977517eb642004-06-07 10:00:31 +0000702 }
danielk197700e279d2004-06-21 07:36:32 +0000703
danielk1977517eb642004-06-07 10:00:31 +0000704 return zType;
705}
706
707/*
708** Generate code that will tell the VDBE the declaration types of columns
709** in the result set.
drhfcb78a42003-01-18 20:11:05 +0000710*/
711static void generateColumnTypes(
712 Parse *pParse, /* Parser context */
drhfcb78a42003-01-18 20:11:05 +0000713 SrcList *pTabList, /* List of tables */
714 ExprList *pEList /* Expressions defining the result set */
715){
716 Vdbe *v = pParse->pVdbe;
danielk1977517eb642004-06-07 10:00:31 +0000717 int i;
drhfcb78a42003-01-18 20:11:05 +0000718 for(i=0; i<pEList->nExpr; i++){
719 Expr *p = pEList->a[i].pExpr;
danielk1977517eb642004-06-07 10:00:31 +0000720 const char *zType = columnType(pParse, pTabList, p);
danielk197700e279d2004-06-21 07:36:32 +0000721 if( zType==0 ) continue;
danielk1977fbcd5852004-06-15 02:44:18 +0000722 /* The vdbe must make it's own copy of the column-type, in case the
723 ** schema is reset before this virtual machine is deleted.
724 */
725 sqlite3VdbeSetColName(v, i+pEList->nExpr, zType, strlen(zType));
drhfcb78a42003-01-18 20:11:05 +0000726 }
727}
728
729/*
730** Generate code that will tell the VDBE the names of columns
731** in the result set. This information is used to provide the
drhfcabd462004-02-20 14:50:58 +0000732** azCol[] values in the callback.
drh82c3d632000-06-06 21:56:07 +0000733*/
drh832508b2002-03-02 17:04:07 +0000734static void generateColumnNames(
735 Parse *pParse, /* Parser context */
drhad3cab52002-05-24 02:04:32 +0000736 SrcList *pTabList, /* List of tables */
drh832508b2002-03-02 17:04:07 +0000737 ExprList *pEList /* Expressions defining the result set */
738){
drhd8bc7082000-06-07 23:51:50 +0000739 Vdbe *v = pParse->pVdbe;
drh6a3ea0e2003-05-02 14:32:12 +0000740 int i, j;
drh9bb575f2004-09-06 17:24:11 +0000741 sqlite3 *db = pParse->db;
drhfcabd462004-02-20 14:50:58 +0000742 int fullNames, shortNames;
743
drhfe2093d2005-01-20 22:48:47 +0000744#ifndef SQLITE_OMIT_EXPLAIN
danielk19773cf86062004-05-26 10:11:05 +0000745 /* If this is an EXPLAIN, skip this step */
746 if( pParse->explain ){
danielk197761de0d12004-05-27 23:56:16 +0000747 return;
danielk19773cf86062004-05-26 10:11:05 +0000748 }
danielk19775338a5f2005-01-20 13:03:10 +0000749#endif
danielk19773cf86062004-05-26 10:11:05 +0000750
drhd6502752004-02-16 03:44:01 +0000751 assert( v!=0 );
danielk19776f8a5032004-05-10 10:34:51 +0000752 if( pParse->colNamesSet || v==0 || sqlite3_malloc_failed ) return;
drhd8bc7082000-06-07 23:51:50 +0000753 pParse->colNamesSet = 1;
drhfcabd462004-02-20 14:50:58 +0000754 fullNames = (db->flags & SQLITE_FullColNames)!=0;
755 shortNames = (db->flags & SQLITE_ShortColNames)!=0;
danielk197722322fd2004-05-25 23:35:17 +0000756 sqlite3VdbeSetNumCols(v, pEList->nExpr);
drh82c3d632000-06-06 21:56:07 +0000757 for(i=0; i<pEList->nExpr; i++){
758 Expr *p;
drh5a387052003-01-11 14:19:51 +0000759 p = pEList->a[i].pExpr;
760 if( p==0 ) continue;
drh82c3d632000-06-06 21:56:07 +0000761 if( pEList->a[i].zName ){
762 char *zName = pEList->a[i].zName;
danielk1977d8123362004-06-12 09:25:12 +0000763 sqlite3VdbeSetColName(v, i, zName, strlen(zName));
drh82c3d632000-06-06 21:56:07 +0000764 continue;
765 }
drhfa173a72002-07-10 21:26:00 +0000766 if( p->op==TK_COLUMN && pTabList ){
drh6a3ea0e2003-05-02 14:32:12 +0000767 Table *pTab;
drh97665872002-02-13 23:22:53 +0000768 char *zCol;
drh8aff1012001-12-22 14:49:24 +0000769 int iCol = p->iColumn;
drh6a3ea0e2003-05-02 14:32:12 +0000770 for(j=0; j<pTabList->nSrc && pTabList->a[j].iCursor!=p->iTable; j++){}
771 assert( j<pTabList->nSrc );
772 pTab = pTabList->a[j].pTab;
drh8aff1012001-12-22 14:49:24 +0000773 if( iCol<0 ) iCol = pTab->iPKey;
drh97665872002-02-13 23:22:53 +0000774 assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
drhb1363202002-06-26 02:45:03 +0000775 if( iCol<0 ){
drh47a6db22005-01-18 16:02:40 +0000776 zCol = "rowid";
drhb1363202002-06-26 02:45:03 +0000777 }else{
778 zCol = pTab->aCol[iCol].zName;
drhb1363202002-06-26 02:45:03 +0000779 }
drhfcabd462004-02-20 14:50:58 +0000780 if( !shortNames && !fullNames && p->span.z && p->span.z[0] ){
danielk19773cf86062004-05-26 10:11:05 +0000781 sqlite3VdbeSetColName(v, i, p->span.z, p->span.n);
drhfcabd462004-02-20 14:50:58 +0000782 }else if( fullNames || (!shortNames && pTabList->nSrc>1) ){
drh82c3d632000-06-06 21:56:07 +0000783 char *zName = 0;
drh82c3d632000-06-06 21:56:07 +0000784 char *zTab;
785
drh6a3ea0e2003-05-02 14:32:12 +0000786 zTab = pTabList->a[j].zAlias;
drhfcabd462004-02-20 14:50:58 +0000787 if( fullNames || zTab==0 ) zTab = pTab->zName;
danielk19774adee202004-05-08 08:23:19 +0000788 sqlite3SetString(&zName, zTab, ".", zCol, 0);
danielk19773cf86062004-05-26 10:11:05 +0000789 sqlite3VdbeSetColName(v, i, zName, P3_DYNAMIC);
drh82c3d632000-06-06 21:56:07 +0000790 }else{
drh47a6db22005-01-18 16:02:40 +0000791 sqlite3VdbeSetColName(v, i, zCol, strlen(zCol));
drh82c3d632000-06-06 21:56:07 +0000792 }
drh6977fea2002-10-22 23:38:04 +0000793 }else if( p->span.z && p->span.z[0] ){
danielk19773cf86062004-05-26 10:11:05 +0000794 sqlite3VdbeSetColName(v, i, p->span.z, p->span.n);
795 /* sqlite3VdbeCompressSpace(v, addr); */
drh1bee3d72001-10-15 00:44:35 +0000796 }else{
797 char zName[30];
798 assert( p->op!=TK_COLUMN || pTabList==0 );
799 sprintf(zName, "column%d", i+1);
danielk19773cf86062004-05-26 10:11:05 +0000800 sqlite3VdbeSetColName(v, i, zName, 0);
drh82c3d632000-06-06 21:56:07 +0000801 }
802 }
danielk197776d505b2004-05-28 13:13:02 +0000803 generateColumnTypes(pParse, pTabList, pEList);
drh82c3d632000-06-06 21:56:07 +0000804}
805
806/*
drhd8bc7082000-06-07 23:51:50 +0000807** Name of the connection operator, used for error messages.
808*/
809static const char *selectOpName(int id){
810 char *z;
811 switch( id ){
812 case TK_ALL: z = "UNION ALL"; break;
813 case TK_INTERSECT: z = "INTERSECT"; break;
814 case TK_EXCEPT: z = "EXCEPT"; break;
815 default: z = "UNION"; break;
816 }
817 return z;
818}
819
820/*
drh315555c2002-10-20 15:53:03 +0000821** Forward declaration
822*/
drh9b3187e2005-01-18 14:45:47 +0000823static int prepSelectStmt(Parse*, Select*);
drh315555c2002-10-20 15:53:03 +0000824
825/*
drh22f70c32002-02-18 01:17:00 +0000826** Given a SELECT statement, generate a Table structure that describes
827** the result set of that SELECT.
828*/
danielk19774adee202004-05-08 08:23:19 +0000829Table *sqlite3ResultSetOfSelect(Parse *pParse, char *zTabName, Select *pSelect){
drh22f70c32002-02-18 01:17:00 +0000830 Table *pTab;
drhb733d032004-01-24 20:18:12 +0000831 int i, j;
drh22f70c32002-02-18 01:17:00 +0000832 ExprList *pEList;
drh290c1942004-08-21 17:54:45 +0000833 Column *aCol, *pCol;
drh22f70c32002-02-18 01:17:00 +0000834
drh9b3187e2005-01-18 14:45:47 +0000835 if( prepSelectStmt(pParse, pSelect) ){
drh22f70c32002-02-18 01:17:00 +0000836 return 0;
837 }
838 pTab = sqliteMalloc( sizeof(Table) );
839 if( pTab==0 ){
840 return 0;
841 }
842 pTab->zName = zTabName ? sqliteStrDup(zTabName) : 0;
843 pEList = pSelect->pEList;
844 pTab->nCol = pEList->nExpr;
drh417be792002-03-03 18:59:40 +0000845 assert( pTab->nCol>0 );
drhb733d032004-01-24 20:18:12 +0000846 pTab->aCol = aCol = sqliteMalloc( sizeof(pTab->aCol[0])*pTab->nCol );
drh290c1942004-08-21 17:54:45 +0000847 for(i=0, pCol=aCol; i<pTab->nCol; i++, pCol++){
drh79d5f632005-01-18 17:20:10 +0000848 Expr *p, *pR;
danielk1977517eb642004-06-07 10:00:31 +0000849 char *zType;
drh91bb0ee2004-09-01 03:06:34 +0000850 char *zName;
drh79d5f632005-01-18 17:20:10 +0000851 char *zBasename;
852 int cnt;
853
854 /* Get an appropriate name for the column
855 */
856 p = pEList->a[i].pExpr;
drh290c1942004-08-21 17:54:45 +0000857 assert( p->pRight==0 || p->pRight->token.z==0 || p->pRight->token.z[0]!=0 );
drh91bb0ee2004-09-01 03:06:34 +0000858 if( (zName = pEList->a[i].zName)!=0 ){
drh79d5f632005-01-18 17:20:10 +0000859 /* If the column contains an "AS <name>" phrase, use <name> as the name */
drh91bb0ee2004-09-01 03:06:34 +0000860 zName = sqliteStrDup(zName);
danielk1977517eb642004-06-07 10:00:31 +0000861 }else if( p->op==TK_DOT
drh79d5f632005-01-18 17:20:10 +0000862 && (pR=p->pRight)!=0 && pR->token.z && pR->token.z[0] ){
863 /* For columns of the from A.B use B as the name */
drh91bb0ee2004-09-01 03:06:34 +0000864 zName = sqlite3MPrintf("%T", &pR->token);
drhb733d032004-01-24 20:18:12 +0000865 }else if( p->span.z && p->span.z[0] ){
drh79d5f632005-01-18 17:20:10 +0000866 /* Use the original text of the column expression as its name */
drh91bb0ee2004-09-01 03:06:34 +0000867 zName = sqlite3MPrintf("%T", &p->span);
drh22f70c32002-02-18 01:17:00 +0000868 }else{
drh79d5f632005-01-18 17:20:10 +0000869 /* If all else fails, make up a name */
drh91bb0ee2004-09-01 03:06:34 +0000870 zName = sqlite3MPrintf("column%d", i+1);
drh22f70c32002-02-18 01:17:00 +0000871 }
drh91bb0ee2004-09-01 03:06:34 +0000872 sqlite3Dequote(zName);
drh79d5f632005-01-18 17:20:10 +0000873
874 /* Make sure the column name is unique. If the name is not unique,
875 ** append a integer to the name so that it becomes unique.
876 */
877 zBasename = zName;
878 for(j=cnt=0; j<i; j++){
879 if( sqlite3StrICmp(aCol[j].zName, zName)==0 ){
880 zName = sqlite3MPrintf("%s:%d", zBasename, ++cnt);
881 j = -1;
882 }
883 }
884 if( zBasename!=zName ){
885 sqliteFree(zBasename);
886 }
drh91bb0ee2004-09-01 03:06:34 +0000887 pCol->zName = zName;
danielk1977517eb642004-06-07 10:00:31 +0000888
drh79d5f632005-01-18 17:20:10 +0000889 /* Get the typename, type affinity, and collating sequence for the
890 ** column.
891 */
danielk1977517eb642004-06-07 10:00:31 +0000892 zType = sqliteStrDup(columnType(pParse, pSelect->pSrc ,p));
drh290c1942004-08-21 17:54:45 +0000893 pCol->zType = zType;
894 pCol->affinity = SQLITE_AFF_NUMERIC;
danielk1977517eb642004-06-07 10:00:31 +0000895 if( zType ){
drh290c1942004-08-21 17:54:45 +0000896 pCol->affinity = sqlite3AffinityType(zType, strlen(zType));
danielk1977517eb642004-06-07 10:00:31 +0000897 }
drh290c1942004-08-21 17:54:45 +0000898 pCol->pColl = sqlite3ExprCollSeq(pParse, p);
899 if( !pCol->pColl ){
900 pCol->pColl = pParse->db->pDfltColl;
danielk19770202b292004-06-09 09:55:16 +0000901 }
drh22f70c32002-02-18 01:17:00 +0000902 }
903 pTab->iPKey = -1;
904 return pTab;
905}
906
907/*
drh9b3187e2005-01-18 14:45:47 +0000908** Prepare a SELECT statement for processing by doing the following
909** things:
drhd8bc7082000-06-07 23:51:50 +0000910**
drh9b3187e2005-01-18 14:45:47 +0000911** (1) Make sure VDBE cursor numbers have been assigned to every
912** element of the FROM clause.
913**
914** (2) Fill in the pTabList->a[].pTab fields in the SrcList that
915** defines FROM clause. When views appear in the FROM clause,
drh63eb5f22003-04-29 16:20:44 +0000916** fill pTabList->a[].pSelect with a copy of the SELECT statement
917** that implements the view. A copy is made of the view's SELECT
918** statement so that we can freely modify or delete that statement
919** without worrying about messing up the presistent representation
920** of the view.
drhd8bc7082000-06-07 23:51:50 +0000921**
drh9b3187e2005-01-18 14:45:47 +0000922** (3) Add terms to the WHERE clause to accomodate the NATURAL keyword
drhad2d8302002-05-24 20:31:36 +0000923** on joins and the ON and USING clause of joins.
924**
drh9b3187e2005-01-18 14:45:47 +0000925** (4) Scan the list of columns in the result set (pEList) looking
drh54473222002-04-04 02:10:55 +0000926** for instances of the "*" operator or the TABLE.* operator.
927** If found, expand each "*" to be every column in every table
928** and TABLE.* to be every column in TABLE.
drhd8bc7082000-06-07 23:51:50 +0000929**
930** Return 0 on success. If there are problems, leave an error message
931** in pParse and return non-zero.
932*/
drh9b3187e2005-01-18 14:45:47 +0000933static int prepSelectStmt(Parse *pParse, Select *p){
drh54473222002-04-04 02:10:55 +0000934 int i, j, k, rc;
drhad3cab52002-05-24 02:04:32 +0000935 SrcList *pTabList;
drhdaffd0e2001-04-11 14:28:42 +0000936 ExprList *pEList;
drha76b5df2002-02-23 02:32:10 +0000937 Table *pTab;
drh290c1942004-08-21 17:54:45 +0000938 struct SrcList_item *pFrom;
drhdaffd0e2001-04-11 14:28:42 +0000939
940 if( p==0 || p->pSrc==0 ) return 1;
941 pTabList = p->pSrc;
942 pEList = p->pEList;
drhd8bc7082000-06-07 23:51:50 +0000943
drh9b3187e2005-01-18 14:45:47 +0000944 /* Make sure cursor numbers have been assigned to all entries in
945 ** the FROM clause of the SELECT statement.
946 */
947 sqlite3SrcListAssignCursors(pParse, p->pSrc);
948
949 /* Look up every table named in the FROM clause of the select. If
950 ** an entry of the FROM clause is a subquery instead of a table or view,
951 ** then create a transient table structure to describe the subquery.
drhd8bc7082000-06-07 23:51:50 +0000952 */
drh290c1942004-08-21 17:54:45 +0000953 for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
drh9b3187e2005-01-18 14:45:47 +0000954 if( pFrom->pTab!=0 ){
955 /* This statement has already been prepared. There is no need
956 ** to go further. */
957 assert( i==0 );
drhd8bc7082000-06-07 23:51:50 +0000958 return 0;
959 }
drh290c1942004-08-21 17:54:45 +0000960 if( pFrom->zName==0 ){
drh22f70c32002-02-18 01:17:00 +0000961 /* A sub-query in the FROM clause of a SELECT */
drh290c1942004-08-21 17:54:45 +0000962 assert( pFrom->pSelect!=0 );
963 if( pFrom->zAlias==0 ){
drh91bb0ee2004-09-01 03:06:34 +0000964 pFrom->zAlias =
965 sqlite3MPrintf("sqlite_subquery_%p_", (void*)pFrom->pSelect);
drhad2d8302002-05-24 20:31:36 +0000966 }
drh290c1942004-08-21 17:54:45 +0000967 pFrom->pTab = pTab =
968 sqlite3ResultSetOfSelect(pParse, pFrom->zAlias, pFrom->pSelect);
drh22f70c32002-02-18 01:17:00 +0000969 if( pTab==0 ){
970 return 1;
971 }
drh5cf590c2003-04-24 01:45:04 +0000972 /* The isTransient flag indicates that the Table structure has been
973 ** dynamically allocated and may be freed at any time. In other words,
974 ** pTab is not pointing to a persistent table structure that defines
975 ** part of the schema. */
drh22f70c32002-02-18 01:17:00 +0000976 pTab->isTransient = 1;
977 }else{
drha76b5df2002-02-23 02:32:10 +0000978 /* An ordinary table or view name in the FROM clause */
drh290c1942004-08-21 17:54:45 +0000979 pFrom->pTab = pTab =
980 sqlite3LocateTable(pParse,pFrom->zName,pFrom->zDatabase);
drha76b5df2002-02-23 02:32:10 +0000981 if( pTab==0 ){
drh22f70c32002-02-18 01:17:00 +0000982 return 1;
983 }
drha76b5df2002-02-23 02:32:10 +0000984 if( pTab->pSelect ){
drh63eb5f22003-04-29 16:20:44 +0000985 /* We reach here if the named table is a really a view */
danielk19774adee202004-05-08 08:23:19 +0000986 if( sqlite3ViewGetColumnNames(pParse, pTab) ){
drh417be792002-03-03 18:59:40 +0000987 return 1;
988 }
drh290c1942004-08-21 17:54:45 +0000989 /* If pFrom->pSelect!=0 it means we are dealing with a
drh63eb5f22003-04-29 16:20:44 +0000990 ** view within a view. The SELECT structure has already been
991 ** copied by the outer view so we can skip the copy step here
992 ** in the inner view.
993 */
drh290c1942004-08-21 17:54:45 +0000994 if( pFrom->pSelect==0 ){
995 pFrom->pSelect = sqlite3SelectDup(pTab->pSelect);
drh63eb5f22003-04-29 16:20:44 +0000996 }
drha76b5df2002-02-23 02:32:10 +0000997 }
drhd8bc7082000-06-07 23:51:50 +0000998 }
999 }
1000
drhad2d8302002-05-24 20:31:36 +00001001 /* Process NATURAL keywords, and ON and USING clauses of joins.
1002 */
1003 if( sqliteProcessJoin(pParse, p) ) return 1;
1004
drh7c917d12001-12-16 20:05:05 +00001005 /* For every "*" that occurs in the column list, insert the names of
drh54473222002-04-04 02:10:55 +00001006 ** all columns in all tables. And for every TABLE.* insert the names
1007 ** of all columns in TABLE. The parser inserted a special expression
drh7c917d12001-12-16 20:05:05 +00001008 ** with the TK_ALL operator for each "*" that it found in the column list.
1009 ** The following code just has to locate the TK_ALL expressions and expand
1010 ** each one to the list of all columns in all tables.
drh54473222002-04-04 02:10:55 +00001011 **
1012 ** The first loop just checks to see if there are any "*" operators
1013 ** that need expanding.
drhd8bc7082000-06-07 23:51:50 +00001014 */
drh7c917d12001-12-16 20:05:05 +00001015 for(k=0; k<pEList->nExpr; k++){
drh54473222002-04-04 02:10:55 +00001016 Expr *pE = pEList->a[k].pExpr;
1017 if( pE->op==TK_ALL ) break;
1018 if( pE->op==TK_DOT && pE->pRight && pE->pRight->op==TK_ALL
1019 && pE->pLeft && pE->pLeft->op==TK_ID ) break;
drh7c917d12001-12-16 20:05:05 +00001020 }
drh54473222002-04-04 02:10:55 +00001021 rc = 0;
drh7c917d12001-12-16 20:05:05 +00001022 if( k<pEList->nExpr ){
drh54473222002-04-04 02:10:55 +00001023 /*
1024 ** If we get here it means the result set contains one or more "*"
1025 ** operators that need to be expanded. Loop through each expression
1026 ** in the result set and expand them one by one.
1027 */
drh7c917d12001-12-16 20:05:05 +00001028 struct ExprList_item *a = pEList->a;
1029 ExprList *pNew = 0;
1030 for(k=0; k<pEList->nExpr; k++){
drh54473222002-04-04 02:10:55 +00001031 Expr *pE = a[k].pExpr;
1032 if( pE->op!=TK_ALL &&
1033 (pE->op!=TK_DOT || pE->pRight==0 || pE->pRight->op!=TK_ALL) ){
1034 /* This particular expression does not need to be expanded.
1035 */
danielk19774adee202004-05-08 08:23:19 +00001036 pNew = sqlite3ExprListAppend(pNew, a[k].pExpr, 0);
drh7c917d12001-12-16 20:05:05 +00001037 pNew->a[pNew->nExpr-1].zName = a[k].zName;
1038 a[k].pExpr = 0;
1039 a[k].zName = 0;
1040 }else{
drh54473222002-04-04 02:10:55 +00001041 /* This expression is a "*" or a "TABLE.*" and needs to be
1042 ** expanded. */
1043 int tableSeen = 0; /* Set to 1 when TABLE matches */
drhcf55b7a2004-07-20 01:45:19 +00001044 char *zTName; /* text of name of TABLE */
drh54473222002-04-04 02:10:55 +00001045 if( pE->op==TK_DOT && pE->pLeft ){
drhcf55b7a2004-07-20 01:45:19 +00001046 zTName = sqlite3NameFromToken(&pE->pLeft->token);
drh54473222002-04-04 02:10:55 +00001047 }else{
drhcf55b7a2004-07-20 01:45:19 +00001048 zTName = 0;
drh54473222002-04-04 02:10:55 +00001049 }
drh290c1942004-08-21 17:54:45 +00001050 for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
1051 Table *pTab = pFrom->pTab;
1052 char *zTabName = pFrom->zAlias;
drh54473222002-04-04 02:10:55 +00001053 if( zTabName==0 || zTabName[0]==0 ){
1054 zTabName = pTab->zName;
1055 }
drhcf55b7a2004-07-20 01:45:19 +00001056 if( zTName && (zTabName==0 || zTabName[0]==0 ||
1057 sqlite3StrICmp(zTName, zTabName)!=0) ){
drh54473222002-04-04 02:10:55 +00001058 continue;
1059 }
1060 tableSeen = 1;
drh7c917d12001-12-16 20:05:05 +00001061 for(j=0; j<pTab->nCol; j++){
drh22f70c32002-02-18 01:17:00 +00001062 Expr *pExpr, *pLeft, *pRight;
drhad2d8302002-05-24 20:31:36 +00001063 char *zName = pTab->aCol[j].zName;
1064
drh91bb0ee2004-09-01 03:06:34 +00001065 if( i>0 ){
1066 struct SrcList_item *pLeft = &pTabList->a[i-1];
1067 if( (pLeft->jointype & JT_NATURAL)!=0 &&
1068 columnIndex(pLeft->pTab, zName)>=0 ){
1069 /* In a NATURAL join, omit the join columns from the
1070 ** table on the right */
1071 continue;
1072 }
1073 if( sqlite3IdListIndex(pLeft->pUsing, zName)>=0 ){
1074 /* In a join with a USING clause, omit columns in the
1075 ** using clause from the table on the right. */
1076 continue;
1077 }
drhad2d8302002-05-24 20:31:36 +00001078 }
danielk19774adee202004-05-08 08:23:19 +00001079 pRight = sqlite3Expr(TK_ID, 0, 0, 0);
drh22f70c32002-02-18 01:17:00 +00001080 if( pRight==0 ) break;
drh91bb0ee2004-09-01 03:06:34 +00001081 setToken(&pRight->token, zName);
drh4b59ab52002-08-24 18:24:51 +00001082 if( zTabName && pTabList->nSrc>1 ){
danielk19774adee202004-05-08 08:23:19 +00001083 pLeft = sqlite3Expr(TK_ID, 0, 0, 0);
1084 pExpr = sqlite3Expr(TK_DOT, pLeft, pRight, 0);
drh22f70c32002-02-18 01:17:00 +00001085 if( pExpr==0 ) break;
drh91bb0ee2004-09-01 03:06:34 +00001086 setToken(&pLeft->token, zTabName);
1087 setToken(&pExpr->span, sqlite3MPrintf("%s.%s", zTabName, zName));
drh6977fea2002-10-22 23:38:04 +00001088 pExpr->span.dyn = 1;
1089 pExpr->token.z = 0;
1090 pExpr->token.n = 0;
1091 pExpr->token.dyn = 0;
drh7c917d12001-12-16 20:05:05 +00001092 }else{
drh22f70c32002-02-18 01:17:00 +00001093 pExpr = pRight;
drh6977fea2002-10-22 23:38:04 +00001094 pExpr->span = pExpr->token;
drh7c917d12001-12-16 20:05:05 +00001095 }
drh79d5f632005-01-18 17:20:10 +00001096 pNew = sqlite3ExprListAppend(pNew, pExpr, &pRight->token);
drh7c917d12001-12-16 20:05:05 +00001097 }
drh17e24df2001-11-06 14:10:41 +00001098 }
drh54473222002-04-04 02:10:55 +00001099 if( !tableSeen ){
drhcf55b7a2004-07-20 01:45:19 +00001100 if( zTName ){
1101 sqlite3ErrorMsg(pParse, "no such table: %s", zTName);
drhf5db2d32002-06-06 23:42:27 +00001102 }else{
danielk19774adee202004-05-08 08:23:19 +00001103 sqlite3ErrorMsg(pParse, "no tables specified");
drhf5db2d32002-06-06 23:42:27 +00001104 }
drh54473222002-04-04 02:10:55 +00001105 rc = 1;
1106 }
drhcf55b7a2004-07-20 01:45:19 +00001107 sqliteFree(zTName);
drhd8bc7082000-06-07 23:51:50 +00001108 }
1109 }
danielk19774adee202004-05-08 08:23:19 +00001110 sqlite3ExprListDelete(pEList);
drh7c917d12001-12-16 20:05:05 +00001111 p->pEList = pNew;
drhd8bc7082000-06-07 23:51:50 +00001112 }
drh54473222002-04-04 02:10:55 +00001113 return rc;
drhd8bc7082000-06-07 23:51:50 +00001114}
1115
1116/*
drhff78bd22002-02-27 01:47:11 +00001117** This routine recursively unlinks the Select.pSrc.a[].pTab pointers
1118** in a select structure. It just sets the pointers to NULL. This
1119** routine is recursive in the sense that if the Select.pSrc.a[].pSelect
1120** pointer is not NULL, this routine is called recursively on that pointer.
1121**
1122** This routine is called on the Select structure that defines a
1123** VIEW in order to undo any bindings to tables. This is necessary
1124** because those tables might be DROPed by a subsequent SQL command.
drh5cf590c2003-04-24 01:45:04 +00001125** If the bindings are not removed, then the Select.pSrc->a[].pTab field
1126** will be left pointing to a deallocated Table structure after the
1127** DROP and a coredump will occur the next time the VIEW is used.
drhff78bd22002-02-27 01:47:11 +00001128*/
danielk19775338a5f2005-01-20 13:03:10 +00001129#if 0
danielk19774adee202004-05-08 08:23:19 +00001130void sqlite3SelectUnbind(Select *p){
drhff78bd22002-02-27 01:47:11 +00001131 int i;
drhad3cab52002-05-24 02:04:32 +00001132 SrcList *pSrc = p->pSrc;
drh91bb0ee2004-09-01 03:06:34 +00001133 struct SrcList_item *pItem;
drhff78bd22002-02-27 01:47:11 +00001134 Table *pTab;
1135 if( p==0 ) return;
drh91bb0ee2004-09-01 03:06:34 +00001136 for(i=0, pItem=pSrc->a; i<pSrc->nSrc; i++, pItem++){
1137 if( (pTab = pItem->pTab)!=0 ){
drhff78bd22002-02-27 01:47:11 +00001138 if( pTab->isTransient ){
danielk19774adee202004-05-08 08:23:19 +00001139 sqlite3DeleteTable(0, pTab);
drhff78bd22002-02-27 01:47:11 +00001140 }
drh91bb0ee2004-09-01 03:06:34 +00001141 pItem->pTab = 0;
1142 if( pItem->pSelect ){
1143 sqlite3SelectUnbind(pItem->pSelect);
drhff78bd22002-02-27 01:47:11 +00001144 }
1145 }
1146 }
1147}
danielk19775338a5f2005-01-20 13:03:10 +00001148#endif
drhff78bd22002-02-27 01:47:11 +00001149
1150/*
drhd8bc7082000-06-07 23:51:50 +00001151** This routine associates entries in an ORDER BY expression list with
1152** columns in a result. For each ORDER BY expression, the opcode of
drh967e8b72000-06-21 13:59:10 +00001153** the top-level node is changed to TK_COLUMN and the iColumn value of
drhd8bc7082000-06-07 23:51:50 +00001154** the top-level node is filled in with column number and the iTable
1155** value of the top-level node is filled with iTable parameter.
1156**
1157** If there are prior SELECT clauses, they are processed first. A match
1158** in an earlier SELECT takes precedence over a later SELECT.
1159**
1160** Any entry that does not match is flagged as an error. The number
1161** of errors is returned.
1162*/
1163static int matchOrderbyToColumn(
1164 Parse *pParse, /* A place to leave error messages */
1165 Select *pSelect, /* Match to result columns of this SELECT */
1166 ExprList *pOrderBy, /* The ORDER BY values to match against columns */
drhe4de1fe2002-06-02 16:09:01 +00001167 int iTable, /* Insert this value in iTable */
drhd8bc7082000-06-07 23:51:50 +00001168 int mustComplete /* If TRUE all ORDER BYs must match */
1169){
1170 int nErr = 0;
1171 int i, j;
1172 ExprList *pEList;
1173
drhdaffd0e2001-04-11 14:28:42 +00001174 if( pSelect==0 || pOrderBy==0 ) return 1;
drhd8bc7082000-06-07 23:51:50 +00001175 if( mustComplete ){
1176 for(i=0; i<pOrderBy->nExpr; i++){ pOrderBy->a[i].done = 0; }
1177 }
drh9b3187e2005-01-18 14:45:47 +00001178 if( prepSelectStmt(pParse, pSelect) ){
drhd8bc7082000-06-07 23:51:50 +00001179 return 1;
1180 }
1181 if( pSelect->pPrior ){
drh92cd52f2000-06-08 01:55:29 +00001182 if( matchOrderbyToColumn(pParse, pSelect->pPrior, pOrderBy, iTable, 0) ){
1183 return 1;
1184 }
drhd8bc7082000-06-07 23:51:50 +00001185 }
1186 pEList = pSelect->pEList;
1187 for(i=0; i<pOrderBy->nExpr; i++){
1188 Expr *pE = pOrderBy->a[i].pExpr;
drhe4de1fe2002-06-02 16:09:01 +00001189 int iCol = -1;
drhd8bc7082000-06-07 23:51:50 +00001190 if( pOrderBy->a[i].done ) continue;
danielk19774adee202004-05-08 08:23:19 +00001191 if( sqlite3ExprIsInteger(pE, &iCol) ){
drhe4de1fe2002-06-02 16:09:01 +00001192 if( iCol<=0 || iCol>pEList->nExpr ){
danielk19774adee202004-05-08 08:23:19 +00001193 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00001194 "ORDER BY position %d should be between 1 and %d",
1195 iCol, pEList->nExpr);
drhe4de1fe2002-06-02 16:09:01 +00001196 nErr++;
1197 break;
1198 }
drhfcb78a42003-01-18 20:11:05 +00001199 if( !mustComplete ) continue;
drhe4de1fe2002-06-02 16:09:01 +00001200 iCol--;
1201 }
1202 for(j=0; iCol<0 && j<pEList->nExpr; j++){
drh4cfa7932000-06-08 15:10:46 +00001203 if( pEList->a[j].zName && (pE->op==TK_ID || pE->op==TK_STRING) ){
drha76b5df2002-02-23 02:32:10 +00001204 char *zName, *zLabel;
1205 zName = pEList->a[j].zName;
drha99db3b2004-06-19 14:49:12 +00001206 zLabel = sqlite3NameFromToken(&pE->token);
1207 assert( zLabel!=0 );
danielk19774adee202004-05-08 08:23:19 +00001208 if( sqlite3StrICmp(zName, zLabel)==0 ){
drhe4de1fe2002-06-02 16:09:01 +00001209 iCol = j;
drhd8bc7082000-06-07 23:51:50 +00001210 }
drh6e142f52000-06-08 13:36:40 +00001211 sqliteFree(zLabel);
drhd8bc7082000-06-07 23:51:50 +00001212 }
danielk19774adee202004-05-08 08:23:19 +00001213 if( iCol<0 && sqlite3ExprCompare(pE, pEList->a[j].pExpr) ){
drhe4de1fe2002-06-02 16:09:01 +00001214 iCol = j;
drhd8bc7082000-06-07 23:51:50 +00001215 }
1216 }
drhe4de1fe2002-06-02 16:09:01 +00001217 if( iCol>=0 ){
1218 pE->op = TK_COLUMN;
1219 pE->iColumn = iCol;
1220 pE->iTable = iTable;
1221 pOrderBy->a[i].done = 1;
1222 }
1223 if( iCol<0 && mustComplete ){
danielk19774adee202004-05-08 08:23:19 +00001224 sqlite3ErrorMsg(pParse,
drhda93d232003-03-31 02:12:46 +00001225 "ORDER BY term number %d does not match any result column", i+1);
drhd8bc7082000-06-07 23:51:50 +00001226 nErr++;
1227 break;
1228 }
1229 }
1230 return nErr;
1231}
1232
1233/*
1234** Get a VDBE for the given parser context. Create a new one if necessary.
1235** If an error occurs, return NULL and leave a message in pParse.
1236*/
danielk19774adee202004-05-08 08:23:19 +00001237Vdbe *sqlite3GetVdbe(Parse *pParse){
drhd8bc7082000-06-07 23:51:50 +00001238 Vdbe *v = pParse->pVdbe;
1239 if( v==0 ){
danielk19774adee202004-05-08 08:23:19 +00001240 v = pParse->pVdbe = sqlite3VdbeCreate(pParse->db);
drhd8bc7082000-06-07 23:51:50 +00001241 }
drhd8bc7082000-06-07 23:51:50 +00001242 return v;
1243}
drhfcb78a42003-01-18 20:11:05 +00001244
drhd8bc7082000-06-07 23:51:50 +00001245/*
drh7b58dae2003-07-20 01:16:46 +00001246** Compute the iLimit and iOffset fields of the SELECT based on the
1247** nLimit and nOffset fields. nLimit and nOffset hold the integers
1248** that appear in the original SQL statement after the LIMIT and OFFSET
1249** keywords. Or that hold -1 and 0 if those keywords are omitted.
1250** iLimit and iOffset are the integer memory register numbers for
1251** counters used to compute the limit and offset. If there is no
1252** limit and/or offset, then iLimit and iOffset are negative.
1253**
1254** This routine changes the values if iLimit and iOffset only if
1255** a limit or offset is defined by nLimit and nOffset. iLimit and
1256** iOffset should have been preset to appropriate default values
1257** (usually but not always -1) prior to calling this routine.
1258** Only if nLimit>=0 or nOffset>0 do the limit registers get
1259** redefined. The UNION ALL operator uses this property to force
1260** the reuse of the same limit and offset registers across multiple
1261** SELECT statements.
1262*/
1263static void computeLimitRegisters(Parse *pParse, Select *p){
1264 /*
1265 ** If the comparison is p->nLimit>0 then "LIMIT 0" shows
1266 ** all rows. It is the same as no limit. If the comparision is
1267 ** p->nLimit>=0 then "LIMIT 0" show no rows at all.
1268 ** "LIMIT -1" always shows all rows. There is some
1269 ** contraversy about what the correct behavior should be.
1270 ** The current implementation interprets "LIMIT 0" to mean
1271 ** no rows.
1272 */
1273 if( p->nLimit>=0 ){
1274 int iMem = pParse->nMem++;
danielk19774adee202004-05-08 08:23:19 +00001275 Vdbe *v = sqlite3GetVdbe(pParse);
drh7b58dae2003-07-20 01:16:46 +00001276 if( v==0 ) return;
danielk19774adee202004-05-08 08:23:19 +00001277 sqlite3VdbeAddOp(v, OP_Integer, -p->nLimit, 0);
1278 sqlite3VdbeAddOp(v, OP_MemStore, iMem, 1);
drhad6d9462004-09-19 02:15:24 +00001279 VdbeComment((v, "# LIMIT counter"));
drh7b58dae2003-07-20 01:16:46 +00001280 p->iLimit = iMem;
1281 }
1282 if( p->nOffset>0 ){
1283 int iMem = pParse->nMem++;
danielk19774adee202004-05-08 08:23:19 +00001284 Vdbe *v = sqlite3GetVdbe(pParse);
drh7b58dae2003-07-20 01:16:46 +00001285 if( v==0 ) return;
danielk19774adee202004-05-08 08:23:19 +00001286 sqlite3VdbeAddOp(v, OP_Integer, -p->nOffset, 0);
1287 sqlite3VdbeAddOp(v, OP_MemStore, iMem, 1);
drhad6d9462004-09-19 02:15:24 +00001288 VdbeComment((v, "# OFFSET counter"));
drh7b58dae2003-07-20 01:16:46 +00001289 p->iOffset = iMem;
1290 }
1291}
1292
1293/*
drhd3d39e92004-05-20 22:16:29 +00001294** Generate VDBE instructions that will open a transient table that
1295** will be used for an index or to store keyed results for a compound
1296** select. In other words, open a transient table that needs a
1297** KeyInfo structure. The number of columns in the KeyInfo is determined
1298** by the result set of the SELECT statement in the second argument.
1299**
danielk1977dc1bdc42004-06-11 10:51:27 +00001300** Specifically, this routine is called to open an index table for
1301** DISTINCT, UNION, INTERSECT and EXCEPT select statements (but not
1302** UNION ALL).
1303**
drhd3d39e92004-05-20 22:16:29 +00001304** Make the new table a KeyAsData table if keyAsData is true.
danielk1977dc1bdc42004-06-11 10:51:27 +00001305**
1306** The value returned is the address of the OP_OpenTemp instruction.
drhd3d39e92004-05-20 22:16:29 +00001307*/
danielk1977dc1bdc42004-06-11 10:51:27 +00001308static int openTempIndex(Parse *pParse, Select *p, int iTab, int keyAsData){
drhd3d39e92004-05-20 22:16:29 +00001309 KeyInfo *pKeyInfo;
drh736c22b2004-05-21 02:14:24 +00001310 int nColumn;
drh9bb575f2004-09-06 17:24:11 +00001311 sqlite3 *db = pParse->db;
drhd3d39e92004-05-20 22:16:29 +00001312 int i;
1313 Vdbe *v = pParse->pVdbe;
danielk1977dc1bdc42004-06-11 10:51:27 +00001314 int addr;
drhd3d39e92004-05-20 22:16:29 +00001315
drh9b3187e2005-01-18 14:45:47 +00001316 if( prepSelectStmt(pParse, p) ){
danielk1977dc1bdc42004-06-11 10:51:27 +00001317 return 0;
drh736c22b2004-05-21 02:14:24 +00001318 }
1319 nColumn = p->pEList->nExpr;
drhd3d39e92004-05-20 22:16:29 +00001320 pKeyInfo = sqliteMalloc( sizeof(*pKeyInfo)+nColumn*sizeof(CollSeq*) );
danielk1977dc1bdc42004-06-11 10:51:27 +00001321 if( pKeyInfo==0 ) return 0;
drh91bb0ee2004-09-01 03:06:34 +00001322 pKeyInfo->enc = db->enc;
drhd3d39e92004-05-20 22:16:29 +00001323 pKeyInfo->nField = nColumn;
1324 for(i=0; i<nColumn; i++){
danielk1977dc1bdc42004-06-11 10:51:27 +00001325 pKeyInfo->aColl[i] = sqlite3ExprCollSeq(pParse, p->pEList->a[i].pExpr);
1326 if( !pKeyInfo->aColl[i] ){
1327 pKeyInfo->aColl[i] = db->pDfltColl;
1328 }
drhd3d39e92004-05-20 22:16:29 +00001329 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001330 addr = sqlite3VdbeOp3(v, OP_OpenTemp, iTab, 0,
1331 (char*)pKeyInfo, P3_KEYINFO_HANDOFF);
drhd3d39e92004-05-20 22:16:29 +00001332 if( keyAsData ){
1333 sqlite3VdbeAddOp(v, OP_KeyAsData, iTab, 1);
1334 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001335 return addr;
1336}
1337
drhb7f91642004-10-31 02:22:47 +00001338#ifndef SQLITE_OMIT_COMPOUND_SELECT
drhfbc4ee72004-08-29 01:31:05 +00001339/*
drh8cdbf832004-08-29 16:25:03 +00001340** Add the address "addr" to the set of all OpenTemp opcode addresses
1341** that are being accumulated in p->ppOpenTemp.
drhfbc4ee72004-08-29 01:31:05 +00001342*/
drh8cdbf832004-08-29 16:25:03 +00001343static int multiSelectOpenTempAddr(Select *p, int addr){
1344 IdList *pList = *p->ppOpenTemp = sqlite3IdListAppend(*p->ppOpenTemp, 0);
drhfbc4ee72004-08-29 01:31:05 +00001345 if( pList==0 ){
danielk1977dc1bdc42004-06-11 10:51:27 +00001346 return SQLITE_NOMEM;
1347 }
drhfbc4ee72004-08-29 01:31:05 +00001348 pList->a[pList->nId-1].idx = addr;
danielk1977dc1bdc42004-06-11 10:51:27 +00001349 return SQLITE_OK;
1350}
drhb7f91642004-10-31 02:22:47 +00001351#endif /* SQLITE_OMIT_COMPOUND_SELECT */
danielk1977dc1bdc42004-06-11 10:51:27 +00001352
drhb7f91642004-10-31 02:22:47 +00001353#ifndef SQLITE_OMIT_COMPOUND_SELECT
drhfbc4ee72004-08-29 01:31:05 +00001354/*
1355** Return the appropriate collating sequence for the iCol-th column of
1356** the result set for the compound-select statement "p". Return NULL if
1357** the column has no default collating sequence.
1358**
1359** The collating sequence for the compound select is taken from the
1360** left-most term of the select that has a collating sequence.
1361*/
danielk1977dc1bdc42004-06-11 10:51:27 +00001362static CollSeq *multiSelectCollSeq(Parse *pParse, Select *p, int iCol){
drhfbc4ee72004-08-29 01:31:05 +00001363 CollSeq *pRet;
danielk1977dc1bdc42004-06-11 10:51:27 +00001364 if( p->pPrior ){
1365 pRet = multiSelectCollSeq(pParse, p->pPrior, iCol);
drhfbc4ee72004-08-29 01:31:05 +00001366 }else{
1367 pRet = 0;
danielk1977dc1bdc42004-06-11 10:51:27 +00001368 }
drhfbc4ee72004-08-29 01:31:05 +00001369 if( pRet==0 ){
danielk1977dc1bdc42004-06-11 10:51:27 +00001370 pRet = sqlite3ExprCollSeq(pParse, p->pEList->a[iCol].pExpr);
1371 }
1372 return pRet;
drhd3d39e92004-05-20 22:16:29 +00001373}
drhb7f91642004-10-31 02:22:47 +00001374#endif /* SQLITE_OMIT_COMPOUND_SELECT */
drhd3d39e92004-05-20 22:16:29 +00001375
drhb7f91642004-10-31 02:22:47 +00001376#ifndef SQLITE_OMIT_COMPOUND_SELECT
drhd3d39e92004-05-20 22:16:29 +00001377/*
drh82c3d632000-06-06 21:56:07 +00001378** This routine is called to process a query that is really the union
1379** or intersection of two or more separate queries.
drhc926afb2002-06-20 03:38:26 +00001380**
drhe78e8282003-01-19 03:59:45 +00001381** "p" points to the right-most of the two queries. the query on the
1382** left is p->pPrior. The left query could also be a compound query
1383** in which case this routine will be called recursively.
1384**
1385** The results of the total query are to be written into a destination
1386** of type eDest with parameter iParm.
1387**
1388** Example 1: Consider a three-way compound SQL statement.
1389**
1390** SELECT a FROM t1 UNION SELECT b FROM t2 UNION SELECT c FROM t3
1391**
1392** This statement is parsed up as follows:
1393**
1394** SELECT c FROM t3
1395** |
1396** `-----> SELECT b FROM t2
1397** |
jplyon4b11c6d2004-01-19 04:57:53 +00001398** `------> SELECT a FROM t1
drhe78e8282003-01-19 03:59:45 +00001399**
1400** The arrows in the diagram above represent the Select.pPrior pointer.
1401** So if this routine is called with p equal to the t3 query, then
1402** pPrior will be the t2 query. p->op will be TK_UNION in this case.
1403**
1404** Notice that because of the way SQLite parses compound SELECTs, the
1405** individual selects always group from left to right.
drh82c3d632000-06-06 21:56:07 +00001406*/
danielk197784ac9d02004-05-18 09:58:06 +00001407static int multiSelect(
drhfbc4ee72004-08-29 01:31:05 +00001408 Parse *pParse, /* Parsing context */
1409 Select *p, /* The right-most of SELECTs to be coded */
1410 int eDest, /* \___ Store query results as specified */
1411 int iParm, /* / by these two parameters. */
1412 char *aff /* If eDest is SRT_Union, the affinity string */
danielk197784ac9d02004-05-18 09:58:06 +00001413){
drhfbc4ee72004-08-29 01:31:05 +00001414 int rc = SQLITE_OK; /* Success code from a subroutine */
1415 Select *pPrior; /* Another SELECT immediately to our left */
1416 Vdbe *v; /* Generate code to this VDBE */
1417 IdList *pOpenTemp = 0;/* OP_OpenTemp opcodes that need a KeyInfo */
drh8cdbf832004-08-29 16:25:03 +00001418 int aAddr[5]; /* Addresses of SetNumColumns operators */
1419 int nAddr = 0; /* Number used */
1420 int nCol; /* Number of columns in the result set */
drh82c3d632000-06-06 21:56:07 +00001421
drh7b58dae2003-07-20 01:16:46 +00001422 /* Make sure there is no ORDER BY or LIMIT clause on prior SELECTs. Only
drhfbc4ee72004-08-29 01:31:05 +00001423 ** the last (right-most) SELECT in the series may have an ORDER BY or LIMIT.
drh82c3d632000-06-06 21:56:07 +00001424 */
danielk197784ac9d02004-05-18 09:58:06 +00001425 if( p==0 || p->pPrior==0 ){
1426 rc = 1;
1427 goto multi_select_end;
1428 }
drhd8bc7082000-06-07 23:51:50 +00001429 pPrior = p->pPrior;
1430 if( pPrior->pOrderBy ){
danielk19774adee202004-05-08 08:23:19 +00001431 sqlite3ErrorMsg(pParse,"ORDER BY clause should come after %s not before",
drhda93d232003-03-31 02:12:46 +00001432 selectOpName(p->op));
danielk197784ac9d02004-05-18 09:58:06 +00001433 rc = 1;
1434 goto multi_select_end;
drh82c3d632000-06-06 21:56:07 +00001435 }
drh7b58dae2003-07-20 01:16:46 +00001436 if( pPrior->nLimit>=0 || pPrior->nOffset>0 ){
danielk19774adee202004-05-08 08:23:19 +00001437 sqlite3ErrorMsg(pParse,"LIMIT clause should come after %s not before",
drh7b58dae2003-07-20 01:16:46 +00001438 selectOpName(p->op));
danielk197784ac9d02004-05-18 09:58:06 +00001439 rc = 1;
1440 goto multi_select_end;
drh7b58dae2003-07-20 01:16:46 +00001441 }
drh82c3d632000-06-06 21:56:07 +00001442
drhd8bc7082000-06-07 23:51:50 +00001443 /* Make sure we have a valid query engine. If not, create a new one.
1444 */
danielk19774adee202004-05-08 08:23:19 +00001445 v = sqlite3GetVdbe(pParse);
danielk197784ac9d02004-05-18 09:58:06 +00001446 if( v==0 ){
1447 rc = 1;
1448 goto multi_select_end;
1449 }
drhd8bc7082000-06-07 23:51:50 +00001450
drh8cdbf832004-08-29 16:25:03 +00001451 /* If *p this is the right-most select statement, then initialize
1452 ** p->ppOpenTemp to point to pOpenTemp. If *p is not the right most
1453 ** statement then p->ppOpenTemp will have already been initialized
1454 ** by a prior call to this same procedure. Pass along the pOpenTemp
1455 ** pointer to pPrior, the next statement to our left.
drhfbc4ee72004-08-29 01:31:05 +00001456 */
1457 if( p->ppOpenTemp==0 ){
1458 p->ppOpenTemp = &pOpenTemp;
1459 }
1460 pPrior->ppOpenTemp = p->ppOpenTemp;
1461
drh1cc3d752002-03-23 00:31:29 +00001462 /* Create the destination temporary table if necessary
1463 */
1464 if( eDest==SRT_TempTable ){
danielk1977b4964b72004-05-18 01:23:38 +00001465 assert( p->pEList );
danielk19774adee202004-05-08 08:23:19 +00001466 sqlite3VdbeAddOp(v, OP_OpenTemp, iParm, 0);
drh8cdbf832004-08-29 16:25:03 +00001467 assert( nAddr==0 );
1468 aAddr[nAddr++] = sqlite3VdbeAddOp(v, OP_SetNumColumns, iParm, 0);
drh1cc3d752002-03-23 00:31:29 +00001469 eDest = SRT_Table;
1470 }
1471
drhf46f9052002-06-22 02:33:38 +00001472 /* Generate code for the left and right SELECT statements.
drhd8bc7082000-06-07 23:51:50 +00001473 */
drh82c3d632000-06-06 21:56:07 +00001474 switch( p->op ){
drhf46f9052002-06-22 02:33:38 +00001475 case TK_ALL: {
1476 if( p->pOrderBy==0 ){
drh7b58dae2003-07-20 01:16:46 +00001477 pPrior->nLimit = p->nLimit;
1478 pPrior->nOffset = p->nOffset;
drh1398ad32005-01-19 23:24:50 +00001479 rc = sqlite3Select(pParse, pPrior, eDest, iParm, 0, 0, 0, aff, 0);
danielk197784ac9d02004-05-18 09:58:06 +00001480 if( rc ){
1481 goto multi_select_end;
1482 }
drhf46f9052002-06-22 02:33:38 +00001483 p->pPrior = 0;
drh7b58dae2003-07-20 01:16:46 +00001484 p->iLimit = pPrior->iLimit;
1485 p->iOffset = pPrior->iOffset;
1486 p->nLimit = -1;
1487 p->nOffset = 0;
drh1398ad32005-01-19 23:24:50 +00001488 rc = sqlite3Select(pParse, p, eDest, iParm, 0, 0, 0, aff, 0);
drhf46f9052002-06-22 02:33:38 +00001489 p->pPrior = pPrior;
danielk197784ac9d02004-05-18 09:58:06 +00001490 if( rc ){
1491 goto multi_select_end;
1492 }
drhf46f9052002-06-22 02:33:38 +00001493 break;
1494 }
1495 /* For UNION ALL ... ORDER BY fall through to the next case */
1496 }
drh82c3d632000-06-06 21:56:07 +00001497 case TK_EXCEPT:
1498 case TK_UNION: {
drhd8bc7082000-06-07 23:51:50 +00001499 int unionTab; /* Cursor number of the temporary table holding result */
danielk1977742f9472004-06-16 12:02:43 +00001500 int op = 0; /* One of the SRT_ operations to apply to self */
drhd8bc7082000-06-07 23:51:50 +00001501 int priorOp; /* The SRT_ operation to apply to prior selects */
drh7b58dae2003-07-20 01:16:46 +00001502 int nLimit, nOffset; /* Saved values of p->nLimit and p->nOffset */
drhc926afb2002-06-20 03:38:26 +00001503 ExprList *pOrderBy; /* The ORDER BY clause for the right SELECT */
danielk1977dc1bdc42004-06-11 10:51:27 +00001504 int addr;
drh82c3d632000-06-06 21:56:07 +00001505
drhd8bc7082000-06-07 23:51:50 +00001506 priorOp = p->op==TK_ALL ? SRT_Table : SRT_Union;
drh7b58dae2003-07-20 01:16:46 +00001507 if( eDest==priorOp && p->pOrderBy==0 && p->nLimit<0 && p->nOffset==0 ){
drhd8bc7082000-06-07 23:51:50 +00001508 /* We can reuse a temporary table generated by a SELECT to our
drhc926afb2002-06-20 03:38:26 +00001509 ** right.
drhd8bc7082000-06-07 23:51:50 +00001510 */
drh82c3d632000-06-06 21:56:07 +00001511 unionTab = iParm;
1512 }else{
drhd8bc7082000-06-07 23:51:50 +00001513 /* We will need to create our own temporary table to hold the
1514 ** intermediate results.
1515 */
1516 unionTab = pParse->nTab++;
1517 if( p->pOrderBy
1518 && matchOrderbyToColumn(pParse, p, p->pOrderBy, unionTab, 1) ){
danielk197784ac9d02004-05-18 09:58:06 +00001519 rc = 1;
1520 goto multi_select_end;
drhd8bc7082000-06-07 23:51:50 +00001521 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001522 addr = sqlite3VdbeAddOp(v, OP_OpenTemp, unionTab, 0);
drhd8bc7082000-06-07 23:51:50 +00001523 if( p->op!=TK_ALL ){
drh8cdbf832004-08-29 16:25:03 +00001524 rc = multiSelectOpenTempAddr(p, addr);
danielk1977dc1bdc42004-06-11 10:51:27 +00001525 if( rc!=SQLITE_OK ){
1526 goto multi_select_end;
1527 }
1528 sqlite3VdbeAddOp(v, OP_KeyAsData, unionTab, 1);
drhd8bc7082000-06-07 23:51:50 +00001529 }
drh8cdbf832004-08-29 16:25:03 +00001530 assert( nAddr<sizeof(aAddr)/sizeof(aAddr[0]) );
1531 aAddr[nAddr++] = sqlite3VdbeAddOp(v, OP_SetNumColumns, unionTab, 0);
danielk197784ac9d02004-05-18 09:58:06 +00001532 assert( p->pEList );
drh82c3d632000-06-06 21:56:07 +00001533 }
drhd8bc7082000-06-07 23:51:50 +00001534
1535 /* Code the SELECT statements to our left
1536 */
drh1398ad32005-01-19 23:24:50 +00001537 rc = sqlite3Select(pParse, pPrior, priorOp, unionTab, 0, 0, 0, aff, 0);
danielk197784ac9d02004-05-18 09:58:06 +00001538 if( rc ){
1539 goto multi_select_end;
1540 }
drhd8bc7082000-06-07 23:51:50 +00001541
1542 /* Code the current SELECT statement
1543 */
1544 switch( p->op ){
1545 case TK_EXCEPT: op = SRT_Except; break;
1546 case TK_UNION: op = SRT_Union; break;
1547 case TK_ALL: op = SRT_Table; break;
1548 }
drh82c3d632000-06-06 21:56:07 +00001549 p->pPrior = 0;
drhc926afb2002-06-20 03:38:26 +00001550 pOrderBy = p->pOrderBy;
1551 p->pOrderBy = 0;
drh7b58dae2003-07-20 01:16:46 +00001552 nLimit = p->nLimit;
1553 p->nLimit = -1;
1554 nOffset = p->nOffset;
1555 p->nOffset = 0;
drh1398ad32005-01-19 23:24:50 +00001556 rc = sqlite3Select(pParse, p, op, unionTab, 0, 0, 0, aff, 0);
drh82c3d632000-06-06 21:56:07 +00001557 p->pPrior = pPrior;
drhc926afb2002-06-20 03:38:26 +00001558 p->pOrderBy = pOrderBy;
drh7b58dae2003-07-20 01:16:46 +00001559 p->nLimit = nLimit;
1560 p->nOffset = nOffset;
drhbe5fd492004-12-16 21:09:16 +00001561 p->iLimit = -1;
1562 p->iOffset = -1;
danielk197784ac9d02004-05-18 09:58:06 +00001563 if( rc ){
1564 goto multi_select_end;
1565 }
1566
drhd8bc7082000-06-07 23:51:50 +00001567
1568 /* Convert the data in the temporary table into whatever form
1569 ** it is that we currently need.
1570 */
drhc926afb2002-06-20 03:38:26 +00001571 if( eDest!=priorOp || unionTab!=iParm ){
drh6b563442001-11-07 16:48:26 +00001572 int iCont, iBreak, iStart;
drh82c3d632000-06-06 21:56:07 +00001573 assert( p->pEList );
drh41202cc2002-04-23 17:10:18 +00001574 if( eDest==SRT_Callback ){
drh6a3ea0e2003-05-02 14:32:12 +00001575 generateColumnNames(pParse, 0, p->pEList);
drh41202cc2002-04-23 17:10:18 +00001576 }
danielk19774adee202004-05-08 08:23:19 +00001577 iBreak = sqlite3VdbeMakeLabel(v);
1578 iCont = sqlite3VdbeMakeLabel(v);
1579 sqlite3VdbeAddOp(v, OP_Rewind, unionTab, iBreak);
drh7b58dae2003-07-20 01:16:46 +00001580 computeLimitRegisters(pParse, p);
danielk19774adee202004-05-08 08:23:19 +00001581 iStart = sqlite3VdbeCurrentAddr(v);
drh38640e12002-07-05 21:42:36 +00001582 rc = selectInnerLoop(pParse, p, p->pEList, unionTab, p->pEList->nExpr,
drhd8bc7082000-06-07 23:51:50 +00001583 p->pOrderBy, -1, eDest, iParm,
danielk197784ac9d02004-05-18 09:58:06 +00001584 iCont, iBreak, 0);
1585 if( rc ){
1586 rc = 1;
1587 goto multi_select_end;
1588 }
danielk19774adee202004-05-08 08:23:19 +00001589 sqlite3VdbeResolveLabel(v, iCont);
1590 sqlite3VdbeAddOp(v, OP_Next, unionTab, iStart);
1591 sqlite3VdbeResolveLabel(v, iBreak);
1592 sqlite3VdbeAddOp(v, OP_Close, unionTab, 0);
drh82c3d632000-06-06 21:56:07 +00001593 }
1594 break;
1595 }
1596 case TK_INTERSECT: {
1597 int tab1, tab2;
drh6b563442001-11-07 16:48:26 +00001598 int iCont, iBreak, iStart;
drh7b58dae2003-07-20 01:16:46 +00001599 int nLimit, nOffset;
danielk1977dc1bdc42004-06-11 10:51:27 +00001600 int addr;
drh82c3d632000-06-06 21:56:07 +00001601
drhd8bc7082000-06-07 23:51:50 +00001602 /* INTERSECT is different from the others since it requires
drh6206d502000-06-19 19:09:08 +00001603 ** two temporary tables. Hence it has its own case. Begin
drhd8bc7082000-06-07 23:51:50 +00001604 ** by allocating the tables we will need.
1605 */
drh82c3d632000-06-06 21:56:07 +00001606 tab1 = pParse->nTab++;
1607 tab2 = pParse->nTab++;
drhd8bc7082000-06-07 23:51:50 +00001608 if( p->pOrderBy && matchOrderbyToColumn(pParse,p,p->pOrderBy,tab1,1) ){
danielk197784ac9d02004-05-18 09:58:06 +00001609 rc = 1;
1610 goto multi_select_end;
drhd8bc7082000-06-07 23:51:50 +00001611 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001612
1613 addr = sqlite3VdbeAddOp(v, OP_OpenTemp, tab1, 0);
drh8cdbf832004-08-29 16:25:03 +00001614 rc = multiSelectOpenTempAddr(p, addr);
danielk1977dc1bdc42004-06-11 10:51:27 +00001615 if( rc!=SQLITE_OK ){
1616 goto multi_select_end;
1617 }
1618 sqlite3VdbeAddOp(v, OP_KeyAsData, tab1, 1);
drh8cdbf832004-08-29 16:25:03 +00001619 assert( nAddr<sizeof(aAddr)/sizeof(aAddr[0]) );
1620 aAddr[nAddr++] = sqlite3VdbeAddOp(v, OP_SetNumColumns, tab1, 0);
danielk197784ac9d02004-05-18 09:58:06 +00001621 assert( p->pEList );
drhd8bc7082000-06-07 23:51:50 +00001622
1623 /* Code the SELECTs to our left into temporary table "tab1".
1624 */
drh1398ad32005-01-19 23:24:50 +00001625 rc = sqlite3Select(pParse, pPrior, SRT_Union, tab1, 0, 0, 0, aff, 0);
danielk197784ac9d02004-05-18 09:58:06 +00001626 if( rc ){
1627 goto multi_select_end;
1628 }
drhd8bc7082000-06-07 23:51:50 +00001629
1630 /* Code the current SELECT into temporary table "tab2"
1631 */
danielk1977dc1bdc42004-06-11 10:51:27 +00001632 addr = sqlite3VdbeAddOp(v, OP_OpenTemp, tab2, 0);
drh8cdbf832004-08-29 16:25:03 +00001633 rc = multiSelectOpenTempAddr(p, addr);
danielk1977dc1bdc42004-06-11 10:51:27 +00001634 if( rc!=SQLITE_OK ){
1635 goto multi_select_end;
1636 }
1637 sqlite3VdbeAddOp(v, OP_KeyAsData, tab2, 1);
drh8cdbf832004-08-29 16:25:03 +00001638 assert( nAddr<sizeof(aAddr)/sizeof(aAddr[0]) );
1639 aAddr[nAddr++] = sqlite3VdbeAddOp(v, OP_SetNumColumns, tab2, 0);
drh82c3d632000-06-06 21:56:07 +00001640 p->pPrior = 0;
drh7b58dae2003-07-20 01:16:46 +00001641 nLimit = p->nLimit;
1642 p->nLimit = -1;
1643 nOffset = p->nOffset;
1644 p->nOffset = 0;
drh1398ad32005-01-19 23:24:50 +00001645 rc = sqlite3Select(pParse, p, SRT_Union, tab2, 0, 0, 0, aff, 0);
drh82c3d632000-06-06 21:56:07 +00001646 p->pPrior = pPrior;
drh7b58dae2003-07-20 01:16:46 +00001647 p->nLimit = nLimit;
1648 p->nOffset = nOffset;
danielk197784ac9d02004-05-18 09:58:06 +00001649 if( rc ){
1650 goto multi_select_end;
1651 }
drhd8bc7082000-06-07 23:51:50 +00001652
1653 /* Generate code to take the intersection of the two temporary
1654 ** tables.
1655 */
drh82c3d632000-06-06 21:56:07 +00001656 assert( p->pEList );
drh41202cc2002-04-23 17:10:18 +00001657 if( eDest==SRT_Callback ){
drh6a3ea0e2003-05-02 14:32:12 +00001658 generateColumnNames(pParse, 0, p->pEList);
drh41202cc2002-04-23 17:10:18 +00001659 }
danielk19774adee202004-05-08 08:23:19 +00001660 iBreak = sqlite3VdbeMakeLabel(v);
1661 iCont = sqlite3VdbeMakeLabel(v);
1662 sqlite3VdbeAddOp(v, OP_Rewind, tab1, iBreak);
drh7b58dae2003-07-20 01:16:46 +00001663 computeLimitRegisters(pParse, p);
danielk19774adee202004-05-08 08:23:19 +00001664 iStart = sqlite3VdbeAddOp(v, OP_FullKey, tab1, 0);
1665 sqlite3VdbeAddOp(v, OP_NotFound, tab2, iCont);
drh38640e12002-07-05 21:42:36 +00001666 rc = selectInnerLoop(pParse, p, p->pEList, tab1, p->pEList->nExpr,
drhd8bc7082000-06-07 23:51:50 +00001667 p->pOrderBy, -1, eDest, iParm,
danielk197784ac9d02004-05-18 09:58:06 +00001668 iCont, iBreak, 0);
1669 if( rc ){
1670 rc = 1;
1671 goto multi_select_end;
1672 }
danielk19774adee202004-05-08 08:23:19 +00001673 sqlite3VdbeResolveLabel(v, iCont);
1674 sqlite3VdbeAddOp(v, OP_Next, tab1, iStart);
1675 sqlite3VdbeResolveLabel(v, iBreak);
1676 sqlite3VdbeAddOp(v, OP_Close, tab2, 0);
1677 sqlite3VdbeAddOp(v, OP_Close, tab1, 0);
drh82c3d632000-06-06 21:56:07 +00001678 break;
1679 }
1680 }
drh8cdbf832004-08-29 16:25:03 +00001681
1682 /* Make sure all SELECTs in the statement have the same number of elements
1683 ** in their result sets.
1684 */
drh82c3d632000-06-06 21:56:07 +00001685 assert( p->pEList && pPrior->pEList );
1686 if( p->pEList->nExpr!=pPrior->pEList->nExpr ){
danielk19774adee202004-05-08 08:23:19 +00001687 sqlite3ErrorMsg(pParse, "SELECTs to the left and right of %s"
drhda93d232003-03-31 02:12:46 +00001688 " do not have the same number of result columns", selectOpName(p->op));
danielk197784ac9d02004-05-18 09:58:06 +00001689 rc = 1;
1690 goto multi_select_end;
drh22827922000-06-06 17:27:05 +00001691 }
danielk197784ac9d02004-05-18 09:58:06 +00001692
drh8cdbf832004-08-29 16:25:03 +00001693 /* Set the number of columns in temporary tables
1694 */
1695 nCol = p->pEList->nExpr;
1696 while( nAddr>0 ){
1697 nAddr--;
1698 sqlite3VdbeChangeP2(v, aAddr[nAddr], nCol);
1699 }
1700
drhfbc4ee72004-08-29 01:31:05 +00001701 /* Compute collating sequences used by either the ORDER BY clause or
1702 ** by any temporary tables needed to implement the compound select.
1703 ** Attach the KeyInfo structure to all temporary tables. Invoke the
1704 ** ORDER BY processing if there is an ORDER BY clause.
drh8cdbf832004-08-29 16:25:03 +00001705 **
1706 ** This section is run by the right-most SELECT statement only.
1707 ** SELECT statements to the left always skip this part. The right-most
1708 ** SELECT might also skip this part if it has no ORDER BY clause and
1709 ** no temp tables are required.
drhfbc4ee72004-08-29 01:31:05 +00001710 */
danielk1977dc1bdc42004-06-11 10:51:27 +00001711 if( p->pOrderBy || (pOpenTemp && pOpenTemp->nId>0) ){
drhfbc4ee72004-08-29 01:31:05 +00001712 int i; /* Loop counter */
1713 KeyInfo *pKeyInfo; /* Collating sequence for the result set */
1714
drh8cdbf832004-08-29 16:25:03 +00001715 assert( p->ppOpenTemp == &pOpenTemp );
drhfbc4ee72004-08-29 01:31:05 +00001716 pKeyInfo = sqliteMalloc(sizeof(*pKeyInfo)+nCol*sizeof(CollSeq*));
danielk1977dc1bdc42004-06-11 10:51:27 +00001717 if( !pKeyInfo ){
1718 rc = SQLITE_NOMEM;
1719 goto multi_select_end;
1720 }
1721
1722 pKeyInfo->enc = pParse->db->enc;
1723 pKeyInfo->nField = nCol;
1724
1725 for(i=0; i<nCol; i++){
1726 pKeyInfo->aColl[i] = multiSelectCollSeq(pParse, p, i);
1727 if( !pKeyInfo->aColl[i] ){
1728 pKeyInfo->aColl[i] = pParse->db->pDfltColl;
1729 }
1730 }
1731
1732 for(i=0; pOpenTemp && i<pOpenTemp->nId; i++){
1733 int p3type = (i==0?P3_KEYINFO_HANDOFF:P3_KEYINFO);
1734 int addr = pOpenTemp->a[i].idx;
1735 sqlite3VdbeChangeP3(v, addr, (char *)pKeyInfo, p3type);
1736 }
1737
1738 if( p->pOrderBy ){
drhfbc4ee72004-08-29 01:31:05 +00001739 struct ExprList_item *pOrderByTerm = p->pOrderBy->a;
1740 for(i=0; i<p->pOrderBy->nExpr; i++, pOrderByTerm++){
1741 Expr *pExpr = pOrderByTerm->pExpr;
1742 char *zName = pOrderByTerm->zName;
danielk1977dc1bdc42004-06-11 10:51:27 +00001743 assert( pExpr->op==TK_COLUMN && pExpr->iColumn<nCol );
1744 assert( !pExpr->pColl );
1745 if( zName ){
1746 pExpr->pColl = sqlite3LocateCollSeq(pParse, zName, -1);
1747 }else{
1748 pExpr->pColl = pKeyInfo->aColl[pExpr->iColumn];
1749 }
1750 }
1751 generateSortTail(pParse, p, v, p->pEList->nExpr, eDest, iParm);
1752 }
1753
1754 if( !pOpenTemp ){
1755 /* This happens for UNION ALL ... ORDER BY */
1756 sqliteFree(pKeyInfo);
danielk197784ac9d02004-05-18 09:58:06 +00001757 }
1758 }
danielk1977dc1bdc42004-06-11 10:51:27 +00001759
1760multi_select_end:
1761 if( pOpenTemp ){
1762 sqlite3IdListDelete(pOpenTemp);
1763 }
1764 p->ppOpenTemp = 0;
danielk197784ac9d02004-05-18 09:58:06 +00001765 return rc;
drh22827922000-06-06 17:27:05 +00001766}
drhb7f91642004-10-31 02:22:47 +00001767#endif /* SQLITE_OMIT_COMPOUND_SELECT */
drh22827922000-06-06 17:27:05 +00001768
drhb7f91642004-10-31 02:22:47 +00001769#ifndef SQLITE_OMIT_VIEW
drh22827922000-06-06 17:27:05 +00001770/*
drh832508b2002-03-02 17:04:07 +00001771** Scan through the expression pExpr. Replace every reference to
drh6a3ea0e2003-05-02 14:32:12 +00001772** a column in table number iTable with a copy of the iColumn-th
drh84e59202002-03-14 14:33:31 +00001773** entry in pEList. (But leave references to the ROWID column
drh6a3ea0e2003-05-02 14:32:12 +00001774** unchanged.)
drh832508b2002-03-02 17:04:07 +00001775**
1776** This routine is part of the flattening procedure. A subquery
1777** whose result set is defined by pEList appears as entry in the
1778** FROM clause of a SELECT such that the VDBE cursor assigned to that
1779** FORM clause entry is iTable. This routine make the necessary
1780** changes to pExpr so that it refers directly to the source table
1781** of the subquery rather the result set of the subquery.
1782*/
drh6a3ea0e2003-05-02 14:32:12 +00001783static void substExprList(ExprList*,int,ExprList*); /* Forward Decl */
1784static void substExpr(Expr *pExpr, int iTable, ExprList *pEList){
drh832508b2002-03-02 17:04:07 +00001785 if( pExpr==0 ) return;
drh50350a12004-02-13 16:22:22 +00001786 if( pExpr->op==TK_COLUMN && pExpr->iTable==iTable ){
1787 if( pExpr->iColumn<0 ){
1788 pExpr->op = TK_NULL;
1789 }else{
1790 Expr *pNew;
1791 assert( pEList!=0 && pExpr->iColumn<pEList->nExpr );
1792 assert( pExpr->pLeft==0 && pExpr->pRight==0 && pExpr->pList==0 );
1793 pNew = pEList->a[pExpr->iColumn].pExpr;
1794 assert( pNew!=0 );
1795 pExpr->op = pNew->op;
drh50350a12004-02-13 16:22:22 +00001796 assert( pExpr->pLeft==0 );
danielk19774adee202004-05-08 08:23:19 +00001797 pExpr->pLeft = sqlite3ExprDup(pNew->pLeft);
drh50350a12004-02-13 16:22:22 +00001798 assert( pExpr->pRight==0 );
danielk19774adee202004-05-08 08:23:19 +00001799 pExpr->pRight = sqlite3ExprDup(pNew->pRight);
drh50350a12004-02-13 16:22:22 +00001800 assert( pExpr->pList==0 );
danielk19774adee202004-05-08 08:23:19 +00001801 pExpr->pList = sqlite3ExprListDup(pNew->pList);
drh50350a12004-02-13 16:22:22 +00001802 pExpr->iTable = pNew->iTable;
1803 pExpr->iColumn = pNew->iColumn;
1804 pExpr->iAgg = pNew->iAgg;
danielk19774adee202004-05-08 08:23:19 +00001805 sqlite3TokenCopy(&pExpr->token, &pNew->token);
1806 sqlite3TokenCopy(&pExpr->span, &pNew->span);
drh50350a12004-02-13 16:22:22 +00001807 }
drh832508b2002-03-02 17:04:07 +00001808 }else{
drh6a3ea0e2003-05-02 14:32:12 +00001809 substExpr(pExpr->pLeft, iTable, pEList);
1810 substExpr(pExpr->pRight, iTable, pEList);
1811 substExprList(pExpr->pList, iTable, pEList);
drh832508b2002-03-02 17:04:07 +00001812 }
1813}
1814static void
drh6a3ea0e2003-05-02 14:32:12 +00001815substExprList(ExprList *pList, int iTable, ExprList *pEList){
drh832508b2002-03-02 17:04:07 +00001816 int i;
1817 if( pList==0 ) return;
1818 for(i=0; i<pList->nExpr; i++){
drh6a3ea0e2003-05-02 14:32:12 +00001819 substExpr(pList->a[i].pExpr, iTable, pEList);
drh832508b2002-03-02 17:04:07 +00001820 }
1821}
drhb7f91642004-10-31 02:22:47 +00001822#endif /* !defined(SQLITE_OMIT_VIEW) */
drh832508b2002-03-02 17:04:07 +00001823
drhb7f91642004-10-31 02:22:47 +00001824#ifndef SQLITE_OMIT_VIEW
drh832508b2002-03-02 17:04:07 +00001825/*
drh1350b032002-02-27 19:00:20 +00001826** This routine attempts to flatten subqueries in order to speed
1827** execution. It returns 1 if it makes changes and 0 if no flattening
1828** occurs.
1829**
1830** To understand the concept of flattening, consider the following
1831** query:
1832**
1833** SELECT a FROM (SELECT x+y AS a FROM t1 WHERE z<100) WHERE a>5
1834**
1835** The default way of implementing this query is to execute the
1836** subquery first and store the results in a temporary table, then
1837** run the outer query on that temporary table. This requires two
1838** passes over the data. Furthermore, because the temporary table
1839** has no indices, the WHERE clause on the outer query cannot be
drh832508b2002-03-02 17:04:07 +00001840** optimized.
drh1350b032002-02-27 19:00:20 +00001841**
drh832508b2002-03-02 17:04:07 +00001842** This routine attempts to rewrite queries such as the above into
drh1350b032002-02-27 19:00:20 +00001843** a single flat select, like this:
1844**
1845** SELECT x+y AS a FROM t1 WHERE z<100 AND a>5
1846**
1847** The code generated for this simpification gives the same result
drh832508b2002-03-02 17:04:07 +00001848** but only has to scan the data once. And because indices might
1849** exist on the table t1, a complete scan of the data might be
1850** avoided.
drh1350b032002-02-27 19:00:20 +00001851**
drh832508b2002-03-02 17:04:07 +00001852** Flattening is only attempted if all of the following are true:
drh1350b032002-02-27 19:00:20 +00001853**
drh832508b2002-03-02 17:04:07 +00001854** (1) The subquery and the outer query do not both use aggregates.
drh1350b032002-02-27 19:00:20 +00001855**
drh832508b2002-03-02 17:04:07 +00001856** (2) The subquery is not an aggregate or the outer query is not a join.
1857**
drh8af4d3a2003-05-06 20:35:16 +00001858** (3) The subquery is not the right operand of a left outer join, or
1859** the subquery is not itself a join. (Ticket #306)
drh832508b2002-03-02 17:04:07 +00001860**
1861** (4) The subquery is not DISTINCT or the outer query is not a join.
1862**
1863** (5) The subquery is not DISTINCT or the outer query does not use
1864** aggregates.
1865**
1866** (6) The subquery does not use aggregates or the outer query is not
1867** DISTINCT.
1868**
drh08192d52002-04-30 19:20:28 +00001869** (7) The subquery has a FROM clause.
1870**
drhdf199a22002-06-14 22:38:41 +00001871** (8) The subquery does not use LIMIT or the outer query is not a join.
1872**
1873** (9) The subquery does not use LIMIT or the outer query does not use
1874** aggregates.
1875**
1876** (10) The subquery does not use aggregates or the outer query does not
1877** use LIMIT.
1878**
drh174b6192002-12-03 02:22:52 +00001879** (11) The subquery and the outer query do not both have ORDER BY clauses.
1880**
drh3fc673e2003-06-16 00:40:34 +00001881** (12) The subquery is not the right term of a LEFT OUTER JOIN or the
1882** subquery has no WHERE clause. (added by ticket #350)
1883**
drh832508b2002-03-02 17:04:07 +00001884** In this routine, the "p" parameter is a pointer to the outer query.
1885** The subquery is p->pSrc->a[iFrom]. isAgg is true if the outer query
1886** uses aggregates and subqueryIsAgg is true if the subquery uses aggregates.
1887**
drh665de472003-03-31 13:36:09 +00001888** If flattening is not attempted, this routine is a no-op and returns 0.
drh832508b2002-03-02 17:04:07 +00001889** If flattening is attempted this routine returns 1.
1890**
1891** All of the expression analysis must occur on both the outer query and
1892** the subquery before this routine runs.
drh1350b032002-02-27 19:00:20 +00001893*/
drh8c74a8c2002-08-25 19:20:40 +00001894static int flattenSubquery(
1895 Parse *pParse, /* The parsing context */
1896 Select *p, /* The parent or outer SELECT statement */
1897 int iFrom, /* Index in p->pSrc->a[] of the inner subquery */
1898 int isAgg, /* True if outer SELECT uses aggregate functions */
1899 int subqueryIsAgg /* True if the subquery uses aggregate functions */
1900){
drh0bb28102002-05-08 11:54:14 +00001901 Select *pSub; /* The inner query or "subquery" */
drhad3cab52002-05-24 02:04:32 +00001902 SrcList *pSrc; /* The FROM clause of the outer query */
1903 SrcList *pSubSrc; /* The FROM clause of the subquery */
drh0bb28102002-05-08 11:54:14 +00001904 ExprList *pList; /* The result set of the outer query */
drh6a3ea0e2003-05-02 14:32:12 +00001905 int iParent; /* VDBE cursor number of the pSub result set temp table */
drh91bb0ee2004-09-01 03:06:34 +00001906 int i; /* Loop counter */
1907 Expr *pWhere; /* The WHERE clause */
1908 struct SrcList_item *pSubitem; /* The subquery */
drh1350b032002-02-27 19:00:20 +00001909
drh832508b2002-03-02 17:04:07 +00001910 /* Check to see if flattening is permitted. Return 0 if not.
1911 */
1912 if( p==0 ) return 0;
1913 pSrc = p->pSrc;
drhad3cab52002-05-24 02:04:32 +00001914 assert( pSrc && iFrom>=0 && iFrom<pSrc->nSrc );
drh91bb0ee2004-09-01 03:06:34 +00001915 pSubitem = &pSrc->a[iFrom];
1916 pSub = pSubitem->pSelect;
drh832508b2002-03-02 17:04:07 +00001917 assert( pSub!=0 );
1918 if( isAgg && subqueryIsAgg ) return 0;
drhad3cab52002-05-24 02:04:32 +00001919 if( subqueryIsAgg && pSrc->nSrc>1 ) return 0;
drh832508b2002-03-02 17:04:07 +00001920 pSubSrc = pSub->pSrc;
1921 assert( pSubSrc );
drhc31c2eb2003-05-02 16:04:17 +00001922 if( pSubSrc->nSrc==0 ) return 0;
drhdf199a22002-06-14 22:38:41 +00001923 if( (pSub->isDistinct || pSub->nLimit>=0) && (pSrc->nSrc>1 || isAgg) ){
1924 return 0;
1925 }
drhd11d3822002-06-21 23:01:49 +00001926 if( (p->isDistinct || p->nLimit>=0) && subqueryIsAgg ) return 0;
drh174b6192002-12-03 02:22:52 +00001927 if( p->pOrderBy && pSub->pOrderBy ) return 0;
drh832508b2002-03-02 17:04:07 +00001928
drh8af4d3a2003-05-06 20:35:16 +00001929 /* Restriction 3: If the subquery is a join, make sure the subquery is
1930 ** not used as the right operand of an outer join. Examples of why this
1931 ** is not allowed:
1932 **
1933 ** t1 LEFT OUTER JOIN (t2 JOIN t3)
1934 **
1935 ** If we flatten the above, we would get
1936 **
1937 ** (t1 LEFT OUTER JOIN t2) JOIN t3
1938 **
1939 ** which is not at all the same thing.
1940 */
1941 if( pSubSrc->nSrc>1 && iFrom>0 && (pSrc->a[iFrom-1].jointype & JT_OUTER)!=0 ){
1942 return 0;
1943 }
1944
drh3fc673e2003-06-16 00:40:34 +00001945 /* Restriction 12: If the subquery is the right operand of a left outer
1946 ** join, make sure the subquery has no WHERE clause.
1947 ** An examples of why this is not allowed:
1948 **
1949 ** t1 LEFT OUTER JOIN (SELECT * FROM t2 WHERE t2.x>0)
1950 **
1951 ** If we flatten the above, we would get
1952 **
1953 ** (t1 LEFT OUTER JOIN t2) WHERE t2.x>0
1954 **
1955 ** But the t2.x>0 test will always fail on a NULL row of t2, which
1956 ** effectively converts the OUTER JOIN into an INNER JOIN.
1957 */
1958 if( iFrom>0 && (pSrc->a[iFrom-1].jointype & JT_OUTER)!=0
1959 && pSub->pWhere!=0 ){
1960 return 0;
1961 }
1962
drh0bb28102002-05-08 11:54:14 +00001963 /* If we reach this point, it means flattening is permitted for the
drh63eb5f22003-04-29 16:20:44 +00001964 ** iFrom-th entry of the FROM clause in the outer query.
drh832508b2002-03-02 17:04:07 +00001965 */
drhc31c2eb2003-05-02 16:04:17 +00001966
1967 /* Move all of the FROM elements of the subquery into the
1968 ** the FROM clause of the outer query. Before doing this, remember
1969 ** the cursor number for the original outer query FROM element in
1970 ** iParent. The iParent cursor will never be used. Subsequent code
1971 ** will scan expressions looking for iParent references and replace
1972 ** those references with expressions that resolve to the subquery FROM
1973 ** elements we are now copying in.
1974 */
drh91bb0ee2004-09-01 03:06:34 +00001975 iParent = pSubitem->iCursor;
drhc31c2eb2003-05-02 16:04:17 +00001976 {
1977 int nSubSrc = pSubSrc->nSrc;
drh91bb0ee2004-09-01 03:06:34 +00001978 int jointype = pSubitem->jointype;
1979 Table *pTab = pSubitem->pTab;
drhc31c2eb2003-05-02 16:04:17 +00001980
drh91bb0ee2004-09-01 03:06:34 +00001981 if( pTab && pTab->isTransient ){
1982 sqlite3DeleteTable(0, pSubitem->pTab);
drhc31c2eb2003-05-02 16:04:17 +00001983 }
drh91bb0ee2004-09-01 03:06:34 +00001984 sqliteFree(pSubitem->zDatabase);
1985 sqliteFree(pSubitem->zName);
1986 sqliteFree(pSubitem->zAlias);
drhc31c2eb2003-05-02 16:04:17 +00001987 if( nSubSrc>1 ){
1988 int extra = nSubSrc - 1;
1989 for(i=1; i<nSubSrc; i++){
danielk19774adee202004-05-08 08:23:19 +00001990 pSrc = sqlite3SrcListAppend(pSrc, 0, 0);
drhc31c2eb2003-05-02 16:04:17 +00001991 }
1992 p->pSrc = pSrc;
1993 for(i=pSrc->nSrc-1; i-extra>=iFrom; i--){
1994 pSrc->a[i] = pSrc->a[i-extra];
1995 }
1996 }
1997 for(i=0; i<nSubSrc; i++){
1998 pSrc->a[i+iFrom] = pSubSrc->a[i];
1999 memset(&pSubSrc->a[i], 0, sizeof(pSubSrc->a[i]));
2000 }
drh8af4d3a2003-05-06 20:35:16 +00002001 pSrc->a[iFrom+nSubSrc-1].jointype = jointype;
drhc31c2eb2003-05-02 16:04:17 +00002002 }
2003
2004 /* Now begin substituting subquery result set expressions for
2005 ** references to the iParent in the outer query.
2006 **
2007 ** Example:
2008 **
2009 ** SELECT a+5, b*10 FROM (SELECT x*3 AS a, y+10 AS b FROM t1) WHERE a>b;
2010 ** \ \_____________ subquery __________/ /
2011 ** \_____________________ outer query ______________________________/
2012 **
2013 ** We look at every expression in the outer query and every place we see
2014 ** "a" we substitute "x*3" and every place we see "b" we substitute "y+10".
2015 */
drh6a3ea0e2003-05-02 14:32:12 +00002016 substExprList(p->pEList, iParent, pSub->pEList);
drh832508b2002-03-02 17:04:07 +00002017 pList = p->pEList;
2018 for(i=0; i<pList->nExpr; i++){
drh6977fea2002-10-22 23:38:04 +00002019 Expr *pExpr;
2020 if( pList->a[i].zName==0 && (pExpr = pList->a[i].pExpr)->span.z!=0 ){
2021 pList->a[i].zName = sqliteStrNDup(pExpr->span.z, pExpr->span.n);
drh832508b2002-03-02 17:04:07 +00002022 }
2023 }
drh1b2e0322002-03-03 02:49:51 +00002024 if( isAgg ){
drh6a3ea0e2003-05-02 14:32:12 +00002025 substExprList(p->pGroupBy, iParent, pSub->pEList);
2026 substExpr(p->pHaving, iParent, pSub->pEList);
drh1b2e0322002-03-03 02:49:51 +00002027 }
drh174b6192002-12-03 02:22:52 +00002028 if( pSub->pOrderBy ){
2029 assert( p->pOrderBy==0 );
2030 p->pOrderBy = pSub->pOrderBy;
2031 pSub->pOrderBy = 0;
drh174b6192002-12-03 02:22:52 +00002032 }else if( p->pOrderBy ){
drh6a3ea0e2003-05-02 14:32:12 +00002033 substExprList(p->pOrderBy, iParent, pSub->pEList);
drh174b6192002-12-03 02:22:52 +00002034 }
drh832508b2002-03-02 17:04:07 +00002035 if( pSub->pWhere ){
danielk19774adee202004-05-08 08:23:19 +00002036 pWhere = sqlite3ExprDup(pSub->pWhere);
drh832508b2002-03-02 17:04:07 +00002037 }else{
2038 pWhere = 0;
2039 }
2040 if( subqueryIsAgg ){
2041 assert( p->pHaving==0 );
drh1b2e0322002-03-03 02:49:51 +00002042 p->pHaving = p->pWhere;
2043 p->pWhere = pWhere;
drh6a3ea0e2003-05-02 14:32:12 +00002044 substExpr(p->pHaving, iParent, pSub->pEList);
drh91bb0ee2004-09-01 03:06:34 +00002045 p->pHaving = sqlite3ExprAnd(p->pHaving, sqlite3ExprDup(pSub->pHaving));
drh1b2e0322002-03-03 02:49:51 +00002046 assert( p->pGroupBy==0 );
danielk19774adee202004-05-08 08:23:19 +00002047 p->pGroupBy = sqlite3ExprListDup(pSub->pGroupBy);
drh832508b2002-03-02 17:04:07 +00002048 }else{
drh6a3ea0e2003-05-02 14:32:12 +00002049 substExpr(p->pWhere, iParent, pSub->pEList);
drh91bb0ee2004-09-01 03:06:34 +00002050 p->pWhere = sqlite3ExprAnd(p->pWhere, pWhere);
drh832508b2002-03-02 17:04:07 +00002051 }
drhc31c2eb2003-05-02 16:04:17 +00002052
2053 /* The flattened query is distinct if either the inner or the
2054 ** outer query is distinct.
2055 */
drh832508b2002-03-02 17:04:07 +00002056 p->isDistinct = p->isDistinct || pSub->isDistinct;
drh8c74a8c2002-08-25 19:20:40 +00002057
drhc31c2eb2003-05-02 16:04:17 +00002058 /* Transfer the limit expression from the subquery to the outer
2059 ** query.
2060 */
drhdf199a22002-06-14 22:38:41 +00002061 if( pSub->nLimit>=0 ){
2062 if( p->nLimit<0 ){
2063 p->nLimit = pSub->nLimit;
2064 }else if( p->nLimit+p->nOffset > pSub->nLimit+pSub->nOffset ){
2065 p->nLimit = pSub->nLimit + pSub->nOffset - p->nOffset;
2066 }
2067 }
2068 p->nOffset += pSub->nOffset;
drh8c74a8c2002-08-25 19:20:40 +00002069
drhc31c2eb2003-05-02 16:04:17 +00002070 /* Finially, delete what is left of the subquery and return
2071 ** success.
2072 */
danielk19774adee202004-05-08 08:23:19 +00002073 sqlite3SelectDelete(pSub);
drh832508b2002-03-02 17:04:07 +00002074 return 1;
2075}
drhb7f91642004-10-31 02:22:47 +00002076#endif /* SQLITE_OMIT_VIEW */
drh1350b032002-02-27 19:00:20 +00002077
2078/*
drh9562b552002-02-19 15:00:07 +00002079** Analyze the SELECT statement passed in as an argument to see if it
2080** is a simple min() or max() query. If it is and this query can be
2081** satisfied using a single seek to the beginning or end of an index,
drhe78e8282003-01-19 03:59:45 +00002082** then generate the code for this SELECT and return 1. If this is not a
drh9562b552002-02-19 15:00:07 +00002083** simple min() or max() query, then return 0;
2084**
2085** A simply min() or max() query looks like this:
2086**
2087** SELECT min(a) FROM table;
2088** SELECT max(a) FROM table;
2089**
2090** The query may have only a single table in its FROM argument. There
2091** can be no GROUP BY or HAVING or WHERE clauses. The result set must
2092** be the min() or max() of a single column of the table. The column
2093** in the min() or max() function must be indexed.
2094**
danielk19774adee202004-05-08 08:23:19 +00002095** The parameters to this routine are the same as for sqlite3Select().
drh9562b552002-02-19 15:00:07 +00002096** See the header comment on that routine for additional information.
2097*/
2098static int simpleMinMaxQuery(Parse *pParse, Select *p, int eDest, int iParm){
2099 Expr *pExpr;
2100 int iCol;
2101 Table *pTab;
2102 Index *pIdx;
2103 int base;
2104 Vdbe *v;
drh9562b552002-02-19 15:00:07 +00002105 int seekOp;
2106 int cont;
drh6e175292004-03-13 14:00:36 +00002107 ExprList *pEList, *pList, eList;
drh9562b552002-02-19 15:00:07 +00002108 struct ExprList_item eListItem;
drh6e175292004-03-13 14:00:36 +00002109 SrcList *pSrc;
drh9562b552002-02-19 15:00:07 +00002110
2111 /* Check to see if this query is a simple min() or max() query. Return
2112 ** zero if it is not.
2113 */
2114 if( p->pGroupBy || p->pHaving || p->pWhere ) return 0;
drh6e175292004-03-13 14:00:36 +00002115 pSrc = p->pSrc;
2116 if( pSrc->nSrc!=1 ) return 0;
2117 pEList = p->pEList;
2118 if( pEList->nExpr!=1 ) return 0;
2119 pExpr = pEList->a[0].pExpr;
drh9562b552002-02-19 15:00:07 +00002120 if( pExpr->op!=TK_AGG_FUNCTION ) return 0;
drh6e175292004-03-13 14:00:36 +00002121 pList = pExpr->pList;
2122 if( pList==0 || pList->nExpr!=1 ) return 0;
drh6977fea2002-10-22 23:38:04 +00002123 if( pExpr->token.n!=3 ) return 0;
danielk19774adee202004-05-08 08:23:19 +00002124 if( sqlite3StrNICmp(pExpr->token.z,"min",3)==0 ){
drh0bce8352002-02-28 00:41:10 +00002125 seekOp = OP_Rewind;
danielk19774adee202004-05-08 08:23:19 +00002126 }else if( sqlite3StrNICmp(pExpr->token.z,"max",3)==0 ){
drh0bce8352002-02-28 00:41:10 +00002127 seekOp = OP_Last;
2128 }else{
2129 return 0;
2130 }
drh6e175292004-03-13 14:00:36 +00002131 pExpr = pList->a[0].pExpr;
drh9562b552002-02-19 15:00:07 +00002132 if( pExpr->op!=TK_COLUMN ) return 0;
2133 iCol = pExpr->iColumn;
drh6e175292004-03-13 14:00:36 +00002134 pTab = pSrc->a[0].pTab;
drh9562b552002-02-19 15:00:07 +00002135
2136 /* If we get to here, it means the query is of the correct form.
drh17f71932002-02-21 12:01:27 +00002137 ** Check to make sure we have an index and make pIdx point to the
2138 ** appropriate index. If the min() or max() is on an INTEGER PRIMARY
2139 ** key column, no index is necessary so set pIdx to NULL. If no
2140 ** usable index is found, return 0.
drh9562b552002-02-19 15:00:07 +00002141 */
2142 if( iCol<0 ){
2143 pIdx = 0;
2144 }else{
danielk1977dc1bdc42004-06-11 10:51:27 +00002145 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pExpr);
drh9562b552002-02-19 15:00:07 +00002146 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
2147 assert( pIdx->nColumn>=1 );
danielk1977dc1bdc42004-06-11 10:51:27 +00002148 if( pIdx->aiColumn[0]==iCol && pIdx->keyInfo.aColl[0]==pColl ) break;
drh9562b552002-02-19 15:00:07 +00002149 }
2150 if( pIdx==0 ) return 0;
2151 }
2152
drhe5f50722003-07-19 00:44:14 +00002153 /* Identify column types if we will be using the callback. This
drh9562b552002-02-19 15:00:07 +00002154 ** step is skipped if the output is going to a table or a memory cell.
drhe5f50722003-07-19 00:44:14 +00002155 ** The column names have already been generated in the calling function.
drh9562b552002-02-19 15:00:07 +00002156 */
danielk19774adee202004-05-08 08:23:19 +00002157 v = sqlite3GetVdbe(pParse);
drh9562b552002-02-19 15:00:07 +00002158 if( v==0 ) return 0;
drh9562b552002-02-19 15:00:07 +00002159
drh0c37e632004-01-30 02:01:03 +00002160 /* If the output is destined for a temporary table, open that table.
2161 */
2162 if( eDest==SRT_TempTable ){
danielk19774adee202004-05-08 08:23:19 +00002163 sqlite3VdbeAddOp(v, OP_OpenTemp, iParm, 0);
danielk1977b4964b72004-05-18 01:23:38 +00002164 sqlite3VdbeAddOp(v, OP_SetNumColumns, iParm, 1);
drh0c37e632004-01-30 02:01:03 +00002165 }
2166
drh17f71932002-02-21 12:01:27 +00002167 /* Generating code to find the min or the max. Basically all we have
2168 ** to do is find the first or the last entry in the chosen index. If
2169 ** the min() or max() is on the INTEGER PRIMARY KEY, then find the first
2170 ** or last entry in the main table.
drh9562b552002-02-19 15:00:07 +00002171 */
danielk19774adee202004-05-08 08:23:19 +00002172 sqlite3CodeVerifySchema(pParse, pTab->iDb);
drh6e175292004-03-13 14:00:36 +00002173 base = pSrc->a[0].iCursor;
drh7b58dae2003-07-20 01:16:46 +00002174 computeLimitRegisters(pParse, p);
drh6e175292004-03-13 14:00:36 +00002175 if( pSrc->a[0].pSelect==0 ){
drhad6d9462004-09-19 02:15:24 +00002176 sqlite3OpenTableForReading(v, base, pTab);
drh6e175292004-03-13 14:00:36 +00002177 }
danielk19774adee202004-05-08 08:23:19 +00002178 cont = sqlite3VdbeMakeLabel(v);
drh9562b552002-02-19 15:00:07 +00002179 if( pIdx==0 ){
danielk19774adee202004-05-08 08:23:19 +00002180 sqlite3VdbeAddOp(v, seekOp, base, 0);
drh9562b552002-02-19 15:00:07 +00002181 }else{
danielk19773719d7f2005-01-17 08:57:09 +00002182 /* Even though the cursor used to open the index here is closed
2183 ** as soon as a single value has been read from it, allocate it
2184 ** using (pParse->nTab++) to prevent the cursor id from being
2185 ** reused. This is important for statements of the form
2186 ** "INSERT INTO x SELECT max() FROM x".
2187 */
2188 int iIdx;
2189 iIdx = pParse->nTab++;
danielk19774adee202004-05-08 08:23:19 +00002190 sqlite3VdbeAddOp(v, OP_Integer, pIdx->iDb, 0);
danielk19773719d7f2005-01-17 08:57:09 +00002191 sqlite3VdbeOp3(v, OP_OpenRead, iIdx, pIdx->tnum,
drhd3d39e92004-05-20 22:16:29 +00002192 (char*)&pIdx->keyInfo, P3_KEYINFO);
drh9eb516c2004-07-18 20:52:32 +00002193 if( seekOp==OP_Rewind ){
drh1af3fdb2004-07-18 21:33:01 +00002194 sqlite3VdbeAddOp(v, OP_String, 0, 0);
2195 sqlite3VdbeAddOp(v, OP_MakeRecord, 1, 0);
2196 seekOp = OP_MoveGt;
drh9eb516c2004-07-18 20:52:32 +00002197 }
danielk19773719d7f2005-01-17 08:57:09 +00002198 sqlite3VdbeAddOp(v, seekOp, iIdx, 0);
2199 sqlite3VdbeAddOp(v, OP_IdxRecno, iIdx, 0);
2200 sqlite3VdbeAddOp(v, OP_Close, iIdx, 0);
drh7cf6e4d2004-05-19 14:56:55 +00002201 sqlite3VdbeAddOp(v, OP_MoveGe, base, 0);
drh9562b552002-02-19 15:00:07 +00002202 }
drh5cf8e8c2002-02-19 22:42:05 +00002203 eList.nExpr = 1;
2204 memset(&eListItem, 0, sizeof(eListItem));
2205 eList.a = &eListItem;
2206 eList.a[0].pExpr = pExpr;
danielk197784ac9d02004-05-18 09:58:06 +00002207 selectInnerLoop(pParse, p, &eList, 0, 0, 0, -1, eDest, iParm, cont, cont, 0);
danielk19774adee202004-05-08 08:23:19 +00002208 sqlite3VdbeResolveLabel(v, cont);
2209 sqlite3VdbeAddOp(v, OP_Close, base, 0);
drh6e175292004-03-13 14:00:36 +00002210
drh9562b552002-02-19 15:00:07 +00002211 return 1;
2212}
2213
2214/*
drh290c1942004-08-21 17:54:45 +00002215** Analyze and ORDER BY or GROUP BY clause in a SELECT statement. Return
2216** the number of errors seen.
2217**
2218** An ORDER BY or GROUP BY is a list of expressions. If any expression
2219** is an integer constant, then that expression is replaced by the
2220** corresponding entry in the result set.
2221*/
2222static int processOrderGroupBy(
2223 Parse *pParse, /* Parsing context */
2224 ExprList *pOrderBy, /* The ORDER BY or GROUP BY clause to be processed */
2225 SrcList *pTabList, /* The FROM clause */
2226 ExprList *pEList, /* The result set */
drh1398ad32005-01-19 23:24:50 +00002227 NameContext *pNC, /* Name context for enclosing query */
drh290c1942004-08-21 17:54:45 +00002228 int isAgg, /* True if aggregate functions are involved */
2229 const char *zType /* Either "ORDER" or "GROUP", as appropriate */
2230){
2231 int i;
2232 if( pOrderBy==0 ) return 0;
2233 for(i=0; i<pOrderBy->nExpr; i++){
2234 int iCol;
2235 Expr *pE = pOrderBy->a[i].pExpr;
2236 if( sqlite3ExprIsInteger(pE, &iCol) && iCol>0 && iCol<=pEList->nExpr ){
2237 sqlite3ExprDelete(pE);
2238 pE = pOrderBy->a[i].pExpr = sqlite3ExprDup(pEList->a[iCol-1].pExpr);
2239 }
drh1398ad32005-01-19 23:24:50 +00002240 if( sqlite3ExprResolveNames(pParse, pTabList, pEList, pNC, pE, isAgg, 1) ){
drh290c1942004-08-21 17:54:45 +00002241 return 1;
2242 }
2243 if( sqlite3ExprIsConstant(pE) ){
2244 if( sqlite3ExprIsInteger(pE, &iCol)==0 ){
2245 sqlite3ErrorMsg(pParse,
2246 "%s BY terms must not be non-integer constants", zType);
2247 return 1;
2248 }else if( iCol<=0 || iCol>pEList->nExpr ){
2249 sqlite3ErrorMsg(pParse,
2250 "%s BY column number %d out of range - should be "
2251 "between 1 and %d", zType, iCol, pEList->nExpr);
2252 return 1;
2253 }
2254 }
2255 }
2256 return 0;
2257}
2258
2259/*
drh9bb61fe2000-06-05 16:01:39 +00002260** Generate code for the given SELECT statement.
2261**
drhfef52082000-06-06 01:50:43 +00002262** The results are distributed in various ways depending on the
2263** value of eDest and iParm.
2264**
2265** eDest Value Result
2266** ------------ -------------------------------------------
2267** SRT_Callback Invoke the callback for each row of the result.
2268**
2269** SRT_Mem Store first result in memory cell iParm
2270**
danielk1977e014a832004-05-17 10:48:57 +00002271** SRT_Set Store results as keys of table iParm.
drhfef52082000-06-06 01:50:43 +00002272**
drh82c3d632000-06-06 21:56:07 +00002273** SRT_Union Store results as a key in a temporary table iParm
2274**
jplyon4b11c6d2004-01-19 04:57:53 +00002275** SRT_Except Remove results from the temporary table iParm.
drhc4a3c772001-04-04 11:48:57 +00002276**
2277** SRT_Table Store results in temporary table iParm
drh9bb61fe2000-06-05 16:01:39 +00002278**
drhe78e8282003-01-19 03:59:45 +00002279** The table above is incomplete. Additional eDist value have be added
2280** since this comment was written. See the selectInnerLoop() function for
2281** a complete listing of the allowed values of eDest and their meanings.
2282**
drh9bb61fe2000-06-05 16:01:39 +00002283** This routine returns the number of errors. If any errors are
2284** encountered, then an appropriate error message is left in
2285** pParse->zErrMsg.
2286**
2287** This routine does NOT free the Select structure passed in. The
2288** calling function needs to do that.
drh1b2e0322002-03-03 02:49:51 +00002289**
2290** The pParent, parentTab, and *pParentAgg fields are filled in if this
2291** SELECT is a subquery. This routine may try to combine this SELECT
2292** with its parent to form a single flat query. In so doing, it might
2293** change the parent query from a non-aggregate to an aggregate query.
2294** For that reason, the pParentAgg flag is passed as a pointer, so it
2295** can be changed.
drhe78e8282003-01-19 03:59:45 +00002296**
2297** Example 1: The meaning of the pParent parameter.
2298**
2299** SELECT * FROM t1 JOIN (SELECT x, count(*) FROM t2) JOIN t3;
2300** \ \_______ subquery _______/ /
2301** \ /
2302** \____________________ outer query ___________________/
2303**
2304** This routine is called for the outer query first. For that call,
2305** pParent will be NULL. During the processing of the outer query, this
2306** routine is called recursively to handle the subquery. For the recursive
2307** call, pParent will point to the outer query. Because the subquery is
2308** the second element in a three-way join, the parentTab parameter will
2309** be 1 (the 2nd value of a 0-indexed array.)
drh9bb61fe2000-06-05 16:01:39 +00002310*/
danielk19774adee202004-05-08 08:23:19 +00002311int sqlite3Select(
drhcce7d172000-05-31 15:34:51 +00002312 Parse *pParse, /* The parser context */
drh9bb61fe2000-06-05 16:01:39 +00002313 Select *p, /* The SELECT statement being coded. */
drhe78e8282003-01-19 03:59:45 +00002314 int eDest, /* How to dispose of the results */
2315 int iParm, /* A parameter used by the eDest disposal method */
drh832508b2002-03-02 17:04:07 +00002316 Select *pParent, /* Another SELECT for which this is a sub-query */
2317 int parentTab, /* Index in pParent->pSrc of this query */
danielk197784ac9d02004-05-18 09:58:06 +00002318 int *pParentAgg, /* True if pParent uses aggregate functions */
drh1398ad32005-01-19 23:24:50 +00002319 char *aff, /* If eDest is SRT_Union, the affinity string */
2320 NameContext *pNC /* Namespace of the next outer query */
drhcce7d172000-05-31 15:34:51 +00002321){
drhd8bc7082000-06-07 23:51:50 +00002322 int i;
drhcce7d172000-05-31 15:34:51 +00002323 WhereInfo *pWInfo;
2324 Vdbe *v;
2325 int isAgg = 0; /* True for select lists like "count(*)" */
drha2e00042002-01-22 03:13:42 +00002326 ExprList *pEList; /* List of columns to extract. */
drhad3cab52002-05-24 02:04:32 +00002327 SrcList *pTabList; /* List of tables to select from */
drh9bb61fe2000-06-05 16:01:39 +00002328 Expr *pWhere; /* The WHERE clause. May be NULL */
2329 ExprList *pOrderBy; /* The ORDER BY clause. May be NULL */
drh22827922000-06-06 17:27:05 +00002330 ExprList *pGroupBy; /* The GROUP BY clause. May be NULL */
2331 Expr *pHaving; /* The HAVING clause. May be NULL */
drh19a775c2000-06-05 18:54:46 +00002332 int isDistinct; /* True if the DISTINCT keyword is present */
2333 int distinct; /* Table to use for the distinct set */
drh1d83f052002-02-17 00:30:36 +00002334 int rc = 1; /* Value to return from this function */
drh9bb61fe2000-06-05 16:01:39 +00002335
danielk19776f8a5032004-05-10 10:34:51 +00002336 if( sqlite3_malloc_failed || pParse->nErr || p==0 ) return 1;
danielk19774adee202004-05-08 08:23:19 +00002337 if( sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0) ) return 1;
drhdaffd0e2001-04-11 14:28:42 +00002338
drhb7f91642004-10-31 02:22:47 +00002339#ifndef SQLITE_OMIT_COMPOUND_SELECT
drh82c3d632000-06-06 21:56:07 +00002340 /* If there is are a sequence of queries, do the earlier ones first.
2341 */
2342 if( p->pPrior ){
drhb6c29892004-11-22 19:12:19 +00002343#ifndef SQLITE_OMIT_CURSOR
2344 if( p->pFetch ){
2345 sqlite3ErrorMsg(pParse, "cursors cannot be used on compound queries");
2346 goto select_end;
2347 }
2348#endif
danielk197784ac9d02004-05-18 09:58:06 +00002349 return multiSelect(pParse, p, eDest, iParm, aff);
drh82c3d632000-06-06 21:56:07 +00002350 }
drhb7f91642004-10-31 02:22:47 +00002351#endif
drh82c3d632000-06-06 21:56:07 +00002352
2353 /* Make local copies of the parameters for this query.
2354 */
drh9bb61fe2000-06-05 16:01:39 +00002355 pTabList = p->pSrc;
2356 pWhere = p->pWhere;
2357 pOrderBy = p->pOrderBy;
drh22827922000-06-06 17:27:05 +00002358 pGroupBy = p->pGroupBy;
2359 pHaving = p->pHaving;
drh19a775c2000-06-05 18:54:46 +00002360 isDistinct = p->isDistinct;
drh9bb61fe2000-06-05 16:01:39 +00002361
2362 /*
2363 ** Do not even attempt to generate any code if we have already seen
2364 ** errors before this routine starts.
2365 */
drh1d83f052002-02-17 00:30:36 +00002366 if( pParse->nErr>0 ) goto select_end;
drhcce7d172000-05-31 15:34:51 +00002367
drh9b3187e2005-01-18 14:45:47 +00002368 if( prepSelectStmt(pParse, p) ){
drh1d83f052002-02-17 00:30:36 +00002369 goto select_end;
drhcce7d172000-05-31 15:34:51 +00002370 }
drhad2d8302002-05-24 20:31:36 +00002371 pWhere = p->pWhere;
drhd8bc7082000-06-07 23:51:50 +00002372 pEList = p->pEList;
drh1d83f052002-02-17 00:30:36 +00002373 if( pEList==0 ) goto select_end;
drhcce7d172000-05-31 15:34:51 +00002374
drh22827922000-06-06 17:27:05 +00002375 /* If writing to memory or generating a set
2376 ** only a single column may be output.
drh19a775c2000-06-05 18:54:46 +00002377 */
drh51522cd2005-01-20 13:36:19 +00002378 assert( eDest!=SRT_Exists || pEList->nExpr==1 );
drhfef52082000-06-06 01:50:43 +00002379 if( (eDest==SRT_Mem || eDest==SRT_Set) && pEList->nExpr>1 ){
danielk19774adee202004-05-08 08:23:19 +00002380 sqlite3ErrorMsg(pParse, "only a single result allowed for "
drhda93d232003-03-31 02:12:46 +00002381 "a SELECT that is part of an expression");
drh1d83f052002-02-17 00:30:36 +00002382 goto select_end;
drh19a775c2000-06-05 18:54:46 +00002383 }
2384
drhc926afb2002-06-20 03:38:26 +00002385 /* ORDER BY is ignored for some destinations.
drh22827922000-06-06 17:27:05 +00002386 */
drhc926afb2002-06-20 03:38:26 +00002387 switch( eDest ){
2388 case SRT_Union:
2389 case SRT_Except:
2390 case SRT_Discard:
2391 pOrderBy = 0;
2392 break;
2393 default:
2394 break;
drh22827922000-06-06 17:27:05 +00002395 }
2396
drh10e5e3c2000-06-08 00:19:02 +00002397 /* At this point, we should have allocated all the cursors that we
drh832508b2002-03-02 17:04:07 +00002398 ** need to handle subquerys and temporary tables.
drh10e5e3c2000-06-08 00:19:02 +00002399 **
drh967e8b72000-06-21 13:59:10 +00002400 ** Resolve the column names and do a semantics check on all the expressions.
drh22827922000-06-06 17:27:05 +00002401 */
drh4794b982000-06-06 13:54:14 +00002402 for(i=0; i<pEList->nExpr; i++){
drh73b211a2005-01-18 04:00:42 +00002403 Expr *pX = pEList->a[i].pExpr;
drh1398ad32005-01-19 23:24:50 +00002404 if( sqlite3ExprResolveNames(pParse, pTabList, 0, pNC, pX, 1, 1) ){
drh1d83f052002-02-17 00:30:36 +00002405 goto select_end;
drhcce7d172000-05-31 15:34:51 +00002406 }
drh73b211a2005-01-18 04:00:42 +00002407 if( ExprHasProperty(pX, EP_Agg) ) isAgg = 1;
drhcce7d172000-05-31 15:34:51 +00002408 }
drh1398ad32005-01-19 23:24:50 +00002409 if( sqlite3ExprResolveNames(pParse, pTabList, pEList, pNC, pWhere, 0, 1) ){
drh290c1942004-08-21 17:54:45 +00002410 goto select_end;
drhcce7d172000-05-31 15:34:51 +00002411 }
drhc66c5a22002-12-03 02:34:49 +00002412 if( pHaving ){
2413 if( pGroupBy==0 ){
danielk19774adee202004-05-08 08:23:19 +00002414 sqlite3ErrorMsg(pParse, "a GROUP BY clause is required before HAVING");
drhc66c5a22002-12-03 02:34:49 +00002415 goto select_end;
2416 }
drh1398ad32005-01-19 23:24:50 +00002417 if( sqlite3ExprResolveNames(pParse, pTabList, pEList, pNC, pHaving, 1, 1) ){
drhc66c5a22002-12-03 02:34:49 +00002418 goto select_end;
2419 }
drh73b211a2005-01-18 04:00:42 +00002420 if( ExprHasProperty(pHaving, EP_Agg) ) isAgg = 1;
drhc66c5a22002-12-03 02:34:49 +00002421 }
drh49d642d2005-01-03 02:26:54 +00002422 if( pGroupBy && !isAgg ){
2423 sqlite3ErrorMsg(pParse, "GROUP BY may only be used on aggregate queries");
2424 goto select_end;
2425 }
drh1398ad32005-01-19 23:24:50 +00002426 if( processOrderGroupBy(pParse,pOrderBy,pTabList,pEList,pNC,isAgg,"ORDER")
2427 || processOrderGroupBy(pParse,pGroupBy,pTabList,pEList,pNC,isAgg,"GROUP")
drh290c1942004-08-21 17:54:45 +00002428 ){
2429 goto select_end;
drh22827922000-06-06 17:27:05 +00002430 }
drhcce7d172000-05-31 15:34:51 +00002431
drhb6c29892004-11-22 19:12:19 +00002432 /* We cannot use a SQL cursor on a join or on a DISTINCT query
2433 */
2434#ifndef SQLITE_OMIT_CURSOR
2435 if( p->pFetch ){
2436 if( p->isDistinct ){
2437 sqlite3ErrorMsg(pParse, "cursors cannot be used on DISTINCT queries");
2438 goto select_end;
2439 }
2440 if( pTabList->nSrc>0 ){
2441 sqlite3ErrorMsg(pParse, "cursors cannot be used on joins");
2442 goto select_end;
2443 }
2444 if( pTabList->a[0].pSelect ){
2445 sqlite3ErrorMsg(pParse, "cursor cannot be used with nested queries "
2446 "or views");
2447 goto select_end;
2448 }
2449 }
2450#endif
2451
drhd820cb12002-02-18 03:21:45 +00002452 /* Begin generating code.
2453 */
danielk19774adee202004-05-08 08:23:19 +00002454 v = sqlite3GetVdbe(pParse);
drhd820cb12002-02-18 03:21:45 +00002455 if( v==0 ) goto select_end;
2456
drhe78e8282003-01-19 03:59:45 +00002457 /* Identify column names if we will be using them in a callback. This
2458 ** step is skipped if the output is going to some other destination.
drh0bb28102002-05-08 11:54:14 +00002459 */
2460 if( eDest==SRT_Callback ){
drh6a3ea0e2003-05-02 14:32:12 +00002461 generateColumnNames(pParse, pTabList, pEList);
drh0bb28102002-05-08 11:54:14 +00002462 }
2463
drhd820cb12002-02-18 03:21:45 +00002464 /* Generate code for all sub-queries in the FROM clause
2465 */
drh51522cd2005-01-20 13:36:19 +00002466#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
drhad3cab52002-05-24 02:04:32 +00002467 for(i=0; i<pTabList->nSrc; i++){
danielk1977742f9472004-06-16 12:02:43 +00002468 const char *zSavedAuthContext = 0;
drhc31c2eb2003-05-02 16:04:17 +00002469 int needRestoreContext;
2470
drha76b5df2002-02-23 02:32:10 +00002471 if( pTabList->a[i].pSelect==0 ) continue;
drh5cf590c2003-04-24 01:45:04 +00002472 if( pTabList->a[i].zName!=0 ){
2473 zSavedAuthContext = pParse->zAuthContext;
2474 pParse->zAuthContext = pTabList->a[i].zName;
drhc31c2eb2003-05-02 16:04:17 +00002475 needRestoreContext = 1;
2476 }else{
2477 needRestoreContext = 0;
drh5cf590c2003-04-24 01:45:04 +00002478 }
danielk19774adee202004-05-08 08:23:19 +00002479 sqlite3Select(pParse, pTabList->a[i].pSelect, SRT_TempTable,
drh1398ad32005-01-19 23:24:50 +00002480 pTabList->a[i].iCursor, p, i, &isAgg, 0, 0);
drhc31c2eb2003-05-02 16:04:17 +00002481 if( needRestoreContext ){
drh5cf590c2003-04-24 01:45:04 +00002482 pParse->zAuthContext = zSavedAuthContext;
2483 }
drh1b2e0322002-03-03 02:49:51 +00002484 pTabList = p->pSrc;
2485 pWhere = p->pWhere;
drhc31c2eb2003-05-02 16:04:17 +00002486 if( eDest!=SRT_Union && eDest!=SRT_Except && eDest!=SRT_Discard ){
drhacd4c692002-03-07 02:02:51 +00002487 pOrderBy = p->pOrderBy;
2488 }
drh1b2e0322002-03-03 02:49:51 +00002489 pGroupBy = p->pGroupBy;
2490 pHaving = p->pHaving;
2491 isDistinct = p->isDistinct;
drhd820cb12002-02-18 03:21:45 +00002492 }
drh51522cd2005-01-20 13:36:19 +00002493#endif
drhd820cb12002-02-18 03:21:45 +00002494
drh6e175292004-03-13 14:00:36 +00002495 /* Check for the special case of a min() or max() function by itself
2496 ** in the result set.
2497 */
2498 if( simpleMinMaxQuery(pParse, p, eDest, iParm) ){
2499 rc = 0;
2500 goto select_end;
2501 }
2502
drh832508b2002-03-02 17:04:07 +00002503 /* Check to see if this is a subquery that can be "flattened" into its parent.
2504 ** If flattening is a possiblity, do so and return immediately.
2505 */
drhb7f91642004-10-31 02:22:47 +00002506#ifndef SQLITE_OMIT_VIEW
drh1b2e0322002-03-03 02:49:51 +00002507 if( pParent && pParentAgg &&
drh8c74a8c2002-08-25 19:20:40 +00002508 flattenSubquery(pParse, pParent, parentTab, *pParentAgg, isAgg) ){
drh1b2e0322002-03-03 02:49:51 +00002509 if( isAgg ) *pParentAgg = 1;
drh832508b2002-03-02 17:04:07 +00002510 return rc;
2511 }
drhb7f91642004-10-31 02:22:47 +00002512#endif
drh832508b2002-03-02 17:04:07 +00002513
danielk19777cedc8d2004-06-10 10:50:08 +00002514 /* If there is an ORDER BY clause, resolve any collation sequences
2515 ** names that have been explicitly specified.
2516 */
2517 if( pOrderBy ){
2518 for(i=0; i<pOrderBy->nExpr; i++){
2519 if( pOrderBy->a[i].zName ){
2520 pOrderBy->a[i].pExpr->pColl =
2521 sqlite3LocateCollSeq(pParse, pOrderBy->a[i].zName, -1);
2522 }
2523 }
2524 if( pParse->nErr ){
2525 goto select_end;
2526 }
2527 }
2528
drh7b58dae2003-07-20 01:16:46 +00002529 /* Set the limiter.
2530 */
2531 computeLimitRegisters(pParse, p);
2532
drh2d0794e2002-03-03 03:03:52 +00002533 /* If the output is destined for a temporary table, open that table.
2534 */
2535 if( eDest==SRT_TempTable ){
danielk19774adee202004-05-08 08:23:19 +00002536 sqlite3VdbeAddOp(v, OP_OpenTemp, iParm, 0);
danielk1977b4964b72004-05-18 01:23:38 +00002537 sqlite3VdbeAddOp(v, OP_SetNumColumns, iParm, pEList->nExpr);
drh2d0794e2002-03-03 03:03:52 +00002538 }
2539
drh22827922000-06-06 17:27:05 +00002540 /* Do an analysis of aggregate expressions.
drhefb72512000-05-31 20:00:52 +00002541 */
drhd820cb12002-02-18 03:21:45 +00002542 sqliteAggregateInfoReset(pParse);
drhbb999ef2003-02-02 12:41:25 +00002543 if( isAgg || pGroupBy ){
drh0bce8352002-02-28 00:41:10 +00002544 assert( pParse->nAgg==0 );
drhbb999ef2003-02-02 12:41:25 +00002545 isAgg = 1;
drh22827922000-06-06 17:27:05 +00002546 for(i=0; i<pEList->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00002547 if( sqlite3ExprAnalyzeAggregates(pParse, pEList->a[i].pExpr) ){
drh1d83f052002-02-17 00:30:36 +00002548 goto select_end;
drh22827922000-06-06 17:27:05 +00002549 }
2550 }
2551 if( pGroupBy ){
2552 for(i=0; i<pGroupBy->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00002553 if( sqlite3ExprAnalyzeAggregates(pParse, pGroupBy->a[i].pExpr) ){
drh1d83f052002-02-17 00:30:36 +00002554 goto select_end;
drh22827922000-06-06 17:27:05 +00002555 }
2556 }
2557 }
danielk19774adee202004-05-08 08:23:19 +00002558 if( pHaving && sqlite3ExprAnalyzeAggregates(pParse, pHaving) ){
drh1d83f052002-02-17 00:30:36 +00002559 goto select_end;
drh22827922000-06-06 17:27:05 +00002560 }
drh191b6902000-06-08 11:13:01 +00002561 if( pOrderBy ){
2562 for(i=0; i<pOrderBy->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00002563 if( sqlite3ExprAnalyzeAggregates(pParse, pOrderBy->a[i].pExpr) ){
drh1d83f052002-02-17 00:30:36 +00002564 goto select_end;
drh191b6902000-06-08 11:13:01 +00002565 }
2566 }
2567 }
drhefb72512000-05-31 20:00:52 +00002568 }
2569
drh22827922000-06-06 17:27:05 +00002570 /* Reset the aggregator
drhcce7d172000-05-31 15:34:51 +00002571 */
2572 if( isAgg ){
danielk1977e159fdf2004-06-21 10:45:06 +00002573 int addr = sqlite3VdbeAddOp(v, OP_AggReset, (pGroupBy?0:1), pParse->nAgg);
drhe5095352002-02-24 03:25:14 +00002574 for(i=0; i<pParse->nAgg; i++){
drh0bce8352002-02-28 00:41:10 +00002575 FuncDef *pFunc;
2576 if( (pFunc = pParse->aAgg[i].pFunc)!=0 && pFunc->xFinalize!=0 ){
drhf9b596e2004-05-26 16:54:42 +00002577 sqlite3VdbeOp3(v, OP_AggInit, 0, i, (char*)pFunc, P3_FUNCDEF);
drhe5095352002-02-24 03:25:14 +00002578 }
2579 }
danielk1977e159fdf2004-06-21 10:45:06 +00002580 if( pGroupBy ){
danielk1977ce2663c2004-06-11 13:19:21 +00002581 int sz = sizeof(KeyInfo) + pGroupBy->nExpr*sizeof(CollSeq*);
2582 KeyInfo *pKey = (KeyInfo *)sqliteMalloc(sz);
2583 if( 0==pKey ){
2584 goto select_end;
2585 }
2586 pKey->enc = pParse->db->enc;
2587 pKey->nField = pGroupBy->nExpr;
2588 for(i=0; i<pGroupBy->nExpr; i++){
2589 pKey->aColl[i] = sqlite3ExprCollSeq(pParse, pGroupBy->a[i].pExpr);
2590 if( !pKey->aColl[i] ){
2591 pKey->aColl[i] = pParse->db->pDfltColl;
2592 }
2593 }
2594 sqlite3VdbeChangeP3(v, addr, (char *)pKey, P3_KEYINFO_HANDOFF);
drh1bee3d72001-10-15 00:44:35 +00002595 }
drhcce7d172000-05-31 15:34:51 +00002596 }
2597
drh51522cd2005-01-20 13:36:19 +00002598 /* Initialize the memory cell to NULL for SRT_Mem or 0 for SRT_Exists
drh19a775c2000-06-05 18:54:46 +00002599 */
drh51522cd2005-01-20 13:36:19 +00002600 if( eDest==SRT_Mem || eDest==SRT_Exists ){
2601 sqlite3VdbeAddOp(v, eDest==SRT_Mem ? OP_String8 : OP_Integer, 0, 0);
danielk19774adee202004-05-08 08:23:19 +00002602 sqlite3VdbeAddOp(v, OP_MemStore, iParm, 1);
drh19a775c2000-06-05 18:54:46 +00002603 }
2604
drh832508b2002-03-02 17:04:07 +00002605 /* Open a temporary table to use for the distinct set.
drhefb72512000-05-31 20:00:52 +00002606 */
drh19a775c2000-06-05 18:54:46 +00002607 if( isDistinct ){
drh832508b2002-03-02 17:04:07 +00002608 distinct = pParse->nTab++;
drhd3d39e92004-05-20 22:16:29 +00002609 openTempIndex(pParse, p, distinct, 0);
drh832508b2002-03-02 17:04:07 +00002610 }else{
2611 distinct = -1;
drhefb72512000-05-31 20:00:52 +00002612 }
drh832508b2002-03-02 17:04:07 +00002613
2614 /* Begin the database scan
2615 */
drhe6f85e72004-12-25 01:03:13 +00002616 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere,
drhe4e72072004-11-23 01:47:30 +00002617 pGroupBy ? 0 : &pOrderBy, p->pFetch);
drh1d83f052002-02-17 00:30:36 +00002618 if( pWInfo==0 ) goto select_end;
drhcce7d172000-05-31 15:34:51 +00002619
drh22827922000-06-06 17:27:05 +00002620 /* Use the standard inner loop if we are not dealing with
2621 ** aggregates
drhcce7d172000-05-31 15:34:51 +00002622 */
drhda9d6c42000-05-31 18:20:14 +00002623 if( !isAgg ){
drhdf199a22002-06-14 22:38:41 +00002624 if( selectInnerLoop(pParse, p, pEList, 0, 0, pOrderBy, distinct, eDest,
danielk197784ac9d02004-05-18 09:58:06 +00002625 iParm, pWInfo->iContinue, pWInfo->iBreak, aff) ){
drh1d83f052002-02-17 00:30:36 +00002626 goto select_end;
drhda9d6c42000-05-31 18:20:14 +00002627 }
drhcce7d172000-05-31 15:34:51 +00002628 }
drhefb72512000-05-31 20:00:52 +00002629
drhe3184742002-06-19 14:27:05 +00002630 /* If we are dealing with aggregates, then do the special aggregate
drh22827922000-06-06 17:27:05 +00002631 ** processing.
drhefb72512000-05-31 20:00:52 +00002632 */
drh22827922000-06-06 17:27:05 +00002633 else{
drh268380c2004-02-25 13:47:31 +00002634 AggExpr *pAgg;
drh22827922000-06-06 17:27:05 +00002635 if( pGroupBy ){
drh1bee3d72001-10-15 00:44:35 +00002636 int lbl1;
drh22827922000-06-06 17:27:05 +00002637 for(i=0; i<pGroupBy->nExpr; i++){
danielk19774adee202004-05-08 08:23:19 +00002638 sqlite3ExprCode(pParse, pGroupBy->a[i].pExpr);
drh22827922000-06-06 17:27:05 +00002639 }
danielk1977ededfd52004-06-17 07:53:01 +00002640 /* No affinity string is attached to the following OP_MakeRecord
drhd3d39e92004-05-20 22:16:29 +00002641 ** because we do not need to do any coercion of datatypes. */
danielk1977ededfd52004-06-17 07:53:01 +00002642 sqlite3VdbeAddOp(v, OP_MakeRecord, pGroupBy->nExpr, 0);
danielk19774adee202004-05-08 08:23:19 +00002643 lbl1 = sqlite3VdbeMakeLabel(v);
2644 sqlite3VdbeAddOp(v, OP_AggFocus, 0, lbl1);
drh268380c2004-02-25 13:47:31 +00002645 for(i=0, pAgg=pParse->aAgg; i<pParse->nAgg; i++, pAgg++){
2646 if( pAgg->isAgg ) continue;
danielk19774adee202004-05-08 08:23:19 +00002647 sqlite3ExprCode(pParse, pAgg->pExpr);
2648 sqlite3VdbeAddOp(v, OP_AggSet, 0, i);
drhcce7d172000-05-31 15:34:51 +00002649 }
danielk19774adee202004-05-08 08:23:19 +00002650 sqlite3VdbeResolveLabel(v, lbl1);
drhcce7d172000-05-31 15:34:51 +00002651 }
drh268380c2004-02-25 13:47:31 +00002652 for(i=0, pAgg=pParse->aAgg; i<pParse->nAgg; i++, pAgg++){
drh22827922000-06-06 17:27:05 +00002653 Expr *pE;
drh268380c2004-02-25 13:47:31 +00002654 int nExpr;
2655 FuncDef *pDef;
2656 if( !pAgg->isAgg ) continue;
2657 assert( pAgg->pFunc!=0 );
2658 assert( pAgg->pFunc->xStep!=0 );
2659 pDef = pAgg->pFunc;
2660 pE = pAgg->pExpr;
2661 assert( pE!=0 );
drh22827922000-06-06 17:27:05 +00002662 assert( pE->op==TK_AGG_FUNCTION );
drhf9b596e2004-05-26 16:54:42 +00002663 nExpr = sqlite3ExprCodeExprList(pParse, pE->pList);
danielk19774adee202004-05-08 08:23:19 +00002664 sqlite3VdbeAddOp(v, OP_Integer, i, 0);
danielk1977dc1bdc42004-06-11 10:51:27 +00002665 if( pDef->needCollSeq ){
2666 CollSeq *pColl = 0;
2667 int j;
2668 for(j=0; !pColl && j<nExpr; j++){
2669 pColl = sqlite3ExprCollSeq(pParse, pE->pList->a[j].pExpr);
2670 }
2671 if( !pColl ) pColl = pParse->db->pDfltColl;
2672 sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ);
2673 }
danielk19774adee202004-05-08 08:23:19 +00002674 sqlite3VdbeOp3(v, OP_AggFunc, 0, nExpr, (char*)pDef, P3_POINTER);
drh22827922000-06-06 17:27:05 +00002675 }
drhcce7d172000-05-31 15:34:51 +00002676 }
2677
2678 /* End the database scan loop.
2679 */
danielk19774adee202004-05-08 08:23:19 +00002680 sqlite3WhereEnd(pWInfo);
drhcce7d172000-05-31 15:34:51 +00002681
drh22827922000-06-06 17:27:05 +00002682 /* If we are processing aggregates, we need to set up a second loop
2683 ** over all of the aggregate values and process them.
2684 */
2685 if( isAgg ){
danielk19774adee202004-05-08 08:23:19 +00002686 int endagg = sqlite3VdbeMakeLabel(v);
drh22827922000-06-06 17:27:05 +00002687 int startagg;
danielk19774adee202004-05-08 08:23:19 +00002688 startagg = sqlite3VdbeAddOp(v, OP_AggNext, 0, endagg);
drh22827922000-06-06 17:27:05 +00002689 pParse->useAgg = 1;
2690 if( pHaving ){
danielk19774adee202004-05-08 08:23:19 +00002691 sqlite3ExprIfFalse(pParse, pHaving, startagg, 1);
drh22827922000-06-06 17:27:05 +00002692 }
drhdf199a22002-06-14 22:38:41 +00002693 if( selectInnerLoop(pParse, p, pEList, 0, 0, pOrderBy, distinct, eDest,
danielk197784ac9d02004-05-18 09:58:06 +00002694 iParm, startagg, endagg, aff) ){
drh1d83f052002-02-17 00:30:36 +00002695 goto select_end;
drh22827922000-06-06 17:27:05 +00002696 }
danielk19774adee202004-05-08 08:23:19 +00002697 sqlite3VdbeAddOp(v, OP_Goto, 0, startagg);
2698 sqlite3VdbeResolveLabel(v, endagg);
2699 sqlite3VdbeAddOp(v, OP_Noop, 0, 0);
drh22827922000-06-06 17:27:05 +00002700 pParse->useAgg = 0;
2701 }
2702
drhcce7d172000-05-31 15:34:51 +00002703 /* If there is an ORDER BY clause, then we need to sort the results
2704 ** and send them to the callback one by one.
2705 */
2706 if( pOrderBy ){
drhffbc3082004-05-21 01:29:06 +00002707 generateSortTail(pParse, p, v, pEList->nExpr, eDest, iParm);
drhcce7d172000-05-31 15:34:51 +00002708 }
drh6a535342001-10-19 16:44:56 +00002709
drhf620b4e2004-02-09 14:37:50 +00002710 /* If this was a subquery, we have now converted the subquery into a
2711 ** temporary table. So delete the subquery structure from the parent
2712 ** to prevent this subquery from being evaluated again and to force the
2713 ** the use of the temporary table.
2714 */
2715 if( pParent ){
2716 assert( pParent->pSrc->nSrc>parentTab );
2717 assert( pParent->pSrc->a[parentTab].pSelect==p );
danielk19774adee202004-05-08 08:23:19 +00002718 sqlite3SelectDelete(p);
drhf620b4e2004-02-09 14:37:50 +00002719 pParent->pSrc->a[parentTab].pSelect = 0;
2720 }
2721
drh1d83f052002-02-17 00:30:36 +00002722 /* The SELECT was successfully coded. Set the return code to 0
2723 ** to indicate no errors.
2724 */
2725 rc = 0;
2726
2727 /* Control jumps to here if an error is encountered above, or upon
2728 ** successful coding of the SELECT.
2729 */
2730select_end:
2731 sqliteAggregateInfoReset(pParse);
2732 return rc;
drhcce7d172000-05-31 15:34:51 +00002733}