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