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