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