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 | 1cc3d75 | 2002-03-23 00:31:29 +0000 | [diff] [blame^] | 15 | ** $Id: select.c,v 1.77 2002/03/23 00:31:29 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; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 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); |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 133 | sqliteVdbeAddOp(v, OP_PutStrKey, distinct, 0); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 134 | } |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 135 | |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 136 | /* If there is an ORDER BY clause, then store the results |
| 137 | ** in a sorter. |
| 138 | */ |
| 139 | if( pOrderBy ){ |
| 140 | char *zSortOrder; |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 141 | sqliteVdbeAddOp(v, OP_SortMakeRec, nColumn, 0); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 142 | zSortOrder = sqliteMalloc( pOrderBy->nExpr + 1 ); |
| 143 | if( zSortOrder==0 ) return 1; |
| 144 | for(i=0; i<pOrderBy->nExpr; i++){ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 145 | zSortOrder[i] = pOrderBy->a[i].sortOrder ? '-' : '+'; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 146 | sqliteExprCode(pParse, pOrderBy->a[i].pExpr); |
| 147 | } |
| 148 | zSortOrder[pOrderBy->nExpr] = 0; |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 149 | sqliteVdbeAddOp(v, OP_SortMakeKey, pOrderBy->nExpr, 0); |
| 150 | sqliteVdbeChangeP3(v, -1, zSortOrder, strlen(zSortOrder)); |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 151 | sqliteFree(zSortOrder); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 152 | sqliteVdbeAddOp(v, OP_SortPut, 0, 0); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 153 | }else |
| 154 | |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 155 | /* In this mode, write each query result to the key of the temporary |
| 156 | ** table iParm. |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 157 | */ |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 158 | if( eDest==SRT_Union ){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 159 | sqliteVdbeAddOp(v, OP_MakeRecord, nColumn, 0); |
| 160 | sqliteVdbeAddOp(v, OP_String, iParm, 0); |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 161 | sqliteVdbeAddOp(v, OP_PutStrKey, iParm, 0); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 162 | }else |
| 163 | |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 164 | /* Store the result as data using a unique key. |
| 165 | */ |
drh | 2d0794e | 2002-03-03 03:03:52 +0000 | [diff] [blame] | 166 | if( eDest==SRT_Table || eDest==SRT_TempTable ){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 167 | sqliteVdbeAddOp(v, OP_MakeRecord, nColumn, 0); |
| 168 | sqliteVdbeAddOp(v, OP_NewRecno, iParm, 0); |
| 169 | sqliteVdbeAddOp(v, OP_Pull, 1, 0); |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 170 | sqliteVdbeAddOp(v, OP_PutIntKey, iParm, 0); |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 171 | }else |
| 172 | |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 173 | /* Construct a record from the query result, but instead of |
| 174 | ** saving that record, use it as a key to delete elements from |
| 175 | ** the temporary table iParm. |
| 176 | */ |
| 177 | if( eDest==SRT_Except ){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 178 | int addr = sqliteVdbeAddOp(v, OP_MakeRecord, nColumn, 0); |
| 179 | sqliteVdbeAddOp(v, OP_NotFound, iParm, addr+3); |
| 180 | sqliteVdbeAddOp(v, OP_Delete, iParm, 0); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 181 | }else |
| 182 | |
| 183 | /* If we are creating a set for an "expr IN (SELECT ...)" construct, |
| 184 | ** then there should be a single item on the stack. Write this |
| 185 | ** item into the set table with bogus data. |
| 186 | */ |
| 187 | if( eDest==SRT_Set ){ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 188 | assert( nColumn==1 ); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 189 | sqliteVdbeAddOp(v, OP_String, 0, 0); |
drh | 6b12545 | 2002-01-28 15:53:03 +0000 | [diff] [blame] | 190 | sqliteVdbeAddOp(v, OP_PutStrKey, iParm, 0); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 191 | }else |
| 192 | |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 193 | |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 194 | /* If this is a scalar select that is part of an expression, then |
| 195 | ** store the results in the appropriate memory cell and break out |
| 196 | ** of the scan loop. |
| 197 | */ |
| 198 | if( eDest==SRT_Mem ){ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 199 | assert( nColumn==1 ); |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 200 | sqliteVdbeAddOp(v, OP_MemStore, iParm, 1); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 201 | sqliteVdbeAddOp(v, OP_Goto, 0, iBreak); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 202 | }else |
| 203 | |
| 204 | /* If none of the above, send the data to the callback function. |
| 205 | */ |
| 206 | { |
drh | 9bbca4c | 2001-11-06 04:00:18 +0000 | [diff] [blame] | 207 | sqliteVdbeAddOp(v, OP_Callback, nColumn, iBreak); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 208 | } |
| 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | /* |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 213 | ** If the inner loop was generated using a non-null pOrderBy argument, |
| 214 | ** then the results were placed in a sorter. After the loop is terminated |
| 215 | ** we need to run the sorter and output the results. The following |
| 216 | ** routine generates the code needed to do that. |
| 217 | */ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 218 | static void generateSortTail(Vdbe *v, int nColumn){ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 219 | int end = sqliteVdbeMakeLabel(v); |
| 220 | int addr; |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 221 | sqliteVdbeAddOp(v, OP_Sort, 0, 0); |
| 222 | addr = sqliteVdbeAddOp(v, OP_SortNext, 0, end); |
drh | 9bbca4c | 2001-11-06 04:00:18 +0000 | [diff] [blame] | 223 | sqliteVdbeAddOp(v, OP_SortCallback, nColumn, end); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 224 | sqliteVdbeAddOp(v, OP_Goto, 0, addr); |
| 225 | sqliteVdbeResolveLabel(v, end); |
drh | a8b38d2 | 2001-11-01 14:41:34 +0000 | [diff] [blame] | 226 | sqliteVdbeAddOp(v, OP_SortReset, 0, 0); |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | /* |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 230 | ** Generate code that will tell the VDBE how many columns there |
| 231 | ** are in the result and the name for each column. This information |
| 232 | ** is used to provide "argc" and "azCol[]" values in the callback. |
| 233 | */ |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 234 | static void generateColumnNames( |
| 235 | Parse *pParse, /* Parser context */ |
| 236 | int base, /* VDBE cursor corresponding to first entry in pTabList */ |
| 237 | IdList *pTabList, /* List of tables */ |
| 238 | ExprList *pEList /* Expressions defining the result set */ |
| 239 | ){ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 240 | Vdbe *v = pParse->pVdbe; |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 241 | int i; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 242 | if( pParse->colNamesSet || v==0 || sqlite_malloc_failed ) return; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 243 | pParse->colNamesSet = 1; |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 244 | sqliteVdbeAddOp(v, OP_ColumnCount, pEList->nExpr, 0); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 245 | for(i=0; i<pEList->nExpr; i++){ |
| 246 | Expr *p; |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 247 | int showFullNames; |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 248 | if( pEList->a[i].zName ){ |
| 249 | char *zName = pEList->a[i].zName; |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 250 | sqliteVdbeAddOp(v, OP_ColumnName, i, 0); |
| 251 | sqliteVdbeChangeP3(v, -1, zName, strlen(zName)); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 252 | continue; |
| 253 | } |
| 254 | p = pEList->a[i].pExpr; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 255 | if( p==0 ) continue; |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 256 | showFullNames = (pParse->db->flags & SQLITE_FullColNames)!=0; |
| 257 | if( p->span.z && p->span.z[0] && !showFullNames ){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 258 | int addr = sqliteVdbeAddOp(v,OP_ColumnName, i, 0); |
| 259 | sqliteVdbeChangeP3(v, -1, p->span.z, p->span.n); |
drh | e1b6a5b | 2000-07-29 13:06:59 +0000 | [diff] [blame] | 260 | sqliteVdbeCompressSpace(v, addr); |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 261 | }else if( p->op==TK_COLUMN && pTabList ){ |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 262 | Table *pTab = pTabList->a[p->iTable - base].pTab; |
drh | 9766587 | 2002-02-13 23:22:53 +0000 | [diff] [blame] | 263 | char *zCol; |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 264 | int iCol = p->iColumn; |
| 265 | if( iCol<0 ) iCol = pTab->iPKey; |
drh | 9766587 | 2002-02-13 23:22:53 +0000 | [diff] [blame] | 266 | assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) ); |
| 267 | zCol = iCol<0 ? "_ROWID_" : pTab->aCol[iCol].zName; |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 268 | if( pTabList->nId>1 || showFullNames ){ |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 269 | char *zName = 0; |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 270 | char *zTab; |
| 271 | |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 272 | zTab = pTabList->a[p->iTable - base].zAlias; |
drh | 01a3466 | 2001-10-20 12:30:10 +0000 | [diff] [blame] | 273 | if( showFullNames || zTab==0 ) zTab = pTab->zName; |
drh | 9766587 | 2002-02-13 23:22:53 +0000 | [diff] [blame] | 274 | sqliteSetString(&zName, zTab, ".", zCol, 0); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 275 | sqliteVdbeAddOp(v, OP_ColumnName, i, 0); |
| 276 | sqliteVdbeChangeP3(v, -1, zName, strlen(zName)); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 277 | sqliteFree(zName); |
| 278 | }else{ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 279 | sqliteVdbeAddOp(v, OP_ColumnName, i, 0); |
drh | 22f70c3 | 2002-02-18 01:17:00 +0000 | [diff] [blame] | 280 | sqliteVdbeChangeP3(v, -1, zCol, 0); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 281 | } |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 282 | }else if( p->span.z && p->span.z[0] ){ |
| 283 | int addr = sqliteVdbeAddOp(v,OP_ColumnName, i, 0); |
| 284 | sqliteVdbeChangeP3(v, -1, p->span.z, p->span.n); |
| 285 | sqliteVdbeCompressSpace(v, addr); |
| 286 | }else{ |
| 287 | char zName[30]; |
| 288 | assert( p->op!=TK_COLUMN || pTabList==0 ); |
| 289 | sprintf(zName, "column%d", i+1); |
| 290 | sqliteVdbeAddOp(v, OP_ColumnName, i, 0); |
| 291 | sqliteVdbeChangeP3(v, -1, zName, strlen(zName)); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 292 | } |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | /* |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 297 | ** Name of the connection operator, used for error messages. |
| 298 | */ |
| 299 | static const char *selectOpName(int id){ |
| 300 | char *z; |
| 301 | switch( id ){ |
| 302 | case TK_ALL: z = "UNION ALL"; break; |
| 303 | case TK_INTERSECT: z = "INTERSECT"; break; |
| 304 | case TK_EXCEPT: z = "EXCEPT"; break; |
| 305 | default: z = "UNION"; break; |
| 306 | } |
| 307 | return z; |
| 308 | } |
| 309 | |
| 310 | /* |
drh | 22f70c3 | 2002-02-18 01:17:00 +0000 | [diff] [blame] | 311 | ** Given a SELECT statement, generate a Table structure that describes |
| 312 | ** the result set of that SELECT. |
| 313 | */ |
| 314 | Table *sqliteResultSetOfSelect(Parse *pParse, char *zTabName, Select *pSelect){ |
| 315 | Table *pTab; |
| 316 | int i; |
| 317 | ExprList *pEList; |
| 318 | static int fillInColumnList(Parse*, Select*); |
| 319 | |
| 320 | if( fillInColumnList(pParse, pSelect) ){ |
| 321 | return 0; |
| 322 | } |
| 323 | pTab = sqliteMalloc( sizeof(Table) ); |
| 324 | if( pTab==0 ){ |
| 325 | return 0; |
| 326 | } |
| 327 | pTab->zName = zTabName ? sqliteStrDup(zTabName) : 0; |
| 328 | pEList = pSelect->pEList; |
| 329 | pTab->nCol = pEList->nExpr; |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 330 | assert( pTab->nCol>0 ); |
drh | 22f70c3 | 2002-02-18 01:17:00 +0000 | [diff] [blame] | 331 | pTab->aCol = sqliteMalloc( sizeof(pTab->aCol[0])*pTab->nCol ); |
| 332 | for(i=0; i<pTab->nCol; i++){ |
| 333 | Expr *p; |
| 334 | if( pEList->a[i].zName ){ |
| 335 | pTab->aCol[i].zName = sqliteStrDup(pEList->a[i].zName); |
| 336 | }else if( (p=pEList->a[i].pExpr)->span.z && p->span.z[0] ){ |
| 337 | sqliteSetNString(&pTab->aCol[i].zName, p->span.z, p->span.n, 0); |
drh | d820cb1 | 2002-02-18 03:21:45 +0000 | [diff] [blame] | 338 | }else if( p->op==TK_DOT && p->pRight && p->pRight->token.z && |
| 339 | p->pRight->token.z[0] ){ |
| 340 | sqliteSetNString(&pTab->aCol[i].zName, |
| 341 | p->pRight->token.z, p->pRight->token.n, 0); |
drh | 22f70c3 | 2002-02-18 01:17:00 +0000 | [diff] [blame] | 342 | }else{ |
| 343 | char zBuf[30]; |
| 344 | sprintf(zBuf, "column%d", i+1); |
| 345 | pTab->aCol[i].zName = sqliteStrDup(zBuf); |
| 346 | } |
| 347 | } |
| 348 | pTab->iPKey = -1; |
| 349 | return pTab; |
| 350 | } |
| 351 | |
| 352 | /* |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 353 | ** For the given SELECT statement, do two things. |
| 354 | ** |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 355 | ** (1) Fill in the pTabList->a[].pTab fields in the IdList that |
drh | 22f70c3 | 2002-02-18 01:17:00 +0000 | [diff] [blame] | 356 | ** defines the set of tables that should be scanned. |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 357 | ** |
| 358 | ** (2) If the columns to be extracted variable (pEList) is NULL |
| 359 | ** (meaning that a "*" was used in the SQL statement) then |
| 360 | ** create a fake pEList containing the names of all columns |
| 361 | ** of all tables. |
| 362 | ** |
| 363 | ** Return 0 on success. If there are problems, leave an error message |
| 364 | ** in pParse and return non-zero. |
| 365 | */ |
| 366 | static int fillInColumnList(Parse *pParse, Select *p){ |
drh | 7c917d1 | 2001-12-16 20:05:05 +0000 | [diff] [blame] | 367 | int i, j, k; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 368 | IdList *pTabList; |
| 369 | ExprList *pEList; |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 370 | Table *pTab; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 371 | |
| 372 | if( p==0 || p->pSrc==0 ) return 1; |
| 373 | pTabList = p->pSrc; |
| 374 | pEList = p->pEList; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 375 | |
| 376 | /* Look up every table in the table list. |
| 377 | */ |
| 378 | for(i=0; i<pTabList->nId; i++){ |
| 379 | if( pTabList->a[i].pTab ){ |
| 380 | /* This routine has run before! No need to continue */ |
| 381 | return 0; |
| 382 | } |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 383 | if( pTabList->a[i].zName==0 ){ |
drh | 22f70c3 | 2002-02-18 01:17:00 +0000 | [diff] [blame] | 384 | /* A sub-query in the FROM clause of a SELECT */ |
drh | 22f70c3 | 2002-02-18 01:17:00 +0000 | [diff] [blame] | 385 | assert( pTabList->a[i].pSelect!=0 ); |
| 386 | pTabList->a[i].pTab = pTab = |
| 387 | sqliteResultSetOfSelect(pParse, pTabList->a[i].zAlias, |
| 388 | pTabList->a[i].pSelect); |
| 389 | if( pTab==0 ){ |
| 390 | return 1; |
| 391 | } |
| 392 | pTab->isTransient = 1; |
| 393 | }else{ |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 394 | /* An ordinary table or view name in the FROM clause */ |
| 395 | pTabList->a[i].pTab = pTab = |
| 396 | sqliteFindTable(pParse->db, pTabList->a[i].zName); |
| 397 | if( pTab==0 ){ |
drh | 22f70c3 | 2002-02-18 01:17:00 +0000 | [diff] [blame] | 398 | sqliteSetString(&pParse->zErrMsg, "no such table: ", |
| 399 | pTabList->a[i].zName, 0); |
| 400 | pParse->nErr++; |
| 401 | return 1; |
| 402 | } |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 403 | if( pTab->pSelect ){ |
drh | 417be79 | 2002-03-03 18:59:40 +0000 | [diff] [blame] | 404 | if( sqliteViewGetColumnNames(pParse, pTab) ){ |
| 405 | return 1; |
| 406 | } |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 407 | pTabList->a[i].pSelect = sqliteSelectDup(pTab->pSelect); |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 408 | } |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 409 | } |
| 410 | } |
| 411 | |
drh | 7c917d1 | 2001-12-16 20:05:05 +0000 | [diff] [blame] | 412 | /* For every "*" that occurs in the column list, insert the names of |
| 413 | ** all columns in all tables. The parser inserted a special expression |
| 414 | ** with the TK_ALL operator for each "*" that it found in the column list. |
| 415 | ** The following code just has to locate the TK_ALL expressions and expand |
| 416 | ** each one to the list of all columns in all tables. |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 417 | */ |
drh | 7c917d1 | 2001-12-16 20:05:05 +0000 | [diff] [blame] | 418 | for(k=0; k<pEList->nExpr; k++){ |
| 419 | if( pEList->a[k].pExpr->op==TK_ALL ) break; |
| 420 | } |
| 421 | if( k<pEList->nExpr ){ |
| 422 | struct ExprList_item *a = pEList->a; |
| 423 | ExprList *pNew = 0; |
| 424 | for(k=0; k<pEList->nExpr; k++){ |
| 425 | if( a[k].pExpr->op!=TK_ALL ){ |
| 426 | pNew = sqliteExprListAppend(pNew, a[k].pExpr, 0); |
| 427 | pNew->a[pNew->nExpr-1].zName = a[k].zName; |
| 428 | a[k].pExpr = 0; |
| 429 | a[k].zName = 0; |
| 430 | }else{ |
| 431 | for(i=0; i<pTabList->nId; i++){ |
| 432 | Table *pTab = pTabList->a[i].pTab; |
| 433 | for(j=0; j<pTab->nCol; j++){ |
drh | 22f70c3 | 2002-02-18 01:17:00 +0000 | [diff] [blame] | 434 | Expr *pExpr, *pLeft, *pRight; |
| 435 | pRight = sqliteExpr(TK_ID, 0, 0, 0); |
| 436 | if( pRight==0 ) break; |
| 437 | pRight->token.z = pTab->aCol[j].zName; |
| 438 | pRight->token.n = strlen(pTab->aCol[j].zName); |
| 439 | if( pTab->zName ){ |
| 440 | pLeft = sqliteExpr(TK_ID, 0, 0, 0); |
| 441 | if( pLeft==0 ) break; |
| 442 | if( pTabList->a[i].zAlias && pTabList->a[i].zAlias[0] ){ |
| 443 | pLeft->token.z = pTabList->a[i].zAlias; |
| 444 | pLeft->token.n = strlen(pTabList->a[i].zAlias); |
| 445 | }else{ |
| 446 | pLeft->token.z = pTab->zName; |
| 447 | pLeft->token.n = strlen(pTab->zName); |
| 448 | } |
| 449 | pExpr = sqliteExpr(TK_DOT, pLeft, pRight, 0); |
| 450 | if( pExpr==0 ) break; |
drh | 7c917d1 | 2001-12-16 20:05:05 +0000 | [diff] [blame] | 451 | }else{ |
drh | 22f70c3 | 2002-02-18 01:17:00 +0000 | [diff] [blame] | 452 | pExpr = pRight; |
| 453 | pExpr->span = pExpr->token; |
drh | 7c917d1 | 2001-12-16 20:05:05 +0000 | [diff] [blame] | 454 | } |
drh | 7c917d1 | 2001-12-16 20:05:05 +0000 | [diff] [blame] | 455 | pNew = sqliteExprListAppend(pNew, pExpr, 0); |
| 456 | } |
drh | 17e24df | 2001-11-06 14:10:41 +0000 | [diff] [blame] | 457 | } |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 458 | } |
| 459 | } |
drh | 7c917d1 | 2001-12-16 20:05:05 +0000 | [diff] [blame] | 460 | sqliteExprListDelete(pEList); |
| 461 | p->pEList = pNew; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 462 | } |
| 463 | return 0; |
| 464 | } |
| 465 | |
| 466 | /* |
drh | ff78bd2 | 2002-02-27 01:47:11 +0000 | [diff] [blame] | 467 | ** This routine recursively unlinks the Select.pSrc.a[].pTab pointers |
| 468 | ** in a select structure. It just sets the pointers to NULL. This |
| 469 | ** routine is recursive in the sense that if the Select.pSrc.a[].pSelect |
| 470 | ** pointer is not NULL, this routine is called recursively on that pointer. |
| 471 | ** |
| 472 | ** This routine is called on the Select structure that defines a |
| 473 | ** VIEW in order to undo any bindings to tables. This is necessary |
| 474 | ** because those tables might be DROPed by a subsequent SQL command. |
| 475 | */ |
| 476 | void sqliteSelectUnbind(Select *p){ |
| 477 | int i; |
| 478 | IdList *pSrc = p->pSrc; |
| 479 | Table *pTab; |
| 480 | if( p==0 ) return; |
| 481 | for(i=0; i<pSrc->nId; i++){ |
| 482 | if( (pTab = pSrc->a[i].pTab)!=0 ){ |
| 483 | if( pTab->isTransient ){ |
| 484 | sqliteDeleteTable(0, pTab); |
| 485 | sqliteSelectDelete(pSrc->a[i].pSelect); |
| 486 | pSrc->a[i].pSelect = 0; |
| 487 | } |
| 488 | pSrc->a[i].pTab = 0; |
| 489 | if( pSrc->a[i].pSelect ){ |
| 490 | sqliteSelectUnbind(pSrc->a[i].pSelect); |
| 491 | } |
| 492 | } |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | /* |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 497 | ** This routine associates entries in an ORDER BY expression list with |
| 498 | ** columns in a result. For each ORDER BY expression, the opcode of |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 499 | ** 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] | 500 | ** the top-level node is filled in with column number and the iTable |
| 501 | ** value of the top-level node is filled with iTable parameter. |
| 502 | ** |
| 503 | ** If there are prior SELECT clauses, they are processed first. A match |
| 504 | ** in an earlier SELECT takes precedence over a later SELECT. |
| 505 | ** |
| 506 | ** Any entry that does not match is flagged as an error. The number |
| 507 | ** of errors is returned. |
| 508 | */ |
| 509 | static int matchOrderbyToColumn( |
| 510 | Parse *pParse, /* A place to leave error messages */ |
| 511 | Select *pSelect, /* Match to result columns of this SELECT */ |
| 512 | ExprList *pOrderBy, /* The ORDER BY values to match against columns */ |
| 513 | int iTable, /* Insert this this value in iTable */ |
| 514 | int mustComplete /* If TRUE all ORDER BYs must match */ |
| 515 | ){ |
| 516 | int nErr = 0; |
| 517 | int i, j; |
| 518 | ExprList *pEList; |
| 519 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 520 | if( pSelect==0 || pOrderBy==0 ) return 1; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 521 | if( mustComplete ){ |
| 522 | for(i=0; i<pOrderBy->nExpr; i++){ pOrderBy->a[i].done = 0; } |
| 523 | } |
| 524 | if( fillInColumnList(pParse, pSelect) ){ |
| 525 | return 1; |
| 526 | } |
| 527 | if( pSelect->pPrior ){ |
drh | 92cd52f | 2000-06-08 01:55:29 +0000 | [diff] [blame] | 528 | if( matchOrderbyToColumn(pParse, pSelect->pPrior, pOrderBy, iTable, 0) ){ |
| 529 | return 1; |
| 530 | } |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 531 | } |
| 532 | pEList = pSelect->pEList; |
| 533 | for(i=0; i<pOrderBy->nExpr; i++){ |
| 534 | Expr *pE = pOrderBy->a[i].pExpr; |
drh | 92cd52f | 2000-06-08 01:55:29 +0000 | [diff] [blame] | 535 | int match = 0; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 536 | if( pOrderBy->a[i].done ) continue; |
| 537 | for(j=0; j<pEList->nExpr; j++){ |
drh | 4cfa793 | 2000-06-08 15:10:46 +0000 | [diff] [blame] | 538 | if( pEList->a[j].zName && (pE->op==TK_ID || pE->op==TK_STRING) ){ |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 539 | char *zName, *zLabel; |
| 540 | zName = pEList->a[j].zName; |
| 541 | assert( pE->token.z ); |
| 542 | zLabel = sqliteStrNDup(pE->token.z, pE->token.n); |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 543 | sqliteDequote(zLabel); |
| 544 | if( sqliteStrICmp(zName, zLabel)==0 ){ |
| 545 | match = 1; |
| 546 | } |
drh | 6e142f5 | 2000-06-08 13:36:40 +0000 | [diff] [blame] | 547 | sqliteFree(zLabel); |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 548 | } |
drh | 4cfa793 | 2000-06-08 15:10:46 +0000 | [diff] [blame] | 549 | if( match==0 && sqliteExprCompare(pE, pEList->a[j].pExpr) ){ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 550 | match = 1; |
| 551 | } |
| 552 | if( match ){ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 553 | pE->op = TK_COLUMN; |
| 554 | pE->iColumn = j; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 555 | pE->iTable = iTable; |
| 556 | pOrderBy->a[i].done = 1; |
| 557 | break; |
| 558 | } |
| 559 | } |
drh | 92cd52f | 2000-06-08 01:55:29 +0000 | [diff] [blame] | 560 | if( !match && mustComplete ){ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 561 | char zBuf[30]; |
| 562 | sprintf(zBuf,"%d",i+1); |
| 563 | sqliteSetString(&pParse->zErrMsg, "ORDER BY term number ", zBuf, |
| 564 | " does not match any result column", 0); |
| 565 | pParse->nErr++; |
| 566 | nErr++; |
| 567 | break; |
| 568 | } |
| 569 | } |
| 570 | return nErr; |
| 571 | } |
| 572 | |
| 573 | /* |
| 574 | ** Get a VDBE for the given parser context. Create a new one if necessary. |
| 575 | ** If an error occurs, return NULL and leave a message in pParse. |
| 576 | */ |
| 577 | Vdbe *sqliteGetVdbe(Parse *pParse){ |
| 578 | Vdbe *v = pParse->pVdbe; |
| 579 | if( v==0 ){ |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 580 | v = pParse->pVdbe = sqliteVdbeCreate(pParse->db); |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 581 | } |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 582 | return v; |
| 583 | } |
| 584 | |
| 585 | |
| 586 | /* |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 587 | ** This routine is called to process a query that is really the union |
| 588 | ** or intersection of two or more separate queries. |
| 589 | */ |
| 590 | static int multiSelect(Parse *pParse, Select *p, int eDest, int iParm){ |
drh | 10e5e3c | 2000-06-08 00:19:02 +0000 | [diff] [blame] | 591 | int rc; /* Success code from a subroutine */ |
| 592 | Select *pPrior; /* Another SELECT immediately to our left */ |
| 593 | Vdbe *v; /* Generate code to this VDBE */ |
| 594 | int base; /* Baseline value for pParse->nTab */ |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 595 | |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 596 | /* Make sure there is no ORDER BY clause on prior SELECTs. Only the |
| 597 | ** last SELECT in the series may have an ORDER BY. |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 598 | */ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 599 | if( p==0 || p->pPrior==0 ) return 1; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 600 | pPrior = p->pPrior; |
| 601 | if( pPrior->pOrderBy ){ |
| 602 | sqliteSetString(&pParse->zErrMsg,"ORDER BY clause should come after ", |
| 603 | selectOpName(p->op), " not before", 0); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 604 | pParse->nErr++; |
| 605 | return 1; |
| 606 | } |
| 607 | |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 608 | /* Make sure we have a valid query engine. If not, create a new one. |
| 609 | */ |
| 610 | v = sqliteGetVdbe(pParse); |
| 611 | if( v==0 ) return 1; |
| 612 | |
drh | 1cc3d75 | 2002-03-23 00:31:29 +0000 | [diff] [blame^] | 613 | /* Create the destination temporary table if necessary |
| 614 | */ |
| 615 | if( eDest==SRT_TempTable ){ |
| 616 | sqliteVdbeAddOp(v, OP_OpenTemp, iParm, 0); |
| 617 | eDest = SRT_Table; |
| 618 | } |
| 619 | |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 620 | /* Process the UNION or INTERSECTION |
| 621 | */ |
drh | 10e5e3c | 2000-06-08 00:19:02 +0000 | [diff] [blame] | 622 | base = pParse->nTab; |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 623 | switch( p->op ){ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 624 | case TK_ALL: |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 625 | case TK_EXCEPT: |
| 626 | case TK_UNION: { |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 627 | int unionTab; /* Cursor number of the temporary table holding result */ |
| 628 | int op; /* One of the SRT_ operations to apply to self */ |
| 629 | int priorOp; /* The SRT_ operation to apply to prior selects */ |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 630 | |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 631 | priorOp = p->op==TK_ALL ? SRT_Table : SRT_Union; |
| 632 | if( eDest==priorOp ){ |
| 633 | /* We can reuse a temporary table generated by a SELECT to our |
| 634 | ** right. This also means we are not the right-most select and so |
| 635 | ** we cannot have an ORDER BY clause |
| 636 | */ |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 637 | unionTab = iParm; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 638 | assert( p->pOrderBy==0 ); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 639 | }else{ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 640 | /* We will need to create our own temporary table to hold the |
| 641 | ** intermediate results. |
| 642 | */ |
| 643 | unionTab = pParse->nTab++; |
| 644 | if( p->pOrderBy |
| 645 | && matchOrderbyToColumn(pParse, p, p->pOrderBy, unionTab, 1) ){ |
| 646 | return 1; |
| 647 | } |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 648 | if( p->op!=TK_ALL ){ |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 649 | sqliteVdbeAddOp(v, OP_OpenTemp, unionTab, 1); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 650 | sqliteVdbeAddOp(v, OP_KeyAsData, unionTab, 1); |
drh | 345fda3 | 2001-01-15 22:51:08 +0000 | [diff] [blame] | 651 | }else{ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 652 | sqliteVdbeAddOp(v, OP_OpenTemp, unionTab, 0); |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 653 | } |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 654 | } |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 655 | |
| 656 | /* Code the SELECT statements to our left |
| 657 | */ |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 658 | rc = sqliteSelect(pParse, pPrior, priorOp, unionTab, 0, 0, 0); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 659 | if( rc ) return rc; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 660 | |
| 661 | /* Code the current SELECT statement |
| 662 | */ |
| 663 | switch( p->op ){ |
| 664 | case TK_EXCEPT: op = SRT_Except; break; |
| 665 | case TK_UNION: op = SRT_Union; break; |
| 666 | case TK_ALL: op = SRT_Table; break; |
| 667 | } |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 668 | p->pPrior = 0; |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 669 | rc = sqliteSelect(pParse, p, op, unionTab, 0, 0, 0); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 670 | p->pPrior = pPrior; |
| 671 | if( rc ) return rc; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 672 | |
| 673 | /* Convert the data in the temporary table into whatever form |
| 674 | ** it is that we currently need. |
| 675 | */ |
| 676 | if( eDest!=priorOp ){ |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 677 | int iCont, iBreak, iStart; |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 678 | assert( p->pEList ); |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 679 | generateColumnNames(pParse, p->base, 0, p->pEList); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 680 | iBreak = sqliteVdbeMakeLabel(v); |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 681 | iCont = sqliteVdbeMakeLabel(v); |
| 682 | sqliteVdbeAddOp(v, OP_Rewind, unionTab, iBreak); |
| 683 | iStart = sqliteVdbeCurrentAddr(v); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 684 | rc = selectInnerLoop(pParse, 0, unionTab, p->pEList->nExpr, |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 685 | p->pOrderBy, -1, eDest, iParm, |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 686 | iCont, iBreak); |
| 687 | if( rc ) return 1; |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 688 | sqliteVdbeResolveLabel(v, iCont); |
| 689 | sqliteVdbeAddOp(v, OP_Next, unionTab, iStart); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 690 | sqliteVdbeResolveLabel(v, iBreak); |
| 691 | sqliteVdbeAddOp(v, OP_Close, unionTab, 0); |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 692 | if( p->pOrderBy ){ |
| 693 | generateSortTail(v, p->pEList->nExpr); |
| 694 | } |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 695 | } |
| 696 | break; |
| 697 | } |
| 698 | case TK_INTERSECT: { |
| 699 | int tab1, tab2; |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 700 | int iCont, iBreak, iStart; |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 701 | |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 702 | /* INTERSECT is different from the others since it requires |
drh | 6206d50 | 2000-06-19 19:09:08 +0000 | [diff] [blame] | 703 | ** two temporary tables. Hence it has its own case. Begin |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 704 | ** by allocating the tables we will need. |
| 705 | */ |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 706 | tab1 = pParse->nTab++; |
| 707 | tab2 = pParse->nTab++; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 708 | if( p->pOrderBy && matchOrderbyToColumn(pParse,p,p->pOrderBy,tab1,1) ){ |
| 709 | return 1; |
| 710 | } |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 711 | sqliteVdbeAddOp(v, OP_OpenTemp, tab1, 1); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 712 | sqliteVdbeAddOp(v, OP_KeyAsData, tab1, 1); |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 713 | |
| 714 | /* Code the SELECTs to our left into temporary table "tab1". |
| 715 | */ |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 716 | rc = sqliteSelect(pParse, pPrior, SRT_Union, tab1, 0, 0, 0); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 717 | if( rc ) return rc; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 718 | |
| 719 | /* Code the current SELECT into temporary table "tab2" |
| 720 | */ |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 721 | sqliteVdbeAddOp(v, OP_OpenTemp, tab2, 1); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 722 | sqliteVdbeAddOp(v, OP_KeyAsData, tab2, 1); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 723 | p->pPrior = 0; |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 724 | rc = sqliteSelect(pParse, p, SRT_Union, tab2, 0, 0, 0); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 725 | p->pPrior = pPrior; |
| 726 | if( rc ) return rc; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 727 | |
| 728 | /* Generate code to take the intersection of the two temporary |
| 729 | ** tables. |
| 730 | */ |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 731 | assert( p->pEList ); |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 732 | generateColumnNames(pParse, p->base, 0, p->pEList); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 733 | iBreak = sqliteVdbeMakeLabel(v); |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 734 | iCont = sqliteVdbeMakeLabel(v); |
| 735 | sqliteVdbeAddOp(v, OP_Rewind, tab1, iBreak); |
| 736 | iStart = sqliteVdbeAddOp(v, OP_FullKey, tab1, 0); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 737 | sqliteVdbeAddOp(v, OP_NotFound, tab2, iCont); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 738 | rc = selectInnerLoop(pParse, 0, tab1, p->pEList->nExpr, |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 739 | p->pOrderBy, -1, eDest, iParm, |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 740 | iCont, iBreak); |
| 741 | if( rc ) return 1; |
drh | 6b56344 | 2001-11-07 16:48:26 +0000 | [diff] [blame] | 742 | sqliteVdbeResolveLabel(v, iCont); |
| 743 | sqliteVdbeAddOp(v, OP_Next, tab1, iStart); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 744 | sqliteVdbeResolveLabel(v, iBreak); |
| 745 | sqliteVdbeAddOp(v, OP_Close, tab2, 0); |
| 746 | sqliteVdbeAddOp(v, OP_Close, tab1, 0); |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 747 | if( p->pOrderBy ){ |
| 748 | generateSortTail(v, p->pEList->nExpr); |
| 749 | } |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 750 | break; |
| 751 | } |
| 752 | } |
| 753 | assert( p->pEList && pPrior->pEList ); |
| 754 | if( p->pEList->nExpr!=pPrior->pEList->nExpr ){ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 755 | sqliteSetString(&pParse->zErrMsg, "SELECTs to the left and right of ", |
| 756 | selectOpName(p->op), " do not have the same number of result columns", 0); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 757 | pParse->nErr++; |
| 758 | return 1; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 759 | } |
drh | 10e5e3c | 2000-06-08 00:19:02 +0000 | [diff] [blame] | 760 | pParse->nTab = base; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 761 | return 0; |
| 762 | } |
| 763 | |
| 764 | /* |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 765 | ** Recursively scan through an expression tree. For every reference |
| 766 | ** to a column in table number iFrom, change that reference to the |
| 767 | ** same column in table number iTo. |
| 768 | */ |
| 769 | static void changeTables(Expr *pExpr, int iFrom, int iTo){ |
| 770 | if( pExpr==0 ) return; |
| 771 | if( pExpr->op==TK_COLUMN && pExpr->iTable==iFrom ){ |
| 772 | pExpr->iTable = iTo; |
| 773 | }else{ |
drh | 1b2e032 | 2002-03-03 02:49:51 +0000 | [diff] [blame] | 774 | static void changeTablesInList(ExprList*, int, int); |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 775 | changeTables(pExpr->pLeft, iFrom, iTo); |
| 776 | changeTables(pExpr->pRight, iFrom, iTo); |
drh | 1b2e032 | 2002-03-03 02:49:51 +0000 | [diff] [blame] | 777 | changeTablesInList(pExpr->pList, iFrom, iTo); |
| 778 | } |
| 779 | } |
| 780 | static void changeTablesInList(ExprList *pList, int iFrom, int iTo){ |
| 781 | if( pList ){ |
| 782 | int i; |
| 783 | for(i=0; i<pList->nExpr; i++){ |
| 784 | changeTables(pList->a[i].pExpr, iFrom, iTo); |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 785 | } |
| 786 | } |
| 787 | } |
| 788 | |
| 789 | /* |
| 790 | ** Scan through the expression pExpr. Replace every reference to |
| 791 | ** a column in table number iTable with a copy of the corresponding |
drh | 84e5920 | 2002-03-14 14:33:31 +0000 | [diff] [blame] | 792 | ** entry in pEList. (But leave references to the ROWID column |
| 793 | ** unchanged.) When making a copy of an expression in pEList, change |
| 794 | ** references to columns in table iSub into references to table iTable. |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 795 | ** |
| 796 | ** This routine is part of the flattening procedure. A subquery |
| 797 | ** whose result set is defined by pEList appears as entry in the |
| 798 | ** FROM clause of a SELECT such that the VDBE cursor assigned to that |
| 799 | ** FORM clause entry is iTable. This routine make the necessary |
| 800 | ** changes to pExpr so that it refers directly to the source table |
| 801 | ** of the subquery rather the result set of the subquery. |
| 802 | */ |
| 803 | static void substExpr(Expr *pExpr, int iTable, ExprList *pEList, int iSub){ |
| 804 | if( pExpr==0 ) return; |
drh | 84e5920 | 2002-03-14 14:33:31 +0000 | [diff] [blame] | 805 | if( pExpr->op==TK_COLUMN && pExpr->iTable==iTable && pExpr->iColumn>=0 ){ |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 806 | Expr *pNew; |
drh | 84e5920 | 2002-03-14 14:33:31 +0000 | [diff] [blame] | 807 | assert( pEList!=0 && pExpr->iColumn<pEList->nExpr ); |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 808 | assert( pExpr->pLeft==0 && pExpr->pRight==0 && pExpr->pList==0 ); |
| 809 | pNew = pEList->a[pExpr->iColumn].pExpr; |
| 810 | assert( pNew!=0 ); |
| 811 | pExpr->op = pNew->op; |
| 812 | pExpr->pLeft = sqliteExprDup(pNew->pLeft); |
| 813 | pExpr->pRight = sqliteExprDup(pNew->pRight); |
| 814 | pExpr->pList = sqliteExprListDup(pNew->pList); |
| 815 | pExpr->iTable = pNew->iTable; |
| 816 | pExpr->iColumn = pNew->iColumn; |
| 817 | pExpr->iAgg = pNew->iAgg; |
drh | 1b2e032 | 2002-03-03 02:49:51 +0000 | [diff] [blame] | 818 | pExpr->token = pNew->token; |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 819 | if( iSub!=iTable ){ |
| 820 | changeTables(pExpr, iSub, iTable); |
| 821 | } |
| 822 | }else{ |
| 823 | static void substExprList(ExprList*,int,ExprList*,int); |
| 824 | substExpr(pExpr->pLeft, iTable, pEList, iSub); |
| 825 | substExpr(pExpr->pRight, iTable, pEList, iSub); |
| 826 | substExprList(pExpr->pList, iTable, pEList, iSub); |
| 827 | } |
| 828 | } |
| 829 | static void |
| 830 | substExprList(ExprList *pList, int iTable, ExprList *pEList, int iSub){ |
| 831 | int i; |
| 832 | if( pList==0 ) return; |
| 833 | for(i=0; i<pList->nExpr; i++){ |
| 834 | substExpr(pList->a[i].pExpr, iTable, pEList, iSub); |
| 835 | } |
| 836 | } |
| 837 | |
| 838 | /* |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 839 | ** This routine attempts to flatten subqueries in order to speed |
| 840 | ** execution. It returns 1 if it makes changes and 0 if no flattening |
| 841 | ** occurs. |
| 842 | ** |
| 843 | ** To understand the concept of flattening, consider the following |
| 844 | ** query: |
| 845 | ** |
| 846 | ** SELECT a FROM (SELECT x+y AS a FROM t1 WHERE z<100) WHERE a>5 |
| 847 | ** |
| 848 | ** The default way of implementing this query is to execute the |
| 849 | ** subquery first and store the results in a temporary table, then |
| 850 | ** run the outer query on that temporary table. This requires two |
| 851 | ** passes over the data. Furthermore, because the temporary table |
| 852 | ** has no indices, the WHERE clause on the outer query cannot be |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 853 | ** optimized. |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 854 | ** |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 855 | ** This routine attempts to rewrite queries such as the above into |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 856 | ** a single flat select, like this: |
| 857 | ** |
| 858 | ** SELECT x+y AS a FROM t1 WHERE z<100 AND a>5 |
| 859 | ** |
| 860 | ** The code generated for this simpification gives the same result |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 861 | ** but only has to scan the data once. And because indices might |
| 862 | ** exist on the table t1, a complete scan of the data might be |
| 863 | ** avoided. |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 864 | ** |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 865 | ** Flattening is only attempted if all of the following are true: |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 866 | ** |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 867 | ** (1) The subquery and the outer query do not both use aggregates. |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 868 | ** |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 869 | ** (2) The subquery is not an aggregate or the outer query is not a join. |
| 870 | ** |
| 871 | ** (3) The subquery is not a join. |
| 872 | ** |
| 873 | ** (4) The subquery is not DISTINCT or the outer query is not a join. |
| 874 | ** |
| 875 | ** (5) The subquery is not DISTINCT or the outer query does not use |
| 876 | ** aggregates. |
| 877 | ** |
| 878 | ** (6) The subquery does not use aggregates or the outer query is not |
| 879 | ** DISTINCT. |
| 880 | ** |
| 881 | ** In this routine, the "p" parameter is a pointer to the outer query. |
| 882 | ** The subquery is p->pSrc->a[iFrom]. isAgg is true if the outer query |
| 883 | ** uses aggregates and subqueryIsAgg is true if the subquery uses aggregates. |
| 884 | ** |
| 885 | ** If flattening is not attempted, this routine is a no-op and return 0. |
| 886 | ** If flattening is attempted this routine returns 1. |
| 887 | ** |
| 888 | ** All of the expression analysis must occur on both the outer query and |
| 889 | ** the subquery before this routine runs. |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 890 | */ |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 891 | int flattenSubquery(Select *p, int iFrom, int isAgg, int subqueryIsAgg){ |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 892 | Select *pSub; |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 893 | IdList *pSrc, *pSubSrc; |
| 894 | ExprList *pList; |
| 895 | int i; |
| 896 | int iParent, iSub; |
| 897 | Expr *pWhere; |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 898 | |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 899 | /* Check to see if flattening is permitted. Return 0 if not. |
| 900 | */ |
| 901 | if( p==0 ) return 0; |
| 902 | pSrc = p->pSrc; |
| 903 | assert( pSrc && iFrom>=0 && iFrom<pSrc->nId ); |
| 904 | pSub = pSrc->a[iFrom].pSelect; |
| 905 | assert( pSub!=0 ); |
| 906 | if( isAgg && subqueryIsAgg ) return 0; |
| 907 | if( subqueryIsAgg && pSrc->nId>1 ) return 0; |
| 908 | pSubSrc = pSub->pSrc; |
| 909 | assert( pSubSrc ); |
| 910 | if( pSubSrc->nId>1 ) return 0; |
| 911 | if( pSub->isDistinct && pSrc->nId>1 ) return 0; |
| 912 | if( pSub->isDistinct && isAgg ) return 0; |
| 913 | if( p->isDistinct && subqueryIsAgg ) return 0; |
| 914 | |
| 915 | /* If we reach this point, it means flatting is permitted for the |
| 916 | ** i-th entry of the FROM clause in the outer query. |
| 917 | */ |
| 918 | iParent = p->base + iFrom; |
| 919 | iSub = pSub->base; |
| 920 | substExprList(p->pEList, iParent, pSub->pEList, iSub); |
| 921 | pList = p->pEList; |
| 922 | for(i=0; i<pList->nExpr; i++){ |
| 923 | if( pList->a[i].zName==0 ){ |
| 924 | Expr *pExpr = pList->a[i].pExpr; |
| 925 | pList->a[i].zName = sqliteStrNDup(pExpr->span.z, pExpr->span.n); |
| 926 | } |
| 927 | } |
drh | 1b2e032 | 2002-03-03 02:49:51 +0000 | [diff] [blame] | 928 | if( isAgg ){ |
| 929 | substExprList(p->pGroupBy, iParent, pSub->pEList, iSub); |
| 930 | substExpr(p->pHaving, iParent, pSub->pEList, iSub); |
| 931 | } |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 932 | substExprList(p->pOrderBy, iParent, pSub->pEList, iSub); |
| 933 | if( pSub->pWhere ){ |
| 934 | pWhere = sqliteExprDup(pSub->pWhere); |
| 935 | if( iParent!=iSub ){ |
| 936 | changeTables(pWhere, iSub, iParent); |
| 937 | } |
| 938 | }else{ |
| 939 | pWhere = 0; |
| 940 | } |
| 941 | if( subqueryIsAgg ){ |
| 942 | assert( p->pHaving==0 ); |
drh | 1b2e032 | 2002-03-03 02:49:51 +0000 | [diff] [blame] | 943 | p->pHaving = p->pWhere; |
| 944 | p->pWhere = pWhere; |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 945 | substExpr(p->pHaving, iParent, pSub->pEList, iSub); |
drh | 1b2e032 | 2002-03-03 02:49:51 +0000 | [diff] [blame] | 946 | if( pSub->pHaving ){ |
| 947 | Expr *pHaving = sqliteExprDup(pSub->pHaving); |
| 948 | if( iParent!=iSub ){ |
| 949 | changeTables(pHaving, iSub, iParent); |
| 950 | } |
| 951 | if( p->pHaving ){ |
| 952 | p->pHaving = sqliteExpr(TK_AND, p->pHaving, pHaving, 0); |
| 953 | }else{ |
| 954 | p->pHaving = pHaving; |
| 955 | } |
| 956 | } |
| 957 | assert( p->pGroupBy==0 ); |
| 958 | p->pGroupBy = sqliteExprListDup(pSub->pGroupBy); |
| 959 | if( iParent!=iSub ){ |
| 960 | changeTablesInList(p->pGroupBy, iSub, iParent); |
| 961 | } |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 962 | }else if( p->pWhere==0 ){ |
| 963 | p->pWhere = pWhere; |
| 964 | }else{ |
| 965 | substExpr(p->pWhere, iParent, pSub->pEList, iSub); |
| 966 | if( pWhere ){ |
| 967 | p->pWhere = sqliteExpr(TK_AND, p->pWhere, pWhere, 0); |
| 968 | } |
| 969 | } |
| 970 | p->isDistinct = p->isDistinct || pSub->isDistinct; |
| 971 | if( pSrc->a[iFrom].pTab && pSrc->a[iFrom].pTab->isTransient ){ |
| 972 | sqliteDeleteTable(0, pSrc->a[iFrom].pTab); |
| 973 | } |
| 974 | pSrc->a[iFrom].pTab = pSubSrc->a[0].pTab; |
| 975 | pSubSrc->a[0].pTab = 0; |
| 976 | pSrc->a[iFrom].pSelect = pSubSrc->a[0].pSelect; |
| 977 | pSubSrc->a[0].pSelect = 0; |
| 978 | sqliteSelectDelete(pSub); |
| 979 | return 1; |
| 980 | } |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 981 | |
| 982 | /* |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 983 | ** Analyze the SELECT statement passed in as an argument to see if it |
| 984 | ** is a simple min() or max() query. If it is and this query can be |
| 985 | ** satisfied using a single seek to the beginning or end of an index, |
| 986 | ** then generate the code for this SELECT return 1. If this is not a |
| 987 | ** simple min() or max() query, then return 0; |
| 988 | ** |
| 989 | ** A simply min() or max() query looks like this: |
| 990 | ** |
| 991 | ** SELECT min(a) FROM table; |
| 992 | ** SELECT max(a) FROM table; |
| 993 | ** |
| 994 | ** The query may have only a single table in its FROM argument. There |
| 995 | ** can be no GROUP BY or HAVING or WHERE clauses. The result set must |
| 996 | ** be the min() or max() of a single column of the table. The column |
| 997 | ** in the min() or max() function must be indexed. |
| 998 | ** |
| 999 | ** The parameters to this routine are the same as for sqliteSelect(). |
| 1000 | ** See the header comment on that routine for additional information. |
| 1001 | */ |
| 1002 | static int simpleMinMaxQuery(Parse *pParse, Select *p, int eDest, int iParm){ |
| 1003 | Expr *pExpr; |
| 1004 | int iCol; |
| 1005 | Table *pTab; |
| 1006 | Index *pIdx; |
| 1007 | int base; |
| 1008 | Vdbe *v; |
| 1009 | int openOp; |
| 1010 | int seekOp; |
| 1011 | int cont; |
| 1012 | ExprList eList; |
| 1013 | struct ExprList_item eListItem; |
| 1014 | |
| 1015 | /* Check to see if this query is a simple min() or max() query. Return |
| 1016 | ** zero if it is not. |
| 1017 | */ |
| 1018 | if( p->pGroupBy || p->pHaving || p->pWhere ) return 0; |
| 1019 | if( p->pSrc->nId!=1 ) return 0; |
| 1020 | if( p->pEList->nExpr!=1 ) return 0; |
| 1021 | pExpr = p->pEList->a[0].pExpr; |
| 1022 | if( pExpr->op!=TK_AGG_FUNCTION ) return 0; |
| 1023 | if( pExpr->pList==0 || pExpr->pList->nExpr!=1 ) return 0; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1024 | if( pExpr->token.n!=3 ) return 0; |
| 1025 | if( sqliteStrNICmp(pExpr->token.z,"min",3)==0 ){ |
| 1026 | seekOp = OP_Rewind; |
| 1027 | }else if( sqliteStrNICmp(pExpr->token.z,"max",3)==0 ){ |
| 1028 | seekOp = OP_Last; |
| 1029 | }else{ |
| 1030 | return 0; |
| 1031 | } |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 1032 | pExpr = pExpr->pList->a[0].pExpr; |
| 1033 | if( pExpr->op!=TK_COLUMN ) return 0; |
| 1034 | iCol = pExpr->iColumn; |
| 1035 | pTab = p->pSrc->a[0].pTab; |
| 1036 | |
| 1037 | /* 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] | 1038 | ** Check to make sure we have an index and make pIdx point to the |
| 1039 | ** appropriate index. If the min() or max() is on an INTEGER PRIMARY |
| 1040 | ** key column, no index is necessary so set pIdx to NULL. If no |
| 1041 | ** usable index is found, return 0. |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 1042 | */ |
| 1043 | if( iCol<0 ){ |
| 1044 | pIdx = 0; |
| 1045 | }else{ |
| 1046 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 1047 | assert( pIdx->nColumn>=1 ); |
| 1048 | if( pIdx->aiColumn[0]==iCol ) break; |
| 1049 | } |
| 1050 | if( pIdx==0 ) return 0; |
| 1051 | } |
| 1052 | |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 1053 | /* Identify column names if we will be using the callback. This |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 1054 | ** step is skipped if the output is going to a table or a memory cell. |
| 1055 | */ |
| 1056 | v = sqliteGetVdbe(pParse); |
| 1057 | if( v==0 ) return 0; |
| 1058 | if( eDest==SRT_Callback ){ |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1059 | generateColumnNames(pParse, p->base, p->pSrc, p->pEList); |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 1060 | } |
| 1061 | |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 1062 | /* Generating code to find the min or the max. Basically all we have |
| 1063 | ** to do is find the first or the last entry in the chosen index. If |
| 1064 | ** the min() or max() is on the INTEGER PRIMARY KEY, then find the first |
| 1065 | ** or last entry in the main table. |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 1066 | */ |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 1067 | if( !pParse->schemaVerified && (pParse->db->flags & SQLITE_InTrans)==0 ){ |
| 1068 | sqliteVdbeAddOp(v, OP_VerifyCookie, pParse->db->schema_cookie, 0); |
| 1069 | pParse->schemaVerified = 1; |
| 1070 | } |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 1071 | openOp = pTab->isTemp ? OP_OpenAux : OP_Open; |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1072 | base = p->base; |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 1073 | sqliteVdbeAddOp(v, openOp, base, pTab->tnum); |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 1074 | sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC); |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 1075 | if( pIdx==0 ){ |
| 1076 | sqliteVdbeAddOp(v, seekOp, base, 0); |
| 1077 | }else{ |
| 1078 | sqliteVdbeAddOp(v, openOp, base+1, pIdx->tnum); |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 1079 | sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC); |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 1080 | sqliteVdbeAddOp(v, seekOp, base+1, 0); |
| 1081 | sqliteVdbeAddOp(v, OP_IdxRecno, base+1, 0); |
| 1082 | sqliteVdbeAddOp(v, OP_Close, base+1, 0); |
| 1083 | sqliteVdbeAddOp(v, OP_MoveTo, base, 0); |
| 1084 | } |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 1085 | eList.nExpr = 1; |
| 1086 | memset(&eListItem, 0, sizeof(eListItem)); |
| 1087 | eList.a = &eListItem; |
| 1088 | eList.a[0].pExpr = pExpr; |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 1089 | cont = sqliteVdbeMakeLabel(v); |
| 1090 | selectInnerLoop(pParse, &eList, base, 1, 0, -1, eDest, iParm, cont, cont); |
| 1091 | sqliteVdbeResolveLabel(v, cont); |
| 1092 | sqliteVdbeAddOp(v, OP_Close, base, 0); |
| 1093 | return 1; |
| 1094 | } |
| 1095 | |
| 1096 | /* |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 1097 | ** Generate code for the given SELECT statement. |
| 1098 | ** |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1099 | ** The results are distributed in various ways depending on the |
| 1100 | ** value of eDest and iParm. |
| 1101 | ** |
| 1102 | ** eDest Value Result |
| 1103 | ** ------------ ------------------------------------------- |
| 1104 | ** SRT_Callback Invoke the callback for each row of the result. |
| 1105 | ** |
| 1106 | ** SRT_Mem Store first result in memory cell iParm |
| 1107 | ** |
| 1108 | ** SRT_Set Store results as keys of a table with cursor iParm |
| 1109 | ** |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 1110 | ** SRT_Union Store results as a key in a temporary table iParm |
| 1111 | ** |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 1112 | ** SRT_Except Remove results form the temporary table iParm. |
| 1113 | ** |
| 1114 | ** SRT_Table Store results in temporary table iParm |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 1115 | ** |
| 1116 | ** This routine returns the number of errors. If any errors are |
| 1117 | ** encountered, then an appropriate error message is left in |
| 1118 | ** pParse->zErrMsg. |
| 1119 | ** |
| 1120 | ** This routine does NOT free the Select structure passed in. The |
| 1121 | ** calling function needs to do that. |
drh | 1b2e032 | 2002-03-03 02:49:51 +0000 | [diff] [blame] | 1122 | ** |
| 1123 | ** The pParent, parentTab, and *pParentAgg fields are filled in if this |
| 1124 | ** SELECT is a subquery. This routine may try to combine this SELECT |
| 1125 | ** with its parent to form a single flat query. In so doing, it might |
| 1126 | ** change the parent query from a non-aggregate to an aggregate query. |
| 1127 | ** For that reason, the pParentAgg flag is passed as a pointer, so it |
| 1128 | ** can be changed. |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 1129 | */ |
| 1130 | int sqliteSelect( |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1131 | Parse *pParse, /* The parser context */ |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 1132 | Select *p, /* The SELECT statement being coded. */ |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 1133 | int eDest, /* One of: SRT_Callback Mem Set Union Except */ |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1134 | int iParm, /* Save result in this memory location, if >=0 */ |
| 1135 | Select *pParent, /* Another SELECT for which this is a sub-query */ |
| 1136 | int parentTab, /* Index in pParent->pSrc of this query */ |
drh | 1b2e032 | 2002-03-03 02:49:51 +0000 | [diff] [blame] | 1137 | int *pParentAgg /* True if pParent uses aggregate functions */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1138 | ){ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1139 | int i; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1140 | WhereInfo *pWInfo; |
| 1141 | Vdbe *v; |
| 1142 | int isAgg = 0; /* True for select lists like "count(*)" */ |
drh | a2e0004 | 2002-01-22 03:13:42 +0000 | [diff] [blame] | 1143 | ExprList *pEList; /* List of columns to extract. */ |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 1144 | IdList *pTabList; /* List of tables to select from */ |
| 1145 | Expr *pWhere; /* The WHERE clause. May be NULL */ |
| 1146 | ExprList *pOrderBy; /* The ORDER BY clause. May be NULL */ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1147 | ExprList *pGroupBy; /* The GROUP BY clause. May be NULL */ |
| 1148 | Expr *pHaving; /* The HAVING clause. May be NULL */ |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 1149 | int isDistinct; /* True if the DISTINCT keyword is present */ |
| 1150 | int distinct; /* Table to use for the distinct set */ |
drh | 10e5e3c | 2000-06-08 00:19:02 +0000 | [diff] [blame] | 1151 | int base; /* First cursor available for use */ |
drh | 1d83f05 | 2002-02-17 00:30:36 +0000 | [diff] [blame] | 1152 | int rc = 1; /* Value to return from this function */ |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 1153 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1154 | if( sqlite_malloc_failed || pParse->nErr || p==0 ) return 1; |
| 1155 | |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 1156 | /* If there is are a sequence of queries, do the earlier ones first. |
| 1157 | */ |
| 1158 | if( p->pPrior ){ |
| 1159 | return multiSelect(pParse, p, eDest, iParm); |
| 1160 | } |
| 1161 | |
| 1162 | /* Make local copies of the parameters for this query. |
| 1163 | */ |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 1164 | pTabList = p->pSrc; |
| 1165 | pWhere = p->pWhere; |
| 1166 | pOrderBy = p->pOrderBy; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1167 | pGroupBy = p->pGroupBy; |
| 1168 | pHaving = p->pHaving; |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 1169 | isDistinct = p->isDistinct; |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 1170 | |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1171 | /* Allocate a block of VDBE cursors, one for each table in the FROM clause. |
| 1172 | ** The WHERE processing requires that the cursors for the tables in the |
| 1173 | ** FROM clause be consecutive. |
drh | 10e5e3c | 2000-06-08 00:19:02 +0000 | [diff] [blame] | 1174 | */ |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1175 | base = p->base = pParse->nTab; |
| 1176 | pParse->nTab += pTabList->nId; |
drh | 10e5e3c | 2000-06-08 00:19:02 +0000 | [diff] [blame] | 1177 | |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 1178 | /* |
| 1179 | ** Do not even attempt to generate any code if we have already seen |
| 1180 | ** errors before this routine starts. |
| 1181 | */ |
drh | 1d83f05 | 2002-02-17 00:30:36 +0000 | [diff] [blame] | 1182 | if( pParse->nErr>0 ) goto select_end; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1183 | |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1184 | /* Look up every table in the table list and create an appropriate |
| 1185 | ** columnlist in pEList if there isn't one already. (The parser leaves |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 1186 | ** a NULL in the p->pEList if the SQL said "SELECT * FROM ...") |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1187 | */ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1188 | if( fillInColumnList(pParse, p) ){ |
drh | 1d83f05 | 2002-02-17 00:30:36 +0000 | [diff] [blame] | 1189 | goto select_end; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1190 | } |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1191 | pEList = p->pEList; |
drh | 1d83f05 | 2002-02-17 00:30:36 +0000 | [diff] [blame] | 1192 | if( pEList==0 ) goto select_end; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1193 | |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1194 | /* If writing to memory or generating a set |
| 1195 | ** only a single column may be output. |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 1196 | */ |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1197 | if( (eDest==SRT_Mem || eDest==SRT_Set) && pEList->nExpr>1 ){ |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 1198 | sqliteSetString(&pParse->zErrMsg, "only a single result allowed for " |
| 1199 | "a SELECT that is part of an expression", 0); |
| 1200 | pParse->nErr++; |
drh | 1d83f05 | 2002-02-17 00:30:36 +0000 | [diff] [blame] | 1201 | goto select_end; |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 1202 | } |
| 1203 | |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1204 | /* ORDER BY is ignored if we are not sending the result to a callback. |
| 1205 | */ |
| 1206 | if( eDest!=SRT_Callback ){ |
drh | acd4c69 | 2002-03-07 02:02:51 +0000 | [diff] [blame] | 1207 | pOrderBy = 0; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1208 | } |
| 1209 | |
drh | 10e5e3c | 2000-06-08 00:19:02 +0000 | [diff] [blame] | 1210 | /* At this point, we should have allocated all the cursors that we |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1211 | ** need to handle subquerys and temporary tables. |
drh | 10e5e3c | 2000-06-08 00:19:02 +0000 | [diff] [blame] | 1212 | ** |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 1213 | ** Resolve the column names and do a semantics check on all the expressions. |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1214 | */ |
drh | 4794b98 | 2000-06-06 13:54:14 +0000 | [diff] [blame] | 1215 | for(i=0; i<pEList->nExpr; i++){ |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1216 | if( sqliteExprResolveIds(pParse, base, pTabList, 0, pEList->a[i].pExpr) ){ |
drh | 1d83f05 | 2002-02-17 00:30:36 +0000 | [diff] [blame] | 1217 | goto select_end; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1218 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1219 | if( sqliteExprCheck(pParse, pEList->a[i].pExpr, 1, &isAgg) ){ |
drh | 1d83f05 | 2002-02-17 00:30:36 +0000 | [diff] [blame] | 1220 | goto select_end; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1221 | } |
| 1222 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1223 | if( pWhere ){ |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1224 | if( sqliteExprResolveIds(pParse, base, pTabList, pEList, pWhere) ){ |
drh | 1d83f05 | 2002-02-17 00:30:36 +0000 | [diff] [blame] | 1225 | goto select_end; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1226 | } |
| 1227 | if( sqliteExprCheck(pParse, pWhere, 0, 0) ){ |
drh | 1d83f05 | 2002-02-17 00:30:36 +0000 | [diff] [blame] | 1228 | goto select_end; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1229 | } |
| 1230 | } |
| 1231 | if( pOrderBy ){ |
| 1232 | for(i=0; i<pOrderBy->nExpr; i++){ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1233 | Expr *pE = pOrderBy->a[i].pExpr; |
drh | 9208643 | 2002-01-22 14:11:29 +0000 | [diff] [blame] | 1234 | if( sqliteExprIsConstant(pE) ){ |
| 1235 | sqliteSetString(&pParse->zErrMsg, |
| 1236 | "ORDER BY expressions should not be constant", 0); |
| 1237 | pParse->nErr++; |
drh | 1d83f05 | 2002-02-17 00:30:36 +0000 | [diff] [blame] | 1238 | goto select_end; |
drh | 9208643 | 2002-01-22 14:11:29 +0000 | [diff] [blame] | 1239 | } |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1240 | if( sqliteExprResolveIds(pParse, base, pTabList, pEList, pE) ){ |
drh | 1d83f05 | 2002-02-17 00:30:36 +0000 | [diff] [blame] | 1241 | goto select_end; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1242 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1243 | if( sqliteExprCheck(pParse, pE, isAgg, 0) ){ |
drh | 1d83f05 | 2002-02-17 00:30:36 +0000 | [diff] [blame] | 1244 | goto select_end; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1245 | } |
| 1246 | } |
| 1247 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1248 | if( pGroupBy ){ |
| 1249 | for(i=0; i<pGroupBy->nExpr; i++){ |
| 1250 | Expr *pE = pGroupBy->a[i].pExpr; |
drh | 9208643 | 2002-01-22 14:11:29 +0000 | [diff] [blame] | 1251 | if( sqliteExprIsConstant(pE) ){ |
| 1252 | sqliteSetString(&pParse->zErrMsg, |
| 1253 | "GROUP BY expressions should not be constant", 0); |
| 1254 | pParse->nErr++; |
drh | 1d83f05 | 2002-02-17 00:30:36 +0000 | [diff] [blame] | 1255 | goto select_end; |
drh | 9208643 | 2002-01-22 14:11:29 +0000 | [diff] [blame] | 1256 | } |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1257 | if( sqliteExprResolveIds(pParse, base, pTabList, pEList, pE) ){ |
drh | 1d83f05 | 2002-02-17 00:30:36 +0000 | [diff] [blame] | 1258 | goto select_end; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1259 | } |
| 1260 | if( sqliteExprCheck(pParse, pE, isAgg, 0) ){ |
drh | 1d83f05 | 2002-02-17 00:30:36 +0000 | [diff] [blame] | 1261 | goto select_end; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1262 | } |
| 1263 | } |
| 1264 | } |
| 1265 | if( pHaving ){ |
| 1266 | if( pGroupBy==0 ){ |
drh | da93281 | 2000-06-06 18:00:15 +0000 | [diff] [blame] | 1267 | sqliteSetString(&pParse->zErrMsg, "a GROUP BY clause is required " |
| 1268 | "before HAVING", 0); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1269 | pParse->nErr++; |
drh | 1d83f05 | 2002-02-17 00:30:36 +0000 | [diff] [blame] | 1270 | goto select_end; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1271 | } |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1272 | if( sqliteExprResolveIds(pParse, base, pTabList, pEList, pHaving) ){ |
drh | 1d83f05 | 2002-02-17 00:30:36 +0000 | [diff] [blame] | 1273 | goto select_end; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1274 | } |
drh | da93281 | 2000-06-06 18:00:15 +0000 | [diff] [blame] | 1275 | if( sqliteExprCheck(pParse, pHaving, isAgg, 0) ){ |
drh | 1d83f05 | 2002-02-17 00:30:36 +0000 | [diff] [blame] | 1276 | goto select_end; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1277 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1278 | } |
| 1279 | |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 1280 | /* Check for the special case of a min() or max() function by itself |
| 1281 | ** in the result set. |
| 1282 | */ |
| 1283 | if( simpleMinMaxQuery(pParse, p, eDest, iParm) ){ |
drh | 5cf8e8c | 2002-02-19 22:42:05 +0000 | [diff] [blame] | 1284 | rc = 0; |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 1285 | goto select_end; |
| 1286 | } |
| 1287 | |
drh | d820cb1 | 2002-02-18 03:21:45 +0000 | [diff] [blame] | 1288 | /* Begin generating code. |
| 1289 | */ |
| 1290 | v = sqliteGetVdbe(pParse); |
| 1291 | if( v==0 ) goto select_end; |
| 1292 | |
| 1293 | /* Generate code for all sub-queries in the FROM clause |
| 1294 | */ |
| 1295 | for(i=0; i<pTabList->nId; i++){ |
drh | a76b5df | 2002-02-23 02:32:10 +0000 | [diff] [blame] | 1296 | if( pTabList->a[i].pSelect==0 ) continue; |
drh | 2d0794e | 2002-03-03 03:03:52 +0000 | [diff] [blame] | 1297 | sqliteSelect(pParse, pTabList->a[i].pSelect, SRT_TempTable, base+i, |
drh | 1b2e032 | 2002-03-03 02:49:51 +0000 | [diff] [blame] | 1298 | p, i, &isAgg); |
| 1299 | pTabList = p->pSrc; |
| 1300 | pWhere = p->pWhere; |
drh | acd4c69 | 2002-03-07 02:02:51 +0000 | [diff] [blame] | 1301 | if( eDest==SRT_Callback ){ |
| 1302 | pOrderBy = p->pOrderBy; |
| 1303 | } |
drh | 1b2e032 | 2002-03-03 02:49:51 +0000 | [diff] [blame] | 1304 | pGroupBy = p->pGroupBy; |
| 1305 | pHaving = p->pHaving; |
| 1306 | isDistinct = p->isDistinct; |
drh | d820cb1 | 2002-02-18 03:21:45 +0000 | [diff] [blame] | 1307 | } |
| 1308 | |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1309 | /* Check to see if this is a subquery that can be "flattened" into its parent. |
| 1310 | ** If flattening is a possiblity, do so and return immediately. |
| 1311 | */ |
drh | 1b2e032 | 2002-03-03 02:49:51 +0000 | [diff] [blame] | 1312 | if( pParent && pParentAgg && |
| 1313 | flattenSubquery(pParent, parentTab, *pParentAgg, isAgg) ){ |
| 1314 | if( isAgg ) *pParentAgg = 1; |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1315 | return rc; |
| 1316 | } |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1317 | |
drh | 2d0794e | 2002-03-03 03:03:52 +0000 | [diff] [blame] | 1318 | /* If the output is destined for a temporary table, open that table. |
| 1319 | */ |
| 1320 | if( eDest==SRT_TempTable ){ |
| 1321 | sqliteVdbeAddOp(v, OP_OpenTemp, iParm, 0); |
| 1322 | } |
| 1323 | |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1324 | /* Do an analysis of aggregate expressions. |
drh | efb7251 | 2000-05-31 20:00:52 +0000 | [diff] [blame] | 1325 | */ |
drh | d820cb1 | 2002-02-18 03:21:45 +0000 | [diff] [blame] | 1326 | sqliteAggregateInfoReset(pParse); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1327 | if( isAgg ){ |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1328 | assert( pParse->nAgg==0 ); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1329 | for(i=0; i<pEList->nExpr; i++){ |
| 1330 | if( sqliteExprAnalyzeAggregates(pParse, pEList->a[i].pExpr) ){ |
drh | 1d83f05 | 2002-02-17 00:30:36 +0000 | [diff] [blame] | 1331 | goto select_end; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1332 | } |
| 1333 | } |
| 1334 | if( pGroupBy ){ |
| 1335 | for(i=0; i<pGroupBy->nExpr; i++){ |
| 1336 | if( sqliteExprAnalyzeAggregates(pParse, pGroupBy->a[i].pExpr) ){ |
drh | 1d83f05 | 2002-02-17 00:30:36 +0000 | [diff] [blame] | 1337 | goto select_end; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1338 | } |
| 1339 | } |
| 1340 | } |
| 1341 | if( pHaving && sqliteExprAnalyzeAggregates(pParse, pHaving) ){ |
drh | 1d83f05 | 2002-02-17 00:30:36 +0000 | [diff] [blame] | 1342 | goto select_end; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1343 | } |
drh | 191b690 | 2000-06-08 11:13:01 +0000 | [diff] [blame] | 1344 | if( pOrderBy ){ |
| 1345 | for(i=0; i<pOrderBy->nExpr; i++){ |
| 1346 | if( sqliteExprAnalyzeAggregates(pParse, pOrderBy->a[i].pExpr) ){ |
drh | 1d83f05 | 2002-02-17 00:30:36 +0000 | [diff] [blame] | 1347 | goto select_end; |
drh | 191b690 | 2000-06-08 11:13:01 +0000 | [diff] [blame] | 1348 | } |
| 1349 | } |
| 1350 | } |
drh | efb7251 | 2000-05-31 20:00:52 +0000 | [diff] [blame] | 1351 | } |
| 1352 | |
drh | 9bbca4c | 2001-11-06 04:00:18 +0000 | [diff] [blame] | 1353 | /* Set the limiter |
| 1354 | */ |
| 1355 | if( p->nLimit<=0 ){ |
| 1356 | p->nOffset = 0; |
| 1357 | }else{ |
| 1358 | if( p->nOffset<0 ) p->nOffset = 0; |
| 1359 | sqliteVdbeAddOp(v, OP_Limit, p->nLimit, p->nOffset); |
| 1360 | } |
| 1361 | |
| 1362 | |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1363 | /* Identify column names if we will be using in the callback. This |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 1364 | ** 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] | 1365 | */ |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1366 | if( eDest==SRT_Callback ){ |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1367 | generateColumnNames(pParse, p->base, pTabList, pEList); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1368 | } |
| 1369 | |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1370 | /* Reset the aggregator |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1371 | */ |
| 1372 | if( isAgg ){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1373 | sqliteVdbeAddOp(v, OP_AggReset, 0, pParse->nAgg); |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 1374 | for(i=0; i<pParse->nAgg; i++){ |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1375 | FuncDef *pFunc; |
| 1376 | if( (pFunc = pParse->aAgg[i].pFunc)!=0 && pFunc->xFinalize!=0 ){ |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 1377 | sqliteVdbeAddOp(v, OP_AggInit, 0, i); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1378 | sqliteVdbeChangeP3(v, -1, (char*)pFunc, P3_POINTER); |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 1379 | } |
| 1380 | } |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 1381 | if( pGroupBy==0 ){ |
| 1382 | sqliteVdbeAddOp(v, OP_String, 0, 0); |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 1383 | sqliteVdbeAddOp(v, OP_AggFocus, 0, 0); |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 1384 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1385 | } |
| 1386 | |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 1387 | /* Initialize the memory cell to NULL |
| 1388 | */ |
drh | fef5208 | 2000-06-06 01:50:43 +0000 | [diff] [blame] | 1389 | if( eDest==SRT_Mem ){ |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1390 | sqliteVdbeAddOp(v, OP_String, 0, 0); |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 1391 | sqliteVdbeAddOp(v, OP_MemStore, iParm, 1); |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 1392 | } |
| 1393 | |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1394 | /* Open a temporary table to use for the distinct set. |
drh | efb7251 | 2000-05-31 20:00:52 +0000 | [diff] [blame] | 1395 | */ |
drh | 19a775c | 2000-06-05 18:54:46 +0000 | [diff] [blame] | 1396 | if( isDistinct ){ |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1397 | distinct = pParse->nTab++; |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 1398 | sqliteVdbeAddOp(v, OP_OpenTemp, distinct, 1); |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1399 | }else{ |
| 1400 | distinct = -1; |
drh | efb7251 | 2000-05-31 20:00:52 +0000 | [diff] [blame] | 1401 | } |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1402 | |
| 1403 | /* Begin the database scan |
| 1404 | */ |
| 1405 | pWInfo = sqliteWhereBegin(pParse, p->base, pTabList, pWhere, 0); |
drh | 1d83f05 | 2002-02-17 00:30:36 +0000 | [diff] [blame] | 1406 | if( pWInfo==0 ) goto select_end; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1407 | |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1408 | /* Use the standard inner loop if we are not dealing with |
| 1409 | ** aggregates |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1410 | */ |
drh | da9d6c4 | 2000-05-31 18:20:14 +0000 | [diff] [blame] | 1411 | if( !isAgg ){ |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 1412 | if( selectInnerLoop(pParse, pEList, 0, 0, pOrderBy, distinct, eDest, iParm, |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1413 | pWInfo->iContinue, pWInfo->iBreak) ){ |
drh | 1d83f05 | 2002-02-17 00:30:36 +0000 | [diff] [blame] | 1414 | goto select_end; |
drh | da9d6c4 | 2000-05-31 18:20:14 +0000 | [diff] [blame] | 1415 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1416 | } |
drh | efb7251 | 2000-05-31 20:00:52 +0000 | [diff] [blame] | 1417 | |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1418 | /* If we are dealing with aggregates, then to the special aggregate |
| 1419 | ** processing. |
drh | efb7251 | 2000-05-31 20:00:52 +0000 | [diff] [blame] | 1420 | */ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1421 | else{ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1422 | if( pGroupBy ){ |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 1423 | int lbl1; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1424 | for(i=0; i<pGroupBy->nExpr; i++){ |
| 1425 | sqliteExprCode(pParse, pGroupBy->a[i].pExpr); |
| 1426 | } |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1427 | sqliteVdbeAddOp(v, OP_MakeKey, pGroupBy->nExpr, 0); |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 1428 | lbl1 = sqliteVdbeMakeLabel(v); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1429 | sqliteVdbeAddOp(v, OP_AggFocus, 0, lbl1); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1430 | for(i=0; i<pParse->nAgg; i++){ |
| 1431 | if( pParse->aAgg[i].isAgg ) continue; |
| 1432 | sqliteExprCode(pParse, pParse->aAgg[i].pExpr); |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1433 | sqliteVdbeAddOp(v, OP_AggSet, 0, i); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1434 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1435 | sqliteVdbeResolveLabel(v, lbl1); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1436 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1437 | for(i=0; i<pParse->nAgg; i++){ |
| 1438 | Expr *pE; |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1439 | int j; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1440 | if( !pParse->aAgg[i].isAgg ) continue; |
| 1441 | pE = pParse->aAgg[i].pExpr; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1442 | assert( pE->op==TK_AGG_FUNCTION ); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1443 | if( pE->pList ){ |
| 1444 | for(j=0; j<pE->pList->nExpr; j++){ |
| 1445 | sqliteExprCode(pParse, pE->pList->a[j].pExpr); |
| 1446 | } |
drh | e509535 | 2002-02-24 03:25:14 +0000 | [diff] [blame] | 1447 | } |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1448 | sqliteVdbeAddOp(v, OP_Integer, i, 0); |
drh | f55f25f | 2002-02-28 01:46:11 +0000 | [diff] [blame] | 1449 | sqliteVdbeAddOp(v, OP_AggFunc, 0, pE->pList ? pE->pList->nExpr : 0); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 1450 | assert( pParse->aAgg[i].pFunc!=0 ); |
| 1451 | assert( pParse->aAgg[i].pFunc->xStep!=0 ); |
| 1452 | sqliteVdbeChangeP3(v, -1, (char*)pParse->aAgg[i].pFunc, P3_POINTER); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1453 | } |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1454 | } |
| 1455 | |
| 1456 | /* End the database scan loop. |
| 1457 | */ |
| 1458 | sqliteWhereEnd(pWInfo); |
| 1459 | |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1460 | /* If we are processing aggregates, we need to set up a second loop |
| 1461 | ** over all of the aggregate values and process them. |
| 1462 | */ |
| 1463 | if( isAgg ){ |
| 1464 | int endagg = sqliteVdbeMakeLabel(v); |
| 1465 | int startagg; |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1466 | startagg = sqliteVdbeAddOp(v, OP_AggNext, 0, endagg); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1467 | pParse->useAgg = 1; |
| 1468 | if( pHaving ){ |
| 1469 | sqliteExprIfFalse(pParse, pHaving, startagg); |
| 1470 | } |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 1471 | if( selectInnerLoop(pParse, pEList, 0, 0, pOrderBy, distinct, eDest, iParm, |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1472 | startagg, endagg) ){ |
drh | 1d83f05 | 2002-02-17 00:30:36 +0000 | [diff] [blame] | 1473 | goto select_end; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1474 | } |
drh | 99fcd71 | 2001-10-13 01:06:47 +0000 | [diff] [blame] | 1475 | sqliteVdbeAddOp(v, OP_Goto, 0, startagg); |
| 1476 | sqliteVdbeResolveLabel(v, endagg); |
| 1477 | sqliteVdbeAddOp(v, OP_Noop, 0, 0); |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1478 | pParse->useAgg = 0; |
| 1479 | } |
| 1480 | |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1481 | /* If there is an ORDER BY clause, then we need to sort the results |
| 1482 | ** and send them to the callback one by one. |
| 1483 | */ |
| 1484 | if( pOrderBy ){ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1485 | generateSortTail(v, pEList->nExpr); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1486 | } |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 1487 | |
| 1488 | |
| 1489 | /* Issue a null callback if that is what the user wants. |
| 1490 | */ |
| 1491 | if( (pParse->db->flags & SQLITE_NullCallback)!=0 && eDest==SRT_Callback ){ |
| 1492 | sqliteVdbeAddOp(v, OP_NullCallback, pEList->nExpr, 0); |
| 1493 | } |
| 1494 | |
drh | 1d83f05 | 2002-02-17 00:30:36 +0000 | [diff] [blame] | 1495 | /* The SELECT was successfully coded. Set the return code to 0 |
| 1496 | ** to indicate no errors. |
| 1497 | */ |
| 1498 | rc = 0; |
| 1499 | |
| 1500 | /* Control jumps to here if an error is encountered above, or upon |
| 1501 | ** successful coding of the SELECT. |
| 1502 | */ |
| 1503 | select_end: |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 1504 | pParse->nTab = base; |
drh | 1d83f05 | 2002-02-17 00:30:36 +0000 | [diff] [blame] | 1505 | sqliteAggregateInfoReset(pParse); |
| 1506 | return rc; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 1507 | } |