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