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