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 | 2c61c07 | 2004-07-20 00:20:23 +0000 | [diff] [blame^] | 15 | ** $Id: select.c,v 1.200 2004/07/20 00:20:23 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 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 24 | Select *sqlite3SelectNew( |
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 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 38 | sqlite3ExprListDelete(pEList); |
| 39 | sqlite3SrcListDelete(pSrc); |
| 40 | sqlite3ExprDelete(pWhere); |
| 41 | sqlite3ExprListDelete(pGroupBy); |
| 42 | sqlite3ExprDelete(pHaving); |
| 43 | sqlite3ExprListDelete(pOrderBy); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 44 | }else{ |
drh | b733d03 | 2004-01-24 20:18:12 +0000 | [diff] [blame] | 45 | if( pEList==0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 46 | pEList = sqlite3ExprListAppend(0, sqlite3Expr(TK_ALL,0,0,0), 0); |
drh | b733d03 | 2004-01-24 20:18:12 +0000 | [diff] [blame] | 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 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 80 | int sqlite3JoinType(Parse *pParse, Token *pA, Token *pB, Token *pC){ |
drh | 01f3f25 | 2002-05-24 16:14:15 +0000 | [diff] [blame] | 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 |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 105 | && sqlite3StrNICmp(p->z, keywords[j].zKeyword, p->n)==0 ){ |
drh | 01f3f25 | 2002-05-24 16:14:15 +0000 | [diff] [blame] | 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; } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 123 | sqlite3SetNString(&pParse->zErrMsg, "unknown or unsupported join type: ", 0, |
drh | 01f3f25 | 2002-05-24 16:14:15 +0000 | [diff] [blame] | 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 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 128 | sqlite3ErrorMsg(pParse, |
drh | da93d23 | 2003-03-31 02:12:46 +0000 | [diff] [blame] | 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++){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 142 | if( sqlite3StrICmp(pTab->aCol[i].zName, zCol)==0 ) return i; |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 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; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 165 | pE1a = sqlite3Expr(TK_ID, 0, 0, &dummy); |
| 166 | pE2a = sqlite3Expr(TK_ID, 0, 0, &dummy); |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 167 | dummy.z = pTab1->zName; |
| 168 | dummy.n = strlen(dummy.z); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 169 | pE1b = sqlite3Expr(TK_ID, 0, 0, &dummy); |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 170 | dummy.z = pTab2->zName; |
| 171 | dummy.n = strlen(dummy.z); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 172 | pE2b = sqlite3Expr(TK_ID, 0, 0, &dummy); |
| 173 | pE1c = sqlite3Expr(TK_DOT, pE1b, pE1a, 0); |
| 174 | pE2c = sqlite3Expr(TK_DOT, pE2b, pE2a, 0); |
| 175 | pE = sqlite3Expr(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 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 178 | *ppExpr = sqlite3Expr(TK_AND, *ppExpr, pE, 0); |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 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 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 225 | sqlite3ErrorMsg(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 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 240 | sqlite3ErrorMsg(pParse, "cannot have both ON and USING " |
drh | da93d23 | 2003-03-31 02:12:46 +0000 | [diff] [blame] | 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{ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 253 | p->pWhere = sqlite3Expr(TK_AND, p->pWhere, pTerm->pOn, 0); |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 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 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 273 | sqlite3ErrorMsg(pParse, "cannot join using column %s - column " |
drh | da93d23 | 2003-03-31 02:12:46 +0000 | [diff] [blame] | 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 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 287 | void sqlite3SelectDelete(Select *p){ |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 288 | if( p==0 ) return; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 289 | sqlite3ExprListDelete(p->pEList); |
| 290 | sqlite3SrcListDelete(p->pSrc); |
| 291 | sqlite3ExprDelete(p->pWhere); |
| 292 | sqlite3ExprListDelete(p->pGroupBy); |
| 293 | sqlite3ExprDelete(p->pHaving); |
| 294 | sqlite3ExprListDelete(p->pOrderBy); |
| 295 | sqlite3SelectDelete(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){ |
drh | c926afb | 2002-06-20 03:38:26 +0000 | [diff] [blame] | 315 | int i; |
drh | c926afb | 2002-06-20 03:38:26 +0000 | [diff] [blame] | 316 | for(i=0; i<pOrderBy->nExpr; i++){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 317 | sqlite3ExprCode(pParse, pOrderBy->a[i].pExpr); |
drh | c926afb | 2002-06-20 03:38:26 +0000 | [diff] [blame] | 318 | } |
danielk1977 | ededfd5 | 2004-06-17 07:53:01 +0000 | [diff] [blame] | 319 | sqlite3VdbeAddOp(v, OP_MakeRecord, pOrderBy->nExpr, 0); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 320 | sqlite3VdbeAddOp(v, OP_SortPut, 0, 0); |
drh | c926afb | 2002-06-20 03:38:26 +0000 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | /* |
drh | ea48eb2 | 2004-07-19 23:16:38 +0000 | [diff] [blame] | 324 | ** Add code to implement the OFFSET and LIMIT |
| 325 | */ |
| 326 | static void codeLimiter( |
drh | bab39e1 | 2004-07-19 23:38:11 +0000 | [diff] [blame] | 327 | Vdbe *v, /* Generate code into this VM */ |
drh | ea48eb2 | 2004-07-19 23:16:38 +0000 | [diff] [blame] | 328 | Select *p, /* The SELECT statement being coded */ |
| 329 | int iContinue, /* Jump here to skip the current record */ |
| 330 | int iBreak, /* Jump here to end the loop */ |
| 331 | int nPop /* Number of times to pop stack when jumping */ |
| 332 | ){ |
drh | ea48eb2 | 2004-07-19 23:16:38 +0000 | [diff] [blame] | 333 | if( p->iOffset>=0 ){ |
| 334 | int addr = sqlite3VdbeCurrentAddr(v) + 2; |
| 335 | if( nPop>0 ) addr++; |
| 336 | sqlite3VdbeAddOp(v, OP_MemIncr, p->iOffset, addr); |
| 337 | if( nPop>0 ){ |
| 338 | sqlite3VdbeAddOp(v, OP_Pop, nPop, 0); |
| 339 | } |
| 340 | sqlite3VdbeAddOp(v, OP_Goto, 0, iContinue); |
| 341 | } |
| 342 | if( p->iLimit>=0 ){ |
| 343 | sqlite3VdbeAddOp(v, OP_MemIncr, p->iLimit, iBreak); |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | /* |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 348 | ** This routine generates the code for the inside of the inner loop |
| 349 | ** of a SELECT. |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 350 | ** |
drh | 38640e1 | 2002-07-05 21:42:36 +0000 | [diff] [blame] | 351 | ** If srcTab and nColumn are both zero, then the pEList expressions |
| 352 | ** are evaluated in order to get the data for this row. If nColumn>0 |
| 353 | ** then data is pulled from srcTab and pEList is used only to get the |
| 354 | ** datatypes for each column. |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 355 | */ |
| 356 | static int selectInnerLoop( |
| 357 | Parse *pParse, /* The parser context */ |
drh | df199a2 | 2002-06-14 22:38:41 +0000 | [diff] [blame] | 358 | Select *p, /* The complete select statement being coded */ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 359 | ExprList *pEList, /* List of values being extracted */ |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 360 | int srcTab, /* Pull data from this table */ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 361 | int nColumn, /* Number of columns in the source table */ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 362 | ExprList *pOrderBy, /* If not NULL, sort results using this key */ |
| 363 | int distinct, /* If >=0, make sure results are distinct */ |
| 364 | int eDest, /* How to dispose of the results */ |
| 365 | int iParm, /* An argument to the disposal method */ |
| 366 | int iContinue, /* Jump here to continue with next row */ |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 367 | int iBreak, /* Jump here to break out of the inner loop */ |
| 368 | char *aff /* affinity string if eDest is SRT_Union */ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 369 | ){ |
| 370 | Vdbe *v = pParse->pVdbe; |
| 371 | int i; |
drh | ea48eb2 | 2004-07-19 23:16:38 +0000 | [diff] [blame] | 372 | int hasDistinct; /* True if the DISTINCT keyword is present */ |
drh | 38640e1 | 2002-07-05 21:42:36 +0000 | [diff] [blame] | 373 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 374 | if( v==0 ) return 0; |
drh | 38640e1 | 2002-07-05 21:42:36 +0000 | [diff] [blame] | 375 | assert( pEList!=0 ); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 376 | |
drh | df199a2 | 2002-06-14 22:38:41 +0000 | [diff] [blame] | 377 | /* If there was a LIMIT clause on the SELECT statement, then do the check |
| 378 | ** to see if this row should be output. |
| 379 | */ |
drh | ea48eb2 | 2004-07-19 23:16:38 +0000 | [diff] [blame] | 380 | hasDistinct = distinct>=0 && pEList && pEList->nExpr>0; |
| 381 | if( pOrderBy==0 && !hasDistinct ){ |
drh | bab39e1 | 2004-07-19 23:38:11 +0000 | [diff] [blame] | 382 | codeLimiter(v, p, iContinue, iBreak, 0); |
drh | df199a2 | 2002-06-14 22:38:41 +0000 | [diff] [blame] | 383 | } |
| 384 | |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 385 | /* Pull the requested columns. |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 386 | */ |
drh | 38640e1 | 2002-07-05 21:42:36 +0000 | [diff] [blame] | 387 | if( nColumn>0 ){ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 388 | for(i=0; i<nColumn; i++){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 389 | sqlite3VdbeAddOp(v, OP_Column, srcTab, i); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 390 | } |
drh | 38640e1 | 2002-07-05 21:42:36 +0000 | [diff] [blame] | 391 | }else{ |
| 392 | nColumn = pEList->nExpr; |
| 393 | for(i=0; i<pEList->nExpr; i++){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 394 | sqlite3ExprCode(pParse, pEList->a[i].pExpr); |
drh | 38640e1 | 2002-07-05 21:42:36 +0000 | [diff] [blame] | 395 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 396 | } |
| 397 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 398 | /* If the DISTINCT keyword was present on the SELECT statement |
| 399 | ** and this row has been seen before, then do not make this row |
| 400 | ** part of the result. |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 401 | */ |
drh | ea48eb2 | 2004-07-19 23:16:38 +0000 | [diff] [blame] | 402 | if( hasDistinct ){ |
drh | 0bd1f4e | 2002-06-06 18:54:39 +0000 | [diff] [blame] | 403 | #if NULL_ALWAYS_DISTINCT |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 404 | sqlite3VdbeAddOp(v, OP_IsNull, -pEList->nExpr, sqlite3VdbeCurrentAddr(v)+7); |
drh | 0bd1f4e | 2002-06-06 18:54:39 +0000 | [diff] [blame] | 405 | #endif |
danielk1977 | ededfd5 | 2004-06-17 07:53:01 +0000 | [diff] [blame] | 406 | /* Deliberately leave the affinity string off of the following |
| 407 | ** OP_MakeRecord */ |
| 408 | sqlite3VdbeAddOp(v, OP_MakeRecord, pEList->nExpr * -1, 0); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 409 | sqlite3VdbeAddOp(v, OP_Distinct, distinct, sqlite3VdbeCurrentAddr(v)+3); |
| 410 | sqlite3VdbeAddOp(v, OP_Pop, pEList->nExpr+1, 0); |
| 411 | sqlite3VdbeAddOp(v, OP_Goto, 0, iContinue); |
danielk1977 | 0f69c1e | 2004-05-29 11:24:50 +0000 | [diff] [blame] | 412 | sqlite3VdbeAddOp(v, OP_String8, 0, 0); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 413 | sqlite3VdbeAddOp(v, OP_PutStrKey, distinct, 0); |
drh | ea48eb2 | 2004-07-19 23:16:38 +0000 | [diff] [blame] | 414 | if( pOrderBy==0 ){ |
drh | bab39e1 | 2004-07-19 23:38:11 +0000 | [diff] [blame] | 415 | codeLimiter(v, p, iContinue, iBreak, nColumn); |
drh | ea48eb2 | 2004-07-19 23:16:38 +0000 | [diff] [blame] | 416 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 417 | } |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 418 | |
drh | c926afb | 2002-06-20 03:38:26 +0000 | [diff] [blame] | 419 | switch( eDest ){ |
| 420 | /* In this mode, write each query result to the key of the temporary |
| 421 | ** table iParm. |
| 422 | */ |
| 423 | case SRT_Union: { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 424 | sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, NULL_ALWAYS_DISTINCT); |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 425 | sqlite3VdbeChangeP3(v, -1, aff, P3_STATIC); |
danielk1977 | 0f69c1e | 2004-05-29 11:24:50 +0000 | [diff] [blame] | 426 | sqlite3VdbeAddOp(v, OP_String8, 0, 0); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 427 | sqlite3VdbeAddOp(v, OP_PutStrKey, iParm, 0); |
drh | c926afb | 2002-06-20 03:38:26 +0000 | [diff] [blame] | 428 | break; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 429 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 430 | |
drh | c926afb | 2002-06-20 03:38:26 +0000 | [diff] [blame] | 431 | /* Store the result as data using a unique key. |
| 432 | */ |
| 433 | case SRT_Table: |
| 434 | case SRT_TempTable: { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 435 | sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, 0); |
drh | c926afb | 2002-06-20 03:38:26 +0000 | [diff] [blame] | 436 | if( pOrderBy ){ |
| 437 | pushOntoSorter(pParse, v, pOrderBy); |
| 438 | }else{ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 439 | sqlite3VdbeAddOp(v, OP_NewRecno, iParm, 0); |
| 440 | sqlite3VdbeAddOp(v, OP_Pull, 1, 0); |
| 441 | sqlite3VdbeAddOp(v, OP_PutIntKey, iParm, 0); |
drh | c926afb | 2002-06-20 03:38:26 +0000 | [diff] [blame] | 442 | } |
| 443 | break; |
| 444 | } |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 445 | |
drh | c926afb | 2002-06-20 03:38:26 +0000 | [diff] [blame] | 446 | /* Construct a record from the query result, but instead of |
| 447 | ** saving that record, use it as a key to delete elements from |
| 448 | ** the temporary table iParm. |
| 449 | */ |
| 450 | case SRT_Except: { |
| 451 | int addr; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 452 | addr = sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, NULL_ALWAYS_DISTINCT); |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 453 | sqlite3VdbeChangeP3(v, -1, aff, P3_STATIC); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 454 | sqlite3VdbeAddOp(v, OP_NotFound, iParm, addr+3); |
| 455 | sqlite3VdbeAddOp(v, OP_Delete, iParm, 0); |
drh | c926afb | 2002-06-20 03:38:26 +0000 | [diff] [blame] | 456 | break; |
| 457 | } |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 458 | |
drh | c926afb | 2002-06-20 03:38:26 +0000 | [diff] [blame] | 459 | /* If we are creating a set for an "expr IN (SELECT ...)" construct, |
| 460 | ** then there should be a single item on the stack. Write this |
| 461 | ** item into the set table with bogus data. |
| 462 | */ |
| 463 | case SRT_Set: { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 464 | int addr1 = sqlite3VdbeCurrentAddr(v); |
drh | 52b36ca | 2004-01-14 13:38:54 +0000 | [diff] [blame] | 465 | int addr2; |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 466 | |
drh | c926afb | 2002-06-20 03:38:26 +0000 | [diff] [blame] | 467 | assert( nColumn==1 ); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 468 | sqlite3VdbeAddOp(v, OP_NotNull, -1, addr1+3); |
| 469 | sqlite3VdbeAddOp(v, OP_Pop, 1, 0); |
| 470 | addr2 = sqlite3VdbeAddOp(v, OP_Goto, 0, 0); |
drh | c926afb | 2002-06-20 03:38:26 +0000 | [diff] [blame] | 471 | if( pOrderBy ){ |
| 472 | pushOntoSorter(pParse, v, pOrderBy); |
| 473 | }else{ |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 474 | char const *affStr; |
| 475 | char aff = (iParm>>16)&0xFF; |
| 476 | aff = sqlite3CompareAffinity(pEList->a[0].pExpr, aff); |
| 477 | affStr = sqlite3AffinityString(aff); |
danielk1977 | ededfd5 | 2004-06-17 07:53:01 +0000 | [diff] [blame] | 478 | sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, affStr, P3_STATIC); |
danielk1977 | 0f69c1e | 2004-05-29 11:24:50 +0000 | [diff] [blame] | 479 | sqlite3VdbeAddOp(v, OP_String8, 0, 0); |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 480 | sqlite3VdbeAddOp(v, OP_PutStrKey, (iParm&0x0000FFFF), 0); |
drh | c926afb | 2002-06-20 03:38:26 +0000 | [diff] [blame] | 481 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 482 | sqlite3VdbeChangeP2(v, addr2, sqlite3VdbeCurrentAddr(v)); |
drh | c926afb | 2002-06-20 03:38:26 +0000 | [diff] [blame] | 483 | break; |
| 484 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 485 | |
drh | c926afb | 2002-06-20 03:38:26 +0000 | [diff] [blame] | 486 | /* If this is a scalar select that is part of an expression, then |
| 487 | ** store the results in the appropriate memory cell and break out |
| 488 | ** of the scan loop. |
| 489 | */ |
| 490 | case SRT_Mem: { |
| 491 | assert( nColumn==1 ); |
| 492 | if( pOrderBy ){ |
| 493 | pushOntoSorter(pParse, v, pOrderBy); |
| 494 | }else{ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 495 | sqlite3VdbeAddOp(v, OP_MemStore, iParm, 1); |
| 496 | sqlite3VdbeAddOp(v, OP_Goto, 0, iBreak); |
drh | c926afb | 2002-06-20 03:38:26 +0000 | [diff] [blame] | 497 | } |
| 498 | break; |
| 499 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 500 | |
drh | f46f905 | 2002-06-22 02:33:38 +0000 | [diff] [blame] | 501 | /* Send the data to the callback function. |
| 502 | */ |
| 503 | case SRT_Callback: |
| 504 | case SRT_Sorter: { |
| 505 | if( pOrderBy ){ |
drh | ce665cf | 2004-05-21 03:01:58 +0000 | [diff] [blame] | 506 | sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, 0); |
drh | f46f905 | 2002-06-22 02:33:38 +0000 | [diff] [blame] | 507 | pushOntoSorter(pParse, v, pOrderBy); |
| 508 | }else{ |
| 509 | assert( eDest==SRT_Callback ); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 510 | sqlite3VdbeAddOp(v, OP_Callback, nColumn, 0); |
drh | f46f905 | 2002-06-22 02:33:38 +0000 | [diff] [blame] | 511 | } |
| 512 | break; |
| 513 | } |
| 514 | |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 515 | /* Invoke a subroutine to handle the results. The subroutine itself |
| 516 | ** is responsible for popping the results off of the stack. |
| 517 | */ |
| 518 | case SRT_Subroutine: { |
drh | ac82fcf | 2002-09-08 17:23:41 +0000 | [diff] [blame] | 519 | if( pOrderBy ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 520 | sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, 0); |
drh | ac82fcf | 2002-09-08 17:23:41 +0000 | [diff] [blame] | 521 | pushOntoSorter(pParse, v, pOrderBy); |
| 522 | }else{ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 523 | sqlite3VdbeAddOp(v, OP_Gosub, 0, iParm); |
drh | ac82fcf | 2002-09-08 17:23:41 +0000 | [diff] [blame] | 524 | } |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 525 | break; |
| 526 | } |
| 527 | |
drh | c926afb | 2002-06-20 03:38:26 +0000 | [diff] [blame] | 528 | /* Discard the results. This is used for SELECT statements inside |
| 529 | ** the body of a TRIGGER. The purpose of such selects is to call |
| 530 | ** user-defined functions that have side effects. We do not care |
| 531 | ** about the actual results of the select. |
| 532 | */ |
drh | c926afb | 2002-06-20 03:38:26 +0000 | [diff] [blame] | 533 | default: { |
drh | f46f905 | 2002-06-22 02:33:38 +0000 | [diff] [blame] | 534 | assert( eDest==SRT_Discard ); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 535 | sqlite3VdbeAddOp(v, OP_Pop, nColumn, 0); |
drh | c926afb | 2002-06-20 03:38:26 +0000 | [diff] [blame] | 536 | break; |
| 537 | } |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 538 | } |
| 539 | return 0; |
| 540 | } |
| 541 | |
| 542 | /* |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 543 | ** If the inner loop was generated using a non-null pOrderBy argument, |
| 544 | ** then the results were placed in a sorter. After the loop is terminated |
| 545 | ** we need to run the sorter and output the results. The following |
| 546 | ** routine generates the code needed to do that. |
| 547 | */ |
drh | c926afb | 2002-06-20 03:38:26 +0000 | [diff] [blame] | 548 | static void generateSortTail( |
drh | ffbc308 | 2004-05-21 01:29:06 +0000 | [diff] [blame] | 549 | Parse *pParse, /* The parsing context */ |
drh | c926afb | 2002-06-20 03:38:26 +0000 | [diff] [blame] | 550 | Select *p, /* The SELECT statement */ |
| 551 | Vdbe *v, /* Generate code into this VDBE */ |
| 552 | int nColumn, /* Number of columns of data */ |
| 553 | int eDest, /* Write the sorted results here */ |
| 554 | int iParm /* Optional parameter associated with eDest */ |
| 555 | ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 556 | int end1 = sqlite3VdbeMakeLabel(v); |
| 557 | int end2 = sqlite3VdbeMakeLabel(v); |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 558 | int addr; |
drh | ffbc308 | 2004-05-21 01:29:06 +0000 | [diff] [blame] | 559 | KeyInfo *pInfo; |
| 560 | ExprList *pOrderBy; |
| 561 | int nCol, i; |
| 562 | sqlite *db = pParse->db; |
| 563 | |
drh | f46f905 | 2002-06-22 02:33:38 +0000 | [diff] [blame] | 564 | if( eDest==SRT_Sorter ) return; |
drh | ffbc308 | 2004-05-21 01:29:06 +0000 | [diff] [blame] | 565 | pOrderBy = p->pOrderBy; |
| 566 | nCol = pOrderBy->nExpr; |
| 567 | pInfo = sqliteMalloc( sizeof(*pInfo) + nCol*(sizeof(CollSeq*)+1) ); |
| 568 | if( pInfo==0 ) return; |
| 569 | pInfo->aSortOrder = (char*)&pInfo->aColl[nCol]; |
| 570 | pInfo->nField = nCol; |
| 571 | for(i=0; i<nCol; i++){ |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 572 | /* If a collation sequence was specified explicity, then it |
| 573 | ** is stored in pOrderBy->a[i].zName. Otherwise, use the default |
| 574 | ** collation type for the expression. |
| 575 | */ |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 576 | pInfo->aColl[i] = sqlite3ExprCollSeq(pParse, pOrderBy->a[i].pExpr); |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 577 | if( !pInfo->aColl[i] ){ |
| 578 | pInfo->aColl[i] = db->pDfltColl; |
| 579 | } |
drh | ffbc308 | 2004-05-21 01:29:06 +0000 | [diff] [blame] | 580 | pInfo->aSortOrder[i] = pOrderBy->a[i].sortOrder; |
| 581 | } |
| 582 | sqlite3VdbeOp3(v, OP_Sort, 0, 0, (char*)pInfo, P3_KEYINFO_HANDOFF); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 583 | addr = sqlite3VdbeAddOp(v, OP_SortNext, 0, end1); |
drh | bab39e1 | 2004-07-19 23:38:11 +0000 | [diff] [blame] | 584 | codeLimiter(v, p, addr, end2, 1); |
drh | c926afb | 2002-06-20 03:38:26 +0000 | [diff] [blame] | 585 | switch( eDest ){ |
drh | c926afb | 2002-06-20 03:38:26 +0000 | [diff] [blame] | 586 | case SRT_Table: |
| 587 | case SRT_TempTable: { |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 588 | sqlite3VdbeAddOp(v, OP_NewRecno, iParm, 0); |
| 589 | sqlite3VdbeAddOp(v, OP_Pull, 1, 0); |
| 590 | sqlite3VdbeAddOp(v, OP_PutIntKey, iParm, 0); |
drh | c926afb | 2002-06-20 03:38:26 +0000 | [diff] [blame] | 591 | break; |
| 592 | } |
| 593 | case SRT_Set: { |
| 594 | assert( nColumn==1 ); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 595 | sqlite3VdbeAddOp(v, OP_NotNull, -1, sqlite3VdbeCurrentAddr(v)+3); |
| 596 | sqlite3VdbeAddOp(v, OP_Pop, 1, 0); |
| 597 | sqlite3VdbeAddOp(v, OP_Goto, 0, sqlite3VdbeCurrentAddr(v)+3); |
danielk1977 | ededfd5 | 2004-06-17 07:53:01 +0000 | [diff] [blame] | 598 | sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, "n", P3_STATIC); |
danielk1977 | 0f69c1e | 2004-05-29 11:24:50 +0000 | [diff] [blame] | 599 | sqlite3VdbeAddOp(v, OP_String8, 0, 0); |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 600 | sqlite3VdbeAddOp(v, OP_PutStrKey, (iParm&0x0000FFFF), 0); |
drh | c926afb | 2002-06-20 03:38:26 +0000 | [diff] [blame] | 601 | break; |
| 602 | } |
| 603 | case SRT_Mem: { |
| 604 | assert( nColumn==1 ); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 605 | sqlite3VdbeAddOp(v, OP_MemStore, iParm, 1); |
| 606 | sqlite3VdbeAddOp(v, OP_Goto, 0, end1); |
drh | c926afb | 2002-06-20 03:38:26 +0000 | [diff] [blame] | 607 | break; |
| 608 | } |
drh | ce665cf | 2004-05-21 03:01:58 +0000 | [diff] [blame] | 609 | case SRT_Callback: |
drh | ac82fcf | 2002-09-08 17:23:41 +0000 | [diff] [blame] | 610 | case SRT_Subroutine: { |
| 611 | int i; |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 612 | sqlite3VdbeAddOp(v, OP_Integer, p->pEList->nExpr, 0); |
| 613 | sqlite3VdbeAddOp(v, OP_Pull, 1, 0); |
drh | ac82fcf | 2002-09-08 17:23:41 +0000 | [diff] [blame] | 614 | for(i=0; i<nColumn; i++){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 615 | sqlite3VdbeAddOp(v, OP_Column, -1-i, i); |
drh | ac82fcf | 2002-09-08 17:23:41 +0000 | [diff] [blame] | 616 | } |
drh | ce665cf | 2004-05-21 03:01:58 +0000 | [diff] [blame] | 617 | if( eDest==SRT_Callback ){ |
| 618 | sqlite3VdbeAddOp(v, OP_Callback, nColumn, 0); |
| 619 | }else{ |
| 620 | sqlite3VdbeAddOp(v, OP_Gosub, 0, iParm); |
| 621 | } |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 622 | sqlite3VdbeAddOp(v, OP_Pop, 2, 0); |
drh | ac82fcf | 2002-09-08 17:23:41 +0000 | [diff] [blame] | 623 | break; |
| 624 | } |
drh | c926afb | 2002-06-20 03:38:26 +0000 | [diff] [blame] | 625 | default: { |
drh | f46f905 | 2002-06-22 02:33:38 +0000 | [diff] [blame] | 626 | /* Do nothing */ |
drh | c926afb | 2002-06-20 03:38:26 +0000 | [diff] [blame] | 627 | break; |
| 628 | } |
| 629 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 630 | sqlite3VdbeAddOp(v, OP_Goto, 0, addr); |
| 631 | sqlite3VdbeResolveLabel(v, end2); |
| 632 | sqlite3VdbeAddOp(v, OP_Pop, 1, 0); |
| 633 | sqlite3VdbeResolveLabel(v, end1); |
| 634 | sqlite3VdbeAddOp(v, OP_SortReset, 0, 0); |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | /* |
danielk1977 | 517eb64 | 2004-06-07 10:00:31 +0000 | [diff] [blame] | 638 | ** Return a pointer to a string containing the 'declaration type' of the |
| 639 | ** expression pExpr. The string may be treated as static by the caller. |
drh | e78e828 | 2003-01-19 03:59:45 +0000 | [diff] [blame] | 640 | ** |
danielk1977 | 517eb64 | 2004-06-07 10:00:31 +0000 | [diff] [blame] | 641 | ** If the declaration type is the exact datatype definition extracted from |
| 642 | ** the original CREATE TABLE statement if the expression is a column. |
| 643 | ** |
| 644 | ** The declaration type for an expression is either TEXT, NUMERIC or ANY. |
| 645 | ** The declaration type for a ROWID field is INTEGER. |
| 646 | */ |
| 647 | static const char *columnType(Parse *pParse, SrcList *pTabList, Expr *pExpr){ |
danielk1977 | 00e279d | 2004-06-21 07:36:32 +0000 | [diff] [blame] | 648 | char const *zType; |
danielk1977 | 517eb64 | 2004-06-07 10:00:31 +0000 | [diff] [blame] | 649 | int j; |
danielk1977 | 00e279d | 2004-06-21 07:36:32 +0000 | [diff] [blame] | 650 | if( pExpr==0 || pTabList==0 ) return 0; |
| 651 | |
| 652 | switch( pExpr->op ){ |
| 653 | case TK_COLUMN: { |
| 654 | Table *pTab; |
| 655 | int iCol = pExpr->iColumn; |
| 656 | for(j=0; j<pTabList->nSrc && pTabList->a[j].iCursor!=pExpr->iTable; j++){} |
| 657 | assert( j<pTabList->nSrc ); |
| 658 | pTab = pTabList->a[j].pTab; |
| 659 | if( iCol<0 ) iCol = pTab->iPKey; |
| 660 | assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) ); |
| 661 | if( iCol<0 ){ |
| 662 | zType = "INTEGER"; |
| 663 | }else{ |
| 664 | zType = pTab->aCol[iCol].zType; |
| 665 | } |
| 666 | break; |
danielk1977 | 517eb64 | 2004-06-07 10:00:31 +0000 | [diff] [blame] | 667 | } |
danielk1977 | 00e279d | 2004-06-21 07:36:32 +0000 | [diff] [blame] | 668 | case TK_AS: |
| 669 | zType = columnType(pParse, pTabList, pExpr->pLeft); |
| 670 | break; |
| 671 | case TK_SELECT: { |
| 672 | Select *pS = pExpr->pSelect; |
| 673 | zType = columnType(pParse, pS->pSrc, pS->pEList->a[0].pExpr); |
| 674 | break; |
danielk1977 | 517eb64 | 2004-06-07 10:00:31 +0000 | [diff] [blame] | 675 | } |
danielk1977 | 00e279d | 2004-06-21 07:36:32 +0000 | [diff] [blame] | 676 | default: |
| 677 | zType = 0; |
danielk1977 | 517eb64 | 2004-06-07 10:00:31 +0000 | [diff] [blame] | 678 | } |
danielk1977 | 00e279d | 2004-06-21 07:36:32 +0000 | [diff] [blame] | 679 | |
danielk1977 | 517eb64 | 2004-06-07 10:00:31 +0000 | [diff] [blame] | 680 | return zType; |
| 681 | } |
| 682 | |
| 683 | /* |
| 684 | ** Generate code that will tell the VDBE the declaration types of columns |
| 685 | ** in the result set. |
drh | fcb78a4 | 2003-01-18 20:11:05 +0000 | [diff] [blame] | 686 | */ |
| 687 | static void generateColumnTypes( |
| 688 | Parse *pParse, /* Parser context */ |
drh | fcb78a4 | 2003-01-18 20:11:05 +0000 | [diff] [blame] | 689 | SrcList *pTabList, /* List of tables */ |
| 690 | ExprList *pEList /* Expressions defining the result set */ |
| 691 | ){ |
| 692 | Vdbe *v = pParse->pVdbe; |
danielk1977 | 517eb64 | 2004-06-07 10:00:31 +0000 | [diff] [blame] | 693 | int i; |
drh | fcb78a4 | 2003-01-18 20:11:05 +0000 | [diff] [blame] | 694 | for(i=0; i<pEList->nExpr; i++){ |
| 695 | Expr *p = pEList->a[i].pExpr; |
danielk1977 | 517eb64 | 2004-06-07 10:00:31 +0000 | [diff] [blame] | 696 | const char *zType = columnType(pParse, pTabList, p); |
danielk1977 | 00e279d | 2004-06-21 07:36:32 +0000 | [diff] [blame] | 697 | if( zType==0 ) continue; |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 698 | /* The vdbe must make it's own copy of the column-type, in case the |
| 699 | ** schema is reset before this virtual machine is deleted. |
| 700 | */ |
| 701 | sqlite3VdbeSetColName(v, i+pEList->nExpr, zType, strlen(zType)); |
drh | fcb78a4 | 2003-01-18 20:11:05 +0000 | [diff] [blame] | 702 | } |
| 703 | } |
| 704 | |
| 705 | /* |
| 706 | ** Generate code that will tell the VDBE the names of columns |
| 707 | ** in the result set. This information is used to provide the |
drh | fcabd46 | 2004-02-20 14:50:58 +0000 | [diff] [blame] | 708 | ** azCol[] values in the callback. |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 709 | */ |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 710 | static void generateColumnNames( |
| 711 | Parse *pParse, /* Parser context */ |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 712 | SrcList *pTabList, /* List of tables */ |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 713 | ExprList *pEList /* Expressions defining the result set */ |
| 714 | ){ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 715 | Vdbe *v = pParse->pVdbe; |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 716 | int i, j; |
drh | fcabd46 | 2004-02-20 14:50:58 +0000 | [diff] [blame] | 717 | sqlite *db = pParse->db; |
| 718 | int fullNames, shortNames; |
| 719 | |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 720 | /* If this is an EXPLAIN, skip this step */ |
| 721 | if( pParse->explain ){ |
danielk1977 | 61de0d1 | 2004-05-27 23:56:16 +0000 | [diff] [blame] | 722 | return; |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 723 | } |
| 724 | |
drh | d650275 | 2004-02-16 03:44:01 +0000 | [diff] [blame] | 725 | assert( v!=0 ); |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 726 | if( pParse->colNamesSet || v==0 || sqlite3_malloc_failed ) return; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 727 | pParse->colNamesSet = 1; |
drh | fcabd46 | 2004-02-20 14:50:58 +0000 | [diff] [blame] | 728 | fullNames = (db->flags & SQLITE_FullColNames)!=0; |
| 729 | shortNames = (db->flags & SQLITE_ShortColNames)!=0; |
danielk1977 | 22322fd | 2004-05-25 23:35:17 +0000 | [diff] [blame] | 730 | sqlite3VdbeSetNumCols(v, pEList->nExpr); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 731 | for(i=0; i<pEList->nExpr; i++){ |
| 732 | Expr *p; |
drh | 5a38705 | 2003-01-11 14:19:51 +0000 | [diff] [blame] | 733 | p = pEList->a[i].pExpr; |
| 734 | if( p==0 ) continue; |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 735 | if( pEList->a[i].zName ){ |
| 736 | char *zName = pEList->a[i].zName; |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 737 | sqlite3VdbeSetColName(v, i, zName, strlen(zName)); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 738 | continue; |
| 739 | } |
drh | fa173a7 | 2002-07-10 21:26:00 +0000 | [diff] [blame] | 740 | if( p->op==TK_COLUMN && pTabList ){ |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 741 | Table *pTab; |
drh | 9766587 | 2002-02-13 23:22:53 +0000 | [diff] [blame] | 742 | char *zCol; |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 743 | int iCol = p->iColumn; |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 744 | for(j=0; j<pTabList->nSrc && pTabList->a[j].iCursor!=p->iTable; j++){} |
| 745 | assert( j<pTabList->nSrc ); |
| 746 | pTab = pTabList->a[j].pTab; |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 747 | if( iCol<0 ) iCol = pTab->iPKey; |
drh | 9766587 | 2002-02-13 23:22:53 +0000 | [diff] [blame] | 748 | assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) ); |
drh | b136320 | 2002-06-26 02:45:03 +0000 | [diff] [blame] | 749 | if( iCol<0 ){ |
| 750 | zCol = "_ROWID_"; |
drh | b136320 | 2002-06-26 02:45:03 +0000 | [diff] [blame] | 751 | }else{ |
| 752 | zCol = pTab->aCol[iCol].zName; |
drh | b136320 | 2002-06-26 02:45:03 +0000 | [diff] [blame] | 753 | } |
drh | fcabd46 | 2004-02-20 14:50:58 +0000 | [diff] [blame] | 754 | if( !shortNames && !fullNames && p->span.z && p->span.z[0] ){ |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 755 | sqlite3VdbeSetColName(v, i, p->span.z, p->span.n); |
drh | fcabd46 | 2004-02-20 14:50:58 +0000 | [diff] [blame] | 756 | }else if( fullNames || (!shortNames && pTabList->nSrc>1) ){ |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 757 | char *zName = 0; |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 758 | char *zTab; |
| 759 | |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 760 | zTab = pTabList->a[j].zAlias; |
drh | fcabd46 | 2004-02-20 14:50:58 +0000 | [diff] [blame] | 761 | if( fullNames || zTab==0 ) zTab = pTab->zName; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 762 | sqlite3SetString(&zName, zTab, ".", zCol, 0); |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 763 | sqlite3VdbeSetColName(v, i, zName, P3_DYNAMIC); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 764 | }else{ |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 765 | sqlite3VdbeSetColName(v, i, zCol, 0); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 766 | } |
drh | 6977fea | 2002-10-22 23:38:04 +0000 | [diff] [blame] | 767 | }else if( p->span.z && p->span.z[0] ){ |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 768 | sqlite3VdbeSetColName(v, i, p->span.z, p->span.n); |
| 769 | /* sqlite3VdbeCompressSpace(v, addr); */ |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 770 | }else{ |
| 771 | char zName[30]; |
| 772 | assert( p->op!=TK_COLUMN || pTabList==0 ); |
| 773 | sprintf(zName, "column%d", i+1); |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 774 | sqlite3VdbeSetColName(v, i, zName, 0); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 775 | } |
| 776 | } |
danielk1977 | 76d505b | 2004-05-28 13:13:02 +0000 | [diff] [blame] | 777 | generateColumnTypes(pParse, pTabList, pEList); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | /* |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 781 | ** Name of the connection operator, used for error messages. |
| 782 | */ |
| 783 | static const char *selectOpName(int id){ |
| 784 | char *z; |
| 785 | switch( id ){ |
| 786 | case TK_ALL: z = "UNION ALL"; break; |
| 787 | case TK_INTERSECT: z = "INTERSECT"; break; |
| 788 | case TK_EXCEPT: z = "EXCEPT"; break; |
| 789 | default: z = "UNION"; break; |
| 790 | } |
| 791 | return z; |
| 792 | } |
| 793 | |
| 794 | /* |
drh | 315555c | 2002-10-20 15:53:03 +0000 | [diff] [blame] | 795 | ** Forward declaration |
| 796 | */ |
| 797 | static int fillInColumnList(Parse*, Select*); |
| 798 | |
| 799 | /* |
drh | 22f70c3 | 2002-02-18 01:17:00 +0000 | [diff] [blame] | 800 | ** Given a SELECT statement, generate a Table structure that describes |
| 801 | ** the result set of that SELECT. |
| 802 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 803 | Table *sqlite3ResultSetOfSelect(Parse *pParse, char *zTabName, Select *pSelect){ |
drh | 22f70c3 | 2002-02-18 01:17:00 +0000 | [diff] [blame] | 804 | Table *pTab; |
drh | b733d03 | 2004-01-24 20:18:12 +0000 | [diff] [blame] | 805 | int i, j; |
drh | 22f70c3 | 2002-02-18 01:17:00 +0000 | [diff] [blame] | 806 | ExprList *pEList; |
drh | b733d03 | 2004-01-24 20:18:12 +0000 | [diff] [blame] | 807 | Column *aCol; |
drh | 22f70c3 | 2002-02-18 01:17:00 +0000 | [diff] [blame] | 808 | |
| 809 | if( fillInColumnList(pParse, pSelect) ){ |
| 810 | return 0; |
| 811 | } |
| 812 | pTab = sqliteMalloc( sizeof(Table) ); |
| 813 | if( pTab==0 ){ |
| 814 | return 0; |
| 815 | } |
| 816 | pTab->zName = zTabName ? sqliteStrDup(zTabName) : 0; |
| 817 | pEList = pSelect->pEList; |
| 818 | pTab->nCol = pEList->nExpr; |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 819 | assert( pTab->nCol>0 ); |
drh | b733d03 | 2004-01-24 20:18:12 +0000 | [diff] [blame] | 820 | pTab->aCol = aCol = sqliteMalloc( sizeof(pTab->aCol[0])*pTab->nCol ); |
drh | 22f70c3 | 2002-02-18 01:17:00 +0000 | [diff] [blame] | 821 | for(i=0; i<pTab->nCol; i++){ |
danielk1977 | 517eb64 | 2004-06-07 10:00:31 +0000 | [diff] [blame] | 822 | Expr *pR; |
| 823 | char *zType; |
| 824 | Expr *p = pEList->a[i].pExpr; |
drh | 22f70c3 | 2002-02-18 01:17:00 +0000 | [diff] [blame] | 825 | if( pEList->a[i].zName ){ |
drh | b733d03 | 2004-01-24 20:18:12 +0000 | [diff] [blame] | 826 | aCol[i].zName = sqliteStrDup(pEList->a[i].zName); |
danielk1977 | 517eb64 | 2004-06-07 10:00:31 +0000 | [diff] [blame] | 827 | }else if( p->op==TK_DOT |
drh | b733d03 | 2004-01-24 20:18:12 +0000 | [diff] [blame] | 828 | && (pR=p->pRight)!=0 && pR->token.z && pR->token.z[0] ){ |
| 829 | int cnt; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 830 | sqlite3SetNString(&aCol[i].zName, pR->token.z, pR->token.n, 0); |
drh | b733d03 | 2004-01-24 20:18:12 +0000 | [diff] [blame] | 831 | for(j=cnt=0; j<i; j++){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 832 | if( sqlite3StrICmp(aCol[j].zName, aCol[i].zName)==0 ){ |
drh | b733d03 | 2004-01-24 20:18:12 +0000 | [diff] [blame] | 833 | int n; |
| 834 | char zBuf[30]; |
| 835 | sprintf(zBuf,"_%d",++cnt); |
| 836 | n = strlen(zBuf); |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 837 | sqlite3SetNString(&aCol[i].zName, pR->token.z, pR->token.n, zBuf,n,0); |
drh | b733d03 | 2004-01-24 20:18:12 +0000 | [diff] [blame] | 838 | j = -1; |
| 839 | } |
| 840 | } |
| 841 | }else if( p->span.z && p->span.z[0] ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 842 | sqlite3SetNString(&pTab->aCol[i].zName, p->span.z, p->span.n, 0); |
drh | 22f70c3 | 2002-02-18 01:17:00 +0000 | [diff] [blame] | 843 | }else{ |
| 844 | char zBuf[30]; |
| 845 | sprintf(zBuf, "column%d", i+1); |
| 846 | pTab->aCol[i].zName = sqliteStrDup(zBuf); |
| 847 | } |
drh | 2c61c07 | 2004-07-20 00:20:23 +0000 | [diff] [blame^] | 848 | sqlite3Dequote(aCol[i].zName); |
danielk1977 | 517eb64 | 2004-06-07 10:00:31 +0000 | [diff] [blame] | 849 | |
| 850 | zType = sqliteStrDup(columnType(pParse, pSelect->pSrc ,p)); |
| 851 | pTab->aCol[i].zType = zType; |
| 852 | pTab->aCol[i].affinity = SQLITE_AFF_NUMERIC; |
| 853 | if( zType ){ |
| 854 | pTab->aCol[i].affinity = sqlite3AffinityType(zType, strlen(zType)); |
| 855 | } |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 856 | pTab->aCol[i].pColl = sqlite3ExprCollSeq(pParse, p); |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 857 | if( !pTab->aCol[i].pColl ){ |
| 858 | pTab->aCol[i].pColl = pParse->db->pDfltColl; |
| 859 | } |
drh | 22f70c3 | 2002-02-18 01:17:00 +0000 | [diff] [blame] | 860 | } |
| 861 | pTab->iPKey = -1; |
| 862 | return pTab; |
| 863 | } |
| 864 | |
| 865 | /* |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 866 | ** For the given SELECT statement, do three things. |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 867 | ** |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 868 | ** (1) Fill in the pTabList->a[].pTab fields in the SrcList that |
drh | 63eb5f2 | 2003-04-29 16:20:44 +0000 | [diff] [blame] | 869 | ** defines the set of tables that should be scanned. For views, |
| 870 | ** fill pTabList->a[].pSelect with a copy of the SELECT statement |
| 871 | ** that implements the view. A copy is made of the view's SELECT |
| 872 | ** statement so that we can freely modify or delete that statement |
| 873 | ** without worrying about messing up the presistent representation |
| 874 | ** of the view. |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 875 | ** |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 876 | ** (2) Add terms to the WHERE clause to accomodate the NATURAL keyword |
| 877 | ** on joins and the ON and USING clause of joins. |
| 878 | ** |
| 879 | ** (3) Scan the list of columns in the result set (pEList) looking |
drh | 5447322 | 2002-04-04 02:10:55 +0000 | [diff] [blame] | 880 | ** for instances of the "*" operator or the TABLE.* operator. |
| 881 | ** If found, expand each "*" to be every column in every table |
| 882 | ** and TABLE.* to be every column in TABLE. |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 883 | ** |
| 884 | ** Return 0 on success. If there are problems, leave an error message |
| 885 | ** in pParse and return non-zero. |
| 886 | */ |
| 887 | static int fillInColumnList(Parse *pParse, Select *p){ |
drh | 5447322 | 2002-04-04 02:10:55 +0000 | [diff] [blame] | 888 | int i, j, k, rc; |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 889 | SrcList *pTabList; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 890 | ExprList *pEList; |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 891 | Table *pTab; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 892 | |
| 893 | if( p==0 || p->pSrc==0 ) return 1; |
| 894 | pTabList = p->pSrc; |
| 895 | pEList = p->pEList; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 896 | |
| 897 | /* Look up every table in the table list. |
| 898 | */ |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 899 | for(i=0; i<pTabList->nSrc; i++){ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 900 | if( pTabList->a[i].pTab ){ |
| 901 | /* This routine has run before! No need to continue */ |
| 902 | return 0; |
| 903 | } |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 904 | if( pTabList->a[i].zName==0 ){ |
drh | 22f70c3 | 2002-02-18 01:17:00 +0000 | [diff] [blame] | 905 | /* A sub-query in the FROM clause of a SELECT */ |
drh | 22f70c3 | 2002-02-18 01:17:00 +0000 | [diff] [blame] | 906 | assert( pTabList->a[i].pSelect!=0 ); |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 907 | if( pTabList->a[i].zAlias==0 ){ |
| 908 | char zFakeName[60]; |
| 909 | sprintf(zFakeName, "sqlite_subquery_%p_", |
| 910 | (void*)pTabList->a[i].pSelect); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 911 | sqlite3SetString(&pTabList->a[i].zAlias, zFakeName, 0); |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 912 | } |
drh | 22f70c3 | 2002-02-18 01:17:00 +0000 | [diff] [blame] | 913 | pTabList->a[i].pTab = pTab = |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 914 | sqlite3ResultSetOfSelect(pParse, pTabList->a[i].zAlias, |
drh | 22f70c3 | 2002-02-18 01:17:00 +0000 | [diff] [blame] | 915 | pTabList->a[i].pSelect); |
| 916 | if( pTab==0 ){ |
| 917 | return 1; |
| 918 | } |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 919 | /* The isTransient flag indicates that the Table structure has been |
| 920 | ** dynamically allocated and may be freed at any time. In other words, |
| 921 | ** pTab is not pointing to a persistent table structure that defines |
| 922 | ** part of the schema. */ |
drh | 22f70c3 | 2002-02-18 01:17:00 +0000 | [diff] [blame] | 923 | pTab->isTransient = 1; |
| 924 | }else{ |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 925 | /* An ordinary table or view name in the FROM clause */ |
| 926 | pTabList->a[i].pTab = pTab = |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 927 | sqlite3LocateTable(pParse,pTabList->a[i].zName,pTabList->a[i].zDatabase); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 928 | if( pTab==0 ){ |
drh | 22f70c3 | 2002-02-18 01:17:00 +0000 | [diff] [blame] | 929 | return 1; |
| 930 | } |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 931 | if( pTab->pSelect ){ |
drh | 63eb5f2 | 2003-04-29 16:20:44 +0000 | [diff] [blame] | 932 | /* We reach here if the named table is a really a view */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 933 | if( sqlite3ViewGetColumnNames(pParse, pTab) ){ |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 934 | return 1; |
| 935 | } |
drh | 63eb5f2 | 2003-04-29 16:20:44 +0000 | [diff] [blame] | 936 | /* If pTabList->a[i].pSelect!=0 it means we are dealing with a |
| 937 | ** view within a view. The SELECT structure has already been |
| 938 | ** copied by the outer view so we can skip the copy step here |
| 939 | ** in the inner view. |
| 940 | */ |
| 941 | if( pTabList->a[i].pSelect==0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 942 | pTabList->a[i].pSelect = sqlite3SelectDup(pTab->pSelect); |
drh | 63eb5f2 | 2003-04-29 16:20:44 +0000 | [diff] [blame] | 943 | } |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 944 | } |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 945 | } |
| 946 | } |
| 947 | |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 948 | /* Process NATURAL keywords, and ON and USING clauses of joins. |
| 949 | */ |
| 950 | if( sqliteProcessJoin(pParse, p) ) return 1; |
| 951 | |
drh | 7c917d1 | 2001-12-16 20:05:05 +0000 | [diff] [blame] | 952 | /* For every "*" that occurs in the column list, insert the names of |
drh | 5447322 | 2002-04-04 02:10:55 +0000 | [diff] [blame] | 953 | ** all columns in all tables. And for every TABLE.* insert the names |
| 954 | ** of all columns in TABLE. The parser inserted a special expression |
drh | 7c917d1 | 2001-12-16 20:05:05 +0000 | [diff] [blame] | 955 | ** with the TK_ALL operator for each "*" that it found in the column list. |
| 956 | ** The following code just has to locate the TK_ALL expressions and expand |
| 957 | ** each one to the list of all columns in all tables. |
drh | 5447322 | 2002-04-04 02:10:55 +0000 | [diff] [blame] | 958 | ** |
| 959 | ** The first loop just checks to see if there are any "*" operators |
| 960 | ** that need expanding. |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 961 | */ |
drh | 7c917d1 | 2001-12-16 20:05:05 +0000 | [diff] [blame] | 962 | for(k=0; k<pEList->nExpr; k++){ |
drh | 5447322 | 2002-04-04 02:10:55 +0000 | [diff] [blame] | 963 | Expr *pE = pEList->a[k].pExpr; |
| 964 | if( pE->op==TK_ALL ) break; |
| 965 | if( pE->op==TK_DOT && pE->pRight && pE->pRight->op==TK_ALL |
| 966 | && pE->pLeft && pE->pLeft->op==TK_ID ) break; |
drh | 7c917d1 | 2001-12-16 20:05:05 +0000 | [diff] [blame] | 967 | } |
drh | 5447322 | 2002-04-04 02:10:55 +0000 | [diff] [blame] | 968 | rc = 0; |
drh | 7c917d1 | 2001-12-16 20:05:05 +0000 | [diff] [blame] | 969 | if( k<pEList->nExpr ){ |
drh | 5447322 | 2002-04-04 02:10:55 +0000 | [diff] [blame] | 970 | /* |
| 971 | ** If we get here it means the result set contains one or more "*" |
| 972 | ** operators that need to be expanded. Loop through each expression |
| 973 | ** in the result set and expand them one by one. |
| 974 | */ |
drh | 7c917d1 | 2001-12-16 20:05:05 +0000 | [diff] [blame] | 975 | struct ExprList_item *a = pEList->a; |
| 976 | ExprList *pNew = 0; |
| 977 | for(k=0; k<pEList->nExpr; k++){ |
drh | 5447322 | 2002-04-04 02:10:55 +0000 | [diff] [blame] | 978 | Expr *pE = a[k].pExpr; |
| 979 | if( pE->op!=TK_ALL && |
| 980 | (pE->op!=TK_DOT || pE->pRight==0 || pE->pRight->op!=TK_ALL) ){ |
| 981 | /* This particular expression does not need to be expanded. |
| 982 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 983 | pNew = sqlite3ExprListAppend(pNew, a[k].pExpr, 0); |
drh | 7c917d1 | 2001-12-16 20:05:05 +0000 | [diff] [blame] | 984 | pNew->a[pNew->nExpr-1].zName = a[k].zName; |
| 985 | a[k].pExpr = 0; |
| 986 | a[k].zName = 0; |
| 987 | }else{ |
drh | 5447322 | 2002-04-04 02:10:55 +0000 | [diff] [blame] | 988 | /* This expression is a "*" or a "TABLE.*" and needs to be |
| 989 | ** expanded. */ |
| 990 | int tableSeen = 0; /* Set to 1 when TABLE matches */ |
| 991 | Token *pName; /* text of name of TABLE */ |
| 992 | if( pE->op==TK_DOT && pE->pLeft ){ |
| 993 | pName = &pE->pLeft->token; |
| 994 | }else{ |
| 995 | pName = 0; |
| 996 | } |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 997 | for(i=0; i<pTabList->nSrc; i++){ |
drh | 7c917d1 | 2001-12-16 20:05:05 +0000 | [diff] [blame] | 998 | Table *pTab = pTabList->a[i].pTab; |
drh | 5447322 | 2002-04-04 02:10:55 +0000 | [diff] [blame] | 999 | char *zTabName = pTabList->a[i].zAlias; |
| 1000 | if( zTabName==0 || zTabName[0]==0 ){ |
| 1001 | zTabName = pTab->zName; |
| 1002 | } |
drh | c754fa5 | 2002-05-27 03:25:51 +0000 | [diff] [blame] | 1003 | if( pName && (zTabName==0 || zTabName[0]==0 || |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1004 | sqlite3StrNICmp(pName->z, zTabName, pName->n)!=0 || |
drh | c754fa5 | 2002-05-27 03:25:51 +0000 | [diff] [blame] | 1005 | zTabName[pName->n]!=0) ){ |
drh | 5447322 | 2002-04-04 02:10:55 +0000 | [diff] [blame] | 1006 | continue; |
| 1007 | } |
| 1008 | tableSeen = 1; |
drh | 7c917d1 | 2001-12-16 20:05:05 +0000 | [diff] [blame] | 1009 | for(j=0; j<pTab->nCol; j++){ |
drh | 22f70c3 | 2002-02-18 01:17:00 +0000 | [diff] [blame] | 1010 | Expr *pExpr, *pLeft, *pRight; |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 1011 | char *zName = pTab->aCol[j].zName; |
| 1012 | |
| 1013 | if( i>0 && (pTabList->a[i-1].jointype & JT_NATURAL)!=0 && |
| 1014 | columnIndex(pTabList->a[i-1].pTab, zName)>=0 ){ |
| 1015 | /* In a NATURAL join, omit the join columns from the |
| 1016 | ** table on the right */ |
| 1017 | continue; |
| 1018 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1019 | if( i>0 && sqlite3IdListIndex(pTabList->a[i-1].pUsing, zName)>=0 ){ |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 1020 | /* In a join with a USING clause, omit columns in the |
| 1021 | ** using clause from the table on the right. */ |
| 1022 | continue; |
| 1023 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1024 | pRight = sqlite3Expr(TK_ID, 0, 0, 0); |
drh | 22f70c3 | 2002-02-18 01:17:00 +0000 | [diff] [blame] | 1025 | if( pRight==0 ) break; |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 1026 | pRight->token.z = zName; |
| 1027 | pRight->token.n = strlen(zName); |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 1028 | pRight->token.dyn = 0; |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 1029 | if( zTabName && pTabList->nSrc>1 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1030 | pLeft = sqlite3Expr(TK_ID, 0, 0, 0); |
| 1031 | pExpr = sqlite3Expr(TK_DOT, pLeft, pRight, 0); |
drh | 22f70c3 | 2002-02-18 01:17:00 +0000 | [diff] [blame] | 1032 | if( pExpr==0 ) break; |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 1033 | pLeft->token.z = zTabName; |
| 1034 | pLeft->token.n = strlen(zTabName); |
| 1035 | pLeft->token.dyn = 0; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1036 | sqlite3SetString((char**)&pExpr->span.z, zTabName, ".", zName, 0); |
drh | 6977fea | 2002-10-22 23:38:04 +0000 | [diff] [blame] | 1037 | pExpr->span.n = strlen(pExpr->span.z); |
| 1038 | pExpr->span.dyn = 1; |
| 1039 | pExpr->token.z = 0; |
| 1040 | pExpr->token.n = 0; |
| 1041 | pExpr->token.dyn = 0; |
drh | 7c917d1 | 2001-12-16 20:05:05 +0000 | [diff] [blame] | 1042 | }else{ |
drh | 22f70c3 | 2002-02-18 01:17:00 +0000 | [diff] [blame] | 1043 | pExpr = pRight; |
drh | 6977fea | 2002-10-22 23:38:04 +0000 | [diff] [blame] | 1044 | pExpr->span = pExpr->token; |
drh | 7c917d1 | 2001-12-16 20:05:05 +0000 | [diff] [blame] | 1045 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1046 | pNew = sqlite3ExprListAppend(pNew, pExpr, 0); |
drh | 7c917d1 | 2001-12-16 20:05:05 +0000 | [diff] [blame] | 1047 | } |
drh | 17e24df | 2001-11-06 14:10:41 +0000 | [diff] [blame] | 1048 | } |
drh | 5447322 | 2002-04-04 02:10:55 +0000 | [diff] [blame] | 1049 | if( !tableSeen ){ |
drh | f5db2d3 | 2002-06-06 23:42:27 +0000 | [diff] [blame] | 1050 | if( pName ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1051 | sqlite3ErrorMsg(pParse, "no such table: %T", pName); |
drh | f5db2d3 | 2002-06-06 23:42:27 +0000 | [diff] [blame] | 1052 | }else{ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1053 | sqlite3ErrorMsg(pParse, "no tables specified"); |
drh | f5db2d3 | 2002-06-06 23:42:27 +0000 | [diff] [blame] | 1054 | } |
drh | 5447322 | 2002-04-04 02:10:55 +0000 | [diff] [blame] | 1055 | rc = 1; |
| 1056 | } |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1057 | } |
| 1058 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1059 | sqlite3ExprListDelete(pEList); |
drh | 7c917d1 | 2001-12-16 20:05:05 +0000 | [diff] [blame] | 1060 | p->pEList = pNew; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1061 | } |
drh | 5447322 | 2002-04-04 02:10:55 +0000 | [diff] [blame] | 1062 | return rc; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1063 | } |
| 1064 | |
| 1065 | /* |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 1066 | ** This routine recursively unlinks the Select.pSrc.a[].pTab pointers |
| 1067 | ** in a select structure. It just sets the pointers to NULL. This |
| 1068 | ** routine is recursive in the sense that if the Select.pSrc.a[].pSelect |
| 1069 | ** pointer is not NULL, this routine is called recursively on that pointer. |
| 1070 | ** |
| 1071 | ** This routine is called on the Select structure that defines a |
| 1072 | ** VIEW in order to undo any bindings to tables. This is necessary |
| 1073 | ** because those tables might be DROPed by a subsequent SQL command. |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 1074 | ** If the bindings are not removed, then the Select.pSrc->a[].pTab field |
| 1075 | ** will be left pointing to a deallocated Table structure after the |
| 1076 | ** DROP and a coredump will occur the next time the VIEW is used. |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 1077 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1078 | void sqlite3SelectUnbind(Select *p){ |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 1079 | int i; |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 1080 | SrcList *pSrc = p->pSrc; |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 1081 | Table *pTab; |
| 1082 | if( p==0 ) return; |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 1083 | for(i=0; i<pSrc->nSrc; i++){ |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 1084 | if( (pTab = pSrc->a[i].pTab)!=0 ){ |
| 1085 | if( pTab->isTransient ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1086 | sqlite3DeleteTable(0, pTab); |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 1087 | } |
| 1088 | pSrc->a[i].pTab = 0; |
| 1089 | if( pSrc->a[i].pSelect ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1090 | sqlite3SelectUnbind(pSrc->a[i].pSelect); |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 1091 | } |
| 1092 | } |
| 1093 | } |
| 1094 | } |
| 1095 | |
| 1096 | /* |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1097 | ** This routine associates entries in an ORDER BY expression list with |
| 1098 | ** columns in a result. For each ORDER BY expression, the opcode of |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 1099 | ** 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] | 1100 | ** the top-level node is filled in with column number and the iTable |
| 1101 | ** value of the top-level node is filled with iTable parameter. |
| 1102 | ** |
| 1103 | ** If there are prior SELECT clauses, they are processed first. A match |
| 1104 | ** in an earlier SELECT takes precedence over a later SELECT. |
| 1105 | ** |
| 1106 | ** Any entry that does not match is flagged as an error. The number |
| 1107 | ** of errors is returned. |
| 1108 | */ |
| 1109 | static int matchOrderbyToColumn( |
| 1110 | Parse *pParse, /* A place to leave error messages */ |
| 1111 | Select *pSelect, /* Match to result columns of this SELECT */ |
| 1112 | ExprList *pOrderBy, /* The ORDER BY values to match against columns */ |
drh | e4de1fe | 2002-06-02 16:09:01 +0000 | [diff] [blame] | 1113 | int iTable, /* Insert this value in iTable */ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1114 | int mustComplete /* If TRUE all ORDER BYs must match */ |
| 1115 | ){ |
| 1116 | int nErr = 0; |
| 1117 | int i, j; |
| 1118 | ExprList *pEList; |
| 1119 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1120 | if( pSelect==0 || pOrderBy==0 ) return 1; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1121 | if( mustComplete ){ |
| 1122 | for(i=0; i<pOrderBy->nExpr; i++){ pOrderBy->a[i].done = 0; } |
| 1123 | } |
| 1124 | if( fillInColumnList(pParse, pSelect) ){ |
| 1125 | return 1; |
| 1126 | } |
| 1127 | if( pSelect->pPrior ){ |
drh | 92cd52f | 2000-06-08 01:55:29 +0000 | [diff] [blame] | 1128 | if( matchOrderbyToColumn(pParse, pSelect->pPrior, pOrderBy, iTable, 0) ){ |
| 1129 | return 1; |
| 1130 | } |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1131 | } |
| 1132 | pEList = pSelect->pEList; |
| 1133 | for(i=0; i<pOrderBy->nExpr; i++){ |
| 1134 | Expr *pE = pOrderBy->a[i].pExpr; |
drh | e4de1fe | 2002-06-02 16:09:01 +0000 | [diff] [blame] | 1135 | int iCol = -1; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1136 | if( pOrderBy->a[i].done ) continue; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1137 | if( sqlite3ExprIsInteger(pE, &iCol) ){ |
drh | e4de1fe | 2002-06-02 16:09:01 +0000 | [diff] [blame] | 1138 | if( iCol<=0 || iCol>pEList->nExpr ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1139 | sqlite3ErrorMsg(pParse, |
drh | da93d23 | 2003-03-31 02:12:46 +0000 | [diff] [blame] | 1140 | "ORDER BY position %d should be between 1 and %d", |
| 1141 | iCol, pEList->nExpr); |
drh | e4de1fe | 2002-06-02 16:09:01 +0000 | [diff] [blame] | 1142 | nErr++; |
| 1143 | break; |
| 1144 | } |
drh | fcb78a4 | 2003-01-18 20:11:05 +0000 | [diff] [blame] | 1145 | if( !mustComplete ) continue; |
drh | e4de1fe | 2002-06-02 16:09:01 +0000 | [diff] [blame] | 1146 | iCol--; |
| 1147 | } |
| 1148 | for(j=0; iCol<0 && j<pEList->nExpr; j++){ |
drh | 4cfa793 | 2000-06-08 15:10:46 +0000 | [diff] [blame] | 1149 | if( pEList->a[j].zName && (pE->op==TK_ID || pE->op==TK_STRING) ){ |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 1150 | char *zName, *zLabel; |
| 1151 | zName = pEList->a[j].zName; |
drh | a99db3b | 2004-06-19 14:49:12 +0000 | [diff] [blame] | 1152 | zLabel = sqlite3NameFromToken(&pE->token); |
| 1153 | assert( zLabel!=0 ); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1154 | if( sqlite3StrICmp(zName, zLabel)==0 ){ |
drh | e4de1fe | 2002-06-02 16:09:01 +0000 | [diff] [blame] | 1155 | iCol = j; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1156 | } |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 1157 | sqliteFree(zLabel); |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1158 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1159 | if( iCol<0 && sqlite3ExprCompare(pE, pEList->a[j].pExpr) ){ |
drh | e4de1fe | 2002-06-02 16:09:01 +0000 | [diff] [blame] | 1160 | iCol = j; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1161 | } |
| 1162 | } |
drh | e4de1fe | 2002-06-02 16:09:01 +0000 | [diff] [blame] | 1163 | if( iCol>=0 ){ |
| 1164 | pE->op = TK_COLUMN; |
| 1165 | pE->iColumn = iCol; |
| 1166 | pE->iTable = iTable; |
| 1167 | pOrderBy->a[i].done = 1; |
| 1168 | } |
| 1169 | if( iCol<0 && mustComplete ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1170 | sqlite3ErrorMsg(pParse, |
drh | da93d23 | 2003-03-31 02:12:46 +0000 | [diff] [blame] | 1171 | "ORDER BY term number %d does not match any result column", i+1); |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1172 | nErr++; |
| 1173 | break; |
| 1174 | } |
| 1175 | } |
| 1176 | return nErr; |
| 1177 | } |
| 1178 | |
| 1179 | /* |
| 1180 | ** Get a VDBE for the given parser context. Create a new one if necessary. |
| 1181 | ** If an error occurs, return NULL and leave a message in pParse. |
| 1182 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1183 | Vdbe *sqlite3GetVdbe(Parse *pParse){ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1184 | Vdbe *v = pParse->pVdbe; |
| 1185 | if( v==0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1186 | v = pParse->pVdbe = sqlite3VdbeCreate(pParse->db); |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1187 | } |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1188 | return v; |
| 1189 | } |
drh | fcb78a4 | 2003-01-18 20:11:05 +0000 | [diff] [blame] | 1190 | |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 1191 | #if 0 /***** This routine needs deleting *****/ |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 1192 | static void multiSelectAffinity(Select *p, char *zAff){ |
| 1193 | int i; |
| 1194 | |
| 1195 | if( !p ) return; |
| 1196 | multiSelectAffinity(p->pPrior, zAff); |
| 1197 | |
| 1198 | for(i=0; i<p->pEList->nExpr; i++){ |
| 1199 | if( zAff[i]=='\0' ){ |
| 1200 | zAff[i] = sqlite3ExprAffinity(p->pEList->a[i].pExpr); |
| 1201 | } |
| 1202 | } |
| 1203 | } |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 1204 | #endif |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 1205 | |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1206 | /* |
drh | 7b58dae | 2003-07-20 01:16:46 +0000 | [diff] [blame] | 1207 | ** Compute the iLimit and iOffset fields of the SELECT based on the |
| 1208 | ** nLimit and nOffset fields. nLimit and nOffset hold the integers |
| 1209 | ** that appear in the original SQL statement after the LIMIT and OFFSET |
| 1210 | ** keywords. Or that hold -1 and 0 if those keywords are omitted. |
| 1211 | ** iLimit and iOffset are the integer memory register numbers for |
| 1212 | ** counters used to compute the limit and offset. If there is no |
| 1213 | ** limit and/or offset, then iLimit and iOffset are negative. |
| 1214 | ** |
| 1215 | ** This routine changes the values if iLimit and iOffset only if |
| 1216 | ** a limit or offset is defined by nLimit and nOffset. iLimit and |
| 1217 | ** iOffset should have been preset to appropriate default values |
| 1218 | ** (usually but not always -1) prior to calling this routine. |
| 1219 | ** Only if nLimit>=0 or nOffset>0 do the limit registers get |
| 1220 | ** redefined. The UNION ALL operator uses this property to force |
| 1221 | ** the reuse of the same limit and offset registers across multiple |
| 1222 | ** SELECT statements. |
| 1223 | */ |
| 1224 | static void computeLimitRegisters(Parse *pParse, Select *p){ |
| 1225 | /* |
| 1226 | ** If the comparison is p->nLimit>0 then "LIMIT 0" shows |
| 1227 | ** all rows. It is the same as no limit. If the comparision is |
| 1228 | ** p->nLimit>=0 then "LIMIT 0" show no rows at all. |
| 1229 | ** "LIMIT -1" always shows all rows. There is some |
| 1230 | ** contraversy about what the correct behavior should be. |
| 1231 | ** The current implementation interprets "LIMIT 0" to mean |
| 1232 | ** no rows. |
| 1233 | */ |
| 1234 | if( p->nLimit>=0 ){ |
| 1235 | int iMem = pParse->nMem++; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1236 | Vdbe *v = sqlite3GetVdbe(pParse); |
drh | 7b58dae | 2003-07-20 01:16:46 +0000 | [diff] [blame] | 1237 | if( v==0 ) return; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1238 | sqlite3VdbeAddOp(v, OP_Integer, -p->nLimit, 0); |
| 1239 | sqlite3VdbeAddOp(v, OP_MemStore, iMem, 1); |
drh | 7b58dae | 2003-07-20 01:16:46 +0000 | [diff] [blame] | 1240 | p->iLimit = iMem; |
| 1241 | } |
| 1242 | if( p->nOffset>0 ){ |
| 1243 | int iMem = pParse->nMem++; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1244 | Vdbe *v = sqlite3GetVdbe(pParse); |
drh | 7b58dae | 2003-07-20 01:16:46 +0000 | [diff] [blame] | 1245 | if( v==0 ) return; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1246 | sqlite3VdbeAddOp(v, OP_Integer, -p->nOffset, 0); |
| 1247 | sqlite3VdbeAddOp(v, OP_MemStore, iMem, 1); |
drh | 7b58dae | 2003-07-20 01:16:46 +0000 | [diff] [blame] | 1248 | p->iOffset = iMem; |
| 1249 | } |
| 1250 | } |
| 1251 | |
| 1252 | /* |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 1253 | ** Generate VDBE instructions that will open a transient table that |
| 1254 | ** will be used for an index or to store keyed results for a compound |
| 1255 | ** select. In other words, open a transient table that needs a |
| 1256 | ** KeyInfo structure. The number of columns in the KeyInfo is determined |
| 1257 | ** by the result set of the SELECT statement in the second argument. |
| 1258 | ** |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1259 | ** Specifically, this routine is called to open an index table for |
| 1260 | ** DISTINCT, UNION, INTERSECT and EXCEPT select statements (but not |
| 1261 | ** UNION ALL). |
| 1262 | ** |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 1263 | ** Make the new table a KeyAsData table if keyAsData is true. |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1264 | ** |
| 1265 | ** The value returned is the address of the OP_OpenTemp instruction. |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 1266 | */ |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1267 | static int openTempIndex(Parse *pParse, Select *p, int iTab, int keyAsData){ |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 1268 | KeyInfo *pKeyInfo; |
drh | 736c22b | 2004-05-21 02:14:24 +0000 | [diff] [blame] | 1269 | int nColumn; |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 1270 | sqlite *db = pParse->db; |
| 1271 | int i; |
| 1272 | Vdbe *v = pParse->pVdbe; |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1273 | int addr; |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 1274 | |
drh | 736c22b | 2004-05-21 02:14:24 +0000 | [diff] [blame] | 1275 | if( fillInColumnList(pParse, p) ){ |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1276 | return 0; |
drh | 736c22b | 2004-05-21 02:14:24 +0000 | [diff] [blame] | 1277 | } |
| 1278 | nColumn = p->pEList->nExpr; |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 1279 | pKeyInfo = sqliteMalloc( sizeof(*pKeyInfo)+nColumn*sizeof(CollSeq*) ); |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1280 | if( pKeyInfo==0 ) return 0; |
| 1281 | pKeyInfo->enc = pParse->db->enc; |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 1282 | pKeyInfo->nField = nColumn; |
| 1283 | for(i=0; i<nColumn; i++){ |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1284 | pKeyInfo->aColl[i] = sqlite3ExprCollSeq(pParse, p->pEList->a[i].pExpr); |
| 1285 | if( !pKeyInfo->aColl[i] ){ |
| 1286 | pKeyInfo->aColl[i] = db->pDfltColl; |
| 1287 | } |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 1288 | } |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1289 | addr = sqlite3VdbeOp3(v, OP_OpenTemp, iTab, 0, |
| 1290 | (char*)pKeyInfo, P3_KEYINFO_HANDOFF); |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 1291 | if( keyAsData ){ |
| 1292 | sqlite3VdbeAddOp(v, OP_KeyAsData, iTab, 1); |
| 1293 | } |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1294 | return addr; |
| 1295 | } |
| 1296 | |
| 1297 | static int multiSelectOpenTempAddr(Select *p, int addr, IdList **ppOpenTemp){ |
| 1298 | if( !p->ppOpenTemp ){ |
| 1299 | *ppOpenTemp = sqlite3IdListAppend(0, 0); |
| 1300 | p->ppOpenTemp = ppOpenTemp; |
| 1301 | }else{ |
| 1302 | *p->ppOpenTemp = sqlite3IdListAppend(*p->ppOpenTemp, 0); |
| 1303 | } |
| 1304 | if( !(*p->ppOpenTemp) ){ |
| 1305 | return SQLITE_NOMEM; |
| 1306 | } |
| 1307 | (*p->ppOpenTemp)->a[(*p->ppOpenTemp)->nId-1].idx = addr; |
| 1308 | return SQLITE_OK; |
| 1309 | } |
| 1310 | |
| 1311 | static CollSeq *multiSelectCollSeq(Parse *pParse, Select *p, int iCol){ |
| 1312 | CollSeq *pRet = 0; |
| 1313 | if( p->pPrior ){ |
| 1314 | pRet = multiSelectCollSeq(pParse, p->pPrior, iCol); |
| 1315 | } |
| 1316 | if( !pRet ){ |
| 1317 | pRet = sqlite3ExprCollSeq(pParse, p->pEList->a[iCol].pExpr); |
| 1318 | } |
| 1319 | return pRet; |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 1320 | } |
| 1321 | |
| 1322 | /* |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 1323 | ** This routine is called to process a query that is really the union |
| 1324 | ** or intersection of two or more separate queries. |
drh | c926afb | 2002-06-20 03:38:26 +0000 | [diff] [blame] | 1325 | ** |
drh | e78e828 | 2003-01-19 03:59:45 +0000 | [diff] [blame] | 1326 | ** "p" points to the right-most of the two queries. the query on the |
| 1327 | ** left is p->pPrior. The left query could also be a compound query |
| 1328 | ** in which case this routine will be called recursively. |
| 1329 | ** |
| 1330 | ** The results of the total query are to be written into a destination |
| 1331 | ** of type eDest with parameter iParm. |
| 1332 | ** |
| 1333 | ** Example 1: Consider a three-way compound SQL statement. |
| 1334 | ** |
| 1335 | ** SELECT a FROM t1 UNION SELECT b FROM t2 UNION SELECT c FROM t3 |
| 1336 | ** |
| 1337 | ** This statement is parsed up as follows: |
| 1338 | ** |
| 1339 | ** SELECT c FROM t3 |
| 1340 | ** | |
| 1341 | ** `-----> SELECT b FROM t2 |
| 1342 | ** | |
jplyon | 4b11c6d | 2004-01-19 04:57:53 +0000 | [diff] [blame] | 1343 | ** `------> SELECT a FROM t1 |
drh | e78e828 | 2003-01-19 03:59:45 +0000 | [diff] [blame] | 1344 | ** |
| 1345 | ** The arrows in the diagram above represent the Select.pPrior pointer. |
| 1346 | ** So if this routine is called with p equal to the t3 query, then |
| 1347 | ** pPrior will be the t2 query. p->op will be TK_UNION in this case. |
| 1348 | ** |
| 1349 | ** Notice that because of the way SQLite parses compound SELECTs, the |
| 1350 | ** individual selects always group from left to right. |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 1351 | */ |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 1352 | static int multiSelect( |
| 1353 | Parse *pParse, |
| 1354 | Select *p, |
| 1355 | int eDest, |
| 1356 | int iParm, |
| 1357 | char *aff /* If eDest is SRT_Union, the affinity string */ |
| 1358 | ){ |
| 1359 | int rc = SQLITE_OK; /* Success code from a subroutine */ |
drh | 10e5e3c | 2000-06-08 00:19:02 +0000 | [diff] [blame] | 1360 | Select *pPrior; /* Another SELECT immediately to our left */ |
| 1361 | Vdbe *v; /* Generate code to this VDBE */ |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1362 | IdList *pOpenTemp = 0; |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 1363 | |
drh | 7b58dae | 2003-07-20 01:16:46 +0000 | [diff] [blame] | 1364 | /* Make sure there is no ORDER BY or LIMIT clause on prior SELECTs. Only |
| 1365 | ** the last SELECT in the series may have an ORDER BY or LIMIT. |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 1366 | */ |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 1367 | if( p==0 || p->pPrior==0 ){ |
| 1368 | rc = 1; |
| 1369 | goto multi_select_end; |
| 1370 | } |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1371 | pPrior = p->pPrior; |
| 1372 | if( pPrior->pOrderBy ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1373 | sqlite3ErrorMsg(pParse,"ORDER BY clause should come after %s not before", |
drh | da93d23 | 2003-03-31 02:12:46 +0000 | [diff] [blame] | 1374 | selectOpName(p->op)); |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 1375 | rc = 1; |
| 1376 | goto multi_select_end; |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 1377 | } |
drh | 7b58dae | 2003-07-20 01:16:46 +0000 | [diff] [blame] | 1378 | if( pPrior->nLimit>=0 || pPrior->nOffset>0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1379 | sqlite3ErrorMsg(pParse,"LIMIT clause should come after %s not before", |
drh | 7b58dae | 2003-07-20 01:16:46 +0000 | [diff] [blame] | 1380 | selectOpName(p->op)); |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 1381 | rc = 1; |
| 1382 | goto multi_select_end; |
drh | 7b58dae | 2003-07-20 01:16:46 +0000 | [diff] [blame] | 1383 | } |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 1384 | |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1385 | /* Make sure we have a valid query engine. If not, create a new one. |
| 1386 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1387 | v = sqlite3GetVdbe(pParse); |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 1388 | if( v==0 ){ |
| 1389 | rc = 1; |
| 1390 | goto multi_select_end; |
| 1391 | } |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1392 | |
drh | 1cc3d75 | 2002-03-23 00:31:29 +0000 | [diff] [blame] | 1393 | /* Create the destination temporary table if necessary |
| 1394 | */ |
| 1395 | if( eDest==SRT_TempTable ){ |
danielk1977 | b4964b7 | 2004-05-18 01:23:38 +0000 | [diff] [blame] | 1396 | assert( p->pEList ); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1397 | sqlite3VdbeAddOp(v, OP_OpenTemp, iParm, 0); |
danielk1977 | b4964b7 | 2004-05-18 01:23:38 +0000 | [diff] [blame] | 1398 | sqlite3VdbeAddOp(v, OP_SetNumColumns, iParm, p->pEList->nExpr); |
drh | 1cc3d75 | 2002-03-23 00:31:29 +0000 | [diff] [blame] | 1399 | eDest = SRT_Table; |
| 1400 | } |
| 1401 | |
drh | f46f905 | 2002-06-22 02:33:38 +0000 | [diff] [blame] | 1402 | /* Generate code for the left and right SELECT statements. |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1403 | */ |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 1404 | switch( p->op ){ |
drh | f46f905 | 2002-06-22 02:33:38 +0000 | [diff] [blame] | 1405 | case TK_ALL: { |
| 1406 | if( p->pOrderBy==0 ){ |
drh | 7b58dae | 2003-07-20 01:16:46 +0000 | [diff] [blame] | 1407 | pPrior->nLimit = p->nLimit; |
| 1408 | pPrior->nOffset = p->nOffset; |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1409 | pPrior->ppOpenTemp = p->ppOpenTemp; |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 1410 | rc = sqlite3Select(pParse, pPrior, eDest, iParm, 0, 0, 0, aff); |
| 1411 | if( rc ){ |
| 1412 | goto multi_select_end; |
| 1413 | } |
drh | f46f905 | 2002-06-22 02:33:38 +0000 | [diff] [blame] | 1414 | p->pPrior = 0; |
drh | 7b58dae | 2003-07-20 01:16:46 +0000 | [diff] [blame] | 1415 | p->iLimit = pPrior->iLimit; |
| 1416 | p->iOffset = pPrior->iOffset; |
| 1417 | p->nLimit = -1; |
| 1418 | p->nOffset = 0; |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 1419 | rc = sqlite3Select(pParse, p, eDest, iParm, 0, 0, 0, aff); |
drh | f46f905 | 2002-06-22 02:33:38 +0000 | [diff] [blame] | 1420 | p->pPrior = pPrior; |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 1421 | if( rc ){ |
| 1422 | goto multi_select_end; |
| 1423 | } |
drh | f46f905 | 2002-06-22 02:33:38 +0000 | [diff] [blame] | 1424 | break; |
| 1425 | } |
| 1426 | /* For UNION ALL ... ORDER BY fall through to the next case */ |
| 1427 | } |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 1428 | case TK_EXCEPT: |
| 1429 | case TK_UNION: { |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1430 | int unionTab; /* Cursor number of the temporary table holding result */ |
danielk1977 | 742f947 | 2004-06-16 12:02:43 +0000 | [diff] [blame] | 1431 | int op = 0; /* One of the SRT_ operations to apply to self */ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1432 | int priorOp; /* The SRT_ operation to apply to prior selects */ |
drh | 7b58dae | 2003-07-20 01:16:46 +0000 | [diff] [blame] | 1433 | int nLimit, nOffset; /* Saved values of p->nLimit and p->nOffset */ |
drh | c926afb | 2002-06-20 03:38:26 +0000 | [diff] [blame] | 1434 | ExprList *pOrderBy; /* The ORDER BY clause for the right SELECT */ |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1435 | int addr; |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 1436 | |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1437 | priorOp = p->op==TK_ALL ? SRT_Table : SRT_Union; |
drh | 7b58dae | 2003-07-20 01:16:46 +0000 | [diff] [blame] | 1438 | if( eDest==priorOp && p->pOrderBy==0 && p->nLimit<0 && p->nOffset==0 ){ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1439 | /* We can reuse a temporary table generated by a SELECT to our |
drh | c926afb | 2002-06-20 03:38:26 +0000 | [diff] [blame] | 1440 | ** right. |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1441 | */ |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 1442 | unionTab = iParm; |
| 1443 | }else{ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1444 | /* We will need to create our own temporary table to hold the |
| 1445 | ** intermediate results. |
| 1446 | */ |
| 1447 | unionTab = pParse->nTab++; |
| 1448 | if( p->pOrderBy |
| 1449 | && matchOrderbyToColumn(pParse, p, p->pOrderBy, unionTab, 1) ){ |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 1450 | rc = 1; |
| 1451 | goto multi_select_end; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1452 | } |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1453 | addr = sqlite3VdbeAddOp(v, OP_OpenTemp, unionTab, 0); |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1454 | if( p->op!=TK_ALL ){ |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1455 | rc = multiSelectOpenTempAddr(p, addr, &pOpenTemp); |
| 1456 | if( rc!=SQLITE_OK ){ |
| 1457 | goto multi_select_end; |
| 1458 | } |
| 1459 | sqlite3VdbeAddOp(v, OP_KeyAsData, unionTab, 1); |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1460 | } |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 1461 | assert( p->pEList ); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 1462 | } |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1463 | |
| 1464 | /* Code the SELECT statements to our left |
| 1465 | */ |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1466 | pPrior->ppOpenTemp = p->ppOpenTemp; |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 1467 | rc = sqlite3Select(pParse, pPrior, priorOp, unionTab, 0, 0, 0, aff); |
| 1468 | if( rc ){ |
| 1469 | goto multi_select_end; |
| 1470 | } |
| 1471 | if( p->op==TK_ALL ){ |
| 1472 | sqlite3VdbeAddOp(v, OP_SetNumColumns, unionTab, pPrior->pEList->nExpr); |
| 1473 | } |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1474 | |
| 1475 | /* Code the current SELECT statement |
| 1476 | */ |
| 1477 | switch( p->op ){ |
| 1478 | case TK_EXCEPT: op = SRT_Except; break; |
| 1479 | case TK_UNION: op = SRT_Union; break; |
| 1480 | case TK_ALL: op = SRT_Table; break; |
| 1481 | } |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 1482 | p->pPrior = 0; |
drh | c926afb | 2002-06-20 03:38:26 +0000 | [diff] [blame] | 1483 | pOrderBy = p->pOrderBy; |
| 1484 | p->pOrderBy = 0; |
drh | 7b58dae | 2003-07-20 01:16:46 +0000 | [diff] [blame] | 1485 | nLimit = p->nLimit; |
| 1486 | p->nLimit = -1; |
| 1487 | nOffset = p->nOffset; |
| 1488 | p->nOffset = 0; |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 1489 | rc = sqlite3Select(pParse, p, op, unionTab, 0, 0, 0, aff); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 1490 | p->pPrior = pPrior; |
drh | c926afb | 2002-06-20 03:38:26 +0000 | [diff] [blame] | 1491 | p->pOrderBy = pOrderBy; |
drh | 7b58dae | 2003-07-20 01:16:46 +0000 | [diff] [blame] | 1492 | p->nLimit = nLimit; |
| 1493 | p->nOffset = nOffset; |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 1494 | if( rc ){ |
| 1495 | goto multi_select_end; |
| 1496 | } |
| 1497 | |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1498 | |
| 1499 | /* Convert the data in the temporary table into whatever form |
| 1500 | ** it is that we currently need. |
| 1501 | */ |
drh | c926afb | 2002-06-20 03:38:26 +0000 | [diff] [blame] | 1502 | if( eDest!=priorOp || unionTab!=iParm ){ |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 1503 | int iCont, iBreak, iStart; |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 1504 | assert( p->pEList ); |
drh | 41202cc | 2002-04-23 17:10:18 +0000 | [diff] [blame] | 1505 | if( eDest==SRT_Callback ){ |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 1506 | generateColumnNames(pParse, 0, p->pEList); |
drh | 41202cc | 2002-04-23 17:10:18 +0000 | [diff] [blame] | 1507 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1508 | iBreak = sqlite3VdbeMakeLabel(v); |
| 1509 | iCont = sqlite3VdbeMakeLabel(v); |
| 1510 | sqlite3VdbeAddOp(v, OP_Rewind, unionTab, iBreak); |
drh | 7b58dae | 2003-07-20 01:16:46 +0000 | [diff] [blame] | 1511 | computeLimitRegisters(pParse, p); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1512 | iStart = sqlite3VdbeCurrentAddr(v); |
drh | 38640e1 | 2002-07-05 21:42:36 +0000 | [diff] [blame] | 1513 | rc = selectInnerLoop(pParse, p, p->pEList, unionTab, p->pEList->nExpr, |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1514 | p->pOrderBy, -1, eDest, iParm, |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 1515 | iCont, iBreak, 0); |
| 1516 | if( rc ){ |
| 1517 | rc = 1; |
| 1518 | goto multi_select_end; |
| 1519 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1520 | sqlite3VdbeResolveLabel(v, iCont); |
| 1521 | sqlite3VdbeAddOp(v, OP_Next, unionTab, iStart); |
| 1522 | sqlite3VdbeResolveLabel(v, iBreak); |
| 1523 | sqlite3VdbeAddOp(v, OP_Close, unionTab, 0); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 1524 | } |
| 1525 | break; |
| 1526 | } |
| 1527 | case TK_INTERSECT: { |
| 1528 | int tab1, tab2; |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 1529 | int iCont, iBreak, iStart; |
drh | 7b58dae | 2003-07-20 01:16:46 +0000 | [diff] [blame] | 1530 | int nLimit, nOffset; |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1531 | int addr; |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 1532 | |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1533 | /* INTERSECT is different from the others since it requires |
drh | 6206d50 | 2000-06-19 19:09:08 +0000 | [diff] [blame] | 1534 | ** two temporary tables. Hence it has its own case. Begin |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1535 | ** by allocating the tables we will need. |
| 1536 | */ |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 1537 | tab1 = pParse->nTab++; |
| 1538 | tab2 = pParse->nTab++; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1539 | if( p->pOrderBy && matchOrderbyToColumn(pParse,p,p->pOrderBy,tab1,1) ){ |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 1540 | rc = 1; |
| 1541 | goto multi_select_end; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1542 | } |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1543 | |
| 1544 | addr = sqlite3VdbeAddOp(v, OP_OpenTemp, tab1, 0); |
| 1545 | rc = multiSelectOpenTempAddr(p, addr, &pOpenTemp); |
| 1546 | if( rc!=SQLITE_OK ){ |
| 1547 | goto multi_select_end; |
| 1548 | } |
| 1549 | sqlite3VdbeAddOp(v, OP_KeyAsData, tab1, 1); |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 1550 | assert( p->pEList ); |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1551 | |
| 1552 | /* Code the SELECTs to our left into temporary table "tab1". |
| 1553 | */ |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1554 | pPrior->ppOpenTemp = p->ppOpenTemp; |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 1555 | rc = sqlite3Select(pParse, pPrior, SRT_Union, tab1, 0, 0, 0, aff); |
| 1556 | if( rc ){ |
| 1557 | goto multi_select_end; |
| 1558 | } |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1559 | |
| 1560 | /* Code the current SELECT into temporary table "tab2" |
| 1561 | */ |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1562 | addr = sqlite3VdbeAddOp(v, OP_OpenTemp, tab2, 0); |
| 1563 | rc = multiSelectOpenTempAddr(p, addr, &pOpenTemp); |
| 1564 | if( rc!=SQLITE_OK ){ |
| 1565 | goto multi_select_end; |
| 1566 | } |
| 1567 | sqlite3VdbeAddOp(v, OP_KeyAsData, tab2, 1); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 1568 | p->pPrior = 0; |
drh | 7b58dae | 2003-07-20 01:16:46 +0000 | [diff] [blame] | 1569 | nLimit = p->nLimit; |
| 1570 | p->nLimit = -1; |
| 1571 | nOffset = p->nOffset; |
| 1572 | p->nOffset = 0; |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 1573 | rc = sqlite3Select(pParse, p, SRT_Union, tab2, 0, 0, 0, aff); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 1574 | p->pPrior = pPrior; |
drh | 7b58dae | 2003-07-20 01:16:46 +0000 | [diff] [blame] | 1575 | p->nLimit = nLimit; |
| 1576 | p->nOffset = nOffset; |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 1577 | if( rc ){ |
| 1578 | goto multi_select_end; |
| 1579 | } |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1580 | |
| 1581 | /* Generate code to take the intersection of the two temporary |
| 1582 | ** tables. |
| 1583 | */ |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 1584 | assert( p->pEList ); |
drh | 41202cc | 2002-04-23 17:10:18 +0000 | [diff] [blame] | 1585 | if( eDest==SRT_Callback ){ |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 1586 | generateColumnNames(pParse, 0, p->pEList); |
drh | 41202cc | 2002-04-23 17:10:18 +0000 | [diff] [blame] | 1587 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1588 | iBreak = sqlite3VdbeMakeLabel(v); |
| 1589 | iCont = sqlite3VdbeMakeLabel(v); |
| 1590 | sqlite3VdbeAddOp(v, OP_Rewind, tab1, iBreak); |
drh | 7b58dae | 2003-07-20 01:16:46 +0000 | [diff] [blame] | 1591 | computeLimitRegisters(pParse, p); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1592 | iStart = sqlite3VdbeAddOp(v, OP_FullKey, tab1, 0); |
| 1593 | sqlite3VdbeAddOp(v, OP_NotFound, tab2, iCont); |
drh | 38640e1 | 2002-07-05 21:42:36 +0000 | [diff] [blame] | 1594 | rc = selectInnerLoop(pParse, p, p->pEList, tab1, p->pEList->nExpr, |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1595 | p->pOrderBy, -1, eDest, iParm, |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 1596 | iCont, iBreak, 0); |
| 1597 | if( rc ){ |
| 1598 | rc = 1; |
| 1599 | goto multi_select_end; |
| 1600 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1601 | sqlite3VdbeResolveLabel(v, iCont); |
| 1602 | sqlite3VdbeAddOp(v, OP_Next, tab1, iStart); |
| 1603 | sqlite3VdbeResolveLabel(v, iBreak); |
| 1604 | sqlite3VdbeAddOp(v, OP_Close, tab2, 0); |
| 1605 | sqlite3VdbeAddOp(v, OP_Close, tab1, 0); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 1606 | break; |
| 1607 | } |
| 1608 | } |
| 1609 | assert( p->pEList && pPrior->pEList ); |
| 1610 | if( p->pEList->nExpr!=pPrior->pEList->nExpr ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1611 | sqlite3ErrorMsg(pParse, "SELECTs to the left and right of %s" |
drh | da93d23 | 2003-03-31 02:12:46 +0000 | [diff] [blame] | 1612 | " do not have the same number of result columns", selectOpName(p->op)); |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 1613 | rc = 1; |
| 1614 | goto multi_select_end; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1615 | } |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 1616 | |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1617 | if( p->pOrderBy || (pOpenTemp && pOpenTemp->nId>0) ){ |
| 1618 | int nCol = p->pEList->nExpr; |
| 1619 | int i; |
| 1620 | KeyInfo *pKeyInfo = sqliteMalloc(sizeof(*pKeyInfo)+nCol*sizeof(CollSeq*)); |
| 1621 | if( !pKeyInfo ){ |
| 1622 | rc = SQLITE_NOMEM; |
| 1623 | goto multi_select_end; |
| 1624 | } |
| 1625 | |
| 1626 | pKeyInfo->enc = pParse->db->enc; |
| 1627 | pKeyInfo->nField = nCol; |
| 1628 | |
| 1629 | for(i=0; i<nCol; i++){ |
| 1630 | pKeyInfo->aColl[i] = multiSelectCollSeq(pParse, p, i); |
| 1631 | if( !pKeyInfo->aColl[i] ){ |
| 1632 | pKeyInfo->aColl[i] = pParse->db->pDfltColl; |
| 1633 | } |
| 1634 | } |
| 1635 | |
| 1636 | for(i=0; pOpenTemp && i<pOpenTemp->nId; i++){ |
| 1637 | int p3type = (i==0?P3_KEYINFO_HANDOFF:P3_KEYINFO); |
| 1638 | int addr = pOpenTemp->a[i].idx; |
| 1639 | sqlite3VdbeChangeP3(v, addr, (char *)pKeyInfo, p3type); |
| 1640 | } |
| 1641 | |
| 1642 | if( p->pOrderBy ){ |
| 1643 | for(i=0; i<p->pOrderBy->nExpr; i++){ |
| 1644 | Expr *pExpr = p->pOrderBy->a[i].pExpr; |
| 1645 | char *zName = p->pOrderBy->a[i].zName; |
| 1646 | assert( pExpr->op==TK_COLUMN && pExpr->iColumn<nCol ); |
| 1647 | assert( !pExpr->pColl ); |
| 1648 | if( zName ){ |
| 1649 | pExpr->pColl = sqlite3LocateCollSeq(pParse, zName, -1); |
| 1650 | }else{ |
| 1651 | pExpr->pColl = pKeyInfo->aColl[pExpr->iColumn]; |
| 1652 | } |
| 1653 | } |
| 1654 | generateSortTail(pParse, p, v, p->pEList->nExpr, eDest, iParm); |
| 1655 | } |
| 1656 | |
| 1657 | if( !pOpenTemp ){ |
| 1658 | /* This happens for UNION ALL ... ORDER BY */ |
| 1659 | sqliteFree(pKeyInfo); |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 1660 | } |
| 1661 | } |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 1662 | |
| 1663 | multi_select_end: |
| 1664 | if( pOpenTemp ){ |
| 1665 | sqlite3IdListDelete(pOpenTemp); |
| 1666 | } |
| 1667 | p->ppOpenTemp = 0; |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 1668 | return rc; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1669 | } |
| 1670 | |
| 1671 | /* |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1672 | ** Scan through the expression pExpr. Replace every reference to |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 1673 | ** a column in table number iTable with a copy of the iColumn-th |
drh | 84e5920 | 2002-03-14 14:33:31 +0000 | [diff] [blame] | 1674 | ** entry in pEList. (But leave references to the ROWID column |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 1675 | ** unchanged.) |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1676 | ** |
| 1677 | ** This routine is part of the flattening procedure. A subquery |
| 1678 | ** whose result set is defined by pEList appears as entry in the |
| 1679 | ** FROM clause of a SELECT such that the VDBE cursor assigned to that |
| 1680 | ** FORM clause entry is iTable. This routine make the necessary |
| 1681 | ** changes to pExpr so that it refers directly to the source table |
| 1682 | ** of the subquery rather the result set of the subquery. |
| 1683 | */ |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 1684 | static void substExprList(ExprList*,int,ExprList*); /* Forward Decl */ |
| 1685 | static void substExpr(Expr *pExpr, int iTable, ExprList *pEList){ |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1686 | if( pExpr==0 ) return; |
drh | 50350a1 | 2004-02-13 16:22:22 +0000 | [diff] [blame] | 1687 | if( pExpr->op==TK_COLUMN && pExpr->iTable==iTable ){ |
| 1688 | if( pExpr->iColumn<0 ){ |
| 1689 | pExpr->op = TK_NULL; |
| 1690 | }else{ |
| 1691 | Expr *pNew; |
| 1692 | assert( pEList!=0 && pExpr->iColumn<pEList->nExpr ); |
| 1693 | assert( pExpr->pLeft==0 && pExpr->pRight==0 && pExpr->pList==0 ); |
| 1694 | pNew = pEList->a[pExpr->iColumn].pExpr; |
| 1695 | assert( pNew!=0 ); |
| 1696 | pExpr->op = pNew->op; |
drh | 50350a1 | 2004-02-13 16:22:22 +0000 | [diff] [blame] | 1697 | assert( pExpr->pLeft==0 ); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1698 | pExpr->pLeft = sqlite3ExprDup(pNew->pLeft); |
drh | 50350a1 | 2004-02-13 16:22:22 +0000 | [diff] [blame] | 1699 | assert( pExpr->pRight==0 ); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1700 | pExpr->pRight = sqlite3ExprDup(pNew->pRight); |
drh | 50350a1 | 2004-02-13 16:22:22 +0000 | [diff] [blame] | 1701 | assert( pExpr->pList==0 ); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1702 | pExpr->pList = sqlite3ExprListDup(pNew->pList); |
drh | 50350a1 | 2004-02-13 16:22:22 +0000 | [diff] [blame] | 1703 | pExpr->iTable = pNew->iTable; |
| 1704 | pExpr->iColumn = pNew->iColumn; |
| 1705 | pExpr->iAgg = pNew->iAgg; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1706 | sqlite3TokenCopy(&pExpr->token, &pNew->token); |
| 1707 | sqlite3TokenCopy(&pExpr->span, &pNew->span); |
drh | 50350a1 | 2004-02-13 16:22:22 +0000 | [diff] [blame] | 1708 | } |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1709 | }else{ |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 1710 | substExpr(pExpr->pLeft, iTable, pEList); |
| 1711 | substExpr(pExpr->pRight, iTable, pEList); |
| 1712 | substExprList(pExpr->pList, iTable, pEList); |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1713 | } |
| 1714 | } |
| 1715 | static void |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 1716 | substExprList(ExprList *pList, int iTable, ExprList *pEList){ |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1717 | int i; |
| 1718 | if( pList==0 ) return; |
| 1719 | for(i=0; i<pList->nExpr; i++){ |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 1720 | substExpr(pList->a[i].pExpr, iTable, pEList); |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1721 | } |
| 1722 | } |
| 1723 | |
| 1724 | /* |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 1725 | ** This routine attempts to flatten subqueries in order to speed |
| 1726 | ** execution. It returns 1 if it makes changes and 0 if no flattening |
| 1727 | ** occurs. |
| 1728 | ** |
| 1729 | ** To understand the concept of flattening, consider the following |
| 1730 | ** query: |
| 1731 | ** |
| 1732 | ** SELECT a FROM (SELECT x+y AS a FROM t1 WHERE z<100) WHERE a>5 |
| 1733 | ** |
| 1734 | ** The default way of implementing this query is to execute the |
| 1735 | ** subquery first and store the results in a temporary table, then |
| 1736 | ** run the outer query on that temporary table. This requires two |
| 1737 | ** passes over the data. Furthermore, because the temporary table |
| 1738 | ** has no indices, the WHERE clause on the outer query cannot be |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1739 | ** optimized. |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 1740 | ** |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1741 | ** This routine attempts to rewrite queries such as the above into |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 1742 | ** a single flat select, like this: |
| 1743 | ** |
| 1744 | ** SELECT x+y AS a FROM t1 WHERE z<100 AND a>5 |
| 1745 | ** |
| 1746 | ** The code generated for this simpification gives the same result |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1747 | ** but only has to scan the data once. And because indices might |
| 1748 | ** exist on the table t1, a complete scan of the data might be |
| 1749 | ** avoided. |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 1750 | ** |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1751 | ** Flattening is only attempted if all of the following are true: |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 1752 | ** |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1753 | ** (1) The subquery and the outer query do not both use aggregates. |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 1754 | ** |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1755 | ** (2) The subquery is not an aggregate or the outer query is not a join. |
| 1756 | ** |
drh | 8af4d3a | 2003-05-06 20:35:16 +0000 | [diff] [blame] | 1757 | ** (3) The subquery is not the right operand of a left outer join, or |
| 1758 | ** the subquery is not itself a join. (Ticket #306) |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1759 | ** |
| 1760 | ** (4) The subquery is not DISTINCT or the outer query is not a join. |
| 1761 | ** |
| 1762 | ** (5) The subquery is not DISTINCT or the outer query does not use |
| 1763 | ** aggregates. |
| 1764 | ** |
| 1765 | ** (6) The subquery does not use aggregates or the outer query is not |
| 1766 | ** DISTINCT. |
| 1767 | ** |
drh | 08192d5 | 2002-04-30 19:20:28 +0000 | [diff] [blame] | 1768 | ** (7) The subquery has a FROM clause. |
| 1769 | ** |
drh | df199a2 | 2002-06-14 22:38:41 +0000 | [diff] [blame] | 1770 | ** (8) The subquery does not use LIMIT or the outer query is not a join. |
| 1771 | ** |
| 1772 | ** (9) The subquery does not use LIMIT or the outer query does not use |
| 1773 | ** aggregates. |
| 1774 | ** |
| 1775 | ** (10) The subquery does not use aggregates or the outer query does not |
| 1776 | ** use LIMIT. |
| 1777 | ** |
drh | 174b619 | 2002-12-03 02:22:52 +0000 | [diff] [blame] | 1778 | ** (11) The subquery and the outer query do not both have ORDER BY clauses. |
| 1779 | ** |
drh | 3fc673e | 2003-06-16 00:40:34 +0000 | [diff] [blame] | 1780 | ** (12) The subquery is not the right term of a LEFT OUTER JOIN or the |
| 1781 | ** subquery has no WHERE clause. (added by ticket #350) |
| 1782 | ** |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1783 | ** In this routine, the "p" parameter is a pointer to the outer query. |
| 1784 | ** The subquery is p->pSrc->a[iFrom]. isAgg is true if the outer query |
| 1785 | ** uses aggregates and subqueryIsAgg is true if the subquery uses aggregates. |
| 1786 | ** |
drh | 665de47 | 2003-03-31 13:36:09 +0000 | [diff] [blame] | 1787 | ** 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] | 1788 | ** If flattening is attempted this routine returns 1. |
| 1789 | ** |
| 1790 | ** All of the expression analysis must occur on both the outer query and |
| 1791 | ** the subquery before this routine runs. |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 1792 | */ |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 1793 | static int flattenSubquery( |
| 1794 | Parse *pParse, /* The parsing context */ |
| 1795 | Select *p, /* The parent or outer SELECT statement */ |
| 1796 | int iFrom, /* Index in p->pSrc->a[] of the inner subquery */ |
| 1797 | int isAgg, /* True if outer SELECT uses aggregate functions */ |
| 1798 | int subqueryIsAgg /* True if the subquery uses aggregate functions */ |
| 1799 | ){ |
drh | 0bb2810 | 2002-05-08 11:54:14 +0000 | [diff] [blame] | 1800 | Select *pSub; /* The inner query or "subquery" */ |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 1801 | SrcList *pSrc; /* The FROM clause of the outer query */ |
| 1802 | SrcList *pSubSrc; /* The FROM clause of the subquery */ |
drh | 0bb2810 | 2002-05-08 11:54:14 +0000 | [diff] [blame] | 1803 | ExprList *pList; /* The result set of the outer query */ |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 1804 | int iParent; /* VDBE cursor number of the pSub result set temp table */ |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1805 | int i; |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1806 | Expr *pWhere; |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 1807 | |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1808 | /* Check to see if flattening is permitted. Return 0 if not. |
| 1809 | */ |
| 1810 | if( p==0 ) return 0; |
| 1811 | pSrc = p->pSrc; |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 1812 | assert( pSrc && iFrom>=0 && iFrom<pSrc->nSrc ); |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1813 | pSub = pSrc->a[iFrom].pSelect; |
| 1814 | assert( pSub!=0 ); |
| 1815 | if( isAgg && subqueryIsAgg ) return 0; |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 1816 | if( subqueryIsAgg && pSrc->nSrc>1 ) return 0; |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1817 | pSubSrc = pSub->pSrc; |
| 1818 | assert( pSubSrc ); |
drh | c31c2eb | 2003-05-02 16:04:17 +0000 | [diff] [blame] | 1819 | if( pSubSrc->nSrc==0 ) return 0; |
drh | df199a2 | 2002-06-14 22:38:41 +0000 | [diff] [blame] | 1820 | if( (pSub->isDistinct || pSub->nLimit>=0) && (pSrc->nSrc>1 || isAgg) ){ |
| 1821 | return 0; |
| 1822 | } |
drh | d11d382 | 2002-06-21 23:01:49 +0000 | [diff] [blame] | 1823 | if( (p->isDistinct || p->nLimit>=0) && subqueryIsAgg ) return 0; |
drh | 174b619 | 2002-12-03 02:22:52 +0000 | [diff] [blame] | 1824 | if( p->pOrderBy && pSub->pOrderBy ) return 0; |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1825 | |
drh | 8af4d3a | 2003-05-06 20:35:16 +0000 | [diff] [blame] | 1826 | /* Restriction 3: If the subquery is a join, make sure the subquery is |
| 1827 | ** not used as the right operand of an outer join. Examples of why this |
| 1828 | ** is not allowed: |
| 1829 | ** |
| 1830 | ** t1 LEFT OUTER JOIN (t2 JOIN t3) |
| 1831 | ** |
| 1832 | ** If we flatten the above, we would get |
| 1833 | ** |
| 1834 | ** (t1 LEFT OUTER JOIN t2) JOIN t3 |
| 1835 | ** |
| 1836 | ** which is not at all the same thing. |
| 1837 | */ |
| 1838 | if( pSubSrc->nSrc>1 && iFrom>0 && (pSrc->a[iFrom-1].jointype & JT_OUTER)!=0 ){ |
| 1839 | return 0; |
| 1840 | } |
| 1841 | |
drh | 3fc673e | 2003-06-16 00:40:34 +0000 | [diff] [blame] | 1842 | /* Restriction 12: If the subquery is the right operand of a left outer |
| 1843 | ** join, make sure the subquery has no WHERE clause. |
| 1844 | ** An examples of why this is not allowed: |
| 1845 | ** |
| 1846 | ** t1 LEFT OUTER JOIN (SELECT * FROM t2 WHERE t2.x>0) |
| 1847 | ** |
| 1848 | ** If we flatten the above, we would get |
| 1849 | ** |
| 1850 | ** (t1 LEFT OUTER JOIN t2) WHERE t2.x>0 |
| 1851 | ** |
| 1852 | ** But the t2.x>0 test will always fail on a NULL row of t2, which |
| 1853 | ** effectively converts the OUTER JOIN into an INNER JOIN. |
| 1854 | */ |
| 1855 | if( iFrom>0 && (pSrc->a[iFrom-1].jointype & JT_OUTER)!=0 |
| 1856 | && pSub->pWhere!=0 ){ |
| 1857 | return 0; |
| 1858 | } |
| 1859 | |
drh | 0bb2810 | 2002-05-08 11:54:14 +0000 | [diff] [blame] | 1860 | /* If we reach this point, it means flattening is permitted for the |
drh | 63eb5f2 | 2003-04-29 16:20:44 +0000 | [diff] [blame] | 1861 | ** iFrom-th entry of the FROM clause in the outer query. |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1862 | */ |
drh | c31c2eb | 2003-05-02 16:04:17 +0000 | [diff] [blame] | 1863 | |
| 1864 | /* Move all of the FROM elements of the subquery into the |
| 1865 | ** the FROM clause of the outer query. Before doing this, remember |
| 1866 | ** the cursor number for the original outer query FROM element in |
| 1867 | ** iParent. The iParent cursor will never be used. Subsequent code |
| 1868 | ** will scan expressions looking for iParent references and replace |
| 1869 | ** those references with expressions that resolve to the subquery FROM |
| 1870 | ** elements we are now copying in. |
| 1871 | */ |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 1872 | iParent = pSrc->a[iFrom].iCursor; |
drh | c31c2eb | 2003-05-02 16:04:17 +0000 | [diff] [blame] | 1873 | { |
| 1874 | int nSubSrc = pSubSrc->nSrc; |
drh | 8af4d3a | 2003-05-06 20:35:16 +0000 | [diff] [blame] | 1875 | int jointype = pSrc->a[iFrom].jointype; |
drh | c31c2eb | 2003-05-02 16:04:17 +0000 | [diff] [blame] | 1876 | |
| 1877 | if( pSrc->a[iFrom].pTab && pSrc->a[iFrom].pTab->isTransient ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1878 | sqlite3DeleteTable(0, pSrc->a[iFrom].pTab); |
drh | c31c2eb | 2003-05-02 16:04:17 +0000 | [diff] [blame] | 1879 | } |
drh | f26e09c | 2003-05-31 16:21:12 +0000 | [diff] [blame] | 1880 | sqliteFree(pSrc->a[iFrom].zDatabase); |
drh | c31c2eb | 2003-05-02 16:04:17 +0000 | [diff] [blame] | 1881 | sqliteFree(pSrc->a[iFrom].zName); |
| 1882 | sqliteFree(pSrc->a[iFrom].zAlias); |
| 1883 | if( nSubSrc>1 ){ |
| 1884 | int extra = nSubSrc - 1; |
| 1885 | for(i=1; i<nSubSrc; i++){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1886 | pSrc = sqlite3SrcListAppend(pSrc, 0, 0); |
drh | c31c2eb | 2003-05-02 16:04:17 +0000 | [diff] [blame] | 1887 | } |
| 1888 | p->pSrc = pSrc; |
| 1889 | for(i=pSrc->nSrc-1; i-extra>=iFrom; i--){ |
| 1890 | pSrc->a[i] = pSrc->a[i-extra]; |
| 1891 | } |
| 1892 | } |
| 1893 | for(i=0; i<nSubSrc; i++){ |
| 1894 | pSrc->a[i+iFrom] = pSubSrc->a[i]; |
| 1895 | memset(&pSubSrc->a[i], 0, sizeof(pSubSrc->a[i])); |
| 1896 | } |
drh | 8af4d3a | 2003-05-06 20:35:16 +0000 | [diff] [blame] | 1897 | pSrc->a[iFrom+nSubSrc-1].jointype = jointype; |
drh | c31c2eb | 2003-05-02 16:04:17 +0000 | [diff] [blame] | 1898 | } |
| 1899 | |
| 1900 | /* Now begin substituting subquery result set expressions for |
| 1901 | ** references to the iParent in the outer query. |
| 1902 | ** |
| 1903 | ** Example: |
| 1904 | ** |
| 1905 | ** SELECT a+5, b*10 FROM (SELECT x*3 AS a, y+10 AS b FROM t1) WHERE a>b; |
| 1906 | ** \ \_____________ subquery __________/ / |
| 1907 | ** \_____________________ outer query ______________________________/ |
| 1908 | ** |
| 1909 | ** We look at every expression in the outer query and every place we see |
| 1910 | ** "a" we substitute "x*3" and every place we see "b" we substitute "y+10". |
| 1911 | */ |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 1912 | substExprList(p->pEList, iParent, pSub->pEList); |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1913 | pList = p->pEList; |
| 1914 | for(i=0; i<pList->nExpr; i++){ |
drh | 6977fea | 2002-10-22 23:38:04 +0000 | [diff] [blame] | 1915 | Expr *pExpr; |
| 1916 | if( pList->a[i].zName==0 && (pExpr = pList->a[i].pExpr)->span.z!=0 ){ |
| 1917 | pList->a[i].zName = sqliteStrNDup(pExpr->span.z, pExpr->span.n); |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1918 | } |
| 1919 | } |
drh | 1b2e032 | 2002-03-03 02:49:51 +0000 | [diff] [blame] | 1920 | if( isAgg ){ |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 1921 | substExprList(p->pGroupBy, iParent, pSub->pEList); |
| 1922 | substExpr(p->pHaving, iParent, pSub->pEList); |
drh | 1b2e032 | 2002-03-03 02:49:51 +0000 | [diff] [blame] | 1923 | } |
drh | 174b619 | 2002-12-03 02:22:52 +0000 | [diff] [blame] | 1924 | if( pSub->pOrderBy ){ |
| 1925 | assert( p->pOrderBy==0 ); |
| 1926 | p->pOrderBy = pSub->pOrderBy; |
| 1927 | pSub->pOrderBy = 0; |
drh | 174b619 | 2002-12-03 02:22:52 +0000 | [diff] [blame] | 1928 | }else if( p->pOrderBy ){ |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 1929 | substExprList(p->pOrderBy, iParent, pSub->pEList); |
drh | 174b619 | 2002-12-03 02:22:52 +0000 | [diff] [blame] | 1930 | } |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1931 | if( pSub->pWhere ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1932 | pWhere = sqlite3ExprDup(pSub->pWhere); |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1933 | }else{ |
| 1934 | pWhere = 0; |
| 1935 | } |
| 1936 | if( subqueryIsAgg ){ |
| 1937 | assert( p->pHaving==0 ); |
drh | 1b2e032 | 2002-03-03 02:49:51 +0000 | [diff] [blame] | 1938 | p->pHaving = p->pWhere; |
| 1939 | p->pWhere = pWhere; |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 1940 | substExpr(p->pHaving, iParent, pSub->pEList); |
drh | 1b2e032 | 2002-03-03 02:49:51 +0000 | [diff] [blame] | 1941 | if( pSub->pHaving ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1942 | Expr *pHaving = sqlite3ExprDup(pSub->pHaving); |
drh | 1b2e032 | 2002-03-03 02:49:51 +0000 | [diff] [blame] | 1943 | if( p->pHaving ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1944 | p->pHaving = sqlite3Expr(TK_AND, p->pHaving, pHaving, 0); |
drh | 1b2e032 | 2002-03-03 02:49:51 +0000 | [diff] [blame] | 1945 | }else{ |
| 1946 | p->pHaving = pHaving; |
| 1947 | } |
| 1948 | } |
| 1949 | assert( p->pGroupBy==0 ); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1950 | p->pGroupBy = sqlite3ExprListDup(pSub->pGroupBy); |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1951 | }else if( p->pWhere==0 ){ |
| 1952 | p->pWhere = pWhere; |
| 1953 | }else{ |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 1954 | substExpr(p->pWhere, iParent, pSub->pEList); |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1955 | if( pWhere ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1956 | p->pWhere = sqlite3Expr(TK_AND, p->pWhere, pWhere, 0); |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1957 | } |
| 1958 | } |
drh | c31c2eb | 2003-05-02 16:04:17 +0000 | [diff] [blame] | 1959 | |
| 1960 | /* The flattened query is distinct if either the inner or the |
| 1961 | ** outer query is distinct. |
| 1962 | */ |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1963 | p->isDistinct = p->isDistinct || pSub->isDistinct; |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 1964 | |
drh | c31c2eb | 2003-05-02 16:04:17 +0000 | [diff] [blame] | 1965 | /* Transfer the limit expression from the subquery to the outer |
| 1966 | ** query. |
| 1967 | */ |
drh | df199a2 | 2002-06-14 22:38:41 +0000 | [diff] [blame] | 1968 | if( pSub->nLimit>=0 ){ |
| 1969 | if( p->nLimit<0 ){ |
| 1970 | p->nLimit = pSub->nLimit; |
| 1971 | }else if( p->nLimit+p->nOffset > pSub->nLimit+pSub->nOffset ){ |
| 1972 | p->nLimit = pSub->nLimit + pSub->nOffset - p->nOffset; |
| 1973 | } |
| 1974 | } |
| 1975 | p->nOffset += pSub->nOffset; |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 1976 | |
drh | c31c2eb | 2003-05-02 16:04:17 +0000 | [diff] [blame] | 1977 | /* Finially, delete what is left of the subquery and return |
| 1978 | ** success. |
| 1979 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 1980 | sqlite3SelectDelete(pSub); |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1981 | return 1; |
| 1982 | } |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 1983 | |
| 1984 | /* |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 1985 | ** Analyze the SELECT statement passed in as an argument to see if it |
| 1986 | ** is a simple min() or max() query. If it is and this query can be |
| 1987 | ** satisfied using a single seek to the beginning or end of an index, |
drh | e78e828 | 2003-01-19 03:59:45 +0000 | [diff] [blame] | 1988 | ** 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] | 1989 | ** simple min() or max() query, then return 0; |
| 1990 | ** |
| 1991 | ** A simply min() or max() query looks like this: |
| 1992 | ** |
| 1993 | ** SELECT min(a) FROM table; |
| 1994 | ** SELECT max(a) FROM table; |
| 1995 | ** |
| 1996 | ** The query may have only a single table in its FROM argument. There |
| 1997 | ** can be no GROUP BY or HAVING or WHERE clauses. The result set must |
| 1998 | ** be the min() or max() of a single column of the table. The column |
| 1999 | ** in the min() or max() function must be indexed. |
| 2000 | ** |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2001 | ** The parameters to this routine are the same as for sqlite3Select(). |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 2002 | ** See the header comment on that routine for additional information. |
| 2003 | */ |
| 2004 | static int simpleMinMaxQuery(Parse *pParse, Select *p, int eDest, int iParm){ |
| 2005 | Expr *pExpr; |
| 2006 | int iCol; |
| 2007 | Table *pTab; |
| 2008 | Index *pIdx; |
| 2009 | int base; |
| 2010 | Vdbe *v; |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 2011 | int seekOp; |
| 2012 | int cont; |
drh | 6e17529 | 2004-03-13 14:00:36 +0000 | [diff] [blame] | 2013 | ExprList *pEList, *pList, eList; |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 2014 | struct ExprList_item eListItem; |
drh | 6e17529 | 2004-03-13 14:00:36 +0000 | [diff] [blame] | 2015 | SrcList *pSrc; |
| 2016 | |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 2017 | |
| 2018 | /* Check to see if this query is a simple min() or max() query. Return |
| 2019 | ** zero if it is not. |
| 2020 | */ |
| 2021 | if( p->pGroupBy || p->pHaving || p->pWhere ) return 0; |
drh | 6e17529 | 2004-03-13 14:00:36 +0000 | [diff] [blame] | 2022 | pSrc = p->pSrc; |
| 2023 | if( pSrc->nSrc!=1 ) return 0; |
| 2024 | pEList = p->pEList; |
| 2025 | if( pEList->nExpr!=1 ) return 0; |
| 2026 | pExpr = pEList->a[0].pExpr; |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 2027 | if( pExpr->op!=TK_AGG_FUNCTION ) return 0; |
drh | 6e17529 | 2004-03-13 14:00:36 +0000 | [diff] [blame] | 2028 | pList = pExpr->pList; |
| 2029 | if( pList==0 || pList->nExpr!=1 ) return 0; |
drh | 6977fea | 2002-10-22 23:38:04 +0000 | [diff] [blame] | 2030 | if( pExpr->token.n!=3 ) return 0; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2031 | if( sqlite3StrNICmp(pExpr->token.z,"min",3)==0 ){ |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 2032 | seekOp = OP_Rewind; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2033 | }else if( sqlite3StrNICmp(pExpr->token.z,"max",3)==0 ){ |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 2034 | seekOp = OP_Last; |
| 2035 | }else{ |
| 2036 | return 0; |
| 2037 | } |
drh | 6e17529 | 2004-03-13 14:00:36 +0000 | [diff] [blame] | 2038 | pExpr = pList->a[0].pExpr; |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 2039 | if( pExpr->op!=TK_COLUMN ) return 0; |
| 2040 | iCol = pExpr->iColumn; |
drh | 6e17529 | 2004-03-13 14:00:36 +0000 | [diff] [blame] | 2041 | pTab = pSrc->a[0].pTab; |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 2042 | |
| 2043 | /* 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] | 2044 | ** Check to make sure we have an index and make pIdx point to the |
| 2045 | ** appropriate index. If the min() or max() is on an INTEGER PRIMARY |
| 2046 | ** key column, no index is necessary so set pIdx to NULL. If no |
| 2047 | ** usable index is found, return 0. |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 2048 | */ |
| 2049 | if( iCol<0 ){ |
| 2050 | pIdx = 0; |
| 2051 | }else{ |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 2052 | CollSeq *pColl = sqlite3ExprCollSeq(pParse, pExpr); |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 2053 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 2054 | assert( pIdx->nColumn>=1 ); |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 2055 | if( pIdx->aiColumn[0]==iCol && pIdx->keyInfo.aColl[0]==pColl ) break; |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 2056 | } |
| 2057 | if( pIdx==0 ) return 0; |
| 2058 | } |
| 2059 | |
drh | e5f5072 | 2003-07-19 00:44:14 +0000 | [diff] [blame] | 2060 | /* Identify column types if we will be using the callback. This |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 2061 | ** 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] | 2062 | ** The column names have already been generated in the calling function. |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 2063 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2064 | v = sqlite3GetVdbe(pParse); |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 2065 | if( v==0 ) return 0; |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 2066 | |
drh | 0c37e63 | 2004-01-30 02:01:03 +0000 | [diff] [blame] | 2067 | /* If the output is destined for a temporary table, open that table. |
| 2068 | */ |
| 2069 | if( eDest==SRT_TempTable ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2070 | sqlite3VdbeAddOp(v, OP_OpenTemp, iParm, 0); |
danielk1977 | b4964b7 | 2004-05-18 01:23:38 +0000 | [diff] [blame] | 2071 | sqlite3VdbeAddOp(v, OP_SetNumColumns, iParm, 1); |
drh | 0c37e63 | 2004-01-30 02:01:03 +0000 | [diff] [blame] | 2072 | } |
| 2073 | |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 2074 | /* Generating code to find the min or the max. Basically all we have |
| 2075 | ** to do is find the first or the last entry in the chosen index. If |
| 2076 | ** the min() or max() is on the INTEGER PRIMARY KEY, then find the first |
| 2077 | ** or last entry in the main table. |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 2078 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2079 | sqlite3CodeVerifySchema(pParse, pTab->iDb); |
drh | 6e17529 | 2004-03-13 14:00:36 +0000 | [diff] [blame] | 2080 | base = pSrc->a[0].iCursor; |
drh | 7b58dae | 2003-07-20 01:16:46 +0000 | [diff] [blame] | 2081 | computeLimitRegisters(pParse, p); |
drh | 6e17529 | 2004-03-13 14:00:36 +0000 | [diff] [blame] | 2082 | if( pSrc->a[0].pSelect==0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2083 | sqlite3VdbeAddOp(v, OP_Integer, pTab->iDb, 0); |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 2084 | sqlite3VdbeAddOp(v, OP_OpenRead, base, pTab->tnum); |
danielk1977 | b4964b7 | 2004-05-18 01:23:38 +0000 | [diff] [blame] | 2085 | sqlite3VdbeAddOp(v, OP_SetNumColumns, base, pTab->nCol); |
drh | 6e17529 | 2004-03-13 14:00:36 +0000 | [diff] [blame] | 2086 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2087 | cont = sqlite3VdbeMakeLabel(v); |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 2088 | if( pIdx==0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2089 | sqlite3VdbeAddOp(v, seekOp, base, 0); |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 2090 | }else{ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2091 | sqlite3VdbeAddOp(v, OP_Integer, pIdx->iDb, 0); |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 2092 | sqlite3VdbeOp3(v, OP_OpenRead, base+1, pIdx->tnum, |
| 2093 | (char*)&pIdx->keyInfo, P3_KEYINFO); |
drh | 9eb516c | 2004-07-18 20:52:32 +0000 | [diff] [blame] | 2094 | if( seekOp==OP_Rewind ){ |
drh | 1af3fdb | 2004-07-18 21:33:01 +0000 | [diff] [blame] | 2095 | sqlite3VdbeAddOp(v, OP_String, 0, 0); |
| 2096 | sqlite3VdbeAddOp(v, OP_MakeRecord, 1, 0); |
| 2097 | seekOp = OP_MoveGt; |
drh | 9eb516c | 2004-07-18 20:52:32 +0000 | [diff] [blame] | 2098 | } |
drh | 1af3fdb | 2004-07-18 21:33:01 +0000 | [diff] [blame] | 2099 | sqlite3VdbeAddOp(v, seekOp, base+1, 0); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2100 | sqlite3VdbeAddOp(v, OP_IdxRecno, base+1, 0); |
| 2101 | sqlite3VdbeAddOp(v, OP_Close, base+1, 0); |
drh | 7cf6e4d | 2004-05-19 14:56:55 +0000 | [diff] [blame] | 2102 | sqlite3VdbeAddOp(v, OP_MoveGe, base, 0); |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 2103 | } |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 2104 | eList.nExpr = 1; |
| 2105 | memset(&eListItem, 0, sizeof(eListItem)); |
| 2106 | eList.a = &eListItem; |
| 2107 | eList.a[0].pExpr = pExpr; |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 2108 | selectInnerLoop(pParse, p, &eList, 0, 0, 0, -1, eDest, iParm, cont, cont, 0); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2109 | sqlite3VdbeResolveLabel(v, cont); |
| 2110 | sqlite3VdbeAddOp(v, OP_Close, base, 0); |
drh | 6e17529 | 2004-03-13 14:00:36 +0000 | [diff] [blame] | 2111 | |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 2112 | return 1; |
| 2113 | } |
| 2114 | |
| 2115 | /* |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 2116 | ** Generate code for the given SELECT statement. |
| 2117 | ** |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 2118 | ** The results are distributed in various ways depending on the |
| 2119 | ** value of eDest and iParm. |
| 2120 | ** |
| 2121 | ** eDest Value Result |
| 2122 | ** ------------ ------------------------------------------- |
| 2123 | ** SRT_Callback Invoke the callback for each row of the result. |
| 2124 | ** |
| 2125 | ** SRT_Mem Store first result in memory cell iParm |
| 2126 | ** |
danielk1977 | e014a83 | 2004-05-17 10:48:57 +0000 | [diff] [blame] | 2127 | ** SRT_Set Store results as keys of table iParm. |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 2128 | ** |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 2129 | ** SRT_Union Store results as a key in a temporary table iParm |
| 2130 | ** |
jplyon | 4b11c6d | 2004-01-19 04:57:53 +0000 | [diff] [blame] | 2131 | ** SRT_Except Remove results from the temporary table iParm. |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 2132 | ** |
| 2133 | ** SRT_Table Store results in temporary table iParm |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 2134 | ** |
drh | e78e828 | 2003-01-19 03:59:45 +0000 | [diff] [blame] | 2135 | ** The table above is incomplete. Additional eDist value have be added |
| 2136 | ** since this comment was written. See the selectInnerLoop() function for |
| 2137 | ** a complete listing of the allowed values of eDest and their meanings. |
| 2138 | ** |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 2139 | ** This routine returns the number of errors. If any errors are |
| 2140 | ** encountered, then an appropriate error message is left in |
| 2141 | ** pParse->zErrMsg. |
| 2142 | ** |
| 2143 | ** This routine does NOT free the Select structure passed in. The |
| 2144 | ** calling function needs to do that. |
drh | 1b2e032 | 2002-03-03 02:49:51 +0000 | [diff] [blame] | 2145 | ** |
| 2146 | ** The pParent, parentTab, and *pParentAgg fields are filled in if this |
| 2147 | ** SELECT is a subquery. This routine may try to combine this SELECT |
| 2148 | ** with its parent to form a single flat query. In so doing, it might |
| 2149 | ** change the parent query from a non-aggregate to an aggregate query. |
| 2150 | ** For that reason, the pParentAgg flag is passed as a pointer, so it |
| 2151 | ** can be changed. |
drh | e78e828 | 2003-01-19 03:59:45 +0000 | [diff] [blame] | 2152 | ** |
| 2153 | ** Example 1: The meaning of the pParent parameter. |
| 2154 | ** |
| 2155 | ** SELECT * FROM t1 JOIN (SELECT x, count(*) FROM t2) JOIN t3; |
| 2156 | ** \ \_______ subquery _______/ / |
| 2157 | ** \ / |
| 2158 | ** \____________________ outer query ___________________/ |
| 2159 | ** |
| 2160 | ** This routine is called for the outer query first. For that call, |
| 2161 | ** pParent will be NULL. During the processing of the outer query, this |
| 2162 | ** routine is called recursively to handle the subquery. For the recursive |
| 2163 | ** call, pParent will point to the outer query. Because the subquery is |
| 2164 | ** the second element in a three-way join, the parentTab parameter will |
| 2165 | ** be 1 (the 2nd value of a 0-indexed array.) |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 2166 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2167 | int sqlite3Select( |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 2168 | Parse *pParse, /* The parser context */ |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 2169 | Select *p, /* The SELECT statement being coded. */ |
drh | e78e828 | 2003-01-19 03:59:45 +0000 | [diff] [blame] | 2170 | int eDest, /* How to dispose of the results */ |
| 2171 | int iParm, /* A parameter used by the eDest disposal method */ |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 2172 | Select *pParent, /* Another SELECT for which this is a sub-query */ |
| 2173 | int parentTab, /* Index in pParent->pSrc of this query */ |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 2174 | int *pParentAgg, /* True if pParent uses aggregate functions */ |
| 2175 | char *aff /* If eDest is SRT_Union, the affinity string */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 2176 | ){ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 2177 | int i; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 2178 | WhereInfo *pWInfo; |
| 2179 | Vdbe *v; |
| 2180 | int isAgg = 0; /* True for select lists like "count(*)" */ |
drh | a2e0004 | 2002-01-22 03:13:42 +0000 | [diff] [blame] | 2181 | ExprList *pEList; /* List of columns to extract. */ |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 2182 | SrcList *pTabList; /* List of tables to select from */ |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 2183 | Expr *pWhere; /* The WHERE clause. May be NULL */ |
| 2184 | ExprList *pOrderBy; /* The ORDER BY clause. May be NULL */ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 2185 | ExprList *pGroupBy; /* The GROUP BY clause. May be NULL */ |
| 2186 | Expr *pHaving; /* The HAVING clause. May be NULL */ |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 2187 | int isDistinct; /* True if the DISTINCT keyword is present */ |
| 2188 | int distinct; /* Table to use for the distinct set */ |
drh | 1d83f05 | 2002-02-17 00:30:36 +0000 | [diff] [blame] | 2189 | int rc = 1; /* Value to return from this function */ |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 2190 | |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 2191 | if( sqlite3_malloc_failed || pParse->nErr || p==0 ) return 1; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2192 | if( sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0) ) return 1; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 2193 | |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 2194 | /* If there is are a sequence of queries, do the earlier ones first. |
| 2195 | */ |
| 2196 | if( p->pPrior ){ |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 2197 | return multiSelect(pParse, p, eDest, iParm, aff); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 2198 | } |
| 2199 | |
| 2200 | /* Make local copies of the parameters for this query. |
| 2201 | */ |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 2202 | pTabList = p->pSrc; |
| 2203 | pWhere = p->pWhere; |
| 2204 | pOrderBy = p->pOrderBy; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 2205 | pGroupBy = p->pGroupBy; |
| 2206 | pHaving = p->pHaving; |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 2207 | isDistinct = p->isDistinct; |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 2208 | |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 2209 | /* Allocate VDBE cursors for each table in the FROM clause |
drh | 10e5e3c | 2000-06-08 00:19:02 +0000 | [diff] [blame] | 2210 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2211 | sqlite3SrcListAssignCursors(pParse, pTabList); |
drh | 10e5e3c | 2000-06-08 00:19:02 +0000 | [diff] [blame] | 2212 | |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 2213 | /* |
| 2214 | ** Do not even attempt to generate any code if we have already seen |
| 2215 | ** errors before this routine starts. |
| 2216 | */ |
drh | 1d83f05 | 2002-02-17 00:30:36 +0000 | [diff] [blame] | 2217 | if( pParse->nErr>0 ) goto select_end; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 2218 | |
drh | e78e828 | 2003-01-19 03:59:45 +0000 | [diff] [blame] | 2219 | /* Expand any "*" terms in the result set. (For example the "*" in |
| 2220 | ** "SELECT * FROM t1") The fillInColumnlist() routine also does some |
| 2221 | ** other housekeeping - see the header comment for details. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 2222 | */ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 2223 | if( fillInColumnList(pParse, p) ){ |
drh | 1d83f05 | 2002-02-17 00:30:36 +0000 | [diff] [blame] | 2224 | goto select_end; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 2225 | } |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 2226 | pWhere = p->pWhere; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 2227 | pEList = p->pEList; |
drh | 1d83f05 | 2002-02-17 00:30:36 +0000 | [diff] [blame] | 2228 | if( pEList==0 ) goto select_end; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 2229 | |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 2230 | /* If writing to memory or generating a set |
| 2231 | ** only a single column may be output. |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 2232 | */ |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 2233 | if( (eDest==SRT_Mem || eDest==SRT_Set) && pEList->nExpr>1 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2234 | sqlite3ErrorMsg(pParse, "only a single result allowed for " |
drh | da93d23 | 2003-03-31 02:12:46 +0000 | [diff] [blame] | 2235 | "a SELECT that is part of an expression"); |
drh | 1d83f05 | 2002-02-17 00:30:36 +0000 | [diff] [blame] | 2236 | goto select_end; |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 2237 | } |
| 2238 | |
drh | c926afb | 2002-06-20 03:38:26 +0000 | [diff] [blame] | 2239 | /* ORDER BY is ignored for some destinations. |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 2240 | */ |
drh | c926afb | 2002-06-20 03:38:26 +0000 | [diff] [blame] | 2241 | switch( eDest ){ |
| 2242 | case SRT_Union: |
| 2243 | case SRT_Except: |
| 2244 | case SRT_Discard: |
danielk1977 | f93bbbe | 2004-05-27 10:30:52 +0000 | [diff] [blame] | 2245 | case SRT_Set: |
drh | c926afb | 2002-06-20 03:38:26 +0000 | [diff] [blame] | 2246 | pOrderBy = 0; |
| 2247 | break; |
| 2248 | default: |
| 2249 | break; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 2250 | } |
| 2251 | |
drh | 10e5e3c | 2000-06-08 00:19:02 +0000 | [diff] [blame] | 2252 | /* At this point, we should have allocated all the cursors that we |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 2253 | ** need to handle subquerys and temporary tables. |
drh | 10e5e3c | 2000-06-08 00:19:02 +0000 | [diff] [blame] | 2254 | ** |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 2255 | ** Resolve the column names and do a semantics check on all the expressions. |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 2256 | */ |
drh | 4794b98 | 2000-06-06 13:54:14 +0000 | [diff] [blame] | 2257 | for(i=0; i<pEList->nExpr; i++){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2258 | if( sqlite3ExprResolveIds(pParse, pTabList, 0, pEList->a[i].pExpr) ){ |
drh | 1d83f05 | 2002-02-17 00:30:36 +0000 | [diff] [blame] | 2259 | goto select_end; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 2260 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2261 | if( sqlite3ExprCheck(pParse, pEList->a[i].pExpr, 1, &isAgg) ){ |
drh | 1d83f05 | 2002-02-17 00:30:36 +0000 | [diff] [blame] | 2262 | goto select_end; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 2263 | } |
| 2264 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 2265 | if( pWhere ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2266 | if( sqlite3ExprResolveIds(pParse, pTabList, pEList, pWhere) ){ |
drh | 1d83f05 | 2002-02-17 00:30:36 +0000 | [diff] [blame] | 2267 | goto select_end; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 2268 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2269 | if( sqlite3ExprCheck(pParse, pWhere, 0, 0) ){ |
drh | 1d83f05 | 2002-02-17 00:30:36 +0000 | [diff] [blame] | 2270 | goto select_end; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 2271 | } |
| 2272 | } |
drh | c66c5a2 | 2002-12-03 02:34:49 +0000 | [diff] [blame] | 2273 | if( pHaving ){ |
| 2274 | if( pGroupBy==0 ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2275 | sqlite3ErrorMsg(pParse, "a GROUP BY clause is required before HAVING"); |
drh | c66c5a2 | 2002-12-03 02:34:49 +0000 | [diff] [blame] | 2276 | goto select_end; |
| 2277 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2278 | if( sqlite3ExprResolveIds(pParse, pTabList, pEList, pHaving) ){ |
drh | c66c5a2 | 2002-12-03 02:34:49 +0000 | [diff] [blame] | 2279 | goto select_end; |
| 2280 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2281 | if( sqlite3ExprCheck(pParse, pHaving, 1, &isAgg) ){ |
drh | c66c5a2 | 2002-12-03 02:34:49 +0000 | [diff] [blame] | 2282 | goto select_end; |
| 2283 | } |
| 2284 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 2285 | if( pOrderBy ){ |
| 2286 | for(i=0; i<pOrderBy->nExpr; i++){ |
drh | 88eee38 | 2003-01-31 17:16:36 +0000 | [diff] [blame] | 2287 | int iCol; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 2288 | Expr *pE = pOrderBy->a[i].pExpr; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2289 | if( sqlite3ExprIsInteger(pE, &iCol) && iCol>0 && iCol<=pEList->nExpr ){ |
| 2290 | sqlite3ExprDelete(pE); |
| 2291 | pE = pOrderBy->a[i].pExpr = sqlite3ExprDup(pEList->a[iCol-1].pExpr); |
drh | 88eee38 | 2003-01-31 17:16:36 +0000 | [diff] [blame] | 2292 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2293 | if( sqlite3ExprResolveIds(pParse, pTabList, pEList, pE) ){ |
drh | 88eee38 | 2003-01-31 17:16:36 +0000 | [diff] [blame] | 2294 | goto select_end; |
| 2295 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2296 | if( sqlite3ExprCheck(pParse, pE, isAgg, 0) ){ |
drh | 88eee38 | 2003-01-31 17:16:36 +0000 | [diff] [blame] | 2297 | goto select_end; |
| 2298 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2299 | if( sqlite3ExprIsConstant(pE) ){ |
| 2300 | if( sqlite3ExprIsInteger(pE, &iCol)==0 ){ |
| 2301 | sqlite3ErrorMsg(pParse, |
drh | da93d23 | 2003-03-31 02:12:46 +0000 | [diff] [blame] | 2302 | "ORDER BY terms must not be non-integer constants"); |
drh | e4de1fe | 2002-06-02 16:09:01 +0000 | [diff] [blame] | 2303 | goto select_end; |
| 2304 | }else if( iCol<=0 || iCol>pEList->nExpr ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2305 | sqlite3ErrorMsg(pParse, |
drh | da93d23 | 2003-03-31 02:12:46 +0000 | [diff] [blame] | 2306 | "ORDER BY column number %d out of range - should be " |
drh | e4de1fe | 2002-06-02 16:09:01 +0000 | [diff] [blame] | 2307 | "between 1 and %d", iCol, pEList->nExpr); |
drh | e4de1fe | 2002-06-02 16:09:01 +0000 | [diff] [blame] | 2308 | goto select_end; |
| 2309 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 2310 | } |
| 2311 | } |
| 2312 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 2313 | if( pGroupBy ){ |
| 2314 | for(i=0; i<pGroupBy->nExpr; i++){ |
drh | 88eee38 | 2003-01-31 17:16:36 +0000 | [diff] [blame] | 2315 | int iCol; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 2316 | Expr *pE = pGroupBy->a[i].pExpr; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2317 | if( sqlite3ExprIsInteger(pE, &iCol) && iCol>0 && iCol<=pEList->nExpr ){ |
| 2318 | sqlite3ExprDelete(pE); |
| 2319 | pE = pGroupBy->a[i].pExpr = sqlite3ExprDup(pEList->a[iCol-1].pExpr); |
drh | 9208643 | 2002-01-22 14:11:29 +0000 | [diff] [blame] | 2320 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2321 | if( sqlite3ExprResolveIds(pParse, pTabList, pEList, pE) ){ |
drh | 1d83f05 | 2002-02-17 00:30:36 +0000 | [diff] [blame] | 2322 | goto select_end; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 2323 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2324 | if( sqlite3ExprCheck(pParse, pE, isAgg, 0) ){ |
drh | 1d83f05 | 2002-02-17 00:30:36 +0000 | [diff] [blame] | 2325 | goto select_end; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 2326 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2327 | if( sqlite3ExprIsConstant(pE) ){ |
| 2328 | if( sqlite3ExprIsInteger(pE, &iCol)==0 ){ |
| 2329 | sqlite3ErrorMsg(pParse, |
drh | da93d23 | 2003-03-31 02:12:46 +0000 | [diff] [blame] | 2330 | "GROUP BY terms must not be non-integer constants"); |
drh | 88eee38 | 2003-01-31 17:16:36 +0000 | [diff] [blame] | 2331 | goto select_end; |
| 2332 | }else if( iCol<=0 || iCol>pEList->nExpr ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2333 | sqlite3ErrorMsg(pParse, |
drh | da93d23 | 2003-03-31 02:12:46 +0000 | [diff] [blame] | 2334 | "GROUP BY column number %d out of range - should be " |
drh | 88eee38 | 2003-01-31 17:16:36 +0000 | [diff] [blame] | 2335 | "between 1 and %d", iCol, pEList->nExpr); |
drh | 88eee38 | 2003-01-31 17:16:36 +0000 | [diff] [blame] | 2336 | goto select_end; |
| 2337 | } |
| 2338 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 2339 | } |
| 2340 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 2341 | |
drh | d820cb1 | 2002-02-18 03:21:45 +0000 | [diff] [blame] | 2342 | /* Begin generating code. |
| 2343 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2344 | v = sqlite3GetVdbe(pParse); |
drh | d820cb1 | 2002-02-18 03:21:45 +0000 | [diff] [blame] | 2345 | if( v==0 ) goto select_end; |
| 2346 | |
drh | e78e828 | 2003-01-19 03:59:45 +0000 | [diff] [blame] | 2347 | /* Identify column names if we will be using them in a callback. This |
| 2348 | ** step is skipped if the output is going to some other destination. |
drh | 0bb2810 | 2002-05-08 11:54:14 +0000 | [diff] [blame] | 2349 | */ |
| 2350 | if( eDest==SRT_Callback ){ |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 2351 | generateColumnNames(pParse, pTabList, pEList); |
drh | 0bb2810 | 2002-05-08 11:54:14 +0000 | [diff] [blame] | 2352 | } |
| 2353 | |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 2354 | #if 1 /* I do not think we need the following code any more.... */ |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 2355 | /* If the destination is SRT_Union, then set the number of columns in |
| 2356 | ** the records that will be inserted into the temporary table. The caller |
| 2357 | ** couldn't do this, in case the select statement is of the form |
| 2358 | ** "SELECT * FROM ....". |
| 2359 | ** |
| 2360 | ** We need to do this before we start inserting records into the |
| 2361 | ** temporary table (which has had OP_KeyAsData executed on it), because |
| 2362 | ** it is required by the key comparison function. So do it now, even |
| 2363 | ** though this means that OP_SetNumColumns may be executed on the same |
| 2364 | ** cursor more than once. |
| 2365 | */ |
| 2366 | if( eDest==SRT_Union ){ |
| 2367 | sqlite3VdbeAddOp(v, OP_SetNumColumns, iParm, pEList->nExpr); |
| 2368 | } |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 2369 | #endif |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 2370 | |
drh | d820cb1 | 2002-02-18 03:21:45 +0000 | [diff] [blame] | 2371 | /* Generate code for all sub-queries in the FROM clause |
| 2372 | */ |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 2373 | for(i=0; i<pTabList->nSrc; i++){ |
danielk1977 | 742f947 | 2004-06-16 12:02:43 +0000 | [diff] [blame] | 2374 | const char *zSavedAuthContext = 0; |
drh | c31c2eb | 2003-05-02 16:04:17 +0000 | [diff] [blame] | 2375 | int needRestoreContext; |
| 2376 | |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 2377 | if( pTabList->a[i].pSelect==0 ) continue; |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 2378 | if( pTabList->a[i].zName!=0 ){ |
| 2379 | zSavedAuthContext = pParse->zAuthContext; |
| 2380 | pParse->zAuthContext = pTabList->a[i].zName; |
drh | c31c2eb | 2003-05-02 16:04:17 +0000 | [diff] [blame] | 2381 | needRestoreContext = 1; |
| 2382 | }else{ |
| 2383 | needRestoreContext = 0; |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 2384 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2385 | sqlite3Select(pParse, pTabList->a[i].pSelect, SRT_TempTable, |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 2386 | pTabList->a[i].iCursor, p, i, &isAgg, 0); |
drh | c31c2eb | 2003-05-02 16:04:17 +0000 | [diff] [blame] | 2387 | if( needRestoreContext ){ |
drh | 5cf590c | 2003-04-24 01:45:04 +0000 | [diff] [blame] | 2388 | pParse->zAuthContext = zSavedAuthContext; |
| 2389 | } |
drh | 1b2e032 | 2002-03-03 02:49:51 +0000 | [diff] [blame] | 2390 | pTabList = p->pSrc; |
| 2391 | pWhere = p->pWhere; |
drh | c31c2eb | 2003-05-02 16:04:17 +0000 | [diff] [blame] | 2392 | if( eDest!=SRT_Union && eDest!=SRT_Except && eDest!=SRT_Discard ){ |
drh | acd4c69 | 2002-03-07 02:02:51 +0000 | [diff] [blame] | 2393 | pOrderBy = p->pOrderBy; |
| 2394 | } |
drh | 1b2e032 | 2002-03-03 02:49:51 +0000 | [diff] [blame] | 2395 | pGroupBy = p->pGroupBy; |
| 2396 | pHaving = p->pHaving; |
| 2397 | isDistinct = p->isDistinct; |
drh | d820cb1 | 2002-02-18 03:21:45 +0000 | [diff] [blame] | 2398 | } |
| 2399 | |
drh | 6e17529 | 2004-03-13 14:00:36 +0000 | [diff] [blame] | 2400 | /* Check for the special case of a min() or max() function by itself |
| 2401 | ** in the result set. |
| 2402 | */ |
| 2403 | if( simpleMinMaxQuery(pParse, p, eDest, iParm) ){ |
| 2404 | rc = 0; |
| 2405 | goto select_end; |
| 2406 | } |
| 2407 | |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 2408 | /* Check to see if this is a subquery that can be "flattened" into its parent. |
| 2409 | ** If flattening is a possiblity, do so and return immediately. |
| 2410 | */ |
drh | 1b2e032 | 2002-03-03 02:49:51 +0000 | [diff] [blame] | 2411 | if( pParent && pParentAgg && |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 2412 | flattenSubquery(pParse, pParent, parentTab, *pParentAgg, isAgg) ){ |
drh | 1b2e032 | 2002-03-03 02:49:51 +0000 | [diff] [blame] | 2413 | if( isAgg ) *pParentAgg = 1; |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 2414 | return rc; |
| 2415 | } |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 2416 | |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 2417 | /* If there is an ORDER BY clause, resolve any collation sequences |
| 2418 | ** names that have been explicitly specified. |
| 2419 | */ |
| 2420 | if( pOrderBy ){ |
| 2421 | for(i=0; i<pOrderBy->nExpr; i++){ |
| 2422 | if( pOrderBy->a[i].zName ){ |
| 2423 | pOrderBy->a[i].pExpr->pColl = |
| 2424 | sqlite3LocateCollSeq(pParse, pOrderBy->a[i].zName, -1); |
| 2425 | } |
| 2426 | } |
| 2427 | if( pParse->nErr ){ |
| 2428 | goto select_end; |
| 2429 | } |
| 2430 | } |
| 2431 | |
drh | 7b58dae | 2003-07-20 01:16:46 +0000 | [diff] [blame] | 2432 | /* Set the limiter. |
| 2433 | */ |
| 2434 | computeLimitRegisters(pParse, p); |
| 2435 | |
drh | 2d0794e | 2002-03-03 03:03:52 +0000 | [diff] [blame] | 2436 | /* If the output is destined for a temporary table, open that table. |
| 2437 | */ |
| 2438 | if( eDest==SRT_TempTable ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2439 | sqlite3VdbeAddOp(v, OP_OpenTemp, iParm, 0); |
danielk1977 | b4964b7 | 2004-05-18 01:23:38 +0000 | [diff] [blame] | 2440 | sqlite3VdbeAddOp(v, OP_SetNumColumns, iParm, pEList->nExpr); |
drh | 2d0794e | 2002-03-03 03:03:52 +0000 | [diff] [blame] | 2441 | } |
| 2442 | |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 2443 | /* Do an analysis of aggregate expressions. |
drh | efb7251 | 2000-05-31 20:00:52 +0000 | [diff] [blame] | 2444 | */ |
drh | d820cb1 | 2002-02-18 03:21:45 +0000 | [diff] [blame] | 2445 | sqliteAggregateInfoReset(pParse); |
drh | bb999ef | 2003-02-02 12:41:25 +0000 | [diff] [blame] | 2446 | if( isAgg || pGroupBy ){ |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 2447 | assert( pParse->nAgg==0 ); |
drh | bb999ef | 2003-02-02 12:41:25 +0000 | [diff] [blame] | 2448 | isAgg = 1; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 2449 | for(i=0; i<pEList->nExpr; i++){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2450 | if( sqlite3ExprAnalyzeAggregates(pParse, pEList->a[i].pExpr) ){ |
drh | 1d83f05 | 2002-02-17 00:30:36 +0000 | [diff] [blame] | 2451 | goto select_end; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 2452 | } |
| 2453 | } |
| 2454 | if( pGroupBy ){ |
| 2455 | for(i=0; i<pGroupBy->nExpr; i++){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2456 | if( sqlite3ExprAnalyzeAggregates(pParse, pGroupBy->a[i].pExpr) ){ |
drh | 1d83f05 | 2002-02-17 00:30:36 +0000 | [diff] [blame] | 2457 | goto select_end; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 2458 | } |
| 2459 | } |
| 2460 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2461 | if( pHaving && sqlite3ExprAnalyzeAggregates(pParse, pHaving) ){ |
drh | 1d83f05 | 2002-02-17 00:30:36 +0000 | [diff] [blame] | 2462 | goto select_end; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 2463 | } |
drh | 191b690 | 2000-06-08 11:13:01 +0000 | [diff] [blame] | 2464 | if( pOrderBy ){ |
| 2465 | for(i=0; i<pOrderBy->nExpr; i++){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2466 | if( sqlite3ExprAnalyzeAggregates(pParse, pOrderBy->a[i].pExpr) ){ |
drh | 1d83f05 | 2002-02-17 00:30:36 +0000 | [diff] [blame] | 2467 | goto select_end; |
drh | 191b690 | 2000-06-08 11:13:01 +0000 | [diff] [blame] | 2468 | } |
| 2469 | } |
| 2470 | } |
drh | efb7251 | 2000-05-31 20:00:52 +0000 | [diff] [blame] | 2471 | } |
| 2472 | |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 2473 | /* Reset the aggregator |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 2474 | */ |
| 2475 | if( isAgg ){ |
danielk1977 | e159fdf | 2004-06-21 10:45:06 +0000 | [diff] [blame] | 2476 | int addr = sqlite3VdbeAddOp(v, OP_AggReset, (pGroupBy?0:1), pParse->nAgg); |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 2477 | for(i=0; i<pParse->nAgg; i++){ |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 2478 | FuncDef *pFunc; |
| 2479 | if( (pFunc = pParse->aAgg[i].pFunc)!=0 && pFunc->xFinalize!=0 ){ |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 2480 | sqlite3VdbeOp3(v, OP_AggInit, 0, i, (char*)pFunc, P3_FUNCDEF); |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 2481 | } |
| 2482 | } |
danielk1977 | e159fdf | 2004-06-21 10:45:06 +0000 | [diff] [blame] | 2483 | if( pGroupBy ){ |
danielk1977 | ce2663c | 2004-06-11 13:19:21 +0000 | [diff] [blame] | 2484 | int sz = sizeof(KeyInfo) + pGroupBy->nExpr*sizeof(CollSeq*); |
| 2485 | KeyInfo *pKey = (KeyInfo *)sqliteMalloc(sz); |
| 2486 | if( 0==pKey ){ |
| 2487 | goto select_end; |
| 2488 | } |
| 2489 | pKey->enc = pParse->db->enc; |
| 2490 | pKey->nField = pGroupBy->nExpr; |
| 2491 | for(i=0; i<pGroupBy->nExpr; i++){ |
| 2492 | pKey->aColl[i] = sqlite3ExprCollSeq(pParse, pGroupBy->a[i].pExpr); |
| 2493 | if( !pKey->aColl[i] ){ |
| 2494 | pKey->aColl[i] = pParse->db->pDfltColl; |
| 2495 | } |
| 2496 | } |
| 2497 | sqlite3VdbeChangeP3(v, addr, (char *)pKey, P3_KEYINFO_HANDOFF); |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 2498 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 2499 | } |
| 2500 | |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 2501 | /* Initialize the memory cell to NULL |
| 2502 | */ |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 2503 | if( eDest==SRT_Mem ){ |
danielk1977 | 0f69c1e | 2004-05-29 11:24:50 +0000 | [diff] [blame] | 2504 | sqlite3VdbeAddOp(v, OP_String8, 0, 0); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2505 | sqlite3VdbeAddOp(v, OP_MemStore, iParm, 1); |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 2506 | } |
| 2507 | |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 2508 | /* Open a temporary table to use for the distinct set. |
drh | efb7251 | 2000-05-31 20:00:52 +0000 | [diff] [blame] | 2509 | */ |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 2510 | if( isDistinct ){ |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 2511 | distinct = pParse->nTab++; |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 2512 | openTempIndex(pParse, p, distinct, 0); |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 2513 | }else{ |
| 2514 | distinct = -1; |
drh | efb7251 | 2000-05-31 20:00:52 +0000 | [diff] [blame] | 2515 | } |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 2516 | |
| 2517 | /* Begin the database scan |
| 2518 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2519 | pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, |
drh | 68d2e59 | 2002-08-04 00:52:38 +0000 | [diff] [blame] | 2520 | pGroupBy ? 0 : &pOrderBy); |
drh | 1d83f05 | 2002-02-17 00:30:36 +0000 | [diff] [blame] | 2521 | if( pWInfo==0 ) goto select_end; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 2522 | |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 2523 | /* Use the standard inner loop if we are not dealing with |
| 2524 | ** aggregates |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 2525 | */ |
drh | da9d6c4 | 2000-05-31 18:20:14 +0000 | [diff] [blame] | 2526 | if( !isAgg ){ |
drh | df199a2 | 2002-06-14 22:38:41 +0000 | [diff] [blame] | 2527 | if( selectInnerLoop(pParse, p, pEList, 0, 0, pOrderBy, distinct, eDest, |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 2528 | iParm, pWInfo->iContinue, pWInfo->iBreak, aff) ){ |
drh | 1d83f05 | 2002-02-17 00:30:36 +0000 | [diff] [blame] | 2529 | goto select_end; |
drh | da9d6c4 | 2000-05-31 18:20:14 +0000 | [diff] [blame] | 2530 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 2531 | } |
drh | efb7251 | 2000-05-31 20:00:52 +0000 | [diff] [blame] | 2532 | |
drh | e318474 | 2002-06-19 14:27:05 +0000 | [diff] [blame] | 2533 | /* If we are dealing with aggregates, then do the special aggregate |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 2534 | ** processing. |
drh | efb7251 | 2000-05-31 20:00:52 +0000 | [diff] [blame] | 2535 | */ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 2536 | else{ |
drh | 268380c | 2004-02-25 13:47:31 +0000 | [diff] [blame] | 2537 | AggExpr *pAgg; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 2538 | if( pGroupBy ){ |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 2539 | int lbl1; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 2540 | for(i=0; i<pGroupBy->nExpr; i++){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2541 | sqlite3ExprCode(pParse, pGroupBy->a[i].pExpr); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 2542 | } |
danielk1977 | ededfd5 | 2004-06-17 07:53:01 +0000 | [diff] [blame] | 2543 | /* No affinity string is attached to the following OP_MakeRecord |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 2544 | ** because we do not need to do any coercion of datatypes. */ |
danielk1977 | ededfd5 | 2004-06-17 07:53:01 +0000 | [diff] [blame] | 2545 | sqlite3VdbeAddOp(v, OP_MakeRecord, pGroupBy->nExpr, 0); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2546 | lbl1 = sqlite3VdbeMakeLabel(v); |
| 2547 | sqlite3VdbeAddOp(v, OP_AggFocus, 0, lbl1); |
drh | 268380c | 2004-02-25 13:47:31 +0000 | [diff] [blame] | 2548 | for(i=0, pAgg=pParse->aAgg; i<pParse->nAgg; i++, pAgg++){ |
| 2549 | if( pAgg->isAgg ) continue; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2550 | sqlite3ExprCode(pParse, pAgg->pExpr); |
| 2551 | sqlite3VdbeAddOp(v, OP_AggSet, 0, i); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 2552 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2553 | sqlite3VdbeResolveLabel(v, lbl1); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 2554 | } |
drh | 268380c | 2004-02-25 13:47:31 +0000 | [diff] [blame] | 2555 | for(i=0, pAgg=pParse->aAgg; i<pParse->nAgg; i++, pAgg++){ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 2556 | Expr *pE; |
drh | 268380c | 2004-02-25 13:47:31 +0000 | [diff] [blame] | 2557 | int nExpr; |
| 2558 | FuncDef *pDef; |
| 2559 | if( !pAgg->isAgg ) continue; |
| 2560 | assert( pAgg->pFunc!=0 ); |
| 2561 | assert( pAgg->pFunc->xStep!=0 ); |
| 2562 | pDef = pAgg->pFunc; |
| 2563 | pE = pAgg->pExpr; |
| 2564 | assert( pE!=0 ); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 2565 | assert( pE->op==TK_AGG_FUNCTION ); |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 2566 | nExpr = sqlite3ExprCodeExprList(pParse, pE->pList); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2567 | sqlite3VdbeAddOp(v, OP_Integer, i, 0); |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 2568 | if( pDef->needCollSeq ){ |
| 2569 | CollSeq *pColl = 0; |
| 2570 | int j; |
| 2571 | for(j=0; !pColl && j<nExpr; j++){ |
| 2572 | pColl = sqlite3ExprCollSeq(pParse, pE->pList->a[j].pExpr); |
| 2573 | } |
| 2574 | if( !pColl ) pColl = pParse->db->pDfltColl; |
| 2575 | sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ); |
| 2576 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2577 | sqlite3VdbeOp3(v, OP_AggFunc, 0, nExpr, (char*)pDef, P3_POINTER); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 2578 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 2579 | } |
| 2580 | |
| 2581 | /* End the database scan loop. |
| 2582 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2583 | sqlite3WhereEnd(pWInfo); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 2584 | |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 2585 | /* If we are processing aggregates, we need to set up a second loop |
| 2586 | ** over all of the aggregate values and process them. |
| 2587 | */ |
| 2588 | if( isAgg ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2589 | int endagg = sqlite3VdbeMakeLabel(v); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 2590 | int startagg; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2591 | startagg = sqlite3VdbeAddOp(v, OP_AggNext, 0, endagg); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 2592 | pParse->useAgg = 1; |
| 2593 | if( pHaving ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2594 | sqlite3ExprIfFalse(pParse, pHaving, startagg, 1); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 2595 | } |
drh | df199a2 | 2002-06-14 22:38:41 +0000 | [diff] [blame] | 2596 | if( selectInnerLoop(pParse, p, pEList, 0, 0, pOrderBy, distinct, eDest, |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 2597 | iParm, startagg, endagg, aff) ){ |
drh | 1d83f05 | 2002-02-17 00:30:36 +0000 | [diff] [blame] | 2598 | goto select_end; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 2599 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2600 | sqlite3VdbeAddOp(v, OP_Goto, 0, startagg); |
| 2601 | sqlite3VdbeResolveLabel(v, endagg); |
| 2602 | sqlite3VdbeAddOp(v, OP_Noop, 0, 0); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 2603 | pParse->useAgg = 0; |
| 2604 | } |
| 2605 | |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 2606 | /* If there is an ORDER BY clause, then we need to sort the results |
| 2607 | ** and send them to the callback one by one. |
| 2608 | */ |
| 2609 | if( pOrderBy ){ |
drh | ffbc308 | 2004-05-21 01:29:06 +0000 | [diff] [blame] | 2610 | generateSortTail(pParse, p, v, pEList->nExpr, eDest, iParm); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 2611 | } |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 2612 | |
drh | f620b4e | 2004-02-09 14:37:50 +0000 | [diff] [blame] | 2613 | /* If this was a subquery, we have now converted the subquery into a |
| 2614 | ** temporary table. So delete the subquery structure from the parent |
| 2615 | ** to prevent this subquery from being evaluated again and to force the |
| 2616 | ** the use of the temporary table. |
| 2617 | */ |
| 2618 | if( pParent ){ |
| 2619 | assert( pParent->pSrc->nSrc>parentTab ); |
| 2620 | assert( pParent->pSrc->a[parentTab].pSelect==p ); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2621 | sqlite3SelectDelete(p); |
drh | f620b4e | 2004-02-09 14:37:50 +0000 | [diff] [blame] | 2622 | pParent->pSrc->a[parentTab].pSelect = 0; |
| 2623 | } |
| 2624 | |
drh | 1d83f05 | 2002-02-17 00:30:36 +0000 | [diff] [blame] | 2625 | /* The SELECT was successfully coded. Set the return code to 0 |
| 2626 | ** to indicate no errors. |
| 2627 | */ |
| 2628 | rc = 0; |
| 2629 | |
| 2630 | /* Control jumps to here if an error is encountered above, or upon |
| 2631 | ** successful coding of the SELECT. |
| 2632 | */ |
| 2633 | select_end: |
| 2634 | sqliteAggregateInfoReset(pParse); |
| 2635 | return rc; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 2636 | } |