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