drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1 | /* |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2 | ** 2001 September 15 |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 3 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 6 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 7 | ** 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. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 10 | ** |
| 11 | ************************************************************************* |
| 12 | ** This file contains C code routines that are called by the parser |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 13 | ** to handle SELECT statements in SQLite. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 14 | ** |
drh | 195e696 | 2002-05-25 00:18:20 +0000 | [diff] [blame^] | 15 | ** $Id: select.c,v 1.85 2002/05/25 00:18:21 drh Exp $ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 16 | */ |
| 17 | #include "sqliteInt.h" |
| 18 | |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 19 | /* |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 20 | ** Allocate a new Select structure and return a pointer to that |
| 21 | ** structure. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 22 | */ |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 23 | Select *sqliteSelectNew( |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 24 | ExprList *pEList, /* which columns to include in the result */ |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 25 | SrcList *pSrc, /* the FROM clause -- which tables to scan */ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 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 */ |
drh | 9bbca4c | 2001-11-06 04:00:18 +0000 | [diff] [blame] | 30 | 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 */ |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 33 | ){ |
| 34 | Select *pNew; |
| 35 | pNew = sqliteMalloc( sizeof(*pNew) ); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 36 | if( pNew==0 ){ |
| 37 | sqliteExprListDelete(pEList); |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 38 | sqliteSrcListDelete(pSrc); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 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; |
drh | 9bbca4c | 2001-11-06 04:00:18 +0000 | [diff] [blame] | 52 | pNew->nLimit = nLimit; |
| 53 | pNew->nOffset = nOffset; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 54 | } |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 55 | return pNew; |
| 56 | } |
| 57 | |
| 58 | /* |
drh | 01f3f25 | 2002-05-24 16:14:15 +0000 | [diff] [blame] | 59 | ** Given 1 to 3 identifiers preceeding the JOIN keyword, determine the |
| 60 | ** type of join. Return an integer constant that expresses that type |
| 61 | ** in terms of the following bit values: |
| 62 | ** |
| 63 | ** JT_INNER |
| 64 | ** JT_OUTER |
| 65 | ** JT_NATURAL |
| 66 | ** JT_LEFT |
| 67 | ** JT_RIGHT |
| 68 | ** |
| 69 | ** A full outer join is the combination of JT_LEFT and JT_RIGHT. |
| 70 | ** |
| 71 | ** If an illegal or unsupported join type is seen, then still return |
| 72 | ** a join type, but put an error in the pParse structure. |
| 73 | */ |
| 74 | int sqliteJoinType(Parse *pParse, Token *pA, Token *pB, Token *pC){ |
| 75 | int jointype = 0; |
| 76 | Token *apAll[3]; |
| 77 | Token *p; |
| 78 | static struct { |
| 79 | const char *zKeyword; |
| 80 | int nChar; |
| 81 | int code; |
| 82 | } keywords[] = { |
| 83 | { "natural", 7, JT_NATURAL }, |
drh | 195e696 | 2002-05-25 00:18:20 +0000 | [diff] [blame^] | 84 | { "left", 4, JT_LEFT|JT_OUTER }, |
| 85 | { "right", 5, JT_RIGHT|JT_OUTER }, |
| 86 | { "full", 4, JT_LEFT|JT_RIGHT|JT_OUTER }, |
drh | 01f3f25 | 2002-05-24 16:14:15 +0000 | [diff] [blame] | 87 | { "outer", 5, JT_OUTER }, |
| 88 | { "inner", 5, JT_INNER }, |
| 89 | { "cross", 5, JT_INNER }, |
| 90 | }; |
| 91 | int i, j; |
| 92 | apAll[0] = pA; |
| 93 | apAll[1] = pB; |
| 94 | apAll[2] = pC; |
drh | 195e696 | 2002-05-25 00:18:20 +0000 | [diff] [blame^] | 95 | for(i=0; i<3 && apAll[i]; i++){ |
drh | 01f3f25 | 2002-05-24 16:14:15 +0000 | [diff] [blame] | 96 | p = apAll[i]; |
| 97 | for(j=0; j<sizeof(keywords)/sizeof(keywords[0]); j++){ |
| 98 | if( p->n==keywords[j].nChar |
| 99 | && sqliteStrNICmp(p->z, keywords[j].zKeyword, p->n)==0 ){ |
| 100 | jointype |= keywords[j].code; |
| 101 | break; |
| 102 | } |
| 103 | } |
| 104 | if( j>=sizeof(keywords)/sizeof(keywords[0]) ){ |
| 105 | jointype |= JT_ERROR; |
| 106 | break; |
| 107 | } |
| 108 | } |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 109 | if( |
| 110 | (jointype & (JT_INNER|JT_OUTER))==(JT_INNER|JT_OUTER) || |
drh | 195e696 | 2002-05-25 00:18:20 +0000 | [diff] [blame^] | 111 | (jointype & JT_ERROR)!=0 |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 112 | ){ |
drh | 01f3f25 | 2002-05-24 16:14:15 +0000 | [diff] [blame] | 113 | static Token dummy = { 0, 0 }; |
| 114 | char *zSp1 = " ", *zSp2 = " "; |
| 115 | if( pB==0 ){ pB = &dummy; zSp1 = 0; } |
| 116 | if( pC==0 ){ pC = &dummy; zSp2 = 0; } |
| 117 | sqliteSetNString(&pParse->zErrMsg, "unknown or unsupported join type: ", 0, |
| 118 | pA->z, pA->n, zSp1, 1, pB->z, pB->n, zSp2, 1, pC->z, pC->n, 0); |
| 119 | pParse->nErr++; |
| 120 | jointype = JT_INNER; |
drh | 195e696 | 2002-05-25 00:18:20 +0000 | [diff] [blame^] | 121 | }else if( jointype & JT_RIGHT ){ |
| 122 | sqliteSetString(&pParse->zErrMsg, |
| 123 | "RIGHT and FULL OUTER JOINs are not currently supported", 0); |
| 124 | pParse->nErr++; |
| 125 | jointype = JT_INNER; |
drh | 01f3f25 | 2002-05-24 16:14:15 +0000 | [diff] [blame] | 126 | } |
| 127 | return jointype; |
| 128 | } |
| 129 | |
| 130 | /* |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 131 | ** Return the index of a column in a table. Return -1 if the column |
| 132 | ** is not contained in the table. |
| 133 | */ |
| 134 | static int columnIndex(Table *pTab, const char *zCol){ |
| 135 | int i; |
| 136 | for(i=0; i<pTab->nCol; i++){ |
| 137 | if( sqliteStrICmp(pTab->aCol[i].zName, zCol)==0 ) return i; |
| 138 | } |
| 139 | return -1; |
| 140 | } |
| 141 | |
| 142 | /* |
| 143 | ** Add a term to the WHERE expression in *ppExpr that requires the |
| 144 | ** zCol column to be equal in the two tables pTab1 and pTab2. |
| 145 | */ |
| 146 | static void addWhereTerm( |
| 147 | const char *zCol, /* Name of the column */ |
| 148 | const Table *pTab1, /* First table */ |
| 149 | const Table *pTab2, /* Second table */ |
| 150 | Expr **ppExpr /* Add the equality term to this expression */ |
| 151 | ){ |
| 152 | Token dummy; |
| 153 | Expr *pE1a, *pE1b, *pE1c; |
| 154 | Expr *pE2a, *pE2b, *pE2c; |
| 155 | Expr *pE; |
| 156 | |
| 157 | dummy.z = zCol; |
| 158 | dummy.n = strlen(zCol); |
| 159 | pE1a = sqliteExpr(TK_ID, 0, 0, &dummy); |
| 160 | pE2a = sqliteExpr(TK_ID, 0, 0, &dummy); |
| 161 | dummy.z = pTab1->zName; |
| 162 | dummy.n = strlen(dummy.z); |
| 163 | pE1b = sqliteExpr(TK_ID, 0, 0, &dummy); |
| 164 | dummy.z = pTab2->zName; |
| 165 | dummy.n = strlen(dummy.z); |
| 166 | pE2b = sqliteExpr(TK_ID, 0, 0, &dummy); |
| 167 | pE1c = sqliteExpr(TK_DOT, pE1b, pE1a, 0); |
| 168 | pE2c = sqliteExpr(TK_DOT, pE2b, pE2a, 0); |
| 169 | pE = sqliteExpr(TK_EQ, pE1c, pE2c, 0); |
| 170 | if( *ppExpr ){ |
| 171 | *ppExpr = sqliteExpr(TK_AND, *ppExpr, pE, 0); |
| 172 | }else{ |
| 173 | *ppExpr = pE; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | /* |
| 178 | ** This routine processes the join information for a SELECT statement. |
| 179 | ** ON and USING clauses are converted into extra terms of the WHERE clause. |
| 180 | ** NATURAL joins also create extra WHERE clause terms. |
| 181 | ** |
| 182 | ** This routine returns the number of errors encountered. |
| 183 | */ |
| 184 | static int sqliteProcessJoin(Parse *pParse, Select *p){ |
| 185 | SrcList *pSrc; |
| 186 | int i, j; |
| 187 | pSrc = p->pSrc; |
| 188 | for(i=0; i<pSrc->nSrc-1; i++){ |
| 189 | struct SrcList_item *pTerm = &pSrc->a[i]; |
| 190 | struct SrcList_item *pOther = &pSrc->a[i+1]; |
| 191 | |
| 192 | if( pTerm->pTab==0 || pOther->pTab==0 ) continue; |
| 193 | |
| 194 | /* When the NATURAL keyword is present, add WHERE clause terms for |
| 195 | ** every column that the two tables have in common. |
| 196 | */ |
| 197 | if( pTerm->jointype & JT_NATURAL ){ |
| 198 | Table *pTab; |
| 199 | if( pTerm->pOn || pTerm->pUsing ){ |
| 200 | sqliteSetString(&pParse->zErrMsg, "a NATURAL join may not have " |
| 201 | "an ON or USING clause", 0); |
| 202 | pParse->nErr++; |
| 203 | return 1; |
| 204 | } |
| 205 | pTab = pTerm->pTab; |
| 206 | for(j=0; j<pTab->nCol; j++){ |
| 207 | if( columnIndex(pOther->pTab, pTab->aCol[j].zName)>=0 ){ |
| 208 | addWhereTerm(pTab->aCol[j].zName, pTab, pOther->pTab, &p->pWhere); |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | /* Disallow both ON and USING clauses in the same join |
| 214 | */ |
| 215 | if( pTerm->pOn && pTerm->pUsing ){ |
| 216 | sqliteSetString(&pParse->zErrMsg, "cannot have both ON and USING " |
| 217 | "clauses in the same join", 0); |
| 218 | pParse->nErr++; |
| 219 | return 1; |
| 220 | } |
| 221 | |
| 222 | /* Add the ON clause to the end of the WHERE clause, connected by |
| 223 | ** and AND operator. |
| 224 | */ |
| 225 | if( pTerm->pOn ){ |
| 226 | if( p->pWhere==0 ){ |
| 227 | p->pWhere = pTerm->pOn; |
| 228 | }else{ |
| 229 | p->pWhere = sqliteExpr(TK_AND, p->pWhere, pTerm->pOn, 0); |
| 230 | } |
| 231 | pTerm->pOn = 0; |
| 232 | } |
| 233 | |
| 234 | /* Create extra terms on the WHERE clause for each column named |
| 235 | ** in the USING clause. Example: If the two tables to be joined are |
| 236 | ** A and B and the USING clause names X, Y, and Z, then add this |
| 237 | ** to the WHERE clause: A.X=B.X AND A.Y=B.Y AND A.Z=B.Z |
| 238 | ** Report an error if any column mentioned in the USING clause is |
| 239 | ** not contained in both tables to be joined. |
| 240 | */ |
| 241 | if( pTerm->pUsing ){ |
| 242 | IdList *pList; |
| 243 | int j; |
| 244 | assert( i<pSrc->nSrc-1 ); |
| 245 | pList = pTerm->pUsing; |
| 246 | for(j=0; j<pList->nId; j++){ |
| 247 | if( columnIndex(pTerm->pTab, pList->a[i].zName)<0 || |
| 248 | columnIndex(pOther->pTab, pList->a[i].zName)<0 ){ |
| 249 | sqliteSetString(&pParse->zErrMsg, "cannot join using column ", |
| 250 | pList->a[i].zName, " - column not present in both tables", 0); |
| 251 | pParse->nErr++; |
| 252 | return 1; |
| 253 | } |
| 254 | addWhereTerm(pList->a[i].zName, pTerm->pTab, pOther->pTab, &p->pWhere); |
| 255 | } |
| 256 | } |
| 257 | } |
| 258 | return 0; |
| 259 | } |
| 260 | |
| 261 | /* |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 262 | ** Delete the given Select structure and all of its substructures. |
| 263 | */ |
| 264 | void sqliteSelectDelete(Select *p){ |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 265 | if( p==0 ) return; |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 266 | sqliteExprListDelete(p->pEList); |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 267 | sqliteSrcListDelete(p->pSrc); |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 268 | sqliteExprDelete(p->pWhere); |
| 269 | sqliteExprListDelete(p->pGroupBy); |
| 270 | sqliteExprDelete(p->pHaving); |
| 271 | sqliteExprListDelete(p->pOrderBy); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 272 | sqliteSelectDelete(p->pPrior); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 273 | sqliteFree(p->zSelect); |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 274 | sqliteFree(p); |
| 275 | } |
| 276 | |
| 277 | /* |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 278 | ** Delete the aggregate information from the parse structure. |
| 279 | */ |
drh | 1d83f05 | 2002-02-17 00:30:36 +0000 | [diff] [blame] | 280 | static void sqliteAggregateInfoReset(Parse *pParse){ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 281 | sqliteFree(pParse->aAgg); |
| 282 | pParse->aAgg = 0; |
| 283 | pParse->nAgg = 0; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 284 | pParse->useAgg = 0; |
| 285 | } |
| 286 | |
| 287 | /* |
| 288 | ** This routine generates the code for the inside of the inner loop |
| 289 | ** of a SELECT. |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 290 | ** |
| 291 | ** The pEList is used to determine the values for each column in the |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 292 | ** result row. Except if pEList==NULL, then we just read nColumn |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 293 | ** elements from the srcTab table. |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 294 | */ |
| 295 | static int selectInnerLoop( |
| 296 | Parse *pParse, /* The parser context */ |
| 297 | ExprList *pEList, /* List of values being extracted */ |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 298 | int srcTab, /* Pull data from this table */ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 299 | int nColumn, /* Number of columns in the source table */ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 300 | ExprList *pOrderBy, /* If not NULL, sort results using this key */ |
| 301 | int distinct, /* If >=0, make sure results are distinct */ |
| 302 | int eDest, /* How to dispose of the results */ |
| 303 | int iParm, /* An argument to the disposal method */ |
| 304 | int iContinue, /* Jump here to continue with next row */ |
| 305 | int iBreak /* Jump here to break out of the inner loop */ |
| 306 | ){ |
| 307 | Vdbe *v = pParse->pVdbe; |
| 308 | int i; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 309 | if( v==0 ) return 0; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 310 | |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 311 | /* Pull the requested columns. |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 312 | */ |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 313 | if( pEList ){ |
| 314 | for(i=0; i<pEList->nExpr; i++){ |
| 315 | sqliteExprCode(pParse, pEList->a[i].pExpr); |
| 316 | } |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 317 | nColumn = pEList->nExpr; |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 318 | }else{ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 319 | for(i=0; i<nColumn; i++){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 320 | sqliteVdbeAddOp(v, OP_Column, srcTab, i); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 321 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 322 | } |
| 323 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 324 | /* If the DISTINCT keyword was present on the SELECT statement |
| 325 | ** and this row has been seen before, then do not make this row |
| 326 | ** part of the result. |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 327 | */ |
| 328 | if( distinct>=0 ){ |
| 329 | int lbl = sqliteVdbeMakeLabel(v); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 330 | sqliteVdbeAddOp(v, OP_MakeKey, pEList->nExpr, 1); |
| 331 | sqliteVdbeAddOp(v, OP_Distinct, distinct, lbl); |
| 332 | sqliteVdbeAddOp(v, OP_Pop, pEList->nExpr+1, 0); |
| 333 | sqliteVdbeAddOp(v, OP_Goto, 0, iContinue); |
| 334 | sqliteVdbeResolveLabel(v, lbl); |
| 335 | sqliteVdbeAddOp(v, OP_String, 0, 0); |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 336 | sqliteVdbeAddOp(v, OP_PutStrKey, distinct, 0); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 337 | } |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 338 | |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 339 | /* If there is an ORDER BY clause, then store the results |
| 340 | ** in a sorter. |
| 341 | */ |
| 342 | if( pOrderBy ){ |
| 343 | char *zSortOrder; |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 344 | sqliteVdbeAddOp(v, OP_SortMakeRec, nColumn, 0); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 345 | zSortOrder = sqliteMalloc( pOrderBy->nExpr + 1 ); |
| 346 | if( zSortOrder==0 ) return 1; |
| 347 | for(i=0; i<pOrderBy->nExpr; i++){ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 348 | zSortOrder[i] = pOrderBy->a[i].sortOrder ? '-' : '+'; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 349 | sqliteExprCode(pParse, pOrderBy->a[i].pExpr); |
| 350 | } |
| 351 | zSortOrder[pOrderBy->nExpr] = 0; |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 352 | sqliteVdbeAddOp(v, OP_SortMakeKey, pOrderBy->nExpr, 0); |
| 353 | sqliteVdbeChangeP3(v, -1, zSortOrder, strlen(zSortOrder)); |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 354 | sqliteFree(zSortOrder); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 355 | sqliteVdbeAddOp(v, OP_SortPut, 0, 0); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 356 | }else |
| 357 | |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 358 | /* In this mode, write each query result to the key of the temporary |
| 359 | ** table iParm. |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 360 | */ |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 361 | if( eDest==SRT_Union ){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 362 | sqliteVdbeAddOp(v, OP_MakeRecord, nColumn, 0); |
| 363 | sqliteVdbeAddOp(v, OP_String, iParm, 0); |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 364 | sqliteVdbeAddOp(v, OP_PutStrKey, iParm, 0); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 365 | }else |
| 366 | |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 367 | /* Store the result as data using a unique key. |
| 368 | */ |
drh | 2d0794e | 2002-03-03 03:03:52 +0000 | [diff] [blame] | 369 | if( eDest==SRT_Table || eDest==SRT_TempTable ){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 370 | sqliteVdbeAddOp(v, OP_MakeRecord, nColumn, 0); |
| 371 | sqliteVdbeAddOp(v, OP_NewRecno, iParm, 0); |
| 372 | sqliteVdbeAddOp(v, OP_Pull, 1, 0); |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 373 | sqliteVdbeAddOp(v, OP_PutIntKey, iParm, 0); |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 374 | }else |
| 375 | |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 376 | /* Construct a record from the query result, but instead of |
| 377 | ** saving that record, use it as a key to delete elements from |
| 378 | ** the temporary table iParm. |
| 379 | */ |
| 380 | if( eDest==SRT_Except ){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 381 | int addr = sqliteVdbeAddOp(v, OP_MakeRecord, nColumn, 0); |
| 382 | sqliteVdbeAddOp(v, OP_NotFound, iParm, addr+3); |
| 383 | sqliteVdbeAddOp(v, OP_Delete, iParm, 0); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 384 | }else |
| 385 | |
| 386 | /* If we are creating a set for an "expr IN (SELECT ...)" construct, |
| 387 | ** then there should be a single item on the stack. Write this |
| 388 | ** item into the set table with bogus data. |
| 389 | */ |
| 390 | if( eDest==SRT_Set ){ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 391 | assert( nColumn==1 ); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 392 | sqliteVdbeAddOp(v, OP_String, 0, 0); |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 393 | sqliteVdbeAddOp(v, OP_PutStrKey, iParm, 0); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 394 | }else |
| 395 | |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 396 | |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 397 | /* If this is a scalar select that is part of an expression, then |
| 398 | ** store the results in the appropriate memory cell and break out |
| 399 | ** of the scan loop. |
| 400 | */ |
| 401 | if( eDest==SRT_Mem ){ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 402 | assert( nColumn==1 ); |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 403 | sqliteVdbeAddOp(v, OP_MemStore, iParm, 1); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 404 | sqliteVdbeAddOp(v, OP_Goto, 0, iBreak); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 405 | }else |
| 406 | |
| 407 | /* If none of the above, send the data to the callback function. |
| 408 | */ |
| 409 | { |
drh | 9bbca4c | 2001-11-06 04:00:18 +0000 | [diff] [blame] | 410 | sqliteVdbeAddOp(v, OP_Callback, nColumn, iBreak); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 411 | } |
| 412 | return 0; |
| 413 | } |
| 414 | |
| 415 | /* |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 416 | ** If the inner loop was generated using a non-null pOrderBy argument, |
| 417 | ** then the results were placed in a sorter. After the loop is terminated |
| 418 | ** we need to run the sorter and output the results. The following |
| 419 | ** routine generates the code needed to do that. |
| 420 | */ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 421 | static void generateSortTail(Vdbe *v, int nColumn){ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 422 | int end = sqliteVdbeMakeLabel(v); |
| 423 | int addr; |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 424 | sqliteVdbeAddOp(v, OP_Sort, 0, 0); |
| 425 | addr = sqliteVdbeAddOp(v, OP_SortNext, 0, end); |
drh | 9bbca4c | 2001-11-06 04:00:18 +0000 | [diff] [blame] | 426 | sqliteVdbeAddOp(v, OP_SortCallback, nColumn, end); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 427 | sqliteVdbeAddOp(v, OP_Goto, 0, addr); |
| 428 | sqliteVdbeResolveLabel(v, end); |
drh | a8b38d2 | 2001-11-01 14:41:34 +0000 | [diff] [blame] | 429 | sqliteVdbeAddOp(v, OP_SortReset, 0, 0); |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | /* |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 433 | ** Generate code that will tell the VDBE how many columns there |
| 434 | ** are in the result and the name for each column. This information |
| 435 | ** is used to provide "argc" and "azCol[]" values in the callback. |
| 436 | */ |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 437 | static void generateColumnNames( |
| 438 | Parse *pParse, /* Parser context */ |
| 439 | int base, /* VDBE cursor corresponding to first entry in pTabList */ |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 440 | SrcList *pTabList, /* List of tables */ |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 441 | ExprList *pEList /* Expressions defining the result set */ |
| 442 | ){ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 443 | Vdbe *v = pParse->pVdbe; |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 444 | int i; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 445 | if( pParse->colNamesSet || v==0 || sqlite_malloc_failed ) return; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 446 | pParse->colNamesSet = 1; |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 447 | sqliteVdbeAddOp(v, OP_ColumnCount, pEList->nExpr, 0); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 448 | for(i=0; i<pEList->nExpr; i++){ |
| 449 | Expr *p; |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 450 | int showFullNames; |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 451 | if( pEList->a[i].zName ){ |
| 452 | char *zName = pEList->a[i].zName; |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 453 | sqliteVdbeAddOp(v, OP_ColumnName, i, 0); |
| 454 | sqliteVdbeChangeP3(v, -1, zName, strlen(zName)); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 455 | continue; |
| 456 | } |
| 457 | p = pEList->a[i].pExpr; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 458 | if( p==0 ) continue; |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 459 | showFullNames = (pParse->db->flags & SQLITE_FullColNames)!=0; |
| 460 | if( p->span.z && p->span.z[0] && !showFullNames ){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 461 | int addr = sqliteVdbeAddOp(v,OP_ColumnName, i, 0); |
| 462 | sqliteVdbeChangeP3(v, -1, p->span.z, p->span.n); |
drh | e1b6a5b | 2000-07-29 13:06:59 +0000 | [diff] [blame] | 463 | sqliteVdbeCompressSpace(v, addr); |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 464 | }else if( p->op==TK_COLUMN && pTabList ){ |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 465 | Table *pTab = pTabList->a[p->iTable - base].pTab; |
drh | 9766587 | 2002-02-13 23:22:53 +0000 | [diff] [blame] | 466 | char *zCol; |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 467 | int iCol = p->iColumn; |
| 468 | if( iCol<0 ) iCol = pTab->iPKey; |
drh | 9766587 | 2002-02-13 23:22:53 +0000 | [diff] [blame] | 469 | assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) ); |
| 470 | zCol = iCol<0 ? "_ROWID_" : pTab->aCol[iCol].zName; |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 471 | if( pTabList->nSrc>1 || showFullNames ){ |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 472 | char *zName = 0; |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 473 | char *zTab; |
| 474 | |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 475 | zTab = pTabList->a[p->iTable - base].zAlias; |
drh | 01a3466 | 2001-10-20 12:30:10 +0000 | [diff] [blame] | 476 | if( showFullNames || zTab==0 ) zTab = pTab->zName; |
drh | 9766587 | 2002-02-13 23:22:53 +0000 | [diff] [blame] | 477 | sqliteSetString(&zName, zTab, ".", zCol, 0); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 478 | sqliteVdbeAddOp(v, OP_ColumnName, i, 0); |
| 479 | sqliteVdbeChangeP3(v, -1, zName, strlen(zName)); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 480 | sqliteFree(zName); |
| 481 | }else{ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 482 | sqliteVdbeAddOp(v, OP_ColumnName, i, 0); |
drh | 22f70c3 | 2002-02-18 01:17:00 +0000 | [diff] [blame] | 483 | sqliteVdbeChangeP3(v, -1, zCol, 0); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 484 | } |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 485 | }else if( p->span.z && p->span.z[0] ){ |
| 486 | int addr = sqliteVdbeAddOp(v,OP_ColumnName, i, 0); |
| 487 | sqliteVdbeChangeP3(v, -1, p->span.z, p->span.n); |
| 488 | sqliteVdbeCompressSpace(v, addr); |
| 489 | }else{ |
| 490 | char zName[30]; |
| 491 | assert( p->op!=TK_COLUMN || pTabList==0 ); |
| 492 | sprintf(zName, "column%d", i+1); |
| 493 | sqliteVdbeAddOp(v, OP_ColumnName, i, 0); |
| 494 | sqliteVdbeChangeP3(v, -1, zName, strlen(zName)); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 495 | } |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | /* |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 500 | ** Name of the connection operator, used for error messages. |
| 501 | */ |
| 502 | static const char *selectOpName(int id){ |
| 503 | char *z; |
| 504 | switch( id ){ |
| 505 | case TK_ALL: z = "UNION ALL"; break; |
| 506 | case TK_INTERSECT: z = "INTERSECT"; break; |
| 507 | case TK_EXCEPT: z = "EXCEPT"; break; |
| 508 | default: z = "UNION"; break; |
| 509 | } |
| 510 | return z; |
| 511 | } |
| 512 | |
| 513 | /* |
drh | 22f70c3 | 2002-02-18 01:17:00 +0000 | [diff] [blame] | 514 | ** Given a SELECT statement, generate a Table structure that describes |
| 515 | ** the result set of that SELECT. |
| 516 | */ |
| 517 | Table *sqliteResultSetOfSelect(Parse *pParse, char *zTabName, Select *pSelect){ |
| 518 | Table *pTab; |
| 519 | int i; |
| 520 | ExprList *pEList; |
| 521 | static int fillInColumnList(Parse*, Select*); |
| 522 | |
| 523 | if( fillInColumnList(pParse, pSelect) ){ |
| 524 | return 0; |
| 525 | } |
| 526 | pTab = sqliteMalloc( sizeof(Table) ); |
| 527 | if( pTab==0 ){ |
| 528 | return 0; |
| 529 | } |
| 530 | pTab->zName = zTabName ? sqliteStrDup(zTabName) : 0; |
| 531 | pEList = pSelect->pEList; |
| 532 | pTab->nCol = pEList->nExpr; |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 533 | assert( pTab->nCol>0 ); |
drh | 22f70c3 | 2002-02-18 01:17:00 +0000 | [diff] [blame] | 534 | pTab->aCol = sqliteMalloc( sizeof(pTab->aCol[0])*pTab->nCol ); |
| 535 | for(i=0; i<pTab->nCol; i++){ |
| 536 | Expr *p; |
| 537 | if( pEList->a[i].zName ){ |
| 538 | pTab->aCol[i].zName = sqliteStrDup(pEList->a[i].zName); |
| 539 | }else if( (p=pEList->a[i].pExpr)->span.z && p->span.z[0] ){ |
| 540 | sqliteSetNString(&pTab->aCol[i].zName, p->span.z, p->span.n, 0); |
drh | d820cb1 | 2002-02-18 03:21:45 +0000 | [diff] [blame] | 541 | }else if( p->op==TK_DOT && p->pRight && p->pRight->token.z && |
| 542 | p->pRight->token.z[0] ){ |
| 543 | sqliteSetNString(&pTab->aCol[i].zName, |
| 544 | p->pRight->token.z, p->pRight->token.n, 0); |
drh | 22f70c3 | 2002-02-18 01:17:00 +0000 | [diff] [blame] | 545 | }else{ |
| 546 | char zBuf[30]; |
| 547 | sprintf(zBuf, "column%d", i+1); |
| 548 | pTab->aCol[i].zName = sqliteStrDup(zBuf); |
| 549 | } |
| 550 | } |
| 551 | pTab->iPKey = -1; |
| 552 | return pTab; |
| 553 | } |
| 554 | |
| 555 | /* |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 556 | ** For the given SELECT statement, do three things. |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 557 | ** |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 558 | ** (1) Fill in the pTabList->a[].pTab fields in the SrcList that |
drh | 22f70c3 | 2002-02-18 01:17:00 +0000 | [diff] [blame] | 559 | ** defines the set of tables that should be scanned. |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 560 | ** |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 561 | ** (2) Add terms to the WHERE clause to accomodate the NATURAL keyword |
| 562 | ** on joins and the ON and USING clause of joins. |
| 563 | ** |
| 564 | ** (3) Scan the list of columns in the result set (pEList) looking |
drh | 5447322 | 2002-04-04 02:10:55 +0000 | [diff] [blame] | 565 | ** for instances of the "*" operator or the TABLE.* operator. |
| 566 | ** If found, expand each "*" to be every column in every table |
| 567 | ** and TABLE.* to be every column in TABLE. |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 568 | ** |
| 569 | ** Return 0 on success. If there are problems, leave an error message |
| 570 | ** in pParse and return non-zero. |
| 571 | */ |
| 572 | static int fillInColumnList(Parse *pParse, Select *p){ |
drh | 5447322 | 2002-04-04 02:10:55 +0000 | [diff] [blame] | 573 | int i, j, k, rc; |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 574 | SrcList *pTabList; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 575 | ExprList *pEList; |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 576 | Table *pTab; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 577 | |
| 578 | if( p==0 || p->pSrc==0 ) return 1; |
| 579 | pTabList = p->pSrc; |
| 580 | pEList = p->pEList; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 581 | |
| 582 | /* Look up every table in the table list. |
| 583 | */ |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 584 | for(i=0; i<pTabList->nSrc; i++){ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 585 | if( pTabList->a[i].pTab ){ |
| 586 | /* This routine has run before! No need to continue */ |
| 587 | return 0; |
| 588 | } |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 589 | if( pTabList->a[i].zName==0 ){ |
drh | 22f70c3 | 2002-02-18 01:17:00 +0000 | [diff] [blame] | 590 | /* A sub-query in the FROM clause of a SELECT */ |
drh | 22f70c3 | 2002-02-18 01:17:00 +0000 | [diff] [blame] | 591 | assert( pTabList->a[i].pSelect!=0 ); |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 592 | if( pTabList->a[i].zAlias==0 ){ |
| 593 | char zFakeName[60]; |
| 594 | sprintf(zFakeName, "sqlite_subquery_%p_", |
| 595 | (void*)pTabList->a[i].pSelect); |
| 596 | sqliteSetString(&pTabList->a[i].zAlias, zFakeName, 0); |
| 597 | } |
drh | 22f70c3 | 2002-02-18 01:17:00 +0000 | [diff] [blame] | 598 | pTabList->a[i].pTab = pTab = |
| 599 | sqliteResultSetOfSelect(pParse, pTabList->a[i].zAlias, |
| 600 | pTabList->a[i].pSelect); |
| 601 | if( pTab==0 ){ |
| 602 | return 1; |
| 603 | } |
| 604 | pTab->isTransient = 1; |
| 605 | }else{ |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 606 | /* An ordinary table or view name in the FROM clause */ |
| 607 | pTabList->a[i].pTab = pTab = |
| 608 | sqliteFindTable(pParse->db, pTabList->a[i].zName); |
| 609 | if( pTab==0 ){ |
drh | 22f70c3 | 2002-02-18 01:17:00 +0000 | [diff] [blame] | 610 | sqliteSetString(&pParse->zErrMsg, "no such table: ", |
| 611 | pTabList->a[i].zName, 0); |
| 612 | pParse->nErr++; |
| 613 | return 1; |
| 614 | } |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 615 | if( pTab->pSelect ){ |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 616 | if( sqliteViewGetColumnNames(pParse, pTab) ){ |
| 617 | return 1; |
| 618 | } |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 619 | pTabList->a[i].pSelect = sqliteSelectDup(pTab->pSelect); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 620 | } |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 621 | } |
| 622 | } |
| 623 | |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 624 | /* Process NATURAL keywords, and ON and USING clauses of joins. |
| 625 | */ |
| 626 | if( sqliteProcessJoin(pParse, p) ) return 1; |
| 627 | |
drh | 7c917d1 | 2001-12-16 20:05:05 +0000 | [diff] [blame] | 628 | /* For every "*" that occurs in the column list, insert the names of |
drh | 5447322 | 2002-04-04 02:10:55 +0000 | [diff] [blame] | 629 | ** all columns in all tables. And for every TABLE.* insert the names |
| 630 | ** of all columns in TABLE. The parser inserted a special expression |
drh | 7c917d1 | 2001-12-16 20:05:05 +0000 | [diff] [blame] | 631 | ** with the TK_ALL operator for each "*" that it found in the column list. |
| 632 | ** The following code just has to locate the TK_ALL expressions and expand |
| 633 | ** each one to the list of all columns in all tables. |
drh | 5447322 | 2002-04-04 02:10:55 +0000 | [diff] [blame] | 634 | ** |
| 635 | ** The first loop just checks to see if there are any "*" operators |
| 636 | ** that need expanding. |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 637 | */ |
drh | 7c917d1 | 2001-12-16 20:05:05 +0000 | [diff] [blame] | 638 | for(k=0; k<pEList->nExpr; k++){ |
drh | 5447322 | 2002-04-04 02:10:55 +0000 | [diff] [blame] | 639 | Expr *pE = pEList->a[k].pExpr; |
| 640 | if( pE->op==TK_ALL ) break; |
| 641 | if( pE->op==TK_DOT && pE->pRight && pE->pRight->op==TK_ALL |
| 642 | && pE->pLeft && pE->pLeft->op==TK_ID ) break; |
drh | 7c917d1 | 2001-12-16 20:05:05 +0000 | [diff] [blame] | 643 | } |
drh | 5447322 | 2002-04-04 02:10:55 +0000 | [diff] [blame] | 644 | rc = 0; |
drh | 7c917d1 | 2001-12-16 20:05:05 +0000 | [diff] [blame] | 645 | if( k<pEList->nExpr ){ |
drh | 5447322 | 2002-04-04 02:10:55 +0000 | [diff] [blame] | 646 | /* |
| 647 | ** If we get here it means the result set contains one or more "*" |
| 648 | ** operators that need to be expanded. Loop through each expression |
| 649 | ** in the result set and expand them one by one. |
| 650 | */ |
drh | 7c917d1 | 2001-12-16 20:05:05 +0000 | [diff] [blame] | 651 | struct ExprList_item *a = pEList->a; |
| 652 | ExprList *pNew = 0; |
| 653 | for(k=0; k<pEList->nExpr; k++){ |
drh | 5447322 | 2002-04-04 02:10:55 +0000 | [diff] [blame] | 654 | Expr *pE = a[k].pExpr; |
| 655 | if( pE->op!=TK_ALL && |
| 656 | (pE->op!=TK_DOT || pE->pRight==0 || pE->pRight->op!=TK_ALL) ){ |
| 657 | /* This particular expression does not need to be expanded. |
| 658 | */ |
drh | 7c917d1 | 2001-12-16 20:05:05 +0000 | [diff] [blame] | 659 | pNew = sqliteExprListAppend(pNew, a[k].pExpr, 0); |
| 660 | pNew->a[pNew->nExpr-1].zName = a[k].zName; |
| 661 | a[k].pExpr = 0; |
| 662 | a[k].zName = 0; |
| 663 | }else{ |
drh | 5447322 | 2002-04-04 02:10:55 +0000 | [diff] [blame] | 664 | /* This expression is a "*" or a "TABLE.*" and needs to be |
| 665 | ** expanded. */ |
| 666 | int tableSeen = 0; /* Set to 1 when TABLE matches */ |
| 667 | Token *pName; /* text of name of TABLE */ |
| 668 | if( pE->op==TK_DOT && pE->pLeft ){ |
| 669 | pName = &pE->pLeft->token; |
| 670 | }else{ |
| 671 | pName = 0; |
| 672 | } |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 673 | for(i=0; i<pTabList->nSrc; i++){ |
drh | 7c917d1 | 2001-12-16 20:05:05 +0000 | [diff] [blame] | 674 | Table *pTab = pTabList->a[i].pTab; |
drh | 5447322 | 2002-04-04 02:10:55 +0000 | [diff] [blame] | 675 | char *zTabName = pTabList->a[i].zAlias; |
| 676 | if( zTabName==0 || zTabName[0]==0 ){ |
| 677 | zTabName = pTab->zName; |
| 678 | } |
| 679 | if( pName && (zTabName==0 || zTabName[0]==0 || |
| 680 | sqliteStrNICmp(pName->z, zTabName, pName->n)!=0) ){ |
| 681 | continue; |
| 682 | } |
| 683 | tableSeen = 1; |
drh | 7c917d1 | 2001-12-16 20:05:05 +0000 | [diff] [blame] | 684 | for(j=0; j<pTab->nCol; j++){ |
drh | 22f70c3 | 2002-02-18 01:17:00 +0000 | [diff] [blame] | 685 | Expr *pExpr, *pLeft, *pRight; |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 686 | char *zName = pTab->aCol[j].zName; |
| 687 | |
| 688 | if( i>0 && (pTabList->a[i-1].jointype & JT_NATURAL)!=0 && |
| 689 | columnIndex(pTabList->a[i-1].pTab, zName)>=0 ){ |
| 690 | /* In a NATURAL join, omit the join columns from the |
| 691 | ** table on the right */ |
| 692 | continue; |
| 693 | } |
| 694 | if( i>0 && sqliteIdListIndex(pTabList->a[i-1].pUsing, zName)>=0 ){ |
| 695 | /* In a join with a USING clause, omit columns in the |
| 696 | ** using clause from the table on the right. */ |
| 697 | continue; |
| 698 | } |
drh | 22f70c3 | 2002-02-18 01:17:00 +0000 | [diff] [blame] | 699 | pRight = sqliteExpr(TK_ID, 0, 0, 0); |
| 700 | if( pRight==0 ) break; |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 701 | pRight->token.z = zName; |
| 702 | pRight->token.n = strlen(zName); |
drh | 5447322 | 2002-04-04 02:10:55 +0000 | [diff] [blame] | 703 | if( zTabName ){ |
drh | 22f70c3 | 2002-02-18 01:17:00 +0000 | [diff] [blame] | 704 | pLeft = sqliteExpr(TK_ID, 0, 0, 0); |
| 705 | if( pLeft==0 ) break; |
drh | 5447322 | 2002-04-04 02:10:55 +0000 | [diff] [blame] | 706 | pLeft->token.z = zTabName; |
| 707 | pLeft->token.n = strlen(zTabName); |
drh | 22f70c3 | 2002-02-18 01:17:00 +0000 | [diff] [blame] | 708 | pExpr = sqliteExpr(TK_DOT, pLeft, pRight, 0); |
| 709 | if( pExpr==0 ) break; |
drh | 7c917d1 | 2001-12-16 20:05:05 +0000 | [diff] [blame] | 710 | }else{ |
drh | 22f70c3 | 2002-02-18 01:17:00 +0000 | [diff] [blame] | 711 | pExpr = pRight; |
| 712 | pExpr->span = pExpr->token; |
drh | 7c917d1 | 2001-12-16 20:05:05 +0000 | [diff] [blame] | 713 | } |
drh | 7c917d1 | 2001-12-16 20:05:05 +0000 | [diff] [blame] | 714 | pNew = sqliteExprListAppend(pNew, pExpr, 0); |
| 715 | } |
drh | 17e24df | 2001-11-06 14:10:41 +0000 | [diff] [blame] | 716 | } |
drh | 5447322 | 2002-04-04 02:10:55 +0000 | [diff] [blame] | 717 | if( !tableSeen ){ |
| 718 | assert( pName!=0 ); |
| 719 | sqliteSetNString(&pParse->zErrMsg, "no such table: ", -1, |
| 720 | pName->z, pName->n, 0); |
| 721 | rc = 1; |
| 722 | } |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 723 | } |
| 724 | } |
drh | 7c917d1 | 2001-12-16 20:05:05 +0000 | [diff] [blame] | 725 | sqliteExprListDelete(pEList); |
| 726 | p->pEList = pNew; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 727 | } |
drh | 5447322 | 2002-04-04 02:10:55 +0000 | [diff] [blame] | 728 | return rc; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 729 | } |
| 730 | |
| 731 | /* |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 732 | ** This routine recursively unlinks the Select.pSrc.a[].pTab pointers |
| 733 | ** in a select structure. It just sets the pointers to NULL. This |
| 734 | ** routine is recursive in the sense that if the Select.pSrc.a[].pSelect |
| 735 | ** pointer is not NULL, this routine is called recursively on that pointer. |
| 736 | ** |
| 737 | ** This routine is called on the Select structure that defines a |
| 738 | ** VIEW in order to undo any bindings to tables. This is necessary |
| 739 | ** because those tables might be DROPed by a subsequent SQL command. |
| 740 | */ |
| 741 | void sqliteSelectUnbind(Select *p){ |
| 742 | int i; |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 743 | SrcList *pSrc = p->pSrc; |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 744 | Table *pTab; |
| 745 | if( p==0 ) return; |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 746 | for(i=0; i<pSrc->nSrc; i++){ |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 747 | if( (pTab = pSrc->a[i].pTab)!=0 ){ |
| 748 | if( pTab->isTransient ){ |
| 749 | sqliteDeleteTable(0, pTab); |
| 750 | sqliteSelectDelete(pSrc->a[i].pSelect); |
| 751 | pSrc->a[i].pSelect = 0; |
| 752 | } |
| 753 | pSrc->a[i].pTab = 0; |
| 754 | if( pSrc->a[i].pSelect ){ |
| 755 | sqliteSelectUnbind(pSrc->a[i].pSelect); |
| 756 | } |
| 757 | } |
| 758 | } |
| 759 | } |
| 760 | |
| 761 | /* |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 762 | ** This routine associates entries in an ORDER BY expression list with |
| 763 | ** columns in a result. For each ORDER BY expression, the opcode of |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 764 | ** 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] | 765 | ** the top-level node is filled in with column number and the iTable |
| 766 | ** value of the top-level node is filled with iTable parameter. |
| 767 | ** |
| 768 | ** If there are prior SELECT clauses, they are processed first. A match |
| 769 | ** in an earlier SELECT takes precedence over a later SELECT. |
| 770 | ** |
| 771 | ** Any entry that does not match is flagged as an error. The number |
| 772 | ** of errors is returned. |
| 773 | */ |
| 774 | static int matchOrderbyToColumn( |
| 775 | Parse *pParse, /* A place to leave error messages */ |
| 776 | Select *pSelect, /* Match to result columns of this SELECT */ |
| 777 | ExprList *pOrderBy, /* The ORDER BY values to match against columns */ |
| 778 | int iTable, /* Insert this this value in iTable */ |
| 779 | int mustComplete /* If TRUE all ORDER BYs must match */ |
| 780 | ){ |
| 781 | int nErr = 0; |
| 782 | int i, j; |
| 783 | ExprList *pEList; |
| 784 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 785 | if( pSelect==0 || pOrderBy==0 ) return 1; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 786 | if( mustComplete ){ |
| 787 | for(i=0; i<pOrderBy->nExpr; i++){ pOrderBy->a[i].done = 0; } |
| 788 | } |
| 789 | if( fillInColumnList(pParse, pSelect) ){ |
| 790 | return 1; |
| 791 | } |
| 792 | if( pSelect->pPrior ){ |
drh | 92cd52f | 2000-06-08 01:55:29 +0000 | [diff] [blame] | 793 | if( matchOrderbyToColumn(pParse, pSelect->pPrior, pOrderBy, iTable, 0) ){ |
| 794 | return 1; |
| 795 | } |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 796 | } |
| 797 | pEList = pSelect->pEList; |
| 798 | for(i=0; i<pOrderBy->nExpr; i++){ |
| 799 | Expr *pE = pOrderBy->a[i].pExpr; |
drh | 92cd52f | 2000-06-08 01:55:29 +0000 | [diff] [blame] | 800 | int match = 0; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 801 | if( pOrderBy->a[i].done ) continue; |
| 802 | for(j=0; j<pEList->nExpr; j++){ |
drh | 4cfa793 | 2000-06-08 15:10:46 +0000 | [diff] [blame] | 803 | if( pEList->a[j].zName && (pE->op==TK_ID || pE->op==TK_STRING) ){ |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 804 | char *zName, *zLabel; |
| 805 | zName = pEList->a[j].zName; |
| 806 | assert( pE->token.z ); |
| 807 | zLabel = sqliteStrNDup(pE->token.z, pE->token.n); |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 808 | sqliteDequote(zLabel); |
| 809 | if( sqliteStrICmp(zName, zLabel)==0 ){ |
| 810 | match = 1; |
| 811 | } |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 812 | sqliteFree(zLabel); |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 813 | } |
drh | 4cfa793 | 2000-06-08 15:10:46 +0000 | [diff] [blame] | 814 | if( match==0 && sqliteExprCompare(pE, pEList->a[j].pExpr) ){ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 815 | match = 1; |
| 816 | } |
| 817 | if( match ){ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 818 | pE->op = TK_COLUMN; |
| 819 | pE->iColumn = j; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 820 | pE->iTable = iTable; |
| 821 | pOrderBy->a[i].done = 1; |
| 822 | break; |
| 823 | } |
| 824 | } |
drh | 92cd52f | 2000-06-08 01:55:29 +0000 | [diff] [blame] | 825 | if( !match && mustComplete ){ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 826 | char zBuf[30]; |
| 827 | sprintf(zBuf,"%d",i+1); |
| 828 | sqliteSetString(&pParse->zErrMsg, "ORDER BY term number ", zBuf, |
| 829 | " does not match any result column", 0); |
| 830 | pParse->nErr++; |
| 831 | nErr++; |
| 832 | break; |
| 833 | } |
| 834 | } |
| 835 | return nErr; |
| 836 | } |
| 837 | |
| 838 | /* |
| 839 | ** Get a VDBE for the given parser context. Create a new one if necessary. |
| 840 | ** If an error occurs, return NULL and leave a message in pParse. |
| 841 | */ |
| 842 | Vdbe *sqliteGetVdbe(Parse *pParse){ |
| 843 | Vdbe *v = pParse->pVdbe; |
| 844 | if( v==0 ){ |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 845 | v = pParse->pVdbe = sqliteVdbeCreate(pParse->db); |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 846 | } |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 847 | return v; |
| 848 | } |
| 849 | |
| 850 | |
| 851 | /* |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 852 | ** This routine is called to process a query that is really the union |
| 853 | ** or intersection of two or more separate queries. |
| 854 | */ |
| 855 | static int multiSelect(Parse *pParse, Select *p, int eDest, int iParm){ |
drh | 10e5e3c | 2000-06-08 00:19:02 +0000 | [diff] [blame] | 856 | int rc; /* Success code from a subroutine */ |
| 857 | Select *pPrior; /* Another SELECT immediately to our left */ |
| 858 | Vdbe *v; /* Generate code to this VDBE */ |
| 859 | int base; /* Baseline value for pParse->nTab */ |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 860 | |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 861 | /* Make sure there is no ORDER BY clause on prior SELECTs. Only the |
| 862 | ** last SELECT in the series may have an ORDER BY. |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 863 | */ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 864 | if( p==0 || p->pPrior==0 ) return 1; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 865 | pPrior = p->pPrior; |
| 866 | if( pPrior->pOrderBy ){ |
| 867 | sqliteSetString(&pParse->zErrMsg,"ORDER BY clause should come after ", |
| 868 | selectOpName(p->op), " not before", 0); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 869 | pParse->nErr++; |
| 870 | return 1; |
| 871 | } |
| 872 | |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 873 | /* Make sure we have a valid query engine. If not, create a new one. |
| 874 | */ |
| 875 | v = sqliteGetVdbe(pParse); |
| 876 | if( v==0 ) return 1; |
| 877 | |
drh | 1cc3d75 | 2002-03-23 00:31:29 +0000 | [diff] [blame] | 878 | /* Create the destination temporary table if necessary |
| 879 | */ |
| 880 | if( eDest==SRT_TempTable ){ |
| 881 | sqliteVdbeAddOp(v, OP_OpenTemp, iParm, 0); |
| 882 | eDest = SRT_Table; |
| 883 | } |
| 884 | |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 885 | /* Process the UNION or INTERSECTION |
| 886 | */ |
drh | 10e5e3c | 2000-06-08 00:19:02 +0000 | [diff] [blame] | 887 | base = pParse->nTab; |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 888 | switch( p->op ){ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 889 | case TK_ALL: |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 890 | case TK_EXCEPT: |
| 891 | case TK_UNION: { |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 892 | int unionTab; /* Cursor number of the temporary table holding result */ |
| 893 | int op; /* One of the SRT_ operations to apply to self */ |
| 894 | int priorOp; /* The SRT_ operation to apply to prior selects */ |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 895 | |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 896 | priorOp = p->op==TK_ALL ? SRT_Table : SRT_Union; |
| 897 | if( eDest==priorOp ){ |
| 898 | /* We can reuse a temporary table generated by a SELECT to our |
| 899 | ** right. This also means we are not the right-most select and so |
| 900 | ** we cannot have an ORDER BY clause |
| 901 | */ |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 902 | unionTab = iParm; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 903 | assert( p->pOrderBy==0 ); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 904 | }else{ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 905 | /* We will need to create our own temporary table to hold the |
| 906 | ** intermediate results. |
| 907 | */ |
| 908 | unionTab = pParse->nTab++; |
| 909 | if( p->pOrderBy |
| 910 | && matchOrderbyToColumn(pParse, p, p->pOrderBy, unionTab, 1) ){ |
| 911 | return 1; |
| 912 | } |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 913 | if( p->op!=TK_ALL ){ |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 914 | sqliteVdbeAddOp(v, OP_OpenTemp, unionTab, 1); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 915 | sqliteVdbeAddOp(v, OP_KeyAsData, unionTab, 1); |
drh | 345fda3 | 2001-01-15 22:51:08 +0000 | [diff] [blame] | 916 | }else{ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 917 | sqliteVdbeAddOp(v, OP_OpenTemp, unionTab, 0); |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 918 | } |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 919 | } |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 920 | |
| 921 | /* Code the SELECT statements to our left |
| 922 | */ |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 923 | rc = sqliteSelect(pParse, pPrior, priorOp, unionTab, 0, 0, 0); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 924 | if( rc ) return rc; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 925 | |
| 926 | /* Code the current SELECT statement |
| 927 | */ |
| 928 | switch( p->op ){ |
| 929 | case TK_EXCEPT: op = SRT_Except; break; |
| 930 | case TK_UNION: op = SRT_Union; break; |
| 931 | case TK_ALL: op = SRT_Table; break; |
| 932 | } |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 933 | p->pPrior = 0; |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 934 | rc = sqliteSelect(pParse, p, op, unionTab, 0, 0, 0); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 935 | p->pPrior = pPrior; |
| 936 | if( rc ) return rc; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 937 | |
| 938 | /* Convert the data in the temporary table into whatever form |
| 939 | ** it is that we currently need. |
| 940 | */ |
| 941 | if( eDest!=priorOp ){ |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 942 | int iCont, iBreak, iStart; |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 943 | assert( p->pEList ); |
drh | 41202cc | 2002-04-23 17:10:18 +0000 | [diff] [blame] | 944 | if( eDest==SRT_Callback ){ |
| 945 | generateColumnNames(pParse, p->base, 0, p->pEList); |
| 946 | } |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 947 | iBreak = sqliteVdbeMakeLabel(v); |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 948 | iCont = sqliteVdbeMakeLabel(v); |
| 949 | sqliteVdbeAddOp(v, OP_Rewind, unionTab, iBreak); |
| 950 | iStart = sqliteVdbeCurrentAddr(v); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 951 | rc = selectInnerLoop(pParse, 0, unionTab, p->pEList->nExpr, |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 952 | p->pOrderBy, -1, eDest, iParm, |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 953 | iCont, iBreak); |
| 954 | if( rc ) return 1; |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 955 | sqliteVdbeResolveLabel(v, iCont); |
| 956 | sqliteVdbeAddOp(v, OP_Next, unionTab, iStart); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 957 | sqliteVdbeResolveLabel(v, iBreak); |
| 958 | sqliteVdbeAddOp(v, OP_Close, unionTab, 0); |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 959 | if( p->pOrderBy ){ |
| 960 | generateSortTail(v, p->pEList->nExpr); |
| 961 | } |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 962 | } |
| 963 | break; |
| 964 | } |
| 965 | case TK_INTERSECT: { |
| 966 | int tab1, tab2; |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 967 | int iCont, iBreak, iStart; |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 968 | |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 969 | /* INTERSECT is different from the others since it requires |
drh | 6206d50 | 2000-06-19 19:09:08 +0000 | [diff] [blame] | 970 | ** two temporary tables. Hence it has its own case. Begin |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 971 | ** by allocating the tables we will need. |
| 972 | */ |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 973 | tab1 = pParse->nTab++; |
| 974 | tab2 = pParse->nTab++; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 975 | if( p->pOrderBy && matchOrderbyToColumn(pParse,p,p->pOrderBy,tab1,1) ){ |
| 976 | return 1; |
| 977 | } |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 978 | sqliteVdbeAddOp(v, OP_OpenTemp, tab1, 1); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 979 | sqliteVdbeAddOp(v, OP_KeyAsData, tab1, 1); |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 980 | |
| 981 | /* Code the SELECTs to our left into temporary table "tab1". |
| 982 | */ |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 983 | rc = sqliteSelect(pParse, pPrior, SRT_Union, tab1, 0, 0, 0); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 984 | if( rc ) return rc; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 985 | |
| 986 | /* Code the current SELECT into temporary table "tab2" |
| 987 | */ |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 988 | sqliteVdbeAddOp(v, OP_OpenTemp, tab2, 1); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 989 | sqliteVdbeAddOp(v, OP_KeyAsData, tab2, 1); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 990 | p->pPrior = 0; |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 991 | rc = sqliteSelect(pParse, p, SRT_Union, tab2, 0, 0, 0); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 992 | p->pPrior = pPrior; |
| 993 | if( rc ) return rc; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 994 | |
| 995 | /* Generate code to take the intersection of the two temporary |
| 996 | ** tables. |
| 997 | */ |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 998 | assert( p->pEList ); |
drh | 41202cc | 2002-04-23 17:10:18 +0000 | [diff] [blame] | 999 | if( eDest==SRT_Callback ){ |
| 1000 | generateColumnNames(pParse, p->base, 0, p->pEList); |
| 1001 | } |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 1002 | iBreak = sqliteVdbeMakeLabel(v); |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 1003 | iCont = sqliteVdbeMakeLabel(v); |
| 1004 | sqliteVdbeAddOp(v, OP_Rewind, tab1, iBreak); |
| 1005 | iStart = sqliteVdbeAddOp(v, OP_FullKey, tab1, 0); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1006 | sqliteVdbeAddOp(v, OP_NotFound, tab2, iCont); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 1007 | rc = selectInnerLoop(pParse, 0, tab1, p->pEList->nExpr, |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1008 | p->pOrderBy, -1, eDest, iParm, |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 1009 | iCont, iBreak); |
| 1010 | if( rc ) return 1; |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 1011 | sqliteVdbeResolveLabel(v, iCont); |
| 1012 | sqliteVdbeAddOp(v, OP_Next, tab1, iStart); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1013 | sqliteVdbeResolveLabel(v, iBreak); |
| 1014 | sqliteVdbeAddOp(v, OP_Close, tab2, 0); |
| 1015 | sqliteVdbeAddOp(v, OP_Close, tab1, 0); |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1016 | if( p->pOrderBy ){ |
| 1017 | generateSortTail(v, p->pEList->nExpr); |
| 1018 | } |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 1019 | break; |
| 1020 | } |
| 1021 | } |
| 1022 | assert( p->pEList && pPrior->pEList ); |
| 1023 | if( p->pEList->nExpr!=pPrior->pEList->nExpr ){ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1024 | sqliteSetString(&pParse->zErrMsg, "SELECTs to the left and right of ", |
| 1025 | selectOpName(p->op), " do not have the same number of result columns", 0); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 1026 | pParse->nErr++; |
| 1027 | return 1; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1028 | } |
drh | 10e5e3c | 2000-06-08 00:19:02 +0000 | [diff] [blame] | 1029 | pParse->nTab = base; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1030 | return 0; |
| 1031 | } |
| 1032 | |
| 1033 | /* |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1034 | ** Recursively scan through an expression tree. For every reference |
| 1035 | ** to a column in table number iFrom, change that reference to the |
| 1036 | ** same column in table number iTo. |
| 1037 | */ |
| 1038 | static void changeTables(Expr *pExpr, int iFrom, int iTo){ |
| 1039 | if( pExpr==0 ) return; |
| 1040 | if( pExpr->op==TK_COLUMN && pExpr->iTable==iFrom ){ |
| 1041 | pExpr->iTable = iTo; |
| 1042 | }else{ |
drh | 1b2e032 | 2002-03-03 02:49:51 +0000 | [diff] [blame] | 1043 | static void changeTablesInList(ExprList*, int, int); |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1044 | changeTables(pExpr->pLeft, iFrom, iTo); |
| 1045 | changeTables(pExpr->pRight, iFrom, iTo); |
drh | 1b2e032 | 2002-03-03 02:49:51 +0000 | [diff] [blame] | 1046 | changeTablesInList(pExpr->pList, iFrom, iTo); |
| 1047 | } |
| 1048 | } |
| 1049 | static void changeTablesInList(ExprList *pList, int iFrom, int iTo){ |
| 1050 | if( pList ){ |
| 1051 | int i; |
| 1052 | for(i=0; i<pList->nExpr; i++){ |
| 1053 | changeTables(pList->a[i].pExpr, iFrom, iTo); |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1054 | } |
| 1055 | } |
| 1056 | } |
| 1057 | |
| 1058 | /* |
| 1059 | ** Scan through the expression pExpr. Replace every reference to |
| 1060 | ** a column in table number iTable with a copy of the corresponding |
drh | 84e5920 | 2002-03-14 14:33:31 +0000 | [diff] [blame] | 1061 | ** entry in pEList. (But leave references to the ROWID column |
| 1062 | ** unchanged.) When making a copy of an expression in pEList, change |
| 1063 | ** references to columns in table iSub into references to table iTable. |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1064 | ** |
| 1065 | ** This routine is part of the flattening procedure. A subquery |
| 1066 | ** whose result set is defined by pEList appears as entry in the |
| 1067 | ** FROM clause of a SELECT such that the VDBE cursor assigned to that |
| 1068 | ** FORM clause entry is iTable. This routine make the necessary |
| 1069 | ** changes to pExpr so that it refers directly to the source table |
| 1070 | ** of the subquery rather the result set of the subquery. |
| 1071 | */ |
| 1072 | static void substExpr(Expr *pExpr, int iTable, ExprList *pEList, int iSub){ |
| 1073 | if( pExpr==0 ) return; |
drh | 84e5920 | 2002-03-14 14:33:31 +0000 | [diff] [blame] | 1074 | if( pExpr->op==TK_COLUMN && pExpr->iTable==iTable && pExpr->iColumn>=0 ){ |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1075 | Expr *pNew; |
drh | 84e5920 | 2002-03-14 14:33:31 +0000 | [diff] [blame] | 1076 | assert( pEList!=0 && pExpr->iColumn<pEList->nExpr ); |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1077 | assert( pExpr->pLeft==0 && pExpr->pRight==0 && pExpr->pList==0 ); |
| 1078 | pNew = pEList->a[pExpr->iColumn].pExpr; |
| 1079 | assert( pNew!=0 ); |
| 1080 | pExpr->op = pNew->op; |
| 1081 | pExpr->pLeft = sqliteExprDup(pNew->pLeft); |
| 1082 | pExpr->pRight = sqliteExprDup(pNew->pRight); |
| 1083 | pExpr->pList = sqliteExprListDup(pNew->pList); |
| 1084 | pExpr->iTable = pNew->iTable; |
| 1085 | pExpr->iColumn = pNew->iColumn; |
| 1086 | pExpr->iAgg = pNew->iAgg; |
drh | 1b2e032 | 2002-03-03 02:49:51 +0000 | [diff] [blame] | 1087 | pExpr->token = pNew->token; |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1088 | if( iSub!=iTable ){ |
| 1089 | changeTables(pExpr, iSub, iTable); |
| 1090 | } |
| 1091 | }else{ |
| 1092 | static void substExprList(ExprList*,int,ExprList*,int); |
| 1093 | substExpr(pExpr->pLeft, iTable, pEList, iSub); |
| 1094 | substExpr(pExpr->pRight, iTable, pEList, iSub); |
| 1095 | substExprList(pExpr->pList, iTable, pEList, iSub); |
| 1096 | } |
| 1097 | } |
| 1098 | static void |
| 1099 | substExprList(ExprList *pList, int iTable, ExprList *pEList, int iSub){ |
| 1100 | int i; |
| 1101 | if( pList==0 ) return; |
| 1102 | for(i=0; i<pList->nExpr; i++){ |
| 1103 | substExpr(pList->a[i].pExpr, iTable, pEList, iSub); |
| 1104 | } |
| 1105 | } |
| 1106 | |
| 1107 | /* |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 1108 | ** This routine attempts to flatten subqueries in order to speed |
| 1109 | ** execution. It returns 1 if it makes changes and 0 if no flattening |
| 1110 | ** occurs. |
| 1111 | ** |
| 1112 | ** To understand the concept of flattening, consider the following |
| 1113 | ** query: |
| 1114 | ** |
| 1115 | ** SELECT a FROM (SELECT x+y AS a FROM t1 WHERE z<100) WHERE a>5 |
| 1116 | ** |
| 1117 | ** The default way of implementing this query is to execute the |
| 1118 | ** subquery first and store the results in a temporary table, then |
| 1119 | ** run the outer query on that temporary table. This requires two |
| 1120 | ** passes over the data. Furthermore, because the temporary table |
| 1121 | ** has no indices, the WHERE clause on the outer query cannot be |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1122 | ** optimized. |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 1123 | ** |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1124 | ** This routine attempts to rewrite queries such as the above into |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 1125 | ** a single flat select, like this: |
| 1126 | ** |
| 1127 | ** SELECT x+y AS a FROM t1 WHERE z<100 AND a>5 |
| 1128 | ** |
| 1129 | ** The code generated for this simpification gives the same result |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1130 | ** but only has to scan the data once. And because indices might |
| 1131 | ** exist on the table t1, a complete scan of the data might be |
| 1132 | ** avoided. |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 1133 | ** |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1134 | ** Flattening is only attempted if all of the following are true: |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 1135 | ** |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1136 | ** (1) The subquery and the outer query do not both use aggregates. |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 1137 | ** |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1138 | ** (2) The subquery is not an aggregate or the outer query is not a join. |
| 1139 | ** |
| 1140 | ** (3) The subquery is not a join. |
| 1141 | ** |
| 1142 | ** (4) The subquery is not DISTINCT or the outer query is not a join. |
| 1143 | ** |
| 1144 | ** (5) The subquery is not DISTINCT or the outer query does not use |
| 1145 | ** aggregates. |
| 1146 | ** |
| 1147 | ** (6) The subquery does not use aggregates or the outer query is not |
| 1148 | ** DISTINCT. |
| 1149 | ** |
drh | 08192d5 | 2002-04-30 19:20:28 +0000 | [diff] [blame] | 1150 | ** (7) The subquery has a FROM clause. |
| 1151 | ** |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1152 | ** In this routine, the "p" parameter is a pointer to the outer query. |
| 1153 | ** The subquery is p->pSrc->a[iFrom]. isAgg is true if the outer query |
| 1154 | ** uses aggregates and subqueryIsAgg is true if the subquery uses aggregates. |
| 1155 | ** |
| 1156 | ** If flattening is not attempted, this routine is a no-op and return 0. |
| 1157 | ** If flattening is attempted this routine returns 1. |
| 1158 | ** |
| 1159 | ** All of the expression analysis must occur on both the outer query and |
| 1160 | ** the subquery before this routine runs. |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 1161 | */ |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1162 | int flattenSubquery(Select *p, int iFrom, int isAgg, int subqueryIsAgg){ |
drh | 0bb2810 | 2002-05-08 11:54:14 +0000 | [diff] [blame] | 1163 | Select *pSub; /* The inner query or "subquery" */ |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 1164 | SrcList *pSrc; /* The FROM clause of the outer query */ |
| 1165 | SrcList *pSubSrc; /* The FROM clause of the subquery */ |
drh | 0bb2810 | 2002-05-08 11:54:14 +0000 | [diff] [blame] | 1166 | ExprList *pList; /* The result set of the outer query */ |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1167 | int i; |
| 1168 | int iParent, iSub; |
| 1169 | Expr *pWhere; |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 1170 | |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1171 | /* Check to see if flattening is permitted. Return 0 if not. |
| 1172 | */ |
| 1173 | if( p==0 ) return 0; |
| 1174 | pSrc = p->pSrc; |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 1175 | assert( pSrc && iFrom>=0 && iFrom<pSrc->nSrc ); |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1176 | pSub = pSrc->a[iFrom].pSelect; |
| 1177 | assert( pSub!=0 ); |
| 1178 | if( isAgg && subqueryIsAgg ) return 0; |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 1179 | if( subqueryIsAgg && pSrc->nSrc>1 ) return 0; |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1180 | pSubSrc = pSub->pSrc; |
| 1181 | assert( pSubSrc ); |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 1182 | if( pSubSrc->nSrc!=1 ) return 0; |
| 1183 | if( pSub->isDistinct && pSrc->nSrc>1 ) return 0; |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1184 | if( pSub->isDistinct && isAgg ) return 0; |
| 1185 | if( p->isDistinct && subqueryIsAgg ) return 0; |
| 1186 | |
drh | 0bb2810 | 2002-05-08 11:54:14 +0000 | [diff] [blame] | 1187 | /* If we reach this point, it means flattening is permitted for the |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1188 | ** i-th entry of the FROM clause in the outer query. |
| 1189 | */ |
| 1190 | iParent = p->base + iFrom; |
| 1191 | iSub = pSub->base; |
| 1192 | substExprList(p->pEList, iParent, pSub->pEList, iSub); |
| 1193 | pList = p->pEList; |
| 1194 | for(i=0; i<pList->nExpr; i++){ |
| 1195 | if( pList->a[i].zName==0 ){ |
| 1196 | Expr *pExpr = pList->a[i].pExpr; |
| 1197 | pList->a[i].zName = sqliteStrNDup(pExpr->span.z, pExpr->span.n); |
| 1198 | } |
| 1199 | } |
drh | 1b2e032 | 2002-03-03 02:49:51 +0000 | [diff] [blame] | 1200 | if( isAgg ){ |
| 1201 | substExprList(p->pGroupBy, iParent, pSub->pEList, iSub); |
| 1202 | substExpr(p->pHaving, iParent, pSub->pEList, iSub); |
| 1203 | } |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1204 | substExprList(p->pOrderBy, iParent, pSub->pEList, iSub); |
| 1205 | if( pSub->pWhere ){ |
| 1206 | pWhere = sqliteExprDup(pSub->pWhere); |
| 1207 | if( iParent!=iSub ){ |
| 1208 | changeTables(pWhere, iSub, iParent); |
| 1209 | } |
| 1210 | }else{ |
| 1211 | pWhere = 0; |
| 1212 | } |
| 1213 | if( subqueryIsAgg ){ |
| 1214 | assert( p->pHaving==0 ); |
drh | 1b2e032 | 2002-03-03 02:49:51 +0000 | [diff] [blame] | 1215 | p->pHaving = p->pWhere; |
| 1216 | p->pWhere = pWhere; |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1217 | substExpr(p->pHaving, iParent, pSub->pEList, iSub); |
drh | 1b2e032 | 2002-03-03 02:49:51 +0000 | [diff] [blame] | 1218 | if( pSub->pHaving ){ |
| 1219 | Expr *pHaving = sqliteExprDup(pSub->pHaving); |
| 1220 | if( iParent!=iSub ){ |
| 1221 | changeTables(pHaving, iSub, iParent); |
| 1222 | } |
| 1223 | if( p->pHaving ){ |
| 1224 | p->pHaving = sqliteExpr(TK_AND, p->pHaving, pHaving, 0); |
| 1225 | }else{ |
| 1226 | p->pHaving = pHaving; |
| 1227 | } |
| 1228 | } |
| 1229 | assert( p->pGroupBy==0 ); |
| 1230 | p->pGroupBy = sqliteExprListDup(pSub->pGroupBy); |
| 1231 | if( iParent!=iSub ){ |
| 1232 | changeTablesInList(p->pGroupBy, iSub, iParent); |
| 1233 | } |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1234 | }else if( p->pWhere==0 ){ |
| 1235 | p->pWhere = pWhere; |
| 1236 | }else{ |
| 1237 | substExpr(p->pWhere, iParent, pSub->pEList, iSub); |
| 1238 | if( pWhere ){ |
| 1239 | p->pWhere = sqliteExpr(TK_AND, p->pWhere, pWhere, 0); |
| 1240 | } |
| 1241 | } |
| 1242 | p->isDistinct = p->isDistinct || pSub->isDistinct; |
| 1243 | if( pSrc->a[iFrom].pTab && pSrc->a[iFrom].pTab->isTransient ){ |
| 1244 | sqliteDeleteTable(0, pSrc->a[iFrom].pTab); |
| 1245 | } |
| 1246 | pSrc->a[iFrom].pTab = pSubSrc->a[0].pTab; |
| 1247 | pSubSrc->a[0].pTab = 0; |
| 1248 | pSrc->a[iFrom].pSelect = pSubSrc->a[0].pSelect; |
| 1249 | pSubSrc->a[0].pSelect = 0; |
| 1250 | sqliteSelectDelete(pSub); |
| 1251 | return 1; |
| 1252 | } |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 1253 | |
| 1254 | /* |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 1255 | ** Analyze the SELECT statement passed in as an argument to see if it |
| 1256 | ** is a simple min() or max() query. If it is and this query can be |
| 1257 | ** satisfied using a single seek to the beginning or end of an index, |
| 1258 | ** then generate the code for this SELECT return 1. If this is not a |
| 1259 | ** simple min() or max() query, then return 0; |
| 1260 | ** |
| 1261 | ** A simply min() or max() query looks like this: |
| 1262 | ** |
| 1263 | ** SELECT min(a) FROM table; |
| 1264 | ** SELECT max(a) FROM table; |
| 1265 | ** |
| 1266 | ** The query may have only a single table in its FROM argument. There |
| 1267 | ** can be no GROUP BY or HAVING or WHERE clauses. The result set must |
| 1268 | ** be the min() or max() of a single column of the table. The column |
| 1269 | ** in the min() or max() function must be indexed. |
| 1270 | ** |
| 1271 | ** The parameters to this routine are the same as for sqliteSelect(). |
| 1272 | ** See the header comment on that routine for additional information. |
| 1273 | */ |
| 1274 | static int simpleMinMaxQuery(Parse *pParse, Select *p, int eDest, int iParm){ |
| 1275 | Expr *pExpr; |
| 1276 | int iCol; |
| 1277 | Table *pTab; |
| 1278 | Index *pIdx; |
| 1279 | int base; |
| 1280 | Vdbe *v; |
| 1281 | int openOp; |
| 1282 | int seekOp; |
| 1283 | int cont; |
| 1284 | ExprList eList; |
| 1285 | struct ExprList_item eListItem; |
| 1286 | |
| 1287 | /* Check to see if this query is a simple min() or max() query. Return |
| 1288 | ** zero if it is not. |
| 1289 | */ |
| 1290 | if( p->pGroupBy || p->pHaving || p->pWhere ) return 0; |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 1291 | if( p->pSrc->nSrc!=1 ) return 0; |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 1292 | if( p->pEList->nExpr!=1 ) return 0; |
| 1293 | pExpr = p->pEList->a[0].pExpr; |
| 1294 | if( pExpr->op!=TK_AGG_FUNCTION ) return 0; |
| 1295 | if( pExpr->pList==0 || pExpr->pList->nExpr!=1 ) return 0; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1296 | if( pExpr->token.n!=3 ) return 0; |
| 1297 | if( sqliteStrNICmp(pExpr->token.z,"min",3)==0 ){ |
| 1298 | seekOp = OP_Rewind; |
| 1299 | }else if( sqliteStrNICmp(pExpr->token.z,"max",3)==0 ){ |
| 1300 | seekOp = OP_Last; |
| 1301 | }else{ |
| 1302 | return 0; |
| 1303 | } |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 1304 | pExpr = pExpr->pList->a[0].pExpr; |
| 1305 | if( pExpr->op!=TK_COLUMN ) return 0; |
| 1306 | iCol = pExpr->iColumn; |
| 1307 | pTab = p->pSrc->a[0].pTab; |
| 1308 | |
| 1309 | /* If we get to here, it means the query is of the correct form. |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 1310 | ** Check to make sure we have an index and make pIdx point to the |
| 1311 | ** appropriate index. If the min() or max() is on an INTEGER PRIMARY |
| 1312 | ** key column, no index is necessary so set pIdx to NULL. If no |
| 1313 | ** usable index is found, return 0. |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 1314 | */ |
| 1315 | if( iCol<0 ){ |
| 1316 | pIdx = 0; |
| 1317 | }else{ |
| 1318 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 1319 | assert( pIdx->nColumn>=1 ); |
| 1320 | if( pIdx->aiColumn[0]==iCol ) break; |
| 1321 | } |
| 1322 | if( pIdx==0 ) return 0; |
| 1323 | } |
| 1324 | |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 1325 | /* Identify column names if we will be using the callback. This |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 1326 | ** step is skipped if the output is going to a table or a memory cell. |
| 1327 | */ |
| 1328 | v = sqliteGetVdbe(pParse); |
| 1329 | if( v==0 ) return 0; |
| 1330 | if( eDest==SRT_Callback ){ |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1331 | generateColumnNames(pParse, p->base, p->pSrc, p->pEList); |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 1332 | } |
| 1333 | |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 1334 | /* Generating code to find the min or the max. Basically all we have |
| 1335 | ** to do is find the first or the last entry in the chosen index. If |
| 1336 | ** the min() or max() is on the INTEGER PRIMARY KEY, then find the first |
| 1337 | ** or last entry in the main table. |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 1338 | */ |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 1339 | if( !pParse->schemaVerified && (pParse->db->flags & SQLITE_InTrans)==0 ){ |
| 1340 | sqliteVdbeAddOp(v, OP_VerifyCookie, pParse->db->schema_cookie, 0); |
| 1341 | pParse->schemaVerified = 1; |
| 1342 | } |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 1343 | openOp = pTab->isTemp ? OP_OpenAux : OP_Open; |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1344 | base = p->base; |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 1345 | sqliteVdbeAddOp(v, openOp, base, pTab->tnum); |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 1346 | sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC); |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 1347 | if( pIdx==0 ){ |
| 1348 | sqliteVdbeAddOp(v, seekOp, base, 0); |
| 1349 | }else{ |
| 1350 | sqliteVdbeAddOp(v, openOp, base+1, pIdx->tnum); |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 1351 | sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC); |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 1352 | sqliteVdbeAddOp(v, seekOp, base+1, 0); |
| 1353 | sqliteVdbeAddOp(v, OP_IdxRecno, base+1, 0); |
| 1354 | sqliteVdbeAddOp(v, OP_Close, base+1, 0); |
| 1355 | sqliteVdbeAddOp(v, OP_MoveTo, base, 0); |
| 1356 | } |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 1357 | eList.nExpr = 1; |
| 1358 | memset(&eListItem, 0, sizeof(eListItem)); |
| 1359 | eList.a = &eListItem; |
| 1360 | eList.a[0].pExpr = pExpr; |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 1361 | cont = sqliteVdbeMakeLabel(v); |
| 1362 | selectInnerLoop(pParse, &eList, base, 1, 0, -1, eDest, iParm, cont, cont); |
| 1363 | sqliteVdbeResolveLabel(v, cont); |
| 1364 | sqliteVdbeAddOp(v, OP_Close, base, 0); |
| 1365 | return 1; |
| 1366 | } |
| 1367 | |
| 1368 | /* |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 1369 | ** Generate code for the given SELECT statement. |
| 1370 | ** |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1371 | ** The results are distributed in various ways depending on the |
| 1372 | ** value of eDest and iParm. |
| 1373 | ** |
| 1374 | ** eDest Value Result |
| 1375 | ** ------------ ------------------------------------------- |
| 1376 | ** SRT_Callback Invoke the callback for each row of the result. |
| 1377 | ** |
| 1378 | ** SRT_Mem Store first result in memory cell iParm |
| 1379 | ** |
| 1380 | ** SRT_Set Store results as keys of a table with cursor iParm |
| 1381 | ** |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 1382 | ** SRT_Union Store results as a key in a temporary table iParm |
| 1383 | ** |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1384 | ** SRT_Except Remove results form the temporary table iParm. |
| 1385 | ** |
| 1386 | ** SRT_Table Store results in temporary table iParm |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 1387 | ** |
| 1388 | ** This routine returns the number of errors. If any errors are |
| 1389 | ** encountered, then an appropriate error message is left in |
| 1390 | ** pParse->zErrMsg. |
| 1391 | ** |
| 1392 | ** This routine does NOT free the Select structure passed in. The |
| 1393 | ** calling function needs to do that. |
drh | 1b2e032 | 2002-03-03 02:49:51 +0000 | [diff] [blame] | 1394 | ** |
| 1395 | ** The pParent, parentTab, and *pParentAgg fields are filled in if this |
| 1396 | ** SELECT is a subquery. This routine may try to combine this SELECT |
| 1397 | ** with its parent to form a single flat query. In so doing, it might |
| 1398 | ** change the parent query from a non-aggregate to an aggregate query. |
| 1399 | ** For that reason, the pParentAgg flag is passed as a pointer, so it |
| 1400 | ** can be changed. |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 1401 | */ |
| 1402 | int sqliteSelect( |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1403 | Parse *pParse, /* The parser context */ |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 1404 | Select *p, /* The SELECT statement being coded. */ |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 1405 | int eDest, /* One of: SRT_Callback Mem Set Union Except */ |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1406 | int iParm, /* Save result in this memory location, if >=0 */ |
| 1407 | Select *pParent, /* Another SELECT for which this is a sub-query */ |
| 1408 | int parentTab, /* Index in pParent->pSrc of this query */ |
drh | 1b2e032 | 2002-03-03 02:49:51 +0000 | [diff] [blame] | 1409 | int *pParentAgg /* True if pParent uses aggregate functions */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1410 | ){ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1411 | int i; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1412 | WhereInfo *pWInfo; |
| 1413 | Vdbe *v; |
| 1414 | int isAgg = 0; /* True for select lists like "count(*)" */ |
drh | a2e0004 | 2002-01-22 03:13:42 +0000 | [diff] [blame] | 1415 | ExprList *pEList; /* List of columns to extract. */ |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 1416 | SrcList *pTabList; /* List of tables to select from */ |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 1417 | Expr *pWhere; /* The WHERE clause. May be NULL */ |
| 1418 | ExprList *pOrderBy; /* The ORDER BY clause. May be NULL */ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1419 | ExprList *pGroupBy; /* The GROUP BY clause. May be NULL */ |
| 1420 | Expr *pHaving; /* The HAVING clause. May be NULL */ |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 1421 | int isDistinct; /* True if the DISTINCT keyword is present */ |
| 1422 | int distinct; /* Table to use for the distinct set */ |
drh | 10e5e3c | 2000-06-08 00:19:02 +0000 | [diff] [blame] | 1423 | int base; /* First cursor available for use */ |
drh | 1d83f05 | 2002-02-17 00:30:36 +0000 | [diff] [blame] | 1424 | int rc = 1; /* Value to return from this function */ |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 1425 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1426 | if( sqlite_malloc_failed || pParse->nErr || p==0 ) return 1; |
| 1427 | |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 1428 | /* If there is are a sequence of queries, do the earlier ones first. |
| 1429 | */ |
| 1430 | if( p->pPrior ){ |
| 1431 | return multiSelect(pParse, p, eDest, iParm); |
| 1432 | } |
| 1433 | |
| 1434 | /* Make local copies of the parameters for this query. |
| 1435 | */ |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 1436 | pTabList = p->pSrc; |
| 1437 | pWhere = p->pWhere; |
| 1438 | pOrderBy = p->pOrderBy; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1439 | pGroupBy = p->pGroupBy; |
| 1440 | pHaving = p->pHaving; |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 1441 | isDistinct = p->isDistinct; |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 1442 | |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1443 | /* Allocate a block of VDBE cursors, one for each table in the FROM clause. |
| 1444 | ** The WHERE processing requires that the cursors for the tables in the |
| 1445 | ** FROM clause be consecutive. |
drh | 10e5e3c | 2000-06-08 00:19:02 +0000 | [diff] [blame] | 1446 | */ |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1447 | base = p->base = pParse->nTab; |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 1448 | pParse->nTab += pTabList->nSrc; |
drh | 10e5e3c | 2000-06-08 00:19:02 +0000 | [diff] [blame] | 1449 | |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 1450 | /* |
| 1451 | ** Do not even attempt to generate any code if we have already seen |
| 1452 | ** errors before this routine starts. |
| 1453 | */ |
drh | 1d83f05 | 2002-02-17 00:30:36 +0000 | [diff] [blame] | 1454 | if( pParse->nErr>0 ) goto select_end; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1455 | |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1456 | /* Look up every table in the table list and create an appropriate |
| 1457 | ** columnlist in pEList if there isn't one already. (The parser leaves |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 1458 | ** a NULL in the p->pEList if the SQL said "SELECT * FROM ...") |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1459 | */ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1460 | if( fillInColumnList(pParse, p) ){ |
drh | 1d83f05 | 2002-02-17 00:30:36 +0000 | [diff] [blame] | 1461 | goto select_end; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1462 | } |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 1463 | pWhere = p->pWhere; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1464 | pEList = p->pEList; |
drh | 1d83f05 | 2002-02-17 00:30:36 +0000 | [diff] [blame] | 1465 | if( pEList==0 ) goto select_end; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1466 | |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1467 | /* If writing to memory or generating a set |
| 1468 | ** only a single column may be output. |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 1469 | */ |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1470 | if( (eDest==SRT_Mem || eDest==SRT_Set) && pEList->nExpr>1 ){ |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 1471 | sqliteSetString(&pParse->zErrMsg, "only a single result allowed for " |
| 1472 | "a SELECT that is part of an expression", 0); |
| 1473 | pParse->nErr++; |
drh | 1d83f05 | 2002-02-17 00:30:36 +0000 | [diff] [blame] | 1474 | goto select_end; |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 1475 | } |
| 1476 | |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1477 | /* ORDER BY is ignored if we are not sending the result to a callback. |
| 1478 | */ |
| 1479 | if( eDest!=SRT_Callback ){ |
drh | acd4c69 | 2002-03-07 02:02:51 +0000 | [diff] [blame] | 1480 | pOrderBy = 0; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1481 | } |
| 1482 | |
drh | 10e5e3c | 2000-06-08 00:19:02 +0000 | [diff] [blame] | 1483 | /* At this point, we should have allocated all the cursors that we |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1484 | ** need to handle subquerys and temporary tables. |
drh | 10e5e3c | 2000-06-08 00:19:02 +0000 | [diff] [blame] | 1485 | ** |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 1486 | ** Resolve the column names and do a semantics check on all the expressions. |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1487 | */ |
drh | 4794b98 | 2000-06-06 13:54:14 +0000 | [diff] [blame] | 1488 | for(i=0; i<pEList->nExpr; i++){ |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1489 | if( sqliteExprResolveIds(pParse, base, pTabList, 0, pEList->a[i].pExpr) ){ |
drh | 1d83f05 | 2002-02-17 00:30:36 +0000 | [diff] [blame] | 1490 | goto select_end; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1491 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1492 | if( sqliteExprCheck(pParse, pEList->a[i].pExpr, 1, &isAgg) ){ |
drh | 1d83f05 | 2002-02-17 00:30:36 +0000 | [diff] [blame] | 1493 | goto select_end; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1494 | } |
| 1495 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1496 | if( pWhere ){ |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1497 | if( sqliteExprResolveIds(pParse, base, pTabList, pEList, pWhere) ){ |
drh | 1d83f05 | 2002-02-17 00:30:36 +0000 | [diff] [blame] | 1498 | goto select_end; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1499 | } |
| 1500 | if( sqliteExprCheck(pParse, pWhere, 0, 0) ){ |
drh | 1d83f05 | 2002-02-17 00:30:36 +0000 | [diff] [blame] | 1501 | goto select_end; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1502 | } |
| 1503 | } |
| 1504 | if( pOrderBy ){ |
| 1505 | for(i=0; i<pOrderBy->nExpr; i++){ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1506 | Expr *pE = pOrderBy->a[i].pExpr; |
drh | 9208643 | 2002-01-22 14:11:29 +0000 | [diff] [blame] | 1507 | if( sqliteExprIsConstant(pE) ){ |
| 1508 | sqliteSetString(&pParse->zErrMsg, |
| 1509 | "ORDER BY expressions should not be constant", 0); |
| 1510 | pParse->nErr++; |
drh | 1d83f05 | 2002-02-17 00:30:36 +0000 | [diff] [blame] | 1511 | goto select_end; |
drh | 9208643 | 2002-01-22 14:11:29 +0000 | [diff] [blame] | 1512 | } |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1513 | if( sqliteExprResolveIds(pParse, base, pTabList, pEList, pE) ){ |
drh | 1d83f05 | 2002-02-17 00:30:36 +0000 | [diff] [blame] | 1514 | goto select_end; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1515 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1516 | if( sqliteExprCheck(pParse, pE, isAgg, 0) ){ |
drh | 1d83f05 | 2002-02-17 00:30:36 +0000 | [diff] [blame] | 1517 | goto select_end; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1518 | } |
| 1519 | } |
| 1520 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1521 | if( pGroupBy ){ |
| 1522 | for(i=0; i<pGroupBy->nExpr; i++){ |
| 1523 | Expr *pE = pGroupBy->a[i].pExpr; |
drh | 9208643 | 2002-01-22 14:11:29 +0000 | [diff] [blame] | 1524 | if( sqliteExprIsConstant(pE) ){ |
| 1525 | sqliteSetString(&pParse->zErrMsg, |
| 1526 | "GROUP BY expressions should not be constant", 0); |
| 1527 | pParse->nErr++; |
drh | 1d83f05 | 2002-02-17 00:30:36 +0000 | [diff] [blame] | 1528 | goto select_end; |
drh | 9208643 | 2002-01-22 14:11:29 +0000 | [diff] [blame] | 1529 | } |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1530 | if( sqliteExprResolveIds(pParse, base, pTabList, pEList, pE) ){ |
drh | 1d83f05 | 2002-02-17 00:30:36 +0000 | [diff] [blame] | 1531 | goto select_end; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1532 | } |
| 1533 | if( sqliteExprCheck(pParse, pE, isAgg, 0) ){ |
drh | 1d83f05 | 2002-02-17 00:30:36 +0000 | [diff] [blame] | 1534 | goto select_end; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1535 | } |
| 1536 | } |
| 1537 | } |
| 1538 | if( pHaving ){ |
| 1539 | if( pGroupBy==0 ){ |
drh | da93281 | 2000-06-06 18:00:15 +0000 | [diff] [blame] | 1540 | sqliteSetString(&pParse->zErrMsg, "a GROUP BY clause is required " |
| 1541 | "before HAVING", 0); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1542 | pParse->nErr++; |
drh | 1d83f05 | 2002-02-17 00:30:36 +0000 | [diff] [blame] | 1543 | goto select_end; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1544 | } |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1545 | if( sqliteExprResolveIds(pParse, base, pTabList, pEList, pHaving) ){ |
drh | 1d83f05 | 2002-02-17 00:30:36 +0000 | [diff] [blame] | 1546 | goto select_end; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1547 | } |
drh | da93281 | 2000-06-06 18:00:15 +0000 | [diff] [blame] | 1548 | if( sqliteExprCheck(pParse, pHaving, isAgg, 0) ){ |
drh | 1d83f05 | 2002-02-17 00:30:36 +0000 | [diff] [blame] | 1549 | goto select_end; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1550 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1551 | } |
| 1552 | |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 1553 | /* Check for the special case of a min() or max() function by itself |
| 1554 | ** in the result set. |
| 1555 | */ |
| 1556 | if( simpleMinMaxQuery(pParse, p, eDest, iParm) ){ |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 1557 | rc = 0; |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 1558 | goto select_end; |
| 1559 | } |
| 1560 | |
drh | d820cb1 | 2002-02-18 03:21:45 +0000 | [diff] [blame] | 1561 | /* Begin generating code. |
| 1562 | */ |
| 1563 | v = sqliteGetVdbe(pParse); |
| 1564 | if( v==0 ) goto select_end; |
| 1565 | |
drh | 0bb2810 | 2002-05-08 11:54:14 +0000 | [diff] [blame] | 1566 | /* Identify column names if we will be using in the callback. This |
| 1567 | ** step is skipped if the output is going to a table or a memory cell. |
| 1568 | */ |
| 1569 | if( eDest==SRT_Callback ){ |
| 1570 | generateColumnNames(pParse, p->base, pTabList, pEList); |
| 1571 | } |
| 1572 | |
| 1573 | /* Set the limiter |
| 1574 | */ |
| 1575 | if( p->nLimit<=0 ){ |
| 1576 | p->nOffset = 0; |
| 1577 | }else{ |
| 1578 | if( p->nOffset<0 ) p->nOffset = 0; |
| 1579 | sqliteVdbeAddOp(v, OP_Limit, p->nLimit, p->nOffset); |
| 1580 | } |
| 1581 | |
drh | d820cb1 | 2002-02-18 03:21:45 +0000 | [diff] [blame] | 1582 | /* Generate code for all sub-queries in the FROM clause |
| 1583 | */ |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 1584 | for(i=0; i<pTabList->nSrc; i++){ |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 1585 | if( pTabList->a[i].pSelect==0 ) continue; |
drh | 2d0794e | 2002-03-03 03:03:52 +0000 | [diff] [blame] | 1586 | sqliteSelect(pParse, pTabList->a[i].pSelect, SRT_TempTable, base+i, |
drh | 1b2e032 | 2002-03-03 02:49:51 +0000 | [diff] [blame] | 1587 | p, i, &isAgg); |
| 1588 | pTabList = p->pSrc; |
| 1589 | pWhere = p->pWhere; |
drh | acd4c69 | 2002-03-07 02:02:51 +0000 | [diff] [blame] | 1590 | if( eDest==SRT_Callback ){ |
| 1591 | pOrderBy = p->pOrderBy; |
| 1592 | } |
drh | 1b2e032 | 2002-03-03 02:49:51 +0000 | [diff] [blame] | 1593 | pGroupBy = p->pGroupBy; |
| 1594 | pHaving = p->pHaving; |
| 1595 | isDistinct = p->isDistinct; |
drh | d820cb1 | 2002-02-18 03:21:45 +0000 | [diff] [blame] | 1596 | } |
| 1597 | |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1598 | /* Check to see if this is a subquery that can be "flattened" into its parent. |
| 1599 | ** If flattening is a possiblity, do so and return immediately. |
| 1600 | */ |
drh | 1b2e032 | 2002-03-03 02:49:51 +0000 | [diff] [blame] | 1601 | if( pParent && pParentAgg && |
| 1602 | flattenSubquery(pParent, parentTab, *pParentAgg, isAgg) ){ |
| 1603 | if( isAgg ) *pParentAgg = 1; |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1604 | return rc; |
| 1605 | } |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1606 | |
drh | 2d0794e | 2002-03-03 03:03:52 +0000 | [diff] [blame] | 1607 | /* If the output is destined for a temporary table, open that table. |
| 1608 | */ |
| 1609 | if( eDest==SRT_TempTable ){ |
| 1610 | sqliteVdbeAddOp(v, OP_OpenTemp, iParm, 0); |
| 1611 | } |
| 1612 | |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1613 | /* Do an analysis of aggregate expressions. |
drh | efb7251 | 2000-05-31 20:00:52 +0000 | [diff] [blame] | 1614 | */ |
drh | d820cb1 | 2002-02-18 03:21:45 +0000 | [diff] [blame] | 1615 | sqliteAggregateInfoReset(pParse); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1616 | if( isAgg ){ |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1617 | assert( pParse->nAgg==0 ); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1618 | for(i=0; i<pEList->nExpr; i++){ |
| 1619 | if( sqliteExprAnalyzeAggregates(pParse, pEList->a[i].pExpr) ){ |
drh | 1d83f05 | 2002-02-17 00:30:36 +0000 | [diff] [blame] | 1620 | goto select_end; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1621 | } |
| 1622 | } |
| 1623 | if( pGroupBy ){ |
| 1624 | for(i=0; i<pGroupBy->nExpr; i++){ |
| 1625 | if( sqliteExprAnalyzeAggregates(pParse, pGroupBy->a[i].pExpr) ){ |
drh | 1d83f05 | 2002-02-17 00:30:36 +0000 | [diff] [blame] | 1626 | goto select_end; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1627 | } |
| 1628 | } |
| 1629 | } |
| 1630 | if( pHaving && sqliteExprAnalyzeAggregates(pParse, pHaving) ){ |
drh | 1d83f05 | 2002-02-17 00:30:36 +0000 | [diff] [blame] | 1631 | goto select_end; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1632 | } |
drh | 191b690 | 2000-06-08 11:13:01 +0000 | [diff] [blame] | 1633 | if( pOrderBy ){ |
| 1634 | for(i=0; i<pOrderBy->nExpr; i++){ |
| 1635 | if( sqliteExprAnalyzeAggregates(pParse, pOrderBy->a[i].pExpr) ){ |
drh | 1d83f05 | 2002-02-17 00:30:36 +0000 | [diff] [blame] | 1636 | goto select_end; |
drh | 191b690 | 2000-06-08 11:13:01 +0000 | [diff] [blame] | 1637 | } |
| 1638 | } |
| 1639 | } |
drh | efb7251 | 2000-05-31 20:00:52 +0000 | [diff] [blame] | 1640 | } |
| 1641 | |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1642 | /* Reset the aggregator |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1643 | */ |
| 1644 | if( isAgg ){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1645 | sqliteVdbeAddOp(v, OP_AggReset, 0, pParse->nAgg); |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 1646 | for(i=0; i<pParse->nAgg; i++){ |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1647 | FuncDef *pFunc; |
| 1648 | if( (pFunc = pParse->aAgg[i].pFunc)!=0 && pFunc->xFinalize!=0 ){ |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 1649 | sqliteVdbeAddOp(v, OP_AggInit, 0, i); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1650 | sqliteVdbeChangeP3(v, -1, (char*)pFunc, P3_POINTER); |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 1651 | } |
| 1652 | } |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 1653 | if( pGroupBy==0 ){ |
| 1654 | sqliteVdbeAddOp(v, OP_String, 0, 0); |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 1655 | sqliteVdbeAddOp(v, OP_AggFocus, 0, 0); |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 1656 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1657 | } |
| 1658 | |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 1659 | /* Initialize the memory cell to NULL |
| 1660 | */ |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1661 | if( eDest==SRT_Mem ){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1662 | sqliteVdbeAddOp(v, OP_String, 0, 0); |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 1663 | sqliteVdbeAddOp(v, OP_MemStore, iParm, 1); |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 1664 | } |
| 1665 | |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1666 | /* Open a temporary table to use for the distinct set. |
drh | efb7251 | 2000-05-31 20:00:52 +0000 | [diff] [blame] | 1667 | */ |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 1668 | if( isDistinct ){ |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1669 | distinct = pParse->nTab++; |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 1670 | sqliteVdbeAddOp(v, OP_OpenTemp, distinct, 1); |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1671 | }else{ |
| 1672 | distinct = -1; |
drh | efb7251 | 2000-05-31 20:00:52 +0000 | [diff] [blame] | 1673 | } |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1674 | |
| 1675 | /* Begin the database scan |
| 1676 | */ |
| 1677 | pWInfo = sqliteWhereBegin(pParse, p->base, pTabList, pWhere, 0); |
drh | 1d83f05 | 2002-02-17 00:30:36 +0000 | [diff] [blame] | 1678 | if( pWInfo==0 ) goto select_end; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1679 | |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1680 | /* Use the standard inner loop if we are not dealing with |
| 1681 | ** aggregates |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1682 | */ |
drh | da9d6c4 | 2000-05-31 18:20:14 +0000 | [diff] [blame] | 1683 | if( !isAgg ){ |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 1684 | if( selectInnerLoop(pParse, pEList, 0, 0, pOrderBy, distinct, eDest, iParm, |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1685 | pWInfo->iContinue, pWInfo->iBreak) ){ |
drh | 1d83f05 | 2002-02-17 00:30:36 +0000 | [diff] [blame] | 1686 | goto select_end; |
drh | da9d6c4 | 2000-05-31 18:20:14 +0000 | [diff] [blame] | 1687 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1688 | } |
drh | efb7251 | 2000-05-31 20:00:52 +0000 | [diff] [blame] | 1689 | |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1690 | /* If we are dealing with aggregates, then to the special aggregate |
| 1691 | ** processing. |
drh | efb7251 | 2000-05-31 20:00:52 +0000 | [diff] [blame] | 1692 | */ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1693 | else{ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1694 | if( pGroupBy ){ |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 1695 | int lbl1; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1696 | for(i=0; i<pGroupBy->nExpr; i++){ |
| 1697 | sqliteExprCode(pParse, pGroupBy->a[i].pExpr); |
| 1698 | } |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1699 | sqliteVdbeAddOp(v, OP_MakeKey, pGroupBy->nExpr, 0); |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 1700 | lbl1 = sqliteVdbeMakeLabel(v); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1701 | sqliteVdbeAddOp(v, OP_AggFocus, 0, lbl1); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1702 | for(i=0; i<pParse->nAgg; i++){ |
| 1703 | if( pParse->aAgg[i].isAgg ) continue; |
| 1704 | sqliteExprCode(pParse, pParse->aAgg[i].pExpr); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1705 | sqliteVdbeAddOp(v, OP_AggSet, 0, i); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1706 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1707 | sqliteVdbeResolveLabel(v, lbl1); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1708 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1709 | for(i=0; i<pParse->nAgg; i++){ |
| 1710 | Expr *pE; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1711 | int j; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1712 | if( !pParse->aAgg[i].isAgg ) continue; |
| 1713 | pE = pParse->aAgg[i].pExpr; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1714 | assert( pE->op==TK_AGG_FUNCTION ); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1715 | if( pE->pList ){ |
| 1716 | for(j=0; j<pE->pList->nExpr; j++){ |
| 1717 | sqliteExprCode(pParse, pE->pList->a[j].pExpr); |
| 1718 | } |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 1719 | } |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1720 | sqliteVdbeAddOp(v, OP_Integer, i, 0); |
drh | f55f25f | 2002-02-28 01:46:11 +0000 | [diff] [blame] | 1721 | sqliteVdbeAddOp(v, OP_AggFunc, 0, pE->pList ? pE->pList->nExpr : 0); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1722 | assert( pParse->aAgg[i].pFunc!=0 ); |
| 1723 | assert( pParse->aAgg[i].pFunc->xStep!=0 ); |
| 1724 | sqliteVdbeChangeP3(v, -1, (char*)pParse->aAgg[i].pFunc, P3_POINTER); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1725 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1726 | } |
| 1727 | |
| 1728 | /* End the database scan loop. |
| 1729 | */ |
| 1730 | sqliteWhereEnd(pWInfo); |
| 1731 | |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1732 | /* If we are processing aggregates, we need to set up a second loop |
| 1733 | ** over all of the aggregate values and process them. |
| 1734 | */ |
| 1735 | if( isAgg ){ |
| 1736 | int endagg = sqliteVdbeMakeLabel(v); |
| 1737 | int startagg; |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1738 | startagg = sqliteVdbeAddOp(v, OP_AggNext, 0, endagg); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1739 | pParse->useAgg = 1; |
| 1740 | if( pHaving ){ |
| 1741 | sqliteExprIfFalse(pParse, pHaving, startagg); |
| 1742 | } |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 1743 | if( selectInnerLoop(pParse, pEList, 0, 0, pOrderBy, distinct, eDest, iParm, |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1744 | startagg, endagg) ){ |
drh | 1d83f05 | 2002-02-17 00:30:36 +0000 | [diff] [blame] | 1745 | goto select_end; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1746 | } |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1747 | sqliteVdbeAddOp(v, OP_Goto, 0, startagg); |
| 1748 | sqliteVdbeResolveLabel(v, endagg); |
| 1749 | sqliteVdbeAddOp(v, OP_Noop, 0, 0); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1750 | pParse->useAgg = 0; |
| 1751 | } |
| 1752 | |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1753 | /* If there is an ORDER BY clause, then we need to sort the results |
| 1754 | ** and send them to the callback one by one. |
| 1755 | */ |
| 1756 | if( pOrderBy ){ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1757 | generateSortTail(v, pEList->nExpr); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1758 | } |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 1759 | |
| 1760 | |
| 1761 | /* Issue a null callback if that is what the user wants. |
| 1762 | */ |
| 1763 | if( (pParse->db->flags & SQLITE_NullCallback)!=0 && eDest==SRT_Callback ){ |
| 1764 | sqliteVdbeAddOp(v, OP_NullCallback, pEList->nExpr, 0); |
| 1765 | } |
| 1766 | |
drh | 1d83f05 | 2002-02-17 00:30:36 +0000 | [diff] [blame] | 1767 | /* The SELECT was successfully coded. Set the return code to 0 |
| 1768 | ** to indicate no errors. |
| 1769 | */ |
| 1770 | rc = 0; |
| 1771 | |
| 1772 | /* Control jumps to here if an error is encountered above, or upon |
| 1773 | ** successful coding of the SELECT. |
| 1774 | */ |
| 1775 | select_end: |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1776 | pParse->nTab = base; |
drh | 1d83f05 | 2002-02-17 00:30:36 +0000 | [diff] [blame] | 1777 | sqliteAggregateInfoReset(pParse); |
| 1778 | return rc; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1779 | } |