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