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