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