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