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