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