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