blob: b30172cb0ac9ca21b4cc81192f2bf1a463efb66d [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**
drh6b563442001-11-07 16:48:26 +000015** $Id: select.c,v 1.49 2001/11/07 16:48:27 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 ){
drh6b563442001-11-07 16:48:26 +0000560 int iCont, iBreak, iStart;
drh82c3d632000-06-06 21:56:07 +0000561 assert( p->pEList );
drhd8bc7082000-06-07 23:51:50 +0000562 generateColumnNames(pParse, 0, p->pEList);
drh82c3d632000-06-06 21:56:07 +0000563 iBreak = sqliteVdbeMakeLabel(v);
drh6b563442001-11-07 16:48:26 +0000564 iCont = sqliteVdbeMakeLabel(v);
565 sqliteVdbeAddOp(v, OP_Rewind, unionTab, iBreak);
566 iStart = sqliteVdbeCurrentAddr(v);
drh82c3d632000-06-06 21:56:07 +0000567 rc = selectInnerLoop(pParse, 0, unionTab, p->pEList->nExpr,
drhd8bc7082000-06-07 23:51:50 +0000568 p->pOrderBy, -1, eDest, iParm,
drh82c3d632000-06-06 21:56:07 +0000569 iCont, iBreak);
570 if( rc ) return 1;
drh6b563442001-11-07 16:48:26 +0000571 sqliteVdbeResolveLabel(v, iCont);
572 sqliteVdbeAddOp(v, OP_Next, unionTab, iStart);
drh99fcd712001-10-13 01:06:47 +0000573 sqliteVdbeResolveLabel(v, iBreak);
574 sqliteVdbeAddOp(v, OP_Close, unionTab, 0);
drhd8bc7082000-06-07 23:51:50 +0000575 if( p->pOrderBy ){
576 generateSortTail(v, p->pEList->nExpr);
577 }
drh82c3d632000-06-06 21:56:07 +0000578 }
579 break;
580 }
581 case TK_INTERSECT: {
582 int tab1, tab2;
drh6b563442001-11-07 16:48:26 +0000583 int iCont, iBreak, iStart;
drh82c3d632000-06-06 21:56:07 +0000584
drhd8bc7082000-06-07 23:51:50 +0000585 /* INTERSECT is different from the others since it requires
drh6206d502000-06-19 19:09:08 +0000586 ** two temporary tables. Hence it has its own case. Begin
drhd8bc7082000-06-07 23:51:50 +0000587 ** by allocating the tables we will need.
588 */
drh82c3d632000-06-06 21:56:07 +0000589 tab1 = pParse->nTab++;
590 tab2 = pParse->nTab++;
drhd8bc7082000-06-07 23:51:50 +0000591 if( p->pOrderBy && matchOrderbyToColumn(pParse,p,p->pOrderBy,tab1,1) ){
592 return 1;
593 }
drh99fcd712001-10-13 01:06:47 +0000594 sqliteVdbeAddOp(v, OP_OpenTemp, tab1, 0);
595 sqliteVdbeAddOp(v, OP_KeyAsData, tab1, 1);
drhd8bc7082000-06-07 23:51:50 +0000596
597 /* Code the SELECTs to our left into temporary table "tab1".
598 */
drh82c3d632000-06-06 21:56:07 +0000599 rc = sqliteSelect(pParse, pPrior, SRT_Union, tab1);
600 if( rc ) return rc;
drhd8bc7082000-06-07 23:51:50 +0000601
602 /* Code the current SELECT into temporary table "tab2"
603 */
drh99fcd712001-10-13 01:06:47 +0000604 sqliteVdbeAddOp(v, OP_OpenTemp, tab2, 0);
605 sqliteVdbeAddOp(v, OP_KeyAsData, tab2, 1);
drh82c3d632000-06-06 21:56:07 +0000606 p->pPrior = 0;
607 rc = sqliteSelect(pParse, p, SRT_Union, tab2);
608 p->pPrior = pPrior;
609 if( rc ) return rc;
drhd8bc7082000-06-07 23:51:50 +0000610
611 /* Generate code to take the intersection of the two temporary
612 ** tables.
613 */
drh82c3d632000-06-06 21:56:07 +0000614 assert( p->pEList );
drhd8bc7082000-06-07 23:51:50 +0000615 generateColumnNames(pParse, 0, p->pEList);
drh82c3d632000-06-06 21:56:07 +0000616 iBreak = sqliteVdbeMakeLabel(v);
drh6b563442001-11-07 16:48:26 +0000617 iCont = sqliteVdbeMakeLabel(v);
618 sqliteVdbeAddOp(v, OP_Rewind, tab1, iBreak);
619 iStart = sqliteVdbeAddOp(v, OP_FullKey, tab1, 0);
drh99fcd712001-10-13 01:06:47 +0000620 sqliteVdbeAddOp(v, OP_NotFound, tab2, iCont);
drh82c3d632000-06-06 21:56:07 +0000621 rc = selectInnerLoop(pParse, 0, tab1, p->pEList->nExpr,
drhd8bc7082000-06-07 23:51:50 +0000622 p->pOrderBy, -1, eDest, iParm,
drh82c3d632000-06-06 21:56:07 +0000623 iCont, iBreak);
624 if( rc ) return 1;
drh6b563442001-11-07 16:48:26 +0000625 sqliteVdbeResolveLabel(v, iCont);
626 sqliteVdbeAddOp(v, OP_Next, tab1, iStart);
drh99fcd712001-10-13 01:06:47 +0000627 sqliteVdbeResolveLabel(v, iBreak);
628 sqliteVdbeAddOp(v, OP_Close, tab2, 0);
629 sqliteVdbeAddOp(v, OP_Close, tab1, 0);
drhd8bc7082000-06-07 23:51:50 +0000630 if( p->pOrderBy ){
631 generateSortTail(v, p->pEList->nExpr);
632 }
drh82c3d632000-06-06 21:56:07 +0000633 break;
634 }
635 }
636 assert( p->pEList && pPrior->pEList );
637 if( p->pEList->nExpr!=pPrior->pEList->nExpr ){
drhd8bc7082000-06-07 23:51:50 +0000638 sqliteSetString(&pParse->zErrMsg, "SELECTs to the left and right of ",
639 selectOpName(p->op), " do not have the same number of result columns", 0);
drh82c3d632000-06-06 21:56:07 +0000640 pParse->nErr++;
641 return 1;
drh22827922000-06-06 17:27:05 +0000642 }
drh10e5e3c2000-06-08 00:19:02 +0000643 pParse->nTab = base;
drh22827922000-06-06 17:27:05 +0000644 return 0;
645}
646
647/*
drh9bb61fe2000-06-05 16:01:39 +0000648** Generate code for the given SELECT statement.
649**
drhfef52082000-06-06 01:50:43 +0000650** The results are distributed in various ways depending on the
651** value of eDest and iParm.
652**
653** eDest Value Result
654** ------------ -------------------------------------------
655** SRT_Callback Invoke the callback for each row of the result.
656**
657** SRT_Mem Store first result in memory cell iParm
658**
659** SRT_Set Store results as keys of a table with cursor iParm
660**
drh82c3d632000-06-06 21:56:07 +0000661** SRT_Union Store results as a key in a temporary table iParm
662**
drhc4a3c772001-04-04 11:48:57 +0000663** SRT_Except Remove results form the temporary table iParm.
664**
665** SRT_Table Store results in temporary table iParm
drh9bb61fe2000-06-05 16:01:39 +0000666**
667** This routine returns the number of errors. If any errors are
668** encountered, then an appropriate error message is left in
669** pParse->zErrMsg.
670**
671** This routine does NOT free the Select structure passed in. The
672** calling function needs to do that.
673*/
674int sqliteSelect(
drhcce7d172000-05-31 15:34:51 +0000675 Parse *pParse, /* The parser context */
drh9bb61fe2000-06-05 16:01:39 +0000676 Select *p, /* The SELECT statement being coded. */
drh82c3d632000-06-06 21:56:07 +0000677 int eDest, /* One of: SRT_Callback Mem Set Union Except */
drhfef52082000-06-06 01:50:43 +0000678 int iParm /* Save result in this memory location, if >=0 */
drhcce7d172000-05-31 15:34:51 +0000679){
drhd8bc7082000-06-07 23:51:50 +0000680 int i;
drhcce7d172000-05-31 15:34:51 +0000681 WhereInfo *pWInfo;
682 Vdbe *v;
683 int isAgg = 0; /* True for select lists like "count(*)" */
drh967e8b72000-06-21 13:59:10 +0000684 ExprList *pEList; /* List of columns to extract. NULL means "*" */
drh9bb61fe2000-06-05 16:01:39 +0000685 IdList *pTabList; /* List of tables to select from */
686 Expr *pWhere; /* The WHERE clause. May be NULL */
687 ExprList *pOrderBy; /* The ORDER BY clause. May be NULL */
drh22827922000-06-06 17:27:05 +0000688 ExprList *pGroupBy; /* The GROUP BY clause. May be NULL */
689 Expr *pHaving; /* The HAVING clause. May be NULL */
drh19a775c2000-06-05 18:54:46 +0000690 int isDistinct; /* True if the DISTINCT keyword is present */
691 int distinct; /* Table to use for the distinct set */
drh10e5e3c2000-06-08 00:19:02 +0000692 int base; /* First cursor available for use */
drh9bb61fe2000-06-05 16:01:39 +0000693
drhdaffd0e2001-04-11 14:28:42 +0000694 if( sqlite_malloc_failed || pParse->nErr || p==0 ) return 1;
695
drh82c3d632000-06-06 21:56:07 +0000696 /* If there is are a sequence of queries, do the earlier ones first.
697 */
698 if( p->pPrior ){
699 return multiSelect(pParse, p, eDest, iParm);
700 }
701
702 /* Make local copies of the parameters for this query.
703 */
drh9bb61fe2000-06-05 16:01:39 +0000704 pTabList = p->pSrc;
705 pWhere = p->pWhere;
706 pOrderBy = p->pOrderBy;
drh22827922000-06-06 17:27:05 +0000707 pGroupBy = p->pGroupBy;
708 pHaving = p->pHaving;
drh19a775c2000-06-05 18:54:46 +0000709 isDistinct = p->isDistinct;
drh9bb61fe2000-06-05 16:01:39 +0000710
drh10e5e3c2000-06-08 00:19:02 +0000711 /* Save the current value of pParse->nTab. Restore this value before
712 ** we exit.
713 */
714 base = pParse->nTab;
715
drh9bb61fe2000-06-05 16:01:39 +0000716 /*
717 ** Do not even attempt to generate any code if we have already seen
718 ** errors before this routine starts.
719 */
drh10e5e3c2000-06-08 00:19:02 +0000720 if( pParse->nErr>0 ) return 1;
drh22827922000-06-06 17:27:05 +0000721 sqliteParseInfoReset(pParse);
drhcce7d172000-05-31 15:34:51 +0000722
drhd8bc7082000-06-07 23:51:50 +0000723 /* Look up every table in the table list and create an appropriate
724 ** columnlist in pEList if there isn't one already. (The parser leaves
drh967e8b72000-06-21 13:59:10 +0000725 ** a NULL in the p->pEList if the SQL said "SELECT * FROM ...")
drhcce7d172000-05-31 15:34:51 +0000726 */
drhd8bc7082000-06-07 23:51:50 +0000727 if( fillInColumnList(pParse, p) ){
728 return 1;
drhcce7d172000-05-31 15:34:51 +0000729 }
drhd8bc7082000-06-07 23:51:50 +0000730 pEList = p->pEList;
drhdaffd0e2001-04-11 14:28:42 +0000731 if( pEList==0 ) return 1;
drhcce7d172000-05-31 15:34:51 +0000732
drh19a775c2000-06-05 18:54:46 +0000733 /* Allocate a temporary table to use for the DISTINCT set, if
drh22827922000-06-06 17:27:05 +0000734 ** necessary. This must be done early to allocate the cursor before
735 ** any calls to sqliteExprResolveIds().
drh19a775c2000-06-05 18:54:46 +0000736 */
737 if( isDistinct ){
738 distinct = pParse->nTab++;
drh22827922000-06-06 17:27:05 +0000739 }else{
740 distinct = -1;
drh19a775c2000-06-05 18:54:46 +0000741 }
742
drh22827922000-06-06 17:27:05 +0000743 /* If writing to memory or generating a set
744 ** only a single column may be output.
drh19a775c2000-06-05 18:54:46 +0000745 */
drhfef52082000-06-06 01:50:43 +0000746 if( (eDest==SRT_Mem || eDest==SRT_Set) && pEList->nExpr>1 ){
drh19a775c2000-06-05 18:54:46 +0000747 sqliteSetString(&pParse->zErrMsg, "only a single result allowed for "
748 "a SELECT that is part of an expression", 0);
749 pParse->nErr++;
750 return 1;
751 }
752
drh22827922000-06-06 17:27:05 +0000753 /* ORDER BY is ignored if we are not sending the result to a callback.
754 */
755 if( eDest!=SRT_Callback ){
756 pOrderBy = 0;
757 }
758
759 /* Allocate cursors for "expr IN (SELECT ...)" constructs.
drhcce7d172000-05-31 15:34:51 +0000760 */
761 for(i=0; i<pEList->nExpr; i++){
drh4794b982000-06-06 13:54:14 +0000762 sqliteExprResolveInSelect(pParse, pEList->a[i].pExpr);
763 }
764 if( pWhere ) sqliteExprResolveInSelect(pParse, pWhere);
765 if( pOrderBy ){
766 for(i=0; i<pOrderBy->nExpr; i++){
767 sqliteExprResolveInSelect(pParse, pOrderBy->a[i].pExpr);
768 }
769 }
drh22827922000-06-06 17:27:05 +0000770 if( pGroupBy ){
771 for(i=0; i<pGroupBy->nExpr; i++){
772 sqliteExprResolveInSelect(pParse, pGroupBy->a[i].pExpr);
773 }
774 }
775 if( pHaving ) sqliteExprResolveInSelect(pParse, pHaving);
776
drh10e5e3c2000-06-08 00:19:02 +0000777 /* At this point, we should have allocated all the cursors that we
778 ** need to handle subquerys and temporary tables. From here on we
779 ** are committed to keeping the same value for pParse->nTab.
780 **
drh967e8b72000-06-21 13:59:10 +0000781 ** Resolve the column names and do a semantics check on all the expressions.
drh22827922000-06-06 17:27:05 +0000782 */
drh4794b982000-06-06 13:54:14 +0000783 for(i=0; i<pEList->nExpr; i++){
drhcce7d172000-05-31 15:34:51 +0000784 if( sqliteExprResolveIds(pParse, pTabList, pEList->a[i].pExpr) ){
drh9bb61fe2000-06-05 16:01:39 +0000785 return 1;
drhcce7d172000-05-31 15:34:51 +0000786 }
drh22827922000-06-06 17:27:05 +0000787 if( sqliteExprCheck(pParse, pEList->a[i].pExpr, 1, &isAgg) ){
drh9bb61fe2000-06-05 16:01:39 +0000788 return 1;
drhcce7d172000-05-31 15:34:51 +0000789 }
790 }
drhcce7d172000-05-31 15:34:51 +0000791 if( pWhere ){
792 if( sqliteExprResolveIds(pParse, pTabList, pWhere) ){
drh9bb61fe2000-06-05 16:01:39 +0000793 return 1;
drhcce7d172000-05-31 15:34:51 +0000794 }
795 if( sqliteExprCheck(pParse, pWhere, 0, 0) ){
drh9bb61fe2000-06-05 16:01:39 +0000796 return 1;
drhcce7d172000-05-31 15:34:51 +0000797 }
798 }
799 if( pOrderBy ){
800 for(i=0; i<pOrderBy->nExpr; i++){
drh22827922000-06-06 17:27:05 +0000801 Expr *pE = pOrderBy->a[i].pExpr;
802 if( sqliteExprResolveIds(pParse, pTabList, pE) ){
drh9bb61fe2000-06-05 16:01:39 +0000803 return 1;
drhcce7d172000-05-31 15:34:51 +0000804 }
drh22827922000-06-06 17:27:05 +0000805 if( sqliteExprCheck(pParse, pE, isAgg, 0) ){
drh9bb61fe2000-06-05 16:01:39 +0000806 return 1;
drhcce7d172000-05-31 15:34:51 +0000807 }
808 }
809 }
drh22827922000-06-06 17:27:05 +0000810 if( pGroupBy ){
811 for(i=0; i<pGroupBy->nExpr; i++){
812 Expr *pE = pGroupBy->a[i].pExpr;
813 if( sqliteExprResolveIds(pParse, pTabList, pE) ){
814 return 1;
815 }
816 if( sqliteExprCheck(pParse, pE, isAgg, 0) ){
817 return 1;
818 }
819 }
820 }
821 if( pHaving ){
822 if( pGroupBy==0 ){
drhda932812000-06-06 18:00:15 +0000823 sqliteSetString(&pParse->zErrMsg, "a GROUP BY clause is required "
824 "before HAVING", 0);
drh22827922000-06-06 17:27:05 +0000825 pParse->nErr++;
826 return 1;
827 }
828 if( sqliteExprResolveIds(pParse, pTabList, pHaving) ){
829 return 1;
830 }
drhda932812000-06-06 18:00:15 +0000831 if( sqliteExprCheck(pParse, pHaving, isAgg, 0) ){
drh22827922000-06-06 17:27:05 +0000832 return 1;
833 }
drhcce7d172000-05-31 15:34:51 +0000834 }
835
drh22827922000-06-06 17:27:05 +0000836 /* Do an analysis of aggregate expressions.
drhefb72512000-05-31 20:00:52 +0000837 */
drh22827922000-06-06 17:27:05 +0000838 if( isAgg ){
drhaaf88722000-06-08 11:25:00 +0000839 assert( pParse->nAgg==0 && pParse->iAggCount<0 );
drh22827922000-06-06 17:27:05 +0000840 for(i=0; i<pEList->nExpr; i++){
841 if( sqliteExprAnalyzeAggregates(pParse, pEList->a[i].pExpr) ){
842 return 1;
843 }
844 }
845 if( pGroupBy ){
846 for(i=0; i<pGroupBy->nExpr; i++){
847 if( sqliteExprAnalyzeAggregates(pParse, pGroupBy->a[i].pExpr) ){
848 return 1;
849 }
850 }
851 }
852 if( pHaving && sqliteExprAnalyzeAggregates(pParse, pHaving) ){
853 return 1;
854 }
drh191b6902000-06-08 11:13:01 +0000855 if( pOrderBy ){
856 for(i=0; i<pOrderBy->nExpr; i++){
857 if( sqliteExprAnalyzeAggregates(pParse, pOrderBy->a[i].pExpr) ){
858 return 1;
859 }
860 }
861 }
drhefb72512000-05-31 20:00:52 +0000862 }
863
drhcce7d172000-05-31 15:34:51 +0000864 /* Begin generating code.
865 */
drhdaffd0e2001-04-11 14:28:42 +0000866 v = sqliteGetVdbe(pParse);
867 if( v==0 ) return 1;
drhcce7d172000-05-31 15:34:51 +0000868
drh9bbca4c2001-11-06 04:00:18 +0000869 /* Set the limiter
870 */
871 if( p->nLimit<=0 ){
872 p->nOffset = 0;
873 }else{
874 if( p->nOffset<0 ) p->nOffset = 0;
875 sqliteVdbeAddOp(v, OP_Limit, p->nLimit, p->nOffset);
876 }
877
878
drh22827922000-06-06 17:27:05 +0000879 /* Identify column names if we will be using in the callback. This
drh19a775c2000-06-05 18:54:46 +0000880 ** step is skipped if the output is going to a table or a memory cell.
drhcce7d172000-05-31 15:34:51 +0000881 */
drhfef52082000-06-06 01:50:43 +0000882 if( eDest==SRT_Callback ){
drhd8bc7082000-06-07 23:51:50 +0000883 generateColumnNames(pParse, pTabList, pEList);
drhcce7d172000-05-31 15:34:51 +0000884 }
885
drh22827922000-06-06 17:27:05 +0000886 /* Reset the aggregator
drhcce7d172000-05-31 15:34:51 +0000887 */
888 if( isAgg ){
drh99fcd712001-10-13 01:06:47 +0000889 sqliteVdbeAddOp(v, OP_AggReset, 0, pParse->nAgg);
drh1bee3d72001-10-15 00:44:35 +0000890 if( pGroupBy==0 ){
891 sqliteVdbeAddOp(v, OP_String, 0, 0);
892 sqliteVdbeChangeP3(v, -1, "", P3_STATIC);
893 sqliteVdbeAddOp(v, OP_AggFocus, 0, 0);
894 for(i=0; i<pParse->nAgg; i++){
895 Expr *pE;
896 if( !pParse->aAgg[i].isAgg ) continue;
897 pE = pParse->aAgg[i].pExpr;
898 assert( pE==0 || pE->op==TK_AGG_FUNCTION );
899 assert( pE==0 || (pE->pList!=0 && pE->pList->nExpr==1) );
900 if( pE==0 || pE->iColumn==FN_Sum ){
901 sqliteVdbeAddOp(v, OP_Integer, 0, 0);
902 sqliteVdbeAddOp(v, OP_AggSet, 0, i);
903 continue;
904 }
905 }
906 }
drhcce7d172000-05-31 15:34:51 +0000907 }
908
drh19a775c2000-06-05 18:54:46 +0000909 /* Initialize the memory cell to NULL
910 */
drhfef52082000-06-06 01:50:43 +0000911 if( eDest==SRT_Mem ){
drh99fcd712001-10-13 01:06:47 +0000912 sqliteVdbeAddOp(v, OP_String, 0, 0);
drh8721ce42001-11-07 14:22:00 +0000913 sqliteVdbeAddOp(v, OP_MemStore, iParm, 1);
drh19a775c2000-06-05 18:54:46 +0000914 }
915
drhcce7d172000-05-31 15:34:51 +0000916 /* Begin the database scan
drhefb72512000-05-31 20:00:52 +0000917 */
drh19a775c2000-06-05 18:54:46 +0000918 if( isDistinct ){
drh99fcd712001-10-13 01:06:47 +0000919 sqliteVdbeAddOp(v, OP_OpenTemp, distinct, 0);
drhefb72512000-05-31 20:00:52 +0000920 }
drhcce7d172000-05-31 15:34:51 +0000921 pWInfo = sqliteWhereBegin(pParse, pTabList, pWhere, 0);
drh9bb61fe2000-06-05 16:01:39 +0000922 if( pWInfo==0 ) return 1;
drhcce7d172000-05-31 15:34:51 +0000923
drh22827922000-06-06 17:27:05 +0000924 /* Use the standard inner loop if we are not dealing with
925 ** aggregates
drhcce7d172000-05-31 15:34:51 +0000926 */
drhda9d6c42000-05-31 18:20:14 +0000927 if( !isAgg ){
drh82c3d632000-06-06 21:56:07 +0000928 if( selectInnerLoop(pParse, pEList, 0, 0, pOrderBy, distinct, eDest, iParm,
drh22827922000-06-06 17:27:05 +0000929 pWInfo->iContinue, pWInfo->iBreak) ){
930 return 1;
drhda9d6c42000-05-31 18:20:14 +0000931 }
drhcce7d172000-05-31 15:34:51 +0000932 }
drhefb72512000-05-31 20:00:52 +0000933
drh22827922000-06-06 17:27:05 +0000934 /* If we are dealing with aggregates, then to the special aggregate
935 ** processing.
drhefb72512000-05-31 20:00:52 +0000936 */
drh22827922000-06-06 17:27:05 +0000937 else{
drh22827922000-06-06 17:27:05 +0000938 if( pGroupBy ){
drh1bee3d72001-10-15 00:44:35 +0000939 int lbl1;
drh22827922000-06-06 17:27:05 +0000940 for(i=0; i<pGroupBy->nExpr; i++){
941 sqliteExprCode(pParse, pGroupBy->a[i].pExpr);
942 }
drh99fcd712001-10-13 01:06:47 +0000943 sqliteVdbeAddOp(v, OP_MakeKey, pGroupBy->nExpr, 0);
drh1bee3d72001-10-15 00:44:35 +0000944 lbl1 = sqliteVdbeMakeLabel(v);
drh99fcd712001-10-13 01:06:47 +0000945 sqliteVdbeAddOp(v, OP_AggFocus, 0, lbl1);
drh22827922000-06-06 17:27:05 +0000946 for(i=0; i<pParse->nAgg; i++){
947 if( pParse->aAgg[i].isAgg ) continue;
948 sqliteExprCode(pParse, pParse->aAgg[i].pExpr);
drh99fcd712001-10-13 01:06:47 +0000949 sqliteVdbeAddOp(v, OP_AggSet, 0, i);
drhcce7d172000-05-31 15:34:51 +0000950 }
drh22827922000-06-06 17:27:05 +0000951 sqliteVdbeResolveLabel(v, lbl1);
drhcce7d172000-05-31 15:34:51 +0000952 }
drh22827922000-06-06 17:27:05 +0000953 for(i=0; i<pParse->nAgg; i++){
954 Expr *pE;
955 int op;
956 if( !pParse->aAgg[i].isAgg ) continue;
957 pE = pParse->aAgg[i].pExpr;
958 if( pE==0 ){
drh99fcd712001-10-13 01:06:47 +0000959 sqliteVdbeAddOp(v, OP_AggIncr, 1, i);
drh22827922000-06-06 17:27:05 +0000960 continue;
961 }
962 assert( pE->op==TK_AGG_FUNCTION );
963 assert( pE->pList!=0 && pE->pList->nExpr==1 );
964 sqliteExprCode(pParse, pE->pList->a[0].pExpr);
drh99fcd712001-10-13 01:06:47 +0000965 sqliteVdbeAddOp(v, OP_AggGet, 0, i);
drh967e8b72000-06-21 13:59:10 +0000966 switch( pE->iColumn ){
drh22827922000-06-06 17:27:05 +0000967 case FN_Min: op = OP_Min; break;
968 case FN_Max: op = OP_Max; break;
969 case FN_Avg: op = OP_Add; break;
970 case FN_Sum: op = OP_Add; break;
971 }
drh99fcd712001-10-13 01:06:47 +0000972 sqliteVdbeAddOp(v, op, 0, 0);
973 sqliteVdbeAddOp(v, OP_AggSet, 0, i);
drh22827922000-06-06 17:27:05 +0000974 }
drhcce7d172000-05-31 15:34:51 +0000975 }
976
drh22827922000-06-06 17:27:05 +0000977
drhcce7d172000-05-31 15:34:51 +0000978 /* End the database scan loop.
979 */
980 sqliteWhereEnd(pWInfo);
981
drh22827922000-06-06 17:27:05 +0000982 /* If we are processing aggregates, we need to set up a second loop
983 ** over all of the aggregate values and process them.
984 */
985 if( isAgg ){
986 int endagg = sqliteVdbeMakeLabel(v);
987 int startagg;
drh99fcd712001-10-13 01:06:47 +0000988 startagg = sqliteVdbeAddOp(v, OP_AggNext, 0, endagg);
drh22827922000-06-06 17:27:05 +0000989 pParse->useAgg = 1;
990 if( pHaving ){
991 sqliteExprIfFalse(pParse, pHaving, startagg);
992 }
drh82c3d632000-06-06 21:56:07 +0000993 if( selectInnerLoop(pParse, pEList, 0, 0, pOrderBy, distinct, eDest, iParm,
drh22827922000-06-06 17:27:05 +0000994 startagg, endagg) ){
995 return 1;
996 }
drh99fcd712001-10-13 01:06:47 +0000997 sqliteVdbeAddOp(v, OP_Goto, 0, startagg);
998 sqliteVdbeResolveLabel(v, endagg);
999 sqliteVdbeAddOp(v, OP_Noop, 0, 0);
drh22827922000-06-06 17:27:05 +00001000 pParse->useAgg = 0;
1001 }
1002
drhcce7d172000-05-31 15:34:51 +00001003 /* If there is an ORDER BY clause, then we need to sort the results
1004 ** and send them to the callback one by one.
1005 */
1006 if( pOrderBy ){
drhd8bc7082000-06-07 23:51:50 +00001007 generateSortTail(v, pEList->nExpr);
drhcce7d172000-05-31 15:34:51 +00001008 }
drh10e5e3c2000-06-08 00:19:02 +00001009 pParse->nTab = base;
drh6a535342001-10-19 16:44:56 +00001010
1011
1012 /* Issue a null callback if that is what the user wants.
1013 */
1014 if( (pParse->db->flags & SQLITE_NullCallback)!=0 && eDest==SRT_Callback ){
1015 sqliteVdbeAddOp(v, OP_NullCallback, pEList->nExpr, 0);
1016 }
1017
drh9bb61fe2000-06-05 16:01:39 +00001018 return 0;
drhcce7d172000-05-31 15:34:51 +00001019}