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