blob: 9ff90043e99d62336dc476b640cc25006795ad2f [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**
drh8721ce42001-11-07 14:22:00 +000015** $Id: select.c,v 1.48 2001/11/07 14:22:00 drh Exp $
drhcce7d172000-05-31 15:34:51 +000016*/
17#include "sqliteInt.h"
18
drhcce7d172000-05-31 15:34:51 +000019/*
drh9bb61fe2000-06-05 16:01:39 +000020** Allocate a new Select structure and return a pointer to that
21** structure.
drhcce7d172000-05-31 15:34:51 +000022*/
drh9bb61fe2000-06-05 16:01:39 +000023Select *sqliteSelectNew(
drhdaffd0e2001-04-11 14:28:42 +000024 ExprList *pEList, /* which columns to include in the result */
25 IdList *pSrc, /* the FROM clause -- which tables to scan */
26 Expr *pWhere, /* the WHERE clause */
27 ExprList *pGroupBy, /* the GROUP BY clause */
28 Expr *pHaving, /* the HAVING clause */
29 ExprList *pOrderBy, /* the ORDER BY clause */
drh9bbca4c2001-11-06 04:00:18 +000030 int isDistinct, /* true if the DISTINCT keyword is present */
31 int nLimit, /* LIMIT value. -1 means not used */
32 int nOffset /* OFFSET value. -1 means not used */
drh9bb61fe2000-06-05 16:01:39 +000033){
34 Select *pNew;
35 pNew = sqliteMalloc( sizeof(*pNew) );
drhdaffd0e2001-04-11 14:28:42 +000036 if( pNew==0 ){
37 sqliteExprListDelete(pEList);
38 sqliteIdListDelete(pSrc);
39 sqliteExprDelete(pWhere);
40 sqliteExprListDelete(pGroupBy);
41 sqliteExprDelete(pHaving);
42 sqliteExprListDelete(pOrderBy);
43 }else{
44 pNew->pEList = pEList;
45 pNew->pSrc = pSrc;
46 pNew->pWhere = pWhere;
47 pNew->pGroupBy = pGroupBy;
48 pNew->pHaving = pHaving;
49 pNew->pOrderBy = pOrderBy;
50 pNew->isDistinct = isDistinct;
51 pNew->op = TK_SELECT;
drh9bbca4c2001-11-06 04:00:18 +000052 pNew->nLimit = nLimit;
53 pNew->nOffset = nOffset;
drhdaffd0e2001-04-11 14:28:42 +000054 }
drh9bb61fe2000-06-05 16:01:39 +000055 return pNew;
56}
57
58/*
59** Delete the given Select structure and all of its substructures.
60*/
61void sqliteSelectDelete(Select *p){
drh82c3d632000-06-06 21:56:07 +000062 if( p==0 ) return;
drh9bb61fe2000-06-05 16:01:39 +000063 sqliteExprListDelete(p->pEList);
64 sqliteIdListDelete(p->pSrc);
65 sqliteExprDelete(p->pWhere);
66 sqliteExprListDelete(p->pGroupBy);
67 sqliteExprDelete(p->pHaving);
68 sqliteExprListDelete(p->pOrderBy);
drh82c3d632000-06-06 21:56:07 +000069 sqliteSelectDelete(p->pPrior);
drh9bb61fe2000-06-05 16:01:39 +000070 sqliteFree(p);
71}
72
73/*
drh22827922000-06-06 17:27:05 +000074** Delete the aggregate information from the parse structure.
75*/
76void sqliteParseInfoReset(Parse *pParse){
77 sqliteFree(pParse->aAgg);
78 pParse->aAgg = 0;
79 pParse->nAgg = 0;
80 pParse->iAggCount = -1;
81 pParse->useAgg = 0;
82}
83
84/*
85** This routine generates the code for the inside of the inner loop
86** of a SELECT.
drh82c3d632000-06-06 21:56:07 +000087**
88** The pEList is used to determine the values for each column in the
drh967e8b72000-06-21 13:59:10 +000089** result row. Except if pEList==NULL, then we just read nColumn
drh82c3d632000-06-06 21:56:07 +000090** elements from the srcTab table.
drh22827922000-06-06 17:27:05 +000091*/
92static int selectInnerLoop(
93 Parse *pParse, /* The parser context */
94 ExprList *pEList, /* List of values being extracted */
drh82c3d632000-06-06 21:56:07 +000095 int srcTab, /* Pull data from this table */
drh967e8b72000-06-21 13:59:10 +000096 int nColumn, /* Number of columns in the source table */
drh22827922000-06-06 17:27:05 +000097 ExprList *pOrderBy, /* If not NULL, sort results using this key */
98 int distinct, /* If >=0, make sure results are distinct */
99 int eDest, /* How to dispose of the results */
100 int iParm, /* An argument to the disposal method */
101 int iContinue, /* Jump here to continue with next row */
102 int iBreak /* Jump here to break out of the inner loop */
103){
104 Vdbe *v = pParse->pVdbe;
105 int i;
drhdaffd0e2001-04-11 14:28:42 +0000106 if( v==0 ) return 0;
drh22827922000-06-06 17:27:05 +0000107
drh967e8b72000-06-21 13:59:10 +0000108 /* Pull the requested columns.
drh22827922000-06-06 17:27:05 +0000109 */
drh82c3d632000-06-06 21:56:07 +0000110 if( pEList ){
111 for(i=0; i<pEList->nExpr; i++){
112 sqliteExprCode(pParse, pEList->a[i].pExpr);
113 }
drh967e8b72000-06-21 13:59:10 +0000114 nColumn = pEList->nExpr;
drh82c3d632000-06-06 21:56:07 +0000115 }else{
drh967e8b72000-06-21 13:59:10 +0000116 for(i=0; i<nColumn; i++){
drh99fcd712001-10-13 01:06:47 +0000117 sqliteVdbeAddOp(v, OP_Column, srcTab, i);
drh82c3d632000-06-06 21:56:07 +0000118 }
drh22827922000-06-06 17:27:05 +0000119 }
120
drhdaffd0e2001-04-11 14:28:42 +0000121 /* If the DISTINCT keyword was present on the SELECT statement
122 ** and this row has been seen before, then do not make this row
123 ** part of the result.
drh22827922000-06-06 17:27:05 +0000124 */
125 if( distinct>=0 ){
126 int lbl = sqliteVdbeMakeLabel(v);
drh99fcd712001-10-13 01:06:47 +0000127 sqliteVdbeAddOp(v, OP_MakeKey, pEList->nExpr, 1);
128 sqliteVdbeAddOp(v, OP_Distinct, distinct, lbl);
129 sqliteVdbeAddOp(v, OP_Pop, pEList->nExpr+1, 0);
130 sqliteVdbeAddOp(v, OP_Goto, 0, iContinue);
131 sqliteVdbeResolveLabel(v, lbl);
132 sqliteVdbeAddOp(v, OP_String, 0, 0);
133 sqliteVdbeChangeP3(v, -1, "", P3_STATIC);
134 sqliteVdbeAddOp(v, OP_Put, distinct, 0);
drh22827922000-06-06 17:27:05 +0000135 }
drh82c3d632000-06-06 21:56:07 +0000136
drh22827922000-06-06 17:27:05 +0000137 /* If there is an ORDER BY clause, then store the results
138 ** in a sorter.
139 */
140 if( pOrderBy ){
141 char *zSortOrder;
drh99fcd712001-10-13 01:06:47 +0000142 sqliteVdbeAddOp(v, OP_SortMakeRec, nColumn, 0);
drh22827922000-06-06 17:27:05 +0000143 zSortOrder = sqliteMalloc( pOrderBy->nExpr + 1 );
144 if( zSortOrder==0 ) return 1;
145 for(i=0; i<pOrderBy->nExpr; i++){
drhd8bc7082000-06-07 23:51:50 +0000146 zSortOrder[i] = pOrderBy->a[i].sortOrder ? '-' : '+';
drh22827922000-06-06 17:27:05 +0000147 sqliteExprCode(pParse, pOrderBy->a[i].pExpr);
148 }
149 zSortOrder[pOrderBy->nExpr] = 0;
drh99fcd712001-10-13 01:06:47 +0000150 sqliteVdbeAddOp(v, OP_SortMakeKey, pOrderBy->nExpr, 0);
151 sqliteVdbeChangeP3(v, -1, zSortOrder, strlen(zSortOrder));
drh6e142f52000-06-08 13:36:40 +0000152 sqliteFree(zSortOrder);
drh99fcd712001-10-13 01:06:47 +0000153 sqliteVdbeAddOp(v, OP_SortPut, 0, 0);
drh22827922000-06-06 17:27:05 +0000154 }else
155
drh82c3d632000-06-06 21:56:07 +0000156 /* In this mode, write each query result to the key of the temporary
157 ** table iParm.
drh22827922000-06-06 17:27:05 +0000158 */
drh82c3d632000-06-06 21:56:07 +0000159 if( eDest==SRT_Union ){
drh99fcd712001-10-13 01:06:47 +0000160 sqliteVdbeAddOp(v, OP_MakeRecord, nColumn, 0);
161 sqliteVdbeAddOp(v, OP_String, iParm, 0);
162 sqliteVdbeChangeP3(v, -1, "", P3_STATIC);
163 sqliteVdbeAddOp(v, OP_Put, iParm, 0);
drh82c3d632000-06-06 21:56:07 +0000164 }else
165
drh5974a302000-06-07 14:42:26 +0000166 /* Store the result as data using a unique key.
167 */
168 if( eDest==SRT_Table ){
drh99fcd712001-10-13 01:06:47 +0000169 sqliteVdbeAddOp(v, OP_MakeRecord, nColumn, 0);
170 sqliteVdbeAddOp(v, OP_NewRecno, iParm, 0);
171 sqliteVdbeAddOp(v, OP_Pull, 1, 0);
172 sqliteVdbeAddOp(v, OP_Put, iParm, 0);
drh5974a302000-06-07 14:42:26 +0000173 }else
174
drh82c3d632000-06-06 21:56:07 +0000175 /* Construct a record from the query result, but instead of
176 ** saving that record, use it as a key to delete elements from
177 ** the temporary table iParm.
178 */
179 if( eDest==SRT_Except ){
drh99fcd712001-10-13 01:06:47 +0000180 int addr = sqliteVdbeAddOp(v, OP_MakeRecord, nColumn, 0);
181 sqliteVdbeAddOp(v, OP_NotFound, iParm, addr+3);
182 sqliteVdbeAddOp(v, OP_Delete, iParm, 0);
drh22827922000-06-06 17:27:05 +0000183 }else
184
185 /* If we are creating a set for an "expr IN (SELECT ...)" construct,
186 ** then there should be a single item on the stack. Write this
187 ** item into the set table with bogus data.
188 */
189 if( eDest==SRT_Set ){
drh967e8b72000-06-21 13:59:10 +0000190 assert( nColumn==1 );
drh99fcd712001-10-13 01:06:47 +0000191 sqliteVdbeAddOp(v, OP_String, 0, 0);
192 sqliteVdbeChangeP3(v, -1, "", P3_STATIC);
193 sqliteVdbeAddOp(v, OP_Put, iParm, 0);
drh22827922000-06-06 17:27:05 +0000194 }else
195
drh82c3d632000-06-06 21:56:07 +0000196
drh22827922000-06-06 17:27:05 +0000197 /* If this is a scalar select that is part of an expression, then
198 ** store the results in the appropriate memory cell and break out
199 ** of the scan loop.
200 */
201 if( eDest==SRT_Mem ){
drh967e8b72000-06-21 13:59:10 +0000202 assert( nColumn==1 );
drh8721ce42001-11-07 14:22:00 +0000203 sqliteVdbeAddOp(v, OP_MemStore, iParm, 1);
drh99fcd712001-10-13 01:06:47 +0000204 sqliteVdbeAddOp(v, OP_Goto, 0, iBreak);
drh22827922000-06-06 17:27:05 +0000205 }else
206
207 /* If none of the above, send the data to the callback function.
208 */
209 {
drh9bbca4c2001-11-06 04:00:18 +0000210 sqliteVdbeAddOp(v, OP_Callback, nColumn, iBreak);
drh82c3d632000-06-06 21:56:07 +0000211 }
212 return 0;
213}
214
215/*
drhd8bc7082000-06-07 23:51:50 +0000216** If the inner loop was generated using a non-null pOrderBy argument,
217** then the results were placed in a sorter. After the loop is terminated
218** we need to run the sorter and output the results. The following
219** routine generates the code needed to do that.
220*/
drh967e8b72000-06-21 13:59:10 +0000221static void generateSortTail(Vdbe *v, int nColumn){
drhd8bc7082000-06-07 23:51:50 +0000222 int end = sqliteVdbeMakeLabel(v);
223 int addr;
drh99fcd712001-10-13 01:06:47 +0000224 sqliteVdbeAddOp(v, OP_Sort, 0, 0);
225 addr = sqliteVdbeAddOp(v, OP_SortNext, 0, end);
drh9bbca4c2001-11-06 04:00:18 +0000226 sqliteVdbeAddOp(v, OP_SortCallback, nColumn, end);
drh99fcd712001-10-13 01:06:47 +0000227 sqliteVdbeAddOp(v, OP_Goto, 0, addr);
228 sqliteVdbeResolveLabel(v, end);
drha8b38d22001-11-01 14:41:34 +0000229 sqliteVdbeAddOp(v, OP_SortReset, 0, 0);
drhd8bc7082000-06-07 23:51:50 +0000230}
231
232/*
drh82c3d632000-06-06 21:56:07 +0000233** Generate code that will tell the VDBE how many columns there
234** are in the result and the name for each column. This information
235** is used to provide "argc" and "azCol[]" values in the callback.
236*/
drhd8bc7082000-06-07 23:51:50 +0000237static
238void generateColumnNames(Parse *pParse, IdList *pTabList, ExprList *pEList){
239 Vdbe *v = pParse->pVdbe;
drh82c3d632000-06-06 21:56:07 +0000240 int i;
drhdaffd0e2001-04-11 14:28:42 +0000241 if( pParse->colNamesSet || v==0 || sqlite_malloc_failed ) return;
drhd8bc7082000-06-07 23:51:50 +0000242 pParse->colNamesSet = 1;
drh99fcd712001-10-13 01:06:47 +0000243 sqliteVdbeAddOp(v, OP_ColumnCount, pEList->nExpr, 0);
drh82c3d632000-06-06 21:56:07 +0000244 for(i=0; i<pEList->nExpr; i++){
245 Expr *p;
drh1bee3d72001-10-15 00:44:35 +0000246 int showFullNames;
drh82c3d632000-06-06 21:56:07 +0000247 if( pEList->a[i].zName ){
248 char *zName = pEList->a[i].zName;
drh99fcd712001-10-13 01:06:47 +0000249 sqliteVdbeAddOp(v, OP_ColumnName, i, 0);
250 sqliteVdbeChangeP3(v, -1, zName, strlen(zName));
drh82c3d632000-06-06 21:56:07 +0000251 continue;
252 }
253 p = pEList->a[i].pExpr;
drhdaffd0e2001-04-11 14:28:42 +0000254 if( p==0 ) continue;
drh1bee3d72001-10-15 00:44:35 +0000255 showFullNames = (pParse->db->flags & SQLITE_FullColNames)!=0;
256 if( p->span.z && p->span.z[0] && !showFullNames ){
drh99fcd712001-10-13 01:06:47 +0000257 int addr = sqliteVdbeAddOp(v,OP_ColumnName, i, 0);
258 sqliteVdbeChangeP3(v, -1, p->span.z, p->span.n);
drhe1b6a5b2000-07-29 13:06:59 +0000259 sqliteVdbeCompressSpace(v, addr);
drh1bee3d72001-10-15 00:44:35 +0000260 }else if( p->op==TK_COLUMN && pTabList ){
261 if( pTabList->nId>1 || showFullNames ){
drh82c3d632000-06-06 21:56:07 +0000262 char *zName = 0;
drh98808ba2001-10-18 12:34:46 +0000263 Table *pTab = pTabList->a[p->iTable - pParse->nTab].pTab;
drh82c3d632000-06-06 21:56:07 +0000264 char *zTab;
265
drh98808ba2001-10-18 12:34:46 +0000266 zTab = pTabList->a[p->iTable - pParse->nTab].zAlias;
drh01a34662001-10-20 12:30:10 +0000267 if( showFullNames || zTab==0 ) zTab = pTab->zName;
drh967e8b72000-06-21 13:59:10 +0000268 sqliteSetString(&zName, zTab, ".", pTab->aCol[p->iColumn].zName, 0);
drh99fcd712001-10-13 01:06:47 +0000269 sqliteVdbeAddOp(v, OP_ColumnName, i, 0);
270 sqliteVdbeChangeP3(v, -1, zName, strlen(zName));
drh82c3d632000-06-06 21:56:07 +0000271 sqliteFree(zName);
272 }else{
273 Table *pTab = pTabList->a[0].pTab;
drh967e8b72000-06-21 13:59:10 +0000274 char *zName = pTab->aCol[p->iColumn].zName;
drh99fcd712001-10-13 01:06:47 +0000275 sqliteVdbeAddOp(v, OP_ColumnName, i, 0);
276 sqliteVdbeChangeP3(v, -1, zName, P3_STATIC);
drh82c3d632000-06-06 21:56:07 +0000277 }
drh1bee3d72001-10-15 00:44:35 +0000278 }else if( p->span.z && p->span.z[0] ){
279 int addr = sqliteVdbeAddOp(v,OP_ColumnName, i, 0);
280 sqliteVdbeChangeP3(v, -1, p->span.z, p->span.n);
281 sqliteVdbeCompressSpace(v, addr);
282 }else{
283 char zName[30];
284 assert( p->op!=TK_COLUMN || pTabList==0 );
285 sprintf(zName, "column%d", i+1);
286 sqliteVdbeAddOp(v, OP_ColumnName, i, 0);
287 sqliteVdbeChangeP3(v, -1, zName, strlen(zName));
drh82c3d632000-06-06 21:56:07 +0000288 }
289 }
290}
291
292/*
drhd8bc7082000-06-07 23:51:50 +0000293** Name of the connection operator, used for error messages.
294*/
295static const char *selectOpName(int id){
296 char *z;
297 switch( id ){
298 case TK_ALL: z = "UNION ALL"; break;
299 case TK_INTERSECT: z = "INTERSECT"; break;
300 case TK_EXCEPT: z = "EXCEPT"; break;
301 default: z = "UNION"; break;
302 }
303 return z;
304}
305
306/*
307** For the given SELECT statement, do two things.
308**
drh967e8b72000-06-21 13:59:10 +0000309** (1) Fill in the pTabList->a[].pTab fields in the IdList that
310** defines the set of tables that should be scanned.
drhd8bc7082000-06-07 23:51:50 +0000311**
312** (2) If the columns to be extracted variable (pEList) is NULL
313** (meaning that a "*" was used in the SQL statement) then
314** create a fake pEList containing the names of all columns
315** of all tables.
316**
317** Return 0 on success. If there are problems, leave an error message
318** in pParse and return non-zero.
319*/
320static int fillInColumnList(Parse *pParse, Select *p){
321 int i, j;
drhdaffd0e2001-04-11 14:28:42 +0000322 IdList *pTabList;
323 ExprList *pEList;
324
325 if( p==0 || p->pSrc==0 ) return 1;
326 pTabList = p->pSrc;
327 pEList = p->pEList;
drhd8bc7082000-06-07 23:51:50 +0000328
329 /* Look up every table in the table list.
330 */
331 for(i=0; i<pTabList->nId; i++){
332 if( pTabList->a[i].pTab ){
333 /* This routine has run before! No need to continue */
334 return 0;
335 }
drhdaffd0e2001-04-11 14:28:42 +0000336 if( pTabList->a[i].zName==0 ){
337 /* No table name is given. Instead, there is a (SELECT ...) statement
338 ** the results of which should be used in place of the table. The
339 ** was this is implemented is that the (SELECT ...) writes its results
340 ** into a temporary table which is then scanned like any other table.
341 */
342 sqliteSetString(&pParse->zErrMsg,
343 "(SELECT...) in a FROM clause is not yet implemented.", 0);
344 pParse->nErr++;
345 return 1;
346 }
drhd8bc7082000-06-07 23:51:50 +0000347 pTabList->a[i].pTab = sqliteFindTable(pParse->db, pTabList->a[i].zName);
348 if( pTabList->a[i].pTab==0 ){
349 sqliteSetString(&pParse->zErrMsg, "no such table: ",
350 pTabList->a[i].zName, 0);
351 pParse->nErr++;
352 return 1;
353 }
354 }
355
356 /* If the list of columns to retrieve is "*" then replace it with
357 ** a list of all columns from all tables.
358 */
359 if( pEList==0 ){
360 for(i=0; i<pTabList->nId; i++){
361 Table *pTab = pTabList->a[i].pTab;
362 for(j=0; j<pTab->nCol; j++){
363 Expr *pExpr = sqliteExpr(TK_DOT, 0, 0, 0);
drhdaffd0e2001-04-11 14:28:42 +0000364 if( pExpr==0 ) break;
drhd8bc7082000-06-07 23:51:50 +0000365 pExpr->pLeft = sqliteExpr(TK_ID, 0, 0, 0);
drh6d4abfb2001-10-22 02:58:08 +0000366 if( pExpr->pLeft==0 ){ sqliteExprDelete(pExpr); break; }
drh17e24df2001-11-06 14:10:41 +0000367 if( pTabList->a[i].zAlias && pTabList->a[i].zAlias[0] ){
368 pExpr->pLeft->token.z = pTabList->a[i].zAlias;
369 pExpr->pLeft->token.n = strlen(pTabList->a[i].zAlias);
370 }else{
371 pExpr->pLeft->token.z = pTab->zName;
372 pExpr->pLeft->token.n = strlen(pTab->zName);
373 }
drhd8bc7082000-06-07 23:51:50 +0000374 pExpr->pRight = sqliteExpr(TK_ID, 0, 0, 0);
drh6d4abfb2001-10-22 02:58:08 +0000375 if( pExpr->pRight==0 ){ sqliteExprDelete(pExpr); break; }
drhd8bc7082000-06-07 23:51:50 +0000376 pExpr->pRight->token.z = pTab->aCol[j].zName;
377 pExpr->pRight->token.n = strlen(pTab->aCol[j].zName);
drhe1b6a5b2000-07-29 13:06:59 +0000378 pExpr->span.z = "";
379 pExpr->span.n = 0;
drhd8bc7082000-06-07 23:51:50 +0000380 pEList = sqliteExprListAppend(pEList, pExpr, 0);
381 }
382 }
383 p->pEList = pEList;
384 }
385 return 0;
386}
387
388/*
389** This routine associates entries in an ORDER BY expression list with
390** columns in a result. For each ORDER BY expression, the opcode of
drh967e8b72000-06-21 13:59:10 +0000391** the top-level node is changed to TK_COLUMN and the iColumn value of
drhd8bc7082000-06-07 23:51:50 +0000392** the top-level node is filled in with column number and the iTable
393** value of the top-level node is filled with iTable parameter.
394**
395** If there are prior SELECT clauses, they are processed first. A match
396** in an earlier SELECT takes precedence over a later SELECT.
397**
398** Any entry that does not match is flagged as an error. The number
399** of errors is returned.
400*/
401static int matchOrderbyToColumn(
402 Parse *pParse, /* A place to leave error messages */
403 Select *pSelect, /* Match to result columns of this SELECT */
404 ExprList *pOrderBy, /* The ORDER BY values to match against columns */
405 int iTable, /* Insert this this value in iTable */
406 int mustComplete /* If TRUE all ORDER BYs must match */
407){
408 int nErr = 0;
409 int i, j;
410 ExprList *pEList;
411
drhdaffd0e2001-04-11 14:28:42 +0000412 if( pSelect==0 || pOrderBy==0 ) return 1;
drhd8bc7082000-06-07 23:51:50 +0000413 if( mustComplete ){
414 for(i=0; i<pOrderBy->nExpr; i++){ pOrderBy->a[i].done = 0; }
415 }
416 if( fillInColumnList(pParse, pSelect) ){
417 return 1;
418 }
419 if( pSelect->pPrior ){
drh92cd52f2000-06-08 01:55:29 +0000420 if( matchOrderbyToColumn(pParse, pSelect->pPrior, pOrderBy, iTable, 0) ){
421 return 1;
422 }
drhd8bc7082000-06-07 23:51:50 +0000423 }
424 pEList = pSelect->pEList;
425 for(i=0; i<pOrderBy->nExpr; i++){
426 Expr *pE = pOrderBy->a[i].pExpr;
drh92cd52f2000-06-08 01:55:29 +0000427 int match = 0;
drhd8bc7082000-06-07 23:51:50 +0000428 if( pOrderBy->a[i].done ) continue;
429 for(j=0; j<pEList->nExpr; j++){
drh4cfa7932000-06-08 15:10:46 +0000430 if( pEList->a[j].zName && (pE->op==TK_ID || pE->op==TK_STRING) ){
431 char *zName = pEList->a[j].zName;
drh6e142f52000-06-08 13:36:40 +0000432 char *zLabel = sqliteStrNDup(pE->token.z, pE->token.n);
drhd8bc7082000-06-07 23:51:50 +0000433 sqliteDequote(zLabel);
434 if( sqliteStrICmp(zName, zLabel)==0 ){
435 match = 1;
436 }
drh6e142f52000-06-08 13:36:40 +0000437 sqliteFree(zLabel);
drhd8bc7082000-06-07 23:51:50 +0000438 }
drh4cfa7932000-06-08 15:10:46 +0000439 if( match==0 && sqliteExprCompare(pE, pEList->a[j].pExpr) ){
drhd8bc7082000-06-07 23:51:50 +0000440 match = 1;
441 }
442 if( match ){
drh967e8b72000-06-21 13:59:10 +0000443 pE->op = TK_COLUMN;
444 pE->iColumn = j;
drhd8bc7082000-06-07 23:51:50 +0000445 pE->iTable = iTable;
446 pOrderBy->a[i].done = 1;
447 break;
448 }
449 }
drh92cd52f2000-06-08 01:55:29 +0000450 if( !match && mustComplete ){
drhd8bc7082000-06-07 23:51:50 +0000451 char zBuf[30];
452 sprintf(zBuf,"%d",i+1);
453 sqliteSetString(&pParse->zErrMsg, "ORDER BY term number ", zBuf,
454 " does not match any result column", 0);
455 pParse->nErr++;
456 nErr++;
457 break;
458 }
459 }
460 return nErr;
461}
462
463/*
464** Get a VDBE for the given parser context. Create a new one if necessary.
465** If an error occurs, return NULL and leave a message in pParse.
466*/
467Vdbe *sqliteGetVdbe(Parse *pParse){
468 Vdbe *v = pParse->pVdbe;
469 if( v==0 ){
drh4c504392000-10-16 22:06:40 +0000470 v = pParse->pVdbe = sqliteVdbeCreate(pParse->db);
drhd8bc7082000-06-07 23:51:50 +0000471 }
drhd8bc7082000-06-07 23:51:50 +0000472 return v;
473}
474
475
476/*
drh82c3d632000-06-06 21:56:07 +0000477** This routine is called to process a query that is really the union
478** or intersection of two or more separate queries.
479*/
480static int multiSelect(Parse *pParse, Select *p, int eDest, int iParm){
drh10e5e3c2000-06-08 00:19:02 +0000481 int rc; /* Success code from a subroutine */
482 Select *pPrior; /* Another SELECT immediately to our left */
483 Vdbe *v; /* Generate code to this VDBE */
484 int base; /* Baseline value for pParse->nTab */
drh82c3d632000-06-06 21:56:07 +0000485
drhd8bc7082000-06-07 23:51:50 +0000486 /* Make sure there is no ORDER BY clause on prior SELECTs. Only the
487 ** last SELECT in the series may have an ORDER BY.
drh82c3d632000-06-06 21:56:07 +0000488 */
drhdaffd0e2001-04-11 14:28:42 +0000489 if( p==0 || p->pPrior==0 ) return 1;
drhd8bc7082000-06-07 23:51:50 +0000490 pPrior = p->pPrior;
491 if( pPrior->pOrderBy ){
492 sqliteSetString(&pParse->zErrMsg,"ORDER BY clause should come after ",
493 selectOpName(p->op), " not before", 0);
drh82c3d632000-06-06 21:56:07 +0000494 pParse->nErr++;
495 return 1;
496 }
497
drhd8bc7082000-06-07 23:51:50 +0000498 /* Make sure we have a valid query engine. If not, create a new one.
499 */
500 v = sqliteGetVdbe(pParse);
501 if( v==0 ) return 1;
502
503 /* Process the UNION or INTERSECTION
504 */
drh10e5e3c2000-06-08 00:19:02 +0000505 base = pParse->nTab;
drh82c3d632000-06-06 21:56:07 +0000506 switch( p->op ){
drhd8bc7082000-06-07 23:51:50 +0000507 case TK_ALL:
drh82c3d632000-06-06 21:56:07 +0000508 case TK_EXCEPT:
509 case TK_UNION: {
drhd8bc7082000-06-07 23:51:50 +0000510 int unionTab; /* Cursor number of the temporary table holding result */
511 int op; /* One of the SRT_ operations to apply to self */
512 int priorOp; /* The SRT_ operation to apply to prior selects */
drh82c3d632000-06-06 21:56:07 +0000513
drhd8bc7082000-06-07 23:51:50 +0000514 priorOp = p->op==TK_ALL ? SRT_Table : SRT_Union;
515 if( eDest==priorOp ){
516 /* We can reuse a temporary table generated by a SELECT to our
517 ** right. This also means we are not the right-most select and so
518 ** we cannot have an ORDER BY clause
519 */
drh82c3d632000-06-06 21:56:07 +0000520 unionTab = iParm;
drhd8bc7082000-06-07 23:51:50 +0000521 assert( p->pOrderBy==0 );
drh82c3d632000-06-06 21:56:07 +0000522 }else{
drhd8bc7082000-06-07 23:51:50 +0000523 /* We will need to create our own temporary table to hold the
524 ** intermediate results.
525 */
526 unionTab = pParse->nTab++;
527 if( p->pOrderBy
528 && matchOrderbyToColumn(pParse, p, p->pOrderBy, unionTab, 1) ){
529 return 1;
530 }
drhd8bc7082000-06-07 23:51:50 +0000531 if( p->op!=TK_ALL ){
drh99fcd712001-10-13 01:06:47 +0000532 sqliteVdbeAddOp(v, OP_OpenTemp, unionTab, 0);
533 sqliteVdbeAddOp(v, OP_KeyAsData, unionTab, 1);
drh345fda32001-01-15 22:51:08 +0000534 }else{
drh99fcd712001-10-13 01:06:47 +0000535 sqliteVdbeAddOp(v, OP_OpenTemp, unionTab, 0);
drhd8bc7082000-06-07 23:51:50 +0000536 }
drh82c3d632000-06-06 21:56:07 +0000537 }
drhd8bc7082000-06-07 23:51:50 +0000538
539 /* Code the SELECT statements to our left
540 */
541 rc = sqliteSelect(pParse, pPrior, priorOp, unionTab);
drh82c3d632000-06-06 21:56:07 +0000542 if( rc ) return rc;
drhd8bc7082000-06-07 23:51:50 +0000543
544 /* Code the current SELECT statement
545 */
546 switch( p->op ){
547 case TK_EXCEPT: op = SRT_Except; break;
548 case TK_UNION: op = SRT_Union; break;
549 case TK_ALL: op = SRT_Table; break;
550 }
drh82c3d632000-06-06 21:56:07 +0000551 p->pPrior = 0;
552 rc = sqliteSelect(pParse, p, op, unionTab);
553 p->pPrior = pPrior;
554 if( rc ) return rc;
drhd8bc7082000-06-07 23:51:50 +0000555
556 /* Convert the data in the temporary table into whatever form
557 ** it is that we currently need.
558 */
559 if( eDest!=priorOp ){
drh82c3d632000-06-06 21:56:07 +0000560 int iCont, iBreak;
561 assert( p->pEList );
drhd8bc7082000-06-07 23:51:50 +0000562 generateColumnNames(pParse, 0, p->pEList);
drh99fcd712001-10-13 01:06:47 +0000563 sqliteVdbeAddOp(v, OP_Rewind, unionTab, 0);
drh82c3d632000-06-06 21:56:07 +0000564 iBreak = sqliteVdbeMakeLabel(v);
drh99fcd712001-10-13 01:06:47 +0000565 iCont = sqliteVdbeAddOp(v, OP_Next, unionTab, iBreak);
drh82c3d632000-06-06 21:56:07 +0000566 rc = selectInnerLoop(pParse, 0, unionTab, p->pEList->nExpr,
drhd8bc7082000-06-07 23:51:50 +0000567 p->pOrderBy, -1, eDest, iParm,
drh82c3d632000-06-06 21:56:07 +0000568 iCont, iBreak);
569 if( rc ) return 1;
drh99fcd712001-10-13 01:06:47 +0000570 sqliteVdbeAddOp(v, OP_Goto, 0, iCont);
571 sqliteVdbeResolveLabel(v, iBreak);
572 sqliteVdbeAddOp(v, OP_Close, unionTab, 0);
drhd8bc7082000-06-07 23:51:50 +0000573 if( p->pOrderBy ){
574 generateSortTail(v, p->pEList->nExpr);
575 }
drh82c3d632000-06-06 21:56:07 +0000576 }
577 break;
578 }
579 case TK_INTERSECT: {
580 int tab1, tab2;
drh82c3d632000-06-06 21:56:07 +0000581 int iCont, iBreak;
582
drhd8bc7082000-06-07 23:51:50 +0000583 /* INTERSECT is different from the others since it requires
drh6206d502000-06-19 19:09:08 +0000584 ** two temporary tables. Hence it has its own case. Begin
drhd8bc7082000-06-07 23:51:50 +0000585 ** by allocating the tables we will need.
586 */
drh82c3d632000-06-06 21:56:07 +0000587 tab1 = pParse->nTab++;
588 tab2 = pParse->nTab++;
drhd8bc7082000-06-07 23:51:50 +0000589 if( p->pOrderBy && matchOrderbyToColumn(pParse,p,p->pOrderBy,tab1,1) ){
590 return 1;
591 }
drh99fcd712001-10-13 01:06:47 +0000592 sqliteVdbeAddOp(v, OP_OpenTemp, tab1, 0);
593 sqliteVdbeAddOp(v, OP_KeyAsData, tab1, 1);
drhd8bc7082000-06-07 23:51:50 +0000594
595 /* Code the SELECTs to our left into temporary table "tab1".
596 */
drh82c3d632000-06-06 21:56:07 +0000597 rc = sqliteSelect(pParse, pPrior, SRT_Union, tab1);
598 if( rc ) return rc;
drhd8bc7082000-06-07 23:51:50 +0000599
600 /* Code the current SELECT into temporary table "tab2"
601 */
drh99fcd712001-10-13 01:06:47 +0000602 sqliteVdbeAddOp(v, OP_OpenTemp, tab2, 0);
603 sqliteVdbeAddOp(v, OP_KeyAsData, tab2, 1);
drh82c3d632000-06-06 21:56:07 +0000604 p->pPrior = 0;
605 rc = sqliteSelect(pParse, p, SRT_Union, tab2);
606 p->pPrior = pPrior;
607 if( rc ) return rc;
drhd8bc7082000-06-07 23:51:50 +0000608
609 /* Generate code to take the intersection of the two temporary
610 ** tables.
611 */
drh82c3d632000-06-06 21:56:07 +0000612 assert( p->pEList );
drhd8bc7082000-06-07 23:51:50 +0000613 generateColumnNames(pParse, 0, p->pEList);
drh99fcd712001-10-13 01:06:47 +0000614 sqliteVdbeAddOp(v, OP_Rewind, tab1, 0);
drh82c3d632000-06-06 21:56:07 +0000615 iBreak = sqliteVdbeMakeLabel(v);
drh99fcd712001-10-13 01:06:47 +0000616 iCont = sqliteVdbeAddOp(v, OP_Next, tab1, iBreak);
617 sqliteVdbeAddOp(v, OP_FullKey, tab1, 0);
618 sqliteVdbeAddOp(v, OP_NotFound, tab2, iCont);
drh82c3d632000-06-06 21:56:07 +0000619 rc = selectInnerLoop(pParse, 0, tab1, p->pEList->nExpr,
drhd8bc7082000-06-07 23:51:50 +0000620 p->pOrderBy, -1, eDest, iParm,
drh82c3d632000-06-06 21:56:07 +0000621 iCont, iBreak);
622 if( rc ) return 1;
drh99fcd712001-10-13 01:06:47 +0000623 sqliteVdbeAddOp(v, OP_Goto, 0, iCont);
624 sqliteVdbeResolveLabel(v, iBreak);
625 sqliteVdbeAddOp(v, OP_Close, tab2, 0);
626 sqliteVdbeAddOp(v, OP_Close, tab1, 0);
drhd8bc7082000-06-07 23:51:50 +0000627 if( p->pOrderBy ){
628 generateSortTail(v, p->pEList->nExpr);
629 }
drh82c3d632000-06-06 21:56:07 +0000630 break;
631 }
632 }
633 assert( p->pEList && pPrior->pEList );
634 if( p->pEList->nExpr!=pPrior->pEList->nExpr ){
drhd8bc7082000-06-07 23:51:50 +0000635 sqliteSetString(&pParse->zErrMsg, "SELECTs to the left and right of ",
636 selectOpName(p->op), " do not have the same number of result columns", 0);
drh82c3d632000-06-06 21:56:07 +0000637 pParse->nErr++;
638 return 1;
drh22827922000-06-06 17:27:05 +0000639 }
drh10e5e3c2000-06-08 00:19:02 +0000640 pParse->nTab = base;
drh22827922000-06-06 17:27:05 +0000641 return 0;
642}
643
644/*
drh9bb61fe2000-06-05 16:01:39 +0000645** Generate code for the given SELECT statement.
646**
drhfef52082000-06-06 01:50:43 +0000647** The results are distributed in various ways depending on the
648** value of eDest and iParm.
649**
650** eDest Value Result
651** ------------ -------------------------------------------
652** SRT_Callback Invoke the callback for each row of the result.
653**
654** SRT_Mem Store first result in memory cell iParm
655**
656** SRT_Set Store results as keys of a table with cursor iParm
657**
drh82c3d632000-06-06 21:56:07 +0000658** SRT_Union Store results as a key in a temporary table iParm
659**
drhc4a3c772001-04-04 11:48:57 +0000660** SRT_Except Remove results form the temporary table iParm.
661**
662** SRT_Table Store results in temporary table iParm
drh9bb61fe2000-06-05 16:01:39 +0000663**
664** This routine returns the number of errors. If any errors are
665** encountered, then an appropriate error message is left in
666** pParse->zErrMsg.
667**
668** This routine does NOT free the Select structure passed in. The
669** calling function needs to do that.
670*/
671int sqliteSelect(
drhcce7d172000-05-31 15:34:51 +0000672 Parse *pParse, /* The parser context */
drh9bb61fe2000-06-05 16:01:39 +0000673 Select *p, /* The SELECT statement being coded. */
drh82c3d632000-06-06 21:56:07 +0000674 int eDest, /* One of: SRT_Callback Mem Set Union Except */
drhfef52082000-06-06 01:50:43 +0000675 int iParm /* Save result in this memory location, if >=0 */
drhcce7d172000-05-31 15:34:51 +0000676){
drhd8bc7082000-06-07 23:51:50 +0000677 int i;
drhcce7d172000-05-31 15:34:51 +0000678 WhereInfo *pWInfo;
679 Vdbe *v;
680 int isAgg = 0; /* True for select lists like "count(*)" */
drh967e8b72000-06-21 13:59:10 +0000681 ExprList *pEList; /* List of columns to extract. NULL means "*" */
drh9bb61fe2000-06-05 16:01:39 +0000682 IdList *pTabList; /* List of tables to select from */
683 Expr *pWhere; /* The WHERE clause. May be NULL */
684 ExprList *pOrderBy; /* The ORDER BY clause. May be NULL */
drh22827922000-06-06 17:27:05 +0000685 ExprList *pGroupBy; /* The GROUP BY clause. May be NULL */
686 Expr *pHaving; /* The HAVING clause. May be NULL */
drh19a775c2000-06-05 18:54:46 +0000687 int isDistinct; /* True if the DISTINCT keyword is present */
688 int distinct; /* Table to use for the distinct set */
drh10e5e3c2000-06-08 00:19:02 +0000689 int base; /* First cursor available for use */
drh9bb61fe2000-06-05 16:01:39 +0000690
drhdaffd0e2001-04-11 14:28:42 +0000691 if( sqlite_malloc_failed || pParse->nErr || p==0 ) return 1;
692
drh82c3d632000-06-06 21:56:07 +0000693 /* If there is are a sequence of queries, do the earlier ones first.
694 */
695 if( p->pPrior ){
696 return multiSelect(pParse, p, eDest, iParm);
697 }
698
699 /* Make local copies of the parameters for this query.
700 */
drh9bb61fe2000-06-05 16:01:39 +0000701 pTabList = p->pSrc;
702 pWhere = p->pWhere;
703 pOrderBy = p->pOrderBy;
drh22827922000-06-06 17:27:05 +0000704 pGroupBy = p->pGroupBy;
705 pHaving = p->pHaving;
drh19a775c2000-06-05 18:54:46 +0000706 isDistinct = p->isDistinct;
drh9bb61fe2000-06-05 16:01:39 +0000707
drh10e5e3c2000-06-08 00:19:02 +0000708 /* Save the current value of pParse->nTab. Restore this value before
709 ** we exit.
710 */
711 base = pParse->nTab;
712
drh9bb61fe2000-06-05 16:01:39 +0000713 /*
714 ** Do not even attempt to generate any code if we have already seen
715 ** errors before this routine starts.
716 */
drh10e5e3c2000-06-08 00:19:02 +0000717 if( pParse->nErr>0 ) return 1;
drh22827922000-06-06 17:27:05 +0000718 sqliteParseInfoReset(pParse);
drhcce7d172000-05-31 15:34:51 +0000719
drhd8bc7082000-06-07 23:51:50 +0000720 /* Look up every table in the table list and create an appropriate
721 ** columnlist in pEList if there isn't one already. (The parser leaves
drh967e8b72000-06-21 13:59:10 +0000722 ** a NULL in the p->pEList if the SQL said "SELECT * FROM ...")
drhcce7d172000-05-31 15:34:51 +0000723 */
drhd8bc7082000-06-07 23:51:50 +0000724 if( fillInColumnList(pParse, p) ){
725 return 1;
drhcce7d172000-05-31 15:34:51 +0000726 }
drhd8bc7082000-06-07 23:51:50 +0000727 pEList = p->pEList;
drhdaffd0e2001-04-11 14:28:42 +0000728 if( pEList==0 ) return 1;
drhcce7d172000-05-31 15:34:51 +0000729
drh19a775c2000-06-05 18:54:46 +0000730 /* Allocate a temporary table to use for the DISTINCT set, if
drh22827922000-06-06 17:27:05 +0000731 ** necessary. This must be done early to allocate the cursor before
732 ** any calls to sqliteExprResolveIds().
drh19a775c2000-06-05 18:54:46 +0000733 */
734 if( isDistinct ){
735 distinct = pParse->nTab++;
drh22827922000-06-06 17:27:05 +0000736 }else{
737 distinct = -1;
drh19a775c2000-06-05 18:54:46 +0000738 }
739
drh22827922000-06-06 17:27:05 +0000740 /* If writing to memory or generating a set
741 ** only a single column may be output.
drh19a775c2000-06-05 18:54:46 +0000742 */
drhfef52082000-06-06 01:50:43 +0000743 if( (eDest==SRT_Mem || eDest==SRT_Set) && pEList->nExpr>1 ){
drh19a775c2000-06-05 18:54:46 +0000744 sqliteSetString(&pParse->zErrMsg, "only a single result allowed for "
745 "a SELECT that is part of an expression", 0);
746 pParse->nErr++;
747 return 1;
748 }
749
drh22827922000-06-06 17:27:05 +0000750 /* ORDER BY is ignored if we are not sending the result to a callback.
751 */
752 if( eDest!=SRT_Callback ){
753 pOrderBy = 0;
754 }
755
756 /* Allocate cursors for "expr IN (SELECT ...)" constructs.
drhcce7d172000-05-31 15:34:51 +0000757 */
758 for(i=0; i<pEList->nExpr; i++){
drh4794b982000-06-06 13:54:14 +0000759 sqliteExprResolveInSelect(pParse, pEList->a[i].pExpr);
760 }
761 if( pWhere ) sqliteExprResolveInSelect(pParse, pWhere);
762 if( pOrderBy ){
763 for(i=0; i<pOrderBy->nExpr; i++){
764 sqliteExprResolveInSelect(pParse, pOrderBy->a[i].pExpr);
765 }
766 }
drh22827922000-06-06 17:27:05 +0000767 if( pGroupBy ){
768 for(i=0; i<pGroupBy->nExpr; i++){
769 sqliteExprResolveInSelect(pParse, pGroupBy->a[i].pExpr);
770 }
771 }
772 if( pHaving ) sqliteExprResolveInSelect(pParse, pHaving);
773
drh10e5e3c2000-06-08 00:19:02 +0000774 /* At this point, we should have allocated all the cursors that we
775 ** need to handle subquerys and temporary tables. From here on we
776 ** are committed to keeping the same value for pParse->nTab.
777 **
drh967e8b72000-06-21 13:59:10 +0000778 ** Resolve the column names and do a semantics check on all the expressions.
drh22827922000-06-06 17:27:05 +0000779 */
drh4794b982000-06-06 13:54:14 +0000780 for(i=0; i<pEList->nExpr; i++){
drhcce7d172000-05-31 15:34:51 +0000781 if( sqliteExprResolveIds(pParse, pTabList, pEList->a[i].pExpr) ){
drh9bb61fe2000-06-05 16:01:39 +0000782 return 1;
drhcce7d172000-05-31 15:34:51 +0000783 }
drh22827922000-06-06 17:27:05 +0000784 if( sqliteExprCheck(pParse, pEList->a[i].pExpr, 1, &isAgg) ){
drh9bb61fe2000-06-05 16:01:39 +0000785 return 1;
drhcce7d172000-05-31 15:34:51 +0000786 }
787 }
drhcce7d172000-05-31 15:34:51 +0000788 if( pWhere ){
789 if( sqliteExprResolveIds(pParse, pTabList, pWhere) ){
drh9bb61fe2000-06-05 16:01:39 +0000790 return 1;
drhcce7d172000-05-31 15:34:51 +0000791 }
792 if( sqliteExprCheck(pParse, pWhere, 0, 0) ){
drh9bb61fe2000-06-05 16:01:39 +0000793 return 1;
drhcce7d172000-05-31 15:34:51 +0000794 }
795 }
796 if( pOrderBy ){
797 for(i=0; i<pOrderBy->nExpr; i++){
drh22827922000-06-06 17:27:05 +0000798 Expr *pE = pOrderBy->a[i].pExpr;
799 if( sqliteExprResolveIds(pParse, pTabList, pE) ){
drh9bb61fe2000-06-05 16:01:39 +0000800 return 1;
drhcce7d172000-05-31 15:34:51 +0000801 }
drh22827922000-06-06 17:27:05 +0000802 if( sqliteExprCheck(pParse, pE, isAgg, 0) ){
drh9bb61fe2000-06-05 16:01:39 +0000803 return 1;
drhcce7d172000-05-31 15:34:51 +0000804 }
805 }
806 }
drh22827922000-06-06 17:27:05 +0000807 if( pGroupBy ){
808 for(i=0; i<pGroupBy->nExpr; i++){
809 Expr *pE = pGroupBy->a[i].pExpr;
810 if( sqliteExprResolveIds(pParse, pTabList, pE) ){
811 return 1;
812 }
813 if( sqliteExprCheck(pParse, pE, isAgg, 0) ){
814 return 1;
815 }
816 }
817 }
818 if( pHaving ){
819 if( pGroupBy==0 ){
drhda932812000-06-06 18:00:15 +0000820 sqliteSetString(&pParse->zErrMsg, "a GROUP BY clause is required "
821 "before HAVING", 0);
drh22827922000-06-06 17:27:05 +0000822 pParse->nErr++;
823 return 1;
824 }
825 if( sqliteExprResolveIds(pParse, pTabList, pHaving) ){
826 return 1;
827 }
drhda932812000-06-06 18:00:15 +0000828 if( sqliteExprCheck(pParse, pHaving, isAgg, 0) ){
drh22827922000-06-06 17:27:05 +0000829 return 1;
830 }
drhcce7d172000-05-31 15:34:51 +0000831 }
832
drh22827922000-06-06 17:27:05 +0000833 /* Do an analysis of aggregate expressions.
drhefb72512000-05-31 20:00:52 +0000834 */
drh22827922000-06-06 17:27:05 +0000835 if( isAgg ){
drhaaf88722000-06-08 11:25:00 +0000836 assert( pParse->nAgg==0 && pParse->iAggCount<0 );
drh22827922000-06-06 17:27:05 +0000837 for(i=0; i<pEList->nExpr; i++){
838 if( sqliteExprAnalyzeAggregates(pParse, pEList->a[i].pExpr) ){
839 return 1;
840 }
841 }
842 if( pGroupBy ){
843 for(i=0; i<pGroupBy->nExpr; i++){
844 if( sqliteExprAnalyzeAggregates(pParse, pGroupBy->a[i].pExpr) ){
845 return 1;
846 }
847 }
848 }
849 if( pHaving && sqliteExprAnalyzeAggregates(pParse, pHaving) ){
850 return 1;
851 }
drh191b6902000-06-08 11:13:01 +0000852 if( pOrderBy ){
853 for(i=0; i<pOrderBy->nExpr; i++){
854 if( sqliteExprAnalyzeAggregates(pParse, pOrderBy->a[i].pExpr) ){
855 return 1;
856 }
857 }
858 }
drhefb72512000-05-31 20:00:52 +0000859 }
860
drhcce7d172000-05-31 15:34:51 +0000861 /* Begin generating code.
862 */
drhdaffd0e2001-04-11 14:28:42 +0000863 v = sqliteGetVdbe(pParse);
864 if( v==0 ) return 1;
drhcce7d172000-05-31 15:34:51 +0000865
drh9bbca4c2001-11-06 04:00:18 +0000866 /* Set the limiter
867 */
868 if( p->nLimit<=0 ){
869 p->nOffset = 0;
870 }else{
871 if( p->nOffset<0 ) p->nOffset = 0;
872 sqliteVdbeAddOp(v, OP_Limit, p->nLimit, p->nOffset);
873 }
874
875
drh22827922000-06-06 17:27:05 +0000876 /* Identify column names if we will be using in the callback. This
drh19a775c2000-06-05 18:54:46 +0000877 ** step is skipped if the output is going to a table or a memory cell.
drhcce7d172000-05-31 15:34:51 +0000878 */
drhfef52082000-06-06 01:50:43 +0000879 if( eDest==SRT_Callback ){
drhd8bc7082000-06-07 23:51:50 +0000880 generateColumnNames(pParse, pTabList, pEList);
drhcce7d172000-05-31 15:34:51 +0000881 }
882
drh22827922000-06-06 17:27:05 +0000883 /* Reset the aggregator
drhcce7d172000-05-31 15:34:51 +0000884 */
885 if( isAgg ){
drh99fcd712001-10-13 01:06:47 +0000886 sqliteVdbeAddOp(v, OP_AggReset, 0, pParse->nAgg);
drh1bee3d72001-10-15 00:44:35 +0000887 if( pGroupBy==0 ){
888 sqliteVdbeAddOp(v, OP_String, 0, 0);
889 sqliteVdbeChangeP3(v, -1, "", P3_STATIC);
890 sqliteVdbeAddOp(v, OP_AggFocus, 0, 0);
891 for(i=0; i<pParse->nAgg; i++){
892 Expr *pE;
893 if( !pParse->aAgg[i].isAgg ) continue;
894 pE = pParse->aAgg[i].pExpr;
895 assert( pE==0 || pE->op==TK_AGG_FUNCTION );
896 assert( pE==0 || (pE->pList!=0 && pE->pList->nExpr==1) );
897 if( pE==0 || pE->iColumn==FN_Sum ){
898 sqliteVdbeAddOp(v, OP_Integer, 0, 0);
899 sqliteVdbeAddOp(v, OP_AggSet, 0, i);
900 continue;
901 }
902 }
903 }
drhcce7d172000-05-31 15:34:51 +0000904 }
905
drh19a775c2000-06-05 18:54:46 +0000906 /* Initialize the memory cell to NULL
907 */
drhfef52082000-06-06 01:50:43 +0000908 if( eDest==SRT_Mem ){
drh99fcd712001-10-13 01:06:47 +0000909 sqliteVdbeAddOp(v, OP_String, 0, 0);
drh8721ce42001-11-07 14:22:00 +0000910 sqliteVdbeAddOp(v, OP_MemStore, iParm, 1);
drh19a775c2000-06-05 18:54:46 +0000911 }
912
drhcce7d172000-05-31 15:34:51 +0000913 /* Begin the database scan
drhefb72512000-05-31 20:00:52 +0000914 */
drh19a775c2000-06-05 18:54:46 +0000915 if( isDistinct ){
drh99fcd712001-10-13 01:06:47 +0000916 sqliteVdbeAddOp(v, OP_OpenTemp, distinct, 0);
drhefb72512000-05-31 20:00:52 +0000917 }
drhcce7d172000-05-31 15:34:51 +0000918 pWInfo = sqliteWhereBegin(pParse, pTabList, pWhere, 0);
drh9bb61fe2000-06-05 16:01:39 +0000919 if( pWInfo==0 ) return 1;
drhcce7d172000-05-31 15:34:51 +0000920
drh22827922000-06-06 17:27:05 +0000921 /* Use the standard inner loop if we are not dealing with
922 ** aggregates
drhcce7d172000-05-31 15:34:51 +0000923 */
drhda9d6c42000-05-31 18:20:14 +0000924 if( !isAgg ){
drh82c3d632000-06-06 21:56:07 +0000925 if( selectInnerLoop(pParse, pEList, 0, 0, pOrderBy, distinct, eDest, iParm,
drh22827922000-06-06 17:27:05 +0000926 pWInfo->iContinue, pWInfo->iBreak) ){
927 return 1;
drhda9d6c42000-05-31 18:20:14 +0000928 }
drhcce7d172000-05-31 15:34:51 +0000929 }
drhefb72512000-05-31 20:00:52 +0000930
drh22827922000-06-06 17:27:05 +0000931 /* If we are dealing with aggregates, then to the special aggregate
932 ** processing.
drhefb72512000-05-31 20:00:52 +0000933 */
drh22827922000-06-06 17:27:05 +0000934 else{
drh22827922000-06-06 17:27:05 +0000935 if( pGroupBy ){
drh1bee3d72001-10-15 00:44:35 +0000936 int lbl1;
drh22827922000-06-06 17:27:05 +0000937 for(i=0; i<pGroupBy->nExpr; i++){
938 sqliteExprCode(pParse, pGroupBy->a[i].pExpr);
939 }
drh99fcd712001-10-13 01:06:47 +0000940 sqliteVdbeAddOp(v, OP_MakeKey, pGroupBy->nExpr, 0);
drh1bee3d72001-10-15 00:44:35 +0000941 lbl1 = sqliteVdbeMakeLabel(v);
drh99fcd712001-10-13 01:06:47 +0000942 sqliteVdbeAddOp(v, OP_AggFocus, 0, lbl1);
drh22827922000-06-06 17:27:05 +0000943 for(i=0; i<pParse->nAgg; i++){
944 if( pParse->aAgg[i].isAgg ) continue;
945 sqliteExprCode(pParse, pParse->aAgg[i].pExpr);
drh99fcd712001-10-13 01:06:47 +0000946 sqliteVdbeAddOp(v, OP_AggSet, 0, i);
drhcce7d172000-05-31 15:34:51 +0000947 }
drh22827922000-06-06 17:27:05 +0000948 sqliteVdbeResolveLabel(v, lbl1);
drhcce7d172000-05-31 15:34:51 +0000949 }
drh22827922000-06-06 17:27:05 +0000950 for(i=0; i<pParse->nAgg; i++){
951 Expr *pE;
952 int op;
953 if( !pParse->aAgg[i].isAgg ) continue;
954 pE = pParse->aAgg[i].pExpr;
955 if( pE==0 ){
drh99fcd712001-10-13 01:06:47 +0000956 sqliteVdbeAddOp(v, OP_AggIncr, 1, i);
drh22827922000-06-06 17:27:05 +0000957 continue;
958 }
959 assert( pE->op==TK_AGG_FUNCTION );
960 assert( pE->pList!=0 && pE->pList->nExpr==1 );
961 sqliteExprCode(pParse, pE->pList->a[0].pExpr);
drh99fcd712001-10-13 01:06:47 +0000962 sqliteVdbeAddOp(v, OP_AggGet, 0, i);
drh967e8b72000-06-21 13:59:10 +0000963 switch( pE->iColumn ){
drh22827922000-06-06 17:27:05 +0000964 case FN_Min: op = OP_Min; break;
965 case FN_Max: op = OP_Max; break;
966 case FN_Avg: op = OP_Add; break;
967 case FN_Sum: op = OP_Add; break;
968 }
drh99fcd712001-10-13 01:06:47 +0000969 sqliteVdbeAddOp(v, op, 0, 0);
970 sqliteVdbeAddOp(v, OP_AggSet, 0, i);
drh22827922000-06-06 17:27:05 +0000971 }
drhcce7d172000-05-31 15:34:51 +0000972 }
973
drh22827922000-06-06 17:27:05 +0000974
drhcce7d172000-05-31 15:34:51 +0000975 /* End the database scan loop.
976 */
977 sqliteWhereEnd(pWInfo);
978
drh22827922000-06-06 17:27:05 +0000979 /* If we are processing aggregates, we need to set up a second loop
980 ** over all of the aggregate values and process them.
981 */
982 if( isAgg ){
983 int endagg = sqliteVdbeMakeLabel(v);
984 int startagg;
drh99fcd712001-10-13 01:06:47 +0000985 startagg = sqliteVdbeAddOp(v, OP_AggNext, 0, endagg);
drh22827922000-06-06 17:27:05 +0000986 pParse->useAgg = 1;
987 if( pHaving ){
988 sqliteExprIfFalse(pParse, pHaving, startagg);
989 }
drh82c3d632000-06-06 21:56:07 +0000990 if( selectInnerLoop(pParse, pEList, 0, 0, pOrderBy, distinct, eDest, iParm,
drh22827922000-06-06 17:27:05 +0000991 startagg, endagg) ){
992 return 1;
993 }
drh99fcd712001-10-13 01:06:47 +0000994 sqliteVdbeAddOp(v, OP_Goto, 0, startagg);
995 sqliteVdbeResolveLabel(v, endagg);
996 sqliteVdbeAddOp(v, OP_Noop, 0, 0);
drh22827922000-06-06 17:27:05 +0000997 pParse->useAgg = 0;
998 }
999
drhcce7d172000-05-31 15:34:51 +00001000 /* If there is an ORDER BY clause, then we need to sort the results
1001 ** and send them to the callback one by one.
1002 */
1003 if( pOrderBy ){
drhd8bc7082000-06-07 23:51:50 +00001004 generateSortTail(v, pEList->nExpr);
drhcce7d172000-05-31 15:34:51 +00001005 }
drh10e5e3c2000-06-08 00:19:02 +00001006 pParse->nTab = base;
drh6a535342001-10-19 16:44:56 +00001007
1008
1009 /* Issue a null callback if that is what the user wants.
1010 */
1011 if( (pParse->db->flags & SQLITE_NullCallback)!=0 && eDest==SRT_Callback ){
1012 sqliteVdbeAddOp(v, OP_NullCallback, pEList->nExpr, 0);
1013 }
1014
drh9bb61fe2000-06-05 16:01:39 +00001015 return 0;
drhcce7d172000-05-31 15:34:51 +00001016}