drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1 | /* |
| 2 | ** Copyright (c) 1999, 2000 D. Richard Hipp |
| 3 | ** |
| 4 | ** This program is free software; you can redistribute it and/or |
| 5 | ** modify it under the terms of the GNU General Public |
| 6 | ** License as published by the Free Software Foundation; either |
| 7 | ** version 2 of the License, or (at your option) any later version. |
| 8 | ** |
| 9 | ** This program is distributed in the hope that it will be useful, |
| 10 | ** but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | ** General Public License for more details. |
| 13 | ** |
| 14 | ** You should have received a copy of the GNU General Public |
| 15 | ** License along with this library; if not, write to the |
| 16 | ** Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 17 | ** Boston, MA 02111-1307, USA. |
| 18 | ** |
| 19 | ** Author contact information: |
| 20 | ** drh@hwaci.com |
| 21 | ** http://www.hwaci.com/drh/ |
| 22 | ** |
| 23 | ************************************************************************* |
| 24 | ** This file contains C code routines that are called by the parser |
| 25 | ** to handle SELECT statements. |
| 26 | ** |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame^] | 27 | ** $Id: select.c,v 1.30 2001/04/04 11:48:58 drh Exp $ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 28 | */ |
| 29 | #include "sqliteInt.h" |
| 30 | |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 31 | /* |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 32 | ** Allocate a new Select structure and return a pointer to that |
| 33 | ** structure. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 34 | */ |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 35 | Select *sqliteSelectNew( |
| 36 | ExprList *pEList, |
| 37 | IdList *pSrc, |
| 38 | Expr *pWhere, |
| 39 | ExprList *pGroupBy, |
| 40 | Expr *pHaving, |
| 41 | ExprList *pOrderBy, |
| 42 | int isDistinct |
| 43 | ){ |
| 44 | Select *pNew; |
| 45 | pNew = sqliteMalloc( sizeof(*pNew) ); |
| 46 | if( pNew==0 ) return 0; |
| 47 | pNew->pEList = pEList; |
| 48 | pNew->pSrc = pSrc; |
| 49 | pNew->pWhere = pWhere; |
| 50 | pNew->pGroupBy = pGroupBy; |
| 51 | pNew->pHaving = pHaving; |
| 52 | pNew->pOrderBy = pOrderBy; |
| 53 | pNew->isDistinct = isDistinct; |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 54 | pNew->op = TK_SELECT; |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 55 | return pNew; |
| 56 | } |
| 57 | |
| 58 | /* |
| 59 | ** Delete the given Select structure and all of its substructures. |
| 60 | */ |
| 61 | void sqliteSelectDelete(Select *p){ |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 62 | if( p==0 ) return; |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 63 | sqliteExprListDelete(p->pEList); |
| 64 | sqliteIdListDelete(p->pSrc); |
| 65 | sqliteExprDelete(p->pWhere); |
| 66 | sqliteExprListDelete(p->pGroupBy); |
| 67 | sqliteExprDelete(p->pHaving); |
| 68 | sqliteExprListDelete(p->pOrderBy); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 69 | sqliteSelectDelete(p->pPrior); |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 70 | sqliteFree(p); |
| 71 | } |
| 72 | |
| 73 | /* |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 74 | ** Delete the aggregate information from the parse structure. |
| 75 | */ |
| 76 | void 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. |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 87 | ** |
| 88 | ** The pEList is used to determine the values for each column in the |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 89 | ** result row. Except if pEList==NULL, then we just read nColumn |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 90 | ** elements from the srcTab table. |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 91 | */ |
| 92 | static int selectInnerLoop( |
| 93 | Parse *pParse, /* The parser context */ |
| 94 | ExprList *pEList, /* List of values being extracted */ |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 95 | int srcTab, /* Pull data from this table */ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 96 | int nColumn, /* Number of columns in the source table */ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 97 | 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; |
| 106 | |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 107 | /* Pull the requested columns. |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 108 | */ |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 109 | if( pEList ){ |
| 110 | for(i=0; i<pEList->nExpr; i++){ |
| 111 | sqliteExprCode(pParse, pEList->a[i].pExpr); |
| 112 | } |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 113 | nColumn = pEList->nExpr; |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 114 | }else{ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 115 | for(i=0; i<nColumn; i++){ |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 116 | sqliteVdbeAddOp(v, OP_Field, srcTab, i, 0, 0); |
| 117 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | /* If the current result is not distinct, skip the rest |
| 121 | ** of the processing for the current row. |
| 122 | */ |
| 123 | if( distinct>=0 ){ |
| 124 | int lbl = sqliteVdbeMakeLabel(v); |
| 125 | sqliteVdbeAddOp(v, OP_MakeKey, pEList->nExpr, 1, 0, 0); |
| 126 | sqliteVdbeAddOp(v, OP_Distinct, distinct, lbl, 0, 0); |
| 127 | sqliteVdbeAddOp(v, OP_Pop, pEList->nExpr+1, 0, 0, 0); |
| 128 | sqliteVdbeAddOp(v, OP_Goto, 0, iContinue, 0, 0); |
| 129 | sqliteVdbeAddOp(v, OP_String, 0, 0, "", lbl); |
| 130 | sqliteVdbeAddOp(v, OP_Put, distinct, 0, 0, 0); |
| 131 | } |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 132 | |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 133 | /* If there is an ORDER BY clause, then store the results |
| 134 | ** in a sorter. |
| 135 | */ |
| 136 | if( pOrderBy ){ |
| 137 | char *zSortOrder; |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 138 | sqliteVdbeAddOp(v, OP_SortMakeRec, nColumn, 0, 0, 0); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 139 | zSortOrder = sqliteMalloc( pOrderBy->nExpr + 1 ); |
| 140 | if( zSortOrder==0 ) return 1; |
| 141 | for(i=0; i<pOrderBy->nExpr; i++){ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 142 | zSortOrder[i] = pOrderBy->a[i].sortOrder ? '-' : '+'; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 143 | sqliteExprCode(pParse, pOrderBy->a[i].pExpr); |
| 144 | } |
| 145 | zSortOrder[pOrderBy->nExpr] = 0; |
| 146 | sqliteVdbeAddOp(v, OP_SortMakeKey, pOrderBy->nExpr, 0, zSortOrder, 0); |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 147 | sqliteFree(zSortOrder); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 148 | sqliteVdbeAddOp(v, OP_SortPut, 0, 0, 0, 0); |
| 149 | }else |
| 150 | |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 151 | /* In this mode, write each query result to the key of the temporary |
| 152 | ** table iParm. |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 153 | */ |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 154 | if( eDest==SRT_Union ){ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 155 | sqliteVdbeAddOp(v, OP_MakeRecord, nColumn, 0, 0, 0); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 156 | sqliteVdbeAddOp(v, OP_String, iParm, 0, "", 0); |
| 157 | sqliteVdbeAddOp(v, OP_Put, iParm, 0, 0, 0); |
| 158 | }else |
| 159 | |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 160 | /* Store the result as data using a unique key. |
| 161 | */ |
| 162 | if( eDest==SRT_Table ){ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 163 | sqliteVdbeAddOp(v, OP_MakeRecord, nColumn, 0, 0, 0); |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 164 | sqliteVdbeAddOp(v, OP_New, iParm, 0, 0, 0); |
| 165 | sqliteVdbeAddOp(v, OP_Pull, 1, 0, 0, 0); |
| 166 | sqliteVdbeAddOp(v, OP_Put, iParm, 0, 0, 0); |
| 167 | }else |
| 168 | |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 169 | /* Construct a record from the query result, but instead of |
| 170 | ** saving that record, use it as a key to delete elements from |
| 171 | ** the temporary table iParm. |
| 172 | */ |
| 173 | if( eDest==SRT_Except ){ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 174 | sqliteVdbeAddOp(v, OP_MakeRecord, nColumn, 0, 0, 0); |
drh | 1ecec3c | 2000-06-06 22:13:55 +0000 | [diff] [blame] | 175 | sqliteVdbeAddOp(v, OP_Delete, iParm, 0, 0, 0); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 176 | }else |
| 177 | |
| 178 | /* If we are creating a set for an "expr IN (SELECT ...)" construct, |
| 179 | ** then there should be a single item on the stack. Write this |
| 180 | ** item into the set table with bogus data. |
| 181 | */ |
| 182 | if( eDest==SRT_Set ){ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 183 | assert( nColumn==1 ); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 184 | sqliteVdbeAddOp(v, OP_String, 0, 0, "", 0); |
| 185 | sqliteVdbeAddOp(v, OP_Put, iParm, 0, 0, 0); |
| 186 | }else |
| 187 | |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 188 | |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 189 | /* If this is a scalar select that is part of an expression, then |
| 190 | ** store the results in the appropriate memory cell and break out |
| 191 | ** of the scan loop. |
| 192 | */ |
| 193 | if( eDest==SRT_Mem ){ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 194 | assert( nColumn==1 ); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 195 | sqliteVdbeAddOp(v, OP_MemStore, iParm, 0, 0, 0); |
| 196 | sqliteVdbeAddOp(v, OP_Goto, 0, iBreak, 0, 0); |
| 197 | }else |
| 198 | |
| 199 | /* If none of the above, send the data to the callback function. |
| 200 | */ |
| 201 | { |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 202 | sqliteVdbeAddOp(v, OP_Callback, nColumn, 0, 0, 0); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 203 | } |
| 204 | return 0; |
| 205 | } |
| 206 | |
| 207 | /* |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 208 | ** If the inner loop was generated using a non-null pOrderBy argument, |
| 209 | ** then the results were placed in a sorter. After the loop is terminated |
| 210 | ** we need to run the sorter and output the results. The following |
| 211 | ** routine generates the code needed to do that. |
| 212 | */ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 213 | static void generateSortTail(Vdbe *v, int nColumn){ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 214 | int end = sqliteVdbeMakeLabel(v); |
| 215 | int addr; |
| 216 | sqliteVdbeAddOp(v, OP_Sort, 0, 0, 0, 0); |
| 217 | addr = sqliteVdbeAddOp(v, OP_SortNext, 0, end, 0, 0); |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 218 | sqliteVdbeAddOp(v, OP_SortCallback, nColumn, 0, 0, 0); |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 219 | sqliteVdbeAddOp(v, OP_Goto, 0, addr, 0, 0); |
| 220 | sqliteVdbeAddOp(v, OP_SortClose, 0, 0, 0, end); |
| 221 | } |
| 222 | |
| 223 | /* |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 224 | ** Generate code that will tell the VDBE how many columns there |
| 225 | ** are in the result and the name for each column. This information |
| 226 | ** is used to provide "argc" and "azCol[]" values in the callback. |
| 227 | */ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 228 | static |
| 229 | void generateColumnNames(Parse *pParse, IdList *pTabList, ExprList *pEList){ |
| 230 | Vdbe *v = pParse->pVdbe; |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 231 | int i; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 232 | if( pParse->colNamesSet ) return; |
| 233 | pParse->colNamesSet = 1; |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 234 | sqliteVdbeAddOp(v, OP_ColumnCount, pEList->nExpr, 0, 0, 0); |
| 235 | for(i=0; i<pEList->nExpr; i++){ |
| 236 | Expr *p; |
drh | e1b6a5b | 2000-07-29 13:06:59 +0000 | [diff] [blame] | 237 | int addr; |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 238 | if( pEList->a[i].zName ){ |
| 239 | char *zName = pEList->a[i].zName; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 240 | sqliteVdbeAddOp(v, OP_ColumnName, i, 0, zName, 0); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 241 | continue; |
| 242 | } |
| 243 | p = pEList->a[i].pExpr; |
drh | e1b6a5b | 2000-07-29 13:06:59 +0000 | [diff] [blame] | 244 | if( p->span.z && p->span.z[0] ){ |
| 245 | addr = sqliteVdbeAddOp(v,OP_ColumnName, i, 0, 0, 0); |
| 246 | sqliteVdbeChangeP3(v, addr, p->span.z, p->span.n); |
| 247 | sqliteVdbeCompressSpace(v, addr); |
| 248 | }else if( p->op!=TK_COLUMN || pTabList==0 ){ |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 249 | char zName[30]; |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 250 | sprintf(zName, "column%d", i+1); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 251 | sqliteVdbeAddOp(v, OP_ColumnName, i, 0, zName, 0); |
| 252 | }else{ |
| 253 | if( pTabList->nId>1 ){ |
| 254 | char *zName = 0; |
| 255 | Table *pTab = pTabList->a[p->iTable].pTab; |
| 256 | char *zTab; |
| 257 | |
| 258 | zTab = pTabList->a[p->iTable].zAlias; |
| 259 | if( zTab==0 ) zTab = pTab->zName; |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 260 | sqliteSetString(&zName, zTab, ".", pTab->aCol[p->iColumn].zName, 0); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 261 | sqliteVdbeAddOp(v, OP_ColumnName, i, 0, zName, 0); |
| 262 | sqliteFree(zName); |
| 263 | }else{ |
| 264 | Table *pTab = pTabList->a[0].pTab; |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 265 | char *zName = pTab->aCol[p->iColumn].zName; |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 266 | sqliteVdbeAddOp(v, OP_ColumnName, i, 0, zName, 0); |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | /* |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 273 | ** Name of the connection operator, used for error messages. |
| 274 | */ |
| 275 | static const char *selectOpName(int id){ |
| 276 | char *z; |
| 277 | switch( id ){ |
| 278 | case TK_ALL: z = "UNION ALL"; break; |
| 279 | case TK_INTERSECT: z = "INTERSECT"; break; |
| 280 | case TK_EXCEPT: z = "EXCEPT"; break; |
| 281 | default: z = "UNION"; break; |
| 282 | } |
| 283 | return z; |
| 284 | } |
| 285 | |
| 286 | /* |
| 287 | ** For the given SELECT statement, do two things. |
| 288 | ** |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 289 | ** (1) Fill in the pTabList->a[].pTab fields in the IdList that |
| 290 | ** defines the set of tables that should be scanned. |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 291 | ** |
| 292 | ** (2) If the columns to be extracted variable (pEList) is NULL |
| 293 | ** (meaning that a "*" was used in the SQL statement) then |
| 294 | ** create a fake pEList containing the names of all columns |
| 295 | ** of all tables. |
| 296 | ** |
| 297 | ** Return 0 on success. If there are problems, leave an error message |
| 298 | ** in pParse and return non-zero. |
| 299 | */ |
| 300 | static int fillInColumnList(Parse *pParse, Select *p){ |
| 301 | int i, j; |
| 302 | IdList *pTabList = p->pSrc; |
| 303 | ExprList *pEList = p->pEList; |
| 304 | |
| 305 | /* Look up every table in the table list. |
| 306 | */ |
| 307 | for(i=0; i<pTabList->nId; i++){ |
| 308 | if( pTabList->a[i].pTab ){ |
| 309 | /* This routine has run before! No need to continue */ |
| 310 | return 0; |
| 311 | } |
| 312 | pTabList->a[i].pTab = sqliteFindTable(pParse->db, pTabList->a[i].zName); |
| 313 | if( pTabList->a[i].pTab==0 ){ |
| 314 | sqliteSetString(&pParse->zErrMsg, "no such table: ", |
| 315 | pTabList->a[i].zName, 0); |
| 316 | pParse->nErr++; |
| 317 | return 1; |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | /* If the list of columns to retrieve is "*" then replace it with |
| 322 | ** a list of all columns from all tables. |
| 323 | */ |
| 324 | if( pEList==0 ){ |
| 325 | for(i=0; i<pTabList->nId; i++){ |
| 326 | Table *pTab = pTabList->a[i].pTab; |
| 327 | for(j=0; j<pTab->nCol; j++){ |
| 328 | Expr *pExpr = sqliteExpr(TK_DOT, 0, 0, 0); |
| 329 | pExpr->pLeft = sqliteExpr(TK_ID, 0, 0, 0); |
| 330 | pExpr->pLeft->token.z = pTab->zName; |
| 331 | pExpr->pLeft->token.n = strlen(pTab->zName); |
| 332 | pExpr->pRight = sqliteExpr(TK_ID, 0, 0, 0); |
| 333 | pExpr->pRight->token.z = pTab->aCol[j].zName; |
| 334 | pExpr->pRight->token.n = strlen(pTab->aCol[j].zName); |
drh | e1b6a5b | 2000-07-29 13:06:59 +0000 | [diff] [blame] | 335 | pExpr->span.z = ""; |
| 336 | pExpr->span.n = 0; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 337 | pEList = sqliteExprListAppend(pEList, pExpr, 0); |
| 338 | } |
| 339 | } |
| 340 | p->pEList = pEList; |
| 341 | } |
| 342 | return 0; |
| 343 | } |
| 344 | |
| 345 | /* |
| 346 | ** This routine associates entries in an ORDER BY expression list with |
| 347 | ** columns in a result. For each ORDER BY expression, the opcode of |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 348 | ** the top-level node is changed to TK_COLUMN and the iColumn value of |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 349 | ** the top-level node is filled in with column number and the iTable |
| 350 | ** value of the top-level node is filled with iTable parameter. |
| 351 | ** |
| 352 | ** If there are prior SELECT clauses, they are processed first. A match |
| 353 | ** in an earlier SELECT takes precedence over a later SELECT. |
| 354 | ** |
| 355 | ** Any entry that does not match is flagged as an error. The number |
| 356 | ** of errors is returned. |
| 357 | */ |
| 358 | static int matchOrderbyToColumn( |
| 359 | Parse *pParse, /* A place to leave error messages */ |
| 360 | Select *pSelect, /* Match to result columns of this SELECT */ |
| 361 | ExprList *pOrderBy, /* The ORDER BY values to match against columns */ |
| 362 | int iTable, /* Insert this this value in iTable */ |
| 363 | int mustComplete /* If TRUE all ORDER BYs must match */ |
| 364 | ){ |
| 365 | int nErr = 0; |
| 366 | int i, j; |
| 367 | ExprList *pEList; |
| 368 | |
| 369 | assert( pSelect && pOrderBy ); |
| 370 | if( mustComplete ){ |
| 371 | for(i=0; i<pOrderBy->nExpr; i++){ pOrderBy->a[i].done = 0; } |
| 372 | } |
| 373 | if( fillInColumnList(pParse, pSelect) ){ |
| 374 | return 1; |
| 375 | } |
| 376 | if( pSelect->pPrior ){ |
drh | 92cd52f | 2000-06-08 01:55:29 +0000 | [diff] [blame] | 377 | if( matchOrderbyToColumn(pParse, pSelect->pPrior, pOrderBy, iTable, 0) ){ |
| 378 | return 1; |
| 379 | } |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 380 | } |
| 381 | pEList = pSelect->pEList; |
| 382 | for(i=0; i<pOrderBy->nExpr; i++){ |
| 383 | Expr *pE = pOrderBy->a[i].pExpr; |
drh | 92cd52f | 2000-06-08 01:55:29 +0000 | [diff] [blame] | 384 | int match = 0; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 385 | if( pOrderBy->a[i].done ) continue; |
| 386 | for(j=0; j<pEList->nExpr; j++){ |
drh | 4cfa793 | 2000-06-08 15:10:46 +0000 | [diff] [blame] | 387 | if( pEList->a[j].zName && (pE->op==TK_ID || pE->op==TK_STRING) ){ |
| 388 | char *zName = pEList->a[j].zName; |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 389 | char *zLabel = sqliteStrNDup(pE->token.z, pE->token.n); |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 390 | sqliteDequote(zLabel); |
| 391 | if( sqliteStrICmp(zName, zLabel)==0 ){ |
| 392 | match = 1; |
| 393 | } |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 394 | sqliteFree(zLabel); |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 395 | } |
drh | 4cfa793 | 2000-06-08 15:10:46 +0000 | [diff] [blame] | 396 | if( match==0 && sqliteExprCompare(pE, pEList->a[j].pExpr) ){ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 397 | match = 1; |
| 398 | } |
| 399 | if( match ){ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 400 | pE->op = TK_COLUMN; |
| 401 | pE->iColumn = j; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 402 | pE->iTable = iTable; |
| 403 | pOrderBy->a[i].done = 1; |
| 404 | break; |
| 405 | } |
| 406 | } |
drh | 92cd52f | 2000-06-08 01:55:29 +0000 | [diff] [blame] | 407 | if( !match && mustComplete ){ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 408 | char zBuf[30]; |
| 409 | sprintf(zBuf,"%d",i+1); |
| 410 | sqliteSetString(&pParse->zErrMsg, "ORDER BY term number ", zBuf, |
| 411 | " does not match any result column", 0); |
| 412 | pParse->nErr++; |
| 413 | nErr++; |
| 414 | break; |
| 415 | } |
| 416 | } |
| 417 | return nErr; |
| 418 | } |
| 419 | |
| 420 | /* |
| 421 | ** Get a VDBE for the given parser context. Create a new one if necessary. |
| 422 | ** If an error occurs, return NULL and leave a message in pParse. |
| 423 | */ |
| 424 | Vdbe *sqliteGetVdbe(Parse *pParse){ |
| 425 | Vdbe *v = pParse->pVdbe; |
| 426 | if( v==0 ){ |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 427 | v = pParse->pVdbe = sqliteVdbeCreate(pParse->db); |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 428 | } |
| 429 | if( v==0 ){ |
| 430 | sqliteSetString(&pParse->zErrMsg, "out of memory", 0); |
| 431 | pParse->nErr++; |
| 432 | } |
| 433 | return v; |
| 434 | } |
| 435 | |
| 436 | |
| 437 | /* |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 438 | ** This routine is called to process a query that is really the union |
| 439 | ** or intersection of two or more separate queries. |
| 440 | */ |
| 441 | static int multiSelect(Parse *pParse, Select *p, int eDest, int iParm){ |
drh | 10e5e3c | 2000-06-08 00:19:02 +0000 | [diff] [blame] | 442 | int rc; /* Success code from a subroutine */ |
| 443 | Select *pPrior; /* Another SELECT immediately to our left */ |
| 444 | Vdbe *v; /* Generate code to this VDBE */ |
| 445 | int base; /* Baseline value for pParse->nTab */ |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 446 | |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 447 | /* Make sure there is no ORDER BY clause on prior SELECTs. Only the |
| 448 | ** last SELECT in the series may have an ORDER BY. |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 449 | */ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 450 | assert( p->pPrior!=0 ); |
| 451 | pPrior = p->pPrior; |
| 452 | if( pPrior->pOrderBy ){ |
| 453 | sqliteSetString(&pParse->zErrMsg,"ORDER BY clause should come after ", |
| 454 | selectOpName(p->op), " not before", 0); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 455 | pParse->nErr++; |
| 456 | return 1; |
| 457 | } |
| 458 | |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 459 | /* Make sure we have a valid query engine. If not, create a new one. |
| 460 | */ |
| 461 | v = sqliteGetVdbe(pParse); |
| 462 | if( v==0 ) return 1; |
| 463 | |
| 464 | /* Process the UNION or INTERSECTION |
| 465 | */ |
drh | 10e5e3c | 2000-06-08 00:19:02 +0000 | [diff] [blame] | 466 | base = pParse->nTab; |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 467 | switch( p->op ){ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 468 | case TK_ALL: |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 469 | case TK_EXCEPT: |
| 470 | case TK_UNION: { |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 471 | int unionTab; /* Cursor number of the temporary table holding result */ |
| 472 | int op; /* One of the SRT_ operations to apply to self */ |
| 473 | int priorOp; /* The SRT_ operation to apply to prior selects */ |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 474 | |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 475 | priorOp = p->op==TK_ALL ? SRT_Table : SRT_Union; |
| 476 | if( eDest==priorOp ){ |
| 477 | /* We can reuse a temporary table generated by a SELECT to our |
| 478 | ** right. This also means we are not the right-most select and so |
| 479 | ** we cannot have an ORDER BY clause |
| 480 | */ |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 481 | unionTab = iParm; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 482 | assert( p->pOrderBy==0 ); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 483 | }else{ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 484 | /* We will need to create our own temporary table to hold the |
| 485 | ** intermediate results. |
| 486 | */ |
| 487 | unionTab = pParse->nTab++; |
| 488 | if( p->pOrderBy |
| 489 | && matchOrderbyToColumn(pParse, p, p->pOrderBy, unionTab, 1) ){ |
| 490 | return 1; |
| 491 | } |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 492 | if( p->op!=TK_ALL ){ |
drh | 345fda3 | 2001-01-15 22:51:08 +0000 | [diff] [blame] | 493 | sqliteVdbeAddOp(v, OP_OpenIdx, unionTab, 1, 0, 0); |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 494 | sqliteVdbeAddOp(v, OP_KeyAsData, unionTab, 1, 0, 0); |
drh | 345fda3 | 2001-01-15 22:51:08 +0000 | [diff] [blame] | 495 | }else{ |
| 496 | sqliteVdbeAddOp(v, OP_OpenTbl, unionTab, 1, 0, 0); |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 497 | } |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 498 | } |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 499 | |
| 500 | /* Code the SELECT statements to our left |
| 501 | */ |
| 502 | rc = sqliteSelect(pParse, pPrior, priorOp, unionTab); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 503 | if( rc ) return rc; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 504 | |
| 505 | /* Code the current SELECT statement |
| 506 | */ |
| 507 | switch( p->op ){ |
| 508 | case TK_EXCEPT: op = SRT_Except; break; |
| 509 | case TK_UNION: op = SRT_Union; break; |
| 510 | case TK_ALL: op = SRT_Table; break; |
| 511 | } |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 512 | p->pPrior = 0; |
| 513 | rc = sqliteSelect(pParse, p, op, unionTab); |
| 514 | p->pPrior = pPrior; |
| 515 | if( rc ) return rc; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 516 | |
| 517 | /* Convert the data in the temporary table into whatever form |
| 518 | ** it is that we currently need. |
| 519 | */ |
| 520 | if( eDest!=priorOp ){ |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 521 | int iCont, iBreak; |
| 522 | assert( p->pEList ); |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 523 | generateColumnNames(pParse, 0, p->pEList); |
drh | 92dba24 | 2000-06-08 00:28:51 +0000 | [diff] [blame] | 524 | if( p->pOrderBy ){ |
| 525 | sqliteVdbeAddOp(v, OP_SortOpen, 0, 0, 0, 0); |
| 526 | } |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 527 | iBreak = sqliteVdbeMakeLabel(v); |
| 528 | iCont = sqliteVdbeAddOp(v, OP_Next, unionTab, iBreak, 0, 0); |
| 529 | rc = selectInnerLoop(pParse, 0, unionTab, p->pEList->nExpr, |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 530 | p->pOrderBy, -1, eDest, iParm, |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 531 | iCont, iBreak); |
| 532 | if( rc ) return 1; |
| 533 | sqliteVdbeAddOp(v, OP_Goto, 0, iCont, 0, 0); |
| 534 | sqliteVdbeAddOp(v, OP_Close, unionTab, 0, 0, iBreak); |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 535 | if( p->pOrderBy ){ |
| 536 | generateSortTail(v, p->pEList->nExpr); |
| 537 | } |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 538 | } |
| 539 | break; |
| 540 | } |
| 541 | case TK_INTERSECT: { |
| 542 | int tab1, tab2; |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 543 | int iCont, iBreak; |
| 544 | |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 545 | /* INTERSECT is different from the others since it requires |
drh | 6206d50 | 2000-06-19 19:09:08 +0000 | [diff] [blame] | 546 | ** two temporary tables. Hence it has its own case. Begin |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 547 | ** by allocating the tables we will need. |
| 548 | */ |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 549 | tab1 = pParse->nTab++; |
| 550 | tab2 = pParse->nTab++; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 551 | if( p->pOrderBy && matchOrderbyToColumn(pParse,p,p->pOrderBy,tab1,1) ){ |
| 552 | return 1; |
| 553 | } |
drh | 345fda3 | 2001-01-15 22:51:08 +0000 | [diff] [blame] | 554 | sqliteVdbeAddOp(v, OP_OpenIdx, tab1, 1, 0, 0); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 555 | sqliteVdbeAddOp(v, OP_KeyAsData, tab1, 1, 0, 0); |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 556 | |
| 557 | /* Code the SELECTs to our left into temporary table "tab1". |
| 558 | */ |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 559 | rc = sqliteSelect(pParse, pPrior, SRT_Union, tab1); |
| 560 | if( rc ) return rc; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 561 | |
| 562 | /* Code the current SELECT into temporary table "tab2" |
| 563 | */ |
drh | 345fda3 | 2001-01-15 22:51:08 +0000 | [diff] [blame] | 564 | sqliteVdbeAddOp(v, OP_OpenIdx, tab2, 1, 0, 0); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 565 | sqliteVdbeAddOp(v, OP_KeyAsData, tab2, 1, 0, 0); |
| 566 | p->pPrior = 0; |
| 567 | rc = sqliteSelect(pParse, p, SRT_Union, tab2); |
| 568 | p->pPrior = pPrior; |
| 569 | if( rc ) return rc; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 570 | |
| 571 | /* Generate code to take the intersection of the two temporary |
| 572 | ** tables. |
| 573 | */ |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 574 | assert( p->pEList ); |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 575 | generateColumnNames(pParse, 0, p->pEList); |
drh | 92dba24 | 2000-06-08 00:28:51 +0000 | [diff] [blame] | 576 | if( p->pOrderBy ){ |
| 577 | sqliteVdbeAddOp(v, OP_SortOpen, 0, 0, 0, 0); |
| 578 | } |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 579 | iBreak = sqliteVdbeMakeLabel(v); |
| 580 | iCont = sqliteVdbeAddOp(v, OP_Next, tab1, iBreak, 0, 0); |
drh | 573bd27 | 2001-02-19 23:23:38 +0000 | [diff] [blame] | 581 | sqliteVdbeAddOp(v, OP_FullKey, tab1, 0, 0, 0); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 582 | sqliteVdbeAddOp(v, OP_NotFound, tab2, iCont, 0, 0); |
| 583 | rc = selectInnerLoop(pParse, 0, tab1, p->pEList->nExpr, |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 584 | p->pOrderBy, -1, eDest, iParm, |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 585 | iCont, iBreak); |
| 586 | if( rc ) return 1; |
| 587 | sqliteVdbeAddOp(v, OP_Goto, 0, iCont, 0, 0); |
| 588 | sqliteVdbeAddOp(v, OP_Close, tab2, 0, 0, iBreak); |
| 589 | sqliteVdbeAddOp(v, OP_Close, tab1, 0, 0, 0); |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 590 | if( p->pOrderBy ){ |
| 591 | generateSortTail(v, p->pEList->nExpr); |
| 592 | } |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 593 | break; |
| 594 | } |
| 595 | } |
| 596 | assert( p->pEList && pPrior->pEList ); |
| 597 | if( p->pEList->nExpr!=pPrior->pEList->nExpr ){ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 598 | sqliteSetString(&pParse->zErrMsg, "SELECTs to the left and right of ", |
| 599 | selectOpName(p->op), " do not have the same number of result columns", 0); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 600 | pParse->nErr++; |
| 601 | return 1; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 602 | } |
drh | 10e5e3c | 2000-06-08 00:19:02 +0000 | [diff] [blame] | 603 | pParse->nTab = base; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 604 | return 0; |
| 605 | } |
| 606 | |
| 607 | /* |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 608 | ** Generate code for the given SELECT statement. |
| 609 | ** |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 610 | ** The results are distributed in various ways depending on the |
| 611 | ** value of eDest and iParm. |
| 612 | ** |
| 613 | ** eDest Value Result |
| 614 | ** ------------ ------------------------------------------- |
| 615 | ** SRT_Callback Invoke the callback for each row of the result. |
| 616 | ** |
| 617 | ** SRT_Mem Store first result in memory cell iParm |
| 618 | ** |
| 619 | ** SRT_Set Store results as keys of a table with cursor iParm |
| 620 | ** |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 621 | ** SRT_Union Store results as a key in a temporary table iParm |
| 622 | ** |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame^] | 623 | ** SRT_Except Remove results form the temporary table iParm. |
| 624 | ** |
| 625 | ** SRT_Table Store results in temporary table iParm |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 626 | ** |
| 627 | ** This routine returns the number of errors. If any errors are |
| 628 | ** encountered, then an appropriate error message is left in |
| 629 | ** pParse->zErrMsg. |
| 630 | ** |
| 631 | ** This routine does NOT free the Select structure passed in. The |
| 632 | ** calling function needs to do that. |
| 633 | */ |
| 634 | int sqliteSelect( |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 635 | Parse *pParse, /* The parser context */ |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 636 | Select *p, /* The SELECT statement being coded. */ |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 637 | int eDest, /* One of: SRT_Callback Mem Set Union Except */ |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 638 | int iParm /* Save result in this memory location, if >=0 */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 639 | ){ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 640 | int i; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 641 | WhereInfo *pWInfo; |
| 642 | Vdbe *v; |
| 643 | int isAgg = 0; /* True for select lists like "count(*)" */ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 644 | ExprList *pEList; /* List of columns to extract. NULL means "*" */ |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 645 | IdList *pTabList; /* List of tables to select from */ |
| 646 | Expr *pWhere; /* The WHERE clause. May be NULL */ |
| 647 | ExprList *pOrderBy; /* The ORDER BY clause. May be NULL */ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 648 | ExprList *pGroupBy; /* The GROUP BY clause. May be NULL */ |
| 649 | Expr *pHaving; /* The HAVING clause. May be NULL */ |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 650 | int isDistinct; /* True if the DISTINCT keyword is present */ |
| 651 | int distinct; /* Table to use for the distinct set */ |
drh | 10e5e3c | 2000-06-08 00:19:02 +0000 | [diff] [blame] | 652 | int base; /* First cursor available for use */ |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 653 | |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 654 | /* If there is are a sequence of queries, do the earlier ones first. |
| 655 | */ |
| 656 | if( p->pPrior ){ |
| 657 | return multiSelect(pParse, p, eDest, iParm); |
| 658 | } |
| 659 | |
| 660 | /* Make local copies of the parameters for this query. |
| 661 | */ |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 662 | pTabList = p->pSrc; |
| 663 | pWhere = p->pWhere; |
| 664 | pOrderBy = p->pOrderBy; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 665 | pGroupBy = p->pGroupBy; |
| 666 | pHaving = p->pHaving; |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 667 | isDistinct = p->isDistinct; |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 668 | |
drh | 10e5e3c | 2000-06-08 00:19:02 +0000 | [diff] [blame] | 669 | /* Save the current value of pParse->nTab. Restore this value before |
| 670 | ** we exit. |
| 671 | */ |
| 672 | base = pParse->nTab; |
| 673 | |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 674 | /* |
| 675 | ** Do not even attempt to generate any code if we have already seen |
| 676 | ** errors before this routine starts. |
| 677 | */ |
drh | 10e5e3c | 2000-06-08 00:19:02 +0000 | [diff] [blame] | 678 | if( pParse->nErr>0 ) return 1; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 679 | sqliteParseInfoReset(pParse); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 680 | |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 681 | /* Look up every table in the table list and create an appropriate |
| 682 | ** columnlist in pEList if there isn't one already. (The parser leaves |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 683 | ** a NULL in the p->pEList if the SQL said "SELECT * FROM ...") |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 684 | */ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 685 | if( fillInColumnList(pParse, p) ){ |
| 686 | return 1; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 687 | } |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 688 | pEList = p->pEList; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 689 | |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 690 | /* Allocate a temporary table to use for the DISTINCT set, if |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 691 | ** necessary. This must be done early to allocate the cursor before |
| 692 | ** any calls to sqliteExprResolveIds(). |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 693 | */ |
| 694 | if( isDistinct ){ |
| 695 | distinct = pParse->nTab++; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 696 | }else{ |
| 697 | distinct = -1; |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 698 | } |
| 699 | |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 700 | /* If writing to memory or generating a set |
| 701 | ** only a single column may be output. |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 702 | */ |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 703 | if( (eDest==SRT_Mem || eDest==SRT_Set) && pEList->nExpr>1 ){ |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 704 | sqliteSetString(&pParse->zErrMsg, "only a single result allowed for " |
| 705 | "a SELECT that is part of an expression", 0); |
| 706 | pParse->nErr++; |
| 707 | return 1; |
| 708 | } |
| 709 | |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 710 | /* ORDER BY is ignored if we are not sending the result to a callback. |
| 711 | */ |
| 712 | if( eDest!=SRT_Callback ){ |
| 713 | pOrderBy = 0; |
| 714 | } |
| 715 | |
| 716 | /* Allocate cursors for "expr IN (SELECT ...)" constructs. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 717 | */ |
| 718 | for(i=0; i<pEList->nExpr; i++){ |
drh | 4794b98 | 2000-06-06 13:54:14 +0000 | [diff] [blame] | 719 | sqliteExprResolveInSelect(pParse, pEList->a[i].pExpr); |
| 720 | } |
| 721 | if( pWhere ) sqliteExprResolveInSelect(pParse, pWhere); |
| 722 | if( pOrderBy ){ |
| 723 | for(i=0; i<pOrderBy->nExpr; i++){ |
| 724 | sqliteExprResolveInSelect(pParse, pOrderBy->a[i].pExpr); |
| 725 | } |
| 726 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 727 | if( pGroupBy ){ |
| 728 | for(i=0; i<pGroupBy->nExpr; i++){ |
| 729 | sqliteExprResolveInSelect(pParse, pGroupBy->a[i].pExpr); |
| 730 | } |
| 731 | } |
| 732 | if( pHaving ) sqliteExprResolveInSelect(pParse, pHaving); |
| 733 | |
drh | 10e5e3c | 2000-06-08 00:19:02 +0000 | [diff] [blame] | 734 | /* At this point, we should have allocated all the cursors that we |
| 735 | ** need to handle subquerys and temporary tables. From here on we |
| 736 | ** are committed to keeping the same value for pParse->nTab. |
| 737 | ** |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 738 | ** Resolve the column names and do a semantics check on all the expressions. |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 739 | */ |
drh | 4794b98 | 2000-06-06 13:54:14 +0000 | [diff] [blame] | 740 | for(i=0; i<pEList->nExpr; i++){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 741 | if( sqliteExprResolveIds(pParse, pTabList, pEList->a[i].pExpr) ){ |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 742 | return 1; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 743 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 744 | if( sqliteExprCheck(pParse, pEList->a[i].pExpr, 1, &isAgg) ){ |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 745 | return 1; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 746 | } |
| 747 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 748 | if( pWhere ){ |
| 749 | if( sqliteExprResolveIds(pParse, pTabList, pWhere) ){ |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 750 | return 1; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 751 | } |
| 752 | if( sqliteExprCheck(pParse, pWhere, 0, 0) ){ |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 753 | return 1; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 754 | } |
| 755 | } |
| 756 | if( pOrderBy ){ |
| 757 | for(i=0; i<pOrderBy->nExpr; i++){ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 758 | Expr *pE = pOrderBy->a[i].pExpr; |
| 759 | if( sqliteExprResolveIds(pParse, pTabList, pE) ){ |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 760 | return 1; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 761 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 762 | if( sqliteExprCheck(pParse, pE, isAgg, 0) ){ |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 763 | return 1; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 764 | } |
| 765 | } |
| 766 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 767 | if( pGroupBy ){ |
| 768 | for(i=0; i<pGroupBy->nExpr; i++){ |
| 769 | Expr *pE = pGroupBy->a[i].pExpr; |
| 770 | if( sqliteExprResolveIds(pParse, pTabList, pE) ){ |
| 771 | return 1; |
| 772 | } |
| 773 | if( sqliteExprCheck(pParse, pE, isAgg, 0) ){ |
| 774 | return 1; |
| 775 | } |
| 776 | } |
| 777 | } |
| 778 | if( pHaving ){ |
| 779 | if( pGroupBy==0 ){ |
drh | da93281 | 2000-06-06 18:00:15 +0000 | [diff] [blame] | 780 | sqliteSetString(&pParse->zErrMsg, "a GROUP BY clause is required " |
| 781 | "before HAVING", 0); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 782 | pParse->nErr++; |
| 783 | return 1; |
| 784 | } |
| 785 | if( sqliteExprResolveIds(pParse, pTabList, pHaving) ){ |
| 786 | return 1; |
| 787 | } |
drh | da93281 | 2000-06-06 18:00:15 +0000 | [diff] [blame] | 788 | if( sqliteExprCheck(pParse, pHaving, isAgg, 0) ){ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 789 | return 1; |
| 790 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 791 | } |
| 792 | |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 793 | /* Do an analysis of aggregate expressions. |
drh | efb7251 | 2000-05-31 20:00:52 +0000 | [diff] [blame] | 794 | */ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 795 | if( isAgg ){ |
drh | aaf8872 | 2000-06-08 11:25:00 +0000 | [diff] [blame] | 796 | assert( pParse->nAgg==0 && pParse->iAggCount<0 ); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 797 | for(i=0; i<pEList->nExpr; i++){ |
| 798 | if( sqliteExprAnalyzeAggregates(pParse, pEList->a[i].pExpr) ){ |
| 799 | return 1; |
| 800 | } |
| 801 | } |
| 802 | if( pGroupBy ){ |
| 803 | for(i=0; i<pGroupBy->nExpr; i++){ |
| 804 | if( sqliteExprAnalyzeAggregates(pParse, pGroupBy->a[i].pExpr) ){ |
| 805 | return 1; |
| 806 | } |
| 807 | } |
| 808 | } |
| 809 | if( pHaving && sqliteExprAnalyzeAggregates(pParse, pHaving) ){ |
| 810 | return 1; |
| 811 | } |
drh | 191b690 | 2000-06-08 11:13:01 +0000 | [diff] [blame] | 812 | if( pOrderBy ){ |
| 813 | for(i=0; i<pOrderBy->nExpr; i++){ |
| 814 | if( sqliteExprAnalyzeAggregates(pParse, pOrderBy->a[i].pExpr) ){ |
| 815 | return 1; |
| 816 | } |
| 817 | } |
| 818 | } |
drh | efb7251 | 2000-05-31 20:00:52 +0000 | [diff] [blame] | 819 | } |
| 820 | |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 821 | /* Begin generating code. |
| 822 | */ |
| 823 | v = pParse->pVdbe; |
| 824 | if( v==0 ){ |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 825 | v = pParse->pVdbe = sqliteVdbeCreate(pParse->db); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 826 | } |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 827 | if( v==0 ){ |
| 828 | sqliteSetString(&pParse->zErrMsg, "out of memory", 0); |
| 829 | pParse->nErr++; |
| 830 | return 1; |
| 831 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 832 | if( pOrderBy ){ |
| 833 | sqliteVdbeAddOp(v, OP_SortOpen, 0, 0, 0, 0); |
| 834 | } |
| 835 | |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 836 | /* Identify column names if we will be using in the callback. This |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 837 | ** step is skipped if the output is going to a table or a memory cell. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 838 | */ |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 839 | if( eDest==SRT_Callback ){ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 840 | generateColumnNames(pParse, pTabList, pEList); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 841 | } |
| 842 | |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 843 | /* Reset the aggregator |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 844 | */ |
| 845 | if( isAgg ){ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 846 | sqliteVdbeAddOp(v, OP_AggReset, 0, pParse->nAgg, 0, 0); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 847 | } |
| 848 | |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 849 | /* Initialize the memory cell to NULL |
| 850 | */ |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 851 | if( eDest==SRT_Mem ){ |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 852 | sqliteVdbeAddOp(v, OP_Null, 0, 0, 0, 0); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 853 | sqliteVdbeAddOp(v, OP_MemStore, iParm, 0, 0, 0); |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 854 | } |
| 855 | |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 856 | /* Begin the database scan |
drh | efb7251 | 2000-05-31 20:00:52 +0000 | [diff] [blame] | 857 | */ |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 858 | if( isDistinct ){ |
drh | 345fda3 | 2001-01-15 22:51:08 +0000 | [diff] [blame] | 859 | sqliteVdbeAddOp(v, OP_OpenIdx, distinct, 1, 0, 0); |
drh | efb7251 | 2000-05-31 20:00:52 +0000 | [diff] [blame] | 860 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 861 | pWInfo = sqliteWhereBegin(pParse, pTabList, pWhere, 0); |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 862 | if( pWInfo==0 ) return 1; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 863 | |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 864 | /* Use the standard inner loop if we are not dealing with |
| 865 | ** aggregates |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 866 | */ |
drh | da9d6c4 | 2000-05-31 18:20:14 +0000 | [diff] [blame] | 867 | if( !isAgg ){ |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 868 | if( selectInnerLoop(pParse, pEList, 0, 0, pOrderBy, distinct, eDest, iParm, |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 869 | pWInfo->iContinue, pWInfo->iBreak) ){ |
| 870 | return 1; |
drh | da9d6c4 | 2000-05-31 18:20:14 +0000 | [diff] [blame] | 871 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 872 | } |
drh | efb7251 | 2000-05-31 20:00:52 +0000 | [diff] [blame] | 873 | |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 874 | /* If we are dealing with aggregates, then to the special aggregate |
| 875 | ** processing. |
drh | efb7251 | 2000-05-31 20:00:52 +0000 | [diff] [blame] | 876 | */ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 877 | else{ |
| 878 | int doFocus; |
| 879 | if( pGroupBy ){ |
| 880 | for(i=0; i<pGroupBy->nExpr; i++){ |
| 881 | sqliteExprCode(pParse, pGroupBy->a[i].pExpr); |
| 882 | } |
| 883 | sqliteVdbeAddOp(v, OP_MakeKey, pGroupBy->nExpr, 0, 0, 0); |
| 884 | doFocus = 1; |
| 885 | }else{ |
| 886 | doFocus = 0; |
| 887 | for(i=0; i<pParse->nAgg; i++){ |
| 888 | if( !pParse->aAgg[i].isAgg ){ |
| 889 | doFocus = 1; |
| 890 | break; |
| 891 | } |
| 892 | } |
| 893 | if( doFocus ){ |
| 894 | sqliteVdbeAddOp(v, OP_String, 0, 0, "", 0); |
| 895 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 896 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 897 | if( doFocus ){ |
| 898 | int lbl1 = sqliteVdbeMakeLabel(v); |
| 899 | sqliteVdbeAddOp(v, OP_AggFocus, 0, lbl1, 0, 0); |
| 900 | for(i=0; i<pParse->nAgg; i++){ |
| 901 | if( pParse->aAgg[i].isAgg ) continue; |
| 902 | sqliteExprCode(pParse, pParse->aAgg[i].pExpr); |
| 903 | sqliteVdbeAddOp(v, OP_AggSet, 0, i, 0, 0); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 904 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 905 | sqliteVdbeResolveLabel(v, lbl1); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 906 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 907 | for(i=0; i<pParse->nAgg; i++){ |
| 908 | Expr *pE; |
| 909 | int op; |
| 910 | if( !pParse->aAgg[i].isAgg ) continue; |
| 911 | pE = pParse->aAgg[i].pExpr; |
| 912 | if( pE==0 ){ |
| 913 | sqliteVdbeAddOp(v, OP_AggIncr, 1, i, 0, 0); |
| 914 | continue; |
| 915 | } |
| 916 | assert( pE->op==TK_AGG_FUNCTION ); |
| 917 | assert( pE->pList!=0 && pE->pList->nExpr==1 ); |
| 918 | sqliteExprCode(pParse, pE->pList->a[0].pExpr); |
| 919 | sqliteVdbeAddOp(v, OP_AggGet, 0, i, 0, 0); |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 920 | switch( pE->iColumn ){ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 921 | case FN_Min: op = OP_Min; break; |
| 922 | case FN_Max: op = OP_Max; break; |
| 923 | case FN_Avg: op = OP_Add; break; |
| 924 | case FN_Sum: op = OP_Add; break; |
| 925 | } |
| 926 | sqliteVdbeAddOp(v, op, 0, 0, 0, 0); |
| 927 | sqliteVdbeAddOp(v, OP_AggSet, 0, i, 0, 0); |
| 928 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 929 | } |
| 930 | |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 931 | |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 932 | /* End the database scan loop. |
| 933 | */ |
| 934 | sqliteWhereEnd(pWInfo); |
| 935 | |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 936 | /* If we are processing aggregates, we need to set up a second loop |
| 937 | ** over all of the aggregate values and process them. |
| 938 | */ |
| 939 | if( isAgg ){ |
| 940 | int endagg = sqliteVdbeMakeLabel(v); |
| 941 | int startagg; |
| 942 | startagg = sqliteVdbeAddOp(v, OP_AggNext, 0, endagg, 0, 0); |
| 943 | pParse->useAgg = 1; |
| 944 | if( pHaving ){ |
| 945 | sqliteExprIfFalse(pParse, pHaving, startagg); |
| 946 | } |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 947 | if( selectInnerLoop(pParse, pEList, 0, 0, pOrderBy, distinct, eDest, iParm, |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 948 | startagg, endagg) ){ |
| 949 | return 1; |
| 950 | } |
| 951 | sqliteVdbeAddOp(v, OP_Goto, 0, startagg, 0, 0); |
| 952 | sqliteVdbeAddOp(v, OP_Noop, 0, 0, 0, endagg); |
| 953 | pParse->useAgg = 0; |
| 954 | } |
| 955 | |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 956 | /* If there is an ORDER BY clause, then we need to sort the results |
| 957 | ** and send them to the callback one by one. |
| 958 | */ |
| 959 | if( pOrderBy ){ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 960 | generateSortTail(v, pEList->nExpr); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 961 | } |
drh | 10e5e3c | 2000-06-08 00:19:02 +0000 | [diff] [blame] | 962 | pParse->nTab = base; |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 963 | return 0; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 964 | } |