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