blob: fde9270cab851a73f9f84ea9893d28f9fc5b24d7 [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**
jplyon4b11c6d2004-01-19 04:57:53 +000015** $Id: select.c,v 1.148 2004/01/19 04:57:53 jplyon 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*/
drh9bb61fe2000-06-05 16:01:39 +000024Select *sqliteSelectNew(
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 ){
38 sqliteExprListDelete(pEList);
drhad3cab52002-05-24 02:04:32 +000039 sqliteSrcListDelete(pSrc);
drhdaffd0e2001-04-11 14:28:42 +000040 sqliteExprDelete(pWhere);
41 sqliteExprListDelete(pGroupBy);
42 sqliteExprDelete(pHaving);
43 sqliteExprListDelete(pOrderBy);
44 }else{
45 pNew->pEList = pEList;
46 pNew->pSrc = pSrc;
47 pNew->pWhere = pWhere;
48 pNew->pGroupBy = pGroupBy;
49 pNew->pHaving = pHaving;
50 pNew->pOrderBy = pOrderBy;
51 pNew->isDistinct = isDistinct;
52 pNew->op = TK_SELECT;
drh9bbca4c2001-11-06 04:00:18 +000053 pNew->nLimit = nLimit;
54 pNew->nOffset = nOffset;
drh7b58dae2003-07-20 01:16:46 +000055 pNew->iLimit = -1;
56 pNew->iOffset = -1;
drhdaffd0e2001-04-11 14:28:42 +000057 }
drh9bb61fe2000-06-05 16:01:39 +000058 return pNew;
59}
60
61/*
drh01f3f252002-05-24 16:14:15 +000062** Given 1 to 3 identifiers preceeding the JOIN keyword, determine the
63** type of join. Return an integer constant that expresses that type
64** in terms of the following bit values:
65**
66** JT_INNER
67** JT_OUTER
68** JT_NATURAL
69** JT_LEFT
70** JT_RIGHT
71**
72** A full outer join is the combination of JT_LEFT and JT_RIGHT.
73**
74** If an illegal or unsupported join type is seen, then still return
75** a join type, but put an error in the pParse structure.
76*/
77int sqliteJoinType(Parse *pParse, Token *pA, Token *pB, Token *pC){
78 int jointype = 0;
79 Token *apAll[3];
80 Token *p;
81 static struct {
82 const char *zKeyword;
83 int nChar;
84 int code;
85 } keywords[] = {
86 { "natural", 7, JT_NATURAL },
drh195e6962002-05-25 00:18:20 +000087 { "left", 4, JT_LEFT|JT_OUTER },
88 { "right", 5, JT_RIGHT|JT_OUTER },
89 { "full", 4, JT_LEFT|JT_RIGHT|JT_OUTER },
drh01f3f252002-05-24 16:14:15 +000090 { "outer", 5, JT_OUTER },
91 { "inner", 5, JT_INNER },
92 { "cross", 5, JT_INNER },
93 };
94 int i, j;
95 apAll[0] = pA;
96 apAll[1] = pB;
97 apAll[2] = pC;
drh195e6962002-05-25 00:18:20 +000098 for(i=0; i<3 && apAll[i]; i++){
drh01f3f252002-05-24 16:14:15 +000099 p = apAll[i];
100 for(j=0; j<sizeof(keywords)/sizeof(keywords[0]); j++){
101 if( p->n==keywords[j].nChar
102 && sqliteStrNICmp(p->z, keywords[j].zKeyword, p->n)==0 ){
103 jointype |= keywords[j].code;
104 break;
105 }
106 }
107 if( j>=sizeof(keywords)/sizeof(keywords[0]) ){
108 jointype |= JT_ERROR;
109 break;
110 }
111 }
drhad2d8302002-05-24 20:31:36 +0000112 if(
113 (jointype & (JT_INNER|JT_OUTER))==(JT_INNER|JT_OUTER) ||
drh195e6962002-05-25 00:18:20 +0000114 (jointype & JT_ERROR)!=0
drhad2d8302002-05-24 20:31:36 +0000115 ){
drh01f3f252002-05-24 16:14:15 +0000116 static Token dummy = { 0, 0 };
117 char *zSp1 = " ", *zSp2 = " ";
118 if( pB==0 ){ pB = &dummy; zSp1 = 0; }
119 if( pC==0 ){ pC = &dummy; zSp2 = 0; }
120 sqliteSetNString(&pParse->zErrMsg, "unknown or unsupported join type: ", 0,
121 pA->z, pA->n, zSp1, 1, pB->z, pB->n, zSp2, 1, pC->z, pC->n, 0);
122 pParse->nErr++;
123 jointype = JT_INNER;
drh195e6962002-05-25 00:18:20 +0000124 }else if( jointype & JT_RIGHT ){
drhda93d232003-03-31 02:12:46 +0000125 sqliteErrorMsg(pParse,
126 "RIGHT and FULL OUTER JOINs are not currently supported");
drh195e6962002-05-25 00:18:20 +0000127 jointype = JT_INNER;
drh01f3f252002-05-24 16:14:15 +0000128 }
129 return jointype;
130}
131
132/*
drhad2d8302002-05-24 20:31:36 +0000133** Return the index of a column in a table. Return -1 if the column
134** is not contained in the table.
135*/
136static int columnIndex(Table *pTab, const char *zCol){
137 int i;
138 for(i=0; i<pTab->nCol; i++){
139 if( sqliteStrICmp(pTab->aCol[i].zName, zCol)==0 ) return i;
140 }
141 return -1;
142}
143
144/*
145** Add a term to the WHERE expression in *ppExpr that requires the
146** zCol column to be equal in the two tables pTab1 and pTab2.
147*/
148static void addWhereTerm(
149 const char *zCol, /* Name of the column */
150 const Table *pTab1, /* First table */
151 const Table *pTab2, /* Second table */
152 Expr **ppExpr /* Add the equality term to this expression */
153){
154 Token dummy;
155 Expr *pE1a, *pE1b, *pE1c;
156 Expr *pE2a, *pE2b, *pE2c;
157 Expr *pE;
158
159 dummy.z = zCol;
160 dummy.n = strlen(zCol);
drh4b59ab52002-08-24 18:24:51 +0000161 dummy.dyn = 0;
drhad2d8302002-05-24 20:31:36 +0000162 pE1a = sqliteExpr(TK_ID, 0, 0, &dummy);
163 pE2a = sqliteExpr(TK_ID, 0, 0, &dummy);
164 dummy.z = pTab1->zName;
165 dummy.n = strlen(dummy.z);
166 pE1b = sqliteExpr(TK_ID, 0, 0, &dummy);
167 dummy.z = pTab2->zName;
168 dummy.n = strlen(dummy.z);
169 pE2b = sqliteExpr(TK_ID, 0, 0, &dummy);
170 pE1c = sqliteExpr(TK_DOT, pE1b, pE1a, 0);
171 pE2c = sqliteExpr(TK_DOT, pE2b, pE2a, 0);
172 pE = sqliteExpr(TK_EQ, pE1c, pE2c, 0);
drh1f162302002-10-27 19:35:33 +0000173 ExprSetProperty(pE, EP_FromJoin);
drhad2d8302002-05-24 20:31:36 +0000174 if( *ppExpr ){
175 *ppExpr = sqliteExpr(TK_AND, *ppExpr, pE, 0);
176 }else{
177 *ppExpr = pE;
178 }
179}
180
181/*
drh1f162302002-10-27 19:35:33 +0000182** Set the EP_FromJoin property on all terms of the given expression.
drh1cc093c2002-06-24 22:01:57 +0000183**
drhe78e8282003-01-19 03:59:45 +0000184** The EP_FromJoin property is used on terms of an expression to tell
drh1cc093c2002-06-24 22:01:57 +0000185** the LEFT OUTER JOIN processing logic that this term is part of the
drh1f162302002-10-27 19:35:33 +0000186** join restriction specified in the ON or USING clause and not a part
187** of the more general WHERE clause. These terms are moved over to the
188** WHERE clause during join processing but we need to remember that they
189** originated in the ON or USING clause.
drh1cc093c2002-06-24 22:01:57 +0000190*/
191static void setJoinExpr(Expr *p){
192 while( p ){
drh1f162302002-10-27 19:35:33 +0000193 ExprSetProperty(p, EP_FromJoin);
drh1cc093c2002-06-24 22:01:57 +0000194 setJoinExpr(p->pLeft);
195 p = p->pRight;
196 }
197}
198
199/*
drhad2d8302002-05-24 20:31:36 +0000200** This routine processes the join information for a SELECT statement.
201** ON and USING clauses are converted into extra terms of the WHERE clause.
202** NATURAL joins also create extra WHERE clause terms.
203**
204** This routine returns the number of errors encountered.
205*/
206static int sqliteProcessJoin(Parse *pParse, Select *p){
207 SrcList *pSrc;
208 int i, j;
209 pSrc = p->pSrc;
210 for(i=0; i<pSrc->nSrc-1; i++){
211 struct SrcList_item *pTerm = &pSrc->a[i];
212 struct SrcList_item *pOther = &pSrc->a[i+1];
213
214 if( pTerm->pTab==0 || pOther->pTab==0 ) continue;
215
216 /* When the NATURAL keyword is present, add WHERE clause terms for
217 ** every column that the two tables have in common.
218 */
219 if( pTerm->jointype & JT_NATURAL ){
220 Table *pTab;
221 if( pTerm->pOn || pTerm->pUsing ){
drhda93d232003-03-31 02:12:46 +0000222 sqliteErrorMsg(pParse, "a NATURAL join may not have "
drhad2d8302002-05-24 20:31:36 +0000223 "an ON or USING clause", 0);
drhad2d8302002-05-24 20:31:36 +0000224 return 1;
225 }
226 pTab = pTerm->pTab;
227 for(j=0; j<pTab->nCol; j++){
228 if( columnIndex(pOther->pTab, pTab->aCol[j].zName)>=0 ){
229 addWhereTerm(pTab->aCol[j].zName, pTab, pOther->pTab, &p->pWhere);
230 }
231 }
232 }
233
234 /* Disallow both ON and USING clauses in the same join
235 */
236 if( pTerm->pOn && pTerm->pUsing ){
drhda93d232003-03-31 02:12:46 +0000237 sqliteErrorMsg(pParse, "cannot have both ON and USING "
238 "clauses in the same join");
drhad2d8302002-05-24 20:31:36 +0000239 return 1;
240 }
241
242 /* Add the ON clause to the end of the WHERE clause, connected by
243 ** and AND operator.
244 */
245 if( pTerm->pOn ){
drh1cc093c2002-06-24 22:01:57 +0000246 setJoinExpr(pTerm->pOn);
drhad2d8302002-05-24 20:31:36 +0000247 if( p->pWhere==0 ){
248 p->pWhere = pTerm->pOn;
249 }else{
250 p->pWhere = sqliteExpr(TK_AND, p->pWhere, pTerm->pOn, 0);
251 }
252 pTerm->pOn = 0;
253 }
254
255 /* Create extra terms on the WHERE clause for each column named
256 ** in the USING clause. Example: If the two tables to be joined are
257 ** A and B and the USING clause names X, Y, and Z, then add this
258 ** to the WHERE clause: A.X=B.X AND A.Y=B.Y AND A.Z=B.Z
259 ** Report an error if any column mentioned in the USING clause is
260 ** not contained in both tables to be joined.
261 */
262 if( pTerm->pUsing ){
263 IdList *pList;
264 int j;
265 assert( i<pSrc->nSrc-1 );
266 pList = pTerm->pUsing;
267 for(j=0; j<pList->nId; j++){
drhbf5cd972002-06-24 12:20:23 +0000268 if( columnIndex(pTerm->pTab, pList->a[j].zName)<0 ||
269 columnIndex(pOther->pTab, pList->a[j].zName)<0 ){
drhda93d232003-03-31 02:12:46 +0000270 sqliteErrorMsg(pParse, "cannot join using column %s - column "
271 "not present in both tables", pList->a[j].zName);
drhad2d8302002-05-24 20:31:36 +0000272 return 1;
273 }
drhbf5cd972002-06-24 12:20:23 +0000274 addWhereTerm(pList->a[j].zName, pTerm->pTab, pOther->pTab, &p->pWhere);
drhad2d8302002-05-24 20:31:36 +0000275 }
276 }
277 }
278 return 0;
279}
280
281/*
drh9bb61fe2000-06-05 16:01:39 +0000282** Delete the given Select structure and all of its substructures.
283*/
284void sqliteSelectDelete(Select *p){
drh82c3d632000-06-06 21:56:07 +0000285 if( p==0 ) return;
drh9bb61fe2000-06-05 16:01:39 +0000286 sqliteExprListDelete(p->pEList);
drhad3cab52002-05-24 02:04:32 +0000287 sqliteSrcListDelete(p->pSrc);
drh9bb61fe2000-06-05 16:01:39 +0000288 sqliteExprDelete(p->pWhere);
289 sqliteExprListDelete(p->pGroupBy);
290 sqliteExprDelete(p->pHaving);
291 sqliteExprListDelete(p->pOrderBy);
drh82c3d632000-06-06 21:56:07 +0000292 sqliteSelectDelete(p->pPrior);
drha76b5df2002-02-23 02:32:10 +0000293 sqliteFree(p->zSelect);
drh9bb61fe2000-06-05 16:01:39 +0000294 sqliteFree(p);
295}
296
297/*
drh22827922000-06-06 17:27:05 +0000298** Delete the aggregate information from the parse structure.
299*/
drh1d83f052002-02-17 00:30:36 +0000300static void sqliteAggregateInfoReset(Parse *pParse){
drh22827922000-06-06 17:27:05 +0000301 sqliteFree(pParse->aAgg);
302 pParse->aAgg = 0;
303 pParse->nAgg = 0;
drh22827922000-06-06 17:27:05 +0000304 pParse->useAgg = 0;
305}
306
307/*
drhc926afb2002-06-20 03:38:26 +0000308** Insert code into "v" that will push the record on the top of the
309** stack into the sorter.
310*/
311static void pushOntoSorter(Parse *pParse, Vdbe *v, ExprList *pOrderBy){
312 char *zSortOrder;
313 int i;
314 zSortOrder = sqliteMalloc( pOrderBy->nExpr + 1 );
315 if( zSortOrder==0 ) return;
316 for(i=0; i<pOrderBy->nExpr; i++){
drh38640e12002-07-05 21:42:36 +0000317 int order = pOrderBy->a[i].sortOrder;
318 int type;
319 int c;
320 if( (order & SQLITE_SO_TYPEMASK)==SQLITE_SO_TEXT ){
321 type = SQLITE_SO_TEXT;
322 }else if( (order & SQLITE_SO_TYPEMASK)==SQLITE_SO_NUM ){
323 type = SQLITE_SO_NUM;
drh491791a2002-07-18 00:34:09 +0000324 }else if( pParse->db->file_format>=4 ){
drh38640e12002-07-05 21:42:36 +0000325 type = sqliteExprType(pOrderBy->a[i].pExpr);
326 }else{
327 type = SQLITE_SO_NUM;
328 }
329 if( (order & SQLITE_SO_DIRMASK)==SQLITE_SO_ASC ){
330 c = type==SQLITE_SO_TEXT ? 'A' : '+';
331 }else{
332 c = type==SQLITE_SO_TEXT ? 'D' : '-';
333 }
334 zSortOrder[i] = c;
drhc926afb2002-06-20 03:38:26 +0000335 sqliteExprCode(pParse, pOrderBy->a[i].pExpr);
336 }
337 zSortOrder[pOrderBy->nExpr] = 0;
338 sqliteVdbeAddOp(v, OP_SortMakeKey, pOrderBy->nExpr, 0);
339 sqliteVdbeChangeP3(v, -1, zSortOrder, strlen(zSortOrder));
340 sqliteFree(zSortOrder);
341 sqliteVdbeAddOp(v, OP_SortPut, 0, 0);
342}
343
344/*
drh38640e12002-07-05 21:42:36 +0000345** This routine adds a P3 argument to the last VDBE opcode that was
346** inserted. The P3 argument added is a string suitable for the
347** OP_MakeKey or OP_MakeIdxKey opcodes. The string consists of
348** characters 't' or 'n' depending on whether or not the various
349** fields of the key to be generated should be treated as numeric
350** or as text. See the OP_MakeKey and OP_MakeIdxKey opcode
351** documentation for additional information about the P3 string.
352** See also the sqliteAddIdxKeyType() routine.
353*/
354void sqliteAddKeyType(Vdbe *v, ExprList *pEList){
355 int nColumn = pEList->nExpr;
356 char *zType = sqliteMalloc( nColumn+1 );
357 int i;
358 if( zType==0 ) return;
359 for(i=0; i<nColumn; i++){
360 zType[i] = sqliteExprType(pEList->a[i].pExpr)==SQLITE_SO_NUM ? 'n' : 't';
361 }
362 zType[i] = 0;
363 sqliteVdbeChangeP3(v, -1, zType, nColumn);
364 sqliteFree(zType);
365}
366
367/*
drh22827922000-06-06 17:27:05 +0000368** This routine generates the code for the inside of the inner loop
369** of a SELECT.
drh82c3d632000-06-06 21:56:07 +0000370**
drh38640e12002-07-05 21:42:36 +0000371** If srcTab and nColumn are both zero, then the pEList expressions
372** are evaluated in order to get the data for this row. If nColumn>0
373** then data is pulled from srcTab and pEList is used only to get the
374** datatypes for each column.
drh22827922000-06-06 17:27:05 +0000375*/
376static int selectInnerLoop(
377 Parse *pParse, /* The parser context */
drhdf199a22002-06-14 22:38:41 +0000378 Select *p, /* The complete select statement being coded */
drh22827922000-06-06 17:27:05 +0000379 ExprList *pEList, /* List of values being extracted */
drh82c3d632000-06-06 21:56:07 +0000380 int srcTab, /* Pull data from this table */
drh967e8b72000-06-21 13:59:10 +0000381 int nColumn, /* Number of columns in the source table */
drh22827922000-06-06 17:27:05 +0000382 ExprList *pOrderBy, /* If not NULL, sort results using this key */
383 int distinct, /* If >=0, make sure results are distinct */
384 int eDest, /* How to dispose of the results */
385 int iParm, /* An argument to the disposal method */
386 int iContinue, /* Jump here to continue with next row */
387 int iBreak /* Jump here to break out of the inner loop */
388){
389 Vdbe *v = pParse->pVdbe;
390 int i;
drh38640e12002-07-05 21:42:36 +0000391
drhdaffd0e2001-04-11 14:28:42 +0000392 if( v==0 ) return 0;
drh38640e12002-07-05 21:42:36 +0000393 assert( pEList!=0 );
drh22827922000-06-06 17:27:05 +0000394
drhdf199a22002-06-14 22:38:41 +0000395 /* If there was a LIMIT clause on the SELECT statement, then do the check
396 ** to see if this row should be output.
397 */
398 if( pOrderBy==0 ){
drh7b58dae2003-07-20 01:16:46 +0000399 if( p->iOffset>=0 ){
drhd11d3822002-06-21 23:01:49 +0000400 int addr = sqliteVdbeCurrentAddr(v);
drh7b58dae2003-07-20 01:16:46 +0000401 sqliteVdbeAddOp(v, OP_MemIncr, p->iOffset, addr+2);
drhd11d3822002-06-21 23:01:49 +0000402 sqliteVdbeAddOp(v, OP_Goto, 0, iContinue);
drhdf199a22002-06-14 22:38:41 +0000403 }
drh7b58dae2003-07-20 01:16:46 +0000404 if( p->iLimit>=0 ){
405 sqliteVdbeAddOp(v, OP_MemIncr, p->iLimit, iBreak);
drhdf199a22002-06-14 22:38:41 +0000406 }
407 }
408
drh967e8b72000-06-21 13:59:10 +0000409 /* Pull the requested columns.
drh22827922000-06-06 17:27:05 +0000410 */
drh38640e12002-07-05 21:42:36 +0000411 if( nColumn>0 ){
drh967e8b72000-06-21 13:59:10 +0000412 for(i=0; i<nColumn; i++){
drh99fcd712001-10-13 01:06:47 +0000413 sqliteVdbeAddOp(v, OP_Column, srcTab, i);
drh82c3d632000-06-06 21:56:07 +0000414 }
drh38640e12002-07-05 21:42:36 +0000415 }else{
416 nColumn = pEList->nExpr;
417 for(i=0; i<pEList->nExpr; i++){
418 sqliteExprCode(pParse, pEList->a[i].pExpr);
419 }
drh22827922000-06-06 17:27:05 +0000420 }
421
drhdaffd0e2001-04-11 14:28:42 +0000422 /* If the DISTINCT keyword was present on the SELECT statement
423 ** and this row has been seen before, then do not make this row
424 ** part of the result.
drh22827922000-06-06 17:27:05 +0000425 */
drhf5905aa2002-05-26 20:54:33 +0000426 if( distinct>=0 && pEList && pEList->nExpr>0 ){
drh0bd1f4e2002-06-06 18:54:39 +0000427#if NULL_ALWAYS_DISTINCT
428 sqliteVdbeAddOp(v, OP_IsNull, -pEList->nExpr, sqliteVdbeCurrentAddr(v)+7);
429#endif
drh99fcd712001-10-13 01:06:47 +0000430 sqliteVdbeAddOp(v, OP_MakeKey, pEList->nExpr, 1);
drh491791a2002-07-18 00:34:09 +0000431 if( pParse->db->file_format>=4 ) sqliteAddKeyType(v, pEList);
drhf5905aa2002-05-26 20:54:33 +0000432 sqliteVdbeAddOp(v, OP_Distinct, distinct, sqliteVdbeCurrentAddr(v)+3);
drh99fcd712001-10-13 01:06:47 +0000433 sqliteVdbeAddOp(v, OP_Pop, pEList->nExpr+1, 0);
434 sqliteVdbeAddOp(v, OP_Goto, 0, iContinue);
drh99fcd712001-10-13 01:06:47 +0000435 sqliteVdbeAddOp(v, OP_String, 0, 0);
drh6b125452002-01-28 15:53:03 +0000436 sqliteVdbeAddOp(v, OP_PutStrKey, distinct, 0);
drh22827922000-06-06 17:27:05 +0000437 }
drh82c3d632000-06-06 21:56:07 +0000438
drhc926afb2002-06-20 03:38:26 +0000439 switch( eDest ){
440 /* In this mode, write each query result to the key of the temporary
441 ** table iParm.
442 */
443 case SRT_Union: {
444 sqliteVdbeAddOp(v, OP_MakeRecord, nColumn, NULL_ALWAYS_DISTINCT);
445 sqliteVdbeAddOp(v, OP_String, 0, 0);
446 sqliteVdbeAddOp(v, OP_PutStrKey, iParm, 0);
447 break;
drh22827922000-06-06 17:27:05 +0000448 }
drh22827922000-06-06 17:27:05 +0000449
drhc926afb2002-06-20 03:38:26 +0000450 /* Store the result as data using a unique key.
451 */
452 case SRT_Table:
453 case SRT_TempTable: {
454 sqliteVdbeAddOp(v, OP_MakeRecord, nColumn, 0);
455 if( pOrderBy ){
456 pushOntoSorter(pParse, v, pOrderBy);
457 }else{
458 sqliteVdbeAddOp(v, OP_NewRecno, iParm, 0);
459 sqliteVdbeAddOp(v, OP_Pull, 1, 0);
460 sqliteVdbeAddOp(v, OP_PutIntKey, iParm, 0);
461 }
462 break;
463 }
drh82c3d632000-06-06 21:56:07 +0000464
drhc926afb2002-06-20 03:38:26 +0000465 /* Construct a record from the query result, but instead of
466 ** saving that record, use it as a key to delete elements from
467 ** the temporary table iParm.
468 */
469 case SRT_Except: {
470 int addr;
471 addr = sqliteVdbeAddOp(v, OP_MakeRecord, nColumn, NULL_ALWAYS_DISTINCT);
472 sqliteVdbeAddOp(v, OP_NotFound, iParm, addr+3);
473 sqliteVdbeAddOp(v, OP_Delete, iParm, 0);
474 break;
475 }
drh5974a302000-06-07 14:42:26 +0000476
drhc926afb2002-06-20 03:38:26 +0000477 /* If we are creating a set for an "expr IN (SELECT ...)" construct,
478 ** then there should be a single item on the stack. Write this
479 ** item into the set table with bogus data.
480 */
481 case SRT_Set: {
drh52b36ca2004-01-14 13:38:54 +0000482 int addr1 = sqliteVdbeCurrentAddr(v);
483 int addr2;
drhc926afb2002-06-20 03:38:26 +0000484 assert( nColumn==1 );
drh52b36ca2004-01-14 13:38:54 +0000485 sqliteVdbeAddOp(v, OP_NotNull, -1, addr1+3);
486 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
487 addr2 = sqliteVdbeAddOp(v, OP_Goto, 0, 0);
drhc926afb2002-06-20 03:38:26 +0000488 if( pOrderBy ){
489 pushOntoSorter(pParse, v, pOrderBy);
490 }else{
drha9f9d1c2002-06-29 02:20:08 +0000491 sqliteVdbeAddOp(v, OP_String, 0, 0);
drhc926afb2002-06-20 03:38:26 +0000492 sqliteVdbeAddOp(v, OP_PutStrKey, iParm, 0);
493 }
drh52b36ca2004-01-14 13:38:54 +0000494 sqliteVdbeChangeP2(v, addr2, sqliteVdbeCurrentAddr(v));
drhc926afb2002-06-20 03:38:26 +0000495 break;
496 }
drh22827922000-06-06 17:27:05 +0000497
drhc926afb2002-06-20 03:38:26 +0000498 /* If this is a scalar select that is part of an expression, then
499 ** store the results in the appropriate memory cell and break out
500 ** of the scan loop.
501 */
502 case SRT_Mem: {
503 assert( nColumn==1 );
504 if( pOrderBy ){
505 pushOntoSorter(pParse, v, pOrderBy);
506 }else{
507 sqliteVdbeAddOp(v, OP_MemStore, iParm, 1);
508 sqliteVdbeAddOp(v, OP_Goto, 0, iBreak);
509 }
510 break;
511 }
drh22827922000-06-06 17:27:05 +0000512
drhf46f9052002-06-22 02:33:38 +0000513 /* Send the data to the callback function.
514 */
515 case SRT_Callback:
516 case SRT_Sorter: {
517 if( pOrderBy ){
518 sqliteVdbeAddOp(v, OP_SortMakeRec, nColumn, 0);
519 pushOntoSorter(pParse, v, pOrderBy);
520 }else{
521 assert( eDest==SRT_Callback );
522 sqliteVdbeAddOp(v, OP_Callback, nColumn, 0);
523 }
524 break;
525 }
526
drh142e30d2002-08-28 03:00:58 +0000527 /* Invoke a subroutine to handle the results. The subroutine itself
528 ** is responsible for popping the results off of the stack.
529 */
530 case SRT_Subroutine: {
drhac82fcf2002-09-08 17:23:41 +0000531 if( pOrderBy ){
532 sqliteVdbeAddOp(v, OP_MakeRecord, nColumn, 0);
533 pushOntoSorter(pParse, v, pOrderBy);
534 }else{
535 sqliteVdbeAddOp(v, OP_Gosub, 0, iParm);
536 }
drh142e30d2002-08-28 03:00:58 +0000537 break;
538 }
539
drhc926afb2002-06-20 03:38:26 +0000540 /* Discard the results. This is used for SELECT statements inside
541 ** the body of a TRIGGER. The purpose of such selects is to call
542 ** user-defined functions that have side effects. We do not care
543 ** about the actual results of the select.
544 */
drhc926afb2002-06-20 03:38:26 +0000545 default: {
drhf46f9052002-06-22 02:33:38 +0000546 assert( eDest==SRT_Discard );
547 sqliteVdbeAddOp(v, OP_Pop, nColumn, 0);
drhc926afb2002-06-20 03:38:26 +0000548 break;
549 }
drh82c3d632000-06-06 21:56:07 +0000550 }
551 return 0;
552}
553
554/*
drhd8bc7082000-06-07 23:51:50 +0000555** If the inner loop was generated using a non-null pOrderBy argument,
556** then the results were placed in a sorter. After the loop is terminated
557** we need to run the sorter and output the results. The following
558** routine generates the code needed to do that.
559*/
drhc926afb2002-06-20 03:38:26 +0000560static void generateSortTail(
561 Select *p, /* The SELECT statement */
562 Vdbe *v, /* Generate code into this VDBE */
563 int nColumn, /* Number of columns of data */
564 int eDest, /* Write the sorted results here */
565 int iParm /* Optional parameter associated with eDest */
566){
drhd8bc7082000-06-07 23:51:50 +0000567 int end = sqliteVdbeMakeLabel(v);
568 int addr;
drhf46f9052002-06-22 02:33:38 +0000569 if( eDest==SRT_Sorter ) return;
drh99fcd712001-10-13 01:06:47 +0000570 sqliteVdbeAddOp(v, OP_Sort, 0, 0);
571 addr = sqliteVdbeAddOp(v, OP_SortNext, 0, end);
drh7b58dae2003-07-20 01:16:46 +0000572 if( p->iOffset>=0 ){
573 sqliteVdbeAddOp(v, OP_MemIncr, p->iOffset, addr+4);
drhd11d3822002-06-21 23:01:49 +0000574 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
575 sqliteVdbeAddOp(v, OP_Goto, 0, addr);
drhdf199a22002-06-14 22:38:41 +0000576 }
drh7b58dae2003-07-20 01:16:46 +0000577 if( p->iLimit>=0 ){
578 sqliteVdbeAddOp(v, OP_MemIncr, p->iLimit, end);
drhdf199a22002-06-14 22:38:41 +0000579 }
drhc926afb2002-06-20 03:38:26 +0000580 switch( eDest ){
581 case SRT_Callback: {
582 sqliteVdbeAddOp(v, OP_SortCallback, nColumn, 0);
583 break;
584 }
585 case SRT_Table:
586 case SRT_TempTable: {
587 sqliteVdbeAddOp(v, OP_NewRecno, iParm, 0);
588 sqliteVdbeAddOp(v, OP_Pull, 1, 0);
589 sqliteVdbeAddOp(v, OP_PutIntKey, iParm, 0);
590 break;
591 }
592 case SRT_Set: {
593 assert( nColumn==1 );
drh52b36ca2004-01-14 13:38:54 +0000594 sqliteVdbeAddOp(v, OP_NotNull, -1, sqliteVdbeCurrentAddr(v)+3);
595 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
596 sqliteVdbeAddOp(v, OP_Goto, 0, sqliteVdbeCurrentAddr(v)+3);
drhc926afb2002-06-20 03:38:26 +0000597 sqliteVdbeAddOp(v, OP_String, 0, 0);
598 sqliteVdbeAddOp(v, OP_PutStrKey, iParm, 0);
599 break;
600 }
601 case SRT_Mem: {
602 assert( nColumn==1 );
603 sqliteVdbeAddOp(v, OP_MemStore, iParm, 1);
604 sqliteVdbeAddOp(v, OP_Goto, 0, end);
605 break;
606 }
drhac82fcf2002-09-08 17:23:41 +0000607 case SRT_Subroutine: {
608 int i;
609 for(i=0; i<nColumn; i++){
610 sqliteVdbeAddOp(v, OP_Column, -1-i, i);
611 }
612 sqliteVdbeAddOp(v, OP_Gosub, 0, iParm);
613 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
614 break;
615 }
drhc926afb2002-06-20 03:38:26 +0000616 default: {
drhf46f9052002-06-22 02:33:38 +0000617 /* Do nothing */
drhc926afb2002-06-20 03:38:26 +0000618 break;
619 }
620 }
drh99fcd712001-10-13 01:06:47 +0000621 sqliteVdbeAddOp(v, OP_Goto, 0, addr);
622 sqliteVdbeResolveLabel(v, end);
drha8b38d22001-11-01 14:41:34 +0000623 sqliteVdbeAddOp(v, OP_SortReset, 0, 0);
drhd8bc7082000-06-07 23:51:50 +0000624}
625
626/*
drhfcb78a42003-01-18 20:11:05 +0000627** Generate code that will tell the VDBE the datatypes of
628** columns in the result set.
drhe78e8282003-01-19 03:59:45 +0000629**
630** This routine only generates code if the "PRAGMA show_datatypes=on"
631** has been executed. The datatypes are reported out in the azCol
632** parameter to the callback function. The first N azCol[] entries
633** are the names of the columns, and the second N entries are the
634** datatypes for the columns.
635**
636** The "datatype" for a result that is a column of a type is the
637** datatype definition extracted from the CREATE TABLE statement.
638** The datatype for an expression is either TEXT or NUMERIC. The
639** datatype for a ROWID field is INTEGER.
drhfcb78a42003-01-18 20:11:05 +0000640*/
641static void generateColumnTypes(
642 Parse *pParse, /* Parser context */
drhfcb78a42003-01-18 20:11:05 +0000643 SrcList *pTabList, /* List of tables */
644 ExprList *pEList /* Expressions defining the result set */
645){
646 Vdbe *v = pParse->pVdbe;
drh6a3ea0e2003-05-02 14:32:12 +0000647 int i, j;
drh326dce72003-01-29 14:06:07 +0000648 if( pParse->useCallback && (pParse->db->flags & SQLITE_ReportTypes)==0 ){
649 return;
650 }
drhfcb78a42003-01-18 20:11:05 +0000651 for(i=0; i<pEList->nExpr; i++){
652 Expr *p = pEList->a[i].pExpr;
653 char *zType = 0;
654 if( p==0 ) continue;
655 if( p->op==TK_COLUMN && pTabList ){
drh6a3ea0e2003-05-02 14:32:12 +0000656 Table *pTab;
drhfcb78a42003-01-18 20:11:05 +0000657 int iCol = p->iColumn;
drh6a3ea0e2003-05-02 14:32:12 +0000658 for(j=0; j<pTabList->nSrc && pTabList->a[j].iCursor!=p->iTable; j++){}
659 assert( j<pTabList->nSrc );
660 pTab = pTabList->a[j].pTab;
drhfcb78a42003-01-18 20:11:05 +0000661 if( iCol<0 ) iCol = pTab->iPKey;
662 assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
663 if( iCol<0 ){
664 zType = "INTEGER";
665 }else{
666 zType = pTab->aCol[iCol].zType;
667 }
668 }else{
669 if( sqliteExprType(p)==SQLITE_SO_TEXT ){
670 zType = "TEXT";
671 }else{
672 zType = "NUMERIC";
673 }
674 }
675 sqliteVdbeAddOp(v, OP_ColumnName, i + pEList->nExpr, 0);
676 sqliteVdbeChangeP3(v, -1, zType, P3_STATIC);
677 }
678}
679
680/*
681** Generate code that will tell the VDBE the names of columns
682** in the result set. This information is used to provide the
683** azCol[] vaolues in the callback.
drh82c3d632000-06-06 21:56:07 +0000684*/
drh832508b2002-03-02 17:04:07 +0000685static void generateColumnNames(
686 Parse *pParse, /* Parser context */
drhad3cab52002-05-24 02:04:32 +0000687 SrcList *pTabList, /* List of tables */
drh832508b2002-03-02 17:04:07 +0000688 ExprList *pEList /* Expressions defining the result set */
689){
drhd8bc7082000-06-07 23:51:50 +0000690 Vdbe *v = pParse->pVdbe;
drh6a3ea0e2003-05-02 14:32:12 +0000691 int i, j;
drhdaffd0e2001-04-11 14:28:42 +0000692 if( pParse->colNamesSet || v==0 || sqlite_malloc_failed ) return;
drhd8bc7082000-06-07 23:51:50 +0000693 pParse->colNamesSet = 1;
drh82c3d632000-06-06 21:56:07 +0000694 for(i=0; i<pEList->nExpr; i++){
695 Expr *p;
drhb1363202002-06-26 02:45:03 +0000696 char *zType = 0;
drh1bee3d72001-10-15 00:44:35 +0000697 int showFullNames;
drh5a387052003-01-11 14:19:51 +0000698 p = pEList->a[i].pExpr;
699 if( p==0 ) continue;
drh82c3d632000-06-06 21:56:07 +0000700 if( pEList->a[i].zName ){
701 char *zName = pEList->a[i].zName;
drh99fcd712001-10-13 01:06:47 +0000702 sqliteVdbeAddOp(v, OP_ColumnName, i, 0);
703 sqliteVdbeChangeP3(v, -1, zName, strlen(zName));
drh82c3d632000-06-06 21:56:07 +0000704 continue;
705 }
drh1bee3d72001-10-15 00:44:35 +0000706 showFullNames = (pParse->db->flags & SQLITE_FullColNames)!=0;
drhfa173a72002-07-10 21:26:00 +0000707 if( p->op==TK_COLUMN && pTabList ){
drh6a3ea0e2003-05-02 14:32:12 +0000708 Table *pTab;
drh97665872002-02-13 23:22:53 +0000709 char *zCol;
drh8aff1012001-12-22 14:49:24 +0000710 int iCol = p->iColumn;
drh6a3ea0e2003-05-02 14:32:12 +0000711 for(j=0; j<pTabList->nSrc && pTabList->a[j].iCursor!=p->iTable; j++){}
712 assert( j<pTabList->nSrc );
713 pTab = pTabList->a[j].pTab;
drh8aff1012001-12-22 14:49:24 +0000714 if( iCol<0 ) iCol = pTab->iPKey;
drh97665872002-02-13 23:22:53 +0000715 assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
drhb1363202002-06-26 02:45:03 +0000716 if( iCol<0 ){
717 zCol = "_ROWID_";
718 zType = "INTEGER";
719 }else{
720 zCol = pTab->aCol[iCol].zName;
721 zType = pTab->aCol[iCol].zType;
722 }
drh6977fea2002-10-22 23:38:04 +0000723 if( p->span.z && p->span.z[0] && !showFullNames ){
drhfa173a72002-07-10 21:26:00 +0000724 int addr = sqliteVdbeAddOp(v,OP_ColumnName, i, 0);
drh6977fea2002-10-22 23:38:04 +0000725 sqliteVdbeChangeP3(v, -1, p->span.z, p->span.n);
drhfa173a72002-07-10 21:26:00 +0000726 sqliteVdbeCompressSpace(v, addr);
727 }else if( pTabList->nSrc>1 || showFullNames ){
drh82c3d632000-06-06 21:56:07 +0000728 char *zName = 0;
drh82c3d632000-06-06 21:56:07 +0000729 char *zTab;
730
drh6a3ea0e2003-05-02 14:32:12 +0000731 zTab = pTabList->a[j].zAlias;
drh01a34662001-10-20 12:30:10 +0000732 if( showFullNames || zTab==0 ) zTab = pTab->zName;
drh97665872002-02-13 23:22:53 +0000733 sqliteSetString(&zName, zTab, ".", zCol, 0);
drh99fcd712001-10-13 01:06:47 +0000734 sqliteVdbeAddOp(v, OP_ColumnName, i, 0);
735 sqliteVdbeChangeP3(v, -1, zName, strlen(zName));
drh82c3d632000-06-06 21:56:07 +0000736 sqliteFree(zName);
737 }else{
drh99fcd712001-10-13 01:06:47 +0000738 sqliteVdbeAddOp(v, OP_ColumnName, i, 0);
drh22f70c32002-02-18 01:17:00 +0000739 sqliteVdbeChangeP3(v, -1, zCol, 0);
drh82c3d632000-06-06 21:56:07 +0000740 }
drh6977fea2002-10-22 23:38:04 +0000741 }else if( p->span.z && p->span.z[0] ){
drhfa173a72002-07-10 21:26:00 +0000742 int addr = sqliteVdbeAddOp(v,OP_ColumnName, i, 0);
drh6977fea2002-10-22 23:38:04 +0000743 sqliteVdbeChangeP3(v, -1, p->span.z, p->span.n);
drh1bee3d72001-10-15 00:44:35 +0000744 sqliteVdbeCompressSpace(v, addr);
745 }else{
746 char zName[30];
747 assert( p->op!=TK_COLUMN || pTabList==0 );
748 sprintf(zName, "column%d", i+1);
749 sqliteVdbeAddOp(v, OP_ColumnName, i, 0);
750 sqliteVdbeChangeP3(v, -1, zName, strlen(zName));
drh82c3d632000-06-06 21:56:07 +0000751 }
752 }
753}
754
755/*
drhd8bc7082000-06-07 23:51:50 +0000756** Name of the connection operator, used for error messages.
757*/
758static const char *selectOpName(int id){
759 char *z;
760 switch( id ){
761 case TK_ALL: z = "UNION ALL"; break;
762 case TK_INTERSECT: z = "INTERSECT"; break;
763 case TK_EXCEPT: z = "EXCEPT"; break;
764 default: z = "UNION"; break;
765 }
766 return z;
767}
768
769/*
drh315555c2002-10-20 15:53:03 +0000770** Forward declaration
771*/
772static int fillInColumnList(Parse*, Select*);
773
774/*
drh22f70c32002-02-18 01:17:00 +0000775** Given a SELECT statement, generate a Table structure that describes
776** the result set of that SELECT.
777*/
778Table *sqliteResultSetOfSelect(Parse *pParse, char *zTabName, Select *pSelect){
779 Table *pTab;
780 int i;
781 ExprList *pEList;
drh22f70c32002-02-18 01:17:00 +0000782
783 if( fillInColumnList(pParse, pSelect) ){
784 return 0;
785 }
786 pTab = sqliteMalloc( sizeof(Table) );
787 if( pTab==0 ){
788 return 0;
789 }
790 pTab->zName = zTabName ? sqliteStrDup(zTabName) : 0;
791 pEList = pSelect->pEList;
792 pTab->nCol = pEList->nExpr;
drh417be792002-03-03 18:59:40 +0000793 assert( pTab->nCol>0 );
drh22f70c32002-02-18 01:17:00 +0000794 pTab->aCol = sqliteMalloc( sizeof(pTab->aCol[0])*pTab->nCol );
795 for(i=0; i<pTab->nCol; i++){
796 Expr *p;
797 if( pEList->a[i].zName ){
798 pTab->aCol[i].zName = sqliteStrDup(pEList->a[i].zName);
drh6977fea2002-10-22 23:38:04 +0000799 }else if( (p=pEList->a[i].pExpr)->span.z && p->span.z[0] ){
800 sqliteSetNString(&pTab->aCol[i].zName, p->span.z, p->span.n, 0);
drhd820cb12002-02-18 03:21:45 +0000801 }else if( p->op==TK_DOT && p->pRight && p->pRight->token.z &&
802 p->pRight->token.z[0] ){
803 sqliteSetNString(&pTab->aCol[i].zName,
804 p->pRight->token.z, p->pRight->token.n, 0);
drh22f70c32002-02-18 01:17:00 +0000805 }else{
806 char zBuf[30];
807 sprintf(zBuf, "column%d", i+1);
808 pTab->aCol[i].zName = sqliteStrDup(zBuf);
809 }
810 }
811 pTab->iPKey = -1;
812 return pTab;
813}
814
815/*
drhad2d8302002-05-24 20:31:36 +0000816** For the given SELECT statement, do three things.
drhd8bc7082000-06-07 23:51:50 +0000817**
drhad3cab52002-05-24 02:04:32 +0000818** (1) Fill in the pTabList->a[].pTab fields in the SrcList that
drh63eb5f22003-04-29 16:20:44 +0000819** defines the set of tables that should be scanned. For views,
820** fill pTabList->a[].pSelect with a copy of the SELECT statement
821** that implements the view. A copy is made of the view's SELECT
822** statement so that we can freely modify or delete that statement
823** without worrying about messing up the presistent representation
824** of the view.
drhd8bc7082000-06-07 23:51:50 +0000825**
drhad2d8302002-05-24 20:31:36 +0000826** (2) Add terms to the WHERE clause to accomodate the NATURAL keyword
827** on joins and the ON and USING clause of joins.
828**
829** (3) Scan the list of columns in the result set (pEList) looking
drh54473222002-04-04 02:10:55 +0000830** for instances of the "*" operator or the TABLE.* operator.
831** If found, expand each "*" to be every column in every table
832** and TABLE.* to be every column in TABLE.
drhd8bc7082000-06-07 23:51:50 +0000833**
834** Return 0 on success. If there are problems, leave an error message
835** in pParse and return non-zero.
836*/
837static int fillInColumnList(Parse *pParse, Select *p){
drh54473222002-04-04 02:10:55 +0000838 int i, j, k, rc;
drhad3cab52002-05-24 02:04:32 +0000839 SrcList *pTabList;
drhdaffd0e2001-04-11 14:28:42 +0000840 ExprList *pEList;
drha76b5df2002-02-23 02:32:10 +0000841 Table *pTab;
drhdaffd0e2001-04-11 14:28:42 +0000842
843 if( p==0 || p->pSrc==0 ) return 1;
844 pTabList = p->pSrc;
845 pEList = p->pEList;
drhd8bc7082000-06-07 23:51:50 +0000846
847 /* Look up every table in the table list.
848 */
drhad3cab52002-05-24 02:04:32 +0000849 for(i=0; i<pTabList->nSrc; i++){
drhd8bc7082000-06-07 23:51:50 +0000850 if( pTabList->a[i].pTab ){
851 /* This routine has run before! No need to continue */
852 return 0;
853 }
drhdaffd0e2001-04-11 14:28:42 +0000854 if( pTabList->a[i].zName==0 ){
drh22f70c32002-02-18 01:17:00 +0000855 /* A sub-query in the FROM clause of a SELECT */
drh22f70c32002-02-18 01:17:00 +0000856 assert( pTabList->a[i].pSelect!=0 );
drhad2d8302002-05-24 20:31:36 +0000857 if( pTabList->a[i].zAlias==0 ){
858 char zFakeName[60];
859 sprintf(zFakeName, "sqlite_subquery_%p_",
860 (void*)pTabList->a[i].pSelect);
861 sqliteSetString(&pTabList->a[i].zAlias, zFakeName, 0);
862 }
drh22f70c32002-02-18 01:17:00 +0000863 pTabList->a[i].pTab = pTab =
864 sqliteResultSetOfSelect(pParse, pTabList->a[i].zAlias,
865 pTabList->a[i].pSelect);
866 if( pTab==0 ){
867 return 1;
868 }
drh5cf590c2003-04-24 01:45:04 +0000869 /* The isTransient flag indicates that the Table structure has been
870 ** dynamically allocated and may be freed at any time. In other words,
871 ** pTab is not pointing to a persistent table structure that defines
872 ** part of the schema. */
drh22f70c32002-02-18 01:17:00 +0000873 pTab->isTransient = 1;
874 }else{
drha76b5df2002-02-23 02:32:10 +0000875 /* An ordinary table or view name in the FROM clause */
876 pTabList->a[i].pTab = pTab =
drha69d9162003-04-17 22:57:53 +0000877 sqliteLocateTable(pParse,pTabList->a[i].zName,pTabList->a[i].zDatabase);
drha76b5df2002-02-23 02:32:10 +0000878 if( pTab==0 ){
drh22f70c32002-02-18 01:17:00 +0000879 return 1;
880 }
drha76b5df2002-02-23 02:32:10 +0000881 if( pTab->pSelect ){
drh63eb5f22003-04-29 16:20:44 +0000882 /* We reach here if the named table is a really a view */
drh417be792002-03-03 18:59:40 +0000883 if( sqliteViewGetColumnNames(pParse, pTab) ){
884 return 1;
885 }
drh63eb5f22003-04-29 16:20:44 +0000886 /* If pTabList->a[i].pSelect!=0 it means we are dealing with a
887 ** view within a view. The SELECT structure has already been
888 ** copied by the outer view so we can skip the copy step here
889 ** in the inner view.
890 */
891 if( pTabList->a[i].pSelect==0 ){
892 pTabList->a[i].pSelect = sqliteSelectDup(pTab->pSelect);
893 }
drha76b5df2002-02-23 02:32:10 +0000894 }
drhd8bc7082000-06-07 23:51:50 +0000895 }
896 }
897
drhad2d8302002-05-24 20:31:36 +0000898 /* Process NATURAL keywords, and ON and USING clauses of joins.
899 */
900 if( sqliteProcessJoin(pParse, p) ) return 1;
901
drh7c917d12001-12-16 20:05:05 +0000902 /* For every "*" that occurs in the column list, insert the names of
drh54473222002-04-04 02:10:55 +0000903 ** all columns in all tables. And for every TABLE.* insert the names
904 ** of all columns in TABLE. The parser inserted a special expression
drh7c917d12001-12-16 20:05:05 +0000905 ** with the TK_ALL operator for each "*" that it found in the column list.
906 ** The following code just has to locate the TK_ALL expressions and expand
907 ** each one to the list of all columns in all tables.
drh54473222002-04-04 02:10:55 +0000908 **
909 ** The first loop just checks to see if there are any "*" operators
910 ** that need expanding.
drhd8bc7082000-06-07 23:51:50 +0000911 */
drh7c917d12001-12-16 20:05:05 +0000912 for(k=0; k<pEList->nExpr; k++){
drh54473222002-04-04 02:10:55 +0000913 Expr *pE = pEList->a[k].pExpr;
914 if( pE->op==TK_ALL ) break;
915 if( pE->op==TK_DOT && pE->pRight && pE->pRight->op==TK_ALL
916 && pE->pLeft && pE->pLeft->op==TK_ID ) break;
drh7c917d12001-12-16 20:05:05 +0000917 }
drh54473222002-04-04 02:10:55 +0000918 rc = 0;
drh7c917d12001-12-16 20:05:05 +0000919 if( k<pEList->nExpr ){
drh54473222002-04-04 02:10:55 +0000920 /*
921 ** If we get here it means the result set contains one or more "*"
922 ** operators that need to be expanded. Loop through each expression
923 ** in the result set and expand them one by one.
924 */
drh7c917d12001-12-16 20:05:05 +0000925 struct ExprList_item *a = pEList->a;
926 ExprList *pNew = 0;
927 for(k=0; k<pEList->nExpr; k++){
drh54473222002-04-04 02:10:55 +0000928 Expr *pE = a[k].pExpr;
929 if( pE->op!=TK_ALL &&
930 (pE->op!=TK_DOT || pE->pRight==0 || pE->pRight->op!=TK_ALL) ){
931 /* This particular expression does not need to be expanded.
932 */
drh7c917d12001-12-16 20:05:05 +0000933 pNew = sqliteExprListAppend(pNew, a[k].pExpr, 0);
934 pNew->a[pNew->nExpr-1].zName = a[k].zName;
935 a[k].pExpr = 0;
936 a[k].zName = 0;
937 }else{
drh54473222002-04-04 02:10:55 +0000938 /* This expression is a "*" or a "TABLE.*" and needs to be
939 ** expanded. */
940 int tableSeen = 0; /* Set to 1 when TABLE matches */
941 Token *pName; /* text of name of TABLE */
942 if( pE->op==TK_DOT && pE->pLeft ){
943 pName = &pE->pLeft->token;
944 }else{
945 pName = 0;
946 }
drhad3cab52002-05-24 02:04:32 +0000947 for(i=0; i<pTabList->nSrc; i++){
drh7c917d12001-12-16 20:05:05 +0000948 Table *pTab = pTabList->a[i].pTab;
drh54473222002-04-04 02:10:55 +0000949 char *zTabName = pTabList->a[i].zAlias;
950 if( zTabName==0 || zTabName[0]==0 ){
951 zTabName = pTab->zName;
952 }
drhc754fa52002-05-27 03:25:51 +0000953 if( pName && (zTabName==0 || zTabName[0]==0 ||
954 sqliteStrNICmp(pName->z, zTabName, pName->n)!=0 ||
955 zTabName[pName->n]!=0) ){
drh54473222002-04-04 02:10:55 +0000956 continue;
957 }
958 tableSeen = 1;
drh7c917d12001-12-16 20:05:05 +0000959 for(j=0; j<pTab->nCol; j++){
drh22f70c32002-02-18 01:17:00 +0000960 Expr *pExpr, *pLeft, *pRight;
drhad2d8302002-05-24 20:31:36 +0000961 char *zName = pTab->aCol[j].zName;
962
963 if( i>0 && (pTabList->a[i-1].jointype & JT_NATURAL)!=0 &&
964 columnIndex(pTabList->a[i-1].pTab, zName)>=0 ){
965 /* In a NATURAL join, omit the join columns from the
966 ** table on the right */
967 continue;
968 }
969 if( i>0 && sqliteIdListIndex(pTabList->a[i-1].pUsing, zName)>=0 ){
970 /* In a join with a USING clause, omit columns in the
971 ** using clause from the table on the right. */
972 continue;
973 }
drh22f70c32002-02-18 01:17:00 +0000974 pRight = sqliteExpr(TK_ID, 0, 0, 0);
975 if( pRight==0 ) break;
drhad2d8302002-05-24 20:31:36 +0000976 pRight->token.z = zName;
977 pRight->token.n = strlen(zName);
drh4b59ab52002-08-24 18:24:51 +0000978 pRight->token.dyn = 0;
drh4b59ab52002-08-24 18:24:51 +0000979 if( zTabName && pTabList->nSrc>1 ){
drh22f70c32002-02-18 01:17:00 +0000980 pLeft = sqliteExpr(TK_ID, 0, 0, 0);
drh22f70c32002-02-18 01:17:00 +0000981 pExpr = sqliteExpr(TK_DOT, pLeft, pRight, 0);
982 if( pExpr==0 ) break;
drh4b59ab52002-08-24 18:24:51 +0000983 pLeft->token.z = zTabName;
984 pLeft->token.n = strlen(zTabName);
985 pLeft->token.dyn = 0;
drh6977fea2002-10-22 23:38:04 +0000986 sqliteSetString((char**)&pExpr->span.z, zTabName, ".", zName, 0);
987 pExpr->span.n = strlen(pExpr->span.z);
988 pExpr->span.dyn = 1;
989 pExpr->token.z = 0;
990 pExpr->token.n = 0;
991 pExpr->token.dyn = 0;
drh7c917d12001-12-16 20:05:05 +0000992 }else{
drh22f70c32002-02-18 01:17:00 +0000993 pExpr = pRight;
drh6977fea2002-10-22 23:38:04 +0000994 pExpr->span = pExpr->token;
drh7c917d12001-12-16 20:05:05 +0000995 }
drh7c917d12001-12-16 20:05:05 +0000996 pNew = sqliteExprListAppend(pNew, pExpr, 0);
997 }
drh17e24df2001-11-06 14:10:41 +0000998 }
drh54473222002-04-04 02:10:55 +0000999 if( !tableSeen ){
drhf5db2d32002-06-06 23:42:27 +00001000 if( pName ){
drhda93d232003-03-31 02:12:46 +00001001 sqliteErrorMsg(pParse, "no such table: %T", pName);
drhf5db2d32002-06-06 23:42:27 +00001002 }else{
drhda93d232003-03-31 02:12:46 +00001003 sqliteErrorMsg(pParse, "no tables specified");
drhf5db2d32002-06-06 23:42:27 +00001004 }
drh54473222002-04-04 02:10:55 +00001005 rc = 1;
1006 }
drhd8bc7082000-06-07 23:51:50 +00001007 }
1008 }
drh7c917d12001-12-16 20:05:05 +00001009 sqliteExprListDelete(pEList);
1010 p->pEList = pNew;
drhd8bc7082000-06-07 23:51:50 +00001011 }
drh54473222002-04-04 02:10:55 +00001012 return rc;
drhd8bc7082000-06-07 23:51:50 +00001013}
1014
1015/*
drhff78bd22002-02-27 01:47:11 +00001016** This routine recursively unlinks the Select.pSrc.a[].pTab pointers
1017** in a select structure. It just sets the pointers to NULL. This
1018** routine is recursive in the sense that if the Select.pSrc.a[].pSelect
1019** pointer is not NULL, this routine is called recursively on that pointer.
1020**
1021** This routine is called on the Select structure that defines a
1022** VIEW in order to undo any bindings to tables. This is necessary
1023** because those tables might be DROPed by a subsequent SQL command.
drh5cf590c2003-04-24 01:45:04 +00001024** If the bindings are not removed, then the Select.pSrc->a[].pTab field
1025** will be left pointing to a deallocated Table structure after the
1026** DROP and a coredump will occur the next time the VIEW is used.
drhff78bd22002-02-27 01:47:11 +00001027*/
1028void sqliteSelectUnbind(Select *p){
1029 int i;
drhad3cab52002-05-24 02:04:32 +00001030 SrcList *pSrc = p->pSrc;
drhff78bd22002-02-27 01:47:11 +00001031 Table *pTab;
1032 if( p==0 ) return;
drhad3cab52002-05-24 02:04:32 +00001033 for(i=0; i<pSrc->nSrc; i++){
drhff78bd22002-02-27 01:47:11 +00001034 if( (pTab = pSrc->a[i].pTab)!=0 ){
1035 if( pTab->isTransient ){
1036 sqliteDeleteTable(0, pTab);
drhff78bd22002-02-27 01:47:11 +00001037 }
1038 pSrc->a[i].pTab = 0;
1039 if( pSrc->a[i].pSelect ){
1040 sqliteSelectUnbind(pSrc->a[i].pSelect);
1041 }
1042 }
1043 }
1044}
1045
1046/*
drhd8bc7082000-06-07 23:51:50 +00001047** This routine associates entries in an ORDER BY expression list with
1048** columns in a result. For each ORDER BY expression, the opcode of
drh967e8b72000-06-21 13:59:10 +00001049** the top-level node is changed to TK_COLUMN and the iColumn value of
drhd8bc7082000-06-07 23:51:50 +00001050** the top-level node is filled in with column number and the iTable
1051** value of the top-level node is filled with iTable parameter.
1052**
1053** If there are prior SELECT clauses, they are processed first. A match
1054** in an earlier SELECT takes precedence over a later SELECT.
1055**
1056** Any entry that does not match is flagged as an error. The number
1057** of errors is returned.
drhfcb78a42003-01-18 20:11:05 +00001058**
1059** This routine does NOT correctly initialize the Expr.dataType field
1060** of the ORDER BY expressions. The multiSelectSortOrder() routine
1061** must be called to do that after the individual select statements
1062** have all been analyzed. This routine is unable to compute Expr.dataType
1063** because it must be called before the individual select statements
1064** have been analyzed.
drhd8bc7082000-06-07 23:51:50 +00001065*/
1066static int matchOrderbyToColumn(
1067 Parse *pParse, /* A place to leave error messages */
1068 Select *pSelect, /* Match to result columns of this SELECT */
1069 ExprList *pOrderBy, /* The ORDER BY values to match against columns */
drhe4de1fe2002-06-02 16:09:01 +00001070 int iTable, /* Insert this value in iTable */
drhd8bc7082000-06-07 23:51:50 +00001071 int mustComplete /* If TRUE all ORDER BYs must match */
1072){
1073 int nErr = 0;
1074 int i, j;
1075 ExprList *pEList;
1076
drhdaffd0e2001-04-11 14:28:42 +00001077 if( pSelect==0 || pOrderBy==0 ) return 1;
drhd8bc7082000-06-07 23:51:50 +00001078 if( mustComplete ){
1079 for(i=0; i<pOrderBy->nExpr; i++){ pOrderBy->a[i].done = 0; }
1080 }
1081 if( fillInColumnList(pParse, pSelect) ){
1082 return 1;
1083 }
1084 if( pSelect->pPrior ){
drh92cd52f2000-06-08 01:55:29 +00001085 if( matchOrderbyToColumn(pParse, pSelect->pPrior, pOrderBy, iTable, 0) ){
1086 return 1;
1087 }
drhd8bc7082000-06-07 23:51:50 +00001088 }
1089 pEList = pSelect->pEList;
1090 for(i=0; i<pOrderBy->nExpr; i++){
1091 Expr *pE = pOrderBy->a[i].pExpr;
drhe4de1fe2002-06-02 16:09:01 +00001092 int iCol = -1;
drhd8bc7082000-06-07 23:51:50 +00001093 if( pOrderBy->a[i].done ) continue;
drhe4de1fe2002-06-02 16:09:01 +00001094 if( sqliteExprIsInteger(pE, &iCol) ){
1095 if( iCol<=0 || iCol>pEList->nExpr ){
drhda93d232003-03-31 02:12:46 +00001096 sqliteErrorMsg(pParse,
1097 "ORDER BY position %d should be between 1 and %d",
1098 iCol, pEList->nExpr);
drhe4de1fe2002-06-02 16:09:01 +00001099 nErr++;
1100 break;
1101 }
drhfcb78a42003-01-18 20:11:05 +00001102 if( !mustComplete ) continue;
drhe4de1fe2002-06-02 16:09:01 +00001103 iCol--;
1104 }
1105 for(j=0; iCol<0 && j<pEList->nExpr; j++){
drh4cfa7932000-06-08 15:10:46 +00001106 if( pEList->a[j].zName && (pE->op==TK_ID || pE->op==TK_STRING) ){
drha76b5df2002-02-23 02:32:10 +00001107 char *zName, *zLabel;
1108 zName = pEList->a[j].zName;
1109 assert( pE->token.z );
1110 zLabel = sqliteStrNDup(pE->token.z, pE->token.n);
drhd8bc7082000-06-07 23:51:50 +00001111 sqliteDequote(zLabel);
1112 if( sqliteStrICmp(zName, zLabel)==0 ){
drhe4de1fe2002-06-02 16:09:01 +00001113 iCol = j;
drhd8bc7082000-06-07 23:51:50 +00001114 }
drh6e142f52000-06-08 13:36:40 +00001115 sqliteFree(zLabel);
drhd8bc7082000-06-07 23:51:50 +00001116 }
drhe4de1fe2002-06-02 16:09:01 +00001117 if( iCol<0 && sqliteExprCompare(pE, pEList->a[j].pExpr) ){
1118 iCol = j;
drhd8bc7082000-06-07 23:51:50 +00001119 }
1120 }
drhe4de1fe2002-06-02 16:09:01 +00001121 if( iCol>=0 ){
1122 pE->op = TK_COLUMN;
1123 pE->iColumn = iCol;
1124 pE->iTable = iTable;
1125 pOrderBy->a[i].done = 1;
1126 }
1127 if( iCol<0 && mustComplete ){
drhda93d232003-03-31 02:12:46 +00001128 sqliteErrorMsg(pParse,
1129 "ORDER BY term number %d does not match any result column", i+1);
drhd8bc7082000-06-07 23:51:50 +00001130 nErr++;
1131 break;
1132 }
1133 }
1134 return nErr;
1135}
1136
1137/*
1138** Get a VDBE for the given parser context. Create a new one if necessary.
1139** If an error occurs, return NULL and leave a message in pParse.
1140*/
1141Vdbe *sqliteGetVdbe(Parse *pParse){
1142 Vdbe *v = pParse->pVdbe;
1143 if( v==0 ){
drh4c504392000-10-16 22:06:40 +00001144 v = pParse->pVdbe = sqliteVdbeCreate(pParse->db);
drhd8bc7082000-06-07 23:51:50 +00001145 }
drhd8bc7082000-06-07 23:51:50 +00001146 return v;
1147}
drhfcb78a42003-01-18 20:11:05 +00001148
1149/*
1150** This routine sets the Expr.dataType field on all elements of
1151** the pOrderBy expression list. The pOrderBy list will have been
1152** set up by matchOrderbyToColumn(). Hence each expression has
1153** a TK_COLUMN as its root node. The Expr.iColumn refers to a
1154** column in the result set. The datatype is set to SQLITE_SO_TEXT
1155** if the corresponding column in p and every SELECT to the left of
1156** p has a datatype of SQLITE_SO_TEXT. If the cooressponding column
1157** in p or any of the left SELECTs is SQLITE_SO_NUM, then the datatype
1158** of the order-by expression is set to SQLITE_SO_NUM.
1159**
1160** Examples:
1161**
drhe78e8282003-01-19 03:59:45 +00001162** CREATE TABLE one(a INTEGER, b TEXT);
1163** CREATE TABLE two(c VARCHAR(5), d FLOAT);
1164**
1165** SELECT b, b FROM one UNION SELECT d, c FROM two ORDER BY 1, 2;
1166**
1167** The primary sort key will use SQLITE_SO_NUM because the "d" in
1168** the second SELECT is numeric. The 1st column of the first SELECT
1169** is text but that does not matter because a numeric always overrides
1170** a text.
1171**
1172** The secondary key will use the SQLITE_SO_TEXT sort order because
1173** both the (second) "b" in the first SELECT and the "c" in the second
1174** SELECT have a datatype of text.
drhfcb78a42003-01-18 20:11:05 +00001175*/
1176static void multiSelectSortOrder(Select *p, ExprList *pOrderBy){
1177 int i;
1178 ExprList *pEList;
1179 if( pOrderBy==0 ) return;
1180 if( p==0 ){
1181 for(i=0; i<pOrderBy->nExpr; i++){
1182 pOrderBy->a[i].pExpr->dataType = SQLITE_SO_TEXT;
1183 }
1184 return;
1185 }
1186 multiSelectSortOrder(p->pPrior, pOrderBy);
1187 pEList = p->pEList;
1188 for(i=0; i<pOrderBy->nExpr; i++){
1189 Expr *pE = pOrderBy->a[i].pExpr;
1190 if( pE->dataType==SQLITE_SO_NUM ) continue;
1191 assert( pE->iColumn>=0 );
1192 if( pEList->nExpr>pE->iColumn ){
1193 pE->dataType = sqliteExprType(pEList->a[pE->iColumn].pExpr);
1194 }
1195 }
1196}
drhd8bc7082000-06-07 23:51:50 +00001197
1198/*
drh7b58dae2003-07-20 01:16:46 +00001199** Compute the iLimit and iOffset fields of the SELECT based on the
1200** nLimit and nOffset fields. nLimit and nOffset hold the integers
1201** that appear in the original SQL statement after the LIMIT and OFFSET
1202** keywords. Or that hold -1 and 0 if those keywords are omitted.
1203** iLimit and iOffset are the integer memory register numbers for
1204** counters used to compute the limit and offset. If there is no
1205** limit and/or offset, then iLimit and iOffset are negative.
1206**
1207** This routine changes the values if iLimit and iOffset only if
1208** a limit or offset is defined by nLimit and nOffset. iLimit and
1209** iOffset should have been preset to appropriate default values
1210** (usually but not always -1) prior to calling this routine.
1211** Only if nLimit>=0 or nOffset>0 do the limit registers get
1212** redefined. The UNION ALL operator uses this property to force
1213** the reuse of the same limit and offset registers across multiple
1214** SELECT statements.
1215*/
1216static void computeLimitRegisters(Parse *pParse, Select *p){
1217 /*
1218 ** If the comparison is p->nLimit>0 then "LIMIT 0" shows
1219 ** all rows. It is the same as no limit. If the comparision is
1220 ** p->nLimit>=0 then "LIMIT 0" show no rows at all.
1221 ** "LIMIT -1" always shows all rows. There is some
1222 ** contraversy about what the correct behavior should be.
1223 ** The current implementation interprets "LIMIT 0" to mean
1224 ** no rows.
1225 */
1226 if( p->nLimit>=0 ){
1227 int iMem = pParse->nMem++;
1228 Vdbe *v = sqliteGetVdbe(pParse);
1229 if( v==0 ) return;
1230 sqliteVdbeAddOp(v, OP_Integer, -p->nLimit, 0);
1231 sqliteVdbeAddOp(v, OP_MemStore, iMem, 1);
1232 p->iLimit = iMem;
1233 }
1234 if( p->nOffset>0 ){
1235 int iMem = pParse->nMem++;
1236 Vdbe *v = sqliteGetVdbe(pParse);
1237 if( v==0 ) return;
1238 sqliteVdbeAddOp(v, OP_Integer, -p->nOffset, 0);
1239 sqliteVdbeAddOp(v, OP_MemStore, iMem, 1);
1240 p->iOffset = iMem;
1241 }
1242}
1243
1244/*
drh82c3d632000-06-06 21:56:07 +00001245** This routine is called to process a query that is really the union
1246** or intersection of two or more separate queries.
drhc926afb2002-06-20 03:38:26 +00001247**
drhe78e8282003-01-19 03:59:45 +00001248** "p" points to the right-most of the two queries. the query on the
1249** left is p->pPrior. The left query could also be a compound query
1250** in which case this routine will be called recursively.
1251**
1252** The results of the total query are to be written into a destination
1253** of type eDest with parameter iParm.
1254**
1255** Example 1: Consider a three-way compound SQL statement.
1256**
1257** SELECT a FROM t1 UNION SELECT b FROM t2 UNION SELECT c FROM t3
1258**
1259** This statement is parsed up as follows:
1260**
1261** SELECT c FROM t3
1262** |
1263** `-----> SELECT b FROM t2
1264** |
jplyon4b11c6d2004-01-19 04:57:53 +00001265** `------> SELECT a FROM t1
drhe78e8282003-01-19 03:59:45 +00001266**
1267** The arrows in the diagram above represent the Select.pPrior pointer.
1268** So if this routine is called with p equal to the t3 query, then
1269** pPrior will be the t2 query. p->op will be TK_UNION in this case.
1270**
1271** Notice that because of the way SQLite parses compound SELECTs, the
1272** individual selects always group from left to right.
drh82c3d632000-06-06 21:56:07 +00001273*/
1274static int multiSelect(Parse *pParse, Select *p, int eDest, int iParm){
drh10e5e3c2000-06-08 00:19:02 +00001275 int rc; /* Success code from a subroutine */
1276 Select *pPrior; /* Another SELECT immediately to our left */
1277 Vdbe *v; /* Generate code to this VDBE */
drh82c3d632000-06-06 21:56:07 +00001278
drh7b58dae2003-07-20 01:16:46 +00001279 /* Make sure there is no ORDER BY or LIMIT clause on prior SELECTs. Only
1280 ** the last SELECT in the series may have an ORDER BY or LIMIT.
drh82c3d632000-06-06 21:56:07 +00001281 */
drhdaffd0e2001-04-11 14:28:42 +00001282 if( p==0 || p->pPrior==0 ) return 1;
drhd8bc7082000-06-07 23:51:50 +00001283 pPrior = p->pPrior;
1284 if( pPrior->pOrderBy ){
drhda93d232003-03-31 02:12:46 +00001285 sqliteErrorMsg(pParse,"ORDER BY clause should come after %s not before",
1286 selectOpName(p->op));
drh82c3d632000-06-06 21:56:07 +00001287 return 1;
1288 }
drh7b58dae2003-07-20 01:16:46 +00001289 if( pPrior->nLimit>=0 || pPrior->nOffset>0 ){
1290 sqliteErrorMsg(pParse,"LIMIT clause should come after %s not before",
1291 selectOpName(p->op));
1292 return 1;
1293 }
drh82c3d632000-06-06 21:56:07 +00001294
drhd8bc7082000-06-07 23:51:50 +00001295 /* Make sure we have a valid query engine. If not, create a new one.
1296 */
1297 v = sqliteGetVdbe(pParse);
1298 if( v==0 ) return 1;
1299
drh1cc3d752002-03-23 00:31:29 +00001300 /* Create the destination temporary table if necessary
1301 */
1302 if( eDest==SRT_TempTable ){
1303 sqliteVdbeAddOp(v, OP_OpenTemp, iParm, 0);
1304 eDest = SRT_Table;
1305 }
1306
drhf46f9052002-06-22 02:33:38 +00001307 /* Generate code for the left and right SELECT statements.
drhd8bc7082000-06-07 23:51:50 +00001308 */
drh82c3d632000-06-06 21:56:07 +00001309 switch( p->op ){
drhf46f9052002-06-22 02:33:38 +00001310 case TK_ALL: {
1311 if( p->pOrderBy==0 ){
drh7b58dae2003-07-20 01:16:46 +00001312 pPrior->nLimit = p->nLimit;
1313 pPrior->nOffset = p->nOffset;
drhf46f9052002-06-22 02:33:38 +00001314 rc = sqliteSelect(pParse, pPrior, eDest, iParm, 0, 0, 0);
1315 if( rc ) return rc;
1316 p->pPrior = 0;
drh7b58dae2003-07-20 01:16:46 +00001317 p->iLimit = pPrior->iLimit;
1318 p->iOffset = pPrior->iOffset;
1319 p->nLimit = -1;
1320 p->nOffset = 0;
drhf46f9052002-06-22 02:33:38 +00001321 rc = sqliteSelect(pParse, p, eDest, iParm, 0, 0, 0);
1322 p->pPrior = pPrior;
1323 if( rc ) return rc;
1324 break;
1325 }
1326 /* For UNION ALL ... ORDER BY fall through to the next case */
1327 }
drh82c3d632000-06-06 21:56:07 +00001328 case TK_EXCEPT:
1329 case TK_UNION: {
drhd8bc7082000-06-07 23:51:50 +00001330 int unionTab; /* Cursor number of the temporary table holding result */
1331 int op; /* One of the SRT_ operations to apply to self */
1332 int priorOp; /* The SRT_ operation to apply to prior selects */
drh7b58dae2003-07-20 01:16:46 +00001333 int nLimit, nOffset; /* Saved values of p->nLimit and p->nOffset */
drhc926afb2002-06-20 03:38:26 +00001334 ExprList *pOrderBy; /* The ORDER BY clause for the right SELECT */
drh82c3d632000-06-06 21:56:07 +00001335
drhd8bc7082000-06-07 23:51:50 +00001336 priorOp = p->op==TK_ALL ? SRT_Table : SRT_Union;
drh7b58dae2003-07-20 01:16:46 +00001337 if( eDest==priorOp && p->pOrderBy==0 && p->nLimit<0 && p->nOffset==0 ){
drhd8bc7082000-06-07 23:51:50 +00001338 /* We can reuse a temporary table generated by a SELECT to our
drhc926afb2002-06-20 03:38:26 +00001339 ** right.
drhd8bc7082000-06-07 23:51:50 +00001340 */
drh82c3d632000-06-06 21:56:07 +00001341 unionTab = iParm;
1342 }else{
drhd8bc7082000-06-07 23:51:50 +00001343 /* We will need to create our own temporary table to hold the
1344 ** intermediate results.
1345 */
1346 unionTab = pParse->nTab++;
1347 if( p->pOrderBy
1348 && matchOrderbyToColumn(pParse, p, p->pOrderBy, unionTab, 1) ){
1349 return 1;
1350 }
drhd8bc7082000-06-07 23:51:50 +00001351 if( p->op!=TK_ALL ){
drhc6b52df2002-01-04 03:09:29 +00001352 sqliteVdbeAddOp(v, OP_OpenTemp, unionTab, 1);
drh99fcd712001-10-13 01:06:47 +00001353 sqliteVdbeAddOp(v, OP_KeyAsData, unionTab, 1);
drh345fda32001-01-15 22:51:08 +00001354 }else{
drh99fcd712001-10-13 01:06:47 +00001355 sqliteVdbeAddOp(v, OP_OpenTemp, unionTab, 0);
drhd8bc7082000-06-07 23:51:50 +00001356 }
drh82c3d632000-06-06 21:56:07 +00001357 }
drhd8bc7082000-06-07 23:51:50 +00001358
1359 /* Code the SELECT statements to our left
1360 */
drh832508b2002-03-02 17:04:07 +00001361 rc = sqliteSelect(pParse, pPrior, priorOp, unionTab, 0, 0, 0);
drh82c3d632000-06-06 21:56:07 +00001362 if( rc ) return rc;
drhd8bc7082000-06-07 23:51:50 +00001363
1364 /* Code the current SELECT statement
1365 */
1366 switch( p->op ){
1367 case TK_EXCEPT: op = SRT_Except; break;
1368 case TK_UNION: op = SRT_Union; break;
1369 case TK_ALL: op = SRT_Table; break;
1370 }
drh82c3d632000-06-06 21:56:07 +00001371 p->pPrior = 0;
drhc926afb2002-06-20 03:38:26 +00001372 pOrderBy = p->pOrderBy;
1373 p->pOrderBy = 0;
drh7b58dae2003-07-20 01:16:46 +00001374 nLimit = p->nLimit;
1375 p->nLimit = -1;
1376 nOffset = p->nOffset;
1377 p->nOffset = 0;
drh832508b2002-03-02 17:04:07 +00001378 rc = sqliteSelect(pParse, p, op, unionTab, 0, 0, 0);
drh82c3d632000-06-06 21:56:07 +00001379 p->pPrior = pPrior;
drhc926afb2002-06-20 03:38:26 +00001380 p->pOrderBy = pOrderBy;
drh7b58dae2003-07-20 01:16:46 +00001381 p->nLimit = nLimit;
1382 p->nOffset = nOffset;
drh82c3d632000-06-06 21:56:07 +00001383 if( rc ) return rc;
drhd8bc7082000-06-07 23:51:50 +00001384
1385 /* Convert the data in the temporary table into whatever form
1386 ** it is that we currently need.
1387 */
drhc926afb2002-06-20 03:38:26 +00001388 if( eDest!=priorOp || unionTab!=iParm ){
drh6b563442001-11-07 16:48:26 +00001389 int iCont, iBreak, iStart;
drh82c3d632000-06-06 21:56:07 +00001390 assert( p->pEList );
drh41202cc2002-04-23 17:10:18 +00001391 if( eDest==SRT_Callback ){
drh6a3ea0e2003-05-02 14:32:12 +00001392 generateColumnNames(pParse, 0, p->pEList);
1393 generateColumnTypes(pParse, p->pSrc, p->pEList);
drh41202cc2002-04-23 17:10:18 +00001394 }
drh82c3d632000-06-06 21:56:07 +00001395 iBreak = sqliteVdbeMakeLabel(v);
drh6b563442001-11-07 16:48:26 +00001396 iCont = sqliteVdbeMakeLabel(v);
1397 sqliteVdbeAddOp(v, OP_Rewind, unionTab, iBreak);
drh7b58dae2003-07-20 01:16:46 +00001398 computeLimitRegisters(pParse, p);
drh6b563442001-11-07 16:48:26 +00001399 iStart = sqliteVdbeCurrentAddr(v);
drhfcb78a42003-01-18 20:11:05 +00001400 multiSelectSortOrder(p, p->pOrderBy);
drh38640e12002-07-05 21:42:36 +00001401 rc = selectInnerLoop(pParse, p, p->pEList, unionTab, p->pEList->nExpr,
drhd8bc7082000-06-07 23:51:50 +00001402 p->pOrderBy, -1, eDest, iParm,
drh82c3d632000-06-06 21:56:07 +00001403 iCont, iBreak);
1404 if( rc ) return 1;
drh6b563442001-11-07 16:48:26 +00001405 sqliteVdbeResolveLabel(v, iCont);
1406 sqliteVdbeAddOp(v, OP_Next, unionTab, iStart);
drh99fcd712001-10-13 01:06:47 +00001407 sqliteVdbeResolveLabel(v, iBreak);
1408 sqliteVdbeAddOp(v, OP_Close, unionTab, 0);
drhd8bc7082000-06-07 23:51:50 +00001409 if( p->pOrderBy ){
drhc926afb2002-06-20 03:38:26 +00001410 generateSortTail(p, v, p->pEList->nExpr, eDest, iParm);
drhd8bc7082000-06-07 23:51:50 +00001411 }
drh82c3d632000-06-06 21:56:07 +00001412 }
1413 break;
1414 }
1415 case TK_INTERSECT: {
1416 int tab1, tab2;
drh6b563442001-11-07 16:48:26 +00001417 int iCont, iBreak, iStart;
drh7b58dae2003-07-20 01:16:46 +00001418 int nLimit, nOffset;
drh82c3d632000-06-06 21:56:07 +00001419
drhd8bc7082000-06-07 23:51:50 +00001420 /* INTERSECT is different from the others since it requires
drh6206d502000-06-19 19:09:08 +00001421 ** two temporary tables. Hence it has its own case. Begin
drhd8bc7082000-06-07 23:51:50 +00001422 ** by allocating the tables we will need.
1423 */
drh82c3d632000-06-06 21:56:07 +00001424 tab1 = pParse->nTab++;
1425 tab2 = pParse->nTab++;
drhd8bc7082000-06-07 23:51:50 +00001426 if( p->pOrderBy && matchOrderbyToColumn(pParse,p,p->pOrderBy,tab1,1) ){
1427 return 1;
1428 }
drhc6b52df2002-01-04 03:09:29 +00001429 sqliteVdbeAddOp(v, OP_OpenTemp, tab1, 1);
drh99fcd712001-10-13 01:06:47 +00001430 sqliteVdbeAddOp(v, OP_KeyAsData, tab1, 1);
drhd8bc7082000-06-07 23:51:50 +00001431
1432 /* Code the SELECTs to our left into temporary table "tab1".
1433 */
drh832508b2002-03-02 17:04:07 +00001434 rc = sqliteSelect(pParse, pPrior, SRT_Union, tab1, 0, 0, 0);
drh82c3d632000-06-06 21:56:07 +00001435 if( rc ) return rc;
drhd8bc7082000-06-07 23:51:50 +00001436
1437 /* Code the current SELECT into temporary table "tab2"
1438 */
drhc6b52df2002-01-04 03:09:29 +00001439 sqliteVdbeAddOp(v, OP_OpenTemp, tab2, 1);
drh99fcd712001-10-13 01:06:47 +00001440 sqliteVdbeAddOp(v, OP_KeyAsData, tab2, 1);
drh82c3d632000-06-06 21:56:07 +00001441 p->pPrior = 0;
drh7b58dae2003-07-20 01:16:46 +00001442 nLimit = p->nLimit;
1443 p->nLimit = -1;
1444 nOffset = p->nOffset;
1445 p->nOffset = 0;
drh832508b2002-03-02 17:04:07 +00001446 rc = sqliteSelect(pParse, p, SRT_Union, tab2, 0, 0, 0);
drh82c3d632000-06-06 21:56:07 +00001447 p->pPrior = pPrior;
drh7b58dae2003-07-20 01:16:46 +00001448 p->nLimit = nLimit;
1449 p->nOffset = nOffset;
drh82c3d632000-06-06 21:56:07 +00001450 if( rc ) return rc;
drhd8bc7082000-06-07 23:51:50 +00001451
1452 /* Generate code to take the intersection of the two temporary
1453 ** tables.
1454 */
drh82c3d632000-06-06 21:56:07 +00001455 assert( p->pEList );
drh41202cc2002-04-23 17:10:18 +00001456 if( eDest==SRT_Callback ){
drh6a3ea0e2003-05-02 14:32:12 +00001457 generateColumnNames(pParse, 0, p->pEList);
1458 generateColumnTypes(pParse, p->pSrc, p->pEList);
drh41202cc2002-04-23 17:10:18 +00001459 }
drh82c3d632000-06-06 21:56:07 +00001460 iBreak = sqliteVdbeMakeLabel(v);
drh6b563442001-11-07 16:48:26 +00001461 iCont = sqliteVdbeMakeLabel(v);
1462 sqliteVdbeAddOp(v, OP_Rewind, tab1, iBreak);
drh7b58dae2003-07-20 01:16:46 +00001463 computeLimitRegisters(pParse, p);
drh6b563442001-11-07 16:48:26 +00001464 iStart = sqliteVdbeAddOp(v, OP_FullKey, tab1, 0);
drh99fcd712001-10-13 01:06:47 +00001465 sqliteVdbeAddOp(v, OP_NotFound, tab2, iCont);
drhfcb78a42003-01-18 20:11:05 +00001466 multiSelectSortOrder(p, p->pOrderBy);
drh38640e12002-07-05 21:42:36 +00001467 rc = selectInnerLoop(pParse, p, p->pEList, tab1, p->pEList->nExpr,
drhd8bc7082000-06-07 23:51:50 +00001468 p->pOrderBy, -1, eDest, iParm,
drh82c3d632000-06-06 21:56:07 +00001469 iCont, iBreak);
1470 if( rc ) return 1;
drh6b563442001-11-07 16:48:26 +00001471 sqliteVdbeResolveLabel(v, iCont);
1472 sqliteVdbeAddOp(v, OP_Next, tab1, iStart);
drh99fcd712001-10-13 01:06:47 +00001473 sqliteVdbeResolveLabel(v, iBreak);
1474 sqliteVdbeAddOp(v, OP_Close, tab2, 0);
1475 sqliteVdbeAddOp(v, OP_Close, tab1, 0);
drhd8bc7082000-06-07 23:51:50 +00001476 if( p->pOrderBy ){
drhc926afb2002-06-20 03:38:26 +00001477 generateSortTail(p, v, p->pEList->nExpr, eDest, iParm);
drhd8bc7082000-06-07 23:51:50 +00001478 }
drh82c3d632000-06-06 21:56:07 +00001479 break;
1480 }
1481 }
1482 assert( p->pEList && pPrior->pEList );
1483 if( p->pEList->nExpr!=pPrior->pEList->nExpr ){
drhda93d232003-03-31 02:12:46 +00001484 sqliteErrorMsg(pParse, "SELECTs to the left and right of %s"
1485 " do not have the same number of result columns", selectOpName(p->op));
drh82c3d632000-06-06 21:56:07 +00001486 return 1;
drh22827922000-06-06 17:27:05 +00001487 }
drhfcb78a42003-01-18 20:11:05 +00001488
1489 /* Issue a null callback if that is what the user wants.
1490 */
drh326dce72003-01-29 14:06:07 +00001491 if( eDest==SRT_Callback &&
1492 (pParse->useCallback==0 || (pParse->db->flags & SQLITE_NullCallback)!=0)
1493 ){
drhfcb78a42003-01-18 20:11:05 +00001494 sqliteVdbeAddOp(v, OP_NullCallback, p->pEList->nExpr, 0);
1495 }
drh22827922000-06-06 17:27:05 +00001496 return 0;
1497}
1498
1499/*
drh832508b2002-03-02 17:04:07 +00001500** Scan through the expression pExpr. Replace every reference to
drh6a3ea0e2003-05-02 14:32:12 +00001501** a column in table number iTable with a copy of the iColumn-th
drh84e59202002-03-14 14:33:31 +00001502** entry in pEList. (But leave references to the ROWID column
drh6a3ea0e2003-05-02 14:32:12 +00001503** unchanged.)
drh832508b2002-03-02 17:04:07 +00001504**
1505** This routine is part of the flattening procedure. A subquery
1506** whose result set is defined by pEList appears as entry in the
1507** FROM clause of a SELECT such that the VDBE cursor assigned to that
1508** FORM clause entry is iTable. This routine make the necessary
1509** changes to pExpr so that it refers directly to the source table
1510** of the subquery rather the result set of the subquery.
1511*/
drh6a3ea0e2003-05-02 14:32:12 +00001512static void substExprList(ExprList*,int,ExprList*); /* Forward Decl */
1513static void substExpr(Expr *pExpr, int iTable, ExprList *pEList){
drh832508b2002-03-02 17:04:07 +00001514 if( pExpr==0 ) return;
drh84e59202002-03-14 14:33:31 +00001515 if( pExpr->op==TK_COLUMN && pExpr->iTable==iTable && pExpr->iColumn>=0 ){
drh832508b2002-03-02 17:04:07 +00001516 Expr *pNew;
drh84e59202002-03-14 14:33:31 +00001517 assert( pEList!=0 && pExpr->iColumn<pEList->nExpr );
drh832508b2002-03-02 17:04:07 +00001518 assert( pExpr->pLeft==0 && pExpr->pRight==0 && pExpr->pList==0 );
1519 pNew = pEList->a[pExpr->iColumn].pExpr;
1520 assert( pNew!=0 );
1521 pExpr->op = pNew->op;
drhfcb78a42003-01-18 20:11:05 +00001522 pExpr->dataType = pNew->dataType;
drhd94a6692002-08-25 18:29:11 +00001523 assert( pExpr->pLeft==0 );
drh832508b2002-03-02 17:04:07 +00001524 pExpr->pLeft = sqliteExprDup(pNew->pLeft);
drhd94a6692002-08-25 18:29:11 +00001525 assert( pExpr->pRight==0 );
drh832508b2002-03-02 17:04:07 +00001526 pExpr->pRight = sqliteExprDup(pNew->pRight);
drhd94a6692002-08-25 18:29:11 +00001527 assert( pExpr->pList==0 );
drh832508b2002-03-02 17:04:07 +00001528 pExpr->pList = sqliteExprListDup(pNew->pList);
1529 pExpr->iTable = pNew->iTable;
1530 pExpr->iColumn = pNew->iColumn;
1531 pExpr->iAgg = pNew->iAgg;
drh4b59ab52002-08-24 18:24:51 +00001532 sqliteTokenCopy(&pExpr->token, &pNew->token);
drh6977fea2002-10-22 23:38:04 +00001533 sqliteTokenCopy(&pExpr->span, &pNew->span);
drh832508b2002-03-02 17:04:07 +00001534 }else{
drh6a3ea0e2003-05-02 14:32:12 +00001535 substExpr(pExpr->pLeft, iTable, pEList);
1536 substExpr(pExpr->pRight, iTable, pEList);
1537 substExprList(pExpr->pList, iTable, pEList);
drh832508b2002-03-02 17:04:07 +00001538 }
1539}
1540static void
drh6a3ea0e2003-05-02 14:32:12 +00001541substExprList(ExprList *pList, int iTable, ExprList *pEList){
drh832508b2002-03-02 17:04:07 +00001542 int i;
1543 if( pList==0 ) return;
1544 for(i=0; i<pList->nExpr; i++){
drh6a3ea0e2003-05-02 14:32:12 +00001545 substExpr(pList->a[i].pExpr, iTable, pEList);
drh832508b2002-03-02 17:04:07 +00001546 }
1547}
1548
1549/*
drh1350b032002-02-27 19:00:20 +00001550** This routine attempts to flatten subqueries in order to speed
1551** execution. It returns 1 if it makes changes and 0 if no flattening
1552** occurs.
1553**
1554** To understand the concept of flattening, consider the following
1555** query:
1556**
1557** SELECT a FROM (SELECT x+y AS a FROM t1 WHERE z<100) WHERE a>5
1558**
1559** The default way of implementing this query is to execute the
1560** subquery first and store the results in a temporary table, then
1561** run the outer query on that temporary table. This requires two
1562** passes over the data. Furthermore, because the temporary table
1563** has no indices, the WHERE clause on the outer query cannot be
drh832508b2002-03-02 17:04:07 +00001564** optimized.
drh1350b032002-02-27 19:00:20 +00001565**
drh832508b2002-03-02 17:04:07 +00001566** This routine attempts to rewrite queries such as the above into
drh1350b032002-02-27 19:00:20 +00001567** a single flat select, like this:
1568**
1569** SELECT x+y AS a FROM t1 WHERE z<100 AND a>5
1570**
1571** The code generated for this simpification gives the same result
drh832508b2002-03-02 17:04:07 +00001572** but only has to scan the data once. And because indices might
1573** exist on the table t1, a complete scan of the data might be
1574** avoided.
drh1350b032002-02-27 19:00:20 +00001575**
drh832508b2002-03-02 17:04:07 +00001576** Flattening is only attempted if all of the following are true:
drh1350b032002-02-27 19:00:20 +00001577**
drh832508b2002-03-02 17:04:07 +00001578** (1) The subquery and the outer query do not both use aggregates.
drh1350b032002-02-27 19:00:20 +00001579**
drh832508b2002-03-02 17:04:07 +00001580** (2) The subquery is not an aggregate or the outer query is not a join.
1581**
drh8af4d3a2003-05-06 20:35:16 +00001582** (3) The subquery is not the right operand of a left outer join, or
1583** the subquery is not itself a join. (Ticket #306)
drh832508b2002-03-02 17:04:07 +00001584**
1585** (4) The subquery is not DISTINCT or the outer query is not a join.
1586**
1587** (5) The subquery is not DISTINCT or the outer query does not use
1588** aggregates.
1589**
1590** (6) The subquery does not use aggregates or the outer query is not
1591** DISTINCT.
1592**
drh08192d52002-04-30 19:20:28 +00001593** (7) The subquery has a FROM clause.
1594**
drhdf199a22002-06-14 22:38:41 +00001595** (8) The subquery does not use LIMIT or the outer query is not a join.
1596**
1597** (9) The subquery does not use LIMIT or the outer query does not use
1598** aggregates.
1599**
1600** (10) The subquery does not use aggregates or the outer query does not
1601** use LIMIT.
1602**
drh174b6192002-12-03 02:22:52 +00001603** (11) The subquery and the outer query do not both have ORDER BY clauses.
1604**
drh3fc673e2003-06-16 00:40:34 +00001605** (12) The subquery is not the right term of a LEFT OUTER JOIN or the
1606** subquery has no WHERE clause. (added by ticket #350)
1607**
drh832508b2002-03-02 17:04:07 +00001608** In this routine, the "p" parameter is a pointer to the outer query.
1609** The subquery is p->pSrc->a[iFrom]. isAgg is true if the outer query
1610** uses aggregates and subqueryIsAgg is true if the subquery uses aggregates.
1611**
drh665de472003-03-31 13:36:09 +00001612** If flattening is not attempted, this routine is a no-op and returns 0.
drh832508b2002-03-02 17:04:07 +00001613** If flattening is attempted this routine returns 1.
1614**
1615** All of the expression analysis must occur on both the outer query and
1616** the subquery before this routine runs.
drh1350b032002-02-27 19:00:20 +00001617*/
drh8c74a8c2002-08-25 19:20:40 +00001618static int flattenSubquery(
1619 Parse *pParse, /* The parsing context */
1620 Select *p, /* The parent or outer SELECT statement */
1621 int iFrom, /* Index in p->pSrc->a[] of the inner subquery */
1622 int isAgg, /* True if outer SELECT uses aggregate functions */
1623 int subqueryIsAgg /* True if the subquery uses aggregate functions */
1624){
drh0bb28102002-05-08 11:54:14 +00001625 Select *pSub; /* The inner query or "subquery" */
drhad3cab52002-05-24 02:04:32 +00001626 SrcList *pSrc; /* The FROM clause of the outer query */
1627 SrcList *pSubSrc; /* The FROM clause of the subquery */
drh0bb28102002-05-08 11:54:14 +00001628 ExprList *pList; /* The result set of the outer query */
drh6a3ea0e2003-05-02 14:32:12 +00001629 int iParent; /* VDBE cursor number of the pSub result set temp table */
drh832508b2002-03-02 17:04:07 +00001630 int i;
drh832508b2002-03-02 17:04:07 +00001631 Expr *pWhere;
drh1350b032002-02-27 19:00:20 +00001632
drh832508b2002-03-02 17:04:07 +00001633 /* Check to see if flattening is permitted. Return 0 if not.
1634 */
1635 if( p==0 ) return 0;
1636 pSrc = p->pSrc;
drhad3cab52002-05-24 02:04:32 +00001637 assert( pSrc && iFrom>=0 && iFrom<pSrc->nSrc );
drh832508b2002-03-02 17:04:07 +00001638 pSub = pSrc->a[iFrom].pSelect;
1639 assert( pSub!=0 );
1640 if( isAgg && subqueryIsAgg ) return 0;
drhad3cab52002-05-24 02:04:32 +00001641 if( subqueryIsAgg && pSrc->nSrc>1 ) return 0;
drh832508b2002-03-02 17:04:07 +00001642 pSubSrc = pSub->pSrc;
1643 assert( pSubSrc );
drhc31c2eb2003-05-02 16:04:17 +00001644 if( pSubSrc->nSrc==0 ) return 0;
drhdf199a22002-06-14 22:38:41 +00001645 if( (pSub->isDistinct || pSub->nLimit>=0) && (pSrc->nSrc>1 || isAgg) ){
1646 return 0;
1647 }
drhd11d3822002-06-21 23:01:49 +00001648 if( (p->isDistinct || p->nLimit>=0) && subqueryIsAgg ) return 0;
drh174b6192002-12-03 02:22:52 +00001649 if( p->pOrderBy && pSub->pOrderBy ) return 0;
drh832508b2002-03-02 17:04:07 +00001650
drh8af4d3a2003-05-06 20:35:16 +00001651 /* Restriction 3: If the subquery is a join, make sure the subquery is
1652 ** not used as the right operand of an outer join. Examples of why this
1653 ** is not allowed:
1654 **
1655 ** t1 LEFT OUTER JOIN (t2 JOIN t3)
1656 **
1657 ** If we flatten the above, we would get
1658 **
1659 ** (t1 LEFT OUTER JOIN t2) JOIN t3
1660 **
1661 ** which is not at all the same thing.
1662 */
1663 if( pSubSrc->nSrc>1 && iFrom>0 && (pSrc->a[iFrom-1].jointype & JT_OUTER)!=0 ){
1664 return 0;
1665 }
1666
drh3fc673e2003-06-16 00:40:34 +00001667 /* Restriction 12: If the subquery is the right operand of a left outer
1668 ** join, make sure the subquery has no WHERE clause.
1669 ** An examples of why this is not allowed:
1670 **
1671 ** t1 LEFT OUTER JOIN (SELECT * FROM t2 WHERE t2.x>0)
1672 **
1673 ** If we flatten the above, we would get
1674 **
1675 ** (t1 LEFT OUTER JOIN t2) WHERE t2.x>0
1676 **
1677 ** But the t2.x>0 test will always fail on a NULL row of t2, which
1678 ** effectively converts the OUTER JOIN into an INNER JOIN.
1679 */
1680 if( iFrom>0 && (pSrc->a[iFrom-1].jointype & JT_OUTER)!=0
1681 && pSub->pWhere!=0 ){
1682 return 0;
1683 }
1684
drh0bb28102002-05-08 11:54:14 +00001685 /* If we reach this point, it means flattening is permitted for the
drh63eb5f22003-04-29 16:20:44 +00001686 ** iFrom-th entry of the FROM clause in the outer query.
drh832508b2002-03-02 17:04:07 +00001687 */
drhc31c2eb2003-05-02 16:04:17 +00001688
1689 /* Move all of the FROM elements of the subquery into the
1690 ** the FROM clause of the outer query. Before doing this, remember
1691 ** the cursor number for the original outer query FROM element in
1692 ** iParent. The iParent cursor will never be used. Subsequent code
1693 ** will scan expressions looking for iParent references and replace
1694 ** those references with expressions that resolve to the subquery FROM
1695 ** elements we are now copying in.
1696 */
drh6a3ea0e2003-05-02 14:32:12 +00001697 iParent = pSrc->a[iFrom].iCursor;
drhc31c2eb2003-05-02 16:04:17 +00001698 {
1699 int nSubSrc = pSubSrc->nSrc;
drh8af4d3a2003-05-06 20:35:16 +00001700 int jointype = pSrc->a[iFrom].jointype;
drhc31c2eb2003-05-02 16:04:17 +00001701
1702 if( pSrc->a[iFrom].pTab && pSrc->a[iFrom].pTab->isTransient ){
1703 sqliteDeleteTable(0, pSrc->a[iFrom].pTab);
1704 }
drhf26e09c2003-05-31 16:21:12 +00001705 sqliteFree(pSrc->a[iFrom].zDatabase);
drhc31c2eb2003-05-02 16:04:17 +00001706 sqliteFree(pSrc->a[iFrom].zName);
1707 sqliteFree(pSrc->a[iFrom].zAlias);
1708 if( nSubSrc>1 ){
1709 int extra = nSubSrc - 1;
1710 for(i=1; i<nSubSrc; i++){
1711 pSrc = sqliteSrcListAppend(pSrc, 0, 0);
1712 }
1713 p->pSrc = pSrc;
1714 for(i=pSrc->nSrc-1; i-extra>=iFrom; i--){
1715 pSrc->a[i] = pSrc->a[i-extra];
1716 }
1717 }
1718 for(i=0; i<nSubSrc; i++){
1719 pSrc->a[i+iFrom] = pSubSrc->a[i];
1720 memset(&pSubSrc->a[i], 0, sizeof(pSubSrc->a[i]));
1721 }
drh8af4d3a2003-05-06 20:35:16 +00001722 pSrc->a[iFrom+nSubSrc-1].jointype = jointype;
drhc31c2eb2003-05-02 16:04:17 +00001723 }
1724
1725 /* Now begin substituting subquery result set expressions for
1726 ** references to the iParent in the outer query.
1727 **
1728 ** Example:
1729 **
1730 ** SELECT a+5, b*10 FROM (SELECT x*3 AS a, y+10 AS b FROM t1) WHERE a>b;
1731 ** \ \_____________ subquery __________/ /
1732 ** \_____________________ outer query ______________________________/
1733 **
1734 ** We look at every expression in the outer query and every place we see
1735 ** "a" we substitute "x*3" and every place we see "b" we substitute "y+10".
1736 */
drh6a3ea0e2003-05-02 14:32:12 +00001737 substExprList(p->pEList, iParent, pSub->pEList);
drh832508b2002-03-02 17:04:07 +00001738 pList = p->pEList;
1739 for(i=0; i<pList->nExpr; i++){
drh6977fea2002-10-22 23:38:04 +00001740 Expr *pExpr;
1741 if( pList->a[i].zName==0 && (pExpr = pList->a[i].pExpr)->span.z!=0 ){
1742 pList->a[i].zName = sqliteStrNDup(pExpr->span.z, pExpr->span.n);
drh832508b2002-03-02 17:04:07 +00001743 }
1744 }
drh1b2e0322002-03-03 02:49:51 +00001745 if( isAgg ){
drh6a3ea0e2003-05-02 14:32:12 +00001746 substExprList(p->pGroupBy, iParent, pSub->pEList);
1747 substExpr(p->pHaving, iParent, pSub->pEList);
drh1b2e0322002-03-03 02:49:51 +00001748 }
drh174b6192002-12-03 02:22:52 +00001749 if( pSub->pOrderBy ){
1750 assert( p->pOrderBy==0 );
1751 p->pOrderBy = pSub->pOrderBy;
1752 pSub->pOrderBy = 0;
drh174b6192002-12-03 02:22:52 +00001753 }else if( p->pOrderBy ){
drh6a3ea0e2003-05-02 14:32:12 +00001754 substExprList(p->pOrderBy, iParent, pSub->pEList);
drh174b6192002-12-03 02:22:52 +00001755 }
drh832508b2002-03-02 17:04:07 +00001756 if( pSub->pWhere ){
1757 pWhere = sqliteExprDup(pSub->pWhere);
drh832508b2002-03-02 17:04:07 +00001758 }else{
1759 pWhere = 0;
1760 }
1761 if( subqueryIsAgg ){
1762 assert( p->pHaving==0 );
drh1b2e0322002-03-03 02:49:51 +00001763 p->pHaving = p->pWhere;
1764 p->pWhere = pWhere;
drh6a3ea0e2003-05-02 14:32:12 +00001765 substExpr(p->pHaving, iParent, pSub->pEList);
drh1b2e0322002-03-03 02:49:51 +00001766 if( pSub->pHaving ){
1767 Expr *pHaving = sqliteExprDup(pSub->pHaving);
drh1b2e0322002-03-03 02:49:51 +00001768 if( p->pHaving ){
1769 p->pHaving = sqliteExpr(TK_AND, p->pHaving, pHaving, 0);
1770 }else{
1771 p->pHaving = pHaving;
1772 }
1773 }
1774 assert( p->pGroupBy==0 );
1775 p->pGroupBy = sqliteExprListDup(pSub->pGroupBy);
drh832508b2002-03-02 17:04:07 +00001776 }else if( p->pWhere==0 ){
1777 p->pWhere = pWhere;
1778 }else{
drh6a3ea0e2003-05-02 14:32:12 +00001779 substExpr(p->pWhere, iParent, pSub->pEList);
drh832508b2002-03-02 17:04:07 +00001780 if( pWhere ){
1781 p->pWhere = sqliteExpr(TK_AND, p->pWhere, pWhere, 0);
1782 }
1783 }
drhc31c2eb2003-05-02 16:04:17 +00001784
1785 /* The flattened query is distinct if either the inner or the
1786 ** outer query is distinct.
1787 */
drh832508b2002-03-02 17:04:07 +00001788 p->isDistinct = p->isDistinct || pSub->isDistinct;
drh8c74a8c2002-08-25 19:20:40 +00001789
drhc31c2eb2003-05-02 16:04:17 +00001790 /* Transfer the limit expression from the subquery to the outer
1791 ** query.
1792 */
drhdf199a22002-06-14 22:38:41 +00001793 if( pSub->nLimit>=0 ){
1794 if( p->nLimit<0 ){
1795 p->nLimit = pSub->nLimit;
1796 }else if( p->nLimit+p->nOffset > pSub->nLimit+pSub->nOffset ){
1797 p->nLimit = pSub->nLimit + pSub->nOffset - p->nOffset;
1798 }
1799 }
1800 p->nOffset += pSub->nOffset;
drh8c74a8c2002-08-25 19:20:40 +00001801
drhc31c2eb2003-05-02 16:04:17 +00001802 /* Finially, delete what is left of the subquery and return
1803 ** success.
1804 */
drh832508b2002-03-02 17:04:07 +00001805 sqliteSelectDelete(pSub);
1806 return 1;
1807}
drh1350b032002-02-27 19:00:20 +00001808
1809/*
drh9562b552002-02-19 15:00:07 +00001810** Analyze the SELECT statement passed in as an argument to see if it
1811** is a simple min() or max() query. If it is and this query can be
1812** satisfied using a single seek to the beginning or end of an index,
drhe78e8282003-01-19 03:59:45 +00001813** then generate the code for this SELECT and return 1. If this is not a
drh9562b552002-02-19 15:00:07 +00001814** simple min() or max() query, then return 0;
1815**
1816** A simply min() or max() query looks like this:
1817**
1818** SELECT min(a) FROM table;
1819** SELECT max(a) FROM table;
1820**
1821** The query may have only a single table in its FROM argument. There
1822** can be no GROUP BY or HAVING or WHERE clauses. The result set must
1823** be the min() or max() of a single column of the table. The column
1824** in the min() or max() function must be indexed.
1825**
1826** The parameters to this routine are the same as for sqliteSelect().
1827** See the header comment on that routine for additional information.
1828*/
1829static int simpleMinMaxQuery(Parse *pParse, Select *p, int eDest, int iParm){
1830 Expr *pExpr;
1831 int iCol;
1832 Table *pTab;
1833 Index *pIdx;
1834 int base;
1835 Vdbe *v;
drh9562b552002-02-19 15:00:07 +00001836 int seekOp;
1837 int cont;
1838 ExprList eList;
1839 struct ExprList_item eListItem;
1840
1841 /* Check to see if this query is a simple min() or max() query. Return
1842 ** zero if it is not.
1843 */
1844 if( p->pGroupBy || p->pHaving || p->pWhere ) return 0;
drhad3cab52002-05-24 02:04:32 +00001845 if( p->pSrc->nSrc!=1 ) return 0;
drh9562b552002-02-19 15:00:07 +00001846 if( p->pEList->nExpr!=1 ) return 0;
1847 pExpr = p->pEList->a[0].pExpr;
1848 if( pExpr->op!=TK_AGG_FUNCTION ) return 0;
1849 if( pExpr->pList==0 || pExpr->pList->nExpr!=1 ) return 0;
drh6977fea2002-10-22 23:38:04 +00001850 if( pExpr->token.n!=3 ) return 0;
drh0bce8352002-02-28 00:41:10 +00001851 if( sqliteStrNICmp(pExpr->token.z,"min",3)==0 ){
1852 seekOp = OP_Rewind;
1853 }else if( sqliteStrNICmp(pExpr->token.z,"max",3)==0 ){
1854 seekOp = OP_Last;
1855 }else{
1856 return 0;
1857 }
drh9562b552002-02-19 15:00:07 +00001858 pExpr = pExpr->pList->a[0].pExpr;
1859 if( pExpr->op!=TK_COLUMN ) return 0;
1860 iCol = pExpr->iColumn;
1861 pTab = p->pSrc->a[0].pTab;
1862
1863 /* If we get to here, it means the query is of the correct form.
drh17f71932002-02-21 12:01:27 +00001864 ** Check to make sure we have an index and make pIdx point to the
1865 ** appropriate index. If the min() or max() is on an INTEGER PRIMARY
1866 ** key column, no index is necessary so set pIdx to NULL. If no
1867 ** usable index is found, return 0.
drh9562b552002-02-19 15:00:07 +00001868 */
1869 if( iCol<0 ){
1870 pIdx = 0;
1871 }else{
1872 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
1873 assert( pIdx->nColumn>=1 );
1874 if( pIdx->aiColumn[0]==iCol ) break;
1875 }
1876 if( pIdx==0 ) return 0;
1877 }
1878
drhe5f50722003-07-19 00:44:14 +00001879 /* Identify column types if we will be using the callback. This
drh9562b552002-02-19 15:00:07 +00001880 ** step is skipped if the output is going to a table or a memory cell.
drhe5f50722003-07-19 00:44:14 +00001881 ** The column names have already been generated in the calling function.
drh9562b552002-02-19 15:00:07 +00001882 */
1883 v = sqliteGetVdbe(pParse);
1884 if( v==0 ) return 0;
1885 if( eDest==SRT_Callback ){
drh6a3ea0e2003-05-02 14:32:12 +00001886 generateColumnTypes(pParse, p->pSrc, p->pEList);
drh9562b552002-02-19 15:00:07 +00001887 }
1888
drh17f71932002-02-21 12:01:27 +00001889 /* Generating code to find the min or the max. Basically all we have
1890 ** to do is find the first or the last entry in the chosen index. If
1891 ** the min() or max() is on the INTEGER PRIMARY KEY, then find the first
1892 ** or last entry in the main table.
drh9562b552002-02-19 15:00:07 +00001893 */
drh8bf8dc92003-05-17 17:35:10 +00001894 sqliteCodeVerifySchema(pParse, pTab->iDb);
drh6a3ea0e2003-05-02 14:32:12 +00001895 base = p->pSrc->a[0].iCursor;
drh7b58dae2003-07-20 01:16:46 +00001896 computeLimitRegisters(pParse, p);
drhd24cc422003-03-27 12:51:24 +00001897 sqliteVdbeAddOp(v, OP_Integer, pTab->iDb, 0);
drh001bbcb2003-03-19 03:14:00 +00001898 sqliteVdbeAddOp(v, OP_OpenRead, base, pTab->tnum);
drh5cf8e8c2002-02-19 22:42:05 +00001899 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
drhd4d595f2003-04-17 12:44:23 +00001900 cont = sqliteVdbeMakeLabel(v);
drh9562b552002-02-19 15:00:07 +00001901 if( pIdx==0 ){
1902 sqliteVdbeAddOp(v, seekOp, base, 0);
1903 }else{
drhd24cc422003-03-27 12:51:24 +00001904 sqliteVdbeAddOp(v, OP_Integer, pIdx->iDb, 0);
drh001bbcb2003-03-19 03:14:00 +00001905 sqliteVdbeAddOp(v, OP_OpenRead, base+1, pIdx->tnum);
drh5cf8e8c2002-02-19 22:42:05 +00001906 sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC);
drh9562b552002-02-19 15:00:07 +00001907 sqliteVdbeAddOp(v, seekOp, base+1, 0);
1908 sqliteVdbeAddOp(v, OP_IdxRecno, base+1, 0);
1909 sqliteVdbeAddOp(v, OP_Close, base+1, 0);
1910 sqliteVdbeAddOp(v, OP_MoveTo, base, 0);
1911 }
drh5cf8e8c2002-02-19 22:42:05 +00001912 eList.nExpr = 1;
1913 memset(&eListItem, 0, sizeof(eListItem));
1914 eList.a = &eListItem;
1915 eList.a[0].pExpr = pExpr;
drh38640e12002-07-05 21:42:36 +00001916 selectInnerLoop(pParse, p, &eList, 0, 0, 0, -1, eDest, iParm, cont, cont);
drh9562b552002-02-19 15:00:07 +00001917 sqliteVdbeResolveLabel(v, cont);
1918 sqliteVdbeAddOp(v, OP_Close, base, 0);
1919 return 1;
1920}
1921
1922/*
drh9bb61fe2000-06-05 16:01:39 +00001923** Generate code for the given SELECT statement.
1924**
drhfef52082000-06-06 01:50:43 +00001925** The results are distributed in various ways depending on the
1926** value of eDest and iParm.
1927**
1928** eDest Value Result
1929** ------------ -------------------------------------------
1930** SRT_Callback Invoke the callback for each row of the result.
1931**
1932** SRT_Mem Store first result in memory cell iParm
1933**
1934** SRT_Set Store results as keys of a table with cursor iParm
1935**
drh82c3d632000-06-06 21:56:07 +00001936** SRT_Union Store results as a key in a temporary table iParm
1937**
jplyon4b11c6d2004-01-19 04:57:53 +00001938** SRT_Except Remove results from the temporary table iParm.
drhc4a3c772001-04-04 11:48:57 +00001939**
1940** SRT_Table Store results in temporary table iParm
drh9bb61fe2000-06-05 16:01:39 +00001941**
drhe78e8282003-01-19 03:59:45 +00001942** The table above is incomplete. Additional eDist value have be added
1943** since this comment was written. See the selectInnerLoop() function for
1944** a complete listing of the allowed values of eDest and their meanings.
1945**
drh9bb61fe2000-06-05 16:01:39 +00001946** This routine returns the number of errors. If any errors are
1947** encountered, then an appropriate error message is left in
1948** pParse->zErrMsg.
1949**
1950** This routine does NOT free the Select structure passed in. The
1951** calling function needs to do that.
drh1b2e0322002-03-03 02:49:51 +00001952**
1953** The pParent, parentTab, and *pParentAgg fields are filled in if this
1954** SELECT is a subquery. This routine may try to combine this SELECT
1955** with its parent to form a single flat query. In so doing, it might
1956** change the parent query from a non-aggregate to an aggregate query.
1957** For that reason, the pParentAgg flag is passed as a pointer, so it
1958** can be changed.
drhe78e8282003-01-19 03:59:45 +00001959**
1960** Example 1: The meaning of the pParent parameter.
1961**
1962** SELECT * FROM t1 JOIN (SELECT x, count(*) FROM t2) JOIN t3;
1963** \ \_______ subquery _______/ /
1964** \ /
1965** \____________________ outer query ___________________/
1966**
1967** This routine is called for the outer query first. For that call,
1968** pParent will be NULL. During the processing of the outer query, this
1969** routine is called recursively to handle the subquery. For the recursive
1970** call, pParent will point to the outer query. Because the subquery is
1971** the second element in a three-way join, the parentTab parameter will
1972** be 1 (the 2nd value of a 0-indexed array.)
drh9bb61fe2000-06-05 16:01:39 +00001973*/
1974int sqliteSelect(
drhcce7d172000-05-31 15:34:51 +00001975 Parse *pParse, /* The parser context */
drh9bb61fe2000-06-05 16:01:39 +00001976 Select *p, /* The SELECT statement being coded. */
drhe78e8282003-01-19 03:59:45 +00001977 int eDest, /* How to dispose of the results */
1978 int iParm, /* A parameter used by the eDest disposal method */
drh832508b2002-03-02 17:04:07 +00001979 Select *pParent, /* Another SELECT for which this is a sub-query */
1980 int parentTab, /* Index in pParent->pSrc of this query */
drh1b2e0322002-03-03 02:49:51 +00001981 int *pParentAgg /* True if pParent uses aggregate functions */
drhcce7d172000-05-31 15:34:51 +00001982){
drhd8bc7082000-06-07 23:51:50 +00001983 int i;
drhcce7d172000-05-31 15:34:51 +00001984 WhereInfo *pWInfo;
1985 Vdbe *v;
1986 int isAgg = 0; /* True for select lists like "count(*)" */
drha2e00042002-01-22 03:13:42 +00001987 ExprList *pEList; /* List of columns to extract. */
drhad3cab52002-05-24 02:04:32 +00001988 SrcList *pTabList; /* List of tables to select from */
drh9bb61fe2000-06-05 16:01:39 +00001989 Expr *pWhere; /* The WHERE clause. May be NULL */
1990 ExprList *pOrderBy; /* The ORDER BY clause. May be NULL */
drh22827922000-06-06 17:27:05 +00001991 ExprList *pGroupBy; /* The GROUP BY clause. May be NULL */
1992 Expr *pHaving; /* The HAVING clause. May be NULL */
drh19a775c2000-06-05 18:54:46 +00001993 int isDistinct; /* True if the DISTINCT keyword is present */
1994 int distinct; /* Table to use for the distinct set */
drh1d83f052002-02-17 00:30:36 +00001995 int rc = 1; /* Value to return from this function */
drh9bb61fe2000-06-05 16:01:39 +00001996
drhdaffd0e2001-04-11 14:28:42 +00001997 if( sqlite_malloc_failed || pParse->nErr || p==0 ) return 1;
drhe22a3342003-04-22 20:30:37 +00001998 if( sqliteAuthCheck(pParse, SQLITE_SELECT, 0, 0, 0) ) return 1;
drhdaffd0e2001-04-11 14:28:42 +00001999
drh82c3d632000-06-06 21:56:07 +00002000 /* If there is are a sequence of queries, do the earlier ones first.
2001 */
2002 if( p->pPrior ){
2003 return multiSelect(pParse, p, eDest, iParm);
2004 }
2005
2006 /* Make local copies of the parameters for this query.
2007 */
drh9bb61fe2000-06-05 16:01:39 +00002008 pTabList = p->pSrc;
2009 pWhere = p->pWhere;
2010 pOrderBy = p->pOrderBy;
drh22827922000-06-06 17:27:05 +00002011 pGroupBy = p->pGroupBy;
2012 pHaving = p->pHaving;
drh19a775c2000-06-05 18:54:46 +00002013 isDistinct = p->isDistinct;
drh9bb61fe2000-06-05 16:01:39 +00002014
drh6a3ea0e2003-05-02 14:32:12 +00002015 /* Allocate VDBE cursors for each table in the FROM clause
drh10e5e3c2000-06-08 00:19:02 +00002016 */
drh6a3ea0e2003-05-02 14:32:12 +00002017 sqliteSrcListAssignCursors(pParse, pTabList);
drh10e5e3c2000-06-08 00:19:02 +00002018
drh9bb61fe2000-06-05 16:01:39 +00002019 /*
2020 ** Do not even attempt to generate any code if we have already seen
2021 ** errors before this routine starts.
2022 */
drh1d83f052002-02-17 00:30:36 +00002023 if( pParse->nErr>0 ) goto select_end;
drhcce7d172000-05-31 15:34:51 +00002024
drhe78e8282003-01-19 03:59:45 +00002025 /* Expand any "*" terms in the result set. (For example the "*" in
2026 ** "SELECT * FROM t1") The fillInColumnlist() routine also does some
2027 ** other housekeeping - see the header comment for details.
drhcce7d172000-05-31 15:34:51 +00002028 */
drhd8bc7082000-06-07 23:51:50 +00002029 if( fillInColumnList(pParse, p) ){
drh1d83f052002-02-17 00:30:36 +00002030 goto select_end;
drhcce7d172000-05-31 15:34:51 +00002031 }
drhad2d8302002-05-24 20:31:36 +00002032 pWhere = p->pWhere;
drhd8bc7082000-06-07 23:51:50 +00002033 pEList = p->pEList;
drh1d83f052002-02-17 00:30:36 +00002034 if( pEList==0 ) goto select_end;
drhcce7d172000-05-31 15:34:51 +00002035
drh22827922000-06-06 17:27:05 +00002036 /* If writing to memory or generating a set
2037 ** only a single column may be output.
drh19a775c2000-06-05 18:54:46 +00002038 */
drhfef52082000-06-06 01:50:43 +00002039 if( (eDest==SRT_Mem || eDest==SRT_Set) && pEList->nExpr>1 ){
drhda93d232003-03-31 02:12:46 +00002040 sqliteErrorMsg(pParse, "only a single result allowed for "
2041 "a SELECT that is part of an expression");
drh1d83f052002-02-17 00:30:36 +00002042 goto select_end;
drh19a775c2000-06-05 18:54:46 +00002043 }
2044
drhc926afb2002-06-20 03:38:26 +00002045 /* ORDER BY is ignored for some destinations.
drh22827922000-06-06 17:27:05 +00002046 */
drhc926afb2002-06-20 03:38:26 +00002047 switch( eDest ){
2048 case SRT_Union:
2049 case SRT_Except:
2050 case SRT_Discard:
2051 pOrderBy = 0;
2052 break;
2053 default:
2054 break;
drh22827922000-06-06 17:27:05 +00002055 }
2056
drh10e5e3c2000-06-08 00:19:02 +00002057 /* At this point, we should have allocated all the cursors that we
drh832508b2002-03-02 17:04:07 +00002058 ** need to handle subquerys and temporary tables.
drh10e5e3c2000-06-08 00:19:02 +00002059 **
drh967e8b72000-06-21 13:59:10 +00002060 ** Resolve the column names and do a semantics check on all the expressions.
drh22827922000-06-06 17:27:05 +00002061 */
drh4794b982000-06-06 13:54:14 +00002062 for(i=0; i<pEList->nExpr; i++){
drh6a3ea0e2003-05-02 14:32:12 +00002063 if( sqliteExprResolveIds(pParse, pTabList, 0, pEList->a[i].pExpr) ){
drh1d83f052002-02-17 00:30:36 +00002064 goto select_end;
drhcce7d172000-05-31 15:34:51 +00002065 }
drh22827922000-06-06 17:27:05 +00002066 if( sqliteExprCheck(pParse, pEList->a[i].pExpr, 1, &isAgg) ){
drh1d83f052002-02-17 00:30:36 +00002067 goto select_end;
drhcce7d172000-05-31 15:34:51 +00002068 }
2069 }
drhcce7d172000-05-31 15:34:51 +00002070 if( pWhere ){
drh6a3ea0e2003-05-02 14:32:12 +00002071 if( sqliteExprResolveIds(pParse, pTabList, pEList, pWhere) ){
drh1d83f052002-02-17 00:30:36 +00002072 goto select_end;
drhcce7d172000-05-31 15:34:51 +00002073 }
2074 if( sqliteExprCheck(pParse, pWhere, 0, 0) ){
drh1d83f052002-02-17 00:30:36 +00002075 goto select_end;
drhcce7d172000-05-31 15:34:51 +00002076 }
2077 }
drhc66c5a22002-12-03 02:34:49 +00002078 if( pHaving ){
2079 if( pGroupBy==0 ){
drhda93d232003-03-31 02:12:46 +00002080 sqliteErrorMsg(pParse, "a GROUP BY clause is required before HAVING");
drhc66c5a22002-12-03 02:34:49 +00002081 goto select_end;
2082 }
drh6a3ea0e2003-05-02 14:32:12 +00002083 if( sqliteExprResolveIds(pParse, pTabList, pEList, pHaving) ){
drhc66c5a22002-12-03 02:34:49 +00002084 goto select_end;
2085 }
2086 if( sqliteExprCheck(pParse, pHaving, 1, &isAgg) ){
2087 goto select_end;
2088 }
2089 }
drhcce7d172000-05-31 15:34:51 +00002090 if( pOrderBy ){
2091 for(i=0; i<pOrderBy->nExpr; i++){
drh88eee382003-01-31 17:16:36 +00002092 int iCol;
drh22827922000-06-06 17:27:05 +00002093 Expr *pE = pOrderBy->a[i].pExpr;
drh88eee382003-01-31 17:16:36 +00002094 if( sqliteExprIsInteger(pE, &iCol) && iCol>0 && iCol<=pEList->nExpr ){
2095 sqliteExprDelete(pE);
2096 pE = pOrderBy->a[i].pExpr = sqliteExprDup(pEList->a[iCol-1].pExpr);
2097 }
drh6a3ea0e2003-05-02 14:32:12 +00002098 if( sqliteExprResolveIds(pParse, pTabList, pEList, pE) ){
drh88eee382003-01-31 17:16:36 +00002099 goto select_end;
2100 }
2101 if( sqliteExprCheck(pParse, pE, isAgg, 0) ){
2102 goto select_end;
2103 }
drh92086432002-01-22 14:11:29 +00002104 if( sqliteExprIsConstant(pE) ){
drhe4de1fe2002-06-02 16:09:01 +00002105 if( sqliteExprIsInteger(pE, &iCol)==0 ){
drhda93d232003-03-31 02:12:46 +00002106 sqliteErrorMsg(pParse,
2107 "ORDER BY terms must not be non-integer constants");
drhe4de1fe2002-06-02 16:09:01 +00002108 goto select_end;
2109 }else if( iCol<=0 || iCol>pEList->nExpr ){
drhda93d232003-03-31 02:12:46 +00002110 sqliteErrorMsg(pParse,
2111 "ORDER BY column number %d out of range - should be "
drhe4de1fe2002-06-02 16:09:01 +00002112 "between 1 and %d", iCol, pEList->nExpr);
drhe4de1fe2002-06-02 16:09:01 +00002113 goto select_end;
2114 }
drhcce7d172000-05-31 15:34:51 +00002115 }
2116 }
2117 }
drh22827922000-06-06 17:27:05 +00002118 if( pGroupBy ){
2119 for(i=0; i<pGroupBy->nExpr; i++){
drh88eee382003-01-31 17:16:36 +00002120 int iCol;
drh22827922000-06-06 17:27:05 +00002121 Expr *pE = pGroupBy->a[i].pExpr;
drh88eee382003-01-31 17:16:36 +00002122 if( sqliteExprIsInteger(pE, &iCol) && iCol>0 && iCol<=pEList->nExpr ){
2123 sqliteExprDelete(pE);
2124 pE = pGroupBy->a[i].pExpr = sqliteExprDup(pEList->a[iCol-1].pExpr);
drh92086432002-01-22 14:11:29 +00002125 }
drh6a3ea0e2003-05-02 14:32:12 +00002126 if( sqliteExprResolveIds(pParse, pTabList, pEList, pE) ){
drh1d83f052002-02-17 00:30:36 +00002127 goto select_end;
drh22827922000-06-06 17:27:05 +00002128 }
2129 if( sqliteExprCheck(pParse, pE, isAgg, 0) ){
drh1d83f052002-02-17 00:30:36 +00002130 goto select_end;
drh22827922000-06-06 17:27:05 +00002131 }
drh88eee382003-01-31 17:16:36 +00002132 if( sqliteExprIsConstant(pE) ){
2133 if( sqliteExprIsInteger(pE, &iCol)==0 ){
drhda93d232003-03-31 02:12:46 +00002134 sqliteErrorMsg(pParse,
2135 "GROUP BY terms must not be non-integer constants");
drh88eee382003-01-31 17:16:36 +00002136 goto select_end;
2137 }else if( iCol<=0 || iCol>pEList->nExpr ){
drhda93d232003-03-31 02:12:46 +00002138 sqliteErrorMsg(pParse,
2139 "GROUP BY column number %d out of range - should be "
drh88eee382003-01-31 17:16:36 +00002140 "between 1 and %d", iCol, pEList->nExpr);
drh88eee382003-01-31 17:16:36 +00002141 goto select_end;
2142 }
2143 }
drh22827922000-06-06 17:27:05 +00002144 }
2145 }
drhcce7d172000-05-31 15:34:51 +00002146
drhd820cb12002-02-18 03:21:45 +00002147 /* Begin generating code.
2148 */
2149 v = sqliteGetVdbe(pParse);
2150 if( v==0 ) goto select_end;
2151
drhe78e8282003-01-19 03:59:45 +00002152 /* Identify column names if we will be using them in a callback. This
2153 ** step is skipped if the output is going to some other destination.
drh0bb28102002-05-08 11:54:14 +00002154 */
2155 if( eDest==SRT_Callback ){
drh6a3ea0e2003-05-02 14:32:12 +00002156 generateColumnNames(pParse, pTabList, pEList);
drh0bb28102002-05-08 11:54:14 +00002157 }
2158
drhe5f50722003-07-19 00:44:14 +00002159 /* Check for the special case of a min() or max() function by itself
2160 ** in the result set.
2161 */
2162 if( simpleMinMaxQuery(pParse, p, eDest, iParm) ){
2163 rc = 0;
2164 goto select_end;
2165 }
2166
drhd820cb12002-02-18 03:21:45 +00002167 /* Generate code for all sub-queries in the FROM clause
2168 */
drhad3cab52002-05-24 02:04:32 +00002169 for(i=0; i<pTabList->nSrc; i++){
drh5cf590c2003-04-24 01:45:04 +00002170 const char *zSavedAuthContext;
drhc31c2eb2003-05-02 16:04:17 +00002171 int needRestoreContext;
2172
drha76b5df2002-02-23 02:32:10 +00002173 if( pTabList->a[i].pSelect==0 ) continue;
drh5cf590c2003-04-24 01:45:04 +00002174 if( pTabList->a[i].zName!=0 ){
2175 zSavedAuthContext = pParse->zAuthContext;
2176 pParse->zAuthContext = pTabList->a[i].zName;
drhc31c2eb2003-05-02 16:04:17 +00002177 needRestoreContext = 1;
2178 }else{
2179 needRestoreContext = 0;
drh5cf590c2003-04-24 01:45:04 +00002180 }
drh6a3ea0e2003-05-02 14:32:12 +00002181 sqliteSelect(pParse, pTabList->a[i].pSelect, SRT_TempTable,
2182 pTabList->a[i].iCursor, p, i, &isAgg);
drhc31c2eb2003-05-02 16:04:17 +00002183 if( needRestoreContext ){
drh5cf590c2003-04-24 01:45:04 +00002184 pParse->zAuthContext = zSavedAuthContext;
2185 }
drh1b2e0322002-03-03 02:49:51 +00002186 pTabList = p->pSrc;
2187 pWhere = p->pWhere;
drhc31c2eb2003-05-02 16:04:17 +00002188 if( eDest!=SRT_Union && eDest!=SRT_Except && eDest!=SRT_Discard ){
drhacd4c692002-03-07 02:02:51 +00002189 pOrderBy = p->pOrderBy;
2190 }
drh1b2e0322002-03-03 02:49:51 +00002191 pGroupBy = p->pGroupBy;
2192 pHaving = p->pHaving;
2193 isDistinct = p->isDistinct;
drhd820cb12002-02-18 03:21:45 +00002194 }
2195
drh832508b2002-03-02 17:04:07 +00002196 /* Check to see if this is a subquery that can be "flattened" into its parent.
2197 ** If flattening is a possiblity, do so and return immediately.
2198 */
drh1b2e0322002-03-03 02:49:51 +00002199 if( pParent && pParentAgg &&
drh8c74a8c2002-08-25 19:20:40 +00002200 flattenSubquery(pParse, pParent, parentTab, *pParentAgg, isAgg) ){
drh1b2e0322002-03-03 02:49:51 +00002201 if( isAgg ) *pParentAgg = 1;
drh832508b2002-03-02 17:04:07 +00002202 return rc;
2203 }
drh832508b2002-03-02 17:04:07 +00002204
drh7b58dae2003-07-20 01:16:46 +00002205 /* Set the limiter.
2206 */
2207 computeLimitRegisters(pParse, p);
2208
drhe78e8282003-01-19 03:59:45 +00002209 /* Identify column types if we will be using a callback. This
2210 ** step is skipped if the output is going to a destination other
2211 ** than a callback.
drhe5f50722003-07-19 00:44:14 +00002212 **
2213 ** We have to do this separately from the creation of column names
2214 ** above because if the pTabList contains views then they will not
2215 ** have been resolved and we will not know the column types until
2216 ** now.
drhfcb78a42003-01-18 20:11:05 +00002217 */
2218 if( eDest==SRT_Callback ){
drh6a3ea0e2003-05-02 14:32:12 +00002219 generateColumnTypes(pParse, pTabList, pEList);
drhfcb78a42003-01-18 20:11:05 +00002220 }
2221
drh2d0794e2002-03-03 03:03:52 +00002222 /* If the output is destined for a temporary table, open that table.
2223 */
2224 if( eDest==SRT_TempTable ){
2225 sqliteVdbeAddOp(v, OP_OpenTemp, iParm, 0);
2226 }
2227
drh22827922000-06-06 17:27:05 +00002228 /* Do an analysis of aggregate expressions.
drhefb72512000-05-31 20:00:52 +00002229 */
drhd820cb12002-02-18 03:21:45 +00002230 sqliteAggregateInfoReset(pParse);
drhbb999ef2003-02-02 12:41:25 +00002231 if( isAgg || pGroupBy ){
drh0bce8352002-02-28 00:41:10 +00002232 assert( pParse->nAgg==0 );
drhbb999ef2003-02-02 12:41:25 +00002233 isAgg = 1;
drh22827922000-06-06 17:27:05 +00002234 for(i=0; i<pEList->nExpr; i++){
2235 if( sqliteExprAnalyzeAggregates(pParse, pEList->a[i].pExpr) ){
drh1d83f052002-02-17 00:30:36 +00002236 goto select_end;
drh22827922000-06-06 17:27:05 +00002237 }
2238 }
2239 if( pGroupBy ){
2240 for(i=0; i<pGroupBy->nExpr; i++){
2241 if( sqliteExprAnalyzeAggregates(pParse, pGroupBy->a[i].pExpr) ){
drh1d83f052002-02-17 00:30:36 +00002242 goto select_end;
drh22827922000-06-06 17:27:05 +00002243 }
2244 }
2245 }
2246 if( pHaving && sqliteExprAnalyzeAggregates(pParse, pHaving) ){
drh1d83f052002-02-17 00:30:36 +00002247 goto select_end;
drh22827922000-06-06 17:27:05 +00002248 }
drh191b6902000-06-08 11:13:01 +00002249 if( pOrderBy ){
2250 for(i=0; i<pOrderBy->nExpr; i++){
2251 if( sqliteExprAnalyzeAggregates(pParse, pOrderBy->a[i].pExpr) ){
drh1d83f052002-02-17 00:30:36 +00002252 goto select_end;
drh191b6902000-06-08 11:13:01 +00002253 }
2254 }
2255 }
drhefb72512000-05-31 20:00:52 +00002256 }
2257
drh22827922000-06-06 17:27:05 +00002258 /* Reset the aggregator
drhcce7d172000-05-31 15:34:51 +00002259 */
2260 if( isAgg ){
drh99fcd712001-10-13 01:06:47 +00002261 sqliteVdbeAddOp(v, OP_AggReset, 0, pParse->nAgg);
drhe5095352002-02-24 03:25:14 +00002262 for(i=0; i<pParse->nAgg; i++){
drh0bce8352002-02-28 00:41:10 +00002263 FuncDef *pFunc;
2264 if( (pFunc = pParse->aAgg[i].pFunc)!=0 && pFunc->xFinalize!=0 ){
drh1350b032002-02-27 19:00:20 +00002265 sqliteVdbeAddOp(v, OP_AggInit, 0, i);
drh0bce8352002-02-28 00:41:10 +00002266 sqliteVdbeChangeP3(v, -1, (char*)pFunc, P3_POINTER);
drhe5095352002-02-24 03:25:14 +00002267 }
2268 }
drh1bee3d72001-10-15 00:44:35 +00002269 if( pGroupBy==0 ){
2270 sqliteVdbeAddOp(v, OP_String, 0, 0);
drh1bee3d72001-10-15 00:44:35 +00002271 sqliteVdbeAddOp(v, OP_AggFocus, 0, 0);
drh1bee3d72001-10-15 00:44:35 +00002272 }
drhcce7d172000-05-31 15:34:51 +00002273 }
2274
drh19a775c2000-06-05 18:54:46 +00002275 /* Initialize the memory cell to NULL
2276 */
drhfef52082000-06-06 01:50:43 +00002277 if( eDest==SRT_Mem ){
drh99fcd712001-10-13 01:06:47 +00002278 sqliteVdbeAddOp(v, OP_String, 0, 0);
drh8721ce42001-11-07 14:22:00 +00002279 sqliteVdbeAddOp(v, OP_MemStore, iParm, 1);
drh19a775c2000-06-05 18:54:46 +00002280 }
2281
drh832508b2002-03-02 17:04:07 +00002282 /* Open a temporary table to use for the distinct set.
drhefb72512000-05-31 20:00:52 +00002283 */
drh19a775c2000-06-05 18:54:46 +00002284 if( isDistinct ){
drh832508b2002-03-02 17:04:07 +00002285 distinct = pParse->nTab++;
drhc6b52df2002-01-04 03:09:29 +00002286 sqliteVdbeAddOp(v, OP_OpenTemp, distinct, 1);
drh832508b2002-03-02 17:04:07 +00002287 }else{
2288 distinct = -1;
drhefb72512000-05-31 20:00:52 +00002289 }
drh832508b2002-03-02 17:04:07 +00002290
2291 /* Begin the database scan
2292 */
drh6a3ea0e2003-05-02 14:32:12 +00002293 pWInfo = sqliteWhereBegin(pParse, pTabList, pWhere, 0,
drh68d2e592002-08-04 00:52:38 +00002294 pGroupBy ? 0 : &pOrderBy);
drh1d83f052002-02-17 00:30:36 +00002295 if( pWInfo==0 ) goto select_end;
drhcce7d172000-05-31 15:34:51 +00002296
drh22827922000-06-06 17:27:05 +00002297 /* Use the standard inner loop if we are not dealing with
2298 ** aggregates
drhcce7d172000-05-31 15:34:51 +00002299 */
drhda9d6c42000-05-31 18:20:14 +00002300 if( !isAgg ){
drhdf199a22002-06-14 22:38:41 +00002301 if( selectInnerLoop(pParse, p, pEList, 0, 0, pOrderBy, distinct, eDest,
2302 iParm, pWInfo->iContinue, pWInfo->iBreak) ){
drh1d83f052002-02-17 00:30:36 +00002303 goto select_end;
drhda9d6c42000-05-31 18:20:14 +00002304 }
drhcce7d172000-05-31 15:34:51 +00002305 }
drhefb72512000-05-31 20:00:52 +00002306
drhe3184742002-06-19 14:27:05 +00002307 /* If we are dealing with aggregates, then do the special aggregate
drh22827922000-06-06 17:27:05 +00002308 ** processing.
drhefb72512000-05-31 20:00:52 +00002309 */
drh22827922000-06-06 17:27:05 +00002310 else{
drh22827922000-06-06 17:27:05 +00002311 if( pGroupBy ){
drh1bee3d72001-10-15 00:44:35 +00002312 int lbl1;
drh22827922000-06-06 17:27:05 +00002313 for(i=0; i<pGroupBy->nExpr; i++){
2314 sqliteExprCode(pParse, pGroupBy->a[i].pExpr);
2315 }
drh99fcd712001-10-13 01:06:47 +00002316 sqliteVdbeAddOp(v, OP_MakeKey, pGroupBy->nExpr, 0);
drh491791a2002-07-18 00:34:09 +00002317 if( pParse->db->file_format>=4 ) sqliteAddKeyType(v, pGroupBy);
drh1bee3d72001-10-15 00:44:35 +00002318 lbl1 = sqliteVdbeMakeLabel(v);
drh99fcd712001-10-13 01:06:47 +00002319 sqliteVdbeAddOp(v, OP_AggFocus, 0, lbl1);
drh22827922000-06-06 17:27:05 +00002320 for(i=0; i<pParse->nAgg; i++){
2321 if( pParse->aAgg[i].isAgg ) continue;
2322 sqliteExprCode(pParse, pParse->aAgg[i].pExpr);
drh99fcd712001-10-13 01:06:47 +00002323 sqliteVdbeAddOp(v, OP_AggSet, 0, i);
drhcce7d172000-05-31 15:34:51 +00002324 }
drh22827922000-06-06 17:27:05 +00002325 sqliteVdbeResolveLabel(v, lbl1);
drhcce7d172000-05-31 15:34:51 +00002326 }
drh22827922000-06-06 17:27:05 +00002327 for(i=0; i<pParse->nAgg; i++){
2328 Expr *pE;
drh0bce8352002-02-28 00:41:10 +00002329 int j;
drh22827922000-06-06 17:27:05 +00002330 if( !pParse->aAgg[i].isAgg ) continue;
2331 pE = pParse->aAgg[i].pExpr;
drh22827922000-06-06 17:27:05 +00002332 assert( pE->op==TK_AGG_FUNCTION );
drh0bce8352002-02-28 00:41:10 +00002333 if( pE->pList ){
2334 for(j=0; j<pE->pList->nExpr; j++){
2335 sqliteExprCode(pParse, pE->pList->a[j].pExpr);
2336 }
drhe5095352002-02-24 03:25:14 +00002337 }
drh0bce8352002-02-28 00:41:10 +00002338 sqliteVdbeAddOp(v, OP_Integer, i, 0);
drhf55f25f2002-02-28 01:46:11 +00002339 sqliteVdbeAddOp(v, OP_AggFunc, 0, pE->pList ? pE->pList->nExpr : 0);
drh0bce8352002-02-28 00:41:10 +00002340 assert( pParse->aAgg[i].pFunc!=0 );
2341 assert( pParse->aAgg[i].pFunc->xStep!=0 );
2342 sqliteVdbeChangeP3(v, -1, (char*)pParse->aAgg[i].pFunc, P3_POINTER);
drh22827922000-06-06 17:27:05 +00002343 }
drhcce7d172000-05-31 15:34:51 +00002344 }
2345
2346 /* End the database scan loop.
2347 */
2348 sqliteWhereEnd(pWInfo);
2349
drh22827922000-06-06 17:27:05 +00002350 /* If we are processing aggregates, we need to set up a second loop
2351 ** over all of the aggregate values and process them.
2352 */
2353 if( isAgg ){
2354 int endagg = sqliteVdbeMakeLabel(v);
2355 int startagg;
drh99fcd712001-10-13 01:06:47 +00002356 startagg = sqliteVdbeAddOp(v, OP_AggNext, 0, endagg);
drh22827922000-06-06 17:27:05 +00002357 pParse->useAgg = 1;
2358 if( pHaving ){
drhf5905aa2002-05-26 20:54:33 +00002359 sqliteExprIfFalse(pParse, pHaving, startagg, 1);
drh22827922000-06-06 17:27:05 +00002360 }
drhdf199a22002-06-14 22:38:41 +00002361 if( selectInnerLoop(pParse, p, pEList, 0, 0, pOrderBy, distinct, eDest,
2362 iParm, startagg, endagg) ){
drh1d83f052002-02-17 00:30:36 +00002363 goto select_end;
drh22827922000-06-06 17:27:05 +00002364 }
drh99fcd712001-10-13 01:06:47 +00002365 sqliteVdbeAddOp(v, OP_Goto, 0, startagg);
2366 sqliteVdbeResolveLabel(v, endagg);
2367 sqliteVdbeAddOp(v, OP_Noop, 0, 0);
drh22827922000-06-06 17:27:05 +00002368 pParse->useAgg = 0;
2369 }
2370
drhcce7d172000-05-31 15:34:51 +00002371 /* If there is an ORDER BY clause, then we need to sort the results
2372 ** and send them to the callback one by one.
2373 */
2374 if( pOrderBy ){
drhc926afb2002-06-20 03:38:26 +00002375 generateSortTail(p, v, pEList->nExpr, eDest, iParm);
drhcce7d172000-05-31 15:34:51 +00002376 }
drh6a535342001-10-19 16:44:56 +00002377
2378
2379 /* Issue a null callback if that is what the user wants.
2380 */
drh326dce72003-01-29 14:06:07 +00002381 if( eDest==SRT_Callback &&
2382 (pParse->useCallback==0 || (pParse->db->flags & SQLITE_NullCallback)!=0)
2383 ){
drh6a535342001-10-19 16:44:56 +00002384 sqliteVdbeAddOp(v, OP_NullCallback, pEList->nExpr, 0);
2385 }
2386
drh1d83f052002-02-17 00:30:36 +00002387 /* The SELECT was successfully coded. Set the return code to 0
2388 ** to indicate no errors.
2389 */
2390 rc = 0;
2391
2392 /* Control jumps to here if an error is encountered above, or upon
2393 ** successful coding of the SELECT.
2394 */
2395select_end:
2396 sqliteAggregateInfoReset(pParse);
2397 return rc;
drhcce7d172000-05-31 15:34:51 +00002398}