blob: 2179cb5b4ff912c6fdf3bdfff0239a667296f348 [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**
drh7c917d12001-12-16 20:05:05 +000015** $Id: select.c,v 1.50 2001/12/16 20:05:06 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){
drh7c917d12001-12-16 20:05:05 +0000321 int i, j, k;
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
drh7c917d12001-12-16 20:05:05 +0000356 /* For every "*" that occurs in the column list, insert the names of
357 ** all columns in all tables. The parser inserted a special expression
358 ** with the TK_ALL operator for each "*" that it found in the column list.
359 ** The following code just has to locate the TK_ALL expressions and expand
360 ** each one to the list of all columns in all tables.
drhd8bc7082000-06-07 23:51:50 +0000361 */
drh7c917d12001-12-16 20:05:05 +0000362 for(k=0; k<pEList->nExpr; k++){
363 if( pEList->a[k].pExpr->op==TK_ALL ) break;
364 }
365 if( k<pEList->nExpr ){
366 struct ExprList_item *a = pEList->a;
367 ExprList *pNew = 0;
368 for(k=0; k<pEList->nExpr; k++){
369 if( a[k].pExpr->op!=TK_ALL ){
370 pNew = sqliteExprListAppend(pNew, a[k].pExpr, 0);
371 pNew->a[pNew->nExpr-1].zName = a[k].zName;
372 a[k].pExpr = 0;
373 a[k].zName = 0;
374 }else{
375 for(i=0; i<pTabList->nId; i++){
376 Table *pTab = pTabList->a[i].pTab;
377 for(j=0; j<pTab->nCol; j++){
378 Expr *pExpr = sqliteExpr(TK_DOT, 0, 0, 0);
379 if( pExpr==0 ) break;
380 pExpr->pLeft = sqliteExpr(TK_ID, 0, 0, 0);
381 if( pExpr->pLeft==0 ){ sqliteExprDelete(pExpr); break; }
382 if( pTabList->a[i].zAlias && pTabList->a[i].zAlias[0] ){
383 pExpr->pLeft->token.z = pTabList->a[i].zAlias;
384 pExpr->pLeft->token.n = strlen(pTabList->a[i].zAlias);
385 }else{
386 pExpr->pLeft->token.z = pTab->zName;
387 pExpr->pLeft->token.n = strlen(pTab->zName);
388 }
389 pExpr->pRight = sqliteExpr(TK_ID, 0, 0, 0);
390 if( pExpr->pRight==0 ){ sqliteExprDelete(pExpr); break; }
391 pExpr->pRight->token.z = pTab->aCol[j].zName;
392 pExpr->pRight->token.n = strlen(pTab->aCol[j].zName);
393 pExpr->span.z = "";
394 pExpr->span.n = 0;
395 pNew = sqliteExprListAppend(pNew, pExpr, 0);
396 }
drh17e24df2001-11-06 14:10:41 +0000397 }
drhd8bc7082000-06-07 23:51:50 +0000398 }
399 }
drh7c917d12001-12-16 20:05:05 +0000400 sqliteExprListDelete(pEList);
401 p->pEList = pNew;
drhd8bc7082000-06-07 23:51:50 +0000402 }
403 return 0;
404}
405
406/*
407** This routine associates entries in an ORDER BY expression list with
408** columns in a result. For each ORDER BY expression, the opcode of
drh967e8b72000-06-21 13:59:10 +0000409** the top-level node is changed to TK_COLUMN and the iColumn value of
drhd8bc7082000-06-07 23:51:50 +0000410** the top-level node is filled in with column number and the iTable
411** value of the top-level node is filled with iTable parameter.
412**
413** If there are prior SELECT clauses, they are processed first. A match
414** in an earlier SELECT takes precedence over a later SELECT.
415**
416** Any entry that does not match is flagged as an error. The number
417** of errors is returned.
418*/
419static int matchOrderbyToColumn(
420 Parse *pParse, /* A place to leave error messages */
421 Select *pSelect, /* Match to result columns of this SELECT */
422 ExprList *pOrderBy, /* The ORDER BY values to match against columns */
423 int iTable, /* Insert this this value in iTable */
424 int mustComplete /* If TRUE all ORDER BYs must match */
425){
426 int nErr = 0;
427 int i, j;
428 ExprList *pEList;
429
drhdaffd0e2001-04-11 14:28:42 +0000430 if( pSelect==0 || pOrderBy==0 ) return 1;
drhd8bc7082000-06-07 23:51:50 +0000431 if( mustComplete ){
432 for(i=0; i<pOrderBy->nExpr; i++){ pOrderBy->a[i].done = 0; }
433 }
434 if( fillInColumnList(pParse, pSelect) ){
435 return 1;
436 }
437 if( pSelect->pPrior ){
drh92cd52f2000-06-08 01:55:29 +0000438 if( matchOrderbyToColumn(pParse, pSelect->pPrior, pOrderBy, iTable, 0) ){
439 return 1;
440 }
drhd8bc7082000-06-07 23:51:50 +0000441 }
442 pEList = pSelect->pEList;
443 for(i=0; i<pOrderBy->nExpr; i++){
444 Expr *pE = pOrderBy->a[i].pExpr;
drh92cd52f2000-06-08 01:55:29 +0000445 int match = 0;
drhd8bc7082000-06-07 23:51:50 +0000446 if( pOrderBy->a[i].done ) continue;
447 for(j=0; j<pEList->nExpr; j++){
drh4cfa7932000-06-08 15:10:46 +0000448 if( pEList->a[j].zName && (pE->op==TK_ID || pE->op==TK_STRING) ){
449 char *zName = pEList->a[j].zName;
drh6e142f52000-06-08 13:36:40 +0000450 char *zLabel = sqliteStrNDup(pE->token.z, pE->token.n);
drhd8bc7082000-06-07 23:51:50 +0000451 sqliteDequote(zLabel);
452 if( sqliteStrICmp(zName, zLabel)==0 ){
453 match = 1;
454 }
drh6e142f52000-06-08 13:36:40 +0000455 sqliteFree(zLabel);
drhd8bc7082000-06-07 23:51:50 +0000456 }
drh4cfa7932000-06-08 15:10:46 +0000457 if( match==0 && sqliteExprCompare(pE, pEList->a[j].pExpr) ){
drhd8bc7082000-06-07 23:51:50 +0000458 match = 1;
459 }
460 if( match ){
drh967e8b72000-06-21 13:59:10 +0000461 pE->op = TK_COLUMN;
462 pE->iColumn = j;
drhd8bc7082000-06-07 23:51:50 +0000463 pE->iTable = iTable;
464 pOrderBy->a[i].done = 1;
465 break;
466 }
467 }
drh92cd52f2000-06-08 01:55:29 +0000468 if( !match && mustComplete ){
drhd8bc7082000-06-07 23:51:50 +0000469 char zBuf[30];
470 sprintf(zBuf,"%d",i+1);
471 sqliteSetString(&pParse->zErrMsg, "ORDER BY term number ", zBuf,
472 " does not match any result column", 0);
473 pParse->nErr++;
474 nErr++;
475 break;
476 }
477 }
478 return nErr;
479}
480
481/*
482** Get a VDBE for the given parser context. Create a new one if necessary.
483** If an error occurs, return NULL and leave a message in pParse.
484*/
485Vdbe *sqliteGetVdbe(Parse *pParse){
486 Vdbe *v = pParse->pVdbe;
487 if( v==0 ){
drh4c504392000-10-16 22:06:40 +0000488 v = pParse->pVdbe = sqliteVdbeCreate(pParse->db);
drhd8bc7082000-06-07 23:51:50 +0000489 }
drhd8bc7082000-06-07 23:51:50 +0000490 return v;
491}
492
493
494/*
drh82c3d632000-06-06 21:56:07 +0000495** This routine is called to process a query that is really the union
496** or intersection of two or more separate queries.
497*/
498static int multiSelect(Parse *pParse, Select *p, int eDest, int iParm){
drh10e5e3c2000-06-08 00:19:02 +0000499 int rc; /* Success code from a subroutine */
500 Select *pPrior; /* Another SELECT immediately to our left */
501 Vdbe *v; /* Generate code to this VDBE */
502 int base; /* Baseline value for pParse->nTab */
drh82c3d632000-06-06 21:56:07 +0000503
drhd8bc7082000-06-07 23:51:50 +0000504 /* Make sure there is no ORDER BY clause on prior SELECTs. Only the
505 ** last SELECT in the series may have an ORDER BY.
drh82c3d632000-06-06 21:56:07 +0000506 */
drhdaffd0e2001-04-11 14:28:42 +0000507 if( p==0 || p->pPrior==0 ) return 1;
drhd8bc7082000-06-07 23:51:50 +0000508 pPrior = p->pPrior;
509 if( pPrior->pOrderBy ){
510 sqliteSetString(&pParse->zErrMsg,"ORDER BY clause should come after ",
511 selectOpName(p->op), " not before", 0);
drh82c3d632000-06-06 21:56:07 +0000512 pParse->nErr++;
513 return 1;
514 }
515
drhd8bc7082000-06-07 23:51:50 +0000516 /* Make sure we have a valid query engine. If not, create a new one.
517 */
518 v = sqliteGetVdbe(pParse);
519 if( v==0 ) return 1;
520
521 /* Process the UNION or INTERSECTION
522 */
drh10e5e3c2000-06-08 00:19:02 +0000523 base = pParse->nTab;
drh82c3d632000-06-06 21:56:07 +0000524 switch( p->op ){
drhd8bc7082000-06-07 23:51:50 +0000525 case TK_ALL:
drh82c3d632000-06-06 21:56:07 +0000526 case TK_EXCEPT:
527 case TK_UNION: {
drhd8bc7082000-06-07 23:51:50 +0000528 int unionTab; /* Cursor number of the temporary table holding result */
529 int op; /* One of the SRT_ operations to apply to self */
530 int priorOp; /* The SRT_ operation to apply to prior selects */
drh82c3d632000-06-06 21:56:07 +0000531
drhd8bc7082000-06-07 23:51:50 +0000532 priorOp = p->op==TK_ALL ? SRT_Table : SRT_Union;
533 if( eDest==priorOp ){
534 /* We can reuse a temporary table generated by a SELECT to our
535 ** right. This also means we are not the right-most select and so
536 ** we cannot have an ORDER BY clause
537 */
drh82c3d632000-06-06 21:56:07 +0000538 unionTab = iParm;
drhd8bc7082000-06-07 23:51:50 +0000539 assert( p->pOrderBy==0 );
drh82c3d632000-06-06 21:56:07 +0000540 }else{
drhd8bc7082000-06-07 23:51:50 +0000541 /* We will need to create our own temporary table to hold the
542 ** intermediate results.
543 */
544 unionTab = pParse->nTab++;
545 if( p->pOrderBy
546 && matchOrderbyToColumn(pParse, p, p->pOrderBy, unionTab, 1) ){
547 return 1;
548 }
drhd8bc7082000-06-07 23:51:50 +0000549 if( p->op!=TK_ALL ){
drh99fcd712001-10-13 01:06:47 +0000550 sqliteVdbeAddOp(v, OP_OpenTemp, unionTab, 0);
551 sqliteVdbeAddOp(v, OP_KeyAsData, unionTab, 1);
drh345fda32001-01-15 22:51:08 +0000552 }else{
drh99fcd712001-10-13 01:06:47 +0000553 sqliteVdbeAddOp(v, OP_OpenTemp, unionTab, 0);
drhd8bc7082000-06-07 23:51:50 +0000554 }
drh82c3d632000-06-06 21:56:07 +0000555 }
drhd8bc7082000-06-07 23:51:50 +0000556
557 /* Code the SELECT statements to our left
558 */
559 rc = sqliteSelect(pParse, pPrior, priorOp, unionTab);
drh82c3d632000-06-06 21:56:07 +0000560 if( rc ) return rc;
drhd8bc7082000-06-07 23:51:50 +0000561
562 /* Code the current SELECT statement
563 */
564 switch( p->op ){
565 case TK_EXCEPT: op = SRT_Except; break;
566 case TK_UNION: op = SRT_Union; break;
567 case TK_ALL: op = SRT_Table; break;
568 }
drh82c3d632000-06-06 21:56:07 +0000569 p->pPrior = 0;
570 rc = sqliteSelect(pParse, p, op, unionTab);
571 p->pPrior = pPrior;
572 if( rc ) return rc;
drhd8bc7082000-06-07 23:51:50 +0000573
574 /* Convert the data in the temporary table into whatever form
575 ** it is that we currently need.
576 */
577 if( eDest!=priorOp ){
drh6b563442001-11-07 16:48:26 +0000578 int iCont, iBreak, iStart;
drh82c3d632000-06-06 21:56:07 +0000579 assert( p->pEList );
drhd8bc7082000-06-07 23:51:50 +0000580 generateColumnNames(pParse, 0, p->pEList);
drh82c3d632000-06-06 21:56:07 +0000581 iBreak = sqliteVdbeMakeLabel(v);
drh6b563442001-11-07 16:48:26 +0000582 iCont = sqliteVdbeMakeLabel(v);
583 sqliteVdbeAddOp(v, OP_Rewind, unionTab, iBreak);
584 iStart = sqliteVdbeCurrentAddr(v);
drh82c3d632000-06-06 21:56:07 +0000585 rc = selectInnerLoop(pParse, 0, unionTab, p->pEList->nExpr,
drhd8bc7082000-06-07 23:51:50 +0000586 p->pOrderBy, -1, eDest, iParm,
drh82c3d632000-06-06 21:56:07 +0000587 iCont, iBreak);
588 if( rc ) return 1;
drh6b563442001-11-07 16:48:26 +0000589 sqliteVdbeResolveLabel(v, iCont);
590 sqliteVdbeAddOp(v, OP_Next, unionTab, iStart);
drh99fcd712001-10-13 01:06:47 +0000591 sqliteVdbeResolveLabel(v, iBreak);
592 sqliteVdbeAddOp(v, OP_Close, unionTab, 0);
drhd8bc7082000-06-07 23:51:50 +0000593 if( p->pOrderBy ){
594 generateSortTail(v, p->pEList->nExpr);
595 }
drh82c3d632000-06-06 21:56:07 +0000596 }
597 break;
598 }
599 case TK_INTERSECT: {
600 int tab1, tab2;
drh6b563442001-11-07 16:48:26 +0000601 int iCont, iBreak, iStart;
drh82c3d632000-06-06 21:56:07 +0000602
drhd8bc7082000-06-07 23:51:50 +0000603 /* INTERSECT is different from the others since it requires
drh6206d502000-06-19 19:09:08 +0000604 ** two temporary tables. Hence it has its own case. Begin
drhd8bc7082000-06-07 23:51:50 +0000605 ** by allocating the tables we will need.
606 */
drh82c3d632000-06-06 21:56:07 +0000607 tab1 = pParse->nTab++;
608 tab2 = pParse->nTab++;
drhd8bc7082000-06-07 23:51:50 +0000609 if( p->pOrderBy && matchOrderbyToColumn(pParse,p,p->pOrderBy,tab1,1) ){
610 return 1;
611 }
drh99fcd712001-10-13 01:06:47 +0000612 sqliteVdbeAddOp(v, OP_OpenTemp, tab1, 0);
613 sqliteVdbeAddOp(v, OP_KeyAsData, tab1, 1);
drhd8bc7082000-06-07 23:51:50 +0000614
615 /* Code the SELECTs to our left into temporary table "tab1".
616 */
drh82c3d632000-06-06 21:56:07 +0000617 rc = sqliteSelect(pParse, pPrior, SRT_Union, tab1);
618 if( rc ) return rc;
drhd8bc7082000-06-07 23:51:50 +0000619
620 /* Code the current SELECT into temporary table "tab2"
621 */
drh99fcd712001-10-13 01:06:47 +0000622 sqliteVdbeAddOp(v, OP_OpenTemp, tab2, 0);
623 sqliteVdbeAddOp(v, OP_KeyAsData, tab2, 1);
drh82c3d632000-06-06 21:56:07 +0000624 p->pPrior = 0;
625 rc = sqliteSelect(pParse, p, SRT_Union, tab2);
626 p->pPrior = pPrior;
627 if( rc ) return rc;
drhd8bc7082000-06-07 23:51:50 +0000628
629 /* Generate code to take the intersection of the two temporary
630 ** tables.
631 */
drh82c3d632000-06-06 21:56:07 +0000632 assert( p->pEList );
drhd8bc7082000-06-07 23:51:50 +0000633 generateColumnNames(pParse, 0, p->pEList);
drh82c3d632000-06-06 21:56:07 +0000634 iBreak = sqliteVdbeMakeLabel(v);
drh6b563442001-11-07 16:48:26 +0000635 iCont = sqliteVdbeMakeLabel(v);
636 sqliteVdbeAddOp(v, OP_Rewind, tab1, iBreak);
637 iStart = sqliteVdbeAddOp(v, OP_FullKey, tab1, 0);
drh99fcd712001-10-13 01:06:47 +0000638 sqliteVdbeAddOp(v, OP_NotFound, tab2, iCont);
drh82c3d632000-06-06 21:56:07 +0000639 rc = selectInnerLoop(pParse, 0, tab1, p->pEList->nExpr,
drhd8bc7082000-06-07 23:51:50 +0000640 p->pOrderBy, -1, eDest, iParm,
drh82c3d632000-06-06 21:56:07 +0000641 iCont, iBreak);
642 if( rc ) return 1;
drh6b563442001-11-07 16:48:26 +0000643 sqliteVdbeResolveLabel(v, iCont);
644 sqliteVdbeAddOp(v, OP_Next, tab1, iStart);
drh99fcd712001-10-13 01:06:47 +0000645 sqliteVdbeResolveLabel(v, iBreak);
646 sqliteVdbeAddOp(v, OP_Close, tab2, 0);
647 sqliteVdbeAddOp(v, OP_Close, tab1, 0);
drhd8bc7082000-06-07 23:51:50 +0000648 if( p->pOrderBy ){
649 generateSortTail(v, p->pEList->nExpr);
650 }
drh82c3d632000-06-06 21:56:07 +0000651 break;
652 }
653 }
654 assert( p->pEList && pPrior->pEList );
655 if( p->pEList->nExpr!=pPrior->pEList->nExpr ){
drhd8bc7082000-06-07 23:51:50 +0000656 sqliteSetString(&pParse->zErrMsg, "SELECTs to the left and right of ",
657 selectOpName(p->op), " do not have the same number of result columns", 0);
drh82c3d632000-06-06 21:56:07 +0000658 pParse->nErr++;
659 return 1;
drh22827922000-06-06 17:27:05 +0000660 }
drh10e5e3c2000-06-08 00:19:02 +0000661 pParse->nTab = base;
drh22827922000-06-06 17:27:05 +0000662 return 0;
663}
664
665/*
drh9bb61fe2000-06-05 16:01:39 +0000666** Generate code for the given SELECT statement.
667**
drhfef52082000-06-06 01:50:43 +0000668** The results are distributed in various ways depending on the
669** value of eDest and iParm.
670**
671** eDest Value Result
672** ------------ -------------------------------------------
673** SRT_Callback Invoke the callback for each row of the result.
674**
675** SRT_Mem Store first result in memory cell iParm
676**
677** SRT_Set Store results as keys of a table with cursor iParm
678**
drh82c3d632000-06-06 21:56:07 +0000679** SRT_Union Store results as a key in a temporary table iParm
680**
drhc4a3c772001-04-04 11:48:57 +0000681** SRT_Except Remove results form the temporary table iParm.
682**
683** SRT_Table Store results in temporary table iParm
drh9bb61fe2000-06-05 16:01:39 +0000684**
685** This routine returns the number of errors. If any errors are
686** encountered, then an appropriate error message is left in
687** pParse->zErrMsg.
688**
689** This routine does NOT free the Select structure passed in. The
690** calling function needs to do that.
691*/
692int sqliteSelect(
drhcce7d172000-05-31 15:34:51 +0000693 Parse *pParse, /* The parser context */
drh9bb61fe2000-06-05 16:01:39 +0000694 Select *p, /* The SELECT statement being coded. */
drh82c3d632000-06-06 21:56:07 +0000695 int eDest, /* One of: SRT_Callback Mem Set Union Except */
drhfef52082000-06-06 01:50:43 +0000696 int iParm /* Save result in this memory location, if >=0 */
drhcce7d172000-05-31 15:34:51 +0000697){
drhd8bc7082000-06-07 23:51:50 +0000698 int i;
drhcce7d172000-05-31 15:34:51 +0000699 WhereInfo *pWInfo;
700 Vdbe *v;
701 int isAgg = 0; /* True for select lists like "count(*)" */
drh967e8b72000-06-21 13:59:10 +0000702 ExprList *pEList; /* List of columns to extract. NULL means "*" */
drh9bb61fe2000-06-05 16:01:39 +0000703 IdList *pTabList; /* List of tables to select from */
704 Expr *pWhere; /* The WHERE clause. May be NULL */
705 ExprList *pOrderBy; /* The ORDER BY clause. May be NULL */
drh22827922000-06-06 17:27:05 +0000706 ExprList *pGroupBy; /* The GROUP BY clause. May be NULL */
707 Expr *pHaving; /* The HAVING clause. May be NULL */
drh19a775c2000-06-05 18:54:46 +0000708 int isDistinct; /* True if the DISTINCT keyword is present */
709 int distinct; /* Table to use for the distinct set */
drh10e5e3c2000-06-08 00:19:02 +0000710 int base; /* First cursor available for use */
drh9bb61fe2000-06-05 16:01:39 +0000711
drhdaffd0e2001-04-11 14:28:42 +0000712 if( sqlite_malloc_failed || pParse->nErr || p==0 ) return 1;
713
drh82c3d632000-06-06 21:56:07 +0000714 /* If there is are a sequence of queries, do the earlier ones first.
715 */
716 if( p->pPrior ){
717 return multiSelect(pParse, p, eDest, iParm);
718 }
719
720 /* Make local copies of the parameters for this query.
721 */
drh9bb61fe2000-06-05 16:01:39 +0000722 pTabList = p->pSrc;
723 pWhere = p->pWhere;
724 pOrderBy = p->pOrderBy;
drh22827922000-06-06 17:27:05 +0000725 pGroupBy = p->pGroupBy;
726 pHaving = p->pHaving;
drh19a775c2000-06-05 18:54:46 +0000727 isDistinct = p->isDistinct;
drh9bb61fe2000-06-05 16:01:39 +0000728
drh10e5e3c2000-06-08 00:19:02 +0000729 /* Save the current value of pParse->nTab. Restore this value before
730 ** we exit.
731 */
732 base = pParse->nTab;
733
drh9bb61fe2000-06-05 16:01:39 +0000734 /*
735 ** Do not even attempt to generate any code if we have already seen
736 ** errors before this routine starts.
737 */
drh10e5e3c2000-06-08 00:19:02 +0000738 if( pParse->nErr>0 ) return 1;
drh22827922000-06-06 17:27:05 +0000739 sqliteParseInfoReset(pParse);
drhcce7d172000-05-31 15:34:51 +0000740
drhd8bc7082000-06-07 23:51:50 +0000741 /* Look up every table in the table list and create an appropriate
742 ** columnlist in pEList if there isn't one already. (The parser leaves
drh967e8b72000-06-21 13:59:10 +0000743 ** a NULL in the p->pEList if the SQL said "SELECT * FROM ...")
drhcce7d172000-05-31 15:34:51 +0000744 */
drhd8bc7082000-06-07 23:51:50 +0000745 if( fillInColumnList(pParse, p) ){
746 return 1;
drhcce7d172000-05-31 15:34:51 +0000747 }
drhd8bc7082000-06-07 23:51:50 +0000748 pEList = p->pEList;
drhdaffd0e2001-04-11 14:28:42 +0000749 if( pEList==0 ) return 1;
drhcce7d172000-05-31 15:34:51 +0000750
drh19a775c2000-06-05 18:54:46 +0000751 /* Allocate a temporary table to use for the DISTINCT set, if
drh22827922000-06-06 17:27:05 +0000752 ** necessary. This must be done early to allocate the cursor before
753 ** any calls to sqliteExprResolveIds().
drh19a775c2000-06-05 18:54:46 +0000754 */
755 if( isDistinct ){
756 distinct = pParse->nTab++;
drh22827922000-06-06 17:27:05 +0000757 }else{
758 distinct = -1;
drh19a775c2000-06-05 18:54:46 +0000759 }
760
drh22827922000-06-06 17:27:05 +0000761 /* If writing to memory or generating a set
762 ** only a single column may be output.
drh19a775c2000-06-05 18:54:46 +0000763 */
drhfef52082000-06-06 01:50:43 +0000764 if( (eDest==SRT_Mem || eDest==SRT_Set) && pEList->nExpr>1 ){
drh19a775c2000-06-05 18:54:46 +0000765 sqliteSetString(&pParse->zErrMsg, "only a single result allowed for "
766 "a SELECT that is part of an expression", 0);
767 pParse->nErr++;
768 return 1;
769 }
770
drh22827922000-06-06 17:27:05 +0000771 /* ORDER BY is ignored if we are not sending the result to a callback.
772 */
773 if( eDest!=SRT_Callback ){
774 pOrderBy = 0;
775 }
776
777 /* Allocate cursors for "expr IN (SELECT ...)" constructs.
drhcce7d172000-05-31 15:34:51 +0000778 */
779 for(i=0; i<pEList->nExpr; i++){
drh4794b982000-06-06 13:54:14 +0000780 sqliteExprResolveInSelect(pParse, pEList->a[i].pExpr);
781 }
782 if( pWhere ) sqliteExprResolveInSelect(pParse, pWhere);
783 if( pOrderBy ){
784 for(i=0; i<pOrderBy->nExpr; i++){
785 sqliteExprResolveInSelect(pParse, pOrderBy->a[i].pExpr);
786 }
787 }
drh22827922000-06-06 17:27:05 +0000788 if( pGroupBy ){
789 for(i=0; i<pGroupBy->nExpr; i++){
790 sqliteExprResolveInSelect(pParse, pGroupBy->a[i].pExpr);
791 }
792 }
793 if( pHaving ) sqliteExprResolveInSelect(pParse, pHaving);
794
drh10e5e3c2000-06-08 00:19:02 +0000795 /* At this point, we should have allocated all the cursors that we
796 ** need to handle subquerys and temporary tables. From here on we
797 ** are committed to keeping the same value for pParse->nTab.
798 **
drh967e8b72000-06-21 13:59:10 +0000799 ** Resolve the column names and do a semantics check on all the expressions.
drh22827922000-06-06 17:27:05 +0000800 */
drh4794b982000-06-06 13:54:14 +0000801 for(i=0; i<pEList->nExpr; i++){
drhcce7d172000-05-31 15:34:51 +0000802 if( sqliteExprResolveIds(pParse, pTabList, pEList->a[i].pExpr) ){
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, pEList->a[i].pExpr, 1, &isAgg) ){
drh9bb61fe2000-06-05 16:01:39 +0000806 return 1;
drhcce7d172000-05-31 15:34:51 +0000807 }
808 }
drhcce7d172000-05-31 15:34:51 +0000809 if( pWhere ){
810 if( sqliteExprResolveIds(pParse, pTabList, pWhere) ){
drh9bb61fe2000-06-05 16:01:39 +0000811 return 1;
drhcce7d172000-05-31 15:34:51 +0000812 }
813 if( sqliteExprCheck(pParse, pWhere, 0, 0) ){
drh9bb61fe2000-06-05 16:01:39 +0000814 return 1;
drhcce7d172000-05-31 15:34:51 +0000815 }
816 }
817 if( pOrderBy ){
818 for(i=0; i<pOrderBy->nExpr; i++){
drh22827922000-06-06 17:27:05 +0000819 Expr *pE = pOrderBy->a[i].pExpr;
820 if( sqliteExprResolveIds(pParse, pTabList, pE) ){
drh9bb61fe2000-06-05 16:01:39 +0000821 return 1;
drhcce7d172000-05-31 15:34:51 +0000822 }
drh22827922000-06-06 17:27:05 +0000823 if( sqliteExprCheck(pParse, pE, isAgg, 0) ){
drh9bb61fe2000-06-05 16:01:39 +0000824 return 1;
drhcce7d172000-05-31 15:34:51 +0000825 }
826 }
827 }
drh22827922000-06-06 17:27:05 +0000828 if( pGroupBy ){
829 for(i=0; i<pGroupBy->nExpr; i++){
830 Expr *pE = pGroupBy->a[i].pExpr;
831 if( sqliteExprResolveIds(pParse, pTabList, pE) ){
832 return 1;
833 }
834 if( sqliteExprCheck(pParse, pE, isAgg, 0) ){
835 return 1;
836 }
837 }
838 }
839 if( pHaving ){
840 if( pGroupBy==0 ){
drhda932812000-06-06 18:00:15 +0000841 sqliteSetString(&pParse->zErrMsg, "a GROUP BY clause is required "
842 "before HAVING", 0);
drh22827922000-06-06 17:27:05 +0000843 pParse->nErr++;
844 return 1;
845 }
846 if( sqliteExprResolveIds(pParse, pTabList, pHaving) ){
847 return 1;
848 }
drhda932812000-06-06 18:00:15 +0000849 if( sqliteExprCheck(pParse, pHaving, isAgg, 0) ){
drh22827922000-06-06 17:27:05 +0000850 return 1;
851 }
drhcce7d172000-05-31 15:34:51 +0000852 }
853
drh22827922000-06-06 17:27:05 +0000854 /* Do an analysis of aggregate expressions.
drhefb72512000-05-31 20:00:52 +0000855 */
drh22827922000-06-06 17:27:05 +0000856 if( isAgg ){
drhaaf88722000-06-08 11:25:00 +0000857 assert( pParse->nAgg==0 && pParse->iAggCount<0 );
drh22827922000-06-06 17:27:05 +0000858 for(i=0; i<pEList->nExpr; i++){
859 if( sqliteExprAnalyzeAggregates(pParse, pEList->a[i].pExpr) ){
860 return 1;
861 }
862 }
863 if( pGroupBy ){
864 for(i=0; i<pGroupBy->nExpr; i++){
865 if( sqliteExprAnalyzeAggregates(pParse, pGroupBy->a[i].pExpr) ){
866 return 1;
867 }
868 }
869 }
870 if( pHaving && sqliteExprAnalyzeAggregates(pParse, pHaving) ){
871 return 1;
872 }
drh191b6902000-06-08 11:13:01 +0000873 if( pOrderBy ){
874 for(i=0; i<pOrderBy->nExpr; i++){
875 if( sqliteExprAnalyzeAggregates(pParse, pOrderBy->a[i].pExpr) ){
876 return 1;
877 }
878 }
879 }
drhefb72512000-05-31 20:00:52 +0000880 }
881
drhcce7d172000-05-31 15:34:51 +0000882 /* Begin generating code.
883 */
drhdaffd0e2001-04-11 14:28:42 +0000884 v = sqliteGetVdbe(pParse);
885 if( v==0 ) return 1;
drhcce7d172000-05-31 15:34:51 +0000886
drh9bbca4c2001-11-06 04:00:18 +0000887 /* Set the limiter
888 */
889 if( p->nLimit<=0 ){
890 p->nOffset = 0;
891 }else{
892 if( p->nOffset<0 ) p->nOffset = 0;
893 sqliteVdbeAddOp(v, OP_Limit, p->nLimit, p->nOffset);
894 }
895
896
drh22827922000-06-06 17:27:05 +0000897 /* Identify column names if we will be using in the callback. This
drh19a775c2000-06-05 18:54:46 +0000898 ** step is skipped if the output is going to a table or a memory cell.
drhcce7d172000-05-31 15:34:51 +0000899 */
drhfef52082000-06-06 01:50:43 +0000900 if( eDest==SRT_Callback ){
drhd8bc7082000-06-07 23:51:50 +0000901 generateColumnNames(pParse, pTabList, pEList);
drhcce7d172000-05-31 15:34:51 +0000902 }
903
drh22827922000-06-06 17:27:05 +0000904 /* Reset the aggregator
drhcce7d172000-05-31 15:34:51 +0000905 */
906 if( isAgg ){
drh99fcd712001-10-13 01:06:47 +0000907 sqliteVdbeAddOp(v, OP_AggReset, 0, pParse->nAgg);
drh1bee3d72001-10-15 00:44:35 +0000908 if( pGroupBy==0 ){
909 sqliteVdbeAddOp(v, OP_String, 0, 0);
910 sqliteVdbeChangeP3(v, -1, "", P3_STATIC);
911 sqliteVdbeAddOp(v, OP_AggFocus, 0, 0);
912 for(i=0; i<pParse->nAgg; i++){
913 Expr *pE;
914 if( !pParse->aAgg[i].isAgg ) continue;
915 pE = pParse->aAgg[i].pExpr;
916 assert( pE==0 || pE->op==TK_AGG_FUNCTION );
917 assert( pE==0 || (pE->pList!=0 && pE->pList->nExpr==1) );
918 if( pE==0 || pE->iColumn==FN_Sum ){
919 sqliteVdbeAddOp(v, OP_Integer, 0, 0);
920 sqliteVdbeAddOp(v, OP_AggSet, 0, i);
921 continue;
922 }
923 }
924 }
drhcce7d172000-05-31 15:34:51 +0000925 }
926
drh19a775c2000-06-05 18:54:46 +0000927 /* Initialize the memory cell to NULL
928 */
drhfef52082000-06-06 01:50:43 +0000929 if( eDest==SRT_Mem ){
drh99fcd712001-10-13 01:06:47 +0000930 sqliteVdbeAddOp(v, OP_String, 0, 0);
drh8721ce42001-11-07 14:22:00 +0000931 sqliteVdbeAddOp(v, OP_MemStore, iParm, 1);
drh19a775c2000-06-05 18:54:46 +0000932 }
933
drhcce7d172000-05-31 15:34:51 +0000934 /* Begin the database scan
drhefb72512000-05-31 20:00:52 +0000935 */
drh19a775c2000-06-05 18:54:46 +0000936 if( isDistinct ){
drh99fcd712001-10-13 01:06:47 +0000937 sqliteVdbeAddOp(v, OP_OpenTemp, distinct, 0);
drhefb72512000-05-31 20:00:52 +0000938 }
drhcce7d172000-05-31 15:34:51 +0000939 pWInfo = sqliteWhereBegin(pParse, pTabList, pWhere, 0);
drh9bb61fe2000-06-05 16:01:39 +0000940 if( pWInfo==0 ) return 1;
drhcce7d172000-05-31 15:34:51 +0000941
drh22827922000-06-06 17:27:05 +0000942 /* Use the standard inner loop if we are not dealing with
943 ** aggregates
drhcce7d172000-05-31 15:34:51 +0000944 */
drhda9d6c42000-05-31 18:20:14 +0000945 if( !isAgg ){
drh82c3d632000-06-06 21:56:07 +0000946 if( selectInnerLoop(pParse, pEList, 0, 0, pOrderBy, distinct, eDest, iParm,
drh22827922000-06-06 17:27:05 +0000947 pWInfo->iContinue, pWInfo->iBreak) ){
948 return 1;
drhda9d6c42000-05-31 18:20:14 +0000949 }
drhcce7d172000-05-31 15:34:51 +0000950 }
drhefb72512000-05-31 20:00:52 +0000951
drh22827922000-06-06 17:27:05 +0000952 /* If we are dealing with aggregates, then to the special aggregate
953 ** processing.
drhefb72512000-05-31 20:00:52 +0000954 */
drh22827922000-06-06 17:27:05 +0000955 else{
drh22827922000-06-06 17:27:05 +0000956 if( pGroupBy ){
drh1bee3d72001-10-15 00:44:35 +0000957 int lbl1;
drh22827922000-06-06 17:27:05 +0000958 for(i=0; i<pGroupBy->nExpr; i++){
959 sqliteExprCode(pParse, pGroupBy->a[i].pExpr);
960 }
drh99fcd712001-10-13 01:06:47 +0000961 sqliteVdbeAddOp(v, OP_MakeKey, pGroupBy->nExpr, 0);
drh1bee3d72001-10-15 00:44:35 +0000962 lbl1 = sqliteVdbeMakeLabel(v);
drh99fcd712001-10-13 01:06:47 +0000963 sqliteVdbeAddOp(v, OP_AggFocus, 0, lbl1);
drh22827922000-06-06 17:27:05 +0000964 for(i=0; i<pParse->nAgg; i++){
965 if( pParse->aAgg[i].isAgg ) continue;
966 sqliteExprCode(pParse, pParse->aAgg[i].pExpr);
drh99fcd712001-10-13 01:06:47 +0000967 sqliteVdbeAddOp(v, OP_AggSet, 0, i);
drhcce7d172000-05-31 15:34:51 +0000968 }
drh22827922000-06-06 17:27:05 +0000969 sqliteVdbeResolveLabel(v, lbl1);
drhcce7d172000-05-31 15:34:51 +0000970 }
drh22827922000-06-06 17:27:05 +0000971 for(i=0; i<pParse->nAgg; i++){
972 Expr *pE;
973 int op;
974 if( !pParse->aAgg[i].isAgg ) continue;
975 pE = pParse->aAgg[i].pExpr;
976 if( pE==0 ){
drh99fcd712001-10-13 01:06:47 +0000977 sqliteVdbeAddOp(v, OP_AggIncr, 1, i);
drh22827922000-06-06 17:27:05 +0000978 continue;
979 }
980 assert( pE->op==TK_AGG_FUNCTION );
981 assert( pE->pList!=0 && pE->pList->nExpr==1 );
982 sqliteExprCode(pParse, pE->pList->a[0].pExpr);
drh99fcd712001-10-13 01:06:47 +0000983 sqliteVdbeAddOp(v, OP_AggGet, 0, i);
drh967e8b72000-06-21 13:59:10 +0000984 switch( pE->iColumn ){
drh22827922000-06-06 17:27:05 +0000985 case FN_Min: op = OP_Min; break;
986 case FN_Max: op = OP_Max; break;
987 case FN_Avg: op = OP_Add; break;
988 case FN_Sum: op = OP_Add; break;
989 }
drh99fcd712001-10-13 01:06:47 +0000990 sqliteVdbeAddOp(v, op, 0, 0);
991 sqliteVdbeAddOp(v, OP_AggSet, 0, i);
drh22827922000-06-06 17:27:05 +0000992 }
drhcce7d172000-05-31 15:34:51 +0000993 }
994
drh22827922000-06-06 17:27:05 +0000995
drhcce7d172000-05-31 15:34:51 +0000996 /* End the database scan loop.
997 */
998 sqliteWhereEnd(pWInfo);
999
drh22827922000-06-06 17:27:05 +00001000 /* If we are processing aggregates, we need to set up a second loop
1001 ** over all of the aggregate values and process them.
1002 */
1003 if( isAgg ){
1004 int endagg = sqliteVdbeMakeLabel(v);
1005 int startagg;
drh99fcd712001-10-13 01:06:47 +00001006 startagg = sqliteVdbeAddOp(v, OP_AggNext, 0, endagg);
drh22827922000-06-06 17:27:05 +00001007 pParse->useAgg = 1;
1008 if( pHaving ){
1009 sqliteExprIfFalse(pParse, pHaving, startagg);
1010 }
drh82c3d632000-06-06 21:56:07 +00001011 if( selectInnerLoop(pParse, pEList, 0, 0, pOrderBy, distinct, eDest, iParm,
drh22827922000-06-06 17:27:05 +00001012 startagg, endagg) ){
1013 return 1;
1014 }
drh99fcd712001-10-13 01:06:47 +00001015 sqliteVdbeAddOp(v, OP_Goto, 0, startagg);
1016 sqliteVdbeResolveLabel(v, endagg);
1017 sqliteVdbeAddOp(v, OP_Noop, 0, 0);
drh22827922000-06-06 17:27:05 +00001018 pParse->useAgg = 0;
1019 }
1020
drhcce7d172000-05-31 15:34:51 +00001021 /* If there is an ORDER BY clause, then we need to sort the results
1022 ** and send them to the callback one by one.
1023 */
1024 if( pOrderBy ){
drhd8bc7082000-06-07 23:51:50 +00001025 generateSortTail(v, pEList->nExpr);
drhcce7d172000-05-31 15:34:51 +00001026 }
drh10e5e3c2000-06-08 00:19:02 +00001027 pParse->nTab = base;
drh6a535342001-10-19 16:44:56 +00001028
1029
1030 /* Issue a null callback if that is what the user wants.
1031 */
1032 if( (pParse->db->flags & SQLITE_NullCallback)!=0 && eDest==SRT_Callback ){
1033 sqliteVdbeAddOp(v, OP_NullCallback, pEList->nExpr, 0);
1034 }
1035
drh9bb61fe2000-06-05 16:01:39 +00001036 return 0;
drhcce7d172000-05-31 15:34:51 +00001037}