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