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