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 | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame^] | 27 | ** $Id: select.c,v 1.13 2000/06/06 21:56:08 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 |
| 89 | ** result row. Except if pEList==NULL, then we just read nField |
| 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 */ |
| 96 | int nField, /* Number of fields 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 | |
| 107 | /* Pull the requested fields. |
| 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 | } |
| 113 | nField = pEList->nExpr; |
| 114 | }else{ |
| 115 | for(i=0; i<nField; i++){ |
| 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; |
| 138 | sqliteVdbeAddOp(v, OP_SortMakeRec, pEList->nExpr, 0, 0, 0); |
| 139 | zSortOrder = sqliteMalloc( pOrderBy->nExpr + 1 ); |
| 140 | if( zSortOrder==0 ) return 1; |
| 141 | for(i=0; i<pOrderBy->nExpr; i++){ |
| 142 | zSortOrder[i] = pOrderBy->a[i].idx ? '-' : '+'; |
| 143 | sqliteExprCode(pParse, pOrderBy->a[i].pExpr); |
| 144 | } |
| 145 | zSortOrder[pOrderBy->nExpr] = 0; |
| 146 | sqliteVdbeAddOp(v, OP_SortMakeKey, pOrderBy->nExpr, 0, zSortOrder, 0); |
| 147 | sqliteVdbeAddOp(v, OP_SortPut, 0, 0, 0, 0); |
| 148 | }else |
| 149 | |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame^] | 150 | /* In this mode, write each query result to the key of the temporary |
| 151 | ** table iParm. |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 152 | */ |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame^] | 153 | if( eDest==SRT_Union ){ |
| 154 | sqliteVdbeAddOp(v, OP_MakeRecord, nField, 0, 0, 0); |
| 155 | sqliteVdbeAddOp(v, OP_String, iParm, 0, "", 0); |
| 156 | sqliteVdbeAddOp(v, OP_Put, iParm, 0, 0, 0); |
| 157 | }else |
| 158 | |
| 159 | /* Construct a record from the query result, but instead of |
| 160 | ** saving that record, use it as a key to delete elements from |
| 161 | ** the temporary table iParm. |
| 162 | */ |
| 163 | if( eDest==SRT_Except ){ |
| 164 | assert( pEList->nExpr==1 ); |
| 165 | sqliteVdbeAddOp(v, OP_String, 0, 0, "", 0); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 166 | sqliteVdbeAddOp(v, OP_Put, iParm, 0, 0, 0); |
| 167 | }else |
| 168 | |
| 169 | /* If we are creating a set for an "expr IN (SELECT ...)" construct, |
| 170 | ** then there should be a single item on the stack. Write this |
| 171 | ** item into the set table with bogus data. |
| 172 | */ |
| 173 | if( eDest==SRT_Set ){ |
| 174 | assert( pEList->nExpr==1 ); |
| 175 | sqliteVdbeAddOp(v, OP_String, 0, 0, "", 0); |
| 176 | sqliteVdbeAddOp(v, OP_Put, iParm, 0, 0, 0); |
| 177 | }else |
| 178 | |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame^] | 179 | |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 180 | /* If this is a scalar select that is part of an expression, then |
| 181 | ** store the results in the appropriate memory cell and break out |
| 182 | ** of the scan loop. |
| 183 | */ |
| 184 | if( eDest==SRT_Mem ){ |
| 185 | sqliteVdbeAddOp(v, OP_MemStore, iParm, 0, 0, 0); |
| 186 | sqliteVdbeAddOp(v, OP_Goto, 0, iBreak, 0, 0); |
| 187 | }else |
| 188 | |
| 189 | /* If none of the above, send the data to the callback function. |
| 190 | */ |
| 191 | { |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame^] | 192 | sqliteVdbeAddOp(v, OP_Callback, nField, 0, 0, 0); |
| 193 | } |
| 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | /* |
| 198 | ** Generate code that will tell the VDBE how many columns there |
| 199 | ** are in the result and the name for each column. This information |
| 200 | ** is used to provide "argc" and "azCol[]" values in the callback. |
| 201 | */ |
| 202 | static void generateColumnNames(Vdbe *v, IdList *pTabList, ExprList *pEList){ |
| 203 | int i; |
| 204 | sqliteVdbeAddOp(v, OP_ColumnCount, pEList->nExpr, 0, 0, 0); |
| 205 | for(i=0; i<pEList->nExpr; i++){ |
| 206 | Expr *p; |
| 207 | if( pEList->a[i].zName ){ |
| 208 | char *zName = pEList->a[i].zName; |
| 209 | int addr = sqliteVdbeAddOp(v, OP_ColumnName, i, 0, zName, 0); |
| 210 | if( zName[0]=='\'' || zName[0]=='"' ){ |
| 211 | sqliteVdbeDequoteP3(v, addr); |
| 212 | } |
| 213 | continue; |
| 214 | } |
| 215 | p = pEList->a[i].pExpr; |
| 216 | if( p->op!=TK_FIELD || pTabList==0 ){ |
| 217 | char zName[30]; |
| 218 | sprintf(zName, "field%d", i+1); |
| 219 | sqliteVdbeAddOp(v, OP_ColumnName, i, 0, zName, 0); |
| 220 | }else{ |
| 221 | if( pTabList->nId>1 ){ |
| 222 | char *zName = 0; |
| 223 | Table *pTab = pTabList->a[p->iTable].pTab; |
| 224 | char *zTab; |
| 225 | |
| 226 | zTab = pTabList->a[p->iTable].zAlias; |
| 227 | if( zTab==0 ) zTab = pTab->zName; |
| 228 | sqliteSetString(&zName, zTab, ".", pTab->aCol[p->iField].zName, 0); |
| 229 | sqliteVdbeAddOp(v, OP_ColumnName, i, 0, zName, 0); |
| 230 | sqliteFree(zName); |
| 231 | }else{ |
| 232 | Table *pTab = pTabList->a[0].pTab; |
| 233 | char *zName = pTab->aCol[p->iField].zName; |
| 234 | sqliteVdbeAddOp(v, OP_ColumnName, i, 0, zName, 0); |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | /* |
| 241 | ** This routine is called to process a query that is really the union |
| 242 | ** or intersection of two or more separate queries. |
| 243 | */ |
| 244 | static int multiSelect(Parse *pParse, Select *p, int eDest, int iParm){ |
| 245 | int rc; |
| 246 | Select *pPrior; |
| 247 | Vdbe *v; |
| 248 | int i; |
| 249 | |
| 250 | /* Make sure we have a valid query engine. If not, create a new one. |
| 251 | */ |
| 252 | v = pParse->pVdbe; |
| 253 | if( v==0 ){ |
| 254 | v = pParse->pVdbe = sqliteVdbeCreate(pParse->db->pBe); |
| 255 | } |
| 256 | if( v==0 ){ |
| 257 | sqliteSetString(&pParse->zErrMsg, "out of memory", 0); |
| 258 | pParse->nErr++; |
| 259 | return 1; |
| 260 | } |
| 261 | |
| 262 | assert( p->pPrior!=0 ); |
| 263 | pPrior = p->pPrior; |
| 264 | switch( p->op ){ |
| 265 | case TK_ALL: { |
| 266 | rc = sqliteSelect(pParse, pPrior, eDest, iParm); |
| 267 | if( rc ) return rc; |
| 268 | p->pPrior = 0; |
| 269 | rc = sqliteSelect(pParse, p, eDest, iParm); |
| 270 | p->pPrior = pPrior; |
| 271 | break; |
| 272 | } |
| 273 | case TK_EXCEPT: |
| 274 | case TK_UNION: { |
| 275 | int unionTab; |
| 276 | int op; |
| 277 | |
| 278 | if( eDest==SRT_Union ){ |
| 279 | unionTab = iParm; |
| 280 | }else{ |
| 281 | unionTab = pParse->nTab++; |
| 282 | sqliteVdbeAddOp(v, OP_Open, unionTab, 1, 0, 0); |
| 283 | sqliteVdbeAddOp(v, OP_KeyAsData, unionTab, 1, 0, 0); |
| 284 | } |
| 285 | rc = sqliteSelect(pParse, pPrior, SRT_Union, unionTab); |
| 286 | if( rc ) return rc; |
| 287 | op = p->op==TK_EXCEPT ? SRT_Except : SRT_Union; |
| 288 | p->pPrior = 0; |
| 289 | rc = sqliteSelect(pParse, p, op, unionTab); |
| 290 | p->pPrior = pPrior; |
| 291 | if( rc ) return rc; |
| 292 | if( eDest!=SRT_Union ){ |
| 293 | int iCont, iBreak; |
| 294 | assert( p->pEList ); |
| 295 | generateColumnNames(v, 0, p->pEList); |
| 296 | iBreak = sqliteVdbeMakeLabel(v); |
| 297 | iCont = sqliteVdbeAddOp(v, OP_Next, unionTab, iBreak, 0, 0); |
| 298 | rc = selectInnerLoop(pParse, 0, unionTab, p->pEList->nExpr, |
| 299 | 0, -1, eDest, iParm, |
| 300 | iCont, iBreak); |
| 301 | if( rc ) return 1; |
| 302 | sqliteVdbeAddOp(v, OP_Goto, 0, iCont, 0, 0); |
| 303 | sqliteVdbeAddOp(v, OP_Close, unionTab, 0, 0, iBreak); |
| 304 | } |
| 305 | break; |
| 306 | } |
| 307 | case TK_INTERSECT: { |
| 308 | int tab1, tab2; |
| 309 | Select *pPrior; |
| 310 | int iCont, iBreak; |
| 311 | |
| 312 | tab1 = pParse->nTab++; |
| 313 | tab2 = pParse->nTab++; |
| 314 | sqliteVdbeAddOp(v, OP_Open, tab1, 1, 0, 0); |
| 315 | sqliteVdbeAddOp(v, OP_KeyAsData, tab1, 1, 0, 0); |
| 316 | rc = sqliteSelect(pParse, pPrior, SRT_Union, tab1); |
| 317 | if( rc ) return rc; |
| 318 | sqliteVdbeAddOp(v, OP_Open, tab2, 1, 0, 0); |
| 319 | sqliteVdbeAddOp(v, OP_KeyAsData, tab2, 1, 0, 0); |
| 320 | p->pPrior = 0; |
| 321 | rc = sqliteSelect(pParse, p, SRT_Union, tab2); |
| 322 | p->pPrior = pPrior; |
| 323 | if( rc ) return rc; |
| 324 | assert( p->pEList ); |
| 325 | generateColumnNames(v, 0, p->pEList); |
| 326 | iBreak = sqliteVdbeMakeLabel(v); |
| 327 | iCont = sqliteVdbeAddOp(v, OP_Next, tab1, iBreak, 0, 0); |
| 328 | sqliteVdbeAddOp(v, OP_Key, tab1, 0, 0, 0); |
| 329 | sqliteVdbeAddOp(v, OP_NotFound, tab2, iCont, 0, 0); |
| 330 | rc = selectInnerLoop(pParse, 0, tab1, p->pEList->nExpr, |
| 331 | 0, -1, eDest, iParm, |
| 332 | iCont, iBreak); |
| 333 | if( rc ) return 1; |
| 334 | sqliteVdbeAddOp(v, OP_Goto, 0, iCont, 0, 0); |
| 335 | sqliteVdbeAddOp(v, OP_Close, tab2, 0, 0, iBreak); |
| 336 | sqliteVdbeAddOp(v, OP_Close, tab1, 0, 0, 0); |
| 337 | break; |
| 338 | } |
| 339 | } |
| 340 | assert( p->pEList && pPrior->pEList ); |
| 341 | if( p->pEList->nExpr!=pPrior->pEList->nExpr ){ |
| 342 | sqliteSetString(&pParse->zErrMsg, "SELECTs have different numbers " |
| 343 | "of columns and therefore cannot be joined", 0); |
| 344 | pParse->nErr++; |
| 345 | return 1; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 346 | } |
| 347 | return 0; |
| 348 | } |
| 349 | |
| 350 | /* |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 351 | ** Generate code for the given SELECT statement. |
| 352 | ** |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 353 | ** The results are distributed in various ways depending on the |
| 354 | ** value of eDest and iParm. |
| 355 | ** |
| 356 | ** eDest Value Result |
| 357 | ** ------------ ------------------------------------------- |
| 358 | ** SRT_Callback Invoke the callback for each row of the result. |
| 359 | ** |
| 360 | ** SRT_Mem Store first result in memory cell iParm |
| 361 | ** |
| 362 | ** SRT_Set Store results as keys of a table with cursor iParm |
| 363 | ** |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame^] | 364 | ** SRT_Union Store results as a key in a temporary table iParm |
| 365 | ** |
| 366 | ** SRT_Except Remove results form the temporary talbe iParm. |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 367 | ** |
| 368 | ** This routine returns the number of errors. If any errors are |
| 369 | ** encountered, then an appropriate error message is left in |
| 370 | ** pParse->zErrMsg. |
| 371 | ** |
| 372 | ** This routine does NOT free the Select structure passed in. The |
| 373 | ** calling function needs to do that. |
| 374 | */ |
| 375 | int sqliteSelect( |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 376 | Parse *pParse, /* The parser context */ |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 377 | Select *p, /* The SELECT statement being coded. */ |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame^] | 378 | int eDest, /* One of: SRT_Callback Mem Set Union Except */ |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 379 | int iParm /* Save result in this memory location, if >=0 */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 380 | ){ |
| 381 | int i, j; |
| 382 | WhereInfo *pWInfo; |
| 383 | Vdbe *v; |
| 384 | int isAgg = 0; /* True for select lists like "count(*)" */ |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 385 | ExprList *pEList; /* List of fields to extract. NULL means "*" */ |
| 386 | IdList *pTabList; /* List of tables to select from */ |
| 387 | Expr *pWhere; /* The WHERE clause. May be NULL */ |
| 388 | ExprList *pOrderBy; /* The ORDER BY clause. May be NULL */ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 389 | ExprList *pGroupBy; /* The GROUP BY clause. May be NULL */ |
| 390 | Expr *pHaving; /* The HAVING clause. May be NULL */ |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 391 | int isDistinct; /* True if the DISTINCT keyword is present */ |
| 392 | int distinct; /* Table to use for the distinct set */ |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 393 | |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame^] | 394 | /* If there is are a sequence of queries, do the earlier ones first. |
| 395 | */ |
| 396 | if( p->pPrior ){ |
| 397 | return multiSelect(pParse, p, eDest, iParm); |
| 398 | } |
| 399 | |
| 400 | /* Make local copies of the parameters for this query. |
| 401 | */ |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 402 | pEList = p->pEList; |
| 403 | pTabList = p->pSrc; |
| 404 | pWhere = p->pWhere; |
| 405 | pOrderBy = p->pOrderBy; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 406 | pGroupBy = p->pGroupBy; |
| 407 | pHaving = p->pHaving; |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 408 | isDistinct = p->isDistinct; |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 409 | |
| 410 | /* |
| 411 | ** Do not even attempt to generate any code if we have already seen |
| 412 | ** errors before this routine starts. |
| 413 | */ |
| 414 | if( pParse->nErr>0 ) return 0; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 415 | sqliteParseInfoReset(pParse); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 416 | |
| 417 | /* Look up every table in the table list. |
| 418 | */ |
| 419 | for(i=0; i<pTabList->nId; i++){ |
| 420 | pTabList->a[i].pTab = sqliteFindTable(pParse->db, pTabList->a[i].zName); |
| 421 | if( pTabList->a[i].pTab==0 ){ |
| 422 | sqliteSetString(&pParse->zErrMsg, "no such table: ", |
| 423 | pTabList->a[i].zName, 0); |
| 424 | pParse->nErr++; |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 425 | return 1; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 426 | } |
| 427 | } |
| 428 | |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 429 | /* Allocate a temporary table to use for the DISTINCT set, if |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 430 | ** necessary. This must be done early to allocate the cursor before |
| 431 | ** any calls to sqliteExprResolveIds(). |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 432 | */ |
| 433 | if( isDistinct ){ |
| 434 | distinct = pParse->nTab++; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 435 | }else{ |
| 436 | distinct = -1; |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 437 | } |
| 438 | |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 439 | /* If the list of fields to retrieve is "*" then replace it with |
| 440 | ** a list of all fields from all tables. |
| 441 | */ |
| 442 | if( pEList==0 ){ |
| 443 | for(i=0; i<pTabList->nId; i++){ |
| 444 | Table *pTab = pTabList->a[i].pTab; |
| 445 | for(j=0; j<pTab->nCol; j++){ |
| 446 | Expr *pExpr = sqliteExpr(TK_FIELD, 0, 0, 0); |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 447 | pExpr->iTable = i + pParse->nTab; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 448 | pExpr->iField = j; |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame^] | 449 | p->pEList = pEList = sqliteExprListAppend(pEList, pExpr, 0); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 450 | } |
| 451 | } |
| 452 | } |
| 453 | |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 454 | /* If writing to memory or generating a set |
| 455 | ** only a single column may be output. |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 456 | */ |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 457 | if( (eDest==SRT_Mem || eDest==SRT_Set) && pEList->nExpr>1 ){ |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 458 | sqliteSetString(&pParse->zErrMsg, "only a single result allowed for " |
| 459 | "a SELECT that is part of an expression", 0); |
| 460 | pParse->nErr++; |
| 461 | return 1; |
| 462 | } |
| 463 | |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 464 | /* ORDER BY is ignored if we are not sending the result to a callback. |
| 465 | */ |
| 466 | if( eDest!=SRT_Callback ){ |
| 467 | pOrderBy = 0; |
| 468 | } |
| 469 | |
| 470 | /* Allocate cursors for "expr IN (SELECT ...)" constructs. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 471 | */ |
| 472 | for(i=0; i<pEList->nExpr; i++){ |
drh | 4794b98 | 2000-06-06 13:54:14 +0000 | [diff] [blame] | 473 | sqliteExprResolveInSelect(pParse, pEList->a[i].pExpr); |
| 474 | } |
| 475 | if( pWhere ) sqliteExprResolveInSelect(pParse, pWhere); |
| 476 | if( pOrderBy ){ |
| 477 | for(i=0; i<pOrderBy->nExpr; i++){ |
| 478 | sqliteExprResolveInSelect(pParse, pOrderBy->a[i].pExpr); |
| 479 | } |
| 480 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 481 | if( pGroupBy ){ |
| 482 | for(i=0; i<pGroupBy->nExpr; i++){ |
| 483 | sqliteExprResolveInSelect(pParse, pGroupBy->a[i].pExpr); |
| 484 | } |
| 485 | } |
| 486 | if( pHaving ) sqliteExprResolveInSelect(pParse, pHaving); |
| 487 | |
| 488 | /* Resolve the field names and do a semantics check on all the expressions. |
| 489 | */ |
drh | 4794b98 | 2000-06-06 13:54:14 +0000 | [diff] [blame] | 490 | for(i=0; i<pEList->nExpr; i++){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 491 | if( sqliteExprResolveIds(pParse, pTabList, pEList->a[i].pExpr) ){ |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 492 | return 1; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 493 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 494 | if( sqliteExprCheck(pParse, pEList->a[i].pExpr, 1, &isAgg) ){ |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 495 | return 1; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 496 | } |
| 497 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 498 | if( pWhere ){ |
| 499 | if( sqliteExprResolveIds(pParse, pTabList, pWhere) ){ |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 500 | return 1; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 501 | } |
| 502 | if( sqliteExprCheck(pParse, pWhere, 0, 0) ){ |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 503 | return 1; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 504 | } |
| 505 | } |
| 506 | if( pOrderBy ){ |
| 507 | for(i=0; i<pOrderBy->nExpr; i++){ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 508 | Expr *pE = pOrderBy->a[i].pExpr; |
| 509 | if( sqliteExprResolveIds(pParse, pTabList, pE) ){ |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 510 | return 1; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 511 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 512 | if( sqliteExprCheck(pParse, pE, isAgg, 0) ){ |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 513 | return 1; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 514 | } |
| 515 | } |
| 516 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 517 | if( pGroupBy ){ |
| 518 | for(i=0; i<pGroupBy->nExpr; i++){ |
| 519 | Expr *pE = pGroupBy->a[i].pExpr; |
| 520 | if( sqliteExprResolveIds(pParse, pTabList, pE) ){ |
| 521 | return 1; |
| 522 | } |
| 523 | if( sqliteExprCheck(pParse, pE, isAgg, 0) ){ |
| 524 | return 1; |
| 525 | } |
| 526 | } |
| 527 | } |
| 528 | if( pHaving ){ |
| 529 | if( pGroupBy==0 ){ |
drh | da93281 | 2000-06-06 18:00:15 +0000 | [diff] [blame] | 530 | sqliteSetString(&pParse->zErrMsg, "a GROUP BY clause is required " |
| 531 | "before HAVING", 0); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 532 | pParse->nErr++; |
| 533 | return 1; |
| 534 | } |
| 535 | if( sqliteExprResolveIds(pParse, pTabList, pHaving) ){ |
| 536 | return 1; |
| 537 | } |
drh | da93281 | 2000-06-06 18:00:15 +0000 | [diff] [blame] | 538 | if( sqliteExprCheck(pParse, pHaving, isAgg, 0) ){ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 539 | return 1; |
| 540 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 541 | } |
| 542 | |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 543 | /* Do an analysis of aggregate expressions. |
drh | efb7251 | 2000-05-31 20:00:52 +0000 | [diff] [blame] | 544 | */ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 545 | if( isAgg ){ |
| 546 | for(i=0; i<pEList->nExpr; i++){ |
| 547 | if( sqliteExprAnalyzeAggregates(pParse, pEList->a[i].pExpr) ){ |
| 548 | return 1; |
| 549 | } |
| 550 | } |
| 551 | if( pGroupBy ){ |
| 552 | for(i=0; i<pGroupBy->nExpr; i++){ |
| 553 | if( sqliteExprAnalyzeAggregates(pParse, pGroupBy->a[i].pExpr) ){ |
| 554 | return 1; |
| 555 | } |
| 556 | } |
| 557 | } |
| 558 | if( pHaving && sqliteExprAnalyzeAggregates(pParse, pHaving) ){ |
| 559 | return 1; |
| 560 | } |
drh | efb7251 | 2000-05-31 20:00:52 +0000 | [diff] [blame] | 561 | } |
| 562 | |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 563 | /* Begin generating code. |
| 564 | */ |
| 565 | v = pParse->pVdbe; |
| 566 | if( v==0 ){ |
| 567 | v = pParse->pVdbe = sqliteVdbeCreate(pParse->db->pBe); |
| 568 | } |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 569 | if( v==0 ){ |
| 570 | sqliteSetString(&pParse->zErrMsg, "out of memory", 0); |
| 571 | pParse->nErr++; |
| 572 | return 1; |
| 573 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 574 | if( pOrderBy ){ |
| 575 | sqliteVdbeAddOp(v, OP_SortOpen, 0, 0, 0, 0); |
| 576 | } |
| 577 | |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 578 | /* Identify column names if we will be using in the callback. This |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 579 | ** 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] | 580 | */ |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 581 | if( eDest==SRT_Callback ){ |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame^] | 582 | generateColumnNames(v, pTabList, pEList); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 583 | } |
| 584 | |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 585 | /* Reset the aggregator |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 586 | */ |
| 587 | if( isAgg ){ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 588 | sqliteVdbeAddOp(v, OP_AggReset, 0, pParse->nAgg, 0, 0); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 589 | } |
| 590 | |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 591 | /* Initialize the memory cell to NULL |
| 592 | */ |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 593 | if( eDest==SRT_Mem ){ |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 594 | sqliteVdbeAddOp(v, OP_Null, 0, 0, 0, 0); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 595 | sqliteVdbeAddOp(v, OP_MemStore, iParm, 0, 0, 0); |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 596 | } |
| 597 | |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 598 | /* Begin the database scan |
drh | efb7251 | 2000-05-31 20:00:52 +0000 | [diff] [blame] | 599 | */ |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 600 | if( isDistinct ){ |
drh | eec553b | 2000-06-02 01:51:20 +0000 | [diff] [blame] | 601 | sqliteVdbeAddOp(v, OP_Open, distinct, 1, 0, 0); |
drh | efb7251 | 2000-05-31 20:00:52 +0000 | [diff] [blame] | 602 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 603 | pWInfo = sqliteWhereBegin(pParse, pTabList, pWhere, 0); |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 604 | if( pWInfo==0 ) return 1; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 605 | |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 606 | /* Use the standard inner loop if we are not dealing with |
| 607 | ** aggregates |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 608 | */ |
drh | da9d6c4 | 2000-05-31 18:20:14 +0000 | [diff] [blame] | 609 | if( !isAgg ){ |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame^] | 610 | if( selectInnerLoop(pParse, pEList, 0, 0, pOrderBy, distinct, eDest, iParm, |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 611 | pWInfo->iContinue, pWInfo->iBreak) ){ |
| 612 | return 1; |
drh | da9d6c4 | 2000-05-31 18:20:14 +0000 | [diff] [blame] | 613 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 614 | } |
drh | efb7251 | 2000-05-31 20:00:52 +0000 | [diff] [blame] | 615 | |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 616 | /* If we are dealing with aggregates, then to the special aggregate |
| 617 | ** processing. |
drh | efb7251 | 2000-05-31 20:00:52 +0000 | [diff] [blame] | 618 | */ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 619 | else{ |
| 620 | int doFocus; |
| 621 | if( pGroupBy ){ |
| 622 | for(i=0; i<pGroupBy->nExpr; i++){ |
| 623 | sqliteExprCode(pParse, pGroupBy->a[i].pExpr); |
| 624 | } |
| 625 | sqliteVdbeAddOp(v, OP_MakeKey, pGroupBy->nExpr, 0, 0, 0); |
| 626 | doFocus = 1; |
| 627 | }else{ |
| 628 | doFocus = 0; |
| 629 | for(i=0; i<pParse->nAgg; i++){ |
| 630 | if( !pParse->aAgg[i].isAgg ){ |
| 631 | doFocus = 1; |
| 632 | break; |
| 633 | } |
| 634 | } |
| 635 | if( doFocus ){ |
| 636 | sqliteVdbeAddOp(v, OP_String, 0, 0, "", 0); |
| 637 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 638 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 639 | if( doFocus ){ |
| 640 | int lbl1 = sqliteVdbeMakeLabel(v); |
| 641 | sqliteVdbeAddOp(v, OP_AggFocus, 0, lbl1, 0, 0); |
| 642 | for(i=0; i<pParse->nAgg; i++){ |
| 643 | if( pParse->aAgg[i].isAgg ) continue; |
| 644 | sqliteExprCode(pParse, pParse->aAgg[i].pExpr); |
| 645 | sqliteVdbeAddOp(v, OP_AggSet, 0, i, 0, 0); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 646 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 647 | sqliteVdbeResolveLabel(v, lbl1); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 648 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 649 | for(i=0; i<pParse->nAgg; i++){ |
| 650 | Expr *pE; |
| 651 | int op; |
| 652 | if( !pParse->aAgg[i].isAgg ) continue; |
| 653 | pE = pParse->aAgg[i].pExpr; |
| 654 | if( pE==0 ){ |
| 655 | sqliteVdbeAddOp(v, OP_AggIncr, 1, i, 0, 0); |
| 656 | continue; |
| 657 | } |
| 658 | assert( pE->op==TK_AGG_FUNCTION ); |
| 659 | assert( pE->pList!=0 && pE->pList->nExpr==1 ); |
| 660 | sqliteExprCode(pParse, pE->pList->a[0].pExpr); |
| 661 | sqliteVdbeAddOp(v, OP_AggGet, 0, i, 0, 0); |
| 662 | switch( pE->iField ){ |
| 663 | case FN_Min: op = OP_Min; break; |
| 664 | case FN_Max: op = OP_Max; break; |
| 665 | case FN_Avg: op = OP_Add; break; |
| 666 | case FN_Sum: op = OP_Add; break; |
| 667 | } |
| 668 | sqliteVdbeAddOp(v, op, 0, 0, 0, 0); |
| 669 | sqliteVdbeAddOp(v, OP_AggSet, 0, i, 0, 0); |
| 670 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 671 | } |
| 672 | |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 673 | |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 674 | /* End the database scan loop. |
| 675 | */ |
| 676 | sqliteWhereEnd(pWInfo); |
| 677 | |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 678 | /* If we are processing aggregates, we need to set up a second loop |
| 679 | ** over all of the aggregate values and process them. |
| 680 | */ |
| 681 | if( isAgg ){ |
| 682 | int endagg = sqliteVdbeMakeLabel(v); |
| 683 | int startagg; |
| 684 | startagg = sqliteVdbeAddOp(v, OP_AggNext, 0, endagg, 0, 0); |
| 685 | pParse->useAgg = 1; |
| 686 | if( pHaving ){ |
| 687 | sqliteExprIfFalse(pParse, pHaving, startagg); |
| 688 | } |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame^] | 689 | if( selectInnerLoop(pParse, pEList, 0, 0, pOrderBy, distinct, eDest, iParm, |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 690 | startagg, endagg) ){ |
| 691 | return 1; |
| 692 | } |
| 693 | sqliteVdbeAddOp(v, OP_Goto, 0, startagg, 0, 0); |
| 694 | sqliteVdbeAddOp(v, OP_Noop, 0, 0, 0, endagg); |
| 695 | pParse->useAgg = 0; |
| 696 | } |
| 697 | |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 698 | /* If there is an ORDER BY clause, then we need to sort the results |
| 699 | ** and send them to the callback one by one. |
| 700 | */ |
| 701 | if( pOrderBy ){ |
| 702 | int end = sqliteVdbeMakeLabel(v); |
| 703 | int addr; |
| 704 | sqliteVdbeAddOp(v, OP_Sort, 0, 0, 0, 0); |
| 705 | addr = sqliteVdbeAddOp(v, OP_SortNext, 0, end, 0, 0); |
| 706 | sqliteVdbeAddOp(v, OP_SortCallback, pEList->nExpr, 0, 0, 0); |
| 707 | sqliteVdbeAddOp(v, OP_Goto, 0, addr, 0, 0); |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 708 | sqliteVdbeAddOp(v, OP_SortClose, 0, 0, 0, end); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 709 | } |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 710 | return 0; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 711 | } |