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 | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame^] | 15 | ** $Id: select.c,v 1.49 2001/11/07 16:48:27 drh Exp $ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 16 | */ |
| 17 | #include "sqliteInt.h" |
| 18 | |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 19 | /* |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 20 | ** Allocate a new Select structure and return a pointer to that |
| 21 | ** structure. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 22 | */ |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 23 | Select *sqliteSelectNew( |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 24 | ExprList *pEList, /* which columns to include in the result */ |
| 25 | IdList *pSrc, /* the FROM clause -- which tables to scan */ |
| 26 | Expr *pWhere, /* the WHERE clause */ |
| 27 | ExprList *pGroupBy, /* the GROUP BY clause */ |
| 28 | Expr *pHaving, /* the HAVING clause */ |
| 29 | ExprList *pOrderBy, /* the ORDER BY clause */ |
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){ |
| 321 | int i, j; |
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 | |
| 356 | /* If the list of columns to retrieve is "*" then replace it with |
| 357 | ** a list of all columns from all tables. |
| 358 | */ |
| 359 | if( pEList==0 ){ |
| 360 | for(i=0; i<pTabList->nId; i++){ |
| 361 | Table *pTab = pTabList->a[i].pTab; |
| 362 | for(j=0; j<pTab->nCol; j++){ |
| 363 | Expr *pExpr = sqliteExpr(TK_DOT, 0, 0, 0); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 364 | if( pExpr==0 ) break; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 365 | pExpr->pLeft = sqliteExpr(TK_ID, 0, 0, 0); |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 366 | if( pExpr->pLeft==0 ){ sqliteExprDelete(pExpr); break; } |
drh | 17e24df | 2001-11-06 14:10:41 +0000 | [diff] [blame] | 367 | if( pTabList->a[i].zAlias && pTabList->a[i].zAlias[0] ){ |
| 368 | pExpr->pLeft->token.z = pTabList->a[i].zAlias; |
| 369 | pExpr->pLeft->token.n = strlen(pTabList->a[i].zAlias); |
| 370 | }else{ |
| 371 | pExpr->pLeft->token.z = pTab->zName; |
| 372 | pExpr->pLeft->token.n = strlen(pTab->zName); |
| 373 | } |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 374 | pExpr->pRight = sqliteExpr(TK_ID, 0, 0, 0); |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 375 | if( pExpr->pRight==0 ){ sqliteExprDelete(pExpr); break; } |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 376 | pExpr->pRight->token.z = pTab->aCol[j].zName; |
| 377 | pExpr->pRight->token.n = strlen(pTab->aCol[j].zName); |
drh | e1b6a5b | 2000-07-29 13:06:59 +0000 | [diff] [blame] | 378 | pExpr->span.z = ""; |
| 379 | pExpr->span.n = 0; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 380 | pEList = sqliteExprListAppend(pEList, pExpr, 0); |
| 381 | } |
| 382 | } |
| 383 | p->pEList = pEList; |
| 384 | } |
| 385 | return 0; |
| 386 | } |
| 387 | |
| 388 | /* |
| 389 | ** This routine associates entries in an ORDER BY expression list with |
| 390 | ** columns in a result. For each ORDER BY expression, the opcode of |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 391 | ** 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] | 392 | ** the top-level node is filled in with column number and the iTable |
| 393 | ** value of the top-level node is filled with iTable parameter. |
| 394 | ** |
| 395 | ** If there are prior SELECT clauses, they are processed first. A match |
| 396 | ** in an earlier SELECT takes precedence over a later SELECT. |
| 397 | ** |
| 398 | ** Any entry that does not match is flagged as an error. The number |
| 399 | ** of errors is returned. |
| 400 | */ |
| 401 | static int matchOrderbyToColumn( |
| 402 | Parse *pParse, /* A place to leave error messages */ |
| 403 | Select *pSelect, /* Match to result columns of this SELECT */ |
| 404 | ExprList *pOrderBy, /* The ORDER BY values to match against columns */ |
| 405 | int iTable, /* Insert this this value in iTable */ |
| 406 | int mustComplete /* If TRUE all ORDER BYs must match */ |
| 407 | ){ |
| 408 | int nErr = 0; |
| 409 | int i, j; |
| 410 | ExprList *pEList; |
| 411 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 412 | if( pSelect==0 || pOrderBy==0 ) return 1; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 413 | if( mustComplete ){ |
| 414 | for(i=0; i<pOrderBy->nExpr; i++){ pOrderBy->a[i].done = 0; } |
| 415 | } |
| 416 | if( fillInColumnList(pParse, pSelect) ){ |
| 417 | return 1; |
| 418 | } |
| 419 | if( pSelect->pPrior ){ |
drh | 92cd52f | 2000-06-08 01:55:29 +0000 | [diff] [blame] | 420 | if( matchOrderbyToColumn(pParse, pSelect->pPrior, pOrderBy, iTable, 0) ){ |
| 421 | return 1; |
| 422 | } |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 423 | } |
| 424 | pEList = pSelect->pEList; |
| 425 | for(i=0; i<pOrderBy->nExpr; i++){ |
| 426 | Expr *pE = pOrderBy->a[i].pExpr; |
drh | 92cd52f | 2000-06-08 01:55:29 +0000 | [diff] [blame] | 427 | int match = 0; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 428 | if( pOrderBy->a[i].done ) continue; |
| 429 | for(j=0; j<pEList->nExpr; j++){ |
drh | 4cfa793 | 2000-06-08 15:10:46 +0000 | [diff] [blame] | 430 | if( pEList->a[j].zName && (pE->op==TK_ID || pE->op==TK_STRING) ){ |
| 431 | char *zName = pEList->a[j].zName; |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 432 | char *zLabel = sqliteStrNDup(pE->token.z, pE->token.n); |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 433 | sqliteDequote(zLabel); |
| 434 | if( sqliteStrICmp(zName, zLabel)==0 ){ |
| 435 | match = 1; |
| 436 | } |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 437 | sqliteFree(zLabel); |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 438 | } |
drh | 4cfa793 | 2000-06-08 15:10:46 +0000 | [diff] [blame] | 439 | if( match==0 && sqliteExprCompare(pE, pEList->a[j].pExpr) ){ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 440 | match = 1; |
| 441 | } |
| 442 | if( match ){ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 443 | pE->op = TK_COLUMN; |
| 444 | pE->iColumn = j; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 445 | pE->iTable = iTable; |
| 446 | pOrderBy->a[i].done = 1; |
| 447 | break; |
| 448 | } |
| 449 | } |
drh | 92cd52f | 2000-06-08 01:55:29 +0000 | [diff] [blame] | 450 | if( !match && mustComplete ){ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 451 | char zBuf[30]; |
| 452 | sprintf(zBuf,"%d",i+1); |
| 453 | sqliteSetString(&pParse->zErrMsg, "ORDER BY term number ", zBuf, |
| 454 | " does not match any result column", 0); |
| 455 | pParse->nErr++; |
| 456 | nErr++; |
| 457 | break; |
| 458 | } |
| 459 | } |
| 460 | return nErr; |
| 461 | } |
| 462 | |
| 463 | /* |
| 464 | ** Get a VDBE for the given parser context. Create a new one if necessary. |
| 465 | ** If an error occurs, return NULL and leave a message in pParse. |
| 466 | */ |
| 467 | Vdbe *sqliteGetVdbe(Parse *pParse){ |
| 468 | Vdbe *v = pParse->pVdbe; |
| 469 | if( v==0 ){ |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 470 | v = pParse->pVdbe = sqliteVdbeCreate(pParse->db); |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 471 | } |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 472 | return v; |
| 473 | } |
| 474 | |
| 475 | |
| 476 | /* |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 477 | ** This routine is called to process a query that is really the union |
| 478 | ** or intersection of two or more separate queries. |
| 479 | */ |
| 480 | static int multiSelect(Parse *pParse, Select *p, int eDest, int iParm){ |
drh | 10e5e3c | 2000-06-08 00:19:02 +0000 | [diff] [blame] | 481 | int rc; /* Success code from a subroutine */ |
| 482 | Select *pPrior; /* Another SELECT immediately to our left */ |
| 483 | Vdbe *v; /* Generate code to this VDBE */ |
| 484 | int base; /* Baseline value for pParse->nTab */ |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 485 | |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 486 | /* Make sure there is no ORDER BY clause on prior SELECTs. Only the |
| 487 | ** last SELECT in the series may have an ORDER BY. |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 488 | */ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 489 | if( p==0 || p->pPrior==0 ) return 1; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 490 | pPrior = p->pPrior; |
| 491 | if( pPrior->pOrderBy ){ |
| 492 | sqliteSetString(&pParse->zErrMsg,"ORDER BY clause should come after ", |
| 493 | selectOpName(p->op), " not before", 0); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 494 | pParse->nErr++; |
| 495 | return 1; |
| 496 | } |
| 497 | |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 498 | /* Make sure we have a valid query engine. If not, create a new one. |
| 499 | */ |
| 500 | v = sqliteGetVdbe(pParse); |
| 501 | if( v==0 ) return 1; |
| 502 | |
| 503 | /* Process the UNION or INTERSECTION |
| 504 | */ |
drh | 10e5e3c | 2000-06-08 00:19:02 +0000 | [diff] [blame] | 505 | base = pParse->nTab; |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 506 | switch( p->op ){ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 507 | case TK_ALL: |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 508 | case TK_EXCEPT: |
| 509 | case TK_UNION: { |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 510 | int unionTab; /* Cursor number of the temporary table holding result */ |
| 511 | int op; /* One of the SRT_ operations to apply to self */ |
| 512 | int priorOp; /* The SRT_ operation to apply to prior selects */ |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 513 | |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 514 | priorOp = p->op==TK_ALL ? SRT_Table : SRT_Union; |
| 515 | if( eDest==priorOp ){ |
| 516 | /* We can reuse a temporary table generated by a SELECT to our |
| 517 | ** right. This also means we are not the right-most select and so |
| 518 | ** we cannot have an ORDER BY clause |
| 519 | */ |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 520 | unionTab = iParm; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 521 | assert( p->pOrderBy==0 ); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 522 | }else{ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 523 | /* We will need to create our own temporary table to hold the |
| 524 | ** intermediate results. |
| 525 | */ |
| 526 | unionTab = pParse->nTab++; |
| 527 | if( p->pOrderBy |
| 528 | && matchOrderbyToColumn(pParse, p, p->pOrderBy, unionTab, 1) ){ |
| 529 | return 1; |
| 530 | } |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 531 | if( p->op!=TK_ALL ){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 532 | sqliteVdbeAddOp(v, OP_OpenTemp, unionTab, 0); |
| 533 | sqliteVdbeAddOp(v, OP_KeyAsData, unionTab, 1); |
drh | 345fda3 | 2001-01-15 22:51:08 +0000 | [diff] [blame] | 534 | }else{ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 535 | sqliteVdbeAddOp(v, OP_OpenTemp, unionTab, 0); |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 536 | } |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 537 | } |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 538 | |
| 539 | /* Code the SELECT statements to our left |
| 540 | */ |
| 541 | rc = sqliteSelect(pParse, pPrior, priorOp, unionTab); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 542 | if( rc ) return rc; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 543 | |
| 544 | /* Code the current SELECT statement |
| 545 | */ |
| 546 | switch( p->op ){ |
| 547 | case TK_EXCEPT: op = SRT_Except; break; |
| 548 | case TK_UNION: op = SRT_Union; break; |
| 549 | case TK_ALL: op = SRT_Table; break; |
| 550 | } |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 551 | p->pPrior = 0; |
| 552 | rc = sqliteSelect(pParse, p, op, unionTab); |
| 553 | p->pPrior = pPrior; |
| 554 | if( rc ) return rc; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 555 | |
| 556 | /* Convert the data in the temporary table into whatever form |
| 557 | ** it is that we currently need. |
| 558 | */ |
| 559 | if( eDest!=priorOp ){ |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame^] | 560 | int iCont, iBreak, iStart; |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 561 | assert( p->pEList ); |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 562 | generateColumnNames(pParse, 0, p->pEList); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 563 | iBreak = sqliteVdbeMakeLabel(v); |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame^] | 564 | iCont = sqliteVdbeMakeLabel(v); |
| 565 | sqliteVdbeAddOp(v, OP_Rewind, unionTab, iBreak); |
| 566 | iStart = sqliteVdbeCurrentAddr(v); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 567 | rc = selectInnerLoop(pParse, 0, unionTab, p->pEList->nExpr, |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 568 | p->pOrderBy, -1, eDest, iParm, |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 569 | iCont, iBreak); |
| 570 | if( rc ) return 1; |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame^] | 571 | sqliteVdbeResolveLabel(v, iCont); |
| 572 | sqliteVdbeAddOp(v, OP_Next, unionTab, iStart); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 573 | sqliteVdbeResolveLabel(v, iBreak); |
| 574 | sqliteVdbeAddOp(v, OP_Close, unionTab, 0); |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 575 | if( p->pOrderBy ){ |
| 576 | generateSortTail(v, p->pEList->nExpr); |
| 577 | } |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 578 | } |
| 579 | break; |
| 580 | } |
| 581 | case TK_INTERSECT: { |
| 582 | int tab1, tab2; |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame^] | 583 | int iCont, iBreak, iStart; |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 584 | |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 585 | /* INTERSECT is different from the others since it requires |
drh | 6206d50 | 2000-06-19 19:09:08 +0000 | [diff] [blame] | 586 | ** two temporary tables. Hence it has its own case. Begin |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 587 | ** by allocating the tables we will need. |
| 588 | */ |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 589 | tab1 = pParse->nTab++; |
| 590 | tab2 = pParse->nTab++; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 591 | if( p->pOrderBy && matchOrderbyToColumn(pParse,p,p->pOrderBy,tab1,1) ){ |
| 592 | return 1; |
| 593 | } |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 594 | sqliteVdbeAddOp(v, OP_OpenTemp, tab1, 0); |
| 595 | sqliteVdbeAddOp(v, OP_KeyAsData, tab1, 1); |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 596 | |
| 597 | /* Code the SELECTs to our left into temporary table "tab1". |
| 598 | */ |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 599 | rc = sqliteSelect(pParse, pPrior, SRT_Union, tab1); |
| 600 | if( rc ) return rc; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 601 | |
| 602 | /* Code the current SELECT into temporary table "tab2" |
| 603 | */ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 604 | sqliteVdbeAddOp(v, OP_OpenTemp, tab2, 0); |
| 605 | sqliteVdbeAddOp(v, OP_KeyAsData, tab2, 1); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 606 | p->pPrior = 0; |
| 607 | rc = sqliteSelect(pParse, p, SRT_Union, tab2); |
| 608 | p->pPrior = pPrior; |
| 609 | if( rc ) return rc; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 610 | |
| 611 | /* Generate code to take the intersection of the two temporary |
| 612 | ** tables. |
| 613 | */ |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 614 | assert( p->pEList ); |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 615 | generateColumnNames(pParse, 0, p->pEList); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 616 | iBreak = sqliteVdbeMakeLabel(v); |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame^] | 617 | iCont = sqliteVdbeMakeLabel(v); |
| 618 | sqliteVdbeAddOp(v, OP_Rewind, tab1, iBreak); |
| 619 | iStart = sqliteVdbeAddOp(v, OP_FullKey, tab1, 0); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 620 | sqliteVdbeAddOp(v, OP_NotFound, tab2, iCont); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 621 | rc = selectInnerLoop(pParse, 0, tab1, p->pEList->nExpr, |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 622 | p->pOrderBy, -1, eDest, iParm, |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 623 | iCont, iBreak); |
| 624 | if( rc ) return 1; |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame^] | 625 | sqliteVdbeResolveLabel(v, iCont); |
| 626 | sqliteVdbeAddOp(v, OP_Next, tab1, iStart); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 627 | sqliteVdbeResolveLabel(v, iBreak); |
| 628 | sqliteVdbeAddOp(v, OP_Close, tab2, 0); |
| 629 | sqliteVdbeAddOp(v, OP_Close, tab1, 0); |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 630 | if( p->pOrderBy ){ |
| 631 | generateSortTail(v, p->pEList->nExpr); |
| 632 | } |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 633 | break; |
| 634 | } |
| 635 | } |
| 636 | assert( p->pEList && pPrior->pEList ); |
| 637 | if( p->pEList->nExpr!=pPrior->pEList->nExpr ){ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 638 | sqliteSetString(&pParse->zErrMsg, "SELECTs to the left and right of ", |
| 639 | selectOpName(p->op), " do not have the same number of result columns", 0); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 640 | pParse->nErr++; |
| 641 | return 1; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 642 | } |
drh | 10e5e3c | 2000-06-08 00:19:02 +0000 | [diff] [blame] | 643 | pParse->nTab = base; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 644 | return 0; |
| 645 | } |
| 646 | |
| 647 | /* |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 648 | ** Generate code for the given SELECT statement. |
| 649 | ** |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 650 | ** The results are distributed in various ways depending on the |
| 651 | ** value of eDest and iParm. |
| 652 | ** |
| 653 | ** eDest Value Result |
| 654 | ** ------------ ------------------------------------------- |
| 655 | ** SRT_Callback Invoke the callback for each row of the result. |
| 656 | ** |
| 657 | ** SRT_Mem Store first result in memory cell iParm |
| 658 | ** |
| 659 | ** SRT_Set Store results as keys of a table with cursor iParm |
| 660 | ** |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 661 | ** SRT_Union Store results as a key in a temporary table iParm |
| 662 | ** |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 663 | ** SRT_Except Remove results form the temporary table iParm. |
| 664 | ** |
| 665 | ** SRT_Table Store results in temporary table iParm |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 666 | ** |
| 667 | ** This routine returns the number of errors. If any errors are |
| 668 | ** encountered, then an appropriate error message is left in |
| 669 | ** pParse->zErrMsg. |
| 670 | ** |
| 671 | ** This routine does NOT free the Select structure passed in. The |
| 672 | ** calling function needs to do that. |
| 673 | */ |
| 674 | int sqliteSelect( |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 675 | Parse *pParse, /* The parser context */ |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 676 | Select *p, /* The SELECT statement being coded. */ |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 677 | int eDest, /* One of: SRT_Callback Mem Set Union Except */ |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 678 | int iParm /* Save result in this memory location, if >=0 */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 679 | ){ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 680 | int i; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 681 | WhereInfo *pWInfo; |
| 682 | Vdbe *v; |
| 683 | int isAgg = 0; /* True for select lists like "count(*)" */ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 684 | ExprList *pEList; /* List of columns to extract. NULL means "*" */ |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 685 | IdList *pTabList; /* List of tables to select from */ |
| 686 | Expr *pWhere; /* The WHERE clause. May be NULL */ |
| 687 | ExprList *pOrderBy; /* The ORDER BY clause. May be NULL */ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 688 | ExprList *pGroupBy; /* The GROUP BY clause. May be NULL */ |
| 689 | Expr *pHaving; /* The HAVING clause. May be NULL */ |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 690 | int isDistinct; /* True if the DISTINCT keyword is present */ |
| 691 | int distinct; /* Table to use for the distinct set */ |
drh | 10e5e3c | 2000-06-08 00:19:02 +0000 | [diff] [blame] | 692 | int base; /* First cursor available for use */ |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 693 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 694 | if( sqlite_malloc_failed || pParse->nErr || p==0 ) return 1; |
| 695 | |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 696 | /* If there is are a sequence of queries, do the earlier ones first. |
| 697 | */ |
| 698 | if( p->pPrior ){ |
| 699 | return multiSelect(pParse, p, eDest, iParm); |
| 700 | } |
| 701 | |
| 702 | /* Make local copies of the parameters for this query. |
| 703 | */ |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 704 | pTabList = p->pSrc; |
| 705 | pWhere = p->pWhere; |
| 706 | pOrderBy = p->pOrderBy; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 707 | pGroupBy = p->pGroupBy; |
| 708 | pHaving = p->pHaving; |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 709 | isDistinct = p->isDistinct; |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 710 | |
drh | 10e5e3c | 2000-06-08 00:19:02 +0000 | [diff] [blame] | 711 | /* Save the current value of pParse->nTab. Restore this value before |
| 712 | ** we exit. |
| 713 | */ |
| 714 | base = pParse->nTab; |
| 715 | |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 716 | /* |
| 717 | ** Do not even attempt to generate any code if we have already seen |
| 718 | ** errors before this routine starts. |
| 719 | */ |
drh | 10e5e3c | 2000-06-08 00:19:02 +0000 | [diff] [blame] | 720 | if( pParse->nErr>0 ) return 1; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 721 | sqliteParseInfoReset(pParse); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 722 | |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 723 | /* Look up every table in the table list and create an appropriate |
| 724 | ** columnlist in pEList if there isn't one already. (The parser leaves |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 725 | ** a NULL in the p->pEList if the SQL said "SELECT * FROM ...") |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 726 | */ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 727 | if( fillInColumnList(pParse, p) ){ |
| 728 | return 1; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 729 | } |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 730 | pEList = p->pEList; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 731 | if( pEList==0 ) return 1; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 732 | |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 733 | /* Allocate a temporary table to use for the DISTINCT set, if |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 734 | ** necessary. This must be done early to allocate the cursor before |
| 735 | ** any calls to sqliteExprResolveIds(). |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 736 | */ |
| 737 | if( isDistinct ){ |
| 738 | distinct = pParse->nTab++; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 739 | }else{ |
| 740 | distinct = -1; |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 741 | } |
| 742 | |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 743 | /* If writing to memory or generating a set |
| 744 | ** only a single column may be output. |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 745 | */ |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 746 | if( (eDest==SRT_Mem || eDest==SRT_Set) && pEList->nExpr>1 ){ |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 747 | sqliteSetString(&pParse->zErrMsg, "only a single result allowed for " |
| 748 | "a SELECT that is part of an expression", 0); |
| 749 | pParse->nErr++; |
| 750 | return 1; |
| 751 | } |
| 752 | |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 753 | /* ORDER BY is ignored if we are not sending the result to a callback. |
| 754 | */ |
| 755 | if( eDest!=SRT_Callback ){ |
| 756 | pOrderBy = 0; |
| 757 | } |
| 758 | |
| 759 | /* Allocate cursors for "expr IN (SELECT ...)" constructs. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 760 | */ |
| 761 | for(i=0; i<pEList->nExpr; i++){ |
drh | 4794b98 | 2000-06-06 13:54:14 +0000 | [diff] [blame] | 762 | sqliteExprResolveInSelect(pParse, pEList->a[i].pExpr); |
| 763 | } |
| 764 | if( pWhere ) sqliteExprResolveInSelect(pParse, pWhere); |
| 765 | if( pOrderBy ){ |
| 766 | for(i=0; i<pOrderBy->nExpr; i++){ |
| 767 | sqliteExprResolveInSelect(pParse, pOrderBy->a[i].pExpr); |
| 768 | } |
| 769 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 770 | if( pGroupBy ){ |
| 771 | for(i=0; i<pGroupBy->nExpr; i++){ |
| 772 | sqliteExprResolveInSelect(pParse, pGroupBy->a[i].pExpr); |
| 773 | } |
| 774 | } |
| 775 | if( pHaving ) sqliteExprResolveInSelect(pParse, pHaving); |
| 776 | |
drh | 10e5e3c | 2000-06-08 00:19:02 +0000 | [diff] [blame] | 777 | /* At this point, we should have allocated all the cursors that we |
| 778 | ** need to handle subquerys and temporary tables. From here on we |
| 779 | ** are committed to keeping the same value for pParse->nTab. |
| 780 | ** |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 781 | ** Resolve the column names and do a semantics check on all the expressions. |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 782 | */ |
drh | 4794b98 | 2000-06-06 13:54:14 +0000 | [diff] [blame] | 783 | for(i=0; i<pEList->nExpr; i++){ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 784 | if( sqliteExprResolveIds(pParse, pTabList, pEList->a[i].pExpr) ){ |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 785 | return 1; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 786 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 787 | if( sqliteExprCheck(pParse, pEList->a[i].pExpr, 1, &isAgg) ){ |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 788 | return 1; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 789 | } |
| 790 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 791 | if( pWhere ){ |
| 792 | if( sqliteExprResolveIds(pParse, pTabList, pWhere) ){ |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 793 | return 1; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 794 | } |
| 795 | if( sqliteExprCheck(pParse, pWhere, 0, 0) ){ |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 796 | return 1; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 797 | } |
| 798 | } |
| 799 | if( pOrderBy ){ |
| 800 | for(i=0; i<pOrderBy->nExpr; i++){ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 801 | Expr *pE = pOrderBy->a[i].pExpr; |
| 802 | if( sqliteExprResolveIds(pParse, pTabList, pE) ){ |
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, pE, isAgg, 0) ){ |
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 | } |
| 809 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 810 | if( pGroupBy ){ |
| 811 | for(i=0; i<pGroupBy->nExpr; i++){ |
| 812 | Expr *pE = pGroupBy->a[i].pExpr; |
| 813 | if( sqliteExprResolveIds(pParse, pTabList, pE) ){ |
| 814 | return 1; |
| 815 | } |
| 816 | if( sqliteExprCheck(pParse, pE, isAgg, 0) ){ |
| 817 | return 1; |
| 818 | } |
| 819 | } |
| 820 | } |
| 821 | if( pHaving ){ |
| 822 | if( pGroupBy==0 ){ |
drh | da93281 | 2000-06-06 18:00:15 +0000 | [diff] [blame] | 823 | sqliteSetString(&pParse->zErrMsg, "a GROUP BY clause is required " |
| 824 | "before HAVING", 0); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 825 | pParse->nErr++; |
| 826 | return 1; |
| 827 | } |
| 828 | if( sqliteExprResolveIds(pParse, pTabList, pHaving) ){ |
| 829 | return 1; |
| 830 | } |
drh | da93281 | 2000-06-06 18:00:15 +0000 | [diff] [blame] | 831 | if( sqliteExprCheck(pParse, pHaving, isAgg, 0) ){ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 832 | return 1; |
| 833 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 834 | } |
| 835 | |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 836 | /* Do an analysis of aggregate expressions. |
drh | efb7251 | 2000-05-31 20:00:52 +0000 | [diff] [blame] | 837 | */ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 838 | if( isAgg ){ |
drh | aaf8872 | 2000-06-08 11:25:00 +0000 | [diff] [blame] | 839 | assert( pParse->nAgg==0 && pParse->iAggCount<0 ); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 840 | for(i=0; i<pEList->nExpr; i++){ |
| 841 | if( sqliteExprAnalyzeAggregates(pParse, pEList->a[i].pExpr) ){ |
| 842 | return 1; |
| 843 | } |
| 844 | } |
| 845 | if( pGroupBy ){ |
| 846 | for(i=0; i<pGroupBy->nExpr; i++){ |
| 847 | if( sqliteExprAnalyzeAggregates(pParse, pGroupBy->a[i].pExpr) ){ |
| 848 | return 1; |
| 849 | } |
| 850 | } |
| 851 | } |
| 852 | if( pHaving && sqliteExprAnalyzeAggregates(pParse, pHaving) ){ |
| 853 | return 1; |
| 854 | } |
drh | 191b690 | 2000-06-08 11:13:01 +0000 | [diff] [blame] | 855 | if( pOrderBy ){ |
| 856 | for(i=0; i<pOrderBy->nExpr; i++){ |
| 857 | if( sqliteExprAnalyzeAggregates(pParse, pOrderBy->a[i].pExpr) ){ |
| 858 | return 1; |
| 859 | } |
| 860 | } |
| 861 | } |
drh | efb7251 | 2000-05-31 20:00:52 +0000 | [diff] [blame] | 862 | } |
| 863 | |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 864 | /* Begin generating code. |
| 865 | */ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 866 | v = sqliteGetVdbe(pParse); |
| 867 | if( v==0 ) return 1; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 868 | |
drh | 9bbca4c | 2001-11-06 04:00:18 +0000 | [diff] [blame] | 869 | /* Set the limiter |
| 870 | */ |
| 871 | if( p->nLimit<=0 ){ |
| 872 | p->nOffset = 0; |
| 873 | }else{ |
| 874 | if( p->nOffset<0 ) p->nOffset = 0; |
| 875 | sqliteVdbeAddOp(v, OP_Limit, p->nLimit, p->nOffset); |
| 876 | } |
| 877 | |
| 878 | |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 879 | /* Identify column names if we will be using in the callback. This |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 880 | ** 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] | 881 | */ |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 882 | if( eDest==SRT_Callback ){ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 883 | generateColumnNames(pParse, pTabList, pEList); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 884 | } |
| 885 | |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 886 | /* Reset the aggregator |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 887 | */ |
| 888 | if( isAgg ){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 889 | sqliteVdbeAddOp(v, OP_AggReset, 0, pParse->nAgg); |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 890 | if( pGroupBy==0 ){ |
| 891 | sqliteVdbeAddOp(v, OP_String, 0, 0); |
| 892 | sqliteVdbeChangeP3(v, -1, "", P3_STATIC); |
| 893 | sqliteVdbeAddOp(v, OP_AggFocus, 0, 0); |
| 894 | for(i=0; i<pParse->nAgg; i++){ |
| 895 | Expr *pE; |
| 896 | if( !pParse->aAgg[i].isAgg ) continue; |
| 897 | pE = pParse->aAgg[i].pExpr; |
| 898 | assert( pE==0 || pE->op==TK_AGG_FUNCTION ); |
| 899 | assert( pE==0 || (pE->pList!=0 && pE->pList->nExpr==1) ); |
| 900 | if( pE==0 || pE->iColumn==FN_Sum ){ |
| 901 | sqliteVdbeAddOp(v, OP_Integer, 0, 0); |
| 902 | sqliteVdbeAddOp(v, OP_AggSet, 0, i); |
| 903 | continue; |
| 904 | } |
| 905 | } |
| 906 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 907 | } |
| 908 | |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 909 | /* Initialize the memory cell to NULL |
| 910 | */ |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 911 | if( eDest==SRT_Mem ){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 912 | sqliteVdbeAddOp(v, OP_String, 0, 0); |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 913 | sqliteVdbeAddOp(v, OP_MemStore, iParm, 1); |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 914 | } |
| 915 | |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 916 | /* Begin the database scan |
drh | efb7251 | 2000-05-31 20:00:52 +0000 | [diff] [blame] | 917 | */ |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 918 | if( isDistinct ){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 919 | sqliteVdbeAddOp(v, OP_OpenTemp, distinct, 0); |
drh | efb7251 | 2000-05-31 20:00:52 +0000 | [diff] [blame] | 920 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 921 | pWInfo = sqliteWhereBegin(pParse, pTabList, pWhere, 0); |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 922 | if( pWInfo==0 ) return 1; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 923 | |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 924 | /* Use the standard inner loop if we are not dealing with |
| 925 | ** aggregates |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 926 | */ |
drh | da9d6c4 | 2000-05-31 18:20:14 +0000 | [diff] [blame] | 927 | if( !isAgg ){ |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 928 | if( selectInnerLoop(pParse, pEList, 0, 0, pOrderBy, distinct, eDest, iParm, |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 929 | pWInfo->iContinue, pWInfo->iBreak) ){ |
| 930 | return 1; |
drh | da9d6c4 | 2000-05-31 18:20:14 +0000 | [diff] [blame] | 931 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 932 | } |
drh | efb7251 | 2000-05-31 20:00:52 +0000 | [diff] [blame] | 933 | |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 934 | /* If we are dealing with aggregates, then to the special aggregate |
| 935 | ** processing. |
drh | efb7251 | 2000-05-31 20:00:52 +0000 | [diff] [blame] | 936 | */ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 937 | else{ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 938 | if( pGroupBy ){ |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 939 | int lbl1; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 940 | for(i=0; i<pGroupBy->nExpr; i++){ |
| 941 | sqliteExprCode(pParse, pGroupBy->a[i].pExpr); |
| 942 | } |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 943 | sqliteVdbeAddOp(v, OP_MakeKey, pGroupBy->nExpr, 0); |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 944 | lbl1 = sqliteVdbeMakeLabel(v); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 945 | sqliteVdbeAddOp(v, OP_AggFocus, 0, lbl1); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 946 | for(i=0; i<pParse->nAgg; i++){ |
| 947 | if( pParse->aAgg[i].isAgg ) continue; |
| 948 | sqliteExprCode(pParse, pParse->aAgg[i].pExpr); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 949 | sqliteVdbeAddOp(v, OP_AggSet, 0, i); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 950 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 951 | sqliteVdbeResolveLabel(v, lbl1); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 952 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 953 | for(i=0; i<pParse->nAgg; i++){ |
| 954 | Expr *pE; |
| 955 | int op; |
| 956 | if( !pParse->aAgg[i].isAgg ) continue; |
| 957 | pE = pParse->aAgg[i].pExpr; |
| 958 | if( pE==0 ){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 959 | sqliteVdbeAddOp(v, OP_AggIncr, 1, i); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 960 | continue; |
| 961 | } |
| 962 | assert( pE->op==TK_AGG_FUNCTION ); |
| 963 | assert( pE->pList!=0 && pE->pList->nExpr==1 ); |
| 964 | sqliteExprCode(pParse, pE->pList->a[0].pExpr); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 965 | sqliteVdbeAddOp(v, OP_AggGet, 0, i); |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 966 | switch( pE->iColumn ){ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 967 | case FN_Min: op = OP_Min; break; |
| 968 | case FN_Max: op = OP_Max; break; |
| 969 | case FN_Avg: op = OP_Add; break; |
| 970 | case FN_Sum: op = OP_Add; break; |
| 971 | } |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 972 | sqliteVdbeAddOp(v, op, 0, 0); |
| 973 | sqliteVdbeAddOp(v, OP_AggSet, 0, i); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 974 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 975 | } |
| 976 | |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 977 | |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 978 | /* End the database scan loop. |
| 979 | */ |
| 980 | sqliteWhereEnd(pWInfo); |
| 981 | |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 982 | /* If we are processing aggregates, we need to set up a second loop |
| 983 | ** over all of the aggregate values and process them. |
| 984 | */ |
| 985 | if( isAgg ){ |
| 986 | int endagg = sqliteVdbeMakeLabel(v); |
| 987 | int startagg; |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 988 | startagg = sqliteVdbeAddOp(v, OP_AggNext, 0, endagg); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 989 | pParse->useAgg = 1; |
| 990 | if( pHaving ){ |
| 991 | sqliteExprIfFalse(pParse, pHaving, startagg); |
| 992 | } |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 993 | if( selectInnerLoop(pParse, pEList, 0, 0, pOrderBy, distinct, eDest, iParm, |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 994 | startagg, endagg) ){ |
| 995 | return 1; |
| 996 | } |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 997 | sqliteVdbeAddOp(v, OP_Goto, 0, startagg); |
| 998 | sqliteVdbeResolveLabel(v, endagg); |
| 999 | sqliteVdbeAddOp(v, OP_Noop, 0, 0); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1000 | pParse->useAgg = 0; |
| 1001 | } |
| 1002 | |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1003 | /* If there is an ORDER BY clause, then we need to sort the results |
| 1004 | ** and send them to the callback one by one. |
| 1005 | */ |
| 1006 | if( pOrderBy ){ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1007 | generateSortTail(v, pEList->nExpr); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1008 | } |
drh | 10e5e3c | 2000-06-08 00:19:02 +0000 | [diff] [blame] | 1009 | pParse->nTab = base; |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 1010 | |
| 1011 | |
| 1012 | /* Issue a null callback if that is what the user wants. |
| 1013 | */ |
| 1014 | if( (pParse->db->flags & SQLITE_NullCallback)!=0 && eDest==SRT_Callback ){ |
| 1015 | sqliteVdbeAddOp(v, OP_NullCallback, pEList->nExpr, 0); |
| 1016 | } |
| 1017 | |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 1018 | return 0; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1019 | } |