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