blob: a885a44c6ad1305b22499e13a510d12248d8304b [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**
drh315555c2002-10-20 15:53:03 +000015** $Id: select.c,v 1.113 2002/10/20 15:53:04 drh Exp $
drhcce7d172000-05-31 15:34:51 +000016*/
17#include "sqliteInt.h"
18
drh315555c2002-10-20 15:53:03 +000019
drhcce7d172000-05-31 15:34:51 +000020/*
drh9bb61fe2000-06-05 16:01:39 +000021** Allocate a new Select structure and return a pointer to that
22** structure.
drhcce7d172000-05-31 15:34:51 +000023*/
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 */
33 int nOffset /* OFFSET value. -1 means not used */
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;
drhdaffd0e2001-04-11 14:28:42 +000055 }
drh9bb61fe2000-06-05 16:01:39 +000056 return pNew;
57}
58
59/*
drh01f3f252002-05-24 16:14:15 +000060** Given 1 to 3 identifiers preceeding the JOIN keyword, determine the
61** type of join. Return an integer constant that expresses that type
62** in terms of the following bit values:
63**
64** JT_INNER
65** JT_OUTER
66** JT_NATURAL
67** JT_LEFT
68** JT_RIGHT
69**
70** A full outer join is the combination of JT_LEFT and JT_RIGHT.
71**
72** If an illegal or unsupported join type is seen, then still return
73** a join type, but put an error in the pParse structure.
74*/
75int sqliteJoinType(Parse *pParse, Token *pA, Token *pB, Token *pC){
76 int jointype = 0;
77 Token *apAll[3];
78 Token *p;
79 static struct {
80 const char *zKeyword;
81 int nChar;
82 int code;
83 } keywords[] = {
84 { "natural", 7, JT_NATURAL },
drh195e6962002-05-25 00:18:20 +000085 { "left", 4, JT_LEFT|JT_OUTER },
86 { "right", 5, JT_RIGHT|JT_OUTER },
87 { "full", 4, JT_LEFT|JT_RIGHT|JT_OUTER },
drh01f3f252002-05-24 16:14:15 +000088 { "outer", 5, JT_OUTER },
89 { "inner", 5, JT_INNER },
90 { "cross", 5, JT_INNER },
91 };
92 int i, j;
93 apAll[0] = pA;
94 apAll[1] = pB;
95 apAll[2] = pC;
drh195e6962002-05-25 00:18:20 +000096 for(i=0; i<3 && apAll[i]; i++){
drh01f3f252002-05-24 16:14:15 +000097 p = apAll[i];
98 for(j=0; j<sizeof(keywords)/sizeof(keywords[0]); j++){
99 if( p->n==keywords[j].nChar
100 && sqliteStrNICmp(p->z, keywords[j].zKeyword, p->n)==0 ){
101 jointype |= keywords[j].code;
102 break;
103 }
104 }
105 if( j>=sizeof(keywords)/sizeof(keywords[0]) ){
106 jointype |= JT_ERROR;
107 break;
108 }
109 }
drhad2d8302002-05-24 20:31:36 +0000110 if(
111 (jointype & (JT_INNER|JT_OUTER))==(JT_INNER|JT_OUTER) ||
drh195e6962002-05-25 00:18:20 +0000112 (jointype & JT_ERROR)!=0
drhad2d8302002-05-24 20:31:36 +0000113 ){
drh01f3f252002-05-24 16:14:15 +0000114 static Token dummy = { 0, 0 };
115 char *zSp1 = " ", *zSp2 = " ";
116 if( pB==0 ){ pB = &dummy; zSp1 = 0; }
117 if( pC==0 ){ pC = &dummy; zSp2 = 0; }
118 sqliteSetNString(&pParse->zErrMsg, "unknown or unsupported join type: ", 0,
119 pA->z, pA->n, zSp1, 1, pB->z, pB->n, zSp2, 1, pC->z, pC->n, 0);
120 pParse->nErr++;
121 jointype = JT_INNER;
drh195e6962002-05-25 00:18:20 +0000122 }else if( jointype & JT_RIGHT ){
123 sqliteSetString(&pParse->zErrMsg,
124 "RIGHT and FULL OUTER JOINs are not currently supported", 0);
125 pParse->nErr++;
126 jointype = JT_INNER;
drh01f3f252002-05-24 16:14:15 +0000127 }
128 return jointype;
129}
130
131/*
drhad2d8302002-05-24 20:31:36 +0000132** Return the index of a column in a table. Return -1 if the column
133** is not contained in the table.
134*/
135static int columnIndex(Table *pTab, const char *zCol){
136 int i;
137 for(i=0; i<pTab->nCol; i++){
138 if( sqliteStrICmp(pTab->aCol[i].zName, zCol)==0 ) return i;
139 }
140 return -1;
141}
142
143/*
144** Add a term to the WHERE expression in *ppExpr that requires the
145** zCol column to be equal in the two tables pTab1 and pTab2.
146*/
147static void addWhereTerm(
148 const char *zCol, /* Name of the column */
149 const Table *pTab1, /* First table */
150 const Table *pTab2, /* Second table */
151 Expr **ppExpr /* Add the equality term to this expression */
152){
153 Token dummy;
154 Expr *pE1a, *pE1b, *pE1c;
155 Expr *pE2a, *pE2b, *pE2c;
156 Expr *pE;
157
158 dummy.z = zCol;
159 dummy.n = strlen(zCol);
drh4b59ab52002-08-24 18:24:51 +0000160 dummy.base = 1;
161 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);
drh1cc093c2002-06-24 22:01:57 +0000173 pE->isJoinExpr = 1;
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/*
drh1cc093c2002-06-24 22:01:57 +0000182** Set the Expr.isJoinExpr flag on all terms of the given expression.
183**
184** The Expr.isJoinExpr flag is used at on terms of an expression to tell
185** the LEFT OUTER JOIN processing logic that this term is part of the
186** join restriction and not a part of the more general WHERE clause.
187*/
188static void setJoinExpr(Expr *p){
189 while( p ){
190 p->isJoinExpr = 1;
191 setJoinExpr(p->pLeft);
192 p = p->pRight;
193 }
194}
195
196/*
drhad2d8302002-05-24 20:31:36 +0000197** This routine processes the join information for a SELECT statement.
198** ON and USING clauses are converted into extra terms of the WHERE clause.
199** NATURAL joins also create extra WHERE clause terms.
200**
201** This routine returns the number of errors encountered.
202*/
203static int sqliteProcessJoin(Parse *pParse, Select *p){
204 SrcList *pSrc;
205 int i, j;
206 pSrc = p->pSrc;
207 for(i=0; i<pSrc->nSrc-1; i++){
208 struct SrcList_item *pTerm = &pSrc->a[i];
209 struct SrcList_item *pOther = &pSrc->a[i+1];
210
211 if( pTerm->pTab==0 || pOther->pTab==0 ) continue;
212
213 /* When the NATURAL keyword is present, add WHERE clause terms for
214 ** every column that the two tables have in common.
215 */
216 if( pTerm->jointype & JT_NATURAL ){
217 Table *pTab;
218 if( pTerm->pOn || pTerm->pUsing ){
219 sqliteSetString(&pParse->zErrMsg, "a NATURAL join may not have "
220 "an ON or USING clause", 0);
221 pParse->nErr++;
222 return 1;
223 }
224 pTab = pTerm->pTab;
225 for(j=0; j<pTab->nCol; j++){
226 if( columnIndex(pOther->pTab, pTab->aCol[j].zName)>=0 ){
227 addWhereTerm(pTab->aCol[j].zName, pTab, pOther->pTab, &p->pWhere);
228 }
229 }
230 }
231
232 /* Disallow both ON and USING clauses in the same join
233 */
234 if( pTerm->pOn && pTerm->pUsing ){
235 sqliteSetString(&pParse->zErrMsg, "cannot have both ON and USING "
236 "clauses in the same join", 0);
237 pParse->nErr++;
238 return 1;
239 }
240
241 /* Add the ON clause to the end of the WHERE clause, connected by
242 ** and AND operator.
243 */
244 if( pTerm->pOn ){
drh1cc093c2002-06-24 22:01:57 +0000245 setJoinExpr(pTerm->pOn);
drhad2d8302002-05-24 20:31:36 +0000246 if( p->pWhere==0 ){
247 p->pWhere = pTerm->pOn;
248 }else{
249 p->pWhere = sqliteExpr(TK_AND, p->pWhere, pTerm->pOn, 0);
250 }
251 pTerm->pOn = 0;
252 }
253
254 /* Create extra terms on the WHERE clause for each column named
255 ** in the USING clause. Example: If the two tables to be joined are
256 ** A and B and the USING clause names X, Y, and Z, then add this
257 ** to the WHERE clause: A.X=B.X AND A.Y=B.Y AND A.Z=B.Z
258 ** Report an error if any column mentioned in the USING clause is
259 ** not contained in both tables to be joined.
260 */
261 if( pTerm->pUsing ){
262 IdList *pList;
263 int j;
264 assert( i<pSrc->nSrc-1 );
265 pList = pTerm->pUsing;
266 for(j=0; j<pList->nId; j++){
drhbf5cd972002-06-24 12:20:23 +0000267 if( columnIndex(pTerm->pTab, pList->a[j].zName)<0 ||
268 columnIndex(pOther->pTab, pList->a[j].zName)<0 ){
drhad2d8302002-05-24 20:31:36 +0000269 sqliteSetString(&pParse->zErrMsg, "cannot join using column ",
drhbf5cd972002-06-24 12:20:23 +0000270 pList->a[j].zName, " - column not present in both tables", 0);
drhad2d8302002-05-24 20:31:36 +0000271 pParse->nErr++;
272 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 ){
399 if( p->nOffset>0 ){
drhd11d3822002-06-21 23:01:49 +0000400 int addr = sqliteVdbeCurrentAddr(v);
401 sqliteVdbeAddOp(v, OP_MemIncr, p->nOffset, addr+2);
402 sqliteVdbeAddOp(v, OP_Goto, 0, iContinue);
drhdf199a22002-06-14 22:38:41 +0000403 }
drhd11d3822002-06-21 23:01:49 +0000404 if( p->nLimit>=0 ){
405 sqliteVdbeAddOp(v, OP_MemIncr, p->nLimit, 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: {
drha9f9d1c2002-06-29 02:20:08 +0000482 int lbl = sqliteVdbeMakeLabel(v);
drhc926afb2002-06-20 03:38:26 +0000483 assert( nColumn==1 );
drha9f9d1c2002-06-29 02:20:08 +0000484 sqliteVdbeAddOp(v, OP_IsNull, -1, lbl);
drhc926afb2002-06-20 03:38:26 +0000485 if( pOrderBy ){
486 pushOntoSorter(pParse, v, pOrderBy);
487 }else{
drha9f9d1c2002-06-29 02:20:08 +0000488 sqliteVdbeAddOp(v, OP_String, 0, 0);
drhc926afb2002-06-20 03:38:26 +0000489 sqliteVdbeAddOp(v, OP_PutStrKey, iParm, 0);
490 }
drha9f9d1c2002-06-29 02:20:08 +0000491 sqliteVdbeResolveLabel(v, lbl);
drhc926afb2002-06-20 03:38:26 +0000492 break;
493 }
drh22827922000-06-06 17:27:05 +0000494
drhc926afb2002-06-20 03:38:26 +0000495 /* If this is a scalar select that is part of an expression, then
496 ** store the results in the appropriate memory cell and break out
497 ** of the scan loop.
498 */
499 case SRT_Mem: {
500 assert( nColumn==1 );
501 if( pOrderBy ){
502 pushOntoSorter(pParse, v, pOrderBy);
503 }else{
504 sqliteVdbeAddOp(v, OP_MemStore, iParm, 1);
505 sqliteVdbeAddOp(v, OP_Goto, 0, iBreak);
506 }
507 break;
508 }
drh22827922000-06-06 17:27:05 +0000509
drhf46f9052002-06-22 02:33:38 +0000510 /* Send the data to the callback function.
511 */
512 case SRT_Callback:
513 case SRT_Sorter: {
514 if( pOrderBy ){
515 sqliteVdbeAddOp(v, OP_SortMakeRec, nColumn, 0);
516 pushOntoSorter(pParse, v, pOrderBy);
517 }else{
518 assert( eDest==SRT_Callback );
519 sqliteVdbeAddOp(v, OP_Callback, nColumn, 0);
520 }
521 break;
522 }
523
drh142e30d2002-08-28 03:00:58 +0000524 /* Invoke a subroutine to handle the results. The subroutine itself
525 ** is responsible for popping the results off of the stack.
526 */
527 case SRT_Subroutine: {
drhac82fcf2002-09-08 17:23:41 +0000528 if( pOrderBy ){
529 sqliteVdbeAddOp(v, OP_MakeRecord, nColumn, 0);
530 pushOntoSorter(pParse, v, pOrderBy);
531 }else{
532 sqliteVdbeAddOp(v, OP_Gosub, 0, iParm);
533 }
drh142e30d2002-08-28 03:00:58 +0000534 break;
535 }
536
drhc926afb2002-06-20 03:38:26 +0000537 /* Discard the results. This is used for SELECT statements inside
538 ** the body of a TRIGGER. The purpose of such selects is to call
539 ** user-defined functions that have side effects. We do not care
540 ** about the actual results of the select.
541 */
drhc926afb2002-06-20 03:38:26 +0000542 default: {
drhf46f9052002-06-22 02:33:38 +0000543 assert( eDest==SRT_Discard );
544 sqliteVdbeAddOp(v, OP_Pop, nColumn, 0);
drhc926afb2002-06-20 03:38:26 +0000545 break;
546 }
drh82c3d632000-06-06 21:56:07 +0000547 }
548 return 0;
549}
550
551/*
drhd8bc7082000-06-07 23:51:50 +0000552** If the inner loop was generated using a non-null pOrderBy argument,
553** then the results were placed in a sorter. After the loop is terminated
554** we need to run the sorter and output the results. The following
555** routine generates the code needed to do that.
556*/
drhc926afb2002-06-20 03:38:26 +0000557static void generateSortTail(
558 Select *p, /* The SELECT statement */
559 Vdbe *v, /* Generate code into this VDBE */
560 int nColumn, /* Number of columns of data */
561 int eDest, /* Write the sorted results here */
562 int iParm /* Optional parameter associated with eDest */
563){
drhd8bc7082000-06-07 23:51:50 +0000564 int end = sqliteVdbeMakeLabel(v);
565 int addr;
drhf46f9052002-06-22 02:33:38 +0000566 if( eDest==SRT_Sorter ) return;
drh99fcd712001-10-13 01:06:47 +0000567 sqliteVdbeAddOp(v, OP_Sort, 0, 0);
568 addr = sqliteVdbeAddOp(v, OP_SortNext, 0, end);
drhdf199a22002-06-14 22:38:41 +0000569 if( p->nOffset>0 ){
drhd11d3822002-06-21 23:01:49 +0000570 sqliteVdbeAddOp(v, OP_MemIncr, p->nOffset, addr+4);
571 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
572 sqliteVdbeAddOp(v, OP_Goto, 0, addr);
drhdf199a22002-06-14 22:38:41 +0000573 }
drhd11d3822002-06-21 23:01:49 +0000574 if( p->nLimit>=0 ){
575 sqliteVdbeAddOp(v, OP_MemIncr, p->nLimit, end);
drhdf199a22002-06-14 22:38:41 +0000576 }
drhc926afb2002-06-20 03:38:26 +0000577 switch( eDest ){
578 case SRT_Callback: {
579 sqliteVdbeAddOp(v, OP_SortCallback, nColumn, 0);
580 break;
581 }
582 case SRT_Table:
583 case SRT_TempTable: {
584 sqliteVdbeAddOp(v, OP_NewRecno, iParm, 0);
585 sqliteVdbeAddOp(v, OP_Pull, 1, 0);
586 sqliteVdbeAddOp(v, OP_PutIntKey, iParm, 0);
587 break;
588 }
589 case SRT_Set: {
590 assert( nColumn==1 );
591 sqliteVdbeAddOp(v, OP_IsNull, -1, sqliteVdbeCurrentAddr(v)+3);
592 sqliteVdbeAddOp(v, OP_String, 0, 0);
593 sqliteVdbeAddOp(v, OP_PutStrKey, iParm, 0);
594 break;
595 }
596 case SRT_Mem: {
597 assert( nColumn==1 );
598 sqliteVdbeAddOp(v, OP_MemStore, iParm, 1);
599 sqliteVdbeAddOp(v, OP_Goto, 0, end);
600 break;
601 }
drhac82fcf2002-09-08 17:23:41 +0000602 case SRT_Subroutine: {
603 int i;
604 for(i=0; i<nColumn; i++){
605 sqliteVdbeAddOp(v, OP_Column, -1-i, i);
606 }
607 sqliteVdbeAddOp(v, OP_Gosub, 0, iParm);
608 sqliteVdbeAddOp(v, OP_Pop, 1, 0);
609 break;
610 }
drhc926afb2002-06-20 03:38:26 +0000611 default: {
drhf46f9052002-06-22 02:33:38 +0000612 /* Do nothing */
drhc926afb2002-06-20 03:38:26 +0000613 break;
614 }
615 }
drh99fcd712001-10-13 01:06:47 +0000616 sqliteVdbeAddOp(v, OP_Goto, 0, addr);
617 sqliteVdbeResolveLabel(v, end);
drha8b38d22001-11-01 14:41:34 +0000618 sqliteVdbeAddOp(v, OP_SortReset, 0, 0);
drhd8bc7082000-06-07 23:51:50 +0000619}
620
621/*
drh82c3d632000-06-06 21:56:07 +0000622** Generate code that will tell the VDBE how many columns there
623** are in the result and the name for each column. This information
624** is used to provide "argc" and "azCol[]" values in the callback.
625*/
drh832508b2002-03-02 17:04:07 +0000626static void generateColumnNames(
627 Parse *pParse, /* Parser context */
628 int base, /* VDBE cursor corresponding to first entry in pTabList */
drhad3cab52002-05-24 02:04:32 +0000629 SrcList *pTabList, /* List of tables */
drh832508b2002-03-02 17:04:07 +0000630 ExprList *pEList /* Expressions defining the result set */
631){
drhd8bc7082000-06-07 23:51:50 +0000632 Vdbe *v = pParse->pVdbe;
drh82c3d632000-06-06 21:56:07 +0000633 int i;
drhdaffd0e2001-04-11 14:28:42 +0000634 if( pParse->colNamesSet || v==0 || sqlite_malloc_failed ) return;
drhd8bc7082000-06-07 23:51:50 +0000635 pParse->colNamesSet = 1;
drh5080aaa2002-07-11 12:18:16 +0000636 if( pParse->db->flags & SQLITE_ReportTypes ){
637 sqliteVdbeAddOp(v, OP_ColumnCount, pEList->nExpr*2, 0);
638 }else{
639 sqliteVdbeAddOp(v, OP_ColumnCount, pEList->nExpr, 0);
640 }
drh82c3d632000-06-06 21:56:07 +0000641 for(i=0; i<pEList->nExpr; i++){
642 Expr *p;
drhb1363202002-06-26 02:45:03 +0000643 char *zType = 0;
drh1bee3d72001-10-15 00:44:35 +0000644 int showFullNames;
drh82c3d632000-06-06 21:56:07 +0000645 if( pEList->a[i].zName ){
646 char *zName = pEList->a[i].zName;
drh99fcd712001-10-13 01:06:47 +0000647 sqliteVdbeAddOp(v, OP_ColumnName, i, 0);
648 sqliteVdbeChangeP3(v, -1, zName, strlen(zName));
drh82c3d632000-06-06 21:56:07 +0000649 continue;
650 }
651 p = pEList->a[i].pExpr;
drhdaffd0e2001-04-11 14:28:42 +0000652 if( p==0 ) continue;
drh1bee3d72001-10-15 00:44:35 +0000653 showFullNames = (pParse->db->flags & SQLITE_FullColNames)!=0;
drhfa173a72002-07-10 21:26:00 +0000654 if( p->op==TK_COLUMN && pTabList ){
drh832508b2002-03-02 17:04:07 +0000655 Table *pTab = pTabList->a[p->iTable - base].pTab;
drh97665872002-02-13 23:22:53 +0000656 char *zCol;
drh8aff1012001-12-22 14:49:24 +0000657 int iCol = p->iColumn;
658 if( iCol<0 ) iCol = pTab->iPKey;
drh97665872002-02-13 23:22:53 +0000659 assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
drhb1363202002-06-26 02:45:03 +0000660 if( iCol<0 ){
661 zCol = "_ROWID_";
662 zType = "INTEGER";
663 }else{
664 zCol = pTab->aCol[iCol].zName;
665 zType = pTab->aCol[iCol].zType;
666 }
drh4b59ab52002-08-24 18:24:51 +0000667 if( p->token.z && p->token.z[0] && !showFullNames ){
drhfa173a72002-07-10 21:26:00 +0000668 int addr = sqliteVdbeAddOp(v,OP_ColumnName, i, 0);
drh4b59ab52002-08-24 18:24:51 +0000669 sqliteVdbeChangeP3(v, -1, p->token.z, p->token.n);
drhfa173a72002-07-10 21:26:00 +0000670 sqliteVdbeCompressSpace(v, addr);
671 }else if( pTabList->nSrc>1 || showFullNames ){
drh82c3d632000-06-06 21:56:07 +0000672 char *zName = 0;
drh82c3d632000-06-06 21:56:07 +0000673 char *zTab;
674
drh832508b2002-03-02 17:04:07 +0000675 zTab = pTabList->a[p->iTable - base].zAlias;
drh01a34662001-10-20 12:30:10 +0000676 if( showFullNames || zTab==0 ) zTab = pTab->zName;
drh97665872002-02-13 23:22:53 +0000677 sqliteSetString(&zName, zTab, ".", zCol, 0);
drh99fcd712001-10-13 01:06:47 +0000678 sqliteVdbeAddOp(v, OP_ColumnName, i, 0);
679 sqliteVdbeChangeP3(v, -1, zName, strlen(zName));
drh82c3d632000-06-06 21:56:07 +0000680 sqliteFree(zName);
681 }else{
drh99fcd712001-10-13 01:06:47 +0000682 sqliteVdbeAddOp(v, OP_ColumnName, i, 0);
drh22f70c32002-02-18 01:17:00 +0000683 sqliteVdbeChangeP3(v, -1, zCol, 0);
drh82c3d632000-06-06 21:56:07 +0000684 }
drh4b59ab52002-08-24 18:24:51 +0000685 }else if( p->token.z && p->token.z[0] && !showFullNames ){
drhfa173a72002-07-10 21:26:00 +0000686 int addr = sqliteVdbeAddOp(v,OP_ColumnName, i, 0);
drh4b59ab52002-08-24 18:24:51 +0000687 sqliteVdbeChangeP3(v, -1, p->token.z, p->token.n);
drhfa173a72002-07-10 21:26:00 +0000688 sqliteVdbeCompressSpace(v, addr);
drh4b59ab52002-08-24 18:24:51 +0000689 }else if( p->token.z && p->token.z[0] ){
drh1bee3d72001-10-15 00:44:35 +0000690 int addr = sqliteVdbeAddOp(v,OP_ColumnName, i, 0);
drh4b59ab52002-08-24 18:24:51 +0000691 sqliteVdbeChangeP3(v, -1, p->token.z, p->token.n);
drh1bee3d72001-10-15 00:44:35 +0000692 sqliteVdbeCompressSpace(v, addr);
693 }else{
694 char zName[30];
695 assert( p->op!=TK_COLUMN || pTabList==0 );
696 sprintf(zName, "column%d", i+1);
697 sqliteVdbeAddOp(v, OP_ColumnName, i, 0);
698 sqliteVdbeChangeP3(v, -1, zName, strlen(zName));
drh82c3d632000-06-06 21:56:07 +0000699 }
drh5080aaa2002-07-11 12:18:16 +0000700 if( pParse->db->flags & SQLITE_ReportTypes ){
701 if( zType==0 ){
702 if( sqliteExprType(p)==SQLITE_SO_TEXT ){
703 zType = "TEXT";
704 }else{
705 zType = "NUMERIC";
706 }
drhb1363202002-06-26 02:45:03 +0000707 }
drh5080aaa2002-07-11 12:18:16 +0000708 sqliteVdbeAddOp(v, OP_ColumnName, i + pEList->nExpr, 0);
709 sqliteVdbeChangeP3(v, -1, zType, P3_STATIC);
drhb1363202002-06-26 02:45:03 +0000710 }
drh82c3d632000-06-06 21:56:07 +0000711 }
712}
713
714/*
drhd8bc7082000-06-07 23:51:50 +0000715** Name of the connection operator, used for error messages.
716*/
717static const char *selectOpName(int id){
718 char *z;
719 switch( id ){
720 case TK_ALL: z = "UNION ALL"; break;
721 case TK_INTERSECT: z = "INTERSECT"; break;
722 case TK_EXCEPT: z = "EXCEPT"; break;
723 default: z = "UNION"; break;
724 }
725 return z;
726}
727
728/*
drh315555c2002-10-20 15:53:03 +0000729** Forward declaration
730*/
731static int fillInColumnList(Parse*, Select*);
732
733/*
drh22f70c32002-02-18 01:17:00 +0000734** Given a SELECT statement, generate a Table structure that describes
735** the result set of that SELECT.
736*/
737Table *sqliteResultSetOfSelect(Parse *pParse, char *zTabName, Select *pSelect){
738 Table *pTab;
739 int i;
740 ExprList *pEList;
drh22f70c32002-02-18 01:17:00 +0000741
742 if( fillInColumnList(pParse, pSelect) ){
743 return 0;
744 }
745 pTab = sqliteMalloc( sizeof(Table) );
746 if( pTab==0 ){
747 return 0;
748 }
749 pTab->zName = zTabName ? sqliteStrDup(zTabName) : 0;
750 pEList = pSelect->pEList;
751 pTab->nCol = pEList->nExpr;
drh417be792002-03-03 18:59:40 +0000752 assert( pTab->nCol>0 );
drh22f70c32002-02-18 01:17:00 +0000753 pTab->aCol = sqliteMalloc( sizeof(pTab->aCol[0])*pTab->nCol );
754 for(i=0; i<pTab->nCol; i++){
755 Expr *p;
756 if( pEList->a[i].zName ){
757 pTab->aCol[i].zName = sqliteStrDup(pEList->a[i].zName);
drh4b59ab52002-08-24 18:24:51 +0000758 }else if( (p=pEList->a[i].pExpr)->token.z && p->token.z[0] ){
759 sqliteSetNString(&pTab->aCol[i].zName, p->token.z, p->token.n, 0);
drhd820cb12002-02-18 03:21:45 +0000760 }else if( p->op==TK_DOT && p->pRight && p->pRight->token.z &&
761 p->pRight->token.z[0] ){
762 sqliteSetNString(&pTab->aCol[i].zName,
763 p->pRight->token.z, p->pRight->token.n, 0);
drh22f70c32002-02-18 01:17:00 +0000764 }else{
765 char zBuf[30];
766 sprintf(zBuf, "column%d", i+1);
767 pTab->aCol[i].zName = sqliteStrDup(zBuf);
768 }
769 }
770 pTab->iPKey = -1;
771 return pTab;
772}
773
774/*
drhad2d8302002-05-24 20:31:36 +0000775** For the given SELECT statement, do three things.
drhd8bc7082000-06-07 23:51:50 +0000776**
drhad3cab52002-05-24 02:04:32 +0000777** (1) Fill in the pTabList->a[].pTab fields in the SrcList that
drh22f70c32002-02-18 01:17:00 +0000778** defines the set of tables that should be scanned.
drhd8bc7082000-06-07 23:51:50 +0000779**
drhad2d8302002-05-24 20:31:36 +0000780** (2) Add terms to the WHERE clause to accomodate the NATURAL keyword
781** on joins and the ON and USING clause of joins.
782**
783** (3) Scan the list of columns in the result set (pEList) looking
drh54473222002-04-04 02:10:55 +0000784** for instances of the "*" operator or the TABLE.* operator.
785** If found, expand each "*" to be every column in every table
786** and TABLE.* to be every column in TABLE.
drhd8bc7082000-06-07 23:51:50 +0000787**
788** Return 0 on success. If there are problems, leave an error message
789** in pParse and return non-zero.
790*/
791static int fillInColumnList(Parse *pParse, Select *p){
drh54473222002-04-04 02:10:55 +0000792 int i, j, k, rc;
drhad3cab52002-05-24 02:04:32 +0000793 SrcList *pTabList;
drhdaffd0e2001-04-11 14:28:42 +0000794 ExprList *pEList;
drha76b5df2002-02-23 02:32:10 +0000795 Table *pTab;
drhdaffd0e2001-04-11 14:28:42 +0000796
797 if( p==0 || p->pSrc==0 ) return 1;
798 pTabList = p->pSrc;
799 pEList = p->pEList;
drhd8bc7082000-06-07 23:51:50 +0000800
801 /* Look up every table in the table list.
802 */
drhad3cab52002-05-24 02:04:32 +0000803 for(i=0; i<pTabList->nSrc; i++){
drhd8bc7082000-06-07 23:51:50 +0000804 if( pTabList->a[i].pTab ){
805 /* This routine has run before! No need to continue */
806 return 0;
807 }
drhdaffd0e2001-04-11 14:28:42 +0000808 if( pTabList->a[i].zName==0 ){
drh22f70c32002-02-18 01:17:00 +0000809 /* A sub-query in the FROM clause of a SELECT */
drh22f70c32002-02-18 01:17:00 +0000810 assert( pTabList->a[i].pSelect!=0 );
drhad2d8302002-05-24 20:31:36 +0000811 if( pTabList->a[i].zAlias==0 ){
812 char zFakeName[60];
813 sprintf(zFakeName, "sqlite_subquery_%p_",
814 (void*)pTabList->a[i].pSelect);
815 sqliteSetString(&pTabList->a[i].zAlias, zFakeName, 0);
816 }
drh22f70c32002-02-18 01:17:00 +0000817 pTabList->a[i].pTab = pTab =
818 sqliteResultSetOfSelect(pParse, pTabList->a[i].zAlias,
819 pTabList->a[i].pSelect);
820 if( pTab==0 ){
821 return 1;
822 }
823 pTab->isTransient = 1;
824 }else{
drha76b5df2002-02-23 02:32:10 +0000825 /* An ordinary table or view name in the FROM clause */
826 pTabList->a[i].pTab = pTab =
827 sqliteFindTable(pParse->db, pTabList->a[i].zName);
828 if( pTab==0 ){
drh22f70c32002-02-18 01:17:00 +0000829 sqliteSetString(&pParse->zErrMsg, "no such table: ",
830 pTabList->a[i].zName, 0);
831 pParse->nErr++;
832 return 1;
833 }
drha76b5df2002-02-23 02:32:10 +0000834 if( pTab->pSelect ){
drh417be792002-03-03 18:59:40 +0000835 if( sqliteViewGetColumnNames(pParse, pTab) ){
836 return 1;
837 }
drhd94a6692002-08-25 18:29:11 +0000838 sqliteSelectDelete(pTabList->a[i].pSelect);
drhff78bd22002-02-27 01:47:11 +0000839 pTabList->a[i].pSelect = sqliteSelectDup(pTab->pSelect);
drha76b5df2002-02-23 02:32:10 +0000840 }
drhd8bc7082000-06-07 23:51:50 +0000841 }
842 }
843
drhad2d8302002-05-24 20:31:36 +0000844 /* Process NATURAL keywords, and ON and USING clauses of joins.
845 */
846 if( sqliteProcessJoin(pParse, p) ) return 1;
847
drh7c917d12001-12-16 20:05:05 +0000848 /* For every "*" that occurs in the column list, insert the names of
drh54473222002-04-04 02:10:55 +0000849 ** all columns in all tables. And for every TABLE.* insert the names
850 ** of all columns in TABLE. The parser inserted a special expression
drh7c917d12001-12-16 20:05:05 +0000851 ** with the TK_ALL operator for each "*" that it found in the column list.
852 ** The following code just has to locate the TK_ALL expressions and expand
853 ** each one to the list of all columns in all tables.
drh54473222002-04-04 02:10:55 +0000854 **
855 ** The first loop just checks to see if there are any "*" operators
856 ** that need expanding.
drhd8bc7082000-06-07 23:51:50 +0000857 */
drh7c917d12001-12-16 20:05:05 +0000858 for(k=0; k<pEList->nExpr; k++){
drh54473222002-04-04 02:10:55 +0000859 Expr *pE = pEList->a[k].pExpr;
860 if( pE->op==TK_ALL ) break;
861 if( pE->op==TK_DOT && pE->pRight && pE->pRight->op==TK_ALL
862 && pE->pLeft && pE->pLeft->op==TK_ID ) break;
drh7c917d12001-12-16 20:05:05 +0000863 }
drh54473222002-04-04 02:10:55 +0000864 rc = 0;
drh7c917d12001-12-16 20:05:05 +0000865 if( k<pEList->nExpr ){
drh54473222002-04-04 02:10:55 +0000866 /*
867 ** If we get here it means the result set contains one or more "*"
868 ** operators that need to be expanded. Loop through each expression
869 ** in the result set and expand them one by one.
870 */
drh7c917d12001-12-16 20:05:05 +0000871 struct ExprList_item *a = pEList->a;
872 ExprList *pNew = 0;
873 for(k=0; k<pEList->nExpr; k++){
drh54473222002-04-04 02:10:55 +0000874 Expr *pE = a[k].pExpr;
875 if( pE->op!=TK_ALL &&
876 (pE->op!=TK_DOT || pE->pRight==0 || pE->pRight->op!=TK_ALL) ){
877 /* This particular expression does not need to be expanded.
878 */
drh7c917d12001-12-16 20:05:05 +0000879 pNew = sqliteExprListAppend(pNew, a[k].pExpr, 0);
880 pNew->a[pNew->nExpr-1].zName = a[k].zName;
881 a[k].pExpr = 0;
882 a[k].zName = 0;
883 }else{
drh54473222002-04-04 02:10:55 +0000884 /* This expression is a "*" or a "TABLE.*" and needs to be
885 ** expanded. */
886 int tableSeen = 0; /* Set to 1 when TABLE matches */
887 Token *pName; /* text of name of TABLE */
888 if( pE->op==TK_DOT && pE->pLeft ){
889 pName = &pE->pLeft->token;
890 }else{
891 pName = 0;
892 }
drhad3cab52002-05-24 02:04:32 +0000893 for(i=0; i<pTabList->nSrc; i++){
drh7c917d12001-12-16 20:05:05 +0000894 Table *pTab = pTabList->a[i].pTab;
drh54473222002-04-04 02:10:55 +0000895 char *zTabName = pTabList->a[i].zAlias;
896 if( zTabName==0 || zTabName[0]==0 ){
897 zTabName = pTab->zName;
898 }
drhc754fa52002-05-27 03:25:51 +0000899 if( pName && (zTabName==0 || zTabName[0]==0 ||
900 sqliteStrNICmp(pName->z, zTabName, pName->n)!=0 ||
901 zTabName[pName->n]!=0) ){
drh54473222002-04-04 02:10:55 +0000902 continue;
903 }
904 tableSeen = 1;
drh7c917d12001-12-16 20:05:05 +0000905 for(j=0; j<pTab->nCol; j++){
drh22f70c32002-02-18 01:17:00 +0000906 Expr *pExpr, *pLeft, *pRight;
drhad2d8302002-05-24 20:31:36 +0000907 char *zName = pTab->aCol[j].zName;
908
909 if( i>0 && (pTabList->a[i-1].jointype & JT_NATURAL)!=0 &&
910 columnIndex(pTabList->a[i-1].pTab, zName)>=0 ){
911 /* In a NATURAL join, omit the join columns from the
912 ** table on the right */
913 continue;
914 }
915 if( i>0 && sqliteIdListIndex(pTabList->a[i-1].pUsing, zName)>=0 ){
916 /* In a join with a USING clause, omit columns in the
917 ** using clause from the table on the right. */
918 continue;
919 }
drh22f70c32002-02-18 01:17:00 +0000920 pRight = sqliteExpr(TK_ID, 0, 0, 0);
921 if( pRight==0 ) break;
drhad2d8302002-05-24 20:31:36 +0000922 pRight->token.z = zName;
923 pRight->token.n = strlen(zName);
drh4b59ab52002-08-24 18:24:51 +0000924 pRight->token.dyn = 0;
925 pRight->token.base = 1;
926 if( zTabName && pTabList->nSrc>1 ){
drh22f70c32002-02-18 01:17:00 +0000927 pLeft = sqliteExpr(TK_ID, 0, 0, 0);
drh22f70c32002-02-18 01:17:00 +0000928 pExpr = sqliteExpr(TK_DOT, pLeft, pRight, 0);
929 if( pExpr==0 ) break;
drh4b59ab52002-08-24 18:24:51 +0000930 pLeft->token.z = zTabName;
931 pLeft->token.n = strlen(zTabName);
932 pLeft->token.dyn = 0;
933 pLeft->token.base = 1;
934 sqliteSetString((char**)&pExpr->token.z, zTabName, ".", zName, 0);
935 pExpr->token.n = strlen(pExpr->token.z);
936 pExpr->token.base = 0;
937 pExpr->token.dyn = 1;
drh7c917d12001-12-16 20:05:05 +0000938 }else{
drh22f70c32002-02-18 01:17:00 +0000939 pExpr = pRight;
drh7c917d12001-12-16 20:05:05 +0000940 }
drh7c917d12001-12-16 20:05:05 +0000941 pNew = sqliteExprListAppend(pNew, pExpr, 0);
942 }
drh17e24df2001-11-06 14:10:41 +0000943 }
drh54473222002-04-04 02:10:55 +0000944 if( !tableSeen ){
drhf5db2d32002-06-06 23:42:27 +0000945 if( pName ){
946 sqliteSetNString(&pParse->zErrMsg, "no such table: ", -1,
947 pName->z, pName->n, 0);
948 }else{
949 sqliteSetString(&pParse->zErrMsg, "no tables specified", 0);
950 }
drh54473222002-04-04 02:10:55 +0000951 rc = 1;
952 }
drhd8bc7082000-06-07 23:51:50 +0000953 }
954 }
drh7c917d12001-12-16 20:05:05 +0000955 sqliteExprListDelete(pEList);
956 p->pEList = pNew;
drhd8bc7082000-06-07 23:51:50 +0000957 }
drh54473222002-04-04 02:10:55 +0000958 return rc;
drhd8bc7082000-06-07 23:51:50 +0000959}
960
961/*
drhff78bd22002-02-27 01:47:11 +0000962** This routine recursively unlinks the Select.pSrc.a[].pTab pointers
963** in a select structure. It just sets the pointers to NULL. This
964** routine is recursive in the sense that if the Select.pSrc.a[].pSelect
965** pointer is not NULL, this routine is called recursively on that pointer.
966**
967** This routine is called on the Select structure that defines a
968** VIEW in order to undo any bindings to tables. This is necessary
969** because those tables might be DROPed by a subsequent SQL command.
970*/
971void sqliteSelectUnbind(Select *p){
972 int i;
drhad3cab52002-05-24 02:04:32 +0000973 SrcList *pSrc = p->pSrc;
drhff78bd22002-02-27 01:47:11 +0000974 Table *pTab;
975 if( p==0 ) return;
drhad3cab52002-05-24 02:04:32 +0000976 for(i=0; i<pSrc->nSrc; i++){
drhff78bd22002-02-27 01:47:11 +0000977 if( (pTab = pSrc->a[i].pTab)!=0 ){
978 if( pTab->isTransient ){
979 sqliteDeleteTable(0, pTab);
drh4b59ab52002-08-24 18:24:51 +0000980#if 0
drhff78bd22002-02-27 01:47:11 +0000981 sqliteSelectDelete(pSrc->a[i].pSelect);
982 pSrc->a[i].pSelect = 0;
drh4b59ab52002-08-24 18:24:51 +0000983#endif
drhff78bd22002-02-27 01:47:11 +0000984 }
985 pSrc->a[i].pTab = 0;
986 if( pSrc->a[i].pSelect ){
987 sqliteSelectUnbind(pSrc->a[i].pSelect);
988 }
989 }
990 }
991}
992
993/*
drhd8bc7082000-06-07 23:51:50 +0000994** This routine associates entries in an ORDER BY expression list with
995** columns in a result. For each ORDER BY expression, the opcode of
drh967e8b72000-06-21 13:59:10 +0000996** the top-level node is changed to TK_COLUMN and the iColumn value of
drhd8bc7082000-06-07 23:51:50 +0000997** the top-level node is filled in with column number and the iTable
998** value of the top-level node is filled with iTable parameter.
999**
1000** If there are prior SELECT clauses, they are processed first. A match
1001** in an earlier SELECT takes precedence over a later SELECT.
1002**
1003** Any entry that does not match is flagged as an error. The number
1004** of errors is returned.
1005*/
1006static int matchOrderbyToColumn(
1007 Parse *pParse, /* A place to leave error messages */
1008 Select *pSelect, /* Match to result columns of this SELECT */
1009 ExprList *pOrderBy, /* The ORDER BY values to match against columns */
drhe4de1fe2002-06-02 16:09:01 +00001010 int iTable, /* Insert this value in iTable */
drhd8bc7082000-06-07 23:51:50 +00001011 int mustComplete /* If TRUE all ORDER BYs must match */
1012){
1013 int nErr = 0;
1014 int i, j;
1015 ExprList *pEList;
1016
drhdaffd0e2001-04-11 14:28:42 +00001017 if( pSelect==0 || pOrderBy==0 ) return 1;
drhd8bc7082000-06-07 23:51:50 +00001018 if( mustComplete ){
1019 for(i=0; i<pOrderBy->nExpr; i++){ pOrderBy->a[i].done = 0; }
1020 }
1021 if( fillInColumnList(pParse, pSelect) ){
1022 return 1;
1023 }
1024 if( pSelect->pPrior ){
drh92cd52f2000-06-08 01:55:29 +00001025 if( matchOrderbyToColumn(pParse, pSelect->pPrior, pOrderBy, iTable, 0) ){
1026 return 1;
1027 }
drhd8bc7082000-06-07 23:51:50 +00001028 }
1029 pEList = pSelect->pEList;
1030 for(i=0; i<pOrderBy->nExpr; i++){
1031 Expr *pE = pOrderBy->a[i].pExpr;
drhe4de1fe2002-06-02 16:09:01 +00001032 int iCol = -1;
drhd8bc7082000-06-07 23:51:50 +00001033 if( pOrderBy->a[i].done ) continue;
drhe4de1fe2002-06-02 16:09:01 +00001034 if( sqliteExprIsInteger(pE, &iCol) ){
1035 if( iCol<=0 || iCol>pEList->nExpr ){
1036 char zBuf[200];
1037 sprintf(zBuf,"ORDER BY position %d should be between 1 and %d",
1038 iCol, pEList->nExpr);
1039 sqliteSetString(&pParse->zErrMsg, zBuf, 0);
1040 pParse->nErr++;
1041 nErr++;
1042 break;
1043 }
1044 iCol--;
1045 }
1046 for(j=0; iCol<0 && j<pEList->nExpr; j++){
drh4cfa7932000-06-08 15:10:46 +00001047 if( pEList->a[j].zName && (pE->op==TK_ID || pE->op==TK_STRING) ){
drha76b5df2002-02-23 02:32:10 +00001048 char *zName, *zLabel;
1049 zName = pEList->a[j].zName;
1050 assert( pE->token.z );
1051 zLabel = sqliteStrNDup(pE->token.z, pE->token.n);
drhd8bc7082000-06-07 23:51:50 +00001052 sqliteDequote(zLabel);
1053 if( sqliteStrICmp(zName, zLabel)==0 ){
drhe4de1fe2002-06-02 16:09:01 +00001054 iCol = j;
drhd8bc7082000-06-07 23:51:50 +00001055 }
drh6e142f52000-06-08 13:36:40 +00001056 sqliteFree(zLabel);
drhd8bc7082000-06-07 23:51:50 +00001057 }
drhe4de1fe2002-06-02 16:09:01 +00001058 if( iCol<0 && sqliteExprCompare(pE, pEList->a[j].pExpr) ){
1059 iCol = j;
drhd8bc7082000-06-07 23:51:50 +00001060 }
1061 }
drhe4de1fe2002-06-02 16:09:01 +00001062 if( iCol>=0 ){
1063 pE->op = TK_COLUMN;
1064 pE->iColumn = iCol;
1065 pE->iTable = iTable;
1066 pOrderBy->a[i].done = 1;
1067 }
1068 if( iCol<0 && mustComplete ){
drhd8bc7082000-06-07 23:51:50 +00001069 char zBuf[30];
1070 sprintf(zBuf,"%d",i+1);
1071 sqliteSetString(&pParse->zErrMsg, "ORDER BY term number ", zBuf,
1072 " does not match any result column", 0);
1073 pParse->nErr++;
1074 nErr++;
1075 break;
1076 }
1077 }
1078 return nErr;
1079}
1080
1081/*
1082** Get a VDBE for the given parser context. Create a new one if necessary.
1083** If an error occurs, return NULL and leave a message in pParse.
1084*/
1085Vdbe *sqliteGetVdbe(Parse *pParse){
1086 Vdbe *v = pParse->pVdbe;
1087 if( v==0 ){
drh4c504392000-10-16 22:06:40 +00001088 v = pParse->pVdbe = sqliteVdbeCreate(pParse->db);
drhd8bc7082000-06-07 23:51:50 +00001089 }
drhd8bc7082000-06-07 23:51:50 +00001090 return v;
1091}
1092
1093
1094/*
drh82c3d632000-06-06 21:56:07 +00001095** This routine is called to process a query that is really the union
1096** or intersection of two or more separate queries.
drhc926afb2002-06-20 03:38:26 +00001097**
1098** "p" points to the right-most of the two queries. The results should
1099** be stored in eDest with parameter iParm.
drh82c3d632000-06-06 21:56:07 +00001100*/
1101static int multiSelect(Parse *pParse, Select *p, int eDest, int iParm){
drh10e5e3c2000-06-08 00:19:02 +00001102 int rc; /* Success code from a subroutine */
1103 Select *pPrior; /* Another SELECT immediately to our left */
1104 Vdbe *v; /* Generate code to this VDBE */
drh82c3d632000-06-06 21:56:07 +00001105
drhd8bc7082000-06-07 23:51:50 +00001106 /* Make sure there is no ORDER BY clause on prior SELECTs. Only the
1107 ** last SELECT in the series may have an ORDER BY.
drh82c3d632000-06-06 21:56:07 +00001108 */
drhdaffd0e2001-04-11 14:28:42 +00001109 if( p==0 || p->pPrior==0 ) return 1;
drhd8bc7082000-06-07 23:51:50 +00001110 pPrior = p->pPrior;
1111 if( pPrior->pOrderBy ){
1112 sqliteSetString(&pParse->zErrMsg,"ORDER BY clause should come after ",
1113 selectOpName(p->op), " not before", 0);
drh82c3d632000-06-06 21:56:07 +00001114 pParse->nErr++;
1115 return 1;
1116 }
1117
drhd8bc7082000-06-07 23:51:50 +00001118 /* Make sure we have a valid query engine. If not, create a new one.
1119 */
1120 v = sqliteGetVdbe(pParse);
1121 if( v==0 ) return 1;
1122
drh1cc3d752002-03-23 00:31:29 +00001123 /* Create the destination temporary table if necessary
1124 */
1125 if( eDest==SRT_TempTable ){
1126 sqliteVdbeAddOp(v, OP_OpenTemp, iParm, 0);
1127 eDest = SRT_Table;
1128 }
1129
drhf46f9052002-06-22 02:33:38 +00001130 /* Generate code for the left and right SELECT statements.
drhd8bc7082000-06-07 23:51:50 +00001131 */
drh82c3d632000-06-06 21:56:07 +00001132 switch( p->op ){
drhf46f9052002-06-22 02:33:38 +00001133 case TK_ALL: {
1134 if( p->pOrderBy==0 ){
1135 rc = sqliteSelect(pParse, pPrior, eDest, iParm, 0, 0, 0);
1136 if( rc ) return rc;
1137 p->pPrior = 0;
1138 rc = sqliteSelect(pParse, p, eDest, iParm, 0, 0, 0);
1139 p->pPrior = pPrior;
1140 if( rc ) return rc;
1141 break;
1142 }
1143 /* For UNION ALL ... ORDER BY fall through to the next case */
1144 }
drh82c3d632000-06-06 21:56:07 +00001145 case TK_EXCEPT:
1146 case TK_UNION: {
drhd8bc7082000-06-07 23:51:50 +00001147 int unionTab; /* Cursor number of the temporary table holding result */
1148 int op; /* One of the SRT_ operations to apply to self */
1149 int priorOp; /* The SRT_ operation to apply to prior selects */
drhc926afb2002-06-20 03:38:26 +00001150 ExprList *pOrderBy; /* The ORDER BY clause for the right SELECT */
drh82c3d632000-06-06 21:56:07 +00001151
drhd8bc7082000-06-07 23:51:50 +00001152 priorOp = p->op==TK_ALL ? SRT_Table : SRT_Union;
drhc926afb2002-06-20 03:38:26 +00001153 if( eDest==priorOp && p->pOrderBy==0 ){
drhd8bc7082000-06-07 23:51:50 +00001154 /* We can reuse a temporary table generated by a SELECT to our
drhc926afb2002-06-20 03:38:26 +00001155 ** right.
drhd8bc7082000-06-07 23:51:50 +00001156 */
drh82c3d632000-06-06 21:56:07 +00001157 unionTab = iParm;
1158 }else{
drhd8bc7082000-06-07 23:51:50 +00001159 /* We will need to create our own temporary table to hold the
1160 ** intermediate results.
1161 */
1162 unionTab = pParse->nTab++;
1163 if( p->pOrderBy
1164 && matchOrderbyToColumn(pParse, p, p->pOrderBy, unionTab, 1) ){
1165 return 1;
1166 }
drhd8bc7082000-06-07 23:51:50 +00001167 if( p->op!=TK_ALL ){
drhc6b52df2002-01-04 03:09:29 +00001168 sqliteVdbeAddOp(v, OP_OpenTemp, unionTab, 1);
drh99fcd712001-10-13 01:06:47 +00001169 sqliteVdbeAddOp(v, OP_KeyAsData, unionTab, 1);
drh345fda32001-01-15 22:51:08 +00001170 }else{
drh99fcd712001-10-13 01:06:47 +00001171 sqliteVdbeAddOp(v, OP_OpenTemp, unionTab, 0);
drhd8bc7082000-06-07 23:51:50 +00001172 }
drh82c3d632000-06-06 21:56:07 +00001173 }
drhd8bc7082000-06-07 23:51:50 +00001174
1175 /* Code the SELECT statements to our left
1176 */
drh832508b2002-03-02 17:04:07 +00001177 rc = sqliteSelect(pParse, pPrior, priorOp, unionTab, 0, 0, 0);
drh82c3d632000-06-06 21:56:07 +00001178 if( rc ) return rc;
drhd8bc7082000-06-07 23:51:50 +00001179
1180 /* Code the current SELECT statement
1181 */
1182 switch( p->op ){
1183 case TK_EXCEPT: op = SRT_Except; break;
1184 case TK_UNION: op = SRT_Union; break;
1185 case TK_ALL: op = SRT_Table; break;
1186 }
drh82c3d632000-06-06 21:56:07 +00001187 p->pPrior = 0;
drhc926afb2002-06-20 03:38:26 +00001188 pOrderBy = p->pOrderBy;
1189 p->pOrderBy = 0;
drh832508b2002-03-02 17:04:07 +00001190 rc = sqliteSelect(pParse, p, op, unionTab, 0, 0, 0);
drh82c3d632000-06-06 21:56:07 +00001191 p->pPrior = pPrior;
drhc926afb2002-06-20 03:38:26 +00001192 p->pOrderBy = pOrderBy;
drh82c3d632000-06-06 21:56:07 +00001193 if( rc ) return rc;
drhd8bc7082000-06-07 23:51:50 +00001194
1195 /* Convert the data in the temporary table into whatever form
1196 ** it is that we currently need.
1197 */
drhc926afb2002-06-20 03:38:26 +00001198 if( eDest!=priorOp || unionTab!=iParm ){
drh6b563442001-11-07 16:48:26 +00001199 int iCont, iBreak, iStart;
drh82c3d632000-06-06 21:56:07 +00001200 assert( p->pEList );
drh41202cc2002-04-23 17:10:18 +00001201 if( eDest==SRT_Callback ){
1202 generateColumnNames(pParse, p->base, 0, p->pEList);
1203 }
drh82c3d632000-06-06 21:56:07 +00001204 iBreak = sqliteVdbeMakeLabel(v);
drh6b563442001-11-07 16:48:26 +00001205 iCont = sqliteVdbeMakeLabel(v);
1206 sqliteVdbeAddOp(v, OP_Rewind, unionTab, iBreak);
1207 iStart = sqliteVdbeCurrentAddr(v);
drh38640e12002-07-05 21:42:36 +00001208 rc = selectInnerLoop(pParse, p, p->pEList, unionTab, p->pEList->nExpr,
drhd8bc7082000-06-07 23:51:50 +00001209 p->pOrderBy, -1, eDest, iParm,
drh82c3d632000-06-06 21:56:07 +00001210 iCont, iBreak);
1211 if( rc ) return 1;
drh6b563442001-11-07 16:48:26 +00001212 sqliteVdbeResolveLabel(v, iCont);
1213 sqliteVdbeAddOp(v, OP_Next, unionTab, iStart);
drh99fcd712001-10-13 01:06:47 +00001214 sqliteVdbeResolveLabel(v, iBreak);
1215 sqliteVdbeAddOp(v, OP_Close, unionTab, 0);
drhd8bc7082000-06-07 23:51:50 +00001216 if( p->pOrderBy ){
drhc926afb2002-06-20 03:38:26 +00001217 generateSortTail(p, v, p->pEList->nExpr, eDest, iParm);
drhd8bc7082000-06-07 23:51:50 +00001218 }
drh82c3d632000-06-06 21:56:07 +00001219 }
1220 break;
1221 }
1222 case TK_INTERSECT: {
1223 int tab1, tab2;
drh6b563442001-11-07 16:48:26 +00001224 int iCont, iBreak, iStart;
drh82c3d632000-06-06 21:56:07 +00001225
drhd8bc7082000-06-07 23:51:50 +00001226 /* INTERSECT is different from the others since it requires
drh6206d502000-06-19 19:09:08 +00001227 ** two temporary tables. Hence it has its own case. Begin
drhd8bc7082000-06-07 23:51:50 +00001228 ** by allocating the tables we will need.
1229 */
drh82c3d632000-06-06 21:56:07 +00001230 tab1 = pParse->nTab++;
1231 tab2 = pParse->nTab++;
drhd8bc7082000-06-07 23:51:50 +00001232 if( p->pOrderBy && matchOrderbyToColumn(pParse,p,p->pOrderBy,tab1,1) ){
1233 return 1;
1234 }
drhc6b52df2002-01-04 03:09:29 +00001235 sqliteVdbeAddOp(v, OP_OpenTemp, tab1, 1);
drh99fcd712001-10-13 01:06:47 +00001236 sqliteVdbeAddOp(v, OP_KeyAsData, tab1, 1);
drhd8bc7082000-06-07 23:51:50 +00001237
1238 /* Code the SELECTs to our left into temporary table "tab1".
1239 */
drh832508b2002-03-02 17:04:07 +00001240 rc = sqliteSelect(pParse, pPrior, SRT_Union, tab1, 0, 0, 0);
drh82c3d632000-06-06 21:56:07 +00001241 if( rc ) return rc;
drhd8bc7082000-06-07 23:51:50 +00001242
1243 /* Code the current SELECT into temporary table "tab2"
1244 */
drhc6b52df2002-01-04 03:09:29 +00001245 sqliteVdbeAddOp(v, OP_OpenTemp, tab2, 1);
drh99fcd712001-10-13 01:06:47 +00001246 sqliteVdbeAddOp(v, OP_KeyAsData, tab2, 1);
drh82c3d632000-06-06 21:56:07 +00001247 p->pPrior = 0;
drh832508b2002-03-02 17:04:07 +00001248 rc = sqliteSelect(pParse, p, SRT_Union, tab2, 0, 0, 0);
drh82c3d632000-06-06 21:56:07 +00001249 p->pPrior = pPrior;
1250 if( rc ) return rc;
drhd8bc7082000-06-07 23:51:50 +00001251
1252 /* Generate code to take the intersection of the two temporary
1253 ** tables.
1254 */
drh82c3d632000-06-06 21:56:07 +00001255 assert( p->pEList );
drh41202cc2002-04-23 17:10:18 +00001256 if( eDest==SRT_Callback ){
1257 generateColumnNames(pParse, p->base, 0, p->pEList);
1258 }
drh82c3d632000-06-06 21:56:07 +00001259 iBreak = sqliteVdbeMakeLabel(v);
drh6b563442001-11-07 16:48:26 +00001260 iCont = sqliteVdbeMakeLabel(v);
1261 sqliteVdbeAddOp(v, OP_Rewind, tab1, iBreak);
1262 iStart = sqliteVdbeAddOp(v, OP_FullKey, tab1, 0);
drh99fcd712001-10-13 01:06:47 +00001263 sqliteVdbeAddOp(v, OP_NotFound, tab2, iCont);
drh38640e12002-07-05 21:42:36 +00001264 rc = selectInnerLoop(pParse, p, p->pEList, tab1, p->pEList->nExpr,
drhd8bc7082000-06-07 23:51:50 +00001265 p->pOrderBy, -1, eDest, iParm,
drh82c3d632000-06-06 21:56:07 +00001266 iCont, iBreak);
1267 if( rc ) return 1;
drh6b563442001-11-07 16:48:26 +00001268 sqliteVdbeResolveLabel(v, iCont);
1269 sqliteVdbeAddOp(v, OP_Next, tab1, iStart);
drh99fcd712001-10-13 01:06:47 +00001270 sqliteVdbeResolveLabel(v, iBreak);
1271 sqliteVdbeAddOp(v, OP_Close, tab2, 0);
1272 sqliteVdbeAddOp(v, OP_Close, tab1, 0);
drhd8bc7082000-06-07 23:51:50 +00001273 if( p->pOrderBy ){
drhc926afb2002-06-20 03:38:26 +00001274 generateSortTail(p, v, p->pEList->nExpr, eDest, iParm);
drhd8bc7082000-06-07 23:51:50 +00001275 }
drh82c3d632000-06-06 21:56:07 +00001276 break;
1277 }
1278 }
1279 assert( p->pEList && pPrior->pEList );
1280 if( p->pEList->nExpr!=pPrior->pEList->nExpr ){
drhd8bc7082000-06-07 23:51:50 +00001281 sqliteSetString(&pParse->zErrMsg, "SELECTs to the left and right of ",
1282 selectOpName(p->op), " do not have the same number of result columns", 0);
drh82c3d632000-06-06 21:56:07 +00001283 pParse->nErr++;
1284 return 1;
drh22827922000-06-06 17:27:05 +00001285 }
1286 return 0;
1287}
1288
1289/*
drh832508b2002-03-02 17:04:07 +00001290** Recursively scan through an expression tree. For every reference
1291** to a column in table number iFrom, change that reference to the
1292** same column in table number iTo.
1293*/
drh315555c2002-10-20 15:53:03 +00001294static void changeTablesInList(ExprList*, int, int); /* Forward Declaration */
drh832508b2002-03-02 17:04:07 +00001295static void changeTables(Expr *pExpr, int iFrom, int iTo){
1296 if( pExpr==0 ) return;
1297 if( pExpr->op==TK_COLUMN && pExpr->iTable==iFrom ){
1298 pExpr->iTable = iTo;
1299 }else{
1300 changeTables(pExpr->pLeft, iFrom, iTo);
1301 changeTables(pExpr->pRight, iFrom, iTo);
drh1b2e0322002-03-03 02:49:51 +00001302 changeTablesInList(pExpr->pList, iFrom, iTo);
1303 }
1304}
1305static void changeTablesInList(ExprList *pList, int iFrom, int iTo){
1306 if( pList ){
1307 int i;
1308 for(i=0; i<pList->nExpr; i++){
1309 changeTables(pList->a[i].pExpr, iFrom, iTo);
drh832508b2002-03-02 17:04:07 +00001310 }
1311 }
1312}
1313
1314/*
1315** Scan through the expression pExpr. Replace every reference to
1316** a column in table number iTable with a copy of the corresponding
drh84e59202002-03-14 14:33:31 +00001317** entry in pEList. (But leave references to the ROWID column
1318** unchanged.) When making a copy of an expression in pEList, change
1319** references to columns in table iSub into references to table iTable.
drh832508b2002-03-02 17:04:07 +00001320**
1321** This routine is part of the flattening procedure. A subquery
1322** whose result set is defined by pEList appears as entry in the
1323** FROM clause of a SELECT such that the VDBE cursor assigned to that
1324** FORM clause entry is iTable. This routine make the necessary
1325** changes to pExpr so that it refers directly to the source table
1326** of the subquery rather the result set of the subquery.
1327*/
drh315555c2002-10-20 15:53:03 +00001328static void substExprList(ExprList*,int,ExprList*,int); /* Forward Decl */
drh832508b2002-03-02 17:04:07 +00001329static void substExpr(Expr *pExpr, int iTable, ExprList *pEList, int iSub){
1330 if( pExpr==0 ) return;
drh84e59202002-03-14 14:33:31 +00001331 if( pExpr->op==TK_COLUMN && pExpr->iTable==iTable && pExpr->iColumn>=0 ){
drh832508b2002-03-02 17:04:07 +00001332 Expr *pNew;
drh84e59202002-03-14 14:33:31 +00001333 assert( pEList!=0 && pExpr->iColumn<pEList->nExpr );
drh832508b2002-03-02 17:04:07 +00001334 assert( pExpr->pLeft==0 && pExpr->pRight==0 && pExpr->pList==0 );
1335 pNew = pEList->a[pExpr->iColumn].pExpr;
1336 assert( pNew!=0 );
1337 pExpr->op = pNew->op;
drhd94a6692002-08-25 18:29:11 +00001338 assert( pExpr->pLeft==0 );
drh832508b2002-03-02 17:04:07 +00001339 pExpr->pLeft = sqliteExprDup(pNew->pLeft);
drhd94a6692002-08-25 18:29:11 +00001340 assert( pExpr->pRight==0 );
drh832508b2002-03-02 17:04:07 +00001341 pExpr->pRight = sqliteExprDup(pNew->pRight);
drhd94a6692002-08-25 18:29:11 +00001342 assert( pExpr->pList==0 );
drh832508b2002-03-02 17:04:07 +00001343 pExpr->pList = sqliteExprListDup(pNew->pList);
1344 pExpr->iTable = pNew->iTable;
1345 pExpr->iColumn = pNew->iColumn;
1346 pExpr->iAgg = pNew->iAgg;
drh4b59ab52002-08-24 18:24:51 +00001347 pExpr->nFuncName = pNew->nFuncName;
1348 sqliteTokenCopy(&pExpr->token, &pNew->token);
drh832508b2002-03-02 17:04:07 +00001349 if( iSub!=iTable ){
1350 changeTables(pExpr, iSub, iTable);
1351 }
1352 }else{
drh832508b2002-03-02 17:04:07 +00001353 substExpr(pExpr->pLeft, iTable, pEList, iSub);
1354 substExpr(pExpr->pRight, iTable, pEList, iSub);
1355 substExprList(pExpr->pList, iTable, pEList, iSub);
1356 }
1357}
1358static void
1359substExprList(ExprList *pList, int iTable, ExprList *pEList, int iSub){
1360 int i;
1361 if( pList==0 ) return;
1362 for(i=0; i<pList->nExpr; i++){
1363 substExpr(pList->a[i].pExpr, iTable, pEList, iSub);
1364 }
1365}
1366
1367/*
drh1350b032002-02-27 19:00:20 +00001368** This routine attempts to flatten subqueries in order to speed
1369** execution. It returns 1 if it makes changes and 0 if no flattening
1370** occurs.
1371**
1372** To understand the concept of flattening, consider the following
1373** query:
1374**
1375** SELECT a FROM (SELECT x+y AS a FROM t1 WHERE z<100) WHERE a>5
1376**
1377** The default way of implementing this query is to execute the
1378** subquery first and store the results in a temporary table, then
1379** run the outer query on that temporary table. This requires two
1380** passes over the data. Furthermore, because the temporary table
1381** has no indices, the WHERE clause on the outer query cannot be
drh832508b2002-03-02 17:04:07 +00001382** optimized.
drh1350b032002-02-27 19:00:20 +00001383**
drh832508b2002-03-02 17:04:07 +00001384** This routine attempts to rewrite queries such as the above into
drh1350b032002-02-27 19:00:20 +00001385** a single flat select, like this:
1386**
1387** SELECT x+y AS a FROM t1 WHERE z<100 AND a>5
1388**
1389** The code generated for this simpification gives the same result
drh832508b2002-03-02 17:04:07 +00001390** but only has to scan the data once. And because indices might
1391** exist on the table t1, a complete scan of the data might be
1392** avoided.
drh1350b032002-02-27 19:00:20 +00001393**
drh832508b2002-03-02 17:04:07 +00001394** Flattening is only attempted if all of the following are true:
drh1350b032002-02-27 19:00:20 +00001395**
drh832508b2002-03-02 17:04:07 +00001396** (1) The subquery and the outer query do not both use aggregates.
drh1350b032002-02-27 19:00:20 +00001397**
drh832508b2002-03-02 17:04:07 +00001398** (2) The subquery is not an aggregate or the outer query is not a join.
1399**
1400** (3) The subquery is not a join.
1401**
1402** (4) The subquery is not DISTINCT or the outer query is not a join.
1403**
1404** (5) The subquery is not DISTINCT or the outer query does not use
1405** aggregates.
1406**
1407** (6) The subquery does not use aggregates or the outer query is not
1408** DISTINCT.
1409**
drh08192d52002-04-30 19:20:28 +00001410** (7) The subquery has a FROM clause.
1411**
drhdf199a22002-06-14 22:38:41 +00001412** (8) The subquery does not use LIMIT or the outer query is not a join.
1413**
1414** (9) The subquery does not use LIMIT or the outer query does not use
1415** aggregates.
1416**
1417** (10) The subquery does not use aggregates or the outer query does not
1418** use LIMIT.
1419**
drh832508b2002-03-02 17:04:07 +00001420** In this routine, the "p" parameter is a pointer to the outer query.
1421** The subquery is p->pSrc->a[iFrom]. isAgg is true if the outer query
1422** uses aggregates and subqueryIsAgg is true if the subquery uses aggregates.
1423**
1424** If flattening is not attempted, this routine is a no-op and return 0.
1425** If flattening is attempted this routine returns 1.
1426**
1427** All of the expression analysis must occur on both the outer query and
1428** the subquery before this routine runs.
drh1350b032002-02-27 19:00:20 +00001429*/
drh8c74a8c2002-08-25 19:20:40 +00001430static int flattenSubquery(
1431 Parse *pParse, /* The parsing context */
1432 Select *p, /* The parent or outer SELECT statement */
1433 int iFrom, /* Index in p->pSrc->a[] of the inner subquery */
1434 int isAgg, /* True if outer SELECT uses aggregate functions */
1435 int subqueryIsAgg /* True if the subquery uses aggregate functions */
1436){
drh0bb28102002-05-08 11:54:14 +00001437 Select *pSub; /* The inner query or "subquery" */
drhad3cab52002-05-24 02:04:32 +00001438 SrcList *pSrc; /* The FROM clause of the outer query */
1439 SrcList *pSubSrc; /* The FROM clause of the subquery */
drh0bb28102002-05-08 11:54:14 +00001440 ExprList *pList; /* The result set of the outer query */
drh832508b2002-03-02 17:04:07 +00001441 int i;
1442 int iParent, iSub;
1443 Expr *pWhere;
drh1350b032002-02-27 19:00:20 +00001444
drh832508b2002-03-02 17:04:07 +00001445 /* Check to see if flattening is permitted. Return 0 if not.
1446 */
1447 if( p==0 ) return 0;
1448 pSrc = p->pSrc;
drhad3cab52002-05-24 02:04:32 +00001449 assert( pSrc && iFrom>=0 && iFrom<pSrc->nSrc );
drh832508b2002-03-02 17:04:07 +00001450 pSub = pSrc->a[iFrom].pSelect;
1451 assert( pSub!=0 );
1452 if( isAgg && subqueryIsAgg ) return 0;
drhad3cab52002-05-24 02:04:32 +00001453 if( subqueryIsAgg && pSrc->nSrc>1 ) return 0;
drh832508b2002-03-02 17:04:07 +00001454 pSubSrc = pSub->pSrc;
1455 assert( pSubSrc );
drhad3cab52002-05-24 02:04:32 +00001456 if( pSubSrc->nSrc!=1 ) return 0;
drhdf199a22002-06-14 22:38:41 +00001457 if( (pSub->isDistinct || pSub->nLimit>=0) && (pSrc->nSrc>1 || isAgg) ){
1458 return 0;
1459 }
drhd11d3822002-06-21 23:01:49 +00001460 if( (p->isDistinct || p->nLimit>=0) && subqueryIsAgg ) return 0;
drh832508b2002-03-02 17:04:07 +00001461
drh0bb28102002-05-08 11:54:14 +00001462 /* If we reach this point, it means flattening is permitted for the
drh832508b2002-03-02 17:04:07 +00001463 ** i-th entry of the FROM clause in the outer query.
1464 */
1465 iParent = p->base + iFrom;
1466 iSub = pSub->base;
1467 substExprList(p->pEList, iParent, pSub->pEList, iSub);
1468 pList = p->pEList;
1469 for(i=0; i<pList->nExpr; i++){
1470 if( pList->a[i].zName==0 ){
1471 Expr *pExpr = pList->a[i].pExpr;
drh4b59ab52002-08-24 18:24:51 +00001472 assert( pExpr->token.z!=0 );
1473 pList->a[i].zName = sqliteStrNDup(pExpr->token.z, pExpr->token.n);
drh832508b2002-03-02 17:04:07 +00001474 }
1475 }
drh1b2e0322002-03-03 02:49:51 +00001476 if( isAgg ){
1477 substExprList(p->pGroupBy, iParent, pSub->pEList, iSub);
1478 substExpr(p->pHaving, iParent, pSub->pEList, iSub);
1479 }
drh832508b2002-03-02 17:04:07 +00001480 substExprList(p->pOrderBy, iParent, pSub->pEList, iSub);
1481 if( pSub->pWhere ){
1482 pWhere = sqliteExprDup(pSub->pWhere);
1483 if( iParent!=iSub ){
1484 changeTables(pWhere, iSub, iParent);
1485 }
1486 }else{
1487 pWhere = 0;
1488 }
1489 if( subqueryIsAgg ){
1490 assert( p->pHaving==0 );
drh1b2e0322002-03-03 02:49:51 +00001491 p->pHaving = p->pWhere;
1492 p->pWhere = pWhere;
drh832508b2002-03-02 17:04:07 +00001493 substExpr(p->pHaving, iParent, pSub->pEList, iSub);
drh1b2e0322002-03-03 02:49:51 +00001494 if( pSub->pHaving ){
1495 Expr *pHaving = sqliteExprDup(pSub->pHaving);
1496 if( iParent!=iSub ){
1497 changeTables(pHaving, iSub, iParent);
1498 }
1499 if( p->pHaving ){
1500 p->pHaving = sqliteExpr(TK_AND, p->pHaving, pHaving, 0);
1501 }else{
1502 p->pHaving = pHaving;
1503 }
1504 }
1505 assert( p->pGroupBy==0 );
1506 p->pGroupBy = sqliteExprListDup(pSub->pGroupBy);
1507 if( iParent!=iSub ){
1508 changeTablesInList(p->pGroupBy, iSub, iParent);
1509 }
drh832508b2002-03-02 17:04:07 +00001510 }else if( p->pWhere==0 ){
1511 p->pWhere = pWhere;
1512 }else{
1513 substExpr(p->pWhere, iParent, pSub->pEList, iSub);
1514 if( pWhere ){
1515 p->pWhere = sqliteExpr(TK_AND, p->pWhere, pWhere, 0);
1516 }
1517 }
1518 p->isDistinct = p->isDistinct || pSub->isDistinct;
drh8c74a8c2002-08-25 19:20:40 +00001519
drhdf199a22002-06-14 22:38:41 +00001520 if( pSub->nLimit>=0 ){
1521 if( p->nLimit<0 ){
1522 p->nLimit = pSub->nLimit;
1523 }else if( p->nLimit+p->nOffset > pSub->nLimit+pSub->nOffset ){
1524 p->nLimit = pSub->nLimit + pSub->nOffset - p->nOffset;
1525 }
1526 }
1527 p->nOffset += pSub->nOffset;
drh8c74a8c2002-08-25 19:20:40 +00001528
1529 /* If the subquery contains subqueries of its own, that were not
1530 ** flattened, then code will have already been generated to put
1531 ** the results of those sub-subqueries into VDBE cursors relative
1532 ** to the subquery. We must translate the cursor number into values
1533 ** suitable for use by the outer query.
1534 */
1535 for(i=0; i<pSubSrc->nSrc; i++){
1536 Vdbe *v;
1537 if( pSubSrc->a[i].pSelect==0 ) continue;
1538 v = sqliteGetVdbe(pParse);
1539 sqliteVdbeAddOp(v, OP_RenameCursor, pSub->base+i, p->base+i);
1540 }
1541
drh832508b2002-03-02 17:04:07 +00001542 if( pSrc->a[iFrom].pTab && pSrc->a[iFrom].pTab->isTransient ){
1543 sqliteDeleteTable(0, pSrc->a[iFrom].pTab);
1544 }
1545 pSrc->a[iFrom].pTab = pSubSrc->a[0].pTab;
1546 pSubSrc->a[0].pTab = 0;
drhd94a6692002-08-25 18:29:11 +00001547 assert( pSrc->a[iFrom].pSelect==pSub );
drh832508b2002-03-02 17:04:07 +00001548 pSrc->a[iFrom].pSelect = pSubSrc->a[0].pSelect;
1549 pSubSrc->a[0].pSelect = 0;
1550 sqliteSelectDelete(pSub);
1551 return 1;
1552}
drh1350b032002-02-27 19:00:20 +00001553
1554/*
drh9562b552002-02-19 15:00:07 +00001555** Analyze the SELECT statement passed in as an argument to see if it
1556** is a simple min() or max() query. If it is and this query can be
1557** satisfied using a single seek to the beginning or end of an index,
1558** then generate the code for this SELECT return 1. If this is not a
1559** simple min() or max() query, then return 0;
1560**
1561** A simply min() or max() query looks like this:
1562**
1563** SELECT min(a) FROM table;
1564** SELECT max(a) FROM table;
1565**
1566** The query may have only a single table in its FROM argument. There
1567** can be no GROUP BY or HAVING or WHERE clauses. The result set must
1568** be the min() or max() of a single column of the table. The column
1569** in the min() or max() function must be indexed.
1570**
1571** The parameters to this routine are the same as for sqliteSelect().
1572** See the header comment on that routine for additional information.
1573*/
1574static int simpleMinMaxQuery(Parse *pParse, Select *p, int eDest, int iParm){
1575 Expr *pExpr;
1576 int iCol;
1577 Table *pTab;
1578 Index *pIdx;
1579 int base;
1580 Vdbe *v;
1581 int openOp;
1582 int seekOp;
1583 int cont;
1584 ExprList eList;
1585 struct ExprList_item eListItem;
1586
1587 /* Check to see if this query is a simple min() or max() query. Return
1588 ** zero if it is not.
1589 */
1590 if( p->pGroupBy || p->pHaving || p->pWhere ) return 0;
drhad3cab52002-05-24 02:04:32 +00001591 if( p->pSrc->nSrc!=1 ) return 0;
drh9562b552002-02-19 15:00:07 +00001592 if( p->pEList->nExpr!=1 ) return 0;
1593 pExpr = p->pEList->a[0].pExpr;
1594 if( pExpr->op!=TK_AGG_FUNCTION ) return 0;
1595 if( pExpr->pList==0 || pExpr->pList->nExpr!=1 ) return 0;
drh4b59ab52002-08-24 18:24:51 +00001596 if( pExpr->nFuncName!=3 ) return 0;
drh0bce8352002-02-28 00:41:10 +00001597 if( sqliteStrNICmp(pExpr->token.z,"min",3)==0 ){
1598 seekOp = OP_Rewind;
1599 }else if( sqliteStrNICmp(pExpr->token.z,"max",3)==0 ){
1600 seekOp = OP_Last;
1601 }else{
1602 return 0;
1603 }
drh9562b552002-02-19 15:00:07 +00001604 pExpr = pExpr->pList->a[0].pExpr;
1605 if( pExpr->op!=TK_COLUMN ) return 0;
1606 iCol = pExpr->iColumn;
1607 pTab = p->pSrc->a[0].pTab;
1608
1609 /* If we get to here, it means the query is of the correct form.
drh17f71932002-02-21 12:01:27 +00001610 ** Check to make sure we have an index and make pIdx point to the
1611 ** appropriate index. If the min() or max() is on an INTEGER PRIMARY
1612 ** key column, no index is necessary so set pIdx to NULL. If no
1613 ** usable index is found, return 0.
drh9562b552002-02-19 15:00:07 +00001614 */
1615 if( iCol<0 ){
1616 pIdx = 0;
1617 }else{
1618 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
1619 assert( pIdx->nColumn>=1 );
1620 if( pIdx->aiColumn[0]==iCol ) break;
1621 }
1622 if( pIdx==0 ) return 0;
1623 }
1624
drh17f71932002-02-21 12:01:27 +00001625 /* Identify column names if we will be using the callback. This
drh9562b552002-02-19 15:00:07 +00001626 ** step is skipped if the output is going to a table or a memory cell.
1627 */
1628 v = sqliteGetVdbe(pParse);
1629 if( v==0 ) return 0;
1630 if( eDest==SRT_Callback ){
drh832508b2002-03-02 17:04:07 +00001631 generateColumnNames(pParse, p->base, p->pSrc, p->pEList);
drh9562b552002-02-19 15:00:07 +00001632 }
1633
drh17f71932002-02-21 12:01:27 +00001634 /* Generating code to find the min or the max. Basically all we have
1635 ** to do is find the first or the last entry in the chosen index. If
1636 ** the min() or max() is on the INTEGER PRIMARY KEY, then find the first
1637 ** or last entry in the main table.
drh9562b552002-02-19 15:00:07 +00001638 */
drh5cf8e8c2002-02-19 22:42:05 +00001639 if( !pParse->schemaVerified && (pParse->db->flags & SQLITE_InTrans)==0 ){
1640 sqliteVdbeAddOp(v, OP_VerifyCookie, pParse->db->schema_cookie, 0);
1641 pParse->schemaVerified = 1;
1642 }
drh9562b552002-02-19 15:00:07 +00001643 openOp = pTab->isTemp ? OP_OpenAux : OP_Open;
drh832508b2002-03-02 17:04:07 +00001644 base = p->base;
drh9562b552002-02-19 15:00:07 +00001645 sqliteVdbeAddOp(v, openOp, base, pTab->tnum);
drh5cf8e8c2002-02-19 22:42:05 +00001646 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
drh9562b552002-02-19 15:00:07 +00001647 if( pIdx==0 ){
1648 sqliteVdbeAddOp(v, seekOp, base, 0);
1649 }else{
1650 sqliteVdbeAddOp(v, openOp, base+1, pIdx->tnum);
drh5cf8e8c2002-02-19 22:42:05 +00001651 sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC);
drh9562b552002-02-19 15:00:07 +00001652 sqliteVdbeAddOp(v, seekOp, base+1, 0);
1653 sqliteVdbeAddOp(v, OP_IdxRecno, base+1, 0);
1654 sqliteVdbeAddOp(v, OP_Close, base+1, 0);
1655 sqliteVdbeAddOp(v, OP_MoveTo, base, 0);
1656 }
drh5cf8e8c2002-02-19 22:42:05 +00001657 eList.nExpr = 1;
1658 memset(&eListItem, 0, sizeof(eListItem));
1659 eList.a = &eListItem;
1660 eList.a[0].pExpr = pExpr;
drh9562b552002-02-19 15:00:07 +00001661 cont = sqliteVdbeMakeLabel(v);
drh38640e12002-07-05 21:42:36 +00001662 selectInnerLoop(pParse, p, &eList, 0, 0, 0, -1, eDest, iParm, cont, cont);
drh9562b552002-02-19 15:00:07 +00001663 sqliteVdbeResolveLabel(v, cont);
1664 sqliteVdbeAddOp(v, OP_Close, base, 0);
1665 return 1;
1666}
1667
1668/*
drh9bb61fe2000-06-05 16:01:39 +00001669** Generate code for the given SELECT statement.
1670**
drhfef52082000-06-06 01:50:43 +00001671** The results are distributed in various ways depending on the
1672** value of eDest and iParm.
1673**
1674** eDest Value Result
1675** ------------ -------------------------------------------
1676** SRT_Callback Invoke the callback for each row of the result.
1677**
1678** SRT_Mem Store first result in memory cell iParm
1679**
1680** SRT_Set Store results as keys of a table with cursor iParm
1681**
drh82c3d632000-06-06 21:56:07 +00001682** SRT_Union Store results as a key in a temporary table iParm
1683**
drhc4a3c772001-04-04 11:48:57 +00001684** SRT_Except Remove results form the temporary table iParm.
1685**
1686** SRT_Table Store results in temporary table iParm
drh9bb61fe2000-06-05 16:01:39 +00001687**
1688** This routine returns the number of errors. If any errors are
1689** encountered, then an appropriate error message is left in
1690** pParse->zErrMsg.
1691**
1692** This routine does NOT free the Select structure passed in. The
1693** calling function needs to do that.
drh1b2e0322002-03-03 02:49:51 +00001694**
1695** The pParent, parentTab, and *pParentAgg fields are filled in if this
1696** SELECT is a subquery. This routine may try to combine this SELECT
1697** with its parent to form a single flat query. In so doing, it might
1698** change the parent query from a non-aggregate to an aggregate query.
1699** For that reason, the pParentAgg flag is passed as a pointer, so it
1700** can be changed.
drh9bb61fe2000-06-05 16:01:39 +00001701*/
1702int sqliteSelect(
drhcce7d172000-05-31 15:34:51 +00001703 Parse *pParse, /* The parser context */
drh9bb61fe2000-06-05 16:01:39 +00001704 Select *p, /* The SELECT statement being coded. */
drh82c3d632000-06-06 21:56:07 +00001705 int eDest, /* One of: SRT_Callback Mem Set Union Except */
drh832508b2002-03-02 17:04:07 +00001706 int iParm, /* Save result in this memory location, if >=0 */
1707 Select *pParent, /* Another SELECT for which this is a sub-query */
1708 int parentTab, /* Index in pParent->pSrc of this query */
drh1b2e0322002-03-03 02:49:51 +00001709 int *pParentAgg /* True if pParent uses aggregate functions */
drhcce7d172000-05-31 15:34:51 +00001710){
drhd8bc7082000-06-07 23:51:50 +00001711 int i;
drhcce7d172000-05-31 15:34:51 +00001712 WhereInfo *pWInfo;
1713 Vdbe *v;
1714 int isAgg = 0; /* True for select lists like "count(*)" */
drha2e00042002-01-22 03:13:42 +00001715 ExprList *pEList; /* List of columns to extract. */
drhad3cab52002-05-24 02:04:32 +00001716 SrcList *pTabList; /* List of tables to select from */
drh9bb61fe2000-06-05 16:01:39 +00001717 Expr *pWhere; /* The WHERE clause. May be NULL */
1718 ExprList *pOrderBy; /* The ORDER BY clause. May be NULL */
drh22827922000-06-06 17:27:05 +00001719 ExprList *pGroupBy; /* The GROUP BY clause. May be NULL */
1720 Expr *pHaving; /* The HAVING clause. May be NULL */
drh19a775c2000-06-05 18:54:46 +00001721 int isDistinct; /* True if the DISTINCT keyword is present */
1722 int distinct; /* Table to use for the distinct set */
drh10e5e3c2000-06-08 00:19:02 +00001723 int base; /* First cursor available for use */
drh1d83f052002-02-17 00:30:36 +00001724 int rc = 1; /* Value to return from this function */
drh9bb61fe2000-06-05 16:01:39 +00001725
drhdaffd0e2001-04-11 14:28:42 +00001726 if( sqlite_malloc_failed || pParse->nErr || p==0 ) return 1;
1727
drh82c3d632000-06-06 21:56:07 +00001728 /* If there is are a sequence of queries, do the earlier ones first.
1729 */
1730 if( p->pPrior ){
1731 return multiSelect(pParse, p, eDest, iParm);
1732 }
1733
1734 /* Make local copies of the parameters for this query.
1735 */
drh9bb61fe2000-06-05 16:01:39 +00001736 pTabList = p->pSrc;
1737 pWhere = p->pWhere;
1738 pOrderBy = p->pOrderBy;
drh22827922000-06-06 17:27:05 +00001739 pGroupBy = p->pGroupBy;
1740 pHaving = p->pHaving;
drh19a775c2000-06-05 18:54:46 +00001741 isDistinct = p->isDistinct;
drh9bb61fe2000-06-05 16:01:39 +00001742
drh832508b2002-03-02 17:04:07 +00001743 /* Allocate a block of VDBE cursors, one for each table in the FROM clause.
1744 ** The WHERE processing requires that the cursors for the tables in the
1745 ** FROM clause be consecutive.
drh10e5e3c2000-06-08 00:19:02 +00001746 */
drh832508b2002-03-02 17:04:07 +00001747 base = p->base = pParse->nTab;
drhad3cab52002-05-24 02:04:32 +00001748 pParse->nTab += pTabList->nSrc;
drh10e5e3c2000-06-08 00:19:02 +00001749
drh9bb61fe2000-06-05 16:01:39 +00001750 /*
1751 ** Do not even attempt to generate any code if we have already seen
1752 ** errors before this routine starts.
1753 */
drh1d83f052002-02-17 00:30:36 +00001754 if( pParse->nErr>0 ) goto select_end;
drhcce7d172000-05-31 15:34:51 +00001755
drhd8bc7082000-06-07 23:51:50 +00001756 /* Look up every table in the table list and create an appropriate
1757 ** columnlist in pEList if there isn't one already. (The parser leaves
drh967e8b72000-06-21 13:59:10 +00001758 ** a NULL in the p->pEList if the SQL said "SELECT * FROM ...")
drhcce7d172000-05-31 15:34:51 +00001759 */
drhd8bc7082000-06-07 23:51:50 +00001760 if( fillInColumnList(pParse, p) ){
drh1d83f052002-02-17 00:30:36 +00001761 goto select_end;
drhcce7d172000-05-31 15:34:51 +00001762 }
drhad2d8302002-05-24 20:31:36 +00001763 pWhere = p->pWhere;
drhd8bc7082000-06-07 23:51:50 +00001764 pEList = p->pEList;
drh1d83f052002-02-17 00:30:36 +00001765 if( pEList==0 ) goto select_end;
drhcce7d172000-05-31 15:34:51 +00001766
drh22827922000-06-06 17:27:05 +00001767 /* If writing to memory or generating a set
1768 ** only a single column may be output.
drh19a775c2000-06-05 18:54:46 +00001769 */
drhfef52082000-06-06 01:50:43 +00001770 if( (eDest==SRT_Mem || eDest==SRT_Set) && pEList->nExpr>1 ){
drh19a775c2000-06-05 18:54:46 +00001771 sqliteSetString(&pParse->zErrMsg, "only a single result allowed for "
1772 "a SELECT that is part of an expression", 0);
1773 pParse->nErr++;
drh1d83f052002-02-17 00:30:36 +00001774 goto select_end;
drh19a775c2000-06-05 18:54:46 +00001775 }
1776
drhc926afb2002-06-20 03:38:26 +00001777 /* ORDER BY is ignored for some destinations.
drh22827922000-06-06 17:27:05 +00001778 */
drhc926afb2002-06-20 03:38:26 +00001779 switch( eDest ){
1780 case SRT_Union:
1781 case SRT_Except:
1782 case SRT_Discard:
1783 pOrderBy = 0;
1784 break;
1785 default:
1786 break;
drh22827922000-06-06 17:27:05 +00001787 }
1788
drh10e5e3c2000-06-08 00:19:02 +00001789 /* At this point, we should have allocated all the cursors that we
drh832508b2002-03-02 17:04:07 +00001790 ** need to handle subquerys and temporary tables.
drh10e5e3c2000-06-08 00:19:02 +00001791 **
drh967e8b72000-06-21 13:59:10 +00001792 ** Resolve the column names and do a semantics check on all the expressions.
drh22827922000-06-06 17:27:05 +00001793 */
drh4794b982000-06-06 13:54:14 +00001794 for(i=0; i<pEList->nExpr; i++){
drh832508b2002-03-02 17:04:07 +00001795 if( sqliteExprResolveIds(pParse, base, pTabList, 0, pEList->a[i].pExpr) ){
drh1d83f052002-02-17 00:30:36 +00001796 goto select_end;
drhcce7d172000-05-31 15:34:51 +00001797 }
drh22827922000-06-06 17:27:05 +00001798 if( sqliteExprCheck(pParse, pEList->a[i].pExpr, 1, &isAgg) ){
drh1d83f052002-02-17 00:30:36 +00001799 goto select_end;
drhcce7d172000-05-31 15:34:51 +00001800 }
1801 }
drhcce7d172000-05-31 15:34:51 +00001802 if( pWhere ){
drh832508b2002-03-02 17:04:07 +00001803 if( sqliteExprResolveIds(pParse, base, pTabList, pEList, pWhere) ){
drh1d83f052002-02-17 00:30:36 +00001804 goto select_end;
drhcce7d172000-05-31 15:34:51 +00001805 }
1806 if( sqliteExprCheck(pParse, pWhere, 0, 0) ){
drh1d83f052002-02-17 00:30:36 +00001807 goto select_end;
drhcce7d172000-05-31 15:34:51 +00001808 }
1809 }
1810 if( pOrderBy ){
1811 for(i=0; i<pOrderBy->nExpr; i++){
drh22827922000-06-06 17:27:05 +00001812 Expr *pE = pOrderBy->a[i].pExpr;
drh92086432002-01-22 14:11:29 +00001813 if( sqliteExprIsConstant(pE) ){
drhe4de1fe2002-06-02 16:09:01 +00001814 int iCol;
1815 if( sqliteExprIsInteger(pE, &iCol)==0 ){
1816 sqliteSetString(&pParse->zErrMsg,
1817 "ORDER BY terms must not be non-integer constants", 0);
1818 pParse->nErr++;
1819 goto select_end;
1820 }else if( iCol<=0 || iCol>pEList->nExpr ){
1821 char zBuf[2000];
1822 sprintf(zBuf,"ORDER BY column number %d out of range - should be "
1823 "between 1 and %d", iCol, pEList->nExpr);
1824 sqliteSetString(&pParse->zErrMsg, zBuf, 0);
1825 pParse->nErr++;
1826 goto select_end;
1827 }
1828 sqliteExprDelete(pE);
1829 pE = pOrderBy->a[i].pExpr = sqliteExprDup(pEList->a[iCol-1].pExpr);
drh92086432002-01-22 14:11:29 +00001830 }
drh832508b2002-03-02 17:04:07 +00001831 if( sqliteExprResolveIds(pParse, base, pTabList, pEList, pE) ){
drh1d83f052002-02-17 00:30:36 +00001832 goto select_end;
drhcce7d172000-05-31 15:34:51 +00001833 }
drh22827922000-06-06 17:27:05 +00001834 if( sqliteExprCheck(pParse, pE, isAgg, 0) ){
drh1d83f052002-02-17 00:30:36 +00001835 goto select_end;
drhcce7d172000-05-31 15:34:51 +00001836 }
1837 }
1838 }
drh22827922000-06-06 17:27:05 +00001839 if( pGroupBy ){
1840 for(i=0; i<pGroupBy->nExpr; i++){
1841 Expr *pE = pGroupBy->a[i].pExpr;
drh92086432002-01-22 14:11:29 +00001842 if( sqliteExprIsConstant(pE) ){
1843 sqliteSetString(&pParse->zErrMsg,
1844 "GROUP BY expressions should not be constant", 0);
1845 pParse->nErr++;
drh1d83f052002-02-17 00:30:36 +00001846 goto select_end;
drh92086432002-01-22 14:11:29 +00001847 }
drh832508b2002-03-02 17:04:07 +00001848 if( sqliteExprResolveIds(pParse, base, pTabList, pEList, pE) ){
drh1d83f052002-02-17 00:30:36 +00001849 goto select_end;
drh22827922000-06-06 17:27:05 +00001850 }
1851 if( sqliteExprCheck(pParse, pE, isAgg, 0) ){
drh1d83f052002-02-17 00:30:36 +00001852 goto select_end;
drh22827922000-06-06 17:27:05 +00001853 }
1854 }
1855 }
1856 if( pHaving ){
1857 if( pGroupBy==0 ){
drhda932812000-06-06 18:00:15 +00001858 sqliteSetString(&pParse->zErrMsg, "a GROUP BY clause is required "
1859 "before HAVING", 0);
drh22827922000-06-06 17:27:05 +00001860 pParse->nErr++;
drh1d83f052002-02-17 00:30:36 +00001861 goto select_end;
drh22827922000-06-06 17:27:05 +00001862 }
drh832508b2002-03-02 17:04:07 +00001863 if( sqliteExprResolveIds(pParse, base, pTabList, pEList, pHaving) ){
drh1d83f052002-02-17 00:30:36 +00001864 goto select_end;
drh22827922000-06-06 17:27:05 +00001865 }
drhda932812000-06-06 18:00:15 +00001866 if( sqliteExprCheck(pParse, pHaving, isAgg, 0) ){
drh1d83f052002-02-17 00:30:36 +00001867 goto select_end;
drh22827922000-06-06 17:27:05 +00001868 }
drhcce7d172000-05-31 15:34:51 +00001869 }
1870
drh9562b552002-02-19 15:00:07 +00001871 /* Check for the special case of a min() or max() function by itself
1872 ** in the result set.
1873 */
1874 if( simpleMinMaxQuery(pParse, p, eDest, iParm) ){
drh5cf8e8c2002-02-19 22:42:05 +00001875 rc = 0;
drh9562b552002-02-19 15:00:07 +00001876 goto select_end;
1877 }
1878
drhd820cb12002-02-18 03:21:45 +00001879 /* Begin generating code.
1880 */
1881 v = sqliteGetVdbe(pParse);
1882 if( v==0 ) goto select_end;
1883
drh0bb28102002-05-08 11:54:14 +00001884 /* Identify column names if we will be using in the callback. This
1885 ** step is skipped if the output is going to a table or a memory cell.
1886 */
1887 if( eDest==SRT_Callback ){
1888 generateColumnNames(pParse, p->base, pTabList, pEList);
1889 }
1890
1891 /* Set the limiter
1892 */
1893 if( p->nLimit<=0 ){
drhd11d3822002-06-21 23:01:49 +00001894 p->nLimit = -1;
drh0bb28102002-05-08 11:54:14 +00001895 p->nOffset = 0;
1896 }else{
drhd11d3822002-06-21 23:01:49 +00001897 int iMem = pParse->nMem++;
1898 sqliteVdbeAddOp(v, OP_Integer, -p->nLimit, 0);
drhbf5cd972002-06-24 12:20:23 +00001899 sqliteVdbeAddOp(v, OP_MemStore, iMem, 1);
drhd11d3822002-06-21 23:01:49 +00001900 p->nLimit = iMem;
1901 if( p->nOffset<=0 ){
1902 p->nOffset = 0;
1903 }else{
1904 iMem = pParse->nMem++;
1905 sqliteVdbeAddOp(v, OP_Integer, -p->nOffset, 0);
drhbf5cd972002-06-24 12:20:23 +00001906 sqliteVdbeAddOp(v, OP_MemStore, iMem, 1);
drhd11d3822002-06-21 23:01:49 +00001907 p->nOffset = iMem;
1908 }
drh0bb28102002-05-08 11:54:14 +00001909 }
1910
drhd820cb12002-02-18 03:21:45 +00001911 /* Generate code for all sub-queries in the FROM clause
1912 */
drhad3cab52002-05-24 02:04:32 +00001913 for(i=0; i<pTabList->nSrc; i++){
drha76b5df2002-02-23 02:32:10 +00001914 if( pTabList->a[i].pSelect==0 ) continue;
drh2d0794e2002-03-03 03:03:52 +00001915 sqliteSelect(pParse, pTabList->a[i].pSelect, SRT_TempTable, base+i,
drh1b2e0322002-03-03 02:49:51 +00001916 p, i, &isAgg);
1917 pTabList = p->pSrc;
1918 pWhere = p->pWhere;
drhacd4c692002-03-07 02:02:51 +00001919 if( eDest==SRT_Callback ){
1920 pOrderBy = p->pOrderBy;
1921 }
drh1b2e0322002-03-03 02:49:51 +00001922 pGroupBy = p->pGroupBy;
1923 pHaving = p->pHaving;
1924 isDistinct = p->isDistinct;
drhd820cb12002-02-18 03:21:45 +00001925 }
1926
drh832508b2002-03-02 17:04:07 +00001927 /* Check to see if this is a subquery that can be "flattened" into its parent.
1928 ** If flattening is a possiblity, do so and return immediately.
1929 */
drh1b2e0322002-03-03 02:49:51 +00001930 if( pParent && pParentAgg &&
drh8c74a8c2002-08-25 19:20:40 +00001931 flattenSubquery(pParse, pParent, parentTab, *pParentAgg, isAgg) ){
drh1b2e0322002-03-03 02:49:51 +00001932 if( isAgg ) *pParentAgg = 1;
drh832508b2002-03-02 17:04:07 +00001933 return rc;
1934 }
drh832508b2002-03-02 17:04:07 +00001935
drh2d0794e2002-03-03 03:03:52 +00001936 /* If the output is destined for a temporary table, open that table.
1937 */
1938 if( eDest==SRT_TempTable ){
1939 sqliteVdbeAddOp(v, OP_OpenTemp, iParm, 0);
1940 }
1941
drh22827922000-06-06 17:27:05 +00001942 /* Do an analysis of aggregate expressions.
drhefb72512000-05-31 20:00:52 +00001943 */
drhd820cb12002-02-18 03:21:45 +00001944 sqliteAggregateInfoReset(pParse);
drh22827922000-06-06 17:27:05 +00001945 if( isAgg ){
drh0bce8352002-02-28 00:41:10 +00001946 assert( pParse->nAgg==0 );
drh22827922000-06-06 17:27:05 +00001947 for(i=0; i<pEList->nExpr; i++){
1948 if( sqliteExprAnalyzeAggregates(pParse, pEList->a[i].pExpr) ){
drh1d83f052002-02-17 00:30:36 +00001949 goto select_end;
drh22827922000-06-06 17:27:05 +00001950 }
1951 }
1952 if( pGroupBy ){
1953 for(i=0; i<pGroupBy->nExpr; i++){
1954 if( sqliteExprAnalyzeAggregates(pParse, pGroupBy->a[i].pExpr) ){
drh1d83f052002-02-17 00:30:36 +00001955 goto select_end;
drh22827922000-06-06 17:27:05 +00001956 }
1957 }
1958 }
1959 if( pHaving && sqliteExprAnalyzeAggregates(pParse, pHaving) ){
drh1d83f052002-02-17 00:30:36 +00001960 goto select_end;
drh22827922000-06-06 17:27:05 +00001961 }
drh191b6902000-06-08 11:13:01 +00001962 if( pOrderBy ){
1963 for(i=0; i<pOrderBy->nExpr; i++){
1964 if( sqliteExprAnalyzeAggregates(pParse, pOrderBy->a[i].pExpr) ){
drh1d83f052002-02-17 00:30:36 +00001965 goto select_end;
drh191b6902000-06-08 11:13:01 +00001966 }
1967 }
1968 }
drhefb72512000-05-31 20:00:52 +00001969 }
1970
drh22827922000-06-06 17:27:05 +00001971 /* Reset the aggregator
drhcce7d172000-05-31 15:34:51 +00001972 */
1973 if( isAgg ){
drh99fcd712001-10-13 01:06:47 +00001974 sqliteVdbeAddOp(v, OP_AggReset, 0, pParse->nAgg);
drhe5095352002-02-24 03:25:14 +00001975 for(i=0; i<pParse->nAgg; i++){
drh0bce8352002-02-28 00:41:10 +00001976 FuncDef *pFunc;
1977 if( (pFunc = pParse->aAgg[i].pFunc)!=0 && pFunc->xFinalize!=0 ){
drh1350b032002-02-27 19:00:20 +00001978 sqliteVdbeAddOp(v, OP_AggInit, 0, i);
drh0bce8352002-02-28 00:41:10 +00001979 sqliteVdbeChangeP3(v, -1, (char*)pFunc, P3_POINTER);
drhe5095352002-02-24 03:25:14 +00001980 }
1981 }
drh1bee3d72001-10-15 00:44:35 +00001982 if( pGroupBy==0 ){
1983 sqliteVdbeAddOp(v, OP_String, 0, 0);
drh1bee3d72001-10-15 00:44:35 +00001984 sqliteVdbeAddOp(v, OP_AggFocus, 0, 0);
drh1bee3d72001-10-15 00:44:35 +00001985 }
drhcce7d172000-05-31 15:34:51 +00001986 }
1987
drh19a775c2000-06-05 18:54:46 +00001988 /* Initialize the memory cell to NULL
1989 */
drhfef52082000-06-06 01:50:43 +00001990 if( eDest==SRT_Mem ){
drh99fcd712001-10-13 01:06:47 +00001991 sqliteVdbeAddOp(v, OP_String, 0, 0);
drh8721ce42001-11-07 14:22:00 +00001992 sqliteVdbeAddOp(v, OP_MemStore, iParm, 1);
drh19a775c2000-06-05 18:54:46 +00001993 }
1994
drh832508b2002-03-02 17:04:07 +00001995 /* Open a temporary table to use for the distinct set.
drhefb72512000-05-31 20:00:52 +00001996 */
drh19a775c2000-06-05 18:54:46 +00001997 if( isDistinct ){
drh832508b2002-03-02 17:04:07 +00001998 distinct = pParse->nTab++;
drhc6b52df2002-01-04 03:09:29 +00001999 sqliteVdbeAddOp(v, OP_OpenTemp, distinct, 1);
drh832508b2002-03-02 17:04:07 +00002000 }else{
2001 distinct = -1;
drhefb72512000-05-31 20:00:52 +00002002 }
drh832508b2002-03-02 17:04:07 +00002003
2004 /* Begin the database scan
2005 */
drh68d2e592002-08-04 00:52:38 +00002006 pWInfo = sqliteWhereBegin(pParse, p->base, pTabList, pWhere, 0,
2007 pGroupBy ? 0 : &pOrderBy);
drh1d83f052002-02-17 00:30:36 +00002008 if( pWInfo==0 ) goto select_end;
drhcce7d172000-05-31 15:34:51 +00002009
drh22827922000-06-06 17:27:05 +00002010 /* Use the standard inner loop if we are not dealing with
2011 ** aggregates
drhcce7d172000-05-31 15:34:51 +00002012 */
drhda9d6c42000-05-31 18:20:14 +00002013 if( !isAgg ){
drhdf199a22002-06-14 22:38:41 +00002014 if( selectInnerLoop(pParse, p, pEList, 0, 0, pOrderBy, distinct, eDest,
2015 iParm, pWInfo->iContinue, pWInfo->iBreak) ){
drh1d83f052002-02-17 00:30:36 +00002016 goto select_end;
drhda9d6c42000-05-31 18:20:14 +00002017 }
drhcce7d172000-05-31 15:34:51 +00002018 }
drhefb72512000-05-31 20:00:52 +00002019
drhe3184742002-06-19 14:27:05 +00002020 /* If we are dealing with aggregates, then do the special aggregate
drh22827922000-06-06 17:27:05 +00002021 ** processing.
drhefb72512000-05-31 20:00:52 +00002022 */
drh22827922000-06-06 17:27:05 +00002023 else{
drh22827922000-06-06 17:27:05 +00002024 if( pGroupBy ){
drh1bee3d72001-10-15 00:44:35 +00002025 int lbl1;
drh22827922000-06-06 17:27:05 +00002026 for(i=0; i<pGroupBy->nExpr; i++){
2027 sqliteExprCode(pParse, pGroupBy->a[i].pExpr);
2028 }
drh99fcd712001-10-13 01:06:47 +00002029 sqliteVdbeAddOp(v, OP_MakeKey, pGroupBy->nExpr, 0);
drh491791a2002-07-18 00:34:09 +00002030 if( pParse->db->file_format>=4 ) sqliteAddKeyType(v, pGroupBy);
drh1bee3d72001-10-15 00:44:35 +00002031 lbl1 = sqliteVdbeMakeLabel(v);
drh99fcd712001-10-13 01:06:47 +00002032 sqliteVdbeAddOp(v, OP_AggFocus, 0, lbl1);
drh22827922000-06-06 17:27:05 +00002033 for(i=0; i<pParse->nAgg; i++){
2034 if( pParse->aAgg[i].isAgg ) continue;
2035 sqliteExprCode(pParse, pParse->aAgg[i].pExpr);
drh99fcd712001-10-13 01:06:47 +00002036 sqliteVdbeAddOp(v, OP_AggSet, 0, i);
drhcce7d172000-05-31 15:34:51 +00002037 }
drh22827922000-06-06 17:27:05 +00002038 sqliteVdbeResolveLabel(v, lbl1);
drhcce7d172000-05-31 15:34:51 +00002039 }
drh22827922000-06-06 17:27:05 +00002040 for(i=0; i<pParse->nAgg; i++){
2041 Expr *pE;
drh0bce8352002-02-28 00:41:10 +00002042 int j;
drh22827922000-06-06 17:27:05 +00002043 if( !pParse->aAgg[i].isAgg ) continue;
2044 pE = pParse->aAgg[i].pExpr;
drh22827922000-06-06 17:27:05 +00002045 assert( pE->op==TK_AGG_FUNCTION );
drh0bce8352002-02-28 00:41:10 +00002046 if( pE->pList ){
2047 for(j=0; j<pE->pList->nExpr; j++){
2048 sqliteExprCode(pParse, pE->pList->a[j].pExpr);
2049 }
drhe5095352002-02-24 03:25:14 +00002050 }
drh0bce8352002-02-28 00:41:10 +00002051 sqliteVdbeAddOp(v, OP_Integer, i, 0);
drhf55f25f2002-02-28 01:46:11 +00002052 sqliteVdbeAddOp(v, OP_AggFunc, 0, pE->pList ? pE->pList->nExpr : 0);
drh0bce8352002-02-28 00:41:10 +00002053 assert( pParse->aAgg[i].pFunc!=0 );
2054 assert( pParse->aAgg[i].pFunc->xStep!=0 );
2055 sqliteVdbeChangeP3(v, -1, (char*)pParse->aAgg[i].pFunc, P3_POINTER);
drh22827922000-06-06 17:27:05 +00002056 }
drhcce7d172000-05-31 15:34:51 +00002057 }
2058
2059 /* End the database scan loop.
2060 */
2061 sqliteWhereEnd(pWInfo);
2062
drh22827922000-06-06 17:27:05 +00002063 /* If we are processing aggregates, we need to set up a second loop
2064 ** over all of the aggregate values and process them.
2065 */
2066 if( isAgg ){
2067 int endagg = sqliteVdbeMakeLabel(v);
2068 int startagg;
drh99fcd712001-10-13 01:06:47 +00002069 startagg = sqliteVdbeAddOp(v, OP_AggNext, 0, endagg);
drh22827922000-06-06 17:27:05 +00002070 pParse->useAgg = 1;
2071 if( pHaving ){
drhf5905aa2002-05-26 20:54:33 +00002072 sqliteExprIfFalse(pParse, pHaving, startagg, 1);
drh22827922000-06-06 17:27:05 +00002073 }
drhdf199a22002-06-14 22:38:41 +00002074 if( selectInnerLoop(pParse, p, pEList, 0, 0, pOrderBy, distinct, eDest,
2075 iParm, startagg, endagg) ){
drh1d83f052002-02-17 00:30:36 +00002076 goto select_end;
drh22827922000-06-06 17:27:05 +00002077 }
drh99fcd712001-10-13 01:06:47 +00002078 sqliteVdbeAddOp(v, OP_Goto, 0, startagg);
2079 sqliteVdbeResolveLabel(v, endagg);
2080 sqliteVdbeAddOp(v, OP_Noop, 0, 0);
drh22827922000-06-06 17:27:05 +00002081 pParse->useAgg = 0;
2082 }
2083
drhcce7d172000-05-31 15:34:51 +00002084 /* If there is an ORDER BY clause, then we need to sort the results
2085 ** and send them to the callback one by one.
2086 */
2087 if( pOrderBy ){
drhc926afb2002-06-20 03:38:26 +00002088 generateSortTail(p, v, pEList->nExpr, eDest, iParm);
drhcce7d172000-05-31 15:34:51 +00002089 }
drh6a535342001-10-19 16:44:56 +00002090
2091
2092 /* Issue a null callback if that is what the user wants.
2093 */
2094 if( (pParse->db->flags & SQLITE_NullCallback)!=0 && eDest==SRT_Callback ){
2095 sqliteVdbeAddOp(v, OP_NullCallback, pEList->nExpr, 0);
2096 }
2097
drh1d83f052002-02-17 00:30:36 +00002098 /* The SELECT was successfully coded. Set the return code to 0
2099 ** to indicate no errors.
2100 */
2101 rc = 0;
2102
2103 /* Control jumps to here if an error is encountered above, or upon
2104 ** successful coding of the SELECT.
2105 */
2106select_end:
drh142e30d2002-08-28 03:00:58 +00002107 /* pParse->nTab = base; */
drh1d83f052002-02-17 00:30:36 +00002108 sqliteAggregateInfoReset(pParse);
2109 return rc;
drhcce7d172000-05-31 15:34:51 +00002110}