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 | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame^] | 15 | ** $Id: select.c,v 1.37 2001/09/16 00:13:27 drh Exp $ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 16 | */ |
| 17 | #include "sqliteInt.h" |
| 18 | |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 19 | /* |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 20 | ** Allocate a new Select structure and return a pointer to that |
| 21 | ** structure. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 22 | */ |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 23 | Select *sqliteSelectNew( |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 24 | ExprList *pEList, /* which columns to include in the result */ |
| 25 | IdList *pSrc, /* the FROM clause -- which tables to scan */ |
| 26 | Expr *pWhere, /* the WHERE clause */ |
| 27 | ExprList *pGroupBy, /* the GROUP BY clause */ |
| 28 | Expr *pHaving, /* the HAVING clause */ |
| 29 | ExprList *pOrderBy, /* the ORDER BY clause */ |
| 30 | int isDistinct /* true if the DISTINCT keyword is present */ |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 31 | ){ |
| 32 | Select *pNew; |
| 33 | pNew = sqliteMalloc( sizeof(*pNew) ); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 34 | if( pNew==0 ){ |
| 35 | sqliteExprListDelete(pEList); |
| 36 | sqliteIdListDelete(pSrc); |
| 37 | sqliteExprDelete(pWhere); |
| 38 | sqliteExprListDelete(pGroupBy); |
| 39 | sqliteExprDelete(pHaving); |
| 40 | sqliteExprListDelete(pOrderBy); |
| 41 | }else{ |
| 42 | pNew->pEList = pEList; |
| 43 | pNew->pSrc = pSrc; |
| 44 | pNew->pWhere = pWhere; |
| 45 | pNew->pGroupBy = pGroupBy; |
| 46 | pNew->pHaving = pHaving; |
| 47 | pNew->pOrderBy = pOrderBy; |
| 48 | pNew->isDistinct = isDistinct; |
| 49 | pNew->op = TK_SELECT; |
| 50 | } |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 51 | return pNew; |
| 52 | } |
| 53 | |
| 54 | /* |
| 55 | ** Delete the given Select structure and all of its substructures. |
| 56 | */ |
| 57 | void sqliteSelectDelete(Select *p){ |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 58 | if( p==0 ) return; |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 59 | sqliteExprListDelete(p->pEList); |
| 60 | sqliteIdListDelete(p->pSrc); |
| 61 | sqliteExprDelete(p->pWhere); |
| 62 | sqliteExprListDelete(p->pGroupBy); |
| 63 | sqliteExprDelete(p->pHaving); |
| 64 | sqliteExprListDelete(p->pOrderBy); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 65 | sqliteSelectDelete(p->pPrior); |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 66 | sqliteFree(p); |
| 67 | } |
| 68 | |
| 69 | /* |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 70 | ** Delete the aggregate information from the parse structure. |
| 71 | */ |
| 72 | void sqliteParseInfoReset(Parse *pParse){ |
| 73 | sqliteFree(pParse->aAgg); |
| 74 | pParse->aAgg = 0; |
| 75 | pParse->nAgg = 0; |
| 76 | pParse->iAggCount = -1; |
| 77 | pParse->useAgg = 0; |
| 78 | } |
| 79 | |
| 80 | /* |
| 81 | ** This routine generates the code for the inside of the inner loop |
| 82 | ** of a SELECT. |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 83 | ** |
| 84 | ** The pEList is used to determine the values for each column in the |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 85 | ** result row. Except if pEList==NULL, then we just read nColumn |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 86 | ** elements from the srcTab table. |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 87 | */ |
| 88 | static int selectInnerLoop( |
| 89 | Parse *pParse, /* The parser context */ |
| 90 | ExprList *pEList, /* List of values being extracted */ |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 91 | int srcTab, /* Pull data from this table */ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 92 | int nColumn, /* Number of columns in the source table */ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 93 | ExprList *pOrderBy, /* If not NULL, sort results using this key */ |
| 94 | int distinct, /* If >=0, make sure results are distinct */ |
| 95 | int eDest, /* How to dispose of the results */ |
| 96 | int iParm, /* An argument to the disposal method */ |
| 97 | int iContinue, /* Jump here to continue with next row */ |
| 98 | int iBreak /* Jump here to break out of the inner loop */ |
| 99 | ){ |
| 100 | Vdbe *v = pParse->pVdbe; |
| 101 | int i; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 102 | if( v==0 ) return 0; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 103 | |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 104 | /* Pull the requested columns. |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 105 | */ |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 106 | if( pEList ){ |
| 107 | for(i=0; i<pEList->nExpr; i++){ |
| 108 | sqliteExprCode(pParse, pEList->a[i].pExpr); |
| 109 | } |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 110 | nColumn = pEList->nExpr; |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 111 | }else{ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 112 | for(i=0; i<nColumn; i++){ |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 113 | sqliteVdbeAddOp(v, OP_Column, srcTab, i, 0, 0); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 114 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 115 | } |
| 116 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 117 | /* If the DISTINCT keyword was present on the SELECT statement |
| 118 | ** and this row has been seen before, then do not make this row |
| 119 | ** part of the result. |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 120 | */ |
| 121 | if( distinct>=0 ){ |
| 122 | int lbl = sqliteVdbeMakeLabel(v); |
| 123 | sqliteVdbeAddOp(v, OP_MakeKey, pEList->nExpr, 1, 0, 0); |
| 124 | sqliteVdbeAddOp(v, OP_Distinct, distinct, lbl, 0, 0); |
| 125 | sqliteVdbeAddOp(v, OP_Pop, pEList->nExpr+1, 0, 0, 0); |
| 126 | sqliteVdbeAddOp(v, OP_Goto, 0, iContinue, 0, 0); |
| 127 | sqliteVdbeAddOp(v, OP_String, 0, 0, "", lbl); |
| 128 | sqliteVdbeAddOp(v, OP_Put, distinct, 0, 0, 0); |
| 129 | } |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 130 | |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 131 | /* If there is an ORDER BY clause, then store the results |
| 132 | ** in a sorter. |
| 133 | */ |
| 134 | if( pOrderBy ){ |
| 135 | char *zSortOrder; |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 136 | sqliteVdbeAddOp(v, OP_SortMakeRec, nColumn, 0, 0, 0); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 137 | zSortOrder = sqliteMalloc( pOrderBy->nExpr + 1 ); |
| 138 | if( zSortOrder==0 ) return 1; |
| 139 | for(i=0; i<pOrderBy->nExpr; i++){ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 140 | zSortOrder[i] = pOrderBy->a[i].sortOrder ? '-' : '+'; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 141 | sqliteExprCode(pParse, pOrderBy->a[i].pExpr); |
| 142 | } |
| 143 | zSortOrder[pOrderBy->nExpr] = 0; |
| 144 | sqliteVdbeAddOp(v, OP_SortMakeKey, pOrderBy->nExpr, 0, zSortOrder, 0); |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 145 | sqliteFree(zSortOrder); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 146 | sqliteVdbeAddOp(v, OP_SortPut, 0, 0, 0, 0); |
| 147 | }else |
| 148 | |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 149 | /* In this mode, write each query result to the key of the temporary |
| 150 | ** table iParm. |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 151 | */ |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 152 | if( eDest==SRT_Union ){ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 153 | sqliteVdbeAddOp(v, OP_MakeRecord, nColumn, 0, 0, 0); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 154 | sqliteVdbeAddOp(v, OP_String, iParm, 0, "", 0); |
| 155 | sqliteVdbeAddOp(v, OP_Put, iParm, 0, 0, 0); |
| 156 | }else |
| 157 | |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 158 | /* Store the result as data using a unique key. |
| 159 | */ |
| 160 | if( eDest==SRT_Table ){ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 161 | sqliteVdbeAddOp(v, OP_MakeRecord, nColumn, 0, 0, 0); |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 162 | sqliteVdbeAddOp(v, OP_NewRecno, iParm, 0, 0, 0); |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 163 | sqliteVdbeAddOp(v, OP_Pull, 1, 0, 0, 0); |
| 164 | sqliteVdbeAddOp(v, OP_Put, iParm, 0, 0, 0); |
| 165 | }else |
| 166 | |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 167 | /* Construct a record from the query result, but instead of |
| 168 | ** saving that record, use it as a key to delete elements from |
| 169 | ** the temporary table iParm. |
| 170 | */ |
| 171 | if( eDest==SRT_Except ){ |
drh | 3fc190c | 2001-09-14 03:24:23 +0000 | [diff] [blame] | 172 | int addr = sqliteVdbeAddOp(v, OP_MakeRecord, nColumn, 0, 0, 0); |
| 173 | sqliteVdbeAddOp(v, OP_NotFound, iParm, addr+3, 0, 0); |
drh | 1ecec3c | 2000-06-06 22:13:55 +0000 | [diff] [blame] | 174 | sqliteVdbeAddOp(v, OP_Delete, iParm, 0, 0, 0); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 175 | }else |
| 176 | |
| 177 | /* If we are creating a set for an "expr IN (SELECT ...)" construct, |
| 178 | ** then there should be a single item on the stack. Write this |
| 179 | ** item into the set table with bogus data. |
| 180 | */ |
| 181 | if( eDest==SRT_Set ){ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 182 | assert( nColumn==1 ); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 183 | sqliteVdbeAddOp(v, OP_String, 0, 0, "", 0); |
| 184 | sqliteVdbeAddOp(v, OP_Put, iParm, 0, 0, 0); |
| 185 | }else |
| 186 | |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 187 | |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 188 | /* If this is a scalar select that is part of an expression, then |
| 189 | ** store the results in the appropriate memory cell and break out |
| 190 | ** of the scan loop. |
| 191 | */ |
| 192 | if( eDest==SRT_Mem ){ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 193 | assert( nColumn==1 ); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 194 | sqliteVdbeAddOp(v, OP_MemStore, iParm, 0, 0, 0); |
| 195 | sqliteVdbeAddOp(v, OP_Goto, 0, iBreak, 0, 0); |
| 196 | }else |
| 197 | |
| 198 | /* If none of the above, send the data to the callback function. |
| 199 | */ |
| 200 | { |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 201 | sqliteVdbeAddOp(v, OP_Callback, nColumn, 0, 0, 0); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 202 | } |
| 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | /* |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 207 | ** If the inner loop was generated using a non-null pOrderBy argument, |
| 208 | ** then the results were placed in a sorter. After the loop is terminated |
| 209 | ** we need to run the sorter and output the results. The following |
| 210 | ** routine generates the code needed to do that. |
| 211 | */ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 212 | static void generateSortTail(Vdbe *v, int nColumn){ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 213 | int end = sqliteVdbeMakeLabel(v); |
| 214 | int addr; |
| 215 | sqliteVdbeAddOp(v, OP_Sort, 0, 0, 0, 0); |
| 216 | addr = sqliteVdbeAddOp(v, OP_SortNext, 0, end, 0, 0); |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 217 | sqliteVdbeAddOp(v, OP_SortCallback, nColumn, 0, 0, 0); |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 218 | sqliteVdbeAddOp(v, OP_Goto, 0, addr, 0, 0); |
| 219 | sqliteVdbeAddOp(v, OP_SortClose, 0, 0, 0, end); |
| 220 | } |
| 221 | |
| 222 | /* |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 223 | ** Generate code that will tell the VDBE how many columns there |
| 224 | ** are in the result and the name for each column. This information |
| 225 | ** is used to provide "argc" and "azCol[]" values in the callback. |
| 226 | */ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 227 | static |
| 228 | void generateColumnNames(Parse *pParse, IdList *pTabList, ExprList *pEList){ |
| 229 | Vdbe *v = pParse->pVdbe; |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 230 | int i; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 231 | if( pParse->colNamesSet || v==0 || sqlite_malloc_failed ) return; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 232 | pParse->colNamesSet = 1; |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 233 | sqliteVdbeAddOp(v, OP_ColumnCount, pEList->nExpr, 0, 0, 0); |
| 234 | for(i=0; i<pEList->nExpr; i++){ |
| 235 | Expr *p; |
drh | e1b6a5b | 2000-07-29 13:06:59 +0000 | [diff] [blame] | 236 | int addr; |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 237 | if( pEList->a[i].zName ){ |
| 238 | char *zName = pEList->a[i].zName; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 239 | sqliteVdbeAddOp(v, OP_ColumnName, i, 0, zName, 0); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 240 | continue; |
| 241 | } |
| 242 | p = pEList->a[i].pExpr; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 243 | if( p==0 ) continue; |
drh | e1b6a5b | 2000-07-29 13:06:59 +0000 | [diff] [blame] | 244 | if( p->span.z && p->span.z[0] ){ |
| 245 | addr = sqliteVdbeAddOp(v,OP_ColumnName, i, 0, 0, 0); |
| 246 | sqliteVdbeChangeP3(v, addr, p->span.z, p->span.n); |
| 247 | sqliteVdbeCompressSpace(v, addr); |
| 248 | }else if( p->op!=TK_COLUMN || pTabList==0 ){ |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 249 | char zName[30]; |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 250 | sprintf(zName, "column%d", i+1); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 251 | sqliteVdbeAddOp(v, OP_ColumnName, i, 0, zName, 0); |
| 252 | }else{ |
| 253 | if( pTabList->nId>1 ){ |
| 254 | char *zName = 0; |
| 255 | Table *pTab = pTabList->a[p->iTable].pTab; |
| 256 | char *zTab; |
| 257 | |
| 258 | zTab = pTabList->a[p->iTable].zAlias; |
| 259 | if( zTab==0 ) zTab = pTab->zName; |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 260 | sqliteSetString(&zName, zTab, ".", pTab->aCol[p->iColumn].zName, 0); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 261 | sqliteVdbeAddOp(v, OP_ColumnName, i, 0, zName, 0); |
| 262 | sqliteFree(zName); |
| 263 | }else{ |
| 264 | Table *pTab = pTabList->a[0].pTab; |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 265 | char *zName = pTab->aCol[p->iColumn].zName; |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 266 | sqliteVdbeAddOp(v, OP_ColumnName, i, 0, zName, 0); |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | /* |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 273 | ** Name of the connection operator, used for error messages. |
| 274 | */ |
| 275 | static const char *selectOpName(int id){ |
| 276 | char *z; |
| 277 | switch( id ){ |
| 278 | case TK_ALL: z = "UNION ALL"; break; |
| 279 | case TK_INTERSECT: z = "INTERSECT"; break; |
| 280 | case TK_EXCEPT: z = "EXCEPT"; break; |
| 281 | default: z = "UNION"; break; |
| 282 | } |
| 283 | return z; |
| 284 | } |
| 285 | |
| 286 | /* |
| 287 | ** For the given SELECT statement, do two things. |
| 288 | ** |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 289 | ** (1) Fill in the pTabList->a[].pTab fields in the IdList that |
| 290 | ** defines the set of tables that should be scanned. |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 291 | ** |
| 292 | ** (2) If the columns to be extracted variable (pEList) is NULL |
| 293 | ** (meaning that a "*" was used in the SQL statement) then |
| 294 | ** create a fake pEList containing the names of all columns |
| 295 | ** of all tables. |
| 296 | ** |
| 297 | ** Return 0 on success. If there are problems, leave an error message |
| 298 | ** in pParse and return non-zero. |
| 299 | */ |
| 300 | static int fillInColumnList(Parse *pParse, Select *p){ |
| 301 | int i, j; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 302 | IdList *pTabList; |
| 303 | ExprList *pEList; |
| 304 | |
| 305 | if( p==0 || p->pSrc==0 ) return 1; |
| 306 | pTabList = p->pSrc; |
| 307 | pEList = p->pEList; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 308 | |
| 309 | /* Look up every table in the table list. |
| 310 | */ |
| 311 | for(i=0; i<pTabList->nId; i++){ |
| 312 | if( pTabList->a[i].pTab ){ |
| 313 | /* This routine has run before! No need to continue */ |
| 314 | return 0; |
| 315 | } |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 316 | if( pTabList->a[i].zName==0 ){ |
| 317 | /* No table name is given. Instead, there is a (SELECT ...) statement |
| 318 | ** the results of which should be used in place of the table. The |
| 319 | ** was this is implemented is that the (SELECT ...) writes its results |
| 320 | ** into a temporary table which is then scanned like any other table. |
| 321 | */ |
| 322 | sqliteSetString(&pParse->zErrMsg, |
| 323 | "(SELECT...) in a FROM clause is not yet implemented.", 0); |
| 324 | pParse->nErr++; |
| 325 | return 1; |
| 326 | } |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 327 | pTabList->a[i].pTab = sqliteFindTable(pParse->db, pTabList->a[i].zName); |
| 328 | if( pTabList->a[i].pTab==0 ){ |
| 329 | sqliteSetString(&pParse->zErrMsg, "no such table: ", |
| 330 | pTabList->a[i].zName, 0); |
| 331 | pParse->nErr++; |
| 332 | return 1; |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | /* If the list of columns to retrieve is "*" then replace it with |
| 337 | ** a list of all columns from all tables. |
| 338 | */ |
| 339 | if( pEList==0 ){ |
| 340 | for(i=0; i<pTabList->nId; i++){ |
| 341 | Table *pTab = pTabList->a[i].pTab; |
| 342 | for(j=0; j<pTab->nCol; j++){ |
| 343 | Expr *pExpr = sqliteExpr(TK_DOT, 0, 0, 0); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 344 | if( pExpr==0 ) break; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 345 | pExpr->pLeft = sqliteExpr(TK_ID, 0, 0, 0); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 346 | if( pExpr->pLeft==0 ) break; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 347 | pExpr->pLeft->token.z = pTab->zName; |
| 348 | pExpr->pLeft->token.n = strlen(pTab->zName); |
| 349 | pExpr->pRight = sqliteExpr(TK_ID, 0, 0, 0); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 350 | if( pExpr->pRight==0 ) break; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 351 | pExpr->pRight->token.z = pTab->aCol[j].zName; |
| 352 | pExpr->pRight->token.n = strlen(pTab->aCol[j].zName); |
drh | e1b6a5b | 2000-07-29 13:06:59 +0000 | [diff] [blame] | 353 | pExpr->span.z = ""; |
| 354 | pExpr->span.n = 0; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 355 | pEList = sqliteExprListAppend(pEList, pExpr, 0); |
| 356 | } |
| 357 | } |
| 358 | p->pEList = pEList; |
| 359 | } |
| 360 | return 0; |
| 361 | } |
| 362 | |
| 363 | /* |
| 364 | ** This routine associates entries in an ORDER BY expression list with |
| 365 | ** columns in a result. For each ORDER BY expression, the opcode of |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 366 | ** 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] | 367 | ** the top-level node is filled in with column number and the iTable |
| 368 | ** value of the top-level node is filled with iTable parameter. |
| 369 | ** |
| 370 | ** If there are prior SELECT clauses, they are processed first. A match |
| 371 | ** in an earlier SELECT takes precedence over a later SELECT. |
| 372 | ** |
| 373 | ** Any entry that does not match is flagged as an error. The number |
| 374 | ** of errors is returned. |
| 375 | */ |
| 376 | static int matchOrderbyToColumn( |
| 377 | Parse *pParse, /* A place to leave error messages */ |
| 378 | Select *pSelect, /* Match to result columns of this SELECT */ |
| 379 | ExprList *pOrderBy, /* The ORDER BY values to match against columns */ |
| 380 | int iTable, /* Insert this this value in iTable */ |
| 381 | int mustComplete /* If TRUE all ORDER BYs must match */ |
| 382 | ){ |
| 383 | int nErr = 0; |
| 384 | int i, j; |
| 385 | ExprList *pEList; |
| 386 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 387 | if( pSelect==0 || pOrderBy==0 ) return 1; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 388 | if( mustComplete ){ |
| 389 | for(i=0; i<pOrderBy->nExpr; i++){ pOrderBy->a[i].done = 0; } |
| 390 | } |
| 391 | if( fillInColumnList(pParse, pSelect) ){ |
| 392 | return 1; |
| 393 | } |
| 394 | if( pSelect->pPrior ){ |
drh | 92cd52f | 2000-06-08 01:55:29 +0000 | [diff] [blame] | 395 | if( matchOrderbyToColumn(pParse, pSelect->pPrior, pOrderBy, iTable, 0) ){ |
| 396 | return 1; |
| 397 | } |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 398 | } |
| 399 | pEList = pSelect->pEList; |
| 400 | for(i=0; i<pOrderBy->nExpr; i++){ |
| 401 | Expr *pE = pOrderBy->a[i].pExpr; |
drh | 92cd52f | 2000-06-08 01:55:29 +0000 | [diff] [blame] | 402 | int match = 0; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 403 | if( pOrderBy->a[i].done ) continue; |
| 404 | for(j=0; j<pEList->nExpr; j++){ |
drh | 4cfa793 | 2000-06-08 15:10:46 +0000 | [diff] [blame] | 405 | if( pEList->a[j].zName && (pE->op==TK_ID || pE->op==TK_STRING) ){ |
| 406 | char *zName = pEList->a[j].zName; |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 407 | char *zLabel = sqliteStrNDup(pE->token.z, pE->token.n); |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 408 | sqliteDequote(zLabel); |
| 409 | if( sqliteStrICmp(zName, zLabel)==0 ){ |
| 410 | match = 1; |
| 411 | } |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 412 | sqliteFree(zLabel); |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 413 | } |
drh | 4cfa793 | 2000-06-08 15:10:46 +0000 | [diff] [blame] | 414 | if( match==0 && sqliteExprCompare(pE, pEList->a[j].pExpr) ){ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 415 | match = 1; |
| 416 | } |
| 417 | if( match ){ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 418 | pE->op = TK_COLUMN; |
| 419 | pE->iColumn = j; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 420 | pE->iTable = iTable; |
| 421 | pOrderBy->a[i].done = 1; |
| 422 | break; |
| 423 | } |
| 424 | } |
drh | 92cd52f | 2000-06-08 01:55:29 +0000 | [diff] [blame] | 425 | if( !match && mustComplete ){ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 426 | char zBuf[30]; |
| 427 | sprintf(zBuf,"%d",i+1); |
| 428 | sqliteSetString(&pParse->zErrMsg, "ORDER BY term number ", zBuf, |
| 429 | " does not match any result column", 0); |
| 430 | pParse->nErr++; |
| 431 | nErr++; |
| 432 | break; |
| 433 | } |
| 434 | } |
| 435 | return nErr; |
| 436 | } |
| 437 | |
| 438 | /* |
| 439 | ** Get a VDBE for the given parser context. Create a new one if necessary. |
| 440 | ** If an error occurs, return NULL and leave a message in pParse. |
| 441 | */ |
| 442 | Vdbe *sqliteGetVdbe(Parse *pParse){ |
| 443 | Vdbe *v = pParse->pVdbe; |
| 444 | if( v==0 ){ |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 445 | v = pParse->pVdbe = sqliteVdbeCreate(pParse->db); |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 446 | } |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 447 | return v; |
| 448 | } |
| 449 | |
| 450 | |
| 451 | /* |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 452 | ** This routine is called to process a query that is really the union |
| 453 | ** or intersection of two or more separate queries. |
| 454 | */ |
| 455 | static int multiSelect(Parse *pParse, Select *p, int eDest, int iParm){ |
drh | 10e5e3c | 2000-06-08 00:19:02 +0000 | [diff] [blame] | 456 | int rc; /* Success code from a subroutine */ |
| 457 | Select *pPrior; /* Another SELECT immediately to our left */ |
| 458 | Vdbe *v; /* Generate code to this VDBE */ |
| 459 | int base; /* Baseline value for pParse->nTab */ |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 460 | |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 461 | /* Make sure there is no ORDER BY clause on prior SELECTs. Only the |
| 462 | ** last SELECT in the series may have an ORDER BY. |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 463 | */ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 464 | if( p==0 || p->pPrior==0 ) return 1; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 465 | pPrior = p->pPrior; |
| 466 | if( pPrior->pOrderBy ){ |
| 467 | sqliteSetString(&pParse->zErrMsg,"ORDER BY clause should come after ", |
| 468 | selectOpName(p->op), " not before", 0); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 469 | pParse->nErr++; |
| 470 | return 1; |
| 471 | } |
| 472 | |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 473 | /* Make sure we have a valid query engine. If not, create a new one. |
| 474 | */ |
| 475 | v = sqliteGetVdbe(pParse); |
| 476 | if( v==0 ) return 1; |
| 477 | |
| 478 | /* Process the UNION or INTERSECTION |
| 479 | */ |
drh | 10e5e3c | 2000-06-08 00:19:02 +0000 | [diff] [blame] | 480 | base = pParse->nTab; |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 481 | switch( p->op ){ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 482 | case TK_ALL: |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 483 | case TK_EXCEPT: |
| 484 | case TK_UNION: { |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 485 | int unionTab; /* Cursor number of the temporary table holding result */ |
| 486 | int op; /* One of the SRT_ operations to apply to self */ |
| 487 | int priorOp; /* The SRT_ operation to apply to prior selects */ |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 488 | |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 489 | priorOp = p->op==TK_ALL ? SRT_Table : SRT_Union; |
| 490 | if( eDest==priorOp ){ |
| 491 | /* We can reuse a temporary table generated by a SELECT to our |
| 492 | ** right. This also means we are not the right-most select and so |
| 493 | ** we cannot have an ORDER BY clause |
| 494 | */ |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 495 | unionTab = iParm; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 496 | assert( p->pOrderBy==0 ); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 497 | }else{ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 498 | /* We will need to create our own temporary table to hold the |
| 499 | ** intermediate results. |
| 500 | */ |
| 501 | unionTab = pParse->nTab++; |
| 502 | if( p->pOrderBy |
| 503 | && matchOrderbyToColumn(pParse, p, p->pOrderBy, unionTab, 1) ){ |
| 504 | return 1; |
| 505 | } |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 506 | if( p->op!=TK_ALL ){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 507 | sqliteVdbeAddOp(v, OP_OpenTemp, unionTab, 0, 0, 0); |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 508 | sqliteVdbeAddOp(v, OP_KeyAsData, unionTab, 1, 0, 0); |
drh | 345fda3 | 2001-01-15 22:51:08 +0000 | [diff] [blame] | 509 | }else{ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 510 | sqliteVdbeAddOp(v, OP_OpenTemp, unionTab, 0, 0, 0); |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 511 | } |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 512 | } |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 513 | |
| 514 | /* Code the SELECT statements to our left |
| 515 | */ |
| 516 | rc = sqliteSelect(pParse, pPrior, priorOp, unionTab); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 517 | if( rc ) return rc; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 518 | |
| 519 | /* Code the current SELECT statement |
| 520 | */ |
| 521 | switch( p->op ){ |
| 522 | case TK_EXCEPT: op = SRT_Except; break; |
| 523 | case TK_UNION: op = SRT_Union; break; |
| 524 | case TK_ALL: op = SRT_Table; break; |
| 525 | } |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 526 | p->pPrior = 0; |
| 527 | rc = sqliteSelect(pParse, p, op, unionTab); |
| 528 | p->pPrior = pPrior; |
| 529 | if( rc ) return rc; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 530 | |
| 531 | /* Convert the data in the temporary table into whatever form |
| 532 | ** it is that we currently need. |
| 533 | */ |
| 534 | if( eDest!=priorOp ){ |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 535 | int iCont, iBreak; |
| 536 | assert( p->pEList ); |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 537 | generateColumnNames(pParse, 0, p->pEList); |
drh | 92dba24 | 2000-06-08 00:28:51 +0000 | [diff] [blame] | 538 | if( p->pOrderBy ){ |
| 539 | sqliteVdbeAddOp(v, OP_SortOpen, 0, 0, 0, 0); |
| 540 | } |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 541 | sqliteVdbeAddOp(v, OP_Rewind, unionTab, 0, 0, 0); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 542 | iBreak = sqliteVdbeMakeLabel(v); |
| 543 | iCont = sqliteVdbeAddOp(v, OP_Next, unionTab, iBreak, 0, 0); |
| 544 | rc = selectInnerLoop(pParse, 0, unionTab, p->pEList->nExpr, |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 545 | p->pOrderBy, -1, eDest, iParm, |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 546 | iCont, iBreak); |
| 547 | if( rc ) return 1; |
| 548 | sqliteVdbeAddOp(v, OP_Goto, 0, iCont, 0, 0); |
| 549 | sqliteVdbeAddOp(v, OP_Close, unionTab, 0, 0, iBreak); |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 550 | if( p->pOrderBy ){ |
| 551 | generateSortTail(v, p->pEList->nExpr); |
| 552 | } |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 553 | } |
| 554 | break; |
| 555 | } |
| 556 | case TK_INTERSECT: { |
| 557 | int tab1, tab2; |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 558 | int iCont, iBreak; |
| 559 | |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 560 | /* INTERSECT is different from the others since it requires |
drh | 6206d50 | 2000-06-19 19:09:08 +0000 | [diff] [blame] | 561 | ** two temporary tables. Hence it has its own case. Begin |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 562 | ** by allocating the tables we will need. |
| 563 | */ |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 564 | tab1 = pParse->nTab++; |
| 565 | tab2 = pParse->nTab++; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 566 | if( p->pOrderBy && matchOrderbyToColumn(pParse,p,p->pOrderBy,tab1,1) ){ |
| 567 | return 1; |
| 568 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 569 | sqliteVdbeAddOp(v, OP_OpenTemp, tab1, 0, 0, 0); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 570 | sqliteVdbeAddOp(v, OP_KeyAsData, tab1, 1, 0, 0); |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 571 | |
| 572 | /* Code the SELECTs to our left into temporary table "tab1". |
| 573 | */ |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 574 | rc = sqliteSelect(pParse, pPrior, SRT_Union, tab1); |
| 575 | if( rc ) return rc; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 576 | |
| 577 | /* Code the current SELECT into temporary table "tab2" |
| 578 | */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 579 | sqliteVdbeAddOp(v, OP_OpenTemp, tab2, 0, 0, 0); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 580 | sqliteVdbeAddOp(v, OP_KeyAsData, tab2, 1, 0, 0); |
| 581 | p->pPrior = 0; |
| 582 | rc = sqliteSelect(pParse, p, SRT_Union, tab2); |
| 583 | p->pPrior = pPrior; |
| 584 | if( rc ) return rc; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 585 | |
| 586 | /* Generate code to take the intersection of the two temporary |
| 587 | ** tables. |
| 588 | */ |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 589 | assert( p->pEList ); |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 590 | generateColumnNames(pParse, 0, p->pEList); |
drh | 92dba24 | 2000-06-08 00:28:51 +0000 | [diff] [blame] | 591 | if( p->pOrderBy ){ |
| 592 | sqliteVdbeAddOp(v, OP_SortOpen, 0, 0, 0, 0); |
| 593 | } |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 594 | sqliteVdbeAddOp(v, OP_Rewind, tab1, 0, 0, 0); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 595 | iBreak = sqliteVdbeMakeLabel(v); |
| 596 | iCont = sqliteVdbeAddOp(v, OP_Next, tab1, iBreak, 0, 0); |
drh | 573bd27 | 2001-02-19 23:23:38 +0000 | [diff] [blame] | 597 | sqliteVdbeAddOp(v, OP_FullKey, tab1, 0, 0, 0); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 598 | sqliteVdbeAddOp(v, OP_NotFound, tab2, iCont, 0, 0); |
| 599 | rc = selectInnerLoop(pParse, 0, tab1, p->pEList->nExpr, |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 600 | p->pOrderBy, -1, eDest, iParm, |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 601 | iCont, iBreak); |
| 602 | if( rc ) return 1; |
| 603 | sqliteVdbeAddOp(v, OP_Goto, 0, iCont, 0, 0); |
| 604 | sqliteVdbeAddOp(v, OP_Close, tab2, 0, 0, iBreak); |
| 605 | sqliteVdbeAddOp(v, OP_Close, tab1, 0, 0, 0); |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 606 | if( p->pOrderBy ){ |
| 607 | generateSortTail(v, p->pEList->nExpr); |
| 608 | } |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 609 | break; |
| 610 | } |
| 611 | } |
| 612 | assert( p->pEList && pPrior->pEList ); |
| 613 | if( p->pEList->nExpr!=pPrior->pEList->nExpr ){ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 614 | sqliteSetString(&pParse->zErrMsg, "SELECTs to the left and right of ", |
| 615 | selectOpName(p->op), " do not have the same number of result columns", 0); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 616 | pParse->nErr++; |
| 617 | return 1; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 618 | } |
drh | 10e5e3c | 2000-06-08 00:19:02 +0000 | [diff] [blame] | 619 | pParse->nTab = base; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 620 | return 0; |
| 621 | } |
| 622 | |
| 623 | /* |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 624 | ** Generate code for the given SELECT statement. |
| 625 | ** |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 626 | ** The results are distributed in various ways depending on the |
| 627 | ** value of eDest and iParm. |
| 628 | ** |
| 629 | ** eDest Value Result |
| 630 | ** ------------ ------------------------------------------- |
| 631 | ** SRT_Callback Invoke the callback for each row of the result. |
| 632 | ** |
| 633 | ** SRT_Mem Store first result in memory cell iParm |
| 634 | ** |
| 635 | ** SRT_Set Store results as keys of a table with cursor iParm |
| 636 | ** |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 637 | ** SRT_Union Store results as a key in a temporary table iParm |
| 638 | ** |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 639 | ** SRT_Except Remove results form the temporary table iParm. |
| 640 | ** |
| 641 | ** SRT_Table Store results in temporary table iParm |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 642 | ** |
| 643 | ** This routine returns the number of errors. If any errors are |
| 644 | ** encountered, then an appropriate error message is left in |
| 645 | ** pParse->zErrMsg. |
| 646 | ** |
| 647 | ** This routine does NOT free the Select structure passed in. The |
| 648 | ** calling function needs to do that. |
| 649 | */ |
| 650 | int sqliteSelect( |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 651 | Parse *pParse, /* The parser context */ |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 652 | Select *p, /* The SELECT statement being coded. */ |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 653 | int eDest, /* One of: SRT_Callback Mem Set Union Except */ |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 654 | int iParm /* Save result in this memory location, if >=0 */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 655 | ){ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 656 | int i; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 657 | WhereInfo *pWInfo; |
| 658 | Vdbe *v; |
| 659 | int isAgg = 0; /* True for select lists like "count(*)" */ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 660 | ExprList *pEList; /* List of columns to extract. NULL means "*" */ |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 661 | IdList *pTabList; /* List of tables to select from */ |
| 662 | Expr *pWhere; /* The WHERE clause. May be NULL */ |
| 663 | ExprList *pOrderBy; /* The ORDER BY clause. May be NULL */ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 664 | ExprList *pGroupBy; /* The GROUP BY clause. May be NULL */ |
| 665 | Expr *pHaving; /* The HAVING clause. May be NULL */ |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 666 | int isDistinct; /* True if the DISTINCT keyword is present */ |
| 667 | int distinct; /* Table to use for the distinct set */ |
drh | 10e5e3c | 2000-06-08 00:19:02 +0000 | [diff] [blame] | 668 | int base; /* First cursor available for use */ |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 669 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 670 | if( sqlite_malloc_failed || pParse->nErr || p==0 ) return 1; |
| 671 | |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 672 | /* If there is are a sequence of queries, do the earlier ones first. |
| 673 | */ |
| 674 | if( p->pPrior ){ |
| 675 | return multiSelect(pParse, p, eDest, iParm); |
| 676 | } |
| 677 | |
| 678 | /* Make local copies of the parameters for this query. |
| 679 | */ |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 680 | pTabList = p->pSrc; |
| 681 | pWhere = p->pWhere; |
| 682 | pOrderBy = p->pOrderBy; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 683 | pGroupBy = p->pGroupBy; |
| 684 | pHaving = p->pHaving; |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 685 | isDistinct = p->isDistinct; |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 686 | |
drh | 10e5e3c | 2000-06-08 00:19:02 +0000 | [diff] [blame] | 687 | /* Save the current value of pParse->nTab. Restore this value before |
| 688 | ** we exit. |
| 689 | */ |
| 690 | base = pParse->nTab; |
| 691 | |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 692 | /* |
| 693 | ** Do not even attempt to generate any code if we have already seen |
| 694 | ** errors before this routine starts. |
| 695 | */ |
drh | 10e5e3c | 2000-06-08 00:19:02 +0000 | [diff] [blame] | 696 | if( pParse->nErr>0 ) return 1; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 697 | sqliteParseInfoReset(pParse); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 698 | |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 699 | /* Look up every table in the table list and create an appropriate |
| 700 | ** columnlist in pEList if there isn't one already. (The parser leaves |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 701 | ** a NULL in the p->pEList if the SQL said "SELECT * FROM ...") |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 702 | */ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 703 | if( fillInColumnList(pParse, p) ){ |
| 704 | return 1; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 705 | } |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 706 | pEList = p->pEList; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 707 | if( pEList==0 ) return 1; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 708 | |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 709 | /* Allocate a temporary table to use for the DISTINCT set, if |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 710 | ** necessary. This must be done early to allocate the cursor before |
| 711 | ** any calls to sqliteExprResolveIds(). |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 712 | */ |
| 713 | if( isDistinct ){ |
| 714 | distinct = pParse->nTab++; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 715 | }else{ |
| 716 | distinct = -1; |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 717 | } |
| 718 | |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 719 | /* If writing to memory or generating a set |
| 720 | ** only a single column may be output. |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 721 | */ |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 722 | if( (eDest==SRT_Mem || eDest==SRT_Set) && pEList->nExpr>1 ){ |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 723 | sqliteSetString(&pParse->zErrMsg, "only a single result allowed for " |
| 724 | "a SELECT that is part of an expression", 0); |
| 725 | pParse->nErr++; |
| 726 | return 1; |
| 727 | } |
| 728 | |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 729 | /* ORDER BY is ignored if we are not sending the result to a callback. |
| 730 | */ |
| 731 | if( eDest!=SRT_Callback ){ |
| 732 | pOrderBy = 0; |
| 733 | } |
| 734 | |
| 735 | /* Allocate cursors for "expr IN (SELECT ...)" constructs. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 736 | */ |
| 737 | for(i=0; i<pEList->nExpr; i++){ |
drh | 4794b98 | 2000-06-06 13:54:14 +0000 | [diff] [blame] | 738 | sqliteExprResolveInSelect(pParse, pEList->a[i].pExpr); |
| 739 | } |
| 740 | if( pWhere ) sqliteExprResolveInSelect(pParse, pWhere); |
| 741 | if( pOrderBy ){ |
| 742 | for(i=0; i<pOrderBy->nExpr; i++){ |
| 743 | sqliteExprResolveInSelect(pParse, pOrderBy->a[i].pExpr); |
| 744 | } |
| 745 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 746 | if( pGroupBy ){ |
| 747 | for(i=0; i<pGroupBy->nExpr; i++){ |
| 748 | sqliteExprResolveInSelect(pParse, pGroupBy->a[i].pExpr); |
| 749 | } |
| 750 | } |
| 751 | if( pHaving ) sqliteExprResolveInSelect(pParse, pHaving); |
| 752 | |
drh | 10e5e3c | 2000-06-08 00:19:02 +0000 | [diff] [blame] | 753 | /* At this point, we should have allocated all the cursors that we |
| 754 | ** need to handle subquerys and temporary tables. From here on we |
| 755 | ** are committed to keeping the same value for pParse->nTab. |
| 756 | ** |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 757 | ** Resolve the column names and do a semantics check on all the expressions. |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 758 | */ |
drh | 4794b98 | 2000-06-06 13:54:14 +0000 | [diff] [blame] | 759 | for(i=0; i<pEList->nExpr; i++){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 760 | if( sqliteExprResolveIds(pParse, pTabList, pEList->a[i].pExpr) ){ |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 761 | return 1; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 762 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 763 | if( sqliteExprCheck(pParse, pEList->a[i].pExpr, 1, &isAgg) ){ |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 764 | return 1; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 765 | } |
| 766 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 767 | if( pWhere ){ |
| 768 | if( sqliteExprResolveIds(pParse, pTabList, pWhere) ){ |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 769 | return 1; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 770 | } |
| 771 | if( sqliteExprCheck(pParse, pWhere, 0, 0) ){ |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 772 | return 1; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 773 | } |
| 774 | } |
| 775 | if( pOrderBy ){ |
| 776 | for(i=0; i<pOrderBy->nExpr; i++){ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 777 | Expr *pE = pOrderBy->a[i].pExpr; |
| 778 | if( sqliteExprResolveIds(pParse, pTabList, pE) ){ |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 779 | return 1; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 780 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 781 | if( sqliteExprCheck(pParse, pE, isAgg, 0) ){ |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 782 | return 1; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 783 | } |
| 784 | } |
| 785 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 786 | if( pGroupBy ){ |
| 787 | for(i=0; i<pGroupBy->nExpr; i++){ |
| 788 | Expr *pE = pGroupBy->a[i].pExpr; |
| 789 | if( sqliteExprResolveIds(pParse, pTabList, pE) ){ |
| 790 | return 1; |
| 791 | } |
| 792 | if( sqliteExprCheck(pParse, pE, isAgg, 0) ){ |
| 793 | return 1; |
| 794 | } |
| 795 | } |
| 796 | } |
| 797 | if( pHaving ){ |
| 798 | if( pGroupBy==0 ){ |
drh | da93281 | 2000-06-06 18:00:15 +0000 | [diff] [blame] | 799 | sqliteSetString(&pParse->zErrMsg, "a GROUP BY clause is required " |
| 800 | "before HAVING", 0); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 801 | pParse->nErr++; |
| 802 | return 1; |
| 803 | } |
| 804 | if( sqliteExprResolveIds(pParse, pTabList, pHaving) ){ |
| 805 | return 1; |
| 806 | } |
drh | da93281 | 2000-06-06 18:00:15 +0000 | [diff] [blame] | 807 | if( sqliteExprCheck(pParse, pHaving, isAgg, 0) ){ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 808 | return 1; |
| 809 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 810 | } |
| 811 | |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 812 | /* Do an analysis of aggregate expressions. |
drh | efb7251 | 2000-05-31 20:00:52 +0000 | [diff] [blame] | 813 | */ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 814 | if( isAgg ){ |
drh | aaf8872 | 2000-06-08 11:25:00 +0000 | [diff] [blame] | 815 | assert( pParse->nAgg==0 && pParse->iAggCount<0 ); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 816 | for(i=0; i<pEList->nExpr; i++){ |
| 817 | if( sqliteExprAnalyzeAggregates(pParse, pEList->a[i].pExpr) ){ |
| 818 | return 1; |
| 819 | } |
| 820 | } |
| 821 | if( pGroupBy ){ |
| 822 | for(i=0; i<pGroupBy->nExpr; i++){ |
| 823 | if( sqliteExprAnalyzeAggregates(pParse, pGroupBy->a[i].pExpr) ){ |
| 824 | return 1; |
| 825 | } |
| 826 | } |
| 827 | } |
| 828 | if( pHaving && sqliteExprAnalyzeAggregates(pParse, pHaving) ){ |
| 829 | return 1; |
| 830 | } |
drh | 191b690 | 2000-06-08 11:13:01 +0000 | [diff] [blame] | 831 | if( pOrderBy ){ |
| 832 | for(i=0; i<pOrderBy->nExpr; i++){ |
| 833 | if( sqliteExprAnalyzeAggregates(pParse, pOrderBy->a[i].pExpr) ){ |
| 834 | return 1; |
| 835 | } |
| 836 | } |
| 837 | } |
drh | efb7251 | 2000-05-31 20:00:52 +0000 | [diff] [blame] | 838 | } |
| 839 | |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 840 | /* Begin generating code. |
| 841 | */ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 842 | v = sqliteGetVdbe(pParse); |
| 843 | if( v==0 ) return 1; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 844 | if( pOrderBy ){ |
| 845 | sqliteVdbeAddOp(v, OP_SortOpen, 0, 0, 0, 0); |
| 846 | } |
| 847 | |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 848 | /* Identify column names if we will be using in the callback. This |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 849 | ** step is skipped if the output is going to a table or a memory cell. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 850 | */ |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 851 | if( eDest==SRT_Callback ){ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 852 | generateColumnNames(pParse, pTabList, pEList); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 853 | } |
| 854 | |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 855 | /* Reset the aggregator |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 856 | */ |
| 857 | if( isAgg ){ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 858 | sqliteVdbeAddOp(v, OP_AggReset, 0, pParse->nAgg, 0, 0); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 859 | } |
| 860 | |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 861 | /* Initialize the memory cell to NULL |
| 862 | */ |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 863 | if( eDest==SRT_Mem ){ |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 864 | sqliteVdbeAddOp(v, OP_Null, 0, 0, 0, 0); |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 865 | sqliteVdbeAddOp(v, OP_MemStore, iParm, 0, 0, 0); |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 866 | } |
| 867 | |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 868 | /* Begin the database scan |
drh | efb7251 | 2000-05-31 20:00:52 +0000 | [diff] [blame] | 869 | */ |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 870 | if( isDistinct ){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 871 | sqliteVdbeAddOp(v, OP_OpenTemp, distinct, 0, 0, 0); |
drh | efb7251 | 2000-05-31 20:00:52 +0000 | [diff] [blame] | 872 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 873 | pWInfo = sqliteWhereBegin(pParse, pTabList, pWhere, 0); |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 874 | if( pWInfo==0 ) return 1; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 875 | |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 876 | /* Use the standard inner loop if we are not dealing with |
| 877 | ** aggregates |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 878 | */ |
drh | da9d6c4 | 2000-05-31 18:20:14 +0000 | [diff] [blame] | 879 | if( !isAgg ){ |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 880 | if( selectInnerLoop(pParse, pEList, 0, 0, pOrderBy, distinct, eDest, iParm, |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 881 | pWInfo->iContinue, pWInfo->iBreak) ){ |
| 882 | return 1; |
drh | da9d6c4 | 2000-05-31 18:20:14 +0000 | [diff] [blame] | 883 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 884 | } |
drh | efb7251 | 2000-05-31 20:00:52 +0000 | [diff] [blame] | 885 | |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 886 | /* If we are dealing with aggregates, then to the special aggregate |
| 887 | ** processing. |
drh | efb7251 | 2000-05-31 20:00:52 +0000 | [diff] [blame] | 888 | */ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 889 | else{ |
| 890 | int doFocus; |
| 891 | if( pGroupBy ){ |
| 892 | for(i=0; i<pGroupBy->nExpr; i++){ |
| 893 | sqliteExprCode(pParse, pGroupBy->a[i].pExpr); |
| 894 | } |
| 895 | sqliteVdbeAddOp(v, OP_MakeKey, pGroupBy->nExpr, 0, 0, 0); |
| 896 | doFocus = 1; |
| 897 | }else{ |
| 898 | doFocus = 0; |
| 899 | for(i=0; i<pParse->nAgg; i++){ |
| 900 | if( !pParse->aAgg[i].isAgg ){ |
| 901 | doFocus = 1; |
| 902 | break; |
| 903 | } |
| 904 | } |
| 905 | if( doFocus ){ |
| 906 | sqliteVdbeAddOp(v, OP_String, 0, 0, "", 0); |
| 907 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 908 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 909 | if( doFocus ){ |
| 910 | int lbl1 = sqliteVdbeMakeLabel(v); |
| 911 | sqliteVdbeAddOp(v, OP_AggFocus, 0, lbl1, 0, 0); |
| 912 | for(i=0; i<pParse->nAgg; i++){ |
| 913 | if( pParse->aAgg[i].isAgg ) continue; |
| 914 | sqliteExprCode(pParse, pParse->aAgg[i].pExpr); |
| 915 | sqliteVdbeAddOp(v, OP_AggSet, 0, i, 0, 0); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 916 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 917 | sqliteVdbeResolveLabel(v, lbl1); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 918 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 919 | for(i=0; i<pParse->nAgg; i++){ |
| 920 | Expr *pE; |
| 921 | int op; |
| 922 | if( !pParse->aAgg[i].isAgg ) continue; |
| 923 | pE = pParse->aAgg[i].pExpr; |
| 924 | if( pE==0 ){ |
| 925 | sqliteVdbeAddOp(v, OP_AggIncr, 1, i, 0, 0); |
| 926 | continue; |
| 927 | } |
| 928 | assert( pE->op==TK_AGG_FUNCTION ); |
| 929 | assert( pE->pList!=0 && pE->pList->nExpr==1 ); |
| 930 | sqliteExprCode(pParse, pE->pList->a[0].pExpr); |
| 931 | sqliteVdbeAddOp(v, OP_AggGet, 0, i, 0, 0); |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 932 | switch( pE->iColumn ){ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 933 | case FN_Min: op = OP_Min; break; |
| 934 | case FN_Max: op = OP_Max; break; |
| 935 | case FN_Avg: op = OP_Add; break; |
| 936 | case FN_Sum: op = OP_Add; break; |
| 937 | } |
| 938 | sqliteVdbeAddOp(v, op, 0, 0, 0, 0); |
| 939 | sqliteVdbeAddOp(v, OP_AggSet, 0, i, 0, 0); |
| 940 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 941 | } |
| 942 | |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 943 | |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 944 | /* End the database scan loop. |
| 945 | */ |
| 946 | sqliteWhereEnd(pWInfo); |
| 947 | |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 948 | /* If we are processing aggregates, we need to set up a second loop |
| 949 | ** over all of the aggregate values and process them. |
| 950 | */ |
| 951 | if( isAgg ){ |
| 952 | int endagg = sqliteVdbeMakeLabel(v); |
| 953 | int startagg; |
| 954 | startagg = sqliteVdbeAddOp(v, OP_AggNext, 0, endagg, 0, 0); |
| 955 | pParse->useAgg = 1; |
| 956 | if( pHaving ){ |
| 957 | sqliteExprIfFalse(pParse, pHaving, startagg); |
| 958 | } |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 959 | if( selectInnerLoop(pParse, pEList, 0, 0, pOrderBy, distinct, eDest, iParm, |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 960 | startagg, endagg) ){ |
| 961 | return 1; |
| 962 | } |
| 963 | sqliteVdbeAddOp(v, OP_Goto, 0, startagg, 0, 0); |
| 964 | sqliteVdbeAddOp(v, OP_Noop, 0, 0, 0, endagg); |
| 965 | pParse->useAgg = 0; |
| 966 | } |
| 967 | |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 968 | /* If there is an ORDER BY clause, then we need to sort the results |
| 969 | ** and send them to the callback one by one. |
| 970 | */ |
| 971 | if( pOrderBy ){ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 972 | generateSortTail(v, pEList->nExpr); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 973 | } |
drh | 10e5e3c | 2000-06-08 00:19:02 +0000 | [diff] [blame] | 974 | pParse->nTab = base; |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 975 | return 0; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 976 | } |