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